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_MAX_BITS 10
62 /* ftrace_enabled is a method to turn ftrace on or off */
63 int ftrace_enabled __read_mostly;
64 static int last_ftrace_enabled;
66 /* Quick disabling of function tracer. */
67 int function_trace_stop;
69 /* List for set_ftrace_pid's pids. */
70 LIST_HEAD(ftrace_pids);
72 struct list_head list;
77 * ftrace_disabled is set when an anomaly is discovered.
78 * ftrace_disabled is much stronger than ftrace_enabled.
80 static int ftrace_disabled __read_mostly;
82 static DEFINE_MUTEX(ftrace_lock);
84 static struct ftrace_ops ftrace_list_end __read_mostly =
89 static struct ftrace_ops *ftrace_list __read_mostly = &ftrace_list_end;
90 ftrace_func_t ftrace_trace_function __read_mostly = ftrace_stub;
91 ftrace_func_t __ftrace_trace_function __read_mostly = ftrace_stub;
92 ftrace_func_t ftrace_pid_function __read_mostly = ftrace_stub;
95 * Traverse the ftrace_list, invoking all entries. The reason that we
96 * can use rcu_dereference_raw() is that elements removed from this list
97 * are simply leaked, so there is no need to interact with a grace-period
98 * mechanism. The rcu_dereference_raw() calls are needed to handle
99 * concurrent insertions into the ftrace_list.
101 * Silly Alpha and silly pointer-speculation compiler optimizations!
103 static void ftrace_list_func(unsigned long ip, unsigned long parent_ip)
105 struct ftrace_ops *op = rcu_dereference_raw(ftrace_list); /*see above*/
107 while (op != &ftrace_list_end) {
108 op->func(ip, parent_ip);
109 op = rcu_dereference_raw(op->next); /*see above*/
113 static void ftrace_pid_func(unsigned long ip, unsigned long parent_ip)
115 if (!test_tsk_trace_trace(current))
118 ftrace_pid_function(ip, parent_ip);
121 static void set_ftrace_pid_function(ftrace_func_t func)
123 /* do not set ftrace_pid_function to itself! */
124 if (func != ftrace_pid_func)
125 ftrace_pid_function = func;
129 * clear_ftrace_function - reset the ftrace function
131 * This NULLs the ftrace function and in essence stops
132 * tracing. There may be lag
134 void clear_ftrace_function(void)
136 ftrace_trace_function = ftrace_stub;
137 __ftrace_trace_function = ftrace_stub;
138 ftrace_pid_function = ftrace_stub;
141 #ifndef CONFIG_HAVE_FUNCTION_TRACE_MCOUNT_TEST
143 * For those archs that do not test ftrace_trace_stop in their
144 * mcount call site, we need to do it from C.
146 static void ftrace_test_stop_func(unsigned long ip, unsigned long parent_ip)
148 if (function_trace_stop)
151 __ftrace_trace_function(ip, parent_ip);
155 static void update_ftrace_function(void)
160 * If there's only one function registered, then call that
161 * function directly. Otherwise, we need to iterate over the
162 * registered callers.
164 if (ftrace_list == &ftrace_list_end ||
165 ftrace_list->next == &ftrace_list_end)
166 func = ftrace_list->func;
168 func = ftrace_list_func;
170 /* If we filter on pids, update to use the pid function */
171 if (!list_empty(&ftrace_pids)) {
172 set_ftrace_pid_function(func);
173 func = ftrace_pid_func;
175 #ifdef CONFIG_HAVE_FUNCTION_TRACE_MCOUNT_TEST
176 ftrace_trace_function = func;
178 __ftrace_trace_function = func;
179 ftrace_trace_function = ftrace_test_stop_func;
183 static int __register_ftrace_function(struct ftrace_ops *ops)
185 ops->next = ftrace_list;
187 * We are entering ops into the ftrace_list but another
188 * CPU might be walking that list. We need to make sure
189 * the ops->next pointer is valid before another CPU sees
190 * the ops pointer included into the ftrace_list.
192 rcu_assign_pointer(ftrace_list, ops);
195 update_ftrace_function();
200 static int __unregister_ftrace_function(struct ftrace_ops *ops)
202 struct ftrace_ops **p;
205 * If we are removing the last function, then simply point
206 * to the ftrace_stub.
208 if (ftrace_list == ops && ops->next == &ftrace_list_end) {
209 ftrace_trace_function = ftrace_stub;
210 ftrace_list = &ftrace_list_end;
214 for (p = &ftrace_list; *p != &ftrace_list_end; p = &(*p)->next)
224 update_ftrace_function();
229 static void ftrace_update_pid_func(void)
231 /* Only do something if we are tracing something */
232 if (ftrace_trace_function == ftrace_stub)
235 update_ftrace_function();
238 #ifdef CONFIG_FUNCTION_PROFILER
239 struct ftrace_profile {
240 struct hlist_node node;
242 unsigned long counter;
243 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
244 unsigned long long time;
245 unsigned long long time_squared;
249 struct ftrace_profile_page {
250 struct ftrace_profile_page *next;
252 struct ftrace_profile records[];
255 struct ftrace_profile_stat {
257 struct hlist_head *hash;
258 struct ftrace_profile_page *pages;
259 struct ftrace_profile_page *start;
260 struct tracer_stat stat;
263 #define PROFILE_RECORDS_SIZE \
264 (PAGE_SIZE - offsetof(struct ftrace_profile_page, records))
266 #define PROFILES_PER_PAGE \
267 (PROFILE_RECORDS_SIZE / sizeof(struct ftrace_profile))
269 static int ftrace_profile_bits __read_mostly;
270 static int ftrace_profile_enabled __read_mostly;
272 /* ftrace_profile_lock - synchronize the enable and disable of the profiler */
273 static DEFINE_MUTEX(ftrace_profile_lock);
275 static DEFINE_PER_CPU(struct ftrace_profile_stat, ftrace_profile_stats);
277 #define FTRACE_PROFILE_HASH_SIZE 1024 /* must be power of 2 */
280 function_stat_next(void *v, int idx)
282 struct ftrace_profile *rec = v;
283 struct ftrace_profile_page *pg;
285 pg = (struct ftrace_profile_page *)((unsigned long)rec & PAGE_MASK);
291 if ((void *)rec >= (void *)&pg->records[pg->index]) {
295 rec = &pg->records[0];
303 static void *function_stat_start(struct tracer_stat *trace)
305 struct ftrace_profile_stat *stat =
306 container_of(trace, struct ftrace_profile_stat, stat);
308 if (!stat || !stat->start)
311 return function_stat_next(&stat->start->records[0], 0);
314 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
315 /* function graph compares on total time */
316 static int function_stat_cmp(void *p1, void *p2)
318 struct ftrace_profile *a = p1;
319 struct ftrace_profile *b = p2;
321 if (a->time < b->time)
323 if (a->time > b->time)
329 /* not function graph compares against hits */
330 static int function_stat_cmp(void *p1, void *p2)
332 struct ftrace_profile *a = p1;
333 struct ftrace_profile *b = p2;
335 if (a->counter < b->counter)
337 if (a->counter > b->counter)
344 static int function_stat_headers(struct seq_file *m)
346 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
347 seq_printf(m, " Function "
350 "--- ---- --- ---\n");
352 seq_printf(m, " Function Hit\n"
358 static int function_stat_show(struct seq_file *m, void *v)
360 struct ftrace_profile *rec = v;
361 char str[KSYM_SYMBOL_LEN];
363 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
364 static struct trace_seq s;
365 unsigned long long avg;
366 unsigned long long stddev;
368 mutex_lock(&ftrace_profile_lock);
370 /* we raced with function_profile_reset() */
371 if (unlikely(rec->counter == 0)) {
376 kallsyms_lookup(rec->ip, NULL, NULL, NULL, str);
377 seq_printf(m, " %-30.30s %10lu", str, rec->counter);
379 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
382 do_div(avg, rec->counter);
384 /* Sample standard deviation (s^2) */
385 if (rec->counter <= 1)
388 stddev = rec->time_squared - rec->counter * avg * avg;
390 * Divide only 1000 for ns^2 -> us^2 conversion.
391 * trace_print_graph_duration will divide 1000 again.
393 do_div(stddev, (rec->counter - 1) * 1000);
397 trace_print_graph_duration(rec->time, &s);
398 trace_seq_puts(&s, " ");
399 trace_print_graph_duration(avg, &s);
400 trace_seq_puts(&s, " ");
401 trace_print_graph_duration(stddev, &s);
402 trace_print_seq(m, &s);
406 mutex_unlock(&ftrace_profile_lock);
411 static void ftrace_profile_reset(struct ftrace_profile_stat *stat)
413 struct ftrace_profile_page *pg;
415 pg = stat->pages = stat->start;
418 memset(pg->records, 0, PROFILE_RECORDS_SIZE);
423 memset(stat->hash, 0,
424 FTRACE_PROFILE_HASH_SIZE * sizeof(struct hlist_head));
427 int ftrace_profile_pages_init(struct ftrace_profile_stat *stat)
429 struct ftrace_profile_page *pg;
434 /* If we already allocated, do nothing */
438 stat->pages = (void *)get_zeroed_page(GFP_KERNEL);
442 #ifdef CONFIG_DYNAMIC_FTRACE
443 functions = ftrace_update_tot_cnt;
446 * We do not know the number of functions that exist because
447 * dynamic tracing is what counts them. With past experience
448 * we have around 20K functions. That should be more than enough.
449 * It is highly unlikely we will execute every function in
455 pg = stat->start = stat->pages;
457 pages = DIV_ROUND_UP(functions, PROFILES_PER_PAGE);
459 for (i = 0; i < pages; i++) {
460 pg->next = (void *)get_zeroed_page(GFP_KERNEL);
471 unsigned long tmp = (unsigned long)pg;
477 free_page((unsigned long)stat->pages);
484 static int ftrace_profile_init_cpu(int cpu)
486 struct ftrace_profile_stat *stat;
489 stat = &per_cpu(ftrace_profile_stats, cpu);
492 /* If the profile is already created, simply reset it */
493 ftrace_profile_reset(stat);
498 * We are profiling all functions, but usually only a few thousand
499 * functions are hit. We'll make a hash of 1024 items.
501 size = FTRACE_PROFILE_HASH_SIZE;
503 stat->hash = kzalloc(sizeof(struct hlist_head) * size, GFP_KERNEL);
508 if (!ftrace_profile_bits) {
511 for (; size; size >>= 1)
512 ftrace_profile_bits++;
515 /* Preallocate the function profiling pages */
516 if (ftrace_profile_pages_init(stat) < 0) {
525 static int ftrace_profile_init(void)
530 for_each_online_cpu(cpu) {
531 ret = ftrace_profile_init_cpu(cpu);
539 /* interrupts must be disabled */
540 static struct ftrace_profile *
541 ftrace_find_profiled_func(struct ftrace_profile_stat *stat, unsigned long ip)
543 struct ftrace_profile *rec;
544 struct hlist_head *hhd;
545 struct hlist_node *n;
548 key = hash_long(ip, ftrace_profile_bits);
549 hhd = &stat->hash[key];
551 if (hlist_empty(hhd))
554 hlist_for_each_entry_rcu(rec, n, hhd, node) {
562 static void ftrace_add_profile(struct ftrace_profile_stat *stat,
563 struct ftrace_profile *rec)
567 key = hash_long(rec->ip, ftrace_profile_bits);
568 hlist_add_head_rcu(&rec->node, &stat->hash[key]);
572 * The memory is already allocated, this simply finds a new record to use.
574 static struct ftrace_profile *
575 ftrace_profile_alloc(struct ftrace_profile_stat *stat, unsigned long ip)
577 struct ftrace_profile *rec = NULL;
579 /* prevent recursion (from NMIs) */
580 if (atomic_inc_return(&stat->disabled) != 1)
584 * Try to find the function again since an NMI
585 * could have added it
587 rec = ftrace_find_profiled_func(stat, ip);
591 if (stat->pages->index == PROFILES_PER_PAGE) {
592 if (!stat->pages->next)
594 stat->pages = stat->pages->next;
597 rec = &stat->pages->records[stat->pages->index++];
599 ftrace_add_profile(stat, rec);
602 atomic_dec(&stat->disabled);
608 function_profile_call(unsigned long ip, unsigned long parent_ip)
610 struct ftrace_profile_stat *stat;
611 struct ftrace_profile *rec;
614 if (!ftrace_profile_enabled)
617 local_irq_save(flags);
619 stat = &__get_cpu_var(ftrace_profile_stats);
620 if (!stat->hash || !ftrace_profile_enabled)
623 rec = ftrace_find_profiled_func(stat, ip);
625 rec = ftrace_profile_alloc(stat, ip);
632 local_irq_restore(flags);
635 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
636 static int profile_graph_entry(struct ftrace_graph_ent *trace)
638 function_profile_call(trace->func, 0);
642 static void profile_graph_return(struct ftrace_graph_ret *trace)
644 struct ftrace_profile_stat *stat;
645 unsigned long long calltime;
646 struct ftrace_profile *rec;
649 local_irq_save(flags);
650 stat = &__get_cpu_var(ftrace_profile_stats);
651 if (!stat->hash || !ftrace_profile_enabled)
654 /* If the calltime was zero'd ignore it */
655 if (!trace->calltime)
658 calltime = trace->rettime - trace->calltime;
660 if (!(trace_flags & TRACE_ITER_GRAPH_TIME)) {
663 index = trace->depth;
665 /* Append this call time to the parent time to subtract */
667 current->ret_stack[index - 1].subtime += calltime;
669 if (current->ret_stack[index].subtime < calltime)
670 calltime -= current->ret_stack[index].subtime;
675 rec = ftrace_find_profiled_func(stat, trace->func);
677 rec->time += calltime;
678 rec->time_squared += calltime * calltime;
682 local_irq_restore(flags);
685 static int register_ftrace_profiler(void)
687 return register_ftrace_graph(&profile_graph_return,
688 &profile_graph_entry);
691 static void unregister_ftrace_profiler(void)
693 unregister_ftrace_graph();
696 static struct ftrace_ops ftrace_profile_ops __read_mostly =
698 .func = function_profile_call,
701 static int register_ftrace_profiler(void)
703 return register_ftrace_function(&ftrace_profile_ops);
706 static void unregister_ftrace_profiler(void)
708 unregister_ftrace_function(&ftrace_profile_ops);
710 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
713 ftrace_profile_write(struct file *filp, const char __user *ubuf,
714 size_t cnt, loff_t *ppos)
717 char buf[64]; /* big enough to hold a number */
720 if (cnt >= sizeof(buf))
723 if (copy_from_user(&buf, ubuf, cnt))
728 ret = strict_strtoul(buf, 10, &val);
734 mutex_lock(&ftrace_profile_lock);
735 if (ftrace_profile_enabled ^ val) {
737 ret = ftrace_profile_init();
743 ret = register_ftrace_profiler();
748 ftrace_profile_enabled = 1;
750 ftrace_profile_enabled = 0;
752 * unregister_ftrace_profiler calls stop_machine
753 * so this acts like an synchronize_sched.
755 unregister_ftrace_profiler();
759 mutex_unlock(&ftrace_profile_lock);
767 ftrace_profile_read(struct file *filp, char __user *ubuf,
768 size_t cnt, loff_t *ppos)
770 char buf[64]; /* big enough to hold a number */
773 r = sprintf(buf, "%u\n", ftrace_profile_enabled);
774 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
777 static const struct file_operations ftrace_profile_fops = {
778 .open = tracing_open_generic,
779 .read = ftrace_profile_read,
780 .write = ftrace_profile_write,
781 .llseek = default_llseek,
784 /* used to initialize the real stat files */
785 static struct tracer_stat function_stats __initdata = {
787 .stat_start = function_stat_start,
788 .stat_next = function_stat_next,
789 .stat_cmp = function_stat_cmp,
790 .stat_headers = function_stat_headers,
791 .stat_show = function_stat_show
794 static __init void ftrace_profile_debugfs(struct dentry *d_tracer)
796 struct ftrace_profile_stat *stat;
797 struct dentry *entry;
802 for_each_possible_cpu(cpu) {
803 stat = &per_cpu(ftrace_profile_stats, cpu);
805 /* allocate enough for function name + cpu number */
806 name = kmalloc(32, GFP_KERNEL);
809 * The files created are permanent, if something happens
810 * we still do not free memory.
813 "Could not allocate stat file for cpu %d\n",
817 stat->stat = function_stats;
818 snprintf(name, 32, "function%d", cpu);
819 stat->stat.name = name;
820 ret = register_stat_tracer(&stat->stat);
823 "Could not register function stat for cpu %d\n",
830 entry = debugfs_create_file("function_profile_enabled", 0644,
831 d_tracer, NULL, &ftrace_profile_fops);
833 pr_warning("Could not create debugfs "
834 "'function_profile_enabled' entry\n");
837 #else /* CONFIG_FUNCTION_PROFILER */
838 static __init void ftrace_profile_debugfs(struct dentry *d_tracer)
841 #endif /* CONFIG_FUNCTION_PROFILER */
843 static struct pid * const ftrace_swapper_pid = &init_struct_pid;
845 #ifdef CONFIG_DYNAMIC_FTRACE
847 #ifndef CONFIG_FTRACE_MCOUNT_RECORD
848 # error Dynamic ftrace depends on MCOUNT_RECORD
851 static struct hlist_head ftrace_func_hash[FTRACE_FUNC_HASHSIZE] __read_mostly;
853 struct ftrace_func_probe {
854 struct hlist_node node;
855 struct ftrace_probe_ops *ops;
863 FTRACE_ENABLE_CALLS = (1 << 0),
864 FTRACE_DISABLE_CALLS = (1 << 1),
865 FTRACE_UPDATE_TRACE_FUNC = (1 << 2),
866 FTRACE_START_FUNC_RET = (1 << 3),
867 FTRACE_STOP_FUNC_RET = (1 << 4),
869 struct ftrace_func_entry {
870 struct hlist_node hlist;
875 unsigned long size_bits;
876 struct hlist_head *buckets;
880 static struct hlist_head notrace_buckets[1 << FTRACE_HASH_MAX_BITS];
881 static struct ftrace_hash notrace_hash = {
882 .size_bits = FTRACE_HASH_MAX_BITS,
883 .buckets = notrace_buckets,
886 static int ftrace_filtered;
888 static struct dyn_ftrace *ftrace_new_addrs;
890 static DEFINE_MUTEX(ftrace_regex_lock);
893 struct ftrace_page *next;
895 struct dyn_ftrace records[];
898 #define ENTRIES_PER_PAGE \
899 ((PAGE_SIZE - sizeof(struct ftrace_page)) / sizeof(struct dyn_ftrace))
901 /* estimate from running different kernels */
902 #define NR_TO_INIT 10000
904 static struct ftrace_page *ftrace_pages_start;
905 static struct ftrace_page *ftrace_pages;
907 static struct dyn_ftrace *ftrace_free_records;
909 static struct ftrace_func_entry *
910 ftrace_lookup_ip(struct ftrace_hash *hash, unsigned long ip)
913 struct ftrace_func_entry *entry;
914 struct hlist_head *hhd;
915 struct hlist_node *n;
920 if (hash->size_bits > 0)
921 key = hash_long(ip, hash->size_bits);
925 hhd = &hash->buckets[key];
927 hlist_for_each_entry_rcu(entry, n, hhd, hlist) {
934 static int add_hash_entry(struct ftrace_hash *hash, unsigned long ip)
936 struct ftrace_func_entry *entry;
937 struct hlist_head *hhd;
940 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
945 key = hash_long(ip, hash->size_bits);
950 hhd = &hash->buckets[key];
951 hlist_add_head(&entry->hlist, hhd);
958 remove_hash_entry(struct ftrace_hash *hash,
959 struct ftrace_func_entry *entry)
961 hlist_del(&entry->hlist);
966 static void ftrace_hash_clear(struct ftrace_hash *hash)
968 struct hlist_head *hhd;
969 struct hlist_node *tp, *tn;
970 struct ftrace_func_entry *entry;
971 int size = 1 << hash->size_bits;
974 for (i = 0; i < size; i++) {
975 hhd = &hash->buckets[i];
976 hlist_for_each_entry_safe(entry, tp, tn, hhd, hlist)
977 remove_hash_entry(hash, entry);
979 FTRACE_WARN_ON(hash->count);
983 * This is a double for. Do not use 'break' to break out of the loop,
984 * you must use a goto.
986 #define do_for_each_ftrace_rec(pg, rec) \
987 for (pg = ftrace_pages_start; pg; pg = pg->next) { \
989 for (_____i = 0; _____i < pg->index; _____i++) { \
990 rec = &pg->records[_____i];
992 #define while_for_each_ftrace_rec() \
996 static void ftrace_free_rec(struct dyn_ftrace *rec)
998 rec->freelist = ftrace_free_records;
999 ftrace_free_records = rec;
1000 rec->flags |= FTRACE_FL_FREE;
1003 static struct dyn_ftrace *ftrace_alloc_dyn_node(unsigned long ip)
1005 struct dyn_ftrace *rec;
1007 /* First check for freed records */
1008 if (ftrace_free_records) {
1009 rec = ftrace_free_records;
1011 if (unlikely(!(rec->flags & FTRACE_FL_FREE))) {
1012 FTRACE_WARN_ON_ONCE(1);
1013 ftrace_free_records = NULL;
1017 ftrace_free_records = rec->freelist;
1018 memset(rec, 0, sizeof(*rec));
1022 if (ftrace_pages->index == ENTRIES_PER_PAGE) {
1023 if (!ftrace_pages->next) {
1024 /* allocate another page */
1025 ftrace_pages->next =
1026 (void *)get_zeroed_page(GFP_KERNEL);
1027 if (!ftrace_pages->next)
1030 ftrace_pages = ftrace_pages->next;
1033 return &ftrace_pages->records[ftrace_pages->index++];
1036 static struct dyn_ftrace *
1037 ftrace_record_ip(unsigned long ip)
1039 struct dyn_ftrace *rec;
1041 if (ftrace_disabled)
1044 rec = ftrace_alloc_dyn_node(ip);
1049 rec->newlist = ftrace_new_addrs;
1050 ftrace_new_addrs = rec;
1055 static void print_ip_ins(const char *fmt, unsigned char *p)
1059 printk(KERN_CONT "%s", fmt);
1061 for (i = 0; i < MCOUNT_INSN_SIZE; i++)
1062 printk(KERN_CONT "%s%02x", i ? ":" : "", p[i]);
1065 static void ftrace_bug(int failed, unsigned long ip)
1069 FTRACE_WARN_ON_ONCE(1);
1070 pr_info("ftrace faulted on modifying ");
1074 FTRACE_WARN_ON_ONCE(1);
1075 pr_info("ftrace failed to modify ");
1077 print_ip_ins(" actual: ", (unsigned char *)ip);
1078 printk(KERN_CONT "\n");
1081 FTRACE_WARN_ON_ONCE(1);
1082 pr_info("ftrace faulted on writing ");
1086 FTRACE_WARN_ON_ONCE(1);
1087 pr_info("ftrace faulted on unknown error ");
1093 /* Return 1 if the address range is reserved for ftrace */
1094 int ftrace_text_reserved(void *start, void *end)
1096 struct dyn_ftrace *rec;
1097 struct ftrace_page *pg;
1099 do_for_each_ftrace_rec(pg, rec) {
1100 if (rec->ip <= (unsigned long)end &&
1101 rec->ip + MCOUNT_INSN_SIZE > (unsigned long)start)
1103 } while_for_each_ftrace_rec();
1109 __ftrace_replace_code(struct dyn_ftrace *rec, int enable)
1111 unsigned long ftrace_addr;
1112 unsigned long flag = 0UL;
1114 ftrace_addr = (unsigned long)FTRACE_ADDR;
1117 * If this record is not to be traced or we want to disable it,
1120 * If we want to enable it and filtering is off, then enable it.
1122 * If we want to enable it and filtering is on, enable it only if
1125 if (enable && !ftrace_lookup_ip(¬race_hash, rec->ip)) {
1126 if (!ftrace_filtered || (rec->flags & FTRACE_FL_FILTER))
1127 flag = FTRACE_FL_ENABLED;
1130 /* If the state of this record hasn't changed, then do nothing */
1131 if ((rec->flags & FTRACE_FL_ENABLED) == flag)
1135 rec->flags |= FTRACE_FL_ENABLED;
1136 return ftrace_make_call(rec, ftrace_addr);
1139 rec->flags &= ~FTRACE_FL_ENABLED;
1140 return ftrace_make_nop(NULL, rec, ftrace_addr);
1143 static void ftrace_replace_code(int enable)
1145 struct dyn_ftrace *rec;
1146 struct ftrace_page *pg;
1149 if (unlikely(ftrace_disabled))
1152 do_for_each_ftrace_rec(pg, rec) {
1153 /* Skip over free records */
1154 if (rec->flags & FTRACE_FL_FREE)
1157 failed = __ftrace_replace_code(rec, enable);
1159 ftrace_bug(failed, rec->ip);
1160 /* Stop processing */
1163 } while_for_each_ftrace_rec();
1167 ftrace_code_disable(struct module *mod, struct dyn_ftrace *rec)
1174 if (unlikely(ftrace_disabled))
1177 ret = ftrace_make_nop(mod, rec, MCOUNT_ADDR);
1179 ftrace_bug(ret, ip);
1186 * archs can override this function if they must do something
1187 * before the modifying code is performed.
1189 int __weak ftrace_arch_code_modify_prepare(void)
1195 * archs can override this function if they must do something
1196 * after the modifying code is performed.
1198 int __weak ftrace_arch_code_modify_post_process(void)
1203 static int __ftrace_modify_code(void *data)
1205 int *command = data;
1207 if (*command & FTRACE_ENABLE_CALLS)
1208 ftrace_replace_code(1);
1209 else if (*command & FTRACE_DISABLE_CALLS)
1210 ftrace_replace_code(0);
1212 if (*command & FTRACE_UPDATE_TRACE_FUNC)
1213 ftrace_update_ftrace_func(ftrace_trace_function);
1215 if (*command & FTRACE_START_FUNC_RET)
1216 ftrace_enable_ftrace_graph_caller();
1217 else if (*command & FTRACE_STOP_FUNC_RET)
1218 ftrace_disable_ftrace_graph_caller();
1223 static void ftrace_run_update_code(int command)
1227 ret = ftrace_arch_code_modify_prepare();
1228 FTRACE_WARN_ON(ret);
1232 stop_machine(__ftrace_modify_code, &command, NULL);
1234 ret = ftrace_arch_code_modify_post_process();
1235 FTRACE_WARN_ON(ret);
1238 static ftrace_func_t saved_ftrace_func;
1239 static int ftrace_start_up;
1241 static void ftrace_startup_enable(int command)
1243 if (saved_ftrace_func != ftrace_trace_function) {
1244 saved_ftrace_func = ftrace_trace_function;
1245 command |= FTRACE_UPDATE_TRACE_FUNC;
1248 if (!command || !ftrace_enabled)
1251 ftrace_run_update_code(command);
1254 static void ftrace_startup(int command)
1256 if (unlikely(ftrace_disabled))
1260 command |= FTRACE_ENABLE_CALLS;
1262 ftrace_startup_enable(command);
1265 static void ftrace_shutdown(int command)
1267 if (unlikely(ftrace_disabled))
1272 * Just warn in case of unbalance, no need to kill ftrace, it's not
1273 * critical but the ftrace_call callers may be never nopped again after
1274 * further ftrace uses.
1276 WARN_ON_ONCE(ftrace_start_up < 0);
1278 if (!ftrace_start_up)
1279 command |= FTRACE_DISABLE_CALLS;
1281 if (saved_ftrace_func != ftrace_trace_function) {
1282 saved_ftrace_func = ftrace_trace_function;
1283 command |= FTRACE_UPDATE_TRACE_FUNC;
1286 if (!command || !ftrace_enabled)
1289 ftrace_run_update_code(command);
1292 static void ftrace_startup_sysctl(void)
1294 if (unlikely(ftrace_disabled))
1297 /* Force update next time */
1298 saved_ftrace_func = NULL;
1299 /* ftrace_start_up is true if we want ftrace running */
1300 if (ftrace_start_up)
1301 ftrace_run_update_code(FTRACE_ENABLE_CALLS);
1304 static void ftrace_shutdown_sysctl(void)
1306 if (unlikely(ftrace_disabled))
1309 /* ftrace_start_up is true if ftrace is running */
1310 if (ftrace_start_up)
1311 ftrace_run_update_code(FTRACE_DISABLE_CALLS);
1314 static cycle_t ftrace_update_time;
1315 static unsigned long ftrace_update_cnt;
1316 unsigned long ftrace_update_tot_cnt;
1318 static int ftrace_update_code(struct module *mod)
1320 struct dyn_ftrace *p;
1321 cycle_t start, stop;
1323 start = ftrace_now(raw_smp_processor_id());
1324 ftrace_update_cnt = 0;
1326 while (ftrace_new_addrs) {
1328 /* If something went wrong, bail without enabling anything */
1329 if (unlikely(ftrace_disabled))
1332 p = ftrace_new_addrs;
1333 ftrace_new_addrs = p->newlist;
1337 * Do the initial record conversion from mcount jump
1338 * to the NOP instructions.
1340 if (!ftrace_code_disable(mod, p)) {
1346 ftrace_update_cnt++;
1349 * If the tracing is enabled, go ahead and enable the record.
1351 * The reason not to enable the record immediatelly is the
1352 * inherent check of ftrace_make_nop/ftrace_make_call for
1353 * correct previous instructions. Making first the NOP
1354 * conversion puts the module to the correct state, thus
1355 * passing the ftrace_make_call check.
1357 if (ftrace_start_up) {
1358 int failed = __ftrace_replace_code(p, 1);
1360 ftrace_bug(failed, p->ip);
1366 stop = ftrace_now(raw_smp_processor_id());
1367 ftrace_update_time = stop - start;
1368 ftrace_update_tot_cnt += ftrace_update_cnt;
1373 static int __init ftrace_dyn_table_alloc(unsigned long num_to_init)
1375 struct ftrace_page *pg;
1379 /* allocate a few pages */
1380 ftrace_pages_start = (void *)get_zeroed_page(GFP_KERNEL);
1381 if (!ftrace_pages_start)
1385 * Allocate a few more pages.
1387 * TODO: have some parser search vmlinux before
1388 * final linking to find all calls to ftrace.
1390 * a) know how many pages to allocate.
1392 * b) set up the table then.
1394 * The dynamic code is still necessary for
1398 pg = ftrace_pages = ftrace_pages_start;
1400 cnt = num_to_init / ENTRIES_PER_PAGE;
1401 pr_info("ftrace: allocating %ld entries in %d pages\n",
1402 num_to_init, cnt + 1);
1404 for (i = 0; i < cnt; i++) {
1405 pg->next = (void *)get_zeroed_page(GFP_KERNEL);
1407 /* If we fail, we'll try later anyway */
1418 FTRACE_ITER_FILTER = (1 << 0),
1419 FTRACE_ITER_NOTRACE = (1 << 1),
1420 FTRACE_ITER_PRINTALL = (1 << 2),
1421 FTRACE_ITER_HASH = (1 << 3),
1424 #define FTRACE_BUFF_MAX (KSYM_SYMBOL_LEN+4) /* room for wildcards */
1426 struct ftrace_iterator {
1429 struct ftrace_page *pg;
1430 struct dyn_ftrace *func;
1431 struct ftrace_func_probe *probe;
1432 struct trace_parser parser;
1439 t_hash_next(struct seq_file *m, loff_t *pos)
1441 struct ftrace_iterator *iter = m->private;
1442 struct hlist_node *hnd = NULL;
1443 struct hlist_head *hhd;
1449 hnd = &iter->probe->node;
1451 if (iter->hidx >= FTRACE_FUNC_HASHSIZE)
1454 hhd = &ftrace_func_hash[iter->hidx];
1456 if (hlist_empty(hhd)) {
1472 if (WARN_ON_ONCE(!hnd))
1475 iter->probe = hlist_entry(hnd, struct ftrace_func_probe, node);
1480 static void *t_hash_start(struct seq_file *m, loff_t *pos)
1482 struct ftrace_iterator *iter = m->private;
1486 if (iter->func_pos > *pos)
1490 for (l = 0; l <= (*pos - iter->func_pos); ) {
1491 p = t_hash_next(m, &l);
1498 /* Only set this if we have an item */
1499 iter->flags |= FTRACE_ITER_HASH;
1505 t_hash_show(struct seq_file *m, struct ftrace_iterator *iter)
1507 struct ftrace_func_probe *rec;
1510 if (WARN_ON_ONCE(!rec))
1513 if (rec->ops->print)
1514 return rec->ops->print(m, rec->ip, rec->ops, rec->data);
1516 seq_printf(m, "%ps:%ps", (void *)rec->ip, (void *)rec->ops->func);
1519 seq_printf(m, ":%p", rec->data);
1526 t_next(struct seq_file *m, void *v, loff_t *pos)
1528 struct ftrace_iterator *iter = m->private;
1529 struct dyn_ftrace *rec = NULL;
1531 if (unlikely(ftrace_disabled))
1534 if (iter->flags & FTRACE_ITER_HASH)
1535 return t_hash_next(m, pos);
1538 iter->pos = iter->func_pos = *pos;
1540 if (iter->flags & FTRACE_ITER_PRINTALL)
1541 return t_hash_start(m, pos);
1544 if (iter->idx >= iter->pg->index) {
1545 if (iter->pg->next) {
1546 iter->pg = iter->pg->next;
1551 rec = &iter->pg->records[iter->idx++];
1552 if ((rec->flags & FTRACE_FL_FREE) ||
1554 ((iter->flags & FTRACE_ITER_FILTER) &&
1555 !(rec->flags & FTRACE_FL_FILTER)) ||
1557 ((iter->flags & FTRACE_ITER_NOTRACE) &&
1558 !ftrace_lookup_ip(¬race_hash, rec->ip))) {
1565 return t_hash_start(m, pos);
1572 static void reset_iter_read(struct ftrace_iterator *iter)
1576 iter->flags &= ~(FTRACE_ITER_PRINTALL & FTRACE_ITER_HASH);
1579 static void *t_start(struct seq_file *m, loff_t *pos)
1581 struct ftrace_iterator *iter = m->private;
1585 mutex_lock(&ftrace_lock);
1587 if (unlikely(ftrace_disabled))
1591 * If an lseek was done, then reset and start from beginning.
1593 if (*pos < iter->pos)
1594 reset_iter_read(iter);
1597 * For set_ftrace_filter reading, if we have the filter
1598 * off, we can short cut and just print out that all
1599 * functions are enabled.
1601 if (iter->flags & FTRACE_ITER_FILTER && !ftrace_filtered) {
1603 return t_hash_start(m, pos);
1604 iter->flags |= FTRACE_ITER_PRINTALL;
1605 /* reset in case of seek/pread */
1606 iter->flags &= ~FTRACE_ITER_HASH;
1610 if (iter->flags & FTRACE_ITER_HASH)
1611 return t_hash_start(m, pos);
1614 * Unfortunately, we need to restart at ftrace_pages_start
1615 * every time we let go of the ftrace_mutex. This is because
1616 * those pointers can change without the lock.
1618 iter->pg = ftrace_pages_start;
1620 for (l = 0; l <= *pos; ) {
1621 p = t_next(m, p, &l);
1627 if (iter->flags & FTRACE_ITER_FILTER)
1628 return t_hash_start(m, pos);
1636 static void t_stop(struct seq_file *m, void *p)
1638 mutex_unlock(&ftrace_lock);
1641 static int t_show(struct seq_file *m, void *v)
1643 struct ftrace_iterator *iter = m->private;
1644 struct dyn_ftrace *rec;
1646 if (iter->flags & FTRACE_ITER_HASH)
1647 return t_hash_show(m, iter);
1649 if (iter->flags & FTRACE_ITER_PRINTALL) {
1650 seq_printf(m, "#### all functions enabled ####\n");
1659 seq_printf(m, "%ps\n", (void *)rec->ip);
1664 static const struct seq_operations show_ftrace_seq_ops = {
1672 ftrace_avail_open(struct inode *inode, struct file *file)
1674 struct ftrace_iterator *iter;
1677 if (unlikely(ftrace_disabled))
1680 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
1684 iter->pg = ftrace_pages_start;
1686 ret = seq_open(file, &show_ftrace_seq_ops);
1688 struct seq_file *m = file->private_data;
1698 static void ftrace_filter_reset(int enable)
1700 struct ftrace_page *pg;
1701 struct dyn_ftrace *rec;
1703 mutex_lock(&ftrace_lock);
1705 ftrace_filtered = 0;
1706 do_for_each_ftrace_rec(pg, rec) {
1707 rec->flags &= ~FTRACE_FL_FILTER;
1708 } while_for_each_ftrace_rec();
1710 ftrace_hash_clear(¬race_hash);
1711 mutex_unlock(&ftrace_lock);
1715 ftrace_regex_open(struct inode *inode, struct file *file, int enable)
1717 struct ftrace_iterator *iter;
1720 if (unlikely(ftrace_disabled))
1723 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
1727 if (trace_parser_get_init(&iter->parser, FTRACE_BUFF_MAX)) {
1732 mutex_lock(&ftrace_regex_lock);
1733 if ((file->f_mode & FMODE_WRITE) &&
1734 (file->f_flags & O_TRUNC))
1735 ftrace_filter_reset(enable);
1737 if (file->f_mode & FMODE_READ) {
1738 iter->pg = ftrace_pages_start;
1739 iter->flags = enable ? FTRACE_ITER_FILTER :
1740 FTRACE_ITER_NOTRACE;
1742 ret = seq_open(file, &show_ftrace_seq_ops);
1744 struct seq_file *m = file->private_data;
1747 trace_parser_put(&iter->parser);
1751 file->private_data = iter;
1752 mutex_unlock(&ftrace_regex_lock);
1758 ftrace_filter_open(struct inode *inode, struct file *file)
1760 return ftrace_regex_open(inode, file, 1);
1764 ftrace_notrace_open(struct inode *inode, struct file *file)
1766 return ftrace_regex_open(inode, file, 0);
1770 ftrace_regex_lseek(struct file *file, loff_t offset, int origin)
1774 if (file->f_mode & FMODE_READ)
1775 ret = seq_lseek(file, offset, origin);
1777 file->f_pos = ret = 1;
1782 static int ftrace_match(char *str, char *regex, int len, int type)
1789 if (strcmp(str, regex) == 0)
1792 case MATCH_FRONT_ONLY:
1793 if (strncmp(str, regex, len) == 0)
1796 case MATCH_MIDDLE_ONLY:
1797 if (strstr(str, regex))
1800 case MATCH_END_ONLY:
1802 if (slen >= len && memcmp(str + slen - len, regex, len) == 0)
1811 update_record(struct dyn_ftrace *rec, int enable, int not)
1813 struct ftrace_func_entry *entry;
1814 struct ftrace_hash *hash = ¬race_hash;
1819 rec->flags &= ~FTRACE_FL_FILTER;
1821 rec->flags |= FTRACE_FL_FILTER;
1824 /* Do nothing if it doesn't exist */
1825 entry = ftrace_lookup_ip(hash, rec->ip);
1829 remove_hash_entry(hash, entry);
1831 /* Do nothing if it exists */
1832 entry = ftrace_lookup_ip(hash, rec->ip);
1836 ret = add_hash_entry(hash, rec->ip);
1843 ftrace_match_record(struct dyn_ftrace *rec, char *mod,
1844 char *regex, int len, int type)
1846 char str[KSYM_SYMBOL_LEN];
1849 kallsyms_lookup(rec->ip, NULL, NULL, &modname, str);
1852 /* module lookup requires matching the module */
1853 if (!modname || strcmp(modname, mod))
1856 /* blank search means to match all funcs in the mod */
1861 return ftrace_match(str, regex, len, type);
1864 static int match_records(char *buff, int len, char *mod, int enable, int not)
1866 unsigned search_len = 0;
1867 struct ftrace_page *pg;
1868 struct dyn_ftrace *rec;
1869 int type = MATCH_FULL;
1870 char *search = buff;
1875 type = filter_parse_regex(buff, len, &search, ¬);
1876 search_len = strlen(search);
1879 mutex_lock(&ftrace_lock);
1881 if (unlikely(ftrace_disabled))
1884 do_for_each_ftrace_rec(pg, rec) {
1886 if (ftrace_match_record(rec, mod, search, search_len, type)) {
1887 ret = update_record(rec, enable, not);
1895 * Only enable filtering if we have a function that
1898 if (enable && (rec->flags & FTRACE_FL_FILTER))
1899 ftrace_filtered = 1;
1901 } while_for_each_ftrace_rec();
1903 mutex_unlock(&ftrace_lock);
1909 ftrace_match_records(char *buff, int len, int enable)
1911 return match_records(buff, len, NULL, enable, 0);
1914 static int ftrace_match_module_records(char *buff, char *mod, int enable)
1918 /* blank or '*' mean the same */
1919 if (strcmp(buff, "*") == 0)
1922 /* handle the case of 'dont filter this module' */
1923 if (strcmp(buff, "!") == 0 || strcmp(buff, "!*") == 0) {
1928 return match_records(buff, strlen(buff), mod, enable, not);
1932 * We register the module command as a template to show others how
1933 * to register the a command as well.
1937 ftrace_mod_callback(char *func, char *cmd, char *param, int enable)
1943 * cmd == 'mod' because we only registered this func
1944 * for the 'mod' ftrace_func_command.
1945 * But if you register one func with multiple commands,
1946 * you can tell which command was used by the cmd
1950 /* we must have a module name */
1954 mod = strsep(¶m, ":");
1958 ret = ftrace_match_module_records(func, mod, enable);
1967 static struct ftrace_func_command ftrace_mod_cmd = {
1969 .func = ftrace_mod_callback,
1972 static int __init ftrace_mod_cmd_init(void)
1974 return register_ftrace_command(&ftrace_mod_cmd);
1976 device_initcall(ftrace_mod_cmd_init);
1979 function_trace_probe_call(unsigned long ip, unsigned long parent_ip)
1981 struct ftrace_func_probe *entry;
1982 struct hlist_head *hhd;
1983 struct hlist_node *n;
1986 key = hash_long(ip, FTRACE_HASH_BITS);
1988 hhd = &ftrace_func_hash[key];
1990 if (hlist_empty(hhd))
1994 * Disable preemption for these calls to prevent a RCU grace
1995 * period. This syncs the hash iteration and freeing of items
1996 * on the hash. rcu_read_lock is too dangerous here.
1998 preempt_disable_notrace();
1999 hlist_for_each_entry_rcu(entry, n, hhd, node) {
2000 if (entry->ip == ip)
2001 entry->ops->func(ip, parent_ip, &entry->data);
2003 preempt_enable_notrace();
2006 static struct ftrace_ops trace_probe_ops __read_mostly =
2008 .func = function_trace_probe_call,
2011 static int ftrace_probe_registered;
2013 static void __enable_ftrace_function_probe(void)
2017 if (ftrace_probe_registered)
2020 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
2021 struct hlist_head *hhd = &ftrace_func_hash[i];
2025 /* Nothing registered? */
2026 if (i == FTRACE_FUNC_HASHSIZE)
2029 __register_ftrace_function(&trace_probe_ops);
2031 ftrace_probe_registered = 1;
2034 static void __disable_ftrace_function_probe(void)
2038 if (!ftrace_probe_registered)
2041 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
2042 struct hlist_head *hhd = &ftrace_func_hash[i];
2047 /* no more funcs left */
2048 __unregister_ftrace_function(&trace_probe_ops);
2050 ftrace_probe_registered = 0;
2054 static void ftrace_free_entry_rcu(struct rcu_head *rhp)
2056 struct ftrace_func_probe *entry =
2057 container_of(rhp, struct ftrace_func_probe, rcu);
2059 if (entry->ops->free)
2060 entry->ops->free(&entry->data);
2066 register_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
2069 struct ftrace_func_probe *entry;
2070 struct ftrace_page *pg;
2071 struct dyn_ftrace *rec;
2077 type = filter_parse_regex(glob, strlen(glob), &search, ¬);
2078 len = strlen(search);
2080 /* we do not support '!' for function probes */
2084 mutex_lock(&ftrace_lock);
2086 if (unlikely(ftrace_disabled))
2089 do_for_each_ftrace_rec(pg, rec) {
2091 if (!ftrace_match_record(rec, NULL, search, len, type))
2094 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
2096 /* If we did not process any, then return error */
2107 * The caller might want to do something special
2108 * for each function we find. We call the callback
2109 * to give the caller an opportunity to do so.
2111 if (ops->callback) {
2112 if (ops->callback(rec->ip, &entry->data) < 0) {
2113 /* caller does not like this func */
2120 entry->ip = rec->ip;
2122 key = hash_long(entry->ip, FTRACE_HASH_BITS);
2123 hlist_add_head_rcu(&entry->node, &ftrace_func_hash[key]);
2125 } while_for_each_ftrace_rec();
2126 __enable_ftrace_function_probe();
2129 mutex_unlock(&ftrace_lock);
2135 PROBE_TEST_FUNC = 1,
2140 __unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
2141 void *data, int flags)
2143 struct ftrace_func_probe *entry;
2144 struct hlist_node *n, *tmp;
2145 char str[KSYM_SYMBOL_LEN];
2146 int type = MATCH_FULL;
2150 if (glob && (strcmp(glob, "*") == 0 || !strlen(glob)))
2155 type = filter_parse_regex(glob, strlen(glob), &search, ¬);
2156 len = strlen(search);
2158 /* we do not support '!' for function probes */
2163 mutex_lock(&ftrace_lock);
2164 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
2165 struct hlist_head *hhd = &ftrace_func_hash[i];
2167 hlist_for_each_entry_safe(entry, n, tmp, hhd, node) {
2169 /* break up if statements for readability */
2170 if ((flags & PROBE_TEST_FUNC) && entry->ops != ops)
2173 if ((flags & PROBE_TEST_DATA) && entry->data != data)
2176 /* do this last, since it is the most expensive */
2178 kallsyms_lookup(entry->ip, NULL, NULL,
2180 if (!ftrace_match(str, glob, len, type))
2184 hlist_del(&entry->node);
2185 call_rcu(&entry->rcu, ftrace_free_entry_rcu);
2188 __disable_ftrace_function_probe();
2189 mutex_unlock(&ftrace_lock);
2193 unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
2196 __unregister_ftrace_function_probe(glob, ops, data,
2197 PROBE_TEST_FUNC | PROBE_TEST_DATA);
2201 unregister_ftrace_function_probe_func(char *glob, struct ftrace_probe_ops *ops)
2203 __unregister_ftrace_function_probe(glob, ops, NULL, PROBE_TEST_FUNC);
2206 void unregister_ftrace_function_probe_all(char *glob)
2208 __unregister_ftrace_function_probe(glob, NULL, NULL, 0);
2211 static LIST_HEAD(ftrace_commands);
2212 static DEFINE_MUTEX(ftrace_cmd_mutex);
2214 int register_ftrace_command(struct ftrace_func_command *cmd)
2216 struct ftrace_func_command *p;
2219 mutex_lock(&ftrace_cmd_mutex);
2220 list_for_each_entry(p, &ftrace_commands, list) {
2221 if (strcmp(cmd->name, p->name) == 0) {
2226 list_add(&cmd->list, &ftrace_commands);
2228 mutex_unlock(&ftrace_cmd_mutex);
2233 int unregister_ftrace_command(struct ftrace_func_command *cmd)
2235 struct ftrace_func_command *p, *n;
2238 mutex_lock(&ftrace_cmd_mutex);
2239 list_for_each_entry_safe(p, n, &ftrace_commands, list) {
2240 if (strcmp(cmd->name, p->name) == 0) {
2242 list_del_init(&p->list);
2247 mutex_unlock(&ftrace_cmd_mutex);
2252 static int ftrace_process_regex(char *buff, int len, int enable)
2254 char *func, *command, *next = buff;
2255 struct ftrace_func_command *p;
2258 func = strsep(&next, ":");
2261 ret = ftrace_match_records(func, len, enable);
2271 command = strsep(&next, ":");
2273 mutex_lock(&ftrace_cmd_mutex);
2274 list_for_each_entry(p, &ftrace_commands, list) {
2275 if (strcmp(p->name, command) == 0) {
2276 ret = p->func(func, command, next, enable);
2281 mutex_unlock(&ftrace_cmd_mutex);
2287 ftrace_regex_write(struct file *file, const char __user *ubuf,
2288 size_t cnt, loff_t *ppos, int enable)
2290 struct ftrace_iterator *iter;
2291 struct trace_parser *parser;
2297 mutex_lock(&ftrace_regex_lock);
2300 if (unlikely(ftrace_disabled))
2303 if (file->f_mode & FMODE_READ) {
2304 struct seq_file *m = file->private_data;
2307 iter = file->private_data;
2309 parser = &iter->parser;
2310 read = trace_get_user(parser, ubuf, cnt, ppos);
2312 if (read >= 0 && trace_parser_loaded(parser) &&
2313 !trace_parser_cont(parser)) {
2314 ret = ftrace_process_regex(parser->buffer,
2315 parser->idx, enable);
2316 trace_parser_clear(parser);
2323 mutex_unlock(&ftrace_regex_lock);
2329 ftrace_filter_write(struct file *file, const char __user *ubuf,
2330 size_t cnt, loff_t *ppos)
2332 return ftrace_regex_write(file, ubuf, cnt, ppos, 1);
2336 ftrace_notrace_write(struct file *file, const char __user *ubuf,
2337 size_t cnt, loff_t *ppos)
2339 return ftrace_regex_write(file, ubuf, cnt, ppos, 0);
2343 ftrace_set_regex(unsigned char *buf, int len, int reset, int enable)
2345 if (unlikely(ftrace_disabled))
2348 mutex_lock(&ftrace_regex_lock);
2350 ftrace_filter_reset(enable);
2352 ftrace_match_records(buf, len, enable);
2353 mutex_unlock(&ftrace_regex_lock);
2357 * ftrace_set_filter - set a function to filter on in ftrace
2358 * @buf - the string that holds the function filter text.
2359 * @len - the length of the string.
2360 * @reset - non zero to reset all filters before applying this filter.
2362 * Filters denote which functions should be enabled when tracing is enabled.
2363 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
2365 void ftrace_set_filter(unsigned char *buf, int len, int reset)
2367 ftrace_set_regex(buf, len, reset, 1);
2371 * ftrace_set_notrace - set a function to not trace in ftrace
2372 * @buf - the string that holds the function notrace text.
2373 * @len - the length of the string.
2374 * @reset - non zero to reset all filters before applying this filter.
2376 * Notrace Filters denote which functions should not be enabled when tracing
2377 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
2380 void ftrace_set_notrace(unsigned char *buf, int len, int reset)
2382 ftrace_set_regex(buf, len, reset, 0);
2386 * command line interface to allow users to set filters on boot up.
2388 #define FTRACE_FILTER_SIZE COMMAND_LINE_SIZE
2389 static char ftrace_notrace_buf[FTRACE_FILTER_SIZE] __initdata;
2390 static char ftrace_filter_buf[FTRACE_FILTER_SIZE] __initdata;
2392 static int __init set_ftrace_notrace(char *str)
2394 strncpy(ftrace_notrace_buf, str, FTRACE_FILTER_SIZE);
2397 __setup("ftrace_notrace=", set_ftrace_notrace);
2399 static int __init set_ftrace_filter(char *str)
2401 strncpy(ftrace_filter_buf, str, FTRACE_FILTER_SIZE);
2404 __setup("ftrace_filter=", set_ftrace_filter);
2406 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
2407 static char ftrace_graph_buf[FTRACE_FILTER_SIZE] __initdata;
2408 static int ftrace_set_func(unsigned long *array, int *idx, char *buffer);
2410 static int __init set_graph_function(char *str)
2412 strlcpy(ftrace_graph_buf, str, FTRACE_FILTER_SIZE);
2415 __setup("ftrace_graph_filter=", set_graph_function);
2417 static void __init set_ftrace_early_graph(char *buf)
2423 func = strsep(&buf, ",");
2424 /* we allow only one expression at a time */
2425 ret = ftrace_set_func(ftrace_graph_funcs, &ftrace_graph_count,
2428 printk(KERN_DEBUG "ftrace: function %s not "
2429 "traceable\n", func);
2432 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
2434 static void __init set_ftrace_early_filter(char *buf, int enable)
2439 func = strsep(&buf, ",");
2440 ftrace_set_regex(func, strlen(func), 0, enable);
2444 static void __init set_ftrace_early_filters(void)
2446 if (ftrace_filter_buf[0])
2447 set_ftrace_early_filter(ftrace_filter_buf, 1);
2448 if (ftrace_notrace_buf[0])
2449 set_ftrace_early_filter(ftrace_notrace_buf, 0);
2450 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
2451 if (ftrace_graph_buf[0])
2452 set_ftrace_early_graph(ftrace_graph_buf);
2453 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
2457 ftrace_regex_release(struct inode *inode, struct file *file, int enable)
2459 struct seq_file *m = (struct seq_file *)file->private_data;
2460 struct ftrace_iterator *iter;
2461 struct trace_parser *parser;
2463 mutex_lock(&ftrace_regex_lock);
2464 if (file->f_mode & FMODE_READ) {
2467 seq_release(inode, file);
2469 iter = file->private_data;
2471 parser = &iter->parser;
2472 if (trace_parser_loaded(parser)) {
2473 parser->buffer[parser->idx] = 0;
2474 ftrace_match_records(parser->buffer, parser->idx, enable);
2477 trace_parser_put(parser);
2480 if (file->f_mode & FMODE_WRITE) {
2481 mutex_lock(&ftrace_lock);
2482 if (ftrace_start_up && ftrace_enabled)
2483 ftrace_run_update_code(FTRACE_ENABLE_CALLS);
2484 mutex_unlock(&ftrace_lock);
2487 mutex_unlock(&ftrace_regex_lock);
2492 ftrace_filter_release(struct inode *inode, struct file *file)
2494 return ftrace_regex_release(inode, file, 1);
2498 ftrace_notrace_release(struct inode *inode, struct file *file)
2500 return ftrace_regex_release(inode, file, 0);
2503 static const struct file_operations ftrace_avail_fops = {
2504 .open = ftrace_avail_open,
2506 .llseek = seq_lseek,
2507 .release = seq_release_private,
2510 static const struct file_operations ftrace_filter_fops = {
2511 .open = ftrace_filter_open,
2513 .write = ftrace_filter_write,
2514 .llseek = ftrace_regex_lseek,
2515 .release = ftrace_filter_release,
2518 static const struct file_operations ftrace_notrace_fops = {
2519 .open = ftrace_notrace_open,
2521 .write = ftrace_notrace_write,
2522 .llseek = ftrace_regex_lseek,
2523 .release = ftrace_notrace_release,
2526 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
2528 static DEFINE_MUTEX(graph_lock);
2530 int ftrace_graph_count;
2531 int ftrace_graph_filter_enabled;
2532 unsigned long ftrace_graph_funcs[FTRACE_GRAPH_MAX_FUNCS] __read_mostly;
2535 __g_next(struct seq_file *m, loff_t *pos)
2537 if (*pos >= ftrace_graph_count)
2539 return &ftrace_graph_funcs[*pos];
2543 g_next(struct seq_file *m, void *v, loff_t *pos)
2546 return __g_next(m, pos);
2549 static void *g_start(struct seq_file *m, loff_t *pos)
2551 mutex_lock(&graph_lock);
2553 /* Nothing, tell g_show to print all functions are enabled */
2554 if (!ftrace_graph_filter_enabled && !*pos)
2557 return __g_next(m, pos);
2560 static void g_stop(struct seq_file *m, void *p)
2562 mutex_unlock(&graph_lock);
2565 static int g_show(struct seq_file *m, void *v)
2567 unsigned long *ptr = v;
2572 if (ptr == (unsigned long *)1) {
2573 seq_printf(m, "#### all functions enabled ####\n");
2577 seq_printf(m, "%ps\n", (void *)*ptr);
2582 static const struct seq_operations ftrace_graph_seq_ops = {
2590 ftrace_graph_open(struct inode *inode, struct file *file)
2594 if (unlikely(ftrace_disabled))
2597 mutex_lock(&graph_lock);
2598 if ((file->f_mode & FMODE_WRITE) &&
2599 (file->f_flags & O_TRUNC)) {
2600 ftrace_graph_filter_enabled = 0;
2601 ftrace_graph_count = 0;
2602 memset(ftrace_graph_funcs, 0, sizeof(ftrace_graph_funcs));
2604 mutex_unlock(&graph_lock);
2606 if (file->f_mode & FMODE_READ)
2607 ret = seq_open(file, &ftrace_graph_seq_ops);
2613 ftrace_graph_release(struct inode *inode, struct file *file)
2615 if (file->f_mode & FMODE_READ)
2616 seq_release(inode, file);
2621 ftrace_set_func(unsigned long *array, int *idx, char *buffer)
2623 struct dyn_ftrace *rec;
2624 struct ftrace_page *pg;
2633 type = filter_parse_regex(buffer, strlen(buffer), &search, ¬);
2634 if (!not && *idx >= FTRACE_GRAPH_MAX_FUNCS)
2637 search_len = strlen(search);
2639 mutex_lock(&ftrace_lock);
2641 if (unlikely(ftrace_disabled)) {
2642 mutex_unlock(&ftrace_lock);
2646 do_for_each_ftrace_rec(pg, rec) {
2648 if (rec->flags & FTRACE_FL_FREE)
2651 if (ftrace_match_record(rec, NULL, search, search_len, type)) {
2652 /* if it is in the array */
2654 for (i = 0; i < *idx; i++) {
2655 if (array[i] == rec->ip) {
2664 array[(*idx)++] = rec->ip;
2665 if (*idx >= FTRACE_GRAPH_MAX_FUNCS)
2670 array[i] = array[--(*idx)];
2676 } while_for_each_ftrace_rec();
2678 mutex_unlock(&ftrace_lock);
2683 ftrace_graph_filter_enabled = 1;
2688 ftrace_graph_write(struct file *file, const char __user *ubuf,
2689 size_t cnt, loff_t *ppos)
2691 struct trace_parser parser;
2697 mutex_lock(&graph_lock);
2699 if (trace_parser_get_init(&parser, FTRACE_BUFF_MAX)) {
2704 read = trace_get_user(&parser, ubuf, cnt, ppos);
2706 if (read >= 0 && trace_parser_loaded((&parser))) {
2707 parser.buffer[parser.idx] = 0;
2709 /* we allow only one expression at a time */
2710 ret = ftrace_set_func(ftrace_graph_funcs, &ftrace_graph_count,
2719 trace_parser_put(&parser);
2721 mutex_unlock(&graph_lock);
2726 static const struct file_operations ftrace_graph_fops = {
2727 .open = ftrace_graph_open,
2729 .write = ftrace_graph_write,
2730 .release = ftrace_graph_release,
2731 .llseek = seq_lseek,
2733 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
2735 static __init int ftrace_init_dyn_debugfs(struct dentry *d_tracer)
2738 trace_create_file("available_filter_functions", 0444,
2739 d_tracer, NULL, &ftrace_avail_fops);
2741 trace_create_file("set_ftrace_filter", 0644, d_tracer,
2742 NULL, &ftrace_filter_fops);
2744 trace_create_file("set_ftrace_notrace", 0644, d_tracer,
2745 NULL, &ftrace_notrace_fops);
2747 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
2748 trace_create_file("set_graph_function", 0444, d_tracer,
2750 &ftrace_graph_fops);
2751 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
2756 static int ftrace_process_locs(struct module *mod,
2757 unsigned long *start,
2763 mutex_lock(&ftrace_lock);
2766 addr = ftrace_call_adjust(*p++);
2768 * Some architecture linkers will pad between
2769 * the different mcount_loc sections of different
2770 * object files to satisfy alignments.
2771 * Skip any NULL pointers.
2775 ftrace_record_ip(addr);
2778 ftrace_update_code(mod);
2779 mutex_unlock(&ftrace_lock);
2784 #ifdef CONFIG_MODULES
2785 void ftrace_release_mod(struct module *mod)
2787 struct dyn_ftrace *rec;
2788 struct ftrace_page *pg;
2790 mutex_lock(&ftrace_lock);
2792 if (ftrace_disabled)
2795 do_for_each_ftrace_rec(pg, rec) {
2796 if (within_module_core(rec->ip, mod)) {
2798 * rec->ip is changed in ftrace_free_rec()
2799 * It should not between s and e if record was freed.
2801 FTRACE_WARN_ON(rec->flags & FTRACE_FL_FREE);
2802 ftrace_free_rec(rec);
2804 } while_for_each_ftrace_rec();
2806 mutex_unlock(&ftrace_lock);
2809 static void ftrace_init_module(struct module *mod,
2810 unsigned long *start, unsigned long *end)
2812 if (ftrace_disabled || start == end)
2814 ftrace_process_locs(mod, start, end);
2817 static int ftrace_module_notify(struct notifier_block *self,
2818 unsigned long val, void *data)
2820 struct module *mod = data;
2823 case MODULE_STATE_COMING:
2824 ftrace_init_module(mod, mod->ftrace_callsites,
2825 mod->ftrace_callsites +
2826 mod->num_ftrace_callsites);
2828 case MODULE_STATE_GOING:
2829 ftrace_release_mod(mod);
2836 static int ftrace_module_notify(struct notifier_block *self,
2837 unsigned long val, void *data)
2841 #endif /* CONFIG_MODULES */
2843 struct notifier_block ftrace_module_nb = {
2844 .notifier_call = ftrace_module_notify,
2848 extern unsigned long __start_mcount_loc[];
2849 extern unsigned long __stop_mcount_loc[];
2851 void __init ftrace_init(void)
2853 unsigned long count, addr, flags;
2856 /* Keep the ftrace pointer to the stub */
2857 addr = (unsigned long)ftrace_stub;
2859 local_irq_save(flags);
2860 ftrace_dyn_arch_init(&addr);
2861 local_irq_restore(flags);
2863 /* ftrace_dyn_arch_init places the return code in addr */
2867 count = __stop_mcount_loc - __start_mcount_loc;
2869 ret = ftrace_dyn_table_alloc(count);
2873 last_ftrace_enabled = ftrace_enabled = 1;
2875 ret = ftrace_process_locs(NULL,
2879 ret = register_module_notifier(&ftrace_module_nb);
2881 pr_warning("Failed to register trace ftrace module notifier\n");
2883 set_ftrace_early_filters();
2887 ftrace_disabled = 1;
2892 static int __init ftrace_nodyn_init(void)
2897 device_initcall(ftrace_nodyn_init);
2899 static inline int ftrace_init_dyn_debugfs(struct dentry *d_tracer) { return 0; }
2900 static inline void ftrace_startup_enable(int command) { }
2901 /* Keep as macros so we do not need to define the commands */
2902 # define ftrace_startup(command) do { } while (0)
2903 # define ftrace_shutdown(command) do { } while (0)
2904 # define ftrace_startup_sysctl() do { } while (0)
2905 # define ftrace_shutdown_sysctl() do { } while (0)
2906 #endif /* CONFIG_DYNAMIC_FTRACE */
2908 static void clear_ftrace_swapper(void)
2910 struct task_struct *p;
2914 for_each_online_cpu(cpu) {
2916 clear_tsk_trace_trace(p);
2921 static void set_ftrace_swapper(void)
2923 struct task_struct *p;
2927 for_each_online_cpu(cpu) {
2929 set_tsk_trace_trace(p);
2934 static void clear_ftrace_pid(struct pid *pid)
2936 struct task_struct *p;
2939 do_each_pid_task(pid, PIDTYPE_PID, p) {
2940 clear_tsk_trace_trace(p);
2941 } while_each_pid_task(pid, PIDTYPE_PID, p);
2947 static void set_ftrace_pid(struct pid *pid)
2949 struct task_struct *p;
2952 do_each_pid_task(pid, PIDTYPE_PID, p) {
2953 set_tsk_trace_trace(p);
2954 } while_each_pid_task(pid, PIDTYPE_PID, p);
2958 static void clear_ftrace_pid_task(struct pid *pid)
2960 if (pid == ftrace_swapper_pid)
2961 clear_ftrace_swapper();
2963 clear_ftrace_pid(pid);
2966 static void set_ftrace_pid_task(struct pid *pid)
2968 if (pid == ftrace_swapper_pid)
2969 set_ftrace_swapper();
2971 set_ftrace_pid(pid);
2974 static int ftrace_pid_add(int p)
2977 struct ftrace_pid *fpid;
2980 mutex_lock(&ftrace_lock);
2983 pid = ftrace_swapper_pid;
2985 pid = find_get_pid(p);
2992 list_for_each_entry(fpid, &ftrace_pids, list)
2993 if (fpid->pid == pid)
2998 fpid = kmalloc(sizeof(*fpid), GFP_KERNEL);
3002 list_add(&fpid->list, &ftrace_pids);
3005 set_ftrace_pid_task(pid);
3007 ftrace_update_pid_func();
3008 ftrace_startup_enable(0);
3010 mutex_unlock(&ftrace_lock);
3014 if (pid != ftrace_swapper_pid)
3018 mutex_unlock(&ftrace_lock);
3022 static void ftrace_pid_reset(void)
3024 struct ftrace_pid *fpid, *safe;
3026 mutex_lock(&ftrace_lock);
3027 list_for_each_entry_safe(fpid, safe, &ftrace_pids, list) {
3028 struct pid *pid = fpid->pid;
3030 clear_ftrace_pid_task(pid);
3032 list_del(&fpid->list);
3036 ftrace_update_pid_func();
3037 ftrace_startup_enable(0);
3039 mutex_unlock(&ftrace_lock);
3042 static void *fpid_start(struct seq_file *m, loff_t *pos)
3044 mutex_lock(&ftrace_lock);
3046 if (list_empty(&ftrace_pids) && (!*pos))
3049 return seq_list_start(&ftrace_pids, *pos);
3052 static void *fpid_next(struct seq_file *m, void *v, loff_t *pos)
3057 return seq_list_next(v, &ftrace_pids, pos);
3060 static void fpid_stop(struct seq_file *m, void *p)
3062 mutex_unlock(&ftrace_lock);
3065 static int fpid_show(struct seq_file *m, void *v)
3067 const struct ftrace_pid *fpid = list_entry(v, struct ftrace_pid, list);
3069 if (v == (void *)1) {
3070 seq_printf(m, "no pid\n");
3074 if (fpid->pid == ftrace_swapper_pid)
3075 seq_printf(m, "swapper tasks\n");
3077 seq_printf(m, "%u\n", pid_vnr(fpid->pid));
3082 static const struct seq_operations ftrace_pid_sops = {
3083 .start = fpid_start,
3090 ftrace_pid_open(struct inode *inode, struct file *file)
3094 if ((file->f_mode & FMODE_WRITE) &&
3095 (file->f_flags & O_TRUNC))
3098 if (file->f_mode & FMODE_READ)
3099 ret = seq_open(file, &ftrace_pid_sops);
3105 ftrace_pid_write(struct file *filp, const char __user *ubuf,
3106 size_t cnt, loff_t *ppos)
3112 if (cnt >= sizeof(buf))
3115 if (copy_from_user(&buf, ubuf, cnt))
3121 * Allow "echo > set_ftrace_pid" or "echo -n '' > set_ftrace_pid"
3122 * to clean the filter quietly.
3124 tmp = strstrip(buf);
3125 if (strlen(tmp) == 0)
3128 ret = strict_strtol(tmp, 10, &val);
3132 ret = ftrace_pid_add(val);
3134 return ret ? ret : cnt;
3138 ftrace_pid_release(struct inode *inode, struct file *file)
3140 if (file->f_mode & FMODE_READ)
3141 seq_release(inode, file);
3146 static const struct file_operations ftrace_pid_fops = {
3147 .open = ftrace_pid_open,
3148 .write = ftrace_pid_write,
3150 .llseek = seq_lseek,
3151 .release = ftrace_pid_release,
3154 static __init int ftrace_init_debugfs(void)
3156 struct dentry *d_tracer;
3158 d_tracer = tracing_init_dentry();
3162 ftrace_init_dyn_debugfs(d_tracer);
3164 trace_create_file("set_ftrace_pid", 0644, d_tracer,
3165 NULL, &ftrace_pid_fops);
3167 ftrace_profile_debugfs(d_tracer);
3171 fs_initcall(ftrace_init_debugfs);
3174 * ftrace_kill - kill ftrace
3176 * This function should be used by panic code. It stops ftrace
3177 * but in a not so nice way. If you need to simply kill ftrace
3178 * from a non-atomic section, use ftrace_kill.
3180 void ftrace_kill(void)
3182 ftrace_disabled = 1;
3184 clear_ftrace_function();
3188 * register_ftrace_function - register a function for profiling
3189 * @ops - ops structure that holds the function for profiling.
3191 * Register a function to be called by all functions in the
3194 * Note: @ops->func and all the functions it calls must be labeled
3195 * with "notrace", otherwise it will go into a
3198 int register_ftrace_function(struct ftrace_ops *ops)
3202 mutex_lock(&ftrace_lock);
3204 if (unlikely(ftrace_disabled))
3207 ret = __register_ftrace_function(ops);
3211 mutex_unlock(&ftrace_lock);
3216 * unregister_ftrace_function - unregister a function for profiling.
3217 * @ops - ops structure that holds the function to unregister
3219 * Unregister a function that was added to be called by ftrace profiling.
3221 int unregister_ftrace_function(struct ftrace_ops *ops)
3225 mutex_lock(&ftrace_lock);
3226 ret = __unregister_ftrace_function(ops);
3228 mutex_unlock(&ftrace_lock);
3234 ftrace_enable_sysctl(struct ctl_table *table, int write,
3235 void __user *buffer, size_t *lenp,
3240 mutex_lock(&ftrace_lock);
3242 if (unlikely(ftrace_disabled))
3245 ret = proc_dointvec(table, write, buffer, lenp, ppos);
3247 if (ret || !write || (last_ftrace_enabled == !!ftrace_enabled))
3250 last_ftrace_enabled = !!ftrace_enabled;
3252 if (ftrace_enabled) {
3254 ftrace_startup_sysctl();
3256 /* we are starting ftrace again */
3257 if (ftrace_list != &ftrace_list_end) {
3258 if (ftrace_list->next == &ftrace_list_end)
3259 ftrace_trace_function = ftrace_list->func;
3261 ftrace_trace_function = ftrace_list_func;
3265 /* stopping ftrace calls (just send to ftrace_stub) */
3266 ftrace_trace_function = ftrace_stub;
3268 ftrace_shutdown_sysctl();
3272 mutex_unlock(&ftrace_lock);
3276 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
3278 static int ftrace_graph_active;
3279 static struct notifier_block ftrace_suspend_notifier;
3281 int ftrace_graph_entry_stub(struct ftrace_graph_ent *trace)
3286 /* The callbacks that hook a function */
3287 trace_func_graph_ret_t ftrace_graph_return =
3288 (trace_func_graph_ret_t)ftrace_stub;
3289 trace_func_graph_ent_t ftrace_graph_entry = ftrace_graph_entry_stub;
3291 /* Try to assign a return stack array on FTRACE_RETSTACK_ALLOC_SIZE tasks. */
3292 static int alloc_retstack_tasklist(struct ftrace_ret_stack **ret_stack_list)
3296 unsigned long flags;
3297 int start = 0, end = FTRACE_RETSTACK_ALLOC_SIZE;
3298 struct task_struct *g, *t;
3300 for (i = 0; i < FTRACE_RETSTACK_ALLOC_SIZE; i++) {
3301 ret_stack_list[i] = kmalloc(FTRACE_RETFUNC_DEPTH
3302 * sizeof(struct ftrace_ret_stack),
3304 if (!ret_stack_list[i]) {
3312 read_lock_irqsave(&tasklist_lock, flags);
3313 do_each_thread(g, t) {
3319 if (t->ret_stack == NULL) {
3320 atomic_set(&t->tracing_graph_pause, 0);
3321 atomic_set(&t->trace_overrun, 0);
3322 t->curr_ret_stack = -1;
3323 /* Make sure the tasks see the -1 first: */
3325 t->ret_stack = ret_stack_list[start++];
3327 } while_each_thread(g, t);
3330 read_unlock_irqrestore(&tasklist_lock, flags);
3332 for (i = start; i < end; i++)
3333 kfree(ret_stack_list[i]);
3338 ftrace_graph_probe_sched_switch(void *ignore,
3339 struct task_struct *prev, struct task_struct *next)
3341 unsigned long long timestamp;
3345 * Does the user want to count the time a function was asleep.
3346 * If so, do not update the time stamps.
3348 if (trace_flags & TRACE_ITER_SLEEP_TIME)
3351 timestamp = trace_clock_local();
3353 prev->ftrace_timestamp = timestamp;
3355 /* only process tasks that we timestamped */
3356 if (!next->ftrace_timestamp)
3360 * Update all the counters in next to make up for the
3361 * time next was sleeping.
3363 timestamp -= next->ftrace_timestamp;
3365 for (index = next->curr_ret_stack; index >= 0; index--)
3366 next->ret_stack[index].calltime += timestamp;
3369 /* Allocate a return stack for each task */
3370 static int start_graph_tracing(void)
3372 struct ftrace_ret_stack **ret_stack_list;
3375 ret_stack_list = kmalloc(FTRACE_RETSTACK_ALLOC_SIZE *
3376 sizeof(struct ftrace_ret_stack *),
3379 if (!ret_stack_list)
3382 /* The cpu_boot init_task->ret_stack will never be freed */
3383 for_each_online_cpu(cpu) {
3384 if (!idle_task(cpu)->ret_stack)
3385 ftrace_graph_init_idle_task(idle_task(cpu), cpu);
3389 ret = alloc_retstack_tasklist(ret_stack_list);
3390 } while (ret == -EAGAIN);
3393 ret = register_trace_sched_switch(ftrace_graph_probe_sched_switch, NULL);
3395 pr_info("ftrace_graph: Couldn't activate tracepoint"
3396 " probe to kernel_sched_switch\n");
3399 kfree(ret_stack_list);
3404 * Hibernation protection.
3405 * The state of the current task is too much unstable during
3406 * suspend/restore to disk. We want to protect against that.
3409 ftrace_suspend_notifier_call(struct notifier_block *bl, unsigned long state,
3413 case PM_HIBERNATION_PREPARE:
3414 pause_graph_tracing();
3417 case PM_POST_HIBERNATION:
3418 unpause_graph_tracing();
3424 int register_ftrace_graph(trace_func_graph_ret_t retfunc,
3425 trace_func_graph_ent_t entryfunc)
3429 mutex_lock(&ftrace_lock);
3431 /* we currently allow only one tracer registered at a time */
3432 if (ftrace_graph_active) {
3437 ftrace_suspend_notifier.notifier_call = ftrace_suspend_notifier_call;
3438 register_pm_notifier(&ftrace_suspend_notifier);
3440 ftrace_graph_active++;
3441 ret = start_graph_tracing();
3443 ftrace_graph_active--;
3447 ftrace_graph_return = retfunc;
3448 ftrace_graph_entry = entryfunc;
3450 ftrace_startup(FTRACE_START_FUNC_RET);
3453 mutex_unlock(&ftrace_lock);
3457 void unregister_ftrace_graph(void)
3459 mutex_lock(&ftrace_lock);
3461 if (unlikely(!ftrace_graph_active))
3464 ftrace_graph_active--;
3465 ftrace_graph_return = (trace_func_graph_ret_t)ftrace_stub;
3466 ftrace_graph_entry = ftrace_graph_entry_stub;
3467 ftrace_shutdown(FTRACE_STOP_FUNC_RET);
3468 unregister_pm_notifier(&ftrace_suspend_notifier);
3469 unregister_trace_sched_switch(ftrace_graph_probe_sched_switch, NULL);
3472 mutex_unlock(&ftrace_lock);
3475 static DEFINE_PER_CPU(struct ftrace_ret_stack *, idle_ret_stack);
3478 graph_init_task(struct task_struct *t, struct ftrace_ret_stack *ret_stack)
3480 atomic_set(&t->tracing_graph_pause, 0);
3481 atomic_set(&t->trace_overrun, 0);
3482 t->ftrace_timestamp = 0;
3483 /* make curr_ret_stack visible before we add the ret_stack */
3485 t->ret_stack = ret_stack;
3489 * Allocate a return stack for the idle task. May be the first
3490 * time through, or it may be done by CPU hotplug online.
3492 void ftrace_graph_init_idle_task(struct task_struct *t, int cpu)
3494 t->curr_ret_stack = -1;
3496 * The idle task has no parent, it either has its own
3497 * stack or no stack at all.
3500 WARN_ON(t->ret_stack != per_cpu(idle_ret_stack, cpu));
3502 if (ftrace_graph_active) {
3503 struct ftrace_ret_stack *ret_stack;
3505 ret_stack = per_cpu(idle_ret_stack, cpu);
3507 ret_stack = kmalloc(FTRACE_RETFUNC_DEPTH
3508 * sizeof(struct ftrace_ret_stack),
3512 per_cpu(idle_ret_stack, cpu) = ret_stack;
3514 graph_init_task(t, ret_stack);
3518 /* Allocate a return stack for newly created task */
3519 void ftrace_graph_init_task(struct task_struct *t)
3521 /* Make sure we do not use the parent ret_stack */
3522 t->ret_stack = NULL;
3523 t->curr_ret_stack = -1;
3525 if (ftrace_graph_active) {
3526 struct ftrace_ret_stack *ret_stack;
3528 ret_stack = kmalloc(FTRACE_RETFUNC_DEPTH
3529 * sizeof(struct ftrace_ret_stack),
3533 graph_init_task(t, ret_stack);
3537 void ftrace_graph_exit_task(struct task_struct *t)
3539 struct ftrace_ret_stack *ret_stack = t->ret_stack;
3541 t->ret_stack = NULL;
3542 /* NULL must become visible to IRQs before we free it: */
3548 void ftrace_graph_stop(void)