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_CONTROL)
67 #ifdef CONFIG_DYNAMIC_FTRACE
68 #define INIT_OPS_HASH(opsname) \
69 .func_hash = &opsname.local_hash, \
70 .local_hash.regex_lock = __MUTEX_INITIALIZER(opsname.local_hash.regex_lock),
71 #define ASSIGN_OPS_HASH(opsname, val) \
73 .local_hash.regex_lock = __MUTEX_INITIALIZER(opsname.local_hash.regex_lock),
75 #define INIT_OPS_HASH(opsname)
76 #define ASSIGN_OPS_HASH(opsname, val)
79 static struct ftrace_ops ftrace_list_end __read_mostly = {
81 .flags = FTRACE_OPS_FL_RECURSION_SAFE | FTRACE_OPS_FL_STUB,
82 INIT_OPS_HASH(ftrace_list_end)
85 /* ftrace_enabled is a method to turn ftrace on or off */
86 int ftrace_enabled __read_mostly;
87 static int last_ftrace_enabled;
89 /* Current function tracing op */
90 struct ftrace_ops *function_trace_op __read_mostly = &ftrace_list_end;
91 /* What to set function_trace_op to */
92 static struct ftrace_ops *set_function_trace_op;
94 /* List for set_ftrace_pid's pids. */
95 LIST_HEAD(ftrace_pids);
97 struct list_head list;
102 * ftrace_disabled is set when an anomaly is discovered.
103 * ftrace_disabled is much stronger than ftrace_enabled.
105 static int ftrace_disabled __read_mostly;
107 static DEFINE_MUTEX(ftrace_lock);
109 static struct ftrace_ops *ftrace_control_list __read_mostly = &ftrace_list_end;
110 static struct ftrace_ops *ftrace_ops_list __read_mostly = &ftrace_list_end;
111 ftrace_func_t ftrace_trace_function __read_mostly = ftrace_stub;
112 ftrace_func_t ftrace_pid_function __read_mostly = ftrace_stub;
113 static struct ftrace_ops global_ops;
114 static struct ftrace_ops control_ops;
116 #if ARCH_SUPPORTS_FTRACE_OPS
117 static void ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip,
118 struct ftrace_ops *op, struct pt_regs *regs);
120 /* See comment below, where ftrace_ops_list_func is defined */
121 static void ftrace_ops_no_ops(unsigned long ip, unsigned long parent_ip);
122 #define ftrace_ops_list_func ((ftrace_func_t)ftrace_ops_no_ops)
126 * Traverse the ftrace_global_list, invoking all entries. The reason that we
127 * can use rcu_dereference_raw_notrace() is that elements removed from this list
128 * are simply leaked, so there is no need to interact with a grace-period
129 * mechanism. The rcu_dereference_raw_notrace() calls are needed to handle
130 * concurrent insertions into the ftrace_global_list.
132 * Silly Alpha and silly pointer-speculation compiler optimizations!
134 #define do_for_each_ftrace_op(op, list) \
135 op = rcu_dereference_raw_notrace(list); \
139 * Optimized for just a single item in the list (as that is the normal case).
141 #define while_for_each_ftrace_op(op) \
142 while (likely(op = rcu_dereference_raw_notrace((op)->next)) && \
143 unlikely((op) != &ftrace_list_end))
145 static inline void ftrace_ops_init(struct ftrace_ops *ops)
147 #ifdef CONFIG_DYNAMIC_FTRACE
148 if (!(ops->flags & FTRACE_OPS_FL_INITIALIZED)) {
149 mutex_init(&ops->local_hash.regex_lock);
150 ops->func_hash = &ops->local_hash;
151 ops->flags |= FTRACE_OPS_FL_INITIALIZED;
157 * ftrace_nr_registered_ops - return number of ops registered
159 * Returns the number of ftrace_ops registered and tracing functions
161 int ftrace_nr_registered_ops(void)
163 struct ftrace_ops *ops;
166 mutex_lock(&ftrace_lock);
168 for (ops = ftrace_ops_list;
169 ops != &ftrace_list_end; ops = ops->next)
172 mutex_unlock(&ftrace_lock);
177 static void ftrace_pid_func(unsigned long ip, unsigned long parent_ip,
178 struct ftrace_ops *op, struct pt_regs *regs)
180 if (!test_tsk_trace_trace(current))
183 ftrace_pid_function(ip, parent_ip, op, regs);
186 static void set_ftrace_pid_function(ftrace_func_t func)
188 /* do not set ftrace_pid_function to itself! */
189 if (func != ftrace_pid_func)
190 ftrace_pid_function = func;
194 * clear_ftrace_function - reset the ftrace function
196 * This NULLs the ftrace function and in essence stops
197 * tracing. There may be lag
199 void clear_ftrace_function(void)
201 ftrace_trace_function = ftrace_stub;
202 ftrace_pid_function = ftrace_stub;
205 static void control_ops_disable_all(struct ftrace_ops *ops)
209 for_each_possible_cpu(cpu)
210 *per_cpu_ptr(ops->disabled, cpu) = 1;
213 static int control_ops_alloc(struct ftrace_ops *ops)
215 int __percpu *disabled;
217 disabled = alloc_percpu(int);
221 ops->disabled = disabled;
222 control_ops_disable_all(ops);
226 static void ftrace_sync(struct work_struct *work)
229 * This function is just a stub to implement a hard force
230 * of synchronize_sched(). This requires synchronizing
231 * tasks even in userspace and idle.
233 * Yes, function tracing is rude.
237 static void ftrace_sync_ipi(void *data)
239 /* Probably not needed, but do it anyway */
243 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
244 static void update_function_graph_func(void);
246 static inline void update_function_graph_func(void) { }
249 static void update_ftrace_function(void)
254 * If we are at the end of the list and this ops is
255 * recursion safe and not dynamic and the arch supports passing ops,
256 * then have the mcount trampoline call the function directly.
258 if (ftrace_ops_list == &ftrace_list_end ||
259 (ftrace_ops_list->next == &ftrace_list_end &&
260 !(ftrace_ops_list->flags & FTRACE_OPS_FL_DYNAMIC) &&
261 (ftrace_ops_list->flags & FTRACE_OPS_FL_RECURSION_SAFE) &&
262 !FTRACE_FORCE_LIST_FUNC)) {
263 /* Set the ftrace_ops that the arch callback uses */
264 set_function_trace_op = ftrace_ops_list;
265 func = ftrace_ops_list->func;
267 /* Just use the default ftrace_ops */
268 set_function_trace_op = &ftrace_list_end;
269 func = ftrace_ops_list_func;
272 update_function_graph_func();
274 /* If there's no change, then do nothing more here */
275 if (ftrace_trace_function == func)
279 * If we are using the list function, it doesn't care
280 * about the function_trace_ops.
282 if (func == ftrace_ops_list_func) {
283 ftrace_trace_function = func;
285 * Don't even bother setting function_trace_ops,
286 * it would be racy to do so anyway.
291 #ifndef CONFIG_DYNAMIC_FTRACE
293 * For static tracing, we need to be a bit more careful.
294 * The function change takes affect immediately. Thus,
295 * we need to coorditate the setting of the function_trace_ops
296 * with the setting of the ftrace_trace_function.
298 * Set the function to the list ops, which will call the
299 * function we want, albeit indirectly, but it handles the
300 * ftrace_ops and doesn't depend on function_trace_op.
302 ftrace_trace_function = ftrace_ops_list_func;
304 * Make sure all CPUs see this. Yes this is slow, but static
305 * tracing is slow and nasty to have enabled.
307 schedule_on_each_cpu(ftrace_sync);
308 /* Now all cpus are using the list ops. */
309 function_trace_op = set_function_trace_op;
310 /* Make sure the function_trace_op is visible on all CPUs */
312 /* Nasty way to force a rmb on all cpus */
313 smp_call_function(ftrace_sync_ipi, NULL, 1);
314 /* OK, we are all set to update the ftrace_trace_function now! */
315 #endif /* !CONFIG_DYNAMIC_FTRACE */
317 ftrace_trace_function = func;
320 int using_ftrace_ops_list_func(void)
322 return ftrace_trace_function == ftrace_ops_list_func;
325 static void add_ftrace_ops(struct ftrace_ops **list, struct ftrace_ops *ops)
329 * We are entering ops into the list but another
330 * CPU might be walking that list. We need to make sure
331 * the ops->next pointer is valid before another CPU sees
332 * the ops pointer included into the list.
334 rcu_assign_pointer(*list, ops);
337 static int remove_ftrace_ops(struct ftrace_ops **list, struct ftrace_ops *ops)
339 struct ftrace_ops **p;
342 * If we are removing the last function, then simply point
343 * to the ftrace_stub.
345 if (*list == ops && ops->next == &ftrace_list_end) {
346 *list = &ftrace_list_end;
350 for (p = list; *p != &ftrace_list_end; p = &(*p)->next)
361 static void add_ftrace_list_ops(struct ftrace_ops **list,
362 struct ftrace_ops *main_ops,
363 struct ftrace_ops *ops)
365 int first = *list == &ftrace_list_end;
366 add_ftrace_ops(list, ops);
368 add_ftrace_ops(&ftrace_ops_list, main_ops);
371 static int remove_ftrace_list_ops(struct ftrace_ops **list,
372 struct ftrace_ops *main_ops,
373 struct ftrace_ops *ops)
375 int ret = remove_ftrace_ops(list, ops);
376 if (!ret && *list == &ftrace_list_end)
377 ret = remove_ftrace_ops(&ftrace_ops_list, main_ops);
381 static int __register_ftrace_function(struct ftrace_ops *ops)
383 if (ops->flags & FTRACE_OPS_FL_DELETED)
386 if (WARN_ON(ops->flags & FTRACE_OPS_FL_ENABLED))
389 #ifndef CONFIG_DYNAMIC_FTRACE_WITH_REGS
391 * If the ftrace_ops specifies SAVE_REGS, then it only can be used
392 * if the arch supports it, or SAVE_REGS_IF_SUPPORTED is also set.
393 * Setting SAVE_REGS_IF_SUPPORTED makes SAVE_REGS irrelevant.
395 if (ops->flags & FTRACE_OPS_FL_SAVE_REGS &&
396 !(ops->flags & FTRACE_OPS_FL_SAVE_REGS_IF_SUPPORTED))
399 if (ops->flags & FTRACE_OPS_FL_SAVE_REGS_IF_SUPPORTED)
400 ops->flags |= FTRACE_OPS_FL_SAVE_REGS;
403 if (!core_kernel_data((unsigned long)ops))
404 ops->flags |= FTRACE_OPS_FL_DYNAMIC;
406 if (ops->flags & FTRACE_OPS_FL_CONTROL) {
407 if (control_ops_alloc(ops))
409 add_ftrace_list_ops(&ftrace_control_list, &control_ops, ops);
411 add_ftrace_ops(&ftrace_ops_list, ops);
414 update_ftrace_function();
419 static int __unregister_ftrace_function(struct ftrace_ops *ops)
423 if (WARN_ON(!(ops->flags & FTRACE_OPS_FL_ENABLED)))
426 if (ops->flags & FTRACE_OPS_FL_CONTROL) {
427 ret = remove_ftrace_list_ops(&ftrace_control_list,
430 ret = remove_ftrace_ops(&ftrace_ops_list, ops);
436 update_ftrace_function();
441 static void ftrace_update_pid_func(void)
443 /* Only do something if we are tracing something */
444 if (ftrace_trace_function == ftrace_stub)
447 update_ftrace_function();
450 #ifdef CONFIG_FUNCTION_PROFILER
451 struct ftrace_profile {
452 struct hlist_node node;
454 unsigned long counter;
455 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
456 unsigned long long time;
457 unsigned long long time_squared;
461 struct ftrace_profile_page {
462 struct ftrace_profile_page *next;
464 struct ftrace_profile records[];
467 struct ftrace_profile_stat {
469 struct hlist_head *hash;
470 struct ftrace_profile_page *pages;
471 struct ftrace_profile_page *start;
472 struct tracer_stat stat;
475 #define PROFILE_RECORDS_SIZE \
476 (PAGE_SIZE - offsetof(struct ftrace_profile_page, records))
478 #define PROFILES_PER_PAGE \
479 (PROFILE_RECORDS_SIZE / sizeof(struct ftrace_profile))
481 static int ftrace_profile_enabled __read_mostly;
483 /* ftrace_profile_lock - synchronize the enable and disable of the profiler */
484 static DEFINE_MUTEX(ftrace_profile_lock);
486 static DEFINE_PER_CPU(struct ftrace_profile_stat, ftrace_profile_stats);
488 #define FTRACE_PROFILE_HASH_BITS 10
489 #define FTRACE_PROFILE_HASH_SIZE (1 << FTRACE_PROFILE_HASH_BITS)
492 function_stat_next(void *v, int idx)
494 struct ftrace_profile *rec = v;
495 struct ftrace_profile_page *pg;
497 pg = (struct ftrace_profile_page *)((unsigned long)rec & PAGE_MASK);
503 if ((void *)rec >= (void *)&pg->records[pg->index]) {
507 rec = &pg->records[0];
515 static void *function_stat_start(struct tracer_stat *trace)
517 struct ftrace_profile_stat *stat =
518 container_of(trace, struct ftrace_profile_stat, stat);
520 if (!stat || !stat->start)
523 return function_stat_next(&stat->start->records[0], 0);
526 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
527 /* function graph compares on total time */
528 static int function_stat_cmp(void *p1, void *p2)
530 struct ftrace_profile *a = p1;
531 struct ftrace_profile *b = p2;
533 if (a->time < b->time)
535 if (a->time > b->time)
541 /* not function graph compares against hits */
542 static int function_stat_cmp(void *p1, void *p2)
544 struct ftrace_profile *a = p1;
545 struct ftrace_profile *b = p2;
547 if (a->counter < b->counter)
549 if (a->counter > b->counter)
556 static int function_stat_headers(struct seq_file *m)
558 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
559 seq_printf(m, " Function "
562 "--- ---- --- ---\n");
564 seq_printf(m, " Function Hit\n"
570 static int function_stat_show(struct seq_file *m, void *v)
572 struct ftrace_profile *rec = v;
573 char str[KSYM_SYMBOL_LEN];
575 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
576 static struct trace_seq s;
577 unsigned long long avg;
578 unsigned long long stddev;
580 mutex_lock(&ftrace_profile_lock);
582 /* we raced with function_profile_reset() */
583 if (unlikely(rec->counter == 0)) {
588 kallsyms_lookup(rec->ip, NULL, NULL, NULL, str);
589 seq_printf(m, " %-30.30s %10lu", str, rec->counter);
591 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
594 do_div(avg, rec->counter);
596 /* Sample standard deviation (s^2) */
597 if (rec->counter <= 1)
601 * Apply Welford's method:
602 * s^2 = 1 / (n * (n-1)) * (n * \Sum (x_i)^2 - (\Sum x_i)^2)
604 stddev = rec->counter * rec->time_squared -
605 rec->time * rec->time;
608 * Divide only 1000 for ns^2 -> us^2 conversion.
609 * trace_print_graph_duration will divide 1000 again.
611 do_div(stddev, rec->counter * (rec->counter - 1) * 1000);
615 trace_print_graph_duration(rec->time, &s);
616 trace_seq_puts(&s, " ");
617 trace_print_graph_duration(avg, &s);
618 trace_seq_puts(&s, " ");
619 trace_print_graph_duration(stddev, &s);
620 trace_print_seq(m, &s);
624 mutex_unlock(&ftrace_profile_lock);
629 static void ftrace_profile_reset(struct ftrace_profile_stat *stat)
631 struct ftrace_profile_page *pg;
633 pg = stat->pages = stat->start;
636 memset(pg->records, 0, PROFILE_RECORDS_SIZE);
641 memset(stat->hash, 0,
642 FTRACE_PROFILE_HASH_SIZE * sizeof(struct hlist_head));
645 int ftrace_profile_pages_init(struct ftrace_profile_stat *stat)
647 struct ftrace_profile_page *pg;
652 /* If we already allocated, do nothing */
656 stat->pages = (void *)get_zeroed_page(GFP_KERNEL);
660 #ifdef CONFIG_DYNAMIC_FTRACE
661 functions = ftrace_update_tot_cnt;
664 * We do not know the number of functions that exist because
665 * dynamic tracing is what counts them. With past experience
666 * we have around 20K functions. That should be more than enough.
667 * It is highly unlikely we will execute every function in
673 pg = stat->start = stat->pages;
675 pages = DIV_ROUND_UP(functions, PROFILES_PER_PAGE);
677 for (i = 1; i < pages; i++) {
678 pg->next = (void *)get_zeroed_page(GFP_KERNEL);
689 unsigned long tmp = (unsigned long)pg;
701 static int ftrace_profile_init_cpu(int cpu)
703 struct ftrace_profile_stat *stat;
706 stat = &per_cpu(ftrace_profile_stats, cpu);
709 /* If the profile is already created, simply reset it */
710 ftrace_profile_reset(stat);
715 * We are profiling all functions, but usually only a few thousand
716 * functions are hit. We'll make a hash of 1024 items.
718 size = FTRACE_PROFILE_HASH_SIZE;
720 stat->hash = kzalloc(sizeof(struct hlist_head) * size, GFP_KERNEL);
725 /* Preallocate the function profiling pages */
726 if (ftrace_profile_pages_init(stat) < 0) {
735 static int ftrace_profile_init(void)
740 for_each_possible_cpu(cpu) {
741 ret = ftrace_profile_init_cpu(cpu);
749 /* interrupts must be disabled */
750 static struct ftrace_profile *
751 ftrace_find_profiled_func(struct ftrace_profile_stat *stat, unsigned long ip)
753 struct ftrace_profile *rec;
754 struct hlist_head *hhd;
757 key = hash_long(ip, FTRACE_PROFILE_HASH_BITS);
758 hhd = &stat->hash[key];
760 if (hlist_empty(hhd))
763 hlist_for_each_entry_rcu_notrace(rec, hhd, node) {
771 static void ftrace_add_profile(struct ftrace_profile_stat *stat,
772 struct ftrace_profile *rec)
776 key = hash_long(rec->ip, FTRACE_PROFILE_HASH_BITS);
777 hlist_add_head_rcu(&rec->node, &stat->hash[key]);
781 * The memory is already allocated, this simply finds a new record to use.
783 static struct ftrace_profile *
784 ftrace_profile_alloc(struct ftrace_profile_stat *stat, unsigned long ip)
786 struct ftrace_profile *rec = NULL;
788 /* prevent recursion (from NMIs) */
789 if (atomic_inc_return(&stat->disabled) != 1)
793 * Try to find the function again since an NMI
794 * could have added it
796 rec = ftrace_find_profiled_func(stat, ip);
800 if (stat->pages->index == PROFILES_PER_PAGE) {
801 if (!stat->pages->next)
803 stat->pages = stat->pages->next;
806 rec = &stat->pages->records[stat->pages->index++];
808 ftrace_add_profile(stat, rec);
811 atomic_dec(&stat->disabled);
817 function_profile_call(unsigned long ip, unsigned long parent_ip,
818 struct ftrace_ops *ops, struct pt_regs *regs)
820 struct ftrace_profile_stat *stat;
821 struct ftrace_profile *rec;
824 if (!ftrace_profile_enabled)
827 local_irq_save(flags);
829 stat = this_cpu_ptr(&ftrace_profile_stats);
830 if (!stat->hash || !ftrace_profile_enabled)
833 rec = ftrace_find_profiled_func(stat, ip);
835 rec = ftrace_profile_alloc(stat, ip);
842 local_irq_restore(flags);
845 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
846 static int profile_graph_entry(struct ftrace_graph_ent *trace)
848 function_profile_call(trace->func, 0, NULL, NULL);
852 static void profile_graph_return(struct ftrace_graph_ret *trace)
854 struct ftrace_profile_stat *stat;
855 unsigned long long calltime;
856 struct ftrace_profile *rec;
859 local_irq_save(flags);
860 stat = this_cpu_ptr(&ftrace_profile_stats);
861 if (!stat->hash || !ftrace_profile_enabled)
864 /* If the calltime was zero'd ignore it */
865 if (!trace->calltime)
868 calltime = trace->rettime - trace->calltime;
870 if (!(trace_flags & TRACE_ITER_GRAPH_TIME)) {
873 index = trace->depth;
875 /* Append this call time to the parent time to subtract */
877 current->ret_stack[index - 1].subtime += calltime;
879 if (current->ret_stack[index].subtime < calltime)
880 calltime -= current->ret_stack[index].subtime;
885 rec = ftrace_find_profiled_func(stat, trace->func);
887 rec->time += calltime;
888 rec->time_squared += calltime * calltime;
892 local_irq_restore(flags);
895 static int register_ftrace_profiler(void)
897 return register_ftrace_graph(&profile_graph_return,
898 &profile_graph_entry);
901 static void unregister_ftrace_profiler(void)
903 unregister_ftrace_graph();
906 static struct ftrace_ops ftrace_profile_ops __read_mostly = {
907 .func = function_profile_call,
908 .flags = FTRACE_OPS_FL_RECURSION_SAFE | FTRACE_OPS_FL_INITIALIZED,
909 INIT_OPS_HASH(ftrace_profile_ops)
912 static int register_ftrace_profiler(void)
914 return register_ftrace_function(&ftrace_profile_ops);
917 static void unregister_ftrace_profiler(void)
919 unregister_ftrace_function(&ftrace_profile_ops);
921 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
924 ftrace_profile_write(struct file *filp, const char __user *ubuf,
925 size_t cnt, loff_t *ppos)
930 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
936 mutex_lock(&ftrace_profile_lock);
937 if (ftrace_profile_enabled ^ val) {
939 ret = ftrace_profile_init();
945 ret = register_ftrace_profiler();
950 ftrace_profile_enabled = 1;
952 ftrace_profile_enabled = 0;
954 * unregister_ftrace_profiler calls stop_machine
955 * so this acts like an synchronize_sched.
957 unregister_ftrace_profiler();
961 mutex_unlock(&ftrace_profile_lock);
969 ftrace_profile_read(struct file *filp, char __user *ubuf,
970 size_t cnt, loff_t *ppos)
972 char buf[64]; /* big enough to hold a number */
975 r = sprintf(buf, "%u\n", ftrace_profile_enabled);
976 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
979 static const struct file_operations ftrace_profile_fops = {
980 .open = tracing_open_generic,
981 .read = ftrace_profile_read,
982 .write = ftrace_profile_write,
983 .llseek = default_llseek,
986 /* used to initialize the real stat files */
987 static struct tracer_stat function_stats __initdata = {
989 .stat_start = function_stat_start,
990 .stat_next = function_stat_next,
991 .stat_cmp = function_stat_cmp,
992 .stat_headers = function_stat_headers,
993 .stat_show = function_stat_show
996 static __init void ftrace_profile_debugfs(struct dentry *d_tracer)
998 struct ftrace_profile_stat *stat;
999 struct dentry *entry;
1004 for_each_possible_cpu(cpu) {
1005 stat = &per_cpu(ftrace_profile_stats, cpu);
1007 /* allocate enough for function name + cpu number */
1008 name = kmalloc(32, GFP_KERNEL);
1011 * The files created are permanent, if something happens
1012 * we still do not free memory.
1015 "Could not allocate stat file for cpu %d\n",
1019 stat->stat = function_stats;
1020 snprintf(name, 32, "function%d", cpu);
1021 stat->stat.name = name;
1022 ret = register_stat_tracer(&stat->stat);
1025 "Could not register function stat for cpu %d\n",
1032 entry = debugfs_create_file("function_profile_enabled", 0644,
1033 d_tracer, NULL, &ftrace_profile_fops);
1035 pr_warning("Could not create debugfs "
1036 "'function_profile_enabled' entry\n");
1039 #else /* CONFIG_FUNCTION_PROFILER */
1040 static __init void ftrace_profile_debugfs(struct dentry *d_tracer)
1043 #endif /* CONFIG_FUNCTION_PROFILER */
1045 static struct pid * const ftrace_swapper_pid = &init_struct_pid;
1047 #ifdef CONFIG_DYNAMIC_FTRACE
1049 static struct ftrace_ops *removed_ops;
1051 #ifndef CONFIG_FTRACE_MCOUNT_RECORD
1052 # error Dynamic ftrace depends on MCOUNT_RECORD
1055 static struct hlist_head ftrace_func_hash[FTRACE_FUNC_HASHSIZE] __read_mostly;
1057 struct ftrace_func_probe {
1058 struct hlist_node node;
1059 struct ftrace_probe_ops *ops;
1060 unsigned long flags;
1063 struct list_head free_list;
1066 struct ftrace_func_entry {
1067 struct hlist_node hlist;
1071 struct ftrace_hash {
1072 unsigned long size_bits;
1073 struct hlist_head *buckets;
1074 unsigned long count;
1075 struct rcu_head rcu;
1079 * We make these constant because no one should touch them,
1080 * but they are used as the default "empty hash", to avoid allocating
1081 * it all the time. These are in a read only section such that if
1082 * anyone does try to modify it, it will cause an exception.
1084 static const struct hlist_head empty_buckets[1];
1085 static const struct ftrace_hash empty_hash = {
1086 .buckets = (struct hlist_head *)empty_buckets,
1088 #define EMPTY_HASH ((struct ftrace_hash *)&empty_hash)
1090 static struct ftrace_ops global_ops = {
1091 .func = ftrace_stub,
1092 .local_hash.notrace_hash = EMPTY_HASH,
1093 .local_hash.filter_hash = EMPTY_HASH,
1094 INIT_OPS_HASH(global_ops)
1095 .flags = FTRACE_OPS_FL_RECURSION_SAFE |
1096 FTRACE_OPS_FL_INITIALIZED,
1099 struct ftrace_page {
1100 struct ftrace_page *next;
1101 struct dyn_ftrace *records;
1106 #define ENTRY_SIZE sizeof(struct dyn_ftrace)
1107 #define ENTRIES_PER_PAGE (PAGE_SIZE / ENTRY_SIZE)
1109 /* estimate from running different kernels */
1110 #define NR_TO_INIT 10000
1112 static struct ftrace_page *ftrace_pages_start;
1113 static struct ftrace_page *ftrace_pages;
1115 static bool __always_inline ftrace_hash_empty(struct ftrace_hash *hash)
1117 return !hash || !hash->count;
1120 static struct ftrace_func_entry *
1121 ftrace_lookup_ip(struct ftrace_hash *hash, unsigned long ip)
1124 struct ftrace_func_entry *entry;
1125 struct hlist_head *hhd;
1127 if (ftrace_hash_empty(hash))
1130 if (hash->size_bits > 0)
1131 key = hash_long(ip, hash->size_bits);
1135 hhd = &hash->buckets[key];
1137 hlist_for_each_entry_rcu_notrace(entry, hhd, hlist) {
1138 if (entry->ip == ip)
1144 static void __add_hash_entry(struct ftrace_hash *hash,
1145 struct ftrace_func_entry *entry)
1147 struct hlist_head *hhd;
1150 if (hash->size_bits)
1151 key = hash_long(entry->ip, hash->size_bits);
1155 hhd = &hash->buckets[key];
1156 hlist_add_head(&entry->hlist, hhd);
1160 static int add_hash_entry(struct ftrace_hash *hash, unsigned long ip)
1162 struct ftrace_func_entry *entry;
1164 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
1169 __add_hash_entry(hash, entry);
1175 free_hash_entry(struct ftrace_hash *hash,
1176 struct ftrace_func_entry *entry)
1178 hlist_del(&entry->hlist);
1184 remove_hash_entry(struct ftrace_hash *hash,
1185 struct ftrace_func_entry *entry)
1187 hlist_del(&entry->hlist);
1191 static void ftrace_hash_clear(struct ftrace_hash *hash)
1193 struct hlist_head *hhd;
1194 struct hlist_node *tn;
1195 struct ftrace_func_entry *entry;
1196 int size = 1 << hash->size_bits;
1202 for (i = 0; i < size; i++) {
1203 hhd = &hash->buckets[i];
1204 hlist_for_each_entry_safe(entry, tn, hhd, hlist)
1205 free_hash_entry(hash, entry);
1207 FTRACE_WARN_ON(hash->count);
1210 static void free_ftrace_hash(struct ftrace_hash *hash)
1212 if (!hash || hash == EMPTY_HASH)
1214 ftrace_hash_clear(hash);
1215 kfree(hash->buckets);
1219 static void __free_ftrace_hash_rcu(struct rcu_head *rcu)
1221 struct ftrace_hash *hash;
1223 hash = container_of(rcu, struct ftrace_hash, rcu);
1224 free_ftrace_hash(hash);
1227 static void free_ftrace_hash_rcu(struct ftrace_hash *hash)
1229 if (!hash || hash == EMPTY_HASH)
1231 call_rcu_sched(&hash->rcu, __free_ftrace_hash_rcu);
1234 void ftrace_free_filter(struct ftrace_ops *ops)
1236 ftrace_ops_init(ops);
1237 free_ftrace_hash(ops->func_hash->filter_hash);
1238 free_ftrace_hash(ops->func_hash->notrace_hash);
1241 static struct ftrace_hash *alloc_ftrace_hash(int size_bits)
1243 struct ftrace_hash *hash;
1246 hash = kzalloc(sizeof(*hash), GFP_KERNEL);
1250 size = 1 << size_bits;
1251 hash->buckets = kcalloc(size, sizeof(*hash->buckets), GFP_KERNEL);
1253 if (!hash->buckets) {
1258 hash->size_bits = size_bits;
1263 static struct ftrace_hash *
1264 alloc_and_copy_ftrace_hash(int size_bits, struct ftrace_hash *hash)
1266 struct ftrace_func_entry *entry;
1267 struct ftrace_hash *new_hash;
1272 new_hash = alloc_ftrace_hash(size_bits);
1277 if (ftrace_hash_empty(hash))
1280 size = 1 << hash->size_bits;
1281 for (i = 0; i < size; i++) {
1282 hlist_for_each_entry(entry, &hash->buckets[i], hlist) {
1283 ret = add_hash_entry(new_hash, entry->ip);
1289 FTRACE_WARN_ON(new_hash->count != hash->count);
1294 free_ftrace_hash(new_hash);
1299 ftrace_hash_rec_disable_modify(struct ftrace_ops *ops, int filter_hash);
1301 ftrace_hash_rec_enable_modify(struct ftrace_ops *ops, int filter_hash);
1304 ftrace_hash_move(struct ftrace_ops *ops, int enable,
1305 struct ftrace_hash **dst, struct ftrace_hash *src)
1307 struct ftrace_func_entry *entry;
1308 struct hlist_node *tn;
1309 struct hlist_head *hhd;
1310 struct ftrace_hash *old_hash;
1311 struct ftrace_hash *new_hash;
1312 int size = src->count;
1317 * If the new source is empty, just free dst and assign it
1321 new_hash = EMPTY_HASH;
1326 * Make the hash size about 1/2 the # found
1328 for (size /= 2; size; size >>= 1)
1331 /* Don't allocate too much */
1332 if (bits > FTRACE_HASH_MAX_BITS)
1333 bits = FTRACE_HASH_MAX_BITS;
1335 new_hash = alloc_ftrace_hash(bits);
1339 size = 1 << src->size_bits;
1340 for (i = 0; i < size; i++) {
1341 hhd = &src->buckets[i];
1342 hlist_for_each_entry_safe(entry, tn, hhd, hlist) {
1343 remove_hash_entry(src, entry);
1344 __add_hash_entry(new_hash, entry);
1350 * Remove the current set, update the hash and add
1353 ftrace_hash_rec_disable_modify(ops, enable);
1356 rcu_assign_pointer(*dst, new_hash);
1357 free_ftrace_hash_rcu(old_hash);
1359 ftrace_hash_rec_enable_modify(ops, enable);
1365 * Test the hashes for this ops to see if we want to call
1366 * the ops->func or not.
1368 * It's a match if the ip is in the ops->filter_hash or
1369 * the filter_hash does not exist or is empty,
1371 * the ip is not in the ops->notrace_hash.
1373 * This needs to be called with preemption disabled as
1374 * the hashes are freed with call_rcu_sched().
1377 ftrace_ops_test(struct ftrace_ops *ops, unsigned long ip, void *regs)
1379 struct ftrace_hash *filter_hash;
1380 struct ftrace_hash *notrace_hash;
1383 #ifdef CONFIG_DYNAMIC_FTRACE_WITH_REGS
1385 * There's a small race when adding ops that the ftrace handler
1386 * that wants regs, may be called without them. We can not
1387 * allow that handler to be called if regs is NULL.
1389 if (regs == NULL && (ops->flags & FTRACE_OPS_FL_SAVE_REGS))
1393 filter_hash = rcu_dereference_raw_notrace(ops->func_hash->filter_hash);
1394 notrace_hash = rcu_dereference_raw_notrace(ops->func_hash->notrace_hash);
1396 if ((ftrace_hash_empty(filter_hash) ||
1397 ftrace_lookup_ip(filter_hash, ip)) &&
1398 (ftrace_hash_empty(notrace_hash) ||
1399 !ftrace_lookup_ip(notrace_hash, ip)))
1408 * This is a double for. Do not use 'break' to break out of the loop,
1409 * you must use a goto.
1411 #define do_for_each_ftrace_rec(pg, rec) \
1412 for (pg = ftrace_pages_start; pg; pg = pg->next) { \
1414 for (_____i = 0; _____i < pg->index; _____i++) { \
1415 rec = &pg->records[_____i];
1417 #define while_for_each_ftrace_rec() \
1422 static int ftrace_cmp_recs(const void *a, const void *b)
1424 const struct dyn_ftrace *key = a;
1425 const struct dyn_ftrace *rec = b;
1427 if (key->flags < rec->ip)
1429 if (key->ip >= rec->ip + MCOUNT_INSN_SIZE)
1434 static unsigned long ftrace_location_range(unsigned long start, unsigned long end)
1436 struct ftrace_page *pg;
1437 struct dyn_ftrace *rec;
1438 struct dyn_ftrace key;
1441 key.flags = end; /* overload flags, as it is unsigned long */
1443 for (pg = ftrace_pages_start; pg; pg = pg->next) {
1444 if (end < pg->records[0].ip ||
1445 start >= (pg->records[pg->index - 1].ip + MCOUNT_INSN_SIZE))
1447 rec = bsearch(&key, pg->records, pg->index,
1448 sizeof(struct dyn_ftrace),
1458 * ftrace_location - return true if the ip giving is a traced location
1459 * @ip: the instruction pointer to check
1461 * Returns rec->ip if @ip given is a pointer to a ftrace location.
1462 * That is, the instruction that is either a NOP or call to
1463 * the function tracer. It checks the ftrace internal tables to
1464 * determine if the address belongs or not.
1466 unsigned long ftrace_location(unsigned long ip)
1468 return ftrace_location_range(ip, ip);
1472 * ftrace_text_reserved - return true if range contains an ftrace location
1473 * @start: start of range to search
1474 * @end: end of range to search (inclusive). @end points to the last byte to check.
1476 * Returns 1 if @start and @end contains a ftrace location.
1477 * That is, the instruction that is either a NOP or call to
1478 * the function tracer. It checks the ftrace internal tables to
1479 * determine if the address belongs or not.
1481 int ftrace_text_reserved(const void *start, const void *end)
1485 ret = ftrace_location_range((unsigned long)start,
1486 (unsigned long)end);
1491 /* Test if ops registered to this rec needs regs */
1492 static bool test_rec_ops_needs_regs(struct dyn_ftrace *rec)
1494 struct ftrace_ops *ops;
1495 bool keep_regs = false;
1497 for (ops = ftrace_ops_list;
1498 ops != &ftrace_list_end; ops = ops->next) {
1499 /* pass rec in as regs to have non-NULL val */
1500 if (ftrace_ops_test(ops, rec->ip, rec)) {
1501 if (ops->flags & FTRACE_OPS_FL_SAVE_REGS) {
1511 static void ftrace_remove_tramp(struct ftrace_ops *ops,
1512 struct dyn_ftrace *rec)
1514 /* If TRAMP is not set, no ops should have a trampoline for this */
1515 if (!(rec->flags & FTRACE_FL_TRAMP))
1518 rec->flags &= ~FTRACE_FL_TRAMP;
1520 if ((!ftrace_hash_empty(ops->func_hash->filter_hash) &&
1521 !ftrace_lookup_ip(ops->func_hash->filter_hash, rec->ip)) ||
1522 ftrace_lookup_ip(ops->func_hash->notrace_hash, rec->ip))
1525 * The tramp_hash entry will be removed at time
1528 ops->nr_trampolines--;
1531 static void ftrace_clear_tramps(struct dyn_ftrace *rec, struct ftrace_ops *ops)
1533 struct ftrace_ops *op;
1535 /* If TRAMP is not set, no ops should have a trampoline for this */
1536 if (!(rec->flags & FTRACE_FL_TRAMP))
1539 do_for_each_ftrace_op(op, ftrace_ops_list) {
1541 * This function is called to clear other tramps
1542 * not the one that is being updated.
1546 if (op->nr_trampolines)
1547 ftrace_remove_tramp(op, rec);
1548 } while_for_each_ftrace_op(op);
1551 static void __ftrace_hash_rec_update(struct ftrace_ops *ops,
1555 struct ftrace_hash *hash;
1556 struct ftrace_hash *other_hash;
1557 struct ftrace_page *pg;
1558 struct dyn_ftrace *rec;
1562 /* Only update if the ops has been registered */
1563 if (!(ops->flags & FTRACE_OPS_FL_ENABLED))
1567 * In the filter_hash case:
1568 * If the count is zero, we update all records.
1569 * Otherwise we just update the items in the hash.
1571 * In the notrace_hash case:
1572 * We enable the update in the hash.
1573 * As disabling notrace means enabling the tracing,
1574 * and enabling notrace means disabling, the inc variable
1578 hash = ops->func_hash->filter_hash;
1579 other_hash = ops->func_hash->notrace_hash;
1580 if (ftrace_hash_empty(hash))
1584 hash = ops->func_hash->notrace_hash;
1585 other_hash = ops->func_hash->filter_hash;
1587 * If the notrace hash has no items,
1588 * then there's nothing to do.
1590 if (ftrace_hash_empty(hash))
1594 do_for_each_ftrace_rec(pg, rec) {
1595 int in_other_hash = 0;
1601 * Only the filter_hash affects all records.
1602 * Update if the record is not in the notrace hash.
1604 if (!other_hash || !ftrace_lookup_ip(other_hash, rec->ip))
1607 in_hash = !!ftrace_lookup_ip(hash, rec->ip);
1608 in_other_hash = !!ftrace_lookup_ip(other_hash, rec->ip);
1611 * If filter_hash is set, we want to match all functions
1612 * that are in the hash but not in the other hash.
1614 * If filter_hash is not set, then we are decrementing.
1615 * That means we match anything that is in the hash
1616 * and also in the other_hash. That is, we need to turn
1617 * off functions in the other hash because they are disabled
1620 if (filter_hash && in_hash && !in_other_hash)
1622 else if (!filter_hash && in_hash &&
1623 (in_other_hash || ftrace_hash_empty(other_hash)))
1631 if (FTRACE_WARN_ON(ftrace_rec_count(rec) == FTRACE_REF_MAX))
1635 * If there's only a single callback registered to a
1636 * function, and the ops has a trampoline registered
1637 * for it, then we can call it directly.
1639 if (ftrace_rec_count(rec) == 1 && ops->trampoline) {
1640 rec->flags |= FTRACE_FL_TRAMP;
1641 ops->nr_trampolines++;
1644 * If we are adding another function callback
1645 * to this function, and the previous had a
1646 * custom trampoline in use, then we need to go
1647 * back to the default trampoline.
1649 ftrace_clear_tramps(rec, ops);
1653 * If any ops wants regs saved for this function
1654 * then all ops will get saved regs.
1656 if (ops->flags & FTRACE_OPS_FL_SAVE_REGS)
1657 rec->flags |= FTRACE_FL_REGS;
1659 if (FTRACE_WARN_ON(ftrace_rec_count(rec) == 0))
1663 if (ops->trampoline && !ftrace_rec_count(rec))
1664 ftrace_remove_tramp(ops, rec);
1667 * If the rec had REGS enabled and the ops that is
1668 * being removed had REGS set, then see if there is
1669 * still any ops for this record that wants regs.
1670 * If not, we can stop recording them.
1672 if (ftrace_rec_count(rec) > 0 &&
1673 rec->flags & FTRACE_FL_REGS &&
1674 ops->flags & FTRACE_OPS_FL_SAVE_REGS) {
1675 if (!test_rec_ops_needs_regs(rec))
1676 rec->flags &= ~FTRACE_FL_REGS;
1680 * flags will be cleared in ftrace_check_record()
1681 * if rec count is zero.
1685 /* Shortcut, if we handled all records, we are done. */
1686 if (!all && count == hash->count)
1688 } while_for_each_ftrace_rec();
1691 static void ftrace_hash_rec_disable(struct ftrace_ops *ops,
1694 __ftrace_hash_rec_update(ops, filter_hash, 0);
1697 static void ftrace_hash_rec_enable(struct ftrace_ops *ops,
1700 __ftrace_hash_rec_update(ops, filter_hash, 1);
1703 static void ftrace_hash_rec_update_modify(struct ftrace_ops *ops,
1704 int filter_hash, int inc)
1706 struct ftrace_ops *op;
1708 __ftrace_hash_rec_update(ops, filter_hash, inc);
1710 if (ops->func_hash != &global_ops.local_hash)
1714 * If the ops shares the global_ops hash, then we need to update
1715 * all ops that are enabled and use this hash.
1717 do_for_each_ftrace_op(op, ftrace_ops_list) {
1721 if (op->func_hash == &global_ops.local_hash)
1722 __ftrace_hash_rec_update(op, filter_hash, inc);
1723 } while_for_each_ftrace_op(op);
1726 static void ftrace_hash_rec_disable_modify(struct ftrace_ops *ops,
1729 ftrace_hash_rec_update_modify(ops, filter_hash, 0);
1732 static void ftrace_hash_rec_enable_modify(struct ftrace_ops *ops,
1735 ftrace_hash_rec_update_modify(ops, filter_hash, 1);
1738 static void print_ip_ins(const char *fmt, unsigned char *p)
1742 printk(KERN_CONT "%s", fmt);
1744 for (i = 0; i < MCOUNT_INSN_SIZE; i++)
1745 printk(KERN_CONT "%s%02x", i ? ":" : "", p[i]);
1749 * ftrace_bug - report and shutdown function tracer
1750 * @failed: The failed type (EFAULT, EINVAL, EPERM)
1751 * @ip: The address that failed
1753 * The arch code that enables or disables the function tracing
1754 * can call ftrace_bug() when it has detected a problem in
1755 * modifying the code. @failed should be one of either:
1756 * EFAULT - if the problem happens on reading the @ip address
1757 * EINVAL - if what is read at @ip is not what was expected
1758 * EPERM - if the problem happens on writting to the @ip address
1760 void ftrace_bug(int failed, unsigned long ip)
1764 FTRACE_WARN_ON_ONCE(1);
1765 pr_info("ftrace faulted on modifying ");
1769 FTRACE_WARN_ON_ONCE(1);
1770 pr_info("ftrace failed to modify ");
1772 print_ip_ins(" actual: ", (unsigned char *)ip);
1773 printk(KERN_CONT "\n");
1776 FTRACE_WARN_ON_ONCE(1);
1777 pr_info("ftrace faulted on writing ");
1781 FTRACE_WARN_ON_ONCE(1);
1782 pr_info("ftrace faulted on unknown error ");
1787 static int ftrace_check_record(struct dyn_ftrace *rec, int enable, int update)
1789 unsigned long flag = 0UL;
1792 * If we are updating calls:
1794 * If the record has a ref count, then we need to enable it
1795 * because someone is using it.
1797 * Otherwise we make sure its disabled.
1799 * If we are disabling calls, then disable all records that
1802 if (enable && ftrace_rec_count(rec))
1803 flag = FTRACE_FL_ENABLED;
1806 * If enabling and the REGS flag does not match the REGS_EN, or
1807 * the TRAMP flag doesn't match the TRAMP_EN, then do not ignore
1808 * this record. Set flags to fail the compare against ENABLED.
1811 if (!(rec->flags & FTRACE_FL_REGS) !=
1812 !(rec->flags & FTRACE_FL_REGS_EN))
1813 flag |= FTRACE_FL_REGS;
1815 if (!(rec->flags & FTRACE_FL_TRAMP) !=
1816 !(rec->flags & FTRACE_FL_TRAMP_EN))
1817 flag |= FTRACE_FL_TRAMP;
1820 /* If the state of this record hasn't changed, then do nothing */
1821 if ((rec->flags & FTRACE_FL_ENABLED) == flag)
1822 return FTRACE_UPDATE_IGNORE;
1825 /* Save off if rec is being enabled (for return value) */
1826 flag ^= rec->flags & FTRACE_FL_ENABLED;
1829 rec->flags |= FTRACE_FL_ENABLED;
1830 if (flag & FTRACE_FL_REGS) {
1831 if (rec->flags & FTRACE_FL_REGS)
1832 rec->flags |= FTRACE_FL_REGS_EN;
1834 rec->flags &= ~FTRACE_FL_REGS_EN;
1836 if (flag & FTRACE_FL_TRAMP) {
1837 if (rec->flags & FTRACE_FL_TRAMP)
1838 rec->flags |= FTRACE_FL_TRAMP_EN;
1840 rec->flags &= ~FTRACE_FL_TRAMP_EN;
1845 * If this record is being updated from a nop, then
1846 * return UPDATE_MAKE_CALL.
1848 * return UPDATE_MODIFY_CALL to tell the caller to convert
1849 * from the save regs, to a non-save regs function or
1850 * vice versa, or from a trampoline call.
1852 if (flag & FTRACE_FL_ENABLED)
1853 return FTRACE_UPDATE_MAKE_CALL;
1855 return FTRACE_UPDATE_MODIFY_CALL;
1859 /* If there's no more users, clear all flags */
1860 if (!ftrace_rec_count(rec))
1863 /* Just disable the record (keep REGS state) */
1864 rec->flags &= ~FTRACE_FL_ENABLED;
1867 return FTRACE_UPDATE_MAKE_NOP;
1871 * ftrace_update_record, set a record that now is tracing or not
1872 * @rec: the record to update
1873 * @enable: set to 1 if the record is tracing, zero to force disable
1875 * The records that represent all functions that can be traced need
1876 * to be updated when tracing has been enabled.
1878 int ftrace_update_record(struct dyn_ftrace *rec, int enable)
1880 return ftrace_check_record(rec, enable, 1);
1884 * ftrace_test_record, check if the record has been enabled or not
1885 * @rec: the record to test
1886 * @enable: set to 1 to check if enabled, 0 if it is disabled
1888 * The arch code may need to test if a record is already set to
1889 * tracing to determine how to modify the function code that it
1892 int ftrace_test_record(struct dyn_ftrace *rec, int enable)
1894 return ftrace_check_record(rec, enable, 0);
1897 static struct ftrace_ops *
1898 ftrace_find_tramp_ops_curr(struct dyn_ftrace *rec)
1900 struct ftrace_ops *op;
1902 /* Removed ops need to be tested first */
1903 if (removed_ops && removed_ops->tramp_hash) {
1904 if (ftrace_lookup_ip(removed_ops->tramp_hash, rec->ip))
1908 do_for_each_ftrace_op(op, ftrace_ops_list) {
1909 if (!op->tramp_hash)
1912 if (ftrace_lookup_ip(op->tramp_hash, rec->ip))
1915 } while_for_each_ftrace_op(op);
1920 static struct ftrace_ops *
1921 ftrace_find_tramp_ops_new(struct dyn_ftrace *rec)
1923 struct ftrace_ops *op;
1925 do_for_each_ftrace_op(op, ftrace_ops_list) {
1926 /* pass rec in as regs to have non-NULL val */
1927 if (ftrace_ops_test(op, rec->ip, rec))
1929 } while_for_each_ftrace_op(op);
1935 * ftrace_get_addr_new - Get the call address to set to
1936 * @rec: The ftrace record descriptor
1938 * If the record has the FTRACE_FL_REGS set, that means that it
1939 * wants to convert to a callback that saves all regs. If FTRACE_FL_REGS
1940 * is not not set, then it wants to convert to the normal callback.
1942 * Returns the address of the trampoline to set to
1944 unsigned long ftrace_get_addr_new(struct dyn_ftrace *rec)
1946 struct ftrace_ops *ops;
1948 /* Trampolines take precedence over regs */
1949 if (rec->flags & FTRACE_FL_TRAMP) {
1950 ops = ftrace_find_tramp_ops_new(rec);
1951 if (FTRACE_WARN_ON(!ops || !ops->trampoline)) {
1952 pr_warn("Bad trampoline accounting at: %p (%pS) (%lx)\n",
1953 (void *)rec->ip, (void *)rec->ip, rec->flags);
1954 /* Ftrace is shutting down, return anything */
1955 return (unsigned long)FTRACE_ADDR;
1957 return ops->trampoline;
1960 if (rec->flags & FTRACE_FL_REGS)
1961 return (unsigned long)FTRACE_REGS_ADDR;
1963 return (unsigned long)FTRACE_ADDR;
1967 * ftrace_get_addr_curr - Get the call address that is already there
1968 * @rec: The ftrace record descriptor
1970 * The FTRACE_FL_REGS_EN is set when the record already points to
1971 * a function that saves all the regs. Basically the '_EN' version
1972 * represents the current state of the function.
1974 * Returns the address of the trampoline that is currently being called
1976 unsigned long ftrace_get_addr_curr(struct dyn_ftrace *rec)
1978 struct ftrace_ops *ops;
1980 /* Trampolines take precedence over regs */
1981 if (rec->flags & FTRACE_FL_TRAMP_EN) {
1982 ops = ftrace_find_tramp_ops_curr(rec);
1983 if (FTRACE_WARN_ON(!ops)) {
1984 pr_warning("Bad trampoline accounting at: %p (%pS)\n",
1985 (void *)rec->ip, (void *)rec->ip);
1986 /* Ftrace is shutting down, return anything */
1987 return (unsigned long)FTRACE_ADDR;
1989 return ops->trampoline;
1992 if (rec->flags & FTRACE_FL_REGS_EN)
1993 return (unsigned long)FTRACE_REGS_ADDR;
1995 return (unsigned long)FTRACE_ADDR;
1999 __ftrace_replace_code(struct dyn_ftrace *rec, int enable)
2001 unsigned long ftrace_old_addr;
2002 unsigned long ftrace_addr;
2005 ftrace_addr = ftrace_get_addr_new(rec);
2007 /* This needs to be done before we call ftrace_update_record */
2008 ftrace_old_addr = ftrace_get_addr_curr(rec);
2010 ret = ftrace_update_record(rec, enable);
2013 case FTRACE_UPDATE_IGNORE:
2016 case FTRACE_UPDATE_MAKE_CALL:
2017 return ftrace_make_call(rec, ftrace_addr);
2019 case FTRACE_UPDATE_MAKE_NOP:
2020 return ftrace_make_nop(NULL, rec, ftrace_addr);
2022 case FTRACE_UPDATE_MODIFY_CALL:
2023 return ftrace_modify_call(rec, ftrace_old_addr, ftrace_addr);
2026 return -1; /* unknow ftrace bug */
2029 void __weak ftrace_replace_code(int enable)
2031 struct dyn_ftrace *rec;
2032 struct ftrace_page *pg;
2035 if (unlikely(ftrace_disabled))
2038 do_for_each_ftrace_rec(pg, rec) {
2039 failed = __ftrace_replace_code(rec, enable);
2041 ftrace_bug(failed, rec->ip);
2042 /* Stop processing */
2045 } while_for_each_ftrace_rec();
2048 struct ftrace_rec_iter {
2049 struct ftrace_page *pg;
2054 * ftrace_rec_iter_start, start up iterating over traced functions
2056 * Returns an iterator handle that is used to iterate over all
2057 * the records that represent address locations where functions
2060 * May return NULL if no records are available.
2062 struct ftrace_rec_iter *ftrace_rec_iter_start(void)
2065 * We only use a single iterator.
2066 * Protected by the ftrace_lock mutex.
2068 static struct ftrace_rec_iter ftrace_rec_iter;
2069 struct ftrace_rec_iter *iter = &ftrace_rec_iter;
2071 iter->pg = ftrace_pages_start;
2074 /* Could have empty pages */
2075 while (iter->pg && !iter->pg->index)
2076 iter->pg = iter->pg->next;
2085 * ftrace_rec_iter_next, get the next record to process.
2086 * @iter: The handle to the iterator.
2088 * Returns the next iterator after the given iterator @iter.
2090 struct ftrace_rec_iter *ftrace_rec_iter_next(struct ftrace_rec_iter *iter)
2094 if (iter->index >= iter->pg->index) {
2095 iter->pg = iter->pg->next;
2098 /* Could have empty pages */
2099 while (iter->pg && !iter->pg->index)
2100 iter->pg = iter->pg->next;
2110 * ftrace_rec_iter_record, get the record at the iterator location
2111 * @iter: The current iterator location
2113 * Returns the record that the current @iter is at.
2115 struct dyn_ftrace *ftrace_rec_iter_record(struct ftrace_rec_iter *iter)
2117 return &iter->pg->records[iter->index];
2121 ftrace_code_disable(struct module *mod, struct dyn_ftrace *rec)
2128 if (unlikely(ftrace_disabled))
2131 ret = ftrace_make_nop(mod, rec, MCOUNT_ADDR);
2133 ftrace_bug(ret, ip);
2140 * archs can override this function if they must do something
2141 * before the modifying code is performed.
2143 int __weak ftrace_arch_code_modify_prepare(void)
2149 * archs can override this function if they must do something
2150 * after the modifying code is performed.
2152 int __weak ftrace_arch_code_modify_post_process(void)
2157 void ftrace_modify_all_code(int command)
2159 int update = command & FTRACE_UPDATE_TRACE_FUNC;
2163 * If the ftrace_caller calls a ftrace_ops func directly,
2164 * we need to make sure that it only traces functions it
2165 * expects to trace. When doing the switch of functions,
2166 * we need to update to the ftrace_ops_list_func first
2167 * before the transition between old and new calls are set,
2168 * as the ftrace_ops_list_func will check the ops hashes
2169 * to make sure the ops are having the right functions
2173 err = ftrace_update_ftrace_func(ftrace_ops_list_func);
2174 if (FTRACE_WARN_ON(err))
2178 if (command & FTRACE_UPDATE_CALLS)
2179 ftrace_replace_code(1);
2180 else if (command & FTRACE_DISABLE_CALLS)
2181 ftrace_replace_code(0);
2183 if (update && ftrace_trace_function != ftrace_ops_list_func) {
2184 function_trace_op = set_function_trace_op;
2186 /* If irqs are disabled, we are in stop machine */
2187 if (!irqs_disabled())
2188 smp_call_function(ftrace_sync_ipi, NULL, 1);
2189 err = ftrace_update_ftrace_func(ftrace_trace_function);
2190 if (FTRACE_WARN_ON(err))
2194 if (command & FTRACE_START_FUNC_RET)
2195 err = ftrace_enable_ftrace_graph_caller();
2196 else if (command & FTRACE_STOP_FUNC_RET)
2197 err = ftrace_disable_ftrace_graph_caller();
2198 FTRACE_WARN_ON(err);
2201 static int __ftrace_modify_code(void *data)
2203 int *command = data;
2205 ftrace_modify_all_code(*command);
2211 * ftrace_run_stop_machine, go back to the stop machine method
2212 * @command: The command to tell ftrace what to do
2214 * If an arch needs to fall back to the stop machine method, the
2215 * it can call this function.
2217 void ftrace_run_stop_machine(int command)
2219 stop_machine(__ftrace_modify_code, &command, NULL);
2223 * arch_ftrace_update_code, modify the code to trace or not trace
2224 * @command: The command that needs to be done
2226 * Archs can override this function if it does not need to
2227 * run stop_machine() to modify code.
2229 void __weak arch_ftrace_update_code(int command)
2231 ftrace_run_stop_machine(command);
2234 static int ftrace_save_ops_tramp_hash(struct ftrace_ops *ops)
2236 struct ftrace_page *pg;
2237 struct dyn_ftrace *rec;
2241 size = ops->nr_trampolines;
2244 * Make the hash size about 1/2 the # found
2246 for (size /= 2; size; size >>= 1)
2249 ops->tramp_hash = alloc_ftrace_hash(bits);
2251 * TODO: a failed allocation is going to screw up
2252 * the accounting of what needs to be modified
2253 * and not. For now, we kill ftrace if we fail
2254 * to allocate here. But there are ways around this,
2255 * but that will take a little more work.
2257 if (!ops->tramp_hash)
2260 do_for_each_ftrace_rec(pg, rec) {
2261 if (ftrace_rec_count(rec) == 1 &&
2262 ftrace_ops_test(ops, rec->ip, rec)) {
2265 * If another ops adds to a rec, the rec will
2266 * lose its trampoline and never get it back
2267 * until all ops are off of it.
2269 if (!(rec->flags & FTRACE_FL_TRAMP))
2272 /* This record had better have a trampoline */
2273 if (FTRACE_WARN_ON(!(rec->flags & FTRACE_FL_TRAMP_EN)))
2276 ret = add_hash_entry(ops->tramp_hash, rec->ip);
2280 } while_for_each_ftrace_rec();
2282 /* The number of recs in the hash must match nr_trampolines */
2283 if (FTRACE_WARN_ON(ops->tramp_hash->count != ops->nr_trampolines))
2284 pr_warn("count=%ld trampolines=%d\n",
2285 ops->tramp_hash->count,
2286 ops->nr_trampolines);
2291 static int ftrace_save_tramp_hashes(void)
2293 struct ftrace_ops *op;
2297 * Now that any trampoline is being used, we need to save the
2298 * hashes for the ops that have them. This allows the mapping
2299 * back from the record to the ops that has the trampoline to
2300 * know what code is being replaced. Modifying code must always
2301 * verify what it is changing.
2303 do_for_each_ftrace_op(op, ftrace_ops_list) {
2305 /* The tramp_hash is recreated each time. */
2306 free_ftrace_hash(op->tramp_hash);
2307 op->tramp_hash = NULL;
2309 if (op->nr_trampolines) {
2310 ret = ftrace_save_ops_tramp_hash(op);
2315 } while_for_each_ftrace_op(op);
2320 static void ftrace_run_update_code(int command)
2324 ret = ftrace_arch_code_modify_prepare();
2325 FTRACE_WARN_ON(ret);
2330 * By default we use stop_machine() to modify the code.
2331 * But archs can do what ever they want as long as it
2332 * is safe. The stop_machine() is the safest, but also
2333 * produces the most overhead.
2335 arch_ftrace_update_code(command);
2337 ret = ftrace_arch_code_modify_post_process();
2338 FTRACE_WARN_ON(ret);
2340 ret = ftrace_save_tramp_hashes();
2341 FTRACE_WARN_ON(ret);
2344 static ftrace_func_t saved_ftrace_func;
2345 static int ftrace_start_up;
2347 static void control_ops_free(struct ftrace_ops *ops)
2349 free_percpu(ops->disabled);
2352 static void ftrace_startup_enable(int command)
2354 if (saved_ftrace_func != ftrace_trace_function) {
2355 saved_ftrace_func = ftrace_trace_function;
2356 command |= FTRACE_UPDATE_TRACE_FUNC;
2359 if (!command || !ftrace_enabled)
2362 ftrace_run_update_code(command);
2365 static int ftrace_startup(struct ftrace_ops *ops, int command)
2369 if (unlikely(ftrace_disabled))
2372 ret = __register_ftrace_function(ops);
2377 command |= FTRACE_UPDATE_CALLS;
2379 ops->flags |= FTRACE_OPS_FL_ENABLED;
2381 ftrace_hash_rec_enable(ops, 1);
2383 ftrace_startup_enable(command);
2388 static int ftrace_shutdown(struct ftrace_ops *ops, int command)
2392 if (unlikely(ftrace_disabled))
2395 ret = __unregister_ftrace_function(ops);
2401 * Just warn in case of unbalance, no need to kill ftrace, it's not
2402 * critical but the ftrace_call callers may be never nopped again after
2403 * further ftrace uses.
2405 WARN_ON_ONCE(ftrace_start_up < 0);
2407 ftrace_hash_rec_disable(ops, 1);
2409 ops->flags &= ~FTRACE_OPS_FL_ENABLED;
2411 command |= FTRACE_UPDATE_CALLS;
2413 if (saved_ftrace_func != ftrace_trace_function) {
2414 saved_ftrace_func = ftrace_trace_function;
2415 command |= FTRACE_UPDATE_TRACE_FUNC;
2418 if (!command || !ftrace_enabled) {
2420 * If these are control ops, they still need their
2421 * per_cpu field freed. Since, function tracing is
2422 * not currently active, we can just free them
2423 * without synchronizing all CPUs.
2425 if (ops->flags & FTRACE_OPS_FL_CONTROL)
2426 control_ops_free(ops);
2431 * If the ops uses a trampoline, then it needs to be
2432 * tested first on update.
2436 ftrace_run_update_code(command);
2441 * Dynamic ops may be freed, we must make sure that all
2442 * callers are done before leaving this function.
2443 * The same goes for freeing the per_cpu data of the control
2446 * Again, normal synchronize_sched() is not good enough.
2447 * We need to do a hard force of sched synchronization.
2448 * This is because we use preempt_disable() to do RCU, but
2449 * the function tracers can be called where RCU is not watching
2450 * (like before user_exit()). We can not rely on the RCU
2451 * infrastructure to do the synchronization, thus we must do it
2454 if (ops->flags & (FTRACE_OPS_FL_DYNAMIC | FTRACE_OPS_FL_CONTROL)) {
2455 schedule_on_each_cpu(ftrace_sync);
2457 if (ops->flags & FTRACE_OPS_FL_CONTROL)
2458 control_ops_free(ops);
2464 static void ftrace_startup_sysctl(void)
2466 if (unlikely(ftrace_disabled))
2469 /* Force update next time */
2470 saved_ftrace_func = NULL;
2471 /* ftrace_start_up is true if we want ftrace running */
2472 if (ftrace_start_up)
2473 ftrace_run_update_code(FTRACE_UPDATE_CALLS);
2476 static void ftrace_shutdown_sysctl(void)
2478 if (unlikely(ftrace_disabled))
2481 /* ftrace_start_up is true if ftrace is running */
2482 if (ftrace_start_up)
2483 ftrace_run_update_code(FTRACE_DISABLE_CALLS);
2486 static cycle_t ftrace_update_time;
2487 unsigned long ftrace_update_tot_cnt;
2489 static inline int ops_traces_mod(struct ftrace_ops *ops)
2492 * Filter_hash being empty will default to trace module.
2493 * But notrace hash requires a test of individual module functions.
2495 return ftrace_hash_empty(ops->func_hash->filter_hash) &&
2496 ftrace_hash_empty(ops->func_hash->notrace_hash);
2500 * Check if the current ops references the record.
2502 * If the ops traces all functions, then it was already accounted for.
2503 * If the ops does not trace the current record function, skip it.
2504 * If the ops ignores the function via notrace filter, skip it.
2507 ops_references_rec(struct ftrace_ops *ops, struct dyn_ftrace *rec)
2509 /* If ops isn't enabled, ignore it */
2510 if (!(ops->flags & FTRACE_OPS_FL_ENABLED))
2513 /* If ops traces all mods, we already accounted for it */
2514 if (ops_traces_mod(ops))
2517 /* The function must be in the filter */
2518 if (!ftrace_hash_empty(ops->func_hash->filter_hash) &&
2519 !ftrace_lookup_ip(ops->func_hash->filter_hash, rec->ip))
2522 /* If in notrace hash, we ignore it too */
2523 if (ftrace_lookup_ip(ops->func_hash->notrace_hash, rec->ip))
2529 static int referenced_filters(struct dyn_ftrace *rec)
2531 struct ftrace_ops *ops;
2534 for (ops = ftrace_ops_list; ops != &ftrace_list_end; ops = ops->next) {
2535 if (ops_references_rec(ops, rec))
2542 static int ftrace_update_code(struct module *mod, struct ftrace_page *new_pgs)
2544 struct ftrace_page *pg;
2545 struct dyn_ftrace *p;
2546 cycle_t start, stop;
2547 unsigned long update_cnt = 0;
2548 unsigned long ref = 0;
2553 * When adding a module, we need to check if tracers are
2554 * currently enabled and if they are set to trace all functions.
2555 * If they are, we need to enable the module functions as well
2556 * as update the reference counts for those function records.
2559 struct ftrace_ops *ops;
2561 for (ops = ftrace_ops_list;
2562 ops != &ftrace_list_end; ops = ops->next) {
2563 if (ops->flags & FTRACE_OPS_FL_ENABLED) {
2564 if (ops_traces_mod(ops))
2572 start = ftrace_now(raw_smp_processor_id());
2574 for (pg = new_pgs; pg; pg = pg->next) {
2576 for (i = 0; i < pg->index; i++) {
2579 /* If something went wrong, bail without enabling anything */
2580 if (unlikely(ftrace_disabled))
2583 p = &pg->records[i];
2585 cnt += referenced_filters(p);
2589 * Do the initial record conversion from mcount jump
2590 * to the NOP instructions.
2592 if (!ftrace_code_disable(mod, p))
2598 * If the tracing is enabled, go ahead and enable the record.
2600 * The reason not to enable the record immediatelly is the
2601 * inherent check of ftrace_make_nop/ftrace_make_call for
2602 * correct previous instructions. Making first the NOP
2603 * conversion puts the module to the correct state, thus
2604 * passing the ftrace_make_call check.
2606 if (ftrace_start_up && cnt) {
2607 int failed = __ftrace_replace_code(p, 1);
2609 ftrace_bug(failed, p->ip);
2614 stop = ftrace_now(raw_smp_processor_id());
2615 ftrace_update_time = stop - start;
2616 ftrace_update_tot_cnt += update_cnt;
2621 static int ftrace_allocate_records(struct ftrace_page *pg, int count)
2626 if (WARN_ON(!count))
2629 order = get_count_order(DIV_ROUND_UP(count, ENTRIES_PER_PAGE));
2632 * We want to fill as much as possible. No more than a page
2635 while ((PAGE_SIZE << order) / ENTRY_SIZE >= count + ENTRIES_PER_PAGE)
2639 pg->records = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, order);
2642 /* if we can't allocate this size, try something smaller */
2649 cnt = (PAGE_SIZE << order) / ENTRY_SIZE;
2658 static struct ftrace_page *
2659 ftrace_allocate_pages(unsigned long num_to_init)
2661 struct ftrace_page *start_pg;
2662 struct ftrace_page *pg;
2669 start_pg = pg = kzalloc(sizeof(*pg), GFP_KERNEL);
2674 * Try to allocate as much as possible in one continues
2675 * location that fills in all of the space. We want to
2676 * waste as little space as possible.
2679 cnt = ftrace_allocate_records(pg, num_to_init);
2687 pg->next = kzalloc(sizeof(*pg), GFP_KERNEL);
2699 order = get_count_order(pg->size / ENTRIES_PER_PAGE);
2700 free_pages((unsigned long)pg->records, order);
2701 start_pg = pg->next;
2705 pr_info("ftrace: FAILED to allocate memory for functions\n");
2709 #define FTRACE_BUFF_MAX (KSYM_SYMBOL_LEN+4) /* room for wildcards */
2711 struct ftrace_iterator {
2714 struct ftrace_page *pg;
2715 struct dyn_ftrace *func;
2716 struct ftrace_func_probe *probe;
2717 struct trace_parser parser;
2718 struct ftrace_hash *hash;
2719 struct ftrace_ops *ops;
2726 t_hash_next(struct seq_file *m, loff_t *pos)
2728 struct ftrace_iterator *iter = m->private;
2729 struct hlist_node *hnd = NULL;
2730 struct hlist_head *hhd;
2736 hnd = &iter->probe->node;
2738 if (iter->hidx >= FTRACE_FUNC_HASHSIZE)
2741 hhd = &ftrace_func_hash[iter->hidx];
2743 if (hlist_empty(hhd)) {
2759 if (WARN_ON_ONCE(!hnd))
2762 iter->probe = hlist_entry(hnd, struct ftrace_func_probe, node);
2767 static void *t_hash_start(struct seq_file *m, loff_t *pos)
2769 struct ftrace_iterator *iter = m->private;
2773 if (!(iter->flags & FTRACE_ITER_DO_HASH))
2776 if (iter->func_pos > *pos)
2780 for (l = 0; l <= (*pos - iter->func_pos); ) {
2781 p = t_hash_next(m, &l);
2788 /* Only set this if we have an item */
2789 iter->flags |= FTRACE_ITER_HASH;
2795 t_hash_show(struct seq_file *m, struct ftrace_iterator *iter)
2797 struct ftrace_func_probe *rec;
2800 if (WARN_ON_ONCE(!rec))
2803 if (rec->ops->print)
2804 return rec->ops->print(m, rec->ip, rec->ops, rec->data);
2806 seq_printf(m, "%ps:%ps", (void *)rec->ip, (void *)rec->ops->func);
2809 seq_printf(m, ":%p", rec->data);
2816 t_next(struct seq_file *m, void *v, loff_t *pos)
2818 struct ftrace_iterator *iter = m->private;
2819 struct ftrace_ops *ops = iter->ops;
2820 struct dyn_ftrace *rec = NULL;
2822 if (unlikely(ftrace_disabled))
2825 if (iter->flags & FTRACE_ITER_HASH)
2826 return t_hash_next(m, pos);
2829 iter->pos = iter->func_pos = *pos;
2831 if (iter->flags & FTRACE_ITER_PRINTALL)
2832 return t_hash_start(m, pos);
2835 if (iter->idx >= iter->pg->index) {
2836 if (iter->pg->next) {
2837 iter->pg = iter->pg->next;
2842 rec = &iter->pg->records[iter->idx++];
2843 if (((iter->flags & FTRACE_ITER_FILTER) &&
2844 !(ftrace_lookup_ip(ops->func_hash->filter_hash, rec->ip))) ||
2846 ((iter->flags & FTRACE_ITER_NOTRACE) &&
2847 !ftrace_lookup_ip(ops->func_hash->notrace_hash, rec->ip)) ||
2849 ((iter->flags & FTRACE_ITER_ENABLED) &&
2850 !(rec->flags & FTRACE_FL_ENABLED))) {
2858 return t_hash_start(m, pos);
2865 static void reset_iter_read(struct ftrace_iterator *iter)
2869 iter->flags &= ~(FTRACE_ITER_PRINTALL | FTRACE_ITER_HASH);
2872 static void *t_start(struct seq_file *m, loff_t *pos)
2874 struct ftrace_iterator *iter = m->private;
2875 struct ftrace_ops *ops = iter->ops;
2879 mutex_lock(&ftrace_lock);
2881 if (unlikely(ftrace_disabled))
2885 * If an lseek was done, then reset and start from beginning.
2887 if (*pos < iter->pos)
2888 reset_iter_read(iter);
2891 * For set_ftrace_filter reading, if we have the filter
2892 * off, we can short cut and just print out that all
2893 * functions are enabled.
2895 if ((iter->flags & FTRACE_ITER_FILTER &&
2896 ftrace_hash_empty(ops->func_hash->filter_hash)) ||
2897 (iter->flags & FTRACE_ITER_NOTRACE &&
2898 ftrace_hash_empty(ops->func_hash->notrace_hash))) {
2900 return t_hash_start(m, pos);
2901 iter->flags |= FTRACE_ITER_PRINTALL;
2902 /* reset in case of seek/pread */
2903 iter->flags &= ~FTRACE_ITER_HASH;
2907 if (iter->flags & FTRACE_ITER_HASH)
2908 return t_hash_start(m, pos);
2911 * Unfortunately, we need to restart at ftrace_pages_start
2912 * every time we let go of the ftrace_mutex. This is because
2913 * those pointers can change without the lock.
2915 iter->pg = ftrace_pages_start;
2917 for (l = 0; l <= *pos; ) {
2918 p = t_next(m, p, &l);
2924 return t_hash_start(m, pos);
2929 static void t_stop(struct seq_file *m, void *p)
2931 mutex_unlock(&ftrace_lock);
2934 static int t_show(struct seq_file *m, void *v)
2936 struct ftrace_iterator *iter = m->private;
2937 struct dyn_ftrace *rec;
2939 if (iter->flags & FTRACE_ITER_HASH)
2940 return t_hash_show(m, iter);
2942 if (iter->flags & FTRACE_ITER_PRINTALL) {
2943 if (iter->flags & FTRACE_ITER_NOTRACE)
2944 seq_printf(m, "#### no functions disabled ####\n");
2946 seq_printf(m, "#### all functions enabled ####\n");
2955 seq_printf(m, "%ps", (void *)rec->ip);
2956 if (iter->flags & FTRACE_ITER_ENABLED) {
2957 seq_printf(m, " (%ld)%s",
2958 ftrace_rec_count(rec),
2959 rec->flags & FTRACE_FL_REGS ? " R" : " ");
2960 if (rec->flags & FTRACE_FL_TRAMP_EN) {
2961 struct ftrace_ops *ops;
2963 ops = ftrace_find_tramp_ops_curr(rec);
2964 if (ops && ops->trampoline)
2965 seq_printf(m, "\ttramp: %pS",
2966 (void *)ops->trampoline);
2968 seq_printf(m, "\ttramp: ERROR!");
2972 seq_printf(m, "\n");
2977 static const struct seq_operations show_ftrace_seq_ops = {
2985 ftrace_avail_open(struct inode *inode, struct file *file)
2987 struct ftrace_iterator *iter;
2989 if (unlikely(ftrace_disabled))
2992 iter = __seq_open_private(file, &show_ftrace_seq_ops, sizeof(*iter));
2994 iter->pg = ftrace_pages_start;
2995 iter->ops = &global_ops;
2998 return iter ? 0 : -ENOMEM;
3002 ftrace_enabled_open(struct inode *inode, struct file *file)
3004 struct ftrace_iterator *iter;
3006 if (unlikely(ftrace_disabled))
3009 iter = __seq_open_private(file, &show_ftrace_seq_ops, sizeof(*iter));
3011 iter->pg = ftrace_pages_start;
3012 iter->flags = FTRACE_ITER_ENABLED;
3013 iter->ops = &global_ops;
3016 return iter ? 0 : -ENOMEM;
3020 * ftrace_regex_open - initialize function tracer filter files
3021 * @ops: The ftrace_ops that hold the hash filters
3022 * @flag: The type of filter to process
3023 * @inode: The inode, usually passed in to your open routine
3024 * @file: The file, usually passed in to your open routine
3026 * ftrace_regex_open() initializes the filter files for the
3027 * @ops. Depending on @flag it may process the filter hash or
3028 * the notrace hash of @ops. With this called from the open
3029 * routine, you can use ftrace_filter_write() for the write
3030 * routine if @flag has FTRACE_ITER_FILTER set, or
3031 * ftrace_notrace_write() if @flag has FTRACE_ITER_NOTRACE set.
3032 * tracing_lseek() should be used as the lseek routine, and
3033 * release must call ftrace_regex_release().
3036 ftrace_regex_open(struct ftrace_ops *ops, int flag,
3037 struct inode *inode, struct file *file)
3039 struct ftrace_iterator *iter;
3040 struct ftrace_hash *hash;
3043 ftrace_ops_init(ops);
3045 if (unlikely(ftrace_disabled))
3048 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
3052 if (trace_parser_get_init(&iter->parser, FTRACE_BUFF_MAX)) {
3060 mutex_lock(&ops->func_hash->regex_lock);
3062 if (flag & FTRACE_ITER_NOTRACE)
3063 hash = ops->func_hash->notrace_hash;
3065 hash = ops->func_hash->filter_hash;
3067 if (file->f_mode & FMODE_WRITE) {
3068 const int size_bits = FTRACE_HASH_DEFAULT_BITS;
3070 if (file->f_flags & O_TRUNC)
3071 iter->hash = alloc_ftrace_hash(size_bits);
3073 iter->hash = alloc_and_copy_ftrace_hash(size_bits, hash);
3076 trace_parser_put(&iter->parser);
3083 if (file->f_mode & FMODE_READ) {
3084 iter->pg = ftrace_pages_start;
3086 ret = seq_open(file, &show_ftrace_seq_ops);
3088 struct seq_file *m = file->private_data;
3092 free_ftrace_hash(iter->hash);
3093 trace_parser_put(&iter->parser);
3097 file->private_data = iter;
3100 mutex_unlock(&ops->func_hash->regex_lock);
3106 ftrace_filter_open(struct inode *inode, struct file *file)
3108 struct ftrace_ops *ops = inode->i_private;
3110 return ftrace_regex_open(ops,
3111 FTRACE_ITER_FILTER | FTRACE_ITER_DO_HASH,
3116 ftrace_notrace_open(struct inode *inode, struct file *file)
3118 struct ftrace_ops *ops = inode->i_private;
3120 return ftrace_regex_open(ops, FTRACE_ITER_NOTRACE,
3124 static int ftrace_match(char *str, char *regex, int len, int type)
3131 if (strcmp(str, regex) == 0)
3134 case MATCH_FRONT_ONLY:
3135 if (strncmp(str, regex, len) == 0)
3138 case MATCH_MIDDLE_ONLY:
3139 if (strstr(str, regex))
3142 case MATCH_END_ONLY:
3144 if (slen >= len && memcmp(str + slen - len, regex, len) == 0)
3153 enter_record(struct ftrace_hash *hash, struct dyn_ftrace *rec, int not)
3155 struct ftrace_func_entry *entry;
3158 entry = ftrace_lookup_ip(hash, rec->ip);
3160 /* Do nothing if it doesn't exist */
3164 free_hash_entry(hash, entry);
3166 /* Do nothing if it exists */
3170 ret = add_hash_entry(hash, rec->ip);
3176 ftrace_match_record(struct dyn_ftrace *rec, char *mod,
3177 char *regex, int len, int type)
3179 char str[KSYM_SYMBOL_LEN];
3182 kallsyms_lookup(rec->ip, NULL, NULL, &modname, str);
3185 /* module lookup requires matching the module */
3186 if (!modname || strcmp(modname, mod))
3189 /* blank search means to match all funcs in the mod */
3194 return ftrace_match(str, regex, len, type);
3198 match_records(struct ftrace_hash *hash, char *buff,
3199 int len, char *mod, int not)
3201 unsigned search_len = 0;
3202 struct ftrace_page *pg;
3203 struct dyn_ftrace *rec;
3204 int type = MATCH_FULL;
3205 char *search = buff;
3210 type = filter_parse_regex(buff, len, &search, ¬);
3211 search_len = strlen(search);
3214 mutex_lock(&ftrace_lock);
3216 if (unlikely(ftrace_disabled))
3219 do_for_each_ftrace_rec(pg, rec) {
3220 if (ftrace_match_record(rec, mod, search, search_len, type)) {
3221 ret = enter_record(hash, rec, not);
3228 } while_for_each_ftrace_rec();
3230 mutex_unlock(&ftrace_lock);
3236 ftrace_match_records(struct ftrace_hash *hash, char *buff, int len)
3238 return match_records(hash, buff, len, NULL, 0);
3242 ftrace_match_module_records(struct ftrace_hash *hash, char *buff, char *mod)
3246 /* blank or '*' mean the same */
3247 if (strcmp(buff, "*") == 0)
3250 /* handle the case of 'dont filter this module' */
3251 if (strcmp(buff, "!") == 0 || strcmp(buff, "!*") == 0) {
3256 return match_records(hash, buff, strlen(buff), mod, not);
3260 * We register the module command as a template to show others how
3261 * to register the a command as well.
3265 ftrace_mod_callback(struct ftrace_hash *hash,
3266 char *func, char *cmd, char *param, int enable)
3272 * cmd == 'mod' because we only registered this func
3273 * for the 'mod' ftrace_func_command.
3274 * But if you register one func with multiple commands,
3275 * you can tell which command was used by the cmd
3279 /* we must have a module name */
3283 mod = strsep(¶m, ":");
3287 ret = ftrace_match_module_records(hash, func, mod);
3296 static struct ftrace_func_command ftrace_mod_cmd = {
3298 .func = ftrace_mod_callback,
3301 static int __init ftrace_mod_cmd_init(void)
3303 return register_ftrace_command(&ftrace_mod_cmd);
3305 core_initcall(ftrace_mod_cmd_init);
3307 static void function_trace_probe_call(unsigned long ip, unsigned long parent_ip,
3308 struct ftrace_ops *op, struct pt_regs *pt_regs)
3310 struct ftrace_func_probe *entry;
3311 struct hlist_head *hhd;
3314 key = hash_long(ip, FTRACE_HASH_BITS);
3316 hhd = &ftrace_func_hash[key];
3318 if (hlist_empty(hhd))
3322 * Disable preemption for these calls to prevent a RCU grace
3323 * period. This syncs the hash iteration and freeing of items
3324 * on the hash. rcu_read_lock is too dangerous here.
3326 preempt_disable_notrace();
3327 hlist_for_each_entry_rcu_notrace(entry, hhd, node) {
3328 if (entry->ip == ip)
3329 entry->ops->func(ip, parent_ip, &entry->data);
3331 preempt_enable_notrace();
3334 static struct ftrace_ops trace_probe_ops __read_mostly =
3336 .func = function_trace_probe_call,
3337 .flags = FTRACE_OPS_FL_INITIALIZED,
3338 INIT_OPS_HASH(trace_probe_ops)
3341 static int ftrace_probe_registered;
3343 static void __enable_ftrace_function_probe(void)
3348 if (ftrace_probe_registered) {
3349 /* still need to update the function call sites */
3351 ftrace_run_update_code(FTRACE_UPDATE_CALLS);
3355 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
3356 struct hlist_head *hhd = &ftrace_func_hash[i];
3360 /* Nothing registered? */
3361 if (i == FTRACE_FUNC_HASHSIZE)
3364 ret = ftrace_startup(&trace_probe_ops, 0);
3366 ftrace_probe_registered = 1;
3369 static void __disable_ftrace_function_probe(void)
3373 if (!ftrace_probe_registered)
3376 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
3377 struct hlist_head *hhd = &ftrace_func_hash[i];
3382 /* no more funcs left */
3383 ftrace_shutdown(&trace_probe_ops, 0);
3385 ftrace_probe_registered = 0;
3389 static void ftrace_free_entry(struct ftrace_func_probe *entry)
3391 if (entry->ops->free)
3392 entry->ops->free(entry->ops, entry->ip, &entry->data);
3397 register_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
3400 struct ftrace_func_probe *entry;
3401 struct ftrace_hash **orig_hash = &trace_probe_ops.func_hash->filter_hash;
3402 struct ftrace_hash *hash;
3403 struct ftrace_page *pg;
3404 struct dyn_ftrace *rec;
3411 type = filter_parse_regex(glob, strlen(glob), &search, ¬);
3412 len = strlen(search);
3414 /* we do not support '!' for function probes */
3418 mutex_lock(&trace_probe_ops.func_hash->regex_lock);
3420 hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, *orig_hash);
3426 if (unlikely(ftrace_disabled)) {
3431 mutex_lock(&ftrace_lock);
3433 do_for_each_ftrace_rec(pg, rec) {
3435 if (!ftrace_match_record(rec, NULL, search, len, type))
3438 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
3440 /* If we did not process any, then return error */
3451 * The caller might want to do something special
3452 * for each function we find. We call the callback
3453 * to give the caller an opportunity to do so.
3456 if (ops->init(ops, rec->ip, &entry->data) < 0) {
3457 /* caller does not like this func */
3463 ret = enter_record(hash, rec, 0);
3471 entry->ip = rec->ip;
3473 key = hash_long(entry->ip, FTRACE_HASH_BITS);
3474 hlist_add_head_rcu(&entry->node, &ftrace_func_hash[key]);
3476 } while_for_each_ftrace_rec();
3478 ret = ftrace_hash_move(&trace_probe_ops, 1, orig_hash, hash);
3482 __enable_ftrace_function_probe();
3485 mutex_unlock(&ftrace_lock);
3487 mutex_unlock(&trace_probe_ops.func_hash->regex_lock);
3488 free_ftrace_hash(hash);
3494 PROBE_TEST_FUNC = 1,
3499 __unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
3500 void *data, int flags)
3502 struct ftrace_func_entry *rec_entry;
3503 struct ftrace_func_probe *entry;
3504 struct ftrace_func_probe *p;
3505 struct ftrace_hash **orig_hash = &trace_probe_ops.func_hash->filter_hash;
3506 struct list_head free_list;
3507 struct ftrace_hash *hash;
3508 struct hlist_node *tmp;
3509 char str[KSYM_SYMBOL_LEN];
3510 int type = MATCH_FULL;
3514 if (glob && (strcmp(glob, "*") == 0 || !strlen(glob)))
3519 type = filter_parse_regex(glob, strlen(glob), &search, ¬);
3520 len = strlen(search);
3522 /* we do not support '!' for function probes */
3527 mutex_lock(&trace_probe_ops.func_hash->regex_lock);
3529 hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, *orig_hash);
3531 /* Hmm, should report this somehow */
3534 INIT_LIST_HEAD(&free_list);
3536 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
3537 struct hlist_head *hhd = &ftrace_func_hash[i];
3539 hlist_for_each_entry_safe(entry, tmp, hhd, node) {
3541 /* break up if statements for readability */
3542 if ((flags & PROBE_TEST_FUNC) && entry->ops != ops)
3545 if ((flags & PROBE_TEST_DATA) && entry->data != data)
3548 /* do this last, since it is the most expensive */
3550 kallsyms_lookup(entry->ip, NULL, NULL,
3552 if (!ftrace_match(str, glob, len, type))
3556 rec_entry = ftrace_lookup_ip(hash, entry->ip);
3557 /* It is possible more than one entry had this ip */
3559 free_hash_entry(hash, rec_entry);
3561 hlist_del_rcu(&entry->node);
3562 list_add(&entry->free_list, &free_list);
3565 mutex_lock(&ftrace_lock);
3566 __disable_ftrace_function_probe();
3568 * Remove after the disable is called. Otherwise, if the last
3569 * probe is removed, a null hash means *all enabled*.
3571 ftrace_hash_move(&trace_probe_ops, 1, orig_hash, hash);
3572 synchronize_sched();
3573 list_for_each_entry_safe(entry, p, &free_list, free_list) {
3574 list_del(&entry->free_list);
3575 ftrace_free_entry(entry);
3577 mutex_unlock(&ftrace_lock);
3580 mutex_unlock(&trace_probe_ops.func_hash->regex_lock);
3581 free_ftrace_hash(hash);
3585 unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
3588 __unregister_ftrace_function_probe(glob, ops, data,
3589 PROBE_TEST_FUNC | PROBE_TEST_DATA);
3593 unregister_ftrace_function_probe_func(char *glob, struct ftrace_probe_ops *ops)
3595 __unregister_ftrace_function_probe(glob, ops, NULL, PROBE_TEST_FUNC);
3598 void unregister_ftrace_function_probe_all(char *glob)
3600 __unregister_ftrace_function_probe(glob, NULL, NULL, 0);
3603 static LIST_HEAD(ftrace_commands);
3604 static DEFINE_MUTEX(ftrace_cmd_mutex);
3607 * Currently we only register ftrace commands from __init, so mark this
3610 __init int register_ftrace_command(struct ftrace_func_command *cmd)
3612 struct ftrace_func_command *p;
3615 mutex_lock(&ftrace_cmd_mutex);
3616 list_for_each_entry(p, &ftrace_commands, list) {
3617 if (strcmp(cmd->name, p->name) == 0) {
3622 list_add(&cmd->list, &ftrace_commands);
3624 mutex_unlock(&ftrace_cmd_mutex);
3630 * Currently we only unregister ftrace commands from __init, so mark
3633 __init int unregister_ftrace_command(struct ftrace_func_command *cmd)
3635 struct ftrace_func_command *p, *n;
3638 mutex_lock(&ftrace_cmd_mutex);
3639 list_for_each_entry_safe(p, n, &ftrace_commands, list) {
3640 if (strcmp(cmd->name, p->name) == 0) {
3642 list_del_init(&p->list);
3647 mutex_unlock(&ftrace_cmd_mutex);
3652 static int ftrace_process_regex(struct ftrace_hash *hash,
3653 char *buff, int len, int enable)
3655 char *func, *command, *next = buff;
3656 struct ftrace_func_command *p;
3659 func = strsep(&next, ":");
3662 ret = ftrace_match_records(hash, func, len);
3672 command = strsep(&next, ":");
3674 mutex_lock(&ftrace_cmd_mutex);
3675 list_for_each_entry(p, &ftrace_commands, list) {
3676 if (strcmp(p->name, command) == 0) {
3677 ret = p->func(hash, func, command, next, enable);
3682 mutex_unlock(&ftrace_cmd_mutex);
3688 ftrace_regex_write(struct file *file, const char __user *ubuf,
3689 size_t cnt, loff_t *ppos, int enable)
3691 struct ftrace_iterator *iter;
3692 struct trace_parser *parser;
3698 if (file->f_mode & FMODE_READ) {
3699 struct seq_file *m = file->private_data;
3702 iter = file->private_data;
3704 if (unlikely(ftrace_disabled))
3707 /* iter->hash is a local copy, so we don't need regex_lock */
3709 parser = &iter->parser;
3710 read = trace_get_user(parser, ubuf, cnt, ppos);
3712 if (read >= 0 && trace_parser_loaded(parser) &&
3713 !trace_parser_cont(parser)) {
3714 ret = ftrace_process_regex(iter->hash, parser->buffer,
3715 parser->idx, enable);
3716 trace_parser_clear(parser);
3727 ftrace_filter_write(struct file *file, const char __user *ubuf,
3728 size_t cnt, loff_t *ppos)
3730 return ftrace_regex_write(file, ubuf, cnt, ppos, 1);
3734 ftrace_notrace_write(struct file *file, const char __user *ubuf,
3735 size_t cnt, loff_t *ppos)
3737 return ftrace_regex_write(file, ubuf, cnt, ppos, 0);
3741 ftrace_match_addr(struct ftrace_hash *hash, unsigned long ip, int remove)
3743 struct ftrace_func_entry *entry;
3745 if (!ftrace_location(ip))
3749 entry = ftrace_lookup_ip(hash, ip);
3752 free_hash_entry(hash, entry);
3756 return add_hash_entry(hash, ip);
3759 static void ftrace_ops_update_code(struct ftrace_ops *ops)
3761 if (ops->flags & FTRACE_OPS_FL_ENABLED && ftrace_enabled)
3762 ftrace_run_update_code(FTRACE_UPDATE_CALLS);
3766 ftrace_set_hash(struct ftrace_ops *ops, unsigned char *buf, int len,
3767 unsigned long ip, int remove, int reset, int enable)
3769 struct ftrace_hash **orig_hash;
3770 struct ftrace_hash *hash;
3773 if (unlikely(ftrace_disabled))
3776 mutex_lock(&ops->func_hash->regex_lock);
3779 orig_hash = &ops->func_hash->filter_hash;
3781 orig_hash = &ops->func_hash->notrace_hash;
3784 hash = alloc_ftrace_hash(FTRACE_HASH_DEFAULT_BITS);
3786 hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, *orig_hash);
3790 goto out_regex_unlock;
3793 if (buf && !ftrace_match_records(hash, buf, len)) {
3795 goto out_regex_unlock;
3798 ret = ftrace_match_addr(hash, ip, remove);
3800 goto out_regex_unlock;
3803 mutex_lock(&ftrace_lock);
3804 ret = ftrace_hash_move(ops, enable, orig_hash, hash);
3806 ftrace_ops_update_code(ops);
3808 mutex_unlock(&ftrace_lock);
3811 mutex_unlock(&ops->func_hash->regex_lock);
3813 free_ftrace_hash(hash);
3818 ftrace_set_addr(struct ftrace_ops *ops, unsigned long ip, int remove,
3819 int reset, int enable)
3821 return ftrace_set_hash(ops, 0, 0, ip, remove, reset, enable);
3825 * ftrace_set_filter_ip - set a function to filter on in ftrace by address
3826 * @ops - the ops to set the filter with
3827 * @ip - the address to add to or remove from the filter.
3828 * @remove - non zero to remove the ip from the filter
3829 * @reset - non zero to reset all filters before applying this filter.
3831 * Filters denote which functions should be enabled when tracing is enabled
3832 * If @ip is NULL, it failes to update filter.
3834 int ftrace_set_filter_ip(struct ftrace_ops *ops, unsigned long ip,
3835 int remove, int reset)
3837 ftrace_ops_init(ops);
3838 return ftrace_set_addr(ops, ip, remove, reset, 1);
3840 EXPORT_SYMBOL_GPL(ftrace_set_filter_ip);
3843 ftrace_set_regex(struct ftrace_ops *ops, unsigned char *buf, int len,
3844 int reset, int enable)
3846 return ftrace_set_hash(ops, buf, len, 0, 0, reset, enable);
3850 * ftrace_set_filter - set a function to filter on in ftrace
3851 * @ops - the ops to set the filter with
3852 * @buf - the string that holds the function filter text.
3853 * @len - the length of the string.
3854 * @reset - non zero to reset all filters before applying this filter.
3856 * Filters denote which functions should be enabled when tracing is enabled.
3857 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
3859 int ftrace_set_filter(struct ftrace_ops *ops, unsigned char *buf,
3862 ftrace_ops_init(ops);
3863 return ftrace_set_regex(ops, buf, len, reset, 1);
3865 EXPORT_SYMBOL_GPL(ftrace_set_filter);
3868 * ftrace_set_notrace - set a function to not trace in ftrace
3869 * @ops - the ops to set the notrace filter with
3870 * @buf - the string that holds the function notrace text.
3871 * @len - the length of the string.
3872 * @reset - non zero to reset all filters before applying this filter.
3874 * Notrace Filters denote which functions should not be enabled when tracing
3875 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
3878 int ftrace_set_notrace(struct ftrace_ops *ops, unsigned char *buf,
3881 ftrace_ops_init(ops);
3882 return ftrace_set_regex(ops, buf, len, reset, 0);
3884 EXPORT_SYMBOL_GPL(ftrace_set_notrace);
3886 * ftrace_set_global_filter - set a function to filter on with global tracers
3887 * @buf - the string that holds the function filter text.
3888 * @len - the length of the string.
3889 * @reset - non zero to reset all filters before applying this filter.
3891 * Filters denote which functions should be enabled when tracing is enabled.
3892 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
3894 void ftrace_set_global_filter(unsigned char *buf, int len, int reset)
3896 ftrace_set_regex(&global_ops, buf, len, reset, 1);
3898 EXPORT_SYMBOL_GPL(ftrace_set_global_filter);
3901 * ftrace_set_global_notrace - set a function to not trace with global tracers
3902 * @buf - the string that holds the function notrace text.
3903 * @len - the length of the string.
3904 * @reset - non zero to reset all filters before applying this filter.
3906 * Notrace Filters denote which functions should not be enabled when tracing
3907 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
3910 void ftrace_set_global_notrace(unsigned char *buf, int len, int reset)
3912 ftrace_set_regex(&global_ops, buf, len, reset, 0);
3914 EXPORT_SYMBOL_GPL(ftrace_set_global_notrace);
3917 * command line interface to allow users to set filters on boot up.
3919 #define FTRACE_FILTER_SIZE COMMAND_LINE_SIZE
3920 static char ftrace_notrace_buf[FTRACE_FILTER_SIZE] __initdata;
3921 static char ftrace_filter_buf[FTRACE_FILTER_SIZE] __initdata;
3923 /* Used by function selftest to not test if filter is set */
3924 bool ftrace_filter_param __initdata;
3926 static int __init set_ftrace_notrace(char *str)
3928 ftrace_filter_param = true;
3929 strlcpy(ftrace_notrace_buf, str, FTRACE_FILTER_SIZE);
3932 __setup("ftrace_notrace=", set_ftrace_notrace);
3934 static int __init set_ftrace_filter(char *str)
3936 ftrace_filter_param = true;
3937 strlcpy(ftrace_filter_buf, str, FTRACE_FILTER_SIZE);
3940 __setup("ftrace_filter=", set_ftrace_filter);
3942 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
3943 static char ftrace_graph_buf[FTRACE_FILTER_SIZE] __initdata;
3944 static char ftrace_graph_notrace_buf[FTRACE_FILTER_SIZE] __initdata;
3945 static int ftrace_set_func(unsigned long *array, int *idx, int size, char *buffer);
3947 static int __init set_graph_function(char *str)
3949 strlcpy(ftrace_graph_buf, str, FTRACE_FILTER_SIZE);
3952 __setup("ftrace_graph_filter=", set_graph_function);
3954 static int __init set_graph_notrace_function(char *str)
3956 strlcpy(ftrace_graph_notrace_buf, str, FTRACE_FILTER_SIZE);
3959 __setup("ftrace_graph_notrace=", set_graph_notrace_function);
3961 static void __init set_ftrace_early_graph(char *buf, int enable)
3965 unsigned long *table = ftrace_graph_funcs;
3966 int *count = &ftrace_graph_count;
3969 table = ftrace_graph_notrace_funcs;
3970 count = &ftrace_graph_notrace_count;
3974 func = strsep(&buf, ",");
3975 /* we allow only one expression at a time */
3976 ret = ftrace_set_func(table, count, FTRACE_GRAPH_MAX_FUNCS, func);
3978 printk(KERN_DEBUG "ftrace: function %s not "
3979 "traceable\n", func);
3982 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
3985 ftrace_set_early_filter(struct ftrace_ops *ops, char *buf, int enable)
3989 ftrace_ops_init(ops);
3992 func = strsep(&buf, ",");
3993 ftrace_set_regex(ops, func, strlen(func), 0, enable);
3997 static void __init set_ftrace_early_filters(void)
3999 if (ftrace_filter_buf[0])
4000 ftrace_set_early_filter(&global_ops, ftrace_filter_buf, 1);
4001 if (ftrace_notrace_buf[0])
4002 ftrace_set_early_filter(&global_ops, ftrace_notrace_buf, 0);
4003 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
4004 if (ftrace_graph_buf[0])
4005 set_ftrace_early_graph(ftrace_graph_buf, 1);
4006 if (ftrace_graph_notrace_buf[0])
4007 set_ftrace_early_graph(ftrace_graph_notrace_buf, 0);
4008 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
4011 int ftrace_regex_release(struct inode *inode, struct file *file)
4013 struct seq_file *m = (struct seq_file *)file->private_data;
4014 struct ftrace_iterator *iter;
4015 struct ftrace_hash **orig_hash;
4016 struct trace_parser *parser;
4020 if (file->f_mode & FMODE_READ) {
4022 seq_release(inode, file);
4024 iter = file->private_data;
4026 parser = &iter->parser;
4027 if (trace_parser_loaded(parser)) {
4028 parser->buffer[parser->idx] = 0;
4029 ftrace_match_records(iter->hash, parser->buffer, parser->idx);
4032 trace_parser_put(parser);
4034 mutex_lock(&iter->ops->func_hash->regex_lock);
4036 if (file->f_mode & FMODE_WRITE) {
4037 filter_hash = !!(iter->flags & FTRACE_ITER_FILTER);
4040 orig_hash = &iter->ops->func_hash->filter_hash;
4042 orig_hash = &iter->ops->func_hash->notrace_hash;
4044 mutex_lock(&ftrace_lock);
4045 ret = ftrace_hash_move(iter->ops, filter_hash,
4046 orig_hash, iter->hash);
4048 ftrace_ops_update_code(iter->ops);
4050 mutex_unlock(&ftrace_lock);
4053 mutex_unlock(&iter->ops->func_hash->regex_lock);
4054 free_ftrace_hash(iter->hash);
4060 static const struct file_operations ftrace_avail_fops = {
4061 .open = ftrace_avail_open,
4063 .llseek = seq_lseek,
4064 .release = seq_release_private,
4067 static const struct file_operations ftrace_enabled_fops = {
4068 .open = ftrace_enabled_open,
4070 .llseek = seq_lseek,
4071 .release = seq_release_private,
4074 static const struct file_operations ftrace_filter_fops = {
4075 .open = ftrace_filter_open,
4077 .write = ftrace_filter_write,
4078 .llseek = tracing_lseek,
4079 .release = ftrace_regex_release,
4082 static const struct file_operations ftrace_notrace_fops = {
4083 .open = ftrace_notrace_open,
4085 .write = ftrace_notrace_write,
4086 .llseek = tracing_lseek,
4087 .release = ftrace_regex_release,
4090 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
4092 static DEFINE_MUTEX(graph_lock);
4094 int ftrace_graph_count;
4095 int ftrace_graph_notrace_count;
4096 unsigned long ftrace_graph_funcs[FTRACE_GRAPH_MAX_FUNCS] __read_mostly;
4097 unsigned long ftrace_graph_notrace_funcs[FTRACE_GRAPH_MAX_FUNCS] __read_mostly;
4099 struct ftrace_graph_data {
4100 unsigned long *table;
4103 const struct seq_operations *seq_ops;
4107 __g_next(struct seq_file *m, loff_t *pos)
4109 struct ftrace_graph_data *fgd = m->private;
4111 if (*pos >= *fgd->count)
4113 return &fgd->table[*pos];
4117 g_next(struct seq_file *m, void *v, loff_t *pos)
4120 return __g_next(m, pos);
4123 static void *g_start(struct seq_file *m, loff_t *pos)
4125 struct ftrace_graph_data *fgd = m->private;
4127 mutex_lock(&graph_lock);
4129 /* Nothing, tell g_show to print all functions are enabled */
4130 if (!*fgd->count && !*pos)
4133 return __g_next(m, pos);
4136 static void g_stop(struct seq_file *m, void *p)
4138 mutex_unlock(&graph_lock);
4141 static int g_show(struct seq_file *m, void *v)
4143 unsigned long *ptr = v;
4148 if (ptr == (unsigned long *)1) {
4149 struct ftrace_graph_data *fgd = m->private;
4151 if (fgd->table == ftrace_graph_funcs)
4152 seq_printf(m, "#### all functions enabled ####\n");
4154 seq_printf(m, "#### no functions disabled ####\n");
4158 seq_printf(m, "%ps\n", (void *)*ptr);
4163 static const struct seq_operations ftrace_graph_seq_ops = {
4171 __ftrace_graph_open(struct inode *inode, struct file *file,
4172 struct ftrace_graph_data *fgd)
4176 mutex_lock(&graph_lock);
4177 if ((file->f_mode & FMODE_WRITE) &&
4178 (file->f_flags & O_TRUNC)) {
4180 memset(fgd->table, 0, fgd->size * sizeof(*fgd->table));
4182 mutex_unlock(&graph_lock);
4184 if (file->f_mode & FMODE_READ) {
4185 ret = seq_open(file, fgd->seq_ops);
4187 struct seq_file *m = file->private_data;
4191 file->private_data = fgd;
4197 ftrace_graph_open(struct inode *inode, struct file *file)
4199 struct ftrace_graph_data *fgd;
4201 if (unlikely(ftrace_disabled))
4204 fgd = kmalloc(sizeof(*fgd), GFP_KERNEL);
4208 fgd->table = ftrace_graph_funcs;
4209 fgd->size = FTRACE_GRAPH_MAX_FUNCS;
4210 fgd->count = &ftrace_graph_count;
4211 fgd->seq_ops = &ftrace_graph_seq_ops;
4213 return __ftrace_graph_open(inode, file, fgd);
4217 ftrace_graph_notrace_open(struct inode *inode, struct file *file)
4219 struct ftrace_graph_data *fgd;
4221 if (unlikely(ftrace_disabled))
4224 fgd = kmalloc(sizeof(*fgd), GFP_KERNEL);
4228 fgd->table = ftrace_graph_notrace_funcs;
4229 fgd->size = FTRACE_GRAPH_MAX_FUNCS;
4230 fgd->count = &ftrace_graph_notrace_count;
4231 fgd->seq_ops = &ftrace_graph_seq_ops;
4233 return __ftrace_graph_open(inode, file, fgd);
4237 ftrace_graph_release(struct inode *inode, struct file *file)
4239 if (file->f_mode & FMODE_READ) {
4240 struct seq_file *m = file->private_data;
4243 seq_release(inode, file);
4245 kfree(file->private_data);
4252 ftrace_set_func(unsigned long *array, int *idx, int size, char *buffer)
4254 struct dyn_ftrace *rec;
4255 struct ftrace_page *pg;
4264 type = filter_parse_regex(buffer, strlen(buffer), &search, ¬);
4265 if (!not && *idx >= size)
4268 search_len = strlen(search);
4270 mutex_lock(&ftrace_lock);
4272 if (unlikely(ftrace_disabled)) {
4273 mutex_unlock(&ftrace_lock);
4277 do_for_each_ftrace_rec(pg, rec) {
4279 if (ftrace_match_record(rec, NULL, search, search_len, type)) {
4280 /* if it is in the array */
4282 for (i = 0; i < *idx; i++) {
4283 if (array[i] == rec->ip) {
4292 array[(*idx)++] = rec->ip;
4298 array[i] = array[--(*idx)];
4304 } while_for_each_ftrace_rec();
4306 mutex_unlock(&ftrace_lock);
4315 ftrace_graph_write(struct file *file, const char __user *ubuf,
4316 size_t cnt, loff_t *ppos)
4318 struct trace_parser parser;
4319 ssize_t read, ret = 0;
4320 struct ftrace_graph_data *fgd = file->private_data;
4325 if (trace_parser_get_init(&parser, FTRACE_BUFF_MAX))
4328 read = trace_get_user(&parser, ubuf, cnt, ppos);
4330 if (read >= 0 && trace_parser_loaded((&parser))) {
4331 parser.buffer[parser.idx] = 0;
4333 mutex_lock(&graph_lock);
4335 /* we allow only one expression at a time */
4336 ret = ftrace_set_func(fgd->table, fgd->count, fgd->size,
4339 mutex_unlock(&graph_lock);
4345 trace_parser_put(&parser);
4350 static const struct file_operations ftrace_graph_fops = {
4351 .open = ftrace_graph_open,
4353 .write = ftrace_graph_write,
4354 .llseek = tracing_lseek,
4355 .release = ftrace_graph_release,
4358 static const struct file_operations ftrace_graph_notrace_fops = {
4359 .open = ftrace_graph_notrace_open,
4361 .write = ftrace_graph_write,
4362 .llseek = tracing_lseek,
4363 .release = ftrace_graph_release,
4365 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
4367 void ftrace_create_filter_files(struct ftrace_ops *ops,
4368 struct dentry *parent)
4371 trace_create_file("set_ftrace_filter", 0644, parent,
4372 ops, &ftrace_filter_fops);
4374 trace_create_file("set_ftrace_notrace", 0644, parent,
4375 ops, &ftrace_notrace_fops);
4379 * The name "destroy_filter_files" is really a misnomer. Although
4380 * in the future, it may actualy delete the files, but this is
4381 * really intended to make sure the ops passed in are disabled
4382 * and that when this function returns, the caller is free to
4385 * The "destroy" name is only to match the "create" name that this
4386 * should be paired with.
4388 void ftrace_destroy_filter_files(struct ftrace_ops *ops)
4390 mutex_lock(&ftrace_lock);
4391 if (ops->flags & FTRACE_OPS_FL_ENABLED)
4392 ftrace_shutdown(ops, 0);
4393 ops->flags |= FTRACE_OPS_FL_DELETED;
4394 mutex_unlock(&ftrace_lock);
4397 static __init int ftrace_init_dyn_debugfs(struct dentry *d_tracer)
4400 trace_create_file("available_filter_functions", 0444,
4401 d_tracer, NULL, &ftrace_avail_fops);
4403 trace_create_file("enabled_functions", 0444,
4404 d_tracer, NULL, &ftrace_enabled_fops);
4406 ftrace_create_filter_files(&global_ops, d_tracer);
4408 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
4409 trace_create_file("set_graph_function", 0444, d_tracer,
4411 &ftrace_graph_fops);
4412 trace_create_file("set_graph_notrace", 0444, d_tracer,
4414 &ftrace_graph_notrace_fops);
4415 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
4420 static int ftrace_cmp_ips(const void *a, const void *b)
4422 const unsigned long *ipa = a;
4423 const unsigned long *ipb = b;
4432 static void ftrace_swap_ips(void *a, void *b, int size)
4434 unsigned long *ipa = a;
4435 unsigned long *ipb = b;
4443 static int ftrace_process_locs(struct module *mod,
4444 unsigned long *start,
4447 struct ftrace_page *start_pg;
4448 struct ftrace_page *pg;
4449 struct dyn_ftrace *rec;
4450 unsigned long count;
4453 unsigned long flags = 0; /* Shut up gcc */
4456 count = end - start;
4461 sort(start, count, sizeof(*start),
4462 ftrace_cmp_ips, ftrace_swap_ips);
4464 start_pg = ftrace_allocate_pages(count);
4468 mutex_lock(&ftrace_lock);
4471 * Core and each module needs their own pages, as
4472 * modules will free them when they are removed.
4473 * Force a new page to be allocated for modules.
4476 WARN_ON(ftrace_pages || ftrace_pages_start);
4477 /* First initialization */
4478 ftrace_pages = ftrace_pages_start = start_pg;
4483 if (WARN_ON(ftrace_pages->next)) {
4484 /* Hmm, we have free pages? */
4485 while (ftrace_pages->next)
4486 ftrace_pages = ftrace_pages->next;
4489 ftrace_pages->next = start_pg;
4495 addr = ftrace_call_adjust(*p++);
4497 * Some architecture linkers will pad between
4498 * the different mcount_loc sections of different
4499 * object files to satisfy alignments.
4500 * Skip any NULL pointers.
4505 if (pg->index == pg->size) {
4506 /* We should have allocated enough */
4507 if (WARN_ON(!pg->next))
4512 rec = &pg->records[pg->index++];
4516 /* We should have used all pages */
4519 /* Assign the last page to ftrace_pages */
4523 * We only need to disable interrupts on start up
4524 * because we are modifying code that an interrupt
4525 * may execute, and the modification is not atomic.
4526 * But for modules, nothing runs the code we modify
4527 * until we are finished with it, and there's no
4528 * reason to cause large interrupt latencies while we do it.
4531 local_irq_save(flags);
4532 ftrace_update_code(mod, start_pg);
4534 local_irq_restore(flags);
4537 mutex_unlock(&ftrace_lock);
4542 #ifdef CONFIG_MODULES
4544 #define next_to_ftrace_page(p) container_of(p, struct ftrace_page, next)
4546 void ftrace_release_mod(struct module *mod)
4548 struct dyn_ftrace *rec;
4549 struct ftrace_page **last_pg;
4550 struct ftrace_page *pg;
4553 mutex_lock(&ftrace_lock);
4555 if (ftrace_disabled)
4559 * Each module has its own ftrace_pages, remove
4560 * them from the list.
4562 last_pg = &ftrace_pages_start;
4563 for (pg = ftrace_pages_start; pg; pg = *last_pg) {
4564 rec = &pg->records[0];
4565 if (within_module_core(rec->ip, mod)) {
4567 * As core pages are first, the first
4568 * page should never be a module page.
4570 if (WARN_ON(pg == ftrace_pages_start))
4573 /* Check if we are deleting the last page */
4574 if (pg == ftrace_pages)
4575 ftrace_pages = next_to_ftrace_page(last_pg);
4577 *last_pg = pg->next;
4578 order = get_count_order(pg->size / ENTRIES_PER_PAGE);
4579 free_pages((unsigned long)pg->records, order);
4582 last_pg = &pg->next;
4585 mutex_unlock(&ftrace_lock);
4588 static void ftrace_init_module(struct module *mod,
4589 unsigned long *start, unsigned long *end)
4591 if (ftrace_disabled || start == end)
4593 ftrace_process_locs(mod, start, end);
4596 void ftrace_module_init(struct module *mod)
4598 ftrace_init_module(mod, mod->ftrace_callsites,
4599 mod->ftrace_callsites +
4600 mod->num_ftrace_callsites);
4603 static int ftrace_module_notify_exit(struct notifier_block *self,
4604 unsigned long val, void *data)
4606 struct module *mod = data;
4608 if (val == MODULE_STATE_GOING)
4609 ftrace_release_mod(mod);
4614 static int ftrace_module_notify_exit(struct notifier_block *self,
4615 unsigned long val, void *data)
4619 #endif /* CONFIG_MODULES */
4621 struct notifier_block ftrace_module_exit_nb = {
4622 .notifier_call = ftrace_module_notify_exit,
4623 .priority = INT_MIN, /* Run after anything that can remove kprobes */
4626 void __init ftrace_init(void)
4628 extern unsigned long __start_mcount_loc[];
4629 extern unsigned long __stop_mcount_loc[];
4630 unsigned long count, flags;
4633 local_irq_save(flags);
4634 ret = ftrace_dyn_arch_init();
4635 local_irq_restore(flags);
4639 count = __stop_mcount_loc - __start_mcount_loc;
4641 pr_info("ftrace: No functions to be traced?\n");
4645 pr_info("ftrace: allocating %ld entries in %ld pages\n",
4646 count, count / ENTRIES_PER_PAGE + 1);
4648 last_ftrace_enabled = ftrace_enabled = 1;
4650 ret = ftrace_process_locs(NULL,
4654 ret = register_module_notifier(&ftrace_module_exit_nb);
4656 pr_warning("Failed to register trace ftrace module exit notifier\n");
4658 set_ftrace_early_filters();
4662 ftrace_disabled = 1;
4667 static struct ftrace_ops global_ops = {
4668 .func = ftrace_stub,
4669 .flags = FTRACE_OPS_FL_RECURSION_SAFE | FTRACE_OPS_FL_INITIALIZED,
4672 static int __init ftrace_nodyn_init(void)
4677 core_initcall(ftrace_nodyn_init);
4679 static inline int ftrace_init_dyn_debugfs(struct dentry *d_tracer) { return 0; }
4680 static inline void ftrace_startup_enable(int command) { }
4681 /* Keep as macros so we do not need to define the commands */
4682 # define ftrace_startup(ops, command) \
4684 int ___ret = __register_ftrace_function(ops); \
4686 (ops)->flags |= FTRACE_OPS_FL_ENABLED; \
4689 # define ftrace_shutdown(ops, command) \
4691 int ___ret = __unregister_ftrace_function(ops); \
4693 (ops)->flags &= ~FTRACE_OPS_FL_ENABLED; \
4697 # define ftrace_startup_sysctl() do { } while (0)
4698 # define ftrace_shutdown_sysctl() do { } while (0)
4701 ftrace_ops_test(struct ftrace_ops *ops, unsigned long ip, void *regs)
4706 #endif /* CONFIG_DYNAMIC_FTRACE */
4708 __init void ftrace_init_global_array_ops(struct trace_array *tr)
4710 tr->ops = &global_ops;
4711 tr->ops->private = tr;
4714 void ftrace_init_array_ops(struct trace_array *tr, ftrace_func_t func)
4716 /* If we filter on pids, update to use the pid function */
4717 if (tr->flags & TRACE_ARRAY_FL_GLOBAL) {
4718 if (WARN_ON(tr->ops->func != ftrace_stub))
4719 printk("ftrace ops had %pS for function\n",
4721 /* Only the top level instance does pid tracing */
4722 if (!list_empty(&ftrace_pids)) {
4723 set_ftrace_pid_function(func);
4724 func = ftrace_pid_func;
4727 tr->ops->func = func;
4728 tr->ops->private = tr;
4731 void ftrace_reset_array_ops(struct trace_array *tr)
4733 tr->ops->func = ftrace_stub;
4737 ftrace_ops_control_func(unsigned long ip, unsigned long parent_ip,
4738 struct ftrace_ops *op, struct pt_regs *regs)
4740 if (unlikely(trace_recursion_test(TRACE_CONTROL_BIT)))
4744 * Some of the ops may be dynamically allocated,
4745 * they must be freed after a synchronize_sched().
4747 preempt_disable_notrace();
4748 trace_recursion_set(TRACE_CONTROL_BIT);
4751 * Control funcs (perf) uses RCU. Only trace if
4752 * RCU is currently active.
4754 if (!rcu_is_watching())
4757 do_for_each_ftrace_op(op, ftrace_control_list) {
4758 if (!(op->flags & FTRACE_OPS_FL_STUB) &&
4759 !ftrace_function_local_disabled(op) &&
4760 ftrace_ops_test(op, ip, regs))
4761 op->func(ip, parent_ip, op, regs);
4762 } while_for_each_ftrace_op(op);
4764 trace_recursion_clear(TRACE_CONTROL_BIT);
4765 preempt_enable_notrace();
4768 static struct ftrace_ops control_ops = {
4769 .func = ftrace_ops_control_func,
4770 .flags = FTRACE_OPS_FL_RECURSION_SAFE | FTRACE_OPS_FL_INITIALIZED,
4771 INIT_OPS_HASH(control_ops)
4775 __ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip,
4776 struct ftrace_ops *ignored, struct pt_regs *regs)
4778 struct ftrace_ops *op;
4781 bit = trace_test_and_set_recursion(TRACE_LIST_START, TRACE_LIST_MAX);
4786 * Some of the ops may be dynamically allocated,
4787 * they must be freed after a synchronize_sched().
4789 preempt_disable_notrace();
4790 do_for_each_ftrace_op(op, ftrace_ops_list) {
4791 if (ftrace_ops_test(op, ip, regs)) {
4792 if (FTRACE_WARN_ON(!op->func)) {
4793 pr_warn("op=%p %pS\n", op, op);
4796 op->func(ip, parent_ip, op, regs);
4798 } while_for_each_ftrace_op(op);
4800 preempt_enable_notrace();
4801 trace_clear_recursion(bit);
4805 * Some archs only support passing ip and parent_ip. Even though
4806 * the list function ignores the op parameter, we do not want any
4807 * C side effects, where a function is called without the caller
4808 * sending a third parameter.
4809 * Archs are to support both the regs and ftrace_ops at the same time.
4810 * If they support ftrace_ops, it is assumed they support regs.
4811 * If call backs want to use regs, they must either check for regs
4812 * being NULL, or CONFIG_DYNAMIC_FTRACE_WITH_REGS.
4813 * Note, CONFIG_DYNAMIC_FTRACE_WITH_REGS expects a full regs to be saved.
4814 * An architecture can pass partial regs with ftrace_ops and still
4815 * set the ARCH_SUPPORT_FTARCE_OPS.
4817 #if ARCH_SUPPORTS_FTRACE_OPS
4818 static void ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip,
4819 struct ftrace_ops *op, struct pt_regs *regs)
4821 __ftrace_ops_list_func(ip, parent_ip, NULL, regs);
4824 static void ftrace_ops_no_ops(unsigned long ip, unsigned long parent_ip)
4826 __ftrace_ops_list_func(ip, parent_ip, NULL, NULL);
4830 static void clear_ftrace_swapper(void)
4832 struct task_struct *p;
4836 for_each_online_cpu(cpu) {
4838 clear_tsk_trace_trace(p);
4843 static void set_ftrace_swapper(void)
4845 struct task_struct *p;
4849 for_each_online_cpu(cpu) {
4851 set_tsk_trace_trace(p);
4856 static void clear_ftrace_pid(struct pid *pid)
4858 struct task_struct *p;
4861 do_each_pid_task(pid, PIDTYPE_PID, p) {
4862 clear_tsk_trace_trace(p);
4863 } while_each_pid_task(pid, PIDTYPE_PID, p);
4869 static void set_ftrace_pid(struct pid *pid)
4871 struct task_struct *p;
4874 do_each_pid_task(pid, PIDTYPE_PID, p) {
4875 set_tsk_trace_trace(p);
4876 } while_each_pid_task(pid, PIDTYPE_PID, p);
4880 static void clear_ftrace_pid_task(struct pid *pid)
4882 if (pid == ftrace_swapper_pid)
4883 clear_ftrace_swapper();
4885 clear_ftrace_pid(pid);
4888 static void set_ftrace_pid_task(struct pid *pid)
4890 if (pid == ftrace_swapper_pid)
4891 set_ftrace_swapper();
4893 set_ftrace_pid(pid);
4896 static int ftrace_pid_add(int p)
4899 struct ftrace_pid *fpid;
4902 mutex_lock(&ftrace_lock);
4905 pid = ftrace_swapper_pid;
4907 pid = find_get_pid(p);
4914 list_for_each_entry(fpid, &ftrace_pids, list)
4915 if (fpid->pid == pid)
4920 fpid = kmalloc(sizeof(*fpid), GFP_KERNEL);
4924 list_add(&fpid->list, &ftrace_pids);
4927 set_ftrace_pid_task(pid);
4929 ftrace_update_pid_func();
4930 ftrace_startup_enable(0);
4932 mutex_unlock(&ftrace_lock);
4936 if (pid != ftrace_swapper_pid)
4940 mutex_unlock(&ftrace_lock);
4944 static void ftrace_pid_reset(void)
4946 struct ftrace_pid *fpid, *safe;
4948 mutex_lock(&ftrace_lock);
4949 list_for_each_entry_safe(fpid, safe, &ftrace_pids, list) {
4950 struct pid *pid = fpid->pid;
4952 clear_ftrace_pid_task(pid);
4954 list_del(&fpid->list);
4958 ftrace_update_pid_func();
4959 ftrace_startup_enable(0);
4961 mutex_unlock(&ftrace_lock);
4964 static void *fpid_start(struct seq_file *m, loff_t *pos)
4966 mutex_lock(&ftrace_lock);
4968 if (list_empty(&ftrace_pids) && (!*pos))
4971 return seq_list_start(&ftrace_pids, *pos);
4974 static void *fpid_next(struct seq_file *m, void *v, loff_t *pos)
4979 return seq_list_next(v, &ftrace_pids, pos);
4982 static void fpid_stop(struct seq_file *m, void *p)
4984 mutex_unlock(&ftrace_lock);
4987 static int fpid_show(struct seq_file *m, void *v)
4989 const struct ftrace_pid *fpid = list_entry(v, struct ftrace_pid, list);
4991 if (v == (void *)1) {
4992 seq_printf(m, "no pid\n");
4996 if (fpid->pid == ftrace_swapper_pid)
4997 seq_printf(m, "swapper tasks\n");
4999 seq_printf(m, "%u\n", pid_vnr(fpid->pid));
5004 static const struct seq_operations ftrace_pid_sops = {
5005 .start = fpid_start,
5012 ftrace_pid_open(struct inode *inode, struct file *file)
5016 if ((file->f_mode & FMODE_WRITE) &&
5017 (file->f_flags & O_TRUNC))
5020 if (file->f_mode & FMODE_READ)
5021 ret = seq_open(file, &ftrace_pid_sops);
5027 ftrace_pid_write(struct file *filp, const char __user *ubuf,
5028 size_t cnt, loff_t *ppos)
5034 if (cnt >= sizeof(buf))
5037 if (copy_from_user(&buf, ubuf, cnt))
5043 * Allow "echo > set_ftrace_pid" or "echo -n '' > set_ftrace_pid"
5044 * to clean the filter quietly.
5046 tmp = strstrip(buf);
5047 if (strlen(tmp) == 0)
5050 ret = kstrtol(tmp, 10, &val);
5054 ret = ftrace_pid_add(val);
5056 return ret ? ret : cnt;
5060 ftrace_pid_release(struct inode *inode, struct file *file)
5062 if (file->f_mode & FMODE_READ)
5063 seq_release(inode, file);
5068 static const struct file_operations ftrace_pid_fops = {
5069 .open = ftrace_pid_open,
5070 .write = ftrace_pid_write,
5072 .llseek = tracing_lseek,
5073 .release = ftrace_pid_release,
5076 static __init int ftrace_init_debugfs(void)
5078 struct dentry *d_tracer;
5080 d_tracer = tracing_init_dentry();
5084 ftrace_init_dyn_debugfs(d_tracer);
5086 trace_create_file("set_ftrace_pid", 0644, d_tracer,
5087 NULL, &ftrace_pid_fops);
5089 ftrace_profile_debugfs(d_tracer);
5093 fs_initcall(ftrace_init_debugfs);
5096 * ftrace_kill - kill ftrace
5098 * This function should be used by panic code. It stops ftrace
5099 * but in a not so nice way. If you need to simply kill ftrace
5100 * from a non-atomic section, use ftrace_kill.
5102 void ftrace_kill(void)
5104 ftrace_disabled = 1;
5106 clear_ftrace_function();
5110 * Test if ftrace is dead or not.
5112 int ftrace_is_dead(void)
5114 return ftrace_disabled;
5118 * register_ftrace_function - register a function for profiling
5119 * @ops - ops structure that holds the function for profiling.
5121 * Register a function to be called by all functions in the
5124 * Note: @ops->func and all the functions it calls must be labeled
5125 * with "notrace", otherwise it will go into a
5128 int register_ftrace_function(struct ftrace_ops *ops)
5132 ftrace_ops_init(ops);
5134 mutex_lock(&ftrace_lock);
5136 ret = ftrace_startup(ops, 0);
5138 mutex_unlock(&ftrace_lock);
5142 EXPORT_SYMBOL_GPL(register_ftrace_function);
5145 * unregister_ftrace_function - unregister a function for profiling.
5146 * @ops - ops structure that holds the function to unregister
5148 * Unregister a function that was added to be called by ftrace profiling.
5150 int unregister_ftrace_function(struct ftrace_ops *ops)
5154 mutex_lock(&ftrace_lock);
5155 ret = ftrace_shutdown(ops, 0);
5156 mutex_unlock(&ftrace_lock);
5160 EXPORT_SYMBOL_GPL(unregister_ftrace_function);
5163 ftrace_enable_sysctl(struct ctl_table *table, int write,
5164 void __user *buffer, size_t *lenp,
5169 mutex_lock(&ftrace_lock);
5171 if (unlikely(ftrace_disabled))
5174 ret = proc_dointvec(table, write, buffer, lenp, ppos);
5176 if (ret || !write || (last_ftrace_enabled == !!ftrace_enabled))
5179 last_ftrace_enabled = !!ftrace_enabled;
5181 if (ftrace_enabled) {
5183 ftrace_startup_sysctl();
5185 /* we are starting ftrace again */
5186 if (ftrace_ops_list != &ftrace_list_end)
5187 update_ftrace_function();
5190 /* stopping ftrace calls (just send to ftrace_stub) */
5191 ftrace_trace_function = ftrace_stub;
5193 ftrace_shutdown_sysctl();
5197 mutex_unlock(&ftrace_lock);
5201 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
5203 static struct ftrace_ops graph_ops = {
5204 .func = ftrace_stub,
5205 .flags = FTRACE_OPS_FL_RECURSION_SAFE |
5206 FTRACE_OPS_FL_INITIALIZED |
5208 #ifdef FTRACE_GRAPH_TRAMP_ADDR
5209 .trampoline = FTRACE_GRAPH_TRAMP_ADDR,
5211 ASSIGN_OPS_HASH(graph_ops, &global_ops.local_hash)
5214 static int ftrace_graph_active;
5216 int ftrace_graph_entry_stub(struct ftrace_graph_ent *trace)
5221 /* The callbacks that hook a function */
5222 trace_func_graph_ret_t ftrace_graph_return =
5223 (trace_func_graph_ret_t)ftrace_stub;
5224 trace_func_graph_ent_t ftrace_graph_entry = ftrace_graph_entry_stub;
5225 static trace_func_graph_ent_t __ftrace_graph_entry = ftrace_graph_entry_stub;
5227 /* Try to assign a return stack array on FTRACE_RETSTACK_ALLOC_SIZE tasks. */
5228 static int alloc_retstack_tasklist(struct ftrace_ret_stack **ret_stack_list)
5232 unsigned long flags;
5233 int start = 0, end = FTRACE_RETSTACK_ALLOC_SIZE;
5234 struct task_struct *g, *t;
5236 for (i = 0; i < FTRACE_RETSTACK_ALLOC_SIZE; i++) {
5237 ret_stack_list[i] = kmalloc(FTRACE_RETFUNC_DEPTH
5238 * sizeof(struct ftrace_ret_stack),
5240 if (!ret_stack_list[i]) {
5248 read_lock_irqsave(&tasklist_lock, flags);
5249 do_each_thread(g, t) {
5255 if (t->ret_stack == NULL) {
5256 atomic_set(&t->tracing_graph_pause, 0);
5257 atomic_set(&t->trace_overrun, 0);
5258 t->curr_ret_stack = -1;
5259 /* Make sure the tasks see the -1 first: */
5261 t->ret_stack = ret_stack_list[start++];
5263 } while_each_thread(g, t);
5266 read_unlock_irqrestore(&tasklist_lock, flags);
5268 for (i = start; i < end; i++)
5269 kfree(ret_stack_list[i]);
5274 ftrace_graph_probe_sched_switch(void *ignore,
5275 struct task_struct *prev, struct task_struct *next)
5277 unsigned long long timestamp;
5281 * Does the user want to count the time a function was asleep.
5282 * If so, do not update the time stamps.
5284 if (trace_flags & TRACE_ITER_SLEEP_TIME)
5287 timestamp = trace_clock_local();
5289 prev->ftrace_timestamp = timestamp;
5291 /* only process tasks that we timestamped */
5292 if (!next->ftrace_timestamp)
5296 * Update all the counters in next to make up for the
5297 * time next was sleeping.
5299 timestamp -= next->ftrace_timestamp;
5301 for (index = next->curr_ret_stack; index >= 0; index--)
5302 next->ret_stack[index].calltime += timestamp;
5305 /* Allocate a return stack for each task */
5306 static int start_graph_tracing(void)
5308 struct ftrace_ret_stack **ret_stack_list;
5311 ret_stack_list = kmalloc(FTRACE_RETSTACK_ALLOC_SIZE *
5312 sizeof(struct ftrace_ret_stack *),
5315 if (!ret_stack_list)
5318 /* The cpu_boot init_task->ret_stack will never be freed */
5319 for_each_online_cpu(cpu) {
5320 if (!idle_task(cpu)->ret_stack)
5321 ftrace_graph_init_idle_task(idle_task(cpu), cpu);
5325 ret = alloc_retstack_tasklist(ret_stack_list);
5326 } while (ret == -EAGAIN);
5329 ret = register_trace_sched_switch(ftrace_graph_probe_sched_switch, NULL);
5331 pr_info("ftrace_graph: Couldn't activate tracepoint"
5332 " probe to kernel_sched_switch\n");
5335 kfree(ret_stack_list);
5340 * Hibernation protection.
5341 * The state of the current task is too much unstable during
5342 * suspend/restore to disk. We want to protect against that.
5345 ftrace_suspend_notifier_call(struct notifier_block *bl, unsigned long state,
5349 case PM_HIBERNATION_PREPARE:
5350 pause_graph_tracing();
5353 case PM_POST_HIBERNATION:
5354 unpause_graph_tracing();
5360 static int ftrace_graph_entry_test(struct ftrace_graph_ent *trace)
5362 if (!ftrace_ops_test(&global_ops, trace->func, NULL))
5364 return __ftrace_graph_entry(trace);
5368 * The function graph tracer should only trace the functions defined
5369 * by set_ftrace_filter and set_ftrace_notrace. If another function
5370 * tracer ops is registered, the graph tracer requires testing the
5371 * function against the global ops, and not just trace any function
5372 * that any ftrace_ops registered.
5374 static void update_function_graph_func(void)
5376 struct ftrace_ops *op;
5377 bool do_test = false;
5380 * The graph and global ops share the same set of functions
5381 * to test. If any other ops is on the list, then
5382 * the graph tracing needs to test if its the function
5385 do_for_each_ftrace_op(op, ftrace_ops_list) {
5386 if (op != &global_ops && op != &graph_ops &&
5387 op != &ftrace_list_end) {
5389 /* in double loop, break out with goto */
5392 } while_for_each_ftrace_op(op);
5395 ftrace_graph_entry = ftrace_graph_entry_test;
5397 ftrace_graph_entry = __ftrace_graph_entry;
5400 static struct notifier_block ftrace_suspend_notifier = {
5401 .notifier_call = ftrace_suspend_notifier_call,
5404 int register_ftrace_graph(trace_func_graph_ret_t retfunc,
5405 trace_func_graph_ent_t entryfunc)
5409 mutex_lock(&ftrace_lock);
5411 /* we currently allow only one tracer registered at a time */
5412 if (ftrace_graph_active) {
5417 register_pm_notifier(&ftrace_suspend_notifier);
5419 ftrace_graph_active++;
5420 ret = start_graph_tracing();
5422 ftrace_graph_active--;
5426 ftrace_graph_return = retfunc;
5429 * Update the indirect function to the entryfunc, and the
5430 * function that gets called to the entry_test first. Then
5431 * call the update fgraph entry function to determine if
5432 * the entryfunc should be called directly or not.
5434 __ftrace_graph_entry = entryfunc;
5435 ftrace_graph_entry = ftrace_graph_entry_test;
5436 update_function_graph_func();
5438 ret = ftrace_startup(&graph_ops, FTRACE_START_FUNC_RET);
5441 mutex_unlock(&ftrace_lock);
5445 void unregister_ftrace_graph(void)
5447 mutex_lock(&ftrace_lock);
5449 if (unlikely(!ftrace_graph_active))
5452 ftrace_graph_active--;
5453 ftrace_graph_return = (trace_func_graph_ret_t)ftrace_stub;
5454 ftrace_graph_entry = ftrace_graph_entry_stub;
5455 __ftrace_graph_entry = ftrace_graph_entry_stub;
5456 ftrace_shutdown(&graph_ops, FTRACE_STOP_FUNC_RET);
5457 unregister_pm_notifier(&ftrace_suspend_notifier);
5458 unregister_trace_sched_switch(ftrace_graph_probe_sched_switch, NULL);
5461 mutex_unlock(&ftrace_lock);
5464 static DEFINE_PER_CPU(struct ftrace_ret_stack *, idle_ret_stack);
5467 graph_init_task(struct task_struct *t, struct ftrace_ret_stack *ret_stack)
5469 atomic_set(&t->tracing_graph_pause, 0);
5470 atomic_set(&t->trace_overrun, 0);
5471 t->ftrace_timestamp = 0;
5472 /* make curr_ret_stack visible before we add the ret_stack */
5474 t->ret_stack = ret_stack;
5478 * Allocate a return stack for the idle task. May be the first
5479 * time through, or it may be done by CPU hotplug online.
5481 void ftrace_graph_init_idle_task(struct task_struct *t, int cpu)
5483 t->curr_ret_stack = -1;
5485 * The idle task has no parent, it either has its own
5486 * stack or no stack at all.
5489 WARN_ON(t->ret_stack != per_cpu(idle_ret_stack, cpu));
5491 if (ftrace_graph_active) {
5492 struct ftrace_ret_stack *ret_stack;
5494 ret_stack = per_cpu(idle_ret_stack, cpu);
5496 ret_stack = kmalloc(FTRACE_RETFUNC_DEPTH
5497 * sizeof(struct ftrace_ret_stack),
5501 per_cpu(idle_ret_stack, cpu) = ret_stack;
5503 graph_init_task(t, ret_stack);
5507 /* Allocate a return stack for newly created task */
5508 void ftrace_graph_init_task(struct task_struct *t)
5510 /* Make sure we do not use the parent ret_stack */
5511 t->ret_stack = NULL;
5512 t->curr_ret_stack = -1;
5514 if (ftrace_graph_active) {
5515 struct ftrace_ret_stack *ret_stack;
5517 ret_stack = kmalloc(FTRACE_RETFUNC_DEPTH
5518 * sizeof(struct ftrace_ret_stack),
5522 graph_init_task(t, ret_stack);
5526 void ftrace_graph_exit_task(struct task_struct *t)
5528 struct ftrace_ret_stack *ret_stack = t->ret_stack;
5530 t->ret_stack = NULL;
5531 /* NULL must become visible to IRQs before we free it: */