Linux 4.9.251
[linux/fpc-iii.git] / kernel / trace / ftrace.c
blob16ca877745f62ae2c2390135dad956a550992138
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/tracefs.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>
35 #include <linux/kprobes.h>
37 #include <trace/events/sched.h>
39 #include <asm/setup.h>
41 #include "trace_output.h"
42 #include "trace_stat.h"
44 #define FTRACE_WARN_ON(cond) \
45 ({ \
46 int ___r = cond; \
47 if (WARN_ON(___r)) \
48 ftrace_kill(); \
49 ___r; \
52 #define FTRACE_WARN_ON_ONCE(cond) \
53 ({ \
54 int ___r = cond; \
55 if (WARN_ON_ONCE(___r)) \
56 ftrace_kill(); \
57 ___r; \
60 /* hash bits for specific function selection */
61 #define FTRACE_HASH_BITS 7
62 #define FTRACE_FUNC_HASHSIZE (1 << FTRACE_HASH_BITS)
63 #define FTRACE_HASH_DEFAULT_BITS 10
64 #define FTRACE_HASH_MAX_BITS 12
66 #ifdef CONFIG_DYNAMIC_FTRACE
67 #define INIT_OPS_HASH(opsname) \
68 .func_hash = &opsname.local_hash, \
69 .local_hash.regex_lock = __MUTEX_INITIALIZER(opsname.local_hash.regex_lock),
70 #define ASSIGN_OPS_HASH(opsname, val) \
71 .func_hash = val, \
72 .local_hash.regex_lock = __MUTEX_INITIALIZER(opsname.local_hash.regex_lock),
73 #else
74 #define INIT_OPS_HASH(opsname)
75 #define ASSIGN_OPS_HASH(opsname, val)
76 #endif
78 static struct ftrace_ops ftrace_list_end __read_mostly = {
79 .func = ftrace_stub,
80 .flags = FTRACE_OPS_FL_RECURSION_SAFE | FTRACE_OPS_FL_STUB,
81 INIT_OPS_HASH(ftrace_list_end)
84 /* ftrace_enabled is a method to turn ftrace on or off */
85 int ftrace_enabled __read_mostly;
86 static int last_ftrace_enabled;
88 /* Current function tracing op */
89 struct ftrace_ops *function_trace_op __read_mostly = &ftrace_list_end;
90 /* What to set function_trace_op to */
91 static struct ftrace_ops *set_function_trace_op;
93 static bool ftrace_pids_enabled(struct ftrace_ops *ops)
95 struct trace_array *tr;
97 if (!(ops->flags & FTRACE_OPS_FL_PID) || !ops->private)
98 return false;
100 tr = ops->private;
102 return tr->function_pids != NULL;
105 static void ftrace_update_trampoline(struct ftrace_ops *ops);
108 * ftrace_disabled is set when an anomaly is discovered.
109 * ftrace_disabled is much stronger than ftrace_enabled.
111 static int ftrace_disabled __read_mostly;
113 static DEFINE_MUTEX(ftrace_lock);
115 static struct ftrace_ops *ftrace_ops_list __read_mostly = &ftrace_list_end;
116 ftrace_func_t ftrace_trace_function __read_mostly = ftrace_stub;
117 static struct ftrace_ops global_ops;
119 #if ARCH_SUPPORTS_FTRACE_OPS
120 static void ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip,
121 struct ftrace_ops *op, struct pt_regs *regs);
122 #else
123 /* See comment below, where ftrace_ops_list_func is defined */
124 static void ftrace_ops_no_ops(unsigned long ip, unsigned long parent_ip);
125 #define ftrace_ops_list_func ((ftrace_func_t)ftrace_ops_no_ops)
126 #endif
129 * Traverse the ftrace_global_list, invoking all entries. The reason that we
130 * can use rcu_dereference_raw_notrace() is that elements removed from this list
131 * are simply leaked, so there is no need to interact with a grace-period
132 * mechanism. The rcu_dereference_raw_notrace() calls are needed to handle
133 * concurrent insertions into the ftrace_global_list.
135 * Silly Alpha and silly pointer-speculation compiler optimizations!
137 #define do_for_each_ftrace_op(op, list) \
138 op = rcu_dereference_raw_notrace(list); \
142 * Optimized for just a single item in the list (as that is the normal case).
144 #define while_for_each_ftrace_op(op) \
145 while (likely(op = rcu_dereference_raw_notrace((op)->next)) && \
146 unlikely((op) != &ftrace_list_end))
148 static inline void ftrace_ops_init(struct ftrace_ops *ops)
150 #ifdef CONFIG_DYNAMIC_FTRACE
151 if (!(ops->flags & FTRACE_OPS_FL_INITIALIZED)) {
152 mutex_init(&ops->local_hash.regex_lock);
153 ops->func_hash = &ops->local_hash;
154 ops->flags |= FTRACE_OPS_FL_INITIALIZED;
156 #endif
160 * ftrace_nr_registered_ops - return number of ops registered
162 * Returns the number of ftrace_ops registered and tracing functions
164 int ftrace_nr_registered_ops(void)
166 struct ftrace_ops *ops;
167 int cnt = 0;
169 mutex_lock(&ftrace_lock);
171 for (ops = ftrace_ops_list;
172 ops != &ftrace_list_end; ops = ops->next)
173 cnt++;
175 mutex_unlock(&ftrace_lock);
177 return cnt;
180 static void ftrace_pid_func(unsigned long ip, unsigned long parent_ip,
181 struct ftrace_ops *op, struct pt_regs *regs)
183 struct trace_array *tr = op->private;
185 if (tr && this_cpu_read(tr->trace_buffer.data->ftrace_ignore_pid))
186 return;
188 op->saved_func(ip, parent_ip, op, regs);
192 * clear_ftrace_function - reset the ftrace function
194 * This NULLs the ftrace function and in essence stops
195 * tracing. There may be lag
197 void clear_ftrace_function(void)
199 ftrace_trace_function = ftrace_stub;
202 static void per_cpu_ops_disable_all(struct ftrace_ops *ops)
204 int cpu;
206 for_each_possible_cpu(cpu)
207 *per_cpu_ptr(ops->disabled, cpu) = 1;
210 static int per_cpu_ops_alloc(struct ftrace_ops *ops)
212 int __percpu *disabled;
214 if (WARN_ON_ONCE(!(ops->flags & FTRACE_OPS_FL_PER_CPU)))
215 return -EINVAL;
217 disabled = alloc_percpu(int);
218 if (!disabled)
219 return -ENOMEM;
221 ops->disabled = disabled;
222 per_cpu_ops_disable_all(ops);
223 return 0;
226 static void ftrace_sync(struct work_struct *work)
229 * This function is just a stub to implement a hard force
230 * of synchronize_sched(). This requires synchronizing
231 * tasks even in userspace and idle.
233 * Yes, function tracing is rude.
237 static void ftrace_sync_ipi(void *data)
239 /* Probably not needed, but do it anyway */
240 smp_rmb();
243 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
244 static void update_function_graph_func(void);
246 /* Both enabled by default (can be cleared by function_graph tracer flags */
247 static bool fgraph_sleep_time = true;
248 static bool fgraph_graph_time = true;
250 #else
251 static inline void update_function_graph_func(void) { }
252 #endif
255 static ftrace_func_t ftrace_ops_get_list_func(struct ftrace_ops *ops)
258 * If this is a dynamic, RCU, or per CPU ops, or we force list func,
259 * then it needs to call the list anyway.
261 if (ops->flags & (FTRACE_OPS_FL_DYNAMIC | FTRACE_OPS_FL_PER_CPU |
262 FTRACE_OPS_FL_RCU) || FTRACE_FORCE_LIST_FUNC)
263 return ftrace_ops_list_func;
265 return ftrace_ops_get_func(ops);
268 static void update_ftrace_function(void)
270 ftrace_func_t func;
273 * Prepare the ftrace_ops that the arch callback will use.
274 * If there's only one ftrace_ops registered, the ftrace_ops_list
275 * will point to the ops we want.
277 set_function_trace_op = ftrace_ops_list;
279 /* If there's no ftrace_ops registered, just call the stub function */
280 if (ftrace_ops_list == &ftrace_list_end) {
281 func = ftrace_stub;
284 * If we are at the end of the list and this ops is
285 * recursion safe and not dynamic and the arch supports passing ops,
286 * then have the mcount trampoline call the function directly.
288 } else if (ftrace_ops_list->next == &ftrace_list_end) {
289 func = ftrace_ops_get_list_func(ftrace_ops_list);
291 } else {
292 /* Just use the default ftrace_ops */
293 set_function_trace_op = &ftrace_list_end;
294 func = ftrace_ops_list_func;
297 update_function_graph_func();
299 /* If there's no change, then do nothing more here */
300 if (ftrace_trace_function == func)
301 return;
304 * If we are using the list function, it doesn't care
305 * about the function_trace_ops.
307 if (func == ftrace_ops_list_func) {
308 ftrace_trace_function = func;
310 * Don't even bother setting function_trace_ops,
311 * it would be racy to do so anyway.
313 return;
316 #ifndef CONFIG_DYNAMIC_FTRACE
318 * For static tracing, we need to be a bit more careful.
319 * The function change takes affect immediately. Thus,
320 * we need to coorditate the setting of the function_trace_ops
321 * with the setting of the ftrace_trace_function.
323 * Set the function to the list ops, which will call the
324 * function we want, albeit indirectly, but it handles the
325 * ftrace_ops and doesn't depend on function_trace_op.
327 ftrace_trace_function = ftrace_ops_list_func;
329 * Make sure all CPUs see this. Yes this is slow, but static
330 * tracing is slow and nasty to have enabled.
332 schedule_on_each_cpu(ftrace_sync);
333 /* Now all cpus are using the list ops. */
334 function_trace_op = set_function_trace_op;
335 /* Make sure the function_trace_op is visible on all CPUs */
336 smp_wmb();
337 /* Nasty way to force a rmb on all cpus */
338 smp_call_function(ftrace_sync_ipi, NULL, 1);
339 /* OK, we are all set to update the ftrace_trace_function now! */
340 #endif /* !CONFIG_DYNAMIC_FTRACE */
342 ftrace_trace_function = func;
345 int using_ftrace_ops_list_func(void)
347 return ftrace_trace_function == ftrace_ops_list_func;
350 static void add_ftrace_ops(struct ftrace_ops **list, struct ftrace_ops *ops)
352 ops->next = *list;
354 * We are entering ops into the list but another
355 * CPU might be walking that list. We need to make sure
356 * the ops->next pointer is valid before another CPU sees
357 * the ops pointer included into the list.
359 rcu_assign_pointer(*list, ops);
362 static int remove_ftrace_ops(struct ftrace_ops **list, struct ftrace_ops *ops)
364 struct ftrace_ops **p;
367 * If we are removing the last function, then simply point
368 * to the ftrace_stub.
370 if (*list == ops && ops->next == &ftrace_list_end) {
371 *list = &ftrace_list_end;
372 return 0;
375 for (p = list; *p != &ftrace_list_end; p = &(*p)->next)
376 if (*p == ops)
377 break;
379 if (*p != ops)
380 return -1;
382 *p = (*p)->next;
383 return 0;
386 static void ftrace_update_trampoline(struct ftrace_ops *ops);
388 static int __register_ftrace_function(struct ftrace_ops *ops)
390 if (ops->flags & FTRACE_OPS_FL_DELETED)
391 return -EINVAL;
393 if (WARN_ON(ops->flags & FTRACE_OPS_FL_ENABLED))
394 return -EBUSY;
396 #ifndef CONFIG_DYNAMIC_FTRACE_WITH_REGS
398 * If the ftrace_ops specifies SAVE_REGS, then it only can be used
399 * if the arch supports it, or SAVE_REGS_IF_SUPPORTED is also set.
400 * Setting SAVE_REGS_IF_SUPPORTED makes SAVE_REGS irrelevant.
402 if (ops->flags & FTRACE_OPS_FL_SAVE_REGS &&
403 !(ops->flags & FTRACE_OPS_FL_SAVE_REGS_IF_SUPPORTED))
404 return -EINVAL;
406 if (ops->flags & FTRACE_OPS_FL_SAVE_REGS_IF_SUPPORTED)
407 ops->flags |= FTRACE_OPS_FL_SAVE_REGS;
408 #endif
410 if (!core_kernel_data((unsigned long)ops))
411 ops->flags |= FTRACE_OPS_FL_DYNAMIC;
413 if (ops->flags & FTRACE_OPS_FL_PER_CPU) {
414 if (per_cpu_ops_alloc(ops))
415 return -ENOMEM;
418 add_ftrace_ops(&ftrace_ops_list, ops);
420 /* Always save the function, and reset at unregistering */
421 ops->saved_func = ops->func;
423 if (ftrace_pids_enabled(ops))
424 ops->func = ftrace_pid_func;
426 ftrace_update_trampoline(ops);
428 if (ftrace_enabled)
429 update_ftrace_function();
431 return 0;
434 static int __unregister_ftrace_function(struct ftrace_ops *ops)
436 int ret;
438 if (WARN_ON(!(ops->flags & FTRACE_OPS_FL_ENABLED)))
439 return -EBUSY;
441 ret = remove_ftrace_ops(&ftrace_ops_list, ops);
443 if (ret < 0)
444 return ret;
446 if (ftrace_enabled)
447 update_ftrace_function();
449 ops->func = ops->saved_func;
451 return 0;
454 static void ftrace_update_pid_func(void)
456 struct ftrace_ops *op;
458 /* Only do something if we are tracing something */
459 if (ftrace_trace_function == ftrace_stub)
460 return;
462 do_for_each_ftrace_op(op, ftrace_ops_list) {
463 if (op->flags & FTRACE_OPS_FL_PID) {
464 op->func = ftrace_pids_enabled(op) ?
465 ftrace_pid_func : op->saved_func;
466 ftrace_update_trampoline(op);
468 } while_for_each_ftrace_op(op);
470 update_ftrace_function();
473 #ifdef CONFIG_FUNCTION_PROFILER
474 struct ftrace_profile {
475 struct hlist_node node;
476 unsigned long ip;
477 unsigned long counter;
478 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
479 unsigned long long time;
480 unsigned long long time_squared;
481 #endif
484 struct ftrace_profile_page {
485 struct ftrace_profile_page *next;
486 unsigned long index;
487 struct ftrace_profile records[];
490 struct ftrace_profile_stat {
491 atomic_t disabled;
492 struct hlist_head *hash;
493 struct ftrace_profile_page *pages;
494 struct ftrace_profile_page *start;
495 struct tracer_stat stat;
498 #define PROFILE_RECORDS_SIZE \
499 (PAGE_SIZE - offsetof(struct ftrace_profile_page, records))
501 #define PROFILES_PER_PAGE \
502 (PROFILE_RECORDS_SIZE / sizeof(struct ftrace_profile))
504 static int ftrace_profile_enabled __read_mostly;
506 /* ftrace_profile_lock - synchronize the enable and disable of the profiler */
507 static DEFINE_MUTEX(ftrace_profile_lock);
509 static DEFINE_PER_CPU(struct ftrace_profile_stat, ftrace_profile_stats);
511 #define FTRACE_PROFILE_HASH_BITS 10
512 #define FTRACE_PROFILE_HASH_SIZE (1 << FTRACE_PROFILE_HASH_BITS)
514 static void *
515 function_stat_next(void *v, int idx)
517 struct ftrace_profile *rec = v;
518 struct ftrace_profile_page *pg;
520 pg = (struct ftrace_profile_page *)((unsigned long)rec & PAGE_MASK);
522 again:
523 if (idx != 0)
524 rec++;
526 if ((void *)rec >= (void *)&pg->records[pg->index]) {
527 pg = pg->next;
528 if (!pg)
529 return NULL;
530 rec = &pg->records[0];
531 if (!rec->counter)
532 goto again;
535 return rec;
538 static void *function_stat_start(struct tracer_stat *trace)
540 struct ftrace_profile_stat *stat =
541 container_of(trace, struct ftrace_profile_stat, stat);
543 if (!stat || !stat->start)
544 return NULL;
546 return function_stat_next(&stat->start->records[0], 0);
549 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
550 /* function graph compares on total time */
551 static int function_stat_cmp(void *p1, void *p2)
553 struct ftrace_profile *a = p1;
554 struct ftrace_profile *b = p2;
556 if (a->time < b->time)
557 return -1;
558 if (a->time > b->time)
559 return 1;
560 else
561 return 0;
563 #else
564 /* not function graph compares against hits */
565 static int function_stat_cmp(void *p1, void *p2)
567 struct ftrace_profile *a = p1;
568 struct ftrace_profile *b = p2;
570 if (a->counter < b->counter)
571 return -1;
572 if (a->counter > b->counter)
573 return 1;
574 else
575 return 0;
577 #endif
579 static int function_stat_headers(struct seq_file *m)
581 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
582 seq_puts(m, " Function "
583 "Hit Time Avg s^2\n"
584 " -------- "
585 "--- ---- --- ---\n");
586 #else
587 seq_puts(m, " Function Hit\n"
588 " -------- ---\n");
589 #endif
590 return 0;
593 static int function_stat_show(struct seq_file *m, void *v)
595 struct ftrace_profile *rec = v;
596 char str[KSYM_SYMBOL_LEN];
597 int ret = 0;
598 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
599 static struct trace_seq s;
600 unsigned long long avg;
601 unsigned long long stddev;
602 #endif
603 mutex_lock(&ftrace_profile_lock);
605 /* we raced with function_profile_reset() */
606 if (unlikely(rec->counter == 0)) {
607 ret = -EBUSY;
608 goto out;
611 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
612 avg = div64_ul(rec->time, rec->counter);
613 if (tracing_thresh && (avg < tracing_thresh))
614 goto out;
615 #endif
617 kallsyms_lookup(rec->ip, NULL, NULL, NULL, str);
618 seq_printf(m, " %-30.30s %10lu", str, rec->counter);
620 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
621 seq_puts(m, " ");
623 /* Sample standard deviation (s^2) */
624 if (rec->counter <= 1)
625 stddev = 0;
626 else {
628 * Apply Welford's method:
629 * s^2 = 1 / (n * (n-1)) * (n * \Sum (x_i)^2 - (\Sum x_i)^2)
631 stddev = rec->counter * rec->time_squared -
632 rec->time * rec->time;
635 * Divide only 1000 for ns^2 -> us^2 conversion.
636 * trace_print_graph_duration will divide 1000 again.
638 stddev = div64_ul(stddev,
639 rec->counter * (rec->counter - 1) * 1000);
642 trace_seq_init(&s);
643 trace_print_graph_duration(rec->time, &s);
644 trace_seq_puts(&s, " ");
645 trace_print_graph_duration(avg, &s);
646 trace_seq_puts(&s, " ");
647 trace_print_graph_duration(stddev, &s);
648 trace_print_seq(m, &s);
649 #endif
650 seq_putc(m, '\n');
651 out:
652 mutex_unlock(&ftrace_profile_lock);
654 return ret;
657 static void ftrace_profile_reset(struct ftrace_profile_stat *stat)
659 struct ftrace_profile_page *pg;
661 pg = stat->pages = stat->start;
663 while (pg) {
664 memset(pg->records, 0, PROFILE_RECORDS_SIZE);
665 pg->index = 0;
666 pg = pg->next;
669 memset(stat->hash, 0,
670 FTRACE_PROFILE_HASH_SIZE * sizeof(struct hlist_head));
673 int ftrace_profile_pages_init(struct ftrace_profile_stat *stat)
675 struct ftrace_profile_page *pg;
676 int functions;
677 int pages;
678 int i;
680 /* If we already allocated, do nothing */
681 if (stat->pages)
682 return 0;
684 stat->pages = (void *)get_zeroed_page(GFP_KERNEL);
685 if (!stat->pages)
686 return -ENOMEM;
688 #ifdef CONFIG_DYNAMIC_FTRACE
689 functions = ftrace_update_tot_cnt;
690 #else
692 * We do not know the number of functions that exist because
693 * dynamic tracing is what counts them. With past experience
694 * we have around 20K functions. That should be more than enough.
695 * It is highly unlikely we will execute every function in
696 * the kernel.
698 functions = 20000;
699 #endif
701 pg = stat->start = stat->pages;
703 pages = DIV_ROUND_UP(functions, PROFILES_PER_PAGE);
705 for (i = 1; i < pages; i++) {
706 pg->next = (void *)get_zeroed_page(GFP_KERNEL);
707 if (!pg->next)
708 goto out_free;
709 pg = pg->next;
712 return 0;
714 out_free:
715 pg = stat->start;
716 while (pg) {
717 unsigned long tmp = (unsigned long)pg;
719 pg = pg->next;
720 free_page(tmp);
723 stat->pages = NULL;
724 stat->start = NULL;
726 return -ENOMEM;
729 static int ftrace_profile_init_cpu(int cpu)
731 struct ftrace_profile_stat *stat;
732 int size;
734 stat = &per_cpu(ftrace_profile_stats, cpu);
736 if (stat->hash) {
737 /* If the profile is already created, simply reset it */
738 ftrace_profile_reset(stat);
739 return 0;
743 * We are profiling all functions, but usually only a few thousand
744 * functions are hit. We'll make a hash of 1024 items.
746 size = FTRACE_PROFILE_HASH_SIZE;
748 stat->hash = kzalloc(sizeof(struct hlist_head) * size, GFP_KERNEL);
750 if (!stat->hash)
751 return -ENOMEM;
753 /* Preallocate the function profiling pages */
754 if (ftrace_profile_pages_init(stat) < 0) {
755 kfree(stat->hash);
756 stat->hash = NULL;
757 return -ENOMEM;
760 return 0;
763 static int ftrace_profile_init(void)
765 int cpu;
766 int ret = 0;
768 for_each_possible_cpu(cpu) {
769 ret = ftrace_profile_init_cpu(cpu);
770 if (ret)
771 break;
774 return ret;
777 /* interrupts must be disabled */
778 static struct ftrace_profile *
779 ftrace_find_profiled_func(struct ftrace_profile_stat *stat, unsigned long ip)
781 struct ftrace_profile *rec;
782 struct hlist_head *hhd;
783 unsigned long key;
785 key = hash_long(ip, FTRACE_PROFILE_HASH_BITS);
786 hhd = &stat->hash[key];
788 if (hlist_empty(hhd))
789 return NULL;
791 hlist_for_each_entry_rcu_notrace(rec, hhd, node) {
792 if (rec->ip == ip)
793 return rec;
796 return NULL;
799 static void ftrace_add_profile(struct ftrace_profile_stat *stat,
800 struct ftrace_profile *rec)
802 unsigned long key;
804 key = hash_long(rec->ip, FTRACE_PROFILE_HASH_BITS);
805 hlist_add_head_rcu(&rec->node, &stat->hash[key]);
809 * The memory is already allocated, this simply finds a new record to use.
811 static struct ftrace_profile *
812 ftrace_profile_alloc(struct ftrace_profile_stat *stat, unsigned long ip)
814 struct ftrace_profile *rec = NULL;
816 /* prevent recursion (from NMIs) */
817 if (atomic_inc_return(&stat->disabled) != 1)
818 goto out;
821 * Try to find the function again since an NMI
822 * could have added it
824 rec = ftrace_find_profiled_func(stat, ip);
825 if (rec)
826 goto out;
828 if (stat->pages->index == PROFILES_PER_PAGE) {
829 if (!stat->pages->next)
830 goto out;
831 stat->pages = stat->pages->next;
834 rec = &stat->pages->records[stat->pages->index++];
835 rec->ip = ip;
836 ftrace_add_profile(stat, rec);
838 out:
839 atomic_dec(&stat->disabled);
841 return rec;
844 static void
845 function_profile_call(unsigned long ip, unsigned long parent_ip,
846 struct ftrace_ops *ops, struct pt_regs *regs)
848 struct ftrace_profile_stat *stat;
849 struct ftrace_profile *rec;
850 unsigned long flags;
852 if (!ftrace_profile_enabled)
853 return;
855 local_irq_save(flags);
857 stat = this_cpu_ptr(&ftrace_profile_stats);
858 if (!stat->hash || !ftrace_profile_enabled)
859 goto out;
861 rec = ftrace_find_profiled_func(stat, ip);
862 if (!rec) {
863 rec = ftrace_profile_alloc(stat, ip);
864 if (!rec)
865 goto out;
868 rec->counter++;
869 out:
870 local_irq_restore(flags);
873 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
874 static int profile_graph_entry(struct ftrace_graph_ent *trace)
876 int index = trace->depth;
878 function_profile_call(trace->func, 0, NULL, NULL);
880 /* If function graph is shutting down, ret_stack can be NULL */
881 if (!current->ret_stack)
882 return 0;
884 if (index >= 0 && index < FTRACE_RETFUNC_DEPTH)
885 current->ret_stack[index].subtime = 0;
887 return 1;
890 static void profile_graph_return(struct ftrace_graph_ret *trace)
892 struct ftrace_profile_stat *stat;
893 unsigned long long calltime;
894 struct ftrace_profile *rec;
895 unsigned long flags;
897 local_irq_save(flags);
898 stat = this_cpu_ptr(&ftrace_profile_stats);
899 if (!stat->hash || !ftrace_profile_enabled)
900 goto out;
902 /* If the calltime was zero'd ignore it */
903 if (!trace->calltime)
904 goto out;
906 calltime = trace->rettime - trace->calltime;
908 if (!fgraph_graph_time) {
909 int index;
911 index = trace->depth;
913 /* Append this call time to the parent time to subtract */
914 if (index)
915 current->ret_stack[index - 1].subtime += calltime;
917 if (current->ret_stack[index].subtime < calltime)
918 calltime -= current->ret_stack[index].subtime;
919 else
920 calltime = 0;
923 rec = ftrace_find_profiled_func(stat, trace->func);
924 if (rec) {
925 rec->time += calltime;
926 rec->time_squared += calltime * calltime;
929 out:
930 local_irq_restore(flags);
933 static int register_ftrace_profiler(void)
935 return register_ftrace_graph(&profile_graph_return,
936 &profile_graph_entry);
939 static void unregister_ftrace_profiler(void)
941 unregister_ftrace_graph();
943 #else
944 static struct ftrace_ops ftrace_profile_ops __read_mostly = {
945 .func = function_profile_call,
946 .flags = FTRACE_OPS_FL_RECURSION_SAFE | FTRACE_OPS_FL_INITIALIZED,
947 INIT_OPS_HASH(ftrace_profile_ops)
950 static int register_ftrace_profiler(void)
952 return register_ftrace_function(&ftrace_profile_ops);
955 static void unregister_ftrace_profiler(void)
957 unregister_ftrace_function(&ftrace_profile_ops);
959 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
961 static ssize_t
962 ftrace_profile_write(struct file *filp, const char __user *ubuf,
963 size_t cnt, loff_t *ppos)
965 unsigned long val;
966 int ret;
968 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
969 if (ret)
970 return ret;
972 val = !!val;
974 mutex_lock(&ftrace_profile_lock);
975 if (ftrace_profile_enabled ^ val) {
976 if (val) {
977 ret = ftrace_profile_init();
978 if (ret < 0) {
979 cnt = ret;
980 goto out;
983 ret = register_ftrace_profiler();
984 if (ret < 0) {
985 cnt = ret;
986 goto out;
988 ftrace_profile_enabled = 1;
989 } else {
990 ftrace_profile_enabled = 0;
992 * unregister_ftrace_profiler calls stop_machine
993 * so this acts like an synchronize_sched.
995 unregister_ftrace_profiler();
998 out:
999 mutex_unlock(&ftrace_profile_lock);
1001 *ppos += cnt;
1003 return cnt;
1006 static ssize_t
1007 ftrace_profile_read(struct file *filp, char __user *ubuf,
1008 size_t cnt, loff_t *ppos)
1010 char buf[64]; /* big enough to hold a number */
1011 int r;
1013 r = sprintf(buf, "%u\n", ftrace_profile_enabled);
1014 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
1017 static const struct file_operations ftrace_profile_fops = {
1018 .open = tracing_open_generic,
1019 .read = ftrace_profile_read,
1020 .write = ftrace_profile_write,
1021 .llseek = default_llseek,
1024 /* used to initialize the real stat files */
1025 static struct tracer_stat function_stats __initdata = {
1026 .name = "functions",
1027 .stat_start = function_stat_start,
1028 .stat_next = function_stat_next,
1029 .stat_cmp = function_stat_cmp,
1030 .stat_headers = function_stat_headers,
1031 .stat_show = function_stat_show
1034 static __init void ftrace_profile_tracefs(struct dentry *d_tracer)
1036 struct ftrace_profile_stat *stat;
1037 struct dentry *entry;
1038 char *name;
1039 int ret;
1040 int cpu;
1042 for_each_possible_cpu(cpu) {
1043 stat = &per_cpu(ftrace_profile_stats, cpu);
1045 name = kasprintf(GFP_KERNEL, "function%d", cpu);
1046 if (!name) {
1048 * The files created are permanent, if something happens
1049 * we still do not free memory.
1051 WARN(1,
1052 "Could not allocate stat file for cpu %d\n",
1053 cpu);
1054 return;
1056 stat->stat = function_stats;
1057 stat->stat.name = name;
1058 ret = register_stat_tracer(&stat->stat);
1059 if (ret) {
1060 WARN(1,
1061 "Could not register function stat for cpu %d\n",
1062 cpu);
1063 kfree(name);
1064 return;
1068 entry = tracefs_create_file("function_profile_enabled", 0644,
1069 d_tracer, NULL, &ftrace_profile_fops);
1070 if (!entry)
1071 pr_warn("Could not create tracefs 'function_profile_enabled' entry\n");
1074 #else /* CONFIG_FUNCTION_PROFILER */
1075 static __init void ftrace_profile_tracefs(struct dentry *d_tracer)
1078 #endif /* CONFIG_FUNCTION_PROFILER */
1080 static struct pid * const ftrace_swapper_pid = &init_struct_pid;
1082 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
1083 static int ftrace_graph_active;
1084 #else
1085 # define ftrace_graph_active 0
1086 #endif
1088 #ifdef CONFIG_DYNAMIC_FTRACE
1090 static struct ftrace_ops *removed_ops;
1093 * Set when doing a global update, like enabling all recs or disabling them.
1094 * It is not set when just updating a single ftrace_ops.
1096 static bool update_all_ops;
1098 #ifndef CONFIG_FTRACE_MCOUNT_RECORD
1099 # error Dynamic ftrace depends on MCOUNT_RECORD
1100 #endif
1102 static struct hlist_head ftrace_func_hash[FTRACE_FUNC_HASHSIZE] __read_mostly;
1104 struct ftrace_func_probe {
1105 struct hlist_node node;
1106 struct ftrace_probe_ops *ops;
1107 unsigned long flags;
1108 unsigned long ip;
1109 void *data;
1110 struct list_head free_list;
1113 struct ftrace_func_entry {
1114 struct hlist_node hlist;
1115 unsigned long ip;
1118 struct ftrace_hash {
1119 unsigned long size_bits;
1120 struct hlist_head *buckets;
1121 unsigned long count;
1122 struct rcu_head rcu;
1126 * We make these constant because no one should touch them,
1127 * but they are used as the default "empty hash", to avoid allocating
1128 * it all the time. These are in a read only section such that if
1129 * anyone does try to modify it, it will cause an exception.
1131 static const struct hlist_head empty_buckets[1];
1132 static const struct ftrace_hash empty_hash = {
1133 .buckets = (struct hlist_head *)empty_buckets,
1135 #define EMPTY_HASH ((struct ftrace_hash *)&empty_hash)
1137 static struct ftrace_ops global_ops = {
1138 .func = ftrace_stub,
1139 .local_hash.notrace_hash = EMPTY_HASH,
1140 .local_hash.filter_hash = EMPTY_HASH,
1141 INIT_OPS_HASH(global_ops)
1142 .flags = FTRACE_OPS_FL_RECURSION_SAFE |
1143 FTRACE_OPS_FL_INITIALIZED |
1144 FTRACE_OPS_FL_PID,
1148 * This is used by __kernel_text_address() to return true if the
1149 * address is on a dynamically allocated trampoline that would
1150 * not return true for either core_kernel_text() or
1151 * is_module_text_address().
1153 bool is_ftrace_trampoline(unsigned long addr)
1155 struct ftrace_ops *op;
1156 bool ret = false;
1159 * Some of the ops may be dynamically allocated,
1160 * they are freed after a synchronize_sched().
1162 preempt_disable_notrace();
1164 do_for_each_ftrace_op(op, ftrace_ops_list) {
1166 * This is to check for dynamically allocated trampolines.
1167 * Trampolines that are in kernel text will have
1168 * core_kernel_text() return true.
1170 if (op->trampoline && op->trampoline_size)
1171 if (addr >= op->trampoline &&
1172 addr < op->trampoline + op->trampoline_size) {
1173 ret = true;
1174 goto out;
1176 } while_for_each_ftrace_op(op);
1178 out:
1179 preempt_enable_notrace();
1181 return ret;
1184 struct ftrace_page {
1185 struct ftrace_page *next;
1186 struct dyn_ftrace *records;
1187 int index;
1188 int size;
1191 #define ENTRY_SIZE sizeof(struct dyn_ftrace)
1192 #define ENTRIES_PER_PAGE (PAGE_SIZE / ENTRY_SIZE)
1194 /* estimate from running different kernels */
1195 #define NR_TO_INIT 10000
1197 static struct ftrace_page *ftrace_pages_start;
1198 static struct ftrace_page *ftrace_pages;
1200 static bool __always_inline ftrace_hash_empty(struct ftrace_hash *hash)
1202 return !hash || !hash->count;
1205 static struct ftrace_func_entry *
1206 ftrace_lookup_ip(struct ftrace_hash *hash, unsigned long ip)
1208 unsigned long key;
1209 struct ftrace_func_entry *entry;
1210 struct hlist_head *hhd;
1212 if (ftrace_hash_empty(hash))
1213 return NULL;
1215 if (hash->size_bits > 0)
1216 key = hash_long(ip, hash->size_bits);
1217 else
1218 key = 0;
1220 hhd = &hash->buckets[key];
1222 hlist_for_each_entry_rcu_notrace(entry, hhd, hlist) {
1223 if (entry->ip == ip)
1224 return entry;
1226 return NULL;
1229 static void __add_hash_entry(struct ftrace_hash *hash,
1230 struct ftrace_func_entry *entry)
1232 struct hlist_head *hhd;
1233 unsigned long key;
1235 if (hash->size_bits)
1236 key = hash_long(entry->ip, hash->size_bits);
1237 else
1238 key = 0;
1240 hhd = &hash->buckets[key];
1241 hlist_add_head(&entry->hlist, hhd);
1242 hash->count++;
1245 static int add_hash_entry(struct ftrace_hash *hash, unsigned long ip)
1247 struct ftrace_func_entry *entry;
1249 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
1250 if (!entry)
1251 return -ENOMEM;
1253 entry->ip = ip;
1254 __add_hash_entry(hash, entry);
1256 return 0;
1259 static void
1260 free_hash_entry(struct ftrace_hash *hash,
1261 struct ftrace_func_entry *entry)
1263 hlist_del(&entry->hlist);
1264 kfree(entry);
1265 hash->count--;
1268 static void
1269 remove_hash_entry(struct ftrace_hash *hash,
1270 struct ftrace_func_entry *entry)
1272 hlist_del(&entry->hlist);
1273 hash->count--;
1276 static void ftrace_hash_clear(struct ftrace_hash *hash)
1278 struct hlist_head *hhd;
1279 struct hlist_node *tn;
1280 struct ftrace_func_entry *entry;
1281 int size = 1 << hash->size_bits;
1282 int i;
1284 if (!hash->count)
1285 return;
1287 for (i = 0; i < size; i++) {
1288 hhd = &hash->buckets[i];
1289 hlist_for_each_entry_safe(entry, tn, hhd, hlist)
1290 free_hash_entry(hash, entry);
1292 FTRACE_WARN_ON(hash->count);
1295 static void free_ftrace_hash(struct ftrace_hash *hash)
1297 if (!hash || hash == EMPTY_HASH)
1298 return;
1299 ftrace_hash_clear(hash);
1300 kfree(hash->buckets);
1301 kfree(hash);
1304 static void __free_ftrace_hash_rcu(struct rcu_head *rcu)
1306 struct ftrace_hash *hash;
1308 hash = container_of(rcu, struct ftrace_hash, rcu);
1309 free_ftrace_hash(hash);
1312 static void free_ftrace_hash_rcu(struct ftrace_hash *hash)
1314 if (!hash || hash == EMPTY_HASH)
1315 return;
1316 call_rcu_sched(&hash->rcu, __free_ftrace_hash_rcu);
1319 void ftrace_free_filter(struct ftrace_ops *ops)
1321 ftrace_ops_init(ops);
1322 free_ftrace_hash(ops->func_hash->filter_hash);
1323 free_ftrace_hash(ops->func_hash->notrace_hash);
1326 static struct ftrace_hash *alloc_ftrace_hash(int size_bits)
1328 struct ftrace_hash *hash;
1329 int size;
1331 hash = kzalloc(sizeof(*hash), GFP_KERNEL);
1332 if (!hash)
1333 return NULL;
1335 size = 1 << size_bits;
1336 hash->buckets = kcalloc(size, sizeof(*hash->buckets), GFP_KERNEL);
1338 if (!hash->buckets) {
1339 kfree(hash);
1340 return NULL;
1343 hash->size_bits = size_bits;
1345 return hash;
1348 static struct ftrace_hash *
1349 alloc_and_copy_ftrace_hash(int size_bits, struct ftrace_hash *hash)
1351 struct ftrace_func_entry *entry;
1352 struct ftrace_hash *new_hash;
1353 int size;
1354 int ret;
1355 int i;
1357 new_hash = alloc_ftrace_hash(size_bits);
1358 if (!new_hash)
1359 return NULL;
1361 /* Empty hash? */
1362 if (ftrace_hash_empty(hash))
1363 return new_hash;
1365 size = 1 << hash->size_bits;
1366 for (i = 0; i < size; i++) {
1367 hlist_for_each_entry(entry, &hash->buckets[i], hlist) {
1368 ret = add_hash_entry(new_hash, entry->ip);
1369 if (ret < 0)
1370 goto free_hash;
1374 FTRACE_WARN_ON(new_hash->count != hash->count);
1376 return new_hash;
1378 free_hash:
1379 free_ftrace_hash(new_hash);
1380 return NULL;
1383 static void
1384 ftrace_hash_rec_disable_modify(struct ftrace_ops *ops, int filter_hash);
1385 static void
1386 ftrace_hash_rec_enable_modify(struct ftrace_ops *ops, int filter_hash);
1388 static int ftrace_hash_ipmodify_update(struct ftrace_ops *ops,
1389 struct ftrace_hash *new_hash);
1391 static int
1392 ftrace_hash_move(struct ftrace_ops *ops, int enable,
1393 struct ftrace_hash **dst, struct ftrace_hash *src)
1395 struct ftrace_func_entry *entry;
1396 struct hlist_node *tn;
1397 struct hlist_head *hhd;
1398 struct ftrace_hash *new_hash;
1399 int size = src->count;
1400 int bits = 0;
1401 int ret;
1402 int i;
1404 /* Reject setting notrace hash on IPMODIFY ftrace_ops */
1405 if (ops->flags & FTRACE_OPS_FL_IPMODIFY && !enable)
1406 return -EINVAL;
1409 * If the new source is empty, just free dst and assign it
1410 * the empty_hash.
1412 if (!src->count) {
1413 new_hash = EMPTY_HASH;
1414 goto update;
1418 * Make the hash size about 1/2 the # found
1420 for (size /= 2; size; size >>= 1)
1421 bits++;
1423 /* Don't allocate too much */
1424 if (bits > FTRACE_HASH_MAX_BITS)
1425 bits = FTRACE_HASH_MAX_BITS;
1427 new_hash = alloc_ftrace_hash(bits);
1428 if (!new_hash)
1429 return -ENOMEM;
1431 size = 1 << src->size_bits;
1432 for (i = 0; i < size; i++) {
1433 hhd = &src->buckets[i];
1434 hlist_for_each_entry_safe(entry, tn, hhd, hlist) {
1435 remove_hash_entry(src, entry);
1436 __add_hash_entry(new_hash, entry);
1440 update:
1441 /* Make sure this can be applied if it is IPMODIFY ftrace_ops */
1442 if (enable) {
1443 /* IPMODIFY should be updated only when filter_hash updating */
1444 ret = ftrace_hash_ipmodify_update(ops, new_hash);
1445 if (ret < 0) {
1446 free_ftrace_hash(new_hash);
1447 return ret;
1452 * Remove the current set, update the hash and add
1453 * them back.
1455 ftrace_hash_rec_disable_modify(ops, enable);
1457 rcu_assign_pointer(*dst, new_hash);
1459 ftrace_hash_rec_enable_modify(ops, enable);
1461 return 0;
1464 static bool hash_contains_ip(unsigned long ip,
1465 struct ftrace_ops_hash *hash)
1468 * The function record is a match if it exists in the filter
1469 * hash and not in the notrace hash. Note, an emty hash is
1470 * considered a match for the filter hash, but an empty
1471 * notrace hash is considered not in the notrace hash.
1473 return (ftrace_hash_empty(hash->filter_hash) ||
1474 ftrace_lookup_ip(hash->filter_hash, ip)) &&
1475 (ftrace_hash_empty(hash->notrace_hash) ||
1476 !ftrace_lookup_ip(hash->notrace_hash, ip));
1480 * Test the hashes for this ops to see if we want to call
1481 * the ops->func or not.
1483 * It's a match if the ip is in the ops->filter_hash or
1484 * the filter_hash does not exist or is empty,
1485 * AND
1486 * the ip is not in the ops->notrace_hash.
1488 * This needs to be called with preemption disabled as
1489 * the hashes are freed with call_rcu_sched().
1491 static int
1492 ftrace_ops_test(struct ftrace_ops *ops, unsigned long ip, void *regs)
1494 struct ftrace_ops_hash hash;
1495 int ret;
1497 #ifdef CONFIG_DYNAMIC_FTRACE_WITH_REGS
1499 * There's a small race when adding ops that the ftrace handler
1500 * that wants regs, may be called without them. We can not
1501 * allow that handler to be called if regs is NULL.
1503 if (regs == NULL && (ops->flags & FTRACE_OPS_FL_SAVE_REGS))
1504 return 0;
1505 #endif
1507 hash.filter_hash = rcu_dereference_raw_notrace(ops->func_hash->filter_hash);
1508 hash.notrace_hash = rcu_dereference_raw_notrace(ops->func_hash->notrace_hash);
1510 if (hash_contains_ip(ip, &hash))
1511 ret = 1;
1512 else
1513 ret = 0;
1515 return ret;
1519 * This is a double for. Do not use 'break' to break out of the loop,
1520 * you must use a goto.
1522 #define do_for_each_ftrace_rec(pg, rec) \
1523 for (pg = ftrace_pages_start; pg; pg = pg->next) { \
1524 int _____i; \
1525 for (_____i = 0; _____i < pg->index; _____i++) { \
1526 rec = &pg->records[_____i];
1528 #define while_for_each_ftrace_rec() \
1533 static int ftrace_cmp_recs(const void *a, const void *b)
1535 const struct dyn_ftrace *key = a;
1536 const struct dyn_ftrace *rec = b;
1538 if (key->flags < rec->ip)
1539 return -1;
1540 if (key->ip >= rec->ip + MCOUNT_INSN_SIZE)
1541 return 1;
1542 return 0;
1546 * ftrace_location_range - return the first address of a traced location
1547 * if it touches the given ip range
1548 * @start: start of range to search.
1549 * @end: end of range to search (inclusive). @end points to the last byte
1550 * to check.
1552 * Returns rec->ip if the related ftrace location is a least partly within
1553 * the given address range. That is, the first address of the instruction
1554 * that is either a NOP or call to the function tracer. It checks the ftrace
1555 * internal tables to determine if the address belongs or not.
1557 unsigned long ftrace_location_range(unsigned long start, unsigned long end)
1559 struct ftrace_page *pg;
1560 struct dyn_ftrace *rec;
1561 struct dyn_ftrace key;
1563 key.ip = start;
1564 key.flags = end; /* overload flags, as it is unsigned long */
1566 for (pg = ftrace_pages_start; pg; pg = pg->next) {
1567 if (end < pg->records[0].ip ||
1568 start >= (pg->records[pg->index - 1].ip + MCOUNT_INSN_SIZE))
1569 continue;
1570 rec = bsearch(&key, pg->records, pg->index,
1571 sizeof(struct dyn_ftrace),
1572 ftrace_cmp_recs);
1573 if (rec)
1574 return rec->ip;
1577 return 0;
1581 * ftrace_location - return true if the ip giving is a traced location
1582 * @ip: the instruction pointer to check
1584 * Returns rec->ip if @ip given is a pointer to a ftrace location.
1585 * That is, the instruction that is either a NOP or call to
1586 * the function tracer. It checks the ftrace internal tables to
1587 * determine if the address belongs or not.
1589 unsigned long ftrace_location(unsigned long ip)
1591 return ftrace_location_range(ip, ip);
1595 * ftrace_text_reserved - return true if range contains an ftrace location
1596 * @start: start of range to search
1597 * @end: end of range to search (inclusive). @end points to the last byte to check.
1599 * Returns 1 if @start and @end contains a ftrace location.
1600 * That is, the instruction that is either a NOP or call to
1601 * the function tracer. It checks the ftrace internal tables to
1602 * determine if the address belongs or not.
1604 int ftrace_text_reserved(const void *start, const void *end)
1606 unsigned long ret;
1608 ret = ftrace_location_range((unsigned long)start,
1609 (unsigned long)end);
1611 return (int)!!ret;
1614 /* Test if ops registered to this rec needs regs */
1615 static bool test_rec_ops_needs_regs(struct dyn_ftrace *rec)
1617 struct ftrace_ops *ops;
1618 bool keep_regs = false;
1620 for (ops = ftrace_ops_list;
1621 ops != &ftrace_list_end; ops = ops->next) {
1622 /* pass rec in as regs to have non-NULL val */
1623 if (ftrace_ops_test(ops, rec->ip, rec)) {
1624 if (ops->flags & FTRACE_OPS_FL_SAVE_REGS) {
1625 keep_regs = true;
1626 break;
1631 return keep_regs;
1634 static struct ftrace_ops *
1635 ftrace_find_tramp_ops_any(struct dyn_ftrace *rec);
1636 static struct ftrace_ops *
1637 ftrace_find_tramp_ops_any_other(struct dyn_ftrace *rec, struct ftrace_ops *op_exclude);
1638 static struct ftrace_ops *
1639 ftrace_find_tramp_ops_next(struct dyn_ftrace *rec, struct ftrace_ops *ops);
1641 static bool __ftrace_hash_rec_update(struct ftrace_ops *ops,
1642 int filter_hash,
1643 bool inc)
1645 struct ftrace_hash *hash;
1646 struct ftrace_hash *other_hash;
1647 struct ftrace_page *pg;
1648 struct dyn_ftrace *rec;
1649 bool update = false;
1650 int count = 0;
1651 int all = 0;
1653 /* Only update if the ops has been registered */
1654 if (!(ops->flags & FTRACE_OPS_FL_ENABLED))
1655 return false;
1658 * In the filter_hash case:
1659 * If the count is zero, we update all records.
1660 * Otherwise we just update the items in the hash.
1662 * In the notrace_hash case:
1663 * We enable the update in the hash.
1664 * As disabling notrace means enabling the tracing,
1665 * and enabling notrace means disabling, the inc variable
1666 * gets inversed.
1668 if (filter_hash) {
1669 hash = ops->func_hash->filter_hash;
1670 other_hash = ops->func_hash->notrace_hash;
1671 if (ftrace_hash_empty(hash))
1672 all = 1;
1673 } else {
1674 inc = !inc;
1675 hash = ops->func_hash->notrace_hash;
1676 other_hash = ops->func_hash->filter_hash;
1678 * If the notrace hash has no items,
1679 * then there's nothing to do.
1681 if (ftrace_hash_empty(hash))
1682 return false;
1685 do_for_each_ftrace_rec(pg, rec) {
1686 int in_other_hash = 0;
1687 int in_hash = 0;
1688 int match = 0;
1690 if (rec->flags & FTRACE_FL_DISABLED)
1691 continue;
1693 if (all) {
1695 * Only the filter_hash affects all records.
1696 * Update if the record is not in the notrace hash.
1698 if (!other_hash || !ftrace_lookup_ip(other_hash, rec->ip))
1699 match = 1;
1700 } else {
1701 in_hash = !!ftrace_lookup_ip(hash, rec->ip);
1702 in_other_hash = !!ftrace_lookup_ip(other_hash, rec->ip);
1705 * If filter_hash is set, we want to match all functions
1706 * that are in the hash but not in the other hash.
1708 * If filter_hash is not set, then we are decrementing.
1709 * That means we match anything that is in the hash
1710 * and also in the other_hash. That is, we need to turn
1711 * off functions in the other hash because they are disabled
1712 * by this hash.
1714 if (filter_hash && in_hash && !in_other_hash)
1715 match = 1;
1716 else if (!filter_hash && in_hash &&
1717 (in_other_hash || ftrace_hash_empty(other_hash)))
1718 match = 1;
1720 if (!match)
1721 continue;
1723 if (inc) {
1724 rec->flags++;
1725 if (FTRACE_WARN_ON(ftrace_rec_count(rec) == FTRACE_REF_MAX))
1726 return false;
1729 * If there's only a single callback registered to a
1730 * function, and the ops has a trampoline registered
1731 * for it, then we can call it directly.
1733 if (ftrace_rec_count(rec) == 1 && ops->trampoline)
1734 rec->flags |= FTRACE_FL_TRAMP;
1735 else
1737 * If we are adding another function callback
1738 * to this function, and the previous had a
1739 * custom trampoline in use, then we need to go
1740 * back to the default trampoline.
1742 rec->flags &= ~FTRACE_FL_TRAMP;
1745 * If any ops wants regs saved for this function
1746 * then all ops will get saved regs.
1748 if (ops->flags & FTRACE_OPS_FL_SAVE_REGS)
1749 rec->flags |= FTRACE_FL_REGS;
1750 } else {
1751 if (FTRACE_WARN_ON(ftrace_rec_count(rec) == 0))
1752 return false;
1753 rec->flags--;
1756 * If the rec had REGS enabled and the ops that is
1757 * being removed had REGS set, then see if there is
1758 * still any ops for this record that wants regs.
1759 * If not, we can stop recording them.
1761 if (ftrace_rec_count(rec) > 0 &&
1762 rec->flags & FTRACE_FL_REGS &&
1763 ops->flags & FTRACE_OPS_FL_SAVE_REGS) {
1764 if (!test_rec_ops_needs_regs(rec))
1765 rec->flags &= ~FTRACE_FL_REGS;
1769 * The TRAMP needs to be set only if rec count
1770 * is decremented to one, and the ops that is
1771 * left has a trampoline. As TRAMP can only be
1772 * enabled if there is only a single ops attached
1773 * to it.
1775 if (ftrace_rec_count(rec) == 1 &&
1776 ftrace_find_tramp_ops_any_other(rec, ops))
1777 rec->flags |= FTRACE_FL_TRAMP;
1778 else
1779 rec->flags &= ~FTRACE_FL_TRAMP;
1782 * flags will be cleared in ftrace_check_record()
1783 * if rec count is zero.
1786 count++;
1788 /* Must match FTRACE_UPDATE_CALLS in ftrace_modify_all_code() */
1789 update |= ftrace_test_record(rec, 1) != FTRACE_UPDATE_IGNORE;
1791 /* Shortcut, if we handled all records, we are done. */
1792 if (!all && count == hash->count)
1793 return update;
1794 } while_for_each_ftrace_rec();
1796 return update;
1799 static bool ftrace_hash_rec_disable(struct ftrace_ops *ops,
1800 int filter_hash)
1802 return __ftrace_hash_rec_update(ops, filter_hash, 0);
1805 static bool ftrace_hash_rec_enable(struct ftrace_ops *ops,
1806 int filter_hash)
1808 return __ftrace_hash_rec_update(ops, filter_hash, 1);
1811 static void ftrace_hash_rec_update_modify(struct ftrace_ops *ops,
1812 int filter_hash, int inc)
1814 struct ftrace_ops *op;
1816 __ftrace_hash_rec_update(ops, filter_hash, inc);
1818 if (ops->func_hash != &global_ops.local_hash)
1819 return;
1822 * If the ops shares the global_ops hash, then we need to update
1823 * all ops that are enabled and use this hash.
1825 do_for_each_ftrace_op(op, ftrace_ops_list) {
1826 /* Already done */
1827 if (op == ops)
1828 continue;
1829 if (op->func_hash == &global_ops.local_hash)
1830 __ftrace_hash_rec_update(op, filter_hash, inc);
1831 } while_for_each_ftrace_op(op);
1834 static void ftrace_hash_rec_disable_modify(struct ftrace_ops *ops,
1835 int filter_hash)
1837 ftrace_hash_rec_update_modify(ops, filter_hash, 0);
1840 static void ftrace_hash_rec_enable_modify(struct ftrace_ops *ops,
1841 int filter_hash)
1843 ftrace_hash_rec_update_modify(ops, filter_hash, 1);
1847 * Try to update IPMODIFY flag on each ftrace_rec. Return 0 if it is OK
1848 * or no-needed to update, -EBUSY if it detects a conflict of the flag
1849 * on a ftrace_rec, and -EINVAL if the new_hash tries to trace all recs.
1850 * Note that old_hash and new_hash has below meanings
1851 * - If the hash is NULL, it hits all recs (if IPMODIFY is set, this is rejected)
1852 * - If the hash is EMPTY_HASH, it hits nothing
1853 * - Anything else hits the recs which match the hash entries.
1855 static int __ftrace_hash_update_ipmodify(struct ftrace_ops *ops,
1856 struct ftrace_hash *old_hash,
1857 struct ftrace_hash *new_hash)
1859 struct ftrace_page *pg;
1860 struct dyn_ftrace *rec, *end = NULL;
1861 int in_old, in_new;
1863 /* Only update if the ops has been registered */
1864 if (!(ops->flags & FTRACE_OPS_FL_ENABLED))
1865 return 0;
1867 if (!(ops->flags & FTRACE_OPS_FL_IPMODIFY))
1868 return 0;
1871 * Since the IPMODIFY is a very address sensitive action, we do not
1872 * allow ftrace_ops to set all functions to new hash.
1874 if (!new_hash || !old_hash)
1875 return -EINVAL;
1877 /* Update rec->flags */
1878 do_for_each_ftrace_rec(pg, rec) {
1880 if (rec->flags & FTRACE_FL_DISABLED)
1881 continue;
1883 /* We need to update only differences of filter_hash */
1884 in_old = !!ftrace_lookup_ip(old_hash, rec->ip);
1885 in_new = !!ftrace_lookup_ip(new_hash, rec->ip);
1886 if (in_old == in_new)
1887 continue;
1889 if (in_new) {
1890 /* New entries must ensure no others are using it */
1891 if (rec->flags & FTRACE_FL_IPMODIFY)
1892 goto rollback;
1893 rec->flags |= FTRACE_FL_IPMODIFY;
1894 } else /* Removed entry */
1895 rec->flags &= ~FTRACE_FL_IPMODIFY;
1896 } while_for_each_ftrace_rec();
1898 return 0;
1900 rollback:
1901 end = rec;
1903 /* Roll back what we did above */
1904 do_for_each_ftrace_rec(pg, rec) {
1906 if (rec->flags & FTRACE_FL_DISABLED)
1907 continue;
1909 if (rec == end)
1910 goto err_out;
1912 in_old = !!ftrace_lookup_ip(old_hash, rec->ip);
1913 in_new = !!ftrace_lookup_ip(new_hash, rec->ip);
1914 if (in_old == in_new)
1915 continue;
1917 if (in_new)
1918 rec->flags &= ~FTRACE_FL_IPMODIFY;
1919 else
1920 rec->flags |= FTRACE_FL_IPMODIFY;
1921 } while_for_each_ftrace_rec();
1923 err_out:
1924 return -EBUSY;
1927 static int ftrace_hash_ipmodify_enable(struct ftrace_ops *ops)
1929 struct ftrace_hash *hash = ops->func_hash->filter_hash;
1931 if (ftrace_hash_empty(hash))
1932 hash = NULL;
1934 return __ftrace_hash_update_ipmodify(ops, EMPTY_HASH, hash);
1937 /* Disabling always succeeds */
1938 static void ftrace_hash_ipmodify_disable(struct ftrace_ops *ops)
1940 struct ftrace_hash *hash = ops->func_hash->filter_hash;
1942 if (ftrace_hash_empty(hash))
1943 hash = NULL;
1945 __ftrace_hash_update_ipmodify(ops, hash, EMPTY_HASH);
1948 static int ftrace_hash_ipmodify_update(struct ftrace_ops *ops,
1949 struct ftrace_hash *new_hash)
1951 struct ftrace_hash *old_hash = ops->func_hash->filter_hash;
1953 if (ftrace_hash_empty(old_hash))
1954 old_hash = NULL;
1956 if (ftrace_hash_empty(new_hash))
1957 new_hash = NULL;
1959 return __ftrace_hash_update_ipmodify(ops, old_hash, new_hash);
1962 static void print_ip_ins(const char *fmt, const unsigned char *p)
1964 int i;
1966 printk(KERN_CONT "%s", fmt);
1968 for (i = 0; i < MCOUNT_INSN_SIZE; i++)
1969 printk(KERN_CONT "%s%02x", i ? ":" : "", p[i]);
1972 enum ftrace_bug_type ftrace_bug_type;
1973 const void *ftrace_expected;
1975 static void print_bug_type(void)
1977 switch (ftrace_bug_type) {
1978 case FTRACE_BUG_UNKNOWN:
1979 break;
1980 case FTRACE_BUG_INIT:
1981 pr_info("Initializing ftrace call sites\n");
1982 break;
1983 case FTRACE_BUG_NOP:
1984 pr_info("Setting ftrace call site to NOP\n");
1985 break;
1986 case FTRACE_BUG_CALL:
1987 pr_info("Setting ftrace call site to call ftrace function\n");
1988 break;
1989 case FTRACE_BUG_UPDATE:
1990 pr_info("Updating ftrace call site to call a different ftrace function\n");
1991 break;
1996 * ftrace_bug - report and shutdown function tracer
1997 * @failed: The failed type (EFAULT, EINVAL, EPERM)
1998 * @rec: The record that failed
2000 * The arch code that enables or disables the function tracing
2001 * can call ftrace_bug() when it has detected a problem in
2002 * modifying the code. @failed should be one of either:
2003 * EFAULT - if the problem happens on reading the @ip address
2004 * EINVAL - if what is read at @ip is not what was expected
2005 * EPERM - if the problem happens on writting to the @ip address
2007 void ftrace_bug(int failed, struct dyn_ftrace *rec)
2009 unsigned long ip = rec ? rec->ip : 0;
2011 switch (failed) {
2012 case -EFAULT:
2013 FTRACE_WARN_ON_ONCE(1);
2014 pr_info("ftrace faulted on modifying ");
2015 print_ip_sym(ip);
2016 break;
2017 case -EINVAL:
2018 FTRACE_WARN_ON_ONCE(1);
2019 pr_info("ftrace failed to modify ");
2020 print_ip_sym(ip);
2021 print_ip_ins(" actual: ", (unsigned char *)ip);
2022 pr_cont("\n");
2023 if (ftrace_expected) {
2024 print_ip_ins(" expected: ", ftrace_expected);
2025 pr_cont("\n");
2027 break;
2028 case -EPERM:
2029 FTRACE_WARN_ON_ONCE(1);
2030 pr_info("ftrace faulted on writing ");
2031 print_ip_sym(ip);
2032 break;
2033 default:
2034 FTRACE_WARN_ON_ONCE(1);
2035 pr_info("ftrace faulted on unknown error ");
2036 print_ip_sym(ip);
2038 print_bug_type();
2039 if (rec) {
2040 struct ftrace_ops *ops = NULL;
2042 pr_info("ftrace record flags: %lx\n", rec->flags);
2043 pr_cont(" (%ld)%s", ftrace_rec_count(rec),
2044 rec->flags & FTRACE_FL_REGS ? " R" : " ");
2045 if (rec->flags & FTRACE_FL_TRAMP_EN) {
2046 ops = ftrace_find_tramp_ops_any(rec);
2047 if (ops) {
2048 do {
2049 pr_cont("\ttramp: %pS (%pS)",
2050 (void *)ops->trampoline,
2051 (void *)ops->func);
2052 ops = ftrace_find_tramp_ops_next(rec, ops);
2053 } while (ops);
2054 } else
2055 pr_cont("\ttramp: ERROR!");
2058 ip = ftrace_get_addr_curr(rec);
2059 pr_cont("\n expected tramp: %lx\n", ip);
2063 static int ftrace_check_record(struct dyn_ftrace *rec, int enable, int update)
2065 unsigned long flag = 0UL;
2067 ftrace_bug_type = FTRACE_BUG_UNKNOWN;
2069 if (rec->flags & FTRACE_FL_DISABLED)
2070 return FTRACE_UPDATE_IGNORE;
2073 * If we are updating calls:
2075 * If the record has a ref count, then we need to enable it
2076 * because someone is using it.
2078 * Otherwise we make sure its disabled.
2080 * If we are disabling calls, then disable all records that
2081 * are enabled.
2083 if (enable && ftrace_rec_count(rec))
2084 flag = FTRACE_FL_ENABLED;
2087 * If enabling and the REGS flag does not match the REGS_EN, or
2088 * the TRAMP flag doesn't match the TRAMP_EN, then do not ignore
2089 * this record. Set flags to fail the compare against ENABLED.
2091 if (flag) {
2092 if (!(rec->flags & FTRACE_FL_REGS) !=
2093 !(rec->flags & FTRACE_FL_REGS_EN))
2094 flag |= FTRACE_FL_REGS;
2096 if (!(rec->flags & FTRACE_FL_TRAMP) !=
2097 !(rec->flags & FTRACE_FL_TRAMP_EN))
2098 flag |= FTRACE_FL_TRAMP;
2101 /* If the state of this record hasn't changed, then do nothing */
2102 if ((rec->flags & FTRACE_FL_ENABLED) == flag)
2103 return FTRACE_UPDATE_IGNORE;
2105 if (flag) {
2106 /* Save off if rec is being enabled (for return value) */
2107 flag ^= rec->flags & FTRACE_FL_ENABLED;
2109 if (update) {
2110 rec->flags |= FTRACE_FL_ENABLED;
2111 if (flag & FTRACE_FL_REGS) {
2112 if (rec->flags & FTRACE_FL_REGS)
2113 rec->flags |= FTRACE_FL_REGS_EN;
2114 else
2115 rec->flags &= ~FTRACE_FL_REGS_EN;
2117 if (flag & FTRACE_FL_TRAMP) {
2118 if (rec->flags & FTRACE_FL_TRAMP)
2119 rec->flags |= FTRACE_FL_TRAMP_EN;
2120 else
2121 rec->flags &= ~FTRACE_FL_TRAMP_EN;
2126 * If this record is being updated from a nop, then
2127 * return UPDATE_MAKE_CALL.
2128 * Otherwise,
2129 * return UPDATE_MODIFY_CALL to tell the caller to convert
2130 * from the save regs, to a non-save regs function or
2131 * vice versa, or from a trampoline call.
2133 if (flag & FTRACE_FL_ENABLED) {
2134 ftrace_bug_type = FTRACE_BUG_CALL;
2135 return FTRACE_UPDATE_MAKE_CALL;
2138 ftrace_bug_type = FTRACE_BUG_UPDATE;
2139 return FTRACE_UPDATE_MODIFY_CALL;
2142 if (update) {
2143 /* If there's no more users, clear all flags */
2144 if (!ftrace_rec_count(rec))
2145 rec->flags = 0;
2146 else
2148 * Just disable the record, but keep the ops TRAMP
2149 * and REGS states. The _EN flags must be disabled though.
2151 rec->flags &= ~(FTRACE_FL_ENABLED | FTRACE_FL_TRAMP_EN |
2152 FTRACE_FL_REGS_EN);
2155 ftrace_bug_type = FTRACE_BUG_NOP;
2156 return FTRACE_UPDATE_MAKE_NOP;
2160 * ftrace_update_record, set a record that now is tracing or not
2161 * @rec: the record to update
2162 * @enable: set to 1 if the record is tracing, zero to force disable
2164 * The records that represent all functions that can be traced need
2165 * to be updated when tracing has been enabled.
2167 int ftrace_update_record(struct dyn_ftrace *rec, int enable)
2169 return ftrace_check_record(rec, enable, 1);
2173 * ftrace_test_record, check if the record has been enabled or not
2174 * @rec: the record to test
2175 * @enable: set to 1 to check if enabled, 0 if it is disabled
2177 * The arch code may need to test if a record is already set to
2178 * tracing to determine how to modify the function code that it
2179 * represents.
2181 int ftrace_test_record(struct dyn_ftrace *rec, int enable)
2183 return ftrace_check_record(rec, enable, 0);
2186 static struct ftrace_ops *
2187 ftrace_find_tramp_ops_any(struct dyn_ftrace *rec)
2189 struct ftrace_ops *op;
2190 unsigned long ip = rec->ip;
2192 do_for_each_ftrace_op(op, ftrace_ops_list) {
2194 if (!op->trampoline)
2195 continue;
2197 if (hash_contains_ip(ip, op->func_hash))
2198 return op;
2199 } while_for_each_ftrace_op(op);
2201 return NULL;
2204 static struct ftrace_ops *
2205 ftrace_find_tramp_ops_any_other(struct dyn_ftrace *rec, struct ftrace_ops *op_exclude)
2207 struct ftrace_ops *op;
2208 unsigned long ip = rec->ip;
2210 do_for_each_ftrace_op(op, ftrace_ops_list) {
2212 if (op == op_exclude || !op->trampoline)
2213 continue;
2215 if (hash_contains_ip(ip, op->func_hash))
2216 return op;
2217 } while_for_each_ftrace_op(op);
2219 return NULL;
2222 static struct ftrace_ops *
2223 ftrace_find_tramp_ops_next(struct dyn_ftrace *rec,
2224 struct ftrace_ops *op)
2226 unsigned long ip = rec->ip;
2228 while_for_each_ftrace_op(op) {
2230 if (!op->trampoline)
2231 continue;
2233 if (hash_contains_ip(ip, op->func_hash))
2234 return op;
2237 return NULL;
2240 static struct ftrace_ops *
2241 ftrace_find_tramp_ops_curr(struct dyn_ftrace *rec)
2243 struct ftrace_ops *op;
2244 unsigned long ip = rec->ip;
2247 * Need to check removed ops first.
2248 * If they are being removed, and this rec has a tramp,
2249 * and this rec is in the ops list, then it would be the
2250 * one with the tramp.
2252 if (removed_ops) {
2253 if (hash_contains_ip(ip, &removed_ops->old_hash))
2254 return removed_ops;
2258 * Need to find the current trampoline for a rec.
2259 * Now, a trampoline is only attached to a rec if there
2260 * was a single 'ops' attached to it. But this can be called
2261 * when we are adding another op to the rec or removing the
2262 * current one. Thus, if the op is being added, we can
2263 * ignore it because it hasn't attached itself to the rec
2264 * yet.
2266 * If an ops is being modified (hooking to different functions)
2267 * then we don't care about the new functions that are being
2268 * added, just the old ones (that are probably being removed).
2270 * If we are adding an ops to a function that already is using
2271 * a trampoline, it needs to be removed (trampolines are only
2272 * for single ops connected), then an ops that is not being
2273 * modified also needs to be checked.
2275 do_for_each_ftrace_op(op, ftrace_ops_list) {
2277 if (!op->trampoline)
2278 continue;
2281 * If the ops is being added, it hasn't gotten to
2282 * the point to be removed from this tree yet.
2284 if (op->flags & FTRACE_OPS_FL_ADDING)
2285 continue;
2289 * If the ops is being modified and is in the old
2290 * hash, then it is probably being removed from this
2291 * function.
2293 if ((op->flags & FTRACE_OPS_FL_MODIFYING) &&
2294 hash_contains_ip(ip, &op->old_hash))
2295 return op;
2297 * If the ops is not being added or modified, and it's
2298 * in its normal filter hash, then this must be the one
2299 * we want!
2301 if (!(op->flags & FTRACE_OPS_FL_MODIFYING) &&
2302 hash_contains_ip(ip, op->func_hash))
2303 return op;
2305 } while_for_each_ftrace_op(op);
2307 return NULL;
2310 static struct ftrace_ops *
2311 ftrace_find_tramp_ops_new(struct dyn_ftrace *rec)
2313 struct ftrace_ops *op;
2314 unsigned long ip = rec->ip;
2316 do_for_each_ftrace_op(op, ftrace_ops_list) {
2317 /* pass rec in as regs to have non-NULL val */
2318 if (hash_contains_ip(ip, op->func_hash))
2319 return op;
2320 } while_for_each_ftrace_op(op);
2322 return NULL;
2326 * ftrace_get_addr_new - Get the call address to set to
2327 * @rec: The ftrace record descriptor
2329 * If the record has the FTRACE_FL_REGS set, that means that it
2330 * wants to convert to a callback that saves all regs. If FTRACE_FL_REGS
2331 * is not not set, then it wants to convert to the normal callback.
2333 * Returns the address of the trampoline to set to
2335 unsigned long ftrace_get_addr_new(struct dyn_ftrace *rec)
2337 struct ftrace_ops *ops;
2339 /* Trampolines take precedence over regs */
2340 if (rec->flags & FTRACE_FL_TRAMP) {
2341 ops = ftrace_find_tramp_ops_new(rec);
2342 if (FTRACE_WARN_ON(!ops || !ops->trampoline)) {
2343 pr_warn("Bad trampoline accounting at: %p (%pS) (%lx)\n",
2344 (void *)rec->ip, (void *)rec->ip, rec->flags);
2345 /* Ftrace is shutting down, return anything */
2346 return (unsigned long)FTRACE_ADDR;
2348 return ops->trampoline;
2351 if (rec->flags & FTRACE_FL_REGS)
2352 return (unsigned long)FTRACE_REGS_ADDR;
2353 else
2354 return (unsigned long)FTRACE_ADDR;
2358 * ftrace_get_addr_curr - Get the call address that is already there
2359 * @rec: The ftrace record descriptor
2361 * The FTRACE_FL_REGS_EN is set when the record already points to
2362 * a function that saves all the regs. Basically the '_EN' version
2363 * represents the current state of the function.
2365 * Returns the address of the trampoline that is currently being called
2367 unsigned long ftrace_get_addr_curr(struct dyn_ftrace *rec)
2369 struct ftrace_ops *ops;
2371 /* Trampolines take precedence over regs */
2372 if (rec->flags & FTRACE_FL_TRAMP_EN) {
2373 ops = ftrace_find_tramp_ops_curr(rec);
2374 if (FTRACE_WARN_ON(!ops)) {
2375 pr_warn("Bad trampoline accounting at: %p (%pS)\n",
2376 (void *)rec->ip, (void *)rec->ip);
2377 /* Ftrace is shutting down, return anything */
2378 return (unsigned long)FTRACE_ADDR;
2380 return ops->trampoline;
2383 if (rec->flags & FTRACE_FL_REGS_EN)
2384 return (unsigned long)FTRACE_REGS_ADDR;
2385 else
2386 return (unsigned long)FTRACE_ADDR;
2389 static int
2390 __ftrace_replace_code(struct dyn_ftrace *rec, int enable)
2392 unsigned long ftrace_old_addr;
2393 unsigned long ftrace_addr;
2394 int ret;
2396 ftrace_addr = ftrace_get_addr_new(rec);
2398 /* This needs to be done before we call ftrace_update_record */
2399 ftrace_old_addr = ftrace_get_addr_curr(rec);
2401 ret = ftrace_update_record(rec, enable);
2403 ftrace_bug_type = FTRACE_BUG_UNKNOWN;
2405 switch (ret) {
2406 case FTRACE_UPDATE_IGNORE:
2407 return 0;
2409 case FTRACE_UPDATE_MAKE_CALL:
2410 ftrace_bug_type = FTRACE_BUG_CALL;
2411 return ftrace_make_call(rec, ftrace_addr);
2413 case FTRACE_UPDATE_MAKE_NOP:
2414 ftrace_bug_type = FTRACE_BUG_NOP;
2415 return ftrace_make_nop(NULL, rec, ftrace_old_addr);
2417 case FTRACE_UPDATE_MODIFY_CALL:
2418 ftrace_bug_type = FTRACE_BUG_UPDATE;
2419 return ftrace_modify_call(rec, ftrace_old_addr, ftrace_addr);
2422 return -1; /* unknow ftrace bug */
2425 void __weak ftrace_replace_code(int enable)
2427 struct dyn_ftrace *rec;
2428 struct ftrace_page *pg;
2429 int failed;
2431 if (unlikely(ftrace_disabled))
2432 return;
2434 do_for_each_ftrace_rec(pg, rec) {
2436 if (rec->flags & FTRACE_FL_DISABLED)
2437 continue;
2439 failed = __ftrace_replace_code(rec, enable);
2440 if (failed) {
2441 ftrace_bug(failed, rec);
2442 /* Stop processing */
2443 return;
2445 } while_for_each_ftrace_rec();
2448 struct ftrace_rec_iter {
2449 struct ftrace_page *pg;
2450 int index;
2454 * ftrace_rec_iter_start, start up iterating over traced functions
2456 * Returns an iterator handle that is used to iterate over all
2457 * the records that represent address locations where functions
2458 * are traced.
2460 * May return NULL if no records are available.
2462 struct ftrace_rec_iter *ftrace_rec_iter_start(void)
2465 * We only use a single iterator.
2466 * Protected by the ftrace_lock mutex.
2468 static struct ftrace_rec_iter ftrace_rec_iter;
2469 struct ftrace_rec_iter *iter = &ftrace_rec_iter;
2471 iter->pg = ftrace_pages_start;
2472 iter->index = 0;
2474 /* Could have empty pages */
2475 while (iter->pg && !iter->pg->index)
2476 iter->pg = iter->pg->next;
2478 if (!iter->pg)
2479 return NULL;
2481 return iter;
2485 * ftrace_rec_iter_next, get the next record to process.
2486 * @iter: The handle to the iterator.
2488 * Returns the next iterator after the given iterator @iter.
2490 struct ftrace_rec_iter *ftrace_rec_iter_next(struct ftrace_rec_iter *iter)
2492 iter->index++;
2494 if (iter->index >= iter->pg->index) {
2495 iter->pg = iter->pg->next;
2496 iter->index = 0;
2498 /* Could have empty pages */
2499 while (iter->pg && !iter->pg->index)
2500 iter->pg = iter->pg->next;
2503 if (!iter->pg)
2504 return NULL;
2506 return iter;
2510 * ftrace_rec_iter_record, get the record at the iterator location
2511 * @iter: The current iterator location
2513 * Returns the record that the current @iter is at.
2515 struct dyn_ftrace *ftrace_rec_iter_record(struct ftrace_rec_iter *iter)
2517 return &iter->pg->records[iter->index];
2520 static int
2521 ftrace_code_disable(struct module *mod, struct dyn_ftrace *rec)
2523 int ret;
2525 if (unlikely(ftrace_disabled))
2526 return 0;
2528 ret = ftrace_make_nop(mod, rec, MCOUNT_ADDR);
2529 if (ret) {
2530 ftrace_bug_type = FTRACE_BUG_INIT;
2531 ftrace_bug(ret, rec);
2532 return 0;
2534 return 1;
2538 * archs can override this function if they must do something
2539 * before the modifying code is performed.
2541 int __weak ftrace_arch_code_modify_prepare(void)
2543 return 0;
2547 * archs can override this function if they must do something
2548 * after the modifying code is performed.
2550 int __weak ftrace_arch_code_modify_post_process(void)
2552 return 0;
2555 void ftrace_modify_all_code(int command)
2557 int update = command & FTRACE_UPDATE_TRACE_FUNC;
2558 int err = 0;
2561 * If the ftrace_caller calls a ftrace_ops func directly,
2562 * we need to make sure that it only traces functions it
2563 * expects to trace. When doing the switch of functions,
2564 * we need to update to the ftrace_ops_list_func first
2565 * before the transition between old and new calls are set,
2566 * as the ftrace_ops_list_func will check the ops hashes
2567 * to make sure the ops are having the right functions
2568 * traced.
2570 if (update) {
2571 err = ftrace_update_ftrace_func(ftrace_ops_list_func);
2572 if (FTRACE_WARN_ON(err))
2573 return;
2576 if (command & FTRACE_UPDATE_CALLS)
2577 ftrace_replace_code(1);
2578 else if (command & FTRACE_DISABLE_CALLS)
2579 ftrace_replace_code(0);
2581 if (update && ftrace_trace_function != ftrace_ops_list_func) {
2582 function_trace_op = set_function_trace_op;
2583 smp_wmb();
2584 /* If irqs are disabled, we are in stop machine */
2585 if (!irqs_disabled())
2586 smp_call_function(ftrace_sync_ipi, NULL, 1);
2587 err = ftrace_update_ftrace_func(ftrace_trace_function);
2588 if (FTRACE_WARN_ON(err))
2589 return;
2592 if (command & FTRACE_START_FUNC_RET)
2593 err = ftrace_enable_ftrace_graph_caller();
2594 else if (command & FTRACE_STOP_FUNC_RET)
2595 err = ftrace_disable_ftrace_graph_caller();
2596 FTRACE_WARN_ON(err);
2599 static int __ftrace_modify_code(void *data)
2601 int *command = data;
2603 ftrace_modify_all_code(*command);
2605 return 0;
2609 * ftrace_run_stop_machine, go back to the stop machine method
2610 * @command: The command to tell ftrace what to do
2612 * If an arch needs to fall back to the stop machine method, the
2613 * it can call this function.
2615 void ftrace_run_stop_machine(int command)
2617 stop_machine(__ftrace_modify_code, &command, NULL);
2621 * arch_ftrace_update_code, modify the code to trace or not trace
2622 * @command: The command that needs to be done
2624 * Archs can override this function if it does not need to
2625 * run stop_machine() to modify code.
2627 void __weak arch_ftrace_update_code(int command)
2629 ftrace_run_stop_machine(command);
2632 static void ftrace_run_update_code(int command)
2634 int ret;
2636 ret = ftrace_arch_code_modify_prepare();
2637 FTRACE_WARN_ON(ret);
2638 if (ret)
2639 return;
2642 * By default we use stop_machine() to modify the code.
2643 * But archs can do what ever they want as long as it
2644 * is safe. The stop_machine() is the safest, but also
2645 * produces the most overhead.
2647 arch_ftrace_update_code(command);
2649 ret = ftrace_arch_code_modify_post_process();
2650 FTRACE_WARN_ON(ret);
2653 static void ftrace_run_modify_code(struct ftrace_ops *ops, int command,
2654 struct ftrace_ops_hash *old_hash)
2656 ops->flags |= FTRACE_OPS_FL_MODIFYING;
2657 ops->old_hash.filter_hash = old_hash->filter_hash;
2658 ops->old_hash.notrace_hash = old_hash->notrace_hash;
2659 ftrace_run_update_code(command);
2660 ops->old_hash.filter_hash = NULL;
2661 ops->old_hash.notrace_hash = NULL;
2662 ops->flags &= ~FTRACE_OPS_FL_MODIFYING;
2665 static ftrace_func_t saved_ftrace_func;
2666 static int ftrace_start_up;
2668 void __weak arch_ftrace_trampoline_free(struct ftrace_ops *ops)
2672 static void per_cpu_ops_free(struct ftrace_ops *ops)
2674 free_percpu(ops->disabled);
2677 static void ftrace_startup_enable(int command)
2679 if (saved_ftrace_func != ftrace_trace_function) {
2680 saved_ftrace_func = ftrace_trace_function;
2681 command |= FTRACE_UPDATE_TRACE_FUNC;
2684 if (!command || !ftrace_enabled)
2685 return;
2687 ftrace_run_update_code(command);
2690 static void ftrace_startup_all(int command)
2692 update_all_ops = true;
2693 ftrace_startup_enable(command);
2694 update_all_ops = false;
2697 static int ftrace_startup(struct ftrace_ops *ops, int command)
2699 int ret;
2701 if (unlikely(ftrace_disabled))
2702 return -ENODEV;
2704 ret = __register_ftrace_function(ops);
2705 if (ret)
2706 return ret;
2708 ftrace_start_up++;
2711 * Note that ftrace probes uses this to start up
2712 * and modify functions it will probe. But we still
2713 * set the ADDING flag for modification, as probes
2714 * do not have trampolines. If they add them in the
2715 * future, then the probes will need to distinguish
2716 * between adding and updating probes.
2718 ops->flags |= FTRACE_OPS_FL_ENABLED | FTRACE_OPS_FL_ADDING;
2720 ret = ftrace_hash_ipmodify_enable(ops);
2721 if (ret < 0) {
2722 /* Rollback registration process */
2723 __unregister_ftrace_function(ops);
2724 ftrace_start_up--;
2725 ops->flags &= ~FTRACE_OPS_FL_ENABLED;
2726 return ret;
2729 if (ftrace_hash_rec_enable(ops, 1))
2730 command |= FTRACE_UPDATE_CALLS;
2732 ftrace_startup_enable(command);
2734 ops->flags &= ~FTRACE_OPS_FL_ADDING;
2736 return 0;
2739 static int ftrace_shutdown(struct ftrace_ops *ops, int command)
2741 int ret;
2743 if (unlikely(ftrace_disabled))
2744 return -ENODEV;
2746 ret = __unregister_ftrace_function(ops);
2747 if (ret)
2748 return ret;
2750 ftrace_start_up--;
2752 * Just warn in case of unbalance, no need to kill ftrace, it's not
2753 * critical but the ftrace_call callers may be never nopped again after
2754 * further ftrace uses.
2756 WARN_ON_ONCE(ftrace_start_up < 0);
2758 /* Disabling ipmodify never fails */
2759 ftrace_hash_ipmodify_disable(ops);
2761 if (ftrace_hash_rec_disable(ops, 1))
2762 command |= FTRACE_UPDATE_CALLS;
2764 ops->flags &= ~FTRACE_OPS_FL_ENABLED;
2766 if (saved_ftrace_func != ftrace_trace_function) {
2767 saved_ftrace_func = ftrace_trace_function;
2768 command |= FTRACE_UPDATE_TRACE_FUNC;
2771 if (!command || !ftrace_enabled) {
2773 * If these are dynamic or per_cpu ops, they still
2774 * need their data freed. Since, function tracing is
2775 * not currently active, we can just free them
2776 * without synchronizing all CPUs.
2778 if (ops->flags & (FTRACE_OPS_FL_DYNAMIC | FTRACE_OPS_FL_PER_CPU))
2779 goto free_ops;
2781 return 0;
2785 * If the ops uses a trampoline, then it needs to be
2786 * tested first on update.
2788 ops->flags |= FTRACE_OPS_FL_REMOVING;
2789 removed_ops = ops;
2791 /* The trampoline logic checks the old hashes */
2792 ops->old_hash.filter_hash = ops->func_hash->filter_hash;
2793 ops->old_hash.notrace_hash = ops->func_hash->notrace_hash;
2795 ftrace_run_update_code(command);
2798 * If there's no more ops registered with ftrace, run a
2799 * sanity check to make sure all rec flags are cleared.
2801 if (ftrace_ops_list == &ftrace_list_end) {
2802 struct ftrace_page *pg;
2803 struct dyn_ftrace *rec;
2805 do_for_each_ftrace_rec(pg, rec) {
2806 if (FTRACE_WARN_ON_ONCE(rec->flags & ~FTRACE_FL_DISABLED))
2807 pr_warn(" %pS flags:%lx\n",
2808 (void *)rec->ip, rec->flags);
2809 } while_for_each_ftrace_rec();
2812 ops->old_hash.filter_hash = NULL;
2813 ops->old_hash.notrace_hash = NULL;
2815 removed_ops = NULL;
2816 ops->flags &= ~FTRACE_OPS_FL_REMOVING;
2819 * Dynamic ops may be freed, we must make sure that all
2820 * callers are done before leaving this function.
2821 * The same goes for freeing the per_cpu data of the per_cpu
2822 * ops.
2824 * Again, normal synchronize_sched() is not good enough.
2825 * We need to do a hard force of sched synchronization.
2826 * This is because we use preempt_disable() to do RCU, but
2827 * the function tracers can be called where RCU is not watching
2828 * (like before user_exit()). We can not rely on the RCU
2829 * infrastructure to do the synchronization, thus we must do it
2830 * ourselves.
2832 if (ops->flags & (FTRACE_OPS_FL_DYNAMIC | FTRACE_OPS_FL_PER_CPU)) {
2833 schedule_on_each_cpu(ftrace_sync);
2835 free_ops:
2836 arch_ftrace_trampoline_free(ops);
2838 if (ops->flags & FTRACE_OPS_FL_PER_CPU)
2839 per_cpu_ops_free(ops);
2842 return 0;
2845 static void ftrace_startup_sysctl(void)
2847 int command;
2849 if (unlikely(ftrace_disabled))
2850 return;
2852 /* Force update next time */
2853 saved_ftrace_func = NULL;
2854 /* ftrace_start_up is true if we want ftrace running */
2855 if (ftrace_start_up) {
2856 command = FTRACE_UPDATE_CALLS;
2857 if (ftrace_graph_active)
2858 command |= FTRACE_START_FUNC_RET;
2859 ftrace_startup_enable(command);
2863 static void ftrace_shutdown_sysctl(void)
2865 int command;
2867 if (unlikely(ftrace_disabled))
2868 return;
2870 /* ftrace_start_up is true if ftrace is running */
2871 if (ftrace_start_up) {
2872 command = FTRACE_DISABLE_CALLS;
2873 if (ftrace_graph_active)
2874 command |= FTRACE_STOP_FUNC_RET;
2875 ftrace_run_update_code(command);
2879 static cycle_t ftrace_update_time;
2880 unsigned long ftrace_update_tot_cnt;
2882 static inline int ops_traces_mod(struct ftrace_ops *ops)
2885 * Filter_hash being empty will default to trace module.
2886 * But notrace hash requires a test of individual module functions.
2888 return ftrace_hash_empty(ops->func_hash->filter_hash) &&
2889 ftrace_hash_empty(ops->func_hash->notrace_hash);
2893 * Check if the current ops references the record.
2895 * If the ops traces all functions, then it was already accounted for.
2896 * If the ops does not trace the current record function, skip it.
2897 * If the ops ignores the function via notrace filter, skip it.
2899 static inline bool
2900 ops_references_rec(struct ftrace_ops *ops, struct dyn_ftrace *rec)
2902 /* If ops isn't enabled, ignore it */
2903 if (!(ops->flags & FTRACE_OPS_FL_ENABLED))
2904 return 0;
2906 /* If ops traces all then it includes this function */
2907 if (ops_traces_mod(ops))
2908 return 1;
2910 /* The function must be in the filter */
2911 if (!ftrace_hash_empty(ops->func_hash->filter_hash) &&
2912 !ftrace_lookup_ip(ops->func_hash->filter_hash, rec->ip))
2913 return 0;
2915 /* If in notrace hash, we ignore it too */
2916 if (ftrace_lookup_ip(ops->func_hash->notrace_hash, rec->ip))
2917 return 0;
2919 return 1;
2922 static int ftrace_update_code(struct module *mod, struct ftrace_page *new_pgs)
2924 struct ftrace_page *pg;
2925 struct dyn_ftrace *p;
2926 cycle_t start, stop;
2927 unsigned long update_cnt = 0;
2928 unsigned long rec_flags = 0;
2929 int i;
2931 start = ftrace_now(raw_smp_processor_id());
2934 * When a module is loaded, this function is called to convert
2935 * the calls to mcount in its text to nops, and also to create
2936 * an entry in the ftrace data. Now, if ftrace is activated
2937 * after this call, but before the module sets its text to
2938 * read-only, the modification of enabling ftrace can fail if
2939 * the read-only is done while ftrace is converting the calls.
2940 * To prevent this, the module's records are set as disabled
2941 * and will be enabled after the call to set the module's text
2942 * to read-only.
2944 if (mod)
2945 rec_flags |= FTRACE_FL_DISABLED;
2947 for (pg = new_pgs; pg; pg = pg->next) {
2949 for (i = 0; i < pg->index; i++) {
2951 /* If something went wrong, bail without enabling anything */
2952 if (unlikely(ftrace_disabled))
2953 return -1;
2955 p = &pg->records[i];
2956 p->flags = rec_flags;
2959 * Do the initial record conversion from mcount jump
2960 * to the NOP instructions.
2962 if (!ftrace_code_disable(mod, p))
2963 break;
2965 update_cnt++;
2969 stop = ftrace_now(raw_smp_processor_id());
2970 ftrace_update_time = stop - start;
2971 ftrace_update_tot_cnt += update_cnt;
2973 return 0;
2976 static int ftrace_allocate_records(struct ftrace_page *pg, int count)
2978 int order;
2979 int cnt;
2981 if (WARN_ON(!count))
2982 return -EINVAL;
2984 order = get_count_order(DIV_ROUND_UP(count, ENTRIES_PER_PAGE));
2987 * We want to fill as much as possible. No more than a page
2988 * may be empty.
2990 while ((PAGE_SIZE << order) / ENTRY_SIZE >= count + ENTRIES_PER_PAGE)
2991 order--;
2993 again:
2994 pg->records = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, order);
2996 if (!pg->records) {
2997 /* if we can't allocate this size, try something smaller */
2998 if (!order)
2999 return -ENOMEM;
3000 order >>= 1;
3001 goto again;
3004 cnt = (PAGE_SIZE << order) / ENTRY_SIZE;
3005 pg->size = cnt;
3007 if (cnt > count)
3008 cnt = count;
3010 return cnt;
3013 static struct ftrace_page *
3014 ftrace_allocate_pages(unsigned long num_to_init)
3016 struct ftrace_page *start_pg;
3017 struct ftrace_page *pg;
3018 int order;
3019 int cnt;
3021 if (!num_to_init)
3022 return 0;
3024 start_pg = pg = kzalloc(sizeof(*pg), GFP_KERNEL);
3025 if (!pg)
3026 return NULL;
3029 * Try to allocate as much as possible in one continues
3030 * location that fills in all of the space. We want to
3031 * waste as little space as possible.
3033 for (;;) {
3034 cnt = ftrace_allocate_records(pg, num_to_init);
3035 if (cnt < 0)
3036 goto free_pages;
3038 num_to_init -= cnt;
3039 if (!num_to_init)
3040 break;
3042 pg->next = kzalloc(sizeof(*pg), GFP_KERNEL);
3043 if (!pg->next)
3044 goto free_pages;
3046 pg = pg->next;
3049 return start_pg;
3051 free_pages:
3052 pg = start_pg;
3053 while (pg) {
3054 order = get_count_order(pg->size / ENTRIES_PER_PAGE);
3055 free_pages((unsigned long)pg->records, order);
3056 start_pg = pg->next;
3057 kfree(pg);
3058 pg = start_pg;
3060 pr_info("ftrace: FAILED to allocate memory for functions\n");
3061 return NULL;
3064 #define FTRACE_BUFF_MAX (KSYM_SYMBOL_LEN+4) /* room for wildcards */
3066 struct ftrace_iterator {
3067 loff_t pos;
3068 loff_t func_pos;
3069 struct ftrace_page *pg;
3070 struct dyn_ftrace *func;
3071 struct ftrace_func_probe *probe;
3072 struct trace_parser parser;
3073 struct ftrace_hash *hash;
3074 struct ftrace_ops *ops;
3075 int hidx;
3076 int idx;
3077 unsigned flags;
3080 static void *
3081 t_hash_next(struct seq_file *m, loff_t *pos)
3083 struct ftrace_iterator *iter = m->private;
3084 struct hlist_node *hnd = NULL;
3085 struct hlist_head *hhd;
3087 (*pos)++;
3088 iter->pos = *pos;
3090 if (iter->probe)
3091 hnd = &iter->probe->node;
3092 retry:
3093 if (iter->hidx >= FTRACE_FUNC_HASHSIZE)
3094 return NULL;
3096 hhd = &ftrace_func_hash[iter->hidx];
3098 if (hlist_empty(hhd)) {
3099 iter->hidx++;
3100 hnd = NULL;
3101 goto retry;
3104 if (!hnd)
3105 hnd = hhd->first;
3106 else {
3107 hnd = hnd->next;
3108 if (!hnd) {
3109 iter->hidx++;
3110 goto retry;
3114 if (WARN_ON_ONCE(!hnd))
3115 return NULL;
3117 iter->probe = hlist_entry(hnd, struct ftrace_func_probe, node);
3119 return iter;
3122 static void *t_hash_start(struct seq_file *m, loff_t *pos)
3124 struct ftrace_iterator *iter = m->private;
3125 void *p = NULL;
3126 loff_t l;
3128 if (!(iter->flags & FTRACE_ITER_DO_HASH))
3129 return NULL;
3131 if (iter->func_pos > *pos)
3132 return NULL;
3134 iter->hidx = 0;
3135 for (l = 0; l <= (*pos - iter->func_pos); ) {
3136 p = t_hash_next(m, &l);
3137 if (!p)
3138 break;
3140 if (!p)
3141 return NULL;
3143 /* Only set this if we have an item */
3144 iter->flags |= FTRACE_ITER_HASH;
3146 return iter;
3149 static int
3150 t_hash_show(struct seq_file *m, struct ftrace_iterator *iter)
3152 struct ftrace_func_probe *rec;
3154 rec = iter->probe;
3155 if (WARN_ON_ONCE(!rec))
3156 return -EIO;
3158 if (rec->ops->print)
3159 return rec->ops->print(m, rec->ip, rec->ops, rec->data);
3161 seq_printf(m, "%ps:%ps", (void *)rec->ip, (void *)rec->ops->func);
3163 if (rec->data)
3164 seq_printf(m, ":%p", rec->data);
3165 seq_putc(m, '\n');
3167 return 0;
3170 static void *
3171 t_next(struct seq_file *m, void *v, loff_t *pos)
3173 struct ftrace_iterator *iter = m->private;
3174 struct ftrace_ops *ops = iter->ops;
3175 struct dyn_ftrace *rec = NULL;
3177 if (unlikely(ftrace_disabled))
3178 return NULL;
3180 if (iter->flags & FTRACE_ITER_HASH)
3181 return t_hash_next(m, pos);
3183 (*pos)++;
3184 iter->pos = iter->func_pos = *pos;
3186 if (iter->flags & FTRACE_ITER_PRINTALL)
3187 return t_hash_start(m, pos);
3189 retry:
3190 if (iter->idx >= iter->pg->index) {
3191 if (iter->pg->next) {
3192 iter->pg = iter->pg->next;
3193 iter->idx = 0;
3194 goto retry;
3196 } else {
3197 rec = &iter->pg->records[iter->idx++];
3198 if (((iter->flags & FTRACE_ITER_FILTER) &&
3199 !(ftrace_lookup_ip(ops->func_hash->filter_hash, rec->ip))) ||
3201 ((iter->flags & FTRACE_ITER_NOTRACE) &&
3202 !ftrace_lookup_ip(ops->func_hash->notrace_hash, rec->ip)) ||
3204 ((iter->flags & FTRACE_ITER_ENABLED) &&
3205 !(rec->flags & FTRACE_FL_ENABLED))) {
3207 rec = NULL;
3208 goto retry;
3212 if (!rec)
3213 return t_hash_start(m, pos);
3215 iter->func = rec;
3217 return iter;
3220 static void reset_iter_read(struct ftrace_iterator *iter)
3222 iter->pos = 0;
3223 iter->func_pos = 0;
3224 iter->flags &= ~(FTRACE_ITER_PRINTALL | FTRACE_ITER_HASH);
3227 static void *t_start(struct seq_file *m, loff_t *pos)
3229 struct ftrace_iterator *iter = m->private;
3230 struct ftrace_ops *ops = iter->ops;
3231 void *p = NULL;
3232 loff_t l;
3234 mutex_lock(&ftrace_lock);
3236 if (unlikely(ftrace_disabled))
3237 return NULL;
3240 * If an lseek was done, then reset and start from beginning.
3242 if (*pos < iter->pos)
3243 reset_iter_read(iter);
3246 * For set_ftrace_filter reading, if we have the filter
3247 * off, we can short cut and just print out that all
3248 * functions are enabled.
3250 if ((iter->flags & FTRACE_ITER_FILTER &&
3251 ftrace_hash_empty(ops->func_hash->filter_hash)) ||
3252 (iter->flags & FTRACE_ITER_NOTRACE &&
3253 ftrace_hash_empty(ops->func_hash->notrace_hash))) {
3254 if (*pos > 0)
3255 return t_hash_start(m, pos);
3256 iter->flags |= FTRACE_ITER_PRINTALL;
3257 /* reset in case of seek/pread */
3258 iter->flags &= ~FTRACE_ITER_HASH;
3259 return iter;
3262 if (iter->flags & FTRACE_ITER_HASH)
3263 return t_hash_start(m, pos);
3266 * Unfortunately, we need to restart at ftrace_pages_start
3267 * every time we let go of the ftrace_mutex. This is because
3268 * those pointers can change without the lock.
3270 iter->pg = ftrace_pages_start;
3271 iter->idx = 0;
3272 for (l = 0; l <= *pos; ) {
3273 p = t_next(m, p, &l);
3274 if (!p)
3275 break;
3278 if (!p)
3279 return t_hash_start(m, pos);
3281 return iter;
3284 static void t_stop(struct seq_file *m, void *p)
3286 mutex_unlock(&ftrace_lock);
3289 void * __weak
3290 arch_ftrace_trampoline_func(struct ftrace_ops *ops, struct dyn_ftrace *rec)
3292 return NULL;
3295 static void add_trampoline_func(struct seq_file *m, struct ftrace_ops *ops,
3296 struct dyn_ftrace *rec)
3298 void *ptr;
3300 ptr = arch_ftrace_trampoline_func(ops, rec);
3301 if (ptr)
3302 seq_printf(m, " ->%pS", ptr);
3305 static int t_show(struct seq_file *m, void *v)
3307 struct ftrace_iterator *iter = m->private;
3308 struct dyn_ftrace *rec;
3310 if (iter->flags & FTRACE_ITER_HASH)
3311 return t_hash_show(m, iter);
3313 if (iter->flags & FTRACE_ITER_PRINTALL) {
3314 if (iter->flags & FTRACE_ITER_NOTRACE)
3315 seq_puts(m, "#### no functions disabled ####\n");
3316 else
3317 seq_puts(m, "#### all functions enabled ####\n");
3318 return 0;
3321 rec = iter->func;
3323 if (!rec)
3324 return 0;
3326 seq_printf(m, "%ps", (void *)rec->ip);
3327 if (iter->flags & FTRACE_ITER_ENABLED) {
3328 struct ftrace_ops *ops;
3330 seq_printf(m, " (%ld)%s%s",
3331 ftrace_rec_count(rec),
3332 rec->flags & FTRACE_FL_REGS ? " R" : " ",
3333 rec->flags & FTRACE_FL_IPMODIFY ? " I" : " ");
3334 if (rec->flags & FTRACE_FL_TRAMP_EN) {
3335 ops = ftrace_find_tramp_ops_any(rec);
3336 if (ops) {
3337 do {
3338 seq_printf(m, "\ttramp: %pS (%pS)",
3339 (void *)ops->trampoline,
3340 (void *)ops->func);
3341 add_trampoline_func(m, ops, rec);
3342 ops = ftrace_find_tramp_ops_next(rec, ops);
3343 } while (ops);
3344 } else
3345 seq_puts(m, "\ttramp: ERROR!");
3346 } else {
3347 add_trampoline_func(m, NULL, rec);
3351 seq_putc(m, '\n');
3353 return 0;
3356 static const struct seq_operations show_ftrace_seq_ops = {
3357 .start = t_start,
3358 .next = t_next,
3359 .stop = t_stop,
3360 .show = t_show,
3363 static int
3364 ftrace_avail_open(struct inode *inode, struct file *file)
3366 struct ftrace_iterator *iter;
3368 if (unlikely(ftrace_disabled))
3369 return -ENODEV;
3371 iter = __seq_open_private(file, &show_ftrace_seq_ops, sizeof(*iter));
3372 if (iter) {
3373 iter->pg = ftrace_pages_start;
3374 iter->ops = &global_ops;
3377 return iter ? 0 : -ENOMEM;
3380 static int
3381 ftrace_enabled_open(struct inode *inode, struct file *file)
3383 struct ftrace_iterator *iter;
3385 iter = __seq_open_private(file, &show_ftrace_seq_ops, sizeof(*iter));
3386 if (iter) {
3387 iter->pg = ftrace_pages_start;
3388 iter->flags = FTRACE_ITER_ENABLED;
3389 iter->ops = &global_ops;
3392 return iter ? 0 : -ENOMEM;
3396 * ftrace_regex_open - initialize function tracer filter files
3397 * @ops: The ftrace_ops that hold the hash filters
3398 * @flag: The type of filter to process
3399 * @inode: The inode, usually passed in to your open routine
3400 * @file: The file, usually passed in to your open routine
3402 * ftrace_regex_open() initializes the filter files for the
3403 * @ops. Depending on @flag it may process the filter hash or
3404 * the notrace hash of @ops. With this called from the open
3405 * routine, you can use ftrace_filter_write() for the write
3406 * routine if @flag has FTRACE_ITER_FILTER set, or
3407 * ftrace_notrace_write() if @flag has FTRACE_ITER_NOTRACE set.
3408 * tracing_lseek() should be used as the lseek routine, and
3409 * release must call ftrace_regex_release().
3412 ftrace_regex_open(struct ftrace_ops *ops, int flag,
3413 struct inode *inode, struct file *file)
3415 struct ftrace_iterator *iter;
3416 struct ftrace_hash *hash;
3417 int ret = 0;
3419 ftrace_ops_init(ops);
3421 if (unlikely(ftrace_disabled))
3422 return -ENODEV;
3424 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
3425 if (!iter)
3426 return -ENOMEM;
3428 if (trace_parser_get_init(&iter->parser, FTRACE_BUFF_MAX)) {
3429 kfree(iter);
3430 return -ENOMEM;
3433 iter->ops = ops;
3434 iter->flags = flag;
3436 mutex_lock(&ops->func_hash->regex_lock);
3438 if (flag & FTRACE_ITER_NOTRACE)
3439 hash = ops->func_hash->notrace_hash;
3440 else
3441 hash = ops->func_hash->filter_hash;
3443 if (file->f_mode & FMODE_WRITE) {
3444 const int size_bits = FTRACE_HASH_DEFAULT_BITS;
3446 if (file->f_flags & O_TRUNC)
3447 iter->hash = alloc_ftrace_hash(size_bits);
3448 else
3449 iter->hash = alloc_and_copy_ftrace_hash(size_bits, hash);
3451 if (!iter->hash) {
3452 trace_parser_put(&iter->parser);
3453 kfree(iter);
3454 ret = -ENOMEM;
3455 goto out_unlock;
3459 if (file->f_mode & FMODE_READ) {
3460 iter->pg = ftrace_pages_start;
3462 ret = seq_open(file, &show_ftrace_seq_ops);
3463 if (!ret) {
3464 struct seq_file *m = file->private_data;
3465 m->private = iter;
3466 } else {
3467 /* Failed */
3468 free_ftrace_hash(iter->hash);
3469 trace_parser_put(&iter->parser);
3470 kfree(iter);
3472 } else
3473 file->private_data = iter;
3475 out_unlock:
3476 mutex_unlock(&ops->func_hash->regex_lock);
3478 return ret;
3481 static int
3482 ftrace_filter_open(struct inode *inode, struct file *file)
3484 struct ftrace_ops *ops = inode->i_private;
3486 return ftrace_regex_open(ops,
3487 FTRACE_ITER_FILTER | FTRACE_ITER_DO_HASH,
3488 inode, file);
3491 static int
3492 ftrace_notrace_open(struct inode *inode, struct file *file)
3494 struct ftrace_ops *ops = inode->i_private;
3496 return ftrace_regex_open(ops, FTRACE_ITER_NOTRACE,
3497 inode, file);
3500 /* Type for quick search ftrace basic regexes (globs) from filter_parse_regex */
3501 struct ftrace_glob {
3502 char *search;
3503 unsigned len;
3504 int type;
3508 * If symbols in an architecture don't correspond exactly to the user-visible
3509 * name of what they represent, it is possible to define this function to
3510 * perform the necessary adjustments.
3512 char * __weak arch_ftrace_match_adjust(char *str, const char *search)
3514 return str;
3517 static int ftrace_match(char *str, struct ftrace_glob *g)
3519 int matched = 0;
3520 int slen;
3522 str = arch_ftrace_match_adjust(str, g->search);
3524 switch (g->type) {
3525 case MATCH_FULL:
3526 if (strcmp(str, g->search) == 0)
3527 matched = 1;
3528 break;
3529 case MATCH_FRONT_ONLY:
3530 if (strncmp(str, g->search, g->len) == 0)
3531 matched = 1;
3532 break;
3533 case MATCH_MIDDLE_ONLY:
3534 if (strstr(str, g->search))
3535 matched = 1;
3536 break;
3537 case MATCH_END_ONLY:
3538 slen = strlen(str);
3539 if (slen >= g->len &&
3540 memcmp(str + slen - g->len, g->search, g->len) == 0)
3541 matched = 1;
3542 break;
3545 return matched;
3548 static int
3549 enter_record(struct ftrace_hash *hash, struct dyn_ftrace *rec, int clear_filter)
3551 struct ftrace_func_entry *entry;
3552 int ret = 0;
3554 entry = ftrace_lookup_ip(hash, rec->ip);
3555 if (clear_filter) {
3556 /* Do nothing if it doesn't exist */
3557 if (!entry)
3558 return 0;
3560 free_hash_entry(hash, entry);
3561 } else {
3562 /* Do nothing if it exists */
3563 if (entry)
3564 return 0;
3566 ret = add_hash_entry(hash, rec->ip);
3568 return ret;
3571 static int
3572 ftrace_match_record(struct dyn_ftrace *rec, struct ftrace_glob *func_g,
3573 struct ftrace_glob *mod_g, int exclude_mod)
3575 char str[KSYM_SYMBOL_LEN];
3576 char *modname;
3578 kallsyms_lookup(rec->ip, NULL, NULL, &modname, str);
3580 if (mod_g) {
3581 int mod_matches = (modname) ? ftrace_match(modname, mod_g) : 0;
3583 /* blank module name to match all modules */
3584 if (!mod_g->len) {
3585 /* blank module globbing: modname xor exclude_mod */
3586 if ((!exclude_mod) != (!modname))
3587 goto func_match;
3588 return 0;
3591 /* not matching the module */
3592 if (!modname || !mod_matches) {
3593 if (exclude_mod)
3594 goto func_match;
3595 else
3596 return 0;
3599 if (mod_matches && exclude_mod)
3600 return 0;
3602 func_match:
3603 /* blank search means to match all funcs in the mod */
3604 if (!func_g->len)
3605 return 1;
3608 return ftrace_match(str, func_g);
3611 static int
3612 match_records(struct ftrace_hash *hash, char *func, int len, char *mod)
3614 struct ftrace_page *pg;
3615 struct dyn_ftrace *rec;
3616 struct ftrace_glob func_g = { .type = MATCH_FULL };
3617 struct ftrace_glob mod_g = { .type = MATCH_FULL };
3618 struct ftrace_glob *mod_match = (mod) ? &mod_g : NULL;
3619 int exclude_mod = 0;
3620 int found = 0;
3621 int ret;
3622 int clear_filter = 0;
3624 if (func) {
3625 func_g.type = filter_parse_regex(func, len, &func_g.search,
3626 &clear_filter);
3627 func_g.len = strlen(func_g.search);
3630 if (mod) {
3631 mod_g.type = filter_parse_regex(mod, strlen(mod),
3632 &mod_g.search, &exclude_mod);
3633 mod_g.len = strlen(mod_g.search);
3636 mutex_lock(&ftrace_lock);
3638 if (unlikely(ftrace_disabled))
3639 goto out_unlock;
3641 do_for_each_ftrace_rec(pg, rec) {
3643 if (rec->flags & FTRACE_FL_DISABLED)
3644 continue;
3646 if (ftrace_match_record(rec, &func_g, mod_match, exclude_mod)) {
3647 ret = enter_record(hash, rec, clear_filter);
3648 if (ret < 0) {
3649 found = ret;
3650 goto out_unlock;
3652 found = 1;
3654 } while_for_each_ftrace_rec();
3655 out_unlock:
3656 mutex_unlock(&ftrace_lock);
3658 return found;
3661 static int
3662 ftrace_match_records(struct ftrace_hash *hash, char *buff, int len)
3664 return match_records(hash, buff, len, NULL);
3669 * We register the module command as a template to show others how
3670 * to register the a command as well.
3673 static int
3674 ftrace_mod_callback(struct ftrace_hash *hash,
3675 char *func, char *cmd, char *module, int enable)
3677 int ret;
3680 * cmd == 'mod' because we only registered this func
3681 * for the 'mod' ftrace_func_command.
3682 * But if you register one func with multiple commands,
3683 * you can tell which command was used by the cmd
3684 * parameter.
3686 ret = match_records(hash, func, strlen(func), module);
3687 if (!ret)
3688 return -EINVAL;
3689 if (ret < 0)
3690 return ret;
3691 return 0;
3694 static struct ftrace_func_command ftrace_mod_cmd = {
3695 .name = "mod",
3696 .func = ftrace_mod_callback,
3699 static int __init ftrace_mod_cmd_init(void)
3701 return register_ftrace_command(&ftrace_mod_cmd);
3703 core_initcall(ftrace_mod_cmd_init);
3705 static void function_trace_probe_call(unsigned long ip, unsigned long parent_ip,
3706 struct ftrace_ops *op, struct pt_regs *pt_regs)
3708 struct ftrace_func_probe *entry;
3709 struct hlist_head *hhd;
3710 unsigned long key;
3712 key = hash_long(ip, FTRACE_HASH_BITS);
3714 hhd = &ftrace_func_hash[key];
3716 if (hlist_empty(hhd))
3717 return;
3720 * Disable preemption for these calls to prevent a RCU grace
3721 * period. This syncs the hash iteration and freeing of items
3722 * on the hash. rcu_read_lock is too dangerous here.
3724 preempt_disable_notrace();
3725 hlist_for_each_entry_rcu_notrace(entry, hhd, node) {
3726 if (entry->ip == ip)
3727 entry->ops->func(ip, parent_ip, &entry->data);
3729 preempt_enable_notrace();
3732 static struct ftrace_ops trace_probe_ops __read_mostly =
3734 .func = function_trace_probe_call,
3735 .flags = FTRACE_OPS_FL_INITIALIZED,
3736 INIT_OPS_HASH(trace_probe_ops)
3739 static int ftrace_probe_registered;
3741 static void __enable_ftrace_function_probe(struct ftrace_ops_hash *old_hash)
3743 int ret;
3744 int i;
3746 if (ftrace_probe_registered) {
3747 /* still need to update the function call sites */
3748 if (ftrace_enabled)
3749 ftrace_run_modify_code(&trace_probe_ops, FTRACE_UPDATE_CALLS,
3750 old_hash);
3751 return;
3754 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
3755 struct hlist_head *hhd = &ftrace_func_hash[i];
3756 if (hhd->first)
3757 break;
3759 /* Nothing registered? */
3760 if (i == FTRACE_FUNC_HASHSIZE)
3761 return;
3763 ret = ftrace_startup(&trace_probe_ops, 0);
3765 ftrace_probe_registered = 1;
3768 static bool __disable_ftrace_function_probe(void)
3770 int i;
3772 if (!ftrace_probe_registered)
3773 return false;
3775 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
3776 struct hlist_head *hhd = &ftrace_func_hash[i];
3777 if (hhd->first)
3778 return false;
3781 /* no more funcs left */
3782 ftrace_shutdown(&trace_probe_ops, 0);
3784 ftrace_probe_registered = 0;
3785 return true;
3789 static void ftrace_free_entry(struct ftrace_func_probe *entry)
3791 if (entry->ops->free)
3792 entry->ops->free(entry->ops, entry->ip, &entry->data);
3793 kfree(entry);
3797 register_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
3798 void *data)
3800 struct ftrace_ops_hash old_hash_ops;
3801 struct ftrace_func_probe *entry;
3802 struct ftrace_glob func_g;
3803 struct ftrace_hash **orig_hash = &trace_probe_ops.func_hash->filter_hash;
3804 struct ftrace_hash *old_hash = *orig_hash;
3805 struct ftrace_hash *hash;
3806 struct ftrace_page *pg;
3807 struct dyn_ftrace *rec;
3808 int not;
3809 unsigned long key;
3810 int count = 0;
3811 int ret;
3813 func_g.type = filter_parse_regex(glob, strlen(glob),
3814 &func_g.search, &not);
3815 func_g.len = strlen(func_g.search);
3817 /* we do not support '!' for function probes */
3818 if (WARN_ON(not))
3819 return -EINVAL;
3821 mutex_lock(&trace_probe_ops.func_hash->regex_lock);
3823 old_hash_ops.filter_hash = old_hash;
3824 /* Probes only have filters */
3825 old_hash_ops.notrace_hash = NULL;
3827 hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, old_hash);
3828 if (!hash) {
3829 count = -ENOMEM;
3830 goto out;
3833 if (unlikely(ftrace_disabled)) {
3834 count = -ENODEV;
3835 goto out;
3838 mutex_lock(&ftrace_lock);
3840 do_for_each_ftrace_rec(pg, rec) {
3842 if (rec->flags & FTRACE_FL_DISABLED)
3843 continue;
3845 if (!ftrace_match_record(rec, &func_g, NULL, 0))
3846 continue;
3848 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
3849 if (!entry) {
3850 /* If we did not process any, then return error */
3851 if (!count)
3852 count = -ENOMEM;
3853 goto out_unlock;
3856 count++;
3858 entry->data = data;
3861 * The caller might want to do something special
3862 * for each function we find. We call the callback
3863 * to give the caller an opportunity to do so.
3865 if (ops->init) {
3866 if (ops->init(ops, rec->ip, &entry->data) < 0) {
3867 /* caller does not like this func */
3868 kfree(entry);
3869 continue;
3873 ret = enter_record(hash, rec, 0);
3874 if (ret < 0) {
3875 kfree(entry);
3876 count = ret;
3877 goto out_unlock;
3880 entry->ops = ops;
3881 entry->ip = rec->ip;
3883 key = hash_long(entry->ip, FTRACE_HASH_BITS);
3884 hlist_add_head_rcu(&entry->node, &ftrace_func_hash[key]);
3886 } while_for_each_ftrace_rec();
3888 ret = ftrace_hash_move(&trace_probe_ops, 1, orig_hash, hash);
3890 __enable_ftrace_function_probe(&old_hash_ops);
3892 if (!ret)
3893 free_ftrace_hash_rcu(old_hash);
3894 else
3895 count = ret;
3897 out_unlock:
3898 mutex_unlock(&ftrace_lock);
3899 out:
3900 mutex_unlock(&trace_probe_ops.func_hash->regex_lock);
3901 free_ftrace_hash(hash);
3903 return count;
3906 enum {
3907 PROBE_TEST_FUNC = 1,
3908 PROBE_TEST_DATA = 2
3911 static void
3912 __unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
3913 void *data, int flags)
3915 struct ftrace_ops_hash old_hash_ops;
3916 struct ftrace_func_entry *rec_entry;
3917 struct ftrace_func_probe *entry;
3918 struct ftrace_func_probe *p;
3919 struct ftrace_glob func_g;
3920 struct ftrace_hash **orig_hash = &trace_probe_ops.func_hash->filter_hash;
3921 struct ftrace_hash *old_hash = *orig_hash;
3922 struct list_head free_list;
3923 struct ftrace_hash *hash;
3924 struct hlist_node *tmp;
3925 char str[KSYM_SYMBOL_LEN];
3926 int i, ret;
3927 bool disabled;
3929 if (glob && (strcmp(glob, "*") == 0 || !strlen(glob)))
3930 func_g.search = NULL;
3931 else if (glob) {
3932 int not;
3934 func_g.type = filter_parse_regex(glob, strlen(glob),
3935 &func_g.search, &not);
3936 func_g.len = strlen(func_g.search);
3938 /* we do not support '!' for function probes */
3939 if (WARN_ON(not))
3940 return;
3943 mutex_lock(&trace_probe_ops.func_hash->regex_lock);
3945 old_hash_ops.filter_hash = old_hash;
3946 /* Probes only have filters */
3947 old_hash_ops.notrace_hash = NULL;
3949 hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, *orig_hash);
3950 if (!hash)
3951 /* Hmm, should report this somehow */
3952 goto out_unlock;
3954 INIT_LIST_HEAD(&free_list);
3956 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
3957 struct hlist_head *hhd = &ftrace_func_hash[i];
3959 hlist_for_each_entry_safe(entry, tmp, hhd, node) {
3961 /* break up if statements for readability */
3962 if ((flags & PROBE_TEST_FUNC) && entry->ops != ops)
3963 continue;
3965 if ((flags & PROBE_TEST_DATA) && entry->data != data)
3966 continue;
3968 /* do this last, since it is the most expensive */
3969 if (func_g.search) {
3970 kallsyms_lookup(entry->ip, NULL, NULL,
3971 NULL, str);
3972 if (!ftrace_match(str, &func_g))
3973 continue;
3976 rec_entry = ftrace_lookup_ip(hash, entry->ip);
3977 /* It is possible more than one entry had this ip */
3978 if (rec_entry)
3979 free_hash_entry(hash, rec_entry);
3981 hlist_del_rcu(&entry->node);
3982 list_add(&entry->free_list, &free_list);
3985 mutex_lock(&ftrace_lock);
3986 disabled = __disable_ftrace_function_probe();
3988 * Remove after the disable is called. Otherwise, if the last
3989 * probe is removed, a null hash means *all enabled*.
3991 ret = ftrace_hash_move(&trace_probe_ops, 1, orig_hash, hash);
3993 /* still need to update the function call sites */
3994 if (ftrace_enabled && !disabled)
3995 ftrace_run_modify_code(&trace_probe_ops, FTRACE_UPDATE_CALLS,
3996 &old_hash_ops);
3997 synchronize_sched();
3998 if (!ret)
3999 free_ftrace_hash_rcu(old_hash);
4001 list_for_each_entry_safe(entry, p, &free_list, free_list) {
4002 list_del(&entry->free_list);
4003 ftrace_free_entry(entry);
4005 mutex_unlock(&ftrace_lock);
4007 out_unlock:
4008 mutex_unlock(&trace_probe_ops.func_hash->regex_lock);
4009 free_ftrace_hash(hash);
4012 void
4013 unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
4014 void *data)
4016 __unregister_ftrace_function_probe(glob, ops, data,
4017 PROBE_TEST_FUNC | PROBE_TEST_DATA);
4020 void
4021 unregister_ftrace_function_probe_func(char *glob, struct ftrace_probe_ops *ops)
4023 __unregister_ftrace_function_probe(glob, ops, NULL, PROBE_TEST_FUNC);
4026 void unregister_ftrace_function_probe_all(char *glob)
4028 __unregister_ftrace_function_probe(glob, NULL, NULL, 0);
4031 static LIST_HEAD(ftrace_commands);
4032 static DEFINE_MUTEX(ftrace_cmd_mutex);
4035 * Currently we only register ftrace commands from __init, so mark this
4036 * __init too.
4038 __init int register_ftrace_command(struct ftrace_func_command *cmd)
4040 struct ftrace_func_command *p;
4041 int ret = 0;
4043 mutex_lock(&ftrace_cmd_mutex);
4044 list_for_each_entry(p, &ftrace_commands, list) {
4045 if (strcmp(cmd->name, p->name) == 0) {
4046 ret = -EBUSY;
4047 goto out_unlock;
4050 list_add(&cmd->list, &ftrace_commands);
4051 out_unlock:
4052 mutex_unlock(&ftrace_cmd_mutex);
4054 return ret;
4058 * Currently we only unregister ftrace commands from __init, so mark
4059 * this __init too.
4061 __init int unregister_ftrace_command(struct ftrace_func_command *cmd)
4063 struct ftrace_func_command *p, *n;
4064 int ret = -ENODEV;
4066 mutex_lock(&ftrace_cmd_mutex);
4067 list_for_each_entry_safe(p, n, &ftrace_commands, list) {
4068 if (strcmp(cmd->name, p->name) == 0) {
4069 ret = 0;
4070 list_del_init(&p->list);
4071 goto out_unlock;
4074 out_unlock:
4075 mutex_unlock(&ftrace_cmd_mutex);
4077 return ret;
4080 static int ftrace_process_regex(struct ftrace_hash *hash,
4081 char *buff, int len, int enable)
4083 char *func, *command, *next = buff;
4084 struct ftrace_func_command *p;
4085 int ret = -EINVAL;
4087 func = strsep(&next, ":");
4089 if (!next) {
4090 ret = ftrace_match_records(hash, func, len);
4091 if (!ret)
4092 ret = -EINVAL;
4093 if (ret < 0)
4094 return ret;
4095 return 0;
4098 /* command found */
4100 command = strsep(&next, ":");
4102 mutex_lock(&ftrace_cmd_mutex);
4103 list_for_each_entry(p, &ftrace_commands, list) {
4104 if (strcmp(p->name, command) == 0) {
4105 ret = p->func(hash, func, command, next, enable);
4106 goto out_unlock;
4109 out_unlock:
4110 mutex_unlock(&ftrace_cmd_mutex);
4112 return ret;
4115 static ssize_t
4116 ftrace_regex_write(struct file *file, const char __user *ubuf,
4117 size_t cnt, loff_t *ppos, int enable)
4119 struct ftrace_iterator *iter;
4120 struct trace_parser *parser;
4121 ssize_t ret, read;
4123 if (!cnt)
4124 return 0;
4126 if (file->f_mode & FMODE_READ) {
4127 struct seq_file *m = file->private_data;
4128 iter = m->private;
4129 } else
4130 iter = file->private_data;
4132 if (unlikely(ftrace_disabled))
4133 return -ENODEV;
4135 /* iter->hash is a local copy, so we don't need regex_lock */
4137 parser = &iter->parser;
4138 read = trace_get_user(parser, ubuf, cnt, ppos);
4140 if (read >= 0 && trace_parser_loaded(parser) &&
4141 !trace_parser_cont(parser)) {
4142 ret = ftrace_process_regex(iter->hash, parser->buffer,
4143 parser->idx, enable);
4144 trace_parser_clear(parser);
4145 if (ret < 0)
4146 goto out;
4149 ret = read;
4150 out:
4151 return ret;
4154 ssize_t
4155 ftrace_filter_write(struct file *file, const char __user *ubuf,
4156 size_t cnt, loff_t *ppos)
4158 return ftrace_regex_write(file, ubuf, cnt, ppos, 1);
4161 ssize_t
4162 ftrace_notrace_write(struct file *file, const char __user *ubuf,
4163 size_t cnt, loff_t *ppos)
4165 return ftrace_regex_write(file, ubuf, cnt, ppos, 0);
4168 static int
4169 ftrace_match_addr(struct ftrace_hash *hash, unsigned long ip, int remove)
4171 struct ftrace_func_entry *entry;
4173 if (!ftrace_location(ip))
4174 return -EINVAL;
4176 if (remove) {
4177 entry = ftrace_lookup_ip(hash, ip);
4178 if (!entry)
4179 return -ENOENT;
4180 free_hash_entry(hash, entry);
4181 return 0;
4184 return add_hash_entry(hash, ip);
4187 static void ftrace_ops_update_code(struct ftrace_ops *ops,
4188 struct ftrace_ops_hash *old_hash)
4190 struct ftrace_ops *op;
4192 if (!ftrace_enabled)
4193 return;
4195 if (ops->flags & FTRACE_OPS_FL_ENABLED) {
4196 ftrace_run_modify_code(ops, FTRACE_UPDATE_CALLS, old_hash);
4197 return;
4201 * If this is the shared global_ops filter, then we need to
4202 * check if there is another ops that shares it, is enabled.
4203 * If so, we still need to run the modify code.
4205 if (ops->func_hash != &global_ops.local_hash)
4206 return;
4208 do_for_each_ftrace_op(op, ftrace_ops_list) {
4209 if (op->func_hash == &global_ops.local_hash &&
4210 op->flags & FTRACE_OPS_FL_ENABLED) {
4211 ftrace_run_modify_code(op, FTRACE_UPDATE_CALLS, old_hash);
4212 /* Only need to do this once */
4213 return;
4215 } while_for_each_ftrace_op(op);
4218 static int
4219 ftrace_set_hash(struct ftrace_ops *ops, unsigned char *buf, int len,
4220 unsigned long ip, int remove, int reset, int enable)
4222 struct ftrace_hash **orig_hash;
4223 struct ftrace_ops_hash old_hash_ops;
4224 struct ftrace_hash *old_hash;
4225 struct ftrace_hash *hash;
4226 int ret;
4228 if (unlikely(ftrace_disabled))
4229 return -ENODEV;
4231 mutex_lock(&ops->func_hash->regex_lock);
4233 if (enable)
4234 orig_hash = &ops->func_hash->filter_hash;
4235 else
4236 orig_hash = &ops->func_hash->notrace_hash;
4238 if (reset)
4239 hash = alloc_ftrace_hash(FTRACE_HASH_DEFAULT_BITS);
4240 else
4241 hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, *orig_hash);
4243 if (!hash) {
4244 ret = -ENOMEM;
4245 goto out_regex_unlock;
4248 if (buf && !ftrace_match_records(hash, buf, len)) {
4249 ret = -EINVAL;
4250 goto out_regex_unlock;
4252 if (ip) {
4253 ret = ftrace_match_addr(hash, ip, remove);
4254 if (ret < 0)
4255 goto out_regex_unlock;
4258 mutex_lock(&ftrace_lock);
4259 old_hash = *orig_hash;
4260 old_hash_ops.filter_hash = ops->func_hash->filter_hash;
4261 old_hash_ops.notrace_hash = ops->func_hash->notrace_hash;
4262 ret = ftrace_hash_move(ops, enable, orig_hash, hash);
4263 if (!ret) {
4264 ftrace_ops_update_code(ops, &old_hash_ops);
4265 free_ftrace_hash_rcu(old_hash);
4267 mutex_unlock(&ftrace_lock);
4269 out_regex_unlock:
4270 mutex_unlock(&ops->func_hash->regex_lock);
4272 free_ftrace_hash(hash);
4273 return ret;
4276 static int
4277 ftrace_set_addr(struct ftrace_ops *ops, unsigned long ip, int remove,
4278 int reset, int enable)
4280 return ftrace_set_hash(ops, 0, 0, ip, remove, reset, enable);
4284 * ftrace_set_filter_ip - set a function to filter on in ftrace by address
4285 * @ops - the ops to set the filter with
4286 * @ip - the address to add to or remove from the filter.
4287 * @remove - non zero to remove the ip from the filter
4288 * @reset - non zero to reset all filters before applying this filter.
4290 * Filters denote which functions should be enabled when tracing is enabled
4291 * If @ip is NULL, it failes to update filter.
4293 int ftrace_set_filter_ip(struct ftrace_ops *ops, unsigned long ip,
4294 int remove, int reset)
4296 ftrace_ops_init(ops);
4297 return ftrace_set_addr(ops, ip, remove, reset, 1);
4299 EXPORT_SYMBOL_GPL(ftrace_set_filter_ip);
4301 static int
4302 ftrace_set_regex(struct ftrace_ops *ops, unsigned char *buf, int len,
4303 int reset, int enable)
4305 return ftrace_set_hash(ops, buf, len, 0, 0, reset, enable);
4309 * ftrace_set_filter - set a function to filter on in ftrace
4310 * @ops - the ops to set the filter with
4311 * @buf - the string that holds the function filter text.
4312 * @len - the length of the string.
4313 * @reset - non zero to reset all filters before applying this filter.
4315 * Filters denote which functions should be enabled when tracing is enabled.
4316 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
4318 int ftrace_set_filter(struct ftrace_ops *ops, unsigned char *buf,
4319 int len, int reset)
4321 ftrace_ops_init(ops);
4322 return ftrace_set_regex(ops, buf, len, reset, 1);
4324 EXPORT_SYMBOL_GPL(ftrace_set_filter);
4327 * ftrace_set_notrace - set a function to not trace in ftrace
4328 * @ops - the ops to set the notrace filter with
4329 * @buf - the string that holds the function notrace text.
4330 * @len - the length of the string.
4331 * @reset - non zero to reset all filters before applying this filter.
4333 * Notrace Filters denote which functions should not be enabled when tracing
4334 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
4335 * for tracing.
4337 int ftrace_set_notrace(struct ftrace_ops *ops, unsigned char *buf,
4338 int len, int reset)
4340 ftrace_ops_init(ops);
4341 return ftrace_set_regex(ops, buf, len, reset, 0);
4343 EXPORT_SYMBOL_GPL(ftrace_set_notrace);
4345 * ftrace_set_global_filter - set a function to filter on with global tracers
4346 * @buf - the string that holds the function filter text.
4347 * @len - the length of the string.
4348 * @reset - non zero to reset all filters before applying this filter.
4350 * Filters denote which functions should be enabled when tracing is enabled.
4351 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
4353 void ftrace_set_global_filter(unsigned char *buf, int len, int reset)
4355 ftrace_set_regex(&global_ops, buf, len, reset, 1);
4357 EXPORT_SYMBOL_GPL(ftrace_set_global_filter);
4360 * ftrace_set_global_notrace - set a function to not trace with global tracers
4361 * @buf - the string that holds the function notrace text.
4362 * @len - the length of the string.
4363 * @reset - non zero to reset all filters before applying this filter.
4365 * Notrace Filters denote which functions should not be enabled when tracing
4366 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
4367 * for tracing.
4369 void ftrace_set_global_notrace(unsigned char *buf, int len, int reset)
4371 ftrace_set_regex(&global_ops, buf, len, reset, 0);
4373 EXPORT_SYMBOL_GPL(ftrace_set_global_notrace);
4376 * command line interface to allow users to set filters on boot up.
4378 #define FTRACE_FILTER_SIZE COMMAND_LINE_SIZE
4379 static char ftrace_notrace_buf[FTRACE_FILTER_SIZE] __initdata;
4380 static char ftrace_filter_buf[FTRACE_FILTER_SIZE] __initdata;
4382 /* Used by function selftest to not test if filter is set */
4383 bool ftrace_filter_param __initdata;
4385 static int __init set_ftrace_notrace(char *str)
4387 ftrace_filter_param = true;
4388 strlcpy(ftrace_notrace_buf, str, FTRACE_FILTER_SIZE);
4389 return 1;
4391 __setup("ftrace_notrace=", set_ftrace_notrace);
4393 static int __init set_ftrace_filter(char *str)
4395 ftrace_filter_param = true;
4396 strlcpy(ftrace_filter_buf, str, FTRACE_FILTER_SIZE);
4397 return 1;
4399 __setup("ftrace_filter=", set_ftrace_filter);
4401 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
4402 static char ftrace_graph_buf[FTRACE_FILTER_SIZE] __initdata;
4403 static char ftrace_graph_notrace_buf[FTRACE_FILTER_SIZE] __initdata;
4404 static int ftrace_set_func(unsigned long *array, int *idx, int size, char *buffer);
4406 static int __init set_graph_function(char *str)
4408 strlcpy(ftrace_graph_buf, str, FTRACE_FILTER_SIZE);
4409 return 1;
4411 __setup("ftrace_graph_filter=", set_graph_function);
4413 static int __init set_graph_notrace_function(char *str)
4415 strlcpy(ftrace_graph_notrace_buf, str, FTRACE_FILTER_SIZE);
4416 return 1;
4418 __setup("ftrace_graph_notrace=", set_graph_notrace_function);
4420 static void __init set_ftrace_early_graph(char *buf, int enable)
4422 int ret;
4423 char *func;
4424 unsigned long *table = ftrace_graph_funcs;
4425 int *count = &ftrace_graph_count;
4427 if (!enable) {
4428 table = ftrace_graph_notrace_funcs;
4429 count = &ftrace_graph_notrace_count;
4432 while (buf) {
4433 func = strsep(&buf, ",");
4434 /* we allow only one expression at a time */
4435 ret = ftrace_set_func(table, count, FTRACE_GRAPH_MAX_FUNCS, func);
4436 if (ret)
4437 printk(KERN_DEBUG "ftrace: function %s not "
4438 "traceable\n", func);
4441 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
4443 void __init
4444 ftrace_set_early_filter(struct ftrace_ops *ops, char *buf, int enable)
4446 char *func;
4448 ftrace_ops_init(ops);
4450 while (buf) {
4451 func = strsep(&buf, ",");
4452 ftrace_set_regex(ops, func, strlen(func), 0, enable);
4456 static void __init set_ftrace_early_filters(void)
4458 if (ftrace_filter_buf[0])
4459 ftrace_set_early_filter(&global_ops, ftrace_filter_buf, 1);
4460 if (ftrace_notrace_buf[0])
4461 ftrace_set_early_filter(&global_ops, ftrace_notrace_buf, 0);
4462 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
4463 if (ftrace_graph_buf[0])
4464 set_ftrace_early_graph(ftrace_graph_buf, 1);
4465 if (ftrace_graph_notrace_buf[0])
4466 set_ftrace_early_graph(ftrace_graph_notrace_buf, 0);
4467 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
4470 int ftrace_regex_release(struct inode *inode, struct file *file)
4472 struct seq_file *m = (struct seq_file *)file->private_data;
4473 struct ftrace_ops_hash old_hash_ops;
4474 struct ftrace_iterator *iter;
4475 struct ftrace_hash **orig_hash;
4476 struct ftrace_hash *old_hash;
4477 struct trace_parser *parser;
4478 int filter_hash;
4479 int ret;
4481 if (file->f_mode & FMODE_READ) {
4482 iter = m->private;
4483 seq_release(inode, file);
4484 } else
4485 iter = file->private_data;
4487 parser = &iter->parser;
4488 if (trace_parser_loaded(parser)) {
4489 parser->buffer[parser->idx] = 0;
4490 ftrace_match_records(iter->hash, parser->buffer, parser->idx);
4493 trace_parser_put(parser);
4495 mutex_lock(&iter->ops->func_hash->regex_lock);
4497 if (file->f_mode & FMODE_WRITE) {
4498 filter_hash = !!(iter->flags & FTRACE_ITER_FILTER);
4500 if (filter_hash)
4501 orig_hash = &iter->ops->func_hash->filter_hash;
4502 else
4503 orig_hash = &iter->ops->func_hash->notrace_hash;
4505 mutex_lock(&ftrace_lock);
4506 old_hash = *orig_hash;
4507 old_hash_ops.filter_hash = iter->ops->func_hash->filter_hash;
4508 old_hash_ops.notrace_hash = iter->ops->func_hash->notrace_hash;
4509 ret = ftrace_hash_move(iter->ops, filter_hash,
4510 orig_hash, iter->hash);
4511 if (!ret) {
4512 ftrace_ops_update_code(iter->ops, &old_hash_ops);
4513 free_ftrace_hash_rcu(old_hash);
4515 mutex_unlock(&ftrace_lock);
4518 mutex_unlock(&iter->ops->func_hash->regex_lock);
4519 free_ftrace_hash(iter->hash);
4520 kfree(iter);
4522 return 0;
4525 static const struct file_operations ftrace_avail_fops = {
4526 .open = ftrace_avail_open,
4527 .read = seq_read,
4528 .llseek = seq_lseek,
4529 .release = seq_release_private,
4532 static const struct file_operations ftrace_enabled_fops = {
4533 .open = ftrace_enabled_open,
4534 .read = seq_read,
4535 .llseek = seq_lseek,
4536 .release = seq_release_private,
4539 static const struct file_operations ftrace_filter_fops = {
4540 .open = ftrace_filter_open,
4541 .read = seq_read,
4542 .write = ftrace_filter_write,
4543 .llseek = tracing_lseek,
4544 .release = ftrace_regex_release,
4547 static const struct file_operations ftrace_notrace_fops = {
4548 .open = ftrace_notrace_open,
4549 .read = seq_read,
4550 .write = ftrace_notrace_write,
4551 .llseek = tracing_lseek,
4552 .release = ftrace_regex_release,
4555 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
4557 static DEFINE_MUTEX(graph_lock);
4559 int ftrace_graph_count;
4560 int ftrace_graph_notrace_count;
4561 unsigned long ftrace_graph_funcs[FTRACE_GRAPH_MAX_FUNCS] __read_mostly;
4562 unsigned long ftrace_graph_notrace_funcs[FTRACE_GRAPH_MAX_FUNCS] __read_mostly;
4564 struct ftrace_graph_data {
4565 unsigned long *table;
4566 size_t size;
4567 int *count;
4568 const struct seq_operations *seq_ops;
4571 static void *
4572 __g_next(struct seq_file *m, loff_t *pos)
4574 struct ftrace_graph_data *fgd = m->private;
4576 if (*pos >= *fgd->count)
4577 return NULL;
4578 return &fgd->table[*pos];
4581 static void *
4582 g_next(struct seq_file *m, void *v, loff_t *pos)
4584 (*pos)++;
4585 return __g_next(m, pos);
4588 static void *g_start(struct seq_file *m, loff_t *pos)
4590 struct ftrace_graph_data *fgd = m->private;
4592 mutex_lock(&graph_lock);
4594 /* Nothing, tell g_show to print all functions are enabled */
4595 if (!*fgd->count && !*pos)
4596 return (void *)1;
4598 return __g_next(m, pos);
4601 static void g_stop(struct seq_file *m, void *p)
4603 mutex_unlock(&graph_lock);
4606 static int g_show(struct seq_file *m, void *v)
4608 unsigned long *ptr = v;
4610 if (!ptr)
4611 return 0;
4613 if (ptr == (unsigned long *)1) {
4614 struct ftrace_graph_data *fgd = m->private;
4616 if (fgd->table == ftrace_graph_funcs)
4617 seq_puts(m, "#### all functions enabled ####\n");
4618 else
4619 seq_puts(m, "#### no functions disabled ####\n");
4620 return 0;
4623 seq_printf(m, "%ps\n", (void *)*ptr);
4625 return 0;
4628 static const struct seq_operations ftrace_graph_seq_ops = {
4629 .start = g_start,
4630 .next = g_next,
4631 .stop = g_stop,
4632 .show = g_show,
4635 static int
4636 __ftrace_graph_open(struct inode *inode, struct file *file,
4637 struct ftrace_graph_data *fgd)
4639 int ret = 0;
4641 mutex_lock(&graph_lock);
4642 if ((file->f_mode & FMODE_WRITE) &&
4643 (file->f_flags & O_TRUNC)) {
4644 *fgd->count = 0;
4645 memset(fgd->table, 0, fgd->size * sizeof(*fgd->table));
4647 mutex_unlock(&graph_lock);
4649 if (file->f_mode & FMODE_READ) {
4650 ret = seq_open(file, fgd->seq_ops);
4651 if (!ret) {
4652 struct seq_file *m = file->private_data;
4653 m->private = fgd;
4655 } else
4656 file->private_data = fgd;
4658 return ret;
4661 static int
4662 ftrace_graph_open(struct inode *inode, struct file *file)
4664 struct ftrace_graph_data *fgd;
4666 if (unlikely(ftrace_disabled))
4667 return -ENODEV;
4669 fgd = kmalloc(sizeof(*fgd), GFP_KERNEL);
4670 if (fgd == NULL)
4671 return -ENOMEM;
4673 fgd->table = ftrace_graph_funcs;
4674 fgd->size = FTRACE_GRAPH_MAX_FUNCS;
4675 fgd->count = &ftrace_graph_count;
4676 fgd->seq_ops = &ftrace_graph_seq_ops;
4678 return __ftrace_graph_open(inode, file, fgd);
4681 static int
4682 ftrace_graph_notrace_open(struct inode *inode, struct file *file)
4684 struct ftrace_graph_data *fgd;
4686 if (unlikely(ftrace_disabled))
4687 return -ENODEV;
4689 fgd = kmalloc(sizeof(*fgd), GFP_KERNEL);
4690 if (fgd == NULL)
4691 return -ENOMEM;
4693 fgd->table = ftrace_graph_notrace_funcs;
4694 fgd->size = FTRACE_GRAPH_MAX_FUNCS;
4695 fgd->count = &ftrace_graph_notrace_count;
4696 fgd->seq_ops = &ftrace_graph_seq_ops;
4698 return __ftrace_graph_open(inode, file, fgd);
4701 static int
4702 ftrace_graph_release(struct inode *inode, struct file *file)
4704 if (file->f_mode & FMODE_READ) {
4705 struct seq_file *m = file->private_data;
4707 kfree(m->private);
4708 seq_release(inode, file);
4709 } else {
4710 kfree(file->private_data);
4713 return 0;
4716 static int
4717 ftrace_set_func(unsigned long *array, int *idx, int size, char *buffer)
4719 struct ftrace_glob func_g;
4720 struct dyn_ftrace *rec;
4721 struct ftrace_page *pg;
4722 int fail = 1;
4723 int not;
4724 bool exists;
4725 int i;
4727 /* decode regex */
4728 func_g.type = filter_parse_regex(buffer, strlen(buffer),
4729 &func_g.search, &not);
4730 if (!not && *idx >= size)
4731 return -EBUSY;
4733 func_g.len = strlen(func_g.search);
4735 mutex_lock(&ftrace_lock);
4737 if (unlikely(ftrace_disabled)) {
4738 mutex_unlock(&ftrace_lock);
4739 return -ENODEV;
4742 do_for_each_ftrace_rec(pg, rec) {
4744 if (rec->flags & FTRACE_FL_DISABLED)
4745 continue;
4747 if (ftrace_match_record(rec, &func_g, NULL, 0)) {
4748 /* if it is in the array */
4749 exists = false;
4750 for (i = 0; i < *idx; i++) {
4751 if (array[i] == rec->ip) {
4752 exists = true;
4753 break;
4757 if (!not) {
4758 fail = 0;
4759 if (!exists) {
4760 array[(*idx)++] = rec->ip;
4761 if (*idx >= size)
4762 goto out;
4764 } else {
4765 if (exists) {
4766 array[i] = array[--(*idx)];
4767 array[*idx] = 0;
4768 fail = 0;
4772 } while_for_each_ftrace_rec();
4773 out:
4774 mutex_unlock(&ftrace_lock);
4776 if (fail)
4777 return -EINVAL;
4779 return 0;
4782 static ssize_t
4783 ftrace_graph_write(struct file *file, const char __user *ubuf,
4784 size_t cnt, loff_t *ppos)
4786 struct trace_parser parser;
4787 ssize_t read, ret = 0;
4788 struct ftrace_graph_data *fgd = file->private_data;
4790 if (!cnt)
4791 return 0;
4793 if (trace_parser_get_init(&parser, FTRACE_BUFF_MAX))
4794 return -ENOMEM;
4796 read = trace_get_user(&parser, ubuf, cnt, ppos);
4798 if (read >= 0 && trace_parser_loaded((&parser))) {
4799 parser.buffer[parser.idx] = 0;
4801 mutex_lock(&graph_lock);
4803 /* we allow only one expression at a time */
4804 ret = ftrace_set_func(fgd->table, fgd->count, fgd->size,
4805 parser.buffer);
4807 mutex_unlock(&graph_lock);
4810 if (!ret)
4811 ret = read;
4813 trace_parser_put(&parser);
4815 return ret;
4818 static const struct file_operations ftrace_graph_fops = {
4819 .open = ftrace_graph_open,
4820 .read = seq_read,
4821 .write = ftrace_graph_write,
4822 .llseek = tracing_lseek,
4823 .release = ftrace_graph_release,
4826 static const struct file_operations ftrace_graph_notrace_fops = {
4827 .open = ftrace_graph_notrace_open,
4828 .read = seq_read,
4829 .write = ftrace_graph_write,
4830 .llseek = tracing_lseek,
4831 .release = ftrace_graph_release,
4833 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
4835 void ftrace_create_filter_files(struct ftrace_ops *ops,
4836 struct dentry *parent)
4839 trace_create_file("set_ftrace_filter", 0644, parent,
4840 ops, &ftrace_filter_fops);
4842 trace_create_file("set_ftrace_notrace", 0644, parent,
4843 ops, &ftrace_notrace_fops);
4847 * The name "destroy_filter_files" is really a misnomer. Although
4848 * in the future, it may actualy delete the files, but this is
4849 * really intended to make sure the ops passed in are disabled
4850 * and that when this function returns, the caller is free to
4851 * free the ops.
4853 * The "destroy" name is only to match the "create" name that this
4854 * should be paired with.
4856 void ftrace_destroy_filter_files(struct ftrace_ops *ops)
4858 mutex_lock(&ftrace_lock);
4859 if (ops->flags & FTRACE_OPS_FL_ENABLED)
4860 ftrace_shutdown(ops, 0);
4861 ops->flags |= FTRACE_OPS_FL_DELETED;
4862 ftrace_free_filter(ops);
4863 mutex_unlock(&ftrace_lock);
4866 static __init int ftrace_init_dyn_tracefs(struct dentry *d_tracer)
4869 trace_create_file("available_filter_functions", 0444,
4870 d_tracer, NULL, &ftrace_avail_fops);
4872 trace_create_file("enabled_functions", 0444,
4873 d_tracer, NULL, &ftrace_enabled_fops);
4875 ftrace_create_filter_files(&global_ops, d_tracer);
4877 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
4878 trace_create_file("set_graph_function", 0444, d_tracer,
4879 NULL,
4880 &ftrace_graph_fops);
4881 trace_create_file("set_graph_notrace", 0444, d_tracer,
4882 NULL,
4883 &ftrace_graph_notrace_fops);
4884 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
4886 return 0;
4889 static int ftrace_cmp_ips(const void *a, const void *b)
4891 const unsigned long *ipa = a;
4892 const unsigned long *ipb = b;
4894 if (*ipa > *ipb)
4895 return 1;
4896 if (*ipa < *ipb)
4897 return -1;
4898 return 0;
4901 static int ftrace_process_locs(struct module *mod,
4902 unsigned long *start,
4903 unsigned long *end)
4905 struct ftrace_page *start_pg;
4906 struct ftrace_page *pg;
4907 struct dyn_ftrace *rec;
4908 unsigned long count;
4909 unsigned long *p;
4910 unsigned long addr;
4911 unsigned long flags = 0; /* Shut up gcc */
4912 int ret = -ENOMEM;
4914 count = end - start;
4916 if (!count)
4917 return 0;
4919 sort(start, count, sizeof(*start),
4920 ftrace_cmp_ips, NULL);
4922 start_pg = ftrace_allocate_pages(count);
4923 if (!start_pg)
4924 return -ENOMEM;
4926 mutex_lock(&ftrace_lock);
4929 * Core and each module needs their own pages, as
4930 * modules will free them when they are removed.
4931 * Force a new page to be allocated for modules.
4933 if (!mod) {
4934 WARN_ON(ftrace_pages || ftrace_pages_start);
4935 /* First initialization */
4936 ftrace_pages = ftrace_pages_start = start_pg;
4937 } else {
4938 if (!ftrace_pages)
4939 goto out;
4941 if (WARN_ON(ftrace_pages->next)) {
4942 /* Hmm, we have free pages? */
4943 while (ftrace_pages->next)
4944 ftrace_pages = ftrace_pages->next;
4947 ftrace_pages->next = start_pg;
4950 p = start;
4951 pg = start_pg;
4952 while (p < end) {
4953 addr = ftrace_call_adjust(*p++);
4955 * Some architecture linkers will pad between
4956 * the different mcount_loc sections of different
4957 * object files to satisfy alignments.
4958 * Skip any NULL pointers.
4960 if (!addr)
4961 continue;
4963 if (pg->index == pg->size) {
4964 /* We should have allocated enough */
4965 if (WARN_ON(!pg->next))
4966 break;
4967 pg = pg->next;
4970 rec = &pg->records[pg->index++];
4971 rec->ip = addr;
4974 /* We should have used all pages */
4975 WARN_ON(pg->next);
4977 /* Assign the last page to ftrace_pages */
4978 ftrace_pages = pg;
4981 * We only need to disable interrupts on start up
4982 * because we are modifying code that an interrupt
4983 * may execute, and the modification is not atomic.
4984 * But for modules, nothing runs the code we modify
4985 * until we are finished with it, and there's no
4986 * reason to cause large interrupt latencies while we do it.
4988 if (!mod)
4989 local_irq_save(flags);
4990 ftrace_update_code(mod, start_pg);
4991 if (!mod)
4992 local_irq_restore(flags);
4993 ret = 0;
4994 out:
4995 mutex_unlock(&ftrace_lock);
4997 return ret;
5000 #ifdef CONFIG_MODULES
5002 #define next_to_ftrace_page(p) container_of(p, struct ftrace_page, next)
5004 static int referenced_filters(struct dyn_ftrace *rec)
5006 struct ftrace_ops *ops;
5007 int cnt = 0;
5009 for (ops = ftrace_ops_list; ops != &ftrace_list_end; ops = ops->next) {
5010 if (ops_references_rec(ops, rec)) {
5011 cnt++;
5012 if (ops->flags & FTRACE_OPS_FL_SAVE_REGS)
5013 rec->flags |= FTRACE_FL_REGS;
5017 return cnt;
5020 void ftrace_release_mod(struct module *mod)
5022 struct dyn_ftrace *rec;
5023 struct ftrace_page **last_pg;
5024 struct ftrace_page *pg;
5025 int order;
5027 mutex_lock(&ftrace_lock);
5029 if (ftrace_disabled)
5030 goto out_unlock;
5033 * Each module has its own ftrace_pages, remove
5034 * them from the list.
5036 last_pg = &ftrace_pages_start;
5037 for (pg = ftrace_pages_start; pg; pg = *last_pg) {
5038 rec = &pg->records[0];
5039 if (within_module_core(rec->ip, mod)) {
5041 * As core pages are first, the first
5042 * page should never be a module page.
5044 if (WARN_ON(pg == ftrace_pages_start))
5045 goto out_unlock;
5047 /* Check if we are deleting the last page */
5048 if (pg == ftrace_pages)
5049 ftrace_pages = next_to_ftrace_page(last_pg);
5051 *last_pg = pg->next;
5052 order = get_count_order(pg->size / ENTRIES_PER_PAGE);
5053 free_pages((unsigned long)pg->records, order);
5054 kfree(pg);
5055 } else
5056 last_pg = &pg->next;
5058 out_unlock:
5059 mutex_unlock(&ftrace_lock);
5062 void ftrace_module_enable(struct module *mod)
5064 struct dyn_ftrace *rec;
5065 struct ftrace_page *pg;
5067 mutex_lock(&ftrace_lock);
5069 if (ftrace_disabled)
5070 goto out_unlock;
5073 * If the tracing is enabled, go ahead and enable the record.
5075 * The reason not to enable the record immediatelly is the
5076 * inherent check of ftrace_make_nop/ftrace_make_call for
5077 * correct previous instructions. Making first the NOP
5078 * conversion puts the module to the correct state, thus
5079 * passing the ftrace_make_call check.
5081 * We also delay this to after the module code already set the
5082 * text to read-only, as we now need to set it back to read-write
5083 * so that we can modify the text.
5085 if (ftrace_start_up)
5086 ftrace_arch_code_modify_prepare();
5088 do_for_each_ftrace_rec(pg, rec) {
5089 int cnt;
5091 * do_for_each_ftrace_rec() is a double loop.
5092 * module text shares the pg. If a record is
5093 * not part of this module, then skip this pg,
5094 * which the "break" will do.
5096 if (!within_module_core(rec->ip, mod))
5097 break;
5099 cnt = 0;
5102 * When adding a module, we need to check if tracers are
5103 * currently enabled and if they are, and can trace this record,
5104 * we need to enable the module functions as well as update the
5105 * reference counts for those function records.
5107 if (ftrace_start_up)
5108 cnt += referenced_filters(rec);
5110 rec->flags &= ~FTRACE_FL_DISABLED;
5111 rec->flags += cnt;
5113 if (ftrace_start_up && cnt) {
5114 int failed = __ftrace_replace_code(rec, 1);
5115 if (failed) {
5116 ftrace_bug(failed, rec);
5117 goto out_loop;
5121 } while_for_each_ftrace_rec();
5123 out_loop:
5124 if (ftrace_start_up)
5125 ftrace_arch_code_modify_post_process();
5127 out_unlock:
5128 mutex_unlock(&ftrace_lock);
5131 void ftrace_module_init(struct module *mod)
5133 if (ftrace_disabled || !mod->num_ftrace_callsites)
5134 return;
5136 ftrace_process_locs(mod, mod->ftrace_callsites,
5137 mod->ftrace_callsites + mod->num_ftrace_callsites);
5139 #endif /* CONFIG_MODULES */
5141 void __init ftrace_init(void)
5143 extern unsigned long __start_mcount_loc[];
5144 extern unsigned long __stop_mcount_loc[];
5145 unsigned long count, flags;
5146 int ret;
5148 local_irq_save(flags);
5149 ret = ftrace_dyn_arch_init();
5150 local_irq_restore(flags);
5151 if (ret)
5152 goto failed;
5154 count = __stop_mcount_loc - __start_mcount_loc;
5155 if (!count) {
5156 pr_info("ftrace: No functions to be traced?\n");
5157 goto failed;
5160 pr_info("ftrace: allocating %ld entries in %ld pages\n",
5161 count, count / ENTRIES_PER_PAGE + 1);
5163 last_ftrace_enabled = ftrace_enabled = 1;
5165 ret = ftrace_process_locs(NULL,
5166 __start_mcount_loc,
5167 __stop_mcount_loc);
5169 set_ftrace_early_filters();
5171 return;
5172 failed:
5173 ftrace_disabled = 1;
5176 /* Do nothing if arch does not support this */
5177 void __weak arch_ftrace_update_trampoline(struct ftrace_ops *ops)
5181 static void ftrace_update_trampoline(struct ftrace_ops *ops)
5185 * Currently there's no safe way to free a trampoline when the kernel
5186 * is configured with PREEMPT. That is because a task could be preempted
5187 * when it jumped to the trampoline, it may be preempted for a long time
5188 * depending on the system load, and currently there's no way to know
5189 * when it will be off the trampoline. If the trampoline is freed
5190 * too early, when the task runs again, it will be executing on freed
5191 * memory and crash.
5193 #ifdef CONFIG_PREEMPT
5194 /* Currently, only non dynamic ops can have a trampoline */
5195 if (ops->flags & FTRACE_OPS_FL_DYNAMIC)
5196 return;
5197 #endif
5199 arch_ftrace_update_trampoline(ops);
5202 #else
5204 static struct ftrace_ops global_ops = {
5205 .func = ftrace_stub,
5206 .flags = FTRACE_OPS_FL_RECURSION_SAFE |
5207 FTRACE_OPS_FL_INITIALIZED |
5208 FTRACE_OPS_FL_PID,
5211 static int __init ftrace_nodyn_init(void)
5213 ftrace_enabled = 1;
5214 return 0;
5216 core_initcall(ftrace_nodyn_init);
5218 static inline int ftrace_init_dyn_tracefs(struct dentry *d_tracer) { return 0; }
5219 static inline void ftrace_startup_enable(int command) { }
5220 static inline void ftrace_startup_all(int command) { }
5221 /* Keep as macros so we do not need to define the commands */
5222 # define ftrace_startup(ops, command) \
5223 ({ \
5224 int ___ret = __register_ftrace_function(ops); \
5225 if (!___ret) \
5226 (ops)->flags |= FTRACE_OPS_FL_ENABLED; \
5227 ___ret; \
5229 # define ftrace_shutdown(ops, command) \
5230 ({ \
5231 int ___ret = __unregister_ftrace_function(ops); \
5232 if (!___ret) \
5233 (ops)->flags &= ~FTRACE_OPS_FL_ENABLED; \
5234 ___ret; \
5237 # define ftrace_startup_sysctl() do { } while (0)
5238 # define ftrace_shutdown_sysctl() do { } while (0)
5240 static inline int
5241 ftrace_ops_test(struct ftrace_ops *ops, unsigned long ip, void *regs)
5243 return 1;
5246 static void ftrace_update_trampoline(struct ftrace_ops *ops)
5250 #endif /* CONFIG_DYNAMIC_FTRACE */
5252 __init void ftrace_init_global_array_ops(struct trace_array *tr)
5254 tr->ops = &global_ops;
5255 tr->ops->private = tr;
5258 void ftrace_init_array_ops(struct trace_array *tr, ftrace_func_t func)
5260 /* If we filter on pids, update to use the pid function */
5261 if (tr->flags & TRACE_ARRAY_FL_GLOBAL) {
5262 if (WARN_ON(tr->ops->func != ftrace_stub))
5263 printk("ftrace ops had %pS for function\n",
5264 tr->ops->func);
5266 tr->ops->func = func;
5267 tr->ops->private = tr;
5270 void ftrace_reset_array_ops(struct trace_array *tr)
5272 tr->ops->func = ftrace_stub;
5275 static nokprobe_inline void
5276 __ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip,
5277 struct ftrace_ops *ignored, struct pt_regs *regs)
5279 struct ftrace_ops *op;
5280 int bit;
5282 bit = trace_test_and_set_recursion(TRACE_LIST_START, TRACE_LIST_MAX);
5283 if (bit < 0)
5284 return;
5287 * Some of the ops may be dynamically allocated,
5288 * they must be freed after a synchronize_sched().
5290 preempt_disable_notrace();
5292 do_for_each_ftrace_op(op, ftrace_ops_list) {
5294 * Check the following for each ops before calling their func:
5295 * if RCU flag is set, then rcu_is_watching() must be true
5296 * if PER_CPU is set, then ftrace_function_local_disable()
5297 * must be false
5298 * Otherwise test if the ip matches the ops filter
5300 * If any of the above fails then the op->func() is not executed.
5302 if ((!(op->flags & FTRACE_OPS_FL_RCU) || rcu_is_watching()) &&
5303 (!(op->flags & FTRACE_OPS_FL_PER_CPU) ||
5304 !ftrace_function_local_disabled(op)) &&
5305 ftrace_ops_test(op, ip, regs)) {
5307 if (FTRACE_WARN_ON(!op->func)) {
5308 pr_warn("op=%p %pS\n", op, op);
5309 goto out;
5311 op->func(ip, parent_ip, op, regs);
5313 } while_for_each_ftrace_op(op);
5314 out:
5315 preempt_enable_notrace();
5316 trace_clear_recursion(bit);
5320 * Some archs only support passing ip and parent_ip. Even though
5321 * the list function ignores the op parameter, we do not want any
5322 * C side effects, where a function is called without the caller
5323 * sending a third parameter.
5324 * Archs are to support both the regs and ftrace_ops at the same time.
5325 * If they support ftrace_ops, it is assumed they support regs.
5326 * If call backs want to use regs, they must either check for regs
5327 * being NULL, or CONFIG_DYNAMIC_FTRACE_WITH_REGS.
5328 * Note, CONFIG_DYNAMIC_FTRACE_WITH_REGS expects a full regs to be saved.
5329 * An architecture can pass partial regs with ftrace_ops and still
5330 * set the ARCH_SUPPORTS_FTRACE_OPS.
5332 #if ARCH_SUPPORTS_FTRACE_OPS
5333 static void ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip,
5334 struct ftrace_ops *op, struct pt_regs *regs)
5336 __ftrace_ops_list_func(ip, parent_ip, NULL, regs);
5338 NOKPROBE_SYMBOL(ftrace_ops_list_func);
5339 #else
5340 static void ftrace_ops_no_ops(unsigned long ip, unsigned long parent_ip)
5342 __ftrace_ops_list_func(ip, parent_ip, NULL, NULL);
5344 NOKPROBE_SYMBOL(ftrace_ops_no_ops);
5345 #endif
5348 * If there's only one function registered but it does not support
5349 * recursion, needs RCU protection and/or requires per cpu handling, then
5350 * this function will be called by the mcount trampoline.
5352 static void ftrace_ops_assist_func(unsigned long ip, unsigned long parent_ip,
5353 struct ftrace_ops *op, struct pt_regs *regs)
5355 int bit;
5357 bit = trace_test_and_set_recursion(TRACE_LIST_START, TRACE_LIST_MAX);
5358 if (bit < 0)
5359 return;
5361 preempt_disable_notrace();
5363 if ((!(op->flags & FTRACE_OPS_FL_RCU) || rcu_is_watching()) &&
5364 (!(op->flags & FTRACE_OPS_FL_PER_CPU) ||
5365 !ftrace_function_local_disabled(op))) {
5366 op->func(ip, parent_ip, op, regs);
5369 preempt_enable_notrace();
5370 trace_clear_recursion(bit);
5372 NOKPROBE_SYMBOL(ftrace_ops_assist_func);
5375 * ftrace_ops_get_func - get the function a trampoline should call
5376 * @ops: the ops to get the function for
5378 * Normally the mcount trampoline will call the ops->func, but there
5379 * are times that it should not. For example, if the ops does not
5380 * have its own recursion protection, then it should call the
5381 * ftrace_ops_recurs_func() instead.
5383 * Returns the function that the trampoline should call for @ops.
5385 ftrace_func_t ftrace_ops_get_func(struct ftrace_ops *ops)
5388 * If the function does not handle recursion, needs to be RCU safe,
5389 * or does per cpu logic, then we need to call the assist handler.
5391 if (!(ops->flags & FTRACE_OPS_FL_RECURSION_SAFE) ||
5392 ops->flags & (FTRACE_OPS_FL_RCU | FTRACE_OPS_FL_PER_CPU))
5393 return ftrace_ops_assist_func;
5395 return ops->func;
5398 static void
5399 ftrace_filter_pid_sched_switch_probe(void *data, bool preempt,
5400 struct task_struct *prev, struct task_struct *next)
5402 struct trace_array *tr = data;
5403 struct trace_pid_list *pid_list;
5405 pid_list = rcu_dereference_sched(tr->function_pids);
5407 this_cpu_write(tr->trace_buffer.data->ftrace_ignore_pid,
5408 trace_ignore_this_task(pid_list, next));
5411 static void clear_ftrace_pids(struct trace_array *tr)
5413 struct trace_pid_list *pid_list;
5414 int cpu;
5416 pid_list = rcu_dereference_protected(tr->function_pids,
5417 lockdep_is_held(&ftrace_lock));
5418 if (!pid_list)
5419 return;
5421 unregister_trace_sched_switch(ftrace_filter_pid_sched_switch_probe, tr);
5423 for_each_possible_cpu(cpu)
5424 per_cpu_ptr(tr->trace_buffer.data, cpu)->ftrace_ignore_pid = false;
5426 rcu_assign_pointer(tr->function_pids, NULL);
5428 /* Wait till all users are no longer using pid filtering */
5429 synchronize_sched();
5431 trace_free_pid_list(pid_list);
5434 void ftrace_clear_pids(struct trace_array *tr)
5436 mutex_lock(&ftrace_lock);
5438 clear_ftrace_pids(tr);
5440 mutex_unlock(&ftrace_lock);
5443 static void ftrace_pid_reset(struct trace_array *tr)
5445 mutex_lock(&ftrace_lock);
5446 clear_ftrace_pids(tr);
5448 ftrace_update_pid_func();
5449 ftrace_startup_all(0);
5451 mutex_unlock(&ftrace_lock);
5454 /* Greater than any max PID */
5455 #define FTRACE_NO_PIDS (void *)(PID_MAX_LIMIT + 1)
5457 static void *fpid_start(struct seq_file *m, loff_t *pos)
5458 __acquires(RCU)
5460 struct trace_pid_list *pid_list;
5461 struct trace_array *tr = m->private;
5463 mutex_lock(&ftrace_lock);
5464 rcu_read_lock_sched();
5466 pid_list = rcu_dereference_sched(tr->function_pids);
5468 if (!pid_list)
5469 return !(*pos) ? FTRACE_NO_PIDS : NULL;
5471 return trace_pid_start(pid_list, pos);
5474 static void *fpid_next(struct seq_file *m, void *v, loff_t *pos)
5476 struct trace_array *tr = m->private;
5477 struct trace_pid_list *pid_list = rcu_dereference_sched(tr->function_pids);
5479 if (v == FTRACE_NO_PIDS) {
5480 (*pos)++;
5481 return NULL;
5483 return trace_pid_next(pid_list, v, pos);
5486 static void fpid_stop(struct seq_file *m, void *p)
5487 __releases(RCU)
5489 rcu_read_unlock_sched();
5490 mutex_unlock(&ftrace_lock);
5493 static int fpid_show(struct seq_file *m, void *v)
5495 if (v == FTRACE_NO_PIDS) {
5496 seq_puts(m, "no pid\n");
5497 return 0;
5500 return trace_pid_show(m, v);
5503 static const struct seq_operations ftrace_pid_sops = {
5504 .start = fpid_start,
5505 .next = fpid_next,
5506 .stop = fpid_stop,
5507 .show = fpid_show,
5510 static int
5511 ftrace_pid_open(struct inode *inode, struct file *file)
5513 struct trace_array *tr = inode->i_private;
5514 struct seq_file *m;
5515 int ret = 0;
5517 if (trace_array_get(tr) < 0)
5518 return -ENODEV;
5520 if ((file->f_mode & FMODE_WRITE) &&
5521 (file->f_flags & O_TRUNC))
5522 ftrace_pid_reset(tr);
5524 ret = seq_open(file, &ftrace_pid_sops);
5525 if (ret < 0) {
5526 trace_array_put(tr);
5527 } else {
5528 m = file->private_data;
5529 /* copy tr over to seq ops */
5530 m->private = tr;
5533 return ret;
5536 static void ignore_task_cpu(void *data)
5538 struct trace_array *tr = data;
5539 struct trace_pid_list *pid_list;
5542 * This function is called by on_each_cpu() while the
5543 * event_mutex is held.
5545 pid_list = rcu_dereference_protected(tr->function_pids,
5546 mutex_is_locked(&ftrace_lock));
5548 this_cpu_write(tr->trace_buffer.data->ftrace_ignore_pid,
5549 trace_ignore_this_task(pid_list, current));
5552 static ssize_t
5553 ftrace_pid_write(struct file *filp, const char __user *ubuf,
5554 size_t cnt, loff_t *ppos)
5556 struct seq_file *m = filp->private_data;
5557 struct trace_array *tr = m->private;
5558 struct trace_pid_list *filtered_pids = NULL;
5559 struct trace_pid_list *pid_list;
5560 ssize_t ret;
5562 if (!cnt)
5563 return 0;
5565 mutex_lock(&ftrace_lock);
5567 filtered_pids = rcu_dereference_protected(tr->function_pids,
5568 lockdep_is_held(&ftrace_lock));
5570 ret = trace_pid_write(filtered_pids, &pid_list, ubuf, cnt);
5571 if (ret < 0)
5572 goto out;
5574 rcu_assign_pointer(tr->function_pids, pid_list);
5576 if (filtered_pids) {
5577 synchronize_sched();
5578 trace_free_pid_list(filtered_pids);
5579 } else if (pid_list) {
5580 /* Register a probe to set whether to ignore the tracing of a task */
5581 register_trace_sched_switch(ftrace_filter_pid_sched_switch_probe, tr);
5585 * Ignoring of pids is done at task switch. But we have to
5586 * check for those tasks that are currently running.
5587 * Always do this in case a pid was appended or removed.
5589 on_each_cpu(ignore_task_cpu, tr, 1);
5591 ftrace_update_pid_func();
5592 ftrace_startup_all(0);
5593 out:
5594 mutex_unlock(&ftrace_lock);
5596 if (ret > 0)
5597 *ppos += ret;
5599 return ret;
5602 static int
5603 ftrace_pid_release(struct inode *inode, struct file *file)
5605 struct trace_array *tr = inode->i_private;
5607 trace_array_put(tr);
5609 return seq_release(inode, file);
5612 static const struct file_operations ftrace_pid_fops = {
5613 .open = ftrace_pid_open,
5614 .write = ftrace_pid_write,
5615 .read = seq_read,
5616 .llseek = tracing_lseek,
5617 .release = ftrace_pid_release,
5620 void ftrace_init_tracefs(struct trace_array *tr, struct dentry *d_tracer)
5622 trace_create_file("set_ftrace_pid", 0644, d_tracer,
5623 tr, &ftrace_pid_fops);
5626 void __init ftrace_init_tracefs_toplevel(struct trace_array *tr,
5627 struct dentry *d_tracer)
5629 /* Only the top level directory has the dyn_tracefs and profile */
5630 WARN_ON(!(tr->flags & TRACE_ARRAY_FL_GLOBAL));
5632 ftrace_init_dyn_tracefs(d_tracer);
5633 ftrace_profile_tracefs(d_tracer);
5637 * ftrace_kill - kill ftrace
5639 * This function should be used by panic code. It stops ftrace
5640 * but in a not so nice way. If you need to simply kill ftrace
5641 * from a non-atomic section, use ftrace_kill.
5643 void ftrace_kill(void)
5645 ftrace_disabled = 1;
5646 ftrace_enabled = 0;
5647 clear_ftrace_function();
5651 * Test if ftrace is dead or not.
5653 int ftrace_is_dead(void)
5655 return ftrace_disabled;
5659 * register_ftrace_function - register a function for profiling
5660 * @ops - ops structure that holds the function for profiling.
5662 * Register a function to be called by all functions in the
5663 * kernel.
5665 * Note: @ops->func and all the functions it calls must be labeled
5666 * with "notrace", otherwise it will go into a
5667 * recursive loop.
5669 int register_ftrace_function(struct ftrace_ops *ops)
5671 int ret = -1;
5673 ftrace_ops_init(ops);
5675 mutex_lock(&ftrace_lock);
5677 ret = ftrace_startup(ops, 0);
5679 mutex_unlock(&ftrace_lock);
5681 return ret;
5683 EXPORT_SYMBOL_GPL(register_ftrace_function);
5686 * unregister_ftrace_function - unregister a function for profiling.
5687 * @ops - ops structure that holds the function to unregister
5689 * Unregister a function that was added to be called by ftrace profiling.
5691 int unregister_ftrace_function(struct ftrace_ops *ops)
5693 int ret;
5695 mutex_lock(&ftrace_lock);
5696 ret = ftrace_shutdown(ops, 0);
5697 mutex_unlock(&ftrace_lock);
5699 return ret;
5701 EXPORT_SYMBOL_GPL(unregister_ftrace_function);
5704 ftrace_enable_sysctl(struct ctl_table *table, int write,
5705 void __user *buffer, size_t *lenp,
5706 loff_t *ppos)
5708 int ret = -ENODEV;
5710 mutex_lock(&ftrace_lock);
5712 if (unlikely(ftrace_disabled))
5713 goto out;
5715 ret = proc_dointvec(table, write, buffer, lenp, ppos);
5717 if (ret || !write || (last_ftrace_enabled == !!ftrace_enabled))
5718 goto out;
5720 last_ftrace_enabled = !!ftrace_enabled;
5722 if (ftrace_enabled) {
5724 /* we are starting ftrace again */
5725 if (ftrace_ops_list != &ftrace_list_end)
5726 update_ftrace_function();
5728 ftrace_startup_sysctl();
5730 } else {
5731 /* stopping ftrace calls (just send to ftrace_stub) */
5732 ftrace_trace_function = ftrace_stub;
5734 ftrace_shutdown_sysctl();
5737 out:
5738 mutex_unlock(&ftrace_lock);
5739 return ret;
5742 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
5744 static struct ftrace_ops graph_ops = {
5745 .func = ftrace_stub,
5746 .flags = FTRACE_OPS_FL_RECURSION_SAFE |
5747 FTRACE_OPS_FL_INITIALIZED |
5748 FTRACE_OPS_FL_PID |
5749 FTRACE_OPS_FL_STUB,
5750 #ifdef FTRACE_GRAPH_TRAMP_ADDR
5751 .trampoline = FTRACE_GRAPH_TRAMP_ADDR,
5752 /* trampoline_size is only needed for dynamically allocated tramps */
5753 #endif
5754 ASSIGN_OPS_HASH(graph_ops, &global_ops.local_hash)
5757 void ftrace_graph_sleep_time_control(bool enable)
5759 fgraph_sleep_time = enable;
5762 void ftrace_graph_graph_time_control(bool enable)
5764 fgraph_graph_time = enable;
5767 int ftrace_graph_entry_stub(struct ftrace_graph_ent *trace)
5769 return 0;
5772 /* The callbacks that hook a function */
5773 trace_func_graph_ret_t ftrace_graph_return =
5774 (trace_func_graph_ret_t)ftrace_stub;
5775 trace_func_graph_ent_t ftrace_graph_entry = ftrace_graph_entry_stub;
5776 static trace_func_graph_ent_t __ftrace_graph_entry = ftrace_graph_entry_stub;
5778 /* Try to assign a return stack array on FTRACE_RETSTACK_ALLOC_SIZE tasks. */
5779 static int alloc_retstack_tasklist(struct ftrace_ret_stack **ret_stack_list)
5781 int i;
5782 int ret = 0;
5783 int start = 0, end = FTRACE_RETSTACK_ALLOC_SIZE;
5784 struct task_struct *g, *t;
5786 for (i = 0; i < FTRACE_RETSTACK_ALLOC_SIZE; i++) {
5787 ret_stack_list[i] = kmalloc(FTRACE_RETFUNC_DEPTH
5788 * sizeof(struct ftrace_ret_stack),
5789 GFP_KERNEL);
5790 if (!ret_stack_list[i]) {
5791 start = 0;
5792 end = i;
5793 ret = -ENOMEM;
5794 goto free;
5798 read_lock(&tasklist_lock);
5799 do_each_thread(g, t) {
5800 if (start == end) {
5801 ret = -EAGAIN;
5802 goto unlock;
5805 if (t->ret_stack == NULL) {
5806 atomic_set(&t->tracing_graph_pause, 0);
5807 atomic_set(&t->trace_overrun, 0);
5808 t->curr_ret_stack = -1;
5809 /* Make sure the tasks see the -1 first: */
5810 smp_wmb();
5811 t->ret_stack = ret_stack_list[start++];
5813 } while_each_thread(g, t);
5815 unlock:
5816 read_unlock(&tasklist_lock);
5817 free:
5818 for (i = start; i < end; i++)
5819 kfree(ret_stack_list[i]);
5820 return ret;
5823 static void
5824 ftrace_graph_probe_sched_switch(void *ignore, bool preempt,
5825 struct task_struct *prev, struct task_struct *next)
5827 unsigned long long timestamp;
5828 int index;
5831 * Does the user want to count the time a function was asleep.
5832 * If so, do not update the time stamps.
5834 if (fgraph_sleep_time)
5835 return;
5837 timestamp = trace_clock_local();
5839 prev->ftrace_timestamp = timestamp;
5841 /* only process tasks that we timestamped */
5842 if (!next->ftrace_timestamp)
5843 return;
5846 * Update all the counters in next to make up for the
5847 * time next was sleeping.
5849 timestamp -= next->ftrace_timestamp;
5851 for (index = next->curr_ret_stack; index >= 0; index--)
5852 next->ret_stack[index].calltime += timestamp;
5855 /* Allocate a return stack for each task */
5856 static int start_graph_tracing(void)
5858 struct ftrace_ret_stack **ret_stack_list;
5859 int ret, cpu;
5861 ret_stack_list = kmalloc(FTRACE_RETSTACK_ALLOC_SIZE *
5862 sizeof(struct ftrace_ret_stack *),
5863 GFP_KERNEL);
5865 if (!ret_stack_list)
5866 return -ENOMEM;
5868 /* The cpu_boot init_task->ret_stack will never be freed */
5869 for_each_online_cpu(cpu) {
5870 if (!idle_task(cpu)->ret_stack)
5871 ftrace_graph_init_idle_task(idle_task(cpu), cpu);
5874 do {
5875 ret = alloc_retstack_tasklist(ret_stack_list);
5876 } while (ret == -EAGAIN);
5878 if (!ret) {
5879 ret = register_trace_sched_switch(ftrace_graph_probe_sched_switch, NULL);
5880 if (ret)
5881 pr_info("ftrace_graph: Couldn't activate tracepoint"
5882 " probe to kernel_sched_switch\n");
5885 kfree(ret_stack_list);
5886 return ret;
5890 * Hibernation protection.
5891 * The state of the current task is too much unstable during
5892 * suspend/restore to disk. We want to protect against that.
5894 static int
5895 ftrace_suspend_notifier_call(struct notifier_block *bl, unsigned long state,
5896 void *unused)
5898 switch (state) {
5899 case PM_HIBERNATION_PREPARE:
5900 pause_graph_tracing();
5901 break;
5903 case PM_POST_HIBERNATION:
5904 unpause_graph_tracing();
5905 break;
5907 return NOTIFY_DONE;
5910 static int ftrace_graph_entry_test(struct ftrace_graph_ent *trace)
5912 if (!ftrace_ops_test(&global_ops, trace->func, NULL))
5913 return 0;
5914 return __ftrace_graph_entry(trace);
5918 * The function graph tracer should only trace the functions defined
5919 * by set_ftrace_filter and set_ftrace_notrace. If another function
5920 * tracer ops is registered, the graph tracer requires testing the
5921 * function against the global ops, and not just trace any function
5922 * that any ftrace_ops registered.
5924 static void update_function_graph_func(void)
5926 struct ftrace_ops *op;
5927 bool do_test = false;
5930 * The graph and global ops share the same set of functions
5931 * to test. If any other ops is on the list, then
5932 * the graph tracing needs to test if its the function
5933 * it should call.
5935 do_for_each_ftrace_op(op, ftrace_ops_list) {
5936 if (op != &global_ops && op != &graph_ops &&
5937 op != &ftrace_list_end) {
5938 do_test = true;
5939 /* in double loop, break out with goto */
5940 goto out;
5942 } while_for_each_ftrace_op(op);
5943 out:
5944 if (do_test)
5945 ftrace_graph_entry = ftrace_graph_entry_test;
5946 else
5947 ftrace_graph_entry = __ftrace_graph_entry;
5950 static struct notifier_block ftrace_suspend_notifier = {
5951 .notifier_call = ftrace_suspend_notifier_call,
5954 int register_ftrace_graph(trace_func_graph_ret_t retfunc,
5955 trace_func_graph_ent_t entryfunc)
5957 int ret = 0;
5959 mutex_lock(&ftrace_lock);
5961 /* we currently allow only one tracer registered at a time */
5962 if (ftrace_graph_active) {
5963 ret = -EBUSY;
5964 goto out;
5967 register_pm_notifier(&ftrace_suspend_notifier);
5969 ftrace_graph_active++;
5970 ret = start_graph_tracing();
5971 if (ret) {
5972 ftrace_graph_active--;
5973 goto out;
5976 ftrace_graph_return = retfunc;
5979 * Update the indirect function to the entryfunc, and the
5980 * function that gets called to the entry_test first. Then
5981 * call the update fgraph entry function to determine if
5982 * the entryfunc should be called directly or not.
5984 __ftrace_graph_entry = entryfunc;
5985 ftrace_graph_entry = ftrace_graph_entry_test;
5986 update_function_graph_func();
5988 ret = ftrace_startup(&graph_ops, FTRACE_START_FUNC_RET);
5989 out:
5990 mutex_unlock(&ftrace_lock);
5991 return ret;
5994 void unregister_ftrace_graph(void)
5996 mutex_lock(&ftrace_lock);
5998 if (unlikely(!ftrace_graph_active))
5999 goto out;
6001 ftrace_graph_active--;
6002 ftrace_graph_return = (trace_func_graph_ret_t)ftrace_stub;
6003 ftrace_graph_entry = ftrace_graph_entry_stub;
6004 __ftrace_graph_entry = ftrace_graph_entry_stub;
6005 ftrace_shutdown(&graph_ops, FTRACE_STOP_FUNC_RET);
6006 unregister_pm_notifier(&ftrace_suspend_notifier);
6007 unregister_trace_sched_switch(ftrace_graph_probe_sched_switch, NULL);
6009 out:
6010 mutex_unlock(&ftrace_lock);
6013 static DEFINE_PER_CPU(struct ftrace_ret_stack *, idle_ret_stack);
6015 static void
6016 graph_init_task(struct task_struct *t, struct ftrace_ret_stack *ret_stack)
6018 atomic_set(&t->tracing_graph_pause, 0);
6019 atomic_set(&t->trace_overrun, 0);
6020 t->ftrace_timestamp = 0;
6021 /* make curr_ret_stack visible before we add the ret_stack */
6022 smp_wmb();
6023 t->ret_stack = ret_stack;
6027 * Allocate a return stack for the idle task. May be the first
6028 * time through, or it may be done by CPU hotplug online.
6030 void ftrace_graph_init_idle_task(struct task_struct *t, int cpu)
6032 t->curr_ret_stack = -1;
6034 * The idle task has no parent, it either has its own
6035 * stack or no stack at all.
6037 if (t->ret_stack)
6038 WARN_ON(t->ret_stack != per_cpu(idle_ret_stack, cpu));
6040 if (ftrace_graph_active) {
6041 struct ftrace_ret_stack *ret_stack;
6043 ret_stack = per_cpu(idle_ret_stack, cpu);
6044 if (!ret_stack) {
6045 ret_stack = kmalloc(FTRACE_RETFUNC_DEPTH
6046 * sizeof(struct ftrace_ret_stack),
6047 GFP_KERNEL);
6048 if (!ret_stack)
6049 return;
6050 per_cpu(idle_ret_stack, cpu) = ret_stack;
6052 graph_init_task(t, ret_stack);
6056 /* Allocate a return stack for newly created task */
6057 void ftrace_graph_init_task(struct task_struct *t)
6059 /* Make sure we do not use the parent ret_stack */
6060 t->ret_stack = NULL;
6061 t->curr_ret_stack = -1;
6063 if (ftrace_graph_active) {
6064 struct ftrace_ret_stack *ret_stack;
6066 ret_stack = kmalloc(FTRACE_RETFUNC_DEPTH
6067 * sizeof(struct ftrace_ret_stack),
6068 GFP_KERNEL);
6069 if (!ret_stack)
6070 return;
6071 graph_init_task(t, ret_stack);
6075 void ftrace_graph_exit_task(struct task_struct *t)
6077 struct ftrace_ret_stack *ret_stack = t->ret_stack;
6079 t->ret_stack = NULL;
6080 /* NULL must become visible to IRQs before we free it: */
6081 barrier();
6083 kfree(ret_stack);
6085 #endif