1 // SPDX-License-Identifier: GPL-2.0
3 * Infrastructure for profiling code inserted by 'gcc -pg'.
5 * Copyright (C) 2007-2008 Steven Rostedt <srostedt@redhat.com>
6 * Copyright (C) 2004-2008 Ingo Molnar <mingo@redhat.com>
8 * Originally ported from the -rt patch by:
9 * Copyright (C) 2007 Arnaldo Carvalho de Melo <acme@redhat.com>
11 * Based on code in the latency_tracer, that is:
13 * Copyright (C) 2004-2006 Ingo Molnar
14 * Copyright (C) 2004 Nadia Yvette Chambers
17 #include <linux/stop_machine.h>
18 #include <linux/clocksource.h>
19 #include <linux/sched/task.h>
20 #include <linux/kallsyms.h>
21 #include <linux/security.h>
22 #include <linux/seq_file.h>
23 #include <linux/tracefs.h>
24 #include <linux/hardirq.h>
25 #include <linux/kthread.h>
26 #include <linux/uaccess.h>
27 #include <linux/bsearch.h>
28 #include <linux/module.h>
29 #include <linux/ftrace.h>
30 #include <linux/sysctl.h>
31 #include <linux/slab.h>
32 #include <linux/ctype.h>
33 #include <linux/sort.h>
34 #include <linux/list.h>
35 #include <linux/hash.h>
36 #include <linux/rcupdate.h>
37 #include <linux/kprobes.h>
39 #include <trace/events/sched.h>
41 #include <asm/sections.h>
42 #include <asm/setup.h>
44 #include "ftrace_internal.h"
45 #include "trace_output.h"
46 #include "trace_stat.h"
48 /* Flags that do not get reset */
49 #define FTRACE_NOCLEAR_FLAGS (FTRACE_FL_DISABLED | FTRACE_FL_TOUCHED | \
52 #define FTRACE_INVALID_FUNCTION "__ftrace_invalid_address__"
54 #define FTRACE_WARN_ON(cond) \
62 #define FTRACE_WARN_ON_ONCE(cond) \
65 if (WARN_ON_ONCE(___r)) \
70 /* hash bits for specific function selection */
71 #define FTRACE_HASH_DEFAULT_BITS 10
72 #define FTRACE_HASH_MAX_BITS 12
74 #ifdef CONFIG_DYNAMIC_FTRACE
75 #define INIT_OPS_HASH(opsname) \
76 .func_hash = &opsname.local_hash, \
77 .local_hash.regex_lock = __MUTEX_INITIALIZER(opsname.local_hash.regex_lock), \
78 .subop_list = LIST_HEAD_INIT(opsname.subop_list),
80 #define INIT_OPS_HASH(opsname)
84 FTRACE_MODIFY_ENABLE_FL
= (1 << 0),
85 FTRACE_MODIFY_MAY_SLEEP_FL
= (1 << 1),
88 struct ftrace_ops ftrace_list_end __read_mostly
= {
90 .flags
= FTRACE_OPS_FL_STUB
,
91 INIT_OPS_HASH(ftrace_list_end
)
94 /* ftrace_enabled is a method to turn ftrace on or off */
95 int ftrace_enabled __read_mostly
;
96 static int __maybe_unused last_ftrace_enabled
;
98 /* Current function tracing op */
99 struct ftrace_ops
*function_trace_op __read_mostly
= &ftrace_list_end
;
100 /* What to set function_trace_op to */
101 static struct ftrace_ops
*set_function_trace_op
;
103 bool ftrace_pids_enabled(struct ftrace_ops
*ops
)
105 struct trace_array
*tr
;
107 if (!(ops
->flags
& FTRACE_OPS_FL_PID
) || !ops
->private)
112 return tr
->function_pids
!= NULL
|| tr
->function_no_pids
!= NULL
;
115 static void ftrace_update_trampoline(struct ftrace_ops
*ops
);
118 * ftrace_disabled is set when an anomaly is discovered.
119 * ftrace_disabled is much stronger than ftrace_enabled.
121 static int ftrace_disabled __read_mostly
;
123 DEFINE_MUTEX(ftrace_lock
);
125 struct ftrace_ops __rcu
*ftrace_ops_list __read_mostly
= (struct ftrace_ops __rcu
*)&ftrace_list_end
;
126 ftrace_func_t ftrace_trace_function __read_mostly
= ftrace_stub
;
127 struct ftrace_ops global_ops
;
129 /* Defined by vmlinux.lds.h see the comment above arch_ftrace_ops_list_func for details */
130 void ftrace_ops_list_func(unsigned long ip
, unsigned long parent_ip
,
131 struct ftrace_ops
*op
, struct ftrace_regs
*fregs
);
133 #ifdef CONFIG_DYNAMIC_FTRACE_WITH_CALL_OPS
135 * Stub used to invoke the list ops without requiring a separate trampoline.
137 const struct ftrace_ops ftrace_list_ops
= {
138 .func
= ftrace_ops_list_func
,
139 .flags
= FTRACE_OPS_FL_STUB
,
142 static void ftrace_ops_nop_func(unsigned long ip
, unsigned long parent_ip
,
143 struct ftrace_ops
*op
,
144 struct ftrace_regs
*fregs
)
150 * Stub used when a call site is disabled. May be called transiently by threads
151 * which have made it into ftrace_caller but haven't yet recovered the ops at
152 * the point the call site is disabled.
154 const struct ftrace_ops ftrace_nop_ops
= {
155 .func
= ftrace_ops_nop_func
,
156 .flags
= FTRACE_OPS_FL_STUB
,
160 static inline void ftrace_ops_init(struct ftrace_ops
*ops
)
162 #ifdef CONFIG_DYNAMIC_FTRACE
163 if (!(ops
->flags
& FTRACE_OPS_FL_INITIALIZED
)) {
164 mutex_init(&ops
->local_hash
.regex_lock
);
165 INIT_LIST_HEAD(&ops
->subop_list
);
166 ops
->func_hash
= &ops
->local_hash
;
167 ops
->flags
|= FTRACE_OPS_FL_INITIALIZED
;
172 /* Call this function for when a callback filters on set_ftrace_pid */
173 static void ftrace_pid_func(unsigned long ip
, unsigned long parent_ip
,
174 struct ftrace_ops
*op
, struct ftrace_regs
*fregs
)
176 struct trace_array
*tr
= op
->private;
180 pid
= this_cpu_read(tr
->array_buffer
.data
->ftrace_ignore_pid
);
181 if (pid
== FTRACE_PID_IGNORE
)
183 if (pid
!= FTRACE_PID_TRACE
&&
188 op
->saved_func(ip
, parent_ip
, op
, fregs
);
191 static void ftrace_sync_ipi(void *data
)
193 /* Probably not needed, but do it anyway */
197 static ftrace_func_t
ftrace_ops_get_list_func(struct ftrace_ops
*ops
)
200 * If this is a dynamic or RCU ops, or we force list func,
201 * then it needs to call the list anyway.
203 if (ops
->flags
& (FTRACE_OPS_FL_DYNAMIC
| FTRACE_OPS_FL_RCU
) ||
204 FTRACE_FORCE_LIST_FUNC
)
205 return ftrace_ops_list_func
;
207 return ftrace_ops_get_func(ops
);
210 static void update_ftrace_function(void)
215 * Prepare the ftrace_ops that the arch callback will use.
216 * If there's only one ftrace_ops registered, the ftrace_ops_list
217 * will point to the ops we want.
219 set_function_trace_op
= rcu_dereference_protected(ftrace_ops_list
,
220 lockdep_is_held(&ftrace_lock
));
222 /* If there's no ftrace_ops registered, just call the stub function */
223 if (set_function_trace_op
== &ftrace_list_end
) {
227 * If we are at the end of the list and this ops is
228 * recursion safe and not dynamic and the arch supports passing ops,
229 * then have the mcount trampoline call the function directly.
231 } else if (rcu_dereference_protected(ftrace_ops_list
->next
,
232 lockdep_is_held(&ftrace_lock
)) == &ftrace_list_end
) {
233 func
= ftrace_ops_get_list_func(ftrace_ops_list
);
236 /* Just use the default ftrace_ops */
237 set_function_trace_op
= &ftrace_list_end
;
238 func
= ftrace_ops_list_func
;
241 /* If there's no change, then do nothing more here */
242 if (ftrace_trace_function
== func
)
246 * If we are using the list function, it doesn't care
247 * about the function_trace_ops.
249 if (func
== ftrace_ops_list_func
) {
250 ftrace_trace_function
= func
;
252 * Don't even bother setting function_trace_ops,
253 * it would be racy to do so anyway.
258 #ifndef CONFIG_DYNAMIC_FTRACE
260 * For static tracing, we need to be a bit more careful.
261 * The function change takes affect immediately. Thus,
262 * we need to coordinate the setting of the function_trace_ops
263 * with the setting of the ftrace_trace_function.
265 * Set the function to the list ops, which will call the
266 * function we want, albeit indirectly, but it handles the
267 * ftrace_ops and doesn't depend on function_trace_op.
269 ftrace_trace_function
= ftrace_ops_list_func
;
271 * Make sure all CPUs see this. Yes this is slow, but static
272 * tracing is slow and nasty to have enabled.
274 synchronize_rcu_tasks_rude();
275 /* Now all cpus are using the list ops. */
276 function_trace_op
= set_function_trace_op
;
277 /* Make sure the function_trace_op is visible on all CPUs */
279 /* Nasty way to force a rmb on all cpus */
280 smp_call_function(ftrace_sync_ipi
, NULL
, 1);
281 /* OK, we are all set to update the ftrace_trace_function now! */
282 #endif /* !CONFIG_DYNAMIC_FTRACE */
284 ftrace_trace_function
= func
;
287 static void add_ftrace_ops(struct ftrace_ops __rcu
**list
,
288 struct ftrace_ops
*ops
)
290 rcu_assign_pointer(ops
->next
, *list
);
293 * We are entering ops into the list but another
294 * CPU might be walking that list. We need to make sure
295 * the ops->next pointer is valid before another CPU sees
296 * the ops pointer included into the list.
298 rcu_assign_pointer(*list
, ops
);
301 static int remove_ftrace_ops(struct ftrace_ops __rcu
**list
,
302 struct ftrace_ops
*ops
)
304 struct ftrace_ops
**p
;
307 * If we are removing the last function, then simply point
308 * to the ftrace_stub.
310 if (rcu_dereference_protected(*list
,
311 lockdep_is_held(&ftrace_lock
)) == ops
&&
312 rcu_dereference_protected(ops
->next
,
313 lockdep_is_held(&ftrace_lock
)) == &ftrace_list_end
) {
314 rcu_assign_pointer(*list
, &ftrace_list_end
);
318 for (p
= list
; *p
!= &ftrace_list_end
; p
= &(*p
)->next
)
329 static void ftrace_update_trampoline(struct ftrace_ops
*ops
);
331 int __register_ftrace_function(struct ftrace_ops
*ops
)
333 if (ops
->flags
& FTRACE_OPS_FL_DELETED
)
336 if (WARN_ON(ops
->flags
& FTRACE_OPS_FL_ENABLED
))
339 #ifndef CONFIG_DYNAMIC_FTRACE_WITH_REGS
341 * If the ftrace_ops specifies SAVE_REGS, then it only can be used
342 * if the arch supports it, or SAVE_REGS_IF_SUPPORTED is also set.
343 * Setting SAVE_REGS_IF_SUPPORTED makes SAVE_REGS irrelevant.
345 if (ops
->flags
& FTRACE_OPS_FL_SAVE_REGS
&&
346 !(ops
->flags
& FTRACE_OPS_FL_SAVE_REGS_IF_SUPPORTED
))
349 if (ops
->flags
& FTRACE_OPS_FL_SAVE_REGS_IF_SUPPORTED
)
350 ops
->flags
|= FTRACE_OPS_FL_SAVE_REGS
;
352 if (!ftrace_enabled
&& (ops
->flags
& FTRACE_OPS_FL_PERMANENT
))
355 if (!is_kernel_core_data((unsigned long)ops
))
356 ops
->flags
|= FTRACE_OPS_FL_DYNAMIC
;
358 add_ftrace_ops(&ftrace_ops_list
, ops
);
360 /* Always save the function, and reset at unregistering */
361 ops
->saved_func
= ops
->func
;
363 if (ftrace_pids_enabled(ops
))
364 ops
->func
= ftrace_pid_func
;
366 ftrace_update_trampoline(ops
);
369 update_ftrace_function();
374 int __unregister_ftrace_function(struct ftrace_ops
*ops
)
378 if (WARN_ON(!(ops
->flags
& FTRACE_OPS_FL_ENABLED
)))
381 ret
= remove_ftrace_ops(&ftrace_ops_list
, ops
);
387 update_ftrace_function();
389 ops
->func
= ops
->saved_func
;
394 static void ftrace_update_pid_func(void)
396 struct ftrace_ops
*op
;
398 /* Only do something if we are tracing something */
399 if (ftrace_trace_function
== ftrace_stub
)
402 do_for_each_ftrace_op(op
, ftrace_ops_list
) {
403 if (op
->flags
& FTRACE_OPS_FL_PID
) {
404 op
->func
= ftrace_pids_enabled(op
) ?
405 ftrace_pid_func
: op
->saved_func
;
406 ftrace_update_trampoline(op
);
408 } while_for_each_ftrace_op(op
);
410 fgraph_update_pid_func();
412 update_ftrace_function();
415 #ifdef CONFIG_FUNCTION_PROFILER
416 struct ftrace_profile
{
417 struct hlist_node node
;
419 unsigned long counter
;
420 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
421 unsigned long long time
;
422 unsigned long long time_squared
;
426 struct ftrace_profile_page
{
427 struct ftrace_profile_page
*next
;
429 struct ftrace_profile records
[];
432 struct ftrace_profile_stat
{
434 struct hlist_head
*hash
;
435 struct ftrace_profile_page
*pages
;
436 struct ftrace_profile_page
*start
;
437 struct tracer_stat stat
;
440 #define PROFILE_RECORDS_SIZE \
441 (PAGE_SIZE - offsetof(struct ftrace_profile_page, records))
443 #define PROFILES_PER_PAGE \
444 (PROFILE_RECORDS_SIZE / sizeof(struct ftrace_profile))
446 static int ftrace_profile_enabled __read_mostly
;
448 /* ftrace_profile_lock - synchronize the enable and disable of the profiler */
449 static DEFINE_MUTEX(ftrace_profile_lock
);
451 static DEFINE_PER_CPU(struct ftrace_profile_stat
, ftrace_profile_stats
);
453 #define FTRACE_PROFILE_HASH_BITS 10
454 #define FTRACE_PROFILE_HASH_SIZE (1 << FTRACE_PROFILE_HASH_BITS)
457 function_stat_next(void *v
, int idx
)
459 struct ftrace_profile
*rec
= v
;
460 struct ftrace_profile_page
*pg
;
462 pg
= (struct ftrace_profile_page
*)((unsigned long)rec
& PAGE_MASK
);
468 if ((void *)rec
>= (void *)&pg
->records
[pg
->index
]) {
472 rec
= &pg
->records
[0];
480 static void *function_stat_start(struct tracer_stat
*trace
)
482 struct ftrace_profile_stat
*stat
=
483 container_of(trace
, struct ftrace_profile_stat
, stat
);
485 if (!stat
|| !stat
->start
)
488 return function_stat_next(&stat
->start
->records
[0], 0);
491 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
492 /* function graph compares on total time */
493 static int function_stat_cmp(const void *p1
, const void *p2
)
495 const struct ftrace_profile
*a
= p1
;
496 const struct ftrace_profile
*b
= p2
;
498 if (a
->time
< b
->time
)
500 if (a
->time
> b
->time
)
506 /* not function graph compares against hits */
507 static int function_stat_cmp(const void *p1
, const void *p2
)
509 const struct ftrace_profile
*a
= p1
;
510 const struct ftrace_profile
*b
= p2
;
512 if (a
->counter
< b
->counter
)
514 if (a
->counter
> b
->counter
)
521 static int function_stat_headers(struct seq_file
*m
)
523 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
524 seq_puts(m
, " Function "
527 "--- ---- --- ---\n");
529 seq_puts(m
, " Function Hit\n"
535 static int function_stat_show(struct seq_file
*m
, void *v
)
537 struct ftrace_profile
*rec
= v
;
538 char str
[KSYM_SYMBOL_LEN
];
540 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
541 static struct trace_seq s
;
542 unsigned long long avg
;
543 unsigned long long stddev
;
545 mutex_lock(&ftrace_profile_lock
);
547 /* we raced with function_profile_reset() */
548 if (unlikely(rec
->counter
== 0)) {
553 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
554 avg
= div64_ul(rec
->time
, rec
->counter
);
555 if (tracing_thresh
&& (avg
< tracing_thresh
))
559 kallsyms_lookup(rec
->ip
, NULL
, NULL
, NULL
, str
);
560 seq_printf(m
, " %-30.30s %10lu", str
, rec
->counter
);
562 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
565 /* Sample standard deviation (s^2) */
566 if (rec
->counter
<= 1)
570 * Apply Welford's method:
571 * s^2 = 1 / (n * (n-1)) * (n * \Sum (x_i)^2 - (\Sum x_i)^2)
573 stddev
= rec
->counter
* rec
->time_squared
-
574 rec
->time
* rec
->time
;
577 * Divide only 1000 for ns^2 -> us^2 conversion.
578 * trace_print_graph_duration will divide 1000 again.
580 stddev
= div64_ul(stddev
,
581 rec
->counter
* (rec
->counter
- 1) * 1000);
585 trace_print_graph_duration(rec
->time
, &s
);
586 trace_seq_puts(&s
, " ");
587 trace_print_graph_duration(avg
, &s
);
588 trace_seq_puts(&s
, " ");
589 trace_print_graph_duration(stddev
, &s
);
590 trace_print_seq(m
, &s
);
594 mutex_unlock(&ftrace_profile_lock
);
599 static void ftrace_profile_reset(struct ftrace_profile_stat
*stat
)
601 struct ftrace_profile_page
*pg
;
603 pg
= stat
->pages
= stat
->start
;
606 memset(pg
->records
, 0, PROFILE_RECORDS_SIZE
);
611 memset(stat
->hash
, 0,
612 FTRACE_PROFILE_HASH_SIZE
* sizeof(struct hlist_head
));
615 static int ftrace_profile_pages_init(struct ftrace_profile_stat
*stat
)
617 struct ftrace_profile_page
*pg
;
622 /* If we already allocated, do nothing */
626 stat
->pages
= (void *)get_zeroed_page(GFP_KERNEL
);
630 #ifdef CONFIG_DYNAMIC_FTRACE
631 functions
= ftrace_update_tot_cnt
;
634 * We do not know the number of functions that exist because
635 * dynamic tracing is what counts them. With past experience
636 * we have around 20K functions. That should be more than enough.
637 * It is highly unlikely we will execute every function in
643 pg
= stat
->start
= stat
->pages
;
645 pages
= DIV_ROUND_UP(functions
, PROFILES_PER_PAGE
);
647 for (i
= 1; i
< pages
; i
++) {
648 pg
->next
= (void *)get_zeroed_page(GFP_KERNEL
);
659 unsigned long tmp
= (unsigned long)pg
;
671 static int ftrace_profile_init_cpu(int cpu
)
673 struct ftrace_profile_stat
*stat
;
676 stat
= &per_cpu(ftrace_profile_stats
, cpu
);
679 /* If the profile is already created, simply reset it */
680 ftrace_profile_reset(stat
);
685 * We are profiling all functions, but usually only a few thousand
686 * functions are hit. We'll make a hash of 1024 items.
688 size
= FTRACE_PROFILE_HASH_SIZE
;
690 stat
->hash
= kcalloc(size
, sizeof(struct hlist_head
), GFP_KERNEL
);
695 /* Preallocate the function profiling pages */
696 if (ftrace_profile_pages_init(stat
) < 0) {
705 static int ftrace_profile_init(void)
710 for_each_possible_cpu(cpu
) {
711 ret
= ftrace_profile_init_cpu(cpu
);
719 /* interrupts must be disabled */
720 static struct ftrace_profile
*
721 ftrace_find_profiled_func(struct ftrace_profile_stat
*stat
, unsigned long ip
)
723 struct ftrace_profile
*rec
;
724 struct hlist_head
*hhd
;
727 key
= hash_long(ip
, FTRACE_PROFILE_HASH_BITS
);
728 hhd
= &stat
->hash
[key
];
730 if (hlist_empty(hhd
))
733 hlist_for_each_entry_rcu_notrace(rec
, hhd
, node
) {
741 static void ftrace_add_profile(struct ftrace_profile_stat
*stat
,
742 struct ftrace_profile
*rec
)
746 key
= hash_long(rec
->ip
, FTRACE_PROFILE_HASH_BITS
);
747 hlist_add_head_rcu(&rec
->node
, &stat
->hash
[key
]);
751 * The memory is already allocated, this simply finds a new record to use.
753 static struct ftrace_profile
*
754 ftrace_profile_alloc(struct ftrace_profile_stat
*stat
, unsigned long ip
)
756 struct ftrace_profile
*rec
= NULL
;
758 /* prevent recursion (from NMIs) */
759 if (atomic_inc_return(&stat
->disabled
) != 1)
763 * Try to find the function again since an NMI
764 * could have added it
766 rec
= ftrace_find_profiled_func(stat
, ip
);
770 if (stat
->pages
->index
== PROFILES_PER_PAGE
) {
771 if (!stat
->pages
->next
)
773 stat
->pages
= stat
->pages
->next
;
776 rec
= &stat
->pages
->records
[stat
->pages
->index
++];
778 ftrace_add_profile(stat
, rec
);
781 atomic_dec(&stat
->disabled
);
787 function_profile_call(unsigned long ip
, unsigned long parent_ip
,
788 struct ftrace_ops
*ops
, struct ftrace_regs
*fregs
)
790 struct ftrace_profile_stat
*stat
;
791 struct ftrace_profile
*rec
;
794 if (!ftrace_profile_enabled
)
797 local_irq_save(flags
);
799 stat
= this_cpu_ptr(&ftrace_profile_stats
);
800 if (!stat
->hash
|| !ftrace_profile_enabled
)
803 rec
= ftrace_find_profiled_func(stat
, ip
);
805 rec
= ftrace_profile_alloc(stat
, ip
);
812 local_irq_restore(flags
);
815 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
816 static bool fgraph_graph_time
= true;
818 void ftrace_graph_graph_time_control(bool enable
)
820 fgraph_graph_time
= enable
;
823 struct profile_fgraph_data
{
824 unsigned long long calltime
;
825 unsigned long long subtime
;
826 unsigned long long sleeptime
;
829 static int profile_graph_entry(struct ftrace_graph_ent
*trace
,
830 struct fgraph_ops
*gops
)
832 struct profile_fgraph_data
*profile_data
;
834 function_profile_call(trace
->func
, 0, NULL
, NULL
);
836 /* If function graph is shutting down, ret_stack can be NULL */
837 if (!current
->ret_stack
)
840 profile_data
= fgraph_reserve_data(gops
->idx
, sizeof(*profile_data
));
844 profile_data
->subtime
= 0;
845 profile_data
->sleeptime
= current
->ftrace_sleeptime
;
846 profile_data
->calltime
= trace_clock_local();
851 static void profile_graph_return(struct ftrace_graph_ret
*trace
,
852 struct fgraph_ops
*gops
)
854 struct profile_fgraph_data
*profile_data
;
855 struct ftrace_profile_stat
*stat
;
856 unsigned long long calltime
;
857 unsigned long long rettime
= trace_clock_local();
858 struct ftrace_profile
*rec
;
862 local_irq_save(flags
);
863 stat
= this_cpu_ptr(&ftrace_profile_stats
);
864 if (!stat
->hash
|| !ftrace_profile_enabled
)
867 profile_data
= fgraph_retrieve_data(gops
->idx
, &size
);
869 /* If the calltime was zero'd ignore it */
870 if (!profile_data
|| !profile_data
->calltime
)
873 calltime
= rettime
- profile_data
->calltime
;
875 if (!fgraph_sleep_time
) {
876 if (current
->ftrace_sleeptime
)
877 calltime
-= current
->ftrace_sleeptime
- profile_data
->sleeptime
;
880 if (!fgraph_graph_time
) {
881 struct profile_fgraph_data
*parent_data
;
883 /* Append this call time to the parent time to subtract */
884 parent_data
= fgraph_retrieve_parent_data(gops
->idx
, &size
, 1);
886 parent_data
->subtime
+= calltime
;
888 if (profile_data
->subtime
&& profile_data
->subtime
< calltime
)
889 calltime
-= profile_data
->subtime
;
894 rec
= ftrace_find_profiled_func(stat
, trace
->func
);
896 rec
->time
+= calltime
;
897 rec
->time_squared
+= calltime
* calltime
;
901 local_irq_restore(flags
);
904 static struct fgraph_ops fprofiler_ops
= {
906 .flags
= FTRACE_OPS_FL_INITIALIZED
,
907 INIT_OPS_HASH(fprofiler_ops
.ops
)
909 .entryfunc
= &profile_graph_entry
,
910 .retfunc
= &profile_graph_return
,
913 static int register_ftrace_profiler(void)
915 return register_ftrace_graph(&fprofiler_ops
);
918 static void unregister_ftrace_profiler(void)
920 unregister_ftrace_graph(&fprofiler_ops
);
923 static struct ftrace_ops ftrace_profile_ops __read_mostly
= {
924 .func
= function_profile_call
,
925 .flags
= FTRACE_OPS_FL_INITIALIZED
,
926 INIT_OPS_HASH(ftrace_profile_ops
)
929 static int register_ftrace_profiler(void)
931 return register_ftrace_function(&ftrace_profile_ops
);
934 static void unregister_ftrace_profiler(void)
936 unregister_ftrace_function(&ftrace_profile_ops
);
938 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
941 ftrace_profile_write(struct file
*filp
, const char __user
*ubuf
,
942 size_t cnt
, loff_t
*ppos
)
947 ret
= kstrtoul_from_user(ubuf
, cnt
, 10, &val
);
953 mutex_lock(&ftrace_profile_lock
);
954 if (ftrace_profile_enabled
^ val
) {
956 ret
= ftrace_profile_init();
962 ret
= register_ftrace_profiler();
967 ftrace_profile_enabled
= 1;
969 ftrace_profile_enabled
= 0;
971 * unregister_ftrace_profiler calls stop_machine
972 * so this acts like an synchronize_rcu.
974 unregister_ftrace_profiler();
978 mutex_unlock(&ftrace_profile_lock
);
986 ftrace_profile_read(struct file
*filp
, char __user
*ubuf
,
987 size_t cnt
, loff_t
*ppos
)
989 char buf
[64]; /* big enough to hold a number */
992 r
= sprintf(buf
, "%u\n", ftrace_profile_enabled
);
993 return simple_read_from_buffer(ubuf
, cnt
, ppos
, buf
, r
);
996 static const struct file_operations ftrace_profile_fops
= {
997 .open
= tracing_open_generic
,
998 .read
= ftrace_profile_read
,
999 .write
= ftrace_profile_write
,
1000 .llseek
= default_llseek
,
1003 /* used to initialize the real stat files */
1004 static struct tracer_stat function_stats __initdata
= {
1005 .name
= "functions",
1006 .stat_start
= function_stat_start
,
1007 .stat_next
= function_stat_next
,
1008 .stat_cmp
= function_stat_cmp
,
1009 .stat_headers
= function_stat_headers
,
1010 .stat_show
= function_stat_show
1013 static __init
void ftrace_profile_tracefs(struct dentry
*d_tracer
)
1015 struct ftrace_profile_stat
*stat
;
1020 for_each_possible_cpu(cpu
) {
1021 stat
= &per_cpu(ftrace_profile_stats
, cpu
);
1023 name
= kasprintf(GFP_KERNEL
, "function%d", cpu
);
1026 * The files created are permanent, if something happens
1027 * we still do not free memory.
1030 "Could not allocate stat file for cpu %d\n",
1034 stat
->stat
= function_stats
;
1035 stat
->stat
.name
= name
;
1036 ret
= register_stat_tracer(&stat
->stat
);
1039 "Could not register function stat for cpu %d\n",
1046 trace_create_file("function_profile_enabled",
1047 TRACE_MODE_WRITE
, d_tracer
, NULL
,
1048 &ftrace_profile_fops
);
1051 #else /* CONFIG_FUNCTION_PROFILER */
1052 static __init
void ftrace_profile_tracefs(struct dentry
*d_tracer
)
1055 #endif /* CONFIG_FUNCTION_PROFILER */
1057 #ifdef CONFIG_DYNAMIC_FTRACE
1059 static struct ftrace_ops
*removed_ops
;
1062 * Set when doing a global update, like enabling all recs or disabling them.
1063 * It is not set when just updating a single ftrace_ops.
1065 static bool update_all_ops
;
1067 #ifndef CONFIG_FTRACE_MCOUNT_RECORD
1068 # error Dynamic ftrace depends on MCOUNT_RECORD
1071 struct ftrace_func_probe
{
1072 struct ftrace_probe_ops
*probe_ops
;
1073 struct ftrace_ops ops
;
1074 struct trace_array
*tr
;
1075 struct list_head list
;
1081 * We make these constant because no one should touch them,
1082 * but they are used as the default "empty hash", to avoid allocating
1083 * it all the time. These are in a read only section such that if
1084 * anyone does try to modify it, it will cause an exception.
1086 static const struct hlist_head empty_buckets
[1];
1087 static const struct ftrace_hash empty_hash
= {
1088 .buckets
= (struct hlist_head
*)empty_buckets
,
1090 #define EMPTY_HASH ((struct ftrace_hash *)&empty_hash)
1092 struct ftrace_ops global_ops
= {
1093 .func
= ftrace_stub
,
1094 .local_hash
.notrace_hash
= EMPTY_HASH
,
1095 .local_hash
.filter_hash
= EMPTY_HASH
,
1096 INIT_OPS_HASH(global_ops
)
1097 .flags
= FTRACE_OPS_FL_INITIALIZED
|
1102 * Used by the stack unwinder to know about dynamic ftrace trampolines.
1104 struct ftrace_ops
*ftrace_ops_trampoline(unsigned long addr
)
1106 struct ftrace_ops
*op
= NULL
;
1109 * Some of the ops may be dynamically allocated,
1110 * they are freed after a synchronize_rcu().
1112 preempt_disable_notrace();
1114 do_for_each_ftrace_op(op
, ftrace_ops_list
) {
1116 * This is to check for dynamically allocated trampolines.
1117 * Trampolines that are in kernel text will have
1118 * core_kernel_text() return true.
1120 if (op
->trampoline
&& op
->trampoline_size
)
1121 if (addr
>= op
->trampoline
&&
1122 addr
< op
->trampoline
+ op
->trampoline_size
) {
1123 preempt_enable_notrace();
1126 } while_for_each_ftrace_op(op
);
1127 preempt_enable_notrace();
1133 * This is used by __kernel_text_address() to return true if the
1134 * address is on a dynamically allocated trampoline that would
1135 * not return true for either core_kernel_text() or
1136 * is_module_text_address().
1138 bool is_ftrace_trampoline(unsigned long addr
)
1140 return ftrace_ops_trampoline(addr
) != NULL
;
1143 struct ftrace_page
{
1144 struct ftrace_page
*next
;
1145 struct dyn_ftrace
*records
;
1150 #define ENTRY_SIZE sizeof(struct dyn_ftrace)
1151 #define ENTRIES_PER_PAGE (PAGE_SIZE / ENTRY_SIZE)
1153 static struct ftrace_page
*ftrace_pages_start
;
1154 static struct ftrace_page
*ftrace_pages
;
1156 static __always_inline
unsigned long
1157 ftrace_hash_key(struct ftrace_hash
*hash
, unsigned long ip
)
1159 if (hash
->size_bits
> 0)
1160 return hash_long(ip
, hash
->size_bits
);
1165 /* Only use this function if ftrace_hash_empty() has already been tested */
1166 static __always_inline
struct ftrace_func_entry
*
1167 __ftrace_lookup_ip(struct ftrace_hash
*hash
, unsigned long ip
)
1170 struct ftrace_func_entry
*entry
;
1171 struct hlist_head
*hhd
;
1173 key
= ftrace_hash_key(hash
, ip
);
1174 hhd
= &hash
->buckets
[key
];
1176 hlist_for_each_entry_rcu_notrace(entry
, hhd
, hlist
) {
1177 if (entry
->ip
== ip
)
1184 * ftrace_lookup_ip - Test to see if an ip exists in an ftrace_hash
1185 * @hash: The hash to look at
1186 * @ip: The instruction pointer to test
1188 * Search a given @hash to see if a given instruction pointer (@ip)
1191 * Returns: the entry that holds the @ip if found. NULL otherwise.
1193 struct ftrace_func_entry
*
1194 ftrace_lookup_ip(struct ftrace_hash
*hash
, unsigned long ip
)
1196 if (ftrace_hash_empty(hash
))
1199 return __ftrace_lookup_ip(hash
, ip
);
1202 static void __add_hash_entry(struct ftrace_hash
*hash
,
1203 struct ftrace_func_entry
*entry
)
1205 struct hlist_head
*hhd
;
1208 key
= ftrace_hash_key(hash
, entry
->ip
);
1209 hhd
= &hash
->buckets
[key
];
1210 hlist_add_head(&entry
->hlist
, hhd
);
1214 static struct ftrace_func_entry
*
1215 add_hash_entry(struct ftrace_hash
*hash
, unsigned long ip
)
1217 struct ftrace_func_entry
*entry
;
1219 entry
= kmalloc(sizeof(*entry
), GFP_KERNEL
);
1224 __add_hash_entry(hash
, entry
);
1230 free_hash_entry(struct ftrace_hash
*hash
,
1231 struct ftrace_func_entry
*entry
)
1233 hlist_del(&entry
->hlist
);
1239 remove_hash_entry(struct ftrace_hash
*hash
,
1240 struct ftrace_func_entry
*entry
)
1242 hlist_del_rcu(&entry
->hlist
);
1246 static void ftrace_hash_clear(struct ftrace_hash
*hash
)
1248 struct hlist_head
*hhd
;
1249 struct hlist_node
*tn
;
1250 struct ftrace_func_entry
*entry
;
1251 int size
= 1 << hash
->size_bits
;
1257 for (i
= 0; i
< size
; i
++) {
1258 hhd
= &hash
->buckets
[i
];
1259 hlist_for_each_entry_safe(entry
, tn
, hhd
, hlist
)
1260 free_hash_entry(hash
, entry
);
1262 FTRACE_WARN_ON(hash
->count
);
1265 static void free_ftrace_mod(struct ftrace_mod_load
*ftrace_mod
)
1267 list_del(&ftrace_mod
->list
);
1268 kfree(ftrace_mod
->module
);
1269 kfree(ftrace_mod
->func
);
1273 static void clear_ftrace_mod_list(struct list_head
*head
)
1275 struct ftrace_mod_load
*p
, *n
;
1277 /* stack tracer isn't supported yet */
1281 mutex_lock(&ftrace_lock
);
1282 list_for_each_entry_safe(p
, n
, head
, list
)
1284 mutex_unlock(&ftrace_lock
);
1287 static void free_ftrace_hash(struct ftrace_hash
*hash
)
1289 if (!hash
|| hash
== EMPTY_HASH
)
1291 ftrace_hash_clear(hash
);
1292 kfree(hash
->buckets
);
1296 static void __free_ftrace_hash_rcu(struct rcu_head
*rcu
)
1298 struct ftrace_hash
*hash
;
1300 hash
= container_of(rcu
, struct ftrace_hash
, rcu
);
1301 free_ftrace_hash(hash
);
1304 static void free_ftrace_hash_rcu(struct ftrace_hash
*hash
)
1306 if (!hash
|| hash
== EMPTY_HASH
)
1308 call_rcu(&hash
->rcu
, __free_ftrace_hash_rcu
);
1312 * ftrace_free_filter - remove all filters for an ftrace_ops
1313 * @ops: the ops to remove the filters from
1315 void ftrace_free_filter(struct ftrace_ops
*ops
)
1317 ftrace_ops_init(ops
);
1318 free_ftrace_hash(ops
->func_hash
->filter_hash
);
1319 free_ftrace_hash(ops
->func_hash
->notrace_hash
);
1321 EXPORT_SYMBOL_GPL(ftrace_free_filter
);
1323 static struct ftrace_hash
*alloc_ftrace_hash(int size_bits
)
1325 struct ftrace_hash
*hash
;
1328 hash
= kzalloc(sizeof(*hash
), GFP_KERNEL
);
1332 size
= 1 << size_bits
;
1333 hash
->buckets
= kcalloc(size
, sizeof(*hash
->buckets
), GFP_KERNEL
);
1335 if (!hash
->buckets
) {
1340 hash
->size_bits
= size_bits
;
1345 /* Used to save filters on functions for modules not loaded yet */
1346 static int ftrace_add_mod(struct trace_array
*tr
,
1347 const char *func
, const char *module
,
1350 struct ftrace_mod_load
*ftrace_mod
;
1351 struct list_head
*mod_head
= enable
? &tr
->mod_trace
: &tr
->mod_notrace
;
1353 ftrace_mod
= kzalloc(sizeof(*ftrace_mod
), GFP_KERNEL
);
1357 INIT_LIST_HEAD(&ftrace_mod
->list
);
1358 ftrace_mod
->func
= kstrdup(func
, GFP_KERNEL
);
1359 ftrace_mod
->module
= kstrdup(module
, GFP_KERNEL
);
1360 ftrace_mod
->enable
= enable
;
1362 if (!ftrace_mod
->func
|| !ftrace_mod
->module
)
1365 list_add(&ftrace_mod
->list
, mod_head
);
1370 free_ftrace_mod(ftrace_mod
);
1375 static struct ftrace_hash
*
1376 alloc_and_copy_ftrace_hash(int size_bits
, struct ftrace_hash
*hash
)
1378 struct ftrace_func_entry
*entry
;
1379 struct ftrace_hash
*new_hash
;
1383 new_hash
= alloc_ftrace_hash(size_bits
);
1388 new_hash
->flags
= hash
->flags
;
1391 if (ftrace_hash_empty(hash
))
1394 size
= 1 << hash
->size_bits
;
1395 for (i
= 0; i
< size
; i
++) {
1396 hlist_for_each_entry(entry
, &hash
->buckets
[i
], hlist
) {
1397 if (add_hash_entry(new_hash
, entry
->ip
) == NULL
)
1402 FTRACE_WARN_ON(new_hash
->count
!= hash
->count
);
1407 free_ftrace_hash(new_hash
);
1411 static void ftrace_hash_rec_disable_modify(struct ftrace_ops
*ops
);
1412 static void ftrace_hash_rec_enable_modify(struct ftrace_ops
*ops
);
1414 static int ftrace_hash_ipmodify_update(struct ftrace_ops
*ops
,
1415 struct ftrace_hash
*new_hash
);
1418 * Allocate a new hash and remove entries from @src and move them to the new hash.
1419 * On success, the @src hash will be empty and should be freed.
1421 static struct ftrace_hash
*__move_hash(struct ftrace_hash
*src
, int size
)
1423 struct ftrace_func_entry
*entry
;
1424 struct ftrace_hash
*new_hash
;
1425 struct hlist_head
*hhd
;
1426 struct hlist_node
*tn
;
1431 * Use around half the size (max bit of it), but
1432 * a minimum of 2 is fine (as size of 0 or 1 both give 1 for bits).
1434 bits
= fls(size
/ 2);
1436 /* Don't allocate too much */
1437 if (bits
> FTRACE_HASH_MAX_BITS
)
1438 bits
= FTRACE_HASH_MAX_BITS
;
1440 new_hash
= alloc_ftrace_hash(bits
);
1444 new_hash
->flags
= src
->flags
;
1446 size
= 1 << src
->size_bits
;
1447 for (i
= 0; i
< size
; i
++) {
1448 hhd
= &src
->buckets
[i
];
1449 hlist_for_each_entry_safe(entry
, tn
, hhd
, hlist
) {
1450 remove_hash_entry(src
, entry
);
1451 __add_hash_entry(new_hash
, entry
);
1457 /* Move the @src entries to a newly allocated hash */
1458 static struct ftrace_hash
*
1459 __ftrace_hash_move(struct ftrace_hash
*src
)
1461 int size
= src
->count
;
1464 * If the new source is empty, just return the empty_hash.
1466 if (ftrace_hash_empty(src
))
1469 return __move_hash(src
, size
);
1473 * ftrace_hash_move - move a new hash to a filter and do updates
1474 * @ops: The ops with the hash that @dst points to
1475 * @enable: True if for the filter hash, false for the notrace hash
1476 * @dst: Points to the @ops hash that should be updated
1477 * @src: The hash to update @dst with
1479 * This is called when an ftrace_ops hash is being updated and the
1480 * the kernel needs to reflect this. Note, this only updates the kernel
1481 * function callbacks if the @ops is enabled (not to be confused with
1482 * @enable above). If the @ops is enabled, its hash determines what
1483 * callbacks get called. This function gets called when the @ops hash
1484 * is updated and it requires new callbacks.
1486 * On success the elements of @src is moved to @dst, and @dst is updated
1487 * properly, as well as the functions determined by the @ops hashes
1488 * are now calling the @ops callback function.
1490 * Regardless of return type, @src should be freed with free_ftrace_hash().
1493 ftrace_hash_move(struct ftrace_ops
*ops
, int enable
,
1494 struct ftrace_hash
**dst
, struct ftrace_hash
*src
)
1496 struct ftrace_hash
*new_hash
;
1499 /* Reject setting notrace hash on IPMODIFY ftrace_ops */
1500 if (ops
->flags
& FTRACE_OPS_FL_IPMODIFY
&& !enable
)
1503 new_hash
= __ftrace_hash_move(src
);
1507 /* Make sure this can be applied if it is IPMODIFY ftrace_ops */
1509 /* IPMODIFY should be updated only when filter_hash updating */
1510 ret
= ftrace_hash_ipmodify_update(ops
, new_hash
);
1512 free_ftrace_hash(new_hash
);
1518 * Remove the current set, update the hash and add
1521 ftrace_hash_rec_disable_modify(ops
);
1523 rcu_assign_pointer(*dst
, new_hash
);
1525 ftrace_hash_rec_enable_modify(ops
);
1530 static bool hash_contains_ip(unsigned long ip
,
1531 struct ftrace_ops_hash
*hash
)
1534 * The function record is a match if it exists in the filter
1535 * hash and not in the notrace hash. Note, an empty hash is
1536 * considered a match for the filter hash, but an empty
1537 * notrace hash is considered not in the notrace hash.
1539 return (ftrace_hash_empty(hash
->filter_hash
) ||
1540 __ftrace_lookup_ip(hash
->filter_hash
, ip
)) &&
1541 (ftrace_hash_empty(hash
->notrace_hash
) ||
1542 !__ftrace_lookup_ip(hash
->notrace_hash
, ip
));
1546 * Test the hashes for this ops to see if we want to call
1547 * the ops->func or not.
1549 * It's a match if the ip is in the ops->filter_hash or
1550 * the filter_hash does not exist or is empty,
1552 * the ip is not in the ops->notrace_hash.
1554 * This needs to be called with preemption disabled as
1555 * the hashes are freed with call_rcu().
1558 ftrace_ops_test(struct ftrace_ops
*ops
, unsigned long ip
, void *regs
)
1560 struct ftrace_ops_hash hash
;
1563 #ifdef CONFIG_DYNAMIC_FTRACE_WITH_REGS
1565 * There's a small race when adding ops that the ftrace handler
1566 * that wants regs, may be called without them. We can not
1567 * allow that handler to be called if regs is NULL.
1569 if (regs
== NULL
&& (ops
->flags
& FTRACE_OPS_FL_SAVE_REGS
))
1573 rcu_assign_pointer(hash
.filter_hash
, ops
->func_hash
->filter_hash
);
1574 rcu_assign_pointer(hash
.notrace_hash
, ops
->func_hash
->notrace_hash
);
1576 if (hash_contains_ip(ip
, &hash
))
1585 * This is a double for. Do not use 'break' to break out of the loop,
1586 * you must use a goto.
1588 #define do_for_each_ftrace_rec(pg, rec) \
1589 for (pg = ftrace_pages_start; pg; pg = pg->next) { \
1591 for (_____i = 0; _____i < pg->index; _____i++) { \
1592 rec = &pg->records[_____i];
1594 #define while_for_each_ftrace_rec() \
1599 static int ftrace_cmp_recs(const void *a
, const void *b
)
1601 const struct dyn_ftrace
*key
= a
;
1602 const struct dyn_ftrace
*rec
= b
;
1604 if (key
->flags
< rec
->ip
)
1606 if (key
->ip
>= rec
->ip
+ MCOUNT_INSN_SIZE
)
1611 static struct dyn_ftrace
*lookup_rec(unsigned long start
, unsigned long end
)
1613 struct ftrace_page
*pg
;
1614 struct dyn_ftrace
*rec
= NULL
;
1615 struct dyn_ftrace key
;
1618 key
.flags
= end
; /* overload flags, as it is unsigned long */
1620 for (pg
= ftrace_pages_start
; pg
; pg
= pg
->next
) {
1621 if (pg
->index
== 0 ||
1622 end
< pg
->records
[0].ip
||
1623 start
>= (pg
->records
[pg
->index
- 1].ip
+ MCOUNT_INSN_SIZE
))
1625 rec
= bsearch(&key
, pg
->records
, pg
->index
,
1626 sizeof(struct dyn_ftrace
),
1635 * ftrace_location_range - return the first address of a traced location
1636 * if it touches the given ip range
1637 * @start: start of range to search.
1638 * @end: end of range to search (inclusive). @end points to the last byte
1641 * Returns: rec->ip if the related ftrace location is a least partly within
1642 * the given address range. That is, the first address of the instruction
1643 * that is either a NOP or call to the function tracer. It checks the ftrace
1644 * internal tables to determine if the address belongs or not.
1646 unsigned long ftrace_location_range(unsigned long start
, unsigned long end
)
1648 struct dyn_ftrace
*rec
;
1649 unsigned long ip
= 0;
1652 rec
= lookup_rec(start
, end
);
1661 * ftrace_location - return the ftrace location
1662 * @ip: the instruction pointer to check
1665 * * If @ip matches the ftrace location, return @ip.
1666 * * If @ip matches sym+0, return sym's ftrace location.
1667 * * Otherwise, return 0.
1669 unsigned long ftrace_location(unsigned long ip
)
1672 unsigned long offset
;
1675 loc
= ftrace_location_range(ip
, ip
);
1677 if (!kallsyms_lookup_size_offset(ip
, &size
, &offset
))
1680 /* map sym+0 to __fentry__ */
1682 loc
= ftrace_location_range(ip
, ip
+ size
- 1);
1690 * ftrace_text_reserved - return true if range contains an ftrace location
1691 * @start: start of range to search
1692 * @end: end of range to search (inclusive). @end points to the last byte to check.
1694 * Returns: 1 if @start and @end contains a ftrace location.
1695 * That is, the instruction that is either a NOP or call to
1696 * the function tracer. It checks the ftrace internal tables to
1697 * determine if the address belongs or not.
1699 int ftrace_text_reserved(const void *start
, const void *end
)
1703 ret
= ftrace_location_range((unsigned long)start
,
1704 (unsigned long)end
);
1709 /* Test if ops registered to this rec needs regs */
1710 static bool test_rec_ops_needs_regs(struct dyn_ftrace
*rec
)
1712 struct ftrace_ops
*ops
;
1713 bool keep_regs
= false;
1715 for (ops
= ftrace_ops_list
;
1716 ops
!= &ftrace_list_end
; ops
= ops
->next
) {
1717 /* pass rec in as regs to have non-NULL val */
1718 if (ftrace_ops_test(ops
, rec
->ip
, rec
)) {
1719 if (ops
->flags
& FTRACE_OPS_FL_SAVE_REGS
) {
1729 static struct ftrace_ops
*
1730 ftrace_find_tramp_ops_any(struct dyn_ftrace
*rec
);
1731 static struct ftrace_ops
*
1732 ftrace_find_tramp_ops_any_other(struct dyn_ftrace
*rec
, struct ftrace_ops
*op_exclude
);
1733 static struct ftrace_ops
*
1734 ftrace_find_tramp_ops_next(struct dyn_ftrace
*rec
, struct ftrace_ops
*ops
);
1736 static bool skip_record(struct dyn_ftrace
*rec
)
1739 * At boot up, weak functions are set to disable. Function tracing
1740 * can be enabled before they are, and they still need to be disabled now.
1741 * If the record is disabled, still continue if it is marked as already
1742 * enabled (this is needed to keep the accounting working).
1744 return rec
->flags
& FTRACE_FL_DISABLED
&&
1745 !(rec
->flags
& FTRACE_FL_ENABLED
);
1749 * This is the main engine to the ftrace updates to the dyn_ftrace records.
1751 * It will iterate through all the available ftrace functions
1752 * (the ones that ftrace can have callbacks to) and set the flags
1753 * in the associated dyn_ftrace records.
1755 * @inc: If true, the functions associated to @ops are added to
1756 * the dyn_ftrace records, otherwise they are removed.
1758 static bool __ftrace_hash_rec_update(struct ftrace_ops
*ops
,
1761 struct ftrace_hash
*hash
;
1762 struct ftrace_hash
*notrace_hash
;
1763 struct ftrace_page
*pg
;
1764 struct dyn_ftrace
*rec
;
1765 bool update
= false;
1769 /* Only update if the ops has been registered */
1770 if (!(ops
->flags
& FTRACE_OPS_FL_ENABLED
))
1774 * If the count is zero, we update all records.
1775 * Otherwise we just update the items in the hash.
1777 hash
= ops
->func_hash
->filter_hash
;
1778 notrace_hash
= ops
->func_hash
->notrace_hash
;
1779 if (ftrace_hash_empty(hash
))
1782 do_for_each_ftrace_rec(pg
, rec
) {
1783 int in_notrace_hash
= 0;
1787 if (skip_record(rec
))
1792 * Only the filter_hash affects all records.
1793 * Update if the record is not in the notrace hash.
1795 if (!notrace_hash
|| !ftrace_lookup_ip(notrace_hash
, rec
->ip
))
1798 in_hash
= !!ftrace_lookup_ip(hash
, rec
->ip
);
1799 in_notrace_hash
= !!ftrace_lookup_ip(notrace_hash
, rec
->ip
);
1802 * We want to match all functions that are in the hash but
1803 * not in the other hash.
1805 if (in_hash
&& !in_notrace_hash
)
1813 if (FTRACE_WARN_ON(ftrace_rec_count(rec
) == FTRACE_REF_MAX
))
1816 if (ops
->flags
& FTRACE_OPS_FL_DIRECT
)
1817 rec
->flags
|= FTRACE_FL_DIRECT
;
1820 * If there's only a single callback registered to a
1821 * function, and the ops has a trampoline registered
1822 * for it, then we can call it directly.
1824 if (ftrace_rec_count(rec
) == 1 && ops
->trampoline
)
1825 rec
->flags
|= FTRACE_FL_TRAMP
;
1828 * If we are adding another function callback
1829 * to this function, and the previous had a
1830 * custom trampoline in use, then we need to go
1831 * back to the default trampoline.
1833 rec
->flags
&= ~FTRACE_FL_TRAMP
;
1836 * If any ops wants regs saved for this function
1837 * then all ops will get saved regs.
1839 if (ops
->flags
& FTRACE_OPS_FL_SAVE_REGS
)
1840 rec
->flags
|= FTRACE_FL_REGS
;
1842 if (FTRACE_WARN_ON(ftrace_rec_count(rec
) == 0))
1847 * Only the internal direct_ops should have the
1848 * DIRECT flag set. Thus, if it is removing a
1849 * function, then that function should no longer
1852 if (ops
->flags
& FTRACE_OPS_FL_DIRECT
)
1853 rec
->flags
&= ~FTRACE_FL_DIRECT
;
1856 * If the rec had REGS enabled and the ops that is
1857 * being removed had REGS set, then see if there is
1858 * still any ops for this record that wants regs.
1859 * If not, we can stop recording them.
1861 if (ftrace_rec_count(rec
) > 0 &&
1862 rec
->flags
& FTRACE_FL_REGS
&&
1863 ops
->flags
& FTRACE_OPS_FL_SAVE_REGS
) {
1864 if (!test_rec_ops_needs_regs(rec
))
1865 rec
->flags
&= ~FTRACE_FL_REGS
;
1869 * The TRAMP needs to be set only if rec count
1870 * is decremented to one, and the ops that is
1871 * left has a trampoline. As TRAMP can only be
1872 * enabled if there is only a single ops attached
1875 if (ftrace_rec_count(rec
) == 1 &&
1876 ftrace_find_tramp_ops_any_other(rec
, ops
))
1877 rec
->flags
|= FTRACE_FL_TRAMP
;
1879 rec
->flags
&= ~FTRACE_FL_TRAMP
;
1882 * flags will be cleared in ftrace_check_record()
1883 * if rec count is zero.
1888 * If the rec has a single associated ops, and ops->func can be
1889 * called directly, allow the call site to call via the ops.
1891 if (IS_ENABLED(CONFIG_DYNAMIC_FTRACE_WITH_CALL_OPS
) &&
1892 ftrace_rec_count(rec
) == 1 &&
1893 ftrace_ops_get_func(ops
) == ops
->func
)
1894 rec
->flags
|= FTRACE_FL_CALL_OPS
;
1896 rec
->flags
&= ~FTRACE_FL_CALL_OPS
;
1900 /* Must match FTRACE_UPDATE_CALLS in ftrace_modify_all_code() */
1901 update
|= ftrace_test_record(rec
, true) != FTRACE_UPDATE_IGNORE
;
1903 /* Shortcut, if we handled all records, we are done. */
1904 if (!all
&& count
== hash
->count
)
1906 } while_for_each_ftrace_rec();
1912 * This is called when an ops is removed from tracing. It will decrement
1913 * the counters of the dyn_ftrace records for all the functions that
1914 * the @ops attached to.
1916 static bool ftrace_hash_rec_disable(struct ftrace_ops
*ops
)
1918 return __ftrace_hash_rec_update(ops
, false);
1922 * This is called when an ops is added to tracing. It will increment
1923 * the counters of the dyn_ftrace records for all the functions that
1924 * the @ops attached to.
1926 static bool ftrace_hash_rec_enable(struct ftrace_ops
*ops
)
1928 return __ftrace_hash_rec_update(ops
, true);
1932 * This function will update what functions @ops traces when its filter
1935 * The @inc states if the @ops callbacks are going to be added or removed.
1936 * When one of the @ops hashes are updated to a "new_hash" the dyn_ftrace
1937 * records are update via:
1939 * ftrace_hash_rec_disable_modify(ops);
1940 * ops->hash = new_hash
1941 * ftrace_hash_rec_enable_modify(ops);
1943 * Where the @ops is removed from all the records it is tracing using
1944 * its old hash. The @ops hash is updated to the new hash, and then
1945 * the @ops is added back to the records so that it is tracing all
1946 * the new functions.
1948 static void ftrace_hash_rec_update_modify(struct ftrace_ops
*ops
, bool inc
)
1950 struct ftrace_ops
*op
;
1952 __ftrace_hash_rec_update(ops
, inc
);
1954 if (ops
->func_hash
!= &global_ops
.local_hash
)
1958 * If the ops shares the global_ops hash, then we need to update
1959 * all ops that are enabled and use this hash.
1961 do_for_each_ftrace_op(op
, ftrace_ops_list
) {
1965 if (op
->func_hash
== &global_ops
.local_hash
)
1966 __ftrace_hash_rec_update(op
, inc
);
1967 } while_for_each_ftrace_op(op
);
1970 static void ftrace_hash_rec_disable_modify(struct ftrace_ops
*ops
)
1972 ftrace_hash_rec_update_modify(ops
, false);
1975 static void ftrace_hash_rec_enable_modify(struct ftrace_ops
*ops
)
1977 ftrace_hash_rec_update_modify(ops
, true);
1981 * Try to update IPMODIFY flag on each ftrace_rec. Return 0 if it is OK
1982 * or no-needed to update, -EBUSY if it detects a conflict of the flag
1983 * on a ftrace_rec, and -EINVAL if the new_hash tries to trace all recs.
1984 * Note that old_hash and new_hash has below meanings
1985 * - If the hash is NULL, it hits all recs (if IPMODIFY is set, this is rejected)
1986 * - If the hash is EMPTY_HASH, it hits nothing
1987 * - Anything else hits the recs which match the hash entries.
1989 * DIRECT ops does not have IPMODIFY flag, but we still need to check it
1990 * against functions with FTRACE_FL_IPMODIFY. If there is any overlap, call
1991 * ops_func(SHARE_IPMODIFY_SELF) to make sure current ops can share with
1992 * IPMODIFY. If ops_func(SHARE_IPMODIFY_SELF) returns non-zero, propagate
1993 * the return value to the caller and eventually to the owner of the DIRECT
1996 static int __ftrace_hash_update_ipmodify(struct ftrace_ops
*ops
,
1997 struct ftrace_hash
*old_hash
,
1998 struct ftrace_hash
*new_hash
)
2000 struct ftrace_page
*pg
;
2001 struct dyn_ftrace
*rec
, *end
= NULL
;
2003 bool is_ipmodify
, is_direct
;
2005 /* Only update if the ops has been registered */
2006 if (!(ops
->flags
& FTRACE_OPS_FL_ENABLED
))
2009 is_ipmodify
= ops
->flags
& FTRACE_OPS_FL_IPMODIFY
;
2010 is_direct
= ops
->flags
& FTRACE_OPS_FL_DIRECT
;
2012 /* neither IPMODIFY nor DIRECT, skip */
2013 if (!is_ipmodify
&& !is_direct
)
2016 if (WARN_ON_ONCE(is_ipmodify
&& is_direct
))
2020 * Since the IPMODIFY and DIRECT are very address sensitive
2021 * actions, we do not allow ftrace_ops to set all functions to new
2024 if (!new_hash
|| !old_hash
)
2027 /* Update rec->flags */
2028 do_for_each_ftrace_rec(pg
, rec
) {
2030 if (rec
->flags
& FTRACE_FL_DISABLED
)
2033 /* We need to update only differences of filter_hash */
2034 in_old
= !!ftrace_lookup_ip(old_hash
, rec
->ip
);
2035 in_new
= !!ftrace_lookup_ip(new_hash
, rec
->ip
);
2036 if (in_old
== in_new
)
2040 if (rec
->flags
& FTRACE_FL_IPMODIFY
) {
2043 /* Cannot have two ipmodify on same rec */
2047 FTRACE_WARN_ON(rec
->flags
& FTRACE_FL_DIRECT
);
2050 * Another ops with IPMODIFY is already
2051 * attached. We are now attaching a direct
2052 * ops. Run SHARE_IPMODIFY_SELF, to check
2053 * whether sharing is supported.
2057 ret
= ops
->ops_func(ops
, FTRACE_OPS_CMD_ENABLE_SHARE_IPMODIFY_SELF
);
2060 } else if (is_ipmodify
) {
2061 rec
->flags
|= FTRACE_FL_IPMODIFY
;
2063 } else if (is_ipmodify
) {
2064 rec
->flags
&= ~FTRACE_FL_IPMODIFY
;
2066 } while_for_each_ftrace_rec();
2073 /* Roll back what we did above */
2074 do_for_each_ftrace_rec(pg
, rec
) {
2076 if (rec
->flags
& FTRACE_FL_DISABLED
)
2082 in_old
= !!ftrace_lookup_ip(old_hash
, rec
->ip
);
2083 in_new
= !!ftrace_lookup_ip(new_hash
, rec
->ip
);
2084 if (in_old
== in_new
)
2088 rec
->flags
&= ~FTRACE_FL_IPMODIFY
;
2090 rec
->flags
|= FTRACE_FL_IPMODIFY
;
2091 } while_for_each_ftrace_rec();
2097 static int ftrace_hash_ipmodify_enable(struct ftrace_ops
*ops
)
2099 struct ftrace_hash
*hash
= ops
->func_hash
->filter_hash
;
2101 if (ftrace_hash_empty(hash
))
2104 return __ftrace_hash_update_ipmodify(ops
, EMPTY_HASH
, hash
);
2107 /* Disabling always succeeds */
2108 static void ftrace_hash_ipmodify_disable(struct ftrace_ops
*ops
)
2110 struct ftrace_hash
*hash
= ops
->func_hash
->filter_hash
;
2112 if (ftrace_hash_empty(hash
))
2115 __ftrace_hash_update_ipmodify(ops
, hash
, EMPTY_HASH
);
2118 static int ftrace_hash_ipmodify_update(struct ftrace_ops
*ops
,
2119 struct ftrace_hash
*new_hash
)
2121 struct ftrace_hash
*old_hash
= ops
->func_hash
->filter_hash
;
2123 if (ftrace_hash_empty(old_hash
))
2126 if (ftrace_hash_empty(new_hash
))
2129 return __ftrace_hash_update_ipmodify(ops
, old_hash
, new_hash
);
2132 static void print_ip_ins(const char *fmt
, const unsigned char *p
)
2134 char ins
[MCOUNT_INSN_SIZE
];
2136 if (copy_from_kernel_nofault(ins
, p
, MCOUNT_INSN_SIZE
)) {
2137 printk(KERN_CONT
"%s[FAULT] %px\n", fmt
, p
);
2141 printk(KERN_CONT
"%s", fmt
);
2142 pr_cont("%*phC", MCOUNT_INSN_SIZE
, ins
);
2145 enum ftrace_bug_type ftrace_bug_type
;
2146 const void *ftrace_expected
;
2148 static void print_bug_type(void)
2150 switch (ftrace_bug_type
) {
2151 case FTRACE_BUG_UNKNOWN
:
2153 case FTRACE_BUG_INIT
:
2154 pr_info("Initializing ftrace call sites\n");
2156 case FTRACE_BUG_NOP
:
2157 pr_info("Setting ftrace call site to NOP\n");
2159 case FTRACE_BUG_CALL
:
2160 pr_info("Setting ftrace call site to call ftrace function\n");
2162 case FTRACE_BUG_UPDATE
:
2163 pr_info("Updating ftrace call site to call a different ftrace function\n");
2169 * ftrace_bug - report and shutdown function tracer
2170 * @failed: The failed type (EFAULT, EINVAL, EPERM)
2171 * @rec: The record that failed
2173 * The arch code that enables or disables the function tracing
2174 * can call ftrace_bug() when it has detected a problem in
2175 * modifying the code. @failed should be one of either:
2176 * EFAULT - if the problem happens on reading the @ip address
2177 * EINVAL - if what is read at @ip is not what was expected
2178 * EPERM - if the problem happens on writing to the @ip address
2180 void ftrace_bug(int failed
, struct dyn_ftrace
*rec
)
2182 unsigned long ip
= rec
? rec
->ip
: 0;
2184 pr_info("------------[ ftrace bug ]------------\n");
2188 pr_info("ftrace faulted on modifying ");
2189 print_ip_sym(KERN_INFO
, ip
);
2192 pr_info("ftrace failed to modify ");
2193 print_ip_sym(KERN_INFO
, ip
);
2194 print_ip_ins(" actual: ", (unsigned char *)ip
);
2196 if (ftrace_expected
) {
2197 print_ip_ins(" expected: ", ftrace_expected
);
2202 pr_info("ftrace faulted on writing ");
2203 print_ip_sym(KERN_INFO
, ip
);
2206 pr_info("ftrace faulted on unknown error ");
2207 print_ip_sym(KERN_INFO
, ip
);
2211 struct ftrace_ops
*ops
= NULL
;
2213 pr_info("ftrace record flags: %lx\n", rec
->flags
);
2214 pr_cont(" (%ld)%s%s", ftrace_rec_count(rec
),
2215 rec
->flags
& FTRACE_FL_REGS
? " R" : " ",
2216 rec
->flags
& FTRACE_FL_CALL_OPS
? " O" : " ");
2217 if (rec
->flags
& FTRACE_FL_TRAMP_EN
) {
2218 ops
= ftrace_find_tramp_ops_any(rec
);
2221 pr_cont("\ttramp: %pS (%pS)",
2222 (void *)ops
->trampoline
,
2224 ops
= ftrace_find_tramp_ops_next(rec
, ops
);
2227 pr_cont("\ttramp: ERROR!");
2230 ip
= ftrace_get_addr_curr(rec
);
2231 pr_cont("\n expected tramp: %lx\n", ip
);
2234 FTRACE_WARN_ON_ONCE(1);
2237 static int ftrace_check_record(struct dyn_ftrace
*rec
, bool enable
, bool update
)
2239 unsigned long flag
= 0UL;
2241 ftrace_bug_type
= FTRACE_BUG_UNKNOWN
;
2243 if (skip_record(rec
))
2244 return FTRACE_UPDATE_IGNORE
;
2247 * If we are updating calls:
2249 * If the record has a ref count, then we need to enable it
2250 * because someone is using it.
2252 * Otherwise we make sure its disabled.
2254 * If we are disabling calls, then disable all records that
2257 if (enable
&& ftrace_rec_count(rec
))
2258 flag
= FTRACE_FL_ENABLED
;
2261 * If enabling and the REGS flag does not match the REGS_EN, or
2262 * the TRAMP flag doesn't match the TRAMP_EN, then do not ignore
2263 * this record. Set flags to fail the compare against ENABLED.
2264 * Same for direct calls.
2267 if (!(rec
->flags
& FTRACE_FL_REGS
) !=
2268 !(rec
->flags
& FTRACE_FL_REGS_EN
))
2269 flag
|= FTRACE_FL_REGS
;
2271 if (!(rec
->flags
& FTRACE_FL_TRAMP
) !=
2272 !(rec
->flags
& FTRACE_FL_TRAMP_EN
))
2273 flag
|= FTRACE_FL_TRAMP
;
2276 * Direct calls are special, as count matters.
2277 * We must test the record for direct, if the
2278 * DIRECT and DIRECT_EN do not match, but only
2279 * if the count is 1. That's because, if the
2280 * count is something other than one, we do not
2281 * want the direct enabled (it will be done via the
2282 * direct helper). But if DIRECT_EN is set, and
2283 * the count is not one, we need to clear it.
2286 if (ftrace_rec_count(rec
) == 1) {
2287 if (!(rec
->flags
& FTRACE_FL_DIRECT
) !=
2288 !(rec
->flags
& FTRACE_FL_DIRECT_EN
))
2289 flag
|= FTRACE_FL_DIRECT
;
2290 } else if (rec
->flags
& FTRACE_FL_DIRECT_EN
) {
2291 flag
|= FTRACE_FL_DIRECT
;
2295 * Ops calls are special, as count matters.
2296 * As with direct calls, they must only be enabled when count
2297 * is one, otherwise they'll be handled via the list ops.
2299 if (ftrace_rec_count(rec
) == 1) {
2300 if (!(rec
->flags
& FTRACE_FL_CALL_OPS
) !=
2301 !(rec
->flags
& FTRACE_FL_CALL_OPS_EN
))
2302 flag
|= FTRACE_FL_CALL_OPS
;
2303 } else if (rec
->flags
& FTRACE_FL_CALL_OPS_EN
) {
2304 flag
|= FTRACE_FL_CALL_OPS
;
2308 /* If the state of this record hasn't changed, then do nothing */
2309 if ((rec
->flags
& FTRACE_FL_ENABLED
) == flag
)
2310 return FTRACE_UPDATE_IGNORE
;
2313 /* Save off if rec is being enabled (for return value) */
2314 flag
^= rec
->flags
& FTRACE_FL_ENABLED
;
2317 rec
->flags
|= FTRACE_FL_ENABLED
| FTRACE_FL_TOUCHED
;
2318 if (flag
& FTRACE_FL_REGS
) {
2319 if (rec
->flags
& FTRACE_FL_REGS
)
2320 rec
->flags
|= FTRACE_FL_REGS_EN
;
2322 rec
->flags
&= ~FTRACE_FL_REGS_EN
;
2324 if (flag
& FTRACE_FL_TRAMP
) {
2325 if (rec
->flags
& FTRACE_FL_TRAMP
)
2326 rec
->flags
|= FTRACE_FL_TRAMP_EN
;
2328 rec
->flags
&= ~FTRACE_FL_TRAMP_EN
;
2331 /* Keep track of anything that modifies the function */
2332 if (rec
->flags
& (FTRACE_FL_DIRECT
| FTRACE_FL_IPMODIFY
))
2333 rec
->flags
|= FTRACE_FL_MODIFIED
;
2335 if (flag
& FTRACE_FL_DIRECT
) {
2337 * If there's only one user (direct_ops helper)
2338 * then we can call the direct function
2339 * directly (no ftrace trampoline).
2341 if (ftrace_rec_count(rec
) == 1) {
2342 if (rec
->flags
& FTRACE_FL_DIRECT
)
2343 rec
->flags
|= FTRACE_FL_DIRECT_EN
;
2345 rec
->flags
&= ~FTRACE_FL_DIRECT_EN
;
2348 * Can only call directly if there's
2349 * only one callback to the function.
2351 rec
->flags
&= ~FTRACE_FL_DIRECT_EN
;
2355 if (flag
& FTRACE_FL_CALL_OPS
) {
2356 if (ftrace_rec_count(rec
) == 1) {
2357 if (rec
->flags
& FTRACE_FL_CALL_OPS
)
2358 rec
->flags
|= FTRACE_FL_CALL_OPS_EN
;
2360 rec
->flags
&= ~FTRACE_FL_CALL_OPS_EN
;
2363 * Can only call directly if there's
2364 * only one set of associated ops.
2366 rec
->flags
&= ~FTRACE_FL_CALL_OPS_EN
;
2372 * If this record is being updated from a nop, then
2373 * return UPDATE_MAKE_CALL.
2375 * return UPDATE_MODIFY_CALL to tell the caller to convert
2376 * from the save regs, to a non-save regs function or
2377 * vice versa, or from a trampoline call.
2379 if (flag
& FTRACE_FL_ENABLED
) {
2380 ftrace_bug_type
= FTRACE_BUG_CALL
;
2381 return FTRACE_UPDATE_MAKE_CALL
;
2384 ftrace_bug_type
= FTRACE_BUG_UPDATE
;
2385 return FTRACE_UPDATE_MODIFY_CALL
;
2389 /* If there's no more users, clear all flags */
2390 if (!ftrace_rec_count(rec
))
2391 rec
->flags
&= FTRACE_NOCLEAR_FLAGS
;
2394 * Just disable the record, but keep the ops TRAMP
2395 * and REGS states. The _EN flags must be disabled though.
2397 rec
->flags
&= ~(FTRACE_FL_ENABLED
| FTRACE_FL_TRAMP_EN
|
2398 FTRACE_FL_REGS_EN
| FTRACE_FL_DIRECT_EN
|
2399 FTRACE_FL_CALL_OPS_EN
);
2402 ftrace_bug_type
= FTRACE_BUG_NOP
;
2403 return FTRACE_UPDATE_MAKE_NOP
;
2407 * ftrace_update_record - set a record that now is tracing or not
2408 * @rec: the record to update
2409 * @enable: set to true if the record is tracing, false to force disable
2411 * The records that represent all functions that can be traced need
2412 * to be updated when tracing has been enabled.
2414 int ftrace_update_record(struct dyn_ftrace
*rec
, bool enable
)
2416 return ftrace_check_record(rec
, enable
, true);
2420 * ftrace_test_record - check if the record has been enabled or not
2421 * @rec: the record to test
2422 * @enable: set to true to check if enabled, false if it is disabled
2424 * The arch code may need to test if a record is already set to
2425 * tracing to determine how to modify the function code that it
2428 int ftrace_test_record(struct dyn_ftrace
*rec
, bool enable
)
2430 return ftrace_check_record(rec
, enable
, false);
2433 static struct ftrace_ops
*
2434 ftrace_find_tramp_ops_any(struct dyn_ftrace
*rec
)
2436 struct ftrace_ops
*op
;
2437 unsigned long ip
= rec
->ip
;
2439 do_for_each_ftrace_op(op
, ftrace_ops_list
) {
2441 if (!op
->trampoline
)
2444 if (hash_contains_ip(ip
, op
->func_hash
))
2446 } while_for_each_ftrace_op(op
);
2451 static struct ftrace_ops
*
2452 ftrace_find_tramp_ops_any_other(struct dyn_ftrace
*rec
, struct ftrace_ops
*op_exclude
)
2454 struct ftrace_ops
*op
;
2455 unsigned long ip
= rec
->ip
;
2457 do_for_each_ftrace_op(op
, ftrace_ops_list
) {
2459 if (op
== op_exclude
|| !op
->trampoline
)
2462 if (hash_contains_ip(ip
, op
->func_hash
))
2464 } while_for_each_ftrace_op(op
);
2469 static struct ftrace_ops
*
2470 ftrace_find_tramp_ops_next(struct dyn_ftrace
*rec
,
2471 struct ftrace_ops
*op
)
2473 unsigned long ip
= rec
->ip
;
2475 while_for_each_ftrace_op(op
) {
2477 if (!op
->trampoline
)
2480 if (hash_contains_ip(ip
, op
->func_hash
))
2487 static struct ftrace_ops
*
2488 ftrace_find_tramp_ops_curr(struct dyn_ftrace
*rec
)
2490 struct ftrace_ops
*op
;
2491 unsigned long ip
= rec
->ip
;
2494 * Need to check removed ops first.
2495 * If they are being removed, and this rec has a tramp,
2496 * and this rec is in the ops list, then it would be the
2497 * one with the tramp.
2500 if (hash_contains_ip(ip
, &removed_ops
->old_hash
))
2505 * Need to find the current trampoline for a rec.
2506 * Now, a trampoline is only attached to a rec if there
2507 * was a single 'ops' attached to it. But this can be called
2508 * when we are adding another op to the rec or removing the
2509 * current one. Thus, if the op is being added, we can
2510 * ignore it because it hasn't attached itself to the rec
2513 * If an ops is being modified (hooking to different functions)
2514 * then we don't care about the new functions that are being
2515 * added, just the old ones (that are probably being removed).
2517 * If we are adding an ops to a function that already is using
2518 * a trampoline, it needs to be removed (trampolines are only
2519 * for single ops connected), then an ops that is not being
2520 * modified also needs to be checked.
2522 do_for_each_ftrace_op(op
, ftrace_ops_list
) {
2524 if (!op
->trampoline
)
2528 * If the ops is being added, it hasn't gotten to
2529 * the point to be removed from this tree yet.
2531 if (op
->flags
& FTRACE_OPS_FL_ADDING
)
2536 * If the ops is being modified and is in the old
2537 * hash, then it is probably being removed from this
2540 if ((op
->flags
& FTRACE_OPS_FL_MODIFYING
) &&
2541 hash_contains_ip(ip
, &op
->old_hash
))
2544 * If the ops is not being added or modified, and it's
2545 * in its normal filter hash, then this must be the one
2548 if (!(op
->flags
& FTRACE_OPS_FL_MODIFYING
) &&
2549 hash_contains_ip(ip
, op
->func_hash
))
2552 } while_for_each_ftrace_op(op
);
2557 static struct ftrace_ops
*
2558 ftrace_find_tramp_ops_new(struct dyn_ftrace
*rec
)
2560 struct ftrace_ops
*op
;
2561 unsigned long ip
= rec
->ip
;
2563 do_for_each_ftrace_op(op
, ftrace_ops_list
) {
2564 /* pass rec in as regs to have non-NULL val */
2565 if (hash_contains_ip(ip
, op
->func_hash
))
2567 } while_for_each_ftrace_op(op
);
2573 ftrace_find_unique_ops(struct dyn_ftrace
*rec
)
2575 struct ftrace_ops
*op
, *found
= NULL
;
2576 unsigned long ip
= rec
->ip
;
2578 do_for_each_ftrace_op(op
, ftrace_ops_list
) {
2580 if (hash_contains_ip(ip
, op
->func_hash
)) {
2586 } while_for_each_ftrace_op(op
);
2591 #ifdef CONFIG_DYNAMIC_FTRACE_WITH_DIRECT_CALLS
2592 /* Protected by rcu_tasks for reading, and direct_mutex for writing */
2593 static struct ftrace_hash __rcu
*direct_functions
= EMPTY_HASH
;
2594 static DEFINE_MUTEX(direct_mutex
);
2597 * Search the direct_functions hash to see if the given instruction pointer
2598 * has a direct caller attached to it.
2600 unsigned long ftrace_find_rec_direct(unsigned long ip
)
2602 struct ftrace_func_entry
*entry
;
2604 entry
= __ftrace_lookup_ip(direct_functions
, ip
);
2608 return entry
->direct
;
2611 static void call_direct_funcs(unsigned long ip
, unsigned long pip
,
2612 struct ftrace_ops
*ops
, struct ftrace_regs
*fregs
)
2614 unsigned long addr
= READ_ONCE(ops
->direct_call
);
2619 arch_ftrace_set_direct_caller(fregs
, addr
);
2621 #endif /* CONFIG_DYNAMIC_FTRACE_WITH_DIRECT_CALLS */
2624 * ftrace_get_addr_new - Get the call address to set to
2625 * @rec: The ftrace record descriptor
2627 * If the record has the FTRACE_FL_REGS set, that means that it
2628 * wants to convert to a callback that saves all regs. If FTRACE_FL_REGS
2629 * is not set, then it wants to convert to the normal callback.
2631 * Returns: the address of the trampoline to set to
2633 unsigned long ftrace_get_addr_new(struct dyn_ftrace
*rec
)
2635 struct ftrace_ops
*ops
;
2638 if ((rec
->flags
& FTRACE_FL_DIRECT
) &&
2639 (ftrace_rec_count(rec
) == 1)) {
2640 addr
= ftrace_find_rec_direct(rec
->ip
);
2646 /* Trampolines take precedence over regs */
2647 if (rec
->flags
& FTRACE_FL_TRAMP
) {
2648 ops
= ftrace_find_tramp_ops_new(rec
);
2649 if (FTRACE_WARN_ON(!ops
|| !ops
->trampoline
)) {
2650 pr_warn("Bad trampoline accounting at: %p (%pS) (%lx)\n",
2651 (void *)rec
->ip
, (void *)rec
->ip
, rec
->flags
);
2652 /* Ftrace is shutting down, return anything */
2653 return (unsigned long)FTRACE_ADDR
;
2655 return ops
->trampoline
;
2658 if (rec
->flags
& FTRACE_FL_REGS
)
2659 return (unsigned long)FTRACE_REGS_ADDR
;
2661 return (unsigned long)FTRACE_ADDR
;
2665 * ftrace_get_addr_curr - Get the call address that is already there
2666 * @rec: The ftrace record descriptor
2668 * The FTRACE_FL_REGS_EN is set when the record already points to
2669 * a function that saves all the regs. Basically the '_EN' version
2670 * represents the current state of the function.
2672 * Returns: the address of the trampoline that is currently being called
2674 unsigned long ftrace_get_addr_curr(struct dyn_ftrace
*rec
)
2676 struct ftrace_ops
*ops
;
2679 /* Direct calls take precedence over trampolines */
2680 if (rec
->flags
& FTRACE_FL_DIRECT_EN
) {
2681 addr
= ftrace_find_rec_direct(rec
->ip
);
2687 /* Trampolines take precedence over regs */
2688 if (rec
->flags
& FTRACE_FL_TRAMP_EN
) {
2689 ops
= ftrace_find_tramp_ops_curr(rec
);
2690 if (FTRACE_WARN_ON(!ops
)) {
2691 pr_warn("Bad trampoline accounting at: %p (%pS)\n",
2692 (void *)rec
->ip
, (void *)rec
->ip
);
2693 /* Ftrace is shutting down, return anything */
2694 return (unsigned long)FTRACE_ADDR
;
2696 return ops
->trampoline
;
2699 if (rec
->flags
& FTRACE_FL_REGS_EN
)
2700 return (unsigned long)FTRACE_REGS_ADDR
;
2702 return (unsigned long)FTRACE_ADDR
;
2706 __ftrace_replace_code(struct dyn_ftrace
*rec
, bool enable
)
2708 unsigned long ftrace_old_addr
;
2709 unsigned long ftrace_addr
;
2712 ftrace_addr
= ftrace_get_addr_new(rec
);
2714 /* This needs to be done before we call ftrace_update_record */
2715 ftrace_old_addr
= ftrace_get_addr_curr(rec
);
2717 ret
= ftrace_update_record(rec
, enable
);
2719 ftrace_bug_type
= FTRACE_BUG_UNKNOWN
;
2722 case FTRACE_UPDATE_IGNORE
:
2725 case FTRACE_UPDATE_MAKE_CALL
:
2726 ftrace_bug_type
= FTRACE_BUG_CALL
;
2727 return ftrace_make_call(rec
, ftrace_addr
);
2729 case FTRACE_UPDATE_MAKE_NOP
:
2730 ftrace_bug_type
= FTRACE_BUG_NOP
;
2731 return ftrace_make_nop(NULL
, rec
, ftrace_old_addr
);
2733 case FTRACE_UPDATE_MODIFY_CALL
:
2734 ftrace_bug_type
= FTRACE_BUG_UPDATE
;
2735 return ftrace_modify_call(rec
, ftrace_old_addr
, ftrace_addr
);
2738 return -1; /* unknown ftrace bug */
2741 void __weak
ftrace_replace_code(int mod_flags
)
2743 struct dyn_ftrace
*rec
;
2744 struct ftrace_page
*pg
;
2745 bool enable
= mod_flags
& FTRACE_MODIFY_ENABLE_FL
;
2746 int schedulable
= mod_flags
& FTRACE_MODIFY_MAY_SLEEP_FL
;
2749 if (unlikely(ftrace_disabled
))
2752 do_for_each_ftrace_rec(pg
, rec
) {
2754 if (skip_record(rec
))
2757 failed
= __ftrace_replace_code(rec
, enable
);
2759 ftrace_bug(failed
, rec
);
2760 /* Stop processing */
2765 } while_for_each_ftrace_rec();
2768 struct ftrace_rec_iter
{
2769 struct ftrace_page
*pg
;
2774 * ftrace_rec_iter_start - start up iterating over traced functions
2776 * Returns: an iterator handle that is used to iterate over all
2777 * the records that represent address locations where functions
2780 * May return NULL if no records are available.
2782 struct ftrace_rec_iter
*ftrace_rec_iter_start(void)
2785 * We only use a single iterator.
2786 * Protected by the ftrace_lock mutex.
2788 static struct ftrace_rec_iter ftrace_rec_iter
;
2789 struct ftrace_rec_iter
*iter
= &ftrace_rec_iter
;
2791 iter
->pg
= ftrace_pages_start
;
2794 /* Could have empty pages */
2795 while (iter
->pg
&& !iter
->pg
->index
)
2796 iter
->pg
= iter
->pg
->next
;
2805 * ftrace_rec_iter_next - get the next record to process.
2806 * @iter: The handle to the iterator.
2808 * Returns: the next iterator after the given iterator @iter.
2810 struct ftrace_rec_iter
*ftrace_rec_iter_next(struct ftrace_rec_iter
*iter
)
2814 if (iter
->index
>= iter
->pg
->index
) {
2815 iter
->pg
= iter
->pg
->next
;
2818 /* Could have empty pages */
2819 while (iter
->pg
&& !iter
->pg
->index
)
2820 iter
->pg
= iter
->pg
->next
;
2830 * ftrace_rec_iter_record - get the record at the iterator location
2831 * @iter: The current iterator location
2833 * Returns: the record that the current @iter is at.
2835 struct dyn_ftrace
*ftrace_rec_iter_record(struct ftrace_rec_iter
*iter
)
2837 return &iter
->pg
->records
[iter
->index
];
2841 ftrace_nop_initialize(struct module
*mod
, struct dyn_ftrace
*rec
)
2845 if (unlikely(ftrace_disabled
))
2848 ret
= ftrace_init_nop(mod
, rec
);
2850 ftrace_bug_type
= FTRACE_BUG_INIT
;
2851 ftrace_bug(ret
, rec
);
2858 * archs can override this function if they must do something
2859 * before the modifying code is performed.
2861 void __weak
ftrace_arch_code_modify_prepare(void)
2866 * archs can override this function if they must do something
2867 * after the modifying code is performed.
2869 void __weak
ftrace_arch_code_modify_post_process(void)
2873 static int update_ftrace_func(ftrace_func_t func
)
2875 static ftrace_func_t save_func
;
2877 /* Avoid updating if it hasn't changed */
2878 if (func
== save_func
)
2883 return ftrace_update_ftrace_func(func
);
2886 void ftrace_modify_all_code(int command
)
2888 int update
= command
& FTRACE_UPDATE_TRACE_FUNC
;
2892 if (command
& FTRACE_MAY_SLEEP
)
2893 mod_flags
= FTRACE_MODIFY_MAY_SLEEP_FL
;
2896 * If the ftrace_caller calls a ftrace_ops func directly,
2897 * we need to make sure that it only traces functions it
2898 * expects to trace. When doing the switch of functions,
2899 * we need to update to the ftrace_ops_list_func first
2900 * before the transition between old and new calls are set,
2901 * as the ftrace_ops_list_func will check the ops hashes
2902 * to make sure the ops are having the right functions
2906 err
= update_ftrace_func(ftrace_ops_list_func
);
2907 if (FTRACE_WARN_ON(err
))
2911 if (command
& FTRACE_UPDATE_CALLS
)
2912 ftrace_replace_code(mod_flags
| FTRACE_MODIFY_ENABLE_FL
);
2913 else if (command
& FTRACE_DISABLE_CALLS
)
2914 ftrace_replace_code(mod_flags
);
2916 if (update
&& ftrace_trace_function
!= ftrace_ops_list_func
) {
2917 function_trace_op
= set_function_trace_op
;
2919 /* If irqs are disabled, we are in stop machine */
2920 if (!irqs_disabled())
2921 smp_call_function(ftrace_sync_ipi
, NULL
, 1);
2922 err
= update_ftrace_func(ftrace_trace_function
);
2923 if (FTRACE_WARN_ON(err
))
2927 if (command
& FTRACE_START_FUNC_RET
)
2928 err
= ftrace_enable_ftrace_graph_caller();
2929 else if (command
& FTRACE_STOP_FUNC_RET
)
2930 err
= ftrace_disable_ftrace_graph_caller();
2931 FTRACE_WARN_ON(err
);
2934 static int __ftrace_modify_code(void *data
)
2936 int *command
= data
;
2938 ftrace_modify_all_code(*command
);
2944 * ftrace_run_stop_machine - go back to the stop machine method
2945 * @command: The command to tell ftrace what to do
2947 * If an arch needs to fall back to the stop machine method, the
2948 * it can call this function.
2950 void ftrace_run_stop_machine(int command
)
2952 stop_machine(__ftrace_modify_code
, &command
, NULL
);
2956 * arch_ftrace_update_code - modify the code to trace or not trace
2957 * @command: The command that needs to be done
2959 * Archs can override this function if it does not need to
2960 * run stop_machine() to modify code.
2962 void __weak
arch_ftrace_update_code(int command
)
2964 ftrace_run_stop_machine(command
);
2967 static void ftrace_run_update_code(int command
)
2969 ftrace_arch_code_modify_prepare();
2972 * By default we use stop_machine() to modify the code.
2973 * But archs can do what ever they want as long as it
2974 * is safe. The stop_machine() is the safest, but also
2975 * produces the most overhead.
2977 arch_ftrace_update_code(command
);
2979 ftrace_arch_code_modify_post_process();
2982 static void ftrace_run_modify_code(struct ftrace_ops
*ops
, int command
,
2983 struct ftrace_ops_hash
*old_hash
)
2985 ops
->flags
|= FTRACE_OPS_FL_MODIFYING
;
2986 ops
->old_hash
.filter_hash
= old_hash
->filter_hash
;
2987 ops
->old_hash
.notrace_hash
= old_hash
->notrace_hash
;
2988 ftrace_run_update_code(command
);
2989 ops
->old_hash
.filter_hash
= NULL
;
2990 ops
->old_hash
.notrace_hash
= NULL
;
2991 ops
->flags
&= ~FTRACE_OPS_FL_MODIFYING
;
2994 static ftrace_func_t saved_ftrace_func
;
2995 static int ftrace_start_up
;
2997 void __weak
arch_ftrace_trampoline_free(struct ftrace_ops
*ops
)
3001 /* List of trace_ops that have allocated trampolines */
3002 static LIST_HEAD(ftrace_ops_trampoline_list
);
3004 static void ftrace_add_trampoline_to_kallsyms(struct ftrace_ops
*ops
)
3006 lockdep_assert_held(&ftrace_lock
);
3007 list_add_rcu(&ops
->list
, &ftrace_ops_trampoline_list
);
3010 static void ftrace_remove_trampoline_from_kallsyms(struct ftrace_ops
*ops
)
3012 lockdep_assert_held(&ftrace_lock
);
3013 list_del_rcu(&ops
->list
);
3018 * "__builtin__ftrace" is used as a module name in /proc/kallsyms for symbols
3019 * for pages allocated for ftrace purposes, even though "__builtin__ftrace" is
3022 #define FTRACE_TRAMPOLINE_MOD "__builtin__ftrace"
3023 #define FTRACE_TRAMPOLINE_SYM "ftrace_trampoline"
3025 static void ftrace_trampoline_free(struct ftrace_ops
*ops
)
3027 if (ops
&& (ops
->flags
& FTRACE_OPS_FL_ALLOC_TRAMP
) &&
3030 * Record the text poke event before the ksymbol unregister
3033 perf_event_text_poke((void *)ops
->trampoline
,
3034 (void *)ops
->trampoline
,
3035 ops
->trampoline_size
, NULL
, 0);
3036 perf_event_ksymbol(PERF_RECORD_KSYMBOL_TYPE_OOL
,
3037 ops
->trampoline
, ops
->trampoline_size
,
3038 true, FTRACE_TRAMPOLINE_SYM
);
3039 /* Remove from kallsyms after the perf events */
3040 ftrace_remove_trampoline_from_kallsyms(ops
);
3043 arch_ftrace_trampoline_free(ops
);
3046 static void ftrace_startup_enable(int command
)
3048 if (saved_ftrace_func
!= ftrace_trace_function
) {
3049 saved_ftrace_func
= ftrace_trace_function
;
3050 command
|= FTRACE_UPDATE_TRACE_FUNC
;
3053 if (!command
|| !ftrace_enabled
)
3056 ftrace_run_update_code(command
);
3059 static void ftrace_startup_all(int command
)
3061 update_all_ops
= true;
3062 ftrace_startup_enable(command
);
3063 update_all_ops
= false;
3066 int ftrace_startup(struct ftrace_ops
*ops
, int command
)
3070 if (unlikely(ftrace_disabled
))
3073 ret
= __register_ftrace_function(ops
);
3080 * Note that ftrace probes uses this to start up
3081 * and modify functions it will probe. But we still
3082 * set the ADDING flag for modification, as probes
3083 * do not have trampolines. If they add them in the
3084 * future, then the probes will need to distinguish
3085 * between adding and updating probes.
3087 ops
->flags
|= FTRACE_OPS_FL_ENABLED
| FTRACE_OPS_FL_ADDING
;
3089 ret
= ftrace_hash_ipmodify_enable(ops
);
3091 /* Rollback registration process */
3092 __unregister_ftrace_function(ops
);
3094 ops
->flags
&= ~FTRACE_OPS_FL_ENABLED
;
3095 if (ops
->flags
& FTRACE_OPS_FL_DYNAMIC
)
3096 ftrace_trampoline_free(ops
);
3100 if (ftrace_hash_rec_enable(ops
))
3101 command
|= FTRACE_UPDATE_CALLS
;
3103 ftrace_startup_enable(command
);
3106 * If ftrace is in an undefined state, we just remove ops from list
3107 * to prevent the NULL pointer, instead of totally rolling it back and
3108 * free trampoline, because those actions could cause further damage.
3110 if (unlikely(ftrace_disabled
)) {
3111 __unregister_ftrace_function(ops
);
3115 ops
->flags
&= ~FTRACE_OPS_FL_ADDING
;
3120 int ftrace_shutdown(struct ftrace_ops
*ops
, int command
)
3124 if (unlikely(ftrace_disabled
))
3127 ret
= __unregister_ftrace_function(ops
);
3133 * Just warn in case of unbalance, no need to kill ftrace, it's not
3134 * critical but the ftrace_call callers may be never nopped again after
3135 * further ftrace uses.
3137 WARN_ON_ONCE(ftrace_start_up
< 0);
3139 /* Disabling ipmodify never fails */
3140 ftrace_hash_ipmodify_disable(ops
);
3142 if (ftrace_hash_rec_disable(ops
))
3143 command
|= FTRACE_UPDATE_CALLS
;
3145 ops
->flags
&= ~FTRACE_OPS_FL_ENABLED
;
3147 if (saved_ftrace_func
!= ftrace_trace_function
) {
3148 saved_ftrace_func
= ftrace_trace_function
;
3149 command
|= FTRACE_UPDATE_TRACE_FUNC
;
3152 if (!command
|| !ftrace_enabled
)
3156 * If the ops uses a trampoline, then it needs to be
3157 * tested first on update.
3159 ops
->flags
|= FTRACE_OPS_FL_REMOVING
;
3162 /* The trampoline logic checks the old hashes */
3163 ops
->old_hash
.filter_hash
= ops
->func_hash
->filter_hash
;
3164 ops
->old_hash
.notrace_hash
= ops
->func_hash
->notrace_hash
;
3166 ftrace_run_update_code(command
);
3169 * If there's no more ops registered with ftrace, run a
3170 * sanity check to make sure all rec flags are cleared.
3172 if (rcu_dereference_protected(ftrace_ops_list
,
3173 lockdep_is_held(&ftrace_lock
)) == &ftrace_list_end
) {
3174 struct ftrace_page
*pg
;
3175 struct dyn_ftrace
*rec
;
3177 do_for_each_ftrace_rec(pg
, rec
) {
3178 if (FTRACE_WARN_ON_ONCE(rec
->flags
& ~FTRACE_NOCLEAR_FLAGS
))
3179 pr_warn(" %pS flags:%lx\n",
3180 (void *)rec
->ip
, rec
->flags
);
3181 } while_for_each_ftrace_rec();
3184 ops
->old_hash
.filter_hash
= NULL
;
3185 ops
->old_hash
.notrace_hash
= NULL
;
3188 ops
->flags
&= ~FTRACE_OPS_FL_REMOVING
;
3192 * Dynamic ops may be freed, we must make sure that all
3193 * callers are done before leaving this function.
3195 if (ops
->flags
& FTRACE_OPS_FL_DYNAMIC
) {
3197 * We need to do a hard force of sched synchronization.
3198 * This is because we use preempt_disable() to do RCU, but
3199 * the function tracers can be called where RCU is not watching
3200 * (like before user_exit()). We can not rely on the RCU
3201 * infrastructure to do the synchronization, thus we must do it
3204 synchronize_rcu_tasks_rude();
3207 * When the kernel is preemptive, tasks can be preempted
3208 * while on a ftrace trampoline. Just scheduling a task on
3209 * a CPU is not good enough to flush them. Calling
3210 * synchronize_rcu_tasks() will wait for those tasks to
3211 * execute and either schedule voluntarily or enter user space.
3213 synchronize_rcu_tasks();
3215 ftrace_trampoline_free(ops
);
3221 /* Simply make a copy of @src and return it */
3222 static struct ftrace_hash
*copy_hash(struct ftrace_hash
*src
)
3224 if (ftrace_hash_empty(src
))
3227 return alloc_and_copy_ftrace_hash(src
->size_bits
, src
);
3231 * Append @new_hash entries to @hash:
3233 * If @hash is the EMPTY_HASH then it traces all functions and nothing
3236 * If @new_hash is the EMPTY_HASH, then make *hash the EMPTY_HASH so
3237 * that it traces everything.
3239 * Otherwise, go through all of @new_hash and add anything that @hash
3240 * doesn't already have, to @hash.
3242 * The filter_hash updates uses just the append_hash() function
3243 * and the notrace_hash does not.
3245 static int append_hash(struct ftrace_hash
**hash
, struct ftrace_hash
*new_hash
)
3247 struct ftrace_func_entry
*entry
;
3251 /* An empty hash does everything */
3252 if (ftrace_hash_empty(*hash
))
3255 /* If new_hash has everything make hash have everything */
3256 if (ftrace_hash_empty(new_hash
)) {
3257 free_ftrace_hash(*hash
);
3262 size
= 1 << new_hash
->size_bits
;
3263 for (i
= 0; i
< size
; i
++) {
3264 hlist_for_each_entry(entry
, &new_hash
->buckets
[i
], hlist
) {
3265 /* Only add if not already in hash */
3266 if (!__ftrace_lookup_ip(*hash
, entry
->ip
) &&
3267 add_hash_entry(*hash
, entry
->ip
) == NULL
)
3275 * Add to @hash only those that are in both @new_hash1 and @new_hash2
3277 * The notrace_hash updates uses just the intersect_hash() function
3278 * and the filter_hash does not.
3280 static int intersect_hash(struct ftrace_hash
**hash
, struct ftrace_hash
*new_hash1
,
3281 struct ftrace_hash
*new_hash2
)
3283 struct ftrace_func_entry
*entry
;
3288 * If new_hash1 or new_hash2 is the EMPTY_HASH then make the hash
3289 * empty as well as empty for notrace means none are notraced.
3291 if (ftrace_hash_empty(new_hash1
) || ftrace_hash_empty(new_hash2
)) {
3292 free_ftrace_hash(*hash
);
3297 size
= 1 << new_hash1
->size_bits
;
3298 for (i
= 0; i
< size
; i
++) {
3299 hlist_for_each_entry(entry
, &new_hash1
->buckets
[i
], hlist
) {
3300 /* Only add if in both @new_hash1 and @new_hash2 */
3301 if (__ftrace_lookup_ip(new_hash2
, entry
->ip
) &&
3302 add_hash_entry(*hash
, entry
->ip
) == NULL
)
3306 /* If nothing intersects, make it the empty set */
3307 if (ftrace_hash_empty(*hash
)) {
3308 free_ftrace_hash(*hash
);
3314 /* Return a new hash that has a union of all @ops->filter_hash entries */
3315 static struct ftrace_hash
*append_hashes(struct ftrace_ops
*ops
)
3317 struct ftrace_hash
*new_hash
;
3318 struct ftrace_ops
*subops
;
3321 new_hash
= alloc_ftrace_hash(ops
->func_hash
->filter_hash
->size_bits
);
3325 list_for_each_entry(subops
, &ops
->subop_list
, list
) {
3326 ret
= append_hash(&new_hash
, subops
->func_hash
->filter_hash
);
3328 free_ftrace_hash(new_hash
);
3331 /* Nothing more to do if new_hash is empty */
3332 if (ftrace_hash_empty(new_hash
))
3338 /* Make @ops trace evenything except what all its subops do not trace */
3339 static struct ftrace_hash
*intersect_hashes(struct ftrace_ops
*ops
)
3341 struct ftrace_hash
*new_hash
= NULL
;
3342 struct ftrace_ops
*subops
;
3346 list_for_each_entry(subops
, &ops
->subop_list
, list
) {
3347 struct ftrace_hash
*next_hash
;
3350 size_bits
= subops
->func_hash
->notrace_hash
->size_bits
;
3351 new_hash
= alloc_and_copy_ftrace_hash(size_bits
, ops
->func_hash
->notrace_hash
);
3356 size_bits
= new_hash
->size_bits
;
3357 next_hash
= new_hash
;
3358 new_hash
= alloc_ftrace_hash(size_bits
);
3359 ret
= intersect_hash(&new_hash
, next_hash
, subops
->func_hash
->notrace_hash
);
3360 free_ftrace_hash(next_hash
);
3362 free_ftrace_hash(new_hash
);
3365 /* Nothing more to do if new_hash is empty */
3366 if (ftrace_hash_empty(new_hash
))
3372 static bool ops_equal(struct ftrace_hash
*A
, struct ftrace_hash
*B
)
3374 struct ftrace_func_entry
*entry
;
3378 if (ftrace_hash_empty(A
))
3379 return ftrace_hash_empty(B
);
3381 if (ftrace_hash_empty(B
))
3382 return ftrace_hash_empty(A
);
3384 if (A
->count
!= B
->count
)
3387 size
= 1 << A
->size_bits
;
3388 for (i
= 0; i
< size
; i
++) {
3389 hlist_for_each_entry(entry
, &A
->buckets
[i
], hlist
) {
3390 if (!__ftrace_lookup_ip(B
, entry
->ip
))
3398 static void ftrace_ops_update_code(struct ftrace_ops
*ops
,
3399 struct ftrace_ops_hash
*old_hash
);
3401 static int __ftrace_hash_move_and_update_ops(struct ftrace_ops
*ops
,
3402 struct ftrace_hash
**orig_hash
,
3403 struct ftrace_hash
*hash
,
3406 struct ftrace_ops_hash old_hash_ops
;
3407 struct ftrace_hash
*old_hash
;
3410 old_hash
= *orig_hash
;
3411 old_hash_ops
.filter_hash
= ops
->func_hash
->filter_hash
;
3412 old_hash_ops
.notrace_hash
= ops
->func_hash
->notrace_hash
;
3413 ret
= ftrace_hash_move(ops
, enable
, orig_hash
, hash
);
3415 ftrace_ops_update_code(ops
, &old_hash_ops
);
3416 free_ftrace_hash_rcu(old_hash
);
3421 static int ftrace_update_ops(struct ftrace_ops
*ops
, struct ftrace_hash
*filter_hash
,
3422 struct ftrace_hash
*notrace_hash
)
3426 if (!ops_equal(filter_hash
, ops
->func_hash
->filter_hash
)) {
3427 ret
= __ftrace_hash_move_and_update_ops(ops
, &ops
->func_hash
->filter_hash
,
3433 if (!ops_equal(notrace_hash
, ops
->func_hash
->notrace_hash
)) {
3434 ret
= __ftrace_hash_move_and_update_ops(ops
, &ops
->func_hash
->notrace_hash
,
3444 * ftrace_startup_subops - enable tracing for subops of an ops
3445 * @ops: Manager ops (used to pick all the functions of its subops)
3446 * @subops: A new ops to add to @ops
3447 * @command: Extra commands to use to enable tracing
3449 * The @ops is a manager @ops that has the filter that includes all the functions
3450 * that its list of subops are tracing. Adding a new @subops will add the
3451 * functions of @subops to @ops.
3453 int ftrace_startup_subops(struct ftrace_ops
*ops
, struct ftrace_ops
*subops
, int command
)
3455 struct ftrace_hash
*filter_hash
;
3456 struct ftrace_hash
*notrace_hash
;
3457 struct ftrace_hash
*save_filter_hash
;
3458 struct ftrace_hash
*save_notrace_hash
;
3462 if (unlikely(ftrace_disabled
))
3465 ftrace_ops_init(ops
);
3466 ftrace_ops_init(subops
);
3468 if (WARN_ON_ONCE(subops
->flags
& FTRACE_OPS_FL_ENABLED
))
3471 /* Make everything canonical (Just in case!) */
3472 if (!ops
->func_hash
->filter_hash
)
3473 ops
->func_hash
->filter_hash
= EMPTY_HASH
;
3474 if (!ops
->func_hash
->notrace_hash
)
3475 ops
->func_hash
->notrace_hash
= EMPTY_HASH
;
3476 if (!subops
->func_hash
->filter_hash
)
3477 subops
->func_hash
->filter_hash
= EMPTY_HASH
;
3478 if (!subops
->func_hash
->notrace_hash
)
3479 subops
->func_hash
->notrace_hash
= EMPTY_HASH
;
3481 /* For the first subops to ops just enable it normally */
3482 if (list_empty(&ops
->subop_list
)) {
3483 /* Just use the subops hashes */
3484 filter_hash
= copy_hash(subops
->func_hash
->filter_hash
);
3485 notrace_hash
= copy_hash(subops
->func_hash
->notrace_hash
);
3486 if (!filter_hash
|| !notrace_hash
) {
3487 free_ftrace_hash(filter_hash
);
3488 free_ftrace_hash(notrace_hash
);
3492 save_filter_hash
= ops
->func_hash
->filter_hash
;
3493 save_notrace_hash
= ops
->func_hash
->notrace_hash
;
3495 ops
->func_hash
->filter_hash
= filter_hash
;
3496 ops
->func_hash
->notrace_hash
= notrace_hash
;
3497 list_add(&subops
->list
, &ops
->subop_list
);
3498 ret
= ftrace_startup(ops
, command
);
3500 list_del(&subops
->list
);
3501 ops
->func_hash
->filter_hash
= save_filter_hash
;
3502 ops
->func_hash
->notrace_hash
= save_notrace_hash
;
3503 free_ftrace_hash(filter_hash
);
3504 free_ftrace_hash(notrace_hash
);
3506 free_ftrace_hash(save_filter_hash
);
3507 free_ftrace_hash(save_notrace_hash
);
3508 subops
->flags
|= FTRACE_OPS_FL_ENABLED
| FTRACE_OPS_FL_SUBOP
;
3509 subops
->managed
= ops
;
3515 * Here there's already something attached. Here are the rules:
3516 * o If either filter_hash is empty then the final stays empty
3517 * o Otherwise, the final is a superset of both hashes
3518 * o If either notrace_hash is empty then the final stays empty
3519 * o Otherwise, the final is an intersection between the hashes
3521 if (ftrace_hash_empty(ops
->func_hash
->filter_hash
) ||
3522 ftrace_hash_empty(subops
->func_hash
->filter_hash
)) {
3523 filter_hash
= EMPTY_HASH
;
3525 size_bits
= max(ops
->func_hash
->filter_hash
->size_bits
,
3526 subops
->func_hash
->filter_hash
->size_bits
);
3527 filter_hash
= alloc_and_copy_ftrace_hash(size_bits
, ops
->func_hash
->filter_hash
);
3530 ret
= append_hash(&filter_hash
, subops
->func_hash
->filter_hash
);
3532 free_ftrace_hash(filter_hash
);
3537 if (ftrace_hash_empty(ops
->func_hash
->notrace_hash
) ||
3538 ftrace_hash_empty(subops
->func_hash
->notrace_hash
)) {
3539 notrace_hash
= EMPTY_HASH
;
3541 size_bits
= max(ops
->func_hash
->filter_hash
->size_bits
,
3542 subops
->func_hash
->filter_hash
->size_bits
);
3543 notrace_hash
= alloc_ftrace_hash(size_bits
);
3544 if (!notrace_hash
) {
3545 free_ftrace_hash(filter_hash
);
3549 ret
= intersect_hash(¬race_hash
, ops
->func_hash
->filter_hash
,
3550 subops
->func_hash
->filter_hash
);
3552 free_ftrace_hash(filter_hash
);
3553 free_ftrace_hash(notrace_hash
);
3558 list_add(&subops
->list
, &ops
->subop_list
);
3560 ret
= ftrace_update_ops(ops
, filter_hash
, notrace_hash
);
3561 free_ftrace_hash(filter_hash
);
3562 free_ftrace_hash(notrace_hash
);
3564 list_del(&subops
->list
);
3566 subops
->flags
|= FTRACE_OPS_FL_ENABLED
| FTRACE_OPS_FL_SUBOP
;
3567 subops
->managed
= ops
;
3573 * ftrace_shutdown_subops - Remove a subops from a manager ops
3574 * @ops: A manager ops to remove @subops from
3575 * @subops: The subops to remove from @ops
3576 * @command: Any extra command flags to add to modifying the text
3578 * Removes the functions being traced by the @subops from @ops. Note, it
3579 * will not affect functions that are being traced by other subops that
3580 * still exist in @ops.
3582 * If the last subops is removed from @ops, then @ops is shutdown normally.
3584 int ftrace_shutdown_subops(struct ftrace_ops
*ops
, struct ftrace_ops
*subops
, int command
)
3586 struct ftrace_hash
*filter_hash
;
3587 struct ftrace_hash
*notrace_hash
;
3590 if (unlikely(ftrace_disabled
))
3593 if (WARN_ON_ONCE(!(subops
->flags
& FTRACE_OPS_FL_ENABLED
)))
3596 list_del(&subops
->list
);
3598 if (list_empty(&ops
->subop_list
)) {
3599 /* Last one, just disable the current ops */
3601 ret
= ftrace_shutdown(ops
, command
);
3603 list_add(&subops
->list
, &ops
->subop_list
);
3607 subops
->flags
&= ~FTRACE_OPS_FL_ENABLED
;
3609 free_ftrace_hash(ops
->func_hash
->filter_hash
);
3610 free_ftrace_hash(ops
->func_hash
->notrace_hash
);
3611 ops
->func_hash
->filter_hash
= EMPTY_HASH
;
3612 ops
->func_hash
->notrace_hash
= EMPTY_HASH
;
3613 subops
->flags
&= ~(FTRACE_OPS_FL_ENABLED
| FTRACE_OPS_FL_SUBOP
);
3614 subops
->managed
= NULL
;
3619 /* Rebuild the hashes without subops */
3620 filter_hash
= append_hashes(ops
);
3621 notrace_hash
= intersect_hashes(ops
);
3622 if (!filter_hash
|| !notrace_hash
) {
3623 free_ftrace_hash(filter_hash
);
3624 free_ftrace_hash(notrace_hash
);
3625 list_add(&subops
->list
, &ops
->subop_list
);
3629 ret
= ftrace_update_ops(ops
, filter_hash
, notrace_hash
);
3631 list_add(&subops
->list
, &ops
->subop_list
);
3633 subops
->flags
&= ~(FTRACE_OPS_FL_ENABLED
| FTRACE_OPS_FL_SUBOP
);
3634 subops
->managed
= NULL
;
3636 free_ftrace_hash(filter_hash
);
3637 free_ftrace_hash(notrace_hash
);
3641 static int ftrace_hash_move_and_update_subops(struct ftrace_ops
*subops
,
3642 struct ftrace_hash
**orig_subhash
,
3643 struct ftrace_hash
*hash
,
3646 struct ftrace_ops
*ops
= subops
->managed
;
3647 struct ftrace_hash
**orig_hash
;
3648 struct ftrace_hash
*save_hash
;
3649 struct ftrace_hash
*new_hash
;
3652 /* Manager ops can not be subops (yet) */
3653 if (WARN_ON_ONCE(!ops
|| ops
->flags
& FTRACE_OPS_FL_SUBOP
))
3656 /* Move the new hash over to the subops hash */
3657 save_hash
= *orig_subhash
;
3658 *orig_subhash
= __ftrace_hash_move(hash
);
3659 if (!*orig_subhash
) {
3660 *orig_subhash
= save_hash
;
3664 /* Create a new_hash to hold the ops new functions */
3666 orig_hash
= &ops
->func_hash
->filter_hash
;
3667 new_hash
= append_hashes(ops
);
3669 orig_hash
= &ops
->func_hash
->notrace_hash
;
3670 new_hash
= intersect_hashes(ops
);
3673 /* Move the hash over to the new hash */
3674 ret
= __ftrace_hash_move_and_update_ops(ops
, orig_hash
, new_hash
, enable
);
3676 free_ftrace_hash(new_hash
);
3679 /* Put back the original hash */
3680 free_ftrace_hash_rcu(*orig_subhash
);
3681 *orig_subhash
= save_hash
;
3683 free_ftrace_hash_rcu(save_hash
);
3689 u64 ftrace_update_time
;
3690 u64 ftrace_total_mod_time
;
3691 unsigned long ftrace_update_tot_cnt
;
3692 unsigned long ftrace_number_of_pages
;
3693 unsigned long ftrace_number_of_groups
;
3695 static inline int ops_traces_mod(struct ftrace_ops
*ops
)
3698 * Filter_hash being empty will default to trace module.
3699 * But notrace hash requires a test of individual module functions.
3701 return ftrace_hash_empty(ops
->func_hash
->filter_hash
) &&
3702 ftrace_hash_empty(ops
->func_hash
->notrace_hash
);
3705 static int ftrace_update_code(struct module
*mod
, struct ftrace_page
*new_pgs
)
3707 bool init_nop
= ftrace_need_init_nop();
3708 struct ftrace_page
*pg
;
3709 struct dyn_ftrace
*p
;
3710 u64 start
, stop
, update_time
;
3711 unsigned long update_cnt
= 0;
3712 unsigned long rec_flags
= 0;
3715 start
= ftrace_now(raw_smp_processor_id());
3718 * When a module is loaded, this function is called to convert
3719 * the calls to mcount in its text to nops, and also to create
3720 * an entry in the ftrace data. Now, if ftrace is activated
3721 * after this call, but before the module sets its text to
3722 * read-only, the modification of enabling ftrace can fail if
3723 * the read-only is done while ftrace is converting the calls.
3724 * To prevent this, the module's records are set as disabled
3725 * and will be enabled after the call to set the module's text
3729 rec_flags
|= FTRACE_FL_DISABLED
;
3731 for (pg
= new_pgs
; pg
; pg
= pg
->next
) {
3733 for (i
= 0; i
< pg
->index
; i
++) {
3735 /* If something went wrong, bail without enabling anything */
3736 if (unlikely(ftrace_disabled
))
3739 p
= &pg
->records
[i
];
3740 p
->flags
= rec_flags
;
3743 * Do the initial record conversion from mcount jump
3744 * to the NOP instructions.
3746 if (init_nop
&& !ftrace_nop_initialize(mod
, p
))
3753 stop
= ftrace_now(raw_smp_processor_id());
3754 update_time
= stop
- start
;
3756 ftrace_total_mod_time
+= update_time
;
3758 ftrace_update_time
= update_time
;
3759 ftrace_update_tot_cnt
+= update_cnt
;
3764 static int ftrace_allocate_records(struct ftrace_page
*pg
, int count
)
3770 if (WARN_ON(!count
))
3773 /* We want to fill as much as possible, with no empty pages */
3774 pages
= DIV_ROUND_UP(count
, ENTRIES_PER_PAGE
);
3775 order
= fls(pages
) - 1;
3778 pg
->records
= (void *)__get_free_pages(GFP_KERNEL
| __GFP_ZERO
, order
);
3781 /* if we can't allocate this size, try something smaller */
3788 ftrace_number_of_pages
+= 1 << order
;
3789 ftrace_number_of_groups
++;
3791 cnt
= (PAGE_SIZE
<< order
) / ENTRY_SIZE
;
3800 static void ftrace_free_pages(struct ftrace_page
*pages
)
3802 struct ftrace_page
*pg
= pages
;
3806 free_pages((unsigned long)pg
->records
, pg
->order
);
3807 ftrace_number_of_pages
-= 1 << pg
->order
;
3812 ftrace_number_of_groups
--;
3816 static struct ftrace_page
*
3817 ftrace_allocate_pages(unsigned long num_to_init
)
3819 struct ftrace_page
*start_pg
;
3820 struct ftrace_page
*pg
;
3826 start_pg
= pg
= kzalloc(sizeof(*pg
), GFP_KERNEL
);
3831 * Try to allocate as much as possible in one continues
3832 * location that fills in all of the space. We want to
3833 * waste as little space as possible.
3836 cnt
= ftrace_allocate_records(pg
, num_to_init
);
3844 pg
->next
= kzalloc(sizeof(*pg
), GFP_KERNEL
);
3854 ftrace_free_pages(start_pg
);
3855 pr_info("ftrace: FAILED to allocate memory for functions\n");
3859 #define FTRACE_BUFF_MAX (KSYM_SYMBOL_LEN+4) /* room for wildcards */
3861 struct ftrace_iterator
{
3865 struct ftrace_page
*pg
;
3866 struct dyn_ftrace
*func
;
3867 struct ftrace_func_probe
*probe
;
3868 struct ftrace_func_entry
*probe_entry
;
3869 struct trace_parser parser
;
3870 struct ftrace_hash
*hash
;
3871 struct ftrace_ops
*ops
;
3872 struct trace_array
*tr
;
3873 struct list_head
*mod_list
;
3880 t_probe_next(struct seq_file
*m
, loff_t
*pos
)
3882 struct ftrace_iterator
*iter
= m
->private;
3883 struct trace_array
*tr
= iter
->ops
->private;
3884 struct list_head
*func_probes
;
3885 struct ftrace_hash
*hash
;
3886 struct list_head
*next
;
3887 struct hlist_node
*hnd
= NULL
;
3888 struct hlist_head
*hhd
;
3897 func_probes
= &tr
->func_probes
;
3898 if (list_empty(func_probes
))
3902 next
= func_probes
->next
;
3903 iter
->probe
= list_entry(next
, struct ftrace_func_probe
, list
);
3906 if (iter
->probe_entry
)
3907 hnd
= &iter
->probe_entry
->hlist
;
3909 hash
= iter
->probe
->ops
.func_hash
->filter_hash
;
3912 * A probe being registered may temporarily have an empty hash
3913 * and it's at the end of the func_probes list.
3915 if (!hash
|| hash
== EMPTY_HASH
)
3918 size
= 1 << hash
->size_bits
;
3921 if (iter
->pidx
>= size
) {
3922 if (iter
->probe
->list
.next
== func_probes
)
3924 next
= iter
->probe
->list
.next
;
3925 iter
->probe
= list_entry(next
, struct ftrace_func_probe
, list
);
3926 hash
= iter
->probe
->ops
.func_hash
->filter_hash
;
3927 size
= 1 << hash
->size_bits
;
3931 hhd
= &hash
->buckets
[iter
->pidx
];
3933 if (hlist_empty(hhd
)) {
3949 if (WARN_ON_ONCE(!hnd
))
3952 iter
->probe_entry
= hlist_entry(hnd
, struct ftrace_func_entry
, hlist
);
3957 static void *t_probe_start(struct seq_file
*m
, loff_t
*pos
)
3959 struct ftrace_iterator
*iter
= m
->private;
3963 if (!(iter
->flags
& FTRACE_ITER_DO_PROBES
))
3966 if (iter
->mod_pos
> *pos
)
3970 iter
->probe_entry
= NULL
;
3972 for (l
= 0; l
<= (*pos
- iter
->mod_pos
); ) {
3973 p
= t_probe_next(m
, &l
);
3980 /* Only set this if we have an item */
3981 iter
->flags
|= FTRACE_ITER_PROBE
;
3987 t_probe_show(struct seq_file
*m
, struct ftrace_iterator
*iter
)
3989 struct ftrace_func_entry
*probe_entry
;
3990 struct ftrace_probe_ops
*probe_ops
;
3991 struct ftrace_func_probe
*probe
;
3993 probe
= iter
->probe
;
3994 probe_entry
= iter
->probe_entry
;
3996 if (WARN_ON_ONCE(!probe
|| !probe_entry
))
3999 probe_ops
= probe
->probe_ops
;
4001 if (probe_ops
->print
)
4002 return probe_ops
->print(m
, probe_entry
->ip
, probe_ops
, probe
->data
);
4004 seq_printf(m
, "%ps:%ps\n", (void *)probe_entry
->ip
,
4005 (void *)probe_ops
->func
);
4011 t_mod_next(struct seq_file
*m
, loff_t
*pos
)
4013 struct ftrace_iterator
*iter
= m
->private;
4014 struct trace_array
*tr
= iter
->tr
;
4019 iter
->mod_list
= iter
->mod_list
->next
;
4021 if (iter
->mod_list
== &tr
->mod_trace
||
4022 iter
->mod_list
== &tr
->mod_notrace
) {
4023 iter
->flags
&= ~FTRACE_ITER_MOD
;
4027 iter
->mod_pos
= *pos
;
4032 static void *t_mod_start(struct seq_file
*m
, loff_t
*pos
)
4034 struct ftrace_iterator
*iter
= m
->private;
4038 if (iter
->func_pos
> *pos
)
4041 iter
->mod_pos
= iter
->func_pos
;
4043 /* probes are only available if tr is set */
4047 for (l
= 0; l
<= (*pos
- iter
->func_pos
); ) {
4048 p
= t_mod_next(m
, &l
);
4053 iter
->flags
&= ~FTRACE_ITER_MOD
;
4054 return t_probe_start(m
, pos
);
4057 /* Only set this if we have an item */
4058 iter
->flags
|= FTRACE_ITER_MOD
;
4064 t_mod_show(struct seq_file
*m
, struct ftrace_iterator
*iter
)
4066 struct ftrace_mod_load
*ftrace_mod
;
4067 struct trace_array
*tr
= iter
->tr
;
4069 if (WARN_ON_ONCE(!iter
->mod_list
) ||
4070 iter
->mod_list
== &tr
->mod_trace
||
4071 iter
->mod_list
== &tr
->mod_notrace
)
4074 ftrace_mod
= list_entry(iter
->mod_list
, struct ftrace_mod_load
, list
);
4076 if (ftrace_mod
->func
)
4077 seq_printf(m
, "%s", ftrace_mod
->func
);
4081 seq_printf(m
, ":mod:%s\n", ftrace_mod
->module
);
4087 t_func_next(struct seq_file
*m
, loff_t
*pos
)
4089 struct ftrace_iterator
*iter
= m
->private;
4090 struct dyn_ftrace
*rec
= NULL
;
4095 if (iter
->idx
>= iter
->pg
->index
) {
4096 if (iter
->pg
->next
) {
4097 iter
->pg
= iter
->pg
->next
;
4102 rec
= &iter
->pg
->records
[iter
->idx
++];
4103 if (((iter
->flags
& (FTRACE_ITER_FILTER
| FTRACE_ITER_NOTRACE
)) &&
4104 !ftrace_lookup_ip(iter
->hash
, rec
->ip
)) ||
4106 ((iter
->flags
& FTRACE_ITER_ENABLED
) &&
4107 !(rec
->flags
& FTRACE_FL_ENABLED
)) ||
4109 ((iter
->flags
& FTRACE_ITER_TOUCHED
) &&
4110 !(rec
->flags
& FTRACE_FL_TOUCHED
))) {
4120 iter
->pos
= iter
->func_pos
= *pos
;
4127 t_next(struct seq_file
*m
, void *v
, loff_t
*pos
)
4129 struct ftrace_iterator
*iter
= m
->private;
4130 loff_t l
= *pos
; /* t_probe_start() must use original pos */
4133 if (unlikely(ftrace_disabled
))
4136 if (iter
->flags
& FTRACE_ITER_PROBE
)
4137 return t_probe_next(m
, pos
);
4139 if (iter
->flags
& FTRACE_ITER_MOD
)
4140 return t_mod_next(m
, pos
);
4142 if (iter
->flags
& FTRACE_ITER_PRINTALL
) {
4143 /* next must increment pos, and t_probe_start does not */
4145 return t_mod_start(m
, &l
);
4148 ret
= t_func_next(m
, pos
);
4151 return t_mod_start(m
, &l
);
4156 static void reset_iter_read(struct ftrace_iterator
*iter
)
4160 iter
->flags
&= ~(FTRACE_ITER_PRINTALL
| FTRACE_ITER_PROBE
| FTRACE_ITER_MOD
);
4163 static void *t_start(struct seq_file
*m
, loff_t
*pos
)
4165 struct ftrace_iterator
*iter
= m
->private;
4169 mutex_lock(&ftrace_lock
);
4171 if (unlikely(ftrace_disabled
))
4175 * If an lseek was done, then reset and start from beginning.
4177 if (*pos
< iter
->pos
)
4178 reset_iter_read(iter
);
4181 * For set_ftrace_filter reading, if we have the filter
4182 * off, we can short cut and just print out that all
4183 * functions are enabled.
4185 if ((iter
->flags
& (FTRACE_ITER_FILTER
| FTRACE_ITER_NOTRACE
)) &&
4186 ftrace_hash_empty(iter
->hash
)) {
4187 iter
->func_pos
= 1; /* Account for the message */
4189 return t_mod_start(m
, pos
);
4190 iter
->flags
|= FTRACE_ITER_PRINTALL
;
4191 /* reset in case of seek/pread */
4192 iter
->flags
&= ~FTRACE_ITER_PROBE
;
4196 if (iter
->flags
& FTRACE_ITER_MOD
)
4197 return t_mod_start(m
, pos
);
4200 * Unfortunately, we need to restart at ftrace_pages_start
4201 * every time we let go of the ftrace_mutex. This is because
4202 * those pointers can change without the lock.
4204 iter
->pg
= ftrace_pages_start
;
4206 for (l
= 0; l
<= *pos
; ) {
4207 p
= t_func_next(m
, &l
);
4213 return t_mod_start(m
, pos
);
4218 static void t_stop(struct seq_file
*m
, void *p
)
4220 mutex_unlock(&ftrace_lock
);
4224 arch_ftrace_trampoline_func(struct ftrace_ops
*ops
, struct dyn_ftrace
*rec
)
4229 static void add_trampoline_func(struct seq_file
*m
, struct ftrace_ops
*ops
,
4230 struct dyn_ftrace
*rec
)
4234 ptr
= arch_ftrace_trampoline_func(ops
, rec
);
4236 seq_printf(m
, " ->%pS", ptr
);
4239 #ifdef FTRACE_MCOUNT_MAX_OFFSET
4241 * Weak functions can still have an mcount/fentry that is saved in
4242 * the __mcount_loc section. These can be detected by having a
4243 * symbol offset of greater than FTRACE_MCOUNT_MAX_OFFSET, as the
4244 * symbol found by kallsyms is not the function that the mcount/fentry
4245 * is part of. The offset is much greater in these cases.
4247 * Test the record to make sure that the ip points to a valid kallsyms
4248 * and if not, mark it disabled.
4250 static int test_for_valid_rec(struct dyn_ftrace
*rec
)
4252 char str
[KSYM_SYMBOL_LEN
];
4253 unsigned long offset
;
4256 ret
= kallsyms_lookup(rec
->ip
, NULL
, &offset
, NULL
, str
);
4258 /* Weak functions can cause invalid addresses */
4259 if (!ret
|| offset
> FTRACE_MCOUNT_MAX_OFFSET
) {
4260 rec
->flags
|= FTRACE_FL_DISABLED
;
4266 static struct workqueue_struct
*ftrace_check_wq __initdata
;
4267 static struct work_struct ftrace_check_work __initdata
;
4270 * Scan all the mcount/fentry entries to make sure they are valid.
4272 static __init
void ftrace_check_work_func(struct work_struct
*work
)
4274 struct ftrace_page
*pg
;
4275 struct dyn_ftrace
*rec
;
4277 mutex_lock(&ftrace_lock
);
4278 do_for_each_ftrace_rec(pg
, rec
) {
4279 test_for_valid_rec(rec
);
4280 } while_for_each_ftrace_rec();
4281 mutex_unlock(&ftrace_lock
);
4284 static int __init
ftrace_check_for_weak_functions(void)
4286 INIT_WORK(&ftrace_check_work
, ftrace_check_work_func
);
4288 ftrace_check_wq
= alloc_workqueue("ftrace_check_wq", WQ_UNBOUND
, 0);
4290 queue_work(ftrace_check_wq
, &ftrace_check_work
);
4294 static int __init
ftrace_check_sync(void)
4296 /* Make sure the ftrace_check updates are finished */
4297 if (ftrace_check_wq
)
4298 destroy_workqueue(ftrace_check_wq
);
4302 late_initcall_sync(ftrace_check_sync
);
4303 subsys_initcall(ftrace_check_for_weak_functions
);
4305 static int print_rec(struct seq_file
*m
, unsigned long ip
)
4307 unsigned long offset
;
4308 char str
[KSYM_SYMBOL_LEN
];
4312 ret
= kallsyms_lookup(ip
, NULL
, &offset
, &modname
, str
);
4313 /* Weak functions can cause invalid addresses */
4314 if (!ret
|| offset
> FTRACE_MCOUNT_MAX_OFFSET
) {
4315 snprintf(str
, KSYM_SYMBOL_LEN
, "%s_%ld",
4316 FTRACE_INVALID_FUNCTION
, offset
);
4322 seq_printf(m
, " [%s]", modname
);
4323 return ret
== NULL
? -1 : 0;
4326 static inline int test_for_valid_rec(struct dyn_ftrace
*rec
)
4331 static inline int print_rec(struct seq_file
*m
, unsigned long ip
)
4333 seq_printf(m
, "%ps", (void *)ip
);
4338 static int t_show(struct seq_file
*m
, void *v
)
4340 struct ftrace_iterator
*iter
= m
->private;
4341 struct dyn_ftrace
*rec
;
4343 if (iter
->flags
& FTRACE_ITER_PROBE
)
4344 return t_probe_show(m
, iter
);
4346 if (iter
->flags
& FTRACE_ITER_MOD
)
4347 return t_mod_show(m
, iter
);
4349 if (iter
->flags
& FTRACE_ITER_PRINTALL
) {
4350 if (iter
->flags
& FTRACE_ITER_NOTRACE
)
4351 seq_puts(m
, "#### no functions disabled ####\n");
4353 seq_puts(m
, "#### all functions enabled ####\n");
4362 if (iter
->flags
& FTRACE_ITER_ADDRS
)
4363 seq_printf(m
, "%lx ", rec
->ip
);
4365 if (print_rec(m
, rec
->ip
)) {
4366 /* This should only happen when a rec is disabled */
4367 WARN_ON_ONCE(!(rec
->flags
& FTRACE_FL_DISABLED
));
4372 if (iter
->flags
& (FTRACE_ITER_ENABLED
| FTRACE_ITER_TOUCHED
)) {
4373 struct ftrace_ops
*ops
;
4375 seq_printf(m
, " (%ld)%s%s%s%s%s",
4376 ftrace_rec_count(rec
),
4377 rec
->flags
& FTRACE_FL_REGS
? " R" : " ",
4378 rec
->flags
& FTRACE_FL_IPMODIFY
? " I" : " ",
4379 rec
->flags
& FTRACE_FL_DIRECT
? " D" : " ",
4380 rec
->flags
& FTRACE_FL_CALL_OPS
? " O" : " ",
4381 rec
->flags
& FTRACE_FL_MODIFIED
? " M " : " ");
4382 if (rec
->flags
& FTRACE_FL_TRAMP_EN
) {
4383 ops
= ftrace_find_tramp_ops_any(rec
);
4386 seq_printf(m
, "\ttramp: %pS (%pS)",
4387 (void *)ops
->trampoline
,
4389 add_trampoline_func(m
, ops
, rec
);
4390 ops
= ftrace_find_tramp_ops_next(rec
, ops
);
4393 seq_puts(m
, "\ttramp: ERROR!");
4395 add_trampoline_func(m
, NULL
, rec
);
4397 if (rec
->flags
& FTRACE_FL_CALL_OPS_EN
) {
4398 ops
= ftrace_find_unique_ops(rec
);
4400 seq_printf(m
, "\tops: %pS (%pS)",
4403 seq_puts(m
, "\tops: ERROR!");
4406 if (rec
->flags
& FTRACE_FL_DIRECT
) {
4407 unsigned long direct
;
4409 direct
= ftrace_find_rec_direct(rec
->ip
);
4411 seq_printf(m
, "\n\tdirect-->%pS", (void *)direct
);
4420 static const struct seq_operations show_ftrace_seq_ops
= {
4428 ftrace_avail_open(struct inode
*inode
, struct file
*file
)
4430 struct ftrace_iterator
*iter
;
4433 ret
= security_locked_down(LOCKDOWN_TRACEFS
);
4437 if (unlikely(ftrace_disabled
))
4440 iter
= __seq_open_private(file
, &show_ftrace_seq_ops
, sizeof(*iter
));
4444 iter
->pg
= ftrace_pages_start
;
4445 iter
->ops
= &global_ops
;
4451 ftrace_enabled_open(struct inode
*inode
, struct file
*file
)
4453 struct ftrace_iterator
*iter
;
4456 * This shows us what functions are currently being
4457 * traced and by what. Not sure if we want lockdown
4458 * to hide such critical information for an admin.
4459 * Although, perhaps it can show information we don't
4460 * want people to see, but if something is tracing
4461 * something, we probably want to know about it.
4464 iter
= __seq_open_private(file
, &show_ftrace_seq_ops
, sizeof(*iter
));
4468 iter
->pg
= ftrace_pages_start
;
4469 iter
->flags
= FTRACE_ITER_ENABLED
;
4470 iter
->ops
= &global_ops
;
4476 ftrace_touched_open(struct inode
*inode
, struct file
*file
)
4478 struct ftrace_iterator
*iter
;
4481 * This shows us what functions have ever been enabled
4482 * (traced, direct, patched, etc). Not sure if we want lockdown
4483 * to hide such critical information for an admin.
4484 * Although, perhaps it can show information we don't
4485 * want people to see, but if something had traced
4486 * something, we probably want to know about it.
4489 iter
= __seq_open_private(file
, &show_ftrace_seq_ops
, sizeof(*iter
));
4493 iter
->pg
= ftrace_pages_start
;
4494 iter
->flags
= FTRACE_ITER_TOUCHED
;
4495 iter
->ops
= &global_ops
;
4501 ftrace_avail_addrs_open(struct inode
*inode
, struct file
*file
)
4503 struct ftrace_iterator
*iter
;
4506 ret
= security_locked_down(LOCKDOWN_TRACEFS
);
4510 if (unlikely(ftrace_disabled
))
4513 iter
= __seq_open_private(file
, &show_ftrace_seq_ops
, sizeof(*iter
));
4517 iter
->pg
= ftrace_pages_start
;
4518 iter
->flags
= FTRACE_ITER_ADDRS
;
4519 iter
->ops
= &global_ops
;
4525 * ftrace_regex_open - initialize function tracer filter files
4526 * @ops: The ftrace_ops that hold the hash filters
4527 * @flag: The type of filter to process
4528 * @inode: The inode, usually passed in to your open routine
4529 * @file: The file, usually passed in to your open routine
4531 * ftrace_regex_open() initializes the filter files for the
4532 * @ops. Depending on @flag it may process the filter hash or
4533 * the notrace hash of @ops. With this called from the open
4534 * routine, you can use ftrace_filter_write() for the write
4535 * routine if @flag has FTRACE_ITER_FILTER set, or
4536 * ftrace_notrace_write() if @flag has FTRACE_ITER_NOTRACE set.
4537 * tracing_lseek() should be used as the lseek routine, and
4538 * release must call ftrace_regex_release().
4540 * Returns: 0 on success or a negative errno value on failure
4543 ftrace_regex_open(struct ftrace_ops
*ops
, int flag
,
4544 struct inode
*inode
, struct file
*file
)
4546 struct ftrace_iterator
*iter
;
4547 struct ftrace_hash
*hash
;
4548 struct list_head
*mod_head
;
4549 struct trace_array
*tr
= ops
->private;
4552 ftrace_ops_init(ops
);
4554 if (unlikely(ftrace_disabled
))
4557 if (tracing_check_open_get_tr(tr
))
4560 iter
= kzalloc(sizeof(*iter
), GFP_KERNEL
);
4564 if (trace_parser_get_init(&iter
->parser
, FTRACE_BUFF_MAX
))
4571 mutex_lock(&ops
->func_hash
->regex_lock
);
4573 if (flag
& FTRACE_ITER_NOTRACE
) {
4574 hash
= ops
->func_hash
->notrace_hash
;
4575 mod_head
= tr
? &tr
->mod_notrace
: NULL
;
4577 hash
= ops
->func_hash
->filter_hash
;
4578 mod_head
= tr
? &tr
->mod_trace
: NULL
;
4581 iter
->mod_list
= mod_head
;
4583 if (file
->f_mode
& FMODE_WRITE
) {
4584 const int size_bits
= FTRACE_HASH_DEFAULT_BITS
;
4586 if (file
->f_flags
& O_TRUNC
) {
4587 iter
->hash
= alloc_ftrace_hash(size_bits
);
4588 clear_ftrace_mod_list(mod_head
);
4590 iter
->hash
= alloc_and_copy_ftrace_hash(size_bits
, hash
);
4594 trace_parser_put(&iter
->parser
);
4602 if (file
->f_mode
& FMODE_READ
) {
4603 iter
->pg
= ftrace_pages_start
;
4605 ret
= seq_open(file
, &show_ftrace_seq_ops
);
4607 struct seq_file
*m
= file
->private_data
;
4611 free_ftrace_hash(iter
->hash
);
4612 trace_parser_put(&iter
->parser
);
4615 file
->private_data
= iter
;
4618 mutex_unlock(&ops
->func_hash
->regex_lock
);
4624 trace_array_put(tr
);
4631 ftrace_filter_open(struct inode
*inode
, struct file
*file
)
4633 struct ftrace_ops
*ops
= inode
->i_private
;
4635 /* Checks for tracefs lockdown */
4636 return ftrace_regex_open(ops
,
4637 FTRACE_ITER_FILTER
| FTRACE_ITER_DO_PROBES
,
4642 ftrace_notrace_open(struct inode
*inode
, struct file
*file
)
4644 struct ftrace_ops
*ops
= inode
->i_private
;
4646 /* Checks for tracefs lockdown */
4647 return ftrace_regex_open(ops
, FTRACE_ITER_NOTRACE
,
4651 /* Type for quick search ftrace basic regexes (globs) from filter_parse_regex */
4652 struct ftrace_glob
{
4659 * If symbols in an architecture don't correspond exactly to the user-visible
4660 * name of what they represent, it is possible to define this function to
4661 * perform the necessary adjustments.
4663 char * __weak
arch_ftrace_match_adjust(char *str
, const char *search
)
4668 static int ftrace_match(char *str
, struct ftrace_glob
*g
)
4673 str
= arch_ftrace_match_adjust(str
, g
->search
);
4677 if (strcmp(str
, g
->search
) == 0)
4680 case MATCH_FRONT_ONLY
:
4681 if (strncmp(str
, g
->search
, g
->len
) == 0)
4684 case MATCH_MIDDLE_ONLY
:
4685 if (strstr(str
, g
->search
))
4688 case MATCH_END_ONLY
:
4690 if (slen
>= g
->len
&&
4691 memcmp(str
+ slen
- g
->len
, g
->search
, g
->len
) == 0)
4695 if (glob_match(g
->search
, str
))
4704 enter_record(struct ftrace_hash
*hash
, struct dyn_ftrace
*rec
, int clear_filter
)
4706 struct ftrace_func_entry
*entry
;
4709 entry
= ftrace_lookup_ip(hash
, rec
->ip
);
4711 /* Do nothing if it doesn't exist */
4715 free_hash_entry(hash
, entry
);
4717 /* Do nothing if it exists */
4720 if (add_hash_entry(hash
, rec
->ip
) == NULL
)
4727 add_rec_by_index(struct ftrace_hash
*hash
, struct ftrace_glob
*func_g
,
4731 struct ftrace_page
*pg
;
4732 struct dyn_ftrace
*rec
;
4734 /* The index starts at 1 */
4735 if (kstrtoul(func_g
->search
, 0, &index
) || --index
< 0)
4738 do_for_each_ftrace_rec(pg
, rec
) {
4739 if (pg
->index
<= index
) {
4741 /* this is a double loop, break goes to the next page */
4744 rec
= &pg
->records
[index
];
4745 enter_record(hash
, rec
, clear_filter
);
4747 } while_for_each_ftrace_rec();
4751 #ifdef FTRACE_MCOUNT_MAX_OFFSET
4752 static int lookup_ip(unsigned long ip
, char **modname
, char *str
)
4754 unsigned long offset
;
4756 kallsyms_lookup(ip
, NULL
, &offset
, modname
, str
);
4757 if (offset
> FTRACE_MCOUNT_MAX_OFFSET
)
4762 static int lookup_ip(unsigned long ip
, char **modname
, char *str
)
4764 kallsyms_lookup(ip
, NULL
, NULL
, modname
, str
);
4770 ftrace_match_record(struct dyn_ftrace
*rec
, struct ftrace_glob
*func_g
,
4771 struct ftrace_glob
*mod_g
, int exclude_mod
)
4773 char str
[KSYM_SYMBOL_LEN
];
4776 if (lookup_ip(rec
->ip
, &modname
, str
)) {
4777 /* This should only happen when a rec is disabled */
4778 WARN_ON_ONCE(system_state
== SYSTEM_RUNNING
&&
4779 !(rec
->flags
& FTRACE_FL_DISABLED
));
4784 int mod_matches
= (modname
) ? ftrace_match(modname
, mod_g
) : 0;
4786 /* blank module name to match all modules */
4788 /* blank module globbing: modname xor exclude_mod */
4789 if (!exclude_mod
!= !modname
)
4795 * exclude_mod is set to trace everything but the given
4796 * module. If it is set and the module matches, then
4797 * return 0. If it is not set, and the module doesn't match
4798 * also return 0. Otherwise, check the function to see if
4801 if (!mod_matches
== !exclude_mod
)
4804 /* blank search means to match all funcs in the mod */
4809 return ftrace_match(str
, func_g
);
4813 match_records(struct ftrace_hash
*hash
, char *func
, int len
, char *mod
)
4815 struct ftrace_page
*pg
;
4816 struct dyn_ftrace
*rec
;
4817 struct ftrace_glob func_g
= { .type
= MATCH_FULL
};
4818 struct ftrace_glob mod_g
= { .type
= MATCH_FULL
};
4819 struct ftrace_glob
*mod_match
= (mod
) ? &mod_g
: NULL
;
4820 int exclude_mod
= 0;
4823 int clear_filter
= 0;
4826 func_g
.type
= filter_parse_regex(func
, len
, &func_g
.search
,
4828 func_g
.len
= strlen(func_g
.search
);
4832 mod_g
.type
= filter_parse_regex(mod
, strlen(mod
),
4833 &mod_g
.search
, &exclude_mod
);
4834 mod_g
.len
= strlen(mod_g
.search
);
4837 guard(mutex
)(&ftrace_lock
);
4839 if (unlikely(ftrace_disabled
))
4842 if (func_g
.type
== MATCH_INDEX
)
4843 return add_rec_by_index(hash
, &func_g
, clear_filter
);
4845 do_for_each_ftrace_rec(pg
, rec
) {
4847 if (rec
->flags
& FTRACE_FL_DISABLED
)
4850 if (ftrace_match_record(rec
, &func_g
, mod_match
, exclude_mod
)) {
4851 ret
= enter_record(hash
, rec
, clear_filter
);
4857 } while_for_each_ftrace_rec();
4863 ftrace_match_records(struct ftrace_hash
*hash
, char *buff
, int len
)
4865 return match_records(hash
, buff
, len
, NULL
);
4868 static void ftrace_ops_update_code(struct ftrace_ops
*ops
,
4869 struct ftrace_ops_hash
*old_hash
)
4871 struct ftrace_ops
*op
;
4873 if (!ftrace_enabled
)
4876 if (ops
->flags
& FTRACE_OPS_FL_ENABLED
) {
4877 ftrace_run_modify_code(ops
, FTRACE_UPDATE_CALLS
, old_hash
);
4882 * If this is the shared global_ops filter, then we need to
4883 * check if there is another ops that shares it, is enabled.
4884 * If so, we still need to run the modify code.
4886 if (ops
->func_hash
!= &global_ops
.local_hash
)
4889 do_for_each_ftrace_op(op
, ftrace_ops_list
) {
4890 if (op
->func_hash
== &global_ops
.local_hash
&&
4891 op
->flags
& FTRACE_OPS_FL_ENABLED
) {
4892 ftrace_run_modify_code(op
, FTRACE_UPDATE_CALLS
, old_hash
);
4893 /* Only need to do this once */
4896 } while_for_each_ftrace_op(op
);
4899 static int ftrace_hash_move_and_update_ops(struct ftrace_ops
*ops
,
4900 struct ftrace_hash
**orig_hash
,
4901 struct ftrace_hash
*hash
,
4904 if (ops
->flags
& FTRACE_OPS_FL_SUBOP
)
4905 return ftrace_hash_move_and_update_subops(ops
, orig_hash
, hash
, enable
);
4908 * If this ops is not enabled, it could be sharing its filters
4909 * with a subop. If that's the case, update the subop instead of
4910 * this ops. Shared filters are only allowed to have one ops set
4911 * at a time, and if we update the ops that is not enabled,
4912 * it will not affect subops that share it.
4914 if (!(ops
->flags
& FTRACE_OPS_FL_ENABLED
)) {
4915 struct ftrace_ops
*op
;
4917 /* Check if any other manager subops maps to this hash */
4918 do_for_each_ftrace_op(op
, ftrace_ops_list
) {
4919 struct ftrace_ops
*subops
;
4921 list_for_each_entry(subops
, &op
->subop_list
, list
) {
4922 if ((subops
->flags
& FTRACE_OPS_FL_ENABLED
) &&
4923 subops
->func_hash
== ops
->func_hash
) {
4924 return ftrace_hash_move_and_update_subops(subops
, orig_hash
, hash
, enable
);
4927 } while_for_each_ftrace_op(op
);
4930 return __ftrace_hash_move_and_update_ops(ops
, orig_hash
, hash
, enable
);
4933 static bool module_exists(const char *module
)
4935 /* All modules have the symbol __this_module */
4936 static const char this_mod
[] = "__this_module";
4937 char modname
[MAX_PARAM_PREFIX_LEN
+ sizeof(this_mod
) + 2];
4941 n
= snprintf(modname
, sizeof(modname
), "%s:%s", module
, this_mod
);
4943 if (n
> sizeof(modname
) - 1)
4946 val
= module_kallsyms_lookup_name(modname
);
4950 static int cache_mod(struct trace_array
*tr
,
4951 const char *func
, char *module
, int enable
)
4953 struct ftrace_mod_load
*ftrace_mod
, *n
;
4954 struct list_head
*head
= enable
? &tr
->mod_trace
: &tr
->mod_notrace
;
4956 guard(mutex
)(&ftrace_lock
);
4958 /* We do not cache inverse filters */
4959 if (func
[0] == '!') {
4964 /* Look to remove this hash */
4965 list_for_each_entry_safe(ftrace_mod
, n
, head
, list
) {
4966 if (strcmp(ftrace_mod
->module
, module
) != 0)
4969 /* no func matches all */
4970 if (strcmp(func
, "*") == 0 ||
4971 (ftrace_mod
->func
&&
4972 strcmp(ftrace_mod
->func
, func
) == 0)) {
4974 free_ftrace_mod(ftrace_mod
);
4981 /* We only care about modules that have not been loaded yet */
4982 if (module_exists(module
))
4985 /* Save this string off, and execute it when the module is loaded */
4986 return ftrace_add_mod(tr
, func
, module
, enable
);
4990 ftrace_set_regex(struct ftrace_ops
*ops
, unsigned char *buf
, int len
,
4991 int reset
, int enable
);
4993 #ifdef CONFIG_MODULES
4994 static void process_mod_list(struct list_head
*head
, struct ftrace_ops
*ops
,
4995 char *mod
, bool enable
)
4997 struct ftrace_mod_load
*ftrace_mod
, *n
;
4998 struct ftrace_hash
**orig_hash
, *new_hash
;
4999 LIST_HEAD(process_mods
);
5002 mutex_lock(&ops
->func_hash
->regex_lock
);
5005 orig_hash
= &ops
->func_hash
->filter_hash
;
5007 orig_hash
= &ops
->func_hash
->notrace_hash
;
5009 new_hash
= alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS
,
5012 goto out
; /* warn? */
5014 mutex_lock(&ftrace_lock
);
5016 list_for_each_entry_safe(ftrace_mod
, n
, head
, list
) {
5018 if (strcmp(ftrace_mod
->module
, mod
) != 0)
5021 if (ftrace_mod
->func
)
5022 func
= kstrdup(ftrace_mod
->func
, GFP_KERNEL
);
5024 func
= kstrdup("*", GFP_KERNEL
);
5026 if (!func
) /* warn? */
5029 list_move(&ftrace_mod
->list
, &process_mods
);
5031 /* Use the newly allocated func, as it may be "*" */
5032 kfree(ftrace_mod
->func
);
5033 ftrace_mod
->func
= func
;
5036 mutex_unlock(&ftrace_lock
);
5038 list_for_each_entry_safe(ftrace_mod
, n
, &process_mods
, list
) {
5040 func
= ftrace_mod
->func
;
5042 /* Grabs ftrace_lock, which is why we have this extra step */
5043 match_records(new_hash
, func
, strlen(func
), mod
);
5044 free_ftrace_mod(ftrace_mod
);
5047 if (enable
&& list_empty(head
))
5048 new_hash
->flags
&= ~FTRACE_HASH_FL_MOD
;
5050 mutex_lock(&ftrace_lock
);
5052 ftrace_hash_move_and_update_ops(ops
, orig_hash
,
5054 mutex_unlock(&ftrace_lock
);
5057 mutex_unlock(&ops
->func_hash
->regex_lock
);
5059 free_ftrace_hash(new_hash
);
5062 static void process_cached_mods(const char *mod_name
)
5064 struct trace_array
*tr
;
5067 mod
= kstrdup(mod_name
, GFP_KERNEL
);
5071 mutex_lock(&trace_types_lock
);
5072 list_for_each_entry(tr
, &ftrace_trace_arrays
, list
) {
5073 if (!list_empty(&tr
->mod_trace
))
5074 process_mod_list(&tr
->mod_trace
, tr
->ops
, mod
, true);
5075 if (!list_empty(&tr
->mod_notrace
))
5076 process_mod_list(&tr
->mod_notrace
, tr
->ops
, mod
, false);
5078 mutex_unlock(&trace_types_lock
);
5085 * We register the module command as a template to show others how
5086 * to register the a command as well.
5090 ftrace_mod_callback(struct trace_array
*tr
, struct ftrace_hash
*hash
,
5091 char *func_orig
, char *cmd
, char *module
, int enable
)
5099 /* match_records() modifies func, and we need the original */
5100 func
= kstrdup(func_orig
, GFP_KERNEL
);
5105 * cmd == 'mod' because we only registered this func
5106 * for the 'mod' ftrace_func_command.
5107 * But if you register one func with multiple commands,
5108 * you can tell which command was used by the cmd
5111 ret
= match_records(hash
, func
, strlen(func
), module
);
5115 return cache_mod(tr
, func_orig
, module
, enable
);
5121 static struct ftrace_func_command ftrace_mod_cmd
= {
5123 .func
= ftrace_mod_callback
,
5126 static int __init
ftrace_mod_cmd_init(void)
5128 return register_ftrace_command(&ftrace_mod_cmd
);
5130 core_initcall(ftrace_mod_cmd_init
);
5132 static void function_trace_probe_call(unsigned long ip
, unsigned long parent_ip
,
5133 struct ftrace_ops
*op
, struct ftrace_regs
*fregs
)
5135 struct ftrace_probe_ops
*probe_ops
;
5136 struct ftrace_func_probe
*probe
;
5138 probe
= container_of(op
, struct ftrace_func_probe
, ops
);
5139 probe_ops
= probe
->probe_ops
;
5142 * Disable preemption for these calls to prevent a RCU grace
5143 * period. This syncs the hash iteration and freeing of items
5144 * on the hash. rcu_read_lock is too dangerous here.
5146 preempt_disable_notrace();
5147 probe_ops
->func(ip
, parent_ip
, probe
->tr
, probe_ops
, probe
->data
);
5148 preempt_enable_notrace();
5151 struct ftrace_func_map
{
5152 struct ftrace_func_entry entry
;
5156 struct ftrace_func_mapper
{
5157 struct ftrace_hash hash
;
5161 * allocate_ftrace_func_mapper - allocate a new ftrace_func_mapper
5163 * Returns: a ftrace_func_mapper descriptor that can be used to map ips to data.
5165 struct ftrace_func_mapper
*allocate_ftrace_func_mapper(void)
5167 struct ftrace_hash
*hash
;
5170 * The mapper is simply a ftrace_hash, but since the entries
5171 * in the hash are not ftrace_func_entry type, we define it
5172 * as a separate structure.
5174 hash
= alloc_ftrace_hash(FTRACE_HASH_DEFAULT_BITS
);
5175 return (struct ftrace_func_mapper
*)hash
;
5179 * ftrace_func_mapper_find_ip - Find some data mapped to an ip
5180 * @mapper: The mapper that has the ip maps
5181 * @ip: the instruction pointer to find the data for
5183 * Returns: the data mapped to @ip if found otherwise NULL. The return
5184 * is actually the address of the mapper data pointer. The address is
5185 * returned for use cases where the data is no bigger than a long, and
5186 * the user can use the data pointer as its data instead of having to
5187 * allocate more memory for the reference.
5189 void **ftrace_func_mapper_find_ip(struct ftrace_func_mapper
*mapper
,
5192 struct ftrace_func_entry
*entry
;
5193 struct ftrace_func_map
*map
;
5195 entry
= ftrace_lookup_ip(&mapper
->hash
, ip
);
5199 map
= (struct ftrace_func_map
*)entry
;
5204 * ftrace_func_mapper_add_ip - Map some data to an ip
5205 * @mapper: The mapper that has the ip maps
5206 * @ip: The instruction pointer address to map @data to
5207 * @data: The data to map to @ip
5209 * Returns: 0 on success otherwise an error.
5211 int ftrace_func_mapper_add_ip(struct ftrace_func_mapper
*mapper
,
5212 unsigned long ip
, void *data
)
5214 struct ftrace_func_entry
*entry
;
5215 struct ftrace_func_map
*map
;
5217 entry
= ftrace_lookup_ip(&mapper
->hash
, ip
);
5221 map
= kmalloc(sizeof(*map
), GFP_KERNEL
);
5228 __add_hash_entry(&mapper
->hash
, &map
->entry
);
5234 * ftrace_func_mapper_remove_ip - Remove an ip from the mapping
5235 * @mapper: The mapper that has the ip maps
5236 * @ip: The instruction pointer address to remove the data from
5238 * Returns: the data if it is found, otherwise NULL.
5239 * Note, if the data pointer is used as the data itself, (see
5240 * ftrace_func_mapper_find_ip(), then the return value may be meaningless,
5241 * if the data pointer was set to zero.
5243 void *ftrace_func_mapper_remove_ip(struct ftrace_func_mapper
*mapper
,
5246 struct ftrace_func_entry
*entry
;
5247 struct ftrace_func_map
*map
;
5250 entry
= ftrace_lookup_ip(&mapper
->hash
, ip
);
5254 map
= (struct ftrace_func_map
*)entry
;
5257 remove_hash_entry(&mapper
->hash
, entry
);
5264 * free_ftrace_func_mapper - free a mapping of ips and data
5265 * @mapper: The mapper that has the ip maps
5266 * @free_func: A function to be called on each data item.
5268 * This is used to free the function mapper. The @free_func is optional
5269 * and can be used if the data needs to be freed as well.
5271 void free_ftrace_func_mapper(struct ftrace_func_mapper
*mapper
,
5272 ftrace_mapper_func free_func
)
5274 struct ftrace_func_entry
*entry
;
5275 struct ftrace_func_map
*map
;
5276 struct hlist_head
*hhd
;
5282 if (free_func
&& mapper
->hash
.count
) {
5283 size
= 1 << mapper
->hash
.size_bits
;
5284 for (i
= 0; i
< size
; i
++) {
5285 hhd
= &mapper
->hash
.buckets
[i
];
5286 hlist_for_each_entry(entry
, hhd
, hlist
) {
5287 map
= (struct ftrace_func_map
*)entry
;
5292 free_ftrace_hash(&mapper
->hash
);
5295 static void release_probe(struct ftrace_func_probe
*probe
)
5297 struct ftrace_probe_ops
*probe_ops
;
5299 guard(mutex
)(&ftrace_lock
);
5301 WARN_ON(probe
->ref
<= 0);
5303 /* Subtract the ref that was used to protect this instance */
5307 probe_ops
= probe
->probe_ops
;
5309 * Sending zero as ip tells probe_ops to free
5310 * the probe->data itself
5312 if (probe_ops
->free
)
5313 probe_ops
->free(probe_ops
, probe
->tr
, 0, probe
->data
);
5314 list_del(&probe
->list
);
5319 static void acquire_probe_locked(struct ftrace_func_probe
*probe
)
5322 * Add one ref to keep it from being freed when releasing the
5323 * ftrace_lock mutex.
5329 register_ftrace_function_probe(char *glob
, struct trace_array
*tr
,
5330 struct ftrace_probe_ops
*probe_ops
,
5333 struct ftrace_func_probe
*probe
= NULL
, *iter
;
5334 struct ftrace_func_entry
*entry
;
5335 struct ftrace_hash
**orig_hash
;
5336 struct ftrace_hash
*old_hash
;
5337 struct ftrace_hash
*hash
;
5346 /* We do not support '!' for function probes */
5347 if (WARN_ON(glob
[0] == '!'))
5351 mutex_lock(&ftrace_lock
);
5352 /* Check if the probe_ops is already registered */
5353 list_for_each_entry(iter
, &tr
->func_probes
, list
) {
5354 if (iter
->probe_ops
== probe_ops
) {
5360 probe
= kzalloc(sizeof(*probe
), GFP_KERNEL
);
5362 mutex_unlock(&ftrace_lock
);
5365 probe
->probe_ops
= probe_ops
;
5366 probe
->ops
.func
= function_trace_probe_call
;
5368 ftrace_ops_init(&probe
->ops
);
5369 list_add(&probe
->list
, &tr
->func_probes
);
5372 acquire_probe_locked(probe
);
5374 mutex_unlock(&ftrace_lock
);
5377 * Note, there's a small window here that the func_hash->filter_hash
5378 * may be NULL or empty. Need to be careful when reading the loop.
5380 mutex_lock(&probe
->ops
.func_hash
->regex_lock
);
5382 orig_hash
= &probe
->ops
.func_hash
->filter_hash
;
5383 old_hash
= *orig_hash
;
5384 hash
= alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS
, old_hash
);
5391 ret
= ftrace_match_records(hash
, glob
, strlen(glob
));
5393 /* Nothing found? */
5400 size
= 1 << hash
->size_bits
;
5401 for (i
= 0; i
< size
; i
++) {
5402 hlist_for_each_entry(entry
, &hash
->buckets
[i
], hlist
) {
5403 if (ftrace_lookup_ip(old_hash
, entry
->ip
))
5406 * The caller might want to do something special
5407 * for each function we find. We call the callback
5408 * to give the caller an opportunity to do so.
5410 if (probe_ops
->init
) {
5411 ret
= probe_ops
->init(probe_ops
, tr
,
5415 if (probe_ops
->free
&& count
)
5416 probe_ops
->free(probe_ops
, tr
,
5426 mutex_lock(&ftrace_lock
);
5429 /* Nothing was added? */
5434 ret
= ftrace_hash_move_and_update_ops(&probe
->ops
, orig_hash
,
5439 /* One ref for each new function traced */
5440 probe
->ref
+= count
;
5442 if (!(probe
->ops
.flags
& FTRACE_OPS_FL_ENABLED
))
5443 ret
= ftrace_startup(&probe
->ops
, 0);
5446 mutex_unlock(&ftrace_lock
);
5451 mutex_unlock(&probe
->ops
.func_hash
->regex_lock
);
5452 free_ftrace_hash(hash
);
5454 release_probe(probe
);
5459 if (!probe_ops
->free
|| !count
)
5462 /* Failed to do the move, need to call the free functions */
5463 for (i
= 0; i
< size
; i
++) {
5464 hlist_for_each_entry(entry
, &hash
->buckets
[i
], hlist
) {
5465 if (ftrace_lookup_ip(old_hash
, entry
->ip
))
5467 probe_ops
->free(probe_ops
, tr
, entry
->ip
, probe
->data
);
5474 unregister_ftrace_function_probe_func(char *glob
, struct trace_array
*tr
,
5475 struct ftrace_probe_ops
*probe_ops
)
5477 struct ftrace_func_probe
*probe
= NULL
, *iter
;
5478 struct ftrace_ops_hash old_hash_ops
;
5479 struct ftrace_func_entry
*entry
;
5480 struct ftrace_glob func_g
;
5481 struct ftrace_hash
**orig_hash
;
5482 struct ftrace_hash
*old_hash
;
5483 struct ftrace_hash
*hash
= NULL
;
5484 struct hlist_node
*tmp
;
5485 struct hlist_head hhd
;
5486 char str
[KSYM_SYMBOL_LEN
];
5488 int i
, ret
= -ENODEV
;
5491 if (!glob
|| !strlen(glob
) || !strcmp(glob
, "*"))
5492 func_g
.search
= NULL
;
5496 func_g
.type
= filter_parse_regex(glob
, strlen(glob
),
5497 &func_g
.search
, ¬);
5498 func_g
.len
= strlen(func_g
.search
);
5500 /* we do not support '!' for function probes */
5505 mutex_lock(&ftrace_lock
);
5506 /* Check if the probe_ops is already registered */
5507 list_for_each_entry(iter
, &tr
->func_probes
, list
) {
5508 if (iter
->probe_ops
== probe_ops
) {
5514 goto err_unlock_ftrace
;
5517 if (!(probe
->ops
.flags
& FTRACE_OPS_FL_INITIALIZED
))
5518 goto err_unlock_ftrace
;
5520 acquire_probe_locked(probe
);
5522 mutex_unlock(&ftrace_lock
);
5524 mutex_lock(&probe
->ops
.func_hash
->regex_lock
);
5526 orig_hash
= &probe
->ops
.func_hash
->filter_hash
;
5527 old_hash
= *orig_hash
;
5529 if (ftrace_hash_empty(old_hash
))
5532 old_hash_ops
.filter_hash
= old_hash
;
5533 /* Probes only have filters */
5534 old_hash_ops
.notrace_hash
= NULL
;
5537 hash
= alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS
, old_hash
);
5541 INIT_HLIST_HEAD(&hhd
);
5543 size
= 1 << hash
->size_bits
;
5544 for (i
= 0; i
< size
; i
++) {
5545 hlist_for_each_entry_safe(entry
, tmp
, &hash
->buckets
[i
], hlist
) {
5547 if (func_g
.search
) {
5548 kallsyms_lookup(entry
->ip
, NULL
, NULL
,
5550 if (!ftrace_match(str
, &func_g
))
5554 remove_hash_entry(hash
, entry
);
5555 hlist_add_head(&entry
->hlist
, &hhd
);
5559 /* Nothing found? */
5565 mutex_lock(&ftrace_lock
);
5567 WARN_ON(probe
->ref
< count
);
5569 probe
->ref
-= count
;
5571 if (ftrace_hash_empty(hash
))
5572 ftrace_shutdown(&probe
->ops
, 0);
5574 ret
= ftrace_hash_move_and_update_ops(&probe
->ops
, orig_hash
,
5577 /* still need to update the function call sites */
5578 if (ftrace_enabled
&& !ftrace_hash_empty(hash
))
5579 ftrace_run_modify_code(&probe
->ops
, FTRACE_UPDATE_CALLS
,
5583 hlist_for_each_entry_safe(entry
, tmp
, &hhd
, hlist
) {
5584 hlist_del(&entry
->hlist
);
5585 if (probe_ops
->free
)
5586 probe_ops
->free(probe_ops
, tr
, entry
->ip
, probe
->data
);
5589 mutex_unlock(&ftrace_lock
);
5592 mutex_unlock(&probe
->ops
.func_hash
->regex_lock
);
5593 free_ftrace_hash(hash
);
5595 release_probe(probe
);
5600 mutex_unlock(&ftrace_lock
);
5604 void clear_ftrace_function_probes(struct trace_array
*tr
)
5606 struct ftrace_func_probe
*probe
, *n
;
5608 list_for_each_entry_safe(probe
, n
, &tr
->func_probes
, list
)
5609 unregister_ftrace_function_probe_func(NULL
, tr
, probe
->probe_ops
);
5612 static LIST_HEAD(ftrace_commands
);
5613 static DEFINE_MUTEX(ftrace_cmd_mutex
);
5616 * Currently we only register ftrace commands from __init, so mark this
5619 __init
int register_ftrace_command(struct ftrace_func_command
*cmd
)
5621 struct ftrace_func_command
*p
;
5624 mutex_lock(&ftrace_cmd_mutex
);
5625 list_for_each_entry(p
, &ftrace_commands
, list
) {
5626 if (strcmp(cmd
->name
, p
->name
) == 0) {
5631 list_add(&cmd
->list
, &ftrace_commands
);
5633 mutex_unlock(&ftrace_cmd_mutex
);
5639 * Currently we only unregister ftrace commands from __init, so mark
5642 __init
int unregister_ftrace_command(struct ftrace_func_command
*cmd
)
5644 struct ftrace_func_command
*p
, *n
;
5647 mutex_lock(&ftrace_cmd_mutex
);
5648 list_for_each_entry_safe(p
, n
, &ftrace_commands
, list
) {
5649 if (strcmp(cmd
->name
, p
->name
) == 0) {
5651 list_del_init(&p
->list
);
5656 mutex_unlock(&ftrace_cmd_mutex
);
5661 static int ftrace_process_regex(struct ftrace_iterator
*iter
,
5662 char *buff
, int len
, int enable
)
5664 struct ftrace_hash
*hash
= iter
->hash
;
5665 struct trace_array
*tr
= iter
->ops
->private;
5666 char *func
, *command
, *next
= buff
;
5667 struct ftrace_func_command
*p
;
5670 func
= strsep(&next
, ":");
5673 ret
= ftrace_match_records(hash
, func
, len
);
5683 command
= strsep(&next
, ":");
5685 mutex_lock(&ftrace_cmd_mutex
);
5686 list_for_each_entry(p
, &ftrace_commands
, list
) {
5687 if (strcmp(p
->name
, command
) == 0) {
5688 ret
= p
->func(tr
, hash
, func
, command
, next
, enable
);
5693 mutex_unlock(&ftrace_cmd_mutex
);
5699 ftrace_regex_write(struct file
*file
, const char __user
*ubuf
,
5700 size_t cnt
, loff_t
*ppos
, int enable
)
5702 struct ftrace_iterator
*iter
;
5703 struct trace_parser
*parser
;
5709 if (file
->f_mode
& FMODE_READ
) {
5710 struct seq_file
*m
= file
->private_data
;
5713 iter
= file
->private_data
;
5715 if (unlikely(ftrace_disabled
))
5718 /* iter->hash is a local copy, so we don't need regex_lock */
5720 parser
= &iter
->parser
;
5721 read
= trace_get_user(parser
, ubuf
, cnt
, ppos
);
5723 if (read
>= 0 && trace_parser_loaded(parser
) &&
5724 !trace_parser_cont(parser
)) {
5725 ret
= ftrace_process_regex(iter
, parser
->buffer
,
5726 parser
->idx
, enable
);
5727 trace_parser_clear(parser
);
5738 ftrace_filter_write(struct file
*file
, const char __user
*ubuf
,
5739 size_t cnt
, loff_t
*ppos
)
5741 return ftrace_regex_write(file
, ubuf
, cnt
, ppos
, 1);
5745 ftrace_notrace_write(struct file
*file
, const char __user
*ubuf
,
5746 size_t cnt
, loff_t
*ppos
)
5748 return ftrace_regex_write(file
, ubuf
, cnt
, ppos
, 0);
5752 __ftrace_match_addr(struct ftrace_hash
*hash
, unsigned long ip
, int remove
)
5754 struct ftrace_func_entry
*entry
;
5756 ip
= ftrace_location(ip
);
5761 entry
= ftrace_lookup_ip(hash
, ip
);
5764 free_hash_entry(hash
, entry
);
5768 entry
= add_hash_entry(hash
, ip
);
5769 return entry
? 0 : -ENOMEM
;
5773 ftrace_match_addr(struct ftrace_hash
*hash
, unsigned long *ips
,
5774 unsigned int cnt
, int remove
)
5779 for (i
= 0; i
< cnt
; i
++) {
5780 err
= __ftrace_match_addr(hash
, ips
[i
], remove
);
5783 * This expects the @hash is a temporary hash and if this
5784 * fails the caller must free the @hash.
5793 ftrace_set_hash(struct ftrace_ops
*ops
, unsigned char *buf
, int len
,
5794 unsigned long *ips
, unsigned int cnt
,
5795 int remove
, int reset
, int enable
)
5797 struct ftrace_hash
**orig_hash
;
5798 struct ftrace_hash
*hash
;
5801 if (unlikely(ftrace_disabled
))
5804 mutex_lock(&ops
->func_hash
->regex_lock
);
5807 orig_hash
= &ops
->func_hash
->filter_hash
;
5809 orig_hash
= &ops
->func_hash
->notrace_hash
;
5812 hash
= alloc_ftrace_hash(FTRACE_HASH_DEFAULT_BITS
);
5814 hash
= alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS
, *orig_hash
);
5818 goto out_regex_unlock
;
5821 if (buf
&& !ftrace_match_records(hash
, buf
, len
)) {
5823 goto out_regex_unlock
;
5826 ret
= ftrace_match_addr(hash
, ips
, cnt
, remove
);
5828 goto out_regex_unlock
;
5831 mutex_lock(&ftrace_lock
);
5832 ret
= ftrace_hash_move_and_update_ops(ops
, orig_hash
, hash
, enable
);
5833 mutex_unlock(&ftrace_lock
);
5836 mutex_unlock(&ops
->func_hash
->regex_lock
);
5838 free_ftrace_hash(hash
);
5843 ftrace_set_addr(struct ftrace_ops
*ops
, unsigned long *ips
, unsigned int cnt
,
5844 int remove
, int reset
, int enable
)
5846 return ftrace_set_hash(ops
, NULL
, 0, ips
, cnt
, remove
, reset
, enable
);
5849 #ifdef CONFIG_DYNAMIC_FTRACE_WITH_DIRECT_CALLS
5851 static int register_ftrace_function_nolock(struct ftrace_ops
*ops
);
5854 * If there are multiple ftrace_ops, use SAVE_REGS by default, so that direct
5855 * call will be jumped from ftrace_regs_caller. Only if the architecture does
5856 * not support ftrace_regs_caller but direct_call, use SAVE_ARGS so that it
5857 * jumps from ftrace_caller for multiple ftrace_ops.
5859 #ifndef CONFIG_HAVE_DYNAMIC_FTRACE_WITH_REGS
5860 #define MULTI_FLAGS (FTRACE_OPS_FL_DIRECT | FTRACE_OPS_FL_SAVE_ARGS)
5862 #define MULTI_FLAGS (FTRACE_OPS_FL_DIRECT | FTRACE_OPS_FL_SAVE_REGS)
5865 static int check_direct_multi(struct ftrace_ops
*ops
)
5867 if (!(ops
->flags
& FTRACE_OPS_FL_INITIALIZED
))
5869 if ((ops
->flags
& MULTI_FLAGS
) != MULTI_FLAGS
)
5874 static void remove_direct_functions_hash(struct ftrace_hash
*hash
, unsigned long addr
)
5876 struct ftrace_func_entry
*entry
, *del
;
5879 size
= 1 << hash
->size_bits
;
5880 for (i
= 0; i
< size
; i
++) {
5881 hlist_for_each_entry(entry
, &hash
->buckets
[i
], hlist
) {
5882 del
= __ftrace_lookup_ip(direct_functions
, entry
->ip
);
5883 if (del
&& del
->direct
== addr
) {
5884 remove_hash_entry(direct_functions
, del
);
5891 static void register_ftrace_direct_cb(struct rcu_head
*rhp
)
5893 struct ftrace_hash
*fhp
= container_of(rhp
, struct ftrace_hash
, rcu
);
5895 free_ftrace_hash(fhp
);
5899 * register_ftrace_direct - Call a custom trampoline directly
5900 * for multiple functions registered in @ops
5901 * @ops: The address of the struct ftrace_ops object
5902 * @addr: The address of the trampoline to call at @ops functions
5904 * This is used to connect a direct calls to @addr from the nop locations
5905 * of the functions registered in @ops (with by ftrace_set_filter_ip
5908 * The location that it calls (@addr) must be able to handle a direct call,
5909 * and save the parameters of the function being traced, and restore them
5910 * (or inject new ones if needed), before returning.
5914 * -EINVAL - The @ops object was already registered with this call or
5915 * when there are no functions in @ops object.
5916 * -EBUSY - Another direct function is already attached (there can be only one)
5917 * -ENODEV - @ip does not point to a ftrace nop location (or not supported)
5918 * -ENOMEM - There was an allocation failure.
5920 int register_ftrace_direct(struct ftrace_ops
*ops
, unsigned long addr
)
5922 struct ftrace_hash
*hash
, *new_hash
= NULL
, *free_hash
= NULL
;
5923 struct ftrace_func_entry
*entry
, *new;
5924 int err
= -EBUSY
, size
, i
;
5926 if (ops
->func
|| ops
->trampoline
)
5928 if (!(ops
->flags
& FTRACE_OPS_FL_INITIALIZED
))
5930 if (ops
->flags
& FTRACE_OPS_FL_ENABLED
)
5933 hash
= ops
->func_hash
->filter_hash
;
5934 if (ftrace_hash_empty(hash
))
5937 mutex_lock(&direct_mutex
);
5939 /* Make sure requested entries are not already registered.. */
5940 size
= 1 << hash
->size_bits
;
5941 for (i
= 0; i
< size
; i
++) {
5942 hlist_for_each_entry(entry
, &hash
->buckets
[i
], hlist
) {
5943 if (ftrace_find_rec_direct(entry
->ip
))
5950 /* Make a copy hash to place the new and the old entries in */
5951 size
= hash
->count
+ direct_functions
->count
;
5954 new_hash
= alloc_ftrace_hash(fls(size
));
5958 /* Now copy over the existing direct entries */
5959 size
= 1 << direct_functions
->size_bits
;
5960 for (i
= 0; i
< size
; i
++) {
5961 hlist_for_each_entry(entry
, &direct_functions
->buckets
[i
], hlist
) {
5962 new = add_hash_entry(new_hash
, entry
->ip
);
5965 new->direct
= entry
->direct
;
5969 /* ... and add the new entries */
5970 size
= 1 << hash
->size_bits
;
5971 for (i
= 0; i
< size
; i
++) {
5972 hlist_for_each_entry(entry
, &hash
->buckets
[i
], hlist
) {
5973 new = add_hash_entry(new_hash
, entry
->ip
);
5976 /* Update both the copy and the hash entry */
5978 entry
->direct
= addr
;
5982 free_hash
= direct_functions
;
5983 rcu_assign_pointer(direct_functions
, new_hash
);
5986 ops
->func
= call_direct_funcs
;
5987 ops
->flags
= MULTI_FLAGS
;
5988 ops
->trampoline
= FTRACE_REGS_ADDR
;
5989 ops
->direct_call
= addr
;
5991 err
= register_ftrace_function_nolock(ops
);
5994 mutex_unlock(&direct_mutex
);
5996 if (free_hash
&& free_hash
!= EMPTY_HASH
)
5997 call_rcu_tasks(&free_hash
->rcu
, register_ftrace_direct_cb
);
6000 free_ftrace_hash(new_hash
);
6004 EXPORT_SYMBOL_GPL(register_ftrace_direct
);
6007 * unregister_ftrace_direct - Remove calls to custom trampoline
6008 * previously registered by register_ftrace_direct for @ops object.
6009 * @ops: The address of the struct ftrace_ops object
6010 * @addr: The address of the direct function that is called by the @ops functions
6011 * @free_filters: Set to true to remove all filters for the ftrace_ops, false otherwise
6013 * This is used to remove a direct calls to @addr from the nop locations
6014 * of the functions registered in @ops (with by ftrace_set_filter_ip
6019 * -EINVAL - The @ops object was not properly registered.
6021 int unregister_ftrace_direct(struct ftrace_ops
*ops
, unsigned long addr
,
6024 struct ftrace_hash
*hash
= ops
->func_hash
->filter_hash
;
6027 if (check_direct_multi(ops
))
6029 if (!(ops
->flags
& FTRACE_OPS_FL_ENABLED
))
6032 mutex_lock(&direct_mutex
);
6033 err
= unregister_ftrace_function(ops
);
6034 remove_direct_functions_hash(hash
, addr
);
6035 mutex_unlock(&direct_mutex
);
6037 /* cleanup for possible another register call */
6039 ops
->trampoline
= 0;
6042 ftrace_free_filter(ops
);
6045 EXPORT_SYMBOL_GPL(unregister_ftrace_direct
);
6048 __modify_ftrace_direct(struct ftrace_ops
*ops
, unsigned long addr
)
6050 struct ftrace_hash
*hash
;
6051 struct ftrace_func_entry
*entry
, *iter
;
6052 static struct ftrace_ops tmp_ops
= {
6053 .func
= ftrace_stub
,
6054 .flags
= FTRACE_OPS_FL_STUB
,
6059 lockdep_assert_held_once(&direct_mutex
);
6061 /* Enable the tmp_ops to have the same functions as the direct ops */
6062 ftrace_ops_init(&tmp_ops
);
6063 tmp_ops
.func_hash
= ops
->func_hash
;
6064 tmp_ops
.direct_call
= addr
;
6066 err
= register_ftrace_function_nolock(&tmp_ops
);
6071 * Now the ftrace_ops_list_func() is called to do the direct callers.
6072 * We can safely change the direct functions attached to each entry.
6074 mutex_lock(&ftrace_lock
);
6076 hash
= ops
->func_hash
->filter_hash
;
6077 size
= 1 << hash
->size_bits
;
6078 for (i
= 0; i
< size
; i
++) {
6079 hlist_for_each_entry(iter
, &hash
->buckets
[i
], hlist
) {
6080 entry
= __ftrace_lookup_ip(direct_functions
, iter
->ip
);
6083 entry
->direct
= addr
;
6086 /* Prevent store tearing if a trampoline concurrently accesses the value */
6087 WRITE_ONCE(ops
->direct_call
, addr
);
6089 mutex_unlock(&ftrace_lock
);
6091 /* Removing the tmp_ops will add the updated direct callers to the functions */
6092 unregister_ftrace_function(&tmp_ops
);
6098 * modify_ftrace_direct_nolock - Modify an existing direct 'multi' call
6099 * to call something else
6100 * @ops: The address of the struct ftrace_ops object
6101 * @addr: The address of the new trampoline to call at @ops functions
6103 * This is used to unregister currently registered direct caller and
6104 * register new one @addr on functions registered in @ops object.
6106 * Note there's window between ftrace_shutdown and ftrace_startup calls
6107 * where there will be no callbacks called.
6109 * Caller should already have direct_mutex locked, so we don't lock
6110 * direct_mutex here.
6112 * Returns: zero on success. Non zero on error, which includes:
6113 * -EINVAL - The @ops object was not properly registered.
6115 int modify_ftrace_direct_nolock(struct ftrace_ops
*ops
, unsigned long addr
)
6117 if (check_direct_multi(ops
))
6119 if (!(ops
->flags
& FTRACE_OPS_FL_ENABLED
))
6122 return __modify_ftrace_direct(ops
, addr
);
6124 EXPORT_SYMBOL_GPL(modify_ftrace_direct_nolock
);
6127 * modify_ftrace_direct - Modify an existing direct 'multi' call
6128 * to call something else
6129 * @ops: The address of the struct ftrace_ops object
6130 * @addr: The address of the new trampoline to call at @ops functions
6132 * This is used to unregister currently registered direct caller and
6133 * register new one @addr on functions registered in @ops object.
6135 * Note there's window between ftrace_shutdown and ftrace_startup calls
6136 * where there will be no callbacks called.
6138 * Returns: zero on success. Non zero on error, which includes:
6139 * -EINVAL - The @ops object was not properly registered.
6141 int modify_ftrace_direct(struct ftrace_ops
*ops
, unsigned long addr
)
6145 if (check_direct_multi(ops
))
6147 if (!(ops
->flags
& FTRACE_OPS_FL_ENABLED
))
6150 mutex_lock(&direct_mutex
);
6151 err
= __modify_ftrace_direct(ops
, addr
);
6152 mutex_unlock(&direct_mutex
);
6155 EXPORT_SYMBOL_GPL(modify_ftrace_direct
);
6156 #endif /* CONFIG_DYNAMIC_FTRACE_WITH_DIRECT_CALLS */
6159 * ftrace_set_filter_ip - set a function to filter on in ftrace by address
6160 * @ops: the ops to set the filter with
6161 * @ip: the address to add to or remove from the filter.
6162 * @remove: non zero to remove the ip from the filter
6163 * @reset: non zero to reset all filters before applying this filter.
6165 * Filters denote which functions should be enabled when tracing is enabled
6166 * If @ip is NULL, it fails to update filter.
6168 * This can allocate memory which must be freed before @ops can be freed,
6169 * either by removing each filtered addr or by using
6170 * ftrace_free_filter(@ops).
6172 int ftrace_set_filter_ip(struct ftrace_ops
*ops
, unsigned long ip
,
6173 int remove
, int reset
)
6175 ftrace_ops_init(ops
);
6176 return ftrace_set_addr(ops
, &ip
, 1, remove
, reset
, 1);
6178 EXPORT_SYMBOL_GPL(ftrace_set_filter_ip
);
6181 * ftrace_set_filter_ips - set functions to filter on in ftrace by addresses
6182 * @ops: the ops to set the filter with
6183 * @ips: the array of addresses to add to or remove from the filter.
6184 * @cnt: the number of addresses in @ips
6185 * @remove: non zero to remove ips from the filter
6186 * @reset: non zero to reset all filters before applying this filter.
6188 * Filters denote which functions should be enabled when tracing is enabled
6189 * If @ips array or any ip specified within is NULL , it fails to update filter.
6191 * This can allocate memory which must be freed before @ops can be freed,
6192 * either by removing each filtered addr or by using
6193 * ftrace_free_filter(@ops).
6195 int ftrace_set_filter_ips(struct ftrace_ops
*ops
, unsigned long *ips
,
6196 unsigned int cnt
, int remove
, int reset
)
6198 ftrace_ops_init(ops
);
6199 return ftrace_set_addr(ops
, ips
, cnt
, remove
, reset
, 1);
6201 EXPORT_SYMBOL_GPL(ftrace_set_filter_ips
);
6204 * ftrace_ops_set_global_filter - setup ops to use global filters
6205 * @ops: the ops which will use the global filters
6207 * ftrace users who need global function trace filtering should call this.
6208 * It can set the global filter only if ops were not initialized before.
6210 void ftrace_ops_set_global_filter(struct ftrace_ops
*ops
)
6212 if (ops
->flags
& FTRACE_OPS_FL_INITIALIZED
)
6215 ftrace_ops_init(ops
);
6216 ops
->func_hash
= &global_ops
.local_hash
;
6218 EXPORT_SYMBOL_GPL(ftrace_ops_set_global_filter
);
6221 ftrace_set_regex(struct ftrace_ops
*ops
, unsigned char *buf
, int len
,
6222 int reset
, int enable
)
6224 return ftrace_set_hash(ops
, buf
, len
, NULL
, 0, 0, reset
, enable
);
6228 * ftrace_set_filter - set a function to filter on in ftrace
6229 * @ops: the ops to set the filter with
6230 * @buf: the string that holds the function filter text.
6231 * @len: the length of the string.
6232 * @reset: non-zero to reset all filters before applying this filter.
6234 * Filters denote which functions should be enabled when tracing is enabled.
6235 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
6237 * This can allocate memory which must be freed before @ops can be freed,
6238 * either by removing each filtered addr or by using
6239 * ftrace_free_filter(@ops).
6241 int ftrace_set_filter(struct ftrace_ops
*ops
, unsigned char *buf
,
6244 ftrace_ops_init(ops
);
6245 return ftrace_set_regex(ops
, buf
, len
, reset
, 1);
6247 EXPORT_SYMBOL_GPL(ftrace_set_filter
);
6250 * ftrace_set_notrace - set a function to not trace in ftrace
6251 * @ops: the ops to set the notrace filter with
6252 * @buf: the string that holds the function notrace text.
6253 * @len: the length of the string.
6254 * @reset: non-zero to reset all filters before applying this filter.
6256 * Notrace Filters denote which functions should not be enabled when tracing
6257 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
6260 * This can allocate memory which must be freed before @ops can be freed,
6261 * either by removing each filtered addr or by using
6262 * ftrace_free_filter(@ops).
6264 int ftrace_set_notrace(struct ftrace_ops
*ops
, unsigned char *buf
,
6267 ftrace_ops_init(ops
);
6268 return ftrace_set_regex(ops
, buf
, len
, reset
, 0);
6270 EXPORT_SYMBOL_GPL(ftrace_set_notrace
);
6272 * ftrace_set_global_filter - set a function to filter on with global tracers
6273 * @buf: the string that holds the function filter text.
6274 * @len: the length of the string.
6275 * @reset: non-zero to reset all filters before applying this filter.
6277 * Filters denote which functions should be enabled when tracing is enabled.
6278 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
6280 void ftrace_set_global_filter(unsigned char *buf
, int len
, int reset
)
6282 ftrace_set_regex(&global_ops
, buf
, len
, reset
, 1);
6284 EXPORT_SYMBOL_GPL(ftrace_set_global_filter
);
6287 * ftrace_set_global_notrace - set a function to not trace with global tracers
6288 * @buf: the string that holds the function notrace text.
6289 * @len: the length of the string.
6290 * @reset: non-zero to reset all filters before applying this filter.
6292 * Notrace Filters denote which functions should not be enabled when tracing
6293 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
6296 void ftrace_set_global_notrace(unsigned char *buf
, int len
, int reset
)
6298 ftrace_set_regex(&global_ops
, buf
, len
, reset
, 0);
6300 EXPORT_SYMBOL_GPL(ftrace_set_global_notrace
);
6303 * command line interface to allow users to set filters on boot up.
6305 #define FTRACE_FILTER_SIZE COMMAND_LINE_SIZE
6306 static char ftrace_notrace_buf
[FTRACE_FILTER_SIZE
] __initdata
;
6307 static char ftrace_filter_buf
[FTRACE_FILTER_SIZE
] __initdata
;
6309 /* Used by function selftest to not test if filter is set */
6310 bool ftrace_filter_param __initdata
;
6312 static int __init
set_ftrace_notrace(char *str
)
6314 ftrace_filter_param
= true;
6315 strscpy(ftrace_notrace_buf
, str
, FTRACE_FILTER_SIZE
);
6318 __setup("ftrace_notrace=", set_ftrace_notrace
);
6320 static int __init
set_ftrace_filter(char *str
)
6322 ftrace_filter_param
= true;
6323 strscpy(ftrace_filter_buf
, str
, FTRACE_FILTER_SIZE
);
6326 __setup("ftrace_filter=", set_ftrace_filter
);
6328 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
6329 static char ftrace_graph_buf
[FTRACE_FILTER_SIZE
] __initdata
;
6330 static char ftrace_graph_notrace_buf
[FTRACE_FILTER_SIZE
] __initdata
;
6331 static int ftrace_graph_set_hash(struct ftrace_hash
*hash
, char *buffer
);
6333 static int __init
set_graph_function(char *str
)
6335 strscpy(ftrace_graph_buf
, str
, FTRACE_FILTER_SIZE
);
6338 __setup("ftrace_graph_filter=", set_graph_function
);
6340 static int __init
set_graph_notrace_function(char *str
)
6342 strscpy(ftrace_graph_notrace_buf
, str
, FTRACE_FILTER_SIZE
);
6345 __setup("ftrace_graph_notrace=", set_graph_notrace_function
);
6347 static int __init
set_graph_max_depth_function(char *str
)
6349 if (!str
|| kstrtouint(str
, 0, &fgraph_max_depth
))
6353 __setup("ftrace_graph_max_depth=", set_graph_max_depth_function
);
6355 static void __init
set_ftrace_early_graph(char *buf
, int enable
)
6359 struct ftrace_hash
*hash
;
6361 hash
= alloc_ftrace_hash(FTRACE_HASH_DEFAULT_BITS
);
6362 if (MEM_FAIL(!hash
, "Failed to allocate hash\n"))
6366 func
= strsep(&buf
, ",");
6367 /* we allow only one expression at a time */
6368 ret
= ftrace_graph_set_hash(hash
, func
);
6370 printk(KERN_DEBUG
"ftrace: function %s not "
6371 "traceable\n", func
);
6375 ftrace_graph_hash
= hash
;
6377 ftrace_graph_notrace_hash
= hash
;
6379 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
6382 ftrace_set_early_filter(struct ftrace_ops
*ops
, char *buf
, int enable
)
6386 ftrace_ops_init(ops
);
6389 func
= strsep(&buf
, ",");
6390 ftrace_set_regex(ops
, func
, strlen(func
), 0, enable
);
6394 static void __init
set_ftrace_early_filters(void)
6396 if (ftrace_filter_buf
[0])
6397 ftrace_set_early_filter(&global_ops
, ftrace_filter_buf
, 1);
6398 if (ftrace_notrace_buf
[0])
6399 ftrace_set_early_filter(&global_ops
, ftrace_notrace_buf
, 0);
6400 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
6401 if (ftrace_graph_buf
[0])
6402 set_ftrace_early_graph(ftrace_graph_buf
, 1);
6403 if (ftrace_graph_notrace_buf
[0])
6404 set_ftrace_early_graph(ftrace_graph_notrace_buf
, 0);
6405 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
6408 int ftrace_regex_release(struct inode
*inode
, struct file
*file
)
6410 struct seq_file
*m
= (struct seq_file
*)file
->private_data
;
6411 struct ftrace_iterator
*iter
;
6412 struct ftrace_hash
**orig_hash
;
6413 struct trace_parser
*parser
;
6416 if (file
->f_mode
& FMODE_READ
) {
6418 seq_release(inode
, file
);
6420 iter
= file
->private_data
;
6422 parser
= &iter
->parser
;
6423 if (trace_parser_loaded(parser
)) {
6424 int enable
= !(iter
->flags
& FTRACE_ITER_NOTRACE
);
6426 ftrace_process_regex(iter
, parser
->buffer
,
6427 parser
->idx
, enable
);
6430 trace_parser_put(parser
);
6432 mutex_lock(&iter
->ops
->func_hash
->regex_lock
);
6434 if (file
->f_mode
& FMODE_WRITE
) {
6435 filter_hash
= !!(iter
->flags
& FTRACE_ITER_FILTER
);
6438 orig_hash
= &iter
->ops
->func_hash
->filter_hash
;
6440 if (list_empty(&iter
->tr
->mod_trace
))
6441 iter
->hash
->flags
&= ~FTRACE_HASH_FL_MOD
;
6443 iter
->hash
->flags
|= FTRACE_HASH_FL_MOD
;
6446 orig_hash
= &iter
->ops
->func_hash
->notrace_hash
;
6448 mutex_lock(&ftrace_lock
);
6449 ftrace_hash_move_and_update_ops(iter
->ops
, orig_hash
,
6450 iter
->hash
, filter_hash
);
6451 mutex_unlock(&ftrace_lock
);
6453 /* For read only, the hash is the ops hash */
6457 mutex_unlock(&iter
->ops
->func_hash
->regex_lock
);
6458 free_ftrace_hash(iter
->hash
);
6460 trace_array_put(iter
->tr
);
6466 static const struct file_operations ftrace_avail_fops
= {
6467 .open
= ftrace_avail_open
,
6469 .llseek
= seq_lseek
,
6470 .release
= seq_release_private
,
6473 static const struct file_operations ftrace_enabled_fops
= {
6474 .open
= ftrace_enabled_open
,
6476 .llseek
= seq_lseek
,
6477 .release
= seq_release_private
,
6480 static const struct file_operations ftrace_touched_fops
= {
6481 .open
= ftrace_touched_open
,
6483 .llseek
= seq_lseek
,
6484 .release
= seq_release_private
,
6487 static const struct file_operations ftrace_avail_addrs_fops
= {
6488 .open
= ftrace_avail_addrs_open
,
6490 .llseek
= seq_lseek
,
6491 .release
= seq_release_private
,
6494 static const struct file_operations ftrace_filter_fops
= {
6495 .open
= ftrace_filter_open
,
6497 .write
= ftrace_filter_write
,
6498 .llseek
= tracing_lseek
,
6499 .release
= ftrace_regex_release
,
6502 static const struct file_operations ftrace_notrace_fops
= {
6503 .open
= ftrace_notrace_open
,
6505 .write
= ftrace_notrace_write
,
6506 .llseek
= tracing_lseek
,
6507 .release
= ftrace_regex_release
,
6510 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
6512 static DEFINE_MUTEX(graph_lock
);
6514 struct ftrace_hash __rcu
*ftrace_graph_hash
= EMPTY_HASH
;
6515 struct ftrace_hash __rcu
*ftrace_graph_notrace_hash
= EMPTY_HASH
;
6517 enum graph_filter_type
{
6518 GRAPH_FILTER_NOTRACE
= 0,
6519 GRAPH_FILTER_FUNCTION
,
6522 #define FTRACE_GRAPH_EMPTY ((void *)1)
6524 struct ftrace_graph_data
{
6525 struct ftrace_hash
*hash
;
6526 struct ftrace_func_entry
*entry
;
6527 int idx
; /* for hash table iteration */
6528 enum graph_filter_type type
;
6529 struct ftrace_hash
*new_hash
;
6530 const struct seq_operations
*seq_ops
;
6531 struct trace_parser parser
;
6535 __g_next(struct seq_file
*m
, loff_t
*pos
)
6537 struct ftrace_graph_data
*fgd
= m
->private;
6538 struct ftrace_func_entry
*entry
= fgd
->entry
;
6539 struct hlist_head
*head
;
6540 int i
, idx
= fgd
->idx
;
6542 if (*pos
>= fgd
->hash
->count
)
6546 hlist_for_each_entry_continue(entry
, hlist
) {
6554 for (i
= idx
; i
< 1 << fgd
->hash
->size_bits
; i
++) {
6555 head
= &fgd
->hash
->buckets
[i
];
6556 hlist_for_each_entry(entry
, head
, hlist
) {
6566 g_next(struct seq_file
*m
, void *v
, loff_t
*pos
)
6569 return __g_next(m
, pos
);
6572 static void *g_start(struct seq_file
*m
, loff_t
*pos
)
6574 struct ftrace_graph_data
*fgd
= m
->private;
6576 mutex_lock(&graph_lock
);
6578 if (fgd
->type
== GRAPH_FILTER_FUNCTION
)
6579 fgd
->hash
= rcu_dereference_protected(ftrace_graph_hash
,
6580 lockdep_is_held(&graph_lock
));
6582 fgd
->hash
= rcu_dereference_protected(ftrace_graph_notrace_hash
,
6583 lockdep_is_held(&graph_lock
));
6585 /* Nothing, tell g_show to print all functions are enabled */
6586 if (ftrace_hash_empty(fgd
->hash
) && !*pos
)
6587 return FTRACE_GRAPH_EMPTY
;
6591 return __g_next(m
, pos
);
6594 static void g_stop(struct seq_file
*m
, void *p
)
6596 mutex_unlock(&graph_lock
);
6599 static int g_show(struct seq_file
*m
, void *v
)
6601 struct ftrace_func_entry
*entry
= v
;
6606 if (entry
== FTRACE_GRAPH_EMPTY
) {
6607 struct ftrace_graph_data
*fgd
= m
->private;
6609 if (fgd
->type
== GRAPH_FILTER_FUNCTION
)
6610 seq_puts(m
, "#### all functions enabled ####\n");
6612 seq_puts(m
, "#### no functions disabled ####\n");
6616 seq_printf(m
, "%ps\n", (void *)entry
->ip
);
6621 static const struct seq_operations ftrace_graph_seq_ops
= {
6629 __ftrace_graph_open(struct inode
*inode
, struct file
*file
,
6630 struct ftrace_graph_data
*fgd
)
6633 struct ftrace_hash
*new_hash
= NULL
;
6635 ret
= security_locked_down(LOCKDOWN_TRACEFS
);
6639 if (file
->f_mode
& FMODE_WRITE
) {
6640 const int size_bits
= FTRACE_HASH_DEFAULT_BITS
;
6642 if (trace_parser_get_init(&fgd
->parser
, FTRACE_BUFF_MAX
))
6645 if (file
->f_flags
& O_TRUNC
)
6646 new_hash
= alloc_ftrace_hash(size_bits
);
6648 new_hash
= alloc_and_copy_ftrace_hash(size_bits
,
6656 if (file
->f_mode
& FMODE_READ
) {
6657 ret
= seq_open(file
, &ftrace_graph_seq_ops
);
6659 struct seq_file
*m
= file
->private_data
;
6663 free_ftrace_hash(new_hash
);
6667 file
->private_data
= fgd
;
6670 if (ret
< 0 && file
->f_mode
& FMODE_WRITE
)
6671 trace_parser_put(&fgd
->parser
);
6673 fgd
->new_hash
= new_hash
;
6676 * All uses of fgd->hash must be taken with the graph_lock
6677 * held. The graph_lock is going to be released, so force
6678 * fgd->hash to be reinitialized when it is taken again.
6686 ftrace_graph_open(struct inode
*inode
, struct file
*file
)
6688 struct ftrace_graph_data
*fgd
;
6691 if (unlikely(ftrace_disabled
))
6694 fgd
= kmalloc(sizeof(*fgd
), GFP_KERNEL
);
6698 mutex_lock(&graph_lock
);
6700 fgd
->hash
= rcu_dereference_protected(ftrace_graph_hash
,
6701 lockdep_is_held(&graph_lock
));
6702 fgd
->type
= GRAPH_FILTER_FUNCTION
;
6703 fgd
->seq_ops
= &ftrace_graph_seq_ops
;
6705 ret
= __ftrace_graph_open(inode
, file
, fgd
);
6709 mutex_unlock(&graph_lock
);
6714 ftrace_graph_notrace_open(struct inode
*inode
, struct file
*file
)
6716 struct ftrace_graph_data
*fgd
;
6719 if (unlikely(ftrace_disabled
))
6722 fgd
= kmalloc(sizeof(*fgd
), GFP_KERNEL
);
6726 mutex_lock(&graph_lock
);
6728 fgd
->hash
= rcu_dereference_protected(ftrace_graph_notrace_hash
,
6729 lockdep_is_held(&graph_lock
));
6730 fgd
->type
= GRAPH_FILTER_NOTRACE
;
6731 fgd
->seq_ops
= &ftrace_graph_seq_ops
;
6733 ret
= __ftrace_graph_open(inode
, file
, fgd
);
6737 mutex_unlock(&graph_lock
);
6742 ftrace_graph_release(struct inode
*inode
, struct file
*file
)
6744 struct ftrace_graph_data
*fgd
;
6745 struct ftrace_hash
*old_hash
, *new_hash
;
6746 struct trace_parser
*parser
;
6749 if (file
->f_mode
& FMODE_READ
) {
6750 struct seq_file
*m
= file
->private_data
;
6753 seq_release(inode
, file
);
6755 fgd
= file
->private_data
;
6759 if (file
->f_mode
& FMODE_WRITE
) {
6761 parser
= &fgd
->parser
;
6763 if (trace_parser_loaded((parser
))) {
6764 ret
= ftrace_graph_set_hash(fgd
->new_hash
,
6768 trace_parser_put(parser
);
6770 new_hash
= __ftrace_hash_move(fgd
->new_hash
);
6776 mutex_lock(&graph_lock
);
6778 if (fgd
->type
== GRAPH_FILTER_FUNCTION
) {
6779 old_hash
= rcu_dereference_protected(ftrace_graph_hash
,
6780 lockdep_is_held(&graph_lock
));
6781 rcu_assign_pointer(ftrace_graph_hash
, new_hash
);
6783 old_hash
= rcu_dereference_protected(ftrace_graph_notrace_hash
,
6784 lockdep_is_held(&graph_lock
));
6785 rcu_assign_pointer(ftrace_graph_notrace_hash
, new_hash
);
6788 mutex_unlock(&graph_lock
);
6791 * We need to do a hard force of sched synchronization.
6792 * This is because we use preempt_disable() to do RCU, but
6793 * the function tracers can be called where RCU is not watching
6794 * (like before user_exit()). We can not rely on the RCU
6795 * infrastructure to do the synchronization, thus we must do it
6798 if (old_hash
!= EMPTY_HASH
)
6799 synchronize_rcu_tasks_rude();
6801 free_ftrace_hash(old_hash
);
6805 free_ftrace_hash(fgd
->new_hash
);
6812 ftrace_graph_set_hash(struct ftrace_hash
*hash
, char *buffer
)
6814 struct ftrace_glob func_g
;
6815 struct dyn_ftrace
*rec
;
6816 struct ftrace_page
*pg
;
6817 struct ftrace_func_entry
*entry
;
6822 func_g
.type
= filter_parse_regex(buffer
, strlen(buffer
),
6823 &func_g
.search
, ¬);
6825 func_g
.len
= strlen(func_g
.search
);
6827 guard(mutex
)(&ftrace_lock
);
6829 if (unlikely(ftrace_disabled
))
6832 do_for_each_ftrace_rec(pg
, rec
) {
6834 if (rec
->flags
& FTRACE_FL_DISABLED
)
6837 if (ftrace_match_record(rec
, &func_g
, NULL
, 0)) {
6838 entry
= ftrace_lookup_ip(hash
, rec
->ip
);
6845 if (add_hash_entry(hash
, rec
->ip
) == NULL
)
6849 free_hash_entry(hash
, entry
);
6854 } while_for_each_ftrace_rec();
6856 return fail
? -EINVAL
: 0;
6860 ftrace_graph_write(struct file
*file
, const char __user
*ubuf
,
6861 size_t cnt
, loff_t
*ppos
)
6863 ssize_t read
, ret
= 0;
6864 struct ftrace_graph_data
*fgd
= file
->private_data
;
6865 struct trace_parser
*parser
;
6870 /* Read mode uses seq functions */
6871 if (file
->f_mode
& FMODE_READ
) {
6872 struct seq_file
*m
= file
->private_data
;
6876 parser
= &fgd
->parser
;
6878 read
= trace_get_user(parser
, ubuf
, cnt
, ppos
);
6880 if (read
>= 0 && trace_parser_loaded(parser
) &&
6881 !trace_parser_cont(parser
)) {
6883 ret
= ftrace_graph_set_hash(fgd
->new_hash
,
6885 trace_parser_clear(parser
);
6894 static const struct file_operations ftrace_graph_fops
= {
6895 .open
= ftrace_graph_open
,
6897 .write
= ftrace_graph_write
,
6898 .llseek
= tracing_lseek
,
6899 .release
= ftrace_graph_release
,
6902 static const struct file_operations ftrace_graph_notrace_fops
= {
6903 .open
= ftrace_graph_notrace_open
,
6905 .write
= ftrace_graph_write
,
6906 .llseek
= tracing_lseek
,
6907 .release
= ftrace_graph_release
,
6909 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
6911 void ftrace_create_filter_files(struct ftrace_ops
*ops
,
6912 struct dentry
*parent
)
6915 trace_create_file("set_ftrace_filter", TRACE_MODE_WRITE
, parent
,
6916 ops
, &ftrace_filter_fops
);
6918 trace_create_file("set_ftrace_notrace", TRACE_MODE_WRITE
, parent
,
6919 ops
, &ftrace_notrace_fops
);
6923 * The name "destroy_filter_files" is really a misnomer. Although
6924 * in the future, it may actually delete the files, but this is
6925 * really intended to make sure the ops passed in are disabled
6926 * and that when this function returns, the caller is free to
6929 * The "destroy" name is only to match the "create" name that this
6930 * should be paired with.
6932 void ftrace_destroy_filter_files(struct ftrace_ops
*ops
)
6934 mutex_lock(&ftrace_lock
);
6935 if (ops
->flags
& FTRACE_OPS_FL_ENABLED
)
6936 ftrace_shutdown(ops
, 0);
6937 ops
->flags
|= FTRACE_OPS_FL_DELETED
;
6938 ftrace_free_filter(ops
);
6939 mutex_unlock(&ftrace_lock
);
6942 static __init
int ftrace_init_dyn_tracefs(struct dentry
*d_tracer
)
6945 trace_create_file("available_filter_functions", TRACE_MODE_READ
,
6946 d_tracer
, NULL
, &ftrace_avail_fops
);
6948 trace_create_file("available_filter_functions_addrs", TRACE_MODE_READ
,
6949 d_tracer
, NULL
, &ftrace_avail_addrs_fops
);
6951 trace_create_file("enabled_functions", TRACE_MODE_READ
,
6952 d_tracer
, NULL
, &ftrace_enabled_fops
);
6954 trace_create_file("touched_functions", TRACE_MODE_READ
,
6955 d_tracer
, NULL
, &ftrace_touched_fops
);
6957 ftrace_create_filter_files(&global_ops
, d_tracer
);
6959 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
6960 trace_create_file("set_graph_function", TRACE_MODE_WRITE
, d_tracer
,
6962 &ftrace_graph_fops
);
6963 trace_create_file("set_graph_notrace", TRACE_MODE_WRITE
, d_tracer
,
6965 &ftrace_graph_notrace_fops
);
6966 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
6971 static int ftrace_cmp_ips(const void *a
, const void *b
)
6973 const unsigned long *ipa
= a
;
6974 const unsigned long *ipb
= b
;
6983 #ifdef CONFIG_FTRACE_SORT_STARTUP_TEST
6984 static void test_is_sorted(unsigned long *start
, unsigned long count
)
6988 for (i
= 1; i
< count
; i
++) {
6989 if (WARN(start
[i
- 1] > start
[i
],
6990 "[%d] %pS at %lx is not sorted with %pS at %lx\n", i
,
6991 (void *)start
[i
- 1], start
[i
- 1],
6992 (void *)start
[i
], start
[i
]))
6996 pr_info("ftrace section at %px sorted properly\n", start
);
6999 static void test_is_sorted(unsigned long *start
, unsigned long count
)
7004 static int ftrace_process_locs(struct module
*mod
,
7005 unsigned long *start
,
7008 struct ftrace_page
*pg_unuse
= NULL
;
7009 struct ftrace_page
*start_pg
;
7010 struct ftrace_page
*pg
;
7011 struct dyn_ftrace
*rec
;
7012 unsigned long skipped
= 0;
7013 unsigned long count
;
7016 unsigned long flags
= 0; /* Shut up gcc */
7019 count
= end
- start
;
7025 * Sorting mcount in vmlinux at build time depend on
7026 * CONFIG_BUILDTIME_MCOUNT_SORT, while mcount loc in
7027 * modules can not be sorted at build time.
7029 if (!IS_ENABLED(CONFIG_BUILDTIME_MCOUNT_SORT
) || mod
) {
7030 sort(start
, count
, sizeof(*start
),
7031 ftrace_cmp_ips
, NULL
);
7033 test_is_sorted(start
, count
);
7036 start_pg
= ftrace_allocate_pages(count
);
7040 mutex_lock(&ftrace_lock
);
7043 * Core and each module needs their own pages, as
7044 * modules will free them when they are removed.
7045 * Force a new page to be allocated for modules.
7048 WARN_ON(ftrace_pages
|| ftrace_pages_start
);
7049 /* First initialization */
7050 ftrace_pages
= ftrace_pages_start
= start_pg
;
7055 if (WARN_ON(ftrace_pages
->next
)) {
7056 /* Hmm, we have free pages? */
7057 while (ftrace_pages
->next
)
7058 ftrace_pages
= ftrace_pages
->next
;
7061 ftrace_pages
->next
= start_pg
;
7067 unsigned long end_offset
;
7068 addr
= ftrace_call_adjust(*p
++);
7070 * Some architecture linkers will pad between
7071 * the different mcount_loc sections of different
7072 * object files to satisfy alignments.
7073 * Skip any NULL pointers.
7080 end_offset
= (pg
->index
+1) * sizeof(pg
->records
[0]);
7081 if (end_offset
> PAGE_SIZE
<< pg
->order
) {
7082 /* We should have allocated enough */
7083 if (WARN_ON(!pg
->next
))
7088 rec
= &pg
->records
[pg
->index
++];
7093 pg_unuse
= pg
->next
;
7097 /* Assign the last page to ftrace_pages */
7101 * We only need to disable interrupts on start up
7102 * because we are modifying code that an interrupt
7103 * may execute, and the modification is not atomic.
7104 * But for modules, nothing runs the code we modify
7105 * until we are finished with it, and there's no
7106 * reason to cause large interrupt latencies while we do it.
7109 local_irq_save(flags
);
7110 ftrace_update_code(mod
, start_pg
);
7112 local_irq_restore(flags
);
7115 mutex_unlock(&ftrace_lock
);
7117 /* We should have used all pages unless we skipped some */
7120 /* Need to synchronize with ftrace_location_range() */
7122 ftrace_free_pages(pg_unuse
);
7127 struct ftrace_mod_func
{
7128 struct list_head list
;
7134 struct ftrace_mod_map
{
7135 struct rcu_head rcu
;
7136 struct list_head list
;
7138 unsigned long start_addr
;
7139 unsigned long end_addr
;
7140 struct list_head funcs
;
7141 unsigned int num_funcs
;
7144 static int ftrace_get_trampoline_kallsym(unsigned int symnum
,
7145 unsigned long *value
, char *type
,
7146 char *name
, char *module_name
,
7149 struct ftrace_ops
*op
;
7151 list_for_each_entry_rcu(op
, &ftrace_ops_trampoline_list
, list
) {
7152 if (!op
->trampoline
|| symnum
--)
7154 *value
= op
->trampoline
;
7156 strscpy(name
, FTRACE_TRAMPOLINE_SYM
, KSYM_NAME_LEN
);
7157 strscpy(module_name
, FTRACE_TRAMPOLINE_MOD
, MODULE_NAME_LEN
);
7165 #if defined(CONFIG_DYNAMIC_FTRACE_WITH_DIRECT_CALLS) || defined(CONFIG_MODULES)
7167 * Check if the current ops references the given ip.
7169 * If the ops traces all functions, then it was already accounted for.
7170 * If the ops does not trace the current record function, skip it.
7171 * If the ops ignores the function via notrace filter, skip it.
7174 ops_references_ip(struct ftrace_ops
*ops
, unsigned long ip
)
7176 /* If ops isn't enabled, ignore it */
7177 if (!(ops
->flags
& FTRACE_OPS_FL_ENABLED
))
7180 /* If ops traces all then it includes this function */
7181 if (ops_traces_mod(ops
))
7184 /* The function must be in the filter */
7185 if (!ftrace_hash_empty(ops
->func_hash
->filter_hash
) &&
7186 !__ftrace_lookup_ip(ops
->func_hash
->filter_hash
, ip
))
7189 /* If in notrace hash, we ignore it too */
7190 if (ftrace_lookup_ip(ops
->func_hash
->notrace_hash
, ip
))
7197 #ifdef CONFIG_MODULES
7199 #define next_to_ftrace_page(p) container_of(p, struct ftrace_page, next)
7201 static LIST_HEAD(ftrace_mod_maps
);
7203 static int referenced_filters(struct dyn_ftrace
*rec
)
7205 struct ftrace_ops
*ops
;
7208 for (ops
= ftrace_ops_list
; ops
!= &ftrace_list_end
; ops
= ops
->next
) {
7209 if (ops_references_ip(ops
, rec
->ip
)) {
7210 if (WARN_ON_ONCE(ops
->flags
& FTRACE_OPS_FL_DIRECT
))
7212 if (WARN_ON_ONCE(ops
->flags
& FTRACE_OPS_FL_IPMODIFY
))
7215 if (ops
->flags
& FTRACE_OPS_FL_SAVE_REGS
)
7216 rec
->flags
|= FTRACE_FL_REGS
;
7217 if (cnt
== 1 && ops
->trampoline
)
7218 rec
->flags
|= FTRACE_FL_TRAMP
;
7220 rec
->flags
&= ~FTRACE_FL_TRAMP
;
7228 clear_mod_from_hash(struct ftrace_page
*pg
, struct ftrace_hash
*hash
)
7230 struct ftrace_func_entry
*entry
;
7231 struct dyn_ftrace
*rec
;
7234 if (ftrace_hash_empty(hash
))
7237 for (i
= 0; i
< pg
->index
; i
++) {
7238 rec
= &pg
->records
[i
];
7239 entry
= __ftrace_lookup_ip(hash
, rec
->ip
);
7241 * Do not allow this rec to match again.
7242 * Yeah, it may waste some memory, but will be removed
7243 * if/when the hash is modified again.
7250 /* Clear any records from hashes */
7251 static void clear_mod_from_hashes(struct ftrace_page
*pg
)
7253 struct trace_array
*tr
;
7255 mutex_lock(&trace_types_lock
);
7256 list_for_each_entry(tr
, &ftrace_trace_arrays
, list
) {
7257 if (!tr
->ops
|| !tr
->ops
->func_hash
)
7259 mutex_lock(&tr
->ops
->func_hash
->regex_lock
);
7260 clear_mod_from_hash(pg
, tr
->ops
->func_hash
->filter_hash
);
7261 clear_mod_from_hash(pg
, tr
->ops
->func_hash
->notrace_hash
);
7262 mutex_unlock(&tr
->ops
->func_hash
->regex_lock
);
7264 mutex_unlock(&trace_types_lock
);
7267 static void ftrace_free_mod_map(struct rcu_head
*rcu
)
7269 struct ftrace_mod_map
*mod_map
= container_of(rcu
, struct ftrace_mod_map
, rcu
);
7270 struct ftrace_mod_func
*mod_func
;
7271 struct ftrace_mod_func
*n
;
7273 /* All the contents of mod_map are now not visible to readers */
7274 list_for_each_entry_safe(mod_func
, n
, &mod_map
->funcs
, list
) {
7275 kfree(mod_func
->name
);
7276 list_del(&mod_func
->list
);
7283 void ftrace_release_mod(struct module
*mod
)
7285 struct ftrace_mod_map
*mod_map
;
7286 struct ftrace_mod_map
*n
;
7287 struct dyn_ftrace
*rec
;
7288 struct ftrace_page
**last_pg
;
7289 struct ftrace_page
*tmp_page
= NULL
;
7290 struct ftrace_page
*pg
;
7292 mutex_lock(&ftrace_lock
);
7294 if (ftrace_disabled
)
7297 list_for_each_entry_safe(mod_map
, n
, &ftrace_mod_maps
, list
) {
7298 if (mod_map
->mod
== mod
) {
7299 list_del_rcu(&mod_map
->list
);
7300 call_rcu(&mod_map
->rcu
, ftrace_free_mod_map
);
7306 * Each module has its own ftrace_pages, remove
7307 * them from the list.
7309 last_pg
= &ftrace_pages_start
;
7310 for (pg
= ftrace_pages_start
; pg
; pg
= *last_pg
) {
7311 rec
= &pg
->records
[0];
7312 if (within_module(rec
->ip
, mod
)) {
7314 * As core pages are first, the first
7315 * page should never be a module page.
7317 if (WARN_ON(pg
== ftrace_pages_start
))
7320 /* Check if we are deleting the last page */
7321 if (pg
== ftrace_pages
)
7322 ftrace_pages
= next_to_ftrace_page(last_pg
);
7324 ftrace_update_tot_cnt
-= pg
->index
;
7325 *last_pg
= pg
->next
;
7327 pg
->next
= tmp_page
;
7330 last_pg
= &pg
->next
;
7333 mutex_unlock(&ftrace_lock
);
7335 /* Need to synchronize with ftrace_location_range() */
7338 for (pg
= tmp_page
; pg
; pg
= tmp_page
) {
7340 /* Needs to be called outside of ftrace_lock */
7341 clear_mod_from_hashes(pg
);
7344 free_pages((unsigned long)pg
->records
, pg
->order
);
7345 ftrace_number_of_pages
-= 1 << pg
->order
;
7347 tmp_page
= pg
->next
;
7349 ftrace_number_of_groups
--;
7353 void ftrace_module_enable(struct module
*mod
)
7355 struct dyn_ftrace
*rec
;
7356 struct ftrace_page
*pg
;
7358 mutex_lock(&ftrace_lock
);
7360 if (ftrace_disabled
)
7364 * If the tracing is enabled, go ahead and enable the record.
7366 * The reason not to enable the record immediately is the
7367 * inherent check of ftrace_make_nop/ftrace_make_call for
7368 * correct previous instructions. Making first the NOP
7369 * conversion puts the module to the correct state, thus
7370 * passing the ftrace_make_call check.
7372 * We also delay this to after the module code already set the
7373 * text to read-only, as we now need to set it back to read-write
7374 * so that we can modify the text.
7376 if (ftrace_start_up
)
7377 ftrace_arch_code_modify_prepare();
7379 do_for_each_ftrace_rec(pg
, rec
) {
7382 * do_for_each_ftrace_rec() is a double loop.
7383 * module text shares the pg. If a record is
7384 * not part of this module, then skip this pg,
7385 * which the "break" will do.
7387 if (!within_module(rec
->ip
, mod
))
7390 /* Weak functions should still be ignored */
7391 if (!test_for_valid_rec(rec
)) {
7392 /* Clear all other flags. Should not be enabled anyway */
7393 rec
->flags
= FTRACE_FL_DISABLED
;
7400 * When adding a module, we need to check if tracers are
7401 * currently enabled and if they are, and can trace this record,
7402 * we need to enable the module functions as well as update the
7403 * reference counts for those function records.
7405 if (ftrace_start_up
)
7406 cnt
+= referenced_filters(rec
);
7408 rec
->flags
&= ~FTRACE_FL_DISABLED
;
7411 if (ftrace_start_up
&& cnt
) {
7412 int failed
= __ftrace_replace_code(rec
, 1);
7414 ftrace_bug(failed
, rec
);
7419 } while_for_each_ftrace_rec();
7422 if (ftrace_start_up
)
7423 ftrace_arch_code_modify_post_process();
7426 mutex_unlock(&ftrace_lock
);
7428 process_cached_mods(mod
->name
);
7431 void ftrace_module_init(struct module
*mod
)
7435 if (ftrace_disabled
|| !mod
->num_ftrace_callsites
)
7438 ret
= ftrace_process_locs(mod
, mod
->ftrace_callsites
,
7439 mod
->ftrace_callsites
+ mod
->num_ftrace_callsites
);
7441 pr_warn("ftrace: failed to allocate entries for module '%s' functions\n",
7445 static void save_ftrace_mod_rec(struct ftrace_mod_map
*mod_map
,
7446 struct dyn_ftrace
*rec
)
7448 struct ftrace_mod_func
*mod_func
;
7449 unsigned long symsize
;
7450 unsigned long offset
;
7451 char str
[KSYM_SYMBOL_LEN
];
7455 ret
= kallsyms_lookup(rec
->ip
, &symsize
, &offset
, &modname
, str
);
7459 mod_func
= kmalloc(sizeof(*mod_func
), GFP_KERNEL
);
7463 mod_func
->name
= kstrdup(str
, GFP_KERNEL
);
7464 if (!mod_func
->name
) {
7469 mod_func
->ip
= rec
->ip
- offset
;
7470 mod_func
->size
= symsize
;
7472 mod_map
->num_funcs
++;
7474 list_add_rcu(&mod_func
->list
, &mod_map
->funcs
);
7477 static struct ftrace_mod_map
*
7478 allocate_ftrace_mod_map(struct module
*mod
,
7479 unsigned long start
, unsigned long end
)
7481 struct ftrace_mod_map
*mod_map
;
7483 mod_map
= kmalloc(sizeof(*mod_map
), GFP_KERNEL
);
7488 mod_map
->start_addr
= start
;
7489 mod_map
->end_addr
= end
;
7490 mod_map
->num_funcs
= 0;
7492 INIT_LIST_HEAD_RCU(&mod_map
->funcs
);
7494 list_add_rcu(&mod_map
->list
, &ftrace_mod_maps
);
7500 ftrace_func_address_lookup(struct ftrace_mod_map
*mod_map
,
7501 unsigned long addr
, unsigned long *size
,
7502 unsigned long *off
, char *sym
)
7504 struct ftrace_mod_func
*found_func
= NULL
;
7505 struct ftrace_mod_func
*mod_func
;
7507 list_for_each_entry_rcu(mod_func
, &mod_map
->funcs
, list
) {
7508 if (addr
>= mod_func
->ip
&&
7509 addr
< mod_func
->ip
+ mod_func
->size
) {
7510 found_func
= mod_func
;
7517 *size
= found_func
->size
;
7519 *off
= addr
- found_func
->ip
;
7520 return strscpy(sym
, found_func
->name
, KSYM_NAME_LEN
);
7527 ftrace_mod_address_lookup(unsigned long addr
, unsigned long *size
,
7528 unsigned long *off
, char **modname
, char *sym
)
7530 struct ftrace_mod_map
*mod_map
;
7533 /* mod_map is freed via call_rcu() */
7535 list_for_each_entry_rcu(mod_map
, &ftrace_mod_maps
, list
) {
7536 ret
= ftrace_func_address_lookup(mod_map
, addr
, size
, off
, sym
);
7539 *modname
= mod_map
->mod
->name
;
7548 int ftrace_mod_get_kallsym(unsigned int symnum
, unsigned long *value
,
7549 char *type
, char *name
,
7550 char *module_name
, int *exported
)
7552 struct ftrace_mod_map
*mod_map
;
7553 struct ftrace_mod_func
*mod_func
;
7557 list_for_each_entry_rcu(mod_map
, &ftrace_mod_maps
, list
) {
7559 if (symnum
>= mod_map
->num_funcs
) {
7560 symnum
-= mod_map
->num_funcs
;
7564 list_for_each_entry_rcu(mod_func
, &mod_map
->funcs
, list
) {
7570 *value
= mod_func
->ip
;
7572 strscpy(name
, mod_func
->name
, KSYM_NAME_LEN
);
7573 strscpy(module_name
, mod_map
->mod
->name
, MODULE_NAME_LEN
);
7581 ret
= ftrace_get_trampoline_kallsym(symnum
, value
, type
, name
,
7582 module_name
, exported
);
7588 static void save_ftrace_mod_rec(struct ftrace_mod_map
*mod_map
,
7589 struct dyn_ftrace
*rec
) { }
7590 static inline struct ftrace_mod_map
*
7591 allocate_ftrace_mod_map(struct module
*mod
,
7592 unsigned long start
, unsigned long end
)
7596 int ftrace_mod_get_kallsym(unsigned int symnum
, unsigned long *value
,
7597 char *type
, char *name
, char *module_name
,
7603 ret
= ftrace_get_trampoline_kallsym(symnum
, value
, type
, name
,
7604 module_name
, exported
);
7608 #endif /* CONFIG_MODULES */
7610 struct ftrace_init_func
{
7611 struct list_head list
;
7615 /* Clear any init ips from hashes */
7617 clear_func_from_hash(struct ftrace_init_func
*func
, struct ftrace_hash
*hash
)
7619 struct ftrace_func_entry
*entry
;
7621 entry
= ftrace_lookup_ip(hash
, func
->ip
);
7623 * Do not allow this rec to match again.
7624 * Yeah, it may waste some memory, but will be removed
7625 * if/when the hash is modified again.
7632 clear_func_from_hashes(struct ftrace_init_func
*func
)
7634 struct trace_array
*tr
;
7636 mutex_lock(&trace_types_lock
);
7637 list_for_each_entry(tr
, &ftrace_trace_arrays
, list
) {
7638 if (!tr
->ops
|| !tr
->ops
->func_hash
)
7640 mutex_lock(&tr
->ops
->func_hash
->regex_lock
);
7641 clear_func_from_hash(func
, tr
->ops
->func_hash
->filter_hash
);
7642 clear_func_from_hash(func
, tr
->ops
->func_hash
->notrace_hash
);
7643 mutex_unlock(&tr
->ops
->func_hash
->regex_lock
);
7645 mutex_unlock(&trace_types_lock
);
7648 static void add_to_clear_hash_list(struct list_head
*clear_list
,
7649 struct dyn_ftrace
*rec
)
7651 struct ftrace_init_func
*func
;
7653 func
= kmalloc(sizeof(*func
), GFP_KERNEL
);
7655 MEM_FAIL(1, "alloc failure, ftrace filter could be stale\n");
7660 list_add(&func
->list
, clear_list
);
7663 void ftrace_free_mem(struct module
*mod
, void *start_ptr
, void *end_ptr
)
7665 unsigned long start
= (unsigned long)(start_ptr
);
7666 unsigned long end
= (unsigned long)(end_ptr
);
7667 struct ftrace_page
**last_pg
= &ftrace_pages_start
;
7668 struct ftrace_page
*tmp_page
= NULL
;
7669 struct ftrace_page
*pg
;
7670 struct dyn_ftrace
*rec
;
7671 struct dyn_ftrace key
;
7672 struct ftrace_mod_map
*mod_map
= NULL
;
7673 struct ftrace_init_func
*func
, *func_next
;
7674 LIST_HEAD(clear_hash
);
7677 key
.flags
= end
; /* overload flags, as it is unsigned long */
7679 mutex_lock(&ftrace_lock
);
7682 * If we are freeing module init memory, then check if
7683 * any tracer is active. If so, we need to save a mapping of
7684 * the module functions being freed with the address.
7686 if (mod
&& ftrace_ops_list
!= &ftrace_list_end
)
7687 mod_map
= allocate_ftrace_mod_map(mod
, start
, end
);
7689 for (pg
= ftrace_pages_start
; pg
; last_pg
= &pg
->next
, pg
= *last_pg
) {
7690 if (end
< pg
->records
[0].ip
||
7691 start
>= (pg
->records
[pg
->index
- 1].ip
+ MCOUNT_INSN_SIZE
))
7694 rec
= bsearch(&key
, pg
->records
, pg
->index
,
7695 sizeof(struct dyn_ftrace
),
7700 /* rec will be cleared from hashes after ftrace_lock unlock */
7701 add_to_clear_hash_list(&clear_hash
, rec
);
7704 save_ftrace_mod_rec(mod_map
, rec
);
7707 ftrace_update_tot_cnt
--;
7709 *last_pg
= pg
->next
;
7710 pg
->next
= tmp_page
;
7712 pg
= container_of(last_pg
, struct ftrace_page
, next
);
7717 memmove(rec
, rec
+ 1,
7718 (pg
->index
- (rec
- pg
->records
)) * sizeof(*rec
));
7719 /* More than one function may be in this block */
7722 mutex_unlock(&ftrace_lock
);
7724 list_for_each_entry_safe(func
, func_next
, &clear_hash
, list
) {
7725 clear_func_from_hashes(func
);
7728 /* Need to synchronize with ftrace_location_range() */
7731 ftrace_free_pages(tmp_page
);
7735 void __init
ftrace_free_init_mem(void)
7737 void *start
= (void *)(&__init_begin
);
7738 void *end
= (void *)(&__init_end
);
7740 ftrace_boot_snapshot();
7742 ftrace_free_mem(NULL
, start
, end
);
7745 int __init __weak
ftrace_dyn_arch_init(void)
7750 void __init
ftrace_init(void)
7752 extern unsigned long __start_mcount_loc
[];
7753 extern unsigned long __stop_mcount_loc
[];
7754 unsigned long count
, flags
;
7757 local_irq_save(flags
);
7758 ret
= ftrace_dyn_arch_init();
7759 local_irq_restore(flags
);
7763 count
= __stop_mcount_loc
- __start_mcount_loc
;
7765 pr_info("ftrace: No functions to be traced?\n");
7769 pr_info("ftrace: allocating %ld entries in %ld pages\n",
7770 count
, DIV_ROUND_UP(count
, ENTRIES_PER_PAGE
));
7772 ret
= ftrace_process_locs(NULL
,
7776 pr_warn("ftrace: failed to allocate entries for functions\n");
7780 pr_info("ftrace: allocated %ld pages with %ld groups\n",
7781 ftrace_number_of_pages
, ftrace_number_of_groups
);
7783 last_ftrace_enabled
= ftrace_enabled
= 1;
7785 set_ftrace_early_filters();
7789 ftrace_disabled
= 1;
7792 /* Do nothing if arch does not support this */
7793 void __weak
arch_ftrace_update_trampoline(struct ftrace_ops
*ops
)
7797 static void ftrace_update_trampoline(struct ftrace_ops
*ops
)
7799 unsigned long trampoline
= ops
->trampoline
;
7801 arch_ftrace_update_trampoline(ops
);
7802 if (ops
->trampoline
&& ops
->trampoline
!= trampoline
&&
7803 (ops
->flags
& FTRACE_OPS_FL_ALLOC_TRAMP
)) {
7804 /* Add to kallsyms before the perf events */
7805 ftrace_add_trampoline_to_kallsyms(ops
);
7806 perf_event_ksymbol(PERF_RECORD_KSYMBOL_TYPE_OOL
,
7807 ops
->trampoline
, ops
->trampoline_size
, false,
7808 FTRACE_TRAMPOLINE_SYM
);
7810 * Record the perf text poke event after the ksymbol register
7813 perf_event_text_poke((void *)ops
->trampoline
, NULL
, 0,
7814 (void *)ops
->trampoline
,
7815 ops
->trampoline_size
);
7819 void ftrace_init_trace_array(struct trace_array
*tr
)
7821 INIT_LIST_HEAD(&tr
->func_probes
);
7822 INIT_LIST_HEAD(&tr
->mod_trace
);
7823 INIT_LIST_HEAD(&tr
->mod_notrace
);
7827 struct ftrace_ops global_ops
= {
7828 .func
= ftrace_stub
,
7829 .flags
= FTRACE_OPS_FL_INITIALIZED
|
7833 static int __init
ftrace_nodyn_init(void)
7838 core_initcall(ftrace_nodyn_init
);
7840 static inline int ftrace_init_dyn_tracefs(struct dentry
*d_tracer
) { return 0; }
7841 static inline void ftrace_startup_all(int command
) { }
7843 static void ftrace_update_trampoline(struct ftrace_ops
*ops
)
7847 #endif /* CONFIG_DYNAMIC_FTRACE */
7849 __init
void ftrace_init_global_array_ops(struct trace_array
*tr
)
7851 tr
->ops
= &global_ops
;
7852 tr
->ops
->private = tr
;
7853 ftrace_init_trace_array(tr
);
7854 init_array_fgraph_ops(tr
, tr
->ops
);
7857 void ftrace_init_array_ops(struct trace_array
*tr
, ftrace_func_t func
)
7859 /* If we filter on pids, update to use the pid function */
7860 if (tr
->flags
& TRACE_ARRAY_FL_GLOBAL
) {
7861 if (WARN_ON(tr
->ops
->func
!= ftrace_stub
))
7862 printk("ftrace ops had %pS for function\n",
7865 tr
->ops
->func
= func
;
7866 tr
->ops
->private = tr
;
7869 void ftrace_reset_array_ops(struct trace_array
*tr
)
7871 tr
->ops
->func
= ftrace_stub
;
7874 static nokprobe_inline
void
7875 __ftrace_ops_list_func(unsigned long ip
, unsigned long parent_ip
,
7876 struct ftrace_ops
*ignored
, struct ftrace_regs
*fregs
)
7878 struct pt_regs
*regs
= ftrace_get_regs(fregs
);
7879 struct ftrace_ops
*op
;
7883 * The ftrace_test_and_set_recursion() will disable preemption,
7884 * which is required since some of the ops may be dynamically
7885 * allocated, they must be freed after a synchronize_rcu().
7887 bit
= trace_test_and_set_recursion(ip
, parent_ip
, TRACE_LIST_START
);
7891 do_for_each_ftrace_op(op
, ftrace_ops_list
) {
7892 /* Stub functions don't need to be called nor tested */
7893 if (op
->flags
& FTRACE_OPS_FL_STUB
)
7896 * Check the following for each ops before calling their func:
7897 * if RCU flag is set, then rcu_is_watching() must be true
7898 * Otherwise test if the ip matches the ops filter
7900 * If any of the above fails then the op->func() is not executed.
7902 if ((!(op
->flags
& FTRACE_OPS_FL_RCU
) || rcu_is_watching()) &&
7903 ftrace_ops_test(op
, ip
, regs
)) {
7904 if (FTRACE_WARN_ON(!op
->func
)) {
7905 pr_warn("op=%p %pS\n", op
, op
);
7908 op
->func(ip
, parent_ip
, op
, fregs
);
7910 } while_for_each_ftrace_op(op
);
7912 trace_clear_recursion(bit
);
7916 * Some archs only support passing ip and parent_ip. Even though
7917 * the list function ignores the op parameter, we do not want any
7918 * C side effects, where a function is called without the caller
7919 * sending a third parameter.
7920 * Archs are to support both the regs and ftrace_ops at the same time.
7921 * If they support ftrace_ops, it is assumed they support regs.
7922 * If call backs want to use regs, they must either check for regs
7923 * being NULL, or CONFIG_DYNAMIC_FTRACE_WITH_REGS.
7924 * Note, CONFIG_DYNAMIC_FTRACE_WITH_REGS expects a full regs to be saved.
7925 * An architecture can pass partial regs with ftrace_ops and still
7926 * set the ARCH_SUPPORTS_FTRACE_OPS.
7928 * In vmlinux.lds.h, ftrace_ops_list_func() is defined to be
7929 * arch_ftrace_ops_list_func.
7931 #if ARCH_SUPPORTS_FTRACE_OPS
7932 void arch_ftrace_ops_list_func(unsigned long ip
, unsigned long parent_ip
,
7933 struct ftrace_ops
*op
, struct ftrace_regs
*fregs
)
7935 kmsan_unpoison_memory(fregs
, ftrace_regs_size());
7936 __ftrace_ops_list_func(ip
, parent_ip
, NULL
, fregs
);
7939 void arch_ftrace_ops_list_func(unsigned long ip
, unsigned long parent_ip
)
7941 __ftrace_ops_list_func(ip
, parent_ip
, NULL
, NULL
);
7944 NOKPROBE_SYMBOL(arch_ftrace_ops_list_func
);
7947 * If there's only one function registered but it does not support
7948 * recursion, needs RCU protection, then this function will be called
7949 * by the mcount trampoline.
7951 static void ftrace_ops_assist_func(unsigned long ip
, unsigned long parent_ip
,
7952 struct ftrace_ops
*op
, struct ftrace_regs
*fregs
)
7956 bit
= trace_test_and_set_recursion(ip
, parent_ip
, TRACE_LIST_START
);
7960 if (!(op
->flags
& FTRACE_OPS_FL_RCU
) || rcu_is_watching())
7961 op
->func(ip
, parent_ip
, op
, fregs
);
7963 trace_clear_recursion(bit
);
7965 NOKPROBE_SYMBOL(ftrace_ops_assist_func
);
7968 * ftrace_ops_get_func - get the function a trampoline should call
7969 * @ops: the ops to get the function for
7971 * Normally the mcount trampoline will call the ops->func, but there
7972 * are times that it should not. For example, if the ops does not
7973 * have its own recursion protection, then it should call the
7974 * ftrace_ops_assist_func() instead.
7976 * Returns: the function that the trampoline should call for @ops.
7978 ftrace_func_t
ftrace_ops_get_func(struct ftrace_ops
*ops
)
7981 * If the function does not handle recursion or needs to be RCU safe,
7982 * then we need to call the assist handler.
7984 if (ops
->flags
& (FTRACE_OPS_FL_RECURSION
|
7986 return ftrace_ops_assist_func
;
7992 ftrace_filter_pid_sched_switch_probe(void *data
, bool preempt
,
7993 struct task_struct
*prev
,
7994 struct task_struct
*next
,
7995 unsigned int prev_state
)
7997 struct trace_array
*tr
= data
;
7998 struct trace_pid_list
*pid_list
;
7999 struct trace_pid_list
*no_pid_list
;
8001 pid_list
= rcu_dereference_sched(tr
->function_pids
);
8002 no_pid_list
= rcu_dereference_sched(tr
->function_no_pids
);
8004 if (trace_ignore_this_task(pid_list
, no_pid_list
, next
))
8005 this_cpu_write(tr
->array_buffer
.data
->ftrace_ignore_pid
,
8008 this_cpu_write(tr
->array_buffer
.data
->ftrace_ignore_pid
,
8013 ftrace_pid_follow_sched_process_fork(void *data
,
8014 struct task_struct
*self
,
8015 struct task_struct
*task
)
8017 struct trace_pid_list
*pid_list
;
8018 struct trace_array
*tr
= data
;
8020 pid_list
= rcu_dereference_sched(tr
->function_pids
);
8021 trace_filter_add_remove_task(pid_list
, self
, task
);
8023 pid_list
= rcu_dereference_sched(tr
->function_no_pids
);
8024 trace_filter_add_remove_task(pid_list
, self
, task
);
8028 ftrace_pid_follow_sched_process_exit(void *data
, struct task_struct
*task
)
8030 struct trace_pid_list
*pid_list
;
8031 struct trace_array
*tr
= data
;
8033 pid_list
= rcu_dereference_sched(tr
->function_pids
);
8034 trace_filter_add_remove_task(pid_list
, NULL
, task
);
8036 pid_list
= rcu_dereference_sched(tr
->function_no_pids
);
8037 trace_filter_add_remove_task(pid_list
, NULL
, task
);
8040 void ftrace_pid_follow_fork(struct trace_array
*tr
, bool enable
)
8043 register_trace_sched_process_fork(ftrace_pid_follow_sched_process_fork
,
8045 register_trace_sched_process_free(ftrace_pid_follow_sched_process_exit
,
8048 unregister_trace_sched_process_fork(ftrace_pid_follow_sched_process_fork
,
8050 unregister_trace_sched_process_free(ftrace_pid_follow_sched_process_exit
,
8055 static void clear_ftrace_pids(struct trace_array
*tr
, int type
)
8057 struct trace_pid_list
*pid_list
;
8058 struct trace_pid_list
*no_pid_list
;
8061 pid_list
= rcu_dereference_protected(tr
->function_pids
,
8062 lockdep_is_held(&ftrace_lock
));
8063 no_pid_list
= rcu_dereference_protected(tr
->function_no_pids
,
8064 lockdep_is_held(&ftrace_lock
));
8066 /* Make sure there's something to do */
8067 if (!pid_type_enabled(type
, pid_list
, no_pid_list
))
8070 /* See if the pids still need to be checked after this */
8071 if (!still_need_pid_events(type
, pid_list
, no_pid_list
)) {
8072 unregister_trace_sched_switch(ftrace_filter_pid_sched_switch_probe
, tr
);
8073 for_each_possible_cpu(cpu
)
8074 per_cpu_ptr(tr
->array_buffer
.data
, cpu
)->ftrace_ignore_pid
= FTRACE_PID_TRACE
;
8077 if (type
& TRACE_PIDS
)
8078 rcu_assign_pointer(tr
->function_pids
, NULL
);
8080 if (type
& TRACE_NO_PIDS
)
8081 rcu_assign_pointer(tr
->function_no_pids
, NULL
);
8083 /* Wait till all users are no longer using pid filtering */
8086 if ((type
& TRACE_PIDS
) && pid_list
)
8087 trace_pid_list_free(pid_list
);
8089 if ((type
& TRACE_NO_PIDS
) && no_pid_list
)
8090 trace_pid_list_free(no_pid_list
);
8093 void ftrace_clear_pids(struct trace_array
*tr
)
8095 mutex_lock(&ftrace_lock
);
8097 clear_ftrace_pids(tr
, TRACE_PIDS
| TRACE_NO_PIDS
);
8099 mutex_unlock(&ftrace_lock
);
8102 static void ftrace_pid_reset(struct trace_array
*tr
, int type
)
8104 mutex_lock(&ftrace_lock
);
8105 clear_ftrace_pids(tr
, type
);
8107 ftrace_update_pid_func();
8108 ftrace_startup_all(0);
8110 mutex_unlock(&ftrace_lock
);
8113 /* Greater than any max PID */
8114 #define FTRACE_NO_PIDS (void *)(PID_MAX_LIMIT + 1)
8116 static void *fpid_start(struct seq_file
*m
, loff_t
*pos
)
8119 struct trace_pid_list
*pid_list
;
8120 struct trace_array
*tr
= m
->private;
8122 mutex_lock(&ftrace_lock
);
8123 rcu_read_lock_sched();
8125 pid_list
= rcu_dereference_sched(tr
->function_pids
);
8128 return !(*pos
) ? FTRACE_NO_PIDS
: NULL
;
8130 return trace_pid_start(pid_list
, pos
);
8133 static void *fpid_next(struct seq_file
*m
, void *v
, loff_t
*pos
)
8135 struct trace_array
*tr
= m
->private;
8136 struct trace_pid_list
*pid_list
= rcu_dereference_sched(tr
->function_pids
);
8138 if (v
== FTRACE_NO_PIDS
) {
8142 return trace_pid_next(pid_list
, v
, pos
);
8145 static void fpid_stop(struct seq_file
*m
, void *p
)
8148 rcu_read_unlock_sched();
8149 mutex_unlock(&ftrace_lock
);
8152 static int fpid_show(struct seq_file
*m
, void *v
)
8154 if (v
== FTRACE_NO_PIDS
) {
8155 seq_puts(m
, "no pid\n");
8159 return trace_pid_show(m
, v
);
8162 static const struct seq_operations ftrace_pid_sops
= {
8163 .start
= fpid_start
,
8169 static void *fnpid_start(struct seq_file
*m
, loff_t
*pos
)
8172 struct trace_pid_list
*pid_list
;
8173 struct trace_array
*tr
= m
->private;
8175 mutex_lock(&ftrace_lock
);
8176 rcu_read_lock_sched();
8178 pid_list
= rcu_dereference_sched(tr
->function_no_pids
);
8181 return !(*pos
) ? FTRACE_NO_PIDS
: NULL
;
8183 return trace_pid_start(pid_list
, pos
);
8186 static void *fnpid_next(struct seq_file
*m
, void *v
, loff_t
*pos
)
8188 struct trace_array
*tr
= m
->private;
8189 struct trace_pid_list
*pid_list
= rcu_dereference_sched(tr
->function_no_pids
);
8191 if (v
== FTRACE_NO_PIDS
) {
8195 return trace_pid_next(pid_list
, v
, pos
);
8198 static const struct seq_operations ftrace_no_pid_sops
= {
8199 .start
= fnpid_start
,
8205 static int pid_open(struct inode
*inode
, struct file
*file
, int type
)
8207 const struct seq_operations
*seq_ops
;
8208 struct trace_array
*tr
= inode
->i_private
;
8212 ret
= tracing_check_open_get_tr(tr
);
8216 if ((file
->f_mode
& FMODE_WRITE
) &&
8217 (file
->f_flags
& O_TRUNC
))
8218 ftrace_pid_reset(tr
, type
);
8222 seq_ops
= &ftrace_pid_sops
;
8225 seq_ops
= &ftrace_no_pid_sops
;
8228 trace_array_put(tr
);
8233 ret
= seq_open(file
, seq_ops
);
8235 trace_array_put(tr
);
8237 m
= file
->private_data
;
8238 /* copy tr over to seq ops */
8246 ftrace_pid_open(struct inode
*inode
, struct file
*file
)
8248 return pid_open(inode
, file
, TRACE_PIDS
);
8252 ftrace_no_pid_open(struct inode
*inode
, struct file
*file
)
8254 return pid_open(inode
, file
, TRACE_NO_PIDS
);
8257 static void ignore_task_cpu(void *data
)
8259 struct trace_array
*tr
= data
;
8260 struct trace_pid_list
*pid_list
;
8261 struct trace_pid_list
*no_pid_list
;
8264 * This function is called by on_each_cpu() while the
8265 * event_mutex is held.
8267 pid_list
= rcu_dereference_protected(tr
->function_pids
,
8268 mutex_is_locked(&ftrace_lock
));
8269 no_pid_list
= rcu_dereference_protected(tr
->function_no_pids
,
8270 mutex_is_locked(&ftrace_lock
));
8272 if (trace_ignore_this_task(pid_list
, no_pid_list
, current
))
8273 this_cpu_write(tr
->array_buffer
.data
->ftrace_ignore_pid
,
8276 this_cpu_write(tr
->array_buffer
.data
->ftrace_ignore_pid
,
8281 pid_write(struct file
*filp
, const char __user
*ubuf
,
8282 size_t cnt
, loff_t
*ppos
, int type
)
8284 struct seq_file
*m
= filp
->private_data
;
8285 struct trace_array
*tr
= m
->private;
8286 struct trace_pid_list
*filtered_pids
;
8287 struct trace_pid_list
*other_pids
;
8288 struct trace_pid_list
*pid_list
;
8294 mutex_lock(&ftrace_lock
);
8298 filtered_pids
= rcu_dereference_protected(tr
->function_pids
,
8299 lockdep_is_held(&ftrace_lock
));
8300 other_pids
= rcu_dereference_protected(tr
->function_no_pids
,
8301 lockdep_is_held(&ftrace_lock
));
8304 filtered_pids
= rcu_dereference_protected(tr
->function_no_pids
,
8305 lockdep_is_held(&ftrace_lock
));
8306 other_pids
= rcu_dereference_protected(tr
->function_pids
,
8307 lockdep_is_held(&ftrace_lock
));
8315 ret
= trace_pid_write(filtered_pids
, &pid_list
, ubuf
, cnt
);
8321 rcu_assign_pointer(tr
->function_pids
, pid_list
);
8324 rcu_assign_pointer(tr
->function_no_pids
, pid_list
);
8329 if (filtered_pids
) {
8331 trace_pid_list_free(filtered_pids
);
8332 } else if (pid_list
&& !other_pids
) {
8333 /* Register a probe to set whether to ignore the tracing of a task */
8334 register_trace_sched_switch(ftrace_filter_pid_sched_switch_probe
, tr
);
8338 * Ignoring of pids is done at task switch. But we have to
8339 * check for those tasks that are currently running.
8340 * Always do this in case a pid was appended or removed.
8342 on_each_cpu(ignore_task_cpu
, tr
, 1);
8344 ftrace_update_pid_func();
8345 ftrace_startup_all(0);
8347 mutex_unlock(&ftrace_lock
);
8356 ftrace_pid_write(struct file
*filp
, const char __user
*ubuf
,
8357 size_t cnt
, loff_t
*ppos
)
8359 return pid_write(filp
, ubuf
, cnt
, ppos
, TRACE_PIDS
);
8363 ftrace_no_pid_write(struct file
*filp
, const char __user
*ubuf
,
8364 size_t cnt
, loff_t
*ppos
)
8366 return pid_write(filp
, ubuf
, cnt
, ppos
, TRACE_NO_PIDS
);
8370 ftrace_pid_release(struct inode
*inode
, struct file
*file
)
8372 struct trace_array
*tr
= inode
->i_private
;
8374 trace_array_put(tr
);
8376 return seq_release(inode
, file
);
8379 static const struct file_operations ftrace_pid_fops
= {
8380 .open
= ftrace_pid_open
,
8381 .write
= ftrace_pid_write
,
8383 .llseek
= tracing_lseek
,
8384 .release
= ftrace_pid_release
,
8387 static const struct file_operations ftrace_no_pid_fops
= {
8388 .open
= ftrace_no_pid_open
,
8389 .write
= ftrace_no_pid_write
,
8391 .llseek
= tracing_lseek
,
8392 .release
= ftrace_pid_release
,
8395 void ftrace_init_tracefs(struct trace_array
*tr
, struct dentry
*d_tracer
)
8397 trace_create_file("set_ftrace_pid", TRACE_MODE_WRITE
, d_tracer
,
8398 tr
, &ftrace_pid_fops
);
8399 trace_create_file("set_ftrace_notrace_pid", TRACE_MODE_WRITE
,
8400 d_tracer
, tr
, &ftrace_no_pid_fops
);
8403 void __init
ftrace_init_tracefs_toplevel(struct trace_array
*tr
,
8404 struct dentry
*d_tracer
)
8406 /* Only the top level directory has the dyn_tracefs and profile */
8407 WARN_ON(!(tr
->flags
& TRACE_ARRAY_FL_GLOBAL
));
8409 ftrace_init_dyn_tracefs(d_tracer
);
8410 ftrace_profile_tracefs(d_tracer
);
8414 * ftrace_kill - kill ftrace
8416 * This function should be used by panic code. It stops ftrace
8417 * but in a not so nice way. If you need to simply kill ftrace
8418 * from a non-atomic section, use ftrace_kill.
8420 void ftrace_kill(void)
8422 ftrace_disabled
= 1;
8424 ftrace_trace_function
= ftrace_stub
;
8425 kprobe_ftrace_kill();
8429 * ftrace_is_dead - Test if ftrace is dead or not.
8431 * Returns: 1 if ftrace is "dead", zero otherwise.
8433 int ftrace_is_dead(void)
8435 return ftrace_disabled
;
8438 #ifdef CONFIG_DYNAMIC_FTRACE_WITH_DIRECT_CALLS
8440 * When registering ftrace_ops with IPMODIFY, it is necessary to make sure
8441 * it doesn't conflict with any direct ftrace_ops. If there is existing
8442 * direct ftrace_ops on a kernel function being patched, call
8443 * FTRACE_OPS_CMD_ENABLE_SHARE_IPMODIFY_PEER on it to enable sharing.
8445 * @ops: ftrace_ops being registered.
8449 * Negative on failure.
8451 static int prepare_direct_functions_for_ipmodify(struct ftrace_ops
*ops
)
8453 struct ftrace_func_entry
*entry
;
8454 struct ftrace_hash
*hash
;
8455 struct ftrace_ops
*op
;
8458 lockdep_assert_held_once(&direct_mutex
);
8460 if (!(ops
->flags
& FTRACE_OPS_FL_IPMODIFY
))
8463 hash
= ops
->func_hash
->filter_hash
;
8464 size
= 1 << hash
->size_bits
;
8465 for (i
= 0; i
< size
; i
++) {
8466 hlist_for_each_entry(entry
, &hash
->buckets
[i
], hlist
) {
8467 unsigned long ip
= entry
->ip
;
8468 bool found_op
= false;
8470 mutex_lock(&ftrace_lock
);
8471 do_for_each_ftrace_op(op
, ftrace_ops_list
) {
8472 if (!(op
->flags
& FTRACE_OPS_FL_DIRECT
))
8474 if (ops_references_ip(op
, ip
)) {
8478 } while_for_each_ftrace_op(op
);
8479 mutex_unlock(&ftrace_lock
);
8485 ret
= op
->ops_func(op
, FTRACE_OPS_CMD_ENABLE_SHARE_IPMODIFY_PEER
);
8496 * Similar to prepare_direct_functions_for_ipmodify, clean up after ops
8497 * with IPMODIFY is unregistered. The cleanup is optional for most DIRECT
8500 static void cleanup_direct_functions_after_ipmodify(struct ftrace_ops
*ops
)
8502 struct ftrace_func_entry
*entry
;
8503 struct ftrace_hash
*hash
;
8504 struct ftrace_ops
*op
;
8507 if (!(ops
->flags
& FTRACE_OPS_FL_IPMODIFY
))
8510 mutex_lock(&direct_mutex
);
8512 hash
= ops
->func_hash
->filter_hash
;
8513 size
= 1 << hash
->size_bits
;
8514 for (i
= 0; i
< size
; i
++) {
8515 hlist_for_each_entry(entry
, &hash
->buckets
[i
], hlist
) {
8516 unsigned long ip
= entry
->ip
;
8517 bool found_op
= false;
8519 mutex_lock(&ftrace_lock
);
8520 do_for_each_ftrace_op(op
, ftrace_ops_list
) {
8521 if (!(op
->flags
& FTRACE_OPS_FL_DIRECT
))
8523 if (ops_references_ip(op
, ip
)) {
8527 } while_for_each_ftrace_op(op
);
8528 mutex_unlock(&ftrace_lock
);
8530 /* The cleanup is optional, ignore any errors */
8531 if (found_op
&& op
->ops_func
)
8532 op
->ops_func(op
, FTRACE_OPS_CMD_DISABLE_SHARE_IPMODIFY_PEER
);
8535 mutex_unlock(&direct_mutex
);
8538 #define lock_direct_mutex() mutex_lock(&direct_mutex)
8539 #define unlock_direct_mutex() mutex_unlock(&direct_mutex)
8541 #else /* CONFIG_DYNAMIC_FTRACE_WITH_DIRECT_CALLS */
8543 static int prepare_direct_functions_for_ipmodify(struct ftrace_ops
*ops
)
8548 static void cleanup_direct_functions_after_ipmodify(struct ftrace_ops
*ops
)
8552 #define lock_direct_mutex() do { } while (0)
8553 #define unlock_direct_mutex() do { } while (0)
8555 #endif /* CONFIG_DYNAMIC_FTRACE_WITH_DIRECT_CALLS */
8558 * Similar to register_ftrace_function, except we don't lock direct_mutex.
8560 static int register_ftrace_function_nolock(struct ftrace_ops
*ops
)
8564 ftrace_ops_init(ops
);
8566 mutex_lock(&ftrace_lock
);
8568 ret
= ftrace_startup(ops
, 0);
8570 mutex_unlock(&ftrace_lock
);
8576 * register_ftrace_function - register a function for profiling
8577 * @ops: ops structure that holds the function for profiling.
8579 * Register a function to be called by all functions in the
8582 * Note: @ops->func and all the functions it calls must be labeled
8583 * with "notrace", otherwise it will go into a
8586 int register_ftrace_function(struct ftrace_ops
*ops
)
8590 lock_direct_mutex();
8591 ret
= prepare_direct_functions_for_ipmodify(ops
);
8595 ret
= register_ftrace_function_nolock(ops
);
8598 unlock_direct_mutex();
8601 EXPORT_SYMBOL_GPL(register_ftrace_function
);
8604 * unregister_ftrace_function - unregister a function for profiling.
8605 * @ops: ops structure that holds the function to unregister
8607 * Unregister a function that was added to be called by ftrace profiling.
8609 int unregister_ftrace_function(struct ftrace_ops
*ops
)
8613 mutex_lock(&ftrace_lock
);
8614 ret
= ftrace_shutdown(ops
, 0);
8615 mutex_unlock(&ftrace_lock
);
8617 cleanup_direct_functions_after_ipmodify(ops
);
8620 EXPORT_SYMBOL_GPL(unregister_ftrace_function
);
8622 static int symbols_cmp(const void *a
, const void *b
)
8624 const char **str_a
= (const char **) a
;
8625 const char **str_b
= (const char **) b
;
8627 return strcmp(*str_a
, *str_b
);
8630 struct kallsyms_data
{
8631 unsigned long *addrs
;
8637 /* This function gets called for all kernel and module symbols
8638 * and returns 1 in case we resolved all the requested symbols,
8641 static int kallsyms_callback(void *data
, const char *name
, unsigned long addr
)
8643 struct kallsyms_data
*args
= data
;
8647 sym
= bsearch(&name
, args
->syms
, args
->cnt
, sizeof(*args
->syms
), symbols_cmp
);
8651 idx
= sym
- args
->syms
;
8652 if (args
->addrs
[idx
])
8655 if (!ftrace_location(addr
))
8658 args
->addrs
[idx
] = addr
;
8660 return args
->found
== args
->cnt
? 1 : 0;
8664 * ftrace_lookup_symbols - Lookup addresses for array of symbols
8666 * @sorted_syms: array of symbols pointers symbols to resolve,
8667 * must be alphabetically sorted
8668 * @cnt: number of symbols/addresses in @syms/@addrs arrays
8669 * @addrs: array for storing resulting addresses
8671 * This function looks up addresses for array of symbols provided in
8672 * @syms array (must be alphabetically sorted) and stores them in
8673 * @addrs array, which needs to be big enough to store at least @cnt
8676 * Returns: 0 if all provided symbols are found, -ESRCH otherwise.
8678 int ftrace_lookup_symbols(const char **sorted_syms
, size_t cnt
, unsigned long *addrs
)
8680 struct kallsyms_data args
;
8683 memset(addrs
, 0, sizeof(*addrs
) * cnt
);
8685 args
.syms
= sorted_syms
;
8689 found_all
= kallsyms_on_each_symbol(kallsyms_callback
, &args
);
8692 found_all
= module_kallsyms_on_each_symbol(NULL
, kallsyms_callback
, &args
);
8693 return found_all
? 0 : -ESRCH
;
8696 #ifdef CONFIG_SYSCTL
8698 #ifdef CONFIG_DYNAMIC_FTRACE
8699 static void ftrace_startup_sysctl(void)
8703 if (unlikely(ftrace_disabled
))
8706 /* Force update next time */
8707 saved_ftrace_func
= NULL
;
8708 /* ftrace_start_up is true if we want ftrace running */
8709 if (ftrace_start_up
) {
8710 command
= FTRACE_UPDATE_CALLS
;
8711 if (ftrace_graph_active
)
8712 command
|= FTRACE_START_FUNC_RET
;
8713 ftrace_startup_enable(command
);
8717 static void ftrace_shutdown_sysctl(void)
8721 if (unlikely(ftrace_disabled
))
8724 /* ftrace_start_up is true if ftrace is running */
8725 if (ftrace_start_up
) {
8726 command
= FTRACE_DISABLE_CALLS
;
8727 if (ftrace_graph_active
)
8728 command
|= FTRACE_STOP_FUNC_RET
;
8729 ftrace_run_update_code(command
);
8733 # define ftrace_startup_sysctl() do { } while (0)
8734 # define ftrace_shutdown_sysctl() do { } while (0)
8735 #endif /* CONFIG_DYNAMIC_FTRACE */
8737 static bool is_permanent_ops_registered(void)
8739 struct ftrace_ops
*op
;
8741 do_for_each_ftrace_op(op
, ftrace_ops_list
) {
8742 if (op
->flags
& FTRACE_OPS_FL_PERMANENT
)
8744 } while_for_each_ftrace_op(op
);
8750 ftrace_enable_sysctl(const struct ctl_table
*table
, int write
,
8751 void *buffer
, size_t *lenp
, loff_t
*ppos
)
8755 mutex_lock(&ftrace_lock
);
8757 if (unlikely(ftrace_disabled
))
8760 ret
= proc_dointvec(table
, write
, buffer
, lenp
, ppos
);
8762 if (ret
|| !write
|| (last_ftrace_enabled
== !!ftrace_enabled
))
8765 if (ftrace_enabled
) {
8767 /* we are starting ftrace again */
8768 if (rcu_dereference_protected(ftrace_ops_list
,
8769 lockdep_is_held(&ftrace_lock
)) != &ftrace_list_end
)
8770 update_ftrace_function();
8772 ftrace_startup_sysctl();
8775 if (is_permanent_ops_registered()) {
8776 ftrace_enabled
= true;
8781 /* stopping ftrace calls (just send to ftrace_stub) */
8782 ftrace_trace_function
= ftrace_stub
;
8784 ftrace_shutdown_sysctl();
8787 last_ftrace_enabled
= !!ftrace_enabled
;
8789 mutex_unlock(&ftrace_lock
);
8793 static struct ctl_table ftrace_sysctls
[] = {
8795 .procname
= "ftrace_enabled",
8796 .data
= &ftrace_enabled
,
8797 .maxlen
= sizeof(int),
8799 .proc_handler
= ftrace_enable_sysctl
,
8803 static int __init
ftrace_sysctl_init(void)
8805 register_sysctl_init("kernel", ftrace_sysctls
);
8808 late_initcall(ftrace_sysctl_init
);