2 * Infrastructure for profiling code inserted by 'gcc -pg'.
4 * Copyright (C) 2007-2008 Steven Rostedt <srostedt@redhat.com>
5 * Copyright (C) 2004-2008 Ingo Molnar <mingo@redhat.com>
7 * Originally ported from the -rt patch by:
8 * Copyright (C) 2007 Arnaldo Carvalho de Melo <acme@redhat.com>
10 * Based on code in the latency_tracer, that is:
12 * Copyright (C) 2004-2006 Ingo Molnar
13 * Copyright (C) 2004 Nadia Yvette Chambers
16 #include <linux/stop_machine.h>
17 #include <linux/clocksource.h>
18 #include <linux/sched/task.h>
19 #include <linux/kallsyms.h>
20 #include <linux/seq_file.h>
21 #include <linux/suspend.h>
22 #include <linux/tracefs.h>
23 #include <linux/hardirq.h>
24 #include <linux/kthread.h>
25 #include <linux/uaccess.h>
26 #include <linux/bsearch.h>
27 #include <linux/module.h>
28 #include <linux/ftrace.h>
29 #include <linux/sysctl.h>
30 #include <linux/slab.h>
31 #include <linux/ctype.h>
32 #include <linux/sort.h>
33 #include <linux/list.h>
34 #include <linux/hash.h>
35 #include <linux/rcupdate.h>
36 #include <linux/kprobes.h>
38 #include <trace/events/sched.h>
40 #include <asm/sections.h>
41 #include <asm/setup.h>
43 #include "trace_output.h"
44 #include "trace_stat.h"
46 #define FTRACE_WARN_ON(cond) \
54 #define FTRACE_WARN_ON_ONCE(cond) \
57 if (WARN_ON_ONCE(___r)) \
62 /* hash bits for specific function selection */
63 #define FTRACE_HASH_BITS 7
64 #define FTRACE_FUNC_HASHSIZE (1 << FTRACE_HASH_BITS)
65 #define FTRACE_HASH_DEFAULT_BITS 10
66 #define FTRACE_HASH_MAX_BITS 12
68 #ifdef CONFIG_DYNAMIC_FTRACE
69 #define INIT_OPS_HASH(opsname) \
70 .func_hash = &opsname.local_hash, \
71 .local_hash.regex_lock = __MUTEX_INITIALIZER(opsname.local_hash.regex_lock),
72 #define ASSIGN_OPS_HASH(opsname, val) \
74 .local_hash.regex_lock = __MUTEX_INITIALIZER(opsname.local_hash.regex_lock),
76 #define INIT_OPS_HASH(opsname)
77 #define ASSIGN_OPS_HASH(opsname, val)
80 static struct ftrace_ops ftrace_list_end __read_mostly
= {
82 .flags
= FTRACE_OPS_FL_RECURSION_SAFE
| FTRACE_OPS_FL_STUB
,
83 INIT_OPS_HASH(ftrace_list_end
)
86 /* ftrace_enabled is a method to turn ftrace on or off */
87 int ftrace_enabled __read_mostly
;
88 static int last_ftrace_enabled
;
90 /* Current function tracing op */
91 struct ftrace_ops
*function_trace_op __read_mostly
= &ftrace_list_end
;
92 /* What to set function_trace_op to */
93 static struct ftrace_ops
*set_function_trace_op
;
95 static bool ftrace_pids_enabled(struct ftrace_ops
*ops
)
97 struct trace_array
*tr
;
99 if (!(ops
->flags
& FTRACE_OPS_FL_PID
) || !ops
->private)
104 return tr
->function_pids
!= NULL
;
107 static void ftrace_update_trampoline(struct ftrace_ops
*ops
);
110 * ftrace_disabled is set when an anomaly is discovered.
111 * ftrace_disabled is much stronger than ftrace_enabled.
113 static int ftrace_disabled __read_mostly
;
115 static DEFINE_MUTEX(ftrace_lock
);
117 static struct ftrace_ops __rcu
*ftrace_ops_list __read_mostly
= &ftrace_list_end
;
118 ftrace_func_t ftrace_trace_function __read_mostly
= ftrace_stub
;
119 static struct ftrace_ops global_ops
;
121 #if ARCH_SUPPORTS_FTRACE_OPS
122 static void ftrace_ops_list_func(unsigned long ip
, unsigned long parent_ip
,
123 struct ftrace_ops
*op
, struct pt_regs
*regs
);
125 /* See comment below, where ftrace_ops_list_func is defined */
126 static void ftrace_ops_no_ops(unsigned long ip
, unsigned long parent_ip
);
127 #define ftrace_ops_list_func ((ftrace_func_t)ftrace_ops_no_ops)
131 * Traverse the ftrace_global_list, invoking all entries. The reason that we
132 * can use rcu_dereference_raw_notrace() is that elements removed from this list
133 * are simply leaked, so there is no need to interact with a grace-period
134 * mechanism. The rcu_dereference_raw_notrace() calls are needed to handle
135 * concurrent insertions into the ftrace_global_list.
137 * Silly Alpha and silly pointer-speculation compiler optimizations!
139 #define do_for_each_ftrace_op(op, list) \
140 op = rcu_dereference_raw_notrace(list); \
144 * Optimized for just a single item in the list (as that is the normal case).
146 #define while_for_each_ftrace_op(op) \
147 while (likely(op = rcu_dereference_raw_notrace((op)->next)) && \
148 unlikely((op) != &ftrace_list_end))
150 static inline void ftrace_ops_init(struct ftrace_ops
*ops
)
152 #ifdef CONFIG_DYNAMIC_FTRACE
153 if (!(ops
->flags
& FTRACE_OPS_FL_INITIALIZED
)) {
154 mutex_init(&ops
->local_hash
.regex_lock
);
155 ops
->func_hash
= &ops
->local_hash
;
156 ops
->flags
|= FTRACE_OPS_FL_INITIALIZED
;
162 * ftrace_nr_registered_ops - return number of ops registered
164 * Returns the number of ftrace_ops registered and tracing functions
166 int ftrace_nr_registered_ops(void)
168 struct ftrace_ops
*ops
;
171 mutex_lock(&ftrace_lock
);
173 for (ops
= rcu_dereference_protected(ftrace_ops_list
,
174 lockdep_is_held(&ftrace_lock
));
175 ops
!= &ftrace_list_end
;
176 ops
= rcu_dereference_protected(ops
->next
,
177 lockdep_is_held(&ftrace_lock
)))
180 mutex_unlock(&ftrace_lock
);
185 static void ftrace_pid_func(unsigned long ip
, unsigned long parent_ip
,
186 struct ftrace_ops
*op
, struct pt_regs
*regs
)
188 struct trace_array
*tr
= op
->private;
190 if (tr
&& this_cpu_read(tr
->trace_buffer
.data
->ftrace_ignore_pid
))
193 op
->saved_func(ip
, parent_ip
, op
, regs
);
197 * clear_ftrace_function - reset the ftrace function
199 * This NULLs the ftrace function and in essence stops
200 * tracing. There may be lag
202 void clear_ftrace_function(void)
204 ftrace_trace_function
= ftrace_stub
;
207 static void ftrace_sync(struct work_struct
*work
)
210 * This function is just a stub to implement a hard force
211 * of synchronize_sched(). This requires synchronizing
212 * tasks even in userspace and idle.
214 * Yes, function tracing is rude.
218 static void ftrace_sync_ipi(void *data
)
220 /* Probably not needed, but do it anyway */
224 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
225 static void update_function_graph_func(void);
227 /* Both enabled by default (can be cleared by function_graph tracer flags */
228 static bool fgraph_sleep_time
= true;
229 static bool fgraph_graph_time
= true;
232 static inline void update_function_graph_func(void) { }
236 static ftrace_func_t
ftrace_ops_get_list_func(struct ftrace_ops
*ops
)
239 * If this is a dynamic, RCU, or per CPU ops, or we force list func,
240 * then it needs to call the list anyway.
242 if (ops
->flags
& (FTRACE_OPS_FL_DYNAMIC
| FTRACE_OPS_FL_RCU
) ||
243 FTRACE_FORCE_LIST_FUNC
)
244 return ftrace_ops_list_func
;
246 return ftrace_ops_get_func(ops
);
249 static void update_ftrace_function(void)
254 * Prepare the ftrace_ops that the arch callback will use.
255 * If there's only one ftrace_ops registered, the ftrace_ops_list
256 * will point to the ops we want.
258 set_function_trace_op
= rcu_dereference_protected(ftrace_ops_list
,
259 lockdep_is_held(&ftrace_lock
));
261 /* If there's no ftrace_ops registered, just call the stub function */
262 if (set_function_trace_op
== &ftrace_list_end
) {
266 * If we are at the end of the list and this ops is
267 * recursion safe and not dynamic and the arch supports passing ops,
268 * then have the mcount trampoline call the function directly.
270 } else if (rcu_dereference_protected(ftrace_ops_list
->next
,
271 lockdep_is_held(&ftrace_lock
)) == &ftrace_list_end
) {
272 func
= ftrace_ops_get_list_func(ftrace_ops_list
);
275 /* Just use the default ftrace_ops */
276 set_function_trace_op
= &ftrace_list_end
;
277 func
= ftrace_ops_list_func
;
280 update_function_graph_func();
282 /* If there's no change, then do nothing more here */
283 if (ftrace_trace_function
== func
)
287 * If we are using the list function, it doesn't care
288 * about the function_trace_ops.
290 if (func
== ftrace_ops_list_func
) {
291 ftrace_trace_function
= func
;
293 * Don't even bother setting function_trace_ops,
294 * it would be racy to do so anyway.
299 #ifndef CONFIG_DYNAMIC_FTRACE
301 * For static tracing, we need to be a bit more careful.
302 * The function change takes affect immediately. Thus,
303 * we need to coorditate the setting of the function_trace_ops
304 * with the setting of the ftrace_trace_function.
306 * Set the function to the list ops, which will call the
307 * function we want, albeit indirectly, but it handles the
308 * ftrace_ops and doesn't depend on function_trace_op.
310 ftrace_trace_function
= ftrace_ops_list_func
;
312 * Make sure all CPUs see this. Yes this is slow, but static
313 * tracing is slow and nasty to have enabled.
315 schedule_on_each_cpu(ftrace_sync
);
316 /* Now all cpus are using the list ops. */
317 function_trace_op
= set_function_trace_op
;
318 /* Make sure the function_trace_op is visible on all CPUs */
320 /* Nasty way to force a rmb on all cpus */
321 smp_call_function(ftrace_sync_ipi
, NULL
, 1);
322 /* OK, we are all set to update the ftrace_trace_function now! */
323 #endif /* !CONFIG_DYNAMIC_FTRACE */
325 ftrace_trace_function
= func
;
328 int using_ftrace_ops_list_func(void)
330 return ftrace_trace_function
== ftrace_ops_list_func
;
333 static void add_ftrace_ops(struct ftrace_ops __rcu
**list
,
334 struct ftrace_ops
*ops
)
336 rcu_assign_pointer(ops
->next
, *list
);
339 * We are entering ops into the list but another
340 * CPU might be walking that list. We need to make sure
341 * the ops->next pointer is valid before another CPU sees
342 * the ops pointer included into the list.
344 rcu_assign_pointer(*list
, ops
);
347 static int remove_ftrace_ops(struct ftrace_ops __rcu
**list
,
348 struct ftrace_ops
*ops
)
350 struct ftrace_ops
**p
;
353 * If we are removing the last function, then simply point
354 * to the ftrace_stub.
356 if (rcu_dereference_protected(*list
,
357 lockdep_is_held(&ftrace_lock
)) == ops
&&
358 rcu_dereference_protected(ops
->next
,
359 lockdep_is_held(&ftrace_lock
)) == &ftrace_list_end
) {
360 *list
= &ftrace_list_end
;
364 for (p
= list
; *p
!= &ftrace_list_end
; p
= &(*p
)->next
)
375 static void ftrace_update_trampoline(struct ftrace_ops
*ops
);
377 static int __register_ftrace_function(struct ftrace_ops
*ops
)
379 if (ops
->flags
& FTRACE_OPS_FL_DELETED
)
382 if (WARN_ON(ops
->flags
& FTRACE_OPS_FL_ENABLED
))
385 #ifndef CONFIG_DYNAMIC_FTRACE_WITH_REGS
387 * If the ftrace_ops specifies SAVE_REGS, then it only can be used
388 * if the arch supports it, or SAVE_REGS_IF_SUPPORTED is also set.
389 * Setting SAVE_REGS_IF_SUPPORTED makes SAVE_REGS irrelevant.
391 if (ops
->flags
& FTRACE_OPS_FL_SAVE_REGS
&&
392 !(ops
->flags
& FTRACE_OPS_FL_SAVE_REGS_IF_SUPPORTED
))
395 if (ops
->flags
& FTRACE_OPS_FL_SAVE_REGS_IF_SUPPORTED
)
396 ops
->flags
|= FTRACE_OPS_FL_SAVE_REGS
;
399 if (!core_kernel_data((unsigned long)ops
))
400 ops
->flags
|= FTRACE_OPS_FL_DYNAMIC
;
402 add_ftrace_ops(&ftrace_ops_list
, ops
);
404 /* Always save the function, and reset at unregistering */
405 ops
->saved_func
= ops
->func
;
407 if (ftrace_pids_enabled(ops
))
408 ops
->func
= ftrace_pid_func
;
410 ftrace_update_trampoline(ops
);
413 update_ftrace_function();
418 static int __unregister_ftrace_function(struct ftrace_ops
*ops
)
422 if (WARN_ON(!(ops
->flags
& FTRACE_OPS_FL_ENABLED
)))
425 ret
= remove_ftrace_ops(&ftrace_ops_list
, ops
);
431 update_ftrace_function();
433 ops
->func
= ops
->saved_func
;
438 static void ftrace_update_pid_func(void)
440 struct ftrace_ops
*op
;
442 /* Only do something if we are tracing something */
443 if (ftrace_trace_function
== ftrace_stub
)
446 do_for_each_ftrace_op(op
, ftrace_ops_list
) {
447 if (op
->flags
& FTRACE_OPS_FL_PID
) {
448 op
->func
= ftrace_pids_enabled(op
) ?
449 ftrace_pid_func
: op
->saved_func
;
450 ftrace_update_trampoline(op
);
452 } while_for_each_ftrace_op(op
);
454 update_ftrace_function();
457 #ifdef CONFIG_FUNCTION_PROFILER
458 struct ftrace_profile
{
459 struct hlist_node node
;
461 unsigned long counter
;
462 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
463 unsigned long long time
;
464 unsigned long long time_squared
;
468 struct ftrace_profile_page
{
469 struct ftrace_profile_page
*next
;
471 struct ftrace_profile records
[];
474 struct ftrace_profile_stat
{
476 struct hlist_head
*hash
;
477 struct ftrace_profile_page
*pages
;
478 struct ftrace_profile_page
*start
;
479 struct tracer_stat stat
;
482 #define PROFILE_RECORDS_SIZE \
483 (PAGE_SIZE - offsetof(struct ftrace_profile_page, records))
485 #define PROFILES_PER_PAGE \
486 (PROFILE_RECORDS_SIZE / sizeof(struct ftrace_profile))
488 static int ftrace_profile_enabled __read_mostly
;
490 /* ftrace_profile_lock - synchronize the enable and disable of the profiler */
491 static DEFINE_MUTEX(ftrace_profile_lock
);
493 static DEFINE_PER_CPU(struct ftrace_profile_stat
, ftrace_profile_stats
);
495 #define FTRACE_PROFILE_HASH_BITS 10
496 #define FTRACE_PROFILE_HASH_SIZE (1 << FTRACE_PROFILE_HASH_BITS)
499 function_stat_next(void *v
, int idx
)
501 struct ftrace_profile
*rec
= v
;
502 struct ftrace_profile_page
*pg
;
504 pg
= (struct ftrace_profile_page
*)((unsigned long)rec
& PAGE_MASK
);
510 if ((void *)rec
>= (void *)&pg
->records
[pg
->index
]) {
514 rec
= &pg
->records
[0];
522 static void *function_stat_start(struct tracer_stat
*trace
)
524 struct ftrace_profile_stat
*stat
=
525 container_of(trace
, struct ftrace_profile_stat
, stat
);
527 if (!stat
|| !stat
->start
)
530 return function_stat_next(&stat
->start
->records
[0], 0);
533 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
534 /* function graph compares on total time */
535 static int function_stat_cmp(void *p1
, void *p2
)
537 struct ftrace_profile
*a
= p1
;
538 struct ftrace_profile
*b
= p2
;
540 if (a
->time
< b
->time
)
542 if (a
->time
> b
->time
)
548 /* not function graph compares against hits */
549 static int function_stat_cmp(void *p1
, void *p2
)
551 struct ftrace_profile
*a
= p1
;
552 struct ftrace_profile
*b
= p2
;
554 if (a
->counter
< b
->counter
)
556 if (a
->counter
> b
->counter
)
563 static int function_stat_headers(struct seq_file
*m
)
565 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
566 seq_puts(m
, " Function "
569 "--- ---- --- ---\n");
571 seq_puts(m
, " Function Hit\n"
577 static int function_stat_show(struct seq_file
*m
, void *v
)
579 struct ftrace_profile
*rec
= v
;
580 char str
[KSYM_SYMBOL_LEN
];
582 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
583 static struct trace_seq s
;
584 unsigned long long avg
;
585 unsigned long long stddev
;
587 mutex_lock(&ftrace_profile_lock
);
589 /* we raced with function_profile_reset() */
590 if (unlikely(rec
->counter
== 0)) {
595 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
597 do_div(avg
, rec
->counter
);
598 if (tracing_thresh
&& (avg
< tracing_thresh
))
602 kallsyms_lookup(rec
->ip
, NULL
, NULL
, NULL
, str
);
603 seq_printf(m
, " %-30.30s %10lu", str
, rec
->counter
);
605 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
608 /* Sample standard deviation (s^2) */
609 if (rec
->counter
<= 1)
613 * Apply Welford's method:
614 * s^2 = 1 / (n * (n-1)) * (n * \Sum (x_i)^2 - (\Sum x_i)^2)
616 stddev
= rec
->counter
* rec
->time_squared
-
617 rec
->time
* rec
->time
;
620 * Divide only 1000 for ns^2 -> us^2 conversion.
621 * trace_print_graph_duration will divide 1000 again.
623 do_div(stddev
, rec
->counter
* (rec
->counter
- 1) * 1000);
627 trace_print_graph_duration(rec
->time
, &s
);
628 trace_seq_puts(&s
, " ");
629 trace_print_graph_duration(avg
, &s
);
630 trace_seq_puts(&s
, " ");
631 trace_print_graph_duration(stddev
, &s
);
632 trace_print_seq(m
, &s
);
636 mutex_unlock(&ftrace_profile_lock
);
641 static void ftrace_profile_reset(struct ftrace_profile_stat
*stat
)
643 struct ftrace_profile_page
*pg
;
645 pg
= stat
->pages
= stat
->start
;
648 memset(pg
->records
, 0, PROFILE_RECORDS_SIZE
);
653 memset(stat
->hash
, 0,
654 FTRACE_PROFILE_HASH_SIZE
* sizeof(struct hlist_head
));
657 int ftrace_profile_pages_init(struct ftrace_profile_stat
*stat
)
659 struct ftrace_profile_page
*pg
;
664 /* If we already allocated, do nothing */
668 stat
->pages
= (void *)get_zeroed_page(GFP_KERNEL
);
672 #ifdef CONFIG_DYNAMIC_FTRACE
673 functions
= ftrace_update_tot_cnt
;
676 * We do not know the number of functions that exist because
677 * dynamic tracing is what counts them. With past experience
678 * we have around 20K functions. That should be more than enough.
679 * It is highly unlikely we will execute every function in
685 pg
= stat
->start
= stat
->pages
;
687 pages
= DIV_ROUND_UP(functions
, PROFILES_PER_PAGE
);
689 for (i
= 1; i
< pages
; i
++) {
690 pg
->next
= (void *)get_zeroed_page(GFP_KERNEL
);
701 unsigned long tmp
= (unsigned long)pg
;
713 static int ftrace_profile_init_cpu(int cpu
)
715 struct ftrace_profile_stat
*stat
;
718 stat
= &per_cpu(ftrace_profile_stats
, cpu
);
721 /* If the profile is already created, simply reset it */
722 ftrace_profile_reset(stat
);
727 * We are profiling all functions, but usually only a few thousand
728 * functions are hit. We'll make a hash of 1024 items.
730 size
= FTRACE_PROFILE_HASH_SIZE
;
732 stat
->hash
= kzalloc(sizeof(struct hlist_head
) * size
, GFP_KERNEL
);
737 /* Preallocate the function profiling pages */
738 if (ftrace_profile_pages_init(stat
) < 0) {
747 static int ftrace_profile_init(void)
752 for_each_possible_cpu(cpu
) {
753 ret
= ftrace_profile_init_cpu(cpu
);
761 /* interrupts must be disabled */
762 static struct ftrace_profile
*
763 ftrace_find_profiled_func(struct ftrace_profile_stat
*stat
, unsigned long ip
)
765 struct ftrace_profile
*rec
;
766 struct hlist_head
*hhd
;
769 key
= hash_long(ip
, FTRACE_PROFILE_HASH_BITS
);
770 hhd
= &stat
->hash
[key
];
772 if (hlist_empty(hhd
))
775 hlist_for_each_entry_rcu_notrace(rec
, hhd
, node
) {
783 static void ftrace_add_profile(struct ftrace_profile_stat
*stat
,
784 struct ftrace_profile
*rec
)
788 key
= hash_long(rec
->ip
, FTRACE_PROFILE_HASH_BITS
);
789 hlist_add_head_rcu(&rec
->node
, &stat
->hash
[key
]);
793 * The memory is already allocated, this simply finds a new record to use.
795 static struct ftrace_profile
*
796 ftrace_profile_alloc(struct ftrace_profile_stat
*stat
, unsigned long ip
)
798 struct ftrace_profile
*rec
= NULL
;
800 /* prevent recursion (from NMIs) */
801 if (atomic_inc_return(&stat
->disabled
) != 1)
805 * Try to find the function again since an NMI
806 * could have added it
808 rec
= ftrace_find_profiled_func(stat
, ip
);
812 if (stat
->pages
->index
== PROFILES_PER_PAGE
) {
813 if (!stat
->pages
->next
)
815 stat
->pages
= stat
->pages
->next
;
818 rec
= &stat
->pages
->records
[stat
->pages
->index
++];
820 ftrace_add_profile(stat
, rec
);
823 atomic_dec(&stat
->disabled
);
829 function_profile_call(unsigned long ip
, unsigned long parent_ip
,
830 struct ftrace_ops
*ops
, struct pt_regs
*regs
)
832 struct ftrace_profile_stat
*stat
;
833 struct ftrace_profile
*rec
;
836 if (!ftrace_profile_enabled
)
839 local_irq_save(flags
);
841 stat
= this_cpu_ptr(&ftrace_profile_stats
);
842 if (!stat
->hash
|| !ftrace_profile_enabled
)
845 rec
= ftrace_find_profiled_func(stat
, ip
);
847 rec
= ftrace_profile_alloc(stat
, ip
);
854 local_irq_restore(flags
);
857 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
858 static int profile_graph_entry(struct ftrace_graph_ent
*trace
)
860 int index
= current
->curr_ret_stack
;
862 function_profile_call(trace
->func
, 0, NULL
, NULL
);
864 /* If function graph is shutting down, ret_stack can be NULL */
865 if (!current
->ret_stack
)
868 if (index
>= 0 && index
< FTRACE_RETFUNC_DEPTH
)
869 current
->ret_stack
[index
].subtime
= 0;
874 static void profile_graph_return(struct ftrace_graph_ret
*trace
)
876 struct ftrace_profile_stat
*stat
;
877 unsigned long long calltime
;
878 struct ftrace_profile
*rec
;
881 local_irq_save(flags
);
882 stat
= this_cpu_ptr(&ftrace_profile_stats
);
883 if (!stat
->hash
|| !ftrace_profile_enabled
)
886 /* If the calltime was zero'd ignore it */
887 if (!trace
->calltime
)
890 calltime
= trace
->rettime
- trace
->calltime
;
892 if (!fgraph_graph_time
) {
895 index
= current
->curr_ret_stack
;
897 /* Append this call time to the parent time to subtract */
899 current
->ret_stack
[index
- 1].subtime
+= calltime
;
901 if (current
->ret_stack
[index
].subtime
< calltime
)
902 calltime
-= current
->ret_stack
[index
].subtime
;
907 rec
= ftrace_find_profiled_func(stat
, trace
->func
);
909 rec
->time
+= calltime
;
910 rec
->time_squared
+= calltime
* calltime
;
914 local_irq_restore(flags
);
917 static int register_ftrace_profiler(void)
919 return register_ftrace_graph(&profile_graph_return
,
920 &profile_graph_entry
);
923 static void unregister_ftrace_profiler(void)
925 unregister_ftrace_graph();
928 static struct ftrace_ops ftrace_profile_ops __read_mostly
= {
929 .func
= function_profile_call
,
930 .flags
= FTRACE_OPS_FL_RECURSION_SAFE
| FTRACE_OPS_FL_INITIALIZED
,
931 INIT_OPS_HASH(ftrace_profile_ops
)
934 static int register_ftrace_profiler(void)
936 return register_ftrace_function(&ftrace_profile_ops
);
939 static void unregister_ftrace_profiler(void)
941 unregister_ftrace_function(&ftrace_profile_ops
);
943 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
946 ftrace_profile_write(struct file
*filp
, const char __user
*ubuf
,
947 size_t cnt
, loff_t
*ppos
)
952 ret
= kstrtoul_from_user(ubuf
, cnt
, 10, &val
);
958 mutex_lock(&ftrace_profile_lock
);
959 if (ftrace_profile_enabled
^ val
) {
961 ret
= ftrace_profile_init();
967 ret
= register_ftrace_profiler();
972 ftrace_profile_enabled
= 1;
974 ftrace_profile_enabled
= 0;
976 * unregister_ftrace_profiler calls stop_machine
977 * so this acts like an synchronize_sched.
979 unregister_ftrace_profiler();
983 mutex_unlock(&ftrace_profile_lock
);
991 ftrace_profile_read(struct file
*filp
, char __user
*ubuf
,
992 size_t cnt
, loff_t
*ppos
)
994 char buf
[64]; /* big enough to hold a number */
997 r
= sprintf(buf
, "%u\n", ftrace_profile_enabled
);
998 return simple_read_from_buffer(ubuf
, cnt
, ppos
, buf
, r
);
1001 static const struct file_operations ftrace_profile_fops
= {
1002 .open
= tracing_open_generic
,
1003 .read
= ftrace_profile_read
,
1004 .write
= ftrace_profile_write
,
1005 .llseek
= default_llseek
,
1008 /* used to initialize the real stat files */
1009 static struct tracer_stat function_stats __initdata
= {
1010 .name
= "functions",
1011 .stat_start
= function_stat_start
,
1012 .stat_next
= function_stat_next
,
1013 .stat_cmp
= function_stat_cmp
,
1014 .stat_headers
= function_stat_headers
,
1015 .stat_show
= function_stat_show
1018 static __init
void ftrace_profile_tracefs(struct dentry
*d_tracer
)
1020 struct ftrace_profile_stat
*stat
;
1021 struct dentry
*entry
;
1026 for_each_possible_cpu(cpu
) {
1027 stat
= &per_cpu(ftrace_profile_stats
, cpu
);
1029 name
= kasprintf(GFP_KERNEL
, "function%d", cpu
);
1032 * The files created are permanent, if something happens
1033 * we still do not free memory.
1036 "Could not allocate stat file for cpu %d\n",
1040 stat
->stat
= function_stats
;
1041 stat
->stat
.name
= name
;
1042 ret
= register_stat_tracer(&stat
->stat
);
1045 "Could not register function stat for cpu %d\n",
1052 entry
= tracefs_create_file("function_profile_enabled", 0644,
1053 d_tracer
, NULL
, &ftrace_profile_fops
);
1055 pr_warn("Could not create tracefs 'function_profile_enabled' entry\n");
1058 #else /* CONFIG_FUNCTION_PROFILER */
1059 static __init
void ftrace_profile_tracefs(struct dentry
*d_tracer
)
1062 #endif /* CONFIG_FUNCTION_PROFILER */
1064 static struct pid
* const ftrace_swapper_pid
= &init_struct_pid
;
1066 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
1067 static int ftrace_graph_active
;
1069 # define ftrace_graph_active 0
1072 #ifdef CONFIG_DYNAMIC_FTRACE
1074 static struct ftrace_ops
*removed_ops
;
1077 * Set when doing a global update, like enabling all recs or disabling them.
1078 * It is not set when just updating a single ftrace_ops.
1080 static bool update_all_ops
;
1082 #ifndef CONFIG_FTRACE_MCOUNT_RECORD
1083 # error Dynamic ftrace depends on MCOUNT_RECORD
1086 struct ftrace_func_entry
{
1087 struct hlist_node hlist
;
1091 struct ftrace_func_probe
{
1092 struct ftrace_probe_ops
*probe_ops
;
1093 struct ftrace_ops ops
;
1094 struct trace_array
*tr
;
1095 struct list_head list
;
1101 * We make these constant because no one should touch them,
1102 * but they are used as the default "empty hash", to avoid allocating
1103 * it all the time. These are in a read only section such that if
1104 * anyone does try to modify it, it will cause an exception.
1106 static const struct hlist_head empty_buckets
[1];
1107 static const struct ftrace_hash empty_hash
= {
1108 .buckets
= (struct hlist_head
*)empty_buckets
,
1110 #define EMPTY_HASH ((struct ftrace_hash *)&empty_hash)
1112 static struct ftrace_ops global_ops
= {
1113 .func
= ftrace_stub
,
1114 .local_hash
.notrace_hash
= EMPTY_HASH
,
1115 .local_hash
.filter_hash
= EMPTY_HASH
,
1116 INIT_OPS_HASH(global_ops
)
1117 .flags
= FTRACE_OPS_FL_RECURSION_SAFE
|
1118 FTRACE_OPS_FL_INITIALIZED
|
1123 * Used by the stack undwinder to know about dynamic ftrace trampolines.
1125 struct ftrace_ops
*ftrace_ops_trampoline(unsigned long addr
)
1127 struct ftrace_ops
*op
= NULL
;
1130 * Some of the ops may be dynamically allocated,
1131 * they are freed after a synchronize_sched().
1133 preempt_disable_notrace();
1135 do_for_each_ftrace_op(op
, ftrace_ops_list
) {
1137 * This is to check for dynamically allocated trampolines.
1138 * Trampolines that are in kernel text will have
1139 * core_kernel_text() return true.
1141 if (op
->trampoline
&& op
->trampoline_size
)
1142 if (addr
>= op
->trampoline
&&
1143 addr
< op
->trampoline
+ op
->trampoline_size
) {
1144 preempt_enable_notrace();
1147 } while_for_each_ftrace_op(op
);
1148 preempt_enable_notrace();
1154 * This is used by __kernel_text_address() to return true if the
1155 * address is on a dynamically allocated trampoline that would
1156 * not return true for either core_kernel_text() or
1157 * is_module_text_address().
1159 bool is_ftrace_trampoline(unsigned long addr
)
1161 return ftrace_ops_trampoline(addr
) != NULL
;
1164 struct ftrace_page
{
1165 struct ftrace_page
*next
;
1166 struct dyn_ftrace
*records
;
1171 #define ENTRY_SIZE sizeof(struct dyn_ftrace)
1172 #define ENTRIES_PER_PAGE (PAGE_SIZE / ENTRY_SIZE)
1174 /* estimate from running different kernels */
1175 #define NR_TO_INIT 10000
1177 static struct ftrace_page
*ftrace_pages_start
;
1178 static struct ftrace_page
*ftrace_pages
;
1180 static __always_inline
unsigned long
1181 ftrace_hash_key(struct ftrace_hash
*hash
, unsigned long ip
)
1183 if (hash
->size_bits
> 0)
1184 return hash_long(ip
, hash
->size_bits
);
1189 /* Only use this function if ftrace_hash_empty() has already been tested */
1190 static __always_inline
struct ftrace_func_entry
*
1191 __ftrace_lookup_ip(struct ftrace_hash
*hash
, unsigned long ip
)
1194 struct ftrace_func_entry
*entry
;
1195 struct hlist_head
*hhd
;
1197 key
= ftrace_hash_key(hash
, ip
);
1198 hhd
= &hash
->buckets
[key
];
1200 hlist_for_each_entry_rcu_notrace(entry
, hhd
, hlist
) {
1201 if (entry
->ip
== ip
)
1208 * ftrace_lookup_ip - Test to see if an ip exists in an ftrace_hash
1209 * @hash: The hash to look at
1210 * @ip: The instruction pointer to test
1212 * Search a given @hash to see if a given instruction pointer (@ip)
1215 * Returns the entry that holds the @ip if found. NULL otherwise.
1217 struct ftrace_func_entry
*
1218 ftrace_lookup_ip(struct ftrace_hash
*hash
, unsigned long ip
)
1220 if (ftrace_hash_empty(hash
))
1223 return __ftrace_lookup_ip(hash
, ip
);
1226 static void __add_hash_entry(struct ftrace_hash
*hash
,
1227 struct ftrace_func_entry
*entry
)
1229 struct hlist_head
*hhd
;
1232 key
= ftrace_hash_key(hash
, entry
->ip
);
1233 hhd
= &hash
->buckets
[key
];
1234 hlist_add_head(&entry
->hlist
, hhd
);
1238 static int add_hash_entry(struct ftrace_hash
*hash
, unsigned long ip
)
1240 struct ftrace_func_entry
*entry
;
1242 entry
= kmalloc(sizeof(*entry
), GFP_KERNEL
);
1247 __add_hash_entry(hash
, entry
);
1253 free_hash_entry(struct ftrace_hash
*hash
,
1254 struct ftrace_func_entry
*entry
)
1256 hlist_del(&entry
->hlist
);
1262 remove_hash_entry(struct ftrace_hash
*hash
,
1263 struct ftrace_func_entry
*entry
)
1265 hlist_del_rcu(&entry
->hlist
);
1269 static void ftrace_hash_clear(struct ftrace_hash
*hash
)
1271 struct hlist_head
*hhd
;
1272 struct hlist_node
*tn
;
1273 struct ftrace_func_entry
*entry
;
1274 int size
= 1 << hash
->size_bits
;
1280 for (i
= 0; i
< size
; i
++) {
1281 hhd
= &hash
->buckets
[i
];
1282 hlist_for_each_entry_safe(entry
, tn
, hhd
, hlist
)
1283 free_hash_entry(hash
, entry
);
1285 FTRACE_WARN_ON(hash
->count
);
1288 static void free_ftrace_mod(struct ftrace_mod_load
*ftrace_mod
)
1290 list_del(&ftrace_mod
->list
);
1291 kfree(ftrace_mod
->module
);
1292 kfree(ftrace_mod
->func
);
1296 static void clear_ftrace_mod_list(struct list_head
*head
)
1298 struct ftrace_mod_load
*p
, *n
;
1300 /* stack tracer isn't supported yet */
1304 mutex_lock(&ftrace_lock
);
1305 list_for_each_entry_safe(p
, n
, head
, list
)
1307 mutex_unlock(&ftrace_lock
);
1310 static void free_ftrace_hash(struct ftrace_hash
*hash
)
1312 if (!hash
|| hash
== EMPTY_HASH
)
1314 ftrace_hash_clear(hash
);
1315 kfree(hash
->buckets
);
1319 static void __free_ftrace_hash_rcu(struct rcu_head
*rcu
)
1321 struct ftrace_hash
*hash
;
1323 hash
= container_of(rcu
, struct ftrace_hash
, rcu
);
1324 free_ftrace_hash(hash
);
1327 static void free_ftrace_hash_rcu(struct ftrace_hash
*hash
)
1329 if (!hash
|| hash
== EMPTY_HASH
)
1331 call_rcu_sched(&hash
->rcu
, __free_ftrace_hash_rcu
);
1334 void ftrace_free_filter(struct ftrace_ops
*ops
)
1336 ftrace_ops_init(ops
);
1337 free_ftrace_hash(ops
->func_hash
->filter_hash
);
1338 free_ftrace_hash(ops
->func_hash
->notrace_hash
);
1341 static struct ftrace_hash
*alloc_ftrace_hash(int size_bits
)
1343 struct ftrace_hash
*hash
;
1346 hash
= kzalloc(sizeof(*hash
), GFP_KERNEL
);
1350 size
= 1 << size_bits
;
1351 hash
->buckets
= kcalloc(size
, sizeof(*hash
->buckets
), GFP_KERNEL
);
1353 if (!hash
->buckets
) {
1358 hash
->size_bits
= size_bits
;
1364 static int ftrace_add_mod(struct trace_array
*tr
,
1365 const char *func
, const char *module
,
1368 struct ftrace_mod_load
*ftrace_mod
;
1369 struct list_head
*mod_head
= enable
? &tr
->mod_trace
: &tr
->mod_notrace
;
1371 ftrace_mod
= kzalloc(sizeof(*ftrace_mod
), GFP_KERNEL
);
1375 ftrace_mod
->func
= kstrdup(func
, GFP_KERNEL
);
1376 ftrace_mod
->module
= kstrdup(module
, GFP_KERNEL
);
1377 ftrace_mod
->enable
= enable
;
1379 if (!ftrace_mod
->func
|| !ftrace_mod
->module
)
1382 list_add(&ftrace_mod
->list
, mod_head
);
1387 free_ftrace_mod(ftrace_mod
);
1392 static struct ftrace_hash
*
1393 alloc_and_copy_ftrace_hash(int size_bits
, struct ftrace_hash
*hash
)
1395 struct ftrace_func_entry
*entry
;
1396 struct ftrace_hash
*new_hash
;
1401 new_hash
= alloc_ftrace_hash(size_bits
);
1406 new_hash
->flags
= hash
->flags
;
1409 if (ftrace_hash_empty(hash
))
1412 size
= 1 << hash
->size_bits
;
1413 for (i
= 0; i
< size
; i
++) {
1414 hlist_for_each_entry(entry
, &hash
->buckets
[i
], hlist
) {
1415 ret
= add_hash_entry(new_hash
, entry
->ip
);
1421 FTRACE_WARN_ON(new_hash
->count
!= hash
->count
);
1426 free_ftrace_hash(new_hash
);
1431 ftrace_hash_rec_disable_modify(struct ftrace_ops
*ops
, int filter_hash
);
1433 ftrace_hash_rec_enable_modify(struct ftrace_ops
*ops
, int filter_hash
);
1435 static int ftrace_hash_ipmodify_update(struct ftrace_ops
*ops
,
1436 struct ftrace_hash
*new_hash
);
1438 static struct ftrace_hash
*
1439 __ftrace_hash_move(struct ftrace_hash
*src
)
1441 struct ftrace_func_entry
*entry
;
1442 struct hlist_node
*tn
;
1443 struct hlist_head
*hhd
;
1444 struct ftrace_hash
*new_hash
;
1445 int size
= src
->count
;
1450 * If the new source is empty, just return the empty_hash.
1452 if (ftrace_hash_empty(src
))
1456 * Make the hash size about 1/2 the # found
1458 for (size
/= 2; size
; size
>>= 1)
1461 /* Don't allocate too much */
1462 if (bits
> FTRACE_HASH_MAX_BITS
)
1463 bits
= FTRACE_HASH_MAX_BITS
;
1465 new_hash
= alloc_ftrace_hash(bits
);
1469 new_hash
->flags
= src
->flags
;
1471 size
= 1 << src
->size_bits
;
1472 for (i
= 0; i
< size
; i
++) {
1473 hhd
= &src
->buckets
[i
];
1474 hlist_for_each_entry_safe(entry
, tn
, hhd
, hlist
) {
1475 remove_hash_entry(src
, entry
);
1476 __add_hash_entry(new_hash
, entry
);
1484 ftrace_hash_move(struct ftrace_ops
*ops
, int enable
,
1485 struct ftrace_hash
**dst
, struct ftrace_hash
*src
)
1487 struct ftrace_hash
*new_hash
;
1490 /* Reject setting notrace hash on IPMODIFY ftrace_ops */
1491 if (ops
->flags
& FTRACE_OPS_FL_IPMODIFY
&& !enable
)
1494 new_hash
= __ftrace_hash_move(src
);
1498 /* Make sure this can be applied if it is IPMODIFY ftrace_ops */
1500 /* IPMODIFY should be updated only when filter_hash updating */
1501 ret
= ftrace_hash_ipmodify_update(ops
, new_hash
);
1503 free_ftrace_hash(new_hash
);
1509 * Remove the current set, update the hash and add
1512 ftrace_hash_rec_disable_modify(ops
, enable
);
1514 rcu_assign_pointer(*dst
, new_hash
);
1516 ftrace_hash_rec_enable_modify(ops
, enable
);
1521 static bool hash_contains_ip(unsigned long ip
,
1522 struct ftrace_ops_hash
*hash
)
1525 * The function record is a match if it exists in the filter
1526 * hash and not in the notrace hash. Note, an emty hash is
1527 * considered a match for the filter hash, but an empty
1528 * notrace hash is considered not in the notrace hash.
1530 return (ftrace_hash_empty(hash
->filter_hash
) ||
1531 __ftrace_lookup_ip(hash
->filter_hash
, ip
)) &&
1532 (ftrace_hash_empty(hash
->notrace_hash
) ||
1533 !__ftrace_lookup_ip(hash
->notrace_hash
, ip
));
1537 * Test the hashes for this ops to see if we want to call
1538 * the ops->func or not.
1540 * It's a match if the ip is in the ops->filter_hash or
1541 * the filter_hash does not exist or is empty,
1543 * the ip is not in the ops->notrace_hash.
1545 * This needs to be called with preemption disabled as
1546 * the hashes are freed with call_rcu_sched().
1549 ftrace_ops_test(struct ftrace_ops
*ops
, unsigned long ip
, void *regs
)
1551 struct ftrace_ops_hash hash
;
1554 #ifdef CONFIG_DYNAMIC_FTRACE_WITH_REGS
1556 * There's a small race when adding ops that the ftrace handler
1557 * that wants regs, may be called without them. We can not
1558 * allow that handler to be called if regs is NULL.
1560 if (regs
== NULL
&& (ops
->flags
& FTRACE_OPS_FL_SAVE_REGS
))
1564 rcu_assign_pointer(hash
.filter_hash
, ops
->func_hash
->filter_hash
);
1565 rcu_assign_pointer(hash
.notrace_hash
, ops
->func_hash
->notrace_hash
);
1567 if (hash_contains_ip(ip
, &hash
))
1576 * This is a double for. Do not use 'break' to break out of the loop,
1577 * you must use a goto.
1579 #define do_for_each_ftrace_rec(pg, rec) \
1580 for (pg = ftrace_pages_start; pg; pg = pg->next) { \
1582 for (_____i = 0; _____i < pg->index; _____i++) { \
1583 rec = &pg->records[_____i];
1585 #define while_for_each_ftrace_rec() \
1590 static int ftrace_cmp_recs(const void *a
, const void *b
)
1592 const struct dyn_ftrace
*key
= a
;
1593 const struct dyn_ftrace
*rec
= b
;
1595 if (key
->flags
< rec
->ip
)
1597 if (key
->ip
>= rec
->ip
+ MCOUNT_INSN_SIZE
)
1603 * ftrace_location_range - return the first address of a traced location
1604 * if it touches the given ip range
1605 * @start: start of range to search.
1606 * @end: end of range to search (inclusive). @end points to the last byte
1609 * Returns rec->ip if the related ftrace location is a least partly within
1610 * the given address range. That is, the first address of the instruction
1611 * that is either a NOP or call to the function tracer. It checks the ftrace
1612 * internal tables to determine if the address belongs or not.
1614 unsigned long ftrace_location_range(unsigned long start
, unsigned long end
)
1616 struct ftrace_page
*pg
;
1617 struct dyn_ftrace
*rec
;
1618 struct dyn_ftrace key
;
1621 key
.flags
= end
; /* overload flags, as it is unsigned long */
1623 for (pg
= ftrace_pages_start
; pg
; pg
= pg
->next
) {
1624 if (end
< pg
->records
[0].ip
||
1625 start
>= (pg
->records
[pg
->index
- 1].ip
+ MCOUNT_INSN_SIZE
))
1627 rec
= bsearch(&key
, pg
->records
, pg
->index
,
1628 sizeof(struct dyn_ftrace
),
1638 * ftrace_location - return true if the ip giving is a traced location
1639 * @ip: the instruction pointer to check
1641 * Returns rec->ip if @ip given is a pointer to a ftrace location.
1642 * That is, the instruction that is either a NOP or call to
1643 * the function tracer. It checks the ftrace internal tables to
1644 * determine if the address belongs or not.
1646 unsigned long ftrace_location(unsigned long ip
)
1648 return ftrace_location_range(ip
, ip
);
1652 * ftrace_text_reserved - return true if range contains an ftrace location
1653 * @start: start of range to search
1654 * @end: end of range to search (inclusive). @end points to the last byte to check.
1656 * Returns 1 if @start and @end contains a ftrace location.
1657 * That is, the instruction that is either a NOP or call to
1658 * the function tracer. It checks the ftrace internal tables to
1659 * determine if the address belongs or not.
1661 int ftrace_text_reserved(const void *start
, const void *end
)
1665 ret
= ftrace_location_range((unsigned long)start
,
1666 (unsigned long)end
);
1671 /* Test if ops registered to this rec needs regs */
1672 static bool test_rec_ops_needs_regs(struct dyn_ftrace
*rec
)
1674 struct ftrace_ops
*ops
;
1675 bool keep_regs
= false;
1677 for (ops
= ftrace_ops_list
;
1678 ops
!= &ftrace_list_end
; ops
= ops
->next
) {
1679 /* pass rec in as regs to have non-NULL val */
1680 if (ftrace_ops_test(ops
, rec
->ip
, rec
)) {
1681 if (ops
->flags
& FTRACE_OPS_FL_SAVE_REGS
) {
1691 static bool __ftrace_hash_rec_update(struct ftrace_ops
*ops
,
1695 struct ftrace_hash
*hash
;
1696 struct ftrace_hash
*other_hash
;
1697 struct ftrace_page
*pg
;
1698 struct dyn_ftrace
*rec
;
1699 bool update
= false;
1703 /* Only update if the ops has been registered */
1704 if (!(ops
->flags
& FTRACE_OPS_FL_ENABLED
))
1708 * In the filter_hash case:
1709 * If the count is zero, we update all records.
1710 * Otherwise we just update the items in the hash.
1712 * In the notrace_hash case:
1713 * We enable the update in the hash.
1714 * As disabling notrace means enabling the tracing,
1715 * and enabling notrace means disabling, the inc variable
1719 hash
= ops
->func_hash
->filter_hash
;
1720 other_hash
= ops
->func_hash
->notrace_hash
;
1721 if (ftrace_hash_empty(hash
))
1725 hash
= ops
->func_hash
->notrace_hash
;
1726 other_hash
= ops
->func_hash
->filter_hash
;
1728 * If the notrace hash has no items,
1729 * then there's nothing to do.
1731 if (ftrace_hash_empty(hash
))
1735 do_for_each_ftrace_rec(pg
, rec
) {
1736 int in_other_hash
= 0;
1740 if (rec
->flags
& FTRACE_FL_DISABLED
)
1745 * Only the filter_hash affects all records.
1746 * Update if the record is not in the notrace hash.
1748 if (!other_hash
|| !ftrace_lookup_ip(other_hash
, rec
->ip
))
1751 in_hash
= !!ftrace_lookup_ip(hash
, rec
->ip
);
1752 in_other_hash
= !!ftrace_lookup_ip(other_hash
, rec
->ip
);
1755 * If filter_hash is set, we want to match all functions
1756 * that are in the hash but not in the other hash.
1758 * If filter_hash is not set, then we are decrementing.
1759 * That means we match anything that is in the hash
1760 * and also in the other_hash. That is, we need to turn
1761 * off functions in the other hash because they are disabled
1764 if (filter_hash
&& in_hash
&& !in_other_hash
)
1766 else if (!filter_hash
&& in_hash
&&
1767 (in_other_hash
|| ftrace_hash_empty(other_hash
)))
1775 if (FTRACE_WARN_ON(ftrace_rec_count(rec
) == FTRACE_REF_MAX
))
1779 * If there's only a single callback registered to a
1780 * function, and the ops has a trampoline registered
1781 * for it, then we can call it directly.
1783 if (ftrace_rec_count(rec
) == 1 && ops
->trampoline
)
1784 rec
->flags
|= FTRACE_FL_TRAMP
;
1787 * If we are adding another function callback
1788 * to this function, and the previous had a
1789 * custom trampoline in use, then we need to go
1790 * back to the default trampoline.
1792 rec
->flags
&= ~FTRACE_FL_TRAMP
;
1795 * If any ops wants regs saved for this function
1796 * then all ops will get saved regs.
1798 if (ops
->flags
& FTRACE_OPS_FL_SAVE_REGS
)
1799 rec
->flags
|= FTRACE_FL_REGS
;
1801 if (FTRACE_WARN_ON(ftrace_rec_count(rec
) == 0))
1806 * If the rec had REGS enabled and the ops that is
1807 * being removed had REGS set, then see if there is
1808 * still any ops for this record that wants regs.
1809 * If not, we can stop recording them.
1811 if (ftrace_rec_count(rec
) > 0 &&
1812 rec
->flags
& FTRACE_FL_REGS
&&
1813 ops
->flags
& FTRACE_OPS_FL_SAVE_REGS
) {
1814 if (!test_rec_ops_needs_regs(rec
))
1815 rec
->flags
&= ~FTRACE_FL_REGS
;
1819 * If the rec had TRAMP enabled, then it needs to
1820 * be cleared. As TRAMP can only be enabled iff
1821 * there is only a single ops attached to it.
1822 * In otherwords, always disable it on decrementing.
1823 * In the future, we may set it if rec count is
1824 * decremented to one, and the ops that is left
1827 rec
->flags
&= ~FTRACE_FL_TRAMP
;
1830 * flags will be cleared in ftrace_check_record()
1831 * if rec count is zero.
1836 /* Must match FTRACE_UPDATE_CALLS in ftrace_modify_all_code() */
1837 update
|= ftrace_test_record(rec
, 1) != FTRACE_UPDATE_IGNORE
;
1839 /* Shortcut, if we handled all records, we are done. */
1840 if (!all
&& count
== hash
->count
)
1842 } while_for_each_ftrace_rec();
1847 static bool ftrace_hash_rec_disable(struct ftrace_ops
*ops
,
1850 return __ftrace_hash_rec_update(ops
, filter_hash
, 0);
1853 static bool ftrace_hash_rec_enable(struct ftrace_ops
*ops
,
1856 return __ftrace_hash_rec_update(ops
, filter_hash
, 1);
1859 static void ftrace_hash_rec_update_modify(struct ftrace_ops
*ops
,
1860 int filter_hash
, int inc
)
1862 struct ftrace_ops
*op
;
1864 __ftrace_hash_rec_update(ops
, filter_hash
, inc
);
1866 if (ops
->func_hash
!= &global_ops
.local_hash
)
1870 * If the ops shares the global_ops hash, then we need to update
1871 * all ops that are enabled and use this hash.
1873 do_for_each_ftrace_op(op
, ftrace_ops_list
) {
1877 if (op
->func_hash
== &global_ops
.local_hash
)
1878 __ftrace_hash_rec_update(op
, filter_hash
, inc
);
1879 } while_for_each_ftrace_op(op
);
1882 static void ftrace_hash_rec_disable_modify(struct ftrace_ops
*ops
,
1885 ftrace_hash_rec_update_modify(ops
, filter_hash
, 0);
1888 static void ftrace_hash_rec_enable_modify(struct ftrace_ops
*ops
,
1891 ftrace_hash_rec_update_modify(ops
, filter_hash
, 1);
1895 * Try to update IPMODIFY flag on each ftrace_rec. Return 0 if it is OK
1896 * or no-needed to update, -EBUSY if it detects a conflict of the flag
1897 * on a ftrace_rec, and -EINVAL if the new_hash tries to trace all recs.
1898 * Note that old_hash and new_hash has below meanings
1899 * - If the hash is NULL, it hits all recs (if IPMODIFY is set, this is rejected)
1900 * - If the hash is EMPTY_HASH, it hits nothing
1901 * - Anything else hits the recs which match the hash entries.
1903 static int __ftrace_hash_update_ipmodify(struct ftrace_ops
*ops
,
1904 struct ftrace_hash
*old_hash
,
1905 struct ftrace_hash
*new_hash
)
1907 struct ftrace_page
*pg
;
1908 struct dyn_ftrace
*rec
, *end
= NULL
;
1911 /* Only update if the ops has been registered */
1912 if (!(ops
->flags
& FTRACE_OPS_FL_ENABLED
))
1915 if (!(ops
->flags
& FTRACE_OPS_FL_IPMODIFY
))
1919 * Since the IPMODIFY is a very address sensitive action, we do not
1920 * allow ftrace_ops to set all functions to new hash.
1922 if (!new_hash
|| !old_hash
)
1925 /* Update rec->flags */
1926 do_for_each_ftrace_rec(pg
, rec
) {
1928 if (rec
->flags
& FTRACE_FL_DISABLED
)
1931 /* We need to update only differences of filter_hash */
1932 in_old
= !!ftrace_lookup_ip(old_hash
, rec
->ip
);
1933 in_new
= !!ftrace_lookup_ip(new_hash
, rec
->ip
);
1934 if (in_old
== in_new
)
1938 /* New entries must ensure no others are using it */
1939 if (rec
->flags
& FTRACE_FL_IPMODIFY
)
1941 rec
->flags
|= FTRACE_FL_IPMODIFY
;
1942 } else /* Removed entry */
1943 rec
->flags
&= ~FTRACE_FL_IPMODIFY
;
1944 } while_for_each_ftrace_rec();
1951 /* Roll back what we did above */
1952 do_for_each_ftrace_rec(pg
, rec
) {
1954 if (rec
->flags
& FTRACE_FL_DISABLED
)
1960 in_old
= !!ftrace_lookup_ip(old_hash
, rec
->ip
);
1961 in_new
= !!ftrace_lookup_ip(new_hash
, rec
->ip
);
1962 if (in_old
== in_new
)
1966 rec
->flags
&= ~FTRACE_FL_IPMODIFY
;
1968 rec
->flags
|= FTRACE_FL_IPMODIFY
;
1969 } while_for_each_ftrace_rec();
1975 static int ftrace_hash_ipmodify_enable(struct ftrace_ops
*ops
)
1977 struct ftrace_hash
*hash
= ops
->func_hash
->filter_hash
;
1979 if (ftrace_hash_empty(hash
))
1982 return __ftrace_hash_update_ipmodify(ops
, EMPTY_HASH
, hash
);
1985 /* Disabling always succeeds */
1986 static void ftrace_hash_ipmodify_disable(struct ftrace_ops
*ops
)
1988 struct ftrace_hash
*hash
= ops
->func_hash
->filter_hash
;
1990 if (ftrace_hash_empty(hash
))
1993 __ftrace_hash_update_ipmodify(ops
, hash
, EMPTY_HASH
);
1996 static int ftrace_hash_ipmodify_update(struct ftrace_ops
*ops
,
1997 struct ftrace_hash
*new_hash
)
1999 struct ftrace_hash
*old_hash
= ops
->func_hash
->filter_hash
;
2001 if (ftrace_hash_empty(old_hash
))
2004 if (ftrace_hash_empty(new_hash
))
2007 return __ftrace_hash_update_ipmodify(ops
, old_hash
, new_hash
);
2010 static void print_ip_ins(const char *fmt
, const unsigned char *p
)
2014 printk(KERN_CONT
"%s", fmt
);
2016 for (i
= 0; i
< MCOUNT_INSN_SIZE
; i
++)
2017 printk(KERN_CONT
"%s%02x", i
? ":" : "", p
[i
]);
2020 static struct ftrace_ops
*
2021 ftrace_find_tramp_ops_any(struct dyn_ftrace
*rec
);
2022 static struct ftrace_ops
*
2023 ftrace_find_tramp_ops_next(struct dyn_ftrace
*rec
, struct ftrace_ops
*ops
);
2025 enum ftrace_bug_type ftrace_bug_type
;
2026 const void *ftrace_expected
;
2028 static void print_bug_type(void)
2030 switch (ftrace_bug_type
) {
2031 case FTRACE_BUG_UNKNOWN
:
2033 case FTRACE_BUG_INIT
:
2034 pr_info("Initializing ftrace call sites\n");
2036 case FTRACE_BUG_NOP
:
2037 pr_info("Setting ftrace call site to NOP\n");
2039 case FTRACE_BUG_CALL
:
2040 pr_info("Setting ftrace call site to call ftrace function\n");
2042 case FTRACE_BUG_UPDATE
:
2043 pr_info("Updating ftrace call site to call a different ftrace function\n");
2049 * ftrace_bug - report and shutdown function tracer
2050 * @failed: The failed type (EFAULT, EINVAL, EPERM)
2051 * @rec: The record that failed
2053 * The arch code that enables or disables the function tracing
2054 * can call ftrace_bug() when it has detected a problem in
2055 * modifying the code. @failed should be one of either:
2056 * EFAULT - if the problem happens on reading the @ip address
2057 * EINVAL - if what is read at @ip is not what was expected
2058 * EPERM - if the problem happens on writting to the @ip address
2060 void ftrace_bug(int failed
, struct dyn_ftrace
*rec
)
2062 unsigned long ip
= rec
? rec
->ip
: 0;
2066 FTRACE_WARN_ON_ONCE(1);
2067 pr_info("ftrace faulted on modifying ");
2071 FTRACE_WARN_ON_ONCE(1);
2072 pr_info("ftrace failed to modify ");
2074 print_ip_ins(" actual: ", (unsigned char *)ip
);
2076 if (ftrace_expected
) {
2077 print_ip_ins(" expected: ", ftrace_expected
);
2082 FTRACE_WARN_ON_ONCE(1);
2083 pr_info("ftrace faulted on writing ");
2087 FTRACE_WARN_ON_ONCE(1);
2088 pr_info("ftrace faulted on unknown error ");
2093 struct ftrace_ops
*ops
= NULL
;
2095 pr_info("ftrace record flags: %lx\n", rec
->flags
);
2096 pr_cont(" (%ld)%s", ftrace_rec_count(rec
),
2097 rec
->flags
& FTRACE_FL_REGS
? " R" : " ");
2098 if (rec
->flags
& FTRACE_FL_TRAMP_EN
) {
2099 ops
= ftrace_find_tramp_ops_any(rec
);
2102 pr_cont("\ttramp: %pS (%pS)",
2103 (void *)ops
->trampoline
,
2105 ops
= ftrace_find_tramp_ops_next(rec
, ops
);
2108 pr_cont("\ttramp: ERROR!");
2111 ip
= ftrace_get_addr_curr(rec
);
2112 pr_cont("\n expected tramp: %lx\n", ip
);
2116 static int ftrace_check_record(struct dyn_ftrace
*rec
, int enable
, int update
)
2118 unsigned long flag
= 0UL;
2120 ftrace_bug_type
= FTRACE_BUG_UNKNOWN
;
2122 if (rec
->flags
& FTRACE_FL_DISABLED
)
2123 return FTRACE_UPDATE_IGNORE
;
2126 * If we are updating calls:
2128 * If the record has a ref count, then we need to enable it
2129 * because someone is using it.
2131 * Otherwise we make sure its disabled.
2133 * If we are disabling calls, then disable all records that
2136 if (enable
&& ftrace_rec_count(rec
))
2137 flag
= FTRACE_FL_ENABLED
;
2140 * If enabling and the REGS flag does not match the REGS_EN, or
2141 * the TRAMP flag doesn't match the TRAMP_EN, then do not ignore
2142 * this record. Set flags to fail the compare against ENABLED.
2145 if (!(rec
->flags
& FTRACE_FL_REGS
) !=
2146 !(rec
->flags
& FTRACE_FL_REGS_EN
))
2147 flag
|= FTRACE_FL_REGS
;
2149 if (!(rec
->flags
& FTRACE_FL_TRAMP
) !=
2150 !(rec
->flags
& FTRACE_FL_TRAMP_EN
))
2151 flag
|= FTRACE_FL_TRAMP
;
2154 /* If the state of this record hasn't changed, then do nothing */
2155 if ((rec
->flags
& FTRACE_FL_ENABLED
) == flag
)
2156 return FTRACE_UPDATE_IGNORE
;
2159 /* Save off if rec is being enabled (for return value) */
2160 flag
^= rec
->flags
& FTRACE_FL_ENABLED
;
2163 rec
->flags
|= FTRACE_FL_ENABLED
;
2164 if (flag
& FTRACE_FL_REGS
) {
2165 if (rec
->flags
& FTRACE_FL_REGS
)
2166 rec
->flags
|= FTRACE_FL_REGS_EN
;
2168 rec
->flags
&= ~FTRACE_FL_REGS_EN
;
2170 if (flag
& FTRACE_FL_TRAMP
) {
2171 if (rec
->flags
& FTRACE_FL_TRAMP
)
2172 rec
->flags
|= FTRACE_FL_TRAMP_EN
;
2174 rec
->flags
&= ~FTRACE_FL_TRAMP_EN
;
2179 * If this record is being updated from a nop, then
2180 * return UPDATE_MAKE_CALL.
2182 * return UPDATE_MODIFY_CALL to tell the caller to convert
2183 * from the save regs, to a non-save regs function or
2184 * vice versa, or from a trampoline call.
2186 if (flag
& FTRACE_FL_ENABLED
) {
2187 ftrace_bug_type
= FTRACE_BUG_CALL
;
2188 return FTRACE_UPDATE_MAKE_CALL
;
2191 ftrace_bug_type
= FTRACE_BUG_UPDATE
;
2192 return FTRACE_UPDATE_MODIFY_CALL
;
2196 /* If there's no more users, clear all flags */
2197 if (!ftrace_rec_count(rec
))
2201 * Just disable the record, but keep the ops TRAMP
2202 * and REGS states. The _EN flags must be disabled though.
2204 rec
->flags
&= ~(FTRACE_FL_ENABLED
| FTRACE_FL_TRAMP_EN
|
2208 ftrace_bug_type
= FTRACE_BUG_NOP
;
2209 return FTRACE_UPDATE_MAKE_NOP
;
2213 * ftrace_update_record, set a record that now is tracing or not
2214 * @rec: the record to update
2215 * @enable: set to 1 if the record is tracing, zero to force disable
2217 * The records that represent all functions that can be traced need
2218 * to be updated when tracing has been enabled.
2220 int ftrace_update_record(struct dyn_ftrace
*rec
, int enable
)
2222 return ftrace_check_record(rec
, enable
, 1);
2226 * ftrace_test_record, check if the record has been enabled or not
2227 * @rec: the record to test
2228 * @enable: set to 1 to check if enabled, 0 if it is disabled
2230 * The arch code may need to test if a record is already set to
2231 * tracing to determine how to modify the function code that it
2234 int ftrace_test_record(struct dyn_ftrace
*rec
, int enable
)
2236 return ftrace_check_record(rec
, enable
, 0);
2239 static struct ftrace_ops
*
2240 ftrace_find_tramp_ops_any(struct dyn_ftrace
*rec
)
2242 struct ftrace_ops
*op
;
2243 unsigned long ip
= rec
->ip
;
2245 do_for_each_ftrace_op(op
, ftrace_ops_list
) {
2247 if (!op
->trampoline
)
2250 if (hash_contains_ip(ip
, op
->func_hash
))
2252 } while_for_each_ftrace_op(op
);
2257 static struct ftrace_ops
*
2258 ftrace_find_tramp_ops_next(struct dyn_ftrace
*rec
,
2259 struct ftrace_ops
*op
)
2261 unsigned long ip
= rec
->ip
;
2263 while_for_each_ftrace_op(op
) {
2265 if (!op
->trampoline
)
2268 if (hash_contains_ip(ip
, op
->func_hash
))
2275 static struct ftrace_ops
*
2276 ftrace_find_tramp_ops_curr(struct dyn_ftrace
*rec
)
2278 struct ftrace_ops
*op
;
2279 unsigned long ip
= rec
->ip
;
2282 * Need to check removed ops first.
2283 * If they are being removed, and this rec has a tramp,
2284 * and this rec is in the ops list, then it would be the
2285 * one with the tramp.
2288 if (hash_contains_ip(ip
, &removed_ops
->old_hash
))
2293 * Need to find the current trampoline for a rec.
2294 * Now, a trampoline is only attached to a rec if there
2295 * was a single 'ops' attached to it. But this can be called
2296 * when we are adding another op to the rec or removing the
2297 * current one. Thus, if the op is being added, we can
2298 * ignore it because it hasn't attached itself to the rec
2301 * If an ops is being modified (hooking to different functions)
2302 * then we don't care about the new functions that are being
2303 * added, just the old ones (that are probably being removed).
2305 * If we are adding an ops to a function that already is using
2306 * a trampoline, it needs to be removed (trampolines are only
2307 * for single ops connected), then an ops that is not being
2308 * modified also needs to be checked.
2310 do_for_each_ftrace_op(op
, ftrace_ops_list
) {
2312 if (!op
->trampoline
)
2316 * If the ops is being added, it hasn't gotten to
2317 * the point to be removed from this tree yet.
2319 if (op
->flags
& FTRACE_OPS_FL_ADDING
)
2324 * If the ops is being modified and is in the old
2325 * hash, then it is probably being removed from this
2328 if ((op
->flags
& FTRACE_OPS_FL_MODIFYING
) &&
2329 hash_contains_ip(ip
, &op
->old_hash
))
2332 * If the ops is not being added or modified, and it's
2333 * in its normal filter hash, then this must be the one
2336 if (!(op
->flags
& FTRACE_OPS_FL_MODIFYING
) &&
2337 hash_contains_ip(ip
, op
->func_hash
))
2340 } while_for_each_ftrace_op(op
);
2345 static struct ftrace_ops
*
2346 ftrace_find_tramp_ops_new(struct dyn_ftrace
*rec
)
2348 struct ftrace_ops
*op
;
2349 unsigned long ip
= rec
->ip
;
2351 do_for_each_ftrace_op(op
, ftrace_ops_list
) {
2352 /* pass rec in as regs to have non-NULL val */
2353 if (hash_contains_ip(ip
, op
->func_hash
))
2355 } while_for_each_ftrace_op(op
);
2361 * ftrace_get_addr_new - Get the call address to set to
2362 * @rec: The ftrace record descriptor
2364 * If the record has the FTRACE_FL_REGS set, that means that it
2365 * wants to convert to a callback that saves all regs. If FTRACE_FL_REGS
2366 * is not not set, then it wants to convert to the normal callback.
2368 * Returns the address of the trampoline to set to
2370 unsigned long ftrace_get_addr_new(struct dyn_ftrace
*rec
)
2372 struct ftrace_ops
*ops
;
2374 /* Trampolines take precedence over regs */
2375 if (rec
->flags
& FTRACE_FL_TRAMP
) {
2376 ops
= ftrace_find_tramp_ops_new(rec
);
2377 if (FTRACE_WARN_ON(!ops
|| !ops
->trampoline
)) {
2378 pr_warn("Bad trampoline accounting at: %p (%pS) (%lx)\n",
2379 (void *)rec
->ip
, (void *)rec
->ip
, rec
->flags
);
2380 /* Ftrace is shutting down, return anything */
2381 return (unsigned long)FTRACE_ADDR
;
2383 return ops
->trampoline
;
2386 if (rec
->flags
& FTRACE_FL_REGS
)
2387 return (unsigned long)FTRACE_REGS_ADDR
;
2389 return (unsigned long)FTRACE_ADDR
;
2393 * ftrace_get_addr_curr - Get the call address that is already there
2394 * @rec: The ftrace record descriptor
2396 * The FTRACE_FL_REGS_EN is set when the record already points to
2397 * a function that saves all the regs. Basically the '_EN' version
2398 * represents the current state of the function.
2400 * Returns the address of the trampoline that is currently being called
2402 unsigned long ftrace_get_addr_curr(struct dyn_ftrace
*rec
)
2404 struct ftrace_ops
*ops
;
2406 /* Trampolines take precedence over regs */
2407 if (rec
->flags
& FTRACE_FL_TRAMP_EN
) {
2408 ops
= ftrace_find_tramp_ops_curr(rec
);
2409 if (FTRACE_WARN_ON(!ops
)) {
2410 pr_warn("Bad trampoline accounting at: %p (%pS)\n",
2411 (void *)rec
->ip
, (void *)rec
->ip
);
2412 /* Ftrace is shutting down, return anything */
2413 return (unsigned long)FTRACE_ADDR
;
2415 return ops
->trampoline
;
2418 if (rec
->flags
& FTRACE_FL_REGS_EN
)
2419 return (unsigned long)FTRACE_REGS_ADDR
;
2421 return (unsigned long)FTRACE_ADDR
;
2425 __ftrace_replace_code(struct dyn_ftrace
*rec
, int enable
)
2427 unsigned long ftrace_old_addr
;
2428 unsigned long ftrace_addr
;
2431 ftrace_addr
= ftrace_get_addr_new(rec
);
2433 /* This needs to be done before we call ftrace_update_record */
2434 ftrace_old_addr
= ftrace_get_addr_curr(rec
);
2436 ret
= ftrace_update_record(rec
, enable
);
2438 ftrace_bug_type
= FTRACE_BUG_UNKNOWN
;
2441 case FTRACE_UPDATE_IGNORE
:
2444 case FTRACE_UPDATE_MAKE_CALL
:
2445 ftrace_bug_type
= FTRACE_BUG_CALL
;
2446 return ftrace_make_call(rec
, ftrace_addr
);
2448 case FTRACE_UPDATE_MAKE_NOP
:
2449 ftrace_bug_type
= FTRACE_BUG_NOP
;
2450 return ftrace_make_nop(NULL
, rec
, ftrace_old_addr
);
2452 case FTRACE_UPDATE_MODIFY_CALL
:
2453 ftrace_bug_type
= FTRACE_BUG_UPDATE
;
2454 return ftrace_modify_call(rec
, ftrace_old_addr
, ftrace_addr
);
2457 return -1; /* unknow ftrace bug */
2460 void __weak
ftrace_replace_code(int enable
)
2462 struct dyn_ftrace
*rec
;
2463 struct ftrace_page
*pg
;
2466 if (unlikely(ftrace_disabled
))
2469 do_for_each_ftrace_rec(pg
, rec
) {
2471 if (rec
->flags
& FTRACE_FL_DISABLED
)
2474 failed
= __ftrace_replace_code(rec
, enable
);
2476 ftrace_bug(failed
, rec
);
2477 /* Stop processing */
2480 } while_for_each_ftrace_rec();
2483 struct ftrace_rec_iter
{
2484 struct ftrace_page
*pg
;
2489 * ftrace_rec_iter_start, start up iterating over traced functions
2491 * Returns an iterator handle that is used to iterate over all
2492 * the records that represent address locations where functions
2495 * May return NULL if no records are available.
2497 struct ftrace_rec_iter
*ftrace_rec_iter_start(void)
2500 * We only use a single iterator.
2501 * Protected by the ftrace_lock mutex.
2503 static struct ftrace_rec_iter ftrace_rec_iter
;
2504 struct ftrace_rec_iter
*iter
= &ftrace_rec_iter
;
2506 iter
->pg
= ftrace_pages_start
;
2509 /* Could have empty pages */
2510 while (iter
->pg
&& !iter
->pg
->index
)
2511 iter
->pg
= iter
->pg
->next
;
2520 * ftrace_rec_iter_next, get the next record to process.
2521 * @iter: The handle to the iterator.
2523 * Returns the next iterator after the given iterator @iter.
2525 struct ftrace_rec_iter
*ftrace_rec_iter_next(struct ftrace_rec_iter
*iter
)
2529 if (iter
->index
>= iter
->pg
->index
) {
2530 iter
->pg
= iter
->pg
->next
;
2533 /* Could have empty pages */
2534 while (iter
->pg
&& !iter
->pg
->index
)
2535 iter
->pg
= iter
->pg
->next
;
2545 * ftrace_rec_iter_record, get the record at the iterator location
2546 * @iter: The current iterator location
2548 * Returns the record that the current @iter is at.
2550 struct dyn_ftrace
*ftrace_rec_iter_record(struct ftrace_rec_iter
*iter
)
2552 return &iter
->pg
->records
[iter
->index
];
2556 ftrace_code_disable(struct module
*mod
, struct dyn_ftrace
*rec
)
2560 if (unlikely(ftrace_disabled
))
2563 ret
= ftrace_make_nop(mod
, rec
, MCOUNT_ADDR
);
2565 ftrace_bug_type
= FTRACE_BUG_INIT
;
2566 ftrace_bug(ret
, rec
);
2573 * archs can override this function if they must do something
2574 * before the modifying code is performed.
2576 int __weak
ftrace_arch_code_modify_prepare(void)
2582 * archs can override this function if they must do something
2583 * after the modifying code is performed.
2585 int __weak
ftrace_arch_code_modify_post_process(void)
2590 void ftrace_modify_all_code(int command
)
2592 int update
= command
& FTRACE_UPDATE_TRACE_FUNC
;
2596 * If the ftrace_caller calls a ftrace_ops func directly,
2597 * we need to make sure that it only traces functions it
2598 * expects to trace. When doing the switch of functions,
2599 * we need to update to the ftrace_ops_list_func first
2600 * before the transition between old and new calls are set,
2601 * as the ftrace_ops_list_func will check the ops hashes
2602 * to make sure the ops are having the right functions
2606 err
= ftrace_update_ftrace_func(ftrace_ops_list_func
);
2607 if (FTRACE_WARN_ON(err
))
2611 if (command
& FTRACE_UPDATE_CALLS
)
2612 ftrace_replace_code(1);
2613 else if (command
& FTRACE_DISABLE_CALLS
)
2614 ftrace_replace_code(0);
2616 if (update
&& ftrace_trace_function
!= ftrace_ops_list_func
) {
2617 function_trace_op
= set_function_trace_op
;
2619 /* If irqs are disabled, we are in stop machine */
2620 if (!irqs_disabled())
2621 smp_call_function(ftrace_sync_ipi
, NULL
, 1);
2622 err
= ftrace_update_ftrace_func(ftrace_trace_function
);
2623 if (FTRACE_WARN_ON(err
))
2627 if (command
& FTRACE_START_FUNC_RET
)
2628 err
= ftrace_enable_ftrace_graph_caller();
2629 else if (command
& FTRACE_STOP_FUNC_RET
)
2630 err
= ftrace_disable_ftrace_graph_caller();
2631 FTRACE_WARN_ON(err
);
2634 static int __ftrace_modify_code(void *data
)
2636 int *command
= data
;
2638 ftrace_modify_all_code(*command
);
2644 * ftrace_run_stop_machine, go back to the stop machine method
2645 * @command: The command to tell ftrace what to do
2647 * If an arch needs to fall back to the stop machine method, the
2648 * it can call this function.
2650 void ftrace_run_stop_machine(int command
)
2652 stop_machine(__ftrace_modify_code
, &command
, NULL
);
2656 * arch_ftrace_update_code, modify the code to trace or not trace
2657 * @command: The command that needs to be done
2659 * Archs can override this function if it does not need to
2660 * run stop_machine() to modify code.
2662 void __weak
arch_ftrace_update_code(int command
)
2664 ftrace_run_stop_machine(command
);
2667 static void ftrace_run_update_code(int command
)
2671 ret
= ftrace_arch_code_modify_prepare();
2672 FTRACE_WARN_ON(ret
);
2677 * By default we use stop_machine() to modify the code.
2678 * But archs can do what ever they want as long as it
2679 * is safe. The stop_machine() is the safest, but also
2680 * produces the most overhead.
2682 arch_ftrace_update_code(command
);
2684 ret
= ftrace_arch_code_modify_post_process();
2685 FTRACE_WARN_ON(ret
);
2688 static void ftrace_run_modify_code(struct ftrace_ops
*ops
, int command
,
2689 struct ftrace_ops_hash
*old_hash
)
2691 ops
->flags
|= FTRACE_OPS_FL_MODIFYING
;
2692 ops
->old_hash
.filter_hash
= old_hash
->filter_hash
;
2693 ops
->old_hash
.notrace_hash
= old_hash
->notrace_hash
;
2694 ftrace_run_update_code(command
);
2695 ops
->old_hash
.filter_hash
= NULL
;
2696 ops
->old_hash
.notrace_hash
= NULL
;
2697 ops
->flags
&= ~FTRACE_OPS_FL_MODIFYING
;
2700 static ftrace_func_t saved_ftrace_func
;
2701 static int ftrace_start_up
;
2703 void __weak
arch_ftrace_trampoline_free(struct ftrace_ops
*ops
)
2707 static void ftrace_startup_enable(int command
)
2709 if (saved_ftrace_func
!= ftrace_trace_function
) {
2710 saved_ftrace_func
= ftrace_trace_function
;
2711 command
|= FTRACE_UPDATE_TRACE_FUNC
;
2714 if (!command
|| !ftrace_enabled
)
2717 ftrace_run_update_code(command
);
2720 static void ftrace_startup_all(int command
)
2722 update_all_ops
= true;
2723 ftrace_startup_enable(command
);
2724 update_all_ops
= false;
2727 static int ftrace_startup(struct ftrace_ops
*ops
, int command
)
2731 if (unlikely(ftrace_disabled
))
2734 ret
= __register_ftrace_function(ops
);
2741 * Note that ftrace probes uses this to start up
2742 * and modify functions it will probe. But we still
2743 * set the ADDING flag for modification, as probes
2744 * do not have trampolines. If they add them in the
2745 * future, then the probes will need to distinguish
2746 * between adding and updating probes.
2748 ops
->flags
|= FTRACE_OPS_FL_ENABLED
| FTRACE_OPS_FL_ADDING
;
2750 ret
= ftrace_hash_ipmodify_enable(ops
);
2752 /* Rollback registration process */
2753 __unregister_ftrace_function(ops
);
2755 ops
->flags
&= ~FTRACE_OPS_FL_ENABLED
;
2759 if (ftrace_hash_rec_enable(ops
, 1))
2760 command
|= FTRACE_UPDATE_CALLS
;
2762 ftrace_startup_enable(command
);
2764 ops
->flags
&= ~FTRACE_OPS_FL_ADDING
;
2769 static int ftrace_shutdown(struct ftrace_ops
*ops
, int command
)
2773 if (unlikely(ftrace_disabled
))
2776 ret
= __unregister_ftrace_function(ops
);
2782 * Just warn in case of unbalance, no need to kill ftrace, it's not
2783 * critical but the ftrace_call callers may be never nopped again after
2784 * further ftrace uses.
2786 WARN_ON_ONCE(ftrace_start_up
< 0);
2788 /* Disabling ipmodify never fails */
2789 ftrace_hash_ipmodify_disable(ops
);
2791 if (ftrace_hash_rec_disable(ops
, 1))
2792 command
|= FTRACE_UPDATE_CALLS
;
2794 ops
->flags
&= ~FTRACE_OPS_FL_ENABLED
;
2796 if (saved_ftrace_func
!= ftrace_trace_function
) {
2797 saved_ftrace_func
= ftrace_trace_function
;
2798 command
|= FTRACE_UPDATE_TRACE_FUNC
;
2801 if (!command
|| !ftrace_enabled
) {
2803 * If these are dynamic or per_cpu ops, they still
2804 * need their data freed. Since, function tracing is
2805 * not currently active, we can just free them
2806 * without synchronizing all CPUs.
2808 if (ops
->flags
& FTRACE_OPS_FL_DYNAMIC
)
2815 * If the ops uses a trampoline, then it needs to be
2816 * tested first on update.
2818 ops
->flags
|= FTRACE_OPS_FL_REMOVING
;
2821 /* The trampoline logic checks the old hashes */
2822 ops
->old_hash
.filter_hash
= ops
->func_hash
->filter_hash
;
2823 ops
->old_hash
.notrace_hash
= ops
->func_hash
->notrace_hash
;
2825 ftrace_run_update_code(command
);
2828 * If there's no more ops registered with ftrace, run a
2829 * sanity check to make sure all rec flags are cleared.
2831 if (rcu_dereference_protected(ftrace_ops_list
,
2832 lockdep_is_held(&ftrace_lock
)) == &ftrace_list_end
) {
2833 struct ftrace_page
*pg
;
2834 struct dyn_ftrace
*rec
;
2836 do_for_each_ftrace_rec(pg
, rec
) {
2837 if (FTRACE_WARN_ON_ONCE(rec
->flags
& ~FTRACE_FL_DISABLED
))
2838 pr_warn(" %pS flags:%lx\n",
2839 (void *)rec
->ip
, rec
->flags
);
2840 } while_for_each_ftrace_rec();
2843 ops
->old_hash
.filter_hash
= NULL
;
2844 ops
->old_hash
.notrace_hash
= NULL
;
2847 ops
->flags
&= ~FTRACE_OPS_FL_REMOVING
;
2850 * Dynamic ops may be freed, we must make sure that all
2851 * callers are done before leaving this function.
2852 * The same goes for freeing the per_cpu data of the per_cpu
2855 if (ops
->flags
& FTRACE_OPS_FL_DYNAMIC
) {
2857 * We need to do a hard force of sched synchronization.
2858 * This is because we use preempt_disable() to do RCU, but
2859 * the function tracers can be called where RCU is not watching
2860 * (like before user_exit()). We can not rely on the RCU
2861 * infrastructure to do the synchronization, thus we must do it
2864 schedule_on_each_cpu(ftrace_sync
);
2867 * When the kernel is preeptive, tasks can be preempted
2868 * while on a ftrace trampoline. Just scheduling a task on
2869 * a CPU is not good enough to flush them. Calling
2870 * synchornize_rcu_tasks() will wait for those tasks to
2871 * execute and either schedule voluntarily or enter user space.
2873 if (IS_ENABLED(CONFIG_PREEMPT
))
2874 synchronize_rcu_tasks();
2877 arch_ftrace_trampoline_free(ops
);
2883 static void ftrace_startup_sysctl(void)
2887 if (unlikely(ftrace_disabled
))
2890 /* Force update next time */
2891 saved_ftrace_func
= NULL
;
2892 /* ftrace_start_up is true if we want ftrace running */
2893 if (ftrace_start_up
) {
2894 command
= FTRACE_UPDATE_CALLS
;
2895 if (ftrace_graph_active
)
2896 command
|= FTRACE_START_FUNC_RET
;
2897 ftrace_startup_enable(command
);
2901 static void ftrace_shutdown_sysctl(void)
2905 if (unlikely(ftrace_disabled
))
2908 /* ftrace_start_up is true if ftrace is running */
2909 if (ftrace_start_up
) {
2910 command
= FTRACE_DISABLE_CALLS
;
2911 if (ftrace_graph_active
)
2912 command
|= FTRACE_STOP_FUNC_RET
;
2913 ftrace_run_update_code(command
);
2917 static u64 ftrace_update_time
;
2918 unsigned long ftrace_update_tot_cnt
;
2920 static inline int ops_traces_mod(struct ftrace_ops
*ops
)
2923 * Filter_hash being empty will default to trace module.
2924 * But notrace hash requires a test of individual module functions.
2926 return ftrace_hash_empty(ops
->func_hash
->filter_hash
) &&
2927 ftrace_hash_empty(ops
->func_hash
->notrace_hash
);
2931 * Check if the current ops references the record.
2933 * If the ops traces all functions, then it was already accounted for.
2934 * If the ops does not trace the current record function, skip it.
2935 * If the ops ignores the function via notrace filter, skip it.
2938 ops_references_rec(struct ftrace_ops
*ops
, struct dyn_ftrace
*rec
)
2940 /* If ops isn't enabled, ignore it */
2941 if (!(ops
->flags
& FTRACE_OPS_FL_ENABLED
))
2944 /* If ops traces all then it includes this function */
2945 if (ops_traces_mod(ops
))
2948 /* The function must be in the filter */
2949 if (!ftrace_hash_empty(ops
->func_hash
->filter_hash
) &&
2950 !__ftrace_lookup_ip(ops
->func_hash
->filter_hash
, rec
->ip
))
2953 /* If in notrace hash, we ignore it too */
2954 if (ftrace_lookup_ip(ops
->func_hash
->notrace_hash
, rec
->ip
))
2960 static int ftrace_update_code(struct module
*mod
, struct ftrace_page
*new_pgs
)
2962 struct ftrace_page
*pg
;
2963 struct dyn_ftrace
*p
;
2965 unsigned long update_cnt
= 0;
2966 unsigned long rec_flags
= 0;
2969 start
= ftrace_now(raw_smp_processor_id());
2972 * When a module is loaded, this function is called to convert
2973 * the calls to mcount in its text to nops, and also to create
2974 * an entry in the ftrace data. Now, if ftrace is activated
2975 * after this call, but before the module sets its text to
2976 * read-only, the modification of enabling ftrace can fail if
2977 * the read-only is done while ftrace is converting the calls.
2978 * To prevent this, the module's records are set as disabled
2979 * and will be enabled after the call to set the module's text
2983 rec_flags
|= FTRACE_FL_DISABLED
;
2985 for (pg
= new_pgs
; pg
; pg
= pg
->next
) {
2987 for (i
= 0; i
< pg
->index
; i
++) {
2989 /* If something went wrong, bail without enabling anything */
2990 if (unlikely(ftrace_disabled
))
2993 p
= &pg
->records
[i
];
2994 p
->flags
= rec_flags
;
2997 * Do the initial record conversion from mcount jump
2998 * to the NOP instructions.
3000 if (!ftrace_code_disable(mod
, p
))
3007 stop
= ftrace_now(raw_smp_processor_id());
3008 ftrace_update_time
= stop
- start
;
3009 ftrace_update_tot_cnt
+= update_cnt
;
3014 static int ftrace_allocate_records(struct ftrace_page
*pg
, int count
)
3019 if (WARN_ON(!count
))
3022 order
= get_count_order(DIV_ROUND_UP(count
, ENTRIES_PER_PAGE
));
3025 * We want to fill as much as possible. No more than a page
3028 while ((PAGE_SIZE
<< order
) / ENTRY_SIZE
>= count
+ ENTRIES_PER_PAGE
)
3032 pg
->records
= (void *)__get_free_pages(GFP_KERNEL
| __GFP_ZERO
, order
);
3035 /* if we can't allocate this size, try something smaller */
3042 cnt
= (PAGE_SIZE
<< order
) / ENTRY_SIZE
;
3051 static struct ftrace_page
*
3052 ftrace_allocate_pages(unsigned long num_to_init
)
3054 struct ftrace_page
*start_pg
;
3055 struct ftrace_page
*pg
;
3062 start_pg
= pg
= kzalloc(sizeof(*pg
), GFP_KERNEL
);
3067 * Try to allocate as much as possible in one continues
3068 * location that fills in all of the space. We want to
3069 * waste as little space as possible.
3072 cnt
= ftrace_allocate_records(pg
, num_to_init
);
3080 pg
->next
= kzalloc(sizeof(*pg
), GFP_KERNEL
);
3092 order
= get_count_order(pg
->size
/ ENTRIES_PER_PAGE
);
3093 free_pages((unsigned long)pg
->records
, order
);
3094 start_pg
= pg
->next
;
3098 pr_info("ftrace: FAILED to allocate memory for functions\n");
3102 #define FTRACE_BUFF_MAX (KSYM_SYMBOL_LEN+4) /* room for wildcards */
3104 struct ftrace_iterator
{
3108 struct ftrace_page
*pg
;
3109 struct dyn_ftrace
*func
;
3110 struct ftrace_func_probe
*probe
;
3111 struct ftrace_func_entry
*probe_entry
;
3112 struct trace_parser parser
;
3113 struct ftrace_hash
*hash
;
3114 struct ftrace_ops
*ops
;
3115 struct trace_array
*tr
;
3116 struct list_head
*mod_list
;
3123 t_probe_next(struct seq_file
*m
, loff_t
*pos
)
3125 struct ftrace_iterator
*iter
= m
->private;
3126 struct trace_array
*tr
= iter
->ops
->private;
3127 struct list_head
*func_probes
;
3128 struct ftrace_hash
*hash
;
3129 struct list_head
*next
;
3130 struct hlist_node
*hnd
= NULL
;
3131 struct hlist_head
*hhd
;
3140 func_probes
= &tr
->func_probes
;
3141 if (list_empty(func_probes
))
3145 next
= func_probes
->next
;
3146 iter
->probe
= list_entry(next
, struct ftrace_func_probe
, list
);
3149 if (iter
->probe_entry
)
3150 hnd
= &iter
->probe_entry
->hlist
;
3152 hash
= iter
->probe
->ops
.func_hash
->filter_hash
;
3153 size
= 1 << hash
->size_bits
;
3156 if (iter
->pidx
>= size
) {
3157 if (iter
->probe
->list
.next
== func_probes
)
3159 next
= iter
->probe
->list
.next
;
3160 iter
->probe
= list_entry(next
, struct ftrace_func_probe
, list
);
3161 hash
= iter
->probe
->ops
.func_hash
->filter_hash
;
3162 size
= 1 << hash
->size_bits
;
3166 hhd
= &hash
->buckets
[iter
->pidx
];
3168 if (hlist_empty(hhd
)) {
3184 if (WARN_ON_ONCE(!hnd
))
3187 iter
->probe_entry
= hlist_entry(hnd
, struct ftrace_func_entry
, hlist
);
3192 static void *t_probe_start(struct seq_file
*m
, loff_t
*pos
)
3194 struct ftrace_iterator
*iter
= m
->private;
3198 if (!(iter
->flags
& FTRACE_ITER_DO_PROBES
))
3201 if (iter
->mod_pos
> *pos
)
3205 iter
->probe_entry
= NULL
;
3207 for (l
= 0; l
<= (*pos
- iter
->mod_pos
); ) {
3208 p
= t_probe_next(m
, &l
);
3215 /* Only set this if we have an item */
3216 iter
->flags
|= FTRACE_ITER_PROBE
;
3222 t_probe_show(struct seq_file
*m
, struct ftrace_iterator
*iter
)
3224 struct ftrace_func_entry
*probe_entry
;
3225 struct ftrace_probe_ops
*probe_ops
;
3226 struct ftrace_func_probe
*probe
;
3228 probe
= iter
->probe
;
3229 probe_entry
= iter
->probe_entry
;
3231 if (WARN_ON_ONCE(!probe
|| !probe_entry
))
3234 probe_ops
= probe
->probe_ops
;
3236 if (probe_ops
->print
)
3237 return probe_ops
->print(m
, probe_entry
->ip
, probe_ops
, probe
->data
);
3239 seq_printf(m
, "%ps:%ps\n", (void *)probe_entry
->ip
,
3240 (void *)probe_ops
->func
);
3246 t_mod_next(struct seq_file
*m
, loff_t
*pos
)
3248 struct ftrace_iterator
*iter
= m
->private;
3249 struct trace_array
*tr
= iter
->tr
;
3254 iter
->mod_list
= iter
->mod_list
->next
;
3256 if (iter
->mod_list
== &tr
->mod_trace
||
3257 iter
->mod_list
== &tr
->mod_notrace
) {
3258 iter
->flags
&= ~FTRACE_ITER_MOD
;
3262 iter
->mod_pos
= *pos
;
3267 static void *t_mod_start(struct seq_file
*m
, loff_t
*pos
)
3269 struct ftrace_iterator
*iter
= m
->private;
3273 if (iter
->func_pos
> *pos
)
3276 iter
->mod_pos
= iter
->func_pos
;
3278 /* probes are only available if tr is set */
3282 for (l
= 0; l
<= (*pos
- iter
->func_pos
); ) {
3283 p
= t_mod_next(m
, &l
);
3288 iter
->flags
&= ~FTRACE_ITER_MOD
;
3289 return t_probe_start(m
, pos
);
3292 /* Only set this if we have an item */
3293 iter
->flags
|= FTRACE_ITER_MOD
;
3299 t_mod_show(struct seq_file
*m
, struct ftrace_iterator
*iter
)
3301 struct ftrace_mod_load
*ftrace_mod
;
3302 struct trace_array
*tr
= iter
->tr
;
3304 if (WARN_ON_ONCE(!iter
->mod_list
) ||
3305 iter
->mod_list
== &tr
->mod_trace
||
3306 iter
->mod_list
== &tr
->mod_notrace
)
3309 ftrace_mod
= list_entry(iter
->mod_list
, struct ftrace_mod_load
, list
);
3311 if (ftrace_mod
->func
)
3312 seq_printf(m
, "%s", ftrace_mod
->func
);
3316 seq_printf(m
, ":mod:%s\n", ftrace_mod
->module
);
3322 t_func_next(struct seq_file
*m
, loff_t
*pos
)
3324 struct ftrace_iterator
*iter
= m
->private;
3325 struct dyn_ftrace
*rec
= NULL
;
3330 if (iter
->idx
>= iter
->pg
->index
) {
3331 if (iter
->pg
->next
) {
3332 iter
->pg
= iter
->pg
->next
;
3337 rec
= &iter
->pg
->records
[iter
->idx
++];
3338 if (((iter
->flags
& (FTRACE_ITER_FILTER
| FTRACE_ITER_NOTRACE
)) &&
3339 !ftrace_lookup_ip(iter
->hash
, rec
->ip
)) ||
3341 ((iter
->flags
& FTRACE_ITER_ENABLED
) &&
3342 !(rec
->flags
& FTRACE_FL_ENABLED
))) {
3352 iter
->pos
= iter
->func_pos
= *pos
;
3359 t_next(struct seq_file
*m
, void *v
, loff_t
*pos
)
3361 struct ftrace_iterator
*iter
= m
->private;
3362 loff_t l
= *pos
; /* t_probe_start() must use original pos */
3365 if (unlikely(ftrace_disabled
))
3368 if (iter
->flags
& FTRACE_ITER_PROBE
)
3369 return t_probe_next(m
, pos
);
3371 if (iter
->flags
& FTRACE_ITER_MOD
)
3372 return t_mod_next(m
, pos
);
3374 if (iter
->flags
& FTRACE_ITER_PRINTALL
) {
3375 /* next must increment pos, and t_probe_start does not */
3377 return t_mod_start(m
, &l
);
3380 ret
= t_func_next(m
, pos
);
3383 return t_mod_start(m
, &l
);
3388 static void reset_iter_read(struct ftrace_iterator
*iter
)
3392 iter
->flags
&= ~(FTRACE_ITER_PRINTALL
| FTRACE_ITER_PROBE
| FTRACE_ITER_MOD
);
3395 static void *t_start(struct seq_file
*m
, loff_t
*pos
)
3397 struct ftrace_iterator
*iter
= m
->private;
3401 mutex_lock(&ftrace_lock
);
3403 if (unlikely(ftrace_disabled
))
3407 * If an lseek was done, then reset and start from beginning.
3409 if (*pos
< iter
->pos
)
3410 reset_iter_read(iter
);
3413 * For set_ftrace_filter reading, if we have the filter
3414 * off, we can short cut and just print out that all
3415 * functions are enabled.
3417 if ((iter
->flags
& (FTRACE_ITER_FILTER
| FTRACE_ITER_NOTRACE
)) &&
3418 ftrace_hash_empty(iter
->hash
)) {
3419 iter
->func_pos
= 1; /* Account for the message */
3421 return t_mod_start(m
, pos
);
3422 iter
->flags
|= FTRACE_ITER_PRINTALL
;
3423 /* reset in case of seek/pread */
3424 iter
->flags
&= ~FTRACE_ITER_PROBE
;
3428 if (iter
->flags
& FTRACE_ITER_MOD
)
3429 return t_mod_start(m
, pos
);
3432 * Unfortunately, we need to restart at ftrace_pages_start
3433 * every time we let go of the ftrace_mutex. This is because
3434 * those pointers can change without the lock.
3436 iter
->pg
= ftrace_pages_start
;
3438 for (l
= 0; l
<= *pos
; ) {
3439 p
= t_func_next(m
, &l
);
3445 return t_mod_start(m
, pos
);
3450 static void t_stop(struct seq_file
*m
, void *p
)
3452 mutex_unlock(&ftrace_lock
);
3456 arch_ftrace_trampoline_func(struct ftrace_ops
*ops
, struct dyn_ftrace
*rec
)
3461 static void add_trampoline_func(struct seq_file
*m
, struct ftrace_ops
*ops
,
3462 struct dyn_ftrace
*rec
)
3466 ptr
= arch_ftrace_trampoline_func(ops
, rec
);
3468 seq_printf(m
, " ->%pS", ptr
);
3471 static int t_show(struct seq_file
*m
, void *v
)
3473 struct ftrace_iterator
*iter
= m
->private;
3474 struct dyn_ftrace
*rec
;
3476 if (iter
->flags
& FTRACE_ITER_PROBE
)
3477 return t_probe_show(m
, iter
);
3479 if (iter
->flags
& FTRACE_ITER_MOD
)
3480 return t_mod_show(m
, iter
);
3482 if (iter
->flags
& FTRACE_ITER_PRINTALL
) {
3483 if (iter
->flags
& FTRACE_ITER_NOTRACE
)
3484 seq_puts(m
, "#### no functions disabled ####\n");
3486 seq_puts(m
, "#### all functions enabled ####\n");
3495 seq_printf(m
, "%ps", (void *)rec
->ip
);
3496 if (iter
->flags
& FTRACE_ITER_ENABLED
) {
3497 struct ftrace_ops
*ops
;
3499 seq_printf(m
, " (%ld)%s%s",
3500 ftrace_rec_count(rec
),
3501 rec
->flags
& FTRACE_FL_REGS
? " R" : " ",
3502 rec
->flags
& FTRACE_FL_IPMODIFY
? " I" : " ");
3503 if (rec
->flags
& FTRACE_FL_TRAMP_EN
) {
3504 ops
= ftrace_find_tramp_ops_any(rec
);
3507 seq_printf(m
, "\ttramp: %pS (%pS)",
3508 (void *)ops
->trampoline
,
3510 add_trampoline_func(m
, ops
, rec
);
3511 ops
= ftrace_find_tramp_ops_next(rec
, ops
);
3514 seq_puts(m
, "\ttramp: ERROR!");
3516 add_trampoline_func(m
, NULL
, rec
);
3525 static const struct seq_operations show_ftrace_seq_ops
= {
3533 ftrace_avail_open(struct inode
*inode
, struct file
*file
)
3535 struct ftrace_iterator
*iter
;
3537 if (unlikely(ftrace_disabled
))
3540 iter
= __seq_open_private(file
, &show_ftrace_seq_ops
, sizeof(*iter
));
3544 iter
->pg
= ftrace_pages_start
;
3545 iter
->ops
= &global_ops
;
3551 ftrace_enabled_open(struct inode
*inode
, struct file
*file
)
3553 struct ftrace_iterator
*iter
;
3555 iter
= __seq_open_private(file
, &show_ftrace_seq_ops
, sizeof(*iter
));
3559 iter
->pg
= ftrace_pages_start
;
3560 iter
->flags
= FTRACE_ITER_ENABLED
;
3561 iter
->ops
= &global_ops
;
3567 * ftrace_regex_open - initialize function tracer filter files
3568 * @ops: The ftrace_ops that hold the hash filters
3569 * @flag: The type of filter to process
3570 * @inode: The inode, usually passed in to your open routine
3571 * @file: The file, usually passed in to your open routine
3573 * ftrace_regex_open() initializes the filter files for the
3574 * @ops. Depending on @flag it may process the filter hash or
3575 * the notrace hash of @ops. With this called from the open
3576 * routine, you can use ftrace_filter_write() for the write
3577 * routine if @flag has FTRACE_ITER_FILTER set, or
3578 * ftrace_notrace_write() if @flag has FTRACE_ITER_NOTRACE set.
3579 * tracing_lseek() should be used as the lseek routine, and
3580 * release must call ftrace_regex_release().
3583 ftrace_regex_open(struct ftrace_ops
*ops
, int flag
,
3584 struct inode
*inode
, struct file
*file
)
3586 struct ftrace_iterator
*iter
;
3587 struct ftrace_hash
*hash
;
3588 struct list_head
*mod_head
;
3589 struct trace_array
*tr
= ops
->private;
3592 ftrace_ops_init(ops
);
3594 if (unlikely(ftrace_disabled
))
3597 iter
= kzalloc(sizeof(*iter
), GFP_KERNEL
);
3601 if (trace_parser_get_init(&iter
->parser
, FTRACE_BUFF_MAX
)) {
3610 mutex_lock(&ops
->func_hash
->regex_lock
);
3612 if (flag
& FTRACE_ITER_NOTRACE
) {
3613 hash
= ops
->func_hash
->notrace_hash
;
3614 mod_head
= tr
? &tr
->mod_notrace
: NULL
;
3616 hash
= ops
->func_hash
->filter_hash
;
3617 mod_head
= tr
? &tr
->mod_trace
: NULL
;
3620 iter
->mod_list
= mod_head
;
3622 if (file
->f_mode
& FMODE_WRITE
) {
3623 const int size_bits
= FTRACE_HASH_DEFAULT_BITS
;
3625 if (file
->f_flags
& O_TRUNC
) {
3626 iter
->hash
= alloc_ftrace_hash(size_bits
);
3627 clear_ftrace_mod_list(mod_head
);
3629 iter
->hash
= alloc_and_copy_ftrace_hash(size_bits
, hash
);
3633 trace_parser_put(&iter
->parser
);
3641 if (file
->f_mode
& FMODE_READ
) {
3642 iter
->pg
= ftrace_pages_start
;
3644 ret
= seq_open(file
, &show_ftrace_seq_ops
);
3646 struct seq_file
*m
= file
->private_data
;
3650 free_ftrace_hash(iter
->hash
);
3651 trace_parser_put(&iter
->parser
);
3655 file
->private_data
= iter
;
3658 mutex_unlock(&ops
->func_hash
->regex_lock
);
3664 ftrace_filter_open(struct inode
*inode
, struct file
*file
)
3666 struct ftrace_ops
*ops
= inode
->i_private
;
3668 return ftrace_regex_open(ops
,
3669 FTRACE_ITER_FILTER
| FTRACE_ITER_DO_PROBES
,
3674 ftrace_notrace_open(struct inode
*inode
, struct file
*file
)
3676 struct ftrace_ops
*ops
= inode
->i_private
;
3678 return ftrace_regex_open(ops
, FTRACE_ITER_NOTRACE
,
3682 /* Type for quick search ftrace basic regexes (globs) from filter_parse_regex */
3683 struct ftrace_glob
{
3690 * If symbols in an architecture don't correspond exactly to the user-visible
3691 * name of what they represent, it is possible to define this function to
3692 * perform the necessary adjustments.
3694 char * __weak
arch_ftrace_match_adjust(char *str
, const char *search
)
3699 static int ftrace_match(char *str
, struct ftrace_glob
*g
)
3704 str
= arch_ftrace_match_adjust(str
, g
->search
);
3708 if (strcmp(str
, g
->search
) == 0)
3711 case MATCH_FRONT_ONLY
:
3712 if (strncmp(str
, g
->search
, g
->len
) == 0)
3715 case MATCH_MIDDLE_ONLY
:
3716 if (strstr(str
, g
->search
))
3719 case MATCH_END_ONLY
:
3721 if (slen
>= g
->len
&&
3722 memcmp(str
+ slen
- g
->len
, g
->search
, g
->len
) == 0)
3726 if (glob_match(g
->search
, str
))
3735 enter_record(struct ftrace_hash
*hash
, struct dyn_ftrace
*rec
, int clear_filter
)
3737 struct ftrace_func_entry
*entry
;
3740 entry
= ftrace_lookup_ip(hash
, rec
->ip
);
3742 /* Do nothing if it doesn't exist */
3746 free_hash_entry(hash
, entry
);
3748 /* Do nothing if it exists */
3752 ret
= add_hash_entry(hash
, rec
->ip
);
3758 ftrace_match_record(struct dyn_ftrace
*rec
, struct ftrace_glob
*func_g
,
3759 struct ftrace_glob
*mod_g
, int exclude_mod
)
3761 char str
[KSYM_SYMBOL_LEN
];
3764 kallsyms_lookup(rec
->ip
, NULL
, NULL
, &modname
, str
);
3767 int mod_matches
= (modname
) ? ftrace_match(modname
, mod_g
) : 0;
3769 /* blank module name to match all modules */
3771 /* blank module globbing: modname xor exclude_mod */
3772 if (!exclude_mod
!= !modname
)
3778 * exclude_mod is set to trace everything but the given
3779 * module. If it is set and the module matches, then
3780 * return 0. If it is not set, and the module doesn't match
3781 * also return 0. Otherwise, check the function to see if
3784 if (!mod_matches
== !exclude_mod
)
3787 /* blank search means to match all funcs in the mod */
3792 return ftrace_match(str
, func_g
);
3796 match_records(struct ftrace_hash
*hash
, char *func
, int len
, char *mod
)
3798 struct ftrace_page
*pg
;
3799 struct dyn_ftrace
*rec
;
3800 struct ftrace_glob func_g
= { .type
= MATCH_FULL
};
3801 struct ftrace_glob mod_g
= { .type
= MATCH_FULL
};
3802 struct ftrace_glob
*mod_match
= (mod
) ? &mod_g
: NULL
;
3803 int exclude_mod
= 0;
3806 int clear_filter
= 0;
3809 func_g
.type
= filter_parse_regex(func
, len
, &func_g
.search
,
3811 func_g
.len
= strlen(func_g
.search
);
3815 mod_g
.type
= filter_parse_regex(mod
, strlen(mod
),
3816 &mod_g
.search
, &exclude_mod
);
3817 mod_g
.len
= strlen(mod_g
.search
);
3820 mutex_lock(&ftrace_lock
);
3822 if (unlikely(ftrace_disabled
))
3825 do_for_each_ftrace_rec(pg
, rec
) {
3827 if (rec
->flags
& FTRACE_FL_DISABLED
)
3830 if (ftrace_match_record(rec
, &func_g
, mod_match
, exclude_mod
)) {
3831 ret
= enter_record(hash
, rec
, clear_filter
);
3838 } while_for_each_ftrace_rec();
3840 mutex_unlock(&ftrace_lock
);
3846 ftrace_match_records(struct ftrace_hash
*hash
, char *buff
, int len
)
3848 return match_records(hash
, buff
, len
, NULL
);
3851 static void ftrace_ops_update_code(struct ftrace_ops
*ops
,
3852 struct ftrace_ops_hash
*old_hash
)
3854 struct ftrace_ops
*op
;
3856 if (!ftrace_enabled
)
3859 if (ops
->flags
& FTRACE_OPS_FL_ENABLED
) {
3860 ftrace_run_modify_code(ops
, FTRACE_UPDATE_CALLS
, old_hash
);
3865 * If this is the shared global_ops filter, then we need to
3866 * check if there is another ops that shares it, is enabled.
3867 * If so, we still need to run the modify code.
3869 if (ops
->func_hash
!= &global_ops
.local_hash
)
3872 do_for_each_ftrace_op(op
, ftrace_ops_list
) {
3873 if (op
->func_hash
== &global_ops
.local_hash
&&
3874 op
->flags
& FTRACE_OPS_FL_ENABLED
) {
3875 ftrace_run_modify_code(op
, FTRACE_UPDATE_CALLS
, old_hash
);
3876 /* Only need to do this once */
3879 } while_for_each_ftrace_op(op
);
3882 static int ftrace_hash_move_and_update_ops(struct ftrace_ops
*ops
,
3883 struct ftrace_hash
**orig_hash
,
3884 struct ftrace_hash
*hash
,
3887 struct ftrace_ops_hash old_hash_ops
;
3888 struct ftrace_hash
*old_hash
;
3891 old_hash
= *orig_hash
;
3892 old_hash_ops
.filter_hash
= ops
->func_hash
->filter_hash
;
3893 old_hash_ops
.notrace_hash
= ops
->func_hash
->notrace_hash
;
3894 ret
= ftrace_hash_move(ops
, enable
, orig_hash
, hash
);
3896 ftrace_ops_update_code(ops
, &old_hash_ops
);
3897 free_ftrace_hash_rcu(old_hash
);
3902 static bool module_exists(const char *module
)
3904 /* All modules have the symbol __this_module */
3905 const char this_mod
[] = "__this_module";
3906 const int modname_size
= MAX_PARAM_PREFIX_LEN
+ sizeof(this_mod
) + 1;
3907 char modname
[modname_size
+ 1];
3911 n
= snprintf(modname
, modname_size
+ 1, "%s:%s", module
, this_mod
);
3913 if (n
> modname_size
)
3916 val
= module_kallsyms_lookup_name(modname
);
3920 static int cache_mod(struct trace_array
*tr
,
3921 const char *func
, char *module
, int enable
)
3923 struct ftrace_mod_load
*ftrace_mod
, *n
;
3924 struct list_head
*head
= enable
? &tr
->mod_trace
: &tr
->mod_notrace
;
3927 mutex_lock(&ftrace_lock
);
3929 /* We do not cache inverse filters */
3930 if (func
[0] == '!') {
3934 /* Look to remove this hash */
3935 list_for_each_entry_safe(ftrace_mod
, n
, head
, list
) {
3936 if (strcmp(ftrace_mod
->module
, module
) != 0)
3939 /* no func matches all */
3940 if (strcmp(func
, "*") == 0 ||
3941 (ftrace_mod
->func
&&
3942 strcmp(ftrace_mod
->func
, func
) == 0)) {
3944 free_ftrace_mod(ftrace_mod
);
3952 /* We only care about modules that have not been loaded yet */
3953 if (module_exists(module
))
3956 /* Save this string off, and execute it when the module is loaded */
3957 ret
= ftrace_add_mod(tr
, func
, module
, enable
);
3959 mutex_unlock(&ftrace_lock
);
3965 ftrace_set_regex(struct ftrace_ops
*ops
, unsigned char *buf
, int len
,
3966 int reset
, int enable
);
3968 #ifdef CONFIG_MODULES
3969 static void process_mod_list(struct list_head
*head
, struct ftrace_ops
*ops
,
3970 char *mod
, bool enable
)
3972 struct ftrace_mod_load
*ftrace_mod
, *n
;
3973 struct ftrace_hash
**orig_hash
, *new_hash
;
3974 LIST_HEAD(process_mods
);
3978 mutex_lock(&ops
->func_hash
->regex_lock
);
3981 orig_hash
= &ops
->func_hash
->filter_hash
;
3983 orig_hash
= &ops
->func_hash
->notrace_hash
;
3985 new_hash
= alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS
,
3988 goto out
; /* warn? */
3990 mutex_lock(&ftrace_lock
);
3992 list_for_each_entry_safe(ftrace_mod
, n
, head
, list
) {
3994 if (strcmp(ftrace_mod
->module
, mod
) != 0)
3997 if (ftrace_mod
->func
)
3998 func
= kstrdup(ftrace_mod
->func
, GFP_KERNEL
);
4000 func
= kstrdup("*", GFP_KERNEL
);
4002 if (!func
) /* warn? */
4005 list_del(&ftrace_mod
->list
);
4006 list_add(&ftrace_mod
->list
, &process_mods
);
4008 /* Use the newly allocated func, as it may be "*" */
4009 kfree(ftrace_mod
->func
);
4010 ftrace_mod
->func
= func
;
4013 mutex_unlock(&ftrace_lock
);
4015 list_for_each_entry_safe(ftrace_mod
, n
, &process_mods
, list
) {
4017 func
= ftrace_mod
->func
;
4019 /* Grabs ftrace_lock, which is why we have this extra step */
4020 match_records(new_hash
, func
, strlen(func
), mod
);
4021 free_ftrace_mod(ftrace_mod
);
4024 if (enable
&& list_empty(head
))
4025 new_hash
->flags
&= ~FTRACE_HASH_FL_MOD
;
4027 mutex_lock(&ftrace_lock
);
4029 ret
= ftrace_hash_move_and_update_ops(ops
, orig_hash
,
4031 mutex_unlock(&ftrace_lock
);
4034 mutex_unlock(&ops
->func_hash
->regex_lock
);
4036 free_ftrace_hash(new_hash
);
4039 static void process_cached_mods(const char *mod_name
)
4041 struct trace_array
*tr
;
4044 mod
= kstrdup(mod_name
, GFP_KERNEL
);
4048 mutex_lock(&trace_types_lock
);
4049 list_for_each_entry(tr
, &ftrace_trace_arrays
, list
) {
4050 if (!list_empty(&tr
->mod_trace
))
4051 process_mod_list(&tr
->mod_trace
, tr
->ops
, mod
, true);
4052 if (!list_empty(&tr
->mod_notrace
))
4053 process_mod_list(&tr
->mod_notrace
, tr
->ops
, mod
, false);
4055 mutex_unlock(&trace_types_lock
);
4062 * We register the module command as a template to show others how
4063 * to register the a command as well.
4067 ftrace_mod_callback(struct trace_array
*tr
, struct ftrace_hash
*hash
,
4068 char *func_orig
, char *cmd
, char *module
, int enable
)
4073 /* match_records() modifies func, and we need the original */
4074 func
= kstrdup(func_orig
, GFP_KERNEL
);
4079 * cmd == 'mod' because we only registered this func
4080 * for the 'mod' ftrace_func_command.
4081 * But if you register one func with multiple commands,
4082 * you can tell which command was used by the cmd
4085 ret
= match_records(hash
, func
, strlen(func
), module
);
4089 return cache_mod(tr
, func_orig
, module
, enable
);
4095 static struct ftrace_func_command ftrace_mod_cmd
= {
4097 .func
= ftrace_mod_callback
,
4100 static int __init
ftrace_mod_cmd_init(void)
4102 return register_ftrace_command(&ftrace_mod_cmd
);
4104 core_initcall(ftrace_mod_cmd_init
);
4106 static void function_trace_probe_call(unsigned long ip
, unsigned long parent_ip
,
4107 struct ftrace_ops
*op
, struct pt_regs
*pt_regs
)
4109 struct ftrace_probe_ops
*probe_ops
;
4110 struct ftrace_func_probe
*probe
;
4112 probe
= container_of(op
, struct ftrace_func_probe
, ops
);
4113 probe_ops
= probe
->probe_ops
;
4116 * Disable preemption for these calls to prevent a RCU grace
4117 * period. This syncs the hash iteration and freeing of items
4118 * on the hash. rcu_read_lock is too dangerous here.
4120 preempt_disable_notrace();
4121 probe_ops
->func(ip
, parent_ip
, probe
->tr
, probe_ops
, probe
->data
);
4122 preempt_enable_notrace();
4125 struct ftrace_func_map
{
4126 struct ftrace_func_entry entry
;
4130 struct ftrace_func_mapper
{
4131 struct ftrace_hash hash
;
4135 * allocate_ftrace_func_mapper - allocate a new ftrace_func_mapper
4137 * Returns a ftrace_func_mapper descriptor that can be used to map ips to data.
4139 struct ftrace_func_mapper
*allocate_ftrace_func_mapper(void)
4141 struct ftrace_hash
*hash
;
4144 * The mapper is simply a ftrace_hash, but since the entries
4145 * in the hash are not ftrace_func_entry type, we define it
4146 * as a separate structure.
4148 hash
= alloc_ftrace_hash(FTRACE_HASH_DEFAULT_BITS
);
4149 return (struct ftrace_func_mapper
*)hash
;
4153 * ftrace_func_mapper_find_ip - Find some data mapped to an ip
4154 * @mapper: The mapper that has the ip maps
4155 * @ip: the instruction pointer to find the data for
4157 * Returns the data mapped to @ip if found otherwise NULL. The return
4158 * is actually the address of the mapper data pointer. The address is
4159 * returned for use cases where the data is no bigger than a long, and
4160 * the user can use the data pointer as its data instead of having to
4161 * allocate more memory for the reference.
4163 void **ftrace_func_mapper_find_ip(struct ftrace_func_mapper
*mapper
,
4166 struct ftrace_func_entry
*entry
;
4167 struct ftrace_func_map
*map
;
4169 entry
= ftrace_lookup_ip(&mapper
->hash
, ip
);
4173 map
= (struct ftrace_func_map
*)entry
;
4178 * ftrace_func_mapper_add_ip - Map some data to an ip
4179 * @mapper: The mapper that has the ip maps
4180 * @ip: The instruction pointer address to map @data to
4181 * @data: The data to map to @ip
4183 * Returns 0 on succes otherwise an error.
4185 int ftrace_func_mapper_add_ip(struct ftrace_func_mapper
*mapper
,
4186 unsigned long ip
, void *data
)
4188 struct ftrace_func_entry
*entry
;
4189 struct ftrace_func_map
*map
;
4191 entry
= ftrace_lookup_ip(&mapper
->hash
, ip
);
4195 map
= kmalloc(sizeof(*map
), GFP_KERNEL
);
4202 __add_hash_entry(&mapper
->hash
, &map
->entry
);
4208 * ftrace_func_mapper_remove_ip - Remove an ip from the mapping
4209 * @mapper: The mapper that has the ip maps
4210 * @ip: The instruction pointer address to remove the data from
4212 * Returns the data if it is found, otherwise NULL.
4213 * Note, if the data pointer is used as the data itself, (see
4214 * ftrace_func_mapper_find_ip(), then the return value may be meaningless,
4215 * if the data pointer was set to zero.
4217 void *ftrace_func_mapper_remove_ip(struct ftrace_func_mapper
*mapper
,
4220 struct ftrace_func_entry
*entry
;
4221 struct ftrace_func_map
*map
;
4224 entry
= ftrace_lookup_ip(&mapper
->hash
, ip
);
4228 map
= (struct ftrace_func_map
*)entry
;
4231 remove_hash_entry(&mapper
->hash
, entry
);
4238 * free_ftrace_func_mapper - free a mapping of ips and data
4239 * @mapper: The mapper that has the ip maps
4240 * @free_func: A function to be called on each data item.
4242 * This is used to free the function mapper. The @free_func is optional
4243 * and can be used if the data needs to be freed as well.
4245 void free_ftrace_func_mapper(struct ftrace_func_mapper
*mapper
,
4246 ftrace_mapper_func free_func
)
4248 struct ftrace_func_entry
*entry
;
4249 struct ftrace_func_map
*map
;
4250 struct hlist_head
*hhd
;
4251 int size
= 1 << mapper
->hash
.size_bits
;
4254 if (free_func
&& mapper
->hash
.count
) {
4255 for (i
= 0; i
< size
; i
++) {
4256 hhd
= &mapper
->hash
.buckets
[i
];
4257 hlist_for_each_entry(entry
, hhd
, hlist
) {
4258 map
= (struct ftrace_func_map
*)entry
;
4263 free_ftrace_hash(&mapper
->hash
);
4266 static void release_probe(struct ftrace_func_probe
*probe
)
4268 struct ftrace_probe_ops
*probe_ops
;
4270 mutex_lock(&ftrace_lock
);
4272 WARN_ON(probe
->ref
<= 0);
4274 /* Subtract the ref that was used to protect this instance */
4278 probe_ops
= probe
->probe_ops
;
4280 * Sending zero as ip tells probe_ops to free
4281 * the probe->data itself
4283 if (probe_ops
->free
)
4284 probe_ops
->free(probe_ops
, probe
->tr
, 0, probe
->data
);
4285 list_del(&probe
->list
);
4288 mutex_unlock(&ftrace_lock
);
4291 static void acquire_probe_locked(struct ftrace_func_probe
*probe
)
4294 * Add one ref to keep it from being freed when releasing the
4295 * ftrace_lock mutex.
4301 register_ftrace_function_probe(char *glob
, struct trace_array
*tr
,
4302 struct ftrace_probe_ops
*probe_ops
,
4305 struct ftrace_func_entry
*entry
;
4306 struct ftrace_func_probe
*probe
;
4307 struct ftrace_hash
**orig_hash
;
4308 struct ftrace_hash
*old_hash
;
4309 struct ftrace_hash
*hash
;
4318 /* We do not support '!' for function probes */
4319 if (WARN_ON(glob
[0] == '!'))
4323 mutex_lock(&ftrace_lock
);
4324 /* Check if the probe_ops is already registered */
4325 list_for_each_entry(probe
, &tr
->func_probes
, list
) {
4326 if (probe
->probe_ops
== probe_ops
)
4329 if (&probe
->list
== &tr
->func_probes
) {
4330 probe
= kzalloc(sizeof(*probe
), GFP_KERNEL
);
4332 mutex_unlock(&ftrace_lock
);
4335 probe
->probe_ops
= probe_ops
;
4336 probe
->ops
.func
= function_trace_probe_call
;
4338 ftrace_ops_init(&probe
->ops
);
4339 list_add(&probe
->list
, &tr
->func_probes
);
4342 acquire_probe_locked(probe
);
4344 mutex_unlock(&ftrace_lock
);
4346 mutex_lock(&probe
->ops
.func_hash
->regex_lock
);
4348 orig_hash
= &probe
->ops
.func_hash
->filter_hash
;
4349 old_hash
= *orig_hash
;
4350 hash
= alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS
, old_hash
);
4352 ret
= ftrace_match_records(hash
, glob
, strlen(glob
));
4354 /* Nothing found? */
4361 size
= 1 << hash
->size_bits
;
4362 for (i
= 0; i
< size
; i
++) {
4363 hlist_for_each_entry(entry
, &hash
->buckets
[i
], hlist
) {
4364 if (ftrace_lookup_ip(old_hash
, entry
->ip
))
4367 * The caller might want to do something special
4368 * for each function we find. We call the callback
4369 * to give the caller an opportunity to do so.
4371 if (probe_ops
->init
) {
4372 ret
= probe_ops
->init(probe_ops
, tr
,
4376 if (probe_ops
->free
&& count
)
4377 probe_ops
->free(probe_ops
, tr
,
4387 mutex_lock(&ftrace_lock
);
4390 /* Nothing was added? */
4395 ret
= ftrace_hash_move_and_update_ops(&probe
->ops
, orig_hash
,
4400 /* One ref for each new function traced */
4401 probe
->ref
+= count
;
4403 if (!(probe
->ops
.flags
& FTRACE_OPS_FL_ENABLED
))
4404 ret
= ftrace_startup(&probe
->ops
, 0);
4407 mutex_unlock(&ftrace_lock
);
4412 mutex_unlock(&probe
->ops
.func_hash
->regex_lock
);
4413 free_ftrace_hash(hash
);
4415 release_probe(probe
);
4420 if (!probe_ops
->free
|| !count
)
4423 /* Failed to do the move, need to call the free functions */
4424 for (i
= 0; i
< size
; i
++) {
4425 hlist_for_each_entry(entry
, &hash
->buckets
[i
], hlist
) {
4426 if (ftrace_lookup_ip(old_hash
, entry
->ip
))
4428 probe_ops
->free(probe_ops
, tr
, entry
->ip
, probe
->data
);
4435 unregister_ftrace_function_probe_func(char *glob
, struct trace_array
*tr
,
4436 struct ftrace_probe_ops
*probe_ops
)
4438 struct ftrace_ops_hash old_hash_ops
;
4439 struct ftrace_func_entry
*entry
;
4440 struct ftrace_func_probe
*probe
;
4441 struct ftrace_glob func_g
;
4442 struct ftrace_hash
**orig_hash
;
4443 struct ftrace_hash
*old_hash
;
4444 struct ftrace_hash
*hash
= NULL
;
4445 struct hlist_node
*tmp
;
4446 struct hlist_head hhd
;
4447 char str
[KSYM_SYMBOL_LEN
];
4449 int i
, ret
= -ENODEV
;
4452 if (!glob
|| !strlen(glob
) || !strcmp(glob
, "*"))
4453 func_g
.search
= NULL
;
4457 func_g
.type
= filter_parse_regex(glob
, strlen(glob
),
4458 &func_g
.search
, ¬);
4459 func_g
.len
= strlen(func_g
.search
);
4461 /* we do not support '!' for function probes */
4466 mutex_lock(&ftrace_lock
);
4467 /* Check if the probe_ops is already registered */
4468 list_for_each_entry(probe
, &tr
->func_probes
, list
) {
4469 if (probe
->probe_ops
== probe_ops
)
4472 if (&probe
->list
== &tr
->func_probes
)
4473 goto err_unlock_ftrace
;
4476 if (!(probe
->ops
.flags
& FTRACE_OPS_FL_INITIALIZED
))
4477 goto err_unlock_ftrace
;
4479 acquire_probe_locked(probe
);
4481 mutex_unlock(&ftrace_lock
);
4483 mutex_lock(&probe
->ops
.func_hash
->regex_lock
);
4485 orig_hash
= &probe
->ops
.func_hash
->filter_hash
;
4486 old_hash
= *orig_hash
;
4488 if (ftrace_hash_empty(old_hash
))
4491 old_hash_ops
.filter_hash
= old_hash
;
4492 /* Probes only have filters */
4493 old_hash_ops
.notrace_hash
= NULL
;
4496 hash
= alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS
, old_hash
);
4500 INIT_HLIST_HEAD(&hhd
);
4502 size
= 1 << hash
->size_bits
;
4503 for (i
= 0; i
< size
; i
++) {
4504 hlist_for_each_entry_safe(entry
, tmp
, &hash
->buckets
[i
], hlist
) {
4506 if (func_g
.search
) {
4507 kallsyms_lookup(entry
->ip
, NULL
, NULL
,
4509 if (!ftrace_match(str
, &func_g
))
4513 remove_hash_entry(hash
, entry
);
4514 hlist_add_head(&entry
->hlist
, &hhd
);
4518 /* Nothing found? */
4524 mutex_lock(&ftrace_lock
);
4526 WARN_ON(probe
->ref
< count
);
4528 probe
->ref
-= count
;
4530 if (ftrace_hash_empty(hash
))
4531 ftrace_shutdown(&probe
->ops
, 0);
4533 ret
= ftrace_hash_move_and_update_ops(&probe
->ops
, orig_hash
,
4536 /* still need to update the function call sites */
4537 if (ftrace_enabled
&& !ftrace_hash_empty(hash
))
4538 ftrace_run_modify_code(&probe
->ops
, FTRACE_UPDATE_CALLS
,
4540 synchronize_sched();
4542 hlist_for_each_entry_safe(entry
, tmp
, &hhd
, hlist
) {
4543 hlist_del(&entry
->hlist
);
4544 if (probe_ops
->free
)
4545 probe_ops
->free(probe_ops
, tr
, entry
->ip
, probe
->data
);
4548 mutex_unlock(&ftrace_lock
);
4551 mutex_unlock(&probe
->ops
.func_hash
->regex_lock
);
4552 free_ftrace_hash(hash
);
4554 release_probe(probe
);
4559 mutex_unlock(&ftrace_lock
);
4563 void clear_ftrace_function_probes(struct trace_array
*tr
)
4565 struct ftrace_func_probe
*probe
, *n
;
4567 list_for_each_entry_safe(probe
, n
, &tr
->func_probes
, list
)
4568 unregister_ftrace_function_probe_func(NULL
, tr
, probe
->probe_ops
);
4571 static LIST_HEAD(ftrace_commands
);
4572 static DEFINE_MUTEX(ftrace_cmd_mutex
);
4575 * Currently we only register ftrace commands from __init, so mark this
4578 __init
int register_ftrace_command(struct ftrace_func_command
*cmd
)
4580 struct ftrace_func_command
*p
;
4583 mutex_lock(&ftrace_cmd_mutex
);
4584 list_for_each_entry(p
, &ftrace_commands
, list
) {
4585 if (strcmp(cmd
->name
, p
->name
) == 0) {
4590 list_add(&cmd
->list
, &ftrace_commands
);
4592 mutex_unlock(&ftrace_cmd_mutex
);
4598 * Currently we only unregister ftrace commands from __init, so mark
4601 __init
int unregister_ftrace_command(struct ftrace_func_command
*cmd
)
4603 struct ftrace_func_command
*p
, *n
;
4606 mutex_lock(&ftrace_cmd_mutex
);
4607 list_for_each_entry_safe(p
, n
, &ftrace_commands
, list
) {
4608 if (strcmp(cmd
->name
, p
->name
) == 0) {
4610 list_del_init(&p
->list
);
4615 mutex_unlock(&ftrace_cmd_mutex
);
4620 static int ftrace_process_regex(struct ftrace_iterator
*iter
,
4621 char *buff
, int len
, int enable
)
4623 struct ftrace_hash
*hash
= iter
->hash
;
4624 struct trace_array
*tr
= iter
->ops
->private;
4625 char *func
, *command
, *next
= buff
;
4626 struct ftrace_func_command
*p
;
4629 func
= strsep(&next
, ":");
4632 ret
= ftrace_match_records(hash
, func
, len
);
4642 command
= strsep(&next
, ":");
4644 mutex_lock(&ftrace_cmd_mutex
);
4645 list_for_each_entry(p
, &ftrace_commands
, list
) {
4646 if (strcmp(p
->name
, command
) == 0) {
4647 ret
= p
->func(tr
, hash
, func
, command
, next
, enable
);
4652 mutex_unlock(&ftrace_cmd_mutex
);
4658 ftrace_regex_write(struct file
*file
, const char __user
*ubuf
,
4659 size_t cnt
, loff_t
*ppos
, int enable
)
4661 struct ftrace_iterator
*iter
;
4662 struct trace_parser
*parser
;
4668 if (file
->f_mode
& FMODE_READ
) {
4669 struct seq_file
*m
= file
->private_data
;
4672 iter
= file
->private_data
;
4674 if (unlikely(ftrace_disabled
))
4677 /* iter->hash is a local copy, so we don't need regex_lock */
4679 parser
= &iter
->parser
;
4680 read
= trace_get_user(parser
, ubuf
, cnt
, ppos
);
4682 if (read
>= 0 && trace_parser_loaded(parser
) &&
4683 !trace_parser_cont(parser
)) {
4684 ret
= ftrace_process_regex(iter
, parser
->buffer
,
4685 parser
->idx
, enable
);
4686 trace_parser_clear(parser
);
4697 ftrace_filter_write(struct file
*file
, const char __user
*ubuf
,
4698 size_t cnt
, loff_t
*ppos
)
4700 return ftrace_regex_write(file
, ubuf
, cnt
, ppos
, 1);
4704 ftrace_notrace_write(struct file
*file
, const char __user
*ubuf
,
4705 size_t cnt
, loff_t
*ppos
)
4707 return ftrace_regex_write(file
, ubuf
, cnt
, ppos
, 0);
4711 ftrace_match_addr(struct ftrace_hash
*hash
, unsigned long ip
, int remove
)
4713 struct ftrace_func_entry
*entry
;
4715 if (!ftrace_location(ip
))
4719 entry
= ftrace_lookup_ip(hash
, ip
);
4722 free_hash_entry(hash
, entry
);
4726 return add_hash_entry(hash
, ip
);
4730 ftrace_set_hash(struct ftrace_ops
*ops
, unsigned char *buf
, int len
,
4731 unsigned long ip
, int remove
, int reset
, int enable
)
4733 struct ftrace_hash
**orig_hash
;
4734 struct ftrace_hash
*hash
;
4737 if (unlikely(ftrace_disabled
))
4740 mutex_lock(&ops
->func_hash
->regex_lock
);
4743 orig_hash
= &ops
->func_hash
->filter_hash
;
4745 orig_hash
= &ops
->func_hash
->notrace_hash
;
4748 hash
= alloc_ftrace_hash(FTRACE_HASH_DEFAULT_BITS
);
4750 hash
= alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS
, *orig_hash
);
4754 goto out_regex_unlock
;
4757 if (buf
&& !ftrace_match_records(hash
, buf
, len
)) {
4759 goto out_regex_unlock
;
4762 ret
= ftrace_match_addr(hash
, ip
, remove
);
4764 goto out_regex_unlock
;
4767 mutex_lock(&ftrace_lock
);
4768 ret
= ftrace_hash_move_and_update_ops(ops
, orig_hash
, hash
, enable
);
4769 mutex_unlock(&ftrace_lock
);
4772 mutex_unlock(&ops
->func_hash
->regex_lock
);
4774 free_ftrace_hash(hash
);
4779 ftrace_set_addr(struct ftrace_ops
*ops
, unsigned long ip
, int remove
,
4780 int reset
, int enable
)
4782 return ftrace_set_hash(ops
, 0, 0, ip
, remove
, reset
, enable
);
4786 * ftrace_set_filter_ip - set a function to filter on in ftrace by address
4787 * @ops - the ops to set the filter with
4788 * @ip - the address to add to or remove from the filter.
4789 * @remove - non zero to remove the ip from the filter
4790 * @reset - non zero to reset all filters before applying this filter.
4792 * Filters denote which functions should be enabled when tracing is enabled
4793 * If @ip is NULL, it failes to update filter.
4795 int ftrace_set_filter_ip(struct ftrace_ops
*ops
, unsigned long ip
,
4796 int remove
, int reset
)
4798 ftrace_ops_init(ops
);
4799 return ftrace_set_addr(ops
, ip
, remove
, reset
, 1);
4801 EXPORT_SYMBOL_GPL(ftrace_set_filter_ip
);
4804 * ftrace_ops_set_global_filter - setup ops to use global filters
4805 * @ops - the ops which will use the global filters
4807 * ftrace users who need global function trace filtering should call this.
4808 * It can set the global filter only if ops were not initialized before.
4810 void ftrace_ops_set_global_filter(struct ftrace_ops
*ops
)
4812 if (ops
->flags
& FTRACE_OPS_FL_INITIALIZED
)
4815 ftrace_ops_init(ops
);
4816 ops
->func_hash
= &global_ops
.local_hash
;
4818 EXPORT_SYMBOL_GPL(ftrace_ops_set_global_filter
);
4821 ftrace_set_regex(struct ftrace_ops
*ops
, unsigned char *buf
, int len
,
4822 int reset
, int enable
)
4824 return ftrace_set_hash(ops
, buf
, len
, 0, 0, reset
, enable
);
4828 * ftrace_set_filter - set a function to filter on in ftrace
4829 * @ops - the ops to set the filter with
4830 * @buf - the string that holds the function filter text.
4831 * @len - the length of the string.
4832 * @reset - non zero to reset all filters before applying this filter.
4834 * Filters denote which functions should be enabled when tracing is enabled.
4835 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
4837 int ftrace_set_filter(struct ftrace_ops
*ops
, unsigned char *buf
,
4840 ftrace_ops_init(ops
);
4841 return ftrace_set_regex(ops
, buf
, len
, reset
, 1);
4843 EXPORT_SYMBOL_GPL(ftrace_set_filter
);
4846 * ftrace_set_notrace - set a function to not trace in ftrace
4847 * @ops - the ops to set the notrace filter with
4848 * @buf - the string that holds the function notrace text.
4849 * @len - the length of the string.
4850 * @reset - non zero to reset all filters before applying this filter.
4852 * Notrace Filters denote which functions should not be enabled when tracing
4853 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
4856 int ftrace_set_notrace(struct ftrace_ops
*ops
, unsigned char *buf
,
4859 ftrace_ops_init(ops
);
4860 return ftrace_set_regex(ops
, buf
, len
, reset
, 0);
4862 EXPORT_SYMBOL_GPL(ftrace_set_notrace
);
4864 * ftrace_set_global_filter - set a function to filter on with global tracers
4865 * @buf - the string that holds the function filter text.
4866 * @len - the length of the string.
4867 * @reset - non zero to reset all filters before applying this filter.
4869 * Filters denote which functions should be enabled when tracing is enabled.
4870 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
4872 void ftrace_set_global_filter(unsigned char *buf
, int len
, int reset
)
4874 ftrace_set_regex(&global_ops
, buf
, len
, reset
, 1);
4876 EXPORT_SYMBOL_GPL(ftrace_set_global_filter
);
4879 * ftrace_set_global_notrace - set a function to not trace with global tracers
4880 * @buf - the string that holds the function notrace text.
4881 * @len - the length of the string.
4882 * @reset - non zero to reset all filters before applying this filter.
4884 * Notrace Filters denote which functions should not be enabled when tracing
4885 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
4888 void ftrace_set_global_notrace(unsigned char *buf
, int len
, int reset
)
4890 ftrace_set_regex(&global_ops
, buf
, len
, reset
, 0);
4892 EXPORT_SYMBOL_GPL(ftrace_set_global_notrace
);
4895 * command line interface to allow users to set filters on boot up.
4897 #define FTRACE_FILTER_SIZE COMMAND_LINE_SIZE
4898 static char ftrace_notrace_buf
[FTRACE_FILTER_SIZE
] __initdata
;
4899 static char ftrace_filter_buf
[FTRACE_FILTER_SIZE
] __initdata
;
4901 /* Used by function selftest to not test if filter is set */
4902 bool ftrace_filter_param __initdata
;
4904 static int __init
set_ftrace_notrace(char *str
)
4906 ftrace_filter_param
= true;
4907 strlcpy(ftrace_notrace_buf
, str
, FTRACE_FILTER_SIZE
);
4910 __setup("ftrace_notrace=", set_ftrace_notrace
);
4912 static int __init
set_ftrace_filter(char *str
)
4914 ftrace_filter_param
= true;
4915 strlcpy(ftrace_filter_buf
, str
, FTRACE_FILTER_SIZE
);
4918 __setup("ftrace_filter=", set_ftrace_filter
);
4920 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
4921 static char ftrace_graph_buf
[FTRACE_FILTER_SIZE
] __initdata
;
4922 static char ftrace_graph_notrace_buf
[FTRACE_FILTER_SIZE
] __initdata
;
4923 static int ftrace_graph_set_hash(struct ftrace_hash
*hash
, char *buffer
);
4925 static int __init
set_graph_function(char *str
)
4927 strlcpy(ftrace_graph_buf
, str
, FTRACE_FILTER_SIZE
);
4930 __setup("ftrace_graph_filter=", set_graph_function
);
4932 static int __init
set_graph_notrace_function(char *str
)
4934 strlcpy(ftrace_graph_notrace_buf
, str
, FTRACE_FILTER_SIZE
);
4937 __setup("ftrace_graph_notrace=", set_graph_notrace_function
);
4939 static int __init
set_graph_max_depth_function(char *str
)
4943 fgraph_max_depth
= simple_strtoul(str
, NULL
, 0);
4946 __setup("ftrace_graph_max_depth=", set_graph_max_depth_function
);
4948 static void __init
set_ftrace_early_graph(char *buf
, int enable
)
4952 struct ftrace_hash
*hash
;
4954 hash
= alloc_ftrace_hash(FTRACE_HASH_DEFAULT_BITS
);
4959 func
= strsep(&buf
, ",");
4960 /* we allow only one expression at a time */
4961 ret
= ftrace_graph_set_hash(hash
, func
);
4963 printk(KERN_DEBUG
"ftrace: function %s not "
4964 "traceable\n", func
);
4968 ftrace_graph_hash
= hash
;
4970 ftrace_graph_notrace_hash
= hash
;
4972 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
4975 ftrace_set_early_filter(struct ftrace_ops
*ops
, char *buf
, int enable
)
4979 ftrace_ops_init(ops
);
4982 func
= strsep(&buf
, ",");
4983 ftrace_set_regex(ops
, func
, strlen(func
), 0, enable
);
4987 static void __init
set_ftrace_early_filters(void)
4989 if (ftrace_filter_buf
[0])
4990 ftrace_set_early_filter(&global_ops
, ftrace_filter_buf
, 1);
4991 if (ftrace_notrace_buf
[0])
4992 ftrace_set_early_filter(&global_ops
, ftrace_notrace_buf
, 0);
4993 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
4994 if (ftrace_graph_buf
[0])
4995 set_ftrace_early_graph(ftrace_graph_buf
, 1);
4996 if (ftrace_graph_notrace_buf
[0])
4997 set_ftrace_early_graph(ftrace_graph_notrace_buf
, 0);
4998 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
5001 int ftrace_regex_release(struct inode
*inode
, struct file
*file
)
5003 struct seq_file
*m
= (struct seq_file
*)file
->private_data
;
5004 struct ftrace_iterator
*iter
;
5005 struct ftrace_hash
**orig_hash
;
5006 struct trace_parser
*parser
;
5010 if (file
->f_mode
& FMODE_READ
) {
5012 seq_release(inode
, file
);
5014 iter
= file
->private_data
;
5016 parser
= &iter
->parser
;
5017 if (trace_parser_loaded(parser
)) {
5018 parser
->buffer
[parser
->idx
] = 0;
5019 ftrace_match_records(iter
->hash
, parser
->buffer
, parser
->idx
);
5022 trace_parser_put(parser
);
5024 mutex_lock(&iter
->ops
->func_hash
->regex_lock
);
5026 if (file
->f_mode
& FMODE_WRITE
) {
5027 filter_hash
= !!(iter
->flags
& FTRACE_ITER_FILTER
);
5030 orig_hash
= &iter
->ops
->func_hash
->filter_hash
;
5031 if (iter
->tr
&& !list_empty(&iter
->tr
->mod_trace
))
5032 iter
->hash
->flags
|= FTRACE_HASH_FL_MOD
;
5034 orig_hash
= &iter
->ops
->func_hash
->notrace_hash
;
5036 mutex_lock(&ftrace_lock
);
5037 ret
= ftrace_hash_move_and_update_ops(iter
->ops
, orig_hash
,
5038 iter
->hash
, filter_hash
);
5039 mutex_unlock(&ftrace_lock
);
5041 /* For read only, the hash is the ops hash */
5045 mutex_unlock(&iter
->ops
->func_hash
->regex_lock
);
5046 free_ftrace_hash(iter
->hash
);
5052 static const struct file_operations ftrace_avail_fops
= {
5053 .open
= ftrace_avail_open
,
5055 .llseek
= seq_lseek
,
5056 .release
= seq_release_private
,
5059 static const struct file_operations ftrace_enabled_fops
= {
5060 .open
= ftrace_enabled_open
,
5062 .llseek
= seq_lseek
,
5063 .release
= seq_release_private
,
5066 static const struct file_operations ftrace_filter_fops
= {
5067 .open
= ftrace_filter_open
,
5069 .write
= ftrace_filter_write
,
5070 .llseek
= tracing_lseek
,
5071 .release
= ftrace_regex_release
,
5074 static const struct file_operations ftrace_notrace_fops
= {
5075 .open
= ftrace_notrace_open
,
5077 .write
= ftrace_notrace_write
,
5078 .llseek
= tracing_lseek
,
5079 .release
= ftrace_regex_release
,
5082 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
5084 static DEFINE_MUTEX(graph_lock
);
5086 struct ftrace_hash
*ftrace_graph_hash
= EMPTY_HASH
;
5087 struct ftrace_hash
*ftrace_graph_notrace_hash
= EMPTY_HASH
;
5089 enum graph_filter_type
{
5090 GRAPH_FILTER_NOTRACE
= 0,
5091 GRAPH_FILTER_FUNCTION
,
5094 #define FTRACE_GRAPH_EMPTY ((void *)1)
5096 struct ftrace_graph_data
{
5097 struct ftrace_hash
*hash
;
5098 struct ftrace_func_entry
*entry
;
5099 int idx
; /* for hash table iteration */
5100 enum graph_filter_type type
;
5101 struct ftrace_hash
*new_hash
;
5102 const struct seq_operations
*seq_ops
;
5103 struct trace_parser parser
;
5107 __g_next(struct seq_file
*m
, loff_t
*pos
)
5109 struct ftrace_graph_data
*fgd
= m
->private;
5110 struct ftrace_func_entry
*entry
= fgd
->entry
;
5111 struct hlist_head
*head
;
5112 int i
, idx
= fgd
->idx
;
5114 if (*pos
>= fgd
->hash
->count
)
5118 hlist_for_each_entry_continue(entry
, hlist
) {
5126 for (i
= idx
; i
< 1 << fgd
->hash
->size_bits
; i
++) {
5127 head
= &fgd
->hash
->buckets
[i
];
5128 hlist_for_each_entry(entry
, head
, hlist
) {
5138 g_next(struct seq_file
*m
, void *v
, loff_t
*pos
)
5141 return __g_next(m
, pos
);
5144 static void *g_start(struct seq_file
*m
, loff_t
*pos
)
5146 struct ftrace_graph_data
*fgd
= m
->private;
5148 mutex_lock(&graph_lock
);
5150 if (fgd
->type
== GRAPH_FILTER_FUNCTION
)
5151 fgd
->hash
= rcu_dereference_protected(ftrace_graph_hash
,
5152 lockdep_is_held(&graph_lock
));
5154 fgd
->hash
= rcu_dereference_protected(ftrace_graph_notrace_hash
,
5155 lockdep_is_held(&graph_lock
));
5157 /* Nothing, tell g_show to print all functions are enabled */
5158 if (ftrace_hash_empty(fgd
->hash
) && !*pos
)
5159 return FTRACE_GRAPH_EMPTY
;
5163 return __g_next(m
, pos
);
5166 static void g_stop(struct seq_file
*m
, void *p
)
5168 mutex_unlock(&graph_lock
);
5171 static int g_show(struct seq_file
*m
, void *v
)
5173 struct ftrace_func_entry
*entry
= v
;
5178 if (entry
== FTRACE_GRAPH_EMPTY
) {
5179 struct ftrace_graph_data
*fgd
= m
->private;
5181 if (fgd
->type
== GRAPH_FILTER_FUNCTION
)
5182 seq_puts(m
, "#### all functions enabled ####\n");
5184 seq_puts(m
, "#### no functions disabled ####\n");
5188 seq_printf(m
, "%ps\n", (void *)entry
->ip
);
5193 static const struct seq_operations ftrace_graph_seq_ops
= {
5201 __ftrace_graph_open(struct inode
*inode
, struct file
*file
,
5202 struct ftrace_graph_data
*fgd
)
5205 struct ftrace_hash
*new_hash
= NULL
;
5207 if (file
->f_mode
& FMODE_WRITE
) {
5208 const int size_bits
= FTRACE_HASH_DEFAULT_BITS
;
5210 if (trace_parser_get_init(&fgd
->parser
, FTRACE_BUFF_MAX
))
5213 if (file
->f_flags
& O_TRUNC
)
5214 new_hash
= alloc_ftrace_hash(size_bits
);
5216 new_hash
= alloc_and_copy_ftrace_hash(size_bits
,
5224 if (file
->f_mode
& FMODE_READ
) {
5225 ret
= seq_open(file
, &ftrace_graph_seq_ops
);
5227 struct seq_file
*m
= file
->private_data
;
5231 free_ftrace_hash(new_hash
);
5235 file
->private_data
= fgd
;
5238 if (ret
< 0 && file
->f_mode
& FMODE_WRITE
)
5239 trace_parser_put(&fgd
->parser
);
5241 fgd
->new_hash
= new_hash
;
5244 * All uses of fgd->hash must be taken with the graph_lock
5245 * held. The graph_lock is going to be released, so force
5246 * fgd->hash to be reinitialized when it is taken again.
5254 ftrace_graph_open(struct inode
*inode
, struct file
*file
)
5256 struct ftrace_graph_data
*fgd
;
5259 if (unlikely(ftrace_disabled
))
5262 fgd
= kmalloc(sizeof(*fgd
), GFP_KERNEL
);
5266 mutex_lock(&graph_lock
);
5268 fgd
->hash
= rcu_dereference_protected(ftrace_graph_hash
,
5269 lockdep_is_held(&graph_lock
));
5270 fgd
->type
= GRAPH_FILTER_FUNCTION
;
5271 fgd
->seq_ops
= &ftrace_graph_seq_ops
;
5273 ret
= __ftrace_graph_open(inode
, file
, fgd
);
5277 mutex_unlock(&graph_lock
);
5282 ftrace_graph_notrace_open(struct inode
*inode
, struct file
*file
)
5284 struct ftrace_graph_data
*fgd
;
5287 if (unlikely(ftrace_disabled
))
5290 fgd
= kmalloc(sizeof(*fgd
), GFP_KERNEL
);
5294 mutex_lock(&graph_lock
);
5296 fgd
->hash
= rcu_dereference_protected(ftrace_graph_notrace_hash
,
5297 lockdep_is_held(&graph_lock
));
5298 fgd
->type
= GRAPH_FILTER_NOTRACE
;
5299 fgd
->seq_ops
= &ftrace_graph_seq_ops
;
5301 ret
= __ftrace_graph_open(inode
, file
, fgd
);
5305 mutex_unlock(&graph_lock
);
5310 ftrace_graph_release(struct inode
*inode
, struct file
*file
)
5312 struct ftrace_graph_data
*fgd
;
5313 struct ftrace_hash
*old_hash
, *new_hash
;
5314 struct trace_parser
*parser
;
5317 if (file
->f_mode
& FMODE_READ
) {
5318 struct seq_file
*m
= file
->private_data
;
5321 seq_release(inode
, file
);
5323 fgd
= file
->private_data
;
5327 if (file
->f_mode
& FMODE_WRITE
) {
5329 parser
= &fgd
->parser
;
5331 if (trace_parser_loaded((parser
))) {
5332 parser
->buffer
[parser
->idx
] = 0;
5333 ret
= ftrace_graph_set_hash(fgd
->new_hash
,
5337 trace_parser_put(parser
);
5339 new_hash
= __ftrace_hash_move(fgd
->new_hash
);
5345 mutex_lock(&graph_lock
);
5347 if (fgd
->type
== GRAPH_FILTER_FUNCTION
) {
5348 old_hash
= rcu_dereference_protected(ftrace_graph_hash
,
5349 lockdep_is_held(&graph_lock
));
5350 rcu_assign_pointer(ftrace_graph_hash
, new_hash
);
5352 old_hash
= rcu_dereference_protected(ftrace_graph_notrace_hash
,
5353 lockdep_is_held(&graph_lock
));
5354 rcu_assign_pointer(ftrace_graph_notrace_hash
, new_hash
);
5357 mutex_unlock(&graph_lock
);
5359 /* Wait till all users are no longer using the old hash */
5360 synchronize_sched();
5362 free_ftrace_hash(old_hash
);
5366 free_ftrace_hash(fgd
->new_hash
);
5373 ftrace_graph_set_hash(struct ftrace_hash
*hash
, char *buffer
)
5375 struct ftrace_glob func_g
;
5376 struct dyn_ftrace
*rec
;
5377 struct ftrace_page
*pg
;
5378 struct ftrace_func_entry
*entry
;
5383 func_g
.type
= filter_parse_regex(buffer
, strlen(buffer
),
5384 &func_g
.search
, ¬);
5386 func_g
.len
= strlen(func_g
.search
);
5388 mutex_lock(&ftrace_lock
);
5390 if (unlikely(ftrace_disabled
)) {
5391 mutex_unlock(&ftrace_lock
);
5395 do_for_each_ftrace_rec(pg
, rec
) {
5397 if (rec
->flags
& FTRACE_FL_DISABLED
)
5400 if (ftrace_match_record(rec
, &func_g
, NULL
, 0)) {
5401 entry
= ftrace_lookup_ip(hash
, rec
->ip
);
5408 if (add_hash_entry(hash
, rec
->ip
) < 0)
5412 free_hash_entry(hash
, entry
);
5417 } while_for_each_ftrace_rec();
5419 mutex_unlock(&ftrace_lock
);
5428 ftrace_graph_write(struct file
*file
, const char __user
*ubuf
,
5429 size_t cnt
, loff_t
*ppos
)
5431 ssize_t read
, ret
= 0;
5432 struct ftrace_graph_data
*fgd
= file
->private_data
;
5433 struct trace_parser
*parser
;
5438 /* Read mode uses seq functions */
5439 if (file
->f_mode
& FMODE_READ
) {
5440 struct seq_file
*m
= file
->private_data
;
5444 parser
= &fgd
->parser
;
5446 read
= trace_get_user(parser
, ubuf
, cnt
, ppos
);
5448 if (read
>= 0 && trace_parser_loaded(parser
) &&
5449 !trace_parser_cont(parser
)) {
5451 ret
= ftrace_graph_set_hash(fgd
->new_hash
,
5453 trace_parser_clear(parser
);
5462 static const struct file_operations ftrace_graph_fops
= {
5463 .open
= ftrace_graph_open
,
5465 .write
= ftrace_graph_write
,
5466 .llseek
= tracing_lseek
,
5467 .release
= ftrace_graph_release
,
5470 static const struct file_operations ftrace_graph_notrace_fops
= {
5471 .open
= ftrace_graph_notrace_open
,
5473 .write
= ftrace_graph_write
,
5474 .llseek
= tracing_lseek
,
5475 .release
= ftrace_graph_release
,
5477 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
5479 void ftrace_create_filter_files(struct ftrace_ops
*ops
,
5480 struct dentry
*parent
)
5483 trace_create_file("set_ftrace_filter", 0644, parent
,
5484 ops
, &ftrace_filter_fops
);
5486 trace_create_file("set_ftrace_notrace", 0644, parent
,
5487 ops
, &ftrace_notrace_fops
);
5491 * The name "destroy_filter_files" is really a misnomer. Although
5492 * in the future, it may actualy delete the files, but this is
5493 * really intended to make sure the ops passed in are disabled
5494 * and that when this function returns, the caller is free to
5497 * The "destroy" name is only to match the "create" name that this
5498 * should be paired with.
5500 void ftrace_destroy_filter_files(struct ftrace_ops
*ops
)
5502 mutex_lock(&ftrace_lock
);
5503 if (ops
->flags
& FTRACE_OPS_FL_ENABLED
)
5504 ftrace_shutdown(ops
, 0);
5505 ops
->flags
|= FTRACE_OPS_FL_DELETED
;
5506 ftrace_free_filter(ops
);
5507 mutex_unlock(&ftrace_lock
);
5510 static __init
int ftrace_init_dyn_tracefs(struct dentry
*d_tracer
)
5513 trace_create_file("available_filter_functions", 0444,
5514 d_tracer
, NULL
, &ftrace_avail_fops
);
5516 trace_create_file("enabled_functions", 0444,
5517 d_tracer
, NULL
, &ftrace_enabled_fops
);
5519 ftrace_create_filter_files(&global_ops
, d_tracer
);
5521 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
5522 trace_create_file("set_graph_function", 0444, d_tracer
,
5524 &ftrace_graph_fops
);
5525 trace_create_file("set_graph_notrace", 0444, d_tracer
,
5527 &ftrace_graph_notrace_fops
);
5528 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
5533 static int ftrace_cmp_ips(const void *a
, const void *b
)
5535 const unsigned long *ipa
= a
;
5536 const unsigned long *ipb
= b
;
5545 static int ftrace_process_locs(struct module
*mod
,
5546 unsigned long *start
,
5549 struct ftrace_page
*start_pg
;
5550 struct ftrace_page
*pg
;
5551 struct dyn_ftrace
*rec
;
5552 unsigned long count
;
5555 unsigned long flags
= 0; /* Shut up gcc */
5558 count
= end
- start
;
5563 sort(start
, count
, sizeof(*start
),
5564 ftrace_cmp_ips
, NULL
);
5566 start_pg
= ftrace_allocate_pages(count
);
5570 mutex_lock(&ftrace_lock
);
5573 * Core and each module needs their own pages, as
5574 * modules will free them when they are removed.
5575 * Force a new page to be allocated for modules.
5578 WARN_ON(ftrace_pages
|| ftrace_pages_start
);
5579 /* First initialization */
5580 ftrace_pages
= ftrace_pages_start
= start_pg
;
5585 if (WARN_ON(ftrace_pages
->next
)) {
5586 /* Hmm, we have free pages? */
5587 while (ftrace_pages
->next
)
5588 ftrace_pages
= ftrace_pages
->next
;
5591 ftrace_pages
->next
= start_pg
;
5597 addr
= ftrace_call_adjust(*p
++);
5599 * Some architecture linkers will pad between
5600 * the different mcount_loc sections of different
5601 * object files to satisfy alignments.
5602 * Skip any NULL pointers.
5607 if (pg
->index
== pg
->size
) {
5608 /* We should have allocated enough */
5609 if (WARN_ON(!pg
->next
))
5614 rec
= &pg
->records
[pg
->index
++];
5618 /* We should have used all pages */
5621 /* Assign the last page to ftrace_pages */
5625 * We only need to disable interrupts on start up
5626 * because we are modifying code that an interrupt
5627 * may execute, and the modification is not atomic.
5628 * But for modules, nothing runs the code we modify
5629 * until we are finished with it, and there's no
5630 * reason to cause large interrupt latencies while we do it.
5633 local_irq_save(flags
);
5634 ftrace_update_code(mod
, start_pg
);
5636 local_irq_restore(flags
);
5639 mutex_unlock(&ftrace_lock
);
5644 struct ftrace_mod_func
{
5645 struct list_head list
;
5651 struct ftrace_mod_map
{
5652 struct rcu_head rcu
;
5653 struct list_head list
;
5655 unsigned long start_addr
;
5656 unsigned long end_addr
;
5657 struct list_head funcs
;
5658 unsigned int num_funcs
;
5661 #ifdef CONFIG_MODULES
5663 #define next_to_ftrace_page(p) container_of(p, struct ftrace_page, next)
5665 static LIST_HEAD(ftrace_mod_maps
);
5667 static int referenced_filters(struct dyn_ftrace
*rec
)
5669 struct ftrace_ops
*ops
;
5672 for (ops
= ftrace_ops_list
; ops
!= &ftrace_list_end
; ops
= ops
->next
) {
5673 if (ops_references_rec(ops
, rec
))
5681 clear_mod_from_hash(struct ftrace_page
*pg
, struct ftrace_hash
*hash
)
5683 struct ftrace_func_entry
*entry
;
5684 struct dyn_ftrace
*rec
;
5687 if (ftrace_hash_empty(hash
))
5690 for (i
= 0; i
< pg
->index
; i
++) {
5691 rec
= &pg
->records
[i
];
5692 entry
= __ftrace_lookup_ip(hash
, rec
->ip
);
5694 * Do not allow this rec to match again.
5695 * Yeah, it may waste some memory, but will be removed
5696 * if/when the hash is modified again.
5703 /* Clear any records from hashs */
5704 static void clear_mod_from_hashes(struct ftrace_page
*pg
)
5706 struct trace_array
*tr
;
5708 mutex_lock(&trace_types_lock
);
5709 list_for_each_entry(tr
, &ftrace_trace_arrays
, list
) {
5710 if (!tr
->ops
|| !tr
->ops
->func_hash
)
5712 mutex_lock(&tr
->ops
->func_hash
->regex_lock
);
5713 clear_mod_from_hash(pg
, tr
->ops
->func_hash
->filter_hash
);
5714 clear_mod_from_hash(pg
, tr
->ops
->func_hash
->notrace_hash
);
5715 mutex_unlock(&tr
->ops
->func_hash
->regex_lock
);
5717 mutex_unlock(&trace_types_lock
);
5720 static void ftrace_free_mod_map(struct rcu_head
*rcu
)
5722 struct ftrace_mod_map
*mod_map
= container_of(rcu
, struct ftrace_mod_map
, rcu
);
5723 struct ftrace_mod_func
*mod_func
;
5724 struct ftrace_mod_func
*n
;
5726 /* All the contents of mod_map are now not visible to readers */
5727 list_for_each_entry_safe(mod_func
, n
, &mod_map
->funcs
, list
) {
5728 kfree(mod_func
->name
);
5729 list_del(&mod_func
->list
);
5736 void ftrace_release_mod(struct module
*mod
)
5738 struct ftrace_mod_map
*mod_map
;
5739 struct ftrace_mod_map
*n
;
5740 struct dyn_ftrace
*rec
;
5741 struct ftrace_page
**last_pg
;
5742 struct ftrace_page
*tmp_page
= NULL
;
5743 struct ftrace_page
*pg
;
5746 mutex_lock(&ftrace_lock
);
5748 if (ftrace_disabled
)
5751 list_for_each_entry_safe(mod_map
, n
, &ftrace_mod_maps
, list
) {
5752 if (mod_map
->mod
== mod
) {
5753 list_del_rcu(&mod_map
->list
);
5754 call_rcu_sched(&mod_map
->rcu
, ftrace_free_mod_map
);
5760 * Each module has its own ftrace_pages, remove
5761 * them from the list.
5763 last_pg
= &ftrace_pages_start
;
5764 for (pg
= ftrace_pages_start
; pg
; pg
= *last_pg
) {
5765 rec
= &pg
->records
[0];
5766 if (within_module_core(rec
->ip
, mod
) ||
5767 within_module_init(rec
->ip
, mod
)) {
5769 * As core pages are first, the first
5770 * page should never be a module page.
5772 if (WARN_ON(pg
== ftrace_pages_start
))
5775 /* Check if we are deleting the last page */
5776 if (pg
== ftrace_pages
)
5777 ftrace_pages
= next_to_ftrace_page(last_pg
);
5779 ftrace_update_tot_cnt
-= pg
->index
;
5780 *last_pg
= pg
->next
;
5782 pg
->next
= tmp_page
;
5785 last_pg
= &pg
->next
;
5788 mutex_unlock(&ftrace_lock
);
5790 for (pg
= tmp_page
; pg
; pg
= tmp_page
) {
5792 /* Needs to be called outside of ftrace_lock */
5793 clear_mod_from_hashes(pg
);
5795 order
= get_count_order(pg
->size
/ ENTRIES_PER_PAGE
);
5796 free_pages((unsigned long)pg
->records
, order
);
5797 tmp_page
= pg
->next
;
5802 void ftrace_module_enable(struct module
*mod
)
5804 struct dyn_ftrace
*rec
;
5805 struct ftrace_page
*pg
;
5807 mutex_lock(&ftrace_lock
);
5809 if (ftrace_disabled
)
5813 * If the tracing is enabled, go ahead and enable the record.
5815 * The reason not to enable the record immediatelly is the
5816 * inherent check of ftrace_make_nop/ftrace_make_call for
5817 * correct previous instructions. Making first the NOP
5818 * conversion puts the module to the correct state, thus
5819 * passing the ftrace_make_call check.
5821 * We also delay this to after the module code already set the
5822 * text to read-only, as we now need to set it back to read-write
5823 * so that we can modify the text.
5825 if (ftrace_start_up
)
5826 ftrace_arch_code_modify_prepare();
5828 do_for_each_ftrace_rec(pg
, rec
) {
5831 * do_for_each_ftrace_rec() is a double loop.
5832 * module text shares the pg. If a record is
5833 * not part of this module, then skip this pg,
5834 * which the "break" will do.
5836 if (!within_module_core(rec
->ip
, mod
) &&
5837 !within_module_init(rec
->ip
, mod
))
5843 * When adding a module, we need to check if tracers are
5844 * currently enabled and if they are, and can trace this record,
5845 * we need to enable the module functions as well as update the
5846 * reference counts for those function records.
5848 if (ftrace_start_up
)
5849 cnt
+= referenced_filters(rec
);
5851 /* This clears FTRACE_FL_DISABLED */
5854 if (ftrace_start_up
&& cnt
) {
5855 int failed
= __ftrace_replace_code(rec
, 1);
5857 ftrace_bug(failed
, rec
);
5862 } while_for_each_ftrace_rec();
5865 if (ftrace_start_up
)
5866 ftrace_arch_code_modify_post_process();
5869 mutex_unlock(&ftrace_lock
);
5871 process_cached_mods(mod
->name
);
5874 void ftrace_module_init(struct module
*mod
)
5876 if (ftrace_disabled
|| !mod
->num_ftrace_callsites
)
5879 ftrace_process_locs(mod
, mod
->ftrace_callsites
,
5880 mod
->ftrace_callsites
+ mod
->num_ftrace_callsites
);
5883 static void save_ftrace_mod_rec(struct ftrace_mod_map
*mod_map
,
5884 struct dyn_ftrace
*rec
)
5886 struct ftrace_mod_func
*mod_func
;
5887 unsigned long symsize
;
5888 unsigned long offset
;
5889 char str
[KSYM_SYMBOL_LEN
];
5893 ret
= kallsyms_lookup(rec
->ip
, &symsize
, &offset
, &modname
, str
);
5897 mod_func
= kmalloc(sizeof(*mod_func
), GFP_KERNEL
);
5901 mod_func
->name
= kstrdup(str
, GFP_KERNEL
);
5902 if (!mod_func
->name
) {
5907 mod_func
->ip
= rec
->ip
- offset
;
5908 mod_func
->size
= symsize
;
5910 mod_map
->num_funcs
++;
5912 list_add_rcu(&mod_func
->list
, &mod_map
->funcs
);
5915 static struct ftrace_mod_map
*
5916 allocate_ftrace_mod_map(struct module
*mod
,
5917 unsigned long start
, unsigned long end
)
5919 struct ftrace_mod_map
*mod_map
;
5921 mod_map
= kmalloc(sizeof(*mod_map
), GFP_KERNEL
);
5926 mod_map
->start_addr
= start
;
5927 mod_map
->end_addr
= end
;
5928 mod_map
->num_funcs
= 0;
5930 INIT_LIST_HEAD_RCU(&mod_map
->funcs
);
5932 list_add_rcu(&mod_map
->list
, &ftrace_mod_maps
);
5938 ftrace_func_address_lookup(struct ftrace_mod_map
*mod_map
,
5939 unsigned long addr
, unsigned long *size
,
5940 unsigned long *off
, char *sym
)
5942 struct ftrace_mod_func
*found_func
= NULL
;
5943 struct ftrace_mod_func
*mod_func
;
5945 list_for_each_entry_rcu(mod_func
, &mod_map
->funcs
, list
) {
5946 if (addr
>= mod_func
->ip
&&
5947 addr
< mod_func
->ip
+ mod_func
->size
) {
5948 found_func
= mod_func
;
5955 *size
= found_func
->size
;
5957 *off
= addr
- found_func
->ip
;
5959 strlcpy(sym
, found_func
->name
, KSYM_NAME_LEN
);
5961 return found_func
->name
;
5968 ftrace_mod_address_lookup(unsigned long addr
, unsigned long *size
,
5969 unsigned long *off
, char **modname
, char *sym
)
5971 struct ftrace_mod_map
*mod_map
;
5972 const char *ret
= NULL
;
5974 /* mod_map is freed via call_rcu_sched() */
5976 list_for_each_entry_rcu(mod_map
, &ftrace_mod_maps
, list
) {
5977 ret
= ftrace_func_address_lookup(mod_map
, addr
, size
, off
, sym
);
5980 *modname
= mod_map
->mod
->name
;
5989 int ftrace_mod_get_kallsym(unsigned int symnum
, unsigned long *value
,
5990 char *type
, char *name
,
5991 char *module_name
, int *exported
)
5993 struct ftrace_mod_map
*mod_map
;
5994 struct ftrace_mod_func
*mod_func
;
5997 list_for_each_entry_rcu(mod_map
, &ftrace_mod_maps
, list
) {
5999 if (symnum
>= mod_map
->num_funcs
) {
6000 symnum
-= mod_map
->num_funcs
;
6004 list_for_each_entry_rcu(mod_func
, &mod_map
->funcs
, list
) {
6010 *value
= mod_func
->ip
;
6012 strlcpy(name
, mod_func
->name
, KSYM_NAME_LEN
);
6013 strlcpy(module_name
, mod_map
->mod
->name
, MODULE_NAME_LEN
);
6026 static void save_ftrace_mod_rec(struct ftrace_mod_map
*mod_map
,
6027 struct dyn_ftrace
*rec
) { }
6028 static inline struct ftrace_mod_map
*
6029 allocate_ftrace_mod_map(struct module
*mod
,
6030 unsigned long start
, unsigned long end
)
6034 #endif /* CONFIG_MODULES */
6036 struct ftrace_init_func
{
6037 struct list_head list
;
6041 /* Clear any init ips from hashes */
6043 clear_func_from_hash(struct ftrace_init_func
*func
, struct ftrace_hash
*hash
)
6045 struct ftrace_func_entry
*entry
;
6047 if (ftrace_hash_empty(hash
))
6050 entry
= __ftrace_lookup_ip(hash
, func
->ip
);
6053 * Do not allow this rec to match again.
6054 * Yeah, it may waste some memory, but will be removed
6055 * if/when the hash is modified again.
6062 clear_func_from_hashes(struct ftrace_init_func
*func
)
6064 struct trace_array
*tr
;
6066 mutex_lock(&trace_types_lock
);
6067 list_for_each_entry(tr
, &ftrace_trace_arrays
, list
) {
6068 if (!tr
->ops
|| !tr
->ops
->func_hash
)
6070 mutex_lock(&tr
->ops
->func_hash
->regex_lock
);
6071 clear_func_from_hash(func
, tr
->ops
->func_hash
->filter_hash
);
6072 clear_func_from_hash(func
, tr
->ops
->func_hash
->notrace_hash
);
6073 mutex_unlock(&tr
->ops
->func_hash
->regex_lock
);
6075 mutex_unlock(&trace_types_lock
);
6078 static void add_to_clear_hash_list(struct list_head
*clear_list
,
6079 struct dyn_ftrace
*rec
)
6081 struct ftrace_init_func
*func
;
6083 func
= kmalloc(sizeof(*func
), GFP_KERNEL
);
6085 WARN_ONCE(1, "alloc failure, ftrace filter could be stale\n");
6090 list_add(&func
->list
, clear_list
);
6093 void ftrace_free_mem(struct module
*mod
, void *start_ptr
, void *end_ptr
)
6095 unsigned long start
= (unsigned long)(start_ptr
);
6096 unsigned long end
= (unsigned long)(end_ptr
);
6097 struct ftrace_page
**last_pg
= &ftrace_pages_start
;
6098 struct ftrace_page
*pg
;
6099 struct dyn_ftrace
*rec
;
6100 struct dyn_ftrace key
;
6101 struct ftrace_mod_map
*mod_map
= NULL
;
6102 struct ftrace_init_func
*func
, *func_next
;
6103 struct list_head clear_hash
;
6106 INIT_LIST_HEAD(&clear_hash
);
6109 key
.flags
= end
; /* overload flags, as it is unsigned long */
6111 mutex_lock(&ftrace_lock
);
6114 * If we are freeing module init memory, then check if
6115 * any tracer is active. If so, we need to save a mapping of
6116 * the module functions being freed with the address.
6118 if (mod
&& ftrace_ops_list
!= &ftrace_list_end
)
6119 mod_map
= allocate_ftrace_mod_map(mod
, start
, end
);
6121 for (pg
= ftrace_pages_start
; pg
; last_pg
= &pg
->next
, pg
= *last_pg
) {
6122 if (end
< pg
->records
[0].ip
||
6123 start
>= (pg
->records
[pg
->index
- 1].ip
+ MCOUNT_INSN_SIZE
))
6126 rec
= bsearch(&key
, pg
->records
, pg
->index
,
6127 sizeof(struct dyn_ftrace
),
6132 /* rec will be cleared from hashes after ftrace_lock unlock */
6133 add_to_clear_hash_list(&clear_hash
, rec
);
6136 save_ftrace_mod_rec(mod_map
, rec
);
6139 ftrace_update_tot_cnt
--;
6141 *last_pg
= pg
->next
;
6142 order
= get_count_order(pg
->size
/ ENTRIES_PER_PAGE
);
6143 free_pages((unsigned long)pg
->records
, order
);
6145 pg
= container_of(last_pg
, struct ftrace_page
, next
);
6150 memmove(rec
, rec
+ 1,
6151 (pg
->index
- (rec
- pg
->records
)) * sizeof(*rec
));
6152 /* More than one function may be in this block */
6155 mutex_unlock(&ftrace_lock
);
6157 list_for_each_entry_safe(func
, func_next
, &clear_hash
, list
) {
6158 clear_func_from_hashes(func
);
6163 void __init
ftrace_free_init_mem(void)
6165 void *start
= (void *)(&__init_begin
);
6166 void *end
= (void *)(&__init_end
);
6168 ftrace_free_mem(NULL
, start
, end
);
6171 void __init
ftrace_init(void)
6173 extern unsigned long __start_mcount_loc
[];
6174 extern unsigned long __stop_mcount_loc
[];
6175 unsigned long count
, flags
;
6178 local_irq_save(flags
);
6179 ret
= ftrace_dyn_arch_init();
6180 local_irq_restore(flags
);
6184 count
= __stop_mcount_loc
- __start_mcount_loc
;
6186 pr_info("ftrace: No functions to be traced?\n");
6190 pr_info("ftrace: allocating %ld entries in %ld pages\n",
6191 count
, count
/ ENTRIES_PER_PAGE
+ 1);
6193 last_ftrace_enabled
= ftrace_enabled
= 1;
6195 ret
= ftrace_process_locs(NULL
,
6199 set_ftrace_early_filters();
6203 ftrace_disabled
= 1;
6206 /* Do nothing if arch does not support this */
6207 void __weak
arch_ftrace_update_trampoline(struct ftrace_ops
*ops
)
6211 static void ftrace_update_trampoline(struct ftrace_ops
*ops
)
6213 arch_ftrace_update_trampoline(ops
);
6216 void ftrace_init_trace_array(struct trace_array
*tr
)
6218 INIT_LIST_HEAD(&tr
->func_probes
);
6219 INIT_LIST_HEAD(&tr
->mod_trace
);
6220 INIT_LIST_HEAD(&tr
->mod_notrace
);
6224 static struct ftrace_ops global_ops
= {
6225 .func
= ftrace_stub
,
6226 .flags
= FTRACE_OPS_FL_RECURSION_SAFE
|
6227 FTRACE_OPS_FL_INITIALIZED
|
6231 static int __init
ftrace_nodyn_init(void)
6236 core_initcall(ftrace_nodyn_init
);
6238 static inline int ftrace_init_dyn_tracefs(struct dentry
*d_tracer
) { return 0; }
6239 static inline void ftrace_startup_enable(int command
) { }
6240 static inline void ftrace_startup_all(int command
) { }
6241 /* Keep as macros so we do not need to define the commands */
6242 # define ftrace_startup(ops, command) \
6244 int ___ret = __register_ftrace_function(ops); \
6246 (ops)->flags |= FTRACE_OPS_FL_ENABLED; \
6249 # define ftrace_shutdown(ops, command) \
6251 int ___ret = __unregister_ftrace_function(ops); \
6253 (ops)->flags &= ~FTRACE_OPS_FL_ENABLED; \
6257 # define ftrace_startup_sysctl() do { } while (0)
6258 # define ftrace_shutdown_sysctl() do { } while (0)
6261 ftrace_ops_test(struct ftrace_ops
*ops
, unsigned long ip
, void *regs
)
6266 static void ftrace_update_trampoline(struct ftrace_ops
*ops
)
6270 #endif /* CONFIG_DYNAMIC_FTRACE */
6272 __init
void ftrace_init_global_array_ops(struct trace_array
*tr
)
6274 tr
->ops
= &global_ops
;
6275 tr
->ops
->private = tr
;
6276 ftrace_init_trace_array(tr
);
6279 void ftrace_init_array_ops(struct trace_array
*tr
, ftrace_func_t func
)
6281 /* If we filter on pids, update to use the pid function */
6282 if (tr
->flags
& TRACE_ARRAY_FL_GLOBAL
) {
6283 if (WARN_ON(tr
->ops
->func
!= ftrace_stub
))
6284 printk("ftrace ops had %pS for function\n",
6287 tr
->ops
->func
= func
;
6288 tr
->ops
->private = tr
;
6291 void ftrace_reset_array_ops(struct trace_array
*tr
)
6293 tr
->ops
->func
= ftrace_stub
;
6296 static nokprobe_inline
void
6297 __ftrace_ops_list_func(unsigned long ip
, unsigned long parent_ip
,
6298 struct ftrace_ops
*ignored
, struct pt_regs
*regs
)
6300 struct ftrace_ops
*op
;
6303 bit
= trace_test_and_set_recursion(TRACE_LIST_START
, TRACE_LIST_MAX
);
6308 * Some of the ops may be dynamically allocated,
6309 * they must be freed after a synchronize_sched().
6311 preempt_disable_notrace();
6313 do_for_each_ftrace_op(op
, ftrace_ops_list
) {
6315 * Check the following for each ops before calling their func:
6316 * if RCU flag is set, then rcu_is_watching() must be true
6317 * if PER_CPU is set, then ftrace_function_local_disable()
6319 * Otherwise test if the ip matches the ops filter
6321 * If any of the above fails then the op->func() is not executed.
6323 if ((!(op
->flags
& FTRACE_OPS_FL_RCU
) || rcu_is_watching()) &&
6324 ftrace_ops_test(op
, ip
, regs
)) {
6325 if (FTRACE_WARN_ON(!op
->func
)) {
6326 pr_warn("op=%p %pS\n", op
, op
);
6329 op
->func(ip
, parent_ip
, op
, regs
);
6331 } while_for_each_ftrace_op(op
);
6333 preempt_enable_notrace();
6334 trace_clear_recursion(bit
);
6338 * Some archs only support passing ip and parent_ip. Even though
6339 * the list function ignores the op parameter, we do not want any
6340 * C side effects, where a function is called without the caller
6341 * sending a third parameter.
6342 * Archs are to support both the regs and ftrace_ops at the same time.
6343 * If they support ftrace_ops, it is assumed they support regs.
6344 * If call backs want to use regs, they must either check for regs
6345 * being NULL, or CONFIG_DYNAMIC_FTRACE_WITH_REGS.
6346 * Note, CONFIG_DYNAMIC_FTRACE_WITH_REGS expects a full regs to be saved.
6347 * An architecture can pass partial regs with ftrace_ops and still
6348 * set the ARCH_SUPPORTS_FTRACE_OPS.
6350 #if ARCH_SUPPORTS_FTRACE_OPS
6351 static void ftrace_ops_list_func(unsigned long ip
, unsigned long parent_ip
,
6352 struct ftrace_ops
*op
, struct pt_regs
*regs
)
6354 __ftrace_ops_list_func(ip
, parent_ip
, NULL
, regs
);
6356 NOKPROBE_SYMBOL(ftrace_ops_list_func
);
6358 static void ftrace_ops_no_ops(unsigned long ip
, unsigned long parent_ip
)
6360 __ftrace_ops_list_func(ip
, parent_ip
, NULL
, NULL
);
6362 NOKPROBE_SYMBOL(ftrace_ops_no_ops
);
6366 * If there's only one function registered but it does not support
6367 * recursion, needs RCU protection and/or requires per cpu handling, then
6368 * this function will be called by the mcount trampoline.
6370 static void ftrace_ops_assist_func(unsigned long ip
, unsigned long parent_ip
,
6371 struct ftrace_ops
*op
, struct pt_regs
*regs
)
6375 if ((op
->flags
& FTRACE_OPS_FL_RCU
) && !rcu_is_watching())
6378 bit
= trace_test_and_set_recursion(TRACE_LIST_START
, TRACE_LIST_MAX
);
6382 preempt_disable_notrace();
6384 op
->func(ip
, parent_ip
, op
, regs
);
6386 preempt_enable_notrace();
6387 trace_clear_recursion(bit
);
6389 NOKPROBE_SYMBOL(ftrace_ops_assist_func
);
6392 * ftrace_ops_get_func - get the function a trampoline should call
6393 * @ops: the ops to get the function for
6395 * Normally the mcount trampoline will call the ops->func, but there
6396 * are times that it should not. For example, if the ops does not
6397 * have its own recursion protection, then it should call the
6398 * ftrace_ops_assist_func() instead.
6400 * Returns the function that the trampoline should call for @ops.
6402 ftrace_func_t
ftrace_ops_get_func(struct ftrace_ops
*ops
)
6405 * If the function does not handle recursion, needs to be RCU safe,
6406 * or does per cpu logic, then we need to call the assist handler.
6408 if (!(ops
->flags
& FTRACE_OPS_FL_RECURSION_SAFE
) ||
6409 ops
->flags
& FTRACE_OPS_FL_RCU
)
6410 return ftrace_ops_assist_func
;
6416 ftrace_filter_pid_sched_switch_probe(void *data
, bool preempt
,
6417 struct task_struct
*prev
, struct task_struct
*next
)
6419 struct trace_array
*tr
= data
;
6420 struct trace_pid_list
*pid_list
;
6422 pid_list
= rcu_dereference_sched(tr
->function_pids
);
6424 this_cpu_write(tr
->trace_buffer
.data
->ftrace_ignore_pid
,
6425 trace_ignore_this_task(pid_list
, next
));
6429 ftrace_pid_follow_sched_process_fork(void *data
,
6430 struct task_struct
*self
,
6431 struct task_struct
*task
)
6433 struct trace_pid_list
*pid_list
;
6434 struct trace_array
*tr
= data
;
6436 pid_list
= rcu_dereference_sched(tr
->function_pids
);
6437 trace_filter_add_remove_task(pid_list
, self
, task
);
6441 ftrace_pid_follow_sched_process_exit(void *data
, struct task_struct
*task
)
6443 struct trace_pid_list
*pid_list
;
6444 struct trace_array
*tr
= data
;
6446 pid_list
= rcu_dereference_sched(tr
->function_pids
);
6447 trace_filter_add_remove_task(pid_list
, NULL
, task
);
6450 void ftrace_pid_follow_fork(struct trace_array
*tr
, bool enable
)
6453 register_trace_sched_process_fork(ftrace_pid_follow_sched_process_fork
,
6455 register_trace_sched_process_exit(ftrace_pid_follow_sched_process_exit
,
6458 unregister_trace_sched_process_fork(ftrace_pid_follow_sched_process_fork
,
6460 unregister_trace_sched_process_exit(ftrace_pid_follow_sched_process_exit
,
6465 static void clear_ftrace_pids(struct trace_array
*tr
)
6467 struct trace_pid_list
*pid_list
;
6470 pid_list
= rcu_dereference_protected(tr
->function_pids
,
6471 lockdep_is_held(&ftrace_lock
));
6475 unregister_trace_sched_switch(ftrace_filter_pid_sched_switch_probe
, tr
);
6477 for_each_possible_cpu(cpu
)
6478 per_cpu_ptr(tr
->trace_buffer
.data
, cpu
)->ftrace_ignore_pid
= false;
6480 rcu_assign_pointer(tr
->function_pids
, NULL
);
6482 /* Wait till all users are no longer using pid filtering */
6483 synchronize_sched();
6485 trace_free_pid_list(pid_list
);
6488 void ftrace_clear_pids(struct trace_array
*tr
)
6490 mutex_lock(&ftrace_lock
);
6492 clear_ftrace_pids(tr
);
6494 mutex_unlock(&ftrace_lock
);
6497 static void ftrace_pid_reset(struct trace_array
*tr
)
6499 mutex_lock(&ftrace_lock
);
6500 clear_ftrace_pids(tr
);
6502 ftrace_update_pid_func();
6503 ftrace_startup_all(0);
6505 mutex_unlock(&ftrace_lock
);
6508 /* Greater than any max PID */
6509 #define FTRACE_NO_PIDS (void *)(PID_MAX_LIMIT + 1)
6511 static void *fpid_start(struct seq_file
*m
, loff_t
*pos
)
6514 struct trace_pid_list
*pid_list
;
6515 struct trace_array
*tr
= m
->private;
6517 mutex_lock(&ftrace_lock
);
6518 rcu_read_lock_sched();
6520 pid_list
= rcu_dereference_sched(tr
->function_pids
);
6523 return !(*pos
) ? FTRACE_NO_PIDS
: NULL
;
6525 return trace_pid_start(pid_list
, pos
);
6528 static void *fpid_next(struct seq_file
*m
, void *v
, loff_t
*pos
)
6530 struct trace_array
*tr
= m
->private;
6531 struct trace_pid_list
*pid_list
= rcu_dereference_sched(tr
->function_pids
);
6533 if (v
== FTRACE_NO_PIDS
)
6536 return trace_pid_next(pid_list
, v
, pos
);
6539 static void fpid_stop(struct seq_file
*m
, void *p
)
6542 rcu_read_unlock_sched();
6543 mutex_unlock(&ftrace_lock
);
6546 static int fpid_show(struct seq_file
*m
, void *v
)
6548 if (v
== FTRACE_NO_PIDS
) {
6549 seq_puts(m
, "no pid\n");
6553 return trace_pid_show(m
, v
);
6556 static const struct seq_operations ftrace_pid_sops
= {
6557 .start
= fpid_start
,
6564 ftrace_pid_open(struct inode
*inode
, struct file
*file
)
6566 struct trace_array
*tr
= inode
->i_private
;
6570 if (trace_array_get(tr
) < 0)
6573 if ((file
->f_mode
& FMODE_WRITE
) &&
6574 (file
->f_flags
& O_TRUNC
))
6575 ftrace_pid_reset(tr
);
6577 ret
= seq_open(file
, &ftrace_pid_sops
);
6579 trace_array_put(tr
);
6581 m
= file
->private_data
;
6582 /* copy tr over to seq ops */
6589 static void ignore_task_cpu(void *data
)
6591 struct trace_array
*tr
= data
;
6592 struct trace_pid_list
*pid_list
;
6595 * This function is called by on_each_cpu() while the
6596 * event_mutex is held.
6598 pid_list
= rcu_dereference_protected(tr
->function_pids
,
6599 mutex_is_locked(&ftrace_lock
));
6601 this_cpu_write(tr
->trace_buffer
.data
->ftrace_ignore_pid
,
6602 trace_ignore_this_task(pid_list
, current
));
6606 ftrace_pid_write(struct file
*filp
, const char __user
*ubuf
,
6607 size_t cnt
, loff_t
*ppos
)
6609 struct seq_file
*m
= filp
->private_data
;
6610 struct trace_array
*tr
= m
->private;
6611 struct trace_pid_list
*filtered_pids
= NULL
;
6612 struct trace_pid_list
*pid_list
;
6618 mutex_lock(&ftrace_lock
);
6620 filtered_pids
= rcu_dereference_protected(tr
->function_pids
,
6621 lockdep_is_held(&ftrace_lock
));
6623 ret
= trace_pid_write(filtered_pids
, &pid_list
, ubuf
, cnt
);
6627 rcu_assign_pointer(tr
->function_pids
, pid_list
);
6629 if (filtered_pids
) {
6630 synchronize_sched();
6631 trace_free_pid_list(filtered_pids
);
6632 } else if (pid_list
) {
6633 /* Register a probe to set whether to ignore the tracing of a task */
6634 register_trace_sched_switch(ftrace_filter_pid_sched_switch_probe
, tr
);
6638 * Ignoring of pids is done at task switch. But we have to
6639 * check for those tasks that are currently running.
6640 * Always do this in case a pid was appended or removed.
6642 on_each_cpu(ignore_task_cpu
, tr
, 1);
6644 ftrace_update_pid_func();
6645 ftrace_startup_all(0);
6647 mutex_unlock(&ftrace_lock
);
6656 ftrace_pid_release(struct inode
*inode
, struct file
*file
)
6658 struct trace_array
*tr
= inode
->i_private
;
6660 trace_array_put(tr
);
6662 return seq_release(inode
, file
);
6665 static const struct file_operations ftrace_pid_fops
= {
6666 .open
= ftrace_pid_open
,
6667 .write
= ftrace_pid_write
,
6669 .llseek
= tracing_lseek
,
6670 .release
= ftrace_pid_release
,
6673 void ftrace_init_tracefs(struct trace_array
*tr
, struct dentry
*d_tracer
)
6675 trace_create_file("set_ftrace_pid", 0644, d_tracer
,
6676 tr
, &ftrace_pid_fops
);
6679 void __init
ftrace_init_tracefs_toplevel(struct trace_array
*tr
,
6680 struct dentry
*d_tracer
)
6682 /* Only the top level directory has the dyn_tracefs and profile */
6683 WARN_ON(!(tr
->flags
& TRACE_ARRAY_FL_GLOBAL
));
6685 ftrace_init_dyn_tracefs(d_tracer
);
6686 ftrace_profile_tracefs(d_tracer
);
6690 * ftrace_kill - kill ftrace
6692 * This function should be used by panic code. It stops ftrace
6693 * but in a not so nice way. If you need to simply kill ftrace
6694 * from a non-atomic section, use ftrace_kill.
6696 void ftrace_kill(void)
6698 ftrace_disabled
= 1;
6700 clear_ftrace_function();
6704 * Test if ftrace is dead or not.
6706 int ftrace_is_dead(void)
6708 return ftrace_disabled
;
6712 * register_ftrace_function - register a function for profiling
6713 * @ops - ops structure that holds the function for profiling.
6715 * Register a function to be called by all functions in the
6718 * Note: @ops->func and all the functions it calls must be labeled
6719 * with "notrace", otherwise it will go into a
6722 int register_ftrace_function(struct ftrace_ops
*ops
)
6726 ftrace_ops_init(ops
);
6728 mutex_lock(&ftrace_lock
);
6730 ret
= ftrace_startup(ops
, 0);
6732 mutex_unlock(&ftrace_lock
);
6736 EXPORT_SYMBOL_GPL(register_ftrace_function
);
6739 * unregister_ftrace_function - unregister a function for profiling.
6740 * @ops - ops structure that holds the function to unregister
6742 * Unregister a function that was added to be called by ftrace profiling.
6744 int unregister_ftrace_function(struct ftrace_ops
*ops
)
6748 mutex_lock(&ftrace_lock
);
6749 ret
= ftrace_shutdown(ops
, 0);
6750 mutex_unlock(&ftrace_lock
);
6754 EXPORT_SYMBOL_GPL(unregister_ftrace_function
);
6757 ftrace_enable_sysctl(struct ctl_table
*table
, int write
,
6758 void __user
*buffer
, size_t *lenp
,
6763 mutex_lock(&ftrace_lock
);
6765 if (unlikely(ftrace_disabled
))
6768 ret
= proc_dointvec(table
, write
, buffer
, lenp
, ppos
);
6770 if (ret
|| !write
|| (last_ftrace_enabled
== !!ftrace_enabled
))
6773 last_ftrace_enabled
= !!ftrace_enabled
;
6775 if (ftrace_enabled
) {
6777 /* we are starting ftrace again */
6778 if (rcu_dereference_protected(ftrace_ops_list
,
6779 lockdep_is_held(&ftrace_lock
)) != &ftrace_list_end
)
6780 update_ftrace_function();
6782 ftrace_startup_sysctl();
6785 /* stopping ftrace calls (just send to ftrace_stub) */
6786 ftrace_trace_function
= ftrace_stub
;
6788 ftrace_shutdown_sysctl();
6792 mutex_unlock(&ftrace_lock
);
6796 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
6798 static struct ftrace_ops graph_ops
= {
6799 .func
= ftrace_stub
,
6800 .flags
= FTRACE_OPS_FL_RECURSION_SAFE
|
6801 FTRACE_OPS_FL_INITIALIZED
|
6804 #ifdef FTRACE_GRAPH_TRAMP_ADDR
6805 .trampoline
= FTRACE_GRAPH_TRAMP_ADDR
,
6806 /* trampoline_size is only needed for dynamically allocated tramps */
6808 ASSIGN_OPS_HASH(graph_ops
, &global_ops
.local_hash
)
6811 void ftrace_graph_sleep_time_control(bool enable
)
6813 fgraph_sleep_time
= enable
;
6816 void ftrace_graph_graph_time_control(bool enable
)
6818 fgraph_graph_time
= enable
;
6821 int ftrace_graph_entry_stub(struct ftrace_graph_ent
*trace
)
6826 /* The callbacks that hook a function */
6827 trace_func_graph_ret_t ftrace_graph_return
=
6828 (trace_func_graph_ret_t
)ftrace_stub
;
6829 trace_func_graph_ent_t ftrace_graph_entry
= ftrace_graph_entry_stub
;
6830 static trace_func_graph_ent_t __ftrace_graph_entry
= ftrace_graph_entry_stub
;
6832 /* Try to assign a return stack array on FTRACE_RETSTACK_ALLOC_SIZE tasks. */
6833 static int alloc_retstack_tasklist(struct ftrace_ret_stack
**ret_stack_list
)
6837 int start
= 0, end
= FTRACE_RETSTACK_ALLOC_SIZE
;
6838 struct task_struct
*g
, *t
;
6840 for (i
= 0; i
< FTRACE_RETSTACK_ALLOC_SIZE
; i
++) {
6841 ret_stack_list
[i
] = kmalloc(FTRACE_RETFUNC_DEPTH
6842 * sizeof(struct ftrace_ret_stack
),
6844 if (!ret_stack_list
[i
]) {
6852 read_lock(&tasklist_lock
);
6853 do_each_thread(g
, t
) {
6859 if (t
->ret_stack
== NULL
) {
6860 atomic_set(&t
->tracing_graph_pause
, 0);
6861 atomic_set(&t
->trace_overrun
, 0);
6862 t
->curr_ret_stack
= -1;
6863 t
->curr_ret_depth
= -1;
6864 /* Make sure the tasks see the -1 first: */
6866 t
->ret_stack
= ret_stack_list
[start
++];
6868 } while_each_thread(g
, t
);
6871 read_unlock(&tasklist_lock
);
6873 for (i
= start
; i
< end
; i
++)
6874 kfree(ret_stack_list
[i
]);
6879 ftrace_graph_probe_sched_switch(void *ignore
, bool preempt
,
6880 struct task_struct
*prev
, struct task_struct
*next
)
6882 unsigned long long timestamp
;
6886 * Does the user want to count the time a function was asleep.
6887 * If so, do not update the time stamps.
6889 if (fgraph_sleep_time
)
6892 timestamp
= trace_clock_local();
6894 prev
->ftrace_timestamp
= timestamp
;
6896 /* only process tasks that we timestamped */
6897 if (!next
->ftrace_timestamp
)
6901 * Update all the counters in next to make up for the
6902 * time next was sleeping.
6904 timestamp
-= next
->ftrace_timestamp
;
6906 for (index
= next
->curr_ret_stack
; index
>= 0; index
--)
6907 next
->ret_stack
[index
].calltime
+= timestamp
;
6910 /* Allocate a return stack for each task */
6911 static int start_graph_tracing(void)
6913 struct ftrace_ret_stack
**ret_stack_list
;
6916 ret_stack_list
= kmalloc(FTRACE_RETSTACK_ALLOC_SIZE
*
6917 sizeof(struct ftrace_ret_stack
*),
6920 if (!ret_stack_list
)
6923 /* The cpu_boot init_task->ret_stack will never be freed */
6924 for_each_online_cpu(cpu
) {
6925 if (!idle_task(cpu
)->ret_stack
)
6926 ftrace_graph_init_idle_task(idle_task(cpu
), cpu
);
6930 ret
= alloc_retstack_tasklist(ret_stack_list
);
6931 } while (ret
== -EAGAIN
);
6934 ret
= register_trace_sched_switch(ftrace_graph_probe_sched_switch
, NULL
);
6936 pr_info("ftrace_graph: Couldn't activate tracepoint"
6937 " probe to kernel_sched_switch\n");
6940 kfree(ret_stack_list
);
6945 * Hibernation protection.
6946 * The state of the current task is too much unstable during
6947 * suspend/restore to disk. We want to protect against that.
6950 ftrace_suspend_notifier_call(struct notifier_block
*bl
, unsigned long state
,
6954 case PM_HIBERNATION_PREPARE
:
6955 pause_graph_tracing();
6958 case PM_POST_HIBERNATION
:
6959 unpause_graph_tracing();
6965 static int ftrace_graph_entry_test(struct ftrace_graph_ent
*trace
)
6967 if (!ftrace_ops_test(&global_ops
, trace
->func
, NULL
))
6969 return __ftrace_graph_entry(trace
);
6973 * The function graph tracer should only trace the functions defined
6974 * by set_ftrace_filter and set_ftrace_notrace. If another function
6975 * tracer ops is registered, the graph tracer requires testing the
6976 * function against the global ops, and not just trace any function
6977 * that any ftrace_ops registered.
6979 static void update_function_graph_func(void)
6981 struct ftrace_ops
*op
;
6982 bool do_test
= false;
6985 * The graph and global ops share the same set of functions
6986 * to test. If any other ops is on the list, then
6987 * the graph tracing needs to test if its the function
6990 do_for_each_ftrace_op(op
, ftrace_ops_list
) {
6991 if (op
!= &global_ops
&& op
!= &graph_ops
&&
6992 op
!= &ftrace_list_end
) {
6994 /* in double loop, break out with goto */
6997 } while_for_each_ftrace_op(op
);
7000 ftrace_graph_entry
= ftrace_graph_entry_test
;
7002 ftrace_graph_entry
= __ftrace_graph_entry
;
7005 static struct notifier_block ftrace_suspend_notifier
= {
7006 .notifier_call
= ftrace_suspend_notifier_call
,
7009 int register_ftrace_graph(trace_func_graph_ret_t retfunc
,
7010 trace_func_graph_ent_t entryfunc
)
7014 mutex_lock(&ftrace_lock
);
7016 /* we currently allow only one tracer registered at a time */
7017 if (ftrace_graph_active
) {
7022 register_pm_notifier(&ftrace_suspend_notifier
);
7024 ftrace_graph_active
++;
7025 ret
= start_graph_tracing();
7027 ftrace_graph_active
--;
7031 ftrace_graph_return
= retfunc
;
7034 * Update the indirect function to the entryfunc, and the
7035 * function that gets called to the entry_test first. Then
7036 * call the update fgraph entry function to determine if
7037 * the entryfunc should be called directly or not.
7039 __ftrace_graph_entry
= entryfunc
;
7040 ftrace_graph_entry
= ftrace_graph_entry_test
;
7041 update_function_graph_func();
7043 ret
= ftrace_startup(&graph_ops
, FTRACE_START_FUNC_RET
);
7045 mutex_unlock(&ftrace_lock
);
7049 void unregister_ftrace_graph(void)
7051 mutex_lock(&ftrace_lock
);
7053 if (unlikely(!ftrace_graph_active
))
7056 ftrace_graph_active
--;
7057 ftrace_graph_return
= (trace_func_graph_ret_t
)ftrace_stub
;
7058 ftrace_graph_entry
= ftrace_graph_entry_stub
;
7059 __ftrace_graph_entry
= ftrace_graph_entry_stub
;
7060 ftrace_shutdown(&graph_ops
, FTRACE_STOP_FUNC_RET
);
7061 unregister_pm_notifier(&ftrace_suspend_notifier
);
7062 unregister_trace_sched_switch(ftrace_graph_probe_sched_switch
, NULL
);
7065 mutex_unlock(&ftrace_lock
);
7068 static DEFINE_PER_CPU(struct ftrace_ret_stack
*, idle_ret_stack
);
7071 graph_init_task(struct task_struct
*t
, struct ftrace_ret_stack
*ret_stack
)
7073 atomic_set(&t
->tracing_graph_pause
, 0);
7074 atomic_set(&t
->trace_overrun
, 0);
7075 t
->ftrace_timestamp
= 0;
7076 /* make curr_ret_stack visible before we add the ret_stack */
7078 t
->ret_stack
= ret_stack
;
7082 * Allocate a return stack for the idle task. May be the first
7083 * time through, or it may be done by CPU hotplug online.
7085 void ftrace_graph_init_idle_task(struct task_struct
*t
, int cpu
)
7087 t
->curr_ret_stack
= -1;
7088 t
->curr_ret_depth
= -1;
7090 * The idle task has no parent, it either has its own
7091 * stack or no stack at all.
7094 WARN_ON(t
->ret_stack
!= per_cpu(idle_ret_stack
, cpu
));
7096 if (ftrace_graph_active
) {
7097 struct ftrace_ret_stack
*ret_stack
;
7099 ret_stack
= per_cpu(idle_ret_stack
, cpu
);
7101 ret_stack
= kmalloc(FTRACE_RETFUNC_DEPTH
7102 * sizeof(struct ftrace_ret_stack
),
7106 per_cpu(idle_ret_stack
, cpu
) = ret_stack
;
7108 graph_init_task(t
, ret_stack
);
7112 /* Allocate a return stack for newly created task */
7113 void ftrace_graph_init_task(struct task_struct
*t
)
7115 /* Make sure we do not use the parent ret_stack */
7116 t
->ret_stack
= NULL
;
7117 t
->curr_ret_stack
= -1;
7118 t
->curr_ret_depth
= -1;
7120 if (ftrace_graph_active
) {
7121 struct ftrace_ret_stack
*ret_stack
;
7123 ret_stack
= kmalloc(FTRACE_RETFUNC_DEPTH
7124 * sizeof(struct ftrace_ret_stack
),
7128 graph_init_task(t
, ret_stack
);
7132 void ftrace_graph_exit_task(struct task_struct
*t
)
7134 struct ftrace_ret_stack
*ret_stack
= t
->ret_stack
;
7136 t
->ret_stack
= NULL
;
7137 /* NULL must become visible to IRQs before we free it: */