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;
920 * We make these constant because no one should touch them,
921 * but they are used as the default "empty hash", to avoid allocating
922 * it all the time. These are in a read only section such that if
923 * anyone does try to modify it, it will cause an exception.
925 static const struct hlist_head empty_buckets[1];
926 static const struct ftrace_hash empty_hash = {
927 .buckets = (struct hlist_head *)empty_buckets,
929 #define EMPTY_HASH ((struct ftrace_hash *)&empty_hash)
932 FTRACE_OPS_FL_ENABLED = 1,
935 static struct ftrace_ops global_ops = {
937 .notrace_hash = EMPTY_HASH,
938 .filter_hash = EMPTY_HASH,
941 static struct dyn_ftrace *ftrace_new_addrs;
943 static DEFINE_MUTEX(ftrace_regex_lock);
946 struct ftrace_page *next;
948 struct dyn_ftrace records[];
951 #define ENTRIES_PER_PAGE \
952 ((PAGE_SIZE - sizeof(struct ftrace_page)) / sizeof(struct dyn_ftrace))
954 /* estimate from running different kernels */
955 #define NR_TO_INIT 10000
957 static struct ftrace_page *ftrace_pages_start;
958 static struct ftrace_page *ftrace_pages;
960 static struct dyn_ftrace *ftrace_free_records;
962 static struct ftrace_func_entry *
963 ftrace_lookup_ip(struct ftrace_hash *hash, unsigned long ip)
966 struct ftrace_func_entry *entry;
967 struct hlist_head *hhd;
968 struct hlist_node *n;
973 if (hash->size_bits > 0)
974 key = hash_long(ip, hash->size_bits);
978 hhd = &hash->buckets[key];
980 hlist_for_each_entry_rcu(entry, n, hhd, hlist) {
987 static void __add_hash_entry(struct ftrace_hash *hash,
988 struct ftrace_func_entry *entry)
990 struct hlist_head *hhd;
994 key = hash_long(entry->ip, hash->size_bits);
998 hhd = &hash->buckets[key];
999 hlist_add_head(&entry->hlist, hhd);
1003 static int add_hash_entry(struct ftrace_hash *hash, unsigned long ip)
1005 struct ftrace_func_entry *entry;
1007 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
1012 __add_hash_entry(hash, entry);
1018 free_hash_entry(struct ftrace_hash *hash,
1019 struct ftrace_func_entry *entry)
1021 hlist_del(&entry->hlist);
1027 remove_hash_entry(struct ftrace_hash *hash,
1028 struct ftrace_func_entry *entry)
1030 hlist_del(&entry->hlist);
1034 static void ftrace_hash_clear(struct ftrace_hash *hash)
1036 struct hlist_head *hhd;
1037 struct hlist_node *tp, *tn;
1038 struct ftrace_func_entry *entry;
1039 int size = 1 << hash->size_bits;
1045 for (i = 0; i < size; i++) {
1046 hhd = &hash->buckets[i];
1047 hlist_for_each_entry_safe(entry, tp, tn, hhd, hlist)
1048 free_hash_entry(hash, entry);
1050 FTRACE_WARN_ON(hash->count);
1053 static void free_ftrace_hash(struct ftrace_hash *hash)
1055 if (!hash || hash == EMPTY_HASH)
1057 ftrace_hash_clear(hash);
1058 kfree(hash->buckets);
1062 static void __free_ftrace_hash_rcu(struct rcu_head *rcu)
1064 struct ftrace_hash *hash;
1066 hash = container_of(rcu, struct ftrace_hash, rcu);
1067 free_ftrace_hash(hash);
1070 static void free_ftrace_hash_rcu(struct ftrace_hash *hash)
1072 if (!hash || hash == EMPTY_HASH)
1074 call_rcu_sched(&hash->rcu, __free_ftrace_hash_rcu);
1077 static struct ftrace_hash *alloc_ftrace_hash(int size_bits)
1079 struct ftrace_hash *hash;
1082 hash = kzalloc(sizeof(*hash), GFP_KERNEL);
1086 size = 1 << size_bits;
1087 hash->buckets = kzalloc(sizeof(*hash->buckets) * size, GFP_KERNEL);
1089 if (!hash->buckets) {
1094 hash->size_bits = size_bits;
1099 static struct ftrace_hash *
1100 alloc_and_copy_ftrace_hash(int size_bits, struct ftrace_hash *hash)
1102 struct ftrace_func_entry *entry;
1103 struct ftrace_hash *new_hash;
1104 struct hlist_node *tp;
1109 new_hash = alloc_ftrace_hash(size_bits);
1114 if (!hash || !hash->count)
1117 size = 1 << hash->size_bits;
1118 for (i = 0; i < size; i++) {
1119 hlist_for_each_entry(entry, tp, &hash->buckets[i], hlist) {
1120 ret = add_hash_entry(new_hash, entry->ip);
1126 FTRACE_WARN_ON(new_hash->count != hash->count);
1131 free_ftrace_hash(new_hash);
1136 ftrace_hash_move(struct ftrace_hash **dst, struct ftrace_hash *src)
1138 struct ftrace_func_entry *entry;
1139 struct hlist_node *tp, *tn;
1140 struct hlist_head *hhd;
1141 struct ftrace_hash *old_hash;
1142 struct ftrace_hash *new_hash;
1144 int size = src->count;
1149 * If the new source is empty, just free dst and assign it
1153 free_ftrace_hash_rcu(*dst);
1154 rcu_assign_pointer(*dst, EMPTY_HASH);
1159 * Make the hash size about 1/2 the # found
1161 for (size /= 2; size; size >>= 1)
1164 /* Don't allocate too much */
1165 if (bits > FTRACE_HASH_MAX_BITS)
1166 bits = FTRACE_HASH_MAX_BITS;
1168 new_hash = alloc_ftrace_hash(bits);
1172 size = 1 << src->size_bits;
1173 for (i = 0; i < size; i++) {
1174 hhd = &src->buckets[i];
1175 hlist_for_each_entry_safe(entry, tp, tn, hhd, hlist) {
1177 key = hash_long(entry->ip, bits);
1180 remove_hash_entry(src, entry);
1181 __add_hash_entry(new_hash, entry);
1186 rcu_assign_pointer(*dst, new_hash);
1187 free_ftrace_hash_rcu(old_hash);
1193 * This is a double for. Do not use 'break' to break out of the loop,
1194 * you must use a goto.
1196 #define do_for_each_ftrace_rec(pg, rec) \
1197 for (pg = ftrace_pages_start; pg; pg = pg->next) { \
1199 for (_____i = 0; _____i < pg->index; _____i++) { \
1200 rec = &pg->records[_____i];
1202 #define while_for_each_ftrace_rec() \
1206 static void __ftrace_hash_rec_update(struct ftrace_ops *ops,
1210 struct ftrace_hash *hash;
1211 struct ftrace_hash *other_hash;
1212 struct ftrace_page *pg;
1213 struct dyn_ftrace *rec;
1217 /* Only update if the ops has been registered */
1218 if (!(ops->flags & FTRACE_OPS_FL_ENABLED))
1222 * In the filter_hash case:
1223 * If the count is zero, we update all records.
1224 * Otherwise we just update the items in the hash.
1226 * In the notrace_hash case:
1227 * We enable the update in the hash.
1228 * As disabling notrace means enabling the tracing,
1229 * and enabling notrace means disabling, the inc variable
1233 hash = ops->filter_hash;
1234 other_hash = ops->notrace_hash;
1239 hash = ops->notrace_hash;
1240 other_hash = ops->filter_hash;
1242 * If the notrace hash has no items,
1243 * then there's nothing to do.
1249 do_for_each_ftrace_rec(pg, rec) {
1250 int in_other_hash = 0;
1256 * Only the filter_hash affects all records.
1257 * Update if the record is not in the notrace hash.
1259 if (!ftrace_lookup_ip(other_hash, rec->ip))
1262 in_hash = !!ftrace_lookup_ip(hash, rec->ip);
1263 in_other_hash = !!ftrace_lookup_ip(other_hash, rec->ip);
1268 if (filter_hash && in_hash && !in_other_hash)
1270 else if (!filter_hash && in_hash &&
1271 (in_other_hash || !other_hash->count))
1279 if (FTRACE_WARN_ON((rec->flags & ~FTRACE_FL_MASK) == FTRACE_REF_MAX))
1282 if (FTRACE_WARN_ON((rec->flags & ~FTRACE_FL_MASK) == 0))
1287 /* Shortcut, if we handled all records, we are done. */
1288 if (!all && count == hash->count)
1290 } while_for_each_ftrace_rec();
1293 static void ftrace_hash_rec_disable(struct ftrace_ops *ops,
1296 __ftrace_hash_rec_update(ops, filter_hash, 0);
1299 static void ftrace_hash_rec_enable(struct ftrace_ops *ops,
1302 __ftrace_hash_rec_update(ops, filter_hash, 1);
1305 static void ftrace_free_rec(struct dyn_ftrace *rec)
1307 rec->freelist = ftrace_free_records;
1308 ftrace_free_records = rec;
1309 rec->flags |= FTRACE_FL_FREE;
1312 static struct dyn_ftrace *ftrace_alloc_dyn_node(unsigned long ip)
1314 struct dyn_ftrace *rec;
1316 /* First check for freed records */
1317 if (ftrace_free_records) {
1318 rec = ftrace_free_records;
1320 if (unlikely(!(rec->flags & FTRACE_FL_FREE))) {
1321 FTRACE_WARN_ON_ONCE(1);
1322 ftrace_free_records = NULL;
1326 ftrace_free_records = rec->freelist;
1327 memset(rec, 0, sizeof(*rec));
1331 if (ftrace_pages->index == ENTRIES_PER_PAGE) {
1332 if (!ftrace_pages->next) {
1333 /* allocate another page */
1334 ftrace_pages->next =
1335 (void *)get_zeroed_page(GFP_KERNEL);
1336 if (!ftrace_pages->next)
1339 ftrace_pages = ftrace_pages->next;
1342 return &ftrace_pages->records[ftrace_pages->index++];
1345 static struct dyn_ftrace *
1346 ftrace_record_ip(unsigned long ip)
1348 struct dyn_ftrace *rec;
1350 if (ftrace_disabled)
1353 rec = ftrace_alloc_dyn_node(ip);
1358 rec->newlist = ftrace_new_addrs;
1359 ftrace_new_addrs = rec;
1364 static void print_ip_ins(const char *fmt, unsigned char *p)
1368 printk(KERN_CONT "%s", fmt);
1370 for (i = 0; i < MCOUNT_INSN_SIZE; i++)
1371 printk(KERN_CONT "%s%02x", i ? ":" : "", p[i]);
1374 static void ftrace_bug(int failed, unsigned long ip)
1378 FTRACE_WARN_ON_ONCE(1);
1379 pr_info("ftrace faulted on modifying ");
1383 FTRACE_WARN_ON_ONCE(1);
1384 pr_info("ftrace failed to modify ");
1386 print_ip_ins(" actual: ", (unsigned char *)ip);
1387 printk(KERN_CONT "\n");
1390 FTRACE_WARN_ON_ONCE(1);
1391 pr_info("ftrace faulted on writing ");
1395 FTRACE_WARN_ON_ONCE(1);
1396 pr_info("ftrace faulted on unknown error ");
1402 /* Return 1 if the address range is reserved for ftrace */
1403 int ftrace_text_reserved(void *start, void *end)
1405 struct dyn_ftrace *rec;
1406 struct ftrace_page *pg;
1408 do_for_each_ftrace_rec(pg, rec) {
1409 if (rec->ip <= (unsigned long)end &&
1410 rec->ip + MCOUNT_INSN_SIZE > (unsigned long)start)
1412 } while_for_each_ftrace_rec();
1418 __ftrace_replace_code(struct dyn_ftrace *rec, int enable)
1420 unsigned long ftrace_addr;
1421 unsigned long flag = 0UL;
1423 ftrace_addr = (unsigned long)FTRACE_ADDR;
1426 * If we are enabling tracing:
1428 * If the record has a ref count, then we need to enable it
1429 * because someone is using it.
1431 * Otherwise we make sure its disabled.
1433 * If we are disabling tracing, then disable all records that
1436 if (enable && (rec->flags & ~FTRACE_FL_MASK))
1437 flag = FTRACE_FL_ENABLED;
1439 /* If the state of this record hasn't changed, then do nothing */
1440 if ((rec->flags & FTRACE_FL_ENABLED) == flag)
1444 rec->flags |= FTRACE_FL_ENABLED;
1445 return ftrace_make_call(rec, ftrace_addr);
1448 rec->flags &= ~FTRACE_FL_ENABLED;
1449 return ftrace_make_nop(NULL, rec, ftrace_addr);
1452 static void ftrace_replace_code(int enable)
1454 struct dyn_ftrace *rec;
1455 struct ftrace_page *pg;
1458 if (unlikely(ftrace_disabled))
1461 do_for_each_ftrace_rec(pg, rec) {
1462 /* Skip over free records */
1463 if (rec->flags & FTRACE_FL_FREE)
1466 failed = __ftrace_replace_code(rec, enable);
1468 ftrace_bug(failed, rec->ip);
1469 /* Stop processing */
1472 } while_for_each_ftrace_rec();
1476 ftrace_code_disable(struct module *mod, struct dyn_ftrace *rec)
1483 if (unlikely(ftrace_disabled))
1486 ret = ftrace_make_nop(mod, rec, MCOUNT_ADDR);
1488 ftrace_bug(ret, ip);
1495 * archs can override this function if they must do something
1496 * before the modifying code is performed.
1498 int __weak ftrace_arch_code_modify_prepare(void)
1504 * archs can override this function if they must do something
1505 * after the modifying code is performed.
1507 int __weak ftrace_arch_code_modify_post_process(void)
1512 static int __ftrace_modify_code(void *data)
1514 int *command = data;
1516 if (*command & FTRACE_ENABLE_CALLS)
1517 ftrace_replace_code(1);
1518 else if (*command & FTRACE_DISABLE_CALLS)
1519 ftrace_replace_code(0);
1521 if (*command & FTRACE_UPDATE_TRACE_FUNC)
1522 ftrace_update_ftrace_func(ftrace_trace_function);
1524 if (*command & FTRACE_START_FUNC_RET)
1525 ftrace_enable_ftrace_graph_caller();
1526 else if (*command & FTRACE_STOP_FUNC_RET)
1527 ftrace_disable_ftrace_graph_caller();
1532 static void ftrace_run_update_code(int command)
1536 ret = ftrace_arch_code_modify_prepare();
1537 FTRACE_WARN_ON(ret);
1541 stop_machine(__ftrace_modify_code, &command, NULL);
1543 ret = ftrace_arch_code_modify_post_process();
1544 FTRACE_WARN_ON(ret);
1547 static ftrace_func_t saved_ftrace_func;
1548 static int ftrace_start_up;
1550 static void ftrace_startup_enable(int command)
1552 if (saved_ftrace_func != ftrace_trace_function) {
1553 saved_ftrace_func = ftrace_trace_function;
1554 command |= FTRACE_UPDATE_TRACE_FUNC;
1557 if (!command || !ftrace_enabled)
1560 ftrace_run_update_code(command);
1563 static void ftrace_startup(struct ftrace_ops *ops, int command)
1565 if (unlikely(ftrace_disabled))
1569 command |= FTRACE_ENABLE_CALLS;
1571 ops->flags |= FTRACE_OPS_FL_ENABLED;
1572 if (ftrace_start_up == 1)
1573 ftrace_hash_rec_enable(ops, 1);
1575 ftrace_startup_enable(command);
1578 static void ftrace_shutdown(struct ftrace_ops *ops, int command)
1580 if (unlikely(ftrace_disabled))
1585 * Just warn in case of unbalance, no need to kill ftrace, it's not
1586 * critical but the ftrace_call callers may be never nopped again after
1587 * further ftrace uses.
1589 WARN_ON_ONCE(ftrace_start_up < 0);
1591 if (!ftrace_start_up)
1592 ftrace_hash_rec_disable(ops, 1);
1594 if (!ftrace_start_up) {
1595 command |= FTRACE_DISABLE_CALLS;
1596 ops->flags &= ~FTRACE_OPS_FL_ENABLED;
1599 if (saved_ftrace_func != ftrace_trace_function) {
1600 saved_ftrace_func = ftrace_trace_function;
1601 command |= FTRACE_UPDATE_TRACE_FUNC;
1604 if (!command || !ftrace_enabled)
1607 ftrace_run_update_code(command);
1610 static void ftrace_startup_sysctl(void)
1612 if (unlikely(ftrace_disabled))
1615 /* Force update next time */
1616 saved_ftrace_func = NULL;
1617 /* ftrace_start_up is true if we want ftrace running */
1618 if (ftrace_start_up)
1619 ftrace_run_update_code(FTRACE_ENABLE_CALLS);
1622 static void ftrace_shutdown_sysctl(void)
1624 if (unlikely(ftrace_disabled))
1627 /* ftrace_start_up is true if ftrace is running */
1628 if (ftrace_start_up)
1629 ftrace_run_update_code(FTRACE_DISABLE_CALLS);
1632 static cycle_t ftrace_update_time;
1633 static unsigned long ftrace_update_cnt;
1634 unsigned long ftrace_update_tot_cnt;
1636 static int ftrace_update_code(struct module *mod)
1638 struct dyn_ftrace *p;
1639 cycle_t start, stop;
1641 start = ftrace_now(raw_smp_processor_id());
1642 ftrace_update_cnt = 0;
1644 while (ftrace_new_addrs) {
1646 /* If something went wrong, bail without enabling anything */
1647 if (unlikely(ftrace_disabled))
1650 p = ftrace_new_addrs;
1651 ftrace_new_addrs = p->newlist;
1655 * Do the initial record conversion from mcount jump
1656 * to the NOP instructions.
1658 if (!ftrace_code_disable(mod, p)) {
1664 ftrace_update_cnt++;
1667 * If the tracing is enabled, go ahead and enable the record.
1669 * The reason not to enable the record immediatelly is the
1670 * inherent check of ftrace_make_nop/ftrace_make_call for
1671 * correct previous instructions. Making first the NOP
1672 * conversion puts the module to the correct state, thus
1673 * passing the ftrace_make_call check.
1675 if (ftrace_start_up) {
1676 int failed = __ftrace_replace_code(p, 1);
1678 ftrace_bug(failed, p->ip);
1684 stop = ftrace_now(raw_smp_processor_id());
1685 ftrace_update_time = stop - start;
1686 ftrace_update_tot_cnt += ftrace_update_cnt;
1691 static int __init ftrace_dyn_table_alloc(unsigned long num_to_init)
1693 struct ftrace_page *pg;
1697 /* allocate a few pages */
1698 ftrace_pages_start = (void *)get_zeroed_page(GFP_KERNEL);
1699 if (!ftrace_pages_start)
1703 * Allocate a few more pages.
1705 * TODO: have some parser search vmlinux before
1706 * final linking to find all calls to ftrace.
1708 * a) know how many pages to allocate.
1710 * b) set up the table then.
1712 * The dynamic code is still necessary for
1716 pg = ftrace_pages = ftrace_pages_start;
1718 cnt = num_to_init / ENTRIES_PER_PAGE;
1719 pr_info("ftrace: allocating %ld entries in %d pages\n",
1720 num_to_init, cnt + 1);
1722 for (i = 0; i < cnt; i++) {
1723 pg->next = (void *)get_zeroed_page(GFP_KERNEL);
1725 /* If we fail, we'll try later anyway */
1736 FTRACE_ITER_FILTER = (1 << 0),
1737 FTRACE_ITER_NOTRACE = (1 << 1),
1738 FTRACE_ITER_PRINTALL = (1 << 2),
1739 FTRACE_ITER_HASH = (1 << 3),
1740 FTRACE_ITER_ENABLED = (1 << 4),
1743 #define FTRACE_BUFF_MAX (KSYM_SYMBOL_LEN+4) /* room for wildcards */
1745 struct ftrace_iterator {
1748 struct ftrace_page *pg;
1749 struct dyn_ftrace *func;
1750 struct ftrace_func_probe *probe;
1751 struct trace_parser parser;
1752 struct ftrace_hash *hash;
1753 struct ftrace_ops *ops;
1760 t_hash_next(struct seq_file *m, loff_t *pos)
1762 struct ftrace_iterator *iter = m->private;
1763 struct hlist_node *hnd = NULL;
1764 struct hlist_head *hhd;
1770 hnd = &iter->probe->node;
1772 if (iter->hidx >= FTRACE_FUNC_HASHSIZE)
1775 hhd = &ftrace_func_hash[iter->hidx];
1777 if (hlist_empty(hhd)) {
1793 if (WARN_ON_ONCE(!hnd))
1796 iter->probe = hlist_entry(hnd, struct ftrace_func_probe, node);
1801 static void *t_hash_start(struct seq_file *m, loff_t *pos)
1803 struct ftrace_iterator *iter = m->private;
1807 if (iter->func_pos > *pos)
1811 for (l = 0; l <= (*pos - iter->func_pos); ) {
1812 p = t_hash_next(m, &l);
1819 /* Only set this if we have an item */
1820 iter->flags |= FTRACE_ITER_HASH;
1826 t_hash_show(struct seq_file *m, struct ftrace_iterator *iter)
1828 struct ftrace_func_probe *rec;
1831 if (WARN_ON_ONCE(!rec))
1834 if (rec->ops->print)
1835 return rec->ops->print(m, rec->ip, rec->ops, rec->data);
1837 seq_printf(m, "%ps:%ps", (void *)rec->ip, (void *)rec->ops->func);
1840 seq_printf(m, ":%p", rec->data);
1847 t_next(struct seq_file *m, void *v, loff_t *pos)
1849 struct ftrace_iterator *iter = m->private;
1850 struct ftrace_ops *ops = &global_ops;
1851 struct dyn_ftrace *rec = NULL;
1853 if (unlikely(ftrace_disabled))
1856 if (iter->flags & FTRACE_ITER_HASH)
1857 return t_hash_next(m, pos);
1860 iter->pos = iter->func_pos = *pos;
1862 if (iter->flags & FTRACE_ITER_PRINTALL)
1863 return t_hash_start(m, pos);
1866 if (iter->idx >= iter->pg->index) {
1867 if (iter->pg->next) {
1868 iter->pg = iter->pg->next;
1873 rec = &iter->pg->records[iter->idx++];
1874 if ((rec->flags & FTRACE_FL_FREE) ||
1876 ((iter->flags & FTRACE_ITER_FILTER) &&
1877 !(ftrace_lookup_ip(ops->filter_hash, rec->ip))) ||
1879 ((iter->flags & FTRACE_ITER_NOTRACE) &&
1880 !ftrace_lookup_ip(ops->notrace_hash, rec->ip)) ||
1882 ((iter->flags & FTRACE_ITER_ENABLED) &&
1883 !(rec->flags & ~FTRACE_FL_MASK))) {
1891 return t_hash_start(m, pos);
1898 static void reset_iter_read(struct ftrace_iterator *iter)
1902 iter->flags &= ~(FTRACE_ITER_PRINTALL & FTRACE_ITER_HASH);
1905 static void *t_start(struct seq_file *m, loff_t *pos)
1907 struct ftrace_iterator *iter = m->private;
1908 struct ftrace_ops *ops = &global_ops;
1912 mutex_lock(&ftrace_lock);
1914 if (unlikely(ftrace_disabled))
1918 * If an lseek was done, then reset and start from beginning.
1920 if (*pos < iter->pos)
1921 reset_iter_read(iter);
1924 * For set_ftrace_filter reading, if we have the filter
1925 * off, we can short cut and just print out that all
1926 * functions are enabled.
1928 if (iter->flags & FTRACE_ITER_FILTER && !ops->filter_hash->count) {
1930 return t_hash_start(m, pos);
1931 iter->flags |= FTRACE_ITER_PRINTALL;
1932 /* reset in case of seek/pread */
1933 iter->flags &= ~FTRACE_ITER_HASH;
1937 if (iter->flags & FTRACE_ITER_HASH)
1938 return t_hash_start(m, pos);
1941 * Unfortunately, we need to restart at ftrace_pages_start
1942 * every time we let go of the ftrace_mutex. This is because
1943 * those pointers can change without the lock.
1945 iter->pg = ftrace_pages_start;
1947 for (l = 0; l <= *pos; ) {
1948 p = t_next(m, p, &l);
1954 if (iter->flags & FTRACE_ITER_FILTER)
1955 return t_hash_start(m, pos);
1963 static void t_stop(struct seq_file *m, void *p)
1965 mutex_unlock(&ftrace_lock);
1968 static int t_show(struct seq_file *m, void *v)
1970 struct ftrace_iterator *iter = m->private;
1971 struct dyn_ftrace *rec;
1973 if (iter->flags & FTRACE_ITER_HASH)
1974 return t_hash_show(m, iter);
1976 if (iter->flags & FTRACE_ITER_PRINTALL) {
1977 seq_printf(m, "#### all functions enabled ####\n");
1986 seq_printf(m, "%ps", (void *)rec->ip);
1987 if (iter->flags & FTRACE_ITER_ENABLED)
1988 seq_printf(m, " (%ld)",
1989 rec->flags & ~FTRACE_FL_MASK);
1990 seq_printf(m, "\n");
1995 static const struct seq_operations show_ftrace_seq_ops = {
2003 ftrace_avail_open(struct inode *inode, struct file *file)
2005 struct ftrace_iterator *iter;
2008 if (unlikely(ftrace_disabled))
2011 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
2015 iter->pg = ftrace_pages_start;
2017 ret = seq_open(file, &show_ftrace_seq_ops);
2019 struct seq_file *m = file->private_data;
2030 ftrace_enabled_open(struct inode *inode, struct file *file)
2032 struct ftrace_iterator *iter;
2035 if (unlikely(ftrace_disabled))
2038 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
2042 iter->pg = ftrace_pages_start;
2043 iter->flags = FTRACE_ITER_ENABLED;
2045 ret = seq_open(file, &show_ftrace_seq_ops);
2047 struct seq_file *m = file->private_data;
2057 static void ftrace_filter_reset(struct ftrace_hash *hash)
2059 mutex_lock(&ftrace_lock);
2060 ftrace_hash_clear(hash);
2061 mutex_unlock(&ftrace_lock);
2065 ftrace_regex_open(struct ftrace_ops *ops, int flag,
2066 struct inode *inode, struct file *file)
2068 struct ftrace_iterator *iter;
2069 struct ftrace_hash *hash;
2072 if (unlikely(ftrace_disabled))
2075 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
2079 if (trace_parser_get_init(&iter->parser, FTRACE_BUFF_MAX)) {
2084 if (flag & FTRACE_ITER_NOTRACE)
2085 hash = ops->notrace_hash;
2087 hash = ops->filter_hash;
2092 if (file->f_mode & FMODE_WRITE) {
2093 mutex_lock(&ftrace_lock);
2094 iter->hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, hash);
2095 mutex_unlock(&ftrace_lock);
2098 trace_parser_put(&iter->parser);
2104 mutex_lock(&ftrace_regex_lock);
2106 if ((file->f_mode & FMODE_WRITE) &&
2107 (file->f_flags & O_TRUNC))
2108 ftrace_filter_reset(iter->hash);
2110 if (file->f_mode & FMODE_READ) {
2111 iter->pg = ftrace_pages_start;
2113 ret = seq_open(file, &show_ftrace_seq_ops);
2115 struct seq_file *m = file->private_data;
2119 free_ftrace_hash(iter->hash);
2120 trace_parser_put(&iter->parser);
2124 file->private_data = iter;
2125 mutex_unlock(&ftrace_regex_lock);
2131 ftrace_filter_open(struct inode *inode, struct file *file)
2133 return ftrace_regex_open(&global_ops, FTRACE_ITER_FILTER,
2138 ftrace_notrace_open(struct inode *inode, struct file *file)
2140 return ftrace_regex_open(&global_ops, FTRACE_ITER_NOTRACE,
2145 ftrace_regex_lseek(struct file *file, loff_t offset, int origin)
2149 if (file->f_mode & FMODE_READ)
2150 ret = seq_lseek(file, offset, origin);
2152 file->f_pos = ret = 1;
2157 static int ftrace_match(char *str, char *regex, int len, int type)
2164 if (strcmp(str, regex) == 0)
2167 case MATCH_FRONT_ONLY:
2168 if (strncmp(str, regex, len) == 0)
2171 case MATCH_MIDDLE_ONLY:
2172 if (strstr(str, regex))
2175 case MATCH_END_ONLY:
2177 if (slen >= len && memcmp(str + slen - len, regex, len) == 0)
2186 enter_record(struct ftrace_hash *hash, struct dyn_ftrace *rec, int not)
2188 struct ftrace_func_entry *entry;
2191 entry = ftrace_lookup_ip(hash, rec->ip);
2193 /* Do nothing if it doesn't exist */
2197 free_hash_entry(hash, entry);
2199 /* Do nothing if it exists */
2203 ret = add_hash_entry(hash, rec->ip);
2209 ftrace_match_record(struct dyn_ftrace *rec, char *mod,
2210 char *regex, int len, int type)
2212 char str[KSYM_SYMBOL_LEN];
2215 kallsyms_lookup(rec->ip, NULL, NULL, &modname, str);
2218 /* module lookup requires matching the module */
2219 if (!modname || strcmp(modname, mod))
2222 /* blank search means to match all funcs in the mod */
2227 return ftrace_match(str, regex, len, type);
2231 match_records(struct ftrace_hash *hash, char *buff,
2232 int len, char *mod, int not)
2234 unsigned search_len = 0;
2235 struct ftrace_page *pg;
2236 struct dyn_ftrace *rec;
2237 int type = MATCH_FULL;
2238 char *search = buff;
2243 type = filter_parse_regex(buff, len, &search, ¬);
2244 search_len = strlen(search);
2247 mutex_lock(&ftrace_lock);
2249 if (unlikely(ftrace_disabled))
2252 do_for_each_ftrace_rec(pg, rec) {
2254 if (ftrace_match_record(rec, mod, search, search_len, type)) {
2255 ret = enter_record(hash, rec, not);
2262 } while_for_each_ftrace_rec();
2264 mutex_unlock(&ftrace_lock);
2270 ftrace_match_records(struct ftrace_hash *hash, char *buff, int len)
2272 return match_records(hash, buff, len, NULL, 0);
2276 ftrace_match_module_records(struct ftrace_hash *hash, char *buff, char *mod)
2280 /* blank or '*' mean the same */
2281 if (strcmp(buff, "*") == 0)
2284 /* handle the case of 'dont filter this module' */
2285 if (strcmp(buff, "!") == 0 || strcmp(buff, "!*") == 0) {
2290 return match_records(hash, buff, strlen(buff), mod, not);
2294 * We register the module command as a template to show others how
2295 * to register the a command as well.
2299 ftrace_mod_callback(char *func, char *cmd, char *param, int enable)
2301 struct ftrace_ops *ops = &global_ops;
2302 struct ftrace_hash *hash;
2307 * cmd == 'mod' because we only registered this func
2308 * for the 'mod' ftrace_func_command.
2309 * But if you register one func with multiple commands,
2310 * you can tell which command was used by the cmd
2314 /* we must have a module name */
2318 mod = strsep(¶m, ":");
2323 hash = ops->filter_hash;
2325 hash = ops->notrace_hash;
2327 ret = ftrace_match_module_records(hash, func, mod);
2336 static struct ftrace_func_command ftrace_mod_cmd = {
2338 .func = ftrace_mod_callback,
2341 static int __init ftrace_mod_cmd_init(void)
2343 return register_ftrace_command(&ftrace_mod_cmd);
2345 device_initcall(ftrace_mod_cmd_init);
2348 function_trace_probe_call(unsigned long ip, unsigned long parent_ip)
2350 struct ftrace_func_probe *entry;
2351 struct hlist_head *hhd;
2352 struct hlist_node *n;
2355 key = hash_long(ip, FTRACE_HASH_BITS);
2357 hhd = &ftrace_func_hash[key];
2359 if (hlist_empty(hhd))
2363 * Disable preemption for these calls to prevent a RCU grace
2364 * period. This syncs the hash iteration and freeing of items
2365 * on the hash. rcu_read_lock is too dangerous here.
2367 preempt_disable_notrace();
2368 hlist_for_each_entry_rcu(entry, n, hhd, node) {
2369 if (entry->ip == ip)
2370 entry->ops->func(ip, parent_ip, &entry->data);
2372 preempt_enable_notrace();
2375 static struct ftrace_ops trace_probe_ops __read_mostly =
2377 .func = function_trace_probe_call,
2380 static int ftrace_probe_registered;
2382 static void __enable_ftrace_function_probe(void)
2386 if (ftrace_probe_registered)
2389 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
2390 struct hlist_head *hhd = &ftrace_func_hash[i];
2394 /* Nothing registered? */
2395 if (i == FTRACE_FUNC_HASHSIZE)
2398 __register_ftrace_function(&trace_probe_ops);
2399 ftrace_startup(&global_ops, 0);
2400 ftrace_probe_registered = 1;
2403 static void __disable_ftrace_function_probe(void)
2407 if (!ftrace_probe_registered)
2410 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
2411 struct hlist_head *hhd = &ftrace_func_hash[i];
2416 /* no more funcs left */
2417 __unregister_ftrace_function(&trace_probe_ops);
2418 ftrace_shutdown(&global_ops, 0);
2419 ftrace_probe_registered = 0;
2423 static void ftrace_free_entry_rcu(struct rcu_head *rhp)
2425 struct ftrace_func_probe *entry =
2426 container_of(rhp, struct ftrace_func_probe, rcu);
2428 if (entry->ops->free)
2429 entry->ops->free(&entry->data);
2435 register_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
2438 struct ftrace_func_probe *entry;
2439 struct ftrace_page *pg;
2440 struct dyn_ftrace *rec;
2446 type = filter_parse_regex(glob, strlen(glob), &search, ¬);
2447 len = strlen(search);
2449 /* we do not support '!' for function probes */
2453 mutex_lock(&ftrace_lock);
2455 if (unlikely(ftrace_disabled))
2458 do_for_each_ftrace_rec(pg, rec) {
2460 if (!ftrace_match_record(rec, NULL, search, len, type))
2463 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
2465 /* If we did not process any, then return error */
2476 * The caller might want to do something special
2477 * for each function we find. We call the callback
2478 * to give the caller an opportunity to do so.
2480 if (ops->callback) {
2481 if (ops->callback(rec->ip, &entry->data) < 0) {
2482 /* caller does not like this func */
2489 entry->ip = rec->ip;
2491 key = hash_long(entry->ip, FTRACE_HASH_BITS);
2492 hlist_add_head_rcu(&entry->node, &ftrace_func_hash[key]);
2494 } while_for_each_ftrace_rec();
2495 __enable_ftrace_function_probe();
2498 mutex_unlock(&ftrace_lock);
2504 PROBE_TEST_FUNC = 1,
2509 __unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
2510 void *data, int flags)
2512 struct ftrace_func_probe *entry;
2513 struct hlist_node *n, *tmp;
2514 char str[KSYM_SYMBOL_LEN];
2515 int type = MATCH_FULL;
2519 if (glob && (strcmp(glob, "*") == 0 || !strlen(glob)))
2524 type = filter_parse_regex(glob, strlen(glob), &search, ¬);
2525 len = strlen(search);
2527 /* we do not support '!' for function probes */
2532 mutex_lock(&ftrace_lock);
2533 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
2534 struct hlist_head *hhd = &ftrace_func_hash[i];
2536 hlist_for_each_entry_safe(entry, n, tmp, hhd, node) {
2538 /* break up if statements for readability */
2539 if ((flags & PROBE_TEST_FUNC) && entry->ops != ops)
2542 if ((flags & PROBE_TEST_DATA) && entry->data != data)
2545 /* do this last, since it is the most expensive */
2547 kallsyms_lookup(entry->ip, NULL, NULL,
2549 if (!ftrace_match(str, glob, len, type))
2553 hlist_del(&entry->node);
2554 call_rcu(&entry->rcu, ftrace_free_entry_rcu);
2557 __disable_ftrace_function_probe();
2558 mutex_unlock(&ftrace_lock);
2562 unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
2565 __unregister_ftrace_function_probe(glob, ops, data,
2566 PROBE_TEST_FUNC | PROBE_TEST_DATA);
2570 unregister_ftrace_function_probe_func(char *glob, struct ftrace_probe_ops *ops)
2572 __unregister_ftrace_function_probe(glob, ops, NULL, PROBE_TEST_FUNC);
2575 void unregister_ftrace_function_probe_all(char *glob)
2577 __unregister_ftrace_function_probe(glob, NULL, NULL, 0);
2580 static LIST_HEAD(ftrace_commands);
2581 static DEFINE_MUTEX(ftrace_cmd_mutex);
2583 int register_ftrace_command(struct ftrace_func_command *cmd)
2585 struct ftrace_func_command *p;
2588 mutex_lock(&ftrace_cmd_mutex);
2589 list_for_each_entry(p, &ftrace_commands, list) {
2590 if (strcmp(cmd->name, p->name) == 0) {
2595 list_add(&cmd->list, &ftrace_commands);
2597 mutex_unlock(&ftrace_cmd_mutex);
2602 int unregister_ftrace_command(struct ftrace_func_command *cmd)
2604 struct ftrace_func_command *p, *n;
2607 mutex_lock(&ftrace_cmd_mutex);
2608 list_for_each_entry_safe(p, n, &ftrace_commands, list) {
2609 if (strcmp(cmd->name, p->name) == 0) {
2611 list_del_init(&p->list);
2616 mutex_unlock(&ftrace_cmd_mutex);
2621 static int ftrace_process_regex(struct ftrace_hash *hash,
2622 char *buff, int len, int enable)
2624 char *func, *command, *next = buff;
2625 struct ftrace_func_command *p;
2628 func = strsep(&next, ":");
2631 ret = ftrace_match_records(hash, func, len);
2641 command = strsep(&next, ":");
2643 mutex_lock(&ftrace_cmd_mutex);
2644 list_for_each_entry(p, &ftrace_commands, list) {
2645 if (strcmp(p->name, command) == 0) {
2646 ret = p->func(func, command, next, enable);
2651 mutex_unlock(&ftrace_cmd_mutex);
2657 ftrace_regex_write(struct file *file, const char __user *ubuf,
2658 size_t cnt, loff_t *ppos, int enable)
2660 struct ftrace_iterator *iter;
2661 struct trace_parser *parser;
2667 mutex_lock(&ftrace_regex_lock);
2670 if (unlikely(ftrace_disabled))
2673 if (file->f_mode & FMODE_READ) {
2674 struct seq_file *m = file->private_data;
2677 iter = file->private_data;
2679 parser = &iter->parser;
2680 read = trace_get_user(parser, ubuf, cnt, ppos);
2682 if (read >= 0 && trace_parser_loaded(parser) &&
2683 !trace_parser_cont(parser)) {
2684 ret = ftrace_process_regex(iter->hash, parser->buffer,
2685 parser->idx, enable);
2686 trace_parser_clear(parser);
2693 mutex_unlock(&ftrace_regex_lock);
2699 ftrace_filter_write(struct file *file, const char __user *ubuf,
2700 size_t cnt, loff_t *ppos)
2702 return ftrace_regex_write(file, ubuf, cnt, ppos, 1);
2706 ftrace_notrace_write(struct file *file, const char __user *ubuf,
2707 size_t cnt, loff_t *ppos)
2709 return ftrace_regex_write(file, ubuf, cnt, ppos, 0);
2713 ftrace_set_regex(struct ftrace_ops *ops, unsigned char *buf, int len,
2714 int reset, int enable)
2716 struct ftrace_hash **orig_hash;
2717 struct ftrace_hash *hash;
2720 if (unlikely(ftrace_disabled))
2724 orig_hash = &ops->filter_hash;
2726 orig_hash = &ops->notrace_hash;
2728 hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, *orig_hash);
2732 mutex_lock(&ftrace_regex_lock);
2734 ftrace_filter_reset(hash);
2736 ftrace_match_records(hash, buf, len);
2738 mutex_lock(&ftrace_lock);
2739 ret = ftrace_hash_move(orig_hash, hash);
2740 mutex_unlock(&ftrace_lock);
2742 mutex_unlock(&ftrace_regex_lock);
2744 free_ftrace_hash(hash);
2749 * ftrace_set_filter - set a function to filter on in ftrace
2750 * @buf - the string that holds the function filter text.
2751 * @len - the length of the string.
2752 * @reset - non zero to reset all filters before applying this filter.
2754 * Filters denote which functions should be enabled when tracing is enabled.
2755 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
2757 void ftrace_set_filter(unsigned char *buf, int len, int reset)
2759 ftrace_set_regex(&global_ops, buf, len, reset, 1);
2763 * ftrace_set_notrace - set a function to not trace in ftrace
2764 * @buf - the string that holds the function notrace text.
2765 * @len - the length of the string.
2766 * @reset - non zero to reset all filters before applying this filter.
2768 * Notrace Filters denote which functions should not be enabled when tracing
2769 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
2772 void ftrace_set_notrace(unsigned char *buf, int len, int reset)
2774 ftrace_set_regex(&global_ops, buf, len, reset, 0);
2778 * command line interface to allow users to set filters on boot up.
2780 #define FTRACE_FILTER_SIZE COMMAND_LINE_SIZE
2781 static char ftrace_notrace_buf[FTRACE_FILTER_SIZE] __initdata;
2782 static char ftrace_filter_buf[FTRACE_FILTER_SIZE] __initdata;
2784 static int __init set_ftrace_notrace(char *str)
2786 strncpy(ftrace_notrace_buf, str, FTRACE_FILTER_SIZE);
2789 __setup("ftrace_notrace=", set_ftrace_notrace);
2791 static int __init set_ftrace_filter(char *str)
2793 strncpy(ftrace_filter_buf, str, FTRACE_FILTER_SIZE);
2796 __setup("ftrace_filter=", set_ftrace_filter);
2798 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
2799 static char ftrace_graph_buf[FTRACE_FILTER_SIZE] __initdata;
2800 static int ftrace_set_func(unsigned long *array, int *idx, char *buffer);
2802 static int __init set_graph_function(char *str)
2804 strlcpy(ftrace_graph_buf, str, FTRACE_FILTER_SIZE);
2807 __setup("ftrace_graph_filter=", set_graph_function);
2809 static void __init set_ftrace_early_graph(char *buf)
2815 func = strsep(&buf, ",");
2816 /* we allow only one expression at a time */
2817 ret = ftrace_set_func(ftrace_graph_funcs, &ftrace_graph_count,
2820 printk(KERN_DEBUG "ftrace: function %s not "
2821 "traceable\n", func);
2824 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
2827 set_ftrace_early_filter(struct ftrace_ops *ops, char *buf, int enable)
2832 func = strsep(&buf, ",");
2833 ftrace_set_regex(ops, func, strlen(func), 0, enable);
2837 static void __init set_ftrace_early_filters(void)
2839 if (ftrace_filter_buf[0])
2840 set_ftrace_early_filter(&global_ops, ftrace_filter_buf, 1);
2841 if (ftrace_notrace_buf[0])
2842 set_ftrace_early_filter(&global_ops, ftrace_notrace_buf, 0);
2843 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
2844 if (ftrace_graph_buf[0])
2845 set_ftrace_early_graph(ftrace_graph_buf);
2846 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
2850 ftrace_regex_release(struct inode *inode, struct file *file)
2852 struct seq_file *m = (struct seq_file *)file->private_data;
2853 struct ftrace_iterator *iter;
2854 struct ftrace_hash **orig_hash;
2855 struct trace_parser *parser;
2859 mutex_lock(&ftrace_regex_lock);
2860 if (file->f_mode & FMODE_READ) {
2863 seq_release(inode, file);
2865 iter = file->private_data;
2867 parser = &iter->parser;
2868 if (trace_parser_loaded(parser)) {
2869 parser->buffer[parser->idx] = 0;
2870 ftrace_match_records(iter->hash, parser->buffer, parser->idx);
2873 trace_parser_put(parser);
2875 if (file->f_mode & FMODE_WRITE) {
2876 filter_hash = !!(iter->flags & FTRACE_ITER_FILTER);
2879 orig_hash = &iter->ops->filter_hash;
2881 orig_hash = &iter->ops->notrace_hash;
2883 mutex_lock(&ftrace_lock);
2885 * Remove the current set, update the hash and add
2888 ftrace_hash_rec_disable(iter->ops, filter_hash);
2889 ret = ftrace_hash_move(orig_hash, iter->hash);
2891 ftrace_hash_rec_enable(iter->ops, filter_hash);
2892 if (iter->ops->flags & FTRACE_OPS_FL_ENABLED
2894 ftrace_run_update_code(FTRACE_ENABLE_CALLS);
2896 mutex_unlock(&ftrace_lock);
2898 free_ftrace_hash(iter->hash);
2901 mutex_unlock(&ftrace_regex_lock);
2905 static const struct file_operations ftrace_avail_fops = {
2906 .open = ftrace_avail_open,
2908 .llseek = seq_lseek,
2909 .release = seq_release_private,
2912 static const struct file_operations ftrace_enabled_fops = {
2913 .open = ftrace_enabled_open,
2915 .llseek = seq_lseek,
2916 .release = seq_release_private,
2919 static const struct file_operations ftrace_filter_fops = {
2920 .open = ftrace_filter_open,
2922 .write = ftrace_filter_write,
2923 .llseek = ftrace_regex_lseek,
2924 .release = ftrace_regex_release,
2927 static const struct file_operations ftrace_notrace_fops = {
2928 .open = ftrace_notrace_open,
2930 .write = ftrace_notrace_write,
2931 .llseek = ftrace_regex_lseek,
2932 .release = ftrace_regex_release,
2935 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
2937 static DEFINE_MUTEX(graph_lock);
2939 int ftrace_graph_count;
2940 int ftrace_graph_filter_enabled;
2941 unsigned long ftrace_graph_funcs[FTRACE_GRAPH_MAX_FUNCS] __read_mostly;
2944 __g_next(struct seq_file *m, loff_t *pos)
2946 if (*pos >= ftrace_graph_count)
2948 return &ftrace_graph_funcs[*pos];
2952 g_next(struct seq_file *m, void *v, loff_t *pos)
2955 return __g_next(m, pos);
2958 static void *g_start(struct seq_file *m, loff_t *pos)
2960 mutex_lock(&graph_lock);
2962 /* Nothing, tell g_show to print all functions are enabled */
2963 if (!ftrace_graph_filter_enabled && !*pos)
2966 return __g_next(m, pos);
2969 static void g_stop(struct seq_file *m, void *p)
2971 mutex_unlock(&graph_lock);
2974 static int g_show(struct seq_file *m, void *v)
2976 unsigned long *ptr = v;
2981 if (ptr == (unsigned long *)1) {
2982 seq_printf(m, "#### all functions enabled ####\n");
2986 seq_printf(m, "%ps\n", (void *)*ptr);
2991 static const struct seq_operations ftrace_graph_seq_ops = {
2999 ftrace_graph_open(struct inode *inode, struct file *file)
3003 if (unlikely(ftrace_disabled))
3006 mutex_lock(&graph_lock);
3007 if ((file->f_mode & FMODE_WRITE) &&
3008 (file->f_flags & O_TRUNC)) {
3009 ftrace_graph_filter_enabled = 0;
3010 ftrace_graph_count = 0;
3011 memset(ftrace_graph_funcs, 0, sizeof(ftrace_graph_funcs));
3013 mutex_unlock(&graph_lock);
3015 if (file->f_mode & FMODE_READ)
3016 ret = seq_open(file, &ftrace_graph_seq_ops);
3022 ftrace_graph_release(struct inode *inode, struct file *file)
3024 if (file->f_mode & FMODE_READ)
3025 seq_release(inode, file);
3030 ftrace_set_func(unsigned long *array, int *idx, char *buffer)
3032 struct dyn_ftrace *rec;
3033 struct ftrace_page *pg;
3042 type = filter_parse_regex(buffer, strlen(buffer), &search, ¬);
3043 if (!not && *idx >= FTRACE_GRAPH_MAX_FUNCS)
3046 search_len = strlen(search);
3048 mutex_lock(&ftrace_lock);
3050 if (unlikely(ftrace_disabled)) {
3051 mutex_unlock(&ftrace_lock);
3055 do_for_each_ftrace_rec(pg, rec) {
3057 if (rec->flags & FTRACE_FL_FREE)
3060 if (ftrace_match_record(rec, NULL, search, search_len, type)) {
3061 /* if it is in the array */
3063 for (i = 0; i < *idx; i++) {
3064 if (array[i] == rec->ip) {
3073 array[(*idx)++] = rec->ip;
3074 if (*idx >= FTRACE_GRAPH_MAX_FUNCS)
3079 array[i] = array[--(*idx)];
3085 } while_for_each_ftrace_rec();
3087 mutex_unlock(&ftrace_lock);
3092 ftrace_graph_filter_enabled = 1;
3097 ftrace_graph_write(struct file *file, const char __user *ubuf,
3098 size_t cnt, loff_t *ppos)
3100 struct trace_parser parser;
3106 mutex_lock(&graph_lock);
3108 if (trace_parser_get_init(&parser, FTRACE_BUFF_MAX)) {
3113 read = trace_get_user(&parser, ubuf, cnt, ppos);
3115 if (read >= 0 && trace_parser_loaded((&parser))) {
3116 parser.buffer[parser.idx] = 0;
3118 /* we allow only one expression at a time */
3119 ret = ftrace_set_func(ftrace_graph_funcs, &ftrace_graph_count,
3128 trace_parser_put(&parser);
3130 mutex_unlock(&graph_lock);
3135 static const struct file_operations ftrace_graph_fops = {
3136 .open = ftrace_graph_open,
3138 .write = ftrace_graph_write,
3139 .release = ftrace_graph_release,
3140 .llseek = seq_lseek,
3142 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
3144 static __init int ftrace_init_dyn_debugfs(struct dentry *d_tracer)
3147 trace_create_file("available_filter_functions", 0444,
3148 d_tracer, NULL, &ftrace_avail_fops);
3150 trace_create_file("enabled_functions", 0444,
3151 d_tracer, NULL, &ftrace_enabled_fops);
3153 trace_create_file("set_ftrace_filter", 0644, d_tracer,
3154 NULL, &ftrace_filter_fops);
3156 trace_create_file("set_ftrace_notrace", 0644, d_tracer,
3157 NULL, &ftrace_notrace_fops);
3159 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
3160 trace_create_file("set_graph_function", 0444, d_tracer,
3162 &ftrace_graph_fops);
3163 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
3168 static int ftrace_process_locs(struct module *mod,
3169 unsigned long *start,
3175 mutex_lock(&ftrace_lock);
3178 addr = ftrace_call_adjust(*p++);
3180 * Some architecture linkers will pad between
3181 * the different mcount_loc sections of different
3182 * object files to satisfy alignments.
3183 * Skip any NULL pointers.
3187 ftrace_record_ip(addr);
3190 ftrace_update_code(mod);
3191 mutex_unlock(&ftrace_lock);
3196 #ifdef CONFIG_MODULES
3197 void ftrace_release_mod(struct module *mod)
3199 struct dyn_ftrace *rec;
3200 struct ftrace_page *pg;
3202 mutex_lock(&ftrace_lock);
3204 if (ftrace_disabled)
3207 do_for_each_ftrace_rec(pg, rec) {
3208 if (within_module_core(rec->ip, mod)) {
3210 * rec->ip is changed in ftrace_free_rec()
3211 * It should not between s and e if record was freed.
3213 FTRACE_WARN_ON(rec->flags & FTRACE_FL_FREE);
3214 ftrace_free_rec(rec);
3216 } while_for_each_ftrace_rec();
3218 mutex_unlock(&ftrace_lock);
3221 static void ftrace_init_module(struct module *mod,
3222 unsigned long *start, unsigned long *end)
3224 if (ftrace_disabled || start == end)
3226 ftrace_process_locs(mod, start, end);
3229 static int ftrace_module_notify(struct notifier_block *self,
3230 unsigned long val, void *data)
3232 struct module *mod = data;
3235 case MODULE_STATE_COMING:
3236 ftrace_init_module(mod, mod->ftrace_callsites,
3237 mod->ftrace_callsites +
3238 mod->num_ftrace_callsites);
3240 case MODULE_STATE_GOING:
3241 ftrace_release_mod(mod);
3248 static int ftrace_module_notify(struct notifier_block *self,
3249 unsigned long val, void *data)
3253 #endif /* CONFIG_MODULES */
3255 struct notifier_block ftrace_module_nb = {
3256 .notifier_call = ftrace_module_notify,
3260 extern unsigned long __start_mcount_loc[];
3261 extern unsigned long __stop_mcount_loc[];
3263 void __init ftrace_init(void)
3265 unsigned long count, addr, flags;
3268 /* Keep the ftrace pointer to the stub */
3269 addr = (unsigned long)ftrace_stub;
3271 local_irq_save(flags);
3272 ftrace_dyn_arch_init(&addr);
3273 local_irq_restore(flags);
3275 /* ftrace_dyn_arch_init places the return code in addr */
3279 count = __stop_mcount_loc - __start_mcount_loc;
3281 ret = ftrace_dyn_table_alloc(count);
3285 last_ftrace_enabled = ftrace_enabled = 1;
3287 ret = ftrace_process_locs(NULL,
3291 ret = register_module_notifier(&ftrace_module_nb);
3293 pr_warning("Failed to register trace ftrace module notifier\n");
3295 set_ftrace_early_filters();
3299 ftrace_disabled = 1;
3304 static struct ftrace_ops global_ops = {
3305 .func = ftrace_stub,
3308 static int __init ftrace_nodyn_init(void)
3313 device_initcall(ftrace_nodyn_init);
3315 static inline int ftrace_init_dyn_debugfs(struct dentry *d_tracer) { return 0; }
3316 static inline void ftrace_startup_enable(int command) { }
3317 /* Keep as macros so we do not need to define the commands */
3318 # define ftrace_startup(ops, command) do { } while (0)
3319 # define ftrace_shutdown(ops, command) do { } while (0)
3320 # define ftrace_startup_sysctl() do { } while (0)
3321 # define ftrace_shutdown_sysctl() do { } while (0)
3322 #endif /* CONFIG_DYNAMIC_FTRACE */
3324 static void clear_ftrace_swapper(void)
3326 struct task_struct *p;
3330 for_each_online_cpu(cpu) {
3332 clear_tsk_trace_trace(p);
3337 static void set_ftrace_swapper(void)
3339 struct task_struct *p;
3343 for_each_online_cpu(cpu) {
3345 set_tsk_trace_trace(p);
3350 static void clear_ftrace_pid(struct pid *pid)
3352 struct task_struct *p;
3355 do_each_pid_task(pid, PIDTYPE_PID, p) {
3356 clear_tsk_trace_trace(p);
3357 } while_each_pid_task(pid, PIDTYPE_PID, p);
3363 static void set_ftrace_pid(struct pid *pid)
3365 struct task_struct *p;
3368 do_each_pid_task(pid, PIDTYPE_PID, p) {
3369 set_tsk_trace_trace(p);
3370 } while_each_pid_task(pid, PIDTYPE_PID, p);
3374 static void clear_ftrace_pid_task(struct pid *pid)
3376 if (pid == ftrace_swapper_pid)
3377 clear_ftrace_swapper();
3379 clear_ftrace_pid(pid);
3382 static void set_ftrace_pid_task(struct pid *pid)
3384 if (pid == ftrace_swapper_pid)
3385 set_ftrace_swapper();
3387 set_ftrace_pid(pid);
3390 static int ftrace_pid_add(int p)
3393 struct ftrace_pid *fpid;
3396 mutex_lock(&ftrace_lock);
3399 pid = ftrace_swapper_pid;
3401 pid = find_get_pid(p);
3408 list_for_each_entry(fpid, &ftrace_pids, list)
3409 if (fpid->pid == pid)
3414 fpid = kmalloc(sizeof(*fpid), GFP_KERNEL);
3418 list_add(&fpid->list, &ftrace_pids);
3421 set_ftrace_pid_task(pid);
3423 ftrace_update_pid_func();
3424 ftrace_startup_enable(0);
3426 mutex_unlock(&ftrace_lock);
3430 if (pid != ftrace_swapper_pid)
3434 mutex_unlock(&ftrace_lock);
3438 static void ftrace_pid_reset(void)
3440 struct ftrace_pid *fpid, *safe;
3442 mutex_lock(&ftrace_lock);
3443 list_for_each_entry_safe(fpid, safe, &ftrace_pids, list) {
3444 struct pid *pid = fpid->pid;
3446 clear_ftrace_pid_task(pid);
3448 list_del(&fpid->list);
3452 ftrace_update_pid_func();
3453 ftrace_startup_enable(0);
3455 mutex_unlock(&ftrace_lock);
3458 static void *fpid_start(struct seq_file *m, loff_t *pos)
3460 mutex_lock(&ftrace_lock);
3462 if (list_empty(&ftrace_pids) && (!*pos))
3465 return seq_list_start(&ftrace_pids, *pos);
3468 static void *fpid_next(struct seq_file *m, void *v, loff_t *pos)
3473 return seq_list_next(v, &ftrace_pids, pos);
3476 static void fpid_stop(struct seq_file *m, void *p)
3478 mutex_unlock(&ftrace_lock);
3481 static int fpid_show(struct seq_file *m, void *v)
3483 const struct ftrace_pid *fpid = list_entry(v, struct ftrace_pid, list);
3485 if (v == (void *)1) {
3486 seq_printf(m, "no pid\n");
3490 if (fpid->pid == ftrace_swapper_pid)
3491 seq_printf(m, "swapper tasks\n");
3493 seq_printf(m, "%u\n", pid_vnr(fpid->pid));
3498 static const struct seq_operations ftrace_pid_sops = {
3499 .start = fpid_start,
3506 ftrace_pid_open(struct inode *inode, struct file *file)
3510 if ((file->f_mode & FMODE_WRITE) &&
3511 (file->f_flags & O_TRUNC))
3514 if (file->f_mode & FMODE_READ)
3515 ret = seq_open(file, &ftrace_pid_sops);
3521 ftrace_pid_write(struct file *filp, const char __user *ubuf,
3522 size_t cnt, loff_t *ppos)
3528 if (cnt >= sizeof(buf))
3531 if (copy_from_user(&buf, ubuf, cnt))
3537 * Allow "echo > set_ftrace_pid" or "echo -n '' > set_ftrace_pid"
3538 * to clean the filter quietly.
3540 tmp = strstrip(buf);
3541 if (strlen(tmp) == 0)
3544 ret = strict_strtol(tmp, 10, &val);
3548 ret = ftrace_pid_add(val);
3550 return ret ? ret : cnt;
3554 ftrace_pid_release(struct inode *inode, struct file *file)
3556 if (file->f_mode & FMODE_READ)
3557 seq_release(inode, file);
3562 static const struct file_operations ftrace_pid_fops = {
3563 .open = ftrace_pid_open,
3564 .write = ftrace_pid_write,
3566 .llseek = seq_lseek,
3567 .release = ftrace_pid_release,
3570 static __init int ftrace_init_debugfs(void)
3572 struct dentry *d_tracer;
3574 d_tracer = tracing_init_dentry();
3578 ftrace_init_dyn_debugfs(d_tracer);
3580 trace_create_file("set_ftrace_pid", 0644, d_tracer,
3581 NULL, &ftrace_pid_fops);
3583 ftrace_profile_debugfs(d_tracer);
3587 fs_initcall(ftrace_init_debugfs);
3590 * ftrace_kill - kill ftrace
3592 * This function should be used by panic code. It stops ftrace
3593 * but in a not so nice way. If you need to simply kill ftrace
3594 * from a non-atomic section, use ftrace_kill.
3596 void ftrace_kill(void)
3598 ftrace_disabled = 1;
3600 clear_ftrace_function();
3604 * register_ftrace_function - register a function for profiling
3605 * @ops - ops structure that holds the function for profiling.
3607 * Register a function to be called by all functions in the
3610 * Note: @ops->func and all the functions it calls must be labeled
3611 * with "notrace", otherwise it will go into a
3614 int register_ftrace_function(struct ftrace_ops *ops)
3618 mutex_lock(&ftrace_lock);
3620 if (unlikely(ftrace_disabled))
3623 ret = __register_ftrace_function(ops);
3624 ftrace_startup(&global_ops, 0);
3627 mutex_unlock(&ftrace_lock);
3632 * unregister_ftrace_function - unregister a function for profiling.
3633 * @ops - ops structure that holds the function to unregister
3635 * Unregister a function that was added to be called by ftrace profiling.
3637 int unregister_ftrace_function(struct ftrace_ops *ops)
3641 mutex_lock(&ftrace_lock);
3642 ret = __unregister_ftrace_function(ops);
3643 ftrace_shutdown(&global_ops, 0);
3644 mutex_unlock(&ftrace_lock);
3650 ftrace_enable_sysctl(struct ctl_table *table, int write,
3651 void __user *buffer, size_t *lenp,
3656 mutex_lock(&ftrace_lock);
3658 if (unlikely(ftrace_disabled))
3661 ret = proc_dointvec(table, write, buffer, lenp, ppos);
3663 if (ret || !write || (last_ftrace_enabled == !!ftrace_enabled))
3666 last_ftrace_enabled = !!ftrace_enabled;
3668 if (ftrace_enabled) {
3670 ftrace_startup_sysctl();
3672 /* we are starting ftrace again */
3673 if (ftrace_list != &ftrace_list_end) {
3674 if (ftrace_list->next == &ftrace_list_end)
3675 ftrace_trace_function = ftrace_list->func;
3677 ftrace_trace_function = ftrace_list_func;
3681 /* stopping ftrace calls (just send to ftrace_stub) */
3682 ftrace_trace_function = ftrace_stub;
3684 ftrace_shutdown_sysctl();
3688 mutex_unlock(&ftrace_lock);
3692 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
3694 static int ftrace_graph_active;
3695 static struct notifier_block ftrace_suspend_notifier;
3697 int ftrace_graph_entry_stub(struct ftrace_graph_ent *trace)
3702 /* The callbacks that hook a function */
3703 trace_func_graph_ret_t ftrace_graph_return =
3704 (trace_func_graph_ret_t)ftrace_stub;
3705 trace_func_graph_ent_t ftrace_graph_entry = ftrace_graph_entry_stub;
3707 /* Try to assign a return stack array on FTRACE_RETSTACK_ALLOC_SIZE tasks. */
3708 static int alloc_retstack_tasklist(struct ftrace_ret_stack **ret_stack_list)
3712 unsigned long flags;
3713 int start = 0, end = FTRACE_RETSTACK_ALLOC_SIZE;
3714 struct task_struct *g, *t;
3716 for (i = 0; i < FTRACE_RETSTACK_ALLOC_SIZE; i++) {
3717 ret_stack_list[i] = kmalloc(FTRACE_RETFUNC_DEPTH
3718 * sizeof(struct ftrace_ret_stack),
3720 if (!ret_stack_list[i]) {
3728 read_lock_irqsave(&tasklist_lock, flags);
3729 do_each_thread(g, t) {
3735 if (t->ret_stack == NULL) {
3736 atomic_set(&t->tracing_graph_pause, 0);
3737 atomic_set(&t->trace_overrun, 0);
3738 t->curr_ret_stack = -1;
3739 /* Make sure the tasks see the -1 first: */
3741 t->ret_stack = ret_stack_list[start++];
3743 } while_each_thread(g, t);
3746 read_unlock_irqrestore(&tasklist_lock, flags);
3748 for (i = start; i < end; i++)
3749 kfree(ret_stack_list[i]);
3754 ftrace_graph_probe_sched_switch(void *ignore,
3755 struct task_struct *prev, struct task_struct *next)
3757 unsigned long long timestamp;
3761 * Does the user want to count the time a function was asleep.
3762 * If so, do not update the time stamps.
3764 if (trace_flags & TRACE_ITER_SLEEP_TIME)
3767 timestamp = trace_clock_local();
3769 prev->ftrace_timestamp = timestamp;
3771 /* only process tasks that we timestamped */
3772 if (!next->ftrace_timestamp)
3776 * Update all the counters in next to make up for the
3777 * time next was sleeping.
3779 timestamp -= next->ftrace_timestamp;
3781 for (index = next->curr_ret_stack; index >= 0; index--)
3782 next->ret_stack[index].calltime += timestamp;
3785 /* Allocate a return stack for each task */
3786 static int start_graph_tracing(void)
3788 struct ftrace_ret_stack **ret_stack_list;
3791 ret_stack_list = kmalloc(FTRACE_RETSTACK_ALLOC_SIZE *
3792 sizeof(struct ftrace_ret_stack *),
3795 if (!ret_stack_list)
3798 /* The cpu_boot init_task->ret_stack will never be freed */
3799 for_each_online_cpu(cpu) {
3800 if (!idle_task(cpu)->ret_stack)
3801 ftrace_graph_init_idle_task(idle_task(cpu), cpu);
3805 ret = alloc_retstack_tasklist(ret_stack_list);
3806 } while (ret == -EAGAIN);
3809 ret = register_trace_sched_switch(ftrace_graph_probe_sched_switch, NULL);
3811 pr_info("ftrace_graph: Couldn't activate tracepoint"
3812 " probe to kernel_sched_switch\n");
3815 kfree(ret_stack_list);
3820 * Hibernation protection.
3821 * The state of the current task is too much unstable during
3822 * suspend/restore to disk. We want to protect against that.
3825 ftrace_suspend_notifier_call(struct notifier_block *bl, unsigned long state,
3829 case PM_HIBERNATION_PREPARE:
3830 pause_graph_tracing();
3833 case PM_POST_HIBERNATION:
3834 unpause_graph_tracing();
3840 int register_ftrace_graph(trace_func_graph_ret_t retfunc,
3841 trace_func_graph_ent_t entryfunc)
3845 mutex_lock(&ftrace_lock);
3847 /* we currently allow only one tracer registered at a time */
3848 if (ftrace_graph_active) {
3853 ftrace_suspend_notifier.notifier_call = ftrace_suspend_notifier_call;
3854 register_pm_notifier(&ftrace_suspend_notifier);
3856 ftrace_graph_active++;
3857 ret = start_graph_tracing();
3859 ftrace_graph_active--;
3863 ftrace_graph_return = retfunc;
3864 ftrace_graph_entry = entryfunc;
3866 ftrace_startup(&global_ops, FTRACE_START_FUNC_RET);
3869 mutex_unlock(&ftrace_lock);
3873 void unregister_ftrace_graph(void)
3875 mutex_lock(&ftrace_lock);
3877 if (unlikely(!ftrace_graph_active))
3880 ftrace_graph_active--;
3881 ftrace_graph_return = (trace_func_graph_ret_t)ftrace_stub;
3882 ftrace_graph_entry = ftrace_graph_entry_stub;
3883 ftrace_shutdown(&global_ops, FTRACE_STOP_FUNC_RET);
3884 unregister_pm_notifier(&ftrace_suspend_notifier);
3885 unregister_trace_sched_switch(ftrace_graph_probe_sched_switch, NULL);
3888 mutex_unlock(&ftrace_lock);
3891 static DEFINE_PER_CPU(struct ftrace_ret_stack *, idle_ret_stack);
3894 graph_init_task(struct task_struct *t, struct ftrace_ret_stack *ret_stack)
3896 atomic_set(&t->tracing_graph_pause, 0);
3897 atomic_set(&t->trace_overrun, 0);
3898 t->ftrace_timestamp = 0;
3899 /* make curr_ret_stack visible before we add the ret_stack */
3901 t->ret_stack = ret_stack;
3905 * Allocate a return stack for the idle task. May be the first
3906 * time through, or it may be done by CPU hotplug online.
3908 void ftrace_graph_init_idle_task(struct task_struct *t, int cpu)
3910 t->curr_ret_stack = -1;
3912 * The idle task has no parent, it either has its own
3913 * stack or no stack at all.
3916 WARN_ON(t->ret_stack != per_cpu(idle_ret_stack, cpu));
3918 if (ftrace_graph_active) {
3919 struct ftrace_ret_stack *ret_stack;
3921 ret_stack = per_cpu(idle_ret_stack, cpu);
3923 ret_stack = kmalloc(FTRACE_RETFUNC_DEPTH
3924 * sizeof(struct ftrace_ret_stack),
3928 per_cpu(idle_ret_stack, cpu) = ret_stack;
3930 graph_init_task(t, ret_stack);
3934 /* Allocate a return stack for newly created task */
3935 void ftrace_graph_init_task(struct task_struct *t)
3937 /* Make sure we do not use the parent ret_stack */
3938 t->ret_stack = NULL;
3939 t->curr_ret_stack = -1;
3941 if (ftrace_graph_active) {
3942 struct ftrace_ret_stack *ret_stack;
3944 ret_stack = kmalloc(FTRACE_RETFUNC_DEPTH
3945 * sizeof(struct ftrace_ret_stack),
3949 graph_init_task(t, ret_stack);
3953 void ftrace_graph_exit_task(struct task_struct *t)
3955 struct ftrace_ret_stack *ret_stack = t->ret_stack;
3957 t->ret_stack = NULL;
3958 /* NULL must become visible to IRQs before we free it: */
3964 void ftrace_graph_stop(void)