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/ftrace.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 =
90 static struct ftrace_ops *ftrace_list __read_mostly = &ftrace_list_end;
91 ftrace_func_t ftrace_trace_function __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 * Traverse the ftrace_list, invoking all entries. The reason that we
98 * can use rcu_dereference_raw() is that elements removed from this list
99 * are simply leaked, so there is no need to interact with a grace-period
100 * mechanism. The rcu_dereference_raw() calls are needed to handle
101 * concurrent insertions into the ftrace_list.
103 * Silly Alpha and silly pointer-speculation compiler optimizations!
105 static void ftrace_list_func(unsigned long ip, unsigned long parent_ip)
107 struct ftrace_ops *op = rcu_dereference_raw(ftrace_list); /*see above*/
109 while (op != &ftrace_list_end) {
110 op->func(ip, parent_ip);
111 op = rcu_dereference_raw(op->next); /*see above*/
115 static void ftrace_pid_func(unsigned long ip, unsigned long parent_ip)
117 if (!test_tsk_trace_trace(current))
120 ftrace_pid_function(ip, parent_ip);
123 static void set_ftrace_pid_function(ftrace_func_t func)
125 /* do not set ftrace_pid_function to itself! */
126 if (func != ftrace_pid_func)
127 ftrace_pid_function = func;
131 * clear_ftrace_function - reset the ftrace function
133 * This NULLs the ftrace function and in essence stops
134 * tracing. There may be lag
136 void clear_ftrace_function(void)
138 ftrace_trace_function = ftrace_stub;
139 __ftrace_trace_function = ftrace_stub;
140 ftrace_pid_function = ftrace_stub;
143 #ifndef CONFIG_HAVE_FUNCTION_TRACE_MCOUNT_TEST
145 * For those archs that do not test ftrace_trace_stop in their
146 * mcount call site, we need to do it from C.
148 static void ftrace_test_stop_func(unsigned long ip, unsigned long parent_ip)
150 if (function_trace_stop)
153 __ftrace_trace_function(ip, parent_ip);
157 static void update_global_ops(void)
162 * If there's only one function registered, then call that
163 * function directly. Otherwise, we need to iterate over the
164 * registered callers.
166 if (ftrace_list == &ftrace_list_end ||
167 ftrace_list->next == &ftrace_list_end)
168 func = ftrace_list->func;
170 func = ftrace_list_func;
172 /* If we filter on pids, update to use the pid function */
173 if (!list_empty(&ftrace_pids)) {
174 set_ftrace_pid_function(func);
175 func = ftrace_pid_func;
178 global_ops.func = func;
181 static void update_ftrace_function(void)
187 func = global_ops.func;
189 #ifdef CONFIG_HAVE_FUNCTION_TRACE_MCOUNT_TEST
190 ftrace_trace_function = func;
192 __ftrace_trace_function = func;
193 ftrace_trace_function = ftrace_test_stop_func;
197 static void add_ftrace_ops(struct ftrace_ops **list, struct ftrace_ops *ops)
201 * We are entering ops into the ftrace_list but another
202 * CPU might be walking that list. We need to make sure
203 * the ops->next pointer is valid before another CPU sees
204 * the ops pointer included into the ftrace_list.
206 rcu_assign_pointer(*list, ops);
209 static int remove_ftrace_ops(struct ftrace_ops **list, struct ftrace_ops *ops)
211 struct ftrace_ops **p;
214 * If we are removing the last function, then simply point
215 * to the ftrace_stub.
217 if (*list == ops && ops->next == &ftrace_list_end) {
218 *list = &ftrace_list_end;
222 for (p = list; *p != &ftrace_list_end; p = &(*p)->next)
233 static int __register_ftrace_function(struct ftrace_ops *ops)
238 if (FTRACE_WARN_ON(ops == &global_ops))
241 add_ftrace_ops(&ftrace_list, ops);
243 update_ftrace_function();
248 static int __unregister_ftrace_function(struct ftrace_ops *ops)
255 if (FTRACE_WARN_ON(ops == &global_ops))
258 ret = remove_ftrace_ops(&ftrace_list, ops);
262 update_ftrace_function();
267 static void ftrace_update_pid_func(void)
269 /* Only do something if we are tracing something */
270 if (ftrace_trace_function == ftrace_stub)
273 update_ftrace_function();
276 #ifdef CONFIG_FUNCTION_PROFILER
277 struct ftrace_profile {
278 struct hlist_node node;
280 unsigned long counter;
281 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
282 unsigned long long time;
283 unsigned long long time_squared;
287 struct ftrace_profile_page {
288 struct ftrace_profile_page *next;
290 struct ftrace_profile records[];
293 struct ftrace_profile_stat {
295 struct hlist_head *hash;
296 struct ftrace_profile_page *pages;
297 struct ftrace_profile_page *start;
298 struct tracer_stat stat;
301 #define PROFILE_RECORDS_SIZE \
302 (PAGE_SIZE - offsetof(struct ftrace_profile_page, records))
304 #define PROFILES_PER_PAGE \
305 (PROFILE_RECORDS_SIZE / sizeof(struct ftrace_profile))
307 static int ftrace_profile_bits __read_mostly;
308 static int ftrace_profile_enabled __read_mostly;
310 /* ftrace_profile_lock - synchronize the enable and disable of the profiler */
311 static DEFINE_MUTEX(ftrace_profile_lock);
313 static DEFINE_PER_CPU(struct ftrace_profile_stat, ftrace_profile_stats);
315 #define FTRACE_PROFILE_HASH_SIZE 1024 /* must be power of 2 */
318 function_stat_next(void *v, int idx)
320 struct ftrace_profile *rec = v;
321 struct ftrace_profile_page *pg;
323 pg = (struct ftrace_profile_page *)((unsigned long)rec & PAGE_MASK);
329 if ((void *)rec >= (void *)&pg->records[pg->index]) {
333 rec = &pg->records[0];
341 static void *function_stat_start(struct tracer_stat *trace)
343 struct ftrace_profile_stat *stat =
344 container_of(trace, struct ftrace_profile_stat, stat);
346 if (!stat || !stat->start)
349 return function_stat_next(&stat->start->records[0], 0);
352 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
353 /* function graph compares on total time */
354 static int function_stat_cmp(void *p1, void *p2)
356 struct ftrace_profile *a = p1;
357 struct ftrace_profile *b = p2;
359 if (a->time < b->time)
361 if (a->time > b->time)
367 /* not function graph compares against hits */
368 static int function_stat_cmp(void *p1, void *p2)
370 struct ftrace_profile *a = p1;
371 struct ftrace_profile *b = p2;
373 if (a->counter < b->counter)
375 if (a->counter > b->counter)
382 static int function_stat_headers(struct seq_file *m)
384 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
385 seq_printf(m, " Function "
388 "--- ---- --- ---\n");
390 seq_printf(m, " Function Hit\n"
396 static int function_stat_show(struct seq_file *m, void *v)
398 struct ftrace_profile *rec = v;
399 char str[KSYM_SYMBOL_LEN];
401 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
402 static struct trace_seq s;
403 unsigned long long avg;
404 unsigned long long stddev;
406 mutex_lock(&ftrace_profile_lock);
408 /* we raced with function_profile_reset() */
409 if (unlikely(rec->counter == 0)) {
414 kallsyms_lookup(rec->ip, NULL, NULL, NULL, str);
415 seq_printf(m, " %-30.30s %10lu", str, rec->counter);
417 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
420 do_div(avg, rec->counter);
422 /* Sample standard deviation (s^2) */
423 if (rec->counter <= 1)
426 stddev = rec->time_squared - rec->counter * avg * avg;
428 * Divide only 1000 for ns^2 -> us^2 conversion.
429 * trace_print_graph_duration will divide 1000 again.
431 do_div(stddev, (rec->counter - 1) * 1000);
435 trace_print_graph_duration(rec->time, &s);
436 trace_seq_puts(&s, " ");
437 trace_print_graph_duration(avg, &s);
438 trace_seq_puts(&s, " ");
439 trace_print_graph_duration(stddev, &s);
440 trace_print_seq(m, &s);
444 mutex_unlock(&ftrace_profile_lock);
449 static void ftrace_profile_reset(struct ftrace_profile_stat *stat)
451 struct ftrace_profile_page *pg;
453 pg = stat->pages = stat->start;
456 memset(pg->records, 0, PROFILE_RECORDS_SIZE);
461 memset(stat->hash, 0,
462 FTRACE_PROFILE_HASH_SIZE * sizeof(struct hlist_head));
465 int ftrace_profile_pages_init(struct ftrace_profile_stat *stat)
467 struct ftrace_profile_page *pg;
472 /* If we already allocated, do nothing */
476 stat->pages = (void *)get_zeroed_page(GFP_KERNEL);
480 #ifdef CONFIG_DYNAMIC_FTRACE
481 functions = ftrace_update_tot_cnt;
484 * We do not know the number of functions that exist because
485 * dynamic tracing is what counts them. With past experience
486 * we have around 20K functions. That should be more than enough.
487 * It is highly unlikely we will execute every function in
493 pg = stat->start = stat->pages;
495 pages = DIV_ROUND_UP(functions, PROFILES_PER_PAGE);
497 for (i = 0; i < pages; i++) {
498 pg->next = (void *)get_zeroed_page(GFP_KERNEL);
509 unsigned long tmp = (unsigned long)pg;
515 free_page((unsigned long)stat->pages);
522 static int ftrace_profile_init_cpu(int cpu)
524 struct ftrace_profile_stat *stat;
527 stat = &per_cpu(ftrace_profile_stats, cpu);
530 /* If the profile is already created, simply reset it */
531 ftrace_profile_reset(stat);
536 * We are profiling all functions, but usually only a few thousand
537 * functions are hit. We'll make a hash of 1024 items.
539 size = FTRACE_PROFILE_HASH_SIZE;
541 stat->hash = kzalloc(sizeof(struct hlist_head) * size, GFP_KERNEL);
546 if (!ftrace_profile_bits) {
549 for (; size; size >>= 1)
550 ftrace_profile_bits++;
553 /* Preallocate the function profiling pages */
554 if (ftrace_profile_pages_init(stat) < 0) {
563 static int ftrace_profile_init(void)
568 for_each_online_cpu(cpu) {
569 ret = ftrace_profile_init_cpu(cpu);
577 /* interrupts must be disabled */
578 static struct ftrace_profile *
579 ftrace_find_profiled_func(struct ftrace_profile_stat *stat, unsigned long ip)
581 struct ftrace_profile *rec;
582 struct hlist_head *hhd;
583 struct hlist_node *n;
586 key = hash_long(ip, ftrace_profile_bits);
587 hhd = &stat->hash[key];
589 if (hlist_empty(hhd))
592 hlist_for_each_entry_rcu(rec, n, hhd, node) {
600 static void ftrace_add_profile(struct ftrace_profile_stat *stat,
601 struct ftrace_profile *rec)
605 key = hash_long(rec->ip, ftrace_profile_bits);
606 hlist_add_head_rcu(&rec->node, &stat->hash[key]);
610 * The memory is already allocated, this simply finds a new record to use.
612 static struct ftrace_profile *
613 ftrace_profile_alloc(struct ftrace_profile_stat *stat, unsigned long ip)
615 struct ftrace_profile *rec = NULL;
617 /* prevent recursion (from NMIs) */
618 if (atomic_inc_return(&stat->disabled) != 1)
622 * Try to find the function again since an NMI
623 * could have added it
625 rec = ftrace_find_profiled_func(stat, ip);
629 if (stat->pages->index == PROFILES_PER_PAGE) {
630 if (!stat->pages->next)
632 stat->pages = stat->pages->next;
635 rec = &stat->pages->records[stat->pages->index++];
637 ftrace_add_profile(stat, rec);
640 atomic_dec(&stat->disabled);
646 function_profile_call(unsigned long ip, unsigned long parent_ip)
648 struct ftrace_profile_stat *stat;
649 struct ftrace_profile *rec;
652 if (!ftrace_profile_enabled)
655 local_irq_save(flags);
657 stat = &__get_cpu_var(ftrace_profile_stats);
658 if (!stat->hash || !ftrace_profile_enabled)
661 rec = ftrace_find_profiled_func(stat, ip);
663 rec = ftrace_profile_alloc(stat, ip);
670 local_irq_restore(flags);
673 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
674 static int profile_graph_entry(struct ftrace_graph_ent *trace)
676 function_profile_call(trace->func, 0);
680 static void profile_graph_return(struct ftrace_graph_ret *trace)
682 struct ftrace_profile_stat *stat;
683 unsigned long long calltime;
684 struct ftrace_profile *rec;
687 local_irq_save(flags);
688 stat = &__get_cpu_var(ftrace_profile_stats);
689 if (!stat->hash || !ftrace_profile_enabled)
692 /* If the calltime was zero'd ignore it */
693 if (!trace->calltime)
696 calltime = trace->rettime - trace->calltime;
698 if (!(trace_flags & TRACE_ITER_GRAPH_TIME)) {
701 index = trace->depth;
703 /* Append this call time to the parent time to subtract */
705 current->ret_stack[index - 1].subtime += calltime;
707 if (current->ret_stack[index].subtime < calltime)
708 calltime -= current->ret_stack[index].subtime;
713 rec = ftrace_find_profiled_func(stat, trace->func);
715 rec->time += calltime;
716 rec->time_squared += calltime * calltime;
720 local_irq_restore(flags);
723 static int register_ftrace_profiler(void)
725 return register_ftrace_graph(&profile_graph_return,
726 &profile_graph_entry);
729 static void unregister_ftrace_profiler(void)
731 unregister_ftrace_graph();
734 static struct ftrace_ops ftrace_profile_ops __read_mostly =
736 .func = function_profile_call,
739 static int register_ftrace_profiler(void)
741 return register_ftrace_function(&ftrace_profile_ops);
744 static void unregister_ftrace_profiler(void)
746 unregister_ftrace_function(&ftrace_profile_ops);
748 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
751 ftrace_profile_write(struct file *filp, const char __user *ubuf,
752 size_t cnt, loff_t *ppos)
755 char buf[64]; /* big enough to hold a number */
758 if (cnt >= sizeof(buf))
761 if (copy_from_user(&buf, ubuf, cnt))
766 ret = strict_strtoul(buf, 10, &val);
772 mutex_lock(&ftrace_profile_lock);
773 if (ftrace_profile_enabled ^ val) {
775 ret = ftrace_profile_init();
781 ret = register_ftrace_profiler();
786 ftrace_profile_enabled = 1;
788 ftrace_profile_enabled = 0;
790 * unregister_ftrace_profiler calls stop_machine
791 * so this acts like an synchronize_sched.
793 unregister_ftrace_profiler();
797 mutex_unlock(&ftrace_profile_lock);
805 ftrace_profile_read(struct file *filp, char __user *ubuf,
806 size_t cnt, loff_t *ppos)
808 char buf[64]; /* big enough to hold a number */
811 r = sprintf(buf, "%u\n", ftrace_profile_enabled);
812 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
815 static const struct file_operations ftrace_profile_fops = {
816 .open = tracing_open_generic,
817 .read = ftrace_profile_read,
818 .write = ftrace_profile_write,
819 .llseek = default_llseek,
822 /* used to initialize the real stat files */
823 static struct tracer_stat function_stats __initdata = {
825 .stat_start = function_stat_start,
826 .stat_next = function_stat_next,
827 .stat_cmp = function_stat_cmp,
828 .stat_headers = function_stat_headers,
829 .stat_show = function_stat_show
832 static __init void ftrace_profile_debugfs(struct dentry *d_tracer)
834 struct ftrace_profile_stat *stat;
835 struct dentry *entry;
840 for_each_possible_cpu(cpu) {
841 stat = &per_cpu(ftrace_profile_stats, cpu);
843 /* allocate enough for function name + cpu number */
844 name = kmalloc(32, GFP_KERNEL);
847 * The files created are permanent, if something happens
848 * we still do not free memory.
851 "Could not allocate stat file for cpu %d\n",
855 stat->stat = function_stats;
856 snprintf(name, 32, "function%d", cpu);
857 stat->stat.name = name;
858 ret = register_stat_tracer(&stat->stat);
861 "Could not register function stat for cpu %d\n",
868 entry = debugfs_create_file("function_profile_enabled", 0644,
869 d_tracer, NULL, &ftrace_profile_fops);
871 pr_warning("Could not create debugfs "
872 "'function_profile_enabled' entry\n");
875 #else /* CONFIG_FUNCTION_PROFILER */
876 static __init void ftrace_profile_debugfs(struct dentry *d_tracer)
879 #endif /* CONFIG_FUNCTION_PROFILER */
881 static struct pid * const ftrace_swapper_pid = &init_struct_pid;
883 #ifdef CONFIG_DYNAMIC_FTRACE
885 #ifndef CONFIG_FTRACE_MCOUNT_RECORD
886 # error Dynamic ftrace depends on MCOUNT_RECORD
889 static struct hlist_head ftrace_func_hash[FTRACE_FUNC_HASHSIZE] __read_mostly;
891 struct ftrace_func_probe {
892 struct hlist_node node;
893 struct ftrace_probe_ops *ops;
901 FTRACE_ENABLE_CALLS = (1 << 0),
902 FTRACE_DISABLE_CALLS = (1 << 1),
903 FTRACE_UPDATE_TRACE_FUNC = (1 << 2),
904 FTRACE_START_FUNC_RET = (1 << 3),
905 FTRACE_STOP_FUNC_RET = (1 << 4),
907 struct ftrace_func_entry {
908 struct hlist_node hlist;
913 unsigned long size_bits;
914 struct hlist_head *buckets;
919 * We make these constant because no one should touch them,
920 * but they are used as the default "empty hash", to avoid allocating
921 * it all the time. These are in a read only section such that if
922 * anyone does try to modify it, it will cause an exception.
924 static const struct hlist_head empty_buckets[1];
925 static const struct ftrace_hash empty_hash = {
926 .buckets = (struct hlist_head *)empty_buckets,
928 #define EMPTY_HASH ((struct ftrace_hash *)&empty_hash)
931 FTRACE_OPS_FL_ENABLED = 1,
934 static struct ftrace_ops global_ops = {
936 .notrace_hash = EMPTY_HASH,
937 .filter_hash = EMPTY_HASH,
940 static struct dyn_ftrace *ftrace_new_addrs;
942 static DEFINE_MUTEX(ftrace_regex_lock);
945 struct ftrace_page *next;
947 struct dyn_ftrace records[];
950 #define ENTRIES_PER_PAGE \
951 ((PAGE_SIZE - sizeof(struct ftrace_page)) / sizeof(struct dyn_ftrace))
953 /* estimate from running different kernels */
954 #define NR_TO_INIT 10000
956 static struct ftrace_page *ftrace_pages_start;
957 static struct ftrace_page *ftrace_pages;
959 static struct dyn_ftrace *ftrace_free_records;
961 static struct ftrace_func_entry *
962 ftrace_lookup_ip(struct ftrace_hash *hash, unsigned long ip)
965 struct ftrace_func_entry *entry;
966 struct hlist_head *hhd;
967 struct hlist_node *n;
972 if (hash->size_bits > 0)
973 key = hash_long(ip, hash->size_bits);
977 hhd = &hash->buckets[key];
979 hlist_for_each_entry_rcu(entry, n, hhd, hlist) {
986 static void __add_hash_entry(struct ftrace_hash *hash,
987 struct ftrace_func_entry *entry)
989 struct hlist_head *hhd;
993 key = hash_long(entry->ip, hash->size_bits);
997 hhd = &hash->buckets[key];
998 hlist_add_head(&entry->hlist, hhd);
1002 static int add_hash_entry(struct ftrace_hash *hash, unsigned long ip)
1004 struct ftrace_func_entry *entry;
1006 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
1011 __add_hash_entry(hash, entry);
1017 free_hash_entry(struct ftrace_hash *hash,
1018 struct ftrace_func_entry *entry)
1020 hlist_del(&entry->hlist);
1026 remove_hash_entry(struct ftrace_hash *hash,
1027 struct ftrace_func_entry *entry)
1029 hlist_del(&entry->hlist);
1033 static void ftrace_hash_clear(struct ftrace_hash *hash)
1035 struct hlist_head *hhd;
1036 struct hlist_node *tp, *tn;
1037 struct ftrace_func_entry *entry;
1038 int size = 1 << hash->size_bits;
1044 for (i = 0; i < size; i++) {
1045 hhd = &hash->buckets[i];
1046 hlist_for_each_entry_safe(entry, tp, tn, hhd, hlist)
1047 free_hash_entry(hash, entry);
1049 FTRACE_WARN_ON(hash->count);
1052 static void free_ftrace_hash(struct ftrace_hash *hash)
1054 if (!hash || hash == EMPTY_HASH)
1056 ftrace_hash_clear(hash);
1057 kfree(hash->buckets);
1061 static struct ftrace_hash *alloc_ftrace_hash(int size_bits)
1063 struct ftrace_hash *hash;
1066 hash = kzalloc(sizeof(*hash), GFP_KERNEL);
1070 size = 1 << size_bits;
1071 hash->buckets = kzalloc(sizeof(*hash->buckets) * size, GFP_KERNEL);
1073 if (!hash->buckets) {
1078 hash->size_bits = size_bits;
1083 static struct ftrace_hash *
1084 alloc_and_copy_ftrace_hash(int size_bits, struct ftrace_hash *hash)
1086 struct ftrace_func_entry *entry;
1087 struct ftrace_hash *new_hash;
1088 struct hlist_node *tp;
1093 new_hash = alloc_ftrace_hash(size_bits);
1098 if (!hash || !hash->count)
1101 size = 1 << hash->size_bits;
1102 for (i = 0; i < size; i++) {
1103 hlist_for_each_entry(entry, tp, &hash->buckets[i], hlist) {
1104 ret = add_hash_entry(new_hash, entry->ip);
1110 FTRACE_WARN_ON(new_hash->count != hash->count);
1115 free_ftrace_hash(new_hash);
1120 ftrace_hash_move(struct ftrace_hash **dst, struct ftrace_hash *src)
1122 struct ftrace_func_entry *entry;
1123 struct hlist_node *tp, *tn;
1124 struct hlist_head *hhd;
1125 struct ftrace_hash *hash = *dst;
1127 int size = src->count;
1132 * If the new source is empty, just free dst and assign it
1136 free_ftrace_hash(*dst);
1141 ftrace_hash_clear(hash);
1144 * Make the hash size about 1/2 the # found
1146 for (size /= 2; size; size >>= 1)
1149 /* Don't allocate too much */
1150 if (bits > FTRACE_HASH_MAX_BITS)
1151 bits = FTRACE_HASH_MAX_BITS;
1153 /* We can't modify the empty_hash */
1154 if (hash == EMPTY_HASH) {
1155 /* Create a new hash */
1156 *dst = alloc_ftrace_hash(bits);
1165 /* Use the old hash, but create new buckets */
1166 hhd = kzalloc(sizeof(*hhd) * size, GFP_KERNEL);
1170 kfree(hash->buckets);
1171 hash->buckets = hhd;
1172 hash->size_bits = bits;
1175 size = 1 << src->size_bits;
1176 for (i = 0; i < size; i++) {
1177 hhd = &src->buckets[i];
1178 hlist_for_each_entry_safe(entry, tp, tn, hhd, hlist) {
1180 key = hash_long(entry->ip, bits);
1183 remove_hash_entry(src, entry);
1184 __add_hash_entry(hash, entry);
1192 * This is a double for. Do not use 'break' to break out of the loop,
1193 * you must use a goto.
1195 #define do_for_each_ftrace_rec(pg, rec) \
1196 for (pg = ftrace_pages_start; pg; pg = pg->next) { \
1198 for (_____i = 0; _____i < pg->index; _____i++) { \
1199 rec = &pg->records[_____i];
1201 #define while_for_each_ftrace_rec() \
1205 static void __ftrace_hash_rec_update(struct ftrace_ops *ops,
1209 struct ftrace_hash *hash;
1210 struct ftrace_hash *other_hash;
1211 struct ftrace_page *pg;
1212 struct dyn_ftrace *rec;
1216 /* Only update if the ops has been registered */
1217 if (!(ops->flags & FTRACE_OPS_FL_ENABLED))
1221 * In the filter_hash case:
1222 * If the count is zero, we update all records.
1223 * Otherwise we just update the items in the hash.
1225 * In the notrace_hash case:
1226 * We enable the update in the hash.
1227 * As disabling notrace means enabling the tracing,
1228 * and enabling notrace means disabling, the inc variable
1232 hash = ops->filter_hash;
1233 other_hash = ops->notrace_hash;
1238 hash = ops->notrace_hash;
1239 other_hash = ops->filter_hash;
1241 * If the notrace hash has no items,
1242 * then there's nothing to do.
1248 do_for_each_ftrace_rec(pg, rec) {
1249 int in_other_hash = 0;
1255 * Only the filter_hash affects all records.
1256 * Update if the record is not in the notrace hash.
1258 if (!ftrace_lookup_ip(other_hash, rec->ip))
1261 in_hash = !!ftrace_lookup_ip(hash, rec->ip);
1262 in_other_hash = !!ftrace_lookup_ip(other_hash, rec->ip);
1267 if (filter_hash && in_hash && !in_other_hash)
1269 else if (!filter_hash && in_hash &&
1270 (in_other_hash || !other_hash->count))
1278 if (FTRACE_WARN_ON((rec->flags & ~FTRACE_FL_MASK) == FTRACE_REF_MAX))
1281 if (FTRACE_WARN_ON((rec->flags & ~FTRACE_FL_MASK) == 0))
1286 /* Shortcut, if we handled all records, we are done. */
1287 if (!all && count == hash->count)
1289 } while_for_each_ftrace_rec();
1292 static void ftrace_hash_rec_disable(struct ftrace_ops *ops,
1295 __ftrace_hash_rec_update(ops, filter_hash, 0);
1298 static void ftrace_hash_rec_enable(struct ftrace_ops *ops,
1301 __ftrace_hash_rec_update(ops, filter_hash, 1);
1304 static void ftrace_free_rec(struct dyn_ftrace *rec)
1306 rec->freelist = ftrace_free_records;
1307 ftrace_free_records = rec;
1308 rec->flags |= FTRACE_FL_FREE;
1311 static struct dyn_ftrace *ftrace_alloc_dyn_node(unsigned long ip)
1313 struct dyn_ftrace *rec;
1315 /* First check for freed records */
1316 if (ftrace_free_records) {
1317 rec = ftrace_free_records;
1319 if (unlikely(!(rec->flags & FTRACE_FL_FREE))) {
1320 FTRACE_WARN_ON_ONCE(1);
1321 ftrace_free_records = NULL;
1325 ftrace_free_records = rec->freelist;
1326 memset(rec, 0, sizeof(*rec));
1330 if (ftrace_pages->index == ENTRIES_PER_PAGE) {
1331 if (!ftrace_pages->next) {
1332 /* allocate another page */
1333 ftrace_pages->next =
1334 (void *)get_zeroed_page(GFP_KERNEL);
1335 if (!ftrace_pages->next)
1338 ftrace_pages = ftrace_pages->next;
1341 return &ftrace_pages->records[ftrace_pages->index++];
1344 static struct dyn_ftrace *
1345 ftrace_record_ip(unsigned long ip)
1347 struct dyn_ftrace *rec;
1349 if (ftrace_disabled)
1352 rec = ftrace_alloc_dyn_node(ip);
1357 rec->newlist = ftrace_new_addrs;
1358 ftrace_new_addrs = rec;
1363 static void print_ip_ins(const char *fmt, unsigned char *p)
1367 printk(KERN_CONT "%s", fmt);
1369 for (i = 0; i < MCOUNT_INSN_SIZE; i++)
1370 printk(KERN_CONT "%s%02x", i ? ":" : "", p[i]);
1373 static void ftrace_bug(int failed, unsigned long ip)
1377 FTRACE_WARN_ON_ONCE(1);
1378 pr_info("ftrace faulted on modifying ");
1382 FTRACE_WARN_ON_ONCE(1);
1383 pr_info("ftrace failed to modify ");
1385 print_ip_ins(" actual: ", (unsigned char *)ip);
1386 printk(KERN_CONT "\n");
1389 FTRACE_WARN_ON_ONCE(1);
1390 pr_info("ftrace faulted on writing ");
1394 FTRACE_WARN_ON_ONCE(1);
1395 pr_info("ftrace faulted on unknown error ");
1401 /* Return 1 if the address range is reserved for ftrace */
1402 int ftrace_text_reserved(void *start, void *end)
1404 struct dyn_ftrace *rec;
1405 struct ftrace_page *pg;
1407 do_for_each_ftrace_rec(pg, rec) {
1408 if (rec->ip <= (unsigned long)end &&
1409 rec->ip + MCOUNT_INSN_SIZE > (unsigned long)start)
1411 } while_for_each_ftrace_rec();
1417 __ftrace_replace_code(struct dyn_ftrace *rec, int enable)
1419 unsigned long ftrace_addr;
1420 unsigned long flag = 0UL;
1422 ftrace_addr = (unsigned long)FTRACE_ADDR;
1425 * If we are enabling tracing:
1427 * If the record has a ref count, then we need to enable it
1428 * because someone is using it.
1430 * Otherwise we make sure its disabled.
1432 * If we are disabling tracing, then disable all records that
1435 if (enable && (rec->flags & ~FTRACE_FL_MASK))
1436 flag = FTRACE_FL_ENABLED;
1438 /* If the state of this record hasn't changed, then do nothing */
1439 if ((rec->flags & FTRACE_FL_ENABLED) == flag)
1443 rec->flags |= FTRACE_FL_ENABLED;
1444 return ftrace_make_call(rec, ftrace_addr);
1447 rec->flags &= ~FTRACE_FL_ENABLED;
1448 return ftrace_make_nop(NULL, rec, ftrace_addr);
1451 static void ftrace_replace_code(int enable)
1453 struct dyn_ftrace *rec;
1454 struct ftrace_page *pg;
1457 if (unlikely(ftrace_disabled))
1460 do_for_each_ftrace_rec(pg, rec) {
1461 /* Skip over free records */
1462 if (rec->flags & FTRACE_FL_FREE)
1465 failed = __ftrace_replace_code(rec, enable);
1467 ftrace_bug(failed, rec->ip);
1468 /* Stop processing */
1471 } while_for_each_ftrace_rec();
1475 ftrace_code_disable(struct module *mod, struct dyn_ftrace *rec)
1482 if (unlikely(ftrace_disabled))
1485 ret = ftrace_make_nop(mod, rec, MCOUNT_ADDR);
1487 ftrace_bug(ret, ip);
1494 * archs can override this function if they must do something
1495 * before the modifying code is performed.
1497 int __weak ftrace_arch_code_modify_prepare(void)
1503 * archs can override this function if they must do something
1504 * after the modifying code is performed.
1506 int __weak ftrace_arch_code_modify_post_process(void)
1511 static int __ftrace_modify_code(void *data)
1513 int *command = data;
1515 if (*command & FTRACE_ENABLE_CALLS)
1516 ftrace_replace_code(1);
1517 else if (*command & FTRACE_DISABLE_CALLS)
1518 ftrace_replace_code(0);
1520 if (*command & FTRACE_UPDATE_TRACE_FUNC)
1521 ftrace_update_ftrace_func(ftrace_trace_function);
1523 if (*command & FTRACE_START_FUNC_RET)
1524 ftrace_enable_ftrace_graph_caller();
1525 else if (*command & FTRACE_STOP_FUNC_RET)
1526 ftrace_disable_ftrace_graph_caller();
1531 static void ftrace_run_update_code(int command)
1535 ret = ftrace_arch_code_modify_prepare();
1536 FTRACE_WARN_ON(ret);
1540 stop_machine(__ftrace_modify_code, &command, NULL);
1542 ret = ftrace_arch_code_modify_post_process();
1543 FTRACE_WARN_ON(ret);
1546 static ftrace_func_t saved_ftrace_func;
1547 static int ftrace_start_up;
1549 static void ftrace_startup_enable(int command)
1551 if (saved_ftrace_func != ftrace_trace_function) {
1552 saved_ftrace_func = ftrace_trace_function;
1553 command |= FTRACE_UPDATE_TRACE_FUNC;
1556 if (!command || !ftrace_enabled)
1559 ftrace_run_update_code(command);
1562 static void ftrace_startup(struct ftrace_ops *ops, int command)
1564 if (unlikely(ftrace_disabled))
1568 command |= FTRACE_ENABLE_CALLS;
1570 ops->flags |= FTRACE_OPS_FL_ENABLED;
1571 if (ftrace_start_up == 1)
1572 ftrace_hash_rec_enable(ops, 1);
1574 ftrace_startup_enable(command);
1577 static void ftrace_shutdown(struct ftrace_ops *ops, int command)
1579 if (unlikely(ftrace_disabled))
1584 * Just warn in case of unbalance, no need to kill ftrace, it's not
1585 * critical but the ftrace_call callers may be never nopped again after
1586 * further ftrace uses.
1588 WARN_ON_ONCE(ftrace_start_up < 0);
1590 if (!ftrace_start_up)
1591 ftrace_hash_rec_disable(ops, 1);
1593 if (!ftrace_start_up) {
1594 command |= FTRACE_DISABLE_CALLS;
1595 ops->flags &= ~FTRACE_OPS_FL_ENABLED;
1598 if (saved_ftrace_func != ftrace_trace_function) {
1599 saved_ftrace_func = ftrace_trace_function;
1600 command |= FTRACE_UPDATE_TRACE_FUNC;
1603 if (!command || !ftrace_enabled)
1606 ftrace_run_update_code(command);
1609 static void ftrace_startup_sysctl(void)
1611 if (unlikely(ftrace_disabled))
1614 /* Force update next time */
1615 saved_ftrace_func = NULL;
1616 /* ftrace_start_up is true if we want ftrace running */
1617 if (ftrace_start_up)
1618 ftrace_run_update_code(FTRACE_ENABLE_CALLS);
1621 static void ftrace_shutdown_sysctl(void)
1623 if (unlikely(ftrace_disabled))
1626 /* ftrace_start_up is true if ftrace is running */
1627 if (ftrace_start_up)
1628 ftrace_run_update_code(FTRACE_DISABLE_CALLS);
1631 static cycle_t ftrace_update_time;
1632 static unsigned long ftrace_update_cnt;
1633 unsigned long ftrace_update_tot_cnt;
1635 static int ftrace_update_code(struct module *mod)
1637 struct dyn_ftrace *p;
1638 cycle_t start, stop;
1640 start = ftrace_now(raw_smp_processor_id());
1641 ftrace_update_cnt = 0;
1643 while (ftrace_new_addrs) {
1645 /* If something went wrong, bail without enabling anything */
1646 if (unlikely(ftrace_disabled))
1649 p = ftrace_new_addrs;
1650 ftrace_new_addrs = p->newlist;
1654 * Do the initial record conversion from mcount jump
1655 * to the NOP instructions.
1657 if (!ftrace_code_disable(mod, p)) {
1663 ftrace_update_cnt++;
1666 * If the tracing is enabled, go ahead and enable the record.
1668 * The reason not to enable the record immediatelly is the
1669 * inherent check of ftrace_make_nop/ftrace_make_call for
1670 * correct previous instructions. Making first the NOP
1671 * conversion puts the module to the correct state, thus
1672 * passing the ftrace_make_call check.
1674 if (ftrace_start_up) {
1675 int failed = __ftrace_replace_code(p, 1);
1677 ftrace_bug(failed, p->ip);
1683 stop = ftrace_now(raw_smp_processor_id());
1684 ftrace_update_time = stop - start;
1685 ftrace_update_tot_cnt += ftrace_update_cnt;
1690 static int __init ftrace_dyn_table_alloc(unsigned long num_to_init)
1692 struct ftrace_page *pg;
1696 /* allocate a few pages */
1697 ftrace_pages_start = (void *)get_zeroed_page(GFP_KERNEL);
1698 if (!ftrace_pages_start)
1702 * Allocate a few more pages.
1704 * TODO: have some parser search vmlinux before
1705 * final linking to find all calls to ftrace.
1707 * a) know how many pages to allocate.
1709 * b) set up the table then.
1711 * The dynamic code is still necessary for
1715 pg = ftrace_pages = ftrace_pages_start;
1717 cnt = num_to_init / ENTRIES_PER_PAGE;
1718 pr_info("ftrace: allocating %ld entries in %d pages\n",
1719 num_to_init, cnt + 1);
1721 for (i = 0; i < cnt; i++) {
1722 pg->next = (void *)get_zeroed_page(GFP_KERNEL);
1724 /* If we fail, we'll try later anyway */
1735 FTRACE_ITER_FILTER = (1 << 0),
1736 FTRACE_ITER_NOTRACE = (1 << 1),
1737 FTRACE_ITER_PRINTALL = (1 << 2),
1738 FTRACE_ITER_HASH = (1 << 3),
1739 FTRACE_ITER_ENABLED = (1 << 4),
1742 #define FTRACE_BUFF_MAX (KSYM_SYMBOL_LEN+4) /* room for wildcards */
1744 struct ftrace_iterator {
1747 struct ftrace_page *pg;
1748 struct dyn_ftrace *func;
1749 struct ftrace_func_probe *probe;
1750 struct trace_parser parser;
1751 struct ftrace_hash *hash;
1752 struct ftrace_ops *ops;
1759 t_hash_next(struct seq_file *m, loff_t *pos)
1761 struct ftrace_iterator *iter = m->private;
1762 struct hlist_node *hnd = NULL;
1763 struct hlist_head *hhd;
1769 hnd = &iter->probe->node;
1771 if (iter->hidx >= FTRACE_FUNC_HASHSIZE)
1774 hhd = &ftrace_func_hash[iter->hidx];
1776 if (hlist_empty(hhd)) {
1792 if (WARN_ON_ONCE(!hnd))
1795 iter->probe = hlist_entry(hnd, struct ftrace_func_probe, node);
1800 static void *t_hash_start(struct seq_file *m, loff_t *pos)
1802 struct ftrace_iterator *iter = m->private;
1806 if (iter->func_pos > *pos)
1810 for (l = 0; l <= (*pos - iter->func_pos); ) {
1811 p = t_hash_next(m, &l);
1818 /* Only set this if we have an item */
1819 iter->flags |= FTRACE_ITER_HASH;
1825 t_hash_show(struct seq_file *m, struct ftrace_iterator *iter)
1827 struct ftrace_func_probe *rec;
1830 if (WARN_ON_ONCE(!rec))
1833 if (rec->ops->print)
1834 return rec->ops->print(m, rec->ip, rec->ops, rec->data);
1836 seq_printf(m, "%ps:%ps", (void *)rec->ip, (void *)rec->ops->func);
1839 seq_printf(m, ":%p", rec->data);
1846 t_next(struct seq_file *m, void *v, loff_t *pos)
1848 struct ftrace_iterator *iter = m->private;
1849 struct ftrace_ops *ops = &global_ops;
1850 struct dyn_ftrace *rec = NULL;
1852 if (unlikely(ftrace_disabled))
1855 if (iter->flags & FTRACE_ITER_HASH)
1856 return t_hash_next(m, pos);
1859 iter->pos = iter->func_pos = *pos;
1861 if (iter->flags & FTRACE_ITER_PRINTALL)
1862 return t_hash_start(m, pos);
1865 if (iter->idx >= iter->pg->index) {
1866 if (iter->pg->next) {
1867 iter->pg = iter->pg->next;
1872 rec = &iter->pg->records[iter->idx++];
1873 if ((rec->flags & FTRACE_FL_FREE) ||
1875 ((iter->flags & FTRACE_ITER_FILTER) &&
1876 !(ftrace_lookup_ip(ops->filter_hash, rec->ip))) ||
1878 ((iter->flags & FTRACE_ITER_NOTRACE) &&
1879 !ftrace_lookup_ip(ops->notrace_hash, rec->ip)) ||
1881 ((iter->flags & FTRACE_ITER_ENABLED) &&
1882 !(rec->flags & ~FTRACE_FL_MASK))) {
1890 return t_hash_start(m, pos);
1897 static void reset_iter_read(struct ftrace_iterator *iter)
1901 iter->flags &= ~(FTRACE_ITER_PRINTALL & FTRACE_ITER_HASH);
1904 static void *t_start(struct seq_file *m, loff_t *pos)
1906 struct ftrace_iterator *iter = m->private;
1907 struct ftrace_ops *ops = &global_ops;
1911 mutex_lock(&ftrace_lock);
1913 if (unlikely(ftrace_disabled))
1917 * If an lseek was done, then reset and start from beginning.
1919 if (*pos < iter->pos)
1920 reset_iter_read(iter);
1923 * For set_ftrace_filter reading, if we have the filter
1924 * off, we can short cut and just print out that all
1925 * functions are enabled.
1927 if (iter->flags & FTRACE_ITER_FILTER && !ops->filter_hash->count) {
1929 return t_hash_start(m, pos);
1930 iter->flags |= FTRACE_ITER_PRINTALL;
1931 /* reset in case of seek/pread */
1932 iter->flags &= ~FTRACE_ITER_HASH;
1936 if (iter->flags & FTRACE_ITER_HASH)
1937 return t_hash_start(m, pos);
1940 * Unfortunately, we need to restart at ftrace_pages_start
1941 * every time we let go of the ftrace_mutex. This is because
1942 * those pointers can change without the lock.
1944 iter->pg = ftrace_pages_start;
1946 for (l = 0; l <= *pos; ) {
1947 p = t_next(m, p, &l);
1953 if (iter->flags & FTRACE_ITER_FILTER)
1954 return t_hash_start(m, pos);
1962 static void t_stop(struct seq_file *m, void *p)
1964 mutex_unlock(&ftrace_lock);
1967 static int t_show(struct seq_file *m, void *v)
1969 struct ftrace_iterator *iter = m->private;
1970 struct dyn_ftrace *rec;
1972 if (iter->flags & FTRACE_ITER_HASH)
1973 return t_hash_show(m, iter);
1975 if (iter->flags & FTRACE_ITER_PRINTALL) {
1976 seq_printf(m, "#### all functions enabled ####\n");
1985 seq_printf(m, "%ps", (void *)rec->ip);
1986 if (iter->flags & FTRACE_ITER_ENABLED)
1987 seq_printf(m, " (%ld)",
1988 rec->flags & ~FTRACE_FL_MASK);
1989 seq_printf(m, "\n");
1994 static const struct seq_operations show_ftrace_seq_ops = {
2002 ftrace_avail_open(struct inode *inode, struct file *file)
2004 struct ftrace_iterator *iter;
2007 if (unlikely(ftrace_disabled))
2010 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
2014 iter->pg = ftrace_pages_start;
2016 ret = seq_open(file, &show_ftrace_seq_ops);
2018 struct seq_file *m = file->private_data;
2029 ftrace_enabled_open(struct inode *inode, struct file *file)
2031 struct ftrace_iterator *iter;
2034 if (unlikely(ftrace_disabled))
2037 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
2041 iter->pg = ftrace_pages_start;
2042 iter->flags = FTRACE_ITER_ENABLED;
2044 ret = seq_open(file, &show_ftrace_seq_ops);
2046 struct seq_file *m = file->private_data;
2056 static void ftrace_filter_reset(struct ftrace_hash *hash)
2058 mutex_lock(&ftrace_lock);
2059 ftrace_hash_clear(hash);
2060 mutex_unlock(&ftrace_lock);
2064 ftrace_regex_open(struct ftrace_ops *ops, int flag,
2065 struct inode *inode, struct file *file)
2067 struct ftrace_iterator *iter;
2068 struct ftrace_hash *hash;
2071 if (unlikely(ftrace_disabled))
2074 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
2078 if (trace_parser_get_init(&iter->parser, FTRACE_BUFF_MAX)) {
2083 if (flag & FTRACE_ITER_NOTRACE)
2084 hash = ops->notrace_hash;
2086 hash = ops->filter_hash;
2091 if (file->f_mode & FMODE_WRITE) {
2092 mutex_lock(&ftrace_lock);
2093 iter->hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, hash);
2094 mutex_unlock(&ftrace_lock);
2097 trace_parser_put(&iter->parser);
2103 mutex_lock(&ftrace_regex_lock);
2105 if ((file->f_mode & FMODE_WRITE) &&
2106 (file->f_flags & O_TRUNC))
2107 ftrace_filter_reset(iter->hash);
2109 if (file->f_mode & FMODE_READ) {
2110 iter->pg = ftrace_pages_start;
2112 ret = seq_open(file, &show_ftrace_seq_ops);
2114 struct seq_file *m = file->private_data;
2118 free_ftrace_hash(iter->hash);
2119 trace_parser_put(&iter->parser);
2123 file->private_data = iter;
2124 mutex_unlock(&ftrace_regex_lock);
2130 ftrace_filter_open(struct inode *inode, struct file *file)
2132 return ftrace_regex_open(&global_ops, FTRACE_ITER_FILTER,
2137 ftrace_notrace_open(struct inode *inode, struct file *file)
2139 return ftrace_regex_open(&global_ops, FTRACE_ITER_NOTRACE,
2144 ftrace_regex_lseek(struct file *file, loff_t offset, int origin)
2148 if (file->f_mode & FMODE_READ)
2149 ret = seq_lseek(file, offset, origin);
2151 file->f_pos = ret = 1;
2156 static int ftrace_match(char *str, char *regex, int len, int type)
2163 if (strcmp(str, regex) == 0)
2166 case MATCH_FRONT_ONLY:
2167 if (strncmp(str, regex, len) == 0)
2170 case MATCH_MIDDLE_ONLY:
2171 if (strstr(str, regex))
2174 case MATCH_END_ONLY:
2176 if (slen >= len && memcmp(str + slen - len, regex, len) == 0)
2185 enter_record(struct ftrace_hash *hash, struct dyn_ftrace *rec, int not)
2187 struct ftrace_func_entry *entry;
2190 entry = ftrace_lookup_ip(hash, rec->ip);
2192 /* Do nothing if it doesn't exist */
2196 free_hash_entry(hash, entry);
2198 /* Do nothing if it exists */
2202 ret = add_hash_entry(hash, rec->ip);
2208 ftrace_match_record(struct dyn_ftrace *rec, char *mod,
2209 char *regex, int len, int type)
2211 char str[KSYM_SYMBOL_LEN];
2214 kallsyms_lookup(rec->ip, NULL, NULL, &modname, str);
2217 /* module lookup requires matching the module */
2218 if (!modname || strcmp(modname, mod))
2221 /* blank search means to match all funcs in the mod */
2226 return ftrace_match(str, regex, len, type);
2230 match_records(struct ftrace_hash *hash, char *buff,
2231 int len, char *mod, int not)
2233 unsigned search_len = 0;
2234 struct ftrace_page *pg;
2235 struct dyn_ftrace *rec;
2236 int type = MATCH_FULL;
2237 char *search = buff;
2242 type = filter_parse_regex(buff, len, &search, ¬);
2243 search_len = strlen(search);
2246 mutex_lock(&ftrace_lock);
2248 if (unlikely(ftrace_disabled))
2251 do_for_each_ftrace_rec(pg, rec) {
2253 if (ftrace_match_record(rec, mod, search, search_len, type)) {
2254 ret = enter_record(hash, rec, not);
2261 } while_for_each_ftrace_rec();
2263 mutex_unlock(&ftrace_lock);
2269 ftrace_match_records(struct ftrace_hash *hash, char *buff, int len)
2271 return match_records(hash, buff, len, NULL, 0);
2275 ftrace_match_module_records(struct ftrace_hash *hash, char *buff, char *mod)
2279 /* blank or '*' mean the same */
2280 if (strcmp(buff, "*") == 0)
2283 /* handle the case of 'dont filter this module' */
2284 if (strcmp(buff, "!") == 0 || strcmp(buff, "!*") == 0) {
2289 return match_records(hash, buff, strlen(buff), mod, not);
2293 * We register the module command as a template to show others how
2294 * to register the a command as well.
2298 ftrace_mod_callback(char *func, char *cmd, char *param, int enable)
2300 struct ftrace_ops *ops = &global_ops;
2301 struct ftrace_hash *hash;
2306 * cmd == 'mod' because we only registered this func
2307 * for the 'mod' ftrace_func_command.
2308 * But if you register one func with multiple commands,
2309 * you can tell which command was used by the cmd
2313 /* we must have a module name */
2317 mod = strsep(¶m, ":");
2322 hash = ops->filter_hash;
2324 hash = ops->notrace_hash;
2326 ret = ftrace_match_module_records(hash, func, mod);
2335 static struct ftrace_func_command ftrace_mod_cmd = {
2337 .func = ftrace_mod_callback,
2340 static int __init ftrace_mod_cmd_init(void)
2342 return register_ftrace_command(&ftrace_mod_cmd);
2344 device_initcall(ftrace_mod_cmd_init);
2347 function_trace_probe_call(unsigned long ip, unsigned long parent_ip)
2349 struct ftrace_func_probe *entry;
2350 struct hlist_head *hhd;
2351 struct hlist_node *n;
2354 key = hash_long(ip, FTRACE_HASH_BITS);
2356 hhd = &ftrace_func_hash[key];
2358 if (hlist_empty(hhd))
2362 * Disable preemption for these calls to prevent a RCU grace
2363 * period. This syncs the hash iteration and freeing of items
2364 * on the hash. rcu_read_lock is too dangerous here.
2366 preempt_disable_notrace();
2367 hlist_for_each_entry_rcu(entry, n, hhd, node) {
2368 if (entry->ip == ip)
2369 entry->ops->func(ip, parent_ip, &entry->data);
2371 preempt_enable_notrace();
2374 static struct ftrace_ops trace_probe_ops __read_mostly =
2376 .func = function_trace_probe_call,
2379 static int ftrace_probe_registered;
2381 static void __enable_ftrace_function_probe(void)
2385 if (ftrace_probe_registered)
2388 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
2389 struct hlist_head *hhd = &ftrace_func_hash[i];
2393 /* Nothing registered? */
2394 if (i == FTRACE_FUNC_HASHSIZE)
2397 __register_ftrace_function(&trace_probe_ops);
2398 ftrace_startup(&global_ops, 0);
2399 ftrace_probe_registered = 1;
2402 static void __disable_ftrace_function_probe(void)
2406 if (!ftrace_probe_registered)
2409 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
2410 struct hlist_head *hhd = &ftrace_func_hash[i];
2415 /* no more funcs left */
2416 __unregister_ftrace_function(&trace_probe_ops);
2417 ftrace_shutdown(&global_ops, 0);
2418 ftrace_probe_registered = 0;
2422 static void ftrace_free_entry_rcu(struct rcu_head *rhp)
2424 struct ftrace_func_probe *entry =
2425 container_of(rhp, struct ftrace_func_probe, rcu);
2427 if (entry->ops->free)
2428 entry->ops->free(&entry->data);
2434 register_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
2437 struct ftrace_func_probe *entry;
2438 struct ftrace_page *pg;
2439 struct dyn_ftrace *rec;
2445 type = filter_parse_regex(glob, strlen(glob), &search, ¬);
2446 len = strlen(search);
2448 /* we do not support '!' for function probes */
2452 mutex_lock(&ftrace_lock);
2454 if (unlikely(ftrace_disabled))
2457 do_for_each_ftrace_rec(pg, rec) {
2459 if (!ftrace_match_record(rec, NULL, search, len, type))
2462 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
2464 /* If we did not process any, then return error */
2475 * The caller might want to do something special
2476 * for each function we find. We call the callback
2477 * to give the caller an opportunity to do so.
2479 if (ops->callback) {
2480 if (ops->callback(rec->ip, &entry->data) < 0) {
2481 /* caller does not like this func */
2488 entry->ip = rec->ip;
2490 key = hash_long(entry->ip, FTRACE_HASH_BITS);
2491 hlist_add_head_rcu(&entry->node, &ftrace_func_hash[key]);
2493 } while_for_each_ftrace_rec();
2494 __enable_ftrace_function_probe();
2497 mutex_unlock(&ftrace_lock);
2503 PROBE_TEST_FUNC = 1,
2508 __unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
2509 void *data, int flags)
2511 struct ftrace_func_probe *entry;
2512 struct hlist_node *n, *tmp;
2513 char str[KSYM_SYMBOL_LEN];
2514 int type = MATCH_FULL;
2518 if (glob && (strcmp(glob, "*") == 0 || !strlen(glob)))
2523 type = filter_parse_regex(glob, strlen(glob), &search, ¬);
2524 len = strlen(search);
2526 /* we do not support '!' for function probes */
2531 mutex_lock(&ftrace_lock);
2532 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
2533 struct hlist_head *hhd = &ftrace_func_hash[i];
2535 hlist_for_each_entry_safe(entry, n, tmp, hhd, node) {
2537 /* break up if statements for readability */
2538 if ((flags & PROBE_TEST_FUNC) && entry->ops != ops)
2541 if ((flags & PROBE_TEST_DATA) && entry->data != data)
2544 /* do this last, since it is the most expensive */
2546 kallsyms_lookup(entry->ip, NULL, NULL,
2548 if (!ftrace_match(str, glob, len, type))
2552 hlist_del(&entry->node);
2553 call_rcu(&entry->rcu, ftrace_free_entry_rcu);
2556 __disable_ftrace_function_probe();
2557 mutex_unlock(&ftrace_lock);
2561 unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
2564 __unregister_ftrace_function_probe(glob, ops, data,
2565 PROBE_TEST_FUNC | PROBE_TEST_DATA);
2569 unregister_ftrace_function_probe_func(char *glob, struct ftrace_probe_ops *ops)
2571 __unregister_ftrace_function_probe(glob, ops, NULL, PROBE_TEST_FUNC);
2574 void unregister_ftrace_function_probe_all(char *glob)
2576 __unregister_ftrace_function_probe(glob, NULL, NULL, 0);
2579 static LIST_HEAD(ftrace_commands);
2580 static DEFINE_MUTEX(ftrace_cmd_mutex);
2582 int register_ftrace_command(struct ftrace_func_command *cmd)
2584 struct ftrace_func_command *p;
2587 mutex_lock(&ftrace_cmd_mutex);
2588 list_for_each_entry(p, &ftrace_commands, list) {
2589 if (strcmp(cmd->name, p->name) == 0) {
2594 list_add(&cmd->list, &ftrace_commands);
2596 mutex_unlock(&ftrace_cmd_mutex);
2601 int unregister_ftrace_command(struct ftrace_func_command *cmd)
2603 struct ftrace_func_command *p, *n;
2606 mutex_lock(&ftrace_cmd_mutex);
2607 list_for_each_entry_safe(p, n, &ftrace_commands, list) {
2608 if (strcmp(cmd->name, p->name) == 0) {
2610 list_del_init(&p->list);
2615 mutex_unlock(&ftrace_cmd_mutex);
2620 static int ftrace_process_regex(struct ftrace_hash *hash,
2621 char *buff, int len, int enable)
2623 char *func, *command, *next = buff;
2624 struct ftrace_func_command *p;
2627 func = strsep(&next, ":");
2630 ret = ftrace_match_records(hash, func, len);
2640 command = strsep(&next, ":");
2642 mutex_lock(&ftrace_cmd_mutex);
2643 list_for_each_entry(p, &ftrace_commands, list) {
2644 if (strcmp(p->name, command) == 0) {
2645 ret = p->func(func, command, next, enable);
2650 mutex_unlock(&ftrace_cmd_mutex);
2656 ftrace_regex_write(struct file *file, const char __user *ubuf,
2657 size_t cnt, loff_t *ppos, int enable)
2659 struct ftrace_iterator *iter;
2660 struct trace_parser *parser;
2666 mutex_lock(&ftrace_regex_lock);
2669 if (unlikely(ftrace_disabled))
2672 if (file->f_mode & FMODE_READ) {
2673 struct seq_file *m = file->private_data;
2676 iter = file->private_data;
2678 parser = &iter->parser;
2679 read = trace_get_user(parser, ubuf, cnt, ppos);
2681 if (read >= 0 && trace_parser_loaded(parser) &&
2682 !trace_parser_cont(parser)) {
2683 ret = ftrace_process_regex(iter->hash, parser->buffer,
2684 parser->idx, enable);
2685 trace_parser_clear(parser);
2692 mutex_unlock(&ftrace_regex_lock);
2698 ftrace_filter_write(struct file *file, const char __user *ubuf,
2699 size_t cnt, loff_t *ppos)
2701 return ftrace_regex_write(file, ubuf, cnt, ppos, 1);
2705 ftrace_notrace_write(struct file *file, const char __user *ubuf,
2706 size_t cnt, loff_t *ppos)
2708 return ftrace_regex_write(file, ubuf, cnt, ppos, 0);
2712 ftrace_set_regex(struct ftrace_ops *ops, unsigned char *buf, int len,
2713 int reset, int enable)
2715 struct ftrace_hash **orig_hash;
2716 struct ftrace_hash *hash;
2719 if (unlikely(ftrace_disabled))
2723 orig_hash = &ops->filter_hash;
2725 orig_hash = &ops->notrace_hash;
2727 hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, *orig_hash);
2731 mutex_lock(&ftrace_regex_lock);
2733 ftrace_filter_reset(hash);
2735 ftrace_match_records(hash, buf, len);
2737 mutex_lock(&ftrace_lock);
2738 ret = ftrace_hash_move(orig_hash, hash);
2739 mutex_unlock(&ftrace_lock);
2741 mutex_unlock(&ftrace_regex_lock);
2743 free_ftrace_hash(hash);
2748 * ftrace_set_filter - set a function to filter on in ftrace
2749 * @buf - the string that holds the function filter text.
2750 * @len - the length of the string.
2751 * @reset - non zero to reset all filters before applying this filter.
2753 * Filters denote which functions should be enabled when tracing is enabled.
2754 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
2756 void ftrace_set_filter(unsigned char *buf, int len, int reset)
2758 ftrace_set_regex(&global_ops, buf, len, reset, 1);
2762 * ftrace_set_notrace - set a function to not trace in ftrace
2763 * @buf - the string that holds the function notrace text.
2764 * @len - the length of the string.
2765 * @reset - non zero to reset all filters before applying this filter.
2767 * Notrace Filters denote which functions should not be enabled when tracing
2768 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
2771 void ftrace_set_notrace(unsigned char *buf, int len, int reset)
2773 ftrace_set_regex(&global_ops, buf, len, reset, 0);
2777 * command line interface to allow users to set filters on boot up.
2779 #define FTRACE_FILTER_SIZE COMMAND_LINE_SIZE
2780 static char ftrace_notrace_buf[FTRACE_FILTER_SIZE] __initdata;
2781 static char ftrace_filter_buf[FTRACE_FILTER_SIZE] __initdata;
2783 static int __init set_ftrace_notrace(char *str)
2785 strncpy(ftrace_notrace_buf, str, FTRACE_FILTER_SIZE);
2788 __setup("ftrace_notrace=", set_ftrace_notrace);
2790 static int __init set_ftrace_filter(char *str)
2792 strncpy(ftrace_filter_buf, str, FTRACE_FILTER_SIZE);
2795 __setup("ftrace_filter=", set_ftrace_filter);
2797 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
2798 static char ftrace_graph_buf[FTRACE_FILTER_SIZE] __initdata;
2799 static int ftrace_set_func(unsigned long *array, int *idx, char *buffer);
2801 static int __init set_graph_function(char *str)
2803 strlcpy(ftrace_graph_buf, str, FTRACE_FILTER_SIZE);
2806 __setup("ftrace_graph_filter=", set_graph_function);
2808 static void __init set_ftrace_early_graph(char *buf)
2814 func = strsep(&buf, ",");
2815 /* we allow only one expression at a time */
2816 ret = ftrace_set_func(ftrace_graph_funcs, &ftrace_graph_count,
2819 printk(KERN_DEBUG "ftrace: function %s not "
2820 "traceable\n", func);
2823 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
2826 set_ftrace_early_filter(struct ftrace_ops *ops, char *buf, int enable)
2831 func = strsep(&buf, ",");
2832 ftrace_set_regex(ops, func, strlen(func), 0, enable);
2836 static void __init set_ftrace_early_filters(void)
2838 if (ftrace_filter_buf[0])
2839 set_ftrace_early_filter(&global_ops, ftrace_filter_buf, 1);
2840 if (ftrace_notrace_buf[0])
2841 set_ftrace_early_filter(&global_ops, ftrace_notrace_buf, 0);
2842 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
2843 if (ftrace_graph_buf[0])
2844 set_ftrace_early_graph(ftrace_graph_buf);
2845 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
2849 ftrace_regex_release(struct inode *inode, struct file *file)
2851 struct seq_file *m = (struct seq_file *)file->private_data;
2852 struct ftrace_iterator *iter;
2853 struct ftrace_hash **orig_hash;
2854 struct trace_parser *parser;
2858 mutex_lock(&ftrace_regex_lock);
2859 if (file->f_mode & FMODE_READ) {
2862 seq_release(inode, file);
2864 iter = file->private_data;
2866 parser = &iter->parser;
2867 if (trace_parser_loaded(parser)) {
2868 parser->buffer[parser->idx] = 0;
2869 ftrace_match_records(iter->hash, parser->buffer, parser->idx);
2872 trace_parser_put(parser);
2874 if (file->f_mode & FMODE_WRITE) {
2875 filter_hash = !!(iter->flags & FTRACE_ITER_FILTER);
2878 orig_hash = &iter->ops->filter_hash;
2880 orig_hash = &iter->ops->notrace_hash;
2882 mutex_lock(&ftrace_lock);
2884 * Remove the current set, update the hash and add
2887 ftrace_hash_rec_disable(iter->ops, filter_hash);
2888 ret = ftrace_hash_move(orig_hash, iter->hash);
2890 ftrace_hash_rec_enable(iter->ops, filter_hash);
2891 if (iter->ops->flags & FTRACE_OPS_FL_ENABLED
2893 ftrace_run_update_code(FTRACE_ENABLE_CALLS);
2895 mutex_unlock(&ftrace_lock);
2897 free_ftrace_hash(iter->hash);
2900 mutex_unlock(&ftrace_regex_lock);
2904 static const struct file_operations ftrace_avail_fops = {
2905 .open = ftrace_avail_open,
2907 .llseek = seq_lseek,
2908 .release = seq_release_private,
2911 static const struct file_operations ftrace_enabled_fops = {
2912 .open = ftrace_enabled_open,
2914 .llseek = seq_lseek,
2915 .release = seq_release_private,
2918 static const struct file_operations ftrace_filter_fops = {
2919 .open = ftrace_filter_open,
2921 .write = ftrace_filter_write,
2922 .llseek = ftrace_regex_lseek,
2923 .release = ftrace_regex_release,
2926 static const struct file_operations ftrace_notrace_fops = {
2927 .open = ftrace_notrace_open,
2929 .write = ftrace_notrace_write,
2930 .llseek = ftrace_regex_lseek,
2931 .release = ftrace_regex_release,
2934 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
2936 static DEFINE_MUTEX(graph_lock);
2938 int ftrace_graph_count;
2939 int ftrace_graph_filter_enabled;
2940 unsigned long ftrace_graph_funcs[FTRACE_GRAPH_MAX_FUNCS] __read_mostly;
2943 __g_next(struct seq_file *m, loff_t *pos)
2945 if (*pos >= ftrace_graph_count)
2947 return &ftrace_graph_funcs[*pos];
2951 g_next(struct seq_file *m, void *v, loff_t *pos)
2954 return __g_next(m, pos);
2957 static void *g_start(struct seq_file *m, loff_t *pos)
2959 mutex_lock(&graph_lock);
2961 /* Nothing, tell g_show to print all functions are enabled */
2962 if (!ftrace_graph_filter_enabled && !*pos)
2965 return __g_next(m, pos);
2968 static void g_stop(struct seq_file *m, void *p)
2970 mutex_unlock(&graph_lock);
2973 static int g_show(struct seq_file *m, void *v)
2975 unsigned long *ptr = v;
2980 if (ptr == (unsigned long *)1) {
2981 seq_printf(m, "#### all functions enabled ####\n");
2985 seq_printf(m, "%ps\n", (void *)*ptr);
2990 static const struct seq_operations ftrace_graph_seq_ops = {
2998 ftrace_graph_open(struct inode *inode, struct file *file)
3002 if (unlikely(ftrace_disabled))
3005 mutex_lock(&graph_lock);
3006 if ((file->f_mode & FMODE_WRITE) &&
3007 (file->f_flags & O_TRUNC)) {
3008 ftrace_graph_filter_enabled = 0;
3009 ftrace_graph_count = 0;
3010 memset(ftrace_graph_funcs, 0, sizeof(ftrace_graph_funcs));
3012 mutex_unlock(&graph_lock);
3014 if (file->f_mode & FMODE_READ)
3015 ret = seq_open(file, &ftrace_graph_seq_ops);
3021 ftrace_graph_release(struct inode *inode, struct file *file)
3023 if (file->f_mode & FMODE_READ)
3024 seq_release(inode, file);
3029 ftrace_set_func(unsigned long *array, int *idx, char *buffer)
3031 struct dyn_ftrace *rec;
3032 struct ftrace_page *pg;
3041 type = filter_parse_regex(buffer, strlen(buffer), &search, ¬);
3042 if (!not && *idx >= FTRACE_GRAPH_MAX_FUNCS)
3045 search_len = strlen(search);
3047 mutex_lock(&ftrace_lock);
3049 if (unlikely(ftrace_disabled)) {
3050 mutex_unlock(&ftrace_lock);
3054 do_for_each_ftrace_rec(pg, rec) {
3056 if (rec->flags & FTRACE_FL_FREE)
3059 if (ftrace_match_record(rec, NULL, search, search_len, type)) {
3060 /* if it is in the array */
3062 for (i = 0; i < *idx; i++) {
3063 if (array[i] == rec->ip) {
3072 array[(*idx)++] = rec->ip;
3073 if (*idx >= FTRACE_GRAPH_MAX_FUNCS)
3078 array[i] = array[--(*idx)];
3084 } while_for_each_ftrace_rec();
3086 mutex_unlock(&ftrace_lock);
3091 ftrace_graph_filter_enabled = 1;
3096 ftrace_graph_write(struct file *file, const char __user *ubuf,
3097 size_t cnt, loff_t *ppos)
3099 struct trace_parser parser;
3105 mutex_lock(&graph_lock);
3107 if (trace_parser_get_init(&parser, FTRACE_BUFF_MAX)) {
3112 read = trace_get_user(&parser, ubuf, cnt, ppos);
3114 if (read >= 0 && trace_parser_loaded((&parser))) {
3115 parser.buffer[parser.idx] = 0;
3117 /* we allow only one expression at a time */
3118 ret = ftrace_set_func(ftrace_graph_funcs, &ftrace_graph_count,
3127 trace_parser_put(&parser);
3129 mutex_unlock(&graph_lock);
3134 static const struct file_operations ftrace_graph_fops = {
3135 .open = ftrace_graph_open,
3137 .write = ftrace_graph_write,
3138 .release = ftrace_graph_release,
3139 .llseek = seq_lseek,
3141 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
3143 static __init int ftrace_init_dyn_debugfs(struct dentry *d_tracer)
3146 trace_create_file("available_filter_functions", 0444,
3147 d_tracer, NULL, &ftrace_avail_fops);
3149 trace_create_file("enabled_functions", 0444,
3150 d_tracer, NULL, &ftrace_enabled_fops);
3152 trace_create_file("set_ftrace_filter", 0644, d_tracer,
3153 NULL, &ftrace_filter_fops);
3155 trace_create_file("set_ftrace_notrace", 0644, d_tracer,
3156 NULL, &ftrace_notrace_fops);
3158 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
3159 trace_create_file("set_graph_function", 0444, d_tracer,
3161 &ftrace_graph_fops);
3162 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
3167 static int ftrace_process_locs(struct module *mod,
3168 unsigned long *start,
3174 mutex_lock(&ftrace_lock);
3177 addr = ftrace_call_adjust(*p++);
3179 * Some architecture linkers will pad between
3180 * the different mcount_loc sections of different
3181 * object files to satisfy alignments.
3182 * Skip any NULL pointers.
3186 ftrace_record_ip(addr);
3189 ftrace_update_code(mod);
3190 mutex_unlock(&ftrace_lock);
3195 #ifdef CONFIG_MODULES
3196 void ftrace_release_mod(struct module *mod)
3198 struct dyn_ftrace *rec;
3199 struct ftrace_page *pg;
3201 mutex_lock(&ftrace_lock);
3203 if (ftrace_disabled)
3206 do_for_each_ftrace_rec(pg, rec) {
3207 if (within_module_core(rec->ip, mod)) {
3209 * rec->ip is changed in ftrace_free_rec()
3210 * It should not between s and e if record was freed.
3212 FTRACE_WARN_ON(rec->flags & FTRACE_FL_FREE);
3213 ftrace_free_rec(rec);
3215 } while_for_each_ftrace_rec();
3217 mutex_unlock(&ftrace_lock);
3220 static void ftrace_init_module(struct module *mod,
3221 unsigned long *start, unsigned long *end)
3223 if (ftrace_disabled || start == end)
3225 ftrace_process_locs(mod, start, end);
3228 static int ftrace_module_notify(struct notifier_block *self,
3229 unsigned long val, void *data)
3231 struct module *mod = data;
3234 case MODULE_STATE_COMING:
3235 ftrace_init_module(mod, mod->ftrace_callsites,
3236 mod->ftrace_callsites +
3237 mod->num_ftrace_callsites);
3239 case MODULE_STATE_GOING:
3240 ftrace_release_mod(mod);
3247 static int ftrace_module_notify(struct notifier_block *self,
3248 unsigned long val, void *data)
3252 #endif /* CONFIG_MODULES */
3254 struct notifier_block ftrace_module_nb = {
3255 .notifier_call = ftrace_module_notify,
3259 extern unsigned long __start_mcount_loc[];
3260 extern unsigned long __stop_mcount_loc[];
3262 void __init ftrace_init(void)
3264 unsigned long count, addr, flags;
3267 /* Keep the ftrace pointer to the stub */
3268 addr = (unsigned long)ftrace_stub;
3270 local_irq_save(flags);
3271 ftrace_dyn_arch_init(&addr);
3272 local_irq_restore(flags);
3274 /* ftrace_dyn_arch_init places the return code in addr */
3278 count = __stop_mcount_loc - __start_mcount_loc;
3280 ret = ftrace_dyn_table_alloc(count);
3284 last_ftrace_enabled = ftrace_enabled = 1;
3286 ret = ftrace_process_locs(NULL,
3290 ret = register_module_notifier(&ftrace_module_nb);
3292 pr_warning("Failed to register trace ftrace module notifier\n");
3294 set_ftrace_early_filters();
3298 ftrace_disabled = 1;
3303 static struct ftrace_ops global_ops = {
3304 .func = ftrace_stub,
3307 static int __init ftrace_nodyn_init(void)
3312 device_initcall(ftrace_nodyn_init);
3314 static inline int ftrace_init_dyn_debugfs(struct dentry *d_tracer) { return 0; }
3315 static inline void ftrace_startup_enable(int command) { }
3316 /* Keep as macros so we do not need to define the commands */
3317 # define ftrace_startup(ops, command) do { } while (0)
3318 # define ftrace_shutdown(ops, command) do { } while (0)
3319 # define ftrace_startup_sysctl() do { } while (0)
3320 # define ftrace_shutdown_sysctl() do { } while (0)
3321 #endif /* CONFIG_DYNAMIC_FTRACE */
3323 static void clear_ftrace_swapper(void)
3325 struct task_struct *p;
3329 for_each_online_cpu(cpu) {
3331 clear_tsk_trace_trace(p);
3336 static void set_ftrace_swapper(void)
3338 struct task_struct *p;
3342 for_each_online_cpu(cpu) {
3344 set_tsk_trace_trace(p);
3349 static void clear_ftrace_pid(struct pid *pid)
3351 struct task_struct *p;
3354 do_each_pid_task(pid, PIDTYPE_PID, p) {
3355 clear_tsk_trace_trace(p);
3356 } while_each_pid_task(pid, PIDTYPE_PID, p);
3362 static void set_ftrace_pid(struct pid *pid)
3364 struct task_struct *p;
3367 do_each_pid_task(pid, PIDTYPE_PID, p) {
3368 set_tsk_trace_trace(p);
3369 } while_each_pid_task(pid, PIDTYPE_PID, p);
3373 static void clear_ftrace_pid_task(struct pid *pid)
3375 if (pid == ftrace_swapper_pid)
3376 clear_ftrace_swapper();
3378 clear_ftrace_pid(pid);
3381 static void set_ftrace_pid_task(struct pid *pid)
3383 if (pid == ftrace_swapper_pid)
3384 set_ftrace_swapper();
3386 set_ftrace_pid(pid);
3389 static int ftrace_pid_add(int p)
3392 struct ftrace_pid *fpid;
3395 mutex_lock(&ftrace_lock);
3398 pid = ftrace_swapper_pid;
3400 pid = find_get_pid(p);
3407 list_for_each_entry(fpid, &ftrace_pids, list)
3408 if (fpid->pid == pid)
3413 fpid = kmalloc(sizeof(*fpid), GFP_KERNEL);
3417 list_add(&fpid->list, &ftrace_pids);
3420 set_ftrace_pid_task(pid);
3422 ftrace_update_pid_func();
3423 ftrace_startup_enable(0);
3425 mutex_unlock(&ftrace_lock);
3429 if (pid != ftrace_swapper_pid)
3433 mutex_unlock(&ftrace_lock);
3437 static void ftrace_pid_reset(void)
3439 struct ftrace_pid *fpid, *safe;
3441 mutex_lock(&ftrace_lock);
3442 list_for_each_entry_safe(fpid, safe, &ftrace_pids, list) {
3443 struct pid *pid = fpid->pid;
3445 clear_ftrace_pid_task(pid);
3447 list_del(&fpid->list);
3451 ftrace_update_pid_func();
3452 ftrace_startup_enable(0);
3454 mutex_unlock(&ftrace_lock);
3457 static void *fpid_start(struct seq_file *m, loff_t *pos)
3459 mutex_lock(&ftrace_lock);
3461 if (list_empty(&ftrace_pids) && (!*pos))
3464 return seq_list_start(&ftrace_pids, *pos);
3467 static void *fpid_next(struct seq_file *m, void *v, loff_t *pos)
3472 return seq_list_next(v, &ftrace_pids, pos);
3475 static void fpid_stop(struct seq_file *m, void *p)
3477 mutex_unlock(&ftrace_lock);
3480 static int fpid_show(struct seq_file *m, void *v)
3482 const struct ftrace_pid *fpid = list_entry(v, struct ftrace_pid, list);
3484 if (v == (void *)1) {
3485 seq_printf(m, "no pid\n");
3489 if (fpid->pid == ftrace_swapper_pid)
3490 seq_printf(m, "swapper tasks\n");
3492 seq_printf(m, "%u\n", pid_vnr(fpid->pid));
3497 static const struct seq_operations ftrace_pid_sops = {
3498 .start = fpid_start,
3505 ftrace_pid_open(struct inode *inode, struct file *file)
3509 if ((file->f_mode & FMODE_WRITE) &&
3510 (file->f_flags & O_TRUNC))
3513 if (file->f_mode & FMODE_READ)
3514 ret = seq_open(file, &ftrace_pid_sops);
3520 ftrace_pid_write(struct file *filp, const char __user *ubuf,
3521 size_t cnt, loff_t *ppos)
3527 if (cnt >= sizeof(buf))
3530 if (copy_from_user(&buf, ubuf, cnt))
3536 * Allow "echo > set_ftrace_pid" or "echo -n '' > set_ftrace_pid"
3537 * to clean the filter quietly.
3539 tmp = strstrip(buf);
3540 if (strlen(tmp) == 0)
3543 ret = strict_strtol(tmp, 10, &val);
3547 ret = ftrace_pid_add(val);
3549 return ret ? ret : cnt;
3553 ftrace_pid_release(struct inode *inode, struct file *file)
3555 if (file->f_mode & FMODE_READ)
3556 seq_release(inode, file);
3561 static const struct file_operations ftrace_pid_fops = {
3562 .open = ftrace_pid_open,
3563 .write = ftrace_pid_write,
3565 .llseek = seq_lseek,
3566 .release = ftrace_pid_release,
3569 static __init int ftrace_init_debugfs(void)
3571 struct dentry *d_tracer;
3573 d_tracer = tracing_init_dentry();
3577 ftrace_init_dyn_debugfs(d_tracer);
3579 trace_create_file("set_ftrace_pid", 0644, d_tracer,
3580 NULL, &ftrace_pid_fops);
3582 ftrace_profile_debugfs(d_tracer);
3586 fs_initcall(ftrace_init_debugfs);
3589 * ftrace_kill - kill ftrace
3591 * This function should be used by panic code. It stops ftrace
3592 * but in a not so nice way. If you need to simply kill ftrace
3593 * from a non-atomic section, use ftrace_kill.
3595 void ftrace_kill(void)
3597 ftrace_disabled = 1;
3599 clear_ftrace_function();
3603 * register_ftrace_function - register a function for profiling
3604 * @ops - ops structure that holds the function for profiling.
3606 * Register a function to be called by all functions in the
3609 * Note: @ops->func and all the functions it calls must be labeled
3610 * with "notrace", otherwise it will go into a
3613 int register_ftrace_function(struct ftrace_ops *ops)
3617 mutex_lock(&ftrace_lock);
3619 if (unlikely(ftrace_disabled))
3622 ret = __register_ftrace_function(ops);
3623 ftrace_startup(&global_ops, 0);
3626 mutex_unlock(&ftrace_lock);
3631 * unregister_ftrace_function - unregister a function for profiling.
3632 * @ops - ops structure that holds the function to unregister
3634 * Unregister a function that was added to be called by ftrace profiling.
3636 int unregister_ftrace_function(struct ftrace_ops *ops)
3640 mutex_lock(&ftrace_lock);
3641 ret = __unregister_ftrace_function(ops);
3642 ftrace_shutdown(&global_ops, 0);
3643 mutex_unlock(&ftrace_lock);
3649 ftrace_enable_sysctl(struct ctl_table *table, int write,
3650 void __user *buffer, size_t *lenp,
3655 mutex_lock(&ftrace_lock);
3657 if (unlikely(ftrace_disabled))
3660 ret = proc_dointvec(table, write, buffer, lenp, ppos);
3662 if (ret || !write || (last_ftrace_enabled == !!ftrace_enabled))
3665 last_ftrace_enabled = !!ftrace_enabled;
3667 if (ftrace_enabled) {
3669 ftrace_startup_sysctl();
3671 /* we are starting ftrace again */
3672 if (ftrace_list != &ftrace_list_end) {
3673 if (ftrace_list->next == &ftrace_list_end)
3674 ftrace_trace_function = ftrace_list->func;
3676 ftrace_trace_function = ftrace_list_func;
3680 /* stopping ftrace calls (just send to ftrace_stub) */
3681 ftrace_trace_function = ftrace_stub;
3683 ftrace_shutdown_sysctl();
3687 mutex_unlock(&ftrace_lock);
3691 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
3693 static int ftrace_graph_active;
3694 static struct notifier_block ftrace_suspend_notifier;
3696 int ftrace_graph_entry_stub(struct ftrace_graph_ent *trace)
3701 /* The callbacks that hook a function */
3702 trace_func_graph_ret_t ftrace_graph_return =
3703 (trace_func_graph_ret_t)ftrace_stub;
3704 trace_func_graph_ent_t ftrace_graph_entry = ftrace_graph_entry_stub;
3706 /* Try to assign a return stack array on FTRACE_RETSTACK_ALLOC_SIZE tasks. */
3707 static int alloc_retstack_tasklist(struct ftrace_ret_stack **ret_stack_list)
3711 unsigned long flags;
3712 int start = 0, end = FTRACE_RETSTACK_ALLOC_SIZE;
3713 struct task_struct *g, *t;
3715 for (i = 0; i < FTRACE_RETSTACK_ALLOC_SIZE; i++) {
3716 ret_stack_list[i] = kmalloc(FTRACE_RETFUNC_DEPTH
3717 * sizeof(struct ftrace_ret_stack),
3719 if (!ret_stack_list[i]) {
3727 read_lock_irqsave(&tasklist_lock, flags);
3728 do_each_thread(g, t) {
3734 if (t->ret_stack == NULL) {
3735 atomic_set(&t->tracing_graph_pause, 0);
3736 atomic_set(&t->trace_overrun, 0);
3737 t->curr_ret_stack = -1;
3738 /* Make sure the tasks see the -1 first: */
3740 t->ret_stack = ret_stack_list[start++];
3742 } while_each_thread(g, t);
3745 read_unlock_irqrestore(&tasklist_lock, flags);
3747 for (i = start; i < end; i++)
3748 kfree(ret_stack_list[i]);
3753 ftrace_graph_probe_sched_switch(void *ignore,
3754 struct task_struct *prev, struct task_struct *next)
3756 unsigned long long timestamp;
3760 * Does the user want to count the time a function was asleep.
3761 * If so, do not update the time stamps.
3763 if (trace_flags & TRACE_ITER_SLEEP_TIME)
3766 timestamp = trace_clock_local();
3768 prev->ftrace_timestamp = timestamp;
3770 /* only process tasks that we timestamped */
3771 if (!next->ftrace_timestamp)
3775 * Update all the counters in next to make up for the
3776 * time next was sleeping.
3778 timestamp -= next->ftrace_timestamp;
3780 for (index = next->curr_ret_stack; index >= 0; index--)
3781 next->ret_stack[index].calltime += timestamp;
3784 /* Allocate a return stack for each task */
3785 static int start_graph_tracing(void)
3787 struct ftrace_ret_stack **ret_stack_list;
3790 ret_stack_list = kmalloc(FTRACE_RETSTACK_ALLOC_SIZE *
3791 sizeof(struct ftrace_ret_stack *),
3794 if (!ret_stack_list)
3797 /* The cpu_boot init_task->ret_stack will never be freed */
3798 for_each_online_cpu(cpu) {
3799 if (!idle_task(cpu)->ret_stack)
3800 ftrace_graph_init_idle_task(idle_task(cpu), cpu);
3804 ret = alloc_retstack_tasklist(ret_stack_list);
3805 } while (ret == -EAGAIN);
3808 ret = register_trace_sched_switch(ftrace_graph_probe_sched_switch, NULL);
3810 pr_info("ftrace_graph: Couldn't activate tracepoint"
3811 " probe to kernel_sched_switch\n");
3814 kfree(ret_stack_list);
3819 * Hibernation protection.
3820 * The state of the current task is too much unstable during
3821 * suspend/restore to disk. We want to protect against that.
3824 ftrace_suspend_notifier_call(struct notifier_block *bl, unsigned long state,
3828 case PM_HIBERNATION_PREPARE:
3829 pause_graph_tracing();
3832 case PM_POST_HIBERNATION:
3833 unpause_graph_tracing();
3839 int register_ftrace_graph(trace_func_graph_ret_t retfunc,
3840 trace_func_graph_ent_t entryfunc)
3844 mutex_lock(&ftrace_lock);
3846 /* we currently allow only one tracer registered at a time */
3847 if (ftrace_graph_active) {
3852 ftrace_suspend_notifier.notifier_call = ftrace_suspend_notifier_call;
3853 register_pm_notifier(&ftrace_suspend_notifier);
3855 ftrace_graph_active++;
3856 ret = start_graph_tracing();
3858 ftrace_graph_active--;
3862 ftrace_graph_return = retfunc;
3863 ftrace_graph_entry = entryfunc;
3865 ftrace_startup(&global_ops, FTRACE_START_FUNC_RET);
3868 mutex_unlock(&ftrace_lock);
3872 void unregister_ftrace_graph(void)
3874 mutex_lock(&ftrace_lock);
3876 if (unlikely(!ftrace_graph_active))
3879 ftrace_graph_active--;
3880 ftrace_graph_return = (trace_func_graph_ret_t)ftrace_stub;
3881 ftrace_graph_entry = ftrace_graph_entry_stub;
3882 ftrace_shutdown(&global_ops, FTRACE_STOP_FUNC_RET);
3883 unregister_pm_notifier(&ftrace_suspend_notifier);
3884 unregister_trace_sched_switch(ftrace_graph_probe_sched_switch, NULL);
3887 mutex_unlock(&ftrace_lock);
3890 static DEFINE_PER_CPU(struct ftrace_ret_stack *, idle_ret_stack);
3893 graph_init_task(struct task_struct *t, struct ftrace_ret_stack *ret_stack)
3895 atomic_set(&t->tracing_graph_pause, 0);
3896 atomic_set(&t->trace_overrun, 0);
3897 t->ftrace_timestamp = 0;
3898 /* make curr_ret_stack visible before we add the ret_stack */
3900 t->ret_stack = ret_stack;
3904 * Allocate a return stack for the idle task. May be the first
3905 * time through, or it may be done by CPU hotplug online.
3907 void ftrace_graph_init_idle_task(struct task_struct *t, int cpu)
3909 t->curr_ret_stack = -1;
3911 * The idle task has no parent, it either has its own
3912 * stack or no stack at all.
3915 WARN_ON(t->ret_stack != per_cpu(idle_ret_stack, cpu));
3917 if (ftrace_graph_active) {
3918 struct ftrace_ret_stack *ret_stack;
3920 ret_stack = per_cpu(idle_ret_stack, cpu);
3922 ret_stack = kmalloc(FTRACE_RETFUNC_DEPTH
3923 * sizeof(struct ftrace_ret_stack),
3927 per_cpu(idle_ret_stack, cpu) = ret_stack;
3929 graph_init_task(t, ret_stack);
3933 /* Allocate a return stack for newly created task */
3934 void ftrace_graph_init_task(struct task_struct *t)
3936 /* Make sure we do not use the parent ret_stack */
3937 t->ret_stack = NULL;
3938 t->curr_ret_stack = -1;
3940 if (ftrace_graph_active) {
3941 struct ftrace_ret_stack *ret_stack;
3943 ret_stack = kmalloc(FTRACE_RETFUNC_DEPTH
3944 * sizeof(struct ftrace_ret_stack),
3948 graph_init_task(t, ret_stack);
3952 void ftrace_graph_exit_task(struct task_struct *t)
3954 struct ftrace_ret_stack *ret_stack = t->ret_stack;
3956 t->ret_stack = NULL;
3957 /* NULL must become visible to IRQs before we free it: */
3963 void ftrace_graph_stop(void)