Linux 4.9.243
[linux/fpc-iii.git] / kernel / trace / ftrace.c
blob51e47e18764e308211e1727cb85d298af9c8b4cb
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_next(struct dyn_ftrace *rec, struct ftrace_ops *ops);
1639 static bool __ftrace_hash_rec_update(struct ftrace_ops *ops,
1640 int filter_hash,
1641 bool inc)
1643 struct ftrace_hash *hash;
1644 struct ftrace_hash *other_hash;
1645 struct ftrace_page *pg;
1646 struct dyn_ftrace *rec;
1647 bool update = false;
1648 int count = 0;
1649 int all = 0;
1651 /* Only update if the ops has been registered */
1652 if (!(ops->flags & FTRACE_OPS_FL_ENABLED))
1653 return false;
1656 * In the filter_hash case:
1657 * If the count is zero, we update all records.
1658 * Otherwise we just update the items in the hash.
1660 * In the notrace_hash case:
1661 * We enable the update in the hash.
1662 * As disabling notrace means enabling the tracing,
1663 * and enabling notrace means disabling, the inc variable
1664 * gets inversed.
1666 if (filter_hash) {
1667 hash = ops->func_hash->filter_hash;
1668 other_hash = ops->func_hash->notrace_hash;
1669 if (ftrace_hash_empty(hash))
1670 all = 1;
1671 } else {
1672 inc = !inc;
1673 hash = ops->func_hash->notrace_hash;
1674 other_hash = ops->func_hash->filter_hash;
1676 * If the notrace hash has no items,
1677 * then there's nothing to do.
1679 if (ftrace_hash_empty(hash))
1680 return false;
1683 do_for_each_ftrace_rec(pg, rec) {
1684 int in_other_hash = 0;
1685 int in_hash = 0;
1686 int match = 0;
1688 if (rec->flags & FTRACE_FL_DISABLED)
1689 continue;
1691 if (all) {
1693 * Only the filter_hash affects all records.
1694 * Update if the record is not in the notrace hash.
1696 if (!other_hash || !ftrace_lookup_ip(other_hash, rec->ip))
1697 match = 1;
1698 } else {
1699 in_hash = !!ftrace_lookup_ip(hash, rec->ip);
1700 in_other_hash = !!ftrace_lookup_ip(other_hash, rec->ip);
1703 * If filter_hash is set, we want to match all functions
1704 * that are in the hash but not in the other hash.
1706 * If filter_hash is not set, then we are decrementing.
1707 * That means we match anything that is in the hash
1708 * and also in the other_hash. That is, we need to turn
1709 * off functions in the other hash because they are disabled
1710 * by this hash.
1712 if (filter_hash && in_hash && !in_other_hash)
1713 match = 1;
1714 else if (!filter_hash && in_hash &&
1715 (in_other_hash || ftrace_hash_empty(other_hash)))
1716 match = 1;
1718 if (!match)
1719 continue;
1721 if (inc) {
1722 rec->flags++;
1723 if (FTRACE_WARN_ON(ftrace_rec_count(rec) == FTRACE_REF_MAX))
1724 return false;
1727 * If there's only a single callback registered to a
1728 * function, and the ops has a trampoline registered
1729 * for it, then we can call it directly.
1731 if (ftrace_rec_count(rec) == 1 && ops->trampoline)
1732 rec->flags |= FTRACE_FL_TRAMP;
1733 else
1735 * If we are adding another function callback
1736 * to this function, and the previous had a
1737 * custom trampoline in use, then we need to go
1738 * back to the default trampoline.
1740 rec->flags &= ~FTRACE_FL_TRAMP;
1743 * If any ops wants regs saved for this function
1744 * then all ops will get saved regs.
1746 if (ops->flags & FTRACE_OPS_FL_SAVE_REGS)
1747 rec->flags |= FTRACE_FL_REGS;
1748 } else {
1749 if (FTRACE_WARN_ON(ftrace_rec_count(rec) == 0))
1750 return false;
1751 rec->flags--;
1754 * If the rec had REGS enabled and the ops that is
1755 * being removed had REGS set, then see if there is
1756 * still any ops for this record that wants regs.
1757 * If not, we can stop recording them.
1759 if (ftrace_rec_count(rec) > 0 &&
1760 rec->flags & FTRACE_FL_REGS &&
1761 ops->flags & FTRACE_OPS_FL_SAVE_REGS) {
1762 if (!test_rec_ops_needs_regs(rec))
1763 rec->flags &= ~FTRACE_FL_REGS;
1767 * The TRAMP needs to be set only if rec count
1768 * is decremented to one, and the ops that is
1769 * left has a trampoline. As TRAMP can only be
1770 * enabled if there is only a single ops attached
1771 * to it.
1773 if (ftrace_rec_count(rec) == 1 &&
1774 ftrace_find_tramp_ops_any(rec))
1775 rec->flags |= FTRACE_FL_TRAMP;
1776 else
1777 rec->flags &= ~FTRACE_FL_TRAMP;
1780 * flags will be cleared in ftrace_check_record()
1781 * if rec count is zero.
1784 count++;
1786 /* Must match FTRACE_UPDATE_CALLS in ftrace_modify_all_code() */
1787 update |= ftrace_test_record(rec, 1) != FTRACE_UPDATE_IGNORE;
1789 /* Shortcut, if we handled all records, we are done. */
1790 if (!all && count == hash->count)
1791 return update;
1792 } while_for_each_ftrace_rec();
1794 return update;
1797 static bool ftrace_hash_rec_disable(struct ftrace_ops *ops,
1798 int filter_hash)
1800 return __ftrace_hash_rec_update(ops, filter_hash, 0);
1803 static bool ftrace_hash_rec_enable(struct ftrace_ops *ops,
1804 int filter_hash)
1806 return __ftrace_hash_rec_update(ops, filter_hash, 1);
1809 static void ftrace_hash_rec_update_modify(struct ftrace_ops *ops,
1810 int filter_hash, int inc)
1812 struct ftrace_ops *op;
1814 __ftrace_hash_rec_update(ops, filter_hash, inc);
1816 if (ops->func_hash != &global_ops.local_hash)
1817 return;
1820 * If the ops shares the global_ops hash, then we need to update
1821 * all ops that are enabled and use this hash.
1823 do_for_each_ftrace_op(op, ftrace_ops_list) {
1824 /* Already done */
1825 if (op == ops)
1826 continue;
1827 if (op->func_hash == &global_ops.local_hash)
1828 __ftrace_hash_rec_update(op, filter_hash, inc);
1829 } while_for_each_ftrace_op(op);
1832 static void ftrace_hash_rec_disable_modify(struct ftrace_ops *ops,
1833 int filter_hash)
1835 ftrace_hash_rec_update_modify(ops, filter_hash, 0);
1838 static void ftrace_hash_rec_enable_modify(struct ftrace_ops *ops,
1839 int filter_hash)
1841 ftrace_hash_rec_update_modify(ops, filter_hash, 1);
1845 * Try to update IPMODIFY flag on each ftrace_rec. Return 0 if it is OK
1846 * or no-needed to update, -EBUSY if it detects a conflict of the flag
1847 * on a ftrace_rec, and -EINVAL if the new_hash tries to trace all recs.
1848 * Note that old_hash and new_hash has below meanings
1849 * - If the hash is NULL, it hits all recs (if IPMODIFY is set, this is rejected)
1850 * - If the hash is EMPTY_HASH, it hits nothing
1851 * - Anything else hits the recs which match the hash entries.
1853 static int __ftrace_hash_update_ipmodify(struct ftrace_ops *ops,
1854 struct ftrace_hash *old_hash,
1855 struct ftrace_hash *new_hash)
1857 struct ftrace_page *pg;
1858 struct dyn_ftrace *rec, *end = NULL;
1859 int in_old, in_new;
1861 /* Only update if the ops has been registered */
1862 if (!(ops->flags & FTRACE_OPS_FL_ENABLED))
1863 return 0;
1865 if (!(ops->flags & FTRACE_OPS_FL_IPMODIFY))
1866 return 0;
1869 * Since the IPMODIFY is a very address sensitive action, we do not
1870 * allow ftrace_ops to set all functions to new hash.
1872 if (!new_hash || !old_hash)
1873 return -EINVAL;
1875 /* Update rec->flags */
1876 do_for_each_ftrace_rec(pg, rec) {
1878 if (rec->flags & FTRACE_FL_DISABLED)
1879 continue;
1881 /* We need to update only differences of filter_hash */
1882 in_old = !!ftrace_lookup_ip(old_hash, rec->ip);
1883 in_new = !!ftrace_lookup_ip(new_hash, rec->ip);
1884 if (in_old == in_new)
1885 continue;
1887 if (in_new) {
1888 /* New entries must ensure no others are using it */
1889 if (rec->flags & FTRACE_FL_IPMODIFY)
1890 goto rollback;
1891 rec->flags |= FTRACE_FL_IPMODIFY;
1892 } else /* Removed entry */
1893 rec->flags &= ~FTRACE_FL_IPMODIFY;
1894 } while_for_each_ftrace_rec();
1896 return 0;
1898 rollback:
1899 end = rec;
1901 /* Roll back what we did above */
1902 do_for_each_ftrace_rec(pg, rec) {
1904 if (rec->flags & FTRACE_FL_DISABLED)
1905 continue;
1907 if (rec == end)
1908 goto err_out;
1910 in_old = !!ftrace_lookup_ip(old_hash, rec->ip);
1911 in_new = !!ftrace_lookup_ip(new_hash, rec->ip);
1912 if (in_old == in_new)
1913 continue;
1915 if (in_new)
1916 rec->flags &= ~FTRACE_FL_IPMODIFY;
1917 else
1918 rec->flags |= FTRACE_FL_IPMODIFY;
1919 } while_for_each_ftrace_rec();
1921 err_out:
1922 return -EBUSY;
1925 static int ftrace_hash_ipmodify_enable(struct ftrace_ops *ops)
1927 struct ftrace_hash *hash = ops->func_hash->filter_hash;
1929 if (ftrace_hash_empty(hash))
1930 hash = NULL;
1932 return __ftrace_hash_update_ipmodify(ops, EMPTY_HASH, hash);
1935 /* Disabling always succeeds */
1936 static void ftrace_hash_ipmodify_disable(struct ftrace_ops *ops)
1938 struct ftrace_hash *hash = ops->func_hash->filter_hash;
1940 if (ftrace_hash_empty(hash))
1941 hash = NULL;
1943 __ftrace_hash_update_ipmodify(ops, hash, EMPTY_HASH);
1946 static int ftrace_hash_ipmodify_update(struct ftrace_ops *ops,
1947 struct ftrace_hash *new_hash)
1949 struct ftrace_hash *old_hash = ops->func_hash->filter_hash;
1951 if (ftrace_hash_empty(old_hash))
1952 old_hash = NULL;
1954 if (ftrace_hash_empty(new_hash))
1955 new_hash = NULL;
1957 return __ftrace_hash_update_ipmodify(ops, old_hash, new_hash);
1960 static void print_ip_ins(const char *fmt, const unsigned char *p)
1962 int i;
1964 printk(KERN_CONT "%s", fmt);
1966 for (i = 0; i < MCOUNT_INSN_SIZE; i++)
1967 printk(KERN_CONT "%s%02x", i ? ":" : "", p[i]);
1970 enum ftrace_bug_type ftrace_bug_type;
1971 const void *ftrace_expected;
1973 static void print_bug_type(void)
1975 switch (ftrace_bug_type) {
1976 case FTRACE_BUG_UNKNOWN:
1977 break;
1978 case FTRACE_BUG_INIT:
1979 pr_info("Initializing ftrace call sites\n");
1980 break;
1981 case FTRACE_BUG_NOP:
1982 pr_info("Setting ftrace call site to NOP\n");
1983 break;
1984 case FTRACE_BUG_CALL:
1985 pr_info("Setting ftrace call site to call ftrace function\n");
1986 break;
1987 case FTRACE_BUG_UPDATE:
1988 pr_info("Updating ftrace call site to call a different ftrace function\n");
1989 break;
1994 * ftrace_bug - report and shutdown function tracer
1995 * @failed: The failed type (EFAULT, EINVAL, EPERM)
1996 * @rec: The record that failed
1998 * The arch code that enables or disables the function tracing
1999 * can call ftrace_bug() when it has detected a problem in
2000 * modifying the code. @failed should be one of either:
2001 * EFAULT - if the problem happens on reading the @ip address
2002 * EINVAL - if what is read at @ip is not what was expected
2003 * EPERM - if the problem happens on writting to the @ip address
2005 void ftrace_bug(int failed, struct dyn_ftrace *rec)
2007 unsigned long ip = rec ? rec->ip : 0;
2009 switch (failed) {
2010 case -EFAULT:
2011 FTRACE_WARN_ON_ONCE(1);
2012 pr_info("ftrace faulted on modifying ");
2013 print_ip_sym(ip);
2014 break;
2015 case -EINVAL:
2016 FTRACE_WARN_ON_ONCE(1);
2017 pr_info("ftrace failed to modify ");
2018 print_ip_sym(ip);
2019 print_ip_ins(" actual: ", (unsigned char *)ip);
2020 pr_cont("\n");
2021 if (ftrace_expected) {
2022 print_ip_ins(" expected: ", ftrace_expected);
2023 pr_cont("\n");
2025 break;
2026 case -EPERM:
2027 FTRACE_WARN_ON_ONCE(1);
2028 pr_info("ftrace faulted on writing ");
2029 print_ip_sym(ip);
2030 break;
2031 default:
2032 FTRACE_WARN_ON_ONCE(1);
2033 pr_info("ftrace faulted on unknown error ");
2034 print_ip_sym(ip);
2036 print_bug_type();
2037 if (rec) {
2038 struct ftrace_ops *ops = NULL;
2040 pr_info("ftrace record flags: %lx\n", rec->flags);
2041 pr_cont(" (%ld)%s", ftrace_rec_count(rec),
2042 rec->flags & FTRACE_FL_REGS ? " R" : " ");
2043 if (rec->flags & FTRACE_FL_TRAMP_EN) {
2044 ops = ftrace_find_tramp_ops_any(rec);
2045 if (ops) {
2046 do {
2047 pr_cont("\ttramp: %pS (%pS)",
2048 (void *)ops->trampoline,
2049 (void *)ops->func);
2050 ops = ftrace_find_tramp_ops_next(rec, ops);
2051 } while (ops);
2052 } else
2053 pr_cont("\ttramp: ERROR!");
2056 ip = ftrace_get_addr_curr(rec);
2057 pr_cont("\n expected tramp: %lx\n", ip);
2061 static int ftrace_check_record(struct dyn_ftrace *rec, int enable, int update)
2063 unsigned long flag = 0UL;
2065 ftrace_bug_type = FTRACE_BUG_UNKNOWN;
2067 if (rec->flags & FTRACE_FL_DISABLED)
2068 return FTRACE_UPDATE_IGNORE;
2071 * If we are updating calls:
2073 * If the record has a ref count, then we need to enable it
2074 * because someone is using it.
2076 * Otherwise we make sure its disabled.
2078 * If we are disabling calls, then disable all records that
2079 * are enabled.
2081 if (enable && ftrace_rec_count(rec))
2082 flag = FTRACE_FL_ENABLED;
2085 * If enabling and the REGS flag does not match the REGS_EN, or
2086 * the TRAMP flag doesn't match the TRAMP_EN, then do not ignore
2087 * this record. Set flags to fail the compare against ENABLED.
2089 if (flag) {
2090 if (!(rec->flags & FTRACE_FL_REGS) !=
2091 !(rec->flags & FTRACE_FL_REGS_EN))
2092 flag |= FTRACE_FL_REGS;
2094 if (!(rec->flags & FTRACE_FL_TRAMP) !=
2095 !(rec->flags & FTRACE_FL_TRAMP_EN))
2096 flag |= FTRACE_FL_TRAMP;
2099 /* If the state of this record hasn't changed, then do nothing */
2100 if ((rec->flags & FTRACE_FL_ENABLED) == flag)
2101 return FTRACE_UPDATE_IGNORE;
2103 if (flag) {
2104 /* Save off if rec is being enabled (for return value) */
2105 flag ^= rec->flags & FTRACE_FL_ENABLED;
2107 if (update) {
2108 rec->flags |= FTRACE_FL_ENABLED;
2109 if (flag & FTRACE_FL_REGS) {
2110 if (rec->flags & FTRACE_FL_REGS)
2111 rec->flags |= FTRACE_FL_REGS_EN;
2112 else
2113 rec->flags &= ~FTRACE_FL_REGS_EN;
2115 if (flag & FTRACE_FL_TRAMP) {
2116 if (rec->flags & FTRACE_FL_TRAMP)
2117 rec->flags |= FTRACE_FL_TRAMP_EN;
2118 else
2119 rec->flags &= ~FTRACE_FL_TRAMP_EN;
2124 * If this record is being updated from a nop, then
2125 * return UPDATE_MAKE_CALL.
2126 * Otherwise,
2127 * return UPDATE_MODIFY_CALL to tell the caller to convert
2128 * from the save regs, to a non-save regs function or
2129 * vice versa, or from a trampoline call.
2131 if (flag & FTRACE_FL_ENABLED) {
2132 ftrace_bug_type = FTRACE_BUG_CALL;
2133 return FTRACE_UPDATE_MAKE_CALL;
2136 ftrace_bug_type = FTRACE_BUG_UPDATE;
2137 return FTRACE_UPDATE_MODIFY_CALL;
2140 if (update) {
2141 /* If there's no more users, clear all flags */
2142 if (!ftrace_rec_count(rec))
2143 rec->flags = 0;
2144 else
2146 * Just disable the record, but keep the ops TRAMP
2147 * and REGS states. The _EN flags must be disabled though.
2149 rec->flags &= ~(FTRACE_FL_ENABLED | FTRACE_FL_TRAMP_EN |
2150 FTRACE_FL_REGS_EN);
2153 ftrace_bug_type = FTRACE_BUG_NOP;
2154 return FTRACE_UPDATE_MAKE_NOP;
2158 * ftrace_update_record, set a record that now is tracing or not
2159 * @rec: the record to update
2160 * @enable: set to 1 if the record is tracing, zero to force disable
2162 * The records that represent all functions that can be traced need
2163 * to be updated when tracing has been enabled.
2165 int ftrace_update_record(struct dyn_ftrace *rec, int enable)
2167 return ftrace_check_record(rec, enable, 1);
2171 * ftrace_test_record, check if the record has been enabled or not
2172 * @rec: the record to test
2173 * @enable: set to 1 to check if enabled, 0 if it is disabled
2175 * The arch code may need to test if a record is already set to
2176 * tracing to determine how to modify the function code that it
2177 * represents.
2179 int ftrace_test_record(struct dyn_ftrace *rec, int enable)
2181 return ftrace_check_record(rec, enable, 0);
2184 static struct ftrace_ops *
2185 ftrace_find_tramp_ops_any(struct dyn_ftrace *rec)
2187 struct ftrace_ops *op;
2188 unsigned long ip = rec->ip;
2190 do_for_each_ftrace_op(op, ftrace_ops_list) {
2192 if (!op->trampoline)
2193 continue;
2195 if (hash_contains_ip(ip, op->func_hash))
2196 return op;
2197 } while_for_each_ftrace_op(op);
2199 return NULL;
2202 static struct ftrace_ops *
2203 ftrace_find_tramp_ops_next(struct dyn_ftrace *rec,
2204 struct ftrace_ops *op)
2206 unsigned long ip = rec->ip;
2208 while_for_each_ftrace_op(op) {
2210 if (!op->trampoline)
2211 continue;
2213 if (hash_contains_ip(ip, op->func_hash))
2214 return op;
2217 return NULL;
2220 static struct ftrace_ops *
2221 ftrace_find_tramp_ops_curr(struct dyn_ftrace *rec)
2223 struct ftrace_ops *op;
2224 unsigned long ip = rec->ip;
2227 * Need to check removed ops first.
2228 * If they are being removed, and this rec has a tramp,
2229 * and this rec is in the ops list, then it would be the
2230 * one with the tramp.
2232 if (removed_ops) {
2233 if (hash_contains_ip(ip, &removed_ops->old_hash))
2234 return removed_ops;
2238 * Need to find the current trampoline for a rec.
2239 * Now, a trampoline is only attached to a rec if there
2240 * was a single 'ops' attached to it. But this can be called
2241 * when we are adding another op to the rec or removing the
2242 * current one. Thus, if the op is being added, we can
2243 * ignore it because it hasn't attached itself to the rec
2244 * yet.
2246 * If an ops is being modified (hooking to different functions)
2247 * then we don't care about the new functions that are being
2248 * added, just the old ones (that are probably being removed).
2250 * If we are adding an ops to a function that already is using
2251 * a trampoline, it needs to be removed (trampolines are only
2252 * for single ops connected), then an ops that is not being
2253 * modified also needs to be checked.
2255 do_for_each_ftrace_op(op, ftrace_ops_list) {
2257 if (!op->trampoline)
2258 continue;
2261 * If the ops is being added, it hasn't gotten to
2262 * the point to be removed from this tree yet.
2264 if (op->flags & FTRACE_OPS_FL_ADDING)
2265 continue;
2269 * If the ops is being modified and is in the old
2270 * hash, then it is probably being removed from this
2271 * function.
2273 if ((op->flags & FTRACE_OPS_FL_MODIFYING) &&
2274 hash_contains_ip(ip, &op->old_hash))
2275 return op;
2277 * If the ops is not being added or modified, and it's
2278 * in its normal filter hash, then this must be the one
2279 * we want!
2281 if (!(op->flags & FTRACE_OPS_FL_MODIFYING) &&
2282 hash_contains_ip(ip, op->func_hash))
2283 return op;
2285 } while_for_each_ftrace_op(op);
2287 return NULL;
2290 static struct ftrace_ops *
2291 ftrace_find_tramp_ops_new(struct dyn_ftrace *rec)
2293 struct ftrace_ops *op;
2294 unsigned long ip = rec->ip;
2296 do_for_each_ftrace_op(op, ftrace_ops_list) {
2297 /* pass rec in as regs to have non-NULL val */
2298 if (hash_contains_ip(ip, op->func_hash))
2299 return op;
2300 } while_for_each_ftrace_op(op);
2302 return NULL;
2306 * ftrace_get_addr_new - Get the call address to set to
2307 * @rec: The ftrace record descriptor
2309 * If the record has the FTRACE_FL_REGS set, that means that it
2310 * wants to convert to a callback that saves all regs. If FTRACE_FL_REGS
2311 * is not not set, then it wants to convert to the normal callback.
2313 * Returns the address of the trampoline to set to
2315 unsigned long ftrace_get_addr_new(struct dyn_ftrace *rec)
2317 struct ftrace_ops *ops;
2319 /* Trampolines take precedence over regs */
2320 if (rec->flags & FTRACE_FL_TRAMP) {
2321 ops = ftrace_find_tramp_ops_new(rec);
2322 if (FTRACE_WARN_ON(!ops || !ops->trampoline)) {
2323 pr_warn("Bad trampoline accounting at: %p (%pS) (%lx)\n",
2324 (void *)rec->ip, (void *)rec->ip, rec->flags);
2325 /* Ftrace is shutting down, return anything */
2326 return (unsigned long)FTRACE_ADDR;
2328 return ops->trampoline;
2331 if (rec->flags & FTRACE_FL_REGS)
2332 return (unsigned long)FTRACE_REGS_ADDR;
2333 else
2334 return (unsigned long)FTRACE_ADDR;
2338 * ftrace_get_addr_curr - Get the call address that is already there
2339 * @rec: The ftrace record descriptor
2341 * The FTRACE_FL_REGS_EN is set when the record already points to
2342 * a function that saves all the regs. Basically the '_EN' version
2343 * represents the current state of the function.
2345 * Returns the address of the trampoline that is currently being called
2347 unsigned long ftrace_get_addr_curr(struct dyn_ftrace *rec)
2349 struct ftrace_ops *ops;
2351 /* Trampolines take precedence over regs */
2352 if (rec->flags & FTRACE_FL_TRAMP_EN) {
2353 ops = ftrace_find_tramp_ops_curr(rec);
2354 if (FTRACE_WARN_ON(!ops)) {
2355 pr_warn("Bad trampoline accounting at: %p (%pS)\n",
2356 (void *)rec->ip, (void *)rec->ip);
2357 /* Ftrace is shutting down, return anything */
2358 return (unsigned long)FTRACE_ADDR;
2360 return ops->trampoline;
2363 if (rec->flags & FTRACE_FL_REGS_EN)
2364 return (unsigned long)FTRACE_REGS_ADDR;
2365 else
2366 return (unsigned long)FTRACE_ADDR;
2369 static int
2370 __ftrace_replace_code(struct dyn_ftrace *rec, int enable)
2372 unsigned long ftrace_old_addr;
2373 unsigned long ftrace_addr;
2374 int ret;
2376 ftrace_addr = ftrace_get_addr_new(rec);
2378 /* This needs to be done before we call ftrace_update_record */
2379 ftrace_old_addr = ftrace_get_addr_curr(rec);
2381 ret = ftrace_update_record(rec, enable);
2383 ftrace_bug_type = FTRACE_BUG_UNKNOWN;
2385 switch (ret) {
2386 case FTRACE_UPDATE_IGNORE:
2387 return 0;
2389 case FTRACE_UPDATE_MAKE_CALL:
2390 ftrace_bug_type = FTRACE_BUG_CALL;
2391 return ftrace_make_call(rec, ftrace_addr);
2393 case FTRACE_UPDATE_MAKE_NOP:
2394 ftrace_bug_type = FTRACE_BUG_NOP;
2395 return ftrace_make_nop(NULL, rec, ftrace_old_addr);
2397 case FTRACE_UPDATE_MODIFY_CALL:
2398 ftrace_bug_type = FTRACE_BUG_UPDATE;
2399 return ftrace_modify_call(rec, ftrace_old_addr, ftrace_addr);
2402 return -1; /* unknow ftrace bug */
2405 void __weak ftrace_replace_code(int enable)
2407 struct dyn_ftrace *rec;
2408 struct ftrace_page *pg;
2409 int failed;
2411 if (unlikely(ftrace_disabled))
2412 return;
2414 do_for_each_ftrace_rec(pg, rec) {
2416 if (rec->flags & FTRACE_FL_DISABLED)
2417 continue;
2419 failed = __ftrace_replace_code(rec, enable);
2420 if (failed) {
2421 ftrace_bug(failed, rec);
2422 /* Stop processing */
2423 return;
2425 } while_for_each_ftrace_rec();
2428 struct ftrace_rec_iter {
2429 struct ftrace_page *pg;
2430 int index;
2434 * ftrace_rec_iter_start, start up iterating over traced functions
2436 * Returns an iterator handle that is used to iterate over all
2437 * the records that represent address locations where functions
2438 * are traced.
2440 * May return NULL if no records are available.
2442 struct ftrace_rec_iter *ftrace_rec_iter_start(void)
2445 * We only use a single iterator.
2446 * Protected by the ftrace_lock mutex.
2448 static struct ftrace_rec_iter ftrace_rec_iter;
2449 struct ftrace_rec_iter *iter = &ftrace_rec_iter;
2451 iter->pg = ftrace_pages_start;
2452 iter->index = 0;
2454 /* Could have empty pages */
2455 while (iter->pg && !iter->pg->index)
2456 iter->pg = iter->pg->next;
2458 if (!iter->pg)
2459 return NULL;
2461 return iter;
2465 * ftrace_rec_iter_next, get the next record to process.
2466 * @iter: The handle to the iterator.
2468 * Returns the next iterator after the given iterator @iter.
2470 struct ftrace_rec_iter *ftrace_rec_iter_next(struct ftrace_rec_iter *iter)
2472 iter->index++;
2474 if (iter->index >= iter->pg->index) {
2475 iter->pg = iter->pg->next;
2476 iter->index = 0;
2478 /* Could have empty pages */
2479 while (iter->pg && !iter->pg->index)
2480 iter->pg = iter->pg->next;
2483 if (!iter->pg)
2484 return NULL;
2486 return iter;
2490 * ftrace_rec_iter_record, get the record at the iterator location
2491 * @iter: The current iterator location
2493 * Returns the record that the current @iter is at.
2495 struct dyn_ftrace *ftrace_rec_iter_record(struct ftrace_rec_iter *iter)
2497 return &iter->pg->records[iter->index];
2500 static int
2501 ftrace_code_disable(struct module *mod, struct dyn_ftrace *rec)
2503 int ret;
2505 if (unlikely(ftrace_disabled))
2506 return 0;
2508 ret = ftrace_make_nop(mod, rec, MCOUNT_ADDR);
2509 if (ret) {
2510 ftrace_bug_type = FTRACE_BUG_INIT;
2511 ftrace_bug(ret, rec);
2512 return 0;
2514 return 1;
2518 * archs can override this function if they must do something
2519 * before the modifying code is performed.
2521 int __weak ftrace_arch_code_modify_prepare(void)
2523 return 0;
2527 * archs can override this function if they must do something
2528 * after the modifying code is performed.
2530 int __weak ftrace_arch_code_modify_post_process(void)
2532 return 0;
2535 void ftrace_modify_all_code(int command)
2537 int update = command & FTRACE_UPDATE_TRACE_FUNC;
2538 int err = 0;
2541 * If the ftrace_caller calls a ftrace_ops func directly,
2542 * we need to make sure that it only traces functions it
2543 * expects to trace. When doing the switch of functions,
2544 * we need to update to the ftrace_ops_list_func first
2545 * before the transition between old and new calls are set,
2546 * as the ftrace_ops_list_func will check the ops hashes
2547 * to make sure the ops are having the right functions
2548 * traced.
2550 if (update) {
2551 err = ftrace_update_ftrace_func(ftrace_ops_list_func);
2552 if (FTRACE_WARN_ON(err))
2553 return;
2556 if (command & FTRACE_UPDATE_CALLS)
2557 ftrace_replace_code(1);
2558 else if (command & FTRACE_DISABLE_CALLS)
2559 ftrace_replace_code(0);
2561 if (update && ftrace_trace_function != ftrace_ops_list_func) {
2562 function_trace_op = set_function_trace_op;
2563 smp_wmb();
2564 /* If irqs are disabled, we are in stop machine */
2565 if (!irqs_disabled())
2566 smp_call_function(ftrace_sync_ipi, NULL, 1);
2567 err = ftrace_update_ftrace_func(ftrace_trace_function);
2568 if (FTRACE_WARN_ON(err))
2569 return;
2572 if (command & FTRACE_START_FUNC_RET)
2573 err = ftrace_enable_ftrace_graph_caller();
2574 else if (command & FTRACE_STOP_FUNC_RET)
2575 err = ftrace_disable_ftrace_graph_caller();
2576 FTRACE_WARN_ON(err);
2579 static int __ftrace_modify_code(void *data)
2581 int *command = data;
2583 ftrace_modify_all_code(*command);
2585 return 0;
2589 * ftrace_run_stop_machine, go back to the stop machine method
2590 * @command: The command to tell ftrace what to do
2592 * If an arch needs to fall back to the stop machine method, the
2593 * it can call this function.
2595 void ftrace_run_stop_machine(int command)
2597 stop_machine(__ftrace_modify_code, &command, NULL);
2601 * arch_ftrace_update_code, modify the code to trace or not trace
2602 * @command: The command that needs to be done
2604 * Archs can override this function if it does not need to
2605 * run stop_machine() to modify code.
2607 void __weak arch_ftrace_update_code(int command)
2609 ftrace_run_stop_machine(command);
2612 static void ftrace_run_update_code(int command)
2614 int ret;
2616 ret = ftrace_arch_code_modify_prepare();
2617 FTRACE_WARN_ON(ret);
2618 if (ret)
2619 return;
2622 * By default we use stop_machine() to modify the code.
2623 * But archs can do what ever they want as long as it
2624 * is safe. The stop_machine() is the safest, but also
2625 * produces the most overhead.
2627 arch_ftrace_update_code(command);
2629 ret = ftrace_arch_code_modify_post_process();
2630 FTRACE_WARN_ON(ret);
2633 static void ftrace_run_modify_code(struct ftrace_ops *ops, int command,
2634 struct ftrace_ops_hash *old_hash)
2636 ops->flags |= FTRACE_OPS_FL_MODIFYING;
2637 ops->old_hash.filter_hash = old_hash->filter_hash;
2638 ops->old_hash.notrace_hash = old_hash->notrace_hash;
2639 ftrace_run_update_code(command);
2640 ops->old_hash.filter_hash = NULL;
2641 ops->old_hash.notrace_hash = NULL;
2642 ops->flags &= ~FTRACE_OPS_FL_MODIFYING;
2645 static ftrace_func_t saved_ftrace_func;
2646 static int ftrace_start_up;
2648 void __weak arch_ftrace_trampoline_free(struct ftrace_ops *ops)
2652 static void per_cpu_ops_free(struct ftrace_ops *ops)
2654 free_percpu(ops->disabled);
2657 static void ftrace_startup_enable(int command)
2659 if (saved_ftrace_func != ftrace_trace_function) {
2660 saved_ftrace_func = ftrace_trace_function;
2661 command |= FTRACE_UPDATE_TRACE_FUNC;
2664 if (!command || !ftrace_enabled)
2665 return;
2667 ftrace_run_update_code(command);
2670 static void ftrace_startup_all(int command)
2672 update_all_ops = true;
2673 ftrace_startup_enable(command);
2674 update_all_ops = false;
2677 static int ftrace_startup(struct ftrace_ops *ops, int command)
2679 int ret;
2681 if (unlikely(ftrace_disabled))
2682 return -ENODEV;
2684 ret = __register_ftrace_function(ops);
2685 if (ret)
2686 return ret;
2688 ftrace_start_up++;
2691 * Note that ftrace probes uses this to start up
2692 * and modify functions it will probe. But we still
2693 * set the ADDING flag for modification, as probes
2694 * do not have trampolines. If they add them in the
2695 * future, then the probes will need to distinguish
2696 * between adding and updating probes.
2698 ops->flags |= FTRACE_OPS_FL_ENABLED | FTRACE_OPS_FL_ADDING;
2700 ret = ftrace_hash_ipmodify_enable(ops);
2701 if (ret < 0) {
2702 /* Rollback registration process */
2703 __unregister_ftrace_function(ops);
2704 ftrace_start_up--;
2705 ops->flags &= ~FTRACE_OPS_FL_ENABLED;
2706 return ret;
2709 if (ftrace_hash_rec_enable(ops, 1))
2710 command |= FTRACE_UPDATE_CALLS;
2712 ftrace_startup_enable(command);
2714 ops->flags &= ~FTRACE_OPS_FL_ADDING;
2716 return 0;
2719 static int ftrace_shutdown(struct ftrace_ops *ops, int command)
2721 int ret;
2723 if (unlikely(ftrace_disabled))
2724 return -ENODEV;
2726 ret = __unregister_ftrace_function(ops);
2727 if (ret)
2728 return ret;
2730 ftrace_start_up--;
2732 * Just warn in case of unbalance, no need to kill ftrace, it's not
2733 * critical but the ftrace_call callers may be never nopped again after
2734 * further ftrace uses.
2736 WARN_ON_ONCE(ftrace_start_up < 0);
2738 /* Disabling ipmodify never fails */
2739 ftrace_hash_ipmodify_disable(ops);
2741 if (ftrace_hash_rec_disable(ops, 1))
2742 command |= FTRACE_UPDATE_CALLS;
2744 ops->flags &= ~FTRACE_OPS_FL_ENABLED;
2746 if (saved_ftrace_func != ftrace_trace_function) {
2747 saved_ftrace_func = ftrace_trace_function;
2748 command |= FTRACE_UPDATE_TRACE_FUNC;
2751 if (!command || !ftrace_enabled) {
2753 * If these are dynamic or per_cpu ops, they still
2754 * need their data freed. Since, function tracing is
2755 * not currently active, we can just free them
2756 * without synchronizing all CPUs.
2758 if (ops->flags & (FTRACE_OPS_FL_DYNAMIC | FTRACE_OPS_FL_PER_CPU))
2759 goto free_ops;
2761 return 0;
2765 * If the ops uses a trampoline, then it needs to be
2766 * tested first on update.
2768 ops->flags |= FTRACE_OPS_FL_REMOVING;
2769 removed_ops = ops;
2771 /* The trampoline logic checks the old hashes */
2772 ops->old_hash.filter_hash = ops->func_hash->filter_hash;
2773 ops->old_hash.notrace_hash = ops->func_hash->notrace_hash;
2775 ftrace_run_update_code(command);
2778 * If there's no more ops registered with ftrace, run a
2779 * sanity check to make sure all rec flags are cleared.
2781 if (ftrace_ops_list == &ftrace_list_end) {
2782 struct ftrace_page *pg;
2783 struct dyn_ftrace *rec;
2785 do_for_each_ftrace_rec(pg, rec) {
2786 if (FTRACE_WARN_ON_ONCE(rec->flags & ~FTRACE_FL_DISABLED))
2787 pr_warn(" %pS flags:%lx\n",
2788 (void *)rec->ip, rec->flags);
2789 } while_for_each_ftrace_rec();
2792 ops->old_hash.filter_hash = NULL;
2793 ops->old_hash.notrace_hash = NULL;
2795 removed_ops = NULL;
2796 ops->flags &= ~FTRACE_OPS_FL_REMOVING;
2799 * Dynamic ops may be freed, we must make sure that all
2800 * callers are done before leaving this function.
2801 * The same goes for freeing the per_cpu data of the per_cpu
2802 * ops.
2804 * Again, normal synchronize_sched() is not good enough.
2805 * We need to do a hard force of sched synchronization.
2806 * This is because we use preempt_disable() to do RCU, but
2807 * the function tracers can be called where RCU is not watching
2808 * (like before user_exit()). We can not rely on the RCU
2809 * infrastructure to do the synchronization, thus we must do it
2810 * ourselves.
2812 if (ops->flags & (FTRACE_OPS_FL_DYNAMIC | FTRACE_OPS_FL_PER_CPU)) {
2813 schedule_on_each_cpu(ftrace_sync);
2815 free_ops:
2816 arch_ftrace_trampoline_free(ops);
2818 if (ops->flags & FTRACE_OPS_FL_PER_CPU)
2819 per_cpu_ops_free(ops);
2822 return 0;
2825 static void ftrace_startup_sysctl(void)
2827 int command;
2829 if (unlikely(ftrace_disabled))
2830 return;
2832 /* Force update next time */
2833 saved_ftrace_func = NULL;
2834 /* ftrace_start_up is true if we want ftrace running */
2835 if (ftrace_start_up) {
2836 command = FTRACE_UPDATE_CALLS;
2837 if (ftrace_graph_active)
2838 command |= FTRACE_START_FUNC_RET;
2839 ftrace_startup_enable(command);
2843 static void ftrace_shutdown_sysctl(void)
2845 int command;
2847 if (unlikely(ftrace_disabled))
2848 return;
2850 /* ftrace_start_up is true if ftrace is running */
2851 if (ftrace_start_up) {
2852 command = FTRACE_DISABLE_CALLS;
2853 if (ftrace_graph_active)
2854 command |= FTRACE_STOP_FUNC_RET;
2855 ftrace_run_update_code(command);
2859 static cycle_t ftrace_update_time;
2860 unsigned long ftrace_update_tot_cnt;
2862 static inline int ops_traces_mod(struct ftrace_ops *ops)
2865 * Filter_hash being empty will default to trace module.
2866 * But notrace hash requires a test of individual module functions.
2868 return ftrace_hash_empty(ops->func_hash->filter_hash) &&
2869 ftrace_hash_empty(ops->func_hash->notrace_hash);
2873 * Check if the current ops references the record.
2875 * If the ops traces all functions, then it was already accounted for.
2876 * If the ops does not trace the current record function, skip it.
2877 * If the ops ignores the function via notrace filter, skip it.
2879 static inline bool
2880 ops_references_rec(struct ftrace_ops *ops, struct dyn_ftrace *rec)
2882 /* If ops isn't enabled, ignore it */
2883 if (!(ops->flags & FTRACE_OPS_FL_ENABLED))
2884 return 0;
2886 /* If ops traces all then it includes this function */
2887 if (ops_traces_mod(ops))
2888 return 1;
2890 /* The function must be in the filter */
2891 if (!ftrace_hash_empty(ops->func_hash->filter_hash) &&
2892 !ftrace_lookup_ip(ops->func_hash->filter_hash, rec->ip))
2893 return 0;
2895 /* If in notrace hash, we ignore it too */
2896 if (ftrace_lookup_ip(ops->func_hash->notrace_hash, rec->ip))
2897 return 0;
2899 return 1;
2902 static int ftrace_update_code(struct module *mod, struct ftrace_page *new_pgs)
2904 struct ftrace_page *pg;
2905 struct dyn_ftrace *p;
2906 cycle_t start, stop;
2907 unsigned long update_cnt = 0;
2908 unsigned long rec_flags = 0;
2909 int i;
2911 start = ftrace_now(raw_smp_processor_id());
2914 * When a module is loaded, this function is called to convert
2915 * the calls to mcount in its text to nops, and also to create
2916 * an entry in the ftrace data. Now, if ftrace is activated
2917 * after this call, but before the module sets its text to
2918 * read-only, the modification of enabling ftrace can fail if
2919 * the read-only is done while ftrace is converting the calls.
2920 * To prevent this, the module's records are set as disabled
2921 * and will be enabled after the call to set the module's text
2922 * to read-only.
2924 if (mod)
2925 rec_flags |= FTRACE_FL_DISABLED;
2927 for (pg = new_pgs; pg; pg = pg->next) {
2929 for (i = 0; i < pg->index; i++) {
2931 /* If something went wrong, bail without enabling anything */
2932 if (unlikely(ftrace_disabled))
2933 return -1;
2935 p = &pg->records[i];
2936 p->flags = rec_flags;
2939 * Do the initial record conversion from mcount jump
2940 * to the NOP instructions.
2942 if (!ftrace_code_disable(mod, p))
2943 break;
2945 update_cnt++;
2949 stop = ftrace_now(raw_smp_processor_id());
2950 ftrace_update_time = stop - start;
2951 ftrace_update_tot_cnt += update_cnt;
2953 return 0;
2956 static int ftrace_allocate_records(struct ftrace_page *pg, int count)
2958 int order;
2959 int cnt;
2961 if (WARN_ON(!count))
2962 return -EINVAL;
2964 order = get_count_order(DIV_ROUND_UP(count, ENTRIES_PER_PAGE));
2967 * We want to fill as much as possible. No more than a page
2968 * may be empty.
2970 while ((PAGE_SIZE << order) / ENTRY_SIZE >= count + ENTRIES_PER_PAGE)
2971 order--;
2973 again:
2974 pg->records = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, order);
2976 if (!pg->records) {
2977 /* if we can't allocate this size, try something smaller */
2978 if (!order)
2979 return -ENOMEM;
2980 order >>= 1;
2981 goto again;
2984 cnt = (PAGE_SIZE << order) / ENTRY_SIZE;
2985 pg->size = cnt;
2987 if (cnt > count)
2988 cnt = count;
2990 return cnt;
2993 static struct ftrace_page *
2994 ftrace_allocate_pages(unsigned long num_to_init)
2996 struct ftrace_page *start_pg;
2997 struct ftrace_page *pg;
2998 int order;
2999 int cnt;
3001 if (!num_to_init)
3002 return 0;
3004 start_pg = pg = kzalloc(sizeof(*pg), GFP_KERNEL);
3005 if (!pg)
3006 return NULL;
3009 * Try to allocate as much as possible in one continues
3010 * location that fills in all of the space. We want to
3011 * waste as little space as possible.
3013 for (;;) {
3014 cnt = ftrace_allocate_records(pg, num_to_init);
3015 if (cnt < 0)
3016 goto free_pages;
3018 num_to_init -= cnt;
3019 if (!num_to_init)
3020 break;
3022 pg->next = kzalloc(sizeof(*pg), GFP_KERNEL);
3023 if (!pg->next)
3024 goto free_pages;
3026 pg = pg->next;
3029 return start_pg;
3031 free_pages:
3032 pg = start_pg;
3033 while (pg) {
3034 order = get_count_order(pg->size / ENTRIES_PER_PAGE);
3035 free_pages((unsigned long)pg->records, order);
3036 start_pg = pg->next;
3037 kfree(pg);
3038 pg = start_pg;
3040 pr_info("ftrace: FAILED to allocate memory for functions\n");
3041 return NULL;
3044 #define FTRACE_BUFF_MAX (KSYM_SYMBOL_LEN+4) /* room for wildcards */
3046 struct ftrace_iterator {
3047 loff_t pos;
3048 loff_t func_pos;
3049 struct ftrace_page *pg;
3050 struct dyn_ftrace *func;
3051 struct ftrace_func_probe *probe;
3052 struct trace_parser parser;
3053 struct ftrace_hash *hash;
3054 struct ftrace_ops *ops;
3055 int hidx;
3056 int idx;
3057 unsigned flags;
3060 static void *
3061 t_hash_next(struct seq_file *m, loff_t *pos)
3063 struct ftrace_iterator *iter = m->private;
3064 struct hlist_node *hnd = NULL;
3065 struct hlist_head *hhd;
3067 (*pos)++;
3068 iter->pos = *pos;
3070 if (iter->probe)
3071 hnd = &iter->probe->node;
3072 retry:
3073 if (iter->hidx >= FTRACE_FUNC_HASHSIZE)
3074 return NULL;
3076 hhd = &ftrace_func_hash[iter->hidx];
3078 if (hlist_empty(hhd)) {
3079 iter->hidx++;
3080 hnd = NULL;
3081 goto retry;
3084 if (!hnd)
3085 hnd = hhd->first;
3086 else {
3087 hnd = hnd->next;
3088 if (!hnd) {
3089 iter->hidx++;
3090 goto retry;
3094 if (WARN_ON_ONCE(!hnd))
3095 return NULL;
3097 iter->probe = hlist_entry(hnd, struct ftrace_func_probe, node);
3099 return iter;
3102 static void *t_hash_start(struct seq_file *m, loff_t *pos)
3104 struct ftrace_iterator *iter = m->private;
3105 void *p = NULL;
3106 loff_t l;
3108 if (!(iter->flags & FTRACE_ITER_DO_HASH))
3109 return NULL;
3111 if (iter->func_pos > *pos)
3112 return NULL;
3114 iter->hidx = 0;
3115 for (l = 0; l <= (*pos - iter->func_pos); ) {
3116 p = t_hash_next(m, &l);
3117 if (!p)
3118 break;
3120 if (!p)
3121 return NULL;
3123 /* Only set this if we have an item */
3124 iter->flags |= FTRACE_ITER_HASH;
3126 return iter;
3129 static int
3130 t_hash_show(struct seq_file *m, struct ftrace_iterator *iter)
3132 struct ftrace_func_probe *rec;
3134 rec = iter->probe;
3135 if (WARN_ON_ONCE(!rec))
3136 return -EIO;
3138 if (rec->ops->print)
3139 return rec->ops->print(m, rec->ip, rec->ops, rec->data);
3141 seq_printf(m, "%ps:%ps", (void *)rec->ip, (void *)rec->ops->func);
3143 if (rec->data)
3144 seq_printf(m, ":%p", rec->data);
3145 seq_putc(m, '\n');
3147 return 0;
3150 static void *
3151 t_next(struct seq_file *m, void *v, loff_t *pos)
3153 struct ftrace_iterator *iter = m->private;
3154 struct ftrace_ops *ops = iter->ops;
3155 struct dyn_ftrace *rec = NULL;
3157 if (unlikely(ftrace_disabled))
3158 return NULL;
3160 if (iter->flags & FTRACE_ITER_HASH)
3161 return t_hash_next(m, pos);
3163 (*pos)++;
3164 iter->pos = iter->func_pos = *pos;
3166 if (iter->flags & FTRACE_ITER_PRINTALL)
3167 return t_hash_start(m, pos);
3169 retry:
3170 if (iter->idx >= iter->pg->index) {
3171 if (iter->pg->next) {
3172 iter->pg = iter->pg->next;
3173 iter->idx = 0;
3174 goto retry;
3176 } else {
3177 rec = &iter->pg->records[iter->idx++];
3178 if (((iter->flags & FTRACE_ITER_FILTER) &&
3179 !(ftrace_lookup_ip(ops->func_hash->filter_hash, rec->ip))) ||
3181 ((iter->flags & FTRACE_ITER_NOTRACE) &&
3182 !ftrace_lookup_ip(ops->func_hash->notrace_hash, rec->ip)) ||
3184 ((iter->flags & FTRACE_ITER_ENABLED) &&
3185 !(rec->flags & FTRACE_FL_ENABLED))) {
3187 rec = NULL;
3188 goto retry;
3192 if (!rec)
3193 return t_hash_start(m, pos);
3195 iter->func = rec;
3197 return iter;
3200 static void reset_iter_read(struct ftrace_iterator *iter)
3202 iter->pos = 0;
3203 iter->func_pos = 0;
3204 iter->flags &= ~(FTRACE_ITER_PRINTALL | FTRACE_ITER_HASH);
3207 static void *t_start(struct seq_file *m, loff_t *pos)
3209 struct ftrace_iterator *iter = m->private;
3210 struct ftrace_ops *ops = iter->ops;
3211 void *p = NULL;
3212 loff_t l;
3214 mutex_lock(&ftrace_lock);
3216 if (unlikely(ftrace_disabled))
3217 return NULL;
3220 * If an lseek was done, then reset and start from beginning.
3222 if (*pos < iter->pos)
3223 reset_iter_read(iter);
3226 * For set_ftrace_filter reading, if we have the filter
3227 * off, we can short cut and just print out that all
3228 * functions are enabled.
3230 if ((iter->flags & FTRACE_ITER_FILTER &&
3231 ftrace_hash_empty(ops->func_hash->filter_hash)) ||
3232 (iter->flags & FTRACE_ITER_NOTRACE &&
3233 ftrace_hash_empty(ops->func_hash->notrace_hash))) {
3234 if (*pos > 0)
3235 return t_hash_start(m, pos);
3236 iter->flags |= FTRACE_ITER_PRINTALL;
3237 /* reset in case of seek/pread */
3238 iter->flags &= ~FTRACE_ITER_HASH;
3239 return iter;
3242 if (iter->flags & FTRACE_ITER_HASH)
3243 return t_hash_start(m, pos);
3246 * Unfortunately, we need to restart at ftrace_pages_start
3247 * every time we let go of the ftrace_mutex. This is because
3248 * those pointers can change without the lock.
3250 iter->pg = ftrace_pages_start;
3251 iter->idx = 0;
3252 for (l = 0; l <= *pos; ) {
3253 p = t_next(m, p, &l);
3254 if (!p)
3255 break;
3258 if (!p)
3259 return t_hash_start(m, pos);
3261 return iter;
3264 static void t_stop(struct seq_file *m, void *p)
3266 mutex_unlock(&ftrace_lock);
3269 void * __weak
3270 arch_ftrace_trampoline_func(struct ftrace_ops *ops, struct dyn_ftrace *rec)
3272 return NULL;
3275 static void add_trampoline_func(struct seq_file *m, struct ftrace_ops *ops,
3276 struct dyn_ftrace *rec)
3278 void *ptr;
3280 ptr = arch_ftrace_trampoline_func(ops, rec);
3281 if (ptr)
3282 seq_printf(m, " ->%pS", ptr);
3285 static int t_show(struct seq_file *m, void *v)
3287 struct ftrace_iterator *iter = m->private;
3288 struct dyn_ftrace *rec;
3290 if (iter->flags & FTRACE_ITER_HASH)
3291 return t_hash_show(m, iter);
3293 if (iter->flags & FTRACE_ITER_PRINTALL) {
3294 if (iter->flags & FTRACE_ITER_NOTRACE)
3295 seq_puts(m, "#### no functions disabled ####\n");
3296 else
3297 seq_puts(m, "#### all functions enabled ####\n");
3298 return 0;
3301 rec = iter->func;
3303 if (!rec)
3304 return 0;
3306 seq_printf(m, "%ps", (void *)rec->ip);
3307 if (iter->flags & FTRACE_ITER_ENABLED) {
3308 struct ftrace_ops *ops;
3310 seq_printf(m, " (%ld)%s%s",
3311 ftrace_rec_count(rec),
3312 rec->flags & FTRACE_FL_REGS ? " R" : " ",
3313 rec->flags & FTRACE_FL_IPMODIFY ? " I" : " ");
3314 if (rec->flags & FTRACE_FL_TRAMP_EN) {
3315 ops = ftrace_find_tramp_ops_any(rec);
3316 if (ops) {
3317 do {
3318 seq_printf(m, "\ttramp: %pS (%pS)",
3319 (void *)ops->trampoline,
3320 (void *)ops->func);
3321 add_trampoline_func(m, ops, rec);
3322 ops = ftrace_find_tramp_ops_next(rec, ops);
3323 } while (ops);
3324 } else
3325 seq_puts(m, "\ttramp: ERROR!");
3326 } else {
3327 add_trampoline_func(m, NULL, rec);
3331 seq_putc(m, '\n');
3333 return 0;
3336 static const struct seq_operations show_ftrace_seq_ops = {
3337 .start = t_start,
3338 .next = t_next,
3339 .stop = t_stop,
3340 .show = t_show,
3343 static int
3344 ftrace_avail_open(struct inode *inode, struct file *file)
3346 struct ftrace_iterator *iter;
3348 if (unlikely(ftrace_disabled))
3349 return -ENODEV;
3351 iter = __seq_open_private(file, &show_ftrace_seq_ops, sizeof(*iter));
3352 if (iter) {
3353 iter->pg = ftrace_pages_start;
3354 iter->ops = &global_ops;
3357 return iter ? 0 : -ENOMEM;
3360 static int
3361 ftrace_enabled_open(struct inode *inode, struct file *file)
3363 struct ftrace_iterator *iter;
3365 iter = __seq_open_private(file, &show_ftrace_seq_ops, sizeof(*iter));
3366 if (iter) {
3367 iter->pg = ftrace_pages_start;
3368 iter->flags = FTRACE_ITER_ENABLED;
3369 iter->ops = &global_ops;
3372 return iter ? 0 : -ENOMEM;
3376 * ftrace_regex_open - initialize function tracer filter files
3377 * @ops: The ftrace_ops that hold the hash filters
3378 * @flag: The type of filter to process
3379 * @inode: The inode, usually passed in to your open routine
3380 * @file: The file, usually passed in to your open routine
3382 * ftrace_regex_open() initializes the filter files for the
3383 * @ops. Depending on @flag it may process the filter hash or
3384 * the notrace hash of @ops. With this called from the open
3385 * routine, you can use ftrace_filter_write() for the write
3386 * routine if @flag has FTRACE_ITER_FILTER set, or
3387 * ftrace_notrace_write() if @flag has FTRACE_ITER_NOTRACE set.
3388 * tracing_lseek() should be used as the lseek routine, and
3389 * release must call ftrace_regex_release().
3392 ftrace_regex_open(struct ftrace_ops *ops, int flag,
3393 struct inode *inode, struct file *file)
3395 struct ftrace_iterator *iter;
3396 struct ftrace_hash *hash;
3397 int ret = 0;
3399 ftrace_ops_init(ops);
3401 if (unlikely(ftrace_disabled))
3402 return -ENODEV;
3404 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
3405 if (!iter)
3406 return -ENOMEM;
3408 if (trace_parser_get_init(&iter->parser, FTRACE_BUFF_MAX)) {
3409 kfree(iter);
3410 return -ENOMEM;
3413 iter->ops = ops;
3414 iter->flags = flag;
3416 mutex_lock(&ops->func_hash->regex_lock);
3418 if (flag & FTRACE_ITER_NOTRACE)
3419 hash = ops->func_hash->notrace_hash;
3420 else
3421 hash = ops->func_hash->filter_hash;
3423 if (file->f_mode & FMODE_WRITE) {
3424 const int size_bits = FTRACE_HASH_DEFAULT_BITS;
3426 if (file->f_flags & O_TRUNC)
3427 iter->hash = alloc_ftrace_hash(size_bits);
3428 else
3429 iter->hash = alloc_and_copy_ftrace_hash(size_bits, hash);
3431 if (!iter->hash) {
3432 trace_parser_put(&iter->parser);
3433 kfree(iter);
3434 ret = -ENOMEM;
3435 goto out_unlock;
3439 if (file->f_mode & FMODE_READ) {
3440 iter->pg = ftrace_pages_start;
3442 ret = seq_open(file, &show_ftrace_seq_ops);
3443 if (!ret) {
3444 struct seq_file *m = file->private_data;
3445 m->private = iter;
3446 } else {
3447 /* Failed */
3448 free_ftrace_hash(iter->hash);
3449 trace_parser_put(&iter->parser);
3450 kfree(iter);
3452 } else
3453 file->private_data = iter;
3455 out_unlock:
3456 mutex_unlock(&ops->func_hash->regex_lock);
3458 return ret;
3461 static int
3462 ftrace_filter_open(struct inode *inode, struct file *file)
3464 struct ftrace_ops *ops = inode->i_private;
3466 return ftrace_regex_open(ops,
3467 FTRACE_ITER_FILTER | FTRACE_ITER_DO_HASH,
3468 inode, file);
3471 static int
3472 ftrace_notrace_open(struct inode *inode, struct file *file)
3474 struct ftrace_ops *ops = inode->i_private;
3476 return ftrace_regex_open(ops, FTRACE_ITER_NOTRACE,
3477 inode, file);
3480 /* Type for quick search ftrace basic regexes (globs) from filter_parse_regex */
3481 struct ftrace_glob {
3482 char *search;
3483 unsigned len;
3484 int type;
3488 * If symbols in an architecture don't correspond exactly to the user-visible
3489 * name of what they represent, it is possible to define this function to
3490 * perform the necessary adjustments.
3492 char * __weak arch_ftrace_match_adjust(char *str, const char *search)
3494 return str;
3497 static int ftrace_match(char *str, struct ftrace_glob *g)
3499 int matched = 0;
3500 int slen;
3502 str = arch_ftrace_match_adjust(str, g->search);
3504 switch (g->type) {
3505 case MATCH_FULL:
3506 if (strcmp(str, g->search) == 0)
3507 matched = 1;
3508 break;
3509 case MATCH_FRONT_ONLY:
3510 if (strncmp(str, g->search, g->len) == 0)
3511 matched = 1;
3512 break;
3513 case MATCH_MIDDLE_ONLY:
3514 if (strstr(str, g->search))
3515 matched = 1;
3516 break;
3517 case MATCH_END_ONLY:
3518 slen = strlen(str);
3519 if (slen >= g->len &&
3520 memcmp(str + slen - g->len, g->search, g->len) == 0)
3521 matched = 1;
3522 break;
3525 return matched;
3528 static int
3529 enter_record(struct ftrace_hash *hash, struct dyn_ftrace *rec, int clear_filter)
3531 struct ftrace_func_entry *entry;
3532 int ret = 0;
3534 entry = ftrace_lookup_ip(hash, rec->ip);
3535 if (clear_filter) {
3536 /* Do nothing if it doesn't exist */
3537 if (!entry)
3538 return 0;
3540 free_hash_entry(hash, entry);
3541 } else {
3542 /* Do nothing if it exists */
3543 if (entry)
3544 return 0;
3546 ret = add_hash_entry(hash, rec->ip);
3548 return ret;
3551 static int
3552 ftrace_match_record(struct dyn_ftrace *rec, struct ftrace_glob *func_g,
3553 struct ftrace_glob *mod_g, int exclude_mod)
3555 char str[KSYM_SYMBOL_LEN];
3556 char *modname;
3558 kallsyms_lookup(rec->ip, NULL, NULL, &modname, str);
3560 if (mod_g) {
3561 int mod_matches = (modname) ? ftrace_match(modname, mod_g) : 0;
3563 /* blank module name to match all modules */
3564 if (!mod_g->len) {
3565 /* blank module globbing: modname xor exclude_mod */
3566 if ((!exclude_mod) != (!modname))
3567 goto func_match;
3568 return 0;
3571 /* not matching the module */
3572 if (!modname || !mod_matches) {
3573 if (exclude_mod)
3574 goto func_match;
3575 else
3576 return 0;
3579 if (mod_matches && exclude_mod)
3580 return 0;
3582 func_match:
3583 /* blank search means to match all funcs in the mod */
3584 if (!func_g->len)
3585 return 1;
3588 return ftrace_match(str, func_g);
3591 static int
3592 match_records(struct ftrace_hash *hash, char *func, int len, char *mod)
3594 struct ftrace_page *pg;
3595 struct dyn_ftrace *rec;
3596 struct ftrace_glob func_g = { .type = MATCH_FULL };
3597 struct ftrace_glob mod_g = { .type = MATCH_FULL };
3598 struct ftrace_glob *mod_match = (mod) ? &mod_g : NULL;
3599 int exclude_mod = 0;
3600 int found = 0;
3601 int ret;
3602 int clear_filter = 0;
3604 if (func) {
3605 func_g.type = filter_parse_regex(func, len, &func_g.search,
3606 &clear_filter);
3607 func_g.len = strlen(func_g.search);
3610 if (mod) {
3611 mod_g.type = filter_parse_regex(mod, strlen(mod),
3612 &mod_g.search, &exclude_mod);
3613 mod_g.len = strlen(mod_g.search);
3616 mutex_lock(&ftrace_lock);
3618 if (unlikely(ftrace_disabled))
3619 goto out_unlock;
3621 do_for_each_ftrace_rec(pg, rec) {
3623 if (rec->flags & FTRACE_FL_DISABLED)
3624 continue;
3626 if (ftrace_match_record(rec, &func_g, mod_match, exclude_mod)) {
3627 ret = enter_record(hash, rec, clear_filter);
3628 if (ret < 0) {
3629 found = ret;
3630 goto out_unlock;
3632 found = 1;
3634 } while_for_each_ftrace_rec();
3635 out_unlock:
3636 mutex_unlock(&ftrace_lock);
3638 return found;
3641 static int
3642 ftrace_match_records(struct ftrace_hash *hash, char *buff, int len)
3644 return match_records(hash, buff, len, NULL);
3649 * We register the module command as a template to show others how
3650 * to register the a command as well.
3653 static int
3654 ftrace_mod_callback(struct ftrace_hash *hash,
3655 char *func, char *cmd, char *module, int enable)
3657 int ret;
3660 * cmd == 'mod' because we only registered this func
3661 * for the 'mod' ftrace_func_command.
3662 * But if you register one func with multiple commands,
3663 * you can tell which command was used by the cmd
3664 * parameter.
3666 ret = match_records(hash, func, strlen(func), module);
3667 if (!ret)
3668 return -EINVAL;
3669 if (ret < 0)
3670 return ret;
3671 return 0;
3674 static struct ftrace_func_command ftrace_mod_cmd = {
3675 .name = "mod",
3676 .func = ftrace_mod_callback,
3679 static int __init ftrace_mod_cmd_init(void)
3681 return register_ftrace_command(&ftrace_mod_cmd);
3683 core_initcall(ftrace_mod_cmd_init);
3685 static void function_trace_probe_call(unsigned long ip, unsigned long parent_ip,
3686 struct ftrace_ops *op, struct pt_regs *pt_regs)
3688 struct ftrace_func_probe *entry;
3689 struct hlist_head *hhd;
3690 unsigned long key;
3692 key = hash_long(ip, FTRACE_HASH_BITS);
3694 hhd = &ftrace_func_hash[key];
3696 if (hlist_empty(hhd))
3697 return;
3700 * Disable preemption for these calls to prevent a RCU grace
3701 * period. This syncs the hash iteration and freeing of items
3702 * on the hash. rcu_read_lock is too dangerous here.
3704 preempt_disable_notrace();
3705 hlist_for_each_entry_rcu_notrace(entry, hhd, node) {
3706 if (entry->ip == ip)
3707 entry->ops->func(ip, parent_ip, &entry->data);
3709 preempt_enable_notrace();
3712 static struct ftrace_ops trace_probe_ops __read_mostly =
3714 .func = function_trace_probe_call,
3715 .flags = FTRACE_OPS_FL_INITIALIZED,
3716 INIT_OPS_HASH(trace_probe_ops)
3719 static int ftrace_probe_registered;
3721 static void __enable_ftrace_function_probe(struct ftrace_ops_hash *old_hash)
3723 int ret;
3724 int i;
3726 if (ftrace_probe_registered) {
3727 /* still need to update the function call sites */
3728 if (ftrace_enabled)
3729 ftrace_run_modify_code(&trace_probe_ops, FTRACE_UPDATE_CALLS,
3730 old_hash);
3731 return;
3734 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
3735 struct hlist_head *hhd = &ftrace_func_hash[i];
3736 if (hhd->first)
3737 break;
3739 /* Nothing registered? */
3740 if (i == FTRACE_FUNC_HASHSIZE)
3741 return;
3743 ret = ftrace_startup(&trace_probe_ops, 0);
3745 ftrace_probe_registered = 1;
3748 static bool __disable_ftrace_function_probe(void)
3750 int i;
3752 if (!ftrace_probe_registered)
3753 return false;
3755 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
3756 struct hlist_head *hhd = &ftrace_func_hash[i];
3757 if (hhd->first)
3758 return false;
3761 /* no more funcs left */
3762 ftrace_shutdown(&trace_probe_ops, 0);
3764 ftrace_probe_registered = 0;
3765 return true;
3769 static void ftrace_free_entry(struct ftrace_func_probe *entry)
3771 if (entry->ops->free)
3772 entry->ops->free(entry->ops, entry->ip, &entry->data);
3773 kfree(entry);
3777 register_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
3778 void *data)
3780 struct ftrace_ops_hash old_hash_ops;
3781 struct ftrace_func_probe *entry;
3782 struct ftrace_glob func_g;
3783 struct ftrace_hash **orig_hash = &trace_probe_ops.func_hash->filter_hash;
3784 struct ftrace_hash *old_hash = *orig_hash;
3785 struct ftrace_hash *hash;
3786 struct ftrace_page *pg;
3787 struct dyn_ftrace *rec;
3788 int not;
3789 unsigned long key;
3790 int count = 0;
3791 int ret;
3793 func_g.type = filter_parse_regex(glob, strlen(glob),
3794 &func_g.search, &not);
3795 func_g.len = strlen(func_g.search);
3797 /* we do not support '!' for function probes */
3798 if (WARN_ON(not))
3799 return -EINVAL;
3801 mutex_lock(&trace_probe_ops.func_hash->regex_lock);
3803 old_hash_ops.filter_hash = old_hash;
3804 /* Probes only have filters */
3805 old_hash_ops.notrace_hash = NULL;
3807 hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, old_hash);
3808 if (!hash) {
3809 count = -ENOMEM;
3810 goto out;
3813 if (unlikely(ftrace_disabled)) {
3814 count = -ENODEV;
3815 goto out;
3818 mutex_lock(&ftrace_lock);
3820 do_for_each_ftrace_rec(pg, rec) {
3822 if (rec->flags & FTRACE_FL_DISABLED)
3823 continue;
3825 if (!ftrace_match_record(rec, &func_g, NULL, 0))
3826 continue;
3828 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
3829 if (!entry) {
3830 /* If we did not process any, then return error */
3831 if (!count)
3832 count = -ENOMEM;
3833 goto out_unlock;
3836 count++;
3838 entry->data = data;
3841 * The caller might want to do something special
3842 * for each function we find. We call the callback
3843 * to give the caller an opportunity to do so.
3845 if (ops->init) {
3846 if (ops->init(ops, rec->ip, &entry->data) < 0) {
3847 /* caller does not like this func */
3848 kfree(entry);
3849 continue;
3853 ret = enter_record(hash, rec, 0);
3854 if (ret < 0) {
3855 kfree(entry);
3856 count = ret;
3857 goto out_unlock;
3860 entry->ops = ops;
3861 entry->ip = rec->ip;
3863 key = hash_long(entry->ip, FTRACE_HASH_BITS);
3864 hlist_add_head_rcu(&entry->node, &ftrace_func_hash[key]);
3866 } while_for_each_ftrace_rec();
3868 ret = ftrace_hash_move(&trace_probe_ops, 1, orig_hash, hash);
3870 __enable_ftrace_function_probe(&old_hash_ops);
3872 if (!ret)
3873 free_ftrace_hash_rcu(old_hash);
3874 else
3875 count = ret;
3877 out_unlock:
3878 mutex_unlock(&ftrace_lock);
3879 out:
3880 mutex_unlock(&trace_probe_ops.func_hash->regex_lock);
3881 free_ftrace_hash(hash);
3883 return count;
3886 enum {
3887 PROBE_TEST_FUNC = 1,
3888 PROBE_TEST_DATA = 2
3891 static void
3892 __unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
3893 void *data, int flags)
3895 struct ftrace_ops_hash old_hash_ops;
3896 struct ftrace_func_entry *rec_entry;
3897 struct ftrace_func_probe *entry;
3898 struct ftrace_func_probe *p;
3899 struct ftrace_glob func_g;
3900 struct ftrace_hash **orig_hash = &trace_probe_ops.func_hash->filter_hash;
3901 struct ftrace_hash *old_hash = *orig_hash;
3902 struct list_head free_list;
3903 struct ftrace_hash *hash;
3904 struct hlist_node *tmp;
3905 char str[KSYM_SYMBOL_LEN];
3906 int i, ret;
3907 bool disabled;
3909 if (glob && (strcmp(glob, "*") == 0 || !strlen(glob)))
3910 func_g.search = NULL;
3911 else if (glob) {
3912 int not;
3914 func_g.type = filter_parse_regex(glob, strlen(glob),
3915 &func_g.search, &not);
3916 func_g.len = strlen(func_g.search);
3918 /* we do not support '!' for function probes */
3919 if (WARN_ON(not))
3920 return;
3923 mutex_lock(&trace_probe_ops.func_hash->regex_lock);
3925 old_hash_ops.filter_hash = old_hash;
3926 /* Probes only have filters */
3927 old_hash_ops.notrace_hash = NULL;
3929 hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, *orig_hash);
3930 if (!hash)
3931 /* Hmm, should report this somehow */
3932 goto out_unlock;
3934 INIT_LIST_HEAD(&free_list);
3936 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
3937 struct hlist_head *hhd = &ftrace_func_hash[i];
3939 hlist_for_each_entry_safe(entry, tmp, hhd, node) {
3941 /* break up if statements for readability */
3942 if ((flags & PROBE_TEST_FUNC) && entry->ops != ops)
3943 continue;
3945 if ((flags & PROBE_TEST_DATA) && entry->data != data)
3946 continue;
3948 /* do this last, since it is the most expensive */
3949 if (func_g.search) {
3950 kallsyms_lookup(entry->ip, NULL, NULL,
3951 NULL, str);
3952 if (!ftrace_match(str, &func_g))
3953 continue;
3956 rec_entry = ftrace_lookup_ip(hash, entry->ip);
3957 /* It is possible more than one entry had this ip */
3958 if (rec_entry)
3959 free_hash_entry(hash, rec_entry);
3961 hlist_del_rcu(&entry->node);
3962 list_add(&entry->free_list, &free_list);
3965 mutex_lock(&ftrace_lock);
3966 disabled = __disable_ftrace_function_probe();
3968 * Remove after the disable is called. Otherwise, if the last
3969 * probe is removed, a null hash means *all enabled*.
3971 ret = ftrace_hash_move(&trace_probe_ops, 1, orig_hash, hash);
3973 /* still need to update the function call sites */
3974 if (ftrace_enabled && !disabled)
3975 ftrace_run_modify_code(&trace_probe_ops, FTRACE_UPDATE_CALLS,
3976 &old_hash_ops);
3977 synchronize_sched();
3978 if (!ret)
3979 free_ftrace_hash_rcu(old_hash);
3981 list_for_each_entry_safe(entry, p, &free_list, free_list) {
3982 list_del(&entry->free_list);
3983 ftrace_free_entry(entry);
3985 mutex_unlock(&ftrace_lock);
3987 out_unlock:
3988 mutex_unlock(&trace_probe_ops.func_hash->regex_lock);
3989 free_ftrace_hash(hash);
3992 void
3993 unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
3994 void *data)
3996 __unregister_ftrace_function_probe(glob, ops, data,
3997 PROBE_TEST_FUNC | PROBE_TEST_DATA);
4000 void
4001 unregister_ftrace_function_probe_func(char *glob, struct ftrace_probe_ops *ops)
4003 __unregister_ftrace_function_probe(glob, ops, NULL, PROBE_TEST_FUNC);
4006 void unregister_ftrace_function_probe_all(char *glob)
4008 __unregister_ftrace_function_probe(glob, NULL, NULL, 0);
4011 static LIST_HEAD(ftrace_commands);
4012 static DEFINE_MUTEX(ftrace_cmd_mutex);
4015 * Currently we only register ftrace commands from __init, so mark this
4016 * __init too.
4018 __init int register_ftrace_command(struct ftrace_func_command *cmd)
4020 struct ftrace_func_command *p;
4021 int ret = 0;
4023 mutex_lock(&ftrace_cmd_mutex);
4024 list_for_each_entry(p, &ftrace_commands, list) {
4025 if (strcmp(cmd->name, p->name) == 0) {
4026 ret = -EBUSY;
4027 goto out_unlock;
4030 list_add(&cmd->list, &ftrace_commands);
4031 out_unlock:
4032 mutex_unlock(&ftrace_cmd_mutex);
4034 return ret;
4038 * Currently we only unregister ftrace commands from __init, so mark
4039 * this __init too.
4041 __init int unregister_ftrace_command(struct ftrace_func_command *cmd)
4043 struct ftrace_func_command *p, *n;
4044 int ret = -ENODEV;
4046 mutex_lock(&ftrace_cmd_mutex);
4047 list_for_each_entry_safe(p, n, &ftrace_commands, list) {
4048 if (strcmp(cmd->name, p->name) == 0) {
4049 ret = 0;
4050 list_del_init(&p->list);
4051 goto out_unlock;
4054 out_unlock:
4055 mutex_unlock(&ftrace_cmd_mutex);
4057 return ret;
4060 static int ftrace_process_regex(struct ftrace_hash *hash,
4061 char *buff, int len, int enable)
4063 char *func, *command, *next = buff;
4064 struct ftrace_func_command *p;
4065 int ret = -EINVAL;
4067 func = strsep(&next, ":");
4069 if (!next) {
4070 ret = ftrace_match_records(hash, func, len);
4071 if (!ret)
4072 ret = -EINVAL;
4073 if (ret < 0)
4074 return ret;
4075 return 0;
4078 /* command found */
4080 command = strsep(&next, ":");
4082 mutex_lock(&ftrace_cmd_mutex);
4083 list_for_each_entry(p, &ftrace_commands, list) {
4084 if (strcmp(p->name, command) == 0) {
4085 ret = p->func(hash, func, command, next, enable);
4086 goto out_unlock;
4089 out_unlock:
4090 mutex_unlock(&ftrace_cmd_mutex);
4092 return ret;
4095 static ssize_t
4096 ftrace_regex_write(struct file *file, const char __user *ubuf,
4097 size_t cnt, loff_t *ppos, int enable)
4099 struct ftrace_iterator *iter;
4100 struct trace_parser *parser;
4101 ssize_t ret, read;
4103 if (!cnt)
4104 return 0;
4106 if (file->f_mode & FMODE_READ) {
4107 struct seq_file *m = file->private_data;
4108 iter = m->private;
4109 } else
4110 iter = file->private_data;
4112 if (unlikely(ftrace_disabled))
4113 return -ENODEV;
4115 /* iter->hash is a local copy, so we don't need regex_lock */
4117 parser = &iter->parser;
4118 read = trace_get_user(parser, ubuf, cnt, ppos);
4120 if (read >= 0 && trace_parser_loaded(parser) &&
4121 !trace_parser_cont(parser)) {
4122 ret = ftrace_process_regex(iter->hash, parser->buffer,
4123 parser->idx, enable);
4124 trace_parser_clear(parser);
4125 if (ret < 0)
4126 goto out;
4129 ret = read;
4130 out:
4131 return ret;
4134 ssize_t
4135 ftrace_filter_write(struct file *file, const char __user *ubuf,
4136 size_t cnt, loff_t *ppos)
4138 return ftrace_regex_write(file, ubuf, cnt, ppos, 1);
4141 ssize_t
4142 ftrace_notrace_write(struct file *file, const char __user *ubuf,
4143 size_t cnt, loff_t *ppos)
4145 return ftrace_regex_write(file, ubuf, cnt, ppos, 0);
4148 static int
4149 ftrace_match_addr(struct ftrace_hash *hash, unsigned long ip, int remove)
4151 struct ftrace_func_entry *entry;
4153 if (!ftrace_location(ip))
4154 return -EINVAL;
4156 if (remove) {
4157 entry = ftrace_lookup_ip(hash, ip);
4158 if (!entry)
4159 return -ENOENT;
4160 free_hash_entry(hash, entry);
4161 return 0;
4164 return add_hash_entry(hash, ip);
4167 static void ftrace_ops_update_code(struct ftrace_ops *ops,
4168 struct ftrace_ops_hash *old_hash)
4170 struct ftrace_ops *op;
4172 if (!ftrace_enabled)
4173 return;
4175 if (ops->flags & FTRACE_OPS_FL_ENABLED) {
4176 ftrace_run_modify_code(ops, FTRACE_UPDATE_CALLS, old_hash);
4177 return;
4181 * If this is the shared global_ops filter, then we need to
4182 * check if there is another ops that shares it, is enabled.
4183 * If so, we still need to run the modify code.
4185 if (ops->func_hash != &global_ops.local_hash)
4186 return;
4188 do_for_each_ftrace_op(op, ftrace_ops_list) {
4189 if (op->func_hash == &global_ops.local_hash &&
4190 op->flags & FTRACE_OPS_FL_ENABLED) {
4191 ftrace_run_modify_code(op, FTRACE_UPDATE_CALLS, old_hash);
4192 /* Only need to do this once */
4193 return;
4195 } while_for_each_ftrace_op(op);
4198 static int
4199 ftrace_set_hash(struct ftrace_ops *ops, unsigned char *buf, int len,
4200 unsigned long ip, int remove, int reset, int enable)
4202 struct ftrace_hash **orig_hash;
4203 struct ftrace_ops_hash old_hash_ops;
4204 struct ftrace_hash *old_hash;
4205 struct ftrace_hash *hash;
4206 int ret;
4208 if (unlikely(ftrace_disabled))
4209 return -ENODEV;
4211 mutex_lock(&ops->func_hash->regex_lock);
4213 if (enable)
4214 orig_hash = &ops->func_hash->filter_hash;
4215 else
4216 orig_hash = &ops->func_hash->notrace_hash;
4218 if (reset)
4219 hash = alloc_ftrace_hash(FTRACE_HASH_DEFAULT_BITS);
4220 else
4221 hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, *orig_hash);
4223 if (!hash) {
4224 ret = -ENOMEM;
4225 goto out_regex_unlock;
4228 if (buf && !ftrace_match_records(hash, buf, len)) {
4229 ret = -EINVAL;
4230 goto out_regex_unlock;
4232 if (ip) {
4233 ret = ftrace_match_addr(hash, ip, remove);
4234 if (ret < 0)
4235 goto out_regex_unlock;
4238 mutex_lock(&ftrace_lock);
4239 old_hash = *orig_hash;
4240 old_hash_ops.filter_hash = ops->func_hash->filter_hash;
4241 old_hash_ops.notrace_hash = ops->func_hash->notrace_hash;
4242 ret = ftrace_hash_move(ops, enable, orig_hash, hash);
4243 if (!ret) {
4244 ftrace_ops_update_code(ops, &old_hash_ops);
4245 free_ftrace_hash_rcu(old_hash);
4247 mutex_unlock(&ftrace_lock);
4249 out_regex_unlock:
4250 mutex_unlock(&ops->func_hash->regex_lock);
4252 free_ftrace_hash(hash);
4253 return ret;
4256 static int
4257 ftrace_set_addr(struct ftrace_ops *ops, unsigned long ip, int remove,
4258 int reset, int enable)
4260 return ftrace_set_hash(ops, 0, 0, ip, remove, reset, enable);
4264 * ftrace_set_filter_ip - set a function to filter on in ftrace by address
4265 * @ops - the ops to set the filter with
4266 * @ip - the address to add to or remove from the filter.
4267 * @remove - non zero to remove the ip from the filter
4268 * @reset - non zero to reset all filters before applying this filter.
4270 * Filters denote which functions should be enabled when tracing is enabled
4271 * If @ip is NULL, it failes to update filter.
4273 int ftrace_set_filter_ip(struct ftrace_ops *ops, unsigned long ip,
4274 int remove, int reset)
4276 ftrace_ops_init(ops);
4277 return ftrace_set_addr(ops, ip, remove, reset, 1);
4279 EXPORT_SYMBOL_GPL(ftrace_set_filter_ip);
4281 static int
4282 ftrace_set_regex(struct ftrace_ops *ops, unsigned char *buf, int len,
4283 int reset, int enable)
4285 return ftrace_set_hash(ops, buf, len, 0, 0, reset, enable);
4289 * ftrace_set_filter - set a function to filter on in ftrace
4290 * @ops - the ops to set the filter with
4291 * @buf - the string that holds the function filter text.
4292 * @len - the length of the string.
4293 * @reset - non zero to reset all filters before applying this filter.
4295 * Filters denote which functions should be enabled when tracing is enabled.
4296 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
4298 int ftrace_set_filter(struct ftrace_ops *ops, unsigned char *buf,
4299 int len, int reset)
4301 ftrace_ops_init(ops);
4302 return ftrace_set_regex(ops, buf, len, reset, 1);
4304 EXPORT_SYMBOL_GPL(ftrace_set_filter);
4307 * ftrace_set_notrace - set a function to not trace in ftrace
4308 * @ops - the ops to set the notrace filter with
4309 * @buf - the string that holds the function notrace text.
4310 * @len - the length of the string.
4311 * @reset - non zero to reset all filters before applying this filter.
4313 * Notrace Filters denote which functions should not be enabled when tracing
4314 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
4315 * for tracing.
4317 int ftrace_set_notrace(struct ftrace_ops *ops, unsigned char *buf,
4318 int len, int reset)
4320 ftrace_ops_init(ops);
4321 return ftrace_set_regex(ops, buf, len, reset, 0);
4323 EXPORT_SYMBOL_GPL(ftrace_set_notrace);
4325 * ftrace_set_global_filter - set a function to filter on with global tracers
4326 * @buf - the string that holds the function filter text.
4327 * @len - the length of the string.
4328 * @reset - non zero to reset all filters before applying this filter.
4330 * Filters denote which functions should be enabled when tracing is enabled.
4331 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
4333 void ftrace_set_global_filter(unsigned char *buf, int len, int reset)
4335 ftrace_set_regex(&global_ops, buf, len, reset, 1);
4337 EXPORT_SYMBOL_GPL(ftrace_set_global_filter);
4340 * ftrace_set_global_notrace - set a function to not trace with global tracers
4341 * @buf - the string that holds the function notrace text.
4342 * @len - the length of the string.
4343 * @reset - non zero to reset all filters before applying this filter.
4345 * Notrace Filters denote which functions should not be enabled when tracing
4346 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
4347 * for tracing.
4349 void ftrace_set_global_notrace(unsigned char *buf, int len, int reset)
4351 ftrace_set_regex(&global_ops, buf, len, reset, 0);
4353 EXPORT_SYMBOL_GPL(ftrace_set_global_notrace);
4356 * command line interface to allow users to set filters on boot up.
4358 #define FTRACE_FILTER_SIZE COMMAND_LINE_SIZE
4359 static char ftrace_notrace_buf[FTRACE_FILTER_SIZE] __initdata;
4360 static char ftrace_filter_buf[FTRACE_FILTER_SIZE] __initdata;
4362 /* Used by function selftest to not test if filter is set */
4363 bool ftrace_filter_param __initdata;
4365 static int __init set_ftrace_notrace(char *str)
4367 ftrace_filter_param = true;
4368 strlcpy(ftrace_notrace_buf, str, FTRACE_FILTER_SIZE);
4369 return 1;
4371 __setup("ftrace_notrace=", set_ftrace_notrace);
4373 static int __init set_ftrace_filter(char *str)
4375 ftrace_filter_param = true;
4376 strlcpy(ftrace_filter_buf, str, FTRACE_FILTER_SIZE);
4377 return 1;
4379 __setup("ftrace_filter=", set_ftrace_filter);
4381 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
4382 static char ftrace_graph_buf[FTRACE_FILTER_SIZE] __initdata;
4383 static char ftrace_graph_notrace_buf[FTRACE_FILTER_SIZE] __initdata;
4384 static int ftrace_set_func(unsigned long *array, int *idx, int size, char *buffer);
4386 static int __init set_graph_function(char *str)
4388 strlcpy(ftrace_graph_buf, str, FTRACE_FILTER_SIZE);
4389 return 1;
4391 __setup("ftrace_graph_filter=", set_graph_function);
4393 static int __init set_graph_notrace_function(char *str)
4395 strlcpy(ftrace_graph_notrace_buf, str, FTRACE_FILTER_SIZE);
4396 return 1;
4398 __setup("ftrace_graph_notrace=", set_graph_notrace_function);
4400 static void __init set_ftrace_early_graph(char *buf, int enable)
4402 int ret;
4403 char *func;
4404 unsigned long *table = ftrace_graph_funcs;
4405 int *count = &ftrace_graph_count;
4407 if (!enable) {
4408 table = ftrace_graph_notrace_funcs;
4409 count = &ftrace_graph_notrace_count;
4412 while (buf) {
4413 func = strsep(&buf, ",");
4414 /* we allow only one expression at a time */
4415 ret = ftrace_set_func(table, count, FTRACE_GRAPH_MAX_FUNCS, func);
4416 if (ret)
4417 printk(KERN_DEBUG "ftrace: function %s not "
4418 "traceable\n", func);
4421 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
4423 void __init
4424 ftrace_set_early_filter(struct ftrace_ops *ops, char *buf, int enable)
4426 char *func;
4428 ftrace_ops_init(ops);
4430 while (buf) {
4431 func = strsep(&buf, ",");
4432 ftrace_set_regex(ops, func, strlen(func), 0, enable);
4436 static void __init set_ftrace_early_filters(void)
4438 if (ftrace_filter_buf[0])
4439 ftrace_set_early_filter(&global_ops, ftrace_filter_buf, 1);
4440 if (ftrace_notrace_buf[0])
4441 ftrace_set_early_filter(&global_ops, ftrace_notrace_buf, 0);
4442 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
4443 if (ftrace_graph_buf[0])
4444 set_ftrace_early_graph(ftrace_graph_buf, 1);
4445 if (ftrace_graph_notrace_buf[0])
4446 set_ftrace_early_graph(ftrace_graph_notrace_buf, 0);
4447 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
4450 int ftrace_regex_release(struct inode *inode, struct file *file)
4452 struct seq_file *m = (struct seq_file *)file->private_data;
4453 struct ftrace_ops_hash old_hash_ops;
4454 struct ftrace_iterator *iter;
4455 struct ftrace_hash **orig_hash;
4456 struct ftrace_hash *old_hash;
4457 struct trace_parser *parser;
4458 int filter_hash;
4459 int ret;
4461 if (file->f_mode & FMODE_READ) {
4462 iter = m->private;
4463 seq_release(inode, file);
4464 } else
4465 iter = file->private_data;
4467 parser = &iter->parser;
4468 if (trace_parser_loaded(parser)) {
4469 parser->buffer[parser->idx] = 0;
4470 ftrace_match_records(iter->hash, parser->buffer, parser->idx);
4473 trace_parser_put(parser);
4475 mutex_lock(&iter->ops->func_hash->regex_lock);
4477 if (file->f_mode & FMODE_WRITE) {
4478 filter_hash = !!(iter->flags & FTRACE_ITER_FILTER);
4480 if (filter_hash)
4481 orig_hash = &iter->ops->func_hash->filter_hash;
4482 else
4483 orig_hash = &iter->ops->func_hash->notrace_hash;
4485 mutex_lock(&ftrace_lock);
4486 old_hash = *orig_hash;
4487 old_hash_ops.filter_hash = iter->ops->func_hash->filter_hash;
4488 old_hash_ops.notrace_hash = iter->ops->func_hash->notrace_hash;
4489 ret = ftrace_hash_move(iter->ops, filter_hash,
4490 orig_hash, iter->hash);
4491 if (!ret) {
4492 ftrace_ops_update_code(iter->ops, &old_hash_ops);
4493 free_ftrace_hash_rcu(old_hash);
4495 mutex_unlock(&ftrace_lock);
4498 mutex_unlock(&iter->ops->func_hash->regex_lock);
4499 free_ftrace_hash(iter->hash);
4500 kfree(iter);
4502 return 0;
4505 static const struct file_operations ftrace_avail_fops = {
4506 .open = ftrace_avail_open,
4507 .read = seq_read,
4508 .llseek = seq_lseek,
4509 .release = seq_release_private,
4512 static const struct file_operations ftrace_enabled_fops = {
4513 .open = ftrace_enabled_open,
4514 .read = seq_read,
4515 .llseek = seq_lseek,
4516 .release = seq_release_private,
4519 static const struct file_operations ftrace_filter_fops = {
4520 .open = ftrace_filter_open,
4521 .read = seq_read,
4522 .write = ftrace_filter_write,
4523 .llseek = tracing_lseek,
4524 .release = ftrace_regex_release,
4527 static const struct file_operations ftrace_notrace_fops = {
4528 .open = ftrace_notrace_open,
4529 .read = seq_read,
4530 .write = ftrace_notrace_write,
4531 .llseek = tracing_lseek,
4532 .release = ftrace_regex_release,
4535 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
4537 static DEFINE_MUTEX(graph_lock);
4539 int ftrace_graph_count;
4540 int ftrace_graph_notrace_count;
4541 unsigned long ftrace_graph_funcs[FTRACE_GRAPH_MAX_FUNCS] __read_mostly;
4542 unsigned long ftrace_graph_notrace_funcs[FTRACE_GRAPH_MAX_FUNCS] __read_mostly;
4544 struct ftrace_graph_data {
4545 unsigned long *table;
4546 size_t size;
4547 int *count;
4548 const struct seq_operations *seq_ops;
4551 static void *
4552 __g_next(struct seq_file *m, loff_t *pos)
4554 struct ftrace_graph_data *fgd = m->private;
4556 if (*pos >= *fgd->count)
4557 return NULL;
4558 return &fgd->table[*pos];
4561 static void *
4562 g_next(struct seq_file *m, void *v, loff_t *pos)
4564 (*pos)++;
4565 return __g_next(m, pos);
4568 static void *g_start(struct seq_file *m, loff_t *pos)
4570 struct ftrace_graph_data *fgd = m->private;
4572 mutex_lock(&graph_lock);
4574 /* Nothing, tell g_show to print all functions are enabled */
4575 if (!*fgd->count && !*pos)
4576 return (void *)1;
4578 return __g_next(m, pos);
4581 static void g_stop(struct seq_file *m, void *p)
4583 mutex_unlock(&graph_lock);
4586 static int g_show(struct seq_file *m, void *v)
4588 unsigned long *ptr = v;
4590 if (!ptr)
4591 return 0;
4593 if (ptr == (unsigned long *)1) {
4594 struct ftrace_graph_data *fgd = m->private;
4596 if (fgd->table == ftrace_graph_funcs)
4597 seq_puts(m, "#### all functions enabled ####\n");
4598 else
4599 seq_puts(m, "#### no functions disabled ####\n");
4600 return 0;
4603 seq_printf(m, "%ps\n", (void *)*ptr);
4605 return 0;
4608 static const struct seq_operations ftrace_graph_seq_ops = {
4609 .start = g_start,
4610 .next = g_next,
4611 .stop = g_stop,
4612 .show = g_show,
4615 static int
4616 __ftrace_graph_open(struct inode *inode, struct file *file,
4617 struct ftrace_graph_data *fgd)
4619 int ret = 0;
4621 mutex_lock(&graph_lock);
4622 if ((file->f_mode & FMODE_WRITE) &&
4623 (file->f_flags & O_TRUNC)) {
4624 *fgd->count = 0;
4625 memset(fgd->table, 0, fgd->size * sizeof(*fgd->table));
4627 mutex_unlock(&graph_lock);
4629 if (file->f_mode & FMODE_READ) {
4630 ret = seq_open(file, fgd->seq_ops);
4631 if (!ret) {
4632 struct seq_file *m = file->private_data;
4633 m->private = fgd;
4635 } else
4636 file->private_data = fgd;
4638 return ret;
4641 static int
4642 ftrace_graph_open(struct inode *inode, struct file *file)
4644 struct ftrace_graph_data *fgd;
4646 if (unlikely(ftrace_disabled))
4647 return -ENODEV;
4649 fgd = kmalloc(sizeof(*fgd), GFP_KERNEL);
4650 if (fgd == NULL)
4651 return -ENOMEM;
4653 fgd->table = ftrace_graph_funcs;
4654 fgd->size = FTRACE_GRAPH_MAX_FUNCS;
4655 fgd->count = &ftrace_graph_count;
4656 fgd->seq_ops = &ftrace_graph_seq_ops;
4658 return __ftrace_graph_open(inode, file, fgd);
4661 static int
4662 ftrace_graph_notrace_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_notrace_funcs;
4674 fgd->size = FTRACE_GRAPH_MAX_FUNCS;
4675 fgd->count = &ftrace_graph_notrace_count;
4676 fgd->seq_ops = &ftrace_graph_seq_ops;
4678 return __ftrace_graph_open(inode, file, fgd);
4681 static int
4682 ftrace_graph_release(struct inode *inode, struct file *file)
4684 if (file->f_mode & FMODE_READ) {
4685 struct seq_file *m = file->private_data;
4687 kfree(m->private);
4688 seq_release(inode, file);
4689 } else {
4690 kfree(file->private_data);
4693 return 0;
4696 static int
4697 ftrace_set_func(unsigned long *array, int *idx, int size, char *buffer)
4699 struct ftrace_glob func_g;
4700 struct dyn_ftrace *rec;
4701 struct ftrace_page *pg;
4702 int fail = 1;
4703 int not;
4704 bool exists;
4705 int i;
4707 /* decode regex */
4708 func_g.type = filter_parse_regex(buffer, strlen(buffer),
4709 &func_g.search, &not);
4710 if (!not && *idx >= size)
4711 return -EBUSY;
4713 func_g.len = strlen(func_g.search);
4715 mutex_lock(&ftrace_lock);
4717 if (unlikely(ftrace_disabled)) {
4718 mutex_unlock(&ftrace_lock);
4719 return -ENODEV;
4722 do_for_each_ftrace_rec(pg, rec) {
4724 if (rec->flags & FTRACE_FL_DISABLED)
4725 continue;
4727 if (ftrace_match_record(rec, &func_g, NULL, 0)) {
4728 /* if it is in the array */
4729 exists = false;
4730 for (i = 0; i < *idx; i++) {
4731 if (array[i] == rec->ip) {
4732 exists = true;
4733 break;
4737 if (!not) {
4738 fail = 0;
4739 if (!exists) {
4740 array[(*idx)++] = rec->ip;
4741 if (*idx >= size)
4742 goto out;
4744 } else {
4745 if (exists) {
4746 array[i] = array[--(*idx)];
4747 array[*idx] = 0;
4748 fail = 0;
4752 } while_for_each_ftrace_rec();
4753 out:
4754 mutex_unlock(&ftrace_lock);
4756 if (fail)
4757 return -EINVAL;
4759 return 0;
4762 static ssize_t
4763 ftrace_graph_write(struct file *file, const char __user *ubuf,
4764 size_t cnt, loff_t *ppos)
4766 struct trace_parser parser;
4767 ssize_t read, ret = 0;
4768 struct ftrace_graph_data *fgd = file->private_data;
4770 if (!cnt)
4771 return 0;
4773 if (trace_parser_get_init(&parser, FTRACE_BUFF_MAX))
4774 return -ENOMEM;
4776 read = trace_get_user(&parser, ubuf, cnt, ppos);
4778 if (read >= 0 && trace_parser_loaded((&parser))) {
4779 parser.buffer[parser.idx] = 0;
4781 mutex_lock(&graph_lock);
4783 /* we allow only one expression at a time */
4784 ret = ftrace_set_func(fgd->table, fgd->count, fgd->size,
4785 parser.buffer);
4787 mutex_unlock(&graph_lock);
4790 if (!ret)
4791 ret = read;
4793 trace_parser_put(&parser);
4795 return ret;
4798 static const struct file_operations ftrace_graph_fops = {
4799 .open = ftrace_graph_open,
4800 .read = seq_read,
4801 .write = ftrace_graph_write,
4802 .llseek = tracing_lseek,
4803 .release = ftrace_graph_release,
4806 static const struct file_operations ftrace_graph_notrace_fops = {
4807 .open = ftrace_graph_notrace_open,
4808 .read = seq_read,
4809 .write = ftrace_graph_write,
4810 .llseek = tracing_lseek,
4811 .release = ftrace_graph_release,
4813 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
4815 void ftrace_create_filter_files(struct ftrace_ops *ops,
4816 struct dentry *parent)
4819 trace_create_file("set_ftrace_filter", 0644, parent,
4820 ops, &ftrace_filter_fops);
4822 trace_create_file("set_ftrace_notrace", 0644, parent,
4823 ops, &ftrace_notrace_fops);
4827 * The name "destroy_filter_files" is really a misnomer. Although
4828 * in the future, it may actualy delete the files, but this is
4829 * really intended to make sure the ops passed in are disabled
4830 * and that when this function returns, the caller is free to
4831 * free the ops.
4833 * The "destroy" name is only to match the "create" name that this
4834 * should be paired with.
4836 void ftrace_destroy_filter_files(struct ftrace_ops *ops)
4838 mutex_lock(&ftrace_lock);
4839 if (ops->flags & FTRACE_OPS_FL_ENABLED)
4840 ftrace_shutdown(ops, 0);
4841 ops->flags |= FTRACE_OPS_FL_DELETED;
4842 ftrace_free_filter(ops);
4843 mutex_unlock(&ftrace_lock);
4846 static __init int ftrace_init_dyn_tracefs(struct dentry *d_tracer)
4849 trace_create_file("available_filter_functions", 0444,
4850 d_tracer, NULL, &ftrace_avail_fops);
4852 trace_create_file("enabled_functions", 0444,
4853 d_tracer, NULL, &ftrace_enabled_fops);
4855 ftrace_create_filter_files(&global_ops, d_tracer);
4857 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
4858 trace_create_file("set_graph_function", 0444, d_tracer,
4859 NULL,
4860 &ftrace_graph_fops);
4861 trace_create_file("set_graph_notrace", 0444, d_tracer,
4862 NULL,
4863 &ftrace_graph_notrace_fops);
4864 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
4866 return 0;
4869 static int ftrace_cmp_ips(const void *a, const void *b)
4871 const unsigned long *ipa = a;
4872 const unsigned long *ipb = b;
4874 if (*ipa > *ipb)
4875 return 1;
4876 if (*ipa < *ipb)
4877 return -1;
4878 return 0;
4881 static int ftrace_process_locs(struct module *mod,
4882 unsigned long *start,
4883 unsigned long *end)
4885 struct ftrace_page *start_pg;
4886 struct ftrace_page *pg;
4887 struct dyn_ftrace *rec;
4888 unsigned long count;
4889 unsigned long *p;
4890 unsigned long addr;
4891 unsigned long flags = 0; /* Shut up gcc */
4892 int ret = -ENOMEM;
4894 count = end - start;
4896 if (!count)
4897 return 0;
4899 sort(start, count, sizeof(*start),
4900 ftrace_cmp_ips, NULL);
4902 start_pg = ftrace_allocate_pages(count);
4903 if (!start_pg)
4904 return -ENOMEM;
4906 mutex_lock(&ftrace_lock);
4909 * Core and each module needs their own pages, as
4910 * modules will free them when they are removed.
4911 * Force a new page to be allocated for modules.
4913 if (!mod) {
4914 WARN_ON(ftrace_pages || ftrace_pages_start);
4915 /* First initialization */
4916 ftrace_pages = ftrace_pages_start = start_pg;
4917 } else {
4918 if (!ftrace_pages)
4919 goto out;
4921 if (WARN_ON(ftrace_pages->next)) {
4922 /* Hmm, we have free pages? */
4923 while (ftrace_pages->next)
4924 ftrace_pages = ftrace_pages->next;
4927 ftrace_pages->next = start_pg;
4930 p = start;
4931 pg = start_pg;
4932 while (p < end) {
4933 addr = ftrace_call_adjust(*p++);
4935 * Some architecture linkers will pad between
4936 * the different mcount_loc sections of different
4937 * object files to satisfy alignments.
4938 * Skip any NULL pointers.
4940 if (!addr)
4941 continue;
4943 if (pg->index == pg->size) {
4944 /* We should have allocated enough */
4945 if (WARN_ON(!pg->next))
4946 break;
4947 pg = pg->next;
4950 rec = &pg->records[pg->index++];
4951 rec->ip = addr;
4954 /* We should have used all pages */
4955 WARN_ON(pg->next);
4957 /* Assign the last page to ftrace_pages */
4958 ftrace_pages = pg;
4961 * We only need to disable interrupts on start up
4962 * because we are modifying code that an interrupt
4963 * may execute, and the modification is not atomic.
4964 * But for modules, nothing runs the code we modify
4965 * until we are finished with it, and there's no
4966 * reason to cause large interrupt latencies while we do it.
4968 if (!mod)
4969 local_irq_save(flags);
4970 ftrace_update_code(mod, start_pg);
4971 if (!mod)
4972 local_irq_restore(flags);
4973 ret = 0;
4974 out:
4975 mutex_unlock(&ftrace_lock);
4977 return ret;
4980 #ifdef CONFIG_MODULES
4982 #define next_to_ftrace_page(p) container_of(p, struct ftrace_page, next)
4984 static int referenced_filters(struct dyn_ftrace *rec)
4986 struct ftrace_ops *ops;
4987 int cnt = 0;
4989 for (ops = ftrace_ops_list; ops != &ftrace_list_end; ops = ops->next) {
4990 if (ops_references_rec(ops, rec)) {
4991 cnt++;
4992 if (ops->flags & FTRACE_OPS_FL_SAVE_REGS)
4993 rec->flags |= FTRACE_FL_REGS;
4997 return cnt;
5000 void ftrace_release_mod(struct module *mod)
5002 struct dyn_ftrace *rec;
5003 struct ftrace_page **last_pg;
5004 struct ftrace_page *pg;
5005 int order;
5007 mutex_lock(&ftrace_lock);
5009 if (ftrace_disabled)
5010 goto out_unlock;
5013 * Each module has its own ftrace_pages, remove
5014 * them from the list.
5016 last_pg = &ftrace_pages_start;
5017 for (pg = ftrace_pages_start; pg; pg = *last_pg) {
5018 rec = &pg->records[0];
5019 if (within_module_core(rec->ip, mod)) {
5021 * As core pages are first, the first
5022 * page should never be a module page.
5024 if (WARN_ON(pg == ftrace_pages_start))
5025 goto out_unlock;
5027 /* Check if we are deleting the last page */
5028 if (pg == ftrace_pages)
5029 ftrace_pages = next_to_ftrace_page(last_pg);
5031 *last_pg = pg->next;
5032 order = get_count_order(pg->size / ENTRIES_PER_PAGE);
5033 free_pages((unsigned long)pg->records, order);
5034 kfree(pg);
5035 } else
5036 last_pg = &pg->next;
5038 out_unlock:
5039 mutex_unlock(&ftrace_lock);
5042 void ftrace_module_enable(struct module *mod)
5044 struct dyn_ftrace *rec;
5045 struct ftrace_page *pg;
5047 mutex_lock(&ftrace_lock);
5049 if (ftrace_disabled)
5050 goto out_unlock;
5053 * If the tracing is enabled, go ahead and enable the record.
5055 * The reason not to enable the record immediatelly is the
5056 * inherent check of ftrace_make_nop/ftrace_make_call for
5057 * correct previous instructions. Making first the NOP
5058 * conversion puts the module to the correct state, thus
5059 * passing the ftrace_make_call check.
5061 * We also delay this to after the module code already set the
5062 * text to read-only, as we now need to set it back to read-write
5063 * so that we can modify the text.
5065 if (ftrace_start_up)
5066 ftrace_arch_code_modify_prepare();
5068 do_for_each_ftrace_rec(pg, rec) {
5069 int cnt;
5071 * do_for_each_ftrace_rec() is a double loop.
5072 * module text shares the pg. If a record is
5073 * not part of this module, then skip this pg,
5074 * which the "break" will do.
5076 if (!within_module_core(rec->ip, mod))
5077 break;
5079 cnt = 0;
5082 * When adding a module, we need to check if tracers are
5083 * currently enabled and if they are, and can trace this record,
5084 * we need to enable the module functions as well as update the
5085 * reference counts for those function records.
5087 if (ftrace_start_up)
5088 cnt += referenced_filters(rec);
5090 rec->flags &= ~FTRACE_FL_DISABLED;
5091 rec->flags += cnt;
5093 if (ftrace_start_up && cnt) {
5094 int failed = __ftrace_replace_code(rec, 1);
5095 if (failed) {
5096 ftrace_bug(failed, rec);
5097 goto out_loop;
5101 } while_for_each_ftrace_rec();
5103 out_loop:
5104 if (ftrace_start_up)
5105 ftrace_arch_code_modify_post_process();
5107 out_unlock:
5108 mutex_unlock(&ftrace_lock);
5111 void ftrace_module_init(struct module *mod)
5113 if (ftrace_disabled || !mod->num_ftrace_callsites)
5114 return;
5116 ftrace_process_locs(mod, mod->ftrace_callsites,
5117 mod->ftrace_callsites + mod->num_ftrace_callsites);
5119 #endif /* CONFIG_MODULES */
5121 void __init ftrace_init(void)
5123 extern unsigned long __start_mcount_loc[];
5124 extern unsigned long __stop_mcount_loc[];
5125 unsigned long count, flags;
5126 int ret;
5128 local_irq_save(flags);
5129 ret = ftrace_dyn_arch_init();
5130 local_irq_restore(flags);
5131 if (ret)
5132 goto failed;
5134 count = __stop_mcount_loc - __start_mcount_loc;
5135 if (!count) {
5136 pr_info("ftrace: No functions to be traced?\n");
5137 goto failed;
5140 pr_info("ftrace: allocating %ld entries in %ld pages\n",
5141 count, count / ENTRIES_PER_PAGE + 1);
5143 last_ftrace_enabled = ftrace_enabled = 1;
5145 ret = ftrace_process_locs(NULL,
5146 __start_mcount_loc,
5147 __stop_mcount_loc);
5149 set_ftrace_early_filters();
5151 return;
5152 failed:
5153 ftrace_disabled = 1;
5156 /* Do nothing if arch does not support this */
5157 void __weak arch_ftrace_update_trampoline(struct ftrace_ops *ops)
5161 static void ftrace_update_trampoline(struct ftrace_ops *ops)
5165 * Currently there's no safe way to free a trampoline when the kernel
5166 * is configured with PREEMPT. That is because a task could be preempted
5167 * when it jumped to the trampoline, it may be preempted for a long time
5168 * depending on the system load, and currently there's no way to know
5169 * when it will be off the trampoline. If the trampoline is freed
5170 * too early, when the task runs again, it will be executing on freed
5171 * memory and crash.
5173 #ifdef CONFIG_PREEMPT
5174 /* Currently, only non dynamic ops can have a trampoline */
5175 if (ops->flags & FTRACE_OPS_FL_DYNAMIC)
5176 return;
5177 #endif
5179 arch_ftrace_update_trampoline(ops);
5182 #else
5184 static struct ftrace_ops global_ops = {
5185 .func = ftrace_stub,
5186 .flags = FTRACE_OPS_FL_RECURSION_SAFE |
5187 FTRACE_OPS_FL_INITIALIZED |
5188 FTRACE_OPS_FL_PID,
5191 static int __init ftrace_nodyn_init(void)
5193 ftrace_enabled = 1;
5194 return 0;
5196 core_initcall(ftrace_nodyn_init);
5198 static inline int ftrace_init_dyn_tracefs(struct dentry *d_tracer) { return 0; }
5199 static inline void ftrace_startup_enable(int command) { }
5200 static inline void ftrace_startup_all(int command) { }
5201 /* Keep as macros so we do not need to define the commands */
5202 # define ftrace_startup(ops, command) \
5203 ({ \
5204 int ___ret = __register_ftrace_function(ops); \
5205 if (!___ret) \
5206 (ops)->flags |= FTRACE_OPS_FL_ENABLED; \
5207 ___ret; \
5209 # define ftrace_shutdown(ops, command) \
5210 ({ \
5211 int ___ret = __unregister_ftrace_function(ops); \
5212 if (!___ret) \
5213 (ops)->flags &= ~FTRACE_OPS_FL_ENABLED; \
5214 ___ret; \
5217 # define ftrace_startup_sysctl() do { } while (0)
5218 # define ftrace_shutdown_sysctl() do { } while (0)
5220 static inline int
5221 ftrace_ops_test(struct ftrace_ops *ops, unsigned long ip, void *regs)
5223 return 1;
5226 static void ftrace_update_trampoline(struct ftrace_ops *ops)
5230 #endif /* CONFIG_DYNAMIC_FTRACE */
5232 __init void ftrace_init_global_array_ops(struct trace_array *tr)
5234 tr->ops = &global_ops;
5235 tr->ops->private = tr;
5238 void ftrace_init_array_ops(struct trace_array *tr, ftrace_func_t func)
5240 /* If we filter on pids, update to use the pid function */
5241 if (tr->flags & TRACE_ARRAY_FL_GLOBAL) {
5242 if (WARN_ON(tr->ops->func != ftrace_stub))
5243 printk("ftrace ops had %pS for function\n",
5244 tr->ops->func);
5246 tr->ops->func = func;
5247 tr->ops->private = tr;
5250 void ftrace_reset_array_ops(struct trace_array *tr)
5252 tr->ops->func = ftrace_stub;
5255 static nokprobe_inline void
5256 __ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip,
5257 struct ftrace_ops *ignored, struct pt_regs *regs)
5259 struct ftrace_ops *op;
5260 int bit;
5262 bit = trace_test_and_set_recursion(TRACE_LIST_START, TRACE_LIST_MAX);
5263 if (bit < 0)
5264 return;
5267 * Some of the ops may be dynamically allocated,
5268 * they must be freed after a synchronize_sched().
5270 preempt_disable_notrace();
5272 do_for_each_ftrace_op(op, ftrace_ops_list) {
5274 * Check the following for each ops before calling their func:
5275 * if RCU flag is set, then rcu_is_watching() must be true
5276 * if PER_CPU is set, then ftrace_function_local_disable()
5277 * must be false
5278 * Otherwise test if the ip matches the ops filter
5280 * If any of the above fails then the op->func() is not executed.
5282 if ((!(op->flags & FTRACE_OPS_FL_RCU) || rcu_is_watching()) &&
5283 (!(op->flags & FTRACE_OPS_FL_PER_CPU) ||
5284 !ftrace_function_local_disabled(op)) &&
5285 ftrace_ops_test(op, ip, regs)) {
5287 if (FTRACE_WARN_ON(!op->func)) {
5288 pr_warn("op=%p %pS\n", op, op);
5289 goto out;
5291 op->func(ip, parent_ip, op, regs);
5293 } while_for_each_ftrace_op(op);
5294 out:
5295 preempt_enable_notrace();
5296 trace_clear_recursion(bit);
5300 * Some archs only support passing ip and parent_ip. Even though
5301 * the list function ignores the op parameter, we do not want any
5302 * C side effects, where a function is called without the caller
5303 * sending a third parameter.
5304 * Archs are to support both the regs and ftrace_ops at the same time.
5305 * If they support ftrace_ops, it is assumed they support regs.
5306 * If call backs want to use regs, they must either check for regs
5307 * being NULL, or CONFIG_DYNAMIC_FTRACE_WITH_REGS.
5308 * Note, CONFIG_DYNAMIC_FTRACE_WITH_REGS expects a full regs to be saved.
5309 * An architecture can pass partial regs with ftrace_ops and still
5310 * set the ARCH_SUPPORTS_FTRACE_OPS.
5312 #if ARCH_SUPPORTS_FTRACE_OPS
5313 static void ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip,
5314 struct ftrace_ops *op, struct pt_regs *regs)
5316 __ftrace_ops_list_func(ip, parent_ip, NULL, regs);
5318 NOKPROBE_SYMBOL(ftrace_ops_list_func);
5319 #else
5320 static void ftrace_ops_no_ops(unsigned long ip, unsigned long parent_ip)
5322 __ftrace_ops_list_func(ip, parent_ip, NULL, NULL);
5324 NOKPROBE_SYMBOL(ftrace_ops_no_ops);
5325 #endif
5328 * If there's only one function registered but it does not support
5329 * recursion, needs RCU protection and/or requires per cpu handling, then
5330 * this function will be called by the mcount trampoline.
5332 static void ftrace_ops_assist_func(unsigned long ip, unsigned long parent_ip,
5333 struct ftrace_ops *op, struct pt_regs *regs)
5335 int bit;
5337 bit = trace_test_and_set_recursion(TRACE_LIST_START, TRACE_LIST_MAX);
5338 if (bit < 0)
5339 return;
5341 preempt_disable_notrace();
5343 if ((!(op->flags & FTRACE_OPS_FL_RCU) || rcu_is_watching()) &&
5344 (!(op->flags & FTRACE_OPS_FL_PER_CPU) ||
5345 !ftrace_function_local_disabled(op))) {
5346 op->func(ip, parent_ip, op, regs);
5349 preempt_enable_notrace();
5350 trace_clear_recursion(bit);
5352 NOKPROBE_SYMBOL(ftrace_ops_assist_func);
5355 * ftrace_ops_get_func - get the function a trampoline should call
5356 * @ops: the ops to get the function for
5358 * Normally the mcount trampoline will call the ops->func, but there
5359 * are times that it should not. For example, if the ops does not
5360 * have its own recursion protection, then it should call the
5361 * ftrace_ops_recurs_func() instead.
5363 * Returns the function that the trampoline should call for @ops.
5365 ftrace_func_t ftrace_ops_get_func(struct ftrace_ops *ops)
5368 * If the function does not handle recursion, needs to be RCU safe,
5369 * or does per cpu logic, then we need to call the assist handler.
5371 if (!(ops->flags & FTRACE_OPS_FL_RECURSION_SAFE) ||
5372 ops->flags & (FTRACE_OPS_FL_RCU | FTRACE_OPS_FL_PER_CPU))
5373 return ftrace_ops_assist_func;
5375 return ops->func;
5378 static void
5379 ftrace_filter_pid_sched_switch_probe(void *data, bool preempt,
5380 struct task_struct *prev, struct task_struct *next)
5382 struct trace_array *tr = data;
5383 struct trace_pid_list *pid_list;
5385 pid_list = rcu_dereference_sched(tr->function_pids);
5387 this_cpu_write(tr->trace_buffer.data->ftrace_ignore_pid,
5388 trace_ignore_this_task(pid_list, next));
5391 static void clear_ftrace_pids(struct trace_array *tr)
5393 struct trace_pid_list *pid_list;
5394 int cpu;
5396 pid_list = rcu_dereference_protected(tr->function_pids,
5397 lockdep_is_held(&ftrace_lock));
5398 if (!pid_list)
5399 return;
5401 unregister_trace_sched_switch(ftrace_filter_pid_sched_switch_probe, tr);
5403 for_each_possible_cpu(cpu)
5404 per_cpu_ptr(tr->trace_buffer.data, cpu)->ftrace_ignore_pid = false;
5406 rcu_assign_pointer(tr->function_pids, NULL);
5408 /* Wait till all users are no longer using pid filtering */
5409 synchronize_sched();
5411 trace_free_pid_list(pid_list);
5414 void ftrace_clear_pids(struct trace_array *tr)
5416 mutex_lock(&ftrace_lock);
5418 clear_ftrace_pids(tr);
5420 mutex_unlock(&ftrace_lock);
5423 static void ftrace_pid_reset(struct trace_array *tr)
5425 mutex_lock(&ftrace_lock);
5426 clear_ftrace_pids(tr);
5428 ftrace_update_pid_func();
5429 ftrace_startup_all(0);
5431 mutex_unlock(&ftrace_lock);
5434 /* Greater than any max PID */
5435 #define FTRACE_NO_PIDS (void *)(PID_MAX_LIMIT + 1)
5437 static void *fpid_start(struct seq_file *m, loff_t *pos)
5438 __acquires(RCU)
5440 struct trace_pid_list *pid_list;
5441 struct trace_array *tr = m->private;
5443 mutex_lock(&ftrace_lock);
5444 rcu_read_lock_sched();
5446 pid_list = rcu_dereference_sched(tr->function_pids);
5448 if (!pid_list)
5449 return !(*pos) ? FTRACE_NO_PIDS : NULL;
5451 return trace_pid_start(pid_list, pos);
5454 static void *fpid_next(struct seq_file *m, void *v, loff_t *pos)
5456 struct trace_array *tr = m->private;
5457 struct trace_pid_list *pid_list = rcu_dereference_sched(tr->function_pids);
5459 if (v == FTRACE_NO_PIDS) {
5460 (*pos)++;
5461 return NULL;
5463 return trace_pid_next(pid_list, v, pos);
5466 static void fpid_stop(struct seq_file *m, void *p)
5467 __releases(RCU)
5469 rcu_read_unlock_sched();
5470 mutex_unlock(&ftrace_lock);
5473 static int fpid_show(struct seq_file *m, void *v)
5475 if (v == FTRACE_NO_PIDS) {
5476 seq_puts(m, "no pid\n");
5477 return 0;
5480 return trace_pid_show(m, v);
5483 static const struct seq_operations ftrace_pid_sops = {
5484 .start = fpid_start,
5485 .next = fpid_next,
5486 .stop = fpid_stop,
5487 .show = fpid_show,
5490 static int
5491 ftrace_pid_open(struct inode *inode, struct file *file)
5493 struct trace_array *tr = inode->i_private;
5494 struct seq_file *m;
5495 int ret = 0;
5497 if (trace_array_get(tr) < 0)
5498 return -ENODEV;
5500 if ((file->f_mode & FMODE_WRITE) &&
5501 (file->f_flags & O_TRUNC))
5502 ftrace_pid_reset(tr);
5504 ret = seq_open(file, &ftrace_pid_sops);
5505 if (ret < 0) {
5506 trace_array_put(tr);
5507 } else {
5508 m = file->private_data;
5509 /* copy tr over to seq ops */
5510 m->private = tr;
5513 return ret;
5516 static void ignore_task_cpu(void *data)
5518 struct trace_array *tr = data;
5519 struct trace_pid_list *pid_list;
5522 * This function is called by on_each_cpu() while the
5523 * event_mutex is held.
5525 pid_list = rcu_dereference_protected(tr->function_pids,
5526 mutex_is_locked(&ftrace_lock));
5528 this_cpu_write(tr->trace_buffer.data->ftrace_ignore_pid,
5529 trace_ignore_this_task(pid_list, current));
5532 static ssize_t
5533 ftrace_pid_write(struct file *filp, const char __user *ubuf,
5534 size_t cnt, loff_t *ppos)
5536 struct seq_file *m = filp->private_data;
5537 struct trace_array *tr = m->private;
5538 struct trace_pid_list *filtered_pids = NULL;
5539 struct trace_pid_list *pid_list;
5540 ssize_t ret;
5542 if (!cnt)
5543 return 0;
5545 mutex_lock(&ftrace_lock);
5547 filtered_pids = rcu_dereference_protected(tr->function_pids,
5548 lockdep_is_held(&ftrace_lock));
5550 ret = trace_pid_write(filtered_pids, &pid_list, ubuf, cnt);
5551 if (ret < 0)
5552 goto out;
5554 rcu_assign_pointer(tr->function_pids, pid_list);
5556 if (filtered_pids) {
5557 synchronize_sched();
5558 trace_free_pid_list(filtered_pids);
5559 } else if (pid_list) {
5560 /* Register a probe to set whether to ignore the tracing of a task */
5561 register_trace_sched_switch(ftrace_filter_pid_sched_switch_probe, tr);
5565 * Ignoring of pids is done at task switch. But we have to
5566 * check for those tasks that are currently running.
5567 * Always do this in case a pid was appended or removed.
5569 on_each_cpu(ignore_task_cpu, tr, 1);
5571 ftrace_update_pid_func();
5572 ftrace_startup_all(0);
5573 out:
5574 mutex_unlock(&ftrace_lock);
5576 if (ret > 0)
5577 *ppos += ret;
5579 return ret;
5582 static int
5583 ftrace_pid_release(struct inode *inode, struct file *file)
5585 struct trace_array *tr = inode->i_private;
5587 trace_array_put(tr);
5589 return seq_release(inode, file);
5592 static const struct file_operations ftrace_pid_fops = {
5593 .open = ftrace_pid_open,
5594 .write = ftrace_pid_write,
5595 .read = seq_read,
5596 .llseek = tracing_lseek,
5597 .release = ftrace_pid_release,
5600 void ftrace_init_tracefs(struct trace_array *tr, struct dentry *d_tracer)
5602 trace_create_file("set_ftrace_pid", 0644, d_tracer,
5603 tr, &ftrace_pid_fops);
5606 void __init ftrace_init_tracefs_toplevel(struct trace_array *tr,
5607 struct dentry *d_tracer)
5609 /* Only the top level directory has the dyn_tracefs and profile */
5610 WARN_ON(!(tr->flags & TRACE_ARRAY_FL_GLOBAL));
5612 ftrace_init_dyn_tracefs(d_tracer);
5613 ftrace_profile_tracefs(d_tracer);
5617 * ftrace_kill - kill ftrace
5619 * This function should be used by panic code. It stops ftrace
5620 * but in a not so nice way. If you need to simply kill ftrace
5621 * from a non-atomic section, use ftrace_kill.
5623 void ftrace_kill(void)
5625 ftrace_disabled = 1;
5626 ftrace_enabled = 0;
5627 clear_ftrace_function();
5631 * Test if ftrace is dead or not.
5633 int ftrace_is_dead(void)
5635 return ftrace_disabled;
5639 * register_ftrace_function - register a function for profiling
5640 * @ops - ops structure that holds the function for profiling.
5642 * Register a function to be called by all functions in the
5643 * kernel.
5645 * Note: @ops->func and all the functions it calls must be labeled
5646 * with "notrace", otherwise it will go into a
5647 * recursive loop.
5649 int register_ftrace_function(struct ftrace_ops *ops)
5651 int ret = -1;
5653 ftrace_ops_init(ops);
5655 mutex_lock(&ftrace_lock);
5657 ret = ftrace_startup(ops, 0);
5659 mutex_unlock(&ftrace_lock);
5661 return ret;
5663 EXPORT_SYMBOL_GPL(register_ftrace_function);
5666 * unregister_ftrace_function - unregister a function for profiling.
5667 * @ops - ops structure that holds the function to unregister
5669 * Unregister a function that was added to be called by ftrace profiling.
5671 int unregister_ftrace_function(struct ftrace_ops *ops)
5673 int ret;
5675 mutex_lock(&ftrace_lock);
5676 ret = ftrace_shutdown(ops, 0);
5677 mutex_unlock(&ftrace_lock);
5679 return ret;
5681 EXPORT_SYMBOL_GPL(unregister_ftrace_function);
5684 ftrace_enable_sysctl(struct ctl_table *table, int write,
5685 void __user *buffer, size_t *lenp,
5686 loff_t *ppos)
5688 int ret = -ENODEV;
5690 mutex_lock(&ftrace_lock);
5692 if (unlikely(ftrace_disabled))
5693 goto out;
5695 ret = proc_dointvec(table, write, buffer, lenp, ppos);
5697 if (ret || !write || (last_ftrace_enabled == !!ftrace_enabled))
5698 goto out;
5700 last_ftrace_enabled = !!ftrace_enabled;
5702 if (ftrace_enabled) {
5704 /* we are starting ftrace again */
5705 if (ftrace_ops_list != &ftrace_list_end)
5706 update_ftrace_function();
5708 ftrace_startup_sysctl();
5710 } else {
5711 /* stopping ftrace calls (just send to ftrace_stub) */
5712 ftrace_trace_function = ftrace_stub;
5714 ftrace_shutdown_sysctl();
5717 out:
5718 mutex_unlock(&ftrace_lock);
5719 return ret;
5722 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
5724 static struct ftrace_ops graph_ops = {
5725 .func = ftrace_stub,
5726 .flags = FTRACE_OPS_FL_RECURSION_SAFE |
5727 FTRACE_OPS_FL_INITIALIZED |
5728 FTRACE_OPS_FL_PID |
5729 FTRACE_OPS_FL_STUB,
5730 #ifdef FTRACE_GRAPH_TRAMP_ADDR
5731 .trampoline = FTRACE_GRAPH_TRAMP_ADDR,
5732 /* trampoline_size is only needed for dynamically allocated tramps */
5733 #endif
5734 ASSIGN_OPS_HASH(graph_ops, &global_ops.local_hash)
5737 void ftrace_graph_sleep_time_control(bool enable)
5739 fgraph_sleep_time = enable;
5742 void ftrace_graph_graph_time_control(bool enable)
5744 fgraph_graph_time = enable;
5747 int ftrace_graph_entry_stub(struct ftrace_graph_ent *trace)
5749 return 0;
5752 /* The callbacks that hook a function */
5753 trace_func_graph_ret_t ftrace_graph_return =
5754 (trace_func_graph_ret_t)ftrace_stub;
5755 trace_func_graph_ent_t ftrace_graph_entry = ftrace_graph_entry_stub;
5756 static trace_func_graph_ent_t __ftrace_graph_entry = ftrace_graph_entry_stub;
5758 /* Try to assign a return stack array on FTRACE_RETSTACK_ALLOC_SIZE tasks. */
5759 static int alloc_retstack_tasklist(struct ftrace_ret_stack **ret_stack_list)
5761 int i;
5762 int ret = 0;
5763 int start = 0, end = FTRACE_RETSTACK_ALLOC_SIZE;
5764 struct task_struct *g, *t;
5766 for (i = 0; i < FTRACE_RETSTACK_ALLOC_SIZE; i++) {
5767 ret_stack_list[i] = kmalloc(FTRACE_RETFUNC_DEPTH
5768 * sizeof(struct ftrace_ret_stack),
5769 GFP_KERNEL);
5770 if (!ret_stack_list[i]) {
5771 start = 0;
5772 end = i;
5773 ret = -ENOMEM;
5774 goto free;
5778 read_lock(&tasklist_lock);
5779 do_each_thread(g, t) {
5780 if (start == end) {
5781 ret = -EAGAIN;
5782 goto unlock;
5785 if (t->ret_stack == NULL) {
5786 atomic_set(&t->tracing_graph_pause, 0);
5787 atomic_set(&t->trace_overrun, 0);
5788 t->curr_ret_stack = -1;
5789 /* Make sure the tasks see the -1 first: */
5790 smp_wmb();
5791 t->ret_stack = ret_stack_list[start++];
5793 } while_each_thread(g, t);
5795 unlock:
5796 read_unlock(&tasklist_lock);
5797 free:
5798 for (i = start; i < end; i++)
5799 kfree(ret_stack_list[i]);
5800 return ret;
5803 static void
5804 ftrace_graph_probe_sched_switch(void *ignore, bool preempt,
5805 struct task_struct *prev, struct task_struct *next)
5807 unsigned long long timestamp;
5808 int index;
5811 * Does the user want to count the time a function was asleep.
5812 * If so, do not update the time stamps.
5814 if (fgraph_sleep_time)
5815 return;
5817 timestamp = trace_clock_local();
5819 prev->ftrace_timestamp = timestamp;
5821 /* only process tasks that we timestamped */
5822 if (!next->ftrace_timestamp)
5823 return;
5826 * Update all the counters in next to make up for the
5827 * time next was sleeping.
5829 timestamp -= next->ftrace_timestamp;
5831 for (index = next->curr_ret_stack; index >= 0; index--)
5832 next->ret_stack[index].calltime += timestamp;
5835 /* Allocate a return stack for each task */
5836 static int start_graph_tracing(void)
5838 struct ftrace_ret_stack **ret_stack_list;
5839 int ret, cpu;
5841 ret_stack_list = kmalloc(FTRACE_RETSTACK_ALLOC_SIZE *
5842 sizeof(struct ftrace_ret_stack *),
5843 GFP_KERNEL);
5845 if (!ret_stack_list)
5846 return -ENOMEM;
5848 /* The cpu_boot init_task->ret_stack will never be freed */
5849 for_each_online_cpu(cpu) {
5850 if (!idle_task(cpu)->ret_stack)
5851 ftrace_graph_init_idle_task(idle_task(cpu), cpu);
5854 do {
5855 ret = alloc_retstack_tasklist(ret_stack_list);
5856 } while (ret == -EAGAIN);
5858 if (!ret) {
5859 ret = register_trace_sched_switch(ftrace_graph_probe_sched_switch, NULL);
5860 if (ret)
5861 pr_info("ftrace_graph: Couldn't activate tracepoint"
5862 " probe to kernel_sched_switch\n");
5865 kfree(ret_stack_list);
5866 return ret;
5870 * Hibernation protection.
5871 * The state of the current task is too much unstable during
5872 * suspend/restore to disk. We want to protect against that.
5874 static int
5875 ftrace_suspend_notifier_call(struct notifier_block *bl, unsigned long state,
5876 void *unused)
5878 switch (state) {
5879 case PM_HIBERNATION_PREPARE:
5880 pause_graph_tracing();
5881 break;
5883 case PM_POST_HIBERNATION:
5884 unpause_graph_tracing();
5885 break;
5887 return NOTIFY_DONE;
5890 static int ftrace_graph_entry_test(struct ftrace_graph_ent *trace)
5892 if (!ftrace_ops_test(&global_ops, trace->func, NULL))
5893 return 0;
5894 return __ftrace_graph_entry(trace);
5898 * The function graph tracer should only trace the functions defined
5899 * by set_ftrace_filter and set_ftrace_notrace. If another function
5900 * tracer ops is registered, the graph tracer requires testing the
5901 * function against the global ops, and not just trace any function
5902 * that any ftrace_ops registered.
5904 static void update_function_graph_func(void)
5906 struct ftrace_ops *op;
5907 bool do_test = false;
5910 * The graph and global ops share the same set of functions
5911 * to test. If any other ops is on the list, then
5912 * the graph tracing needs to test if its the function
5913 * it should call.
5915 do_for_each_ftrace_op(op, ftrace_ops_list) {
5916 if (op != &global_ops && op != &graph_ops &&
5917 op != &ftrace_list_end) {
5918 do_test = true;
5919 /* in double loop, break out with goto */
5920 goto out;
5922 } while_for_each_ftrace_op(op);
5923 out:
5924 if (do_test)
5925 ftrace_graph_entry = ftrace_graph_entry_test;
5926 else
5927 ftrace_graph_entry = __ftrace_graph_entry;
5930 static struct notifier_block ftrace_suspend_notifier = {
5931 .notifier_call = ftrace_suspend_notifier_call,
5934 int register_ftrace_graph(trace_func_graph_ret_t retfunc,
5935 trace_func_graph_ent_t entryfunc)
5937 int ret = 0;
5939 mutex_lock(&ftrace_lock);
5941 /* we currently allow only one tracer registered at a time */
5942 if (ftrace_graph_active) {
5943 ret = -EBUSY;
5944 goto out;
5947 register_pm_notifier(&ftrace_suspend_notifier);
5949 ftrace_graph_active++;
5950 ret = start_graph_tracing();
5951 if (ret) {
5952 ftrace_graph_active--;
5953 goto out;
5956 ftrace_graph_return = retfunc;
5959 * Update the indirect function to the entryfunc, and the
5960 * function that gets called to the entry_test first. Then
5961 * call the update fgraph entry function to determine if
5962 * the entryfunc should be called directly or not.
5964 __ftrace_graph_entry = entryfunc;
5965 ftrace_graph_entry = ftrace_graph_entry_test;
5966 update_function_graph_func();
5968 ret = ftrace_startup(&graph_ops, FTRACE_START_FUNC_RET);
5969 out:
5970 mutex_unlock(&ftrace_lock);
5971 return ret;
5974 void unregister_ftrace_graph(void)
5976 mutex_lock(&ftrace_lock);
5978 if (unlikely(!ftrace_graph_active))
5979 goto out;
5981 ftrace_graph_active--;
5982 ftrace_graph_return = (trace_func_graph_ret_t)ftrace_stub;
5983 ftrace_graph_entry = ftrace_graph_entry_stub;
5984 __ftrace_graph_entry = ftrace_graph_entry_stub;
5985 ftrace_shutdown(&graph_ops, FTRACE_STOP_FUNC_RET);
5986 unregister_pm_notifier(&ftrace_suspend_notifier);
5987 unregister_trace_sched_switch(ftrace_graph_probe_sched_switch, NULL);
5989 out:
5990 mutex_unlock(&ftrace_lock);
5993 static DEFINE_PER_CPU(struct ftrace_ret_stack *, idle_ret_stack);
5995 static void
5996 graph_init_task(struct task_struct *t, struct ftrace_ret_stack *ret_stack)
5998 atomic_set(&t->tracing_graph_pause, 0);
5999 atomic_set(&t->trace_overrun, 0);
6000 t->ftrace_timestamp = 0;
6001 /* make curr_ret_stack visible before we add the ret_stack */
6002 smp_wmb();
6003 t->ret_stack = ret_stack;
6007 * Allocate a return stack for the idle task. May be the first
6008 * time through, or it may be done by CPU hotplug online.
6010 void ftrace_graph_init_idle_task(struct task_struct *t, int cpu)
6012 t->curr_ret_stack = -1;
6014 * The idle task has no parent, it either has its own
6015 * stack or no stack at all.
6017 if (t->ret_stack)
6018 WARN_ON(t->ret_stack != per_cpu(idle_ret_stack, cpu));
6020 if (ftrace_graph_active) {
6021 struct ftrace_ret_stack *ret_stack;
6023 ret_stack = per_cpu(idle_ret_stack, cpu);
6024 if (!ret_stack) {
6025 ret_stack = kmalloc(FTRACE_RETFUNC_DEPTH
6026 * sizeof(struct ftrace_ret_stack),
6027 GFP_KERNEL);
6028 if (!ret_stack)
6029 return;
6030 per_cpu(idle_ret_stack, cpu) = ret_stack;
6032 graph_init_task(t, ret_stack);
6036 /* Allocate a return stack for newly created task */
6037 void ftrace_graph_init_task(struct task_struct *t)
6039 /* Make sure we do not use the parent ret_stack */
6040 t->ret_stack = NULL;
6041 t->curr_ret_stack = -1;
6043 if (ftrace_graph_active) {
6044 struct ftrace_ret_stack *ret_stack;
6046 ret_stack = kmalloc(FTRACE_RETFUNC_DEPTH
6047 * sizeof(struct ftrace_ret_stack),
6048 GFP_KERNEL);
6049 if (!ret_stack)
6050 return;
6051 graph_init_task(t, ret_stack);
6055 void ftrace_graph_exit_task(struct task_struct *t)
6057 struct ftrace_ret_stack *ret_stack = t->ret_stack;
6059 t->ret_stack = NULL;
6060 /* NULL must become visible to IRQs before we free it: */
6061 barrier();
6063 kfree(ret_stack);
6065 #endif