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 static struct ftrace_ops ftrace_list_end __read_mostly
= {
69 .flags
= FTRACE_OPS_FL_RECURSION_SAFE
,
72 /* ftrace_enabled is a method to turn ftrace on or off */
73 int ftrace_enabled __read_mostly
;
74 static int last_ftrace_enabled
;
76 /* Quick disabling of function tracer. */
77 int function_trace_stop __read_mostly
;
79 /* Current function tracing op */
80 struct ftrace_ops
*function_trace_op __read_mostly
= &ftrace_list_end
;
82 /* List for set_ftrace_pid's pids. */
83 LIST_HEAD(ftrace_pids
);
85 struct list_head list
;
90 * ftrace_disabled is set when an anomaly is discovered.
91 * ftrace_disabled is much stronger than ftrace_enabled.
93 static int ftrace_disabled __read_mostly
;
95 static DEFINE_MUTEX(ftrace_lock
);
97 static struct ftrace_ops
*ftrace_global_list __read_mostly
= &ftrace_list_end
;
98 static struct ftrace_ops
*ftrace_control_list __read_mostly
= &ftrace_list_end
;
99 static struct ftrace_ops
*ftrace_ops_list __read_mostly
= &ftrace_list_end
;
100 ftrace_func_t ftrace_trace_function __read_mostly
= ftrace_stub
;
101 ftrace_func_t ftrace_pid_function __read_mostly
= ftrace_stub
;
102 static struct ftrace_ops global_ops
;
103 static struct ftrace_ops control_ops
;
105 #if ARCH_SUPPORTS_FTRACE_OPS
106 static void ftrace_ops_list_func(unsigned long ip
, unsigned long parent_ip
,
107 struct ftrace_ops
*op
, struct pt_regs
*regs
);
109 /* See comment below, where ftrace_ops_list_func is defined */
110 static void ftrace_ops_no_ops(unsigned long ip
, unsigned long parent_ip
);
111 #define ftrace_ops_list_func ((ftrace_func_t)ftrace_ops_no_ops)
115 * Traverse the ftrace_global_list, invoking all entries. The reason that we
116 * can use rcu_dereference_raw() is that elements removed from this list
117 * are simply leaked, so there is no need to interact with a grace-period
118 * mechanism. The rcu_dereference_raw() calls are needed to handle
119 * concurrent insertions into the ftrace_global_list.
121 * Silly Alpha and silly pointer-speculation compiler optimizations!
123 #define do_for_each_ftrace_op(op, list) \
124 op = rcu_dereference_raw(list); \
128 * Optimized for just a single item in the list (as that is the normal case).
130 #define while_for_each_ftrace_op(op) \
131 while (likely(op = rcu_dereference_raw((op)->next)) && \
132 unlikely((op) != &ftrace_list_end))
135 * ftrace_nr_registered_ops - return number of ops registered
137 * Returns the number of ftrace_ops registered and tracing functions
139 int ftrace_nr_registered_ops(void)
141 struct ftrace_ops
*ops
;
144 mutex_lock(&ftrace_lock
);
146 for (ops
= ftrace_ops_list
;
147 ops
!= &ftrace_list_end
; ops
= ops
->next
)
150 mutex_unlock(&ftrace_lock
);
156 ftrace_global_list_func(unsigned long ip
, unsigned long parent_ip
,
157 struct ftrace_ops
*op
, struct pt_regs
*regs
)
161 bit
= trace_test_and_set_recursion(TRACE_GLOBAL_START
, TRACE_GLOBAL_MAX
);
165 do_for_each_ftrace_op(op
, ftrace_global_list
) {
166 op
->func(ip
, parent_ip
, op
, regs
);
167 } while_for_each_ftrace_op(op
);
169 trace_clear_recursion(bit
);
172 static void ftrace_pid_func(unsigned long ip
, unsigned long parent_ip
,
173 struct ftrace_ops
*op
, struct pt_regs
*regs
)
175 if (!test_tsk_trace_trace(current
))
178 ftrace_pid_function(ip
, parent_ip
, op
, regs
);
181 static void set_ftrace_pid_function(ftrace_func_t func
)
183 /* do not set ftrace_pid_function to itself! */
184 if (func
!= ftrace_pid_func
)
185 ftrace_pid_function
= func
;
189 * clear_ftrace_function - reset the ftrace function
191 * This NULLs the ftrace function and in essence stops
192 * tracing. There may be lag
194 void clear_ftrace_function(void)
196 ftrace_trace_function
= ftrace_stub
;
197 ftrace_pid_function
= ftrace_stub
;
200 static void control_ops_disable_all(struct ftrace_ops
*ops
)
204 for_each_possible_cpu(cpu
)
205 *per_cpu_ptr(ops
->disabled
, cpu
) = 1;
208 static int control_ops_alloc(struct ftrace_ops
*ops
)
210 int __percpu
*disabled
;
212 disabled
= alloc_percpu(int);
216 ops
->disabled
= disabled
;
217 control_ops_disable_all(ops
);
221 static void control_ops_free(struct ftrace_ops
*ops
)
223 free_percpu(ops
->disabled
);
226 static void update_global_ops(void)
231 * If there's only one function registered, then call that
232 * function directly. Otherwise, we need to iterate over the
233 * registered callers.
235 if (ftrace_global_list
== &ftrace_list_end
||
236 ftrace_global_list
->next
== &ftrace_list_end
) {
237 func
= ftrace_global_list
->func
;
239 * As we are calling the function directly.
240 * If it does not have recursion protection,
241 * the function_trace_op needs to be updated
244 if (ftrace_global_list
->flags
& FTRACE_OPS_FL_RECURSION_SAFE
)
245 global_ops
.flags
|= FTRACE_OPS_FL_RECURSION_SAFE
;
247 global_ops
.flags
&= ~FTRACE_OPS_FL_RECURSION_SAFE
;
249 func
= ftrace_global_list_func
;
250 /* The list has its own recursion protection. */
251 global_ops
.flags
|= FTRACE_OPS_FL_RECURSION_SAFE
;
255 /* If we filter on pids, update to use the pid function */
256 if (!list_empty(&ftrace_pids
)) {
257 set_ftrace_pid_function(func
);
258 func
= ftrace_pid_func
;
261 global_ops
.func
= func
;
264 static void update_ftrace_function(void)
271 * If we are at the end of the list and this ops is
272 * recursion safe and not dynamic and the arch supports passing ops,
273 * then have the mcount trampoline call the function directly.
275 if (ftrace_ops_list
== &ftrace_list_end
||
276 (ftrace_ops_list
->next
== &ftrace_list_end
&&
277 !(ftrace_ops_list
->flags
& FTRACE_OPS_FL_DYNAMIC
) &&
278 (ftrace_ops_list
->flags
& FTRACE_OPS_FL_RECURSION_SAFE
) &&
279 !FTRACE_FORCE_LIST_FUNC
)) {
280 /* Set the ftrace_ops that the arch callback uses */
281 if (ftrace_ops_list
== &global_ops
)
282 function_trace_op
= ftrace_global_list
;
284 function_trace_op
= ftrace_ops_list
;
285 func
= ftrace_ops_list
->func
;
287 /* Just use the default ftrace_ops */
288 function_trace_op
= &ftrace_list_end
;
289 func
= ftrace_ops_list_func
;
292 ftrace_trace_function
= func
;
295 static void add_ftrace_ops(struct ftrace_ops
**list
, struct ftrace_ops
*ops
)
299 * We are entering ops into the list but another
300 * CPU might be walking that list. We need to make sure
301 * the ops->next pointer is valid before another CPU sees
302 * the ops pointer included into the list.
304 rcu_assign_pointer(*list
, ops
);
307 static int remove_ftrace_ops(struct ftrace_ops
**list
, struct ftrace_ops
*ops
)
309 struct ftrace_ops
**p
;
312 * If we are removing the last function, then simply point
313 * to the ftrace_stub.
315 if (*list
== ops
&& ops
->next
== &ftrace_list_end
) {
316 *list
= &ftrace_list_end
;
320 for (p
= list
; *p
!= &ftrace_list_end
; p
= &(*p
)->next
)
331 static void add_ftrace_list_ops(struct ftrace_ops
**list
,
332 struct ftrace_ops
*main_ops
,
333 struct ftrace_ops
*ops
)
335 int first
= *list
== &ftrace_list_end
;
336 add_ftrace_ops(list
, ops
);
338 add_ftrace_ops(&ftrace_ops_list
, main_ops
);
341 static int remove_ftrace_list_ops(struct ftrace_ops
**list
,
342 struct ftrace_ops
*main_ops
,
343 struct ftrace_ops
*ops
)
345 int ret
= remove_ftrace_ops(list
, ops
);
346 if (!ret
&& *list
== &ftrace_list_end
)
347 ret
= remove_ftrace_ops(&ftrace_ops_list
, main_ops
);
351 static int __register_ftrace_function(struct ftrace_ops
*ops
)
353 if (unlikely(ftrace_disabled
))
356 if (FTRACE_WARN_ON(ops
== &global_ops
))
359 if (WARN_ON(ops
->flags
& FTRACE_OPS_FL_ENABLED
))
362 /* We don't support both control and global flags set. */
363 if ((ops
->flags
& FL_GLOBAL_CONTROL_MASK
) == FL_GLOBAL_CONTROL_MASK
)
366 #ifndef CONFIG_DYNAMIC_FTRACE_WITH_REGS
368 * If the ftrace_ops specifies SAVE_REGS, then it only can be used
369 * if the arch supports it, or SAVE_REGS_IF_SUPPORTED is also set.
370 * Setting SAVE_REGS_IF_SUPPORTED makes SAVE_REGS irrelevant.
372 if (ops
->flags
& FTRACE_OPS_FL_SAVE_REGS
&&
373 !(ops
->flags
& FTRACE_OPS_FL_SAVE_REGS_IF_SUPPORTED
))
376 if (ops
->flags
& FTRACE_OPS_FL_SAVE_REGS_IF_SUPPORTED
)
377 ops
->flags
|= FTRACE_OPS_FL_SAVE_REGS
;
380 if (!core_kernel_data((unsigned long)ops
))
381 ops
->flags
|= FTRACE_OPS_FL_DYNAMIC
;
383 if (ops
->flags
& FTRACE_OPS_FL_GLOBAL
) {
384 add_ftrace_list_ops(&ftrace_global_list
, &global_ops
, ops
);
385 ops
->flags
|= FTRACE_OPS_FL_ENABLED
;
386 } else if (ops
->flags
& FTRACE_OPS_FL_CONTROL
) {
387 if (control_ops_alloc(ops
))
389 add_ftrace_list_ops(&ftrace_control_list
, &control_ops
, ops
);
391 add_ftrace_ops(&ftrace_ops_list
, ops
);
394 update_ftrace_function();
399 static int __unregister_ftrace_function(struct ftrace_ops
*ops
)
406 if (WARN_ON(!(ops
->flags
& FTRACE_OPS_FL_ENABLED
)))
409 if (FTRACE_WARN_ON(ops
== &global_ops
))
412 if (ops
->flags
& FTRACE_OPS_FL_GLOBAL
) {
413 ret
= remove_ftrace_list_ops(&ftrace_global_list
,
416 ops
->flags
&= ~FTRACE_OPS_FL_ENABLED
;
417 } else if (ops
->flags
& FTRACE_OPS_FL_CONTROL
) {
418 ret
= remove_ftrace_list_ops(&ftrace_control_list
,
422 * The ftrace_ops is now removed from the list,
423 * so there'll be no new users. We must ensure
424 * all current users are done before we free
428 control_ops_free(ops
);
431 ret
= remove_ftrace_ops(&ftrace_ops_list
, ops
);
437 update_ftrace_function();
440 * Dynamic ops may be freed, we must make sure that all
441 * callers are done before leaving this function.
443 if (ops
->flags
& FTRACE_OPS_FL_DYNAMIC
)
449 static void ftrace_update_pid_func(void)
451 /* Only do something if we are tracing something */
452 if (ftrace_trace_function
== ftrace_stub
)
455 update_ftrace_function();
458 #ifdef CONFIG_FUNCTION_PROFILER
459 struct ftrace_profile
{
460 struct hlist_node node
;
462 unsigned long counter
;
463 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
464 unsigned long long time
;
465 unsigned long long time_squared
;
469 struct ftrace_profile_page
{
470 struct ftrace_profile_page
*next
;
472 struct ftrace_profile records
[];
475 struct ftrace_profile_stat
{
477 struct hlist_head
*hash
;
478 struct ftrace_profile_page
*pages
;
479 struct ftrace_profile_page
*start
;
480 struct tracer_stat stat
;
483 #define PROFILE_RECORDS_SIZE \
484 (PAGE_SIZE - offsetof(struct ftrace_profile_page, records))
486 #define PROFILES_PER_PAGE \
487 (PROFILE_RECORDS_SIZE / sizeof(struct ftrace_profile))
489 static int ftrace_profile_bits __read_mostly
;
490 static int ftrace_profile_enabled __read_mostly
;
492 /* ftrace_profile_lock - synchronize the enable and disable of the profiler */
493 static DEFINE_MUTEX(ftrace_profile_lock
);
495 static DEFINE_PER_CPU(struct ftrace_profile_stat
, ftrace_profile_stats
);
497 #define FTRACE_PROFILE_HASH_SIZE 1024 /* must be power of 2 */
500 function_stat_next(void *v
, int idx
)
502 struct ftrace_profile
*rec
= v
;
503 struct ftrace_profile_page
*pg
;
505 pg
= (struct ftrace_profile_page
*)((unsigned long)rec
& PAGE_MASK
);
511 if ((void *)rec
>= (void *)&pg
->records
[pg
->index
]) {
515 rec
= &pg
->records
[0];
523 static void *function_stat_start(struct tracer_stat
*trace
)
525 struct ftrace_profile_stat
*stat
=
526 container_of(trace
, struct ftrace_profile_stat
, stat
);
528 if (!stat
|| !stat
->start
)
531 return function_stat_next(&stat
->start
->records
[0], 0);
534 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
535 /* function graph compares on total time */
536 static int function_stat_cmp(void *p1
, void *p2
)
538 struct ftrace_profile
*a
= p1
;
539 struct ftrace_profile
*b
= p2
;
541 if (a
->time
< b
->time
)
543 if (a
->time
> b
->time
)
549 /* not function graph compares against hits */
550 static int function_stat_cmp(void *p1
, void *p2
)
552 struct ftrace_profile
*a
= p1
;
553 struct ftrace_profile
*b
= p2
;
555 if (a
->counter
< b
->counter
)
557 if (a
->counter
> b
->counter
)
564 static int function_stat_headers(struct seq_file
*m
)
566 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
567 seq_printf(m
, " Function "
570 "--- ---- --- ---\n");
572 seq_printf(m
, " Function Hit\n"
578 static int function_stat_show(struct seq_file
*m
, void *v
)
580 struct ftrace_profile
*rec
= v
;
581 char str
[KSYM_SYMBOL_LEN
];
583 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
584 static struct trace_seq s
;
585 unsigned long long avg
;
586 unsigned long long stddev
;
588 mutex_lock(&ftrace_profile_lock
);
590 /* we raced with function_profile_reset() */
591 if (unlikely(rec
->counter
== 0)) {
596 kallsyms_lookup(rec
->ip
, NULL
, NULL
, NULL
, str
);
597 seq_printf(m
, " %-30.30s %10lu", str
, rec
->counter
);
599 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
602 do_div(avg
, rec
->counter
);
604 /* Sample standard deviation (s^2) */
605 if (rec
->counter
<= 1)
608 stddev
= rec
->time_squared
- rec
->counter
* avg
* avg
;
610 * Divide only 1000 for ns^2 -> us^2 conversion.
611 * trace_print_graph_duration will divide 1000 again.
613 do_div(stddev
, (rec
->counter
- 1) * 1000);
617 trace_print_graph_duration(rec
->time
, &s
);
618 trace_seq_puts(&s
, " ");
619 trace_print_graph_duration(avg
, &s
);
620 trace_seq_puts(&s
, " ");
621 trace_print_graph_duration(stddev
, &s
);
622 trace_print_seq(m
, &s
);
626 mutex_unlock(&ftrace_profile_lock
);
631 static void ftrace_profile_reset(struct ftrace_profile_stat
*stat
)
633 struct ftrace_profile_page
*pg
;
635 pg
= stat
->pages
= stat
->start
;
638 memset(pg
->records
, 0, PROFILE_RECORDS_SIZE
);
643 memset(stat
->hash
, 0,
644 FTRACE_PROFILE_HASH_SIZE
* sizeof(struct hlist_head
));
647 int ftrace_profile_pages_init(struct ftrace_profile_stat
*stat
)
649 struct ftrace_profile_page
*pg
;
654 /* If we already allocated, do nothing */
658 stat
->pages
= (void *)get_zeroed_page(GFP_KERNEL
);
662 #ifdef CONFIG_DYNAMIC_FTRACE
663 functions
= ftrace_update_tot_cnt
;
666 * We do not know the number of functions that exist because
667 * dynamic tracing is what counts them. With past experience
668 * we have around 20K functions. That should be more than enough.
669 * It is highly unlikely we will execute every function in
675 pg
= stat
->start
= stat
->pages
;
677 pages
= DIV_ROUND_UP(functions
, PROFILES_PER_PAGE
);
679 for (i
= 0; i
< pages
; i
++) {
680 pg
->next
= (void *)get_zeroed_page(GFP_KERNEL
);
691 unsigned long tmp
= (unsigned long)pg
;
697 free_page((unsigned long)stat
->pages
);
704 static int ftrace_profile_init_cpu(int cpu
)
706 struct ftrace_profile_stat
*stat
;
709 stat
= &per_cpu(ftrace_profile_stats
, cpu
);
712 /* If the profile is already created, simply reset it */
713 ftrace_profile_reset(stat
);
718 * We are profiling all functions, but usually only a few thousand
719 * functions are hit. We'll make a hash of 1024 items.
721 size
= FTRACE_PROFILE_HASH_SIZE
;
723 stat
->hash
= kzalloc(sizeof(struct hlist_head
) * size
, GFP_KERNEL
);
728 if (!ftrace_profile_bits
) {
731 for (; size
; size
>>= 1)
732 ftrace_profile_bits
++;
735 /* Preallocate the function profiling pages */
736 if (ftrace_profile_pages_init(stat
) < 0) {
745 static int ftrace_profile_init(void)
750 for_each_online_cpu(cpu
) {
751 ret
= ftrace_profile_init_cpu(cpu
);
759 /* interrupts must be disabled */
760 static struct ftrace_profile
*
761 ftrace_find_profiled_func(struct ftrace_profile_stat
*stat
, unsigned long ip
)
763 struct ftrace_profile
*rec
;
764 struct hlist_head
*hhd
;
765 struct hlist_node
*n
;
768 key
= hash_long(ip
, ftrace_profile_bits
);
769 hhd
= &stat
->hash
[key
];
771 if (hlist_empty(hhd
))
774 hlist_for_each_entry_rcu(rec
, n
, hhd
, node
) {
782 static void ftrace_add_profile(struct ftrace_profile_stat
*stat
,
783 struct ftrace_profile
*rec
)
787 key
= hash_long(rec
->ip
, ftrace_profile_bits
);
788 hlist_add_head_rcu(&rec
->node
, &stat
->hash
[key
]);
792 * The memory is already allocated, this simply finds a new record to use.
794 static struct ftrace_profile
*
795 ftrace_profile_alloc(struct ftrace_profile_stat
*stat
, unsigned long ip
)
797 struct ftrace_profile
*rec
= NULL
;
799 /* prevent recursion (from NMIs) */
800 if (atomic_inc_return(&stat
->disabled
) != 1)
804 * Try to find the function again since an NMI
805 * could have added it
807 rec
= ftrace_find_profiled_func(stat
, ip
);
811 if (stat
->pages
->index
== PROFILES_PER_PAGE
) {
812 if (!stat
->pages
->next
)
814 stat
->pages
= stat
->pages
->next
;
817 rec
= &stat
->pages
->records
[stat
->pages
->index
++];
819 ftrace_add_profile(stat
, rec
);
822 atomic_dec(&stat
->disabled
);
828 function_profile_call(unsigned long ip
, unsigned long parent_ip
,
829 struct ftrace_ops
*ops
, struct pt_regs
*regs
)
831 struct ftrace_profile_stat
*stat
;
832 struct ftrace_profile
*rec
;
835 if (!ftrace_profile_enabled
)
838 local_irq_save(flags
);
840 stat
= &__get_cpu_var(ftrace_profile_stats
);
841 if (!stat
->hash
|| !ftrace_profile_enabled
)
844 rec
= ftrace_find_profiled_func(stat
, ip
);
846 rec
= ftrace_profile_alloc(stat
, ip
);
853 local_irq_restore(flags
);
856 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
857 static int profile_graph_entry(struct ftrace_graph_ent
*trace
)
859 function_profile_call(trace
->func
, 0, NULL
, NULL
);
863 static void profile_graph_return(struct ftrace_graph_ret
*trace
)
865 struct ftrace_profile_stat
*stat
;
866 unsigned long long calltime
;
867 struct ftrace_profile
*rec
;
870 local_irq_save(flags
);
871 stat
= &__get_cpu_var(ftrace_profile_stats
);
872 if (!stat
->hash
|| !ftrace_profile_enabled
)
875 /* If the calltime was zero'd ignore it */
876 if (!trace
->calltime
)
879 calltime
= trace
->rettime
- trace
->calltime
;
881 if (!(trace_flags
& TRACE_ITER_GRAPH_TIME
)) {
884 index
= trace
->depth
;
886 /* Append this call time to the parent time to subtract */
888 current
->ret_stack
[index
- 1].subtime
+= calltime
;
890 if (current
->ret_stack
[index
].subtime
< calltime
)
891 calltime
-= current
->ret_stack
[index
].subtime
;
896 rec
= ftrace_find_profiled_func(stat
, trace
->func
);
898 rec
->time
+= calltime
;
899 rec
->time_squared
+= calltime
* calltime
;
903 local_irq_restore(flags
);
906 static int register_ftrace_profiler(void)
908 return register_ftrace_graph(&profile_graph_return
,
909 &profile_graph_entry
);
912 static void unregister_ftrace_profiler(void)
914 unregister_ftrace_graph();
917 static struct ftrace_ops ftrace_profile_ops __read_mostly
= {
918 .func
= function_profile_call
,
919 .flags
= FTRACE_OPS_FL_RECURSION_SAFE
,
922 static int register_ftrace_profiler(void)
924 return register_ftrace_function(&ftrace_profile_ops
);
927 static void unregister_ftrace_profiler(void)
929 unregister_ftrace_function(&ftrace_profile_ops
);
931 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
934 ftrace_profile_write(struct file
*filp
, const char __user
*ubuf
,
935 size_t cnt
, loff_t
*ppos
)
940 ret
= kstrtoul_from_user(ubuf
, cnt
, 10, &val
);
946 mutex_lock(&ftrace_profile_lock
);
947 if (ftrace_profile_enabled
^ val
) {
949 ret
= ftrace_profile_init();
955 ret
= register_ftrace_profiler();
960 ftrace_profile_enabled
= 1;
962 ftrace_profile_enabled
= 0;
964 * unregister_ftrace_profiler calls stop_machine
965 * so this acts like an synchronize_sched.
967 unregister_ftrace_profiler();
971 mutex_unlock(&ftrace_profile_lock
);
979 ftrace_profile_read(struct file
*filp
, char __user
*ubuf
,
980 size_t cnt
, loff_t
*ppos
)
982 char buf
[64]; /* big enough to hold a number */
985 r
= sprintf(buf
, "%u\n", ftrace_profile_enabled
);
986 return simple_read_from_buffer(ubuf
, cnt
, ppos
, buf
, r
);
989 static const struct file_operations ftrace_profile_fops
= {
990 .open
= tracing_open_generic
,
991 .read
= ftrace_profile_read
,
992 .write
= ftrace_profile_write
,
993 .llseek
= default_llseek
,
996 /* used to initialize the real stat files */
997 static struct tracer_stat function_stats __initdata
= {
999 .stat_start
= function_stat_start
,
1000 .stat_next
= function_stat_next
,
1001 .stat_cmp
= function_stat_cmp
,
1002 .stat_headers
= function_stat_headers
,
1003 .stat_show
= function_stat_show
1006 static __init
void ftrace_profile_debugfs(struct dentry
*d_tracer
)
1008 struct ftrace_profile_stat
*stat
;
1009 struct dentry
*entry
;
1014 for_each_possible_cpu(cpu
) {
1015 stat
= &per_cpu(ftrace_profile_stats
, cpu
);
1017 /* allocate enough for function name + cpu number */
1018 name
= kmalloc(32, GFP_KERNEL
);
1021 * The files created are permanent, if something happens
1022 * we still do not free memory.
1025 "Could not allocate stat file for cpu %d\n",
1029 stat
->stat
= function_stats
;
1030 snprintf(name
, 32, "function%d", cpu
);
1031 stat
->stat
.name
= name
;
1032 ret
= register_stat_tracer(&stat
->stat
);
1035 "Could not register function stat for cpu %d\n",
1042 entry
= debugfs_create_file("function_profile_enabled", 0644,
1043 d_tracer
, NULL
, &ftrace_profile_fops
);
1045 pr_warning("Could not create debugfs "
1046 "'function_profile_enabled' entry\n");
1049 #else /* CONFIG_FUNCTION_PROFILER */
1050 static __init
void ftrace_profile_debugfs(struct dentry
*d_tracer
)
1053 #endif /* CONFIG_FUNCTION_PROFILER */
1055 static struct pid
* const ftrace_swapper_pid
= &init_struct_pid
;
1057 #ifdef CONFIG_DYNAMIC_FTRACE
1059 #ifndef CONFIG_FTRACE_MCOUNT_RECORD
1060 # error Dynamic ftrace depends on MCOUNT_RECORD
1063 static struct hlist_head ftrace_func_hash
[FTRACE_FUNC_HASHSIZE
] __read_mostly
;
1065 struct ftrace_func_probe
{
1066 struct hlist_node node
;
1067 struct ftrace_probe_ops
*ops
;
1068 unsigned long flags
;
1071 struct list_head free_list
;
1074 struct ftrace_func_entry
{
1075 struct hlist_node hlist
;
1079 struct ftrace_hash
{
1080 unsigned long size_bits
;
1081 struct hlist_head
*buckets
;
1082 unsigned long count
;
1083 struct rcu_head rcu
;
1087 * We make these constant because no one should touch them,
1088 * but they are used as the default "empty hash", to avoid allocating
1089 * it all the time. These are in a read only section such that if
1090 * anyone does try to modify it, it will cause an exception.
1092 static const struct hlist_head empty_buckets
[1];
1093 static const struct ftrace_hash empty_hash
= {
1094 .buckets
= (struct hlist_head
*)empty_buckets
,
1096 #define EMPTY_HASH ((struct ftrace_hash *)&empty_hash)
1098 static struct ftrace_ops global_ops
= {
1099 .func
= ftrace_stub
,
1100 .notrace_hash
= EMPTY_HASH
,
1101 .filter_hash
= EMPTY_HASH
,
1102 .flags
= FTRACE_OPS_FL_RECURSION_SAFE
,
1105 static DEFINE_MUTEX(ftrace_regex_lock
);
1107 struct ftrace_page
{
1108 struct ftrace_page
*next
;
1109 struct dyn_ftrace
*records
;
1114 static struct ftrace_page
*ftrace_new_pgs
;
1116 #define ENTRY_SIZE sizeof(struct dyn_ftrace)
1117 #define ENTRIES_PER_PAGE (PAGE_SIZE / ENTRY_SIZE)
1119 /* estimate from running different kernels */
1120 #define NR_TO_INIT 10000
1122 static struct ftrace_page
*ftrace_pages_start
;
1123 static struct ftrace_page
*ftrace_pages
;
1125 static bool ftrace_hash_empty(struct ftrace_hash
*hash
)
1127 return !hash
|| !hash
->count
;
1130 static struct ftrace_func_entry
*
1131 ftrace_lookup_ip(struct ftrace_hash
*hash
, unsigned long ip
)
1134 struct ftrace_func_entry
*entry
;
1135 struct hlist_head
*hhd
;
1136 struct hlist_node
*n
;
1138 if (ftrace_hash_empty(hash
))
1141 if (hash
->size_bits
> 0)
1142 key
= hash_long(ip
, hash
->size_bits
);
1146 hhd
= &hash
->buckets
[key
];
1148 hlist_for_each_entry_rcu(entry
, n
, hhd
, hlist
) {
1149 if (entry
->ip
== ip
)
1155 static void __add_hash_entry(struct ftrace_hash
*hash
,
1156 struct ftrace_func_entry
*entry
)
1158 struct hlist_head
*hhd
;
1161 if (hash
->size_bits
)
1162 key
= hash_long(entry
->ip
, hash
->size_bits
);
1166 hhd
= &hash
->buckets
[key
];
1167 hlist_add_head(&entry
->hlist
, hhd
);
1171 static int add_hash_entry(struct ftrace_hash
*hash
, unsigned long ip
)
1173 struct ftrace_func_entry
*entry
;
1175 entry
= kmalloc(sizeof(*entry
), GFP_KERNEL
);
1180 __add_hash_entry(hash
, entry
);
1186 free_hash_entry(struct ftrace_hash
*hash
,
1187 struct ftrace_func_entry
*entry
)
1189 hlist_del(&entry
->hlist
);
1195 remove_hash_entry(struct ftrace_hash
*hash
,
1196 struct ftrace_func_entry
*entry
)
1198 hlist_del(&entry
->hlist
);
1202 static void ftrace_hash_clear(struct ftrace_hash
*hash
)
1204 struct hlist_head
*hhd
;
1205 struct hlist_node
*tp
, *tn
;
1206 struct ftrace_func_entry
*entry
;
1207 int size
= 1 << hash
->size_bits
;
1213 for (i
= 0; i
< size
; i
++) {
1214 hhd
= &hash
->buckets
[i
];
1215 hlist_for_each_entry_safe(entry
, tp
, tn
, hhd
, hlist
)
1216 free_hash_entry(hash
, entry
);
1218 FTRACE_WARN_ON(hash
->count
);
1221 static void free_ftrace_hash(struct ftrace_hash
*hash
)
1223 if (!hash
|| hash
== EMPTY_HASH
)
1225 ftrace_hash_clear(hash
);
1226 kfree(hash
->buckets
);
1230 static void __free_ftrace_hash_rcu(struct rcu_head
*rcu
)
1232 struct ftrace_hash
*hash
;
1234 hash
= container_of(rcu
, struct ftrace_hash
, rcu
);
1235 free_ftrace_hash(hash
);
1238 static void free_ftrace_hash_rcu(struct ftrace_hash
*hash
)
1240 if (!hash
|| hash
== EMPTY_HASH
)
1242 call_rcu_sched(&hash
->rcu
, __free_ftrace_hash_rcu
);
1245 void ftrace_free_filter(struct ftrace_ops
*ops
)
1247 free_ftrace_hash(ops
->filter_hash
);
1248 free_ftrace_hash(ops
->notrace_hash
);
1251 static struct ftrace_hash
*alloc_ftrace_hash(int size_bits
)
1253 struct ftrace_hash
*hash
;
1256 hash
= kzalloc(sizeof(*hash
), GFP_KERNEL
);
1260 size
= 1 << size_bits
;
1261 hash
->buckets
= kcalloc(size
, sizeof(*hash
->buckets
), GFP_KERNEL
);
1263 if (!hash
->buckets
) {
1268 hash
->size_bits
= size_bits
;
1273 static struct ftrace_hash
*
1274 alloc_and_copy_ftrace_hash(int size_bits
, struct ftrace_hash
*hash
)
1276 struct ftrace_func_entry
*entry
;
1277 struct ftrace_hash
*new_hash
;
1278 struct hlist_node
*tp
;
1283 new_hash
= alloc_ftrace_hash(size_bits
);
1288 if (ftrace_hash_empty(hash
))
1291 size
= 1 << hash
->size_bits
;
1292 for (i
= 0; i
< size
; i
++) {
1293 hlist_for_each_entry(entry
, tp
, &hash
->buckets
[i
], hlist
) {
1294 ret
= add_hash_entry(new_hash
, entry
->ip
);
1300 FTRACE_WARN_ON(new_hash
->count
!= hash
->count
);
1305 free_ftrace_hash(new_hash
);
1310 ftrace_hash_rec_disable(struct ftrace_ops
*ops
, int filter_hash
);
1312 ftrace_hash_rec_enable(struct ftrace_ops
*ops
, int filter_hash
);
1315 ftrace_hash_move(struct ftrace_ops
*ops
, int enable
,
1316 struct ftrace_hash
**dst
, struct ftrace_hash
*src
)
1318 struct ftrace_func_entry
*entry
;
1319 struct hlist_node
*tp
, *tn
;
1320 struct hlist_head
*hhd
;
1321 struct ftrace_hash
*old_hash
;
1322 struct ftrace_hash
*new_hash
;
1324 int size
= src
->count
;
1330 * Remove the current set, update the hash and add
1333 ftrace_hash_rec_disable(ops
, enable
);
1336 * If the new source is empty, just free dst and assign it
1340 free_ftrace_hash_rcu(*dst
);
1341 rcu_assign_pointer(*dst
, EMPTY_HASH
);
1342 /* still need to update the function records */
1348 * Make the hash size about 1/2 the # found
1350 for (size
/= 2; size
; size
>>= 1)
1353 /* Don't allocate too much */
1354 if (bits
> FTRACE_HASH_MAX_BITS
)
1355 bits
= FTRACE_HASH_MAX_BITS
;
1358 new_hash
= alloc_ftrace_hash(bits
);
1362 size
= 1 << src
->size_bits
;
1363 for (i
= 0; i
< size
; i
++) {
1364 hhd
= &src
->buckets
[i
];
1365 hlist_for_each_entry_safe(entry
, tp
, tn
, hhd
, hlist
) {
1367 key
= hash_long(entry
->ip
, bits
);
1370 remove_hash_entry(src
, entry
);
1371 __add_hash_entry(new_hash
, entry
);
1376 rcu_assign_pointer(*dst
, new_hash
);
1377 free_ftrace_hash_rcu(old_hash
);
1382 * Enable regardless of ret:
1383 * On success, we enable the new hash.
1384 * On failure, we re-enable the original hash.
1386 ftrace_hash_rec_enable(ops
, enable
);
1392 * Test the hashes for this ops to see if we want to call
1393 * the ops->func or not.
1395 * It's a match if the ip is in the ops->filter_hash or
1396 * the filter_hash does not exist or is empty,
1398 * the ip is not in the ops->notrace_hash.
1400 * This needs to be called with preemption disabled as
1401 * the hashes are freed with call_rcu_sched().
1404 ftrace_ops_test(struct ftrace_ops
*ops
, unsigned long ip
)
1406 struct ftrace_hash
*filter_hash
;
1407 struct ftrace_hash
*notrace_hash
;
1410 filter_hash
= rcu_dereference_raw(ops
->filter_hash
);
1411 notrace_hash
= rcu_dereference_raw(ops
->notrace_hash
);
1413 if ((ftrace_hash_empty(filter_hash
) ||
1414 ftrace_lookup_ip(filter_hash
, ip
)) &&
1415 (ftrace_hash_empty(notrace_hash
) ||
1416 !ftrace_lookup_ip(notrace_hash
, ip
)))
1425 * This is a double for. Do not use 'break' to break out of the loop,
1426 * you must use a goto.
1428 #define do_for_each_ftrace_rec(pg, rec) \
1429 for (pg = ftrace_pages_start; pg; pg = pg->next) { \
1431 for (_____i = 0; _____i < pg->index; _____i++) { \
1432 rec = &pg->records[_____i];
1434 #define while_for_each_ftrace_rec() \
1439 static int ftrace_cmp_recs(const void *a
, const void *b
)
1441 const struct dyn_ftrace
*key
= a
;
1442 const struct dyn_ftrace
*rec
= b
;
1444 if (key
->flags
< rec
->ip
)
1446 if (key
->ip
>= rec
->ip
+ MCOUNT_INSN_SIZE
)
1451 static unsigned long ftrace_location_range(unsigned long start
, unsigned long end
)
1453 struct ftrace_page
*pg
;
1454 struct dyn_ftrace
*rec
;
1455 struct dyn_ftrace key
;
1458 key
.flags
= end
; /* overload flags, as it is unsigned long */
1460 for (pg
= ftrace_pages_start
; pg
; pg
= pg
->next
) {
1461 if (end
< pg
->records
[0].ip
||
1462 start
>= (pg
->records
[pg
->index
- 1].ip
+ MCOUNT_INSN_SIZE
))
1464 rec
= bsearch(&key
, pg
->records
, pg
->index
,
1465 sizeof(struct dyn_ftrace
),
1475 * ftrace_location - return true if the ip giving is a traced location
1476 * @ip: the instruction pointer to check
1478 * Returns rec->ip if @ip given is a pointer to a ftrace location.
1479 * That is, the instruction that is either a NOP or call to
1480 * the function tracer. It checks the ftrace internal tables to
1481 * determine if the address belongs or not.
1483 unsigned long ftrace_location(unsigned long ip
)
1485 return ftrace_location_range(ip
, ip
);
1489 * ftrace_text_reserved - return true if range contains an ftrace location
1490 * @start: start of range to search
1491 * @end: end of range to search (inclusive). @end points to the last byte to check.
1493 * Returns 1 if @start and @end contains a ftrace location.
1494 * That is, the instruction that is either a NOP or call to
1495 * the function tracer. It checks the ftrace internal tables to
1496 * determine if the address belongs or not.
1498 int ftrace_text_reserved(void *start
, void *end
)
1502 ret
= ftrace_location_range((unsigned long)start
,
1503 (unsigned long)end
);
1508 static void __ftrace_hash_rec_update(struct ftrace_ops
*ops
,
1512 struct ftrace_hash
*hash
;
1513 struct ftrace_hash
*other_hash
;
1514 struct ftrace_page
*pg
;
1515 struct dyn_ftrace
*rec
;
1519 /* Only update if the ops has been registered */
1520 if (!(ops
->flags
& FTRACE_OPS_FL_ENABLED
))
1524 * In the filter_hash case:
1525 * If the count is zero, we update all records.
1526 * Otherwise we just update the items in the hash.
1528 * In the notrace_hash case:
1529 * We enable the update in the hash.
1530 * As disabling notrace means enabling the tracing,
1531 * and enabling notrace means disabling, the inc variable
1535 hash
= ops
->filter_hash
;
1536 other_hash
= ops
->notrace_hash
;
1537 if (ftrace_hash_empty(hash
))
1541 hash
= ops
->notrace_hash
;
1542 other_hash
= ops
->filter_hash
;
1544 * If the notrace hash has no items,
1545 * then there's nothing to do.
1547 if (ftrace_hash_empty(hash
))
1551 do_for_each_ftrace_rec(pg
, rec
) {
1552 int in_other_hash
= 0;
1558 * Only the filter_hash affects all records.
1559 * Update if the record is not in the notrace hash.
1561 if (!other_hash
|| !ftrace_lookup_ip(other_hash
, rec
->ip
))
1564 in_hash
= !!ftrace_lookup_ip(hash
, rec
->ip
);
1565 in_other_hash
= !!ftrace_lookup_ip(other_hash
, rec
->ip
);
1570 if (filter_hash
&& in_hash
&& !in_other_hash
)
1572 else if (!filter_hash
&& in_hash
&&
1573 (in_other_hash
|| ftrace_hash_empty(other_hash
)))
1581 if (FTRACE_WARN_ON((rec
->flags
& ~FTRACE_FL_MASK
) == FTRACE_REF_MAX
))
1584 * If any ops wants regs saved for this function
1585 * then all ops will get saved regs.
1587 if (ops
->flags
& FTRACE_OPS_FL_SAVE_REGS
)
1588 rec
->flags
|= FTRACE_FL_REGS
;
1590 if (FTRACE_WARN_ON((rec
->flags
& ~FTRACE_FL_MASK
) == 0))
1595 /* Shortcut, if we handled all records, we are done. */
1596 if (!all
&& count
== hash
->count
)
1598 } while_for_each_ftrace_rec();
1601 static void ftrace_hash_rec_disable(struct ftrace_ops
*ops
,
1604 __ftrace_hash_rec_update(ops
, filter_hash
, 0);
1607 static void ftrace_hash_rec_enable(struct ftrace_ops
*ops
,
1610 __ftrace_hash_rec_update(ops
, filter_hash
, 1);
1613 static void print_ip_ins(const char *fmt
, unsigned char *p
)
1617 printk(KERN_CONT
"%s", fmt
);
1619 for (i
= 0; i
< MCOUNT_INSN_SIZE
; i
++)
1620 printk(KERN_CONT
"%s%02x", i
? ":" : "", p
[i
]);
1624 * ftrace_bug - report and shutdown function tracer
1625 * @failed: The failed type (EFAULT, EINVAL, EPERM)
1626 * @ip: The address that failed
1628 * The arch code that enables or disables the function tracing
1629 * can call ftrace_bug() when it has detected a problem in
1630 * modifying the code. @failed should be one of either:
1631 * EFAULT - if the problem happens on reading the @ip address
1632 * EINVAL - if what is read at @ip is not what was expected
1633 * EPERM - if the problem happens on writting to the @ip address
1635 void ftrace_bug(int failed
, unsigned long ip
)
1639 FTRACE_WARN_ON_ONCE(1);
1640 pr_info("ftrace faulted on modifying ");
1644 FTRACE_WARN_ON_ONCE(1);
1645 pr_info("ftrace failed to modify ");
1647 print_ip_ins(" actual: ", (unsigned char *)ip
);
1648 printk(KERN_CONT
"\n");
1651 FTRACE_WARN_ON_ONCE(1);
1652 pr_info("ftrace faulted on writing ");
1656 FTRACE_WARN_ON_ONCE(1);
1657 pr_info("ftrace faulted on unknown error ");
1662 static int ftrace_check_record(struct dyn_ftrace
*rec
, int enable
, int update
)
1664 unsigned long flag
= 0UL;
1667 * If we are updating calls:
1669 * If the record has a ref count, then we need to enable it
1670 * because someone is using it.
1672 * Otherwise we make sure its disabled.
1674 * If we are disabling calls, then disable all records that
1677 if (enable
&& (rec
->flags
& ~FTRACE_FL_MASK
))
1678 flag
= FTRACE_FL_ENABLED
;
1681 * If enabling and the REGS flag does not match the REGS_EN, then
1682 * do not ignore this record. Set flags to fail the compare against
1686 (!(rec
->flags
& FTRACE_FL_REGS
) != !(rec
->flags
& FTRACE_FL_REGS_EN
)))
1687 flag
|= FTRACE_FL_REGS
;
1689 /* If the state of this record hasn't changed, then do nothing */
1690 if ((rec
->flags
& FTRACE_FL_ENABLED
) == flag
)
1691 return FTRACE_UPDATE_IGNORE
;
1694 /* Save off if rec is being enabled (for return value) */
1695 flag
^= rec
->flags
& FTRACE_FL_ENABLED
;
1698 rec
->flags
|= FTRACE_FL_ENABLED
;
1699 if (flag
& FTRACE_FL_REGS
) {
1700 if (rec
->flags
& FTRACE_FL_REGS
)
1701 rec
->flags
|= FTRACE_FL_REGS_EN
;
1703 rec
->flags
&= ~FTRACE_FL_REGS_EN
;
1708 * If this record is being updated from a nop, then
1709 * return UPDATE_MAKE_CALL.
1710 * Otherwise, if the EN flag is set, then return
1711 * UPDATE_MODIFY_CALL_REGS to tell the caller to convert
1712 * from the non-save regs, to a save regs function.
1714 * return UPDATE_MODIFY_CALL to tell the caller to convert
1715 * from the save regs, to a non-save regs function.
1717 if (flag
& FTRACE_FL_ENABLED
)
1718 return FTRACE_UPDATE_MAKE_CALL
;
1719 else if (rec
->flags
& FTRACE_FL_REGS_EN
)
1720 return FTRACE_UPDATE_MODIFY_CALL_REGS
;
1722 return FTRACE_UPDATE_MODIFY_CALL
;
1726 /* If there's no more users, clear all flags */
1727 if (!(rec
->flags
& ~FTRACE_FL_MASK
))
1730 /* Just disable the record (keep REGS state) */
1731 rec
->flags
&= ~FTRACE_FL_ENABLED
;
1734 return FTRACE_UPDATE_MAKE_NOP
;
1738 * ftrace_update_record, set a record that now is tracing or not
1739 * @rec: the record to update
1740 * @enable: set to 1 if the record is tracing, zero to force disable
1742 * The records that represent all functions that can be traced need
1743 * to be updated when tracing has been enabled.
1745 int ftrace_update_record(struct dyn_ftrace
*rec
, int enable
)
1747 return ftrace_check_record(rec
, enable
, 1);
1751 * ftrace_test_record, check if the record has been enabled or not
1752 * @rec: the record to test
1753 * @enable: set to 1 to check if enabled, 0 if it is disabled
1755 * The arch code may need to test if a record is already set to
1756 * tracing to determine how to modify the function code that it
1759 int ftrace_test_record(struct dyn_ftrace
*rec
, int enable
)
1761 return ftrace_check_record(rec
, enable
, 0);
1765 __ftrace_replace_code(struct dyn_ftrace
*rec
, int enable
)
1767 unsigned long ftrace_old_addr
;
1768 unsigned long ftrace_addr
;
1771 ret
= ftrace_update_record(rec
, enable
);
1773 if (rec
->flags
& FTRACE_FL_REGS
)
1774 ftrace_addr
= (unsigned long)FTRACE_REGS_ADDR
;
1776 ftrace_addr
= (unsigned long)FTRACE_ADDR
;
1779 case FTRACE_UPDATE_IGNORE
:
1782 case FTRACE_UPDATE_MAKE_CALL
:
1783 return ftrace_make_call(rec
, ftrace_addr
);
1785 case FTRACE_UPDATE_MAKE_NOP
:
1786 return ftrace_make_nop(NULL
, rec
, ftrace_addr
);
1788 case FTRACE_UPDATE_MODIFY_CALL_REGS
:
1789 case FTRACE_UPDATE_MODIFY_CALL
:
1790 if (rec
->flags
& FTRACE_FL_REGS
)
1791 ftrace_old_addr
= (unsigned long)FTRACE_ADDR
;
1793 ftrace_old_addr
= (unsigned long)FTRACE_REGS_ADDR
;
1795 return ftrace_modify_call(rec
, ftrace_old_addr
, ftrace_addr
);
1798 return -1; /* unknow ftrace bug */
1801 void __weak
ftrace_replace_code(int enable
)
1803 struct dyn_ftrace
*rec
;
1804 struct ftrace_page
*pg
;
1807 if (unlikely(ftrace_disabled
))
1810 do_for_each_ftrace_rec(pg
, rec
) {
1811 failed
= __ftrace_replace_code(rec
, enable
);
1813 ftrace_bug(failed
, rec
->ip
);
1814 /* Stop processing */
1817 } while_for_each_ftrace_rec();
1820 struct ftrace_rec_iter
{
1821 struct ftrace_page
*pg
;
1826 * ftrace_rec_iter_start, start up iterating over traced functions
1828 * Returns an iterator handle that is used to iterate over all
1829 * the records that represent address locations where functions
1832 * May return NULL if no records are available.
1834 struct ftrace_rec_iter
*ftrace_rec_iter_start(void)
1837 * We only use a single iterator.
1838 * Protected by the ftrace_lock mutex.
1840 static struct ftrace_rec_iter ftrace_rec_iter
;
1841 struct ftrace_rec_iter
*iter
= &ftrace_rec_iter
;
1843 iter
->pg
= ftrace_pages_start
;
1846 /* Could have empty pages */
1847 while (iter
->pg
&& !iter
->pg
->index
)
1848 iter
->pg
= iter
->pg
->next
;
1857 * ftrace_rec_iter_next, get the next record to process.
1858 * @iter: The handle to the iterator.
1860 * Returns the next iterator after the given iterator @iter.
1862 struct ftrace_rec_iter
*ftrace_rec_iter_next(struct ftrace_rec_iter
*iter
)
1866 if (iter
->index
>= iter
->pg
->index
) {
1867 iter
->pg
= iter
->pg
->next
;
1870 /* Could have empty pages */
1871 while (iter
->pg
&& !iter
->pg
->index
)
1872 iter
->pg
= iter
->pg
->next
;
1882 * ftrace_rec_iter_record, get the record at the iterator location
1883 * @iter: The current iterator location
1885 * Returns the record that the current @iter is at.
1887 struct dyn_ftrace
*ftrace_rec_iter_record(struct ftrace_rec_iter
*iter
)
1889 return &iter
->pg
->records
[iter
->index
];
1893 ftrace_code_disable(struct module
*mod
, struct dyn_ftrace
*rec
)
1900 if (unlikely(ftrace_disabled
))
1903 ret
= ftrace_make_nop(mod
, rec
, MCOUNT_ADDR
);
1905 ftrace_bug(ret
, ip
);
1912 * archs can override this function if they must do something
1913 * before the modifying code is performed.
1915 int __weak
ftrace_arch_code_modify_prepare(void)
1921 * archs can override this function if they must do something
1922 * after the modifying code is performed.
1924 int __weak
ftrace_arch_code_modify_post_process(void)
1929 void ftrace_modify_all_code(int command
)
1931 if (command
& FTRACE_UPDATE_CALLS
)
1932 ftrace_replace_code(1);
1933 else if (command
& FTRACE_DISABLE_CALLS
)
1934 ftrace_replace_code(0);
1936 if (command
& FTRACE_UPDATE_TRACE_FUNC
)
1937 ftrace_update_ftrace_func(ftrace_trace_function
);
1939 if (command
& FTRACE_START_FUNC_RET
)
1940 ftrace_enable_ftrace_graph_caller();
1941 else if (command
& FTRACE_STOP_FUNC_RET
)
1942 ftrace_disable_ftrace_graph_caller();
1945 static int __ftrace_modify_code(void *data
)
1947 int *command
= data
;
1949 ftrace_modify_all_code(*command
);
1955 * ftrace_run_stop_machine, go back to the stop machine method
1956 * @command: The command to tell ftrace what to do
1958 * If an arch needs to fall back to the stop machine method, the
1959 * it can call this function.
1961 void ftrace_run_stop_machine(int command
)
1963 stop_machine(__ftrace_modify_code
, &command
, NULL
);
1967 * arch_ftrace_update_code, modify the code to trace or not trace
1968 * @command: The command that needs to be done
1970 * Archs can override this function if it does not need to
1971 * run stop_machine() to modify code.
1973 void __weak
arch_ftrace_update_code(int command
)
1975 ftrace_run_stop_machine(command
);
1978 static void ftrace_run_update_code(int command
)
1982 ret
= ftrace_arch_code_modify_prepare();
1983 FTRACE_WARN_ON(ret
);
1987 * Do not call function tracer while we update the code.
1988 * We are in stop machine.
1990 function_trace_stop
++;
1993 * By default we use stop_machine() to modify the code.
1994 * But archs can do what ever they want as long as it
1995 * is safe. The stop_machine() is the safest, but also
1996 * produces the most overhead.
1998 arch_ftrace_update_code(command
);
2000 function_trace_stop
--;
2002 ret
= ftrace_arch_code_modify_post_process();
2003 FTRACE_WARN_ON(ret
);
2006 static ftrace_func_t saved_ftrace_func
;
2007 static int ftrace_start_up
;
2008 static int global_start_up
;
2010 static void ftrace_startup_enable(int command
)
2012 if (saved_ftrace_func
!= ftrace_trace_function
) {
2013 saved_ftrace_func
= ftrace_trace_function
;
2014 command
|= FTRACE_UPDATE_TRACE_FUNC
;
2017 if (!command
|| !ftrace_enabled
)
2020 ftrace_run_update_code(command
);
2023 static int ftrace_startup(struct ftrace_ops
*ops
, int command
)
2025 bool hash_enable
= true;
2027 if (unlikely(ftrace_disabled
))
2031 command
|= FTRACE_UPDATE_CALLS
;
2033 /* ops marked global share the filter hashes */
2034 if (ops
->flags
& FTRACE_OPS_FL_GLOBAL
) {
2036 /* Don't update hash if global is already set */
2037 if (global_start_up
)
2038 hash_enable
= false;
2042 ops
->flags
|= FTRACE_OPS_FL_ENABLED
;
2044 ftrace_hash_rec_enable(ops
, 1);
2046 ftrace_startup_enable(command
);
2051 static void ftrace_shutdown(struct ftrace_ops
*ops
, int command
)
2053 bool hash_disable
= true;
2055 if (unlikely(ftrace_disabled
))
2060 * Just warn in case of unbalance, no need to kill ftrace, it's not
2061 * critical but the ftrace_call callers may be never nopped again after
2062 * further ftrace uses.
2064 WARN_ON_ONCE(ftrace_start_up
< 0);
2066 if (ops
->flags
& FTRACE_OPS_FL_GLOBAL
) {
2069 WARN_ON_ONCE(global_start_up
< 0);
2070 /* Don't update hash if global still has users */
2071 if (global_start_up
) {
2072 WARN_ON_ONCE(!ftrace_start_up
);
2073 hash_disable
= false;
2078 ftrace_hash_rec_disable(ops
, 1);
2080 if (ops
!= &global_ops
|| !global_start_up
)
2081 ops
->flags
&= ~FTRACE_OPS_FL_ENABLED
;
2083 command
|= FTRACE_UPDATE_CALLS
;
2085 if (saved_ftrace_func
!= ftrace_trace_function
) {
2086 saved_ftrace_func
= ftrace_trace_function
;
2087 command
|= FTRACE_UPDATE_TRACE_FUNC
;
2090 if (!command
|| !ftrace_enabled
)
2093 ftrace_run_update_code(command
);
2096 static void ftrace_startup_sysctl(void)
2098 if (unlikely(ftrace_disabled
))
2101 /* Force update next time */
2102 saved_ftrace_func
= NULL
;
2103 /* ftrace_start_up is true if we want ftrace running */
2104 if (ftrace_start_up
)
2105 ftrace_run_update_code(FTRACE_UPDATE_CALLS
);
2108 static void ftrace_shutdown_sysctl(void)
2110 if (unlikely(ftrace_disabled
))
2113 /* ftrace_start_up is true if ftrace is running */
2114 if (ftrace_start_up
)
2115 ftrace_run_update_code(FTRACE_DISABLE_CALLS
);
2118 static cycle_t ftrace_update_time
;
2119 static unsigned long ftrace_update_cnt
;
2120 unsigned long ftrace_update_tot_cnt
;
2122 static int ops_traces_mod(struct ftrace_ops
*ops
)
2124 struct ftrace_hash
*hash
;
2126 hash
= ops
->filter_hash
;
2127 return ftrace_hash_empty(hash
);
2130 static int ftrace_update_code(struct module
*mod
)
2132 struct ftrace_page
*pg
;
2133 struct dyn_ftrace
*p
;
2134 cycle_t start
, stop
;
2135 unsigned long ref
= 0;
2139 * When adding a module, we need to check if tracers are
2140 * currently enabled and if they are set to trace all functions.
2141 * If they are, we need to enable the module functions as well
2142 * as update the reference counts for those function records.
2145 struct ftrace_ops
*ops
;
2147 for (ops
= ftrace_ops_list
;
2148 ops
!= &ftrace_list_end
; ops
= ops
->next
) {
2149 if (ops
->flags
& FTRACE_OPS_FL_ENABLED
&&
2150 ops_traces_mod(ops
))
2155 start
= ftrace_now(raw_smp_processor_id());
2156 ftrace_update_cnt
= 0;
2158 for (pg
= ftrace_new_pgs
; pg
; pg
= pg
->next
) {
2160 for (i
= 0; i
< pg
->index
; i
++) {
2161 /* If something went wrong, bail without enabling anything */
2162 if (unlikely(ftrace_disabled
))
2165 p
= &pg
->records
[i
];
2169 * Do the initial record conversion from mcount jump
2170 * to the NOP instructions.
2172 if (!ftrace_code_disable(mod
, p
))
2175 ftrace_update_cnt
++;
2178 * If the tracing is enabled, go ahead and enable the record.
2180 * The reason not to enable the record immediatelly is the
2181 * inherent check of ftrace_make_nop/ftrace_make_call for
2182 * correct previous instructions. Making first the NOP
2183 * conversion puts the module to the correct state, thus
2184 * passing the ftrace_make_call check.
2186 if (ftrace_start_up
&& ref
) {
2187 int failed
= __ftrace_replace_code(p
, 1);
2189 ftrace_bug(failed
, p
->ip
);
2194 ftrace_new_pgs
= NULL
;
2196 stop
= ftrace_now(raw_smp_processor_id());
2197 ftrace_update_time
= stop
- start
;
2198 ftrace_update_tot_cnt
+= ftrace_update_cnt
;
2203 static int ftrace_allocate_records(struct ftrace_page
*pg
, int count
)
2208 if (WARN_ON(!count
))
2211 order
= get_count_order(DIV_ROUND_UP(count
, ENTRIES_PER_PAGE
));
2214 * We want to fill as much as possible. No more than a page
2217 while ((PAGE_SIZE
<< order
) / ENTRY_SIZE
>= count
+ ENTRIES_PER_PAGE
)
2221 pg
->records
= (void *)__get_free_pages(GFP_KERNEL
| __GFP_ZERO
, order
);
2224 /* if we can't allocate this size, try something smaller */
2231 cnt
= (PAGE_SIZE
<< order
) / ENTRY_SIZE
;
2240 static struct ftrace_page
*
2241 ftrace_allocate_pages(unsigned long num_to_init
)
2243 struct ftrace_page
*start_pg
;
2244 struct ftrace_page
*pg
;
2251 start_pg
= pg
= kzalloc(sizeof(*pg
), GFP_KERNEL
);
2256 * Try to allocate as much as possible in one continues
2257 * location that fills in all of the space. We want to
2258 * waste as little space as possible.
2261 cnt
= ftrace_allocate_records(pg
, num_to_init
);
2269 pg
->next
= kzalloc(sizeof(*pg
), GFP_KERNEL
);
2280 order
= get_count_order(pg
->size
/ ENTRIES_PER_PAGE
);
2281 free_pages((unsigned long)pg
->records
, order
);
2282 start_pg
= pg
->next
;
2286 pr_info("ftrace: FAILED to allocate memory for functions\n");
2290 static int __init
ftrace_dyn_table_alloc(unsigned long num_to_init
)
2295 pr_info("ftrace: No functions to be traced?\n");
2299 cnt
= num_to_init
/ ENTRIES_PER_PAGE
;
2300 pr_info("ftrace: allocating %ld entries in %d pages\n",
2301 num_to_init
, cnt
+ 1);
2306 #define FTRACE_BUFF_MAX (KSYM_SYMBOL_LEN+4) /* room for wildcards */
2308 struct ftrace_iterator
{
2311 struct ftrace_page
*pg
;
2312 struct dyn_ftrace
*func
;
2313 struct ftrace_func_probe
*probe
;
2314 struct trace_parser parser
;
2315 struct ftrace_hash
*hash
;
2316 struct ftrace_ops
*ops
;
2323 t_hash_next(struct seq_file
*m
, loff_t
*pos
)
2325 struct ftrace_iterator
*iter
= m
->private;
2326 struct hlist_node
*hnd
= NULL
;
2327 struct hlist_head
*hhd
;
2333 hnd
= &iter
->probe
->node
;
2335 if (iter
->hidx
>= FTRACE_FUNC_HASHSIZE
)
2338 hhd
= &ftrace_func_hash
[iter
->hidx
];
2340 if (hlist_empty(hhd
)) {
2356 if (WARN_ON_ONCE(!hnd
))
2359 iter
->probe
= hlist_entry(hnd
, struct ftrace_func_probe
, node
);
2364 static void *t_hash_start(struct seq_file
*m
, loff_t
*pos
)
2366 struct ftrace_iterator
*iter
= m
->private;
2370 if (!(iter
->flags
& FTRACE_ITER_DO_HASH
))
2373 if (iter
->func_pos
> *pos
)
2377 for (l
= 0; l
<= (*pos
- iter
->func_pos
); ) {
2378 p
= t_hash_next(m
, &l
);
2385 /* Only set this if we have an item */
2386 iter
->flags
|= FTRACE_ITER_HASH
;
2392 t_hash_show(struct seq_file
*m
, struct ftrace_iterator
*iter
)
2394 struct ftrace_func_probe
*rec
;
2397 if (WARN_ON_ONCE(!rec
))
2400 if (rec
->ops
->print
)
2401 return rec
->ops
->print(m
, rec
->ip
, rec
->ops
, rec
->data
);
2403 seq_printf(m
, "%ps:%ps", (void *)rec
->ip
, (void *)rec
->ops
->func
);
2406 seq_printf(m
, ":%p", rec
->data
);
2413 t_next(struct seq_file
*m
, void *v
, loff_t
*pos
)
2415 struct ftrace_iterator
*iter
= m
->private;
2416 struct ftrace_ops
*ops
= iter
->ops
;
2417 struct dyn_ftrace
*rec
= NULL
;
2419 if (unlikely(ftrace_disabled
))
2422 if (iter
->flags
& FTRACE_ITER_HASH
)
2423 return t_hash_next(m
, pos
);
2426 iter
->pos
= iter
->func_pos
= *pos
;
2428 if (iter
->flags
& FTRACE_ITER_PRINTALL
)
2429 return t_hash_start(m
, pos
);
2432 if (iter
->idx
>= iter
->pg
->index
) {
2433 if (iter
->pg
->next
) {
2434 iter
->pg
= iter
->pg
->next
;
2439 rec
= &iter
->pg
->records
[iter
->idx
++];
2440 if (((iter
->flags
& FTRACE_ITER_FILTER
) &&
2441 !(ftrace_lookup_ip(ops
->filter_hash
, rec
->ip
))) ||
2443 ((iter
->flags
& FTRACE_ITER_NOTRACE
) &&
2444 !ftrace_lookup_ip(ops
->notrace_hash
, rec
->ip
)) ||
2446 ((iter
->flags
& FTRACE_ITER_ENABLED
) &&
2447 !(rec
->flags
& ~FTRACE_FL_MASK
))) {
2455 return t_hash_start(m
, pos
);
2462 static void reset_iter_read(struct ftrace_iterator
*iter
)
2466 iter
->flags
&= ~(FTRACE_ITER_PRINTALL
| FTRACE_ITER_HASH
);
2469 static void *t_start(struct seq_file
*m
, loff_t
*pos
)
2471 struct ftrace_iterator
*iter
= m
->private;
2472 struct ftrace_ops
*ops
= iter
->ops
;
2476 mutex_lock(&ftrace_lock
);
2478 if (unlikely(ftrace_disabled
))
2482 * If an lseek was done, then reset and start from beginning.
2484 if (*pos
< iter
->pos
)
2485 reset_iter_read(iter
);
2488 * For set_ftrace_filter reading, if we have the filter
2489 * off, we can short cut and just print out that all
2490 * functions are enabled.
2492 if (iter
->flags
& FTRACE_ITER_FILTER
&&
2493 ftrace_hash_empty(ops
->filter_hash
)) {
2495 return t_hash_start(m
, pos
);
2496 iter
->flags
|= FTRACE_ITER_PRINTALL
;
2497 /* reset in case of seek/pread */
2498 iter
->flags
&= ~FTRACE_ITER_HASH
;
2502 if (iter
->flags
& FTRACE_ITER_HASH
)
2503 return t_hash_start(m
, pos
);
2506 * Unfortunately, we need to restart at ftrace_pages_start
2507 * every time we let go of the ftrace_mutex. This is because
2508 * those pointers can change without the lock.
2510 iter
->pg
= ftrace_pages_start
;
2512 for (l
= 0; l
<= *pos
; ) {
2513 p
= t_next(m
, p
, &l
);
2519 return t_hash_start(m
, pos
);
2524 static void t_stop(struct seq_file
*m
, void *p
)
2526 mutex_unlock(&ftrace_lock
);
2529 static int t_show(struct seq_file
*m
, void *v
)
2531 struct ftrace_iterator
*iter
= m
->private;
2532 struct dyn_ftrace
*rec
;
2534 if (iter
->flags
& FTRACE_ITER_HASH
)
2535 return t_hash_show(m
, iter
);
2537 if (iter
->flags
& FTRACE_ITER_PRINTALL
) {
2538 seq_printf(m
, "#### all functions enabled ####\n");
2547 seq_printf(m
, "%ps", (void *)rec
->ip
);
2548 if (iter
->flags
& FTRACE_ITER_ENABLED
)
2549 seq_printf(m
, " (%ld)%s",
2550 rec
->flags
& ~FTRACE_FL_MASK
,
2551 rec
->flags
& FTRACE_FL_REGS
? " R" : "");
2552 seq_printf(m
, "\n");
2557 static const struct seq_operations show_ftrace_seq_ops
= {
2565 ftrace_avail_open(struct inode
*inode
, struct file
*file
)
2567 struct ftrace_iterator
*iter
;
2569 if (unlikely(ftrace_disabled
))
2572 iter
= __seq_open_private(file
, &show_ftrace_seq_ops
, sizeof(*iter
));
2574 iter
->pg
= ftrace_pages_start
;
2575 iter
->ops
= &global_ops
;
2578 return iter
? 0 : -ENOMEM
;
2582 ftrace_enabled_open(struct inode
*inode
, struct file
*file
)
2584 struct ftrace_iterator
*iter
;
2586 if (unlikely(ftrace_disabled
))
2589 iter
= __seq_open_private(file
, &show_ftrace_seq_ops
, sizeof(*iter
));
2591 iter
->pg
= ftrace_pages_start
;
2592 iter
->flags
= FTRACE_ITER_ENABLED
;
2593 iter
->ops
= &global_ops
;
2596 return iter
? 0 : -ENOMEM
;
2599 static void ftrace_filter_reset(struct ftrace_hash
*hash
)
2601 mutex_lock(&ftrace_lock
);
2602 ftrace_hash_clear(hash
);
2603 mutex_unlock(&ftrace_lock
);
2607 * ftrace_regex_open - initialize function tracer filter files
2608 * @ops: The ftrace_ops that hold the hash filters
2609 * @flag: The type of filter to process
2610 * @inode: The inode, usually passed in to your open routine
2611 * @file: The file, usually passed in to your open routine
2613 * ftrace_regex_open() initializes the filter files for the
2614 * @ops. Depending on @flag it may process the filter hash or
2615 * the notrace hash of @ops. With this called from the open
2616 * routine, you can use ftrace_filter_write() for the write
2617 * routine if @flag has FTRACE_ITER_FILTER set, or
2618 * ftrace_notrace_write() if @flag has FTRACE_ITER_NOTRACE set.
2619 * ftrace_regex_lseek() should be used as the lseek routine, and
2620 * release must call ftrace_regex_release().
2623 ftrace_regex_open(struct ftrace_ops
*ops
, int flag
,
2624 struct inode
*inode
, struct file
*file
)
2626 struct ftrace_iterator
*iter
;
2627 struct ftrace_hash
*hash
;
2630 if (unlikely(ftrace_disabled
))
2633 iter
= kzalloc(sizeof(*iter
), GFP_KERNEL
);
2637 if (trace_parser_get_init(&iter
->parser
, FTRACE_BUFF_MAX
)) {
2642 if (flag
& FTRACE_ITER_NOTRACE
)
2643 hash
= ops
->notrace_hash
;
2645 hash
= ops
->filter_hash
;
2650 if (file
->f_mode
& FMODE_WRITE
) {
2651 mutex_lock(&ftrace_lock
);
2652 iter
->hash
= alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS
, hash
);
2653 mutex_unlock(&ftrace_lock
);
2656 trace_parser_put(&iter
->parser
);
2662 mutex_lock(&ftrace_regex_lock
);
2664 if ((file
->f_mode
& FMODE_WRITE
) &&
2665 (file
->f_flags
& O_TRUNC
))
2666 ftrace_filter_reset(iter
->hash
);
2668 if (file
->f_mode
& FMODE_READ
) {
2669 iter
->pg
= ftrace_pages_start
;
2671 ret
= seq_open(file
, &show_ftrace_seq_ops
);
2673 struct seq_file
*m
= file
->private_data
;
2677 free_ftrace_hash(iter
->hash
);
2678 trace_parser_put(&iter
->parser
);
2682 file
->private_data
= iter
;
2683 mutex_unlock(&ftrace_regex_lock
);
2689 ftrace_filter_open(struct inode
*inode
, struct file
*file
)
2691 return ftrace_regex_open(&global_ops
,
2692 FTRACE_ITER_FILTER
| FTRACE_ITER_DO_HASH
,
2697 ftrace_notrace_open(struct inode
*inode
, struct file
*file
)
2699 return ftrace_regex_open(&global_ops
, FTRACE_ITER_NOTRACE
,
2704 ftrace_regex_lseek(struct file
*file
, loff_t offset
, int whence
)
2708 if (file
->f_mode
& FMODE_READ
)
2709 ret
= seq_lseek(file
, offset
, whence
);
2711 file
->f_pos
= ret
= 1;
2716 static int ftrace_match(char *str
, char *regex
, int len
, int type
)
2723 if (strcmp(str
, regex
) == 0)
2726 case MATCH_FRONT_ONLY
:
2727 if (strncmp(str
, regex
, len
) == 0)
2730 case MATCH_MIDDLE_ONLY
:
2731 if (strstr(str
, regex
))
2734 case MATCH_END_ONLY
:
2736 if (slen
>= len
&& memcmp(str
+ slen
- len
, regex
, len
) == 0)
2745 enter_record(struct ftrace_hash
*hash
, struct dyn_ftrace
*rec
, int not)
2747 struct ftrace_func_entry
*entry
;
2750 entry
= ftrace_lookup_ip(hash
, rec
->ip
);
2752 /* Do nothing if it doesn't exist */
2756 free_hash_entry(hash
, entry
);
2758 /* Do nothing if it exists */
2762 ret
= add_hash_entry(hash
, rec
->ip
);
2768 ftrace_match_record(struct dyn_ftrace
*rec
, char *mod
,
2769 char *regex
, int len
, int type
)
2771 char str
[KSYM_SYMBOL_LEN
];
2774 kallsyms_lookup(rec
->ip
, NULL
, NULL
, &modname
, str
);
2777 /* module lookup requires matching the module */
2778 if (!modname
|| strcmp(modname
, mod
))
2781 /* blank search means to match all funcs in the mod */
2786 return ftrace_match(str
, regex
, len
, type
);
2790 match_records(struct ftrace_hash
*hash
, char *buff
,
2791 int len
, char *mod
, int not)
2793 unsigned search_len
= 0;
2794 struct ftrace_page
*pg
;
2795 struct dyn_ftrace
*rec
;
2796 int type
= MATCH_FULL
;
2797 char *search
= buff
;
2802 type
= filter_parse_regex(buff
, len
, &search
, ¬);
2803 search_len
= strlen(search
);
2806 mutex_lock(&ftrace_lock
);
2808 if (unlikely(ftrace_disabled
))
2811 do_for_each_ftrace_rec(pg
, rec
) {
2812 if (ftrace_match_record(rec
, mod
, search
, search_len
, type
)) {
2813 ret
= enter_record(hash
, rec
, not);
2820 } while_for_each_ftrace_rec();
2822 mutex_unlock(&ftrace_lock
);
2828 ftrace_match_records(struct ftrace_hash
*hash
, char *buff
, int len
)
2830 return match_records(hash
, buff
, len
, NULL
, 0);
2834 ftrace_match_module_records(struct ftrace_hash
*hash
, char *buff
, char *mod
)
2838 /* blank or '*' mean the same */
2839 if (strcmp(buff
, "*") == 0)
2842 /* handle the case of 'dont filter this module' */
2843 if (strcmp(buff
, "!") == 0 || strcmp(buff
, "!*") == 0) {
2848 return match_records(hash
, buff
, strlen(buff
), mod
, not);
2852 * We register the module command as a template to show others how
2853 * to register the a command as well.
2857 ftrace_mod_callback(struct ftrace_hash
*hash
,
2858 char *func
, char *cmd
, char *param
, int enable
)
2864 * cmd == 'mod' because we only registered this func
2865 * for the 'mod' ftrace_func_command.
2866 * But if you register one func with multiple commands,
2867 * you can tell which command was used by the cmd
2871 /* we must have a module name */
2875 mod
= strsep(¶m
, ":");
2879 ret
= ftrace_match_module_records(hash
, func
, mod
);
2888 static struct ftrace_func_command ftrace_mod_cmd
= {
2890 .func
= ftrace_mod_callback
,
2893 static int __init
ftrace_mod_cmd_init(void)
2895 return register_ftrace_command(&ftrace_mod_cmd
);
2897 core_initcall(ftrace_mod_cmd_init
);
2899 static void function_trace_probe_call(unsigned long ip
, unsigned long parent_ip
,
2900 struct ftrace_ops
*op
, struct pt_regs
*pt_regs
)
2902 struct ftrace_func_probe
*entry
;
2903 struct hlist_head
*hhd
;
2904 struct hlist_node
*n
;
2907 key
= hash_long(ip
, FTRACE_HASH_BITS
);
2909 hhd
= &ftrace_func_hash
[key
];
2911 if (hlist_empty(hhd
))
2915 * Disable preemption for these calls to prevent a RCU grace
2916 * period. This syncs the hash iteration and freeing of items
2917 * on the hash. rcu_read_lock is too dangerous here.
2919 preempt_disable_notrace();
2920 hlist_for_each_entry_rcu(entry
, n
, hhd
, node
) {
2921 if (entry
->ip
== ip
)
2922 entry
->ops
->func(ip
, parent_ip
, &entry
->data
);
2924 preempt_enable_notrace();
2927 static struct ftrace_ops trace_probe_ops __read_mostly
=
2929 .func
= function_trace_probe_call
,
2932 static int ftrace_probe_registered
;
2934 static void __enable_ftrace_function_probe(void)
2939 if (ftrace_probe_registered
)
2942 for (i
= 0; i
< FTRACE_FUNC_HASHSIZE
; i
++) {
2943 struct hlist_head
*hhd
= &ftrace_func_hash
[i
];
2947 /* Nothing registered? */
2948 if (i
== FTRACE_FUNC_HASHSIZE
)
2951 ret
= __register_ftrace_function(&trace_probe_ops
);
2953 ret
= ftrace_startup(&trace_probe_ops
, 0);
2955 ftrace_probe_registered
= 1;
2958 static void __disable_ftrace_function_probe(void)
2963 if (!ftrace_probe_registered
)
2966 for (i
= 0; i
< FTRACE_FUNC_HASHSIZE
; i
++) {
2967 struct hlist_head
*hhd
= &ftrace_func_hash
[i
];
2972 /* no more funcs left */
2973 ret
= __unregister_ftrace_function(&trace_probe_ops
);
2975 ftrace_shutdown(&trace_probe_ops
, 0);
2977 ftrace_probe_registered
= 0;
2981 static void ftrace_free_entry(struct ftrace_func_probe
*entry
)
2983 if (entry
->ops
->free
)
2984 entry
->ops
->free(entry
->ops
, entry
->ip
, &entry
->data
);
2989 register_ftrace_function_probe(char *glob
, struct ftrace_probe_ops
*ops
,
2992 struct ftrace_func_probe
*entry
;
2993 struct ftrace_hash
**orig_hash
= &trace_probe_ops
.filter_hash
;
2994 struct ftrace_hash
*hash
;
2995 struct ftrace_page
*pg
;
2996 struct dyn_ftrace
*rec
;
3003 type
= filter_parse_regex(glob
, strlen(glob
), &search
, ¬);
3004 len
= strlen(search
);
3006 /* we do not support '!' for function probes */
3010 mutex_lock(&ftrace_lock
);
3012 hash
= alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS
, *orig_hash
);
3018 if (unlikely(ftrace_disabled
)) {
3023 do_for_each_ftrace_rec(pg
, rec
) {
3025 if (!ftrace_match_record(rec
, NULL
, search
, len
, type
))
3028 entry
= kmalloc(sizeof(*entry
), GFP_KERNEL
);
3030 /* If we did not process any, then return error */
3041 * The caller might want to do something special
3042 * for each function we find. We call the callback
3043 * to give the caller an opportunity to do so.
3046 if (ops
->init(ops
, rec
->ip
, &entry
->data
) < 0) {
3047 /* caller does not like this func */
3053 ret
= enter_record(hash
, rec
, 0);
3061 entry
->ip
= rec
->ip
;
3063 key
= hash_long(entry
->ip
, FTRACE_HASH_BITS
);
3064 hlist_add_head_rcu(&entry
->node
, &ftrace_func_hash
[key
]);
3066 } while_for_each_ftrace_rec();
3068 ret
= ftrace_hash_move(&trace_probe_ops
, 1, orig_hash
, hash
);
3072 __enable_ftrace_function_probe();
3075 mutex_unlock(&ftrace_lock
);
3076 free_ftrace_hash(hash
);
3082 PROBE_TEST_FUNC
= 1,
3087 __unregister_ftrace_function_probe(char *glob
, struct ftrace_probe_ops
*ops
,
3088 void *data
, int flags
)
3090 struct ftrace_func_entry
*rec_entry
;
3091 struct ftrace_func_probe
*entry
;
3092 struct ftrace_func_probe
*p
;
3093 struct ftrace_hash
**orig_hash
= &trace_probe_ops
.filter_hash
;
3094 struct list_head free_list
;
3095 struct ftrace_hash
*hash
;
3096 struct hlist_node
*n
, *tmp
;
3097 char str
[KSYM_SYMBOL_LEN
];
3098 int type
= MATCH_FULL
;
3102 if (glob
&& (strcmp(glob
, "*") == 0 || !strlen(glob
)))
3107 type
= filter_parse_regex(glob
, strlen(glob
), &search
, ¬);
3108 len
= strlen(search
);
3110 /* we do not support '!' for function probes */
3115 mutex_lock(&ftrace_lock
);
3117 hash
= alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS
, *orig_hash
);
3119 /* Hmm, should report this somehow */
3122 INIT_LIST_HEAD(&free_list
);
3124 for (i
= 0; i
< FTRACE_FUNC_HASHSIZE
; i
++) {
3125 struct hlist_head
*hhd
= &ftrace_func_hash
[i
];
3127 hlist_for_each_entry_safe(entry
, n
, tmp
, hhd
, node
) {
3129 /* break up if statements for readability */
3130 if ((flags
& PROBE_TEST_FUNC
) && entry
->ops
!= ops
)
3133 if ((flags
& PROBE_TEST_DATA
) && entry
->data
!= data
)
3136 /* do this last, since it is the most expensive */
3138 kallsyms_lookup(entry
->ip
, NULL
, NULL
,
3140 if (!ftrace_match(str
, glob
, len
, type
))
3144 rec_entry
= ftrace_lookup_ip(hash
, entry
->ip
);
3145 /* It is possible more than one entry had this ip */
3147 free_hash_entry(hash
, rec_entry
);
3149 hlist_del_rcu(&entry
->node
);
3150 list_add(&entry
->free_list
, &free_list
);
3153 __disable_ftrace_function_probe();
3155 * Remove after the disable is called. Otherwise, if the last
3156 * probe is removed, a null hash means *all enabled*.
3158 ftrace_hash_move(&trace_probe_ops
, 1, orig_hash
, hash
);
3159 synchronize_sched();
3160 list_for_each_entry_safe(entry
, p
, &free_list
, free_list
) {
3161 list_del(&entry
->free_list
);
3162 ftrace_free_entry(entry
);
3166 mutex_unlock(&ftrace_lock
);
3167 free_ftrace_hash(hash
);
3171 unregister_ftrace_function_probe(char *glob
, struct ftrace_probe_ops
*ops
,
3174 __unregister_ftrace_function_probe(glob
, ops
, data
,
3175 PROBE_TEST_FUNC
| PROBE_TEST_DATA
);
3179 unregister_ftrace_function_probe_func(char *glob
, struct ftrace_probe_ops
*ops
)
3181 __unregister_ftrace_function_probe(glob
, ops
, NULL
, PROBE_TEST_FUNC
);
3184 void unregister_ftrace_function_probe_all(char *glob
)
3186 __unregister_ftrace_function_probe(glob
, NULL
, NULL
, 0);
3189 static LIST_HEAD(ftrace_commands
);
3190 static DEFINE_MUTEX(ftrace_cmd_mutex
);
3192 int register_ftrace_command(struct ftrace_func_command
*cmd
)
3194 struct ftrace_func_command
*p
;
3197 mutex_lock(&ftrace_cmd_mutex
);
3198 list_for_each_entry(p
, &ftrace_commands
, list
) {
3199 if (strcmp(cmd
->name
, p
->name
) == 0) {
3204 list_add(&cmd
->list
, &ftrace_commands
);
3206 mutex_unlock(&ftrace_cmd_mutex
);
3211 int unregister_ftrace_command(struct ftrace_func_command
*cmd
)
3213 struct ftrace_func_command
*p
, *n
;
3216 mutex_lock(&ftrace_cmd_mutex
);
3217 list_for_each_entry_safe(p
, n
, &ftrace_commands
, list
) {
3218 if (strcmp(cmd
->name
, p
->name
) == 0) {
3220 list_del_init(&p
->list
);
3225 mutex_unlock(&ftrace_cmd_mutex
);
3230 static int ftrace_process_regex(struct ftrace_hash
*hash
,
3231 char *buff
, int len
, int enable
)
3233 char *func
, *command
, *next
= buff
;
3234 struct ftrace_func_command
*p
;
3237 func
= strsep(&next
, ":");
3240 ret
= ftrace_match_records(hash
, func
, len
);
3250 command
= strsep(&next
, ":");
3252 mutex_lock(&ftrace_cmd_mutex
);
3253 list_for_each_entry(p
, &ftrace_commands
, list
) {
3254 if (strcmp(p
->name
, command
) == 0) {
3255 ret
= p
->func(hash
, func
, command
, next
, enable
);
3260 mutex_unlock(&ftrace_cmd_mutex
);
3266 ftrace_regex_write(struct file
*file
, const char __user
*ubuf
,
3267 size_t cnt
, loff_t
*ppos
, int enable
)
3269 struct ftrace_iterator
*iter
;
3270 struct trace_parser
*parser
;
3276 mutex_lock(&ftrace_regex_lock
);
3279 if (unlikely(ftrace_disabled
))
3282 if (file
->f_mode
& FMODE_READ
) {
3283 struct seq_file
*m
= file
->private_data
;
3286 iter
= file
->private_data
;
3288 parser
= &iter
->parser
;
3289 read
= trace_get_user(parser
, ubuf
, cnt
, ppos
);
3291 if (read
>= 0 && trace_parser_loaded(parser
) &&
3292 !trace_parser_cont(parser
)) {
3293 ret
= ftrace_process_regex(iter
->hash
, parser
->buffer
,
3294 parser
->idx
, enable
);
3295 trace_parser_clear(parser
);
3302 mutex_unlock(&ftrace_regex_lock
);
3308 ftrace_filter_write(struct file
*file
, const char __user
*ubuf
,
3309 size_t cnt
, loff_t
*ppos
)
3311 return ftrace_regex_write(file
, ubuf
, cnt
, ppos
, 1);
3315 ftrace_notrace_write(struct file
*file
, const char __user
*ubuf
,
3316 size_t cnt
, loff_t
*ppos
)
3318 return ftrace_regex_write(file
, ubuf
, cnt
, ppos
, 0);
3322 ftrace_match_addr(struct ftrace_hash
*hash
, unsigned long ip
, int remove
)
3324 struct ftrace_func_entry
*entry
;
3326 if (!ftrace_location(ip
))
3330 entry
= ftrace_lookup_ip(hash
, ip
);
3333 free_hash_entry(hash
, entry
);
3337 return add_hash_entry(hash
, ip
);
3341 ftrace_set_hash(struct ftrace_ops
*ops
, unsigned char *buf
, int len
,
3342 unsigned long ip
, int remove
, int reset
, int enable
)
3344 struct ftrace_hash
**orig_hash
;
3345 struct ftrace_hash
*hash
;
3348 /* All global ops uses the global ops filters */
3349 if (ops
->flags
& FTRACE_OPS_FL_GLOBAL
)
3352 if (unlikely(ftrace_disabled
))
3356 orig_hash
= &ops
->filter_hash
;
3358 orig_hash
= &ops
->notrace_hash
;
3360 hash
= alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS
, *orig_hash
);
3364 mutex_lock(&ftrace_regex_lock
);
3366 ftrace_filter_reset(hash
);
3367 if (buf
&& !ftrace_match_records(hash
, buf
, len
)) {
3369 goto out_regex_unlock
;
3372 ret
= ftrace_match_addr(hash
, ip
, remove
);
3374 goto out_regex_unlock
;
3377 mutex_lock(&ftrace_lock
);
3378 ret
= ftrace_hash_move(ops
, enable
, orig_hash
, hash
);
3379 if (!ret
&& ops
->flags
& FTRACE_OPS_FL_ENABLED
3381 ftrace_run_update_code(FTRACE_UPDATE_CALLS
);
3383 mutex_unlock(&ftrace_lock
);
3386 mutex_unlock(&ftrace_regex_lock
);
3388 free_ftrace_hash(hash
);
3393 ftrace_set_addr(struct ftrace_ops
*ops
, unsigned long ip
, int remove
,
3394 int reset
, int enable
)
3396 return ftrace_set_hash(ops
, 0, 0, ip
, remove
, reset
, enable
);
3400 * ftrace_set_filter_ip - set a function to filter on in ftrace by address
3401 * @ops - the ops to set the filter with
3402 * @ip - the address to add to or remove from the filter.
3403 * @remove - non zero to remove the ip from the filter
3404 * @reset - non zero to reset all filters before applying this filter.
3406 * Filters denote which functions should be enabled when tracing is enabled
3407 * If @ip is NULL, it failes to update filter.
3409 int ftrace_set_filter_ip(struct ftrace_ops
*ops
, unsigned long ip
,
3410 int remove
, int reset
)
3412 return ftrace_set_addr(ops
, ip
, remove
, reset
, 1);
3414 EXPORT_SYMBOL_GPL(ftrace_set_filter_ip
);
3417 ftrace_set_regex(struct ftrace_ops
*ops
, unsigned char *buf
, int len
,
3418 int reset
, int enable
)
3420 return ftrace_set_hash(ops
, buf
, len
, 0, 0, reset
, enable
);
3424 * ftrace_set_filter - set a function to filter on in ftrace
3425 * @ops - the ops to set the filter with
3426 * @buf - the string that holds the function filter text.
3427 * @len - the length of the string.
3428 * @reset - non zero to reset all filters before applying this filter.
3430 * Filters denote which functions should be enabled when tracing is enabled.
3431 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
3433 int ftrace_set_filter(struct ftrace_ops
*ops
, unsigned char *buf
,
3436 return ftrace_set_regex(ops
, buf
, len
, reset
, 1);
3438 EXPORT_SYMBOL_GPL(ftrace_set_filter
);
3441 * ftrace_set_notrace - set a function to not trace in ftrace
3442 * @ops - the ops to set the notrace filter with
3443 * @buf - the string that holds the function notrace text.
3444 * @len - the length of the string.
3445 * @reset - non zero to reset all filters before applying this filter.
3447 * Notrace Filters denote which functions should not be enabled when tracing
3448 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
3451 int ftrace_set_notrace(struct ftrace_ops
*ops
, unsigned char *buf
,
3454 return ftrace_set_regex(ops
, buf
, len
, reset
, 0);
3456 EXPORT_SYMBOL_GPL(ftrace_set_notrace
);
3458 * ftrace_set_filter - set a function to filter on in ftrace
3459 * @ops - the ops to set the filter with
3460 * @buf - the string that holds the function filter text.
3461 * @len - the length of the string.
3462 * @reset - non zero to reset all filters before applying this filter.
3464 * Filters denote which functions should be enabled when tracing is enabled.
3465 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
3467 void ftrace_set_global_filter(unsigned char *buf
, int len
, int reset
)
3469 ftrace_set_regex(&global_ops
, buf
, len
, reset
, 1);
3471 EXPORT_SYMBOL_GPL(ftrace_set_global_filter
);
3474 * ftrace_set_notrace - set a function to not trace in ftrace
3475 * @ops - the ops to set the notrace filter with
3476 * @buf - the string that holds the function notrace text.
3477 * @len - the length of the string.
3478 * @reset - non zero to reset all filters before applying this filter.
3480 * Notrace Filters denote which functions should not be enabled when tracing
3481 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
3484 void ftrace_set_global_notrace(unsigned char *buf
, int len
, int reset
)
3486 ftrace_set_regex(&global_ops
, buf
, len
, reset
, 0);
3488 EXPORT_SYMBOL_GPL(ftrace_set_global_notrace
);
3491 * command line interface to allow users to set filters on boot up.
3493 #define FTRACE_FILTER_SIZE COMMAND_LINE_SIZE
3494 static char ftrace_notrace_buf
[FTRACE_FILTER_SIZE
] __initdata
;
3495 static char ftrace_filter_buf
[FTRACE_FILTER_SIZE
] __initdata
;
3497 static int __init
set_ftrace_notrace(char *str
)
3499 strncpy(ftrace_notrace_buf
, str
, FTRACE_FILTER_SIZE
);
3502 __setup("ftrace_notrace=", set_ftrace_notrace
);
3504 static int __init
set_ftrace_filter(char *str
)
3506 strncpy(ftrace_filter_buf
, str
, FTRACE_FILTER_SIZE
);
3509 __setup("ftrace_filter=", set_ftrace_filter
);
3511 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
3512 static char ftrace_graph_buf
[FTRACE_FILTER_SIZE
] __initdata
;
3513 static int ftrace_set_func(unsigned long *array
, int *idx
, char *buffer
);
3515 static int __init
set_graph_function(char *str
)
3517 strlcpy(ftrace_graph_buf
, str
, FTRACE_FILTER_SIZE
);
3520 __setup("ftrace_graph_filter=", set_graph_function
);
3522 static void __init
set_ftrace_early_graph(char *buf
)
3528 func
= strsep(&buf
, ",");
3529 /* we allow only one expression at a time */
3530 ret
= ftrace_set_func(ftrace_graph_funcs
, &ftrace_graph_count
,
3533 printk(KERN_DEBUG
"ftrace: function %s not "
3534 "traceable\n", func
);
3537 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
3540 ftrace_set_early_filter(struct ftrace_ops
*ops
, char *buf
, int enable
)
3545 func
= strsep(&buf
, ",");
3546 ftrace_set_regex(ops
, func
, strlen(func
), 0, enable
);
3550 static void __init
set_ftrace_early_filters(void)
3552 if (ftrace_filter_buf
[0])
3553 ftrace_set_early_filter(&global_ops
, ftrace_filter_buf
, 1);
3554 if (ftrace_notrace_buf
[0])
3555 ftrace_set_early_filter(&global_ops
, ftrace_notrace_buf
, 0);
3556 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
3557 if (ftrace_graph_buf
[0])
3558 set_ftrace_early_graph(ftrace_graph_buf
);
3559 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
3562 int ftrace_regex_release(struct inode
*inode
, struct file
*file
)
3564 struct seq_file
*m
= (struct seq_file
*)file
->private_data
;
3565 struct ftrace_iterator
*iter
;
3566 struct ftrace_hash
**orig_hash
;
3567 struct trace_parser
*parser
;
3571 mutex_lock(&ftrace_regex_lock
);
3572 if (file
->f_mode
& FMODE_READ
) {
3575 seq_release(inode
, file
);
3577 iter
= file
->private_data
;
3579 parser
= &iter
->parser
;
3580 if (trace_parser_loaded(parser
)) {
3581 parser
->buffer
[parser
->idx
] = 0;
3582 ftrace_match_records(iter
->hash
, parser
->buffer
, parser
->idx
);
3585 trace_parser_put(parser
);
3587 if (file
->f_mode
& FMODE_WRITE
) {
3588 filter_hash
= !!(iter
->flags
& FTRACE_ITER_FILTER
);
3591 orig_hash
= &iter
->ops
->filter_hash
;
3593 orig_hash
= &iter
->ops
->notrace_hash
;
3595 mutex_lock(&ftrace_lock
);
3596 ret
= ftrace_hash_move(iter
->ops
, filter_hash
,
3597 orig_hash
, iter
->hash
);
3598 if (!ret
&& (iter
->ops
->flags
& FTRACE_OPS_FL_ENABLED
)
3600 ftrace_run_update_code(FTRACE_UPDATE_CALLS
);
3602 mutex_unlock(&ftrace_lock
);
3604 free_ftrace_hash(iter
->hash
);
3607 mutex_unlock(&ftrace_regex_lock
);
3611 static const struct file_operations ftrace_avail_fops
= {
3612 .open
= ftrace_avail_open
,
3614 .llseek
= seq_lseek
,
3615 .release
= seq_release_private
,
3618 static const struct file_operations ftrace_enabled_fops
= {
3619 .open
= ftrace_enabled_open
,
3621 .llseek
= seq_lseek
,
3622 .release
= seq_release_private
,
3625 static const struct file_operations ftrace_filter_fops
= {
3626 .open
= ftrace_filter_open
,
3628 .write
= ftrace_filter_write
,
3629 .llseek
= ftrace_regex_lseek
,
3630 .release
= ftrace_regex_release
,
3633 static const struct file_operations ftrace_notrace_fops
= {
3634 .open
= ftrace_notrace_open
,
3636 .write
= ftrace_notrace_write
,
3637 .llseek
= ftrace_regex_lseek
,
3638 .release
= ftrace_regex_release
,
3641 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
3643 static DEFINE_MUTEX(graph_lock
);
3645 int ftrace_graph_count
;
3646 int ftrace_graph_filter_enabled
;
3647 unsigned long ftrace_graph_funcs
[FTRACE_GRAPH_MAX_FUNCS
] __read_mostly
;
3650 __g_next(struct seq_file
*m
, loff_t
*pos
)
3652 if (*pos
>= ftrace_graph_count
)
3654 return &ftrace_graph_funcs
[*pos
];
3658 g_next(struct seq_file
*m
, void *v
, loff_t
*pos
)
3661 return __g_next(m
, pos
);
3664 static void *g_start(struct seq_file
*m
, loff_t
*pos
)
3666 mutex_lock(&graph_lock
);
3668 /* Nothing, tell g_show to print all functions are enabled */
3669 if (!ftrace_graph_filter_enabled
&& !*pos
)
3672 return __g_next(m
, pos
);
3675 static void g_stop(struct seq_file
*m
, void *p
)
3677 mutex_unlock(&graph_lock
);
3680 static int g_show(struct seq_file
*m
, void *v
)
3682 unsigned long *ptr
= v
;
3687 if (ptr
== (unsigned long *)1) {
3688 seq_printf(m
, "#### all functions enabled ####\n");
3692 seq_printf(m
, "%ps\n", (void *)*ptr
);
3697 static const struct seq_operations ftrace_graph_seq_ops
= {
3705 ftrace_graph_open(struct inode
*inode
, struct file
*file
)
3709 if (unlikely(ftrace_disabled
))
3712 mutex_lock(&graph_lock
);
3713 if ((file
->f_mode
& FMODE_WRITE
) &&
3714 (file
->f_flags
& O_TRUNC
)) {
3715 ftrace_graph_filter_enabled
= 0;
3716 ftrace_graph_count
= 0;
3717 memset(ftrace_graph_funcs
, 0, sizeof(ftrace_graph_funcs
));
3719 mutex_unlock(&graph_lock
);
3721 if (file
->f_mode
& FMODE_READ
)
3722 ret
= seq_open(file
, &ftrace_graph_seq_ops
);
3728 ftrace_graph_release(struct inode
*inode
, struct file
*file
)
3730 if (file
->f_mode
& FMODE_READ
)
3731 seq_release(inode
, file
);
3736 ftrace_set_func(unsigned long *array
, int *idx
, char *buffer
)
3738 struct dyn_ftrace
*rec
;
3739 struct ftrace_page
*pg
;
3748 type
= filter_parse_regex(buffer
, strlen(buffer
), &search
, ¬);
3749 if (!not && *idx
>= FTRACE_GRAPH_MAX_FUNCS
)
3752 search_len
= strlen(search
);
3754 mutex_lock(&ftrace_lock
);
3756 if (unlikely(ftrace_disabled
)) {
3757 mutex_unlock(&ftrace_lock
);
3761 do_for_each_ftrace_rec(pg
, rec
) {
3763 if (ftrace_match_record(rec
, NULL
, search
, search_len
, type
)) {
3764 /* if it is in the array */
3766 for (i
= 0; i
< *idx
; i
++) {
3767 if (array
[i
] == rec
->ip
) {
3776 array
[(*idx
)++] = rec
->ip
;
3777 if (*idx
>= FTRACE_GRAPH_MAX_FUNCS
)
3782 array
[i
] = array
[--(*idx
)];
3788 } while_for_each_ftrace_rec();
3790 mutex_unlock(&ftrace_lock
);
3795 ftrace_graph_filter_enabled
= 1;
3800 ftrace_graph_write(struct file
*file
, const char __user
*ubuf
,
3801 size_t cnt
, loff_t
*ppos
)
3803 struct trace_parser parser
;
3809 mutex_lock(&graph_lock
);
3811 if (trace_parser_get_init(&parser
, FTRACE_BUFF_MAX
)) {
3816 read
= trace_get_user(&parser
, ubuf
, cnt
, ppos
);
3818 if (read
>= 0 && trace_parser_loaded((&parser
))) {
3819 parser
.buffer
[parser
.idx
] = 0;
3821 /* we allow only one expression at a time */
3822 ret
= ftrace_set_func(ftrace_graph_funcs
, &ftrace_graph_count
,
3831 trace_parser_put(&parser
);
3833 mutex_unlock(&graph_lock
);
3838 static const struct file_operations ftrace_graph_fops
= {
3839 .open
= ftrace_graph_open
,
3841 .write
= ftrace_graph_write
,
3842 .release
= ftrace_graph_release
,
3843 .llseek
= seq_lseek
,
3845 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
3847 static __init
int ftrace_init_dyn_debugfs(struct dentry
*d_tracer
)
3850 trace_create_file("available_filter_functions", 0444,
3851 d_tracer
, NULL
, &ftrace_avail_fops
);
3853 trace_create_file("enabled_functions", 0444,
3854 d_tracer
, NULL
, &ftrace_enabled_fops
);
3856 trace_create_file("set_ftrace_filter", 0644, d_tracer
,
3857 NULL
, &ftrace_filter_fops
);
3859 trace_create_file("set_ftrace_notrace", 0644, d_tracer
,
3860 NULL
, &ftrace_notrace_fops
);
3862 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
3863 trace_create_file("set_graph_function", 0444, d_tracer
,
3865 &ftrace_graph_fops
);
3866 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
3871 static int ftrace_cmp_ips(const void *a
, const void *b
)
3873 const unsigned long *ipa
= a
;
3874 const unsigned long *ipb
= b
;
3883 static void ftrace_swap_ips(void *a
, void *b
, int size
)
3885 unsigned long *ipa
= a
;
3886 unsigned long *ipb
= b
;
3894 static int ftrace_process_locs(struct module
*mod
,
3895 unsigned long *start
,
3898 struct ftrace_page
*start_pg
;
3899 struct ftrace_page
*pg
;
3900 struct dyn_ftrace
*rec
;
3901 unsigned long count
;
3904 unsigned long flags
= 0; /* Shut up gcc */
3907 count
= end
- start
;
3912 sort(start
, count
, sizeof(*start
),
3913 ftrace_cmp_ips
, ftrace_swap_ips
);
3915 start_pg
= ftrace_allocate_pages(count
);
3919 mutex_lock(&ftrace_lock
);
3922 * Core and each module needs their own pages, as
3923 * modules will free them when they are removed.
3924 * Force a new page to be allocated for modules.
3927 WARN_ON(ftrace_pages
|| ftrace_pages_start
);
3928 /* First initialization */
3929 ftrace_pages
= ftrace_pages_start
= start_pg
;
3934 if (WARN_ON(ftrace_pages
->next
)) {
3935 /* Hmm, we have free pages? */
3936 while (ftrace_pages
->next
)
3937 ftrace_pages
= ftrace_pages
->next
;
3940 ftrace_pages
->next
= start_pg
;
3946 addr
= ftrace_call_adjust(*p
++);
3948 * Some architecture linkers will pad between
3949 * the different mcount_loc sections of different
3950 * object files to satisfy alignments.
3951 * Skip any NULL pointers.
3956 if (pg
->index
== pg
->size
) {
3957 /* We should have allocated enough */
3958 if (WARN_ON(!pg
->next
))
3963 rec
= &pg
->records
[pg
->index
++];
3967 /* We should have used all pages */
3970 /* Assign the last page to ftrace_pages */
3973 /* These new locations need to be initialized */
3974 ftrace_new_pgs
= start_pg
;
3977 * We only need to disable interrupts on start up
3978 * because we are modifying code that an interrupt
3979 * may execute, and the modification is not atomic.
3980 * But for modules, nothing runs the code we modify
3981 * until we are finished with it, and there's no
3982 * reason to cause large interrupt latencies while we do it.
3985 local_irq_save(flags
);
3986 ftrace_update_code(mod
);
3988 local_irq_restore(flags
);
3991 mutex_unlock(&ftrace_lock
);
3996 #ifdef CONFIG_MODULES
3998 #define next_to_ftrace_page(p) container_of(p, struct ftrace_page, next)
4000 void ftrace_release_mod(struct module
*mod
)
4002 struct dyn_ftrace
*rec
;
4003 struct ftrace_page
**last_pg
;
4004 struct ftrace_page
*pg
;
4007 mutex_lock(&ftrace_lock
);
4009 if (ftrace_disabled
)
4013 * Each module has its own ftrace_pages, remove
4014 * them from the list.
4016 last_pg
= &ftrace_pages_start
;
4017 for (pg
= ftrace_pages_start
; pg
; pg
= *last_pg
) {
4018 rec
= &pg
->records
[0];
4019 if (within_module_core(rec
->ip
, mod
)) {
4021 * As core pages are first, the first
4022 * page should never be a module page.
4024 if (WARN_ON(pg
== ftrace_pages_start
))
4027 /* Check if we are deleting the last page */
4028 if (pg
== ftrace_pages
)
4029 ftrace_pages
= next_to_ftrace_page(last_pg
);
4031 *last_pg
= pg
->next
;
4032 order
= get_count_order(pg
->size
/ ENTRIES_PER_PAGE
);
4033 free_pages((unsigned long)pg
->records
, order
);
4036 last_pg
= &pg
->next
;
4039 mutex_unlock(&ftrace_lock
);
4042 static void ftrace_init_module(struct module
*mod
,
4043 unsigned long *start
, unsigned long *end
)
4045 if (ftrace_disabled
|| start
== end
)
4047 ftrace_process_locs(mod
, start
, end
);
4050 static int ftrace_module_notify_enter(struct notifier_block
*self
,
4051 unsigned long val
, void *data
)
4053 struct module
*mod
= data
;
4055 if (val
== MODULE_STATE_COMING
)
4056 ftrace_init_module(mod
, mod
->ftrace_callsites
,
4057 mod
->ftrace_callsites
+
4058 mod
->num_ftrace_callsites
);
4062 static int ftrace_module_notify_exit(struct notifier_block
*self
,
4063 unsigned long val
, void *data
)
4065 struct module
*mod
= data
;
4067 if (val
== MODULE_STATE_GOING
)
4068 ftrace_release_mod(mod
);
4073 static int ftrace_module_notify_enter(struct notifier_block
*self
,
4074 unsigned long val
, void *data
)
4078 static int ftrace_module_notify_exit(struct notifier_block
*self
,
4079 unsigned long val
, void *data
)
4083 #endif /* CONFIG_MODULES */
4085 struct notifier_block ftrace_module_enter_nb
= {
4086 .notifier_call
= ftrace_module_notify_enter
,
4087 .priority
= INT_MAX
, /* Run before anything that can use kprobes */
4090 struct notifier_block ftrace_module_exit_nb
= {
4091 .notifier_call
= ftrace_module_notify_exit
,
4092 .priority
= INT_MIN
, /* Run after anything that can remove kprobes */
4095 extern unsigned long __start_mcount_loc
[];
4096 extern unsigned long __stop_mcount_loc
[];
4098 void __init
ftrace_init(void)
4100 unsigned long count
, addr
, flags
;
4103 /* Keep the ftrace pointer to the stub */
4104 addr
= (unsigned long)ftrace_stub
;
4106 local_irq_save(flags
);
4107 ftrace_dyn_arch_init(&addr
);
4108 local_irq_restore(flags
);
4110 /* ftrace_dyn_arch_init places the return code in addr */
4114 count
= __stop_mcount_loc
- __start_mcount_loc
;
4116 ret
= ftrace_dyn_table_alloc(count
);
4120 last_ftrace_enabled
= ftrace_enabled
= 1;
4122 ret
= ftrace_process_locs(NULL
,
4126 ret
= register_module_notifier(&ftrace_module_enter_nb
);
4128 pr_warning("Failed to register trace ftrace module enter notifier\n");
4130 ret
= register_module_notifier(&ftrace_module_exit_nb
);
4132 pr_warning("Failed to register trace ftrace module exit notifier\n");
4134 set_ftrace_early_filters();
4138 ftrace_disabled
= 1;
4143 static struct ftrace_ops global_ops
= {
4144 .func
= ftrace_stub
,
4145 .flags
= FTRACE_OPS_FL_RECURSION_SAFE
,
4148 static int __init
ftrace_nodyn_init(void)
4153 core_initcall(ftrace_nodyn_init
);
4155 static inline int ftrace_init_dyn_debugfs(struct dentry
*d_tracer
) { return 0; }
4156 static inline void ftrace_startup_enable(int command
) { }
4157 /* Keep as macros so we do not need to define the commands */
4158 # define ftrace_startup(ops, command) \
4160 (ops)->flags |= FTRACE_OPS_FL_ENABLED; \
4163 # define ftrace_shutdown(ops, command) do { } while (0)
4164 # define ftrace_startup_sysctl() do { } while (0)
4165 # define ftrace_shutdown_sysctl() do { } while (0)
4168 ftrace_ops_test(struct ftrace_ops
*ops
, unsigned long ip
)
4173 #endif /* CONFIG_DYNAMIC_FTRACE */
4176 ftrace_ops_control_func(unsigned long ip
, unsigned long parent_ip
,
4177 struct ftrace_ops
*op
, struct pt_regs
*regs
)
4179 if (unlikely(trace_recursion_test(TRACE_CONTROL_BIT
)))
4183 * Some of the ops may be dynamically allocated,
4184 * they must be freed after a synchronize_sched().
4186 preempt_disable_notrace();
4187 trace_recursion_set(TRACE_CONTROL_BIT
);
4188 do_for_each_ftrace_op(op
, ftrace_control_list
) {
4189 if (!ftrace_function_local_disabled(op
) &&
4190 ftrace_ops_test(op
, ip
))
4191 op
->func(ip
, parent_ip
, op
, regs
);
4192 } while_for_each_ftrace_op(op
);
4193 trace_recursion_clear(TRACE_CONTROL_BIT
);
4194 preempt_enable_notrace();
4197 static struct ftrace_ops control_ops
= {
4198 .func
= ftrace_ops_control_func
,
4199 .flags
= FTRACE_OPS_FL_RECURSION_SAFE
,
4203 __ftrace_ops_list_func(unsigned long ip
, unsigned long parent_ip
,
4204 struct ftrace_ops
*ignored
, struct pt_regs
*regs
)
4206 struct ftrace_ops
*op
;
4209 if (function_trace_stop
)
4212 bit
= trace_test_and_set_recursion(TRACE_LIST_START
, TRACE_LIST_MAX
);
4217 * Some of the ops may be dynamically allocated,
4218 * they must be freed after a synchronize_sched().
4220 preempt_disable_notrace();
4221 do_for_each_ftrace_op(op
, ftrace_ops_list
) {
4222 if (ftrace_ops_test(op
, ip
))
4223 op
->func(ip
, parent_ip
, op
, regs
);
4224 } while_for_each_ftrace_op(op
);
4225 preempt_enable_notrace();
4226 trace_clear_recursion(bit
);
4230 * Some archs only support passing ip and parent_ip. Even though
4231 * the list function ignores the op parameter, we do not want any
4232 * C side effects, where a function is called without the caller
4233 * sending a third parameter.
4234 * Archs are to support both the regs and ftrace_ops at the same time.
4235 * If they support ftrace_ops, it is assumed they support regs.
4236 * If call backs want to use regs, they must either check for regs
4237 * being NULL, or CONFIG_DYNAMIC_FTRACE_WITH_REGS.
4238 * Note, CONFIG_DYNAMIC_FTRACE_WITH_REGS expects a full regs to be saved.
4239 * An architecture can pass partial regs with ftrace_ops and still
4240 * set the ARCH_SUPPORT_FTARCE_OPS.
4242 #if ARCH_SUPPORTS_FTRACE_OPS
4243 static void ftrace_ops_list_func(unsigned long ip
, unsigned long parent_ip
,
4244 struct ftrace_ops
*op
, struct pt_regs
*regs
)
4246 __ftrace_ops_list_func(ip
, parent_ip
, NULL
, regs
);
4249 static void ftrace_ops_no_ops(unsigned long ip
, unsigned long parent_ip
)
4251 __ftrace_ops_list_func(ip
, parent_ip
, NULL
, NULL
);
4255 static void clear_ftrace_swapper(void)
4257 struct task_struct
*p
;
4261 for_each_online_cpu(cpu
) {
4263 clear_tsk_trace_trace(p
);
4268 static void set_ftrace_swapper(void)
4270 struct task_struct
*p
;
4274 for_each_online_cpu(cpu
) {
4276 set_tsk_trace_trace(p
);
4281 static void clear_ftrace_pid(struct pid
*pid
)
4283 struct task_struct
*p
;
4286 do_each_pid_task(pid
, PIDTYPE_PID
, p
) {
4287 clear_tsk_trace_trace(p
);
4288 } while_each_pid_task(pid
, PIDTYPE_PID
, p
);
4294 static void set_ftrace_pid(struct pid
*pid
)
4296 struct task_struct
*p
;
4299 do_each_pid_task(pid
, PIDTYPE_PID
, p
) {
4300 set_tsk_trace_trace(p
);
4301 } while_each_pid_task(pid
, PIDTYPE_PID
, p
);
4305 static void clear_ftrace_pid_task(struct pid
*pid
)
4307 if (pid
== ftrace_swapper_pid
)
4308 clear_ftrace_swapper();
4310 clear_ftrace_pid(pid
);
4313 static void set_ftrace_pid_task(struct pid
*pid
)
4315 if (pid
== ftrace_swapper_pid
)
4316 set_ftrace_swapper();
4318 set_ftrace_pid(pid
);
4321 static int ftrace_pid_add(int p
)
4324 struct ftrace_pid
*fpid
;
4327 mutex_lock(&ftrace_lock
);
4330 pid
= ftrace_swapper_pid
;
4332 pid
= find_get_pid(p
);
4339 list_for_each_entry(fpid
, &ftrace_pids
, list
)
4340 if (fpid
->pid
== pid
)
4345 fpid
= kmalloc(sizeof(*fpid
), GFP_KERNEL
);
4349 list_add(&fpid
->list
, &ftrace_pids
);
4352 set_ftrace_pid_task(pid
);
4354 ftrace_update_pid_func();
4355 ftrace_startup_enable(0);
4357 mutex_unlock(&ftrace_lock
);
4361 if (pid
!= ftrace_swapper_pid
)
4365 mutex_unlock(&ftrace_lock
);
4369 static void ftrace_pid_reset(void)
4371 struct ftrace_pid
*fpid
, *safe
;
4373 mutex_lock(&ftrace_lock
);
4374 list_for_each_entry_safe(fpid
, safe
, &ftrace_pids
, list
) {
4375 struct pid
*pid
= fpid
->pid
;
4377 clear_ftrace_pid_task(pid
);
4379 list_del(&fpid
->list
);
4383 ftrace_update_pid_func();
4384 ftrace_startup_enable(0);
4386 mutex_unlock(&ftrace_lock
);
4389 static void *fpid_start(struct seq_file
*m
, loff_t
*pos
)
4391 mutex_lock(&ftrace_lock
);
4393 if (list_empty(&ftrace_pids
) && (!*pos
))
4396 return seq_list_start(&ftrace_pids
, *pos
);
4399 static void *fpid_next(struct seq_file
*m
, void *v
, loff_t
*pos
)
4404 return seq_list_next(v
, &ftrace_pids
, pos
);
4407 static void fpid_stop(struct seq_file
*m
, void *p
)
4409 mutex_unlock(&ftrace_lock
);
4412 static int fpid_show(struct seq_file
*m
, void *v
)
4414 const struct ftrace_pid
*fpid
= list_entry(v
, struct ftrace_pid
, list
);
4416 if (v
== (void *)1) {
4417 seq_printf(m
, "no pid\n");
4421 if (fpid
->pid
== ftrace_swapper_pid
)
4422 seq_printf(m
, "swapper tasks\n");
4424 seq_printf(m
, "%u\n", pid_vnr(fpid
->pid
));
4429 static const struct seq_operations ftrace_pid_sops
= {
4430 .start
= fpid_start
,
4437 ftrace_pid_open(struct inode
*inode
, struct file
*file
)
4441 if ((file
->f_mode
& FMODE_WRITE
) &&
4442 (file
->f_flags
& O_TRUNC
))
4445 if (file
->f_mode
& FMODE_READ
)
4446 ret
= seq_open(file
, &ftrace_pid_sops
);
4452 ftrace_pid_write(struct file
*filp
, const char __user
*ubuf
,
4453 size_t cnt
, loff_t
*ppos
)
4459 if (cnt
>= sizeof(buf
))
4462 if (copy_from_user(&buf
, ubuf
, cnt
))
4468 * Allow "echo > set_ftrace_pid" or "echo -n '' > set_ftrace_pid"
4469 * to clean the filter quietly.
4471 tmp
= strstrip(buf
);
4472 if (strlen(tmp
) == 0)
4475 ret
= kstrtol(tmp
, 10, &val
);
4479 ret
= ftrace_pid_add(val
);
4481 return ret
? ret
: cnt
;
4485 ftrace_pid_release(struct inode
*inode
, struct file
*file
)
4487 if (file
->f_mode
& FMODE_READ
)
4488 seq_release(inode
, file
);
4493 static const struct file_operations ftrace_pid_fops
= {
4494 .open
= ftrace_pid_open
,
4495 .write
= ftrace_pid_write
,
4497 .llseek
= seq_lseek
,
4498 .release
= ftrace_pid_release
,
4501 static __init
int ftrace_init_debugfs(void)
4503 struct dentry
*d_tracer
;
4505 d_tracer
= tracing_init_dentry();
4509 ftrace_init_dyn_debugfs(d_tracer
);
4511 trace_create_file("set_ftrace_pid", 0644, d_tracer
,
4512 NULL
, &ftrace_pid_fops
);
4514 ftrace_profile_debugfs(d_tracer
);
4518 fs_initcall(ftrace_init_debugfs
);
4521 * ftrace_kill - kill ftrace
4523 * This function should be used by panic code. It stops ftrace
4524 * but in a not so nice way. If you need to simply kill ftrace
4525 * from a non-atomic section, use ftrace_kill.
4527 void ftrace_kill(void)
4529 ftrace_disabled
= 1;
4531 clear_ftrace_function();
4535 * Test if ftrace is dead or not.
4537 int ftrace_is_dead(void)
4539 return ftrace_disabled
;
4543 * register_ftrace_function - register a function for profiling
4544 * @ops - ops structure that holds the function for profiling.
4546 * Register a function to be called by all functions in the
4549 * Note: @ops->func and all the functions it calls must be labeled
4550 * with "notrace", otherwise it will go into a
4553 int register_ftrace_function(struct ftrace_ops
*ops
)
4557 mutex_lock(&ftrace_lock
);
4559 ret
= __register_ftrace_function(ops
);
4561 ret
= ftrace_startup(ops
, 0);
4563 mutex_unlock(&ftrace_lock
);
4567 EXPORT_SYMBOL_GPL(register_ftrace_function
);
4570 * unregister_ftrace_function - unregister a function for profiling.
4571 * @ops - ops structure that holds the function to unregister
4573 * Unregister a function that was added to be called by ftrace profiling.
4575 int unregister_ftrace_function(struct ftrace_ops
*ops
)
4579 mutex_lock(&ftrace_lock
);
4580 ret
= __unregister_ftrace_function(ops
);
4582 ftrace_shutdown(ops
, 0);
4583 mutex_unlock(&ftrace_lock
);
4587 EXPORT_SYMBOL_GPL(unregister_ftrace_function
);
4590 ftrace_enable_sysctl(struct ctl_table
*table
, int write
,
4591 void __user
*buffer
, size_t *lenp
,
4596 mutex_lock(&ftrace_lock
);
4598 if (unlikely(ftrace_disabled
))
4601 ret
= proc_dointvec(table
, write
, buffer
, lenp
, ppos
);
4603 if (ret
|| !write
|| (last_ftrace_enabled
== !!ftrace_enabled
))
4606 last_ftrace_enabled
= !!ftrace_enabled
;
4608 if (ftrace_enabled
) {
4610 ftrace_startup_sysctl();
4612 /* we are starting ftrace again */
4613 if (ftrace_ops_list
!= &ftrace_list_end
) {
4614 if (ftrace_ops_list
->next
== &ftrace_list_end
)
4615 ftrace_trace_function
= ftrace_ops_list
->func
;
4617 ftrace_trace_function
= ftrace_ops_list_func
;
4621 /* stopping ftrace calls (just send to ftrace_stub) */
4622 ftrace_trace_function
= ftrace_stub
;
4624 ftrace_shutdown_sysctl();
4628 mutex_unlock(&ftrace_lock
);
4632 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
4634 static int ftrace_graph_active
;
4635 static struct notifier_block ftrace_suspend_notifier
;
4637 int ftrace_graph_entry_stub(struct ftrace_graph_ent
*trace
)
4642 /* The callbacks that hook a function */
4643 trace_func_graph_ret_t ftrace_graph_return
=
4644 (trace_func_graph_ret_t
)ftrace_stub
;
4645 trace_func_graph_ent_t ftrace_graph_entry
= ftrace_graph_entry_stub
;
4647 /* Try to assign a return stack array on FTRACE_RETSTACK_ALLOC_SIZE tasks. */
4648 static int alloc_retstack_tasklist(struct ftrace_ret_stack
**ret_stack_list
)
4652 unsigned long flags
;
4653 int start
= 0, end
= FTRACE_RETSTACK_ALLOC_SIZE
;
4654 struct task_struct
*g
, *t
;
4656 for (i
= 0; i
< FTRACE_RETSTACK_ALLOC_SIZE
; i
++) {
4657 ret_stack_list
[i
] = kmalloc(FTRACE_RETFUNC_DEPTH
4658 * sizeof(struct ftrace_ret_stack
),
4660 if (!ret_stack_list
[i
]) {
4668 read_lock_irqsave(&tasklist_lock
, flags
);
4669 do_each_thread(g
, t
) {
4675 if (t
->ret_stack
== NULL
) {
4676 atomic_set(&t
->tracing_graph_pause
, 0);
4677 atomic_set(&t
->trace_overrun
, 0);
4678 t
->curr_ret_stack
= -1;
4679 /* Make sure the tasks see the -1 first: */
4681 t
->ret_stack
= ret_stack_list
[start
++];
4683 } while_each_thread(g
, t
);
4686 read_unlock_irqrestore(&tasklist_lock
, flags
);
4688 for (i
= start
; i
< end
; i
++)
4689 kfree(ret_stack_list
[i
]);
4694 ftrace_graph_probe_sched_switch(void *ignore
,
4695 struct task_struct
*prev
, struct task_struct
*next
)
4697 unsigned long long timestamp
;
4701 * Does the user want to count the time a function was asleep.
4702 * If so, do not update the time stamps.
4704 if (trace_flags
& TRACE_ITER_SLEEP_TIME
)
4707 timestamp
= trace_clock_local();
4709 prev
->ftrace_timestamp
= timestamp
;
4711 /* only process tasks that we timestamped */
4712 if (!next
->ftrace_timestamp
)
4716 * Update all the counters in next to make up for the
4717 * time next was sleeping.
4719 timestamp
-= next
->ftrace_timestamp
;
4721 for (index
= next
->curr_ret_stack
; index
>= 0; index
--)
4722 next
->ret_stack
[index
].calltime
+= timestamp
;
4725 /* Allocate a return stack for each task */
4726 static int start_graph_tracing(void)
4728 struct ftrace_ret_stack
**ret_stack_list
;
4731 ret_stack_list
= kmalloc(FTRACE_RETSTACK_ALLOC_SIZE
*
4732 sizeof(struct ftrace_ret_stack
*),
4735 if (!ret_stack_list
)
4738 /* The cpu_boot init_task->ret_stack will never be freed */
4739 for_each_online_cpu(cpu
) {
4740 if (!idle_task(cpu
)->ret_stack
)
4741 ftrace_graph_init_idle_task(idle_task(cpu
), cpu
);
4745 ret
= alloc_retstack_tasklist(ret_stack_list
);
4746 } while (ret
== -EAGAIN
);
4749 ret
= register_trace_sched_switch(ftrace_graph_probe_sched_switch
, NULL
);
4751 pr_info("ftrace_graph: Couldn't activate tracepoint"
4752 " probe to kernel_sched_switch\n");
4755 kfree(ret_stack_list
);
4760 * Hibernation protection.
4761 * The state of the current task is too much unstable during
4762 * suspend/restore to disk. We want to protect against that.
4765 ftrace_suspend_notifier_call(struct notifier_block
*bl
, unsigned long state
,
4769 case PM_HIBERNATION_PREPARE
:
4770 pause_graph_tracing();
4773 case PM_POST_HIBERNATION
:
4774 unpause_graph_tracing();
4780 int register_ftrace_graph(trace_func_graph_ret_t retfunc
,
4781 trace_func_graph_ent_t entryfunc
)
4785 mutex_lock(&ftrace_lock
);
4787 /* we currently allow only one tracer registered at a time */
4788 if (ftrace_graph_active
) {
4793 ftrace_suspend_notifier
.notifier_call
= ftrace_suspend_notifier_call
;
4794 register_pm_notifier(&ftrace_suspend_notifier
);
4796 ftrace_graph_active
++;
4797 ret
= start_graph_tracing();
4799 ftrace_graph_active
--;
4803 ftrace_graph_return
= retfunc
;
4804 ftrace_graph_entry
= entryfunc
;
4806 ret
= ftrace_startup(&global_ops
, FTRACE_START_FUNC_RET
);
4809 mutex_unlock(&ftrace_lock
);
4813 void unregister_ftrace_graph(void)
4815 mutex_lock(&ftrace_lock
);
4817 if (unlikely(!ftrace_graph_active
))
4820 ftrace_graph_active
--;
4821 ftrace_graph_return
= (trace_func_graph_ret_t
)ftrace_stub
;
4822 ftrace_graph_entry
= ftrace_graph_entry_stub
;
4823 ftrace_shutdown(&global_ops
, FTRACE_STOP_FUNC_RET
);
4824 unregister_pm_notifier(&ftrace_suspend_notifier
);
4825 unregister_trace_sched_switch(ftrace_graph_probe_sched_switch
, NULL
);
4828 mutex_unlock(&ftrace_lock
);
4831 static DEFINE_PER_CPU(struct ftrace_ret_stack
*, idle_ret_stack
);
4834 graph_init_task(struct task_struct
*t
, struct ftrace_ret_stack
*ret_stack
)
4836 atomic_set(&t
->tracing_graph_pause
, 0);
4837 atomic_set(&t
->trace_overrun
, 0);
4838 t
->ftrace_timestamp
= 0;
4839 /* make curr_ret_stack visible before we add the ret_stack */
4841 t
->ret_stack
= ret_stack
;
4845 * Allocate a return stack for the idle task. May be the first
4846 * time through, or it may be done by CPU hotplug online.
4848 void ftrace_graph_init_idle_task(struct task_struct
*t
, int cpu
)
4850 t
->curr_ret_stack
= -1;
4852 * The idle task has no parent, it either has its own
4853 * stack or no stack at all.
4856 WARN_ON(t
->ret_stack
!= per_cpu(idle_ret_stack
, cpu
));
4858 if (ftrace_graph_active
) {
4859 struct ftrace_ret_stack
*ret_stack
;
4861 ret_stack
= per_cpu(idle_ret_stack
, cpu
);
4863 ret_stack
= kmalloc(FTRACE_RETFUNC_DEPTH
4864 * sizeof(struct ftrace_ret_stack
),
4868 per_cpu(idle_ret_stack
, cpu
) = ret_stack
;
4870 graph_init_task(t
, ret_stack
);
4874 /* Allocate a return stack for newly created task */
4875 void ftrace_graph_init_task(struct task_struct
*t
)
4877 /* Make sure we do not use the parent ret_stack */
4878 t
->ret_stack
= NULL
;
4879 t
->curr_ret_stack
= -1;
4881 if (ftrace_graph_active
) {
4882 struct ftrace_ret_stack
*ret_stack
;
4884 ret_stack
= kmalloc(FTRACE_RETFUNC_DEPTH
4885 * sizeof(struct ftrace_ret_stack
),
4889 graph_init_task(t
, ret_stack
);
4893 void ftrace_graph_exit_task(struct task_struct
*t
)
4895 struct ftrace_ret_stack
*ret_stack
= t
->ret_stack
;
4897 t
->ret_stack
= NULL
;
4898 /* NULL must become visible to IRQs before we free it: */
4904 void ftrace_graph_stop(void)