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;
89 /* List for set_ftrace_pid's pids. */
90 LIST_HEAD(ftrace_pids);
92 struct list_head list;
97 * ftrace_disabled is set when an anomaly is discovered.
98 * ftrace_disabled is much stronger than ftrace_enabled.
100 static int ftrace_disabled __read_mostly;
102 static DEFINE_MUTEX(ftrace_lock);
104 static struct ftrace_ops *ftrace_global_list __read_mostly = &ftrace_list_end;
105 static struct ftrace_ops *ftrace_control_list __read_mostly = &ftrace_list_end;
106 static struct ftrace_ops *ftrace_ops_list __read_mostly = &ftrace_list_end;
107 ftrace_func_t ftrace_trace_function __read_mostly = ftrace_stub;
108 ftrace_func_t ftrace_pid_function __read_mostly = ftrace_stub;
109 static struct ftrace_ops global_ops;
110 static struct ftrace_ops control_ops;
112 #if ARCH_SUPPORTS_FTRACE_OPS
113 static void ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip,
114 struct ftrace_ops *op, struct pt_regs *regs);
116 /* See comment below, where ftrace_ops_list_func is defined */
117 static void ftrace_ops_no_ops(unsigned long ip, unsigned long parent_ip);
118 #define ftrace_ops_list_func ((ftrace_func_t)ftrace_ops_no_ops)
122 * Traverse the ftrace_global_list, invoking all entries. The reason that we
123 * can use rcu_dereference_raw_notrace() is that elements removed from this list
124 * are simply leaked, so there is no need to interact with a grace-period
125 * mechanism. The rcu_dereference_raw_notrace() calls are needed to handle
126 * concurrent insertions into the ftrace_global_list.
128 * Silly Alpha and silly pointer-speculation compiler optimizations!
130 #define do_for_each_ftrace_op(op, list) \
131 op = rcu_dereference_raw_notrace(list); \
135 * Optimized for just a single item in the list (as that is the normal case).
137 #define while_for_each_ftrace_op(op) \
138 while (likely(op = rcu_dereference_raw_notrace((op)->next)) && \
139 unlikely((op) != &ftrace_list_end))
141 static inline void ftrace_ops_init(struct ftrace_ops *ops)
143 #ifdef CONFIG_DYNAMIC_FTRACE
144 if (!(ops->flags & FTRACE_OPS_FL_INITIALIZED)) {
145 mutex_init(&ops->regex_lock);
146 ops->flags |= FTRACE_OPS_FL_INITIALIZED;
152 * ftrace_nr_registered_ops - return number of ops registered
154 * Returns the number of ftrace_ops registered and tracing functions
156 int ftrace_nr_registered_ops(void)
158 struct ftrace_ops *ops;
161 mutex_lock(&ftrace_lock);
163 for (ops = ftrace_ops_list;
164 ops != &ftrace_list_end; ops = ops->next)
167 mutex_unlock(&ftrace_lock);
173 ftrace_global_list_func(unsigned long ip, unsigned long parent_ip,
174 struct ftrace_ops *op, struct pt_regs *regs)
178 bit = trace_test_and_set_recursion(TRACE_GLOBAL_START, TRACE_GLOBAL_MAX);
182 do_for_each_ftrace_op(op, ftrace_global_list) {
183 op->func(ip, parent_ip, op, regs);
184 } while_for_each_ftrace_op(op);
186 trace_clear_recursion(bit);
189 static void ftrace_pid_func(unsigned long ip, unsigned long parent_ip,
190 struct ftrace_ops *op, struct pt_regs *regs)
192 if (!test_tsk_trace_trace(current))
195 ftrace_pid_function(ip, parent_ip, op, regs);
198 static void set_ftrace_pid_function(ftrace_func_t func)
200 /* do not set ftrace_pid_function to itself! */
201 if (func != ftrace_pid_func)
202 ftrace_pid_function = func;
206 * clear_ftrace_function - reset the ftrace function
208 * This NULLs the ftrace function and in essence stops
209 * tracing. There may be lag
211 void clear_ftrace_function(void)
213 ftrace_trace_function = ftrace_stub;
214 ftrace_pid_function = ftrace_stub;
217 static void control_ops_disable_all(struct ftrace_ops *ops)
221 for_each_possible_cpu(cpu)
222 *per_cpu_ptr(ops->disabled, cpu) = 1;
225 static int control_ops_alloc(struct ftrace_ops *ops)
227 int __percpu *disabled;
229 disabled = alloc_percpu(int);
233 ops->disabled = disabled;
234 control_ops_disable_all(ops);
238 static void control_ops_free(struct ftrace_ops *ops)
240 free_percpu(ops->disabled);
243 static void update_global_ops(void)
248 * If there's only one function registered, then call that
249 * function directly. Otherwise, we need to iterate over the
250 * registered callers.
252 if (ftrace_global_list == &ftrace_list_end ||
253 ftrace_global_list->next == &ftrace_list_end) {
254 func = ftrace_global_list->func;
256 * As we are calling the function directly.
257 * If it does not have recursion protection,
258 * the function_trace_op needs to be updated
261 if (ftrace_global_list->flags & FTRACE_OPS_FL_RECURSION_SAFE)
262 global_ops.flags |= FTRACE_OPS_FL_RECURSION_SAFE;
264 global_ops.flags &= ~FTRACE_OPS_FL_RECURSION_SAFE;
266 func = ftrace_global_list_func;
267 /* The list has its own recursion protection. */
268 global_ops.flags |= FTRACE_OPS_FL_RECURSION_SAFE;
272 /* If we filter on pids, update to use the pid function */
273 if (!list_empty(&ftrace_pids)) {
274 set_ftrace_pid_function(func);
275 func = ftrace_pid_func;
278 global_ops.func = func;
281 static void update_ftrace_function(void)
288 * If we are at the end of the list and this ops is
289 * recursion safe and not dynamic and the arch supports passing ops,
290 * then have the mcount trampoline call the function directly.
292 if (ftrace_ops_list == &ftrace_list_end ||
293 (ftrace_ops_list->next == &ftrace_list_end &&
294 !(ftrace_ops_list->flags & FTRACE_OPS_FL_DYNAMIC) &&
295 (ftrace_ops_list->flags & FTRACE_OPS_FL_RECURSION_SAFE) &&
296 !FTRACE_FORCE_LIST_FUNC)) {
297 /* Set the ftrace_ops that the arch callback uses */
298 if (ftrace_ops_list == &global_ops)
299 function_trace_op = ftrace_global_list;
301 function_trace_op = ftrace_ops_list;
302 func = ftrace_ops_list->func;
304 /* Just use the default ftrace_ops */
305 function_trace_op = &ftrace_list_end;
306 func = ftrace_ops_list_func;
309 ftrace_trace_function = func;
312 static void add_ftrace_ops(struct ftrace_ops **list, struct ftrace_ops *ops)
316 * We are entering ops into the list but another
317 * CPU might be walking that list. We need to make sure
318 * the ops->next pointer is valid before another CPU sees
319 * the ops pointer included into the list.
321 rcu_assign_pointer(*list, ops);
324 static int remove_ftrace_ops(struct ftrace_ops **list, struct ftrace_ops *ops)
326 struct ftrace_ops **p;
329 * If we are removing the last function, then simply point
330 * to the ftrace_stub.
332 if (*list == ops && ops->next == &ftrace_list_end) {
333 *list = &ftrace_list_end;
337 for (p = list; *p != &ftrace_list_end; p = &(*p)->next)
348 static void add_ftrace_list_ops(struct ftrace_ops **list,
349 struct ftrace_ops *main_ops,
350 struct ftrace_ops *ops)
352 int first = *list == &ftrace_list_end;
353 add_ftrace_ops(list, ops);
355 add_ftrace_ops(&ftrace_ops_list, main_ops);
358 static int remove_ftrace_list_ops(struct ftrace_ops **list,
359 struct ftrace_ops *main_ops,
360 struct ftrace_ops *ops)
362 int ret = remove_ftrace_ops(list, ops);
363 if (!ret && *list == &ftrace_list_end)
364 ret = remove_ftrace_ops(&ftrace_ops_list, main_ops);
368 static int __register_ftrace_function(struct ftrace_ops *ops)
370 if (FTRACE_WARN_ON(ops == &global_ops))
373 if (WARN_ON(ops->flags & FTRACE_OPS_FL_ENABLED))
376 /* We don't support both control and global flags set. */
377 if ((ops->flags & FL_GLOBAL_CONTROL_MASK) == FL_GLOBAL_CONTROL_MASK)
380 #ifndef CONFIG_DYNAMIC_FTRACE_WITH_REGS
382 * If the ftrace_ops specifies SAVE_REGS, then it only can be used
383 * if the arch supports it, or SAVE_REGS_IF_SUPPORTED is also set.
384 * Setting SAVE_REGS_IF_SUPPORTED makes SAVE_REGS irrelevant.
386 if (ops->flags & FTRACE_OPS_FL_SAVE_REGS &&
387 !(ops->flags & FTRACE_OPS_FL_SAVE_REGS_IF_SUPPORTED))
390 if (ops->flags & FTRACE_OPS_FL_SAVE_REGS_IF_SUPPORTED)
391 ops->flags |= FTRACE_OPS_FL_SAVE_REGS;
394 if (!core_kernel_data((unsigned long)ops))
395 ops->flags |= FTRACE_OPS_FL_DYNAMIC;
397 if (ops->flags & FTRACE_OPS_FL_GLOBAL) {
398 add_ftrace_list_ops(&ftrace_global_list, &global_ops, ops);
399 ops->flags |= FTRACE_OPS_FL_ENABLED;
400 } else if (ops->flags & FTRACE_OPS_FL_CONTROL) {
401 if (control_ops_alloc(ops))
403 add_ftrace_list_ops(&ftrace_control_list, &control_ops, ops);
405 add_ftrace_ops(&ftrace_ops_list, ops);
408 update_ftrace_function();
413 static void ftrace_sync(struct work_struct *work)
416 * This function is just a stub to implement a hard force
417 * of synchronize_sched(). This requires synchronizing
418 * tasks even in userspace and idle.
420 * Yes, function tracing is rude.
424 static int __unregister_ftrace_function(struct ftrace_ops *ops)
428 if (WARN_ON(!(ops->flags & FTRACE_OPS_FL_ENABLED)))
431 if (FTRACE_WARN_ON(ops == &global_ops))
434 if (ops->flags & FTRACE_OPS_FL_GLOBAL) {
435 ret = remove_ftrace_list_ops(&ftrace_global_list,
438 ops->flags &= ~FTRACE_OPS_FL_ENABLED;
439 } else if (ops->flags & FTRACE_OPS_FL_CONTROL) {
440 ret = remove_ftrace_list_ops(&ftrace_control_list,
444 * The ftrace_ops is now removed from the list,
445 * so there'll be no new users. We must ensure
446 * all current users are done before we free
448 * Note synchronize_sched() is not enough, as we
449 * use preempt_disable() to do RCU, but the function
450 * tracer can be called where RCU is not active
451 * (before user_exit()).
453 schedule_on_each_cpu(ftrace_sync);
454 control_ops_free(ops);
457 ret = remove_ftrace_ops(&ftrace_ops_list, ops);
463 update_ftrace_function();
466 * Dynamic ops may be freed, we must make sure that all
467 * callers are done before leaving this function.
469 * Again, normal synchronize_sched() is not good enough.
470 * We need to do a hard force of sched synchronization.
472 if (ops->flags & FTRACE_OPS_FL_DYNAMIC)
473 schedule_on_each_cpu(ftrace_sync);
479 static void ftrace_update_pid_func(void)
481 /* Only do something if we are tracing something */
482 if (ftrace_trace_function == ftrace_stub)
485 update_ftrace_function();
488 #ifdef CONFIG_FUNCTION_PROFILER
489 struct ftrace_profile {
490 struct hlist_node node;
492 unsigned long counter;
493 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
494 unsigned long long time;
495 unsigned long long time_squared;
499 struct ftrace_profile_page {
500 struct ftrace_profile_page *next;
502 struct ftrace_profile records[];
505 struct ftrace_profile_stat {
507 struct hlist_head *hash;
508 struct ftrace_profile_page *pages;
509 struct ftrace_profile_page *start;
510 struct tracer_stat stat;
513 #define PROFILE_RECORDS_SIZE \
514 (PAGE_SIZE - offsetof(struct ftrace_profile_page, records))
516 #define PROFILES_PER_PAGE \
517 (PROFILE_RECORDS_SIZE / sizeof(struct ftrace_profile))
519 static int ftrace_profile_enabled __read_mostly;
521 /* ftrace_profile_lock - synchronize the enable and disable of the profiler */
522 static DEFINE_MUTEX(ftrace_profile_lock);
524 static DEFINE_PER_CPU(struct ftrace_profile_stat, ftrace_profile_stats);
526 #define FTRACE_PROFILE_HASH_BITS 10
527 #define FTRACE_PROFILE_HASH_SIZE (1 << FTRACE_PROFILE_HASH_BITS)
530 function_stat_next(void *v, int idx)
532 struct ftrace_profile *rec = v;
533 struct ftrace_profile_page *pg;
535 pg = (struct ftrace_profile_page *)((unsigned long)rec & PAGE_MASK);
541 if ((void *)rec >= (void *)&pg->records[pg->index]) {
545 rec = &pg->records[0];
553 static void *function_stat_start(struct tracer_stat *trace)
555 struct ftrace_profile_stat *stat =
556 container_of(trace, struct ftrace_profile_stat, stat);
558 if (!stat || !stat->start)
561 return function_stat_next(&stat->start->records[0], 0);
564 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
565 /* function graph compares on total time */
566 static int function_stat_cmp(void *p1, void *p2)
568 struct ftrace_profile *a = p1;
569 struct ftrace_profile *b = p2;
571 if (a->time < b->time)
573 if (a->time > b->time)
579 /* not function graph compares against hits */
580 static int function_stat_cmp(void *p1, void *p2)
582 struct ftrace_profile *a = p1;
583 struct ftrace_profile *b = p2;
585 if (a->counter < b->counter)
587 if (a->counter > b->counter)
594 static int function_stat_headers(struct seq_file *m)
596 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
597 seq_printf(m, " Function "
600 "--- ---- --- ---\n");
602 seq_printf(m, " Function Hit\n"
608 static int function_stat_show(struct seq_file *m, void *v)
610 struct ftrace_profile *rec = v;
611 char str[KSYM_SYMBOL_LEN];
613 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
614 static struct trace_seq s;
615 unsigned long long avg;
616 unsigned long long stddev;
618 mutex_lock(&ftrace_profile_lock);
620 /* we raced with function_profile_reset() */
621 if (unlikely(rec->counter == 0)) {
626 kallsyms_lookup(rec->ip, NULL, NULL, NULL, str);
627 seq_printf(m, " %-30.30s %10lu", str, rec->counter);
629 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
632 do_div(avg, rec->counter);
634 /* Sample standard deviation (s^2) */
635 if (rec->counter <= 1)
639 * Apply Welford's method:
640 * s^2 = 1 / (n * (n-1)) * (n * \Sum (x_i)^2 - (\Sum x_i)^2)
642 stddev = rec->counter * rec->time_squared -
643 rec->time * rec->time;
646 * Divide only 1000 for ns^2 -> us^2 conversion.
647 * trace_print_graph_duration will divide 1000 again.
649 do_div(stddev, rec->counter * (rec->counter - 1) * 1000);
653 trace_print_graph_duration(rec->time, &s);
654 trace_seq_puts(&s, " ");
655 trace_print_graph_duration(avg, &s);
656 trace_seq_puts(&s, " ");
657 trace_print_graph_duration(stddev, &s);
658 trace_print_seq(m, &s);
662 mutex_unlock(&ftrace_profile_lock);
667 static void ftrace_profile_reset(struct ftrace_profile_stat *stat)
669 struct ftrace_profile_page *pg;
671 pg = stat->pages = stat->start;
674 memset(pg->records, 0, PROFILE_RECORDS_SIZE);
679 memset(stat->hash, 0,
680 FTRACE_PROFILE_HASH_SIZE * sizeof(struct hlist_head));
683 int ftrace_profile_pages_init(struct ftrace_profile_stat *stat)
685 struct ftrace_profile_page *pg;
690 /* If we already allocated, do nothing */
694 stat->pages = (void *)get_zeroed_page(GFP_KERNEL);
698 #ifdef CONFIG_DYNAMIC_FTRACE
699 functions = ftrace_update_tot_cnt;
702 * We do not know the number of functions that exist because
703 * dynamic tracing is what counts them. With past experience
704 * we have around 20K functions. That should be more than enough.
705 * It is highly unlikely we will execute every function in
711 pg = stat->start = stat->pages;
713 pages = DIV_ROUND_UP(functions, PROFILES_PER_PAGE);
715 for (i = 1; i < pages; i++) {
716 pg->next = (void *)get_zeroed_page(GFP_KERNEL);
727 unsigned long tmp = (unsigned long)pg;
739 static int ftrace_profile_init_cpu(int cpu)
741 struct ftrace_profile_stat *stat;
744 stat = &per_cpu(ftrace_profile_stats, cpu);
747 /* If the profile is already created, simply reset it */
748 ftrace_profile_reset(stat);
753 * We are profiling all functions, but usually only a few thousand
754 * functions are hit. We'll make a hash of 1024 items.
756 size = FTRACE_PROFILE_HASH_SIZE;
758 stat->hash = kzalloc(sizeof(struct hlist_head) * size, GFP_KERNEL);
763 /* Preallocate the function profiling pages */
764 if (ftrace_profile_pages_init(stat) < 0) {
773 static int ftrace_profile_init(void)
778 for_each_online_cpu(cpu) {
779 ret = ftrace_profile_init_cpu(cpu);
787 /* interrupts must be disabled */
788 static struct ftrace_profile *
789 ftrace_find_profiled_func(struct ftrace_profile_stat *stat, unsigned long ip)
791 struct ftrace_profile *rec;
792 struct hlist_head *hhd;
795 key = hash_long(ip, FTRACE_PROFILE_HASH_BITS);
796 hhd = &stat->hash[key];
798 if (hlist_empty(hhd))
801 hlist_for_each_entry_rcu_notrace(rec, hhd, node) {
809 static void ftrace_add_profile(struct ftrace_profile_stat *stat,
810 struct ftrace_profile *rec)
814 key = hash_long(rec->ip, FTRACE_PROFILE_HASH_BITS);
815 hlist_add_head_rcu(&rec->node, &stat->hash[key]);
819 * The memory is already allocated, this simply finds a new record to use.
821 static struct ftrace_profile *
822 ftrace_profile_alloc(struct ftrace_profile_stat *stat, unsigned long ip)
824 struct ftrace_profile *rec = NULL;
826 /* prevent recursion (from NMIs) */
827 if (atomic_inc_return(&stat->disabled) != 1)
831 * Try to find the function again since an NMI
832 * could have added it
834 rec = ftrace_find_profiled_func(stat, ip);
838 if (stat->pages->index == PROFILES_PER_PAGE) {
839 if (!stat->pages->next)
841 stat->pages = stat->pages->next;
844 rec = &stat->pages->records[stat->pages->index++];
846 ftrace_add_profile(stat, rec);
849 atomic_dec(&stat->disabled);
855 function_profile_call(unsigned long ip, unsigned long parent_ip,
856 struct ftrace_ops *ops, struct pt_regs *regs)
858 struct ftrace_profile_stat *stat;
859 struct ftrace_profile *rec;
862 if (!ftrace_profile_enabled)
865 local_irq_save(flags);
867 stat = &__get_cpu_var(ftrace_profile_stats);
868 if (!stat->hash || !ftrace_profile_enabled)
871 rec = ftrace_find_profiled_func(stat, ip);
873 rec = ftrace_profile_alloc(stat, ip);
880 local_irq_restore(flags);
883 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
884 static int profile_graph_entry(struct ftrace_graph_ent *trace)
886 function_profile_call(trace->func, 0, NULL, NULL);
890 static void profile_graph_return(struct ftrace_graph_ret *trace)
892 struct ftrace_profile_stat *stat;
893 unsigned long long calltime;
894 struct ftrace_profile *rec;
897 local_irq_save(flags);
898 stat = &__get_cpu_var(ftrace_profile_stats);
899 if (!stat->hash || !ftrace_profile_enabled)
902 /* If the calltime was zero'd ignore it */
903 if (!trace->calltime)
906 calltime = trace->rettime - trace->calltime;
908 if (!(trace_flags & TRACE_ITER_GRAPH_TIME)) {
911 index = trace->depth;
913 /* Append this call time to the parent time to subtract */
915 current->ret_stack[index - 1].subtime += calltime;
917 if (current->ret_stack[index].subtime < calltime)
918 calltime -= current->ret_stack[index].subtime;
923 rec = ftrace_find_profiled_func(stat, trace->func);
925 rec->time += calltime;
926 rec->time_squared += calltime * calltime;
930 local_irq_restore(flags);
933 static int register_ftrace_profiler(void)
935 return register_ftrace_graph(&profile_graph_return,
936 &profile_graph_entry);
939 static void unregister_ftrace_profiler(void)
941 unregister_ftrace_graph();
944 static struct ftrace_ops ftrace_profile_ops __read_mostly = {
945 .func = function_profile_call,
946 .flags = FTRACE_OPS_FL_RECURSION_SAFE | FTRACE_OPS_FL_INITIALIZED,
947 INIT_REGEX_LOCK(ftrace_profile_ops)
950 static int register_ftrace_profiler(void)
952 return register_ftrace_function(&ftrace_profile_ops);
955 static void unregister_ftrace_profiler(void)
957 unregister_ftrace_function(&ftrace_profile_ops);
959 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
962 ftrace_profile_write(struct file *filp, const char __user *ubuf,
963 size_t cnt, loff_t *ppos)
968 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
974 mutex_lock(&ftrace_profile_lock);
975 if (ftrace_profile_enabled ^ val) {
977 ret = ftrace_profile_init();
983 ret = register_ftrace_profiler();
988 ftrace_profile_enabled = 1;
990 ftrace_profile_enabled = 0;
992 * unregister_ftrace_profiler calls stop_machine
993 * so this acts like an synchronize_sched.
995 unregister_ftrace_profiler();
999 mutex_unlock(&ftrace_profile_lock);
1007 ftrace_profile_read(struct file *filp, char __user *ubuf,
1008 size_t cnt, loff_t *ppos)
1010 char buf[64]; /* big enough to hold a number */
1013 r = sprintf(buf, "%u\n", ftrace_profile_enabled);
1014 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
1017 static const struct file_operations ftrace_profile_fops = {
1018 .open = tracing_open_generic,
1019 .read = ftrace_profile_read,
1020 .write = ftrace_profile_write,
1021 .llseek = default_llseek,
1024 /* used to initialize the real stat files */
1025 static struct tracer_stat function_stats __initdata = {
1026 .name = "functions",
1027 .stat_start = function_stat_start,
1028 .stat_next = function_stat_next,
1029 .stat_cmp = function_stat_cmp,
1030 .stat_headers = function_stat_headers,
1031 .stat_show = function_stat_show
1034 static __init void ftrace_profile_debugfs(struct dentry *d_tracer)
1036 struct ftrace_profile_stat *stat;
1037 struct dentry *entry;
1042 for_each_possible_cpu(cpu) {
1043 stat = &per_cpu(ftrace_profile_stats, cpu);
1045 /* allocate enough for function name + cpu number */
1046 name = kmalloc(32, GFP_KERNEL);
1049 * The files created are permanent, if something happens
1050 * we still do not free memory.
1053 "Could not allocate stat file for cpu %d\n",
1057 stat->stat = function_stats;
1058 snprintf(name, 32, "function%d", cpu);
1059 stat->stat.name = name;
1060 ret = register_stat_tracer(&stat->stat);
1063 "Could not register function stat for cpu %d\n",
1070 entry = debugfs_create_file("function_profile_enabled", 0644,
1071 d_tracer, NULL, &ftrace_profile_fops);
1073 pr_warning("Could not create debugfs "
1074 "'function_profile_enabled' entry\n");
1077 #else /* CONFIG_FUNCTION_PROFILER */
1078 static __init void ftrace_profile_debugfs(struct dentry *d_tracer)
1081 #endif /* CONFIG_FUNCTION_PROFILER */
1083 static struct pid * const ftrace_swapper_pid = &init_struct_pid;
1085 #ifdef CONFIG_DYNAMIC_FTRACE
1087 #ifndef CONFIG_FTRACE_MCOUNT_RECORD
1088 # error Dynamic ftrace depends on MCOUNT_RECORD
1091 static struct hlist_head ftrace_func_hash[FTRACE_FUNC_HASHSIZE] __read_mostly;
1093 struct ftrace_func_probe {
1094 struct hlist_node node;
1095 struct ftrace_probe_ops *ops;
1096 unsigned long flags;
1099 struct list_head free_list;
1102 struct ftrace_func_entry {
1103 struct hlist_node hlist;
1107 struct ftrace_hash {
1108 unsigned long size_bits;
1109 struct hlist_head *buckets;
1110 unsigned long count;
1111 struct rcu_head rcu;
1115 * We make these constant because no one should touch them,
1116 * but they are used as the default "empty hash", to avoid allocating
1117 * it all the time. These are in a read only section such that if
1118 * anyone does try to modify it, it will cause an exception.
1120 static const struct hlist_head empty_buckets[1];
1121 static const struct ftrace_hash empty_hash = {
1122 .buckets = (struct hlist_head *)empty_buckets,
1124 #define EMPTY_HASH ((struct ftrace_hash *)&empty_hash)
1126 static struct ftrace_ops global_ops = {
1127 .func = ftrace_stub,
1128 .notrace_hash = EMPTY_HASH,
1129 .filter_hash = EMPTY_HASH,
1130 .flags = FTRACE_OPS_FL_RECURSION_SAFE | FTRACE_OPS_FL_INITIALIZED,
1131 INIT_REGEX_LOCK(global_ops)
1134 struct ftrace_page {
1135 struct ftrace_page *next;
1136 struct dyn_ftrace *records;
1141 static struct ftrace_page *ftrace_new_pgs;
1143 #define ENTRY_SIZE sizeof(struct dyn_ftrace)
1144 #define ENTRIES_PER_PAGE (PAGE_SIZE / ENTRY_SIZE)
1146 /* estimate from running different kernels */
1147 #define NR_TO_INIT 10000
1149 static struct ftrace_page *ftrace_pages_start;
1150 static struct ftrace_page *ftrace_pages;
1152 static bool ftrace_hash_empty(struct ftrace_hash *hash)
1154 return !hash || !hash->count;
1157 static struct ftrace_func_entry *
1158 ftrace_lookup_ip(struct ftrace_hash *hash, unsigned long ip)
1161 struct ftrace_func_entry *entry;
1162 struct hlist_head *hhd;
1164 if (ftrace_hash_empty(hash))
1167 if (hash->size_bits > 0)
1168 key = hash_long(ip, hash->size_bits);
1172 hhd = &hash->buckets[key];
1174 hlist_for_each_entry_rcu_notrace(entry, hhd, hlist) {
1175 if (entry->ip == ip)
1181 static void __add_hash_entry(struct ftrace_hash *hash,
1182 struct ftrace_func_entry *entry)
1184 struct hlist_head *hhd;
1187 if (hash->size_bits)
1188 key = hash_long(entry->ip, hash->size_bits);
1192 hhd = &hash->buckets[key];
1193 hlist_add_head(&entry->hlist, hhd);
1197 static int add_hash_entry(struct ftrace_hash *hash, unsigned long ip)
1199 struct ftrace_func_entry *entry;
1201 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
1206 __add_hash_entry(hash, entry);
1212 free_hash_entry(struct ftrace_hash *hash,
1213 struct ftrace_func_entry *entry)
1215 hlist_del(&entry->hlist);
1221 remove_hash_entry(struct ftrace_hash *hash,
1222 struct ftrace_func_entry *entry)
1224 hlist_del(&entry->hlist);
1228 static void ftrace_hash_clear(struct ftrace_hash *hash)
1230 struct hlist_head *hhd;
1231 struct hlist_node *tn;
1232 struct ftrace_func_entry *entry;
1233 int size = 1 << hash->size_bits;
1239 for (i = 0; i < size; i++) {
1240 hhd = &hash->buckets[i];
1241 hlist_for_each_entry_safe(entry, tn, hhd, hlist)
1242 free_hash_entry(hash, entry);
1244 FTRACE_WARN_ON(hash->count);
1247 static void free_ftrace_hash(struct ftrace_hash *hash)
1249 if (!hash || hash == EMPTY_HASH)
1251 ftrace_hash_clear(hash);
1252 kfree(hash->buckets);
1256 static void __free_ftrace_hash_rcu(struct rcu_head *rcu)
1258 struct ftrace_hash *hash;
1260 hash = container_of(rcu, struct ftrace_hash, rcu);
1261 free_ftrace_hash(hash);
1264 static void free_ftrace_hash_rcu(struct ftrace_hash *hash)
1266 if (!hash || hash == EMPTY_HASH)
1268 call_rcu_sched(&hash->rcu, __free_ftrace_hash_rcu);
1271 void ftrace_free_filter(struct ftrace_ops *ops)
1273 ftrace_ops_init(ops);
1274 free_ftrace_hash(ops->filter_hash);
1275 free_ftrace_hash(ops->notrace_hash);
1278 static struct ftrace_hash *alloc_ftrace_hash(int size_bits)
1280 struct ftrace_hash *hash;
1283 hash = kzalloc(sizeof(*hash), GFP_KERNEL);
1287 size = 1 << size_bits;
1288 hash->buckets = kcalloc(size, sizeof(*hash->buckets), GFP_KERNEL);
1290 if (!hash->buckets) {
1295 hash->size_bits = size_bits;
1300 static struct ftrace_hash *
1301 alloc_and_copy_ftrace_hash(int size_bits, struct ftrace_hash *hash)
1303 struct ftrace_func_entry *entry;
1304 struct ftrace_hash *new_hash;
1309 new_hash = alloc_ftrace_hash(size_bits);
1314 if (ftrace_hash_empty(hash))
1317 size = 1 << hash->size_bits;
1318 for (i = 0; i < size; i++) {
1319 hlist_for_each_entry(entry, &hash->buckets[i], hlist) {
1320 ret = add_hash_entry(new_hash, entry->ip);
1326 FTRACE_WARN_ON(new_hash->count != hash->count);
1331 free_ftrace_hash(new_hash);
1336 ftrace_hash_rec_disable(struct ftrace_ops *ops, int filter_hash);
1338 ftrace_hash_rec_enable(struct ftrace_ops *ops, int filter_hash);
1341 ftrace_hash_move(struct ftrace_ops *ops, int enable,
1342 struct ftrace_hash **dst, struct ftrace_hash *src)
1344 struct ftrace_func_entry *entry;
1345 struct hlist_node *tn;
1346 struct hlist_head *hhd;
1347 struct ftrace_hash *old_hash;
1348 struct ftrace_hash *new_hash;
1349 int size = src->count;
1355 * Remove the current set, update the hash and add
1358 ftrace_hash_rec_disable(ops, enable);
1361 * If the new source is empty, just free dst and assign it
1365 free_ftrace_hash_rcu(*dst);
1366 rcu_assign_pointer(*dst, EMPTY_HASH);
1367 /* still need to update the function records */
1373 * Make the hash size about 1/2 the # found
1375 for (size /= 2; size; size >>= 1)
1378 /* Don't allocate too much */
1379 if (bits > FTRACE_HASH_MAX_BITS)
1380 bits = FTRACE_HASH_MAX_BITS;
1383 new_hash = alloc_ftrace_hash(bits);
1387 size = 1 << src->size_bits;
1388 for (i = 0; i < size; i++) {
1389 hhd = &src->buckets[i];
1390 hlist_for_each_entry_safe(entry, tn, hhd, hlist) {
1391 remove_hash_entry(src, entry);
1392 __add_hash_entry(new_hash, entry);
1397 rcu_assign_pointer(*dst, new_hash);
1398 free_ftrace_hash_rcu(old_hash);
1403 * Enable regardless of ret:
1404 * On success, we enable the new hash.
1405 * On failure, we re-enable the original hash.
1407 ftrace_hash_rec_enable(ops, enable);
1413 * Test the hashes for this ops to see if we want to call
1414 * the ops->func or not.
1416 * It's a match if the ip is in the ops->filter_hash or
1417 * the filter_hash does not exist or is empty,
1419 * the ip is not in the ops->notrace_hash.
1421 * This needs to be called with preemption disabled as
1422 * the hashes are freed with call_rcu_sched().
1425 ftrace_ops_test(struct ftrace_ops *ops, unsigned long ip, void *regs)
1427 struct ftrace_hash *filter_hash;
1428 struct ftrace_hash *notrace_hash;
1431 #ifdef CONFIG_DYNAMIC_FTRACE_WITH_REGS
1433 * There's a small race when adding ops that the ftrace handler
1434 * that wants regs, may be called without them. We can not
1435 * allow that handler to be called if regs is NULL.
1437 if (regs == NULL && (ops->flags & FTRACE_OPS_FL_SAVE_REGS))
1441 filter_hash = rcu_dereference_raw_notrace(ops->filter_hash);
1442 notrace_hash = rcu_dereference_raw_notrace(ops->notrace_hash);
1444 if ((ftrace_hash_empty(filter_hash) ||
1445 ftrace_lookup_ip(filter_hash, ip)) &&
1446 (ftrace_hash_empty(notrace_hash) ||
1447 !ftrace_lookup_ip(notrace_hash, ip)))
1456 * This is a double for. Do not use 'break' to break out of the loop,
1457 * you must use a goto.
1459 #define do_for_each_ftrace_rec(pg, rec) \
1460 for (pg = ftrace_pages_start; pg; pg = pg->next) { \
1462 for (_____i = 0; _____i < pg->index; _____i++) { \
1463 rec = &pg->records[_____i];
1465 #define while_for_each_ftrace_rec() \
1470 static int ftrace_cmp_recs(const void *a, const void *b)
1472 const struct dyn_ftrace *key = a;
1473 const struct dyn_ftrace *rec = b;
1475 if (key->flags < rec->ip)
1477 if (key->ip >= rec->ip + MCOUNT_INSN_SIZE)
1482 static unsigned long ftrace_location_range(unsigned long start, unsigned long end)
1484 struct ftrace_page *pg;
1485 struct dyn_ftrace *rec;
1486 struct dyn_ftrace key;
1489 key.flags = end; /* overload flags, as it is unsigned long */
1491 for (pg = ftrace_pages_start; pg; pg = pg->next) {
1492 if (end < pg->records[0].ip ||
1493 start >= (pg->records[pg->index - 1].ip + MCOUNT_INSN_SIZE))
1495 rec = bsearch(&key, pg->records, pg->index,
1496 sizeof(struct dyn_ftrace),
1506 * ftrace_location - return true if the ip giving is a traced location
1507 * @ip: the instruction pointer to check
1509 * Returns rec->ip if @ip given is a pointer to a ftrace location.
1510 * That is, the instruction that is either a NOP or call to
1511 * the function tracer. It checks the ftrace internal tables to
1512 * determine if the address belongs or not.
1514 unsigned long ftrace_location(unsigned long ip)
1516 return ftrace_location_range(ip, ip);
1520 * ftrace_text_reserved - return true if range contains an ftrace location
1521 * @start: start of range to search
1522 * @end: end of range to search (inclusive). @end points to the last byte to check.
1524 * Returns 1 if @start and @end contains a ftrace location.
1525 * That is, the instruction that is either a NOP or call to
1526 * the function tracer. It checks the ftrace internal tables to
1527 * determine if the address belongs or not.
1529 int ftrace_text_reserved(void *start, void *end)
1533 ret = ftrace_location_range((unsigned long)start,
1534 (unsigned long)end);
1539 static void __ftrace_hash_rec_update(struct ftrace_ops *ops,
1543 struct ftrace_hash *hash;
1544 struct ftrace_hash *other_hash;
1545 struct ftrace_page *pg;
1546 struct dyn_ftrace *rec;
1550 /* Only update if the ops has been registered */
1551 if (!(ops->flags & FTRACE_OPS_FL_ENABLED))
1555 * In the filter_hash case:
1556 * If the count is zero, we update all records.
1557 * Otherwise we just update the items in the hash.
1559 * In the notrace_hash case:
1560 * We enable the update in the hash.
1561 * As disabling notrace means enabling the tracing,
1562 * and enabling notrace means disabling, the inc variable
1566 hash = ops->filter_hash;
1567 other_hash = ops->notrace_hash;
1568 if (ftrace_hash_empty(hash))
1572 hash = ops->notrace_hash;
1573 other_hash = ops->filter_hash;
1575 * If the notrace hash has no items,
1576 * then there's nothing to do.
1578 if (ftrace_hash_empty(hash))
1582 do_for_each_ftrace_rec(pg, rec) {
1583 int in_other_hash = 0;
1589 * Only the filter_hash affects all records.
1590 * Update if the record is not in the notrace hash.
1592 if (!other_hash || !ftrace_lookup_ip(other_hash, rec->ip))
1595 in_hash = !!ftrace_lookup_ip(hash, rec->ip);
1596 in_other_hash = !!ftrace_lookup_ip(other_hash, rec->ip);
1601 if (filter_hash && in_hash && !in_other_hash)
1603 else if (!filter_hash && in_hash &&
1604 (in_other_hash || ftrace_hash_empty(other_hash)))
1612 if (FTRACE_WARN_ON((rec->flags & ~FTRACE_FL_MASK) == FTRACE_REF_MAX))
1615 * If any ops wants regs saved for this function
1616 * then all ops will get saved regs.
1618 if (ops->flags & FTRACE_OPS_FL_SAVE_REGS)
1619 rec->flags |= FTRACE_FL_REGS;
1621 if (FTRACE_WARN_ON((rec->flags & ~FTRACE_FL_MASK) == 0))
1626 /* Shortcut, if we handled all records, we are done. */
1627 if (!all && count == hash->count)
1629 } while_for_each_ftrace_rec();
1632 static void ftrace_hash_rec_disable(struct ftrace_ops *ops,
1635 __ftrace_hash_rec_update(ops, filter_hash, 0);
1638 static void ftrace_hash_rec_enable(struct ftrace_ops *ops,
1641 __ftrace_hash_rec_update(ops, filter_hash, 1);
1644 static void print_ip_ins(const char *fmt, unsigned char *p)
1648 printk(KERN_CONT "%s", fmt);
1650 for (i = 0; i < MCOUNT_INSN_SIZE; i++)
1651 printk(KERN_CONT "%s%02x", i ? ":" : "", p[i]);
1655 * ftrace_bug - report and shutdown function tracer
1656 * @failed: The failed type (EFAULT, EINVAL, EPERM)
1657 * @ip: The address that failed
1659 * The arch code that enables or disables the function tracing
1660 * can call ftrace_bug() when it has detected a problem in
1661 * modifying the code. @failed should be one of either:
1662 * EFAULT - if the problem happens on reading the @ip address
1663 * EINVAL - if what is read at @ip is not what was expected
1664 * EPERM - if the problem happens on writting to the @ip address
1666 void ftrace_bug(int failed, unsigned long ip)
1670 FTRACE_WARN_ON_ONCE(1);
1671 pr_info("ftrace faulted on modifying ");
1675 FTRACE_WARN_ON_ONCE(1);
1676 pr_info("ftrace failed to modify ");
1678 print_ip_ins(" actual: ", (unsigned char *)ip);
1679 printk(KERN_CONT "\n");
1682 FTRACE_WARN_ON_ONCE(1);
1683 pr_info("ftrace faulted on writing ");
1687 FTRACE_WARN_ON_ONCE(1);
1688 pr_info("ftrace faulted on unknown error ");
1693 static int ftrace_check_record(struct dyn_ftrace *rec, int enable, int update)
1695 unsigned long flag = 0UL;
1698 * If we are updating calls:
1700 * If the record has a ref count, then we need to enable it
1701 * because someone is using it.
1703 * Otherwise we make sure its disabled.
1705 * If we are disabling calls, then disable all records that
1708 if (enable && (rec->flags & ~FTRACE_FL_MASK))
1709 flag = FTRACE_FL_ENABLED;
1712 * If enabling and the REGS flag does not match the REGS_EN, then
1713 * do not ignore this record. Set flags to fail the compare against
1717 (!(rec->flags & FTRACE_FL_REGS) != !(rec->flags & FTRACE_FL_REGS_EN)))
1718 flag |= FTRACE_FL_REGS;
1720 /* If the state of this record hasn't changed, then do nothing */
1721 if ((rec->flags & FTRACE_FL_ENABLED) == flag)
1722 return FTRACE_UPDATE_IGNORE;
1725 /* Save off if rec is being enabled (for return value) */
1726 flag ^= rec->flags & FTRACE_FL_ENABLED;
1729 rec->flags |= FTRACE_FL_ENABLED;
1730 if (flag & FTRACE_FL_REGS) {
1731 if (rec->flags & FTRACE_FL_REGS)
1732 rec->flags |= FTRACE_FL_REGS_EN;
1734 rec->flags &= ~FTRACE_FL_REGS_EN;
1739 * If this record is being updated from a nop, then
1740 * return UPDATE_MAKE_CALL.
1741 * Otherwise, if the EN flag is set, then return
1742 * UPDATE_MODIFY_CALL_REGS to tell the caller to convert
1743 * from the non-save regs, to a save regs function.
1745 * return UPDATE_MODIFY_CALL to tell the caller to convert
1746 * from the save regs, to a non-save regs function.
1748 if (flag & FTRACE_FL_ENABLED)
1749 return FTRACE_UPDATE_MAKE_CALL;
1750 else if (rec->flags & FTRACE_FL_REGS_EN)
1751 return FTRACE_UPDATE_MODIFY_CALL_REGS;
1753 return FTRACE_UPDATE_MODIFY_CALL;
1757 /* If there's no more users, clear all flags */
1758 if (!(rec->flags & ~FTRACE_FL_MASK))
1761 /* Just disable the record (keep REGS state) */
1762 rec->flags &= ~FTRACE_FL_ENABLED;
1765 return FTRACE_UPDATE_MAKE_NOP;
1769 * ftrace_update_record, set a record that now is tracing or not
1770 * @rec: the record to update
1771 * @enable: set to 1 if the record is tracing, zero to force disable
1773 * The records that represent all functions that can be traced need
1774 * to be updated when tracing has been enabled.
1776 int ftrace_update_record(struct dyn_ftrace *rec, int enable)
1778 return ftrace_check_record(rec, enable, 1);
1782 * ftrace_test_record, check if the record has been enabled or not
1783 * @rec: the record to test
1784 * @enable: set to 1 to check if enabled, 0 if it is disabled
1786 * The arch code may need to test if a record is already set to
1787 * tracing to determine how to modify the function code that it
1790 int ftrace_test_record(struct dyn_ftrace *rec, int enable)
1792 return ftrace_check_record(rec, enable, 0);
1796 __ftrace_replace_code(struct dyn_ftrace *rec, int enable)
1798 unsigned long ftrace_old_addr;
1799 unsigned long ftrace_addr;
1802 ret = ftrace_update_record(rec, enable);
1804 if (rec->flags & FTRACE_FL_REGS)
1805 ftrace_addr = (unsigned long)FTRACE_REGS_ADDR;
1807 ftrace_addr = (unsigned long)FTRACE_ADDR;
1810 case FTRACE_UPDATE_IGNORE:
1813 case FTRACE_UPDATE_MAKE_CALL:
1814 return ftrace_make_call(rec, ftrace_addr);
1816 case FTRACE_UPDATE_MAKE_NOP:
1817 return ftrace_make_nop(NULL, rec, ftrace_addr);
1819 case FTRACE_UPDATE_MODIFY_CALL_REGS:
1820 case FTRACE_UPDATE_MODIFY_CALL:
1821 if (rec->flags & FTRACE_FL_REGS)
1822 ftrace_old_addr = (unsigned long)FTRACE_ADDR;
1824 ftrace_old_addr = (unsigned long)FTRACE_REGS_ADDR;
1826 return ftrace_modify_call(rec, ftrace_old_addr, ftrace_addr);
1829 return -1; /* unknow ftrace bug */
1832 void __weak ftrace_replace_code(int enable)
1834 struct dyn_ftrace *rec;
1835 struct ftrace_page *pg;
1838 if (unlikely(ftrace_disabled))
1841 do_for_each_ftrace_rec(pg, rec) {
1842 failed = __ftrace_replace_code(rec, enable);
1844 ftrace_bug(failed, rec->ip);
1845 /* Stop processing */
1848 } while_for_each_ftrace_rec();
1851 struct ftrace_rec_iter {
1852 struct ftrace_page *pg;
1857 * ftrace_rec_iter_start, start up iterating over traced functions
1859 * Returns an iterator handle that is used to iterate over all
1860 * the records that represent address locations where functions
1863 * May return NULL if no records are available.
1865 struct ftrace_rec_iter *ftrace_rec_iter_start(void)
1868 * We only use a single iterator.
1869 * Protected by the ftrace_lock mutex.
1871 static struct ftrace_rec_iter ftrace_rec_iter;
1872 struct ftrace_rec_iter *iter = &ftrace_rec_iter;
1874 iter->pg = ftrace_pages_start;
1877 /* Could have empty pages */
1878 while (iter->pg && !iter->pg->index)
1879 iter->pg = iter->pg->next;
1888 * ftrace_rec_iter_next, get the next record to process.
1889 * @iter: The handle to the iterator.
1891 * Returns the next iterator after the given iterator @iter.
1893 struct ftrace_rec_iter *ftrace_rec_iter_next(struct ftrace_rec_iter *iter)
1897 if (iter->index >= iter->pg->index) {
1898 iter->pg = iter->pg->next;
1901 /* Could have empty pages */
1902 while (iter->pg && !iter->pg->index)
1903 iter->pg = iter->pg->next;
1913 * ftrace_rec_iter_record, get the record at the iterator location
1914 * @iter: The current iterator location
1916 * Returns the record that the current @iter is at.
1918 struct dyn_ftrace *ftrace_rec_iter_record(struct ftrace_rec_iter *iter)
1920 return &iter->pg->records[iter->index];
1924 ftrace_code_disable(struct module *mod, struct dyn_ftrace *rec)
1931 if (unlikely(ftrace_disabled))
1934 ret = ftrace_make_nop(mod, rec, MCOUNT_ADDR);
1936 ftrace_bug(ret, ip);
1943 * archs can override this function if they must do something
1944 * before the modifying code is performed.
1946 int __weak ftrace_arch_code_modify_prepare(void)
1952 * archs can override this function if they must do something
1953 * after the modifying code is performed.
1955 int __weak ftrace_arch_code_modify_post_process(void)
1960 void ftrace_modify_all_code(int command)
1962 int update = command & FTRACE_UPDATE_TRACE_FUNC;
1965 * If the ftrace_caller calls a ftrace_ops func directly,
1966 * we need to make sure that it only traces functions it
1967 * expects to trace. When doing the switch of functions,
1968 * we need to update to the ftrace_ops_list_func first
1969 * before the transition between old and new calls are set,
1970 * as the ftrace_ops_list_func will check the ops hashes
1971 * to make sure the ops are having the right functions
1975 ftrace_update_ftrace_func(ftrace_ops_list_func);
1977 if (command & FTRACE_UPDATE_CALLS)
1978 ftrace_replace_code(1);
1979 else if (command & FTRACE_DISABLE_CALLS)
1980 ftrace_replace_code(0);
1982 if (update && ftrace_trace_function != ftrace_ops_list_func)
1983 ftrace_update_ftrace_func(ftrace_trace_function);
1985 if (command & FTRACE_START_FUNC_RET)
1986 ftrace_enable_ftrace_graph_caller();
1987 else if (command & FTRACE_STOP_FUNC_RET)
1988 ftrace_disable_ftrace_graph_caller();
1991 static int __ftrace_modify_code(void *data)
1993 int *command = data;
1995 ftrace_modify_all_code(*command);
2001 * ftrace_run_stop_machine, go back to the stop machine method
2002 * @command: The command to tell ftrace what to do
2004 * If an arch needs to fall back to the stop machine method, the
2005 * it can call this function.
2007 void ftrace_run_stop_machine(int command)
2009 stop_machine(__ftrace_modify_code, &command, NULL);
2013 * arch_ftrace_update_code, modify the code to trace or not trace
2014 * @command: The command that needs to be done
2016 * Archs can override this function if it does not need to
2017 * run stop_machine() to modify code.
2019 void __weak arch_ftrace_update_code(int command)
2021 ftrace_run_stop_machine(command);
2024 static void ftrace_run_update_code(int command)
2028 ret = ftrace_arch_code_modify_prepare();
2029 FTRACE_WARN_ON(ret);
2033 * Do not call function tracer while we update the code.
2034 * We are in stop machine.
2036 function_trace_stop++;
2039 * By default we use stop_machine() to modify the code.
2040 * But archs can do what ever they want as long as it
2041 * is safe. The stop_machine() is the safest, but also
2042 * produces the most overhead.
2044 arch_ftrace_update_code(command);
2046 function_trace_stop--;
2048 ret = ftrace_arch_code_modify_post_process();
2049 FTRACE_WARN_ON(ret);
2052 static ftrace_func_t saved_ftrace_func;
2053 static int ftrace_start_up;
2054 static int global_start_up;
2056 static void ftrace_startup_enable(int command)
2058 if (saved_ftrace_func != ftrace_trace_function) {
2059 saved_ftrace_func = ftrace_trace_function;
2060 command |= FTRACE_UPDATE_TRACE_FUNC;
2063 if (!command || !ftrace_enabled)
2066 ftrace_run_update_code(command);
2069 static int ftrace_startup(struct ftrace_ops *ops, int command)
2071 bool hash_enable = true;
2074 if (unlikely(ftrace_disabled))
2077 ret = __register_ftrace_function(ops);
2082 command |= FTRACE_UPDATE_CALLS;
2084 /* ops marked global share the filter hashes */
2085 if (ops->flags & FTRACE_OPS_FL_GLOBAL) {
2087 /* Don't update hash if global is already set */
2088 if (global_start_up)
2089 hash_enable = false;
2093 ops->flags |= FTRACE_OPS_FL_ENABLED;
2095 ftrace_hash_rec_enable(ops, 1);
2097 ftrace_startup_enable(command);
2102 static int ftrace_shutdown(struct ftrace_ops *ops, int command)
2104 bool hash_disable = true;
2107 if (unlikely(ftrace_disabled))
2110 ret = __unregister_ftrace_function(ops);
2116 * Just warn in case of unbalance, no need to kill ftrace, it's not
2117 * critical but the ftrace_call callers may be never nopped again after
2118 * further ftrace uses.
2120 WARN_ON_ONCE(ftrace_start_up < 0);
2122 if (ops->flags & FTRACE_OPS_FL_GLOBAL) {
2125 WARN_ON_ONCE(global_start_up < 0);
2126 /* Don't update hash if global still has users */
2127 if (global_start_up) {
2128 WARN_ON_ONCE(!ftrace_start_up);
2129 hash_disable = false;
2134 ftrace_hash_rec_disable(ops, 1);
2136 if (ops != &global_ops || !global_start_up)
2137 ops->flags &= ~FTRACE_OPS_FL_ENABLED;
2139 command |= FTRACE_UPDATE_CALLS;
2141 if (saved_ftrace_func != ftrace_trace_function) {
2142 saved_ftrace_func = ftrace_trace_function;
2143 command |= FTRACE_UPDATE_TRACE_FUNC;
2146 if (!command || !ftrace_enabled)
2149 ftrace_run_update_code(command);
2153 static void ftrace_startup_sysctl(void)
2155 if (unlikely(ftrace_disabled))
2158 /* Force update next time */
2159 saved_ftrace_func = NULL;
2160 /* ftrace_start_up is true if we want ftrace running */
2161 if (ftrace_start_up)
2162 ftrace_run_update_code(FTRACE_UPDATE_CALLS);
2165 static void ftrace_shutdown_sysctl(void)
2167 if (unlikely(ftrace_disabled))
2170 /* ftrace_start_up is true if ftrace is running */
2171 if (ftrace_start_up)
2172 ftrace_run_update_code(FTRACE_DISABLE_CALLS);
2175 static cycle_t ftrace_update_time;
2176 static unsigned long ftrace_update_cnt;
2177 unsigned long ftrace_update_tot_cnt;
2179 static inline int ops_traces_mod(struct ftrace_ops *ops)
2182 * Filter_hash being empty will default to trace module.
2183 * But notrace hash requires a test of individual module functions.
2185 return ftrace_hash_empty(ops->filter_hash) &&
2186 ftrace_hash_empty(ops->notrace_hash);
2190 * Check if the current ops references the record.
2192 * If the ops traces all functions, then it was already accounted for.
2193 * If the ops does not trace the current record function, skip it.
2194 * If the ops ignores the function via notrace filter, skip it.
2197 ops_references_rec(struct ftrace_ops *ops, struct dyn_ftrace *rec)
2199 /* If ops isn't enabled, ignore it */
2200 if (!(ops->flags & FTRACE_OPS_FL_ENABLED))
2203 /* If ops traces all mods, we already accounted for it */
2204 if (ops_traces_mod(ops))
2207 /* The function must be in the filter */
2208 if (!ftrace_hash_empty(ops->filter_hash) &&
2209 !ftrace_lookup_ip(ops->filter_hash, rec->ip))
2212 /* If in notrace hash, we ignore it too */
2213 if (ftrace_lookup_ip(ops->notrace_hash, rec->ip))
2219 static int referenced_filters(struct dyn_ftrace *rec)
2221 struct ftrace_ops *ops;
2224 for (ops = ftrace_ops_list; ops != &ftrace_list_end; ops = ops->next) {
2225 if (ops_references_rec(ops, rec))
2232 static int ftrace_update_code(struct module *mod)
2234 struct ftrace_page *pg;
2235 struct dyn_ftrace *p;
2236 cycle_t start, stop;
2237 unsigned long ref = 0;
2242 * When adding a module, we need to check if tracers are
2243 * currently enabled and if they are set to trace all functions.
2244 * If they are, we need to enable the module functions as well
2245 * as update the reference counts for those function records.
2248 struct ftrace_ops *ops;
2250 for (ops = ftrace_ops_list;
2251 ops != &ftrace_list_end; ops = ops->next) {
2252 if (ops->flags & FTRACE_OPS_FL_ENABLED) {
2253 if (ops_traces_mod(ops))
2261 start = ftrace_now(raw_smp_processor_id());
2262 ftrace_update_cnt = 0;
2264 for (pg = ftrace_new_pgs; pg; pg = pg->next) {
2266 for (i = 0; i < pg->index; i++) {
2269 /* If something went wrong, bail without enabling anything */
2270 if (unlikely(ftrace_disabled))
2273 p = &pg->records[i];
2275 cnt += referenced_filters(p);
2279 * Do the initial record conversion from mcount jump
2280 * to the NOP instructions.
2282 if (!ftrace_code_disable(mod, p))
2285 ftrace_update_cnt++;
2288 * If the tracing is enabled, go ahead and enable the record.
2290 * The reason not to enable the record immediatelly is the
2291 * inherent check of ftrace_make_nop/ftrace_make_call for
2292 * correct previous instructions. Making first the NOP
2293 * conversion puts the module to the correct state, thus
2294 * passing the ftrace_make_call check.
2296 if (ftrace_start_up && cnt) {
2297 int failed = __ftrace_replace_code(p, 1);
2299 ftrace_bug(failed, p->ip);
2304 ftrace_new_pgs = NULL;
2306 stop = ftrace_now(raw_smp_processor_id());
2307 ftrace_update_time = stop - start;
2308 ftrace_update_tot_cnt += ftrace_update_cnt;
2313 static int ftrace_allocate_records(struct ftrace_page *pg, int count)
2318 if (WARN_ON(!count))
2321 order = get_count_order(DIV_ROUND_UP(count, ENTRIES_PER_PAGE));
2324 * We want to fill as much as possible. No more than a page
2327 while ((PAGE_SIZE << order) / ENTRY_SIZE >= count + ENTRIES_PER_PAGE)
2331 pg->records = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, order);
2334 /* if we can't allocate this size, try something smaller */
2341 cnt = (PAGE_SIZE << order) / ENTRY_SIZE;
2350 static struct ftrace_page *
2351 ftrace_allocate_pages(unsigned long num_to_init)
2353 struct ftrace_page *start_pg;
2354 struct ftrace_page *pg;
2361 start_pg = pg = kzalloc(sizeof(*pg), GFP_KERNEL);
2366 * Try to allocate as much as possible in one continues
2367 * location that fills in all of the space. We want to
2368 * waste as little space as possible.
2371 cnt = ftrace_allocate_records(pg, num_to_init);
2379 pg->next = kzalloc(sizeof(*pg), GFP_KERNEL);
2390 order = get_count_order(pg->size / ENTRIES_PER_PAGE);
2391 free_pages((unsigned long)pg->records, order);
2392 start_pg = pg->next;
2396 pr_info("ftrace: FAILED to allocate memory for functions\n");
2400 static int __init ftrace_dyn_table_alloc(unsigned long num_to_init)
2405 pr_info("ftrace: No functions to be traced?\n");
2409 cnt = num_to_init / ENTRIES_PER_PAGE;
2410 pr_info("ftrace: allocating %ld entries in %d pages\n",
2411 num_to_init, cnt + 1);
2416 #define FTRACE_BUFF_MAX (KSYM_SYMBOL_LEN+4) /* room for wildcards */
2418 struct ftrace_iterator {
2421 struct ftrace_page *pg;
2422 struct dyn_ftrace *func;
2423 struct ftrace_func_probe *probe;
2424 struct trace_parser parser;
2425 struct ftrace_hash *hash;
2426 struct ftrace_ops *ops;
2433 t_hash_next(struct seq_file *m, loff_t *pos)
2435 struct ftrace_iterator *iter = m->private;
2436 struct hlist_node *hnd = NULL;
2437 struct hlist_head *hhd;
2443 hnd = &iter->probe->node;
2445 if (iter->hidx >= FTRACE_FUNC_HASHSIZE)
2448 hhd = &ftrace_func_hash[iter->hidx];
2450 if (hlist_empty(hhd)) {
2466 if (WARN_ON_ONCE(!hnd))
2469 iter->probe = hlist_entry(hnd, struct ftrace_func_probe, node);
2474 static void *t_hash_start(struct seq_file *m, loff_t *pos)
2476 struct ftrace_iterator *iter = m->private;
2480 if (!(iter->flags & FTRACE_ITER_DO_HASH))
2483 if (iter->func_pos > *pos)
2487 for (l = 0; l <= (*pos - iter->func_pos); ) {
2488 p = t_hash_next(m, &l);
2495 /* Only set this if we have an item */
2496 iter->flags |= FTRACE_ITER_HASH;
2502 t_hash_show(struct seq_file *m, struct ftrace_iterator *iter)
2504 struct ftrace_func_probe *rec;
2507 if (WARN_ON_ONCE(!rec))
2510 if (rec->ops->print)
2511 return rec->ops->print(m, rec->ip, rec->ops, rec->data);
2513 seq_printf(m, "%ps:%ps", (void *)rec->ip, (void *)rec->ops->func);
2516 seq_printf(m, ":%p", rec->data);
2523 t_next(struct seq_file *m, void *v, loff_t *pos)
2525 struct ftrace_iterator *iter = m->private;
2526 struct ftrace_ops *ops = iter->ops;
2527 struct dyn_ftrace *rec = NULL;
2529 if (unlikely(ftrace_disabled))
2532 if (iter->flags & FTRACE_ITER_HASH)
2533 return t_hash_next(m, pos);
2536 iter->pos = iter->func_pos = *pos;
2538 if (iter->flags & FTRACE_ITER_PRINTALL)
2539 return t_hash_start(m, pos);
2542 if (iter->idx >= iter->pg->index) {
2543 if (iter->pg->next) {
2544 iter->pg = iter->pg->next;
2549 rec = &iter->pg->records[iter->idx++];
2550 if (((iter->flags & FTRACE_ITER_FILTER) &&
2551 !(ftrace_lookup_ip(ops->filter_hash, rec->ip))) ||
2553 ((iter->flags & FTRACE_ITER_NOTRACE) &&
2554 !ftrace_lookup_ip(ops->notrace_hash, rec->ip)) ||
2556 ((iter->flags & FTRACE_ITER_ENABLED) &&
2557 !(rec->flags & FTRACE_FL_ENABLED))) {
2565 return t_hash_start(m, pos);
2572 static void reset_iter_read(struct ftrace_iterator *iter)
2576 iter->flags &= ~(FTRACE_ITER_PRINTALL | FTRACE_ITER_HASH);
2579 static void *t_start(struct seq_file *m, loff_t *pos)
2581 struct ftrace_iterator *iter = m->private;
2582 struct ftrace_ops *ops = iter->ops;
2586 mutex_lock(&ftrace_lock);
2588 if (unlikely(ftrace_disabled))
2592 * If an lseek was done, then reset and start from beginning.
2594 if (*pos < iter->pos)
2595 reset_iter_read(iter);
2598 * For set_ftrace_filter reading, if we have the filter
2599 * off, we can short cut and just print out that all
2600 * functions are enabled.
2602 if (iter->flags & FTRACE_ITER_FILTER &&
2603 ftrace_hash_empty(ops->filter_hash)) {
2605 return t_hash_start(m, pos);
2606 iter->flags |= FTRACE_ITER_PRINTALL;
2607 /* reset in case of seek/pread */
2608 iter->flags &= ~FTRACE_ITER_HASH;
2612 if (iter->flags & FTRACE_ITER_HASH)
2613 return t_hash_start(m, pos);
2616 * Unfortunately, we need to restart at ftrace_pages_start
2617 * every time we let go of the ftrace_mutex. This is because
2618 * those pointers can change without the lock.
2620 iter->pg = ftrace_pages_start;
2622 for (l = 0; l <= *pos; ) {
2623 p = t_next(m, p, &l);
2629 return t_hash_start(m, pos);
2634 static void t_stop(struct seq_file *m, void *p)
2636 mutex_unlock(&ftrace_lock);
2639 static int t_show(struct seq_file *m, void *v)
2641 struct ftrace_iterator *iter = m->private;
2642 struct dyn_ftrace *rec;
2644 if (iter->flags & FTRACE_ITER_HASH)
2645 return t_hash_show(m, iter);
2647 if (iter->flags & FTRACE_ITER_PRINTALL) {
2648 seq_printf(m, "#### all functions enabled ####\n");
2657 seq_printf(m, "%ps", (void *)rec->ip);
2658 if (iter->flags & FTRACE_ITER_ENABLED)
2659 seq_printf(m, " (%ld)%s",
2660 rec->flags & ~FTRACE_FL_MASK,
2661 rec->flags & FTRACE_FL_REGS ? " R" : "");
2662 seq_printf(m, "\n");
2667 static const struct seq_operations show_ftrace_seq_ops = {
2675 ftrace_avail_open(struct inode *inode, struct file *file)
2677 struct ftrace_iterator *iter;
2679 if (unlikely(ftrace_disabled))
2682 iter = __seq_open_private(file, &show_ftrace_seq_ops, sizeof(*iter));
2684 iter->pg = ftrace_pages_start;
2685 iter->ops = &global_ops;
2688 return iter ? 0 : -ENOMEM;
2692 ftrace_enabled_open(struct inode *inode, struct file *file)
2694 struct ftrace_iterator *iter;
2696 if (unlikely(ftrace_disabled))
2699 iter = __seq_open_private(file, &show_ftrace_seq_ops, sizeof(*iter));
2701 iter->pg = ftrace_pages_start;
2702 iter->flags = FTRACE_ITER_ENABLED;
2703 iter->ops = &global_ops;
2706 return iter ? 0 : -ENOMEM;
2709 static void ftrace_filter_reset(struct ftrace_hash *hash)
2711 mutex_lock(&ftrace_lock);
2712 ftrace_hash_clear(hash);
2713 mutex_unlock(&ftrace_lock);
2717 * ftrace_regex_open - initialize function tracer filter files
2718 * @ops: The ftrace_ops that hold the hash filters
2719 * @flag: The type of filter to process
2720 * @inode: The inode, usually passed in to your open routine
2721 * @file: The file, usually passed in to your open routine
2723 * ftrace_regex_open() initializes the filter files for the
2724 * @ops. Depending on @flag it may process the filter hash or
2725 * the notrace hash of @ops. With this called from the open
2726 * routine, you can use ftrace_filter_write() for the write
2727 * routine if @flag has FTRACE_ITER_FILTER set, or
2728 * ftrace_notrace_write() if @flag has FTRACE_ITER_NOTRACE set.
2729 * tracing_lseek() should be used as the lseek routine, and
2730 * release must call ftrace_regex_release().
2733 ftrace_regex_open(struct ftrace_ops *ops, int flag,
2734 struct inode *inode, struct file *file)
2736 struct ftrace_iterator *iter;
2737 struct ftrace_hash *hash;
2740 ftrace_ops_init(ops);
2742 if (unlikely(ftrace_disabled))
2745 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
2749 if (trace_parser_get_init(&iter->parser, FTRACE_BUFF_MAX)) {
2757 mutex_lock(&ops->regex_lock);
2759 if (flag & FTRACE_ITER_NOTRACE)
2760 hash = ops->notrace_hash;
2762 hash = ops->filter_hash;
2764 if (file->f_mode & FMODE_WRITE) {
2765 iter->hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, hash);
2767 trace_parser_put(&iter->parser);
2774 if ((file->f_mode & FMODE_WRITE) &&
2775 (file->f_flags & O_TRUNC))
2776 ftrace_filter_reset(iter->hash);
2778 if (file->f_mode & FMODE_READ) {
2779 iter->pg = ftrace_pages_start;
2781 ret = seq_open(file, &show_ftrace_seq_ops);
2783 struct seq_file *m = file->private_data;
2787 free_ftrace_hash(iter->hash);
2788 trace_parser_put(&iter->parser);
2792 file->private_data = iter;
2795 mutex_unlock(&ops->regex_lock);
2801 ftrace_filter_open(struct inode *inode, struct file *file)
2803 return ftrace_regex_open(&global_ops,
2804 FTRACE_ITER_FILTER | FTRACE_ITER_DO_HASH,
2809 ftrace_notrace_open(struct inode *inode, struct file *file)
2811 return ftrace_regex_open(&global_ops, FTRACE_ITER_NOTRACE,
2815 static int ftrace_match(char *str, char *regex, int len, int type)
2822 if (strcmp(str, regex) == 0)
2825 case MATCH_FRONT_ONLY:
2826 if (strncmp(str, regex, len) == 0)
2829 case MATCH_MIDDLE_ONLY:
2830 if (strstr(str, regex))
2833 case MATCH_END_ONLY:
2835 if (slen >= len && memcmp(str + slen - len, regex, len) == 0)
2844 enter_record(struct ftrace_hash *hash, struct dyn_ftrace *rec, int not)
2846 struct ftrace_func_entry *entry;
2849 entry = ftrace_lookup_ip(hash, rec->ip);
2851 /* Do nothing if it doesn't exist */
2855 free_hash_entry(hash, entry);
2857 /* Do nothing if it exists */
2861 ret = add_hash_entry(hash, rec->ip);
2867 ftrace_match_record(struct dyn_ftrace *rec, char *mod,
2868 char *regex, int len, int type)
2870 char str[KSYM_SYMBOL_LEN];
2873 kallsyms_lookup(rec->ip, NULL, NULL, &modname, str);
2876 /* module lookup requires matching the module */
2877 if (!modname || strcmp(modname, mod))
2880 /* blank search means to match all funcs in the mod */
2885 return ftrace_match(str, regex, len, type);
2889 match_records(struct ftrace_hash *hash, char *buff,
2890 int len, char *mod, int not)
2892 unsigned search_len = 0;
2893 struct ftrace_page *pg;
2894 struct dyn_ftrace *rec;
2895 int type = MATCH_FULL;
2896 char *search = buff;
2901 type = filter_parse_regex(buff, len, &search, ¬);
2902 search_len = strlen(search);
2905 mutex_lock(&ftrace_lock);
2907 if (unlikely(ftrace_disabled))
2910 do_for_each_ftrace_rec(pg, rec) {
2911 if (ftrace_match_record(rec, mod, search, search_len, type)) {
2912 ret = enter_record(hash, rec, not);
2919 } while_for_each_ftrace_rec();
2921 mutex_unlock(&ftrace_lock);
2927 ftrace_match_records(struct ftrace_hash *hash, char *buff, int len)
2929 return match_records(hash, buff, len, NULL, 0);
2933 ftrace_match_module_records(struct ftrace_hash *hash, char *buff, char *mod)
2937 /* blank or '*' mean the same */
2938 if (strcmp(buff, "*") == 0)
2941 /* handle the case of 'dont filter this module' */
2942 if (strcmp(buff, "!") == 0 || strcmp(buff, "!*") == 0) {
2947 return match_records(hash, buff, strlen(buff), mod, not);
2951 * We register the module command as a template to show others how
2952 * to register the a command as well.
2956 ftrace_mod_callback(struct ftrace_hash *hash,
2957 char *func, char *cmd, char *param, int enable)
2963 * cmd == 'mod' because we only registered this func
2964 * for the 'mod' ftrace_func_command.
2965 * But if you register one func with multiple commands,
2966 * you can tell which command was used by the cmd
2970 /* we must have a module name */
2974 mod = strsep(¶m, ":");
2978 ret = ftrace_match_module_records(hash, func, mod);
2987 static struct ftrace_func_command ftrace_mod_cmd = {
2989 .func = ftrace_mod_callback,
2992 static int __init ftrace_mod_cmd_init(void)
2994 return register_ftrace_command(&ftrace_mod_cmd);
2996 core_initcall(ftrace_mod_cmd_init);
2998 static void function_trace_probe_call(unsigned long ip, unsigned long parent_ip,
2999 struct ftrace_ops *op, struct pt_regs *pt_regs)
3001 struct ftrace_func_probe *entry;
3002 struct hlist_head *hhd;
3005 key = hash_long(ip, FTRACE_HASH_BITS);
3007 hhd = &ftrace_func_hash[key];
3009 if (hlist_empty(hhd))
3013 * Disable preemption for these calls to prevent a RCU grace
3014 * period. This syncs the hash iteration and freeing of items
3015 * on the hash. rcu_read_lock is too dangerous here.
3017 preempt_disable_notrace();
3018 hlist_for_each_entry_rcu_notrace(entry, hhd, node) {
3019 if (entry->ip == ip)
3020 entry->ops->func(ip, parent_ip, &entry->data);
3022 preempt_enable_notrace();
3025 static struct ftrace_ops trace_probe_ops __read_mostly =
3027 .func = function_trace_probe_call,
3028 .flags = FTRACE_OPS_FL_INITIALIZED,
3029 INIT_REGEX_LOCK(trace_probe_ops)
3032 static int ftrace_probe_registered;
3034 static void __enable_ftrace_function_probe(void)
3039 if (ftrace_probe_registered) {
3040 /* still need to update the function call sites */
3042 ftrace_run_update_code(FTRACE_UPDATE_CALLS);
3046 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
3047 struct hlist_head *hhd = &ftrace_func_hash[i];
3051 /* Nothing registered? */
3052 if (i == FTRACE_FUNC_HASHSIZE)
3055 ret = ftrace_startup(&trace_probe_ops, 0);
3057 ftrace_probe_registered = 1;
3060 static void __disable_ftrace_function_probe(void)
3064 if (!ftrace_probe_registered)
3067 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
3068 struct hlist_head *hhd = &ftrace_func_hash[i];
3073 /* no more funcs left */
3074 ftrace_shutdown(&trace_probe_ops, 0);
3076 ftrace_probe_registered = 0;
3080 static void ftrace_free_entry(struct ftrace_func_probe *entry)
3082 if (entry->ops->free)
3083 entry->ops->free(entry->ops, entry->ip, &entry->data);
3088 register_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
3091 struct ftrace_func_probe *entry;
3092 struct ftrace_hash **orig_hash = &trace_probe_ops.filter_hash;
3093 struct ftrace_hash *hash;
3094 struct ftrace_page *pg;
3095 struct dyn_ftrace *rec;
3102 type = filter_parse_regex(glob, strlen(glob), &search, ¬);
3103 len = strlen(search);
3105 /* we do not support '!' for function probes */
3109 mutex_lock(&trace_probe_ops.regex_lock);
3111 hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, *orig_hash);
3117 if (unlikely(ftrace_disabled)) {
3122 mutex_lock(&ftrace_lock);
3124 do_for_each_ftrace_rec(pg, rec) {
3126 if (!ftrace_match_record(rec, NULL, search, len, type))
3129 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
3131 /* If we did not process any, then return error */
3142 * The caller might want to do something special
3143 * for each function we find. We call the callback
3144 * to give the caller an opportunity to do so.
3147 if (ops->init(ops, rec->ip, &entry->data) < 0) {
3148 /* caller does not like this func */
3154 ret = enter_record(hash, rec, 0);
3162 entry->ip = rec->ip;
3164 key = hash_long(entry->ip, FTRACE_HASH_BITS);
3165 hlist_add_head_rcu(&entry->node, &ftrace_func_hash[key]);
3167 } while_for_each_ftrace_rec();
3169 ret = ftrace_hash_move(&trace_probe_ops, 1, orig_hash, hash);
3173 __enable_ftrace_function_probe();
3176 mutex_unlock(&ftrace_lock);
3178 mutex_unlock(&trace_probe_ops.regex_lock);
3179 free_ftrace_hash(hash);
3185 PROBE_TEST_FUNC = 1,
3190 __unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
3191 void *data, int flags)
3193 struct ftrace_func_entry *rec_entry;
3194 struct ftrace_func_probe *entry;
3195 struct ftrace_func_probe *p;
3196 struct ftrace_hash **orig_hash = &trace_probe_ops.filter_hash;
3197 struct list_head free_list;
3198 struct ftrace_hash *hash;
3199 struct hlist_node *tmp;
3200 char str[KSYM_SYMBOL_LEN];
3201 int type = MATCH_FULL;
3205 if (glob && (strcmp(glob, "*") == 0 || !strlen(glob)))
3210 type = filter_parse_regex(glob, strlen(glob), &search, ¬);
3211 len = strlen(search);
3213 /* we do not support '!' for function probes */
3218 mutex_lock(&trace_probe_ops.regex_lock);
3220 hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, *orig_hash);
3222 /* Hmm, should report this somehow */
3225 INIT_LIST_HEAD(&free_list);
3227 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
3228 struct hlist_head *hhd = &ftrace_func_hash[i];
3230 hlist_for_each_entry_safe(entry, tmp, hhd, node) {
3232 /* break up if statements for readability */
3233 if ((flags & PROBE_TEST_FUNC) && entry->ops != ops)
3236 if ((flags & PROBE_TEST_DATA) && entry->data != data)
3239 /* do this last, since it is the most expensive */
3241 kallsyms_lookup(entry->ip, NULL, NULL,
3243 if (!ftrace_match(str, glob, len, type))
3247 rec_entry = ftrace_lookup_ip(hash, entry->ip);
3248 /* It is possible more than one entry had this ip */
3250 free_hash_entry(hash, rec_entry);
3252 hlist_del_rcu(&entry->node);
3253 list_add(&entry->free_list, &free_list);
3256 mutex_lock(&ftrace_lock);
3257 __disable_ftrace_function_probe();
3259 * Remove after the disable is called. Otherwise, if the last
3260 * probe is removed, a null hash means *all enabled*.
3262 ftrace_hash_move(&trace_probe_ops, 1, orig_hash, hash);
3263 synchronize_sched();
3264 list_for_each_entry_safe(entry, p, &free_list, free_list) {
3265 list_del(&entry->free_list);
3266 ftrace_free_entry(entry);
3268 mutex_unlock(&ftrace_lock);
3271 mutex_unlock(&trace_probe_ops.regex_lock);
3272 free_ftrace_hash(hash);
3276 unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
3279 __unregister_ftrace_function_probe(glob, ops, data,
3280 PROBE_TEST_FUNC | PROBE_TEST_DATA);
3284 unregister_ftrace_function_probe_func(char *glob, struct ftrace_probe_ops *ops)
3286 __unregister_ftrace_function_probe(glob, ops, NULL, PROBE_TEST_FUNC);
3289 void unregister_ftrace_function_probe_all(char *glob)
3291 __unregister_ftrace_function_probe(glob, NULL, NULL, 0);
3294 static LIST_HEAD(ftrace_commands);
3295 static DEFINE_MUTEX(ftrace_cmd_mutex);
3298 * Currently we only register ftrace commands from __init, so mark this
3301 __init int register_ftrace_command(struct ftrace_func_command *cmd)
3303 struct ftrace_func_command *p;
3306 mutex_lock(&ftrace_cmd_mutex);
3307 list_for_each_entry(p, &ftrace_commands, list) {
3308 if (strcmp(cmd->name, p->name) == 0) {
3313 list_add(&cmd->list, &ftrace_commands);
3315 mutex_unlock(&ftrace_cmd_mutex);
3321 * Currently we only unregister ftrace commands from __init, so mark
3324 __init int unregister_ftrace_command(struct ftrace_func_command *cmd)
3326 struct ftrace_func_command *p, *n;
3329 mutex_lock(&ftrace_cmd_mutex);
3330 list_for_each_entry_safe(p, n, &ftrace_commands, list) {
3331 if (strcmp(cmd->name, p->name) == 0) {
3333 list_del_init(&p->list);
3338 mutex_unlock(&ftrace_cmd_mutex);
3343 static int ftrace_process_regex(struct ftrace_hash *hash,
3344 char *buff, int len, int enable)
3346 char *func, *command, *next = buff;
3347 struct ftrace_func_command *p;
3350 func = strsep(&next, ":");
3353 ret = ftrace_match_records(hash, func, len);
3363 command = strsep(&next, ":");
3365 mutex_lock(&ftrace_cmd_mutex);
3366 list_for_each_entry(p, &ftrace_commands, list) {
3367 if (strcmp(p->name, command) == 0) {
3368 ret = p->func(hash, func, command, next, enable);
3373 mutex_unlock(&ftrace_cmd_mutex);
3379 ftrace_regex_write(struct file *file, const char __user *ubuf,
3380 size_t cnt, loff_t *ppos, int enable)
3382 struct ftrace_iterator *iter;
3383 struct trace_parser *parser;
3389 if (file->f_mode & FMODE_READ) {
3390 struct seq_file *m = file->private_data;
3393 iter = file->private_data;
3395 if (unlikely(ftrace_disabled))
3398 /* iter->hash is a local copy, so we don't need regex_lock */
3400 parser = &iter->parser;
3401 read = trace_get_user(parser, ubuf, cnt, ppos);
3403 if (read >= 0 && trace_parser_loaded(parser) &&
3404 !trace_parser_cont(parser)) {
3405 ret = ftrace_process_regex(iter->hash, parser->buffer,
3406 parser->idx, enable);
3407 trace_parser_clear(parser);
3418 ftrace_filter_write(struct file *file, const char __user *ubuf,
3419 size_t cnt, loff_t *ppos)
3421 return ftrace_regex_write(file, ubuf, cnt, ppos, 1);
3425 ftrace_notrace_write(struct file *file, const char __user *ubuf,
3426 size_t cnt, loff_t *ppos)
3428 return ftrace_regex_write(file, ubuf, cnt, ppos, 0);
3432 ftrace_match_addr(struct ftrace_hash *hash, unsigned long ip, int remove)
3434 struct ftrace_func_entry *entry;
3436 if (!ftrace_location(ip))
3440 entry = ftrace_lookup_ip(hash, ip);
3443 free_hash_entry(hash, entry);
3447 return add_hash_entry(hash, ip);
3450 static void ftrace_ops_update_code(struct ftrace_ops *ops)
3452 if (ops->flags & FTRACE_OPS_FL_ENABLED && ftrace_enabled)
3453 ftrace_run_update_code(FTRACE_UPDATE_CALLS);
3457 ftrace_set_hash(struct ftrace_ops *ops, unsigned char *buf, int len,
3458 unsigned long ip, int remove, int reset, int enable)
3460 struct ftrace_hash **orig_hash;
3461 struct ftrace_hash *hash;
3464 /* All global ops uses the global ops filters */
3465 if (ops->flags & FTRACE_OPS_FL_GLOBAL)
3468 if (unlikely(ftrace_disabled))
3471 mutex_lock(&ops->regex_lock);
3474 orig_hash = &ops->filter_hash;
3476 orig_hash = &ops->notrace_hash;
3478 hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, *orig_hash);
3481 goto out_regex_unlock;
3485 ftrace_filter_reset(hash);
3486 if (buf && !ftrace_match_records(hash, buf, len)) {
3488 goto out_regex_unlock;
3491 ret = ftrace_match_addr(hash, ip, remove);
3493 goto out_regex_unlock;
3496 mutex_lock(&ftrace_lock);
3497 ret = ftrace_hash_move(ops, enable, orig_hash, hash);
3499 ftrace_ops_update_code(ops);
3501 mutex_unlock(&ftrace_lock);
3504 mutex_unlock(&ops->regex_lock);
3506 free_ftrace_hash(hash);
3511 ftrace_set_addr(struct ftrace_ops *ops, unsigned long ip, int remove,
3512 int reset, int enable)
3514 return ftrace_set_hash(ops, 0, 0, ip, remove, reset, enable);
3518 * ftrace_set_filter_ip - set a function to filter on in ftrace by address
3519 * @ops - the ops to set the filter with
3520 * @ip - the address to add to or remove from the filter.
3521 * @remove - non zero to remove the ip from the filter
3522 * @reset - non zero to reset all filters before applying this filter.
3524 * Filters denote which functions should be enabled when tracing is enabled
3525 * If @ip is NULL, it failes to update filter.
3527 int ftrace_set_filter_ip(struct ftrace_ops *ops, unsigned long ip,
3528 int remove, int reset)
3530 ftrace_ops_init(ops);
3531 return ftrace_set_addr(ops, ip, remove, reset, 1);
3533 EXPORT_SYMBOL_GPL(ftrace_set_filter_ip);
3536 ftrace_set_regex(struct ftrace_ops *ops, unsigned char *buf, int len,
3537 int reset, int enable)
3539 return ftrace_set_hash(ops, buf, len, 0, 0, reset, enable);
3543 * ftrace_set_filter - set a function to filter on in ftrace
3544 * @ops - the ops to set the filter with
3545 * @buf - the string that holds the function filter text.
3546 * @len - the length of the string.
3547 * @reset - non zero to reset all filters before applying this filter.
3549 * Filters denote which functions should be enabled when tracing is enabled.
3550 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
3552 int ftrace_set_filter(struct ftrace_ops *ops, unsigned char *buf,
3555 ftrace_ops_init(ops);
3556 return ftrace_set_regex(ops, buf, len, reset, 1);
3558 EXPORT_SYMBOL_GPL(ftrace_set_filter);
3561 * ftrace_set_notrace - set a function to not trace in ftrace
3562 * @ops - the ops to set the notrace filter with
3563 * @buf - the string that holds the function notrace text.
3564 * @len - the length of the string.
3565 * @reset - non zero to reset all filters before applying this filter.
3567 * Notrace Filters denote which functions should not be enabled when tracing
3568 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
3571 int ftrace_set_notrace(struct ftrace_ops *ops, unsigned char *buf,
3574 ftrace_ops_init(ops);
3575 return ftrace_set_regex(ops, buf, len, reset, 0);
3577 EXPORT_SYMBOL_GPL(ftrace_set_notrace);
3579 * ftrace_set_filter - set a function to filter on in ftrace
3580 * @ops - the ops to set the filter with
3581 * @buf - the string that holds the function filter text.
3582 * @len - the length of the string.
3583 * @reset - non zero to reset all filters before applying this filter.
3585 * Filters denote which functions should be enabled when tracing is enabled.
3586 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
3588 void ftrace_set_global_filter(unsigned char *buf, int len, int reset)
3590 ftrace_set_regex(&global_ops, buf, len, reset, 1);
3592 EXPORT_SYMBOL_GPL(ftrace_set_global_filter);
3595 * ftrace_set_notrace - set a function to not trace in ftrace
3596 * @ops - the ops to set the notrace filter with
3597 * @buf - the string that holds the function notrace text.
3598 * @len - the length of the string.
3599 * @reset - non zero to reset all filters before applying this filter.
3601 * Notrace Filters denote which functions should not be enabled when tracing
3602 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
3605 void ftrace_set_global_notrace(unsigned char *buf, int len, int reset)
3607 ftrace_set_regex(&global_ops, buf, len, reset, 0);
3609 EXPORT_SYMBOL_GPL(ftrace_set_global_notrace);
3612 * command line interface to allow users to set filters on boot up.
3614 #define FTRACE_FILTER_SIZE COMMAND_LINE_SIZE
3615 static char ftrace_notrace_buf[FTRACE_FILTER_SIZE] __initdata;
3616 static char ftrace_filter_buf[FTRACE_FILTER_SIZE] __initdata;
3618 /* Used by function selftest to not test if filter is set */
3619 bool ftrace_filter_param __initdata;
3621 static int __init set_ftrace_notrace(char *str)
3623 ftrace_filter_param = true;
3624 strlcpy(ftrace_notrace_buf, str, FTRACE_FILTER_SIZE);
3627 __setup("ftrace_notrace=", set_ftrace_notrace);
3629 static int __init set_ftrace_filter(char *str)
3631 ftrace_filter_param = true;
3632 strlcpy(ftrace_filter_buf, str, FTRACE_FILTER_SIZE);
3635 __setup("ftrace_filter=", set_ftrace_filter);
3637 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
3638 static char ftrace_graph_buf[FTRACE_FILTER_SIZE] __initdata;
3639 static int ftrace_set_func(unsigned long *array, int *idx, int size, char *buffer);
3641 static int __init set_graph_function(char *str)
3643 strlcpy(ftrace_graph_buf, str, FTRACE_FILTER_SIZE);
3646 __setup("ftrace_graph_filter=", set_graph_function);
3648 static void __init set_ftrace_early_graph(char *buf)
3654 func = strsep(&buf, ",");
3655 /* we allow only one expression at a time */
3656 ret = ftrace_set_func(ftrace_graph_funcs, &ftrace_graph_count,
3657 FTRACE_GRAPH_MAX_FUNCS, func);
3659 printk(KERN_DEBUG "ftrace: function %s not "
3660 "traceable\n", func);
3663 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
3666 ftrace_set_early_filter(struct ftrace_ops *ops, char *buf, int enable)
3670 ftrace_ops_init(ops);
3673 func = strsep(&buf, ",");
3674 ftrace_set_regex(ops, func, strlen(func), 0, enable);
3678 static void __init set_ftrace_early_filters(void)
3680 if (ftrace_filter_buf[0])
3681 ftrace_set_early_filter(&global_ops, ftrace_filter_buf, 1);
3682 if (ftrace_notrace_buf[0])
3683 ftrace_set_early_filter(&global_ops, ftrace_notrace_buf, 0);
3684 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
3685 if (ftrace_graph_buf[0])
3686 set_ftrace_early_graph(ftrace_graph_buf);
3687 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
3690 int ftrace_regex_release(struct inode *inode, struct file *file)
3692 struct seq_file *m = (struct seq_file *)file->private_data;
3693 struct ftrace_iterator *iter;
3694 struct ftrace_hash **orig_hash;
3695 struct trace_parser *parser;
3699 if (file->f_mode & FMODE_READ) {
3701 seq_release(inode, file);
3703 iter = file->private_data;
3705 parser = &iter->parser;
3706 if (trace_parser_loaded(parser)) {
3707 parser->buffer[parser->idx] = 0;
3708 ftrace_match_records(iter->hash, parser->buffer, parser->idx);
3711 trace_parser_put(parser);
3713 mutex_lock(&iter->ops->regex_lock);
3715 if (file->f_mode & FMODE_WRITE) {
3716 filter_hash = !!(iter->flags & FTRACE_ITER_FILTER);
3719 orig_hash = &iter->ops->filter_hash;
3721 orig_hash = &iter->ops->notrace_hash;
3723 mutex_lock(&ftrace_lock);
3724 ret = ftrace_hash_move(iter->ops, filter_hash,
3725 orig_hash, iter->hash);
3727 ftrace_ops_update_code(iter->ops);
3729 mutex_unlock(&ftrace_lock);
3732 mutex_unlock(&iter->ops->regex_lock);
3733 free_ftrace_hash(iter->hash);
3739 static const struct file_operations ftrace_avail_fops = {
3740 .open = ftrace_avail_open,
3742 .llseek = seq_lseek,
3743 .release = seq_release_private,
3746 static const struct file_operations ftrace_enabled_fops = {
3747 .open = ftrace_enabled_open,
3749 .llseek = seq_lseek,
3750 .release = seq_release_private,
3753 static const struct file_operations ftrace_filter_fops = {
3754 .open = ftrace_filter_open,
3756 .write = ftrace_filter_write,
3757 .llseek = tracing_lseek,
3758 .release = ftrace_regex_release,
3761 static const struct file_operations ftrace_notrace_fops = {
3762 .open = ftrace_notrace_open,
3764 .write = ftrace_notrace_write,
3765 .llseek = tracing_lseek,
3766 .release = ftrace_regex_release,
3769 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
3771 static DEFINE_MUTEX(graph_lock);
3773 int ftrace_graph_count;
3774 int ftrace_graph_notrace_count;
3775 unsigned long ftrace_graph_funcs[FTRACE_GRAPH_MAX_FUNCS] __read_mostly;
3776 unsigned long ftrace_graph_notrace_funcs[FTRACE_GRAPH_MAX_FUNCS] __read_mostly;
3778 struct ftrace_graph_data {
3779 unsigned long *table;
3782 const struct seq_operations *seq_ops;
3786 __g_next(struct seq_file *m, loff_t *pos)
3788 struct ftrace_graph_data *fgd = m->private;
3790 if (*pos >= *fgd->count)
3792 return &fgd->table[*pos];
3796 g_next(struct seq_file *m, void *v, loff_t *pos)
3799 return __g_next(m, pos);
3802 static void *g_start(struct seq_file *m, loff_t *pos)
3804 struct ftrace_graph_data *fgd = m->private;
3806 mutex_lock(&graph_lock);
3808 /* Nothing, tell g_show to print all functions are enabled */
3809 if (!*fgd->count && !*pos)
3812 return __g_next(m, pos);
3815 static void g_stop(struct seq_file *m, void *p)
3817 mutex_unlock(&graph_lock);
3820 static int g_show(struct seq_file *m, void *v)
3822 unsigned long *ptr = v;
3827 if (ptr == (unsigned long *)1) {
3828 seq_printf(m, "#### all functions enabled ####\n");
3832 seq_printf(m, "%ps\n", (void *)*ptr);
3837 static const struct seq_operations ftrace_graph_seq_ops = {
3845 __ftrace_graph_open(struct inode *inode, struct file *file,
3846 struct ftrace_graph_data *fgd)
3850 mutex_lock(&graph_lock);
3851 if ((file->f_mode & FMODE_WRITE) &&
3852 (file->f_flags & O_TRUNC)) {
3854 memset(fgd->table, 0, fgd->size * sizeof(*fgd->table));
3856 mutex_unlock(&graph_lock);
3858 if (file->f_mode & FMODE_READ) {
3859 ret = seq_open(file, fgd->seq_ops);
3861 struct seq_file *m = file->private_data;
3865 file->private_data = fgd;
3871 ftrace_graph_open(struct inode *inode, struct file *file)
3873 struct ftrace_graph_data *fgd;
3875 if (unlikely(ftrace_disabled))
3878 fgd = kmalloc(sizeof(*fgd), GFP_KERNEL);
3882 fgd->table = ftrace_graph_funcs;
3883 fgd->size = FTRACE_GRAPH_MAX_FUNCS;
3884 fgd->count = &ftrace_graph_count;
3885 fgd->seq_ops = &ftrace_graph_seq_ops;
3887 return __ftrace_graph_open(inode, file, fgd);
3891 ftrace_graph_notrace_open(struct inode *inode, struct file *file)
3893 struct ftrace_graph_data *fgd;
3895 if (unlikely(ftrace_disabled))
3898 fgd = kmalloc(sizeof(*fgd), GFP_KERNEL);
3902 fgd->table = ftrace_graph_notrace_funcs;
3903 fgd->size = FTRACE_GRAPH_MAX_FUNCS;
3904 fgd->count = &ftrace_graph_notrace_count;
3905 fgd->seq_ops = &ftrace_graph_seq_ops;
3907 return __ftrace_graph_open(inode, file, fgd);
3911 ftrace_graph_release(struct inode *inode, struct file *file)
3913 if (file->f_mode & FMODE_READ) {
3914 struct seq_file *m = file->private_data;
3917 seq_release(inode, file);
3919 kfree(file->private_data);
3926 ftrace_set_func(unsigned long *array, int *idx, int size, char *buffer)
3928 struct dyn_ftrace *rec;
3929 struct ftrace_page *pg;
3938 type = filter_parse_regex(buffer, strlen(buffer), &search, ¬);
3939 if (!not && *idx >= size)
3942 search_len = strlen(search);
3944 mutex_lock(&ftrace_lock);
3946 if (unlikely(ftrace_disabled)) {
3947 mutex_unlock(&ftrace_lock);
3951 do_for_each_ftrace_rec(pg, rec) {
3953 if (ftrace_match_record(rec, NULL, search, search_len, type)) {
3954 /* if it is in the array */
3956 for (i = 0; i < *idx; i++) {
3957 if (array[i] == rec->ip) {
3966 array[(*idx)++] = rec->ip;
3972 array[i] = array[--(*idx)];
3978 } while_for_each_ftrace_rec();
3980 mutex_unlock(&ftrace_lock);
3989 ftrace_graph_write(struct file *file, const char __user *ubuf,
3990 size_t cnt, loff_t *ppos)
3992 struct trace_parser parser;
3993 ssize_t read, ret = 0;
3994 struct ftrace_graph_data *fgd = file->private_data;
3999 if (trace_parser_get_init(&parser, FTRACE_BUFF_MAX))
4002 read = trace_get_user(&parser, ubuf, cnt, ppos);
4004 if (read >= 0 && trace_parser_loaded((&parser))) {
4005 parser.buffer[parser.idx] = 0;
4007 mutex_lock(&graph_lock);
4009 /* we allow only one expression at a time */
4010 ret = ftrace_set_func(fgd->table, fgd->count, fgd->size,
4013 mutex_unlock(&graph_lock);
4019 trace_parser_put(&parser);
4024 static const struct file_operations ftrace_graph_fops = {
4025 .open = ftrace_graph_open,
4027 .write = ftrace_graph_write,
4028 .llseek = tracing_lseek,
4029 .release = ftrace_graph_release,
4032 static const struct file_operations ftrace_graph_notrace_fops = {
4033 .open = ftrace_graph_notrace_open,
4035 .write = ftrace_graph_write,
4036 .llseek = tracing_lseek,
4037 .release = ftrace_graph_release,
4039 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
4041 static __init int ftrace_init_dyn_debugfs(struct dentry *d_tracer)
4044 trace_create_file("available_filter_functions", 0444,
4045 d_tracer, NULL, &ftrace_avail_fops);
4047 trace_create_file("enabled_functions", 0444,
4048 d_tracer, NULL, &ftrace_enabled_fops);
4050 trace_create_file("set_ftrace_filter", 0644, d_tracer,
4051 NULL, &ftrace_filter_fops);
4053 trace_create_file("set_ftrace_notrace", 0644, d_tracer,
4054 NULL, &ftrace_notrace_fops);
4056 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
4057 trace_create_file("set_graph_function", 0444, d_tracer,
4059 &ftrace_graph_fops);
4060 trace_create_file("set_graph_notrace", 0444, d_tracer,
4062 &ftrace_graph_notrace_fops);
4063 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
4068 static int ftrace_cmp_ips(const void *a, const void *b)
4070 const unsigned long *ipa = a;
4071 const unsigned long *ipb = b;
4080 static void ftrace_swap_ips(void *a, void *b, int size)
4082 unsigned long *ipa = a;
4083 unsigned long *ipb = b;
4091 static int ftrace_process_locs(struct module *mod,
4092 unsigned long *start,
4095 struct ftrace_page *start_pg;
4096 struct ftrace_page *pg;
4097 struct dyn_ftrace *rec;
4098 unsigned long count;
4101 unsigned long flags = 0; /* Shut up gcc */
4104 count = end - start;
4109 sort(start, count, sizeof(*start),
4110 ftrace_cmp_ips, ftrace_swap_ips);
4112 start_pg = ftrace_allocate_pages(count);
4116 mutex_lock(&ftrace_lock);
4119 * Core and each module needs their own pages, as
4120 * modules will free them when they are removed.
4121 * Force a new page to be allocated for modules.
4124 WARN_ON(ftrace_pages || ftrace_pages_start);
4125 /* First initialization */
4126 ftrace_pages = ftrace_pages_start = start_pg;
4131 if (WARN_ON(ftrace_pages->next)) {
4132 /* Hmm, we have free pages? */
4133 while (ftrace_pages->next)
4134 ftrace_pages = ftrace_pages->next;
4137 ftrace_pages->next = start_pg;
4143 addr = ftrace_call_adjust(*p++);
4145 * Some architecture linkers will pad between
4146 * the different mcount_loc sections of different
4147 * object files to satisfy alignments.
4148 * Skip any NULL pointers.
4153 if (pg->index == pg->size) {
4154 /* We should have allocated enough */
4155 if (WARN_ON(!pg->next))
4160 rec = &pg->records[pg->index++];
4164 /* We should have used all pages */
4167 /* Assign the last page to ftrace_pages */
4170 /* These new locations need to be initialized */
4171 ftrace_new_pgs = start_pg;
4174 * We only need to disable interrupts on start up
4175 * because we are modifying code that an interrupt
4176 * may execute, and the modification is not atomic.
4177 * But for modules, nothing runs the code we modify
4178 * until we are finished with it, and there's no
4179 * reason to cause large interrupt latencies while we do it.
4182 local_irq_save(flags);
4183 ftrace_update_code(mod);
4185 local_irq_restore(flags);
4188 mutex_unlock(&ftrace_lock);
4193 #ifdef CONFIG_MODULES
4195 #define next_to_ftrace_page(p) container_of(p, struct ftrace_page, next)
4197 void ftrace_release_mod(struct module *mod)
4199 struct dyn_ftrace *rec;
4200 struct ftrace_page **last_pg;
4201 struct ftrace_page *pg;
4204 mutex_lock(&ftrace_lock);
4206 if (ftrace_disabled)
4210 * Each module has its own ftrace_pages, remove
4211 * them from the list.
4213 last_pg = &ftrace_pages_start;
4214 for (pg = ftrace_pages_start; pg; pg = *last_pg) {
4215 rec = &pg->records[0];
4216 if (within_module_core(rec->ip, mod)) {
4218 * As core pages are first, the first
4219 * page should never be a module page.
4221 if (WARN_ON(pg == ftrace_pages_start))
4224 /* Check if we are deleting the last page */
4225 if (pg == ftrace_pages)
4226 ftrace_pages = next_to_ftrace_page(last_pg);
4228 *last_pg = pg->next;
4229 order = get_count_order(pg->size / ENTRIES_PER_PAGE);
4230 free_pages((unsigned long)pg->records, order);
4233 last_pg = &pg->next;
4236 mutex_unlock(&ftrace_lock);
4239 static void ftrace_init_module(struct module *mod,
4240 unsigned long *start, unsigned long *end)
4242 if (ftrace_disabled || start == end)
4244 ftrace_process_locs(mod, start, end);
4247 static int ftrace_module_notify_enter(struct notifier_block *self,
4248 unsigned long val, void *data)
4250 struct module *mod = data;
4252 if (val == MODULE_STATE_COMING)
4253 ftrace_init_module(mod, mod->ftrace_callsites,
4254 mod->ftrace_callsites +
4255 mod->num_ftrace_callsites);
4259 static int ftrace_module_notify_exit(struct notifier_block *self,
4260 unsigned long val, void *data)
4262 struct module *mod = data;
4264 if (val == MODULE_STATE_GOING)
4265 ftrace_release_mod(mod);
4270 static int ftrace_module_notify_enter(struct notifier_block *self,
4271 unsigned long val, void *data)
4275 static int ftrace_module_notify_exit(struct notifier_block *self,
4276 unsigned long val, void *data)
4280 #endif /* CONFIG_MODULES */
4282 struct notifier_block ftrace_module_enter_nb = {
4283 .notifier_call = ftrace_module_notify_enter,
4284 .priority = INT_MAX, /* Run before anything that can use kprobes */
4287 struct notifier_block ftrace_module_exit_nb = {
4288 .notifier_call = ftrace_module_notify_exit,
4289 .priority = INT_MIN, /* Run after anything that can remove kprobes */
4292 extern unsigned long __start_mcount_loc[];
4293 extern unsigned long __stop_mcount_loc[];
4295 void __init ftrace_init(void)
4297 unsigned long count, addr, flags;
4300 /* Keep the ftrace pointer to the stub */
4301 addr = (unsigned long)ftrace_stub;
4303 local_irq_save(flags);
4304 ftrace_dyn_arch_init(&addr);
4305 local_irq_restore(flags);
4307 /* ftrace_dyn_arch_init places the return code in addr */
4311 count = __stop_mcount_loc - __start_mcount_loc;
4313 ret = ftrace_dyn_table_alloc(count);
4317 last_ftrace_enabled = ftrace_enabled = 1;
4319 ret = ftrace_process_locs(NULL,
4323 ret = register_module_notifier(&ftrace_module_enter_nb);
4325 pr_warning("Failed to register trace ftrace module enter notifier\n");
4327 ret = register_module_notifier(&ftrace_module_exit_nb);
4329 pr_warning("Failed to register trace ftrace module exit notifier\n");
4331 set_ftrace_early_filters();
4335 ftrace_disabled = 1;
4340 static struct ftrace_ops global_ops = {
4341 .func = ftrace_stub,
4342 .flags = FTRACE_OPS_FL_RECURSION_SAFE | FTRACE_OPS_FL_INITIALIZED,
4343 INIT_REGEX_LOCK(global_ops)
4346 static int __init ftrace_nodyn_init(void)
4351 core_initcall(ftrace_nodyn_init);
4353 static inline int ftrace_init_dyn_debugfs(struct dentry *d_tracer) { return 0; }
4354 static inline void ftrace_startup_enable(int command) { }
4355 /* Keep as macros so we do not need to define the commands */
4356 # define ftrace_startup(ops, command) \
4358 int ___ret = __register_ftrace_function(ops); \
4360 (ops)->flags |= FTRACE_OPS_FL_ENABLED; \
4363 # define ftrace_shutdown(ops, command) __unregister_ftrace_function(ops)
4365 # define ftrace_startup_sysctl() do { } while (0)
4366 # define ftrace_shutdown_sysctl() do { } while (0)
4369 ftrace_ops_test(struct ftrace_ops *ops, unsigned long ip, void *regs)
4374 #endif /* CONFIG_DYNAMIC_FTRACE */
4377 ftrace_ops_control_func(unsigned long ip, unsigned long parent_ip,
4378 struct ftrace_ops *op, struct pt_regs *regs)
4380 if (unlikely(trace_recursion_test(TRACE_CONTROL_BIT)))
4384 * Some of the ops may be dynamically allocated,
4385 * they must be freed after a synchronize_sched().
4387 preempt_disable_notrace();
4388 trace_recursion_set(TRACE_CONTROL_BIT);
4391 * Control funcs (perf) uses RCU. Only trace if
4392 * RCU is currently active.
4394 if (!rcu_is_watching())
4397 do_for_each_ftrace_op(op, ftrace_control_list) {
4398 if (!(op->flags & FTRACE_OPS_FL_STUB) &&
4399 !ftrace_function_local_disabled(op) &&
4400 ftrace_ops_test(op, ip, regs))
4401 op->func(ip, parent_ip, op, regs);
4402 } while_for_each_ftrace_op(op);
4404 trace_recursion_clear(TRACE_CONTROL_BIT);
4405 preempt_enable_notrace();
4408 static struct ftrace_ops control_ops = {
4409 .func = ftrace_ops_control_func,
4410 .flags = FTRACE_OPS_FL_RECURSION_SAFE | FTRACE_OPS_FL_INITIALIZED,
4411 INIT_REGEX_LOCK(control_ops)
4415 __ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip,
4416 struct ftrace_ops *ignored, struct pt_regs *regs)
4418 struct ftrace_ops *op;
4421 if (function_trace_stop)
4424 bit = trace_test_and_set_recursion(TRACE_LIST_START, TRACE_LIST_MAX);
4429 * Some of the ops may be dynamically allocated,
4430 * they must be freed after a synchronize_sched().
4432 preempt_disable_notrace();
4433 do_for_each_ftrace_op(op, ftrace_ops_list) {
4434 if (ftrace_ops_test(op, ip, regs))
4435 op->func(ip, parent_ip, op, regs);
4436 } while_for_each_ftrace_op(op);
4437 preempt_enable_notrace();
4438 trace_clear_recursion(bit);
4442 * Some archs only support passing ip and parent_ip. Even though
4443 * the list function ignores the op parameter, we do not want any
4444 * C side effects, where a function is called without the caller
4445 * sending a third parameter.
4446 * Archs are to support both the regs and ftrace_ops at the same time.
4447 * If they support ftrace_ops, it is assumed they support regs.
4448 * If call backs want to use regs, they must either check for regs
4449 * being NULL, or CONFIG_DYNAMIC_FTRACE_WITH_REGS.
4450 * Note, CONFIG_DYNAMIC_FTRACE_WITH_REGS expects a full regs to be saved.
4451 * An architecture can pass partial regs with ftrace_ops and still
4452 * set the ARCH_SUPPORT_FTARCE_OPS.
4454 #if ARCH_SUPPORTS_FTRACE_OPS
4455 static void ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip,
4456 struct ftrace_ops *op, struct pt_regs *regs)
4458 __ftrace_ops_list_func(ip, parent_ip, NULL, regs);
4461 static void ftrace_ops_no_ops(unsigned long ip, unsigned long parent_ip)
4463 __ftrace_ops_list_func(ip, parent_ip, NULL, NULL);
4467 static void clear_ftrace_swapper(void)
4469 struct task_struct *p;
4473 for_each_online_cpu(cpu) {
4475 clear_tsk_trace_trace(p);
4480 static void set_ftrace_swapper(void)
4482 struct task_struct *p;
4486 for_each_online_cpu(cpu) {
4488 set_tsk_trace_trace(p);
4493 static void clear_ftrace_pid(struct pid *pid)
4495 struct task_struct *p;
4498 do_each_pid_task(pid, PIDTYPE_PID, p) {
4499 clear_tsk_trace_trace(p);
4500 } while_each_pid_task(pid, PIDTYPE_PID, p);
4506 static void set_ftrace_pid(struct pid *pid)
4508 struct task_struct *p;
4511 do_each_pid_task(pid, PIDTYPE_PID, p) {
4512 set_tsk_trace_trace(p);
4513 } while_each_pid_task(pid, PIDTYPE_PID, p);
4517 static void clear_ftrace_pid_task(struct pid *pid)
4519 if (pid == ftrace_swapper_pid)
4520 clear_ftrace_swapper();
4522 clear_ftrace_pid(pid);
4525 static void set_ftrace_pid_task(struct pid *pid)
4527 if (pid == ftrace_swapper_pid)
4528 set_ftrace_swapper();
4530 set_ftrace_pid(pid);
4533 static int ftrace_pid_add(int p)
4536 struct ftrace_pid *fpid;
4539 mutex_lock(&ftrace_lock);
4542 pid = ftrace_swapper_pid;
4544 pid = find_get_pid(p);
4551 list_for_each_entry(fpid, &ftrace_pids, list)
4552 if (fpid->pid == pid)
4557 fpid = kmalloc(sizeof(*fpid), GFP_KERNEL);
4561 list_add(&fpid->list, &ftrace_pids);
4564 set_ftrace_pid_task(pid);
4566 ftrace_update_pid_func();
4567 ftrace_startup_enable(0);
4569 mutex_unlock(&ftrace_lock);
4573 if (pid != ftrace_swapper_pid)
4577 mutex_unlock(&ftrace_lock);
4581 static void ftrace_pid_reset(void)
4583 struct ftrace_pid *fpid, *safe;
4585 mutex_lock(&ftrace_lock);
4586 list_for_each_entry_safe(fpid, safe, &ftrace_pids, list) {
4587 struct pid *pid = fpid->pid;
4589 clear_ftrace_pid_task(pid);
4591 list_del(&fpid->list);
4595 ftrace_update_pid_func();
4596 ftrace_startup_enable(0);
4598 mutex_unlock(&ftrace_lock);
4601 static void *fpid_start(struct seq_file *m, loff_t *pos)
4603 mutex_lock(&ftrace_lock);
4605 if (list_empty(&ftrace_pids) && (!*pos))
4608 return seq_list_start(&ftrace_pids, *pos);
4611 static void *fpid_next(struct seq_file *m, void *v, loff_t *pos)
4616 return seq_list_next(v, &ftrace_pids, pos);
4619 static void fpid_stop(struct seq_file *m, void *p)
4621 mutex_unlock(&ftrace_lock);
4624 static int fpid_show(struct seq_file *m, void *v)
4626 const struct ftrace_pid *fpid = list_entry(v, struct ftrace_pid, list);
4628 if (v == (void *)1) {
4629 seq_printf(m, "no pid\n");
4633 if (fpid->pid == ftrace_swapper_pid)
4634 seq_printf(m, "swapper tasks\n");
4636 seq_printf(m, "%u\n", pid_vnr(fpid->pid));
4641 static const struct seq_operations ftrace_pid_sops = {
4642 .start = fpid_start,
4649 ftrace_pid_open(struct inode *inode, struct file *file)
4653 if ((file->f_mode & FMODE_WRITE) &&
4654 (file->f_flags & O_TRUNC))
4657 if (file->f_mode & FMODE_READ)
4658 ret = seq_open(file, &ftrace_pid_sops);
4664 ftrace_pid_write(struct file *filp, const char __user *ubuf,
4665 size_t cnt, loff_t *ppos)
4671 if (cnt >= sizeof(buf))
4674 if (copy_from_user(&buf, ubuf, cnt))
4680 * Allow "echo > set_ftrace_pid" or "echo -n '' > set_ftrace_pid"
4681 * to clean the filter quietly.
4683 tmp = strstrip(buf);
4684 if (strlen(tmp) == 0)
4687 ret = kstrtol(tmp, 10, &val);
4691 ret = ftrace_pid_add(val);
4693 return ret ? ret : cnt;
4697 ftrace_pid_release(struct inode *inode, struct file *file)
4699 if (file->f_mode & FMODE_READ)
4700 seq_release(inode, file);
4705 static const struct file_operations ftrace_pid_fops = {
4706 .open = ftrace_pid_open,
4707 .write = ftrace_pid_write,
4709 .llseek = tracing_lseek,
4710 .release = ftrace_pid_release,
4713 static __init int ftrace_init_debugfs(void)
4715 struct dentry *d_tracer;
4717 d_tracer = tracing_init_dentry();
4721 ftrace_init_dyn_debugfs(d_tracer);
4723 trace_create_file("set_ftrace_pid", 0644, d_tracer,
4724 NULL, &ftrace_pid_fops);
4726 ftrace_profile_debugfs(d_tracer);
4730 fs_initcall(ftrace_init_debugfs);
4733 * ftrace_kill - kill ftrace
4735 * This function should be used by panic code. It stops ftrace
4736 * but in a not so nice way. If you need to simply kill ftrace
4737 * from a non-atomic section, use ftrace_kill.
4739 void ftrace_kill(void)
4741 ftrace_disabled = 1;
4743 clear_ftrace_function();
4747 * Test if ftrace is dead or not.
4749 int ftrace_is_dead(void)
4751 return ftrace_disabled;
4755 * register_ftrace_function - register a function for profiling
4756 * @ops - ops structure that holds the function for profiling.
4758 * Register a function to be called by all functions in the
4761 * Note: @ops->func and all the functions it calls must be labeled
4762 * with "notrace", otherwise it will go into a
4765 int register_ftrace_function(struct ftrace_ops *ops)
4769 ftrace_ops_init(ops);
4771 mutex_lock(&ftrace_lock);
4773 ret = ftrace_startup(ops, 0);
4775 mutex_unlock(&ftrace_lock);
4779 EXPORT_SYMBOL_GPL(register_ftrace_function);
4782 * unregister_ftrace_function - unregister a function for profiling.
4783 * @ops - ops structure that holds the function to unregister
4785 * Unregister a function that was added to be called by ftrace profiling.
4787 int unregister_ftrace_function(struct ftrace_ops *ops)
4791 mutex_lock(&ftrace_lock);
4792 ret = ftrace_shutdown(ops, 0);
4793 mutex_unlock(&ftrace_lock);
4797 EXPORT_SYMBOL_GPL(unregister_ftrace_function);
4800 ftrace_enable_sysctl(struct ctl_table *table, int write,
4801 void __user *buffer, size_t *lenp,
4806 mutex_lock(&ftrace_lock);
4808 if (unlikely(ftrace_disabled))
4811 ret = proc_dointvec(table, write, buffer, lenp, ppos);
4813 if (ret || !write || (last_ftrace_enabled == !!ftrace_enabled))
4816 last_ftrace_enabled = !!ftrace_enabled;
4818 if (ftrace_enabled) {
4820 ftrace_startup_sysctl();
4822 /* we are starting ftrace again */
4823 if (ftrace_ops_list != &ftrace_list_end)
4824 update_ftrace_function();
4827 /* stopping ftrace calls (just send to ftrace_stub) */
4828 ftrace_trace_function = ftrace_stub;
4830 ftrace_shutdown_sysctl();
4834 mutex_unlock(&ftrace_lock);
4838 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
4840 static int ftrace_graph_active;
4841 static struct notifier_block ftrace_suspend_notifier;
4843 int ftrace_graph_entry_stub(struct ftrace_graph_ent *trace)
4848 /* The callbacks that hook a function */
4849 trace_func_graph_ret_t ftrace_graph_return =
4850 (trace_func_graph_ret_t)ftrace_stub;
4851 trace_func_graph_ent_t ftrace_graph_entry = ftrace_graph_entry_stub;
4853 /* Try to assign a return stack array on FTRACE_RETSTACK_ALLOC_SIZE tasks. */
4854 static int alloc_retstack_tasklist(struct ftrace_ret_stack **ret_stack_list)
4858 unsigned long flags;
4859 int start = 0, end = FTRACE_RETSTACK_ALLOC_SIZE;
4860 struct task_struct *g, *t;
4862 for (i = 0; i < FTRACE_RETSTACK_ALLOC_SIZE; i++) {
4863 ret_stack_list[i] = kmalloc(FTRACE_RETFUNC_DEPTH
4864 * sizeof(struct ftrace_ret_stack),
4866 if (!ret_stack_list[i]) {
4874 read_lock_irqsave(&tasklist_lock, flags);
4875 do_each_thread(g, t) {
4881 if (t->ret_stack == NULL) {
4882 atomic_set(&t->tracing_graph_pause, 0);
4883 atomic_set(&t->trace_overrun, 0);
4884 t->curr_ret_stack = -1;
4885 /* Make sure the tasks see the -1 first: */
4887 t->ret_stack = ret_stack_list[start++];
4889 } while_each_thread(g, t);
4892 read_unlock_irqrestore(&tasklist_lock, flags);
4894 for (i = start; i < end; i++)
4895 kfree(ret_stack_list[i]);
4900 ftrace_graph_probe_sched_switch(void *ignore,
4901 struct task_struct *prev, struct task_struct *next)
4903 unsigned long long timestamp;
4907 * Does the user want to count the time a function was asleep.
4908 * If so, do not update the time stamps.
4910 if (trace_flags & TRACE_ITER_SLEEP_TIME)
4913 timestamp = trace_clock_local();
4915 prev->ftrace_timestamp = timestamp;
4917 /* only process tasks that we timestamped */
4918 if (!next->ftrace_timestamp)
4922 * Update all the counters in next to make up for the
4923 * time next was sleeping.
4925 timestamp -= next->ftrace_timestamp;
4927 for (index = next->curr_ret_stack; index >= 0; index--)
4928 next->ret_stack[index].calltime += timestamp;
4931 /* Allocate a return stack for each task */
4932 static int start_graph_tracing(void)
4934 struct ftrace_ret_stack **ret_stack_list;
4937 ret_stack_list = kmalloc(FTRACE_RETSTACK_ALLOC_SIZE *
4938 sizeof(struct ftrace_ret_stack *),
4941 if (!ret_stack_list)
4944 /* The cpu_boot init_task->ret_stack will never be freed */
4945 for_each_online_cpu(cpu) {
4946 if (!idle_task(cpu)->ret_stack)
4947 ftrace_graph_init_idle_task(idle_task(cpu), cpu);
4951 ret = alloc_retstack_tasklist(ret_stack_list);
4952 } while (ret == -EAGAIN);
4955 ret = register_trace_sched_switch(ftrace_graph_probe_sched_switch, NULL);
4957 pr_info("ftrace_graph: Couldn't activate tracepoint"
4958 " probe to kernel_sched_switch\n");
4961 kfree(ret_stack_list);
4966 * Hibernation protection.
4967 * The state of the current task is too much unstable during
4968 * suspend/restore to disk. We want to protect against that.
4971 ftrace_suspend_notifier_call(struct notifier_block *bl, unsigned long state,
4975 case PM_HIBERNATION_PREPARE:
4976 pause_graph_tracing();
4979 case PM_POST_HIBERNATION:
4980 unpause_graph_tracing();
4986 /* Just a place holder for function graph */
4987 static struct ftrace_ops fgraph_ops __read_mostly = {
4988 .func = ftrace_stub,
4989 .flags = FTRACE_OPS_FL_STUB | FTRACE_OPS_FL_GLOBAL |
4990 FTRACE_OPS_FL_RECURSION_SAFE,
4993 int register_ftrace_graph(trace_func_graph_ret_t retfunc,
4994 trace_func_graph_ent_t entryfunc)
4998 mutex_lock(&ftrace_lock);
5000 /* we currently allow only one tracer registered at a time */
5001 if (ftrace_graph_active) {
5006 ftrace_suspend_notifier.notifier_call = ftrace_suspend_notifier_call;
5007 register_pm_notifier(&ftrace_suspend_notifier);
5009 ftrace_graph_active++;
5010 ret = start_graph_tracing();
5012 ftrace_graph_active--;
5016 ftrace_graph_return = retfunc;
5017 ftrace_graph_entry = entryfunc;
5019 ret = ftrace_startup(&fgraph_ops, FTRACE_START_FUNC_RET);
5022 mutex_unlock(&ftrace_lock);
5026 void unregister_ftrace_graph(void)
5028 mutex_lock(&ftrace_lock);
5030 if (unlikely(!ftrace_graph_active))
5033 ftrace_graph_active--;
5034 ftrace_graph_return = (trace_func_graph_ret_t)ftrace_stub;
5035 ftrace_graph_entry = ftrace_graph_entry_stub;
5036 ftrace_shutdown(&fgraph_ops, FTRACE_STOP_FUNC_RET);
5037 unregister_pm_notifier(&ftrace_suspend_notifier);
5038 unregister_trace_sched_switch(ftrace_graph_probe_sched_switch, NULL);
5041 mutex_unlock(&ftrace_lock);
5044 static DEFINE_PER_CPU(struct ftrace_ret_stack *, idle_ret_stack);
5047 graph_init_task(struct task_struct *t, struct ftrace_ret_stack *ret_stack)
5049 atomic_set(&t->tracing_graph_pause, 0);
5050 atomic_set(&t->trace_overrun, 0);
5051 t->ftrace_timestamp = 0;
5052 /* make curr_ret_stack visible before we add the ret_stack */
5054 t->ret_stack = ret_stack;
5058 * Allocate a return stack for the idle task. May be the first
5059 * time through, or it may be done by CPU hotplug online.
5061 void ftrace_graph_init_idle_task(struct task_struct *t, int cpu)
5063 t->curr_ret_stack = -1;
5065 * The idle task has no parent, it either has its own
5066 * stack or no stack at all.
5069 WARN_ON(t->ret_stack != per_cpu(idle_ret_stack, cpu));
5071 if (ftrace_graph_active) {
5072 struct ftrace_ret_stack *ret_stack;
5074 ret_stack = per_cpu(idle_ret_stack, cpu);
5076 ret_stack = kmalloc(FTRACE_RETFUNC_DEPTH
5077 * sizeof(struct ftrace_ret_stack),
5081 per_cpu(idle_ret_stack, cpu) = ret_stack;
5083 graph_init_task(t, ret_stack);
5087 /* Allocate a return stack for newly created task */
5088 void ftrace_graph_init_task(struct task_struct *t)
5090 /* Make sure we do not use the parent ret_stack */
5091 t->ret_stack = NULL;
5092 t->curr_ret_stack = -1;
5094 if (ftrace_graph_active) {
5095 struct ftrace_ret_stack *ret_stack;
5097 ret_stack = kmalloc(FTRACE_RETFUNC_DEPTH
5098 * sizeof(struct ftrace_ret_stack),
5102 graph_init_task(t, ret_stack);
5106 void ftrace_graph_exit_task(struct task_struct *t)
5108 struct ftrace_ret_stack *ret_stack = t->ret_stack;
5110 t->ret_stack = NULL;
5111 /* NULL must become visible to IRQs before we free it: */
5117 void ftrace_graph_stop(void)