Merge tag 'phy-for-4.20-rc' of git://git.kernel.org/pub/scm/linux/kernel/git/kishon...
[linux/fpc-iii.git] / kernel / trace / ftrace.c
blobf536f601bd46e321a5ae77b2fbb0f3d642edb13e
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Infrastructure for profiling code inserted by 'gcc -pg'.
5 * Copyright (C) 2007-2008 Steven Rostedt <srostedt@redhat.com>
6 * Copyright (C) 2004-2008 Ingo Molnar <mingo@redhat.com>
8 * Originally ported from the -rt patch by:
9 * Copyright (C) 2007 Arnaldo Carvalho de Melo <acme@redhat.com>
11 * Based on code in the latency_tracer, that is:
13 * Copyright (C) 2004-2006 Ingo Molnar
14 * Copyright (C) 2004 Nadia Yvette Chambers
17 #include <linux/stop_machine.h>
18 #include <linux/clocksource.h>
19 #include <linux/sched/task.h>
20 #include <linux/kallsyms.h>
21 #include <linux/seq_file.h>
22 #include <linux/suspend.h>
23 #include <linux/tracefs.h>
24 #include <linux/hardirq.h>
25 #include <linux/kthread.h>
26 #include <linux/uaccess.h>
27 #include <linux/bsearch.h>
28 #include <linux/module.h>
29 #include <linux/ftrace.h>
30 #include <linux/sysctl.h>
31 #include <linux/slab.h>
32 #include <linux/ctype.h>
33 #include <linux/sort.h>
34 #include <linux/list.h>
35 #include <linux/hash.h>
36 #include <linux/rcupdate.h>
38 #include <trace/events/sched.h>
40 #include <asm/sections.h>
41 #include <asm/setup.h>
43 #include "trace_output.h"
44 #include "trace_stat.h"
46 #define FTRACE_WARN_ON(cond) \
47 ({ \
48 int ___r = cond; \
49 if (WARN_ON(___r)) \
50 ftrace_kill(); \
51 ___r; \
54 #define FTRACE_WARN_ON_ONCE(cond) \
55 ({ \
56 int ___r = cond; \
57 if (WARN_ON_ONCE(___r)) \
58 ftrace_kill(); \
59 ___r; \
62 /* hash bits for specific function selection */
63 #define FTRACE_HASH_BITS 7
64 #define FTRACE_FUNC_HASHSIZE (1 << FTRACE_HASH_BITS)
65 #define FTRACE_HASH_DEFAULT_BITS 10
66 #define FTRACE_HASH_MAX_BITS 12
68 #ifdef CONFIG_DYNAMIC_FTRACE
69 #define INIT_OPS_HASH(opsname) \
70 .func_hash = &opsname.local_hash, \
71 .local_hash.regex_lock = __MUTEX_INITIALIZER(opsname.local_hash.regex_lock),
72 #define ASSIGN_OPS_HASH(opsname, val) \
73 .func_hash = val, \
74 .local_hash.regex_lock = __MUTEX_INITIALIZER(opsname.local_hash.regex_lock),
75 #else
76 #define INIT_OPS_HASH(opsname)
77 #define ASSIGN_OPS_HASH(opsname, val)
78 #endif
80 static struct ftrace_ops ftrace_list_end __read_mostly = {
81 .func = ftrace_stub,
82 .flags = FTRACE_OPS_FL_RECURSION_SAFE | FTRACE_OPS_FL_STUB,
83 INIT_OPS_HASH(ftrace_list_end)
86 /* ftrace_enabled is a method to turn ftrace on or off */
87 int ftrace_enabled __read_mostly;
88 static int last_ftrace_enabled;
90 /* Current function tracing op */
91 struct ftrace_ops *function_trace_op __read_mostly = &ftrace_list_end;
92 /* What to set function_trace_op to */
93 static struct ftrace_ops *set_function_trace_op;
95 static bool ftrace_pids_enabled(struct ftrace_ops *ops)
97 struct trace_array *tr;
99 if (!(ops->flags & FTRACE_OPS_FL_PID) || !ops->private)
100 return false;
102 tr = ops->private;
104 return tr->function_pids != NULL;
107 static void ftrace_update_trampoline(struct ftrace_ops *ops);
110 * ftrace_disabled is set when an anomaly is discovered.
111 * ftrace_disabled is much stronger than ftrace_enabled.
113 static int ftrace_disabled __read_mostly;
115 static DEFINE_MUTEX(ftrace_lock);
117 static struct ftrace_ops __rcu *ftrace_ops_list __read_mostly = &ftrace_list_end;
118 ftrace_func_t ftrace_trace_function __read_mostly = ftrace_stub;
119 static struct ftrace_ops global_ops;
121 #if ARCH_SUPPORTS_FTRACE_OPS
122 static void ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip,
123 struct ftrace_ops *op, struct pt_regs *regs);
124 #else
125 /* See comment below, where ftrace_ops_list_func is defined */
126 static void ftrace_ops_no_ops(unsigned long ip, unsigned long parent_ip);
127 #define ftrace_ops_list_func ((ftrace_func_t)ftrace_ops_no_ops)
128 #endif
131 * Traverse the ftrace_global_list, invoking all entries. The reason that we
132 * can use rcu_dereference_raw_notrace() is that elements removed from this list
133 * are simply leaked, so there is no need to interact with a grace-period
134 * mechanism. The rcu_dereference_raw_notrace() calls are needed to handle
135 * concurrent insertions into the ftrace_global_list.
137 * Silly Alpha and silly pointer-speculation compiler optimizations!
139 #define do_for_each_ftrace_op(op, list) \
140 op = rcu_dereference_raw_notrace(list); \
144 * Optimized for just a single item in the list (as that is the normal case).
146 #define while_for_each_ftrace_op(op) \
147 while (likely(op = rcu_dereference_raw_notrace((op)->next)) && \
148 unlikely((op) != &ftrace_list_end))
150 static inline void ftrace_ops_init(struct ftrace_ops *ops)
152 #ifdef CONFIG_DYNAMIC_FTRACE
153 if (!(ops->flags & FTRACE_OPS_FL_INITIALIZED)) {
154 mutex_init(&ops->local_hash.regex_lock);
155 ops->func_hash = &ops->local_hash;
156 ops->flags |= FTRACE_OPS_FL_INITIALIZED;
158 #endif
161 static void ftrace_pid_func(unsigned long ip, unsigned long parent_ip,
162 struct ftrace_ops *op, struct pt_regs *regs)
164 struct trace_array *tr = op->private;
166 if (tr && this_cpu_read(tr->trace_buffer.data->ftrace_ignore_pid))
167 return;
169 op->saved_func(ip, parent_ip, op, regs);
172 static void ftrace_sync(struct work_struct *work)
175 * This function is just a stub to implement a hard force
176 * of synchronize_sched(). This requires synchronizing
177 * tasks even in userspace and idle.
179 * Yes, function tracing is rude.
183 static void ftrace_sync_ipi(void *data)
185 /* Probably not needed, but do it anyway */
186 smp_rmb();
189 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
190 static void update_function_graph_func(void);
192 /* Both enabled by default (can be cleared by function_graph tracer flags */
193 static bool fgraph_sleep_time = true;
194 static bool fgraph_graph_time = true;
196 #else
197 static inline void update_function_graph_func(void) { }
198 #endif
201 static ftrace_func_t ftrace_ops_get_list_func(struct ftrace_ops *ops)
204 * If this is a dynamic, RCU, or per CPU ops, or we force list func,
205 * then it needs to call the list anyway.
207 if (ops->flags & (FTRACE_OPS_FL_DYNAMIC | FTRACE_OPS_FL_RCU) ||
208 FTRACE_FORCE_LIST_FUNC)
209 return ftrace_ops_list_func;
211 return ftrace_ops_get_func(ops);
214 static void update_ftrace_function(void)
216 ftrace_func_t func;
219 * Prepare the ftrace_ops that the arch callback will use.
220 * If there's only one ftrace_ops registered, the ftrace_ops_list
221 * will point to the ops we want.
223 set_function_trace_op = rcu_dereference_protected(ftrace_ops_list,
224 lockdep_is_held(&ftrace_lock));
226 /* If there's no ftrace_ops registered, just call the stub function */
227 if (set_function_trace_op == &ftrace_list_end) {
228 func = ftrace_stub;
231 * If we are at the end of the list and this ops is
232 * recursion safe and not dynamic and the arch supports passing ops,
233 * then have the mcount trampoline call the function directly.
235 } else if (rcu_dereference_protected(ftrace_ops_list->next,
236 lockdep_is_held(&ftrace_lock)) == &ftrace_list_end) {
237 func = ftrace_ops_get_list_func(ftrace_ops_list);
239 } else {
240 /* Just use the default ftrace_ops */
241 set_function_trace_op = &ftrace_list_end;
242 func = ftrace_ops_list_func;
245 update_function_graph_func();
247 /* If there's no change, then do nothing more here */
248 if (ftrace_trace_function == func)
249 return;
252 * If we are using the list function, it doesn't care
253 * about the function_trace_ops.
255 if (func == ftrace_ops_list_func) {
256 ftrace_trace_function = func;
258 * Don't even bother setting function_trace_ops,
259 * it would be racy to do so anyway.
261 return;
264 #ifndef CONFIG_DYNAMIC_FTRACE
266 * For static tracing, we need to be a bit more careful.
267 * The function change takes affect immediately. Thus,
268 * we need to coorditate the setting of the function_trace_ops
269 * with the setting of the ftrace_trace_function.
271 * Set the function to the list ops, which will call the
272 * function we want, albeit indirectly, but it handles the
273 * ftrace_ops and doesn't depend on function_trace_op.
275 ftrace_trace_function = ftrace_ops_list_func;
277 * Make sure all CPUs see this. Yes this is slow, but static
278 * tracing is slow and nasty to have enabled.
280 schedule_on_each_cpu(ftrace_sync);
281 /* Now all cpus are using the list ops. */
282 function_trace_op = set_function_trace_op;
283 /* Make sure the function_trace_op is visible on all CPUs */
284 smp_wmb();
285 /* Nasty way to force a rmb on all cpus */
286 smp_call_function(ftrace_sync_ipi, NULL, 1);
287 /* OK, we are all set to update the ftrace_trace_function now! */
288 #endif /* !CONFIG_DYNAMIC_FTRACE */
290 ftrace_trace_function = func;
293 static void add_ftrace_ops(struct ftrace_ops __rcu **list,
294 struct ftrace_ops *ops)
296 rcu_assign_pointer(ops->next, *list);
299 * We are entering ops into the list but another
300 * CPU might be walking that list. We need to make sure
301 * the ops->next pointer is valid before another CPU sees
302 * the ops pointer included into the list.
304 rcu_assign_pointer(*list, ops);
307 static int remove_ftrace_ops(struct ftrace_ops __rcu **list,
308 struct ftrace_ops *ops)
310 struct ftrace_ops **p;
313 * If we are removing the last function, then simply point
314 * to the ftrace_stub.
316 if (rcu_dereference_protected(*list,
317 lockdep_is_held(&ftrace_lock)) == ops &&
318 rcu_dereference_protected(ops->next,
319 lockdep_is_held(&ftrace_lock)) == &ftrace_list_end) {
320 *list = &ftrace_list_end;
321 return 0;
324 for (p = list; *p != &ftrace_list_end; p = &(*p)->next)
325 if (*p == ops)
326 break;
328 if (*p != ops)
329 return -1;
331 *p = (*p)->next;
332 return 0;
335 static void ftrace_update_trampoline(struct ftrace_ops *ops);
337 static int __register_ftrace_function(struct ftrace_ops *ops)
339 if (ops->flags & FTRACE_OPS_FL_DELETED)
340 return -EINVAL;
342 if (WARN_ON(ops->flags & FTRACE_OPS_FL_ENABLED))
343 return -EBUSY;
345 #ifndef CONFIG_DYNAMIC_FTRACE_WITH_REGS
347 * If the ftrace_ops specifies SAVE_REGS, then it only can be used
348 * if the arch supports it, or SAVE_REGS_IF_SUPPORTED is also set.
349 * Setting SAVE_REGS_IF_SUPPORTED makes SAVE_REGS irrelevant.
351 if (ops->flags & FTRACE_OPS_FL_SAVE_REGS &&
352 !(ops->flags & FTRACE_OPS_FL_SAVE_REGS_IF_SUPPORTED))
353 return -EINVAL;
355 if (ops->flags & FTRACE_OPS_FL_SAVE_REGS_IF_SUPPORTED)
356 ops->flags |= FTRACE_OPS_FL_SAVE_REGS;
357 #endif
359 if (!core_kernel_data((unsigned long)ops))
360 ops->flags |= FTRACE_OPS_FL_DYNAMIC;
362 add_ftrace_ops(&ftrace_ops_list, ops);
364 /* Always save the function, and reset at unregistering */
365 ops->saved_func = ops->func;
367 if (ftrace_pids_enabled(ops))
368 ops->func = ftrace_pid_func;
370 ftrace_update_trampoline(ops);
372 if (ftrace_enabled)
373 update_ftrace_function();
375 return 0;
378 static int __unregister_ftrace_function(struct ftrace_ops *ops)
380 int ret;
382 if (WARN_ON(!(ops->flags & FTRACE_OPS_FL_ENABLED)))
383 return -EBUSY;
385 ret = remove_ftrace_ops(&ftrace_ops_list, ops);
387 if (ret < 0)
388 return ret;
390 if (ftrace_enabled)
391 update_ftrace_function();
393 ops->func = ops->saved_func;
395 return 0;
398 static void ftrace_update_pid_func(void)
400 struct ftrace_ops *op;
402 /* Only do something if we are tracing something */
403 if (ftrace_trace_function == ftrace_stub)
404 return;
406 do_for_each_ftrace_op(op, ftrace_ops_list) {
407 if (op->flags & FTRACE_OPS_FL_PID) {
408 op->func = ftrace_pids_enabled(op) ?
409 ftrace_pid_func : op->saved_func;
410 ftrace_update_trampoline(op);
412 } while_for_each_ftrace_op(op);
414 update_ftrace_function();
417 #ifdef CONFIG_FUNCTION_PROFILER
418 struct ftrace_profile {
419 struct hlist_node node;
420 unsigned long ip;
421 unsigned long counter;
422 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
423 unsigned long long time;
424 unsigned long long time_squared;
425 #endif
428 struct ftrace_profile_page {
429 struct ftrace_profile_page *next;
430 unsigned long index;
431 struct ftrace_profile records[];
434 struct ftrace_profile_stat {
435 atomic_t disabled;
436 struct hlist_head *hash;
437 struct ftrace_profile_page *pages;
438 struct ftrace_profile_page *start;
439 struct tracer_stat stat;
442 #define PROFILE_RECORDS_SIZE \
443 (PAGE_SIZE - offsetof(struct ftrace_profile_page, records))
445 #define PROFILES_PER_PAGE \
446 (PROFILE_RECORDS_SIZE / sizeof(struct ftrace_profile))
448 static int ftrace_profile_enabled __read_mostly;
450 /* ftrace_profile_lock - synchronize the enable and disable of the profiler */
451 static DEFINE_MUTEX(ftrace_profile_lock);
453 static DEFINE_PER_CPU(struct ftrace_profile_stat, ftrace_profile_stats);
455 #define FTRACE_PROFILE_HASH_BITS 10
456 #define FTRACE_PROFILE_HASH_SIZE (1 << FTRACE_PROFILE_HASH_BITS)
458 static void *
459 function_stat_next(void *v, int idx)
461 struct ftrace_profile *rec = v;
462 struct ftrace_profile_page *pg;
464 pg = (struct ftrace_profile_page *)((unsigned long)rec & PAGE_MASK);
466 again:
467 if (idx != 0)
468 rec++;
470 if ((void *)rec >= (void *)&pg->records[pg->index]) {
471 pg = pg->next;
472 if (!pg)
473 return NULL;
474 rec = &pg->records[0];
475 if (!rec->counter)
476 goto again;
479 return rec;
482 static void *function_stat_start(struct tracer_stat *trace)
484 struct ftrace_profile_stat *stat =
485 container_of(trace, struct ftrace_profile_stat, stat);
487 if (!stat || !stat->start)
488 return NULL;
490 return function_stat_next(&stat->start->records[0], 0);
493 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
494 /* function graph compares on total time */
495 static int function_stat_cmp(void *p1, void *p2)
497 struct ftrace_profile *a = p1;
498 struct ftrace_profile *b = p2;
500 if (a->time < b->time)
501 return -1;
502 if (a->time > b->time)
503 return 1;
504 else
505 return 0;
507 #else
508 /* not function graph compares against hits */
509 static int function_stat_cmp(void *p1, void *p2)
511 struct ftrace_profile *a = p1;
512 struct ftrace_profile *b = p2;
514 if (a->counter < b->counter)
515 return -1;
516 if (a->counter > b->counter)
517 return 1;
518 else
519 return 0;
521 #endif
523 static int function_stat_headers(struct seq_file *m)
525 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
526 seq_puts(m, " Function "
527 "Hit Time Avg s^2\n"
528 " -------- "
529 "--- ---- --- ---\n");
530 #else
531 seq_puts(m, " Function Hit\n"
532 " -------- ---\n");
533 #endif
534 return 0;
537 static int function_stat_show(struct seq_file *m, void *v)
539 struct ftrace_profile *rec = v;
540 char str[KSYM_SYMBOL_LEN];
541 int ret = 0;
542 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
543 static struct trace_seq s;
544 unsigned long long avg;
545 unsigned long long stddev;
546 #endif
547 mutex_lock(&ftrace_profile_lock);
549 /* we raced with function_profile_reset() */
550 if (unlikely(rec->counter == 0)) {
551 ret = -EBUSY;
552 goto out;
555 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
556 avg = rec->time;
557 do_div(avg, rec->counter);
558 if (tracing_thresh && (avg < tracing_thresh))
559 goto out;
560 #endif
562 kallsyms_lookup(rec->ip, NULL, NULL, NULL, str);
563 seq_printf(m, " %-30.30s %10lu", str, rec->counter);
565 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
566 seq_puts(m, " ");
568 /* Sample standard deviation (s^2) */
569 if (rec->counter <= 1)
570 stddev = 0;
571 else {
573 * Apply Welford's method:
574 * s^2 = 1 / (n * (n-1)) * (n * \Sum (x_i)^2 - (\Sum x_i)^2)
576 stddev = rec->counter * rec->time_squared -
577 rec->time * rec->time;
580 * Divide only 1000 for ns^2 -> us^2 conversion.
581 * trace_print_graph_duration will divide 1000 again.
583 do_div(stddev, rec->counter * (rec->counter - 1) * 1000);
586 trace_seq_init(&s);
587 trace_print_graph_duration(rec->time, &s);
588 trace_seq_puts(&s, " ");
589 trace_print_graph_duration(avg, &s);
590 trace_seq_puts(&s, " ");
591 trace_print_graph_duration(stddev, &s);
592 trace_print_seq(m, &s);
593 #endif
594 seq_putc(m, '\n');
595 out:
596 mutex_unlock(&ftrace_profile_lock);
598 return ret;
601 static void ftrace_profile_reset(struct ftrace_profile_stat *stat)
603 struct ftrace_profile_page *pg;
605 pg = stat->pages = stat->start;
607 while (pg) {
608 memset(pg->records, 0, PROFILE_RECORDS_SIZE);
609 pg->index = 0;
610 pg = pg->next;
613 memset(stat->hash, 0,
614 FTRACE_PROFILE_HASH_SIZE * sizeof(struct hlist_head));
617 int ftrace_profile_pages_init(struct ftrace_profile_stat *stat)
619 struct ftrace_profile_page *pg;
620 int functions;
621 int pages;
622 int i;
624 /* If we already allocated, do nothing */
625 if (stat->pages)
626 return 0;
628 stat->pages = (void *)get_zeroed_page(GFP_KERNEL);
629 if (!stat->pages)
630 return -ENOMEM;
632 #ifdef CONFIG_DYNAMIC_FTRACE
633 functions = ftrace_update_tot_cnt;
634 #else
636 * We do not know the number of functions that exist because
637 * dynamic tracing is what counts them. With past experience
638 * we have around 20K functions. That should be more than enough.
639 * It is highly unlikely we will execute every function in
640 * the kernel.
642 functions = 20000;
643 #endif
645 pg = stat->start = stat->pages;
647 pages = DIV_ROUND_UP(functions, PROFILES_PER_PAGE);
649 for (i = 1; i < pages; i++) {
650 pg->next = (void *)get_zeroed_page(GFP_KERNEL);
651 if (!pg->next)
652 goto out_free;
653 pg = pg->next;
656 return 0;
658 out_free:
659 pg = stat->start;
660 while (pg) {
661 unsigned long tmp = (unsigned long)pg;
663 pg = pg->next;
664 free_page(tmp);
667 stat->pages = NULL;
668 stat->start = NULL;
670 return -ENOMEM;
673 static int ftrace_profile_init_cpu(int cpu)
675 struct ftrace_profile_stat *stat;
676 int size;
678 stat = &per_cpu(ftrace_profile_stats, cpu);
680 if (stat->hash) {
681 /* If the profile is already created, simply reset it */
682 ftrace_profile_reset(stat);
683 return 0;
687 * We are profiling all functions, but usually only a few thousand
688 * functions are hit. We'll make a hash of 1024 items.
690 size = FTRACE_PROFILE_HASH_SIZE;
692 stat->hash = kcalloc(size, sizeof(struct hlist_head), GFP_KERNEL);
694 if (!stat->hash)
695 return -ENOMEM;
697 /* Preallocate the function profiling pages */
698 if (ftrace_profile_pages_init(stat) < 0) {
699 kfree(stat->hash);
700 stat->hash = NULL;
701 return -ENOMEM;
704 return 0;
707 static int ftrace_profile_init(void)
709 int cpu;
710 int ret = 0;
712 for_each_possible_cpu(cpu) {
713 ret = ftrace_profile_init_cpu(cpu);
714 if (ret)
715 break;
718 return ret;
721 /* interrupts must be disabled */
722 static struct ftrace_profile *
723 ftrace_find_profiled_func(struct ftrace_profile_stat *stat, unsigned long ip)
725 struct ftrace_profile *rec;
726 struct hlist_head *hhd;
727 unsigned long key;
729 key = hash_long(ip, FTRACE_PROFILE_HASH_BITS);
730 hhd = &stat->hash[key];
732 if (hlist_empty(hhd))
733 return NULL;
735 hlist_for_each_entry_rcu_notrace(rec, hhd, node) {
736 if (rec->ip == ip)
737 return rec;
740 return NULL;
743 static void ftrace_add_profile(struct ftrace_profile_stat *stat,
744 struct ftrace_profile *rec)
746 unsigned long key;
748 key = hash_long(rec->ip, FTRACE_PROFILE_HASH_BITS);
749 hlist_add_head_rcu(&rec->node, &stat->hash[key]);
753 * The memory is already allocated, this simply finds a new record to use.
755 static struct ftrace_profile *
756 ftrace_profile_alloc(struct ftrace_profile_stat *stat, unsigned long ip)
758 struct ftrace_profile *rec = NULL;
760 /* prevent recursion (from NMIs) */
761 if (atomic_inc_return(&stat->disabled) != 1)
762 goto out;
765 * Try to find the function again since an NMI
766 * could have added it
768 rec = ftrace_find_profiled_func(stat, ip);
769 if (rec)
770 goto out;
772 if (stat->pages->index == PROFILES_PER_PAGE) {
773 if (!stat->pages->next)
774 goto out;
775 stat->pages = stat->pages->next;
778 rec = &stat->pages->records[stat->pages->index++];
779 rec->ip = ip;
780 ftrace_add_profile(stat, rec);
782 out:
783 atomic_dec(&stat->disabled);
785 return rec;
788 static void
789 function_profile_call(unsigned long ip, unsigned long parent_ip,
790 struct ftrace_ops *ops, struct pt_regs *regs)
792 struct ftrace_profile_stat *stat;
793 struct ftrace_profile *rec;
794 unsigned long flags;
796 if (!ftrace_profile_enabled)
797 return;
799 local_irq_save(flags);
801 stat = this_cpu_ptr(&ftrace_profile_stats);
802 if (!stat->hash || !ftrace_profile_enabled)
803 goto out;
805 rec = ftrace_find_profiled_func(stat, ip);
806 if (!rec) {
807 rec = ftrace_profile_alloc(stat, ip);
808 if (!rec)
809 goto out;
812 rec->counter++;
813 out:
814 local_irq_restore(flags);
817 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
818 static int profile_graph_entry(struct ftrace_graph_ent *trace)
820 int index = trace->depth;
822 function_profile_call(trace->func, 0, NULL, NULL);
824 /* If function graph is shutting down, ret_stack can be NULL */
825 if (!current->ret_stack)
826 return 0;
828 if (index >= 0 && index < FTRACE_RETFUNC_DEPTH)
829 current->ret_stack[index].subtime = 0;
831 return 1;
834 static void profile_graph_return(struct ftrace_graph_ret *trace)
836 struct ftrace_profile_stat *stat;
837 unsigned long long calltime;
838 struct ftrace_profile *rec;
839 unsigned long flags;
841 local_irq_save(flags);
842 stat = this_cpu_ptr(&ftrace_profile_stats);
843 if (!stat->hash || !ftrace_profile_enabled)
844 goto out;
846 /* If the calltime was zero'd ignore it */
847 if (!trace->calltime)
848 goto out;
850 calltime = trace->rettime - trace->calltime;
852 if (!fgraph_graph_time) {
853 int index;
855 index = trace->depth;
857 /* Append this call time to the parent time to subtract */
858 if (index)
859 current->ret_stack[index - 1].subtime += calltime;
861 if (current->ret_stack[index].subtime < calltime)
862 calltime -= current->ret_stack[index].subtime;
863 else
864 calltime = 0;
867 rec = ftrace_find_profiled_func(stat, trace->func);
868 if (rec) {
869 rec->time += calltime;
870 rec->time_squared += calltime * calltime;
873 out:
874 local_irq_restore(flags);
877 static int register_ftrace_profiler(void)
879 return register_ftrace_graph(&profile_graph_return,
880 &profile_graph_entry);
883 static void unregister_ftrace_profiler(void)
885 unregister_ftrace_graph();
887 #else
888 static struct ftrace_ops ftrace_profile_ops __read_mostly = {
889 .func = function_profile_call,
890 .flags = FTRACE_OPS_FL_RECURSION_SAFE | FTRACE_OPS_FL_INITIALIZED,
891 INIT_OPS_HASH(ftrace_profile_ops)
894 static int register_ftrace_profiler(void)
896 return register_ftrace_function(&ftrace_profile_ops);
899 static void unregister_ftrace_profiler(void)
901 unregister_ftrace_function(&ftrace_profile_ops);
903 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
905 static ssize_t
906 ftrace_profile_write(struct file *filp, const char __user *ubuf,
907 size_t cnt, loff_t *ppos)
909 unsigned long val;
910 int ret;
912 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
913 if (ret)
914 return ret;
916 val = !!val;
918 mutex_lock(&ftrace_profile_lock);
919 if (ftrace_profile_enabled ^ val) {
920 if (val) {
921 ret = ftrace_profile_init();
922 if (ret < 0) {
923 cnt = ret;
924 goto out;
927 ret = register_ftrace_profiler();
928 if (ret < 0) {
929 cnt = ret;
930 goto out;
932 ftrace_profile_enabled = 1;
933 } else {
934 ftrace_profile_enabled = 0;
936 * unregister_ftrace_profiler calls stop_machine
937 * so this acts like an synchronize_sched.
939 unregister_ftrace_profiler();
942 out:
943 mutex_unlock(&ftrace_profile_lock);
945 *ppos += cnt;
947 return cnt;
950 static ssize_t
951 ftrace_profile_read(struct file *filp, char __user *ubuf,
952 size_t cnt, loff_t *ppos)
954 char buf[64]; /* big enough to hold a number */
955 int r;
957 r = sprintf(buf, "%u\n", ftrace_profile_enabled);
958 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
961 static const struct file_operations ftrace_profile_fops = {
962 .open = tracing_open_generic,
963 .read = ftrace_profile_read,
964 .write = ftrace_profile_write,
965 .llseek = default_llseek,
968 /* used to initialize the real stat files */
969 static struct tracer_stat function_stats __initdata = {
970 .name = "functions",
971 .stat_start = function_stat_start,
972 .stat_next = function_stat_next,
973 .stat_cmp = function_stat_cmp,
974 .stat_headers = function_stat_headers,
975 .stat_show = function_stat_show
978 static __init void ftrace_profile_tracefs(struct dentry *d_tracer)
980 struct ftrace_profile_stat *stat;
981 struct dentry *entry;
982 char *name;
983 int ret;
984 int cpu;
986 for_each_possible_cpu(cpu) {
987 stat = &per_cpu(ftrace_profile_stats, cpu);
989 name = kasprintf(GFP_KERNEL, "function%d", cpu);
990 if (!name) {
992 * The files created are permanent, if something happens
993 * we still do not free memory.
995 WARN(1,
996 "Could not allocate stat file for cpu %d\n",
997 cpu);
998 return;
1000 stat->stat = function_stats;
1001 stat->stat.name = name;
1002 ret = register_stat_tracer(&stat->stat);
1003 if (ret) {
1004 WARN(1,
1005 "Could not register function stat for cpu %d\n",
1006 cpu);
1007 kfree(name);
1008 return;
1012 entry = tracefs_create_file("function_profile_enabled", 0644,
1013 d_tracer, NULL, &ftrace_profile_fops);
1014 if (!entry)
1015 pr_warn("Could not create tracefs 'function_profile_enabled' entry\n");
1018 #else /* CONFIG_FUNCTION_PROFILER */
1019 static __init void ftrace_profile_tracefs(struct dentry *d_tracer)
1022 #endif /* CONFIG_FUNCTION_PROFILER */
1024 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
1025 static int ftrace_graph_active;
1026 #else
1027 # define ftrace_graph_active 0
1028 #endif
1030 #ifdef CONFIG_DYNAMIC_FTRACE
1032 static struct ftrace_ops *removed_ops;
1035 * Set when doing a global update, like enabling all recs or disabling them.
1036 * It is not set when just updating a single ftrace_ops.
1038 static bool update_all_ops;
1040 #ifndef CONFIG_FTRACE_MCOUNT_RECORD
1041 # error Dynamic ftrace depends on MCOUNT_RECORD
1042 #endif
1044 struct ftrace_func_entry {
1045 struct hlist_node hlist;
1046 unsigned long ip;
1049 struct ftrace_func_probe {
1050 struct ftrace_probe_ops *probe_ops;
1051 struct ftrace_ops ops;
1052 struct trace_array *tr;
1053 struct list_head list;
1054 void *data;
1055 int ref;
1059 * We make these constant because no one should touch them,
1060 * but they are used as the default "empty hash", to avoid allocating
1061 * it all the time. These are in a read only section such that if
1062 * anyone does try to modify it, it will cause an exception.
1064 static const struct hlist_head empty_buckets[1];
1065 static const struct ftrace_hash empty_hash = {
1066 .buckets = (struct hlist_head *)empty_buckets,
1068 #define EMPTY_HASH ((struct ftrace_hash *)&empty_hash)
1070 static struct ftrace_ops global_ops = {
1071 .func = ftrace_stub,
1072 .local_hash.notrace_hash = EMPTY_HASH,
1073 .local_hash.filter_hash = EMPTY_HASH,
1074 INIT_OPS_HASH(global_ops)
1075 .flags = FTRACE_OPS_FL_RECURSION_SAFE |
1076 FTRACE_OPS_FL_INITIALIZED |
1077 FTRACE_OPS_FL_PID,
1081 * Used by the stack undwinder to know about dynamic ftrace trampolines.
1083 struct ftrace_ops *ftrace_ops_trampoline(unsigned long addr)
1085 struct ftrace_ops *op = NULL;
1088 * Some of the ops may be dynamically allocated,
1089 * they are freed after a synchronize_sched().
1091 preempt_disable_notrace();
1093 do_for_each_ftrace_op(op, ftrace_ops_list) {
1095 * This is to check for dynamically allocated trampolines.
1096 * Trampolines that are in kernel text will have
1097 * core_kernel_text() return true.
1099 if (op->trampoline && op->trampoline_size)
1100 if (addr >= op->trampoline &&
1101 addr < op->trampoline + op->trampoline_size) {
1102 preempt_enable_notrace();
1103 return op;
1105 } while_for_each_ftrace_op(op);
1106 preempt_enable_notrace();
1108 return NULL;
1112 * This is used by __kernel_text_address() to return true if the
1113 * address is on a dynamically allocated trampoline that would
1114 * not return true for either core_kernel_text() or
1115 * is_module_text_address().
1117 bool is_ftrace_trampoline(unsigned long addr)
1119 return ftrace_ops_trampoline(addr) != NULL;
1122 struct ftrace_page {
1123 struct ftrace_page *next;
1124 struct dyn_ftrace *records;
1125 int index;
1126 int size;
1129 #define ENTRY_SIZE sizeof(struct dyn_ftrace)
1130 #define ENTRIES_PER_PAGE (PAGE_SIZE / ENTRY_SIZE)
1132 /* estimate from running different kernels */
1133 #define NR_TO_INIT 10000
1135 static struct ftrace_page *ftrace_pages_start;
1136 static struct ftrace_page *ftrace_pages;
1138 static __always_inline unsigned long
1139 ftrace_hash_key(struct ftrace_hash *hash, unsigned long ip)
1141 if (hash->size_bits > 0)
1142 return hash_long(ip, hash->size_bits);
1144 return 0;
1147 /* Only use this function if ftrace_hash_empty() has already been tested */
1148 static __always_inline struct ftrace_func_entry *
1149 __ftrace_lookup_ip(struct ftrace_hash *hash, unsigned long ip)
1151 unsigned long key;
1152 struct ftrace_func_entry *entry;
1153 struct hlist_head *hhd;
1155 key = ftrace_hash_key(hash, ip);
1156 hhd = &hash->buckets[key];
1158 hlist_for_each_entry_rcu_notrace(entry, hhd, hlist) {
1159 if (entry->ip == ip)
1160 return entry;
1162 return NULL;
1166 * ftrace_lookup_ip - Test to see if an ip exists in an ftrace_hash
1167 * @hash: The hash to look at
1168 * @ip: The instruction pointer to test
1170 * Search a given @hash to see if a given instruction pointer (@ip)
1171 * exists in it.
1173 * Returns the entry that holds the @ip if found. NULL otherwise.
1175 struct ftrace_func_entry *
1176 ftrace_lookup_ip(struct ftrace_hash *hash, unsigned long ip)
1178 if (ftrace_hash_empty(hash))
1179 return NULL;
1181 return __ftrace_lookup_ip(hash, ip);
1184 static void __add_hash_entry(struct ftrace_hash *hash,
1185 struct ftrace_func_entry *entry)
1187 struct hlist_head *hhd;
1188 unsigned long key;
1190 key = ftrace_hash_key(hash, entry->ip);
1191 hhd = &hash->buckets[key];
1192 hlist_add_head(&entry->hlist, hhd);
1193 hash->count++;
1196 static int add_hash_entry(struct ftrace_hash *hash, unsigned long ip)
1198 struct ftrace_func_entry *entry;
1200 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
1201 if (!entry)
1202 return -ENOMEM;
1204 entry->ip = ip;
1205 __add_hash_entry(hash, entry);
1207 return 0;
1210 static void
1211 free_hash_entry(struct ftrace_hash *hash,
1212 struct ftrace_func_entry *entry)
1214 hlist_del(&entry->hlist);
1215 kfree(entry);
1216 hash->count--;
1219 static void
1220 remove_hash_entry(struct ftrace_hash *hash,
1221 struct ftrace_func_entry *entry)
1223 hlist_del_rcu(&entry->hlist);
1224 hash->count--;
1227 static void ftrace_hash_clear(struct ftrace_hash *hash)
1229 struct hlist_head *hhd;
1230 struct hlist_node *tn;
1231 struct ftrace_func_entry *entry;
1232 int size = 1 << hash->size_bits;
1233 int i;
1235 if (!hash->count)
1236 return;
1238 for (i = 0; i < size; i++) {
1239 hhd = &hash->buckets[i];
1240 hlist_for_each_entry_safe(entry, tn, hhd, hlist)
1241 free_hash_entry(hash, entry);
1243 FTRACE_WARN_ON(hash->count);
1246 static void free_ftrace_mod(struct ftrace_mod_load *ftrace_mod)
1248 list_del(&ftrace_mod->list);
1249 kfree(ftrace_mod->module);
1250 kfree(ftrace_mod->func);
1251 kfree(ftrace_mod);
1254 static void clear_ftrace_mod_list(struct list_head *head)
1256 struct ftrace_mod_load *p, *n;
1258 /* stack tracer isn't supported yet */
1259 if (!head)
1260 return;
1262 mutex_lock(&ftrace_lock);
1263 list_for_each_entry_safe(p, n, head, list)
1264 free_ftrace_mod(p);
1265 mutex_unlock(&ftrace_lock);
1268 static void free_ftrace_hash(struct ftrace_hash *hash)
1270 if (!hash || hash == EMPTY_HASH)
1271 return;
1272 ftrace_hash_clear(hash);
1273 kfree(hash->buckets);
1274 kfree(hash);
1277 static void __free_ftrace_hash_rcu(struct rcu_head *rcu)
1279 struct ftrace_hash *hash;
1281 hash = container_of(rcu, struct ftrace_hash, rcu);
1282 free_ftrace_hash(hash);
1285 static void free_ftrace_hash_rcu(struct ftrace_hash *hash)
1287 if (!hash || hash == EMPTY_HASH)
1288 return;
1289 call_rcu_sched(&hash->rcu, __free_ftrace_hash_rcu);
1292 void ftrace_free_filter(struct ftrace_ops *ops)
1294 ftrace_ops_init(ops);
1295 free_ftrace_hash(ops->func_hash->filter_hash);
1296 free_ftrace_hash(ops->func_hash->notrace_hash);
1299 static struct ftrace_hash *alloc_ftrace_hash(int size_bits)
1301 struct ftrace_hash *hash;
1302 int size;
1304 hash = kzalloc(sizeof(*hash), GFP_KERNEL);
1305 if (!hash)
1306 return NULL;
1308 size = 1 << size_bits;
1309 hash->buckets = kcalloc(size, sizeof(*hash->buckets), GFP_KERNEL);
1311 if (!hash->buckets) {
1312 kfree(hash);
1313 return NULL;
1316 hash->size_bits = size_bits;
1318 return hash;
1322 static int ftrace_add_mod(struct trace_array *tr,
1323 const char *func, const char *module,
1324 int enable)
1326 struct ftrace_mod_load *ftrace_mod;
1327 struct list_head *mod_head = enable ? &tr->mod_trace : &tr->mod_notrace;
1329 ftrace_mod = kzalloc(sizeof(*ftrace_mod), GFP_KERNEL);
1330 if (!ftrace_mod)
1331 return -ENOMEM;
1333 ftrace_mod->func = kstrdup(func, GFP_KERNEL);
1334 ftrace_mod->module = kstrdup(module, GFP_KERNEL);
1335 ftrace_mod->enable = enable;
1337 if (!ftrace_mod->func || !ftrace_mod->module)
1338 goto out_free;
1340 list_add(&ftrace_mod->list, mod_head);
1342 return 0;
1344 out_free:
1345 free_ftrace_mod(ftrace_mod);
1347 return -ENOMEM;
1350 static struct ftrace_hash *
1351 alloc_and_copy_ftrace_hash(int size_bits, struct ftrace_hash *hash)
1353 struct ftrace_func_entry *entry;
1354 struct ftrace_hash *new_hash;
1355 int size;
1356 int ret;
1357 int i;
1359 new_hash = alloc_ftrace_hash(size_bits);
1360 if (!new_hash)
1361 return NULL;
1363 if (hash)
1364 new_hash->flags = hash->flags;
1366 /* Empty hash? */
1367 if (ftrace_hash_empty(hash))
1368 return new_hash;
1370 size = 1 << hash->size_bits;
1371 for (i = 0; i < size; i++) {
1372 hlist_for_each_entry(entry, &hash->buckets[i], hlist) {
1373 ret = add_hash_entry(new_hash, entry->ip);
1374 if (ret < 0)
1375 goto free_hash;
1379 FTRACE_WARN_ON(new_hash->count != hash->count);
1381 return new_hash;
1383 free_hash:
1384 free_ftrace_hash(new_hash);
1385 return NULL;
1388 static void
1389 ftrace_hash_rec_disable_modify(struct ftrace_ops *ops, int filter_hash);
1390 static void
1391 ftrace_hash_rec_enable_modify(struct ftrace_ops *ops, int filter_hash);
1393 static int ftrace_hash_ipmodify_update(struct ftrace_ops *ops,
1394 struct ftrace_hash *new_hash);
1396 static struct ftrace_hash *
1397 __ftrace_hash_move(struct ftrace_hash *src)
1399 struct ftrace_func_entry *entry;
1400 struct hlist_node *tn;
1401 struct hlist_head *hhd;
1402 struct ftrace_hash *new_hash;
1403 int size = src->count;
1404 int bits = 0;
1405 int i;
1408 * If the new source is empty, just return the empty_hash.
1410 if (ftrace_hash_empty(src))
1411 return EMPTY_HASH;
1414 * Make the hash size about 1/2 the # found
1416 for (size /= 2; size; size >>= 1)
1417 bits++;
1419 /* Don't allocate too much */
1420 if (bits > FTRACE_HASH_MAX_BITS)
1421 bits = FTRACE_HASH_MAX_BITS;
1423 new_hash = alloc_ftrace_hash(bits);
1424 if (!new_hash)
1425 return NULL;
1427 new_hash->flags = src->flags;
1429 size = 1 << src->size_bits;
1430 for (i = 0; i < size; i++) {
1431 hhd = &src->buckets[i];
1432 hlist_for_each_entry_safe(entry, tn, hhd, hlist) {
1433 remove_hash_entry(src, entry);
1434 __add_hash_entry(new_hash, entry);
1438 return new_hash;
1441 static int
1442 ftrace_hash_move(struct ftrace_ops *ops, int enable,
1443 struct ftrace_hash **dst, struct ftrace_hash *src)
1445 struct ftrace_hash *new_hash;
1446 int ret;
1448 /* Reject setting notrace hash on IPMODIFY ftrace_ops */
1449 if (ops->flags & FTRACE_OPS_FL_IPMODIFY && !enable)
1450 return -EINVAL;
1452 new_hash = __ftrace_hash_move(src);
1453 if (!new_hash)
1454 return -ENOMEM;
1456 /* Make sure this can be applied if it is IPMODIFY ftrace_ops */
1457 if (enable) {
1458 /* IPMODIFY should be updated only when filter_hash updating */
1459 ret = ftrace_hash_ipmodify_update(ops, new_hash);
1460 if (ret < 0) {
1461 free_ftrace_hash(new_hash);
1462 return ret;
1467 * Remove the current set, update the hash and add
1468 * them back.
1470 ftrace_hash_rec_disable_modify(ops, enable);
1472 rcu_assign_pointer(*dst, new_hash);
1474 ftrace_hash_rec_enable_modify(ops, enable);
1476 return 0;
1479 static bool hash_contains_ip(unsigned long ip,
1480 struct ftrace_ops_hash *hash)
1483 * The function record is a match if it exists in the filter
1484 * hash and not in the notrace hash. Note, an emty hash is
1485 * considered a match for the filter hash, but an empty
1486 * notrace hash is considered not in the notrace hash.
1488 return (ftrace_hash_empty(hash->filter_hash) ||
1489 __ftrace_lookup_ip(hash->filter_hash, ip)) &&
1490 (ftrace_hash_empty(hash->notrace_hash) ||
1491 !__ftrace_lookup_ip(hash->notrace_hash, ip));
1495 * Test the hashes for this ops to see if we want to call
1496 * the ops->func or not.
1498 * It's a match if the ip is in the ops->filter_hash or
1499 * the filter_hash does not exist or is empty,
1500 * AND
1501 * the ip is not in the ops->notrace_hash.
1503 * This needs to be called with preemption disabled as
1504 * the hashes are freed with call_rcu_sched().
1506 static int
1507 ftrace_ops_test(struct ftrace_ops *ops, unsigned long ip, void *regs)
1509 struct ftrace_ops_hash hash;
1510 int ret;
1512 #ifdef CONFIG_DYNAMIC_FTRACE_WITH_REGS
1514 * There's a small race when adding ops that the ftrace handler
1515 * that wants regs, may be called without them. We can not
1516 * allow that handler to be called if regs is NULL.
1518 if (regs == NULL && (ops->flags & FTRACE_OPS_FL_SAVE_REGS))
1519 return 0;
1520 #endif
1522 rcu_assign_pointer(hash.filter_hash, ops->func_hash->filter_hash);
1523 rcu_assign_pointer(hash.notrace_hash, ops->func_hash->notrace_hash);
1525 if (hash_contains_ip(ip, &hash))
1526 ret = 1;
1527 else
1528 ret = 0;
1530 return ret;
1534 * This is a double for. Do not use 'break' to break out of the loop,
1535 * you must use a goto.
1537 #define do_for_each_ftrace_rec(pg, rec) \
1538 for (pg = ftrace_pages_start; pg; pg = pg->next) { \
1539 int _____i; \
1540 for (_____i = 0; _____i < pg->index; _____i++) { \
1541 rec = &pg->records[_____i];
1543 #define while_for_each_ftrace_rec() \
1548 static int ftrace_cmp_recs(const void *a, const void *b)
1550 const struct dyn_ftrace *key = a;
1551 const struct dyn_ftrace *rec = b;
1553 if (key->flags < rec->ip)
1554 return -1;
1555 if (key->ip >= rec->ip + MCOUNT_INSN_SIZE)
1556 return 1;
1557 return 0;
1561 * ftrace_location_range - return the first address of a traced location
1562 * if it touches the given ip range
1563 * @start: start of range to search.
1564 * @end: end of range to search (inclusive). @end points to the last byte
1565 * to check.
1567 * Returns rec->ip if the related ftrace location is a least partly within
1568 * the given address range. That is, the first address of the instruction
1569 * that is either a NOP or call to the function tracer. It checks the ftrace
1570 * internal tables to determine if the address belongs or not.
1572 unsigned long ftrace_location_range(unsigned long start, unsigned long end)
1574 struct ftrace_page *pg;
1575 struct dyn_ftrace *rec;
1576 struct dyn_ftrace key;
1578 key.ip = start;
1579 key.flags = end; /* overload flags, as it is unsigned long */
1581 for (pg = ftrace_pages_start; pg; pg = pg->next) {
1582 if (end < pg->records[0].ip ||
1583 start >= (pg->records[pg->index - 1].ip + MCOUNT_INSN_SIZE))
1584 continue;
1585 rec = bsearch(&key, pg->records, pg->index,
1586 sizeof(struct dyn_ftrace),
1587 ftrace_cmp_recs);
1588 if (rec)
1589 return rec->ip;
1592 return 0;
1596 * ftrace_location - return true if the ip giving is a traced location
1597 * @ip: the instruction pointer to check
1599 * Returns rec->ip if @ip given is a pointer to 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 unsigned long ftrace_location(unsigned long ip)
1606 return ftrace_location_range(ip, ip);
1610 * ftrace_text_reserved - return true if range contains an ftrace location
1611 * @start: start of range to search
1612 * @end: end of range to search (inclusive). @end points to the last byte to check.
1614 * Returns 1 if @start and @end contains a ftrace location.
1615 * That is, the instruction that is either a NOP or call to
1616 * the function tracer. It checks the ftrace internal tables to
1617 * determine if the address belongs or not.
1619 int ftrace_text_reserved(const void *start, const void *end)
1621 unsigned long ret;
1623 ret = ftrace_location_range((unsigned long)start,
1624 (unsigned long)end);
1626 return (int)!!ret;
1629 /* Test if ops registered to this rec needs regs */
1630 static bool test_rec_ops_needs_regs(struct dyn_ftrace *rec)
1632 struct ftrace_ops *ops;
1633 bool keep_regs = false;
1635 for (ops = ftrace_ops_list;
1636 ops != &ftrace_list_end; ops = ops->next) {
1637 /* pass rec in as regs to have non-NULL val */
1638 if (ftrace_ops_test(ops, rec->ip, rec)) {
1639 if (ops->flags & FTRACE_OPS_FL_SAVE_REGS) {
1640 keep_regs = true;
1641 break;
1646 return keep_regs;
1649 static bool __ftrace_hash_rec_update(struct ftrace_ops *ops,
1650 int filter_hash,
1651 bool inc)
1653 struct ftrace_hash *hash;
1654 struct ftrace_hash *other_hash;
1655 struct ftrace_page *pg;
1656 struct dyn_ftrace *rec;
1657 bool update = false;
1658 int count = 0;
1659 int all = false;
1661 /* Only update if the ops has been registered */
1662 if (!(ops->flags & FTRACE_OPS_FL_ENABLED))
1663 return false;
1666 * In the filter_hash case:
1667 * If the count is zero, we update all records.
1668 * Otherwise we just update the items in the hash.
1670 * In the notrace_hash case:
1671 * We enable the update in the hash.
1672 * As disabling notrace means enabling the tracing,
1673 * and enabling notrace means disabling, the inc variable
1674 * gets inversed.
1676 if (filter_hash) {
1677 hash = ops->func_hash->filter_hash;
1678 other_hash = ops->func_hash->notrace_hash;
1679 if (ftrace_hash_empty(hash))
1680 all = true;
1681 } else {
1682 inc = !inc;
1683 hash = ops->func_hash->notrace_hash;
1684 other_hash = ops->func_hash->filter_hash;
1686 * If the notrace hash has no items,
1687 * then there's nothing to do.
1689 if (ftrace_hash_empty(hash))
1690 return false;
1693 do_for_each_ftrace_rec(pg, rec) {
1694 int in_other_hash = 0;
1695 int in_hash = 0;
1696 int match = 0;
1698 if (rec->flags & FTRACE_FL_DISABLED)
1699 continue;
1701 if (all) {
1703 * Only the filter_hash affects all records.
1704 * Update if the record is not in the notrace hash.
1706 if (!other_hash || !ftrace_lookup_ip(other_hash, rec->ip))
1707 match = 1;
1708 } else {
1709 in_hash = !!ftrace_lookup_ip(hash, rec->ip);
1710 in_other_hash = !!ftrace_lookup_ip(other_hash, rec->ip);
1713 * If filter_hash is set, we want to match all functions
1714 * that are in the hash but not in the other hash.
1716 * If filter_hash is not set, then we are decrementing.
1717 * That means we match anything that is in the hash
1718 * and also in the other_hash. That is, we need to turn
1719 * off functions in the other hash because they are disabled
1720 * by this hash.
1722 if (filter_hash && in_hash && !in_other_hash)
1723 match = 1;
1724 else if (!filter_hash && in_hash &&
1725 (in_other_hash || ftrace_hash_empty(other_hash)))
1726 match = 1;
1728 if (!match)
1729 continue;
1731 if (inc) {
1732 rec->flags++;
1733 if (FTRACE_WARN_ON(ftrace_rec_count(rec) == FTRACE_REF_MAX))
1734 return false;
1737 * If there's only a single callback registered to a
1738 * function, and the ops has a trampoline registered
1739 * for it, then we can call it directly.
1741 if (ftrace_rec_count(rec) == 1 && ops->trampoline)
1742 rec->flags |= FTRACE_FL_TRAMP;
1743 else
1745 * If we are adding another function callback
1746 * to this function, and the previous had a
1747 * custom trampoline in use, then we need to go
1748 * back to the default trampoline.
1750 rec->flags &= ~FTRACE_FL_TRAMP;
1753 * If any ops wants regs saved for this function
1754 * then all ops will get saved regs.
1756 if (ops->flags & FTRACE_OPS_FL_SAVE_REGS)
1757 rec->flags |= FTRACE_FL_REGS;
1758 } else {
1759 if (FTRACE_WARN_ON(ftrace_rec_count(rec) == 0))
1760 return false;
1761 rec->flags--;
1764 * If the rec had REGS enabled and the ops that is
1765 * being removed had REGS set, then see if there is
1766 * still any ops for this record that wants regs.
1767 * If not, we can stop recording them.
1769 if (ftrace_rec_count(rec) > 0 &&
1770 rec->flags & FTRACE_FL_REGS &&
1771 ops->flags & FTRACE_OPS_FL_SAVE_REGS) {
1772 if (!test_rec_ops_needs_regs(rec))
1773 rec->flags &= ~FTRACE_FL_REGS;
1777 * If the rec had TRAMP enabled, then it needs to
1778 * be cleared. As TRAMP can only be enabled iff
1779 * there is only a single ops attached to it.
1780 * In otherwords, always disable it on decrementing.
1781 * In the future, we may set it if rec count is
1782 * decremented to one, and the ops that is left
1783 * has a trampoline.
1785 rec->flags &= ~FTRACE_FL_TRAMP;
1788 * flags will be cleared in ftrace_check_record()
1789 * if rec count is zero.
1792 count++;
1794 /* Must match FTRACE_UPDATE_CALLS in ftrace_modify_all_code() */
1795 update |= ftrace_test_record(rec, 1) != FTRACE_UPDATE_IGNORE;
1797 /* Shortcut, if we handled all records, we are done. */
1798 if (!all && count == hash->count)
1799 return update;
1800 } while_for_each_ftrace_rec();
1802 return update;
1805 static bool ftrace_hash_rec_disable(struct ftrace_ops *ops,
1806 int filter_hash)
1808 return __ftrace_hash_rec_update(ops, filter_hash, 0);
1811 static bool ftrace_hash_rec_enable(struct ftrace_ops *ops,
1812 int filter_hash)
1814 return __ftrace_hash_rec_update(ops, filter_hash, 1);
1817 static void ftrace_hash_rec_update_modify(struct ftrace_ops *ops,
1818 int filter_hash, int inc)
1820 struct ftrace_ops *op;
1822 __ftrace_hash_rec_update(ops, filter_hash, inc);
1824 if (ops->func_hash != &global_ops.local_hash)
1825 return;
1828 * If the ops shares the global_ops hash, then we need to update
1829 * all ops that are enabled and use this hash.
1831 do_for_each_ftrace_op(op, ftrace_ops_list) {
1832 /* Already done */
1833 if (op == ops)
1834 continue;
1835 if (op->func_hash == &global_ops.local_hash)
1836 __ftrace_hash_rec_update(op, filter_hash, inc);
1837 } while_for_each_ftrace_op(op);
1840 static void ftrace_hash_rec_disable_modify(struct ftrace_ops *ops,
1841 int filter_hash)
1843 ftrace_hash_rec_update_modify(ops, filter_hash, 0);
1846 static void ftrace_hash_rec_enable_modify(struct ftrace_ops *ops,
1847 int filter_hash)
1849 ftrace_hash_rec_update_modify(ops, filter_hash, 1);
1853 * Try to update IPMODIFY flag on each ftrace_rec. Return 0 if it is OK
1854 * or no-needed to update, -EBUSY if it detects a conflict of the flag
1855 * on a ftrace_rec, and -EINVAL if the new_hash tries to trace all recs.
1856 * Note that old_hash and new_hash has below meanings
1857 * - If the hash is NULL, it hits all recs (if IPMODIFY is set, this is rejected)
1858 * - If the hash is EMPTY_HASH, it hits nothing
1859 * - Anything else hits the recs which match the hash entries.
1861 static int __ftrace_hash_update_ipmodify(struct ftrace_ops *ops,
1862 struct ftrace_hash *old_hash,
1863 struct ftrace_hash *new_hash)
1865 struct ftrace_page *pg;
1866 struct dyn_ftrace *rec, *end = NULL;
1867 int in_old, in_new;
1869 /* Only update if the ops has been registered */
1870 if (!(ops->flags & FTRACE_OPS_FL_ENABLED))
1871 return 0;
1873 if (!(ops->flags & FTRACE_OPS_FL_IPMODIFY))
1874 return 0;
1877 * Since the IPMODIFY is a very address sensitive action, we do not
1878 * allow ftrace_ops to set all functions to new hash.
1880 if (!new_hash || !old_hash)
1881 return -EINVAL;
1883 /* Update rec->flags */
1884 do_for_each_ftrace_rec(pg, rec) {
1886 if (rec->flags & FTRACE_FL_DISABLED)
1887 continue;
1889 /* We need to update only differences of filter_hash */
1890 in_old = !!ftrace_lookup_ip(old_hash, rec->ip);
1891 in_new = !!ftrace_lookup_ip(new_hash, rec->ip);
1892 if (in_old == in_new)
1893 continue;
1895 if (in_new) {
1896 /* New entries must ensure no others are using it */
1897 if (rec->flags & FTRACE_FL_IPMODIFY)
1898 goto rollback;
1899 rec->flags |= FTRACE_FL_IPMODIFY;
1900 } else /* Removed entry */
1901 rec->flags &= ~FTRACE_FL_IPMODIFY;
1902 } while_for_each_ftrace_rec();
1904 return 0;
1906 rollback:
1907 end = rec;
1909 /* Roll back what we did above */
1910 do_for_each_ftrace_rec(pg, rec) {
1912 if (rec->flags & FTRACE_FL_DISABLED)
1913 continue;
1915 if (rec == end)
1916 goto err_out;
1918 in_old = !!ftrace_lookup_ip(old_hash, rec->ip);
1919 in_new = !!ftrace_lookup_ip(new_hash, rec->ip);
1920 if (in_old == in_new)
1921 continue;
1923 if (in_new)
1924 rec->flags &= ~FTRACE_FL_IPMODIFY;
1925 else
1926 rec->flags |= FTRACE_FL_IPMODIFY;
1927 } while_for_each_ftrace_rec();
1929 err_out:
1930 return -EBUSY;
1933 static int ftrace_hash_ipmodify_enable(struct ftrace_ops *ops)
1935 struct ftrace_hash *hash = ops->func_hash->filter_hash;
1937 if (ftrace_hash_empty(hash))
1938 hash = NULL;
1940 return __ftrace_hash_update_ipmodify(ops, EMPTY_HASH, hash);
1943 /* Disabling always succeeds */
1944 static void ftrace_hash_ipmodify_disable(struct ftrace_ops *ops)
1946 struct ftrace_hash *hash = ops->func_hash->filter_hash;
1948 if (ftrace_hash_empty(hash))
1949 hash = NULL;
1951 __ftrace_hash_update_ipmodify(ops, hash, EMPTY_HASH);
1954 static int ftrace_hash_ipmodify_update(struct ftrace_ops *ops,
1955 struct ftrace_hash *new_hash)
1957 struct ftrace_hash *old_hash = ops->func_hash->filter_hash;
1959 if (ftrace_hash_empty(old_hash))
1960 old_hash = NULL;
1962 if (ftrace_hash_empty(new_hash))
1963 new_hash = NULL;
1965 return __ftrace_hash_update_ipmodify(ops, old_hash, new_hash);
1968 static void print_ip_ins(const char *fmt, const unsigned char *p)
1970 int i;
1972 printk(KERN_CONT "%s", fmt);
1974 for (i = 0; i < MCOUNT_INSN_SIZE; i++)
1975 printk(KERN_CONT "%s%02x", i ? ":" : "", p[i]);
1978 static struct ftrace_ops *
1979 ftrace_find_tramp_ops_any(struct dyn_ftrace *rec);
1980 static struct ftrace_ops *
1981 ftrace_find_tramp_ops_next(struct dyn_ftrace *rec, struct ftrace_ops *ops);
1983 enum ftrace_bug_type ftrace_bug_type;
1984 const void *ftrace_expected;
1986 static void print_bug_type(void)
1988 switch (ftrace_bug_type) {
1989 case FTRACE_BUG_UNKNOWN:
1990 break;
1991 case FTRACE_BUG_INIT:
1992 pr_info("Initializing ftrace call sites\n");
1993 break;
1994 case FTRACE_BUG_NOP:
1995 pr_info("Setting ftrace call site to NOP\n");
1996 break;
1997 case FTRACE_BUG_CALL:
1998 pr_info("Setting ftrace call site to call ftrace function\n");
1999 break;
2000 case FTRACE_BUG_UPDATE:
2001 pr_info("Updating ftrace call site to call a different ftrace function\n");
2002 break;
2007 * ftrace_bug - report and shutdown function tracer
2008 * @failed: The failed type (EFAULT, EINVAL, EPERM)
2009 * @rec: The record that failed
2011 * The arch code that enables or disables the function tracing
2012 * can call ftrace_bug() when it has detected a problem in
2013 * modifying the code. @failed should be one of either:
2014 * EFAULT - if the problem happens on reading the @ip address
2015 * EINVAL - if what is read at @ip is not what was expected
2016 * EPERM - if the problem happens on writting to the @ip address
2018 void ftrace_bug(int failed, struct dyn_ftrace *rec)
2020 unsigned long ip = rec ? rec->ip : 0;
2022 switch (failed) {
2023 case -EFAULT:
2024 FTRACE_WARN_ON_ONCE(1);
2025 pr_info("ftrace faulted on modifying ");
2026 print_ip_sym(ip);
2027 break;
2028 case -EINVAL:
2029 FTRACE_WARN_ON_ONCE(1);
2030 pr_info("ftrace failed to modify ");
2031 print_ip_sym(ip);
2032 print_ip_ins(" actual: ", (unsigned char *)ip);
2033 pr_cont("\n");
2034 if (ftrace_expected) {
2035 print_ip_ins(" expected: ", ftrace_expected);
2036 pr_cont("\n");
2038 break;
2039 case -EPERM:
2040 FTRACE_WARN_ON_ONCE(1);
2041 pr_info("ftrace faulted on writing ");
2042 print_ip_sym(ip);
2043 break;
2044 default:
2045 FTRACE_WARN_ON_ONCE(1);
2046 pr_info("ftrace faulted on unknown error ");
2047 print_ip_sym(ip);
2049 print_bug_type();
2050 if (rec) {
2051 struct ftrace_ops *ops = NULL;
2053 pr_info("ftrace record flags: %lx\n", rec->flags);
2054 pr_cont(" (%ld)%s", ftrace_rec_count(rec),
2055 rec->flags & FTRACE_FL_REGS ? " R" : " ");
2056 if (rec->flags & FTRACE_FL_TRAMP_EN) {
2057 ops = ftrace_find_tramp_ops_any(rec);
2058 if (ops) {
2059 do {
2060 pr_cont("\ttramp: %pS (%pS)",
2061 (void *)ops->trampoline,
2062 (void *)ops->func);
2063 ops = ftrace_find_tramp_ops_next(rec, ops);
2064 } while (ops);
2065 } else
2066 pr_cont("\ttramp: ERROR!");
2069 ip = ftrace_get_addr_curr(rec);
2070 pr_cont("\n expected tramp: %lx\n", ip);
2074 static int ftrace_check_record(struct dyn_ftrace *rec, int enable, int update)
2076 unsigned long flag = 0UL;
2078 ftrace_bug_type = FTRACE_BUG_UNKNOWN;
2080 if (rec->flags & FTRACE_FL_DISABLED)
2081 return FTRACE_UPDATE_IGNORE;
2084 * If we are updating calls:
2086 * If the record has a ref count, then we need to enable it
2087 * because someone is using it.
2089 * Otherwise we make sure its disabled.
2091 * If we are disabling calls, then disable all records that
2092 * are enabled.
2094 if (enable && ftrace_rec_count(rec))
2095 flag = FTRACE_FL_ENABLED;
2098 * If enabling and the REGS flag does not match the REGS_EN, or
2099 * the TRAMP flag doesn't match the TRAMP_EN, then do not ignore
2100 * this record. Set flags to fail the compare against ENABLED.
2102 if (flag) {
2103 if (!(rec->flags & FTRACE_FL_REGS) !=
2104 !(rec->flags & FTRACE_FL_REGS_EN))
2105 flag |= FTRACE_FL_REGS;
2107 if (!(rec->flags & FTRACE_FL_TRAMP) !=
2108 !(rec->flags & FTRACE_FL_TRAMP_EN))
2109 flag |= FTRACE_FL_TRAMP;
2112 /* If the state of this record hasn't changed, then do nothing */
2113 if ((rec->flags & FTRACE_FL_ENABLED) == flag)
2114 return FTRACE_UPDATE_IGNORE;
2116 if (flag) {
2117 /* Save off if rec is being enabled (for return value) */
2118 flag ^= rec->flags & FTRACE_FL_ENABLED;
2120 if (update) {
2121 rec->flags |= FTRACE_FL_ENABLED;
2122 if (flag & FTRACE_FL_REGS) {
2123 if (rec->flags & FTRACE_FL_REGS)
2124 rec->flags |= FTRACE_FL_REGS_EN;
2125 else
2126 rec->flags &= ~FTRACE_FL_REGS_EN;
2128 if (flag & FTRACE_FL_TRAMP) {
2129 if (rec->flags & FTRACE_FL_TRAMP)
2130 rec->flags |= FTRACE_FL_TRAMP_EN;
2131 else
2132 rec->flags &= ~FTRACE_FL_TRAMP_EN;
2137 * If this record is being updated from a nop, then
2138 * return UPDATE_MAKE_CALL.
2139 * Otherwise,
2140 * return UPDATE_MODIFY_CALL to tell the caller to convert
2141 * from the save regs, to a non-save regs function or
2142 * vice versa, or from a trampoline call.
2144 if (flag & FTRACE_FL_ENABLED) {
2145 ftrace_bug_type = FTRACE_BUG_CALL;
2146 return FTRACE_UPDATE_MAKE_CALL;
2149 ftrace_bug_type = FTRACE_BUG_UPDATE;
2150 return FTRACE_UPDATE_MODIFY_CALL;
2153 if (update) {
2154 /* If there's no more users, clear all flags */
2155 if (!ftrace_rec_count(rec))
2156 rec->flags = 0;
2157 else
2159 * Just disable the record, but keep the ops TRAMP
2160 * and REGS states. The _EN flags must be disabled though.
2162 rec->flags &= ~(FTRACE_FL_ENABLED | FTRACE_FL_TRAMP_EN |
2163 FTRACE_FL_REGS_EN);
2166 ftrace_bug_type = FTRACE_BUG_NOP;
2167 return FTRACE_UPDATE_MAKE_NOP;
2171 * ftrace_update_record, set a record that now is tracing or not
2172 * @rec: the record to update
2173 * @enable: set to 1 if the record is tracing, zero to force disable
2175 * The records that represent all functions that can be traced need
2176 * to be updated when tracing has been enabled.
2178 int ftrace_update_record(struct dyn_ftrace *rec, int enable)
2180 return ftrace_check_record(rec, enable, 1);
2184 * ftrace_test_record, check if the record has been enabled or not
2185 * @rec: the record to test
2186 * @enable: set to 1 to check if enabled, 0 if it is disabled
2188 * The arch code may need to test if a record is already set to
2189 * tracing to determine how to modify the function code that it
2190 * represents.
2192 int ftrace_test_record(struct dyn_ftrace *rec, int enable)
2194 return ftrace_check_record(rec, enable, 0);
2197 static struct ftrace_ops *
2198 ftrace_find_tramp_ops_any(struct dyn_ftrace *rec)
2200 struct ftrace_ops *op;
2201 unsigned long ip = rec->ip;
2203 do_for_each_ftrace_op(op, ftrace_ops_list) {
2205 if (!op->trampoline)
2206 continue;
2208 if (hash_contains_ip(ip, op->func_hash))
2209 return op;
2210 } while_for_each_ftrace_op(op);
2212 return NULL;
2215 static struct ftrace_ops *
2216 ftrace_find_tramp_ops_next(struct dyn_ftrace *rec,
2217 struct ftrace_ops *op)
2219 unsigned long ip = rec->ip;
2221 while_for_each_ftrace_op(op) {
2223 if (!op->trampoline)
2224 continue;
2226 if (hash_contains_ip(ip, op->func_hash))
2227 return op;
2230 return NULL;
2233 static struct ftrace_ops *
2234 ftrace_find_tramp_ops_curr(struct dyn_ftrace *rec)
2236 struct ftrace_ops *op;
2237 unsigned long ip = rec->ip;
2240 * Need to check removed ops first.
2241 * If they are being removed, and this rec has a tramp,
2242 * and this rec is in the ops list, then it would be the
2243 * one with the tramp.
2245 if (removed_ops) {
2246 if (hash_contains_ip(ip, &removed_ops->old_hash))
2247 return removed_ops;
2251 * Need to find the current trampoline for a rec.
2252 * Now, a trampoline is only attached to a rec if there
2253 * was a single 'ops' attached to it. But this can be called
2254 * when we are adding another op to the rec or removing the
2255 * current one. Thus, if the op is being added, we can
2256 * ignore it because it hasn't attached itself to the rec
2257 * yet.
2259 * If an ops is being modified (hooking to different functions)
2260 * then we don't care about the new functions that are being
2261 * added, just the old ones (that are probably being removed).
2263 * If we are adding an ops to a function that already is using
2264 * a trampoline, it needs to be removed (trampolines are only
2265 * for single ops connected), then an ops that is not being
2266 * modified also needs to be checked.
2268 do_for_each_ftrace_op(op, ftrace_ops_list) {
2270 if (!op->trampoline)
2271 continue;
2274 * If the ops is being added, it hasn't gotten to
2275 * the point to be removed from this tree yet.
2277 if (op->flags & FTRACE_OPS_FL_ADDING)
2278 continue;
2282 * If the ops is being modified and is in the old
2283 * hash, then it is probably being removed from this
2284 * function.
2286 if ((op->flags & FTRACE_OPS_FL_MODIFYING) &&
2287 hash_contains_ip(ip, &op->old_hash))
2288 return op;
2290 * If the ops is not being added or modified, and it's
2291 * in its normal filter hash, then this must be the one
2292 * we want!
2294 if (!(op->flags & FTRACE_OPS_FL_MODIFYING) &&
2295 hash_contains_ip(ip, op->func_hash))
2296 return op;
2298 } while_for_each_ftrace_op(op);
2300 return NULL;
2303 static struct ftrace_ops *
2304 ftrace_find_tramp_ops_new(struct dyn_ftrace *rec)
2306 struct ftrace_ops *op;
2307 unsigned long ip = rec->ip;
2309 do_for_each_ftrace_op(op, ftrace_ops_list) {
2310 /* pass rec in as regs to have non-NULL val */
2311 if (hash_contains_ip(ip, op->func_hash))
2312 return op;
2313 } while_for_each_ftrace_op(op);
2315 return NULL;
2319 * ftrace_get_addr_new - Get the call address to set to
2320 * @rec: The ftrace record descriptor
2322 * If the record has the FTRACE_FL_REGS set, that means that it
2323 * wants to convert to a callback that saves all regs. If FTRACE_FL_REGS
2324 * is not not set, then it wants to convert to the normal callback.
2326 * Returns the address of the trampoline to set to
2328 unsigned long ftrace_get_addr_new(struct dyn_ftrace *rec)
2330 struct ftrace_ops *ops;
2332 /* Trampolines take precedence over regs */
2333 if (rec->flags & FTRACE_FL_TRAMP) {
2334 ops = ftrace_find_tramp_ops_new(rec);
2335 if (FTRACE_WARN_ON(!ops || !ops->trampoline)) {
2336 pr_warn("Bad trampoline accounting at: %p (%pS) (%lx)\n",
2337 (void *)rec->ip, (void *)rec->ip, rec->flags);
2338 /* Ftrace is shutting down, return anything */
2339 return (unsigned long)FTRACE_ADDR;
2341 return ops->trampoline;
2344 if (rec->flags & FTRACE_FL_REGS)
2345 return (unsigned long)FTRACE_REGS_ADDR;
2346 else
2347 return (unsigned long)FTRACE_ADDR;
2351 * ftrace_get_addr_curr - Get the call address that is already there
2352 * @rec: The ftrace record descriptor
2354 * The FTRACE_FL_REGS_EN is set when the record already points to
2355 * a function that saves all the regs. Basically the '_EN' version
2356 * represents the current state of the function.
2358 * Returns the address of the trampoline that is currently being called
2360 unsigned long ftrace_get_addr_curr(struct dyn_ftrace *rec)
2362 struct ftrace_ops *ops;
2364 /* Trampolines take precedence over regs */
2365 if (rec->flags & FTRACE_FL_TRAMP_EN) {
2366 ops = ftrace_find_tramp_ops_curr(rec);
2367 if (FTRACE_WARN_ON(!ops)) {
2368 pr_warn("Bad trampoline accounting at: %p (%pS)\n",
2369 (void *)rec->ip, (void *)rec->ip);
2370 /* Ftrace is shutting down, return anything */
2371 return (unsigned long)FTRACE_ADDR;
2373 return ops->trampoline;
2376 if (rec->flags & FTRACE_FL_REGS_EN)
2377 return (unsigned long)FTRACE_REGS_ADDR;
2378 else
2379 return (unsigned long)FTRACE_ADDR;
2382 static int
2383 __ftrace_replace_code(struct dyn_ftrace *rec, int enable)
2385 unsigned long ftrace_old_addr;
2386 unsigned long ftrace_addr;
2387 int ret;
2389 ftrace_addr = ftrace_get_addr_new(rec);
2391 /* This needs to be done before we call ftrace_update_record */
2392 ftrace_old_addr = ftrace_get_addr_curr(rec);
2394 ret = ftrace_update_record(rec, enable);
2396 ftrace_bug_type = FTRACE_BUG_UNKNOWN;
2398 switch (ret) {
2399 case FTRACE_UPDATE_IGNORE:
2400 return 0;
2402 case FTRACE_UPDATE_MAKE_CALL:
2403 ftrace_bug_type = FTRACE_BUG_CALL;
2404 return ftrace_make_call(rec, ftrace_addr);
2406 case FTRACE_UPDATE_MAKE_NOP:
2407 ftrace_bug_type = FTRACE_BUG_NOP;
2408 return ftrace_make_nop(NULL, rec, ftrace_old_addr);
2410 case FTRACE_UPDATE_MODIFY_CALL:
2411 ftrace_bug_type = FTRACE_BUG_UPDATE;
2412 return ftrace_modify_call(rec, ftrace_old_addr, ftrace_addr);
2415 return -1; /* unknow ftrace bug */
2418 void __weak ftrace_replace_code(int enable)
2420 struct dyn_ftrace *rec;
2421 struct ftrace_page *pg;
2422 int failed;
2424 if (unlikely(ftrace_disabled))
2425 return;
2427 do_for_each_ftrace_rec(pg, rec) {
2429 if (rec->flags & FTRACE_FL_DISABLED)
2430 continue;
2432 failed = __ftrace_replace_code(rec, enable);
2433 if (failed) {
2434 ftrace_bug(failed, rec);
2435 /* Stop processing */
2436 return;
2438 } while_for_each_ftrace_rec();
2441 struct ftrace_rec_iter {
2442 struct ftrace_page *pg;
2443 int index;
2447 * ftrace_rec_iter_start, start up iterating over traced functions
2449 * Returns an iterator handle that is used to iterate over all
2450 * the records that represent address locations where functions
2451 * are traced.
2453 * May return NULL if no records are available.
2455 struct ftrace_rec_iter *ftrace_rec_iter_start(void)
2458 * We only use a single iterator.
2459 * Protected by the ftrace_lock mutex.
2461 static struct ftrace_rec_iter ftrace_rec_iter;
2462 struct ftrace_rec_iter *iter = &ftrace_rec_iter;
2464 iter->pg = ftrace_pages_start;
2465 iter->index = 0;
2467 /* Could have empty pages */
2468 while (iter->pg && !iter->pg->index)
2469 iter->pg = iter->pg->next;
2471 if (!iter->pg)
2472 return NULL;
2474 return iter;
2478 * ftrace_rec_iter_next, get the next record to process.
2479 * @iter: The handle to the iterator.
2481 * Returns the next iterator after the given iterator @iter.
2483 struct ftrace_rec_iter *ftrace_rec_iter_next(struct ftrace_rec_iter *iter)
2485 iter->index++;
2487 if (iter->index >= iter->pg->index) {
2488 iter->pg = iter->pg->next;
2489 iter->index = 0;
2491 /* Could have empty pages */
2492 while (iter->pg && !iter->pg->index)
2493 iter->pg = iter->pg->next;
2496 if (!iter->pg)
2497 return NULL;
2499 return iter;
2503 * ftrace_rec_iter_record, get the record at the iterator location
2504 * @iter: The current iterator location
2506 * Returns the record that the current @iter is at.
2508 struct dyn_ftrace *ftrace_rec_iter_record(struct ftrace_rec_iter *iter)
2510 return &iter->pg->records[iter->index];
2513 static int
2514 ftrace_code_disable(struct module *mod, struct dyn_ftrace *rec)
2516 int ret;
2518 if (unlikely(ftrace_disabled))
2519 return 0;
2521 ret = ftrace_make_nop(mod, rec, MCOUNT_ADDR);
2522 if (ret) {
2523 ftrace_bug_type = FTRACE_BUG_INIT;
2524 ftrace_bug(ret, rec);
2525 return 0;
2527 return 1;
2531 * archs can override this function if they must do something
2532 * before the modifying code is performed.
2534 int __weak ftrace_arch_code_modify_prepare(void)
2536 return 0;
2540 * archs can override this function if they must do something
2541 * after the modifying code is performed.
2543 int __weak ftrace_arch_code_modify_post_process(void)
2545 return 0;
2548 void ftrace_modify_all_code(int command)
2550 int update = command & FTRACE_UPDATE_TRACE_FUNC;
2551 int err = 0;
2554 * If the ftrace_caller calls a ftrace_ops func directly,
2555 * we need to make sure that it only traces functions it
2556 * expects to trace. When doing the switch of functions,
2557 * we need to update to the ftrace_ops_list_func first
2558 * before the transition between old and new calls are set,
2559 * as the ftrace_ops_list_func will check the ops hashes
2560 * to make sure the ops are having the right functions
2561 * traced.
2563 if (update) {
2564 err = ftrace_update_ftrace_func(ftrace_ops_list_func);
2565 if (FTRACE_WARN_ON(err))
2566 return;
2569 if (command & FTRACE_UPDATE_CALLS)
2570 ftrace_replace_code(1);
2571 else if (command & FTRACE_DISABLE_CALLS)
2572 ftrace_replace_code(0);
2574 if (update && ftrace_trace_function != ftrace_ops_list_func) {
2575 function_trace_op = set_function_trace_op;
2576 smp_wmb();
2577 /* If irqs are disabled, we are in stop machine */
2578 if (!irqs_disabled())
2579 smp_call_function(ftrace_sync_ipi, NULL, 1);
2580 err = ftrace_update_ftrace_func(ftrace_trace_function);
2581 if (FTRACE_WARN_ON(err))
2582 return;
2585 if (command & FTRACE_START_FUNC_RET)
2586 err = ftrace_enable_ftrace_graph_caller();
2587 else if (command & FTRACE_STOP_FUNC_RET)
2588 err = ftrace_disable_ftrace_graph_caller();
2589 FTRACE_WARN_ON(err);
2592 static int __ftrace_modify_code(void *data)
2594 int *command = data;
2596 ftrace_modify_all_code(*command);
2598 return 0;
2602 * ftrace_run_stop_machine, go back to the stop machine method
2603 * @command: The command to tell ftrace what to do
2605 * If an arch needs to fall back to the stop machine method, the
2606 * it can call this function.
2608 void ftrace_run_stop_machine(int command)
2610 stop_machine(__ftrace_modify_code, &command, NULL);
2614 * arch_ftrace_update_code, modify the code to trace or not trace
2615 * @command: The command that needs to be done
2617 * Archs can override this function if it does not need to
2618 * run stop_machine() to modify code.
2620 void __weak arch_ftrace_update_code(int command)
2622 ftrace_run_stop_machine(command);
2625 static void ftrace_run_update_code(int command)
2627 int ret;
2629 ret = ftrace_arch_code_modify_prepare();
2630 FTRACE_WARN_ON(ret);
2631 if (ret)
2632 return;
2635 * By default we use stop_machine() to modify the code.
2636 * But archs can do what ever they want as long as it
2637 * is safe. The stop_machine() is the safest, but also
2638 * produces the most overhead.
2640 arch_ftrace_update_code(command);
2642 ret = ftrace_arch_code_modify_post_process();
2643 FTRACE_WARN_ON(ret);
2646 static void ftrace_run_modify_code(struct ftrace_ops *ops, int command,
2647 struct ftrace_ops_hash *old_hash)
2649 ops->flags |= FTRACE_OPS_FL_MODIFYING;
2650 ops->old_hash.filter_hash = old_hash->filter_hash;
2651 ops->old_hash.notrace_hash = old_hash->notrace_hash;
2652 ftrace_run_update_code(command);
2653 ops->old_hash.filter_hash = NULL;
2654 ops->old_hash.notrace_hash = NULL;
2655 ops->flags &= ~FTRACE_OPS_FL_MODIFYING;
2658 static ftrace_func_t saved_ftrace_func;
2659 static int ftrace_start_up;
2661 void __weak arch_ftrace_trampoline_free(struct ftrace_ops *ops)
2665 static void ftrace_startup_enable(int command)
2667 if (saved_ftrace_func != ftrace_trace_function) {
2668 saved_ftrace_func = ftrace_trace_function;
2669 command |= FTRACE_UPDATE_TRACE_FUNC;
2672 if (!command || !ftrace_enabled)
2673 return;
2675 ftrace_run_update_code(command);
2678 static void ftrace_startup_all(int command)
2680 update_all_ops = true;
2681 ftrace_startup_enable(command);
2682 update_all_ops = false;
2685 static int ftrace_startup(struct ftrace_ops *ops, int command)
2687 int ret;
2689 if (unlikely(ftrace_disabled))
2690 return -ENODEV;
2692 ret = __register_ftrace_function(ops);
2693 if (ret)
2694 return ret;
2696 ftrace_start_up++;
2699 * Note that ftrace probes uses this to start up
2700 * and modify functions it will probe. But we still
2701 * set the ADDING flag for modification, as probes
2702 * do not have trampolines. If they add them in the
2703 * future, then the probes will need to distinguish
2704 * between adding and updating probes.
2706 ops->flags |= FTRACE_OPS_FL_ENABLED | FTRACE_OPS_FL_ADDING;
2708 ret = ftrace_hash_ipmodify_enable(ops);
2709 if (ret < 0) {
2710 /* Rollback registration process */
2711 __unregister_ftrace_function(ops);
2712 ftrace_start_up--;
2713 ops->flags &= ~FTRACE_OPS_FL_ENABLED;
2714 return ret;
2717 if (ftrace_hash_rec_enable(ops, 1))
2718 command |= FTRACE_UPDATE_CALLS;
2720 ftrace_startup_enable(command);
2722 ops->flags &= ~FTRACE_OPS_FL_ADDING;
2724 return 0;
2727 static int ftrace_shutdown(struct ftrace_ops *ops, int command)
2729 int ret;
2731 if (unlikely(ftrace_disabled))
2732 return -ENODEV;
2734 ret = __unregister_ftrace_function(ops);
2735 if (ret)
2736 return ret;
2738 ftrace_start_up--;
2740 * Just warn in case of unbalance, no need to kill ftrace, it's not
2741 * critical but the ftrace_call callers may be never nopped again after
2742 * further ftrace uses.
2744 WARN_ON_ONCE(ftrace_start_up < 0);
2746 /* Disabling ipmodify never fails */
2747 ftrace_hash_ipmodify_disable(ops);
2749 if (ftrace_hash_rec_disable(ops, 1))
2750 command |= FTRACE_UPDATE_CALLS;
2752 ops->flags &= ~FTRACE_OPS_FL_ENABLED;
2754 if (saved_ftrace_func != ftrace_trace_function) {
2755 saved_ftrace_func = ftrace_trace_function;
2756 command |= FTRACE_UPDATE_TRACE_FUNC;
2759 if (!command || !ftrace_enabled) {
2761 * If these are dynamic or per_cpu ops, they still
2762 * need their data freed. Since, function tracing is
2763 * not currently active, we can just free them
2764 * without synchronizing all CPUs.
2766 if (ops->flags & FTRACE_OPS_FL_DYNAMIC)
2767 goto free_ops;
2769 return 0;
2773 * If the ops uses a trampoline, then it needs to be
2774 * tested first on update.
2776 ops->flags |= FTRACE_OPS_FL_REMOVING;
2777 removed_ops = ops;
2779 /* The trampoline logic checks the old hashes */
2780 ops->old_hash.filter_hash = ops->func_hash->filter_hash;
2781 ops->old_hash.notrace_hash = ops->func_hash->notrace_hash;
2783 ftrace_run_update_code(command);
2786 * If there's no more ops registered with ftrace, run a
2787 * sanity check to make sure all rec flags are cleared.
2789 if (rcu_dereference_protected(ftrace_ops_list,
2790 lockdep_is_held(&ftrace_lock)) == &ftrace_list_end) {
2791 struct ftrace_page *pg;
2792 struct dyn_ftrace *rec;
2794 do_for_each_ftrace_rec(pg, rec) {
2795 if (FTRACE_WARN_ON_ONCE(rec->flags & ~FTRACE_FL_DISABLED))
2796 pr_warn(" %pS flags:%lx\n",
2797 (void *)rec->ip, rec->flags);
2798 } while_for_each_ftrace_rec();
2801 ops->old_hash.filter_hash = NULL;
2802 ops->old_hash.notrace_hash = NULL;
2804 removed_ops = NULL;
2805 ops->flags &= ~FTRACE_OPS_FL_REMOVING;
2808 * Dynamic ops may be freed, we must make sure that all
2809 * callers are done before leaving this function.
2810 * The same goes for freeing the per_cpu data of the per_cpu
2811 * ops.
2813 if (ops->flags & FTRACE_OPS_FL_DYNAMIC) {
2815 * We need to do a hard force of sched synchronization.
2816 * This is because we use preempt_disable() to do RCU, but
2817 * the function tracers can be called where RCU is not watching
2818 * (like before user_exit()). We can not rely on the RCU
2819 * infrastructure to do the synchronization, thus we must do it
2820 * ourselves.
2822 schedule_on_each_cpu(ftrace_sync);
2825 * When the kernel is preeptive, tasks can be preempted
2826 * while on a ftrace trampoline. Just scheduling a task on
2827 * a CPU is not good enough to flush them. Calling
2828 * synchornize_rcu_tasks() will wait for those tasks to
2829 * execute and either schedule voluntarily or enter user space.
2831 if (IS_ENABLED(CONFIG_PREEMPT))
2832 synchronize_rcu_tasks();
2834 free_ops:
2835 arch_ftrace_trampoline_free(ops);
2838 return 0;
2841 static void ftrace_startup_sysctl(void)
2843 int command;
2845 if (unlikely(ftrace_disabled))
2846 return;
2848 /* Force update next time */
2849 saved_ftrace_func = NULL;
2850 /* ftrace_start_up is true if we want ftrace running */
2851 if (ftrace_start_up) {
2852 command = FTRACE_UPDATE_CALLS;
2853 if (ftrace_graph_active)
2854 command |= FTRACE_START_FUNC_RET;
2855 ftrace_startup_enable(command);
2859 static void ftrace_shutdown_sysctl(void)
2861 int command;
2863 if (unlikely(ftrace_disabled))
2864 return;
2866 /* ftrace_start_up is true if ftrace is running */
2867 if (ftrace_start_up) {
2868 command = FTRACE_DISABLE_CALLS;
2869 if (ftrace_graph_active)
2870 command |= FTRACE_STOP_FUNC_RET;
2871 ftrace_run_update_code(command);
2875 static u64 ftrace_update_time;
2876 unsigned long ftrace_update_tot_cnt;
2878 static inline int ops_traces_mod(struct ftrace_ops *ops)
2881 * Filter_hash being empty will default to trace module.
2882 * But notrace hash requires a test of individual module functions.
2884 return ftrace_hash_empty(ops->func_hash->filter_hash) &&
2885 ftrace_hash_empty(ops->func_hash->notrace_hash);
2889 * Check if the current ops references the record.
2891 * If the ops traces all functions, then it was already accounted for.
2892 * If the ops does not trace the current record function, skip it.
2893 * If the ops ignores the function via notrace filter, skip it.
2895 static inline bool
2896 ops_references_rec(struct ftrace_ops *ops, struct dyn_ftrace *rec)
2898 /* If ops isn't enabled, ignore it */
2899 if (!(ops->flags & FTRACE_OPS_FL_ENABLED))
2900 return false;
2902 /* If ops traces all then it includes this function */
2903 if (ops_traces_mod(ops))
2904 return true;
2906 /* The function must be in the filter */
2907 if (!ftrace_hash_empty(ops->func_hash->filter_hash) &&
2908 !__ftrace_lookup_ip(ops->func_hash->filter_hash, rec->ip))
2909 return false;
2911 /* If in notrace hash, we ignore it too */
2912 if (ftrace_lookup_ip(ops->func_hash->notrace_hash, rec->ip))
2913 return false;
2915 return true;
2918 static int ftrace_update_code(struct module *mod, struct ftrace_page *new_pgs)
2920 struct ftrace_page *pg;
2921 struct dyn_ftrace *p;
2922 u64 start, stop;
2923 unsigned long update_cnt = 0;
2924 unsigned long rec_flags = 0;
2925 int i;
2927 start = ftrace_now(raw_smp_processor_id());
2930 * When a module is loaded, this function is called to convert
2931 * the calls to mcount in its text to nops, and also to create
2932 * an entry in the ftrace data. Now, if ftrace is activated
2933 * after this call, but before the module sets its text to
2934 * read-only, the modification of enabling ftrace can fail if
2935 * the read-only is done while ftrace is converting the calls.
2936 * To prevent this, the module's records are set as disabled
2937 * and will be enabled after the call to set the module's text
2938 * to read-only.
2940 if (mod)
2941 rec_flags |= FTRACE_FL_DISABLED;
2943 for (pg = new_pgs; pg; pg = pg->next) {
2945 for (i = 0; i < pg->index; i++) {
2947 /* If something went wrong, bail without enabling anything */
2948 if (unlikely(ftrace_disabled))
2949 return -1;
2951 p = &pg->records[i];
2952 p->flags = rec_flags;
2954 #ifndef CC_USING_NOP_MCOUNT
2956 * Do the initial record conversion from mcount jump
2957 * to the NOP instructions.
2959 if (!ftrace_code_disable(mod, p))
2960 break;
2961 #endif
2963 update_cnt++;
2967 stop = ftrace_now(raw_smp_processor_id());
2968 ftrace_update_time = stop - start;
2969 ftrace_update_tot_cnt += update_cnt;
2971 return 0;
2974 static int ftrace_allocate_records(struct ftrace_page *pg, int count)
2976 int order;
2977 int cnt;
2979 if (WARN_ON(!count))
2980 return -EINVAL;
2982 order = get_count_order(DIV_ROUND_UP(count, ENTRIES_PER_PAGE));
2985 * We want to fill as much as possible. No more than a page
2986 * may be empty.
2988 while ((PAGE_SIZE << order) / ENTRY_SIZE >= count + ENTRIES_PER_PAGE)
2989 order--;
2991 again:
2992 pg->records = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, order);
2994 if (!pg->records) {
2995 /* if we can't allocate this size, try something smaller */
2996 if (!order)
2997 return -ENOMEM;
2998 order >>= 1;
2999 goto again;
3002 cnt = (PAGE_SIZE << order) / ENTRY_SIZE;
3003 pg->size = cnt;
3005 if (cnt > count)
3006 cnt = count;
3008 return cnt;
3011 static struct ftrace_page *
3012 ftrace_allocate_pages(unsigned long num_to_init)
3014 struct ftrace_page *start_pg;
3015 struct ftrace_page *pg;
3016 int order;
3017 int cnt;
3019 if (!num_to_init)
3020 return 0;
3022 start_pg = pg = kzalloc(sizeof(*pg), GFP_KERNEL);
3023 if (!pg)
3024 return NULL;
3027 * Try to allocate as much as possible in one continues
3028 * location that fills in all of the space. We want to
3029 * waste as little space as possible.
3031 for (;;) {
3032 cnt = ftrace_allocate_records(pg, num_to_init);
3033 if (cnt < 0)
3034 goto free_pages;
3036 num_to_init -= cnt;
3037 if (!num_to_init)
3038 break;
3040 pg->next = kzalloc(sizeof(*pg), GFP_KERNEL);
3041 if (!pg->next)
3042 goto free_pages;
3044 pg = pg->next;
3047 return start_pg;
3049 free_pages:
3050 pg = start_pg;
3051 while (pg) {
3052 order = get_count_order(pg->size / ENTRIES_PER_PAGE);
3053 free_pages((unsigned long)pg->records, order);
3054 start_pg = pg->next;
3055 kfree(pg);
3056 pg = start_pg;
3058 pr_info("ftrace: FAILED to allocate memory for functions\n");
3059 return NULL;
3062 #define FTRACE_BUFF_MAX (KSYM_SYMBOL_LEN+4) /* room for wildcards */
3064 struct ftrace_iterator {
3065 loff_t pos;
3066 loff_t func_pos;
3067 loff_t mod_pos;
3068 struct ftrace_page *pg;
3069 struct dyn_ftrace *func;
3070 struct ftrace_func_probe *probe;
3071 struct ftrace_func_entry *probe_entry;
3072 struct trace_parser parser;
3073 struct ftrace_hash *hash;
3074 struct ftrace_ops *ops;
3075 struct trace_array *tr;
3076 struct list_head *mod_list;
3077 int pidx;
3078 int idx;
3079 unsigned flags;
3082 static void *
3083 t_probe_next(struct seq_file *m, loff_t *pos)
3085 struct ftrace_iterator *iter = m->private;
3086 struct trace_array *tr = iter->ops->private;
3087 struct list_head *func_probes;
3088 struct ftrace_hash *hash;
3089 struct list_head *next;
3090 struct hlist_node *hnd = NULL;
3091 struct hlist_head *hhd;
3092 int size;
3094 (*pos)++;
3095 iter->pos = *pos;
3097 if (!tr)
3098 return NULL;
3100 func_probes = &tr->func_probes;
3101 if (list_empty(func_probes))
3102 return NULL;
3104 if (!iter->probe) {
3105 next = func_probes->next;
3106 iter->probe = list_entry(next, struct ftrace_func_probe, list);
3109 if (iter->probe_entry)
3110 hnd = &iter->probe_entry->hlist;
3112 hash = iter->probe->ops.func_hash->filter_hash;
3113 size = 1 << hash->size_bits;
3115 retry:
3116 if (iter->pidx >= size) {
3117 if (iter->probe->list.next == func_probes)
3118 return NULL;
3119 next = iter->probe->list.next;
3120 iter->probe = list_entry(next, struct ftrace_func_probe, list);
3121 hash = iter->probe->ops.func_hash->filter_hash;
3122 size = 1 << hash->size_bits;
3123 iter->pidx = 0;
3126 hhd = &hash->buckets[iter->pidx];
3128 if (hlist_empty(hhd)) {
3129 iter->pidx++;
3130 hnd = NULL;
3131 goto retry;
3134 if (!hnd)
3135 hnd = hhd->first;
3136 else {
3137 hnd = hnd->next;
3138 if (!hnd) {
3139 iter->pidx++;
3140 goto retry;
3144 if (WARN_ON_ONCE(!hnd))
3145 return NULL;
3147 iter->probe_entry = hlist_entry(hnd, struct ftrace_func_entry, hlist);
3149 return iter;
3152 static void *t_probe_start(struct seq_file *m, loff_t *pos)
3154 struct ftrace_iterator *iter = m->private;
3155 void *p = NULL;
3156 loff_t l;
3158 if (!(iter->flags & FTRACE_ITER_DO_PROBES))
3159 return NULL;
3161 if (iter->mod_pos > *pos)
3162 return NULL;
3164 iter->probe = NULL;
3165 iter->probe_entry = NULL;
3166 iter->pidx = 0;
3167 for (l = 0; l <= (*pos - iter->mod_pos); ) {
3168 p = t_probe_next(m, &l);
3169 if (!p)
3170 break;
3172 if (!p)
3173 return NULL;
3175 /* Only set this if we have an item */
3176 iter->flags |= FTRACE_ITER_PROBE;
3178 return iter;
3181 static int
3182 t_probe_show(struct seq_file *m, struct ftrace_iterator *iter)
3184 struct ftrace_func_entry *probe_entry;
3185 struct ftrace_probe_ops *probe_ops;
3186 struct ftrace_func_probe *probe;
3188 probe = iter->probe;
3189 probe_entry = iter->probe_entry;
3191 if (WARN_ON_ONCE(!probe || !probe_entry))
3192 return -EIO;
3194 probe_ops = probe->probe_ops;
3196 if (probe_ops->print)
3197 return probe_ops->print(m, probe_entry->ip, probe_ops, probe->data);
3199 seq_printf(m, "%ps:%ps\n", (void *)probe_entry->ip,
3200 (void *)probe_ops->func);
3202 return 0;
3205 static void *
3206 t_mod_next(struct seq_file *m, loff_t *pos)
3208 struct ftrace_iterator *iter = m->private;
3209 struct trace_array *tr = iter->tr;
3211 (*pos)++;
3212 iter->pos = *pos;
3214 iter->mod_list = iter->mod_list->next;
3216 if (iter->mod_list == &tr->mod_trace ||
3217 iter->mod_list == &tr->mod_notrace) {
3218 iter->flags &= ~FTRACE_ITER_MOD;
3219 return NULL;
3222 iter->mod_pos = *pos;
3224 return iter;
3227 static void *t_mod_start(struct seq_file *m, loff_t *pos)
3229 struct ftrace_iterator *iter = m->private;
3230 void *p = NULL;
3231 loff_t l;
3233 if (iter->func_pos > *pos)
3234 return NULL;
3236 iter->mod_pos = iter->func_pos;
3238 /* probes are only available if tr is set */
3239 if (!iter->tr)
3240 return NULL;
3242 for (l = 0; l <= (*pos - iter->func_pos); ) {
3243 p = t_mod_next(m, &l);
3244 if (!p)
3245 break;
3247 if (!p) {
3248 iter->flags &= ~FTRACE_ITER_MOD;
3249 return t_probe_start(m, pos);
3252 /* Only set this if we have an item */
3253 iter->flags |= FTRACE_ITER_MOD;
3255 return iter;
3258 static int
3259 t_mod_show(struct seq_file *m, struct ftrace_iterator *iter)
3261 struct ftrace_mod_load *ftrace_mod;
3262 struct trace_array *tr = iter->tr;
3264 if (WARN_ON_ONCE(!iter->mod_list) ||
3265 iter->mod_list == &tr->mod_trace ||
3266 iter->mod_list == &tr->mod_notrace)
3267 return -EIO;
3269 ftrace_mod = list_entry(iter->mod_list, struct ftrace_mod_load, list);
3271 if (ftrace_mod->func)
3272 seq_printf(m, "%s", ftrace_mod->func);
3273 else
3274 seq_putc(m, '*');
3276 seq_printf(m, ":mod:%s\n", ftrace_mod->module);
3278 return 0;
3281 static void *
3282 t_func_next(struct seq_file *m, loff_t *pos)
3284 struct ftrace_iterator *iter = m->private;
3285 struct dyn_ftrace *rec = NULL;
3287 (*pos)++;
3289 retry:
3290 if (iter->idx >= iter->pg->index) {
3291 if (iter->pg->next) {
3292 iter->pg = iter->pg->next;
3293 iter->idx = 0;
3294 goto retry;
3296 } else {
3297 rec = &iter->pg->records[iter->idx++];
3298 if (((iter->flags & (FTRACE_ITER_FILTER | FTRACE_ITER_NOTRACE)) &&
3299 !ftrace_lookup_ip(iter->hash, rec->ip)) ||
3301 ((iter->flags & FTRACE_ITER_ENABLED) &&
3302 !(rec->flags & FTRACE_FL_ENABLED))) {
3304 rec = NULL;
3305 goto retry;
3309 if (!rec)
3310 return NULL;
3312 iter->pos = iter->func_pos = *pos;
3313 iter->func = rec;
3315 return iter;
3318 static void *
3319 t_next(struct seq_file *m, void *v, loff_t *pos)
3321 struct ftrace_iterator *iter = m->private;
3322 loff_t l = *pos; /* t_probe_start() must use original pos */
3323 void *ret;
3325 if (unlikely(ftrace_disabled))
3326 return NULL;
3328 if (iter->flags & FTRACE_ITER_PROBE)
3329 return t_probe_next(m, pos);
3331 if (iter->flags & FTRACE_ITER_MOD)
3332 return t_mod_next(m, pos);
3334 if (iter->flags & FTRACE_ITER_PRINTALL) {
3335 /* next must increment pos, and t_probe_start does not */
3336 (*pos)++;
3337 return t_mod_start(m, &l);
3340 ret = t_func_next(m, pos);
3342 if (!ret)
3343 return t_mod_start(m, &l);
3345 return ret;
3348 static void reset_iter_read(struct ftrace_iterator *iter)
3350 iter->pos = 0;
3351 iter->func_pos = 0;
3352 iter->flags &= ~(FTRACE_ITER_PRINTALL | FTRACE_ITER_PROBE | FTRACE_ITER_MOD);
3355 static void *t_start(struct seq_file *m, loff_t *pos)
3357 struct ftrace_iterator *iter = m->private;
3358 void *p = NULL;
3359 loff_t l;
3361 mutex_lock(&ftrace_lock);
3363 if (unlikely(ftrace_disabled))
3364 return NULL;
3367 * If an lseek was done, then reset and start from beginning.
3369 if (*pos < iter->pos)
3370 reset_iter_read(iter);
3373 * For set_ftrace_filter reading, if we have the filter
3374 * off, we can short cut and just print out that all
3375 * functions are enabled.
3377 if ((iter->flags & (FTRACE_ITER_FILTER | FTRACE_ITER_NOTRACE)) &&
3378 ftrace_hash_empty(iter->hash)) {
3379 iter->func_pos = 1; /* Account for the message */
3380 if (*pos > 0)
3381 return t_mod_start(m, pos);
3382 iter->flags |= FTRACE_ITER_PRINTALL;
3383 /* reset in case of seek/pread */
3384 iter->flags &= ~FTRACE_ITER_PROBE;
3385 return iter;
3388 if (iter->flags & FTRACE_ITER_MOD)
3389 return t_mod_start(m, pos);
3392 * Unfortunately, we need to restart at ftrace_pages_start
3393 * every time we let go of the ftrace_mutex. This is because
3394 * those pointers can change without the lock.
3396 iter->pg = ftrace_pages_start;
3397 iter->idx = 0;
3398 for (l = 0; l <= *pos; ) {
3399 p = t_func_next(m, &l);
3400 if (!p)
3401 break;
3404 if (!p)
3405 return t_mod_start(m, pos);
3407 return iter;
3410 static void t_stop(struct seq_file *m, void *p)
3412 mutex_unlock(&ftrace_lock);
3415 void * __weak
3416 arch_ftrace_trampoline_func(struct ftrace_ops *ops, struct dyn_ftrace *rec)
3418 return NULL;
3421 static void add_trampoline_func(struct seq_file *m, struct ftrace_ops *ops,
3422 struct dyn_ftrace *rec)
3424 void *ptr;
3426 ptr = arch_ftrace_trampoline_func(ops, rec);
3427 if (ptr)
3428 seq_printf(m, " ->%pS", ptr);
3431 static int t_show(struct seq_file *m, void *v)
3433 struct ftrace_iterator *iter = m->private;
3434 struct dyn_ftrace *rec;
3436 if (iter->flags & FTRACE_ITER_PROBE)
3437 return t_probe_show(m, iter);
3439 if (iter->flags & FTRACE_ITER_MOD)
3440 return t_mod_show(m, iter);
3442 if (iter->flags & FTRACE_ITER_PRINTALL) {
3443 if (iter->flags & FTRACE_ITER_NOTRACE)
3444 seq_puts(m, "#### no functions disabled ####\n");
3445 else
3446 seq_puts(m, "#### all functions enabled ####\n");
3447 return 0;
3450 rec = iter->func;
3452 if (!rec)
3453 return 0;
3455 seq_printf(m, "%ps", (void *)rec->ip);
3456 if (iter->flags & FTRACE_ITER_ENABLED) {
3457 struct ftrace_ops *ops;
3459 seq_printf(m, " (%ld)%s%s",
3460 ftrace_rec_count(rec),
3461 rec->flags & FTRACE_FL_REGS ? " R" : " ",
3462 rec->flags & FTRACE_FL_IPMODIFY ? " I" : " ");
3463 if (rec->flags & FTRACE_FL_TRAMP_EN) {
3464 ops = ftrace_find_tramp_ops_any(rec);
3465 if (ops) {
3466 do {
3467 seq_printf(m, "\ttramp: %pS (%pS)",
3468 (void *)ops->trampoline,
3469 (void *)ops->func);
3470 add_trampoline_func(m, ops, rec);
3471 ops = ftrace_find_tramp_ops_next(rec, ops);
3472 } while (ops);
3473 } else
3474 seq_puts(m, "\ttramp: ERROR!");
3475 } else {
3476 add_trampoline_func(m, NULL, rec);
3480 seq_putc(m, '\n');
3482 return 0;
3485 static const struct seq_operations show_ftrace_seq_ops = {
3486 .start = t_start,
3487 .next = t_next,
3488 .stop = t_stop,
3489 .show = t_show,
3492 static int
3493 ftrace_avail_open(struct inode *inode, struct file *file)
3495 struct ftrace_iterator *iter;
3497 if (unlikely(ftrace_disabled))
3498 return -ENODEV;
3500 iter = __seq_open_private(file, &show_ftrace_seq_ops, sizeof(*iter));
3501 if (!iter)
3502 return -ENOMEM;
3504 iter->pg = ftrace_pages_start;
3505 iter->ops = &global_ops;
3507 return 0;
3510 static int
3511 ftrace_enabled_open(struct inode *inode, struct file *file)
3513 struct ftrace_iterator *iter;
3515 iter = __seq_open_private(file, &show_ftrace_seq_ops, sizeof(*iter));
3516 if (!iter)
3517 return -ENOMEM;
3519 iter->pg = ftrace_pages_start;
3520 iter->flags = FTRACE_ITER_ENABLED;
3521 iter->ops = &global_ops;
3523 return 0;
3527 * ftrace_regex_open - initialize function tracer filter files
3528 * @ops: The ftrace_ops that hold the hash filters
3529 * @flag: The type of filter to process
3530 * @inode: The inode, usually passed in to your open routine
3531 * @file: The file, usually passed in to your open routine
3533 * ftrace_regex_open() initializes the filter files for the
3534 * @ops. Depending on @flag it may process the filter hash or
3535 * the notrace hash of @ops. With this called from the open
3536 * routine, you can use ftrace_filter_write() for the write
3537 * routine if @flag has FTRACE_ITER_FILTER set, or
3538 * ftrace_notrace_write() if @flag has FTRACE_ITER_NOTRACE set.
3539 * tracing_lseek() should be used as the lseek routine, and
3540 * release must call ftrace_regex_release().
3543 ftrace_regex_open(struct ftrace_ops *ops, int flag,
3544 struct inode *inode, struct file *file)
3546 struct ftrace_iterator *iter;
3547 struct ftrace_hash *hash;
3548 struct list_head *mod_head;
3549 struct trace_array *tr = ops->private;
3550 int ret = 0;
3552 ftrace_ops_init(ops);
3554 if (unlikely(ftrace_disabled))
3555 return -ENODEV;
3557 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
3558 if (!iter)
3559 return -ENOMEM;
3561 if (trace_parser_get_init(&iter->parser, FTRACE_BUFF_MAX)) {
3562 kfree(iter);
3563 return -ENOMEM;
3566 iter->ops = ops;
3567 iter->flags = flag;
3568 iter->tr = tr;
3570 mutex_lock(&ops->func_hash->regex_lock);
3572 if (flag & FTRACE_ITER_NOTRACE) {
3573 hash = ops->func_hash->notrace_hash;
3574 mod_head = tr ? &tr->mod_notrace : NULL;
3575 } else {
3576 hash = ops->func_hash->filter_hash;
3577 mod_head = tr ? &tr->mod_trace : NULL;
3580 iter->mod_list = mod_head;
3582 if (file->f_mode & FMODE_WRITE) {
3583 const int size_bits = FTRACE_HASH_DEFAULT_BITS;
3585 if (file->f_flags & O_TRUNC) {
3586 iter->hash = alloc_ftrace_hash(size_bits);
3587 clear_ftrace_mod_list(mod_head);
3588 } else {
3589 iter->hash = alloc_and_copy_ftrace_hash(size_bits, hash);
3592 if (!iter->hash) {
3593 trace_parser_put(&iter->parser);
3594 kfree(iter);
3595 ret = -ENOMEM;
3596 goto out_unlock;
3598 } else
3599 iter->hash = hash;
3601 if (file->f_mode & FMODE_READ) {
3602 iter->pg = ftrace_pages_start;
3604 ret = seq_open(file, &show_ftrace_seq_ops);
3605 if (!ret) {
3606 struct seq_file *m = file->private_data;
3607 m->private = iter;
3608 } else {
3609 /* Failed */
3610 free_ftrace_hash(iter->hash);
3611 trace_parser_put(&iter->parser);
3612 kfree(iter);
3614 } else
3615 file->private_data = iter;
3617 out_unlock:
3618 mutex_unlock(&ops->func_hash->regex_lock);
3620 return ret;
3623 static int
3624 ftrace_filter_open(struct inode *inode, struct file *file)
3626 struct ftrace_ops *ops = inode->i_private;
3628 return ftrace_regex_open(ops,
3629 FTRACE_ITER_FILTER | FTRACE_ITER_DO_PROBES,
3630 inode, file);
3633 static int
3634 ftrace_notrace_open(struct inode *inode, struct file *file)
3636 struct ftrace_ops *ops = inode->i_private;
3638 return ftrace_regex_open(ops, FTRACE_ITER_NOTRACE,
3639 inode, file);
3642 /* Type for quick search ftrace basic regexes (globs) from filter_parse_regex */
3643 struct ftrace_glob {
3644 char *search;
3645 unsigned len;
3646 int type;
3650 * If symbols in an architecture don't correspond exactly to the user-visible
3651 * name of what they represent, it is possible to define this function to
3652 * perform the necessary adjustments.
3654 char * __weak arch_ftrace_match_adjust(char *str, const char *search)
3656 return str;
3659 static int ftrace_match(char *str, struct ftrace_glob *g)
3661 int matched = 0;
3662 int slen;
3664 str = arch_ftrace_match_adjust(str, g->search);
3666 switch (g->type) {
3667 case MATCH_FULL:
3668 if (strcmp(str, g->search) == 0)
3669 matched = 1;
3670 break;
3671 case MATCH_FRONT_ONLY:
3672 if (strncmp(str, g->search, g->len) == 0)
3673 matched = 1;
3674 break;
3675 case MATCH_MIDDLE_ONLY:
3676 if (strstr(str, g->search))
3677 matched = 1;
3678 break;
3679 case MATCH_END_ONLY:
3680 slen = strlen(str);
3681 if (slen >= g->len &&
3682 memcmp(str + slen - g->len, g->search, g->len) == 0)
3683 matched = 1;
3684 break;
3685 case MATCH_GLOB:
3686 if (glob_match(g->search, str))
3687 matched = 1;
3688 break;
3691 return matched;
3694 static int
3695 enter_record(struct ftrace_hash *hash, struct dyn_ftrace *rec, int clear_filter)
3697 struct ftrace_func_entry *entry;
3698 int ret = 0;
3700 entry = ftrace_lookup_ip(hash, rec->ip);
3701 if (clear_filter) {
3702 /* Do nothing if it doesn't exist */
3703 if (!entry)
3704 return 0;
3706 free_hash_entry(hash, entry);
3707 } else {
3708 /* Do nothing if it exists */
3709 if (entry)
3710 return 0;
3712 ret = add_hash_entry(hash, rec->ip);
3714 return ret;
3717 static int
3718 ftrace_match_record(struct dyn_ftrace *rec, struct ftrace_glob *func_g,
3719 struct ftrace_glob *mod_g, int exclude_mod)
3721 char str[KSYM_SYMBOL_LEN];
3722 char *modname;
3724 kallsyms_lookup(rec->ip, NULL, NULL, &modname, str);
3726 if (mod_g) {
3727 int mod_matches = (modname) ? ftrace_match(modname, mod_g) : 0;
3729 /* blank module name to match all modules */
3730 if (!mod_g->len) {
3731 /* blank module globbing: modname xor exclude_mod */
3732 if (!exclude_mod != !modname)
3733 goto func_match;
3734 return 0;
3738 * exclude_mod is set to trace everything but the given
3739 * module. If it is set and the module matches, then
3740 * return 0. If it is not set, and the module doesn't match
3741 * also return 0. Otherwise, check the function to see if
3742 * that matches.
3744 if (!mod_matches == !exclude_mod)
3745 return 0;
3746 func_match:
3747 /* blank search means to match all funcs in the mod */
3748 if (!func_g->len)
3749 return 1;
3752 return ftrace_match(str, func_g);
3755 static int
3756 match_records(struct ftrace_hash *hash, char *func, int len, char *mod)
3758 struct ftrace_page *pg;
3759 struct dyn_ftrace *rec;
3760 struct ftrace_glob func_g = { .type = MATCH_FULL };
3761 struct ftrace_glob mod_g = { .type = MATCH_FULL };
3762 struct ftrace_glob *mod_match = (mod) ? &mod_g : NULL;
3763 int exclude_mod = 0;
3764 int found = 0;
3765 int ret;
3766 int clear_filter = 0;
3768 if (func) {
3769 func_g.type = filter_parse_regex(func, len, &func_g.search,
3770 &clear_filter);
3771 func_g.len = strlen(func_g.search);
3774 if (mod) {
3775 mod_g.type = filter_parse_regex(mod, strlen(mod),
3776 &mod_g.search, &exclude_mod);
3777 mod_g.len = strlen(mod_g.search);
3780 mutex_lock(&ftrace_lock);
3782 if (unlikely(ftrace_disabled))
3783 goto out_unlock;
3785 do_for_each_ftrace_rec(pg, rec) {
3787 if (rec->flags & FTRACE_FL_DISABLED)
3788 continue;
3790 if (ftrace_match_record(rec, &func_g, mod_match, exclude_mod)) {
3791 ret = enter_record(hash, rec, clear_filter);
3792 if (ret < 0) {
3793 found = ret;
3794 goto out_unlock;
3796 found = 1;
3798 } while_for_each_ftrace_rec();
3799 out_unlock:
3800 mutex_unlock(&ftrace_lock);
3802 return found;
3805 static int
3806 ftrace_match_records(struct ftrace_hash *hash, char *buff, int len)
3808 return match_records(hash, buff, len, NULL);
3811 static void ftrace_ops_update_code(struct ftrace_ops *ops,
3812 struct ftrace_ops_hash *old_hash)
3814 struct ftrace_ops *op;
3816 if (!ftrace_enabled)
3817 return;
3819 if (ops->flags & FTRACE_OPS_FL_ENABLED) {
3820 ftrace_run_modify_code(ops, FTRACE_UPDATE_CALLS, old_hash);
3821 return;
3825 * If this is the shared global_ops filter, then we need to
3826 * check if there is another ops that shares it, is enabled.
3827 * If so, we still need to run the modify code.
3829 if (ops->func_hash != &global_ops.local_hash)
3830 return;
3832 do_for_each_ftrace_op(op, ftrace_ops_list) {
3833 if (op->func_hash == &global_ops.local_hash &&
3834 op->flags & FTRACE_OPS_FL_ENABLED) {
3835 ftrace_run_modify_code(op, FTRACE_UPDATE_CALLS, old_hash);
3836 /* Only need to do this once */
3837 return;
3839 } while_for_each_ftrace_op(op);
3842 static int ftrace_hash_move_and_update_ops(struct ftrace_ops *ops,
3843 struct ftrace_hash **orig_hash,
3844 struct ftrace_hash *hash,
3845 int enable)
3847 struct ftrace_ops_hash old_hash_ops;
3848 struct ftrace_hash *old_hash;
3849 int ret;
3851 old_hash = *orig_hash;
3852 old_hash_ops.filter_hash = ops->func_hash->filter_hash;
3853 old_hash_ops.notrace_hash = ops->func_hash->notrace_hash;
3854 ret = ftrace_hash_move(ops, enable, orig_hash, hash);
3855 if (!ret) {
3856 ftrace_ops_update_code(ops, &old_hash_ops);
3857 free_ftrace_hash_rcu(old_hash);
3859 return ret;
3862 static bool module_exists(const char *module)
3864 /* All modules have the symbol __this_module */
3865 const char this_mod[] = "__this_module";
3866 char modname[MAX_PARAM_PREFIX_LEN + sizeof(this_mod) + 2];
3867 unsigned long val;
3868 int n;
3870 n = snprintf(modname, sizeof(modname), "%s:%s", module, this_mod);
3872 if (n > sizeof(modname) - 1)
3873 return false;
3875 val = module_kallsyms_lookup_name(modname);
3876 return val != 0;
3879 static int cache_mod(struct trace_array *tr,
3880 const char *func, char *module, int enable)
3882 struct ftrace_mod_load *ftrace_mod, *n;
3883 struct list_head *head = enable ? &tr->mod_trace : &tr->mod_notrace;
3884 int ret;
3886 mutex_lock(&ftrace_lock);
3888 /* We do not cache inverse filters */
3889 if (func[0] == '!') {
3890 func++;
3891 ret = -EINVAL;
3893 /* Look to remove this hash */
3894 list_for_each_entry_safe(ftrace_mod, n, head, list) {
3895 if (strcmp(ftrace_mod->module, module) != 0)
3896 continue;
3898 /* no func matches all */
3899 if (strcmp(func, "*") == 0 ||
3900 (ftrace_mod->func &&
3901 strcmp(ftrace_mod->func, func) == 0)) {
3902 ret = 0;
3903 free_ftrace_mod(ftrace_mod);
3904 continue;
3907 goto out;
3910 ret = -EINVAL;
3911 /* We only care about modules that have not been loaded yet */
3912 if (module_exists(module))
3913 goto out;
3915 /* Save this string off, and execute it when the module is loaded */
3916 ret = ftrace_add_mod(tr, func, module, enable);
3917 out:
3918 mutex_unlock(&ftrace_lock);
3920 return ret;
3923 static int
3924 ftrace_set_regex(struct ftrace_ops *ops, unsigned char *buf, int len,
3925 int reset, int enable);
3927 #ifdef CONFIG_MODULES
3928 static void process_mod_list(struct list_head *head, struct ftrace_ops *ops,
3929 char *mod, bool enable)
3931 struct ftrace_mod_load *ftrace_mod, *n;
3932 struct ftrace_hash **orig_hash, *new_hash;
3933 LIST_HEAD(process_mods);
3934 char *func;
3935 int ret;
3937 mutex_lock(&ops->func_hash->regex_lock);
3939 if (enable)
3940 orig_hash = &ops->func_hash->filter_hash;
3941 else
3942 orig_hash = &ops->func_hash->notrace_hash;
3944 new_hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS,
3945 *orig_hash);
3946 if (!new_hash)
3947 goto out; /* warn? */
3949 mutex_lock(&ftrace_lock);
3951 list_for_each_entry_safe(ftrace_mod, n, head, list) {
3953 if (strcmp(ftrace_mod->module, mod) != 0)
3954 continue;
3956 if (ftrace_mod->func)
3957 func = kstrdup(ftrace_mod->func, GFP_KERNEL);
3958 else
3959 func = kstrdup("*", GFP_KERNEL);
3961 if (!func) /* warn? */
3962 continue;
3964 list_del(&ftrace_mod->list);
3965 list_add(&ftrace_mod->list, &process_mods);
3967 /* Use the newly allocated func, as it may be "*" */
3968 kfree(ftrace_mod->func);
3969 ftrace_mod->func = func;
3972 mutex_unlock(&ftrace_lock);
3974 list_for_each_entry_safe(ftrace_mod, n, &process_mods, list) {
3976 func = ftrace_mod->func;
3978 /* Grabs ftrace_lock, which is why we have this extra step */
3979 match_records(new_hash, func, strlen(func), mod);
3980 free_ftrace_mod(ftrace_mod);
3983 if (enable && list_empty(head))
3984 new_hash->flags &= ~FTRACE_HASH_FL_MOD;
3986 mutex_lock(&ftrace_lock);
3988 ret = ftrace_hash_move_and_update_ops(ops, orig_hash,
3989 new_hash, enable);
3990 mutex_unlock(&ftrace_lock);
3992 out:
3993 mutex_unlock(&ops->func_hash->regex_lock);
3995 free_ftrace_hash(new_hash);
3998 static void process_cached_mods(const char *mod_name)
4000 struct trace_array *tr;
4001 char *mod;
4003 mod = kstrdup(mod_name, GFP_KERNEL);
4004 if (!mod)
4005 return;
4007 mutex_lock(&trace_types_lock);
4008 list_for_each_entry(tr, &ftrace_trace_arrays, list) {
4009 if (!list_empty(&tr->mod_trace))
4010 process_mod_list(&tr->mod_trace, tr->ops, mod, true);
4011 if (!list_empty(&tr->mod_notrace))
4012 process_mod_list(&tr->mod_notrace, tr->ops, mod, false);
4014 mutex_unlock(&trace_types_lock);
4016 kfree(mod);
4018 #endif
4021 * We register the module command as a template to show others how
4022 * to register the a command as well.
4025 static int
4026 ftrace_mod_callback(struct trace_array *tr, struct ftrace_hash *hash,
4027 char *func_orig, char *cmd, char *module, int enable)
4029 char *func;
4030 int ret;
4032 /* match_records() modifies func, and we need the original */
4033 func = kstrdup(func_orig, GFP_KERNEL);
4034 if (!func)
4035 return -ENOMEM;
4038 * cmd == 'mod' because we only registered this func
4039 * for the 'mod' ftrace_func_command.
4040 * But if you register one func with multiple commands,
4041 * you can tell which command was used by the cmd
4042 * parameter.
4044 ret = match_records(hash, func, strlen(func), module);
4045 kfree(func);
4047 if (!ret)
4048 return cache_mod(tr, func_orig, module, enable);
4049 if (ret < 0)
4050 return ret;
4051 return 0;
4054 static struct ftrace_func_command ftrace_mod_cmd = {
4055 .name = "mod",
4056 .func = ftrace_mod_callback,
4059 static int __init ftrace_mod_cmd_init(void)
4061 return register_ftrace_command(&ftrace_mod_cmd);
4063 core_initcall(ftrace_mod_cmd_init);
4065 static void function_trace_probe_call(unsigned long ip, unsigned long parent_ip,
4066 struct ftrace_ops *op, struct pt_regs *pt_regs)
4068 struct ftrace_probe_ops *probe_ops;
4069 struct ftrace_func_probe *probe;
4071 probe = container_of(op, struct ftrace_func_probe, ops);
4072 probe_ops = probe->probe_ops;
4075 * Disable preemption for these calls to prevent a RCU grace
4076 * period. This syncs the hash iteration and freeing of items
4077 * on the hash. rcu_read_lock is too dangerous here.
4079 preempt_disable_notrace();
4080 probe_ops->func(ip, parent_ip, probe->tr, probe_ops, probe->data);
4081 preempt_enable_notrace();
4084 struct ftrace_func_map {
4085 struct ftrace_func_entry entry;
4086 void *data;
4089 struct ftrace_func_mapper {
4090 struct ftrace_hash hash;
4094 * allocate_ftrace_func_mapper - allocate a new ftrace_func_mapper
4096 * Returns a ftrace_func_mapper descriptor that can be used to map ips to data.
4098 struct ftrace_func_mapper *allocate_ftrace_func_mapper(void)
4100 struct ftrace_hash *hash;
4103 * The mapper is simply a ftrace_hash, but since the entries
4104 * in the hash are not ftrace_func_entry type, we define it
4105 * as a separate structure.
4107 hash = alloc_ftrace_hash(FTRACE_HASH_DEFAULT_BITS);
4108 return (struct ftrace_func_mapper *)hash;
4112 * ftrace_func_mapper_find_ip - Find some data mapped to an ip
4113 * @mapper: The mapper that has the ip maps
4114 * @ip: the instruction pointer to find the data for
4116 * Returns the data mapped to @ip if found otherwise NULL. The return
4117 * is actually the address of the mapper data pointer. The address is
4118 * returned for use cases where the data is no bigger than a long, and
4119 * the user can use the data pointer as its data instead of having to
4120 * allocate more memory for the reference.
4122 void **ftrace_func_mapper_find_ip(struct ftrace_func_mapper *mapper,
4123 unsigned long ip)
4125 struct ftrace_func_entry *entry;
4126 struct ftrace_func_map *map;
4128 entry = ftrace_lookup_ip(&mapper->hash, ip);
4129 if (!entry)
4130 return NULL;
4132 map = (struct ftrace_func_map *)entry;
4133 return &map->data;
4137 * ftrace_func_mapper_add_ip - Map some data to an ip
4138 * @mapper: The mapper that has the ip maps
4139 * @ip: The instruction pointer address to map @data to
4140 * @data: The data to map to @ip
4142 * Returns 0 on succes otherwise an error.
4144 int ftrace_func_mapper_add_ip(struct ftrace_func_mapper *mapper,
4145 unsigned long ip, void *data)
4147 struct ftrace_func_entry *entry;
4148 struct ftrace_func_map *map;
4150 entry = ftrace_lookup_ip(&mapper->hash, ip);
4151 if (entry)
4152 return -EBUSY;
4154 map = kmalloc(sizeof(*map), GFP_KERNEL);
4155 if (!map)
4156 return -ENOMEM;
4158 map->entry.ip = ip;
4159 map->data = data;
4161 __add_hash_entry(&mapper->hash, &map->entry);
4163 return 0;
4167 * ftrace_func_mapper_remove_ip - Remove an ip from the mapping
4168 * @mapper: The mapper that has the ip maps
4169 * @ip: The instruction pointer address to remove the data from
4171 * Returns the data if it is found, otherwise NULL.
4172 * Note, if the data pointer is used as the data itself, (see
4173 * ftrace_func_mapper_find_ip(), then the return value may be meaningless,
4174 * if the data pointer was set to zero.
4176 void *ftrace_func_mapper_remove_ip(struct ftrace_func_mapper *mapper,
4177 unsigned long ip)
4179 struct ftrace_func_entry *entry;
4180 struct ftrace_func_map *map;
4181 void *data;
4183 entry = ftrace_lookup_ip(&mapper->hash, ip);
4184 if (!entry)
4185 return NULL;
4187 map = (struct ftrace_func_map *)entry;
4188 data = map->data;
4190 remove_hash_entry(&mapper->hash, entry);
4191 kfree(entry);
4193 return data;
4197 * free_ftrace_func_mapper - free a mapping of ips and data
4198 * @mapper: The mapper that has the ip maps
4199 * @free_func: A function to be called on each data item.
4201 * This is used to free the function mapper. The @free_func is optional
4202 * and can be used if the data needs to be freed as well.
4204 void free_ftrace_func_mapper(struct ftrace_func_mapper *mapper,
4205 ftrace_mapper_func free_func)
4207 struct ftrace_func_entry *entry;
4208 struct ftrace_func_map *map;
4209 struct hlist_head *hhd;
4210 int size = 1 << mapper->hash.size_bits;
4211 int i;
4213 if (free_func && mapper->hash.count) {
4214 for (i = 0; i < size; i++) {
4215 hhd = &mapper->hash.buckets[i];
4216 hlist_for_each_entry(entry, hhd, hlist) {
4217 map = (struct ftrace_func_map *)entry;
4218 free_func(map);
4222 free_ftrace_hash(&mapper->hash);
4225 static void release_probe(struct ftrace_func_probe *probe)
4227 struct ftrace_probe_ops *probe_ops;
4229 mutex_lock(&ftrace_lock);
4231 WARN_ON(probe->ref <= 0);
4233 /* Subtract the ref that was used to protect this instance */
4234 probe->ref--;
4236 if (!probe->ref) {
4237 probe_ops = probe->probe_ops;
4239 * Sending zero as ip tells probe_ops to free
4240 * the probe->data itself
4242 if (probe_ops->free)
4243 probe_ops->free(probe_ops, probe->tr, 0, probe->data);
4244 list_del(&probe->list);
4245 kfree(probe);
4247 mutex_unlock(&ftrace_lock);
4250 static void acquire_probe_locked(struct ftrace_func_probe *probe)
4253 * Add one ref to keep it from being freed when releasing the
4254 * ftrace_lock mutex.
4256 probe->ref++;
4260 register_ftrace_function_probe(char *glob, struct trace_array *tr,
4261 struct ftrace_probe_ops *probe_ops,
4262 void *data)
4264 struct ftrace_func_entry *entry;
4265 struct ftrace_func_probe *probe;
4266 struct ftrace_hash **orig_hash;
4267 struct ftrace_hash *old_hash;
4268 struct ftrace_hash *hash;
4269 int count = 0;
4270 int size;
4271 int ret;
4272 int i;
4274 if (WARN_ON(!tr))
4275 return -EINVAL;
4277 /* We do not support '!' for function probes */
4278 if (WARN_ON(glob[0] == '!'))
4279 return -EINVAL;
4282 mutex_lock(&ftrace_lock);
4283 /* Check if the probe_ops is already registered */
4284 list_for_each_entry(probe, &tr->func_probes, list) {
4285 if (probe->probe_ops == probe_ops)
4286 break;
4288 if (&probe->list == &tr->func_probes) {
4289 probe = kzalloc(sizeof(*probe), GFP_KERNEL);
4290 if (!probe) {
4291 mutex_unlock(&ftrace_lock);
4292 return -ENOMEM;
4294 probe->probe_ops = probe_ops;
4295 probe->ops.func = function_trace_probe_call;
4296 probe->tr = tr;
4297 ftrace_ops_init(&probe->ops);
4298 list_add(&probe->list, &tr->func_probes);
4301 acquire_probe_locked(probe);
4303 mutex_unlock(&ftrace_lock);
4305 mutex_lock(&probe->ops.func_hash->regex_lock);
4307 orig_hash = &probe->ops.func_hash->filter_hash;
4308 old_hash = *orig_hash;
4309 hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, old_hash);
4311 ret = ftrace_match_records(hash, glob, strlen(glob));
4313 /* Nothing found? */
4314 if (!ret)
4315 ret = -EINVAL;
4317 if (ret < 0)
4318 goto out;
4320 size = 1 << hash->size_bits;
4321 for (i = 0; i < size; i++) {
4322 hlist_for_each_entry(entry, &hash->buckets[i], hlist) {
4323 if (ftrace_lookup_ip(old_hash, entry->ip))
4324 continue;
4326 * The caller might want to do something special
4327 * for each function we find. We call the callback
4328 * to give the caller an opportunity to do so.
4330 if (probe_ops->init) {
4331 ret = probe_ops->init(probe_ops, tr,
4332 entry->ip, data,
4333 &probe->data);
4334 if (ret < 0) {
4335 if (probe_ops->free && count)
4336 probe_ops->free(probe_ops, tr,
4337 0, probe->data);
4338 probe->data = NULL;
4339 goto out;
4342 count++;
4346 mutex_lock(&ftrace_lock);
4348 if (!count) {
4349 /* Nothing was added? */
4350 ret = -EINVAL;
4351 goto out_unlock;
4354 ret = ftrace_hash_move_and_update_ops(&probe->ops, orig_hash,
4355 hash, 1);
4356 if (ret < 0)
4357 goto err_unlock;
4359 /* One ref for each new function traced */
4360 probe->ref += count;
4362 if (!(probe->ops.flags & FTRACE_OPS_FL_ENABLED))
4363 ret = ftrace_startup(&probe->ops, 0);
4365 out_unlock:
4366 mutex_unlock(&ftrace_lock);
4368 if (!ret)
4369 ret = count;
4370 out:
4371 mutex_unlock(&probe->ops.func_hash->regex_lock);
4372 free_ftrace_hash(hash);
4374 release_probe(probe);
4376 return ret;
4378 err_unlock:
4379 if (!probe_ops->free || !count)
4380 goto out_unlock;
4382 /* Failed to do the move, need to call the free functions */
4383 for (i = 0; i < size; i++) {
4384 hlist_for_each_entry(entry, &hash->buckets[i], hlist) {
4385 if (ftrace_lookup_ip(old_hash, entry->ip))
4386 continue;
4387 probe_ops->free(probe_ops, tr, entry->ip, probe->data);
4390 goto out_unlock;
4394 unregister_ftrace_function_probe_func(char *glob, struct trace_array *tr,
4395 struct ftrace_probe_ops *probe_ops)
4397 struct ftrace_ops_hash old_hash_ops;
4398 struct ftrace_func_entry *entry;
4399 struct ftrace_func_probe *probe;
4400 struct ftrace_glob func_g;
4401 struct ftrace_hash **orig_hash;
4402 struct ftrace_hash *old_hash;
4403 struct ftrace_hash *hash = NULL;
4404 struct hlist_node *tmp;
4405 struct hlist_head hhd;
4406 char str[KSYM_SYMBOL_LEN];
4407 int count = 0;
4408 int i, ret = -ENODEV;
4409 int size;
4411 if (!glob || !strlen(glob) || !strcmp(glob, "*"))
4412 func_g.search = NULL;
4413 else {
4414 int not;
4416 func_g.type = filter_parse_regex(glob, strlen(glob),
4417 &func_g.search, &not);
4418 func_g.len = strlen(func_g.search);
4420 /* we do not support '!' for function probes */
4421 if (WARN_ON(not))
4422 return -EINVAL;
4425 mutex_lock(&ftrace_lock);
4426 /* Check if the probe_ops is already registered */
4427 list_for_each_entry(probe, &tr->func_probes, list) {
4428 if (probe->probe_ops == probe_ops)
4429 break;
4431 if (&probe->list == &tr->func_probes)
4432 goto err_unlock_ftrace;
4434 ret = -EINVAL;
4435 if (!(probe->ops.flags & FTRACE_OPS_FL_INITIALIZED))
4436 goto err_unlock_ftrace;
4438 acquire_probe_locked(probe);
4440 mutex_unlock(&ftrace_lock);
4442 mutex_lock(&probe->ops.func_hash->regex_lock);
4444 orig_hash = &probe->ops.func_hash->filter_hash;
4445 old_hash = *orig_hash;
4447 if (ftrace_hash_empty(old_hash))
4448 goto out_unlock;
4450 old_hash_ops.filter_hash = old_hash;
4451 /* Probes only have filters */
4452 old_hash_ops.notrace_hash = NULL;
4454 ret = -ENOMEM;
4455 hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, old_hash);
4456 if (!hash)
4457 goto out_unlock;
4459 INIT_HLIST_HEAD(&hhd);
4461 size = 1 << hash->size_bits;
4462 for (i = 0; i < size; i++) {
4463 hlist_for_each_entry_safe(entry, tmp, &hash->buckets[i], hlist) {
4465 if (func_g.search) {
4466 kallsyms_lookup(entry->ip, NULL, NULL,
4467 NULL, str);
4468 if (!ftrace_match(str, &func_g))
4469 continue;
4471 count++;
4472 remove_hash_entry(hash, entry);
4473 hlist_add_head(&entry->hlist, &hhd);
4477 /* Nothing found? */
4478 if (!count) {
4479 ret = -EINVAL;
4480 goto out_unlock;
4483 mutex_lock(&ftrace_lock);
4485 WARN_ON(probe->ref < count);
4487 probe->ref -= count;
4489 if (ftrace_hash_empty(hash))
4490 ftrace_shutdown(&probe->ops, 0);
4492 ret = ftrace_hash_move_and_update_ops(&probe->ops, orig_hash,
4493 hash, 1);
4495 /* still need to update the function call sites */
4496 if (ftrace_enabled && !ftrace_hash_empty(hash))
4497 ftrace_run_modify_code(&probe->ops, FTRACE_UPDATE_CALLS,
4498 &old_hash_ops);
4499 synchronize_sched();
4501 hlist_for_each_entry_safe(entry, tmp, &hhd, hlist) {
4502 hlist_del(&entry->hlist);
4503 if (probe_ops->free)
4504 probe_ops->free(probe_ops, tr, entry->ip, probe->data);
4505 kfree(entry);
4507 mutex_unlock(&ftrace_lock);
4509 out_unlock:
4510 mutex_unlock(&probe->ops.func_hash->regex_lock);
4511 free_ftrace_hash(hash);
4513 release_probe(probe);
4515 return ret;
4517 err_unlock_ftrace:
4518 mutex_unlock(&ftrace_lock);
4519 return ret;
4522 void clear_ftrace_function_probes(struct trace_array *tr)
4524 struct ftrace_func_probe *probe, *n;
4526 list_for_each_entry_safe(probe, n, &tr->func_probes, list)
4527 unregister_ftrace_function_probe_func(NULL, tr, probe->probe_ops);
4530 static LIST_HEAD(ftrace_commands);
4531 static DEFINE_MUTEX(ftrace_cmd_mutex);
4534 * Currently we only register ftrace commands from __init, so mark this
4535 * __init too.
4537 __init int register_ftrace_command(struct ftrace_func_command *cmd)
4539 struct ftrace_func_command *p;
4540 int ret = 0;
4542 mutex_lock(&ftrace_cmd_mutex);
4543 list_for_each_entry(p, &ftrace_commands, list) {
4544 if (strcmp(cmd->name, p->name) == 0) {
4545 ret = -EBUSY;
4546 goto out_unlock;
4549 list_add(&cmd->list, &ftrace_commands);
4550 out_unlock:
4551 mutex_unlock(&ftrace_cmd_mutex);
4553 return ret;
4557 * Currently we only unregister ftrace commands from __init, so mark
4558 * this __init too.
4560 __init int unregister_ftrace_command(struct ftrace_func_command *cmd)
4562 struct ftrace_func_command *p, *n;
4563 int ret = -ENODEV;
4565 mutex_lock(&ftrace_cmd_mutex);
4566 list_for_each_entry_safe(p, n, &ftrace_commands, list) {
4567 if (strcmp(cmd->name, p->name) == 0) {
4568 ret = 0;
4569 list_del_init(&p->list);
4570 goto out_unlock;
4573 out_unlock:
4574 mutex_unlock(&ftrace_cmd_mutex);
4576 return ret;
4579 static int ftrace_process_regex(struct ftrace_iterator *iter,
4580 char *buff, int len, int enable)
4582 struct ftrace_hash *hash = iter->hash;
4583 struct trace_array *tr = iter->ops->private;
4584 char *func, *command, *next = buff;
4585 struct ftrace_func_command *p;
4586 int ret = -EINVAL;
4588 func = strsep(&next, ":");
4590 if (!next) {
4591 ret = ftrace_match_records(hash, func, len);
4592 if (!ret)
4593 ret = -EINVAL;
4594 if (ret < 0)
4595 return ret;
4596 return 0;
4599 /* command found */
4601 command = strsep(&next, ":");
4603 mutex_lock(&ftrace_cmd_mutex);
4604 list_for_each_entry(p, &ftrace_commands, list) {
4605 if (strcmp(p->name, command) == 0) {
4606 ret = p->func(tr, hash, func, command, next, enable);
4607 goto out_unlock;
4610 out_unlock:
4611 mutex_unlock(&ftrace_cmd_mutex);
4613 return ret;
4616 static ssize_t
4617 ftrace_regex_write(struct file *file, const char __user *ubuf,
4618 size_t cnt, loff_t *ppos, int enable)
4620 struct ftrace_iterator *iter;
4621 struct trace_parser *parser;
4622 ssize_t ret, read;
4624 if (!cnt)
4625 return 0;
4627 if (file->f_mode & FMODE_READ) {
4628 struct seq_file *m = file->private_data;
4629 iter = m->private;
4630 } else
4631 iter = file->private_data;
4633 if (unlikely(ftrace_disabled))
4634 return -ENODEV;
4636 /* iter->hash is a local copy, so we don't need regex_lock */
4638 parser = &iter->parser;
4639 read = trace_get_user(parser, ubuf, cnt, ppos);
4641 if (read >= 0 && trace_parser_loaded(parser) &&
4642 !trace_parser_cont(parser)) {
4643 ret = ftrace_process_regex(iter, parser->buffer,
4644 parser->idx, enable);
4645 trace_parser_clear(parser);
4646 if (ret < 0)
4647 goto out;
4650 ret = read;
4651 out:
4652 return ret;
4655 ssize_t
4656 ftrace_filter_write(struct file *file, const char __user *ubuf,
4657 size_t cnt, loff_t *ppos)
4659 return ftrace_regex_write(file, ubuf, cnt, ppos, 1);
4662 ssize_t
4663 ftrace_notrace_write(struct file *file, const char __user *ubuf,
4664 size_t cnt, loff_t *ppos)
4666 return ftrace_regex_write(file, ubuf, cnt, ppos, 0);
4669 static int
4670 ftrace_match_addr(struct ftrace_hash *hash, unsigned long ip, int remove)
4672 struct ftrace_func_entry *entry;
4674 if (!ftrace_location(ip))
4675 return -EINVAL;
4677 if (remove) {
4678 entry = ftrace_lookup_ip(hash, ip);
4679 if (!entry)
4680 return -ENOENT;
4681 free_hash_entry(hash, entry);
4682 return 0;
4685 return add_hash_entry(hash, ip);
4688 static int
4689 ftrace_set_hash(struct ftrace_ops *ops, unsigned char *buf, int len,
4690 unsigned long ip, int remove, int reset, int enable)
4692 struct ftrace_hash **orig_hash;
4693 struct ftrace_hash *hash;
4694 int ret;
4696 if (unlikely(ftrace_disabled))
4697 return -ENODEV;
4699 mutex_lock(&ops->func_hash->regex_lock);
4701 if (enable)
4702 orig_hash = &ops->func_hash->filter_hash;
4703 else
4704 orig_hash = &ops->func_hash->notrace_hash;
4706 if (reset)
4707 hash = alloc_ftrace_hash(FTRACE_HASH_DEFAULT_BITS);
4708 else
4709 hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, *orig_hash);
4711 if (!hash) {
4712 ret = -ENOMEM;
4713 goto out_regex_unlock;
4716 if (buf && !ftrace_match_records(hash, buf, len)) {
4717 ret = -EINVAL;
4718 goto out_regex_unlock;
4720 if (ip) {
4721 ret = ftrace_match_addr(hash, ip, remove);
4722 if (ret < 0)
4723 goto out_regex_unlock;
4726 mutex_lock(&ftrace_lock);
4727 ret = ftrace_hash_move_and_update_ops(ops, orig_hash, hash, enable);
4728 mutex_unlock(&ftrace_lock);
4730 out_regex_unlock:
4731 mutex_unlock(&ops->func_hash->regex_lock);
4733 free_ftrace_hash(hash);
4734 return ret;
4737 static int
4738 ftrace_set_addr(struct ftrace_ops *ops, unsigned long ip, int remove,
4739 int reset, int enable)
4741 return ftrace_set_hash(ops, 0, 0, ip, remove, reset, enable);
4745 * ftrace_set_filter_ip - set a function to filter on in ftrace by address
4746 * @ops - the ops to set the filter with
4747 * @ip - the address to add to or remove from the filter.
4748 * @remove - non zero to remove the ip from the filter
4749 * @reset - non zero to reset all filters before applying this filter.
4751 * Filters denote which functions should be enabled when tracing is enabled
4752 * If @ip is NULL, it failes to update filter.
4754 int ftrace_set_filter_ip(struct ftrace_ops *ops, unsigned long ip,
4755 int remove, int reset)
4757 ftrace_ops_init(ops);
4758 return ftrace_set_addr(ops, ip, remove, reset, 1);
4760 EXPORT_SYMBOL_GPL(ftrace_set_filter_ip);
4763 * ftrace_ops_set_global_filter - setup ops to use global filters
4764 * @ops - the ops which will use the global filters
4766 * ftrace users who need global function trace filtering should call this.
4767 * It can set the global filter only if ops were not initialized before.
4769 void ftrace_ops_set_global_filter(struct ftrace_ops *ops)
4771 if (ops->flags & FTRACE_OPS_FL_INITIALIZED)
4772 return;
4774 ftrace_ops_init(ops);
4775 ops->func_hash = &global_ops.local_hash;
4777 EXPORT_SYMBOL_GPL(ftrace_ops_set_global_filter);
4779 static int
4780 ftrace_set_regex(struct ftrace_ops *ops, unsigned char *buf, int len,
4781 int reset, int enable)
4783 return ftrace_set_hash(ops, buf, len, 0, 0, reset, enable);
4787 * ftrace_set_filter - set a function to filter on in ftrace
4788 * @ops - the ops to set the filter with
4789 * @buf - the string that holds the function filter text.
4790 * @len - the length of the string.
4791 * @reset - non zero to reset all filters before applying this filter.
4793 * Filters denote which functions should be enabled when tracing is enabled.
4794 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
4796 int ftrace_set_filter(struct ftrace_ops *ops, unsigned char *buf,
4797 int len, int reset)
4799 ftrace_ops_init(ops);
4800 return ftrace_set_regex(ops, buf, len, reset, 1);
4802 EXPORT_SYMBOL_GPL(ftrace_set_filter);
4805 * ftrace_set_notrace - set a function to not trace in ftrace
4806 * @ops - the ops to set the notrace filter with
4807 * @buf - the string that holds the function notrace text.
4808 * @len - the length of the string.
4809 * @reset - non zero to reset all filters before applying this filter.
4811 * Notrace Filters denote which functions should not be enabled when tracing
4812 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
4813 * for tracing.
4815 int ftrace_set_notrace(struct ftrace_ops *ops, unsigned char *buf,
4816 int len, int reset)
4818 ftrace_ops_init(ops);
4819 return ftrace_set_regex(ops, buf, len, reset, 0);
4821 EXPORT_SYMBOL_GPL(ftrace_set_notrace);
4823 * ftrace_set_global_filter - set a function to filter on with global tracers
4824 * @buf - the string that holds the function filter text.
4825 * @len - the length of the string.
4826 * @reset - non zero to reset all filters before applying this filter.
4828 * Filters denote which functions should be enabled when tracing is enabled.
4829 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
4831 void ftrace_set_global_filter(unsigned char *buf, int len, int reset)
4833 ftrace_set_regex(&global_ops, buf, len, reset, 1);
4835 EXPORT_SYMBOL_GPL(ftrace_set_global_filter);
4838 * ftrace_set_global_notrace - set a function to not trace with global tracers
4839 * @buf - the string that holds the function notrace text.
4840 * @len - the length of the string.
4841 * @reset - non zero to reset all filters before applying this filter.
4843 * Notrace Filters denote which functions should not be enabled when tracing
4844 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
4845 * for tracing.
4847 void ftrace_set_global_notrace(unsigned char *buf, int len, int reset)
4849 ftrace_set_regex(&global_ops, buf, len, reset, 0);
4851 EXPORT_SYMBOL_GPL(ftrace_set_global_notrace);
4854 * command line interface to allow users to set filters on boot up.
4856 #define FTRACE_FILTER_SIZE COMMAND_LINE_SIZE
4857 static char ftrace_notrace_buf[FTRACE_FILTER_SIZE] __initdata;
4858 static char ftrace_filter_buf[FTRACE_FILTER_SIZE] __initdata;
4860 /* Used by function selftest to not test if filter is set */
4861 bool ftrace_filter_param __initdata;
4863 static int __init set_ftrace_notrace(char *str)
4865 ftrace_filter_param = true;
4866 strlcpy(ftrace_notrace_buf, str, FTRACE_FILTER_SIZE);
4867 return 1;
4869 __setup("ftrace_notrace=", set_ftrace_notrace);
4871 static int __init set_ftrace_filter(char *str)
4873 ftrace_filter_param = true;
4874 strlcpy(ftrace_filter_buf, str, FTRACE_FILTER_SIZE);
4875 return 1;
4877 __setup("ftrace_filter=", set_ftrace_filter);
4879 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
4880 static char ftrace_graph_buf[FTRACE_FILTER_SIZE] __initdata;
4881 static char ftrace_graph_notrace_buf[FTRACE_FILTER_SIZE] __initdata;
4882 static int ftrace_graph_set_hash(struct ftrace_hash *hash, char *buffer);
4884 static int __init set_graph_function(char *str)
4886 strlcpy(ftrace_graph_buf, str, FTRACE_FILTER_SIZE);
4887 return 1;
4889 __setup("ftrace_graph_filter=", set_graph_function);
4891 static int __init set_graph_notrace_function(char *str)
4893 strlcpy(ftrace_graph_notrace_buf, str, FTRACE_FILTER_SIZE);
4894 return 1;
4896 __setup("ftrace_graph_notrace=", set_graph_notrace_function);
4898 static int __init set_graph_max_depth_function(char *str)
4900 if (!str)
4901 return 0;
4902 fgraph_max_depth = simple_strtoul(str, NULL, 0);
4903 return 1;
4905 __setup("ftrace_graph_max_depth=", set_graph_max_depth_function);
4907 static void __init set_ftrace_early_graph(char *buf, int enable)
4909 int ret;
4910 char *func;
4911 struct ftrace_hash *hash;
4913 hash = alloc_ftrace_hash(FTRACE_HASH_DEFAULT_BITS);
4914 if (WARN_ON(!hash))
4915 return;
4917 while (buf) {
4918 func = strsep(&buf, ",");
4919 /* we allow only one expression at a time */
4920 ret = ftrace_graph_set_hash(hash, func);
4921 if (ret)
4922 printk(KERN_DEBUG "ftrace: function %s not "
4923 "traceable\n", func);
4926 if (enable)
4927 ftrace_graph_hash = hash;
4928 else
4929 ftrace_graph_notrace_hash = hash;
4931 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
4933 void __init
4934 ftrace_set_early_filter(struct ftrace_ops *ops, char *buf, int enable)
4936 char *func;
4938 ftrace_ops_init(ops);
4940 while (buf) {
4941 func = strsep(&buf, ",");
4942 ftrace_set_regex(ops, func, strlen(func), 0, enable);
4946 static void __init set_ftrace_early_filters(void)
4948 if (ftrace_filter_buf[0])
4949 ftrace_set_early_filter(&global_ops, ftrace_filter_buf, 1);
4950 if (ftrace_notrace_buf[0])
4951 ftrace_set_early_filter(&global_ops, ftrace_notrace_buf, 0);
4952 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
4953 if (ftrace_graph_buf[0])
4954 set_ftrace_early_graph(ftrace_graph_buf, 1);
4955 if (ftrace_graph_notrace_buf[0])
4956 set_ftrace_early_graph(ftrace_graph_notrace_buf, 0);
4957 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
4960 int ftrace_regex_release(struct inode *inode, struct file *file)
4962 struct seq_file *m = (struct seq_file *)file->private_data;
4963 struct ftrace_iterator *iter;
4964 struct ftrace_hash **orig_hash;
4965 struct trace_parser *parser;
4966 int filter_hash;
4967 int ret;
4969 if (file->f_mode & FMODE_READ) {
4970 iter = m->private;
4971 seq_release(inode, file);
4972 } else
4973 iter = file->private_data;
4975 parser = &iter->parser;
4976 if (trace_parser_loaded(parser)) {
4977 ftrace_match_records(iter->hash, parser->buffer, parser->idx);
4980 trace_parser_put(parser);
4982 mutex_lock(&iter->ops->func_hash->regex_lock);
4984 if (file->f_mode & FMODE_WRITE) {
4985 filter_hash = !!(iter->flags & FTRACE_ITER_FILTER);
4987 if (filter_hash) {
4988 orig_hash = &iter->ops->func_hash->filter_hash;
4989 if (iter->tr && !list_empty(&iter->tr->mod_trace))
4990 iter->hash->flags |= FTRACE_HASH_FL_MOD;
4991 } else
4992 orig_hash = &iter->ops->func_hash->notrace_hash;
4994 mutex_lock(&ftrace_lock);
4995 ret = ftrace_hash_move_and_update_ops(iter->ops, orig_hash,
4996 iter->hash, filter_hash);
4997 mutex_unlock(&ftrace_lock);
4998 } else {
4999 /* For read only, the hash is the ops hash */
5000 iter->hash = NULL;
5003 mutex_unlock(&iter->ops->func_hash->regex_lock);
5004 free_ftrace_hash(iter->hash);
5005 kfree(iter);
5007 return 0;
5010 static const struct file_operations ftrace_avail_fops = {
5011 .open = ftrace_avail_open,
5012 .read = seq_read,
5013 .llseek = seq_lseek,
5014 .release = seq_release_private,
5017 static const struct file_operations ftrace_enabled_fops = {
5018 .open = ftrace_enabled_open,
5019 .read = seq_read,
5020 .llseek = seq_lseek,
5021 .release = seq_release_private,
5024 static const struct file_operations ftrace_filter_fops = {
5025 .open = ftrace_filter_open,
5026 .read = seq_read,
5027 .write = ftrace_filter_write,
5028 .llseek = tracing_lseek,
5029 .release = ftrace_regex_release,
5032 static const struct file_operations ftrace_notrace_fops = {
5033 .open = ftrace_notrace_open,
5034 .read = seq_read,
5035 .write = ftrace_notrace_write,
5036 .llseek = tracing_lseek,
5037 .release = ftrace_regex_release,
5040 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
5042 static DEFINE_MUTEX(graph_lock);
5044 struct ftrace_hash *ftrace_graph_hash = EMPTY_HASH;
5045 struct ftrace_hash *ftrace_graph_notrace_hash = EMPTY_HASH;
5047 enum graph_filter_type {
5048 GRAPH_FILTER_NOTRACE = 0,
5049 GRAPH_FILTER_FUNCTION,
5052 #define FTRACE_GRAPH_EMPTY ((void *)1)
5054 struct ftrace_graph_data {
5055 struct ftrace_hash *hash;
5056 struct ftrace_func_entry *entry;
5057 int idx; /* for hash table iteration */
5058 enum graph_filter_type type;
5059 struct ftrace_hash *new_hash;
5060 const struct seq_operations *seq_ops;
5061 struct trace_parser parser;
5064 static void *
5065 __g_next(struct seq_file *m, loff_t *pos)
5067 struct ftrace_graph_data *fgd = m->private;
5068 struct ftrace_func_entry *entry = fgd->entry;
5069 struct hlist_head *head;
5070 int i, idx = fgd->idx;
5072 if (*pos >= fgd->hash->count)
5073 return NULL;
5075 if (entry) {
5076 hlist_for_each_entry_continue(entry, hlist) {
5077 fgd->entry = entry;
5078 return entry;
5081 idx++;
5084 for (i = idx; i < 1 << fgd->hash->size_bits; i++) {
5085 head = &fgd->hash->buckets[i];
5086 hlist_for_each_entry(entry, head, hlist) {
5087 fgd->entry = entry;
5088 fgd->idx = i;
5089 return entry;
5092 return NULL;
5095 static void *
5096 g_next(struct seq_file *m, void *v, loff_t *pos)
5098 (*pos)++;
5099 return __g_next(m, pos);
5102 static void *g_start(struct seq_file *m, loff_t *pos)
5104 struct ftrace_graph_data *fgd = m->private;
5106 mutex_lock(&graph_lock);
5108 if (fgd->type == GRAPH_FILTER_FUNCTION)
5109 fgd->hash = rcu_dereference_protected(ftrace_graph_hash,
5110 lockdep_is_held(&graph_lock));
5111 else
5112 fgd->hash = rcu_dereference_protected(ftrace_graph_notrace_hash,
5113 lockdep_is_held(&graph_lock));
5115 /* Nothing, tell g_show to print all functions are enabled */
5116 if (ftrace_hash_empty(fgd->hash) && !*pos)
5117 return FTRACE_GRAPH_EMPTY;
5119 fgd->idx = 0;
5120 fgd->entry = NULL;
5121 return __g_next(m, pos);
5124 static void g_stop(struct seq_file *m, void *p)
5126 mutex_unlock(&graph_lock);
5129 static int g_show(struct seq_file *m, void *v)
5131 struct ftrace_func_entry *entry = v;
5133 if (!entry)
5134 return 0;
5136 if (entry == FTRACE_GRAPH_EMPTY) {
5137 struct ftrace_graph_data *fgd = m->private;
5139 if (fgd->type == GRAPH_FILTER_FUNCTION)
5140 seq_puts(m, "#### all functions enabled ####\n");
5141 else
5142 seq_puts(m, "#### no functions disabled ####\n");
5143 return 0;
5146 seq_printf(m, "%ps\n", (void *)entry->ip);
5148 return 0;
5151 static const struct seq_operations ftrace_graph_seq_ops = {
5152 .start = g_start,
5153 .next = g_next,
5154 .stop = g_stop,
5155 .show = g_show,
5158 static int
5159 __ftrace_graph_open(struct inode *inode, struct file *file,
5160 struct ftrace_graph_data *fgd)
5162 int ret = 0;
5163 struct ftrace_hash *new_hash = NULL;
5165 if (file->f_mode & FMODE_WRITE) {
5166 const int size_bits = FTRACE_HASH_DEFAULT_BITS;
5168 if (trace_parser_get_init(&fgd->parser, FTRACE_BUFF_MAX))
5169 return -ENOMEM;
5171 if (file->f_flags & O_TRUNC)
5172 new_hash = alloc_ftrace_hash(size_bits);
5173 else
5174 new_hash = alloc_and_copy_ftrace_hash(size_bits,
5175 fgd->hash);
5176 if (!new_hash) {
5177 ret = -ENOMEM;
5178 goto out;
5182 if (file->f_mode & FMODE_READ) {
5183 ret = seq_open(file, &ftrace_graph_seq_ops);
5184 if (!ret) {
5185 struct seq_file *m = file->private_data;
5186 m->private = fgd;
5187 } else {
5188 /* Failed */
5189 free_ftrace_hash(new_hash);
5190 new_hash = NULL;
5192 } else
5193 file->private_data = fgd;
5195 out:
5196 if (ret < 0 && file->f_mode & FMODE_WRITE)
5197 trace_parser_put(&fgd->parser);
5199 fgd->new_hash = new_hash;
5202 * All uses of fgd->hash must be taken with the graph_lock
5203 * held. The graph_lock is going to be released, so force
5204 * fgd->hash to be reinitialized when it is taken again.
5206 fgd->hash = NULL;
5208 return ret;
5211 static int
5212 ftrace_graph_open(struct inode *inode, struct file *file)
5214 struct ftrace_graph_data *fgd;
5215 int ret;
5217 if (unlikely(ftrace_disabled))
5218 return -ENODEV;
5220 fgd = kmalloc(sizeof(*fgd), GFP_KERNEL);
5221 if (fgd == NULL)
5222 return -ENOMEM;
5224 mutex_lock(&graph_lock);
5226 fgd->hash = rcu_dereference_protected(ftrace_graph_hash,
5227 lockdep_is_held(&graph_lock));
5228 fgd->type = GRAPH_FILTER_FUNCTION;
5229 fgd->seq_ops = &ftrace_graph_seq_ops;
5231 ret = __ftrace_graph_open(inode, file, fgd);
5232 if (ret < 0)
5233 kfree(fgd);
5235 mutex_unlock(&graph_lock);
5236 return ret;
5239 static int
5240 ftrace_graph_notrace_open(struct inode *inode, struct file *file)
5242 struct ftrace_graph_data *fgd;
5243 int ret;
5245 if (unlikely(ftrace_disabled))
5246 return -ENODEV;
5248 fgd = kmalloc(sizeof(*fgd), GFP_KERNEL);
5249 if (fgd == NULL)
5250 return -ENOMEM;
5252 mutex_lock(&graph_lock);
5254 fgd->hash = rcu_dereference_protected(ftrace_graph_notrace_hash,
5255 lockdep_is_held(&graph_lock));
5256 fgd->type = GRAPH_FILTER_NOTRACE;
5257 fgd->seq_ops = &ftrace_graph_seq_ops;
5259 ret = __ftrace_graph_open(inode, file, fgd);
5260 if (ret < 0)
5261 kfree(fgd);
5263 mutex_unlock(&graph_lock);
5264 return ret;
5267 static int
5268 ftrace_graph_release(struct inode *inode, struct file *file)
5270 struct ftrace_graph_data *fgd;
5271 struct ftrace_hash *old_hash, *new_hash;
5272 struct trace_parser *parser;
5273 int ret = 0;
5275 if (file->f_mode & FMODE_READ) {
5276 struct seq_file *m = file->private_data;
5278 fgd = m->private;
5279 seq_release(inode, file);
5280 } else {
5281 fgd = file->private_data;
5285 if (file->f_mode & FMODE_WRITE) {
5287 parser = &fgd->parser;
5289 if (trace_parser_loaded((parser))) {
5290 ret = ftrace_graph_set_hash(fgd->new_hash,
5291 parser->buffer);
5294 trace_parser_put(parser);
5296 new_hash = __ftrace_hash_move(fgd->new_hash);
5297 if (!new_hash) {
5298 ret = -ENOMEM;
5299 goto out;
5302 mutex_lock(&graph_lock);
5304 if (fgd->type == GRAPH_FILTER_FUNCTION) {
5305 old_hash = rcu_dereference_protected(ftrace_graph_hash,
5306 lockdep_is_held(&graph_lock));
5307 rcu_assign_pointer(ftrace_graph_hash, new_hash);
5308 } else {
5309 old_hash = rcu_dereference_protected(ftrace_graph_notrace_hash,
5310 lockdep_is_held(&graph_lock));
5311 rcu_assign_pointer(ftrace_graph_notrace_hash, new_hash);
5314 mutex_unlock(&graph_lock);
5316 /* Wait till all users are no longer using the old hash */
5317 synchronize_sched();
5319 free_ftrace_hash(old_hash);
5322 out:
5323 free_ftrace_hash(fgd->new_hash);
5324 kfree(fgd);
5326 return ret;
5329 static int
5330 ftrace_graph_set_hash(struct ftrace_hash *hash, char *buffer)
5332 struct ftrace_glob func_g;
5333 struct dyn_ftrace *rec;
5334 struct ftrace_page *pg;
5335 struct ftrace_func_entry *entry;
5336 int fail = 1;
5337 int not;
5339 /* decode regex */
5340 func_g.type = filter_parse_regex(buffer, strlen(buffer),
5341 &func_g.search, &not);
5343 func_g.len = strlen(func_g.search);
5345 mutex_lock(&ftrace_lock);
5347 if (unlikely(ftrace_disabled)) {
5348 mutex_unlock(&ftrace_lock);
5349 return -ENODEV;
5352 do_for_each_ftrace_rec(pg, rec) {
5354 if (rec->flags & FTRACE_FL_DISABLED)
5355 continue;
5357 if (ftrace_match_record(rec, &func_g, NULL, 0)) {
5358 entry = ftrace_lookup_ip(hash, rec->ip);
5360 if (!not) {
5361 fail = 0;
5363 if (entry)
5364 continue;
5365 if (add_hash_entry(hash, rec->ip) < 0)
5366 goto out;
5367 } else {
5368 if (entry) {
5369 free_hash_entry(hash, entry);
5370 fail = 0;
5374 } while_for_each_ftrace_rec();
5375 out:
5376 mutex_unlock(&ftrace_lock);
5378 if (fail)
5379 return -EINVAL;
5381 return 0;
5384 static ssize_t
5385 ftrace_graph_write(struct file *file, const char __user *ubuf,
5386 size_t cnt, loff_t *ppos)
5388 ssize_t read, ret = 0;
5389 struct ftrace_graph_data *fgd = file->private_data;
5390 struct trace_parser *parser;
5392 if (!cnt)
5393 return 0;
5395 /* Read mode uses seq functions */
5396 if (file->f_mode & FMODE_READ) {
5397 struct seq_file *m = file->private_data;
5398 fgd = m->private;
5401 parser = &fgd->parser;
5403 read = trace_get_user(parser, ubuf, cnt, ppos);
5405 if (read >= 0 && trace_parser_loaded(parser) &&
5406 !trace_parser_cont(parser)) {
5408 ret = ftrace_graph_set_hash(fgd->new_hash,
5409 parser->buffer);
5410 trace_parser_clear(parser);
5413 if (!ret)
5414 ret = read;
5416 return ret;
5419 static const struct file_operations ftrace_graph_fops = {
5420 .open = ftrace_graph_open,
5421 .read = seq_read,
5422 .write = ftrace_graph_write,
5423 .llseek = tracing_lseek,
5424 .release = ftrace_graph_release,
5427 static const struct file_operations ftrace_graph_notrace_fops = {
5428 .open = ftrace_graph_notrace_open,
5429 .read = seq_read,
5430 .write = ftrace_graph_write,
5431 .llseek = tracing_lseek,
5432 .release = ftrace_graph_release,
5434 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
5436 void ftrace_create_filter_files(struct ftrace_ops *ops,
5437 struct dentry *parent)
5440 trace_create_file("set_ftrace_filter", 0644, parent,
5441 ops, &ftrace_filter_fops);
5443 trace_create_file("set_ftrace_notrace", 0644, parent,
5444 ops, &ftrace_notrace_fops);
5448 * The name "destroy_filter_files" is really a misnomer. Although
5449 * in the future, it may actualy delete the files, but this is
5450 * really intended to make sure the ops passed in are disabled
5451 * and that when this function returns, the caller is free to
5452 * free the ops.
5454 * The "destroy" name is only to match the "create" name that this
5455 * should be paired with.
5457 void ftrace_destroy_filter_files(struct ftrace_ops *ops)
5459 mutex_lock(&ftrace_lock);
5460 if (ops->flags & FTRACE_OPS_FL_ENABLED)
5461 ftrace_shutdown(ops, 0);
5462 ops->flags |= FTRACE_OPS_FL_DELETED;
5463 mutex_unlock(&ftrace_lock);
5466 static __init int ftrace_init_dyn_tracefs(struct dentry *d_tracer)
5469 trace_create_file("available_filter_functions", 0444,
5470 d_tracer, NULL, &ftrace_avail_fops);
5472 trace_create_file("enabled_functions", 0444,
5473 d_tracer, NULL, &ftrace_enabled_fops);
5475 ftrace_create_filter_files(&global_ops, d_tracer);
5477 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
5478 trace_create_file("set_graph_function", 0644, d_tracer,
5479 NULL,
5480 &ftrace_graph_fops);
5481 trace_create_file("set_graph_notrace", 0644, d_tracer,
5482 NULL,
5483 &ftrace_graph_notrace_fops);
5484 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
5486 return 0;
5489 static int ftrace_cmp_ips(const void *a, const void *b)
5491 const unsigned long *ipa = a;
5492 const unsigned long *ipb = b;
5494 if (*ipa > *ipb)
5495 return 1;
5496 if (*ipa < *ipb)
5497 return -1;
5498 return 0;
5501 static int ftrace_process_locs(struct module *mod,
5502 unsigned long *start,
5503 unsigned long *end)
5505 struct ftrace_page *start_pg;
5506 struct ftrace_page *pg;
5507 struct dyn_ftrace *rec;
5508 unsigned long count;
5509 unsigned long *p;
5510 unsigned long addr;
5511 unsigned long flags = 0; /* Shut up gcc */
5512 int ret = -ENOMEM;
5514 count = end - start;
5516 if (!count)
5517 return 0;
5519 sort(start, count, sizeof(*start),
5520 ftrace_cmp_ips, NULL);
5522 start_pg = ftrace_allocate_pages(count);
5523 if (!start_pg)
5524 return -ENOMEM;
5526 mutex_lock(&ftrace_lock);
5529 * Core and each module needs their own pages, as
5530 * modules will free them when they are removed.
5531 * Force a new page to be allocated for modules.
5533 if (!mod) {
5534 WARN_ON(ftrace_pages || ftrace_pages_start);
5535 /* First initialization */
5536 ftrace_pages = ftrace_pages_start = start_pg;
5537 } else {
5538 if (!ftrace_pages)
5539 goto out;
5541 if (WARN_ON(ftrace_pages->next)) {
5542 /* Hmm, we have free pages? */
5543 while (ftrace_pages->next)
5544 ftrace_pages = ftrace_pages->next;
5547 ftrace_pages->next = start_pg;
5550 p = start;
5551 pg = start_pg;
5552 while (p < end) {
5553 addr = ftrace_call_adjust(*p++);
5555 * Some architecture linkers will pad between
5556 * the different mcount_loc sections of different
5557 * object files to satisfy alignments.
5558 * Skip any NULL pointers.
5560 if (!addr)
5561 continue;
5563 if (pg->index == pg->size) {
5564 /* We should have allocated enough */
5565 if (WARN_ON(!pg->next))
5566 break;
5567 pg = pg->next;
5570 rec = &pg->records[pg->index++];
5571 rec->ip = addr;
5574 /* We should have used all pages */
5575 WARN_ON(pg->next);
5577 /* Assign the last page to ftrace_pages */
5578 ftrace_pages = pg;
5581 * We only need to disable interrupts on start up
5582 * because we are modifying code that an interrupt
5583 * may execute, and the modification is not atomic.
5584 * But for modules, nothing runs the code we modify
5585 * until we are finished with it, and there's no
5586 * reason to cause large interrupt latencies while we do it.
5588 if (!mod)
5589 local_irq_save(flags);
5590 ftrace_update_code(mod, start_pg);
5591 if (!mod)
5592 local_irq_restore(flags);
5593 ret = 0;
5594 out:
5595 mutex_unlock(&ftrace_lock);
5597 return ret;
5600 struct ftrace_mod_func {
5601 struct list_head list;
5602 char *name;
5603 unsigned long ip;
5604 unsigned int size;
5607 struct ftrace_mod_map {
5608 struct rcu_head rcu;
5609 struct list_head list;
5610 struct module *mod;
5611 unsigned long start_addr;
5612 unsigned long end_addr;
5613 struct list_head funcs;
5614 unsigned int num_funcs;
5617 #ifdef CONFIG_MODULES
5619 #define next_to_ftrace_page(p) container_of(p, struct ftrace_page, next)
5621 static LIST_HEAD(ftrace_mod_maps);
5623 static int referenced_filters(struct dyn_ftrace *rec)
5625 struct ftrace_ops *ops;
5626 int cnt = 0;
5628 for (ops = ftrace_ops_list; ops != &ftrace_list_end; ops = ops->next) {
5629 if (ops_references_rec(ops, rec))
5630 cnt++;
5633 return cnt;
5636 static void
5637 clear_mod_from_hash(struct ftrace_page *pg, struct ftrace_hash *hash)
5639 struct ftrace_func_entry *entry;
5640 struct dyn_ftrace *rec;
5641 int i;
5643 if (ftrace_hash_empty(hash))
5644 return;
5646 for (i = 0; i < pg->index; i++) {
5647 rec = &pg->records[i];
5648 entry = __ftrace_lookup_ip(hash, rec->ip);
5650 * Do not allow this rec to match again.
5651 * Yeah, it may waste some memory, but will be removed
5652 * if/when the hash is modified again.
5654 if (entry)
5655 entry->ip = 0;
5659 /* Clear any records from hashs */
5660 static void clear_mod_from_hashes(struct ftrace_page *pg)
5662 struct trace_array *tr;
5664 mutex_lock(&trace_types_lock);
5665 list_for_each_entry(tr, &ftrace_trace_arrays, list) {
5666 if (!tr->ops || !tr->ops->func_hash)
5667 continue;
5668 mutex_lock(&tr->ops->func_hash->regex_lock);
5669 clear_mod_from_hash(pg, tr->ops->func_hash->filter_hash);
5670 clear_mod_from_hash(pg, tr->ops->func_hash->notrace_hash);
5671 mutex_unlock(&tr->ops->func_hash->regex_lock);
5673 mutex_unlock(&trace_types_lock);
5676 static void ftrace_free_mod_map(struct rcu_head *rcu)
5678 struct ftrace_mod_map *mod_map = container_of(rcu, struct ftrace_mod_map, rcu);
5679 struct ftrace_mod_func *mod_func;
5680 struct ftrace_mod_func *n;
5682 /* All the contents of mod_map are now not visible to readers */
5683 list_for_each_entry_safe(mod_func, n, &mod_map->funcs, list) {
5684 kfree(mod_func->name);
5685 list_del(&mod_func->list);
5686 kfree(mod_func);
5689 kfree(mod_map);
5692 void ftrace_release_mod(struct module *mod)
5694 struct ftrace_mod_map *mod_map;
5695 struct ftrace_mod_map *n;
5696 struct dyn_ftrace *rec;
5697 struct ftrace_page **last_pg;
5698 struct ftrace_page *tmp_page = NULL;
5699 struct ftrace_page *pg;
5700 int order;
5702 mutex_lock(&ftrace_lock);
5704 if (ftrace_disabled)
5705 goto out_unlock;
5707 list_for_each_entry_safe(mod_map, n, &ftrace_mod_maps, list) {
5708 if (mod_map->mod == mod) {
5709 list_del_rcu(&mod_map->list);
5710 call_rcu_sched(&mod_map->rcu, ftrace_free_mod_map);
5711 break;
5716 * Each module has its own ftrace_pages, remove
5717 * them from the list.
5719 last_pg = &ftrace_pages_start;
5720 for (pg = ftrace_pages_start; pg; pg = *last_pg) {
5721 rec = &pg->records[0];
5722 if (within_module_core(rec->ip, mod) ||
5723 within_module_init(rec->ip, mod)) {
5725 * As core pages are first, the first
5726 * page should never be a module page.
5728 if (WARN_ON(pg == ftrace_pages_start))
5729 goto out_unlock;
5731 /* Check if we are deleting the last page */
5732 if (pg == ftrace_pages)
5733 ftrace_pages = next_to_ftrace_page(last_pg);
5735 ftrace_update_tot_cnt -= pg->index;
5736 *last_pg = pg->next;
5738 pg->next = tmp_page;
5739 tmp_page = pg;
5740 } else
5741 last_pg = &pg->next;
5743 out_unlock:
5744 mutex_unlock(&ftrace_lock);
5746 for (pg = tmp_page; pg; pg = tmp_page) {
5748 /* Needs to be called outside of ftrace_lock */
5749 clear_mod_from_hashes(pg);
5751 order = get_count_order(pg->size / ENTRIES_PER_PAGE);
5752 free_pages((unsigned long)pg->records, order);
5753 tmp_page = pg->next;
5754 kfree(pg);
5758 void ftrace_module_enable(struct module *mod)
5760 struct dyn_ftrace *rec;
5761 struct ftrace_page *pg;
5763 mutex_lock(&ftrace_lock);
5765 if (ftrace_disabled)
5766 goto out_unlock;
5769 * If the tracing is enabled, go ahead and enable the record.
5771 * The reason not to enable the record immediatelly is the
5772 * inherent check of ftrace_make_nop/ftrace_make_call for
5773 * correct previous instructions. Making first the NOP
5774 * conversion puts the module to the correct state, thus
5775 * passing the ftrace_make_call check.
5777 * We also delay this to after the module code already set the
5778 * text to read-only, as we now need to set it back to read-write
5779 * so that we can modify the text.
5781 if (ftrace_start_up)
5782 ftrace_arch_code_modify_prepare();
5784 do_for_each_ftrace_rec(pg, rec) {
5785 int cnt;
5787 * do_for_each_ftrace_rec() is a double loop.
5788 * module text shares the pg. If a record is
5789 * not part of this module, then skip this pg,
5790 * which the "break" will do.
5792 if (!within_module_core(rec->ip, mod) &&
5793 !within_module_init(rec->ip, mod))
5794 break;
5796 cnt = 0;
5799 * When adding a module, we need to check if tracers are
5800 * currently enabled and if they are, and can trace this record,
5801 * we need to enable the module functions as well as update the
5802 * reference counts for those function records.
5804 if (ftrace_start_up)
5805 cnt += referenced_filters(rec);
5807 /* This clears FTRACE_FL_DISABLED */
5808 rec->flags = cnt;
5810 if (ftrace_start_up && cnt) {
5811 int failed = __ftrace_replace_code(rec, 1);
5812 if (failed) {
5813 ftrace_bug(failed, rec);
5814 goto out_loop;
5818 } while_for_each_ftrace_rec();
5820 out_loop:
5821 if (ftrace_start_up)
5822 ftrace_arch_code_modify_post_process();
5824 out_unlock:
5825 mutex_unlock(&ftrace_lock);
5827 process_cached_mods(mod->name);
5830 void ftrace_module_init(struct module *mod)
5832 if (ftrace_disabled || !mod->num_ftrace_callsites)
5833 return;
5835 ftrace_process_locs(mod, mod->ftrace_callsites,
5836 mod->ftrace_callsites + mod->num_ftrace_callsites);
5839 static void save_ftrace_mod_rec(struct ftrace_mod_map *mod_map,
5840 struct dyn_ftrace *rec)
5842 struct ftrace_mod_func *mod_func;
5843 unsigned long symsize;
5844 unsigned long offset;
5845 char str[KSYM_SYMBOL_LEN];
5846 char *modname;
5847 const char *ret;
5849 ret = kallsyms_lookup(rec->ip, &symsize, &offset, &modname, str);
5850 if (!ret)
5851 return;
5853 mod_func = kmalloc(sizeof(*mod_func), GFP_KERNEL);
5854 if (!mod_func)
5855 return;
5857 mod_func->name = kstrdup(str, GFP_KERNEL);
5858 if (!mod_func->name) {
5859 kfree(mod_func);
5860 return;
5863 mod_func->ip = rec->ip - offset;
5864 mod_func->size = symsize;
5866 mod_map->num_funcs++;
5868 list_add_rcu(&mod_func->list, &mod_map->funcs);
5871 static struct ftrace_mod_map *
5872 allocate_ftrace_mod_map(struct module *mod,
5873 unsigned long start, unsigned long end)
5875 struct ftrace_mod_map *mod_map;
5877 mod_map = kmalloc(sizeof(*mod_map), GFP_KERNEL);
5878 if (!mod_map)
5879 return NULL;
5881 mod_map->mod = mod;
5882 mod_map->start_addr = start;
5883 mod_map->end_addr = end;
5884 mod_map->num_funcs = 0;
5886 INIT_LIST_HEAD_RCU(&mod_map->funcs);
5888 list_add_rcu(&mod_map->list, &ftrace_mod_maps);
5890 return mod_map;
5893 static const char *
5894 ftrace_func_address_lookup(struct ftrace_mod_map *mod_map,
5895 unsigned long addr, unsigned long *size,
5896 unsigned long *off, char *sym)
5898 struct ftrace_mod_func *found_func = NULL;
5899 struct ftrace_mod_func *mod_func;
5901 list_for_each_entry_rcu(mod_func, &mod_map->funcs, list) {
5902 if (addr >= mod_func->ip &&
5903 addr < mod_func->ip + mod_func->size) {
5904 found_func = mod_func;
5905 break;
5909 if (found_func) {
5910 if (size)
5911 *size = found_func->size;
5912 if (off)
5913 *off = addr - found_func->ip;
5914 if (sym)
5915 strlcpy(sym, found_func->name, KSYM_NAME_LEN);
5917 return found_func->name;
5920 return NULL;
5923 const char *
5924 ftrace_mod_address_lookup(unsigned long addr, unsigned long *size,
5925 unsigned long *off, char **modname, char *sym)
5927 struct ftrace_mod_map *mod_map;
5928 const char *ret = NULL;
5930 /* mod_map is freed via call_rcu_sched() */
5931 preempt_disable();
5932 list_for_each_entry_rcu(mod_map, &ftrace_mod_maps, list) {
5933 ret = ftrace_func_address_lookup(mod_map, addr, size, off, sym);
5934 if (ret) {
5935 if (modname)
5936 *modname = mod_map->mod->name;
5937 break;
5940 preempt_enable();
5942 return ret;
5945 int ftrace_mod_get_kallsym(unsigned int symnum, unsigned long *value,
5946 char *type, char *name,
5947 char *module_name, int *exported)
5949 struct ftrace_mod_map *mod_map;
5950 struct ftrace_mod_func *mod_func;
5952 preempt_disable();
5953 list_for_each_entry_rcu(mod_map, &ftrace_mod_maps, list) {
5955 if (symnum >= mod_map->num_funcs) {
5956 symnum -= mod_map->num_funcs;
5957 continue;
5960 list_for_each_entry_rcu(mod_func, &mod_map->funcs, list) {
5961 if (symnum > 1) {
5962 symnum--;
5963 continue;
5966 *value = mod_func->ip;
5967 *type = 'T';
5968 strlcpy(name, mod_func->name, KSYM_NAME_LEN);
5969 strlcpy(module_name, mod_map->mod->name, MODULE_NAME_LEN);
5970 *exported = 1;
5971 preempt_enable();
5972 return 0;
5974 WARN_ON(1);
5975 break;
5977 preempt_enable();
5978 return -ERANGE;
5981 #else
5982 static void save_ftrace_mod_rec(struct ftrace_mod_map *mod_map,
5983 struct dyn_ftrace *rec) { }
5984 static inline struct ftrace_mod_map *
5985 allocate_ftrace_mod_map(struct module *mod,
5986 unsigned long start, unsigned long end)
5988 return NULL;
5990 #endif /* CONFIG_MODULES */
5992 struct ftrace_init_func {
5993 struct list_head list;
5994 unsigned long ip;
5997 /* Clear any init ips from hashes */
5998 static void
5999 clear_func_from_hash(struct ftrace_init_func *func, struct ftrace_hash *hash)
6001 struct ftrace_func_entry *entry;
6003 if (ftrace_hash_empty(hash))
6004 return;
6006 entry = __ftrace_lookup_ip(hash, func->ip);
6009 * Do not allow this rec to match again.
6010 * Yeah, it may waste some memory, but will be removed
6011 * if/when the hash is modified again.
6013 if (entry)
6014 entry->ip = 0;
6017 static void
6018 clear_func_from_hashes(struct ftrace_init_func *func)
6020 struct trace_array *tr;
6022 mutex_lock(&trace_types_lock);
6023 list_for_each_entry(tr, &ftrace_trace_arrays, list) {
6024 if (!tr->ops || !tr->ops->func_hash)
6025 continue;
6026 mutex_lock(&tr->ops->func_hash->regex_lock);
6027 clear_func_from_hash(func, tr->ops->func_hash->filter_hash);
6028 clear_func_from_hash(func, tr->ops->func_hash->notrace_hash);
6029 mutex_unlock(&tr->ops->func_hash->regex_lock);
6031 mutex_unlock(&trace_types_lock);
6034 static void add_to_clear_hash_list(struct list_head *clear_list,
6035 struct dyn_ftrace *rec)
6037 struct ftrace_init_func *func;
6039 func = kmalloc(sizeof(*func), GFP_KERNEL);
6040 if (!func) {
6041 WARN_ONCE(1, "alloc failure, ftrace filter could be stale\n");
6042 return;
6045 func->ip = rec->ip;
6046 list_add(&func->list, clear_list);
6049 void ftrace_free_mem(struct module *mod, void *start_ptr, void *end_ptr)
6051 unsigned long start = (unsigned long)(start_ptr);
6052 unsigned long end = (unsigned long)(end_ptr);
6053 struct ftrace_page **last_pg = &ftrace_pages_start;
6054 struct ftrace_page *pg;
6055 struct dyn_ftrace *rec;
6056 struct dyn_ftrace key;
6057 struct ftrace_mod_map *mod_map = NULL;
6058 struct ftrace_init_func *func, *func_next;
6059 struct list_head clear_hash;
6060 int order;
6062 INIT_LIST_HEAD(&clear_hash);
6064 key.ip = start;
6065 key.flags = end; /* overload flags, as it is unsigned long */
6067 mutex_lock(&ftrace_lock);
6070 * If we are freeing module init memory, then check if
6071 * any tracer is active. If so, we need to save a mapping of
6072 * the module functions being freed with the address.
6074 if (mod && ftrace_ops_list != &ftrace_list_end)
6075 mod_map = allocate_ftrace_mod_map(mod, start, end);
6077 for (pg = ftrace_pages_start; pg; last_pg = &pg->next, pg = *last_pg) {
6078 if (end < pg->records[0].ip ||
6079 start >= (pg->records[pg->index - 1].ip + MCOUNT_INSN_SIZE))
6080 continue;
6081 again:
6082 rec = bsearch(&key, pg->records, pg->index,
6083 sizeof(struct dyn_ftrace),
6084 ftrace_cmp_recs);
6085 if (!rec)
6086 continue;
6088 /* rec will be cleared from hashes after ftrace_lock unlock */
6089 add_to_clear_hash_list(&clear_hash, rec);
6091 if (mod_map)
6092 save_ftrace_mod_rec(mod_map, rec);
6094 pg->index--;
6095 ftrace_update_tot_cnt--;
6096 if (!pg->index) {
6097 *last_pg = pg->next;
6098 order = get_count_order(pg->size / ENTRIES_PER_PAGE);
6099 free_pages((unsigned long)pg->records, order);
6100 kfree(pg);
6101 pg = container_of(last_pg, struct ftrace_page, next);
6102 if (!(*last_pg))
6103 ftrace_pages = pg;
6104 continue;
6106 memmove(rec, rec + 1,
6107 (pg->index - (rec - pg->records)) * sizeof(*rec));
6108 /* More than one function may be in this block */
6109 goto again;
6111 mutex_unlock(&ftrace_lock);
6113 list_for_each_entry_safe(func, func_next, &clear_hash, list) {
6114 clear_func_from_hashes(func);
6115 kfree(func);
6119 void __init ftrace_free_init_mem(void)
6121 void *start = (void *)(&__init_begin);
6122 void *end = (void *)(&__init_end);
6124 ftrace_free_mem(NULL, start, end);
6127 void __init ftrace_init(void)
6129 extern unsigned long __start_mcount_loc[];
6130 extern unsigned long __stop_mcount_loc[];
6131 unsigned long count, flags;
6132 int ret;
6134 local_irq_save(flags);
6135 ret = ftrace_dyn_arch_init();
6136 local_irq_restore(flags);
6137 if (ret)
6138 goto failed;
6140 count = __stop_mcount_loc - __start_mcount_loc;
6141 if (!count) {
6142 pr_info("ftrace: No functions to be traced?\n");
6143 goto failed;
6146 pr_info("ftrace: allocating %ld entries in %ld pages\n",
6147 count, count / ENTRIES_PER_PAGE + 1);
6149 last_ftrace_enabled = ftrace_enabled = 1;
6151 ret = ftrace_process_locs(NULL,
6152 __start_mcount_loc,
6153 __stop_mcount_loc);
6155 set_ftrace_early_filters();
6157 return;
6158 failed:
6159 ftrace_disabled = 1;
6162 /* Do nothing if arch does not support this */
6163 void __weak arch_ftrace_update_trampoline(struct ftrace_ops *ops)
6167 static void ftrace_update_trampoline(struct ftrace_ops *ops)
6169 arch_ftrace_update_trampoline(ops);
6172 void ftrace_init_trace_array(struct trace_array *tr)
6174 INIT_LIST_HEAD(&tr->func_probes);
6175 INIT_LIST_HEAD(&tr->mod_trace);
6176 INIT_LIST_HEAD(&tr->mod_notrace);
6178 #else
6180 static struct ftrace_ops global_ops = {
6181 .func = ftrace_stub,
6182 .flags = FTRACE_OPS_FL_RECURSION_SAFE |
6183 FTRACE_OPS_FL_INITIALIZED |
6184 FTRACE_OPS_FL_PID,
6187 static int __init ftrace_nodyn_init(void)
6189 ftrace_enabled = 1;
6190 return 0;
6192 core_initcall(ftrace_nodyn_init);
6194 static inline int ftrace_init_dyn_tracefs(struct dentry *d_tracer) { return 0; }
6195 static inline void ftrace_startup_enable(int command) { }
6196 static inline void ftrace_startup_all(int command) { }
6197 /* Keep as macros so we do not need to define the commands */
6198 # define ftrace_startup(ops, command) \
6199 ({ \
6200 int ___ret = __register_ftrace_function(ops); \
6201 if (!___ret) \
6202 (ops)->flags |= FTRACE_OPS_FL_ENABLED; \
6203 ___ret; \
6205 # define ftrace_shutdown(ops, command) \
6206 ({ \
6207 int ___ret = __unregister_ftrace_function(ops); \
6208 if (!___ret) \
6209 (ops)->flags &= ~FTRACE_OPS_FL_ENABLED; \
6210 ___ret; \
6213 # define ftrace_startup_sysctl() do { } while (0)
6214 # define ftrace_shutdown_sysctl() do { } while (0)
6216 static inline int
6217 ftrace_ops_test(struct ftrace_ops *ops, unsigned long ip, void *regs)
6219 return 1;
6222 static void ftrace_update_trampoline(struct ftrace_ops *ops)
6226 #endif /* CONFIG_DYNAMIC_FTRACE */
6228 __init void ftrace_init_global_array_ops(struct trace_array *tr)
6230 tr->ops = &global_ops;
6231 tr->ops->private = tr;
6232 ftrace_init_trace_array(tr);
6235 void ftrace_init_array_ops(struct trace_array *tr, ftrace_func_t func)
6237 /* If we filter on pids, update to use the pid function */
6238 if (tr->flags & TRACE_ARRAY_FL_GLOBAL) {
6239 if (WARN_ON(tr->ops->func != ftrace_stub))
6240 printk("ftrace ops had %pS for function\n",
6241 tr->ops->func);
6243 tr->ops->func = func;
6244 tr->ops->private = tr;
6247 void ftrace_reset_array_ops(struct trace_array *tr)
6249 tr->ops->func = ftrace_stub;
6252 static inline void
6253 __ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip,
6254 struct ftrace_ops *ignored, struct pt_regs *regs)
6256 struct ftrace_ops *op;
6257 int bit;
6259 bit = trace_test_and_set_recursion(TRACE_LIST_START, TRACE_LIST_MAX);
6260 if (bit < 0)
6261 return;
6264 * Some of the ops may be dynamically allocated,
6265 * they must be freed after a synchronize_sched().
6267 preempt_disable_notrace();
6269 do_for_each_ftrace_op(op, ftrace_ops_list) {
6271 * Check the following for each ops before calling their func:
6272 * if RCU flag is set, then rcu_is_watching() must be true
6273 * if PER_CPU is set, then ftrace_function_local_disable()
6274 * must be false
6275 * Otherwise test if the ip matches the ops filter
6277 * If any of the above fails then the op->func() is not executed.
6279 if ((!(op->flags & FTRACE_OPS_FL_RCU) || rcu_is_watching()) &&
6280 ftrace_ops_test(op, ip, regs)) {
6281 if (FTRACE_WARN_ON(!op->func)) {
6282 pr_warn("op=%p %pS\n", op, op);
6283 goto out;
6285 op->func(ip, parent_ip, op, regs);
6287 } while_for_each_ftrace_op(op);
6288 out:
6289 preempt_enable_notrace();
6290 trace_clear_recursion(bit);
6294 * Some archs only support passing ip and parent_ip. Even though
6295 * the list function ignores the op parameter, we do not want any
6296 * C side effects, where a function is called without the caller
6297 * sending a third parameter.
6298 * Archs are to support both the regs and ftrace_ops at the same time.
6299 * If they support ftrace_ops, it is assumed they support regs.
6300 * If call backs want to use regs, they must either check for regs
6301 * being NULL, or CONFIG_DYNAMIC_FTRACE_WITH_REGS.
6302 * Note, CONFIG_DYNAMIC_FTRACE_WITH_REGS expects a full regs to be saved.
6303 * An architecture can pass partial regs with ftrace_ops and still
6304 * set the ARCH_SUPPORTS_FTRACE_OPS.
6306 #if ARCH_SUPPORTS_FTRACE_OPS
6307 static void ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip,
6308 struct ftrace_ops *op, struct pt_regs *regs)
6310 __ftrace_ops_list_func(ip, parent_ip, NULL, regs);
6312 #else
6313 static void ftrace_ops_no_ops(unsigned long ip, unsigned long parent_ip)
6315 __ftrace_ops_list_func(ip, parent_ip, NULL, NULL);
6317 #endif
6320 * If there's only one function registered but it does not support
6321 * recursion, needs RCU protection and/or requires per cpu handling, then
6322 * this function will be called by the mcount trampoline.
6324 static void ftrace_ops_assist_func(unsigned long ip, unsigned long parent_ip,
6325 struct ftrace_ops *op, struct pt_regs *regs)
6327 int bit;
6329 if ((op->flags & FTRACE_OPS_FL_RCU) && !rcu_is_watching())
6330 return;
6332 bit = trace_test_and_set_recursion(TRACE_LIST_START, TRACE_LIST_MAX);
6333 if (bit < 0)
6334 return;
6336 preempt_disable_notrace();
6338 op->func(ip, parent_ip, op, regs);
6340 preempt_enable_notrace();
6341 trace_clear_recursion(bit);
6345 * ftrace_ops_get_func - get the function a trampoline should call
6346 * @ops: the ops to get the function for
6348 * Normally the mcount trampoline will call the ops->func, but there
6349 * are times that it should not. For example, if the ops does not
6350 * have its own recursion protection, then it should call the
6351 * ftrace_ops_assist_func() instead.
6353 * Returns the function that the trampoline should call for @ops.
6355 ftrace_func_t ftrace_ops_get_func(struct ftrace_ops *ops)
6358 * If the function does not handle recursion, needs to be RCU safe,
6359 * or does per cpu logic, then we need to call the assist handler.
6361 if (!(ops->flags & FTRACE_OPS_FL_RECURSION_SAFE) ||
6362 ops->flags & FTRACE_OPS_FL_RCU)
6363 return ftrace_ops_assist_func;
6365 return ops->func;
6368 static void
6369 ftrace_filter_pid_sched_switch_probe(void *data, bool preempt,
6370 struct task_struct *prev, struct task_struct *next)
6372 struct trace_array *tr = data;
6373 struct trace_pid_list *pid_list;
6375 pid_list = rcu_dereference_sched(tr->function_pids);
6377 this_cpu_write(tr->trace_buffer.data->ftrace_ignore_pid,
6378 trace_ignore_this_task(pid_list, next));
6381 static void
6382 ftrace_pid_follow_sched_process_fork(void *data,
6383 struct task_struct *self,
6384 struct task_struct *task)
6386 struct trace_pid_list *pid_list;
6387 struct trace_array *tr = data;
6389 pid_list = rcu_dereference_sched(tr->function_pids);
6390 trace_filter_add_remove_task(pid_list, self, task);
6393 static void
6394 ftrace_pid_follow_sched_process_exit(void *data, struct task_struct *task)
6396 struct trace_pid_list *pid_list;
6397 struct trace_array *tr = data;
6399 pid_list = rcu_dereference_sched(tr->function_pids);
6400 trace_filter_add_remove_task(pid_list, NULL, task);
6403 void ftrace_pid_follow_fork(struct trace_array *tr, bool enable)
6405 if (enable) {
6406 register_trace_sched_process_fork(ftrace_pid_follow_sched_process_fork,
6407 tr);
6408 register_trace_sched_process_exit(ftrace_pid_follow_sched_process_exit,
6409 tr);
6410 } else {
6411 unregister_trace_sched_process_fork(ftrace_pid_follow_sched_process_fork,
6412 tr);
6413 unregister_trace_sched_process_exit(ftrace_pid_follow_sched_process_exit,
6414 tr);
6418 static void clear_ftrace_pids(struct trace_array *tr)
6420 struct trace_pid_list *pid_list;
6421 int cpu;
6423 pid_list = rcu_dereference_protected(tr->function_pids,
6424 lockdep_is_held(&ftrace_lock));
6425 if (!pid_list)
6426 return;
6428 unregister_trace_sched_switch(ftrace_filter_pid_sched_switch_probe, tr);
6430 for_each_possible_cpu(cpu)
6431 per_cpu_ptr(tr->trace_buffer.data, cpu)->ftrace_ignore_pid = false;
6433 rcu_assign_pointer(tr->function_pids, NULL);
6435 /* Wait till all users are no longer using pid filtering */
6436 synchronize_sched();
6438 trace_free_pid_list(pid_list);
6441 void ftrace_clear_pids(struct trace_array *tr)
6443 mutex_lock(&ftrace_lock);
6445 clear_ftrace_pids(tr);
6447 mutex_unlock(&ftrace_lock);
6450 static void ftrace_pid_reset(struct trace_array *tr)
6452 mutex_lock(&ftrace_lock);
6453 clear_ftrace_pids(tr);
6455 ftrace_update_pid_func();
6456 ftrace_startup_all(0);
6458 mutex_unlock(&ftrace_lock);
6461 /* Greater than any max PID */
6462 #define FTRACE_NO_PIDS (void *)(PID_MAX_LIMIT + 1)
6464 static void *fpid_start(struct seq_file *m, loff_t *pos)
6465 __acquires(RCU)
6467 struct trace_pid_list *pid_list;
6468 struct trace_array *tr = m->private;
6470 mutex_lock(&ftrace_lock);
6471 rcu_read_lock_sched();
6473 pid_list = rcu_dereference_sched(tr->function_pids);
6475 if (!pid_list)
6476 return !(*pos) ? FTRACE_NO_PIDS : NULL;
6478 return trace_pid_start(pid_list, pos);
6481 static void *fpid_next(struct seq_file *m, void *v, loff_t *pos)
6483 struct trace_array *tr = m->private;
6484 struct trace_pid_list *pid_list = rcu_dereference_sched(tr->function_pids);
6486 if (v == FTRACE_NO_PIDS)
6487 return NULL;
6489 return trace_pid_next(pid_list, v, pos);
6492 static void fpid_stop(struct seq_file *m, void *p)
6493 __releases(RCU)
6495 rcu_read_unlock_sched();
6496 mutex_unlock(&ftrace_lock);
6499 static int fpid_show(struct seq_file *m, void *v)
6501 if (v == FTRACE_NO_PIDS) {
6502 seq_puts(m, "no pid\n");
6503 return 0;
6506 return trace_pid_show(m, v);
6509 static const struct seq_operations ftrace_pid_sops = {
6510 .start = fpid_start,
6511 .next = fpid_next,
6512 .stop = fpid_stop,
6513 .show = fpid_show,
6516 static int
6517 ftrace_pid_open(struct inode *inode, struct file *file)
6519 struct trace_array *tr = inode->i_private;
6520 struct seq_file *m;
6521 int ret = 0;
6523 if (trace_array_get(tr) < 0)
6524 return -ENODEV;
6526 if ((file->f_mode & FMODE_WRITE) &&
6527 (file->f_flags & O_TRUNC))
6528 ftrace_pid_reset(tr);
6530 ret = seq_open(file, &ftrace_pid_sops);
6531 if (ret < 0) {
6532 trace_array_put(tr);
6533 } else {
6534 m = file->private_data;
6535 /* copy tr over to seq ops */
6536 m->private = tr;
6539 return ret;
6542 static void ignore_task_cpu(void *data)
6544 struct trace_array *tr = data;
6545 struct trace_pid_list *pid_list;
6548 * This function is called by on_each_cpu() while the
6549 * event_mutex is held.
6551 pid_list = rcu_dereference_protected(tr->function_pids,
6552 mutex_is_locked(&ftrace_lock));
6554 this_cpu_write(tr->trace_buffer.data->ftrace_ignore_pid,
6555 trace_ignore_this_task(pid_list, current));
6558 static ssize_t
6559 ftrace_pid_write(struct file *filp, const char __user *ubuf,
6560 size_t cnt, loff_t *ppos)
6562 struct seq_file *m = filp->private_data;
6563 struct trace_array *tr = m->private;
6564 struct trace_pid_list *filtered_pids = NULL;
6565 struct trace_pid_list *pid_list;
6566 ssize_t ret;
6568 if (!cnt)
6569 return 0;
6571 mutex_lock(&ftrace_lock);
6573 filtered_pids = rcu_dereference_protected(tr->function_pids,
6574 lockdep_is_held(&ftrace_lock));
6576 ret = trace_pid_write(filtered_pids, &pid_list, ubuf, cnt);
6577 if (ret < 0)
6578 goto out;
6580 rcu_assign_pointer(tr->function_pids, pid_list);
6582 if (filtered_pids) {
6583 synchronize_sched();
6584 trace_free_pid_list(filtered_pids);
6585 } else if (pid_list) {
6586 /* Register a probe to set whether to ignore the tracing of a task */
6587 register_trace_sched_switch(ftrace_filter_pid_sched_switch_probe, tr);
6591 * Ignoring of pids is done at task switch. But we have to
6592 * check for those tasks that are currently running.
6593 * Always do this in case a pid was appended or removed.
6595 on_each_cpu(ignore_task_cpu, tr, 1);
6597 ftrace_update_pid_func();
6598 ftrace_startup_all(0);
6599 out:
6600 mutex_unlock(&ftrace_lock);
6602 if (ret > 0)
6603 *ppos += ret;
6605 return ret;
6608 static int
6609 ftrace_pid_release(struct inode *inode, struct file *file)
6611 struct trace_array *tr = inode->i_private;
6613 trace_array_put(tr);
6615 return seq_release(inode, file);
6618 static const struct file_operations ftrace_pid_fops = {
6619 .open = ftrace_pid_open,
6620 .write = ftrace_pid_write,
6621 .read = seq_read,
6622 .llseek = tracing_lseek,
6623 .release = ftrace_pid_release,
6626 void ftrace_init_tracefs(struct trace_array *tr, struct dentry *d_tracer)
6628 trace_create_file("set_ftrace_pid", 0644, d_tracer,
6629 tr, &ftrace_pid_fops);
6632 void __init ftrace_init_tracefs_toplevel(struct trace_array *tr,
6633 struct dentry *d_tracer)
6635 /* Only the top level directory has the dyn_tracefs and profile */
6636 WARN_ON(!(tr->flags & TRACE_ARRAY_FL_GLOBAL));
6638 ftrace_init_dyn_tracefs(d_tracer);
6639 ftrace_profile_tracefs(d_tracer);
6643 * ftrace_kill - kill ftrace
6645 * This function should be used by panic code. It stops ftrace
6646 * but in a not so nice way. If you need to simply kill ftrace
6647 * from a non-atomic section, use ftrace_kill.
6649 void ftrace_kill(void)
6651 ftrace_disabled = 1;
6652 ftrace_enabled = 0;
6653 ftrace_trace_function = ftrace_stub;
6657 * Test if ftrace is dead or not.
6659 int ftrace_is_dead(void)
6661 return ftrace_disabled;
6665 * register_ftrace_function - register a function for profiling
6666 * @ops - ops structure that holds the function for profiling.
6668 * Register a function to be called by all functions in the
6669 * kernel.
6671 * Note: @ops->func and all the functions it calls must be labeled
6672 * with "notrace", otherwise it will go into a
6673 * recursive loop.
6675 int register_ftrace_function(struct ftrace_ops *ops)
6677 int ret = -1;
6679 ftrace_ops_init(ops);
6681 mutex_lock(&ftrace_lock);
6683 ret = ftrace_startup(ops, 0);
6685 mutex_unlock(&ftrace_lock);
6687 return ret;
6689 EXPORT_SYMBOL_GPL(register_ftrace_function);
6692 * unregister_ftrace_function - unregister a function for profiling.
6693 * @ops - ops structure that holds the function to unregister
6695 * Unregister a function that was added to be called by ftrace profiling.
6697 int unregister_ftrace_function(struct ftrace_ops *ops)
6699 int ret;
6701 mutex_lock(&ftrace_lock);
6702 ret = ftrace_shutdown(ops, 0);
6703 mutex_unlock(&ftrace_lock);
6705 return ret;
6707 EXPORT_SYMBOL_GPL(unregister_ftrace_function);
6710 ftrace_enable_sysctl(struct ctl_table *table, int write,
6711 void __user *buffer, size_t *lenp,
6712 loff_t *ppos)
6714 int ret = -ENODEV;
6716 mutex_lock(&ftrace_lock);
6718 if (unlikely(ftrace_disabled))
6719 goto out;
6721 ret = proc_dointvec(table, write, buffer, lenp, ppos);
6723 if (ret || !write || (last_ftrace_enabled == !!ftrace_enabled))
6724 goto out;
6726 last_ftrace_enabled = !!ftrace_enabled;
6728 if (ftrace_enabled) {
6730 /* we are starting ftrace again */
6731 if (rcu_dereference_protected(ftrace_ops_list,
6732 lockdep_is_held(&ftrace_lock)) != &ftrace_list_end)
6733 update_ftrace_function();
6735 ftrace_startup_sysctl();
6737 } else {
6738 /* stopping ftrace calls (just send to ftrace_stub) */
6739 ftrace_trace_function = ftrace_stub;
6741 ftrace_shutdown_sysctl();
6744 out:
6745 mutex_unlock(&ftrace_lock);
6746 return ret;
6749 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
6751 static struct ftrace_ops graph_ops = {
6752 .func = ftrace_stub,
6753 .flags = FTRACE_OPS_FL_RECURSION_SAFE |
6754 FTRACE_OPS_FL_INITIALIZED |
6755 FTRACE_OPS_FL_PID |
6756 FTRACE_OPS_FL_STUB,
6757 #ifdef FTRACE_GRAPH_TRAMP_ADDR
6758 .trampoline = FTRACE_GRAPH_TRAMP_ADDR,
6759 /* trampoline_size is only needed for dynamically allocated tramps */
6760 #endif
6761 ASSIGN_OPS_HASH(graph_ops, &global_ops.local_hash)
6764 void ftrace_graph_sleep_time_control(bool enable)
6766 fgraph_sleep_time = enable;
6769 void ftrace_graph_graph_time_control(bool enable)
6771 fgraph_graph_time = enable;
6774 int ftrace_graph_entry_stub(struct ftrace_graph_ent *trace)
6776 return 0;
6779 /* The callbacks that hook a function */
6780 trace_func_graph_ret_t ftrace_graph_return =
6781 (trace_func_graph_ret_t)ftrace_stub;
6782 trace_func_graph_ent_t ftrace_graph_entry = ftrace_graph_entry_stub;
6783 static trace_func_graph_ent_t __ftrace_graph_entry = ftrace_graph_entry_stub;
6785 /* Try to assign a return stack array on FTRACE_RETSTACK_ALLOC_SIZE tasks. */
6786 static int alloc_retstack_tasklist(struct ftrace_ret_stack **ret_stack_list)
6788 int i;
6789 int ret = 0;
6790 int start = 0, end = FTRACE_RETSTACK_ALLOC_SIZE;
6791 struct task_struct *g, *t;
6793 for (i = 0; i < FTRACE_RETSTACK_ALLOC_SIZE; i++) {
6794 ret_stack_list[i] =
6795 kmalloc_array(FTRACE_RETFUNC_DEPTH,
6796 sizeof(struct ftrace_ret_stack),
6797 GFP_KERNEL);
6798 if (!ret_stack_list[i]) {
6799 start = 0;
6800 end = i;
6801 ret = -ENOMEM;
6802 goto free;
6806 read_lock(&tasklist_lock);
6807 do_each_thread(g, t) {
6808 if (start == end) {
6809 ret = -EAGAIN;
6810 goto unlock;
6813 if (t->ret_stack == NULL) {
6814 atomic_set(&t->tracing_graph_pause, 0);
6815 atomic_set(&t->trace_overrun, 0);
6816 t->curr_ret_stack = -1;
6817 /* Make sure the tasks see the -1 first: */
6818 smp_wmb();
6819 t->ret_stack = ret_stack_list[start++];
6821 } while_each_thread(g, t);
6823 unlock:
6824 read_unlock(&tasklist_lock);
6825 free:
6826 for (i = start; i < end; i++)
6827 kfree(ret_stack_list[i]);
6828 return ret;
6831 static void
6832 ftrace_graph_probe_sched_switch(void *ignore, bool preempt,
6833 struct task_struct *prev, struct task_struct *next)
6835 unsigned long long timestamp;
6836 int index;
6839 * Does the user want to count the time a function was asleep.
6840 * If so, do not update the time stamps.
6842 if (fgraph_sleep_time)
6843 return;
6845 timestamp = trace_clock_local();
6847 prev->ftrace_timestamp = timestamp;
6849 /* only process tasks that we timestamped */
6850 if (!next->ftrace_timestamp)
6851 return;
6854 * Update all the counters in next to make up for the
6855 * time next was sleeping.
6857 timestamp -= next->ftrace_timestamp;
6859 for (index = next->curr_ret_stack; index >= 0; index--)
6860 next->ret_stack[index].calltime += timestamp;
6863 /* Allocate a return stack for each task */
6864 static int start_graph_tracing(void)
6866 struct ftrace_ret_stack **ret_stack_list;
6867 int ret, cpu;
6869 ret_stack_list = kmalloc_array(FTRACE_RETSTACK_ALLOC_SIZE,
6870 sizeof(struct ftrace_ret_stack *),
6871 GFP_KERNEL);
6873 if (!ret_stack_list)
6874 return -ENOMEM;
6876 /* The cpu_boot init_task->ret_stack will never be freed */
6877 for_each_online_cpu(cpu) {
6878 if (!idle_task(cpu)->ret_stack)
6879 ftrace_graph_init_idle_task(idle_task(cpu), cpu);
6882 do {
6883 ret = alloc_retstack_tasklist(ret_stack_list);
6884 } while (ret == -EAGAIN);
6886 if (!ret) {
6887 ret = register_trace_sched_switch(ftrace_graph_probe_sched_switch, NULL);
6888 if (ret)
6889 pr_info("ftrace_graph: Couldn't activate tracepoint"
6890 " probe to kernel_sched_switch\n");
6893 kfree(ret_stack_list);
6894 return ret;
6898 * Hibernation protection.
6899 * The state of the current task is too much unstable during
6900 * suspend/restore to disk. We want to protect against that.
6902 static int
6903 ftrace_suspend_notifier_call(struct notifier_block *bl, unsigned long state,
6904 void *unused)
6906 switch (state) {
6907 case PM_HIBERNATION_PREPARE:
6908 pause_graph_tracing();
6909 break;
6911 case PM_POST_HIBERNATION:
6912 unpause_graph_tracing();
6913 break;
6915 return NOTIFY_DONE;
6918 static int ftrace_graph_entry_test(struct ftrace_graph_ent *trace)
6920 if (!ftrace_ops_test(&global_ops, trace->func, NULL))
6921 return 0;
6922 return __ftrace_graph_entry(trace);
6926 * The function graph tracer should only trace the functions defined
6927 * by set_ftrace_filter and set_ftrace_notrace. If another function
6928 * tracer ops is registered, the graph tracer requires testing the
6929 * function against the global ops, and not just trace any function
6930 * that any ftrace_ops registered.
6932 static void update_function_graph_func(void)
6934 struct ftrace_ops *op;
6935 bool do_test = false;
6938 * The graph and global ops share the same set of functions
6939 * to test. If any other ops is on the list, then
6940 * the graph tracing needs to test if its the function
6941 * it should call.
6943 do_for_each_ftrace_op(op, ftrace_ops_list) {
6944 if (op != &global_ops && op != &graph_ops &&
6945 op != &ftrace_list_end) {
6946 do_test = true;
6947 /* in double loop, break out with goto */
6948 goto out;
6950 } while_for_each_ftrace_op(op);
6951 out:
6952 if (do_test)
6953 ftrace_graph_entry = ftrace_graph_entry_test;
6954 else
6955 ftrace_graph_entry = __ftrace_graph_entry;
6958 static struct notifier_block ftrace_suspend_notifier = {
6959 .notifier_call = ftrace_suspend_notifier_call,
6962 int register_ftrace_graph(trace_func_graph_ret_t retfunc,
6963 trace_func_graph_ent_t entryfunc)
6965 int ret = 0;
6967 mutex_lock(&ftrace_lock);
6969 /* we currently allow only one tracer registered at a time */
6970 if (ftrace_graph_active) {
6971 ret = -EBUSY;
6972 goto out;
6975 register_pm_notifier(&ftrace_suspend_notifier);
6977 ftrace_graph_active++;
6978 ret = start_graph_tracing();
6979 if (ret) {
6980 ftrace_graph_active--;
6981 goto out;
6984 ftrace_graph_return = retfunc;
6987 * Update the indirect function to the entryfunc, and the
6988 * function that gets called to the entry_test first. Then
6989 * call the update fgraph entry function to determine if
6990 * the entryfunc should be called directly or not.
6992 __ftrace_graph_entry = entryfunc;
6993 ftrace_graph_entry = ftrace_graph_entry_test;
6994 update_function_graph_func();
6996 ret = ftrace_startup(&graph_ops, FTRACE_START_FUNC_RET);
6997 out:
6998 mutex_unlock(&ftrace_lock);
6999 return ret;
7002 void unregister_ftrace_graph(void)
7004 mutex_lock(&ftrace_lock);
7006 if (unlikely(!ftrace_graph_active))
7007 goto out;
7009 ftrace_graph_active--;
7010 ftrace_graph_return = (trace_func_graph_ret_t)ftrace_stub;
7011 ftrace_graph_entry = ftrace_graph_entry_stub;
7012 __ftrace_graph_entry = ftrace_graph_entry_stub;
7013 ftrace_shutdown(&graph_ops, FTRACE_STOP_FUNC_RET);
7014 unregister_pm_notifier(&ftrace_suspend_notifier);
7015 unregister_trace_sched_switch(ftrace_graph_probe_sched_switch, NULL);
7017 out:
7018 mutex_unlock(&ftrace_lock);
7021 static DEFINE_PER_CPU(struct ftrace_ret_stack *, idle_ret_stack);
7023 static void
7024 graph_init_task(struct task_struct *t, struct ftrace_ret_stack *ret_stack)
7026 atomic_set(&t->tracing_graph_pause, 0);
7027 atomic_set(&t->trace_overrun, 0);
7028 t->ftrace_timestamp = 0;
7029 /* make curr_ret_stack visible before we add the ret_stack */
7030 smp_wmb();
7031 t->ret_stack = ret_stack;
7035 * Allocate a return stack for the idle task. May be the first
7036 * time through, or it may be done by CPU hotplug online.
7038 void ftrace_graph_init_idle_task(struct task_struct *t, int cpu)
7040 t->curr_ret_stack = -1;
7042 * The idle task has no parent, it either has its own
7043 * stack or no stack at all.
7045 if (t->ret_stack)
7046 WARN_ON(t->ret_stack != per_cpu(idle_ret_stack, cpu));
7048 if (ftrace_graph_active) {
7049 struct ftrace_ret_stack *ret_stack;
7051 ret_stack = per_cpu(idle_ret_stack, cpu);
7052 if (!ret_stack) {
7053 ret_stack =
7054 kmalloc_array(FTRACE_RETFUNC_DEPTH,
7055 sizeof(struct ftrace_ret_stack),
7056 GFP_KERNEL);
7057 if (!ret_stack)
7058 return;
7059 per_cpu(idle_ret_stack, cpu) = ret_stack;
7061 graph_init_task(t, ret_stack);
7065 /* Allocate a return stack for newly created task */
7066 void ftrace_graph_init_task(struct task_struct *t)
7068 /* Make sure we do not use the parent ret_stack */
7069 t->ret_stack = NULL;
7070 t->curr_ret_stack = -1;
7072 if (ftrace_graph_active) {
7073 struct ftrace_ret_stack *ret_stack;
7075 ret_stack = kmalloc_array(FTRACE_RETFUNC_DEPTH,
7076 sizeof(struct ftrace_ret_stack),
7077 GFP_KERNEL);
7078 if (!ret_stack)
7079 return;
7080 graph_init_task(t, ret_stack);
7084 void ftrace_graph_exit_task(struct task_struct *t)
7086 struct ftrace_ret_stack *ret_stack = t->ret_stack;
7088 t->ret_stack = NULL;
7089 /* NULL must become visible to IRQs before we free it: */
7090 barrier();
7092 kfree(ret_stack);
7094 #endif