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/ftrace.h>
26 #include <linux/sysctl.h>
27 #include <linux/slab.h>
28 #include <linux/ctype.h>
29 #include <linux/list.h>
30 #include <linux/hash.h>
31 #include <linux/rcupdate.h>
33 #include <trace/events/sched.h>
35 #include <asm/setup.h>
37 #include "trace_output.h"
38 #include "trace_stat.h"
40 #define FTRACE_WARN_ON(cond) \
48 #define FTRACE_WARN_ON_ONCE(cond) \
51 if (WARN_ON_ONCE(___r)) \
56 /* hash bits for specific function selection */
57 #define FTRACE_HASH_BITS 7
58 #define FTRACE_FUNC_HASHSIZE (1 << FTRACE_HASH_BITS)
59 #define FTRACE_HASH_DEFAULT_BITS 10
60 #define FTRACE_HASH_MAX_BITS 12
62 /* ftrace_enabled is a method to turn ftrace on or off */
63 int ftrace_enabled __read_mostly;
64 static int last_ftrace_enabled;
66 /* Quick disabling of function tracer. */
67 int function_trace_stop;
69 /* List for set_ftrace_pid's pids. */
70 LIST_HEAD(ftrace_pids);
72 struct list_head list;
77 * ftrace_disabled is set when an anomaly is discovered.
78 * ftrace_disabled is much stronger than ftrace_enabled.
80 static int ftrace_disabled __read_mostly;
82 static DEFINE_MUTEX(ftrace_lock);
84 static struct ftrace_ops ftrace_list_end __read_mostly = {
88 static struct ftrace_ops *ftrace_global_list __read_mostly = &ftrace_list_end;
89 static struct ftrace_ops *ftrace_ops_list __read_mostly = &ftrace_list_end;
90 ftrace_func_t ftrace_trace_function __read_mostly = ftrace_stub;
91 static ftrace_func_t __ftrace_trace_function_delay __read_mostly = ftrace_stub;
92 ftrace_func_t __ftrace_trace_function __read_mostly = ftrace_stub;
93 ftrace_func_t ftrace_pid_function __read_mostly = ftrace_stub;
94 static struct ftrace_ops global_ops;
97 ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip);
100 * Traverse the ftrace_global_list, invoking all entries. The reason that we
101 * can use rcu_dereference_raw() is that elements removed from this list
102 * are simply leaked, so there is no need to interact with a grace-period
103 * mechanism. The rcu_dereference_raw() calls are needed to handle
104 * concurrent insertions into the ftrace_global_list.
106 * Silly Alpha and silly pointer-speculation compiler optimizations!
108 static void ftrace_global_list_func(unsigned long ip,
109 unsigned long parent_ip)
111 struct ftrace_ops *op;
113 if (unlikely(trace_recursion_test(TRACE_GLOBAL_BIT)))
116 trace_recursion_set(TRACE_GLOBAL_BIT);
117 op = rcu_dereference_raw(ftrace_global_list); /*see above*/
118 while (op != &ftrace_list_end) {
119 op->func(ip, parent_ip);
120 op = rcu_dereference_raw(op->next); /*see above*/
122 trace_recursion_clear(TRACE_GLOBAL_BIT);
125 static void ftrace_pid_func(unsigned long ip, unsigned long parent_ip)
127 if (!test_tsk_trace_trace(current))
130 ftrace_pid_function(ip, parent_ip);
133 static void set_ftrace_pid_function(ftrace_func_t func)
135 /* do not set ftrace_pid_function to itself! */
136 if (func != ftrace_pid_func)
137 ftrace_pid_function = func;
141 * clear_ftrace_function - reset the ftrace function
143 * This NULLs the ftrace function and in essence stops
144 * tracing. There may be lag
146 void clear_ftrace_function(void)
148 ftrace_trace_function = ftrace_stub;
149 __ftrace_trace_function = ftrace_stub;
150 __ftrace_trace_function_delay = ftrace_stub;
151 ftrace_pid_function = ftrace_stub;
154 #ifndef CONFIG_HAVE_FUNCTION_TRACE_MCOUNT_TEST
156 * For those archs that do not test ftrace_trace_stop in their
157 * mcount call site, we need to do it from C.
159 static void ftrace_test_stop_func(unsigned long ip, unsigned long parent_ip)
161 if (function_trace_stop)
164 __ftrace_trace_function(ip, parent_ip);
168 static void update_global_ops(void)
173 * If there's only one function registered, then call that
174 * function directly. Otherwise, we need to iterate over the
175 * registered callers.
177 if (ftrace_global_list == &ftrace_list_end ||
178 ftrace_global_list->next == &ftrace_list_end)
179 func = ftrace_global_list->func;
181 func = ftrace_global_list_func;
183 /* If we filter on pids, update to use the pid function */
184 if (!list_empty(&ftrace_pids)) {
185 set_ftrace_pid_function(func);
186 func = ftrace_pid_func;
189 global_ops.func = func;
192 static void update_ftrace_function(void)
199 * If we are at the end of the list and this ops is
200 * not dynamic, then have the mcount trampoline call
201 * the function directly
203 if (ftrace_ops_list == &ftrace_list_end ||
204 (ftrace_ops_list->next == &ftrace_list_end &&
205 !(ftrace_ops_list->flags & FTRACE_OPS_FL_DYNAMIC)))
206 func = ftrace_ops_list->func;
208 func = ftrace_ops_list_func;
210 #ifdef CONFIG_HAVE_FUNCTION_TRACE_MCOUNT_TEST
211 ftrace_trace_function = func;
213 #ifdef CONFIG_DYNAMIC_FTRACE
214 /* do not update till all functions have been modified */
215 __ftrace_trace_function_delay = func;
217 __ftrace_trace_function = func;
219 ftrace_trace_function = ftrace_test_stop_func;
223 static void add_ftrace_ops(struct ftrace_ops **list, struct ftrace_ops *ops)
227 * We are entering ops into the list but another
228 * CPU might be walking that list. We need to make sure
229 * the ops->next pointer is valid before another CPU sees
230 * the ops pointer included into the list.
232 rcu_assign_pointer(*list, ops);
235 static int remove_ftrace_ops(struct ftrace_ops **list, struct ftrace_ops *ops)
237 struct ftrace_ops **p;
240 * If we are removing the last function, then simply point
241 * to the ftrace_stub.
243 if (*list == ops && ops->next == &ftrace_list_end) {
244 *list = &ftrace_list_end;
248 for (p = list; *p != &ftrace_list_end; p = &(*p)->next)
259 static int __register_ftrace_function(struct ftrace_ops *ops)
264 if (FTRACE_WARN_ON(ops == &global_ops))
267 if (WARN_ON(ops->flags & FTRACE_OPS_FL_ENABLED))
270 if (!core_kernel_data((unsigned long)ops))
271 ops->flags |= FTRACE_OPS_FL_DYNAMIC;
273 if (ops->flags & FTRACE_OPS_FL_GLOBAL) {
274 int first = ftrace_global_list == &ftrace_list_end;
275 add_ftrace_ops(&ftrace_global_list, ops);
276 ops->flags |= FTRACE_OPS_FL_ENABLED;
278 add_ftrace_ops(&ftrace_ops_list, &global_ops);
280 add_ftrace_ops(&ftrace_ops_list, ops);
283 update_ftrace_function();
288 static int __unregister_ftrace_function(struct ftrace_ops *ops)
295 if (WARN_ON(!(ops->flags & FTRACE_OPS_FL_ENABLED)))
298 if (FTRACE_WARN_ON(ops == &global_ops))
301 if (ops->flags & FTRACE_OPS_FL_GLOBAL) {
302 ret = remove_ftrace_ops(&ftrace_global_list, ops);
303 if (!ret && ftrace_global_list == &ftrace_list_end)
304 ret = remove_ftrace_ops(&ftrace_ops_list, &global_ops);
306 ops->flags &= ~FTRACE_OPS_FL_ENABLED;
308 ret = remove_ftrace_ops(&ftrace_ops_list, ops);
314 update_ftrace_function();
317 * Dynamic ops may be freed, we must make sure that all
318 * callers are done before leaving this function.
320 if (ops->flags & FTRACE_OPS_FL_DYNAMIC)
326 static void ftrace_update_pid_func(void)
328 /* Only do something if we are tracing something */
329 if (ftrace_trace_function == ftrace_stub)
332 update_ftrace_function();
335 #ifdef CONFIG_FUNCTION_PROFILER
336 struct ftrace_profile {
337 struct hlist_node node;
339 unsigned long counter;
340 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
341 unsigned long long time;
342 unsigned long long time_squared;
346 struct ftrace_profile_page {
347 struct ftrace_profile_page *next;
349 struct ftrace_profile records[];
352 struct ftrace_profile_stat {
354 struct hlist_head *hash;
355 struct ftrace_profile_page *pages;
356 struct ftrace_profile_page *start;
357 struct tracer_stat stat;
360 #define PROFILE_RECORDS_SIZE \
361 (PAGE_SIZE - offsetof(struct ftrace_profile_page, records))
363 #define PROFILES_PER_PAGE \
364 (PROFILE_RECORDS_SIZE / sizeof(struct ftrace_profile))
366 static int ftrace_profile_bits __read_mostly;
367 static int ftrace_profile_enabled __read_mostly;
369 /* ftrace_profile_lock - synchronize the enable and disable of the profiler */
370 static DEFINE_MUTEX(ftrace_profile_lock);
372 static DEFINE_PER_CPU(struct ftrace_profile_stat, ftrace_profile_stats);
374 #define FTRACE_PROFILE_HASH_SIZE 1024 /* must be power of 2 */
377 function_stat_next(void *v, int idx)
379 struct ftrace_profile *rec = v;
380 struct ftrace_profile_page *pg;
382 pg = (struct ftrace_profile_page *)((unsigned long)rec & PAGE_MASK);
388 if ((void *)rec >= (void *)&pg->records[pg->index]) {
392 rec = &pg->records[0];
400 static void *function_stat_start(struct tracer_stat *trace)
402 struct ftrace_profile_stat *stat =
403 container_of(trace, struct ftrace_profile_stat, stat);
405 if (!stat || !stat->start)
408 return function_stat_next(&stat->start->records[0], 0);
411 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
412 /* function graph compares on total time */
413 static int function_stat_cmp(void *p1, void *p2)
415 struct ftrace_profile *a = p1;
416 struct ftrace_profile *b = p2;
418 if (a->time < b->time)
420 if (a->time > b->time)
426 /* not function graph compares against hits */
427 static int function_stat_cmp(void *p1, void *p2)
429 struct ftrace_profile *a = p1;
430 struct ftrace_profile *b = p2;
432 if (a->counter < b->counter)
434 if (a->counter > b->counter)
441 static int function_stat_headers(struct seq_file *m)
443 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
444 seq_printf(m, " Function "
447 "--- ---- --- ---\n");
449 seq_printf(m, " Function Hit\n"
455 static int function_stat_show(struct seq_file *m, void *v)
457 struct ftrace_profile *rec = v;
458 char str[KSYM_SYMBOL_LEN];
460 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
461 static struct trace_seq s;
462 unsigned long long avg;
463 unsigned long long stddev;
465 mutex_lock(&ftrace_profile_lock);
467 /* we raced with function_profile_reset() */
468 if (unlikely(rec->counter == 0)) {
473 kallsyms_lookup(rec->ip, NULL, NULL, NULL, str);
474 seq_printf(m, " %-30.30s %10lu", str, rec->counter);
476 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
479 do_div(avg, rec->counter);
481 /* Sample standard deviation (s^2) */
482 if (rec->counter <= 1)
485 stddev = rec->time_squared - rec->counter * avg * avg;
487 * Divide only 1000 for ns^2 -> us^2 conversion.
488 * trace_print_graph_duration will divide 1000 again.
490 do_div(stddev, (rec->counter - 1) * 1000);
494 trace_print_graph_duration(rec->time, &s);
495 trace_seq_puts(&s, " ");
496 trace_print_graph_duration(avg, &s);
497 trace_seq_puts(&s, " ");
498 trace_print_graph_duration(stddev, &s);
499 trace_print_seq(m, &s);
503 mutex_unlock(&ftrace_profile_lock);
508 static void ftrace_profile_reset(struct ftrace_profile_stat *stat)
510 struct ftrace_profile_page *pg;
512 pg = stat->pages = stat->start;
515 memset(pg->records, 0, PROFILE_RECORDS_SIZE);
520 memset(stat->hash, 0,
521 FTRACE_PROFILE_HASH_SIZE * sizeof(struct hlist_head));
524 int ftrace_profile_pages_init(struct ftrace_profile_stat *stat)
526 struct ftrace_profile_page *pg;
531 /* If we already allocated, do nothing */
535 stat->pages = (void *)get_zeroed_page(GFP_KERNEL);
539 #ifdef CONFIG_DYNAMIC_FTRACE
540 functions = ftrace_update_tot_cnt;
543 * We do not know the number of functions that exist because
544 * dynamic tracing is what counts them. With past experience
545 * we have around 20K functions. That should be more than enough.
546 * It is highly unlikely we will execute every function in
552 pg = stat->start = stat->pages;
554 pages = DIV_ROUND_UP(functions, PROFILES_PER_PAGE);
556 for (i = 0; i < pages; i++) {
557 pg->next = (void *)get_zeroed_page(GFP_KERNEL);
568 unsigned long tmp = (unsigned long)pg;
574 free_page((unsigned long)stat->pages);
581 static int ftrace_profile_init_cpu(int cpu)
583 struct ftrace_profile_stat *stat;
586 stat = &per_cpu(ftrace_profile_stats, cpu);
589 /* If the profile is already created, simply reset it */
590 ftrace_profile_reset(stat);
595 * We are profiling all functions, but usually only a few thousand
596 * functions are hit. We'll make a hash of 1024 items.
598 size = FTRACE_PROFILE_HASH_SIZE;
600 stat->hash = kzalloc(sizeof(struct hlist_head) * size, GFP_KERNEL);
605 if (!ftrace_profile_bits) {
608 for (; size; size >>= 1)
609 ftrace_profile_bits++;
612 /* Preallocate the function profiling pages */
613 if (ftrace_profile_pages_init(stat) < 0) {
622 static int ftrace_profile_init(void)
627 for_each_online_cpu(cpu) {
628 ret = ftrace_profile_init_cpu(cpu);
636 /* interrupts must be disabled */
637 static struct ftrace_profile *
638 ftrace_find_profiled_func(struct ftrace_profile_stat *stat, unsigned long ip)
640 struct ftrace_profile *rec;
641 struct hlist_head *hhd;
642 struct hlist_node *n;
645 key = hash_long(ip, ftrace_profile_bits);
646 hhd = &stat->hash[key];
648 if (hlist_empty(hhd))
651 hlist_for_each_entry_rcu(rec, n, hhd, node) {
659 static void ftrace_add_profile(struct ftrace_profile_stat *stat,
660 struct ftrace_profile *rec)
664 key = hash_long(rec->ip, ftrace_profile_bits);
665 hlist_add_head_rcu(&rec->node, &stat->hash[key]);
669 * The memory is already allocated, this simply finds a new record to use.
671 static struct ftrace_profile *
672 ftrace_profile_alloc(struct ftrace_profile_stat *stat, unsigned long ip)
674 struct ftrace_profile *rec = NULL;
676 /* prevent recursion (from NMIs) */
677 if (atomic_inc_return(&stat->disabled) != 1)
681 * Try to find the function again since an NMI
682 * could have added it
684 rec = ftrace_find_profiled_func(stat, ip);
688 if (stat->pages->index == PROFILES_PER_PAGE) {
689 if (!stat->pages->next)
691 stat->pages = stat->pages->next;
694 rec = &stat->pages->records[stat->pages->index++];
696 ftrace_add_profile(stat, rec);
699 atomic_dec(&stat->disabled);
705 function_profile_call(unsigned long ip, unsigned long parent_ip)
707 struct ftrace_profile_stat *stat;
708 struct ftrace_profile *rec;
711 if (!ftrace_profile_enabled)
714 local_irq_save(flags);
716 stat = &__get_cpu_var(ftrace_profile_stats);
717 if (!stat->hash || !ftrace_profile_enabled)
720 rec = ftrace_find_profiled_func(stat, ip);
722 rec = ftrace_profile_alloc(stat, ip);
729 local_irq_restore(flags);
732 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
733 static int profile_graph_entry(struct ftrace_graph_ent *trace)
735 function_profile_call(trace->func, 0);
739 static void profile_graph_return(struct ftrace_graph_ret *trace)
741 struct ftrace_profile_stat *stat;
742 unsigned long long calltime;
743 struct ftrace_profile *rec;
746 local_irq_save(flags);
747 stat = &__get_cpu_var(ftrace_profile_stats);
748 if (!stat->hash || !ftrace_profile_enabled)
751 /* If the calltime was zero'd ignore it */
752 if (!trace->calltime)
755 calltime = trace->rettime - trace->calltime;
757 if (!(trace_flags & TRACE_ITER_GRAPH_TIME)) {
760 index = trace->depth;
762 /* Append this call time to the parent time to subtract */
764 current->ret_stack[index - 1].subtime += calltime;
766 if (current->ret_stack[index].subtime < calltime)
767 calltime -= current->ret_stack[index].subtime;
772 rec = ftrace_find_profiled_func(stat, trace->func);
774 rec->time += calltime;
775 rec->time_squared += calltime * calltime;
779 local_irq_restore(flags);
782 static int register_ftrace_profiler(void)
784 return register_ftrace_graph(&profile_graph_return,
785 &profile_graph_entry);
788 static void unregister_ftrace_profiler(void)
790 unregister_ftrace_graph();
793 static struct ftrace_ops ftrace_profile_ops __read_mostly = {
794 .func = function_profile_call,
797 static int register_ftrace_profiler(void)
799 return register_ftrace_function(&ftrace_profile_ops);
802 static void unregister_ftrace_profiler(void)
804 unregister_ftrace_function(&ftrace_profile_ops);
806 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
809 ftrace_profile_write(struct file *filp, const char __user *ubuf,
810 size_t cnt, loff_t *ppos)
815 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
821 mutex_lock(&ftrace_profile_lock);
822 if (ftrace_profile_enabled ^ val) {
824 ret = ftrace_profile_init();
830 ret = register_ftrace_profiler();
835 ftrace_profile_enabled = 1;
837 ftrace_profile_enabled = 0;
839 * unregister_ftrace_profiler calls stop_machine
840 * so this acts like an synchronize_sched.
842 unregister_ftrace_profiler();
846 mutex_unlock(&ftrace_profile_lock);
854 ftrace_profile_read(struct file *filp, char __user *ubuf,
855 size_t cnt, loff_t *ppos)
857 char buf[64]; /* big enough to hold a number */
860 r = sprintf(buf, "%u\n", ftrace_profile_enabled);
861 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
864 static const struct file_operations ftrace_profile_fops = {
865 .open = tracing_open_generic,
866 .read = ftrace_profile_read,
867 .write = ftrace_profile_write,
868 .llseek = default_llseek,
871 /* used to initialize the real stat files */
872 static struct tracer_stat function_stats __initdata = {
874 .stat_start = function_stat_start,
875 .stat_next = function_stat_next,
876 .stat_cmp = function_stat_cmp,
877 .stat_headers = function_stat_headers,
878 .stat_show = function_stat_show
881 static __init void ftrace_profile_debugfs(struct dentry *d_tracer)
883 struct ftrace_profile_stat *stat;
884 struct dentry *entry;
889 for_each_possible_cpu(cpu) {
890 stat = &per_cpu(ftrace_profile_stats, cpu);
892 /* allocate enough for function name + cpu number */
893 name = kmalloc(32, GFP_KERNEL);
896 * The files created are permanent, if something happens
897 * we still do not free memory.
900 "Could not allocate stat file for cpu %d\n",
904 stat->stat = function_stats;
905 snprintf(name, 32, "function%d", cpu);
906 stat->stat.name = name;
907 ret = register_stat_tracer(&stat->stat);
910 "Could not register function stat for cpu %d\n",
917 entry = debugfs_create_file("function_profile_enabled", 0644,
918 d_tracer, NULL, &ftrace_profile_fops);
920 pr_warning("Could not create debugfs "
921 "'function_profile_enabled' entry\n");
924 #else /* CONFIG_FUNCTION_PROFILER */
925 static __init void ftrace_profile_debugfs(struct dentry *d_tracer)
928 #endif /* CONFIG_FUNCTION_PROFILER */
930 static struct pid * const ftrace_swapper_pid = &init_struct_pid;
932 #ifdef CONFIG_DYNAMIC_FTRACE
934 #ifndef CONFIG_FTRACE_MCOUNT_RECORD
935 # error Dynamic ftrace depends on MCOUNT_RECORD
938 static struct hlist_head ftrace_func_hash[FTRACE_FUNC_HASHSIZE] __read_mostly;
940 struct ftrace_func_probe {
941 struct hlist_node node;
942 struct ftrace_probe_ops *ops;
950 FTRACE_ENABLE_CALLS = (1 << 0),
951 FTRACE_DISABLE_CALLS = (1 << 1),
952 FTRACE_UPDATE_TRACE_FUNC = (1 << 2),
953 FTRACE_START_FUNC_RET = (1 << 3),
954 FTRACE_STOP_FUNC_RET = (1 << 4),
956 struct ftrace_func_entry {
957 struct hlist_node hlist;
962 unsigned long size_bits;
963 struct hlist_head *buckets;
969 * We make these constant because no one should touch them,
970 * but they are used as the default "empty hash", to avoid allocating
971 * it all the time. These are in a read only section such that if
972 * anyone does try to modify it, it will cause an exception.
974 static const struct hlist_head empty_buckets[1];
975 static const struct ftrace_hash empty_hash = {
976 .buckets = (struct hlist_head *)empty_buckets,
978 #define EMPTY_HASH ((struct ftrace_hash *)&empty_hash)
980 static struct ftrace_ops global_ops = {
982 .notrace_hash = EMPTY_HASH,
983 .filter_hash = EMPTY_HASH,
986 static struct dyn_ftrace *ftrace_new_addrs;
988 static DEFINE_MUTEX(ftrace_regex_lock);
991 struct ftrace_page *next;
993 struct dyn_ftrace records[];
996 #define ENTRIES_PER_PAGE \
997 ((PAGE_SIZE - sizeof(struct ftrace_page)) / sizeof(struct dyn_ftrace))
999 /* estimate from running different kernels */
1000 #define NR_TO_INIT 10000
1002 static struct ftrace_page *ftrace_pages_start;
1003 static struct ftrace_page *ftrace_pages;
1005 static struct dyn_ftrace *ftrace_free_records;
1007 static struct ftrace_func_entry *
1008 ftrace_lookup_ip(struct ftrace_hash *hash, unsigned long ip)
1011 struct ftrace_func_entry *entry;
1012 struct hlist_head *hhd;
1013 struct hlist_node *n;
1018 if (hash->size_bits > 0)
1019 key = hash_long(ip, hash->size_bits);
1023 hhd = &hash->buckets[key];
1025 hlist_for_each_entry_rcu(entry, n, hhd, hlist) {
1026 if (entry->ip == ip)
1032 static void __add_hash_entry(struct ftrace_hash *hash,
1033 struct ftrace_func_entry *entry)
1035 struct hlist_head *hhd;
1038 if (hash->size_bits)
1039 key = hash_long(entry->ip, hash->size_bits);
1043 hhd = &hash->buckets[key];
1044 hlist_add_head(&entry->hlist, hhd);
1048 static int add_hash_entry(struct ftrace_hash *hash, unsigned long ip)
1050 struct ftrace_func_entry *entry;
1052 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
1057 __add_hash_entry(hash, entry);
1063 free_hash_entry(struct ftrace_hash *hash,
1064 struct ftrace_func_entry *entry)
1066 hlist_del(&entry->hlist);
1072 remove_hash_entry(struct ftrace_hash *hash,
1073 struct ftrace_func_entry *entry)
1075 hlist_del(&entry->hlist);
1079 static void ftrace_hash_clear(struct ftrace_hash *hash)
1081 struct hlist_head *hhd;
1082 struct hlist_node *tp, *tn;
1083 struct ftrace_func_entry *entry;
1084 int size = 1 << hash->size_bits;
1090 for (i = 0; i < size; i++) {
1091 hhd = &hash->buckets[i];
1092 hlist_for_each_entry_safe(entry, tp, tn, hhd, hlist)
1093 free_hash_entry(hash, entry);
1095 FTRACE_WARN_ON(hash->count);
1098 static void free_ftrace_hash(struct ftrace_hash *hash)
1100 if (!hash || hash == EMPTY_HASH)
1102 ftrace_hash_clear(hash);
1103 kfree(hash->buckets);
1107 static void __free_ftrace_hash_rcu(struct rcu_head *rcu)
1109 struct ftrace_hash *hash;
1111 hash = container_of(rcu, struct ftrace_hash, rcu);
1112 free_ftrace_hash(hash);
1115 static void free_ftrace_hash_rcu(struct ftrace_hash *hash)
1117 if (!hash || hash == EMPTY_HASH)
1119 call_rcu_sched(&hash->rcu, __free_ftrace_hash_rcu);
1122 static struct ftrace_hash *alloc_ftrace_hash(int size_bits)
1124 struct ftrace_hash *hash;
1127 hash = kzalloc(sizeof(*hash), GFP_KERNEL);
1131 size = 1 << size_bits;
1132 hash->buckets = kzalloc(sizeof(*hash->buckets) * size, GFP_KERNEL);
1134 if (!hash->buckets) {
1139 hash->size_bits = size_bits;
1144 static struct ftrace_hash *
1145 alloc_and_copy_ftrace_hash(int size_bits, struct ftrace_hash *hash)
1147 struct ftrace_func_entry *entry;
1148 struct ftrace_hash *new_hash;
1149 struct hlist_node *tp;
1154 new_hash = alloc_ftrace_hash(size_bits);
1159 if (!hash || !hash->count)
1162 size = 1 << hash->size_bits;
1163 for (i = 0; i < size; i++) {
1164 hlist_for_each_entry(entry, tp, &hash->buckets[i], hlist) {
1165 ret = add_hash_entry(new_hash, entry->ip);
1171 FTRACE_WARN_ON(new_hash->count != hash->count);
1176 free_ftrace_hash(new_hash);
1181 ftrace_hash_rec_disable(struct ftrace_ops *ops, int filter_hash);
1183 ftrace_hash_rec_enable(struct ftrace_ops *ops, int filter_hash);
1186 ftrace_hash_move(struct ftrace_ops *ops, int enable,
1187 struct ftrace_hash **dst, struct ftrace_hash *src)
1189 struct ftrace_func_entry *entry;
1190 struct hlist_node *tp, *tn;
1191 struct hlist_head *hhd;
1192 struct ftrace_hash *old_hash;
1193 struct ftrace_hash *new_hash;
1195 int size = src->count;
1201 * Remove the current set, update the hash and add
1204 ftrace_hash_rec_disable(ops, enable);
1207 * If the new source is empty, just free dst and assign it
1211 free_ftrace_hash_rcu(*dst);
1212 rcu_assign_pointer(*dst, EMPTY_HASH);
1217 * Make the hash size about 1/2 the # found
1219 for (size /= 2; size; size >>= 1)
1222 /* Don't allocate too much */
1223 if (bits > FTRACE_HASH_MAX_BITS)
1224 bits = FTRACE_HASH_MAX_BITS;
1227 new_hash = alloc_ftrace_hash(bits);
1231 size = 1 << src->size_bits;
1232 for (i = 0; i < size; i++) {
1233 hhd = &src->buckets[i];
1234 hlist_for_each_entry_safe(entry, tp, tn, hhd, hlist) {
1236 key = hash_long(entry->ip, bits);
1239 remove_hash_entry(src, entry);
1240 __add_hash_entry(new_hash, entry);
1245 rcu_assign_pointer(*dst, new_hash);
1246 free_ftrace_hash_rcu(old_hash);
1251 * Enable regardless of ret:
1252 * On success, we enable the new hash.
1253 * On failure, we re-enable the original hash.
1255 ftrace_hash_rec_enable(ops, enable);
1261 * Test the hashes for this ops to see if we want to call
1262 * the ops->func or not.
1264 * It's a match if the ip is in the ops->filter_hash or
1265 * the filter_hash does not exist or is empty,
1267 * the ip is not in the ops->notrace_hash.
1269 * This needs to be called with preemption disabled as
1270 * the hashes are freed with call_rcu_sched().
1273 ftrace_ops_test(struct ftrace_ops *ops, unsigned long ip)
1275 struct ftrace_hash *filter_hash;
1276 struct ftrace_hash *notrace_hash;
1279 filter_hash = rcu_dereference_raw(ops->filter_hash);
1280 notrace_hash = rcu_dereference_raw(ops->notrace_hash);
1282 if ((!filter_hash || !filter_hash->count ||
1283 ftrace_lookup_ip(filter_hash, ip)) &&
1284 (!notrace_hash || !notrace_hash->count ||
1285 !ftrace_lookup_ip(notrace_hash, ip)))
1294 * This is a double for. Do not use 'break' to break out of the loop,
1295 * you must use a goto.
1297 #define do_for_each_ftrace_rec(pg, rec) \
1298 for (pg = ftrace_pages_start; pg; pg = pg->next) { \
1300 for (_____i = 0; _____i < pg->index; _____i++) { \
1301 rec = &pg->records[_____i];
1303 #define while_for_each_ftrace_rec() \
1307 static void __ftrace_hash_rec_update(struct ftrace_ops *ops,
1311 struct ftrace_hash *hash;
1312 struct ftrace_hash *other_hash;
1313 struct ftrace_page *pg;
1314 struct dyn_ftrace *rec;
1318 /* Only update if the ops has been registered */
1319 if (!(ops->flags & FTRACE_OPS_FL_ENABLED))
1323 * In the filter_hash case:
1324 * If the count is zero, we update all records.
1325 * Otherwise we just update the items in the hash.
1327 * In the notrace_hash case:
1328 * We enable the update in the hash.
1329 * As disabling notrace means enabling the tracing,
1330 * and enabling notrace means disabling, the inc variable
1334 hash = ops->filter_hash;
1335 other_hash = ops->notrace_hash;
1336 if (!hash || !hash->count)
1340 hash = ops->notrace_hash;
1341 other_hash = ops->filter_hash;
1343 * If the notrace hash has no items,
1344 * then there's nothing to do.
1346 if (hash && !hash->count)
1350 do_for_each_ftrace_rec(pg, rec) {
1351 int in_other_hash = 0;
1357 * Only the filter_hash affects all records.
1358 * Update if the record is not in the notrace hash.
1360 if (!other_hash || !ftrace_lookup_ip(other_hash, rec->ip))
1363 in_hash = hash && !!ftrace_lookup_ip(hash, rec->ip);
1364 in_other_hash = other_hash && !!ftrace_lookup_ip(other_hash, rec->ip);
1369 if (filter_hash && in_hash && !in_other_hash)
1371 else if (!filter_hash && in_hash &&
1372 (in_other_hash || !other_hash->count))
1380 if (FTRACE_WARN_ON((rec->flags & ~FTRACE_FL_MASK) == FTRACE_REF_MAX))
1383 if (FTRACE_WARN_ON((rec->flags & ~FTRACE_FL_MASK) == 0))
1388 /* Shortcut, if we handled all records, we are done. */
1389 if (!all && count == hash->count)
1391 } while_for_each_ftrace_rec();
1394 static void ftrace_hash_rec_disable(struct ftrace_ops *ops,
1397 __ftrace_hash_rec_update(ops, filter_hash, 0);
1400 static void ftrace_hash_rec_enable(struct ftrace_ops *ops,
1403 __ftrace_hash_rec_update(ops, filter_hash, 1);
1406 static void ftrace_free_rec(struct dyn_ftrace *rec)
1408 rec->freelist = ftrace_free_records;
1409 ftrace_free_records = rec;
1410 rec->flags |= FTRACE_FL_FREE;
1413 static struct dyn_ftrace *ftrace_alloc_dyn_node(unsigned long ip)
1415 struct dyn_ftrace *rec;
1417 /* First check for freed records */
1418 if (ftrace_free_records) {
1419 rec = ftrace_free_records;
1421 if (unlikely(!(rec->flags & FTRACE_FL_FREE))) {
1422 FTRACE_WARN_ON_ONCE(1);
1423 ftrace_free_records = NULL;
1427 ftrace_free_records = rec->freelist;
1428 memset(rec, 0, sizeof(*rec));
1432 if (ftrace_pages->index == ENTRIES_PER_PAGE) {
1433 if (!ftrace_pages->next) {
1434 /* allocate another page */
1435 ftrace_pages->next =
1436 (void *)get_zeroed_page(GFP_KERNEL);
1437 if (!ftrace_pages->next)
1440 ftrace_pages = ftrace_pages->next;
1443 return &ftrace_pages->records[ftrace_pages->index++];
1446 static struct dyn_ftrace *
1447 ftrace_record_ip(unsigned long ip)
1449 struct dyn_ftrace *rec;
1451 if (ftrace_disabled)
1454 rec = ftrace_alloc_dyn_node(ip);
1459 rec->newlist = ftrace_new_addrs;
1460 ftrace_new_addrs = rec;
1465 static void print_ip_ins(const char *fmt, unsigned char *p)
1469 printk(KERN_CONT "%s", fmt);
1471 for (i = 0; i < MCOUNT_INSN_SIZE; i++)
1472 printk(KERN_CONT "%s%02x", i ? ":" : "", p[i]);
1475 static void ftrace_bug(int failed, unsigned long ip)
1479 FTRACE_WARN_ON_ONCE(1);
1480 pr_info("ftrace faulted on modifying ");
1484 FTRACE_WARN_ON_ONCE(1);
1485 pr_info("ftrace failed to modify ");
1487 print_ip_ins(" actual: ", (unsigned char *)ip);
1488 printk(KERN_CONT "\n");
1491 FTRACE_WARN_ON_ONCE(1);
1492 pr_info("ftrace faulted on writing ");
1496 FTRACE_WARN_ON_ONCE(1);
1497 pr_info("ftrace faulted on unknown error ");
1503 /* Return 1 if the address range is reserved for ftrace */
1504 int ftrace_text_reserved(void *start, void *end)
1506 struct dyn_ftrace *rec;
1507 struct ftrace_page *pg;
1509 do_for_each_ftrace_rec(pg, rec) {
1510 if (rec->ip <= (unsigned long)end &&
1511 rec->ip + MCOUNT_INSN_SIZE > (unsigned long)start)
1513 } while_for_each_ftrace_rec();
1519 __ftrace_replace_code(struct dyn_ftrace *rec, int enable)
1521 unsigned long ftrace_addr;
1522 unsigned long flag = 0UL;
1524 ftrace_addr = (unsigned long)FTRACE_ADDR;
1527 * If we are enabling tracing:
1529 * If the record has a ref count, then we need to enable it
1530 * because someone is using it.
1532 * Otherwise we make sure its disabled.
1534 * If we are disabling tracing, then disable all records that
1537 if (enable && (rec->flags & ~FTRACE_FL_MASK))
1538 flag = FTRACE_FL_ENABLED;
1540 /* If the state of this record hasn't changed, then do nothing */
1541 if ((rec->flags & FTRACE_FL_ENABLED) == flag)
1545 rec->flags |= FTRACE_FL_ENABLED;
1546 return ftrace_make_call(rec, ftrace_addr);
1549 rec->flags &= ~FTRACE_FL_ENABLED;
1550 return ftrace_make_nop(NULL, rec, ftrace_addr);
1553 static void ftrace_replace_code(int enable)
1555 struct dyn_ftrace *rec;
1556 struct ftrace_page *pg;
1559 if (unlikely(ftrace_disabled))
1562 do_for_each_ftrace_rec(pg, rec) {
1563 /* Skip over free records */
1564 if (rec->flags & FTRACE_FL_FREE)
1567 failed = __ftrace_replace_code(rec, enable);
1569 ftrace_bug(failed, rec->ip);
1570 /* Stop processing */
1573 } while_for_each_ftrace_rec();
1577 ftrace_code_disable(struct module *mod, struct dyn_ftrace *rec)
1584 if (unlikely(ftrace_disabled))
1587 ret = ftrace_make_nop(mod, rec, MCOUNT_ADDR);
1589 ftrace_bug(ret, ip);
1596 * archs can override this function if they must do something
1597 * before the modifying code is performed.
1599 int __weak ftrace_arch_code_modify_prepare(void)
1605 * archs can override this function if they must do something
1606 * after the modifying code is performed.
1608 int __weak ftrace_arch_code_modify_post_process(void)
1613 static int __ftrace_modify_code(void *data)
1615 int *command = data;
1618 * Do not call function tracer while we update the code.
1619 * We are in stop machine, no worrying about races.
1621 function_trace_stop++;
1623 if (*command & FTRACE_ENABLE_CALLS)
1624 ftrace_replace_code(1);
1625 else if (*command & FTRACE_DISABLE_CALLS)
1626 ftrace_replace_code(0);
1628 if (*command & FTRACE_UPDATE_TRACE_FUNC)
1629 ftrace_update_ftrace_func(ftrace_trace_function);
1631 if (*command & FTRACE_START_FUNC_RET)
1632 ftrace_enable_ftrace_graph_caller();
1633 else if (*command & FTRACE_STOP_FUNC_RET)
1634 ftrace_disable_ftrace_graph_caller();
1636 #ifndef CONFIG_HAVE_FUNCTION_TRACE_MCOUNT_TEST
1638 * For archs that call ftrace_test_stop_func(), we must
1639 * wait till after we update all the function callers
1640 * before we update the callback. This keeps different
1641 * ops that record different functions from corrupting
1644 __ftrace_trace_function = __ftrace_trace_function_delay;
1646 function_trace_stop--;
1651 static void ftrace_run_update_code(int command)
1655 ret = ftrace_arch_code_modify_prepare();
1656 FTRACE_WARN_ON(ret);
1660 stop_machine(__ftrace_modify_code, &command, NULL);
1662 ret = ftrace_arch_code_modify_post_process();
1663 FTRACE_WARN_ON(ret);
1666 static ftrace_func_t saved_ftrace_func;
1667 static int ftrace_start_up;
1668 static int global_start_up;
1670 static void ftrace_startup_enable(int command)
1672 if (saved_ftrace_func != ftrace_trace_function) {
1673 saved_ftrace_func = ftrace_trace_function;
1674 command |= FTRACE_UPDATE_TRACE_FUNC;
1677 if (!command || !ftrace_enabled)
1680 ftrace_run_update_code(command);
1683 static int ftrace_startup(struct ftrace_ops *ops, int command)
1685 bool hash_enable = true;
1687 if (unlikely(ftrace_disabled))
1691 command |= FTRACE_ENABLE_CALLS;
1693 /* ops marked global share the filter hashes */
1694 if (ops->flags & FTRACE_OPS_FL_GLOBAL) {
1696 /* Don't update hash if global is already set */
1697 if (global_start_up)
1698 hash_enable = false;
1702 ops->flags |= FTRACE_OPS_FL_ENABLED;
1704 ftrace_hash_rec_enable(ops, 1);
1706 ftrace_startup_enable(command);
1711 static void ftrace_shutdown(struct ftrace_ops *ops, int command)
1713 bool hash_disable = true;
1715 if (unlikely(ftrace_disabled))
1720 * Just warn in case of unbalance, no need to kill ftrace, it's not
1721 * critical but the ftrace_call callers may be never nopped again after
1722 * further ftrace uses.
1724 WARN_ON_ONCE(ftrace_start_up < 0);
1726 if (ops->flags & FTRACE_OPS_FL_GLOBAL) {
1729 WARN_ON_ONCE(global_start_up < 0);
1730 /* Don't update hash if global still has users */
1731 if (global_start_up) {
1732 WARN_ON_ONCE(!ftrace_start_up);
1733 hash_disable = false;
1738 ftrace_hash_rec_disable(ops, 1);
1740 if (ops != &global_ops || !global_start_up)
1741 ops->flags &= ~FTRACE_OPS_FL_ENABLED;
1743 if (!ftrace_start_up)
1744 command |= FTRACE_DISABLE_CALLS;
1746 if (saved_ftrace_func != ftrace_trace_function) {
1747 saved_ftrace_func = ftrace_trace_function;
1748 command |= FTRACE_UPDATE_TRACE_FUNC;
1751 if (!command || !ftrace_enabled)
1754 ftrace_run_update_code(command);
1757 static void ftrace_startup_sysctl(void)
1759 if (unlikely(ftrace_disabled))
1762 /* Force update next time */
1763 saved_ftrace_func = NULL;
1764 /* ftrace_start_up is true if we want ftrace running */
1765 if (ftrace_start_up)
1766 ftrace_run_update_code(FTRACE_ENABLE_CALLS);
1769 static void ftrace_shutdown_sysctl(void)
1771 if (unlikely(ftrace_disabled))
1774 /* ftrace_start_up is true if ftrace is running */
1775 if (ftrace_start_up)
1776 ftrace_run_update_code(FTRACE_DISABLE_CALLS);
1779 static cycle_t ftrace_update_time;
1780 static unsigned long ftrace_update_cnt;
1781 unsigned long ftrace_update_tot_cnt;
1783 static int ops_traces_mod(struct ftrace_ops *ops)
1785 struct ftrace_hash *hash;
1787 hash = ops->filter_hash;
1788 return !!(!hash || !hash->count);
1791 static int ftrace_update_code(struct module *mod)
1793 struct dyn_ftrace *p;
1794 cycle_t start, stop;
1795 unsigned long ref = 0;
1798 * When adding a module, we need to check if tracers are
1799 * currently enabled and if they are set to trace all functions.
1800 * If they are, we need to enable the module functions as well
1801 * as update the reference counts for those function records.
1804 struct ftrace_ops *ops;
1806 for (ops = ftrace_ops_list;
1807 ops != &ftrace_list_end; ops = ops->next) {
1808 if (ops->flags & FTRACE_OPS_FL_ENABLED &&
1809 ops_traces_mod(ops))
1814 start = ftrace_now(raw_smp_processor_id());
1815 ftrace_update_cnt = 0;
1817 while (ftrace_new_addrs) {
1819 /* If something went wrong, bail without enabling anything */
1820 if (unlikely(ftrace_disabled))
1823 p = ftrace_new_addrs;
1824 ftrace_new_addrs = p->newlist;
1828 * Do the initial record conversion from mcount jump
1829 * to the NOP instructions.
1831 if (!ftrace_code_disable(mod, p)) {
1837 ftrace_update_cnt++;
1840 * If the tracing is enabled, go ahead and enable the record.
1842 * The reason not to enable the record immediatelly is the
1843 * inherent check of ftrace_make_nop/ftrace_make_call for
1844 * correct previous instructions. Making first the NOP
1845 * conversion puts the module to the correct state, thus
1846 * passing the ftrace_make_call check.
1848 if (ftrace_start_up && ref) {
1849 int failed = __ftrace_replace_code(p, 1);
1851 ftrace_bug(failed, p->ip);
1857 stop = ftrace_now(raw_smp_processor_id());
1858 ftrace_update_time = stop - start;
1859 ftrace_update_tot_cnt += ftrace_update_cnt;
1864 static int __init ftrace_dyn_table_alloc(unsigned long num_to_init)
1866 struct ftrace_page *pg;
1870 /* allocate a few pages */
1871 ftrace_pages_start = (void *)get_zeroed_page(GFP_KERNEL);
1872 if (!ftrace_pages_start)
1876 * Allocate a few more pages.
1878 * TODO: have some parser search vmlinux before
1879 * final linking to find all calls to ftrace.
1881 * a) know how many pages to allocate.
1883 * b) set up the table then.
1885 * The dynamic code is still necessary for
1889 pg = ftrace_pages = ftrace_pages_start;
1891 cnt = num_to_init / ENTRIES_PER_PAGE;
1892 pr_info("ftrace: allocating %ld entries in %d pages\n",
1893 num_to_init, cnt + 1);
1895 for (i = 0; i < cnt; i++) {
1896 pg->next = (void *)get_zeroed_page(GFP_KERNEL);
1898 /* If we fail, we'll try later anyway */
1909 FTRACE_ITER_FILTER = (1 << 0),
1910 FTRACE_ITER_NOTRACE = (1 << 1),
1911 FTRACE_ITER_PRINTALL = (1 << 2),
1912 FTRACE_ITER_HASH = (1 << 3),
1913 FTRACE_ITER_ENABLED = (1 << 4),
1916 #define FTRACE_BUFF_MAX (KSYM_SYMBOL_LEN+4) /* room for wildcards */
1918 struct ftrace_iterator {
1921 struct ftrace_page *pg;
1922 struct dyn_ftrace *func;
1923 struct ftrace_func_probe *probe;
1924 struct trace_parser parser;
1925 struct ftrace_hash *hash;
1926 struct ftrace_ops *ops;
1933 t_hash_next(struct seq_file *m, loff_t *pos)
1935 struct ftrace_iterator *iter = m->private;
1936 struct hlist_node *hnd = NULL;
1937 struct hlist_head *hhd;
1943 hnd = &iter->probe->node;
1945 if (iter->hidx >= FTRACE_FUNC_HASHSIZE)
1948 hhd = &ftrace_func_hash[iter->hidx];
1950 if (hlist_empty(hhd)) {
1966 if (WARN_ON_ONCE(!hnd))
1969 iter->probe = hlist_entry(hnd, struct ftrace_func_probe, node);
1974 static void *t_hash_start(struct seq_file *m, loff_t *pos)
1976 struct ftrace_iterator *iter = m->private;
1980 if (iter->func_pos > *pos)
1984 for (l = 0; l <= (*pos - iter->func_pos); ) {
1985 p = t_hash_next(m, &l);
1992 /* Only set this if we have an item */
1993 iter->flags |= FTRACE_ITER_HASH;
1999 t_hash_show(struct seq_file *m, struct ftrace_iterator *iter)
2001 struct ftrace_func_probe *rec;
2004 if (WARN_ON_ONCE(!rec))
2007 if (rec->ops->print)
2008 return rec->ops->print(m, rec->ip, rec->ops, rec->data);
2010 seq_printf(m, "%ps:%ps", (void *)rec->ip, (void *)rec->ops->func);
2013 seq_printf(m, ":%p", rec->data);
2020 t_next(struct seq_file *m, void *v, loff_t *pos)
2022 struct ftrace_iterator *iter = m->private;
2023 struct ftrace_ops *ops = &global_ops;
2024 struct dyn_ftrace *rec = NULL;
2026 if (unlikely(ftrace_disabled))
2029 if (iter->flags & FTRACE_ITER_HASH)
2030 return t_hash_next(m, pos);
2033 iter->pos = iter->func_pos = *pos;
2035 if (iter->flags & FTRACE_ITER_PRINTALL)
2036 return t_hash_start(m, pos);
2039 if (iter->idx >= iter->pg->index) {
2040 if (iter->pg->next) {
2041 iter->pg = iter->pg->next;
2046 rec = &iter->pg->records[iter->idx++];
2047 if ((rec->flags & FTRACE_FL_FREE) ||
2049 ((iter->flags & FTRACE_ITER_FILTER) &&
2050 !(ftrace_lookup_ip(ops->filter_hash, rec->ip))) ||
2052 ((iter->flags & FTRACE_ITER_NOTRACE) &&
2053 !ftrace_lookup_ip(ops->notrace_hash, rec->ip)) ||
2055 ((iter->flags & FTRACE_ITER_ENABLED) &&
2056 !(rec->flags & ~FTRACE_FL_MASK))) {
2064 return t_hash_start(m, pos);
2071 static void reset_iter_read(struct ftrace_iterator *iter)
2075 iter->flags &= ~(FTRACE_ITER_PRINTALL & FTRACE_ITER_HASH);
2078 static void *t_start(struct seq_file *m, loff_t *pos)
2080 struct ftrace_iterator *iter = m->private;
2081 struct ftrace_ops *ops = &global_ops;
2085 mutex_lock(&ftrace_lock);
2087 if (unlikely(ftrace_disabled))
2091 * If an lseek was done, then reset and start from beginning.
2093 if (*pos < iter->pos)
2094 reset_iter_read(iter);
2097 * For set_ftrace_filter reading, if we have the filter
2098 * off, we can short cut and just print out that all
2099 * functions are enabled.
2101 if (iter->flags & FTRACE_ITER_FILTER && !ops->filter_hash->count) {
2103 return t_hash_start(m, pos);
2104 iter->flags |= FTRACE_ITER_PRINTALL;
2105 /* reset in case of seek/pread */
2106 iter->flags &= ~FTRACE_ITER_HASH;
2110 if (iter->flags & FTRACE_ITER_HASH)
2111 return t_hash_start(m, pos);
2114 * Unfortunately, we need to restart at ftrace_pages_start
2115 * every time we let go of the ftrace_mutex. This is because
2116 * those pointers can change without the lock.
2118 iter->pg = ftrace_pages_start;
2120 for (l = 0; l <= *pos; ) {
2121 p = t_next(m, p, &l);
2127 if (iter->flags & FTRACE_ITER_FILTER)
2128 return t_hash_start(m, pos);
2136 static void t_stop(struct seq_file *m, void *p)
2138 mutex_unlock(&ftrace_lock);
2141 static int t_show(struct seq_file *m, void *v)
2143 struct ftrace_iterator *iter = m->private;
2144 struct dyn_ftrace *rec;
2146 if (iter->flags & FTRACE_ITER_HASH)
2147 return t_hash_show(m, iter);
2149 if (iter->flags & FTRACE_ITER_PRINTALL) {
2150 seq_printf(m, "#### all functions enabled ####\n");
2159 seq_printf(m, "%ps", (void *)rec->ip);
2160 if (iter->flags & FTRACE_ITER_ENABLED)
2161 seq_printf(m, " (%ld)",
2162 rec->flags & ~FTRACE_FL_MASK);
2163 seq_printf(m, "\n");
2168 static const struct seq_operations show_ftrace_seq_ops = {
2176 ftrace_avail_open(struct inode *inode, struct file *file)
2178 struct ftrace_iterator *iter;
2181 if (unlikely(ftrace_disabled))
2184 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
2188 iter->pg = ftrace_pages_start;
2190 ret = seq_open(file, &show_ftrace_seq_ops);
2192 struct seq_file *m = file->private_data;
2203 ftrace_enabled_open(struct inode *inode, struct file *file)
2205 struct ftrace_iterator *iter;
2208 if (unlikely(ftrace_disabled))
2211 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
2215 iter->pg = ftrace_pages_start;
2216 iter->flags = FTRACE_ITER_ENABLED;
2218 ret = seq_open(file, &show_ftrace_seq_ops);
2220 struct seq_file *m = file->private_data;
2230 static void ftrace_filter_reset(struct ftrace_hash *hash)
2232 mutex_lock(&ftrace_lock);
2233 ftrace_hash_clear(hash);
2234 mutex_unlock(&ftrace_lock);
2238 ftrace_regex_open(struct ftrace_ops *ops, int flag,
2239 struct inode *inode, struct file *file)
2241 struct ftrace_iterator *iter;
2242 struct ftrace_hash *hash;
2245 if (unlikely(ftrace_disabled))
2248 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
2252 if (trace_parser_get_init(&iter->parser, FTRACE_BUFF_MAX)) {
2257 if (flag & FTRACE_ITER_NOTRACE)
2258 hash = ops->notrace_hash;
2260 hash = ops->filter_hash;
2265 if (file->f_mode & FMODE_WRITE) {
2266 mutex_lock(&ftrace_lock);
2267 iter->hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, hash);
2268 mutex_unlock(&ftrace_lock);
2271 trace_parser_put(&iter->parser);
2277 mutex_lock(&ftrace_regex_lock);
2279 if ((file->f_mode & FMODE_WRITE) &&
2280 (file->f_flags & O_TRUNC))
2281 ftrace_filter_reset(iter->hash);
2283 if (file->f_mode & FMODE_READ) {
2284 iter->pg = ftrace_pages_start;
2286 ret = seq_open(file, &show_ftrace_seq_ops);
2288 struct seq_file *m = file->private_data;
2292 free_ftrace_hash(iter->hash);
2293 trace_parser_put(&iter->parser);
2297 file->private_data = iter;
2298 mutex_unlock(&ftrace_regex_lock);
2304 ftrace_filter_open(struct inode *inode, struct file *file)
2306 return ftrace_regex_open(&global_ops, FTRACE_ITER_FILTER,
2311 ftrace_notrace_open(struct inode *inode, struct file *file)
2313 return ftrace_regex_open(&global_ops, FTRACE_ITER_NOTRACE,
2318 ftrace_regex_lseek(struct file *file, loff_t offset, int origin)
2322 if (file->f_mode & FMODE_READ)
2323 ret = seq_lseek(file, offset, origin);
2325 file->f_pos = ret = 1;
2330 static int ftrace_match(char *str, char *regex, int len, int type)
2337 if (strcmp(str, regex) == 0)
2340 case MATCH_FRONT_ONLY:
2341 if (strncmp(str, regex, len) == 0)
2344 case MATCH_MIDDLE_ONLY:
2345 if (strstr(str, regex))
2348 case MATCH_END_ONLY:
2350 if (slen >= len && memcmp(str + slen - len, regex, len) == 0)
2359 enter_record(struct ftrace_hash *hash, struct dyn_ftrace *rec, int not)
2361 struct ftrace_func_entry *entry;
2364 entry = ftrace_lookup_ip(hash, rec->ip);
2366 /* Do nothing if it doesn't exist */
2370 free_hash_entry(hash, entry);
2372 /* Do nothing if it exists */
2376 ret = add_hash_entry(hash, rec->ip);
2382 ftrace_match_record(struct dyn_ftrace *rec, char *mod,
2383 char *regex, int len, int type)
2385 char str[KSYM_SYMBOL_LEN];
2388 kallsyms_lookup(rec->ip, NULL, NULL, &modname, str);
2391 /* module lookup requires matching the module */
2392 if (!modname || strcmp(modname, mod))
2395 /* blank search means to match all funcs in the mod */
2400 return ftrace_match(str, regex, len, type);
2404 match_records(struct ftrace_hash *hash, char *buff,
2405 int len, char *mod, int not)
2407 unsigned search_len = 0;
2408 struct ftrace_page *pg;
2409 struct dyn_ftrace *rec;
2410 int type = MATCH_FULL;
2411 char *search = buff;
2416 type = filter_parse_regex(buff, len, &search, ¬);
2417 search_len = strlen(search);
2420 mutex_lock(&ftrace_lock);
2422 if (unlikely(ftrace_disabled))
2425 do_for_each_ftrace_rec(pg, rec) {
2427 if (ftrace_match_record(rec, mod, search, search_len, type)) {
2428 ret = enter_record(hash, rec, not);
2435 } while_for_each_ftrace_rec();
2437 mutex_unlock(&ftrace_lock);
2443 ftrace_match_records(struct ftrace_hash *hash, char *buff, int len)
2445 return match_records(hash, buff, len, NULL, 0);
2449 ftrace_match_module_records(struct ftrace_hash *hash, char *buff, char *mod)
2453 /* blank or '*' mean the same */
2454 if (strcmp(buff, "*") == 0)
2457 /* handle the case of 'dont filter this module' */
2458 if (strcmp(buff, "!") == 0 || strcmp(buff, "!*") == 0) {
2463 return match_records(hash, buff, strlen(buff), mod, not);
2467 * We register the module command as a template to show others how
2468 * to register the a command as well.
2472 ftrace_mod_callback(struct ftrace_hash *hash,
2473 char *func, char *cmd, char *param, int enable)
2479 * cmd == 'mod' because we only registered this func
2480 * for the 'mod' ftrace_func_command.
2481 * But if you register one func with multiple commands,
2482 * you can tell which command was used by the cmd
2486 /* we must have a module name */
2490 mod = strsep(¶m, ":");
2494 ret = ftrace_match_module_records(hash, func, mod);
2503 static struct ftrace_func_command ftrace_mod_cmd = {
2505 .func = ftrace_mod_callback,
2508 static int __init ftrace_mod_cmd_init(void)
2510 return register_ftrace_command(&ftrace_mod_cmd);
2512 device_initcall(ftrace_mod_cmd_init);
2515 function_trace_probe_call(unsigned long ip, unsigned long parent_ip)
2517 struct ftrace_func_probe *entry;
2518 struct hlist_head *hhd;
2519 struct hlist_node *n;
2522 key = hash_long(ip, FTRACE_HASH_BITS);
2524 hhd = &ftrace_func_hash[key];
2526 if (hlist_empty(hhd))
2530 * Disable preemption for these calls to prevent a RCU grace
2531 * period. This syncs the hash iteration and freeing of items
2532 * on the hash. rcu_read_lock is too dangerous here.
2534 preempt_disable_notrace();
2535 hlist_for_each_entry_rcu(entry, n, hhd, node) {
2536 if (entry->ip == ip)
2537 entry->ops->func(ip, parent_ip, &entry->data);
2539 preempt_enable_notrace();
2542 static struct ftrace_ops trace_probe_ops __read_mostly =
2544 .func = function_trace_probe_call,
2547 static int ftrace_probe_registered;
2549 static void __enable_ftrace_function_probe(void)
2554 if (ftrace_probe_registered)
2557 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
2558 struct hlist_head *hhd = &ftrace_func_hash[i];
2562 /* Nothing registered? */
2563 if (i == FTRACE_FUNC_HASHSIZE)
2566 ret = __register_ftrace_function(&trace_probe_ops);
2568 ret = ftrace_startup(&trace_probe_ops, 0);
2570 ftrace_probe_registered = 1;
2573 static void __disable_ftrace_function_probe(void)
2578 if (!ftrace_probe_registered)
2581 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
2582 struct hlist_head *hhd = &ftrace_func_hash[i];
2587 /* no more funcs left */
2588 ret = __unregister_ftrace_function(&trace_probe_ops);
2590 ftrace_shutdown(&trace_probe_ops, 0);
2592 ftrace_probe_registered = 0;
2596 static void ftrace_free_entry_rcu(struct rcu_head *rhp)
2598 struct ftrace_func_probe *entry =
2599 container_of(rhp, struct ftrace_func_probe, rcu);
2601 if (entry->ops->free)
2602 entry->ops->free(&entry->data);
2608 register_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
2611 struct ftrace_func_probe *entry;
2612 struct ftrace_page *pg;
2613 struct dyn_ftrace *rec;
2619 type = filter_parse_regex(glob, strlen(glob), &search, ¬);
2620 len = strlen(search);
2622 /* we do not support '!' for function probes */
2626 mutex_lock(&ftrace_lock);
2628 if (unlikely(ftrace_disabled))
2631 do_for_each_ftrace_rec(pg, rec) {
2633 if (!ftrace_match_record(rec, NULL, search, len, type))
2636 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
2638 /* If we did not process any, then return error */
2649 * The caller might want to do something special
2650 * for each function we find. We call the callback
2651 * to give the caller an opportunity to do so.
2653 if (ops->callback) {
2654 if (ops->callback(rec->ip, &entry->data) < 0) {
2655 /* caller does not like this func */
2662 entry->ip = rec->ip;
2664 key = hash_long(entry->ip, FTRACE_HASH_BITS);
2665 hlist_add_head_rcu(&entry->node, &ftrace_func_hash[key]);
2667 } while_for_each_ftrace_rec();
2668 __enable_ftrace_function_probe();
2671 mutex_unlock(&ftrace_lock);
2677 PROBE_TEST_FUNC = 1,
2682 __unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
2683 void *data, int flags)
2685 struct ftrace_func_probe *entry;
2686 struct hlist_node *n, *tmp;
2687 char str[KSYM_SYMBOL_LEN];
2688 int type = MATCH_FULL;
2692 if (glob && (strcmp(glob, "*") == 0 || !strlen(glob)))
2697 type = filter_parse_regex(glob, strlen(glob), &search, ¬);
2698 len = strlen(search);
2700 /* we do not support '!' for function probes */
2705 mutex_lock(&ftrace_lock);
2706 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
2707 struct hlist_head *hhd = &ftrace_func_hash[i];
2709 hlist_for_each_entry_safe(entry, n, tmp, hhd, node) {
2711 /* break up if statements for readability */
2712 if ((flags & PROBE_TEST_FUNC) && entry->ops != ops)
2715 if ((flags & PROBE_TEST_DATA) && entry->data != data)
2718 /* do this last, since it is the most expensive */
2720 kallsyms_lookup(entry->ip, NULL, NULL,
2722 if (!ftrace_match(str, glob, len, type))
2726 hlist_del(&entry->node);
2727 call_rcu(&entry->rcu, ftrace_free_entry_rcu);
2730 __disable_ftrace_function_probe();
2731 mutex_unlock(&ftrace_lock);
2735 unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
2738 __unregister_ftrace_function_probe(glob, ops, data,
2739 PROBE_TEST_FUNC | PROBE_TEST_DATA);
2743 unregister_ftrace_function_probe_func(char *glob, struct ftrace_probe_ops *ops)
2745 __unregister_ftrace_function_probe(glob, ops, NULL, PROBE_TEST_FUNC);
2748 void unregister_ftrace_function_probe_all(char *glob)
2750 __unregister_ftrace_function_probe(glob, NULL, NULL, 0);
2753 static LIST_HEAD(ftrace_commands);
2754 static DEFINE_MUTEX(ftrace_cmd_mutex);
2756 int register_ftrace_command(struct ftrace_func_command *cmd)
2758 struct ftrace_func_command *p;
2761 mutex_lock(&ftrace_cmd_mutex);
2762 list_for_each_entry(p, &ftrace_commands, list) {
2763 if (strcmp(cmd->name, p->name) == 0) {
2768 list_add(&cmd->list, &ftrace_commands);
2770 mutex_unlock(&ftrace_cmd_mutex);
2775 int unregister_ftrace_command(struct ftrace_func_command *cmd)
2777 struct ftrace_func_command *p, *n;
2780 mutex_lock(&ftrace_cmd_mutex);
2781 list_for_each_entry_safe(p, n, &ftrace_commands, list) {
2782 if (strcmp(cmd->name, p->name) == 0) {
2784 list_del_init(&p->list);
2789 mutex_unlock(&ftrace_cmd_mutex);
2794 static int ftrace_process_regex(struct ftrace_hash *hash,
2795 char *buff, int len, int enable)
2797 char *func, *command, *next = buff;
2798 struct ftrace_func_command *p;
2801 func = strsep(&next, ":");
2804 ret = ftrace_match_records(hash, func, len);
2814 command = strsep(&next, ":");
2816 mutex_lock(&ftrace_cmd_mutex);
2817 list_for_each_entry(p, &ftrace_commands, list) {
2818 if (strcmp(p->name, command) == 0) {
2819 ret = p->func(hash, func, command, next, enable);
2824 mutex_unlock(&ftrace_cmd_mutex);
2830 ftrace_regex_write(struct file *file, const char __user *ubuf,
2831 size_t cnt, loff_t *ppos, int enable)
2833 struct ftrace_iterator *iter;
2834 struct trace_parser *parser;
2840 mutex_lock(&ftrace_regex_lock);
2843 if (unlikely(ftrace_disabled))
2846 if (file->f_mode & FMODE_READ) {
2847 struct seq_file *m = file->private_data;
2850 iter = file->private_data;
2852 parser = &iter->parser;
2853 read = trace_get_user(parser, ubuf, cnt, ppos);
2855 if (read >= 0 && trace_parser_loaded(parser) &&
2856 !trace_parser_cont(parser)) {
2857 ret = ftrace_process_regex(iter->hash, parser->buffer,
2858 parser->idx, enable);
2859 trace_parser_clear(parser);
2866 mutex_unlock(&ftrace_regex_lock);
2872 ftrace_filter_write(struct file *file, const char __user *ubuf,
2873 size_t cnt, loff_t *ppos)
2875 return ftrace_regex_write(file, ubuf, cnt, ppos, 1);
2879 ftrace_notrace_write(struct file *file, const char __user *ubuf,
2880 size_t cnt, loff_t *ppos)
2882 return ftrace_regex_write(file, ubuf, cnt, ppos, 0);
2886 ftrace_set_regex(struct ftrace_ops *ops, unsigned char *buf, int len,
2887 int reset, int enable)
2889 struct ftrace_hash **orig_hash;
2890 struct ftrace_hash *hash;
2893 /* All global ops uses the global ops filters */
2894 if (ops->flags & FTRACE_OPS_FL_GLOBAL)
2897 if (unlikely(ftrace_disabled))
2901 orig_hash = &ops->filter_hash;
2903 orig_hash = &ops->notrace_hash;
2905 hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, *orig_hash);
2909 mutex_lock(&ftrace_regex_lock);
2911 ftrace_filter_reset(hash);
2913 ftrace_match_records(hash, buf, len);
2915 mutex_lock(&ftrace_lock);
2916 ret = ftrace_hash_move(ops, enable, orig_hash, hash);
2917 if (!ret && ops->flags & FTRACE_OPS_FL_ENABLED
2919 ftrace_run_update_code(FTRACE_ENABLE_CALLS);
2921 mutex_unlock(&ftrace_lock);
2923 mutex_unlock(&ftrace_regex_lock);
2925 free_ftrace_hash(hash);
2930 * ftrace_set_filter - set a function to filter on in ftrace
2931 * @ops - the ops to set the filter with
2932 * @buf - the string that holds the function filter text.
2933 * @len - the length of the string.
2934 * @reset - non zero to reset all filters before applying this filter.
2936 * Filters denote which functions should be enabled when tracing is enabled.
2937 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
2939 void ftrace_set_filter(struct ftrace_ops *ops, unsigned char *buf,
2942 ftrace_set_regex(ops, buf, len, reset, 1);
2944 EXPORT_SYMBOL_GPL(ftrace_set_filter);
2947 * ftrace_set_notrace - set a function to not trace in ftrace
2948 * @ops - the ops to set the notrace filter with
2949 * @buf - the string that holds the function notrace text.
2950 * @len - the length of the string.
2951 * @reset - non zero to reset all filters before applying this filter.
2953 * Notrace Filters denote which functions should not be enabled when tracing
2954 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
2957 void ftrace_set_notrace(struct ftrace_ops *ops, unsigned char *buf,
2960 ftrace_set_regex(ops, buf, len, reset, 0);
2962 EXPORT_SYMBOL_GPL(ftrace_set_notrace);
2964 * ftrace_set_filter - set a function to filter on in ftrace
2965 * @ops - the ops to set the filter with
2966 * @buf - the string that holds the function filter text.
2967 * @len - the length of the string.
2968 * @reset - non zero to reset all filters before applying this filter.
2970 * Filters denote which functions should be enabled when tracing is enabled.
2971 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
2973 void ftrace_set_global_filter(unsigned char *buf, int len, int reset)
2975 ftrace_set_regex(&global_ops, buf, len, reset, 1);
2977 EXPORT_SYMBOL_GPL(ftrace_set_global_filter);
2980 * ftrace_set_notrace - set a function to not trace in ftrace
2981 * @ops - the ops to set the notrace filter with
2982 * @buf - the string that holds the function notrace text.
2983 * @len - the length of the string.
2984 * @reset - non zero to reset all filters before applying this filter.
2986 * Notrace Filters denote which functions should not be enabled when tracing
2987 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
2990 void ftrace_set_global_notrace(unsigned char *buf, int len, int reset)
2992 ftrace_set_regex(&global_ops, buf, len, reset, 0);
2994 EXPORT_SYMBOL_GPL(ftrace_set_global_notrace);
2997 * command line interface to allow users to set filters on boot up.
2999 #define FTRACE_FILTER_SIZE COMMAND_LINE_SIZE
3000 static char ftrace_notrace_buf[FTRACE_FILTER_SIZE] __initdata;
3001 static char ftrace_filter_buf[FTRACE_FILTER_SIZE] __initdata;
3003 static int __init set_ftrace_notrace(char *str)
3005 strncpy(ftrace_notrace_buf, str, FTRACE_FILTER_SIZE);
3008 __setup("ftrace_notrace=", set_ftrace_notrace);
3010 static int __init set_ftrace_filter(char *str)
3012 strncpy(ftrace_filter_buf, str, FTRACE_FILTER_SIZE);
3015 __setup("ftrace_filter=", set_ftrace_filter);
3017 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
3018 static char ftrace_graph_buf[FTRACE_FILTER_SIZE] __initdata;
3019 static int ftrace_set_func(unsigned long *array, int *idx, char *buffer);
3021 static int __init set_graph_function(char *str)
3023 strlcpy(ftrace_graph_buf, str, FTRACE_FILTER_SIZE);
3026 __setup("ftrace_graph_filter=", set_graph_function);
3028 static void __init set_ftrace_early_graph(char *buf)
3034 func = strsep(&buf, ",");
3035 /* we allow only one expression at a time */
3036 ret = ftrace_set_func(ftrace_graph_funcs, &ftrace_graph_count,
3039 printk(KERN_DEBUG "ftrace: function %s not "
3040 "traceable\n", func);
3043 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
3046 set_ftrace_early_filter(struct ftrace_ops *ops, char *buf, int enable)
3051 func = strsep(&buf, ",");
3052 ftrace_set_regex(ops, func, strlen(func), 0, enable);
3056 static void __init set_ftrace_early_filters(void)
3058 if (ftrace_filter_buf[0])
3059 set_ftrace_early_filter(&global_ops, ftrace_filter_buf, 1);
3060 if (ftrace_notrace_buf[0])
3061 set_ftrace_early_filter(&global_ops, ftrace_notrace_buf, 0);
3062 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
3063 if (ftrace_graph_buf[0])
3064 set_ftrace_early_graph(ftrace_graph_buf);
3065 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
3069 ftrace_regex_release(struct inode *inode, struct file *file)
3071 struct seq_file *m = (struct seq_file *)file->private_data;
3072 struct ftrace_iterator *iter;
3073 struct ftrace_hash **orig_hash;
3074 struct trace_parser *parser;
3078 mutex_lock(&ftrace_regex_lock);
3079 if (file->f_mode & FMODE_READ) {
3082 seq_release(inode, file);
3084 iter = file->private_data;
3086 parser = &iter->parser;
3087 if (trace_parser_loaded(parser)) {
3088 parser->buffer[parser->idx] = 0;
3089 ftrace_match_records(iter->hash, parser->buffer, parser->idx);
3092 trace_parser_put(parser);
3094 if (file->f_mode & FMODE_WRITE) {
3095 filter_hash = !!(iter->flags & FTRACE_ITER_FILTER);
3098 orig_hash = &iter->ops->filter_hash;
3100 orig_hash = &iter->ops->notrace_hash;
3102 mutex_lock(&ftrace_lock);
3103 ret = ftrace_hash_move(iter->ops, filter_hash,
3104 orig_hash, iter->hash);
3105 if (!ret && (iter->ops->flags & FTRACE_OPS_FL_ENABLED)
3107 ftrace_run_update_code(FTRACE_ENABLE_CALLS);
3109 mutex_unlock(&ftrace_lock);
3111 free_ftrace_hash(iter->hash);
3114 mutex_unlock(&ftrace_regex_lock);
3118 static const struct file_operations ftrace_avail_fops = {
3119 .open = ftrace_avail_open,
3121 .llseek = seq_lseek,
3122 .release = seq_release_private,
3125 static const struct file_operations ftrace_enabled_fops = {
3126 .open = ftrace_enabled_open,
3128 .llseek = seq_lseek,
3129 .release = seq_release_private,
3132 static const struct file_operations ftrace_filter_fops = {
3133 .open = ftrace_filter_open,
3135 .write = ftrace_filter_write,
3136 .llseek = ftrace_regex_lseek,
3137 .release = ftrace_regex_release,
3140 static const struct file_operations ftrace_notrace_fops = {
3141 .open = ftrace_notrace_open,
3143 .write = ftrace_notrace_write,
3144 .llseek = ftrace_regex_lseek,
3145 .release = ftrace_regex_release,
3148 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
3150 static DEFINE_MUTEX(graph_lock);
3152 int ftrace_graph_count;
3153 int ftrace_graph_filter_enabled;
3154 unsigned long ftrace_graph_funcs[FTRACE_GRAPH_MAX_FUNCS] __read_mostly;
3157 __g_next(struct seq_file *m, loff_t *pos)
3159 if (*pos >= ftrace_graph_count)
3161 return &ftrace_graph_funcs[*pos];
3165 g_next(struct seq_file *m, void *v, loff_t *pos)
3168 return __g_next(m, pos);
3171 static void *g_start(struct seq_file *m, loff_t *pos)
3173 mutex_lock(&graph_lock);
3175 /* Nothing, tell g_show to print all functions are enabled */
3176 if (!ftrace_graph_filter_enabled && !*pos)
3179 return __g_next(m, pos);
3182 static void g_stop(struct seq_file *m, void *p)
3184 mutex_unlock(&graph_lock);
3187 static int g_show(struct seq_file *m, void *v)
3189 unsigned long *ptr = v;
3194 if (ptr == (unsigned long *)1) {
3195 seq_printf(m, "#### all functions enabled ####\n");
3199 seq_printf(m, "%ps\n", (void *)*ptr);
3204 static const struct seq_operations ftrace_graph_seq_ops = {
3212 ftrace_graph_open(struct inode *inode, struct file *file)
3216 if (unlikely(ftrace_disabled))
3219 mutex_lock(&graph_lock);
3220 if ((file->f_mode & FMODE_WRITE) &&
3221 (file->f_flags & O_TRUNC)) {
3222 ftrace_graph_filter_enabled = 0;
3223 ftrace_graph_count = 0;
3224 memset(ftrace_graph_funcs, 0, sizeof(ftrace_graph_funcs));
3226 mutex_unlock(&graph_lock);
3228 if (file->f_mode & FMODE_READ)
3229 ret = seq_open(file, &ftrace_graph_seq_ops);
3235 ftrace_graph_release(struct inode *inode, struct file *file)
3237 if (file->f_mode & FMODE_READ)
3238 seq_release(inode, file);
3243 ftrace_set_func(unsigned long *array, int *idx, char *buffer)
3245 struct dyn_ftrace *rec;
3246 struct ftrace_page *pg;
3255 type = filter_parse_regex(buffer, strlen(buffer), &search, ¬);
3256 if (!not && *idx >= FTRACE_GRAPH_MAX_FUNCS)
3259 search_len = strlen(search);
3261 mutex_lock(&ftrace_lock);
3263 if (unlikely(ftrace_disabled)) {
3264 mutex_unlock(&ftrace_lock);
3268 do_for_each_ftrace_rec(pg, rec) {
3270 if (rec->flags & FTRACE_FL_FREE)
3273 if (ftrace_match_record(rec, NULL, search, search_len, type)) {
3274 /* if it is in the array */
3276 for (i = 0; i < *idx; i++) {
3277 if (array[i] == rec->ip) {
3286 array[(*idx)++] = rec->ip;
3287 if (*idx >= FTRACE_GRAPH_MAX_FUNCS)
3292 array[i] = array[--(*idx)];
3298 } while_for_each_ftrace_rec();
3300 mutex_unlock(&ftrace_lock);
3305 ftrace_graph_filter_enabled = 1;
3310 ftrace_graph_write(struct file *file, const char __user *ubuf,
3311 size_t cnt, loff_t *ppos)
3313 struct trace_parser parser;
3319 mutex_lock(&graph_lock);
3321 if (trace_parser_get_init(&parser, FTRACE_BUFF_MAX)) {
3326 read = trace_get_user(&parser, ubuf, cnt, ppos);
3328 if (read >= 0 && trace_parser_loaded((&parser))) {
3329 parser.buffer[parser.idx] = 0;
3331 /* we allow only one expression at a time */
3332 ret = ftrace_set_func(ftrace_graph_funcs, &ftrace_graph_count,
3341 trace_parser_put(&parser);
3343 mutex_unlock(&graph_lock);
3348 static const struct file_operations ftrace_graph_fops = {
3349 .open = ftrace_graph_open,
3351 .write = ftrace_graph_write,
3352 .release = ftrace_graph_release,
3353 .llseek = seq_lseek,
3355 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
3357 static __init int ftrace_init_dyn_debugfs(struct dentry *d_tracer)
3360 trace_create_file("available_filter_functions", 0444,
3361 d_tracer, NULL, &ftrace_avail_fops);
3363 trace_create_file("enabled_functions", 0444,
3364 d_tracer, NULL, &ftrace_enabled_fops);
3366 trace_create_file("set_ftrace_filter", 0644, d_tracer,
3367 NULL, &ftrace_filter_fops);
3369 trace_create_file("set_ftrace_notrace", 0644, d_tracer,
3370 NULL, &ftrace_notrace_fops);
3372 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
3373 trace_create_file("set_graph_function", 0444, d_tracer,
3375 &ftrace_graph_fops);
3376 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
3381 static int ftrace_process_locs(struct module *mod,
3382 unsigned long *start,
3387 unsigned long flags = 0; /* Shut up gcc */
3389 mutex_lock(&ftrace_lock);
3392 addr = ftrace_call_adjust(*p++);
3394 * Some architecture linkers will pad between
3395 * the different mcount_loc sections of different
3396 * object files to satisfy alignments.
3397 * Skip any NULL pointers.
3401 ftrace_record_ip(addr);
3405 * We only need to disable interrupts on start up
3406 * because we are modifying code that an interrupt
3407 * may execute, and the modification is not atomic.
3408 * But for modules, nothing runs the code we modify
3409 * until we are finished with it, and there's no
3410 * reason to cause large interrupt latencies while we do it.
3413 local_irq_save(flags);
3414 ftrace_update_code(mod);
3416 local_irq_restore(flags);
3417 mutex_unlock(&ftrace_lock);
3422 #ifdef CONFIG_MODULES
3423 void ftrace_release_mod(struct module *mod)
3425 struct dyn_ftrace *rec;
3426 struct ftrace_page *pg;
3428 mutex_lock(&ftrace_lock);
3430 if (ftrace_disabled)
3433 do_for_each_ftrace_rec(pg, rec) {
3434 if (within_module_core(rec->ip, mod)) {
3436 * rec->ip is changed in ftrace_free_rec()
3437 * It should not between s and e if record was freed.
3439 FTRACE_WARN_ON(rec->flags & FTRACE_FL_FREE);
3440 ftrace_free_rec(rec);
3442 } while_for_each_ftrace_rec();
3444 mutex_unlock(&ftrace_lock);
3447 static void ftrace_init_module(struct module *mod,
3448 unsigned long *start, unsigned long *end)
3450 if (ftrace_disabled || start == end)
3452 ftrace_process_locs(mod, start, end);
3455 static int ftrace_module_notify(struct notifier_block *self,
3456 unsigned long val, void *data)
3458 struct module *mod = data;
3461 case MODULE_STATE_COMING:
3462 ftrace_init_module(mod, mod->ftrace_callsites,
3463 mod->ftrace_callsites +
3464 mod->num_ftrace_callsites);
3466 case MODULE_STATE_GOING:
3467 ftrace_release_mod(mod);
3474 static int ftrace_module_notify(struct notifier_block *self,
3475 unsigned long val, void *data)
3479 #endif /* CONFIG_MODULES */
3481 struct notifier_block ftrace_module_nb = {
3482 .notifier_call = ftrace_module_notify,
3486 extern unsigned long __start_mcount_loc[];
3487 extern unsigned long __stop_mcount_loc[];
3489 void __init ftrace_init(void)
3491 unsigned long count, addr, flags;
3494 /* Keep the ftrace pointer to the stub */
3495 addr = (unsigned long)ftrace_stub;
3497 local_irq_save(flags);
3498 ftrace_dyn_arch_init(&addr);
3499 local_irq_restore(flags);
3501 /* ftrace_dyn_arch_init places the return code in addr */
3505 count = __stop_mcount_loc - __start_mcount_loc;
3507 ret = ftrace_dyn_table_alloc(count);
3511 last_ftrace_enabled = ftrace_enabled = 1;
3513 ret = ftrace_process_locs(NULL,
3517 ret = register_module_notifier(&ftrace_module_nb);
3519 pr_warning("Failed to register trace ftrace module notifier\n");
3521 set_ftrace_early_filters();
3525 ftrace_disabled = 1;
3530 static struct ftrace_ops global_ops = {
3531 .func = ftrace_stub,
3534 static int __init ftrace_nodyn_init(void)
3539 device_initcall(ftrace_nodyn_init);
3541 static inline int ftrace_init_dyn_debugfs(struct dentry *d_tracer) { return 0; }
3542 static inline void ftrace_startup_enable(int command) { }
3543 /* Keep as macros so we do not need to define the commands */
3544 # define ftrace_startup(ops, command) \
3546 (ops)->flags |= FTRACE_OPS_FL_ENABLED; \
3549 # define ftrace_shutdown(ops, command) do { } while (0)
3550 # define ftrace_startup_sysctl() do { } while (0)
3551 # define ftrace_shutdown_sysctl() do { } while (0)
3554 ftrace_ops_test(struct ftrace_ops *ops, unsigned long ip)
3559 #endif /* CONFIG_DYNAMIC_FTRACE */
3562 ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip)
3564 struct ftrace_ops *op;
3566 if (unlikely(trace_recursion_test(TRACE_INTERNAL_BIT)))
3569 trace_recursion_set(TRACE_INTERNAL_BIT);
3571 * Some of the ops may be dynamically allocated,
3572 * they must be freed after a synchronize_sched().
3574 preempt_disable_notrace();
3575 op = rcu_dereference_raw(ftrace_ops_list);
3576 while (op != &ftrace_list_end) {
3577 if (ftrace_ops_test(op, ip))
3578 op->func(ip, parent_ip);
3579 op = rcu_dereference_raw(op->next);
3581 preempt_enable_notrace();
3582 trace_recursion_clear(TRACE_INTERNAL_BIT);
3585 static void clear_ftrace_swapper(void)
3587 struct task_struct *p;
3591 for_each_online_cpu(cpu) {
3593 clear_tsk_trace_trace(p);
3598 static void set_ftrace_swapper(void)
3600 struct task_struct *p;
3604 for_each_online_cpu(cpu) {
3606 set_tsk_trace_trace(p);
3611 static void clear_ftrace_pid(struct pid *pid)
3613 struct task_struct *p;
3616 do_each_pid_task(pid, PIDTYPE_PID, p) {
3617 clear_tsk_trace_trace(p);
3618 } while_each_pid_task(pid, PIDTYPE_PID, p);
3624 static void set_ftrace_pid(struct pid *pid)
3626 struct task_struct *p;
3629 do_each_pid_task(pid, PIDTYPE_PID, p) {
3630 set_tsk_trace_trace(p);
3631 } while_each_pid_task(pid, PIDTYPE_PID, p);
3635 static void clear_ftrace_pid_task(struct pid *pid)
3637 if (pid == ftrace_swapper_pid)
3638 clear_ftrace_swapper();
3640 clear_ftrace_pid(pid);
3643 static void set_ftrace_pid_task(struct pid *pid)
3645 if (pid == ftrace_swapper_pid)
3646 set_ftrace_swapper();
3648 set_ftrace_pid(pid);
3651 static int ftrace_pid_add(int p)
3654 struct ftrace_pid *fpid;
3657 mutex_lock(&ftrace_lock);
3660 pid = ftrace_swapper_pid;
3662 pid = find_get_pid(p);
3669 list_for_each_entry(fpid, &ftrace_pids, list)
3670 if (fpid->pid == pid)
3675 fpid = kmalloc(sizeof(*fpid), GFP_KERNEL);
3679 list_add(&fpid->list, &ftrace_pids);
3682 set_ftrace_pid_task(pid);
3684 ftrace_update_pid_func();
3685 ftrace_startup_enable(0);
3687 mutex_unlock(&ftrace_lock);
3691 if (pid != ftrace_swapper_pid)
3695 mutex_unlock(&ftrace_lock);
3699 static void ftrace_pid_reset(void)
3701 struct ftrace_pid *fpid, *safe;
3703 mutex_lock(&ftrace_lock);
3704 list_for_each_entry_safe(fpid, safe, &ftrace_pids, list) {
3705 struct pid *pid = fpid->pid;
3707 clear_ftrace_pid_task(pid);
3709 list_del(&fpid->list);
3713 ftrace_update_pid_func();
3714 ftrace_startup_enable(0);
3716 mutex_unlock(&ftrace_lock);
3719 static void *fpid_start(struct seq_file *m, loff_t *pos)
3721 mutex_lock(&ftrace_lock);
3723 if (list_empty(&ftrace_pids) && (!*pos))
3726 return seq_list_start(&ftrace_pids, *pos);
3729 static void *fpid_next(struct seq_file *m, void *v, loff_t *pos)
3734 return seq_list_next(v, &ftrace_pids, pos);
3737 static void fpid_stop(struct seq_file *m, void *p)
3739 mutex_unlock(&ftrace_lock);
3742 static int fpid_show(struct seq_file *m, void *v)
3744 const struct ftrace_pid *fpid = list_entry(v, struct ftrace_pid, list);
3746 if (v == (void *)1) {
3747 seq_printf(m, "no pid\n");
3751 if (fpid->pid == ftrace_swapper_pid)
3752 seq_printf(m, "swapper tasks\n");
3754 seq_printf(m, "%u\n", pid_vnr(fpid->pid));
3759 static const struct seq_operations ftrace_pid_sops = {
3760 .start = fpid_start,
3767 ftrace_pid_open(struct inode *inode, struct file *file)
3771 if ((file->f_mode & FMODE_WRITE) &&
3772 (file->f_flags & O_TRUNC))
3775 if (file->f_mode & FMODE_READ)
3776 ret = seq_open(file, &ftrace_pid_sops);
3782 ftrace_pid_write(struct file *filp, const char __user *ubuf,
3783 size_t cnt, loff_t *ppos)
3789 if (cnt >= sizeof(buf))
3792 if (copy_from_user(&buf, ubuf, cnt))
3798 * Allow "echo > set_ftrace_pid" or "echo -n '' > set_ftrace_pid"
3799 * to clean the filter quietly.
3801 tmp = strstrip(buf);
3802 if (strlen(tmp) == 0)
3805 ret = strict_strtol(tmp, 10, &val);
3809 ret = ftrace_pid_add(val);
3811 return ret ? ret : cnt;
3815 ftrace_pid_release(struct inode *inode, struct file *file)
3817 if (file->f_mode & FMODE_READ)
3818 seq_release(inode, file);
3823 static const struct file_operations ftrace_pid_fops = {
3824 .open = ftrace_pid_open,
3825 .write = ftrace_pid_write,
3827 .llseek = seq_lseek,
3828 .release = ftrace_pid_release,
3831 static __init int ftrace_init_debugfs(void)
3833 struct dentry *d_tracer;
3835 d_tracer = tracing_init_dentry();
3839 ftrace_init_dyn_debugfs(d_tracer);
3841 trace_create_file("set_ftrace_pid", 0644, d_tracer,
3842 NULL, &ftrace_pid_fops);
3844 ftrace_profile_debugfs(d_tracer);
3848 fs_initcall(ftrace_init_debugfs);
3851 * ftrace_kill - kill ftrace
3853 * This function should be used by panic code. It stops ftrace
3854 * but in a not so nice way. If you need to simply kill ftrace
3855 * from a non-atomic section, use ftrace_kill.
3857 void ftrace_kill(void)
3859 ftrace_disabled = 1;
3861 clear_ftrace_function();
3865 * register_ftrace_function - register a function for profiling
3866 * @ops - ops structure that holds the function for profiling.
3868 * Register a function to be called by all functions in the
3871 * Note: @ops->func and all the functions it calls must be labeled
3872 * with "notrace", otherwise it will go into a
3875 int register_ftrace_function(struct ftrace_ops *ops)
3879 mutex_lock(&ftrace_lock);
3881 if (unlikely(ftrace_disabled))
3884 ret = __register_ftrace_function(ops);
3886 ret = ftrace_startup(ops, 0);
3890 mutex_unlock(&ftrace_lock);
3893 EXPORT_SYMBOL_GPL(register_ftrace_function);
3896 * unregister_ftrace_function - unregister a function for profiling.
3897 * @ops - ops structure that holds the function to unregister
3899 * Unregister a function that was added to be called by ftrace profiling.
3901 int unregister_ftrace_function(struct ftrace_ops *ops)
3905 mutex_lock(&ftrace_lock);
3906 ret = __unregister_ftrace_function(ops);
3908 ftrace_shutdown(ops, 0);
3909 mutex_unlock(&ftrace_lock);
3913 EXPORT_SYMBOL_GPL(unregister_ftrace_function);
3916 ftrace_enable_sysctl(struct ctl_table *table, int write,
3917 void __user *buffer, size_t *lenp,
3922 mutex_lock(&ftrace_lock);
3924 if (unlikely(ftrace_disabled))
3927 ret = proc_dointvec(table, write, buffer, lenp, ppos);
3929 if (ret || !write || (last_ftrace_enabled == !!ftrace_enabled))
3932 last_ftrace_enabled = !!ftrace_enabled;
3934 if (ftrace_enabled) {
3936 ftrace_startup_sysctl();
3938 /* we are starting ftrace again */
3939 if (ftrace_ops_list != &ftrace_list_end) {
3940 if (ftrace_ops_list->next == &ftrace_list_end)
3941 ftrace_trace_function = ftrace_ops_list->func;
3943 ftrace_trace_function = ftrace_ops_list_func;
3947 /* stopping ftrace calls (just send to ftrace_stub) */
3948 ftrace_trace_function = ftrace_stub;
3950 ftrace_shutdown_sysctl();
3954 mutex_unlock(&ftrace_lock);
3958 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
3960 static int ftrace_graph_active;
3961 static struct notifier_block ftrace_suspend_notifier;
3963 int ftrace_graph_entry_stub(struct ftrace_graph_ent *trace)
3968 /* The callbacks that hook a function */
3969 trace_func_graph_ret_t ftrace_graph_return =
3970 (trace_func_graph_ret_t)ftrace_stub;
3971 trace_func_graph_ent_t ftrace_graph_entry = ftrace_graph_entry_stub;
3973 /* Try to assign a return stack array on FTRACE_RETSTACK_ALLOC_SIZE tasks. */
3974 static int alloc_retstack_tasklist(struct ftrace_ret_stack **ret_stack_list)
3978 unsigned long flags;
3979 int start = 0, end = FTRACE_RETSTACK_ALLOC_SIZE;
3980 struct task_struct *g, *t;
3982 for (i = 0; i < FTRACE_RETSTACK_ALLOC_SIZE; i++) {
3983 ret_stack_list[i] = kmalloc(FTRACE_RETFUNC_DEPTH
3984 * sizeof(struct ftrace_ret_stack),
3986 if (!ret_stack_list[i]) {
3994 read_lock_irqsave(&tasklist_lock, flags);
3995 do_each_thread(g, t) {
4001 if (t->ret_stack == NULL) {
4002 atomic_set(&t->tracing_graph_pause, 0);
4003 atomic_set(&t->trace_overrun, 0);
4004 t->curr_ret_stack = -1;
4005 /* Make sure the tasks see the -1 first: */
4007 t->ret_stack = ret_stack_list[start++];
4009 } while_each_thread(g, t);
4012 read_unlock_irqrestore(&tasklist_lock, flags);
4014 for (i = start; i < end; i++)
4015 kfree(ret_stack_list[i]);
4020 ftrace_graph_probe_sched_switch(void *ignore,
4021 struct task_struct *prev, struct task_struct *next)
4023 unsigned long long timestamp;
4027 * Does the user want to count the time a function was asleep.
4028 * If so, do not update the time stamps.
4030 if (trace_flags & TRACE_ITER_SLEEP_TIME)
4033 timestamp = trace_clock_local();
4035 prev->ftrace_timestamp = timestamp;
4037 /* only process tasks that we timestamped */
4038 if (!next->ftrace_timestamp)
4042 * Update all the counters in next to make up for the
4043 * time next was sleeping.
4045 timestamp -= next->ftrace_timestamp;
4047 for (index = next->curr_ret_stack; index >= 0; index--)
4048 next->ret_stack[index].calltime += timestamp;
4051 /* Allocate a return stack for each task */
4052 static int start_graph_tracing(void)
4054 struct ftrace_ret_stack **ret_stack_list;
4057 ret_stack_list = kmalloc(FTRACE_RETSTACK_ALLOC_SIZE *
4058 sizeof(struct ftrace_ret_stack *),
4061 if (!ret_stack_list)
4064 /* The cpu_boot init_task->ret_stack will never be freed */
4065 for_each_online_cpu(cpu) {
4066 if (!idle_task(cpu)->ret_stack)
4067 ftrace_graph_init_idle_task(idle_task(cpu), cpu);
4071 ret = alloc_retstack_tasklist(ret_stack_list);
4072 } while (ret == -EAGAIN);
4075 ret = register_trace_sched_switch(ftrace_graph_probe_sched_switch, NULL);
4077 pr_info("ftrace_graph: Couldn't activate tracepoint"
4078 " probe to kernel_sched_switch\n");
4081 kfree(ret_stack_list);
4086 * Hibernation protection.
4087 * The state of the current task is too much unstable during
4088 * suspend/restore to disk. We want to protect against that.
4091 ftrace_suspend_notifier_call(struct notifier_block *bl, unsigned long state,
4095 case PM_HIBERNATION_PREPARE:
4096 pause_graph_tracing();
4099 case PM_POST_HIBERNATION:
4100 unpause_graph_tracing();
4106 int register_ftrace_graph(trace_func_graph_ret_t retfunc,
4107 trace_func_graph_ent_t entryfunc)
4111 mutex_lock(&ftrace_lock);
4113 /* we currently allow only one tracer registered at a time */
4114 if (ftrace_graph_active) {
4119 ftrace_suspend_notifier.notifier_call = ftrace_suspend_notifier_call;
4120 register_pm_notifier(&ftrace_suspend_notifier);
4122 ftrace_graph_active++;
4123 ret = start_graph_tracing();
4125 ftrace_graph_active--;
4129 ftrace_graph_return = retfunc;
4130 ftrace_graph_entry = entryfunc;
4132 ret = ftrace_startup(&global_ops, FTRACE_START_FUNC_RET);
4135 mutex_unlock(&ftrace_lock);
4139 void unregister_ftrace_graph(void)
4141 mutex_lock(&ftrace_lock);
4143 if (unlikely(!ftrace_graph_active))
4146 ftrace_graph_active--;
4147 ftrace_graph_return = (trace_func_graph_ret_t)ftrace_stub;
4148 ftrace_graph_entry = ftrace_graph_entry_stub;
4149 ftrace_shutdown(&global_ops, FTRACE_STOP_FUNC_RET);
4150 unregister_pm_notifier(&ftrace_suspend_notifier);
4151 unregister_trace_sched_switch(ftrace_graph_probe_sched_switch, NULL);
4154 mutex_unlock(&ftrace_lock);
4157 static DEFINE_PER_CPU(struct ftrace_ret_stack *, idle_ret_stack);
4160 graph_init_task(struct task_struct *t, struct ftrace_ret_stack *ret_stack)
4162 atomic_set(&t->tracing_graph_pause, 0);
4163 atomic_set(&t->trace_overrun, 0);
4164 t->ftrace_timestamp = 0;
4165 /* make curr_ret_stack visible before we add the ret_stack */
4167 t->ret_stack = ret_stack;
4171 * Allocate a return stack for the idle task. May be the first
4172 * time through, or it may be done by CPU hotplug online.
4174 void ftrace_graph_init_idle_task(struct task_struct *t, int cpu)
4176 t->curr_ret_stack = -1;
4178 * The idle task has no parent, it either has its own
4179 * stack or no stack at all.
4182 WARN_ON(t->ret_stack != per_cpu(idle_ret_stack, cpu));
4184 if (ftrace_graph_active) {
4185 struct ftrace_ret_stack *ret_stack;
4187 ret_stack = per_cpu(idle_ret_stack, cpu);
4189 ret_stack = kmalloc(FTRACE_RETFUNC_DEPTH
4190 * sizeof(struct ftrace_ret_stack),
4194 per_cpu(idle_ret_stack, cpu) = ret_stack;
4196 graph_init_task(t, ret_stack);
4200 /* Allocate a return stack for newly created task */
4201 void ftrace_graph_init_task(struct task_struct *t)
4203 /* Make sure we do not use the parent ret_stack */
4204 t->ret_stack = NULL;
4205 t->curr_ret_stack = -1;
4207 if (ftrace_graph_active) {
4208 struct ftrace_ret_stack *ret_stack;
4210 ret_stack = kmalloc(FTRACE_RETFUNC_DEPTH
4211 * sizeof(struct ftrace_ret_stack),
4215 graph_init_task(t, ret_stack);
4219 void ftrace_graph_exit_task(struct task_struct *t)
4221 struct ftrace_ret_stack *ret_stack = t->ret_stack;
4223 t->ret_stack = NULL;
4224 /* NULL must become visible to IRQs before we free it: */
4230 void ftrace_graph_stop(void)