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 static void ftrace_ops_recurs_func(unsigned long ip, unsigned long parent_ip,
117 struct ftrace_ops *op, struct pt_regs *regs);
119 #if ARCH_SUPPORTS_FTRACE_OPS
120 static void ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip,
121 struct ftrace_ops *op, struct pt_regs *regs);
123 /* See comment below, where ftrace_ops_list_func is defined */
124 static void ftrace_ops_no_ops(unsigned long ip, unsigned long parent_ip);
125 #define ftrace_ops_list_func ((ftrace_func_t)ftrace_ops_no_ops)
129 * Traverse the ftrace_global_list, invoking all entries. The reason that we
130 * can use rcu_dereference_raw_notrace() is that elements removed from this list
131 * are simply leaked, so there is no need to interact with a grace-period
132 * mechanism. The rcu_dereference_raw_notrace() calls are needed to handle
133 * concurrent insertions into the ftrace_global_list.
135 * Silly Alpha and silly pointer-speculation compiler optimizations!
137 #define do_for_each_ftrace_op(op, list) \
138 op = rcu_dereference_raw_notrace(list); \
142 * Optimized for just a single item in the list (as that is the normal case).
144 #define while_for_each_ftrace_op(op) \
145 while (likely(op = rcu_dereference_raw_notrace((op)->next)) && \
146 unlikely((op) != &ftrace_list_end))
148 static inline void ftrace_ops_init(struct ftrace_ops *ops)
150 #ifdef CONFIG_DYNAMIC_FTRACE
151 if (!(ops->flags & FTRACE_OPS_FL_INITIALIZED)) {
152 mutex_init(&ops->local_hash.regex_lock);
153 ops->func_hash = &ops->local_hash;
154 ops->flags |= FTRACE_OPS_FL_INITIALIZED;
160 * ftrace_nr_registered_ops - return number of ops registered
162 * Returns the number of ftrace_ops registered and tracing functions
164 int ftrace_nr_registered_ops(void)
166 struct ftrace_ops *ops;
169 mutex_lock(&ftrace_lock);
171 for (ops = ftrace_ops_list;
172 ops != &ftrace_list_end; ops = ops->next)
175 mutex_unlock(&ftrace_lock);
180 static void ftrace_pid_func(unsigned long ip, unsigned long parent_ip,
181 struct ftrace_ops *op, struct pt_regs *regs)
183 if (!test_tsk_trace_trace(current))
186 ftrace_pid_function(ip, parent_ip, op, regs);
189 static void set_ftrace_pid_function(ftrace_func_t func)
191 /* do not set ftrace_pid_function to itself! */
192 if (func != ftrace_pid_func)
193 ftrace_pid_function = func;
197 * clear_ftrace_function - reset the ftrace function
199 * This NULLs the ftrace function and in essence stops
200 * tracing. There may be lag
202 void clear_ftrace_function(void)
204 ftrace_trace_function = ftrace_stub;
205 ftrace_pid_function = ftrace_stub;
208 static void control_ops_disable_all(struct ftrace_ops *ops)
212 for_each_possible_cpu(cpu)
213 *per_cpu_ptr(ops->disabled, cpu) = 1;
216 static int control_ops_alloc(struct ftrace_ops *ops)
218 int __percpu *disabled;
220 disabled = alloc_percpu(int);
224 ops->disabled = disabled;
225 control_ops_disable_all(ops);
229 static void ftrace_sync(struct work_struct *work)
232 * This function is just a stub to implement a hard force
233 * of synchronize_sched(). This requires synchronizing
234 * tasks even in userspace and idle.
236 * Yes, function tracing is rude.
240 static void ftrace_sync_ipi(void *data)
242 /* Probably not needed, but do it anyway */
246 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
247 static void update_function_graph_func(void);
249 static inline void update_function_graph_func(void) { }
252 static void update_ftrace_function(void)
257 * Prepare the ftrace_ops that the arch callback will use.
258 * If there's only one ftrace_ops registered, the ftrace_ops_list
259 * will point to the ops we want.
261 set_function_trace_op = ftrace_ops_list;
263 /* If there's no ftrace_ops registered, just call the stub function */
264 if (ftrace_ops_list == &ftrace_list_end) {
268 * If we are at the end of the list and this ops is
269 * recursion safe and not dynamic and the arch supports passing ops,
270 * then have the mcount trampoline call the function directly.
272 } else if (ftrace_ops_list->next == &ftrace_list_end) {
273 func = ftrace_ops_get_func(ftrace_ops_list);
276 /* Just use the default ftrace_ops */
277 set_function_trace_op = &ftrace_list_end;
278 func = ftrace_ops_list_func;
281 update_function_graph_func();
283 /* If there's no change, then do nothing more here */
284 if (ftrace_trace_function == func)
288 * If we are using the list function, it doesn't care
289 * about the function_trace_ops.
291 if (func == ftrace_ops_list_func) {
292 ftrace_trace_function = func;
294 * Don't even bother setting function_trace_ops,
295 * it would be racy to do so anyway.
300 #ifndef CONFIG_DYNAMIC_FTRACE
302 * For static tracing, we need to be a bit more careful.
303 * The function change takes affect immediately. Thus,
304 * we need to coorditate the setting of the function_trace_ops
305 * with the setting of the ftrace_trace_function.
307 * Set the function to the list ops, which will call the
308 * function we want, albeit indirectly, but it handles the
309 * ftrace_ops and doesn't depend on function_trace_op.
311 ftrace_trace_function = ftrace_ops_list_func;
313 * Make sure all CPUs see this. Yes this is slow, but static
314 * tracing is slow and nasty to have enabled.
316 schedule_on_each_cpu(ftrace_sync);
317 /* Now all cpus are using the list ops. */
318 function_trace_op = set_function_trace_op;
319 /* Make sure the function_trace_op is visible on all CPUs */
321 /* Nasty way to force a rmb on all cpus */
322 smp_call_function(ftrace_sync_ipi, NULL, 1);
323 /* OK, we are all set to update the ftrace_trace_function now! */
324 #endif /* !CONFIG_DYNAMIC_FTRACE */
326 ftrace_trace_function = func;
329 int using_ftrace_ops_list_func(void)
331 return ftrace_trace_function == ftrace_ops_list_func;
334 static void add_ftrace_ops(struct ftrace_ops **list, struct ftrace_ops *ops)
338 * We are entering ops into the list but another
339 * CPU might be walking that list. We need to make sure
340 * the ops->next pointer is valid before another CPU sees
341 * the ops pointer included into the list.
343 rcu_assign_pointer(*list, ops);
346 static int remove_ftrace_ops(struct ftrace_ops **list, struct ftrace_ops *ops)
348 struct ftrace_ops **p;
351 * If we are removing the last function, then simply point
352 * to the ftrace_stub.
354 if (*list == ops && ops->next == &ftrace_list_end) {
355 *list = &ftrace_list_end;
359 for (p = list; *p != &ftrace_list_end; p = &(*p)->next)
370 static void add_ftrace_list_ops(struct ftrace_ops **list,
371 struct ftrace_ops *main_ops,
372 struct ftrace_ops *ops)
374 int first = *list == &ftrace_list_end;
375 add_ftrace_ops(list, ops);
377 add_ftrace_ops(&ftrace_ops_list, main_ops);
380 static int remove_ftrace_list_ops(struct ftrace_ops **list,
381 struct ftrace_ops *main_ops,
382 struct ftrace_ops *ops)
384 int ret = remove_ftrace_ops(list, ops);
385 if (!ret && *list == &ftrace_list_end)
386 ret = remove_ftrace_ops(&ftrace_ops_list, main_ops);
390 static void ftrace_update_trampoline(struct ftrace_ops *ops);
392 static int __register_ftrace_function(struct ftrace_ops *ops)
394 if (ops->flags & FTRACE_OPS_FL_DELETED)
397 if (WARN_ON(ops->flags & FTRACE_OPS_FL_ENABLED))
400 #ifndef CONFIG_DYNAMIC_FTRACE_WITH_REGS
402 * If the ftrace_ops specifies SAVE_REGS, then it only can be used
403 * if the arch supports it, or SAVE_REGS_IF_SUPPORTED is also set.
404 * Setting SAVE_REGS_IF_SUPPORTED makes SAVE_REGS irrelevant.
406 if (ops->flags & FTRACE_OPS_FL_SAVE_REGS &&
407 !(ops->flags & FTRACE_OPS_FL_SAVE_REGS_IF_SUPPORTED))
410 if (ops->flags & FTRACE_OPS_FL_SAVE_REGS_IF_SUPPORTED)
411 ops->flags |= FTRACE_OPS_FL_SAVE_REGS;
414 if (!core_kernel_data((unsigned long)ops))
415 ops->flags |= FTRACE_OPS_FL_DYNAMIC;
417 if (ops->flags & FTRACE_OPS_FL_CONTROL) {
418 if (control_ops_alloc(ops))
420 add_ftrace_list_ops(&ftrace_control_list, &control_ops, ops);
421 /* The control_ops needs the trampoline update */
424 add_ftrace_ops(&ftrace_ops_list, ops);
426 ftrace_update_trampoline(ops);
429 update_ftrace_function();
434 static int __unregister_ftrace_function(struct ftrace_ops *ops)
438 if (WARN_ON(!(ops->flags & FTRACE_OPS_FL_ENABLED)))
441 if (ops->flags & FTRACE_OPS_FL_CONTROL) {
442 ret = remove_ftrace_list_ops(&ftrace_control_list,
445 ret = remove_ftrace_ops(&ftrace_ops_list, ops);
451 update_ftrace_function();
456 static void ftrace_update_pid_func(void)
458 /* Only do something if we are tracing something */
459 if (ftrace_trace_function == ftrace_stub)
462 update_ftrace_function();
465 #ifdef CONFIG_FUNCTION_PROFILER
466 struct ftrace_profile {
467 struct hlist_node node;
469 unsigned long counter;
470 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
471 unsigned long long time;
472 unsigned long long time_squared;
476 struct ftrace_profile_page {
477 struct ftrace_profile_page *next;
479 struct ftrace_profile records[];
482 struct ftrace_profile_stat {
484 struct hlist_head *hash;
485 struct ftrace_profile_page *pages;
486 struct ftrace_profile_page *start;
487 struct tracer_stat stat;
490 #define PROFILE_RECORDS_SIZE \
491 (PAGE_SIZE - offsetof(struct ftrace_profile_page, records))
493 #define PROFILES_PER_PAGE \
494 (PROFILE_RECORDS_SIZE / sizeof(struct ftrace_profile))
496 static int ftrace_profile_enabled __read_mostly;
498 /* ftrace_profile_lock - synchronize the enable and disable of the profiler */
499 static DEFINE_MUTEX(ftrace_profile_lock);
501 static DEFINE_PER_CPU(struct ftrace_profile_stat, ftrace_profile_stats);
503 #define FTRACE_PROFILE_HASH_BITS 10
504 #define FTRACE_PROFILE_HASH_SIZE (1 << FTRACE_PROFILE_HASH_BITS)
507 function_stat_next(void *v, int idx)
509 struct ftrace_profile *rec = v;
510 struct ftrace_profile_page *pg;
512 pg = (struct ftrace_profile_page *)((unsigned long)rec & PAGE_MASK);
518 if ((void *)rec >= (void *)&pg->records[pg->index]) {
522 rec = &pg->records[0];
530 static void *function_stat_start(struct tracer_stat *trace)
532 struct ftrace_profile_stat *stat =
533 container_of(trace, struct ftrace_profile_stat, stat);
535 if (!stat || !stat->start)
538 return function_stat_next(&stat->start->records[0], 0);
541 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
542 /* function graph compares on total time */
543 static int function_stat_cmp(void *p1, void *p2)
545 struct ftrace_profile *a = p1;
546 struct ftrace_profile *b = p2;
548 if (a->time < b->time)
550 if (a->time > b->time)
556 /* not function graph compares against hits */
557 static int function_stat_cmp(void *p1, void *p2)
559 struct ftrace_profile *a = p1;
560 struct ftrace_profile *b = p2;
562 if (a->counter < b->counter)
564 if (a->counter > b->counter)
571 static int function_stat_headers(struct seq_file *m)
573 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
574 seq_puts(m, " Function "
577 "--- ---- --- ---\n");
579 seq_puts(m, " Function Hit\n"
585 static int function_stat_show(struct seq_file *m, void *v)
587 struct ftrace_profile *rec = v;
588 char str[KSYM_SYMBOL_LEN];
590 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
591 static struct trace_seq s;
592 unsigned long long avg;
593 unsigned long long stddev;
595 mutex_lock(&ftrace_profile_lock);
597 /* we raced with function_profile_reset() */
598 if (unlikely(rec->counter == 0)) {
603 kallsyms_lookup(rec->ip, NULL, NULL, NULL, str);
604 seq_printf(m, " %-30.30s %10lu", str, rec->counter);
606 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
609 do_div(avg, rec->counter);
611 /* Sample standard deviation (s^2) */
612 if (rec->counter <= 1)
616 * Apply Welford's method:
617 * s^2 = 1 / (n * (n-1)) * (n * \Sum (x_i)^2 - (\Sum x_i)^2)
619 stddev = rec->counter * rec->time_squared -
620 rec->time * rec->time;
623 * Divide only 1000 for ns^2 -> us^2 conversion.
624 * trace_print_graph_duration will divide 1000 again.
626 do_div(stddev, rec->counter * (rec->counter - 1) * 1000);
630 trace_print_graph_duration(rec->time, &s);
631 trace_seq_puts(&s, " ");
632 trace_print_graph_duration(avg, &s);
633 trace_seq_puts(&s, " ");
634 trace_print_graph_duration(stddev, &s);
635 trace_print_seq(m, &s);
639 mutex_unlock(&ftrace_profile_lock);
644 static void ftrace_profile_reset(struct ftrace_profile_stat *stat)
646 struct ftrace_profile_page *pg;
648 pg = stat->pages = stat->start;
651 memset(pg->records, 0, PROFILE_RECORDS_SIZE);
656 memset(stat->hash, 0,
657 FTRACE_PROFILE_HASH_SIZE * sizeof(struct hlist_head));
660 int ftrace_profile_pages_init(struct ftrace_profile_stat *stat)
662 struct ftrace_profile_page *pg;
667 /* If we already allocated, do nothing */
671 stat->pages = (void *)get_zeroed_page(GFP_KERNEL);
675 #ifdef CONFIG_DYNAMIC_FTRACE
676 functions = ftrace_update_tot_cnt;
679 * We do not know the number of functions that exist because
680 * dynamic tracing is what counts them. With past experience
681 * we have around 20K functions. That should be more than enough.
682 * It is highly unlikely we will execute every function in
688 pg = stat->start = stat->pages;
690 pages = DIV_ROUND_UP(functions, PROFILES_PER_PAGE);
692 for (i = 1; i < pages; i++) {
693 pg->next = (void *)get_zeroed_page(GFP_KERNEL);
704 unsigned long tmp = (unsigned long)pg;
716 static int ftrace_profile_init_cpu(int cpu)
718 struct ftrace_profile_stat *stat;
721 stat = &per_cpu(ftrace_profile_stats, cpu);
724 /* If the profile is already created, simply reset it */
725 ftrace_profile_reset(stat);
730 * We are profiling all functions, but usually only a few thousand
731 * functions are hit. We'll make a hash of 1024 items.
733 size = FTRACE_PROFILE_HASH_SIZE;
735 stat->hash = kzalloc(sizeof(struct hlist_head) * size, GFP_KERNEL);
740 /* Preallocate the function profiling pages */
741 if (ftrace_profile_pages_init(stat) < 0) {
750 static int ftrace_profile_init(void)
755 for_each_possible_cpu(cpu) {
756 ret = ftrace_profile_init_cpu(cpu);
764 /* interrupts must be disabled */
765 static struct ftrace_profile *
766 ftrace_find_profiled_func(struct ftrace_profile_stat *stat, unsigned long ip)
768 struct ftrace_profile *rec;
769 struct hlist_head *hhd;
772 key = hash_long(ip, FTRACE_PROFILE_HASH_BITS);
773 hhd = &stat->hash[key];
775 if (hlist_empty(hhd))
778 hlist_for_each_entry_rcu_notrace(rec, hhd, node) {
786 static void ftrace_add_profile(struct ftrace_profile_stat *stat,
787 struct ftrace_profile *rec)
791 key = hash_long(rec->ip, FTRACE_PROFILE_HASH_BITS);
792 hlist_add_head_rcu(&rec->node, &stat->hash[key]);
796 * The memory is already allocated, this simply finds a new record to use.
798 static struct ftrace_profile *
799 ftrace_profile_alloc(struct ftrace_profile_stat *stat, unsigned long ip)
801 struct ftrace_profile *rec = NULL;
803 /* prevent recursion (from NMIs) */
804 if (atomic_inc_return(&stat->disabled) != 1)
808 * Try to find the function again since an NMI
809 * could have added it
811 rec = ftrace_find_profiled_func(stat, ip);
815 if (stat->pages->index == PROFILES_PER_PAGE) {
816 if (!stat->pages->next)
818 stat->pages = stat->pages->next;
821 rec = &stat->pages->records[stat->pages->index++];
823 ftrace_add_profile(stat, rec);
826 atomic_dec(&stat->disabled);
832 function_profile_call(unsigned long ip, unsigned long parent_ip,
833 struct ftrace_ops *ops, struct pt_regs *regs)
835 struct ftrace_profile_stat *stat;
836 struct ftrace_profile *rec;
839 if (!ftrace_profile_enabled)
842 local_irq_save(flags);
844 stat = this_cpu_ptr(&ftrace_profile_stats);
845 if (!stat->hash || !ftrace_profile_enabled)
848 rec = ftrace_find_profiled_func(stat, ip);
850 rec = ftrace_profile_alloc(stat, ip);
857 local_irq_restore(flags);
860 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
861 static int profile_graph_entry(struct ftrace_graph_ent *trace)
863 function_profile_call(trace->func, 0, NULL, NULL);
867 static void profile_graph_return(struct ftrace_graph_ret *trace)
869 struct ftrace_profile_stat *stat;
870 unsigned long long calltime;
871 struct ftrace_profile *rec;
874 local_irq_save(flags);
875 stat = this_cpu_ptr(&ftrace_profile_stats);
876 if (!stat->hash || !ftrace_profile_enabled)
879 /* If the calltime was zero'd ignore it */
880 if (!trace->calltime)
883 calltime = trace->rettime - trace->calltime;
885 if (!(trace_flags & TRACE_ITER_GRAPH_TIME)) {
888 index = trace->depth;
890 /* Append this call time to the parent time to subtract */
892 current->ret_stack[index - 1].subtime += calltime;
894 if (current->ret_stack[index].subtime < calltime)
895 calltime -= current->ret_stack[index].subtime;
900 rec = ftrace_find_profiled_func(stat, trace->func);
902 rec->time += calltime;
903 rec->time_squared += calltime * calltime;
907 local_irq_restore(flags);
910 static int register_ftrace_profiler(void)
912 return register_ftrace_graph(&profile_graph_return,
913 &profile_graph_entry);
916 static void unregister_ftrace_profiler(void)
918 unregister_ftrace_graph();
921 static struct ftrace_ops ftrace_profile_ops __read_mostly = {
922 .func = function_profile_call,
923 .flags = FTRACE_OPS_FL_RECURSION_SAFE | FTRACE_OPS_FL_INITIALIZED,
924 INIT_OPS_HASH(ftrace_profile_ops)
927 static int register_ftrace_profiler(void)
929 return register_ftrace_function(&ftrace_profile_ops);
932 static void unregister_ftrace_profiler(void)
934 unregister_ftrace_function(&ftrace_profile_ops);
936 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
939 ftrace_profile_write(struct file *filp, const char __user *ubuf,
940 size_t cnt, loff_t *ppos)
945 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
951 mutex_lock(&ftrace_profile_lock);
952 if (ftrace_profile_enabled ^ val) {
954 ret = ftrace_profile_init();
960 ret = register_ftrace_profiler();
965 ftrace_profile_enabled = 1;
967 ftrace_profile_enabled = 0;
969 * unregister_ftrace_profiler calls stop_machine
970 * so this acts like an synchronize_sched.
972 unregister_ftrace_profiler();
976 mutex_unlock(&ftrace_profile_lock);
984 ftrace_profile_read(struct file *filp, char __user *ubuf,
985 size_t cnt, loff_t *ppos)
987 char buf[64]; /* big enough to hold a number */
990 r = sprintf(buf, "%u\n", ftrace_profile_enabled);
991 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
994 static const struct file_operations ftrace_profile_fops = {
995 .open = tracing_open_generic,
996 .read = ftrace_profile_read,
997 .write = ftrace_profile_write,
998 .llseek = default_llseek,
1001 /* used to initialize the real stat files */
1002 static struct tracer_stat function_stats __initdata = {
1003 .name = "functions",
1004 .stat_start = function_stat_start,
1005 .stat_next = function_stat_next,
1006 .stat_cmp = function_stat_cmp,
1007 .stat_headers = function_stat_headers,
1008 .stat_show = function_stat_show
1011 static __init void ftrace_profile_debugfs(struct dentry *d_tracer)
1013 struct ftrace_profile_stat *stat;
1014 struct dentry *entry;
1019 for_each_possible_cpu(cpu) {
1020 stat = &per_cpu(ftrace_profile_stats, cpu);
1022 /* allocate enough for function name + cpu number */
1023 name = kmalloc(32, GFP_KERNEL);
1026 * The files created are permanent, if something happens
1027 * we still do not free memory.
1030 "Could not allocate stat file for cpu %d\n",
1034 stat->stat = function_stats;
1035 snprintf(name, 32, "function%d", cpu);
1036 stat->stat.name = name;
1037 ret = register_stat_tracer(&stat->stat);
1040 "Could not register function stat for cpu %d\n",
1047 entry = debugfs_create_file("function_profile_enabled", 0644,
1048 d_tracer, NULL, &ftrace_profile_fops);
1050 pr_warning("Could not create debugfs "
1051 "'function_profile_enabled' entry\n");
1054 #else /* CONFIG_FUNCTION_PROFILER */
1055 static __init void ftrace_profile_debugfs(struct dentry *d_tracer)
1058 #endif /* CONFIG_FUNCTION_PROFILER */
1060 static struct pid * const ftrace_swapper_pid = &init_struct_pid;
1062 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
1063 static int ftrace_graph_active;
1065 # define ftrace_graph_active 0
1068 #ifdef CONFIG_DYNAMIC_FTRACE
1070 static struct ftrace_ops *removed_ops;
1073 * Set when doing a global update, like enabling all recs or disabling them.
1074 * It is not set when just updating a single ftrace_ops.
1076 static bool update_all_ops;
1078 #ifndef CONFIG_FTRACE_MCOUNT_RECORD
1079 # error Dynamic ftrace depends on MCOUNT_RECORD
1082 static struct hlist_head ftrace_func_hash[FTRACE_FUNC_HASHSIZE] __read_mostly;
1084 struct ftrace_func_probe {
1085 struct hlist_node node;
1086 struct ftrace_probe_ops *ops;
1087 unsigned long flags;
1090 struct list_head free_list;
1093 struct ftrace_func_entry {
1094 struct hlist_node hlist;
1098 struct ftrace_hash {
1099 unsigned long size_bits;
1100 struct hlist_head *buckets;
1101 unsigned long count;
1102 struct rcu_head rcu;
1106 * We make these constant because no one should touch them,
1107 * but they are used as the default "empty hash", to avoid allocating
1108 * it all the time. These are in a read only section such that if
1109 * anyone does try to modify it, it will cause an exception.
1111 static const struct hlist_head empty_buckets[1];
1112 static const struct ftrace_hash empty_hash = {
1113 .buckets = (struct hlist_head *)empty_buckets,
1115 #define EMPTY_HASH ((struct ftrace_hash *)&empty_hash)
1117 static struct ftrace_ops global_ops = {
1118 .func = ftrace_stub,
1119 .local_hash.notrace_hash = EMPTY_HASH,
1120 .local_hash.filter_hash = EMPTY_HASH,
1121 INIT_OPS_HASH(global_ops)
1122 .flags = FTRACE_OPS_FL_RECURSION_SAFE |
1123 FTRACE_OPS_FL_INITIALIZED,
1127 * This is used by __kernel_text_address() to return true if the
1128 * address is on a dynamically allocated trampoline that would
1129 * not return true for either core_kernel_text() or
1130 * is_module_text_address().
1132 bool is_ftrace_trampoline(unsigned long addr)
1134 struct ftrace_ops *op;
1138 * Some of the ops may be dynamically allocated,
1139 * they are freed after a synchronize_sched().
1141 preempt_disable_notrace();
1143 do_for_each_ftrace_op(op, ftrace_ops_list) {
1145 * This is to check for dynamically allocated trampolines.
1146 * Trampolines that are in kernel text will have
1147 * core_kernel_text() return true.
1149 if (op->trampoline && op->trampoline_size)
1150 if (addr >= op->trampoline &&
1151 addr < op->trampoline + op->trampoline_size) {
1155 } while_for_each_ftrace_op(op);
1158 preempt_enable_notrace();
1163 struct ftrace_page {
1164 struct ftrace_page *next;
1165 struct dyn_ftrace *records;
1170 #define ENTRY_SIZE sizeof(struct dyn_ftrace)
1171 #define ENTRIES_PER_PAGE (PAGE_SIZE / ENTRY_SIZE)
1173 /* estimate from running different kernels */
1174 #define NR_TO_INIT 10000
1176 static struct ftrace_page *ftrace_pages_start;
1177 static struct ftrace_page *ftrace_pages;
1179 static bool __always_inline ftrace_hash_empty(struct ftrace_hash *hash)
1181 return !hash || !hash->count;
1184 static struct ftrace_func_entry *
1185 ftrace_lookup_ip(struct ftrace_hash *hash, unsigned long ip)
1188 struct ftrace_func_entry *entry;
1189 struct hlist_head *hhd;
1191 if (ftrace_hash_empty(hash))
1194 if (hash->size_bits > 0)
1195 key = hash_long(ip, hash->size_bits);
1199 hhd = &hash->buckets[key];
1201 hlist_for_each_entry_rcu_notrace(entry, hhd, hlist) {
1202 if (entry->ip == ip)
1208 static void __add_hash_entry(struct ftrace_hash *hash,
1209 struct ftrace_func_entry *entry)
1211 struct hlist_head *hhd;
1214 if (hash->size_bits)
1215 key = hash_long(entry->ip, hash->size_bits);
1219 hhd = &hash->buckets[key];
1220 hlist_add_head(&entry->hlist, hhd);
1224 static int add_hash_entry(struct ftrace_hash *hash, unsigned long ip)
1226 struct ftrace_func_entry *entry;
1228 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
1233 __add_hash_entry(hash, entry);
1239 free_hash_entry(struct ftrace_hash *hash,
1240 struct ftrace_func_entry *entry)
1242 hlist_del(&entry->hlist);
1248 remove_hash_entry(struct ftrace_hash *hash,
1249 struct ftrace_func_entry *entry)
1251 hlist_del(&entry->hlist);
1255 static void ftrace_hash_clear(struct ftrace_hash *hash)
1257 struct hlist_head *hhd;
1258 struct hlist_node *tn;
1259 struct ftrace_func_entry *entry;
1260 int size = 1 << hash->size_bits;
1266 for (i = 0; i < size; i++) {
1267 hhd = &hash->buckets[i];
1268 hlist_for_each_entry_safe(entry, tn, hhd, hlist)
1269 free_hash_entry(hash, entry);
1271 FTRACE_WARN_ON(hash->count);
1274 static void free_ftrace_hash(struct ftrace_hash *hash)
1276 if (!hash || hash == EMPTY_HASH)
1278 ftrace_hash_clear(hash);
1279 kfree(hash->buckets);
1283 static void __free_ftrace_hash_rcu(struct rcu_head *rcu)
1285 struct ftrace_hash *hash;
1287 hash = container_of(rcu, struct ftrace_hash, rcu);
1288 free_ftrace_hash(hash);
1291 static void free_ftrace_hash_rcu(struct ftrace_hash *hash)
1293 if (!hash || hash == EMPTY_HASH)
1295 call_rcu_sched(&hash->rcu, __free_ftrace_hash_rcu);
1298 void ftrace_free_filter(struct ftrace_ops *ops)
1300 ftrace_ops_init(ops);
1301 free_ftrace_hash(ops->func_hash->filter_hash);
1302 free_ftrace_hash(ops->func_hash->notrace_hash);
1305 static struct ftrace_hash *alloc_ftrace_hash(int size_bits)
1307 struct ftrace_hash *hash;
1310 hash = kzalloc(sizeof(*hash), GFP_KERNEL);
1314 size = 1 << size_bits;
1315 hash->buckets = kcalloc(size, sizeof(*hash->buckets), GFP_KERNEL);
1317 if (!hash->buckets) {
1322 hash->size_bits = size_bits;
1327 static struct ftrace_hash *
1328 alloc_and_copy_ftrace_hash(int size_bits, struct ftrace_hash *hash)
1330 struct ftrace_func_entry *entry;
1331 struct ftrace_hash *new_hash;
1336 new_hash = alloc_ftrace_hash(size_bits);
1341 if (ftrace_hash_empty(hash))
1344 size = 1 << hash->size_bits;
1345 for (i = 0; i < size; i++) {
1346 hlist_for_each_entry(entry, &hash->buckets[i], hlist) {
1347 ret = add_hash_entry(new_hash, entry->ip);
1353 FTRACE_WARN_ON(new_hash->count != hash->count);
1358 free_ftrace_hash(new_hash);
1363 ftrace_hash_rec_disable_modify(struct ftrace_ops *ops, int filter_hash);
1365 ftrace_hash_rec_enable_modify(struct ftrace_ops *ops, int filter_hash);
1367 static int ftrace_hash_ipmodify_update(struct ftrace_ops *ops,
1368 struct ftrace_hash *new_hash);
1371 ftrace_hash_move(struct ftrace_ops *ops, int enable,
1372 struct ftrace_hash **dst, struct ftrace_hash *src)
1374 struct ftrace_func_entry *entry;
1375 struct hlist_node *tn;
1376 struct hlist_head *hhd;
1377 struct ftrace_hash *new_hash;
1378 int size = src->count;
1383 /* Reject setting notrace hash on IPMODIFY ftrace_ops */
1384 if (ops->flags & FTRACE_OPS_FL_IPMODIFY && !enable)
1388 * If the new source is empty, just free dst and assign it
1392 new_hash = EMPTY_HASH;
1397 * Make the hash size about 1/2 the # found
1399 for (size /= 2; size; size >>= 1)
1402 /* Don't allocate too much */
1403 if (bits > FTRACE_HASH_MAX_BITS)
1404 bits = FTRACE_HASH_MAX_BITS;
1406 new_hash = alloc_ftrace_hash(bits);
1410 size = 1 << src->size_bits;
1411 for (i = 0; i < size; i++) {
1412 hhd = &src->buckets[i];
1413 hlist_for_each_entry_safe(entry, tn, hhd, hlist) {
1414 remove_hash_entry(src, entry);
1415 __add_hash_entry(new_hash, entry);
1420 /* Make sure this can be applied if it is IPMODIFY ftrace_ops */
1422 /* IPMODIFY should be updated only when filter_hash updating */
1423 ret = ftrace_hash_ipmodify_update(ops, new_hash);
1425 free_ftrace_hash(new_hash);
1431 * Remove the current set, update the hash and add
1434 ftrace_hash_rec_disable_modify(ops, enable);
1436 rcu_assign_pointer(*dst, new_hash);
1438 ftrace_hash_rec_enable_modify(ops, enable);
1443 static bool hash_contains_ip(unsigned long ip,
1444 struct ftrace_ops_hash *hash)
1447 * The function record is a match if it exists in the filter
1448 * hash and not in the notrace hash. Note, an emty hash is
1449 * considered a match for the filter hash, but an empty
1450 * notrace hash is considered not in the notrace hash.
1452 return (ftrace_hash_empty(hash->filter_hash) ||
1453 ftrace_lookup_ip(hash->filter_hash, ip)) &&
1454 (ftrace_hash_empty(hash->notrace_hash) ||
1455 !ftrace_lookup_ip(hash->notrace_hash, ip));
1459 * Test the hashes for this ops to see if we want to call
1460 * the ops->func or not.
1462 * It's a match if the ip is in the ops->filter_hash or
1463 * the filter_hash does not exist or is empty,
1465 * the ip is not in the ops->notrace_hash.
1467 * This needs to be called with preemption disabled as
1468 * the hashes are freed with call_rcu_sched().
1471 ftrace_ops_test(struct ftrace_ops *ops, unsigned long ip, void *regs)
1473 struct ftrace_ops_hash hash;
1476 #ifdef CONFIG_DYNAMIC_FTRACE_WITH_REGS
1478 * There's a small race when adding ops that the ftrace handler
1479 * that wants regs, may be called without them. We can not
1480 * allow that handler to be called if regs is NULL.
1482 if (regs == NULL && (ops->flags & FTRACE_OPS_FL_SAVE_REGS))
1486 hash.filter_hash = rcu_dereference_raw_notrace(ops->func_hash->filter_hash);
1487 hash.notrace_hash = rcu_dereference_raw_notrace(ops->func_hash->notrace_hash);
1489 if (hash_contains_ip(ip, &hash))
1498 * This is a double for. Do not use 'break' to break out of the loop,
1499 * you must use a goto.
1501 #define do_for_each_ftrace_rec(pg, rec) \
1502 for (pg = ftrace_pages_start; pg; pg = pg->next) { \
1504 for (_____i = 0; _____i < pg->index; _____i++) { \
1505 rec = &pg->records[_____i];
1507 #define while_for_each_ftrace_rec() \
1512 static int ftrace_cmp_recs(const void *a, const void *b)
1514 const struct dyn_ftrace *key = a;
1515 const struct dyn_ftrace *rec = b;
1517 if (key->flags < rec->ip)
1519 if (key->ip >= rec->ip + MCOUNT_INSN_SIZE)
1524 static unsigned long ftrace_location_range(unsigned long start, unsigned long end)
1526 struct ftrace_page *pg;
1527 struct dyn_ftrace *rec;
1528 struct dyn_ftrace key;
1531 key.flags = end; /* overload flags, as it is unsigned long */
1533 for (pg = ftrace_pages_start; pg; pg = pg->next) {
1534 if (end < pg->records[0].ip ||
1535 start >= (pg->records[pg->index - 1].ip + MCOUNT_INSN_SIZE))
1537 rec = bsearch(&key, pg->records, pg->index,
1538 sizeof(struct dyn_ftrace),
1548 * ftrace_location - return true if the ip giving is a traced location
1549 * @ip: the instruction pointer to check
1551 * Returns rec->ip if @ip given is a pointer to a ftrace location.
1552 * That is, the instruction that is either a NOP or call to
1553 * the function tracer. It checks the ftrace internal tables to
1554 * determine if the address belongs or not.
1556 unsigned long ftrace_location(unsigned long ip)
1558 return ftrace_location_range(ip, ip);
1562 * ftrace_text_reserved - return true if range contains an ftrace location
1563 * @start: start of range to search
1564 * @end: end of range to search (inclusive). @end points to the last byte to check.
1566 * Returns 1 if @start and @end contains a ftrace location.
1567 * That is, the instruction that is either a NOP or call to
1568 * the function tracer. It checks the ftrace internal tables to
1569 * determine if the address belongs or not.
1571 int ftrace_text_reserved(const void *start, const void *end)
1575 ret = ftrace_location_range((unsigned long)start,
1576 (unsigned long)end);
1581 /* Test if ops registered to this rec needs regs */
1582 static bool test_rec_ops_needs_regs(struct dyn_ftrace *rec)
1584 struct ftrace_ops *ops;
1585 bool keep_regs = false;
1587 for (ops = ftrace_ops_list;
1588 ops != &ftrace_list_end; ops = ops->next) {
1589 /* pass rec in as regs to have non-NULL val */
1590 if (ftrace_ops_test(ops, rec->ip, rec)) {
1591 if (ops->flags & FTRACE_OPS_FL_SAVE_REGS) {
1601 static void __ftrace_hash_rec_update(struct ftrace_ops *ops,
1605 struct ftrace_hash *hash;
1606 struct ftrace_hash *other_hash;
1607 struct ftrace_page *pg;
1608 struct dyn_ftrace *rec;
1612 /* Only update if the ops has been registered */
1613 if (!(ops->flags & FTRACE_OPS_FL_ENABLED))
1617 * In the filter_hash case:
1618 * If the count is zero, we update all records.
1619 * Otherwise we just update the items in the hash.
1621 * In the notrace_hash case:
1622 * We enable the update in the hash.
1623 * As disabling notrace means enabling the tracing,
1624 * and enabling notrace means disabling, the inc variable
1628 hash = ops->func_hash->filter_hash;
1629 other_hash = ops->func_hash->notrace_hash;
1630 if (ftrace_hash_empty(hash))
1634 hash = ops->func_hash->notrace_hash;
1635 other_hash = ops->func_hash->filter_hash;
1637 * If the notrace hash has no items,
1638 * then there's nothing to do.
1640 if (ftrace_hash_empty(hash))
1644 do_for_each_ftrace_rec(pg, rec) {
1645 int in_other_hash = 0;
1651 * Only the filter_hash affects all records.
1652 * Update if the record is not in the notrace hash.
1654 if (!other_hash || !ftrace_lookup_ip(other_hash, rec->ip))
1657 in_hash = !!ftrace_lookup_ip(hash, rec->ip);
1658 in_other_hash = !!ftrace_lookup_ip(other_hash, rec->ip);
1661 * If filter_hash is set, we want to match all functions
1662 * that are in the hash but not in the other hash.
1664 * If filter_hash is not set, then we are decrementing.
1665 * That means we match anything that is in the hash
1666 * and also in the other_hash. That is, we need to turn
1667 * off functions in the other hash because they are disabled
1670 if (filter_hash && in_hash && !in_other_hash)
1672 else if (!filter_hash && in_hash &&
1673 (in_other_hash || ftrace_hash_empty(other_hash)))
1681 if (FTRACE_WARN_ON(ftrace_rec_count(rec) == FTRACE_REF_MAX))
1685 * If there's only a single callback registered to a
1686 * function, and the ops has a trampoline registered
1687 * for it, then we can call it directly.
1689 if (ftrace_rec_count(rec) == 1 && ops->trampoline)
1690 rec->flags |= FTRACE_FL_TRAMP;
1693 * If we are adding another function callback
1694 * to this function, and the previous had a
1695 * custom trampoline in use, then we need to go
1696 * back to the default trampoline.
1698 rec->flags &= ~FTRACE_FL_TRAMP;
1701 * If any ops wants regs saved for this function
1702 * then all ops will get saved regs.
1704 if (ops->flags & FTRACE_OPS_FL_SAVE_REGS)
1705 rec->flags |= FTRACE_FL_REGS;
1707 if (FTRACE_WARN_ON(ftrace_rec_count(rec) == 0))
1712 * If the rec had REGS enabled and the ops that is
1713 * being removed had REGS set, then see if there is
1714 * still any ops for this record that wants regs.
1715 * If not, we can stop recording them.
1717 if (ftrace_rec_count(rec) > 0 &&
1718 rec->flags & FTRACE_FL_REGS &&
1719 ops->flags & FTRACE_OPS_FL_SAVE_REGS) {
1720 if (!test_rec_ops_needs_regs(rec))
1721 rec->flags &= ~FTRACE_FL_REGS;
1725 * If the rec had TRAMP enabled, then it needs to
1726 * be cleared. As TRAMP can only be enabled iff
1727 * there is only a single ops attached to it.
1728 * In otherwords, always disable it on decrementing.
1729 * In the future, we may set it if rec count is
1730 * decremented to one, and the ops that is left
1733 rec->flags &= ~FTRACE_FL_TRAMP;
1736 * flags will be cleared in ftrace_check_record()
1737 * if rec count is zero.
1741 /* Shortcut, if we handled all records, we are done. */
1742 if (!all && count == hash->count)
1744 } while_for_each_ftrace_rec();
1747 static void ftrace_hash_rec_disable(struct ftrace_ops *ops,
1750 __ftrace_hash_rec_update(ops, filter_hash, 0);
1753 static void ftrace_hash_rec_enable(struct ftrace_ops *ops,
1756 __ftrace_hash_rec_update(ops, filter_hash, 1);
1759 static void ftrace_hash_rec_update_modify(struct ftrace_ops *ops,
1760 int filter_hash, int inc)
1762 struct ftrace_ops *op;
1764 __ftrace_hash_rec_update(ops, filter_hash, inc);
1766 if (ops->func_hash != &global_ops.local_hash)
1770 * If the ops shares the global_ops hash, then we need to update
1771 * all ops that are enabled and use this hash.
1773 do_for_each_ftrace_op(op, ftrace_ops_list) {
1777 if (op->func_hash == &global_ops.local_hash)
1778 __ftrace_hash_rec_update(op, filter_hash, inc);
1779 } while_for_each_ftrace_op(op);
1782 static void ftrace_hash_rec_disable_modify(struct ftrace_ops *ops,
1785 ftrace_hash_rec_update_modify(ops, filter_hash, 0);
1788 static void ftrace_hash_rec_enable_modify(struct ftrace_ops *ops,
1791 ftrace_hash_rec_update_modify(ops, filter_hash, 1);
1795 * Try to update IPMODIFY flag on each ftrace_rec. Return 0 if it is OK
1796 * or no-needed to update, -EBUSY if it detects a conflict of the flag
1797 * on a ftrace_rec, and -EINVAL if the new_hash tries to trace all recs.
1798 * Note that old_hash and new_hash has below meanings
1799 * - If the hash is NULL, it hits all recs (if IPMODIFY is set, this is rejected)
1800 * - If the hash is EMPTY_HASH, it hits nothing
1801 * - Anything else hits the recs which match the hash entries.
1803 static int __ftrace_hash_update_ipmodify(struct ftrace_ops *ops,
1804 struct ftrace_hash *old_hash,
1805 struct ftrace_hash *new_hash)
1807 struct ftrace_page *pg;
1808 struct dyn_ftrace *rec, *end = NULL;
1811 /* Only update if the ops has been registered */
1812 if (!(ops->flags & FTRACE_OPS_FL_ENABLED))
1815 if (!(ops->flags & FTRACE_OPS_FL_IPMODIFY))
1819 * Since the IPMODIFY is a very address sensitive action, we do not
1820 * allow ftrace_ops to set all functions to new hash.
1822 if (!new_hash || !old_hash)
1825 /* Update rec->flags */
1826 do_for_each_ftrace_rec(pg, rec) {
1827 /* We need to update only differences of filter_hash */
1828 in_old = !!ftrace_lookup_ip(old_hash, rec->ip);
1829 in_new = !!ftrace_lookup_ip(new_hash, rec->ip);
1830 if (in_old == in_new)
1834 /* New entries must ensure no others are using it */
1835 if (rec->flags & FTRACE_FL_IPMODIFY)
1837 rec->flags |= FTRACE_FL_IPMODIFY;
1838 } else /* Removed entry */
1839 rec->flags &= ~FTRACE_FL_IPMODIFY;
1840 } while_for_each_ftrace_rec();
1847 /* Roll back what we did above */
1848 do_for_each_ftrace_rec(pg, rec) {
1852 in_old = !!ftrace_lookup_ip(old_hash, rec->ip);
1853 in_new = !!ftrace_lookup_ip(new_hash, rec->ip);
1854 if (in_old == in_new)
1858 rec->flags &= ~FTRACE_FL_IPMODIFY;
1860 rec->flags |= FTRACE_FL_IPMODIFY;
1861 } while_for_each_ftrace_rec();
1867 static int ftrace_hash_ipmodify_enable(struct ftrace_ops *ops)
1869 struct ftrace_hash *hash = ops->func_hash->filter_hash;
1871 if (ftrace_hash_empty(hash))
1874 return __ftrace_hash_update_ipmodify(ops, EMPTY_HASH, hash);
1877 /* Disabling always succeeds */
1878 static void ftrace_hash_ipmodify_disable(struct ftrace_ops *ops)
1880 struct ftrace_hash *hash = ops->func_hash->filter_hash;
1882 if (ftrace_hash_empty(hash))
1885 __ftrace_hash_update_ipmodify(ops, hash, EMPTY_HASH);
1888 static int ftrace_hash_ipmodify_update(struct ftrace_ops *ops,
1889 struct ftrace_hash *new_hash)
1891 struct ftrace_hash *old_hash = ops->func_hash->filter_hash;
1893 if (ftrace_hash_empty(old_hash))
1896 if (ftrace_hash_empty(new_hash))
1899 return __ftrace_hash_update_ipmodify(ops, old_hash, new_hash);
1902 static void print_ip_ins(const char *fmt, unsigned char *p)
1906 printk(KERN_CONT "%s", fmt);
1908 for (i = 0; i < MCOUNT_INSN_SIZE; i++)
1909 printk(KERN_CONT "%s%02x", i ? ":" : "", p[i]);
1912 static struct ftrace_ops *
1913 ftrace_find_tramp_ops_any(struct dyn_ftrace *rec);
1916 * ftrace_bug - report and shutdown function tracer
1917 * @failed: The failed type (EFAULT, EINVAL, EPERM)
1918 * @rec: The record that failed
1920 * The arch code that enables or disables the function tracing
1921 * can call ftrace_bug() when it has detected a problem in
1922 * modifying the code. @failed should be one of either:
1923 * EFAULT - if the problem happens on reading the @ip address
1924 * EINVAL - if what is read at @ip is not what was expected
1925 * EPERM - if the problem happens on writting to the @ip address
1927 void ftrace_bug(int failed, struct dyn_ftrace *rec)
1929 unsigned long ip = rec ? rec->ip : 0;
1933 FTRACE_WARN_ON_ONCE(1);
1934 pr_info("ftrace faulted on modifying ");
1938 FTRACE_WARN_ON_ONCE(1);
1939 pr_info("ftrace failed to modify ");
1941 print_ip_ins(" actual: ", (unsigned char *)ip);
1945 FTRACE_WARN_ON_ONCE(1);
1946 pr_info("ftrace faulted on writing ");
1950 FTRACE_WARN_ON_ONCE(1);
1951 pr_info("ftrace faulted on unknown error ");
1955 struct ftrace_ops *ops = NULL;
1957 pr_info("ftrace record flags: %lx\n", rec->flags);
1958 pr_cont(" (%ld)%s", ftrace_rec_count(rec),
1959 rec->flags & FTRACE_FL_REGS ? " R" : " ");
1960 if (rec->flags & FTRACE_FL_TRAMP_EN) {
1961 ops = ftrace_find_tramp_ops_any(rec);
1963 pr_cont("\ttramp: %pS",
1964 (void *)ops->trampoline);
1966 pr_cont("\ttramp: ERROR!");
1969 ip = ftrace_get_addr_curr(rec);
1970 pr_cont(" expected tramp: %lx\n", ip);
1974 static int ftrace_check_record(struct dyn_ftrace *rec, int enable, int update)
1976 unsigned long flag = 0UL;
1979 * If we are updating calls:
1981 * If the record has a ref count, then we need to enable it
1982 * because someone is using it.
1984 * Otherwise we make sure its disabled.
1986 * If we are disabling calls, then disable all records that
1989 if (enable && ftrace_rec_count(rec))
1990 flag = FTRACE_FL_ENABLED;
1993 * If enabling and the REGS flag does not match the REGS_EN, or
1994 * the TRAMP flag doesn't match the TRAMP_EN, then do not ignore
1995 * this record. Set flags to fail the compare against ENABLED.
1998 if (!(rec->flags & FTRACE_FL_REGS) !=
1999 !(rec->flags & FTRACE_FL_REGS_EN))
2000 flag |= FTRACE_FL_REGS;
2002 if (!(rec->flags & FTRACE_FL_TRAMP) !=
2003 !(rec->flags & FTRACE_FL_TRAMP_EN))
2004 flag |= FTRACE_FL_TRAMP;
2007 /* If the state of this record hasn't changed, then do nothing */
2008 if ((rec->flags & FTRACE_FL_ENABLED) == flag)
2009 return FTRACE_UPDATE_IGNORE;
2012 /* Save off if rec is being enabled (for return value) */
2013 flag ^= rec->flags & FTRACE_FL_ENABLED;
2016 rec->flags |= FTRACE_FL_ENABLED;
2017 if (flag & FTRACE_FL_REGS) {
2018 if (rec->flags & FTRACE_FL_REGS)
2019 rec->flags |= FTRACE_FL_REGS_EN;
2021 rec->flags &= ~FTRACE_FL_REGS_EN;
2023 if (flag & FTRACE_FL_TRAMP) {
2024 if (rec->flags & FTRACE_FL_TRAMP)
2025 rec->flags |= FTRACE_FL_TRAMP_EN;
2027 rec->flags &= ~FTRACE_FL_TRAMP_EN;
2032 * If this record is being updated from a nop, then
2033 * return UPDATE_MAKE_CALL.
2035 * return UPDATE_MODIFY_CALL to tell the caller to convert
2036 * from the save regs, to a non-save regs function or
2037 * vice versa, or from a trampoline call.
2039 if (flag & FTRACE_FL_ENABLED)
2040 return FTRACE_UPDATE_MAKE_CALL;
2042 return FTRACE_UPDATE_MODIFY_CALL;
2046 /* If there's no more users, clear all flags */
2047 if (!ftrace_rec_count(rec))
2051 * Just disable the record, but keep the ops TRAMP
2052 * and REGS states. The _EN flags must be disabled though.
2054 rec->flags &= ~(FTRACE_FL_ENABLED | FTRACE_FL_TRAMP_EN |
2058 return FTRACE_UPDATE_MAKE_NOP;
2062 * ftrace_update_record, set a record that now is tracing or not
2063 * @rec: the record to update
2064 * @enable: set to 1 if the record is tracing, zero to force disable
2066 * The records that represent all functions that can be traced need
2067 * to be updated when tracing has been enabled.
2069 int ftrace_update_record(struct dyn_ftrace *rec, int enable)
2071 return ftrace_check_record(rec, enable, 1);
2075 * ftrace_test_record, check if the record has been enabled or not
2076 * @rec: the record to test
2077 * @enable: set to 1 to check if enabled, 0 if it is disabled
2079 * The arch code may need to test if a record is already set to
2080 * tracing to determine how to modify the function code that it
2083 int ftrace_test_record(struct dyn_ftrace *rec, int enable)
2085 return ftrace_check_record(rec, enable, 0);
2088 static struct ftrace_ops *
2089 ftrace_find_tramp_ops_any(struct dyn_ftrace *rec)
2091 struct ftrace_ops *op;
2092 unsigned long ip = rec->ip;
2094 do_for_each_ftrace_op(op, ftrace_ops_list) {
2096 if (!op->trampoline)
2099 if (hash_contains_ip(ip, op->func_hash))
2101 } while_for_each_ftrace_op(op);
2106 static struct ftrace_ops *
2107 ftrace_find_tramp_ops_curr(struct dyn_ftrace *rec)
2109 struct ftrace_ops *op;
2110 unsigned long ip = rec->ip;
2113 * Need to check removed ops first.
2114 * If they are being removed, and this rec has a tramp,
2115 * and this rec is in the ops list, then it would be the
2116 * one with the tramp.
2119 if (hash_contains_ip(ip, &removed_ops->old_hash))
2124 * Need to find the current trampoline for a rec.
2125 * Now, a trampoline is only attached to a rec if there
2126 * was a single 'ops' attached to it. But this can be called
2127 * when we are adding another op to the rec or removing the
2128 * current one. Thus, if the op is being added, we can
2129 * ignore it because it hasn't attached itself to the rec
2132 * If an ops is being modified (hooking to different functions)
2133 * then we don't care about the new functions that are being
2134 * added, just the old ones (that are probably being removed).
2136 * If we are adding an ops to a function that already is using
2137 * a trampoline, it needs to be removed (trampolines are only
2138 * for single ops connected), then an ops that is not being
2139 * modified also needs to be checked.
2141 do_for_each_ftrace_op(op, ftrace_ops_list) {
2143 if (!op->trampoline)
2147 * If the ops is being added, it hasn't gotten to
2148 * the point to be removed from this tree yet.
2150 if (op->flags & FTRACE_OPS_FL_ADDING)
2155 * If the ops is being modified and is in the old
2156 * hash, then it is probably being removed from this
2159 if ((op->flags & FTRACE_OPS_FL_MODIFYING) &&
2160 hash_contains_ip(ip, &op->old_hash))
2163 * If the ops is not being added or modified, and it's
2164 * in its normal filter hash, then this must be the one
2167 if (!(op->flags & FTRACE_OPS_FL_MODIFYING) &&
2168 hash_contains_ip(ip, op->func_hash))
2171 } while_for_each_ftrace_op(op);
2176 static struct ftrace_ops *
2177 ftrace_find_tramp_ops_new(struct dyn_ftrace *rec)
2179 struct ftrace_ops *op;
2180 unsigned long ip = rec->ip;
2182 do_for_each_ftrace_op(op, ftrace_ops_list) {
2183 /* pass rec in as regs to have non-NULL val */
2184 if (hash_contains_ip(ip, op->func_hash))
2186 } while_for_each_ftrace_op(op);
2192 * ftrace_get_addr_new - Get the call address to set to
2193 * @rec: The ftrace record descriptor
2195 * If the record has the FTRACE_FL_REGS set, that means that it
2196 * wants to convert to a callback that saves all regs. If FTRACE_FL_REGS
2197 * is not not set, then it wants to convert to the normal callback.
2199 * Returns the address of the trampoline to set to
2201 unsigned long ftrace_get_addr_new(struct dyn_ftrace *rec)
2203 struct ftrace_ops *ops;
2205 /* Trampolines take precedence over regs */
2206 if (rec->flags & FTRACE_FL_TRAMP) {
2207 ops = ftrace_find_tramp_ops_new(rec);
2208 if (FTRACE_WARN_ON(!ops || !ops->trampoline)) {
2209 pr_warn("Bad trampoline accounting at: %p (%pS) (%lx)\n",
2210 (void *)rec->ip, (void *)rec->ip, rec->flags);
2211 /* Ftrace is shutting down, return anything */
2212 return (unsigned long)FTRACE_ADDR;
2214 return ops->trampoline;
2217 if (rec->flags & FTRACE_FL_REGS)
2218 return (unsigned long)FTRACE_REGS_ADDR;
2220 return (unsigned long)FTRACE_ADDR;
2224 * ftrace_get_addr_curr - Get the call address that is already there
2225 * @rec: The ftrace record descriptor
2227 * The FTRACE_FL_REGS_EN is set when the record already points to
2228 * a function that saves all the regs. Basically the '_EN' version
2229 * represents the current state of the function.
2231 * Returns the address of the trampoline that is currently being called
2233 unsigned long ftrace_get_addr_curr(struct dyn_ftrace *rec)
2235 struct ftrace_ops *ops;
2237 /* Trampolines take precedence over regs */
2238 if (rec->flags & FTRACE_FL_TRAMP_EN) {
2239 ops = ftrace_find_tramp_ops_curr(rec);
2240 if (FTRACE_WARN_ON(!ops)) {
2241 pr_warning("Bad trampoline accounting at: %p (%pS)\n",
2242 (void *)rec->ip, (void *)rec->ip);
2243 /* Ftrace is shutting down, return anything */
2244 return (unsigned long)FTRACE_ADDR;
2246 return ops->trampoline;
2249 if (rec->flags & FTRACE_FL_REGS_EN)
2250 return (unsigned long)FTRACE_REGS_ADDR;
2252 return (unsigned long)FTRACE_ADDR;
2256 __ftrace_replace_code(struct dyn_ftrace *rec, int enable)
2258 unsigned long ftrace_old_addr;
2259 unsigned long ftrace_addr;
2262 ftrace_addr = ftrace_get_addr_new(rec);
2264 /* This needs to be done before we call ftrace_update_record */
2265 ftrace_old_addr = ftrace_get_addr_curr(rec);
2267 ret = ftrace_update_record(rec, enable);
2270 case FTRACE_UPDATE_IGNORE:
2273 case FTRACE_UPDATE_MAKE_CALL:
2274 return ftrace_make_call(rec, ftrace_addr);
2276 case FTRACE_UPDATE_MAKE_NOP:
2277 return ftrace_make_nop(NULL, rec, ftrace_old_addr);
2279 case FTRACE_UPDATE_MODIFY_CALL:
2280 return ftrace_modify_call(rec, ftrace_old_addr, ftrace_addr);
2283 return -1; /* unknow ftrace bug */
2286 void __weak ftrace_replace_code(int enable)
2288 struct dyn_ftrace *rec;
2289 struct ftrace_page *pg;
2292 if (unlikely(ftrace_disabled))
2295 do_for_each_ftrace_rec(pg, rec) {
2296 failed = __ftrace_replace_code(rec, enable);
2298 ftrace_bug(failed, rec);
2299 /* Stop processing */
2302 } while_for_each_ftrace_rec();
2305 struct ftrace_rec_iter {
2306 struct ftrace_page *pg;
2311 * ftrace_rec_iter_start, start up iterating over traced functions
2313 * Returns an iterator handle that is used to iterate over all
2314 * the records that represent address locations where functions
2317 * May return NULL if no records are available.
2319 struct ftrace_rec_iter *ftrace_rec_iter_start(void)
2322 * We only use a single iterator.
2323 * Protected by the ftrace_lock mutex.
2325 static struct ftrace_rec_iter ftrace_rec_iter;
2326 struct ftrace_rec_iter *iter = &ftrace_rec_iter;
2328 iter->pg = ftrace_pages_start;
2331 /* Could have empty pages */
2332 while (iter->pg && !iter->pg->index)
2333 iter->pg = iter->pg->next;
2342 * ftrace_rec_iter_next, get the next record to process.
2343 * @iter: The handle to the iterator.
2345 * Returns the next iterator after the given iterator @iter.
2347 struct ftrace_rec_iter *ftrace_rec_iter_next(struct ftrace_rec_iter *iter)
2351 if (iter->index >= iter->pg->index) {
2352 iter->pg = iter->pg->next;
2355 /* Could have empty pages */
2356 while (iter->pg && !iter->pg->index)
2357 iter->pg = iter->pg->next;
2367 * ftrace_rec_iter_record, get the record at the iterator location
2368 * @iter: The current iterator location
2370 * Returns the record that the current @iter is at.
2372 struct dyn_ftrace *ftrace_rec_iter_record(struct ftrace_rec_iter *iter)
2374 return &iter->pg->records[iter->index];
2378 ftrace_code_disable(struct module *mod, struct dyn_ftrace *rec)
2382 if (unlikely(ftrace_disabled))
2385 ret = ftrace_make_nop(mod, rec, MCOUNT_ADDR);
2387 ftrace_bug(ret, rec);
2394 * archs can override this function if they must do something
2395 * before the modifying code is performed.
2397 int __weak ftrace_arch_code_modify_prepare(void)
2403 * archs can override this function if they must do something
2404 * after the modifying code is performed.
2406 int __weak ftrace_arch_code_modify_post_process(void)
2411 void ftrace_modify_all_code(int command)
2413 int update = command & FTRACE_UPDATE_TRACE_FUNC;
2417 * If the ftrace_caller calls a ftrace_ops func directly,
2418 * we need to make sure that it only traces functions it
2419 * expects to trace. When doing the switch of functions,
2420 * we need to update to the ftrace_ops_list_func first
2421 * before the transition between old and new calls are set,
2422 * as the ftrace_ops_list_func will check the ops hashes
2423 * to make sure the ops are having the right functions
2427 err = ftrace_update_ftrace_func(ftrace_ops_list_func);
2428 if (FTRACE_WARN_ON(err))
2432 if (command & FTRACE_UPDATE_CALLS)
2433 ftrace_replace_code(1);
2434 else if (command & FTRACE_DISABLE_CALLS)
2435 ftrace_replace_code(0);
2437 if (update && ftrace_trace_function != ftrace_ops_list_func) {
2438 function_trace_op = set_function_trace_op;
2440 /* If irqs are disabled, we are in stop machine */
2441 if (!irqs_disabled())
2442 smp_call_function(ftrace_sync_ipi, NULL, 1);
2443 err = ftrace_update_ftrace_func(ftrace_trace_function);
2444 if (FTRACE_WARN_ON(err))
2448 if (command & FTRACE_START_FUNC_RET)
2449 err = ftrace_enable_ftrace_graph_caller();
2450 else if (command & FTRACE_STOP_FUNC_RET)
2451 err = ftrace_disable_ftrace_graph_caller();
2452 FTRACE_WARN_ON(err);
2455 static int __ftrace_modify_code(void *data)
2457 int *command = data;
2459 ftrace_modify_all_code(*command);
2465 * ftrace_run_stop_machine, go back to the stop machine method
2466 * @command: The command to tell ftrace what to do
2468 * If an arch needs to fall back to the stop machine method, the
2469 * it can call this function.
2471 void ftrace_run_stop_machine(int command)
2473 stop_machine(__ftrace_modify_code, &command, NULL);
2477 * arch_ftrace_update_code, modify the code to trace or not trace
2478 * @command: The command that needs to be done
2480 * Archs can override this function if it does not need to
2481 * run stop_machine() to modify code.
2483 void __weak arch_ftrace_update_code(int command)
2485 ftrace_run_stop_machine(command);
2488 static void ftrace_run_update_code(int command)
2492 ret = ftrace_arch_code_modify_prepare();
2493 FTRACE_WARN_ON(ret);
2498 * By default we use stop_machine() to modify the code.
2499 * But archs can do what ever they want as long as it
2500 * is safe. The stop_machine() is the safest, but also
2501 * produces the most overhead.
2503 arch_ftrace_update_code(command);
2505 ret = ftrace_arch_code_modify_post_process();
2506 FTRACE_WARN_ON(ret);
2509 static void ftrace_run_modify_code(struct ftrace_ops *ops, int command,
2510 struct ftrace_ops_hash *old_hash)
2512 ops->flags |= FTRACE_OPS_FL_MODIFYING;
2513 ops->old_hash.filter_hash = old_hash->filter_hash;
2514 ops->old_hash.notrace_hash = old_hash->notrace_hash;
2515 ftrace_run_update_code(command);
2516 ops->old_hash.filter_hash = NULL;
2517 ops->old_hash.notrace_hash = NULL;
2518 ops->flags &= ~FTRACE_OPS_FL_MODIFYING;
2521 static ftrace_func_t saved_ftrace_func;
2522 static int ftrace_start_up;
2524 void __weak arch_ftrace_trampoline_free(struct ftrace_ops *ops)
2528 static void control_ops_free(struct ftrace_ops *ops)
2530 free_percpu(ops->disabled);
2533 static void ftrace_startup_enable(int command)
2535 if (saved_ftrace_func != ftrace_trace_function) {
2536 saved_ftrace_func = ftrace_trace_function;
2537 command |= FTRACE_UPDATE_TRACE_FUNC;
2540 if (!command || !ftrace_enabled)
2543 ftrace_run_update_code(command);
2546 static void ftrace_startup_all(int command)
2548 update_all_ops = true;
2549 ftrace_startup_enable(command);
2550 update_all_ops = false;
2553 static int ftrace_startup(struct ftrace_ops *ops, int command)
2557 if (unlikely(ftrace_disabled))
2560 ret = __register_ftrace_function(ops);
2565 command |= FTRACE_UPDATE_CALLS;
2568 * Note that ftrace probes uses this to start up
2569 * and modify functions it will probe. But we still
2570 * set the ADDING flag for modification, as probes
2571 * do not have trampolines. If they add them in the
2572 * future, then the probes will need to distinguish
2573 * between adding and updating probes.
2575 ops->flags |= FTRACE_OPS_FL_ENABLED | FTRACE_OPS_FL_ADDING;
2577 ret = ftrace_hash_ipmodify_enable(ops);
2579 /* Rollback registration process */
2580 __unregister_ftrace_function(ops);
2582 ops->flags &= ~FTRACE_OPS_FL_ENABLED;
2586 ftrace_hash_rec_enable(ops, 1);
2588 ftrace_startup_enable(command);
2590 ops->flags &= ~FTRACE_OPS_FL_ADDING;
2595 static int ftrace_shutdown(struct ftrace_ops *ops, int command)
2599 if (unlikely(ftrace_disabled))
2602 ret = __unregister_ftrace_function(ops);
2608 * Just warn in case of unbalance, no need to kill ftrace, it's not
2609 * critical but the ftrace_call callers may be never nopped again after
2610 * further ftrace uses.
2612 WARN_ON_ONCE(ftrace_start_up < 0);
2614 /* Disabling ipmodify never fails */
2615 ftrace_hash_ipmodify_disable(ops);
2616 ftrace_hash_rec_disable(ops, 1);
2618 ops->flags &= ~FTRACE_OPS_FL_ENABLED;
2620 command |= FTRACE_UPDATE_CALLS;
2622 if (saved_ftrace_func != ftrace_trace_function) {
2623 saved_ftrace_func = ftrace_trace_function;
2624 command |= FTRACE_UPDATE_TRACE_FUNC;
2627 if (!command || !ftrace_enabled) {
2629 * If these are control ops, they still need their
2630 * per_cpu field freed. Since, function tracing is
2631 * not currently active, we can just free them
2632 * without synchronizing all CPUs.
2634 if (ops->flags & FTRACE_OPS_FL_CONTROL)
2635 control_ops_free(ops);
2640 * If the ops uses a trampoline, then it needs to be
2641 * tested first on update.
2643 ops->flags |= FTRACE_OPS_FL_REMOVING;
2646 /* The trampoline logic checks the old hashes */
2647 ops->old_hash.filter_hash = ops->func_hash->filter_hash;
2648 ops->old_hash.notrace_hash = ops->func_hash->notrace_hash;
2650 ftrace_run_update_code(command);
2653 * If there's no more ops registered with ftrace, run a
2654 * sanity check to make sure all rec flags are cleared.
2656 if (ftrace_ops_list == &ftrace_list_end) {
2657 struct ftrace_page *pg;
2658 struct dyn_ftrace *rec;
2660 do_for_each_ftrace_rec(pg, rec) {
2661 if (FTRACE_WARN_ON_ONCE(rec->flags))
2662 pr_warn(" %pS flags:%lx\n",
2663 (void *)rec->ip, rec->flags);
2664 } while_for_each_ftrace_rec();
2667 ops->old_hash.filter_hash = NULL;
2668 ops->old_hash.notrace_hash = NULL;
2671 ops->flags &= ~FTRACE_OPS_FL_REMOVING;
2674 * Dynamic ops may be freed, we must make sure that all
2675 * callers are done before leaving this function.
2676 * The same goes for freeing the per_cpu data of the control
2679 * Again, normal synchronize_sched() is not good enough.
2680 * We need to do a hard force of sched synchronization.
2681 * This is because we use preempt_disable() to do RCU, but
2682 * the function tracers can be called where RCU is not watching
2683 * (like before user_exit()). We can not rely on the RCU
2684 * infrastructure to do the synchronization, thus we must do it
2687 if (ops->flags & (FTRACE_OPS_FL_DYNAMIC | FTRACE_OPS_FL_CONTROL)) {
2688 schedule_on_each_cpu(ftrace_sync);
2690 arch_ftrace_trampoline_free(ops);
2692 if (ops->flags & FTRACE_OPS_FL_CONTROL)
2693 control_ops_free(ops);
2699 static void ftrace_startup_sysctl(void)
2703 if (unlikely(ftrace_disabled))
2706 /* Force update next time */
2707 saved_ftrace_func = NULL;
2708 /* ftrace_start_up is true if we want ftrace running */
2709 if (ftrace_start_up) {
2710 command = FTRACE_UPDATE_CALLS;
2711 if (ftrace_graph_active)
2712 command |= FTRACE_START_FUNC_RET;
2713 ftrace_startup_enable(command);
2717 static void ftrace_shutdown_sysctl(void)
2721 if (unlikely(ftrace_disabled))
2724 /* ftrace_start_up is true if ftrace is running */
2725 if (ftrace_start_up) {
2726 command = FTRACE_DISABLE_CALLS;
2727 if (ftrace_graph_active)
2728 command |= FTRACE_STOP_FUNC_RET;
2729 ftrace_run_update_code(command);
2733 static cycle_t ftrace_update_time;
2734 unsigned long ftrace_update_tot_cnt;
2736 static inline int ops_traces_mod(struct ftrace_ops *ops)
2739 * Filter_hash being empty will default to trace module.
2740 * But notrace hash requires a test of individual module functions.
2742 return ftrace_hash_empty(ops->func_hash->filter_hash) &&
2743 ftrace_hash_empty(ops->func_hash->notrace_hash);
2747 * Check if the current ops references the record.
2749 * If the ops traces all functions, then it was already accounted for.
2750 * If the ops does not trace the current record function, skip it.
2751 * If the ops ignores the function via notrace filter, skip it.
2754 ops_references_rec(struct ftrace_ops *ops, struct dyn_ftrace *rec)
2756 /* If ops isn't enabled, ignore it */
2757 if (!(ops->flags & FTRACE_OPS_FL_ENABLED))
2760 /* If ops traces all mods, we already accounted for it */
2761 if (ops_traces_mod(ops))
2764 /* The function must be in the filter */
2765 if (!ftrace_hash_empty(ops->func_hash->filter_hash) &&
2766 !ftrace_lookup_ip(ops->func_hash->filter_hash, rec->ip))
2769 /* If in notrace hash, we ignore it too */
2770 if (ftrace_lookup_ip(ops->func_hash->notrace_hash, rec->ip))
2776 static int referenced_filters(struct dyn_ftrace *rec)
2778 struct ftrace_ops *ops;
2781 for (ops = ftrace_ops_list; ops != &ftrace_list_end; ops = ops->next) {
2782 if (ops_references_rec(ops, rec))
2789 static int ftrace_update_code(struct module *mod, struct ftrace_page *new_pgs)
2791 struct ftrace_page *pg;
2792 struct dyn_ftrace *p;
2793 cycle_t start, stop;
2794 unsigned long update_cnt = 0;
2795 unsigned long ref = 0;
2800 * When adding a module, we need to check if tracers are
2801 * currently enabled and if they are set to trace all functions.
2802 * If they are, we need to enable the module functions as well
2803 * as update the reference counts for those function records.
2806 struct ftrace_ops *ops;
2808 for (ops = ftrace_ops_list;
2809 ops != &ftrace_list_end; ops = ops->next) {
2810 if (ops->flags & FTRACE_OPS_FL_ENABLED) {
2811 if (ops_traces_mod(ops))
2819 start = ftrace_now(raw_smp_processor_id());
2821 for (pg = new_pgs; pg; pg = pg->next) {
2823 for (i = 0; i < pg->index; i++) {
2826 /* If something went wrong, bail without enabling anything */
2827 if (unlikely(ftrace_disabled))
2830 p = &pg->records[i];
2832 cnt += referenced_filters(p);
2836 * Do the initial record conversion from mcount jump
2837 * to the NOP instructions.
2839 if (!ftrace_code_disable(mod, p))
2845 * If the tracing is enabled, go ahead and enable the record.
2847 * The reason not to enable the record immediatelly is the
2848 * inherent check of ftrace_make_nop/ftrace_make_call for
2849 * correct previous instructions. Making first the NOP
2850 * conversion puts the module to the correct state, thus
2851 * passing the ftrace_make_call check.
2853 if (ftrace_start_up && cnt) {
2854 int failed = __ftrace_replace_code(p, 1);
2856 ftrace_bug(failed, p);
2861 stop = ftrace_now(raw_smp_processor_id());
2862 ftrace_update_time = stop - start;
2863 ftrace_update_tot_cnt += update_cnt;
2868 static int ftrace_allocate_records(struct ftrace_page *pg, int count)
2873 if (WARN_ON(!count))
2876 order = get_count_order(DIV_ROUND_UP(count, ENTRIES_PER_PAGE));
2879 * We want to fill as much as possible. No more than a page
2882 while ((PAGE_SIZE << order) / ENTRY_SIZE >= count + ENTRIES_PER_PAGE)
2886 pg->records = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, order);
2889 /* if we can't allocate this size, try something smaller */
2896 cnt = (PAGE_SIZE << order) / ENTRY_SIZE;
2905 static struct ftrace_page *
2906 ftrace_allocate_pages(unsigned long num_to_init)
2908 struct ftrace_page *start_pg;
2909 struct ftrace_page *pg;
2916 start_pg = pg = kzalloc(sizeof(*pg), GFP_KERNEL);
2921 * Try to allocate as much as possible in one continues
2922 * location that fills in all of the space. We want to
2923 * waste as little space as possible.
2926 cnt = ftrace_allocate_records(pg, num_to_init);
2934 pg->next = kzalloc(sizeof(*pg), GFP_KERNEL);
2946 order = get_count_order(pg->size / ENTRIES_PER_PAGE);
2947 free_pages((unsigned long)pg->records, order);
2948 start_pg = pg->next;
2952 pr_info("ftrace: FAILED to allocate memory for functions\n");
2956 #define FTRACE_BUFF_MAX (KSYM_SYMBOL_LEN+4) /* room for wildcards */
2958 struct ftrace_iterator {
2961 struct ftrace_page *pg;
2962 struct dyn_ftrace *func;
2963 struct ftrace_func_probe *probe;
2964 struct trace_parser parser;
2965 struct ftrace_hash *hash;
2966 struct ftrace_ops *ops;
2973 t_hash_next(struct seq_file *m, loff_t *pos)
2975 struct ftrace_iterator *iter = m->private;
2976 struct hlist_node *hnd = NULL;
2977 struct hlist_head *hhd;
2983 hnd = &iter->probe->node;
2985 if (iter->hidx >= FTRACE_FUNC_HASHSIZE)
2988 hhd = &ftrace_func_hash[iter->hidx];
2990 if (hlist_empty(hhd)) {
3006 if (WARN_ON_ONCE(!hnd))
3009 iter->probe = hlist_entry(hnd, struct ftrace_func_probe, node);
3014 static void *t_hash_start(struct seq_file *m, loff_t *pos)
3016 struct ftrace_iterator *iter = m->private;
3020 if (!(iter->flags & FTRACE_ITER_DO_HASH))
3023 if (iter->func_pos > *pos)
3027 for (l = 0; l <= (*pos - iter->func_pos); ) {
3028 p = t_hash_next(m, &l);
3035 /* Only set this if we have an item */
3036 iter->flags |= FTRACE_ITER_HASH;
3042 t_hash_show(struct seq_file *m, struct ftrace_iterator *iter)
3044 struct ftrace_func_probe *rec;
3047 if (WARN_ON_ONCE(!rec))
3050 if (rec->ops->print)
3051 return rec->ops->print(m, rec->ip, rec->ops, rec->data);
3053 seq_printf(m, "%ps:%ps", (void *)rec->ip, (void *)rec->ops->func);
3056 seq_printf(m, ":%p", rec->data);
3063 t_next(struct seq_file *m, void *v, loff_t *pos)
3065 struct ftrace_iterator *iter = m->private;
3066 struct ftrace_ops *ops = iter->ops;
3067 struct dyn_ftrace *rec = NULL;
3069 if (unlikely(ftrace_disabled))
3072 if (iter->flags & FTRACE_ITER_HASH)
3073 return t_hash_next(m, pos);
3076 iter->pos = iter->func_pos = *pos;
3078 if (iter->flags & FTRACE_ITER_PRINTALL)
3079 return t_hash_start(m, pos);
3082 if (iter->idx >= iter->pg->index) {
3083 if (iter->pg->next) {
3084 iter->pg = iter->pg->next;
3089 rec = &iter->pg->records[iter->idx++];
3090 if (((iter->flags & FTRACE_ITER_FILTER) &&
3091 !(ftrace_lookup_ip(ops->func_hash->filter_hash, rec->ip))) ||
3093 ((iter->flags & FTRACE_ITER_NOTRACE) &&
3094 !ftrace_lookup_ip(ops->func_hash->notrace_hash, rec->ip)) ||
3096 ((iter->flags & FTRACE_ITER_ENABLED) &&
3097 !(rec->flags & FTRACE_FL_ENABLED))) {
3105 return t_hash_start(m, pos);
3112 static void reset_iter_read(struct ftrace_iterator *iter)
3116 iter->flags &= ~(FTRACE_ITER_PRINTALL | FTRACE_ITER_HASH);
3119 static void *t_start(struct seq_file *m, loff_t *pos)
3121 struct ftrace_iterator *iter = m->private;
3122 struct ftrace_ops *ops = iter->ops;
3126 mutex_lock(&ftrace_lock);
3128 if (unlikely(ftrace_disabled))
3132 * If an lseek was done, then reset and start from beginning.
3134 if (*pos < iter->pos)
3135 reset_iter_read(iter);
3138 * For set_ftrace_filter reading, if we have the filter
3139 * off, we can short cut and just print out that all
3140 * functions are enabled.
3142 if ((iter->flags & FTRACE_ITER_FILTER &&
3143 ftrace_hash_empty(ops->func_hash->filter_hash)) ||
3144 (iter->flags & FTRACE_ITER_NOTRACE &&
3145 ftrace_hash_empty(ops->func_hash->notrace_hash))) {
3147 return t_hash_start(m, pos);
3148 iter->flags |= FTRACE_ITER_PRINTALL;
3149 /* reset in case of seek/pread */
3150 iter->flags &= ~FTRACE_ITER_HASH;
3154 if (iter->flags & FTRACE_ITER_HASH)
3155 return t_hash_start(m, pos);
3158 * Unfortunately, we need to restart at ftrace_pages_start
3159 * every time we let go of the ftrace_mutex. This is because
3160 * those pointers can change without the lock.
3162 iter->pg = ftrace_pages_start;
3164 for (l = 0; l <= *pos; ) {
3165 p = t_next(m, p, &l);
3171 return t_hash_start(m, pos);
3176 static void t_stop(struct seq_file *m, void *p)
3178 mutex_unlock(&ftrace_lock);
3182 arch_ftrace_trampoline_func(struct ftrace_ops *ops, struct dyn_ftrace *rec)
3187 static void add_trampoline_func(struct seq_file *m, struct ftrace_ops *ops,
3188 struct dyn_ftrace *rec)
3192 ptr = arch_ftrace_trampoline_func(ops, rec);
3194 seq_printf(m, " ->%pS", ptr);
3197 static int t_show(struct seq_file *m, void *v)
3199 struct ftrace_iterator *iter = m->private;
3200 struct dyn_ftrace *rec;
3202 if (iter->flags & FTRACE_ITER_HASH)
3203 return t_hash_show(m, iter);
3205 if (iter->flags & FTRACE_ITER_PRINTALL) {
3206 if (iter->flags & FTRACE_ITER_NOTRACE)
3207 seq_puts(m, "#### no functions disabled ####\n");
3209 seq_puts(m, "#### all functions enabled ####\n");
3218 seq_printf(m, "%ps", (void *)rec->ip);
3219 if (iter->flags & FTRACE_ITER_ENABLED) {
3220 struct ftrace_ops *ops = NULL;
3222 seq_printf(m, " (%ld)%s%s",
3223 ftrace_rec_count(rec),
3224 rec->flags & FTRACE_FL_REGS ? " R" : " ",
3225 rec->flags & FTRACE_FL_IPMODIFY ? " I" : " ");
3226 if (rec->flags & FTRACE_FL_TRAMP_EN) {
3227 ops = ftrace_find_tramp_ops_any(rec);
3229 seq_printf(m, "\ttramp: %pS",
3230 (void *)ops->trampoline);
3232 seq_puts(m, "\ttramp: ERROR!");
3235 add_trampoline_func(m, ops, rec);
3243 static const struct seq_operations show_ftrace_seq_ops = {
3251 ftrace_avail_open(struct inode *inode, struct file *file)
3253 struct ftrace_iterator *iter;
3255 if (unlikely(ftrace_disabled))
3258 iter = __seq_open_private(file, &show_ftrace_seq_ops, sizeof(*iter));
3260 iter->pg = ftrace_pages_start;
3261 iter->ops = &global_ops;
3264 return iter ? 0 : -ENOMEM;
3268 ftrace_enabled_open(struct inode *inode, struct file *file)
3270 struct ftrace_iterator *iter;
3272 iter = __seq_open_private(file, &show_ftrace_seq_ops, sizeof(*iter));
3274 iter->pg = ftrace_pages_start;
3275 iter->flags = FTRACE_ITER_ENABLED;
3276 iter->ops = &global_ops;
3279 return iter ? 0 : -ENOMEM;
3283 * ftrace_regex_open - initialize function tracer filter files
3284 * @ops: The ftrace_ops that hold the hash filters
3285 * @flag: The type of filter to process
3286 * @inode: The inode, usually passed in to your open routine
3287 * @file: The file, usually passed in to your open routine
3289 * ftrace_regex_open() initializes the filter files for the
3290 * @ops. Depending on @flag it may process the filter hash or
3291 * the notrace hash of @ops. With this called from the open
3292 * routine, you can use ftrace_filter_write() for the write
3293 * routine if @flag has FTRACE_ITER_FILTER set, or
3294 * ftrace_notrace_write() if @flag has FTRACE_ITER_NOTRACE set.
3295 * tracing_lseek() should be used as the lseek routine, and
3296 * release must call ftrace_regex_release().
3299 ftrace_regex_open(struct ftrace_ops *ops, int flag,
3300 struct inode *inode, struct file *file)
3302 struct ftrace_iterator *iter;
3303 struct ftrace_hash *hash;
3306 ftrace_ops_init(ops);
3308 if (unlikely(ftrace_disabled))
3311 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
3315 if (trace_parser_get_init(&iter->parser, FTRACE_BUFF_MAX)) {
3323 mutex_lock(&ops->func_hash->regex_lock);
3325 if (flag & FTRACE_ITER_NOTRACE)
3326 hash = ops->func_hash->notrace_hash;
3328 hash = ops->func_hash->filter_hash;
3330 if (file->f_mode & FMODE_WRITE) {
3331 const int size_bits = FTRACE_HASH_DEFAULT_BITS;
3333 if (file->f_flags & O_TRUNC)
3334 iter->hash = alloc_ftrace_hash(size_bits);
3336 iter->hash = alloc_and_copy_ftrace_hash(size_bits, hash);
3339 trace_parser_put(&iter->parser);
3346 if (file->f_mode & FMODE_READ) {
3347 iter->pg = ftrace_pages_start;
3349 ret = seq_open(file, &show_ftrace_seq_ops);
3351 struct seq_file *m = file->private_data;
3355 free_ftrace_hash(iter->hash);
3356 trace_parser_put(&iter->parser);
3360 file->private_data = iter;
3363 mutex_unlock(&ops->func_hash->regex_lock);
3369 ftrace_filter_open(struct inode *inode, struct file *file)
3371 struct ftrace_ops *ops = inode->i_private;
3373 return ftrace_regex_open(ops,
3374 FTRACE_ITER_FILTER | FTRACE_ITER_DO_HASH,
3379 ftrace_notrace_open(struct inode *inode, struct file *file)
3381 struct ftrace_ops *ops = inode->i_private;
3383 return ftrace_regex_open(ops, FTRACE_ITER_NOTRACE,
3387 static int ftrace_match(char *str, char *regex, int len, int type)
3394 if (strcmp(str, regex) == 0)
3397 case MATCH_FRONT_ONLY:
3398 if (strncmp(str, regex, len) == 0)
3401 case MATCH_MIDDLE_ONLY:
3402 if (strstr(str, regex))
3405 case MATCH_END_ONLY:
3407 if (slen >= len && memcmp(str + slen - len, regex, len) == 0)
3416 enter_record(struct ftrace_hash *hash, struct dyn_ftrace *rec, int not)
3418 struct ftrace_func_entry *entry;
3421 entry = ftrace_lookup_ip(hash, rec->ip);
3423 /* Do nothing if it doesn't exist */
3427 free_hash_entry(hash, entry);
3429 /* Do nothing if it exists */
3433 ret = add_hash_entry(hash, rec->ip);
3439 ftrace_match_record(struct dyn_ftrace *rec, char *mod,
3440 char *regex, int len, int type)
3442 char str[KSYM_SYMBOL_LEN];
3445 kallsyms_lookup(rec->ip, NULL, NULL, &modname, str);
3448 /* module lookup requires matching the module */
3449 if (!modname || strcmp(modname, mod))
3452 /* blank search means to match all funcs in the mod */
3457 return ftrace_match(str, regex, len, type);
3461 match_records(struct ftrace_hash *hash, char *buff,
3462 int len, char *mod, int not)
3464 unsigned search_len = 0;
3465 struct ftrace_page *pg;
3466 struct dyn_ftrace *rec;
3467 int type = MATCH_FULL;
3468 char *search = buff;
3473 type = filter_parse_regex(buff, len, &search, ¬);
3474 search_len = strlen(search);
3477 mutex_lock(&ftrace_lock);
3479 if (unlikely(ftrace_disabled))
3482 do_for_each_ftrace_rec(pg, rec) {
3483 if (ftrace_match_record(rec, mod, search, search_len, type)) {
3484 ret = enter_record(hash, rec, not);
3491 } while_for_each_ftrace_rec();
3493 mutex_unlock(&ftrace_lock);
3499 ftrace_match_records(struct ftrace_hash *hash, char *buff, int len)
3501 return match_records(hash, buff, len, NULL, 0);
3505 ftrace_match_module_records(struct ftrace_hash *hash, char *buff, char *mod)
3509 /* blank or '*' mean the same */
3510 if (strcmp(buff, "*") == 0)
3513 /* handle the case of 'dont filter this module' */
3514 if (strcmp(buff, "!") == 0 || strcmp(buff, "!*") == 0) {
3519 return match_records(hash, buff, strlen(buff), mod, not);
3523 * We register the module command as a template to show others how
3524 * to register the a command as well.
3528 ftrace_mod_callback(struct ftrace_hash *hash,
3529 char *func, char *cmd, char *param, int enable)
3535 * cmd == 'mod' because we only registered this func
3536 * for the 'mod' ftrace_func_command.
3537 * But if you register one func with multiple commands,
3538 * you can tell which command was used by the cmd
3542 /* we must have a module name */
3546 mod = strsep(¶m, ":");
3550 ret = ftrace_match_module_records(hash, func, mod);
3559 static struct ftrace_func_command ftrace_mod_cmd = {
3561 .func = ftrace_mod_callback,
3564 static int __init ftrace_mod_cmd_init(void)
3566 return register_ftrace_command(&ftrace_mod_cmd);
3568 core_initcall(ftrace_mod_cmd_init);
3570 static void function_trace_probe_call(unsigned long ip, unsigned long parent_ip,
3571 struct ftrace_ops *op, struct pt_regs *pt_regs)
3573 struct ftrace_func_probe *entry;
3574 struct hlist_head *hhd;
3577 key = hash_long(ip, FTRACE_HASH_BITS);
3579 hhd = &ftrace_func_hash[key];
3581 if (hlist_empty(hhd))
3585 * Disable preemption for these calls to prevent a RCU grace
3586 * period. This syncs the hash iteration and freeing of items
3587 * on the hash. rcu_read_lock is too dangerous here.
3589 preempt_disable_notrace();
3590 hlist_for_each_entry_rcu_notrace(entry, hhd, node) {
3591 if (entry->ip == ip)
3592 entry->ops->func(ip, parent_ip, &entry->data);
3594 preempt_enable_notrace();
3597 static struct ftrace_ops trace_probe_ops __read_mostly =
3599 .func = function_trace_probe_call,
3600 .flags = FTRACE_OPS_FL_INITIALIZED,
3601 INIT_OPS_HASH(trace_probe_ops)
3604 static int ftrace_probe_registered;
3606 static void __enable_ftrace_function_probe(struct ftrace_ops_hash *old_hash)
3611 if (ftrace_probe_registered) {
3612 /* still need to update the function call sites */
3614 ftrace_run_modify_code(&trace_probe_ops, FTRACE_UPDATE_CALLS,
3619 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
3620 struct hlist_head *hhd = &ftrace_func_hash[i];
3624 /* Nothing registered? */
3625 if (i == FTRACE_FUNC_HASHSIZE)
3628 ret = ftrace_startup(&trace_probe_ops, 0);
3630 ftrace_probe_registered = 1;
3633 static void __disable_ftrace_function_probe(void)
3637 if (!ftrace_probe_registered)
3640 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
3641 struct hlist_head *hhd = &ftrace_func_hash[i];
3646 /* no more funcs left */
3647 ftrace_shutdown(&trace_probe_ops, 0);
3649 ftrace_probe_registered = 0;
3653 static void ftrace_free_entry(struct ftrace_func_probe *entry)
3655 if (entry->ops->free)
3656 entry->ops->free(entry->ops, entry->ip, &entry->data);
3661 register_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
3664 struct ftrace_ops_hash old_hash_ops;
3665 struct ftrace_func_probe *entry;
3666 struct ftrace_hash **orig_hash = &trace_probe_ops.func_hash->filter_hash;
3667 struct ftrace_hash *old_hash = *orig_hash;
3668 struct ftrace_hash *hash;
3669 struct ftrace_page *pg;
3670 struct dyn_ftrace *rec;
3677 type = filter_parse_regex(glob, strlen(glob), &search, ¬);
3678 len = strlen(search);
3680 /* we do not support '!' for function probes */
3684 mutex_lock(&trace_probe_ops.func_hash->regex_lock);
3686 old_hash_ops.filter_hash = old_hash;
3687 /* Probes only have filters */
3688 old_hash_ops.notrace_hash = NULL;
3690 hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, old_hash);
3696 if (unlikely(ftrace_disabled)) {
3701 mutex_lock(&ftrace_lock);
3703 do_for_each_ftrace_rec(pg, rec) {
3705 if (!ftrace_match_record(rec, NULL, search, len, type))
3708 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
3710 /* If we did not process any, then return error */
3721 * The caller might want to do something special
3722 * for each function we find. We call the callback
3723 * to give the caller an opportunity to do so.
3726 if (ops->init(ops, rec->ip, &entry->data) < 0) {
3727 /* caller does not like this func */
3733 ret = enter_record(hash, rec, 0);
3741 entry->ip = rec->ip;
3743 key = hash_long(entry->ip, FTRACE_HASH_BITS);
3744 hlist_add_head_rcu(&entry->node, &ftrace_func_hash[key]);
3746 } while_for_each_ftrace_rec();
3748 ret = ftrace_hash_move(&trace_probe_ops, 1, orig_hash, hash);
3750 __enable_ftrace_function_probe(&old_hash_ops);
3753 free_ftrace_hash_rcu(old_hash);
3758 mutex_unlock(&ftrace_lock);
3760 mutex_unlock(&trace_probe_ops.func_hash->regex_lock);
3761 free_ftrace_hash(hash);
3767 PROBE_TEST_FUNC = 1,
3772 __unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
3773 void *data, int flags)
3775 struct ftrace_func_entry *rec_entry;
3776 struct ftrace_func_probe *entry;
3777 struct ftrace_func_probe *p;
3778 struct ftrace_hash **orig_hash = &trace_probe_ops.func_hash->filter_hash;
3779 struct ftrace_hash *old_hash = *orig_hash;
3780 struct list_head free_list;
3781 struct ftrace_hash *hash;
3782 struct hlist_node *tmp;
3783 char str[KSYM_SYMBOL_LEN];
3784 int type = MATCH_FULL;
3789 if (glob && (strcmp(glob, "*") == 0 || !strlen(glob)))
3794 type = filter_parse_regex(glob, strlen(glob), &search, ¬);
3795 len = strlen(search);
3797 /* we do not support '!' for function probes */
3802 mutex_lock(&trace_probe_ops.func_hash->regex_lock);
3804 hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, *orig_hash);
3806 /* Hmm, should report this somehow */
3809 INIT_LIST_HEAD(&free_list);
3811 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
3812 struct hlist_head *hhd = &ftrace_func_hash[i];
3814 hlist_for_each_entry_safe(entry, tmp, hhd, node) {
3816 /* break up if statements for readability */
3817 if ((flags & PROBE_TEST_FUNC) && entry->ops != ops)
3820 if ((flags & PROBE_TEST_DATA) && entry->data != data)
3823 /* do this last, since it is the most expensive */
3825 kallsyms_lookup(entry->ip, NULL, NULL,
3827 if (!ftrace_match(str, glob, len, type))
3831 rec_entry = ftrace_lookup_ip(hash, entry->ip);
3832 /* It is possible more than one entry had this ip */
3834 free_hash_entry(hash, rec_entry);
3836 hlist_del_rcu(&entry->node);
3837 list_add(&entry->free_list, &free_list);
3840 mutex_lock(&ftrace_lock);
3841 __disable_ftrace_function_probe();
3843 * Remove after the disable is called. Otherwise, if the last
3844 * probe is removed, a null hash means *all enabled*.
3846 ret = ftrace_hash_move(&trace_probe_ops, 1, orig_hash, hash);
3847 synchronize_sched();
3849 free_ftrace_hash_rcu(old_hash);
3851 list_for_each_entry_safe(entry, p, &free_list, free_list) {
3852 list_del(&entry->free_list);
3853 ftrace_free_entry(entry);
3855 mutex_unlock(&ftrace_lock);
3858 mutex_unlock(&trace_probe_ops.func_hash->regex_lock);
3859 free_ftrace_hash(hash);
3863 unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
3866 __unregister_ftrace_function_probe(glob, ops, data,
3867 PROBE_TEST_FUNC | PROBE_TEST_DATA);
3871 unregister_ftrace_function_probe_func(char *glob, struct ftrace_probe_ops *ops)
3873 __unregister_ftrace_function_probe(glob, ops, NULL, PROBE_TEST_FUNC);
3876 void unregister_ftrace_function_probe_all(char *glob)
3878 __unregister_ftrace_function_probe(glob, NULL, NULL, 0);
3881 static LIST_HEAD(ftrace_commands);
3882 static DEFINE_MUTEX(ftrace_cmd_mutex);
3885 * Currently we only register ftrace commands from __init, so mark this
3888 __init int register_ftrace_command(struct ftrace_func_command *cmd)
3890 struct ftrace_func_command *p;
3893 mutex_lock(&ftrace_cmd_mutex);
3894 list_for_each_entry(p, &ftrace_commands, list) {
3895 if (strcmp(cmd->name, p->name) == 0) {
3900 list_add(&cmd->list, &ftrace_commands);
3902 mutex_unlock(&ftrace_cmd_mutex);
3908 * Currently we only unregister ftrace commands from __init, so mark
3911 __init int unregister_ftrace_command(struct ftrace_func_command *cmd)
3913 struct ftrace_func_command *p, *n;
3916 mutex_lock(&ftrace_cmd_mutex);
3917 list_for_each_entry_safe(p, n, &ftrace_commands, list) {
3918 if (strcmp(cmd->name, p->name) == 0) {
3920 list_del_init(&p->list);
3925 mutex_unlock(&ftrace_cmd_mutex);
3930 static int ftrace_process_regex(struct ftrace_hash *hash,
3931 char *buff, int len, int enable)
3933 char *func, *command, *next = buff;
3934 struct ftrace_func_command *p;
3937 func = strsep(&next, ":");
3940 ret = ftrace_match_records(hash, func, len);
3950 command = strsep(&next, ":");
3952 mutex_lock(&ftrace_cmd_mutex);
3953 list_for_each_entry(p, &ftrace_commands, list) {
3954 if (strcmp(p->name, command) == 0) {
3955 ret = p->func(hash, func, command, next, enable);
3960 mutex_unlock(&ftrace_cmd_mutex);
3966 ftrace_regex_write(struct file *file, const char __user *ubuf,
3967 size_t cnt, loff_t *ppos, int enable)
3969 struct ftrace_iterator *iter;
3970 struct trace_parser *parser;
3976 if (file->f_mode & FMODE_READ) {
3977 struct seq_file *m = file->private_data;
3980 iter = file->private_data;
3982 if (unlikely(ftrace_disabled))
3985 /* iter->hash is a local copy, so we don't need regex_lock */
3987 parser = &iter->parser;
3988 read = trace_get_user(parser, ubuf, cnt, ppos);
3990 if (read >= 0 && trace_parser_loaded(parser) &&
3991 !trace_parser_cont(parser)) {
3992 ret = ftrace_process_regex(iter->hash, parser->buffer,
3993 parser->idx, enable);
3994 trace_parser_clear(parser);
4005 ftrace_filter_write(struct file *file, const char __user *ubuf,
4006 size_t cnt, loff_t *ppos)
4008 return ftrace_regex_write(file, ubuf, cnt, ppos, 1);
4012 ftrace_notrace_write(struct file *file, const char __user *ubuf,
4013 size_t cnt, loff_t *ppos)
4015 return ftrace_regex_write(file, ubuf, cnt, ppos, 0);
4019 ftrace_match_addr(struct ftrace_hash *hash, unsigned long ip, int remove)
4021 struct ftrace_func_entry *entry;
4023 if (!ftrace_location(ip))
4027 entry = ftrace_lookup_ip(hash, ip);
4030 free_hash_entry(hash, entry);
4034 return add_hash_entry(hash, ip);
4037 static void ftrace_ops_update_code(struct ftrace_ops *ops,
4038 struct ftrace_ops_hash *old_hash)
4040 struct ftrace_ops *op;
4042 if (!ftrace_enabled)
4045 if (ops->flags & FTRACE_OPS_FL_ENABLED) {
4046 ftrace_run_modify_code(ops, FTRACE_UPDATE_CALLS, old_hash);
4051 * If this is the shared global_ops filter, then we need to
4052 * check if there is another ops that shares it, is enabled.
4053 * If so, we still need to run the modify code.
4055 if (ops->func_hash != &global_ops.local_hash)
4058 do_for_each_ftrace_op(op, ftrace_ops_list) {
4059 if (op->func_hash == &global_ops.local_hash &&
4060 op->flags & FTRACE_OPS_FL_ENABLED) {
4061 ftrace_run_modify_code(op, FTRACE_UPDATE_CALLS, old_hash);
4062 /* Only need to do this once */
4065 } while_for_each_ftrace_op(op);
4069 ftrace_set_hash(struct ftrace_ops *ops, unsigned char *buf, int len,
4070 unsigned long ip, int remove, int reset, int enable)
4072 struct ftrace_hash **orig_hash;
4073 struct ftrace_ops_hash old_hash_ops;
4074 struct ftrace_hash *old_hash;
4075 struct ftrace_hash *hash;
4078 if (unlikely(ftrace_disabled))
4081 mutex_lock(&ops->func_hash->regex_lock);
4084 orig_hash = &ops->func_hash->filter_hash;
4086 orig_hash = &ops->func_hash->notrace_hash;
4089 hash = alloc_ftrace_hash(FTRACE_HASH_DEFAULT_BITS);
4091 hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, *orig_hash);
4095 goto out_regex_unlock;
4098 if (buf && !ftrace_match_records(hash, buf, len)) {
4100 goto out_regex_unlock;
4103 ret = ftrace_match_addr(hash, ip, remove);
4105 goto out_regex_unlock;
4108 mutex_lock(&ftrace_lock);
4109 old_hash = *orig_hash;
4110 old_hash_ops.filter_hash = ops->func_hash->filter_hash;
4111 old_hash_ops.notrace_hash = ops->func_hash->notrace_hash;
4112 ret = ftrace_hash_move(ops, enable, orig_hash, hash);
4114 ftrace_ops_update_code(ops, &old_hash_ops);
4115 free_ftrace_hash_rcu(old_hash);
4117 mutex_unlock(&ftrace_lock);
4120 mutex_unlock(&ops->func_hash->regex_lock);
4122 free_ftrace_hash(hash);
4127 ftrace_set_addr(struct ftrace_ops *ops, unsigned long ip, int remove,
4128 int reset, int enable)
4130 return ftrace_set_hash(ops, 0, 0, ip, remove, reset, enable);
4134 * ftrace_set_filter_ip - set a function to filter on in ftrace by address
4135 * @ops - the ops to set the filter with
4136 * @ip - the address to add to or remove from the filter.
4137 * @remove - non zero to remove the ip from the filter
4138 * @reset - non zero to reset all filters before applying this filter.
4140 * Filters denote which functions should be enabled when tracing is enabled
4141 * If @ip is NULL, it failes to update filter.
4143 int ftrace_set_filter_ip(struct ftrace_ops *ops, unsigned long ip,
4144 int remove, int reset)
4146 ftrace_ops_init(ops);
4147 return ftrace_set_addr(ops, ip, remove, reset, 1);
4149 EXPORT_SYMBOL_GPL(ftrace_set_filter_ip);
4152 ftrace_set_regex(struct ftrace_ops *ops, unsigned char *buf, int len,
4153 int reset, int enable)
4155 return ftrace_set_hash(ops, buf, len, 0, 0, reset, enable);
4159 * ftrace_set_filter - set a function to filter on in ftrace
4160 * @ops - the ops to set the filter with
4161 * @buf - the string that holds the function filter text.
4162 * @len - the length of the string.
4163 * @reset - non zero to reset all filters before applying this filter.
4165 * Filters denote which functions should be enabled when tracing is enabled.
4166 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
4168 int ftrace_set_filter(struct ftrace_ops *ops, unsigned char *buf,
4171 ftrace_ops_init(ops);
4172 return ftrace_set_regex(ops, buf, len, reset, 1);
4174 EXPORT_SYMBOL_GPL(ftrace_set_filter);
4177 * ftrace_set_notrace - set a function to not trace in ftrace
4178 * @ops - the ops to set the notrace filter with
4179 * @buf - the string that holds the function notrace text.
4180 * @len - the length of the string.
4181 * @reset - non zero to reset all filters before applying this filter.
4183 * Notrace Filters denote which functions should not be enabled when tracing
4184 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
4187 int ftrace_set_notrace(struct ftrace_ops *ops, unsigned char *buf,
4190 ftrace_ops_init(ops);
4191 return ftrace_set_regex(ops, buf, len, reset, 0);
4193 EXPORT_SYMBOL_GPL(ftrace_set_notrace);
4195 * ftrace_set_global_filter - set a function to filter on with global tracers
4196 * @buf - the string that holds the function filter text.
4197 * @len - the length of the string.
4198 * @reset - non zero to reset all filters before applying this filter.
4200 * Filters denote which functions should be enabled when tracing is enabled.
4201 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
4203 void ftrace_set_global_filter(unsigned char *buf, int len, int reset)
4205 ftrace_set_regex(&global_ops, buf, len, reset, 1);
4207 EXPORT_SYMBOL_GPL(ftrace_set_global_filter);
4210 * ftrace_set_global_notrace - set a function to not trace with global tracers
4211 * @buf - the string that holds the function notrace text.
4212 * @len - the length of the string.
4213 * @reset - non zero to reset all filters before applying this filter.
4215 * Notrace Filters denote which functions should not be enabled when tracing
4216 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
4219 void ftrace_set_global_notrace(unsigned char *buf, int len, int reset)
4221 ftrace_set_regex(&global_ops, buf, len, reset, 0);
4223 EXPORT_SYMBOL_GPL(ftrace_set_global_notrace);
4226 * command line interface to allow users to set filters on boot up.
4228 #define FTRACE_FILTER_SIZE COMMAND_LINE_SIZE
4229 static char ftrace_notrace_buf[FTRACE_FILTER_SIZE] __initdata;
4230 static char ftrace_filter_buf[FTRACE_FILTER_SIZE] __initdata;
4232 /* Used by function selftest to not test if filter is set */
4233 bool ftrace_filter_param __initdata;
4235 static int __init set_ftrace_notrace(char *str)
4237 ftrace_filter_param = true;
4238 strlcpy(ftrace_notrace_buf, str, FTRACE_FILTER_SIZE);
4241 __setup("ftrace_notrace=", set_ftrace_notrace);
4243 static int __init set_ftrace_filter(char *str)
4245 ftrace_filter_param = true;
4246 strlcpy(ftrace_filter_buf, str, FTRACE_FILTER_SIZE);
4249 __setup("ftrace_filter=", set_ftrace_filter);
4251 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
4252 static char ftrace_graph_buf[FTRACE_FILTER_SIZE] __initdata;
4253 static char ftrace_graph_notrace_buf[FTRACE_FILTER_SIZE] __initdata;
4254 static int ftrace_set_func(unsigned long *array, int *idx, int size, char *buffer);
4256 static unsigned long save_global_trampoline;
4257 static unsigned long save_global_flags;
4259 static int __init set_graph_function(char *str)
4261 strlcpy(ftrace_graph_buf, str, FTRACE_FILTER_SIZE);
4264 __setup("ftrace_graph_filter=", set_graph_function);
4266 static int __init set_graph_notrace_function(char *str)
4268 strlcpy(ftrace_graph_notrace_buf, str, FTRACE_FILTER_SIZE);
4271 __setup("ftrace_graph_notrace=", set_graph_notrace_function);
4273 static void __init set_ftrace_early_graph(char *buf, int enable)
4277 unsigned long *table = ftrace_graph_funcs;
4278 int *count = &ftrace_graph_count;
4281 table = ftrace_graph_notrace_funcs;
4282 count = &ftrace_graph_notrace_count;
4286 func = strsep(&buf, ",");
4287 /* we allow only one expression at a time */
4288 ret = ftrace_set_func(table, count, FTRACE_GRAPH_MAX_FUNCS, func);
4290 printk(KERN_DEBUG "ftrace: function %s not "
4291 "traceable\n", func);
4294 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
4297 ftrace_set_early_filter(struct ftrace_ops *ops, char *buf, int enable)
4301 ftrace_ops_init(ops);
4304 func = strsep(&buf, ",");
4305 ftrace_set_regex(ops, func, strlen(func), 0, enable);
4309 static void __init set_ftrace_early_filters(void)
4311 if (ftrace_filter_buf[0])
4312 ftrace_set_early_filter(&global_ops, ftrace_filter_buf, 1);
4313 if (ftrace_notrace_buf[0])
4314 ftrace_set_early_filter(&global_ops, ftrace_notrace_buf, 0);
4315 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
4316 if (ftrace_graph_buf[0])
4317 set_ftrace_early_graph(ftrace_graph_buf, 1);
4318 if (ftrace_graph_notrace_buf[0])
4319 set_ftrace_early_graph(ftrace_graph_notrace_buf, 0);
4320 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
4323 int ftrace_regex_release(struct inode *inode, struct file *file)
4325 struct seq_file *m = (struct seq_file *)file->private_data;
4326 struct ftrace_ops_hash old_hash_ops;
4327 struct ftrace_iterator *iter;
4328 struct ftrace_hash **orig_hash;
4329 struct ftrace_hash *old_hash;
4330 struct trace_parser *parser;
4334 if (file->f_mode & FMODE_READ) {
4336 seq_release(inode, file);
4338 iter = file->private_data;
4340 parser = &iter->parser;
4341 if (trace_parser_loaded(parser)) {
4342 parser->buffer[parser->idx] = 0;
4343 ftrace_match_records(iter->hash, parser->buffer, parser->idx);
4346 trace_parser_put(parser);
4348 mutex_lock(&iter->ops->func_hash->regex_lock);
4350 if (file->f_mode & FMODE_WRITE) {
4351 filter_hash = !!(iter->flags & FTRACE_ITER_FILTER);
4354 orig_hash = &iter->ops->func_hash->filter_hash;
4356 orig_hash = &iter->ops->func_hash->notrace_hash;
4358 mutex_lock(&ftrace_lock);
4359 old_hash = *orig_hash;
4360 old_hash_ops.filter_hash = iter->ops->func_hash->filter_hash;
4361 old_hash_ops.notrace_hash = iter->ops->func_hash->notrace_hash;
4362 ret = ftrace_hash_move(iter->ops, filter_hash,
4363 orig_hash, iter->hash);
4365 ftrace_ops_update_code(iter->ops, &old_hash_ops);
4366 free_ftrace_hash_rcu(old_hash);
4368 mutex_unlock(&ftrace_lock);
4371 mutex_unlock(&iter->ops->func_hash->regex_lock);
4372 free_ftrace_hash(iter->hash);
4378 static const struct file_operations ftrace_avail_fops = {
4379 .open = ftrace_avail_open,
4381 .llseek = seq_lseek,
4382 .release = seq_release_private,
4385 static const struct file_operations ftrace_enabled_fops = {
4386 .open = ftrace_enabled_open,
4388 .llseek = seq_lseek,
4389 .release = seq_release_private,
4392 static const struct file_operations ftrace_filter_fops = {
4393 .open = ftrace_filter_open,
4395 .write = ftrace_filter_write,
4396 .llseek = tracing_lseek,
4397 .release = ftrace_regex_release,
4400 static const struct file_operations ftrace_notrace_fops = {
4401 .open = ftrace_notrace_open,
4403 .write = ftrace_notrace_write,
4404 .llseek = tracing_lseek,
4405 .release = ftrace_regex_release,
4408 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
4410 static DEFINE_MUTEX(graph_lock);
4412 int ftrace_graph_count;
4413 int ftrace_graph_notrace_count;
4414 unsigned long ftrace_graph_funcs[FTRACE_GRAPH_MAX_FUNCS] __read_mostly;
4415 unsigned long ftrace_graph_notrace_funcs[FTRACE_GRAPH_MAX_FUNCS] __read_mostly;
4417 struct ftrace_graph_data {
4418 unsigned long *table;
4421 const struct seq_operations *seq_ops;
4425 __g_next(struct seq_file *m, loff_t *pos)
4427 struct ftrace_graph_data *fgd = m->private;
4429 if (*pos >= *fgd->count)
4431 return &fgd->table[*pos];
4435 g_next(struct seq_file *m, void *v, loff_t *pos)
4438 return __g_next(m, pos);
4441 static void *g_start(struct seq_file *m, loff_t *pos)
4443 struct ftrace_graph_data *fgd = m->private;
4445 mutex_lock(&graph_lock);
4447 /* Nothing, tell g_show to print all functions are enabled */
4448 if (!*fgd->count && !*pos)
4451 return __g_next(m, pos);
4454 static void g_stop(struct seq_file *m, void *p)
4456 mutex_unlock(&graph_lock);
4459 static int g_show(struct seq_file *m, void *v)
4461 unsigned long *ptr = v;
4466 if (ptr == (unsigned long *)1) {
4467 struct ftrace_graph_data *fgd = m->private;
4469 if (fgd->table == ftrace_graph_funcs)
4470 seq_puts(m, "#### all functions enabled ####\n");
4472 seq_puts(m, "#### no functions disabled ####\n");
4476 seq_printf(m, "%ps\n", (void *)*ptr);
4481 static const struct seq_operations ftrace_graph_seq_ops = {
4489 __ftrace_graph_open(struct inode *inode, struct file *file,
4490 struct ftrace_graph_data *fgd)
4494 mutex_lock(&graph_lock);
4495 if ((file->f_mode & FMODE_WRITE) &&
4496 (file->f_flags & O_TRUNC)) {
4498 memset(fgd->table, 0, fgd->size * sizeof(*fgd->table));
4500 mutex_unlock(&graph_lock);
4502 if (file->f_mode & FMODE_READ) {
4503 ret = seq_open(file, fgd->seq_ops);
4505 struct seq_file *m = file->private_data;
4509 file->private_data = fgd;
4515 ftrace_graph_open(struct inode *inode, struct file *file)
4517 struct ftrace_graph_data *fgd;
4519 if (unlikely(ftrace_disabled))
4522 fgd = kmalloc(sizeof(*fgd), GFP_KERNEL);
4526 fgd->table = ftrace_graph_funcs;
4527 fgd->size = FTRACE_GRAPH_MAX_FUNCS;
4528 fgd->count = &ftrace_graph_count;
4529 fgd->seq_ops = &ftrace_graph_seq_ops;
4531 return __ftrace_graph_open(inode, file, fgd);
4535 ftrace_graph_notrace_open(struct inode *inode, struct file *file)
4537 struct ftrace_graph_data *fgd;
4539 if (unlikely(ftrace_disabled))
4542 fgd = kmalloc(sizeof(*fgd), GFP_KERNEL);
4546 fgd->table = ftrace_graph_notrace_funcs;
4547 fgd->size = FTRACE_GRAPH_MAX_FUNCS;
4548 fgd->count = &ftrace_graph_notrace_count;
4549 fgd->seq_ops = &ftrace_graph_seq_ops;
4551 return __ftrace_graph_open(inode, file, fgd);
4555 ftrace_graph_release(struct inode *inode, struct file *file)
4557 if (file->f_mode & FMODE_READ) {
4558 struct seq_file *m = file->private_data;
4561 seq_release(inode, file);
4563 kfree(file->private_data);
4570 ftrace_set_func(unsigned long *array, int *idx, int size, char *buffer)
4572 struct dyn_ftrace *rec;
4573 struct ftrace_page *pg;
4582 type = filter_parse_regex(buffer, strlen(buffer), &search, ¬);
4583 if (!not && *idx >= size)
4586 search_len = strlen(search);
4588 mutex_lock(&ftrace_lock);
4590 if (unlikely(ftrace_disabled)) {
4591 mutex_unlock(&ftrace_lock);
4595 do_for_each_ftrace_rec(pg, rec) {
4597 if (ftrace_match_record(rec, NULL, search, search_len, type)) {
4598 /* if it is in the array */
4600 for (i = 0; i < *idx; i++) {
4601 if (array[i] == rec->ip) {
4610 array[(*idx)++] = rec->ip;
4616 array[i] = array[--(*idx)];
4622 } while_for_each_ftrace_rec();
4624 mutex_unlock(&ftrace_lock);
4633 ftrace_graph_write(struct file *file, const char __user *ubuf,
4634 size_t cnt, loff_t *ppos)
4636 struct trace_parser parser;
4637 ssize_t read, ret = 0;
4638 struct ftrace_graph_data *fgd = file->private_data;
4643 if (trace_parser_get_init(&parser, FTRACE_BUFF_MAX))
4646 read = trace_get_user(&parser, ubuf, cnt, ppos);
4648 if (read >= 0 && trace_parser_loaded((&parser))) {
4649 parser.buffer[parser.idx] = 0;
4651 mutex_lock(&graph_lock);
4653 /* we allow only one expression at a time */
4654 ret = ftrace_set_func(fgd->table, fgd->count, fgd->size,
4657 mutex_unlock(&graph_lock);
4663 trace_parser_put(&parser);
4668 static const struct file_operations ftrace_graph_fops = {
4669 .open = ftrace_graph_open,
4671 .write = ftrace_graph_write,
4672 .llseek = tracing_lseek,
4673 .release = ftrace_graph_release,
4676 static const struct file_operations ftrace_graph_notrace_fops = {
4677 .open = ftrace_graph_notrace_open,
4679 .write = ftrace_graph_write,
4680 .llseek = tracing_lseek,
4681 .release = ftrace_graph_release,
4683 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
4685 void ftrace_create_filter_files(struct ftrace_ops *ops,
4686 struct dentry *parent)
4689 trace_create_file("set_ftrace_filter", 0644, parent,
4690 ops, &ftrace_filter_fops);
4692 trace_create_file("set_ftrace_notrace", 0644, parent,
4693 ops, &ftrace_notrace_fops);
4697 * The name "destroy_filter_files" is really a misnomer. Although
4698 * in the future, it may actualy delete the files, but this is
4699 * really intended to make sure the ops passed in are disabled
4700 * and that when this function returns, the caller is free to
4703 * The "destroy" name is only to match the "create" name that this
4704 * should be paired with.
4706 void ftrace_destroy_filter_files(struct ftrace_ops *ops)
4708 mutex_lock(&ftrace_lock);
4709 if (ops->flags & FTRACE_OPS_FL_ENABLED)
4710 ftrace_shutdown(ops, 0);
4711 ops->flags |= FTRACE_OPS_FL_DELETED;
4712 mutex_unlock(&ftrace_lock);
4715 static __init int ftrace_init_dyn_debugfs(struct dentry *d_tracer)
4718 trace_create_file("available_filter_functions", 0444,
4719 d_tracer, NULL, &ftrace_avail_fops);
4721 trace_create_file("enabled_functions", 0444,
4722 d_tracer, NULL, &ftrace_enabled_fops);
4724 ftrace_create_filter_files(&global_ops, d_tracer);
4726 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
4727 trace_create_file("set_graph_function", 0444, d_tracer,
4729 &ftrace_graph_fops);
4730 trace_create_file("set_graph_notrace", 0444, d_tracer,
4732 &ftrace_graph_notrace_fops);
4733 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
4738 static int ftrace_cmp_ips(const void *a, const void *b)
4740 const unsigned long *ipa = a;
4741 const unsigned long *ipb = b;
4750 static void ftrace_swap_ips(void *a, void *b, int size)
4752 unsigned long *ipa = a;
4753 unsigned long *ipb = b;
4761 static int ftrace_process_locs(struct module *mod,
4762 unsigned long *start,
4765 struct ftrace_page *start_pg;
4766 struct ftrace_page *pg;
4767 struct dyn_ftrace *rec;
4768 unsigned long count;
4771 unsigned long flags = 0; /* Shut up gcc */
4774 count = end - start;
4779 sort(start, count, sizeof(*start),
4780 ftrace_cmp_ips, ftrace_swap_ips);
4782 start_pg = ftrace_allocate_pages(count);
4786 mutex_lock(&ftrace_lock);
4789 * Core and each module needs their own pages, as
4790 * modules will free them when they are removed.
4791 * Force a new page to be allocated for modules.
4794 WARN_ON(ftrace_pages || ftrace_pages_start);
4795 /* First initialization */
4796 ftrace_pages = ftrace_pages_start = start_pg;
4801 if (WARN_ON(ftrace_pages->next)) {
4802 /* Hmm, we have free pages? */
4803 while (ftrace_pages->next)
4804 ftrace_pages = ftrace_pages->next;
4807 ftrace_pages->next = start_pg;
4813 addr = ftrace_call_adjust(*p++);
4815 * Some architecture linkers will pad between
4816 * the different mcount_loc sections of different
4817 * object files to satisfy alignments.
4818 * Skip any NULL pointers.
4823 if (pg->index == pg->size) {
4824 /* We should have allocated enough */
4825 if (WARN_ON(!pg->next))
4830 rec = &pg->records[pg->index++];
4834 /* We should have used all pages */
4837 /* Assign the last page to ftrace_pages */
4841 * We only need to disable interrupts on start up
4842 * because we are modifying code that an interrupt
4843 * may execute, and the modification is not atomic.
4844 * But for modules, nothing runs the code we modify
4845 * until we are finished with it, and there's no
4846 * reason to cause large interrupt latencies while we do it.
4849 local_irq_save(flags);
4850 ftrace_update_code(mod, start_pg);
4852 local_irq_restore(flags);
4855 mutex_unlock(&ftrace_lock);
4860 #ifdef CONFIG_MODULES
4862 #define next_to_ftrace_page(p) container_of(p, struct ftrace_page, next)
4864 void ftrace_release_mod(struct module *mod)
4866 struct dyn_ftrace *rec;
4867 struct ftrace_page **last_pg;
4868 struct ftrace_page *pg;
4871 mutex_lock(&ftrace_lock);
4873 if (ftrace_disabled)
4877 * Each module has its own ftrace_pages, remove
4878 * them from the list.
4880 last_pg = &ftrace_pages_start;
4881 for (pg = ftrace_pages_start; pg; pg = *last_pg) {
4882 rec = &pg->records[0];
4883 if (within_module_core(rec->ip, mod)) {
4885 * As core pages are first, the first
4886 * page should never be a module page.
4888 if (WARN_ON(pg == ftrace_pages_start))
4891 /* Check if we are deleting the last page */
4892 if (pg == ftrace_pages)
4893 ftrace_pages = next_to_ftrace_page(last_pg);
4895 *last_pg = pg->next;
4896 order = get_count_order(pg->size / ENTRIES_PER_PAGE);
4897 free_pages((unsigned long)pg->records, order);
4900 last_pg = &pg->next;
4903 mutex_unlock(&ftrace_lock);
4906 static void ftrace_init_module(struct module *mod,
4907 unsigned long *start, unsigned long *end)
4909 if (ftrace_disabled || start == end)
4911 ftrace_process_locs(mod, start, end);
4914 void ftrace_module_init(struct module *mod)
4916 ftrace_init_module(mod, mod->ftrace_callsites,
4917 mod->ftrace_callsites +
4918 mod->num_ftrace_callsites);
4921 static int ftrace_module_notify_exit(struct notifier_block *self,
4922 unsigned long val, void *data)
4924 struct module *mod = data;
4926 if (val == MODULE_STATE_GOING)
4927 ftrace_release_mod(mod);
4932 static int ftrace_module_notify_exit(struct notifier_block *self,
4933 unsigned long val, void *data)
4937 #endif /* CONFIG_MODULES */
4939 struct notifier_block ftrace_module_exit_nb = {
4940 .notifier_call = ftrace_module_notify_exit,
4941 .priority = INT_MIN, /* Run after anything that can remove kprobes */
4944 void __init ftrace_init(void)
4946 extern unsigned long __start_mcount_loc[];
4947 extern unsigned long __stop_mcount_loc[];
4948 unsigned long count, flags;
4951 local_irq_save(flags);
4952 ret = ftrace_dyn_arch_init();
4953 local_irq_restore(flags);
4957 count = __stop_mcount_loc - __start_mcount_loc;
4959 pr_info("ftrace: No functions to be traced?\n");
4963 pr_info("ftrace: allocating %ld entries in %ld pages\n",
4964 count, count / ENTRIES_PER_PAGE + 1);
4966 last_ftrace_enabled = ftrace_enabled = 1;
4968 ret = ftrace_process_locs(NULL,
4972 ret = register_module_notifier(&ftrace_module_exit_nb);
4974 pr_warning("Failed to register trace ftrace module exit notifier\n");
4976 set_ftrace_early_filters();
4980 ftrace_disabled = 1;
4983 /* Do nothing if arch does not support this */
4984 void __weak arch_ftrace_update_trampoline(struct ftrace_ops *ops)
4988 static void ftrace_update_trampoline(struct ftrace_ops *ops)
4992 * Currently there's no safe way to free a trampoline when the kernel
4993 * is configured with PREEMPT. That is because a task could be preempted
4994 * when it jumped to the trampoline, it may be preempted for a long time
4995 * depending on the system load, and currently there's no way to know
4996 * when it will be off the trampoline. If the trampoline is freed
4997 * too early, when the task runs again, it will be executing on freed
5000 #ifdef CONFIG_PREEMPT
5001 /* Currently, only non dynamic ops can have a trampoline */
5002 if (ops->flags & FTRACE_OPS_FL_DYNAMIC)
5006 arch_ftrace_update_trampoline(ops);
5011 static struct ftrace_ops global_ops = {
5012 .func = ftrace_stub,
5013 .flags = FTRACE_OPS_FL_RECURSION_SAFE | FTRACE_OPS_FL_INITIALIZED,
5016 static int __init ftrace_nodyn_init(void)
5021 core_initcall(ftrace_nodyn_init);
5023 static inline int ftrace_init_dyn_debugfs(struct dentry *d_tracer) { return 0; }
5024 static inline void ftrace_startup_enable(int command) { }
5025 static inline void ftrace_startup_all(int command) { }
5026 /* Keep as macros so we do not need to define the commands */
5027 # define ftrace_startup(ops, command) \
5029 int ___ret = __register_ftrace_function(ops); \
5031 (ops)->flags |= FTRACE_OPS_FL_ENABLED; \
5034 # define ftrace_shutdown(ops, command) \
5036 int ___ret = __unregister_ftrace_function(ops); \
5038 (ops)->flags &= ~FTRACE_OPS_FL_ENABLED; \
5042 # define ftrace_startup_sysctl() do { } while (0)
5043 # define ftrace_shutdown_sysctl() do { } while (0)
5046 ftrace_ops_test(struct ftrace_ops *ops, unsigned long ip, void *regs)
5051 static void ftrace_update_trampoline(struct ftrace_ops *ops)
5055 #endif /* CONFIG_DYNAMIC_FTRACE */
5057 __init void ftrace_init_global_array_ops(struct trace_array *tr)
5059 tr->ops = &global_ops;
5060 tr->ops->private = tr;
5063 void ftrace_init_array_ops(struct trace_array *tr, ftrace_func_t func)
5065 /* If we filter on pids, update to use the pid function */
5066 if (tr->flags & TRACE_ARRAY_FL_GLOBAL) {
5067 if (WARN_ON(tr->ops->func != ftrace_stub))
5068 printk("ftrace ops had %pS for function\n",
5070 /* Only the top level instance does pid tracing */
5071 if (!list_empty(&ftrace_pids)) {
5072 set_ftrace_pid_function(func);
5073 func = ftrace_pid_func;
5076 tr->ops->func = func;
5077 tr->ops->private = tr;
5080 void ftrace_reset_array_ops(struct trace_array *tr)
5082 tr->ops->func = ftrace_stub;
5086 ftrace_ops_control_func(unsigned long ip, unsigned long parent_ip,
5087 struct ftrace_ops *op, struct pt_regs *regs)
5089 if (unlikely(trace_recursion_test(TRACE_CONTROL_BIT)))
5093 * Some of the ops may be dynamically allocated,
5094 * they must be freed after a synchronize_sched().
5096 preempt_disable_notrace();
5097 trace_recursion_set(TRACE_CONTROL_BIT);
5100 * Control funcs (perf) uses RCU. Only trace if
5101 * RCU is currently active.
5103 if (!rcu_is_watching())
5106 do_for_each_ftrace_op(op, ftrace_control_list) {
5107 if (!(op->flags & FTRACE_OPS_FL_STUB) &&
5108 !ftrace_function_local_disabled(op) &&
5109 ftrace_ops_test(op, ip, regs))
5110 op->func(ip, parent_ip, op, regs);
5111 } while_for_each_ftrace_op(op);
5113 trace_recursion_clear(TRACE_CONTROL_BIT);
5114 preempt_enable_notrace();
5117 static struct ftrace_ops control_ops = {
5118 .func = ftrace_ops_control_func,
5119 .flags = FTRACE_OPS_FL_RECURSION_SAFE | FTRACE_OPS_FL_INITIALIZED,
5120 INIT_OPS_HASH(control_ops)
5124 __ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip,
5125 struct ftrace_ops *ignored, struct pt_regs *regs)
5127 struct ftrace_ops *op;
5130 bit = trace_test_and_set_recursion(TRACE_LIST_START, TRACE_LIST_MAX);
5135 * Some of the ops may be dynamically allocated,
5136 * they must be freed after a synchronize_sched().
5138 preempt_disable_notrace();
5139 do_for_each_ftrace_op(op, ftrace_ops_list) {
5140 if (ftrace_ops_test(op, ip, regs)) {
5141 if (FTRACE_WARN_ON(!op->func)) {
5142 pr_warn("op=%p %pS\n", op, op);
5145 op->func(ip, parent_ip, op, regs);
5147 } while_for_each_ftrace_op(op);
5149 preempt_enable_notrace();
5150 trace_clear_recursion(bit);
5154 * Some archs only support passing ip and parent_ip. Even though
5155 * the list function ignores the op parameter, we do not want any
5156 * C side effects, where a function is called without the caller
5157 * sending a third parameter.
5158 * Archs are to support both the regs and ftrace_ops at the same time.
5159 * If they support ftrace_ops, it is assumed they support regs.
5160 * If call backs want to use regs, they must either check for regs
5161 * being NULL, or CONFIG_DYNAMIC_FTRACE_WITH_REGS.
5162 * Note, CONFIG_DYNAMIC_FTRACE_WITH_REGS expects a full regs to be saved.
5163 * An architecture can pass partial regs with ftrace_ops and still
5164 * set the ARCH_SUPPORT_FTARCE_OPS.
5166 #if ARCH_SUPPORTS_FTRACE_OPS
5167 static void ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip,
5168 struct ftrace_ops *op, struct pt_regs *regs)
5170 __ftrace_ops_list_func(ip, parent_ip, NULL, regs);
5173 static void ftrace_ops_no_ops(unsigned long ip, unsigned long parent_ip)
5175 __ftrace_ops_list_func(ip, parent_ip, NULL, NULL);
5180 * If there's only one function registered but it does not support
5181 * recursion, this function will be called by the mcount trampoline.
5182 * This function will handle recursion protection.
5184 static void ftrace_ops_recurs_func(unsigned long ip, unsigned long parent_ip,
5185 struct ftrace_ops *op, struct pt_regs *regs)
5189 bit = trace_test_and_set_recursion(TRACE_LIST_START, TRACE_LIST_MAX);
5193 op->func(ip, parent_ip, op, regs);
5195 trace_clear_recursion(bit);
5199 * ftrace_ops_get_func - get the function a trampoline should call
5200 * @ops: the ops to get the function for
5202 * Normally the mcount trampoline will call the ops->func, but there
5203 * are times that it should not. For example, if the ops does not
5204 * have its own recursion protection, then it should call the
5205 * ftrace_ops_recurs_func() instead.
5207 * Returns the function that the trampoline should call for @ops.
5209 ftrace_func_t ftrace_ops_get_func(struct ftrace_ops *ops)
5212 * If this is a dynamic ops or we force list func,
5213 * then it needs to call the list anyway.
5215 if (ops->flags & FTRACE_OPS_FL_DYNAMIC || FTRACE_FORCE_LIST_FUNC)
5216 return ftrace_ops_list_func;
5219 * If the func handles its own recursion, call it directly.
5220 * Otherwise call the recursion protected function that
5221 * will call the ftrace ops function.
5223 if (!(ops->flags & FTRACE_OPS_FL_RECURSION_SAFE))
5224 return ftrace_ops_recurs_func;
5229 static void clear_ftrace_swapper(void)
5231 struct task_struct *p;
5235 for_each_online_cpu(cpu) {
5237 clear_tsk_trace_trace(p);
5242 static void set_ftrace_swapper(void)
5244 struct task_struct *p;
5248 for_each_online_cpu(cpu) {
5250 set_tsk_trace_trace(p);
5255 static void clear_ftrace_pid(struct pid *pid)
5257 struct task_struct *p;
5260 do_each_pid_task(pid, PIDTYPE_PID, p) {
5261 clear_tsk_trace_trace(p);
5262 } while_each_pid_task(pid, PIDTYPE_PID, p);
5268 static void set_ftrace_pid(struct pid *pid)
5270 struct task_struct *p;
5273 do_each_pid_task(pid, PIDTYPE_PID, p) {
5274 set_tsk_trace_trace(p);
5275 } while_each_pid_task(pid, PIDTYPE_PID, p);
5279 static void clear_ftrace_pid_task(struct pid *pid)
5281 if (pid == ftrace_swapper_pid)
5282 clear_ftrace_swapper();
5284 clear_ftrace_pid(pid);
5287 static void set_ftrace_pid_task(struct pid *pid)
5289 if (pid == ftrace_swapper_pid)
5290 set_ftrace_swapper();
5292 set_ftrace_pid(pid);
5295 static int ftrace_pid_add(int p)
5298 struct ftrace_pid *fpid;
5301 mutex_lock(&ftrace_lock);
5304 pid = ftrace_swapper_pid;
5306 pid = find_get_pid(p);
5313 list_for_each_entry(fpid, &ftrace_pids, list)
5314 if (fpid->pid == pid)
5319 fpid = kmalloc(sizeof(*fpid), GFP_KERNEL);
5323 list_add(&fpid->list, &ftrace_pids);
5326 set_ftrace_pid_task(pid);
5328 ftrace_update_pid_func();
5330 ftrace_startup_all(0);
5332 mutex_unlock(&ftrace_lock);
5336 if (pid != ftrace_swapper_pid)
5340 mutex_unlock(&ftrace_lock);
5344 static void ftrace_pid_reset(void)
5346 struct ftrace_pid *fpid, *safe;
5348 mutex_lock(&ftrace_lock);
5349 list_for_each_entry_safe(fpid, safe, &ftrace_pids, list) {
5350 struct pid *pid = fpid->pid;
5352 clear_ftrace_pid_task(pid);
5354 list_del(&fpid->list);
5358 ftrace_update_pid_func();
5359 ftrace_startup_all(0);
5361 mutex_unlock(&ftrace_lock);
5364 static void *fpid_start(struct seq_file *m, loff_t *pos)
5366 mutex_lock(&ftrace_lock);
5368 if (list_empty(&ftrace_pids) && (!*pos))
5371 return seq_list_start(&ftrace_pids, *pos);
5374 static void *fpid_next(struct seq_file *m, void *v, loff_t *pos)
5379 return seq_list_next(v, &ftrace_pids, pos);
5382 static void fpid_stop(struct seq_file *m, void *p)
5384 mutex_unlock(&ftrace_lock);
5387 static int fpid_show(struct seq_file *m, void *v)
5389 const struct ftrace_pid *fpid = list_entry(v, struct ftrace_pid, list);
5391 if (v == (void *)1) {
5392 seq_puts(m, "no pid\n");
5396 if (fpid->pid == ftrace_swapper_pid)
5397 seq_puts(m, "swapper tasks\n");
5399 seq_printf(m, "%u\n", pid_vnr(fpid->pid));
5404 static const struct seq_operations ftrace_pid_sops = {
5405 .start = fpid_start,
5412 ftrace_pid_open(struct inode *inode, struct file *file)
5416 if ((file->f_mode & FMODE_WRITE) &&
5417 (file->f_flags & O_TRUNC))
5420 if (file->f_mode & FMODE_READ)
5421 ret = seq_open(file, &ftrace_pid_sops);
5427 ftrace_pid_write(struct file *filp, const char __user *ubuf,
5428 size_t cnt, loff_t *ppos)
5434 if (cnt >= sizeof(buf))
5437 if (copy_from_user(&buf, ubuf, cnt))
5443 * Allow "echo > set_ftrace_pid" or "echo -n '' > set_ftrace_pid"
5444 * to clean the filter quietly.
5446 tmp = strstrip(buf);
5447 if (strlen(tmp) == 0)
5450 ret = kstrtol(tmp, 10, &val);
5454 ret = ftrace_pid_add(val);
5456 return ret ? ret : cnt;
5460 ftrace_pid_release(struct inode *inode, struct file *file)
5462 if (file->f_mode & FMODE_READ)
5463 seq_release(inode, file);
5468 static const struct file_operations ftrace_pid_fops = {
5469 .open = ftrace_pid_open,
5470 .write = ftrace_pid_write,
5472 .llseek = tracing_lseek,
5473 .release = ftrace_pid_release,
5476 static __init int ftrace_init_debugfs(void)
5478 struct dentry *d_tracer;
5480 d_tracer = tracing_init_dentry();
5481 if (IS_ERR(d_tracer))
5484 ftrace_init_dyn_debugfs(d_tracer);
5486 trace_create_file("set_ftrace_pid", 0644, d_tracer,
5487 NULL, &ftrace_pid_fops);
5489 ftrace_profile_debugfs(d_tracer);
5493 fs_initcall(ftrace_init_debugfs);
5496 * ftrace_kill - kill ftrace
5498 * This function should be used by panic code. It stops ftrace
5499 * but in a not so nice way. If you need to simply kill ftrace
5500 * from a non-atomic section, use ftrace_kill.
5502 void ftrace_kill(void)
5504 ftrace_disabled = 1;
5506 clear_ftrace_function();
5510 * Test if ftrace is dead or not.
5512 int ftrace_is_dead(void)
5514 return ftrace_disabled;
5518 * register_ftrace_function - register a function for profiling
5519 * @ops - ops structure that holds the function for profiling.
5521 * Register a function to be called by all functions in the
5524 * Note: @ops->func and all the functions it calls must be labeled
5525 * with "notrace", otherwise it will go into a
5528 int register_ftrace_function(struct ftrace_ops *ops)
5532 ftrace_ops_init(ops);
5534 mutex_lock(&ftrace_lock);
5536 ret = ftrace_startup(ops, 0);
5538 mutex_unlock(&ftrace_lock);
5542 EXPORT_SYMBOL_GPL(register_ftrace_function);
5545 * unregister_ftrace_function - unregister a function for profiling.
5546 * @ops - ops structure that holds the function to unregister
5548 * Unregister a function that was added to be called by ftrace profiling.
5550 int unregister_ftrace_function(struct ftrace_ops *ops)
5554 mutex_lock(&ftrace_lock);
5555 ret = ftrace_shutdown(ops, 0);
5556 mutex_unlock(&ftrace_lock);
5560 EXPORT_SYMBOL_GPL(unregister_ftrace_function);
5563 ftrace_enable_sysctl(struct ctl_table *table, int write,
5564 void __user *buffer, size_t *lenp,
5569 mutex_lock(&ftrace_lock);
5571 if (unlikely(ftrace_disabled))
5574 ret = proc_dointvec(table, write, buffer, lenp, ppos);
5576 if (ret || !write || (last_ftrace_enabled == !!ftrace_enabled))
5579 last_ftrace_enabled = !!ftrace_enabled;
5581 if (ftrace_enabled) {
5583 /* we are starting ftrace again */
5584 if (ftrace_ops_list != &ftrace_list_end)
5585 update_ftrace_function();
5587 ftrace_startup_sysctl();
5590 /* stopping ftrace calls (just send to ftrace_stub) */
5591 ftrace_trace_function = ftrace_stub;
5593 ftrace_shutdown_sysctl();
5597 mutex_unlock(&ftrace_lock);
5601 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
5603 static struct ftrace_ops graph_ops = {
5604 .func = ftrace_stub,
5605 .flags = FTRACE_OPS_FL_RECURSION_SAFE |
5606 FTRACE_OPS_FL_INITIALIZED |
5608 #ifdef FTRACE_GRAPH_TRAMP_ADDR
5609 .trampoline = FTRACE_GRAPH_TRAMP_ADDR,
5610 /* trampoline_size is only needed for dynamically allocated tramps */
5612 ASSIGN_OPS_HASH(graph_ops, &global_ops.local_hash)
5615 int ftrace_graph_entry_stub(struct ftrace_graph_ent *trace)
5620 /* The callbacks that hook a function */
5621 trace_func_graph_ret_t ftrace_graph_return =
5622 (trace_func_graph_ret_t)ftrace_stub;
5623 trace_func_graph_ent_t ftrace_graph_entry = ftrace_graph_entry_stub;
5624 static trace_func_graph_ent_t __ftrace_graph_entry = ftrace_graph_entry_stub;
5626 /* Try to assign a return stack array on FTRACE_RETSTACK_ALLOC_SIZE tasks. */
5627 static int alloc_retstack_tasklist(struct ftrace_ret_stack **ret_stack_list)
5631 unsigned long flags;
5632 int start = 0, end = FTRACE_RETSTACK_ALLOC_SIZE;
5633 struct task_struct *g, *t;
5635 for (i = 0; i < FTRACE_RETSTACK_ALLOC_SIZE; i++) {
5636 ret_stack_list[i] = kmalloc(FTRACE_RETFUNC_DEPTH
5637 * sizeof(struct ftrace_ret_stack),
5639 if (!ret_stack_list[i]) {
5647 read_lock_irqsave(&tasklist_lock, flags);
5648 do_each_thread(g, t) {
5654 if (t->ret_stack == NULL) {
5655 atomic_set(&t->tracing_graph_pause, 0);
5656 atomic_set(&t->trace_overrun, 0);
5657 t->curr_ret_stack = -1;
5658 /* Make sure the tasks see the -1 first: */
5660 t->ret_stack = ret_stack_list[start++];
5662 } while_each_thread(g, t);
5665 read_unlock_irqrestore(&tasklist_lock, flags);
5667 for (i = start; i < end; i++)
5668 kfree(ret_stack_list[i]);
5673 ftrace_graph_probe_sched_switch(void *ignore,
5674 struct task_struct *prev, struct task_struct *next)
5676 unsigned long long timestamp;
5680 * Does the user want to count the time a function was asleep.
5681 * If so, do not update the time stamps.
5683 if (trace_flags & TRACE_ITER_SLEEP_TIME)
5686 timestamp = trace_clock_local();
5688 prev->ftrace_timestamp = timestamp;
5690 /* only process tasks that we timestamped */
5691 if (!next->ftrace_timestamp)
5695 * Update all the counters in next to make up for the
5696 * time next was sleeping.
5698 timestamp -= next->ftrace_timestamp;
5700 for (index = next->curr_ret_stack; index >= 0; index--)
5701 next->ret_stack[index].calltime += timestamp;
5704 /* Allocate a return stack for each task */
5705 static int start_graph_tracing(void)
5707 struct ftrace_ret_stack **ret_stack_list;
5710 ret_stack_list = kmalloc(FTRACE_RETSTACK_ALLOC_SIZE *
5711 sizeof(struct ftrace_ret_stack *),
5714 if (!ret_stack_list)
5717 /* The cpu_boot init_task->ret_stack will never be freed */
5718 for_each_online_cpu(cpu) {
5719 if (!idle_task(cpu)->ret_stack)
5720 ftrace_graph_init_idle_task(idle_task(cpu), cpu);
5724 ret = alloc_retstack_tasklist(ret_stack_list);
5725 } while (ret == -EAGAIN);
5728 ret = register_trace_sched_switch(ftrace_graph_probe_sched_switch, NULL);
5730 pr_info("ftrace_graph: Couldn't activate tracepoint"
5731 " probe to kernel_sched_switch\n");
5734 kfree(ret_stack_list);
5739 * Hibernation protection.
5740 * The state of the current task is too much unstable during
5741 * suspend/restore to disk. We want to protect against that.
5744 ftrace_suspend_notifier_call(struct notifier_block *bl, unsigned long state,
5748 case PM_HIBERNATION_PREPARE:
5749 pause_graph_tracing();
5752 case PM_POST_HIBERNATION:
5753 unpause_graph_tracing();
5759 static int ftrace_graph_entry_test(struct ftrace_graph_ent *trace)
5761 if (!ftrace_ops_test(&global_ops, trace->func, NULL))
5763 return __ftrace_graph_entry(trace);
5767 * The function graph tracer should only trace the functions defined
5768 * by set_ftrace_filter and set_ftrace_notrace. If another function
5769 * tracer ops is registered, the graph tracer requires testing the
5770 * function against the global ops, and not just trace any function
5771 * that any ftrace_ops registered.
5773 static void update_function_graph_func(void)
5775 struct ftrace_ops *op;
5776 bool do_test = false;
5779 * The graph and global ops share the same set of functions
5780 * to test. If any other ops is on the list, then
5781 * the graph tracing needs to test if its the function
5784 do_for_each_ftrace_op(op, ftrace_ops_list) {
5785 if (op != &global_ops && op != &graph_ops &&
5786 op != &ftrace_list_end) {
5788 /* in double loop, break out with goto */
5791 } while_for_each_ftrace_op(op);
5794 ftrace_graph_entry = ftrace_graph_entry_test;
5796 ftrace_graph_entry = __ftrace_graph_entry;
5799 static struct notifier_block ftrace_suspend_notifier = {
5800 .notifier_call = ftrace_suspend_notifier_call,
5803 int register_ftrace_graph(trace_func_graph_ret_t retfunc,
5804 trace_func_graph_ent_t entryfunc)
5808 mutex_lock(&ftrace_lock);
5810 /* we currently allow only one tracer registered at a time */
5811 if (ftrace_graph_active) {
5816 register_pm_notifier(&ftrace_suspend_notifier);
5818 ftrace_graph_active++;
5819 ret = start_graph_tracing();
5821 ftrace_graph_active--;
5825 ftrace_graph_return = retfunc;
5828 * Update the indirect function to the entryfunc, and the
5829 * function that gets called to the entry_test first. Then
5830 * call the update fgraph entry function to determine if
5831 * the entryfunc should be called directly or not.
5833 __ftrace_graph_entry = entryfunc;
5834 ftrace_graph_entry = ftrace_graph_entry_test;
5835 update_function_graph_func();
5837 ret = ftrace_startup(&graph_ops, FTRACE_START_FUNC_RET);
5839 mutex_unlock(&ftrace_lock);
5843 void unregister_ftrace_graph(void)
5845 mutex_lock(&ftrace_lock);
5847 if (unlikely(!ftrace_graph_active))
5850 ftrace_graph_active--;
5851 ftrace_graph_return = (trace_func_graph_ret_t)ftrace_stub;
5852 ftrace_graph_entry = ftrace_graph_entry_stub;
5853 __ftrace_graph_entry = ftrace_graph_entry_stub;
5854 ftrace_shutdown(&graph_ops, FTRACE_STOP_FUNC_RET);
5855 unregister_pm_notifier(&ftrace_suspend_notifier);
5856 unregister_trace_sched_switch(ftrace_graph_probe_sched_switch, NULL);
5858 #ifdef CONFIG_DYNAMIC_FTRACE
5860 * Function graph does not allocate the trampoline, but
5861 * other global_ops do. We need to reset the ALLOC_TRAMP flag
5864 global_ops.trampoline = save_global_trampoline;
5865 if (save_global_flags & FTRACE_OPS_FL_ALLOC_TRAMP)
5866 global_ops.flags |= FTRACE_OPS_FL_ALLOC_TRAMP;
5870 mutex_unlock(&ftrace_lock);
5873 static DEFINE_PER_CPU(struct ftrace_ret_stack *, idle_ret_stack);
5876 graph_init_task(struct task_struct *t, struct ftrace_ret_stack *ret_stack)
5878 atomic_set(&t->tracing_graph_pause, 0);
5879 atomic_set(&t->trace_overrun, 0);
5880 t->ftrace_timestamp = 0;
5881 /* make curr_ret_stack visible before we add the ret_stack */
5883 t->ret_stack = ret_stack;
5887 * Allocate a return stack for the idle task. May be the first
5888 * time through, or it may be done by CPU hotplug online.
5890 void ftrace_graph_init_idle_task(struct task_struct *t, int cpu)
5892 t->curr_ret_stack = -1;
5894 * The idle task has no parent, it either has its own
5895 * stack or no stack at all.
5898 WARN_ON(t->ret_stack != per_cpu(idle_ret_stack, cpu));
5900 if (ftrace_graph_active) {
5901 struct ftrace_ret_stack *ret_stack;
5903 ret_stack = per_cpu(idle_ret_stack, cpu);
5905 ret_stack = kmalloc(FTRACE_RETFUNC_DEPTH
5906 * sizeof(struct ftrace_ret_stack),
5910 per_cpu(idle_ret_stack, cpu) = ret_stack;
5912 graph_init_task(t, ret_stack);
5916 /* Allocate a return stack for newly created task */
5917 void ftrace_graph_init_task(struct task_struct *t)
5919 /* Make sure we do not use the parent ret_stack */
5920 t->ret_stack = NULL;
5921 t->curr_ret_stack = -1;
5923 if (ftrace_graph_active) {
5924 struct ftrace_ret_stack *ret_stack;
5926 ret_stack = kmalloc(FTRACE_RETFUNC_DEPTH
5927 * sizeof(struct ftrace_ret_stack),
5931 graph_init_task(t, ret_stack);
5935 void ftrace_graph_exit_task(struct task_struct *t)
5937 struct ftrace_ret_stack *ret_stack = t->ret_stack;
5939 t->ret_stack = NULL;
5940 /* NULL must become visible to IRQs before we free it: */