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/bsearch.h>
26 #include <linux/module.h>
27 #include <linux/ftrace.h>
28 #include <linux/sysctl.h>
29 #include <linux/slab.h>
30 #include <linux/ctype.h>
31 #include <linux/sort.h>
32 #include <linux/list.h>
33 #include <linux/hash.h>
34 #include <linux/rcupdate.h>
36 #include <trace/events/sched.h>
38 #include <asm/setup.h>
40 #include "trace_output.h"
41 #include "trace_stat.h"
43 #define FTRACE_WARN_ON(cond) \
51 #define FTRACE_WARN_ON_ONCE(cond) \
54 if (WARN_ON_ONCE(___r)) \
59 /* hash bits for specific function selection */
60 #define FTRACE_HASH_BITS 7
61 #define FTRACE_FUNC_HASHSIZE (1 << FTRACE_HASH_BITS)
62 #define FTRACE_HASH_DEFAULT_BITS 10
63 #define FTRACE_HASH_MAX_BITS 12
65 #define FL_GLOBAL_CONTROL_MASK (FTRACE_OPS_FL_GLOBAL | FTRACE_OPS_FL_CONTROL)
67 static struct ftrace_ops ftrace_list_end __read_mostly = {
71 /* ftrace_enabled is a method to turn ftrace on or off */
72 int ftrace_enabled __read_mostly;
73 static int last_ftrace_enabled;
75 /* Quick disabling of function tracer. */
76 int function_trace_stop __read_mostly;
78 /* Current function tracing op */
79 struct ftrace_ops *function_trace_op __read_mostly = &ftrace_list_end;
81 /* List for set_ftrace_pid's pids. */
82 LIST_HEAD(ftrace_pids);
84 struct list_head list;
89 * ftrace_disabled is set when an anomaly is discovered.
90 * ftrace_disabled is much stronger than ftrace_enabled.
92 static int ftrace_disabled __read_mostly;
94 static DEFINE_MUTEX(ftrace_lock);
96 static struct ftrace_ops *ftrace_global_list __read_mostly = &ftrace_list_end;
97 static struct ftrace_ops *ftrace_control_list __read_mostly = &ftrace_list_end;
98 static struct ftrace_ops *ftrace_ops_list __read_mostly = &ftrace_list_end;
99 ftrace_func_t ftrace_trace_function __read_mostly = ftrace_stub;
100 static ftrace_func_t __ftrace_trace_function_delay __read_mostly = ftrace_stub;
101 ftrace_func_t __ftrace_trace_function __read_mostly = ftrace_stub;
102 ftrace_func_t ftrace_pid_function __read_mostly = ftrace_stub;
103 static struct ftrace_ops global_ops;
104 static struct ftrace_ops control_ops;
106 #if ARCH_SUPPORTS_FTRACE_OPS
107 static void ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip,
108 struct ftrace_ops *op);
110 /* See comment below, where ftrace_ops_list_func is defined */
111 static void ftrace_ops_no_ops(unsigned long ip, unsigned long parent_ip);
112 #define ftrace_ops_list_func ((ftrace_func_t)ftrace_ops_no_ops)
116 * Traverse the ftrace_global_list, invoking all entries. The reason that we
117 * can use rcu_dereference_raw() is that elements removed from this list
118 * are simply leaked, so there is no need to interact with a grace-period
119 * mechanism. The rcu_dereference_raw() calls are needed to handle
120 * concurrent insertions into the ftrace_global_list.
122 * Silly Alpha and silly pointer-speculation compiler optimizations!
125 ftrace_global_list_func(unsigned long ip, unsigned long parent_ip,
126 struct ftrace_ops *op)
128 if (unlikely(trace_recursion_test(TRACE_GLOBAL_BIT)))
131 trace_recursion_set(TRACE_GLOBAL_BIT);
132 op = rcu_dereference_raw(ftrace_global_list); /*see above*/
133 while (op != &ftrace_list_end) {
134 op->func(ip, parent_ip, op);
135 op = rcu_dereference_raw(op->next); /*see above*/
137 trace_recursion_clear(TRACE_GLOBAL_BIT);
140 static void ftrace_pid_func(unsigned long ip, unsigned long parent_ip,
141 struct ftrace_ops *op)
143 if (!test_tsk_trace_trace(current))
146 ftrace_pid_function(ip, parent_ip, op);
149 static void set_ftrace_pid_function(ftrace_func_t func)
151 /* do not set ftrace_pid_function to itself! */
152 if (func != ftrace_pid_func)
153 ftrace_pid_function = func;
157 * clear_ftrace_function - reset the ftrace function
159 * This NULLs the ftrace function and in essence stops
160 * tracing. There may be lag
162 void clear_ftrace_function(void)
164 ftrace_trace_function = ftrace_stub;
165 __ftrace_trace_function = ftrace_stub;
166 __ftrace_trace_function_delay = ftrace_stub;
167 ftrace_pid_function = ftrace_stub;
170 #ifndef CONFIG_HAVE_FUNCTION_TRACE_MCOUNT_TEST
172 * For those archs that do not test ftrace_trace_stop in their
173 * mcount call site, we need to do it from C.
175 static void ftrace_test_stop_func(unsigned long ip, unsigned long parent_ip,
176 struct ftrace_ops *op)
178 if (function_trace_stop)
181 __ftrace_trace_function(ip, parent_ip, op);
185 static void control_ops_disable_all(struct ftrace_ops *ops)
189 for_each_possible_cpu(cpu)
190 *per_cpu_ptr(ops->disabled, cpu) = 1;
193 static int control_ops_alloc(struct ftrace_ops *ops)
195 int __percpu *disabled;
197 disabled = alloc_percpu(int);
201 ops->disabled = disabled;
202 control_ops_disable_all(ops);
206 static void control_ops_free(struct ftrace_ops *ops)
208 free_percpu(ops->disabled);
211 static void update_global_ops(void)
216 * If there's only one function registered, then call that
217 * function directly. Otherwise, we need to iterate over the
218 * registered callers.
220 if (ftrace_global_list == &ftrace_list_end ||
221 ftrace_global_list->next == &ftrace_list_end)
222 func = ftrace_global_list->func;
224 func = ftrace_global_list_func;
226 /* If we filter on pids, update to use the pid function */
227 if (!list_empty(&ftrace_pids)) {
228 set_ftrace_pid_function(func);
229 func = ftrace_pid_func;
232 global_ops.func = func;
235 static void update_ftrace_function(void)
242 * If we are at the end of the list and this ops is
243 * not dynamic and the arch supports passing ops, then have the
244 * mcount trampoline call the function directly.
246 if (ftrace_ops_list == &ftrace_list_end ||
247 (ftrace_ops_list->next == &ftrace_list_end &&
248 !(ftrace_ops_list->flags & FTRACE_OPS_FL_DYNAMIC) &&
249 ARCH_SUPPORTS_FTRACE_OPS)) {
250 /* Set the ftrace_ops that the arch callback uses */
251 if (ftrace_ops_list == &global_ops)
252 function_trace_op = ftrace_global_list;
254 function_trace_op = ftrace_ops_list;
255 func = ftrace_ops_list->func;
257 /* Just use the default ftrace_ops */
258 function_trace_op = &ftrace_list_end;
259 func = ftrace_ops_list_func;
262 #ifdef CONFIG_HAVE_FUNCTION_TRACE_MCOUNT_TEST
263 ftrace_trace_function = func;
265 #ifdef CONFIG_DYNAMIC_FTRACE
266 /* do not update till all functions have been modified */
267 __ftrace_trace_function_delay = func;
269 __ftrace_trace_function = func;
271 ftrace_trace_function =
272 (func == ftrace_stub) ? func : ftrace_test_stop_func;
276 static void add_ftrace_ops(struct ftrace_ops **list, struct ftrace_ops *ops)
280 * We are entering ops into the list but another
281 * CPU might be walking that list. We need to make sure
282 * the ops->next pointer is valid before another CPU sees
283 * the ops pointer included into the list.
285 rcu_assign_pointer(*list, ops);
288 static int remove_ftrace_ops(struct ftrace_ops **list, struct ftrace_ops *ops)
290 struct ftrace_ops **p;
293 * If we are removing the last function, then simply point
294 * to the ftrace_stub.
296 if (*list == ops && ops->next == &ftrace_list_end) {
297 *list = &ftrace_list_end;
301 for (p = list; *p != &ftrace_list_end; p = &(*p)->next)
312 static void add_ftrace_list_ops(struct ftrace_ops **list,
313 struct ftrace_ops *main_ops,
314 struct ftrace_ops *ops)
316 int first = *list == &ftrace_list_end;
317 add_ftrace_ops(list, ops);
319 add_ftrace_ops(&ftrace_ops_list, main_ops);
322 static int remove_ftrace_list_ops(struct ftrace_ops **list,
323 struct ftrace_ops *main_ops,
324 struct ftrace_ops *ops)
326 int ret = remove_ftrace_ops(list, ops);
327 if (!ret && *list == &ftrace_list_end)
328 ret = remove_ftrace_ops(&ftrace_ops_list, main_ops);
332 static int __register_ftrace_function(struct ftrace_ops *ops)
334 if (unlikely(ftrace_disabled))
337 if (FTRACE_WARN_ON(ops == &global_ops))
340 if (WARN_ON(ops->flags & FTRACE_OPS_FL_ENABLED))
343 /* We don't support both control and global flags set. */
344 if ((ops->flags & FL_GLOBAL_CONTROL_MASK) == FL_GLOBAL_CONTROL_MASK)
347 if (!core_kernel_data((unsigned long)ops))
348 ops->flags |= FTRACE_OPS_FL_DYNAMIC;
350 if (ops->flags & FTRACE_OPS_FL_GLOBAL) {
351 add_ftrace_list_ops(&ftrace_global_list, &global_ops, ops);
352 ops->flags |= FTRACE_OPS_FL_ENABLED;
353 } else if (ops->flags & FTRACE_OPS_FL_CONTROL) {
354 if (control_ops_alloc(ops))
356 add_ftrace_list_ops(&ftrace_control_list, &control_ops, ops);
358 add_ftrace_ops(&ftrace_ops_list, ops);
361 update_ftrace_function();
366 static int __unregister_ftrace_function(struct ftrace_ops *ops)
373 if (WARN_ON(!(ops->flags & FTRACE_OPS_FL_ENABLED)))
376 if (FTRACE_WARN_ON(ops == &global_ops))
379 if (ops->flags & FTRACE_OPS_FL_GLOBAL) {
380 ret = remove_ftrace_list_ops(&ftrace_global_list,
383 ops->flags &= ~FTRACE_OPS_FL_ENABLED;
384 } else if (ops->flags & FTRACE_OPS_FL_CONTROL) {
385 ret = remove_ftrace_list_ops(&ftrace_control_list,
389 * The ftrace_ops is now removed from the list,
390 * so there'll be no new users. We must ensure
391 * all current users are done before we free
395 control_ops_free(ops);
398 ret = remove_ftrace_ops(&ftrace_ops_list, ops);
404 update_ftrace_function();
407 * Dynamic ops may be freed, we must make sure that all
408 * callers are done before leaving this function.
410 if (ops->flags & FTRACE_OPS_FL_DYNAMIC)
416 static void ftrace_update_pid_func(void)
418 /* Only do something if we are tracing something */
419 if (ftrace_trace_function == ftrace_stub)
422 update_ftrace_function();
425 #ifdef CONFIG_FUNCTION_PROFILER
426 struct ftrace_profile {
427 struct hlist_node node;
429 unsigned long counter;
430 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
431 unsigned long long time;
432 unsigned long long time_squared;
436 struct ftrace_profile_page {
437 struct ftrace_profile_page *next;
439 struct ftrace_profile records[];
442 struct ftrace_profile_stat {
444 struct hlist_head *hash;
445 struct ftrace_profile_page *pages;
446 struct ftrace_profile_page *start;
447 struct tracer_stat stat;
450 #define PROFILE_RECORDS_SIZE \
451 (PAGE_SIZE - offsetof(struct ftrace_profile_page, records))
453 #define PROFILES_PER_PAGE \
454 (PROFILE_RECORDS_SIZE / sizeof(struct ftrace_profile))
456 static int ftrace_profile_bits __read_mostly;
457 static int ftrace_profile_enabled __read_mostly;
459 /* ftrace_profile_lock - synchronize the enable and disable of the profiler */
460 static DEFINE_MUTEX(ftrace_profile_lock);
462 static DEFINE_PER_CPU(struct ftrace_profile_stat, ftrace_profile_stats);
464 #define FTRACE_PROFILE_HASH_SIZE 1024 /* must be power of 2 */
467 function_stat_next(void *v, int idx)
469 struct ftrace_profile *rec = v;
470 struct ftrace_profile_page *pg;
472 pg = (struct ftrace_profile_page *)((unsigned long)rec & PAGE_MASK);
478 if ((void *)rec >= (void *)&pg->records[pg->index]) {
482 rec = &pg->records[0];
490 static void *function_stat_start(struct tracer_stat *trace)
492 struct ftrace_profile_stat *stat =
493 container_of(trace, struct ftrace_profile_stat, stat);
495 if (!stat || !stat->start)
498 return function_stat_next(&stat->start->records[0], 0);
501 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
502 /* function graph compares on total time */
503 static int function_stat_cmp(void *p1, void *p2)
505 struct ftrace_profile *a = p1;
506 struct ftrace_profile *b = p2;
508 if (a->time < b->time)
510 if (a->time > b->time)
516 /* not function graph compares against hits */
517 static int function_stat_cmp(void *p1, void *p2)
519 struct ftrace_profile *a = p1;
520 struct ftrace_profile *b = p2;
522 if (a->counter < b->counter)
524 if (a->counter > b->counter)
531 static int function_stat_headers(struct seq_file *m)
533 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
534 seq_printf(m, " Function "
537 "--- ---- --- ---\n");
539 seq_printf(m, " Function Hit\n"
545 static int function_stat_show(struct seq_file *m, void *v)
547 struct ftrace_profile *rec = v;
548 char str[KSYM_SYMBOL_LEN];
550 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
551 static struct trace_seq s;
552 unsigned long long avg;
553 unsigned long long stddev;
555 mutex_lock(&ftrace_profile_lock);
557 /* we raced with function_profile_reset() */
558 if (unlikely(rec->counter == 0)) {
563 kallsyms_lookup(rec->ip, NULL, NULL, NULL, str);
564 seq_printf(m, " %-30.30s %10lu", str, rec->counter);
566 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
569 do_div(avg, rec->counter);
571 /* Sample standard deviation (s^2) */
572 if (rec->counter <= 1)
575 stddev = rec->time_squared - rec->counter * avg * avg;
577 * Divide only 1000 for ns^2 -> us^2 conversion.
578 * trace_print_graph_duration will divide 1000 again.
580 do_div(stddev, (rec->counter - 1) * 1000);
584 trace_print_graph_duration(rec->time, &s);
585 trace_seq_puts(&s, " ");
586 trace_print_graph_duration(avg, &s);
587 trace_seq_puts(&s, " ");
588 trace_print_graph_duration(stddev, &s);
589 trace_print_seq(m, &s);
593 mutex_unlock(&ftrace_profile_lock);
598 static void ftrace_profile_reset(struct ftrace_profile_stat *stat)
600 struct ftrace_profile_page *pg;
602 pg = stat->pages = stat->start;
605 memset(pg->records, 0, PROFILE_RECORDS_SIZE);
610 memset(stat->hash, 0,
611 FTRACE_PROFILE_HASH_SIZE * sizeof(struct hlist_head));
614 int ftrace_profile_pages_init(struct ftrace_profile_stat *stat)
616 struct ftrace_profile_page *pg;
621 /* If we already allocated, do nothing */
625 stat->pages = (void *)get_zeroed_page(GFP_KERNEL);
629 #ifdef CONFIG_DYNAMIC_FTRACE
630 functions = ftrace_update_tot_cnt;
633 * We do not know the number of functions that exist because
634 * dynamic tracing is what counts them. With past experience
635 * we have around 20K functions. That should be more than enough.
636 * It is highly unlikely we will execute every function in
642 pg = stat->start = stat->pages;
644 pages = DIV_ROUND_UP(functions, PROFILES_PER_PAGE);
646 for (i = 0; i < pages; i++) {
647 pg->next = (void *)get_zeroed_page(GFP_KERNEL);
658 unsigned long tmp = (unsigned long)pg;
664 free_page((unsigned long)stat->pages);
671 static int ftrace_profile_init_cpu(int cpu)
673 struct ftrace_profile_stat *stat;
676 stat = &per_cpu(ftrace_profile_stats, cpu);
679 /* If the profile is already created, simply reset it */
680 ftrace_profile_reset(stat);
685 * We are profiling all functions, but usually only a few thousand
686 * functions are hit. We'll make a hash of 1024 items.
688 size = FTRACE_PROFILE_HASH_SIZE;
690 stat->hash = kzalloc(sizeof(struct hlist_head) * size, GFP_KERNEL);
695 if (!ftrace_profile_bits) {
698 for (; size; size >>= 1)
699 ftrace_profile_bits++;
702 /* Preallocate the function profiling pages */
703 if (ftrace_profile_pages_init(stat) < 0) {
712 static int ftrace_profile_init(void)
717 for_each_online_cpu(cpu) {
718 ret = ftrace_profile_init_cpu(cpu);
726 /* interrupts must be disabled */
727 static struct ftrace_profile *
728 ftrace_find_profiled_func(struct ftrace_profile_stat *stat, unsigned long ip)
730 struct ftrace_profile *rec;
731 struct hlist_head *hhd;
732 struct hlist_node *n;
735 key = hash_long(ip, ftrace_profile_bits);
736 hhd = &stat->hash[key];
738 if (hlist_empty(hhd))
741 hlist_for_each_entry_rcu(rec, n, hhd, node) {
749 static void ftrace_add_profile(struct ftrace_profile_stat *stat,
750 struct ftrace_profile *rec)
754 key = hash_long(rec->ip, ftrace_profile_bits);
755 hlist_add_head_rcu(&rec->node, &stat->hash[key]);
759 * The memory is already allocated, this simply finds a new record to use.
761 static struct ftrace_profile *
762 ftrace_profile_alloc(struct ftrace_profile_stat *stat, unsigned long ip)
764 struct ftrace_profile *rec = NULL;
766 /* prevent recursion (from NMIs) */
767 if (atomic_inc_return(&stat->disabled) != 1)
771 * Try to find the function again since an NMI
772 * could have added it
774 rec = ftrace_find_profiled_func(stat, ip);
778 if (stat->pages->index == PROFILES_PER_PAGE) {
779 if (!stat->pages->next)
781 stat->pages = stat->pages->next;
784 rec = &stat->pages->records[stat->pages->index++];
786 ftrace_add_profile(stat, rec);
789 atomic_dec(&stat->disabled);
795 function_profile_call(unsigned long ip, unsigned long parent_ip,
796 struct ftrace_ops *ops)
798 struct ftrace_profile_stat *stat;
799 struct ftrace_profile *rec;
802 if (!ftrace_profile_enabled)
805 local_irq_save(flags);
807 stat = &__get_cpu_var(ftrace_profile_stats);
808 if (!stat->hash || !ftrace_profile_enabled)
811 rec = ftrace_find_profiled_func(stat, ip);
813 rec = ftrace_profile_alloc(stat, ip);
820 local_irq_restore(flags);
823 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
824 static int profile_graph_entry(struct ftrace_graph_ent *trace)
826 function_profile_call(trace->func, 0, NULL);
830 static void profile_graph_return(struct ftrace_graph_ret *trace)
832 struct ftrace_profile_stat *stat;
833 unsigned long long calltime;
834 struct ftrace_profile *rec;
837 local_irq_save(flags);
838 stat = &__get_cpu_var(ftrace_profile_stats);
839 if (!stat->hash || !ftrace_profile_enabled)
842 /* If the calltime was zero'd ignore it */
843 if (!trace->calltime)
846 calltime = trace->rettime - trace->calltime;
848 if (!(trace_flags & TRACE_ITER_GRAPH_TIME)) {
851 index = trace->depth;
853 /* Append this call time to the parent time to subtract */
855 current->ret_stack[index - 1].subtime += calltime;
857 if (current->ret_stack[index].subtime < calltime)
858 calltime -= current->ret_stack[index].subtime;
863 rec = ftrace_find_profiled_func(stat, trace->func);
865 rec->time += calltime;
866 rec->time_squared += calltime * calltime;
870 local_irq_restore(flags);
873 static int register_ftrace_profiler(void)
875 return register_ftrace_graph(&profile_graph_return,
876 &profile_graph_entry);
879 static void unregister_ftrace_profiler(void)
881 unregister_ftrace_graph();
884 static struct ftrace_ops ftrace_profile_ops __read_mostly = {
885 .func = function_profile_call,
888 static int register_ftrace_profiler(void)
890 return register_ftrace_function(&ftrace_profile_ops);
893 static void unregister_ftrace_profiler(void)
895 unregister_ftrace_function(&ftrace_profile_ops);
897 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
900 ftrace_profile_write(struct file *filp, const char __user *ubuf,
901 size_t cnt, loff_t *ppos)
906 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
912 mutex_lock(&ftrace_profile_lock);
913 if (ftrace_profile_enabled ^ val) {
915 ret = ftrace_profile_init();
921 ret = register_ftrace_profiler();
926 ftrace_profile_enabled = 1;
928 ftrace_profile_enabled = 0;
930 * unregister_ftrace_profiler calls stop_machine
931 * so this acts like an synchronize_sched.
933 unregister_ftrace_profiler();
937 mutex_unlock(&ftrace_profile_lock);
945 ftrace_profile_read(struct file *filp, char __user *ubuf,
946 size_t cnt, loff_t *ppos)
948 char buf[64]; /* big enough to hold a number */
951 r = sprintf(buf, "%u\n", ftrace_profile_enabled);
952 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
955 static const struct file_operations ftrace_profile_fops = {
956 .open = tracing_open_generic,
957 .read = ftrace_profile_read,
958 .write = ftrace_profile_write,
959 .llseek = default_llseek,
962 /* used to initialize the real stat files */
963 static struct tracer_stat function_stats __initdata = {
965 .stat_start = function_stat_start,
966 .stat_next = function_stat_next,
967 .stat_cmp = function_stat_cmp,
968 .stat_headers = function_stat_headers,
969 .stat_show = function_stat_show
972 static __init void ftrace_profile_debugfs(struct dentry *d_tracer)
974 struct ftrace_profile_stat *stat;
975 struct dentry *entry;
980 for_each_possible_cpu(cpu) {
981 stat = &per_cpu(ftrace_profile_stats, cpu);
983 /* allocate enough for function name + cpu number */
984 name = kmalloc(32, GFP_KERNEL);
987 * The files created are permanent, if something happens
988 * we still do not free memory.
991 "Could not allocate stat file for cpu %d\n",
995 stat->stat = function_stats;
996 snprintf(name, 32, "function%d", cpu);
997 stat->stat.name = name;
998 ret = register_stat_tracer(&stat->stat);
1001 "Could not register function stat for cpu %d\n",
1008 entry = debugfs_create_file("function_profile_enabled", 0644,
1009 d_tracer, NULL, &ftrace_profile_fops);
1011 pr_warning("Could not create debugfs "
1012 "'function_profile_enabled' entry\n");
1015 #else /* CONFIG_FUNCTION_PROFILER */
1016 static __init void ftrace_profile_debugfs(struct dentry *d_tracer)
1019 #endif /* CONFIG_FUNCTION_PROFILER */
1021 static struct pid * const ftrace_swapper_pid = &init_struct_pid;
1023 #ifdef CONFIG_DYNAMIC_FTRACE
1025 #ifndef CONFIG_FTRACE_MCOUNT_RECORD
1026 # error Dynamic ftrace depends on MCOUNT_RECORD
1029 static struct hlist_head ftrace_func_hash[FTRACE_FUNC_HASHSIZE] __read_mostly;
1031 struct ftrace_func_probe {
1032 struct hlist_node node;
1033 struct ftrace_probe_ops *ops;
1034 unsigned long flags;
1037 struct rcu_head rcu;
1040 struct ftrace_func_entry {
1041 struct hlist_node hlist;
1045 struct ftrace_hash {
1046 unsigned long size_bits;
1047 struct hlist_head *buckets;
1048 unsigned long count;
1049 struct rcu_head rcu;
1053 * We make these constant because no one should touch them,
1054 * but they are used as the default "empty hash", to avoid allocating
1055 * it all the time. These are in a read only section such that if
1056 * anyone does try to modify it, it will cause an exception.
1058 static const struct hlist_head empty_buckets[1];
1059 static const struct ftrace_hash empty_hash = {
1060 .buckets = (struct hlist_head *)empty_buckets,
1062 #define EMPTY_HASH ((struct ftrace_hash *)&empty_hash)
1064 static struct ftrace_ops global_ops = {
1065 .func = ftrace_stub,
1066 .notrace_hash = EMPTY_HASH,
1067 .filter_hash = EMPTY_HASH,
1070 static DEFINE_MUTEX(ftrace_regex_lock);
1072 struct ftrace_page {
1073 struct ftrace_page *next;
1074 struct dyn_ftrace *records;
1079 static struct ftrace_page *ftrace_new_pgs;
1081 #define ENTRY_SIZE sizeof(struct dyn_ftrace)
1082 #define ENTRIES_PER_PAGE (PAGE_SIZE / ENTRY_SIZE)
1084 /* estimate from running different kernels */
1085 #define NR_TO_INIT 10000
1087 static struct ftrace_page *ftrace_pages_start;
1088 static struct ftrace_page *ftrace_pages;
1090 static bool ftrace_hash_empty(struct ftrace_hash *hash)
1092 return !hash || !hash->count;
1095 static struct ftrace_func_entry *
1096 ftrace_lookup_ip(struct ftrace_hash *hash, unsigned long ip)
1099 struct ftrace_func_entry *entry;
1100 struct hlist_head *hhd;
1101 struct hlist_node *n;
1103 if (ftrace_hash_empty(hash))
1106 if (hash->size_bits > 0)
1107 key = hash_long(ip, hash->size_bits);
1111 hhd = &hash->buckets[key];
1113 hlist_for_each_entry_rcu(entry, n, hhd, hlist) {
1114 if (entry->ip == ip)
1120 static void __add_hash_entry(struct ftrace_hash *hash,
1121 struct ftrace_func_entry *entry)
1123 struct hlist_head *hhd;
1126 if (hash->size_bits)
1127 key = hash_long(entry->ip, hash->size_bits);
1131 hhd = &hash->buckets[key];
1132 hlist_add_head(&entry->hlist, hhd);
1136 static int add_hash_entry(struct ftrace_hash *hash, unsigned long ip)
1138 struct ftrace_func_entry *entry;
1140 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
1145 __add_hash_entry(hash, entry);
1151 free_hash_entry(struct ftrace_hash *hash,
1152 struct ftrace_func_entry *entry)
1154 hlist_del(&entry->hlist);
1160 remove_hash_entry(struct ftrace_hash *hash,
1161 struct ftrace_func_entry *entry)
1163 hlist_del(&entry->hlist);
1167 static void ftrace_hash_clear(struct ftrace_hash *hash)
1169 struct hlist_head *hhd;
1170 struct hlist_node *tp, *tn;
1171 struct ftrace_func_entry *entry;
1172 int size = 1 << hash->size_bits;
1178 for (i = 0; i < size; i++) {
1179 hhd = &hash->buckets[i];
1180 hlist_for_each_entry_safe(entry, tp, tn, hhd, hlist)
1181 free_hash_entry(hash, entry);
1183 FTRACE_WARN_ON(hash->count);
1186 static void free_ftrace_hash(struct ftrace_hash *hash)
1188 if (!hash || hash == EMPTY_HASH)
1190 ftrace_hash_clear(hash);
1191 kfree(hash->buckets);
1195 static void __free_ftrace_hash_rcu(struct rcu_head *rcu)
1197 struct ftrace_hash *hash;
1199 hash = container_of(rcu, struct ftrace_hash, rcu);
1200 free_ftrace_hash(hash);
1203 static void free_ftrace_hash_rcu(struct ftrace_hash *hash)
1205 if (!hash || hash == EMPTY_HASH)
1207 call_rcu_sched(&hash->rcu, __free_ftrace_hash_rcu);
1210 void ftrace_free_filter(struct ftrace_ops *ops)
1212 free_ftrace_hash(ops->filter_hash);
1213 free_ftrace_hash(ops->notrace_hash);
1216 static struct ftrace_hash *alloc_ftrace_hash(int size_bits)
1218 struct ftrace_hash *hash;
1221 hash = kzalloc(sizeof(*hash), GFP_KERNEL);
1225 size = 1 << size_bits;
1226 hash->buckets = kcalloc(size, sizeof(*hash->buckets), GFP_KERNEL);
1228 if (!hash->buckets) {
1233 hash->size_bits = size_bits;
1238 static struct ftrace_hash *
1239 alloc_and_copy_ftrace_hash(int size_bits, struct ftrace_hash *hash)
1241 struct ftrace_func_entry *entry;
1242 struct ftrace_hash *new_hash;
1243 struct hlist_node *tp;
1248 new_hash = alloc_ftrace_hash(size_bits);
1253 if (ftrace_hash_empty(hash))
1256 size = 1 << hash->size_bits;
1257 for (i = 0; i < size; i++) {
1258 hlist_for_each_entry(entry, tp, &hash->buckets[i], hlist) {
1259 ret = add_hash_entry(new_hash, entry->ip);
1265 FTRACE_WARN_ON(new_hash->count != hash->count);
1270 free_ftrace_hash(new_hash);
1275 ftrace_hash_rec_disable(struct ftrace_ops *ops, int filter_hash);
1277 ftrace_hash_rec_enable(struct ftrace_ops *ops, int filter_hash);
1280 ftrace_hash_move(struct ftrace_ops *ops, int enable,
1281 struct ftrace_hash **dst, struct ftrace_hash *src)
1283 struct ftrace_func_entry *entry;
1284 struct hlist_node *tp, *tn;
1285 struct hlist_head *hhd;
1286 struct ftrace_hash *old_hash;
1287 struct ftrace_hash *new_hash;
1289 int size = src->count;
1295 * Remove the current set, update the hash and add
1298 ftrace_hash_rec_disable(ops, enable);
1301 * If the new source is empty, just free dst and assign it
1305 free_ftrace_hash_rcu(*dst);
1306 rcu_assign_pointer(*dst, EMPTY_HASH);
1307 /* still need to update the function records */
1313 * Make the hash size about 1/2 the # found
1315 for (size /= 2; size; size >>= 1)
1318 /* Don't allocate too much */
1319 if (bits > FTRACE_HASH_MAX_BITS)
1320 bits = FTRACE_HASH_MAX_BITS;
1323 new_hash = alloc_ftrace_hash(bits);
1327 size = 1 << src->size_bits;
1328 for (i = 0; i < size; i++) {
1329 hhd = &src->buckets[i];
1330 hlist_for_each_entry_safe(entry, tp, tn, hhd, hlist) {
1332 key = hash_long(entry->ip, bits);
1335 remove_hash_entry(src, entry);
1336 __add_hash_entry(new_hash, entry);
1341 rcu_assign_pointer(*dst, new_hash);
1342 free_ftrace_hash_rcu(old_hash);
1347 * Enable regardless of ret:
1348 * On success, we enable the new hash.
1349 * On failure, we re-enable the original hash.
1351 ftrace_hash_rec_enable(ops, enable);
1357 * Test the hashes for this ops to see if we want to call
1358 * the ops->func or not.
1360 * It's a match if the ip is in the ops->filter_hash or
1361 * the filter_hash does not exist or is empty,
1363 * the ip is not in the ops->notrace_hash.
1365 * This needs to be called with preemption disabled as
1366 * the hashes are freed with call_rcu_sched().
1369 ftrace_ops_test(struct ftrace_ops *ops, unsigned long ip)
1371 struct ftrace_hash *filter_hash;
1372 struct ftrace_hash *notrace_hash;
1375 filter_hash = rcu_dereference_raw(ops->filter_hash);
1376 notrace_hash = rcu_dereference_raw(ops->notrace_hash);
1378 if ((ftrace_hash_empty(filter_hash) ||
1379 ftrace_lookup_ip(filter_hash, ip)) &&
1380 (ftrace_hash_empty(notrace_hash) ||
1381 !ftrace_lookup_ip(notrace_hash, ip)))
1390 * This is a double for. Do not use 'break' to break out of the loop,
1391 * you must use a goto.
1393 #define do_for_each_ftrace_rec(pg, rec) \
1394 for (pg = ftrace_pages_start; pg; pg = pg->next) { \
1396 for (_____i = 0; _____i < pg->index; _____i++) { \
1397 rec = &pg->records[_____i];
1399 #define while_for_each_ftrace_rec() \
1404 static int ftrace_cmp_recs(const void *a, const void *b)
1406 const struct dyn_ftrace *key = a;
1407 const struct dyn_ftrace *rec = b;
1409 if (key->flags < rec->ip)
1411 if (key->ip >= rec->ip + MCOUNT_INSN_SIZE)
1416 static unsigned long ftrace_location_range(unsigned long start, unsigned long end)
1418 struct ftrace_page *pg;
1419 struct dyn_ftrace *rec;
1420 struct dyn_ftrace key;
1423 key.flags = end; /* overload flags, as it is unsigned long */
1425 for (pg = ftrace_pages_start; pg; pg = pg->next) {
1426 if (end < pg->records[0].ip ||
1427 start >= (pg->records[pg->index - 1].ip + MCOUNT_INSN_SIZE))
1429 rec = bsearch(&key, pg->records, pg->index,
1430 sizeof(struct dyn_ftrace),
1440 * ftrace_location - return true if the ip giving is a traced location
1441 * @ip: the instruction pointer to check
1443 * Returns rec->ip if @ip given is a pointer to a ftrace location.
1444 * That is, the instruction that is either a NOP or call to
1445 * the function tracer. It checks the ftrace internal tables to
1446 * determine if the address belongs or not.
1448 unsigned long ftrace_location(unsigned long ip)
1450 return ftrace_location_range(ip, ip);
1454 * ftrace_text_reserved - return true if range contains an ftrace location
1455 * @start: start of range to search
1456 * @end: end of range to search (inclusive). @end points to the last byte to check.
1458 * Returns 1 if @start and @end contains a ftrace location.
1459 * That is, the instruction that is either a NOP or call to
1460 * the function tracer. It checks the ftrace internal tables to
1461 * determine if the address belongs or not.
1463 int ftrace_text_reserved(void *start, void *end)
1467 ret = ftrace_location_range((unsigned long)start,
1468 (unsigned long)end);
1473 static void __ftrace_hash_rec_update(struct ftrace_ops *ops,
1477 struct ftrace_hash *hash;
1478 struct ftrace_hash *other_hash;
1479 struct ftrace_page *pg;
1480 struct dyn_ftrace *rec;
1484 /* Only update if the ops has been registered */
1485 if (!(ops->flags & FTRACE_OPS_FL_ENABLED))
1489 * In the filter_hash case:
1490 * If the count is zero, we update all records.
1491 * Otherwise we just update the items in the hash.
1493 * In the notrace_hash case:
1494 * We enable the update in the hash.
1495 * As disabling notrace means enabling the tracing,
1496 * and enabling notrace means disabling, the inc variable
1500 hash = ops->filter_hash;
1501 other_hash = ops->notrace_hash;
1502 if (ftrace_hash_empty(hash))
1506 hash = ops->notrace_hash;
1507 other_hash = ops->filter_hash;
1509 * If the notrace hash has no items,
1510 * then there's nothing to do.
1512 if (ftrace_hash_empty(hash))
1516 do_for_each_ftrace_rec(pg, rec) {
1517 int in_other_hash = 0;
1523 * Only the filter_hash affects all records.
1524 * Update if the record is not in the notrace hash.
1526 if (!other_hash || !ftrace_lookup_ip(other_hash, rec->ip))
1529 in_hash = !!ftrace_lookup_ip(hash, rec->ip);
1530 in_other_hash = !!ftrace_lookup_ip(other_hash, rec->ip);
1535 if (filter_hash && in_hash && !in_other_hash)
1537 else if (!filter_hash && in_hash &&
1538 (in_other_hash || ftrace_hash_empty(other_hash)))
1546 if (FTRACE_WARN_ON((rec->flags & ~FTRACE_FL_MASK) == FTRACE_REF_MAX))
1549 if (FTRACE_WARN_ON((rec->flags & ~FTRACE_FL_MASK) == 0))
1554 /* Shortcut, if we handled all records, we are done. */
1555 if (!all && count == hash->count)
1557 } while_for_each_ftrace_rec();
1560 static void ftrace_hash_rec_disable(struct ftrace_ops *ops,
1563 __ftrace_hash_rec_update(ops, filter_hash, 0);
1566 static void ftrace_hash_rec_enable(struct ftrace_ops *ops,
1569 __ftrace_hash_rec_update(ops, filter_hash, 1);
1572 static void print_ip_ins(const char *fmt, unsigned char *p)
1576 printk(KERN_CONT "%s", fmt);
1578 for (i = 0; i < MCOUNT_INSN_SIZE; i++)
1579 printk(KERN_CONT "%s%02x", i ? ":" : "", p[i]);
1583 * ftrace_bug - report and shutdown function tracer
1584 * @failed: The failed type (EFAULT, EINVAL, EPERM)
1585 * @ip: The address that failed
1587 * The arch code that enables or disables the function tracing
1588 * can call ftrace_bug() when it has detected a problem in
1589 * modifying the code. @failed should be one of either:
1590 * EFAULT - if the problem happens on reading the @ip address
1591 * EINVAL - if what is read at @ip is not what was expected
1592 * EPERM - if the problem happens on writting to the @ip address
1594 void ftrace_bug(int failed, unsigned long ip)
1598 FTRACE_WARN_ON_ONCE(1);
1599 pr_info("ftrace faulted on modifying ");
1603 FTRACE_WARN_ON_ONCE(1);
1604 pr_info("ftrace failed to modify ");
1606 print_ip_ins(" actual: ", (unsigned char *)ip);
1607 printk(KERN_CONT "\n");
1610 FTRACE_WARN_ON_ONCE(1);
1611 pr_info("ftrace faulted on writing ");
1615 FTRACE_WARN_ON_ONCE(1);
1616 pr_info("ftrace faulted on unknown error ");
1621 static int ftrace_check_record(struct dyn_ftrace *rec, int enable, int update)
1623 unsigned long flag = 0UL;
1626 * If we are updating calls:
1628 * If the record has a ref count, then we need to enable it
1629 * because someone is using it.
1631 * Otherwise we make sure its disabled.
1633 * If we are disabling calls, then disable all records that
1636 if (enable && (rec->flags & ~FTRACE_FL_MASK))
1637 flag = FTRACE_FL_ENABLED;
1639 /* If the state of this record hasn't changed, then do nothing */
1640 if ((rec->flags & FTRACE_FL_ENABLED) == flag)
1641 return FTRACE_UPDATE_IGNORE;
1645 rec->flags |= FTRACE_FL_ENABLED;
1646 return FTRACE_UPDATE_MAKE_CALL;
1650 rec->flags &= ~FTRACE_FL_ENABLED;
1652 return FTRACE_UPDATE_MAKE_NOP;
1656 * ftrace_update_record, set a record that now is tracing or not
1657 * @rec: the record to update
1658 * @enable: set to 1 if the record is tracing, zero to force disable
1660 * The records that represent all functions that can be traced need
1661 * to be updated when tracing has been enabled.
1663 int ftrace_update_record(struct dyn_ftrace *rec, int enable)
1665 return ftrace_check_record(rec, enable, 1);
1669 * ftrace_test_record, check if the record has been enabled or not
1670 * @rec: the record to test
1671 * @enable: set to 1 to check if enabled, 0 if it is disabled
1673 * The arch code may need to test if a record is already set to
1674 * tracing to determine how to modify the function code that it
1677 int ftrace_test_record(struct dyn_ftrace *rec, int enable)
1679 return ftrace_check_record(rec, enable, 0);
1683 __ftrace_replace_code(struct dyn_ftrace *rec, int enable)
1685 unsigned long ftrace_addr;
1688 ftrace_addr = (unsigned long)FTRACE_ADDR;
1690 ret = ftrace_update_record(rec, enable);
1693 case FTRACE_UPDATE_IGNORE:
1696 case FTRACE_UPDATE_MAKE_CALL:
1697 return ftrace_make_call(rec, ftrace_addr);
1699 case FTRACE_UPDATE_MAKE_NOP:
1700 return ftrace_make_nop(NULL, rec, ftrace_addr);
1703 return -1; /* unknow ftrace bug */
1706 void __weak ftrace_replace_code(int enable)
1708 struct dyn_ftrace *rec;
1709 struct ftrace_page *pg;
1712 if (unlikely(ftrace_disabled))
1715 do_for_each_ftrace_rec(pg, rec) {
1716 failed = __ftrace_replace_code(rec, enable);
1718 ftrace_bug(failed, rec->ip);
1719 /* Stop processing */
1722 } while_for_each_ftrace_rec();
1725 struct ftrace_rec_iter {
1726 struct ftrace_page *pg;
1731 * ftrace_rec_iter_start, start up iterating over traced functions
1733 * Returns an iterator handle that is used to iterate over all
1734 * the records that represent address locations where functions
1737 * May return NULL if no records are available.
1739 struct ftrace_rec_iter *ftrace_rec_iter_start(void)
1742 * We only use a single iterator.
1743 * Protected by the ftrace_lock mutex.
1745 static struct ftrace_rec_iter ftrace_rec_iter;
1746 struct ftrace_rec_iter *iter = &ftrace_rec_iter;
1748 iter->pg = ftrace_pages_start;
1751 /* Could have empty pages */
1752 while (iter->pg && !iter->pg->index)
1753 iter->pg = iter->pg->next;
1762 * ftrace_rec_iter_next, get the next record to process.
1763 * @iter: The handle to the iterator.
1765 * Returns the next iterator after the given iterator @iter.
1767 struct ftrace_rec_iter *ftrace_rec_iter_next(struct ftrace_rec_iter *iter)
1771 if (iter->index >= iter->pg->index) {
1772 iter->pg = iter->pg->next;
1775 /* Could have empty pages */
1776 while (iter->pg && !iter->pg->index)
1777 iter->pg = iter->pg->next;
1787 * ftrace_rec_iter_record, get the record at the iterator location
1788 * @iter: The current iterator location
1790 * Returns the record that the current @iter is at.
1792 struct dyn_ftrace *ftrace_rec_iter_record(struct ftrace_rec_iter *iter)
1794 return &iter->pg->records[iter->index];
1798 ftrace_code_disable(struct module *mod, struct dyn_ftrace *rec)
1805 if (unlikely(ftrace_disabled))
1808 ret = ftrace_make_nop(mod, rec, MCOUNT_ADDR);
1810 ftrace_bug(ret, ip);
1817 * archs can override this function if they must do something
1818 * before the modifying code is performed.
1820 int __weak ftrace_arch_code_modify_prepare(void)
1826 * archs can override this function if they must do something
1827 * after the modifying code is performed.
1829 int __weak ftrace_arch_code_modify_post_process(void)
1834 void ftrace_modify_all_code(int command)
1836 if (command & FTRACE_UPDATE_CALLS)
1837 ftrace_replace_code(1);
1838 else if (command & FTRACE_DISABLE_CALLS)
1839 ftrace_replace_code(0);
1841 if (command & FTRACE_UPDATE_TRACE_FUNC)
1842 ftrace_update_ftrace_func(ftrace_trace_function);
1844 if (command & FTRACE_START_FUNC_RET)
1845 ftrace_enable_ftrace_graph_caller();
1846 else if (command & FTRACE_STOP_FUNC_RET)
1847 ftrace_disable_ftrace_graph_caller();
1850 static int __ftrace_modify_code(void *data)
1852 int *command = data;
1854 ftrace_modify_all_code(*command);
1860 * ftrace_run_stop_machine, go back to the stop machine method
1861 * @command: The command to tell ftrace what to do
1863 * If an arch needs to fall back to the stop machine method, the
1864 * it can call this function.
1866 void ftrace_run_stop_machine(int command)
1868 stop_machine(__ftrace_modify_code, &command, NULL);
1872 * arch_ftrace_update_code, modify the code to trace or not trace
1873 * @command: The command that needs to be done
1875 * Archs can override this function if it does not need to
1876 * run stop_machine() to modify code.
1878 void __weak arch_ftrace_update_code(int command)
1880 ftrace_run_stop_machine(command);
1883 static void ftrace_run_update_code(int command)
1887 ret = ftrace_arch_code_modify_prepare();
1888 FTRACE_WARN_ON(ret);
1892 * Do not call function tracer while we update the code.
1893 * We are in stop machine.
1895 function_trace_stop++;
1898 * By default we use stop_machine() to modify the code.
1899 * But archs can do what ever they want as long as it
1900 * is safe. The stop_machine() is the safest, but also
1901 * produces the most overhead.
1903 arch_ftrace_update_code(command);
1905 #ifndef CONFIG_HAVE_FUNCTION_TRACE_MCOUNT_TEST
1907 * For archs that call ftrace_test_stop_func(), we must
1908 * wait till after we update all the function callers
1909 * before we update the callback. This keeps different
1910 * ops that record different functions from corrupting
1913 __ftrace_trace_function = __ftrace_trace_function_delay;
1915 function_trace_stop--;
1917 ret = ftrace_arch_code_modify_post_process();
1918 FTRACE_WARN_ON(ret);
1921 static ftrace_func_t saved_ftrace_func;
1922 static int ftrace_start_up;
1923 static int global_start_up;
1925 static void ftrace_startup_enable(int command)
1927 if (saved_ftrace_func != ftrace_trace_function) {
1928 saved_ftrace_func = ftrace_trace_function;
1929 command |= FTRACE_UPDATE_TRACE_FUNC;
1932 if (!command || !ftrace_enabled)
1935 ftrace_run_update_code(command);
1938 static int ftrace_startup(struct ftrace_ops *ops, int command)
1940 bool hash_enable = true;
1942 if (unlikely(ftrace_disabled))
1946 command |= FTRACE_UPDATE_CALLS;
1948 /* ops marked global share the filter hashes */
1949 if (ops->flags & FTRACE_OPS_FL_GLOBAL) {
1951 /* Don't update hash if global is already set */
1952 if (global_start_up)
1953 hash_enable = false;
1957 ops->flags |= FTRACE_OPS_FL_ENABLED;
1959 ftrace_hash_rec_enable(ops, 1);
1961 ftrace_startup_enable(command);
1966 static void ftrace_shutdown(struct ftrace_ops *ops, int command)
1968 bool hash_disable = true;
1970 if (unlikely(ftrace_disabled))
1975 * Just warn in case of unbalance, no need to kill ftrace, it's not
1976 * critical but the ftrace_call callers may be never nopped again after
1977 * further ftrace uses.
1979 WARN_ON_ONCE(ftrace_start_up < 0);
1981 if (ops->flags & FTRACE_OPS_FL_GLOBAL) {
1984 WARN_ON_ONCE(global_start_up < 0);
1985 /* Don't update hash if global still has users */
1986 if (global_start_up) {
1987 WARN_ON_ONCE(!ftrace_start_up);
1988 hash_disable = false;
1993 ftrace_hash_rec_disable(ops, 1);
1995 if (ops != &global_ops || !global_start_up)
1996 ops->flags &= ~FTRACE_OPS_FL_ENABLED;
1998 command |= FTRACE_UPDATE_CALLS;
2000 if (saved_ftrace_func != ftrace_trace_function) {
2001 saved_ftrace_func = ftrace_trace_function;
2002 command |= FTRACE_UPDATE_TRACE_FUNC;
2005 if (!command || !ftrace_enabled)
2008 ftrace_run_update_code(command);
2011 static void ftrace_startup_sysctl(void)
2013 if (unlikely(ftrace_disabled))
2016 /* Force update next time */
2017 saved_ftrace_func = NULL;
2018 /* ftrace_start_up is true if we want ftrace running */
2019 if (ftrace_start_up)
2020 ftrace_run_update_code(FTRACE_UPDATE_CALLS);
2023 static void ftrace_shutdown_sysctl(void)
2025 if (unlikely(ftrace_disabled))
2028 /* ftrace_start_up is true if ftrace is running */
2029 if (ftrace_start_up)
2030 ftrace_run_update_code(FTRACE_DISABLE_CALLS);
2033 static cycle_t ftrace_update_time;
2034 static unsigned long ftrace_update_cnt;
2035 unsigned long ftrace_update_tot_cnt;
2037 static int ops_traces_mod(struct ftrace_ops *ops)
2039 struct ftrace_hash *hash;
2041 hash = ops->filter_hash;
2042 return ftrace_hash_empty(hash);
2045 static int ftrace_update_code(struct module *mod)
2047 struct ftrace_page *pg;
2048 struct dyn_ftrace *p;
2049 cycle_t start, stop;
2050 unsigned long ref = 0;
2054 * When adding a module, we need to check if tracers are
2055 * currently enabled and if they are set to trace all functions.
2056 * If they are, we need to enable the module functions as well
2057 * as update the reference counts for those function records.
2060 struct ftrace_ops *ops;
2062 for (ops = ftrace_ops_list;
2063 ops != &ftrace_list_end; ops = ops->next) {
2064 if (ops->flags & FTRACE_OPS_FL_ENABLED &&
2065 ops_traces_mod(ops))
2070 start = ftrace_now(raw_smp_processor_id());
2071 ftrace_update_cnt = 0;
2073 for (pg = ftrace_new_pgs; pg; pg = pg->next) {
2075 for (i = 0; i < pg->index; i++) {
2076 /* If something went wrong, bail without enabling anything */
2077 if (unlikely(ftrace_disabled))
2080 p = &pg->records[i];
2084 * Do the initial record conversion from mcount jump
2085 * to the NOP instructions.
2087 if (!ftrace_code_disable(mod, p))
2090 ftrace_update_cnt++;
2093 * If the tracing is enabled, go ahead and enable the record.
2095 * The reason not to enable the record immediatelly is the
2096 * inherent check of ftrace_make_nop/ftrace_make_call for
2097 * correct previous instructions. Making first the NOP
2098 * conversion puts the module to the correct state, thus
2099 * passing the ftrace_make_call check.
2101 if (ftrace_start_up && ref) {
2102 int failed = __ftrace_replace_code(p, 1);
2104 ftrace_bug(failed, p->ip);
2109 ftrace_new_pgs = NULL;
2111 stop = ftrace_now(raw_smp_processor_id());
2112 ftrace_update_time = stop - start;
2113 ftrace_update_tot_cnt += ftrace_update_cnt;
2118 static int ftrace_allocate_records(struct ftrace_page *pg, int count)
2123 if (WARN_ON(!count))
2126 order = get_count_order(DIV_ROUND_UP(count, ENTRIES_PER_PAGE));
2129 * We want to fill as much as possible. No more than a page
2132 while ((PAGE_SIZE << order) / ENTRY_SIZE >= count + ENTRIES_PER_PAGE)
2136 pg->records = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, order);
2139 /* if we can't allocate this size, try something smaller */
2146 cnt = (PAGE_SIZE << order) / ENTRY_SIZE;
2155 static struct ftrace_page *
2156 ftrace_allocate_pages(unsigned long num_to_init)
2158 struct ftrace_page *start_pg;
2159 struct ftrace_page *pg;
2166 start_pg = pg = kzalloc(sizeof(*pg), GFP_KERNEL);
2171 * Try to allocate as much as possible in one continues
2172 * location that fills in all of the space. We want to
2173 * waste as little space as possible.
2176 cnt = ftrace_allocate_records(pg, num_to_init);
2184 pg->next = kzalloc(sizeof(*pg), GFP_KERNEL);
2195 order = get_count_order(pg->size / ENTRIES_PER_PAGE);
2196 free_pages((unsigned long)pg->records, order);
2197 start_pg = pg->next;
2201 pr_info("ftrace: FAILED to allocate memory for functions\n");
2205 static int __init ftrace_dyn_table_alloc(unsigned long num_to_init)
2210 pr_info("ftrace: No functions to be traced?\n");
2214 cnt = num_to_init / ENTRIES_PER_PAGE;
2215 pr_info("ftrace: allocating %ld entries in %d pages\n",
2216 num_to_init, cnt + 1);
2221 #define FTRACE_BUFF_MAX (KSYM_SYMBOL_LEN+4) /* room for wildcards */
2223 struct ftrace_iterator {
2226 struct ftrace_page *pg;
2227 struct dyn_ftrace *func;
2228 struct ftrace_func_probe *probe;
2229 struct trace_parser parser;
2230 struct ftrace_hash *hash;
2231 struct ftrace_ops *ops;
2238 t_hash_next(struct seq_file *m, loff_t *pos)
2240 struct ftrace_iterator *iter = m->private;
2241 struct hlist_node *hnd = NULL;
2242 struct hlist_head *hhd;
2248 hnd = &iter->probe->node;
2250 if (iter->hidx >= FTRACE_FUNC_HASHSIZE)
2253 hhd = &ftrace_func_hash[iter->hidx];
2255 if (hlist_empty(hhd)) {
2271 if (WARN_ON_ONCE(!hnd))
2274 iter->probe = hlist_entry(hnd, struct ftrace_func_probe, node);
2279 static void *t_hash_start(struct seq_file *m, loff_t *pos)
2281 struct ftrace_iterator *iter = m->private;
2285 if (!(iter->flags & FTRACE_ITER_DO_HASH))
2288 if (iter->func_pos > *pos)
2292 for (l = 0; l <= (*pos - iter->func_pos); ) {
2293 p = t_hash_next(m, &l);
2300 /* Only set this if we have an item */
2301 iter->flags |= FTRACE_ITER_HASH;
2307 t_hash_show(struct seq_file *m, struct ftrace_iterator *iter)
2309 struct ftrace_func_probe *rec;
2312 if (WARN_ON_ONCE(!rec))
2315 if (rec->ops->print)
2316 return rec->ops->print(m, rec->ip, rec->ops, rec->data);
2318 seq_printf(m, "%ps:%ps", (void *)rec->ip, (void *)rec->ops->func);
2321 seq_printf(m, ":%p", rec->data);
2328 t_next(struct seq_file *m, void *v, loff_t *pos)
2330 struct ftrace_iterator *iter = m->private;
2331 struct ftrace_ops *ops = iter->ops;
2332 struct dyn_ftrace *rec = NULL;
2334 if (unlikely(ftrace_disabled))
2337 if (iter->flags & FTRACE_ITER_HASH)
2338 return t_hash_next(m, pos);
2341 iter->pos = iter->func_pos = *pos;
2343 if (iter->flags & FTRACE_ITER_PRINTALL)
2344 return t_hash_start(m, pos);
2347 if (iter->idx >= iter->pg->index) {
2348 if (iter->pg->next) {
2349 iter->pg = iter->pg->next;
2354 rec = &iter->pg->records[iter->idx++];
2355 if (((iter->flags & FTRACE_ITER_FILTER) &&
2356 !(ftrace_lookup_ip(ops->filter_hash, rec->ip))) ||
2358 ((iter->flags & FTRACE_ITER_NOTRACE) &&
2359 !ftrace_lookup_ip(ops->notrace_hash, rec->ip)) ||
2361 ((iter->flags & FTRACE_ITER_ENABLED) &&
2362 !(rec->flags & ~FTRACE_FL_MASK))) {
2370 return t_hash_start(m, pos);
2377 static void reset_iter_read(struct ftrace_iterator *iter)
2381 iter->flags &= ~(FTRACE_ITER_PRINTALL & FTRACE_ITER_HASH);
2384 static void *t_start(struct seq_file *m, loff_t *pos)
2386 struct ftrace_iterator *iter = m->private;
2387 struct ftrace_ops *ops = iter->ops;
2391 mutex_lock(&ftrace_lock);
2393 if (unlikely(ftrace_disabled))
2397 * If an lseek was done, then reset and start from beginning.
2399 if (*pos < iter->pos)
2400 reset_iter_read(iter);
2403 * For set_ftrace_filter reading, if we have the filter
2404 * off, we can short cut and just print out that all
2405 * functions are enabled.
2407 if (iter->flags & FTRACE_ITER_FILTER &&
2408 ftrace_hash_empty(ops->filter_hash)) {
2410 return t_hash_start(m, pos);
2411 iter->flags |= FTRACE_ITER_PRINTALL;
2412 /* reset in case of seek/pread */
2413 iter->flags &= ~FTRACE_ITER_HASH;
2417 if (iter->flags & FTRACE_ITER_HASH)
2418 return t_hash_start(m, pos);
2421 * Unfortunately, we need to restart at ftrace_pages_start
2422 * every time we let go of the ftrace_mutex. This is because
2423 * those pointers can change without the lock.
2425 iter->pg = ftrace_pages_start;
2427 for (l = 0; l <= *pos; ) {
2428 p = t_next(m, p, &l);
2434 return t_hash_start(m, pos);
2439 static void t_stop(struct seq_file *m, void *p)
2441 mutex_unlock(&ftrace_lock);
2444 static int t_show(struct seq_file *m, void *v)
2446 struct ftrace_iterator *iter = m->private;
2447 struct dyn_ftrace *rec;
2449 if (iter->flags & FTRACE_ITER_HASH)
2450 return t_hash_show(m, iter);
2452 if (iter->flags & FTRACE_ITER_PRINTALL) {
2453 seq_printf(m, "#### all functions enabled ####\n");
2462 seq_printf(m, "%ps", (void *)rec->ip);
2463 if (iter->flags & FTRACE_ITER_ENABLED)
2464 seq_printf(m, " (%ld)",
2465 rec->flags & ~FTRACE_FL_MASK);
2466 seq_printf(m, "\n");
2471 static const struct seq_operations show_ftrace_seq_ops = {
2479 ftrace_avail_open(struct inode *inode, struct file *file)
2481 struct ftrace_iterator *iter;
2483 if (unlikely(ftrace_disabled))
2486 iter = __seq_open_private(file, &show_ftrace_seq_ops, sizeof(*iter));
2488 iter->pg = ftrace_pages_start;
2489 iter->ops = &global_ops;
2492 return iter ? 0 : -ENOMEM;
2496 ftrace_enabled_open(struct inode *inode, struct file *file)
2498 struct ftrace_iterator *iter;
2500 if (unlikely(ftrace_disabled))
2503 iter = __seq_open_private(file, &show_ftrace_seq_ops, sizeof(*iter));
2505 iter->pg = ftrace_pages_start;
2506 iter->flags = FTRACE_ITER_ENABLED;
2507 iter->ops = &global_ops;
2510 return iter ? 0 : -ENOMEM;
2513 static void ftrace_filter_reset(struct ftrace_hash *hash)
2515 mutex_lock(&ftrace_lock);
2516 ftrace_hash_clear(hash);
2517 mutex_unlock(&ftrace_lock);
2521 * ftrace_regex_open - initialize function tracer filter files
2522 * @ops: The ftrace_ops that hold the hash filters
2523 * @flag: The type of filter to process
2524 * @inode: The inode, usually passed in to your open routine
2525 * @file: The file, usually passed in to your open routine
2527 * ftrace_regex_open() initializes the filter files for the
2528 * @ops. Depending on @flag it may process the filter hash or
2529 * the notrace hash of @ops. With this called from the open
2530 * routine, you can use ftrace_filter_write() for the write
2531 * routine if @flag has FTRACE_ITER_FILTER set, or
2532 * ftrace_notrace_write() if @flag has FTRACE_ITER_NOTRACE set.
2533 * ftrace_regex_lseek() should be used as the lseek routine, and
2534 * release must call ftrace_regex_release().
2537 ftrace_regex_open(struct ftrace_ops *ops, int flag,
2538 struct inode *inode, struct file *file)
2540 struct ftrace_iterator *iter;
2541 struct ftrace_hash *hash;
2544 if (unlikely(ftrace_disabled))
2547 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
2551 if (trace_parser_get_init(&iter->parser, FTRACE_BUFF_MAX)) {
2556 if (flag & FTRACE_ITER_NOTRACE)
2557 hash = ops->notrace_hash;
2559 hash = ops->filter_hash;
2564 if (file->f_mode & FMODE_WRITE) {
2565 mutex_lock(&ftrace_lock);
2566 iter->hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, hash);
2567 mutex_unlock(&ftrace_lock);
2570 trace_parser_put(&iter->parser);
2576 mutex_lock(&ftrace_regex_lock);
2578 if ((file->f_mode & FMODE_WRITE) &&
2579 (file->f_flags & O_TRUNC))
2580 ftrace_filter_reset(iter->hash);
2582 if (file->f_mode & FMODE_READ) {
2583 iter->pg = ftrace_pages_start;
2585 ret = seq_open(file, &show_ftrace_seq_ops);
2587 struct seq_file *m = file->private_data;
2591 free_ftrace_hash(iter->hash);
2592 trace_parser_put(&iter->parser);
2596 file->private_data = iter;
2597 mutex_unlock(&ftrace_regex_lock);
2603 ftrace_filter_open(struct inode *inode, struct file *file)
2605 return ftrace_regex_open(&global_ops,
2606 FTRACE_ITER_FILTER | FTRACE_ITER_DO_HASH,
2611 ftrace_notrace_open(struct inode *inode, struct file *file)
2613 return ftrace_regex_open(&global_ops, FTRACE_ITER_NOTRACE,
2618 ftrace_regex_lseek(struct file *file, loff_t offset, int origin)
2622 if (file->f_mode & FMODE_READ)
2623 ret = seq_lseek(file, offset, origin);
2625 file->f_pos = ret = 1;
2630 static int ftrace_match(char *str, char *regex, int len, int type)
2637 if (strcmp(str, regex) == 0)
2640 case MATCH_FRONT_ONLY:
2641 if (strncmp(str, regex, len) == 0)
2644 case MATCH_MIDDLE_ONLY:
2645 if (strstr(str, regex))
2648 case MATCH_END_ONLY:
2650 if (slen >= len && memcmp(str + slen - len, regex, len) == 0)
2659 enter_record(struct ftrace_hash *hash, struct dyn_ftrace *rec, int not)
2661 struct ftrace_func_entry *entry;
2664 entry = ftrace_lookup_ip(hash, rec->ip);
2666 /* Do nothing if it doesn't exist */
2670 free_hash_entry(hash, entry);
2672 /* Do nothing if it exists */
2676 ret = add_hash_entry(hash, rec->ip);
2682 ftrace_match_record(struct dyn_ftrace *rec, char *mod,
2683 char *regex, int len, int type)
2685 char str[KSYM_SYMBOL_LEN];
2688 kallsyms_lookup(rec->ip, NULL, NULL, &modname, str);
2691 /* module lookup requires matching the module */
2692 if (!modname || strcmp(modname, mod))
2695 /* blank search means to match all funcs in the mod */
2700 return ftrace_match(str, regex, len, type);
2704 match_records(struct ftrace_hash *hash, char *buff,
2705 int len, char *mod, int not)
2707 unsigned search_len = 0;
2708 struct ftrace_page *pg;
2709 struct dyn_ftrace *rec;
2710 int type = MATCH_FULL;
2711 char *search = buff;
2716 type = filter_parse_regex(buff, len, &search, ¬);
2717 search_len = strlen(search);
2720 mutex_lock(&ftrace_lock);
2722 if (unlikely(ftrace_disabled))
2725 do_for_each_ftrace_rec(pg, rec) {
2726 if (ftrace_match_record(rec, mod, search, search_len, type)) {
2727 ret = enter_record(hash, rec, not);
2734 } while_for_each_ftrace_rec();
2736 mutex_unlock(&ftrace_lock);
2742 ftrace_match_records(struct ftrace_hash *hash, char *buff, int len)
2744 return match_records(hash, buff, len, NULL, 0);
2748 ftrace_match_module_records(struct ftrace_hash *hash, char *buff, char *mod)
2752 /* blank or '*' mean the same */
2753 if (strcmp(buff, "*") == 0)
2756 /* handle the case of 'dont filter this module' */
2757 if (strcmp(buff, "!") == 0 || strcmp(buff, "!*") == 0) {
2762 return match_records(hash, buff, strlen(buff), mod, not);
2766 * We register the module command as a template to show others how
2767 * to register the a command as well.
2771 ftrace_mod_callback(struct ftrace_hash *hash,
2772 char *func, char *cmd, char *param, int enable)
2778 * cmd == 'mod' because we only registered this func
2779 * for the 'mod' ftrace_func_command.
2780 * But if you register one func with multiple commands,
2781 * you can tell which command was used by the cmd
2785 /* we must have a module name */
2789 mod = strsep(¶m, ":");
2793 ret = ftrace_match_module_records(hash, func, mod);
2802 static struct ftrace_func_command ftrace_mod_cmd = {
2804 .func = ftrace_mod_callback,
2807 static int __init ftrace_mod_cmd_init(void)
2809 return register_ftrace_command(&ftrace_mod_cmd);
2811 device_initcall(ftrace_mod_cmd_init);
2813 static void function_trace_probe_call(unsigned long ip, unsigned long parent_ip,
2814 struct ftrace_ops *op)
2816 struct ftrace_func_probe *entry;
2817 struct hlist_head *hhd;
2818 struct hlist_node *n;
2821 key = hash_long(ip, FTRACE_HASH_BITS);
2823 hhd = &ftrace_func_hash[key];
2825 if (hlist_empty(hhd))
2829 * Disable preemption for these calls to prevent a RCU grace
2830 * period. This syncs the hash iteration and freeing of items
2831 * on the hash. rcu_read_lock is too dangerous here.
2833 preempt_disable_notrace();
2834 hlist_for_each_entry_rcu(entry, n, hhd, node) {
2835 if (entry->ip == ip)
2836 entry->ops->func(ip, parent_ip, &entry->data);
2838 preempt_enable_notrace();
2841 static struct ftrace_ops trace_probe_ops __read_mostly =
2843 .func = function_trace_probe_call,
2846 static int ftrace_probe_registered;
2848 static void __enable_ftrace_function_probe(void)
2853 if (ftrace_probe_registered)
2856 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
2857 struct hlist_head *hhd = &ftrace_func_hash[i];
2861 /* Nothing registered? */
2862 if (i == FTRACE_FUNC_HASHSIZE)
2865 ret = __register_ftrace_function(&trace_probe_ops);
2867 ret = ftrace_startup(&trace_probe_ops, 0);
2869 ftrace_probe_registered = 1;
2872 static void __disable_ftrace_function_probe(void)
2877 if (!ftrace_probe_registered)
2880 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
2881 struct hlist_head *hhd = &ftrace_func_hash[i];
2886 /* no more funcs left */
2887 ret = __unregister_ftrace_function(&trace_probe_ops);
2889 ftrace_shutdown(&trace_probe_ops, 0);
2891 ftrace_probe_registered = 0;
2895 static void ftrace_free_entry_rcu(struct rcu_head *rhp)
2897 struct ftrace_func_probe *entry =
2898 container_of(rhp, struct ftrace_func_probe, rcu);
2900 if (entry->ops->free)
2901 entry->ops->free(&entry->data);
2907 register_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
2910 struct ftrace_func_probe *entry;
2911 struct ftrace_page *pg;
2912 struct dyn_ftrace *rec;
2918 type = filter_parse_regex(glob, strlen(glob), &search, ¬);
2919 len = strlen(search);
2921 /* we do not support '!' for function probes */
2925 mutex_lock(&ftrace_lock);
2927 if (unlikely(ftrace_disabled))
2930 do_for_each_ftrace_rec(pg, rec) {
2932 if (!ftrace_match_record(rec, NULL, search, len, type))
2935 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
2937 /* If we did not process any, then return error */
2948 * The caller might want to do something special
2949 * for each function we find. We call the callback
2950 * to give the caller an opportunity to do so.
2952 if (ops->callback) {
2953 if (ops->callback(rec->ip, &entry->data) < 0) {
2954 /* caller does not like this func */
2961 entry->ip = rec->ip;
2963 key = hash_long(entry->ip, FTRACE_HASH_BITS);
2964 hlist_add_head_rcu(&entry->node, &ftrace_func_hash[key]);
2966 } while_for_each_ftrace_rec();
2967 __enable_ftrace_function_probe();
2970 mutex_unlock(&ftrace_lock);
2976 PROBE_TEST_FUNC = 1,
2981 __unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
2982 void *data, int flags)
2984 struct ftrace_func_probe *entry;
2985 struct hlist_node *n, *tmp;
2986 char str[KSYM_SYMBOL_LEN];
2987 int type = MATCH_FULL;
2991 if (glob && (strcmp(glob, "*") == 0 || !strlen(glob)))
2996 type = filter_parse_regex(glob, strlen(glob), &search, ¬);
2997 len = strlen(search);
2999 /* we do not support '!' for function probes */
3004 mutex_lock(&ftrace_lock);
3005 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
3006 struct hlist_head *hhd = &ftrace_func_hash[i];
3008 hlist_for_each_entry_safe(entry, n, tmp, hhd, node) {
3010 /* break up if statements for readability */
3011 if ((flags & PROBE_TEST_FUNC) && entry->ops != ops)
3014 if ((flags & PROBE_TEST_DATA) && entry->data != data)
3017 /* do this last, since it is the most expensive */
3019 kallsyms_lookup(entry->ip, NULL, NULL,
3021 if (!ftrace_match(str, glob, len, type))
3025 hlist_del(&entry->node);
3026 call_rcu(&entry->rcu, ftrace_free_entry_rcu);
3029 __disable_ftrace_function_probe();
3030 mutex_unlock(&ftrace_lock);
3034 unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
3037 __unregister_ftrace_function_probe(glob, ops, data,
3038 PROBE_TEST_FUNC | PROBE_TEST_DATA);
3042 unregister_ftrace_function_probe_func(char *glob, struct ftrace_probe_ops *ops)
3044 __unregister_ftrace_function_probe(glob, ops, NULL, PROBE_TEST_FUNC);
3047 void unregister_ftrace_function_probe_all(char *glob)
3049 __unregister_ftrace_function_probe(glob, NULL, NULL, 0);
3052 static LIST_HEAD(ftrace_commands);
3053 static DEFINE_MUTEX(ftrace_cmd_mutex);
3055 int register_ftrace_command(struct ftrace_func_command *cmd)
3057 struct ftrace_func_command *p;
3060 mutex_lock(&ftrace_cmd_mutex);
3061 list_for_each_entry(p, &ftrace_commands, list) {
3062 if (strcmp(cmd->name, p->name) == 0) {
3067 list_add(&cmd->list, &ftrace_commands);
3069 mutex_unlock(&ftrace_cmd_mutex);
3074 int unregister_ftrace_command(struct ftrace_func_command *cmd)
3076 struct ftrace_func_command *p, *n;
3079 mutex_lock(&ftrace_cmd_mutex);
3080 list_for_each_entry_safe(p, n, &ftrace_commands, list) {
3081 if (strcmp(cmd->name, p->name) == 0) {
3083 list_del_init(&p->list);
3088 mutex_unlock(&ftrace_cmd_mutex);
3093 static int ftrace_process_regex(struct ftrace_hash *hash,
3094 char *buff, int len, int enable)
3096 char *func, *command, *next = buff;
3097 struct ftrace_func_command *p;
3100 func = strsep(&next, ":");
3103 ret = ftrace_match_records(hash, func, len);
3113 command = strsep(&next, ":");
3115 mutex_lock(&ftrace_cmd_mutex);
3116 list_for_each_entry(p, &ftrace_commands, list) {
3117 if (strcmp(p->name, command) == 0) {
3118 ret = p->func(hash, func, command, next, enable);
3123 mutex_unlock(&ftrace_cmd_mutex);
3129 ftrace_regex_write(struct file *file, const char __user *ubuf,
3130 size_t cnt, loff_t *ppos, int enable)
3132 struct ftrace_iterator *iter;
3133 struct trace_parser *parser;
3139 mutex_lock(&ftrace_regex_lock);
3142 if (unlikely(ftrace_disabled))
3145 if (file->f_mode & FMODE_READ) {
3146 struct seq_file *m = file->private_data;
3149 iter = file->private_data;
3151 parser = &iter->parser;
3152 read = trace_get_user(parser, ubuf, cnt, ppos);
3154 if (read >= 0 && trace_parser_loaded(parser) &&
3155 !trace_parser_cont(parser)) {
3156 ret = ftrace_process_regex(iter->hash, parser->buffer,
3157 parser->idx, enable);
3158 trace_parser_clear(parser);
3165 mutex_unlock(&ftrace_regex_lock);
3171 ftrace_filter_write(struct file *file, const char __user *ubuf,
3172 size_t cnt, loff_t *ppos)
3174 return ftrace_regex_write(file, ubuf, cnt, ppos, 1);
3178 ftrace_notrace_write(struct file *file, const char __user *ubuf,
3179 size_t cnt, loff_t *ppos)
3181 return ftrace_regex_write(file, ubuf, cnt, ppos, 0);
3185 ftrace_set_regex(struct ftrace_ops *ops, unsigned char *buf, int len,
3186 int reset, int enable)
3188 struct ftrace_hash **orig_hash;
3189 struct ftrace_hash *hash;
3192 /* All global ops uses the global ops filters */
3193 if (ops->flags & FTRACE_OPS_FL_GLOBAL)
3196 if (unlikely(ftrace_disabled))
3200 orig_hash = &ops->filter_hash;
3202 orig_hash = &ops->notrace_hash;
3204 hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, *orig_hash);
3208 mutex_lock(&ftrace_regex_lock);
3210 ftrace_filter_reset(hash);
3211 if (buf && !ftrace_match_records(hash, buf, len)) {
3213 goto out_regex_unlock;
3216 mutex_lock(&ftrace_lock);
3217 ret = ftrace_hash_move(ops, enable, orig_hash, hash);
3218 if (!ret && ops->flags & FTRACE_OPS_FL_ENABLED
3220 ftrace_run_update_code(FTRACE_UPDATE_CALLS);
3222 mutex_unlock(&ftrace_lock);
3225 mutex_unlock(&ftrace_regex_lock);
3227 free_ftrace_hash(hash);
3232 * ftrace_set_filter - set a function to filter on in ftrace
3233 * @ops - the ops to set the filter with
3234 * @buf - the string that holds the function filter text.
3235 * @len - the length of the string.
3236 * @reset - non zero to reset all filters before applying this filter.
3238 * Filters denote which functions should be enabled when tracing is enabled.
3239 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
3241 int ftrace_set_filter(struct ftrace_ops *ops, unsigned char *buf,
3244 return ftrace_set_regex(ops, buf, len, reset, 1);
3246 EXPORT_SYMBOL_GPL(ftrace_set_filter);
3249 * ftrace_set_notrace - set a function to not trace in ftrace
3250 * @ops - the ops to set the notrace filter with
3251 * @buf - the string that holds the function notrace text.
3252 * @len - the length of the string.
3253 * @reset - non zero to reset all filters before applying this filter.
3255 * Notrace Filters denote which functions should not be enabled when tracing
3256 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
3259 int ftrace_set_notrace(struct ftrace_ops *ops, unsigned char *buf,
3262 return ftrace_set_regex(ops, buf, len, reset, 0);
3264 EXPORT_SYMBOL_GPL(ftrace_set_notrace);
3266 * ftrace_set_filter - set a function to filter on in ftrace
3267 * @ops - the ops to set the filter with
3268 * @buf - the string that holds the function filter text.
3269 * @len - the length of the string.
3270 * @reset - non zero to reset all filters before applying this filter.
3272 * Filters denote which functions should be enabled when tracing is enabled.
3273 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
3275 void ftrace_set_global_filter(unsigned char *buf, int len, int reset)
3277 ftrace_set_regex(&global_ops, buf, len, reset, 1);
3279 EXPORT_SYMBOL_GPL(ftrace_set_global_filter);
3282 * ftrace_set_notrace - set a function to not trace in ftrace
3283 * @ops - the ops to set the notrace filter with
3284 * @buf - the string that holds the function notrace text.
3285 * @len - the length of the string.
3286 * @reset - non zero to reset all filters before applying this filter.
3288 * Notrace Filters denote which functions should not be enabled when tracing
3289 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
3292 void ftrace_set_global_notrace(unsigned char *buf, int len, int reset)
3294 ftrace_set_regex(&global_ops, buf, len, reset, 0);
3296 EXPORT_SYMBOL_GPL(ftrace_set_global_notrace);
3299 * command line interface to allow users to set filters on boot up.
3301 #define FTRACE_FILTER_SIZE COMMAND_LINE_SIZE
3302 static char ftrace_notrace_buf[FTRACE_FILTER_SIZE] __initdata;
3303 static char ftrace_filter_buf[FTRACE_FILTER_SIZE] __initdata;
3305 static int __init set_ftrace_notrace(char *str)
3307 strncpy(ftrace_notrace_buf, str, FTRACE_FILTER_SIZE);
3310 __setup("ftrace_notrace=", set_ftrace_notrace);
3312 static int __init set_ftrace_filter(char *str)
3314 strncpy(ftrace_filter_buf, str, FTRACE_FILTER_SIZE);
3317 __setup("ftrace_filter=", set_ftrace_filter);
3319 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
3320 static char ftrace_graph_buf[FTRACE_FILTER_SIZE] __initdata;
3321 static int ftrace_set_func(unsigned long *array, int *idx, char *buffer);
3323 static int __init set_graph_function(char *str)
3325 strlcpy(ftrace_graph_buf, str, FTRACE_FILTER_SIZE);
3328 __setup("ftrace_graph_filter=", set_graph_function);
3330 static void __init set_ftrace_early_graph(char *buf)
3336 func = strsep(&buf, ",");
3337 /* we allow only one expression at a time */
3338 ret = ftrace_set_func(ftrace_graph_funcs, &ftrace_graph_count,
3341 printk(KERN_DEBUG "ftrace: function %s not "
3342 "traceable\n", func);
3345 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
3348 ftrace_set_early_filter(struct ftrace_ops *ops, char *buf, int enable)
3353 func = strsep(&buf, ",");
3354 ftrace_set_regex(ops, func, strlen(func), 0, enable);
3358 static void __init set_ftrace_early_filters(void)
3360 if (ftrace_filter_buf[0])
3361 ftrace_set_early_filter(&global_ops, ftrace_filter_buf, 1);
3362 if (ftrace_notrace_buf[0])
3363 ftrace_set_early_filter(&global_ops, ftrace_notrace_buf, 0);
3364 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
3365 if (ftrace_graph_buf[0])
3366 set_ftrace_early_graph(ftrace_graph_buf);
3367 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
3370 int ftrace_regex_release(struct inode *inode, struct file *file)
3372 struct seq_file *m = (struct seq_file *)file->private_data;
3373 struct ftrace_iterator *iter;
3374 struct ftrace_hash **orig_hash;
3375 struct trace_parser *parser;
3379 mutex_lock(&ftrace_regex_lock);
3380 if (file->f_mode & FMODE_READ) {
3383 seq_release(inode, file);
3385 iter = file->private_data;
3387 parser = &iter->parser;
3388 if (trace_parser_loaded(parser)) {
3389 parser->buffer[parser->idx] = 0;
3390 ftrace_match_records(iter->hash, parser->buffer, parser->idx);
3393 trace_parser_put(parser);
3395 if (file->f_mode & FMODE_WRITE) {
3396 filter_hash = !!(iter->flags & FTRACE_ITER_FILTER);
3399 orig_hash = &iter->ops->filter_hash;
3401 orig_hash = &iter->ops->notrace_hash;
3403 mutex_lock(&ftrace_lock);
3404 ret = ftrace_hash_move(iter->ops, filter_hash,
3405 orig_hash, iter->hash);
3406 if (!ret && (iter->ops->flags & FTRACE_OPS_FL_ENABLED)
3408 ftrace_run_update_code(FTRACE_UPDATE_CALLS);
3410 mutex_unlock(&ftrace_lock);
3412 free_ftrace_hash(iter->hash);
3415 mutex_unlock(&ftrace_regex_lock);
3419 static const struct file_operations ftrace_avail_fops = {
3420 .open = ftrace_avail_open,
3422 .llseek = seq_lseek,
3423 .release = seq_release_private,
3426 static const struct file_operations ftrace_enabled_fops = {
3427 .open = ftrace_enabled_open,
3429 .llseek = seq_lseek,
3430 .release = seq_release_private,
3433 static const struct file_operations ftrace_filter_fops = {
3434 .open = ftrace_filter_open,
3436 .write = ftrace_filter_write,
3437 .llseek = ftrace_regex_lseek,
3438 .release = ftrace_regex_release,
3441 static const struct file_operations ftrace_notrace_fops = {
3442 .open = ftrace_notrace_open,
3444 .write = ftrace_notrace_write,
3445 .llseek = ftrace_regex_lseek,
3446 .release = ftrace_regex_release,
3449 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
3451 static DEFINE_MUTEX(graph_lock);
3453 int ftrace_graph_count;
3454 int ftrace_graph_filter_enabled;
3455 unsigned long ftrace_graph_funcs[FTRACE_GRAPH_MAX_FUNCS] __read_mostly;
3458 __g_next(struct seq_file *m, loff_t *pos)
3460 if (*pos >= ftrace_graph_count)
3462 return &ftrace_graph_funcs[*pos];
3466 g_next(struct seq_file *m, void *v, loff_t *pos)
3469 return __g_next(m, pos);
3472 static void *g_start(struct seq_file *m, loff_t *pos)
3474 mutex_lock(&graph_lock);
3476 /* Nothing, tell g_show to print all functions are enabled */
3477 if (!ftrace_graph_filter_enabled && !*pos)
3480 return __g_next(m, pos);
3483 static void g_stop(struct seq_file *m, void *p)
3485 mutex_unlock(&graph_lock);
3488 static int g_show(struct seq_file *m, void *v)
3490 unsigned long *ptr = v;
3495 if (ptr == (unsigned long *)1) {
3496 seq_printf(m, "#### all functions enabled ####\n");
3500 seq_printf(m, "%ps\n", (void *)*ptr);
3505 static const struct seq_operations ftrace_graph_seq_ops = {
3513 ftrace_graph_open(struct inode *inode, struct file *file)
3517 if (unlikely(ftrace_disabled))
3520 mutex_lock(&graph_lock);
3521 if ((file->f_mode & FMODE_WRITE) &&
3522 (file->f_flags & O_TRUNC)) {
3523 ftrace_graph_filter_enabled = 0;
3524 ftrace_graph_count = 0;
3525 memset(ftrace_graph_funcs, 0, sizeof(ftrace_graph_funcs));
3527 mutex_unlock(&graph_lock);
3529 if (file->f_mode & FMODE_READ)
3530 ret = seq_open(file, &ftrace_graph_seq_ops);
3536 ftrace_graph_release(struct inode *inode, struct file *file)
3538 if (file->f_mode & FMODE_READ)
3539 seq_release(inode, file);
3544 ftrace_set_func(unsigned long *array, int *idx, char *buffer)
3546 struct dyn_ftrace *rec;
3547 struct ftrace_page *pg;
3556 type = filter_parse_regex(buffer, strlen(buffer), &search, ¬);
3557 if (!not && *idx >= FTRACE_GRAPH_MAX_FUNCS)
3560 search_len = strlen(search);
3562 mutex_lock(&ftrace_lock);
3564 if (unlikely(ftrace_disabled)) {
3565 mutex_unlock(&ftrace_lock);
3569 do_for_each_ftrace_rec(pg, rec) {
3571 if (ftrace_match_record(rec, NULL, search, search_len, type)) {
3572 /* if it is in the array */
3574 for (i = 0; i < *idx; i++) {
3575 if (array[i] == rec->ip) {
3584 array[(*idx)++] = rec->ip;
3585 if (*idx >= FTRACE_GRAPH_MAX_FUNCS)
3590 array[i] = array[--(*idx)];
3596 } while_for_each_ftrace_rec();
3598 mutex_unlock(&ftrace_lock);
3603 ftrace_graph_filter_enabled = 1;
3608 ftrace_graph_write(struct file *file, const char __user *ubuf,
3609 size_t cnt, loff_t *ppos)
3611 struct trace_parser parser;
3617 mutex_lock(&graph_lock);
3619 if (trace_parser_get_init(&parser, FTRACE_BUFF_MAX)) {
3624 read = trace_get_user(&parser, ubuf, cnt, ppos);
3626 if (read >= 0 && trace_parser_loaded((&parser))) {
3627 parser.buffer[parser.idx] = 0;
3629 /* we allow only one expression at a time */
3630 ret = ftrace_set_func(ftrace_graph_funcs, &ftrace_graph_count,
3639 trace_parser_put(&parser);
3641 mutex_unlock(&graph_lock);
3646 static const struct file_operations ftrace_graph_fops = {
3647 .open = ftrace_graph_open,
3649 .write = ftrace_graph_write,
3650 .release = ftrace_graph_release,
3651 .llseek = seq_lseek,
3653 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
3655 static __init int ftrace_init_dyn_debugfs(struct dentry *d_tracer)
3658 trace_create_file("available_filter_functions", 0444,
3659 d_tracer, NULL, &ftrace_avail_fops);
3661 trace_create_file("enabled_functions", 0444,
3662 d_tracer, NULL, &ftrace_enabled_fops);
3664 trace_create_file("set_ftrace_filter", 0644, d_tracer,
3665 NULL, &ftrace_filter_fops);
3667 trace_create_file("set_ftrace_notrace", 0644, d_tracer,
3668 NULL, &ftrace_notrace_fops);
3670 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
3671 trace_create_file("set_graph_function", 0444, d_tracer,
3673 &ftrace_graph_fops);
3674 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
3679 static int ftrace_cmp_ips(const void *a, const void *b)
3681 const unsigned long *ipa = a;
3682 const unsigned long *ipb = b;
3691 static void ftrace_swap_ips(void *a, void *b, int size)
3693 unsigned long *ipa = a;
3694 unsigned long *ipb = b;
3702 static int ftrace_process_locs(struct module *mod,
3703 unsigned long *start,
3706 struct ftrace_page *start_pg;
3707 struct ftrace_page *pg;
3708 struct dyn_ftrace *rec;
3709 unsigned long count;
3712 unsigned long flags = 0; /* Shut up gcc */
3715 count = end - start;
3720 sort(start, count, sizeof(*start),
3721 ftrace_cmp_ips, ftrace_swap_ips);
3723 start_pg = ftrace_allocate_pages(count);
3727 mutex_lock(&ftrace_lock);
3730 * Core and each module needs their own pages, as
3731 * modules will free them when they are removed.
3732 * Force a new page to be allocated for modules.
3735 WARN_ON(ftrace_pages || ftrace_pages_start);
3736 /* First initialization */
3737 ftrace_pages = ftrace_pages_start = start_pg;
3742 if (WARN_ON(ftrace_pages->next)) {
3743 /* Hmm, we have free pages? */
3744 while (ftrace_pages->next)
3745 ftrace_pages = ftrace_pages->next;
3748 ftrace_pages->next = start_pg;
3754 addr = ftrace_call_adjust(*p++);
3756 * Some architecture linkers will pad between
3757 * the different mcount_loc sections of different
3758 * object files to satisfy alignments.
3759 * Skip any NULL pointers.
3764 if (pg->index == pg->size) {
3765 /* We should have allocated enough */
3766 if (WARN_ON(!pg->next))
3771 rec = &pg->records[pg->index++];
3775 /* We should have used all pages */
3778 /* Assign the last page to ftrace_pages */
3781 /* These new locations need to be initialized */
3782 ftrace_new_pgs = start_pg;
3785 * We only need to disable interrupts on start up
3786 * because we are modifying code that an interrupt
3787 * may execute, and the modification is not atomic.
3788 * But for modules, nothing runs the code we modify
3789 * until we are finished with it, and there's no
3790 * reason to cause large interrupt latencies while we do it.
3793 local_irq_save(flags);
3794 ftrace_update_code(mod);
3796 local_irq_restore(flags);
3799 mutex_unlock(&ftrace_lock);
3804 #ifdef CONFIG_MODULES
3806 #define next_to_ftrace_page(p) container_of(p, struct ftrace_page, next)
3808 void ftrace_release_mod(struct module *mod)
3810 struct dyn_ftrace *rec;
3811 struct ftrace_page **last_pg;
3812 struct ftrace_page *pg;
3815 mutex_lock(&ftrace_lock);
3817 if (ftrace_disabled)
3821 * Each module has its own ftrace_pages, remove
3822 * them from the list.
3824 last_pg = &ftrace_pages_start;
3825 for (pg = ftrace_pages_start; pg; pg = *last_pg) {
3826 rec = &pg->records[0];
3827 if (within_module_core(rec->ip, mod)) {
3829 * As core pages are first, the first
3830 * page should never be a module page.
3832 if (WARN_ON(pg == ftrace_pages_start))
3835 /* Check if we are deleting the last page */
3836 if (pg == ftrace_pages)
3837 ftrace_pages = next_to_ftrace_page(last_pg);
3839 *last_pg = pg->next;
3840 order = get_count_order(pg->size / ENTRIES_PER_PAGE);
3841 free_pages((unsigned long)pg->records, order);
3844 last_pg = &pg->next;
3847 mutex_unlock(&ftrace_lock);
3850 static void ftrace_init_module(struct module *mod,
3851 unsigned long *start, unsigned long *end)
3853 if (ftrace_disabled || start == end)
3855 ftrace_process_locs(mod, start, end);
3858 static int ftrace_module_notify(struct notifier_block *self,
3859 unsigned long val, void *data)
3861 struct module *mod = data;
3864 case MODULE_STATE_COMING:
3865 ftrace_init_module(mod, mod->ftrace_callsites,
3866 mod->ftrace_callsites +
3867 mod->num_ftrace_callsites);
3869 case MODULE_STATE_GOING:
3870 ftrace_release_mod(mod);
3877 static int ftrace_module_notify(struct notifier_block *self,
3878 unsigned long val, void *data)
3882 #endif /* CONFIG_MODULES */
3884 struct notifier_block ftrace_module_nb = {
3885 .notifier_call = ftrace_module_notify,
3889 extern unsigned long __start_mcount_loc[];
3890 extern unsigned long __stop_mcount_loc[];
3892 void __init ftrace_init(void)
3894 unsigned long count, addr, flags;
3897 /* Keep the ftrace pointer to the stub */
3898 addr = (unsigned long)ftrace_stub;
3900 local_irq_save(flags);
3901 ftrace_dyn_arch_init(&addr);
3902 local_irq_restore(flags);
3904 /* ftrace_dyn_arch_init places the return code in addr */
3908 count = __stop_mcount_loc - __start_mcount_loc;
3910 ret = ftrace_dyn_table_alloc(count);
3914 last_ftrace_enabled = ftrace_enabled = 1;
3916 ret = ftrace_process_locs(NULL,
3920 ret = register_module_notifier(&ftrace_module_nb);
3922 pr_warning("Failed to register trace ftrace module notifier\n");
3924 set_ftrace_early_filters();
3928 ftrace_disabled = 1;
3933 static struct ftrace_ops global_ops = {
3934 .func = ftrace_stub,
3937 static int __init ftrace_nodyn_init(void)
3942 device_initcall(ftrace_nodyn_init);
3944 static inline int ftrace_init_dyn_debugfs(struct dentry *d_tracer) { return 0; }
3945 static inline void ftrace_startup_enable(int command) { }
3946 /* Keep as macros so we do not need to define the commands */
3947 # define ftrace_startup(ops, command) \
3949 (ops)->flags |= FTRACE_OPS_FL_ENABLED; \
3952 # define ftrace_shutdown(ops, command) do { } while (0)
3953 # define ftrace_startup_sysctl() do { } while (0)
3954 # define ftrace_shutdown_sysctl() do { } while (0)
3957 ftrace_ops_test(struct ftrace_ops *ops, unsigned long ip)
3962 #endif /* CONFIG_DYNAMIC_FTRACE */
3965 ftrace_ops_control_func(unsigned long ip, unsigned long parent_ip,
3966 struct ftrace_ops *op)
3968 if (unlikely(trace_recursion_test(TRACE_CONTROL_BIT)))
3972 * Some of the ops may be dynamically allocated,
3973 * they must be freed after a synchronize_sched().
3975 preempt_disable_notrace();
3976 trace_recursion_set(TRACE_CONTROL_BIT);
3977 op = rcu_dereference_raw(ftrace_control_list);
3978 while (op != &ftrace_list_end) {
3979 if (!ftrace_function_local_disabled(op) &&
3980 ftrace_ops_test(op, ip))
3981 op->func(ip, parent_ip, op);
3983 op = rcu_dereference_raw(op->next);
3985 trace_recursion_clear(TRACE_CONTROL_BIT);
3986 preempt_enable_notrace();
3989 static struct ftrace_ops control_ops = {
3990 .func = ftrace_ops_control_func,
3994 __ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip,
3995 struct ftrace_ops *ignored)
3997 struct ftrace_ops *op;
3999 if (unlikely(trace_recursion_test(TRACE_INTERNAL_BIT)))
4002 trace_recursion_set(TRACE_INTERNAL_BIT);
4004 * Some of the ops may be dynamically allocated,
4005 * they must be freed after a synchronize_sched().
4007 preempt_disable_notrace();
4008 op = rcu_dereference_raw(ftrace_ops_list);
4009 while (op != &ftrace_list_end) {
4010 if (ftrace_ops_test(op, ip))
4011 op->func(ip, parent_ip, op);
4012 op = rcu_dereference_raw(op->next);
4014 preempt_enable_notrace();
4015 trace_recursion_clear(TRACE_INTERNAL_BIT);
4019 * Some archs only support passing ip and parent_ip. Even though
4020 * the list function ignores the op parameter, we do not want any
4021 * C side effects, where a function is called without the caller
4022 * sending a third parameter.
4024 #if ARCH_SUPPORTS_FTRACE_OPS
4025 static void ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip,
4026 struct ftrace_ops *op)
4028 __ftrace_ops_list_func(ip, parent_ip, NULL);
4031 static void ftrace_ops_no_ops(unsigned long ip, unsigned long parent_ip)
4033 __ftrace_ops_list_func(ip, parent_ip, NULL);
4037 static void clear_ftrace_swapper(void)
4039 struct task_struct *p;
4043 for_each_online_cpu(cpu) {
4045 clear_tsk_trace_trace(p);
4050 static void set_ftrace_swapper(void)
4052 struct task_struct *p;
4056 for_each_online_cpu(cpu) {
4058 set_tsk_trace_trace(p);
4063 static void clear_ftrace_pid(struct pid *pid)
4065 struct task_struct *p;
4068 do_each_pid_task(pid, PIDTYPE_PID, p) {
4069 clear_tsk_trace_trace(p);
4070 } while_each_pid_task(pid, PIDTYPE_PID, p);
4076 static void set_ftrace_pid(struct pid *pid)
4078 struct task_struct *p;
4081 do_each_pid_task(pid, PIDTYPE_PID, p) {
4082 set_tsk_trace_trace(p);
4083 } while_each_pid_task(pid, PIDTYPE_PID, p);
4087 static void clear_ftrace_pid_task(struct pid *pid)
4089 if (pid == ftrace_swapper_pid)
4090 clear_ftrace_swapper();
4092 clear_ftrace_pid(pid);
4095 static void set_ftrace_pid_task(struct pid *pid)
4097 if (pid == ftrace_swapper_pid)
4098 set_ftrace_swapper();
4100 set_ftrace_pid(pid);
4103 static int ftrace_pid_add(int p)
4106 struct ftrace_pid *fpid;
4109 mutex_lock(&ftrace_lock);
4112 pid = ftrace_swapper_pid;
4114 pid = find_get_pid(p);
4121 list_for_each_entry(fpid, &ftrace_pids, list)
4122 if (fpid->pid == pid)
4127 fpid = kmalloc(sizeof(*fpid), GFP_KERNEL);
4131 list_add(&fpid->list, &ftrace_pids);
4134 set_ftrace_pid_task(pid);
4136 ftrace_update_pid_func();
4137 ftrace_startup_enable(0);
4139 mutex_unlock(&ftrace_lock);
4143 if (pid != ftrace_swapper_pid)
4147 mutex_unlock(&ftrace_lock);
4151 static void ftrace_pid_reset(void)
4153 struct ftrace_pid *fpid, *safe;
4155 mutex_lock(&ftrace_lock);
4156 list_for_each_entry_safe(fpid, safe, &ftrace_pids, list) {
4157 struct pid *pid = fpid->pid;
4159 clear_ftrace_pid_task(pid);
4161 list_del(&fpid->list);
4165 ftrace_update_pid_func();
4166 ftrace_startup_enable(0);
4168 mutex_unlock(&ftrace_lock);
4171 static void *fpid_start(struct seq_file *m, loff_t *pos)
4173 mutex_lock(&ftrace_lock);
4175 if (list_empty(&ftrace_pids) && (!*pos))
4178 return seq_list_start(&ftrace_pids, *pos);
4181 static void *fpid_next(struct seq_file *m, void *v, loff_t *pos)
4186 return seq_list_next(v, &ftrace_pids, pos);
4189 static void fpid_stop(struct seq_file *m, void *p)
4191 mutex_unlock(&ftrace_lock);
4194 static int fpid_show(struct seq_file *m, void *v)
4196 const struct ftrace_pid *fpid = list_entry(v, struct ftrace_pid, list);
4198 if (v == (void *)1) {
4199 seq_printf(m, "no pid\n");
4203 if (fpid->pid == ftrace_swapper_pid)
4204 seq_printf(m, "swapper tasks\n");
4206 seq_printf(m, "%u\n", pid_vnr(fpid->pid));
4211 static const struct seq_operations ftrace_pid_sops = {
4212 .start = fpid_start,
4219 ftrace_pid_open(struct inode *inode, struct file *file)
4223 if ((file->f_mode & FMODE_WRITE) &&
4224 (file->f_flags & O_TRUNC))
4227 if (file->f_mode & FMODE_READ)
4228 ret = seq_open(file, &ftrace_pid_sops);
4234 ftrace_pid_write(struct file *filp, const char __user *ubuf,
4235 size_t cnt, loff_t *ppos)
4241 if (cnt >= sizeof(buf))
4244 if (copy_from_user(&buf, ubuf, cnt))
4250 * Allow "echo > set_ftrace_pid" or "echo -n '' > set_ftrace_pid"
4251 * to clean the filter quietly.
4253 tmp = strstrip(buf);
4254 if (strlen(tmp) == 0)
4257 ret = strict_strtol(tmp, 10, &val);
4261 ret = ftrace_pid_add(val);
4263 return ret ? ret : cnt;
4267 ftrace_pid_release(struct inode *inode, struct file *file)
4269 if (file->f_mode & FMODE_READ)
4270 seq_release(inode, file);
4275 static const struct file_operations ftrace_pid_fops = {
4276 .open = ftrace_pid_open,
4277 .write = ftrace_pid_write,
4279 .llseek = seq_lseek,
4280 .release = ftrace_pid_release,
4283 static __init int ftrace_init_debugfs(void)
4285 struct dentry *d_tracer;
4287 d_tracer = tracing_init_dentry();
4291 ftrace_init_dyn_debugfs(d_tracer);
4293 trace_create_file("set_ftrace_pid", 0644, d_tracer,
4294 NULL, &ftrace_pid_fops);
4296 ftrace_profile_debugfs(d_tracer);
4300 fs_initcall(ftrace_init_debugfs);
4303 * ftrace_kill - kill ftrace
4305 * This function should be used by panic code. It stops ftrace
4306 * but in a not so nice way. If you need to simply kill ftrace
4307 * from a non-atomic section, use ftrace_kill.
4309 void ftrace_kill(void)
4311 ftrace_disabled = 1;
4313 clear_ftrace_function();
4317 * Test if ftrace is dead or not.
4319 int ftrace_is_dead(void)
4321 return ftrace_disabled;
4325 * register_ftrace_function - register a function for profiling
4326 * @ops - ops structure that holds the function for profiling.
4328 * Register a function to be called by all functions in the
4331 * Note: @ops->func and all the functions it calls must be labeled
4332 * with "notrace", otherwise it will go into a
4335 int register_ftrace_function(struct ftrace_ops *ops)
4339 mutex_lock(&ftrace_lock);
4341 ret = __register_ftrace_function(ops);
4343 ret = ftrace_startup(ops, 0);
4345 mutex_unlock(&ftrace_lock);
4349 EXPORT_SYMBOL_GPL(register_ftrace_function);
4352 * unregister_ftrace_function - unregister a function for profiling.
4353 * @ops - ops structure that holds the function to unregister
4355 * Unregister a function that was added to be called by ftrace profiling.
4357 int unregister_ftrace_function(struct ftrace_ops *ops)
4361 mutex_lock(&ftrace_lock);
4362 ret = __unregister_ftrace_function(ops);
4364 ftrace_shutdown(ops, 0);
4365 mutex_unlock(&ftrace_lock);
4369 EXPORT_SYMBOL_GPL(unregister_ftrace_function);
4372 ftrace_enable_sysctl(struct ctl_table *table, int write,
4373 void __user *buffer, size_t *lenp,
4378 mutex_lock(&ftrace_lock);
4380 if (unlikely(ftrace_disabled))
4383 ret = proc_dointvec(table, write, buffer, lenp, ppos);
4385 if (ret || !write || (last_ftrace_enabled == !!ftrace_enabled))
4388 last_ftrace_enabled = !!ftrace_enabled;
4390 if (ftrace_enabled) {
4392 ftrace_startup_sysctl();
4394 /* we are starting ftrace again */
4395 if (ftrace_ops_list != &ftrace_list_end) {
4396 if (ftrace_ops_list->next == &ftrace_list_end)
4397 ftrace_trace_function = ftrace_ops_list->func;
4399 ftrace_trace_function = ftrace_ops_list_func;
4403 /* stopping ftrace calls (just send to ftrace_stub) */
4404 ftrace_trace_function = ftrace_stub;
4406 ftrace_shutdown_sysctl();
4410 mutex_unlock(&ftrace_lock);
4414 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
4416 static int ftrace_graph_active;
4417 static struct notifier_block ftrace_suspend_notifier;
4419 int ftrace_graph_entry_stub(struct ftrace_graph_ent *trace)
4424 /* The callbacks that hook a function */
4425 trace_func_graph_ret_t ftrace_graph_return =
4426 (trace_func_graph_ret_t)ftrace_stub;
4427 trace_func_graph_ent_t ftrace_graph_entry = ftrace_graph_entry_stub;
4429 /* Try to assign a return stack array on FTRACE_RETSTACK_ALLOC_SIZE tasks. */
4430 static int alloc_retstack_tasklist(struct ftrace_ret_stack **ret_stack_list)
4434 unsigned long flags;
4435 int start = 0, end = FTRACE_RETSTACK_ALLOC_SIZE;
4436 struct task_struct *g, *t;
4438 for (i = 0; i < FTRACE_RETSTACK_ALLOC_SIZE; i++) {
4439 ret_stack_list[i] = kmalloc(FTRACE_RETFUNC_DEPTH
4440 * sizeof(struct ftrace_ret_stack),
4442 if (!ret_stack_list[i]) {
4450 read_lock_irqsave(&tasklist_lock, flags);
4451 do_each_thread(g, t) {
4457 if (t->ret_stack == NULL) {
4458 atomic_set(&t->tracing_graph_pause, 0);
4459 atomic_set(&t->trace_overrun, 0);
4460 t->curr_ret_stack = -1;
4461 /* Make sure the tasks see the -1 first: */
4463 t->ret_stack = ret_stack_list[start++];
4465 } while_each_thread(g, t);
4468 read_unlock_irqrestore(&tasklist_lock, flags);
4470 for (i = start; i < end; i++)
4471 kfree(ret_stack_list[i]);
4476 ftrace_graph_probe_sched_switch(void *ignore,
4477 struct task_struct *prev, struct task_struct *next)
4479 unsigned long long timestamp;
4483 * Does the user want to count the time a function was asleep.
4484 * If so, do not update the time stamps.
4486 if (trace_flags & TRACE_ITER_SLEEP_TIME)
4489 timestamp = trace_clock_local();
4491 prev->ftrace_timestamp = timestamp;
4493 /* only process tasks that we timestamped */
4494 if (!next->ftrace_timestamp)
4498 * Update all the counters in next to make up for the
4499 * time next was sleeping.
4501 timestamp -= next->ftrace_timestamp;
4503 for (index = next->curr_ret_stack; index >= 0; index--)
4504 next->ret_stack[index].calltime += timestamp;
4507 /* Allocate a return stack for each task */
4508 static int start_graph_tracing(void)
4510 struct ftrace_ret_stack **ret_stack_list;
4513 ret_stack_list = kmalloc(FTRACE_RETSTACK_ALLOC_SIZE *
4514 sizeof(struct ftrace_ret_stack *),
4517 if (!ret_stack_list)
4520 /* The cpu_boot init_task->ret_stack will never be freed */
4521 for_each_online_cpu(cpu) {
4522 if (!idle_task(cpu)->ret_stack)
4523 ftrace_graph_init_idle_task(idle_task(cpu), cpu);
4527 ret = alloc_retstack_tasklist(ret_stack_list);
4528 } while (ret == -EAGAIN);
4531 ret = register_trace_sched_switch(ftrace_graph_probe_sched_switch, NULL);
4533 pr_info("ftrace_graph: Couldn't activate tracepoint"
4534 " probe to kernel_sched_switch\n");
4537 kfree(ret_stack_list);
4542 * Hibernation protection.
4543 * The state of the current task is too much unstable during
4544 * suspend/restore to disk. We want to protect against that.
4547 ftrace_suspend_notifier_call(struct notifier_block *bl, unsigned long state,
4551 case PM_HIBERNATION_PREPARE:
4552 pause_graph_tracing();
4555 case PM_POST_HIBERNATION:
4556 unpause_graph_tracing();
4562 int register_ftrace_graph(trace_func_graph_ret_t retfunc,
4563 trace_func_graph_ent_t entryfunc)
4567 mutex_lock(&ftrace_lock);
4569 /* we currently allow only one tracer registered at a time */
4570 if (ftrace_graph_active) {
4575 ftrace_suspend_notifier.notifier_call = ftrace_suspend_notifier_call;
4576 register_pm_notifier(&ftrace_suspend_notifier);
4578 ftrace_graph_active++;
4579 ret = start_graph_tracing();
4581 ftrace_graph_active--;
4585 ftrace_graph_return = retfunc;
4586 ftrace_graph_entry = entryfunc;
4588 ret = ftrace_startup(&global_ops, FTRACE_START_FUNC_RET);
4591 mutex_unlock(&ftrace_lock);
4595 void unregister_ftrace_graph(void)
4597 mutex_lock(&ftrace_lock);
4599 if (unlikely(!ftrace_graph_active))
4602 ftrace_graph_active--;
4603 ftrace_graph_return = (trace_func_graph_ret_t)ftrace_stub;
4604 ftrace_graph_entry = ftrace_graph_entry_stub;
4605 ftrace_shutdown(&global_ops, FTRACE_STOP_FUNC_RET);
4606 unregister_pm_notifier(&ftrace_suspend_notifier);
4607 unregister_trace_sched_switch(ftrace_graph_probe_sched_switch, NULL);
4610 mutex_unlock(&ftrace_lock);
4613 static DEFINE_PER_CPU(struct ftrace_ret_stack *, idle_ret_stack);
4616 graph_init_task(struct task_struct *t, struct ftrace_ret_stack *ret_stack)
4618 atomic_set(&t->tracing_graph_pause, 0);
4619 atomic_set(&t->trace_overrun, 0);
4620 t->ftrace_timestamp = 0;
4621 /* make curr_ret_stack visible before we add the ret_stack */
4623 t->ret_stack = ret_stack;
4627 * Allocate a return stack for the idle task. May be the first
4628 * time through, or it may be done by CPU hotplug online.
4630 void ftrace_graph_init_idle_task(struct task_struct *t, int cpu)
4632 t->curr_ret_stack = -1;
4634 * The idle task has no parent, it either has its own
4635 * stack or no stack at all.
4638 WARN_ON(t->ret_stack != per_cpu(idle_ret_stack, cpu));
4640 if (ftrace_graph_active) {
4641 struct ftrace_ret_stack *ret_stack;
4643 ret_stack = per_cpu(idle_ret_stack, cpu);
4645 ret_stack = kmalloc(FTRACE_RETFUNC_DEPTH
4646 * sizeof(struct ftrace_ret_stack),
4650 per_cpu(idle_ret_stack, cpu) = ret_stack;
4652 graph_init_task(t, ret_stack);
4656 /* Allocate a return stack for newly created task */
4657 void ftrace_graph_init_task(struct task_struct *t)
4659 /* Make sure we do not use the parent ret_stack */
4660 t->ret_stack = NULL;
4661 t->curr_ret_stack = -1;
4663 if (ftrace_graph_active) {
4664 struct ftrace_ret_stack *ret_stack;
4666 ret_stack = kmalloc(FTRACE_RETFUNC_DEPTH
4667 * sizeof(struct ftrace_ret_stack),
4671 graph_init_task(t, ret_stack);
4675 void ftrace_graph_exit_task(struct task_struct *t)
4677 struct ftrace_ret_stack *ret_stack = t->ret_stack;
4679 t->ret_stack = NULL;
4680 /* NULL must become visible to IRQs before we free it: */
4686 void ftrace_graph_stop(void)