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 Nadia Yvette Chambers
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 #ifdef CONFIG_DYNAMIC_FTRACE
68 #define INIT_REGEX_LOCK(opsname) \
69 .regex_lock = __MUTEX_INITIALIZER(opsname.regex_lock),
71 #define INIT_REGEX_LOCK(opsname)
74 static struct ftrace_ops ftrace_list_end __read_mostly = {
76 .flags = FTRACE_OPS_FL_RECURSION_SAFE | FTRACE_OPS_FL_STUB,
79 /* ftrace_enabled is a method to turn ftrace on or off */
80 int ftrace_enabled __read_mostly;
81 static int last_ftrace_enabled;
83 /* Quick disabling of function tracer. */
84 int function_trace_stop __read_mostly;
86 /* Current function tracing op */
87 struct ftrace_ops *function_trace_op __read_mostly = &ftrace_list_end;
89 /* List for set_ftrace_pid's pids. */
90 LIST_HEAD(ftrace_pids);
92 struct list_head list;
97 * ftrace_disabled is set when an anomaly is discovered.
98 * ftrace_disabled is much stronger than ftrace_enabled.
100 static int ftrace_disabled __read_mostly;
102 static DEFINE_MUTEX(ftrace_lock);
104 static struct ftrace_ops *ftrace_global_list __read_mostly = &ftrace_list_end;
105 static struct ftrace_ops *ftrace_control_list __read_mostly = &ftrace_list_end;
106 static struct ftrace_ops *ftrace_ops_list __read_mostly = &ftrace_list_end;
107 ftrace_func_t ftrace_trace_function __read_mostly = ftrace_stub;
108 ftrace_func_t ftrace_pid_function __read_mostly = ftrace_stub;
109 static struct ftrace_ops global_ops;
110 static struct ftrace_ops control_ops;
112 #if ARCH_SUPPORTS_FTRACE_OPS
113 static void ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip,
114 struct ftrace_ops *op, struct pt_regs *regs);
116 /* See comment below, where ftrace_ops_list_func is defined */
117 static void ftrace_ops_no_ops(unsigned long ip, unsigned long parent_ip);
118 #define ftrace_ops_list_func ((ftrace_func_t)ftrace_ops_no_ops)
122 * Traverse the ftrace_global_list, invoking all entries. The reason that we
123 * can use rcu_dereference_raw_notrace() is that elements removed from this list
124 * are simply leaked, so there is no need to interact with a grace-period
125 * mechanism. The rcu_dereference_raw_notrace() calls are needed to handle
126 * concurrent insertions into the ftrace_global_list.
128 * Silly Alpha and silly pointer-speculation compiler optimizations!
130 #define do_for_each_ftrace_op(op, list) \
131 op = rcu_dereference_raw_notrace(list); \
135 * Optimized for just a single item in the list (as that is the normal case).
137 #define while_for_each_ftrace_op(op) \
138 while (likely(op = rcu_dereference_raw_notrace((op)->next)) && \
139 unlikely((op) != &ftrace_list_end))
141 static inline void ftrace_ops_init(struct ftrace_ops *ops)
143 #ifdef CONFIG_DYNAMIC_FTRACE
144 if (!(ops->flags & FTRACE_OPS_FL_INITIALIZED)) {
145 mutex_init(&ops->regex_lock);
146 ops->flags |= FTRACE_OPS_FL_INITIALIZED;
152 * ftrace_nr_registered_ops - return number of ops registered
154 * Returns the number of ftrace_ops registered and tracing functions
156 int ftrace_nr_registered_ops(void)
158 struct ftrace_ops *ops;
161 mutex_lock(&ftrace_lock);
163 for (ops = ftrace_ops_list;
164 ops != &ftrace_list_end; ops = ops->next)
167 mutex_unlock(&ftrace_lock);
173 ftrace_global_list_func(unsigned long ip, unsigned long parent_ip,
174 struct ftrace_ops *op, struct pt_regs *regs)
178 bit = trace_test_and_set_recursion(TRACE_GLOBAL_START, TRACE_GLOBAL_MAX);
182 do_for_each_ftrace_op(op, ftrace_global_list) {
183 op->func(ip, parent_ip, op, regs);
184 } while_for_each_ftrace_op(op);
186 trace_clear_recursion(bit);
189 static void ftrace_pid_func(unsigned long ip, unsigned long parent_ip,
190 struct ftrace_ops *op, struct pt_regs *regs)
192 if (!test_tsk_trace_trace(current))
195 ftrace_pid_function(ip, parent_ip, op, regs);
198 static void set_ftrace_pid_function(ftrace_func_t func)
200 /* do not set ftrace_pid_function to itself! */
201 if (func != ftrace_pid_func)
202 ftrace_pid_function = func;
206 * clear_ftrace_function - reset the ftrace function
208 * This NULLs the ftrace function and in essence stops
209 * tracing. There may be lag
211 void clear_ftrace_function(void)
213 ftrace_trace_function = ftrace_stub;
214 ftrace_pid_function = ftrace_stub;
217 static void control_ops_disable_all(struct ftrace_ops *ops)
221 for_each_possible_cpu(cpu)
222 *per_cpu_ptr(ops->disabled, cpu) = 1;
225 static int control_ops_alloc(struct ftrace_ops *ops)
227 int __percpu *disabled;
229 disabled = alloc_percpu(int);
233 ops->disabled = disabled;
234 control_ops_disable_all(ops);
238 static void control_ops_free(struct ftrace_ops *ops)
240 free_percpu(ops->disabled);
243 static void update_global_ops(void)
248 * If there's only one function registered, then call that
249 * function directly. Otherwise, we need to iterate over the
250 * registered callers.
252 if (ftrace_global_list == &ftrace_list_end ||
253 ftrace_global_list->next == &ftrace_list_end) {
254 func = ftrace_global_list->func;
256 * As we are calling the function directly.
257 * If it does not have recursion protection,
258 * the function_trace_op needs to be updated
261 if (ftrace_global_list->flags & FTRACE_OPS_FL_RECURSION_SAFE)
262 global_ops.flags |= FTRACE_OPS_FL_RECURSION_SAFE;
264 global_ops.flags &= ~FTRACE_OPS_FL_RECURSION_SAFE;
266 func = ftrace_global_list_func;
267 /* The list has its own recursion protection. */
268 global_ops.flags |= FTRACE_OPS_FL_RECURSION_SAFE;
272 /* If we filter on pids, update to use the pid function */
273 if (!list_empty(&ftrace_pids)) {
274 set_ftrace_pid_function(func);
275 func = ftrace_pid_func;
278 global_ops.func = func;
281 static void update_ftrace_function(void)
288 * If we are at the end of the list and this ops is
289 * recursion safe and not dynamic and the arch supports passing ops,
290 * then have the mcount trampoline call the function directly.
292 if (ftrace_ops_list == &ftrace_list_end ||
293 (ftrace_ops_list->next == &ftrace_list_end &&
294 !(ftrace_ops_list->flags & FTRACE_OPS_FL_DYNAMIC) &&
295 (ftrace_ops_list->flags & FTRACE_OPS_FL_RECURSION_SAFE) &&
296 !FTRACE_FORCE_LIST_FUNC)) {
297 /* Set the ftrace_ops that the arch callback uses */
298 if (ftrace_ops_list == &global_ops)
299 function_trace_op = ftrace_global_list;
301 function_trace_op = ftrace_ops_list;
302 func = ftrace_ops_list->func;
304 /* Just use the default ftrace_ops */
305 function_trace_op = &ftrace_list_end;
306 func = ftrace_ops_list_func;
309 ftrace_trace_function = func;
312 static void add_ftrace_ops(struct ftrace_ops **list, struct ftrace_ops *ops)
316 * We are entering ops into the list but another
317 * CPU might be walking that list. We need to make sure
318 * the ops->next pointer is valid before another CPU sees
319 * the ops pointer included into the list.
321 rcu_assign_pointer(*list, ops);
324 static int remove_ftrace_ops(struct ftrace_ops **list, struct ftrace_ops *ops)
326 struct ftrace_ops **p;
329 * If we are removing the last function, then simply point
330 * to the ftrace_stub.
332 if (*list == ops && ops->next == &ftrace_list_end) {
333 *list = &ftrace_list_end;
337 for (p = list; *p != &ftrace_list_end; p = &(*p)->next)
348 static void add_ftrace_list_ops(struct ftrace_ops **list,
349 struct ftrace_ops *main_ops,
350 struct ftrace_ops *ops)
352 int first = *list == &ftrace_list_end;
353 add_ftrace_ops(list, ops);
355 add_ftrace_ops(&ftrace_ops_list, main_ops);
358 static int remove_ftrace_list_ops(struct ftrace_ops **list,
359 struct ftrace_ops *main_ops,
360 struct ftrace_ops *ops)
362 int ret = remove_ftrace_ops(list, ops);
363 if (!ret && *list == &ftrace_list_end)
364 ret = remove_ftrace_ops(&ftrace_ops_list, main_ops);
368 static int __register_ftrace_function(struct ftrace_ops *ops)
370 if (unlikely(ftrace_disabled))
373 if (FTRACE_WARN_ON(ops == &global_ops))
376 if (WARN_ON(ops->flags & FTRACE_OPS_FL_ENABLED))
379 /* We don't support both control and global flags set. */
380 if ((ops->flags & FL_GLOBAL_CONTROL_MASK) == FL_GLOBAL_CONTROL_MASK)
383 #ifndef CONFIG_DYNAMIC_FTRACE_WITH_REGS
385 * If the ftrace_ops specifies SAVE_REGS, then it only can be used
386 * if the arch supports it, or SAVE_REGS_IF_SUPPORTED is also set.
387 * Setting SAVE_REGS_IF_SUPPORTED makes SAVE_REGS irrelevant.
389 if (ops->flags & FTRACE_OPS_FL_SAVE_REGS &&
390 !(ops->flags & FTRACE_OPS_FL_SAVE_REGS_IF_SUPPORTED))
393 if (ops->flags & FTRACE_OPS_FL_SAVE_REGS_IF_SUPPORTED)
394 ops->flags |= FTRACE_OPS_FL_SAVE_REGS;
397 if (!core_kernel_data((unsigned long)ops))
398 ops->flags |= FTRACE_OPS_FL_DYNAMIC;
400 if (ops->flags & FTRACE_OPS_FL_GLOBAL) {
401 add_ftrace_list_ops(&ftrace_global_list, &global_ops, ops);
402 ops->flags |= FTRACE_OPS_FL_ENABLED;
403 } else if (ops->flags & FTRACE_OPS_FL_CONTROL) {
404 if (control_ops_alloc(ops))
406 add_ftrace_list_ops(&ftrace_control_list, &control_ops, ops);
408 add_ftrace_ops(&ftrace_ops_list, ops);
411 update_ftrace_function();
416 static void ftrace_sync(struct work_struct *work)
419 * This function is just a stub to implement a hard force
420 * of synchronize_sched(). This requires synchronizing
421 * tasks even in userspace and idle.
423 * Yes, function tracing is rude.
427 static int __unregister_ftrace_function(struct ftrace_ops *ops)
434 if (WARN_ON(!(ops->flags & FTRACE_OPS_FL_ENABLED)))
437 if (FTRACE_WARN_ON(ops == &global_ops))
440 if (ops->flags & FTRACE_OPS_FL_GLOBAL) {
441 ret = remove_ftrace_list_ops(&ftrace_global_list,
444 ops->flags &= ~FTRACE_OPS_FL_ENABLED;
445 } else if (ops->flags & FTRACE_OPS_FL_CONTROL) {
446 ret = remove_ftrace_list_ops(&ftrace_control_list,
450 * The ftrace_ops is now removed from the list,
451 * so there'll be no new users. We must ensure
452 * all current users are done before we free
454 * Note synchronize_sched() is not enough, as we
455 * use preempt_disable() to do RCU, but the function
456 * tracer can be called where RCU is not active
457 * (before user_exit()).
459 schedule_on_each_cpu(ftrace_sync);
460 control_ops_free(ops);
463 ret = remove_ftrace_ops(&ftrace_ops_list, ops);
469 update_ftrace_function();
472 * Dynamic ops may be freed, we must make sure that all
473 * callers are done before leaving this function.
475 * Again, normal synchronize_sched() is not good enough.
476 * We need to do a hard force of sched synchronization.
478 if (ops->flags & FTRACE_OPS_FL_DYNAMIC)
479 schedule_on_each_cpu(ftrace_sync);
485 static void ftrace_update_pid_func(void)
487 /* Only do something if we are tracing something */
488 if (ftrace_trace_function == ftrace_stub)
491 update_ftrace_function();
494 #ifdef CONFIG_FUNCTION_PROFILER
495 struct ftrace_profile {
496 struct hlist_node node;
498 unsigned long counter;
499 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
500 unsigned long long time;
501 unsigned long long time_squared;
505 struct ftrace_profile_page {
506 struct ftrace_profile_page *next;
508 struct ftrace_profile records[];
511 struct ftrace_profile_stat {
513 struct hlist_head *hash;
514 struct ftrace_profile_page *pages;
515 struct ftrace_profile_page *start;
516 struct tracer_stat stat;
519 #define PROFILE_RECORDS_SIZE \
520 (PAGE_SIZE - offsetof(struct ftrace_profile_page, records))
522 #define PROFILES_PER_PAGE \
523 (PROFILE_RECORDS_SIZE / sizeof(struct ftrace_profile))
525 static int ftrace_profile_enabled __read_mostly;
527 /* ftrace_profile_lock - synchronize the enable and disable of the profiler */
528 static DEFINE_MUTEX(ftrace_profile_lock);
530 static DEFINE_PER_CPU(struct ftrace_profile_stat, ftrace_profile_stats);
532 #define FTRACE_PROFILE_HASH_BITS 10
533 #define FTRACE_PROFILE_HASH_SIZE (1 << FTRACE_PROFILE_HASH_BITS)
536 function_stat_next(void *v, int idx)
538 struct ftrace_profile *rec = v;
539 struct ftrace_profile_page *pg;
541 pg = (struct ftrace_profile_page *)((unsigned long)rec & PAGE_MASK);
547 if ((void *)rec >= (void *)&pg->records[pg->index]) {
551 rec = &pg->records[0];
559 static void *function_stat_start(struct tracer_stat *trace)
561 struct ftrace_profile_stat *stat =
562 container_of(trace, struct ftrace_profile_stat, stat);
564 if (!stat || !stat->start)
567 return function_stat_next(&stat->start->records[0], 0);
570 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
571 /* function graph compares on total time */
572 static int function_stat_cmp(void *p1, void *p2)
574 struct ftrace_profile *a = p1;
575 struct ftrace_profile *b = p2;
577 if (a->time < b->time)
579 if (a->time > b->time)
585 /* not function graph compares against hits */
586 static int function_stat_cmp(void *p1, void *p2)
588 struct ftrace_profile *a = p1;
589 struct ftrace_profile *b = p2;
591 if (a->counter < b->counter)
593 if (a->counter > b->counter)
600 static int function_stat_headers(struct seq_file *m)
602 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
603 seq_printf(m, " Function "
606 "--- ---- --- ---\n");
608 seq_printf(m, " Function Hit\n"
614 static int function_stat_show(struct seq_file *m, void *v)
616 struct ftrace_profile *rec = v;
617 char str[KSYM_SYMBOL_LEN];
619 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
620 static struct trace_seq s;
621 unsigned long long avg;
622 unsigned long long stddev;
624 mutex_lock(&ftrace_profile_lock);
626 /* we raced with function_profile_reset() */
627 if (unlikely(rec->counter == 0)) {
632 kallsyms_lookup(rec->ip, NULL, NULL, NULL, str);
633 seq_printf(m, " %-30.30s %10lu", str, rec->counter);
635 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
638 do_div(avg, rec->counter);
640 /* Sample standard deviation (s^2) */
641 if (rec->counter <= 1)
644 stddev = rec->time_squared - rec->counter * avg * avg;
646 * Divide only 1000 for ns^2 -> us^2 conversion.
647 * trace_print_graph_duration will divide 1000 again.
649 do_div(stddev, (rec->counter - 1) * 1000);
653 trace_print_graph_duration(rec->time, &s);
654 trace_seq_puts(&s, " ");
655 trace_print_graph_duration(avg, &s);
656 trace_seq_puts(&s, " ");
657 trace_print_graph_duration(stddev, &s);
658 trace_print_seq(m, &s);
662 mutex_unlock(&ftrace_profile_lock);
667 static void ftrace_profile_reset(struct ftrace_profile_stat *stat)
669 struct ftrace_profile_page *pg;
671 pg = stat->pages = stat->start;
674 memset(pg->records, 0, PROFILE_RECORDS_SIZE);
679 memset(stat->hash, 0,
680 FTRACE_PROFILE_HASH_SIZE * sizeof(struct hlist_head));
683 int ftrace_profile_pages_init(struct ftrace_profile_stat *stat)
685 struct ftrace_profile_page *pg;
690 /* If we already allocated, do nothing */
694 stat->pages = (void *)get_zeroed_page(GFP_KERNEL);
698 #ifdef CONFIG_DYNAMIC_FTRACE
699 functions = ftrace_update_tot_cnt;
702 * We do not know the number of functions that exist because
703 * dynamic tracing is what counts them. With past experience
704 * we have around 20K functions. That should be more than enough.
705 * It is highly unlikely we will execute every function in
711 pg = stat->start = stat->pages;
713 pages = DIV_ROUND_UP(functions, PROFILES_PER_PAGE);
715 for (i = 1; i < pages; i++) {
716 pg->next = (void *)get_zeroed_page(GFP_KERNEL);
727 unsigned long tmp = (unsigned long)pg;
739 static int ftrace_profile_init_cpu(int cpu)
741 struct ftrace_profile_stat *stat;
744 stat = &per_cpu(ftrace_profile_stats, cpu);
747 /* If the profile is already created, simply reset it */
748 ftrace_profile_reset(stat);
753 * We are profiling all functions, but usually only a few thousand
754 * functions are hit. We'll make a hash of 1024 items.
756 size = FTRACE_PROFILE_HASH_SIZE;
758 stat->hash = kzalloc(sizeof(struct hlist_head) * size, GFP_KERNEL);
763 /* Preallocate the function profiling pages */
764 if (ftrace_profile_pages_init(stat) < 0) {
773 static int ftrace_profile_init(void)
778 for_each_online_cpu(cpu) {
779 ret = ftrace_profile_init_cpu(cpu);
787 /* interrupts must be disabled */
788 static struct ftrace_profile *
789 ftrace_find_profiled_func(struct ftrace_profile_stat *stat, unsigned long ip)
791 struct ftrace_profile *rec;
792 struct hlist_head *hhd;
795 key = hash_long(ip, FTRACE_PROFILE_HASH_BITS);
796 hhd = &stat->hash[key];
798 if (hlist_empty(hhd))
801 hlist_for_each_entry_rcu_notrace(rec, hhd, node) {
809 static void ftrace_add_profile(struct ftrace_profile_stat *stat,
810 struct ftrace_profile *rec)
814 key = hash_long(rec->ip, FTRACE_PROFILE_HASH_BITS);
815 hlist_add_head_rcu(&rec->node, &stat->hash[key]);
819 * The memory is already allocated, this simply finds a new record to use.
821 static struct ftrace_profile *
822 ftrace_profile_alloc(struct ftrace_profile_stat *stat, unsigned long ip)
824 struct ftrace_profile *rec = NULL;
826 /* prevent recursion (from NMIs) */
827 if (atomic_inc_return(&stat->disabled) != 1)
831 * Try to find the function again since an NMI
832 * could have added it
834 rec = ftrace_find_profiled_func(stat, ip);
838 if (stat->pages->index == PROFILES_PER_PAGE) {
839 if (!stat->pages->next)
841 stat->pages = stat->pages->next;
844 rec = &stat->pages->records[stat->pages->index++];
846 ftrace_add_profile(stat, rec);
849 atomic_dec(&stat->disabled);
855 function_profile_call(unsigned long ip, unsigned long parent_ip,
856 struct ftrace_ops *ops, struct pt_regs *regs)
858 struct ftrace_profile_stat *stat;
859 struct ftrace_profile *rec;
862 if (!ftrace_profile_enabled)
865 local_irq_save(flags);
867 stat = &__get_cpu_var(ftrace_profile_stats);
868 if (!stat->hash || !ftrace_profile_enabled)
871 rec = ftrace_find_profiled_func(stat, ip);
873 rec = ftrace_profile_alloc(stat, ip);
880 local_irq_restore(flags);
883 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
884 static int profile_graph_entry(struct ftrace_graph_ent *trace)
886 function_profile_call(trace->func, 0, NULL, NULL);
890 static void profile_graph_return(struct ftrace_graph_ret *trace)
892 struct ftrace_profile_stat *stat;
893 unsigned long long calltime;
894 struct ftrace_profile *rec;
897 local_irq_save(flags);
898 stat = &__get_cpu_var(ftrace_profile_stats);
899 if (!stat->hash || !ftrace_profile_enabled)
902 /* If the calltime was zero'd ignore it */
903 if (!trace->calltime)
906 calltime = trace->rettime - trace->calltime;
908 if (!(trace_flags & TRACE_ITER_GRAPH_TIME)) {
911 index = trace->depth;
913 /* Append this call time to the parent time to subtract */
915 current->ret_stack[index - 1].subtime += calltime;
917 if (current->ret_stack[index].subtime < calltime)
918 calltime -= current->ret_stack[index].subtime;
923 rec = ftrace_find_profiled_func(stat, trace->func);
925 rec->time += calltime;
926 rec->time_squared += calltime * calltime;
930 local_irq_restore(flags);
933 static int register_ftrace_profiler(void)
935 return register_ftrace_graph(&profile_graph_return,
936 &profile_graph_entry);
939 static void unregister_ftrace_profiler(void)
941 unregister_ftrace_graph();
944 static struct ftrace_ops ftrace_profile_ops __read_mostly = {
945 .func = function_profile_call,
946 .flags = FTRACE_OPS_FL_RECURSION_SAFE | FTRACE_OPS_FL_INITIALIZED,
947 INIT_REGEX_LOCK(ftrace_profile_ops)
950 static int register_ftrace_profiler(void)
952 return register_ftrace_function(&ftrace_profile_ops);
955 static void unregister_ftrace_profiler(void)
957 unregister_ftrace_function(&ftrace_profile_ops);
959 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
962 ftrace_profile_write(struct file *filp, const char __user *ubuf,
963 size_t cnt, loff_t *ppos)
968 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
974 mutex_lock(&ftrace_profile_lock);
975 if (ftrace_profile_enabled ^ val) {
977 ret = ftrace_profile_init();
983 ret = register_ftrace_profiler();
988 ftrace_profile_enabled = 1;
990 ftrace_profile_enabled = 0;
992 * unregister_ftrace_profiler calls stop_machine
993 * so this acts like an synchronize_sched.
995 unregister_ftrace_profiler();
999 mutex_unlock(&ftrace_profile_lock);
1007 ftrace_profile_read(struct file *filp, char __user *ubuf,
1008 size_t cnt, loff_t *ppos)
1010 char buf[64]; /* big enough to hold a number */
1013 r = sprintf(buf, "%u\n", ftrace_profile_enabled);
1014 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
1017 static const struct file_operations ftrace_profile_fops = {
1018 .open = tracing_open_generic,
1019 .read = ftrace_profile_read,
1020 .write = ftrace_profile_write,
1021 .llseek = default_llseek,
1024 /* used to initialize the real stat files */
1025 static struct tracer_stat function_stats __initdata = {
1026 .name = "functions",
1027 .stat_start = function_stat_start,
1028 .stat_next = function_stat_next,
1029 .stat_cmp = function_stat_cmp,
1030 .stat_headers = function_stat_headers,
1031 .stat_show = function_stat_show
1034 static __init void ftrace_profile_debugfs(struct dentry *d_tracer)
1036 struct ftrace_profile_stat *stat;
1037 struct dentry *entry;
1042 for_each_possible_cpu(cpu) {
1043 stat = &per_cpu(ftrace_profile_stats, cpu);
1045 /* allocate enough for function name + cpu number */
1046 name = kmalloc(32, GFP_KERNEL);
1049 * The files created are permanent, if something happens
1050 * we still do not free memory.
1053 "Could not allocate stat file for cpu %d\n",
1057 stat->stat = function_stats;
1058 snprintf(name, 32, "function%d", cpu);
1059 stat->stat.name = name;
1060 ret = register_stat_tracer(&stat->stat);
1063 "Could not register function stat for cpu %d\n",
1070 entry = debugfs_create_file("function_profile_enabled", 0644,
1071 d_tracer, NULL, &ftrace_profile_fops);
1073 pr_warning("Could not create debugfs "
1074 "'function_profile_enabled' entry\n");
1077 #else /* CONFIG_FUNCTION_PROFILER */
1078 static __init void ftrace_profile_debugfs(struct dentry *d_tracer)
1081 #endif /* CONFIG_FUNCTION_PROFILER */
1083 static struct pid * const ftrace_swapper_pid = &init_struct_pid;
1086 ftrace_filter_lseek(struct file *file, loff_t offset, int whence)
1090 if (file->f_mode & FMODE_READ)
1091 ret = seq_lseek(file, offset, whence);
1093 file->f_pos = ret = 1;
1098 #ifdef CONFIG_DYNAMIC_FTRACE
1100 #ifndef CONFIG_FTRACE_MCOUNT_RECORD
1101 # error Dynamic ftrace depends on MCOUNT_RECORD
1104 static struct hlist_head ftrace_func_hash[FTRACE_FUNC_HASHSIZE] __read_mostly;
1106 struct ftrace_func_probe {
1107 struct hlist_node node;
1108 struct ftrace_probe_ops *ops;
1109 unsigned long flags;
1112 struct list_head free_list;
1115 struct ftrace_func_entry {
1116 struct hlist_node hlist;
1120 struct ftrace_hash {
1121 unsigned long size_bits;
1122 struct hlist_head *buckets;
1123 unsigned long count;
1124 struct rcu_head rcu;
1128 * We make these constant because no one should touch them,
1129 * but they are used as the default "empty hash", to avoid allocating
1130 * it all the time. These are in a read only section such that if
1131 * anyone does try to modify it, it will cause an exception.
1133 static const struct hlist_head empty_buckets[1];
1134 static const struct ftrace_hash empty_hash = {
1135 .buckets = (struct hlist_head *)empty_buckets,
1137 #define EMPTY_HASH ((struct ftrace_hash *)&empty_hash)
1139 static struct ftrace_ops global_ops = {
1140 .func = ftrace_stub,
1141 .notrace_hash = EMPTY_HASH,
1142 .filter_hash = EMPTY_HASH,
1143 .flags = FTRACE_OPS_FL_RECURSION_SAFE | FTRACE_OPS_FL_INITIALIZED,
1144 INIT_REGEX_LOCK(global_ops)
1147 struct ftrace_page {
1148 struct ftrace_page *next;
1149 struct dyn_ftrace *records;
1154 static struct ftrace_page *ftrace_new_pgs;
1156 #define ENTRY_SIZE sizeof(struct dyn_ftrace)
1157 #define ENTRIES_PER_PAGE (PAGE_SIZE / ENTRY_SIZE)
1159 /* estimate from running different kernels */
1160 #define NR_TO_INIT 10000
1162 static struct ftrace_page *ftrace_pages_start;
1163 static struct ftrace_page *ftrace_pages;
1165 static bool ftrace_hash_empty(struct ftrace_hash *hash)
1167 return !hash || !hash->count;
1170 static struct ftrace_func_entry *
1171 ftrace_lookup_ip(struct ftrace_hash *hash, unsigned long ip)
1174 struct ftrace_func_entry *entry;
1175 struct hlist_head *hhd;
1177 if (ftrace_hash_empty(hash))
1180 if (hash->size_bits > 0)
1181 key = hash_long(ip, hash->size_bits);
1185 hhd = &hash->buckets[key];
1187 hlist_for_each_entry_rcu_notrace(entry, hhd, hlist) {
1188 if (entry->ip == ip)
1194 static void __add_hash_entry(struct ftrace_hash *hash,
1195 struct ftrace_func_entry *entry)
1197 struct hlist_head *hhd;
1200 if (hash->size_bits)
1201 key = hash_long(entry->ip, hash->size_bits);
1205 hhd = &hash->buckets[key];
1206 hlist_add_head(&entry->hlist, hhd);
1210 static int add_hash_entry(struct ftrace_hash *hash, unsigned long ip)
1212 struct ftrace_func_entry *entry;
1214 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
1219 __add_hash_entry(hash, entry);
1225 free_hash_entry(struct ftrace_hash *hash,
1226 struct ftrace_func_entry *entry)
1228 hlist_del(&entry->hlist);
1234 remove_hash_entry(struct ftrace_hash *hash,
1235 struct ftrace_func_entry *entry)
1237 hlist_del(&entry->hlist);
1241 static void ftrace_hash_clear(struct ftrace_hash *hash)
1243 struct hlist_head *hhd;
1244 struct hlist_node *tn;
1245 struct ftrace_func_entry *entry;
1246 int size = 1 << hash->size_bits;
1252 for (i = 0; i < size; i++) {
1253 hhd = &hash->buckets[i];
1254 hlist_for_each_entry_safe(entry, tn, hhd, hlist)
1255 free_hash_entry(hash, entry);
1257 FTRACE_WARN_ON(hash->count);
1260 static void free_ftrace_hash(struct ftrace_hash *hash)
1262 if (!hash || hash == EMPTY_HASH)
1264 ftrace_hash_clear(hash);
1265 kfree(hash->buckets);
1269 static void __free_ftrace_hash_rcu(struct rcu_head *rcu)
1271 struct ftrace_hash *hash;
1273 hash = container_of(rcu, struct ftrace_hash, rcu);
1274 free_ftrace_hash(hash);
1277 static void free_ftrace_hash_rcu(struct ftrace_hash *hash)
1279 if (!hash || hash == EMPTY_HASH)
1281 call_rcu_sched(&hash->rcu, __free_ftrace_hash_rcu);
1284 void ftrace_free_filter(struct ftrace_ops *ops)
1286 ftrace_ops_init(ops);
1287 free_ftrace_hash(ops->filter_hash);
1288 free_ftrace_hash(ops->notrace_hash);
1291 static struct ftrace_hash *alloc_ftrace_hash(int size_bits)
1293 struct ftrace_hash *hash;
1296 hash = kzalloc(sizeof(*hash), GFP_KERNEL);
1300 size = 1 << size_bits;
1301 hash->buckets = kcalloc(size, sizeof(*hash->buckets), GFP_KERNEL);
1303 if (!hash->buckets) {
1308 hash->size_bits = size_bits;
1313 static struct ftrace_hash *
1314 alloc_and_copy_ftrace_hash(int size_bits, struct ftrace_hash *hash)
1316 struct ftrace_func_entry *entry;
1317 struct ftrace_hash *new_hash;
1322 new_hash = alloc_ftrace_hash(size_bits);
1327 if (ftrace_hash_empty(hash))
1330 size = 1 << hash->size_bits;
1331 for (i = 0; i < size; i++) {
1332 hlist_for_each_entry(entry, &hash->buckets[i], hlist) {
1333 ret = add_hash_entry(new_hash, entry->ip);
1339 FTRACE_WARN_ON(new_hash->count != hash->count);
1344 free_ftrace_hash(new_hash);
1349 ftrace_hash_rec_disable(struct ftrace_ops *ops, int filter_hash);
1351 ftrace_hash_rec_enable(struct ftrace_ops *ops, int filter_hash);
1354 ftrace_hash_move(struct ftrace_ops *ops, int enable,
1355 struct ftrace_hash **dst, struct ftrace_hash *src)
1357 struct ftrace_func_entry *entry;
1358 struct hlist_node *tn;
1359 struct hlist_head *hhd;
1360 struct ftrace_hash *old_hash;
1361 struct ftrace_hash *new_hash;
1362 int size = src->count;
1368 * Remove the current set, update the hash and add
1371 ftrace_hash_rec_disable(ops, enable);
1374 * If the new source is empty, just free dst and assign it
1378 free_ftrace_hash_rcu(*dst);
1379 rcu_assign_pointer(*dst, EMPTY_HASH);
1380 /* still need to update the function records */
1386 * Make the hash size about 1/2 the # found
1388 for (size /= 2; size; size >>= 1)
1391 /* Don't allocate too much */
1392 if (bits > FTRACE_HASH_MAX_BITS)
1393 bits = FTRACE_HASH_MAX_BITS;
1396 new_hash = alloc_ftrace_hash(bits);
1400 size = 1 << src->size_bits;
1401 for (i = 0; i < size; i++) {
1402 hhd = &src->buckets[i];
1403 hlist_for_each_entry_safe(entry, tn, hhd, hlist) {
1404 remove_hash_entry(src, entry);
1405 __add_hash_entry(new_hash, entry);
1410 rcu_assign_pointer(*dst, new_hash);
1411 free_ftrace_hash_rcu(old_hash);
1416 * Enable regardless of ret:
1417 * On success, we enable the new hash.
1418 * On failure, we re-enable the original hash.
1420 ftrace_hash_rec_enable(ops, enable);
1426 * Test the hashes for this ops to see if we want to call
1427 * the ops->func or not.
1429 * It's a match if the ip is in the ops->filter_hash or
1430 * the filter_hash does not exist or is empty,
1432 * the ip is not in the ops->notrace_hash.
1434 * This needs to be called with preemption disabled as
1435 * the hashes are freed with call_rcu_sched().
1438 ftrace_ops_test(struct ftrace_ops *ops, unsigned long ip)
1440 struct ftrace_hash *filter_hash;
1441 struct ftrace_hash *notrace_hash;
1444 filter_hash = rcu_dereference_raw_notrace(ops->filter_hash);
1445 notrace_hash = rcu_dereference_raw_notrace(ops->notrace_hash);
1447 if ((ftrace_hash_empty(filter_hash) ||
1448 ftrace_lookup_ip(filter_hash, ip)) &&
1449 (ftrace_hash_empty(notrace_hash) ||
1450 !ftrace_lookup_ip(notrace_hash, ip)))
1459 * This is a double for. Do not use 'break' to break out of the loop,
1460 * you must use a goto.
1462 #define do_for_each_ftrace_rec(pg, rec) \
1463 for (pg = ftrace_pages_start; pg; pg = pg->next) { \
1465 for (_____i = 0; _____i < pg->index; _____i++) { \
1466 rec = &pg->records[_____i];
1468 #define while_for_each_ftrace_rec() \
1473 static int ftrace_cmp_recs(const void *a, const void *b)
1475 const struct dyn_ftrace *key = a;
1476 const struct dyn_ftrace *rec = b;
1478 if (key->flags < rec->ip)
1480 if (key->ip >= rec->ip + MCOUNT_INSN_SIZE)
1485 static unsigned long ftrace_location_range(unsigned long start, unsigned long end)
1487 struct ftrace_page *pg;
1488 struct dyn_ftrace *rec;
1489 struct dyn_ftrace key;
1492 key.flags = end; /* overload flags, as it is unsigned long */
1494 for (pg = ftrace_pages_start; pg; pg = pg->next) {
1495 if (end < pg->records[0].ip ||
1496 start >= (pg->records[pg->index - 1].ip + MCOUNT_INSN_SIZE))
1498 rec = bsearch(&key, pg->records, pg->index,
1499 sizeof(struct dyn_ftrace),
1509 * ftrace_location - return true if the ip giving is a traced location
1510 * @ip: the instruction pointer to check
1512 * Returns rec->ip if @ip given is a pointer to a ftrace location.
1513 * That is, the instruction that is either a NOP or call to
1514 * the function tracer. It checks the ftrace internal tables to
1515 * determine if the address belongs or not.
1517 unsigned long ftrace_location(unsigned long ip)
1519 return ftrace_location_range(ip, ip);
1523 * ftrace_text_reserved - return true if range contains an ftrace location
1524 * @start: start of range to search
1525 * @end: end of range to search (inclusive). @end points to the last byte to check.
1527 * Returns 1 if @start and @end contains a ftrace location.
1528 * That is, the instruction that is either a NOP or call to
1529 * the function tracer. It checks the ftrace internal tables to
1530 * determine if the address belongs or not.
1532 int ftrace_text_reserved(void *start, void *end)
1536 ret = ftrace_location_range((unsigned long)start,
1537 (unsigned long)end);
1542 static void __ftrace_hash_rec_update(struct ftrace_ops *ops,
1546 struct ftrace_hash *hash;
1547 struct ftrace_hash *other_hash;
1548 struct ftrace_page *pg;
1549 struct dyn_ftrace *rec;
1553 /* Only update if the ops has been registered */
1554 if (!(ops->flags & FTRACE_OPS_FL_ENABLED))
1558 * In the filter_hash case:
1559 * If the count is zero, we update all records.
1560 * Otherwise we just update the items in the hash.
1562 * In the notrace_hash case:
1563 * We enable the update in the hash.
1564 * As disabling notrace means enabling the tracing,
1565 * and enabling notrace means disabling, the inc variable
1569 hash = ops->filter_hash;
1570 other_hash = ops->notrace_hash;
1571 if (ftrace_hash_empty(hash))
1575 hash = ops->notrace_hash;
1576 other_hash = ops->filter_hash;
1578 * If the notrace hash has no items,
1579 * then there's nothing to do.
1581 if (ftrace_hash_empty(hash))
1585 do_for_each_ftrace_rec(pg, rec) {
1586 int in_other_hash = 0;
1592 * Only the filter_hash affects all records.
1593 * Update if the record is not in the notrace hash.
1595 if (!other_hash || !ftrace_lookup_ip(other_hash, rec->ip))
1598 in_hash = !!ftrace_lookup_ip(hash, rec->ip);
1599 in_other_hash = !!ftrace_lookup_ip(other_hash, rec->ip);
1604 if (filter_hash && in_hash && !in_other_hash)
1606 else if (!filter_hash && in_hash &&
1607 (in_other_hash || ftrace_hash_empty(other_hash)))
1615 if (FTRACE_WARN_ON((rec->flags & ~FTRACE_FL_MASK) == FTRACE_REF_MAX))
1618 * If any ops wants regs saved for this function
1619 * then all ops will get saved regs.
1621 if (ops->flags & FTRACE_OPS_FL_SAVE_REGS)
1622 rec->flags |= FTRACE_FL_REGS;
1624 if (FTRACE_WARN_ON((rec->flags & ~FTRACE_FL_MASK) == 0))
1629 /* Shortcut, if we handled all records, we are done. */
1630 if (!all && count == hash->count)
1632 } while_for_each_ftrace_rec();
1635 static void ftrace_hash_rec_disable(struct ftrace_ops *ops,
1638 __ftrace_hash_rec_update(ops, filter_hash, 0);
1641 static void ftrace_hash_rec_enable(struct ftrace_ops *ops,
1644 __ftrace_hash_rec_update(ops, filter_hash, 1);
1647 static void print_ip_ins(const char *fmt, unsigned char *p)
1651 printk(KERN_CONT "%s", fmt);
1653 for (i = 0; i < MCOUNT_INSN_SIZE; i++)
1654 printk(KERN_CONT "%s%02x", i ? ":" : "", p[i]);
1658 * ftrace_bug - report and shutdown function tracer
1659 * @failed: The failed type (EFAULT, EINVAL, EPERM)
1660 * @ip: The address that failed
1662 * The arch code that enables or disables the function tracing
1663 * can call ftrace_bug() when it has detected a problem in
1664 * modifying the code. @failed should be one of either:
1665 * EFAULT - if the problem happens on reading the @ip address
1666 * EINVAL - if what is read at @ip is not what was expected
1667 * EPERM - if the problem happens on writting to the @ip address
1669 void ftrace_bug(int failed, unsigned long ip)
1673 FTRACE_WARN_ON_ONCE(1);
1674 pr_info("ftrace faulted on modifying ");
1678 FTRACE_WARN_ON_ONCE(1);
1679 pr_info("ftrace failed to modify ");
1681 print_ip_ins(" actual: ", (unsigned char *)ip);
1682 printk(KERN_CONT "\n");
1685 FTRACE_WARN_ON_ONCE(1);
1686 pr_info("ftrace faulted on writing ");
1690 FTRACE_WARN_ON_ONCE(1);
1691 pr_info("ftrace faulted on unknown error ");
1696 static int ftrace_check_record(struct dyn_ftrace *rec, int enable, int update)
1698 unsigned long flag = 0UL;
1701 * If we are updating calls:
1703 * If the record has a ref count, then we need to enable it
1704 * because someone is using it.
1706 * Otherwise we make sure its disabled.
1708 * If we are disabling calls, then disable all records that
1711 if (enable && (rec->flags & ~FTRACE_FL_MASK))
1712 flag = FTRACE_FL_ENABLED;
1715 * If enabling and the REGS flag does not match the REGS_EN, then
1716 * do not ignore this record. Set flags to fail the compare against
1720 (!(rec->flags & FTRACE_FL_REGS) != !(rec->flags & FTRACE_FL_REGS_EN)))
1721 flag |= FTRACE_FL_REGS;
1723 /* If the state of this record hasn't changed, then do nothing */
1724 if ((rec->flags & FTRACE_FL_ENABLED) == flag)
1725 return FTRACE_UPDATE_IGNORE;
1728 /* Save off if rec is being enabled (for return value) */
1729 flag ^= rec->flags & FTRACE_FL_ENABLED;
1732 rec->flags |= FTRACE_FL_ENABLED;
1733 if (flag & FTRACE_FL_REGS) {
1734 if (rec->flags & FTRACE_FL_REGS)
1735 rec->flags |= FTRACE_FL_REGS_EN;
1737 rec->flags &= ~FTRACE_FL_REGS_EN;
1742 * If this record is being updated from a nop, then
1743 * return UPDATE_MAKE_CALL.
1744 * Otherwise, if the EN flag is set, then return
1745 * UPDATE_MODIFY_CALL_REGS to tell the caller to convert
1746 * from the non-save regs, to a save regs function.
1748 * return UPDATE_MODIFY_CALL to tell the caller to convert
1749 * from the save regs, to a non-save regs function.
1751 if (flag & FTRACE_FL_ENABLED)
1752 return FTRACE_UPDATE_MAKE_CALL;
1753 else if (rec->flags & FTRACE_FL_REGS_EN)
1754 return FTRACE_UPDATE_MODIFY_CALL_REGS;
1756 return FTRACE_UPDATE_MODIFY_CALL;
1760 /* If there's no more users, clear all flags */
1761 if (!(rec->flags & ~FTRACE_FL_MASK))
1764 /* Just disable the record (keep REGS state) */
1765 rec->flags &= ~FTRACE_FL_ENABLED;
1768 return FTRACE_UPDATE_MAKE_NOP;
1772 * ftrace_update_record, set a record that now is tracing or not
1773 * @rec: the record to update
1774 * @enable: set to 1 if the record is tracing, zero to force disable
1776 * The records that represent all functions that can be traced need
1777 * to be updated when tracing has been enabled.
1779 int ftrace_update_record(struct dyn_ftrace *rec, int enable)
1781 return ftrace_check_record(rec, enable, 1);
1785 * ftrace_test_record, check if the record has been enabled or not
1786 * @rec: the record to test
1787 * @enable: set to 1 to check if enabled, 0 if it is disabled
1789 * The arch code may need to test if a record is already set to
1790 * tracing to determine how to modify the function code that it
1793 int ftrace_test_record(struct dyn_ftrace *rec, int enable)
1795 return ftrace_check_record(rec, enable, 0);
1799 __ftrace_replace_code(struct dyn_ftrace *rec, int enable)
1801 unsigned long ftrace_old_addr;
1802 unsigned long ftrace_addr;
1805 ret = ftrace_update_record(rec, enable);
1807 if (rec->flags & FTRACE_FL_REGS)
1808 ftrace_addr = (unsigned long)FTRACE_REGS_ADDR;
1810 ftrace_addr = (unsigned long)FTRACE_ADDR;
1813 case FTRACE_UPDATE_IGNORE:
1816 case FTRACE_UPDATE_MAKE_CALL:
1817 return ftrace_make_call(rec, ftrace_addr);
1819 case FTRACE_UPDATE_MAKE_NOP:
1820 return ftrace_make_nop(NULL, rec, ftrace_addr);
1822 case FTRACE_UPDATE_MODIFY_CALL_REGS:
1823 case FTRACE_UPDATE_MODIFY_CALL:
1824 if (rec->flags & FTRACE_FL_REGS)
1825 ftrace_old_addr = (unsigned long)FTRACE_ADDR;
1827 ftrace_old_addr = (unsigned long)FTRACE_REGS_ADDR;
1829 return ftrace_modify_call(rec, ftrace_old_addr, ftrace_addr);
1832 return -1; /* unknow ftrace bug */
1835 void __weak ftrace_replace_code(int enable)
1837 struct dyn_ftrace *rec;
1838 struct ftrace_page *pg;
1841 if (unlikely(ftrace_disabled))
1844 do_for_each_ftrace_rec(pg, rec) {
1845 failed = __ftrace_replace_code(rec, enable);
1847 ftrace_bug(failed, rec->ip);
1848 /* Stop processing */
1851 } while_for_each_ftrace_rec();
1854 struct ftrace_rec_iter {
1855 struct ftrace_page *pg;
1860 * ftrace_rec_iter_start, start up iterating over traced functions
1862 * Returns an iterator handle that is used to iterate over all
1863 * the records that represent address locations where functions
1866 * May return NULL if no records are available.
1868 struct ftrace_rec_iter *ftrace_rec_iter_start(void)
1871 * We only use a single iterator.
1872 * Protected by the ftrace_lock mutex.
1874 static struct ftrace_rec_iter ftrace_rec_iter;
1875 struct ftrace_rec_iter *iter = &ftrace_rec_iter;
1877 iter->pg = ftrace_pages_start;
1880 /* Could have empty pages */
1881 while (iter->pg && !iter->pg->index)
1882 iter->pg = iter->pg->next;
1891 * ftrace_rec_iter_next, get the next record to process.
1892 * @iter: The handle to the iterator.
1894 * Returns the next iterator after the given iterator @iter.
1896 struct ftrace_rec_iter *ftrace_rec_iter_next(struct ftrace_rec_iter *iter)
1900 if (iter->index >= iter->pg->index) {
1901 iter->pg = iter->pg->next;
1904 /* Could have empty pages */
1905 while (iter->pg && !iter->pg->index)
1906 iter->pg = iter->pg->next;
1916 * ftrace_rec_iter_record, get the record at the iterator location
1917 * @iter: The current iterator location
1919 * Returns the record that the current @iter is at.
1921 struct dyn_ftrace *ftrace_rec_iter_record(struct ftrace_rec_iter *iter)
1923 return &iter->pg->records[iter->index];
1927 ftrace_code_disable(struct module *mod, struct dyn_ftrace *rec)
1934 if (unlikely(ftrace_disabled))
1937 ret = ftrace_make_nop(mod, rec, MCOUNT_ADDR);
1939 ftrace_bug(ret, ip);
1946 * archs can override this function if they must do something
1947 * before the modifying code is performed.
1949 int __weak ftrace_arch_code_modify_prepare(void)
1955 * archs can override this function if they must do something
1956 * after the modifying code is performed.
1958 int __weak ftrace_arch_code_modify_post_process(void)
1963 void ftrace_modify_all_code(int command)
1965 if (command & FTRACE_UPDATE_CALLS)
1966 ftrace_replace_code(1);
1967 else if (command & FTRACE_DISABLE_CALLS)
1968 ftrace_replace_code(0);
1970 if (command & FTRACE_UPDATE_TRACE_FUNC)
1971 ftrace_update_ftrace_func(ftrace_trace_function);
1973 if (command & FTRACE_START_FUNC_RET)
1974 ftrace_enable_ftrace_graph_caller();
1975 else if (command & FTRACE_STOP_FUNC_RET)
1976 ftrace_disable_ftrace_graph_caller();
1979 static int __ftrace_modify_code(void *data)
1981 int *command = data;
1983 ftrace_modify_all_code(*command);
1989 * ftrace_run_stop_machine, go back to the stop machine method
1990 * @command: The command to tell ftrace what to do
1992 * If an arch needs to fall back to the stop machine method, the
1993 * it can call this function.
1995 void ftrace_run_stop_machine(int command)
1997 stop_machine(__ftrace_modify_code, &command, NULL);
2001 * arch_ftrace_update_code, modify the code to trace or not trace
2002 * @command: The command that needs to be done
2004 * Archs can override this function if it does not need to
2005 * run stop_machine() to modify code.
2007 void __weak arch_ftrace_update_code(int command)
2009 ftrace_run_stop_machine(command);
2012 static void ftrace_run_update_code(int command)
2016 ret = ftrace_arch_code_modify_prepare();
2017 FTRACE_WARN_ON(ret);
2021 * Do not call function tracer while we update the code.
2022 * We are in stop machine.
2024 function_trace_stop++;
2027 * By default we use stop_machine() to modify the code.
2028 * But archs can do what ever they want as long as it
2029 * is safe. The stop_machine() is the safest, but also
2030 * produces the most overhead.
2032 arch_ftrace_update_code(command);
2034 function_trace_stop--;
2036 ret = ftrace_arch_code_modify_post_process();
2037 FTRACE_WARN_ON(ret);
2040 static ftrace_func_t saved_ftrace_func;
2041 static int ftrace_start_up;
2042 static int global_start_up;
2044 static void ftrace_startup_enable(int command)
2046 if (saved_ftrace_func != ftrace_trace_function) {
2047 saved_ftrace_func = ftrace_trace_function;
2048 command |= FTRACE_UPDATE_TRACE_FUNC;
2051 if (!command || !ftrace_enabled)
2054 ftrace_run_update_code(command);
2057 static int ftrace_startup(struct ftrace_ops *ops, int command)
2059 bool hash_enable = true;
2061 if (unlikely(ftrace_disabled))
2065 command |= FTRACE_UPDATE_CALLS;
2067 /* ops marked global share the filter hashes */
2068 if (ops->flags & FTRACE_OPS_FL_GLOBAL) {
2070 /* Don't update hash if global is already set */
2071 if (global_start_up)
2072 hash_enable = false;
2076 ops->flags |= FTRACE_OPS_FL_ENABLED;
2078 ftrace_hash_rec_enable(ops, 1);
2080 ftrace_startup_enable(command);
2085 static void ftrace_shutdown(struct ftrace_ops *ops, int command)
2087 bool hash_disable = true;
2089 if (unlikely(ftrace_disabled))
2094 * Just warn in case of unbalance, no need to kill ftrace, it's not
2095 * critical but the ftrace_call callers may be never nopped again after
2096 * further ftrace uses.
2098 WARN_ON_ONCE(ftrace_start_up < 0);
2100 if (ops->flags & FTRACE_OPS_FL_GLOBAL) {
2103 WARN_ON_ONCE(global_start_up < 0);
2104 /* Don't update hash if global still has users */
2105 if (global_start_up) {
2106 WARN_ON_ONCE(!ftrace_start_up);
2107 hash_disable = false;
2112 ftrace_hash_rec_disable(ops, 1);
2114 if (ops != &global_ops || !global_start_up)
2115 ops->flags &= ~FTRACE_OPS_FL_ENABLED;
2117 command |= FTRACE_UPDATE_CALLS;
2119 if (saved_ftrace_func != ftrace_trace_function) {
2120 saved_ftrace_func = ftrace_trace_function;
2121 command |= FTRACE_UPDATE_TRACE_FUNC;
2124 if (!command || !ftrace_enabled)
2127 ftrace_run_update_code(command);
2130 static void ftrace_startup_sysctl(void)
2132 if (unlikely(ftrace_disabled))
2135 /* Force update next time */
2136 saved_ftrace_func = NULL;
2137 /* ftrace_start_up is true if we want ftrace running */
2138 if (ftrace_start_up)
2139 ftrace_run_update_code(FTRACE_UPDATE_CALLS);
2142 static void ftrace_shutdown_sysctl(void)
2144 if (unlikely(ftrace_disabled))
2147 /* ftrace_start_up is true if ftrace is running */
2148 if (ftrace_start_up)
2149 ftrace_run_update_code(FTRACE_DISABLE_CALLS);
2152 static cycle_t ftrace_update_time;
2153 static unsigned long ftrace_update_cnt;
2154 unsigned long ftrace_update_tot_cnt;
2156 static int ops_traces_mod(struct ftrace_ops *ops)
2158 struct ftrace_hash *hash;
2160 hash = ops->filter_hash;
2161 return ftrace_hash_empty(hash);
2164 static int ftrace_update_code(struct module *mod)
2166 struct ftrace_page *pg;
2167 struct dyn_ftrace *p;
2168 cycle_t start, stop;
2169 unsigned long ref = 0;
2173 * When adding a module, we need to check if tracers are
2174 * currently enabled and if they are set to trace all functions.
2175 * If they are, we need to enable the module functions as well
2176 * as update the reference counts for those function records.
2179 struct ftrace_ops *ops;
2181 for (ops = ftrace_ops_list;
2182 ops != &ftrace_list_end; ops = ops->next) {
2183 if (ops->flags & FTRACE_OPS_FL_ENABLED &&
2184 ops_traces_mod(ops))
2189 start = ftrace_now(raw_smp_processor_id());
2190 ftrace_update_cnt = 0;
2192 for (pg = ftrace_new_pgs; pg; pg = pg->next) {
2194 for (i = 0; i < pg->index; i++) {
2195 /* If something went wrong, bail without enabling anything */
2196 if (unlikely(ftrace_disabled))
2199 p = &pg->records[i];
2203 * Do the initial record conversion from mcount jump
2204 * to the NOP instructions.
2206 if (!ftrace_code_disable(mod, p))
2209 ftrace_update_cnt++;
2212 * If the tracing is enabled, go ahead and enable the record.
2214 * The reason not to enable the record immediatelly is the
2215 * inherent check of ftrace_make_nop/ftrace_make_call for
2216 * correct previous instructions. Making first the NOP
2217 * conversion puts the module to the correct state, thus
2218 * passing the ftrace_make_call check.
2220 if (ftrace_start_up && ref) {
2221 int failed = __ftrace_replace_code(p, 1);
2223 ftrace_bug(failed, p->ip);
2228 ftrace_new_pgs = NULL;
2230 stop = ftrace_now(raw_smp_processor_id());
2231 ftrace_update_time = stop - start;
2232 ftrace_update_tot_cnt += ftrace_update_cnt;
2237 static int ftrace_allocate_records(struct ftrace_page *pg, int count)
2242 if (WARN_ON(!count))
2245 order = get_count_order(DIV_ROUND_UP(count, ENTRIES_PER_PAGE));
2248 * We want to fill as much as possible. No more than a page
2251 while ((PAGE_SIZE << order) / ENTRY_SIZE >= count + ENTRIES_PER_PAGE)
2255 pg->records = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, order);
2258 /* if we can't allocate this size, try something smaller */
2265 cnt = (PAGE_SIZE << order) / ENTRY_SIZE;
2274 static struct ftrace_page *
2275 ftrace_allocate_pages(unsigned long num_to_init)
2277 struct ftrace_page *start_pg;
2278 struct ftrace_page *pg;
2285 start_pg = pg = kzalloc(sizeof(*pg), GFP_KERNEL);
2290 * Try to allocate as much as possible in one continues
2291 * location that fills in all of the space. We want to
2292 * waste as little space as possible.
2295 cnt = ftrace_allocate_records(pg, num_to_init);
2303 pg->next = kzalloc(sizeof(*pg), GFP_KERNEL);
2314 order = get_count_order(pg->size / ENTRIES_PER_PAGE);
2315 free_pages((unsigned long)pg->records, order);
2316 start_pg = pg->next;
2320 pr_info("ftrace: FAILED to allocate memory for functions\n");
2324 static int __init ftrace_dyn_table_alloc(unsigned long num_to_init)
2329 pr_info("ftrace: No functions to be traced?\n");
2333 cnt = num_to_init / ENTRIES_PER_PAGE;
2334 pr_info("ftrace: allocating %ld entries in %d pages\n",
2335 num_to_init, cnt + 1);
2340 #define FTRACE_BUFF_MAX (KSYM_SYMBOL_LEN+4) /* room for wildcards */
2342 struct ftrace_iterator {
2345 struct ftrace_page *pg;
2346 struct dyn_ftrace *func;
2347 struct ftrace_func_probe *probe;
2348 struct trace_parser parser;
2349 struct ftrace_hash *hash;
2350 struct ftrace_ops *ops;
2357 t_hash_next(struct seq_file *m, loff_t *pos)
2359 struct ftrace_iterator *iter = m->private;
2360 struct hlist_node *hnd = NULL;
2361 struct hlist_head *hhd;
2367 hnd = &iter->probe->node;
2369 if (iter->hidx >= FTRACE_FUNC_HASHSIZE)
2372 hhd = &ftrace_func_hash[iter->hidx];
2374 if (hlist_empty(hhd)) {
2390 if (WARN_ON_ONCE(!hnd))
2393 iter->probe = hlist_entry(hnd, struct ftrace_func_probe, node);
2398 static void *t_hash_start(struct seq_file *m, loff_t *pos)
2400 struct ftrace_iterator *iter = m->private;
2404 if (!(iter->flags & FTRACE_ITER_DO_HASH))
2407 if (iter->func_pos > *pos)
2411 for (l = 0; l <= (*pos - iter->func_pos); ) {
2412 p = t_hash_next(m, &l);
2419 /* Only set this if we have an item */
2420 iter->flags |= FTRACE_ITER_HASH;
2426 t_hash_show(struct seq_file *m, struct ftrace_iterator *iter)
2428 struct ftrace_func_probe *rec;
2431 if (WARN_ON_ONCE(!rec))
2434 if (rec->ops->print)
2435 return rec->ops->print(m, rec->ip, rec->ops, rec->data);
2437 seq_printf(m, "%ps:%ps", (void *)rec->ip, (void *)rec->ops->func);
2440 seq_printf(m, ":%p", rec->data);
2447 t_next(struct seq_file *m, void *v, loff_t *pos)
2449 struct ftrace_iterator *iter = m->private;
2450 struct ftrace_ops *ops = iter->ops;
2451 struct dyn_ftrace *rec = NULL;
2453 if (unlikely(ftrace_disabled))
2456 if (iter->flags & FTRACE_ITER_HASH)
2457 return t_hash_next(m, pos);
2460 iter->pos = iter->func_pos = *pos;
2462 if (iter->flags & FTRACE_ITER_PRINTALL)
2463 return t_hash_start(m, pos);
2466 if (iter->idx >= iter->pg->index) {
2467 if (iter->pg->next) {
2468 iter->pg = iter->pg->next;
2473 rec = &iter->pg->records[iter->idx++];
2474 if (((iter->flags & FTRACE_ITER_FILTER) &&
2475 !(ftrace_lookup_ip(ops->filter_hash, rec->ip))) ||
2477 ((iter->flags & FTRACE_ITER_NOTRACE) &&
2478 !ftrace_lookup_ip(ops->notrace_hash, rec->ip)) ||
2480 ((iter->flags & FTRACE_ITER_ENABLED) &&
2481 !(rec->flags & FTRACE_FL_ENABLED))) {
2489 return t_hash_start(m, pos);
2496 static void reset_iter_read(struct ftrace_iterator *iter)
2500 iter->flags &= ~(FTRACE_ITER_PRINTALL | FTRACE_ITER_HASH);
2503 static void *t_start(struct seq_file *m, loff_t *pos)
2505 struct ftrace_iterator *iter = m->private;
2506 struct ftrace_ops *ops = iter->ops;
2510 mutex_lock(&ftrace_lock);
2512 if (unlikely(ftrace_disabled))
2516 * If an lseek was done, then reset and start from beginning.
2518 if (*pos < iter->pos)
2519 reset_iter_read(iter);
2522 * For set_ftrace_filter reading, if we have the filter
2523 * off, we can short cut and just print out that all
2524 * functions are enabled.
2526 if (iter->flags & FTRACE_ITER_FILTER &&
2527 ftrace_hash_empty(ops->filter_hash)) {
2529 return t_hash_start(m, pos);
2530 iter->flags |= FTRACE_ITER_PRINTALL;
2531 /* reset in case of seek/pread */
2532 iter->flags &= ~FTRACE_ITER_HASH;
2536 if (iter->flags & FTRACE_ITER_HASH)
2537 return t_hash_start(m, pos);
2540 * Unfortunately, we need to restart at ftrace_pages_start
2541 * every time we let go of the ftrace_mutex. This is because
2542 * those pointers can change without the lock.
2544 iter->pg = ftrace_pages_start;
2546 for (l = 0; l <= *pos; ) {
2547 p = t_next(m, p, &l);
2553 return t_hash_start(m, pos);
2558 static void t_stop(struct seq_file *m, void *p)
2560 mutex_unlock(&ftrace_lock);
2563 static int t_show(struct seq_file *m, void *v)
2565 struct ftrace_iterator *iter = m->private;
2566 struct dyn_ftrace *rec;
2568 if (iter->flags & FTRACE_ITER_HASH)
2569 return t_hash_show(m, iter);
2571 if (iter->flags & FTRACE_ITER_PRINTALL) {
2572 seq_printf(m, "#### all functions enabled ####\n");
2581 seq_printf(m, "%ps", (void *)rec->ip);
2582 if (iter->flags & FTRACE_ITER_ENABLED)
2583 seq_printf(m, " (%ld)%s",
2584 rec->flags & ~FTRACE_FL_MASK,
2585 rec->flags & FTRACE_FL_REGS ? " R" : "");
2586 seq_printf(m, "\n");
2591 static const struct seq_operations show_ftrace_seq_ops = {
2599 ftrace_avail_open(struct inode *inode, struct file *file)
2601 struct ftrace_iterator *iter;
2603 if (unlikely(ftrace_disabled))
2606 iter = __seq_open_private(file, &show_ftrace_seq_ops, sizeof(*iter));
2608 iter->pg = ftrace_pages_start;
2609 iter->ops = &global_ops;
2612 return iter ? 0 : -ENOMEM;
2616 ftrace_enabled_open(struct inode *inode, struct file *file)
2618 struct ftrace_iterator *iter;
2620 if (unlikely(ftrace_disabled))
2623 iter = __seq_open_private(file, &show_ftrace_seq_ops, sizeof(*iter));
2625 iter->pg = ftrace_pages_start;
2626 iter->flags = FTRACE_ITER_ENABLED;
2627 iter->ops = &global_ops;
2630 return iter ? 0 : -ENOMEM;
2633 static void ftrace_filter_reset(struct ftrace_hash *hash)
2635 mutex_lock(&ftrace_lock);
2636 ftrace_hash_clear(hash);
2637 mutex_unlock(&ftrace_lock);
2641 * ftrace_regex_open - initialize function tracer filter files
2642 * @ops: The ftrace_ops that hold the hash filters
2643 * @flag: The type of filter to process
2644 * @inode: The inode, usually passed in to your open routine
2645 * @file: The file, usually passed in to your open routine
2647 * ftrace_regex_open() initializes the filter files for the
2648 * @ops. Depending on @flag it may process the filter hash or
2649 * the notrace hash of @ops. With this called from the open
2650 * routine, you can use ftrace_filter_write() for the write
2651 * routine if @flag has FTRACE_ITER_FILTER set, or
2652 * ftrace_notrace_write() if @flag has FTRACE_ITER_NOTRACE set.
2653 * ftrace_filter_lseek() should be used as the lseek routine, and
2654 * release must call ftrace_regex_release().
2657 ftrace_regex_open(struct ftrace_ops *ops, int flag,
2658 struct inode *inode, struct file *file)
2660 struct ftrace_iterator *iter;
2661 struct ftrace_hash *hash;
2664 ftrace_ops_init(ops);
2666 if (unlikely(ftrace_disabled))
2669 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
2673 if (trace_parser_get_init(&iter->parser, FTRACE_BUFF_MAX)) {
2681 mutex_lock(&ops->regex_lock);
2683 if (flag & FTRACE_ITER_NOTRACE)
2684 hash = ops->notrace_hash;
2686 hash = ops->filter_hash;
2688 if (file->f_mode & FMODE_WRITE) {
2689 iter->hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, hash);
2691 trace_parser_put(&iter->parser);
2698 if ((file->f_mode & FMODE_WRITE) &&
2699 (file->f_flags & O_TRUNC))
2700 ftrace_filter_reset(iter->hash);
2702 if (file->f_mode & FMODE_READ) {
2703 iter->pg = ftrace_pages_start;
2705 ret = seq_open(file, &show_ftrace_seq_ops);
2707 struct seq_file *m = file->private_data;
2711 free_ftrace_hash(iter->hash);
2712 trace_parser_put(&iter->parser);
2716 file->private_data = iter;
2719 mutex_unlock(&ops->regex_lock);
2725 ftrace_filter_open(struct inode *inode, struct file *file)
2727 return ftrace_regex_open(&global_ops,
2728 FTRACE_ITER_FILTER | FTRACE_ITER_DO_HASH,
2733 ftrace_notrace_open(struct inode *inode, struct file *file)
2735 return ftrace_regex_open(&global_ops, FTRACE_ITER_NOTRACE,
2739 static int ftrace_match(char *str, char *regex, int len, int type)
2746 if (strcmp(str, regex) == 0)
2749 case MATCH_FRONT_ONLY:
2750 if (strncmp(str, regex, len) == 0)
2753 case MATCH_MIDDLE_ONLY:
2754 if (strstr(str, regex))
2757 case MATCH_END_ONLY:
2759 if (slen >= len && memcmp(str + slen - len, regex, len) == 0)
2768 enter_record(struct ftrace_hash *hash, struct dyn_ftrace *rec, int not)
2770 struct ftrace_func_entry *entry;
2773 entry = ftrace_lookup_ip(hash, rec->ip);
2775 /* Do nothing if it doesn't exist */
2779 free_hash_entry(hash, entry);
2781 /* Do nothing if it exists */
2785 ret = add_hash_entry(hash, rec->ip);
2791 ftrace_match_record(struct dyn_ftrace *rec, char *mod,
2792 char *regex, int len, int type)
2794 char str[KSYM_SYMBOL_LEN];
2797 kallsyms_lookup(rec->ip, NULL, NULL, &modname, str);
2800 /* module lookup requires matching the module */
2801 if (!modname || strcmp(modname, mod))
2804 /* blank search means to match all funcs in the mod */
2809 return ftrace_match(str, regex, len, type);
2813 match_records(struct ftrace_hash *hash, char *buff,
2814 int len, char *mod, int not)
2816 unsigned search_len = 0;
2817 struct ftrace_page *pg;
2818 struct dyn_ftrace *rec;
2819 int type = MATCH_FULL;
2820 char *search = buff;
2825 type = filter_parse_regex(buff, len, &search, ¬);
2826 search_len = strlen(search);
2829 mutex_lock(&ftrace_lock);
2831 if (unlikely(ftrace_disabled))
2834 do_for_each_ftrace_rec(pg, rec) {
2835 if (ftrace_match_record(rec, mod, search, search_len, type)) {
2836 ret = enter_record(hash, rec, not);
2843 } while_for_each_ftrace_rec();
2845 mutex_unlock(&ftrace_lock);
2851 ftrace_match_records(struct ftrace_hash *hash, char *buff, int len)
2853 return match_records(hash, buff, len, NULL, 0);
2857 ftrace_match_module_records(struct ftrace_hash *hash, char *buff, char *mod)
2861 /* blank or '*' mean the same */
2862 if (strcmp(buff, "*") == 0)
2865 /* handle the case of 'dont filter this module' */
2866 if (strcmp(buff, "!") == 0 || strcmp(buff, "!*") == 0) {
2871 return match_records(hash, buff, strlen(buff), mod, not);
2875 * We register the module command as a template to show others how
2876 * to register the a command as well.
2880 ftrace_mod_callback(struct ftrace_hash *hash,
2881 char *func, char *cmd, char *param, int enable)
2887 * cmd == 'mod' because we only registered this func
2888 * for the 'mod' ftrace_func_command.
2889 * But if you register one func with multiple commands,
2890 * you can tell which command was used by the cmd
2894 /* we must have a module name */
2898 mod = strsep(¶m, ":");
2902 ret = ftrace_match_module_records(hash, func, mod);
2911 static struct ftrace_func_command ftrace_mod_cmd = {
2913 .func = ftrace_mod_callback,
2916 static int __init ftrace_mod_cmd_init(void)
2918 return register_ftrace_command(&ftrace_mod_cmd);
2920 core_initcall(ftrace_mod_cmd_init);
2922 static void function_trace_probe_call(unsigned long ip, unsigned long parent_ip,
2923 struct ftrace_ops *op, struct pt_regs *pt_regs)
2925 struct ftrace_func_probe *entry;
2926 struct hlist_head *hhd;
2929 key = hash_long(ip, FTRACE_HASH_BITS);
2931 hhd = &ftrace_func_hash[key];
2933 if (hlist_empty(hhd))
2937 * Disable preemption for these calls to prevent a RCU grace
2938 * period. This syncs the hash iteration and freeing of items
2939 * on the hash. rcu_read_lock is too dangerous here.
2941 preempt_disable_notrace();
2942 hlist_for_each_entry_rcu_notrace(entry, hhd, node) {
2943 if (entry->ip == ip)
2944 entry->ops->func(ip, parent_ip, &entry->data);
2946 preempt_enable_notrace();
2949 static struct ftrace_ops trace_probe_ops __read_mostly =
2951 .func = function_trace_probe_call,
2952 .flags = FTRACE_OPS_FL_INITIALIZED,
2953 INIT_REGEX_LOCK(trace_probe_ops)
2956 static int ftrace_probe_registered;
2958 static void __enable_ftrace_function_probe(void)
2963 if (ftrace_probe_registered) {
2964 /* still need to update the function call sites */
2966 ftrace_run_update_code(FTRACE_UPDATE_CALLS);
2970 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
2971 struct hlist_head *hhd = &ftrace_func_hash[i];
2975 /* Nothing registered? */
2976 if (i == FTRACE_FUNC_HASHSIZE)
2979 ret = __register_ftrace_function(&trace_probe_ops);
2981 ret = ftrace_startup(&trace_probe_ops, 0);
2983 ftrace_probe_registered = 1;
2986 static void __disable_ftrace_function_probe(void)
2991 if (!ftrace_probe_registered)
2994 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
2995 struct hlist_head *hhd = &ftrace_func_hash[i];
3000 /* no more funcs left */
3001 ret = __unregister_ftrace_function(&trace_probe_ops);
3003 ftrace_shutdown(&trace_probe_ops, 0);
3005 ftrace_probe_registered = 0;
3009 static void ftrace_free_entry(struct ftrace_func_probe *entry)
3011 if (entry->ops->free)
3012 entry->ops->free(entry->ops, entry->ip, &entry->data);
3017 register_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
3020 struct ftrace_func_probe *entry;
3021 struct ftrace_hash **orig_hash = &trace_probe_ops.filter_hash;
3022 struct ftrace_hash *hash;
3023 struct ftrace_page *pg;
3024 struct dyn_ftrace *rec;
3031 type = filter_parse_regex(glob, strlen(glob), &search, ¬);
3032 len = strlen(search);
3034 /* we do not support '!' for function probes */
3038 mutex_lock(&trace_probe_ops.regex_lock);
3040 hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, *orig_hash);
3046 if (unlikely(ftrace_disabled)) {
3051 mutex_lock(&ftrace_lock);
3053 do_for_each_ftrace_rec(pg, rec) {
3055 if (!ftrace_match_record(rec, NULL, search, len, type))
3058 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
3060 /* If we did not process any, then return error */
3071 * The caller might want to do something special
3072 * for each function we find. We call the callback
3073 * to give the caller an opportunity to do so.
3076 if (ops->init(ops, rec->ip, &entry->data) < 0) {
3077 /* caller does not like this func */
3083 ret = enter_record(hash, rec, 0);
3091 entry->ip = rec->ip;
3093 key = hash_long(entry->ip, FTRACE_HASH_BITS);
3094 hlist_add_head_rcu(&entry->node, &ftrace_func_hash[key]);
3096 } while_for_each_ftrace_rec();
3098 ret = ftrace_hash_move(&trace_probe_ops, 1, orig_hash, hash);
3102 __enable_ftrace_function_probe();
3105 mutex_unlock(&ftrace_lock);
3107 mutex_unlock(&trace_probe_ops.regex_lock);
3108 free_ftrace_hash(hash);
3114 PROBE_TEST_FUNC = 1,
3119 __unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
3120 void *data, int flags)
3122 struct ftrace_func_entry *rec_entry;
3123 struct ftrace_func_probe *entry;
3124 struct ftrace_func_probe *p;
3125 struct ftrace_hash **orig_hash = &trace_probe_ops.filter_hash;
3126 struct list_head free_list;
3127 struct ftrace_hash *hash;
3128 struct hlist_node *tmp;
3129 char str[KSYM_SYMBOL_LEN];
3130 int type = MATCH_FULL;
3134 if (glob && (strcmp(glob, "*") == 0 || !strlen(glob)))
3139 type = filter_parse_regex(glob, strlen(glob), &search, ¬);
3140 len = strlen(search);
3142 /* we do not support '!' for function probes */
3147 mutex_lock(&trace_probe_ops.regex_lock);
3149 hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, *orig_hash);
3151 /* Hmm, should report this somehow */
3154 INIT_LIST_HEAD(&free_list);
3156 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
3157 struct hlist_head *hhd = &ftrace_func_hash[i];
3159 hlist_for_each_entry_safe(entry, tmp, hhd, node) {
3161 /* break up if statements for readability */
3162 if ((flags & PROBE_TEST_FUNC) && entry->ops != ops)
3165 if ((flags & PROBE_TEST_DATA) && entry->data != data)
3168 /* do this last, since it is the most expensive */
3170 kallsyms_lookup(entry->ip, NULL, NULL,
3172 if (!ftrace_match(str, glob, len, type))
3176 rec_entry = ftrace_lookup_ip(hash, entry->ip);
3177 /* It is possible more than one entry had this ip */
3179 free_hash_entry(hash, rec_entry);
3181 hlist_del_rcu(&entry->node);
3182 list_add(&entry->free_list, &free_list);
3185 mutex_lock(&ftrace_lock);
3186 __disable_ftrace_function_probe();
3188 * Remove after the disable is called. Otherwise, if the last
3189 * probe is removed, a null hash means *all enabled*.
3191 ftrace_hash_move(&trace_probe_ops, 1, orig_hash, hash);
3192 synchronize_sched();
3193 list_for_each_entry_safe(entry, p, &free_list, free_list) {
3194 list_del(&entry->free_list);
3195 ftrace_free_entry(entry);
3197 mutex_unlock(&ftrace_lock);
3200 mutex_unlock(&trace_probe_ops.regex_lock);
3201 free_ftrace_hash(hash);
3205 unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
3208 __unregister_ftrace_function_probe(glob, ops, data,
3209 PROBE_TEST_FUNC | PROBE_TEST_DATA);
3213 unregister_ftrace_function_probe_func(char *glob, struct ftrace_probe_ops *ops)
3215 __unregister_ftrace_function_probe(glob, ops, NULL, PROBE_TEST_FUNC);
3218 void unregister_ftrace_function_probe_all(char *glob)
3220 __unregister_ftrace_function_probe(glob, NULL, NULL, 0);
3223 static LIST_HEAD(ftrace_commands);
3224 static DEFINE_MUTEX(ftrace_cmd_mutex);
3226 int register_ftrace_command(struct ftrace_func_command *cmd)
3228 struct ftrace_func_command *p;
3231 mutex_lock(&ftrace_cmd_mutex);
3232 list_for_each_entry(p, &ftrace_commands, list) {
3233 if (strcmp(cmd->name, p->name) == 0) {
3238 list_add(&cmd->list, &ftrace_commands);
3240 mutex_unlock(&ftrace_cmd_mutex);
3245 int unregister_ftrace_command(struct ftrace_func_command *cmd)
3247 struct ftrace_func_command *p, *n;
3250 mutex_lock(&ftrace_cmd_mutex);
3251 list_for_each_entry_safe(p, n, &ftrace_commands, list) {
3252 if (strcmp(cmd->name, p->name) == 0) {
3254 list_del_init(&p->list);
3259 mutex_unlock(&ftrace_cmd_mutex);
3264 static int ftrace_process_regex(struct ftrace_hash *hash,
3265 char *buff, int len, int enable)
3267 char *func, *command, *next = buff;
3268 struct ftrace_func_command *p;
3271 func = strsep(&next, ":");
3274 ret = ftrace_match_records(hash, func, len);
3284 command = strsep(&next, ":");
3286 mutex_lock(&ftrace_cmd_mutex);
3287 list_for_each_entry(p, &ftrace_commands, list) {
3288 if (strcmp(p->name, command) == 0) {
3289 ret = p->func(hash, func, command, next, enable);
3294 mutex_unlock(&ftrace_cmd_mutex);
3300 ftrace_regex_write(struct file *file, const char __user *ubuf,
3301 size_t cnt, loff_t *ppos, int enable)
3303 struct ftrace_iterator *iter;
3304 struct trace_parser *parser;
3310 if (file->f_mode & FMODE_READ) {
3311 struct seq_file *m = file->private_data;
3314 iter = file->private_data;
3316 if (unlikely(ftrace_disabled))
3319 /* iter->hash is a local copy, so we don't need regex_lock */
3321 parser = &iter->parser;
3322 read = trace_get_user(parser, ubuf, cnt, ppos);
3324 if (read >= 0 && trace_parser_loaded(parser) &&
3325 !trace_parser_cont(parser)) {
3326 ret = ftrace_process_regex(iter->hash, parser->buffer,
3327 parser->idx, enable);
3328 trace_parser_clear(parser);
3339 ftrace_filter_write(struct file *file, const char __user *ubuf,
3340 size_t cnt, loff_t *ppos)
3342 return ftrace_regex_write(file, ubuf, cnt, ppos, 1);
3346 ftrace_notrace_write(struct file *file, const char __user *ubuf,
3347 size_t cnt, loff_t *ppos)
3349 return ftrace_regex_write(file, ubuf, cnt, ppos, 0);
3353 ftrace_match_addr(struct ftrace_hash *hash, unsigned long ip, int remove)
3355 struct ftrace_func_entry *entry;
3357 if (!ftrace_location(ip))
3361 entry = ftrace_lookup_ip(hash, ip);
3364 free_hash_entry(hash, entry);
3368 return add_hash_entry(hash, ip);
3372 ftrace_set_hash(struct ftrace_ops *ops, unsigned char *buf, int len,
3373 unsigned long ip, int remove, int reset, int enable)
3375 struct ftrace_hash **orig_hash;
3376 struct ftrace_hash *hash;
3379 /* All global ops uses the global ops filters */
3380 if (ops->flags & FTRACE_OPS_FL_GLOBAL)
3383 if (unlikely(ftrace_disabled))
3386 mutex_lock(&ops->regex_lock);
3389 orig_hash = &ops->filter_hash;
3391 orig_hash = &ops->notrace_hash;
3393 hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, *orig_hash);
3396 goto out_regex_unlock;
3400 ftrace_filter_reset(hash);
3401 if (buf && !ftrace_match_records(hash, buf, len)) {
3403 goto out_regex_unlock;
3406 ret = ftrace_match_addr(hash, ip, remove);
3408 goto out_regex_unlock;
3411 mutex_lock(&ftrace_lock);
3412 ret = ftrace_hash_move(ops, enable, orig_hash, hash);
3413 if (!ret && ops->flags & FTRACE_OPS_FL_ENABLED
3415 ftrace_run_update_code(FTRACE_UPDATE_CALLS);
3417 mutex_unlock(&ftrace_lock);
3420 mutex_unlock(&ops->regex_lock);
3422 free_ftrace_hash(hash);
3427 ftrace_set_addr(struct ftrace_ops *ops, unsigned long ip, int remove,
3428 int reset, int enable)
3430 return ftrace_set_hash(ops, 0, 0, ip, remove, reset, enable);
3434 * ftrace_set_filter_ip - set a function to filter on in ftrace by address
3435 * @ops - the ops to set the filter with
3436 * @ip - the address to add to or remove from the filter.
3437 * @remove - non zero to remove the ip from the filter
3438 * @reset - non zero to reset all filters before applying this filter.
3440 * Filters denote which functions should be enabled when tracing is enabled
3441 * If @ip is NULL, it failes to update filter.
3443 int ftrace_set_filter_ip(struct ftrace_ops *ops, unsigned long ip,
3444 int remove, int reset)
3446 ftrace_ops_init(ops);
3447 return ftrace_set_addr(ops, ip, remove, reset, 1);
3449 EXPORT_SYMBOL_GPL(ftrace_set_filter_ip);
3452 ftrace_set_regex(struct ftrace_ops *ops, unsigned char *buf, int len,
3453 int reset, int enable)
3455 return ftrace_set_hash(ops, buf, len, 0, 0, reset, enable);
3459 * ftrace_set_filter - set a function to filter on in ftrace
3460 * @ops - the ops to set the filter with
3461 * @buf - the string that holds the function filter text.
3462 * @len - the length of the string.
3463 * @reset - non zero to reset all filters before applying this filter.
3465 * Filters denote which functions should be enabled when tracing is enabled.
3466 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
3468 int ftrace_set_filter(struct ftrace_ops *ops, unsigned char *buf,
3471 ftrace_ops_init(ops);
3472 return ftrace_set_regex(ops, buf, len, reset, 1);
3474 EXPORT_SYMBOL_GPL(ftrace_set_filter);
3477 * ftrace_set_notrace - set a function to not trace in ftrace
3478 * @ops - the ops to set the notrace filter with
3479 * @buf - the string that holds the function notrace text.
3480 * @len - the length of the string.
3481 * @reset - non zero to reset all filters before applying this filter.
3483 * Notrace Filters denote which functions should not be enabled when tracing
3484 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
3487 int ftrace_set_notrace(struct ftrace_ops *ops, unsigned char *buf,
3490 ftrace_ops_init(ops);
3491 return ftrace_set_regex(ops, buf, len, reset, 0);
3493 EXPORT_SYMBOL_GPL(ftrace_set_notrace);
3495 * ftrace_set_filter - set a function to filter on in ftrace
3496 * @ops - the ops to set the filter with
3497 * @buf - the string that holds the function filter text.
3498 * @len - the length of the string.
3499 * @reset - non zero to reset all filters before applying this filter.
3501 * Filters denote which functions should be enabled when tracing is enabled.
3502 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
3504 void ftrace_set_global_filter(unsigned char *buf, int len, int reset)
3506 ftrace_set_regex(&global_ops, buf, len, reset, 1);
3508 EXPORT_SYMBOL_GPL(ftrace_set_global_filter);
3511 * ftrace_set_notrace - set a function to not trace in ftrace
3512 * @ops - the ops to set the notrace filter with
3513 * @buf - the string that holds the function notrace text.
3514 * @len - the length of the string.
3515 * @reset - non zero to reset all filters before applying this filter.
3517 * Notrace Filters denote which functions should not be enabled when tracing
3518 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
3521 void ftrace_set_global_notrace(unsigned char *buf, int len, int reset)
3523 ftrace_set_regex(&global_ops, buf, len, reset, 0);
3525 EXPORT_SYMBOL_GPL(ftrace_set_global_notrace);
3528 * command line interface to allow users to set filters on boot up.
3530 #define FTRACE_FILTER_SIZE COMMAND_LINE_SIZE
3531 static char ftrace_notrace_buf[FTRACE_FILTER_SIZE] __initdata;
3532 static char ftrace_filter_buf[FTRACE_FILTER_SIZE] __initdata;
3534 static int __init set_ftrace_notrace(char *str)
3536 strlcpy(ftrace_notrace_buf, str, FTRACE_FILTER_SIZE);
3539 __setup("ftrace_notrace=", set_ftrace_notrace);
3541 static int __init set_ftrace_filter(char *str)
3543 strlcpy(ftrace_filter_buf, str, FTRACE_FILTER_SIZE);
3546 __setup("ftrace_filter=", set_ftrace_filter);
3548 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
3549 static char ftrace_graph_buf[FTRACE_FILTER_SIZE] __initdata;
3550 static int ftrace_set_func(unsigned long *array, int *idx, char *buffer);
3552 static int __init set_graph_function(char *str)
3554 strlcpy(ftrace_graph_buf, str, FTRACE_FILTER_SIZE);
3557 __setup("ftrace_graph_filter=", set_graph_function);
3559 static void __init set_ftrace_early_graph(char *buf)
3565 func = strsep(&buf, ",");
3566 /* we allow only one expression at a time */
3567 ret = ftrace_set_func(ftrace_graph_funcs, &ftrace_graph_count,
3570 printk(KERN_DEBUG "ftrace: function %s not "
3571 "traceable\n", func);
3574 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
3577 ftrace_set_early_filter(struct ftrace_ops *ops, char *buf, int enable)
3581 ftrace_ops_init(ops);
3584 func = strsep(&buf, ",");
3585 ftrace_set_regex(ops, func, strlen(func), 0, enable);
3589 static void __init set_ftrace_early_filters(void)
3591 if (ftrace_filter_buf[0])
3592 ftrace_set_early_filter(&global_ops, ftrace_filter_buf, 1);
3593 if (ftrace_notrace_buf[0])
3594 ftrace_set_early_filter(&global_ops, ftrace_notrace_buf, 0);
3595 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
3596 if (ftrace_graph_buf[0])
3597 set_ftrace_early_graph(ftrace_graph_buf);
3598 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
3601 int ftrace_regex_release(struct inode *inode, struct file *file)
3603 struct seq_file *m = (struct seq_file *)file->private_data;
3604 struct ftrace_iterator *iter;
3605 struct ftrace_hash **orig_hash;
3606 struct trace_parser *parser;
3610 if (file->f_mode & FMODE_READ) {
3612 seq_release(inode, file);
3614 iter = file->private_data;
3616 parser = &iter->parser;
3617 if (trace_parser_loaded(parser)) {
3618 parser->buffer[parser->idx] = 0;
3619 ftrace_match_records(iter->hash, parser->buffer, parser->idx);
3622 trace_parser_put(parser);
3624 mutex_lock(&iter->ops->regex_lock);
3626 if (file->f_mode & FMODE_WRITE) {
3627 filter_hash = !!(iter->flags & FTRACE_ITER_FILTER);
3630 orig_hash = &iter->ops->filter_hash;
3632 orig_hash = &iter->ops->notrace_hash;
3634 mutex_lock(&ftrace_lock);
3635 ret = ftrace_hash_move(iter->ops, filter_hash,
3636 orig_hash, iter->hash);
3637 if (!ret && (iter->ops->flags & FTRACE_OPS_FL_ENABLED)
3639 ftrace_run_update_code(FTRACE_UPDATE_CALLS);
3641 mutex_unlock(&ftrace_lock);
3644 mutex_unlock(&iter->ops->regex_lock);
3645 free_ftrace_hash(iter->hash);
3651 static const struct file_operations ftrace_avail_fops = {
3652 .open = ftrace_avail_open,
3654 .llseek = seq_lseek,
3655 .release = seq_release_private,
3658 static const struct file_operations ftrace_enabled_fops = {
3659 .open = ftrace_enabled_open,
3661 .llseek = seq_lseek,
3662 .release = seq_release_private,
3665 static const struct file_operations ftrace_filter_fops = {
3666 .open = ftrace_filter_open,
3668 .write = ftrace_filter_write,
3669 .llseek = ftrace_filter_lseek,
3670 .release = ftrace_regex_release,
3673 static const struct file_operations ftrace_notrace_fops = {
3674 .open = ftrace_notrace_open,
3676 .write = ftrace_notrace_write,
3677 .llseek = ftrace_filter_lseek,
3678 .release = ftrace_regex_release,
3681 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
3683 static DEFINE_MUTEX(graph_lock);
3685 int ftrace_graph_count;
3686 int ftrace_graph_filter_enabled;
3687 unsigned long ftrace_graph_funcs[FTRACE_GRAPH_MAX_FUNCS] __read_mostly;
3690 __g_next(struct seq_file *m, loff_t *pos)
3692 if (*pos >= ftrace_graph_count)
3694 return &ftrace_graph_funcs[*pos];
3698 g_next(struct seq_file *m, void *v, loff_t *pos)
3701 return __g_next(m, pos);
3704 static void *g_start(struct seq_file *m, loff_t *pos)
3706 mutex_lock(&graph_lock);
3708 /* Nothing, tell g_show to print all functions are enabled */
3709 if (!ftrace_graph_filter_enabled && !*pos)
3712 return __g_next(m, pos);
3715 static void g_stop(struct seq_file *m, void *p)
3717 mutex_unlock(&graph_lock);
3720 static int g_show(struct seq_file *m, void *v)
3722 unsigned long *ptr = v;
3727 if (ptr == (unsigned long *)1) {
3728 seq_printf(m, "#### all functions enabled ####\n");
3732 seq_printf(m, "%ps\n", (void *)*ptr);
3737 static const struct seq_operations ftrace_graph_seq_ops = {
3745 ftrace_graph_open(struct inode *inode, struct file *file)
3749 if (unlikely(ftrace_disabled))
3752 mutex_lock(&graph_lock);
3753 if ((file->f_mode & FMODE_WRITE) &&
3754 (file->f_flags & O_TRUNC)) {
3755 ftrace_graph_filter_enabled = 0;
3756 ftrace_graph_count = 0;
3757 memset(ftrace_graph_funcs, 0, sizeof(ftrace_graph_funcs));
3759 mutex_unlock(&graph_lock);
3761 if (file->f_mode & FMODE_READ)
3762 ret = seq_open(file, &ftrace_graph_seq_ops);
3768 ftrace_graph_release(struct inode *inode, struct file *file)
3770 if (file->f_mode & FMODE_READ)
3771 seq_release(inode, file);
3776 ftrace_set_func(unsigned long *array, int *idx, char *buffer)
3778 struct dyn_ftrace *rec;
3779 struct ftrace_page *pg;
3788 type = filter_parse_regex(buffer, strlen(buffer), &search, ¬);
3789 if (!not && *idx >= FTRACE_GRAPH_MAX_FUNCS)
3792 search_len = strlen(search);
3794 mutex_lock(&ftrace_lock);
3796 if (unlikely(ftrace_disabled)) {
3797 mutex_unlock(&ftrace_lock);
3801 do_for_each_ftrace_rec(pg, rec) {
3803 if (ftrace_match_record(rec, NULL, search, search_len, type)) {
3804 /* if it is in the array */
3806 for (i = 0; i < *idx; i++) {
3807 if (array[i] == rec->ip) {
3816 array[(*idx)++] = rec->ip;
3817 if (*idx >= FTRACE_GRAPH_MAX_FUNCS)
3822 array[i] = array[--(*idx)];
3828 } while_for_each_ftrace_rec();
3830 mutex_unlock(&ftrace_lock);
3835 ftrace_graph_filter_enabled = !!(*idx);
3841 ftrace_graph_write(struct file *file, const char __user *ubuf,
3842 size_t cnt, loff_t *ppos)
3844 struct trace_parser parser;
3850 mutex_lock(&graph_lock);
3852 if (trace_parser_get_init(&parser, FTRACE_BUFF_MAX)) {
3857 read = trace_get_user(&parser, ubuf, cnt, ppos);
3859 if (read >= 0 && trace_parser_loaded((&parser))) {
3860 parser.buffer[parser.idx] = 0;
3862 /* we allow only one expression at a time */
3863 ret = ftrace_set_func(ftrace_graph_funcs, &ftrace_graph_count,
3872 trace_parser_put(&parser);
3874 mutex_unlock(&graph_lock);
3879 static const struct file_operations ftrace_graph_fops = {
3880 .open = ftrace_graph_open,
3882 .write = ftrace_graph_write,
3883 .llseek = ftrace_filter_lseek,
3884 .release = ftrace_graph_release,
3886 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
3888 static __init int ftrace_init_dyn_debugfs(struct dentry *d_tracer)
3891 trace_create_file("available_filter_functions", 0444,
3892 d_tracer, NULL, &ftrace_avail_fops);
3894 trace_create_file("enabled_functions", 0444,
3895 d_tracer, NULL, &ftrace_enabled_fops);
3897 trace_create_file("set_ftrace_filter", 0644, d_tracer,
3898 NULL, &ftrace_filter_fops);
3900 trace_create_file("set_ftrace_notrace", 0644, d_tracer,
3901 NULL, &ftrace_notrace_fops);
3903 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
3904 trace_create_file("set_graph_function", 0444, d_tracer,
3906 &ftrace_graph_fops);
3907 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
3912 static int ftrace_cmp_ips(const void *a, const void *b)
3914 const unsigned long *ipa = a;
3915 const unsigned long *ipb = b;
3924 static void ftrace_swap_ips(void *a, void *b, int size)
3926 unsigned long *ipa = a;
3927 unsigned long *ipb = b;
3935 static int ftrace_process_locs(struct module *mod,
3936 unsigned long *start,
3939 struct ftrace_page *start_pg;
3940 struct ftrace_page *pg;
3941 struct dyn_ftrace *rec;
3942 unsigned long count;
3945 unsigned long flags = 0; /* Shut up gcc */
3948 count = end - start;
3953 sort(start, count, sizeof(*start),
3954 ftrace_cmp_ips, ftrace_swap_ips);
3956 start_pg = ftrace_allocate_pages(count);
3960 mutex_lock(&ftrace_lock);
3963 * Core and each module needs their own pages, as
3964 * modules will free them when they are removed.
3965 * Force a new page to be allocated for modules.
3968 WARN_ON(ftrace_pages || ftrace_pages_start);
3969 /* First initialization */
3970 ftrace_pages = ftrace_pages_start = start_pg;
3975 if (WARN_ON(ftrace_pages->next)) {
3976 /* Hmm, we have free pages? */
3977 while (ftrace_pages->next)
3978 ftrace_pages = ftrace_pages->next;
3981 ftrace_pages->next = start_pg;
3987 addr = ftrace_call_adjust(*p++);
3989 * Some architecture linkers will pad between
3990 * the different mcount_loc sections of different
3991 * object files to satisfy alignments.
3992 * Skip any NULL pointers.
3997 if (pg->index == pg->size) {
3998 /* We should have allocated enough */
3999 if (WARN_ON(!pg->next))
4004 rec = &pg->records[pg->index++];
4008 /* We should have used all pages */
4011 /* Assign the last page to ftrace_pages */
4014 /* These new locations need to be initialized */
4015 ftrace_new_pgs = start_pg;
4018 * We only need to disable interrupts on start up
4019 * because we are modifying code that an interrupt
4020 * may execute, and the modification is not atomic.
4021 * But for modules, nothing runs the code we modify
4022 * until we are finished with it, and there's no
4023 * reason to cause large interrupt latencies while we do it.
4026 local_irq_save(flags);
4027 ftrace_update_code(mod);
4029 local_irq_restore(flags);
4032 mutex_unlock(&ftrace_lock);
4037 #ifdef CONFIG_MODULES
4039 #define next_to_ftrace_page(p) container_of(p, struct ftrace_page, next)
4041 void ftrace_release_mod(struct module *mod)
4043 struct dyn_ftrace *rec;
4044 struct ftrace_page **last_pg;
4045 struct ftrace_page *pg;
4048 mutex_lock(&ftrace_lock);
4050 if (ftrace_disabled)
4054 * Each module has its own ftrace_pages, remove
4055 * them from the list.
4057 last_pg = &ftrace_pages_start;
4058 for (pg = ftrace_pages_start; pg; pg = *last_pg) {
4059 rec = &pg->records[0];
4060 if (within_module_core(rec->ip, mod)) {
4062 * As core pages are first, the first
4063 * page should never be a module page.
4065 if (WARN_ON(pg == ftrace_pages_start))
4068 /* Check if we are deleting the last page */
4069 if (pg == ftrace_pages)
4070 ftrace_pages = next_to_ftrace_page(last_pg);
4072 *last_pg = pg->next;
4073 order = get_count_order(pg->size / ENTRIES_PER_PAGE);
4074 free_pages((unsigned long)pg->records, order);
4077 last_pg = &pg->next;
4080 mutex_unlock(&ftrace_lock);
4083 static void ftrace_init_module(struct module *mod,
4084 unsigned long *start, unsigned long *end)
4086 if (ftrace_disabled || start == end)
4088 ftrace_process_locs(mod, start, end);
4091 static int ftrace_module_notify_enter(struct notifier_block *self,
4092 unsigned long val, void *data)
4094 struct module *mod = data;
4096 if (val == MODULE_STATE_COMING)
4097 ftrace_init_module(mod, mod->ftrace_callsites,
4098 mod->ftrace_callsites +
4099 mod->num_ftrace_callsites);
4103 static int ftrace_module_notify_exit(struct notifier_block *self,
4104 unsigned long val, void *data)
4106 struct module *mod = data;
4108 if (val == MODULE_STATE_GOING)
4109 ftrace_release_mod(mod);
4114 static int ftrace_module_notify_enter(struct notifier_block *self,
4115 unsigned long val, void *data)
4119 static int ftrace_module_notify_exit(struct notifier_block *self,
4120 unsigned long val, void *data)
4124 #endif /* CONFIG_MODULES */
4126 struct notifier_block ftrace_module_enter_nb = {
4127 .notifier_call = ftrace_module_notify_enter,
4128 .priority = INT_MAX, /* Run before anything that can use kprobes */
4131 struct notifier_block ftrace_module_exit_nb = {
4132 .notifier_call = ftrace_module_notify_exit,
4133 .priority = INT_MIN, /* Run after anything that can remove kprobes */
4136 extern unsigned long __start_mcount_loc[];
4137 extern unsigned long __stop_mcount_loc[];
4139 void __init ftrace_init(void)
4141 unsigned long count, addr, flags;
4144 /* Keep the ftrace pointer to the stub */
4145 addr = (unsigned long)ftrace_stub;
4147 local_irq_save(flags);
4148 ftrace_dyn_arch_init(&addr);
4149 local_irq_restore(flags);
4151 /* ftrace_dyn_arch_init places the return code in addr */
4155 count = __stop_mcount_loc - __start_mcount_loc;
4157 ret = ftrace_dyn_table_alloc(count);
4161 last_ftrace_enabled = ftrace_enabled = 1;
4163 ret = ftrace_process_locs(NULL,
4167 ret = register_module_notifier(&ftrace_module_enter_nb);
4169 pr_warning("Failed to register trace ftrace module enter notifier\n");
4171 ret = register_module_notifier(&ftrace_module_exit_nb);
4173 pr_warning("Failed to register trace ftrace module exit notifier\n");
4175 set_ftrace_early_filters();
4179 ftrace_disabled = 1;
4184 static struct ftrace_ops global_ops = {
4185 .func = ftrace_stub,
4186 .flags = FTRACE_OPS_FL_RECURSION_SAFE | FTRACE_OPS_FL_INITIALIZED,
4187 INIT_REGEX_LOCK(global_ops)
4190 static int __init ftrace_nodyn_init(void)
4195 core_initcall(ftrace_nodyn_init);
4197 static inline int ftrace_init_dyn_debugfs(struct dentry *d_tracer) { return 0; }
4198 static inline void ftrace_startup_enable(int command) { }
4199 /* Keep as macros so we do not need to define the commands */
4200 # define ftrace_startup(ops, command) \
4202 (ops)->flags |= FTRACE_OPS_FL_ENABLED; \
4205 # define ftrace_shutdown(ops, command) do { } while (0)
4206 # define ftrace_startup_sysctl() do { } while (0)
4207 # define ftrace_shutdown_sysctl() do { } while (0)
4210 ftrace_ops_test(struct ftrace_ops *ops, unsigned long ip)
4215 #endif /* CONFIG_DYNAMIC_FTRACE */
4218 ftrace_ops_control_func(unsigned long ip, unsigned long parent_ip,
4219 struct ftrace_ops *op, struct pt_regs *regs)
4221 if (unlikely(trace_recursion_test(TRACE_CONTROL_BIT)))
4225 * Some of the ops may be dynamically allocated,
4226 * they must be freed after a synchronize_sched().
4228 preempt_disable_notrace();
4229 trace_recursion_set(TRACE_CONTROL_BIT);
4230 do_for_each_ftrace_op(op, ftrace_control_list) {
4231 if (!(op->flags & FTRACE_OPS_FL_STUB) &&
4232 !ftrace_function_local_disabled(op) &&
4233 ftrace_ops_test(op, ip))
4234 op->func(ip, parent_ip, op, regs);
4235 } while_for_each_ftrace_op(op);
4236 trace_recursion_clear(TRACE_CONTROL_BIT);
4237 preempt_enable_notrace();
4240 static struct ftrace_ops control_ops = {
4241 .func = ftrace_ops_control_func,
4242 .flags = FTRACE_OPS_FL_RECURSION_SAFE | FTRACE_OPS_FL_INITIALIZED,
4243 INIT_REGEX_LOCK(control_ops)
4247 __ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip,
4248 struct ftrace_ops *ignored, struct pt_regs *regs)
4250 struct ftrace_ops *op;
4253 if (function_trace_stop)
4256 bit = trace_test_and_set_recursion(TRACE_LIST_START, TRACE_LIST_MAX);
4261 * Some of the ops may be dynamically allocated,
4262 * they must be freed after a synchronize_sched().
4264 preempt_disable_notrace();
4265 do_for_each_ftrace_op(op, ftrace_ops_list) {
4266 if (ftrace_ops_test(op, ip))
4267 op->func(ip, parent_ip, op, regs);
4268 } while_for_each_ftrace_op(op);
4269 preempt_enable_notrace();
4270 trace_clear_recursion(bit);
4274 * Some archs only support passing ip and parent_ip. Even though
4275 * the list function ignores the op parameter, we do not want any
4276 * C side effects, where a function is called without the caller
4277 * sending a third parameter.
4278 * Archs are to support both the regs and ftrace_ops at the same time.
4279 * If they support ftrace_ops, it is assumed they support regs.
4280 * If call backs want to use regs, they must either check for regs
4281 * being NULL, or CONFIG_DYNAMIC_FTRACE_WITH_REGS.
4282 * Note, CONFIG_DYNAMIC_FTRACE_WITH_REGS expects a full regs to be saved.
4283 * An architecture can pass partial regs with ftrace_ops and still
4284 * set the ARCH_SUPPORT_FTARCE_OPS.
4286 #if ARCH_SUPPORTS_FTRACE_OPS
4287 static void ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip,
4288 struct ftrace_ops *op, struct pt_regs *regs)
4290 __ftrace_ops_list_func(ip, parent_ip, NULL, regs);
4293 static void ftrace_ops_no_ops(unsigned long ip, unsigned long parent_ip)
4295 __ftrace_ops_list_func(ip, parent_ip, NULL, NULL);
4299 static void clear_ftrace_swapper(void)
4301 struct task_struct *p;
4305 for_each_online_cpu(cpu) {
4307 clear_tsk_trace_trace(p);
4312 static void set_ftrace_swapper(void)
4314 struct task_struct *p;
4318 for_each_online_cpu(cpu) {
4320 set_tsk_trace_trace(p);
4325 static void clear_ftrace_pid(struct pid *pid)
4327 struct task_struct *p;
4330 do_each_pid_task(pid, PIDTYPE_PID, p) {
4331 clear_tsk_trace_trace(p);
4332 } while_each_pid_task(pid, PIDTYPE_PID, p);
4338 static void set_ftrace_pid(struct pid *pid)
4340 struct task_struct *p;
4343 do_each_pid_task(pid, PIDTYPE_PID, p) {
4344 set_tsk_trace_trace(p);
4345 } while_each_pid_task(pid, PIDTYPE_PID, p);
4349 static void clear_ftrace_pid_task(struct pid *pid)
4351 if (pid == ftrace_swapper_pid)
4352 clear_ftrace_swapper();
4354 clear_ftrace_pid(pid);
4357 static void set_ftrace_pid_task(struct pid *pid)
4359 if (pid == ftrace_swapper_pid)
4360 set_ftrace_swapper();
4362 set_ftrace_pid(pid);
4365 static int ftrace_pid_add(int p)
4368 struct ftrace_pid *fpid;
4371 mutex_lock(&ftrace_lock);
4374 pid = ftrace_swapper_pid;
4376 pid = find_get_pid(p);
4383 list_for_each_entry(fpid, &ftrace_pids, list)
4384 if (fpid->pid == pid)
4389 fpid = kmalloc(sizeof(*fpid), GFP_KERNEL);
4393 list_add(&fpid->list, &ftrace_pids);
4396 set_ftrace_pid_task(pid);
4398 ftrace_update_pid_func();
4399 ftrace_startup_enable(0);
4401 mutex_unlock(&ftrace_lock);
4405 if (pid != ftrace_swapper_pid)
4409 mutex_unlock(&ftrace_lock);
4413 static void ftrace_pid_reset(void)
4415 struct ftrace_pid *fpid, *safe;
4417 mutex_lock(&ftrace_lock);
4418 list_for_each_entry_safe(fpid, safe, &ftrace_pids, list) {
4419 struct pid *pid = fpid->pid;
4421 clear_ftrace_pid_task(pid);
4423 list_del(&fpid->list);
4427 ftrace_update_pid_func();
4428 ftrace_startup_enable(0);
4430 mutex_unlock(&ftrace_lock);
4433 static void *fpid_start(struct seq_file *m, loff_t *pos)
4435 mutex_lock(&ftrace_lock);
4437 if (list_empty(&ftrace_pids) && (!*pos))
4440 return seq_list_start(&ftrace_pids, *pos);
4443 static void *fpid_next(struct seq_file *m, void *v, loff_t *pos)
4448 return seq_list_next(v, &ftrace_pids, pos);
4451 static void fpid_stop(struct seq_file *m, void *p)
4453 mutex_unlock(&ftrace_lock);
4456 static int fpid_show(struct seq_file *m, void *v)
4458 const struct ftrace_pid *fpid = list_entry(v, struct ftrace_pid, list);
4460 if (v == (void *)1) {
4461 seq_printf(m, "no pid\n");
4465 if (fpid->pid == ftrace_swapper_pid)
4466 seq_printf(m, "swapper tasks\n");
4468 seq_printf(m, "%u\n", pid_vnr(fpid->pid));
4473 static const struct seq_operations ftrace_pid_sops = {
4474 .start = fpid_start,
4481 ftrace_pid_open(struct inode *inode, struct file *file)
4485 if ((file->f_mode & FMODE_WRITE) &&
4486 (file->f_flags & O_TRUNC))
4489 if (file->f_mode & FMODE_READ)
4490 ret = seq_open(file, &ftrace_pid_sops);
4496 ftrace_pid_write(struct file *filp, const char __user *ubuf,
4497 size_t cnt, loff_t *ppos)
4503 if (cnt >= sizeof(buf))
4506 if (copy_from_user(&buf, ubuf, cnt))
4512 * Allow "echo > set_ftrace_pid" or "echo -n '' > set_ftrace_pid"
4513 * to clean the filter quietly.
4515 tmp = strstrip(buf);
4516 if (strlen(tmp) == 0)
4519 ret = kstrtol(tmp, 10, &val);
4523 ret = ftrace_pid_add(val);
4525 return ret ? ret : cnt;
4529 ftrace_pid_release(struct inode *inode, struct file *file)
4531 if (file->f_mode & FMODE_READ)
4532 seq_release(inode, file);
4537 static const struct file_operations ftrace_pid_fops = {
4538 .open = ftrace_pid_open,
4539 .write = ftrace_pid_write,
4541 .llseek = ftrace_filter_lseek,
4542 .release = ftrace_pid_release,
4545 static __init int ftrace_init_debugfs(void)
4547 struct dentry *d_tracer;
4549 d_tracer = tracing_init_dentry();
4553 ftrace_init_dyn_debugfs(d_tracer);
4555 trace_create_file("set_ftrace_pid", 0644, d_tracer,
4556 NULL, &ftrace_pid_fops);
4558 ftrace_profile_debugfs(d_tracer);
4562 fs_initcall(ftrace_init_debugfs);
4565 * ftrace_kill - kill ftrace
4567 * This function should be used by panic code. It stops ftrace
4568 * but in a not so nice way. If you need to simply kill ftrace
4569 * from a non-atomic section, use ftrace_kill.
4571 void ftrace_kill(void)
4573 ftrace_disabled = 1;
4575 clear_ftrace_function();
4579 * Test if ftrace is dead or not.
4581 int ftrace_is_dead(void)
4583 return ftrace_disabled;
4587 * register_ftrace_function - register a function for profiling
4588 * @ops - ops structure that holds the function for profiling.
4590 * Register a function to be called by all functions in the
4593 * Note: @ops->func and all the functions it calls must be labeled
4594 * with "notrace", otherwise it will go into a
4597 int register_ftrace_function(struct ftrace_ops *ops)
4601 ftrace_ops_init(ops);
4603 mutex_lock(&ftrace_lock);
4605 ret = __register_ftrace_function(ops);
4607 ret = ftrace_startup(ops, 0);
4609 mutex_unlock(&ftrace_lock);
4613 EXPORT_SYMBOL_GPL(register_ftrace_function);
4616 * unregister_ftrace_function - unregister a function for profiling.
4617 * @ops - ops structure that holds the function to unregister
4619 * Unregister a function that was added to be called by ftrace profiling.
4621 int unregister_ftrace_function(struct ftrace_ops *ops)
4625 mutex_lock(&ftrace_lock);
4626 ret = __unregister_ftrace_function(ops);
4628 ftrace_shutdown(ops, 0);
4629 mutex_unlock(&ftrace_lock);
4633 EXPORT_SYMBOL_GPL(unregister_ftrace_function);
4636 ftrace_enable_sysctl(struct ctl_table *table, int write,
4637 void __user *buffer, size_t *lenp,
4642 mutex_lock(&ftrace_lock);
4644 if (unlikely(ftrace_disabled))
4647 ret = proc_dointvec(table, write, buffer, lenp, ppos);
4649 if (ret || !write || (last_ftrace_enabled == !!ftrace_enabled))
4652 last_ftrace_enabled = !!ftrace_enabled;
4654 if (ftrace_enabled) {
4656 ftrace_startup_sysctl();
4658 /* we are starting ftrace again */
4659 if (ftrace_ops_list != &ftrace_list_end)
4660 update_ftrace_function();
4663 /* stopping ftrace calls (just send to ftrace_stub) */
4664 ftrace_trace_function = ftrace_stub;
4666 ftrace_shutdown_sysctl();
4670 mutex_unlock(&ftrace_lock);
4674 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
4676 static int ftrace_graph_active;
4677 static struct notifier_block ftrace_suspend_notifier;
4679 int ftrace_graph_entry_stub(struct ftrace_graph_ent *trace)
4684 /* The callbacks that hook a function */
4685 trace_func_graph_ret_t ftrace_graph_return =
4686 (trace_func_graph_ret_t)ftrace_stub;
4687 trace_func_graph_ent_t ftrace_graph_entry = ftrace_graph_entry_stub;
4689 /* Try to assign a return stack array on FTRACE_RETSTACK_ALLOC_SIZE tasks. */
4690 static int alloc_retstack_tasklist(struct ftrace_ret_stack **ret_stack_list)
4694 unsigned long flags;
4695 int start = 0, end = FTRACE_RETSTACK_ALLOC_SIZE;
4696 struct task_struct *g, *t;
4698 for (i = 0; i < FTRACE_RETSTACK_ALLOC_SIZE; i++) {
4699 ret_stack_list[i] = kmalloc(FTRACE_RETFUNC_DEPTH
4700 * sizeof(struct ftrace_ret_stack),
4702 if (!ret_stack_list[i]) {
4710 read_lock_irqsave(&tasklist_lock, flags);
4711 do_each_thread(g, t) {
4717 if (t->ret_stack == NULL) {
4718 atomic_set(&t->tracing_graph_pause, 0);
4719 atomic_set(&t->trace_overrun, 0);
4720 t->curr_ret_stack = -1;
4721 /* Make sure the tasks see the -1 first: */
4723 t->ret_stack = ret_stack_list[start++];
4725 } while_each_thread(g, t);
4728 read_unlock_irqrestore(&tasklist_lock, flags);
4730 for (i = start; i < end; i++)
4731 kfree(ret_stack_list[i]);
4736 ftrace_graph_probe_sched_switch(void *ignore,
4737 struct task_struct *prev, struct task_struct *next)
4739 unsigned long long timestamp;
4743 * Does the user want to count the time a function was asleep.
4744 * If so, do not update the time stamps.
4746 if (trace_flags & TRACE_ITER_SLEEP_TIME)
4749 timestamp = trace_clock_local();
4751 prev->ftrace_timestamp = timestamp;
4753 /* only process tasks that we timestamped */
4754 if (!next->ftrace_timestamp)
4758 * Update all the counters in next to make up for the
4759 * time next was sleeping.
4761 timestamp -= next->ftrace_timestamp;
4763 for (index = next->curr_ret_stack; index >= 0; index--)
4764 next->ret_stack[index].calltime += timestamp;
4767 /* Allocate a return stack for each task */
4768 static int start_graph_tracing(void)
4770 struct ftrace_ret_stack **ret_stack_list;
4773 ret_stack_list = kmalloc(FTRACE_RETSTACK_ALLOC_SIZE *
4774 sizeof(struct ftrace_ret_stack *),
4777 if (!ret_stack_list)
4780 /* The cpu_boot init_task->ret_stack will never be freed */
4781 for_each_online_cpu(cpu) {
4782 if (!idle_task(cpu)->ret_stack)
4783 ftrace_graph_init_idle_task(idle_task(cpu), cpu);
4787 ret = alloc_retstack_tasklist(ret_stack_list);
4788 } while (ret == -EAGAIN);
4791 ret = register_trace_sched_switch(ftrace_graph_probe_sched_switch, NULL);
4793 pr_info("ftrace_graph: Couldn't activate tracepoint"
4794 " probe to kernel_sched_switch\n");
4797 kfree(ret_stack_list);
4802 * Hibernation protection.
4803 * The state of the current task is too much unstable during
4804 * suspend/restore to disk. We want to protect against that.
4807 ftrace_suspend_notifier_call(struct notifier_block *bl, unsigned long state,
4811 case PM_HIBERNATION_PREPARE:
4812 pause_graph_tracing();
4815 case PM_POST_HIBERNATION:
4816 unpause_graph_tracing();
4822 int register_ftrace_graph(trace_func_graph_ret_t retfunc,
4823 trace_func_graph_ent_t entryfunc)
4827 mutex_lock(&ftrace_lock);
4829 /* we currently allow only one tracer registered at a time */
4830 if (ftrace_graph_active) {
4835 ftrace_suspend_notifier.notifier_call = ftrace_suspend_notifier_call;
4836 register_pm_notifier(&ftrace_suspend_notifier);
4838 ftrace_graph_active++;
4839 ret = start_graph_tracing();
4841 ftrace_graph_active--;
4845 ftrace_graph_return = retfunc;
4846 ftrace_graph_entry = entryfunc;
4848 ret = ftrace_startup(&global_ops, FTRACE_START_FUNC_RET);
4851 mutex_unlock(&ftrace_lock);
4855 void unregister_ftrace_graph(void)
4857 mutex_lock(&ftrace_lock);
4859 if (unlikely(!ftrace_graph_active))
4862 ftrace_graph_active--;
4863 ftrace_graph_return = (trace_func_graph_ret_t)ftrace_stub;
4864 ftrace_graph_entry = ftrace_graph_entry_stub;
4865 ftrace_shutdown(&global_ops, FTRACE_STOP_FUNC_RET);
4866 unregister_pm_notifier(&ftrace_suspend_notifier);
4867 unregister_trace_sched_switch(ftrace_graph_probe_sched_switch, NULL);
4870 mutex_unlock(&ftrace_lock);
4873 static DEFINE_PER_CPU(struct ftrace_ret_stack *, idle_ret_stack);
4876 graph_init_task(struct task_struct *t, struct ftrace_ret_stack *ret_stack)
4878 atomic_set(&t->tracing_graph_pause, 0);
4879 atomic_set(&t->trace_overrun, 0);
4880 t->ftrace_timestamp = 0;
4881 /* make curr_ret_stack visible before we add the ret_stack */
4883 t->ret_stack = ret_stack;
4887 * Allocate a return stack for the idle task. May be the first
4888 * time through, or it may be done by CPU hotplug online.
4890 void ftrace_graph_init_idle_task(struct task_struct *t, int cpu)
4892 t->curr_ret_stack = -1;
4894 * The idle task has no parent, it either has its own
4895 * stack or no stack at all.
4898 WARN_ON(t->ret_stack != per_cpu(idle_ret_stack, cpu));
4900 if (ftrace_graph_active) {
4901 struct ftrace_ret_stack *ret_stack;
4903 ret_stack = per_cpu(idle_ret_stack, cpu);
4905 ret_stack = kmalloc(FTRACE_RETFUNC_DEPTH
4906 * sizeof(struct ftrace_ret_stack),
4910 per_cpu(idle_ret_stack, cpu) = ret_stack;
4912 graph_init_task(t, ret_stack);
4916 /* Allocate a return stack for newly created task */
4917 void ftrace_graph_init_task(struct task_struct *t)
4919 /* Make sure we do not use the parent ret_stack */
4920 t->ret_stack = NULL;
4921 t->curr_ret_stack = -1;
4923 if (ftrace_graph_active) {
4924 struct ftrace_ret_stack *ret_stack;
4926 ret_stack = kmalloc(FTRACE_RETFUNC_DEPTH
4927 * sizeof(struct ftrace_ret_stack),
4931 graph_init_task(t, ret_stack);
4935 void ftrace_graph_exit_task(struct task_struct *t)
4937 struct ftrace_ret_stack *ret_stack = t->ret_stack;
4939 t->ret_stack = NULL;
4940 /* NULL must become visible to IRQs before we free it: */
4946 void ftrace_graph_stop(void)