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 William Lee Irwin III
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/module.h>
26 #include <linux/ftrace.h>
27 #include <linux/sysctl.h>
28 #include <linux/slab.h>
29 #include <linux/ctype.h>
30 #include <linux/list.h>
31 #include <linux/hash.h>
32 #include <linux/rcupdate.h>
34 #include <trace/events/sched.h>
36 #include <asm/setup.h>
38 #include "trace_output.h"
39 #include "trace_stat.h"
41 #define FTRACE_WARN_ON(cond) \
49 #define FTRACE_WARN_ON_ONCE(cond) \
52 if (WARN_ON_ONCE(___r)) \
57 /* hash bits for specific function selection */
58 #define FTRACE_HASH_BITS 7
59 #define FTRACE_FUNC_HASHSIZE (1 << FTRACE_HASH_BITS)
60 #define FTRACE_HASH_DEFAULT_BITS 10
61 #define FTRACE_HASH_MAX_BITS 12
63 /* ftrace_enabled is a method to turn ftrace on or off */
64 int ftrace_enabled __read_mostly;
65 static int last_ftrace_enabled;
67 /* Quick disabling of function tracer. */
68 int function_trace_stop;
70 /* List for set_ftrace_pid's pids. */
71 LIST_HEAD(ftrace_pids);
73 struct list_head list;
78 * ftrace_disabled is set when an anomaly is discovered.
79 * ftrace_disabled is much stronger than ftrace_enabled.
81 static int ftrace_disabled __read_mostly;
83 static DEFINE_MUTEX(ftrace_lock);
85 static struct ftrace_ops ftrace_list_end __read_mostly = {
89 static struct ftrace_ops *ftrace_global_list __read_mostly = &ftrace_list_end;
90 static struct ftrace_ops *ftrace_ops_list __read_mostly = &ftrace_list_end;
91 ftrace_func_t ftrace_trace_function __read_mostly = ftrace_stub;
92 static ftrace_func_t __ftrace_trace_function_delay __read_mostly = ftrace_stub;
93 ftrace_func_t __ftrace_trace_function __read_mostly = ftrace_stub;
94 ftrace_func_t ftrace_pid_function __read_mostly = ftrace_stub;
95 static struct ftrace_ops global_ops;
98 ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip);
101 * Traverse the ftrace_global_list, invoking all entries. The reason that we
102 * can use rcu_dereference_raw() is that elements removed from this list
103 * are simply leaked, so there is no need to interact with a grace-period
104 * mechanism. The rcu_dereference_raw() calls are needed to handle
105 * concurrent insertions into the ftrace_global_list.
107 * Silly Alpha and silly pointer-speculation compiler optimizations!
109 static void ftrace_global_list_func(unsigned long ip,
110 unsigned long parent_ip)
112 struct ftrace_ops *op;
114 if (unlikely(trace_recursion_test(TRACE_GLOBAL_BIT)))
117 trace_recursion_set(TRACE_GLOBAL_BIT);
118 op = rcu_dereference_raw(ftrace_global_list); /*see above*/
119 while (op != &ftrace_list_end) {
120 op->func(ip, parent_ip);
121 op = rcu_dereference_raw(op->next); /*see above*/
123 trace_recursion_clear(TRACE_GLOBAL_BIT);
126 static void ftrace_pid_func(unsigned long ip, unsigned long parent_ip)
128 if (!test_tsk_trace_trace(current))
131 ftrace_pid_function(ip, parent_ip);
134 static void set_ftrace_pid_function(ftrace_func_t func)
136 /* do not set ftrace_pid_function to itself! */
137 if (func != ftrace_pid_func)
138 ftrace_pid_function = func;
142 * clear_ftrace_function - reset the ftrace function
144 * This NULLs the ftrace function and in essence stops
145 * tracing. There may be lag
147 void clear_ftrace_function(void)
149 ftrace_trace_function = ftrace_stub;
150 __ftrace_trace_function = ftrace_stub;
151 __ftrace_trace_function_delay = ftrace_stub;
152 ftrace_pid_function = ftrace_stub;
155 #ifndef CONFIG_HAVE_FUNCTION_TRACE_MCOUNT_TEST
157 * For those archs that do not test ftrace_trace_stop in their
158 * mcount call site, we need to do it from C.
160 static void ftrace_test_stop_func(unsigned long ip, unsigned long parent_ip)
162 if (function_trace_stop)
165 __ftrace_trace_function(ip, parent_ip);
169 static void update_global_ops(void)
174 * If there's only one function registered, then call that
175 * function directly. Otherwise, we need to iterate over the
176 * registered callers.
178 if (ftrace_global_list == &ftrace_list_end ||
179 ftrace_global_list->next == &ftrace_list_end)
180 func = ftrace_global_list->func;
182 func = ftrace_global_list_func;
184 /* If we filter on pids, update to use the pid function */
185 if (!list_empty(&ftrace_pids)) {
186 set_ftrace_pid_function(func);
187 func = ftrace_pid_func;
190 global_ops.func = func;
193 static void update_ftrace_function(void)
200 * If we are at the end of the list and this ops is
201 * not dynamic, then have the mcount trampoline call
202 * the function directly
204 if (ftrace_ops_list == &ftrace_list_end ||
205 (ftrace_ops_list->next == &ftrace_list_end &&
206 !(ftrace_ops_list->flags & FTRACE_OPS_FL_DYNAMIC)))
207 func = ftrace_ops_list->func;
209 func = ftrace_ops_list_func;
211 #ifdef CONFIG_HAVE_FUNCTION_TRACE_MCOUNT_TEST
212 ftrace_trace_function = func;
214 #ifdef CONFIG_DYNAMIC_FTRACE
215 /* do not update till all functions have been modified */
216 __ftrace_trace_function_delay = func;
218 __ftrace_trace_function = func;
220 ftrace_trace_function = ftrace_test_stop_func;
224 static void add_ftrace_ops(struct ftrace_ops **list, struct ftrace_ops *ops)
228 * We are entering ops into the list but another
229 * CPU might be walking that list. We need to make sure
230 * the ops->next pointer is valid before another CPU sees
231 * the ops pointer included into the list.
233 rcu_assign_pointer(*list, ops);
236 static int remove_ftrace_ops(struct ftrace_ops **list, struct ftrace_ops *ops)
238 struct ftrace_ops **p;
241 * If we are removing the last function, then simply point
242 * to the ftrace_stub.
244 if (*list == ops && ops->next == &ftrace_list_end) {
245 *list = &ftrace_list_end;
249 for (p = list; *p != &ftrace_list_end; p = &(*p)->next)
260 static int __register_ftrace_function(struct ftrace_ops *ops)
262 if (FTRACE_WARN_ON(ops == &global_ops))
265 if (WARN_ON(ops->flags & FTRACE_OPS_FL_ENABLED))
268 if (!core_kernel_data((unsigned long)ops))
269 ops->flags |= FTRACE_OPS_FL_DYNAMIC;
271 if (ops->flags & FTRACE_OPS_FL_GLOBAL) {
272 int first = ftrace_global_list == &ftrace_list_end;
273 add_ftrace_ops(&ftrace_global_list, ops);
274 ops->flags |= FTRACE_OPS_FL_ENABLED;
276 add_ftrace_ops(&ftrace_ops_list, &global_ops);
278 add_ftrace_ops(&ftrace_ops_list, ops);
281 update_ftrace_function();
286 static int __unregister_ftrace_function(struct ftrace_ops *ops)
290 if (WARN_ON(!(ops->flags & FTRACE_OPS_FL_ENABLED)))
293 if (FTRACE_WARN_ON(ops == &global_ops))
296 if (ops->flags & FTRACE_OPS_FL_GLOBAL) {
297 ret = remove_ftrace_ops(&ftrace_global_list, ops);
298 if (!ret && ftrace_global_list == &ftrace_list_end)
299 ret = remove_ftrace_ops(&ftrace_ops_list, &global_ops);
301 ops->flags &= ~FTRACE_OPS_FL_ENABLED;
303 ret = remove_ftrace_ops(&ftrace_ops_list, ops);
309 update_ftrace_function();
312 * Dynamic ops may be freed, we must make sure that all
313 * callers are done before leaving this function.
315 if (ops->flags & FTRACE_OPS_FL_DYNAMIC)
321 static void ftrace_update_pid_func(void)
323 /* Only do something if we are tracing something */
324 if (ftrace_trace_function == ftrace_stub)
327 update_ftrace_function();
330 #ifdef CONFIG_FUNCTION_PROFILER
331 struct ftrace_profile {
332 struct hlist_node node;
334 unsigned long counter;
335 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
336 unsigned long long time;
337 unsigned long long time_squared;
341 struct ftrace_profile_page {
342 struct ftrace_profile_page *next;
344 struct ftrace_profile records[];
347 struct ftrace_profile_stat {
349 struct hlist_head *hash;
350 struct ftrace_profile_page *pages;
351 struct ftrace_profile_page *start;
352 struct tracer_stat stat;
355 #define PROFILE_RECORDS_SIZE \
356 (PAGE_SIZE - offsetof(struct ftrace_profile_page, records))
358 #define PROFILES_PER_PAGE \
359 (PROFILE_RECORDS_SIZE / sizeof(struct ftrace_profile))
361 static int ftrace_profile_bits __read_mostly;
362 static int ftrace_profile_enabled __read_mostly;
364 /* ftrace_profile_lock - synchronize the enable and disable of the profiler */
365 static DEFINE_MUTEX(ftrace_profile_lock);
367 static DEFINE_PER_CPU(struct ftrace_profile_stat, ftrace_profile_stats);
369 #define FTRACE_PROFILE_HASH_SIZE 1024 /* must be power of 2 */
372 function_stat_next(void *v, int idx)
374 struct ftrace_profile *rec = v;
375 struct ftrace_profile_page *pg;
377 pg = (struct ftrace_profile_page *)((unsigned long)rec & PAGE_MASK);
383 if ((void *)rec >= (void *)&pg->records[pg->index]) {
387 rec = &pg->records[0];
395 static void *function_stat_start(struct tracer_stat *trace)
397 struct ftrace_profile_stat *stat =
398 container_of(trace, struct ftrace_profile_stat, stat);
400 if (!stat || !stat->start)
403 return function_stat_next(&stat->start->records[0], 0);
406 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
407 /* function graph compares on total time */
408 static int function_stat_cmp(void *p1, void *p2)
410 struct ftrace_profile *a = p1;
411 struct ftrace_profile *b = p2;
413 if (a->time < b->time)
415 if (a->time > b->time)
421 /* not function graph compares against hits */
422 static int function_stat_cmp(void *p1, void *p2)
424 struct ftrace_profile *a = p1;
425 struct ftrace_profile *b = p2;
427 if (a->counter < b->counter)
429 if (a->counter > b->counter)
436 static int function_stat_headers(struct seq_file *m)
438 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
439 seq_printf(m, " Function "
442 "--- ---- --- ---\n");
444 seq_printf(m, " Function Hit\n"
450 static int function_stat_show(struct seq_file *m, void *v)
452 struct ftrace_profile *rec = v;
453 char str[KSYM_SYMBOL_LEN];
455 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
456 static struct trace_seq s;
457 unsigned long long avg;
458 unsigned long long stddev;
460 mutex_lock(&ftrace_profile_lock);
462 /* we raced with function_profile_reset() */
463 if (unlikely(rec->counter == 0)) {
468 kallsyms_lookup(rec->ip, NULL, NULL, NULL, str);
469 seq_printf(m, " %-30.30s %10lu", str, rec->counter);
471 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
474 do_div(avg, rec->counter);
476 /* Sample standard deviation (s^2) */
477 if (rec->counter <= 1)
480 stddev = rec->time_squared - rec->counter * avg * avg;
482 * Divide only 1000 for ns^2 -> us^2 conversion.
483 * trace_print_graph_duration will divide 1000 again.
485 do_div(stddev, (rec->counter - 1) * 1000);
489 trace_print_graph_duration(rec->time, &s);
490 trace_seq_puts(&s, " ");
491 trace_print_graph_duration(avg, &s);
492 trace_seq_puts(&s, " ");
493 trace_print_graph_duration(stddev, &s);
494 trace_print_seq(m, &s);
498 mutex_unlock(&ftrace_profile_lock);
503 static void ftrace_profile_reset(struct ftrace_profile_stat *stat)
505 struct ftrace_profile_page *pg;
507 pg = stat->pages = stat->start;
510 memset(pg->records, 0, PROFILE_RECORDS_SIZE);
515 memset(stat->hash, 0,
516 FTRACE_PROFILE_HASH_SIZE * sizeof(struct hlist_head));
519 int ftrace_profile_pages_init(struct ftrace_profile_stat *stat)
521 struct ftrace_profile_page *pg;
526 /* If we already allocated, do nothing */
530 stat->pages = (void *)get_zeroed_page(GFP_KERNEL);
534 #ifdef CONFIG_DYNAMIC_FTRACE
535 functions = ftrace_update_tot_cnt;
538 * We do not know the number of functions that exist because
539 * dynamic tracing is what counts them. With past experience
540 * we have around 20K functions. That should be more than enough.
541 * It is highly unlikely we will execute every function in
547 pg = stat->start = stat->pages;
549 pages = DIV_ROUND_UP(functions, PROFILES_PER_PAGE);
551 for (i = 1; i < pages; i++) {
552 pg->next = (void *)get_zeroed_page(GFP_KERNEL);
563 unsigned long tmp = (unsigned long)pg;
575 static int ftrace_profile_init_cpu(int cpu)
577 struct ftrace_profile_stat *stat;
580 stat = &per_cpu(ftrace_profile_stats, cpu);
583 /* If the profile is already created, simply reset it */
584 ftrace_profile_reset(stat);
589 * We are profiling all functions, but usually only a few thousand
590 * functions are hit. We'll make a hash of 1024 items.
592 size = FTRACE_PROFILE_HASH_SIZE;
594 stat->hash = kzalloc(sizeof(struct hlist_head) * size, GFP_KERNEL);
599 if (!ftrace_profile_bits) {
602 for (; size; size >>= 1)
603 ftrace_profile_bits++;
606 /* Preallocate the function profiling pages */
607 if (ftrace_profile_pages_init(stat) < 0) {
616 static int ftrace_profile_init(void)
621 for_each_possible_cpu(cpu) {
622 ret = ftrace_profile_init_cpu(cpu);
630 /* interrupts must be disabled */
631 static struct ftrace_profile *
632 ftrace_find_profiled_func(struct ftrace_profile_stat *stat, unsigned long ip)
634 struct ftrace_profile *rec;
635 struct hlist_head *hhd;
636 struct hlist_node *n;
639 key = hash_long(ip, ftrace_profile_bits);
640 hhd = &stat->hash[key];
642 if (hlist_empty(hhd))
645 hlist_for_each_entry_rcu(rec, n, hhd, node) {
653 static void ftrace_add_profile(struct ftrace_profile_stat *stat,
654 struct ftrace_profile *rec)
658 key = hash_long(rec->ip, ftrace_profile_bits);
659 hlist_add_head_rcu(&rec->node, &stat->hash[key]);
663 * The memory is already allocated, this simply finds a new record to use.
665 static struct ftrace_profile *
666 ftrace_profile_alloc(struct ftrace_profile_stat *stat, unsigned long ip)
668 struct ftrace_profile *rec = NULL;
670 /* prevent recursion (from NMIs) */
671 if (atomic_inc_return(&stat->disabled) != 1)
675 * Try to find the function again since an NMI
676 * could have added it
678 rec = ftrace_find_profiled_func(stat, ip);
682 if (stat->pages->index == PROFILES_PER_PAGE) {
683 if (!stat->pages->next)
685 stat->pages = stat->pages->next;
688 rec = &stat->pages->records[stat->pages->index++];
690 ftrace_add_profile(stat, rec);
693 atomic_dec(&stat->disabled);
699 function_profile_call(unsigned long ip, unsigned long parent_ip)
701 struct ftrace_profile_stat *stat;
702 struct ftrace_profile *rec;
705 if (!ftrace_profile_enabled)
708 local_irq_save(flags);
710 stat = &__get_cpu_var(ftrace_profile_stats);
711 if (!stat->hash || !ftrace_profile_enabled)
714 rec = ftrace_find_profiled_func(stat, ip);
716 rec = ftrace_profile_alloc(stat, ip);
723 local_irq_restore(flags);
726 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
727 static int profile_graph_entry(struct ftrace_graph_ent *trace)
729 function_profile_call(trace->func, 0);
733 static void profile_graph_return(struct ftrace_graph_ret *trace)
735 struct ftrace_profile_stat *stat;
736 unsigned long long calltime;
737 struct ftrace_profile *rec;
740 local_irq_save(flags);
741 stat = &__get_cpu_var(ftrace_profile_stats);
742 if (!stat->hash || !ftrace_profile_enabled)
745 /* If the calltime was zero'd ignore it */
746 if (!trace->calltime)
749 calltime = trace->rettime - trace->calltime;
751 if (!(trace_flags & TRACE_ITER_GRAPH_TIME)) {
754 index = trace->depth;
756 /* Append this call time to the parent time to subtract */
758 current->ret_stack[index - 1].subtime += calltime;
760 if (current->ret_stack[index].subtime < calltime)
761 calltime -= current->ret_stack[index].subtime;
766 rec = ftrace_find_profiled_func(stat, trace->func);
768 rec->time += calltime;
769 rec->time_squared += calltime * calltime;
773 local_irq_restore(flags);
776 static int register_ftrace_profiler(void)
778 return register_ftrace_graph(&profile_graph_return,
779 &profile_graph_entry);
782 static void unregister_ftrace_profiler(void)
784 unregister_ftrace_graph();
787 static struct ftrace_ops ftrace_profile_ops __read_mostly = {
788 .func = function_profile_call,
791 static int register_ftrace_profiler(void)
793 return register_ftrace_function(&ftrace_profile_ops);
796 static void unregister_ftrace_profiler(void)
798 unregister_ftrace_function(&ftrace_profile_ops);
800 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
803 ftrace_profile_write(struct file *filp, const char __user *ubuf,
804 size_t cnt, loff_t *ppos)
809 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
815 mutex_lock(&ftrace_profile_lock);
816 if (ftrace_profile_enabled ^ val) {
818 ret = ftrace_profile_init();
824 ret = register_ftrace_profiler();
829 ftrace_profile_enabled = 1;
831 ftrace_profile_enabled = 0;
833 * unregister_ftrace_profiler calls stop_machine
834 * so this acts like an synchronize_sched.
836 unregister_ftrace_profiler();
840 mutex_unlock(&ftrace_profile_lock);
848 ftrace_profile_read(struct file *filp, char __user *ubuf,
849 size_t cnt, loff_t *ppos)
851 char buf[64]; /* big enough to hold a number */
854 r = sprintf(buf, "%u\n", ftrace_profile_enabled);
855 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
858 static const struct file_operations ftrace_profile_fops = {
859 .open = tracing_open_generic,
860 .read = ftrace_profile_read,
861 .write = ftrace_profile_write,
862 .llseek = default_llseek,
865 /* used to initialize the real stat files */
866 static struct tracer_stat function_stats __initdata = {
868 .stat_start = function_stat_start,
869 .stat_next = function_stat_next,
870 .stat_cmp = function_stat_cmp,
871 .stat_headers = function_stat_headers,
872 .stat_show = function_stat_show
875 static __init void ftrace_profile_debugfs(struct dentry *d_tracer)
877 struct ftrace_profile_stat *stat;
878 struct dentry *entry;
883 for_each_possible_cpu(cpu) {
884 stat = &per_cpu(ftrace_profile_stats, cpu);
886 /* allocate enough for function name + cpu number */
887 name = kmalloc(32, GFP_KERNEL);
890 * The files created are permanent, if something happens
891 * we still do not free memory.
894 "Could not allocate stat file for cpu %d\n",
898 stat->stat = function_stats;
899 snprintf(name, 32, "function%d", cpu);
900 stat->stat.name = name;
901 ret = register_stat_tracer(&stat->stat);
904 "Could not register function stat for cpu %d\n",
911 entry = debugfs_create_file("function_profile_enabled", 0644,
912 d_tracer, NULL, &ftrace_profile_fops);
914 pr_warning("Could not create debugfs "
915 "'function_profile_enabled' entry\n");
918 #else /* CONFIG_FUNCTION_PROFILER */
919 static __init void ftrace_profile_debugfs(struct dentry *d_tracer)
922 #endif /* CONFIG_FUNCTION_PROFILER */
924 static struct pid * const ftrace_swapper_pid = &init_struct_pid;
927 ftrace_filter_lseek(struct file *file, loff_t offset, int whence)
931 if (file->f_mode & FMODE_READ)
932 ret = seq_lseek(file, offset, whence);
934 file->f_pos = ret = 1;
939 #ifdef CONFIG_DYNAMIC_FTRACE
941 #ifndef CONFIG_FTRACE_MCOUNT_RECORD
942 # error Dynamic ftrace depends on MCOUNT_RECORD
945 static struct hlist_head ftrace_func_hash[FTRACE_FUNC_HASHSIZE] __read_mostly;
947 struct ftrace_func_probe {
948 struct hlist_node node;
949 struct ftrace_probe_ops *ops;
957 FTRACE_UPDATE_CALLS = (1 << 0),
958 FTRACE_DISABLE_CALLS = (1 << 1),
959 FTRACE_UPDATE_TRACE_FUNC = (1 << 2),
960 FTRACE_START_FUNC_RET = (1 << 3),
961 FTRACE_STOP_FUNC_RET = (1 << 4),
963 struct ftrace_func_entry {
964 struct hlist_node hlist;
969 unsigned long size_bits;
970 struct hlist_head *buckets;
976 * We make these constant because no one should touch them,
977 * but they are used as the default "empty hash", to avoid allocating
978 * it all the time. These are in a read only section such that if
979 * anyone does try to modify it, it will cause an exception.
981 static const struct hlist_head empty_buckets[1];
982 static const struct ftrace_hash empty_hash = {
983 .buckets = (struct hlist_head *)empty_buckets,
985 #define EMPTY_HASH ((struct ftrace_hash *)&empty_hash)
987 static struct ftrace_ops global_ops = {
989 .notrace_hash = EMPTY_HASH,
990 .filter_hash = EMPTY_HASH,
993 static struct dyn_ftrace *ftrace_new_addrs;
995 static DEFINE_MUTEX(ftrace_regex_lock);
998 struct ftrace_page *next;
1000 struct dyn_ftrace records[];
1003 #define ENTRIES_PER_PAGE \
1004 ((PAGE_SIZE - sizeof(struct ftrace_page)) / sizeof(struct dyn_ftrace))
1006 /* estimate from running different kernels */
1007 #define NR_TO_INIT 10000
1009 static struct ftrace_page *ftrace_pages_start;
1010 static struct ftrace_page *ftrace_pages;
1012 static struct dyn_ftrace *ftrace_free_records;
1014 static bool ftrace_hash_empty(struct ftrace_hash *hash)
1016 return !hash || !hash->count;
1019 static struct ftrace_func_entry *
1020 ftrace_lookup_ip(struct ftrace_hash *hash, unsigned long ip)
1023 struct ftrace_func_entry *entry;
1024 struct hlist_head *hhd;
1025 struct hlist_node *n;
1027 if (ftrace_hash_empty(hash))
1030 if (hash->size_bits > 0)
1031 key = hash_long(ip, hash->size_bits);
1035 hhd = &hash->buckets[key];
1037 hlist_for_each_entry_rcu(entry, n, hhd, hlist) {
1038 if (entry->ip == ip)
1044 static void __add_hash_entry(struct ftrace_hash *hash,
1045 struct ftrace_func_entry *entry)
1047 struct hlist_head *hhd;
1050 if (hash->size_bits)
1051 key = hash_long(entry->ip, hash->size_bits);
1055 hhd = &hash->buckets[key];
1056 hlist_add_head(&entry->hlist, hhd);
1060 static int add_hash_entry(struct ftrace_hash *hash, unsigned long ip)
1062 struct ftrace_func_entry *entry;
1064 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
1069 __add_hash_entry(hash, entry);
1075 free_hash_entry(struct ftrace_hash *hash,
1076 struct ftrace_func_entry *entry)
1078 hlist_del(&entry->hlist);
1084 remove_hash_entry(struct ftrace_hash *hash,
1085 struct ftrace_func_entry *entry)
1087 hlist_del(&entry->hlist);
1091 static void ftrace_hash_clear(struct ftrace_hash *hash)
1093 struct hlist_head *hhd;
1094 struct hlist_node *tp, *tn;
1095 struct ftrace_func_entry *entry;
1096 int size = 1 << hash->size_bits;
1102 for (i = 0; i < size; i++) {
1103 hhd = &hash->buckets[i];
1104 hlist_for_each_entry_safe(entry, tp, tn, hhd, hlist)
1105 free_hash_entry(hash, entry);
1107 FTRACE_WARN_ON(hash->count);
1110 static void free_ftrace_hash(struct ftrace_hash *hash)
1112 if (!hash || hash == EMPTY_HASH)
1114 ftrace_hash_clear(hash);
1115 kfree(hash->buckets);
1119 static void __free_ftrace_hash_rcu(struct rcu_head *rcu)
1121 struct ftrace_hash *hash;
1123 hash = container_of(rcu, struct ftrace_hash, rcu);
1124 free_ftrace_hash(hash);
1127 static void free_ftrace_hash_rcu(struct ftrace_hash *hash)
1129 if (!hash || hash == EMPTY_HASH)
1131 call_rcu_sched(&hash->rcu, __free_ftrace_hash_rcu);
1134 static struct ftrace_hash *alloc_ftrace_hash(int size_bits)
1136 struct ftrace_hash *hash;
1139 hash = kzalloc(sizeof(*hash), GFP_KERNEL);
1143 size = 1 << size_bits;
1144 hash->buckets = kzalloc(sizeof(*hash->buckets) * size, GFP_KERNEL);
1146 if (!hash->buckets) {
1151 hash->size_bits = size_bits;
1156 static struct ftrace_hash *
1157 alloc_and_copy_ftrace_hash(int size_bits, struct ftrace_hash *hash)
1159 struct ftrace_func_entry *entry;
1160 struct ftrace_hash *new_hash;
1161 struct hlist_node *tp;
1166 new_hash = alloc_ftrace_hash(size_bits);
1171 if (ftrace_hash_empty(hash))
1174 size = 1 << hash->size_bits;
1175 for (i = 0; i < size; i++) {
1176 hlist_for_each_entry(entry, tp, &hash->buckets[i], hlist) {
1177 ret = add_hash_entry(new_hash, entry->ip);
1183 FTRACE_WARN_ON(new_hash->count != hash->count);
1188 free_ftrace_hash(new_hash);
1193 ftrace_hash_rec_disable(struct ftrace_ops *ops, int filter_hash);
1195 ftrace_hash_rec_enable(struct ftrace_ops *ops, int filter_hash);
1198 ftrace_hash_move(struct ftrace_ops *ops, int enable,
1199 struct ftrace_hash **dst, struct ftrace_hash *src)
1201 struct ftrace_func_entry *entry;
1202 struct hlist_node *tp, *tn;
1203 struct hlist_head *hhd;
1204 struct ftrace_hash *old_hash;
1205 struct ftrace_hash *new_hash;
1207 int size = src->count;
1213 * Remove the current set, update the hash and add
1216 ftrace_hash_rec_disable(ops, enable);
1219 * If the new source is empty, just free dst and assign it
1223 free_ftrace_hash_rcu(*dst);
1224 rcu_assign_pointer(*dst, EMPTY_HASH);
1225 /* still need to update the function records */
1231 * Make the hash size about 1/2 the # found
1233 for (size /= 2; size; size >>= 1)
1236 /* Don't allocate too much */
1237 if (bits > FTRACE_HASH_MAX_BITS)
1238 bits = FTRACE_HASH_MAX_BITS;
1241 new_hash = alloc_ftrace_hash(bits);
1245 size = 1 << src->size_bits;
1246 for (i = 0; i < size; i++) {
1247 hhd = &src->buckets[i];
1248 hlist_for_each_entry_safe(entry, tp, tn, hhd, hlist) {
1250 key = hash_long(entry->ip, bits);
1253 remove_hash_entry(src, entry);
1254 __add_hash_entry(new_hash, entry);
1259 rcu_assign_pointer(*dst, new_hash);
1260 free_ftrace_hash_rcu(old_hash);
1265 * Enable regardless of ret:
1266 * On success, we enable the new hash.
1267 * On failure, we re-enable the original hash.
1269 ftrace_hash_rec_enable(ops, enable);
1275 * Test the hashes for this ops to see if we want to call
1276 * the ops->func or not.
1278 * It's a match if the ip is in the ops->filter_hash or
1279 * the filter_hash does not exist or is empty,
1281 * the ip is not in the ops->notrace_hash.
1283 * This needs to be called with preemption disabled as
1284 * the hashes are freed with call_rcu_sched().
1287 ftrace_ops_test(struct ftrace_ops *ops, unsigned long ip)
1289 struct ftrace_hash *filter_hash;
1290 struct ftrace_hash *notrace_hash;
1293 filter_hash = rcu_dereference_raw(ops->filter_hash);
1294 notrace_hash = rcu_dereference_raw(ops->notrace_hash);
1296 if ((ftrace_hash_empty(filter_hash) ||
1297 ftrace_lookup_ip(filter_hash, ip)) &&
1298 (ftrace_hash_empty(notrace_hash) ||
1299 !ftrace_lookup_ip(notrace_hash, ip)))
1308 * This is a double for. Do not use 'break' to break out of the loop,
1309 * you must use a goto.
1311 #define do_for_each_ftrace_rec(pg, rec) \
1312 for (pg = ftrace_pages_start; pg; pg = pg->next) { \
1314 for (_____i = 0; _____i < pg->index; _____i++) { \
1315 rec = &pg->records[_____i];
1317 #define while_for_each_ftrace_rec() \
1321 static void __ftrace_hash_rec_update(struct ftrace_ops *ops,
1325 struct ftrace_hash *hash;
1326 struct ftrace_hash *other_hash;
1327 struct ftrace_page *pg;
1328 struct dyn_ftrace *rec;
1332 /* Only update if the ops has been registered */
1333 if (!(ops->flags & FTRACE_OPS_FL_ENABLED))
1337 * In the filter_hash case:
1338 * If the count is zero, we update all records.
1339 * Otherwise we just update the items in the hash.
1341 * In the notrace_hash case:
1342 * We enable the update in the hash.
1343 * As disabling notrace means enabling the tracing,
1344 * and enabling notrace means disabling, the inc variable
1348 hash = ops->filter_hash;
1349 other_hash = ops->notrace_hash;
1350 if (ftrace_hash_empty(hash))
1354 hash = ops->notrace_hash;
1355 other_hash = ops->filter_hash;
1357 * If the notrace hash has no items,
1358 * then there's nothing to do.
1360 if (ftrace_hash_empty(hash))
1364 do_for_each_ftrace_rec(pg, rec) {
1365 int in_other_hash = 0;
1371 * Only the filter_hash affects all records.
1372 * Update if the record is not in the notrace hash.
1374 if (!other_hash || !ftrace_lookup_ip(other_hash, rec->ip))
1377 in_hash = !!ftrace_lookup_ip(hash, rec->ip);
1378 in_other_hash = !!ftrace_lookup_ip(other_hash, rec->ip);
1383 if (filter_hash && in_hash && !in_other_hash)
1385 else if (!filter_hash && in_hash &&
1386 (in_other_hash || ftrace_hash_empty(other_hash)))
1394 if (FTRACE_WARN_ON((rec->flags & ~FTRACE_FL_MASK) == FTRACE_REF_MAX))
1397 if (FTRACE_WARN_ON((rec->flags & ~FTRACE_FL_MASK) == 0))
1402 /* Shortcut, if we handled all records, we are done. */
1403 if (!all && count == hash->count)
1405 } while_for_each_ftrace_rec();
1408 static void ftrace_hash_rec_disable(struct ftrace_ops *ops,
1411 __ftrace_hash_rec_update(ops, filter_hash, 0);
1414 static void ftrace_hash_rec_enable(struct ftrace_ops *ops,
1417 __ftrace_hash_rec_update(ops, filter_hash, 1);
1420 static void ftrace_free_rec(struct dyn_ftrace *rec)
1422 rec->freelist = ftrace_free_records;
1423 ftrace_free_records = rec;
1424 rec->flags |= FTRACE_FL_FREE;
1427 static struct dyn_ftrace *ftrace_alloc_dyn_node(unsigned long ip)
1429 struct dyn_ftrace *rec;
1431 /* First check for freed records */
1432 if (ftrace_free_records) {
1433 rec = ftrace_free_records;
1435 if (unlikely(!(rec->flags & FTRACE_FL_FREE))) {
1436 FTRACE_WARN_ON_ONCE(1);
1437 ftrace_free_records = NULL;
1441 ftrace_free_records = rec->freelist;
1442 memset(rec, 0, sizeof(*rec));
1446 if (ftrace_pages->index == ENTRIES_PER_PAGE) {
1447 if (!ftrace_pages->next) {
1448 /* allocate another page */
1449 ftrace_pages->next =
1450 (void *)get_zeroed_page(GFP_KERNEL);
1451 if (!ftrace_pages->next)
1454 ftrace_pages = ftrace_pages->next;
1457 return &ftrace_pages->records[ftrace_pages->index++];
1460 static struct dyn_ftrace *
1461 ftrace_record_ip(unsigned long ip)
1463 struct dyn_ftrace *rec;
1465 if (ftrace_disabled)
1468 rec = ftrace_alloc_dyn_node(ip);
1473 rec->newlist = ftrace_new_addrs;
1474 ftrace_new_addrs = rec;
1479 static void print_ip_ins(const char *fmt, unsigned char *p)
1483 printk(KERN_CONT "%s", fmt);
1485 for (i = 0; i < MCOUNT_INSN_SIZE; i++)
1486 printk(KERN_CONT "%s%02x", i ? ":" : "", p[i]);
1489 static void ftrace_bug(int failed, unsigned long ip)
1493 FTRACE_WARN_ON_ONCE(1);
1494 pr_info("ftrace faulted on modifying ");
1498 FTRACE_WARN_ON_ONCE(1);
1499 pr_info("ftrace failed to modify ");
1501 print_ip_ins(" actual: ", (unsigned char *)ip);
1502 printk(KERN_CONT "\n");
1505 FTRACE_WARN_ON_ONCE(1);
1506 pr_info("ftrace faulted on writing ");
1510 FTRACE_WARN_ON_ONCE(1);
1511 pr_info("ftrace faulted on unknown error ");
1517 /* Return 1 if the address range is reserved for ftrace */
1518 int ftrace_text_reserved(void *start, void *end)
1520 struct dyn_ftrace *rec;
1521 struct ftrace_page *pg;
1523 do_for_each_ftrace_rec(pg, rec) {
1524 if (rec->ip <= (unsigned long)end &&
1525 rec->ip + MCOUNT_INSN_SIZE > (unsigned long)start)
1527 } while_for_each_ftrace_rec();
1533 __ftrace_replace_code(struct dyn_ftrace *rec, int update)
1535 unsigned long ftrace_addr;
1536 unsigned long flag = 0UL;
1538 ftrace_addr = (unsigned long)FTRACE_ADDR;
1541 * If we are updating calls:
1543 * If the record has a ref count, then we need to enable it
1544 * because someone is using it.
1546 * Otherwise we make sure its disabled.
1548 * If we are disabling calls, then disable all records that
1551 if (update && (rec->flags & ~FTRACE_FL_MASK))
1552 flag = FTRACE_FL_ENABLED;
1554 /* If the state of this record hasn't changed, then do nothing */
1555 if ((rec->flags & FTRACE_FL_ENABLED) == flag)
1559 rec->flags |= FTRACE_FL_ENABLED;
1560 return ftrace_make_call(rec, ftrace_addr);
1563 rec->flags &= ~FTRACE_FL_ENABLED;
1564 return ftrace_make_nop(NULL, rec, ftrace_addr);
1567 static void ftrace_replace_code(int update)
1569 struct dyn_ftrace *rec;
1570 struct ftrace_page *pg;
1573 if (unlikely(ftrace_disabled))
1576 do_for_each_ftrace_rec(pg, rec) {
1577 /* Skip over free records */
1578 if (rec->flags & FTRACE_FL_FREE)
1581 failed = __ftrace_replace_code(rec, update);
1583 ftrace_bug(failed, rec->ip);
1584 /* Stop processing */
1587 } while_for_each_ftrace_rec();
1591 ftrace_code_disable(struct module *mod, struct dyn_ftrace *rec)
1598 if (unlikely(ftrace_disabled))
1601 ret = ftrace_make_nop(mod, rec, MCOUNT_ADDR);
1603 ftrace_bug(ret, ip);
1610 * archs can override this function if they must do something
1611 * before the modifying code is performed.
1613 int __weak ftrace_arch_code_modify_prepare(void)
1619 * archs can override this function if they must do something
1620 * after the modifying code is performed.
1622 int __weak ftrace_arch_code_modify_post_process(void)
1627 static int __ftrace_modify_code(void *data)
1629 int *command = data;
1632 * Do not call function tracer while we update the code.
1633 * We are in stop machine, no worrying about races.
1635 function_trace_stop++;
1637 if (*command & FTRACE_UPDATE_CALLS)
1638 ftrace_replace_code(1);
1639 else if (*command & FTRACE_DISABLE_CALLS)
1640 ftrace_replace_code(0);
1642 if (*command & FTRACE_UPDATE_TRACE_FUNC)
1643 ftrace_update_ftrace_func(ftrace_trace_function);
1645 if (*command & FTRACE_START_FUNC_RET)
1646 ftrace_enable_ftrace_graph_caller();
1647 else if (*command & FTRACE_STOP_FUNC_RET)
1648 ftrace_disable_ftrace_graph_caller();
1650 #ifndef CONFIG_HAVE_FUNCTION_TRACE_MCOUNT_TEST
1652 * For archs that call ftrace_test_stop_func(), we must
1653 * wait till after we update all the function callers
1654 * before we update the callback. This keeps different
1655 * ops that record different functions from corrupting
1658 __ftrace_trace_function = __ftrace_trace_function_delay;
1660 function_trace_stop--;
1665 static void ftrace_run_update_code(int command)
1669 ret = ftrace_arch_code_modify_prepare();
1670 FTRACE_WARN_ON(ret);
1674 stop_machine(__ftrace_modify_code, &command, NULL);
1676 ret = ftrace_arch_code_modify_post_process();
1677 FTRACE_WARN_ON(ret);
1680 static ftrace_func_t saved_ftrace_func;
1681 static int ftrace_start_up;
1682 static int global_start_up;
1684 static void ftrace_startup_enable(int command)
1686 if (saved_ftrace_func != ftrace_trace_function) {
1687 saved_ftrace_func = ftrace_trace_function;
1688 command |= FTRACE_UPDATE_TRACE_FUNC;
1691 if (!command || !ftrace_enabled)
1694 ftrace_run_update_code(command);
1697 static int ftrace_startup(struct ftrace_ops *ops, int command)
1699 bool hash_enable = true;
1702 if (unlikely(ftrace_disabled))
1705 ret = __register_ftrace_function(ops);
1710 command |= FTRACE_UPDATE_CALLS;
1712 /* ops marked global share the filter hashes */
1713 if (ops->flags & FTRACE_OPS_FL_GLOBAL) {
1715 /* Don't update hash if global is already set */
1716 if (global_start_up)
1717 hash_enable = false;
1721 ops->flags |= FTRACE_OPS_FL_ENABLED;
1723 ftrace_hash_rec_enable(ops, 1);
1725 ftrace_startup_enable(command);
1730 static int ftrace_shutdown(struct ftrace_ops *ops, int command)
1732 bool hash_disable = true;
1735 if (unlikely(ftrace_disabled))
1738 ret = __unregister_ftrace_function(ops);
1744 * Just warn in case of unbalance, no need to kill ftrace, it's not
1745 * critical but the ftrace_call callers may be never nopped again after
1746 * further ftrace uses.
1748 WARN_ON_ONCE(ftrace_start_up < 0);
1750 if (ops->flags & FTRACE_OPS_FL_GLOBAL) {
1753 WARN_ON_ONCE(global_start_up < 0);
1754 /* Don't update hash if global still has users */
1755 if (global_start_up) {
1756 WARN_ON_ONCE(!ftrace_start_up);
1757 hash_disable = false;
1762 ftrace_hash_rec_disable(ops, 1);
1764 if (ops != &global_ops || !global_start_up)
1765 ops->flags &= ~FTRACE_OPS_FL_ENABLED;
1767 command |= FTRACE_UPDATE_CALLS;
1769 if (saved_ftrace_func != ftrace_trace_function) {
1770 saved_ftrace_func = ftrace_trace_function;
1771 command |= FTRACE_UPDATE_TRACE_FUNC;
1774 if (!command || !ftrace_enabled)
1777 ftrace_run_update_code(command);
1781 static void ftrace_startup_sysctl(void)
1783 if (unlikely(ftrace_disabled))
1786 /* Force update next time */
1787 saved_ftrace_func = NULL;
1788 /* ftrace_start_up is true if we want ftrace running */
1789 if (ftrace_start_up)
1790 ftrace_run_update_code(FTRACE_UPDATE_CALLS);
1793 static void ftrace_shutdown_sysctl(void)
1795 if (unlikely(ftrace_disabled))
1798 /* ftrace_start_up is true if ftrace is running */
1799 if (ftrace_start_up)
1800 ftrace_run_update_code(FTRACE_DISABLE_CALLS);
1803 static cycle_t ftrace_update_time;
1804 static unsigned long ftrace_update_cnt;
1805 unsigned long ftrace_update_tot_cnt;
1807 static inline int ops_traces_mod(struct ftrace_ops *ops)
1810 * Filter_hash being empty will default to trace module.
1811 * But notrace hash requires a test of individual module functions.
1813 return ftrace_hash_empty(ops->filter_hash) &&
1814 ftrace_hash_empty(ops->notrace_hash);
1818 * Check if the current ops references the record.
1820 * If the ops traces all functions, then it was already accounted for.
1821 * If the ops does not trace the current record function, skip it.
1822 * If the ops ignores the function via notrace filter, skip it.
1825 ops_references_rec(struct ftrace_ops *ops, struct dyn_ftrace *rec)
1827 /* If ops isn't enabled, ignore it */
1828 if (!(ops->flags & FTRACE_OPS_FL_ENABLED))
1831 /* If ops traces all mods, we already accounted for it */
1832 if (ops_traces_mod(ops))
1835 /* The function must be in the filter */
1836 if (!ftrace_hash_empty(ops->filter_hash) &&
1837 !ftrace_lookup_ip(ops->filter_hash, rec->ip))
1840 /* If in notrace hash, we ignore it too */
1841 if (ftrace_lookup_ip(ops->notrace_hash, rec->ip))
1847 static int referenced_filters(struct dyn_ftrace *rec)
1849 struct ftrace_ops *ops;
1852 for (ops = ftrace_ops_list; ops != &ftrace_list_end; ops = ops->next) {
1853 if (ops_references_rec(ops, rec))
1860 static int ftrace_update_code(struct module *mod)
1862 struct dyn_ftrace *p;
1863 cycle_t start, stop;
1864 unsigned long ref = 0;
1868 * When adding a module, we need to check if tracers are
1869 * currently enabled and if they are set to trace all functions.
1870 * If they are, we need to enable the module functions as well
1871 * as update the reference counts for those function records.
1874 struct ftrace_ops *ops;
1876 for (ops = ftrace_ops_list;
1877 ops != &ftrace_list_end; ops = ops->next) {
1878 if (ops->flags & FTRACE_OPS_FL_ENABLED) {
1879 if (ops_traces_mod(ops))
1887 start = ftrace_now(raw_smp_processor_id());
1888 ftrace_update_cnt = 0;
1890 while (ftrace_new_addrs) {
1893 /* If something went wrong, bail without enabling anything */
1894 if (unlikely(ftrace_disabled))
1897 p = ftrace_new_addrs;
1898 ftrace_new_addrs = p->newlist;
1900 cnt += referenced_filters(p);
1904 * Do the initial record conversion from mcount jump
1905 * to the NOP instructions.
1907 if (!ftrace_code_disable(mod, p)) {
1913 ftrace_update_cnt++;
1916 * If the tracing is enabled, go ahead and enable the record.
1918 * The reason not to enable the record immediatelly is the
1919 * inherent check of ftrace_make_nop/ftrace_make_call for
1920 * correct previous instructions. Making first the NOP
1921 * conversion puts the module to the correct state, thus
1922 * passing the ftrace_make_call check.
1924 if (ftrace_start_up && cnt) {
1925 int failed = __ftrace_replace_code(p, 1);
1927 ftrace_bug(failed, p->ip);
1933 stop = ftrace_now(raw_smp_processor_id());
1934 ftrace_update_time = stop - start;
1935 ftrace_update_tot_cnt += ftrace_update_cnt;
1940 static int __init ftrace_dyn_table_alloc(unsigned long num_to_init)
1942 struct ftrace_page *pg;
1946 /* allocate a few pages */
1947 ftrace_pages_start = (void *)get_zeroed_page(GFP_KERNEL);
1948 if (!ftrace_pages_start)
1952 * Allocate a few more pages.
1954 * TODO: have some parser search vmlinux before
1955 * final linking to find all calls to ftrace.
1957 * a) know how many pages to allocate.
1959 * b) set up the table then.
1961 * The dynamic code is still necessary for
1965 pg = ftrace_pages = ftrace_pages_start;
1967 cnt = num_to_init / ENTRIES_PER_PAGE;
1968 pr_info("ftrace: allocating %ld entries in %d pages\n",
1969 num_to_init, cnt + 1);
1971 for (i = 0; i < cnt; i++) {
1972 pg->next = (void *)get_zeroed_page(GFP_KERNEL);
1974 /* If we fail, we'll try later anyway */
1985 FTRACE_ITER_FILTER = (1 << 0),
1986 FTRACE_ITER_NOTRACE = (1 << 1),
1987 FTRACE_ITER_PRINTALL = (1 << 2),
1988 FTRACE_ITER_HASH = (1 << 3),
1989 FTRACE_ITER_ENABLED = (1 << 4),
1992 #define FTRACE_BUFF_MAX (KSYM_SYMBOL_LEN+4) /* room for wildcards */
1994 struct ftrace_iterator {
1997 struct ftrace_page *pg;
1998 struct dyn_ftrace *func;
1999 struct ftrace_func_probe *probe;
2000 struct trace_parser parser;
2001 struct ftrace_hash *hash;
2002 struct ftrace_ops *ops;
2009 t_hash_next(struct seq_file *m, loff_t *pos)
2011 struct ftrace_iterator *iter = m->private;
2012 struct hlist_node *hnd = NULL;
2013 struct hlist_head *hhd;
2019 hnd = &iter->probe->node;
2021 if (iter->hidx >= FTRACE_FUNC_HASHSIZE)
2024 hhd = &ftrace_func_hash[iter->hidx];
2026 if (hlist_empty(hhd)) {
2042 if (WARN_ON_ONCE(!hnd))
2045 iter->probe = hlist_entry(hnd, struct ftrace_func_probe, node);
2050 static void *t_hash_start(struct seq_file *m, loff_t *pos)
2052 struct ftrace_iterator *iter = m->private;
2056 if (iter->func_pos > *pos)
2060 for (l = 0; l <= (*pos - iter->func_pos); ) {
2061 p = t_hash_next(m, &l);
2068 /* Only set this if we have an item */
2069 iter->flags |= FTRACE_ITER_HASH;
2075 t_hash_show(struct seq_file *m, struct ftrace_iterator *iter)
2077 struct ftrace_func_probe *rec;
2080 if (WARN_ON_ONCE(!rec))
2083 if (rec->ops->print)
2084 return rec->ops->print(m, rec->ip, rec->ops, rec->data);
2086 seq_printf(m, "%ps:%ps", (void *)rec->ip, (void *)rec->ops->func);
2089 seq_printf(m, ":%p", rec->data);
2096 t_next(struct seq_file *m, void *v, loff_t *pos)
2098 struct ftrace_iterator *iter = m->private;
2099 struct ftrace_ops *ops = &global_ops;
2100 struct dyn_ftrace *rec = NULL;
2102 if (unlikely(ftrace_disabled))
2105 if (iter->flags & FTRACE_ITER_HASH)
2106 return t_hash_next(m, pos);
2109 iter->pos = iter->func_pos = *pos;
2111 if (iter->flags & FTRACE_ITER_PRINTALL)
2112 return t_hash_start(m, pos);
2115 if (iter->idx >= iter->pg->index) {
2116 if (iter->pg->next) {
2117 iter->pg = iter->pg->next;
2122 rec = &iter->pg->records[iter->idx++];
2123 if ((rec->flags & FTRACE_FL_FREE) ||
2125 ((iter->flags & FTRACE_ITER_FILTER) &&
2126 !(ftrace_lookup_ip(ops->filter_hash, rec->ip))) ||
2128 ((iter->flags & FTRACE_ITER_NOTRACE) &&
2129 !ftrace_lookup_ip(ops->notrace_hash, rec->ip)) ||
2131 ((iter->flags & FTRACE_ITER_ENABLED) &&
2132 !(rec->flags & ~FTRACE_FL_MASK))) {
2140 return t_hash_start(m, pos);
2147 static void reset_iter_read(struct ftrace_iterator *iter)
2151 iter->flags &= ~(FTRACE_ITER_PRINTALL | FTRACE_ITER_HASH);
2154 static void *t_start(struct seq_file *m, loff_t *pos)
2156 struct ftrace_iterator *iter = m->private;
2157 struct ftrace_ops *ops = &global_ops;
2161 mutex_lock(&ftrace_lock);
2163 if (unlikely(ftrace_disabled))
2167 * If an lseek was done, then reset and start from beginning.
2169 if (*pos < iter->pos)
2170 reset_iter_read(iter);
2173 * For set_ftrace_filter reading, if we have the filter
2174 * off, we can short cut and just print out that all
2175 * functions are enabled.
2177 if (iter->flags & FTRACE_ITER_FILTER &&
2178 ftrace_hash_empty(ops->filter_hash)) {
2180 return t_hash_start(m, pos);
2181 iter->flags |= FTRACE_ITER_PRINTALL;
2182 /* reset in case of seek/pread */
2183 iter->flags &= ~FTRACE_ITER_HASH;
2187 if (iter->flags & FTRACE_ITER_HASH)
2188 return t_hash_start(m, pos);
2191 * Unfortunately, we need to restart at ftrace_pages_start
2192 * every time we let go of the ftrace_mutex. This is because
2193 * those pointers can change without the lock.
2195 iter->pg = ftrace_pages_start;
2197 for (l = 0; l <= *pos; ) {
2198 p = t_next(m, p, &l);
2204 if (iter->flags & FTRACE_ITER_FILTER)
2205 return t_hash_start(m, pos);
2213 static void t_stop(struct seq_file *m, void *p)
2215 mutex_unlock(&ftrace_lock);
2218 static int t_show(struct seq_file *m, void *v)
2220 struct ftrace_iterator *iter = m->private;
2221 struct dyn_ftrace *rec;
2223 if (iter->flags & FTRACE_ITER_HASH)
2224 return t_hash_show(m, iter);
2226 if (iter->flags & FTRACE_ITER_PRINTALL) {
2227 seq_printf(m, "#### all functions enabled ####\n");
2236 seq_printf(m, "%ps", (void *)rec->ip);
2237 if (iter->flags & FTRACE_ITER_ENABLED)
2238 seq_printf(m, " (%ld)",
2239 rec->flags & ~FTRACE_FL_MASK);
2240 seq_printf(m, "\n");
2245 static const struct seq_operations show_ftrace_seq_ops = {
2253 ftrace_avail_open(struct inode *inode, struct file *file)
2255 struct ftrace_iterator *iter;
2258 if (unlikely(ftrace_disabled))
2261 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
2265 iter->pg = ftrace_pages_start;
2267 ret = seq_open(file, &show_ftrace_seq_ops);
2269 struct seq_file *m = file->private_data;
2280 ftrace_enabled_open(struct inode *inode, struct file *file)
2282 struct ftrace_iterator *iter;
2285 if (unlikely(ftrace_disabled))
2288 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
2292 iter->pg = ftrace_pages_start;
2293 iter->flags = FTRACE_ITER_ENABLED;
2295 ret = seq_open(file, &show_ftrace_seq_ops);
2297 struct seq_file *m = file->private_data;
2307 static void ftrace_filter_reset(struct ftrace_hash *hash)
2309 mutex_lock(&ftrace_lock);
2310 ftrace_hash_clear(hash);
2311 mutex_unlock(&ftrace_lock);
2315 ftrace_regex_open(struct ftrace_ops *ops, int flag,
2316 struct inode *inode, struct file *file)
2318 struct ftrace_iterator *iter;
2319 struct ftrace_hash *hash;
2322 if (unlikely(ftrace_disabled))
2325 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
2329 if (trace_parser_get_init(&iter->parser, FTRACE_BUFF_MAX)) {
2334 if (flag & FTRACE_ITER_NOTRACE)
2335 hash = ops->notrace_hash;
2337 hash = ops->filter_hash;
2342 if (file->f_mode & FMODE_WRITE) {
2343 mutex_lock(&ftrace_lock);
2344 iter->hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, hash);
2345 mutex_unlock(&ftrace_lock);
2348 trace_parser_put(&iter->parser);
2354 mutex_lock(&ftrace_regex_lock);
2356 if ((file->f_mode & FMODE_WRITE) &&
2357 (file->f_flags & O_TRUNC))
2358 ftrace_filter_reset(iter->hash);
2360 if (file->f_mode & FMODE_READ) {
2361 iter->pg = ftrace_pages_start;
2363 ret = seq_open(file, &show_ftrace_seq_ops);
2365 struct seq_file *m = file->private_data;
2369 free_ftrace_hash(iter->hash);
2370 trace_parser_put(&iter->parser);
2374 file->private_data = iter;
2375 mutex_unlock(&ftrace_regex_lock);
2381 ftrace_filter_open(struct inode *inode, struct file *file)
2383 return ftrace_regex_open(&global_ops, FTRACE_ITER_FILTER,
2388 ftrace_notrace_open(struct inode *inode, struct file *file)
2390 return ftrace_regex_open(&global_ops, FTRACE_ITER_NOTRACE,
2394 static int ftrace_match(char *str, char *regex, int len, int type)
2401 if (strcmp(str, regex) == 0)
2404 case MATCH_FRONT_ONLY:
2405 if (strncmp(str, regex, len) == 0)
2408 case MATCH_MIDDLE_ONLY:
2409 if (strstr(str, regex))
2412 case MATCH_END_ONLY:
2414 if (slen >= len && memcmp(str + slen - len, regex, len) == 0)
2423 enter_record(struct ftrace_hash *hash, struct dyn_ftrace *rec, int not)
2425 struct ftrace_func_entry *entry;
2428 entry = ftrace_lookup_ip(hash, rec->ip);
2430 /* Do nothing if it doesn't exist */
2434 free_hash_entry(hash, entry);
2436 /* Do nothing if it exists */
2440 ret = add_hash_entry(hash, rec->ip);
2446 ftrace_match_record(struct dyn_ftrace *rec, char *mod,
2447 char *regex, int len, int type)
2449 char str[KSYM_SYMBOL_LEN];
2452 kallsyms_lookup(rec->ip, NULL, NULL, &modname, str);
2455 /* module lookup requires matching the module */
2456 if (!modname || strcmp(modname, mod))
2459 /* blank search means to match all funcs in the mod */
2464 return ftrace_match(str, regex, len, type);
2468 match_records(struct ftrace_hash *hash, char *buff,
2469 int len, char *mod, int not)
2471 unsigned search_len = 0;
2472 struct ftrace_page *pg;
2473 struct dyn_ftrace *rec;
2474 int type = MATCH_FULL;
2475 char *search = buff;
2480 type = filter_parse_regex(buff, len, &search, ¬);
2481 search_len = strlen(search);
2484 mutex_lock(&ftrace_lock);
2486 if (unlikely(ftrace_disabled))
2489 do_for_each_ftrace_rec(pg, rec) {
2491 if (ftrace_match_record(rec, mod, search, search_len, type)) {
2492 ret = enter_record(hash, rec, not);
2499 } while_for_each_ftrace_rec();
2501 mutex_unlock(&ftrace_lock);
2507 ftrace_match_records(struct ftrace_hash *hash, char *buff, int len)
2509 return match_records(hash, buff, len, NULL, 0);
2513 ftrace_match_module_records(struct ftrace_hash *hash, char *buff, char *mod)
2517 /* blank or '*' mean the same */
2518 if (strcmp(buff, "*") == 0)
2521 /* handle the case of 'dont filter this module' */
2522 if (strcmp(buff, "!") == 0 || strcmp(buff, "!*") == 0) {
2527 return match_records(hash, buff, strlen(buff), mod, not);
2531 * We register the module command as a template to show others how
2532 * to register the a command as well.
2536 ftrace_mod_callback(struct ftrace_hash *hash,
2537 char *func, char *cmd, char *param, int enable)
2543 * cmd == 'mod' because we only registered this func
2544 * for the 'mod' ftrace_func_command.
2545 * But if you register one func with multiple commands,
2546 * you can tell which command was used by the cmd
2550 /* we must have a module name */
2554 mod = strsep(¶m, ":");
2558 ret = ftrace_match_module_records(hash, func, mod);
2567 static struct ftrace_func_command ftrace_mod_cmd = {
2569 .func = ftrace_mod_callback,
2572 static int __init ftrace_mod_cmd_init(void)
2574 return register_ftrace_command(&ftrace_mod_cmd);
2576 device_initcall(ftrace_mod_cmd_init);
2579 function_trace_probe_call(unsigned long ip, unsigned long parent_ip)
2581 struct ftrace_func_probe *entry;
2582 struct hlist_head *hhd;
2583 struct hlist_node *n;
2586 key = hash_long(ip, FTRACE_HASH_BITS);
2588 hhd = &ftrace_func_hash[key];
2590 if (hlist_empty(hhd))
2594 * Disable preemption for these calls to prevent a RCU grace
2595 * period. This syncs the hash iteration and freeing of items
2596 * on the hash. rcu_read_lock is too dangerous here.
2598 preempt_disable_notrace();
2599 hlist_for_each_entry_rcu(entry, n, hhd, node) {
2600 if (entry->ip == ip)
2601 entry->ops->func(ip, parent_ip, &entry->data);
2603 preempt_enable_notrace();
2606 static struct ftrace_ops trace_probe_ops __read_mostly =
2608 .func = function_trace_probe_call,
2611 static int ftrace_probe_registered;
2613 static void __enable_ftrace_function_probe(void)
2618 if (ftrace_probe_registered)
2621 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
2622 struct hlist_head *hhd = &ftrace_func_hash[i];
2626 /* Nothing registered? */
2627 if (i == FTRACE_FUNC_HASHSIZE)
2630 ret = ftrace_startup(&trace_probe_ops, 0);
2632 ftrace_probe_registered = 1;
2635 static void __disable_ftrace_function_probe(void)
2639 if (!ftrace_probe_registered)
2642 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
2643 struct hlist_head *hhd = &ftrace_func_hash[i];
2648 /* no more funcs left */
2649 ftrace_shutdown(&trace_probe_ops, 0);
2651 ftrace_probe_registered = 0;
2655 static void ftrace_free_entry_rcu(struct rcu_head *rhp)
2657 struct ftrace_func_probe *entry =
2658 container_of(rhp, struct ftrace_func_probe, rcu);
2660 if (entry->ops->free)
2661 entry->ops->free(&entry->data);
2667 register_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
2670 struct ftrace_func_probe *entry;
2671 struct ftrace_page *pg;
2672 struct dyn_ftrace *rec;
2678 type = filter_parse_regex(glob, strlen(glob), &search, ¬);
2679 len = strlen(search);
2681 /* we do not support '!' for function probes */
2685 mutex_lock(&ftrace_lock);
2687 if (unlikely(ftrace_disabled))
2690 do_for_each_ftrace_rec(pg, rec) {
2692 if (!ftrace_match_record(rec, NULL, search, len, type))
2695 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
2697 /* If we did not process any, then return error */
2708 * The caller might want to do something special
2709 * for each function we find. We call the callback
2710 * to give the caller an opportunity to do so.
2712 if (ops->callback) {
2713 if (ops->callback(rec->ip, &entry->data) < 0) {
2714 /* caller does not like this func */
2721 entry->ip = rec->ip;
2723 key = hash_long(entry->ip, FTRACE_HASH_BITS);
2724 hlist_add_head_rcu(&entry->node, &ftrace_func_hash[key]);
2726 } while_for_each_ftrace_rec();
2727 __enable_ftrace_function_probe();
2730 mutex_unlock(&ftrace_lock);
2736 PROBE_TEST_FUNC = 1,
2741 __unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
2742 void *data, int flags)
2744 struct ftrace_func_probe *entry;
2745 struct hlist_node *n, *tmp;
2746 char str[KSYM_SYMBOL_LEN];
2747 int type = MATCH_FULL;
2751 if (glob && (strcmp(glob, "*") == 0 || !strlen(glob)))
2756 type = filter_parse_regex(glob, strlen(glob), &search, ¬);
2757 len = strlen(search);
2759 /* we do not support '!' for function probes */
2764 mutex_lock(&ftrace_lock);
2765 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
2766 struct hlist_head *hhd = &ftrace_func_hash[i];
2768 hlist_for_each_entry_safe(entry, n, tmp, hhd, node) {
2770 /* break up if statements for readability */
2771 if ((flags & PROBE_TEST_FUNC) && entry->ops != ops)
2774 if ((flags & PROBE_TEST_DATA) && entry->data != data)
2777 /* do this last, since it is the most expensive */
2779 kallsyms_lookup(entry->ip, NULL, NULL,
2781 if (!ftrace_match(str, glob, len, type))
2785 hlist_del_rcu(&entry->node);
2786 call_rcu_sched(&entry->rcu, ftrace_free_entry_rcu);
2789 __disable_ftrace_function_probe();
2790 mutex_unlock(&ftrace_lock);
2794 unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
2797 __unregister_ftrace_function_probe(glob, ops, data,
2798 PROBE_TEST_FUNC | PROBE_TEST_DATA);
2802 unregister_ftrace_function_probe_func(char *glob, struct ftrace_probe_ops *ops)
2804 __unregister_ftrace_function_probe(glob, ops, NULL, PROBE_TEST_FUNC);
2807 void unregister_ftrace_function_probe_all(char *glob)
2809 __unregister_ftrace_function_probe(glob, NULL, NULL, 0);
2812 static LIST_HEAD(ftrace_commands);
2813 static DEFINE_MUTEX(ftrace_cmd_mutex);
2815 int register_ftrace_command(struct ftrace_func_command *cmd)
2817 struct ftrace_func_command *p;
2820 mutex_lock(&ftrace_cmd_mutex);
2821 list_for_each_entry(p, &ftrace_commands, list) {
2822 if (strcmp(cmd->name, p->name) == 0) {
2827 list_add(&cmd->list, &ftrace_commands);
2829 mutex_unlock(&ftrace_cmd_mutex);
2834 int unregister_ftrace_command(struct ftrace_func_command *cmd)
2836 struct ftrace_func_command *p, *n;
2839 mutex_lock(&ftrace_cmd_mutex);
2840 list_for_each_entry_safe(p, n, &ftrace_commands, list) {
2841 if (strcmp(cmd->name, p->name) == 0) {
2843 list_del_init(&p->list);
2848 mutex_unlock(&ftrace_cmd_mutex);
2853 static int ftrace_process_regex(struct ftrace_hash *hash,
2854 char *buff, int len, int enable)
2856 char *func, *command, *next = buff;
2857 struct ftrace_func_command *p;
2860 func = strsep(&next, ":");
2863 ret = ftrace_match_records(hash, func, len);
2873 command = strsep(&next, ":");
2875 mutex_lock(&ftrace_cmd_mutex);
2876 list_for_each_entry(p, &ftrace_commands, list) {
2877 if (strcmp(p->name, command) == 0) {
2878 ret = p->func(hash, func, command, next, enable);
2883 mutex_unlock(&ftrace_cmd_mutex);
2889 ftrace_regex_write(struct file *file, const char __user *ubuf,
2890 size_t cnt, loff_t *ppos, int enable)
2892 struct ftrace_iterator *iter;
2893 struct trace_parser *parser;
2899 mutex_lock(&ftrace_regex_lock);
2902 if (unlikely(ftrace_disabled))
2905 if (file->f_mode & FMODE_READ) {
2906 struct seq_file *m = file->private_data;
2909 iter = file->private_data;
2911 parser = &iter->parser;
2912 read = trace_get_user(parser, ubuf, cnt, ppos);
2914 if (read >= 0 && trace_parser_loaded(parser) &&
2915 !trace_parser_cont(parser)) {
2916 ret = ftrace_process_regex(iter->hash, parser->buffer,
2917 parser->idx, enable);
2918 trace_parser_clear(parser);
2925 mutex_unlock(&ftrace_regex_lock);
2931 ftrace_filter_write(struct file *file, const char __user *ubuf,
2932 size_t cnt, loff_t *ppos)
2934 return ftrace_regex_write(file, ubuf, cnt, ppos, 1);
2938 ftrace_notrace_write(struct file *file, const char __user *ubuf,
2939 size_t cnt, loff_t *ppos)
2941 return ftrace_regex_write(file, ubuf, cnt, ppos, 0);
2945 ftrace_set_regex(struct ftrace_ops *ops, unsigned char *buf, int len,
2946 int reset, int enable)
2948 struct ftrace_hash **orig_hash;
2949 struct ftrace_hash *hash;
2952 /* All global ops uses the global ops filters */
2953 if (ops->flags & FTRACE_OPS_FL_GLOBAL)
2956 if (unlikely(ftrace_disabled))
2960 orig_hash = &ops->filter_hash;
2962 orig_hash = &ops->notrace_hash;
2964 hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, *orig_hash);
2968 mutex_lock(&ftrace_regex_lock);
2970 ftrace_filter_reset(hash);
2972 ftrace_match_records(hash, buf, len);
2974 mutex_lock(&ftrace_lock);
2975 ret = ftrace_hash_move(ops, enable, orig_hash, hash);
2976 if (!ret && ops->flags & FTRACE_OPS_FL_ENABLED
2978 ftrace_run_update_code(FTRACE_UPDATE_CALLS);
2980 mutex_unlock(&ftrace_lock);
2982 mutex_unlock(&ftrace_regex_lock);
2984 free_ftrace_hash(hash);
2989 * ftrace_set_filter - set a function to filter on in ftrace
2990 * @ops - the ops to set the filter with
2991 * @buf - the string that holds the function filter text.
2992 * @len - the length of the string.
2993 * @reset - non zero to reset all filters before applying this filter.
2995 * Filters denote which functions should be enabled when tracing is enabled.
2996 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
2998 void ftrace_set_filter(struct ftrace_ops *ops, unsigned char *buf,
3001 ftrace_set_regex(ops, buf, len, reset, 1);
3003 EXPORT_SYMBOL_GPL(ftrace_set_filter);
3006 * ftrace_set_notrace - set a function to not trace in ftrace
3007 * @ops - the ops to set the notrace filter with
3008 * @buf - the string that holds the function notrace text.
3009 * @len - the length of the string.
3010 * @reset - non zero to reset all filters before applying this filter.
3012 * Notrace Filters denote which functions should not be enabled when tracing
3013 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
3016 void ftrace_set_notrace(struct ftrace_ops *ops, unsigned char *buf,
3019 ftrace_set_regex(ops, buf, len, reset, 0);
3021 EXPORT_SYMBOL_GPL(ftrace_set_notrace);
3023 * ftrace_set_filter - set a function to filter on in ftrace
3024 * @ops - the ops to set the filter with
3025 * @buf - the string that holds the function filter text.
3026 * @len - the length of the string.
3027 * @reset - non zero to reset all filters before applying this filter.
3029 * Filters denote which functions should be enabled when tracing is enabled.
3030 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
3032 void ftrace_set_global_filter(unsigned char *buf, int len, int reset)
3034 ftrace_set_regex(&global_ops, buf, len, reset, 1);
3036 EXPORT_SYMBOL_GPL(ftrace_set_global_filter);
3039 * ftrace_set_notrace - set a function to not trace in ftrace
3040 * @ops - the ops to set the notrace filter with
3041 * @buf - the string that holds the function notrace text.
3042 * @len - the length of the string.
3043 * @reset - non zero to reset all filters before applying this filter.
3045 * Notrace Filters denote which functions should not be enabled when tracing
3046 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
3049 void ftrace_set_global_notrace(unsigned char *buf, int len, int reset)
3051 ftrace_set_regex(&global_ops, buf, len, reset, 0);
3053 EXPORT_SYMBOL_GPL(ftrace_set_global_notrace);
3056 * command line interface to allow users to set filters on boot up.
3058 #define FTRACE_FILTER_SIZE COMMAND_LINE_SIZE
3059 static char ftrace_notrace_buf[FTRACE_FILTER_SIZE] __initdata;
3060 static char ftrace_filter_buf[FTRACE_FILTER_SIZE] __initdata;
3062 static int __init set_ftrace_notrace(char *str)
3064 strncpy(ftrace_notrace_buf, str, FTRACE_FILTER_SIZE);
3067 __setup("ftrace_notrace=", set_ftrace_notrace);
3069 static int __init set_ftrace_filter(char *str)
3071 strncpy(ftrace_filter_buf, str, FTRACE_FILTER_SIZE);
3074 __setup("ftrace_filter=", set_ftrace_filter);
3076 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
3077 static char ftrace_graph_buf[FTRACE_FILTER_SIZE] __initdata;
3078 static int ftrace_set_func(unsigned long *array, int *idx, char *buffer);
3080 static int __init set_graph_function(char *str)
3082 strlcpy(ftrace_graph_buf, str, FTRACE_FILTER_SIZE);
3085 __setup("ftrace_graph_filter=", set_graph_function);
3087 static void __init set_ftrace_early_graph(char *buf)
3093 func = strsep(&buf, ",");
3094 /* we allow only one expression at a time */
3095 ret = ftrace_set_func(ftrace_graph_funcs, &ftrace_graph_count,
3098 printk(KERN_DEBUG "ftrace: function %s not "
3099 "traceable\n", func);
3102 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
3105 set_ftrace_early_filter(struct ftrace_ops *ops, char *buf, int enable)
3110 func = strsep(&buf, ",");
3111 ftrace_set_regex(ops, func, strlen(func), 0, enable);
3115 static void __init set_ftrace_early_filters(void)
3117 if (ftrace_filter_buf[0])
3118 set_ftrace_early_filter(&global_ops, ftrace_filter_buf, 1);
3119 if (ftrace_notrace_buf[0])
3120 set_ftrace_early_filter(&global_ops, ftrace_notrace_buf, 0);
3121 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
3122 if (ftrace_graph_buf[0])
3123 set_ftrace_early_graph(ftrace_graph_buf);
3124 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
3128 ftrace_regex_release(struct inode *inode, struct file *file)
3130 struct seq_file *m = (struct seq_file *)file->private_data;
3131 struct ftrace_iterator *iter;
3132 struct ftrace_hash **orig_hash;
3133 struct trace_parser *parser;
3137 mutex_lock(&ftrace_regex_lock);
3138 if (file->f_mode & FMODE_READ) {
3141 seq_release(inode, file);
3143 iter = file->private_data;
3145 parser = &iter->parser;
3146 if (trace_parser_loaded(parser)) {
3147 parser->buffer[parser->idx] = 0;
3148 ftrace_match_records(iter->hash, parser->buffer, parser->idx);
3151 trace_parser_put(parser);
3153 if (file->f_mode & FMODE_WRITE) {
3154 filter_hash = !!(iter->flags & FTRACE_ITER_FILTER);
3157 orig_hash = &iter->ops->filter_hash;
3159 orig_hash = &iter->ops->notrace_hash;
3161 mutex_lock(&ftrace_lock);
3162 ret = ftrace_hash_move(iter->ops, filter_hash,
3163 orig_hash, iter->hash);
3164 if (!ret && (iter->ops->flags & FTRACE_OPS_FL_ENABLED)
3166 ftrace_run_update_code(FTRACE_UPDATE_CALLS);
3168 mutex_unlock(&ftrace_lock);
3170 free_ftrace_hash(iter->hash);
3173 mutex_unlock(&ftrace_regex_lock);
3177 static const struct file_operations ftrace_avail_fops = {
3178 .open = ftrace_avail_open,
3180 .llseek = seq_lseek,
3181 .release = seq_release_private,
3184 static const struct file_operations ftrace_enabled_fops = {
3185 .open = ftrace_enabled_open,
3187 .llseek = seq_lseek,
3188 .release = seq_release_private,
3191 static const struct file_operations ftrace_filter_fops = {
3192 .open = ftrace_filter_open,
3194 .write = ftrace_filter_write,
3195 .llseek = ftrace_filter_lseek,
3196 .release = ftrace_regex_release,
3199 static const struct file_operations ftrace_notrace_fops = {
3200 .open = ftrace_notrace_open,
3202 .write = ftrace_notrace_write,
3203 .llseek = ftrace_filter_lseek,
3204 .release = ftrace_regex_release,
3207 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
3209 static DEFINE_MUTEX(graph_lock);
3211 int ftrace_graph_count;
3212 int ftrace_graph_filter_enabled;
3213 unsigned long ftrace_graph_funcs[FTRACE_GRAPH_MAX_FUNCS] __read_mostly;
3216 __g_next(struct seq_file *m, loff_t *pos)
3218 if (*pos >= ftrace_graph_count)
3220 return &ftrace_graph_funcs[*pos];
3224 g_next(struct seq_file *m, void *v, loff_t *pos)
3227 return __g_next(m, pos);
3230 static void *g_start(struct seq_file *m, loff_t *pos)
3232 mutex_lock(&graph_lock);
3234 /* Nothing, tell g_show to print all functions are enabled */
3235 if (!ftrace_graph_filter_enabled && !*pos)
3238 return __g_next(m, pos);
3241 static void g_stop(struct seq_file *m, void *p)
3243 mutex_unlock(&graph_lock);
3246 static int g_show(struct seq_file *m, void *v)
3248 unsigned long *ptr = v;
3253 if (ptr == (unsigned long *)1) {
3254 seq_printf(m, "#### all functions enabled ####\n");
3258 seq_printf(m, "%ps\n", (void *)*ptr);
3263 static const struct seq_operations ftrace_graph_seq_ops = {
3271 ftrace_graph_open(struct inode *inode, struct file *file)
3275 if (unlikely(ftrace_disabled))
3278 mutex_lock(&graph_lock);
3279 if ((file->f_mode & FMODE_WRITE) &&
3280 (file->f_flags & O_TRUNC)) {
3281 ftrace_graph_filter_enabled = 0;
3282 ftrace_graph_count = 0;
3283 memset(ftrace_graph_funcs, 0, sizeof(ftrace_graph_funcs));
3285 mutex_unlock(&graph_lock);
3287 if (file->f_mode & FMODE_READ)
3288 ret = seq_open(file, &ftrace_graph_seq_ops);
3294 ftrace_graph_release(struct inode *inode, struct file *file)
3296 if (file->f_mode & FMODE_READ)
3297 seq_release(inode, file);
3302 ftrace_set_func(unsigned long *array, int *idx, char *buffer)
3304 struct dyn_ftrace *rec;
3305 struct ftrace_page *pg;
3314 type = filter_parse_regex(buffer, strlen(buffer), &search, ¬);
3315 if (!not && *idx >= FTRACE_GRAPH_MAX_FUNCS)
3318 search_len = strlen(search);
3320 mutex_lock(&ftrace_lock);
3322 if (unlikely(ftrace_disabled)) {
3323 mutex_unlock(&ftrace_lock);
3327 do_for_each_ftrace_rec(pg, rec) {
3329 if (rec->flags & FTRACE_FL_FREE)
3332 if (ftrace_match_record(rec, NULL, search, search_len, type)) {
3333 /* if it is in the array */
3335 for (i = 0; i < *idx; i++) {
3336 if (array[i] == rec->ip) {
3345 array[(*idx)++] = rec->ip;
3346 if (*idx >= FTRACE_GRAPH_MAX_FUNCS)
3351 array[i] = array[--(*idx)];
3357 } while_for_each_ftrace_rec();
3359 mutex_unlock(&ftrace_lock);
3364 ftrace_graph_filter_enabled = !!(*idx);
3370 ftrace_graph_write(struct file *file, const char __user *ubuf,
3371 size_t cnt, loff_t *ppos)
3373 struct trace_parser parser;
3379 mutex_lock(&graph_lock);
3381 if (trace_parser_get_init(&parser, FTRACE_BUFF_MAX)) {
3386 read = trace_get_user(&parser, ubuf, cnt, ppos);
3388 if (read >= 0 && trace_parser_loaded((&parser))) {
3389 parser.buffer[parser.idx] = 0;
3391 /* we allow only one expression at a time */
3392 ret = ftrace_set_func(ftrace_graph_funcs, &ftrace_graph_count,
3401 trace_parser_put(&parser);
3403 mutex_unlock(&graph_lock);
3408 static const struct file_operations ftrace_graph_fops = {
3409 .open = ftrace_graph_open,
3411 .write = ftrace_graph_write,
3412 .llseek = ftrace_filter_lseek,
3413 .release = ftrace_graph_release,
3415 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
3417 static __init int ftrace_init_dyn_debugfs(struct dentry *d_tracer)
3420 trace_create_file("available_filter_functions", 0444,
3421 d_tracer, NULL, &ftrace_avail_fops);
3423 trace_create_file("enabled_functions", 0444,
3424 d_tracer, NULL, &ftrace_enabled_fops);
3426 trace_create_file("set_ftrace_filter", 0644, d_tracer,
3427 NULL, &ftrace_filter_fops);
3429 trace_create_file("set_ftrace_notrace", 0644, d_tracer,
3430 NULL, &ftrace_notrace_fops);
3432 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
3433 trace_create_file("set_graph_function", 0444, d_tracer,
3435 &ftrace_graph_fops);
3436 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
3441 static int ftrace_process_locs(struct module *mod,
3442 unsigned long *start,
3447 unsigned long flags = 0; /* Shut up gcc */
3449 mutex_lock(&ftrace_lock);
3452 addr = ftrace_call_adjust(*p++);
3454 * Some architecture linkers will pad between
3455 * the different mcount_loc sections of different
3456 * object files to satisfy alignments.
3457 * Skip any NULL pointers.
3461 ftrace_record_ip(addr);
3465 * We only need to disable interrupts on start up
3466 * because we are modifying code that an interrupt
3467 * may execute, and the modification is not atomic.
3468 * But for modules, nothing runs the code we modify
3469 * until we are finished with it, and there's no
3470 * reason to cause large interrupt latencies while we do it.
3473 local_irq_save(flags);
3474 ftrace_update_code(mod);
3476 local_irq_restore(flags);
3477 mutex_unlock(&ftrace_lock);
3482 #ifdef CONFIG_MODULES
3483 void ftrace_release_mod(struct module *mod)
3485 struct dyn_ftrace *rec;
3486 struct ftrace_page *pg;
3488 mutex_lock(&ftrace_lock);
3490 if (ftrace_disabled)
3493 do_for_each_ftrace_rec(pg, rec) {
3494 if (within_module_core(rec->ip, mod)) {
3496 * rec->ip is changed in ftrace_free_rec()
3497 * It should not between s and e if record was freed.
3499 FTRACE_WARN_ON(rec->flags & FTRACE_FL_FREE);
3500 ftrace_free_rec(rec);
3502 } while_for_each_ftrace_rec();
3504 mutex_unlock(&ftrace_lock);
3507 static void ftrace_init_module(struct module *mod,
3508 unsigned long *start, unsigned long *end)
3510 if (ftrace_disabled || start == end)
3512 ftrace_process_locs(mod, start, end);
3515 static int ftrace_module_notify_enter(struct notifier_block *self,
3516 unsigned long val, void *data)
3518 struct module *mod = data;
3520 if (val == MODULE_STATE_COMING)
3521 ftrace_init_module(mod, mod->ftrace_callsites,
3522 mod->ftrace_callsites +
3523 mod->num_ftrace_callsites);
3527 static int ftrace_module_notify_exit(struct notifier_block *self,
3528 unsigned long val, void *data)
3530 struct module *mod = data;
3532 if (val == MODULE_STATE_GOING)
3533 ftrace_release_mod(mod);
3538 static int ftrace_module_notify_enter(struct notifier_block *self,
3539 unsigned long val, void *data)
3543 static int ftrace_module_notify_exit(struct notifier_block *self,
3544 unsigned long val, void *data)
3548 #endif /* CONFIG_MODULES */
3550 struct notifier_block ftrace_module_enter_nb = {
3551 .notifier_call = ftrace_module_notify_enter,
3552 .priority = INT_MAX, /* Run before anything that can use kprobes */
3555 struct notifier_block ftrace_module_exit_nb = {
3556 .notifier_call = ftrace_module_notify_exit,
3557 .priority = INT_MIN, /* Run after anything that can remove kprobes */
3560 extern unsigned long __start_mcount_loc[];
3561 extern unsigned long __stop_mcount_loc[];
3563 void __init ftrace_init(void)
3565 unsigned long count, addr, flags;
3568 /* Keep the ftrace pointer to the stub */
3569 addr = (unsigned long)ftrace_stub;
3571 local_irq_save(flags);
3572 ftrace_dyn_arch_init(&addr);
3573 local_irq_restore(flags);
3575 /* ftrace_dyn_arch_init places the return code in addr */
3579 count = __stop_mcount_loc - __start_mcount_loc;
3581 ret = ftrace_dyn_table_alloc(count);
3585 last_ftrace_enabled = ftrace_enabled = 1;
3587 ret = ftrace_process_locs(NULL,
3591 ret = register_module_notifier(&ftrace_module_enter_nb);
3593 pr_warning("Failed to register trace ftrace module enter notifier\n");
3595 ret = register_module_notifier(&ftrace_module_exit_nb);
3597 pr_warning("Failed to register trace ftrace module exit notifier\n");
3599 set_ftrace_early_filters();
3603 ftrace_disabled = 1;
3608 static struct ftrace_ops global_ops = {
3609 .func = ftrace_stub,
3612 static int __init ftrace_nodyn_init(void)
3617 device_initcall(ftrace_nodyn_init);
3619 static inline int ftrace_init_dyn_debugfs(struct dentry *d_tracer) { return 0; }
3620 static inline void ftrace_startup_enable(int command) { }
3621 /* Keep as macros so we do not need to define the commands */
3622 # define ftrace_startup(ops, command) \
3624 int ___ret = __register_ftrace_function(ops); \
3626 (ops)->flags |= FTRACE_OPS_FL_ENABLED; \
3629 # define ftrace_shutdown(ops, command) __unregister_ftrace_function(ops)
3631 # define ftrace_startup_sysctl() do { } while (0)
3632 # define ftrace_shutdown_sysctl() do { } while (0)
3635 ftrace_ops_test(struct ftrace_ops *ops, unsigned long ip)
3640 #endif /* CONFIG_DYNAMIC_FTRACE */
3643 ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip)
3645 struct ftrace_ops *op;
3647 if (unlikely(trace_recursion_test(TRACE_INTERNAL_BIT)))
3650 trace_recursion_set(TRACE_INTERNAL_BIT);
3652 * Some of the ops may be dynamically allocated,
3653 * they must be freed after a synchronize_sched().
3655 preempt_disable_notrace();
3656 op = rcu_dereference_raw(ftrace_ops_list);
3657 while (op != &ftrace_list_end) {
3658 if (ftrace_ops_test(op, ip))
3659 op->func(ip, parent_ip);
3660 op = rcu_dereference_raw(op->next);
3662 preempt_enable_notrace();
3663 trace_recursion_clear(TRACE_INTERNAL_BIT);
3666 static void clear_ftrace_swapper(void)
3668 struct task_struct *p;
3672 for_each_online_cpu(cpu) {
3674 clear_tsk_trace_trace(p);
3679 static void set_ftrace_swapper(void)
3681 struct task_struct *p;
3685 for_each_online_cpu(cpu) {
3687 set_tsk_trace_trace(p);
3692 static void clear_ftrace_pid(struct pid *pid)
3694 struct task_struct *p;
3697 do_each_pid_task(pid, PIDTYPE_PID, p) {
3698 clear_tsk_trace_trace(p);
3699 } while_each_pid_task(pid, PIDTYPE_PID, p);
3705 static void set_ftrace_pid(struct pid *pid)
3707 struct task_struct *p;
3710 do_each_pid_task(pid, PIDTYPE_PID, p) {
3711 set_tsk_trace_trace(p);
3712 } while_each_pid_task(pid, PIDTYPE_PID, p);
3716 static void clear_ftrace_pid_task(struct pid *pid)
3718 if (pid == ftrace_swapper_pid)
3719 clear_ftrace_swapper();
3721 clear_ftrace_pid(pid);
3724 static void set_ftrace_pid_task(struct pid *pid)
3726 if (pid == ftrace_swapper_pid)
3727 set_ftrace_swapper();
3729 set_ftrace_pid(pid);
3732 static int ftrace_pid_add(int p)
3735 struct ftrace_pid *fpid;
3738 mutex_lock(&ftrace_lock);
3741 pid = ftrace_swapper_pid;
3743 pid = find_get_pid(p);
3750 list_for_each_entry(fpid, &ftrace_pids, list)
3751 if (fpid->pid == pid)
3756 fpid = kmalloc(sizeof(*fpid), GFP_KERNEL);
3760 list_add(&fpid->list, &ftrace_pids);
3763 set_ftrace_pid_task(pid);
3765 ftrace_update_pid_func();
3766 ftrace_startup_enable(0);
3768 mutex_unlock(&ftrace_lock);
3772 if (pid != ftrace_swapper_pid)
3776 mutex_unlock(&ftrace_lock);
3780 static void ftrace_pid_reset(void)
3782 struct ftrace_pid *fpid, *safe;
3784 mutex_lock(&ftrace_lock);
3785 list_for_each_entry_safe(fpid, safe, &ftrace_pids, list) {
3786 struct pid *pid = fpid->pid;
3788 clear_ftrace_pid_task(pid);
3790 list_del(&fpid->list);
3794 ftrace_update_pid_func();
3795 ftrace_startup_enable(0);
3797 mutex_unlock(&ftrace_lock);
3800 static void *fpid_start(struct seq_file *m, loff_t *pos)
3802 mutex_lock(&ftrace_lock);
3804 if (list_empty(&ftrace_pids) && (!*pos))
3807 return seq_list_start(&ftrace_pids, *pos);
3810 static void *fpid_next(struct seq_file *m, void *v, loff_t *pos)
3815 return seq_list_next(v, &ftrace_pids, pos);
3818 static void fpid_stop(struct seq_file *m, void *p)
3820 mutex_unlock(&ftrace_lock);
3823 static int fpid_show(struct seq_file *m, void *v)
3825 const struct ftrace_pid *fpid = list_entry(v, struct ftrace_pid, list);
3827 if (v == (void *)1) {
3828 seq_printf(m, "no pid\n");
3832 if (fpid->pid == ftrace_swapper_pid)
3833 seq_printf(m, "swapper tasks\n");
3835 seq_printf(m, "%u\n", pid_vnr(fpid->pid));
3840 static const struct seq_operations ftrace_pid_sops = {
3841 .start = fpid_start,
3848 ftrace_pid_open(struct inode *inode, struct file *file)
3852 if ((file->f_mode & FMODE_WRITE) &&
3853 (file->f_flags & O_TRUNC))
3856 if (file->f_mode & FMODE_READ)
3857 ret = seq_open(file, &ftrace_pid_sops);
3863 ftrace_pid_write(struct file *filp, const char __user *ubuf,
3864 size_t cnt, loff_t *ppos)
3870 if (cnt >= sizeof(buf))
3873 if (copy_from_user(&buf, ubuf, cnt))
3879 * Allow "echo > set_ftrace_pid" or "echo -n '' > set_ftrace_pid"
3880 * to clean the filter quietly.
3882 tmp = strstrip(buf);
3883 if (strlen(tmp) == 0)
3886 ret = strict_strtol(tmp, 10, &val);
3890 ret = ftrace_pid_add(val);
3892 return ret ? ret : cnt;
3896 ftrace_pid_release(struct inode *inode, struct file *file)
3898 if (file->f_mode & FMODE_READ)
3899 seq_release(inode, file);
3904 static const struct file_operations ftrace_pid_fops = {
3905 .open = ftrace_pid_open,
3906 .write = ftrace_pid_write,
3908 .llseek = ftrace_filter_lseek,
3909 .release = ftrace_pid_release,
3912 static __init int ftrace_init_debugfs(void)
3914 struct dentry *d_tracer;
3916 d_tracer = tracing_init_dentry();
3920 ftrace_init_dyn_debugfs(d_tracer);
3922 trace_create_file("set_ftrace_pid", 0644, d_tracer,
3923 NULL, &ftrace_pid_fops);
3925 ftrace_profile_debugfs(d_tracer);
3929 fs_initcall(ftrace_init_debugfs);
3932 * ftrace_kill - kill ftrace
3934 * This function should be used by panic code. It stops ftrace
3935 * but in a not so nice way. If you need to simply kill ftrace
3936 * from a non-atomic section, use ftrace_kill.
3938 void ftrace_kill(void)
3940 ftrace_disabled = 1;
3942 clear_ftrace_function();
3946 * Test if ftrace is dead or not.
3948 int ftrace_is_dead(void)
3950 return ftrace_disabled;
3954 * register_ftrace_function - register a function for profiling
3955 * @ops - ops structure that holds the function for profiling.
3957 * Register a function to be called by all functions in the
3960 * Note: @ops->func and all the functions it calls must be labeled
3961 * with "notrace", otherwise it will go into a
3964 int register_ftrace_function(struct ftrace_ops *ops)
3968 mutex_lock(&ftrace_lock);
3970 ret = ftrace_startup(ops, 0);
3972 mutex_unlock(&ftrace_lock);
3975 EXPORT_SYMBOL_GPL(register_ftrace_function);
3978 * unregister_ftrace_function - unregister a function for profiling.
3979 * @ops - ops structure that holds the function to unregister
3981 * Unregister a function that was added to be called by ftrace profiling.
3983 int unregister_ftrace_function(struct ftrace_ops *ops)
3987 mutex_lock(&ftrace_lock);
3988 ret = ftrace_shutdown(ops, 0);
3989 mutex_unlock(&ftrace_lock);
3993 EXPORT_SYMBOL_GPL(unregister_ftrace_function);
3996 ftrace_enable_sysctl(struct ctl_table *table, int write,
3997 void __user *buffer, size_t *lenp,
4002 mutex_lock(&ftrace_lock);
4004 if (unlikely(ftrace_disabled))
4007 ret = proc_dointvec(table, write, buffer, lenp, ppos);
4009 if (ret || !write || (last_ftrace_enabled == !!ftrace_enabled))
4012 last_ftrace_enabled = !!ftrace_enabled;
4014 if (ftrace_enabled) {
4016 ftrace_startup_sysctl();
4018 /* we are starting ftrace again */
4019 if (ftrace_ops_list != &ftrace_list_end)
4020 update_ftrace_function();
4023 /* stopping ftrace calls (just send to ftrace_stub) */
4024 ftrace_trace_function = ftrace_stub;
4026 ftrace_shutdown_sysctl();
4030 mutex_unlock(&ftrace_lock);
4034 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
4036 static int ftrace_graph_active;
4037 static struct notifier_block ftrace_suspend_notifier;
4039 int ftrace_graph_entry_stub(struct ftrace_graph_ent *trace)
4044 /* The callbacks that hook a function */
4045 trace_func_graph_ret_t ftrace_graph_return =
4046 (trace_func_graph_ret_t)ftrace_stub;
4047 trace_func_graph_ent_t ftrace_graph_entry = ftrace_graph_entry_stub;
4049 /* Try to assign a return stack array on FTRACE_RETSTACK_ALLOC_SIZE tasks. */
4050 static int alloc_retstack_tasklist(struct ftrace_ret_stack **ret_stack_list)
4054 unsigned long flags;
4055 int start = 0, end = FTRACE_RETSTACK_ALLOC_SIZE;
4056 struct task_struct *g, *t;
4058 for (i = 0; i < FTRACE_RETSTACK_ALLOC_SIZE; i++) {
4059 ret_stack_list[i] = kmalloc(FTRACE_RETFUNC_DEPTH
4060 * sizeof(struct ftrace_ret_stack),
4062 if (!ret_stack_list[i]) {
4070 read_lock_irqsave(&tasklist_lock, flags);
4071 do_each_thread(g, t) {
4077 if (t->ret_stack == NULL) {
4078 atomic_set(&t->tracing_graph_pause, 0);
4079 atomic_set(&t->trace_overrun, 0);
4080 t->curr_ret_stack = -1;
4081 /* Make sure the tasks see the -1 first: */
4083 t->ret_stack = ret_stack_list[start++];
4085 } while_each_thread(g, t);
4088 read_unlock_irqrestore(&tasklist_lock, flags);
4090 for (i = start; i < end; i++)
4091 kfree(ret_stack_list[i]);
4096 ftrace_graph_probe_sched_switch(void *ignore,
4097 struct task_struct *prev, struct task_struct *next)
4099 unsigned long long timestamp;
4103 * Does the user want to count the time a function was asleep.
4104 * If so, do not update the time stamps.
4106 if (trace_flags & TRACE_ITER_SLEEP_TIME)
4109 timestamp = trace_clock_local();
4111 prev->ftrace_timestamp = timestamp;
4113 /* only process tasks that we timestamped */
4114 if (!next->ftrace_timestamp)
4118 * Update all the counters in next to make up for the
4119 * time next was sleeping.
4121 timestamp -= next->ftrace_timestamp;
4123 for (index = next->curr_ret_stack; index >= 0; index--)
4124 next->ret_stack[index].calltime += timestamp;
4127 /* Allocate a return stack for each task */
4128 static int start_graph_tracing(void)
4130 struct ftrace_ret_stack **ret_stack_list;
4133 ret_stack_list = kmalloc(FTRACE_RETSTACK_ALLOC_SIZE *
4134 sizeof(struct ftrace_ret_stack *),
4137 if (!ret_stack_list)
4140 /* The cpu_boot init_task->ret_stack will never be freed */
4141 for_each_online_cpu(cpu) {
4142 if (!idle_task(cpu)->ret_stack)
4143 ftrace_graph_init_idle_task(idle_task(cpu), cpu);
4147 ret = alloc_retstack_tasklist(ret_stack_list);
4148 } while (ret == -EAGAIN);
4151 ret = register_trace_sched_switch(ftrace_graph_probe_sched_switch, NULL);
4153 pr_info("ftrace_graph: Couldn't activate tracepoint"
4154 " probe to kernel_sched_switch\n");
4157 kfree(ret_stack_list);
4162 * Hibernation protection.
4163 * The state of the current task is too much unstable during
4164 * suspend/restore to disk. We want to protect against that.
4167 ftrace_suspend_notifier_call(struct notifier_block *bl, unsigned long state,
4171 case PM_HIBERNATION_PREPARE:
4172 pause_graph_tracing();
4175 case PM_POST_HIBERNATION:
4176 unpause_graph_tracing();
4182 /* Just a place holder for function graph */
4183 static struct ftrace_ops fgraph_ops __read_mostly = {
4184 .func = ftrace_stub,
4185 .flags = FTRACE_OPS_FL_GLOBAL,
4188 int register_ftrace_graph(trace_func_graph_ret_t retfunc,
4189 trace_func_graph_ent_t entryfunc)
4193 mutex_lock(&ftrace_lock);
4195 /* we currently allow only one tracer registered at a time */
4196 if (ftrace_graph_active) {
4201 ftrace_suspend_notifier.notifier_call = ftrace_suspend_notifier_call;
4202 register_pm_notifier(&ftrace_suspend_notifier);
4204 ftrace_graph_active++;
4205 ret = start_graph_tracing();
4207 ftrace_graph_active--;
4211 ftrace_graph_return = retfunc;
4212 ftrace_graph_entry = entryfunc;
4214 ret = ftrace_startup(&fgraph_ops, FTRACE_START_FUNC_RET);
4217 mutex_unlock(&ftrace_lock);
4221 void unregister_ftrace_graph(void)
4223 mutex_lock(&ftrace_lock);
4225 if (unlikely(!ftrace_graph_active))
4228 ftrace_graph_active--;
4229 ftrace_graph_return = (trace_func_graph_ret_t)ftrace_stub;
4230 ftrace_graph_entry = ftrace_graph_entry_stub;
4231 ftrace_shutdown(&fgraph_ops, FTRACE_STOP_FUNC_RET);
4232 unregister_pm_notifier(&ftrace_suspend_notifier);
4233 unregister_trace_sched_switch(ftrace_graph_probe_sched_switch, NULL);
4236 mutex_unlock(&ftrace_lock);
4239 static DEFINE_PER_CPU(struct ftrace_ret_stack *, idle_ret_stack);
4242 graph_init_task(struct task_struct *t, struct ftrace_ret_stack *ret_stack)
4244 atomic_set(&t->tracing_graph_pause, 0);
4245 atomic_set(&t->trace_overrun, 0);
4246 t->ftrace_timestamp = 0;
4247 /* make curr_ret_stack visible before we add the ret_stack */
4249 t->ret_stack = ret_stack;
4253 * Allocate a return stack for the idle task. May be the first
4254 * time through, or it may be done by CPU hotplug online.
4256 void ftrace_graph_init_idle_task(struct task_struct *t, int cpu)
4258 t->curr_ret_stack = -1;
4260 * The idle task has no parent, it either has its own
4261 * stack or no stack at all.
4264 WARN_ON(t->ret_stack != per_cpu(idle_ret_stack, cpu));
4266 if (ftrace_graph_active) {
4267 struct ftrace_ret_stack *ret_stack;
4269 ret_stack = per_cpu(idle_ret_stack, cpu);
4271 ret_stack = kmalloc(FTRACE_RETFUNC_DEPTH
4272 * sizeof(struct ftrace_ret_stack),
4276 per_cpu(idle_ret_stack, cpu) = ret_stack;
4278 graph_init_task(t, ret_stack);
4282 /* Allocate a return stack for newly created task */
4283 void ftrace_graph_init_task(struct task_struct *t)
4285 /* Make sure we do not use the parent ret_stack */
4286 t->ret_stack = NULL;
4287 t->curr_ret_stack = -1;
4289 if (ftrace_graph_active) {
4290 struct ftrace_ret_stack *ret_stack;
4292 ret_stack = kmalloc(FTRACE_RETFUNC_DEPTH
4293 * sizeof(struct ftrace_ret_stack),
4297 graph_init_task(t, ret_stack);
4301 void ftrace_graph_exit_task(struct task_struct *t)
4303 struct ftrace_ret_stack *ret_stack = t->ret_stack;
4305 t->ret_stack = NULL;
4306 /* NULL must become visible to IRQs before we free it: */
4312 void ftrace_graph_stop(void)