Linux 4.19.168
[linux/fpc-iii.git] / kernel / trace / ftrace.c
blobcce526755471fb99aeb09ac0ecaa486333387a21
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>
37 #include <linux/kprobes.h>
39 #include <trace/events/sched.h>
41 #include <asm/sections.h>
42 #include <asm/setup.h>
44 #include "trace_output.h"
45 #include "trace_stat.h"
47 #define FTRACE_WARN_ON(cond) \
48 ({ \
49 int ___r = cond; \
50 if (WARN_ON(___r)) \
51 ftrace_kill(); \
52 ___r; \
55 #define FTRACE_WARN_ON_ONCE(cond) \
56 ({ \
57 int ___r = cond; \
58 if (WARN_ON_ONCE(___r)) \
59 ftrace_kill(); \
60 ___r; \
63 /* hash bits for specific function selection */
64 #define FTRACE_HASH_BITS 7
65 #define FTRACE_FUNC_HASHSIZE (1 << FTRACE_HASH_BITS)
66 #define FTRACE_HASH_DEFAULT_BITS 10
67 #define FTRACE_HASH_MAX_BITS 12
69 #ifdef CONFIG_DYNAMIC_FTRACE
70 #define INIT_OPS_HASH(opsname) \
71 .func_hash = &opsname.local_hash, \
72 .local_hash.regex_lock = __MUTEX_INITIALIZER(opsname.local_hash.regex_lock),
73 #define ASSIGN_OPS_HASH(opsname, val) \
74 .func_hash = val, \
75 .local_hash.regex_lock = __MUTEX_INITIALIZER(opsname.local_hash.regex_lock),
76 #else
77 #define INIT_OPS_HASH(opsname)
78 #define ASSIGN_OPS_HASH(opsname, val)
79 #endif
81 static struct ftrace_ops ftrace_list_end __read_mostly = {
82 .func = ftrace_stub,
83 .flags = FTRACE_OPS_FL_RECURSION_SAFE | FTRACE_OPS_FL_STUB,
84 INIT_OPS_HASH(ftrace_list_end)
87 /* ftrace_enabled is a method to turn ftrace on or off */
88 int ftrace_enabled __read_mostly;
89 static int last_ftrace_enabled;
91 /* Current function tracing op */
92 struct ftrace_ops *function_trace_op __read_mostly = &ftrace_list_end;
93 /* What to set function_trace_op to */
94 static struct ftrace_ops *set_function_trace_op;
96 static bool ftrace_pids_enabled(struct ftrace_ops *ops)
98 struct trace_array *tr;
100 if (!(ops->flags & FTRACE_OPS_FL_PID) || !ops->private)
101 return false;
103 tr = ops->private;
105 return tr->function_pids != NULL;
108 static void ftrace_update_trampoline(struct ftrace_ops *ops);
111 * ftrace_disabled is set when an anomaly is discovered.
112 * ftrace_disabled is much stronger than ftrace_enabled.
114 static int ftrace_disabled __read_mostly;
116 static DEFINE_MUTEX(ftrace_lock);
118 static struct ftrace_ops __rcu *ftrace_ops_list __read_mostly = &ftrace_list_end;
119 ftrace_func_t ftrace_trace_function __read_mostly = ftrace_stub;
120 static struct ftrace_ops global_ops;
122 #if ARCH_SUPPORTS_FTRACE_OPS
123 static void ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip,
124 struct ftrace_ops *op, struct pt_regs *regs);
125 #else
126 /* See comment below, where ftrace_ops_list_func is defined */
127 static void ftrace_ops_no_ops(unsigned long ip, unsigned long parent_ip);
128 #define ftrace_ops_list_func ((ftrace_func_t)ftrace_ops_no_ops)
129 #endif
132 * Traverse the ftrace_global_list, invoking all entries. The reason that we
133 * can use rcu_dereference_raw_notrace() is that elements removed from this list
134 * are simply leaked, so there is no need to interact with a grace-period
135 * mechanism. The rcu_dereference_raw_notrace() calls are needed to handle
136 * concurrent insertions into the ftrace_global_list.
138 * Silly Alpha and silly pointer-speculation compiler optimizations!
140 #define do_for_each_ftrace_op(op, list) \
141 op = rcu_dereference_raw_notrace(list); \
145 * Optimized for just a single item in the list (as that is the normal case).
147 #define while_for_each_ftrace_op(op) \
148 while (likely(op = rcu_dereference_raw_notrace((op)->next)) && \
149 unlikely((op) != &ftrace_list_end))
151 static inline void ftrace_ops_init(struct ftrace_ops *ops)
153 #ifdef CONFIG_DYNAMIC_FTRACE
154 if (!(ops->flags & FTRACE_OPS_FL_INITIALIZED)) {
155 mutex_init(&ops->local_hash.regex_lock);
156 ops->func_hash = &ops->local_hash;
157 ops->flags |= FTRACE_OPS_FL_INITIALIZED;
159 #endif
162 static void ftrace_pid_func(unsigned long ip, unsigned long parent_ip,
163 struct ftrace_ops *op, struct pt_regs *regs)
165 struct trace_array *tr = op->private;
167 if (tr && this_cpu_read(tr->trace_buffer.data->ftrace_ignore_pid))
168 return;
170 op->saved_func(ip, parent_ip, op, regs);
173 static void ftrace_sync(struct work_struct *work)
176 * This function is just a stub to implement a hard force
177 * of synchronize_sched(). This requires synchronizing
178 * tasks even in userspace and idle.
180 * Yes, function tracing is rude.
184 static void ftrace_sync_ipi(void *data)
186 /* Probably not needed, but do it anyway */
187 smp_rmb();
190 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
191 static void update_function_graph_func(void);
193 /* Both enabled by default (can be cleared by function_graph tracer flags */
194 static bool fgraph_sleep_time = true;
195 static bool fgraph_graph_time = true;
197 #else
198 static inline void update_function_graph_func(void) { }
199 #endif
202 static ftrace_func_t ftrace_ops_get_list_func(struct ftrace_ops *ops)
205 * If this is a dynamic, RCU, or per CPU ops, or we force list func,
206 * then it needs to call the list anyway.
208 if (ops->flags & (FTRACE_OPS_FL_DYNAMIC | FTRACE_OPS_FL_RCU) ||
209 FTRACE_FORCE_LIST_FUNC)
210 return ftrace_ops_list_func;
212 return ftrace_ops_get_func(ops);
215 static void update_ftrace_function(void)
217 ftrace_func_t func;
220 * Prepare the ftrace_ops that the arch callback will use.
221 * If there's only one ftrace_ops registered, the ftrace_ops_list
222 * will point to the ops we want.
224 set_function_trace_op = rcu_dereference_protected(ftrace_ops_list,
225 lockdep_is_held(&ftrace_lock));
227 /* If there's no ftrace_ops registered, just call the stub function */
228 if (set_function_trace_op == &ftrace_list_end) {
229 func = ftrace_stub;
232 * If we are at the end of the list and this ops is
233 * recursion safe and not dynamic and the arch supports passing ops,
234 * then have the mcount trampoline call the function directly.
236 } else if (rcu_dereference_protected(ftrace_ops_list->next,
237 lockdep_is_held(&ftrace_lock)) == &ftrace_list_end) {
238 func = ftrace_ops_get_list_func(ftrace_ops_list);
240 } else {
241 /* Just use the default ftrace_ops */
242 set_function_trace_op = &ftrace_list_end;
243 func = ftrace_ops_list_func;
246 update_function_graph_func();
248 /* If there's no change, then do nothing more here */
249 if (ftrace_trace_function == func)
250 return;
253 * If we are using the list function, it doesn't care
254 * about the function_trace_ops.
256 if (func == ftrace_ops_list_func) {
257 ftrace_trace_function = func;
259 * Don't even bother setting function_trace_ops,
260 * it would be racy to do so anyway.
262 return;
265 #ifndef CONFIG_DYNAMIC_FTRACE
267 * For static tracing, we need to be a bit more careful.
268 * The function change takes affect immediately. Thus,
269 * we need to coorditate the setting of the function_trace_ops
270 * with the setting of the ftrace_trace_function.
272 * Set the function to the list ops, which will call the
273 * function we want, albeit indirectly, but it handles the
274 * ftrace_ops and doesn't depend on function_trace_op.
276 ftrace_trace_function = ftrace_ops_list_func;
278 * Make sure all CPUs see this. Yes this is slow, but static
279 * tracing is slow and nasty to have enabled.
281 schedule_on_each_cpu(ftrace_sync);
282 /* Now all cpus are using the list ops. */
283 function_trace_op = set_function_trace_op;
284 /* Make sure the function_trace_op is visible on all CPUs */
285 smp_wmb();
286 /* Nasty way to force a rmb on all cpus */
287 smp_call_function(ftrace_sync_ipi, NULL, 1);
288 /* OK, we are all set to update the ftrace_trace_function now! */
289 #endif /* !CONFIG_DYNAMIC_FTRACE */
291 ftrace_trace_function = func;
294 static void add_ftrace_ops(struct ftrace_ops __rcu **list,
295 struct ftrace_ops *ops)
297 rcu_assign_pointer(ops->next, *list);
300 * We are entering ops into the list but another
301 * CPU might be walking that list. We need to make sure
302 * the ops->next pointer is valid before another CPU sees
303 * the ops pointer included into the list.
305 rcu_assign_pointer(*list, ops);
308 static int remove_ftrace_ops(struct ftrace_ops __rcu **list,
309 struct ftrace_ops *ops)
311 struct ftrace_ops **p;
314 * If we are removing the last function, then simply point
315 * to the ftrace_stub.
317 if (rcu_dereference_protected(*list,
318 lockdep_is_held(&ftrace_lock)) == ops &&
319 rcu_dereference_protected(ops->next,
320 lockdep_is_held(&ftrace_lock)) == &ftrace_list_end) {
321 *list = &ftrace_list_end;
322 return 0;
325 for (p = list; *p != &ftrace_list_end; p = &(*p)->next)
326 if (*p == ops)
327 break;
329 if (*p != ops)
330 return -1;
332 *p = (*p)->next;
333 return 0;
336 static void ftrace_update_trampoline(struct ftrace_ops *ops);
338 static int __register_ftrace_function(struct ftrace_ops *ops)
340 if (ops->flags & FTRACE_OPS_FL_DELETED)
341 return -EINVAL;
343 if (WARN_ON(ops->flags & FTRACE_OPS_FL_ENABLED))
344 return -EBUSY;
346 #ifndef CONFIG_DYNAMIC_FTRACE_WITH_REGS
348 * If the ftrace_ops specifies SAVE_REGS, then it only can be used
349 * if the arch supports it, or SAVE_REGS_IF_SUPPORTED is also set.
350 * Setting SAVE_REGS_IF_SUPPORTED makes SAVE_REGS irrelevant.
352 if (ops->flags & FTRACE_OPS_FL_SAVE_REGS &&
353 !(ops->flags & FTRACE_OPS_FL_SAVE_REGS_IF_SUPPORTED))
354 return -EINVAL;
356 if (ops->flags & FTRACE_OPS_FL_SAVE_REGS_IF_SUPPORTED)
357 ops->flags |= FTRACE_OPS_FL_SAVE_REGS;
358 #endif
360 if (!core_kernel_data((unsigned long)ops))
361 ops->flags |= FTRACE_OPS_FL_DYNAMIC;
363 add_ftrace_ops(&ftrace_ops_list, ops);
365 /* Always save the function, and reset at unregistering */
366 ops->saved_func = ops->func;
368 if (ftrace_pids_enabled(ops))
369 ops->func = ftrace_pid_func;
371 ftrace_update_trampoline(ops);
373 if (ftrace_enabled)
374 update_ftrace_function();
376 return 0;
379 static int __unregister_ftrace_function(struct ftrace_ops *ops)
381 int ret;
383 if (WARN_ON(!(ops->flags & FTRACE_OPS_FL_ENABLED)))
384 return -EBUSY;
386 ret = remove_ftrace_ops(&ftrace_ops_list, ops);
388 if (ret < 0)
389 return ret;
391 if (ftrace_enabled)
392 update_ftrace_function();
394 ops->func = ops->saved_func;
396 return 0;
399 static void ftrace_update_pid_func(void)
401 struct ftrace_ops *op;
403 /* Only do something if we are tracing something */
404 if (ftrace_trace_function == ftrace_stub)
405 return;
407 do_for_each_ftrace_op(op, ftrace_ops_list) {
408 if (op->flags & FTRACE_OPS_FL_PID) {
409 op->func = ftrace_pids_enabled(op) ?
410 ftrace_pid_func : op->saved_func;
411 ftrace_update_trampoline(op);
413 } while_for_each_ftrace_op(op);
415 update_ftrace_function();
418 #ifdef CONFIG_FUNCTION_PROFILER
419 struct ftrace_profile {
420 struct hlist_node node;
421 unsigned long ip;
422 unsigned long counter;
423 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
424 unsigned long long time;
425 unsigned long long time_squared;
426 #endif
429 struct ftrace_profile_page {
430 struct ftrace_profile_page *next;
431 unsigned long index;
432 struct ftrace_profile records[];
435 struct ftrace_profile_stat {
436 atomic_t disabled;
437 struct hlist_head *hash;
438 struct ftrace_profile_page *pages;
439 struct ftrace_profile_page *start;
440 struct tracer_stat stat;
443 #define PROFILE_RECORDS_SIZE \
444 (PAGE_SIZE - offsetof(struct ftrace_profile_page, records))
446 #define PROFILES_PER_PAGE \
447 (PROFILE_RECORDS_SIZE / sizeof(struct ftrace_profile))
449 static int ftrace_profile_enabled __read_mostly;
451 /* ftrace_profile_lock - synchronize the enable and disable of the profiler */
452 static DEFINE_MUTEX(ftrace_profile_lock);
454 static DEFINE_PER_CPU(struct ftrace_profile_stat, ftrace_profile_stats);
456 #define FTRACE_PROFILE_HASH_BITS 10
457 #define FTRACE_PROFILE_HASH_SIZE (1 << FTRACE_PROFILE_HASH_BITS)
459 static void *
460 function_stat_next(void *v, int idx)
462 struct ftrace_profile *rec = v;
463 struct ftrace_profile_page *pg;
465 pg = (struct ftrace_profile_page *)((unsigned long)rec & PAGE_MASK);
467 again:
468 if (idx != 0)
469 rec++;
471 if ((void *)rec >= (void *)&pg->records[pg->index]) {
472 pg = pg->next;
473 if (!pg)
474 return NULL;
475 rec = &pg->records[0];
476 if (!rec->counter)
477 goto again;
480 return rec;
483 static void *function_stat_start(struct tracer_stat *trace)
485 struct ftrace_profile_stat *stat =
486 container_of(trace, struct ftrace_profile_stat, stat);
488 if (!stat || !stat->start)
489 return NULL;
491 return function_stat_next(&stat->start->records[0], 0);
494 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
495 /* function graph compares on total time */
496 static int function_stat_cmp(void *p1, void *p2)
498 struct ftrace_profile *a = p1;
499 struct ftrace_profile *b = p2;
501 if (a->time < b->time)
502 return -1;
503 if (a->time > b->time)
504 return 1;
505 else
506 return 0;
508 #else
509 /* not function graph compares against hits */
510 static int function_stat_cmp(void *p1, void *p2)
512 struct ftrace_profile *a = p1;
513 struct ftrace_profile *b = p2;
515 if (a->counter < b->counter)
516 return -1;
517 if (a->counter > b->counter)
518 return 1;
519 else
520 return 0;
522 #endif
524 static int function_stat_headers(struct seq_file *m)
526 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
527 seq_puts(m, " Function "
528 "Hit Time Avg s^2\n"
529 " -------- "
530 "--- ---- --- ---\n");
531 #else
532 seq_puts(m, " Function Hit\n"
533 " -------- ---\n");
534 #endif
535 return 0;
538 static int function_stat_show(struct seq_file *m, void *v)
540 struct ftrace_profile *rec = v;
541 char str[KSYM_SYMBOL_LEN];
542 int ret = 0;
543 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
544 static struct trace_seq s;
545 unsigned long long avg;
546 unsigned long long stddev;
547 #endif
548 mutex_lock(&ftrace_profile_lock);
550 /* we raced with function_profile_reset() */
551 if (unlikely(rec->counter == 0)) {
552 ret = -EBUSY;
553 goto out;
556 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
557 avg = div64_ul(rec->time, 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 stddev = div64_ul(stddev,
584 rec->counter * (rec->counter - 1) * 1000);
587 trace_seq_init(&s);
588 trace_print_graph_duration(rec->time, &s);
589 trace_seq_puts(&s, " ");
590 trace_print_graph_duration(avg, &s);
591 trace_seq_puts(&s, " ");
592 trace_print_graph_duration(stddev, &s);
593 trace_print_seq(m, &s);
594 #endif
595 seq_putc(m, '\n');
596 out:
597 mutex_unlock(&ftrace_profile_lock);
599 return ret;
602 static void ftrace_profile_reset(struct ftrace_profile_stat *stat)
604 struct ftrace_profile_page *pg;
606 pg = stat->pages = stat->start;
608 while (pg) {
609 memset(pg->records, 0, PROFILE_RECORDS_SIZE);
610 pg->index = 0;
611 pg = pg->next;
614 memset(stat->hash, 0,
615 FTRACE_PROFILE_HASH_SIZE * sizeof(struct hlist_head));
618 int ftrace_profile_pages_init(struct ftrace_profile_stat *stat)
620 struct ftrace_profile_page *pg;
621 int functions;
622 int pages;
623 int i;
625 /* If we already allocated, do nothing */
626 if (stat->pages)
627 return 0;
629 stat->pages = (void *)get_zeroed_page(GFP_KERNEL);
630 if (!stat->pages)
631 return -ENOMEM;
633 #ifdef CONFIG_DYNAMIC_FTRACE
634 functions = ftrace_update_tot_cnt;
635 #else
637 * We do not know the number of functions that exist because
638 * dynamic tracing is what counts them. With past experience
639 * we have around 20K functions. That should be more than enough.
640 * It is highly unlikely we will execute every function in
641 * the kernel.
643 functions = 20000;
644 #endif
646 pg = stat->start = stat->pages;
648 pages = DIV_ROUND_UP(functions, PROFILES_PER_PAGE);
650 for (i = 1; i < pages; i++) {
651 pg->next = (void *)get_zeroed_page(GFP_KERNEL);
652 if (!pg->next)
653 goto out_free;
654 pg = pg->next;
657 return 0;
659 out_free:
660 pg = stat->start;
661 while (pg) {
662 unsigned long tmp = (unsigned long)pg;
664 pg = pg->next;
665 free_page(tmp);
668 stat->pages = NULL;
669 stat->start = NULL;
671 return -ENOMEM;
674 static int ftrace_profile_init_cpu(int cpu)
676 struct ftrace_profile_stat *stat;
677 int size;
679 stat = &per_cpu(ftrace_profile_stats, cpu);
681 if (stat->hash) {
682 /* If the profile is already created, simply reset it */
683 ftrace_profile_reset(stat);
684 return 0;
688 * We are profiling all functions, but usually only a few thousand
689 * functions are hit. We'll make a hash of 1024 items.
691 size = FTRACE_PROFILE_HASH_SIZE;
693 stat->hash = kcalloc(size, sizeof(struct hlist_head), GFP_KERNEL);
695 if (!stat->hash)
696 return -ENOMEM;
698 /* Preallocate the function profiling pages */
699 if (ftrace_profile_pages_init(stat) < 0) {
700 kfree(stat->hash);
701 stat->hash = NULL;
702 return -ENOMEM;
705 return 0;
708 static int ftrace_profile_init(void)
710 int cpu;
711 int ret = 0;
713 for_each_possible_cpu(cpu) {
714 ret = ftrace_profile_init_cpu(cpu);
715 if (ret)
716 break;
719 return ret;
722 /* interrupts must be disabled */
723 static struct ftrace_profile *
724 ftrace_find_profiled_func(struct ftrace_profile_stat *stat, unsigned long ip)
726 struct ftrace_profile *rec;
727 struct hlist_head *hhd;
728 unsigned long key;
730 key = hash_long(ip, FTRACE_PROFILE_HASH_BITS);
731 hhd = &stat->hash[key];
733 if (hlist_empty(hhd))
734 return NULL;
736 hlist_for_each_entry_rcu_notrace(rec, hhd, node) {
737 if (rec->ip == ip)
738 return rec;
741 return NULL;
744 static void ftrace_add_profile(struct ftrace_profile_stat *stat,
745 struct ftrace_profile *rec)
747 unsigned long key;
749 key = hash_long(rec->ip, FTRACE_PROFILE_HASH_BITS);
750 hlist_add_head_rcu(&rec->node, &stat->hash[key]);
754 * The memory is already allocated, this simply finds a new record to use.
756 static struct ftrace_profile *
757 ftrace_profile_alloc(struct ftrace_profile_stat *stat, unsigned long ip)
759 struct ftrace_profile *rec = NULL;
761 /* prevent recursion (from NMIs) */
762 if (atomic_inc_return(&stat->disabled) != 1)
763 goto out;
766 * Try to find the function again since an NMI
767 * could have added it
769 rec = ftrace_find_profiled_func(stat, ip);
770 if (rec)
771 goto out;
773 if (stat->pages->index == PROFILES_PER_PAGE) {
774 if (!stat->pages->next)
775 goto out;
776 stat->pages = stat->pages->next;
779 rec = &stat->pages->records[stat->pages->index++];
780 rec->ip = ip;
781 ftrace_add_profile(stat, rec);
783 out:
784 atomic_dec(&stat->disabled);
786 return rec;
789 static void
790 function_profile_call(unsigned long ip, unsigned long parent_ip,
791 struct ftrace_ops *ops, struct pt_regs *regs)
793 struct ftrace_profile_stat *stat;
794 struct ftrace_profile *rec;
795 unsigned long flags;
797 if (!ftrace_profile_enabled)
798 return;
800 local_irq_save(flags);
802 stat = this_cpu_ptr(&ftrace_profile_stats);
803 if (!stat->hash || !ftrace_profile_enabled)
804 goto out;
806 rec = ftrace_find_profiled_func(stat, ip);
807 if (!rec) {
808 rec = ftrace_profile_alloc(stat, ip);
809 if (!rec)
810 goto out;
813 rec->counter++;
814 out:
815 local_irq_restore(flags);
818 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
819 static int profile_graph_entry(struct ftrace_graph_ent *trace)
821 int index = current->curr_ret_stack;
823 function_profile_call(trace->func, 0, NULL, NULL);
825 /* If function graph is shutting down, ret_stack can be NULL */
826 if (!current->ret_stack)
827 return 0;
829 if (index >= 0 && index < FTRACE_RETFUNC_DEPTH)
830 current->ret_stack[index].subtime = 0;
832 return 1;
835 static void profile_graph_return(struct ftrace_graph_ret *trace)
837 struct ftrace_profile_stat *stat;
838 unsigned long long calltime;
839 struct ftrace_profile *rec;
840 unsigned long flags;
842 local_irq_save(flags);
843 stat = this_cpu_ptr(&ftrace_profile_stats);
844 if (!stat->hash || !ftrace_profile_enabled)
845 goto out;
847 /* If the calltime was zero'd ignore it */
848 if (!trace->calltime)
849 goto out;
851 calltime = trace->rettime - trace->calltime;
853 if (!fgraph_graph_time) {
854 int index;
856 index = current->curr_ret_stack;
858 /* Append this call time to the parent time to subtract */
859 if (index)
860 current->ret_stack[index - 1].subtime += calltime;
862 if (current->ret_stack[index].subtime < calltime)
863 calltime -= current->ret_stack[index].subtime;
864 else
865 calltime = 0;
868 rec = ftrace_find_profiled_func(stat, trace->func);
869 if (rec) {
870 rec->time += calltime;
871 rec->time_squared += calltime * calltime;
874 out:
875 local_irq_restore(flags);
878 static int register_ftrace_profiler(void)
880 return register_ftrace_graph(&profile_graph_return,
881 &profile_graph_entry);
884 static void unregister_ftrace_profiler(void)
886 unregister_ftrace_graph();
888 #else
889 static struct ftrace_ops ftrace_profile_ops __read_mostly = {
890 .func = function_profile_call,
891 .flags = FTRACE_OPS_FL_RECURSION_SAFE | FTRACE_OPS_FL_INITIALIZED,
892 INIT_OPS_HASH(ftrace_profile_ops)
895 static int register_ftrace_profiler(void)
897 return register_ftrace_function(&ftrace_profile_ops);
900 static void unregister_ftrace_profiler(void)
902 unregister_ftrace_function(&ftrace_profile_ops);
904 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
906 static ssize_t
907 ftrace_profile_write(struct file *filp, const char __user *ubuf,
908 size_t cnt, loff_t *ppos)
910 unsigned long val;
911 int ret;
913 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
914 if (ret)
915 return ret;
917 val = !!val;
919 mutex_lock(&ftrace_profile_lock);
920 if (ftrace_profile_enabled ^ val) {
921 if (val) {
922 ret = ftrace_profile_init();
923 if (ret < 0) {
924 cnt = ret;
925 goto out;
928 ret = register_ftrace_profiler();
929 if (ret < 0) {
930 cnt = ret;
931 goto out;
933 ftrace_profile_enabled = 1;
934 } else {
935 ftrace_profile_enabled = 0;
937 * unregister_ftrace_profiler calls stop_machine
938 * so this acts like an synchronize_sched.
940 unregister_ftrace_profiler();
943 out:
944 mutex_unlock(&ftrace_profile_lock);
946 *ppos += cnt;
948 return cnt;
951 static ssize_t
952 ftrace_profile_read(struct file *filp, char __user *ubuf,
953 size_t cnt, loff_t *ppos)
955 char buf[64]; /* big enough to hold a number */
956 int r;
958 r = sprintf(buf, "%u\n", ftrace_profile_enabled);
959 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
962 static const struct file_operations ftrace_profile_fops = {
963 .open = tracing_open_generic,
964 .read = ftrace_profile_read,
965 .write = ftrace_profile_write,
966 .llseek = default_llseek,
969 /* used to initialize the real stat files */
970 static struct tracer_stat function_stats __initdata = {
971 .name = "functions",
972 .stat_start = function_stat_start,
973 .stat_next = function_stat_next,
974 .stat_cmp = function_stat_cmp,
975 .stat_headers = function_stat_headers,
976 .stat_show = function_stat_show
979 static __init void ftrace_profile_tracefs(struct dentry *d_tracer)
981 struct ftrace_profile_stat *stat;
982 struct dentry *entry;
983 char *name;
984 int ret;
985 int cpu;
987 for_each_possible_cpu(cpu) {
988 stat = &per_cpu(ftrace_profile_stats, cpu);
990 name = kasprintf(GFP_KERNEL, "function%d", cpu);
991 if (!name) {
993 * The files created are permanent, if something happens
994 * we still do not free memory.
996 WARN(1,
997 "Could not allocate stat file for cpu %d\n",
998 cpu);
999 return;
1001 stat->stat = function_stats;
1002 stat->stat.name = name;
1003 ret = register_stat_tracer(&stat->stat);
1004 if (ret) {
1005 WARN(1,
1006 "Could not register function stat for cpu %d\n",
1007 cpu);
1008 kfree(name);
1009 return;
1013 entry = tracefs_create_file("function_profile_enabled", 0644,
1014 d_tracer, NULL, &ftrace_profile_fops);
1015 if (!entry)
1016 pr_warn("Could not create tracefs 'function_profile_enabled' entry\n");
1019 #else /* CONFIG_FUNCTION_PROFILER */
1020 static __init void ftrace_profile_tracefs(struct dentry *d_tracer)
1023 #endif /* CONFIG_FUNCTION_PROFILER */
1025 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
1026 static int ftrace_graph_active;
1027 #else
1028 # define ftrace_graph_active 0
1029 #endif
1031 #ifdef CONFIG_DYNAMIC_FTRACE
1033 static struct ftrace_ops *removed_ops;
1036 * Set when doing a global update, like enabling all recs or disabling them.
1037 * It is not set when just updating a single ftrace_ops.
1039 static bool update_all_ops;
1041 #ifndef CONFIG_FTRACE_MCOUNT_RECORD
1042 # error Dynamic ftrace depends on MCOUNT_RECORD
1043 #endif
1045 struct ftrace_func_entry {
1046 struct hlist_node hlist;
1047 unsigned long ip;
1050 struct ftrace_func_probe {
1051 struct ftrace_probe_ops *probe_ops;
1052 struct ftrace_ops ops;
1053 struct trace_array *tr;
1054 struct list_head list;
1055 void *data;
1056 int ref;
1060 * We make these constant because no one should touch them,
1061 * but they are used as the default "empty hash", to avoid allocating
1062 * it all the time. These are in a read only section such that if
1063 * anyone does try to modify it, it will cause an exception.
1065 static const struct hlist_head empty_buckets[1];
1066 static const struct ftrace_hash empty_hash = {
1067 .buckets = (struct hlist_head *)empty_buckets,
1069 #define EMPTY_HASH ((struct ftrace_hash *)&empty_hash)
1071 static struct ftrace_ops global_ops = {
1072 .func = ftrace_stub,
1073 .local_hash.notrace_hash = EMPTY_HASH,
1074 .local_hash.filter_hash = EMPTY_HASH,
1075 INIT_OPS_HASH(global_ops)
1076 .flags = FTRACE_OPS_FL_RECURSION_SAFE |
1077 FTRACE_OPS_FL_INITIALIZED |
1078 FTRACE_OPS_FL_PID,
1082 * Used by the stack undwinder to know about dynamic ftrace trampolines.
1084 struct ftrace_ops *ftrace_ops_trampoline(unsigned long addr)
1086 struct ftrace_ops *op = NULL;
1089 * Some of the ops may be dynamically allocated,
1090 * they are freed after a synchronize_sched().
1092 preempt_disable_notrace();
1094 do_for_each_ftrace_op(op, ftrace_ops_list) {
1096 * This is to check for dynamically allocated trampolines.
1097 * Trampolines that are in kernel text will have
1098 * core_kernel_text() return true.
1100 if (op->trampoline && op->trampoline_size)
1101 if (addr >= op->trampoline &&
1102 addr < op->trampoline + op->trampoline_size) {
1103 preempt_enable_notrace();
1104 return op;
1106 } while_for_each_ftrace_op(op);
1107 preempt_enable_notrace();
1109 return NULL;
1113 * This is used by __kernel_text_address() to return true if the
1114 * address is on a dynamically allocated trampoline that would
1115 * not return true for either core_kernel_text() or
1116 * is_module_text_address().
1118 bool is_ftrace_trampoline(unsigned long addr)
1120 return ftrace_ops_trampoline(addr) != NULL;
1123 struct ftrace_page {
1124 struct ftrace_page *next;
1125 struct dyn_ftrace *records;
1126 int index;
1127 int size;
1130 #define ENTRY_SIZE sizeof(struct dyn_ftrace)
1131 #define ENTRIES_PER_PAGE (PAGE_SIZE / ENTRY_SIZE)
1133 /* estimate from running different kernels */
1134 #define NR_TO_INIT 10000
1136 static struct ftrace_page *ftrace_pages_start;
1137 static struct ftrace_page *ftrace_pages;
1139 static __always_inline unsigned long
1140 ftrace_hash_key(struct ftrace_hash *hash, unsigned long ip)
1142 if (hash->size_bits > 0)
1143 return hash_long(ip, hash->size_bits);
1145 return 0;
1148 /* Only use this function if ftrace_hash_empty() has already been tested */
1149 static __always_inline struct ftrace_func_entry *
1150 __ftrace_lookup_ip(struct ftrace_hash *hash, unsigned long ip)
1152 unsigned long key;
1153 struct ftrace_func_entry *entry;
1154 struct hlist_head *hhd;
1156 key = ftrace_hash_key(hash, ip);
1157 hhd = &hash->buckets[key];
1159 hlist_for_each_entry_rcu_notrace(entry, hhd, hlist) {
1160 if (entry->ip == ip)
1161 return entry;
1163 return NULL;
1167 * ftrace_lookup_ip - Test to see if an ip exists in an ftrace_hash
1168 * @hash: The hash to look at
1169 * @ip: The instruction pointer to test
1171 * Search a given @hash to see if a given instruction pointer (@ip)
1172 * exists in it.
1174 * Returns the entry that holds the @ip if found. NULL otherwise.
1176 struct ftrace_func_entry *
1177 ftrace_lookup_ip(struct ftrace_hash *hash, unsigned long ip)
1179 if (ftrace_hash_empty(hash))
1180 return NULL;
1182 return __ftrace_lookup_ip(hash, ip);
1185 static void __add_hash_entry(struct ftrace_hash *hash,
1186 struct ftrace_func_entry *entry)
1188 struct hlist_head *hhd;
1189 unsigned long key;
1191 key = ftrace_hash_key(hash, entry->ip);
1192 hhd = &hash->buckets[key];
1193 hlist_add_head(&entry->hlist, hhd);
1194 hash->count++;
1197 static int add_hash_entry(struct ftrace_hash *hash, unsigned long ip)
1199 struct ftrace_func_entry *entry;
1201 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
1202 if (!entry)
1203 return -ENOMEM;
1205 entry->ip = ip;
1206 __add_hash_entry(hash, entry);
1208 return 0;
1211 static void
1212 free_hash_entry(struct ftrace_hash *hash,
1213 struct ftrace_func_entry *entry)
1215 hlist_del(&entry->hlist);
1216 kfree(entry);
1217 hash->count--;
1220 static void
1221 remove_hash_entry(struct ftrace_hash *hash,
1222 struct ftrace_func_entry *entry)
1224 hlist_del_rcu(&entry->hlist);
1225 hash->count--;
1228 static void ftrace_hash_clear(struct ftrace_hash *hash)
1230 struct hlist_head *hhd;
1231 struct hlist_node *tn;
1232 struct ftrace_func_entry *entry;
1233 int size = 1 << hash->size_bits;
1234 int i;
1236 if (!hash->count)
1237 return;
1239 for (i = 0; i < size; i++) {
1240 hhd = &hash->buckets[i];
1241 hlist_for_each_entry_safe(entry, tn, hhd, hlist)
1242 free_hash_entry(hash, entry);
1244 FTRACE_WARN_ON(hash->count);
1247 static void free_ftrace_mod(struct ftrace_mod_load *ftrace_mod)
1249 list_del(&ftrace_mod->list);
1250 kfree(ftrace_mod->module);
1251 kfree(ftrace_mod->func);
1252 kfree(ftrace_mod);
1255 static void clear_ftrace_mod_list(struct list_head *head)
1257 struct ftrace_mod_load *p, *n;
1259 /* stack tracer isn't supported yet */
1260 if (!head)
1261 return;
1263 mutex_lock(&ftrace_lock);
1264 list_for_each_entry_safe(p, n, head, list)
1265 free_ftrace_mod(p);
1266 mutex_unlock(&ftrace_lock);
1269 static void free_ftrace_hash(struct ftrace_hash *hash)
1271 if (!hash || hash == EMPTY_HASH)
1272 return;
1273 ftrace_hash_clear(hash);
1274 kfree(hash->buckets);
1275 kfree(hash);
1278 static void __free_ftrace_hash_rcu(struct rcu_head *rcu)
1280 struct ftrace_hash *hash;
1282 hash = container_of(rcu, struct ftrace_hash, rcu);
1283 free_ftrace_hash(hash);
1286 static void free_ftrace_hash_rcu(struct ftrace_hash *hash)
1288 if (!hash || hash == EMPTY_HASH)
1289 return;
1290 call_rcu_sched(&hash->rcu, __free_ftrace_hash_rcu);
1293 void ftrace_free_filter(struct ftrace_ops *ops)
1295 ftrace_ops_init(ops);
1296 free_ftrace_hash(ops->func_hash->filter_hash);
1297 free_ftrace_hash(ops->func_hash->notrace_hash);
1300 static struct ftrace_hash *alloc_ftrace_hash(int size_bits)
1302 struct ftrace_hash *hash;
1303 int size;
1305 hash = kzalloc(sizeof(*hash), GFP_KERNEL);
1306 if (!hash)
1307 return NULL;
1309 size = 1 << size_bits;
1310 hash->buckets = kcalloc(size, sizeof(*hash->buckets), GFP_KERNEL);
1312 if (!hash->buckets) {
1313 kfree(hash);
1314 return NULL;
1317 hash->size_bits = size_bits;
1319 return hash;
1323 static int ftrace_add_mod(struct trace_array *tr,
1324 const char *func, const char *module,
1325 int enable)
1327 struct ftrace_mod_load *ftrace_mod;
1328 struct list_head *mod_head = enable ? &tr->mod_trace : &tr->mod_notrace;
1330 ftrace_mod = kzalloc(sizeof(*ftrace_mod), GFP_KERNEL);
1331 if (!ftrace_mod)
1332 return -ENOMEM;
1334 ftrace_mod->func = kstrdup(func, GFP_KERNEL);
1335 ftrace_mod->module = kstrdup(module, GFP_KERNEL);
1336 ftrace_mod->enable = enable;
1338 if (!ftrace_mod->func || !ftrace_mod->module)
1339 goto out_free;
1341 list_add(&ftrace_mod->list, mod_head);
1343 return 0;
1345 out_free:
1346 free_ftrace_mod(ftrace_mod);
1348 return -ENOMEM;
1351 static struct ftrace_hash *
1352 alloc_and_copy_ftrace_hash(int size_bits, struct ftrace_hash *hash)
1354 struct ftrace_func_entry *entry;
1355 struct ftrace_hash *new_hash;
1356 int size;
1357 int ret;
1358 int i;
1360 new_hash = alloc_ftrace_hash(size_bits);
1361 if (!new_hash)
1362 return NULL;
1364 if (hash)
1365 new_hash->flags = hash->flags;
1367 /* Empty hash? */
1368 if (ftrace_hash_empty(hash))
1369 return new_hash;
1371 size = 1 << hash->size_bits;
1372 for (i = 0; i < size; i++) {
1373 hlist_for_each_entry(entry, &hash->buckets[i], hlist) {
1374 ret = add_hash_entry(new_hash, entry->ip);
1375 if (ret < 0)
1376 goto free_hash;
1380 FTRACE_WARN_ON(new_hash->count != hash->count);
1382 return new_hash;
1384 free_hash:
1385 free_ftrace_hash(new_hash);
1386 return NULL;
1389 static void
1390 ftrace_hash_rec_disable_modify(struct ftrace_ops *ops, int filter_hash);
1391 static void
1392 ftrace_hash_rec_enable_modify(struct ftrace_ops *ops, int filter_hash);
1394 static int ftrace_hash_ipmodify_update(struct ftrace_ops *ops,
1395 struct ftrace_hash *new_hash);
1397 static struct ftrace_hash *
1398 __ftrace_hash_move(struct ftrace_hash *src)
1400 struct ftrace_func_entry *entry;
1401 struct hlist_node *tn;
1402 struct hlist_head *hhd;
1403 struct ftrace_hash *new_hash;
1404 int size = src->count;
1405 int bits = 0;
1406 int i;
1409 * If the new source is empty, just return the empty_hash.
1411 if (ftrace_hash_empty(src))
1412 return EMPTY_HASH;
1415 * Make the hash size about 1/2 the # found
1417 for (size /= 2; size; size >>= 1)
1418 bits++;
1420 /* Don't allocate too much */
1421 if (bits > FTRACE_HASH_MAX_BITS)
1422 bits = FTRACE_HASH_MAX_BITS;
1424 new_hash = alloc_ftrace_hash(bits);
1425 if (!new_hash)
1426 return NULL;
1428 new_hash->flags = src->flags;
1430 size = 1 << src->size_bits;
1431 for (i = 0; i < size; i++) {
1432 hhd = &src->buckets[i];
1433 hlist_for_each_entry_safe(entry, tn, hhd, hlist) {
1434 remove_hash_entry(src, entry);
1435 __add_hash_entry(new_hash, entry);
1439 return new_hash;
1442 static int
1443 ftrace_hash_move(struct ftrace_ops *ops, int enable,
1444 struct ftrace_hash **dst, struct ftrace_hash *src)
1446 struct ftrace_hash *new_hash;
1447 int ret;
1449 /* Reject setting notrace hash on IPMODIFY ftrace_ops */
1450 if (ops->flags & FTRACE_OPS_FL_IPMODIFY && !enable)
1451 return -EINVAL;
1453 new_hash = __ftrace_hash_move(src);
1454 if (!new_hash)
1455 return -ENOMEM;
1457 /* Make sure this can be applied if it is IPMODIFY ftrace_ops */
1458 if (enable) {
1459 /* IPMODIFY should be updated only when filter_hash updating */
1460 ret = ftrace_hash_ipmodify_update(ops, new_hash);
1461 if (ret < 0) {
1462 free_ftrace_hash(new_hash);
1463 return ret;
1468 * Remove the current set, update the hash and add
1469 * them back.
1471 ftrace_hash_rec_disable_modify(ops, enable);
1473 rcu_assign_pointer(*dst, new_hash);
1475 ftrace_hash_rec_enable_modify(ops, enable);
1477 return 0;
1480 static bool hash_contains_ip(unsigned long ip,
1481 struct ftrace_ops_hash *hash)
1484 * The function record is a match if it exists in the filter
1485 * hash and not in the notrace hash. Note, an emty hash is
1486 * considered a match for the filter hash, but an empty
1487 * notrace hash is considered not in the notrace hash.
1489 return (ftrace_hash_empty(hash->filter_hash) ||
1490 __ftrace_lookup_ip(hash->filter_hash, ip)) &&
1491 (ftrace_hash_empty(hash->notrace_hash) ||
1492 !__ftrace_lookup_ip(hash->notrace_hash, ip));
1496 * Test the hashes for this ops to see if we want to call
1497 * the ops->func or not.
1499 * It's a match if the ip is in the ops->filter_hash or
1500 * the filter_hash does not exist or is empty,
1501 * AND
1502 * the ip is not in the ops->notrace_hash.
1504 * This needs to be called with preemption disabled as
1505 * the hashes are freed with call_rcu_sched().
1507 static int
1508 ftrace_ops_test(struct ftrace_ops *ops, unsigned long ip, void *regs)
1510 struct ftrace_ops_hash hash;
1511 int ret;
1513 #ifdef CONFIG_DYNAMIC_FTRACE_WITH_REGS
1515 * There's a small race when adding ops that the ftrace handler
1516 * that wants regs, may be called without them. We can not
1517 * allow that handler to be called if regs is NULL.
1519 if (regs == NULL && (ops->flags & FTRACE_OPS_FL_SAVE_REGS))
1520 return 0;
1521 #endif
1523 rcu_assign_pointer(hash.filter_hash, ops->func_hash->filter_hash);
1524 rcu_assign_pointer(hash.notrace_hash, ops->func_hash->notrace_hash);
1526 if (hash_contains_ip(ip, &hash))
1527 ret = 1;
1528 else
1529 ret = 0;
1531 return ret;
1535 * This is a double for. Do not use 'break' to break out of the loop,
1536 * you must use a goto.
1538 #define do_for_each_ftrace_rec(pg, rec) \
1539 for (pg = ftrace_pages_start; pg; pg = pg->next) { \
1540 int _____i; \
1541 for (_____i = 0; _____i < pg->index; _____i++) { \
1542 rec = &pg->records[_____i];
1544 #define while_for_each_ftrace_rec() \
1549 static int ftrace_cmp_recs(const void *a, const void *b)
1551 const struct dyn_ftrace *key = a;
1552 const struct dyn_ftrace *rec = b;
1554 if (key->flags < rec->ip)
1555 return -1;
1556 if (key->ip >= rec->ip + MCOUNT_INSN_SIZE)
1557 return 1;
1558 return 0;
1562 * ftrace_location_range - return the first address of a traced location
1563 * if it touches the given ip range
1564 * @start: start of range to search.
1565 * @end: end of range to search (inclusive). @end points to the last byte
1566 * to check.
1568 * Returns rec->ip if the related ftrace location is a least partly within
1569 * the given address range. That is, the first address of the instruction
1570 * that is either a NOP or call to the function tracer. It checks the ftrace
1571 * internal tables to determine if the address belongs or not.
1573 unsigned long ftrace_location_range(unsigned long start, unsigned long end)
1575 struct ftrace_page *pg;
1576 struct dyn_ftrace *rec;
1577 struct dyn_ftrace key;
1579 key.ip = start;
1580 key.flags = end; /* overload flags, as it is unsigned long */
1582 for (pg = ftrace_pages_start; pg; pg = pg->next) {
1583 if (end < pg->records[0].ip ||
1584 start >= (pg->records[pg->index - 1].ip + MCOUNT_INSN_SIZE))
1585 continue;
1586 rec = bsearch(&key, pg->records, pg->index,
1587 sizeof(struct dyn_ftrace),
1588 ftrace_cmp_recs);
1589 if (rec)
1590 return rec->ip;
1593 return 0;
1597 * ftrace_location - return true if the ip giving is a traced location
1598 * @ip: the instruction pointer to check
1600 * Returns rec->ip if @ip given is a pointer to a ftrace location.
1601 * That is, the instruction that is either a NOP or call to
1602 * the function tracer. It checks the ftrace internal tables to
1603 * determine if the address belongs or not.
1605 unsigned long ftrace_location(unsigned long ip)
1607 return ftrace_location_range(ip, ip);
1611 * ftrace_text_reserved - return true if range contains an ftrace location
1612 * @start: start of range to search
1613 * @end: end of range to search (inclusive). @end points to the last byte to check.
1615 * Returns 1 if @start and @end contains a ftrace location.
1616 * That is, the instruction that is either a NOP or call to
1617 * the function tracer. It checks the ftrace internal tables to
1618 * determine if the address belongs or not.
1620 int ftrace_text_reserved(const void *start, const void *end)
1622 unsigned long ret;
1624 ret = ftrace_location_range((unsigned long)start,
1625 (unsigned long)end);
1627 return (int)!!ret;
1630 /* Test if ops registered to this rec needs regs */
1631 static bool test_rec_ops_needs_regs(struct dyn_ftrace *rec)
1633 struct ftrace_ops *ops;
1634 bool keep_regs = false;
1636 for (ops = ftrace_ops_list;
1637 ops != &ftrace_list_end; ops = ops->next) {
1638 /* pass rec in as regs to have non-NULL val */
1639 if (ftrace_ops_test(ops, rec->ip, rec)) {
1640 if (ops->flags & FTRACE_OPS_FL_SAVE_REGS) {
1641 keep_regs = true;
1642 break;
1647 return keep_regs;
1650 static struct ftrace_ops *
1651 ftrace_find_tramp_ops_any(struct dyn_ftrace *rec);
1652 static struct ftrace_ops *
1653 ftrace_find_tramp_ops_any_other(struct dyn_ftrace *rec, struct ftrace_ops *op_exclude);
1654 static struct ftrace_ops *
1655 ftrace_find_tramp_ops_next(struct dyn_ftrace *rec, struct ftrace_ops *ops);
1657 static bool __ftrace_hash_rec_update(struct ftrace_ops *ops,
1658 int filter_hash,
1659 bool inc)
1661 struct ftrace_hash *hash;
1662 struct ftrace_hash *other_hash;
1663 struct ftrace_page *pg;
1664 struct dyn_ftrace *rec;
1665 bool update = false;
1666 int count = 0;
1667 int all = false;
1669 /* Only update if the ops has been registered */
1670 if (!(ops->flags & FTRACE_OPS_FL_ENABLED))
1671 return false;
1674 * In the filter_hash case:
1675 * If the count is zero, we update all records.
1676 * Otherwise we just update the items in the hash.
1678 * In the notrace_hash case:
1679 * We enable the update in the hash.
1680 * As disabling notrace means enabling the tracing,
1681 * and enabling notrace means disabling, the inc variable
1682 * gets inversed.
1684 if (filter_hash) {
1685 hash = ops->func_hash->filter_hash;
1686 other_hash = ops->func_hash->notrace_hash;
1687 if (ftrace_hash_empty(hash))
1688 all = true;
1689 } else {
1690 inc = !inc;
1691 hash = ops->func_hash->notrace_hash;
1692 other_hash = ops->func_hash->filter_hash;
1694 * If the notrace hash has no items,
1695 * then there's nothing to do.
1697 if (ftrace_hash_empty(hash))
1698 return false;
1701 do_for_each_ftrace_rec(pg, rec) {
1702 int in_other_hash = 0;
1703 int in_hash = 0;
1704 int match = 0;
1706 if (rec->flags & FTRACE_FL_DISABLED)
1707 continue;
1709 if (all) {
1711 * Only the filter_hash affects all records.
1712 * Update if the record is not in the notrace hash.
1714 if (!other_hash || !ftrace_lookup_ip(other_hash, rec->ip))
1715 match = 1;
1716 } else {
1717 in_hash = !!ftrace_lookup_ip(hash, rec->ip);
1718 in_other_hash = !!ftrace_lookup_ip(other_hash, rec->ip);
1721 * If filter_hash is set, we want to match all functions
1722 * that are in the hash but not in the other hash.
1724 * If filter_hash is not set, then we are decrementing.
1725 * That means we match anything that is in the hash
1726 * and also in the other_hash. That is, we need to turn
1727 * off functions in the other hash because they are disabled
1728 * by this hash.
1730 if (filter_hash && in_hash && !in_other_hash)
1731 match = 1;
1732 else if (!filter_hash && in_hash &&
1733 (in_other_hash || ftrace_hash_empty(other_hash)))
1734 match = 1;
1736 if (!match)
1737 continue;
1739 if (inc) {
1740 rec->flags++;
1741 if (FTRACE_WARN_ON(ftrace_rec_count(rec) == FTRACE_REF_MAX))
1742 return false;
1745 * If there's only a single callback registered to a
1746 * function, and the ops has a trampoline registered
1747 * for it, then we can call it directly.
1749 if (ftrace_rec_count(rec) == 1 && ops->trampoline)
1750 rec->flags |= FTRACE_FL_TRAMP;
1751 else
1753 * If we are adding another function callback
1754 * to this function, and the previous had a
1755 * custom trampoline in use, then we need to go
1756 * back to the default trampoline.
1758 rec->flags &= ~FTRACE_FL_TRAMP;
1761 * If any ops wants regs saved for this function
1762 * then all ops will get saved regs.
1764 if (ops->flags & FTRACE_OPS_FL_SAVE_REGS)
1765 rec->flags |= FTRACE_FL_REGS;
1766 } else {
1767 if (FTRACE_WARN_ON(ftrace_rec_count(rec) == 0))
1768 return false;
1769 rec->flags--;
1772 * If the rec had REGS enabled and the ops that is
1773 * being removed had REGS set, then see if there is
1774 * still any ops for this record that wants regs.
1775 * If not, we can stop recording them.
1777 if (ftrace_rec_count(rec) > 0 &&
1778 rec->flags & FTRACE_FL_REGS &&
1779 ops->flags & FTRACE_OPS_FL_SAVE_REGS) {
1780 if (!test_rec_ops_needs_regs(rec))
1781 rec->flags &= ~FTRACE_FL_REGS;
1785 * The TRAMP needs to be set only if rec count
1786 * is decremented to one, and the ops that is
1787 * left has a trampoline. As TRAMP can only be
1788 * enabled if there is only a single ops attached
1789 * to it.
1791 if (ftrace_rec_count(rec) == 1 &&
1792 ftrace_find_tramp_ops_any_other(rec, ops))
1793 rec->flags |= FTRACE_FL_TRAMP;
1794 else
1795 rec->flags &= ~FTRACE_FL_TRAMP;
1798 * flags will be cleared in ftrace_check_record()
1799 * if rec count is zero.
1802 count++;
1804 /* Must match FTRACE_UPDATE_CALLS in ftrace_modify_all_code() */
1805 update |= ftrace_test_record(rec, 1) != FTRACE_UPDATE_IGNORE;
1807 /* Shortcut, if we handled all records, we are done. */
1808 if (!all && count == hash->count)
1809 return update;
1810 } while_for_each_ftrace_rec();
1812 return update;
1815 static bool ftrace_hash_rec_disable(struct ftrace_ops *ops,
1816 int filter_hash)
1818 return __ftrace_hash_rec_update(ops, filter_hash, 0);
1821 static bool ftrace_hash_rec_enable(struct ftrace_ops *ops,
1822 int filter_hash)
1824 return __ftrace_hash_rec_update(ops, filter_hash, 1);
1827 static void ftrace_hash_rec_update_modify(struct ftrace_ops *ops,
1828 int filter_hash, int inc)
1830 struct ftrace_ops *op;
1832 __ftrace_hash_rec_update(ops, filter_hash, inc);
1834 if (ops->func_hash != &global_ops.local_hash)
1835 return;
1838 * If the ops shares the global_ops hash, then we need to update
1839 * all ops that are enabled and use this hash.
1841 do_for_each_ftrace_op(op, ftrace_ops_list) {
1842 /* Already done */
1843 if (op == ops)
1844 continue;
1845 if (op->func_hash == &global_ops.local_hash)
1846 __ftrace_hash_rec_update(op, filter_hash, inc);
1847 } while_for_each_ftrace_op(op);
1850 static void ftrace_hash_rec_disable_modify(struct ftrace_ops *ops,
1851 int filter_hash)
1853 ftrace_hash_rec_update_modify(ops, filter_hash, 0);
1856 static void ftrace_hash_rec_enable_modify(struct ftrace_ops *ops,
1857 int filter_hash)
1859 ftrace_hash_rec_update_modify(ops, filter_hash, 1);
1863 * Try to update IPMODIFY flag on each ftrace_rec. Return 0 if it is OK
1864 * or no-needed to update, -EBUSY if it detects a conflict of the flag
1865 * on a ftrace_rec, and -EINVAL if the new_hash tries to trace all recs.
1866 * Note that old_hash and new_hash has below meanings
1867 * - If the hash is NULL, it hits all recs (if IPMODIFY is set, this is rejected)
1868 * - If the hash is EMPTY_HASH, it hits nothing
1869 * - Anything else hits the recs which match the hash entries.
1871 static int __ftrace_hash_update_ipmodify(struct ftrace_ops *ops,
1872 struct ftrace_hash *old_hash,
1873 struct ftrace_hash *new_hash)
1875 struct ftrace_page *pg;
1876 struct dyn_ftrace *rec, *end = NULL;
1877 int in_old, in_new;
1879 /* Only update if the ops has been registered */
1880 if (!(ops->flags & FTRACE_OPS_FL_ENABLED))
1881 return 0;
1883 if (!(ops->flags & FTRACE_OPS_FL_IPMODIFY))
1884 return 0;
1887 * Since the IPMODIFY is a very address sensitive action, we do not
1888 * allow ftrace_ops to set all functions to new hash.
1890 if (!new_hash || !old_hash)
1891 return -EINVAL;
1893 /* Update rec->flags */
1894 do_for_each_ftrace_rec(pg, rec) {
1896 if (rec->flags & FTRACE_FL_DISABLED)
1897 continue;
1899 /* We need to update only differences of filter_hash */
1900 in_old = !!ftrace_lookup_ip(old_hash, rec->ip);
1901 in_new = !!ftrace_lookup_ip(new_hash, rec->ip);
1902 if (in_old == in_new)
1903 continue;
1905 if (in_new) {
1906 /* New entries must ensure no others are using it */
1907 if (rec->flags & FTRACE_FL_IPMODIFY)
1908 goto rollback;
1909 rec->flags |= FTRACE_FL_IPMODIFY;
1910 } else /* Removed entry */
1911 rec->flags &= ~FTRACE_FL_IPMODIFY;
1912 } while_for_each_ftrace_rec();
1914 return 0;
1916 rollback:
1917 end = rec;
1919 /* Roll back what we did above */
1920 do_for_each_ftrace_rec(pg, rec) {
1922 if (rec->flags & FTRACE_FL_DISABLED)
1923 continue;
1925 if (rec == end)
1926 goto err_out;
1928 in_old = !!ftrace_lookup_ip(old_hash, rec->ip);
1929 in_new = !!ftrace_lookup_ip(new_hash, rec->ip);
1930 if (in_old == in_new)
1931 continue;
1933 if (in_new)
1934 rec->flags &= ~FTRACE_FL_IPMODIFY;
1935 else
1936 rec->flags |= FTRACE_FL_IPMODIFY;
1937 } while_for_each_ftrace_rec();
1939 err_out:
1940 return -EBUSY;
1943 static int ftrace_hash_ipmodify_enable(struct ftrace_ops *ops)
1945 struct ftrace_hash *hash = ops->func_hash->filter_hash;
1947 if (ftrace_hash_empty(hash))
1948 hash = NULL;
1950 return __ftrace_hash_update_ipmodify(ops, EMPTY_HASH, hash);
1953 /* Disabling always succeeds */
1954 static void ftrace_hash_ipmodify_disable(struct ftrace_ops *ops)
1956 struct ftrace_hash *hash = ops->func_hash->filter_hash;
1958 if (ftrace_hash_empty(hash))
1959 hash = NULL;
1961 __ftrace_hash_update_ipmodify(ops, hash, EMPTY_HASH);
1964 static int ftrace_hash_ipmodify_update(struct ftrace_ops *ops,
1965 struct ftrace_hash *new_hash)
1967 struct ftrace_hash *old_hash = ops->func_hash->filter_hash;
1969 if (ftrace_hash_empty(old_hash))
1970 old_hash = NULL;
1972 if (ftrace_hash_empty(new_hash))
1973 new_hash = NULL;
1975 return __ftrace_hash_update_ipmodify(ops, old_hash, new_hash);
1978 static void print_ip_ins(const char *fmt, const unsigned char *p)
1980 int i;
1982 printk(KERN_CONT "%s", fmt);
1984 for (i = 0; i < MCOUNT_INSN_SIZE; i++)
1985 printk(KERN_CONT "%s%02x", i ? ":" : "", p[i]);
1988 enum ftrace_bug_type ftrace_bug_type;
1989 const void *ftrace_expected;
1991 static void print_bug_type(void)
1993 switch (ftrace_bug_type) {
1994 case FTRACE_BUG_UNKNOWN:
1995 break;
1996 case FTRACE_BUG_INIT:
1997 pr_info("Initializing ftrace call sites\n");
1998 break;
1999 case FTRACE_BUG_NOP:
2000 pr_info("Setting ftrace call site to NOP\n");
2001 break;
2002 case FTRACE_BUG_CALL:
2003 pr_info("Setting ftrace call site to call ftrace function\n");
2004 break;
2005 case FTRACE_BUG_UPDATE:
2006 pr_info("Updating ftrace call site to call a different ftrace function\n");
2007 break;
2012 * ftrace_bug - report and shutdown function tracer
2013 * @failed: The failed type (EFAULT, EINVAL, EPERM)
2014 * @rec: The record that failed
2016 * The arch code that enables or disables the function tracing
2017 * can call ftrace_bug() when it has detected a problem in
2018 * modifying the code. @failed should be one of either:
2019 * EFAULT - if the problem happens on reading the @ip address
2020 * EINVAL - if what is read at @ip is not what was expected
2021 * EPERM - if the problem happens on writting to the @ip address
2023 void ftrace_bug(int failed, struct dyn_ftrace *rec)
2025 unsigned long ip = rec ? rec->ip : 0;
2027 switch (failed) {
2028 case -EFAULT:
2029 FTRACE_WARN_ON_ONCE(1);
2030 pr_info("ftrace faulted on modifying ");
2031 print_ip_sym(ip);
2032 break;
2033 case -EINVAL:
2034 FTRACE_WARN_ON_ONCE(1);
2035 pr_info("ftrace failed to modify ");
2036 print_ip_sym(ip);
2037 print_ip_ins(" actual: ", (unsigned char *)ip);
2038 pr_cont("\n");
2039 if (ftrace_expected) {
2040 print_ip_ins(" expected: ", ftrace_expected);
2041 pr_cont("\n");
2043 break;
2044 case -EPERM:
2045 FTRACE_WARN_ON_ONCE(1);
2046 pr_info("ftrace faulted on writing ");
2047 print_ip_sym(ip);
2048 break;
2049 default:
2050 FTRACE_WARN_ON_ONCE(1);
2051 pr_info("ftrace faulted on unknown error ");
2052 print_ip_sym(ip);
2054 print_bug_type();
2055 if (rec) {
2056 struct ftrace_ops *ops = NULL;
2058 pr_info("ftrace record flags: %lx\n", rec->flags);
2059 pr_cont(" (%ld)%s", ftrace_rec_count(rec),
2060 rec->flags & FTRACE_FL_REGS ? " R" : " ");
2061 if (rec->flags & FTRACE_FL_TRAMP_EN) {
2062 ops = ftrace_find_tramp_ops_any(rec);
2063 if (ops) {
2064 do {
2065 pr_cont("\ttramp: %pS (%pS)",
2066 (void *)ops->trampoline,
2067 (void *)ops->func);
2068 ops = ftrace_find_tramp_ops_next(rec, ops);
2069 } while (ops);
2070 } else
2071 pr_cont("\ttramp: ERROR!");
2074 ip = ftrace_get_addr_curr(rec);
2075 pr_cont("\n expected tramp: %lx\n", ip);
2079 static int ftrace_check_record(struct dyn_ftrace *rec, int enable, int update)
2081 unsigned long flag = 0UL;
2083 ftrace_bug_type = FTRACE_BUG_UNKNOWN;
2085 if (rec->flags & FTRACE_FL_DISABLED)
2086 return FTRACE_UPDATE_IGNORE;
2089 * If we are updating calls:
2091 * If the record has a ref count, then we need to enable it
2092 * because someone is using it.
2094 * Otherwise we make sure its disabled.
2096 * If we are disabling calls, then disable all records that
2097 * are enabled.
2099 if (enable && ftrace_rec_count(rec))
2100 flag = FTRACE_FL_ENABLED;
2103 * If enabling and the REGS flag does not match the REGS_EN, or
2104 * the TRAMP flag doesn't match the TRAMP_EN, then do not ignore
2105 * this record. Set flags to fail the compare against ENABLED.
2107 if (flag) {
2108 if (!(rec->flags & FTRACE_FL_REGS) !=
2109 !(rec->flags & FTRACE_FL_REGS_EN))
2110 flag |= FTRACE_FL_REGS;
2112 if (!(rec->flags & FTRACE_FL_TRAMP) !=
2113 !(rec->flags & FTRACE_FL_TRAMP_EN))
2114 flag |= FTRACE_FL_TRAMP;
2117 /* If the state of this record hasn't changed, then do nothing */
2118 if ((rec->flags & FTRACE_FL_ENABLED) == flag)
2119 return FTRACE_UPDATE_IGNORE;
2121 if (flag) {
2122 /* Save off if rec is being enabled (for return value) */
2123 flag ^= rec->flags & FTRACE_FL_ENABLED;
2125 if (update) {
2126 rec->flags |= FTRACE_FL_ENABLED;
2127 if (flag & FTRACE_FL_REGS) {
2128 if (rec->flags & FTRACE_FL_REGS)
2129 rec->flags |= FTRACE_FL_REGS_EN;
2130 else
2131 rec->flags &= ~FTRACE_FL_REGS_EN;
2133 if (flag & FTRACE_FL_TRAMP) {
2134 if (rec->flags & FTRACE_FL_TRAMP)
2135 rec->flags |= FTRACE_FL_TRAMP_EN;
2136 else
2137 rec->flags &= ~FTRACE_FL_TRAMP_EN;
2142 * If this record is being updated from a nop, then
2143 * return UPDATE_MAKE_CALL.
2144 * Otherwise,
2145 * return UPDATE_MODIFY_CALL to tell the caller to convert
2146 * from the save regs, to a non-save regs function or
2147 * vice versa, or from a trampoline call.
2149 if (flag & FTRACE_FL_ENABLED) {
2150 ftrace_bug_type = FTRACE_BUG_CALL;
2151 return FTRACE_UPDATE_MAKE_CALL;
2154 ftrace_bug_type = FTRACE_BUG_UPDATE;
2155 return FTRACE_UPDATE_MODIFY_CALL;
2158 if (update) {
2159 /* If there's no more users, clear all flags */
2160 if (!ftrace_rec_count(rec))
2161 rec->flags = 0;
2162 else
2164 * Just disable the record, but keep the ops TRAMP
2165 * and REGS states. The _EN flags must be disabled though.
2167 rec->flags &= ~(FTRACE_FL_ENABLED | FTRACE_FL_TRAMP_EN |
2168 FTRACE_FL_REGS_EN);
2171 ftrace_bug_type = FTRACE_BUG_NOP;
2172 return FTRACE_UPDATE_MAKE_NOP;
2176 * ftrace_update_record, set a record that now is tracing or not
2177 * @rec: the record to update
2178 * @enable: set to 1 if the record is tracing, zero to force disable
2180 * The records that represent all functions that can be traced need
2181 * to be updated when tracing has been enabled.
2183 int ftrace_update_record(struct dyn_ftrace *rec, int enable)
2185 return ftrace_check_record(rec, enable, 1);
2189 * ftrace_test_record, check if the record has been enabled or not
2190 * @rec: the record to test
2191 * @enable: set to 1 to check if enabled, 0 if it is disabled
2193 * The arch code may need to test if a record is already set to
2194 * tracing to determine how to modify the function code that it
2195 * represents.
2197 int ftrace_test_record(struct dyn_ftrace *rec, int enable)
2199 return ftrace_check_record(rec, enable, 0);
2202 static struct ftrace_ops *
2203 ftrace_find_tramp_ops_any(struct dyn_ftrace *rec)
2205 struct ftrace_ops *op;
2206 unsigned long ip = rec->ip;
2208 do_for_each_ftrace_op(op, ftrace_ops_list) {
2210 if (!op->trampoline)
2211 continue;
2213 if (hash_contains_ip(ip, op->func_hash))
2214 return op;
2215 } while_for_each_ftrace_op(op);
2217 return NULL;
2220 static struct ftrace_ops *
2221 ftrace_find_tramp_ops_any_other(struct dyn_ftrace *rec, struct ftrace_ops *op_exclude)
2223 struct ftrace_ops *op;
2224 unsigned long ip = rec->ip;
2226 do_for_each_ftrace_op(op, ftrace_ops_list) {
2228 if (op == op_exclude || !op->trampoline)
2229 continue;
2231 if (hash_contains_ip(ip, op->func_hash))
2232 return op;
2233 } while_for_each_ftrace_op(op);
2235 return NULL;
2238 static struct ftrace_ops *
2239 ftrace_find_tramp_ops_next(struct dyn_ftrace *rec,
2240 struct ftrace_ops *op)
2242 unsigned long ip = rec->ip;
2244 while_for_each_ftrace_op(op) {
2246 if (!op->trampoline)
2247 continue;
2249 if (hash_contains_ip(ip, op->func_hash))
2250 return op;
2253 return NULL;
2256 static struct ftrace_ops *
2257 ftrace_find_tramp_ops_curr(struct dyn_ftrace *rec)
2259 struct ftrace_ops *op;
2260 unsigned long ip = rec->ip;
2263 * Need to check removed ops first.
2264 * If they are being removed, and this rec has a tramp,
2265 * and this rec is in the ops list, then it would be the
2266 * one with the tramp.
2268 if (removed_ops) {
2269 if (hash_contains_ip(ip, &removed_ops->old_hash))
2270 return removed_ops;
2274 * Need to find the current trampoline for a rec.
2275 * Now, a trampoline is only attached to a rec if there
2276 * was a single 'ops' attached to it. But this can be called
2277 * when we are adding another op to the rec or removing the
2278 * current one. Thus, if the op is being added, we can
2279 * ignore it because it hasn't attached itself to the rec
2280 * yet.
2282 * If an ops is being modified (hooking to different functions)
2283 * then we don't care about the new functions that are being
2284 * added, just the old ones (that are probably being removed).
2286 * If we are adding an ops to a function that already is using
2287 * a trampoline, it needs to be removed (trampolines are only
2288 * for single ops connected), then an ops that is not being
2289 * modified also needs to be checked.
2291 do_for_each_ftrace_op(op, ftrace_ops_list) {
2293 if (!op->trampoline)
2294 continue;
2297 * If the ops is being added, it hasn't gotten to
2298 * the point to be removed from this tree yet.
2300 if (op->flags & FTRACE_OPS_FL_ADDING)
2301 continue;
2305 * If the ops is being modified and is in the old
2306 * hash, then it is probably being removed from this
2307 * function.
2309 if ((op->flags & FTRACE_OPS_FL_MODIFYING) &&
2310 hash_contains_ip(ip, &op->old_hash))
2311 return op;
2313 * If the ops is not being added or modified, and it's
2314 * in its normal filter hash, then this must be the one
2315 * we want!
2317 if (!(op->flags & FTRACE_OPS_FL_MODIFYING) &&
2318 hash_contains_ip(ip, op->func_hash))
2319 return op;
2321 } while_for_each_ftrace_op(op);
2323 return NULL;
2326 static struct ftrace_ops *
2327 ftrace_find_tramp_ops_new(struct dyn_ftrace *rec)
2329 struct ftrace_ops *op;
2330 unsigned long ip = rec->ip;
2332 do_for_each_ftrace_op(op, ftrace_ops_list) {
2333 /* pass rec in as regs to have non-NULL val */
2334 if (hash_contains_ip(ip, op->func_hash))
2335 return op;
2336 } while_for_each_ftrace_op(op);
2338 return NULL;
2342 * ftrace_get_addr_new - Get the call address to set to
2343 * @rec: The ftrace record descriptor
2345 * If the record has the FTRACE_FL_REGS set, that means that it
2346 * wants to convert to a callback that saves all regs. If FTRACE_FL_REGS
2347 * is not not set, then it wants to convert to the normal callback.
2349 * Returns the address of the trampoline to set to
2351 unsigned long ftrace_get_addr_new(struct dyn_ftrace *rec)
2353 struct ftrace_ops *ops;
2355 /* Trampolines take precedence over regs */
2356 if (rec->flags & FTRACE_FL_TRAMP) {
2357 ops = ftrace_find_tramp_ops_new(rec);
2358 if (FTRACE_WARN_ON(!ops || !ops->trampoline)) {
2359 pr_warn("Bad trampoline accounting at: %p (%pS) (%lx)\n",
2360 (void *)rec->ip, (void *)rec->ip, rec->flags);
2361 /* Ftrace is shutting down, return anything */
2362 return (unsigned long)FTRACE_ADDR;
2364 return ops->trampoline;
2367 if (rec->flags & FTRACE_FL_REGS)
2368 return (unsigned long)FTRACE_REGS_ADDR;
2369 else
2370 return (unsigned long)FTRACE_ADDR;
2374 * ftrace_get_addr_curr - Get the call address that is already there
2375 * @rec: The ftrace record descriptor
2377 * The FTRACE_FL_REGS_EN is set when the record already points to
2378 * a function that saves all the regs. Basically the '_EN' version
2379 * represents the current state of the function.
2381 * Returns the address of the trampoline that is currently being called
2383 unsigned long ftrace_get_addr_curr(struct dyn_ftrace *rec)
2385 struct ftrace_ops *ops;
2387 /* Trampolines take precedence over regs */
2388 if (rec->flags & FTRACE_FL_TRAMP_EN) {
2389 ops = ftrace_find_tramp_ops_curr(rec);
2390 if (FTRACE_WARN_ON(!ops)) {
2391 pr_warn("Bad trampoline accounting at: %p (%pS)\n",
2392 (void *)rec->ip, (void *)rec->ip);
2393 /* Ftrace is shutting down, return anything */
2394 return (unsigned long)FTRACE_ADDR;
2396 return ops->trampoline;
2399 if (rec->flags & FTRACE_FL_REGS_EN)
2400 return (unsigned long)FTRACE_REGS_ADDR;
2401 else
2402 return (unsigned long)FTRACE_ADDR;
2405 static int
2406 __ftrace_replace_code(struct dyn_ftrace *rec, int enable)
2408 unsigned long ftrace_old_addr;
2409 unsigned long ftrace_addr;
2410 int ret;
2412 ftrace_addr = ftrace_get_addr_new(rec);
2414 /* This needs to be done before we call ftrace_update_record */
2415 ftrace_old_addr = ftrace_get_addr_curr(rec);
2417 ret = ftrace_update_record(rec, enable);
2419 ftrace_bug_type = FTRACE_BUG_UNKNOWN;
2421 switch (ret) {
2422 case FTRACE_UPDATE_IGNORE:
2423 return 0;
2425 case FTRACE_UPDATE_MAKE_CALL:
2426 ftrace_bug_type = FTRACE_BUG_CALL;
2427 return ftrace_make_call(rec, ftrace_addr);
2429 case FTRACE_UPDATE_MAKE_NOP:
2430 ftrace_bug_type = FTRACE_BUG_NOP;
2431 return ftrace_make_nop(NULL, rec, ftrace_old_addr);
2433 case FTRACE_UPDATE_MODIFY_CALL:
2434 ftrace_bug_type = FTRACE_BUG_UPDATE;
2435 return ftrace_modify_call(rec, ftrace_old_addr, ftrace_addr);
2438 return -1; /* unknow ftrace bug */
2441 void __weak ftrace_replace_code(int enable)
2443 struct dyn_ftrace *rec;
2444 struct ftrace_page *pg;
2445 int failed;
2447 if (unlikely(ftrace_disabled))
2448 return;
2450 do_for_each_ftrace_rec(pg, rec) {
2452 if (rec->flags & FTRACE_FL_DISABLED)
2453 continue;
2455 failed = __ftrace_replace_code(rec, enable);
2456 if (failed) {
2457 ftrace_bug(failed, rec);
2458 /* Stop processing */
2459 return;
2461 } while_for_each_ftrace_rec();
2464 struct ftrace_rec_iter {
2465 struct ftrace_page *pg;
2466 int index;
2470 * ftrace_rec_iter_start, start up iterating over traced functions
2472 * Returns an iterator handle that is used to iterate over all
2473 * the records that represent address locations where functions
2474 * are traced.
2476 * May return NULL if no records are available.
2478 struct ftrace_rec_iter *ftrace_rec_iter_start(void)
2481 * We only use a single iterator.
2482 * Protected by the ftrace_lock mutex.
2484 static struct ftrace_rec_iter ftrace_rec_iter;
2485 struct ftrace_rec_iter *iter = &ftrace_rec_iter;
2487 iter->pg = ftrace_pages_start;
2488 iter->index = 0;
2490 /* Could have empty pages */
2491 while (iter->pg && !iter->pg->index)
2492 iter->pg = iter->pg->next;
2494 if (!iter->pg)
2495 return NULL;
2497 return iter;
2501 * ftrace_rec_iter_next, get the next record to process.
2502 * @iter: The handle to the iterator.
2504 * Returns the next iterator after the given iterator @iter.
2506 struct ftrace_rec_iter *ftrace_rec_iter_next(struct ftrace_rec_iter *iter)
2508 iter->index++;
2510 if (iter->index >= iter->pg->index) {
2511 iter->pg = iter->pg->next;
2512 iter->index = 0;
2514 /* Could have empty pages */
2515 while (iter->pg && !iter->pg->index)
2516 iter->pg = iter->pg->next;
2519 if (!iter->pg)
2520 return NULL;
2522 return iter;
2526 * ftrace_rec_iter_record, get the record at the iterator location
2527 * @iter: The current iterator location
2529 * Returns the record that the current @iter is at.
2531 struct dyn_ftrace *ftrace_rec_iter_record(struct ftrace_rec_iter *iter)
2533 return &iter->pg->records[iter->index];
2536 static int
2537 ftrace_code_disable(struct module *mod, struct dyn_ftrace *rec)
2539 int ret;
2541 if (unlikely(ftrace_disabled))
2542 return 0;
2544 ret = ftrace_make_nop(mod, rec, MCOUNT_ADDR);
2545 if (ret) {
2546 ftrace_bug_type = FTRACE_BUG_INIT;
2547 ftrace_bug(ret, rec);
2548 return 0;
2550 return 1;
2554 * archs can override this function if they must do something
2555 * before the modifying code is performed.
2557 int __weak ftrace_arch_code_modify_prepare(void)
2559 return 0;
2563 * archs can override this function if they must do something
2564 * after the modifying code is performed.
2566 int __weak ftrace_arch_code_modify_post_process(void)
2568 return 0;
2571 void ftrace_modify_all_code(int command)
2573 int update = command & FTRACE_UPDATE_TRACE_FUNC;
2574 int err = 0;
2577 * If the ftrace_caller calls a ftrace_ops func directly,
2578 * we need to make sure that it only traces functions it
2579 * expects to trace. When doing the switch of functions,
2580 * we need to update to the ftrace_ops_list_func first
2581 * before the transition between old and new calls are set,
2582 * as the ftrace_ops_list_func will check the ops hashes
2583 * to make sure the ops are having the right functions
2584 * traced.
2586 if (update) {
2587 err = ftrace_update_ftrace_func(ftrace_ops_list_func);
2588 if (FTRACE_WARN_ON(err))
2589 return;
2592 if (command & FTRACE_UPDATE_CALLS)
2593 ftrace_replace_code(1);
2594 else if (command & FTRACE_DISABLE_CALLS)
2595 ftrace_replace_code(0);
2597 if (update && ftrace_trace_function != ftrace_ops_list_func) {
2598 function_trace_op = set_function_trace_op;
2599 smp_wmb();
2600 /* If irqs are disabled, we are in stop machine */
2601 if (!irqs_disabled())
2602 smp_call_function(ftrace_sync_ipi, NULL, 1);
2603 err = ftrace_update_ftrace_func(ftrace_trace_function);
2604 if (FTRACE_WARN_ON(err))
2605 return;
2608 if (command & FTRACE_START_FUNC_RET)
2609 err = ftrace_enable_ftrace_graph_caller();
2610 else if (command & FTRACE_STOP_FUNC_RET)
2611 err = ftrace_disable_ftrace_graph_caller();
2612 FTRACE_WARN_ON(err);
2615 static int __ftrace_modify_code(void *data)
2617 int *command = data;
2619 ftrace_modify_all_code(*command);
2621 return 0;
2625 * ftrace_run_stop_machine, go back to the stop machine method
2626 * @command: The command to tell ftrace what to do
2628 * If an arch needs to fall back to the stop machine method, the
2629 * it can call this function.
2631 void ftrace_run_stop_machine(int command)
2633 stop_machine(__ftrace_modify_code, &command, NULL);
2637 * arch_ftrace_update_code, modify the code to trace or not trace
2638 * @command: The command that needs to be done
2640 * Archs can override this function if it does not need to
2641 * run stop_machine() to modify code.
2643 void __weak arch_ftrace_update_code(int command)
2645 ftrace_run_stop_machine(command);
2648 static void ftrace_run_update_code(int command)
2650 int ret;
2652 ret = ftrace_arch_code_modify_prepare();
2653 FTRACE_WARN_ON(ret);
2654 if (ret)
2655 return;
2658 * By default we use stop_machine() to modify the code.
2659 * But archs can do what ever they want as long as it
2660 * is safe. The stop_machine() is the safest, but also
2661 * produces the most overhead.
2663 arch_ftrace_update_code(command);
2665 ret = ftrace_arch_code_modify_post_process();
2666 FTRACE_WARN_ON(ret);
2669 static void ftrace_run_modify_code(struct ftrace_ops *ops, int command,
2670 struct ftrace_ops_hash *old_hash)
2672 ops->flags |= FTRACE_OPS_FL_MODIFYING;
2673 ops->old_hash.filter_hash = old_hash->filter_hash;
2674 ops->old_hash.notrace_hash = old_hash->notrace_hash;
2675 ftrace_run_update_code(command);
2676 ops->old_hash.filter_hash = NULL;
2677 ops->old_hash.notrace_hash = NULL;
2678 ops->flags &= ~FTRACE_OPS_FL_MODIFYING;
2681 static ftrace_func_t saved_ftrace_func;
2682 static int ftrace_start_up;
2684 void __weak arch_ftrace_trampoline_free(struct ftrace_ops *ops)
2688 static void ftrace_startup_enable(int command)
2690 if (saved_ftrace_func != ftrace_trace_function) {
2691 saved_ftrace_func = ftrace_trace_function;
2692 command |= FTRACE_UPDATE_TRACE_FUNC;
2695 if (!command || !ftrace_enabled)
2696 return;
2698 ftrace_run_update_code(command);
2701 static void ftrace_startup_all(int command)
2703 update_all_ops = true;
2704 ftrace_startup_enable(command);
2705 update_all_ops = false;
2708 static int ftrace_startup(struct ftrace_ops *ops, int command)
2710 int ret;
2712 if (unlikely(ftrace_disabled))
2713 return -ENODEV;
2715 ret = __register_ftrace_function(ops);
2716 if (ret)
2717 return ret;
2719 ftrace_start_up++;
2722 * Note that ftrace probes uses this to start up
2723 * and modify functions it will probe. But we still
2724 * set the ADDING flag for modification, as probes
2725 * do not have trampolines. If they add them in the
2726 * future, then the probes will need to distinguish
2727 * between adding and updating probes.
2729 ops->flags |= FTRACE_OPS_FL_ENABLED | FTRACE_OPS_FL_ADDING;
2731 ret = ftrace_hash_ipmodify_enable(ops);
2732 if (ret < 0) {
2733 /* Rollback registration process */
2734 __unregister_ftrace_function(ops);
2735 ftrace_start_up--;
2736 ops->flags &= ~FTRACE_OPS_FL_ENABLED;
2737 return ret;
2740 if (ftrace_hash_rec_enable(ops, 1))
2741 command |= FTRACE_UPDATE_CALLS;
2743 ftrace_startup_enable(command);
2745 ops->flags &= ~FTRACE_OPS_FL_ADDING;
2747 return 0;
2750 static int ftrace_shutdown(struct ftrace_ops *ops, int command)
2752 int ret;
2754 if (unlikely(ftrace_disabled))
2755 return -ENODEV;
2757 ret = __unregister_ftrace_function(ops);
2758 if (ret)
2759 return ret;
2761 ftrace_start_up--;
2763 * Just warn in case of unbalance, no need to kill ftrace, it's not
2764 * critical but the ftrace_call callers may be never nopped again after
2765 * further ftrace uses.
2767 WARN_ON_ONCE(ftrace_start_up < 0);
2769 /* Disabling ipmodify never fails */
2770 ftrace_hash_ipmodify_disable(ops);
2772 if (ftrace_hash_rec_disable(ops, 1))
2773 command |= FTRACE_UPDATE_CALLS;
2775 ops->flags &= ~FTRACE_OPS_FL_ENABLED;
2777 if (saved_ftrace_func != ftrace_trace_function) {
2778 saved_ftrace_func = ftrace_trace_function;
2779 command |= FTRACE_UPDATE_TRACE_FUNC;
2782 if (!command || !ftrace_enabled) {
2784 * If these are dynamic or per_cpu ops, they still
2785 * need their data freed. Since, function tracing is
2786 * not currently active, we can just free them
2787 * without synchronizing all CPUs.
2789 if (ops->flags & FTRACE_OPS_FL_DYNAMIC)
2790 goto free_ops;
2792 return 0;
2796 * If the ops uses a trampoline, then it needs to be
2797 * tested first on update.
2799 ops->flags |= FTRACE_OPS_FL_REMOVING;
2800 removed_ops = ops;
2802 /* The trampoline logic checks the old hashes */
2803 ops->old_hash.filter_hash = ops->func_hash->filter_hash;
2804 ops->old_hash.notrace_hash = ops->func_hash->notrace_hash;
2806 ftrace_run_update_code(command);
2809 * If there's no more ops registered with ftrace, run a
2810 * sanity check to make sure all rec flags are cleared.
2812 if (rcu_dereference_protected(ftrace_ops_list,
2813 lockdep_is_held(&ftrace_lock)) == &ftrace_list_end) {
2814 struct ftrace_page *pg;
2815 struct dyn_ftrace *rec;
2817 do_for_each_ftrace_rec(pg, rec) {
2818 if (FTRACE_WARN_ON_ONCE(rec->flags & ~FTRACE_FL_DISABLED))
2819 pr_warn(" %pS flags:%lx\n",
2820 (void *)rec->ip, rec->flags);
2821 } while_for_each_ftrace_rec();
2824 ops->old_hash.filter_hash = NULL;
2825 ops->old_hash.notrace_hash = NULL;
2827 removed_ops = NULL;
2828 ops->flags &= ~FTRACE_OPS_FL_REMOVING;
2831 * Dynamic ops may be freed, we must make sure that all
2832 * callers are done before leaving this function.
2833 * The same goes for freeing the per_cpu data of the per_cpu
2834 * ops.
2836 if (ops->flags & FTRACE_OPS_FL_DYNAMIC) {
2838 * We need to do a hard force of sched synchronization.
2839 * This is because we use preempt_disable() to do RCU, but
2840 * the function tracers can be called where RCU is not watching
2841 * (like before user_exit()). We can not rely on the RCU
2842 * infrastructure to do the synchronization, thus we must do it
2843 * ourselves.
2845 schedule_on_each_cpu(ftrace_sync);
2848 * When the kernel is preeptive, tasks can be preempted
2849 * while on a ftrace trampoline. Just scheduling a task on
2850 * a CPU is not good enough to flush them. Calling
2851 * synchornize_rcu_tasks() will wait for those tasks to
2852 * execute and either schedule voluntarily or enter user space.
2854 if (IS_ENABLED(CONFIG_PREEMPT))
2855 synchronize_rcu_tasks();
2857 free_ops:
2858 arch_ftrace_trampoline_free(ops);
2861 return 0;
2864 static void ftrace_startup_sysctl(void)
2866 int command;
2868 if (unlikely(ftrace_disabled))
2869 return;
2871 /* Force update next time */
2872 saved_ftrace_func = NULL;
2873 /* ftrace_start_up is true if we want ftrace running */
2874 if (ftrace_start_up) {
2875 command = FTRACE_UPDATE_CALLS;
2876 if (ftrace_graph_active)
2877 command |= FTRACE_START_FUNC_RET;
2878 ftrace_startup_enable(command);
2882 static void ftrace_shutdown_sysctl(void)
2884 int command;
2886 if (unlikely(ftrace_disabled))
2887 return;
2889 /* ftrace_start_up is true if ftrace is running */
2890 if (ftrace_start_up) {
2891 command = FTRACE_DISABLE_CALLS;
2892 if (ftrace_graph_active)
2893 command |= FTRACE_STOP_FUNC_RET;
2894 ftrace_run_update_code(command);
2898 static u64 ftrace_update_time;
2899 unsigned long ftrace_update_tot_cnt;
2901 static inline int ops_traces_mod(struct ftrace_ops *ops)
2904 * Filter_hash being empty will default to trace module.
2905 * But notrace hash requires a test of individual module functions.
2907 return ftrace_hash_empty(ops->func_hash->filter_hash) &&
2908 ftrace_hash_empty(ops->func_hash->notrace_hash);
2912 * Check if the current ops references the record.
2914 * If the ops traces all functions, then it was already accounted for.
2915 * If the ops does not trace the current record function, skip it.
2916 * If the ops ignores the function via notrace filter, skip it.
2918 static inline bool
2919 ops_references_rec(struct ftrace_ops *ops, struct dyn_ftrace *rec)
2921 /* If ops isn't enabled, ignore it */
2922 if (!(ops->flags & FTRACE_OPS_FL_ENABLED))
2923 return false;
2925 /* If ops traces all then it includes this function */
2926 if (ops_traces_mod(ops))
2927 return true;
2929 /* The function must be in the filter */
2930 if (!ftrace_hash_empty(ops->func_hash->filter_hash) &&
2931 !__ftrace_lookup_ip(ops->func_hash->filter_hash, rec->ip))
2932 return false;
2934 /* If in notrace hash, we ignore it too */
2935 if (ftrace_lookup_ip(ops->func_hash->notrace_hash, rec->ip))
2936 return false;
2938 return true;
2941 static int ftrace_update_code(struct module *mod, struct ftrace_page *new_pgs)
2943 struct ftrace_page *pg;
2944 struct dyn_ftrace *p;
2945 u64 start, stop;
2946 unsigned long update_cnt = 0;
2947 unsigned long rec_flags = 0;
2948 int i;
2950 start = ftrace_now(raw_smp_processor_id());
2953 * When a module is loaded, this function is called to convert
2954 * the calls to mcount in its text to nops, and also to create
2955 * an entry in the ftrace data. Now, if ftrace is activated
2956 * after this call, but before the module sets its text to
2957 * read-only, the modification of enabling ftrace can fail if
2958 * the read-only is done while ftrace is converting the calls.
2959 * To prevent this, the module's records are set as disabled
2960 * and will be enabled after the call to set the module's text
2961 * to read-only.
2963 if (mod)
2964 rec_flags |= FTRACE_FL_DISABLED;
2966 for (pg = new_pgs; pg; pg = pg->next) {
2968 for (i = 0; i < pg->index; i++) {
2970 /* If something went wrong, bail without enabling anything */
2971 if (unlikely(ftrace_disabled))
2972 return -1;
2974 p = &pg->records[i];
2975 p->flags = rec_flags;
2978 * Do the initial record conversion from mcount jump
2979 * to the NOP instructions.
2981 if (!__is_defined(CC_USING_NOP_MCOUNT) &&
2982 !ftrace_code_disable(mod, p))
2983 break;
2985 update_cnt++;
2989 stop = ftrace_now(raw_smp_processor_id());
2990 ftrace_update_time = stop - start;
2991 ftrace_update_tot_cnt += update_cnt;
2993 return 0;
2996 static int ftrace_allocate_records(struct ftrace_page *pg, int count)
2998 int order;
2999 int cnt;
3001 if (WARN_ON(!count))
3002 return -EINVAL;
3004 order = get_count_order(DIV_ROUND_UP(count, ENTRIES_PER_PAGE));
3007 * We want to fill as much as possible. No more than a page
3008 * may be empty.
3010 while ((PAGE_SIZE << order) / ENTRY_SIZE >= count + ENTRIES_PER_PAGE)
3011 order--;
3013 again:
3014 pg->records = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, order);
3016 if (!pg->records) {
3017 /* if we can't allocate this size, try something smaller */
3018 if (!order)
3019 return -ENOMEM;
3020 order >>= 1;
3021 goto again;
3024 cnt = (PAGE_SIZE << order) / ENTRY_SIZE;
3025 pg->size = cnt;
3027 if (cnt > count)
3028 cnt = count;
3030 return cnt;
3033 static struct ftrace_page *
3034 ftrace_allocate_pages(unsigned long num_to_init)
3036 struct ftrace_page *start_pg;
3037 struct ftrace_page *pg;
3038 int order;
3039 int cnt;
3041 if (!num_to_init)
3042 return 0;
3044 start_pg = pg = kzalloc(sizeof(*pg), GFP_KERNEL);
3045 if (!pg)
3046 return NULL;
3049 * Try to allocate as much as possible in one continues
3050 * location that fills in all of the space. We want to
3051 * waste as little space as possible.
3053 for (;;) {
3054 cnt = ftrace_allocate_records(pg, num_to_init);
3055 if (cnt < 0)
3056 goto free_pages;
3058 num_to_init -= cnt;
3059 if (!num_to_init)
3060 break;
3062 pg->next = kzalloc(sizeof(*pg), GFP_KERNEL);
3063 if (!pg->next)
3064 goto free_pages;
3066 pg = pg->next;
3069 return start_pg;
3071 free_pages:
3072 pg = start_pg;
3073 while (pg) {
3074 order = get_count_order(pg->size / ENTRIES_PER_PAGE);
3075 free_pages((unsigned long)pg->records, order);
3076 start_pg = pg->next;
3077 kfree(pg);
3078 pg = start_pg;
3080 pr_info("ftrace: FAILED to allocate memory for functions\n");
3081 return NULL;
3084 #define FTRACE_BUFF_MAX (KSYM_SYMBOL_LEN+4) /* room for wildcards */
3086 struct ftrace_iterator {
3087 loff_t pos;
3088 loff_t func_pos;
3089 loff_t mod_pos;
3090 struct ftrace_page *pg;
3091 struct dyn_ftrace *func;
3092 struct ftrace_func_probe *probe;
3093 struct ftrace_func_entry *probe_entry;
3094 struct trace_parser parser;
3095 struct ftrace_hash *hash;
3096 struct ftrace_ops *ops;
3097 struct trace_array *tr;
3098 struct list_head *mod_list;
3099 int pidx;
3100 int idx;
3101 unsigned flags;
3104 static void *
3105 t_probe_next(struct seq_file *m, loff_t *pos)
3107 struct ftrace_iterator *iter = m->private;
3108 struct trace_array *tr = iter->ops->private;
3109 struct list_head *func_probes;
3110 struct ftrace_hash *hash;
3111 struct list_head *next;
3112 struct hlist_node *hnd = NULL;
3113 struct hlist_head *hhd;
3114 int size;
3116 (*pos)++;
3117 iter->pos = *pos;
3119 if (!tr)
3120 return NULL;
3122 func_probes = &tr->func_probes;
3123 if (list_empty(func_probes))
3124 return NULL;
3126 if (!iter->probe) {
3127 next = func_probes->next;
3128 iter->probe = list_entry(next, struct ftrace_func_probe, list);
3131 if (iter->probe_entry)
3132 hnd = &iter->probe_entry->hlist;
3134 hash = iter->probe->ops.func_hash->filter_hash;
3137 * A probe being registered may temporarily have an empty hash
3138 * and it's at the end of the func_probes list.
3140 if (!hash || hash == EMPTY_HASH)
3141 return NULL;
3143 size = 1 << hash->size_bits;
3145 retry:
3146 if (iter->pidx >= size) {
3147 if (iter->probe->list.next == func_probes)
3148 return NULL;
3149 next = iter->probe->list.next;
3150 iter->probe = list_entry(next, struct ftrace_func_probe, list);
3151 hash = iter->probe->ops.func_hash->filter_hash;
3152 size = 1 << hash->size_bits;
3153 iter->pidx = 0;
3156 hhd = &hash->buckets[iter->pidx];
3158 if (hlist_empty(hhd)) {
3159 iter->pidx++;
3160 hnd = NULL;
3161 goto retry;
3164 if (!hnd)
3165 hnd = hhd->first;
3166 else {
3167 hnd = hnd->next;
3168 if (!hnd) {
3169 iter->pidx++;
3170 goto retry;
3174 if (WARN_ON_ONCE(!hnd))
3175 return NULL;
3177 iter->probe_entry = hlist_entry(hnd, struct ftrace_func_entry, hlist);
3179 return iter;
3182 static void *t_probe_start(struct seq_file *m, loff_t *pos)
3184 struct ftrace_iterator *iter = m->private;
3185 void *p = NULL;
3186 loff_t l;
3188 if (!(iter->flags & FTRACE_ITER_DO_PROBES))
3189 return NULL;
3191 if (iter->mod_pos > *pos)
3192 return NULL;
3194 iter->probe = NULL;
3195 iter->probe_entry = NULL;
3196 iter->pidx = 0;
3197 for (l = 0; l <= (*pos - iter->mod_pos); ) {
3198 p = t_probe_next(m, &l);
3199 if (!p)
3200 break;
3202 if (!p)
3203 return NULL;
3205 /* Only set this if we have an item */
3206 iter->flags |= FTRACE_ITER_PROBE;
3208 return iter;
3211 static int
3212 t_probe_show(struct seq_file *m, struct ftrace_iterator *iter)
3214 struct ftrace_func_entry *probe_entry;
3215 struct ftrace_probe_ops *probe_ops;
3216 struct ftrace_func_probe *probe;
3218 probe = iter->probe;
3219 probe_entry = iter->probe_entry;
3221 if (WARN_ON_ONCE(!probe || !probe_entry))
3222 return -EIO;
3224 probe_ops = probe->probe_ops;
3226 if (probe_ops->print)
3227 return probe_ops->print(m, probe_entry->ip, probe_ops, probe->data);
3229 seq_printf(m, "%ps:%ps\n", (void *)probe_entry->ip,
3230 (void *)probe_ops->func);
3232 return 0;
3235 static void *
3236 t_mod_next(struct seq_file *m, loff_t *pos)
3238 struct ftrace_iterator *iter = m->private;
3239 struct trace_array *tr = iter->tr;
3241 (*pos)++;
3242 iter->pos = *pos;
3244 iter->mod_list = iter->mod_list->next;
3246 if (iter->mod_list == &tr->mod_trace ||
3247 iter->mod_list == &tr->mod_notrace) {
3248 iter->flags &= ~FTRACE_ITER_MOD;
3249 return NULL;
3252 iter->mod_pos = *pos;
3254 return iter;
3257 static void *t_mod_start(struct seq_file *m, loff_t *pos)
3259 struct ftrace_iterator *iter = m->private;
3260 void *p = NULL;
3261 loff_t l;
3263 if (iter->func_pos > *pos)
3264 return NULL;
3266 iter->mod_pos = iter->func_pos;
3268 /* probes are only available if tr is set */
3269 if (!iter->tr)
3270 return NULL;
3272 for (l = 0; l <= (*pos - iter->func_pos); ) {
3273 p = t_mod_next(m, &l);
3274 if (!p)
3275 break;
3277 if (!p) {
3278 iter->flags &= ~FTRACE_ITER_MOD;
3279 return t_probe_start(m, pos);
3282 /* Only set this if we have an item */
3283 iter->flags |= FTRACE_ITER_MOD;
3285 return iter;
3288 static int
3289 t_mod_show(struct seq_file *m, struct ftrace_iterator *iter)
3291 struct ftrace_mod_load *ftrace_mod;
3292 struct trace_array *tr = iter->tr;
3294 if (WARN_ON_ONCE(!iter->mod_list) ||
3295 iter->mod_list == &tr->mod_trace ||
3296 iter->mod_list == &tr->mod_notrace)
3297 return -EIO;
3299 ftrace_mod = list_entry(iter->mod_list, struct ftrace_mod_load, list);
3301 if (ftrace_mod->func)
3302 seq_printf(m, "%s", ftrace_mod->func);
3303 else
3304 seq_putc(m, '*');
3306 seq_printf(m, ":mod:%s\n", ftrace_mod->module);
3308 return 0;
3311 static void *
3312 t_func_next(struct seq_file *m, loff_t *pos)
3314 struct ftrace_iterator *iter = m->private;
3315 struct dyn_ftrace *rec = NULL;
3317 (*pos)++;
3319 retry:
3320 if (iter->idx >= iter->pg->index) {
3321 if (iter->pg->next) {
3322 iter->pg = iter->pg->next;
3323 iter->idx = 0;
3324 goto retry;
3326 } else {
3327 rec = &iter->pg->records[iter->idx++];
3328 if (((iter->flags & (FTRACE_ITER_FILTER | FTRACE_ITER_NOTRACE)) &&
3329 !ftrace_lookup_ip(iter->hash, rec->ip)) ||
3331 ((iter->flags & FTRACE_ITER_ENABLED) &&
3332 !(rec->flags & FTRACE_FL_ENABLED))) {
3334 rec = NULL;
3335 goto retry;
3339 if (!rec)
3340 return NULL;
3342 iter->pos = iter->func_pos = *pos;
3343 iter->func = rec;
3345 return iter;
3348 static void *
3349 t_next(struct seq_file *m, void *v, loff_t *pos)
3351 struct ftrace_iterator *iter = m->private;
3352 loff_t l = *pos; /* t_probe_start() must use original pos */
3353 void *ret;
3355 if (unlikely(ftrace_disabled))
3356 return NULL;
3358 if (iter->flags & FTRACE_ITER_PROBE)
3359 return t_probe_next(m, pos);
3361 if (iter->flags & FTRACE_ITER_MOD)
3362 return t_mod_next(m, pos);
3364 if (iter->flags & FTRACE_ITER_PRINTALL) {
3365 /* next must increment pos, and t_probe_start does not */
3366 (*pos)++;
3367 return t_mod_start(m, &l);
3370 ret = t_func_next(m, pos);
3372 if (!ret)
3373 return t_mod_start(m, &l);
3375 return ret;
3378 static void reset_iter_read(struct ftrace_iterator *iter)
3380 iter->pos = 0;
3381 iter->func_pos = 0;
3382 iter->flags &= ~(FTRACE_ITER_PRINTALL | FTRACE_ITER_PROBE | FTRACE_ITER_MOD);
3385 static void *t_start(struct seq_file *m, loff_t *pos)
3387 struct ftrace_iterator *iter = m->private;
3388 void *p = NULL;
3389 loff_t l;
3391 mutex_lock(&ftrace_lock);
3393 if (unlikely(ftrace_disabled))
3394 return NULL;
3397 * If an lseek was done, then reset and start from beginning.
3399 if (*pos < iter->pos)
3400 reset_iter_read(iter);
3403 * For set_ftrace_filter reading, if we have the filter
3404 * off, we can short cut and just print out that all
3405 * functions are enabled.
3407 if ((iter->flags & (FTRACE_ITER_FILTER | FTRACE_ITER_NOTRACE)) &&
3408 ftrace_hash_empty(iter->hash)) {
3409 iter->func_pos = 1; /* Account for the message */
3410 if (*pos > 0)
3411 return t_mod_start(m, pos);
3412 iter->flags |= FTRACE_ITER_PRINTALL;
3413 /* reset in case of seek/pread */
3414 iter->flags &= ~FTRACE_ITER_PROBE;
3415 return iter;
3418 if (iter->flags & FTRACE_ITER_MOD)
3419 return t_mod_start(m, pos);
3422 * Unfortunately, we need to restart at ftrace_pages_start
3423 * every time we let go of the ftrace_mutex. This is because
3424 * those pointers can change without the lock.
3426 iter->pg = ftrace_pages_start;
3427 iter->idx = 0;
3428 for (l = 0; l <= *pos; ) {
3429 p = t_func_next(m, &l);
3430 if (!p)
3431 break;
3434 if (!p)
3435 return t_mod_start(m, pos);
3437 return iter;
3440 static void t_stop(struct seq_file *m, void *p)
3442 mutex_unlock(&ftrace_lock);
3445 void * __weak
3446 arch_ftrace_trampoline_func(struct ftrace_ops *ops, struct dyn_ftrace *rec)
3448 return NULL;
3451 static void add_trampoline_func(struct seq_file *m, struct ftrace_ops *ops,
3452 struct dyn_ftrace *rec)
3454 void *ptr;
3456 ptr = arch_ftrace_trampoline_func(ops, rec);
3457 if (ptr)
3458 seq_printf(m, " ->%pS", ptr);
3461 static int t_show(struct seq_file *m, void *v)
3463 struct ftrace_iterator *iter = m->private;
3464 struct dyn_ftrace *rec;
3466 if (iter->flags & FTRACE_ITER_PROBE)
3467 return t_probe_show(m, iter);
3469 if (iter->flags & FTRACE_ITER_MOD)
3470 return t_mod_show(m, iter);
3472 if (iter->flags & FTRACE_ITER_PRINTALL) {
3473 if (iter->flags & FTRACE_ITER_NOTRACE)
3474 seq_puts(m, "#### no functions disabled ####\n");
3475 else
3476 seq_puts(m, "#### all functions enabled ####\n");
3477 return 0;
3480 rec = iter->func;
3482 if (!rec)
3483 return 0;
3485 seq_printf(m, "%ps", (void *)rec->ip);
3486 if (iter->flags & FTRACE_ITER_ENABLED) {
3487 struct ftrace_ops *ops;
3489 seq_printf(m, " (%ld)%s%s",
3490 ftrace_rec_count(rec),
3491 rec->flags & FTRACE_FL_REGS ? " R" : " ",
3492 rec->flags & FTRACE_FL_IPMODIFY ? " I" : " ");
3493 if (rec->flags & FTRACE_FL_TRAMP_EN) {
3494 ops = ftrace_find_tramp_ops_any(rec);
3495 if (ops) {
3496 do {
3497 seq_printf(m, "\ttramp: %pS (%pS)",
3498 (void *)ops->trampoline,
3499 (void *)ops->func);
3500 add_trampoline_func(m, ops, rec);
3501 ops = ftrace_find_tramp_ops_next(rec, ops);
3502 } while (ops);
3503 } else
3504 seq_puts(m, "\ttramp: ERROR!");
3505 } else {
3506 add_trampoline_func(m, NULL, rec);
3510 seq_putc(m, '\n');
3512 return 0;
3515 static const struct seq_operations show_ftrace_seq_ops = {
3516 .start = t_start,
3517 .next = t_next,
3518 .stop = t_stop,
3519 .show = t_show,
3522 static int
3523 ftrace_avail_open(struct inode *inode, struct file *file)
3525 struct ftrace_iterator *iter;
3527 if (unlikely(ftrace_disabled))
3528 return -ENODEV;
3530 iter = __seq_open_private(file, &show_ftrace_seq_ops, sizeof(*iter));
3531 if (!iter)
3532 return -ENOMEM;
3534 iter->pg = ftrace_pages_start;
3535 iter->ops = &global_ops;
3537 return 0;
3540 static int
3541 ftrace_enabled_open(struct inode *inode, struct file *file)
3543 struct ftrace_iterator *iter;
3545 iter = __seq_open_private(file, &show_ftrace_seq_ops, sizeof(*iter));
3546 if (!iter)
3547 return -ENOMEM;
3549 iter->pg = ftrace_pages_start;
3550 iter->flags = FTRACE_ITER_ENABLED;
3551 iter->ops = &global_ops;
3553 return 0;
3557 * ftrace_regex_open - initialize function tracer filter files
3558 * @ops: The ftrace_ops that hold the hash filters
3559 * @flag: The type of filter to process
3560 * @inode: The inode, usually passed in to your open routine
3561 * @file: The file, usually passed in to your open routine
3563 * ftrace_regex_open() initializes the filter files for the
3564 * @ops. Depending on @flag it may process the filter hash or
3565 * the notrace hash of @ops. With this called from the open
3566 * routine, you can use ftrace_filter_write() for the write
3567 * routine if @flag has FTRACE_ITER_FILTER set, or
3568 * ftrace_notrace_write() if @flag has FTRACE_ITER_NOTRACE set.
3569 * tracing_lseek() should be used as the lseek routine, and
3570 * release must call ftrace_regex_release().
3573 ftrace_regex_open(struct ftrace_ops *ops, int flag,
3574 struct inode *inode, struct file *file)
3576 struct ftrace_iterator *iter;
3577 struct ftrace_hash *hash;
3578 struct list_head *mod_head;
3579 struct trace_array *tr = ops->private;
3580 int ret = -ENOMEM;
3582 ftrace_ops_init(ops);
3584 if (unlikely(ftrace_disabled))
3585 return -ENODEV;
3587 if (tr && trace_array_get(tr) < 0)
3588 return -ENODEV;
3590 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
3591 if (!iter)
3592 goto out;
3594 if (trace_parser_get_init(&iter->parser, FTRACE_BUFF_MAX))
3595 goto out;
3597 iter->ops = ops;
3598 iter->flags = flag;
3599 iter->tr = tr;
3601 mutex_lock(&ops->func_hash->regex_lock);
3603 if (flag & FTRACE_ITER_NOTRACE) {
3604 hash = ops->func_hash->notrace_hash;
3605 mod_head = tr ? &tr->mod_notrace : NULL;
3606 } else {
3607 hash = ops->func_hash->filter_hash;
3608 mod_head = tr ? &tr->mod_trace : NULL;
3611 iter->mod_list = mod_head;
3613 if (file->f_mode & FMODE_WRITE) {
3614 const int size_bits = FTRACE_HASH_DEFAULT_BITS;
3616 if (file->f_flags & O_TRUNC) {
3617 iter->hash = alloc_ftrace_hash(size_bits);
3618 clear_ftrace_mod_list(mod_head);
3619 } else {
3620 iter->hash = alloc_and_copy_ftrace_hash(size_bits, hash);
3623 if (!iter->hash) {
3624 trace_parser_put(&iter->parser);
3625 goto out_unlock;
3627 } else
3628 iter->hash = hash;
3630 ret = 0;
3632 if (file->f_mode & FMODE_READ) {
3633 iter->pg = ftrace_pages_start;
3635 ret = seq_open(file, &show_ftrace_seq_ops);
3636 if (!ret) {
3637 struct seq_file *m = file->private_data;
3638 m->private = iter;
3639 } else {
3640 /* Failed */
3641 free_ftrace_hash(iter->hash);
3642 trace_parser_put(&iter->parser);
3644 } else
3645 file->private_data = iter;
3647 out_unlock:
3648 mutex_unlock(&ops->func_hash->regex_lock);
3650 out:
3651 if (ret) {
3652 kfree(iter);
3653 if (tr)
3654 trace_array_put(tr);
3657 return ret;
3660 static int
3661 ftrace_filter_open(struct inode *inode, struct file *file)
3663 struct ftrace_ops *ops = inode->i_private;
3665 return ftrace_regex_open(ops,
3666 FTRACE_ITER_FILTER | FTRACE_ITER_DO_PROBES,
3667 inode, file);
3670 static int
3671 ftrace_notrace_open(struct inode *inode, struct file *file)
3673 struct ftrace_ops *ops = inode->i_private;
3675 return ftrace_regex_open(ops, FTRACE_ITER_NOTRACE,
3676 inode, file);
3679 /* Type for quick search ftrace basic regexes (globs) from filter_parse_regex */
3680 struct ftrace_glob {
3681 char *search;
3682 unsigned len;
3683 int type;
3687 * If symbols in an architecture don't correspond exactly to the user-visible
3688 * name of what they represent, it is possible to define this function to
3689 * perform the necessary adjustments.
3691 char * __weak arch_ftrace_match_adjust(char *str, const char *search)
3693 return str;
3696 static int ftrace_match(char *str, struct ftrace_glob *g)
3698 int matched = 0;
3699 int slen;
3701 str = arch_ftrace_match_adjust(str, g->search);
3703 switch (g->type) {
3704 case MATCH_FULL:
3705 if (strcmp(str, g->search) == 0)
3706 matched = 1;
3707 break;
3708 case MATCH_FRONT_ONLY:
3709 if (strncmp(str, g->search, g->len) == 0)
3710 matched = 1;
3711 break;
3712 case MATCH_MIDDLE_ONLY:
3713 if (strstr(str, g->search))
3714 matched = 1;
3715 break;
3716 case MATCH_END_ONLY:
3717 slen = strlen(str);
3718 if (slen >= g->len &&
3719 memcmp(str + slen - g->len, g->search, g->len) == 0)
3720 matched = 1;
3721 break;
3722 case MATCH_GLOB:
3723 if (glob_match(g->search, str))
3724 matched = 1;
3725 break;
3728 return matched;
3731 static int
3732 enter_record(struct ftrace_hash *hash, struct dyn_ftrace *rec, int clear_filter)
3734 struct ftrace_func_entry *entry;
3735 int ret = 0;
3737 entry = ftrace_lookup_ip(hash, rec->ip);
3738 if (clear_filter) {
3739 /* Do nothing if it doesn't exist */
3740 if (!entry)
3741 return 0;
3743 free_hash_entry(hash, entry);
3744 } else {
3745 /* Do nothing if it exists */
3746 if (entry)
3747 return 0;
3749 ret = add_hash_entry(hash, rec->ip);
3751 return ret;
3754 static int
3755 ftrace_match_record(struct dyn_ftrace *rec, struct ftrace_glob *func_g,
3756 struct ftrace_glob *mod_g, int exclude_mod)
3758 char str[KSYM_SYMBOL_LEN];
3759 char *modname;
3761 kallsyms_lookup(rec->ip, NULL, NULL, &modname, str);
3763 if (mod_g) {
3764 int mod_matches = (modname) ? ftrace_match(modname, mod_g) : 0;
3766 /* blank module name to match all modules */
3767 if (!mod_g->len) {
3768 /* blank module globbing: modname xor exclude_mod */
3769 if (!exclude_mod != !modname)
3770 goto func_match;
3771 return 0;
3775 * exclude_mod is set to trace everything but the given
3776 * module. If it is set and the module matches, then
3777 * return 0. If it is not set, and the module doesn't match
3778 * also return 0. Otherwise, check the function to see if
3779 * that matches.
3781 if (!mod_matches == !exclude_mod)
3782 return 0;
3783 func_match:
3784 /* blank search means to match all funcs in the mod */
3785 if (!func_g->len)
3786 return 1;
3789 return ftrace_match(str, func_g);
3792 static int
3793 match_records(struct ftrace_hash *hash, char *func, int len, char *mod)
3795 struct ftrace_page *pg;
3796 struct dyn_ftrace *rec;
3797 struct ftrace_glob func_g = { .type = MATCH_FULL };
3798 struct ftrace_glob mod_g = { .type = MATCH_FULL };
3799 struct ftrace_glob *mod_match = (mod) ? &mod_g : NULL;
3800 int exclude_mod = 0;
3801 int found = 0;
3802 int ret;
3803 int clear_filter = 0;
3805 if (func) {
3806 func_g.type = filter_parse_regex(func, len, &func_g.search,
3807 &clear_filter);
3808 func_g.len = strlen(func_g.search);
3811 if (mod) {
3812 mod_g.type = filter_parse_regex(mod, strlen(mod),
3813 &mod_g.search, &exclude_mod);
3814 mod_g.len = strlen(mod_g.search);
3817 mutex_lock(&ftrace_lock);
3819 if (unlikely(ftrace_disabled))
3820 goto out_unlock;
3822 do_for_each_ftrace_rec(pg, rec) {
3824 if (rec->flags & FTRACE_FL_DISABLED)
3825 continue;
3827 if (ftrace_match_record(rec, &func_g, mod_match, exclude_mod)) {
3828 ret = enter_record(hash, rec, clear_filter);
3829 if (ret < 0) {
3830 found = ret;
3831 goto out_unlock;
3833 found = 1;
3835 } while_for_each_ftrace_rec();
3836 out_unlock:
3837 mutex_unlock(&ftrace_lock);
3839 return found;
3842 static int
3843 ftrace_match_records(struct ftrace_hash *hash, char *buff, int len)
3845 return match_records(hash, buff, len, NULL);
3848 static void ftrace_ops_update_code(struct ftrace_ops *ops,
3849 struct ftrace_ops_hash *old_hash)
3851 struct ftrace_ops *op;
3853 if (!ftrace_enabled)
3854 return;
3856 if (ops->flags & FTRACE_OPS_FL_ENABLED) {
3857 ftrace_run_modify_code(ops, FTRACE_UPDATE_CALLS, old_hash);
3858 return;
3862 * If this is the shared global_ops filter, then we need to
3863 * check if there is another ops that shares it, is enabled.
3864 * If so, we still need to run the modify code.
3866 if (ops->func_hash != &global_ops.local_hash)
3867 return;
3869 do_for_each_ftrace_op(op, ftrace_ops_list) {
3870 if (op->func_hash == &global_ops.local_hash &&
3871 op->flags & FTRACE_OPS_FL_ENABLED) {
3872 ftrace_run_modify_code(op, FTRACE_UPDATE_CALLS, old_hash);
3873 /* Only need to do this once */
3874 return;
3876 } while_for_each_ftrace_op(op);
3879 static int ftrace_hash_move_and_update_ops(struct ftrace_ops *ops,
3880 struct ftrace_hash **orig_hash,
3881 struct ftrace_hash *hash,
3882 int enable)
3884 struct ftrace_ops_hash old_hash_ops;
3885 struct ftrace_hash *old_hash;
3886 int ret;
3888 old_hash = *orig_hash;
3889 old_hash_ops.filter_hash = ops->func_hash->filter_hash;
3890 old_hash_ops.notrace_hash = ops->func_hash->notrace_hash;
3891 ret = ftrace_hash_move(ops, enable, orig_hash, hash);
3892 if (!ret) {
3893 ftrace_ops_update_code(ops, &old_hash_ops);
3894 free_ftrace_hash_rcu(old_hash);
3896 return ret;
3899 static bool module_exists(const char *module)
3901 /* All modules have the symbol __this_module */
3902 const char this_mod[] = "__this_module";
3903 char modname[MAX_PARAM_PREFIX_LEN + sizeof(this_mod) + 2];
3904 unsigned long val;
3905 int n;
3907 n = snprintf(modname, sizeof(modname), "%s:%s", module, this_mod);
3909 if (n > sizeof(modname) - 1)
3910 return false;
3912 val = module_kallsyms_lookup_name(modname);
3913 return val != 0;
3916 static int cache_mod(struct trace_array *tr,
3917 const char *func, char *module, int enable)
3919 struct ftrace_mod_load *ftrace_mod, *n;
3920 struct list_head *head = enable ? &tr->mod_trace : &tr->mod_notrace;
3921 int ret;
3923 mutex_lock(&ftrace_lock);
3925 /* We do not cache inverse filters */
3926 if (func[0] == '!') {
3927 func++;
3928 ret = -EINVAL;
3930 /* Look to remove this hash */
3931 list_for_each_entry_safe(ftrace_mod, n, head, list) {
3932 if (strcmp(ftrace_mod->module, module) != 0)
3933 continue;
3935 /* no func matches all */
3936 if (strcmp(func, "*") == 0 ||
3937 (ftrace_mod->func &&
3938 strcmp(ftrace_mod->func, func) == 0)) {
3939 ret = 0;
3940 free_ftrace_mod(ftrace_mod);
3941 continue;
3944 goto out;
3947 ret = -EINVAL;
3948 /* We only care about modules that have not been loaded yet */
3949 if (module_exists(module))
3950 goto out;
3952 /* Save this string off, and execute it when the module is loaded */
3953 ret = ftrace_add_mod(tr, func, module, enable);
3954 out:
3955 mutex_unlock(&ftrace_lock);
3957 return ret;
3960 static int
3961 ftrace_set_regex(struct ftrace_ops *ops, unsigned char *buf, int len,
3962 int reset, int enable);
3964 #ifdef CONFIG_MODULES
3965 static void process_mod_list(struct list_head *head, struct ftrace_ops *ops,
3966 char *mod, bool enable)
3968 struct ftrace_mod_load *ftrace_mod, *n;
3969 struct ftrace_hash **orig_hash, *new_hash;
3970 LIST_HEAD(process_mods);
3971 char *func;
3972 int ret;
3974 mutex_lock(&ops->func_hash->regex_lock);
3976 if (enable)
3977 orig_hash = &ops->func_hash->filter_hash;
3978 else
3979 orig_hash = &ops->func_hash->notrace_hash;
3981 new_hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS,
3982 *orig_hash);
3983 if (!new_hash)
3984 goto out; /* warn? */
3986 mutex_lock(&ftrace_lock);
3988 list_for_each_entry_safe(ftrace_mod, n, head, list) {
3990 if (strcmp(ftrace_mod->module, mod) != 0)
3991 continue;
3993 if (ftrace_mod->func)
3994 func = kstrdup(ftrace_mod->func, GFP_KERNEL);
3995 else
3996 func = kstrdup("*", GFP_KERNEL);
3998 if (!func) /* warn? */
3999 continue;
4001 list_del(&ftrace_mod->list);
4002 list_add(&ftrace_mod->list, &process_mods);
4004 /* Use the newly allocated func, as it may be "*" */
4005 kfree(ftrace_mod->func);
4006 ftrace_mod->func = func;
4009 mutex_unlock(&ftrace_lock);
4011 list_for_each_entry_safe(ftrace_mod, n, &process_mods, list) {
4013 func = ftrace_mod->func;
4015 /* Grabs ftrace_lock, which is why we have this extra step */
4016 match_records(new_hash, func, strlen(func), mod);
4017 free_ftrace_mod(ftrace_mod);
4020 if (enable && list_empty(head))
4021 new_hash->flags &= ~FTRACE_HASH_FL_MOD;
4023 mutex_lock(&ftrace_lock);
4025 ret = ftrace_hash_move_and_update_ops(ops, orig_hash,
4026 new_hash, enable);
4027 mutex_unlock(&ftrace_lock);
4029 out:
4030 mutex_unlock(&ops->func_hash->regex_lock);
4032 free_ftrace_hash(new_hash);
4035 static void process_cached_mods(const char *mod_name)
4037 struct trace_array *tr;
4038 char *mod;
4040 mod = kstrdup(mod_name, GFP_KERNEL);
4041 if (!mod)
4042 return;
4044 mutex_lock(&trace_types_lock);
4045 list_for_each_entry(tr, &ftrace_trace_arrays, list) {
4046 if (!list_empty(&tr->mod_trace))
4047 process_mod_list(&tr->mod_trace, tr->ops, mod, true);
4048 if (!list_empty(&tr->mod_notrace))
4049 process_mod_list(&tr->mod_notrace, tr->ops, mod, false);
4051 mutex_unlock(&trace_types_lock);
4053 kfree(mod);
4055 #endif
4058 * We register the module command as a template to show others how
4059 * to register the a command as well.
4062 static int
4063 ftrace_mod_callback(struct trace_array *tr, struct ftrace_hash *hash,
4064 char *func_orig, char *cmd, char *module, int enable)
4066 char *func;
4067 int ret;
4069 /* match_records() modifies func, and we need the original */
4070 func = kstrdup(func_orig, GFP_KERNEL);
4071 if (!func)
4072 return -ENOMEM;
4075 * cmd == 'mod' because we only registered this func
4076 * for the 'mod' ftrace_func_command.
4077 * But if you register one func with multiple commands,
4078 * you can tell which command was used by the cmd
4079 * parameter.
4081 ret = match_records(hash, func, strlen(func), module);
4082 kfree(func);
4084 if (!ret)
4085 return cache_mod(tr, func_orig, module, enable);
4086 if (ret < 0)
4087 return ret;
4088 return 0;
4091 static struct ftrace_func_command ftrace_mod_cmd = {
4092 .name = "mod",
4093 .func = ftrace_mod_callback,
4096 static int __init ftrace_mod_cmd_init(void)
4098 return register_ftrace_command(&ftrace_mod_cmd);
4100 core_initcall(ftrace_mod_cmd_init);
4102 static void function_trace_probe_call(unsigned long ip, unsigned long parent_ip,
4103 struct ftrace_ops *op, struct pt_regs *pt_regs)
4105 struct ftrace_probe_ops *probe_ops;
4106 struct ftrace_func_probe *probe;
4108 probe = container_of(op, struct ftrace_func_probe, ops);
4109 probe_ops = probe->probe_ops;
4112 * Disable preemption for these calls to prevent a RCU grace
4113 * period. This syncs the hash iteration and freeing of items
4114 * on the hash. rcu_read_lock is too dangerous here.
4116 preempt_disable_notrace();
4117 probe_ops->func(ip, parent_ip, probe->tr, probe_ops, probe->data);
4118 preempt_enable_notrace();
4121 struct ftrace_func_map {
4122 struct ftrace_func_entry entry;
4123 void *data;
4126 struct ftrace_func_mapper {
4127 struct ftrace_hash hash;
4131 * allocate_ftrace_func_mapper - allocate a new ftrace_func_mapper
4133 * Returns a ftrace_func_mapper descriptor that can be used to map ips to data.
4135 struct ftrace_func_mapper *allocate_ftrace_func_mapper(void)
4137 struct ftrace_hash *hash;
4140 * The mapper is simply a ftrace_hash, but since the entries
4141 * in the hash are not ftrace_func_entry type, we define it
4142 * as a separate structure.
4144 hash = alloc_ftrace_hash(FTRACE_HASH_DEFAULT_BITS);
4145 return (struct ftrace_func_mapper *)hash;
4149 * ftrace_func_mapper_find_ip - Find some data mapped to an ip
4150 * @mapper: The mapper that has the ip maps
4151 * @ip: the instruction pointer to find the data for
4153 * Returns the data mapped to @ip if found otherwise NULL. The return
4154 * is actually the address of the mapper data pointer. The address is
4155 * returned for use cases where the data is no bigger than a long, and
4156 * the user can use the data pointer as its data instead of having to
4157 * allocate more memory for the reference.
4159 void **ftrace_func_mapper_find_ip(struct ftrace_func_mapper *mapper,
4160 unsigned long ip)
4162 struct ftrace_func_entry *entry;
4163 struct ftrace_func_map *map;
4165 entry = ftrace_lookup_ip(&mapper->hash, ip);
4166 if (!entry)
4167 return NULL;
4169 map = (struct ftrace_func_map *)entry;
4170 return &map->data;
4174 * ftrace_func_mapper_add_ip - Map some data to an ip
4175 * @mapper: The mapper that has the ip maps
4176 * @ip: The instruction pointer address to map @data to
4177 * @data: The data to map to @ip
4179 * Returns 0 on succes otherwise an error.
4181 int ftrace_func_mapper_add_ip(struct ftrace_func_mapper *mapper,
4182 unsigned long ip, void *data)
4184 struct ftrace_func_entry *entry;
4185 struct ftrace_func_map *map;
4187 entry = ftrace_lookup_ip(&mapper->hash, ip);
4188 if (entry)
4189 return -EBUSY;
4191 map = kmalloc(sizeof(*map), GFP_KERNEL);
4192 if (!map)
4193 return -ENOMEM;
4195 map->entry.ip = ip;
4196 map->data = data;
4198 __add_hash_entry(&mapper->hash, &map->entry);
4200 return 0;
4204 * ftrace_func_mapper_remove_ip - Remove an ip from the mapping
4205 * @mapper: The mapper that has the ip maps
4206 * @ip: The instruction pointer address to remove the data from
4208 * Returns the data if it is found, otherwise NULL.
4209 * Note, if the data pointer is used as the data itself, (see
4210 * ftrace_func_mapper_find_ip(), then the return value may be meaningless,
4211 * if the data pointer was set to zero.
4213 void *ftrace_func_mapper_remove_ip(struct ftrace_func_mapper *mapper,
4214 unsigned long ip)
4216 struct ftrace_func_entry *entry;
4217 struct ftrace_func_map *map;
4218 void *data;
4220 entry = ftrace_lookup_ip(&mapper->hash, ip);
4221 if (!entry)
4222 return NULL;
4224 map = (struct ftrace_func_map *)entry;
4225 data = map->data;
4227 remove_hash_entry(&mapper->hash, entry);
4228 kfree(entry);
4230 return data;
4234 * free_ftrace_func_mapper - free a mapping of ips and data
4235 * @mapper: The mapper that has the ip maps
4236 * @free_func: A function to be called on each data item.
4238 * This is used to free the function mapper. The @free_func is optional
4239 * and can be used if the data needs to be freed as well.
4241 void free_ftrace_func_mapper(struct ftrace_func_mapper *mapper,
4242 ftrace_mapper_func free_func)
4244 struct ftrace_func_entry *entry;
4245 struct ftrace_func_map *map;
4246 struct hlist_head *hhd;
4247 int size, i;
4249 if (!mapper)
4250 return;
4252 if (free_func && mapper->hash.count) {
4253 size = 1 << mapper->hash.size_bits;
4254 for (i = 0; i < size; i++) {
4255 hhd = &mapper->hash.buckets[i];
4256 hlist_for_each_entry(entry, hhd, hlist) {
4257 map = (struct ftrace_func_map *)entry;
4258 free_func(map);
4262 free_ftrace_hash(&mapper->hash);
4265 static void release_probe(struct ftrace_func_probe *probe)
4267 struct ftrace_probe_ops *probe_ops;
4269 mutex_lock(&ftrace_lock);
4271 WARN_ON(probe->ref <= 0);
4273 /* Subtract the ref that was used to protect this instance */
4274 probe->ref--;
4276 if (!probe->ref) {
4277 probe_ops = probe->probe_ops;
4279 * Sending zero as ip tells probe_ops to free
4280 * the probe->data itself
4282 if (probe_ops->free)
4283 probe_ops->free(probe_ops, probe->tr, 0, probe->data);
4284 list_del(&probe->list);
4285 kfree(probe);
4287 mutex_unlock(&ftrace_lock);
4290 static void acquire_probe_locked(struct ftrace_func_probe *probe)
4293 * Add one ref to keep it from being freed when releasing the
4294 * ftrace_lock mutex.
4296 probe->ref++;
4300 register_ftrace_function_probe(char *glob, struct trace_array *tr,
4301 struct ftrace_probe_ops *probe_ops,
4302 void *data)
4304 struct ftrace_func_entry *entry;
4305 struct ftrace_func_probe *probe;
4306 struct ftrace_hash **orig_hash;
4307 struct ftrace_hash *old_hash;
4308 struct ftrace_hash *hash;
4309 int count = 0;
4310 int size;
4311 int ret;
4312 int i;
4314 if (WARN_ON(!tr))
4315 return -EINVAL;
4317 /* We do not support '!' for function probes */
4318 if (WARN_ON(glob[0] == '!'))
4319 return -EINVAL;
4322 mutex_lock(&ftrace_lock);
4323 /* Check if the probe_ops is already registered */
4324 list_for_each_entry(probe, &tr->func_probes, list) {
4325 if (probe->probe_ops == probe_ops)
4326 break;
4328 if (&probe->list == &tr->func_probes) {
4329 probe = kzalloc(sizeof(*probe), GFP_KERNEL);
4330 if (!probe) {
4331 mutex_unlock(&ftrace_lock);
4332 return -ENOMEM;
4334 probe->probe_ops = probe_ops;
4335 probe->ops.func = function_trace_probe_call;
4336 probe->tr = tr;
4337 ftrace_ops_init(&probe->ops);
4338 list_add(&probe->list, &tr->func_probes);
4341 acquire_probe_locked(probe);
4343 mutex_unlock(&ftrace_lock);
4346 * Note, there's a small window here that the func_hash->filter_hash
4347 * may be NULL or empty. Need to be carefule when reading the loop.
4349 mutex_lock(&probe->ops.func_hash->regex_lock);
4351 orig_hash = &probe->ops.func_hash->filter_hash;
4352 old_hash = *orig_hash;
4353 hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, old_hash);
4355 if (!hash) {
4356 ret = -ENOMEM;
4357 goto out;
4360 ret = ftrace_match_records(hash, glob, strlen(glob));
4362 /* Nothing found? */
4363 if (!ret)
4364 ret = -EINVAL;
4366 if (ret < 0)
4367 goto out;
4369 size = 1 << hash->size_bits;
4370 for (i = 0; i < size; i++) {
4371 hlist_for_each_entry(entry, &hash->buckets[i], hlist) {
4372 if (ftrace_lookup_ip(old_hash, entry->ip))
4373 continue;
4375 * The caller might want to do something special
4376 * for each function we find. We call the callback
4377 * to give the caller an opportunity to do so.
4379 if (probe_ops->init) {
4380 ret = probe_ops->init(probe_ops, tr,
4381 entry->ip, data,
4382 &probe->data);
4383 if (ret < 0) {
4384 if (probe_ops->free && count)
4385 probe_ops->free(probe_ops, tr,
4386 0, probe->data);
4387 probe->data = NULL;
4388 goto out;
4391 count++;
4395 mutex_lock(&ftrace_lock);
4397 if (!count) {
4398 /* Nothing was added? */
4399 ret = -EINVAL;
4400 goto out_unlock;
4403 ret = ftrace_hash_move_and_update_ops(&probe->ops, orig_hash,
4404 hash, 1);
4405 if (ret < 0)
4406 goto err_unlock;
4408 /* One ref for each new function traced */
4409 probe->ref += count;
4411 if (!(probe->ops.flags & FTRACE_OPS_FL_ENABLED))
4412 ret = ftrace_startup(&probe->ops, 0);
4414 out_unlock:
4415 mutex_unlock(&ftrace_lock);
4417 if (!ret)
4418 ret = count;
4419 out:
4420 mutex_unlock(&probe->ops.func_hash->regex_lock);
4421 free_ftrace_hash(hash);
4423 release_probe(probe);
4425 return ret;
4427 err_unlock:
4428 if (!probe_ops->free || !count)
4429 goto out_unlock;
4431 /* Failed to do the move, need to call the free functions */
4432 for (i = 0; i < size; i++) {
4433 hlist_for_each_entry(entry, &hash->buckets[i], hlist) {
4434 if (ftrace_lookup_ip(old_hash, entry->ip))
4435 continue;
4436 probe_ops->free(probe_ops, tr, entry->ip, probe->data);
4439 goto out_unlock;
4443 unregister_ftrace_function_probe_func(char *glob, struct trace_array *tr,
4444 struct ftrace_probe_ops *probe_ops)
4446 struct ftrace_ops_hash old_hash_ops;
4447 struct ftrace_func_entry *entry;
4448 struct ftrace_func_probe *probe;
4449 struct ftrace_glob func_g;
4450 struct ftrace_hash **orig_hash;
4451 struct ftrace_hash *old_hash;
4452 struct ftrace_hash *hash = NULL;
4453 struct hlist_node *tmp;
4454 struct hlist_head hhd;
4455 char str[KSYM_SYMBOL_LEN];
4456 int count = 0;
4457 int i, ret = -ENODEV;
4458 int size;
4460 if (!glob || !strlen(glob) || !strcmp(glob, "*"))
4461 func_g.search = NULL;
4462 else {
4463 int not;
4465 func_g.type = filter_parse_regex(glob, strlen(glob),
4466 &func_g.search, &not);
4467 func_g.len = strlen(func_g.search);
4469 /* we do not support '!' for function probes */
4470 if (WARN_ON(not))
4471 return -EINVAL;
4474 mutex_lock(&ftrace_lock);
4475 /* Check if the probe_ops is already registered */
4476 list_for_each_entry(probe, &tr->func_probes, list) {
4477 if (probe->probe_ops == probe_ops)
4478 break;
4480 if (&probe->list == &tr->func_probes)
4481 goto err_unlock_ftrace;
4483 ret = -EINVAL;
4484 if (!(probe->ops.flags & FTRACE_OPS_FL_INITIALIZED))
4485 goto err_unlock_ftrace;
4487 acquire_probe_locked(probe);
4489 mutex_unlock(&ftrace_lock);
4491 mutex_lock(&probe->ops.func_hash->regex_lock);
4493 orig_hash = &probe->ops.func_hash->filter_hash;
4494 old_hash = *orig_hash;
4496 if (ftrace_hash_empty(old_hash))
4497 goto out_unlock;
4499 old_hash_ops.filter_hash = old_hash;
4500 /* Probes only have filters */
4501 old_hash_ops.notrace_hash = NULL;
4503 ret = -ENOMEM;
4504 hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, old_hash);
4505 if (!hash)
4506 goto out_unlock;
4508 INIT_HLIST_HEAD(&hhd);
4510 size = 1 << hash->size_bits;
4511 for (i = 0; i < size; i++) {
4512 hlist_for_each_entry_safe(entry, tmp, &hash->buckets[i], hlist) {
4514 if (func_g.search) {
4515 kallsyms_lookup(entry->ip, NULL, NULL,
4516 NULL, str);
4517 if (!ftrace_match(str, &func_g))
4518 continue;
4520 count++;
4521 remove_hash_entry(hash, entry);
4522 hlist_add_head(&entry->hlist, &hhd);
4526 /* Nothing found? */
4527 if (!count) {
4528 ret = -EINVAL;
4529 goto out_unlock;
4532 mutex_lock(&ftrace_lock);
4534 WARN_ON(probe->ref < count);
4536 probe->ref -= count;
4538 if (ftrace_hash_empty(hash))
4539 ftrace_shutdown(&probe->ops, 0);
4541 ret = ftrace_hash_move_and_update_ops(&probe->ops, orig_hash,
4542 hash, 1);
4544 /* still need to update the function call sites */
4545 if (ftrace_enabled && !ftrace_hash_empty(hash))
4546 ftrace_run_modify_code(&probe->ops, FTRACE_UPDATE_CALLS,
4547 &old_hash_ops);
4548 synchronize_sched();
4550 hlist_for_each_entry_safe(entry, tmp, &hhd, hlist) {
4551 hlist_del(&entry->hlist);
4552 if (probe_ops->free)
4553 probe_ops->free(probe_ops, tr, entry->ip, probe->data);
4554 kfree(entry);
4556 mutex_unlock(&ftrace_lock);
4558 out_unlock:
4559 mutex_unlock(&probe->ops.func_hash->regex_lock);
4560 free_ftrace_hash(hash);
4562 release_probe(probe);
4564 return ret;
4566 err_unlock_ftrace:
4567 mutex_unlock(&ftrace_lock);
4568 return ret;
4571 void clear_ftrace_function_probes(struct trace_array *tr)
4573 struct ftrace_func_probe *probe, *n;
4575 list_for_each_entry_safe(probe, n, &tr->func_probes, list)
4576 unregister_ftrace_function_probe_func(NULL, tr, probe->probe_ops);
4579 static LIST_HEAD(ftrace_commands);
4580 static DEFINE_MUTEX(ftrace_cmd_mutex);
4583 * Currently we only register ftrace commands from __init, so mark this
4584 * __init too.
4586 __init int register_ftrace_command(struct ftrace_func_command *cmd)
4588 struct ftrace_func_command *p;
4589 int ret = 0;
4591 mutex_lock(&ftrace_cmd_mutex);
4592 list_for_each_entry(p, &ftrace_commands, list) {
4593 if (strcmp(cmd->name, p->name) == 0) {
4594 ret = -EBUSY;
4595 goto out_unlock;
4598 list_add(&cmd->list, &ftrace_commands);
4599 out_unlock:
4600 mutex_unlock(&ftrace_cmd_mutex);
4602 return ret;
4606 * Currently we only unregister ftrace commands from __init, so mark
4607 * this __init too.
4609 __init int unregister_ftrace_command(struct ftrace_func_command *cmd)
4611 struct ftrace_func_command *p, *n;
4612 int ret = -ENODEV;
4614 mutex_lock(&ftrace_cmd_mutex);
4615 list_for_each_entry_safe(p, n, &ftrace_commands, list) {
4616 if (strcmp(cmd->name, p->name) == 0) {
4617 ret = 0;
4618 list_del_init(&p->list);
4619 goto out_unlock;
4622 out_unlock:
4623 mutex_unlock(&ftrace_cmd_mutex);
4625 return ret;
4628 static int ftrace_process_regex(struct ftrace_iterator *iter,
4629 char *buff, int len, int enable)
4631 struct ftrace_hash *hash = iter->hash;
4632 struct trace_array *tr = iter->ops->private;
4633 char *func, *command, *next = buff;
4634 struct ftrace_func_command *p;
4635 int ret = -EINVAL;
4637 func = strsep(&next, ":");
4639 if (!next) {
4640 ret = ftrace_match_records(hash, func, len);
4641 if (!ret)
4642 ret = -EINVAL;
4643 if (ret < 0)
4644 return ret;
4645 return 0;
4648 /* command found */
4650 command = strsep(&next, ":");
4652 mutex_lock(&ftrace_cmd_mutex);
4653 list_for_each_entry(p, &ftrace_commands, list) {
4654 if (strcmp(p->name, command) == 0) {
4655 ret = p->func(tr, hash, func, command, next, enable);
4656 goto out_unlock;
4659 out_unlock:
4660 mutex_unlock(&ftrace_cmd_mutex);
4662 return ret;
4665 static ssize_t
4666 ftrace_regex_write(struct file *file, const char __user *ubuf,
4667 size_t cnt, loff_t *ppos, int enable)
4669 struct ftrace_iterator *iter;
4670 struct trace_parser *parser;
4671 ssize_t ret, read;
4673 if (!cnt)
4674 return 0;
4676 if (file->f_mode & FMODE_READ) {
4677 struct seq_file *m = file->private_data;
4678 iter = m->private;
4679 } else
4680 iter = file->private_data;
4682 if (unlikely(ftrace_disabled))
4683 return -ENODEV;
4685 /* iter->hash is a local copy, so we don't need regex_lock */
4687 parser = &iter->parser;
4688 read = trace_get_user(parser, ubuf, cnt, ppos);
4690 if (read >= 0 && trace_parser_loaded(parser) &&
4691 !trace_parser_cont(parser)) {
4692 ret = ftrace_process_regex(iter, parser->buffer,
4693 parser->idx, enable);
4694 trace_parser_clear(parser);
4695 if (ret < 0)
4696 goto out;
4699 ret = read;
4700 out:
4701 return ret;
4704 ssize_t
4705 ftrace_filter_write(struct file *file, const char __user *ubuf,
4706 size_t cnt, loff_t *ppos)
4708 return ftrace_regex_write(file, ubuf, cnt, ppos, 1);
4711 ssize_t
4712 ftrace_notrace_write(struct file *file, const char __user *ubuf,
4713 size_t cnt, loff_t *ppos)
4715 return ftrace_regex_write(file, ubuf, cnt, ppos, 0);
4718 static int
4719 ftrace_match_addr(struct ftrace_hash *hash, unsigned long ip, int remove)
4721 struct ftrace_func_entry *entry;
4723 if (!ftrace_location(ip))
4724 return -EINVAL;
4726 if (remove) {
4727 entry = ftrace_lookup_ip(hash, ip);
4728 if (!entry)
4729 return -ENOENT;
4730 free_hash_entry(hash, entry);
4731 return 0;
4734 return add_hash_entry(hash, ip);
4737 static int
4738 ftrace_set_hash(struct ftrace_ops *ops, unsigned char *buf, int len,
4739 unsigned long ip, int remove, int reset, int enable)
4741 struct ftrace_hash **orig_hash;
4742 struct ftrace_hash *hash;
4743 int ret;
4745 if (unlikely(ftrace_disabled))
4746 return -ENODEV;
4748 mutex_lock(&ops->func_hash->regex_lock);
4750 if (enable)
4751 orig_hash = &ops->func_hash->filter_hash;
4752 else
4753 orig_hash = &ops->func_hash->notrace_hash;
4755 if (reset)
4756 hash = alloc_ftrace_hash(FTRACE_HASH_DEFAULT_BITS);
4757 else
4758 hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, *orig_hash);
4760 if (!hash) {
4761 ret = -ENOMEM;
4762 goto out_regex_unlock;
4765 if (buf && !ftrace_match_records(hash, buf, len)) {
4766 ret = -EINVAL;
4767 goto out_regex_unlock;
4769 if (ip) {
4770 ret = ftrace_match_addr(hash, ip, remove);
4771 if (ret < 0)
4772 goto out_regex_unlock;
4775 mutex_lock(&ftrace_lock);
4776 ret = ftrace_hash_move_and_update_ops(ops, orig_hash, hash, enable);
4777 mutex_unlock(&ftrace_lock);
4779 out_regex_unlock:
4780 mutex_unlock(&ops->func_hash->regex_lock);
4782 free_ftrace_hash(hash);
4783 return ret;
4786 static int
4787 ftrace_set_addr(struct ftrace_ops *ops, unsigned long ip, int remove,
4788 int reset, int enable)
4790 return ftrace_set_hash(ops, 0, 0, ip, remove, reset, enable);
4794 * ftrace_set_filter_ip - set a function to filter on in ftrace by address
4795 * @ops - the ops to set the filter with
4796 * @ip - the address to add to or remove from the filter.
4797 * @remove - non zero to remove the ip from the filter
4798 * @reset - non zero to reset all filters before applying this filter.
4800 * Filters denote which functions should be enabled when tracing is enabled
4801 * If @ip is NULL, it failes to update filter.
4803 int ftrace_set_filter_ip(struct ftrace_ops *ops, unsigned long ip,
4804 int remove, int reset)
4806 ftrace_ops_init(ops);
4807 return ftrace_set_addr(ops, ip, remove, reset, 1);
4809 EXPORT_SYMBOL_GPL(ftrace_set_filter_ip);
4812 * ftrace_ops_set_global_filter - setup ops to use global filters
4813 * @ops - the ops which will use the global filters
4815 * ftrace users who need global function trace filtering should call this.
4816 * It can set the global filter only if ops were not initialized before.
4818 void ftrace_ops_set_global_filter(struct ftrace_ops *ops)
4820 if (ops->flags & FTRACE_OPS_FL_INITIALIZED)
4821 return;
4823 ftrace_ops_init(ops);
4824 ops->func_hash = &global_ops.local_hash;
4826 EXPORT_SYMBOL_GPL(ftrace_ops_set_global_filter);
4828 static int
4829 ftrace_set_regex(struct ftrace_ops *ops, unsigned char *buf, int len,
4830 int reset, int enable)
4832 return ftrace_set_hash(ops, buf, len, 0, 0, reset, enable);
4836 * ftrace_set_filter - set a function to filter on in ftrace
4837 * @ops - the ops to set the filter with
4838 * @buf - the string that holds the function filter text.
4839 * @len - the length of the string.
4840 * @reset - non zero to reset all filters before applying this filter.
4842 * Filters denote which functions should be enabled when tracing is enabled.
4843 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
4845 int ftrace_set_filter(struct ftrace_ops *ops, unsigned char *buf,
4846 int len, int reset)
4848 ftrace_ops_init(ops);
4849 return ftrace_set_regex(ops, buf, len, reset, 1);
4851 EXPORT_SYMBOL_GPL(ftrace_set_filter);
4854 * ftrace_set_notrace - set a function to not trace in ftrace
4855 * @ops - the ops to set the notrace filter with
4856 * @buf - the string that holds the function notrace text.
4857 * @len - the length of the string.
4858 * @reset - non zero to reset all filters before applying this filter.
4860 * Notrace Filters denote which functions should not be enabled when tracing
4861 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
4862 * for tracing.
4864 int ftrace_set_notrace(struct ftrace_ops *ops, unsigned char *buf,
4865 int len, int reset)
4867 ftrace_ops_init(ops);
4868 return ftrace_set_regex(ops, buf, len, reset, 0);
4870 EXPORT_SYMBOL_GPL(ftrace_set_notrace);
4872 * ftrace_set_global_filter - set a function to filter on with global tracers
4873 * @buf - the string that holds the function filter text.
4874 * @len - the length of the string.
4875 * @reset - non zero to reset all filters before applying this filter.
4877 * Filters denote which functions should be enabled when tracing is enabled.
4878 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
4880 void ftrace_set_global_filter(unsigned char *buf, int len, int reset)
4882 ftrace_set_regex(&global_ops, buf, len, reset, 1);
4884 EXPORT_SYMBOL_GPL(ftrace_set_global_filter);
4887 * ftrace_set_global_notrace - set a function to not trace with global tracers
4888 * @buf - the string that holds the function notrace text.
4889 * @len - the length of the string.
4890 * @reset - non zero to reset all filters before applying this filter.
4892 * Notrace Filters denote which functions should not be enabled when tracing
4893 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
4894 * for tracing.
4896 void ftrace_set_global_notrace(unsigned char *buf, int len, int reset)
4898 ftrace_set_regex(&global_ops, buf, len, reset, 0);
4900 EXPORT_SYMBOL_GPL(ftrace_set_global_notrace);
4903 * command line interface to allow users to set filters on boot up.
4905 #define FTRACE_FILTER_SIZE COMMAND_LINE_SIZE
4906 static char ftrace_notrace_buf[FTRACE_FILTER_SIZE] __initdata;
4907 static char ftrace_filter_buf[FTRACE_FILTER_SIZE] __initdata;
4909 /* Used by function selftest to not test if filter is set */
4910 bool ftrace_filter_param __initdata;
4912 static int __init set_ftrace_notrace(char *str)
4914 ftrace_filter_param = true;
4915 strlcpy(ftrace_notrace_buf, str, FTRACE_FILTER_SIZE);
4916 return 1;
4918 __setup("ftrace_notrace=", set_ftrace_notrace);
4920 static int __init set_ftrace_filter(char *str)
4922 ftrace_filter_param = true;
4923 strlcpy(ftrace_filter_buf, str, FTRACE_FILTER_SIZE);
4924 return 1;
4926 __setup("ftrace_filter=", set_ftrace_filter);
4928 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
4929 static char ftrace_graph_buf[FTRACE_FILTER_SIZE] __initdata;
4930 static char ftrace_graph_notrace_buf[FTRACE_FILTER_SIZE] __initdata;
4931 static int ftrace_graph_set_hash(struct ftrace_hash *hash, char *buffer);
4933 static int __init set_graph_function(char *str)
4935 strlcpy(ftrace_graph_buf, str, FTRACE_FILTER_SIZE);
4936 return 1;
4938 __setup("ftrace_graph_filter=", set_graph_function);
4940 static int __init set_graph_notrace_function(char *str)
4942 strlcpy(ftrace_graph_notrace_buf, str, FTRACE_FILTER_SIZE);
4943 return 1;
4945 __setup("ftrace_graph_notrace=", set_graph_notrace_function);
4947 static int __init set_graph_max_depth_function(char *str)
4949 if (!str)
4950 return 0;
4951 fgraph_max_depth = simple_strtoul(str, NULL, 0);
4952 return 1;
4954 __setup("ftrace_graph_max_depth=", set_graph_max_depth_function);
4956 static void __init set_ftrace_early_graph(char *buf, int enable)
4958 int ret;
4959 char *func;
4960 struct ftrace_hash *hash;
4962 hash = alloc_ftrace_hash(FTRACE_HASH_DEFAULT_BITS);
4963 if (WARN_ON(!hash))
4964 return;
4966 while (buf) {
4967 func = strsep(&buf, ",");
4968 /* we allow only one expression at a time */
4969 ret = ftrace_graph_set_hash(hash, func);
4970 if (ret)
4971 printk(KERN_DEBUG "ftrace: function %s not "
4972 "traceable\n", func);
4975 if (enable)
4976 ftrace_graph_hash = hash;
4977 else
4978 ftrace_graph_notrace_hash = hash;
4980 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
4982 void __init
4983 ftrace_set_early_filter(struct ftrace_ops *ops, char *buf, int enable)
4985 char *func;
4987 ftrace_ops_init(ops);
4989 while (buf) {
4990 func = strsep(&buf, ",");
4991 ftrace_set_regex(ops, func, strlen(func), 0, enable);
4995 static void __init set_ftrace_early_filters(void)
4997 if (ftrace_filter_buf[0])
4998 ftrace_set_early_filter(&global_ops, ftrace_filter_buf, 1);
4999 if (ftrace_notrace_buf[0])
5000 ftrace_set_early_filter(&global_ops, ftrace_notrace_buf, 0);
5001 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
5002 if (ftrace_graph_buf[0])
5003 set_ftrace_early_graph(ftrace_graph_buf, 1);
5004 if (ftrace_graph_notrace_buf[0])
5005 set_ftrace_early_graph(ftrace_graph_notrace_buf, 0);
5006 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
5009 int ftrace_regex_release(struct inode *inode, struct file *file)
5011 struct seq_file *m = (struct seq_file *)file->private_data;
5012 struct ftrace_iterator *iter;
5013 struct ftrace_hash **orig_hash;
5014 struct trace_parser *parser;
5015 int filter_hash;
5016 int ret;
5018 if (file->f_mode & FMODE_READ) {
5019 iter = m->private;
5020 seq_release(inode, file);
5021 } else
5022 iter = file->private_data;
5024 parser = &iter->parser;
5025 if (trace_parser_loaded(parser)) {
5026 ftrace_match_records(iter->hash, parser->buffer, parser->idx);
5029 trace_parser_put(parser);
5031 mutex_lock(&iter->ops->func_hash->regex_lock);
5033 if (file->f_mode & FMODE_WRITE) {
5034 filter_hash = !!(iter->flags & FTRACE_ITER_FILTER);
5036 if (filter_hash) {
5037 orig_hash = &iter->ops->func_hash->filter_hash;
5038 if (iter->tr && !list_empty(&iter->tr->mod_trace))
5039 iter->hash->flags |= FTRACE_HASH_FL_MOD;
5040 } else
5041 orig_hash = &iter->ops->func_hash->notrace_hash;
5043 mutex_lock(&ftrace_lock);
5044 ret = ftrace_hash_move_and_update_ops(iter->ops, orig_hash,
5045 iter->hash, filter_hash);
5046 mutex_unlock(&ftrace_lock);
5047 } else {
5048 /* For read only, the hash is the ops hash */
5049 iter->hash = NULL;
5052 mutex_unlock(&iter->ops->func_hash->regex_lock);
5053 free_ftrace_hash(iter->hash);
5054 if (iter->tr)
5055 trace_array_put(iter->tr);
5056 kfree(iter);
5058 return 0;
5061 static const struct file_operations ftrace_avail_fops = {
5062 .open = ftrace_avail_open,
5063 .read = seq_read,
5064 .llseek = seq_lseek,
5065 .release = seq_release_private,
5068 static const struct file_operations ftrace_enabled_fops = {
5069 .open = ftrace_enabled_open,
5070 .read = seq_read,
5071 .llseek = seq_lseek,
5072 .release = seq_release_private,
5075 static const struct file_operations ftrace_filter_fops = {
5076 .open = ftrace_filter_open,
5077 .read = seq_read,
5078 .write = ftrace_filter_write,
5079 .llseek = tracing_lseek,
5080 .release = ftrace_regex_release,
5083 static const struct file_operations ftrace_notrace_fops = {
5084 .open = ftrace_notrace_open,
5085 .read = seq_read,
5086 .write = ftrace_notrace_write,
5087 .llseek = tracing_lseek,
5088 .release = ftrace_regex_release,
5091 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
5093 static DEFINE_MUTEX(graph_lock);
5095 struct ftrace_hash __rcu *ftrace_graph_hash = EMPTY_HASH;
5096 struct ftrace_hash __rcu *ftrace_graph_notrace_hash = EMPTY_HASH;
5098 enum graph_filter_type {
5099 GRAPH_FILTER_NOTRACE = 0,
5100 GRAPH_FILTER_FUNCTION,
5103 #define FTRACE_GRAPH_EMPTY ((void *)1)
5105 struct ftrace_graph_data {
5106 struct ftrace_hash *hash;
5107 struct ftrace_func_entry *entry;
5108 int idx; /* for hash table iteration */
5109 enum graph_filter_type type;
5110 struct ftrace_hash *new_hash;
5111 const struct seq_operations *seq_ops;
5112 struct trace_parser parser;
5115 static void *
5116 __g_next(struct seq_file *m, loff_t *pos)
5118 struct ftrace_graph_data *fgd = m->private;
5119 struct ftrace_func_entry *entry = fgd->entry;
5120 struct hlist_head *head;
5121 int i, idx = fgd->idx;
5123 if (*pos >= fgd->hash->count)
5124 return NULL;
5126 if (entry) {
5127 hlist_for_each_entry_continue(entry, hlist) {
5128 fgd->entry = entry;
5129 return entry;
5132 idx++;
5135 for (i = idx; i < 1 << fgd->hash->size_bits; i++) {
5136 head = &fgd->hash->buckets[i];
5137 hlist_for_each_entry(entry, head, hlist) {
5138 fgd->entry = entry;
5139 fgd->idx = i;
5140 return entry;
5143 return NULL;
5146 static void *
5147 g_next(struct seq_file *m, void *v, loff_t *pos)
5149 (*pos)++;
5150 return __g_next(m, pos);
5153 static void *g_start(struct seq_file *m, loff_t *pos)
5155 struct ftrace_graph_data *fgd = m->private;
5157 mutex_lock(&graph_lock);
5159 if (fgd->type == GRAPH_FILTER_FUNCTION)
5160 fgd->hash = rcu_dereference_protected(ftrace_graph_hash,
5161 lockdep_is_held(&graph_lock));
5162 else
5163 fgd->hash = rcu_dereference_protected(ftrace_graph_notrace_hash,
5164 lockdep_is_held(&graph_lock));
5166 /* Nothing, tell g_show to print all functions are enabled */
5167 if (ftrace_hash_empty(fgd->hash) && !*pos)
5168 return FTRACE_GRAPH_EMPTY;
5170 fgd->idx = 0;
5171 fgd->entry = NULL;
5172 return __g_next(m, pos);
5175 static void g_stop(struct seq_file *m, void *p)
5177 mutex_unlock(&graph_lock);
5180 static int g_show(struct seq_file *m, void *v)
5182 struct ftrace_func_entry *entry = v;
5184 if (!entry)
5185 return 0;
5187 if (entry == FTRACE_GRAPH_EMPTY) {
5188 struct ftrace_graph_data *fgd = m->private;
5190 if (fgd->type == GRAPH_FILTER_FUNCTION)
5191 seq_puts(m, "#### all functions enabled ####\n");
5192 else
5193 seq_puts(m, "#### no functions disabled ####\n");
5194 return 0;
5197 seq_printf(m, "%ps\n", (void *)entry->ip);
5199 return 0;
5202 static const struct seq_operations ftrace_graph_seq_ops = {
5203 .start = g_start,
5204 .next = g_next,
5205 .stop = g_stop,
5206 .show = g_show,
5209 static int
5210 __ftrace_graph_open(struct inode *inode, struct file *file,
5211 struct ftrace_graph_data *fgd)
5213 int ret = 0;
5214 struct ftrace_hash *new_hash = NULL;
5216 if (file->f_mode & FMODE_WRITE) {
5217 const int size_bits = FTRACE_HASH_DEFAULT_BITS;
5219 if (trace_parser_get_init(&fgd->parser, FTRACE_BUFF_MAX))
5220 return -ENOMEM;
5222 if (file->f_flags & O_TRUNC)
5223 new_hash = alloc_ftrace_hash(size_bits);
5224 else
5225 new_hash = alloc_and_copy_ftrace_hash(size_bits,
5226 fgd->hash);
5227 if (!new_hash) {
5228 ret = -ENOMEM;
5229 goto out;
5233 if (file->f_mode & FMODE_READ) {
5234 ret = seq_open(file, &ftrace_graph_seq_ops);
5235 if (!ret) {
5236 struct seq_file *m = file->private_data;
5237 m->private = fgd;
5238 } else {
5239 /* Failed */
5240 free_ftrace_hash(new_hash);
5241 new_hash = NULL;
5243 } else
5244 file->private_data = fgd;
5246 out:
5247 if (ret < 0 && file->f_mode & FMODE_WRITE)
5248 trace_parser_put(&fgd->parser);
5250 fgd->new_hash = new_hash;
5253 * All uses of fgd->hash must be taken with the graph_lock
5254 * held. The graph_lock is going to be released, so force
5255 * fgd->hash to be reinitialized when it is taken again.
5257 fgd->hash = NULL;
5259 return ret;
5262 static int
5263 ftrace_graph_open(struct inode *inode, struct file *file)
5265 struct ftrace_graph_data *fgd;
5266 int ret;
5268 if (unlikely(ftrace_disabled))
5269 return -ENODEV;
5271 fgd = kmalloc(sizeof(*fgd), GFP_KERNEL);
5272 if (fgd == NULL)
5273 return -ENOMEM;
5275 mutex_lock(&graph_lock);
5277 fgd->hash = rcu_dereference_protected(ftrace_graph_hash,
5278 lockdep_is_held(&graph_lock));
5279 fgd->type = GRAPH_FILTER_FUNCTION;
5280 fgd->seq_ops = &ftrace_graph_seq_ops;
5282 ret = __ftrace_graph_open(inode, file, fgd);
5283 if (ret < 0)
5284 kfree(fgd);
5286 mutex_unlock(&graph_lock);
5287 return ret;
5290 static int
5291 ftrace_graph_notrace_open(struct inode *inode, struct file *file)
5293 struct ftrace_graph_data *fgd;
5294 int ret;
5296 if (unlikely(ftrace_disabled))
5297 return -ENODEV;
5299 fgd = kmalloc(sizeof(*fgd), GFP_KERNEL);
5300 if (fgd == NULL)
5301 return -ENOMEM;
5303 mutex_lock(&graph_lock);
5305 fgd->hash = rcu_dereference_protected(ftrace_graph_notrace_hash,
5306 lockdep_is_held(&graph_lock));
5307 fgd->type = GRAPH_FILTER_NOTRACE;
5308 fgd->seq_ops = &ftrace_graph_seq_ops;
5310 ret = __ftrace_graph_open(inode, file, fgd);
5311 if (ret < 0)
5312 kfree(fgd);
5314 mutex_unlock(&graph_lock);
5315 return ret;
5318 static int
5319 ftrace_graph_release(struct inode *inode, struct file *file)
5321 struct ftrace_graph_data *fgd;
5322 struct ftrace_hash *old_hash, *new_hash;
5323 struct trace_parser *parser;
5324 int ret = 0;
5326 if (file->f_mode & FMODE_READ) {
5327 struct seq_file *m = file->private_data;
5329 fgd = m->private;
5330 seq_release(inode, file);
5331 } else {
5332 fgd = file->private_data;
5336 if (file->f_mode & FMODE_WRITE) {
5338 parser = &fgd->parser;
5340 if (trace_parser_loaded((parser))) {
5341 ret = ftrace_graph_set_hash(fgd->new_hash,
5342 parser->buffer);
5345 trace_parser_put(parser);
5347 new_hash = __ftrace_hash_move(fgd->new_hash);
5348 if (!new_hash) {
5349 ret = -ENOMEM;
5350 goto out;
5353 mutex_lock(&graph_lock);
5355 if (fgd->type == GRAPH_FILTER_FUNCTION) {
5356 old_hash = rcu_dereference_protected(ftrace_graph_hash,
5357 lockdep_is_held(&graph_lock));
5358 rcu_assign_pointer(ftrace_graph_hash, new_hash);
5359 } else {
5360 old_hash = rcu_dereference_protected(ftrace_graph_notrace_hash,
5361 lockdep_is_held(&graph_lock));
5362 rcu_assign_pointer(ftrace_graph_notrace_hash, new_hash);
5365 mutex_unlock(&graph_lock);
5368 * We need to do a hard force of sched synchronization.
5369 * This is because we use preempt_disable() to do RCU, but
5370 * the function tracers can be called where RCU is not watching
5371 * (like before user_exit()). We can not rely on the RCU
5372 * infrastructure to do the synchronization, thus we must do it
5373 * ourselves.
5375 schedule_on_each_cpu(ftrace_sync);
5377 free_ftrace_hash(old_hash);
5380 out:
5381 free_ftrace_hash(fgd->new_hash);
5382 kfree(fgd);
5384 return ret;
5387 static int
5388 ftrace_graph_set_hash(struct ftrace_hash *hash, char *buffer)
5390 struct ftrace_glob func_g;
5391 struct dyn_ftrace *rec;
5392 struct ftrace_page *pg;
5393 struct ftrace_func_entry *entry;
5394 int fail = 1;
5395 int not;
5397 /* decode regex */
5398 func_g.type = filter_parse_regex(buffer, strlen(buffer),
5399 &func_g.search, &not);
5401 func_g.len = strlen(func_g.search);
5403 mutex_lock(&ftrace_lock);
5405 if (unlikely(ftrace_disabled)) {
5406 mutex_unlock(&ftrace_lock);
5407 return -ENODEV;
5410 do_for_each_ftrace_rec(pg, rec) {
5412 if (rec->flags & FTRACE_FL_DISABLED)
5413 continue;
5415 if (ftrace_match_record(rec, &func_g, NULL, 0)) {
5416 entry = ftrace_lookup_ip(hash, rec->ip);
5418 if (!not) {
5419 fail = 0;
5421 if (entry)
5422 continue;
5423 if (add_hash_entry(hash, rec->ip) < 0)
5424 goto out;
5425 } else {
5426 if (entry) {
5427 free_hash_entry(hash, entry);
5428 fail = 0;
5432 } while_for_each_ftrace_rec();
5433 out:
5434 mutex_unlock(&ftrace_lock);
5436 if (fail)
5437 return -EINVAL;
5439 return 0;
5442 static ssize_t
5443 ftrace_graph_write(struct file *file, const char __user *ubuf,
5444 size_t cnt, loff_t *ppos)
5446 ssize_t read, ret = 0;
5447 struct ftrace_graph_data *fgd = file->private_data;
5448 struct trace_parser *parser;
5450 if (!cnt)
5451 return 0;
5453 /* Read mode uses seq functions */
5454 if (file->f_mode & FMODE_READ) {
5455 struct seq_file *m = file->private_data;
5456 fgd = m->private;
5459 parser = &fgd->parser;
5461 read = trace_get_user(parser, ubuf, cnt, ppos);
5463 if (read >= 0 && trace_parser_loaded(parser) &&
5464 !trace_parser_cont(parser)) {
5466 ret = ftrace_graph_set_hash(fgd->new_hash,
5467 parser->buffer);
5468 trace_parser_clear(parser);
5471 if (!ret)
5472 ret = read;
5474 return ret;
5477 static const struct file_operations ftrace_graph_fops = {
5478 .open = ftrace_graph_open,
5479 .read = seq_read,
5480 .write = ftrace_graph_write,
5481 .llseek = tracing_lseek,
5482 .release = ftrace_graph_release,
5485 static const struct file_operations ftrace_graph_notrace_fops = {
5486 .open = ftrace_graph_notrace_open,
5487 .read = seq_read,
5488 .write = ftrace_graph_write,
5489 .llseek = tracing_lseek,
5490 .release = ftrace_graph_release,
5492 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
5494 void ftrace_create_filter_files(struct ftrace_ops *ops,
5495 struct dentry *parent)
5498 trace_create_file("set_ftrace_filter", 0644, parent,
5499 ops, &ftrace_filter_fops);
5501 trace_create_file("set_ftrace_notrace", 0644, parent,
5502 ops, &ftrace_notrace_fops);
5506 * The name "destroy_filter_files" is really a misnomer. Although
5507 * in the future, it may actualy delete the files, but this is
5508 * really intended to make sure the ops passed in are disabled
5509 * and that when this function returns, the caller is free to
5510 * free the ops.
5512 * The "destroy" name is only to match the "create" name that this
5513 * should be paired with.
5515 void ftrace_destroy_filter_files(struct ftrace_ops *ops)
5517 mutex_lock(&ftrace_lock);
5518 if (ops->flags & FTRACE_OPS_FL_ENABLED)
5519 ftrace_shutdown(ops, 0);
5520 ops->flags |= FTRACE_OPS_FL_DELETED;
5521 ftrace_free_filter(ops);
5522 mutex_unlock(&ftrace_lock);
5525 static __init int ftrace_init_dyn_tracefs(struct dentry *d_tracer)
5528 trace_create_file("available_filter_functions", 0444,
5529 d_tracer, NULL, &ftrace_avail_fops);
5531 trace_create_file("enabled_functions", 0444,
5532 d_tracer, NULL, &ftrace_enabled_fops);
5534 ftrace_create_filter_files(&global_ops, d_tracer);
5536 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
5537 trace_create_file("set_graph_function", 0644, d_tracer,
5538 NULL,
5539 &ftrace_graph_fops);
5540 trace_create_file("set_graph_notrace", 0644, d_tracer,
5541 NULL,
5542 &ftrace_graph_notrace_fops);
5543 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
5545 return 0;
5548 static int ftrace_cmp_ips(const void *a, const void *b)
5550 const unsigned long *ipa = a;
5551 const unsigned long *ipb = b;
5553 if (*ipa > *ipb)
5554 return 1;
5555 if (*ipa < *ipb)
5556 return -1;
5557 return 0;
5560 static int ftrace_process_locs(struct module *mod,
5561 unsigned long *start,
5562 unsigned long *end)
5564 struct ftrace_page *start_pg;
5565 struct ftrace_page *pg;
5566 struct dyn_ftrace *rec;
5567 unsigned long count;
5568 unsigned long *p;
5569 unsigned long addr;
5570 unsigned long flags = 0; /* Shut up gcc */
5571 int ret = -ENOMEM;
5573 count = end - start;
5575 if (!count)
5576 return 0;
5578 sort(start, count, sizeof(*start),
5579 ftrace_cmp_ips, NULL);
5581 start_pg = ftrace_allocate_pages(count);
5582 if (!start_pg)
5583 return -ENOMEM;
5585 mutex_lock(&ftrace_lock);
5588 * Core and each module needs their own pages, as
5589 * modules will free them when they are removed.
5590 * Force a new page to be allocated for modules.
5592 if (!mod) {
5593 WARN_ON(ftrace_pages || ftrace_pages_start);
5594 /* First initialization */
5595 ftrace_pages = ftrace_pages_start = start_pg;
5596 } else {
5597 if (!ftrace_pages)
5598 goto out;
5600 if (WARN_ON(ftrace_pages->next)) {
5601 /* Hmm, we have free pages? */
5602 while (ftrace_pages->next)
5603 ftrace_pages = ftrace_pages->next;
5606 ftrace_pages->next = start_pg;
5609 p = start;
5610 pg = start_pg;
5611 while (p < end) {
5612 addr = ftrace_call_adjust(*p++);
5614 * Some architecture linkers will pad between
5615 * the different mcount_loc sections of different
5616 * object files to satisfy alignments.
5617 * Skip any NULL pointers.
5619 if (!addr)
5620 continue;
5622 if (pg->index == pg->size) {
5623 /* We should have allocated enough */
5624 if (WARN_ON(!pg->next))
5625 break;
5626 pg = pg->next;
5629 rec = &pg->records[pg->index++];
5630 rec->ip = addr;
5633 /* We should have used all pages */
5634 WARN_ON(pg->next);
5636 /* Assign the last page to ftrace_pages */
5637 ftrace_pages = pg;
5640 * We only need to disable interrupts on start up
5641 * because we are modifying code that an interrupt
5642 * may execute, and the modification is not atomic.
5643 * But for modules, nothing runs the code we modify
5644 * until we are finished with it, and there's no
5645 * reason to cause large interrupt latencies while we do it.
5647 if (!mod)
5648 local_irq_save(flags);
5649 ftrace_update_code(mod, start_pg);
5650 if (!mod)
5651 local_irq_restore(flags);
5652 ret = 0;
5653 out:
5654 mutex_unlock(&ftrace_lock);
5656 return ret;
5659 struct ftrace_mod_func {
5660 struct list_head list;
5661 char *name;
5662 unsigned long ip;
5663 unsigned int size;
5666 struct ftrace_mod_map {
5667 struct rcu_head rcu;
5668 struct list_head list;
5669 struct module *mod;
5670 unsigned long start_addr;
5671 unsigned long end_addr;
5672 struct list_head funcs;
5673 unsigned int num_funcs;
5676 #ifdef CONFIG_MODULES
5678 #define next_to_ftrace_page(p) container_of(p, struct ftrace_page, next)
5680 static LIST_HEAD(ftrace_mod_maps);
5682 static int referenced_filters(struct dyn_ftrace *rec)
5684 struct ftrace_ops *ops;
5685 int cnt = 0;
5687 for (ops = ftrace_ops_list; ops != &ftrace_list_end; ops = ops->next) {
5688 if (ops_references_rec(ops, rec)) {
5689 cnt++;
5690 if (ops->flags & FTRACE_OPS_FL_SAVE_REGS)
5691 rec->flags |= FTRACE_FL_REGS;
5695 return cnt;
5698 static void
5699 clear_mod_from_hash(struct ftrace_page *pg, struct ftrace_hash *hash)
5701 struct ftrace_func_entry *entry;
5702 struct dyn_ftrace *rec;
5703 int i;
5705 if (ftrace_hash_empty(hash))
5706 return;
5708 for (i = 0; i < pg->index; i++) {
5709 rec = &pg->records[i];
5710 entry = __ftrace_lookup_ip(hash, rec->ip);
5712 * Do not allow this rec to match again.
5713 * Yeah, it may waste some memory, but will be removed
5714 * if/when the hash is modified again.
5716 if (entry)
5717 entry->ip = 0;
5721 /* Clear any records from hashs */
5722 static void clear_mod_from_hashes(struct ftrace_page *pg)
5724 struct trace_array *tr;
5726 mutex_lock(&trace_types_lock);
5727 list_for_each_entry(tr, &ftrace_trace_arrays, list) {
5728 if (!tr->ops || !tr->ops->func_hash)
5729 continue;
5730 mutex_lock(&tr->ops->func_hash->regex_lock);
5731 clear_mod_from_hash(pg, tr->ops->func_hash->filter_hash);
5732 clear_mod_from_hash(pg, tr->ops->func_hash->notrace_hash);
5733 mutex_unlock(&tr->ops->func_hash->regex_lock);
5735 mutex_unlock(&trace_types_lock);
5738 static void ftrace_free_mod_map(struct rcu_head *rcu)
5740 struct ftrace_mod_map *mod_map = container_of(rcu, struct ftrace_mod_map, rcu);
5741 struct ftrace_mod_func *mod_func;
5742 struct ftrace_mod_func *n;
5744 /* All the contents of mod_map are now not visible to readers */
5745 list_for_each_entry_safe(mod_func, n, &mod_map->funcs, list) {
5746 kfree(mod_func->name);
5747 list_del(&mod_func->list);
5748 kfree(mod_func);
5751 kfree(mod_map);
5754 void ftrace_release_mod(struct module *mod)
5756 struct ftrace_mod_map *mod_map;
5757 struct ftrace_mod_map *n;
5758 struct dyn_ftrace *rec;
5759 struct ftrace_page **last_pg;
5760 struct ftrace_page *tmp_page = NULL;
5761 struct ftrace_page *pg;
5762 int order;
5764 mutex_lock(&ftrace_lock);
5766 if (ftrace_disabled)
5767 goto out_unlock;
5769 list_for_each_entry_safe(mod_map, n, &ftrace_mod_maps, list) {
5770 if (mod_map->mod == mod) {
5771 list_del_rcu(&mod_map->list);
5772 call_rcu_sched(&mod_map->rcu, ftrace_free_mod_map);
5773 break;
5778 * Each module has its own ftrace_pages, remove
5779 * them from the list.
5781 last_pg = &ftrace_pages_start;
5782 for (pg = ftrace_pages_start; pg; pg = *last_pg) {
5783 rec = &pg->records[0];
5784 if (within_module_core(rec->ip, mod) ||
5785 within_module_init(rec->ip, mod)) {
5787 * As core pages are first, the first
5788 * page should never be a module page.
5790 if (WARN_ON(pg == ftrace_pages_start))
5791 goto out_unlock;
5793 /* Check if we are deleting the last page */
5794 if (pg == ftrace_pages)
5795 ftrace_pages = next_to_ftrace_page(last_pg);
5797 ftrace_update_tot_cnt -= pg->index;
5798 *last_pg = pg->next;
5800 pg->next = tmp_page;
5801 tmp_page = pg;
5802 } else
5803 last_pg = &pg->next;
5805 out_unlock:
5806 mutex_unlock(&ftrace_lock);
5808 for (pg = tmp_page; pg; pg = tmp_page) {
5810 /* Needs to be called outside of ftrace_lock */
5811 clear_mod_from_hashes(pg);
5813 order = get_count_order(pg->size / ENTRIES_PER_PAGE);
5814 free_pages((unsigned long)pg->records, order);
5815 tmp_page = pg->next;
5816 kfree(pg);
5820 void ftrace_module_enable(struct module *mod)
5822 struct dyn_ftrace *rec;
5823 struct ftrace_page *pg;
5825 mutex_lock(&ftrace_lock);
5827 if (ftrace_disabled)
5828 goto out_unlock;
5831 * If the tracing is enabled, go ahead and enable the record.
5833 * The reason not to enable the record immediatelly is the
5834 * inherent check of ftrace_make_nop/ftrace_make_call for
5835 * correct previous instructions. Making first the NOP
5836 * conversion puts the module to the correct state, thus
5837 * passing the ftrace_make_call check.
5839 * We also delay this to after the module code already set the
5840 * text to read-only, as we now need to set it back to read-write
5841 * so that we can modify the text.
5843 if (ftrace_start_up)
5844 ftrace_arch_code_modify_prepare();
5846 do_for_each_ftrace_rec(pg, rec) {
5847 int cnt;
5849 * do_for_each_ftrace_rec() is a double loop.
5850 * module text shares the pg. If a record is
5851 * not part of this module, then skip this pg,
5852 * which the "break" will do.
5854 if (!within_module_core(rec->ip, mod) &&
5855 !within_module_init(rec->ip, mod))
5856 break;
5858 cnt = 0;
5861 * When adding a module, we need to check if tracers are
5862 * currently enabled and if they are, and can trace this record,
5863 * we need to enable the module functions as well as update the
5864 * reference counts for those function records.
5866 if (ftrace_start_up)
5867 cnt += referenced_filters(rec);
5869 rec->flags &= ~FTRACE_FL_DISABLED;
5870 rec->flags += cnt;
5872 if (ftrace_start_up && cnt) {
5873 int failed = __ftrace_replace_code(rec, 1);
5874 if (failed) {
5875 ftrace_bug(failed, rec);
5876 goto out_loop;
5880 } while_for_each_ftrace_rec();
5882 out_loop:
5883 if (ftrace_start_up)
5884 ftrace_arch_code_modify_post_process();
5886 out_unlock:
5887 mutex_unlock(&ftrace_lock);
5889 process_cached_mods(mod->name);
5892 void ftrace_module_init(struct module *mod)
5894 if (ftrace_disabled || !mod->num_ftrace_callsites)
5895 return;
5897 ftrace_process_locs(mod, mod->ftrace_callsites,
5898 mod->ftrace_callsites + mod->num_ftrace_callsites);
5901 static void save_ftrace_mod_rec(struct ftrace_mod_map *mod_map,
5902 struct dyn_ftrace *rec)
5904 struct ftrace_mod_func *mod_func;
5905 unsigned long symsize;
5906 unsigned long offset;
5907 char str[KSYM_SYMBOL_LEN];
5908 char *modname;
5909 const char *ret;
5911 ret = kallsyms_lookup(rec->ip, &symsize, &offset, &modname, str);
5912 if (!ret)
5913 return;
5915 mod_func = kmalloc(sizeof(*mod_func), GFP_KERNEL);
5916 if (!mod_func)
5917 return;
5919 mod_func->name = kstrdup(str, GFP_KERNEL);
5920 if (!mod_func->name) {
5921 kfree(mod_func);
5922 return;
5925 mod_func->ip = rec->ip - offset;
5926 mod_func->size = symsize;
5928 mod_map->num_funcs++;
5930 list_add_rcu(&mod_func->list, &mod_map->funcs);
5933 static struct ftrace_mod_map *
5934 allocate_ftrace_mod_map(struct module *mod,
5935 unsigned long start, unsigned long end)
5937 struct ftrace_mod_map *mod_map;
5939 mod_map = kmalloc(sizeof(*mod_map), GFP_KERNEL);
5940 if (!mod_map)
5941 return NULL;
5943 mod_map->mod = mod;
5944 mod_map->start_addr = start;
5945 mod_map->end_addr = end;
5946 mod_map->num_funcs = 0;
5948 INIT_LIST_HEAD_RCU(&mod_map->funcs);
5950 list_add_rcu(&mod_map->list, &ftrace_mod_maps);
5952 return mod_map;
5955 static const char *
5956 ftrace_func_address_lookup(struct ftrace_mod_map *mod_map,
5957 unsigned long addr, unsigned long *size,
5958 unsigned long *off, char *sym)
5960 struct ftrace_mod_func *found_func = NULL;
5961 struct ftrace_mod_func *mod_func;
5963 list_for_each_entry_rcu(mod_func, &mod_map->funcs, list) {
5964 if (addr >= mod_func->ip &&
5965 addr < mod_func->ip + mod_func->size) {
5966 found_func = mod_func;
5967 break;
5971 if (found_func) {
5972 if (size)
5973 *size = found_func->size;
5974 if (off)
5975 *off = addr - found_func->ip;
5976 if (sym)
5977 strlcpy(sym, found_func->name, KSYM_NAME_LEN);
5979 return found_func->name;
5982 return NULL;
5985 const char *
5986 ftrace_mod_address_lookup(unsigned long addr, unsigned long *size,
5987 unsigned long *off, char **modname, char *sym)
5989 struct ftrace_mod_map *mod_map;
5990 const char *ret = NULL;
5992 /* mod_map is freed via call_rcu_sched() */
5993 preempt_disable();
5994 list_for_each_entry_rcu(mod_map, &ftrace_mod_maps, list) {
5995 ret = ftrace_func_address_lookup(mod_map, addr, size, off, sym);
5996 if (ret) {
5997 if (modname)
5998 *modname = mod_map->mod->name;
5999 break;
6002 preempt_enable();
6004 return ret;
6007 int ftrace_mod_get_kallsym(unsigned int symnum, unsigned long *value,
6008 char *type, char *name,
6009 char *module_name, int *exported)
6011 struct ftrace_mod_map *mod_map;
6012 struct ftrace_mod_func *mod_func;
6014 preempt_disable();
6015 list_for_each_entry_rcu(mod_map, &ftrace_mod_maps, list) {
6017 if (symnum >= mod_map->num_funcs) {
6018 symnum -= mod_map->num_funcs;
6019 continue;
6022 list_for_each_entry_rcu(mod_func, &mod_map->funcs, list) {
6023 if (symnum > 1) {
6024 symnum--;
6025 continue;
6028 *value = mod_func->ip;
6029 *type = 'T';
6030 strlcpy(name, mod_func->name, KSYM_NAME_LEN);
6031 strlcpy(module_name, mod_map->mod->name, MODULE_NAME_LEN);
6032 *exported = 1;
6033 preempt_enable();
6034 return 0;
6036 WARN_ON(1);
6037 break;
6039 preempt_enable();
6040 return -ERANGE;
6043 #else
6044 static void save_ftrace_mod_rec(struct ftrace_mod_map *mod_map,
6045 struct dyn_ftrace *rec) { }
6046 static inline struct ftrace_mod_map *
6047 allocate_ftrace_mod_map(struct module *mod,
6048 unsigned long start, unsigned long end)
6050 return NULL;
6052 #endif /* CONFIG_MODULES */
6054 struct ftrace_init_func {
6055 struct list_head list;
6056 unsigned long ip;
6059 /* Clear any init ips from hashes */
6060 static void
6061 clear_func_from_hash(struct ftrace_init_func *func, struct ftrace_hash *hash)
6063 struct ftrace_func_entry *entry;
6065 if (ftrace_hash_empty(hash))
6066 return;
6068 entry = __ftrace_lookup_ip(hash, func->ip);
6071 * Do not allow this rec to match again.
6072 * Yeah, it may waste some memory, but will be removed
6073 * if/when the hash is modified again.
6075 if (entry)
6076 entry->ip = 0;
6079 static void
6080 clear_func_from_hashes(struct ftrace_init_func *func)
6082 struct trace_array *tr;
6084 mutex_lock(&trace_types_lock);
6085 list_for_each_entry(tr, &ftrace_trace_arrays, list) {
6086 if (!tr->ops || !tr->ops->func_hash)
6087 continue;
6088 mutex_lock(&tr->ops->func_hash->regex_lock);
6089 clear_func_from_hash(func, tr->ops->func_hash->filter_hash);
6090 clear_func_from_hash(func, tr->ops->func_hash->notrace_hash);
6091 mutex_unlock(&tr->ops->func_hash->regex_lock);
6093 mutex_unlock(&trace_types_lock);
6096 static void add_to_clear_hash_list(struct list_head *clear_list,
6097 struct dyn_ftrace *rec)
6099 struct ftrace_init_func *func;
6101 func = kmalloc(sizeof(*func), GFP_KERNEL);
6102 if (!func) {
6103 WARN_ONCE(1, "alloc failure, ftrace filter could be stale\n");
6104 return;
6107 func->ip = rec->ip;
6108 list_add(&func->list, clear_list);
6111 void ftrace_free_mem(struct module *mod, void *start_ptr, void *end_ptr)
6113 unsigned long start = (unsigned long)(start_ptr);
6114 unsigned long end = (unsigned long)(end_ptr);
6115 struct ftrace_page **last_pg = &ftrace_pages_start;
6116 struct ftrace_page *pg;
6117 struct dyn_ftrace *rec;
6118 struct dyn_ftrace key;
6119 struct ftrace_mod_map *mod_map = NULL;
6120 struct ftrace_init_func *func, *func_next;
6121 struct list_head clear_hash;
6122 int order;
6124 INIT_LIST_HEAD(&clear_hash);
6126 key.ip = start;
6127 key.flags = end; /* overload flags, as it is unsigned long */
6129 mutex_lock(&ftrace_lock);
6132 * If we are freeing module init memory, then check if
6133 * any tracer is active. If so, we need to save a mapping of
6134 * the module functions being freed with the address.
6136 if (mod && ftrace_ops_list != &ftrace_list_end)
6137 mod_map = allocate_ftrace_mod_map(mod, start, end);
6139 for (pg = ftrace_pages_start; pg; last_pg = &pg->next, pg = *last_pg) {
6140 if (end < pg->records[0].ip ||
6141 start >= (pg->records[pg->index - 1].ip + MCOUNT_INSN_SIZE))
6142 continue;
6143 again:
6144 rec = bsearch(&key, pg->records, pg->index,
6145 sizeof(struct dyn_ftrace),
6146 ftrace_cmp_recs);
6147 if (!rec)
6148 continue;
6150 /* rec will be cleared from hashes after ftrace_lock unlock */
6151 add_to_clear_hash_list(&clear_hash, rec);
6153 if (mod_map)
6154 save_ftrace_mod_rec(mod_map, rec);
6156 pg->index--;
6157 ftrace_update_tot_cnt--;
6158 if (!pg->index) {
6159 *last_pg = pg->next;
6160 order = get_count_order(pg->size / ENTRIES_PER_PAGE);
6161 free_pages((unsigned long)pg->records, order);
6162 kfree(pg);
6163 pg = container_of(last_pg, struct ftrace_page, next);
6164 if (!(*last_pg))
6165 ftrace_pages = pg;
6166 continue;
6168 memmove(rec, rec + 1,
6169 (pg->index - (rec - pg->records)) * sizeof(*rec));
6170 /* More than one function may be in this block */
6171 goto again;
6173 mutex_unlock(&ftrace_lock);
6175 list_for_each_entry_safe(func, func_next, &clear_hash, list) {
6176 clear_func_from_hashes(func);
6177 kfree(func);
6181 void __init ftrace_free_init_mem(void)
6183 void *start = (void *)(&__init_begin);
6184 void *end = (void *)(&__init_end);
6186 ftrace_free_mem(NULL, start, end);
6189 void __init ftrace_init(void)
6191 extern unsigned long __start_mcount_loc[];
6192 extern unsigned long __stop_mcount_loc[];
6193 unsigned long count, flags;
6194 int ret;
6196 local_irq_save(flags);
6197 ret = ftrace_dyn_arch_init();
6198 local_irq_restore(flags);
6199 if (ret)
6200 goto failed;
6202 count = __stop_mcount_loc - __start_mcount_loc;
6203 if (!count) {
6204 pr_info("ftrace: No functions to be traced?\n");
6205 goto failed;
6208 pr_info("ftrace: allocating %ld entries in %ld pages\n",
6209 count, count / ENTRIES_PER_PAGE + 1);
6211 last_ftrace_enabled = ftrace_enabled = 1;
6213 ret = ftrace_process_locs(NULL,
6214 __start_mcount_loc,
6215 __stop_mcount_loc);
6217 set_ftrace_early_filters();
6219 return;
6220 failed:
6221 ftrace_disabled = 1;
6224 /* Do nothing if arch does not support this */
6225 void __weak arch_ftrace_update_trampoline(struct ftrace_ops *ops)
6229 static void ftrace_update_trampoline(struct ftrace_ops *ops)
6231 arch_ftrace_update_trampoline(ops);
6234 void ftrace_init_trace_array(struct trace_array *tr)
6236 INIT_LIST_HEAD(&tr->func_probes);
6237 INIT_LIST_HEAD(&tr->mod_trace);
6238 INIT_LIST_HEAD(&tr->mod_notrace);
6240 #else
6242 static struct ftrace_ops global_ops = {
6243 .func = ftrace_stub,
6244 .flags = FTRACE_OPS_FL_RECURSION_SAFE |
6245 FTRACE_OPS_FL_INITIALIZED |
6246 FTRACE_OPS_FL_PID,
6249 static int __init ftrace_nodyn_init(void)
6251 ftrace_enabled = 1;
6252 return 0;
6254 core_initcall(ftrace_nodyn_init);
6256 static inline int ftrace_init_dyn_tracefs(struct dentry *d_tracer) { return 0; }
6257 static inline void ftrace_startup_enable(int command) { }
6258 static inline void ftrace_startup_all(int command) { }
6259 /* Keep as macros so we do not need to define the commands */
6260 # define ftrace_startup(ops, command) \
6261 ({ \
6262 int ___ret = __register_ftrace_function(ops); \
6263 if (!___ret) \
6264 (ops)->flags |= FTRACE_OPS_FL_ENABLED; \
6265 ___ret; \
6267 # define ftrace_shutdown(ops, command) \
6268 ({ \
6269 int ___ret = __unregister_ftrace_function(ops); \
6270 if (!___ret) \
6271 (ops)->flags &= ~FTRACE_OPS_FL_ENABLED; \
6272 ___ret; \
6275 # define ftrace_startup_sysctl() do { } while (0)
6276 # define ftrace_shutdown_sysctl() do { } while (0)
6278 static inline int
6279 ftrace_ops_test(struct ftrace_ops *ops, unsigned long ip, void *regs)
6281 return 1;
6284 static void ftrace_update_trampoline(struct ftrace_ops *ops)
6288 #endif /* CONFIG_DYNAMIC_FTRACE */
6290 __init void ftrace_init_global_array_ops(struct trace_array *tr)
6292 tr->ops = &global_ops;
6293 tr->ops->private = tr;
6294 ftrace_init_trace_array(tr);
6297 void ftrace_init_array_ops(struct trace_array *tr, ftrace_func_t func)
6299 /* If we filter on pids, update to use the pid function */
6300 if (tr->flags & TRACE_ARRAY_FL_GLOBAL) {
6301 if (WARN_ON(tr->ops->func != ftrace_stub))
6302 printk("ftrace ops had %pS for function\n",
6303 tr->ops->func);
6305 tr->ops->func = func;
6306 tr->ops->private = tr;
6309 void ftrace_reset_array_ops(struct trace_array *tr)
6311 tr->ops->func = ftrace_stub;
6314 static nokprobe_inline void
6315 __ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip,
6316 struct ftrace_ops *ignored, struct pt_regs *regs)
6318 struct ftrace_ops *op;
6319 int bit;
6321 bit = trace_test_and_set_recursion(TRACE_LIST_START, TRACE_LIST_MAX);
6322 if (bit < 0)
6323 return;
6326 * Some of the ops may be dynamically allocated,
6327 * they must be freed after a synchronize_sched().
6329 preempt_disable_notrace();
6331 do_for_each_ftrace_op(op, ftrace_ops_list) {
6333 * Check the following for each ops before calling their func:
6334 * if RCU flag is set, then rcu_is_watching() must be true
6335 * if PER_CPU is set, then ftrace_function_local_disable()
6336 * must be false
6337 * Otherwise test if the ip matches the ops filter
6339 * If any of the above fails then the op->func() is not executed.
6341 if ((!(op->flags & FTRACE_OPS_FL_RCU) || rcu_is_watching()) &&
6342 ftrace_ops_test(op, ip, regs)) {
6343 if (FTRACE_WARN_ON(!op->func)) {
6344 pr_warn("op=%p %pS\n", op, op);
6345 goto out;
6347 op->func(ip, parent_ip, op, regs);
6349 } while_for_each_ftrace_op(op);
6350 out:
6351 preempt_enable_notrace();
6352 trace_clear_recursion(bit);
6356 * Some archs only support passing ip and parent_ip. Even though
6357 * the list function ignores the op parameter, we do not want any
6358 * C side effects, where a function is called without the caller
6359 * sending a third parameter.
6360 * Archs are to support both the regs and ftrace_ops at the same time.
6361 * If they support ftrace_ops, it is assumed they support regs.
6362 * If call backs want to use regs, they must either check for regs
6363 * being NULL, or CONFIG_DYNAMIC_FTRACE_WITH_REGS.
6364 * Note, CONFIG_DYNAMIC_FTRACE_WITH_REGS expects a full regs to be saved.
6365 * An architecture can pass partial regs with ftrace_ops and still
6366 * set the ARCH_SUPPORTS_FTRACE_OPS.
6368 #if ARCH_SUPPORTS_FTRACE_OPS
6369 static void ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip,
6370 struct ftrace_ops *op, struct pt_regs *regs)
6372 __ftrace_ops_list_func(ip, parent_ip, NULL, regs);
6374 NOKPROBE_SYMBOL(ftrace_ops_list_func);
6375 #else
6376 static void ftrace_ops_no_ops(unsigned long ip, unsigned long parent_ip)
6378 __ftrace_ops_list_func(ip, parent_ip, NULL, NULL);
6380 NOKPROBE_SYMBOL(ftrace_ops_no_ops);
6381 #endif
6384 * If there's only one function registered but it does not support
6385 * recursion, needs RCU protection and/or requires per cpu handling, then
6386 * this function will be called by the mcount trampoline.
6388 static void ftrace_ops_assist_func(unsigned long ip, unsigned long parent_ip,
6389 struct ftrace_ops *op, struct pt_regs *regs)
6391 int bit;
6393 bit = trace_test_and_set_recursion(TRACE_LIST_START, TRACE_LIST_MAX);
6394 if (bit < 0)
6395 return;
6397 preempt_disable_notrace();
6399 if (!(op->flags & FTRACE_OPS_FL_RCU) || rcu_is_watching())
6400 op->func(ip, parent_ip, op, regs);
6402 preempt_enable_notrace();
6403 trace_clear_recursion(bit);
6405 NOKPROBE_SYMBOL(ftrace_ops_assist_func);
6408 * ftrace_ops_get_func - get the function a trampoline should call
6409 * @ops: the ops to get the function for
6411 * Normally the mcount trampoline will call the ops->func, but there
6412 * are times that it should not. For example, if the ops does not
6413 * have its own recursion protection, then it should call the
6414 * ftrace_ops_assist_func() instead.
6416 * Returns the function that the trampoline should call for @ops.
6418 ftrace_func_t ftrace_ops_get_func(struct ftrace_ops *ops)
6421 * If the function does not handle recursion, needs to be RCU safe,
6422 * or does per cpu logic, then we need to call the assist handler.
6424 if (!(ops->flags & FTRACE_OPS_FL_RECURSION_SAFE) ||
6425 ops->flags & FTRACE_OPS_FL_RCU)
6426 return ftrace_ops_assist_func;
6428 return ops->func;
6431 static void
6432 ftrace_filter_pid_sched_switch_probe(void *data, bool preempt,
6433 struct task_struct *prev, struct task_struct *next)
6435 struct trace_array *tr = data;
6436 struct trace_pid_list *pid_list;
6438 pid_list = rcu_dereference_sched(tr->function_pids);
6440 this_cpu_write(tr->trace_buffer.data->ftrace_ignore_pid,
6441 trace_ignore_this_task(pid_list, next));
6444 static void
6445 ftrace_pid_follow_sched_process_fork(void *data,
6446 struct task_struct *self,
6447 struct task_struct *task)
6449 struct trace_pid_list *pid_list;
6450 struct trace_array *tr = data;
6452 pid_list = rcu_dereference_sched(tr->function_pids);
6453 trace_filter_add_remove_task(pid_list, self, task);
6456 static void
6457 ftrace_pid_follow_sched_process_exit(void *data, struct task_struct *task)
6459 struct trace_pid_list *pid_list;
6460 struct trace_array *tr = data;
6462 pid_list = rcu_dereference_sched(tr->function_pids);
6463 trace_filter_add_remove_task(pid_list, NULL, task);
6466 void ftrace_pid_follow_fork(struct trace_array *tr, bool enable)
6468 if (enable) {
6469 register_trace_sched_process_fork(ftrace_pid_follow_sched_process_fork,
6470 tr);
6471 register_trace_sched_process_free(ftrace_pid_follow_sched_process_exit,
6472 tr);
6473 } else {
6474 unregister_trace_sched_process_fork(ftrace_pid_follow_sched_process_fork,
6475 tr);
6476 unregister_trace_sched_process_free(ftrace_pid_follow_sched_process_exit,
6477 tr);
6481 static void clear_ftrace_pids(struct trace_array *tr)
6483 struct trace_pid_list *pid_list;
6484 int cpu;
6486 pid_list = rcu_dereference_protected(tr->function_pids,
6487 lockdep_is_held(&ftrace_lock));
6488 if (!pid_list)
6489 return;
6491 unregister_trace_sched_switch(ftrace_filter_pid_sched_switch_probe, tr);
6493 for_each_possible_cpu(cpu)
6494 per_cpu_ptr(tr->trace_buffer.data, cpu)->ftrace_ignore_pid = false;
6496 rcu_assign_pointer(tr->function_pids, NULL);
6498 /* Wait till all users are no longer using pid filtering */
6499 synchronize_sched();
6501 trace_free_pid_list(pid_list);
6504 void ftrace_clear_pids(struct trace_array *tr)
6506 mutex_lock(&ftrace_lock);
6508 clear_ftrace_pids(tr);
6510 mutex_unlock(&ftrace_lock);
6513 static void ftrace_pid_reset(struct trace_array *tr)
6515 mutex_lock(&ftrace_lock);
6516 clear_ftrace_pids(tr);
6518 ftrace_update_pid_func();
6519 ftrace_startup_all(0);
6521 mutex_unlock(&ftrace_lock);
6524 /* Greater than any max PID */
6525 #define FTRACE_NO_PIDS (void *)(PID_MAX_LIMIT + 1)
6527 static void *fpid_start(struct seq_file *m, loff_t *pos)
6528 __acquires(RCU)
6530 struct trace_pid_list *pid_list;
6531 struct trace_array *tr = m->private;
6533 mutex_lock(&ftrace_lock);
6534 rcu_read_lock_sched();
6536 pid_list = rcu_dereference_sched(tr->function_pids);
6538 if (!pid_list)
6539 return !(*pos) ? FTRACE_NO_PIDS : NULL;
6541 return trace_pid_start(pid_list, pos);
6544 static void *fpid_next(struct seq_file *m, void *v, loff_t *pos)
6546 struct trace_array *tr = m->private;
6547 struct trace_pid_list *pid_list = rcu_dereference_sched(tr->function_pids);
6549 if (v == FTRACE_NO_PIDS) {
6550 (*pos)++;
6551 return NULL;
6553 return trace_pid_next(pid_list, v, pos);
6556 static void fpid_stop(struct seq_file *m, void *p)
6557 __releases(RCU)
6559 rcu_read_unlock_sched();
6560 mutex_unlock(&ftrace_lock);
6563 static int fpid_show(struct seq_file *m, void *v)
6565 if (v == FTRACE_NO_PIDS) {
6566 seq_puts(m, "no pid\n");
6567 return 0;
6570 return trace_pid_show(m, v);
6573 static const struct seq_operations ftrace_pid_sops = {
6574 .start = fpid_start,
6575 .next = fpid_next,
6576 .stop = fpid_stop,
6577 .show = fpid_show,
6580 static int
6581 ftrace_pid_open(struct inode *inode, struct file *file)
6583 struct trace_array *tr = inode->i_private;
6584 struct seq_file *m;
6585 int ret = 0;
6587 if (trace_array_get(tr) < 0)
6588 return -ENODEV;
6590 if ((file->f_mode & FMODE_WRITE) &&
6591 (file->f_flags & O_TRUNC))
6592 ftrace_pid_reset(tr);
6594 ret = seq_open(file, &ftrace_pid_sops);
6595 if (ret < 0) {
6596 trace_array_put(tr);
6597 } else {
6598 m = file->private_data;
6599 /* copy tr over to seq ops */
6600 m->private = tr;
6603 return ret;
6606 static void ignore_task_cpu(void *data)
6608 struct trace_array *tr = data;
6609 struct trace_pid_list *pid_list;
6612 * This function is called by on_each_cpu() while the
6613 * event_mutex is held.
6615 pid_list = rcu_dereference_protected(tr->function_pids,
6616 mutex_is_locked(&ftrace_lock));
6618 this_cpu_write(tr->trace_buffer.data->ftrace_ignore_pid,
6619 trace_ignore_this_task(pid_list, current));
6622 static ssize_t
6623 ftrace_pid_write(struct file *filp, const char __user *ubuf,
6624 size_t cnt, loff_t *ppos)
6626 struct seq_file *m = filp->private_data;
6627 struct trace_array *tr = m->private;
6628 struct trace_pid_list *filtered_pids = NULL;
6629 struct trace_pid_list *pid_list;
6630 ssize_t ret;
6632 if (!cnt)
6633 return 0;
6635 mutex_lock(&ftrace_lock);
6637 filtered_pids = rcu_dereference_protected(tr->function_pids,
6638 lockdep_is_held(&ftrace_lock));
6640 ret = trace_pid_write(filtered_pids, &pid_list, ubuf, cnt);
6641 if (ret < 0)
6642 goto out;
6644 rcu_assign_pointer(tr->function_pids, pid_list);
6646 if (filtered_pids) {
6647 synchronize_sched();
6648 trace_free_pid_list(filtered_pids);
6649 } else if (pid_list) {
6650 /* Register a probe to set whether to ignore the tracing of a task */
6651 register_trace_sched_switch(ftrace_filter_pid_sched_switch_probe, tr);
6655 * Ignoring of pids is done at task switch. But we have to
6656 * check for those tasks that are currently running.
6657 * Always do this in case a pid was appended or removed.
6659 on_each_cpu(ignore_task_cpu, tr, 1);
6661 ftrace_update_pid_func();
6662 ftrace_startup_all(0);
6663 out:
6664 mutex_unlock(&ftrace_lock);
6666 if (ret > 0)
6667 *ppos += ret;
6669 return ret;
6672 static int
6673 ftrace_pid_release(struct inode *inode, struct file *file)
6675 struct trace_array *tr = inode->i_private;
6677 trace_array_put(tr);
6679 return seq_release(inode, file);
6682 static const struct file_operations ftrace_pid_fops = {
6683 .open = ftrace_pid_open,
6684 .write = ftrace_pid_write,
6685 .read = seq_read,
6686 .llseek = tracing_lseek,
6687 .release = ftrace_pid_release,
6690 void ftrace_init_tracefs(struct trace_array *tr, struct dentry *d_tracer)
6692 trace_create_file("set_ftrace_pid", 0644, d_tracer,
6693 tr, &ftrace_pid_fops);
6696 void __init ftrace_init_tracefs_toplevel(struct trace_array *tr,
6697 struct dentry *d_tracer)
6699 /* Only the top level directory has the dyn_tracefs and profile */
6700 WARN_ON(!(tr->flags & TRACE_ARRAY_FL_GLOBAL));
6702 ftrace_init_dyn_tracefs(d_tracer);
6703 ftrace_profile_tracefs(d_tracer);
6707 * ftrace_kill - kill ftrace
6709 * This function should be used by panic code. It stops ftrace
6710 * but in a not so nice way. If you need to simply kill ftrace
6711 * from a non-atomic section, use ftrace_kill.
6713 void ftrace_kill(void)
6715 ftrace_disabled = 1;
6716 ftrace_enabled = 0;
6717 ftrace_trace_function = ftrace_stub;
6721 * Test if ftrace is dead or not.
6723 int ftrace_is_dead(void)
6725 return ftrace_disabled;
6729 * register_ftrace_function - register a function for profiling
6730 * @ops - ops structure that holds the function for profiling.
6732 * Register a function to be called by all functions in the
6733 * kernel.
6735 * Note: @ops->func and all the functions it calls must be labeled
6736 * with "notrace", otherwise it will go into a
6737 * recursive loop.
6739 int register_ftrace_function(struct ftrace_ops *ops)
6741 int ret = -1;
6743 ftrace_ops_init(ops);
6745 mutex_lock(&ftrace_lock);
6747 ret = ftrace_startup(ops, 0);
6749 mutex_unlock(&ftrace_lock);
6751 return ret;
6753 EXPORT_SYMBOL_GPL(register_ftrace_function);
6756 * unregister_ftrace_function - unregister a function for profiling.
6757 * @ops - ops structure that holds the function to unregister
6759 * Unregister a function that was added to be called by ftrace profiling.
6761 int unregister_ftrace_function(struct ftrace_ops *ops)
6763 int ret;
6765 mutex_lock(&ftrace_lock);
6766 ret = ftrace_shutdown(ops, 0);
6767 mutex_unlock(&ftrace_lock);
6769 return ret;
6771 EXPORT_SYMBOL_GPL(unregister_ftrace_function);
6774 ftrace_enable_sysctl(struct ctl_table *table, int write,
6775 void __user *buffer, size_t *lenp,
6776 loff_t *ppos)
6778 int ret = -ENODEV;
6780 mutex_lock(&ftrace_lock);
6782 if (unlikely(ftrace_disabled))
6783 goto out;
6785 ret = proc_dointvec(table, write, buffer, lenp, ppos);
6787 if (ret || !write || (last_ftrace_enabled == !!ftrace_enabled))
6788 goto out;
6790 last_ftrace_enabled = !!ftrace_enabled;
6792 if (ftrace_enabled) {
6794 /* we are starting ftrace again */
6795 if (rcu_dereference_protected(ftrace_ops_list,
6796 lockdep_is_held(&ftrace_lock)) != &ftrace_list_end)
6797 update_ftrace_function();
6799 ftrace_startup_sysctl();
6801 } else {
6802 /* stopping ftrace calls (just send to ftrace_stub) */
6803 ftrace_trace_function = ftrace_stub;
6805 ftrace_shutdown_sysctl();
6808 out:
6809 mutex_unlock(&ftrace_lock);
6810 return ret;
6813 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
6815 static struct ftrace_ops graph_ops = {
6816 .func = ftrace_stub,
6817 .flags = FTRACE_OPS_FL_RECURSION_SAFE |
6818 FTRACE_OPS_FL_INITIALIZED |
6819 FTRACE_OPS_FL_PID |
6820 FTRACE_OPS_FL_STUB,
6821 #ifdef FTRACE_GRAPH_TRAMP_ADDR
6822 .trampoline = FTRACE_GRAPH_TRAMP_ADDR,
6823 /* trampoline_size is only needed for dynamically allocated tramps */
6824 #endif
6825 ASSIGN_OPS_HASH(graph_ops, &global_ops.local_hash)
6828 void ftrace_graph_sleep_time_control(bool enable)
6830 fgraph_sleep_time = enable;
6833 void ftrace_graph_graph_time_control(bool enable)
6835 fgraph_graph_time = enable;
6838 int ftrace_graph_entry_stub(struct ftrace_graph_ent *trace)
6840 return 0;
6843 /* The callbacks that hook a function */
6844 trace_func_graph_ret_t ftrace_graph_return =
6845 (trace_func_graph_ret_t)ftrace_stub;
6846 trace_func_graph_ent_t ftrace_graph_entry = ftrace_graph_entry_stub;
6847 static trace_func_graph_ent_t __ftrace_graph_entry = ftrace_graph_entry_stub;
6849 /* Try to assign a return stack array on FTRACE_RETSTACK_ALLOC_SIZE tasks. */
6850 static int alloc_retstack_tasklist(struct ftrace_ret_stack **ret_stack_list)
6852 int i;
6853 int ret = 0;
6854 int start = 0, end = FTRACE_RETSTACK_ALLOC_SIZE;
6855 struct task_struct *g, *t;
6857 for (i = 0; i < FTRACE_RETSTACK_ALLOC_SIZE; i++) {
6858 ret_stack_list[i] =
6859 kmalloc_array(FTRACE_RETFUNC_DEPTH,
6860 sizeof(struct ftrace_ret_stack),
6861 GFP_KERNEL);
6862 if (!ret_stack_list[i]) {
6863 start = 0;
6864 end = i;
6865 ret = -ENOMEM;
6866 goto free;
6870 read_lock(&tasklist_lock);
6871 do_each_thread(g, t) {
6872 if (start == end) {
6873 ret = -EAGAIN;
6874 goto unlock;
6877 if (t->ret_stack == NULL) {
6878 atomic_set(&t->tracing_graph_pause, 0);
6879 atomic_set(&t->trace_overrun, 0);
6880 t->curr_ret_stack = -1;
6881 t->curr_ret_depth = -1;
6882 /* Make sure the tasks see the -1 first: */
6883 smp_wmb();
6884 t->ret_stack = ret_stack_list[start++];
6886 } while_each_thread(g, t);
6888 unlock:
6889 read_unlock(&tasklist_lock);
6890 free:
6891 for (i = start; i < end; i++)
6892 kfree(ret_stack_list[i]);
6893 return ret;
6896 static void
6897 ftrace_graph_probe_sched_switch(void *ignore, bool preempt,
6898 struct task_struct *prev, struct task_struct *next)
6900 unsigned long long timestamp;
6901 int index;
6904 * Does the user want to count the time a function was asleep.
6905 * If so, do not update the time stamps.
6907 if (fgraph_sleep_time)
6908 return;
6910 timestamp = trace_clock_local();
6912 prev->ftrace_timestamp = timestamp;
6914 /* only process tasks that we timestamped */
6915 if (!next->ftrace_timestamp)
6916 return;
6919 * Update all the counters in next to make up for the
6920 * time next was sleeping.
6922 timestamp -= next->ftrace_timestamp;
6924 for (index = next->curr_ret_stack; index >= 0; index--)
6925 next->ret_stack[index].calltime += timestamp;
6928 /* Allocate a return stack for each task */
6929 static int start_graph_tracing(void)
6931 struct ftrace_ret_stack **ret_stack_list;
6932 int ret, cpu;
6934 ret_stack_list = kmalloc_array(FTRACE_RETSTACK_ALLOC_SIZE,
6935 sizeof(struct ftrace_ret_stack *),
6936 GFP_KERNEL);
6938 if (!ret_stack_list)
6939 return -ENOMEM;
6941 /* The cpu_boot init_task->ret_stack will never be freed */
6942 for_each_online_cpu(cpu) {
6943 if (!idle_task(cpu)->ret_stack)
6944 ftrace_graph_init_idle_task(idle_task(cpu), cpu);
6947 do {
6948 ret = alloc_retstack_tasklist(ret_stack_list);
6949 } while (ret == -EAGAIN);
6951 if (!ret) {
6952 ret = register_trace_sched_switch(ftrace_graph_probe_sched_switch, NULL);
6953 if (ret)
6954 pr_info("ftrace_graph: Couldn't activate tracepoint"
6955 " probe to kernel_sched_switch\n");
6958 kfree(ret_stack_list);
6959 return ret;
6963 * Hibernation protection.
6964 * The state of the current task is too much unstable during
6965 * suspend/restore to disk. We want to protect against that.
6967 static int
6968 ftrace_suspend_notifier_call(struct notifier_block *bl, unsigned long state,
6969 void *unused)
6971 switch (state) {
6972 case PM_HIBERNATION_PREPARE:
6973 pause_graph_tracing();
6974 break;
6976 case PM_POST_HIBERNATION:
6977 unpause_graph_tracing();
6978 break;
6980 return NOTIFY_DONE;
6983 static int ftrace_graph_entry_test(struct ftrace_graph_ent *trace)
6985 if (!ftrace_ops_test(&global_ops, trace->func, NULL))
6986 return 0;
6987 return __ftrace_graph_entry(trace);
6991 * The function graph tracer should only trace the functions defined
6992 * by set_ftrace_filter and set_ftrace_notrace. If another function
6993 * tracer ops is registered, the graph tracer requires testing the
6994 * function against the global ops, and not just trace any function
6995 * that any ftrace_ops registered.
6997 static void update_function_graph_func(void)
6999 struct ftrace_ops *op;
7000 bool do_test = false;
7003 * The graph and global ops share the same set of functions
7004 * to test. If any other ops is on the list, then
7005 * the graph tracing needs to test if its the function
7006 * it should call.
7008 do_for_each_ftrace_op(op, ftrace_ops_list) {
7009 if (op != &global_ops && op != &graph_ops &&
7010 op != &ftrace_list_end) {
7011 do_test = true;
7012 /* in double loop, break out with goto */
7013 goto out;
7015 } while_for_each_ftrace_op(op);
7016 out:
7017 if (do_test)
7018 ftrace_graph_entry = ftrace_graph_entry_test;
7019 else
7020 ftrace_graph_entry = __ftrace_graph_entry;
7023 static struct notifier_block ftrace_suspend_notifier = {
7024 .notifier_call = ftrace_suspend_notifier_call,
7027 int register_ftrace_graph(trace_func_graph_ret_t retfunc,
7028 trace_func_graph_ent_t entryfunc)
7030 int ret = 0;
7032 mutex_lock(&ftrace_lock);
7034 /* we currently allow only one tracer registered at a time */
7035 if (ftrace_graph_active) {
7036 ret = -EBUSY;
7037 goto out;
7040 register_pm_notifier(&ftrace_suspend_notifier);
7042 ftrace_graph_active++;
7043 ret = start_graph_tracing();
7044 if (ret) {
7045 ftrace_graph_active--;
7046 goto out;
7049 ftrace_graph_return = retfunc;
7052 * Update the indirect function to the entryfunc, and the
7053 * function that gets called to the entry_test first. Then
7054 * call the update fgraph entry function to determine if
7055 * the entryfunc should be called directly or not.
7057 __ftrace_graph_entry = entryfunc;
7058 ftrace_graph_entry = ftrace_graph_entry_test;
7059 update_function_graph_func();
7061 ret = ftrace_startup(&graph_ops, FTRACE_START_FUNC_RET);
7062 out:
7063 mutex_unlock(&ftrace_lock);
7064 return ret;
7067 void unregister_ftrace_graph(void)
7069 mutex_lock(&ftrace_lock);
7071 if (unlikely(!ftrace_graph_active))
7072 goto out;
7074 ftrace_graph_active--;
7075 ftrace_graph_return = (trace_func_graph_ret_t)ftrace_stub;
7076 ftrace_graph_entry = ftrace_graph_entry_stub;
7077 __ftrace_graph_entry = ftrace_graph_entry_stub;
7078 ftrace_shutdown(&graph_ops, FTRACE_STOP_FUNC_RET);
7079 unregister_pm_notifier(&ftrace_suspend_notifier);
7080 unregister_trace_sched_switch(ftrace_graph_probe_sched_switch, NULL);
7082 out:
7083 mutex_unlock(&ftrace_lock);
7086 static DEFINE_PER_CPU(struct ftrace_ret_stack *, idle_ret_stack);
7088 static void
7089 graph_init_task(struct task_struct *t, struct ftrace_ret_stack *ret_stack)
7091 atomic_set(&t->tracing_graph_pause, 0);
7092 atomic_set(&t->trace_overrun, 0);
7093 t->ftrace_timestamp = 0;
7094 /* make curr_ret_stack visible before we add the ret_stack */
7095 smp_wmb();
7096 t->ret_stack = ret_stack;
7100 * Allocate a return stack for the idle task. May be the first
7101 * time through, or it may be done by CPU hotplug online.
7103 void ftrace_graph_init_idle_task(struct task_struct *t, int cpu)
7105 t->curr_ret_stack = -1;
7106 t->curr_ret_depth = -1;
7108 * The idle task has no parent, it either has its own
7109 * stack or no stack at all.
7111 if (t->ret_stack)
7112 WARN_ON(t->ret_stack != per_cpu(idle_ret_stack, cpu));
7114 if (ftrace_graph_active) {
7115 struct ftrace_ret_stack *ret_stack;
7117 ret_stack = per_cpu(idle_ret_stack, cpu);
7118 if (!ret_stack) {
7119 ret_stack =
7120 kmalloc_array(FTRACE_RETFUNC_DEPTH,
7121 sizeof(struct ftrace_ret_stack),
7122 GFP_KERNEL);
7123 if (!ret_stack)
7124 return;
7125 per_cpu(idle_ret_stack, cpu) = ret_stack;
7127 graph_init_task(t, ret_stack);
7131 /* Allocate a return stack for newly created task */
7132 void ftrace_graph_init_task(struct task_struct *t)
7134 /* Make sure we do not use the parent ret_stack */
7135 t->ret_stack = NULL;
7136 t->curr_ret_stack = -1;
7137 t->curr_ret_depth = -1;
7139 if (ftrace_graph_active) {
7140 struct ftrace_ret_stack *ret_stack;
7142 ret_stack = kmalloc_array(FTRACE_RETFUNC_DEPTH,
7143 sizeof(struct ftrace_ret_stack),
7144 GFP_KERNEL);
7145 if (!ret_stack)
7146 return;
7147 graph_init_task(t, ret_stack);
7151 void ftrace_graph_exit_task(struct task_struct *t)
7153 struct ftrace_ret_stack *ret_stack = t->ret_stack;
7155 t->ret_stack = NULL;
7156 /* NULL must become visible to IRQs before we free it: */
7157 barrier();
7159 kfree(ret_stack);
7161 #endif