2 * Infrastructure for profiling code inserted by 'gcc -pg'.
4 * Copyright (C) 2007-2008 Steven Rostedt <srostedt@redhat.com>
5 * Copyright (C) 2004-2008 Ingo Molnar <mingo@redhat.com>
7 * Originally ported from the -rt patch by:
8 * Copyright (C) 2007 Arnaldo Carvalho de Melo <acme@redhat.com>
10 * Based on code in the latency_tracer, that is:
12 * Copyright (C) 2004-2006 Ingo Molnar
13 * Copyright (C) 2004 Nadia Yvette Chambers
16 #include <linux/stop_machine.h>
17 #include <linux/clocksource.h>
18 #include <linux/kallsyms.h>
19 #include <linux/seq_file.h>
20 #include <linux/suspend.h>
21 #include <linux/debugfs.h>
22 #include <linux/hardirq.h>
23 #include <linux/kthread.h>
24 #include <linux/uaccess.h>
25 #include <linux/bsearch.h>
26 #include <linux/module.h>
27 #include <linux/ftrace.h>
28 #include <linux/sysctl.h>
29 #include <linux/slab.h>
30 #include <linux/ctype.h>
31 #include <linux/sort.h>
32 #include <linux/list.h>
33 #include <linux/hash.h>
34 #include <linux/rcupdate.h>
36 #include <trace/events/sched.h>
38 #include <asm/setup.h>
40 #include "trace_output.h"
41 #include "trace_stat.h"
43 #define FTRACE_WARN_ON(cond) \
51 #define FTRACE_WARN_ON_ONCE(cond) \
54 if (WARN_ON_ONCE(___r)) \
59 /* hash bits for specific function selection */
60 #define FTRACE_HASH_BITS 7
61 #define FTRACE_FUNC_HASHSIZE (1 << FTRACE_HASH_BITS)
62 #define FTRACE_HASH_DEFAULT_BITS 10
63 #define FTRACE_HASH_MAX_BITS 12
65 #define FL_GLOBAL_CONTROL_MASK (FTRACE_OPS_FL_GLOBAL | FTRACE_OPS_FL_CONTROL)
67 #ifdef CONFIG_DYNAMIC_FTRACE
68 #define INIT_REGEX_LOCK(opsname) \
69 .regex_lock = __MUTEX_INITIALIZER(opsname.regex_lock),
71 #define INIT_REGEX_LOCK(opsname)
74 static struct ftrace_ops ftrace_list_end __read_mostly
= {
76 .flags
= FTRACE_OPS_FL_RECURSION_SAFE
| FTRACE_OPS_FL_STUB
,
79 /* ftrace_enabled is a method to turn ftrace on or off */
80 int ftrace_enabled __read_mostly
;
81 static int last_ftrace_enabled
;
83 /* Quick disabling of function tracer. */
84 int function_trace_stop __read_mostly
;
86 /* Current function tracing op */
87 struct ftrace_ops
*function_trace_op __read_mostly
= &ftrace_list_end
;
89 /* List for set_ftrace_pid's pids. */
90 LIST_HEAD(ftrace_pids
);
92 struct list_head list
;
97 * ftrace_disabled is set when an anomaly is discovered.
98 * ftrace_disabled is much stronger than ftrace_enabled.
100 static int ftrace_disabled __read_mostly
;
102 static DEFINE_MUTEX(ftrace_lock
);
104 static struct ftrace_ops
*ftrace_global_list __read_mostly
= &ftrace_list_end
;
105 static struct ftrace_ops
*ftrace_control_list __read_mostly
= &ftrace_list_end
;
106 static struct ftrace_ops
*ftrace_ops_list __read_mostly
= &ftrace_list_end
;
107 ftrace_func_t ftrace_trace_function __read_mostly
= ftrace_stub
;
108 ftrace_func_t ftrace_pid_function __read_mostly
= ftrace_stub
;
109 static struct ftrace_ops global_ops
;
110 static struct ftrace_ops control_ops
;
112 #if ARCH_SUPPORTS_FTRACE_OPS
113 static void ftrace_ops_list_func(unsigned long ip
, unsigned long parent_ip
,
114 struct ftrace_ops
*op
, struct pt_regs
*regs
);
116 /* See comment below, where ftrace_ops_list_func is defined */
117 static void ftrace_ops_no_ops(unsigned long ip
, unsigned long parent_ip
);
118 #define ftrace_ops_list_func ((ftrace_func_t)ftrace_ops_no_ops)
122 * Traverse the ftrace_global_list, invoking all entries. The reason that we
123 * can use rcu_dereference_raw_notrace() is that elements removed from this list
124 * are simply leaked, so there is no need to interact with a grace-period
125 * mechanism. The rcu_dereference_raw_notrace() calls are needed to handle
126 * concurrent insertions into the ftrace_global_list.
128 * Silly Alpha and silly pointer-speculation compiler optimizations!
130 #define do_for_each_ftrace_op(op, list) \
131 op = rcu_dereference_raw_notrace(list); \
135 * Optimized for just a single item in the list (as that is the normal case).
137 #define while_for_each_ftrace_op(op) \
138 while (likely(op = rcu_dereference_raw_notrace((op)->next)) && \
139 unlikely((op) != &ftrace_list_end))
141 static inline void ftrace_ops_init(struct ftrace_ops
*ops
)
143 #ifdef CONFIG_DYNAMIC_FTRACE
144 if (!(ops
->flags
& FTRACE_OPS_FL_INITIALIZED
)) {
145 mutex_init(&ops
->regex_lock
);
146 ops
->flags
|= FTRACE_OPS_FL_INITIALIZED
;
152 * ftrace_nr_registered_ops - return number of ops registered
154 * Returns the number of ftrace_ops registered and tracing functions
156 int ftrace_nr_registered_ops(void)
158 struct ftrace_ops
*ops
;
161 mutex_lock(&ftrace_lock
);
163 for (ops
= ftrace_ops_list
;
164 ops
!= &ftrace_list_end
; ops
= ops
->next
)
167 mutex_unlock(&ftrace_lock
);
173 ftrace_global_list_func(unsigned long ip
, unsigned long parent_ip
,
174 struct ftrace_ops
*op
, struct pt_regs
*regs
)
178 bit
= trace_test_and_set_recursion(TRACE_GLOBAL_START
, TRACE_GLOBAL_MAX
);
182 do_for_each_ftrace_op(op
, ftrace_global_list
) {
183 op
->func(ip
, parent_ip
, op
, regs
);
184 } while_for_each_ftrace_op(op
);
186 trace_clear_recursion(bit
);
189 static void ftrace_pid_func(unsigned long ip
, unsigned long parent_ip
,
190 struct ftrace_ops
*op
, struct pt_regs
*regs
)
192 if (!test_tsk_trace_trace(current
))
195 ftrace_pid_function(ip
, parent_ip
, op
, regs
);
198 static void set_ftrace_pid_function(ftrace_func_t func
)
200 /* do not set ftrace_pid_function to itself! */
201 if (func
!= ftrace_pid_func
)
202 ftrace_pid_function
= func
;
206 * clear_ftrace_function - reset the ftrace function
208 * This NULLs the ftrace function and in essence stops
209 * tracing. There may be lag
211 void clear_ftrace_function(void)
213 ftrace_trace_function
= ftrace_stub
;
214 ftrace_pid_function
= ftrace_stub
;
217 static void control_ops_disable_all(struct ftrace_ops
*ops
)
221 for_each_possible_cpu(cpu
)
222 *per_cpu_ptr(ops
->disabled
, cpu
) = 1;
225 static int control_ops_alloc(struct ftrace_ops
*ops
)
227 int __percpu
*disabled
;
229 disabled
= alloc_percpu(int);
233 ops
->disabled
= disabled
;
234 control_ops_disable_all(ops
);
238 static void control_ops_free(struct ftrace_ops
*ops
)
240 free_percpu(ops
->disabled
);
243 static void update_global_ops(void)
248 * If there's only one function registered, then call that
249 * function directly. Otherwise, we need to iterate over the
250 * registered callers.
252 if (ftrace_global_list
== &ftrace_list_end
||
253 ftrace_global_list
->next
== &ftrace_list_end
) {
254 func
= ftrace_global_list
->func
;
256 * As we are calling the function directly.
257 * If it does not have recursion protection,
258 * the function_trace_op needs to be updated
261 if (ftrace_global_list
->flags
& FTRACE_OPS_FL_RECURSION_SAFE
)
262 global_ops
.flags
|= FTRACE_OPS_FL_RECURSION_SAFE
;
264 global_ops
.flags
&= ~FTRACE_OPS_FL_RECURSION_SAFE
;
266 func
= ftrace_global_list_func
;
267 /* The list has its own recursion protection. */
268 global_ops
.flags
|= FTRACE_OPS_FL_RECURSION_SAFE
;
272 /* If we filter on pids, update to use the pid function */
273 if (!list_empty(&ftrace_pids
)) {
274 set_ftrace_pid_function(func
);
275 func
= ftrace_pid_func
;
278 global_ops
.func
= func
;
281 static void update_ftrace_function(void)
288 * If we are at the end of the list and this ops is
289 * recursion safe and not dynamic and the arch supports passing ops,
290 * then have the mcount trampoline call the function directly.
292 if (ftrace_ops_list
== &ftrace_list_end
||
293 (ftrace_ops_list
->next
== &ftrace_list_end
&&
294 !(ftrace_ops_list
->flags
& FTRACE_OPS_FL_DYNAMIC
) &&
295 (ftrace_ops_list
->flags
& FTRACE_OPS_FL_RECURSION_SAFE
) &&
296 !FTRACE_FORCE_LIST_FUNC
)) {
297 /* Set the ftrace_ops that the arch callback uses */
298 if (ftrace_ops_list
== &global_ops
)
299 function_trace_op
= ftrace_global_list
;
301 function_trace_op
= ftrace_ops_list
;
302 func
= ftrace_ops_list
->func
;
304 /* Just use the default ftrace_ops */
305 function_trace_op
= &ftrace_list_end
;
306 func
= ftrace_ops_list_func
;
309 ftrace_trace_function
= func
;
312 static void add_ftrace_ops(struct ftrace_ops
**list
, struct ftrace_ops
*ops
)
316 * We are entering ops into the list but another
317 * CPU might be walking that list. We need to make sure
318 * the ops->next pointer is valid before another CPU sees
319 * the ops pointer included into the list.
321 rcu_assign_pointer(*list
, ops
);
324 static int remove_ftrace_ops(struct ftrace_ops
**list
, struct ftrace_ops
*ops
)
326 struct ftrace_ops
**p
;
329 * If we are removing the last function, then simply point
330 * to the ftrace_stub.
332 if (*list
== ops
&& ops
->next
== &ftrace_list_end
) {
333 *list
= &ftrace_list_end
;
337 for (p
= list
; *p
!= &ftrace_list_end
; p
= &(*p
)->next
)
348 static void add_ftrace_list_ops(struct ftrace_ops
**list
,
349 struct ftrace_ops
*main_ops
,
350 struct ftrace_ops
*ops
)
352 int first
= *list
== &ftrace_list_end
;
353 add_ftrace_ops(list
, ops
);
355 add_ftrace_ops(&ftrace_ops_list
, main_ops
);
358 static int remove_ftrace_list_ops(struct ftrace_ops
**list
,
359 struct ftrace_ops
*main_ops
,
360 struct ftrace_ops
*ops
)
362 int ret
= remove_ftrace_ops(list
, ops
);
363 if (!ret
&& *list
== &ftrace_list_end
)
364 ret
= remove_ftrace_ops(&ftrace_ops_list
, main_ops
);
368 static int __register_ftrace_function(struct ftrace_ops
*ops
)
370 if (unlikely(ftrace_disabled
))
373 if (FTRACE_WARN_ON(ops
== &global_ops
))
376 if (WARN_ON(ops
->flags
& FTRACE_OPS_FL_ENABLED
))
379 /* We don't support both control and global flags set. */
380 if ((ops
->flags
& FL_GLOBAL_CONTROL_MASK
) == FL_GLOBAL_CONTROL_MASK
)
383 #ifndef CONFIG_DYNAMIC_FTRACE_WITH_REGS
385 * If the ftrace_ops specifies SAVE_REGS, then it only can be used
386 * if the arch supports it, or SAVE_REGS_IF_SUPPORTED is also set.
387 * Setting SAVE_REGS_IF_SUPPORTED makes SAVE_REGS irrelevant.
389 if (ops
->flags
& FTRACE_OPS_FL_SAVE_REGS
&&
390 !(ops
->flags
& FTRACE_OPS_FL_SAVE_REGS_IF_SUPPORTED
))
393 if (ops
->flags
& FTRACE_OPS_FL_SAVE_REGS_IF_SUPPORTED
)
394 ops
->flags
|= FTRACE_OPS_FL_SAVE_REGS
;
397 if (!core_kernel_data((unsigned long)ops
))
398 ops
->flags
|= FTRACE_OPS_FL_DYNAMIC
;
400 if (ops
->flags
& FTRACE_OPS_FL_GLOBAL
) {
401 add_ftrace_list_ops(&ftrace_global_list
, &global_ops
, ops
);
402 ops
->flags
|= FTRACE_OPS_FL_ENABLED
;
403 } else if (ops
->flags
& FTRACE_OPS_FL_CONTROL
) {
404 if (control_ops_alloc(ops
))
406 add_ftrace_list_ops(&ftrace_control_list
, &control_ops
, ops
);
408 add_ftrace_ops(&ftrace_ops_list
, ops
);
411 update_ftrace_function();
416 static void ftrace_sync(struct work_struct
*work
)
419 * This function is just a stub to implement a hard force
420 * of synchronize_sched(). This requires synchronizing
421 * tasks even in userspace and idle.
423 * Yes, function tracing is rude.
427 static int __unregister_ftrace_function(struct ftrace_ops
*ops
)
434 if (WARN_ON(!(ops
->flags
& FTRACE_OPS_FL_ENABLED
)))
437 if (FTRACE_WARN_ON(ops
== &global_ops
))
440 if (ops
->flags
& FTRACE_OPS_FL_GLOBAL
) {
441 ret
= remove_ftrace_list_ops(&ftrace_global_list
,
444 ops
->flags
&= ~FTRACE_OPS_FL_ENABLED
;
445 } else if (ops
->flags
& FTRACE_OPS_FL_CONTROL
) {
446 ret
= remove_ftrace_list_ops(&ftrace_control_list
,
450 * The ftrace_ops is now removed from the list,
451 * so there'll be no new users. We must ensure
452 * all current users are done before we free
454 * Note synchronize_sched() is not enough, as we
455 * use preempt_disable() to do RCU, but the function
456 * tracer can be called where RCU is not active
457 * (before user_exit()).
459 schedule_on_each_cpu(ftrace_sync
);
460 control_ops_free(ops
);
463 ret
= remove_ftrace_ops(&ftrace_ops_list
, ops
);
469 update_ftrace_function();
472 * Dynamic ops may be freed, we must make sure that all
473 * callers are done before leaving this function.
475 * Again, normal synchronize_sched() is not good enough.
476 * We need to do a hard force of sched synchronization.
478 if (ops
->flags
& FTRACE_OPS_FL_DYNAMIC
)
479 schedule_on_each_cpu(ftrace_sync
);
485 static void ftrace_update_pid_func(void)
487 /* Only do something if we are tracing something */
488 if (ftrace_trace_function
== ftrace_stub
)
491 update_ftrace_function();
494 #ifdef CONFIG_FUNCTION_PROFILER
495 struct ftrace_profile
{
496 struct hlist_node node
;
498 unsigned long counter
;
499 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
500 unsigned long long time
;
501 unsigned long long time_squared
;
505 struct ftrace_profile_page
{
506 struct ftrace_profile_page
*next
;
508 struct ftrace_profile records
[];
511 struct ftrace_profile_stat
{
513 struct hlist_head
*hash
;
514 struct ftrace_profile_page
*pages
;
515 struct ftrace_profile_page
*start
;
516 struct tracer_stat stat
;
519 #define PROFILE_RECORDS_SIZE \
520 (PAGE_SIZE - offsetof(struct ftrace_profile_page, records))
522 #define PROFILES_PER_PAGE \
523 (PROFILE_RECORDS_SIZE / sizeof(struct ftrace_profile))
525 static int ftrace_profile_enabled __read_mostly
;
527 /* ftrace_profile_lock - synchronize the enable and disable of the profiler */
528 static DEFINE_MUTEX(ftrace_profile_lock
);
530 static DEFINE_PER_CPU(struct ftrace_profile_stat
, ftrace_profile_stats
);
532 #define FTRACE_PROFILE_HASH_BITS 10
533 #define FTRACE_PROFILE_HASH_SIZE (1 << FTRACE_PROFILE_HASH_BITS)
536 function_stat_next(void *v
, int idx
)
538 struct ftrace_profile
*rec
= v
;
539 struct ftrace_profile_page
*pg
;
541 pg
= (struct ftrace_profile_page
*)((unsigned long)rec
& PAGE_MASK
);
547 if ((void *)rec
>= (void *)&pg
->records
[pg
->index
]) {
551 rec
= &pg
->records
[0];
559 static void *function_stat_start(struct tracer_stat
*trace
)
561 struct ftrace_profile_stat
*stat
=
562 container_of(trace
, struct ftrace_profile_stat
, stat
);
564 if (!stat
|| !stat
->start
)
567 return function_stat_next(&stat
->start
->records
[0], 0);
570 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
571 /* function graph compares on total time */
572 static int function_stat_cmp(void *p1
, void *p2
)
574 struct ftrace_profile
*a
= p1
;
575 struct ftrace_profile
*b
= p2
;
577 if (a
->time
< b
->time
)
579 if (a
->time
> b
->time
)
585 /* not function graph compares against hits */
586 static int function_stat_cmp(void *p1
, void *p2
)
588 struct ftrace_profile
*a
= p1
;
589 struct ftrace_profile
*b
= p2
;
591 if (a
->counter
< b
->counter
)
593 if (a
->counter
> b
->counter
)
600 static int function_stat_headers(struct seq_file
*m
)
602 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
603 seq_printf(m
, " Function "
606 "--- ---- --- ---\n");
608 seq_printf(m
, " Function Hit\n"
614 static int function_stat_show(struct seq_file
*m
, void *v
)
616 struct ftrace_profile
*rec
= v
;
617 char str
[KSYM_SYMBOL_LEN
];
619 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
620 static struct trace_seq s
;
621 unsigned long long avg
;
622 unsigned long long stddev
;
624 mutex_lock(&ftrace_profile_lock
);
626 /* we raced with function_profile_reset() */
627 if (unlikely(rec
->counter
== 0)) {
632 kallsyms_lookup(rec
->ip
, NULL
, NULL
, NULL
, str
);
633 seq_printf(m
, " %-30.30s %10lu", str
, rec
->counter
);
635 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
638 do_div(avg
, rec
->counter
);
640 /* Sample standard deviation (s^2) */
641 if (rec
->counter
<= 1)
645 * Apply Welford's method:
646 * s^2 = 1 / (n * (n-1)) * (n * \Sum (x_i)^2 - (\Sum x_i)^2)
648 stddev
= rec
->counter
* rec
->time_squared
-
649 rec
->time
* rec
->time
;
652 * Divide only 1000 for ns^2 -> us^2 conversion.
653 * trace_print_graph_duration will divide 1000 again.
655 do_div(stddev
, rec
->counter
* (rec
->counter
- 1) * 1000);
659 trace_print_graph_duration(rec
->time
, &s
);
660 trace_seq_puts(&s
, " ");
661 trace_print_graph_duration(avg
, &s
);
662 trace_seq_puts(&s
, " ");
663 trace_print_graph_duration(stddev
, &s
);
664 trace_print_seq(m
, &s
);
668 mutex_unlock(&ftrace_profile_lock
);
673 static void ftrace_profile_reset(struct ftrace_profile_stat
*stat
)
675 struct ftrace_profile_page
*pg
;
677 pg
= stat
->pages
= stat
->start
;
680 memset(pg
->records
, 0, PROFILE_RECORDS_SIZE
);
685 memset(stat
->hash
, 0,
686 FTRACE_PROFILE_HASH_SIZE
* sizeof(struct hlist_head
));
689 int ftrace_profile_pages_init(struct ftrace_profile_stat
*stat
)
691 struct ftrace_profile_page
*pg
;
696 /* If we already allocated, do nothing */
700 stat
->pages
= (void *)get_zeroed_page(GFP_KERNEL
);
704 #ifdef CONFIG_DYNAMIC_FTRACE
705 functions
= ftrace_update_tot_cnt
;
708 * We do not know the number of functions that exist because
709 * dynamic tracing is what counts them. With past experience
710 * we have around 20K functions. That should be more than enough.
711 * It is highly unlikely we will execute every function in
717 pg
= stat
->start
= stat
->pages
;
719 pages
= DIV_ROUND_UP(functions
, PROFILES_PER_PAGE
);
721 for (i
= 1; i
< pages
; i
++) {
722 pg
->next
= (void *)get_zeroed_page(GFP_KERNEL
);
733 unsigned long tmp
= (unsigned long)pg
;
745 static int ftrace_profile_init_cpu(int cpu
)
747 struct ftrace_profile_stat
*stat
;
750 stat
= &per_cpu(ftrace_profile_stats
, cpu
);
753 /* If the profile is already created, simply reset it */
754 ftrace_profile_reset(stat
);
759 * We are profiling all functions, but usually only a few thousand
760 * functions are hit. We'll make a hash of 1024 items.
762 size
= FTRACE_PROFILE_HASH_SIZE
;
764 stat
->hash
= kzalloc(sizeof(struct hlist_head
) * size
, GFP_KERNEL
);
769 /* Preallocate the function profiling pages */
770 if (ftrace_profile_pages_init(stat
) < 0) {
779 static int ftrace_profile_init(void)
784 for_each_online_cpu(cpu
) {
785 ret
= ftrace_profile_init_cpu(cpu
);
793 /* interrupts must be disabled */
794 static struct ftrace_profile
*
795 ftrace_find_profiled_func(struct ftrace_profile_stat
*stat
, unsigned long ip
)
797 struct ftrace_profile
*rec
;
798 struct hlist_head
*hhd
;
801 key
= hash_long(ip
, FTRACE_PROFILE_HASH_BITS
);
802 hhd
= &stat
->hash
[key
];
804 if (hlist_empty(hhd
))
807 hlist_for_each_entry_rcu_notrace(rec
, hhd
, node
) {
815 static void ftrace_add_profile(struct ftrace_profile_stat
*stat
,
816 struct ftrace_profile
*rec
)
820 key
= hash_long(rec
->ip
, FTRACE_PROFILE_HASH_BITS
);
821 hlist_add_head_rcu(&rec
->node
, &stat
->hash
[key
]);
825 * The memory is already allocated, this simply finds a new record to use.
827 static struct ftrace_profile
*
828 ftrace_profile_alloc(struct ftrace_profile_stat
*stat
, unsigned long ip
)
830 struct ftrace_profile
*rec
= NULL
;
832 /* prevent recursion (from NMIs) */
833 if (atomic_inc_return(&stat
->disabled
) != 1)
837 * Try to find the function again since an NMI
838 * could have added it
840 rec
= ftrace_find_profiled_func(stat
, ip
);
844 if (stat
->pages
->index
== PROFILES_PER_PAGE
) {
845 if (!stat
->pages
->next
)
847 stat
->pages
= stat
->pages
->next
;
850 rec
= &stat
->pages
->records
[stat
->pages
->index
++];
852 ftrace_add_profile(stat
, rec
);
855 atomic_dec(&stat
->disabled
);
861 function_profile_call(unsigned long ip
, unsigned long parent_ip
,
862 struct ftrace_ops
*ops
, struct pt_regs
*regs
)
864 struct ftrace_profile_stat
*stat
;
865 struct ftrace_profile
*rec
;
868 if (!ftrace_profile_enabled
)
871 local_irq_save(flags
);
873 stat
= &__get_cpu_var(ftrace_profile_stats
);
874 if (!stat
->hash
|| !ftrace_profile_enabled
)
877 rec
= ftrace_find_profiled_func(stat
, ip
);
879 rec
= ftrace_profile_alloc(stat
, ip
);
886 local_irq_restore(flags
);
889 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
890 static int profile_graph_entry(struct ftrace_graph_ent
*trace
)
892 function_profile_call(trace
->func
, 0, NULL
, NULL
);
896 static void profile_graph_return(struct ftrace_graph_ret
*trace
)
898 struct ftrace_profile_stat
*stat
;
899 unsigned long long calltime
;
900 struct ftrace_profile
*rec
;
903 local_irq_save(flags
);
904 stat
= &__get_cpu_var(ftrace_profile_stats
);
905 if (!stat
->hash
|| !ftrace_profile_enabled
)
908 /* If the calltime was zero'd ignore it */
909 if (!trace
->calltime
)
912 calltime
= trace
->rettime
- trace
->calltime
;
914 if (!(trace_flags
& TRACE_ITER_GRAPH_TIME
)) {
917 index
= trace
->depth
;
919 /* Append this call time to the parent time to subtract */
921 current
->ret_stack
[index
- 1].subtime
+= calltime
;
923 if (current
->ret_stack
[index
].subtime
< calltime
)
924 calltime
-= current
->ret_stack
[index
].subtime
;
929 rec
= ftrace_find_profiled_func(stat
, trace
->func
);
931 rec
->time
+= calltime
;
932 rec
->time_squared
+= calltime
* calltime
;
936 local_irq_restore(flags
);
939 static int register_ftrace_profiler(void)
941 return register_ftrace_graph(&profile_graph_return
,
942 &profile_graph_entry
);
945 static void unregister_ftrace_profiler(void)
947 unregister_ftrace_graph();
950 static struct ftrace_ops ftrace_profile_ops __read_mostly
= {
951 .func
= function_profile_call
,
952 .flags
= FTRACE_OPS_FL_RECURSION_SAFE
| FTRACE_OPS_FL_INITIALIZED
,
953 INIT_REGEX_LOCK(ftrace_profile_ops
)
956 static int register_ftrace_profiler(void)
958 return register_ftrace_function(&ftrace_profile_ops
);
961 static void unregister_ftrace_profiler(void)
963 unregister_ftrace_function(&ftrace_profile_ops
);
965 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
968 ftrace_profile_write(struct file
*filp
, const char __user
*ubuf
,
969 size_t cnt
, loff_t
*ppos
)
974 ret
= kstrtoul_from_user(ubuf
, cnt
, 10, &val
);
980 mutex_lock(&ftrace_profile_lock
);
981 if (ftrace_profile_enabled
^ val
) {
983 ret
= ftrace_profile_init();
989 ret
= register_ftrace_profiler();
994 ftrace_profile_enabled
= 1;
996 ftrace_profile_enabled
= 0;
998 * unregister_ftrace_profiler calls stop_machine
999 * so this acts like an synchronize_sched.
1001 unregister_ftrace_profiler();
1005 mutex_unlock(&ftrace_profile_lock
);
1013 ftrace_profile_read(struct file
*filp
, char __user
*ubuf
,
1014 size_t cnt
, loff_t
*ppos
)
1016 char buf
[64]; /* big enough to hold a number */
1019 r
= sprintf(buf
, "%u\n", ftrace_profile_enabled
);
1020 return simple_read_from_buffer(ubuf
, cnt
, ppos
, buf
, r
);
1023 static const struct file_operations ftrace_profile_fops
= {
1024 .open
= tracing_open_generic
,
1025 .read
= ftrace_profile_read
,
1026 .write
= ftrace_profile_write
,
1027 .llseek
= default_llseek
,
1030 /* used to initialize the real stat files */
1031 static struct tracer_stat function_stats __initdata
= {
1032 .name
= "functions",
1033 .stat_start
= function_stat_start
,
1034 .stat_next
= function_stat_next
,
1035 .stat_cmp
= function_stat_cmp
,
1036 .stat_headers
= function_stat_headers
,
1037 .stat_show
= function_stat_show
1040 static __init
void ftrace_profile_debugfs(struct dentry
*d_tracer
)
1042 struct ftrace_profile_stat
*stat
;
1043 struct dentry
*entry
;
1048 for_each_possible_cpu(cpu
) {
1049 stat
= &per_cpu(ftrace_profile_stats
, cpu
);
1051 /* allocate enough for function name + cpu number */
1052 name
= kmalloc(32, GFP_KERNEL
);
1055 * The files created are permanent, if something happens
1056 * we still do not free memory.
1059 "Could not allocate stat file for cpu %d\n",
1063 stat
->stat
= function_stats
;
1064 snprintf(name
, 32, "function%d", cpu
);
1065 stat
->stat
.name
= name
;
1066 ret
= register_stat_tracer(&stat
->stat
);
1069 "Could not register function stat for cpu %d\n",
1076 entry
= debugfs_create_file("function_profile_enabled", 0644,
1077 d_tracer
, NULL
, &ftrace_profile_fops
);
1079 pr_warning("Could not create debugfs "
1080 "'function_profile_enabled' entry\n");
1083 #else /* CONFIG_FUNCTION_PROFILER */
1084 static __init
void ftrace_profile_debugfs(struct dentry
*d_tracer
)
1087 #endif /* CONFIG_FUNCTION_PROFILER */
1089 static struct pid
* const ftrace_swapper_pid
= &init_struct_pid
;
1092 ftrace_filter_lseek(struct file
*file
, loff_t offset
, int whence
)
1096 if (file
->f_mode
& FMODE_READ
)
1097 ret
= seq_lseek(file
, offset
, whence
);
1099 file
->f_pos
= ret
= 1;
1104 #ifdef CONFIG_DYNAMIC_FTRACE
1106 #ifndef CONFIG_FTRACE_MCOUNT_RECORD
1107 # error Dynamic ftrace depends on MCOUNT_RECORD
1110 static struct hlist_head ftrace_func_hash
[FTRACE_FUNC_HASHSIZE
] __read_mostly
;
1112 struct ftrace_func_probe
{
1113 struct hlist_node node
;
1114 struct ftrace_probe_ops
*ops
;
1115 unsigned long flags
;
1118 struct list_head free_list
;
1121 struct ftrace_func_entry
{
1122 struct hlist_node hlist
;
1126 struct ftrace_hash
{
1127 unsigned long size_bits
;
1128 struct hlist_head
*buckets
;
1129 unsigned long count
;
1130 struct rcu_head rcu
;
1134 * We make these constant because no one should touch them,
1135 * but they are used as the default "empty hash", to avoid allocating
1136 * it all the time. These are in a read only section such that if
1137 * anyone does try to modify it, it will cause an exception.
1139 static const struct hlist_head empty_buckets
[1];
1140 static const struct ftrace_hash empty_hash
= {
1141 .buckets
= (struct hlist_head
*)empty_buckets
,
1143 #define EMPTY_HASH ((struct ftrace_hash *)&empty_hash)
1145 static struct ftrace_ops global_ops
= {
1146 .func
= ftrace_stub
,
1147 .notrace_hash
= EMPTY_HASH
,
1148 .filter_hash
= EMPTY_HASH
,
1149 .flags
= FTRACE_OPS_FL_RECURSION_SAFE
| FTRACE_OPS_FL_INITIALIZED
,
1150 INIT_REGEX_LOCK(global_ops
)
1153 struct ftrace_page
{
1154 struct ftrace_page
*next
;
1155 struct dyn_ftrace
*records
;
1160 static struct ftrace_page
*ftrace_new_pgs
;
1162 #define ENTRY_SIZE sizeof(struct dyn_ftrace)
1163 #define ENTRIES_PER_PAGE (PAGE_SIZE / ENTRY_SIZE)
1165 /* estimate from running different kernels */
1166 #define NR_TO_INIT 10000
1168 static struct ftrace_page
*ftrace_pages_start
;
1169 static struct ftrace_page
*ftrace_pages
;
1171 static bool ftrace_hash_empty(struct ftrace_hash
*hash
)
1173 return !hash
|| !hash
->count
;
1176 static struct ftrace_func_entry
*
1177 ftrace_lookup_ip(struct ftrace_hash
*hash
, unsigned long ip
)
1180 struct ftrace_func_entry
*entry
;
1181 struct hlist_head
*hhd
;
1183 if (ftrace_hash_empty(hash
))
1186 if (hash
->size_bits
> 0)
1187 key
= hash_long(ip
, hash
->size_bits
);
1191 hhd
= &hash
->buckets
[key
];
1193 hlist_for_each_entry_rcu_notrace(entry
, hhd
, hlist
) {
1194 if (entry
->ip
== ip
)
1200 static void __add_hash_entry(struct ftrace_hash
*hash
,
1201 struct ftrace_func_entry
*entry
)
1203 struct hlist_head
*hhd
;
1206 if (hash
->size_bits
)
1207 key
= hash_long(entry
->ip
, hash
->size_bits
);
1211 hhd
= &hash
->buckets
[key
];
1212 hlist_add_head(&entry
->hlist
, hhd
);
1216 static int add_hash_entry(struct ftrace_hash
*hash
, unsigned long ip
)
1218 struct ftrace_func_entry
*entry
;
1220 entry
= kmalloc(sizeof(*entry
), GFP_KERNEL
);
1225 __add_hash_entry(hash
, entry
);
1231 free_hash_entry(struct ftrace_hash
*hash
,
1232 struct ftrace_func_entry
*entry
)
1234 hlist_del(&entry
->hlist
);
1240 remove_hash_entry(struct ftrace_hash
*hash
,
1241 struct ftrace_func_entry
*entry
)
1243 hlist_del(&entry
->hlist
);
1247 static void ftrace_hash_clear(struct ftrace_hash
*hash
)
1249 struct hlist_head
*hhd
;
1250 struct hlist_node
*tn
;
1251 struct ftrace_func_entry
*entry
;
1252 int size
= 1 << hash
->size_bits
;
1258 for (i
= 0; i
< size
; i
++) {
1259 hhd
= &hash
->buckets
[i
];
1260 hlist_for_each_entry_safe(entry
, tn
, hhd
, hlist
)
1261 free_hash_entry(hash
, entry
);
1263 FTRACE_WARN_ON(hash
->count
);
1266 static void free_ftrace_hash(struct ftrace_hash
*hash
)
1268 if (!hash
|| hash
== EMPTY_HASH
)
1270 ftrace_hash_clear(hash
);
1271 kfree(hash
->buckets
);
1275 static void __free_ftrace_hash_rcu(struct rcu_head
*rcu
)
1277 struct ftrace_hash
*hash
;
1279 hash
= container_of(rcu
, struct ftrace_hash
, rcu
);
1280 free_ftrace_hash(hash
);
1283 static void free_ftrace_hash_rcu(struct ftrace_hash
*hash
)
1285 if (!hash
|| hash
== EMPTY_HASH
)
1287 call_rcu_sched(&hash
->rcu
, __free_ftrace_hash_rcu
);
1290 void ftrace_free_filter(struct ftrace_ops
*ops
)
1292 ftrace_ops_init(ops
);
1293 free_ftrace_hash(ops
->filter_hash
);
1294 free_ftrace_hash(ops
->notrace_hash
);
1297 static struct ftrace_hash
*alloc_ftrace_hash(int size_bits
)
1299 struct ftrace_hash
*hash
;
1302 hash
= kzalloc(sizeof(*hash
), GFP_KERNEL
);
1306 size
= 1 << size_bits
;
1307 hash
->buckets
= kcalloc(size
, sizeof(*hash
->buckets
), GFP_KERNEL
);
1309 if (!hash
->buckets
) {
1314 hash
->size_bits
= size_bits
;
1319 static struct ftrace_hash
*
1320 alloc_and_copy_ftrace_hash(int size_bits
, struct ftrace_hash
*hash
)
1322 struct ftrace_func_entry
*entry
;
1323 struct ftrace_hash
*new_hash
;
1328 new_hash
= alloc_ftrace_hash(size_bits
);
1333 if (ftrace_hash_empty(hash
))
1336 size
= 1 << hash
->size_bits
;
1337 for (i
= 0; i
< size
; i
++) {
1338 hlist_for_each_entry(entry
, &hash
->buckets
[i
], hlist
) {
1339 ret
= add_hash_entry(new_hash
, entry
->ip
);
1345 FTRACE_WARN_ON(new_hash
->count
!= hash
->count
);
1350 free_ftrace_hash(new_hash
);
1355 ftrace_hash_rec_disable(struct ftrace_ops
*ops
, int filter_hash
);
1357 ftrace_hash_rec_enable(struct ftrace_ops
*ops
, int filter_hash
);
1360 ftrace_hash_move(struct ftrace_ops
*ops
, int enable
,
1361 struct ftrace_hash
**dst
, struct ftrace_hash
*src
)
1363 struct ftrace_func_entry
*entry
;
1364 struct hlist_node
*tn
;
1365 struct hlist_head
*hhd
;
1366 struct ftrace_hash
*old_hash
;
1367 struct ftrace_hash
*new_hash
;
1368 int size
= src
->count
;
1374 * Remove the current set, update the hash and add
1377 ftrace_hash_rec_disable(ops
, enable
);
1380 * If the new source is empty, just free dst and assign it
1384 free_ftrace_hash_rcu(*dst
);
1385 rcu_assign_pointer(*dst
, EMPTY_HASH
);
1386 /* still need to update the function records */
1392 * Make the hash size about 1/2 the # found
1394 for (size
/= 2; size
; size
>>= 1)
1397 /* Don't allocate too much */
1398 if (bits
> FTRACE_HASH_MAX_BITS
)
1399 bits
= FTRACE_HASH_MAX_BITS
;
1402 new_hash
= alloc_ftrace_hash(bits
);
1406 size
= 1 << src
->size_bits
;
1407 for (i
= 0; i
< size
; i
++) {
1408 hhd
= &src
->buckets
[i
];
1409 hlist_for_each_entry_safe(entry
, tn
, hhd
, hlist
) {
1410 remove_hash_entry(src
, entry
);
1411 __add_hash_entry(new_hash
, entry
);
1416 rcu_assign_pointer(*dst
, new_hash
);
1417 free_ftrace_hash_rcu(old_hash
);
1422 * Enable regardless of ret:
1423 * On success, we enable the new hash.
1424 * On failure, we re-enable the original hash.
1426 ftrace_hash_rec_enable(ops
, enable
);
1432 * Test the hashes for this ops to see if we want to call
1433 * the ops->func or not.
1435 * It's a match if the ip is in the ops->filter_hash or
1436 * the filter_hash does not exist or is empty,
1438 * the ip is not in the ops->notrace_hash.
1440 * This needs to be called with preemption disabled as
1441 * the hashes are freed with call_rcu_sched().
1444 ftrace_ops_test(struct ftrace_ops
*ops
, unsigned long ip
, void *regs
)
1446 struct ftrace_hash
*filter_hash
;
1447 struct ftrace_hash
*notrace_hash
;
1450 #ifdef CONFIG_DYNAMIC_FTRACE_WITH_REGS
1452 * There's a small race when adding ops that the ftrace handler
1453 * that wants regs, may be called without them. We can not
1454 * allow that handler to be called if regs is NULL.
1456 if (regs
== NULL
&& (ops
->flags
& FTRACE_OPS_FL_SAVE_REGS
))
1460 filter_hash
= rcu_dereference_raw_notrace(ops
->filter_hash
);
1461 notrace_hash
= rcu_dereference_raw_notrace(ops
->notrace_hash
);
1463 if ((ftrace_hash_empty(filter_hash
) ||
1464 ftrace_lookup_ip(filter_hash
, ip
)) &&
1465 (ftrace_hash_empty(notrace_hash
) ||
1466 !ftrace_lookup_ip(notrace_hash
, ip
)))
1475 * This is a double for. Do not use 'break' to break out of the loop,
1476 * you must use a goto.
1478 #define do_for_each_ftrace_rec(pg, rec) \
1479 for (pg = ftrace_pages_start; pg; pg = pg->next) { \
1481 for (_____i = 0; _____i < pg->index; _____i++) { \
1482 rec = &pg->records[_____i];
1484 #define while_for_each_ftrace_rec() \
1489 static int ftrace_cmp_recs(const void *a
, const void *b
)
1491 const struct dyn_ftrace
*key
= a
;
1492 const struct dyn_ftrace
*rec
= b
;
1494 if (key
->flags
< rec
->ip
)
1496 if (key
->ip
>= rec
->ip
+ MCOUNT_INSN_SIZE
)
1501 static unsigned long ftrace_location_range(unsigned long start
, unsigned long end
)
1503 struct ftrace_page
*pg
;
1504 struct dyn_ftrace
*rec
;
1505 struct dyn_ftrace key
;
1508 key
.flags
= end
; /* overload flags, as it is unsigned long */
1510 for (pg
= ftrace_pages_start
; pg
; pg
= pg
->next
) {
1511 if (end
< pg
->records
[0].ip
||
1512 start
>= (pg
->records
[pg
->index
- 1].ip
+ MCOUNT_INSN_SIZE
))
1514 rec
= bsearch(&key
, pg
->records
, pg
->index
,
1515 sizeof(struct dyn_ftrace
),
1525 * ftrace_location - return true if the ip giving is a traced location
1526 * @ip: the instruction pointer to check
1528 * Returns rec->ip if @ip given is a pointer to a ftrace location.
1529 * That is, the instruction that is either a NOP or call to
1530 * the function tracer. It checks the ftrace internal tables to
1531 * determine if the address belongs or not.
1533 unsigned long ftrace_location(unsigned long ip
)
1535 return ftrace_location_range(ip
, ip
);
1539 * ftrace_text_reserved - return true if range contains an ftrace location
1540 * @start: start of range to search
1541 * @end: end of range to search (inclusive). @end points to the last byte to check.
1543 * Returns 1 if @start and @end contains a ftrace location.
1544 * That is, the instruction that is either a NOP or call to
1545 * the function tracer. It checks the ftrace internal tables to
1546 * determine if the address belongs or not.
1548 int ftrace_text_reserved(void *start
, void *end
)
1552 ret
= ftrace_location_range((unsigned long)start
,
1553 (unsigned long)end
);
1558 static void __ftrace_hash_rec_update(struct ftrace_ops
*ops
,
1562 struct ftrace_hash
*hash
;
1563 struct ftrace_hash
*other_hash
;
1564 struct ftrace_page
*pg
;
1565 struct dyn_ftrace
*rec
;
1569 /* Only update if the ops has been registered */
1570 if (!(ops
->flags
& FTRACE_OPS_FL_ENABLED
))
1574 * In the filter_hash case:
1575 * If the count is zero, we update all records.
1576 * Otherwise we just update the items in the hash.
1578 * In the notrace_hash case:
1579 * We enable the update in the hash.
1580 * As disabling notrace means enabling the tracing,
1581 * and enabling notrace means disabling, the inc variable
1585 hash
= ops
->filter_hash
;
1586 other_hash
= ops
->notrace_hash
;
1587 if (ftrace_hash_empty(hash
))
1591 hash
= ops
->notrace_hash
;
1592 other_hash
= ops
->filter_hash
;
1594 * If the notrace hash has no items,
1595 * then there's nothing to do.
1597 if (ftrace_hash_empty(hash
))
1601 do_for_each_ftrace_rec(pg
, rec
) {
1602 int in_other_hash
= 0;
1608 * Only the filter_hash affects all records.
1609 * Update if the record is not in the notrace hash.
1611 if (!other_hash
|| !ftrace_lookup_ip(other_hash
, rec
->ip
))
1614 in_hash
= !!ftrace_lookup_ip(hash
, rec
->ip
);
1615 in_other_hash
= !!ftrace_lookup_ip(other_hash
, rec
->ip
);
1620 if (filter_hash
&& in_hash
&& !in_other_hash
)
1622 else if (!filter_hash
&& in_hash
&&
1623 (in_other_hash
|| ftrace_hash_empty(other_hash
)))
1631 if (FTRACE_WARN_ON((rec
->flags
& ~FTRACE_FL_MASK
) == FTRACE_REF_MAX
))
1634 * If any ops wants regs saved for this function
1635 * then all ops will get saved regs.
1637 if (ops
->flags
& FTRACE_OPS_FL_SAVE_REGS
)
1638 rec
->flags
|= FTRACE_FL_REGS
;
1640 if (FTRACE_WARN_ON((rec
->flags
& ~FTRACE_FL_MASK
) == 0))
1645 /* Shortcut, if we handled all records, we are done. */
1646 if (!all
&& count
== hash
->count
)
1648 } while_for_each_ftrace_rec();
1651 static void ftrace_hash_rec_disable(struct ftrace_ops
*ops
,
1654 __ftrace_hash_rec_update(ops
, filter_hash
, 0);
1657 static void ftrace_hash_rec_enable(struct ftrace_ops
*ops
,
1660 __ftrace_hash_rec_update(ops
, filter_hash
, 1);
1663 static void print_ip_ins(const char *fmt
, unsigned char *p
)
1667 printk(KERN_CONT
"%s", fmt
);
1669 for (i
= 0; i
< MCOUNT_INSN_SIZE
; i
++)
1670 printk(KERN_CONT
"%s%02x", i
? ":" : "", p
[i
]);
1674 * ftrace_bug - report and shutdown function tracer
1675 * @failed: The failed type (EFAULT, EINVAL, EPERM)
1676 * @ip: The address that failed
1678 * The arch code that enables or disables the function tracing
1679 * can call ftrace_bug() when it has detected a problem in
1680 * modifying the code. @failed should be one of either:
1681 * EFAULT - if the problem happens on reading the @ip address
1682 * EINVAL - if what is read at @ip is not what was expected
1683 * EPERM - if the problem happens on writting to the @ip address
1685 void ftrace_bug(int failed
, unsigned long ip
)
1689 FTRACE_WARN_ON_ONCE(1);
1690 pr_info("ftrace faulted on modifying ");
1694 FTRACE_WARN_ON_ONCE(1);
1695 pr_info("ftrace failed to modify ");
1697 print_ip_ins(" actual: ", (unsigned char *)ip
);
1698 printk(KERN_CONT
"\n");
1701 FTRACE_WARN_ON_ONCE(1);
1702 pr_info("ftrace faulted on writing ");
1706 FTRACE_WARN_ON_ONCE(1);
1707 pr_info("ftrace faulted on unknown error ");
1712 static int ftrace_check_record(struct dyn_ftrace
*rec
, int enable
, int update
)
1714 unsigned long flag
= 0UL;
1717 * If we are updating calls:
1719 * If the record has a ref count, then we need to enable it
1720 * because someone is using it.
1722 * Otherwise we make sure its disabled.
1724 * If we are disabling calls, then disable all records that
1727 if (enable
&& (rec
->flags
& ~FTRACE_FL_MASK
))
1728 flag
= FTRACE_FL_ENABLED
;
1731 * If enabling and the REGS flag does not match the REGS_EN, then
1732 * do not ignore this record. Set flags to fail the compare against
1736 (!(rec
->flags
& FTRACE_FL_REGS
) != !(rec
->flags
& FTRACE_FL_REGS_EN
)))
1737 flag
|= FTRACE_FL_REGS
;
1739 /* If the state of this record hasn't changed, then do nothing */
1740 if ((rec
->flags
& FTRACE_FL_ENABLED
) == flag
)
1741 return FTRACE_UPDATE_IGNORE
;
1744 /* Save off if rec is being enabled (for return value) */
1745 flag
^= rec
->flags
& FTRACE_FL_ENABLED
;
1748 rec
->flags
|= FTRACE_FL_ENABLED
;
1749 if (flag
& FTRACE_FL_REGS
) {
1750 if (rec
->flags
& FTRACE_FL_REGS
)
1751 rec
->flags
|= FTRACE_FL_REGS_EN
;
1753 rec
->flags
&= ~FTRACE_FL_REGS_EN
;
1758 * If this record is being updated from a nop, then
1759 * return UPDATE_MAKE_CALL.
1760 * Otherwise, if the EN flag is set, then return
1761 * UPDATE_MODIFY_CALL_REGS to tell the caller to convert
1762 * from the non-save regs, to a save regs function.
1764 * return UPDATE_MODIFY_CALL to tell the caller to convert
1765 * from the save regs, to a non-save regs function.
1767 if (flag
& FTRACE_FL_ENABLED
)
1768 return FTRACE_UPDATE_MAKE_CALL
;
1769 else if (rec
->flags
& FTRACE_FL_REGS_EN
)
1770 return FTRACE_UPDATE_MODIFY_CALL_REGS
;
1772 return FTRACE_UPDATE_MODIFY_CALL
;
1776 /* If there's no more users, clear all flags */
1777 if (!(rec
->flags
& ~FTRACE_FL_MASK
))
1780 /* Just disable the record (keep REGS state) */
1781 rec
->flags
&= ~FTRACE_FL_ENABLED
;
1784 return FTRACE_UPDATE_MAKE_NOP
;
1788 * ftrace_update_record, set a record that now is tracing or not
1789 * @rec: the record to update
1790 * @enable: set to 1 if the record is tracing, zero to force disable
1792 * The records that represent all functions that can be traced need
1793 * to be updated when tracing has been enabled.
1795 int ftrace_update_record(struct dyn_ftrace
*rec
, int enable
)
1797 return ftrace_check_record(rec
, enable
, 1);
1801 * ftrace_test_record, check if the record has been enabled or not
1802 * @rec: the record to test
1803 * @enable: set to 1 to check if enabled, 0 if it is disabled
1805 * The arch code may need to test if a record is already set to
1806 * tracing to determine how to modify the function code that it
1809 int ftrace_test_record(struct dyn_ftrace
*rec
, int enable
)
1811 return ftrace_check_record(rec
, enable
, 0);
1815 __ftrace_replace_code(struct dyn_ftrace
*rec
, int enable
)
1817 unsigned long ftrace_old_addr
;
1818 unsigned long ftrace_addr
;
1821 ret
= ftrace_update_record(rec
, enable
);
1823 if (rec
->flags
& FTRACE_FL_REGS
)
1824 ftrace_addr
= (unsigned long)FTRACE_REGS_ADDR
;
1826 ftrace_addr
= (unsigned long)FTRACE_ADDR
;
1829 case FTRACE_UPDATE_IGNORE
:
1832 case FTRACE_UPDATE_MAKE_CALL
:
1833 return ftrace_make_call(rec
, ftrace_addr
);
1835 case FTRACE_UPDATE_MAKE_NOP
:
1836 return ftrace_make_nop(NULL
, rec
, ftrace_addr
);
1838 case FTRACE_UPDATE_MODIFY_CALL_REGS
:
1839 case FTRACE_UPDATE_MODIFY_CALL
:
1840 if (rec
->flags
& FTRACE_FL_REGS
)
1841 ftrace_old_addr
= (unsigned long)FTRACE_ADDR
;
1843 ftrace_old_addr
= (unsigned long)FTRACE_REGS_ADDR
;
1845 return ftrace_modify_call(rec
, ftrace_old_addr
, ftrace_addr
);
1848 return -1; /* unknow ftrace bug */
1851 void __weak
ftrace_replace_code(int enable
)
1853 struct dyn_ftrace
*rec
;
1854 struct ftrace_page
*pg
;
1857 if (unlikely(ftrace_disabled
))
1860 do_for_each_ftrace_rec(pg
, rec
) {
1861 failed
= __ftrace_replace_code(rec
, enable
);
1863 ftrace_bug(failed
, rec
->ip
);
1864 /* Stop processing */
1867 } while_for_each_ftrace_rec();
1870 struct ftrace_rec_iter
{
1871 struct ftrace_page
*pg
;
1876 * ftrace_rec_iter_start, start up iterating over traced functions
1878 * Returns an iterator handle that is used to iterate over all
1879 * the records that represent address locations where functions
1882 * May return NULL if no records are available.
1884 struct ftrace_rec_iter
*ftrace_rec_iter_start(void)
1887 * We only use a single iterator.
1888 * Protected by the ftrace_lock mutex.
1890 static struct ftrace_rec_iter ftrace_rec_iter
;
1891 struct ftrace_rec_iter
*iter
= &ftrace_rec_iter
;
1893 iter
->pg
= ftrace_pages_start
;
1896 /* Could have empty pages */
1897 while (iter
->pg
&& !iter
->pg
->index
)
1898 iter
->pg
= iter
->pg
->next
;
1907 * ftrace_rec_iter_next, get the next record to process.
1908 * @iter: The handle to the iterator.
1910 * Returns the next iterator after the given iterator @iter.
1912 struct ftrace_rec_iter
*ftrace_rec_iter_next(struct ftrace_rec_iter
*iter
)
1916 if (iter
->index
>= iter
->pg
->index
) {
1917 iter
->pg
= iter
->pg
->next
;
1920 /* Could have empty pages */
1921 while (iter
->pg
&& !iter
->pg
->index
)
1922 iter
->pg
= iter
->pg
->next
;
1932 * ftrace_rec_iter_record, get the record at the iterator location
1933 * @iter: The current iterator location
1935 * Returns the record that the current @iter is at.
1937 struct dyn_ftrace
*ftrace_rec_iter_record(struct ftrace_rec_iter
*iter
)
1939 return &iter
->pg
->records
[iter
->index
];
1943 ftrace_code_disable(struct module
*mod
, struct dyn_ftrace
*rec
)
1950 if (unlikely(ftrace_disabled
))
1953 ret
= ftrace_make_nop(mod
, rec
, MCOUNT_ADDR
);
1955 ftrace_bug(ret
, ip
);
1962 * archs can override this function if they must do something
1963 * before the modifying code is performed.
1965 int __weak
ftrace_arch_code_modify_prepare(void)
1971 * archs can override this function if they must do something
1972 * after the modifying code is performed.
1974 int __weak
ftrace_arch_code_modify_post_process(void)
1979 void ftrace_modify_all_code(int command
)
1981 if (command
& FTRACE_UPDATE_CALLS
)
1982 ftrace_replace_code(1);
1983 else if (command
& FTRACE_DISABLE_CALLS
)
1984 ftrace_replace_code(0);
1986 if (command
& FTRACE_UPDATE_TRACE_FUNC
)
1987 ftrace_update_ftrace_func(ftrace_trace_function
);
1989 if (command
& FTRACE_START_FUNC_RET
)
1990 ftrace_enable_ftrace_graph_caller();
1991 else if (command
& FTRACE_STOP_FUNC_RET
)
1992 ftrace_disable_ftrace_graph_caller();
1995 static int __ftrace_modify_code(void *data
)
1997 int *command
= data
;
1999 ftrace_modify_all_code(*command
);
2005 * ftrace_run_stop_machine, go back to the stop machine method
2006 * @command: The command to tell ftrace what to do
2008 * If an arch needs to fall back to the stop machine method, the
2009 * it can call this function.
2011 void ftrace_run_stop_machine(int command
)
2013 stop_machine(__ftrace_modify_code
, &command
, NULL
);
2017 * arch_ftrace_update_code, modify the code to trace or not trace
2018 * @command: The command that needs to be done
2020 * Archs can override this function if it does not need to
2021 * run stop_machine() to modify code.
2023 void __weak
arch_ftrace_update_code(int command
)
2025 ftrace_run_stop_machine(command
);
2028 static void ftrace_run_update_code(int command
)
2032 ret
= ftrace_arch_code_modify_prepare();
2033 FTRACE_WARN_ON(ret
);
2037 * Do not call function tracer while we update the code.
2038 * We are in stop machine.
2040 function_trace_stop
++;
2043 * By default we use stop_machine() to modify the code.
2044 * But archs can do what ever they want as long as it
2045 * is safe. The stop_machine() is the safest, but also
2046 * produces the most overhead.
2048 arch_ftrace_update_code(command
);
2050 function_trace_stop
--;
2052 ret
= ftrace_arch_code_modify_post_process();
2053 FTRACE_WARN_ON(ret
);
2056 static ftrace_func_t saved_ftrace_func
;
2057 static int ftrace_start_up
;
2058 static int global_start_up
;
2060 static void ftrace_startup_enable(int command
)
2062 if (saved_ftrace_func
!= ftrace_trace_function
) {
2063 saved_ftrace_func
= ftrace_trace_function
;
2064 command
|= FTRACE_UPDATE_TRACE_FUNC
;
2067 if (!command
|| !ftrace_enabled
)
2070 ftrace_run_update_code(command
);
2073 static int ftrace_startup(struct ftrace_ops
*ops
, int command
)
2075 bool hash_enable
= true;
2077 if (unlikely(ftrace_disabled
))
2081 command
|= FTRACE_UPDATE_CALLS
;
2083 /* ops marked global share the filter hashes */
2084 if (ops
->flags
& FTRACE_OPS_FL_GLOBAL
) {
2086 /* Don't update hash if global is already set */
2087 if (global_start_up
)
2088 hash_enable
= false;
2092 ops
->flags
|= FTRACE_OPS_FL_ENABLED
;
2094 ftrace_hash_rec_enable(ops
, 1);
2096 ftrace_startup_enable(command
);
2101 static void ftrace_shutdown(struct ftrace_ops
*ops
, int command
)
2103 bool hash_disable
= true;
2105 if (unlikely(ftrace_disabled
))
2110 * Just warn in case of unbalance, no need to kill ftrace, it's not
2111 * critical but the ftrace_call callers may be never nopped again after
2112 * further ftrace uses.
2114 WARN_ON_ONCE(ftrace_start_up
< 0);
2116 if (ops
->flags
& FTRACE_OPS_FL_GLOBAL
) {
2119 WARN_ON_ONCE(global_start_up
< 0);
2120 /* Don't update hash if global still has users */
2121 if (global_start_up
) {
2122 WARN_ON_ONCE(!ftrace_start_up
);
2123 hash_disable
= false;
2128 ftrace_hash_rec_disable(ops
, 1);
2130 if (ops
!= &global_ops
|| !global_start_up
)
2131 ops
->flags
&= ~FTRACE_OPS_FL_ENABLED
;
2133 command
|= FTRACE_UPDATE_CALLS
;
2135 if (saved_ftrace_func
!= ftrace_trace_function
) {
2136 saved_ftrace_func
= ftrace_trace_function
;
2137 command
|= FTRACE_UPDATE_TRACE_FUNC
;
2140 if (!command
|| !ftrace_enabled
)
2143 ftrace_run_update_code(command
);
2146 static void ftrace_startup_sysctl(void)
2148 if (unlikely(ftrace_disabled
))
2151 /* Force update next time */
2152 saved_ftrace_func
= NULL
;
2153 /* ftrace_start_up is true if we want ftrace running */
2154 if (ftrace_start_up
)
2155 ftrace_run_update_code(FTRACE_UPDATE_CALLS
);
2158 static void ftrace_shutdown_sysctl(void)
2160 if (unlikely(ftrace_disabled
))
2163 /* ftrace_start_up is true if ftrace is running */
2164 if (ftrace_start_up
)
2165 ftrace_run_update_code(FTRACE_DISABLE_CALLS
);
2168 static cycle_t ftrace_update_time
;
2169 static unsigned long ftrace_update_cnt
;
2170 unsigned long ftrace_update_tot_cnt
;
2172 static inline int ops_traces_mod(struct ftrace_ops
*ops
)
2175 * Filter_hash being empty will default to trace module.
2176 * But notrace hash requires a test of individual module functions.
2178 return ftrace_hash_empty(ops
->filter_hash
) &&
2179 ftrace_hash_empty(ops
->notrace_hash
);
2183 * Check if the current ops references the record.
2185 * If the ops traces all functions, then it was already accounted for.
2186 * If the ops does not trace the current record function, skip it.
2187 * If the ops ignores the function via notrace filter, skip it.
2190 ops_references_rec(struct ftrace_ops
*ops
, struct dyn_ftrace
*rec
)
2192 /* If ops isn't enabled, ignore it */
2193 if (!(ops
->flags
& FTRACE_OPS_FL_ENABLED
))
2196 /* If ops traces all mods, we already accounted for it */
2197 if (ops_traces_mod(ops
))
2200 /* The function must be in the filter */
2201 if (!ftrace_hash_empty(ops
->filter_hash
) &&
2202 !ftrace_lookup_ip(ops
->filter_hash
, rec
->ip
))
2205 /* If in notrace hash, we ignore it too */
2206 if (ftrace_lookup_ip(ops
->notrace_hash
, rec
->ip
))
2212 static int referenced_filters(struct dyn_ftrace
*rec
)
2214 struct ftrace_ops
*ops
;
2217 for (ops
= ftrace_ops_list
; ops
!= &ftrace_list_end
; ops
= ops
->next
) {
2218 if (ops_references_rec(ops
, rec
))
2225 static int ftrace_update_code(struct module
*mod
)
2227 struct ftrace_page
*pg
;
2228 struct dyn_ftrace
*p
;
2229 cycle_t start
, stop
;
2230 unsigned long ref
= 0;
2235 * When adding a module, we need to check if tracers are
2236 * currently enabled and if they are set to trace all functions.
2237 * If they are, we need to enable the module functions as well
2238 * as update the reference counts for those function records.
2241 struct ftrace_ops
*ops
;
2243 for (ops
= ftrace_ops_list
;
2244 ops
!= &ftrace_list_end
; ops
= ops
->next
) {
2245 if (ops
->flags
& FTRACE_OPS_FL_ENABLED
) {
2246 if (ops_traces_mod(ops
))
2254 start
= ftrace_now(raw_smp_processor_id());
2255 ftrace_update_cnt
= 0;
2257 for (pg
= ftrace_new_pgs
; pg
; pg
= pg
->next
) {
2259 for (i
= 0; i
< pg
->index
; i
++) {
2262 /* If something went wrong, bail without enabling anything */
2263 if (unlikely(ftrace_disabled
))
2266 p
= &pg
->records
[i
];
2268 cnt
+= referenced_filters(p
);
2272 * Do the initial record conversion from mcount jump
2273 * to the NOP instructions.
2275 if (!ftrace_code_disable(mod
, p
))
2278 ftrace_update_cnt
++;
2281 * If the tracing is enabled, go ahead and enable the record.
2283 * The reason not to enable the record immediatelly is the
2284 * inherent check of ftrace_make_nop/ftrace_make_call for
2285 * correct previous instructions. Making first the NOP
2286 * conversion puts the module to the correct state, thus
2287 * passing the ftrace_make_call check.
2289 if (ftrace_start_up
&& cnt
) {
2290 int failed
= __ftrace_replace_code(p
, 1);
2292 ftrace_bug(failed
, p
->ip
);
2297 ftrace_new_pgs
= NULL
;
2299 stop
= ftrace_now(raw_smp_processor_id());
2300 ftrace_update_time
= stop
- start
;
2301 ftrace_update_tot_cnt
+= ftrace_update_cnt
;
2306 static int ftrace_allocate_records(struct ftrace_page
*pg
, int count
)
2311 if (WARN_ON(!count
))
2314 order
= get_count_order(DIV_ROUND_UP(count
, ENTRIES_PER_PAGE
));
2317 * We want to fill as much as possible. No more than a page
2320 while ((PAGE_SIZE
<< order
) / ENTRY_SIZE
>= count
+ ENTRIES_PER_PAGE
)
2324 pg
->records
= (void *)__get_free_pages(GFP_KERNEL
| __GFP_ZERO
, order
);
2327 /* if we can't allocate this size, try something smaller */
2334 cnt
= (PAGE_SIZE
<< order
) / ENTRY_SIZE
;
2343 static struct ftrace_page
*
2344 ftrace_allocate_pages(unsigned long num_to_init
)
2346 struct ftrace_page
*start_pg
;
2347 struct ftrace_page
*pg
;
2354 start_pg
= pg
= kzalloc(sizeof(*pg
), GFP_KERNEL
);
2359 * Try to allocate as much as possible in one continues
2360 * location that fills in all of the space. We want to
2361 * waste as little space as possible.
2364 cnt
= ftrace_allocate_records(pg
, num_to_init
);
2372 pg
->next
= kzalloc(sizeof(*pg
), GFP_KERNEL
);
2383 order
= get_count_order(pg
->size
/ ENTRIES_PER_PAGE
);
2384 free_pages((unsigned long)pg
->records
, order
);
2385 start_pg
= pg
->next
;
2389 pr_info("ftrace: FAILED to allocate memory for functions\n");
2393 static int __init
ftrace_dyn_table_alloc(unsigned long num_to_init
)
2398 pr_info("ftrace: No functions to be traced?\n");
2402 cnt
= num_to_init
/ ENTRIES_PER_PAGE
;
2403 pr_info("ftrace: allocating %ld entries in %d pages\n",
2404 num_to_init
, cnt
+ 1);
2409 #define FTRACE_BUFF_MAX (KSYM_SYMBOL_LEN+4) /* room for wildcards */
2411 struct ftrace_iterator
{
2414 struct ftrace_page
*pg
;
2415 struct dyn_ftrace
*func
;
2416 struct ftrace_func_probe
*probe
;
2417 struct trace_parser parser
;
2418 struct ftrace_hash
*hash
;
2419 struct ftrace_ops
*ops
;
2426 t_hash_next(struct seq_file
*m
, loff_t
*pos
)
2428 struct ftrace_iterator
*iter
= m
->private;
2429 struct hlist_node
*hnd
= NULL
;
2430 struct hlist_head
*hhd
;
2436 hnd
= &iter
->probe
->node
;
2438 if (iter
->hidx
>= FTRACE_FUNC_HASHSIZE
)
2441 hhd
= &ftrace_func_hash
[iter
->hidx
];
2443 if (hlist_empty(hhd
)) {
2459 if (WARN_ON_ONCE(!hnd
))
2462 iter
->probe
= hlist_entry(hnd
, struct ftrace_func_probe
, node
);
2467 static void *t_hash_start(struct seq_file
*m
, loff_t
*pos
)
2469 struct ftrace_iterator
*iter
= m
->private;
2473 if (!(iter
->flags
& FTRACE_ITER_DO_HASH
))
2476 if (iter
->func_pos
> *pos
)
2480 for (l
= 0; l
<= (*pos
- iter
->func_pos
); ) {
2481 p
= t_hash_next(m
, &l
);
2488 /* Only set this if we have an item */
2489 iter
->flags
|= FTRACE_ITER_HASH
;
2495 t_hash_show(struct seq_file
*m
, struct ftrace_iterator
*iter
)
2497 struct ftrace_func_probe
*rec
;
2500 if (WARN_ON_ONCE(!rec
))
2503 if (rec
->ops
->print
)
2504 return rec
->ops
->print(m
, rec
->ip
, rec
->ops
, rec
->data
);
2506 seq_printf(m
, "%ps:%ps", (void *)rec
->ip
, (void *)rec
->ops
->func
);
2509 seq_printf(m
, ":%p", rec
->data
);
2516 t_next(struct seq_file
*m
, void *v
, loff_t
*pos
)
2518 struct ftrace_iterator
*iter
= m
->private;
2519 struct ftrace_ops
*ops
= iter
->ops
;
2520 struct dyn_ftrace
*rec
= NULL
;
2522 if (unlikely(ftrace_disabled
))
2525 if (iter
->flags
& FTRACE_ITER_HASH
)
2526 return t_hash_next(m
, pos
);
2529 iter
->pos
= iter
->func_pos
= *pos
;
2531 if (iter
->flags
& FTRACE_ITER_PRINTALL
)
2532 return t_hash_start(m
, pos
);
2535 if (iter
->idx
>= iter
->pg
->index
) {
2536 if (iter
->pg
->next
) {
2537 iter
->pg
= iter
->pg
->next
;
2542 rec
= &iter
->pg
->records
[iter
->idx
++];
2543 if (((iter
->flags
& FTRACE_ITER_FILTER
) &&
2544 !(ftrace_lookup_ip(ops
->filter_hash
, rec
->ip
))) ||
2546 ((iter
->flags
& FTRACE_ITER_NOTRACE
) &&
2547 !ftrace_lookup_ip(ops
->notrace_hash
, rec
->ip
)) ||
2549 ((iter
->flags
& FTRACE_ITER_ENABLED
) &&
2550 !(rec
->flags
& FTRACE_FL_ENABLED
))) {
2558 return t_hash_start(m
, pos
);
2565 static void reset_iter_read(struct ftrace_iterator
*iter
)
2569 iter
->flags
&= ~(FTRACE_ITER_PRINTALL
| FTRACE_ITER_HASH
);
2572 static void *t_start(struct seq_file
*m
, loff_t
*pos
)
2574 struct ftrace_iterator
*iter
= m
->private;
2575 struct ftrace_ops
*ops
= iter
->ops
;
2579 mutex_lock(&ftrace_lock
);
2581 if (unlikely(ftrace_disabled
))
2585 * If an lseek was done, then reset and start from beginning.
2587 if (*pos
< iter
->pos
)
2588 reset_iter_read(iter
);
2591 * For set_ftrace_filter reading, if we have the filter
2592 * off, we can short cut and just print out that all
2593 * functions are enabled.
2595 if (iter
->flags
& FTRACE_ITER_FILTER
&&
2596 ftrace_hash_empty(ops
->filter_hash
)) {
2598 return t_hash_start(m
, pos
);
2599 iter
->flags
|= FTRACE_ITER_PRINTALL
;
2600 /* reset in case of seek/pread */
2601 iter
->flags
&= ~FTRACE_ITER_HASH
;
2605 if (iter
->flags
& FTRACE_ITER_HASH
)
2606 return t_hash_start(m
, pos
);
2609 * Unfortunately, we need to restart at ftrace_pages_start
2610 * every time we let go of the ftrace_mutex. This is because
2611 * those pointers can change without the lock.
2613 iter
->pg
= ftrace_pages_start
;
2615 for (l
= 0; l
<= *pos
; ) {
2616 p
= t_next(m
, p
, &l
);
2622 return t_hash_start(m
, pos
);
2627 static void t_stop(struct seq_file
*m
, void *p
)
2629 mutex_unlock(&ftrace_lock
);
2632 static int t_show(struct seq_file
*m
, void *v
)
2634 struct ftrace_iterator
*iter
= m
->private;
2635 struct dyn_ftrace
*rec
;
2637 if (iter
->flags
& FTRACE_ITER_HASH
)
2638 return t_hash_show(m
, iter
);
2640 if (iter
->flags
& FTRACE_ITER_PRINTALL
) {
2641 seq_printf(m
, "#### all functions enabled ####\n");
2650 seq_printf(m
, "%ps", (void *)rec
->ip
);
2651 if (iter
->flags
& FTRACE_ITER_ENABLED
)
2652 seq_printf(m
, " (%ld)%s",
2653 rec
->flags
& ~FTRACE_FL_MASK
,
2654 rec
->flags
& FTRACE_FL_REGS
? " R" : "");
2655 seq_printf(m
, "\n");
2660 static const struct seq_operations show_ftrace_seq_ops
= {
2668 ftrace_avail_open(struct inode
*inode
, struct file
*file
)
2670 struct ftrace_iterator
*iter
;
2672 if (unlikely(ftrace_disabled
))
2675 iter
= __seq_open_private(file
, &show_ftrace_seq_ops
, sizeof(*iter
));
2677 iter
->pg
= ftrace_pages_start
;
2678 iter
->ops
= &global_ops
;
2681 return iter
? 0 : -ENOMEM
;
2685 ftrace_enabled_open(struct inode
*inode
, struct file
*file
)
2687 struct ftrace_iterator
*iter
;
2689 if (unlikely(ftrace_disabled
))
2692 iter
= __seq_open_private(file
, &show_ftrace_seq_ops
, sizeof(*iter
));
2694 iter
->pg
= ftrace_pages_start
;
2695 iter
->flags
= FTRACE_ITER_ENABLED
;
2696 iter
->ops
= &global_ops
;
2699 return iter
? 0 : -ENOMEM
;
2702 static void ftrace_filter_reset(struct ftrace_hash
*hash
)
2704 mutex_lock(&ftrace_lock
);
2705 ftrace_hash_clear(hash
);
2706 mutex_unlock(&ftrace_lock
);
2710 * ftrace_regex_open - initialize function tracer filter files
2711 * @ops: The ftrace_ops that hold the hash filters
2712 * @flag: The type of filter to process
2713 * @inode: The inode, usually passed in to your open routine
2714 * @file: The file, usually passed in to your open routine
2716 * ftrace_regex_open() initializes the filter files for the
2717 * @ops. Depending on @flag it may process the filter hash or
2718 * the notrace hash of @ops. With this called from the open
2719 * routine, you can use ftrace_filter_write() for the write
2720 * routine if @flag has FTRACE_ITER_FILTER set, or
2721 * ftrace_notrace_write() if @flag has FTRACE_ITER_NOTRACE set.
2722 * ftrace_filter_lseek() should be used as the lseek routine, and
2723 * release must call ftrace_regex_release().
2726 ftrace_regex_open(struct ftrace_ops
*ops
, int flag
,
2727 struct inode
*inode
, struct file
*file
)
2729 struct ftrace_iterator
*iter
;
2730 struct ftrace_hash
*hash
;
2733 ftrace_ops_init(ops
);
2735 if (unlikely(ftrace_disabled
))
2738 iter
= kzalloc(sizeof(*iter
), GFP_KERNEL
);
2742 if (trace_parser_get_init(&iter
->parser
, FTRACE_BUFF_MAX
)) {
2750 mutex_lock(&ops
->regex_lock
);
2752 if (flag
& FTRACE_ITER_NOTRACE
)
2753 hash
= ops
->notrace_hash
;
2755 hash
= ops
->filter_hash
;
2757 if (file
->f_mode
& FMODE_WRITE
) {
2758 iter
->hash
= alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS
, hash
);
2760 trace_parser_put(&iter
->parser
);
2767 if ((file
->f_mode
& FMODE_WRITE
) &&
2768 (file
->f_flags
& O_TRUNC
))
2769 ftrace_filter_reset(iter
->hash
);
2771 if (file
->f_mode
& FMODE_READ
) {
2772 iter
->pg
= ftrace_pages_start
;
2774 ret
= seq_open(file
, &show_ftrace_seq_ops
);
2776 struct seq_file
*m
= file
->private_data
;
2780 free_ftrace_hash(iter
->hash
);
2781 trace_parser_put(&iter
->parser
);
2785 file
->private_data
= iter
;
2788 mutex_unlock(&ops
->regex_lock
);
2794 ftrace_filter_open(struct inode
*inode
, struct file
*file
)
2796 return ftrace_regex_open(&global_ops
,
2797 FTRACE_ITER_FILTER
| FTRACE_ITER_DO_HASH
,
2802 ftrace_notrace_open(struct inode
*inode
, struct file
*file
)
2804 return ftrace_regex_open(&global_ops
, FTRACE_ITER_NOTRACE
,
2808 static int ftrace_match(char *str
, char *regex
, int len
, int type
)
2815 if (strcmp(str
, regex
) == 0)
2818 case MATCH_FRONT_ONLY
:
2819 if (strncmp(str
, regex
, len
) == 0)
2822 case MATCH_MIDDLE_ONLY
:
2823 if (strstr(str
, regex
))
2826 case MATCH_END_ONLY
:
2828 if (slen
>= len
&& memcmp(str
+ slen
- len
, regex
, len
) == 0)
2837 enter_record(struct ftrace_hash
*hash
, struct dyn_ftrace
*rec
, int not)
2839 struct ftrace_func_entry
*entry
;
2842 entry
= ftrace_lookup_ip(hash
, rec
->ip
);
2844 /* Do nothing if it doesn't exist */
2848 free_hash_entry(hash
, entry
);
2850 /* Do nothing if it exists */
2854 ret
= add_hash_entry(hash
, rec
->ip
);
2860 ftrace_match_record(struct dyn_ftrace
*rec
, char *mod
,
2861 char *regex
, int len
, int type
)
2863 char str
[KSYM_SYMBOL_LEN
];
2866 kallsyms_lookup(rec
->ip
, NULL
, NULL
, &modname
, str
);
2869 /* module lookup requires matching the module */
2870 if (!modname
|| strcmp(modname
, mod
))
2873 /* blank search means to match all funcs in the mod */
2878 return ftrace_match(str
, regex
, len
, type
);
2882 match_records(struct ftrace_hash
*hash
, char *buff
,
2883 int len
, char *mod
, int not)
2885 unsigned search_len
= 0;
2886 struct ftrace_page
*pg
;
2887 struct dyn_ftrace
*rec
;
2888 int type
= MATCH_FULL
;
2889 char *search
= buff
;
2894 type
= filter_parse_regex(buff
, len
, &search
, ¬);
2895 search_len
= strlen(search
);
2898 mutex_lock(&ftrace_lock
);
2900 if (unlikely(ftrace_disabled
))
2903 do_for_each_ftrace_rec(pg
, rec
) {
2904 if (ftrace_match_record(rec
, mod
, search
, search_len
, type
)) {
2905 ret
= enter_record(hash
, rec
, not);
2912 } while_for_each_ftrace_rec();
2914 mutex_unlock(&ftrace_lock
);
2920 ftrace_match_records(struct ftrace_hash
*hash
, char *buff
, int len
)
2922 return match_records(hash
, buff
, len
, NULL
, 0);
2926 ftrace_match_module_records(struct ftrace_hash
*hash
, char *buff
, char *mod
)
2930 /* blank or '*' mean the same */
2931 if (strcmp(buff
, "*") == 0)
2934 /* handle the case of 'dont filter this module' */
2935 if (strcmp(buff
, "!") == 0 || strcmp(buff
, "!*") == 0) {
2940 return match_records(hash
, buff
, strlen(buff
), mod
, not);
2944 * We register the module command as a template to show others how
2945 * to register the a command as well.
2949 ftrace_mod_callback(struct ftrace_hash
*hash
,
2950 char *func
, char *cmd
, char *param
, int enable
)
2956 * cmd == 'mod' because we only registered this func
2957 * for the 'mod' ftrace_func_command.
2958 * But if you register one func with multiple commands,
2959 * you can tell which command was used by the cmd
2963 /* we must have a module name */
2967 mod
= strsep(¶m
, ":");
2971 ret
= ftrace_match_module_records(hash
, func
, mod
);
2980 static struct ftrace_func_command ftrace_mod_cmd
= {
2982 .func
= ftrace_mod_callback
,
2985 static int __init
ftrace_mod_cmd_init(void)
2987 return register_ftrace_command(&ftrace_mod_cmd
);
2989 core_initcall(ftrace_mod_cmd_init
);
2991 static void function_trace_probe_call(unsigned long ip
, unsigned long parent_ip
,
2992 struct ftrace_ops
*op
, struct pt_regs
*pt_regs
)
2994 struct ftrace_func_probe
*entry
;
2995 struct hlist_head
*hhd
;
2998 key
= hash_long(ip
, FTRACE_HASH_BITS
);
3000 hhd
= &ftrace_func_hash
[key
];
3002 if (hlist_empty(hhd
))
3006 * Disable preemption for these calls to prevent a RCU grace
3007 * period. This syncs the hash iteration and freeing of items
3008 * on the hash. rcu_read_lock is too dangerous here.
3010 preempt_disable_notrace();
3011 hlist_for_each_entry_rcu_notrace(entry
, hhd
, node
) {
3012 if (entry
->ip
== ip
)
3013 entry
->ops
->func(ip
, parent_ip
, &entry
->data
);
3015 preempt_enable_notrace();
3018 static struct ftrace_ops trace_probe_ops __read_mostly
=
3020 .func
= function_trace_probe_call
,
3021 .flags
= FTRACE_OPS_FL_INITIALIZED
,
3022 INIT_REGEX_LOCK(trace_probe_ops
)
3025 static int ftrace_probe_registered
;
3027 static void __enable_ftrace_function_probe(void)
3032 if (ftrace_probe_registered
) {
3033 /* still need to update the function call sites */
3035 ftrace_run_update_code(FTRACE_UPDATE_CALLS
);
3039 for (i
= 0; i
< FTRACE_FUNC_HASHSIZE
; i
++) {
3040 struct hlist_head
*hhd
= &ftrace_func_hash
[i
];
3044 /* Nothing registered? */
3045 if (i
== FTRACE_FUNC_HASHSIZE
)
3048 ret
= __register_ftrace_function(&trace_probe_ops
);
3050 ret
= ftrace_startup(&trace_probe_ops
, 0);
3052 ftrace_probe_registered
= 1;
3055 static void __disable_ftrace_function_probe(void)
3060 if (!ftrace_probe_registered
)
3063 for (i
= 0; i
< FTRACE_FUNC_HASHSIZE
; i
++) {
3064 struct hlist_head
*hhd
= &ftrace_func_hash
[i
];
3069 /* no more funcs left */
3070 ret
= __unregister_ftrace_function(&trace_probe_ops
);
3072 ftrace_shutdown(&trace_probe_ops
, 0);
3074 ftrace_probe_registered
= 0;
3078 static void ftrace_free_entry(struct ftrace_func_probe
*entry
)
3080 if (entry
->ops
->free
)
3081 entry
->ops
->free(entry
->ops
, entry
->ip
, &entry
->data
);
3086 register_ftrace_function_probe(char *glob
, struct ftrace_probe_ops
*ops
,
3089 struct ftrace_func_probe
*entry
;
3090 struct ftrace_hash
**orig_hash
= &trace_probe_ops
.filter_hash
;
3091 struct ftrace_hash
*hash
;
3092 struct ftrace_page
*pg
;
3093 struct dyn_ftrace
*rec
;
3100 type
= filter_parse_regex(glob
, strlen(glob
), &search
, ¬);
3101 len
= strlen(search
);
3103 /* we do not support '!' for function probes */
3107 mutex_lock(&trace_probe_ops
.regex_lock
);
3109 hash
= alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS
, *orig_hash
);
3115 if (unlikely(ftrace_disabled
)) {
3120 mutex_lock(&ftrace_lock
);
3122 do_for_each_ftrace_rec(pg
, rec
) {
3124 if (!ftrace_match_record(rec
, NULL
, search
, len
, type
))
3127 entry
= kmalloc(sizeof(*entry
), GFP_KERNEL
);
3129 /* If we did not process any, then return error */
3140 * The caller might want to do something special
3141 * for each function we find. We call the callback
3142 * to give the caller an opportunity to do so.
3145 if (ops
->init(ops
, rec
->ip
, &entry
->data
) < 0) {
3146 /* caller does not like this func */
3152 ret
= enter_record(hash
, rec
, 0);
3160 entry
->ip
= rec
->ip
;
3162 key
= hash_long(entry
->ip
, FTRACE_HASH_BITS
);
3163 hlist_add_head_rcu(&entry
->node
, &ftrace_func_hash
[key
]);
3165 } while_for_each_ftrace_rec();
3167 ret
= ftrace_hash_move(&trace_probe_ops
, 1, orig_hash
, hash
);
3171 __enable_ftrace_function_probe();
3174 mutex_unlock(&ftrace_lock
);
3176 mutex_unlock(&trace_probe_ops
.regex_lock
);
3177 free_ftrace_hash(hash
);
3183 PROBE_TEST_FUNC
= 1,
3188 __unregister_ftrace_function_probe(char *glob
, struct ftrace_probe_ops
*ops
,
3189 void *data
, int flags
)
3191 struct ftrace_func_entry
*rec_entry
;
3192 struct ftrace_func_probe
*entry
;
3193 struct ftrace_func_probe
*p
;
3194 struct ftrace_hash
**orig_hash
= &trace_probe_ops
.filter_hash
;
3195 struct list_head free_list
;
3196 struct ftrace_hash
*hash
;
3197 struct hlist_node
*tmp
;
3198 char str
[KSYM_SYMBOL_LEN
];
3199 int type
= MATCH_FULL
;
3203 if (glob
&& (strcmp(glob
, "*") == 0 || !strlen(glob
)))
3208 type
= filter_parse_regex(glob
, strlen(glob
), &search
, ¬);
3209 len
= strlen(search
);
3211 /* we do not support '!' for function probes */
3216 mutex_lock(&trace_probe_ops
.regex_lock
);
3218 hash
= alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS
, *orig_hash
);
3220 /* Hmm, should report this somehow */
3223 INIT_LIST_HEAD(&free_list
);
3225 for (i
= 0; i
< FTRACE_FUNC_HASHSIZE
; i
++) {
3226 struct hlist_head
*hhd
= &ftrace_func_hash
[i
];
3228 hlist_for_each_entry_safe(entry
, tmp
, hhd
, node
) {
3230 /* break up if statements for readability */
3231 if ((flags
& PROBE_TEST_FUNC
) && entry
->ops
!= ops
)
3234 if ((flags
& PROBE_TEST_DATA
) && entry
->data
!= data
)
3237 /* do this last, since it is the most expensive */
3239 kallsyms_lookup(entry
->ip
, NULL
, NULL
,
3241 if (!ftrace_match(str
, glob
, len
, type
))
3245 rec_entry
= ftrace_lookup_ip(hash
, entry
->ip
);
3246 /* It is possible more than one entry had this ip */
3248 free_hash_entry(hash
, rec_entry
);
3250 hlist_del_rcu(&entry
->node
);
3251 list_add(&entry
->free_list
, &free_list
);
3254 mutex_lock(&ftrace_lock
);
3255 __disable_ftrace_function_probe();
3257 * Remove after the disable is called. Otherwise, if the last
3258 * probe is removed, a null hash means *all enabled*.
3260 ftrace_hash_move(&trace_probe_ops
, 1, orig_hash
, hash
);
3261 synchronize_sched();
3262 list_for_each_entry_safe(entry
, p
, &free_list
, free_list
) {
3263 list_del(&entry
->free_list
);
3264 ftrace_free_entry(entry
);
3266 mutex_unlock(&ftrace_lock
);
3269 mutex_unlock(&trace_probe_ops
.regex_lock
);
3270 free_ftrace_hash(hash
);
3274 unregister_ftrace_function_probe(char *glob
, struct ftrace_probe_ops
*ops
,
3277 __unregister_ftrace_function_probe(glob
, ops
, data
,
3278 PROBE_TEST_FUNC
| PROBE_TEST_DATA
);
3282 unregister_ftrace_function_probe_func(char *glob
, struct ftrace_probe_ops
*ops
)
3284 __unregister_ftrace_function_probe(glob
, ops
, NULL
, PROBE_TEST_FUNC
);
3287 void unregister_ftrace_function_probe_all(char *glob
)
3289 __unregister_ftrace_function_probe(glob
, NULL
, NULL
, 0);
3292 static LIST_HEAD(ftrace_commands
);
3293 static DEFINE_MUTEX(ftrace_cmd_mutex
);
3295 int register_ftrace_command(struct ftrace_func_command
*cmd
)
3297 struct ftrace_func_command
*p
;
3300 mutex_lock(&ftrace_cmd_mutex
);
3301 list_for_each_entry(p
, &ftrace_commands
, list
) {
3302 if (strcmp(cmd
->name
, p
->name
) == 0) {
3307 list_add(&cmd
->list
, &ftrace_commands
);
3309 mutex_unlock(&ftrace_cmd_mutex
);
3314 int unregister_ftrace_command(struct ftrace_func_command
*cmd
)
3316 struct ftrace_func_command
*p
, *n
;
3319 mutex_lock(&ftrace_cmd_mutex
);
3320 list_for_each_entry_safe(p
, n
, &ftrace_commands
, list
) {
3321 if (strcmp(cmd
->name
, p
->name
) == 0) {
3323 list_del_init(&p
->list
);
3328 mutex_unlock(&ftrace_cmd_mutex
);
3333 static int ftrace_process_regex(struct ftrace_hash
*hash
,
3334 char *buff
, int len
, int enable
)
3336 char *func
, *command
, *next
= buff
;
3337 struct ftrace_func_command
*p
;
3340 func
= strsep(&next
, ":");
3343 ret
= ftrace_match_records(hash
, func
, len
);
3353 command
= strsep(&next
, ":");
3355 mutex_lock(&ftrace_cmd_mutex
);
3356 list_for_each_entry(p
, &ftrace_commands
, list
) {
3357 if (strcmp(p
->name
, command
) == 0) {
3358 ret
= p
->func(hash
, func
, command
, next
, enable
);
3363 mutex_unlock(&ftrace_cmd_mutex
);
3369 ftrace_regex_write(struct file
*file
, const char __user
*ubuf
,
3370 size_t cnt
, loff_t
*ppos
, int enable
)
3372 struct ftrace_iterator
*iter
;
3373 struct trace_parser
*parser
;
3379 if (file
->f_mode
& FMODE_READ
) {
3380 struct seq_file
*m
= file
->private_data
;
3383 iter
= file
->private_data
;
3385 if (unlikely(ftrace_disabled
))
3388 /* iter->hash is a local copy, so we don't need regex_lock */
3390 parser
= &iter
->parser
;
3391 read
= trace_get_user(parser
, ubuf
, cnt
, ppos
);
3393 if (read
>= 0 && trace_parser_loaded(parser
) &&
3394 !trace_parser_cont(parser
)) {
3395 ret
= ftrace_process_regex(iter
->hash
, parser
->buffer
,
3396 parser
->idx
, enable
);
3397 trace_parser_clear(parser
);
3408 ftrace_filter_write(struct file
*file
, const char __user
*ubuf
,
3409 size_t cnt
, loff_t
*ppos
)
3411 return ftrace_regex_write(file
, ubuf
, cnt
, ppos
, 1);
3415 ftrace_notrace_write(struct file
*file
, const char __user
*ubuf
,
3416 size_t cnt
, loff_t
*ppos
)
3418 return ftrace_regex_write(file
, ubuf
, cnt
, ppos
, 0);
3422 ftrace_match_addr(struct ftrace_hash
*hash
, unsigned long ip
, int remove
)
3424 struct ftrace_func_entry
*entry
;
3426 if (!ftrace_location(ip
))
3430 entry
= ftrace_lookup_ip(hash
, ip
);
3433 free_hash_entry(hash
, entry
);
3437 return add_hash_entry(hash
, ip
);
3440 static void ftrace_ops_update_code(struct ftrace_ops
*ops
)
3442 if (ops
->flags
& FTRACE_OPS_FL_ENABLED
&& ftrace_enabled
)
3443 ftrace_run_update_code(FTRACE_UPDATE_CALLS
);
3447 ftrace_set_hash(struct ftrace_ops
*ops
, unsigned char *buf
, int len
,
3448 unsigned long ip
, int remove
, int reset
, int enable
)
3450 struct ftrace_hash
**orig_hash
;
3451 struct ftrace_hash
*hash
;
3454 /* All global ops uses the global ops filters */
3455 if (ops
->flags
& FTRACE_OPS_FL_GLOBAL
)
3458 if (unlikely(ftrace_disabled
))
3461 mutex_lock(&ops
->regex_lock
);
3464 orig_hash
= &ops
->filter_hash
;
3466 orig_hash
= &ops
->notrace_hash
;
3468 hash
= alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS
, *orig_hash
);
3471 goto out_regex_unlock
;
3475 ftrace_filter_reset(hash
);
3476 if (buf
&& !ftrace_match_records(hash
, buf
, len
)) {
3478 goto out_regex_unlock
;
3481 ret
= ftrace_match_addr(hash
, ip
, remove
);
3483 goto out_regex_unlock
;
3486 mutex_lock(&ftrace_lock
);
3487 ret
= ftrace_hash_move(ops
, enable
, orig_hash
, hash
);
3489 ftrace_ops_update_code(ops
);
3491 mutex_unlock(&ftrace_lock
);
3494 mutex_unlock(&ops
->regex_lock
);
3496 free_ftrace_hash(hash
);
3501 ftrace_set_addr(struct ftrace_ops
*ops
, unsigned long ip
, int remove
,
3502 int reset
, int enable
)
3504 return ftrace_set_hash(ops
, 0, 0, ip
, remove
, reset
, enable
);
3508 * ftrace_set_filter_ip - set a function to filter on in ftrace by address
3509 * @ops - the ops to set the filter with
3510 * @ip - the address to add to or remove from the filter.
3511 * @remove - non zero to remove the ip from the filter
3512 * @reset - non zero to reset all filters before applying this filter.
3514 * Filters denote which functions should be enabled when tracing is enabled
3515 * If @ip is NULL, it failes to update filter.
3517 int ftrace_set_filter_ip(struct ftrace_ops
*ops
, unsigned long ip
,
3518 int remove
, int reset
)
3520 ftrace_ops_init(ops
);
3521 return ftrace_set_addr(ops
, ip
, remove
, reset
, 1);
3523 EXPORT_SYMBOL_GPL(ftrace_set_filter_ip
);
3526 ftrace_set_regex(struct ftrace_ops
*ops
, unsigned char *buf
, int len
,
3527 int reset
, int enable
)
3529 return ftrace_set_hash(ops
, buf
, len
, 0, 0, reset
, enable
);
3533 * ftrace_set_filter - set a function to filter on in ftrace
3534 * @ops - the ops to set the filter with
3535 * @buf - the string that holds the function filter text.
3536 * @len - the length of the string.
3537 * @reset - non zero to reset all filters before applying this filter.
3539 * Filters denote which functions should be enabled when tracing is enabled.
3540 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
3542 int ftrace_set_filter(struct ftrace_ops
*ops
, unsigned char *buf
,
3545 ftrace_ops_init(ops
);
3546 return ftrace_set_regex(ops
, buf
, len
, reset
, 1);
3548 EXPORT_SYMBOL_GPL(ftrace_set_filter
);
3551 * ftrace_set_notrace - set a function to not trace in ftrace
3552 * @ops - the ops to set the notrace filter with
3553 * @buf - the string that holds the function notrace text.
3554 * @len - the length of the string.
3555 * @reset - non zero to reset all filters before applying this filter.
3557 * Notrace Filters denote which functions should not be enabled when tracing
3558 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
3561 int ftrace_set_notrace(struct ftrace_ops
*ops
, unsigned char *buf
,
3564 ftrace_ops_init(ops
);
3565 return ftrace_set_regex(ops
, buf
, len
, reset
, 0);
3567 EXPORT_SYMBOL_GPL(ftrace_set_notrace
);
3569 * ftrace_set_filter - set a function to filter on in ftrace
3570 * @ops - the ops to set the filter with
3571 * @buf - the string that holds the function filter text.
3572 * @len - the length of the string.
3573 * @reset - non zero to reset all filters before applying this filter.
3575 * Filters denote which functions should be enabled when tracing is enabled.
3576 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
3578 void ftrace_set_global_filter(unsigned char *buf
, int len
, int reset
)
3580 ftrace_set_regex(&global_ops
, buf
, len
, reset
, 1);
3582 EXPORT_SYMBOL_GPL(ftrace_set_global_filter
);
3585 * ftrace_set_notrace - set a function to not trace in ftrace
3586 * @ops - the ops to set the notrace filter with
3587 * @buf - the string that holds the function notrace text.
3588 * @len - the length of the string.
3589 * @reset - non zero to reset all filters before applying this filter.
3591 * Notrace Filters denote which functions should not be enabled when tracing
3592 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
3595 void ftrace_set_global_notrace(unsigned char *buf
, int len
, int reset
)
3597 ftrace_set_regex(&global_ops
, buf
, len
, reset
, 0);
3599 EXPORT_SYMBOL_GPL(ftrace_set_global_notrace
);
3602 * command line interface to allow users to set filters on boot up.
3604 #define FTRACE_FILTER_SIZE COMMAND_LINE_SIZE
3605 static char ftrace_notrace_buf
[FTRACE_FILTER_SIZE
] __initdata
;
3606 static char ftrace_filter_buf
[FTRACE_FILTER_SIZE
] __initdata
;
3608 /* Used by function selftest to not test if filter is set */
3609 bool ftrace_filter_param __initdata
;
3611 static int __init
set_ftrace_notrace(char *str
)
3613 ftrace_filter_param
= true;
3614 strlcpy(ftrace_notrace_buf
, str
, FTRACE_FILTER_SIZE
);
3617 __setup("ftrace_notrace=", set_ftrace_notrace
);
3619 static int __init
set_ftrace_filter(char *str
)
3621 ftrace_filter_param
= true;
3622 strlcpy(ftrace_filter_buf
, str
, FTRACE_FILTER_SIZE
);
3625 __setup("ftrace_filter=", set_ftrace_filter
);
3627 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
3628 static char ftrace_graph_buf
[FTRACE_FILTER_SIZE
] __initdata
;
3629 static int ftrace_set_func(unsigned long *array
, int *idx
, char *buffer
);
3631 static int __init
set_graph_function(char *str
)
3633 strlcpy(ftrace_graph_buf
, str
, FTRACE_FILTER_SIZE
);
3636 __setup("ftrace_graph_filter=", set_graph_function
);
3638 static void __init
set_ftrace_early_graph(char *buf
)
3644 func
= strsep(&buf
, ",");
3645 /* we allow only one expression at a time */
3646 ret
= ftrace_set_func(ftrace_graph_funcs
, &ftrace_graph_count
,
3649 printk(KERN_DEBUG
"ftrace: function %s not "
3650 "traceable\n", func
);
3653 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
3656 ftrace_set_early_filter(struct ftrace_ops
*ops
, char *buf
, int enable
)
3660 ftrace_ops_init(ops
);
3663 func
= strsep(&buf
, ",");
3664 ftrace_set_regex(ops
, func
, strlen(func
), 0, enable
);
3668 static void __init
set_ftrace_early_filters(void)
3670 if (ftrace_filter_buf
[0])
3671 ftrace_set_early_filter(&global_ops
, ftrace_filter_buf
, 1);
3672 if (ftrace_notrace_buf
[0])
3673 ftrace_set_early_filter(&global_ops
, ftrace_notrace_buf
, 0);
3674 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
3675 if (ftrace_graph_buf
[0])
3676 set_ftrace_early_graph(ftrace_graph_buf
);
3677 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
3680 int ftrace_regex_release(struct inode
*inode
, struct file
*file
)
3682 struct seq_file
*m
= (struct seq_file
*)file
->private_data
;
3683 struct ftrace_iterator
*iter
;
3684 struct ftrace_hash
**orig_hash
;
3685 struct trace_parser
*parser
;
3689 if (file
->f_mode
& FMODE_READ
) {
3691 seq_release(inode
, file
);
3693 iter
= file
->private_data
;
3695 parser
= &iter
->parser
;
3696 if (trace_parser_loaded(parser
)) {
3697 parser
->buffer
[parser
->idx
] = 0;
3698 ftrace_match_records(iter
->hash
, parser
->buffer
, parser
->idx
);
3701 trace_parser_put(parser
);
3703 mutex_lock(&iter
->ops
->regex_lock
);
3705 if (file
->f_mode
& FMODE_WRITE
) {
3706 filter_hash
= !!(iter
->flags
& FTRACE_ITER_FILTER
);
3709 orig_hash
= &iter
->ops
->filter_hash
;
3711 orig_hash
= &iter
->ops
->notrace_hash
;
3713 mutex_lock(&ftrace_lock
);
3714 ret
= ftrace_hash_move(iter
->ops
, filter_hash
,
3715 orig_hash
, iter
->hash
);
3717 ftrace_ops_update_code(iter
->ops
);
3719 mutex_unlock(&ftrace_lock
);
3722 mutex_unlock(&iter
->ops
->regex_lock
);
3723 free_ftrace_hash(iter
->hash
);
3729 static const struct file_operations ftrace_avail_fops
= {
3730 .open
= ftrace_avail_open
,
3732 .llseek
= seq_lseek
,
3733 .release
= seq_release_private
,
3736 static const struct file_operations ftrace_enabled_fops
= {
3737 .open
= ftrace_enabled_open
,
3739 .llseek
= seq_lseek
,
3740 .release
= seq_release_private
,
3743 static const struct file_operations ftrace_filter_fops
= {
3744 .open
= ftrace_filter_open
,
3746 .write
= ftrace_filter_write
,
3747 .llseek
= ftrace_filter_lseek
,
3748 .release
= ftrace_regex_release
,
3751 static const struct file_operations ftrace_notrace_fops
= {
3752 .open
= ftrace_notrace_open
,
3754 .write
= ftrace_notrace_write
,
3755 .llseek
= ftrace_filter_lseek
,
3756 .release
= ftrace_regex_release
,
3759 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
3761 static DEFINE_MUTEX(graph_lock
);
3763 int ftrace_graph_count
;
3764 int ftrace_graph_filter_enabled
;
3765 unsigned long ftrace_graph_funcs
[FTRACE_GRAPH_MAX_FUNCS
] __read_mostly
;
3768 __g_next(struct seq_file
*m
, loff_t
*pos
)
3770 if (*pos
>= ftrace_graph_count
)
3772 return &ftrace_graph_funcs
[*pos
];
3776 g_next(struct seq_file
*m
, void *v
, loff_t
*pos
)
3779 return __g_next(m
, pos
);
3782 static void *g_start(struct seq_file
*m
, loff_t
*pos
)
3784 mutex_lock(&graph_lock
);
3786 /* Nothing, tell g_show to print all functions are enabled */
3787 if (!ftrace_graph_filter_enabled
&& !*pos
)
3790 return __g_next(m
, pos
);
3793 static void g_stop(struct seq_file
*m
, void *p
)
3795 mutex_unlock(&graph_lock
);
3798 static int g_show(struct seq_file
*m
, void *v
)
3800 unsigned long *ptr
= v
;
3805 if (ptr
== (unsigned long *)1) {
3806 seq_printf(m
, "#### all functions enabled ####\n");
3810 seq_printf(m
, "%ps\n", (void *)*ptr
);
3815 static const struct seq_operations ftrace_graph_seq_ops
= {
3823 ftrace_graph_open(struct inode
*inode
, struct file
*file
)
3827 if (unlikely(ftrace_disabled
))
3830 mutex_lock(&graph_lock
);
3831 if ((file
->f_mode
& FMODE_WRITE
) &&
3832 (file
->f_flags
& O_TRUNC
)) {
3833 ftrace_graph_filter_enabled
= 0;
3834 ftrace_graph_count
= 0;
3835 memset(ftrace_graph_funcs
, 0, sizeof(ftrace_graph_funcs
));
3837 mutex_unlock(&graph_lock
);
3839 if (file
->f_mode
& FMODE_READ
)
3840 ret
= seq_open(file
, &ftrace_graph_seq_ops
);
3846 ftrace_graph_release(struct inode
*inode
, struct file
*file
)
3848 if (file
->f_mode
& FMODE_READ
)
3849 seq_release(inode
, file
);
3854 ftrace_set_func(unsigned long *array
, int *idx
, char *buffer
)
3856 struct dyn_ftrace
*rec
;
3857 struct ftrace_page
*pg
;
3866 type
= filter_parse_regex(buffer
, strlen(buffer
), &search
, ¬);
3867 if (!not && *idx
>= FTRACE_GRAPH_MAX_FUNCS
)
3870 search_len
= strlen(search
);
3872 mutex_lock(&ftrace_lock
);
3874 if (unlikely(ftrace_disabled
)) {
3875 mutex_unlock(&ftrace_lock
);
3879 do_for_each_ftrace_rec(pg
, rec
) {
3881 if (ftrace_match_record(rec
, NULL
, search
, search_len
, type
)) {
3882 /* if it is in the array */
3884 for (i
= 0; i
< *idx
; i
++) {
3885 if (array
[i
] == rec
->ip
) {
3894 array
[(*idx
)++] = rec
->ip
;
3895 if (*idx
>= FTRACE_GRAPH_MAX_FUNCS
)
3900 array
[i
] = array
[--(*idx
)];
3906 } while_for_each_ftrace_rec();
3908 mutex_unlock(&ftrace_lock
);
3913 ftrace_graph_filter_enabled
= !!(*idx
);
3919 ftrace_graph_write(struct file
*file
, const char __user
*ubuf
,
3920 size_t cnt
, loff_t
*ppos
)
3922 struct trace_parser parser
;
3928 mutex_lock(&graph_lock
);
3930 if (trace_parser_get_init(&parser
, FTRACE_BUFF_MAX
)) {
3935 read
= trace_get_user(&parser
, ubuf
, cnt
, ppos
);
3937 if (read
>= 0 && trace_parser_loaded((&parser
))) {
3938 parser
.buffer
[parser
.idx
] = 0;
3940 /* we allow only one expression at a time */
3941 ret
= ftrace_set_func(ftrace_graph_funcs
, &ftrace_graph_count
,
3950 trace_parser_put(&parser
);
3952 mutex_unlock(&graph_lock
);
3957 static const struct file_operations ftrace_graph_fops
= {
3958 .open
= ftrace_graph_open
,
3960 .write
= ftrace_graph_write
,
3961 .llseek
= ftrace_filter_lseek
,
3962 .release
= ftrace_graph_release
,
3964 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
3966 static __init
int ftrace_init_dyn_debugfs(struct dentry
*d_tracer
)
3969 trace_create_file("available_filter_functions", 0444,
3970 d_tracer
, NULL
, &ftrace_avail_fops
);
3972 trace_create_file("enabled_functions", 0444,
3973 d_tracer
, NULL
, &ftrace_enabled_fops
);
3975 trace_create_file("set_ftrace_filter", 0644, d_tracer
,
3976 NULL
, &ftrace_filter_fops
);
3978 trace_create_file("set_ftrace_notrace", 0644, d_tracer
,
3979 NULL
, &ftrace_notrace_fops
);
3981 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
3982 trace_create_file("set_graph_function", 0444, d_tracer
,
3984 &ftrace_graph_fops
);
3985 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
3990 static int ftrace_cmp_ips(const void *a
, const void *b
)
3992 const unsigned long *ipa
= a
;
3993 const unsigned long *ipb
= b
;
4002 static void ftrace_swap_ips(void *a
, void *b
, int size
)
4004 unsigned long *ipa
= a
;
4005 unsigned long *ipb
= b
;
4013 static int ftrace_process_locs(struct module
*mod
,
4014 unsigned long *start
,
4017 struct ftrace_page
*start_pg
;
4018 struct ftrace_page
*pg
;
4019 struct dyn_ftrace
*rec
;
4020 unsigned long count
;
4023 unsigned long flags
= 0; /* Shut up gcc */
4026 count
= end
- start
;
4031 sort(start
, count
, sizeof(*start
),
4032 ftrace_cmp_ips
, ftrace_swap_ips
);
4034 start_pg
= ftrace_allocate_pages(count
);
4038 mutex_lock(&ftrace_lock
);
4041 * Core and each module needs their own pages, as
4042 * modules will free them when they are removed.
4043 * Force a new page to be allocated for modules.
4046 WARN_ON(ftrace_pages
|| ftrace_pages_start
);
4047 /* First initialization */
4048 ftrace_pages
= ftrace_pages_start
= start_pg
;
4053 if (WARN_ON(ftrace_pages
->next
)) {
4054 /* Hmm, we have free pages? */
4055 while (ftrace_pages
->next
)
4056 ftrace_pages
= ftrace_pages
->next
;
4059 ftrace_pages
->next
= start_pg
;
4065 addr
= ftrace_call_adjust(*p
++);
4067 * Some architecture linkers will pad between
4068 * the different mcount_loc sections of different
4069 * object files to satisfy alignments.
4070 * Skip any NULL pointers.
4075 if (pg
->index
== pg
->size
) {
4076 /* We should have allocated enough */
4077 if (WARN_ON(!pg
->next
))
4082 rec
= &pg
->records
[pg
->index
++];
4086 /* We should have used all pages */
4089 /* Assign the last page to ftrace_pages */
4092 /* These new locations need to be initialized */
4093 ftrace_new_pgs
= start_pg
;
4096 * We only need to disable interrupts on start up
4097 * because we are modifying code that an interrupt
4098 * may execute, and the modification is not atomic.
4099 * But for modules, nothing runs the code we modify
4100 * until we are finished with it, and there's no
4101 * reason to cause large interrupt latencies while we do it.
4104 local_irq_save(flags
);
4105 ftrace_update_code(mod
);
4107 local_irq_restore(flags
);
4110 mutex_unlock(&ftrace_lock
);
4115 #ifdef CONFIG_MODULES
4117 #define next_to_ftrace_page(p) container_of(p, struct ftrace_page, next)
4119 void ftrace_release_mod(struct module
*mod
)
4121 struct dyn_ftrace
*rec
;
4122 struct ftrace_page
**last_pg
;
4123 struct ftrace_page
*pg
;
4126 mutex_lock(&ftrace_lock
);
4128 if (ftrace_disabled
)
4132 * Each module has its own ftrace_pages, remove
4133 * them from the list.
4135 last_pg
= &ftrace_pages_start
;
4136 for (pg
= ftrace_pages_start
; pg
; pg
= *last_pg
) {
4137 rec
= &pg
->records
[0];
4138 if (within_module_core(rec
->ip
, mod
)) {
4140 * As core pages are first, the first
4141 * page should never be a module page.
4143 if (WARN_ON(pg
== ftrace_pages_start
))
4146 /* Check if we are deleting the last page */
4147 if (pg
== ftrace_pages
)
4148 ftrace_pages
= next_to_ftrace_page(last_pg
);
4150 *last_pg
= pg
->next
;
4151 order
= get_count_order(pg
->size
/ ENTRIES_PER_PAGE
);
4152 free_pages((unsigned long)pg
->records
, order
);
4155 last_pg
= &pg
->next
;
4158 mutex_unlock(&ftrace_lock
);
4161 static void ftrace_init_module(struct module
*mod
,
4162 unsigned long *start
, unsigned long *end
)
4164 if (ftrace_disabled
|| start
== end
)
4166 ftrace_process_locs(mod
, start
, end
);
4169 static int ftrace_module_notify_enter(struct notifier_block
*self
,
4170 unsigned long val
, void *data
)
4172 struct module
*mod
= data
;
4174 if (val
== MODULE_STATE_COMING
)
4175 ftrace_init_module(mod
, mod
->ftrace_callsites
,
4176 mod
->ftrace_callsites
+
4177 mod
->num_ftrace_callsites
);
4181 static int ftrace_module_notify_exit(struct notifier_block
*self
,
4182 unsigned long val
, void *data
)
4184 struct module
*mod
= data
;
4186 if (val
== MODULE_STATE_GOING
)
4187 ftrace_release_mod(mod
);
4192 static int ftrace_module_notify_enter(struct notifier_block
*self
,
4193 unsigned long val
, void *data
)
4197 static int ftrace_module_notify_exit(struct notifier_block
*self
,
4198 unsigned long val
, void *data
)
4202 #endif /* CONFIG_MODULES */
4204 struct notifier_block ftrace_module_enter_nb
= {
4205 .notifier_call
= ftrace_module_notify_enter
,
4206 .priority
= INT_MAX
, /* Run before anything that can use kprobes */
4209 struct notifier_block ftrace_module_exit_nb
= {
4210 .notifier_call
= ftrace_module_notify_exit
,
4211 .priority
= INT_MIN
, /* Run after anything that can remove kprobes */
4214 extern unsigned long __start_mcount_loc
[];
4215 extern unsigned long __stop_mcount_loc
[];
4217 void __init
ftrace_init(void)
4219 unsigned long count
, addr
, flags
;
4222 /* Keep the ftrace pointer to the stub */
4223 addr
= (unsigned long)ftrace_stub
;
4225 local_irq_save(flags
);
4226 ftrace_dyn_arch_init(&addr
);
4227 local_irq_restore(flags
);
4229 /* ftrace_dyn_arch_init places the return code in addr */
4233 count
= __stop_mcount_loc
- __start_mcount_loc
;
4235 ret
= ftrace_dyn_table_alloc(count
);
4239 last_ftrace_enabled
= ftrace_enabled
= 1;
4241 ret
= ftrace_process_locs(NULL
,
4245 ret
= register_module_notifier(&ftrace_module_enter_nb
);
4247 pr_warning("Failed to register trace ftrace module enter notifier\n");
4249 ret
= register_module_notifier(&ftrace_module_exit_nb
);
4251 pr_warning("Failed to register trace ftrace module exit notifier\n");
4253 set_ftrace_early_filters();
4257 ftrace_disabled
= 1;
4262 static struct ftrace_ops global_ops
= {
4263 .func
= ftrace_stub
,
4264 .flags
= FTRACE_OPS_FL_RECURSION_SAFE
| FTRACE_OPS_FL_INITIALIZED
,
4265 INIT_REGEX_LOCK(global_ops
)
4268 static int __init
ftrace_nodyn_init(void)
4273 core_initcall(ftrace_nodyn_init
);
4275 static inline int ftrace_init_dyn_debugfs(struct dentry
*d_tracer
) { return 0; }
4276 static inline void ftrace_startup_enable(int command
) { }
4277 /* Keep as macros so we do not need to define the commands */
4278 # define ftrace_startup(ops, command) \
4280 (ops)->flags |= FTRACE_OPS_FL_ENABLED; \
4283 # define ftrace_shutdown(ops, command) do { } while (0)
4284 # define ftrace_startup_sysctl() do { } while (0)
4285 # define ftrace_shutdown_sysctl() do { } while (0)
4288 ftrace_ops_test(struct ftrace_ops
*ops
, unsigned long ip
, void *regs
)
4293 #endif /* CONFIG_DYNAMIC_FTRACE */
4296 ftrace_ops_control_func(unsigned long ip
, unsigned long parent_ip
,
4297 struct ftrace_ops
*op
, struct pt_regs
*regs
)
4299 if (unlikely(trace_recursion_test(TRACE_CONTROL_BIT
)))
4303 * Some of the ops may be dynamically allocated,
4304 * they must be freed after a synchronize_sched().
4306 preempt_disable_notrace();
4307 trace_recursion_set(TRACE_CONTROL_BIT
);
4308 do_for_each_ftrace_op(op
, ftrace_control_list
) {
4309 if (!(op
->flags
& FTRACE_OPS_FL_STUB
) &&
4310 !ftrace_function_local_disabled(op
) &&
4311 ftrace_ops_test(op
, ip
, regs
))
4312 op
->func(ip
, parent_ip
, op
, regs
);
4313 } while_for_each_ftrace_op(op
);
4314 trace_recursion_clear(TRACE_CONTROL_BIT
);
4315 preempt_enable_notrace();
4318 static struct ftrace_ops control_ops
= {
4319 .func
= ftrace_ops_control_func
,
4320 .flags
= FTRACE_OPS_FL_RECURSION_SAFE
| FTRACE_OPS_FL_INITIALIZED
,
4321 INIT_REGEX_LOCK(control_ops
)
4325 __ftrace_ops_list_func(unsigned long ip
, unsigned long parent_ip
,
4326 struct ftrace_ops
*ignored
, struct pt_regs
*regs
)
4328 struct ftrace_ops
*op
;
4331 if (function_trace_stop
)
4334 bit
= trace_test_and_set_recursion(TRACE_LIST_START
, TRACE_LIST_MAX
);
4339 * Some of the ops may be dynamically allocated,
4340 * they must be freed after a synchronize_sched().
4342 preempt_disable_notrace();
4343 do_for_each_ftrace_op(op
, ftrace_ops_list
) {
4344 if (ftrace_ops_test(op
, ip
, regs
))
4345 op
->func(ip
, parent_ip
, op
, regs
);
4346 } while_for_each_ftrace_op(op
);
4347 preempt_enable_notrace();
4348 trace_clear_recursion(bit
);
4352 * Some archs only support passing ip and parent_ip. Even though
4353 * the list function ignores the op parameter, we do not want any
4354 * C side effects, where a function is called without the caller
4355 * sending a third parameter.
4356 * Archs are to support both the regs and ftrace_ops at the same time.
4357 * If they support ftrace_ops, it is assumed they support regs.
4358 * If call backs want to use regs, they must either check for regs
4359 * being NULL, or CONFIG_DYNAMIC_FTRACE_WITH_REGS.
4360 * Note, CONFIG_DYNAMIC_FTRACE_WITH_REGS expects a full regs to be saved.
4361 * An architecture can pass partial regs with ftrace_ops and still
4362 * set the ARCH_SUPPORT_FTARCE_OPS.
4364 #if ARCH_SUPPORTS_FTRACE_OPS
4365 static void ftrace_ops_list_func(unsigned long ip
, unsigned long parent_ip
,
4366 struct ftrace_ops
*op
, struct pt_regs
*regs
)
4368 __ftrace_ops_list_func(ip
, parent_ip
, NULL
, regs
);
4371 static void ftrace_ops_no_ops(unsigned long ip
, unsigned long parent_ip
)
4373 __ftrace_ops_list_func(ip
, parent_ip
, NULL
, NULL
);
4377 static void clear_ftrace_swapper(void)
4379 struct task_struct
*p
;
4383 for_each_online_cpu(cpu
) {
4385 clear_tsk_trace_trace(p
);
4390 static void set_ftrace_swapper(void)
4392 struct task_struct
*p
;
4396 for_each_online_cpu(cpu
) {
4398 set_tsk_trace_trace(p
);
4403 static void clear_ftrace_pid(struct pid
*pid
)
4405 struct task_struct
*p
;
4408 do_each_pid_task(pid
, PIDTYPE_PID
, p
) {
4409 clear_tsk_trace_trace(p
);
4410 } while_each_pid_task(pid
, PIDTYPE_PID
, p
);
4416 static void set_ftrace_pid(struct pid
*pid
)
4418 struct task_struct
*p
;
4421 do_each_pid_task(pid
, PIDTYPE_PID
, p
) {
4422 set_tsk_trace_trace(p
);
4423 } while_each_pid_task(pid
, PIDTYPE_PID
, p
);
4427 static void clear_ftrace_pid_task(struct pid
*pid
)
4429 if (pid
== ftrace_swapper_pid
)
4430 clear_ftrace_swapper();
4432 clear_ftrace_pid(pid
);
4435 static void set_ftrace_pid_task(struct pid
*pid
)
4437 if (pid
== ftrace_swapper_pid
)
4438 set_ftrace_swapper();
4440 set_ftrace_pid(pid
);
4443 static int ftrace_pid_add(int p
)
4446 struct ftrace_pid
*fpid
;
4449 mutex_lock(&ftrace_lock
);
4452 pid
= ftrace_swapper_pid
;
4454 pid
= find_get_pid(p
);
4461 list_for_each_entry(fpid
, &ftrace_pids
, list
)
4462 if (fpid
->pid
== pid
)
4467 fpid
= kmalloc(sizeof(*fpid
), GFP_KERNEL
);
4471 list_add(&fpid
->list
, &ftrace_pids
);
4474 set_ftrace_pid_task(pid
);
4476 ftrace_update_pid_func();
4477 ftrace_startup_enable(0);
4479 mutex_unlock(&ftrace_lock
);
4483 if (pid
!= ftrace_swapper_pid
)
4487 mutex_unlock(&ftrace_lock
);
4491 static void ftrace_pid_reset(void)
4493 struct ftrace_pid
*fpid
, *safe
;
4495 mutex_lock(&ftrace_lock
);
4496 list_for_each_entry_safe(fpid
, safe
, &ftrace_pids
, list
) {
4497 struct pid
*pid
= fpid
->pid
;
4499 clear_ftrace_pid_task(pid
);
4501 list_del(&fpid
->list
);
4505 ftrace_update_pid_func();
4506 ftrace_startup_enable(0);
4508 mutex_unlock(&ftrace_lock
);
4511 static void *fpid_start(struct seq_file
*m
, loff_t
*pos
)
4513 mutex_lock(&ftrace_lock
);
4515 if (list_empty(&ftrace_pids
) && (!*pos
))
4518 return seq_list_start(&ftrace_pids
, *pos
);
4521 static void *fpid_next(struct seq_file
*m
, void *v
, loff_t
*pos
)
4526 return seq_list_next(v
, &ftrace_pids
, pos
);
4529 static void fpid_stop(struct seq_file
*m
, void *p
)
4531 mutex_unlock(&ftrace_lock
);
4534 static int fpid_show(struct seq_file
*m
, void *v
)
4536 const struct ftrace_pid
*fpid
= list_entry(v
, struct ftrace_pid
, list
);
4538 if (v
== (void *)1) {
4539 seq_printf(m
, "no pid\n");
4543 if (fpid
->pid
== ftrace_swapper_pid
)
4544 seq_printf(m
, "swapper tasks\n");
4546 seq_printf(m
, "%u\n", pid_vnr(fpid
->pid
));
4551 static const struct seq_operations ftrace_pid_sops
= {
4552 .start
= fpid_start
,
4559 ftrace_pid_open(struct inode
*inode
, struct file
*file
)
4563 if ((file
->f_mode
& FMODE_WRITE
) &&
4564 (file
->f_flags
& O_TRUNC
))
4567 if (file
->f_mode
& FMODE_READ
)
4568 ret
= seq_open(file
, &ftrace_pid_sops
);
4574 ftrace_pid_write(struct file
*filp
, const char __user
*ubuf
,
4575 size_t cnt
, loff_t
*ppos
)
4581 if (cnt
>= sizeof(buf
))
4584 if (copy_from_user(&buf
, ubuf
, cnt
))
4590 * Allow "echo > set_ftrace_pid" or "echo -n '' > set_ftrace_pid"
4591 * to clean the filter quietly.
4593 tmp
= strstrip(buf
);
4594 if (strlen(tmp
) == 0)
4597 ret
= kstrtol(tmp
, 10, &val
);
4601 ret
= ftrace_pid_add(val
);
4603 return ret
? ret
: cnt
;
4607 ftrace_pid_release(struct inode
*inode
, struct file
*file
)
4609 if (file
->f_mode
& FMODE_READ
)
4610 seq_release(inode
, file
);
4615 static const struct file_operations ftrace_pid_fops
= {
4616 .open
= ftrace_pid_open
,
4617 .write
= ftrace_pid_write
,
4619 .llseek
= ftrace_filter_lseek
,
4620 .release
= ftrace_pid_release
,
4623 static __init
int ftrace_init_debugfs(void)
4625 struct dentry
*d_tracer
;
4627 d_tracer
= tracing_init_dentry();
4631 ftrace_init_dyn_debugfs(d_tracer
);
4633 trace_create_file("set_ftrace_pid", 0644, d_tracer
,
4634 NULL
, &ftrace_pid_fops
);
4636 ftrace_profile_debugfs(d_tracer
);
4640 fs_initcall(ftrace_init_debugfs
);
4643 * ftrace_kill - kill ftrace
4645 * This function should be used by panic code. It stops ftrace
4646 * but in a not so nice way. If you need to simply kill ftrace
4647 * from a non-atomic section, use ftrace_kill.
4649 void ftrace_kill(void)
4651 ftrace_disabled
= 1;
4653 clear_ftrace_function();
4657 * Test if ftrace is dead or not.
4659 int ftrace_is_dead(void)
4661 return ftrace_disabled
;
4665 * register_ftrace_function - register a function for profiling
4666 * @ops - ops structure that holds the function for profiling.
4668 * Register a function to be called by all functions in the
4671 * Note: @ops->func and all the functions it calls must be labeled
4672 * with "notrace", otherwise it will go into a
4675 int register_ftrace_function(struct ftrace_ops
*ops
)
4679 ftrace_ops_init(ops
);
4681 mutex_lock(&ftrace_lock
);
4683 ret
= __register_ftrace_function(ops
);
4685 ret
= ftrace_startup(ops
, 0);
4687 mutex_unlock(&ftrace_lock
);
4691 EXPORT_SYMBOL_GPL(register_ftrace_function
);
4694 * unregister_ftrace_function - unregister a function for profiling.
4695 * @ops - ops structure that holds the function to unregister
4697 * Unregister a function that was added to be called by ftrace profiling.
4699 int unregister_ftrace_function(struct ftrace_ops
*ops
)
4703 mutex_lock(&ftrace_lock
);
4704 ret
= __unregister_ftrace_function(ops
);
4706 ftrace_shutdown(ops
, 0);
4707 mutex_unlock(&ftrace_lock
);
4711 EXPORT_SYMBOL_GPL(unregister_ftrace_function
);
4714 ftrace_enable_sysctl(struct ctl_table
*table
, int write
,
4715 void __user
*buffer
, size_t *lenp
,
4720 mutex_lock(&ftrace_lock
);
4722 if (unlikely(ftrace_disabled
))
4725 ret
= proc_dointvec(table
, write
, buffer
, lenp
, ppos
);
4727 if (ret
|| !write
|| (last_ftrace_enabled
== !!ftrace_enabled
))
4730 last_ftrace_enabled
= !!ftrace_enabled
;
4732 if (ftrace_enabled
) {
4734 ftrace_startup_sysctl();
4736 /* we are starting ftrace again */
4737 if (ftrace_ops_list
!= &ftrace_list_end
)
4738 update_ftrace_function();
4741 /* stopping ftrace calls (just send to ftrace_stub) */
4742 ftrace_trace_function
= ftrace_stub
;
4744 ftrace_shutdown_sysctl();
4748 mutex_unlock(&ftrace_lock
);
4752 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
4754 static int ftrace_graph_active
;
4755 static struct notifier_block ftrace_suspend_notifier
;
4757 int ftrace_graph_entry_stub(struct ftrace_graph_ent
*trace
)
4762 /* The callbacks that hook a function */
4763 trace_func_graph_ret_t ftrace_graph_return
=
4764 (trace_func_graph_ret_t
)ftrace_stub
;
4765 trace_func_graph_ent_t ftrace_graph_entry
= ftrace_graph_entry_stub
;
4767 /* Try to assign a return stack array on FTRACE_RETSTACK_ALLOC_SIZE tasks. */
4768 static int alloc_retstack_tasklist(struct ftrace_ret_stack
**ret_stack_list
)
4772 unsigned long flags
;
4773 int start
= 0, end
= FTRACE_RETSTACK_ALLOC_SIZE
;
4774 struct task_struct
*g
, *t
;
4776 for (i
= 0; i
< FTRACE_RETSTACK_ALLOC_SIZE
; i
++) {
4777 ret_stack_list
[i
] = kmalloc(FTRACE_RETFUNC_DEPTH
4778 * sizeof(struct ftrace_ret_stack
),
4780 if (!ret_stack_list
[i
]) {
4788 read_lock_irqsave(&tasklist_lock
, flags
);
4789 do_each_thread(g
, t
) {
4795 if (t
->ret_stack
== NULL
) {
4796 atomic_set(&t
->tracing_graph_pause
, 0);
4797 atomic_set(&t
->trace_overrun
, 0);
4798 t
->curr_ret_stack
= -1;
4799 /* Make sure the tasks see the -1 first: */
4801 t
->ret_stack
= ret_stack_list
[start
++];
4803 } while_each_thread(g
, t
);
4806 read_unlock_irqrestore(&tasklist_lock
, flags
);
4808 for (i
= start
; i
< end
; i
++)
4809 kfree(ret_stack_list
[i
]);
4814 ftrace_graph_probe_sched_switch(void *ignore
,
4815 struct task_struct
*prev
, struct task_struct
*next
)
4817 unsigned long long timestamp
;
4821 * Does the user want to count the time a function was asleep.
4822 * If so, do not update the time stamps.
4824 if (trace_flags
& TRACE_ITER_SLEEP_TIME
)
4827 timestamp
= trace_clock_local();
4829 prev
->ftrace_timestamp
= timestamp
;
4831 /* only process tasks that we timestamped */
4832 if (!next
->ftrace_timestamp
)
4836 * Update all the counters in next to make up for the
4837 * time next was sleeping.
4839 timestamp
-= next
->ftrace_timestamp
;
4841 for (index
= next
->curr_ret_stack
; index
>= 0; index
--)
4842 next
->ret_stack
[index
].calltime
+= timestamp
;
4845 /* Allocate a return stack for each task */
4846 static int start_graph_tracing(void)
4848 struct ftrace_ret_stack
**ret_stack_list
;
4851 ret_stack_list
= kmalloc(FTRACE_RETSTACK_ALLOC_SIZE
*
4852 sizeof(struct ftrace_ret_stack
*),
4855 if (!ret_stack_list
)
4858 /* The cpu_boot init_task->ret_stack will never be freed */
4859 for_each_online_cpu(cpu
) {
4860 if (!idle_task(cpu
)->ret_stack
)
4861 ftrace_graph_init_idle_task(idle_task(cpu
), cpu
);
4865 ret
= alloc_retstack_tasklist(ret_stack_list
);
4866 } while (ret
== -EAGAIN
);
4869 ret
= register_trace_sched_switch(ftrace_graph_probe_sched_switch
, NULL
);
4871 pr_info("ftrace_graph: Couldn't activate tracepoint"
4872 " probe to kernel_sched_switch\n");
4875 kfree(ret_stack_list
);
4880 * Hibernation protection.
4881 * The state of the current task is too much unstable during
4882 * suspend/restore to disk. We want to protect against that.
4885 ftrace_suspend_notifier_call(struct notifier_block
*bl
, unsigned long state
,
4889 case PM_HIBERNATION_PREPARE
:
4890 pause_graph_tracing();
4893 case PM_POST_HIBERNATION
:
4894 unpause_graph_tracing();
4900 int register_ftrace_graph(trace_func_graph_ret_t retfunc
,
4901 trace_func_graph_ent_t entryfunc
)
4905 mutex_lock(&ftrace_lock
);
4907 /* we currently allow only one tracer registered at a time */
4908 if (ftrace_graph_active
) {
4913 ftrace_suspend_notifier
.notifier_call
= ftrace_suspend_notifier_call
;
4914 register_pm_notifier(&ftrace_suspend_notifier
);
4916 ftrace_graph_active
++;
4917 ret
= start_graph_tracing();
4919 ftrace_graph_active
--;
4923 ftrace_graph_return
= retfunc
;
4924 ftrace_graph_entry
= entryfunc
;
4926 ret
= ftrace_startup(&global_ops
, FTRACE_START_FUNC_RET
);
4929 mutex_unlock(&ftrace_lock
);
4933 void unregister_ftrace_graph(void)
4935 mutex_lock(&ftrace_lock
);
4937 if (unlikely(!ftrace_graph_active
))
4940 ftrace_graph_active
--;
4941 ftrace_graph_return
= (trace_func_graph_ret_t
)ftrace_stub
;
4942 ftrace_graph_entry
= ftrace_graph_entry_stub
;
4943 ftrace_shutdown(&global_ops
, FTRACE_STOP_FUNC_RET
);
4944 unregister_pm_notifier(&ftrace_suspend_notifier
);
4945 unregister_trace_sched_switch(ftrace_graph_probe_sched_switch
, NULL
);
4948 mutex_unlock(&ftrace_lock
);
4951 static DEFINE_PER_CPU(struct ftrace_ret_stack
*, idle_ret_stack
);
4954 graph_init_task(struct task_struct
*t
, struct ftrace_ret_stack
*ret_stack
)
4956 atomic_set(&t
->tracing_graph_pause
, 0);
4957 atomic_set(&t
->trace_overrun
, 0);
4958 t
->ftrace_timestamp
= 0;
4959 /* make curr_ret_stack visible before we add the ret_stack */
4961 t
->ret_stack
= ret_stack
;
4965 * Allocate a return stack for the idle task. May be the first
4966 * time through, or it may be done by CPU hotplug online.
4968 void ftrace_graph_init_idle_task(struct task_struct
*t
, int cpu
)
4970 t
->curr_ret_stack
= -1;
4972 * The idle task has no parent, it either has its own
4973 * stack or no stack at all.
4976 WARN_ON(t
->ret_stack
!= per_cpu(idle_ret_stack
, cpu
));
4978 if (ftrace_graph_active
) {
4979 struct ftrace_ret_stack
*ret_stack
;
4981 ret_stack
= per_cpu(idle_ret_stack
, cpu
);
4983 ret_stack
= kmalloc(FTRACE_RETFUNC_DEPTH
4984 * sizeof(struct ftrace_ret_stack
),
4988 per_cpu(idle_ret_stack
, cpu
) = ret_stack
;
4990 graph_init_task(t
, ret_stack
);
4994 /* Allocate a return stack for newly created task */
4995 void ftrace_graph_init_task(struct task_struct
*t
)
4997 /* Make sure we do not use the parent ret_stack */
4998 t
->ret_stack
= NULL
;
4999 t
->curr_ret_stack
= -1;
5001 if (ftrace_graph_active
) {
5002 struct ftrace_ret_stack
*ret_stack
;
5004 ret_stack
= kmalloc(FTRACE_RETFUNC_DEPTH
5005 * sizeof(struct ftrace_ret_stack
),
5009 graph_init_task(t
, ret_stack
);
5013 void ftrace_graph_exit_task(struct task_struct
*t
)
5015 struct ftrace_ret_stack
*ret_stack
= t
->ret_stack
;
5017 t
->ret_stack
= NULL
;
5018 /* NULL must become visible to IRQs before we free it: */
5024 void ftrace_graph_stop(void)