Linux 3.12.39
[linux/fpc-iii.git] / kernel / trace / ftrace.c
blobd2ab10b3a30ecd7b3e9de0ff3c517dddb4ca5c8c
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 update_function_graph_func();
336 /* If there's no change, then do nothing more here */
337 if (ftrace_trace_function == func)
338 return;
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 loff_t
1120 ftrace_filter_lseek(struct file *file, loff_t offset, int whence)
1122 loff_t ret;
1124 if (file->f_mode & FMODE_READ)
1125 ret = seq_lseek(file, offset, whence);
1126 else
1127 file->f_pos = ret = 1;
1129 return ret;
1132 #ifdef CONFIG_DYNAMIC_FTRACE
1134 #ifndef CONFIG_FTRACE_MCOUNT_RECORD
1135 # error Dynamic ftrace depends on MCOUNT_RECORD
1136 #endif
1138 static struct hlist_head ftrace_func_hash[FTRACE_FUNC_HASHSIZE] __read_mostly;
1140 struct ftrace_func_probe {
1141 struct hlist_node node;
1142 struct ftrace_probe_ops *ops;
1143 unsigned long flags;
1144 unsigned long ip;
1145 void *data;
1146 struct list_head free_list;
1149 struct ftrace_func_entry {
1150 struct hlist_node hlist;
1151 unsigned long ip;
1154 struct ftrace_hash {
1155 unsigned long size_bits;
1156 struct hlist_head *buckets;
1157 unsigned long count;
1158 struct rcu_head rcu;
1162 * We make these constant because no one should touch them,
1163 * but they are used as the default "empty hash", to avoid allocating
1164 * it all the time. These are in a read only section such that if
1165 * anyone does try to modify it, it will cause an exception.
1167 static const struct hlist_head empty_buckets[1];
1168 static const struct ftrace_hash empty_hash = {
1169 .buckets = (struct hlist_head *)empty_buckets,
1171 #define EMPTY_HASH ((struct ftrace_hash *)&empty_hash)
1173 static struct ftrace_ops global_ops = {
1174 .func = ftrace_stub,
1175 .notrace_hash = EMPTY_HASH,
1176 .filter_hash = EMPTY_HASH,
1177 .flags = FTRACE_OPS_FL_RECURSION_SAFE | FTRACE_OPS_FL_INITIALIZED,
1178 INIT_REGEX_LOCK(global_ops)
1181 struct ftrace_page {
1182 struct ftrace_page *next;
1183 struct dyn_ftrace *records;
1184 int index;
1185 int size;
1188 static struct ftrace_page *ftrace_new_pgs;
1190 #define ENTRY_SIZE sizeof(struct dyn_ftrace)
1191 #define ENTRIES_PER_PAGE (PAGE_SIZE / ENTRY_SIZE)
1193 /* estimate from running different kernels */
1194 #define NR_TO_INIT 10000
1196 static struct ftrace_page *ftrace_pages_start;
1197 static struct ftrace_page *ftrace_pages;
1199 static bool ftrace_hash_empty(struct ftrace_hash *hash)
1201 return !hash || !hash->count;
1204 static struct ftrace_func_entry *
1205 ftrace_lookup_ip(struct ftrace_hash *hash, unsigned long ip)
1207 unsigned long key;
1208 struct ftrace_func_entry *entry;
1209 struct hlist_head *hhd;
1211 if (ftrace_hash_empty(hash))
1212 return NULL;
1214 if (hash->size_bits > 0)
1215 key = hash_long(ip, hash->size_bits);
1216 else
1217 key = 0;
1219 hhd = &hash->buckets[key];
1221 hlist_for_each_entry_rcu_notrace(entry, hhd, hlist) {
1222 if (entry->ip == ip)
1223 return entry;
1225 return NULL;
1228 static void __add_hash_entry(struct ftrace_hash *hash,
1229 struct ftrace_func_entry *entry)
1231 struct hlist_head *hhd;
1232 unsigned long key;
1234 if (hash->size_bits)
1235 key = hash_long(entry->ip, hash->size_bits);
1236 else
1237 key = 0;
1239 hhd = &hash->buckets[key];
1240 hlist_add_head(&entry->hlist, hhd);
1241 hash->count++;
1244 static int add_hash_entry(struct ftrace_hash *hash, unsigned long ip)
1246 struct ftrace_func_entry *entry;
1248 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
1249 if (!entry)
1250 return -ENOMEM;
1252 entry->ip = ip;
1253 __add_hash_entry(hash, entry);
1255 return 0;
1258 static void
1259 free_hash_entry(struct ftrace_hash *hash,
1260 struct ftrace_func_entry *entry)
1262 hlist_del(&entry->hlist);
1263 kfree(entry);
1264 hash->count--;
1267 static void
1268 remove_hash_entry(struct ftrace_hash *hash,
1269 struct ftrace_func_entry *entry)
1271 hlist_del(&entry->hlist);
1272 hash->count--;
1275 static void ftrace_hash_clear(struct ftrace_hash *hash)
1277 struct hlist_head *hhd;
1278 struct hlist_node *tn;
1279 struct ftrace_func_entry *entry;
1280 int size = 1 << hash->size_bits;
1281 int i;
1283 if (!hash->count)
1284 return;
1286 for (i = 0; i < size; i++) {
1287 hhd = &hash->buckets[i];
1288 hlist_for_each_entry_safe(entry, tn, hhd, hlist)
1289 free_hash_entry(hash, entry);
1291 FTRACE_WARN_ON(hash->count);
1294 static void free_ftrace_hash(struct ftrace_hash *hash)
1296 if (!hash || hash == EMPTY_HASH)
1297 return;
1298 ftrace_hash_clear(hash);
1299 kfree(hash->buckets);
1300 kfree(hash);
1303 static void __free_ftrace_hash_rcu(struct rcu_head *rcu)
1305 struct ftrace_hash *hash;
1307 hash = container_of(rcu, struct ftrace_hash, rcu);
1308 free_ftrace_hash(hash);
1311 static void free_ftrace_hash_rcu(struct ftrace_hash *hash)
1313 if (!hash || hash == EMPTY_HASH)
1314 return;
1315 call_rcu_sched(&hash->rcu, __free_ftrace_hash_rcu);
1318 void ftrace_free_filter(struct ftrace_ops *ops)
1320 ftrace_ops_init(ops);
1321 free_ftrace_hash(ops->filter_hash);
1322 free_ftrace_hash(ops->notrace_hash);
1325 static struct ftrace_hash *alloc_ftrace_hash(int size_bits)
1327 struct ftrace_hash *hash;
1328 int size;
1330 hash = kzalloc(sizeof(*hash), GFP_KERNEL);
1331 if (!hash)
1332 return NULL;
1334 size = 1 << size_bits;
1335 hash->buckets = kcalloc(size, sizeof(*hash->buckets), GFP_KERNEL);
1337 if (!hash->buckets) {
1338 kfree(hash);
1339 return NULL;
1342 hash->size_bits = size_bits;
1344 return hash;
1347 static struct ftrace_hash *
1348 alloc_and_copy_ftrace_hash(int size_bits, struct ftrace_hash *hash)
1350 struct ftrace_func_entry *entry;
1351 struct ftrace_hash *new_hash;
1352 int size;
1353 int ret;
1354 int i;
1356 new_hash = alloc_ftrace_hash(size_bits);
1357 if (!new_hash)
1358 return NULL;
1360 /* Empty hash? */
1361 if (ftrace_hash_empty(hash))
1362 return new_hash;
1364 size = 1 << hash->size_bits;
1365 for (i = 0; i < size; i++) {
1366 hlist_for_each_entry(entry, &hash->buckets[i], hlist) {
1367 ret = add_hash_entry(new_hash, entry->ip);
1368 if (ret < 0)
1369 goto free_hash;
1373 FTRACE_WARN_ON(new_hash->count != hash->count);
1375 return new_hash;
1377 free_hash:
1378 free_ftrace_hash(new_hash);
1379 return NULL;
1382 static void
1383 ftrace_hash_rec_disable(struct ftrace_ops *ops, int filter_hash);
1384 static void
1385 ftrace_hash_rec_enable(struct ftrace_ops *ops, int filter_hash);
1387 static int
1388 ftrace_hash_move(struct ftrace_ops *ops, int enable,
1389 struct ftrace_hash **dst, struct ftrace_hash *src)
1391 struct ftrace_func_entry *entry;
1392 struct hlist_node *tn;
1393 struct hlist_head *hhd;
1394 struct ftrace_hash *old_hash;
1395 struct ftrace_hash *new_hash;
1396 int size = src->count;
1397 int bits = 0;
1398 int ret;
1399 int i;
1402 * Remove the current set, update the hash and add
1403 * them back.
1405 ftrace_hash_rec_disable(ops, enable);
1408 * If the new source is empty, just free dst and assign it
1409 * the empty_hash.
1411 if (!src->count) {
1412 free_ftrace_hash_rcu(*dst);
1413 rcu_assign_pointer(*dst, EMPTY_HASH);
1414 /* still need to update the function records */
1415 ret = 0;
1416 goto out;
1420 * Make the hash size about 1/2 the # found
1422 for (size /= 2; size; size >>= 1)
1423 bits++;
1425 /* Don't allocate too much */
1426 if (bits > FTRACE_HASH_MAX_BITS)
1427 bits = FTRACE_HASH_MAX_BITS;
1429 ret = -ENOMEM;
1430 new_hash = alloc_ftrace_hash(bits);
1431 if (!new_hash)
1432 goto out;
1434 size = 1 << src->size_bits;
1435 for (i = 0; i < size; i++) {
1436 hhd = &src->buckets[i];
1437 hlist_for_each_entry_safe(entry, tn, hhd, hlist) {
1438 remove_hash_entry(src, entry);
1439 __add_hash_entry(new_hash, entry);
1443 old_hash = *dst;
1444 rcu_assign_pointer(*dst, new_hash);
1445 free_ftrace_hash_rcu(old_hash);
1447 ret = 0;
1448 out:
1450 * Enable regardless of ret:
1451 * On success, we enable the new hash.
1452 * On failure, we re-enable the original hash.
1454 ftrace_hash_rec_enable(ops, enable);
1456 return ret;
1460 * Test the hashes for this ops to see if we want to call
1461 * the ops->func or not.
1463 * It's a match if the ip is in the ops->filter_hash or
1464 * the filter_hash does not exist or is empty,
1465 * AND
1466 * the ip is not in the ops->notrace_hash.
1468 * This needs to be called with preemption disabled as
1469 * the hashes are freed with call_rcu_sched().
1471 static int
1472 ftrace_ops_test(struct ftrace_ops *ops, unsigned long ip, void *regs)
1474 struct ftrace_hash *filter_hash;
1475 struct ftrace_hash *notrace_hash;
1476 int ret;
1478 #ifdef CONFIG_DYNAMIC_FTRACE_WITH_REGS
1480 * There's a small race when adding ops that the ftrace handler
1481 * that wants regs, may be called without them. We can not
1482 * allow that handler to be called if regs is NULL.
1484 if (regs == NULL && (ops->flags & FTRACE_OPS_FL_SAVE_REGS))
1485 return 0;
1486 #endif
1488 filter_hash = rcu_dereference_raw_notrace(ops->filter_hash);
1489 notrace_hash = rcu_dereference_raw_notrace(ops->notrace_hash);
1491 if ((ftrace_hash_empty(filter_hash) ||
1492 ftrace_lookup_ip(filter_hash, ip)) &&
1493 (ftrace_hash_empty(notrace_hash) ||
1494 !ftrace_lookup_ip(notrace_hash, ip)))
1495 ret = 1;
1496 else
1497 ret = 0;
1499 return ret;
1503 * This is a double for. Do not use 'break' to break out of the loop,
1504 * you must use a goto.
1506 #define do_for_each_ftrace_rec(pg, rec) \
1507 for (pg = ftrace_pages_start; pg; pg = pg->next) { \
1508 int _____i; \
1509 for (_____i = 0; _____i < pg->index; _____i++) { \
1510 rec = &pg->records[_____i];
1512 #define while_for_each_ftrace_rec() \
1517 static int ftrace_cmp_recs(const void *a, const void *b)
1519 const struct dyn_ftrace *key = a;
1520 const struct dyn_ftrace *rec = b;
1522 if (key->flags < rec->ip)
1523 return -1;
1524 if (key->ip >= rec->ip + MCOUNT_INSN_SIZE)
1525 return 1;
1526 return 0;
1529 static unsigned long ftrace_location_range(unsigned long start, unsigned long end)
1531 struct ftrace_page *pg;
1532 struct dyn_ftrace *rec;
1533 struct dyn_ftrace key;
1535 key.ip = start;
1536 key.flags = end; /* overload flags, as it is unsigned long */
1538 for (pg = ftrace_pages_start; pg; pg = pg->next) {
1539 if (end < pg->records[0].ip ||
1540 start >= (pg->records[pg->index - 1].ip + MCOUNT_INSN_SIZE))
1541 continue;
1542 rec = bsearch(&key, pg->records, pg->index,
1543 sizeof(struct dyn_ftrace),
1544 ftrace_cmp_recs);
1545 if (rec)
1546 return rec->ip;
1549 return 0;
1553 * ftrace_location - return true if the ip giving is a traced location
1554 * @ip: the instruction pointer to check
1556 * Returns rec->ip if @ip given is a pointer to a ftrace location.
1557 * That is, the instruction that is either a NOP or call to
1558 * the function tracer. It checks the ftrace internal tables to
1559 * determine if the address belongs or not.
1561 unsigned long ftrace_location(unsigned long ip)
1563 return ftrace_location_range(ip, ip);
1567 * ftrace_text_reserved - return true if range contains an ftrace location
1568 * @start: start of range to search
1569 * @end: end of range to search (inclusive). @end points to the last byte to check.
1571 * Returns 1 if @start and @end contains a ftrace location.
1572 * That is, the instruction that is either a NOP or call to
1573 * the function tracer. It checks the ftrace internal tables to
1574 * determine if the address belongs or not.
1576 int ftrace_text_reserved(void *start, void *end)
1578 unsigned long ret;
1580 ret = ftrace_location_range((unsigned long)start,
1581 (unsigned long)end);
1583 return (int)!!ret;
1586 static void __ftrace_hash_rec_update(struct ftrace_ops *ops,
1587 int filter_hash,
1588 bool inc)
1590 struct ftrace_hash *hash;
1591 struct ftrace_hash *other_hash;
1592 struct ftrace_page *pg;
1593 struct dyn_ftrace *rec;
1594 int count = 0;
1595 int all = 0;
1597 /* Only update if the ops has been registered */
1598 if (!(ops->flags & FTRACE_OPS_FL_ENABLED))
1599 return;
1602 * In the filter_hash case:
1603 * If the count is zero, we update all records.
1604 * Otherwise we just update the items in the hash.
1606 * In the notrace_hash case:
1607 * We enable the update in the hash.
1608 * As disabling notrace means enabling the tracing,
1609 * and enabling notrace means disabling, the inc variable
1610 * gets inversed.
1612 if (filter_hash) {
1613 hash = ops->filter_hash;
1614 other_hash = ops->notrace_hash;
1615 if (ftrace_hash_empty(hash))
1616 all = 1;
1617 } else {
1618 inc = !inc;
1619 hash = ops->notrace_hash;
1620 other_hash = ops->filter_hash;
1622 * If the notrace hash has no items,
1623 * then there's nothing to do.
1625 if (ftrace_hash_empty(hash))
1626 return;
1629 do_for_each_ftrace_rec(pg, rec) {
1630 int in_other_hash = 0;
1631 int in_hash = 0;
1632 int match = 0;
1634 if (all) {
1636 * Only the filter_hash affects all records.
1637 * Update if the record is not in the notrace hash.
1639 if (!other_hash || !ftrace_lookup_ip(other_hash, rec->ip))
1640 match = 1;
1641 } else {
1642 in_hash = !!ftrace_lookup_ip(hash, rec->ip);
1643 in_other_hash = !!ftrace_lookup_ip(other_hash, rec->ip);
1648 if (filter_hash && in_hash && !in_other_hash)
1649 match = 1;
1650 else if (!filter_hash && in_hash &&
1651 (in_other_hash || ftrace_hash_empty(other_hash)))
1652 match = 1;
1654 if (!match)
1655 continue;
1657 if (inc) {
1658 rec->flags++;
1659 if (FTRACE_WARN_ON((rec->flags & ~FTRACE_FL_MASK) == FTRACE_REF_MAX))
1660 return;
1662 * If any ops wants regs saved for this function
1663 * then all ops will get saved regs.
1665 if (ops->flags & FTRACE_OPS_FL_SAVE_REGS)
1666 rec->flags |= FTRACE_FL_REGS;
1667 } else {
1668 if (FTRACE_WARN_ON((rec->flags & ~FTRACE_FL_MASK) == 0))
1669 return;
1670 rec->flags--;
1672 count++;
1673 /* Shortcut, if we handled all records, we are done. */
1674 if (!all && count == hash->count)
1675 return;
1676 } while_for_each_ftrace_rec();
1679 static void ftrace_hash_rec_disable(struct ftrace_ops *ops,
1680 int filter_hash)
1682 __ftrace_hash_rec_update(ops, filter_hash, 0);
1685 static void ftrace_hash_rec_enable(struct ftrace_ops *ops,
1686 int filter_hash)
1688 __ftrace_hash_rec_update(ops, filter_hash, 1);
1691 static void print_ip_ins(const char *fmt, unsigned char *p)
1693 int i;
1695 printk(KERN_CONT "%s", fmt);
1697 for (i = 0; i < MCOUNT_INSN_SIZE; i++)
1698 printk(KERN_CONT "%s%02x", i ? ":" : "", p[i]);
1702 * ftrace_bug - report and shutdown function tracer
1703 * @failed: The failed type (EFAULT, EINVAL, EPERM)
1704 * @ip: The address that failed
1706 * The arch code that enables or disables the function tracing
1707 * can call ftrace_bug() when it has detected a problem in
1708 * modifying the code. @failed should be one of either:
1709 * EFAULT - if the problem happens on reading the @ip address
1710 * EINVAL - if what is read at @ip is not what was expected
1711 * EPERM - if the problem happens on writting to the @ip address
1713 void ftrace_bug(int failed, unsigned long ip)
1715 switch (failed) {
1716 case -EFAULT:
1717 FTRACE_WARN_ON_ONCE(1);
1718 pr_info("ftrace faulted on modifying ");
1719 print_ip_sym(ip);
1720 break;
1721 case -EINVAL:
1722 FTRACE_WARN_ON_ONCE(1);
1723 pr_info("ftrace failed to modify ");
1724 print_ip_sym(ip);
1725 print_ip_ins(" actual: ", (unsigned char *)ip);
1726 printk(KERN_CONT "\n");
1727 break;
1728 case -EPERM:
1729 FTRACE_WARN_ON_ONCE(1);
1730 pr_info("ftrace faulted on writing ");
1731 print_ip_sym(ip);
1732 break;
1733 default:
1734 FTRACE_WARN_ON_ONCE(1);
1735 pr_info("ftrace faulted on unknown error ");
1736 print_ip_sym(ip);
1740 static int ftrace_check_record(struct dyn_ftrace *rec, int enable, int update)
1742 unsigned long flag = 0UL;
1745 * If we are updating calls:
1747 * If the record has a ref count, then we need to enable it
1748 * because someone is using it.
1750 * Otherwise we make sure its disabled.
1752 * If we are disabling calls, then disable all records that
1753 * are enabled.
1755 if (enable && (rec->flags & ~FTRACE_FL_MASK))
1756 flag = FTRACE_FL_ENABLED;
1759 * If enabling and the REGS flag does not match the REGS_EN, then
1760 * do not ignore this record. Set flags to fail the compare against
1761 * ENABLED.
1763 if (flag &&
1764 (!(rec->flags & FTRACE_FL_REGS) != !(rec->flags & FTRACE_FL_REGS_EN)))
1765 flag |= FTRACE_FL_REGS;
1767 /* If the state of this record hasn't changed, then do nothing */
1768 if ((rec->flags & FTRACE_FL_ENABLED) == flag)
1769 return FTRACE_UPDATE_IGNORE;
1771 if (flag) {
1772 /* Save off if rec is being enabled (for return value) */
1773 flag ^= rec->flags & FTRACE_FL_ENABLED;
1775 if (update) {
1776 rec->flags |= FTRACE_FL_ENABLED;
1777 if (flag & FTRACE_FL_REGS) {
1778 if (rec->flags & FTRACE_FL_REGS)
1779 rec->flags |= FTRACE_FL_REGS_EN;
1780 else
1781 rec->flags &= ~FTRACE_FL_REGS_EN;
1786 * If this record is being updated from a nop, then
1787 * return UPDATE_MAKE_CALL.
1788 * Otherwise, if the EN flag is set, then return
1789 * UPDATE_MODIFY_CALL_REGS to tell the caller to convert
1790 * from the non-save regs, to a save regs function.
1791 * Otherwise,
1792 * return UPDATE_MODIFY_CALL to tell the caller to convert
1793 * from the save regs, to a non-save regs function.
1795 if (flag & FTRACE_FL_ENABLED)
1796 return FTRACE_UPDATE_MAKE_CALL;
1797 else if (rec->flags & FTRACE_FL_REGS_EN)
1798 return FTRACE_UPDATE_MODIFY_CALL_REGS;
1799 else
1800 return FTRACE_UPDATE_MODIFY_CALL;
1803 if (update) {
1804 /* If there's no more users, clear all flags */
1805 if (!(rec->flags & ~FTRACE_FL_MASK))
1806 rec->flags = 0;
1807 else
1808 /* Just disable the record (keep REGS state) */
1809 rec->flags &= ~FTRACE_FL_ENABLED;
1812 return FTRACE_UPDATE_MAKE_NOP;
1816 * ftrace_update_record, set a record that now is tracing or not
1817 * @rec: the record to update
1818 * @enable: set to 1 if the record is tracing, zero to force disable
1820 * The records that represent all functions that can be traced need
1821 * to be updated when tracing has been enabled.
1823 int ftrace_update_record(struct dyn_ftrace *rec, int enable)
1825 return ftrace_check_record(rec, enable, 1);
1829 * ftrace_test_record, check if the record has been enabled or not
1830 * @rec: the record to test
1831 * @enable: set to 1 to check if enabled, 0 if it is disabled
1833 * The arch code may need to test if a record is already set to
1834 * tracing to determine how to modify the function code that it
1835 * represents.
1837 int ftrace_test_record(struct dyn_ftrace *rec, int enable)
1839 return ftrace_check_record(rec, enable, 0);
1842 static int
1843 __ftrace_replace_code(struct dyn_ftrace *rec, int enable)
1845 unsigned long ftrace_old_addr;
1846 unsigned long ftrace_addr;
1847 int ret;
1849 ret = ftrace_update_record(rec, enable);
1851 if (rec->flags & FTRACE_FL_REGS)
1852 ftrace_addr = (unsigned long)FTRACE_REGS_ADDR;
1853 else
1854 ftrace_addr = (unsigned long)FTRACE_ADDR;
1856 switch (ret) {
1857 case FTRACE_UPDATE_IGNORE:
1858 return 0;
1860 case FTRACE_UPDATE_MAKE_CALL:
1861 return ftrace_make_call(rec, ftrace_addr);
1863 case FTRACE_UPDATE_MAKE_NOP:
1864 return ftrace_make_nop(NULL, rec, ftrace_addr);
1866 case FTRACE_UPDATE_MODIFY_CALL_REGS:
1867 case FTRACE_UPDATE_MODIFY_CALL:
1868 if (rec->flags & FTRACE_FL_REGS)
1869 ftrace_old_addr = (unsigned long)FTRACE_ADDR;
1870 else
1871 ftrace_old_addr = (unsigned long)FTRACE_REGS_ADDR;
1873 return ftrace_modify_call(rec, ftrace_old_addr, ftrace_addr);
1876 return -1; /* unknow ftrace bug */
1879 void __weak ftrace_replace_code(int enable)
1881 struct dyn_ftrace *rec;
1882 struct ftrace_page *pg;
1883 int failed;
1885 if (unlikely(ftrace_disabled))
1886 return;
1888 do_for_each_ftrace_rec(pg, rec) {
1889 failed = __ftrace_replace_code(rec, enable);
1890 if (failed) {
1891 ftrace_bug(failed, rec->ip);
1892 /* Stop processing */
1893 return;
1895 } while_for_each_ftrace_rec();
1898 struct ftrace_rec_iter {
1899 struct ftrace_page *pg;
1900 int index;
1904 * ftrace_rec_iter_start, start up iterating over traced functions
1906 * Returns an iterator handle that is used to iterate over all
1907 * the records that represent address locations where functions
1908 * are traced.
1910 * May return NULL if no records are available.
1912 struct ftrace_rec_iter *ftrace_rec_iter_start(void)
1915 * We only use a single iterator.
1916 * Protected by the ftrace_lock mutex.
1918 static struct ftrace_rec_iter ftrace_rec_iter;
1919 struct ftrace_rec_iter *iter = &ftrace_rec_iter;
1921 iter->pg = ftrace_pages_start;
1922 iter->index = 0;
1924 /* Could have empty pages */
1925 while (iter->pg && !iter->pg->index)
1926 iter->pg = iter->pg->next;
1928 if (!iter->pg)
1929 return NULL;
1931 return iter;
1935 * ftrace_rec_iter_next, get the next record to process.
1936 * @iter: The handle to the iterator.
1938 * Returns the next iterator after the given iterator @iter.
1940 struct ftrace_rec_iter *ftrace_rec_iter_next(struct ftrace_rec_iter *iter)
1942 iter->index++;
1944 if (iter->index >= iter->pg->index) {
1945 iter->pg = iter->pg->next;
1946 iter->index = 0;
1948 /* Could have empty pages */
1949 while (iter->pg && !iter->pg->index)
1950 iter->pg = iter->pg->next;
1953 if (!iter->pg)
1954 return NULL;
1956 return iter;
1960 * ftrace_rec_iter_record, get the record at the iterator location
1961 * @iter: The current iterator location
1963 * Returns the record that the current @iter is at.
1965 struct dyn_ftrace *ftrace_rec_iter_record(struct ftrace_rec_iter *iter)
1967 return &iter->pg->records[iter->index];
1970 static int
1971 ftrace_code_disable(struct module *mod, struct dyn_ftrace *rec)
1973 unsigned long ip;
1974 int ret;
1976 ip = rec->ip;
1978 if (unlikely(ftrace_disabled))
1979 return 0;
1981 ret = ftrace_make_nop(mod, rec, MCOUNT_ADDR);
1982 if (ret) {
1983 ftrace_bug(ret, ip);
1984 return 0;
1986 return 1;
1990 * archs can override this function if they must do something
1991 * before the modifying code is performed.
1993 int __weak ftrace_arch_code_modify_prepare(void)
1995 return 0;
1999 * archs can override this function if they must do something
2000 * after the modifying code is performed.
2002 int __weak ftrace_arch_code_modify_post_process(void)
2004 return 0;
2007 void ftrace_modify_all_code(int command)
2009 int update = command & FTRACE_UPDATE_TRACE_FUNC;
2012 * If the ftrace_caller calls a ftrace_ops func directly,
2013 * we need to make sure that it only traces functions it
2014 * expects to trace. When doing the switch of functions,
2015 * we need to update to the ftrace_ops_list_func first
2016 * before the transition between old and new calls are set,
2017 * as the ftrace_ops_list_func will check the ops hashes
2018 * to make sure the ops are having the right functions
2019 * traced.
2021 if (update)
2022 ftrace_update_ftrace_func(ftrace_ops_list_func);
2024 if (command & FTRACE_UPDATE_CALLS)
2025 ftrace_replace_code(1);
2026 else if (command & FTRACE_DISABLE_CALLS)
2027 ftrace_replace_code(0);
2029 if (update && ftrace_trace_function != ftrace_ops_list_func) {
2030 function_trace_op = set_function_trace_op;
2031 smp_wmb();
2032 /* If irqs are disabled, we are in stop machine */
2033 if (!irqs_disabled())
2034 smp_call_function(ftrace_sync_ipi, NULL, 1);
2035 ftrace_update_ftrace_func(ftrace_trace_function);
2038 if (command & FTRACE_START_FUNC_RET)
2039 ftrace_enable_ftrace_graph_caller();
2040 else if (command & FTRACE_STOP_FUNC_RET)
2041 ftrace_disable_ftrace_graph_caller();
2044 static int __ftrace_modify_code(void *data)
2046 int *command = data;
2048 ftrace_modify_all_code(*command);
2050 return 0;
2054 * ftrace_run_stop_machine, go back to the stop machine method
2055 * @command: The command to tell ftrace what to do
2057 * If an arch needs to fall back to the stop machine method, the
2058 * it can call this function.
2060 void ftrace_run_stop_machine(int command)
2062 stop_machine(__ftrace_modify_code, &command, NULL);
2066 * arch_ftrace_update_code, modify the code to trace or not trace
2067 * @command: The command that needs to be done
2069 * Archs can override this function if it does not need to
2070 * run stop_machine() to modify code.
2072 void __weak arch_ftrace_update_code(int command)
2074 ftrace_run_stop_machine(command);
2077 static void ftrace_run_update_code(int command)
2079 int ret;
2081 ret = ftrace_arch_code_modify_prepare();
2082 FTRACE_WARN_ON(ret);
2083 if (ret)
2084 return;
2086 * Do not call function tracer while we update the code.
2087 * We are in stop machine.
2089 function_trace_stop++;
2092 * By default we use stop_machine() to modify the code.
2093 * But archs can do what ever they want as long as it
2094 * is safe. The stop_machine() is the safest, but also
2095 * produces the most overhead.
2097 arch_ftrace_update_code(command);
2099 function_trace_stop--;
2101 ret = ftrace_arch_code_modify_post_process();
2102 FTRACE_WARN_ON(ret);
2105 static ftrace_func_t saved_ftrace_func;
2106 static int ftrace_start_up;
2107 static int global_start_up;
2109 static void ftrace_startup_enable(int command)
2111 if (saved_ftrace_func != ftrace_trace_function) {
2112 saved_ftrace_func = ftrace_trace_function;
2113 command |= FTRACE_UPDATE_TRACE_FUNC;
2116 if (!command || !ftrace_enabled)
2117 return;
2119 ftrace_run_update_code(command);
2122 static int ftrace_startup(struct ftrace_ops *ops, int command)
2124 bool hash_enable = true;
2125 int ret;
2127 if (unlikely(ftrace_disabled))
2128 return -ENODEV;
2130 ret = __register_ftrace_function(ops);
2131 if (ret)
2132 return ret;
2134 ftrace_start_up++;
2135 command |= FTRACE_UPDATE_CALLS;
2137 /* ops marked global share the filter hashes */
2138 if (ops->flags & FTRACE_OPS_FL_GLOBAL) {
2139 ops = &global_ops;
2140 /* Don't update hash if global is already set */
2141 if (global_start_up)
2142 hash_enable = false;
2143 global_start_up++;
2146 ops->flags |= FTRACE_OPS_FL_ENABLED;
2147 if (hash_enable)
2148 ftrace_hash_rec_enable(ops, 1);
2150 ftrace_startup_enable(command);
2152 return 0;
2155 static int ftrace_shutdown(struct ftrace_ops *ops, int command)
2157 bool hash_disable = true;
2158 int ret;
2160 if (unlikely(ftrace_disabled))
2161 return -ENODEV;
2163 ret = __unregister_ftrace_function(ops);
2164 if (ret)
2165 return ret;
2167 ftrace_start_up--;
2169 * Just warn in case of unbalance, no need to kill ftrace, it's not
2170 * critical but the ftrace_call callers may be never nopped again after
2171 * further ftrace uses.
2173 WARN_ON_ONCE(ftrace_start_up < 0);
2175 if (ops->flags & FTRACE_OPS_FL_GLOBAL) {
2176 ops = &global_ops;
2177 global_start_up--;
2178 WARN_ON_ONCE(global_start_up < 0);
2179 /* Don't update hash if global still has users */
2180 if (global_start_up) {
2181 WARN_ON_ONCE(!ftrace_start_up);
2182 hash_disable = false;
2186 if (hash_disable)
2187 ftrace_hash_rec_disable(ops, 1);
2189 if (ops != &global_ops || !global_start_up)
2190 ops->flags &= ~FTRACE_OPS_FL_ENABLED;
2192 command |= FTRACE_UPDATE_CALLS;
2194 if (saved_ftrace_func != ftrace_trace_function) {
2195 saved_ftrace_func = ftrace_trace_function;
2196 command |= FTRACE_UPDATE_TRACE_FUNC;
2199 if (!command || !ftrace_enabled) {
2201 * If these are control ops, they still need their
2202 * per_cpu field freed. Since, function tracing is
2203 * not currently active, we can just free them
2204 * without synchronizing all CPUs.
2206 if (ops->flags & FTRACE_OPS_FL_CONTROL)
2207 control_ops_free(ops);
2208 return 0;
2211 ftrace_run_update_code(command);
2214 * Dynamic ops may be freed, we must make sure that all
2215 * callers are done before leaving this function.
2216 * The same goes for freeing the per_cpu data of the control
2217 * ops.
2219 * Again, normal synchronize_sched() is not good enough.
2220 * We need to do a hard force of sched synchronization.
2221 * This is because we use preempt_disable() to do RCU, but
2222 * the function tracers can be called where RCU is not watching
2223 * (like before user_exit()). We can not rely on the RCU
2224 * infrastructure to do the synchronization, thus we must do it
2225 * ourselves.
2227 if (ops->flags & (FTRACE_OPS_FL_DYNAMIC | FTRACE_OPS_FL_CONTROL)) {
2228 schedule_on_each_cpu(ftrace_sync);
2230 if (ops->flags & FTRACE_OPS_FL_CONTROL)
2231 control_ops_free(ops);
2234 return 0;
2237 static void ftrace_startup_sysctl(void)
2239 if (unlikely(ftrace_disabled))
2240 return;
2242 /* Force update next time */
2243 saved_ftrace_func = NULL;
2244 /* ftrace_start_up is true if we want ftrace running */
2245 if (ftrace_start_up)
2246 ftrace_run_update_code(FTRACE_UPDATE_CALLS);
2249 static void ftrace_shutdown_sysctl(void)
2251 if (unlikely(ftrace_disabled))
2252 return;
2254 /* ftrace_start_up is true if ftrace is running */
2255 if (ftrace_start_up)
2256 ftrace_run_update_code(FTRACE_DISABLE_CALLS);
2259 static cycle_t ftrace_update_time;
2260 static unsigned long ftrace_update_cnt;
2261 unsigned long ftrace_update_tot_cnt;
2263 static inline int ops_traces_mod(struct ftrace_ops *ops)
2266 * Filter_hash being empty will default to trace module.
2267 * But notrace hash requires a test of individual module functions.
2269 return ftrace_hash_empty(ops->filter_hash) &&
2270 ftrace_hash_empty(ops->notrace_hash);
2274 * Check if the current ops references the record.
2276 * If the ops traces all functions, then it was already accounted for.
2277 * If the ops does not trace the current record function, skip it.
2278 * If the ops ignores the function via notrace filter, skip it.
2280 static inline bool
2281 ops_references_rec(struct ftrace_ops *ops, struct dyn_ftrace *rec)
2283 /* If ops isn't enabled, ignore it */
2284 if (!(ops->flags & FTRACE_OPS_FL_ENABLED))
2285 return 0;
2287 /* If ops traces all mods, we already accounted for it */
2288 if (ops_traces_mod(ops))
2289 return 0;
2291 /* The function must be in the filter */
2292 if (!ftrace_hash_empty(ops->filter_hash) &&
2293 !ftrace_lookup_ip(ops->filter_hash, rec->ip))
2294 return 0;
2296 /* If in notrace hash, we ignore it too */
2297 if (ftrace_lookup_ip(ops->notrace_hash, rec->ip))
2298 return 0;
2300 return 1;
2303 static int referenced_filters(struct dyn_ftrace *rec)
2305 struct ftrace_ops *ops;
2306 int cnt = 0;
2308 for (ops = ftrace_ops_list; ops != &ftrace_list_end; ops = ops->next) {
2309 if (ops_references_rec(ops, rec))
2310 cnt++;
2313 return cnt;
2316 static int ftrace_update_code(struct module *mod)
2318 struct ftrace_page *pg;
2319 struct dyn_ftrace *p;
2320 cycle_t start, stop;
2321 unsigned long ref = 0;
2322 bool test = false;
2323 int i;
2326 * When adding a module, we need to check if tracers are
2327 * currently enabled and if they are set to trace all functions.
2328 * If they are, we need to enable the module functions as well
2329 * as update the reference counts for those function records.
2331 if (mod) {
2332 struct ftrace_ops *ops;
2334 for (ops = ftrace_ops_list;
2335 ops != &ftrace_list_end; ops = ops->next) {
2336 if (ops->flags & FTRACE_OPS_FL_ENABLED) {
2337 if (ops_traces_mod(ops))
2338 ref++;
2339 else
2340 test = true;
2345 start = ftrace_now(raw_smp_processor_id());
2346 ftrace_update_cnt = 0;
2348 for (pg = ftrace_new_pgs; pg; pg = pg->next) {
2350 for (i = 0; i < pg->index; i++) {
2351 int cnt = ref;
2353 /* If something went wrong, bail without enabling anything */
2354 if (unlikely(ftrace_disabled))
2355 return -1;
2357 p = &pg->records[i];
2358 if (test)
2359 cnt += referenced_filters(p);
2360 p->flags = cnt;
2363 * Do the initial record conversion from mcount jump
2364 * to the NOP instructions.
2366 if (!ftrace_code_disable(mod, p))
2367 break;
2369 ftrace_update_cnt++;
2372 * If the tracing is enabled, go ahead and enable the record.
2374 * The reason not to enable the record immediatelly is the
2375 * inherent check of ftrace_make_nop/ftrace_make_call for
2376 * correct previous instructions. Making first the NOP
2377 * conversion puts the module to the correct state, thus
2378 * passing the ftrace_make_call check.
2380 if (ftrace_start_up && cnt) {
2381 int failed = __ftrace_replace_code(p, 1);
2382 if (failed)
2383 ftrace_bug(failed, p->ip);
2388 ftrace_new_pgs = NULL;
2390 stop = ftrace_now(raw_smp_processor_id());
2391 ftrace_update_time = stop - start;
2392 ftrace_update_tot_cnt += ftrace_update_cnt;
2394 return 0;
2397 static int ftrace_allocate_records(struct ftrace_page *pg, int count)
2399 int order;
2400 int cnt;
2402 if (WARN_ON(!count))
2403 return -EINVAL;
2405 order = get_count_order(DIV_ROUND_UP(count, ENTRIES_PER_PAGE));
2408 * We want to fill as much as possible. No more than a page
2409 * may be empty.
2411 while ((PAGE_SIZE << order) / ENTRY_SIZE >= count + ENTRIES_PER_PAGE)
2412 order--;
2414 again:
2415 pg->records = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, order);
2417 if (!pg->records) {
2418 /* if we can't allocate this size, try something smaller */
2419 if (!order)
2420 return -ENOMEM;
2421 order >>= 1;
2422 goto again;
2425 cnt = (PAGE_SIZE << order) / ENTRY_SIZE;
2426 pg->size = cnt;
2428 if (cnt > count)
2429 cnt = count;
2431 return cnt;
2434 static struct ftrace_page *
2435 ftrace_allocate_pages(unsigned long num_to_init)
2437 struct ftrace_page *start_pg;
2438 struct ftrace_page *pg;
2439 int order;
2440 int cnt;
2442 if (!num_to_init)
2443 return 0;
2445 start_pg = pg = kzalloc(sizeof(*pg), GFP_KERNEL);
2446 if (!pg)
2447 return NULL;
2450 * Try to allocate as much as possible in one continues
2451 * location that fills in all of the space. We want to
2452 * waste as little space as possible.
2454 for (;;) {
2455 cnt = ftrace_allocate_records(pg, num_to_init);
2456 if (cnt < 0)
2457 goto free_pages;
2459 num_to_init -= cnt;
2460 if (!num_to_init)
2461 break;
2463 pg->next = kzalloc(sizeof(*pg), GFP_KERNEL);
2464 if (!pg->next)
2465 goto free_pages;
2467 pg = pg->next;
2470 return start_pg;
2472 free_pages:
2473 while (start_pg) {
2474 order = get_count_order(pg->size / ENTRIES_PER_PAGE);
2475 free_pages((unsigned long)pg->records, order);
2476 start_pg = pg->next;
2477 kfree(pg);
2478 pg = start_pg;
2480 pr_info("ftrace: FAILED to allocate memory for functions\n");
2481 return NULL;
2484 static int __init ftrace_dyn_table_alloc(unsigned long num_to_init)
2486 int cnt;
2488 if (!num_to_init) {
2489 pr_info("ftrace: No functions to be traced?\n");
2490 return -1;
2493 cnt = num_to_init / ENTRIES_PER_PAGE;
2494 pr_info("ftrace: allocating %ld entries in %d pages\n",
2495 num_to_init, cnt + 1);
2497 return 0;
2500 #define FTRACE_BUFF_MAX (KSYM_SYMBOL_LEN+4) /* room for wildcards */
2502 struct ftrace_iterator {
2503 loff_t pos;
2504 loff_t func_pos;
2505 struct ftrace_page *pg;
2506 struct dyn_ftrace *func;
2507 struct ftrace_func_probe *probe;
2508 struct trace_parser parser;
2509 struct ftrace_hash *hash;
2510 struct ftrace_ops *ops;
2511 int hidx;
2512 int idx;
2513 unsigned flags;
2516 static void *
2517 t_hash_next(struct seq_file *m, loff_t *pos)
2519 struct ftrace_iterator *iter = m->private;
2520 struct hlist_node *hnd = NULL;
2521 struct hlist_head *hhd;
2523 (*pos)++;
2524 iter->pos = *pos;
2526 if (iter->probe)
2527 hnd = &iter->probe->node;
2528 retry:
2529 if (iter->hidx >= FTRACE_FUNC_HASHSIZE)
2530 return NULL;
2532 hhd = &ftrace_func_hash[iter->hidx];
2534 if (hlist_empty(hhd)) {
2535 iter->hidx++;
2536 hnd = NULL;
2537 goto retry;
2540 if (!hnd)
2541 hnd = hhd->first;
2542 else {
2543 hnd = hnd->next;
2544 if (!hnd) {
2545 iter->hidx++;
2546 goto retry;
2550 if (WARN_ON_ONCE(!hnd))
2551 return NULL;
2553 iter->probe = hlist_entry(hnd, struct ftrace_func_probe, node);
2555 return iter;
2558 static void *t_hash_start(struct seq_file *m, loff_t *pos)
2560 struct ftrace_iterator *iter = m->private;
2561 void *p = NULL;
2562 loff_t l;
2564 if (!(iter->flags & FTRACE_ITER_DO_HASH))
2565 return NULL;
2567 if (iter->func_pos > *pos)
2568 return NULL;
2570 iter->hidx = 0;
2571 for (l = 0; l <= (*pos - iter->func_pos); ) {
2572 p = t_hash_next(m, &l);
2573 if (!p)
2574 break;
2576 if (!p)
2577 return NULL;
2579 /* Only set this if we have an item */
2580 iter->flags |= FTRACE_ITER_HASH;
2582 return iter;
2585 static int
2586 t_hash_show(struct seq_file *m, struct ftrace_iterator *iter)
2588 struct ftrace_func_probe *rec;
2590 rec = iter->probe;
2591 if (WARN_ON_ONCE(!rec))
2592 return -EIO;
2594 if (rec->ops->print)
2595 return rec->ops->print(m, rec->ip, rec->ops, rec->data);
2597 seq_printf(m, "%ps:%ps", (void *)rec->ip, (void *)rec->ops->func);
2599 if (rec->data)
2600 seq_printf(m, ":%p", rec->data);
2601 seq_putc(m, '\n');
2603 return 0;
2606 static void *
2607 t_next(struct seq_file *m, void *v, loff_t *pos)
2609 struct ftrace_iterator *iter = m->private;
2610 struct ftrace_ops *ops = iter->ops;
2611 struct dyn_ftrace *rec = NULL;
2613 if (unlikely(ftrace_disabled))
2614 return NULL;
2616 if (iter->flags & FTRACE_ITER_HASH)
2617 return t_hash_next(m, pos);
2619 (*pos)++;
2620 iter->pos = iter->func_pos = *pos;
2622 if (iter->flags & FTRACE_ITER_PRINTALL)
2623 return t_hash_start(m, pos);
2625 retry:
2626 if (iter->idx >= iter->pg->index) {
2627 if (iter->pg->next) {
2628 iter->pg = iter->pg->next;
2629 iter->idx = 0;
2630 goto retry;
2632 } else {
2633 rec = &iter->pg->records[iter->idx++];
2634 if (((iter->flags & FTRACE_ITER_FILTER) &&
2635 !(ftrace_lookup_ip(ops->filter_hash, rec->ip))) ||
2637 ((iter->flags & FTRACE_ITER_NOTRACE) &&
2638 !ftrace_lookup_ip(ops->notrace_hash, rec->ip)) ||
2640 ((iter->flags & FTRACE_ITER_ENABLED) &&
2641 !(rec->flags & FTRACE_FL_ENABLED))) {
2643 rec = NULL;
2644 goto retry;
2648 if (!rec)
2649 return t_hash_start(m, pos);
2651 iter->func = rec;
2653 return iter;
2656 static void reset_iter_read(struct ftrace_iterator *iter)
2658 iter->pos = 0;
2659 iter->func_pos = 0;
2660 iter->flags &= ~(FTRACE_ITER_PRINTALL | FTRACE_ITER_HASH);
2663 static void *t_start(struct seq_file *m, loff_t *pos)
2665 struct ftrace_iterator *iter = m->private;
2666 struct ftrace_ops *ops = iter->ops;
2667 void *p = NULL;
2668 loff_t l;
2670 mutex_lock(&ftrace_lock);
2672 if (unlikely(ftrace_disabled))
2673 return NULL;
2676 * If an lseek was done, then reset and start from beginning.
2678 if (*pos < iter->pos)
2679 reset_iter_read(iter);
2682 * For set_ftrace_filter reading, if we have the filter
2683 * off, we can short cut and just print out that all
2684 * functions are enabled.
2686 if (iter->flags & FTRACE_ITER_FILTER &&
2687 ftrace_hash_empty(ops->filter_hash)) {
2688 if (*pos > 0)
2689 return t_hash_start(m, pos);
2690 iter->flags |= FTRACE_ITER_PRINTALL;
2691 /* reset in case of seek/pread */
2692 iter->flags &= ~FTRACE_ITER_HASH;
2693 return iter;
2696 if (iter->flags & FTRACE_ITER_HASH)
2697 return t_hash_start(m, pos);
2700 * Unfortunately, we need to restart at ftrace_pages_start
2701 * every time we let go of the ftrace_mutex. This is because
2702 * those pointers can change without the lock.
2704 iter->pg = ftrace_pages_start;
2705 iter->idx = 0;
2706 for (l = 0; l <= *pos; ) {
2707 p = t_next(m, p, &l);
2708 if (!p)
2709 break;
2712 if (!p)
2713 return t_hash_start(m, pos);
2715 return iter;
2718 static void t_stop(struct seq_file *m, void *p)
2720 mutex_unlock(&ftrace_lock);
2723 static int t_show(struct seq_file *m, void *v)
2725 struct ftrace_iterator *iter = m->private;
2726 struct dyn_ftrace *rec;
2728 if (iter->flags & FTRACE_ITER_HASH)
2729 return t_hash_show(m, iter);
2731 if (iter->flags & FTRACE_ITER_PRINTALL) {
2732 seq_printf(m, "#### all functions enabled ####\n");
2733 return 0;
2736 rec = iter->func;
2738 if (!rec)
2739 return 0;
2741 seq_printf(m, "%ps", (void *)rec->ip);
2742 if (iter->flags & FTRACE_ITER_ENABLED)
2743 seq_printf(m, " (%ld)%s",
2744 rec->flags & ~FTRACE_FL_MASK,
2745 rec->flags & FTRACE_FL_REGS ? " R" : "");
2746 seq_printf(m, "\n");
2748 return 0;
2751 static const struct seq_operations show_ftrace_seq_ops = {
2752 .start = t_start,
2753 .next = t_next,
2754 .stop = t_stop,
2755 .show = t_show,
2758 static int
2759 ftrace_avail_open(struct inode *inode, struct file *file)
2761 struct ftrace_iterator *iter;
2763 if (unlikely(ftrace_disabled))
2764 return -ENODEV;
2766 iter = __seq_open_private(file, &show_ftrace_seq_ops, sizeof(*iter));
2767 if (iter) {
2768 iter->pg = ftrace_pages_start;
2769 iter->ops = &global_ops;
2772 return iter ? 0 : -ENOMEM;
2775 static int
2776 ftrace_enabled_open(struct inode *inode, struct file *file)
2778 struct ftrace_iterator *iter;
2780 if (unlikely(ftrace_disabled))
2781 return -ENODEV;
2783 iter = __seq_open_private(file, &show_ftrace_seq_ops, sizeof(*iter));
2784 if (iter) {
2785 iter->pg = ftrace_pages_start;
2786 iter->flags = FTRACE_ITER_ENABLED;
2787 iter->ops = &global_ops;
2790 return iter ? 0 : -ENOMEM;
2793 static void ftrace_filter_reset(struct ftrace_hash *hash)
2795 mutex_lock(&ftrace_lock);
2796 ftrace_hash_clear(hash);
2797 mutex_unlock(&ftrace_lock);
2801 * ftrace_regex_open - initialize function tracer filter files
2802 * @ops: The ftrace_ops that hold the hash filters
2803 * @flag: The type of filter to process
2804 * @inode: The inode, usually passed in to your open routine
2805 * @file: The file, usually passed in to your open routine
2807 * ftrace_regex_open() initializes the filter files for the
2808 * @ops. Depending on @flag it may process the filter hash or
2809 * the notrace hash of @ops. With this called from the open
2810 * routine, you can use ftrace_filter_write() for the write
2811 * routine if @flag has FTRACE_ITER_FILTER set, or
2812 * ftrace_notrace_write() if @flag has FTRACE_ITER_NOTRACE set.
2813 * ftrace_filter_lseek() should be used as the lseek routine, and
2814 * release must call ftrace_regex_release().
2817 ftrace_regex_open(struct ftrace_ops *ops, int flag,
2818 struct inode *inode, struct file *file)
2820 struct ftrace_iterator *iter;
2821 struct ftrace_hash *hash;
2822 int ret = 0;
2824 ftrace_ops_init(ops);
2826 if (unlikely(ftrace_disabled))
2827 return -ENODEV;
2829 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
2830 if (!iter)
2831 return -ENOMEM;
2833 if (trace_parser_get_init(&iter->parser, FTRACE_BUFF_MAX)) {
2834 kfree(iter);
2835 return -ENOMEM;
2838 iter->ops = ops;
2839 iter->flags = flag;
2841 mutex_lock(&ops->regex_lock);
2843 if (flag & FTRACE_ITER_NOTRACE)
2844 hash = ops->notrace_hash;
2845 else
2846 hash = ops->filter_hash;
2848 if (file->f_mode & FMODE_WRITE) {
2849 iter->hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, hash);
2850 if (!iter->hash) {
2851 trace_parser_put(&iter->parser);
2852 kfree(iter);
2853 ret = -ENOMEM;
2854 goto out_unlock;
2858 if ((file->f_mode & FMODE_WRITE) &&
2859 (file->f_flags & O_TRUNC))
2860 ftrace_filter_reset(iter->hash);
2862 if (file->f_mode & FMODE_READ) {
2863 iter->pg = ftrace_pages_start;
2865 ret = seq_open(file, &show_ftrace_seq_ops);
2866 if (!ret) {
2867 struct seq_file *m = file->private_data;
2868 m->private = iter;
2869 } else {
2870 /* Failed */
2871 free_ftrace_hash(iter->hash);
2872 trace_parser_put(&iter->parser);
2873 kfree(iter);
2875 } else
2876 file->private_data = iter;
2878 out_unlock:
2879 mutex_unlock(&ops->regex_lock);
2881 return ret;
2884 static int
2885 ftrace_filter_open(struct inode *inode, struct file *file)
2887 return ftrace_regex_open(&global_ops,
2888 FTRACE_ITER_FILTER | FTRACE_ITER_DO_HASH,
2889 inode, file);
2892 static int
2893 ftrace_notrace_open(struct inode *inode, struct file *file)
2895 return ftrace_regex_open(&global_ops, FTRACE_ITER_NOTRACE,
2896 inode, file);
2899 static int ftrace_match(char *str, char *regex, int len, int type)
2901 int matched = 0;
2902 int slen;
2904 switch (type) {
2905 case MATCH_FULL:
2906 if (strcmp(str, regex) == 0)
2907 matched = 1;
2908 break;
2909 case MATCH_FRONT_ONLY:
2910 if (strncmp(str, regex, len) == 0)
2911 matched = 1;
2912 break;
2913 case MATCH_MIDDLE_ONLY:
2914 if (strstr(str, regex))
2915 matched = 1;
2916 break;
2917 case MATCH_END_ONLY:
2918 slen = strlen(str);
2919 if (slen >= len && memcmp(str + slen - len, regex, len) == 0)
2920 matched = 1;
2921 break;
2924 return matched;
2927 static int
2928 enter_record(struct ftrace_hash *hash, struct dyn_ftrace *rec, int not)
2930 struct ftrace_func_entry *entry;
2931 int ret = 0;
2933 entry = ftrace_lookup_ip(hash, rec->ip);
2934 if (not) {
2935 /* Do nothing if it doesn't exist */
2936 if (!entry)
2937 return 0;
2939 free_hash_entry(hash, entry);
2940 } else {
2941 /* Do nothing if it exists */
2942 if (entry)
2943 return 0;
2945 ret = add_hash_entry(hash, rec->ip);
2947 return ret;
2950 static int
2951 ftrace_match_record(struct dyn_ftrace *rec, char *mod,
2952 char *regex, int len, int type)
2954 char str[KSYM_SYMBOL_LEN];
2955 char *modname;
2957 kallsyms_lookup(rec->ip, NULL, NULL, &modname, str);
2959 if (mod) {
2960 /* module lookup requires matching the module */
2961 if (!modname || strcmp(modname, mod))
2962 return 0;
2964 /* blank search means to match all funcs in the mod */
2965 if (!len)
2966 return 1;
2969 return ftrace_match(str, regex, len, type);
2972 static int
2973 match_records(struct ftrace_hash *hash, char *buff,
2974 int len, char *mod, int not)
2976 unsigned search_len = 0;
2977 struct ftrace_page *pg;
2978 struct dyn_ftrace *rec;
2979 int type = MATCH_FULL;
2980 char *search = buff;
2981 int found = 0;
2982 int ret;
2984 if (len) {
2985 type = filter_parse_regex(buff, len, &search, &not);
2986 search_len = strlen(search);
2989 mutex_lock(&ftrace_lock);
2991 if (unlikely(ftrace_disabled))
2992 goto out_unlock;
2994 do_for_each_ftrace_rec(pg, rec) {
2995 if (ftrace_match_record(rec, mod, search, search_len, type)) {
2996 ret = enter_record(hash, rec, not);
2997 if (ret < 0) {
2998 found = ret;
2999 goto out_unlock;
3001 found = 1;
3003 } while_for_each_ftrace_rec();
3004 out_unlock:
3005 mutex_unlock(&ftrace_lock);
3007 return found;
3010 static int
3011 ftrace_match_records(struct ftrace_hash *hash, char *buff, int len)
3013 return match_records(hash, buff, len, NULL, 0);
3016 static int
3017 ftrace_match_module_records(struct ftrace_hash *hash, char *buff, char *mod)
3019 int not = 0;
3021 /* blank or '*' mean the same */
3022 if (strcmp(buff, "*") == 0)
3023 buff[0] = 0;
3025 /* handle the case of 'dont filter this module' */
3026 if (strcmp(buff, "!") == 0 || strcmp(buff, "!*") == 0) {
3027 buff[0] = 0;
3028 not = 1;
3031 return match_records(hash, buff, strlen(buff), mod, not);
3035 * We register the module command as a template to show others how
3036 * to register the a command as well.
3039 static int
3040 ftrace_mod_callback(struct ftrace_hash *hash,
3041 char *func, char *cmd, char *param, int enable)
3043 char *mod;
3044 int ret = -EINVAL;
3047 * cmd == 'mod' because we only registered this func
3048 * for the 'mod' ftrace_func_command.
3049 * But if you register one func with multiple commands,
3050 * you can tell which command was used by the cmd
3051 * parameter.
3054 /* we must have a module name */
3055 if (!param)
3056 return ret;
3058 mod = strsep(&param, ":");
3059 if (!strlen(mod))
3060 return ret;
3062 ret = ftrace_match_module_records(hash, func, mod);
3063 if (!ret)
3064 ret = -EINVAL;
3065 if (ret < 0)
3066 return ret;
3068 return 0;
3071 static struct ftrace_func_command ftrace_mod_cmd = {
3072 .name = "mod",
3073 .func = ftrace_mod_callback,
3076 static int __init ftrace_mod_cmd_init(void)
3078 return register_ftrace_command(&ftrace_mod_cmd);
3080 core_initcall(ftrace_mod_cmd_init);
3082 static void function_trace_probe_call(unsigned long ip, unsigned long parent_ip,
3083 struct ftrace_ops *op, struct pt_regs *pt_regs)
3085 struct ftrace_func_probe *entry;
3086 struct hlist_head *hhd;
3087 unsigned long key;
3089 key = hash_long(ip, FTRACE_HASH_BITS);
3091 hhd = &ftrace_func_hash[key];
3093 if (hlist_empty(hhd))
3094 return;
3097 * Disable preemption for these calls to prevent a RCU grace
3098 * period. This syncs the hash iteration and freeing of items
3099 * on the hash. rcu_read_lock is too dangerous here.
3101 preempt_disable_notrace();
3102 hlist_for_each_entry_rcu_notrace(entry, hhd, node) {
3103 if (entry->ip == ip)
3104 entry->ops->func(ip, parent_ip, &entry->data);
3106 preempt_enable_notrace();
3109 static struct ftrace_ops trace_probe_ops __read_mostly =
3111 .func = function_trace_probe_call,
3112 .flags = FTRACE_OPS_FL_INITIALIZED,
3113 INIT_REGEX_LOCK(trace_probe_ops)
3116 static int ftrace_probe_registered;
3118 static void __enable_ftrace_function_probe(void)
3120 int ret;
3121 int i;
3123 if (ftrace_probe_registered) {
3124 /* still need to update the function call sites */
3125 if (ftrace_enabled)
3126 ftrace_run_update_code(FTRACE_UPDATE_CALLS);
3127 return;
3130 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
3131 struct hlist_head *hhd = &ftrace_func_hash[i];
3132 if (hhd->first)
3133 break;
3135 /* Nothing registered? */
3136 if (i == FTRACE_FUNC_HASHSIZE)
3137 return;
3139 ret = ftrace_startup(&trace_probe_ops, 0);
3141 ftrace_probe_registered = 1;
3144 static void __disable_ftrace_function_probe(void)
3146 int i;
3148 if (!ftrace_probe_registered)
3149 return;
3151 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
3152 struct hlist_head *hhd = &ftrace_func_hash[i];
3153 if (hhd->first)
3154 return;
3157 /* no more funcs left */
3158 ftrace_shutdown(&trace_probe_ops, 0);
3160 ftrace_probe_registered = 0;
3164 static void ftrace_free_entry(struct ftrace_func_probe *entry)
3166 if (entry->ops->free)
3167 entry->ops->free(entry->ops, entry->ip, &entry->data);
3168 kfree(entry);
3172 register_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
3173 void *data)
3175 struct ftrace_func_probe *entry;
3176 struct ftrace_hash **orig_hash = &trace_probe_ops.filter_hash;
3177 struct ftrace_hash *hash;
3178 struct ftrace_page *pg;
3179 struct dyn_ftrace *rec;
3180 int type, len, not;
3181 unsigned long key;
3182 int count = 0;
3183 char *search;
3184 int ret;
3186 type = filter_parse_regex(glob, strlen(glob), &search, &not);
3187 len = strlen(search);
3189 /* we do not support '!' for function probes */
3190 if (WARN_ON(not))
3191 return -EINVAL;
3193 mutex_lock(&trace_probe_ops.regex_lock);
3195 hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, *orig_hash);
3196 if (!hash) {
3197 count = -ENOMEM;
3198 goto out;
3201 if (unlikely(ftrace_disabled)) {
3202 count = -ENODEV;
3203 goto out;
3206 mutex_lock(&ftrace_lock);
3208 do_for_each_ftrace_rec(pg, rec) {
3210 if (!ftrace_match_record(rec, NULL, search, len, type))
3211 continue;
3213 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
3214 if (!entry) {
3215 /* If we did not process any, then return error */
3216 if (!count)
3217 count = -ENOMEM;
3218 goto out_unlock;
3221 count++;
3223 entry->data = data;
3226 * The caller might want to do something special
3227 * for each function we find. We call the callback
3228 * to give the caller an opportunity to do so.
3230 if (ops->init) {
3231 if (ops->init(ops, rec->ip, &entry->data) < 0) {
3232 /* caller does not like this func */
3233 kfree(entry);
3234 continue;
3238 ret = enter_record(hash, rec, 0);
3239 if (ret < 0) {
3240 kfree(entry);
3241 count = ret;
3242 goto out_unlock;
3245 entry->ops = ops;
3246 entry->ip = rec->ip;
3248 key = hash_long(entry->ip, FTRACE_HASH_BITS);
3249 hlist_add_head_rcu(&entry->node, &ftrace_func_hash[key]);
3251 } while_for_each_ftrace_rec();
3253 ret = ftrace_hash_move(&trace_probe_ops, 1, orig_hash, hash);
3254 if (ret < 0)
3255 count = ret;
3257 __enable_ftrace_function_probe();
3259 out_unlock:
3260 mutex_unlock(&ftrace_lock);
3261 out:
3262 mutex_unlock(&trace_probe_ops.regex_lock);
3263 free_ftrace_hash(hash);
3265 return count;
3268 enum {
3269 PROBE_TEST_FUNC = 1,
3270 PROBE_TEST_DATA = 2
3273 static void
3274 __unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
3275 void *data, int flags)
3277 struct ftrace_func_entry *rec_entry;
3278 struct ftrace_func_probe *entry;
3279 struct ftrace_func_probe *p;
3280 struct ftrace_hash **orig_hash = &trace_probe_ops.filter_hash;
3281 struct list_head free_list;
3282 struct ftrace_hash *hash;
3283 struct hlist_node *tmp;
3284 char str[KSYM_SYMBOL_LEN];
3285 int type = MATCH_FULL;
3286 int i, len = 0;
3287 char *search;
3289 if (glob && (strcmp(glob, "*") == 0 || !strlen(glob)))
3290 glob = NULL;
3291 else if (glob) {
3292 int not;
3294 type = filter_parse_regex(glob, strlen(glob), &search, &not);
3295 len = strlen(search);
3297 /* we do not support '!' for function probes */
3298 if (WARN_ON(not))
3299 return;
3302 mutex_lock(&trace_probe_ops.regex_lock);
3304 hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, *orig_hash);
3305 if (!hash)
3306 /* Hmm, should report this somehow */
3307 goto out_unlock;
3309 INIT_LIST_HEAD(&free_list);
3311 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
3312 struct hlist_head *hhd = &ftrace_func_hash[i];
3314 hlist_for_each_entry_safe(entry, tmp, hhd, node) {
3316 /* break up if statements for readability */
3317 if ((flags & PROBE_TEST_FUNC) && entry->ops != ops)
3318 continue;
3320 if ((flags & PROBE_TEST_DATA) && entry->data != data)
3321 continue;
3323 /* do this last, since it is the most expensive */
3324 if (glob) {
3325 kallsyms_lookup(entry->ip, NULL, NULL,
3326 NULL, str);
3327 if (!ftrace_match(str, glob, len, type))
3328 continue;
3331 rec_entry = ftrace_lookup_ip(hash, entry->ip);
3332 /* It is possible more than one entry had this ip */
3333 if (rec_entry)
3334 free_hash_entry(hash, rec_entry);
3336 hlist_del_rcu(&entry->node);
3337 list_add(&entry->free_list, &free_list);
3340 mutex_lock(&ftrace_lock);
3341 __disable_ftrace_function_probe();
3343 * Remove after the disable is called. Otherwise, if the last
3344 * probe is removed, a null hash means *all enabled*.
3346 ftrace_hash_move(&trace_probe_ops, 1, orig_hash, hash);
3347 synchronize_sched();
3348 list_for_each_entry_safe(entry, p, &free_list, free_list) {
3349 list_del(&entry->free_list);
3350 ftrace_free_entry(entry);
3352 mutex_unlock(&ftrace_lock);
3354 out_unlock:
3355 mutex_unlock(&trace_probe_ops.regex_lock);
3356 free_ftrace_hash(hash);
3359 void
3360 unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
3361 void *data)
3363 __unregister_ftrace_function_probe(glob, ops, data,
3364 PROBE_TEST_FUNC | PROBE_TEST_DATA);
3367 void
3368 unregister_ftrace_function_probe_func(char *glob, struct ftrace_probe_ops *ops)
3370 __unregister_ftrace_function_probe(glob, ops, NULL, PROBE_TEST_FUNC);
3373 void unregister_ftrace_function_probe_all(char *glob)
3375 __unregister_ftrace_function_probe(glob, NULL, NULL, 0);
3378 static LIST_HEAD(ftrace_commands);
3379 static DEFINE_MUTEX(ftrace_cmd_mutex);
3381 int register_ftrace_command(struct ftrace_func_command *cmd)
3383 struct ftrace_func_command *p;
3384 int ret = 0;
3386 mutex_lock(&ftrace_cmd_mutex);
3387 list_for_each_entry(p, &ftrace_commands, list) {
3388 if (strcmp(cmd->name, p->name) == 0) {
3389 ret = -EBUSY;
3390 goto out_unlock;
3393 list_add(&cmd->list, &ftrace_commands);
3394 out_unlock:
3395 mutex_unlock(&ftrace_cmd_mutex);
3397 return ret;
3400 int unregister_ftrace_command(struct ftrace_func_command *cmd)
3402 struct ftrace_func_command *p, *n;
3403 int ret = -ENODEV;
3405 mutex_lock(&ftrace_cmd_mutex);
3406 list_for_each_entry_safe(p, n, &ftrace_commands, list) {
3407 if (strcmp(cmd->name, p->name) == 0) {
3408 ret = 0;
3409 list_del_init(&p->list);
3410 goto out_unlock;
3413 out_unlock:
3414 mutex_unlock(&ftrace_cmd_mutex);
3416 return ret;
3419 static int ftrace_process_regex(struct ftrace_hash *hash,
3420 char *buff, int len, int enable)
3422 char *func, *command, *next = buff;
3423 struct ftrace_func_command *p;
3424 int ret = -EINVAL;
3426 func = strsep(&next, ":");
3428 if (!next) {
3429 ret = ftrace_match_records(hash, func, len);
3430 if (!ret)
3431 ret = -EINVAL;
3432 if (ret < 0)
3433 return ret;
3434 return 0;
3437 /* command found */
3439 command = strsep(&next, ":");
3441 mutex_lock(&ftrace_cmd_mutex);
3442 list_for_each_entry(p, &ftrace_commands, list) {
3443 if (strcmp(p->name, command) == 0) {
3444 ret = p->func(hash, func, command, next, enable);
3445 goto out_unlock;
3448 out_unlock:
3449 mutex_unlock(&ftrace_cmd_mutex);
3451 return ret;
3454 static ssize_t
3455 ftrace_regex_write(struct file *file, const char __user *ubuf,
3456 size_t cnt, loff_t *ppos, int enable)
3458 struct ftrace_iterator *iter;
3459 struct trace_parser *parser;
3460 ssize_t ret, read;
3462 if (!cnt)
3463 return 0;
3465 if (file->f_mode & FMODE_READ) {
3466 struct seq_file *m = file->private_data;
3467 iter = m->private;
3468 } else
3469 iter = file->private_data;
3471 if (unlikely(ftrace_disabled))
3472 return -ENODEV;
3474 /* iter->hash is a local copy, so we don't need regex_lock */
3476 parser = &iter->parser;
3477 read = trace_get_user(parser, ubuf, cnt, ppos);
3479 if (read >= 0 && trace_parser_loaded(parser) &&
3480 !trace_parser_cont(parser)) {
3481 ret = ftrace_process_regex(iter->hash, parser->buffer,
3482 parser->idx, enable);
3483 trace_parser_clear(parser);
3484 if (ret < 0)
3485 goto out;
3488 ret = read;
3489 out:
3490 return ret;
3493 ssize_t
3494 ftrace_filter_write(struct file *file, const char __user *ubuf,
3495 size_t cnt, loff_t *ppos)
3497 return ftrace_regex_write(file, ubuf, cnt, ppos, 1);
3500 ssize_t
3501 ftrace_notrace_write(struct file *file, const char __user *ubuf,
3502 size_t cnt, loff_t *ppos)
3504 return ftrace_regex_write(file, ubuf, cnt, ppos, 0);
3507 static int
3508 ftrace_match_addr(struct ftrace_hash *hash, unsigned long ip, int remove)
3510 struct ftrace_func_entry *entry;
3512 if (!ftrace_location(ip))
3513 return -EINVAL;
3515 if (remove) {
3516 entry = ftrace_lookup_ip(hash, ip);
3517 if (!entry)
3518 return -ENOENT;
3519 free_hash_entry(hash, entry);
3520 return 0;
3523 return add_hash_entry(hash, ip);
3526 static void ftrace_ops_update_code(struct ftrace_ops *ops)
3528 if (ops->flags & FTRACE_OPS_FL_ENABLED && ftrace_enabled)
3529 ftrace_run_update_code(FTRACE_UPDATE_CALLS);
3532 static int
3533 ftrace_set_hash(struct ftrace_ops *ops, unsigned char *buf, int len,
3534 unsigned long ip, int remove, int reset, int enable)
3536 struct ftrace_hash **orig_hash;
3537 struct ftrace_hash *hash;
3538 int ret;
3540 /* All global ops uses the global ops filters */
3541 if (ops->flags & FTRACE_OPS_FL_GLOBAL)
3542 ops = &global_ops;
3544 if (unlikely(ftrace_disabled))
3545 return -ENODEV;
3547 mutex_lock(&ops->regex_lock);
3549 if (enable)
3550 orig_hash = &ops->filter_hash;
3551 else
3552 orig_hash = &ops->notrace_hash;
3554 hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, *orig_hash);
3555 if (!hash) {
3556 ret = -ENOMEM;
3557 goto out_regex_unlock;
3560 if (reset)
3561 ftrace_filter_reset(hash);
3562 if (buf && !ftrace_match_records(hash, buf, len)) {
3563 ret = -EINVAL;
3564 goto out_regex_unlock;
3566 if (ip) {
3567 ret = ftrace_match_addr(hash, ip, remove);
3568 if (ret < 0)
3569 goto out_regex_unlock;
3572 mutex_lock(&ftrace_lock);
3573 ret = ftrace_hash_move(ops, enable, orig_hash, hash);
3574 if (!ret)
3575 ftrace_ops_update_code(ops);
3577 mutex_unlock(&ftrace_lock);
3579 out_regex_unlock:
3580 mutex_unlock(&ops->regex_lock);
3582 free_ftrace_hash(hash);
3583 return ret;
3586 static int
3587 ftrace_set_addr(struct ftrace_ops *ops, unsigned long ip, int remove,
3588 int reset, int enable)
3590 return ftrace_set_hash(ops, 0, 0, ip, remove, reset, enable);
3594 * ftrace_set_filter_ip - set a function to filter on in ftrace by address
3595 * @ops - the ops to set the filter with
3596 * @ip - the address to add to or remove from the filter.
3597 * @remove - non zero to remove the ip from the filter
3598 * @reset - non zero to reset all filters before applying this filter.
3600 * Filters denote which functions should be enabled when tracing is enabled
3601 * If @ip is NULL, it failes to update filter.
3603 int ftrace_set_filter_ip(struct ftrace_ops *ops, unsigned long ip,
3604 int remove, int reset)
3606 ftrace_ops_init(ops);
3607 return ftrace_set_addr(ops, ip, remove, reset, 1);
3609 EXPORT_SYMBOL_GPL(ftrace_set_filter_ip);
3611 static int
3612 ftrace_set_regex(struct ftrace_ops *ops, unsigned char *buf, int len,
3613 int reset, int enable)
3615 return ftrace_set_hash(ops, buf, len, 0, 0, reset, enable);
3619 * ftrace_set_filter - set a function to filter on in ftrace
3620 * @ops - the ops to set the filter with
3621 * @buf - the string that holds the function filter text.
3622 * @len - the length of the string.
3623 * @reset - non zero to reset all filters before applying this filter.
3625 * Filters denote which functions should be enabled when tracing is enabled.
3626 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
3628 int ftrace_set_filter(struct ftrace_ops *ops, unsigned char *buf,
3629 int len, int reset)
3631 ftrace_ops_init(ops);
3632 return ftrace_set_regex(ops, buf, len, reset, 1);
3634 EXPORT_SYMBOL_GPL(ftrace_set_filter);
3637 * ftrace_set_notrace - set a function to not trace in ftrace
3638 * @ops - the ops to set the notrace filter with
3639 * @buf - the string that holds the function notrace text.
3640 * @len - the length of the string.
3641 * @reset - non zero to reset all filters before applying this filter.
3643 * Notrace Filters denote which functions should not be enabled when tracing
3644 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
3645 * for tracing.
3647 int ftrace_set_notrace(struct ftrace_ops *ops, unsigned char *buf,
3648 int len, int reset)
3650 ftrace_ops_init(ops);
3651 return ftrace_set_regex(ops, buf, len, reset, 0);
3653 EXPORT_SYMBOL_GPL(ftrace_set_notrace);
3655 * ftrace_set_filter - set a function to filter on in ftrace
3656 * @ops - the ops to set the filter with
3657 * @buf - the string that holds the function filter text.
3658 * @len - the length of the string.
3659 * @reset - non zero to reset all filters before applying this filter.
3661 * Filters denote which functions should be enabled when tracing is enabled.
3662 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
3664 void ftrace_set_global_filter(unsigned char *buf, int len, int reset)
3666 ftrace_set_regex(&global_ops, buf, len, reset, 1);
3668 EXPORT_SYMBOL_GPL(ftrace_set_global_filter);
3671 * ftrace_set_notrace - set a function to not trace in ftrace
3672 * @ops - the ops to set the notrace filter with
3673 * @buf - the string that holds the function notrace text.
3674 * @len - the length of the string.
3675 * @reset - non zero to reset all filters before applying this filter.
3677 * Notrace Filters denote which functions should not be enabled when tracing
3678 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
3679 * for tracing.
3681 void ftrace_set_global_notrace(unsigned char *buf, int len, int reset)
3683 ftrace_set_regex(&global_ops, buf, len, reset, 0);
3685 EXPORT_SYMBOL_GPL(ftrace_set_global_notrace);
3688 * command line interface to allow users to set filters on boot up.
3690 #define FTRACE_FILTER_SIZE COMMAND_LINE_SIZE
3691 static char ftrace_notrace_buf[FTRACE_FILTER_SIZE] __initdata;
3692 static char ftrace_filter_buf[FTRACE_FILTER_SIZE] __initdata;
3694 /* Used by function selftest to not test if filter is set */
3695 bool ftrace_filter_param __initdata;
3697 static int __init set_ftrace_notrace(char *str)
3699 ftrace_filter_param = true;
3700 strlcpy(ftrace_notrace_buf, str, FTRACE_FILTER_SIZE);
3701 return 1;
3703 __setup("ftrace_notrace=", set_ftrace_notrace);
3705 static int __init set_ftrace_filter(char *str)
3707 ftrace_filter_param = true;
3708 strlcpy(ftrace_filter_buf, str, FTRACE_FILTER_SIZE);
3709 return 1;
3711 __setup("ftrace_filter=", set_ftrace_filter);
3713 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
3714 static char ftrace_graph_buf[FTRACE_FILTER_SIZE] __initdata;
3715 static int ftrace_set_func(unsigned long *array, int *idx, char *buffer);
3717 static int __init set_graph_function(char *str)
3719 strlcpy(ftrace_graph_buf, str, FTRACE_FILTER_SIZE);
3720 return 1;
3722 __setup("ftrace_graph_filter=", set_graph_function);
3724 static void __init set_ftrace_early_graph(char *buf)
3726 int ret;
3727 char *func;
3729 while (buf) {
3730 func = strsep(&buf, ",");
3731 /* we allow only one expression at a time */
3732 ret = ftrace_set_func(ftrace_graph_funcs, &ftrace_graph_count,
3733 func);
3734 if (ret)
3735 printk(KERN_DEBUG "ftrace: function %s not "
3736 "traceable\n", func);
3739 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
3741 void __init
3742 ftrace_set_early_filter(struct ftrace_ops *ops, char *buf, int enable)
3744 char *func;
3746 ftrace_ops_init(ops);
3748 while (buf) {
3749 func = strsep(&buf, ",");
3750 ftrace_set_regex(ops, func, strlen(func), 0, enable);
3754 static void __init set_ftrace_early_filters(void)
3756 if (ftrace_filter_buf[0])
3757 ftrace_set_early_filter(&global_ops, ftrace_filter_buf, 1);
3758 if (ftrace_notrace_buf[0])
3759 ftrace_set_early_filter(&global_ops, ftrace_notrace_buf, 0);
3760 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
3761 if (ftrace_graph_buf[0])
3762 set_ftrace_early_graph(ftrace_graph_buf);
3763 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
3766 int ftrace_regex_release(struct inode *inode, struct file *file)
3768 struct seq_file *m = (struct seq_file *)file->private_data;
3769 struct ftrace_iterator *iter;
3770 struct ftrace_hash **orig_hash;
3771 struct trace_parser *parser;
3772 int filter_hash;
3773 int ret;
3775 if (file->f_mode & FMODE_READ) {
3776 iter = m->private;
3777 seq_release(inode, file);
3778 } else
3779 iter = file->private_data;
3781 parser = &iter->parser;
3782 if (trace_parser_loaded(parser)) {
3783 parser->buffer[parser->idx] = 0;
3784 ftrace_match_records(iter->hash, parser->buffer, parser->idx);
3787 trace_parser_put(parser);
3789 mutex_lock(&iter->ops->regex_lock);
3791 if (file->f_mode & FMODE_WRITE) {
3792 filter_hash = !!(iter->flags & FTRACE_ITER_FILTER);
3794 if (filter_hash)
3795 orig_hash = &iter->ops->filter_hash;
3796 else
3797 orig_hash = &iter->ops->notrace_hash;
3799 mutex_lock(&ftrace_lock);
3800 ret = ftrace_hash_move(iter->ops, filter_hash,
3801 orig_hash, iter->hash);
3802 if (!ret)
3803 ftrace_ops_update_code(iter->ops);
3805 mutex_unlock(&ftrace_lock);
3808 mutex_unlock(&iter->ops->regex_lock);
3809 free_ftrace_hash(iter->hash);
3810 kfree(iter);
3812 return 0;
3815 static const struct file_operations ftrace_avail_fops = {
3816 .open = ftrace_avail_open,
3817 .read = seq_read,
3818 .llseek = seq_lseek,
3819 .release = seq_release_private,
3822 static const struct file_operations ftrace_enabled_fops = {
3823 .open = ftrace_enabled_open,
3824 .read = seq_read,
3825 .llseek = seq_lseek,
3826 .release = seq_release_private,
3829 static const struct file_operations ftrace_filter_fops = {
3830 .open = ftrace_filter_open,
3831 .read = seq_read,
3832 .write = ftrace_filter_write,
3833 .llseek = ftrace_filter_lseek,
3834 .release = ftrace_regex_release,
3837 static const struct file_operations ftrace_notrace_fops = {
3838 .open = ftrace_notrace_open,
3839 .read = seq_read,
3840 .write = ftrace_notrace_write,
3841 .llseek = ftrace_filter_lseek,
3842 .release = ftrace_regex_release,
3845 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
3847 static DEFINE_MUTEX(graph_lock);
3849 int ftrace_graph_count;
3850 int ftrace_graph_filter_enabled;
3851 unsigned long ftrace_graph_funcs[FTRACE_GRAPH_MAX_FUNCS] __read_mostly;
3853 static void *
3854 __g_next(struct seq_file *m, loff_t *pos)
3856 if (*pos >= ftrace_graph_count)
3857 return NULL;
3858 return &ftrace_graph_funcs[*pos];
3861 static void *
3862 g_next(struct seq_file *m, void *v, loff_t *pos)
3864 (*pos)++;
3865 return __g_next(m, pos);
3868 static void *g_start(struct seq_file *m, loff_t *pos)
3870 mutex_lock(&graph_lock);
3872 /* Nothing, tell g_show to print all functions are enabled */
3873 if (!ftrace_graph_filter_enabled && !*pos)
3874 return (void *)1;
3876 return __g_next(m, pos);
3879 static void g_stop(struct seq_file *m, void *p)
3881 mutex_unlock(&graph_lock);
3884 static int g_show(struct seq_file *m, void *v)
3886 unsigned long *ptr = v;
3888 if (!ptr)
3889 return 0;
3891 if (ptr == (unsigned long *)1) {
3892 seq_printf(m, "#### all functions enabled ####\n");
3893 return 0;
3896 seq_printf(m, "%ps\n", (void *)*ptr);
3898 return 0;
3901 static const struct seq_operations ftrace_graph_seq_ops = {
3902 .start = g_start,
3903 .next = g_next,
3904 .stop = g_stop,
3905 .show = g_show,
3908 static int
3909 ftrace_graph_open(struct inode *inode, struct file *file)
3911 int ret = 0;
3913 if (unlikely(ftrace_disabled))
3914 return -ENODEV;
3916 mutex_lock(&graph_lock);
3917 if ((file->f_mode & FMODE_WRITE) &&
3918 (file->f_flags & O_TRUNC)) {
3919 ftrace_graph_filter_enabled = 0;
3920 ftrace_graph_count = 0;
3921 memset(ftrace_graph_funcs, 0, sizeof(ftrace_graph_funcs));
3923 mutex_unlock(&graph_lock);
3925 if (file->f_mode & FMODE_READ)
3926 ret = seq_open(file, &ftrace_graph_seq_ops);
3928 return ret;
3931 static int
3932 ftrace_graph_release(struct inode *inode, struct file *file)
3934 if (file->f_mode & FMODE_READ)
3935 seq_release(inode, file);
3936 return 0;
3939 static int
3940 ftrace_set_func(unsigned long *array, int *idx, char *buffer)
3942 struct dyn_ftrace *rec;
3943 struct ftrace_page *pg;
3944 int search_len;
3945 int fail = 1;
3946 int type, not;
3947 char *search;
3948 bool exists;
3949 int i;
3951 /* decode regex */
3952 type = filter_parse_regex(buffer, strlen(buffer), &search, &not);
3953 if (!not && *idx >= FTRACE_GRAPH_MAX_FUNCS)
3954 return -EBUSY;
3956 search_len = strlen(search);
3958 mutex_lock(&ftrace_lock);
3960 if (unlikely(ftrace_disabled)) {
3961 mutex_unlock(&ftrace_lock);
3962 return -ENODEV;
3965 do_for_each_ftrace_rec(pg, rec) {
3967 if (ftrace_match_record(rec, NULL, search, search_len, type)) {
3968 /* if it is in the array */
3969 exists = false;
3970 for (i = 0; i < *idx; i++) {
3971 if (array[i] == rec->ip) {
3972 exists = true;
3973 break;
3977 if (!not) {
3978 fail = 0;
3979 if (!exists) {
3980 array[(*idx)++] = rec->ip;
3981 if (*idx >= FTRACE_GRAPH_MAX_FUNCS)
3982 goto out;
3984 } else {
3985 if (exists) {
3986 array[i] = array[--(*idx)];
3987 array[*idx] = 0;
3988 fail = 0;
3992 } while_for_each_ftrace_rec();
3993 out:
3994 mutex_unlock(&ftrace_lock);
3996 if (fail)
3997 return -EINVAL;
3999 ftrace_graph_filter_enabled = !!(*idx);
4001 return 0;
4004 static ssize_t
4005 ftrace_graph_write(struct file *file, const char __user *ubuf,
4006 size_t cnt, loff_t *ppos)
4008 struct trace_parser parser;
4009 ssize_t read, ret;
4011 if (!cnt)
4012 return 0;
4014 mutex_lock(&graph_lock);
4016 if (trace_parser_get_init(&parser, FTRACE_BUFF_MAX)) {
4017 ret = -ENOMEM;
4018 goto out_unlock;
4021 read = trace_get_user(&parser, ubuf, cnt, ppos);
4023 if (read >= 0 && trace_parser_loaded((&parser))) {
4024 parser.buffer[parser.idx] = 0;
4026 /* we allow only one expression at a time */
4027 ret = ftrace_set_func(ftrace_graph_funcs, &ftrace_graph_count,
4028 parser.buffer);
4029 if (ret)
4030 goto out_free;
4033 ret = read;
4035 out_free:
4036 trace_parser_put(&parser);
4037 out_unlock:
4038 mutex_unlock(&graph_lock);
4040 return ret;
4043 static const struct file_operations ftrace_graph_fops = {
4044 .open = ftrace_graph_open,
4045 .read = seq_read,
4046 .write = ftrace_graph_write,
4047 .llseek = ftrace_filter_lseek,
4048 .release = ftrace_graph_release,
4050 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
4052 static __init int ftrace_init_dyn_debugfs(struct dentry *d_tracer)
4055 trace_create_file("available_filter_functions", 0444,
4056 d_tracer, NULL, &ftrace_avail_fops);
4058 trace_create_file("enabled_functions", 0444,
4059 d_tracer, NULL, &ftrace_enabled_fops);
4061 trace_create_file("set_ftrace_filter", 0644, d_tracer,
4062 NULL, &ftrace_filter_fops);
4064 trace_create_file("set_ftrace_notrace", 0644, d_tracer,
4065 NULL, &ftrace_notrace_fops);
4067 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
4068 trace_create_file("set_graph_function", 0444, d_tracer,
4069 NULL,
4070 &ftrace_graph_fops);
4071 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
4073 return 0;
4076 static int ftrace_cmp_ips(const void *a, const void *b)
4078 const unsigned long *ipa = a;
4079 const unsigned long *ipb = b;
4081 if (*ipa > *ipb)
4082 return 1;
4083 if (*ipa < *ipb)
4084 return -1;
4085 return 0;
4088 static void ftrace_swap_ips(void *a, void *b, int size)
4090 unsigned long *ipa = a;
4091 unsigned long *ipb = b;
4092 unsigned long t;
4094 t = *ipa;
4095 *ipa = *ipb;
4096 *ipb = t;
4099 static int ftrace_process_locs(struct module *mod,
4100 unsigned long *start,
4101 unsigned long *end)
4103 struct ftrace_page *start_pg;
4104 struct ftrace_page *pg;
4105 struct dyn_ftrace *rec;
4106 unsigned long count;
4107 unsigned long *p;
4108 unsigned long addr;
4109 unsigned long flags = 0; /* Shut up gcc */
4110 int ret = -ENOMEM;
4112 count = end - start;
4114 if (!count)
4115 return 0;
4117 sort(start, count, sizeof(*start),
4118 ftrace_cmp_ips, ftrace_swap_ips);
4120 start_pg = ftrace_allocate_pages(count);
4121 if (!start_pg)
4122 return -ENOMEM;
4124 mutex_lock(&ftrace_lock);
4127 * Core and each module needs their own pages, as
4128 * modules will free them when they are removed.
4129 * Force a new page to be allocated for modules.
4131 if (!mod) {
4132 WARN_ON(ftrace_pages || ftrace_pages_start);
4133 /* First initialization */
4134 ftrace_pages = ftrace_pages_start = start_pg;
4135 } else {
4136 if (!ftrace_pages)
4137 goto out;
4139 if (WARN_ON(ftrace_pages->next)) {
4140 /* Hmm, we have free pages? */
4141 while (ftrace_pages->next)
4142 ftrace_pages = ftrace_pages->next;
4145 ftrace_pages->next = start_pg;
4148 p = start;
4149 pg = start_pg;
4150 while (p < end) {
4151 addr = ftrace_call_adjust(*p++);
4153 * Some architecture linkers will pad between
4154 * the different mcount_loc sections of different
4155 * object files to satisfy alignments.
4156 * Skip any NULL pointers.
4158 if (!addr)
4159 continue;
4161 if (pg->index == pg->size) {
4162 /* We should have allocated enough */
4163 if (WARN_ON(!pg->next))
4164 break;
4165 pg = pg->next;
4168 rec = &pg->records[pg->index++];
4169 rec->ip = addr;
4172 /* We should have used all pages */
4173 WARN_ON(pg->next);
4175 /* Assign the last page to ftrace_pages */
4176 ftrace_pages = pg;
4178 /* These new locations need to be initialized */
4179 ftrace_new_pgs = start_pg;
4182 * We only need to disable interrupts on start up
4183 * because we are modifying code that an interrupt
4184 * may execute, and the modification is not atomic.
4185 * But for modules, nothing runs the code we modify
4186 * until we are finished with it, and there's no
4187 * reason to cause large interrupt latencies while we do it.
4189 if (!mod)
4190 local_irq_save(flags);
4191 ftrace_update_code(mod);
4192 if (!mod)
4193 local_irq_restore(flags);
4194 ret = 0;
4195 out:
4196 mutex_unlock(&ftrace_lock);
4198 return ret;
4201 #ifdef CONFIG_MODULES
4203 #define next_to_ftrace_page(p) container_of(p, struct ftrace_page, next)
4205 void ftrace_release_mod(struct module *mod)
4207 struct dyn_ftrace *rec;
4208 struct ftrace_page **last_pg;
4209 struct ftrace_page *pg;
4210 int order;
4212 mutex_lock(&ftrace_lock);
4214 if (ftrace_disabled)
4215 goto out_unlock;
4218 * Each module has its own ftrace_pages, remove
4219 * them from the list.
4221 last_pg = &ftrace_pages_start;
4222 for (pg = ftrace_pages_start; pg; pg = *last_pg) {
4223 rec = &pg->records[0];
4224 if (within_module_core(rec->ip, mod)) {
4226 * As core pages are first, the first
4227 * page should never be a module page.
4229 if (WARN_ON(pg == ftrace_pages_start))
4230 goto out_unlock;
4232 /* Check if we are deleting the last page */
4233 if (pg == ftrace_pages)
4234 ftrace_pages = next_to_ftrace_page(last_pg);
4236 *last_pg = pg->next;
4237 order = get_count_order(pg->size / ENTRIES_PER_PAGE);
4238 free_pages((unsigned long)pg->records, order);
4239 kfree(pg);
4240 } else
4241 last_pg = &pg->next;
4243 out_unlock:
4244 mutex_unlock(&ftrace_lock);
4247 static void ftrace_init_module(struct module *mod,
4248 unsigned long *start, unsigned long *end)
4250 if (ftrace_disabled || start == end)
4251 return;
4252 ftrace_process_locs(mod, start, end);
4255 void ftrace_module_init(struct module *mod)
4257 ftrace_init_module(mod, mod->ftrace_callsites,
4258 mod->ftrace_callsites +
4259 mod->num_ftrace_callsites);
4262 static int ftrace_module_notify_exit(struct notifier_block *self,
4263 unsigned long val, void *data)
4265 struct module *mod = data;
4267 if (val == MODULE_STATE_GOING)
4268 ftrace_release_mod(mod);
4270 return 0;
4272 #else
4273 static int ftrace_module_notify_exit(struct notifier_block *self,
4274 unsigned long val, void *data)
4276 return 0;
4278 #endif /* CONFIG_MODULES */
4280 struct notifier_block ftrace_module_exit_nb = {
4281 .notifier_call = ftrace_module_notify_exit,
4282 .priority = INT_MIN, /* Run after anything that can remove kprobes */
4285 extern unsigned long __start_mcount_loc[];
4286 extern unsigned long __stop_mcount_loc[];
4288 void __init ftrace_init(void)
4290 unsigned long count, addr, flags;
4291 int ret;
4293 /* Keep the ftrace pointer to the stub */
4294 addr = (unsigned long)ftrace_stub;
4296 local_irq_save(flags);
4297 ftrace_dyn_arch_init(&addr);
4298 local_irq_restore(flags);
4300 /* ftrace_dyn_arch_init places the return code in addr */
4301 if (addr)
4302 goto failed;
4304 count = __stop_mcount_loc - __start_mcount_loc;
4306 ret = ftrace_dyn_table_alloc(count);
4307 if (ret)
4308 goto failed;
4310 last_ftrace_enabled = ftrace_enabled = 1;
4312 ret = ftrace_process_locs(NULL,
4313 __start_mcount_loc,
4314 __stop_mcount_loc);
4316 ret = register_module_notifier(&ftrace_module_exit_nb);
4317 if (ret)
4318 pr_warning("Failed to register trace ftrace module exit notifier\n");
4320 set_ftrace_early_filters();
4322 return;
4323 failed:
4324 ftrace_disabled = 1;
4327 #else
4329 static struct ftrace_ops global_ops = {
4330 .func = ftrace_stub,
4331 .flags = FTRACE_OPS_FL_RECURSION_SAFE | FTRACE_OPS_FL_INITIALIZED,
4332 INIT_REGEX_LOCK(global_ops)
4335 static int __init ftrace_nodyn_init(void)
4337 ftrace_enabled = 1;
4338 return 0;
4340 core_initcall(ftrace_nodyn_init);
4342 static inline int ftrace_init_dyn_debugfs(struct dentry *d_tracer) { return 0; }
4343 static inline void ftrace_startup_enable(int command) { }
4344 /* Keep as macros so we do not need to define the commands */
4345 # define ftrace_startup(ops, command) \
4346 ({ \
4347 int ___ret = __register_ftrace_function(ops); \
4348 if (!___ret) \
4349 (ops)->flags |= FTRACE_OPS_FL_ENABLED; \
4350 ___ret; \
4352 # define ftrace_shutdown(ops, command) __unregister_ftrace_function(ops)
4354 # define ftrace_startup_sysctl() do { } while (0)
4355 # define ftrace_shutdown_sysctl() do { } while (0)
4357 static inline int
4358 ftrace_ops_test(struct ftrace_ops *ops, unsigned long ip, void *regs)
4360 return 1;
4363 #endif /* CONFIG_DYNAMIC_FTRACE */
4365 static void
4366 ftrace_ops_control_func(unsigned long ip, unsigned long parent_ip,
4367 struct ftrace_ops *op, struct pt_regs *regs)
4369 if (unlikely(trace_recursion_test(TRACE_CONTROL_BIT)))
4370 return;
4373 * Some of the ops may be dynamically allocated,
4374 * they must be freed after a synchronize_sched().
4376 preempt_disable_notrace();
4377 trace_recursion_set(TRACE_CONTROL_BIT);
4378 do_for_each_ftrace_op(op, ftrace_control_list) {
4379 if (!(op->flags & FTRACE_OPS_FL_STUB) &&
4380 !ftrace_function_local_disabled(op) &&
4381 ftrace_ops_test(op, ip, regs))
4382 op->func(ip, parent_ip, op, regs);
4383 } while_for_each_ftrace_op(op);
4384 trace_recursion_clear(TRACE_CONTROL_BIT);
4385 preempt_enable_notrace();
4388 static struct ftrace_ops control_ops = {
4389 .func = ftrace_ops_control_func,
4390 .flags = FTRACE_OPS_FL_RECURSION_SAFE | FTRACE_OPS_FL_INITIALIZED,
4391 INIT_REGEX_LOCK(control_ops)
4394 static inline void
4395 __ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip,
4396 struct ftrace_ops *ignored, struct pt_regs *regs)
4398 struct ftrace_ops *op;
4399 int bit;
4401 if (function_trace_stop)
4402 return;
4404 bit = trace_test_and_set_recursion(TRACE_LIST_START, TRACE_LIST_MAX);
4405 if (bit < 0)
4406 return;
4409 * Some of the ops may be dynamically allocated,
4410 * they must be freed after a synchronize_sched().
4412 preempt_disable_notrace();
4413 do_for_each_ftrace_op(op, ftrace_ops_list) {
4414 if (ftrace_ops_test(op, ip, regs))
4415 op->func(ip, parent_ip, op, regs);
4416 } while_for_each_ftrace_op(op);
4417 preempt_enable_notrace();
4418 trace_clear_recursion(bit);
4422 * Some archs only support passing ip and parent_ip. Even though
4423 * the list function ignores the op parameter, we do not want any
4424 * C side effects, where a function is called without the caller
4425 * sending a third parameter.
4426 * Archs are to support both the regs and ftrace_ops at the same time.
4427 * If they support ftrace_ops, it is assumed they support regs.
4428 * If call backs want to use regs, they must either check for regs
4429 * being NULL, or CONFIG_DYNAMIC_FTRACE_WITH_REGS.
4430 * Note, CONFIG_DYNAMIC_FTRACE_WITH_REGS expects a full regs to be saved.
4431 * An architecture can pass partial regs with ftrace_ops and still
4432 * set the ARCH_SUPPORT_FTARCE_OPS.
4434 #if ARCH_SUPPORTS_FTRACE_OPS
4435 static void ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip,
4436 struct ftrace_ops *op, struct pt_regs *regs)
4438 __ftrace_ops_list_func(ip, parent_ip, NULL, regs);
4440 #else
4441 static void ftrace_ops_no_ops(unsigned long ip, unsigned long parent_ip)
4443 __ftrace_ops_list_func(ip, parent_ip, NULL, NULL);
4445 #endif
4447 static void clear_ftrace_swapper(void)
4449 struct task_struct *p;
4450 int cpu;
4452 get_online_cpus();
4453 for_each_online_cpu(cpu) {
4454 p = idle_task(cpu);
4455 clear_tsk_trace_trace(p);
4457 put_online_cpus();
4460 static void set_ftrace_swapper(void)
4462 struct task_struct *p;
4463 int cpu;
4465 get_online_cpus();
4466 for_each_online_cpu(cpu) {
4467 p = idle_task(cpu);
4468 set_tsk_trace_trace(p);
4470 put_online_cpus();
4473 static void clear_ftrace_pid(struct pid *pid)
4475 struct task_struct *p;
4477 rcu_read_lock();
4478 do_each_pid_task(pid, PIDTYPE_PID, p) {
4479 clear_tsk_trace_trace(p);
4480 } while_each_pid_task(pid, PIDTYPE_PID, p);
4481 rcu_read_unlock();
4483 put_pid(pid);
4486 static void set_ftrace_pid(struct pid *pid)
4488 struct task_struct *p;
4490 rcu_read_lock();
4491 do_each_pid_task(pid, PIDTYPE_PID, p) {
4492 set_tsk_trace_trace(p);
4493 } while_each_pid_task(pid, PIDTYPE_PID, p);
4494 rcu_read_unlock();
4497 static void clear_ftrace_pid_task(struct pid *pid)
4499 if (pid == ftrace_swapper_pid)
4500 clear_ftrace_swapper();
4501 else
4502 clear_ftrace_pid(pid);
4505 static void set_ftrace_pid_task(struct pid *pid)
4507 if (pid == ftrace_swapper_pid)
4508 set_ftrace_swapper();
4509 else
4510 set_ftrace_pid(pid);
4513 static int ftrace_pid_add(int p)
4515 struct pid *pid;
4516 struct ftrace_pid *fpid;
4517 int ret = -EINVAL;
4519 mutex_lock(&ftrace_lock);
4521 if (!p)
4522 pid = ftrace_swapper_pid;
4523 else
4524 pid = find_get_pid(p);
4526 if (!pid)
4527 goto out;
4529 ret = 0;
4531 list_for_each_entry(fpid, &ftrace_pids, list)
4532 if (fpid->pid == pid)
4533 goto out_put;
4535 ret = -ENOMEM;
4537 fpid = kmalloc(sizeof(*fpid), GFP_KERNEL);
4538 if (!fpid)
4539 goto out_put;
4541 list_add(&fpid->list, &ftrace_pids);
4542 fpid->pid = pid;
4544 set_ftrace_pid_task(pid);
4546 ftrace_update_pid_func();
4547 ftrace_startup_enable(0);
4549 mutex_unlock(&ftrace_lock);
4550 return 0;
4552 out_put:
4553 if (pid != ftrace_swapper_pid)
4554 put_pid(pid);
4556 out:
4557 mutex_unlock(&ftrace_lock);
4558 return ret;
4561 static void ftrace_pid_reset(void)
4563 struct ftrace_pid *fpid, *safe;
4565 mutex_lock(&ftrace_lock);
4566 list_for_each_entry_safe(fpid, safe, &ftrace_pids, list) {
4567 struct pid *pid = fpid->pid;
4569 clear_ftrace_pid_task(pid);
4571 list_del(&fpid->list);
4572 kfree(fpid);
4575 ftrace_update_pid_func();
4576 ftrace_startup_enable(0);
4578 mutex_unlock(&ftrace_lock);
4581 static void *fpid_start(struct seq_file *m, loff_t *pos)
4583 mutex_lock(&ftrace_lock);
4585 if (list_empty(&ftrace_pids) && (!*pos))
4586 return (void *) 1;
4588 return seq_list_start(&ftrace_pids, *pos);
4591 static void *fpid_next(struct seq_file *m, void *v, loff_t *pos)
4593 if (v == (void *)1)
4594 return NULL;
4596 return seq_list_next(v, &ftrace_pids, pos);
4599 static void fpid_stop(struct seq_file *m, void *p)
4601 mutex_unlock(&ftrace_lock);
4604 static int fpid_show(struct seq_file *m, void *v)
4606 const struct ftrace_pid *fpid = list_entry(v, struct ftrace_pid, list);
4608 if (v == (void *)1) {
4609 seq_printf(m, "no pid\n");
4610 return 0;
4613 if (fpid->pid == ftrace_swapper_pid)
4614 seq_printf(m, "swapper tasks\n");
4615 else
4616 seq_printf(m, "%u\n", pid_vnr(fpid->pid));
4618 return 0;
4621 static const struct seq_operations ftrace_pid_sops = {
4622 .start = fpid_start,
4623 .next = fpid_next,
4624 .stop = fpid_stop,
4625 .show = fpid_show,
4628 static int
4629 ftrace_pid_open(struct inode *inode, struct file *file)
4631 int ret = 0;
4633 if ((file->f_mode & FMODE_WRITE) &&
4634 (file->f_flags & O_TRUNC))
4635 ftrace_pid_reset();
4637 if (file->f_mode & FMODE_READ)
4638 ret = seq_open(file, &ftrace_pid_sops);
4640 return ret;
4643 static ssize_t
4644 ftrace_pid_write(struct file *filp, const char __user *ubuf,
4645 size_t cnt, loff_t *ppos)
4647 char buf[64], *tmp;
4648 long val;
4649 int ret;
4651 if (cnt >= sizeof(buf))
4652 return -EINVAL;
4654 if (copy_from_user(&buf, ubuf, cnt))
4655 return -EFAULT;
4657 buf[cnt] = 0;
4660 * Allow "echo > set_ftrace_pid" or "echo -n '' > set_ftrace_pid"
4661 * to clean the filter quietly.
4663 tmp = strstrip(buf);
4664 if (strlen(tmp) == 0)
4665 return 1;
4667 ret = kstrtol(tmp, 10, &val);
4668 if (ret < 0)
4669 return ret;
4671 ret = ftrace_pid_add(val);
4673 return ret ? ret : cnt;
4676 static int
4677 ftrace_pid_release(struct inode *inode, struct file *file)
4679 if (file->f_mode & FMODE_READ)
4680 seq_release(inode, file);
4682 return 0;
4685 static const struct file_operations ftrace_pid_fops = {
4686 .open = ftrace_pid_open,
4687 .write = ftrace_pid_write,
4688 .read = seq_read,
4689 .llseek = ftrace_filter_lseek,
4690 .release = ftrace_pid_release,
4693 static __init int ftrace_init_debugfs(void)
4695 struct dentry *d_tracer;
4697 d_tracer = tracing_init_dentry();
4698 if (!d_tracer)
4699 return 0;
4701 ftrace_init_dyn_debugfs(d_tracer);
4703 trace_create_file("set_ftrace_pid", 0644, d_tracer,
4704 NULL, &ftrace_pid_fops);
4706 ftrace_profile_debugfs(d_tracer);
4708 return 0;
4710 fs_initcall(ftrace_init_debugfs);
4713 * ftrace_kill - kill ftrace
4715 * This function should be used by panic code. It stops ftrace
4716 * but in a not so nice way. If you need to simply kill ftrace
4717 * from a non-atomic section, use ftrace_kill.
4719 void ftrace_kill(void)
4721 ftrace_disabled = 1;
4722 ftrace_enabled = 0;
4723 clear_ftrace_function();
4727 * Test if ftrace is dead or not.
4729 int ftrace_is_dead(void)
4731 return ftrace_disabled;
4735 * register_ftrace_function - register a function for profiling
4736 * @ops - ops structure that holds the function for profiling.
4738 * Register a function to be called by all functions in the
4739 * kernel.
4741 * Note: @ops->func and all the functions it calls must be labeled
4742 * with "notrace", otherwise it will go into a
4743 * recursive loop.
4745 int register_ftrace_function(struct ftrace_ops *ops)
4747 int ret = -1;
4749 ftrace_ops_init(ops);
4751 mutex_lock(&ftrace_lock);
4753 ret = ftrace_startup(ops, 0);
4755 mutex_unlock(&ftrace_lock);
4757 return ret;
4759 EXPORT_SYMBOL_GPL(register_ftrace_function);
4762 * unregister_ftrace_function - unregister a function for profiling.
4763 * @ops - ops structure that holds the function to unregister
4765 * Unregister a function that was added to be called by ftrace profiling.
4767 int unregister_ftrace_function(struct ftrace_ops *ops)
4769 int ret;
4771 mutex_lock(&ftrace_lock);
4772 ret = ftrace_shutdown(ops, 0);
4773 mutex_unlock(&ftrace_lock);
4775 return ret;
4777 EXPORT_SYMBOL_GPL(unregister_ftrace_function);
4780 ftrace_enable_sysctl(struct ctl_table *table, int write,
4781 void __user *buffer, size_t *lenp,
4782 loff_t *ppos)
4784 int ret = -ENODEV;
4786 mutex_lock(&ftrace_lock);
4788 if (unlikely(ftrace_disabled))
4789 goto out;
4791 ret = proc_dointvec(table, write, buffer, lenp, ppos);
4793 if (ret || !write || (last_ftrace_enabled == !!ftrace_enabled))
4794 goto out;
4796 last_ftrace_enabled = !!ftrace_enabled;
4798 if (ftrace_enabled) {
4800 ftrace_startup_sysctl();
4802 /* we are starting ftrace again */
4803 if (ftrace_ops_list != &ftrace_list_end)
4804 update_ftrace_function();
4806 } else {
4807 /* stopping ftrace calls (just send to ftrace_stub) */
4808 ftrace_trace_function = ftrace_stub;
4810 ftrace_shutdown_sysctl();
4813 out:
4814 mutex_unlock(&ftrace_lock);
4815 return ret;
4818 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
4820 static int ftrace_graph_active;
4821 static struct notifier_block ftrace_suspend_notifier;
4823 int ftrace_graph_entry_stub(struct ftrace_graph_ent *trace)
4825 return 0;
4828 /* The callbacks that hook a function */
4829 trace_func_graph_ret_t ftrace_graph_return =
4830 (trace_func_graph_ret_t)ftrace_stub;
4831 trace_func_graph_ent_t ftrace_graph_entry = ftrace_graph_entry_stub;
4832 static trace_func_graph_ent_t __ftrace_graph_entry = ftrace_graph_entry_stub;
4834 /* Try to assign a return stack array on FTRACE_RETSTACK_ALLOC_SIZE tasks. */
4835 static int alloc_retstack_tasklist(struct ftrace_ret_stack **ret_stack_list)
4837 int i;
4838 int ret = 0;
4839 unsigned long flags;
4840 int start = 0, end = FTRACE_RETSTACK_ALLOC_SIZE;
4841 struct task_struct *g, *t;
4843 for (i = 0; i < FTRACE_RETSTACK_ALLOC_SIZE; i++) {
4844 ret_stack_list[i] = kmalloc(FTRACE_RETFUNC_DEPTH
4845 * sizeof(struct ftrace_ret_stack),
4846 GFP_KERNEL);
4847 if (!ret_stack_list[i]) {
4848 start = 0;
4849 end = i;
4850 ret = -ENOMEM;
4851 goto free;
4855 read_lock_irqsave(&tasklist_lock, flags);
4856 do_each_thread(g, t) {
4857 if (start == end) {
4858 ret = -EAGAIN;
4859 goto unlock;
4862 if (t->ret_stack == NULL) {
4863 atomic_set(&t->tracing_graph_pause, 0);
4864 atomic_set(&t->trace_overrun, 0);
4865 t->curr_ret_stack = -1;
4866 /* Make sure the tasks see the -1 first: */
4867 smp_wmb();
4868 t->ret_stack = ret_stack_list[start++];
4870 } while_each_thread(g, t);
4872 unlock:
4873 read_unlock_irqrestore(&tasklist_lock, flags);
4874 free:
4875 for (i = start; i < end; i++)
4876 kfree(ret_stack_list[i]);
4877 return ret;
4880 static void
4881 ftrace_graph_probe_sched_switch(void *ignore,
4882 struct task_struct *prev, struct task_struct *next)
4884 unsigned long long timestamp;
4885 int index;
4888 * Does the user want to count the time a function was asleep.
4889 * If so, do not update the time stamps.
4891 if (trace_flags & TRACE_ITER_SLEEP_TIME)
4892 return;
4894 timestamp = trace_clock_local();
4896 prev->ftrace_timestamp = timestamp;
4898 /* only process tasks that we timestamped */
4899 if (!next->ftrace_timestamp)
4900 return;
4903 * Update all the counters in next to make up for the
4904 * time next was sleeping.
4906 timestamp -= next->ftrace_timestamp;
4908 for (index = next->curr_ret_stack; index >= 0; index--)
4909 next->ret_stack[index].calltime += timestamp;
4912 /* Allocate a return stack for each task */
4913 static int start_graph_tracing(void)
4915 struct ftrace_ret_stack **ret_stack_list;
4916 int ret, cpu;
4918 ret_stack_list = kmalloc(FTRACE_RETSTACK_ALLOC_SIZE *
4919 sizeof(struct ftrace_ret_stack *),
4920 GFP_KERNEL);
4922 if (!ret_stack_list)
4923 return -ENOMEM;
4925 /* The cpu_boot init_task->ret_stack will never be freed */
4926 for_each_online_cpu(cpu) {
4927 if (!idle_task(cpu)->ret_stack)
4928 ftrace_graph_init_idle_task(idle_task(cpu), cpu);
4931 do {
4932 ret = alloc_retstack_tasklist(ret_stack_list);
4933 } while (ret == -EAGAIN);
4935 if (!ret) {
4936 ret = register_trace_sched_switch(ftrace_graph_probe_sched_switch, NULL);
4937 if (ret)
4938 pr_info("ftrace_graph: Couldn't activate tracepoint"
4939 " probe to kernel_sched_switch\n");
4942 kfree(ret_stack_list);
4943 return ret;
4947 * Hibernation protection.
4948 * The state of the current task is too much unstable during
4949 * suspend/restore to disk. We want to protect against that.
4951 static int
4952 ftrace_suspend_notifier_call(struct notifier_block *bl, unsigned long state,
4953 void *unused)
4955 switch (state) {
4956 case PM_HIBERNATION_PREPARE:
4957 pause_graph_tracing();
4958 break;
4960 case PM_POST_HIBERNATION:
4961 unpause_graph_tracing();
4962 break;
4964 return NOTIFY_DONE;
4967 /* Just a place holder for function graph */
4968 static struct ftrace_ops fgraph_ops __read_mostly = {
4969 .func = ftrace_stub,
4970 .flags = FTRACE_OPS_FL_STUB | FTRACE_OPS_FL_GLOBAL |
4971 FTRACE_OPS_FL_RECURSION_SAFE,
4974 static int ftrace_graph_entry_test(struct ftrace_graph_ent *trace)
4976 if (!ftrace_ops_test(&global_ops, trace->func, NULL))
4977 return 0;
4978 return __ftrace_graph_entry(trace);
4982 * The function graph tracer should only trace the functions defined
4983 * by set_ftrace_filter and set_ftrace_notrace. If another function
4984 * tracer ops is registered, the graph tracer requires testing the
4985 * function against the global ops, and not just trace any function
4986 * that any ftrace_ops registered.
4988 static void update_function_graph_func(void)
4990 if (ftrace_ops_list == &ftrace_list_end ||
4991 (ftrace_ops_list == &global_ops &&
4992 global_ops.next == &ftrace_list_end))
4993 ftrace_graph_entry = __ftrace_graph_entry;
4994 else
4995 ftrace_graph_entry = ftrace_graph_entry_test;
4998 int register_ftrace_graph(trace_func_graph_ret_t retfunc,
4999 trace_func_graph_ent_t entryfunc)
5001 int ret = 0;
5003 mutex_lock(&ftrace_lock);
5005 /* we currently allow only one tracer registered at a time */
5006 if (ftrace_graph_active) {
5007 ret = -EBUSY;
5008 goto out;
5011 ftrace_suspend_notifier.notifier_call = ftrace_suspend_notifier_call;
5012 register_pm_notifier(&ftrace_suspend_notifier);
5014 ftrace_graph_active++;
5015 ret = start_graph_tracing();
5016 if (ret) {
5017 ftrace_graph_active--;
5018 goto out;
5021 ftrace_graph_return = retfunc;
5024 * Update the indirect function to the entryfunc, and the
5025 * function that gets called to the entry_test first. Then
5026 * call the update fgraph entry function to determine if
5027 * the entryfunc should be called directly or not.
5029 __ftrace_graph_entry = entryfunc;
5030 ftrace_graph_entry = ftrace_graph_entry_test;
5031 update_function_graph_func();
5033 ret = ftrace_startup(&fgraph_ops, FTRACE_START_FUNC_RET);
5035 out:
5036 mutex_unlock(&ftrace_lock);
5037 return ret;
5040 void unregister_ftrace_graph(void)
5042 mutex_lock(&ftrace_lock);
5044 if (unlikely(!ftrace_graph_active))
5045 goto out;
5047 ftrace_graph_active--;
5048 ftrace_graph_return = (trace_func_graph_ret_t)ftrace_stub;
5049 ftrace_graph_entry = ftrace_graph_entry_stub;
5050 __ftrace_graph_entry = ftrace_graph_entry_stub;
5051 ftrace_shutdown(&fgraph_ops, FTRACE_STOP_FUNC_RET);
5052 unregister_pm_notifier(&ftrace_suspend_notifier);
5053 unregister_trace_sched_switch(ftrace_graph_probe_sched_switch, NULL);
5055 out:
5056 mutex_unlock(&ftrace_lock);
5059 static DEFINE_PER_CPU(struct ftrace_ret_stack *, idle_ret_stack);
5061 static void
5062 graph_init_task(struct task_struct *t, struct ftrace_ret_stack *ret_stack)
5064 atomic_set(&t->tracing_graph_pause, 0);
5065 atomic_set(&t->trace_overrun, 0);
5066 t->ftrace_timestamp = 0;
5067 /* make curr_ret_stack visible before we add the ret_stack */
5068 smp_wmb();
5069 t->ret_stack = ret_stack;
5073 * Allocate a return stack for the idle task. May be the first
5074 * time through, or it may be done by CPU hotplug online.
5076 void ftrace_graph_init_idle_task(struct task_struct *t, int cpu)
5078 t->curr_ret_stack = -1;
5080 * The idle task has no parent, it either has its own
5081 * stack or no stack at all.
5083 if (t->ret_stack)
5084 WARN_ON(t->ret_stack != per_cpu(idle_ret_stack, cpu));
5086 if (ftrace_graph_active) {
5087 struct ftrace_ret_stack *ret_stack;
5089 ret_stack = per_cpu(idle_ret_stack, cpu);
5090 if (!ret_stack) {
5091 ret_stack = kmalloc(FTRACE_RETFUNC_DEPTH
5092 * sizeof(struct ftrace_ret_stack),
5093 GFP_KERNEL);
5094 if (!ret_stack)
5095 return;
5096 per_cpu(idle_ret_stack, cpu) = ret_stack;
5098 graph_init_task(t, ret_stack);
5102 /* Allocate a return stack for newly created task */
5103 void ftrace_graph_init_task(struct task_struct *t)
5105 /* Make sure we do not use the parent ret_stack */
5106 t->ret_stack = NULL;
5107 t->curr_ret_stack = -1;
5109 if (ftrace_graph_active) {
5110 struct ftrace_ret_stack *ret_stack;
5112 ret_stack = kmalloc(FTRACE_RETFUNC_DEPTH
5113 * sizeof(struct ftrace_ret_stack),
5114 GFP_KERNEL);
5115 if (!ret_stack)
5116 return;
5117 graph_init_task(t, ret_stack);
5121 void ftrace_graph_exit_task(struct task_struct *t)
5123 struct ftrace_ret_stack *ret_stack = t->ret_stack;
5125 t->ret_stack = NULL;
5126 /* NULL must become visible to IRQs before we free it: */
5127 barrier();
5129 kfree(ret_stack);
5132 void ftrace_graph_stop(void)
5134 ftrace_stop();
5136 #endif