Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/viro/vfs
[linux/fpc-iii.git] / kernel / trace / ftrace.c
blobcd7f76d1eb867823b47f35c0a93f0c0002c2cbe8
1 /*
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) \
44 ({ \
45 int ___r = cond; \
46 if (WARN_ON(___r)) \
47 ftrace_kill(); \
48 ___r; \
51 #define FTRACE_WARN_ON_ONCE(cond) \
52 ({ \
53 int ___r = cond; \
54 if (WARN_ON_ONCE(___r)) \
55 ftrace_kill(); \
56 ___r; \
59 /* hash bits for specific function selection */
60 #define FTRACE_HASH_BITS 7
61 #define FTRACE_FUNC_HASHSIZE (1 << FTRACE_HASH_BITS)
62 #define FTRACE_HASH_DEFAULT_BITS 10
63 #define FTRACE_HASH_MAX_BITS 12
65 #define FL_GLOBAL_CONTROL_MASK (FTRACE_OPS_FL_GLOBAL | FTRACE_OPS_FL_CONTROL)
67 #ifdef CONFIG_DYNAMIC_FTRACE
68 #define INIT_REGEX_LOCK(opsname) \
69 .regex_lock = __MUTEX_INITIALIZER(opsname.regex_lock),
70 #else
71 #define INIT_REGEX_LOCK(opsname)
72 #endif
74 static struct ftrace_ops ftrace_list_end __read_mostly = {
75 .func = ftrace_stub,
76 .flags = FTRACE_OPS_FL_RECURSION_SAFE | FTRACE_OPS_FL_STUB,
79 /* ftrace_enabled is a method to turn ftrace on or off */
80 int ftrace_enabled __read_mostly;
81 static int last_ftrace_enabled;
83 /* Quick disabling of function tracer. */
84 int function_trace_stop __read_mostly;
86 /* Current function tracing op */
87 struct ftrace_ops *function_trace_op __read_mostly = &ftrace_list_end;
88 /* What to set function_trace_op to */
89 static struct ftrace_ops *set_function_trace_op;
91 /* List for set_ftrace_pid's pids. */
92 LIST_HEAD(ftrace_pids);
93 struct ftrace_pid {
94 struct list_head list;
95 struct pid *pid;
99 * ftrace_disabled is set when an anomaly is discovered.
100 * ftrace_disabled is much stronger than ftrace_enabled.
102 static int ftrace_disabled __read_mostly;
104 static DEFINE_MUTEX(ftrace_lock);
106 static struct ftrace_ops *ftrace_global_list __read_mostly = &ftrace_list_end;
107 static struct ftrace_ops *ftrace_control_list __read_mostly = &ftrace_list_end;
108 static struct ftrace_ops *ftrace_ops_list __read_mostly = &ftrace_list_end;
109 ftrace_func_t ftrace_trace_function __read_mostly = ftrace_stub;
110 ftrace_func_t ftrace_pid_function __read_mostly = ftrace_stub;
111 static struct ftrace_ops global_ops;
112 static struct ftrace_ops control_ops;
114 #if ARCH_SUPPORTS_FTRACE_OPS
115 static void ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip,
116 struct ftrace_ops *op, struct pt_regs *regs);
117 #else
118 /* See comment below, where ftrace_ops_list_func is defined */
119 static void ftrace_ops_no_ops(unsigned long ip, unsigned long parent_ip);
120 #define ftrace_ops_list_func ((ftrace_func_t)ftrace_ops_no_ops)
121 #endif
124 * Traverse the ftrace_global_list, invoking all entries. The reason that we
125 * can use rcu_dereference_raw_notrace() is that elements removed from this list
126 * are simply leaked, so there is no need to interact with a grace-period
127 * mechanism. The rcu_dereference_raw_notrace() calls are needed to handle
128 * concurrent insertions into the ftrace_global_list.
130 * Silly Alpha and silly pointer-speculation compiler optimizations!
132 #define do_for_each_ftrace_op(op, list) \
133 op = rcu_dereference_raw_notrace(list); \
137 * Optimized for just a single item in the list (as that is the normal case).
139 #define while_for_each_ftrace_op(op) \
140 while (likely(op = rcu_dereference_raw_notrace((op)->next)) && \
141 unlikely((op) != &ftrace_list_end))
143 static inline void ftrace_ops_init(struct ftrace_ops *ops)
145 #ifdef CONFIG_DYNAMIC_FTRACE
146 if (!(ops->flags & FTRACE_OPS_FL_INITIALIZED)) {
147 mutex_init(&ops->regex_lock);
148 ops->flags |= FTRACE_OPS_FL_INITIALIZED;
150 #endif
154 * ftrace_nr_registered_ops - return number of ops registered
156 * Returns the number of ftrace_ops registered and tracing functions
158 int ftrace_nr_registered_ops(void)
160 struct ftrace_ops *ops;
161 int cnt = 0;
163 mutex_lock(&ftrace_lock);
165 for (ops = ftrace_ops_list;
166 ops != &ftrace_list_end; ops = ops->next)
167 cnt++;
169 mutex_unlock(&ftrace_lock);
171 return cnt;
174 static void
175 ftrace_global_list_func(unsigned long ip, unsigned long parent_ip,
176 struct ftrace_ops *op, struct pt_regs *regs)
178 int bit;
180 bit = trace_test_and_set_recursion(TRACE_GLOBAL_START, TRACE_GLOBAL_MAX);
181 if (bit < 0)
182 return;
184 do_for_each_ftrace_op(op, ftrace_global_list) {
185 op->func(ip, parent_ip, op, regs);
186 } while_for_each_ftrace_op(op);
188 trace_clear_recursion(bit);
191 static void ftrace_pid_func(unsigned long ip, unsigned long parent_ip,
192 struct ftrace_ops *op, struct pt_regs *regs)
194 if (!test_tsk_trace_trace(current))
195 return;
197 ftrace_pid_function(ip, parent_ip, op, regs);
200 static void set_ftrace_pid_function(ftrace_func_t func)
202 /* do not set ftrace_pid_function to itself! */
203 if (func != ftrace_pid_func)
204 ftrace_pid_function = func;
208 * clear_ftrace_function - reset the ftrace function
210 * This NULLs the ftrace function and in essence stops
211 * tracing. There may be lag
213 void clear_ftrace_function(void)
215 ftrace_trace_function = ftrace_stub;
216 ftrace_pid_function = ftrace_stub;
219 static void control_ops_disable_all(struct ftrace_ops *ops)
221 int cpu;
223 for_each_possible_cpu(cpu)
224 *per_cpu_ptr(ops->disabled, cpu) = 1;
227 static int control_ops_alloc(struct ftrace_ops *ops)
229 int __percpu *disabled;
231 disabled = alloc_percpu(int);
232 if (!disabled)
233 return -ENOMEM;
235 ops->disabled = disabled;
236 control_ops_disable_all(ops);
237 return 0;
240 static void control_ops_free(struct ftrace_ops *ops)
242 free_percpu(ops->disabled);
245 static void update_global_ops(void)
247 ftrace_func_t func;
250 * If there's only one function registered, then call that
251 * function directly. Otherwise, we need to iterate over the
252 * registered callers.
254 if (ftrace_global_list == &ftrace_list_end ||
255 ftrace_global_list->next == &ftrace_list_end) {
256 func = ftrace_global_list->func;
258 * As we are calling the function directly.
259 * If it does not have recursion protection,
260 * the function_trace_op needs to be updated
261 * accordingly.
263 if (ftrace_global_list->flags & FTRACE_OPS_FL_RECURSION_SAFE)
264 global_ops.flags |= FTRACE_OPS_FL_RECURSION_SAFE;
265 else
266 global_ops.flags &= ~FTRACE_OPS_FL_RECURSION_SAFE;
267 } else {
268 func = ftrace_global_list_func;
269 /* The list has its own recursion protection. */
270 global_ops.flags |= FTRACE_OPS_FL_RECURSION_SAFE;
274 /* If we filter on pids, update to use the pid function */
275 if (!list_empty(&ftrace_pids)) {
276 set_ftrace_pid_function(func);
277 func = ftrace_pid_func;
280 global_ops.func = func;
283 static void ftrace_sync(struct work_struct *work)
286 * This function is just a stub to implement a hard force
287 * of synchronize_sched(). This requires synchronizing
288 * tasks even in userspace and idle.
290 * Yes, function tracing is rude.
294 static void ftrace_sync_ipi(void *data)
296 /* Probably not needed, but do it anyway */
297 smp_rmb();
300 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
301 static void update_function_graph_func(void);
302 #else
303 static inline void update_function_graph_func(void) { }
304 #endif
306 static void update_ftrace_function(void)
308 ftrace_func_t func;
310 update_global_ops();
313 * If we are at the end of the list and this ops is
314 * recursion safe and not dynamic and the arch supports passing ops,
315 * then have the mcount trampoline call the function directly.
317 if (ftrace_ops_list == &ftrace_list_end ||
318 (ftrace_ops_list->next == &ftrace_list_end &&
319 !(ftrace_ops_list->flags & FTRACE_OPS_FL_DYNAMIC) &&
320 (ftrace_ops_list->flags & FTRACE_OPS_FL_RECURSION_SAFE) &&
321 !FTRACE_FORCE_LIST_FUNC)) {
322 /* Set the ftrace_ops that the arch callback uses */
323 if (ftrace_ops_list == &global_ops)
324 set_function_trace_op = ftrace_global_list;
325 else
326 set_function_trace_op = ftrace_ops_list;
327 func = ftrace_ops_list->func;
328 } else {
329 /* Just use the default ftrace_ops */
330 set_function_trace_op = &ftrace_list_end;
331 func = ftrace_ops_list_func;
334 /* If there's no change, then do nothing more here */
335 if (ftrace_trace_function == func)
336 return;
338 update_function_graph_func();
341 * If we are using the list function, it doesn't care
342 * about the function_trace_ops.
344 if (func == ftrace_ops_list_func) {
345 ftrace_trace_function = func;
347 * Don't even bother setting function_trace_ops,
348 * it would be racy to do so anyway.
350 return;
353 #ifndef CONFIG_DYNAMIC_FTRACE
355 * For static tracing, we need to be a bit more careful.
356 * The function change takes affect immediately. Thus,
357 * we need to coorditate the setting of the function_trace_ops
358 * with the setting of the ftrace_trace_function.
360 * Set the function to the list ops, which will call the
361 * function we want, albeit indirectly, but it handles the
362 * ftrace_ops and doesn't depend on function_trace_op.
364 ftrace_trace_function = ftrace_ops_list_func;
366 * Make sure all CPUs see this. Yes this is slow, but static
367 * tracing is slow and nasty to have enabled.
369 schedule_on_each_cpu(ftrace_sync);
370 /* Now all cpus are using the list ops. */
371 function_trace_op = set_function_trace_op;
372 /* Make sure the function_trace_op is visible on all CPUs */
373 smp_wmb();
374 /* Nasty way to force a rmb on all cpus */
375 smp_call_function(ftrace_sync_ipi, NULL, 1);
376 /* OK, we are all set to update the ftrace_trace_function now! */
377 #endif /* !CONFIG_DYNAMIC_FTRACE */
379 ftrace_trace_function = func;
382 static void add_ftrace_ops(struct ftrace_ops **list, struct ftrace_ops *ops)
384 ops->next = *list;
386 * We are entering ops into the list but another
387 * CPU might be walking that list. We need to make sure
388 * the ops->next pointer is valid before another CPU sees
389 * the ops pointer included into the list.
391 rcu_assign_pointer(*list, ops);
394 static int remove_ftrace_ops(struct ftrace_ops **list, struct ftrace_ops *ops)
396 struct ftrace_ops **p;
399 * If we are removing the last function, then simply point
400 * to the ftrace_stub.
402 if (*list == ops && ops->next == &ftrace_list_end) {
403 *list = &ftrace_list_end;
404 return 0;
407 for (p = list; *p != &ftrace_list_end; p = &(*p)->next)
408 if (*p == ops)
409 break;
411 if (*p != ops)
412 return -1;
414 *p = (*p)->next;
415 return 0;
418 static void add_ftrace_list_ops(struct ftrace_ops **list,
419 struct ftrace_ops *main_ops,
420 struct ftrace_ops *ops)
422 int first = *list == &ftrace_list_end;
423 add_ftrace_ops(list, ops);
424 if (first)
425 add_ftrace_ops(&ftrace_ops_list, main_ops);
428 static int remove_ftrace_list_ops(struct ftrace_ops **list,
429 struct ftrace_ops *main_ops,
430 struct ftrace_ops *ops)
432 int ret = remove_ftrace_ops(list, ops);
433 if (!ret && *list == &ftrace_list_end)
434 ret = remove_ftrace_ops(&ftrace_ops_list, main_ops);
435 return ret;
438 static int __register_ftrace_function(struct ftrace_ops *ops)
440 if (FTRACE_WARN_ON(ops == &global_ops))
441 return -EINVAL;
443 if (WARN_ON(ops->flags & FTRACE_OPS_FL_ENABLED))
444 return -EBUSY;
446 /* We don't support both control and global flags set. */
447 if ((ops->flags & FL_GLOBAL_CONTROL_MASK) == FL_GLOBAL_CONTROL_MASK)
448 return -EINVAL;
450 #ifndef CONFIG_DYNAMIC_FTRACE_WITH_REGS
452 * If the ftrace_ops specifies SAVE_REGS, then it only can be used
453 * if the arch supports it, or SAVE_REGS_IF_SUPPORTED is also set.
454 * Setting SAVE_REGS_IF_SUPPORTED makes SAVE_REGS irrelevant.
456 if (ops->flags & FTRACE_OPS_FL_SAVE_REGS &&
457 !(ops->flags & FTRACE_OPS_FL_SAVE_REGS_IF_SUPPORTED))
458 return -EINVAL;
460 if (ops->flags & FTRACE_OPS_FL_SAVE_REGS_IF_SUPPORTED)
461 ops->flags |= FTRACE_OPS_FL_SAVE_REGS;
462 #endif
464 if (!core_kernel_data((unsigned long)ops))
465 ops->flags |= FTRACE_OPS_FL_DYNAMIC;
467 if (ops->flags & FTRACE_OPS_FL_GLOBAL) {
468 add_ftrace_list_ops(&ftrace_global_list, &global_ops, ops);
469 ops->flags |= FTRACE_OPS_FL_ENABLED;
470 } else if (ops->flags & FTRACE_OPS_FL_CONTROL) {
471 if (control_ops_alloc(ops))
472 return -ENOMEM;
473 add_ftrace_list_ops(&ftrace_control_list, &control_ops, ops);
474 } else
475 add_ftrace_ops(&ftrace_ops_list, ops);
477 if (ftrace_enabled)
478 update_ftrace_function();
480 return 0;
483 static int __unregister_ftrace_function(struct ftrace_ops *ops)
485 int ret;
487 if (WARN_ON(!(ops->flags & FTRACE_OPS_FL_ENABLED)))
488 return -EBUSY;
490 if (FTRACE_WARN_ON(ops == &global_ops))
491 return -EINVAL;
493 if (ops->flags & FTRACE_OPS_FL_GLOBAL) {
494 ret = remove_ftrace_list_ops(&ftrace_global_list,
495 &global_ops, ops);
496 if (!ret)
497 ops->flags &= ~FTRACE_OPS_FL_ENABLED;
498 } else if (ops->flags & FTRACE_OPS_FL_CONTROL) {
499 ret = remove_ftrace_list_ops(&ftrace_control_list,
500 &control_ops, ops);
501 } else
502 ret = remove_ftrace_ops(&ftrace_ops_list, ops);
504 if (ret < 0)
505 return ret;
507 if (ftrace_enabled)
508 update_ftrace_function();
510 return 0;
513 static void ftrace_update_pid_func(void)
515 /* Only do something if we are tracing something */
516 if (ftrace_trace_function == ftrace_stub)
517 return;
519 update_ftrace_function();
522 #ifdef CONFIG_FUNCTION_PROFILER
523 struct ftrace_profile {
524 struct hlist_node node;
525 unsigned long ip;
526 unsigned long counter;
527 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
528 unsigned long long time;
529 unsigned long long time_squared;
530 #endif
533 struct ftrace_profile_page {
534 struct ftrace_profile_page *next;
535 unsigned long index;
536 struct ftrace_profile records[];
539 struct ftrace_profile_stat {
540 atomic_t disabled;
541 struct hlist_head *hash;
542 struct ftrace_profile_page *pages;
543 struct ftrace_profile_page *start;
544 struct tracer_stat stat;
547 #define PROFILE_RECORDS_SIZE \
548 (PAGE_SIZE - offsetof(struct ftrace_profile_page, records))
550 #define PROFILES_PER_PAGE \
551 (PROFILE_RECORDS_SIZE / sizeof(struct ftrace_profile))
553 static int ftrace_profile_enabled __read_mostly;
555 /* ftrace_profile_lock - synchronize the enable and disable of the profiler */
556 static DEFINE_MUTEX(ftrace_profile_lock);
558 static DEFINE_PER_CPU(struct ftrace_profile_stat, ftrace_profile_stats);
560 #define FTRACE_PROFILE_HASH_BITS 10
561 #define FTRACE_PROFILE_HASH_SIZE (1 << FTRACE_PROFILE_HASH_BITS)
563 static void *
564 function_stat_next(void *v, int idx)
566 struct ftrace_profile *rec = v;
567 struct ftrace_profile_page *pg;
569 pg = (struct ftrace_profile_page *)((unsigned long)rec & PAGE_MASK);
571 again:
572 if (idx != 0)
573 rec++;
575 if ((void *)rec >= (void *)&pg->records[pg->index]) {
576 pg = pg->next;
577 if (!pg)
578 return NULL;
579 rec = &pg->records[0];
580 if (!rec->counter)
581 goto again;
584 return rec;
587 static void *function_stat_start(struct tracer_stat *trace)
589 struct ftrace_profile_stat *stat =
590 container_of(trace, struct ftrace_profile_stat, stat);
592 if (!stat || !stat->start)
593 return NULL;
595 return function_stat_next(&stat->start->records[0], 0);
598 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
599 /* function graph compares on total time */
600 static int function_stat_cmp(void *p1, void *p2)
602 struct ftrace_profile *a = p1;
603 struct ftrace_profile *b = p2;
605 if (a->time < b->time)
606 return -1;
607 if (a->time > b->time)
608 return 1;
609 else
610 return 0;
612 #else
613 /* not function graph compares against hits */
614 static int function_stat_cmp(void *p1, void *p2)
616 struct ftrace_profile *a = p1;
617 struct ftrace_profile *b = p2;
619 if (a->counter < b->counter)
620 return -1;
621 if (a->counter > b->counter)
622 return 1;
623 else
624 return 0;
626 #endif
628 static int function_stat_headers(struct seq_file *m)
630 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
631 seq_printf(m, " Function "
632 "Hit Time Avg s^2\n"
633 " -------- "
634 "--- ---- --- ---\n");
635 #else
636 seq_printf(m, " Function Hit\n"
637 " -------- ---\n");
638 #endif
639 return 0;
642 static int function_stat_show(struct seq_file *m, void *v)
644 struct ftrace_profile *rec = v;
645 char str[KSYM_SYMBOL_LEN];
646 int ret = 0;
647 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
648 static struct trace_seq s;
649 unsigned long long avg;
650 unsigned long long stddev;
651 #endif
652 mutex_lock(&ftrace_profile_lock);
654 /* we raced with function_profile_reset() */
655 if (unlikely(rec->counter == 0)) {
656 ret = -EBUSY;
657 goto out;
660 kallsyms_lookup(rec->ip, NULL, NULL, NULL, str);
661 seq_printf(m, " %-30.30s %10lu", str, rec->counter);
663 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
664 seq_printf(m, " ");
665 avg = rec->time;
666 do_div(avg, rec->counter);
668 /* Sample standard deviation (s^2) */
669 if (rec->counter <= 1)
670 stddev = 0;
671 else {
673 * Apply Welford's method:
674 * s^2 = 1 / (n * (n-1)) * (n * \Sum (x_i)^2 - (\Sum x_i)^2)
676 stddev = rec->counter * rec->time_squared -
677 rec->time * rec->time;
680 * Divide only 1000 for ns^2 -> us^2 conversion.
681 * trace_print_graph_duration will divide 1000 again.
683 do_div(stddev, rec->counter * (rec->counter - 1) * 1000);
686 trace_seq_init(&s);
687 trace_print_graph_duration(rec->time, &s);
688 trace_seq_puts(&s, " ");
689 trace_print_graph_duration(avg, &s);
690 trace_seq_puts(&s, " ");
691 trace_print_graph_duration(stddev, &s);
692 trace_print_seq(m, &s);
693 #endif
694 seq_putc(m, '\n');
695 out:
696 mutex_unlock(&ftrace_profile_lock);
698 return ret;
701 static void ftrace_profile_reset(struct ftrace_profile_stat *stat)
703 struct ftrace_profile_page *pg;
705 pg = stat->pages = stat->start;
707 while (pg) {
708 memset(pg->records, 0, PROFILE_RECORDS_SIZE);
709 pg->index = 0;
710 pg = pg->next;
713 memset(stat->hash, 0,
714 FTRACE_PROFILE_HASH_SIZE * sizeof(struct hlist_head));
717 int ftrace_profile_pages_init(struct ftrace_profile_stat *stat)
719 struct ftrace_profile_page *pg;
720 int functions;
721 int pages;
722 int i;
724 /* If we already allocated, do nothing */
725 if (stat->pages)
726 return 0;
728 stat->pages = (void *)get_zeroed_page(GFP_KERNEL);
729 if (!stat->pages)
730 return -ENOMEM;
732 #ifdef CONFIG_DYNAMIC_FTRACE
733 functions = ftrace_update_tot_cnt;
734 #else
736 * We do not know the number of functions that exist because
737 * dynamic tracing is what counts them. With past experience
738 * we have around 20K functions. That should be more than enough.
739 * It is highly unlikely we will execute every function in
740 * the kernel.
742 functions = 20000;
743 #endif
745 pg = stat->start = stat->pages;
747 pages = DIV_ROUND_UP(functions, PROFILES_PER_PAGE);
749 for (i = 1; i < pages; i++) {
750 pg->next = (void *)get_zeroed_page(GFP_KERNEL);
751 if (!pg->next)
752 goto out_free;
753 pg = pg->next;
756 return 0;
758 out_free:
759 pg = stat->start;
760 while (pg) {
761 unsigned long tmp = (unsigned long)pg;
763 pg = pg->next;
764 free_page(tmp);
767 stat->pages = NULL;
768 stat->start = NULL;
770 return -ENOMEM;
773 static int ftrace_profile_init_cpu(int cpu)
775 struct ftrace_profile_stat *stat;
776 int size;
778 stat = &per_cpu(ftrace_profile_stats, cpu);
780 if (stat->hash) {
781 /* If the profile is already created, simply reset it */
782 ftrace_profile_reset(stat);
783 return 0;
787 * We are profiling all functions, but usually only a few thousand
788 * functions are hit. We'll make a hash of 1024 items.
790 size = FTRACE_PROFILE_HASH_SIZE;
792 stat->hash = kzalloc(sizeof(struct hlist_head) * size, GFP_KERNEL);
794 if (!stat->hash)
795 return -ENOMEM;
797 /* Preallocate the function profiling pages */
798 if (ftrace_profile_pages_init(stat) < 0) {
799 kfree(stat->hash);
800 stat->hash = NULL;
801 return -ENOMEM;
804 return 0;
807 static int ftrace_profile_init(void)
809 int cpu;
810 int ret = 0;
812 for_each_possible_cpu(cpu) {
813 ret = ftrace_profile_init_cpu(cpu);
814 if (ret)
815 break;
818 return ret;
821 /* interrupts must be disabled */
822 static struct ftrace_profile *
823 ftrace_find_profiled_func(struct ftrace_profile_stat *stat, unsigned long ip)
825 struct ftrace_profile *rec;
826 struct hlist_head *hhd;
827 unsigned long key;
829 key = hash_long(ip, FTRACE_PROFILE_HASH_BITS);
830 hhd = &stat->hash[key];
832 if (hlist_empty(hhd))
833 return NULL;
835 hlist_for_each_entry_rcu_notrace(rec, hhd, node) {
836 if (rec->ip == ip)
837 return rec;
840 return NULL;
843 static void ftrace_add_profile(struct ftrace_profile_stat *stat,
844 struct ftrace_profile *rec)
846 unsigned long key;
848 key = hash_long(rec->ip, FTRACE_PROFILE_HASH_BITS);
849 hlist_add_head_rcu(&rec->node, &stat->hash[key]);
853 * The memory is already allocated, this simply finds a new record to use.
855 static struct ftrace_profile *
856 ftrace_profile_alloc(struct ftrace_profile_stat *stat, unsigned long ip)
858 struct ftrace_profile *rec = NULL;
860 /* prevent recursion (from NMIs) */
861 if (atomic_inc_return(&stat->disabled) != 1)
862 goto out;
865 * Try to find the function again since an NMI
866 * could have added it
868 rec = ftrace_find_profiled_func(stat, ip);
869 if (rec)
870 goto out;
872 if (stat->pages->index == PROFILES_PER_PAGE) {
873 if (!stat->pages->next)
874 goto out;
875 stat->pages = stat->pages->next;
878 rec = &stat->pages->records[stat->pages->index++];
879 rec->ip = ip;
880 ftrace_add_profile(stat, rec);
882 out:
883 atomic_dec(&stat->disabled);
885 return rec;
888 static void
889 function_profile_call(unsigned long ip, unsigned long parent_ip,
890 struct ftrace_ops *ops, struct pt_regs *regs)
892 struct ftrace_profile_stat *stat;
893 struct ftrace_profile *rec;
894 unsigned long flags;
896 if (!ftrace_profile_enabled)
897 return;
899 local_irq_save(flags);
901 stat = &__get_cpu_var(ftrace_profile_stats);
902 if (!stat->hash || !ftrace_profile_enabled)
903 goto out;
905 rec = ftrace_find_profiled_func(stat, ip);
906 if (!rec) {
907 rec = ftrace_profile_alloc(stat, ip);
908 if (!rec)
909 goto out;
912 rec->counter++;
913 out:
914 local_irq_restore(flags);
917 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
918 static int profile_graph_entry(struct ftrace_graph_ent *trace)
920 function_profile_call(trace->func, 0, NULL, NULL);
921 return 1;
924 static void profile_graph_return(struct ftrace_graph_ret *trace)
926 struct ftrace_profile_stat *stat;
927 unsigned long long calltime;
928 struct ftrace_profile *rec;
929 unsigned long flags;
931 local_irq_save(flags);
932 stat = &__get_cpu_var(ftrace_profile_stats);
933 if (!stat->hash || !ftrace_profile_enabled)
934 goto out;
936 /* If the calltime was zero'd ignore it */
937 if (!trace->calltime)
938 goto out;
940 calltime = trace->rettime - trace->calltime;
942 if (!(trace_flags & TRACE_ITER_GRAPH_TIME)) {
943 int index;
945 index = trace->depth;
947 /* Append this call time to the parent time to subtract */
948 if (index)
949 current->ret_stack[index - 1].subtime += calltime;
951 if (current->ret_stack[index].subtime < calltime)
952 calltime -= current->ret_stack[index].subtime;
953 else
954 calltime = 0;
957 rec = ftrace_find_profiled_func(stat, trace->func);
958 if (rec) {
959 rec->time += calltime;
960 rec->time_squared += calltime * calltime;
963 out:
964 local_irq_restore(flags);
967 static int register_ftrace_profiler(void)
969 return register_ftrace_graph(&profile_graph_return,
970 &profile_graph_entry);
973 static void unregister_ftrace_profiler(void)
975 unregister_ftrace_graph();
977 #else
978 static struct ftrace_ops ftrace_profile_ops __read_mostly = {
979 .func = function_profile_call,
980 .flags = FTRACE_OPS_FL_RECURSION_SAFE | FTRACE_OPS_FL_INITIALIZED,
981 INIT_REGEX_LOCK(ftrace_profile_ops)
984 static int register_ftrace_profiler(void)
986 return register_ftrace_function(&ftrace_profile_ops);
989 static void unregister_ftrace_profiler(void)
991 unregister_ftrace_function(&ftrace_profile_ops);
993 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
995 static ssize_t
996 ftrace_profile_write(struct file *filp, const char __user *ubuf,
997 size_t cnt, loff_t *ppos)
999 unsigned long val;
1000 int ret;
1002 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
1003 if (ret)
1004 return ret;
1006 val = !!val;
1008 mutex_lock(&ftrace_profile_lock);
1009 if (ftrace_profile_enabled ^ val) {
1010 if (val) {
1011 ret = ftrace_profile_init();
1012 if (ret < 0) {
1013 cnt = ret;
1014 goto out;
1017 ret = register_ftrace_profiler();
1018 if (ret < 0) {
1019 cnt = ret;
1020 goto out;
1022 ftrace_profile_enabled = 1;
1023 } else {
1024 ftrace_profile_enabled = 0;
1026 * unregister_ftrace_profiler calls stop_machine
1027 * so this acts like an synchronize_sched.
1029 unregister_ftrace_profiler();
1032 out:
1033 mutex_unlock(&ftrace_profile_lock);
1035 *ppos += cnt;
1037 return cnt;
1040 static ssize_t
1041 ftrace_profile_read(struct file *filp, char __user *ubuf,
1042 size_t cnt, loff_t *ppos)
1044 char buf[64]; /* big enough to hold a number */
1045 int r;
1047 r = sprintf(buf, "%u\n", ftrace_profile_enabled);
1048 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
1051 static const struct file_operations ftrace_profile_fops = {
1052 .open = tracing_open_generic,
1053 .read = ftrace_profile_read,
1054 .write = ftrace_profile_write,
1055 .llseek = default_llseek,
1058 /* used to initialize the real stat files */
1059 static struct tracer_stat function_stats __initdata = {
1060 .name = "functions",
1061 .stat_start = function_stat_start,
1062 .stat_next = function_stat_next,
1063 .stat_cmp = function_stat_cmp,
1064 .stat_headers = function_stat_headers,
1065 .stat_show = function_stat_show
1068 static __init void ftrace_profile_debugfs(struct dentry *d_tracer)
1070 struct ftrace_profile_stat *stat;
1071 struct dentry *entry;
1072 char *name;
1073 int ret;
1074 int cpu;
1076 for_each_possible_cpu(cpu) {
1077 stat = &per_cpu(ftrace_profile_stats, cpu);
1079 /* allocate enough for function name + cpu number */
1080 name = kmalloc(32, GFP_KERNEL);
1081 if (!name) {
1083 * The files created are permanent, if something happens
1084 * we still do not free memory.
1086 WARN(1,
1087 "Could not allocate stat file for cpu %d\n",
1088 cpu);
1089 return;
1091 stat->stat = function_stats;
1092 snprintf(name, 32, "function%d", cpu);
1093 stat->stat.name = name;
1094 ret = register_stat_tracer(&stat->stat);
1095 if (ret) {
1096 WARN(1,
1097 "Could not register function stat for cpu %d\n",
1098 cpu);
1099 kfree(name);
1100 return;
1104 entry = debugfs_create_file("function_profile_enabled", 0644,
1105 d_tracer, NULL, &ftrace_profile_fops);
1106 if (!entry)
1107 pr_warning("Could not create debugfs "
1108 "'function_profile_enabled' entry\n");
1111 #else /* CONFIG_FUNCTION_PROFILER */
1112 static __init void ftrace_profile_debugfs(struct dentry *d_tracer)
1115 #endif /* CONFIG_FUNCTION_PROFILER */
1117 static struct pid * const ftrace_swapper_pid = &init_struct_pid;
1119 #ifdef CONFIG_DYNAMIC_FTRACE
1121 #ifndef CONFIG_FTRACE_MCOUNT_RECORD
1122 # error Dynamic ftrace depends on MCOUNT_RECORD
1123 #endif
1125 static struct hlist_head ftrace_func_hash[FTRACE_FUNC_HASHSIZE] __read_mostly;
1127 struct ftrace_func_probe {
1128 struct hlist_node node;
1129 struct ftrace_probe_ops *ops;
1130 unsigned long flags;
1131 unsigned long ip;
1132 void *data;
1133 struct list_head free_list;
1136 struct ftrace_func_entry {
1137 struct hlist_node hlist;
1138 unsigned long ip;
1141 struct ftrace_hash {
1142 unsigned long size_bits;
1143 struct hlist_head *buckets;
1144 unsigned long count;
1145 struct rcu_head rcu;
1149 * We make these constant because no one should touch them,
1150 * but they are used as the default "empty hash", to avoid allocating
1151 * it all the time. These are in a read only section such that if
1152 * anyone does try to modify it, it will cause an exception.
1154 static const struct hlist_head empty_buckets[1];
1155 static const struct ftrace_hash empty_hash = {
1156 .buckets = (struct hlist_head *)empty_buckets,
1158 #define EMPTY_HASH ((struct ftrace_hash *)&empty_hash)
1160 static struct ftrace_ops global_ops = {
1161 .func = ftrace_stub,
1162 .notrace_hash = EMPTY_HASH,
1163 .filter_hash = EMPTY_HASH,
1164 .flags = FTRACE_OPS_FL_RECURSION_SAFE | FTRACE_OPS_FL_INITIALIZED,
1165 INIT_REGEX_LOCK(global_ops)
1168 struct ftrace_page {
1169 struct ftrace_page *next;
1170 struct dyn_ftrace *records;
1171 int index;
1172 int size;
1175 static struct ftrace_page *ftrace_new_pgs;
1177 #define ENTRY_SIZE sizeof(struct dyn_ftrace)
1178 #define ENTRIES_PER_PAGE (PAGE_SIZE / ENTRY_SIZE)
1180 /* estimate from running different kernels */
1181 #define NR_TO_INIT 10000
1183 static struct ftrace_page *ftrace_pages_start;
1184 static struct ftrace_page *ftrace_pages;
1186 static bool ftrace_hash_empty(struct ftrace_hash *hash)
1188 return !hash || !hash->count;
1191 static struct ftrace_func_entry *
1192 ftrace_lookup_ip(struct ftrace_hash *hash, unsigned long ip)
1194 unsigned long key;
1195 struct ftrace_func_entry *entry;
1196 struct hlist_head *hhd;
1198 if (ftrace_hash_empty(hash))
1199 return NULL;
1201 if (hash->size_bits > 0)
1202 key = hash_long(ip, hash->size_bits);
1203 else
1204 key = 0;
1206 hhd = &hash->buckets[key];
1208 hlist_for_each_entry_rcu_notrace(entry, hhd, hlist) {
1209 if (entry->ip == ip)
1210 return entry;
1212 return NULL;
1215 static void __add_hash_entry(struct ftrace_hash *hash,
1216 struct ftrace_func_entry *entry)
1218 struct hlist_head *hhd;
1219 unsigned long key;
1221 if (hash->size_bits)
1222 key = hash_long(entry->ip, hash->size_bits);
1223 else
1224 key = 0;
1226 hhd = &hash->buckets[key];
1227 hlist_add_head(&entry->hlist, hhd);
1228 hash->count++;
1231 static int add_hash_entry(struct ftrace_hash *hash, unsigned long ip)
1233 struct ftrace_func_entry *entry;
1235 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
1236 if (!entry)
1237 return -ENOMEM;
1239 entry->ip = ip;
1240 __add_hash_entry(hash, entry);
1242 return 0;
1245 static void
1246 free_hash_entry(struct ftrace_hash *hash,
1247 struct ftrace_func_entry *entry)
1249 hlist_del(&entry->hlist);
1250 kfree(entry);
1251 hash->count--;
1254 static void
1255 remove_hash_entry(struct ftrace_hash *hash,
1256 struct ftrace_func_entry *entry)
1258 hlist_del(&entry->hlist);
1259 hash->count--;
1262 static void ftrace_hash_clear(struct ftrace_hash *hash)
1264 struct hlist_head *hhd;
1265 struct hlist_node *tn;
1266 struct ftrace_func_entry *entry;
1267 int size = 1 << hash->size_bits;
1268 int i;
1270 if (!hash->count)
1271 return;
1273 for (i = 0; i < size; i++) {
1274 hhd = &hash->buckets[i];
1275 hlist_for_each_entry_safe(entry, tn, hhd, hlist)
1276 free_hash_entry(hash, entry);
1278 FTRACE_WARN_ON(hash->count);
1281 static void free_ftrace_hash(struct ftrace_hash *hash)
1283 if (!hash || hash == EMPTY_HASH)
1284 return;
1285 ftrace_hash_clear(hash);
1286 kfree(hash->buckets);
1287 kfree(hash);
1290 static void __free_ftrace_hash_rcu(struct rcu_head *rcu)
1292 struct ftrace_hash *hash;
1294 hash = container_of(rcu, struct ftrace_hash, rcu);
1295 free_ftrace_hash(hash);
1298 static void free_ftrace_hash_rcu(struct ftrace_hash *hash)
1300 if (!hash || hash == EMPTY_HASH)
1301 return;
1302 call_rcu_sched(&hash->rcu, __free_ftrace_hash_rcu);
1305 void ftrace_free_filter(struct ftrace_ops *ops)
1307 ftrace_ops_init(ops);
1308 free_ftrace_hash(ops->filter_hash);
1309 free_ftrace_hash(ops->notrace_hash);
1312 static struct ftrace_hash *alloc_ftrace_hash(int size_bits)
1314 struct ftrace_hash *hash;
1315 int size;
1317 hash = kzalloc(sizeof(*hash), GFP_KERNEL);
1318 if (!hash)
1319 return NULL;
1321 size = 1 << size_bits;
1322 hash->buckets = kcalloc(size, sizeof(*hash->buckets), GFP_KERNEL);
1324 if (!hash->buckets) {
1325 kfree(hash);
1326 return NULL;
1329 hash->size_bits = size_bits;
1331 return hash;
1334 static struct ftrace_hash *
1335 alloc_and_copy_ftrace_hash(int size_bits, struct ftrace_hash *hash)
1337 struct ftrace_func_entry *entry;
1338 struct ftrace_hash *new_hash;
1339 int size;
1340 int ret;
1341 int i;
1343 new_hash = alloc_ftrace_hash(size_bits);
1344 if (!new_hash)
1345 return NULL;
1347 /* Empty hash? */
1348 if (ftrace_hash_empty(hash))
1349 return new_hash;
1351 size = 1 << hash->size_bits;
1352 for (i = 0; i < size; i++) {
1353 hlist_for_each_entry(entry, &hash->buckets[i], hlist) {
1354 ret = add_hash_entry(new_hash, entry->ip);
1355 if (ret < 0)
1356 goto free_hash;
1360 FTRACE_WARN_ON(new_hash->count != hash->count);
1362 return new_hash;
1364 free_hash:
1365 free_ftrace_hash(new_hash);
1366 return NULL;
1369 static void
1370 ftrace_hash_rec_disable(struct ftrace_ops *ops, int filter_hash);
1371 static void
1372 ftrace_hash_rec_enable(struct ftrace_ops *ops, int filter_hash);
1374 static int
1375 ftrace_hash_move(struct ftrace_ops *ops, int enable,
1376 struct ftrace_hash **dst, struct ftrace_hash *src)
1378 struct ftrace_func_entry *entry;
1379 struct hlist_node *tn;
1380 struct hlist_head *hhd;
1381 struct ftrace_hash *old_hash;
1382 struct ftrace_hash *new_hash;
1383 int size = src->count;
1384 int bits = 0;
1385 int ret;
1386 int i;
1389 * Remove the current set, update the hash and add
1390 * them back.
1392 ftrace_hash_rec_disable(ops, enable);
1395 * If the new source is empty, just free dst and assign it
1396 * the empty_hash.
1398 if (!src->count) {
1399 free_ftrace_hash_rcu(*dst);
1400 rcu_assign_pointer(*dst, EMPTY_HASH);
1401 /* still need to update the function records */
1402 ret = 0;
1403 goto out;
1407 * Make the hash size about 1/2 the # found
1409 for (size /= 2; size; size >>= 1)
1410 bits++;
1412 /* Don't allocate too much */
1413 if (bits > FTRACE_HASH_MAX_BITS)
1414 bits = FTRACE_HASH_MAX_BITS;
1416 ret = -ENOMEM;
1417 new_hash = alloc_ftrace_hash(bits);
1418 if (!new_hash)
1419 goto out;
1421 size = 1 << src->size_bits;
1422 for (i = 0; i < size; i++) {
1423 hhd = &src->buckets[i];
1424 hlist_for_each_entry_safe(entry, tn, hhd, hlist) {
1425 remove_hash_entry(src, entry);
1426 __add_hash_entry(new_hash, entry);
1430 old_hash = *dst;
1431 rcu_assign_pointer(*dst, new_hash);
1432 free_ftrace_hash_rcu(old_hash);
1434 ret = 0;
1435 out:
1437 * Enable regardless of ret:
1438 * On success, we enable the new hash.
1439 * On failure, we re-enable the original hash.
1441 ftrace_hash_rec_enable(ops, enable);
1443 return ret;
1447 * Test the hashes for this ops to see if we want to call
1448 * the ops->func or not.
1450 * It's a match if the ip is in the ops->filter_hash or
1451 * the filter_hash does not exist or is empty,
1452 * AND
1453 * the ip is not in the ops->notrace_hash.
1455 * This needs to be called with preemption disabled as
1456 * the hashes are freed with call_rcu_sched().
1458 static int
1459 ftrace_ops_test(struct ftrace_ops *ops, unsigned long ip, void *regs)
1461 struct ftrace_hash *filter_hash;
1462 struct ftrace_hash *notrace_hash;
1463 int ret;
1465 #ifdef CONFIG_DYNAMIC_FTRACE_WITH_REGS
1467 * There's a small race when adding ops that the ftrace handler
1468 * that wants regs, may be called without them. We can not
1469 * allow that handler to be called if regs is NULL.
1471 if (regs == NULL && (ops->flags & FTRACE_OPS_FL_SAVE_REGS))
1472 return 0;
1473 #endif
1475 filter_hash = rcu_dereference_raw_notrace(ops->filter_hash);
1476 notrace_hash = rcu_dereference_raw_notrace(ops->notrace_hash);
1478 if ((ftrace_hash_empty(filter_hash) ||
1479 ftrace_lookup_ip(filter_hash, ip)) &&
1480 (ftrace_hash_empty(notrace_hash) ||
1481 !ftrace_lookup_ip(notrace_hash, ip)))
1482 ret = 1;
1483 else
1484 ret = 0;
1486 return ret;
1490 * This is a double for. Do not use 'break' to break out of the loop,
1491 * you must use a goto.
1493 #define do_for_each_ftrace_rec(pg, rec) \
1494 for (pg = ftrace_pages_start; pg; pg = pg->next) { \
1495 int _____i; \
1496 for (_____i = 0; _____i < pg->index; _____i++) { \
1497 rec = &pg->records[_____i];
1499 #define while_for_each_ftrace_rec() \
1504 static int ftrace_cmp_recs(const void *a, const void *b)
1506 const struct dyn_ftrace *key = a;
1507 const struct dyn_ftrace *rec = b;
1509 if (key->flags < rec->ip)
1510 return -1;
1511 if (key->ip >= rec->ip + MCOUNT_INSN_SIZE)
1512 return 1;
1513 return 0;
1516 static unsigned long ftrace_location_range(unsigned long start, unsigned long end)
1518 struct ftrace_page *pg;
1519 struct dyn_ftrace *rec;
1520 struct dyn_ftrace key;
1522 key.ip = start;
1523 key.flags = end; /* overload flags, as it is unsigned long */
1525 for (pg = ftrace_pages_start; pg; pg = pg->next) {
1526 if (end < pg->records[0].ip ||
1527 start >= (pg->records[pg->index - 1].ip + MCOUNT_INSN_SIZE))
1528 continue;
1529 rec = bsearch(&key, pg->records, pg->index,
1530 sizeof(struct dyn_ftrace),
1531 ftrace_cmp_recs);
1532 if (rec)
1533 return rec->ip;
1536 return 0;
1540 * ftrace_location - return true if the ip giving is a traced location
1541 * @ip: the instruction pointer to check
1543 * Returns rec->ip if @ip given is a pointer to a ftrace location.
1544 * That is, the instruction that is either a NOP or call to
1545 * the function tracer. It checks the ftrace internal tables to
1546 * determine if the address belongs or not.
1548 unsigned long ftrace_location(unsigned long ip)
1550 return ftrace_location_range(ip, ip);
1554 * ftrace_text_reserved - return true if range contains an ftrace location
1555 * @start: start of range to search
1556 * @end: end of range to search (inclusive). @end points to the last byte to check.
1558 * Returns 1 if @start and @end contains a ftrace location.
1559 * That is, the instruction that is either a NOP or call to
1560 * the function tracer. It checks the ftrace internal tables to
1561 * determine if the address belongs or not.
1563 int ftrace_text_reserved(void *start, void *end)
1565 unsigned long ret;
1567 ret = ftrace_location_range((unsigned long)start,
1568 (unsigned long)end);
1570 return (int)!!ret;
1573 static void __ftrace_hash_rec_update(struct ftrace_ops *ops,
1574 int filter_hash,
1575 bool inc)
1577 struct ftrace_hash *hash;
1578 struct ftrace_hash *other_hash;
1579 struct ftrace_page *pg;
1580 struct dyn_ftrace *rec;
1581 int count = 0;
1582 int all = 0;
1584 /* Only update if the ops has been registered */
1585 if (!(ops->flags & FTRACE_OPS_FL_ENABLED))
1586 return;
1589 * In the filter_hash case:
1590 * If the count is zero, we update all records.
1591 * Otherwise we just update the items in the hash.
1593 * In the notrace_hash case:
1594 * We enable the update in the hash.
1595 * As disabling notrace means enabling the tracing,
1596 * and enabling notrace means disabling, the inc variable
1597 * gets inversed.
1599 if (filter_hash) {
1600 hash = ops->filter_hash;
1601 other_hash = ops->notrace_hash;
1602 if (ftrace_hash_empty(hash))
1603 all = 1;
1604 } else {
1605 inc = !inc;
1606 hash = ops->notrace_hash;
1607 other_hash = ops->filter_hash;
1609 * If the notrace hash has no items,
1610 * then there's nothing to do.
1612 if (ftrace_hash_empty(hash))
1613 return;
1616 do_for_each_ftrace_rec(pg, rec) {
1617 int in_other_hash = 0;
1618 int in_hash = 0;
1619 int match = 0;
1621 if (all) {
1623 * Only the filter_hash affects all records.
1624 * Update if the record is not in the notrace hash.
1626 if (!other_hash || !ftrace_lookup_ip(other_hash, rec->ip))
1627 match = 1;
1628 } else {
1629 in_hash = !!ftrace_lookup_ip(hash, rec->ip);
1630 in_other_hash = !!ftrace_lookup_ip(other_hash, rec->ip);
1635 if (filter_hash && in_hash && !in_other_hash)
1636 match = 1;
1637 else if (!filter_hash && in_hash &&
1638 (in_other_hash || ftrace_hash_empty(other_hash)))
1639 match = 1;
1641 if (!match)
1642 continue;
1644 if (inc) {
1645 rec->flags++;
1646 if (FTRACE_WARN_ON((rec->flags & ~FTRACE_FL_MASK) == FTRACE_REF_MAX))
1647 return;
1649 * If any ops wants regs saved for this function
1650 * then all ops will get saved regs.
1652 if (ops->flags & FTRACE_OPS_FL_SAVE_REGS)
1653 rec->flags |= FTRACE_FL_REGS;
1654 } else {
1655 if (FTRACE_WARN_ON((rec->flags & ~FTRACE_FL_MASK) == 0))
1656 return;
1657 rec->flags--;
1659 count++;
1660 /* Shortcut, if we handled all records, we are done. */
1661 if (!all && count == hash->count)
1662 return;
1663 } while_for_each_ftrace_rec();
1666 static void ftrace_hash_rec_disable(struct ftrace_ops *ops,
1667 int filter_hash)
1669 __ftrace_hash_rec_update(ops, filter_hash, 0);
1672 static void ftrace_hash_rec_enable(struct ftrace_ops *ops,
1673 int filter_hash)
1675 __ftrace_hash_rec_update(ops, filter_hash, 1);
1678 static void print_ip_ins(const char *fmt, unsigned char *p)
1680 int i;
1682 printk(KERN_CONT "%s", fmt);
1684 for (i = 0; i < MCOUNT_INSN_SIZE; i++)
1685 printk(KERN_CONT "%s%02x", i ? ":" : "", p[i]);
1689 * ftrace_bug - report and shutdown function tracer
1690 * @failed: The failed type (EFAULT, EINVAL, EPERM)
1691 * @ip: The address that failed
1693 * The arch code that enables or disables the function tracing
1694 * can call ftrace_bug() when it has detected a problem in
1695 * modifying the code. @failed should be one of either:
1696 * EFAULT - if the problem happens on reading the @ip address
1697 * EINVAL - if what is read at @ip is not what was expected
1698 * EPERM - if the problem happens on writting to the @ip address
1700 void ftrace_bug(int failed, unsigned long ip)
1702 switch (failed) {
1703 case -EFAULT:
1704 FTRACE_WARN_ON_ONCE(1);
1705 pr_info("ftrace faulted on modifying ");
1706 print_ip_sym(ip);
1707 break;
1708 case -EINVAL:
1709 FTRACE_WARN_ON_ONCE(1);
1710 pr_info("ftrace failed to modify ");
1711 print_ip_sym(ip);
1712 print_ip_ins(" actual: ", (unsigned char *)ip);
1713 printk(KERN_CONT "\n");
1714 break;
1715 case -EPERM:
1716 FTRACE_WARN_ON_ONCE(1);
1717 pr_info("ftrace faulted on writing ");
1718 print_ip_sym(ip);
1719 break;
1720 default:
1721 FTRACE_WARN_ON_ONCE(1);
1722 pr_info("ftrace faulted on unknown error ");
1723 print_ip_sym(ip);
1727 static int ftrace_check_record(struct dyn_ftrace *rec, int enable, int update)
1729 unsigned long flag = 0UL;
1732 * If we are updating calls:
1734 * If the record has a ref count, then we need to enable it
1735 * because someone is using it.
1737 * Otherwise we make sure its disabled.
1739 * If we are disabling calls, then disable all records that
1740 * are enabled.
1742 if (enable && (rec->flags & ~FTRACE_FL_MASK))
1743 flag = FTRACE_FL_ENABLED;
1746 * If enabling and the REGS flag does not match the REGS_EN, then
1747 * do not ignore this record. Set flags to fail the compare against
1748 * ENABLED.
1750 if (flag &&
1751 (!(rec->flags & FTRACE_FL_REGS) != !(rec->flags & FTRACE_FL_REGS_EN)))
1752 flag |= FTRACE_FL_REGS;
1754 /* If the state of this record hasn't changed, then do nothing */
1755 if ((rec->flags & FTRACE_FL_ENABLED) == flag)
1756 return FTRACE_UPDATE_IGNORE;
1758 if (flag) {
1759 /* Save off if rec is being enabled (for return value) */
1760 flag ^= rec->flags & FTRACE_FL_ENABLED;
1762 if (update) {
1763 rec->flags |= FTRACE_FL_ENABLED;
1764 if (flag & FTRACE_FL_REGS) {
1765 if (rec->flags & FTRACE_FL_REGS)
1766 rec->flags |= FTRACE_FL_REGS_EN;
1767 else
1768 rec->flags &= ~FTRACE_FL_REGS_EN;
1773 * If this record is being updated from a nop, then
1774 * return UPDATE_MAKE_CALL.
1775 * Otherwise, if the EN flag is set, then return
1776 * UPDATE_MODIFY_CALL_REGS to tell the caller to convert
1777 * from the non-save regs, to a save regs function.
1778 * Otherwise,
1779 * return UPDATE_MODIFY_CALL to tell the caller to convert
1780 * from the save regs, to a non-save regs function.
1782 if (flag & FTRACE_FL_ENABLED)
1783 return FTRACE_UPDATE_MAKE_CALL;
1784 else if (rec->flags & FTRACE_FL_REGS_EN)
1785 return FTRACE_UPDATE_MODIFY_CALL_REGS;
1786 else
1787 return FTRACE_UPDATE_MODIFY_CALL;
1790 if (update) {
1791 /* If there's no more users, clear all flags */
1792 if (!(rec->flags & ~FTRACE_FL_MASK))
1793 rec->flags = 0;
1794 else
1795 /* Just disable the record (keep REGS state) */
1796 rec->flags &= ~FTRACE_FL_ENABLED;
1799 return FTRACE_UPDATE_MAKE_NOP;
1803 * ftrace_update_record, set a record that now is tracing or not
1804 * @rec: the record to update
1805 * @enable: set to 1 if the record is tracing, zero to force disable
1807 * The records that represent all functions that can be traced need
1808 * to be updated when tracing has been enabled.
1810 int ftrace_update_record(struct dyn_ftrace *rec, int enable)
1812 return ftrace_check_record(rec, enable, 1);
1816 * ftrace_test_record, check if the record has been enabled or not
1817 * @rec: the record to test
1818 * @enable: set to 1 to check if enabled, 0 if it is disabled
1820 * The arch code may need to test if a record is already set to
1821 * tracing to determine how to modify the function code that it
1822 * represents.
1824 int ftrace_test_record(struct dyn_ftrace *rec, int enable)
1826 return ftrace_check_record(rec, enable, 0);
1829 static int
1830 __ftrace_replace_code(struct dyn_ftrace *rec, int enable)
1832 unsigned long ftrace_old_addr;
1833 unsigned long ftrace_addr;
1834 int ret;
1836 ret = ftrace_update_record(rec, enable);
1838 if (rec->flags & FTRACE_FL_REGS)
1839 ftrace_addr = (unsigned long)FTRACE_REGS_ADDR;
1840 else
1841 ftrace_addr = (unsigned long)FTRACE_ADDR;
1843 switch (ret) {
1844 case FTRACE_UPDATE_IGNORE:
1845 return 0;
1847 case FTRACE_UPDATE_MAKE_CALL:
1848 return ftrace_make_call(rec, ftrace_addr);
1850 case FTRACE_UPDATE_MAKE_NOP:
1851 return ftrace_make_nop(NULL, rec, ftrace_addr);
1853 case FTRACE_UPDATE_MODIFY_CALL_REGS:
1854 case FTRACE_UPDATE_MODIFY_CALL:
1855 if (rec->flags & FTRACE_FL_REGS)
1856 ftrace_old_addr = (unsigned long)FTRACE_ADDR;
1857 else
1858 ftrace_old_addr = (unsigned long)FTRACE_REGS_ADDR;
1860 return ftrace_modify_call(rec, ftrace_old_addr, ftrace_addr);
1863 return -1; /* unknow ftrace bug */
1866 void __weak ftrace_replace_code(int enable)
1868 struct dyn_ftrace *rec;
1869 struct ftrace_page *pg;
1870 int failed;
1872 if (unlikely(ftrace_disabled))
1873 return;
1875 do_for_each_ftrace_rec(pg, rec) {
1876 failed = __ftrace_replace_code(rec, enable);
1877 if (failed) {
1878 ftrace_bug(failed, rec->ip);
1879 /* Stop processing */
1880 return;
1882 } while_for_each_ftrace_rec();
1885 struct ftrace_rec_iter {
1886 struct ftrace_page *pg;
1887 int index;
1891 * ftrace_rec_iter_start, start up iterating over traced functions
1893 * Returns an iterator handle that is used to iterate over all
1894 * the records that represent address locations where functions
1895 * are traced.
1897 * May return NULL if no records are available.
1899 struct ftrace_rec_iter *ftrace_rec_iter_start(void)
1902 * We only use a single iterator.
1903 * Protected by the ftrace_lock mutex.
1905 static struct ftrace_rec_iter ftrace_rec_iter;
1906 struct ftrace_rec_iter *iter = &ftrace_rec_iter;
1908 iter->pg = ftrace_pages_start;
1909 iter->index = 0;
1911 /* Could have empty pages */
1912 while (iter->pg && !iter->pg->index)
1913 iter->pg = iter->pg->next;
1915 if (!iter->pg)
1916 return NULL;
1918 return iter;
1922 * ftrace_rec_iter_next, get the next record to process.
1923 * @iter: The handle to the iterator.
1925 * Returns the next iterator after the given iterator @iter.
1927 struct ftrace_rec_iter *ftrace_rec_iter_next(struct ftrace_rec_iter *iter)
1929 iter->index++;
1931 if (iter->index >= iter->pg->index) {
1932 iter->pg = iter->pg->next;
1933 iter->index = 0;
1935 /* Could have empty pages */
1936 while (iter->pg && !iter->pg->index)
1937 iter->pg = iter->pg->next;
1940 if (!iter->pg)
1941 return NULL;
1943 return iter;
1947 * ftrace_rec_iter_record, get the record at the iterator location
1948 * @iter: The current iterator location
1950 * Returns the record that the current @iter is at.
1952 struct dyn_ftrace *ftrace_rec_iter_record(struct ftrace_rec_iter *iter)
1954 return &iter->pg->records[iter->index];
1957 static int
1958 ftrace_code_disable(struct module *mod, struct dyn_ftrace *rec)
1960 unsigned long ip;
1961 int ret;
1963 ip = rec->ip;
1965 if (unlikely(ftrace_disabled))
1966 return 0;
1968 ret = ftrace_make_nop(mod, rec, MCOUNT_ADDR);
1969 if (ret) {
1970 ftrace_bug(ret, ip);
1971 return 0;
1973 return 1;
1977 * archs can override this function if they must do something
1978 * before the modifying code is performed.
1980 int __weak ftrace_arch_code_modify_prepare(void)
1982 return 0;
1986 * archs can override this function if they must do something
1987 * after the modifying code is performed.
1989 int __weak ftrace_arch_code_modify_post_process(void)
1991 return 0;
1994 void ftrace_modify_all_code(int command)
1996 int update = command & FTRACE_UPDATE_TRACE_FUNC;
1999 * If the ftrace_caller calls a ftrace_ops func directly,
2000 * we need to make sure that it only traces functions it
2001 * expects to trace. When doing the switch of functions,
2002 * we need to update to the ftrace_ops_list_func first
2003 * before the transition between old and new calls are set,
2004 * as the ftrace_ops_list_func will check the ops hashes
2005 * to make sure the ops are having the right functions
2006 * traced.
2008 if (update)
2009 ftrace_update_ftrace_func(ftrace_ops_list_func);
2011 if (command & FTRACE_UPDATE_CALLS)
2012 ftrace_replace_code(1);
2013 else if (command & FTRACE_DISABLE_CALLS)
2014 ftrace_replace_code(0);
2016 if (update && ftrace_trace_function != ftrace_ops_list_func) {
2017 function_trace_op = set_function_trace_op;
2018 smp_wmb();
2019 /* If irqs are disabled, we are in stop machine */
2020 if (!irqs_disabled())
2021 smp_call_function(ftrace_sync_ipi, NULL, 1);
2022 ftrace_update_ftrace_func(ftrace_trace_function);
2025 if (command & FTRACE_START_FUNC_RET)
2026 ftrace_enable_ftrace_graph_caller();
2027 else if (command & FTRACE_STOP_FUNC_RET)
2028 ftrace_disable_ftrace_graph_caller();
2031 static int __ftrace_modify_code(void *data)
2033 int *command = data;
2035 ftrace_modify_all_code(*command);
2037 return 0;
2041 * ftrace_run_stop_machine, go back to the stop machine method
2042 * @command: The command to tell ftrace what to do
2044 * If an arch needs to fall back to the stop machine method, the
2045 * it can call this function.
2047 void ftrace_run_stop_machine(int command)
2049 stop_machine(__ftrace_modify_code, &command, NULL);
2053 * arch_ftrace_update_code, modify the code to trace or not trace
2054 * @command: The command that needs to be done
2056 * Archs can override this function if it does not need to
2057 * run stop_machine() to modify code.
2059 void __weak arch_ftrace_update_code(int command)
2061 ftrace_run_stop_machine(command);
2064 static void ftrace_run_update_code(int command)
2066 int ret;
2068 ret = ftrace_arch_code_modify_prepare();
2069 FTRACE_WARN_ON(ret);
2070 if (ret)
2071 return;
2073 * Do not call function tracer while we update the code.
2074 * We are in stop machine.
2076 function_trace_stop++;
2079 * By default we use stop_machine() to modify the code.
2080 * But archs can do what ever they want as long as it
2081 * is safe. The stop_machine() is the safest, but also
2082 * produces the most overhead.
2084 arch_ftrace_update_code(command);
2086 function_trace_stop--;
2088 ret = ftrace_arch_code_modify_post_process();
2089 FTRACE_WARN_ON(ret);
2092 static ftrace_func_t saved_ftrace_func;
2093 static int ftrace_start_up;
2094 static int global_start_up;
2096 static void ftrace_startup_enable(int command)
2098 if (saved_ftrace_func != ftrace_trace_function) {
2099 saved_ftrace_func = ftrace_trace_function;
2100 command |= FTRACE_UPDATE_TRACE_FUNC;
2103 if (!command || !ftrace_enabled)
2104 return;
2106 ftrace_run_update_code(command);
2109 static int ftrace_startup(struct ftrace_ops *ops, int command)
2111 bool hash_enable = true;
2112 int ret;
2114 if (unlikely(ftrace_disabled))
2115 return -ENODEV;
2117 ret = __register_ftrace_function(ops);
2118 if (ret)
2119 return ret;
2121 ftrace_start_up++;
2122 command |= FTRACE_UPDATE_CALLS;
2124 /* ops marked global share the filter hashes */
2125 if (ops->flags & FTRACE_OPS_FL_GLOBAL) {
2126 ops = &global_ops;
2127 /* Don't update hash if global is already set */
2128 if (global_start_up)
2129 hash_enable = false;
2130 global_start_up++;
2133 ops->flags |= FTRACE_OPS_FL_ENABLED;
2134 if (hash_enable)
2135 ftrace_hash_rec_enable(ops, 1);
2137 ftrace_startup_enable(command);
2139 return 0;
2142 static int ftrace_shutdown(struct ftrace_ops *ops, int command)
2144 bool hash_disable = true;
2145 int ret;
2147 if (unlikely(ftrace_disabled))
2148 return -ENODEV;
2150 ret = __unregister_ftrace_function(ops);
2151 if (ret)
2152 return ret;
2154 ftrace_start_up--;
2156 * Just warn in case of unbalance, no need to kill ftrace, it's not
2157 * critical but the ftrace_call callers may be never nopped again after
2158 * further ftrace uses.
2160 WARN_ON_ONCE(ftrace_start_up < 0);
2162 if (ops->flags & FTRACE_OPS_FL_GLOBAL) {
2163 ops = &global_ops;
2164 global_start_up--;
2165 WARN_ON_ONCE(global_start_up < 0);
2166 /* Don't update hash if global still has users */
2167 if (global_start_up) {
2168 WARN_ON_ONCE(!ftrace_start_up);
2169 hash_disable = false;
2173 if (hash_disable)
2174 ftrace_hash_rec_disable(ops, 1);
2176 if (ops != &global_ops || !global_start_up)
2177 ops->flags &= ~FTRACE_OPS_FL_ENABLED;
2179 command |= FTRACE_UPDATE_CALLS;
2181 if (saved_ftrace_func != ftrace_trace_function) {
2182 saved_ftrace_func = ftrace_trace_function;
2183 command |= FTRACE_UPDATE_TRACE_FUNC;
2186 if (!command || !ftrace_enabled) {
2188 * If these are control ops, they still need their
2189 * per_cpu field freed. Since, function tracing is
2190 * not currently active, we can just free them
2191 * without synchronizing all CPUs.
2193 if (ops->flags & FTRACE_OPS_FL_CONTROL)
2194 control_ops_free(ops);
2195 return 0;
2198 ftrace_run_update_code(command);
2201 * Dynamic ops may be freed, we must make sure that all
2202 * callers are done before leaving this function.
2203 * The same goes for freeing the per_cpu data of the control
2204 * ops.
2206 * Again, normal synchronize_sched() is not good enough.
2207 * We need to do a hard force of sched synchronization.
2208 * This is because we use preempt_disable() to do RCU, but
2209 * the function tracers can be called where RCU is not watching
2210 * (like before user_exit()). We can not rely on the RCU
2211 * infrastructure to do the synchronization, thus we must do it
2212 * ourselves.
2214 if (ops->flags & (FTRACE_OPS_FL_DYNAMIC | FTRACE_OPS_FL_CONTROL)) {
2215 schedule_on_each_cpu(ftrace_sync);
2217 if (ops->flags & FTRACE_OPS_FL_CONTROL)
2218 control_ops_free(ops);
2221 return 0;
2224 static void ftrace_startup_sysctl(void)
2226 if (unlikely(ftrace_disabled))
2227 return;
2229 /* Force update next time */
2230 saved_ftrace_func = NULL;
2231 /* ftrace_start_up is true if we want ftrace running */
2232 if (ftrace_start_up)
2233 ftrace_run_update_code(FTRACE_UPDATE_CALLS);
2236 static void ftrace_shutdown_sysctl(void)
2238 if (unlikely(ftrace_disabled))
2239 return;
2241 /* ftrace_start_up is true if ftrace is running */
2242 if (ftrace_start_up)
2243 ftrace_run_update_code(FTRACE_DISABLE_CALLS);
2246 static cycle_t ftrace_update_time;
2247 static unsigned long ftrace_update_cnt;
2248 unsigned long ftrace_update_tot_cnt;
2250 static inline int ops_traces_mod(struct ftrace_ops *ops)
2253 * Filter_hash being empty will default to trace module.
2254 * But notrace hash requires a test of individual module functions.
2256 return ftrace_hash_empty(ops->filter_hash) &&
2257 ftrace_hash_empty(ops->notrace_hash);
2261 * Check if the current ops references the record.
2263 * If the ops traces all functions, then it was already accounted for.
2264 * If the ops does not trace the current record function, skip it.
2265 * If the ops ignores the function via notrace filter, skip it.
2267 static inline bool
2268 ops_references_rec(struct ftrace_ops *ops, struct dyn_ftrace *rec)
2270 /* If ops isn't enabled, ignore it */
2271 if (!(ops->flags & FTRACE_OPS_FL_ENABLED))
2272 return 0;
2274 /* If ops traces all mods, we already accounted for it */
2275 if (ops_traces_mod(ops))
2276 return 0;
2278 /* The function must be in the filter */
2279 if (!ftrace_hash_empty(ops->filter_hash) &&
2280 !ftrace_lookup_ip(ops->filter_hash, rec->ip))
2281 return 0;
2283 /* If in notrace hash, we ignore it too */
2284 if (ftrace_lookup_ip(ops->notrace_hash, rec->ip))
2285 return 0;
2287 return 1;
2290 static int referenced_filters(struct dyn_ftrace *rec)
2292 struct ftrace_ops *ops;
2293 int cnt = 0;
2295 for (ops = ftrace_ops_list; ops != &ftrace_list_end; ops = ops->next) {
2296 if (ops_references_rec(ops, rec))
2297 cnt++;
2300 return cnt;
2303 static int ftrace_update_code(struct module *mod)
2305 struct ftrace_page *pg;
2306 struct dyn_ftrace *p;
2307 cycle_t start, stop;
2308 unsigned long ref = 0;
2309 bool test = false;
2310 int i;
2313 * When adding a module, we need to check if tracers are
2314 * currently enabled and if they are set to trace all functions.
2315 * If they are, we need to enable the module functions as well
2316 * as update the reference counts for those function records.
2318 if (mod) {
2319 struct ftrace_ops *ops;
2321 for (ops = ftrace_ops_list;
2322 ops != &ftrace_list_end; ops = ops->next) {
2323 if (ops->flags & FTRACE_OPS_FL_ENABLED) {
2324 if (ops_traces_mod(ops))
2325 ref++;
2326 else
2327 test = true;
2332 start = ftrace_now(raw_smp_processor_id());
2333 ftrace_update_cnt = 0;
2335 for (pg = ftrace_new_pgs; pg; pg = pg->next) {
2337 for (i = 0; i < pg->index; i++) {
2338 int cnt = ref;
2340 /* If something went wrong, bail without enabling anything */
2341 if (unlikely(ftrace_disabled))
2342 return -1;
2344 p = &pg->records[i];
2345 if (test)
2346 cnt += referenced_filters(p);
2347 p->flags = cnt;
2350 * Do the initial record conversion from mcount jump
2351 * to the NOP instructions.
2353 if (!ftrace_code_disable(mod, p))
2354 break;
2356 ftrace_update_cnt++;
2359 * If the tracing is enabled, go ahead and enable the record.
2361 * The reason not to enable the record immediatelly is the
2362 * inherent check of ftrace_make_nop/ftrace_make_call for
2363 * correct previous instructions. Making first the NOP
2364 * conversion puts the module to the correct state, thus
2365 * passing the ftrace_make_call check.
2367 if (ftrace_start_up && cnt) {
2368 int failed = __ftrace_replace_code(p, 1);
2369 if (failed)
2370 ftrace_bug(failed, p->ip);
2375 ftrace_new_pgs = NULL;
2377 stop = ftrace_now(raw_smp_processor_id());
2378 ftrace_update_time = stop - start;
2379 ftrace_update_tot_cnt += ftrace_update_cnt;
2381 return 0;
2384 static int ftrace_allocate_records(struct ftrace_page *pg, int count)
2386 int order;
2387 int cnt;
2389 if (WARN_ON(!count))
2390 return -EINVAL;
2392 order = get_count_order(DIV_ROUND_UP(count, ENTRIES_PER_PAGE));
2395 * We want to fill as much as possible. No more than a page
2396 * may be empty.
2398 while ((PAGE_SIZE << order) / ENTRY_SIZE >= count + ENTRIES_PER_PAGE)
2399 order--;
2401 again:
2402 pg->records = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, order);
2404 if (!pg->records) {
2405 /* if we can't allocate this size, try something smaller */
2406 if (!order)
2407 return -ENOMEM;
2408 order >>= 1;
2409 goto again;
2412 cnt = (PAGE_SIZE << order) / ENTRY_SIZE;
2413 pg->size = cnt;
2415 if (cnt > count)
2416 cnt = count;
2418 return cnt;
2421 static struct ftrace_page *
2422 ftrace_allocate_pages(unsigned long num_to_init)
2424 struct ftrace_page *start_pg;
2425 struct ftrace_page *pg;
2426 int order;
2427 int cnt;
2429 if (!num_to_init)
2430 return 0;
2432 start_pg = pg = kzalloc(sizeof(*pg), GFP_KERNEL);
2433 if (!pg)
2434 return NULL;
2437 * Try to allocate as much as possible in one continues
2438 * location that fills in all of the space. We want to
2439 * waste as little space as possible.
2441 for (;;) {
2442 cnt = ftrace_allocate_records(pg, num_to_init);
2443 if (cnt < 0)
2444 goto free_pages;
2446 num_to_init -= cnt;
2447 if (!num_to_init)
2448 break;
2450 pg->next = kzalloc(sizeof(*pg), GFP_KERNEL);
2451 if (!pg->next)
2452 goto free_pages;
2454 pg = pg->next;
2457 return start_pg;
2459 free_pages:
2460 while (start_pg) {
2461 order = get_count_order(pg->size / ENTRIES_PER_PAGE);
2462 free_pages((unsigned long)pg->records, order);
2463 start_pg = pg->next;
2464 kfree(pg);
2465 pg = start_pg;
2467 pr_info("ftrace: FAILED to allocate memory for functions\n");
2468 return NULL;
2471 static int __init ftrace_dyn_table_alloc(unsigned long num_to_init)
2473 int cnt;
2475 if (!num_to_init) {
2476 pr_info("ftrace: No functions to be traced?\n");
2477 return -1;
2480 cnt = num_to_init / ENTRIES_PER_PAGE;
2481 pr_info("ftrace: allocating %ld entries in %d pages\n",
2482 num_to_init, cnt + 1);
2484 return 0;
2487 #define FTRACE_BUFF_MAX (KSYM_SYMBOL_LEN+4) /* room for wildcards */
2489 struct ftrace_iterator {
2490 loff_t pos;
2491 loff_t func_pos;
2492 struct ftrace_page *pg;
2493 struct dyn_ftrace *func;
2494 struct ftrace_func_probe *probe;
2495 struct trace_parser parser;
2496 struct ftrace_hash *hash;
2497 struct ftrace_ops *ops;
2498 int hidx;
2499 int idx;
2500 unsigned flags;
2503 static void *
2504 t_hash_next(struct seq_file *m, loff_t *pos)
2506 struct ftrace_iterator *iter = m->private;
2507 struct hlist_node *hnd = NULL;
2508 struct hlist_head *hhd;
2510 (*pos)++;
2511 iter->pos = *pos;
2513 if (iter->probe)
2514 hnd = &iter->probe->node;
2515 retry:
2516 if (iter->hidx >= FTRACE_FUNC_HASHSIZE)
2517 return NULL;
2519 hhd = &ftrace_func_hash[iter->hidx];
2521 if (hlist_empty(hhd)) {
2522 iter->hidx++;
2523 hnd = NULL;
2524 goto retry;
2527 if (!hnd)
2528 hnd = hhd->first;
2529 else {
2530 hnd = hnd->next;
2531 if (!hnd) {
2532 iter->hidx++;
2533 goto retry;
2537 if (WARN_ON_ONCE(!hnd))
2538 return NULL;
2540 iter->probe = hlist_entry(hnd, struct ftrace_func_probe, node);
2542 return iter;
2545 static void *t_hash_start(struct seq_file *m, loff_t *pos)
2547 struct ftrace_iterator *iter = m->private;
2548 void *p = NULL;
2549 loff_t l;
2551 if (!(iter->flags & FTRACE_ITER_DO_HASH))
2552 return NULL;
2554 if (iter->func_pos > *pos)
2555 return NULL;
2557 iter->hidx = 0;
2558 for (l = 0; l <= (*pos - iter->func_pos); ) {
2559 p = t_hash_next(m, &l);
2560 if (!p)
2561 break;
2563 if (!p)
2564 return NULL;
2566 /* Only set this if we have an item */
2567 iter->flags |= FTRACE_ITER_HASH;
2569 return iter;
2572 static int
2573 t_hash_show(struct seq_file *m, struct ftrace_iterator *iter)
2575 struct ftrace_func_probe *rec;
2577 rec = iter->probe;
2578 if (WARN_ON_ONCE(!rec))
2579 return -EIO;
2581 if (rec->ops->print)
2582 return rec->ops->print(m, rec->ip, rec->ops, rec->data);
2584 seq_printf(m, "%ps:%ps", (void *)rec->ip, (void *)rec->ops->func);
2586 if (rec->data)
2587 seq_printf(m, ":%p", rec->data);
2588 seq_putc(m, '\n');
2590 return 0;
2593 static void *
2594 t_next(struct seq_file *m, void *v, loff_t *pos)
2596 struct ftrace_iterator *iter = m->private;
2597 struct ftrace_ops *ops = iter->ops;
2598 struct dyn_ftrace *rec = NULL;
2600 if (unlikely(ftrace_disabled))
2601 return NULL;
2603 if (iter->flags & FTRACE_ITER_HASH)
2604 return t_hash_next(m, pos);
2606 (*pos)++;
2607 iter->pos = iter->func_pos = *pos;
2609 if (iter->flags & FTRACE_ITER_PRINTALL)
2610 return t_hash_start(m, pos);
2612 retry:
2613 if (iter->idx >= iter->pg->index) {
2614 if (iter->pg->next) {
2615 iter->pg = iter->pg->next;
2616 iter->idx = 0;
2617 goto retry;
2619 } else {
2620 rec = &iter->pg->records[iter->idx++];
2621 if (((iter->flags & FTRACE_ITER_FILTER) &&
2622 !(ftrace_lookup_ip(ops->filter_hash, rec->ip))) ||
2624 ((iter->flags & FTRACE_ITER_NOTRACE) &&
2625 !ftrace_lookup_ip(ops->notrace_hash, rec->ip)) ||
2627 ((iter->flags & FTRACE_ITER_ENABLED) &&
2628 !(rec->flags & FTRACE_FL_ENABLED))) {
2630 rec = NULL;
2631 goto retry;
2635 if (!rec)
2636 return t_hash_start(m, pos);
2638 iter->func = rec;
2640 return iter;
2643 static void reset_iter_read(struct ftrace_iterator *iter)
2645 iter->pos = 0;
2646 iter->func_pos = 0;
2647 iter->flags &= ~(FTRACE_ITER_PRINTALL | FTRACE_ITER_HASH);
2650 static void *t_start(struct seq_file *m, loff_t *pos)
2652 struct ftrace_iterator *iter = m->private;
2653 struct ftrace_ops *ops = iter->ops;
2654 void *p = NULL;
2655 loff_t l;
2657 mutex_lock(&ftrace_lock);
2659 if (unlikely(ftrace_disabled))
2660 return NULL;
2663 * If an lseek was done, then reset and start from beginning.
2665 if (*pos < iter->pos)
2666 reset_iter_read(iter);
2669 * For set_ftrace_filter reading, if we have the filter
2670 * off, we can short cut and just print out that all
2671 * functions are enabled.
2673 if (iter->flags & FTRACE_ITER_FILTER &&
2674 ftrace_hash_empty(ops->filter_hash)) {
2675 if (*pos > 0)
2676 return t_hash_start(m, pos);
2677 iter->flags |= FTRACE_ITER_PRINTALL;
2678 /* reset in case of seek/pread */
2679 iter->flags &= ~FTRACE_ITER_HASH;
2680 return iter;
2683 if (iter->flags & FTRACE_ITER_HASH)
2684 return t_hash_start(m, pos);
2687 * Unfortunately, we need to restart at ftrace_pages_start
2688 * every time we let go of the ftrace_mutex. This is because
2689 * those pointers can change without the lock.
2691 iter->pg = ftrace_pages_start;
2692 iter->idx = 0;
2693 for (l = 0; l <= *pos; ) {
2694 p = t_next(m, p, &l);
2695 if (!p)
2696 break;
2699 if (!p)
2700 return t_hash_start(m, pos);
2702 return iter;
2705 static void t_stop(struct seq_file *m, void *p)
2707 mutex_unlock(&ftrace_lock);
2710 static int t_show(struct seq_file *m, void *v)
2712 struct ftrace_iterator *iter = m->private;
2713 struct dyn_ftrace *rec;
2715 if (iter->flags & FTRACE_ITER_HASH)
2716 return t_hash_show(m, iter);
2718 if (iter->flags & FTRACE_ITER_PRINTALL) {
2719 seq_printf(m, "#### all functions enabled ####\n");
2720 return 0;
2723 rec = iter->func;
2725 if (!rec)
2726 return 0;
2728 seq_printf(m, "%ps", (void *)rec->ip);
2729 if (iter->flags & FTRACE_ITER_ENABLED)
2730 seq_printf(m, " (%ld)%s",
2731 rec->flags & ~FTRACE_FL_MASK,
2732 rec->flags & FTRACE_FL_REGS ? " R" : "");
2733 seq_printf(m, "\n");
2735 return 0;
2738 static const struct seq_operations show_ftrace_seq_ops = {
2739 .start = t_start,
2740 .next = t_next,
2741 .stop = t_stop,
2742 .show = t_show,
2745 static int
2746 ftrace_avail_open(struct inode *inode, struct file *file)
2748 struct ftrace_iterator *iter;
2750 if (unlikely(ftrace_disabled))
2751 return -ENODEV;
2753 iter = __seq_open_private(file, &show_ftrace_seq_ops, sizeof(*iter));
2754 if (iter) {
2755 iter->pg = ftrace_pages_start;
2756 iter->ops = &global_ops;
2759 return iter ? 0 : -ENOMEM;
2762 static int
2763 ftrace_enabled_open(struct inode *inode, struct file *file)
2765 struct ftrace_iterator *iter;
2767 if (unlikely(ftrace_disabled))
2768 return -ENODEV;
2770 iter = __seq_open_private(file, &show_ftrace_seq_ops, sizeof(*iter));
2771 if (iter) {
2772 iter->pg = ftrace_pages_start;
2773 iter->flags = FTRACE_ITER_ENABLED;
2774 iter->ops = &global_ops;
2777 return iter ? 0 : -ENOMEM;
2780 static void ftrace_filter_reset(struct ftrace_hash *hash)
2782 mutex_lock(&ftrace_lock);
2783 ftrace_hash_clear(hash);
2784 mutex_unlock(&ftrace_lock);
2788 * ftrace_regex_open - initialize function tracer filter files
2789 * @ops: The ftrace_ops that hold the hash filters
2790 * @flag: The type of filter to process
2791 * @inode: The inode, usually passed in to your open routine
2792 * @file: The file, usually passed in to your open routine
2794 * ftrace_regex_open() initializes the filter files for the
2795 * @ops. Depending on @flag it may process the filter hash or
2796 * the notrace hash of @ops. With this called from the open
2797 * routine, you can use ftrace_filter_write() for the write
2798 * routine if @flag has FTRACE_ITER_FILTER set, or
2799 * ftrace_notrace_write() if @flag has FTRACE_ITER_NOTRACE set.
2800 * tracing_lseek() should be used as the lseek routine, and
2801 * release must call ftrace_regex_release().
2804 ftrace_regex_open(struct ftrace_ops *ops, int flag,
2805 struct inode *inode, struct file *file)
2807 struct ftrace_iterator *iter;
2808 struct ftrace_hash *hash;
2809 int ret = 0;
2811 ftrace_ops_init(ops);
2813 if (unlikely(ftrace_disabled))
2814 return -ENODEV;
2816 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
2817 if (!iter)
2818 return -ENOMEM;
2820 if (trace_parser_get_init(&iter->parser, FTRACE_BUFF_MAX)) {
2821 kfree(iter);
2822 return -ENOMEM;
2825 iter->ops = ops;
2826 iter->flags = flag;
2828 mutex_lock(&ops->regex_lock);
2830 if (flag & FTRACE_ITER_NOTRACE)
2831 hash = ops->notrace_hash;
2832 else
2833 hash = ops->filter_hash;
2835 if (file->f_mode & FMODE_WRITE) {
2836 iter->hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, hash);
2837 if (!iter->hash) {
2838 trace_parser_put(&iter->parser);
2839 kfree(iter);
2840 ret = -ENOMEM;
2841 goto out_unlock;
2845 if ((file->f_mode & FMODE_WRITE) &&
2846 (file->f_flags & O_TRUNC))
2847 ftrace_filter_reset(iter->hash);
2849 if (file->f_mode & FMODE_READ) {
2850 iter->pg = ftrace_pages_start;
2852 ret = seq_open(file, &show_ftrace_seq_ops);
2853 if (!ret) {
2854 struct seq_file *m = file->private_data;
2855 m->private = iter;
2856 } else {
2857 /* Failed */
2858 free_ftrace_hash(iter->hash);
2859 trace_parser_put(&iter->parser);
2860 kfree(iter);
2862 } else
2863 file->private_data = iter;
2865 out_unlock:
2866 mutex_unlock(&ops->regex_lock);
2868 return ret;
2871 static int
2872 ftrace_filter_open(struct inode *inode, struct file *file)
2874 return ftrace_regex_open(&global_ops,
2875 FTRACE_ITER_FILTER | FTRACE_ITER_DO_HASH,
2876 inode, file);
2879 static int
2880 ftrace_notrace_open(struct inode *inode, struct file *file)
2882 return ftrace_regex_open(&global_ops, FTRACE_ITER_NOTRACE,
2883 inode, file);
2886 static int ftrace_match(char *str, char *regex, int len, int type)
2888 int matched = 0;
2889 int slen;
2891 switch (type) {
2892 case MATCH_FULL:
2893 if (strcmp(str, regex) == 0)
2894 matched = 1;
2895 break;
2896 case MATCH_FRONT_ONLY:
2897 if (strncmp(str, regex, len) == 0)
2898 matched = 1;
2899 break;
2900 case MATCH_MIDDLE_ONLY:
2901 if (strstr(str, regex))
2902 matched = 1;
2903 break;
2904 case MATCH_END_ONLY:
2905 slen = strlen(str);
2906 if (slen >= len && memcmp(str + slen - len, regex, len) == 0)
2907 matched = 1;
2908 break;
2911 return matched;
2914 static int
2915 enter_record(struct ftrace_hash *hash, struct dyn_ftrace *rec, int not)
2917 struct ftrace_func_entry *entry;
2918 int ret = 0;
2920 entry = ftrace_lookup_ip(hash, rec->ip);
2921 if (not) {
2922 /* Do nothing if it doesn't exist */
2923 if (!entry)
2924 return 0;
2926 free_hash_entry(hash, entry);
2927 } else {
2928 /* Do nothing if it exists */
2929 if (entry)
2930 return 0;
2932 ret = add_hash_entry(hash, rec->ip);
2934 return ret;
2937 static int
2938 ftrace_match_record(struct dyn_ftrace *rec, char *mod,
2939 char *regex, int len, int type)
2941 char str[KSYM_SYMBOL_LEN];
2942 char *modname;
2944 kallsyms_lookup(rec->ip, NULL, NULL, &modname, str);
2946 if (mod) {
2947 /* module lookup requires matching the module */
2948 if (!modname || strcmp(modname, mod))
2949 return 0;
2951 /* blank search means to match all funcs in the mod */
2952 if (!len)
2953 return 1;
2956 return ftrace_match(str, regex, len, type);
2959 static int
2960 match_records(struct ftrace_hash *hash, char *buff,
2961 int len, char *mod, int not)
2963 unsigned search_len = 0;
2964 struct ftrace_page *pg;
2965 struct dyn_ftrace *rec;
2966 int type = MATCH_FULL;
2967 char *search = buff;
2968 int found = 0;
2969 int ret;
2971 if (len) {
2972 type = filter_parse_regex(buff, len, &search, &not);
2973 search_len = strlen(search);
2976 mutex_lock(&ftrace_lock);
2978 if (unlikely(ftrace_disabled))
2979 goto out_unlock;
2981 do_for_each_ftrace_rec(pg, rec) {
2982 if (ftrace_match_record(rec, mod, search, search_len, type)) {
2983 ret = enter_record(hash, rec, not);
2984 if (ret < 0) {
2985 found = ret;
2986 goto out_unlock;
2988 found = 1;
2990 } while_for_each_ftrace_rec();
2991 out_unlock:
2992 mutex_unlock(&ftrace_lock);
2994 return found;
2997 static int
2998 ftrace_match_records(struct ftrace_hash *hash, char *buff, int len)
3000 return match_records(hash, buff, len, NULL, 0);
3003 static int
3004 ftrace_match_module_records(struct ftrace_hash *hash, char *buff, char *mod)
3006 int not = 0;
3008 /* blank or '*' mean the same */
3009 if (strcmp(buff, "*") == 0)
3010 buff[0] = 0;
3012 /* handle the case of 'dont filter this module' */
3013 if (strcmp(buff, "!") == 0 || strcmp(buff, "!*") == 0) {
3014 buff[0] = 0;
3015 not = 1;
3018 return match_records(hash, buff, strlen(buff), mod, not);
3022 * We register the module command as a template to show others how
3023 * to register the a command as well.
3026 static int
3027 ftrace_mod_callback(struct ftrace_hash *hash,
3028 char *func, char *cmd, char *param, int enable)
3030 char *mod;
3031 int ret = -EINVAL;
3034 * cmd == 'mod' because we only registered this func
3035 * for the 'mod' ftrace_func_command.
3036 * But if you register one func with multiple commands,
3037 * you can tell which command was used by the cmd
3038 * parameter.
3041 /* we must have a module name */
3042 if (!param)
3043 return ret;
3045 mod = strsep(&param, ":");
3046 if (!strlen(mod))
3047 return ret;
3049 ret = ftrace_match_module_records(hash, func, mod);
3050 if (!ret)
3051 ret = -EINVAL;
3052 if (ret < 0)
3053 return ret;
3055 return 0;
3058 static struct ftrace_func_command ftrace_mod_cmd = {
3059 .name = "mod",
3060 .func = ftrace_mod_callback,
3063 static int __init ftrace_mod_cmd_init(void)
3065 return register_ftrace_command(&ftrace_mod_cmd);
3067 core_initcall(ftrace_mod_cmd_init);
3069 static void function_trace_probe_call(unsigned long ip, unsigned long parent_ip,
3070 struct ftrace_ops *op, struct pt_regs *pt_regs)
3072 struct ftrace_func_probe *entry;
3073 struct hlist_head *hhd;
3074 unsigned long key;
3076 key = hash_long(ip, FTRACE_HASH_BITS);
3078 hhd = &ftrace_func_hash[key];
3080 if (hlist_empty(hhd))
3081 return;
3084 * Disable preemption for these calls to prevent a RCU grace
3085 * period. This syncs the hash iteration and freeing of items
3086 * on the hash. rcu_read_lock is too dangerous here.
3088 preempt_disable_notrace();
3089 hlist_for_each_entry_rcu_notrace(entry, hhd, node) {
3090 if (entry->ip == ip)
3091 entry->ops->func(ip, parent_ip, &entry->data);
3093 preempt_enable_notrace();
3096 static struct ftrace_ops trace_probe_ops __read_mostly =
3098 .func = function_trace_probe_call,
3099 .flags = FTRACE_OPS_FL_INITIALIZED,
3100 INIT_REGEX_LOCK(trace_probe_ops)
3103 static int ftrace_probe_registered;
3105 static void __enable_ftrace_function_probe(void)
3107 int ret;
3108 int i;
3110 if (ftrace_probe_registered) {
3111 /* still need to update the function call sites */
3112 if (ftrace_enabled)
3113 ftrace_run_update_code(FTRACE_UPDATE_CALLS);
3114 return;
3117 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
3118 struct hlist_head *hhd = &ftrace_func_hash[i];
3119 if (hhd->first)
3120 break;
3122 /* Nothing registered? */
3123 if (i == FTRACE_FUNC_HASHSIZE)
3124 return;
3126 ret = ftrace_startup(&trace_probe_ops, 0);
3128 ftrace_probe_registered = 1;
3131 static void __disable_ftrace_function_probe(void)
3133 int i;
3135 if (!ftrace_probe_registered)
3136 return;
3138 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
3139 struct hlist_head *hhd = &ftrace_func_hash[i];
3140 if (hhd->first)
3141 return;
3144 /* no more funcs left */
3145 ftrace_shutdown(&trace_probe_ops, 0);
3147 ftrace_probe_registered = 0;
3151 static void ftrace_free_entry(struct ftrace_func_probe *entry)
3153 if (entry->ops->free)
3154 entry->ops->free(entry->ops, entry->ip, &entry->data);
3155 kfree(entry);
3159 register_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
3160 void *data)
3162 struct ftrace_func_probe *entry;
3163 struct ftrace_hash **orig_hash = &trace_probe_ops.filter_hash;
3164 struct ftrace_hash *hash;
3165 struct ftrace_page *pg;
3166 struct dyn_ftrace *rec;
3167 int type, len, not;
3168 unsigned long key;
3169 int count = 0;
3170 char *search;
3171 int ret;
3173 type = filter_parse_regex(glob, strlen(glob), &search, &not);
3174 len = strlen(search);
3176 /* we do not support '!' for function probes */
3177 if (WARN_ON(not))
3178 return -EINVAL;
3180 mutex_lock(&trace_probe_ops.regex_lock);
3182 hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, *orig_hash);
3183 if (!hash) {
3184 count = -ENOMEM;
3185 goto out;
3188 if (unlikely(ftrace_disabled)) {
3189 count = -ENODEV;
3190 goto out;
3193 mutex_lock(&ftrace_lock);
3195 do_for_each_ftrace_rec(pg, rec) {
3197 if (!ftrace_match_record(rec, NULL, search, len, type))
3198 continue;
3200 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
3201 if (!entry) {
3202 /* If we did not process any, then return error */
3203 if (!count)
3204 count = -ENOMEM;
3205 goto out_unlock;
3208 count++;
3210 entry->data = data;
3213 * The caller might want to do something special
3214 * for each function we find. We call the callback
3215 * to give the caller an opportunity to do so.
3217 if (ops->init) {
3218 if (ops->init(ops, rec->ip, &entry->data) < 0) {
3219 /* caller does not like this func */
3220 kfree(entry);
3221 continue;
3225 ret = enter_record(hash, rec, 0);
3226 if (ret < 0) {
3227 kfree(entry);
3228 count = ret;
3229 goto out_unlock;
3232 entry->ops = ops;
3233 entry->ip = rec->ip;
3235 key = hash_long(entry->ip, FTRACE_HASH_BITS);
3236 hlist_add_head_rcu(&entry->node, &ftrace_func_hash[key]);
3238 } while_for_each_ftrace_rec();
3240 ret = ftrace_hash_move(&trace_probe_ops, 1, orig_hash, hash);
3241 if (ret < 0)
3242 count = ret;
3244 __enable_ftrace_function_probe();
3246 out_unlock:
3247 mutex_unlock(&ftrace_lock);
3248 out:
3249 mutex_unlock(&trace_probe_ops.regex_lock);
3250 free_ftrace_hash(hash);
3252 return count;
3255 enum {
3256 PROBE_TEST_FUNC = 1,
3257 PROBE_TEST_DATA = 2
3260 static void
3261 __unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
3262 void *data, int flags)
3264 struct ftrace_func_entry *rec_entry;
3265 struct ftrace_func_probe *entry;
3266 struct ftrace_func_probe *p;
3267 struct ftrace_hash **orig_hash = &trace_probe_ops.filter_hash;
3268 struct list_head free_list;
3269 struct ftrace_hash *hash;
3270 struct hlist_node *tmp;
3271 char str[KSYM_SYMBOL_LEN];
3272 int type = MATCH_FULL;
3273 int i, len = 0;
3274 char *search;
3276 if (glob && (strcmp(glob, "*") == 0 || !strlen(glob)))
3277 glob = NULL;
3278 else if (glob) {
3279 int not;
3281 type = filter_parse_regex(glob, strlen(glob), &search, &not);
3282 len = strlen(search);
3284 /* we do not support '!' for function probes */
3285 if (WARN_ON(not))
3286 return;
3289 mutex_lock(&trace_probe_ops.regex_lock);
3291 hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, *orig_hash);
3292 if (!hash)
3293 /* Hmm, should report this somehow */
3294 goto out_unlock;
3296 INIT_LIST_HEAD(&free_list);
3298 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
3299 struct hlist_head *hhd = &ftrace_func_hash[i];
3301 hlist_for_each_entry_safe(entry, tmp, hhd, node) {
3303 /* break up if statements for readability */
3304 if ((flags & PROBE_TEST_FUNC) && entry->ops != ops)
3305 continue;
3307 if ((flags & PROBE_TEST_DATA) && entry->data != data)
3308 continue;
3310 /* do this last, since it is the most expensive */
3311 if (glob) {
3312 kallsyms_lookup(entry->ip, NULL, NULL,
3313 NULL, str);
3314 if (!ftrace_match(str, glob, len, type))
3315 continue;
3318 rec_entry = ftrace_lookup_ip(hash, entry->ip);
3319 /* It is possible more than one entry had this ip */
3320 if (rec_entry)
3321 free_hash_entry(hash, rec_entry);
3323 hlist_del_rcu(&entry->node);
3324 list_add(&entry->free_list, &free_list);
3327 mutex_lock(&ftrace_lock);
3328 __disable_ftrace_function_probe();
3330 * Remove after the disable is called. Otherwise, if the last
3331 * probe is removed, a null hash means *all enabled*.
3333 ftrace_hash_move(&trace_probe_ops, 1, orig_hash, hash);
3334 synchronize_sched();
3335 list_for_each_entry_safe(entry, p, &free_list, free_list) {
3336 list_del(&entry->free_list);
3337 ftrace_free_entry(entry);
3339 mutex_unlock(&ftrace_lock);
3341 out_unlock:
3342 mutex_unlock(&trace_probe_ops.regex_lock);
3343 free_ftrace_hash(hash);
3346 void
3347 unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
3348 void *data)
3350 __unregister_ftrace_function_probe(glob, ops, data,
3351 PROBE_TEST_FUNC | PROBE_TEST_DATA);
3354 void
3355 unregister_ftrace_function_probe_func(char *glob, struct ftrace_probe_ops *ops)
3357 __unregister_ftrace_function_probe(glob, ops, NULL, PROBE_TEST_FUNC);
3360 void unregister_ftrace_function_probe_all(char *glob)
3362 __unregister_ftrace_function_probe(glob, NULL, NULL, 0);
3365 static LIST_HEAD(ftrace_commands);
3366 static DEFINE_MUTEX(ftrace_cmd_mutex);
3369 * Currently we only register ftrace commands from __init, so mark this
3370 * __init too.
3372 __init int register_ftrace_command(struct ftrace_func_command *cmd)
3374 struct ftrace_func_command *p;
3375 int ret = 0;
3377 mutex_lock(&ftrace_cmd_mutex);
3378 list_for_each_entry(p, &ftrace_commands, list) {
3379 if (strcmp(cmd->name, p->name) == 0) {
3380 ret = -EBUSY;
3381 goto out_unlock;
3384 list_add(&cmd->list, &ftrace_commands);
3385 out_unlock:
3386 mutex_unlock(&ftrace_cmd_mutex);
3388 return ret;
3392 * Currently we only unregister ftrace commands from __init, so mark
3393 * this __init too.
3395 __init int unregister_ftrace_command(struct ftrace_func_command *cmd)
3397 struct ftrace_func_command *p, *n;
3398 int ret = -ENODEV;
3400 mutex_lock(&ftrace_cmd_mutex);
3401 list_for_each_entry_safe(p, n, &ftrace_commands, list) {
3402 if (strcmp(cmd->name, p->name) == 0) {
3403 ret = 0;
3404 list_del_init(&p->list);
3405 goto out_unlock;
3408 out_unlock:
3409 mutex_unlock(&ftrace_cmd_mutex);
3411 return ret;
3414 static int ftrace_process_regex(struct ftrace_hash *hash,
3415 char *buff, int len, int enable)
3417 char *func, *command, *next = buff;
3418 struct ftrace_func_command *p;
3419 int ret = -EINVAL;
3421 func = strsep(&next, ":");
3423 if (!next) {
3424 ret = ftrace_match_records(hash, func, len);
3425 if (!ret)
3426 ret = -EINVAL;
3427 if (ret < 0)
3428 return ret;
3429 return 0;
3432 /* command found */
3434 command = strsep(&next, ":");
3436 mutex_lock(&ftrace_cmd_mutex);
3437 list_for_each_entry(p, &ftrace_commands, list) {
3438 if (strcmp(p->name, command) == 0) {
3439 ret = p->func(hash, func, command, next, enable);
3440 goto out_unlock;
3443 out_unlock:
3444 mutex_unlock(&ftrace_cmd_mutex);
3446 return ret;
3449 static ssize_t
3450 ftrace_regex_write(struct file *file, const char __user *ubuf,
3451 size_t cnt, loff_t *ppos, int enable)
3453 struct ftrace_iterator *iter;
3454 struct trace_parser *parser;
3455 ssize_t ret, read;
3457 if (!cnt)
3458 return 0;
3460 if (file->f_mode & FMODE_READ) {
3461 struct seq_file *m = file->private_data;
3462 iter = m->private;
3463 } else
3464 iter = file->private_data;
3466 if (unlikely(ftrace_disabled))
3467 return -ENODEV;
3469 /* iter->hash is a local copy, so we don't need regex_lock */
3471 parser = &iter->parser;
3472 read = trace_get_user(parser, ubuf, cnt, ppos);
3474 if (read >= 0 && trace_parser_loaded(parser) &&
3475 !trace_parser_cont(parser)) {
3476 ret = ftrace_process_regex(iter->hash, parser->buffer,
3477 parser->idx, enable);
3478 trace_parser_clear(parser);
3479 if (ret < 0)
3480 goto out;
3483 ret = read;
3484 out:
3485 return ret;
3488 ssize_t
3489 ftrace_filter_write(struct file *file, const char __user *ubuf,
3490 size_t cnt, loff_t *ppos)
3492 return ftrace_regex_write(file, ubuf, cnt, ppos, 1);
3495 ssize_t
3496 ftrace_notrace_write(struct file *file, const char __user *ubuf,
3497 size_t cnt, loff_t *ppos)
3499 return ftrace_regex_write(file, ubuf, cnt, ppos, 0);
3502 static int
3503 ftrace_match_addr(struct ftrace_hash *hash, unsigned long ip, int remove)
3505 struct ftrace_func_entry *entry;
3507 if (!ftrace_location(ip))
3508 return -EINVAL;
3510 if (remove) {
3511 entry = ftrace_lookup_ip(hash, ip);
3512 if (!entry)
3513 return -ENOENT;
3514 free_hash_entry(hash, entry);
3515 return 0;
3518 return add_hash_entry(hash, ip);
3521 static void ftrace_ops_update_code(struct ftrace_ops *ops)
3523 if (ops->flags & FTRACE_OPS_FL_ENABLED && ftrace_enabled)
3524 ftrace_run_update_code(FTRACE_UPDATE_CALLS);
3527 static int
3528 ftrace_set_hash(struct ftrace_ops *ops, unsigned char *buf, int len,
3529 unsigned long ip, int remove, int reset, int enable)
3531 struct ftrace_hash **orig_hash;
3532 struct ftrace_hash *hash;
3533 int ret;
3535 /* All global ops uses the global ops filters */
3536 if (ops->flags & FTRACE_OPS_FL_GLOBAL)
3537 ops = &global_ops;
3539 if (unlikely(ftrace_disabled))
3540 return -ENODEV;
3542 mutex_lock(&ops->regex_lock);
3544 if (enable)
3545 orig_hash = &ops->filter_hash;
3546 else
3547 orig_hash = &ops->notrace_hash;
3549 hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, *orig_hash);
3550 if (!hash) {
3551 ret = -ENOMEM;
3552 goto out_regex_unlock;
3555 if (reset)
3556 ftrace_filter_reset(hash);
3557 if (buf && !ftrace_match_records(hash, buf, len)) {
3558 ret = -EINVAL;
3559 goto out_regex_unlock;
3561 if (ip) {
3562 ret = ftrace_match_addr(hash, ip, remove);
3563 if (ret < 0)
3564 goto out_regex_unlock;
3567 mutex_lock(&ftrace_lock);
3568 ret = ftrace_hash_move(ops, enable, orig_hash, hash);
3569 if (!ret)
3570 ftrace_ops_update_code(ops);
3572 mutex_unlock(&ftrace_lock);
3574 out_regex_unlock:
3575 mutex_unlock(&ops->regex_lock);
3577 free_ftrace_hash(hash);
3578 return ret;
3581 static int
3582 ftrace_set_addr(struct ftrace_ops *ops, unsigned long ip, int remove,
3583 int reset, int enable)
3585 return ftrace_set_hash(ops, 0, 0, ip, remove, reset, enable);
3589 * ftrace_set_filter_ip - set a function to filter on in ftrace by address
3590 * @ops - the ops to set the filter with
3591 * @ip - the address to add to or remove from the filter.
3592 * @remove - non zero to remove the ip from the filter
3593 * @reset - non zero to reset all filters before applying this filter.
3595 * Filters denote which functions should be enabled when tracing is enabled
3596 * If @ip is NULL, it failes to update filter.
3598 int ftrace_set_filter_ip(struct ftrace_ops *ops, unsigned long ip,
3599 int remove, int reset)
3601 ftrace_ops_init(ops);
3602 return ftrace_set_addr(ops, ip, remove, reset, 1);
3604 EXPORT_SYMBOL_GPL(ftrace_set_filter_ip);
3606 static int
3607 ftrace_set_regex(struct ftrace_ops *ops, unsigned char *buf, int len,
3608 int reset, int enable)
3610 return ftrace_set_hash(ops, buf, len, 0, 0, reset, enable);
3614 * ftrace_set_filter - set a function to filter on in ftrace
3615 * @ops - the ops to set the filter with
3616 * @buf - the string that holds the function filter text.
3617 * @len - the length of the string.
3618 * @reset - non zero to reset all filters before applying this filter.
3620 * Filters denote which functions should be enabled when tracing is enabled.
3621 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
3623 int ftrace_set_filter(struct ftrace_ops *ops, unsigned char *buf,
3624 int len, int reset)
3626 ftrace_ops_init(ops);
3627 return ftrace_set_regex(ops, buf, len, reset, 1);
3629 EXPORT_SYMBOL_GPL(ftrace_set_filter);
3632 * ftrace_set_notrace - set a function to not trace in ftrace
3633 * @ops - the ops to set the notrace filter with
3634 * @buf - the string that holds the function notrace text.
3635 * @len - the length of the string.
3636 * @reset - non zero to reset all filters before applying this filter.
3638 * Notrace Filters denote which functions should not be enabled when tracing
3639 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
3640 * for tracing.
3642 int ftrace_set_notrace(struct ftrace_ops *ops, unsigned char *buf,
3643 int len, int reset)
3645 ftrace_ops_init(ops);
3646 return ftrace_set_regex(ops, buf, len, reset, 0);
3648 EXPORT_SYMBOL_GPL(ftrace_set_notrace);
3650 * ftrace_set_filter - set a function to filter on in ftrace
3651 * @ops - the ops to set the filter with
3652 * @buf - the string that holds the function filter text.
3653 * @len - the length of the string.
3654 * @reset - non zero to reset all filters before applying this filter.
3656 * Filters denote which functions should be enabled when tracing is enabled.
3657 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
3659 void ftrace_set_global_filter(unsigned char *buf, int len, int reset)
3661 ftrace_set_regex(&global_ops, buf, len, reset, 1);
3663 EXPORT_SYMBOL_GPL(ftrace_set_global_filter);
3666 * ftrace_set_notrace - set a function to not trace in ftrace
3667 * @ops - the ops to set the notrace filter with
3668 * @buf - the string that holds the function notrace text.
3669 * @len - the length of the string.
3670 * @reset - non zero to reset all filters before applying this filter.
3672 * Notrace Filters denote which functions should not be enabled when tracing
3673 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
3674 * for tracing.
3676 void ftrace_set_global_notrace(unsigned char *buf, int len, int reset)
3678 ftrace_set_regex(&global_ops, buf, len, reset, 0);
3680 EXPORT_SYMBOL_GPL(ftrace_set_global_notrace);
3683 * command line interface to allow users to set filters on boot up.
3685 #define FTRACE_FILTER_SIZE COMMAND_LINE_SIZE
3686 static char ftrace_notrace_buf[FTRACE_FILTER_SIZE] __initdata;
3687 static char ftrace_filter_buf[FTRACE_FILTER_SIZE] __initdata;
3689 /* Used by function selftest to not test if filter is set */
3690 bool ftrace_filter_param __initdata;
3692 static int __init set_ftrace_notrace(char *str)
3694 ftrace_filter_param = true;
3695 strlcpy(ftrace_notrace_buf, str, FTRACE_FILTER_SIZE);
3696 return 1;
3698 __setup("ftrace_notrace=", set_ftrace_notrace);
3700 static int __init set_ftrace_filter(char *str)
3702 ftrace_filter_param = true;
3703 strlcpy(ftrace_filter_buf, str, FTRACE_FILTER_SIZE);
3704 return 1;
3706 __setup("ftrace_filter=", set_ftrace_filter);
3708 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
3709 static char ftrace_graph_buf[FTRACE_FILTER_SIZE] __initdata;
3710 static int ftrace_set_func(unsigned long *array, int *idx, int size, char *buffer);
3712 static int __init set_graph_function(char *str)
3714 strlcpy(ftrace_graph_buf, str, FTRACE_FILTER_SIZE);
3715 return 1;
3717 __setup("ftrace_graph_filter=", set_graph_function);
3719 static void __init set_ftrace_early_graph(char *buf)
3721 int ret;
3722 char *func;
3724 while (buf) {
3725 func = strsep(&buf, ",");
3726 /* we allow only one expression at a time */
3727 ret = ftrace_set_func(ftrace_graph_funcs, &ftrace_graph_count,
3728 FTRACE_GRAPH_MAX_FUNCS, func);
3729 if (ret)
3730 printk(KERN_DEBUG "ftrace: function %s not "
3731 "traceable\n", func);
3734 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
3736 void __init
3737 ftrace_set_early_filter(struct ftrace_ops *ops, char *buf, int enable)
3739 char *func;
3741 ftrace_ops_init(ops);
3743 while (buf) {
3744 func = strsep(&buf, ",");
3745 ftrace_set_regex(ops, func, strlen(func), 0, enable);
3749 static void __init set_ftrace_early_filters(void)
3751 if (ftrace_filter_buf[0])
3752 ftrace_set_early_filter(&global_ops, ftrace_filter_buf, 1);
3753 if (ftrace_notrace_buf[0])
3754 ftrace_set_early_filter(&global_ops, ftrace_notrace_buf, 0);
3755 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
3756 if (ftrace_graph_buf[0])
3757 set_ftrace_early_graph(ftrace_graph_buf);
3758 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
3761 int ftrace_regex_release(struct inode *inode, struct file *file)
3763 struct seq_file *m = (struct seq_file *)file->private_data;
3764 struct ftrace_iterator *iter;
3765 struct ftrace_hash **orig_hash;
3766 struct trace_parser *parser;
3767 int filter_hash;
3768 int ret;
3770 if (file->f_mode & FMODE_READ) {
3771 iter = m->private;
3772 seq_release(inode, file);
3773 } else
3774 iter = file->private_data;
3776 parser = &iter->parser;
3777 if (trace_parser_loaded(parser)) {
3778 parser->buffer[parser->idx] = 0;
3779 ftrace_match_records(iter->hash, parser->buffer, parser->idx);
3782 trace_parser_put(parser);
3784 mutex_lock(&iter->ops->regex_lock);
3786 if (file->f_mode & FMODE_WRITE) {
3787 filter_hash = !!(iter->flags & FTRACE_ITER_FILTER);
3789 if (filter_hash)
3790 orig_hash = &iter->ops->filter_hash;
3791 else
3792 orig_hash = &iter->ops->notrace_hash;
3794 mutex_lock(&ftrace_lock);
3795 ret = ftrace_hash_move(iter->ops, filter_hash,
3796 orig_hash, iter->hash);
3797 if (!ret)
3798 ftrace_ops_update_code(iter->ops);
3800 mutex_unlock(&ftrace_lock);
3803 mutex_unlock(&iter->ops->regex_lock);
3804 free_ftrace_hash(iter->hash);
3805 kfree(iter);
3807 return 0;
3810 static const struct file_operations ftrace_avail_fops = {
3811 .open = ftrace_avail_open,
3812 .read = seq_read,
3813 .llseek = seq_lseek,
3814 .release = seq_release_private,
3817 static const struct file_operations ftrace_enabled_fops = {
3818 .open = ftrace_enabled_open,
3819 .read = seq_read,
3820 .llseek = seq_lseek,
3821 .release = seq_release_private,
3824 static const struct file_operations ftrace_filter_fops = {
3825 .open = ftrace_filter_open,
3826 .read = seq_read,
3827 .write = ftrace_filter_write,
3828 .llseek = tracing_lseek,
3829 .release = ftrace_regex_release,
3832 static const struct file_operations ftrace_notrace_fops = {
3833 .open = ftrace_notrace_open,
3834 .read = seq_read,
3835 .write = ftrace_notrace_write,
3836 .llseek = tracing_lseek,
3837 .release = ftrace_regex_release,
3840 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
3842 static DEFINE_MUTEX(graph_lock);
3844 int ftrace_graph_count;
3845 int ftrace_graph_notrace_count;
3846 unsigned long ftrace_graph_funcs[FTRACE_GRAPH_MAX_FUNCS] __read_mostly;
3847 unsigned long ftrace_graph_notrace_funcs[FTRACE_GRAPH_MAX_FUNCS] __read_mostly;
3849 struct ftrace_graph_data {
3850 unsigned long *table;
3851 size_t size;
3852 int *count;
3853 const struct seq_operations *seq_ops;
3856 static void *
3857 __g_next(struct seq_file *m, loff_t *pos)
3859 struct ftrace_graph_data *fgd = m->private;
3861 if (*pos >= *fgd->count)
3862 return NULL;
3863 return &fgd->table[*pos];
3866 static void *
3867 g_next(struct seq_file *m, void *v, loff_t *pos)
3869 (*pos)++;
3870 return __g_next(m, pos);
3873 static void *g_start(struct seq_file *m, loff_t *pos)
3875 struct ftrace_graph_data *fgd = m->private;
3877 mutex_lock(&graph_lock);
3879 /* Nothing, tell g_show to print all functions are enabled */
3880 if (!*fgd->count && !*pos)
3881 return (void *)1;
3883 return __g_next(m, pos);
3886 static void g_stop(struct seq_file *m, void *p)
3888 mutex_unlock(&graph_lock);
3891 static int g_show(struct seq_file *m, void *v)
3893 unsigned long *ptr = v;
3895 if (!ptr)
3896 return 0;
3898 if (ptr == (unsigned long *)1) {
3899 seq_printf(m, "#### all functions enabled ####\n");
3900 return 0;
3903 seq_printf(m, "%ps\n", (void *)*ptr);
3905 return 0;
3908 static const struct seq_operations ftrace_graph_seq_ops = {
3909 .start = g_start,
3910 .next = g_next,
3911 .stop = g_stop,
3912 .show = g_show,
3915 static int
3916 __ftrace_graph_open(struct inode *inode, struct file *file,
3917 struct ftrace_graph_data *fgd)
3919 int ret = 0;
3921 mutex_lock(&graph_lock);
3922 if ((file->f_mode & FMODE_WRITE) &&
3923 (file->f_flags & O_TRUNC)) {
3924 *fgd->count = 0;
3925 memset(fgd->table, 0, fgd->size * sizeof(*fgd->table));
3927 mutex_unlock(&graph_lock);
3929 if (file->f_mode & FMODE_READ) {
3930 ret = seq_open(file, fgd->seq_ops);
3931 if (!ret) {
3932 struct seq_file *m = file->private_data;
3933 m->private = fgd;
3935 } else
3936 file->private_data = fgd;
3938 return ret;
3941 static int
3942 ftrace_graph_open(struct inode *inode, struct file *file)
3944 struct ftrace_graph_data *fgd;
3946 if (unlikely(ftrace_disabled))
3947 return -ENODEV;
3949 fgd = kmalloc(sizeof(*fgd), GFP_KERNEL);
3950 if (fgd == NULL)
3951 return -ENOMEM;
3953 fgd->table = ftrace_graph_funcs;
3954 fgd->size = FTRACE_GRAPH_MAX_FUNCS;
3955 fgd->count = &ftrace_graph_count;
3956 fgd->seq_ops = &ftrace_graph_seq_ops;
3958 return __ftrace_graph_open(inode, file, fgd);
3961 static int
3962 ftrace_graph_notrace_open(struct inode *inode, struct file *file)
3964 struct ftrace_graph_data *fgd;
3966 if (unlikely(ftrace_disabled))
3967 return -ENODEV;
3969 fgd = kmalloc(sizeof(*fgd), GFP_KERNEL);
3970 if (fgd == NULL)
3971 return -ENOMEM;
3973 fgd->table = ftrace_graph_notrace_funcs;
3974 fgd->size = FTRACE_GRAPH_MAX_FUNCS;
3975 fgd->count = &ftrace_graph_notrace_count;
3976 fgd->seq_ops = &ftrace_graph_seq_ops;
3978 return __ftrace_graph_open(inode, file, fgd);
3981 static int
3982 ftrace_graph_release(struct inode *inode, struct file *file)
3984 if (file->f_mode & FMODE_READ) {
3985 struct seq_file *m = file->private_data;
3987 kfree(m->private);
3988 seq_release(inode, file);
3989 } else {
3990 kfree(file->private_data);
3993 return 0;
3996 static int
3997 ftrace_set_func(unsigned long *array, int *idx, int size, char *buffer)
3999 struct dyn_ftrace *rec;
4000 struct ftrace_page *pg;
4001 int search_len;
4002 int fail = 1;
4003 int type, not;
4004 char *search;
4005 bool exists;
4006 int i;
4008 /* decode regex */
4009 type = filter_parse_regex(buffer, strlen(buffer), &search, &not);
4010 if (!not && *idx >= size)
4011 return -EBUSY;
4013 search_len = strlen(search);
4015 mutex_lock(&ftrace_lock);
4017 if (unlikely(ftrace_disabled)) {
4018 mutex_unlock(&ftrace_lock);
4019 return -ENODEV;
4022 do_for_each_ftrace_rec(pg, rec) {
4024 if (ftrace_match_record(rec, NULL, search, search_len, type)) {
4025 /* if it is in the array */
4026 exists = false;
4027 for (i = 0; i < *idx; i++) {
4028 if (array[i] == rec->ip) {
4029 exists = true;
4030 break;
4034 if (!not) {
4035 fail = 0;
4036 if (!exists) {
4037 array[(*idx)++] = rec->ip;
4038 if (*idx >= size)
4039 goto out;
4041 } else {
4042 if (exists) {
4043 array[i] = array[--(*idx)];
4044 array[*idx] = 0;
4045 fail = 0;
4049 } while_for_each_ftrace_rec();
4050 out:
4051 mutex_unlock(&ftrace_lock);
4053 if (fail)
4054 return -EINVAL;
4056 return 0;
4059 static ssize_t
4060 ftrace_graph_write(struct file *file, const char __user *ubuf,
4061 size_t cnt, loff_t *ppos)
4063 struct trace_parser parser;
4064 ssize_t read, ret = 0;
4065 struct ftrace_graph_data *fgd = file->private_data;
4067 if (!cnt)
4068 return 0;
4070 if (trace_parser_get_init(&parser, FTRACE_BUFF_MAX))
4071 return -ENOMEM;
4073 read = trace_get_user(&parser, ubuf, cnt, ppos);
4075 if (read >= 0 && trace_parser_loaded((&parser))) {
4076 parser.buffer[parser.idx] = 0;
4078 mutex_lock(&graph_lock);
4080 /* we allow only one expression at a time */
4081 ret = ftrace_set_func(fgd->table, fgd->count, fgd->size,
4082 parser.buffer);
4084 mutex_unlock(&graph_lock);
4087 if (!ret)
4088 ret = read;
4090 trace_parser_put(&parser);
4092 return ret;
4095 static const struct file_operations ftrace_graph_fops = {
4096 .open = ftrace_graph_open,
4097 .read = seq_read,
4098 .write = ftrace_graph_write,
4099 .llseek = tracing_lseek,
4100 .release = ftrace_graph_release,
4103 static const struct file_operations ftrace_graph_notrace_fops = {
4104 .open = ftrace_graph_notrace_open,
4105 .read = seq_read,
4106 .write = ftrace_graph_write,
4107 .llseek = tracing_lseek,
4108 .release = ftrace_graph_release,
4110 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
4112 static __init int ftrace_init_dyn_debugfs(struct dentry *d_tracer)
4115 trace_create_file("available_filter_functions", 0444,
4116 d_tracer, NULL, &ftrace_avail_fops);
4118 trace_create_file("enabled_functions", 0444,
4119 d_tracer, NULL, &ftrace_enabled_fops);
4121 trace_create_file("set_ftrace_filter", 0644, d_tracer,
4122 NULL, &ftrace_filter_fops);
4124 trace_create_file("set_ftrace_notrace", 0644, d_tracer,
4125 NULL, &ftrace_notrace_fops);
4127 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
4128 trace_create_file("set_graph_function", 0444, d_tracer,
4129 NULL,
4130 &ftrace_graph_fops);
4131 trace_create_file("set_graph_notrace", 0444, d_tracer,
4132 NULL,
4133 &ftrace_graph_notrace_fops);
4134 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
4136 return 0;
4139 static int ftrace_cmp_ips(const void *a, const void *b)
4141 const unsigned long *ipa = a;
4142 const unsigned long *ipb = b;
4144 if (*ipa > *ipb)
4145 return 1;
4146 if (*ipa < *ipb)
4147 return -1;
4148 return 0;
4151 static void ftrace_swap_ips(void *a, void *b, int size)
4153 unsigned long *ipa = a;
4154 unsigned long *ipb = b;
4155 unsigned long t;
4157 t = *ipa;
4158 *ipa = *ipb;
4159 *ipb = t;
4162 static int ftrace_process_locs(struct module *mod,
4163 unsigned long *start,
4164 unsigned long *end)
4166 struct ftrace_page *start_pg;
4167 struct ftrace_page *pg;
4168 struct dyn_ftrace *rec;
4169 unsigned long count;
4170 unsigned long *p;
4171 unsigned long addr;
4172 unsigned long flags = 0; /* Shut up gcc */
4173 int ret = -ENOMEM;
4175 count = end - start;
4177 if (!count)
4178 return 0;
4180 sort(start, count, sizeof(*start),
4181 ftrace_cmp_ips, ftrace_swap_ips);
4183 start_pg = ftrace_allocate_pages(count);
4184 if (!start_pg)
4185 return -ENOMEM;
4187 mutex_lock(&ftrace_lock);
4190 * Core and each module needs their own pages, as
4191 * modules will free them when they are removed.
4192 * Force a new page to be allocated for modules.
4194 if (!mod) {
4195 WARN_ON(ftrace_pages || ftrace_pages_start);
4196 /* First initialization */
4197 ftrace_pages = ftrace_pages_start = start_pg;
4198 } else {
4199 if (!ftrace_pages)
4200 goto out;
4202 if (WARN_ON(ftrace_pages->next)) {
4203 /* Hmm, we have free pages? */
4204 while (ftrace_pages->next)
4205 ftrace_pages = ftrace_pages->next;
4208 ftrace_pages->next = start_pg;
4211 p = start;
4212 pg = start_pg;
4213 while (p < end) {
4214 addr = ftrace_call_adjust(*p++);
4216 * Some architecture linkers will pad between
4217 * the different mcount_loc sections of different
4218 * object files to satisfy alignments.
4219 * Skip any NULL pointers.
4221 if (!addr)
4222 continue;
4224 if (pg->index == pg->size) {
4225 /* We should have allocated enough */
4226 if (WARN_ON(!pg->next))
4227 break;
4228 pg = pg->next;
4231 rec = &pg->records[pg->index++];
4232 rec->ip = addr;
4235 /* We should have used all pages */
4236 WARN_ON(pg->next);
4238 /* Assign the last page to ftrace_pages */
4239 ftrace_pages = pg;
4241 /* These new locations need to be initialized */
4242 ftrace_new_pgs = start_pg;
4245 * We only need to disable interrupts on start up
4246 * because we are modifying code that an interrupt
4247 * may execute, and the modification is not atomic.
4248 * But for modules, nothing runs the code we modify
4249 * until we are finished with it, and there's no
4250 * reason to cause large interrupt latencies while we do it.
4252 if (!mod)
4253 local_irq_save(flags);
4254 ftrace_update_code(mod);
4255 if (!mod)
4256 local_irq_restore(flags);
4257 ret = 0;
4258 out:
4259 mutex_unlock(&ftrace_lock);
4261 return ret;
4264 #ifdef CONFIG_MODULES
4266 #define next_to_ftrace_page(p) container_of(p, struct ftrace_page, next)
4268 void ftrace_release_mod(struct module *mod)
4270 struct dyn_ftrace *rec;
4271 struct ftrace_page **last_pg;
4272 struct ftrace_page *pg;
4273 int order;
4275 mutex_lock(&ftrace_lock);
4277 if (ftrace_disabled)
4278 goto out_unlock;
4281 * Each module has its own ftrace_pages, remove
4282 * them from the list.
4284 last_pg = &ftrace_pages_start;
4285 for (pg = ftrace_pages_start; pg; pg = *last_pg) {
4286 rec = &pg->records[0];
4287 if (within_module_core(rec->ip, mod)) {
4289 * As core pages are first, the first
4290 * page should never be a module page.
4292 if (WARN_ON(pg == ftrace_pages_start))
4293 goto out_unlock;
4295 /* Check if we are deleting the last page */
4296 if (pg == ftrace_pages)
4297 ftrace_pages = next_to_ftrace_page(last_pg);
4299 *last_pg = pg->next;
4300 order = get_count_order(pg->size / ENTRIES_PER_PAGE);
4301 free_pages((unsigned long)pg->records, order);
4302 kfree(pg);
4303 } else
4304 last_pg = &pg->next;
4306 out_unlock:
4307 mutex_unlock(&ftrace_lock);
4310 static void ftrace_init_module(struct module *mod,
4311 unsigned long *start, unsigned long *end)
4313 if (ftrace_disabled || start == end)
4314 return;
4315 ftrace_process_locs(mod, start, end);
4318 static int ftrace_module_notify_enter(struct notifier_block *self,
4319 unsigned long val, void *data)
4321 struct module *mod = data;
4323 if (val == MODULE_STATE_COMING)
4324 ftrace_init_module(mod, mod->ftrace_callsites,
4325 mod->ftrace_callsites +
4326 mod->num_ftrace_callsites);
4327 return 0;
4330 static int ftrace_module_notify_exit(struct notifier_block *self,
4331 unsigned long val, void *data)
4333 struct module *mod = data;
4335 if (val == MODULE_STATE_GOING)
4336 ftrace_release_mod(mod);
4338 return 0;
4340 #else
4341 static int ftrace_module_notify_enter(struct notifier_block *self,
4342 unsigned long val, void *data)
4344 return 0;
4346 static int ftrace_module_notify_exit(struct notifier_block *self,
4347 unsigned long val, void *data)
4349 return 0;
4351 #endif /* CONFIG_MODULES */
4353 struct notifier_block ftrace_module_enter_nb = {
4354 .notifier_call = ftrace_module_notify_enter,
4355 .priority = INT_MAX, /* Run before anything that can use kprobes */
4358 struct notifier_block ftrace_module_exit_nb = {
4359 .notifier_call = ftrace_module_notify_exit,
4360 .priority = INT_MIN, /* Run after anything that can remove kprobes */
4363 extern unsigned long __start_mcount_loc[];
4364 extern unsigned long __stop_mcount_loc[];
4366 void __init ftrace_init(void)
4368 unsigned long count, addr, flags;
4369 int ret;
4371 /* Keep the ftrace pointer to the stub */
4372 addr = (unsigned long)ftrace_stub;
4374 local_irq_save(flags);
4375 ftrace_dyn_arch_init(&addr);
4376 local_irq_restore(flags);
4378 /* ftrace_dyn_arch_init places the return code in addr */
4379 if (addr)
4380 goto failed;
4382 count = __stop_mcount_loc - __start_mcount_loc;
4384 ret = ftrace_dyn_table_alloc(count);
4385 if (ret)
4386 goto failed;
4388 last_ftrace_enabled = ftrace_enabled = 1;
4390 ret = ftrace_process_locs(NULL,
4391 __start_mcount_loc,
4392 __stop_mcount_loc);
4394 ret = register_module_notifier(&ftrace_module_enter_nb);
4395 if (ret)
4396 pr_warning("Failed to register trace ftrace module enter notifier\n");
4398 ret = register_module_notifier(&ftrace_module_exit_nb);
4399 if (ret)
4400 pr_warning("Failed to register trace ftrace module exit notifier\n");
4402 set_ftrace_early_filters();
4404 return;
4405 failed:
4406 ftrace_disabled = 1;
4409 #else
4411 static struct ftrace_ops global_ops = {
4412 .func = ftrace_stub,
4413 .flags = FTRACE_OPS_FL_RECURSION_SAFE | FTRACE_OPS_FL_INITIALIZED,
4414 INIT_REGEX_LOCK(global_ops)
4417 static int __init ftrace_nodyn_init(void)
4419 ftrace_enabled = 1;
4420 return 0;
4422 core_initcall(ftrace_nodyn_init);
4424 static inline int ftrace_init_dyn_debugfs(struct dentry *d_tracer) { return 0; }
4425 static inline void ftrace_startup_enable(int command) { }
4426 /* Keep as macros so we do not need to define the commands */
4427 # define ftrace_startup(ops, command) \
4428 ({ \
4429 int ___ret = __register_ftrace_function(ops); \
4430 if (!___ret) \
4431 (ops)->flags |= FTRACE_OPS_FL_ENABLED; \
4432 ___ret; \
4434 # define ftrace_shutdown(ops, command) __unregister_ftrace_function(ops)
4436 # define ftrace_startup_sysctl() do { } while (0)
4437 # define ftrace_shutdown_sysctl() do { } while (0)
4439 static inline int
4440 ftrace_ops_test(struct ftrace_ops *ops, unsigned long ip, void *regs)
4442 return 1;
4445 #endif /* CONFIG_DYNAMIC_FTRACE */
4447 static void
4448 ftrace_ops_control_func(unsigned long ip, unsigned long parent_ip,
4449 struct ftrace_ops *op, struct pt_regs *regs)
4451 if (unlikely(trace_recursion_test(TRACE_CONTROL_BIT)))
4452 return;
4455 * Some of the ops may be dynamically allocated,
4456 * they must be freed after a synchronize_sched().
4458 preempt_disable_notrace();
4459 trace_recursion_set(TRACE_CONTROL_BIT);
4462 * Control funcs (perf) uses RCU. Only trace if
4463 * RCU is currently active.
4465 if (!rcu_is_watching())
4466 goto out;
4468 do_for_each_ftrace_op(op, ftrace_control_list) {
4469 if (!(op->flags & FTRACE_OPS_FL_STUB) &&
4470 !ftrace_function_local_disabled(op) &&
4471 ftrace_ops_test(op, ip, regs))
4472 op->func(ip, parent_ip, op, regs);
4473 } while_for_each_ftrace_op(op);
4474 out:
4475 trace_recursion_clear(TRACE_CONTROL_BIT);
4476 preempt_enable_notrace();
4479 static struct ftrace_ops control_ops = {
4480 .func = ftrace_ops_control_func,
4481 .flags = FTRACE_OPS_FL_RECURSION_SAFE | FTRACE_OPS_FL_INITIALIZED,
4482 INIT_REGEX_LOCK(control_ops)
4485 static inline void
4486 __ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip,
4487 struct ftrace_ops *ignored, struct pt_regs *regs)
4489 struct ftrace_ops *op;
4490 int bit;
4492 if (function_trace_stop)
4493 return;
4495 bit = trace_test_and_set_recursion(TRACE_LIST_START, TRACE_LIST_MAX);
4496 if (bit < 0)
4497 return;
4500 * Some of the ops may be dynamically allocated,
4501 * they must be freed after a synchronize_sched().
4503 preempt_disable_notrace();
4504 do_for_each_ftrace_op(op, ftrace_ops_list) {
4505 if (ftrace_ops_test(op, ip, regs))
4506 op->func(ip, parent_ip, op, regs);
4507 } while_for_each_ftrace_op(op);
4508 preempt_enable_notrace();
4509 trace_clear_recursion(bit);
4513 * Some archs only support passing ip and parent_ip. Even though
4514 * the list function ignores the op parameter, we do not want any
4515 * C side effects, where a function is called without the caller
4516 * sending a third parameter.
4517 * Archs are to support both the regs and ftrace_ops at the same time.
4518 * If they support ftrace_ops, it is assumed they support regs.
4519 * If call backs want to use regs, they must either check for regs
4520 * being NULL, or CONFIG_DYNAMIC_FTRACE_WITH_REGS.
4521 * Note, CONFIG_DYNAMIC_FTRACE_WITH_REGS expects a full regs to be saved.
4522 * An architecture can pass partial regs with ftrace_ops and still
4523 * set the ARCH_SUPPORT_FTARCE_OPS.
4525 #if ARCH_SUPPORTS_FTRACE_OPS
4526 static void ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip,
4527 struct ftrace_ops *op, struct pt_regs *regs)
4529 __ftrace_ops_list_func(ip, parent_ip, NULL, regs);
4531 #else
4532 static void ftrace_ops_no_ops(unsigned long ip, unsigned long parent_ip)
4534 __ftrace_ops_list_func(ip, parent_ip, NULL, NULL);
4536 #endif
4538 static void clear_ftrace_swapper(void)
4540 struct task_struct *p;
4541 int cpu;
4543 get_online_cpus();
4544 for_each_online_cpu(cpu) {
4545 p = idle_task(cpu);
4546 clear_tsk_trace_trace(p);
4548 put_online_cpus();
4551 static void set_ftrace_swapper(void)
4553 struct task_struct *p;
4554 int cpu;
4556 get_online_cpus();
4557 for_each_online_cpu(cpu) {
4558 p = idle_task(cpu);
4559 set_tsk_trace_trace(p);
4561 put_online_cpus();
4564 static void clear_ftrace_pid(struct pid *pid)
4566 struct task_struct *p;
4568 rcu_read_lock();
4569 do_each_pid_task(pid, PIDTYPE_PID, p) {
4570 clear_tsk_trace_trace(p);
4571 } while_each_pid_task(pid, PIDTYPE_PID, p);
4572 rcu_read_unlock();
4574 put_pid(pid);
4577 static void set_ftrace_pid(struct pid *pid)
4579 struct task_struct *p;
4581 rcu_read_lock();
4582 do_each_pid_task(pid, PIDTYPE_PID, p) {
4583 set_tsk_trace_trace(p);
4584 } while_each_pid_task(pid, PIDTYPE_PID, p);
4585 rcu_read_unlock();
4588 static void clear_ftrace_pid_task(struct pid *pid)
4590 if (pid == ftrace_swapper_pid)
4591 clear_ftrace_swapper();
4592 else
4593 clear_ftrace_pid(pid);
4596 static void set_ftrace_pid_task(struct pid *pid)
4598 if (pid == ftrace_swapper_pid)
4599 set_ftrace_swapper();
4600 else
4601 set_ftrace_pid(pid);
4604 static int ftrace_pid_add(int p)
4606 struct pid *pid;
4607 struct ftrace_pid *fpid;
4608 int ret = -EINVAL;
4610 mutex_lock(&ftrace_lock);
4612 if (!p)
4613 pid = ftrace_swapper_pid;
4614 else
4615 pid = find_get_pid(p);
4617 if (!pid)
4618 goto out;
4620 ret = 0;
4622 list_for_each_entry(fpid, &ftrace_pids, list)
4623 if (fpid->pid == pid)
4624 goto out_put;
4626 ret = -ENOMEM;
4628 fpid = kmalloc(sizeof(*fpid), GFP_KERNEL);
4629 if (!fpid)
4630 goto out_put;
4632 list_add(&fpid->list, &ftrace_pids);
4633 fpid->pid = pid;
4635 set_ftrace_pid_task(pid);
4637 ftrace_update_pid_func();
4638 ftrace_startup_enable(0);
4640 mutex_unlock(&ftrace_lock);
4641 return 0;
4643 out_put:
4644 if (pid != ftrace_swapper_pid)
4645 put_pid(pid);
4647 out:
4648 mutex_unlock(&ftrace_lock);
4649 return ret;
4652 static void ftrace_pid_reset(void)
4654 struct ftrace_pid *fpid, *safe;
4656 mutex_lock(&ftrace_lock);
4657 list_for_each_entry_safe(fpid, safe, &ftrace_pids, list) {
4658 struct pid *pid = fpid->pid;
4660 clear_ftrace_pid_task(pid);
4662 list_del(&fpid->list);
4663 kfree(fpid);
4666 ftrace_update_pid_func();
4667 ftrace_startup_enable(0);
4669 mutex_unlock(&ftrace_lock);
4672 static void *fpid_start(struct seq_file *m, loff_t *pos)
4674 mutex_lock(&ftrace_lock);
4676 if (list_empty(&ftrace_pids) && (!*pos))
4677 return (void *) 1;
4679 return seq_list_start(&ftrace_pids, *pos);
4682 static void *fpid_next(struct seq_file *m, void *v, loff_t *pos)
4684 if (v == (void *)1)
4685 return NULL;
4687 return seq_list_next(v, &ftrace_pids, pos);
4690 static void fpid_stop(struct seq_file *m, void *p)
4692 mutex_unlock(&ftrace_lock);
4695 static int fpid_show(struct seq_file *m, void *v)
4697 const struct ftrace_pid *fpid = list_entry(v, struct ftrace_pid, list);
4699 if (v == (void *)1) {
4700 seq_printf(m, "no pid\n");
4701 return 0;
4704 if (fpid->pid == ftrace_swapper_pid)
4705 seq_printf(m, "swapper tasks\n");
4706 else
4707 seq_printf(m, "%u\n", pid_vnr(fpid->pid));
4709 return 0;
4712 static const struct seq_operations ftrace_pid_sops = {
4713 .start = fpid_start,
4714 .next = fpid_next,
4715 .stop = fpid_stop,
4716 .show = fpid_show,
4719 static int
4720 ftrace_pid_open(struct inode *inode, struct file *file)
4722 int ret = 0;
4724 if ((file->f_mode & FMODE_WRITE) &&
4725 (file->f_flags & O_TRUNC))
4726 ftrace_pid_reset();
4728 if (file->f_mode & FMODE_READ)
4729 ret = seq_open(file, &ftrace_pid_sops);
4731 return ret;
4734 static ssize_t
4735 ftrace_pid_write(struct file *filp, const char __user *ubuf,
4736 size_t cnt, loff_t *ppos)
4738 char buf[64], *tmp;
4739 long val;
4740 int ret;
4742 if (cnt >= sizeof(buf))
4743 return -EINVAL;
4745 if (copy_from_user(&buf, ubuf, cnt))
4746 return -EFAULT;
4748 buf[cnt] = 0;
4751 * Allow "echo > set_ftrace_pid" or "echo -n '' > set_ftrace_pid"
4752 * to clean the filter quietly.
4754 tmp = strstrip(buf);
4755 if (strlen(tmp) == 0)
4756 return 1;
4758 ret = kstrtol(tmp, 10, &val);
4759 if (ret < 0)
4760 return ret;
4762 ret = ftrace_pid_add(val);
4764 return ret ? ret : cnt;
4767 static int
4768 ftrace_pid_release(struct inode *inode, struct file *file)
4770 if (file->f_mode & FMODE_READ)
4771 seq_release(inode, file);
4773 return 0;
4776 static const struct file_operations ftrace_pid_fops = {
4777 .open = ftrace_pid_open,
4778 .write = ftrace_pid_write,
4779 .read = seq_read,
4780 .llseek = tracing_lseek,
4781 .release = ftrace_pid_release,
4784 static __init int ftrace_init_debugfs(void)
4786 struct dentry *d_tracer;
4788 d_tracer = tracing_init_dentry();
4789 if (!d_tracer)
4790 return 0;
4792 ftrace_init_dyn_debugfs(d_tracer);
4794 trace_create_file("set_ftrace_pid", 0644, d_tracer,
4795 NULL, &ftrace_pid_fops);
4797 ftrace_profile_debugfs(d_tracer);
4799 return 0;
4801 fs_initcall(ftrace_init_debugfs);
4804 * ftrace_kill - kill ftrace
4806 * This function should be used by panic code. It stops ftrace
4807 * but in a not so nice way. If you need to simply kill ftrace
4808 * from a non-atomic section, use ftrace_kill.
4810 void ftrace_kill(void)
4812 ftrace_disabled = 1;
4813 ftrace_enabled = 0;
4814 clear_ftrace_function();
4818 * Test if ftrace is dead or not.
4820 int ftrace_is_dead(void)
4822 return ftrace_disabled;
4826 * register_ftrace_function - register a function for profiling
4827 * @ops - ops structure that holds the function for profiling.
4829 * Register a function to be called by all functions in the
4830 * kernel.
4832 * Note: @ops->func and all the functions it calls must be labeled
4833 * with "notrace", otherwise it will go into a
4834 * recursive loop.
4836 int register_ftrace_function(struct ftrace_ops *ops)
4838 int ret = -1;
4840 ftrace_ops_init(ops);
4842 mutex_lock(&ftrace_lock);
4844 ret = ftrace_startup(ops, 0);
4846 mutex_unlock(&ftrace_lock);
4848 return ret;
4850 EXPORT_SYMBOL_GPL(register_ftrace_function);
4853 * unregister_ftrace_function - unregister a function for profiling.
4854 * @ops - ops structure that holds the function to unregister
4856 * Unregister a function that was added to be called by ftrace profiling.
4858 int unregister_ftrace_function(struct ftrace_ops *ops)
4860 int ret;
4862 mutex_lock(&ftrace_lock);
4863 ret = ftrace_shutdown(ops, 0);
4864 mutex_unlock(&ftrace_lock);
4866 return ret;
4868 EXPORT_SYMBOL_GPL(unregister_ftrace_function);
4871 ftrace_enable_sysctl(struct ctl_table *table, int write,
4872 void __user *buffer, size_t *lenp,
4873 loff_t *ppos)
4875 int ret = -ENODEV;
4877 mutex_lock(&ftrace_lock);
4879 if (unlikely(ftrace_disabled))
4880 goto out;
4882 ret = proc_dointvec(table, write, buffer, lenp, ppos);
4884 if (ret || !write || (last_ftrace_enabled == !!ftrace_enabled))
4885 goto out;
4887 last_ftrace_enabled = !!ftrace_enabled;
4889 if (ftrace_enabled) {
4891 ftrace_startup_sysctl();
4893 /* we are starting ftrace again */
4894 if (ftrace_ops_list != &ftrace_list_end)
4895 update_ftrace_function();
4897 } else {
4898 /* stopping ftrace calls (just send to ftrace_stub) */
4899 ftrace_trace_function = ftrace_stub;
4901 ftrace_shutdown_sysctl();
4904 out:
4905 mutex_unlock(&ftrace_lock);
4906 return ret;
4909 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
4911 static int ftrace_graph_active;
4912 static struct notifier_block ftrace_suspend_notifier;
4914 int ftrace_graph_entry_stub(struct ftrace_graph_ent *trace)
4916 return 0;
4919 /* The callbacks that hook a function */
4920 trace_func_graph_ret_t ftrace_graph_return =
4921 (trace_func_graph_ret_t)ftrace_stub;
4922 trace_func_graph_ent_t ftrace_graph_entry = ftrace_graph_entry_stub;
4923 static trace_func_graph_ent_t __ftrace_graph_entry = ftrace_graph_entry_stub;
4925 /* Try to assign a return stack array on FTRACE_RETSTACK_ALLOC_SIZE tasks. */
4926 static int alloc_retstack_tasklist(struct ftrace_ret_stack **ret_stack_list)
4928 int i;
4929 int ret = 0;
4930 unsigned long flags;
4931 int start = 0, end = FTRACE_RETSTACK_ALLOC_SIZE;
4932 struct task_struct *g, *t;
4934 for (i = 0; i < FTRACE_RETSTACK_ALLOC_SIZE; i++) {
4935 ret_stack_list[i] = kmalloc(FTRACE_RETFUNC_DEPTH
4936 * sizeof(struct ftrace_ret_stack),
4937 GFP_KERNEL);
4938 if (!ret_stack_list[i]) {
4939 start = 0;
4940 end = i;
4941 ret = -ENOMEM;
4942 goto free;
4946 read_lock_irqsave(&tasklist_lock, flags);
4947 do_each_thread(g, t) {
4948 if (start == end) {
4949 ret = -EAGAIN;
4950 goto unlock;
4953 if (t->ret_stack == NULL) {
4954 atomic_set(&t->tracing_graph_pause, 0);
4955 atomic_set(&t->trace_overrun, 0);
4956 t->curr_ret_stack = -1;
4957 /* Make sure the tasks see the -1 first: */
4958 smp_wmb();
4959 t->ret_stack = ret_stack_list[start++];
4961 } while_each_thread(g, t);
4963 unlock:
4964 read_unlock_irqrestore(&tasklist_lock, flags);
4965 free:
4966 for (i = start; i < end; i++)
4967 kfree(ret_stack_list[i]);
4968 return ret;
4971 static void
4972 ftrace_graph_probe_sched_switch(void *ignore,
4973 struct task_struct *prev, struct task_struct *next)
4975 unsigned long long timestamp;
4976 int index;
4979 * Does the user want to count the time a function was asleep.
4980 * If so, do not update the time stamps.
4982 if (trace_flags & TRACE_ITER_SLEEP_TIME)
4983 return;
4985 timestamp = trace_clock_local();
4987 prev->ftrace_timestamp = timestamp;
4989 /* only process tasks that we timestamped */
4990 if (!next->ftrace_timestamp)
4991 return;
4994 * Update all the counters in next to make up for the
4995 * time next was sleeping.
4997 timestamp -= next->ftrace_timestamp;
4999 for (index = next->curr_ret_stack; index >= 0; index--)
5000 next->ret_stack[index].calltime += timestamp;
5003 /* Allocate a return stack for each task */
5004 static int start_graph_tracing(void)
5006 struct ftrace_ret_stack **ret_stack_list;
5007 int ret, cpu;
5009 ret_stack_list = kmalloc(FTRACE_RETSTACK_ALLOC_SIZE *
5010 sizeof(struct ftrace_ret_stack *),
5011 GFP_KERNEL);
5013 if (!ret_stack_list)
5014 return -ENOMEM;
5016 /* The cpu_boot init_task->ret_stack will never be freed */
5017 for_each_online_cpu(cpu) {
5018 if (!idle_task(cpu)->ret_stack)
5019 ftrace_graph_init_idle_task(idle_task(cpu), cpu);
5022 do {
5023 ret = alloc_retstack_tasklist(ret_stack_list);
5024 } while (ret == -EAGAIN);
5026 if (!ret) {
5027 ret = register_trace_sched_switch(ftrace_graph_probe_sched_switch, NULL);
5028 if (ret)
5029 pr_info("ftrace_graph: Couldn't activate tracepoint"
5030 " probe to kernel_sched_switch\n");
5033 kfree(ret_stack_list);
5034 return ret;
5038 * Hibernation protection.
5039 * The state of the current task is too much unstable during
5040 * suspend/restore to disk. We want to protect against that.
5042 static int
5043 ftrace_suspend_notifier_call(struct notifier_block *bl, unsigned long state,
5044 void *unused)
5046 switch (state) {
5047 case PM_HIBERNATION_PREPARE:
5048 pause_graph_tracing();
5049 break;
5051 case PM_POST_HIBERNATION:
5052 unpause_graph_tracing();
5053 break;
5055 return NOTIFY_DONE;
5058 /* Just a place holder for function graph */
5059 static struct ftrace_ops fgraph_ops __read_mostly = {
5060 .func = ftrace_stub,
5061 .flags = FTRACE_OPS_FL_STUB | FTRACE_OPS_FL_GLOBAL |
5062 FTRACE_OPS_FL_RECURSION_SAFE,
5065 static int ftrace_graph_entry_test(struct ftrace_graph_ent *trace)
5067 if (!ftrace_ops_test(&global_ops, trace->func, NULL))
5068 return 0;
5069 return __ftrace_graph_entry(trace);
5073 * The function graph tracer should only trace the functions defined
5074 * by set_ftrace_filter and set_ftrace_notrace. If another function
5075 * tracer ops is registered, the graph tracer requires testing the
5076 * function against the global ops, and not just trace any function
5077 * that any ftrace_ops registered.
5079 static void update_function_graph_func(void)
5081 if (ftrace_ops_list == &ftrace_list_end ||
5082 (ftrace_ops_list == &global_ops &&
5083 global_ops.next == &ftrace_list_end))
5084 ftrace_graph_entry = __ftrace_graph_entry;
5085 else
5086 ftrace_graph_entry = ftrace_graph_entry_test;
5089 int register_ftrace_graph(trace_func_graph_ret_t retfunc,
5090 trace_func_graph_ent_t entryfunc)
5092 int ret = 0;
5094 mutex_lock(&ftrace_lock);
5096 /* we currently allow only one tracer registered at a time */
5097 if (ftrace_graph_active) {
5098 ret = -EBUSY;
5099 goto out;
5102 ftrace_suspend_notifier.notifier_call = ftrace_suspend_notifier_call;
5103 register_pm_notifier(&ftrace_suspend_notifier);
5105 ftrace_graph_active++;
5106 ret = start_graph_tracing();
5107 if (ret) {
5108 ftrace_graph_active--;
5109 goto out;
5112 ftrace_graph_return = retfunc;
5115 * Update the indirect function to the entryfunc, and the
5116 * function that gets called to the entry_test first. Then
5117 * call the update fgraph entry function to determine if
5118 * the entryfunc should be called directly or not.
5120 __ftrace_graph_entry = entryfunc;
5121 ftrace_graph_entry = ftrace_graph_entry_test;
5122 update_function_graph_func();
5124 ret = ftrace_startup(&fgraph_ops, FTRACE_START_FUNC_RET);
5126 out:
5127 mutex_unlock(&ftrace_lock);
5128 return ret;
5131 void unregister_ftrace_graph(void)
5133 mutex_lock(&ftrace_lock);
5135 if (unlikely(!ftrace_graph_active))
5136 goto out;
5138 ftrace_graph_active--;
5139 ftrace_graph_return = (trace_func_graph_ret_t)ftrace_stub;
5140 ftrace_graph_entry = ftrace_graph_entry_stub;
5141 __ftrace_graph_entry = ftrace_graph_entry_stub;
5142 ftrace_shutdown(&fgraph_ops, FTRACE_STOP_FUNC_RET);
5143 unregister_pm_notifier(&ftrace_suspend_notifier);
5144 unregister_trace_sched_switch(ftrace_graph_probe_sched_switch, NULL);
5146 out:
5147 mutex_unlock(&ftrace_lock);
5150 static DEFINE_PER_CPU(struct ftrace_ret_stack *, idle_ret_stack);
5152 static void
5153 graph_init_task(struct task_struct *t, struct ftrace_ret_stack *ret_stack)
5155 atomic_set(&t->tracing_graph_pause, 0);
5156 atomic_set(&t->trace_overrun, 0);
5157 t->ftrace_timestamp = 0;
5158 /* make curr_ret_stack visible before we add the ret_stack */
5159 smp_wmb();
5160 t->ret_stack = ret_stack;
5164 * Allocate a return stack for the idle task. May be the first
5165 * time through, or it may be done by CPU hotplug online.
5167 void ftrace_graph_init_idle_task(struct task_struct *t, int cpu)
5169 t->curr_ret_stack = -1;
5171 * The idle task has no parent, it either has its own
5172 * stack or no stack at all.
5174 if (t->ret_stack)
5175 WARN_ON(t->ret_stack != per_cpu(idle_ret_stack, cpu));
5177 if (ftrace_graph_active) {
5178 struct ftrace_ret_stack *ret_stack;
5180 ret_stack = per_cpu(idle_ret_stack, cpu);
5181 if (!ret_stack) {
5182 ret_stack = kmalloc(FTRACE_RETFUNC_DEPTH
5183 * sizeof(struct ftrace_ret_stack),
5184 GFP_KERNEL);
5185 if (!ret_stack)
5186 return;
5187 per_cpu(idle_ret_stack, cpu) = ret_stack;
5189 graph_init_task(t, ret_stack);
5193 /* Allocate a return stack for newly created task */
5194 void ftrace_graph_init_task(struct task_struct *t)
5196 /* Make sure we do not use the parent ret_stack */
5197 t->ret_stack = NULL;
5198 t->curr_ret_stack = -1;
5200 if (ftrace_graph_active) {
5201 struct ftrace_ret_stack *ret_stack;
5203 ret_stack = kmalloc(FTRACE_RETFUNC_DEPTH
5204 * sizeof(struct ftrace_ret_stack),
5205 GFP_KERNEL);
5206 if (!ret_stack)
5207 return;
5208 graph_init_task(t, ret_stack);
5212 void ftrace_graph_exit_task(struct task_struct *t)
5214 struct ftrace_ret_stack *ret_stack = t->ret_stack;
5216 t->ret_stack = NULL;
5217 /* NULL must become visible to IRQs before we free it: */
5218 barrier();
5220 kfree(ret_stack);
5223 void ftrace_graph_stop(void)
5225 ftrace_stop();
5227 #endif