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 struct hlist_head filter_buckets[1 << FTRACE_HASH_MAX_BITS];
887 static struct ftrace_hash filter_hash = {
888 .size_bits = FTRACE_HASH_MAX_BITS,
889 .buckets = filter_buckets,
892 static struct dyn_ftrace *ftrace_new_addrs;
894 static DEFINE_MUTEX(ftrace_regex_lock);
897 struct ftrace_page *next;
899 struct dyn_ftrace records[];
902 #define ENTRIES_PER_PAGE \
903 ((PAGE_SIZE - sizeof(struct ftrace_page)) / sizeof(struct dyn_ftrace))
905 /* estimate from running different kernels */
906 #define NR_TO_INIT 10000
908 static struct ftrace_page *ftrace_pages_start;
909 static struct ftrace_page *ftrace_pages;
911 static struct dyn_ftrace *ftrace_free_records;
913 static struct ftrace_func_entry *
914 ftrace_lookup_ip(struct ftrace_hash *hash, unsigned long ip)
917 struct ftrace_func_entry *entry;
918 struct hlist_head *hhd;
919 struct hlist_node *n;
924 if (hash->size_bits > 0)
925 key = hash_long(ip, hash->size_bits);
929 hhd = &hash->buckets[key];
931 hlist_for_each_entry_rcu(entry, n, hhd, hlist) {
938 static int add_hash_entry(struct ftrace_hash *hash, unsigned long ip)
940 struct ftrace_func_entry *entry;
941 struct hlist_head *hhd;
944 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
949 key = hash_long(ip, hash->size_bits);
954 hhd = &hash->buckets[key];
955 hlist_add_head(&entry->hlist, hhd);
962 remove_hash_entry(struct ftrace_hash *hash,
963 struct ftrace_func_entry *entry)
965 hlist_del(&entry->hlist);
970 static void ftrace_hash_clear(struct ftrace_hash *hash)
972 struct hlist_head *hhd;
973 struct hlist_node *tp, *tn;
974 struct ftrace_func_entry *entry;
975 int size = 1 << hash->size_bits;
978 for (i = 0; i < size; i++) {
979 hhd = &hash->buckets[i];
980 hlist_for_each_entry_safe(entry, tp, tn, hhd, hlist)
981 remove_hash_entry(hash, entry);
983 FTRACE_WARN_ON(hash->count);
987 * This is a double for. Do not use 'break' to break out of the loop,
988 * you must use a goto.
990 #define do_for_each_ftrace_rec(pg, rec) \
991 for (pg = ftrace_pages_start; pg; pg = pg->next) { \
993 for (_____i = 0; _____i < pg->index; _____i++) { \
994 rec = &pg->records[_____i];
996 #define while_for_each_ftrace_rec() \
1000 static void ftrace_free_rec(struct dyn_ftrace *rec)
1002 rec->freelist = ftrace_free_records;
1003 ftrace_free_records = rec;
1004 rec->flags |= FTRACE_FL_FREE;
1007 static struct dyn_ftrace *ftrace_alloc_dyn_node(unsigned long ip)
1009 struct dyn_ftrace *rec;
1011 /* First check for freed records */
1012 if (ftrace_free_records) {
1013 rec = ftrace_free_records;
1015 if (unlikely(!(rec->flags & FTRACE_FL_FREE))) {
1016 FTRACE_WARN_ON_ONCE(1);
1017 ftrace_free_records = NULL;
1021 ftrace_free_records = rec->freelist;
1022 memset(rec, 0, sizeof(*rec));
1026 if (ftrace_pages->index == ENTRIES_PER_PAGE) {
1027 if (!ftrace_pages->next) {
1028 /* allocate another page */
1029 ftrace_pages->next =
1030 (void *)get_zeroed_page(GFP_KERNEL);
1031 if (!ftrace_pages->next)
1034 ftrace_pages = ftrace_pages->next;
1037 return &ftrace_pages->records[ftrace_pages->index++];
1040 static struct dyn_ftrace *
1041 ftrace_record_ip(unsigned long ip)
1043 struct dyn_ftrace *rec;
1045 if (ftrace_disabled)
1048 rec = ftrace_alloc_dyn_node(ip);
1053 rec->newlist = ftrace_new_addrs;
1054 ftrace_new_addrs = rec;
1059 static void print_ip_ins(const char *fmt, unsigned char *p)
1063 printk(KERN_CONT "%s", fmt);
1065 for (i = 0; i < MCOUNT_INSN_SIZE; i++)
1066 printk(KERN_CONT "%s%02x", i ? ":" : "", p[i]);
1069 static void ftrace_bug(int failed, unsigned long ip)
1073 FTRACE_WARN_ON_ONCE(1);
1074 pr_info("ftrace faulted on modifying ");
1078 FTRACE_WARN_ON_ONCE(1);
1079 pr_info("ftrace failed to modify ");
1081 print_ip_ins(" actual: ", (unsigned char *)ip);
1082 printk(KERN_CONT "\n");
1085 FTRACE_WARN_ON_ONCE(1);
1086 pr_info("ftrace faulted on writing ");
1090 FTRACE_WARN_ON_ONCE(1);
1091 pr_info("ftrace faulted on unknown error ");
1097 /* Return 1 if the address range is reserved for ftrace */
1098 int ftrace_text_reserved(void *start, void *end)
1100 struct dyn_ftrace *rec;
1101 struct ftrace_page *pg;
1103 do_for_each_ftrace_rec(pg, rec) {
1104 if (rec->ip <= (unsigned long)end &&
1105 rec->ip + MCOUNT_INSN_SIZE > (unsigned long)start)
1107 } while_for_each_ftrace_rec();
1113 __ftrace_replace_code(struct dyn_ftrace *rec, int enable)
1115 unsigned long ftrace_addr;
1116 unsigned long flag = 0UL;
1118 ftrace_addr = (unsigned long)FTRACE_ADDR;
1121 * If this record is not to be traced or we want to disable it,
1124 * If we want to enable it and filtering is off, then enable it.
1126 * If we want to enable it and filtering is on, enable it only if
1129 if (enable && !ftrace_lookup_ip(¬race_hash, rec->ip)) {
1130 if (!filter_hash.count || ftrace_lookup_ip(&filter_hash, rec->ip))
1131 flag = FTRACE_FL_ENABLED;
1134 /* If the state of this record hasn't changed, then do nothing */
1135 if ((rec->flags & FTRACE_FL_ENABLED) == flag)
1139 rec->flags |= FTRACE_FL_ENABLED;
1140 return ftrace_make_call(rec, ftrace_addr);
1143 rec->flags &= ~FTRACE_FL_ENABLED;
1144 return ftrace_make_nop(NULL, rec, ftrace_addr);
1147 static void ftrace_replace_code(int enable)
1149 struct dyn_ftrace *rec;
1150 struct ftrace_page *pg;
1153 if (unlikely(ftrace_disabled))
1156 do_for_each_ftrace_rec(pg, rec) {
1157 /* Skip over free records */
1158 if (rec->flags & FTRACE_FL_FREE)
1161 failed = __ftrace_replace_code(rec, enable);
1163 ftrace_bug(failed, rec->ip);
1164 /* Stop processing */
1167 } while_for_each_ftrace_rec();
1171 ftrace_code_disable(struct module *mod, struct dyn_ftrace *rec)
1178 if (unlikely(ftrace_disabled))
1181 ret = ftrace_make_nop(mod, rec, MCOUNT_ADDR);
1183 ftrace_bug(ret, ip);
1190 * archs can override this function if they must do something
1191 * before the modifying code is performed.
1193 int __weak ftrace_arch_code_modify_prepare(void)
1199 * archs can override this function if they must do something
1200 * after the modifying code is performed.
1202 int __weak ftrace_arch_code_modify_post_process(void)
1207 static int __ftrace_modify_code(void *data)
1209 int *command = data;
1211 if (*command & FTRACE_ENABLE_CALLS)
1212 ftrace_replace_code(1);
1213 else if (*command & FTRACE_DISABLE_CALLS)
1214 ftrace_replace_code(0);
1216 if (*command & FTRACE_UPDATE_TRACE_FUNC)
1217 ftrace_update_ftrace_func(ftrace_trace_function);
1219 if (*command & FTRACE_START_FUNC_RET)
1220 ftrace_enable_ftrace_graph_caller();
1221 else if (*command & FTRACE_STOP_FUNC_RET)
1222 ftrace_disable_ftrace_graph_caller();
1227 static void ftrace_run_update_code(int command)
1231 ret = ftrace_arch_code_modify_prepare();
1232 FTRACE_WARN_ON(ret);
1236 stop_machine(__ftrace_modify_code, &command, NULL);
1238 ret = ftrace_arch_code_modify_post_process();
1239 FTRACE_WARN_ON(ret);
1242 static ftrace_func_t saved_ftrace_func;
1243 static int ftrace_start_up;
1245 static void ftrace_startup_enable(int command)
1247 if (saved_ftrace_func != ftrace_trace_function) {
1248 saved_ftrace_func = ftrace_trace_function;
1249 command |= FTRACE_UPDATE_TRACE_FUNC;
1252 if (!command || !ftrace_enabled)
1255 ftrace_run_update_code(command);
1258 static void ftrace_startup(int command)
1260 if (unlikely(ftrace_disabled))
1264 command |= FTRACE_ENABLE_CALLS;
1266 ftrace_startup_enable(command);
1269 static void ftrace_shutdown(int command)
1271 if (unlikely(ftrace_disabled))
1276 * Just warn in case of unbalance, no need to kill ftrace, it's not
1277 * critical but the ftrace_call callers may be never nopped again after
1278 * further ftrace uses.
1280 WARN_ON_ONCE(ftrace_start_up < 0);
1282 if (!ftrace_start_up)
1283 command |= FTRACE_DISABLE_CALLS;
1285 if (saved_ftrace_func != ftrace_trace_function) {
1286 saved_ftrace_func = ftrace_trace_function;
1287 command |= FTRACE_UPDATE_TRACE_FUNC;
1290 if (!command || !ftrace_enabled)
1293 ftrace_run_update_code(command);
1296 static void ftrace_startup_sysctl(void)
1298 if (unlikely(ftrace_disabled))
1301 /* Force update next time */
1302 saved_ftrace_func = NULL;
1303 /* ftrace_start_up is true if we want ftrace running */
1304 if (ftrace_start_up)
1305 ftrace_run_update_code(FTRACE_ENABLE_CALLS);
1308 static void ftrace_shutdown_sysctl(void)
1310 if (unlikely(ftrace_disabled))
1313 /* ftrace_start_up is true if ftrace is running */
1314 if (ftrace_start_up)
1315 ftrace_run_update_code(FTRACE_DISABLE_CALLS);
1318 static cycle_t ftrace_update_time;
1319 static unsigned long ftrace_update_cnt;
1320 unsigned long ftrace_update_tot_cnt;
1322 static int ftrace_update_code(struct module *mod)
1324 struct dyn_ftrace *p;
1325 cycle_t start, stop;
1327 start = ftrace_now(raw_smp_processor_id());
1328 ftrace_update_cnt = 0;
1330 while (ftrace_new_addrs) {
1332 /* If something went wrong, bail without enabling anything */
1333 if (unlikely(ftrace_disabled))
1336 p = ftrace_new_addrs;
1337 ftrace_new_addrs = p->newlist;
1341 * Do the initial record conversion from mcount jump
1342 * to the NOP instructions.
1344 if (!ftrace_code_disable(mod, p)) {
1350 ftrace_update_cnt++;
1353 * If the tracing is enabled, go ahead and enable the record.
1355 * The reason not to enable the record immediatelly is the
1356 * inherent check of ftrace_make_nop/ftrace_make_call for
1357 * correct previous instructions. Making first the NOP
1358 * conversion puts the module to the correct state, thus
1359 * passing the ftrace_make_call check.
1361 if (ftrace_start_up) {
1362 int failed = __ftrace_replace_code(p, 1);
1364 ftrace_bug(failed, p->ip);
1370 stop = ftrace_now(raw_smp_processor_id());
1371 ftrace_update_time = stop - start;
1372 ftrace_update_tot_cnt += ftrace_update_cnt;
1377 static int __init ftrace_dyn_table_alloc(unsigned long num_to_init)
1379 struct ftrace_page *pg;
1383 /* allocate a few pages */
1384 ftrace_pages_start = (void *)get_zeroed_page(GFP_KERNEL);
1385 if (!ftrace_pages_start)
1389 * Allocate a few more pages.
1391 * TODO: have some parser search vmlinux before
1392 * final linking to find all calls to ftrace.
1394 * a) know how many pages to allocate.
1396 * b) set up the table then.
1398 * The dynamic code is still necessary for
1402 pg = ftrace_pages = ftrace_pages_start;
1404 cnt = num_to_init / ENTRIES_PER_PAGE;
1405 pr_info("ftrace: allocating %ld entries in %d pages\n",
1406 num_to_init, cnt + 1);
1408 for (i = 0; i < cnt; i++) {
1409 pg->next = (void *)get_zeroed_page(GFP_KERNEL);
1411 /* If we fail, we'll try later anyway */
1422 FTRACE_ITER_FILTER = (1 << 0),
1423 FTRACE_ITER_NOTRACE = (1 << 1),
1424 FTRACE_ITER_PRINTALL = (1 << 2),
1425 FTRACE_ITER_HASH = (1 << 3),
1428 #define FTRACE_BUFF_MAX (KSYM_SYMBOL_LEN+4) /* room for wildcards */
1430 struct ftrace_iterator {
1433 struct ftrace_page *pg;
1434 struct dyn_ftrace *func;
1435 struct ftrace_func_probe *probe;
1436 struct trace_parser parser;
1437 struct ftrace_hash *hash;
1444 t_hash_next(struct seq_file *m, loff_t *pos)
1446 struct ftrace_iterator *iter = m->private;
1447 struct hlist_node *hnd = NULL;
1448 struct hlist_head *hhd;
1454 hnd = &iter->probe->node;
1456 if (iter->hidx >= FTRACE_FUNC_HASHSIZE)
1459 hhd = &ftrace_func_hash[iter->hidx];
1461 if (hlist_empty(hhd)) {
1477 if (WARN_ON_ONCE(!hnd))
1480 iter->probe = hlist_entry(hnd, struct ftrace_func_probe, node);
1485 static void *t_hash_start(struct seq_file *m, loff_t *pos)
1487 struct ftrace_iterator *iter = m->private;
1491 if (iter->func_pos > *pos)
1495 for (l = 0; l <= (*pos - iter->func_pos); ) {
1496 p = t_hash_next(m, &l);
1503 /* Only set this if we have an item */
1504 iter->flags |= FTRACE_ITER_HASH;
1510 t_hash_show(struct seq_file *m, struct ftrace_iterator *iter)
1512 struct ftrace_func_probe *rec;
1515 if (WARN_ON_ONCE(!rec))
1518 if (rec->ops->print)
1519 return rec->ops->print(m, rec->ip, rec->ops, rec->data);
1521 seq_printf(m, "%ps:%ps", (void *)rec->ip, (void *)rec->ops->func);
1524 seq_printf(m, ":%p", rec->data);
1531 t_next(struct seq_file *m, void *v, loff_t *pos)
1533 struct ftrace_iterator *iter = m->private;
1534 struct dyn_ftrace *rec = NULL;
1536 if (unlikely(ftrace_disabled))
1539 if (iter->flags & FTRACE_ITER_HASH)
1540 return t_hash_next(m, pos);
1543 iter->pos = iter->func_pos = *pos;
1545 if (iter->flags & FTRACE_ITER_PRINTALL)
1546 return t_hash_start(m, pos);
1549 if (iter->idx >= iter->pg->index) {
1550 if (iter->pg->next) {
1551 iter->pg = iter->pg->next;
1556 rec = &iter->pg->records[iter->idx++];
1557 if ((rec->flags & FTRACE_FL_FREE) ||
1559 ((iter->flags & FTRACE_ITER_FILTER) &&
1560 !(ftrace_lookup_ip(&filter_hash, rec->ip))) ||
1562 ((iter->flags & FTRACE_ITER_NOTRACE) &&
1563 !ftrace_lookup_ip(¬race_hash, rec->ip))) {
1570 return t_hash_start(m, pos);
1577 static void reset_iter_read(struct ftrace_iterator *iter)
1581 iter->flags &= ~(FTRACE_ITER_PRINTALL & FTRACE_ITER_HASH);
1584 static void *t_start(struct seq_file *m, loff_t *pos)
1586 struct ftrace_iterator *iter = m->private;
1590 mutex_lock(&ftrace_lock);
1592 if (unlikely(ftrace_disabled))
1596 * If an lseek was done, then reset and start from beginning.
1598 if (*pos < iter->pos)
1599 reset_iter_read(iter);
1602 * For set_ftrace_filter reading, if we have the filter
1603 * off, we can short cut and just print out that all
1604 * functions are enabled.
1606 if (iter->flags & FTRACE_ITER_FILTER && !filter_hash.count) {
1608 return t_hash_start(m, pos);
1609 iter->flags |= FTRACE_ITER_PRINTALL;
1610 /* reset in case of seek/pread */
1611 iter->flags &= ~FTRACE_ITER_HASH;
1615 if (iter->flags & FTRACE_ITER_HASH)
1616 return t_hash_start(m, pos);
1619 * Unfortunately, we need to restart at ftrace_pages_start
1620 * every time we let go of the ftrace_mutex. This is because
1621 * those pointers can change without the lock.
1623 iter->pg = ftrace_pages_start;
1625 for (l = 0; l <= *pos; ) {
1626 p = t_next(m, p, &l);
1632 if (iter->flags & FTRACE_ITER_FILTER)
1633 return t_hash_start(m, pos);
1641 static void t_stop(struct seq_file *m, void *p)
1643 mutex_unlock(&ftrace_lock);
1646 static int t_show(struct seq_file *m, void *v)
1648 struct ftrace_iterator *iter = m->private;
1649 struct dyn_ftrace *rec;
1651 if (iter->flags & FTRACE_ITER_HASH)
1652 return t_hash_show(m, iter);
1654 if (iter->flags & FTRACE_ITER_PRINTALL) {
1655 seq_printf(m, "#### all functions enabled ####\n");
1664 seq_printf(m, "%ps\n", (void *)rec->ip);
1669 static const struct seq_operations show_ftrace_seq_ops = {
1677 ftrace_avail_open(struct inode *inode, struct file *file)
1679 struct ftrace_iterator *iter;
1682 if (unlikely(ftrace_disabled))
1685 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
1689 iter->pg = ftrace_pages_start;
1691 ret = seq_open(file, &show_ftrace_seq_ops);
1693 struct seq_file *m = file->private_data;
1703 static void ftrace_filter_reset(struct ftrace_hash *hash)
1705 mutex_lock(&ftrace_lock);
1706 ftrace_hash_clear(hash);
1707 mutex_unlock(&ftrace_lock);
1711 ftrace_regex_open(struct ftrace_hash *hash, int flag,
1712 struct inode *inode, struct file *file)
1714 struct ftrace_iterator *iter;
1717 if (unlikely(ftrace_disabled))
1720 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
1724 if (trace_parser_get_init(&iter->parser, FTRACE_BUFF_MAX)) {
1731 mutex_lock(&ftrace_regex_lock);
1732 if ((file->f_mode & FMODE_WRITE) &&
1733 (file->f_flags & O_TRUNC))
1734 ftrace_filter_reset(hash);
1736 if (file->f_mode & FMODE_READ) {
1737 iter->pg = ftrace_pages_start;
1740 ret = seq_open(file, &show_ftrace_seq_ops);
1742 struct seq_file *m = file->private_data;
1745 trace_parser_put(&iter->parser);
1749 file->private_data = iter;
1750 mutex_unlock(&ftrace_regex_lock);
1756 ftrace_filter_open(struct inode *inode, struct file *file)
1758 return ftrace_regex_open(&filter_hash, FTRACE_ITER_FILTER,
1763 ftrace_notrace_open(struct inode *inode, struct file *file)
1765 return ftrace_regex_open(¬race_hash, FTRACE_ITER_NOTRACE,
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 enter_record(struct ftrace_hash *hash, struct dyn_ftrace *rec, int not)
1813 struct ftrace_func_entry *entry;
1816 entry = ftrace_lookup_ip(hash, rec->ip);
1818 /* Do nothing if it doesn't exist */
1822 remove_hash_entry(hash, entry);
1824 /* Do nothing if it exists */
1828 ret = add_hash_entry(hash, rec->ip);
1834 ftrace_match_record(struct dyn_ftrace *rec, char *mod,
1835 char *regex, int len, int type)
1837 char str[KSYM_SYMBOL_LEN];
1840 kallsyms_lookup(rec->ip, NULL, NULL, &modname, str);
1843 /* module lookup requires matching the module */
1844 if (!modname || strcmp(modname, mod))
1847 /* blank search means to match all funcs in the mod */
1852 return ftrace_match(str, regex, len, type);
1856 match_records(struct ftrace_hash *hash, char *buff,
1857 int len, char *mod, int not)
1859 unsigned search_len = 0;
1860 struct ftrace_page *pg;
1861 struct dyn_ftrace *rec;
1862 int type = MATCH_FULL;
1863 char *search = buff;
1868 type = filter_parse_regex(buff, len, &search, ¬);
1869 search_len = strlen(search);
1872 mutex_lock(&ftrace_lock);
1874 if (unlikely(ftrace_disabled))
1877 do_for_each_ftrace_rec(pg, rec) {
1879 if (ftrace_match_record(rec, mod, search, search_len, type)) {
1880 ret = enter_record(hash, rec, not);
1887 } while_for_each_ftrace_rec();
1889 mutex_unlock(&ftrace_lock);
1895 ftrace_match_records(struct ftrace_hash *hash, char *buff, int len)
1897 return match_records(hash, buff, len, NULL, 0);
1901 ftrace_match_module_records(struct ftrace_hash *hash, char *buff, char *mod)
1905 /* blank or '*' mean the same */
1906 if (strcmp(buff, "*") == 0)
1909 /* handle the case of 'dont filter this module' */
1910 if (strcmp(buff, "!") == 0 || strcmp(buff, "!*") == 0) {
1915 return match_records(hash, buff, strlen(buff), mod, not);
1919 * We register the module command as a template to show others how
1920 * to register the a command as well.
1924 ftrace_mod_callback(char *func, char *cmd, char *param, int enable)
1926 struct ftrace_hash *hash;
1931 * cmd == 'mod' because we only registered this func
1932 * for the 'mod' ftrace_func_command.
1933 * But if you register one func with multiple commands,
1934 * you can tell which command was used by the cmd
1938 /* we must have a module name */
1942 mod = strsep(¶m, ":");
1947 hash = &filter_hash;
1949 hash = ¬race_hash;
1951 ret = ftrace_match_module_records(hash, func, mod);
1960 static struct ftrace_func_command ftrace_mod_cmd = {
1962 .func = ftrace_mod_callback,
1965 static int __init ftrace_mod_cmd_init(void)
1967 return register_ftrace_command(&ftrace_mod_cmd);
1969 device_initcall(ftrace_mod_cmd_init);
1972 function_trace_probe_call(unsigned long ip, unsigned long parent_ip)
1974 struct ftrace_func_probe *entry;
1975 struct hlist_head *hhd;
1976 struct hlist_node *n;
1979 key = hash_long(ip, FTRACE_HASH_BITS);
1981 hhd = &ftrace_func_hash[key];
1983 if (hlist_empty(hhd))
1987 * Disable preemption for these calls to prevent a RCU grace
1988 * period. This syncs the hash iteration and freeing of items
1989 * on the hash. rcu_read_lock is too dangerous here.
1991 preempt_disable_notrace();
1992 hlist_for_each_entry_rcu(entry, n, hhd, node) {
1993 if (entry->ip == ip)
1994 entry->ops->func(ip, parent_ip, &entry->data);
1996 preempt_enable_notrace();
1999 static struct ftrace_ops trace_probe_ops __read_mostly =
2001 .func = function_trace_probe_call,
2004 static int ftrace_probe_registered;
2006 static void __enable_ftrace_function_probe(void)
2010 if (ftrace_probe_registered)
2013 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
2014 struct hlist_head *hhd = &ftrace_func_hash[i];
2018 /* Nothing registered? */
2019 if (i == FTRACE_FUNC_HASHSIZE)
2022 __register_ftrace_function(&trace_probe_ops);
2024 ftrace_probe_registered = 1;
2027 static void __disable_ftrace_function_probe(void)
2031 if (!ftrace_probe_registered)
2034 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
2035 struct hlist_head *hhd = &ftrace_func_hash[i];
2040 /* no more funcs left */
2041 __unregister_ftrace_function(&trace_probe_ops);
2043 ftrace_probe_registered = 0;
2047 static void ftrace_free_entry_rcu(struct rcu_head *rhp)
2049 struct ftrace_func_probe *entry =
2050 container_of(rhp, struct ftrace_func_probe, rcu);
2052 if (entry->ops->free)
2053 entry->ops->free(&entry->data);
2059 register_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
2062 struct ftrace_func_probe *entry;
2063 struct ftrace_page *pg;
2064 struct dyn_ftrace *rec;
2070 type = filter_parse_regex(glob, strlen(glob), &search, ¬);
2071 len = strlen(search);
2073 /* we do not support '!' for function probes */
2077 mutex_lock(&ftrace_lock);
2079 if (unlikely(ftrace_disabled))
2082 do_for_each_ftrace_rec(pg, rec) {
2084 if (!ftrace_match_record(rec, NULL, search, len, type))
2087 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
2089 /* If we did not process any, then return error */
2100 * The caller might want to do something special
2101 * for each function we find. We call the callback
2102 * to give the caller an opportunity to do so.
2104 if (ops->callback) {
2105 if (ops->callback(rec->ip, &entry->data) < 0) {
2106 /* caller does not like this func */
2113 entry->ip = rec->ip;
2115 key = hash_long(entry->ip, FTRACE_HASH_BITS);
2116 hlist_add_head_rcu(&entry->node, &ftrace_func_hash[key]);
2118 } while_for_each_ftrace_rec();
2119 __enable_ftrace_function_probe();
2122 mutex_unlock(&ftrace_lock);
2128 PROBE_TEST_FUNC = 1,
2133 __unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
2134 void *data, int flags)
2136 struct ftrace_func_probe *entry;
2137 struct hlist_node *n, *tmp;
2138 char str[KSYM_SYMBOL_LEN];
2139 int type = MATCH_FULL;
2143 if (glob && (strcmp(glob, "*") == 0 || !strlen(glob)))
2148 type = filter_parse_regex(glob, strlen(glob), &search, ¬);
2149 len = strlen(search);
2151 /* we do not support '!' for function probes */
2156 mutex_lock(&ftrace_lock);
2157 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
2158 struct hlist_head *hhd = &ftrace_func_hash[i];
2160 hlist_for_each_entry_safe(entry, n, tmp, hhd, node) {
2162 /* break up if statements for readability */
2163 if ((flags & PROBE_TEST_FUNC) && entry->ops != ops)
2166 if ((flags & PROBE_TEST_DATA) && entry->data != data)
2169 /* do this last, since it is the most expensive */
2171 kallsyms_lookup(entry->ip, NULL, NULL,
2173 if (!ftrace_match(str, glob, len, type))
2177 hlist_del(&entry->node);
2178 call_rcu(&entry->rcu, ftrace_free_entry_rcu);
2181 __disable_ftrace_function_probe();
2182 mutex_unlock(&ftrace_lock);
2186 unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
2189 __unregister_ftrace_function_probe(glob, ops, data,
2190 PROBE_TEST_FUNC | PROBE_TEST_DATA);
2194 unregister_ftrace_function_probe_func(char *glob, struct ftrace_probe_ops *ops)
2196 __unregister_ftrace_function_probe(glob, ops, NULL, PROBE_TEST_FUNC);
2199 void unregister_ftrace_function_probe_all(char *glob)
2201 __unregister_ftrace_function_probe(glob, NULL, NULL, 0);
2204 static LIST_HEAD(ftrace_commands);
2205 static DEFINE_MUTEX(ftrace_cmd_mutex);
2207 int register_ftrace_command(struct ftrace_func_command *cmd)
2209 struct ftrace_func_command *p;
2212 mutex_lock(&ftrace_cmd_mutex);
2213 list_for_each_entry(p, &ftrace_commands, list) {
2214 if (strcmp(cmd->name, p->name) == 0) {
2219 list_add(&cmd->list, &ftrace_commands);
2221 mutex_unlock(&ftrace_cmd_mutex);
2226 int unregister_ftrace_command(struct ftrace_func_command *cmd)
2228 struct ftrace_func_command *p, *n;
2231 mutex_lock(&ftrace_cmd_mutex);
2232 list_for_each_entry_safe(p, n, &ftrace_commands, list) {
2233 if (strcmp(cmd->name, p->name) == 0) {
2235 list_del_init(&p->list);
2240 mutex_unlock(&ftrace_cmd_mutex);
2245 static int ftrace_process_regex(char *buff, int len, int enable)
2247 char *func, *command, *next = buff;
2248 struct ftrace_func_command *p;
2249 struct ftrace_hash *hash;
2253 hash = &filter_hash;
2255 hash = ¬race_hash;
2257 func = strsep(&next, ":");
2260 ret = ftrace_match_records(hash, func, len);
2270 command = strsep(&next, ":");
2272 mutex_lock(&ftrace_cmd_mutex);
2273 list_for_each_entry(p, &ftrace_commands, list) {
2274 if (strcmp(p->name, command) == 0) {
2275 ret = p->func(func, command, next, enable);
2280 mutex_unlock(&ftrace_cmd_mutex);
2286 ftrace_regex_write(struct file *file, const char __user *ubuf,
2287 size_t cnt, loff_t *ppos, int enable)
2289 struct ftrace_iterator *iter;
2290 struct trace_parser *parser;
2296 mutex_lock(&ftrace_regex_lock);
2299 if (unlikely(ftrace_disabled))
2302 if (file->f_mode & FMODE_READ) {
2303 struct seq_file *m = file->private_data;
2306 iter = file->private_data;
2308 parser = &iter->parser;
2309 read = trace_get_user(parser, ubuf, cnt, ppos);
2311 if (read >= 0 && trace_parser_loaded(parser) &&
2312 !trace_parser_cont(parser)) {
2313 ret = ftrace_process_regex(parser->buffer,
2314 parser->idx, enable);
2315 trace_parser_clear(parser);
2322 mutex_unlock(&ftrace_regex_lock);
2328 ftrace_filter_write(struct file *file, const char __user *ubuf,
2329 size_t cnt, loff_t *ppos)
2331 return ftrace_regex_write(file, ubuf, cnt, ppos, 1);
2335 ftrace_notrace_write(struct file *file, const char __user *ubuf,
2336 size_t cnt, loff_t *ppos)
2338 return ftrace_regex_write(file, ubuf, cnt, ppos, 0);
2342 ftrace_set_regex(struct ftrace_hash *hash, unsigned char *buf, int len, int reset)
2344 if (unlikely(ftrace_disabled))
2347 mutex_lock(&ftrace_regex_lock);
2349 ftrace_filter_reset(hash);
2351 ftrace_match_records(hash, buf, len);
2352 mutex_unlock(&ftrace_regex_lock);
2356 * ftrace_set_filter - set a function to filter on in ftrace
2357 * @buf - the string that holds the function filter text.
2358 * @len - the length of the string.
2359 * @reset - non zero to reset all filters before applying this filter.
2361 * Filters denote which functions should be enabled when tracing is enabled.
2362 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
2364 void ftrace_set_filter(unsigned char *buf, int len, int reset)
2366 ftrace_set_regex(&filter_hash, buf, len, reset);
2370 * ftrace_set_notrace - set a function to not trace in ftrace
2371 * @buf - the string that holds the function notrace text.
2372 * @len - the length of the string.
2373 * @reset - non zero to reset all filters before applying this filter.
2375 * Notrace Filters denote which functions should not be enabled when tracing
2376 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
2379 void ftrace_set_notrace(unsigned char *buf, int len, int reset)
2381 ftrace_set_regex(¬race_hash, buf, len, reset);
2385 * command line interface to allow users to set filters on boot up.
2387 #define FTRACE_FILTER_SIZE COMMAND_LINE_SIZE
2388 static char ftrace_notrace_buf[FTRACE_FILTER_SIZE] __initdata;
2389 static char ftrace_filter_buf[FTRACE_FILTER_SIZE] __initdata;
2391 static int __init set_ftrace_notrace(char *str)
2393 strncpy(ftrace_notrace_buf, str, FTRACE_FILTER_SIZE);
2396 __setup("ftrace_notrace=", set_ftrace_notrace);
2398 static int __init set_ftrace_filter(char *str)
2400 strncpy(ftrace_filter_buf, str, FTRACE_FILTER_SIZE);
2403 __setup("ftrace_filter=", set_ftrace_filter);
2405 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
2406 static char ftrace_graph_buf[FTRACE_FILTER_SIZE] __initdata;
2407 static int ftrace_set_func(unsigned long *array, int *idx, char *buffer);
2409 static int __init set_graph_function(char *str)
2411 strlcpy(ftrace_graph_buf, str, FTRACE_FILTER_SIZE);
2414 __setup("ftrace_graph_filter=", set_graph_function);
2416 static void __init set_ftrace_early_graph(char *buf)
2422 func = strsep(&buf, ",");
2423 /* we allow only one expression at a time */
2424 ret = ftrace_set_func(ftrace_graph_funcs, &ftrace_graph_count,
2427 printk(KERN_DEBUG "ftrace: function %s not "
2428 "traceable\n", func);
2431 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
2433 static void __init set_ftrace_early_filter(struct ftrace_hash *hash, char *buf)
2438 func = strsep(&buf, ",");
2439 ftrace_set_regex(hash, func, strlen(func), 0);
2443 static void __init set_ftrace_early_filters(void)
2445 if (ftrace_filter_buf[0])
2446 set_ftrace_early_filter(&filter_hash, ftrace_filter_buf);
2447 if (ftrace_notrace_buf[0])
2448 set_ftrace_early_filter(¬race_hash, ftrace_notrace_buf);
2449 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
2450 if (ftrace_graph_buf[0])
2451 set_ftrace_early_graph(ftrace_graph_buf);
2452 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
2456 ftrace_regex_release(struct inode *inode, struct file *file)
2458 struct seq_file *m = (struct seq_file *)file->private_data;
2459 struct ftrace_iterator *iter;
2460 struct trace_parser *parser;
2462 mutex_lock(&ftrace_regex_lock);
2463 if (file->f_mode & FMODE_READ) {
2466 seq_release(inode, file);
2468 iter = file->private_data;
2470 parser = &iter->parser;
2471 if (trace_parser_loaded(parser)) {
2472 parser->buffer[parser->idx] = 0;
2473 ftrace_match_records(iter->hash, parser->buffer, parser->idx);
2476 trace_parser_put(parser);
2479 if (file->f_mode & FMODE_WRITE) {
2480 mutex_lock(&ftrace_lock);
2481 if (ftrace_start_up && ftrace_enabled)
2482 ftrace_run_update_code(FTRACE_ENABLE_CALLS);
2483 mutex_unlock(&ftrace_lock);
2486 mutex_unlock(&ftrace_regex_lock);
2490 static const struct file_operations ftrace_avail_fops = {
2491 .open = ftrace_avail_open,
2493 .llseek = seq_lseek,
2494 .release = seq_release_private,
2497 static const struct file_operations ftrace_filter_fops = {
2498 .open = ftrace_filter_open,
2500 .write = ftrace_filter_write,
2501 .llseek = ftrace_regex_lseek,
2502 .release = ftrace_regex_release,
2505 static const struct file_operations ftrace_notrace_fops = {
2506 .open = ftrace_notrace_open,
2508 .write = ftrace_notrace_write,
2509 .llseek = ftrace_regex_lseek,
2510 .release = ftrace_regex_release,
2513 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
2515 static DEFINE_MUTEX(graph_lock);
2517 int ftrace_graph_count;
2518 int ftrace_graph_filter_enabled;
2519 unsigned long ftrace_graph_funcs[FTRACE_GRAPH_MAX_FUNCS] __read_mostly;
2522 __g_next(struct seq_file *m, loff_t *pos)
2524 if (*pos >= ftrace_graph_count)
2526 return &ftrace_graph_funcs[*pos];
2530 g_next(struct seq_file *m, void *v, loff_t *pos)
2533 return __g_next(m, pos);
2536 static void *g_start(struct seq_file *m, loff_t *pos)
2538 mutex_lock(&graph_lock);
2540 /* Nothing, tell g_show to print all functions are enabled */
2541 if (!ftrace_graph_filter_enabled && !*pos)
2544 return __g_next(m, pos);
2547 static void g_stop(struct seq_file *m, void *p)
2549 mutex_unlock(&graph_lock);
2552 static int g_show(struct seq_file *m, void *v)
2554 unsigned long *ptr = v;
2559 if (ptr == (unsigned long *)1) {
2560 seq_printf(m, "#### all functions enabled ####\n");
2564 seq_printf(m, "%ps\n", (void *)*ptr);
2569 static const struct seq_operations ftrace_graph_seq_ops = {
2577 ftrace_graph_open(struct inode *inode, struct file *file)
2581 if (unlikely(ftrace_disabled))
2584 mutex_lock(&graph_lock);
2585 if ((file->f_mode & FMODE_WRITE) &&
2586 (file->f_flags & O_TRUNC)) {
2587 ftrace_graph_filter_enabled = 0;
2588 ftrace_graph_count = 0;
2589 memset(ftrace_graph_funcs, 0, sizeof(ftrace_graph_funcs));
2591 mutex_unlock(&graph_lock);
2593 if (file->f_mode & FMODE_READ)
2594 ret = seq_open(file, &ftrace_graph_seq_ops);
2600 ftrace_graph_release(struct inode *inode, struct file *file)
2602 if (file->f_mode & FMODE_READ)
2603 seq_release(inode, file);
2608 ftrace_set_func(unsigned long *array, int *idx, char *buffer)
2610 struct dyn_ftrace *rec;
2611 struct ftrace_page *pg;
2620 type = filter_parse_regex(buffer, strlen(buffer), &search, ¬);
2621 if (!not && *idx >= FTRACE_GRAPH_MAX_FUNCS)
2624 search_len = strlen(search);
2626 mutex_lock(&ftrace_lock);
2628 if (unlikely(ftrace_disabled)) {
2629 mutex_unlock(&ftrace_lock);
2633 do_for_each_ftrace_rec(pg, rec) {
2635 if (rec->flags & FTRACE_FL_FREE)
2638 if (ftrace_match_record(rec, NULL, search, search_len, type)) {
2639 /* if it is in the array */
2641 for (i = 0; i < *idx; i++) {
2642 if (array[i] == rec->ip) {
2651 array[(*idx)++] = rec->ip;
2652 if (*idx >= FTRACE_GRAPH_MAX_FUNCS)
2657 array[i] = array[--(*idx)];
2663 } while_for_each_ftrace_rec();
2665 mutex_unlock(&ftrace_lock);
2670 ftrace_graph_filter_enabled = 1;
2675 ftrace_graph_write(struct file *file, const char __user *ubuf,
2676 size_t cnt, loff_t *ppos)
2678 struct trace_parser parser;
2684 mutex_lock(&graph_lock);
2686 if (trace_parser_get_init(&parser, FTRACE_BUFF_MAX)) {
2691 read = trace_get_user(&parser, ubuf, cnt, ppos);
2693 if (read >= 0 && trace_parser_loaded((&parser))) {
2694 parser.buffer[parser.idx] = 0;
2696 /* we allow only one expression at a time */
2697 ret = ftrace_set_func(ftrace_graph_funcs, &ftrace_graph_count,
2706 trace_parser_put(&parser);
2708 mutex_unlock(&graph_lock);
2713 static const struct file_operations ftrace_graph_fops = {
2714 .open = ftrace_graph_open,
2716 .write = ftrace_graph_write,
2717 .release = ftrace_graph_release,
2718 .llseek = seq_lseek,
2720 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
2722 static __init int ftrace_init_dyn_debugfs(struct dentry *d_tracer)
2725 trace_create_file("available_filter_functions", 0444,
2726 d_tracer, NULL, &ftrace_avail_fops);
2728 trace_create_file("set_ftrace_filter", 0644, d_tracer,
2729 NULL, &ftrace_filter_fops);
2731 trace_create_file("set_ftrace_notrace", 0644, d_tracer,
2732 NULL, &ftrace_notrace_fops);
2734 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
2735 trace_create_file("set_graph_function", 0444, d_tracer,
2737 &ftrace_graph_fops);
2738 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
2743 static int ftrace_process_locs(struct module *mod,
2744 unsigned long *start,
2750 mutex_lock(&ftrace_lock);
2753 addr = ftrace_call_adjust(*p++);
2755 * Some architecture linkers will pad between
2756 * the different mcount_loc sections of different
2757 * object files to satisfy alignments.
2758 * Skip any NULL pointers.
2762 ftrace_record_ip(addr);
2765 ftrace_update_code(mod);
2766 mutex_unlock(&ftrace_lock);
2771 #ifdef CONFIG_MODULES
2772 void ftrace_release_mod(struct module *mod)
2774 struct dyn_ftrace *rec;
2775 struct ftrace_page *pg;
2777 mutex_lock(&ftrace_lock);
2779 if (ftrace_disabled)
2782 do_for_each_ftrace_rec(pg, rec) {
2783 if (within_module_core(rec->ip, mod)) {
2785 * rec->ip is changed in ftrace_free_rec()
2786 * It should not between s and e if record was freed.
2788 FTRACE_WARN_ON(rec->flags & FTRACE_FL_FREE);
2789 ftrace_free_rec(rec);
2791 } while_for_each_ftrace_rec();
2793 mutex_unlock(&ftrace_lock);
2796 static void ftrace_init_module(struct module *mod,
2797 unsigned long *start, unsigned long *end)
2799 if (ftrace_disabled || start == end)
2801 ftrace_process_locs(mod, start, end);
2804 static int ftrace_module_notify(struct notifier_block *self,
2805 unsigned long val, void *data)
2807 struct module *mod = data;
2810 case MODULE_STATE_COMING:
2811 ftrace_init_module(mod, mod->ftrace_callsites,
2812 mod->ftrace_callsites +
2813 mod->num_ftrace_callsites);
2815 case MODULE_STATE_GOING:
2816 ftrace_release_mod(mod);
2823 static int ftrace_module_notify(struct notifier_block *self,
2824 unsigned long val, void *data)
2828 #endif /* CONFIG_MODULES */
2830 struct notifier_block ftrace_module_nb = {
2831 .notifier_call = ftrace_module_notify,
2835 extern unsigned long __start_mcount_loc[];
2836 extern unsigned long __stop_mcount_loc[];
2838 void __init ftrace_init(void)
2840 unsigned long count, addr, flags;
2843 /* Keep the ftrace pointer to the stub */
2844 addr = (unsigned long)ftrace_stub;
2846 local_irq_save(flags);
2847 ftrace_dyn_arch_init(&addr);
2848 local_irq_restore(flags);
2850 /* ftrace_dyn_arch_init places the return code in addr */
2854 count = __stop_mcount_loc - __start_mcount_loc;
2856 ret = ftrace_dyn_table_alloc(count);
2860 last_ftrace_enabled = ftrace_enabled = 1;
2862 ret = ftrace_process_locs(NULL,
2866 ret = register_module_notifier(&ftrace_module_nb);
2868 pr_warning("Failed to register trace ftrace module notifier\n");
2870 set_ftrace_early_filters();
2874 ftrace_disabled = 1;
2879 static int __init ftrace_nodyn_init(void)
2884 device_initcall(ftrace_nodyn_init);
2886 static inline int ftrace_init_dyn_debugfs(struct dentry *d_tracer) { return 0; }
2887 static inline void ftrace_startup_enable(int command) { }
2888 /* Keep as macros so we do not need to define the commands */
2889 # define ftrace_startup(command) do { } while (0)
2890 # define ftrace_shutdown(command) do { } while (0)
2891 # define ftrace_startup_sysctl() do { } while (0)
2892 # define ftrace_shutdown_sysctl() do { } while (0)
2893 #endif /* CONFIG_DYNAMIC_FTRACE */
2895 static void clear_ftrace_swapper(void)
2897 struct task_struct *p;
2901 for_each_online_cpu(cpu) {
2903 clear_tsk_trace_trace(p);
2908 static void set_ftrace_swapper(void)
2910 struct task_struct *p;
2914 for_each_online_cpu(cpu) {
2916 set_tsk_trace_trace(p);
2921 static void clear_ftrace_pid(struct pid *pid)
2923 struct task_struct *p;
2926 do_each_pid_task(pid, PIDTYPE_PID, p) {
2927 clear_tsk_trace_trace(p);
2928 } while_each_pid_task(pid, PIDTYPE_PID, p);
2934 static void set_ftrace_pid(struct pid *pid)
2936 struct task_struct *p;
2939 do_each_pid_task(pid, PIDTYPE_PID, p) {
2940 set_tsk_trace_trace(p);
2941 } while_each_pid_task(pid, PIDTYPE_PID, p);
2945 static void clear_ftrace_pid_task(struct pid *pid)
2947 if (pid == ftrace_swapper_pid)
2948 clear_ftrace_swapper();
2950 clear_ftrace_pid(pid);
2953 static void set_ftrace_pid_task(struct pid *pid)
2955 if (pid == ftrace_swapper_pid)
2956 set_ftrace_swapper();
2958 set_ftrace_pid(pid);
2961 static int ftrace_pid_add(int p)
2964 struct ftrace_pid *fpid;
2967 mutex_lock(&ftrace_lock);
2970 pid = ftrace_swapper_pid;
2972 pid = find_get_pid(p);
2979 list_for_each_entry(fpid, &ftrace_pids, list)
2980 if (fpid->pid == pid)
2985 fpid = kmalloc(sizeof(*fpid), GFP_KERNEL);
2989 list_add(&fpid->list, &ftrace_pids);
2992 set_ftrace_pid_task(pid);
2994 ftrace_update_pid_func();
2995 ftrace_startup_enable(0);
2997 mutex_unlock(&ftrace_lock);
3001 if (pid != ftrace_swapper_pid)
3005 mutex_unlock(&ftrace_lock);
3009 static void ftrace_pid_reset(void)
3011 struct ftrace_pid *fpid, *safe;
3013 mutex_lock(&ftrace_lock);
3014 list_for_each_entry_safe(fpid, safe, &ftrace_pids, list) {
3015 struct pid *pid = fpid->pid;
3017 clear_ftrace_pid_task(pid);
3019 list_del(&fpid->list);
3023 ftrace_update_pid_func();
3024 ftrace_startup_enable(0);
3026 mutex_unlock(&ftrace_lock);
3029 static void *fpid_start(struct seq_file *m, loff_t *pos)
3031 mutex_lock(&ftrace_lock);
3033 if (list_empty(&ftrace_pids) && (!*pos))
3036 return seq_list_start(&ftrace_pids, *pos);
3039 static void *fpid_next(struct seq_file *m, void *v, loff_t *pos)
3044 return seq_list_next(v, &ftrace_pids, pos);
3047 static void fpid_stop(struct seq_file *m, void *p)
3049 mutex_unlock(&ftrace_lock);
3052 static int fpid_show(struct seq_file *m, void *v)
3054 const struct ftrace_pid *fpid = list_entry(v, struct ftrace_pid, list);
3056 if (v == (void *)1) {
3057 seq_printf(m, "no pid\n");
3061 if (fpid->pid == ftrace_swapper_pid)
3062 seq_printf(m, "swapper tasks\n");
3064 seq_printf(m, "%u\n", pid_vnr(fpid->pid));
3069 static const struct seq_operations ftrace_pid_sops = {
3070 .start = fpid_start,
3077 ftrace_pid_open(struct inode *inode, struct file *file)
3081 if ((file->f_mode & FMODE_WRITE) &&
3082 (file->f_flags & O_TRUNC))
3085 if (file->f_mode & FMODE_READ)
3086 ret = seq_open(file, &ftrace_pid_sops);
3092 ftrace_pid_write(struct file *filp, const char __user *ubuf,
3093 size_t cnt, loff_t *ppos)
3099 if (cnt >= sizeof(buf))
3102 if (copy_from_user(&buf, ubuf, cnt))
3108 * Allow "echo > set_ftrace_pid" or "echo -n '' > set_ftrace_pid"
3109 * to clean the filter quietly.
3111 tmp = strstrip(buf);
3112 if (strlen(tmp) == 0)
3115 ret = strict_strtol(tmp, 10, &val);
3119 ret = ftrace_pid_add(val);
3121 return ret ? ret : cnt;
3125 ftrace_pid_release(struct inode *inode, struct file *file)
3127 if (file->f_mode & FMODE_READ)
3128 seq_release(inode, file);
3133 static const struct file_operations ftrace_pid_fops = {
3134 .open = ftrace_pid_open,
3135 .write = ftrace_pid_write,
3137 .llseek = seq_lseek,
3138 .release = ftrace_pid_release,
3141 static __init int ftrace_init_debugfs(void)
3143 struct dentry *d_tracer;
3145 d_tracer = tracing_init_dentry();
3149 ftrace_init_dyn_debugfs(d_tracer);
3151 trace_create_file("set_ftrace_pid", 0644, d_tracer,
3152 NULL, &ftrace_pid_fops);
3154 ftrace_profile_debugfs(d_tracer);
3158 fs_initcall(ftrace_init_debugfs);
3161 * ftrace_kill - kill ftrace
3163 * This function should be used by panic code. It stops ftrace
3164 * but in a not so nice way. If you need to simply kill ftrace
3165 * from a non-atomic section, use ftrace_kill.
3167 void ftrace_kill(void)
3169 ftrace_disabled = 1;
3171 clear_ftrace_function();
3175 * register_ftrace_function - register a function for profiling
3176 * @ops - ops structure that holds the function for profiling.
3178 * Register a function to be called by all functions in the
3181 * Note: @ops->func and all the functions it calls must be labeled
3182 * with "notrace", otherwise it will go into a
3185 int register_ftrace_function(struct ftrace_ops *ops)
3189 mutex_lock(&ftrace_lock);
3191 if (unlikely(ftrace_disabled))
3194 ret = __register_ftrace_function(ops);
3198 mutex_unlock(&ftrace_lock);
3203 * unregister_ftrace_function - unregister a function for profiling.
3204 * @ops - ops structure that holds the function to unregister
3206 * Unregister a function that was added to be called by ftrace profiling.
3208 int unregister_ftrace_function(struct ftrace_ops *ops)
3212 mutex_lock(&ftrace_lock);
3213 ret = __unregister_ftrace_function(ops);
3215 mutex_unlock(&ftrace_lock);
3221 ftrace_enable_sysctl(struct ctl_table *table, int write,
3222 void __user *buffer, size_t *lenp,
3227 mutex_lock(&ftrace_lock);
3229 if (unlikely(ftrace_disabled))
3232 ret = proc_dointvec(table, write, buffer, lenp, ppos);
3234 if (ret || !write || (last_ftrace_enabled == !!ftrace_enabled))
3237 last_ftrace_enabled = !!ftrace_enabled;
3239 if (ftrace_enabled) {
3241 ftrace_startup_sysctl();
3243 /* we are starting ftrace again */
3244 if (ftrace_list != &ftrace_list_end) {
3245 if (ftrace_list->next == &ftrace_list_end)
3246 ftrace_trace_function = ftrace_list->func;
3248 ftrace_trace_function = ftrace_list_func;
3252 /* stopping ftrace calls (just send to ftrace_stub) */
3253 ftrace_trace_function = ftrace_stub;
3255 ftrace_shutdown_sysctl();
3259 mutex_unlock(&ftrace_lock);
3263 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
3265 static int ftrace_graph_active;
3266 static struct notifier_block ftrace_suspend_notifier;
3268 int ftrace_graph_entry_stub(struct ftrace_graph_ent *trace)
3273 /* The callbacks that hook a function */
3274 trace_func_graph_ret_t ftrace_graph_return =
3275 (trace_func_graph_ret_t)ftrace_stub;
3276 trace_func_graph_ent_t ftrace_graph_entry = ftrace_graph_entry_stub;
3278 /* Try to assign a return stack array on FTRACE_RETSTACK_ALLOC_SIZE tasks. */
3279 static int alloc_retstack_tasklist(struct ftrace_ret_stack **ret_stack_list)
3283 unsigned long flags;
3284 int start = 0, end = FTRACE_RETSTACK_ALLOC_SIZE;
3285 struct task_struct *g, *t;
3287 for (i = 0; i < FTRACE_RETSTACK_ALLOC_SIZE; i++) {
3288 ret_stack_list[i] = kmalloc(FTRACE_RETFUNC_DEPTH
3289 * sizeof(struct ftrace_ret_stack),
3291 if (!ret_stack_list[i]) {
3299 read_lock_irqsave(&tasklist_lock, flags);
3300 do_each_thread(g, t) {
3306 if (t->ret_stack == NULL) {
3307 atomic_set(&t->tracing_graph_pause, 0);
3308 atomic_set(&t->trace_overrun, 0);
3309 t->curr_ret_stack = -1;
3310 /* Make sure the tasks see the -1 first: */
3312 t->ret_stack = ret_stack_list[start++];
3314 } while_each_thread(g, t);
3317 read_unlock_irqrestore(&tasklist_lock, flags);
3319 for (i = start; i < end; i++)
3320 kfree(ret_stack_list[i]);
3325 ftrace_graph_probe_sched_switch(void *ignore,
3326 struct task_struct *prev, struct task_struct *next)
3328 unsigned long long timestamp;
3332 * Does the user want to count the time a function was asleep.
3333 * If so, do not update the time stamps.
3335 if (trace_flags & TRACE_ITER_SLEEP_TIME)
3338 timestamp = trace_clock_local();
3340 prev->ftrace_timestamp = timestamp;
3342 /* only process tasks that we timestamped */
3343 if (!next->ftrace_timestamp)
3347 * Update all the counters in next to make up for the
3348 * time next was sleeping.
3350 timestamp -= next->ftrace_timestamp;
3352 for (index = next->curr_ret_stack; index >= 0; index--)
3353 next->ret_stack[index].calltime += timestamp;
3356 /* Allocate a return stack for each task */
3357 static int start_graph_tracing(void)
3359 struct ftrace_ret_stack **ret_stack_list;
3362 ret_stack_list = kmalloc(FTRACE_RETSTACK_ALLOC_SIZE *
3363 sizeof(struct ftrace_ret_stack *),
3366 if (!ret_stack_list)
3369 /* The cpu_boot init_task->ret_stack will never be freed */
3370 for_each_online_cpu(cpu) {
3371 if (!idle_task(cpu)->ret_stack)
3372 ftrace_graph_init_idle_task(idle_task(cpu), cpu);
3376 ret = alloc_retstack_tasklist(ret_stack_list);
3377 } while (ret == -EAGAIN);
3380 ret = register_trace_sched_switch(ftrace_graph_probe_sched_switch, NULL);
3382 pr_info("ftrace_graph: Couldn't activate tracepoint"
3383 " probe to kernel_sched_switch\n");
3386 kfree(ret_stack_list);
3391 * Hibernation protection.
3392 * The state of the current task is too much unstable during
3393 * suspend/restore to disk. We want to protect against that.
3396 ftrace_suspend_notifier_call(struct notifier_block *bl, unsigned long state,
3400 case PM_HIBERNATION_PREPARE:
3401 pause_graph_tracing();
3404 case PM_POST_HIBERNATION:
3405 unpause_graph_tracing();
3411 int register_ftrace_graph(trace_func_graph_ret_t retfunc,
3412 trace_func_graph_ent_t entryfunc)
3416 mutex_lock(&ftrace_lock);
3418 /* we currently allow only one tracer registered at a time */
3419 if (ftrace_graph_active) {
3424 ftrace_suspend_notifier.notifier_call = ftrace_suspend_notifier_call;
3425 register_pm_notifier(&ftrace_suspend_notifier);
3427 ftrace_graph_active++;
3428 ret = start_graph_tracing();
3430 ftrace_graph_active--;
3434 ftrace_graph_return = retfunc;
3435 ftrace_graph_entry = entryfunc;
3437 ftrace_startup(FTRACE_START_FUNC_RET);
3440 mutex_unlock(&ftrace_lock);
3444 void unregister_ftrace_graph(void)
3446 mutex_lock(&ftrace_lock);
3448 if (unlikely(!ftrace_graph_active))
3451 ftrace_graph_active--;
3452 ftrace_graph_return = (trace_func_graph_ret_t)ftrace_stub;
3453 ftrace_graph_entry = ftrace_graph_entry_stub;
3454 ftrace_shutdown(FTRACE_STOP_FUNC_RET);
3455 unregister_pm_notifier(&ftrace_suspend_notifier);
3456 unregister_trace_sched_switch(ftrace_graph_probe_sched_switch, NULL);
3459 mutex_unlock(&ftrace_lock);
3462 static DEFINE_PER_CPU(struct ftrace_ret_stack *, idle_ret_stack);
3465 graph_init_task(struct task_struct *t, struct ftrace_ret_stack *ret_stack)
3467 atomic_set(&t->tracing_graph_pause, 0);
3468 atomic_set(&t->trace_overrun, 0);
3469 t->ftrace_timestamp = 0;
3470 /* make curr_ret_stack visible before we add the ret_stack */
3472 t->ret_stack = ret_stack;
3476 * Allocate a return stack for the idle task. May be the first
3477 * time through, or it may be done by CPU hotplug online.
3479 void ftrace_graph_init_idle_task(struct task_struct *t, int cpu)
3481 t->curr_ret_stack = -1;
3483 * The idle task has no parent, it either has its own
3484 * stack or no stack at all.
3487 WARN_ON(t->ret_stack != per_cpu(idle_ret_stack, cpu));
3489 if (ftrace_graph_active) {
3490 struct ftrace_ret_stack *ret_stack;
3492 ret_stack = per_cpu(idle_ret_stack, cpu);
3494 ret_stack = kmalloc(FTRACE_RETFUNC_DEPTH
3495 * sizeof(struct ftrace_ret_stack),
3499 per_cpu(idle_ret_stack, cpu) = ret_stack;
3501 graph_init_task(t, ret_stack);
3505 /* Allocate a return stack for newly created task */
3506 void ftrace_graph_init_task(struct task_struct *t)
3508 /* Make sure we do not use the parent ret_stack */
3509 t->ret_stack = NULL;
3510 t->curr_ret_stack = -1;
3512 if (ftrace_graph_active) {
3513 struct ftrace_ret_stack *ret_stack;
3515 ret_stack = kmalloc(FTRACE_RETFUNC_DEPTH
3516 * sizeof(struct ftrace_ret_stack),
3520 graph_init_task(t, ret_stack);
3524 void ftrace_graph_exit_task(struct task_struct *t)
3526 struct ftrace_ret_stack *ret_stack = t->ret_stack;
3528 t->ret_stack = NULL;
3529 /* NULL must become visible to IRQs before we free it: */
3535 void ftrace_graph_stop(void)