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_enabled __read_mostly
;
491 /* ftrace_profile_lock - synchronize the enable and disable of the profiler */
492 static DEFINE_MUTEX(ftrace_profile_lock
);
494 static DEFINE_PER_CPU(struct ftrace_profile_stat
, ftrace_profile_stats
);
496 #define FTRACE_PROFILE_HASH_BITS 10
497 #define FTRACE_PROFILE_HASH_SIZE (1 << FTRACE_PROFILE_HASH_BITS)
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
= 1; 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 /* Preallocate the function profiling pages */
729 if (ftrace_profile_pages_init(stat
) < 0) {
738 static int ftrace_profile_init(void)
743 for_each_online_cpu(cpu
) {
744 ret
= ftrace_profile_init_cpu(cpu
);
752 /* interrupts must be disabled */
753 static struct ftrace_profile
*
754 ftrace_find_profiled_func(struct ftrace_profile_stat
*stat
, unsigned long ip
)
756 struct ftrace_profile
*rec
;
757 struct hlist_head
*hhd
;
758 struct hlist_node
*n
;
761 key
= hash_long(ip
, FTRACE_PROFILE_HASH_BITS
);
762 hhd
= &stat
->hash
[key
];
764 if (hlist_empty(hhd
))
767 hlist_for_each_entry_rcu(rec
, n
, hhd
, node
) {
775 static void ftrace_add_profile(struct ftrace_profile_stat
*stat
,
776 struct ftrace_profile
*rec
)
780 key
= hash_long(rec
->ip
, FTRACE_PROFILE_HASH_BITS
);
781 hlist_add_head_rcu(&rec
->node
, &stat
->hash
[key
]);
785 * The memory is already allocated, this simply finds a new record to use.
787 static struct ftrace_profile
*
788 ftrace_profile_alloc(struct ftrace_profile_stat
*stat
, unsigned long ip
)
790 struct ftrace_profile
*rec
= NULL
;
792 /* prevent recursion (from NMIs) */
793 if (atomic_inc_return(&stat
->disabled
) != 1)
797 * Try to find the function again since an NMI
798 * could have added it
800 rec
= ftrace_find_profiled_func(stat
, ip
);
804 if (stat
->pages
->index
== PROFILES_PER_PAGE
) {
805 if (!stat
->pages
->next
)
807 stat
->pages
= stat
->pages
->next
;
810 rec
= &stat
->pages
->records
[stat
->pages
->index
++];
812 ftrace_add_profile(stat
, rec
);
815 atomic_dec(&stat
->disabled
);
821 function_profile_call(unsigned long ip
, unsigned long parent_ip
,
822 struct ftrace_ops
*ops
, struct pt_regs
*regs
)
824 struct ftrace_profile_stat
*stat
;
825 struct ftrace_profile
*rec
;
828 if (!ftrace_profile_enabled
)
831 local_irq_save(flags
);
833 stat
= &__get_cpu_var(ftrace_profile_stats
);
834 if (!stat
->hash
|| !ftrace_profile_enabled
)
837 rec
= ftrace_find_profiled_func(stat
, ip
);
839 rec
= ftrace_profile_alloc(stat
, ip
);
846 local_irq_restore(flags
);
849 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
850 static int profile_graph_entry(struct ftrace_graph_ent
*trace
)
852 function_profile_call(trace
->func
, 0, NULL
, NULL
);
856 static void profile_graph_return(struct ftrace_graph_ret
*trace
)
858 struct ftrace_profile_stat
*stat
;
859 unsigned long long calltime
;
860 struct ftrace_profile
*rec
;
863 local_irq_save(flags
);
864 stat
= &__get_cpu_var(ftrace_profile_stats
);
865 if (!stat
->hash
|| !ftrace_profile_enabled
)
868 /* If the calltime was zero'd ignore it */
869 if (!trace
->calltime
)
872 calltime
= trace
->rettime
- trace
->calltime
;
874 if (!(trace_flags
& TRACE_ITER_GRAPH_TIME
)) {
877 index
= trace
->depth
;
879 /* Append this call time to the parent time to subtract */
881 current
->ret_stack
[index
- 1].subtime
+= calltime
;
883 if (current
->ret_stack
[index
].subtime
< calltime
)
884 calltime
-= current
->ret_stack
[index
].subtime
;
889 rec
= ftrace_find_profiled_func(stat
, trace
->func
);
891 rec
->time
+= calltime
;
892 rec
->time_squared
+= calltime
* calltime
;
896 local_irq_restore(flags
);
899 static int register_ftrace_profiler(void)
901 return register_ftrace_graph(&profile_graph_return
,
902 &profile_graph_entry
);
905 static void unregister_ftrace_profiler(void)
907 unregister_ftrace_graph();
910 static struct ftrace_ops ftrace_profile_ops __read_mostly
= {
911 .func
= function_profile_call
,
912 .flags
= FTRACE_OPS_FL_RECURSION_SAFE
,
915 static int register_ftrace_profiler(void)
917 return register_ftrace_function(&ftrace_profile_ops
);
920 static void unregister_ftrace_profiler(void)
922 unregister_ftrace_function(&ftrace_profile_ops
);
924 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
927 ftrace_profile_write(struct file
*filp
, const char __user
*ubuf
,
928 size_t cnt
, loff_t
*ppos
)
933 ret
= kstrtoul_from_user(ubuf
, cnt
, 10, &val
);
939 mutex_lock(&ftrace_profile_lock
);
940 if (ftrace_profile_enabled
^ val
) {
942 ret
= ftrace_profile_init();
948 ret
= register_ftrace_profiler();
953 ftrace_profile_enabled
= 1;
955 ftrace_profile_enabled
= 0;
957 * unregister_ftrace_profiler calls stop_machine
958 * so this acts like an synchronize_sched.
960 unregister_ftrace_profiler();
964 mutex_unlock(&ftrace_profile_lock
);
972 ftrace_profile_read(struct file
*filp
, char __user
*ubuf
,
973 size_t cnt
, loff_t
*ppos
)
975 char buf
[64]; /* big enough to hold a number */
978 r
= sprintf(buf
, "%u\n", ftrace_profile_enabled
);
979 return simple_read_from_buffer(ubuf
, cnt
, ppos
, buf
, r
);
982 static const struct file_operations ftrace_profile_fops
= {
983 .open
= tracing_open_generic
,
984 .read
= ftrace_profile_read
,
985 .write
= ftrace_profile_write
,
986 .llseek
= default_llseek
,
989 /* used to initialize the real stat files */
990 static struct tracer_stat function_stats __initdata
= {
992 .stat_start
= function_stat_start
,
993 .stat_next
= function_stat_next
,
994 .stat_cmp
= function_stat_cmp
,
995 .stat_headers
= function_stat_headers
,
996 .stat_show
= function_stat_show
999 static __init
void ftrace_profile_debugfs(struct dentry
*d_tracer
)
1001 struct ftrace_profile_stat
*stat
;
1002 struct dentry
*entry
;
1007 for_each_possible_cpu(cpu
) {
1008 stat
= &per_cpu(ftrace_profile_stats
, cpu
);
1010 /* allocate enough for function name + cpu number */
1011 name
= kmalloc(32, GFP_KERNEL
);
1014 * The files created are permanent, if something happens
1015 * we still do not free memory.
1018 "Could not allocate stat file for cpu %d\n",
1022 stat
->stat
= function_stats
;
1023 snprintf(name
, 32, "function%d", cpu
);
1024 stat
->stat
.name
= name
;
1025 ret
= register_stat_tracer(&stat
->stat
);
1028 "Could not register function stat for cpu %d\n",
1035 entry
= debugfs_create_file("function_profile_enabled", 0644,
1036 d_tracer
, NULL
, &ftrace_profile_fops
);
1038 pr_warning("Could not create debugfs "
1039 "'function_profile_enabled' entry\n");
1042 #else /* CONFIG_FUNCTION_PROFILER */
1043 static __init
void ftrace_profile_debugfs(struct dentry
*d_tracer
)
1046 #endif /* CONFIG_FUNCTION_PROFILER */
1048 static struct pid
* const ftrace_swapper_pid
= &init_struct_pid
;
1050 #ifdef CONFIG_DYNAMIC_FTRACE
1052 #ifndef CONFIG_FTRACE_MCOUNT_RECORD
1053 # error Dynamic ftrace depends on MCOUNT_RECORD
1056 static struct hlist_head ftrace_func_hash
[FTRACE_FUNC_HASHSIZE
] __read_mostly
;
1058 struct ftrace_func_probe
{
1059 struct hlist_node node
;
1060 struct ftrace_probe_ops
*ops
;
1061 unsigned long flags
;
1064 struct list_head free_list
;
1067 struct ftrace_func_entry
{
1068 struct hlist_node hlist
;
1072 struct ftrace_hash
{
1073 unsigned long size_bits
;
1074 struct hlist_head
*buckets
;
1075 unsigned long count
;
1076 struct rcu_head rcu
;
1080 * We make these constant because no one should touch them,
1081 * but they are used as the default "empty hash", to avoid allocating
1082 * it all the time. These are in a read only section such that if
1083 * anyone does try to modify it, it will cause an exception.
1085 static const struct hlist_head empty_buckets
[1];
1086 static const struct ftrace_hash empty_hash
= {
1087 .buckets
= (struct hlist_head
*)empty_buckets
,
1089 #define EMPTY_HASH ((struct ftrace_hash *)&empty_hash)
1091 static struct ftrace_ops global_ops
= {
1092 .func
= ftrace_stub
,
1093 .notrace_hash
= EMPTY_HASH
,
1094 .filter_hash
= EMPTY_HASH
,
1095 .flags
= FTRACE_OPS_FL_RECURSION_SAFE
,
1098 static DEFINE_MUTEX(ftrace_regex_lock
);
1100 struct ftrace_page
{
1101 struct ftrace_page
*next
;
1102 struct dyn_ftrace
*records
;
1107 static struct ftrace_page
*ftrace_new_pgs
;
1109 #define ENTRY_SIZE sizeof(struct dyn_ftrace)
1110 #define ENTRIES_PER_PAGE (PAGE_SIZE / ENTRY_SIZE)
1112 /* estimate from running different kernels */
1113 #define NR_TO_INIT 10000
1115 static struct ftrace_page
*ftrace_pages_start
;
1116 static struct ftrace_page
*ftrace_pages
;
1118 static bool ftrace_hash_empty(struct ftrace_hash
*hash
)
1120 return !hash
|| !hash
->count
;
1123 static struct ftrace_func_entry
*
1124 ftrace_lookup_ip(struct ftrace_hash
*hash
, unsigned long ip
)
1127 struct ftrace_func_entry
*entry
;
1128 struct hlist_head
*hhd
;
1129 struct hlist_node
*n
;
1131 if (ftrace_hash_empty(hash
))
1134 if (hash
->size_bits
> 0)
1135 key
= hash_long(ip
, hash
->size_bits
);
1139 hhd
= &hash
->buckets
[key
];
1141 hlist_for_each_entry_rcu(entry
, n
, hhd
, hlist
) {
1142 if (entry
->ip
== ip
)
1148 static void __add_hash_entry(struct ftrace_hash
*hash
,
1149 struct ftrace_func_entry
*entry
)
1151 struct hlist_head
*hhd
;
1154 if (hash
->size_bits
)
1155 key
= hash_long(entry
->ip
, hash
->size_bits
);
1159 hhd
= &hash
->buckets
[key
];
1160 hlist_add_head(&entry
->hlist
, hhd
);
1164 static int add_hash_entry(struct ftrace_hash
*hash
, unsigned long ip
)
1166 struct ftrace_func_entry
*entry
;
1168 entry
= kmalloc(sizeof(*entry
), GFP_KERNEL
);
1173 __add_hash_entry(hash
, entry
);
1179 free_hash_entry(struct ftrace_hash
*hash
,
1180 struct ftrace_func_entry
*entry
)
1182 hlist_del(&entry
->hlist
);
1188 remove_hash_entry(struct ftrace_hash
*hash
,
1189 struct ftrace_func_entry
*entry
)
1191 hlist_del(&entry
->hlist
);
1195 static void ftrace_hash_clear(struct ftrace_hash
*hash
)
1197 struct hlist_head
*hhd
;
1198 struct hlist_node
*tp
, *tn
;
1199 struct ftrace_func_entry
*entry
;
1200 int size
= 1 << hash
->size_bits
;
1206 for (i
= 0; i
< size
; i
++) {
1207 hhd
= &hash
->buckets
[i
];
1208 hlist_for_each_entry_safe(entry
, tp
, tn
, hhd
, hlist
)
1209 free_hash_entry(hash
, entry
);
1211 FTRACE_WARN_ON(hash
->count
);
1214 static void free_ftrace_hash(struct ftrace_hash
*hash
)
1216 if (!hash
|| hash
== EMPTY_HASH
)
1218 ftrace_hash_clear(hash
);
1219 kfree(hash
->buckets
);
1223 static void __free_ftrace_hash_rcu(struct rcu_head
*rcu
)
1225 struct ftrace_hash
*hash
;
1227 hash
= container_of(rcu
, struct ftrace_hash
, rcu
);
1228 free_ftrace_hash(hash
);
1231 static void free_ftrace_hash_rcu(struct ftrace_hash
*hash
)
1233 if (!hash
|| hash
== EMPTY_HASH
)
1235 call_rcu_sched(&hash
->rcu
, __free_ftrace_hash_rcu
);
1238 void ftrace_free_filter(struct ftrace_ops
*ops
)
1240 free_ftrace_hash(ops
->filter_hash
);
1241 free_ftrace_hash(ops
->notrace_hash
);
1244 static struct ftrace_hash
*alloc_ftrace_hash(int size_bits
)
1246 struct ftrace_hash
*hash
;
1249 hash
= kzalloc(sizeof(*hash
), GFP_KERNEL
);
1253 size
= 1 << size_bits
;
1254 hash
->buckets
= kcalloc(size
, sizeof(*hash
->buckets
), GFP_KERNEL
);
1256 if (!hash
->buckets
) {
1261 hash
->size_bits
= size_bits
;
1266 static struct ftrace_hash
*
1267 alloc_and_copy_ftrace_hash(int size_bits
, struct ftrace_hash
*hash
)
1269 struct ftrace_func_entry
*entry
;
1270 struct ftrace_hash
*new_hash
;
1271 struct hlist_node
*tp
;
1276 new_hash
= alloc_ftrace_hash(size_bits
);
1281 if (ftrace_hash_empty(hash
))
1284 size
= 1 << hash
->size_bits
;
1285 for (i
= 0; i
< size
; i
++) {
1286 hlist_for_each_entry(entry
, tp
, &hash
->buckets
[i
], hlist
) {
1287 ret
= add_hash_entry(new_hash
, entry
->ip
);
1293 FTRACE_WARN_ON(new_hash
->count
!= hash
->count
);
1298 free_ftrace_hash(new_hash
);
1303 ftrace_hash_rec_disable(struct ftrace_ops
*ops
, int filter_hash
);
1305 ftrace_hash_rec_enable(struct ftrace_ops
*ops
, int filter_hash
);
1308 ftrace_hash_move(struct ftrace_ops
*ops
, int enable
,
1309 struct ftrace_hash
**dst
, struct ftrace_hash
*src
)
1311 struct ftrace_func_entry
*entry
;
1312 struct hlist_node
*tp
, *tn
;
1313 struct hlist_head
*hhd
;
1314 struct ftrace_hash
*old_hash
;
1315 struct ftrace_hash
*new_hash
;
1316 int size
= src
->count
;
1322 * Remove the current set, update the hash and add
1325 ftrace_hash_rec_disable(ops
, enable
);
1328 * If the new source is empty, just free dst and assign it
1332 free_ftrace_hash_rcu(*dst
);
1333 rcu_assign_pointer(*dst
, EMPTY_HASH
);
1334 /* still need to update the function records */
1340 * Make the hash size about 1/2 the # found
1342 for (size
/= 2; size
; size
>>= 1)
1345 /* Don't allocate too much */
1346 if (bits
> FTRACE_HASH_MAX_BITS
)
1347 bits
= FTRACE_HASH_MAX_BITS
;
1350 new_hash
= alloc_ftrace_hash(bits
);
1354 size
= 1 << src
->size_bits
;
1355 for (i
= 0; i
< size
; i
++) {
1356 hhd
= &src
->buckets
[i
];
1357 hlist_for_each_entry_safe(entry
, tp
, tn
, hhd
, hlist
) {
1358 remove_hash_entry(src
, entry
);
1359 __add_hash_entry(new_hash
, entry
);
1364 rcu_assign_pointer(*dst
, new_hash
);
1365 free_ftrace_hash_rcu(old_hash
);
1370 * Enable regardless of ret:
1371 * On success, we enable the new hash.
1372 * On failure, we re-enable the original hash.
1374 ftrace_hash_rec_enable(ops
, enable
);
1380 * Test the hashes for this ops to see if we want to call
1381 * the ops->func or not.
1383 * It's a match if the ip is in the ops->filter_hash or
1384 * the filter_hash does not exist or is empty,
1386 * the ip is not in the ops->notrace_hash.
1388 * This needs to be called with preemption disabled as
1389 * the hashes are freed with call_rcu_sched().
1392 ftrace_ops_test(struct ftrace_ops
*ops
, unsigned long ip
)
1394 struct ftrace_hash
*filter_hash
;
1395 struct ftrace_hash
*notrace_hash
;
1398 filter_hash
= rcu_dereference_raw(ops
->filter_hash
);
1399 notrace_hash
= rcu_dereference_raw(ops
->notrace_hash
);
1401 if ((ftrace_hash_empty(filter_hash
) ||
1402 ftrace_lookup_ip(filter_hash
, ip
)) &&
1403 (ftrace_hash_empty(notrace_hash
) ||
1404 !ftrace_lookup_ip(notrace_hash
, ip
)))
1413 * This is a double for. Do not use 'break' to break out of the loop,
1414 * you must use a goto.
1416 #define do_for_each_ftrace_rec(pg, rec) \
1417 for (pg = ftrace_pages_start; pg; pg = pg->next) { \
1419 for (_____i = 0; _____i < pg->index; _____i++) { \
1420 rec = &pg->records[_____i];
1422 #define while_for_each_ftrace_rec() \
1427 static int ftrace_cmp_recs(const void *a
, const void *b
)
1429 const struct dyn_ftrace
*key
= a
;
1430 const struct dyn_ftrace
*rec
= b
;
1432 if (key
->flags
< rec
->ip
)
1434 if (key
->ip
>= rec
->ip
+ MCOUNT_INSN_SIZE
)
1439 static unsigned long ftrace_location_range(unsigned long start
, unsigned long end
)
1441 struct ftrace_page
*pg
;
1442 struct dyn_ftrace
*rec
;
1443 struct dyn_ftrace key
;
1446 key
.flags
= end
; /* overload flags, as it is unsigned long */
1448 for (pg
= ftrace_pages_start
; pg
; pg
= pg
->next
) {
1449 if (end
< pg
->records
[0].ip
||
1450 start
>= (pg
->records
[pg
->index
- 1].ip
+ MCOUNT_INSN_SIZE
))
1452 rec
= bsearch(&key
, pg
->records
, pg
->index
,
1453 sizeof(struct dyn_ftrace
),
1463 * ftrace_location - return true if the ip giving is a traced location
1464 * @ip: the instruction pointer to check
1466 * Returns rec->ip if @ip given is a pointer to a ftrace location.
1467 * That is, the instruction that is either a NOP or call to
1468 * the function tracer. It checks the ftrace internal tables to
1469 * determine if the address belongs or not.
1471 unsigned long ftrace_location(unsigned long ip
)
1473 return ftrace_location_range(ip
, ip
);
1477 * ftrace_text_reserved - return true if range contains an ftrace location
1478 * @start: start of range to search
1479 * @end: end of range to search (inclusive). @end points to the last byte to check.
1481 * Returns 1 if @start and @end contains a ftrace location.
1482 * That is, the instruction that is either a NOP or call to
1483 * the function tracer. It checks the ftrace internal tables to
1484 * determine if the address belongs or not.
1486 int ftrace_text_reserved(void *start
, void *end
)
1490 ret
= ftrace_location_range((unsigned long)start
,
1491 (unsigned long)end
);
1496 static void __ftrace_hash_rec_update(struct ftrace_ops
*ops
,
1500 struct ftrace_hash
*hash
;
1501 struct ftrace_hash
*other_hash
;
1502 struct ftrace_page
*pg
;
1503 struct dyn_ftrace
*rec
;
1507 /* Only update if the ops has been registered */
1508 if (!(ops
->flags
& FTRACE_OPS_FL_ENABLED
))
1512 * In the filter_hash case:
1513 * If the count is zero, we update all records.
1514 * Otherwise we just update the items in the hash.
1516 * In the notrace_hash case:
1517 * We enable the update in the hash.
1518 * As disabling notrace means enabling the tracing,
1519 * and enabling notrace means disabling, the inc variable
1523 hash
= ops
->filter_hash
;
1524 other_hash
= ops
->notrace_hash
;
1525 if (ftrace_hash_empty(hash
))
1529 hash
= ops
->notrace_hash
;
1530 other_hash
= ops
->filter_hash
;
1532 * If the notrace hash has no items,
1533 * then there's nothing to do.
1535 if (ftrace_hash_empty(hash
))
1539 do_for_each_ftrace_rec(pg
, rec
) {
1540 int in_other_hash
= 0;
1546 * Only the filter_hash affects all records.
1547 * Update if the record is not in the notrace hash.
1549 if (!other_hash
|| !ftrace_lookup_ip(other_hash
, rec
->ip
))
1552 in_hash
= !!ftrace_lookup_ip(hash
, rec
->ip
);
1553 in_other_hash
= !!ftrace_lookup_ip(other_hash
, rec
->ip
);
1558 if (filter_hash
&& in_hash
&& !in_other_hash
)
1560 else if (!filter_hash
&& in_hash
&&
1561 (in_other_hash
|| ftrace_hash_empty(other_hash
)))
1569 if (FTRACE_WARN_ON((rec
->flags
& ~FTRACE_FL_MASK
) == FTRACE_REF_MAX
))
1572 * If any ops wants regs saved for this function
1573 * then all ops will get saved regs.
1575 if (ops
->flags
& FTRACE_OPS_FL_SAVE_REGS
)
1576 rec
->flags
|= FTRACE_FL_REGS
;
1578 if (FTRACE_WARN_ON((rec
->flags
& ~FTRACE_FL_MASK
) == 0))
1583 /* Shortcut, if we handled all records, we are done. */
1584 if (!all
&& count
== hash
->count
)
1586 } while_for_each_ftrace_rec();
1589 static void ftrace_hash_rec_disable(struct ftrace_ops
*ops
,
1592 __ftrace_hash_rec_update(ops
, filter_hash
, 0);
1595 static void ftrace_hash_rec_enable(struct ftrace_ops
*ops
,
1598 __ftrace_hash_rec_update(ops
, filter_hash
, 1);
1601 static void print_ip_ins(const char *fmt
, unsigned char *p
)
1605 printk(KERN_CONT
"%s", fmt
);
1607 for (i
= 0; i
< MCOUNT_INSN_SIZE
; i
++)
1608 printk(KERN_CONT
"%s%02x", i
? ":" : "", p
[i
]);
1612 * ftrace_bug - report and shutdown function tracer
1613 * @failed: The failed type (EFAULT, EINVAL, EPERM)
1614 * @ip: The address that failed
1616 * The arch code that enables or disables the function tracing
1617 * can call ftrace_bug() when it has detected a problem in
1618 * modifying the code. @failed should be one of either:
1619 * EFAULT - if the problem happens on reading the @ip address
1620 * EINVAL - if what is read at @ip is not what was expected
1621 * EPERM - if the problem happens on writting to the @ip address
1623 void ftrace_bug(int failed
, unsigned long ip
)
1627 FTRACE_WARN_ON_ONCE(1);
1628 pr_info("ftrace faulted on modifying ");
1632 FTRACE_WARN_ON_ONCE(1);
1633 pr_info("ftrace failed to modify ");
1635 print_ip_ins(" actual: ", (unsigned char *)ip
);
1636 printk(KERN_CONT
"\n");
1639 FTRACE_WARN_ON_ONCE(1);
1640 pr_info("ftrace faulted on writing ");
1644 FTRACE_WARN_ON_ONCE(1);
1645 pr_info("ftrace faulted on unknown error ");
1650 static int ftrace_check_record(struct dyn_ftrace
*rec
, int enable
, int update
)
1652 unsigned long flag
= 0UL;
1655 * If we are updating calls:
1657 * If the record has a ref count, then we need to enable it
1658 * because someone is using it.
1660 * Otherwise we make sure its disabled.
1662 * If we are disabling calls, then disable all records that
1665 if (enable
&& (rec
->flags
& ~FTRACE_FL_MASK
))
1666 flag
= FTRACE_FL_ENABLED
;
1669 * If enabling and the REGS flag does not match the REGS_EN, then
1670 * do not ignore this record. Set flags to fail the compare against
1674 (!(rec
->flags
& FTRACE_FL_REGS
) != !(rec
->flags
& FTRACE_FL_REGS_EN
)))
1675 flag
|= FTRACE_FL_REGS
;
1677 /* If the state of this record hasn't changed, then do nothing */
1678 if ((rec
->flags
& FTRACE_FL_ENABLED
) == flag
)
1679 return FTRACE_UPDATE_IGNORE
;
1682 /* Save off if rec is being enabled (for return value) */
1683 flag
^= rec
->flags
& FTRACE_FL_ENABLED
;
1686 rec
->flags
|= FTRACE_FL_ENABLED
;
1687 if (flag
& FTRACE_FL_REGS
) {
1688 if (rec
->flags
& FTRACE_FL_REGS
)
1689 rec
->flags
|= FTRACE_FL_REGS_EN
;
1691 rec
->flags
&= ~FTRACE_FL_REGS_EN
;
1696 * If this record is being updated from a nop, then
1697 * return UPDATE_MAKE_CALL.
1698 * Otherwise, if the EN flag is set, then return
1699 * UPDATE_MODIFY_CALL_REGS to tell the caller to convert
1700 * from the non-save regs, to a save regs function.
1702 * return UPDATE_MODIFY_CALL to tell the caller to convert
1703 * from the save regs, to a non-save regs function.
1705 if (flag
& FTRACE_FL_ENABLED
)
1706 return FTRACE_UPDATE_MAKE_CALL
;
1707 else if (rec
->flags
& FTRACE_FL_REGS_EN
)
1708 return FTRACE_UPDATE_MODIFY_CALL_REGS
;
1710 return FTRACE_UPDATE_MODIFY_CALL
;
1714 /* If there's no more users, clear all flags */
1715 if (!(rec
->flags
& ~FTRACE_FL_MASK
))
1718 /* Just disable the record (keep REGS state) */
1719 rec
->flags
&= ~FTRACE_FL_ENABLED
;
1722 return FTRACE_UPDATE_MAKE_NOP
;
1726 * ftrace_update_record, set a record that now is tracing or not
1727 * @rec: the record to update
1728 * @enable: set to 1 if the record is tracing, zero to force disable
1730 * The records that represent all functions that can be traced need
1731 * to be updated when tracing has been enabled.
1733 int ftrace_update_record(struct dyn_ftrace
*rec
, int enable
)
1735 return ftrace_check_record(rec
, enable
, 1);
1739 * ftrace_test_record, check if the record has been enabled or not
1740 * @rec: the record to test
1741 * @enable: set to 1 to check if enabled, 0 if it is disabled
1743 * The arch code may need to test if a record is already set to
1744 * tracing to determine how to modify the function code that it
1747 int ftrace_test_record(struct dyn_ftrace
*rec
, int enable
)
1749 return ftrace_check_record(rec
, enable
, 0);
1753 __ftrace_replace_code(struct dyn_ftrace
*rec
, int enable
)
1755 unsigned long ftrace_old_addr
;
1756 unsigned long ftrace_addr
;
1759 ret
= ftrace_update_record(rec
, enable
);
1761 if (rec
->flags
& FTRACE_FL_REGS
)
1762 ftrace_addr
= (unsigned long)FTRACE_REGS_ADDR
;
1764 ftrace_addr
= (unsigned long)FTRACE_ADDR
;
1767 case FTRACE_UPDATE_IGNORE
:
1770 case FTRACE_UPDATE_MAKE_CALL
:
1771 return ftrace_make_call(rec
, ftrace_addr
);
1773 case FTRACE_UPDATE_MAKE_NOP
:
1774 return ftrace_make_nop(NULL
, rec
, ftrace_addr
);
1776 case FTRACE_UPDATE_MODIFY_CALL_REGS
:
1777 case FTRACE_UPDATE_MODIFY_CALL
:
1778 if (rec
->flags
& FTRACE_FL_REGS
)
1779 ftrace_old_addr
= (unsigned long)FTRACE_ADDR
;
1781 ftrace_old_addr
= (unsigned long)FTRACE_REGS_ADDR
;
1783 return ftrace_modify_call(rec
, ftrace_old_addr
, ftrace_addr
);
1786 return -1; /* unknow ftrace bug */
1789 void __weak
ftrace_replace_code(int enable
)
1791 struct dyn_ftrace
*rec
;
1792 struct ftrace_page
*pg
;
1795 if (unlikely(ftrace_disabled
))
1798 do_for_each_ftrace_rec(pg
, rec
) {
1799 failed
= __ftrace_replace_code(rec
, enable
);
1801 ftrace_bug(failed
, rec
->ip
);
1802 /* Stop processing */
1805 } while_for_each_ftrace_rec();
1808 struct ftrace_rec_iter
{
1809 struct ftrace_page
*pg
;
1814 * ftrace_rec_iter_start, start up iterating over traced functions
1816 * Returns an iterator handle that is used to iterate over all
1817 * the records that represent address locations where functions
1820 * May return NULL if no records are available.
1822 struct ftrace_rec_iter
*ftrace_rec_iter_start(void)
1825 * We only use a single iterator.
1826 * Protected by the ftrace_lock mutex.
1828 static struct ftrace_rec_iter ftrace_rec_iter
;
1829 struct ftrace_rec_iter
*iter
= &ftrace_rec_iter
;
1831 iter
->pg
= ftrace_pages_start
;
1834 /* Could have empty pages */
1835 while (iter
->pg
&& !iter
->pg
->index
)
1836 iter
->pg
= iter
->pg
->next
;
1845 * ftrace_rec_iter_next, get the next record to process.
1846 * @iter: The handle to the iterator.
1848 * Returns the next iterator after the given iterator @iter.
1850 struct ftrace_rec_iter
*ftrace_rec_iter_next(struct ftrace_rec_iter
*iter
)
1854 if (iter
->index
>= iter
->pg
->index
) {
1855 iter
->pg
= iter
->pg
->next
;
1858 /* Could have empty pages */
1859 while (iter
->pg
&& !iter
->pg
->index
)
1860 iter
->pg
= iter
->pg
->next
;
1870 * ftrace_rec_iter_record, get the record at the iterator location
1871 * @iter: The current iterator location
1873 * Returns the record that the current @iter is at.
1875 struct dyn_ftrace
*ftrace_rec_iter_record(struct ftrace_rec_iter
*iter
)
1877 return &iter
->pg
->records
[iter
->index
];
1881 ftrace_code_disable(struct module
*mod
, struct dyn_ftrace
*rec
)
1888 if (unlikely(ftrace_disabled
))
1891 ret
= ftrace_make_nop(mod
, rec
, MCOUNT_ADDR
);
1893 ftrace_bug(ret
, ip
);
1900 * archs can override this function if they must do something
1901 * before the modifying code is performed.
1903 int __weak
ftrace_arch_code_modify_prepare(void)
1909 * archs can override this function if they must do something
1910 * after the modifying code is performed.
1912 int __weak
ftrace_arch_code_modify_post_process(void)
1917 void ftrace_modify_all_code(int command
)
1919 if (command
& FTRACE_UPDATE_CALLS
)
1920 ftrace_replace_code(1);
1921 else if (command
& FTRACE_DISABLE_CALLS
)
1922 ftrace_replace_code(0);
1924 if (command
& FTRACE_UPDATE_TRACE_FUNC
)
1925 ftrace_update_ftrace_func(ftrace_trace_function
);
1927 if (command
& FTRACE_START_FUNC_RET
)
1928 ftrace_enable_ftrace_graph_caller();
1929 else if (command
& FTRACE_STOP_FUNC_RET
)
1930 ftrace_disable_ftrace_graph_caller();
1933 static int __ftrace_modify_code(void *data
)
1935 int *command
= data
;
1937 ftrace_modify_all_code(*command
);
1943 * ftrace_run_stop_machine, go back to the stop machine method
1944 * @command: The command to tell ftrace what to do
1946 * If an arch needs to fall back to the stop machine method, the
1947 * it can call this function.
1949 void ftrace_run_stop_machine(int command
)
1951 stop_machine(__ftrace_modify_code
, &command
, NULL
);
1955 * arch_ftrace_update_code, modify the code to trace or not trace
1956 * @command: The command that needs to be done
1958 * Archs can override this function if it does not need to
1959 * run stop_machine() to modify code.
1961 void __weak
arch_ftrace_update_code(int command
)
1963 ftrace_run_stop_machine(command
);
1966 static void ftrace_run_update_code(int command
)
1970 ret
= ftrace_arch_code_modify_prepare();
1971 FTRACE_WARN_ON(ret
);
1975 * Do not call function tracer while we update the code.
1976 * We are in stop machine.
1978 function_trace_stop
++;
1981 * By default we use stop_machine() to modify the code.
1982 * But archs can do what ever they want as long as it
1983 * is safe. The stop_machine() is the safest, but also
1984 * produces the most overhead.
1986 arch_ftrace_update_code(command
);
1988 function_trace_stop
--;
1990 ret
= ftrace_arch_code_modify_post_process();
1991 FTRACE_WARN_ON(ret
);
1994 static ftrace_func_t saved_ftrace_func
;
1995 static int ftrace_start_up
;
1996 static int global_start_up
;
1998 static void ftrace_startup_enable(int command
)
2000 if (saved_ftrace_func
!= ftrace_trace_function
) {
2001 saved_ftrace_func
= ftrace_trace_function
;
2002 command
|= FTRACE_UPDATE_TRACE_FUNC
;
2005 if (!command
|| !ftrace_enabled
)
2008 ftrace_run_update_code(command
);
2011 static int ftrace_startup(struct ftrace_ops
*ops
, int command
)
2013 bool hash_enable
= true;
2015 if (unlikely(ftrace_disabled
))
2019 command
|= FTRACE_UPDATE_CALLS
;
2021 /* ops marked global share the filter hashes */
2022 if (ops
->flags
& FTRACE_OPS_FL_GLOBAL
) {
2024 /* Don't update hash if global is already set */
2025 if (global_start_up
)
2026 hash_enable
= false;
2030 ops
->flags
|= FTRACE_OPS_FL_ENABLED
;
2032 ftrace_hash_rec_enable(ops
, 1);
2034 ftrace_startup_enable(command
);
2039 static void ftrace_shutdown(struct ftrace_ops
*ops
, int command
)
2041 bool hash_disable
= true;
2043 if (unlikely(ftrace_disabled
))
2048 * Just warn in case of unbalance, no need to kill ftrace, it's not
2049 * critical but the ftrace_call callers may be never nopped again after
2050 * further ftrace uses.
2052 WARN_ON_ONCE(ftrace_start_up
< 0);
2054 if (ops
->flags
& FTRACE_OPS_FL_GLOBAL
) {
2057 WARN_ON_ONCE(global_start_up
< 0);
2058 /* Don't update hash if global still has users */
2059 if (global_start_up
) {
2060 WARN_ON_ONCE(!ftrace_start_up
);
2061 hash_disable
= false;
2066 ftrace_hash_rec_disable(ops
, 1);
2068 if (ops
!= &global_ops
|| !global_start_up
)
2069 ops
->flags
&= ~FTRACE_OPS_FL_ENABLED
;
2071 command
|= FTRACE_UPDATE_CALLS
;
2073 if (saved_ftrace_func
!= ftrace_trace_function
) {
2074 saved_ftrace_func
= ftrace_trace_function
;
2075 command
|= FTRACE_UPDATE_TRACE_FUNC
;
2078 if (!command
|| !ftrace_enabled
)
2081 ftrace_run_update_code(command
);
2084 static void ftrace_startup_sysctl(void)
2086 if (unlikely(ftrace_disabled
))
2089 /* Force update next time */
2090 saved_ftrace_func
= NULL
;
2091 /* ftrace_start_up is true if we want ftrace running */
2092 if (ftrace_start_up
)
2093 ftrace_run_update_code(FTRACE_UPDATE_CALLS
);
2096 static void ftrace_shutdown_sysctl(void)
2098 if (unlikely(ftrace_disabled
))
2101 /* ftrace_start_up is true if ftrace is running */
2102 if (ftrace_start_up
)
2103 ftrace_run_update_code(FTRACE_DISABLE_CALLS
);
2106 static cycle_t ftrace_update_time
;
2107 static unsigned long ftrace_update_cnt
;
2108 unsigned long ftrace_update_tot_cnt
;
2110 static int ops_traces_mod(struct ftrace_ops
*ops
)
2112 struct ftrace_hash
*hash
;
2114 hash
= ops
->filter_hash
;
2115 return ftrace_hash_empty(hash
);
2118 static int ftrace_update_code(struct module
*mod
)
2120 struct ftrace_page
*pg
;
2121 struct dyn_ftrace
*p
;
2122 cycle_t start
, stop
;
2123 unsigned long ref
= 0;
2127 * When adding a module, we need to check if tracers are
2128 * currently enabled and if they are set to trace all functions.
2129 * If they are, we need to enable the module functions as well
2130 * as update the reference counts for those function records.
2133 struct ftrace_ops
*ops
;
2135 for (ops
= ftrace_ops_list
;
2136 ops
!= &ftrace_list_end
; ops
= ops
->next
) {
2137 if (ops
->flags
& FTRACE_OPS_FL_ENABLED
&&
2138 ops_traces_mod(ops
))
2143 start
= ftrace_now(raw_smp_processor_id());
2144 ftrace_update_cnt
= 0;
2146 for (pg
= ftrace_new_pgs
; pg
; pg
= pg
->next
) {
2148 for (i
= 0; i
< pg
->index
; i
++) {
2149 /* If something went wrong, bail without enabling anything */
2150 if (unlikely(ftrace_disabled
))
2153 p
= &pg
->records
[i
];
2157 * Do the initial record conversion from mcount jump
2158 * to the NOP instructions.
2160 if (!ftrace_code_disable(mod
, p
))
2163 ftrace_update_cnt
++;
2166 * If the tracing is enabled, go ahead and enable the record.
2168 * The reason not to enable the record immediatelly is the
2169 * inherent check of ftrace_make_nop/ftrace_make_call for
2170 * correct previous instructions. Making first the NOP
2171 * conversion puts the module to the correct state, thus
2172 * passing the ftrace_make_call check.
2174 if (ftrace_start_up
&& ref
) {
2175 int failed
= __ftrace_replace_code(p
, 1);
2177 ftrace_bug(failed
, p
->ip
);
2182 ftrace_new_pgs
= NULL
;
2184 stop
= ftrace_now(raw_smp_processor_id());
2185 ftrace_update_time
= stop
- start
;
2186 ftrace_update_tot_cnt
+= ftrace_update_cnt
;
2191 static int ftrace_allocate_records(struct ftrace_page
*pg
, int count
)
2196 if (WARN_ON(!count
))
2199 order
= get_count_order(DIV_ROUND_UP(count
, ENTRIES_PER_PAGE
));
2202 * We want to fill as much as possible. No more than a page
2205 while ((PAGE_SIZE
<< order
) / ENTRY_SIZE
>= count
+ ENTRIES_PER_PAGE
)
2209 pg
->records
= (void *)__get_free_pages(GFP_KERNEL
| __GFP_ZERO
, order
);
2212 /* if we can't allocate this size, try something smaller */
2219 cnt
= (PAGE_SIZE
<< order
) / ENTRY_SIZE
;
2228 static struct ftrace_page
*
2229 ftrace_allocate_pages(unsigned long num_to_init
)
2231 struct ftrace_page
*start_pg
;
2232 struct ftrace_page
*pg
;
2239 start_pg
= pg
= kzalloc(sizeof(*pg
), GFP_KERNEL
);
2244 * Try to allocate as much as possible in one continues
2245 * location that fills in all of the space. We want to
2246 * waste as little space as possible.
2249 cnt
= ftrace_allocate_records(pg
, num_to_init
);
2257 pg
->next
= kzalloc(sizeof(*pg
), GFP_KERNEL
);
2268 order
= get_count_order(pg
->size
/ ENTRIES_PER_PAGE
);
2269 free_pages((unsigned long)pg
->records
, order
);
2270 start_pg
= pg
->next
;
2274 pr_info("ftrace: FAILED to allocate memory for functions\n");
2278 static int __init
ftrace_dyn_table_alloc(unsigned long num_to_init
)
2283 pr_info("ftrace: No functions to be traced?\n");
2287 cnt
= num_to_init
/ ENTRIES_PER_PAGE
;
2288 pr_info("ftrace: allocating %ld entries in %d pages\n",
2289 num_to_init
, cnt
+ 1);
2294 #define FTRACE_BUFF_MAX (KSYM_SYMBOL_LEN+4) /* room for wildcards */
2296 struct ftrace_iterator
{
2299 struct ftrace_page
*pg
;
2300 struct dyn_ftrace
*func
;
2301 struct ftrace_func_probe
*probe
;
2302 struct trace_parser parser
;
2303 struct ftrace_hash
*hash
;
2304 struct ftrace_ops
*ops
;
2311 t_hash_next(struct seq_file
*m
, loff_t
*pos
)
2313 struct ftrace_iterator
*iter
= m
->private;
2314 struct hlist_node
*hnd
= NULL
;
2315 struct hlist_head
*hhd
;
2321 hnd
= &iter
->probe
->node
;
2323 if (iter
->hidx
>= FTRACE_FUNC_HASHSIZE
)
2326 hhd
= &ftrace_func_hash
[iter
->hidx
];
2328 if (hlist_empty(hhd
)) {
2344 if (WARN_ON_ONCE(!hnd
))
2347 iter
->probe
= hlist_entry(hnd
, struct ftrace_func_probe
, node
);
2352 static void *t_hash_start(struct seq_file
*m
, loff_t
*pos
)
2354 struct ftrace_iterator
*iter
= m
->private;
2358 if (!(iter
->flags
& FTRACE_ITER_DO_HASH
))
2361 if (iter
->func_pos
> *pos
)
2365 for (l
= 0; l
<= (*pos
- iter
->func_pos
); ) {
2366 p
= t_hash_next(m
, &l
);
2373 /* Only set this if we have an item */
2374 iter
->flags
|= FTRACE_ITER_HASH
;
2380 t_hash_show(struct seq_file
*m
, struct ftrace_iterator
*iter
)
2382 struct ftrace_func_probe
*rec
;
2385 if (WARN_ON_ONCE(!rec
))
2388 if (rec
->ops
->print
)
2389 return rec
->ops
->print(m
, rec
->ip
, rec
->ops
, rec
->data
);
2391 seq_printf(m
, "%ps:%ps", (void *)rec
->ip
, (void *)rec
->ops
->func
);
2394 seq_printf(m
, ":%p", rec
->data
);
2401 t_next(struct seq_file
*m
, void *v
, loff_t
*pos
)
2403 struct ftrace_iterator
*iter
= m
->private;
2404 struct ftrace_ops
*ops
= iter
->ops
;
2405 struct dyn_ftrace
*rec
= NULL
;
2407 if (unlikely(ftrace_disabled
))
2410 if (iter
->flags
& FTRACE_ITER_HASH
)
2411 return t_hash_next(m
, pos
);
2414 iter
->pos
= iter
->func_pos
= *pos
;
2416 if (iter
->flags
& FTRACE_ITER_PRINTALL
)
2417 return t_hash_start(m
, pos
);
2420 if (iter
->idx
>= iter
->pg
->index
) {
2421 if (iter
->pg
->next
) {
2422 iter
->pg
= iter
->pg
->next
;
2427 rec
= &iter
->pg
->records
[iter
->idx
++];
2428 if (((iter
->flags
& FTRACE_ITER_FILTER
) &&
2429 !(ftrace_lookup_ip(ops
->filter_hash
, rec
->ip
))) ||
2431 ((iter
->flags
& FTRACE_ITER_NOTRACE
) &&
2432 !ftrace_lookup_ip(ops
->notrace_hash
, rec
->ip
)) ||
2434 ((iter
->flags
& FTRACE_ITER_ENABLED
) &&
2435 !(rec
->flags
& ~FTRACE_FL_MASK
))) {
2443 return t_hash_start(m
, pos
);
2450 static void reset_iter_read(struct ftrace_iterator
*iter
)
2454 iter
->flags
&= ~(FTRACE_ITER_PRINTALL
| FTRACE_ITER_HASH
);
2457 static void *t_start(struct seq_file
*m
, loff_t
*pos
)
2459 struct ftrace_iterator
*iter
= m
->private;
2460 struct ftrace_ops
*ops
= iter
->ops
;
2464 mutex_lock(&ftrace_lock
);
2466 if (unlikely(ftrace_disabled
))
2470 * If an lseek was done, then reset and start from beginning.
2472 if (*pos
< iter
->pos
)
2473 reset_iter_read(iter
);
2476 * For set_ftrace_filter reading, if we have the filter
2477 * off, we can short cut and just print out that all
2478 * functions are enabled.
2480 if (iter
->flags
& FTRACE_ITER_FILTER
&&
2481 ftrace_hash_empty(ops
->filter_hash
)) {
2483 return t_hash_start(m
, pos
);
2484 iter
->flags
|= FTRACE_ITER_PRINTALL
;
2485 /* reset in case of seek/pread */
2486 iter
->flags
&= ~FTRACE_ITER_HASH
;
2490 if (iter
->flags
& FTRACE_ITER_HASH
)
2491 return t_hash_start(m
, pos
);
2494 * Unfortunately, we need to restart at ftrace_pages_start
2495 * every time we let go of the ftrace_mutex. This is because
2496 * those pointers can change without the lock.
2498 iter
->pg
= ftrace_pages_start
;
2500 for (l
= 0; l
<= *pos
; ) {
2501 p
= t_next(m
, p
, &l
);
2507 return t_hash_start(m
, pos
);
2512 static void t_stop(struct seq_file
*m
, void *p
)
2514 mutex_unlock(&ftrace_lock
);
2517 static int t_show(struct seq_file
*m
, void *v
)
2519 struct ftrace_iterator
*iter
= m
->private;
2520 struct dyn_ftrace
*rec
;
2522 if (iter
->flags
& FTRACE_ITER_HASH
)
2523 return t_hash_show(m
, iter
);
2525 if (iter
->flags
& FTRACE_ITER_PRINTALL
) {
2526 seq_printf(m
, "#### all functions enabled ####\n");
2535 seq_printf(m
, "%ps", (void *)rec
->ip
);
2536 if (iter
->flags
& FTRACE_ITER_ENABLED
)
2537 seq_printf(m
, " (%ld)%s",
2538 rec
->flags
& ~FTRACE_FL_MASK
,
2539 rec
->flags
& FTRACE_FL_REGS
? " R" : "");
2540 seq_printf(m
, "\n");
2545 static const struct seq_operations show_ftrace_seq_ops
= {
2553 ftrace_avail_open(struct inode
*inode
, struct file
*file
)
2555 struct ftrace_iterator
*iter
;
2557 if (unlikely(ftrace_disabled
))
2560 iter
= __seq_open_private(file
, &show_ftrace_seq_ops
, sizeof(*iter
));
2562 iter
->pg
= ftrace_pages_start
;
2563 iter
->ops
= &global_ops
;
2566 return iter
? 0 : -ENOMEM
;
2570 ftrace_enabled_open(struct inode
*inode
, struct file
*file
)
2572 struct ftrace_iterator
*iter
;
2574 if (unlikely(ftrace_disabled
))
2577 iter
= __seq_open_private(file
, &show_ftrace_seq_ops
, sizeof(*iter
));
2579 iter
->pg
= ftrace_pages_start
;
2580 iter
->flags
= FTRACE_ITER_ENABLED
;
2581 iter
->ops
= &global_ops
;
2584 return iter
? 0 : -ENOMEM
;
2587 static void ftrace_filter_reset(struct ftrace_hash
*hash
)
2589 mutex_lock(&ftrace_lock
);
2590 ftrace_hash_clear(hash
);
2591 mutex_unlock(&ftrace_lock
);
2595 * ftrace_regex_open - initialize function tracer filter files
2596 * @ops: The ftrace_ops that hold the hash filters
2597 * @flag: The type of filter to process
2598 * @inode: The inode, usually passed in to your open routine
2599 * @file: The file, usually passed in to your open routine
2601 * ftrace_regex_open() initializes the filter files for the
2602 * @ops. Depending on @flag it may process the filter hash or
2603 * the notrace hash of @ops. With this called from the open
2604 * routine, you can use ftrace_filter_write() for the write
2605 * routine if @flag has FTRACE_ITER_FILTER set, or
2606 * ftrace_notrace_write() if @flag has FTRACE_ITER_NOTRACE set.
2607 * ftrace_regex_lseek() should be used as the lseek routine, and
2608 * release must call ftrace_regex_release().
2611 ftrace_regex_open(struct ftrace_ops
*ops
, int flag
,
2612 struct inode
*inode
, struct file
*file
)
2614 struct ftrace_iterator
*iter
;
2615 struct ftrace_hash
*hash
;
2618 if (unlikely(ftrace_disabled
))
2621 iter
= kzalloc(sizeof(*iter
), GFP_KERNEL
);
2625 if (trace_parser_get_init(&iter
->parser
, FTRACE_BUFF_MAX
)) {
2630 if (flag
& FTRACE_ITER_NOTRACE
)
2631 hash
= ops
->notrace_hash
;
2633 hash
= ops
->filter_hash
;
2638 if (file
->f_mode
& FMODE_WRITE
) {
2639 mutex_lock(&ftrace_lock
);
2640 iter
->hash
= alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS
, hash
);
2641 mutex_unlock(&ftrace_lock
);
2644 trace_parser_put(&iter
->parser
);
2650 mutex_lock(&ftrace_regex_lock
);
2652 if ((file
->f_mode
& FMODE_WRITE
) &&
2653 (file
->f_flags
& O_TRUNC
))
2654 ftrace_filter_reset(iter
->hash
);
2656 if (file
->f_mode
& FMODE_READ
) {
2657 iter
->pg
= ftrace_pages_start
;
2659 ret
= seq_open(file
, &show_ftrace_seq_ops
);
2661 struct seq_file
*m
= file
->private_data
;
2665 free_ftrace_hash(iter
->hash
);
2666 trace_parser_put(&iter
->parser
);
2670 file
->private_data
= iter
;
2671 mutex_unlock(&ftrace_regex_lock
);
2677 ftrace_filter_open(struct inode
*inode
, struct file
*file
)
2679 return ftrace_regex_open(&global_ops
,
2680 FTRACE_ITER_FILTER
| FTRACE_ITER_DO_HASH
,
2685 ftrace_notrace_open(struct inode
*inode
, struct file
*file
)
2687 return ftrace_regex_open(&global_ops
, FTRACE_ITER_NOTRACE
,
2692 ftrace_regex_lseek(struct file
*file
, loff_t offset
, int whence
)
2696 if (file
->f_mode
& FMODE_READ
)
2697 ret
= seq_lseek(file
, offset
, whence
);
2699 file
->f_pos
= ret
= 1;
2704 static int ftrace_match(char *str
, char *regex
, int len
, int type
)
2711 if (strcmp(str
, regex
) == 0)
2714 case MATCH_FRONT_ONLY
:
2715 if (strncmp(str
, regex
, len
) == 0)
2718 case MATCH_MIDDLE_ONLY
:
2719 if (strstr(str
, regex
))
2722 case MATCH_END_ONLY
:
2724 if (slen
>= len
&& memcmp(str
+ slen
- len
, regex
, len
) == 0)
2733 enter_record(struct ftrace_hash
*hash
, struct dyn_ftrace
*rec
, int not)
2735 struct ftrace_func_entry
*entry
;
2738 entry
= ftrace_lookup_ip(hash
, rec
->ip
);
2740 /* Do nothing if it doesn't exist */
2744 free_hash_entry(hash
, entry
);
2746 /* Do nothing if it exists */
2750 ret
= add_hash_entry(hash
, rec
->ip
);
2756 ftrace_match_record(struct dyn_ftrace
*rec
, char *mod
,
2757 char *regex
, int len
, int type
)
2759 char str
[KSYM_SYMBOL_LEN
];
2762 kallsyms_lookup(rec
->ip
, NULL
, NULL
, &modname
, str
);
2765 /* module lookup requires matching the module */
2766 if (!modname
|| strcmp(modname
, mod
))
2769 /* blank search means to match all funcs in the mod */
2774 return ftrace_match(str
, regex
, len
, type
);
2778 match_records(struct ftrace_hash
*hash
, char *buff
,
2779 int len
, char *mod
, int not)
2781 unsigned search_len
= 0;
2782 struct ftrace_page
*pg
;
2783 struct dyn_ftrace
*rec
;
2784 int type
= MATCH_FULL
;
2785 char *search
= buff
;
2790 type
= filter_parse_regex(buff
, len
, &search
, ¬);
2791 search_len
= strlen(search
);
2794 mutex_lock(&ftrace_lock
);
2796 if (unlikely(ftrace_disabled
))
2799 do_for_each_ftrace_rec(pg
, rec
) {
2800 if (ftrace_match_record(rec
, mod
, search
, search_len
, type
)) {
2801 ret
= enter_record(hash
, rec
, not);
2808 } while_for_each_ftrace_rec();
2810 mutex_unlock(&ftrace_lock
);
2816 ftrace_match_records(struct ftrace_hash
*hash
, char *buff
, int len
)
2818 return match_records(hash
, buff
, len
, NULL
, 0);
2822 ftrace_match_module_records(struct ftrace_hash
*hash
, char *buff
, char *mod
)
2826 /* blank or '*' mean the same */
2827 if (strcmp(buff
, "*") == 0)
2830 /* handle the case of 'dont filter this module' */
2831 if (strcmp(buff
, "!") == 0 || strcmp(buff
, "!*") == 0) {
2836 return match_records(hash
, buff
, strlen(buff
), mod
, not);
2840 * We register the module command as a template to show others how
2841 * to register the a command as well.
2845 ftrace_mod_callback(struct ftrace_hash
*hash
,
2846 char *func
, char *cmd
, char *param
, int enable
)
2852 * cmd == 'mod' because we only registered this func
2853 * for the 'mod' ftrace_func_command.
2854 * But if you register one func with multiple commands,
2855 * you can tell which command was used by the cmd
2859 /* we must have a module name */
2863 mod
= strsep(¶m
, ":");
2867 ret
= ftrace_match_module_records(hash
, func
, mod
);
2876 static struct ftrace_func_command ftrace_mod_cmd
= {
2878 .func
= ftrace_mod_callback
,
2881 static int __init
ftrace_mod_cmd_init(void)
2883 return register_ftrace_command(&ftrace_mod_cmd
);
2885 core_initcall(ftrace_mod_cmd_init
);
2887 static void function_trace_probe_call(unsigned long ip
, unsigned long parent_ip
,
2888 struct ftrace_ops
*op
, struct pt_regs
*pt_regs
)
2890 struct ftrace_func_probe
*entry
;
2891 struct hlist_head
*hhd
;
2892 struct hlist_node
*n
;
2895 key
= hash_long(ip
, FTRACE_HASH_BITS
);
2897 hhd
= &ftrace_func_hash
[key
];
2899 if (hlist_empty(hhd
))
2903 * Disable preemption for these calls to prevent a RCU grace
2904 * period. This syncs the hash iteration and freeing of items
2905 * on the hash. rcu_read_lock is too dangerous here.
2907 preempt_disable_notrace();
2908 hlist_for_each_entry_rcu(entry
, n
, hhd
, node
) {
2909 if (entry
->ip
== ip
)
2910 entry
->ops
->func(ip
, parent_ip
, &entry
->data
);
2912 preempt_enable_notrace();
2915 static struct ftrace_ops trace_probe_ops __read_mostly
=
2917 .func
= function_trace_probe_call
,
2920 static int ftrace_probe_registered
;
2922 static void __enable_ftrace_function_probe(void)
2927 if (ftrace_probe_registered
)
2930 for (i
= 0; i
< FTRACE_FUNC_HASHSIZE
; i
++) {
2931 struct hlist_head
*hhd
= &ftrace_func_hash
[i
];
2935 /* Nothing registered? */
2936 if (i
== FTRACE_FUNC_HASHSIZE
)
2939 ret
= __register_ftrace_function(&trace_probe_ops
);
2941 ret
= ftrace_startup(&trace_probe_ops
, 0);
2943 ftrace_probe_registered
= 1;
2946 static void __disable_ftrace_function_probe(void)
2951 if (!ftrace_probe_registered
)
2954 for (i
= 0; i
< FTRACE_FUNC_HASHSIZE
; i
++) {
2955 struct hlist_head
*hhd
= &ftrace_func_hash
[i
];
2960 /* no more funcs left */
2961 ret
= __unregister_ftrace_function(&trace_probe_ops
);
2963 ftrace_shutdown(&trace_probe_ops
, 0);
2965 ftrace_probe_registered
= 0;
2969 static void ftrace_free_entry(struct ftrace_func_probe
*entry
)
2971 if (entry
->ops
->free
)
2972 entry
->ops
->free(entry
->ops
, entry
->ip
, &entry
->data
);
2977 register_ftrace_function_probe(char *glob
, struct ftrace_probe_ops
*ops
,
2980 struct ftrace_func_probe
*entry
;
2981 struct ftrace_hash
**orig_hash
= &trace_probe_ops
.filter_hash
;
2982 struct ftrace_hash
*hash
;
2983 struct ftrace_page
*pg
;
2984 struct dyn_ftrace
*rec
;
2991 type
= filter_parse_regex(glob
, strlen(glob
), &search
, ¬);
2992 len
= strlen(search
);
2994 /* we do not support '!' for function probes */
2998 mutex_lock(&ftrace_lock
);
3000 hash
= alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS
, *orig_hash
);
3006 if (unlikely(ftrace_disabled
)) {
3011 do_for_each_ftrace_rec(pg
, rec
) {
3013 if (!ftrace_match_record(rec
, NULL
, search
, len
, type
))
3016 entry
= kmalloc(sizeof(*entry
), GFP_KERNEL
);
3018 /* If we did not process any, then return error */
3029 * The caller might want to do something special
3030 * for each function we find. We call the callback
3031 * to give the caller an opportunity to do so.
3034 if (ops
->init(ops
, rec
->ip
, &entry
->data
) < 0) {
3035 /* caller does not like this func */
3041 ret
= enter_record(hash
, rec
, 0);
3049 entry
->ip
= rec
->ip
;
3051 key
= hash_long(entry
->ip
, FTRACE_HASH_BITS
);
3052 hlist_add_head_rcu(&entry
->node
, &ftrace_func_hash
[key
]);
3054 } while_for_each_ftrace_rec();
3056 ret
= ftrace_hash_move(&trace_probe_ops
, 1, orig_hash
, hash
);
3060 __enable_ftrace_function_probe();
3063 mutex_unlock(&ftrace_lock
);
3064 free_ftrace_hash(hash
);
3070 PROBE_TEST_FUNC
= 1,
3075 __unregister_ftrace_function_probe(char *glob
, struct ftrace_probe_ops
*ops
,
3076 void *data
, int flags
)
3078 struct ftrace_func_entry
*rec_entry
;
3079 struct ftrace_func_probe
*entry
;
3080 struct ftrace_func_probe
*p
;
3081 struct ftrace_hash
**orig_hash
= &trace_probe_ops
.filter_hash
;
3082 struct list_head free_list
;
3083 struct ftrace_hash
*hash
;
3084 struct hlist_node
*n
, *tmp
;
3085 char str
[KSYM_SYMBOL_LEN
];
3086 int type
= MATCH_FULL
;
3090 if (glob
&& (strcmp(glob
, "*") == 0 || !strlen(glob
)))
3095 type
= filter_parse_regex(glob
, strlen(glob
), &search
, ¬);
3096 len
= strlen(search
);
3098 /* we do not support '!' for function probes */
3103 mutex_lock(&ftrace_lock
);
3105 hash
= alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS
, *orig_hash
);
3107 /* Hmm, should report this somehow */
3110 INIT_LIST_HEAD(&free_list
);
3112 for (i
= 0; i
< FTRACE_FUNC_HASHSIZE
; i
++) {
3113 struct hlist_head
*hhd
= &ftrace_func_hash
[i
];
3115 hlist_for_each_entry_safe(entry
, n
, tmp
, hhd
, node
) {
3117 /* break up if statements for readability */
3118 if ((flags
& PROBE_TEST_FUNC
) && entry
->ops
!= ops
)
3121 if ((flags
& PROBE_TEST_DATA
) && entry
->data
!= data
)
3124 /* do this last, since it is the most expensive */
3126 kallsyms_lookup(entry
->ip
, NULL
, NULL
,
3128 if (!ftrace_match(str
, glob
, len
, type
))
3132 rec_entry
= ftrace_lookup_ip(hash
, entry
->ip
);
3133 /* It is possible more than one entry had this ip */
3135 free_hash_entry(hash
, rec_entry
);
3137 hlist_del_rcu(&entry
->node
);
3138 list_add(&entry
->free_list
, &free_list
);
3141 __disable_ftrace_function_probe();
3143 * Remove after the disable is called. Otherwise, if the last
3144 * probe is removed, a null hash means *all enabled*.
3146 ftrace_hash_move(&trace_probe_ops
, 1, orig_hash
, hash
);
3147 synchronize_sched();
3148 list_for_each_entry_safe(entry
, p
, &free_list
, free_list
) {
3149 list_del(&entry
->free_list
);
3150 ftrace_free_entry(entry
);
3154 mutex_unlock(&ftrace_lock
);
3155 free_ftrace_hash(hash
);
3159 unregister_ftrace_function_probe(char *glob
, struct ftrace_probe_ops
*ops
,
3162 __unregister_ftrace_function_probe(glob
, ops
, data
,
3163 PROBE_TEST_FUNC
| PROBE_TEST_DATA
);
3167 unregister_ftrace_function_probe_func(char *glob
, struct ftrace_probe_ops
*ops
)
3169 __unregister_ftrace_function_probe(glob
, ops
, NULL
, PROBE_TEST_FUNC
);
3172 void unregister_ftrace_function_probe_all(char *glob
)
3174 __unregister_ftrace_function_probe(glob
, NULL
, NULL
, 0);
3177 static LIST_HEAD(ftrace_commands
);
3178 static DEFINE_MUTEX(ftrace_cmd_mutex
);
3180 int register_ftrace_command(struct ftrace_func_command
*cmd
)
3182 struct ftrace_func_command
*p
;
3185 mutex_lock(&ftrace_cmd_mutex
);
3186 list_for_each_entry(p
, &ftrace_commands
, list
) {
3187 if (strcmp(cmd
->name
, p
->name
) == 0) {
3192 list_add(&cmd
->list
, &ftrace_commands
);
3194 mutex_unlock(&ftrace_cmd_mutex
);
3199 int unregister_ftrace_command(struct ftrace_func_command
*cmd
)
3201 struct ftrace_func_command
*p
, *n
;
3204 mutex_lock(&ftrace_cmd_mutex
);
3205 list_for_each_entry_safe(p
, n
, &ftrace_commands
, list
) {
3206 if (strcmp(cmd
->name
, p
->name
) == 0) {
3208 list_del_init(&p
->list
);
3213 mutex_unlock(&ftrace_cmd_mutex
);
3218 static int ftrace_process_regex(struct ftrace_hash
*hash
,
3219 char *buff
, int len
, int enable
)
3221 char *func
, *command
, *next
= buff
;
3222 struct ftrace_func_command
*p
;
3225 func
= strsep(&next
, ":");
3228 ret
= ftrace_match_records(hash
, func
, len
);
3238 command
= strsep(&next
, ":");
3240 mutex_lock(&ftrace_cmd_mutex
);
3241 list_for_each_entry(p
, &ftrace_commands
, list
) {
3242 if (strcmp(p
->name
, command
) == 0) {
3243 ret
= p
->func(hash
, func
, command
, next
, enable
);
3248 mutex_unlock(&ftrace_cmd_mutex
);
3254 ftrace_regex_write(struct file
*file
, const char __user
*ubuf
,
3255 size_t cnt
, loff_t
*ppos
, int enable
)
3257 struct ftrace_iterator
*iter
;
3258 struct trace_parser
*parser
;
3264 mutex_lock(&ftrace_regex_lock
);
3267 if (unlikely(ftrace_disabled
))
3270 if (file
->f_mode
& FMODE_READ
) {
3271 struct seq_file
*m
= file
->private_data
;
3274 iter
= file
->private_data
;
3276 parser
= &iter
->parser
;
3277 read
= trace_get_user(parser
, ubuf
, cnt
, ppos
);
3279 if (read
>= 0 && trace_parser_loaded(parser
) &&
3280 !trace_parser_cont(parser
)) {
3281 ret
= ftrace_process_regex(iter
->hash
, parser
->buffer
,
3282 parser
->idx
, enable
);
3283 trace_parser_clear(parser
);
3290 mutex_unlock(&ftrace_regex_lock
);
3296 ftrace_filter_write(struct file
*file
, const char __user
*ubuf
,
3297 size_t cnt
, loff_t
*ppos
)
3299 return ftrace_regex_write(file
, ubuf
, cnt
, ppos
, 1);
3303 ftrace_notrace_write(struct file
*file
, const char __user
*ubuf
,
3304 size_t cnt
, loff_t
*ppos
)
3306 return ftrace_regex_write(file
, ubuf
, cnt
, ppos
, 0);
3310 ftrace_match_addr(struct ftrace_hash
*hash
, unsigned long ip
, int remove
)
3312 struct ftrace_func_entry
*entry
;
3314 if (!ftrace_location(ip
))
3318 entry
= ftrace_lookup_ip(hash
, ip
);
3321 free_hash_entry(hash
, entry
);
3325 return add_hash_entry(hash
, ip
);
3329 ftrace_set_hash(struct ftrace_ops
*ops
, unsigned char *buf
, int len
,
3330 unsigned long ip
, int remove
, int reset
, int enable
)
3332 struct ftrace_hash
**orig_hash
;
3333 struct ftrace_hash
*hash
;
3336 /* All global ops uses the global ops filters */
3337 if (ops
->flags
& FTRACE_OPS_FL_GLOBAL
)
3340 if (unlikely(ftrace_disabled
))
3344 orig_hash
= &ops
->filter_hash
;
3346 orig_hash
= &ops
->notrace_hash
;
3348 hash
= alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS
, *orig_hash
);
3352 mutex_lock(&ftrace_regex_lock
);
3354 ftrace_filter_reset(hash
);
3355 if (buf
&& !ftrace_match_records(hash
, buf
, len
)) {
3357 goto out_regex_unlock
;
3360 ret
= ftrace_match_addr(hash
, ip
, remove
);
3362 goto out_regex_unlock
;
3365 mutex_lock(&ftrace_lock
);
3366 ret
= ftrace_hash_move(ops
, enable
, orig_hash
, hash
);
3367 if (!ret
&& ops
->flags
& FTRACE_OPS_FL_ENABLED
3369 ftrace_run_update_code(FTRACE_UPDATE_CALLS
);
3371 mutex_unlock(&ftrace_lock
);
3374 mutex_unlock(&ftrace_regex_lock
);
3376 free_ftrace_hash(hash
);
3381 ftrace_set_addr(struct ftrace_ops
*ops
, unsigned long ip
, int remove
,
3382 int reset
, int enable
)
3384 return ftrace_set_hash(ops
, 0, 0, ip
, remove
, reset
, enable
);
3388 * ftrace_set_filter_ip - set a function to filter on in ftrace by address
3389 * @ops - the ops to set the filter with
3390 * @ip - the address to add to or remove from the filter.
3391 * @remove - non zero to remove the ip from the filter
3392 * @reset - non zero to reset all filters before applying this filter.
3394 * Filters denote which functions should be enabled when tracing is enabled
3395 * If @ip is NULL, it failes to update filter.
3397 int ftrace_set_filter_ip(struct ftrace_ops
*ops
, unsigned long ip
,
3398 int remove
, int reset
)
3400 return ftrace_set_addr(ops
, ip
, remove
, reset
, 1);
3402 EXPORT_SYMBOL_GPL(ftrace_set_filter_ip
);
3405 ftrace_set_regex(struct ftrace_ops
*ops
, unsigned char *buf
, int len
,
3406 int reset
, int enable
)
3408 return ftrace_set_hash(ops
, buf
, len
, 0, 0, reset
, enable
);
3412 * ftrace_set_filter - set a function to filter on in ftrace
3413 * @ops - the ops to set the filter with
3414 * @buf - the string that holds the function filter text.
3415 * @len - the length of the string.
3416 * @reset - non zero to reset all filters before applying this filter.
3418 * Filters denote which functions should be enabled when tracing is enabled.
3419 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
3421 int ftrace_set_filter(struct ftrace_ops
*ops
, unsigned char *buf
,
3424 return ftrace_set_regex(ops
, buf
, len
, reset
, 1);
3426 EXPORT_SYMBOL_GPL(ftrace_set_filter
);
3429 * ftrace_set_notrace - set a function to not trace in ftrace
3430 * @ops - the ops to set the notrace filter with
3431 * @buf - the string that holds the function notrace text.
3432 * @len - the length of the string.
3433 * @reset - non zero to reset all filters before applying this filter.
3435 * Notrace Filters denote which functions should not be enabled when tracing
3436 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
3439 int ftrace_set_notrace(struct ftrace_ops
*ops
, unsigned char *buf
,
3442 return ftrace_set_regex(ops
, buf
, len
, reset
, 0);
3444 EXPORT_SYMBOL_GPL(ftrace_set_notrace
);
3446 * ftrace_set_filter - set a function to filter on in ftrace
3447 * @ops - the ops to set the filter with
3448 * @buf - the string that holds the function filter text.
3449 * @len - the length of the string.
3450 * @reset - non zero to reset all filters before applying this filter.
3452 * Filters denote which functions should be enabled when tracing is enabled.
3453 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
3455 void ftrace_set_global_filter(unsigned char *buf
, int len
, int reset
)
3457 ftrace_set_regex(&global_ops
, buf
, len
, reset
, 1);
3459 EXPORT_SYMBOL_GPL(ftrace_set_global_filter
);
3462 * ftrace_set_notrace - set a function to not trace in ftrace
3463 * @ops - the ops to set the notrace filter with
3464 * @buf - the string that holds the function notrace text.
3465 * @len - the length of the string.
3466 * @reset - non zero to reset all filters before applying this filter.
3468 * Notrace Filters denote which functions should not be enabled when tracing
3469 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
3472 void ftrace_set_global_notrace(unsigned char *buf
, int len
, int reset
)
3474 ftrace_set_regex(&global_ops
, buf
, len
, reset
, 0);
3476 EXPORT_SYMBOL_GPL(ftrace_set_global_notrace
);
3479 * command line interface to allow users to set filters on boot up.
3481 #define FTRACE_FILTER_SIZE COMMAND_LINE_SIZE
3482 static char ftrace_notrace_buf
[FTRACE_FILTER_SIZE
] __initdata
;
3483 static char ftrace_filter_buf
[FTRACE_FILTER_SIZE
] __initdata
;
3485 static int __init
set_ftrace_notrace(char *str
)
3487 strlcpy(ftrace_notrace_buf
, str
, FTRACE_FILTER_SIZE
);
3490 __setup("ftrace_notrace=", set_ftrace_notrace
);
3492 static int __init
set_ftrace_filter(char *str
)
3494 strlcpy(ftrace_filter_buf
, str
, FTRACE_FILTER_SIZE
);
3497 __setup("ftrace_filter=", set_ftrace_filter
);
3499 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
3500 static char ftrace_graph_buf
[FTRACE_FILTER_SIZE
] __initdata
;
3501 static int ftrace_set_func(unsigned long *array
, int *idx
, char *buffer
);
3503 static int __init
set_graph_function(char *str
)
3505 strlcpy(ftrace_graph_buf
, str
, FTRACE_FILTER_SIZE
);
3508 __setup("ftrace_graph_filter=", set_graph_function
);
3510 static void __init
set_ftrace_early_graph(char *buf
)
3516 func
= strsep(&buf
, ",");
3517 /* we allow only one expression at a time */
3518 ret
= ftrace_set_func(ftrace_graph_funcs
, &ftrace_graph_count
,
3521 printk(KERN_DEBUG
"ftrace: function %s not "
3522 "traceable\n", func
);
3525 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
3528 ftrace_set_early_filter(struct ftrace_ops
*ops
, char *buf
, int enable
)
3533 func
= strsep(&buf
, ",");
3534 ftrace_set_regex(ops
, func
, strlen(func
), 0, enable
);
3538 static void __init
set_ftrace_early_filters(void)
3540 if (ftrace_filter_buf
[0])
3541 ftrace_set_early_filter(&global_ops
, ftrace_filter_buf
, 1);
3542 if (ftrace_notrace_buf
[0])
3543 ftrace_set_early_filter(&global_ops
, ftrace_notrace_buf
, 0);
3544 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
3545 if (ftrace_graph_buf
[0])
3546 set_ftrace_early_graph(ftrace_graph_buf
);
3547 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
3550 int ftrace_regex_release(struct inode
*inode
, struct file
*file
)
3552 struct seq_file
*m
= (struct seq_file
*)file
->private_data
;
3553 struct ftrace_iterator
*iter
;
3554 struct ftrace_hash
**orig_hash
;
3555 struct trace_parser
*parser
;
3559 mutex_lock(&ftrace_regex_lock
);
3560 if (file
->f_mode
& FMODE_READ
) {
3563 seq_release(inode
, file
);
3565 iter
= file
->private_data
;
3567 parser
= &iter
->parser
;
3568 if (trace_parser_loaded(parser
)) {
3569 parser
->buffer
[parser
->idx
] = 0;
3570 ftrace_match_records(iter
->hash
, parser
->buffer
, parser
->idx
);
3573 trace_parser_put(parser
);
3575 if (file
->f_mode
& FMODE_WRITE
) {
3576 filter_hash
= !!(iter
->flags
& FTRACE_ITER_FILTER
);
3579 orig_hash
= &iter
->ops
->filter_hash
;
3581 orig_hash
= &iter
->ops
->notrace_hash
;
3583 mutex_lock(&ftrace_lock
);
3584 ret
= ftrace_hash_move(iter
->ops
, filter_hash
,
3585 orig_hash
, iter
->hash
);
3586 if (!ret
&& (iter
->ops
->flags
& FTRACE_OPS_FL_ENABLED
)
3588 ftrace_run_update_code(FTRACE_UPDATE_CALLS
);
3590 mutex_unlock(&ftrace_lock
);
3592 free_ftrace_hash(iter
->hash
);
3595 mutex_unlock(&ftrace_regex_lock
);
3599 static const struct file_operations ftrace_avail_fops
= {
3600 .open
= ftrace_avail_open
,
3602 .llseek
= seq_lseek
,
3603 .release
= seq_release_private
,
3606 static const struct file_operations ftrace_enabled_fops
= {
3607 .open
= ftrace_enabled_open
,
3609 .llseek
= seq_lseek
,
3610 .release
= seq_release_private
,
3613 static const struct file_operations ftrace_filter_fops
= {
3614 .open
= ftrace_filter_open
,
3616 .write
= ftrace_filter_write
,
3617 .llseek
= ftrace_regex_lseek
,
3618 .release
= ftrace_regex_release
,
3621 static const struct file_operations ftrace_notrace_fops
= {
3622 .open
= ftrace_notrace_open
,
3624 .write
= ftrace_notrace_write
,
3625 .llseek
= ftrace_regex_lseek
,
3626 .release
= ftrace_regex_release
,
3629 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
3631 static DEFINE_MUTEX(graph_lock
);
3633 int ftrace_graph_count
;
3634 int ftrace_graph_filter_enabled
;
3635 unsigned long ftrace_graph_funcs
[FTRACE_GRAPH_MAX_FUNCS
] __read_mostly
;
3638 __g_next(struct seq_file
*m
, loff_t
*pos
)
3640 if (*pos
>= ftrace_graph_count
)
3642 return &ftrace_graph_funcs
[*pos
];
3646 g_next(struct seq_file
*m
, void *v
, loff_t
*pos
)
3649 return __g_next(m
, pos
);
3652 static void *g_start(struct seq_file
*m
, loff_t
*pos
)
3654 mutex_lock(&graph_lock
);
3656 /* Nothing, tell g_show to print all functions are enabled */
3657 if (!ftrace_graph_filter_enabled
&& !*pos
)
3660 return __g_next(m
, pos
);
3663 static void g_stop(struct seq_file
*m
, void *p
)
3665 mutex_unlock(&graph_lock
);
3668 static int g_show(struct seq_file
*m
, void *v
)
3670 unsigned long *ptr
= v
;
3675 if (ptr
== (unsigned long *)1) {
3676 seq_printf(m
, "#### all functions enabled ####\n");
3680 seq_printf(m
, "%ps\n", (void *)*ptr
);
3685 static const struct seq_operations ftrace_graph_seq_ops
= {
3693 ftrace_graph_open(struct inode
*inode
, struct file
*file
)
3697 if (unlikely(ftrace_disabled
))
3700 mutex_lock(&graph_lock
);
3701 if ((file
->f_mode
& FMODE_WRITE
) &&
3702 (file
->f_flags
& O_TRUNC
)) {
3703 ftrace_graph_filter_enabled
= 0;
3704 ftrace_graph_count
= 0;
3705 memset(ftrace_graph_funcs
, 0, sizeof(ftrace_graph_funcs
));
3707 mutex_unlock(&graph_lock
);
3709 if (file
->f_mode
& FMODE_READ
)
3710 ret
= seq_open(file
, &ftrace_graph_seq_ops
);
3716 ftrace_graph_release(struct inode
*inode
, struct file
*file
)
3718 if (file
->f_mode
& FMODE_READ
)
3719 seq_release(inode
, file
);
3724 ftrace_set_func(unsigned long *array
, int *idx
, char *buffer
)
3726 struct dyn_ftrace
*rec
;
3727 struct ftrace_page
*pg
;
3736 type
= filter_parse_regex(buffer
, strlen(buffer
), &search
, ¬);
3737 if (!not && *idx
>= FTRACE_GRAPH_MAX_FUNCS
)
3740 search_len
= strlen(search
);
3742 mutex_lock(&ftrace_lock
);
3744 if (unlikely(ftrace_disabled
)) {
3745 mutex_unlock(&ftrace_lock
);
3749 do_for_each_ftrace_rec(pg
, rec
) {
3751 if (ftrace_match_record(rec
, NULL
, search
, search_len
, type
)) {
3752 /* if it is in the array */
3754 for (i
= 0; i
< *idx
; i
++) {
3755 if (array
[i
] == rec
->ip
) {
3764 array
[(*idx
)++] = rec
->ip
;
3765 if (*idx
>= FTRACE_GRAPH_MAX_FUNCS
)
3770 array
[i
] = array
[--(*idx
)];
3776 } while_for_each_ftrace_rec();
3778 mutex_unlock(&ftrace_lock
);
3783 ftrace_graph_filter_enabled
= !!(*idx
);
3789 ftrace_graph_write(struct file
*file
, const char __user
*ubuf
,
3790 size_t cnt
, loff_t
*ppos
)
3792 struct trace_parser parser
;
3798 mutex_lock(&graph_lock
);
3800 if (trace_parser_get_init(&parser
, FTRACE_BUFF_MAX
)) {
3805 read
= trace_get_user(&parser
, ubuf
, cnt
, ppos
);
3807 if (read
>= 0 && trace_parser_loaded((&parser
))) {
3808 parser
.buffer
[parser
.idx
] = 0;
3810 /* we allow only one expression at a time */
3811 ret
= ftrace_set_func(ftrace_graph_funcs
, &ftrace_graph_count
,
3820 trace_parser_put(&parser
);
3822 mutex_unlock(&graph_lock
);
3827 static const struct file_operations ftrace_graph_fops
= {
3828 .open
= ftrace_graph_open
,
3830 .write
= ftrace_graph_write
,
3831 .release
= ftrace_graph_release
,
3832 .llseek
= seq_lseek
,
3834 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
3836 static __init
int ftrace_init_dyn_debugfs(struct dentry
*d_tracer
)
3839 trace_create_file("available_filter_functions", 0444,
3840 d_tracer
, NULL
, &ftrace_avail_fops
);
3842 trace_create_file("enabled_functions", 0444,
3843 d_tracer
, NULL
, &ftrace_enabled_fops
);
3845 trace_create_file("set_ftrace_filter", 0644, d_tracer
,
3846 NULL
, &ftrace_filter_fops
);
3848 trace_create_file("set_ftrace_notrace", 0644, d_tracer
,
3849 NULL
, &ftrace_notrace_fops
);
3851 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
3852 trace_create_file("set_graph_function", 0444, d_tracer
,
3854 &ftrace_graph_fops
);
3855 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
3860 static int ftrace_cmp_ips(const void *a
, const void *b
)
3862 const unsigned long *ipa
= a
;
3863 const unsigned long *ipb
= b
;
3872 static void ftrace_swap_ips(void *a
, void *b
, int size
)
3874 unsigned long *ipa
= a
;
3875 unsigned long *ipb
= b
;
3883 static int ftrace_process_locs(struct module
*mod
,
3884 unsigned long *start
,
3887 struct ftrace_page
*start_pg
;
3888 struct ftrace_page
*pg
;
3889 struct dyn_ftrace
*rec
;
3890 unsigned long count
;
3893 unsigned long flags
= 0; /* Shut up gcc */
3896 count
= end
- start
;
3901 sort(start
, count
, sizeof(*start
),
3902 ftrace_cmp_ips
, ftrace_swap_ips
);
3904 start_pg
= ftrace_allocate_pages(count
);
3908 mutex_lock(&ftrace_lock
);
3911 * Core and each module needs their own pages, as
3912 * modules will free them when they are removed.
3913 * Force a new page to be allocated for modules.
3916 WARN_ON(ftrace_pages
|| ftrace_pages_start
);
3917 /* First initialization */
3918 ftrace_pages
= ftrace_pages_start
= start_pg
;
3923 if (WARN_ON(ftrace_pages
->next
)) {
3924 /* Hmm, we have free pages? */
3925 while (ftrace_pages
->next
)
3926 ftrace_pages
= ftrace_pages
->next
;
3929 ftrace_pages
->next
= start_pg
;
3935 addr
= ftrace_call_adjust(*p
++);
3937 * Some architecture linkers will pad between
3938 * the different mcount_loc sections of different
3939 * object files to satisfy alignments.
3940 * Skip any NULL pointers.
3945 if (pg
->index
== pg
->size
) {
3946 /* We should have allocated enough */
3947 if (WARN_ON(!pg
->next
))
3952 rec
= &pg
->records
[pg
->index
++];
3956 /* We should have used all pages */
3959 /* Assign the last page to ftrace_pages */
3962 /* These new locations need to be initialized */
3963 ftrace_new_pgs
= start_pg
;
3966 * We only need to disable interrupts on start up
3967 * because we are modifying code that an interrupt
3968 * may execute, and the modification is not atomic.
3969 * But for modules, nothing runs the code we modify
3970 * until we are finished with it, and there's no
3971 * reason to cause large interrupt latencies while we do it.
3974 local_irq_save(flags
);
3975 ftrace_update_code(mod
);
3977 local_irq_restore(flags
);
3980 mutex_unlock(&ftrace_lock
);
3985 #ifdef CONFIG_MODULES
3987 #define next_to_ftrace_page(p) container_of(p, struct ftrace_page, next)
3989 void ftrace_release_mod(struct module
*mod
)
3991 struct dyn_ftrace
*rec
;
3992 struct ftrace_page
**last_pg
;
3993 struct ftrace_page
*pg
;
3996 mutex_lock(&ftrace_lock
);
3998 if (ftrace_disabled
)
4002 * Each module has its own ftrace_pages, remove
4003 * them from the list.
4005 last_pg
= &ftrace_pages_start
;
4006 for (pg
= ftrace_pages_start
; pg
; pg
= *last_pg
) {
4007 rec
= &pg
->records
[0];
4008 if (within_module_core(rec
->ip
, mod
)) {
4010 * As core pages are first, the first
4011 * page should never be a module page.
4013 if (WARN_ON(pg
== ftrace_pages_start
))
4016 /* Check if we are deleting the last page */
4017 if (pg
== ftrace_pages
)
4018 ftrace_pages
= next_to_ftrace_page(last_pg
);
4020 *last_pg
= pg
->next
;
4021 order
= get_count_order(pg
->size
/ ENTRIES_PER_PAGE
);
4022 free_pages((unsigned long)pg
->records
, order
);
4025 last_pg
= &pg
->next
;
4028 mutex_unlock(&ftrace_lock
);
4031 static void ftrace_init_module(struct module
*mod
,
4032 unsigned long *start
, unsigned long *end
)
4034 if (ftrace_disabled
|| start
== end
)
4036 ftrace_process_locs(mod
, start
, end
);
4039 static int ftrace_module_notify_enter(struct notifier_block
*self
,
4040 unsigned long val
, void *data
)
4042 struct module
*mod
= data
;
4044 if (val
== MODULE_STATE_COMING
)
4045 ftrace_init_module(mod
, mod
->ftrace_callsites
,
4046 mod
->ftrace_callsites
+
4047 mod
->num_ftrace_callsites
);
4051 static int ftrace_module_notify_exit(struct notifier_block
*self
,
4052 unsigned long val
, void *data
)
4054 struct module
*mod
= data
;
4056 if (val
== MODULE_STATE_GOING
)
4057 ftrace_release_mod(mod
);
4062 static int ftrace_module_notify_enter(struct notifier_block
*self
,
4063 unsigned long val
, void *data
)
4067 static int ftrace_module_notify_exit(struct notifier_block
*self
,
4068 unsigned long val
, void *data
)
4072 #endif /* CONFIG_MODULES */
4074 struct notifier_block ftrace_module_enter_nb
= {
4075 .notifier_call
= ftrace_module_notify_enter
,
4076 .priority
= INT_MAX
, /* Run before anything that can use kprobes */
4079 struct notifier_block ftrace_module_exit_nb
= {
4080 .notifier_call
= ftrace_module_notify_exit
,
4081 .priority
= INT_MIN
, /* Run after anything that can remove kprobes */
4084 extern unsigned long __start_mcount_loc
[];
4085 extern unsigned long __stop_mcount_loc
[];
4087 void __init
ftrace_init(void)
4089 unsigned long count
, addr
, flags
;
4092 /* Keep the ftrace pointer to the stub */
4093 addr
= (unsigned long)ftrace_stub
;
4095 local_irq_save(flags
);
4096 ftrace_dyn_arch_init(&addr
);
4097 local_irq_restore(flags
);
4099 /* ftrace_dyn_arch_init places the return code in addr */
4103 count
= __stop_mcount_loc
- __start_mcount_loc
;
4105 ret
= ftrace_dyn_table_alloc(count
);
4109 last_ftrace_enabled
= ftrace_enabled
= 1;
4111 ret
= ftrace_process_locs(NULL
,
4115 ret
= register_module_notifier(&ftrace_module_enter_nb
);
4117 pr_warning("Failed to register trace ftrace module enter notifier\n");
4119 ret
= register_module_notifier(&ftrace_module_exit_nb
);
4121 pr_warning("Failed to register trace ftrace module exit notifier\n");
4123 set_ftrace_early_filters();
4127 ftrace_disabled
= 1;
4132 static struct ftrace_ops global_ops
= {
4133 .func
= ftrace_stub
,
4134 .flags
= FTRACE_OPS_FL_RECURSION_SAFE
,
4137 static int __init
ftrace_nodyn_init(void)
4142 core_initcall(ftrace_nodyn_init
);
4144 static inline int ftrace_init_dyn_debugfs(struct dentry
*d_tracer
) { return 0; }
4145 static inline void ftrace_startup_enable(int command
) { }
4146 /* Keep as macros so we do not need to define the commands */
4147 # define ftrace_startup(ops, command) \
4149 (ops)->flags |= FTRACE_OPS_FL_ENABLED; \
4152 # define ftrace_shutdown(ops, command) do { } while (0)
4153 # define ftrace_startup_sysctl() do { } while (0)
4154 # define ftrace_shutdown_sysctl() do { } while (0)
4157 ftrace_ops_test(struct ftrace_ops
*ops
, unsigned long ip
)
4162 #endif /* CONFIG_DYNAMIC_FTRACE */
4165 ftrace_ops_control_func(unsigned long ip
, unsigned long parent_ip
,
4166 struct ftrace_ops
*op
, struct pt_regs
*regs
)
4168 if (unlikely(trace_recursion_test(TRACE_CONTROL_BIT
)))
4172 * Some of the ops may be dynamically allocated,
4173 * they must be freed after a synchronize_sched().
4175 preempt_disable_notrace();
4176 trace_recursion_set(TRACE_CONTROL_BIT
);
4177 do_for_each_ftrace_op(op
, ftrace_control_list
) {
4178 if (!ftrace_function_local_disabled(op
) &&
4179 ftrace_ops_test(op
, ip
))
4180 op
->func(ip
, parent_ip
, op
, regs
);
4181 } while_for_each_ftrace_op(op
);
4182 trace_recursion_clear(TRACE_CONTROL_BIT
);
4183 preempt_enable_notrace();
4186 static struct ftrace_ops control_ops
= {
4187 .func
= ftrace_ops_control_func
,
4188 .flags
= FTRACE_OPS_FL_RECURSION_SAFE
,
4192 __ftrace_ops_list_func(unsigned long ip
, unsigned long parent_ip
,
4193 struct ftrace_ops
*ignored
, struct pt_regs
*regs
)
4195 struct ftrace_ops
*op
;
4198 if (function_trace_stop
)
4201 bit
= trace_test_and_set_recursion(TRACE_LIST_START
, TRACE_LIST_MAX
);
4206 * Some of the ops may be dynamically allocated,
4207 * they must be freed after a synchronize_sched().
4209 preempt_disable_notrace();
4210 do_for_each_ftrace_op(op
, ftrace_ops_list
) {
4211 if (ftrace_ops_test(op
, ip
))
4212 op
->func(ip
, parent_ip
, op
, regs
);
4213 } while_for_each_ftrace_op(op
);
4214 preempt_enable_notrace();
4215 trace_clear_recursion(bit
);
4219 * Some archs only support passing ip and parent_ip. Even though
4220 * the list function ignores the op parameter, we do not want any
4221 * C side effects, where a function is called without the caller
4222 * sending a third parameter.
4223 * Archs are to support both the regs and ftrace_ops at the same time.
4224 * If they support ftrace_ops, it is assumed they support regs.
4225 * If call backs want to use regs, they must either check for regs
4226 * being NULL, or CONFIG_DYNAMIC_FTRACE_WITH_REGS.
4227 * Note, CONFIG_DYNAMIC_FTRACE_WITH_REGS expects a full regs to be saved.
4228 * An architecture can pass partial regs with ftrace_ops and still
4229 * set the ARCH_SUPPORT_FTARCE_OPS.
4231 #if ARCH_SUPPORTS_FTRACE_OPS
4232 static void ftrace_ops_list_func(unsigned long ip
, unsigned long parent_ip
,
4233 struct ftrace_ops
*op
, struct pt_regs
*regs
)
4235 __ftrace_ops_list_func(ip
, parent_ip
, NULL
, regs
);
4238 static void ftrace_ops_no_ops(unsigned long ip
, unsigned long parent_ip
)
4240 __ftrace_ops_list_func(ip
, parent_ip
, NULL
, NULL
);
4244 static void clear_ftrace_swapper(void)
4246 struct task_struct
*p
;
4250 for_each_online_cpu(cpu
) {
4252 clear_tsk_trace_trace(p
);
4257 static void set_ftrace_swapper(void)
4259 struct task_struct
*p
;
4263 for_each_online_cpu(cpu
) {
4265 set_tsk_trace_trace(p
);
4270 static void clear_ftrace_pid(struct pid
*pid
)
4272 struct task_struct
*p
;
4275 do_each_pid_task(pid
, PIDTYPE_PID
, p
) {
4276 clear_tsk_trace_trace(p
);
4277 } while_each_pid_task(pid
, PIDTYPE_PID
, p
);
4283 static void set_ftrace_pid(struct pid
*pid
)
4285 struct task_struct
*p
;
4288 do_each_pid_task(pid
, PIDTYPE_PID
, p
) {
4289 set_tsk_trace_trace(p
);
4290 } while_each_pid_task(pid
, PIDTYPE_PID
, p
);
4294 static void clear_ftrace_pid_task(struct pid
*pid
)
4296 if (pid
== ftrace_swapper_pid
)
4297 clear_ftrace_swapper();
4299 clear_ftrace_pid(pid
);
4302 static void set_ftrace_pid_task(struct pid
*pid
)
4304 if (pid
== ftrace_swapper_pid
)
4305 set_ftrace_swapper();
4307 set_ftrace_pid(pid
);
4310 static int ftrace_pid_add(int p
)
4313 struct ftrace_pid
*fpid
;
4316 mutex_lock(&ftrace_lock
);
4319 pid
= ftrace_swapper_pid
;
4321 pid
= find_get_pid(p
);
4328 list_for_each_entry(fpid
, &ftrace_pids
, list
)
4329 if (fpid
->pid
== pid
)
4334 fpid
= kmalloc(sizeof(*fpid
), GFP_KERNEL
);
4338 list_add(&fpid
->list
, &ftrace_pids
);
4341 set_ftrace_pid_task(pid
);
4343 ftrace_update_pid_func();
4344 ftrace_startup_enable(0);
4346 mutex_unlock(&ftrace_lock
);
4350 if (pid
!= ftrace_swapper_pid
)
4354 mutex_unlock(&ftrace_lock
);
4358 static void ftrace_pid_reset(void)
4360 struct ftrace_pid
*fpid
, *safe
;
4362 mutex_lock(&ftrace_lock
);
4363 list_for_each_entry_safe(fpid
, safe
, &ftrace_pids
, list
) {
4364 struct pid
*pid
= fpid
->pid
;
4366 clear_ftrace_pid_task(pid
);
4368 list_del(&fpid
->list
);
4372 ftrace_update_pid_func();
4373 ftrace_startup_enable(0);
4375 mutex_unlock(&ftrace_lock
);
4378 static void *fpid_start(struct seq_file
*m
, loff_t
*pos
)
4380 mutex_lock(&ftrace_lock
);
4382 if (list_empty(&ftrace_pids
) && (!*pos
))
4385 return seq_list_start(&ftrace_pids
, *pos
);
4388 static void *fpid_next(struct seq_file
*m
, void *v
, loff_t
*pos
)
4393 return seq_list_next(v
, &ftrace_pids
, pos
);
4396 static void fpid_stop(struct seq_file
*m
, void *p
)
4398 mutex_unlock(&ftrace_lock
);
4401 static int fpid_show(struct seq_file
*m
, void *v
)
4403 const struct ftrace_pid
*fpid
= list_entry(v
, struct ftrace_pid
, list
);
4405 if (v
== (void *)1) {
4406 seq_printf(m
, "no pid\n");
4410 if (fpid
->pid
== ftrace_swapper_pid
)
4411 seq_printf(m
, "swapper tasks\n");
4413 seq_printf(m
, "%u\n", pid_vnr(fpid
->pid
));
4418 static const struct seq_operations ftrace_pid_sops
= {
4419 .start
= fpid_start
,
4426 ftrace_pid_open(struct inode
*inode
, struct file
*file
)
4430 if ((file
->f_mode
& FMODE_WRITE
) &&
4431 (file
->f_flags
& O_TRUNC
))
4434 if (file
->f_mode
& FMODE_READ
)
4435 ret
= seq_open(file
, &ftrace_pid_sops
);
4441 ftrace_pid_write(struct file
*filp
, const char __user
*ubuf
,
4442 size_t cnt
, loff_t
*ppos
)
4448 if (cnt
>= sizeof(buf
))
4451 if (copy_from_user(&buf
, ubuf
, cnt
))
4457 * Allow "echo > set_ftrace_pid" or "echo -n '' > set_ftrace_pid"
4458 * to clean the filter quietly.
4460 tmp
= strstrip(buf
);
4461 if (strlen(tmp
) == 0)
4464 ret
= kstrtol(tmp
, 10, &val
);
4468 ret
= ftrace_pid_add(val
);
4470 return ret
? ret
: cnt
;
4474 ftrace_pid_release(struct inode
*inode
, struct file
*file
)
4476 if (file
->f_mode
& FMODE_READ
)
4477 seq_release(inode
, file
);
4482 static const struct file_operations ftrace_pid_fops
= {
4483 .open
= ftrace_pid_open
,
4484 .write
= ftrace_pid_write
,
4486 .llseek
= seq_lseek
,
4487 .release
= ftrace_pid_release
,
4490 static __init
int ftrace_init_debugfs(void)
4492 struct dentry
*d_tracer
;
4494 d_tracer
= tracing_init_dentry();
4498 ftrace_init_dyn_debugfs(d_tracer
);
4500 trace_create_file("set_ftrace_pid", 0644, d_tracer
,
4501 NULL
, &ftrace_pid_fops
);
4503 ftrace_profile_debugfs(d_tracer
);
4507 fs_initcall(ftrace_init_debugfs
);
4510 * ftrace_kill - kill ftrace
4512 * This function should be used by panic code. It stops ftrace
4513 * but in a not so nice way. If you need to simply kill ftrace
4514 * from a non-atomic section, use ftrace_kill.
4516 void ftrace_kill(void)
4518 ftrace_disabled
= 1;
4520 clear_ftrace_function();
4524 * Test if ftrace is dead or not.
4526 int ftrace_is_dead(void)
4528 return ftrace_disabled
;
4532 * register_ftrace_function - register a function for profiling
4533 * @ops - ops structure that holds the function for profiling.
4535 * Register a function to be called by all functions in the
4538 * Note: @ops->func and all the functions it calls must be labeled
4539 * with "notrace", otherwise it will go into a
4542 int register_ftrace_function(struct ftrace_ops
*ops
)
4546 mutex_lock(&ftrace_lock
);
4548 ret
= __register_ftrace_function(ops
);
4550 ret
= ftrace_startup(ops
, 0);
4552 mutex_unlock(&ftrace_lock
);
4556 EXPORT_SYMBOL_GPL(register_ftrace_function
);
4559 * unregister_ftrace_function - unregister a function for profiling.
4560 * @ops - ops structure that holds the function to unregister
4562 * Unregister a function that was added to be called by ftrace profiling.
4564 int unregister_ftrace_function(struct ftrace_ops
*ops
)
4568 mutex_lock(&ftrace_lock
);
4569 ret
= __unregister_ftrace_function(ops
);
4571 ftrace_shutdown(ops
, 0);
4572 mutex_unlock(&ftrace_lock
);
4576 EXPORT_SYMBOL_GPL(unregister_ftrace_function
);
4579 ftrace_enable_sysctl(struct ctl_table
*table
, int write
,
4580 void __user
*buffer
, size_t *lenp
,
4585 mutex_lock(&ftrace_lock
);
4587 if (unlikely(ftrace_disabled
))
4590 ret
= proc_dointvec(table
, write
, buffer
, lenp
, ppos
);
4592 if (ret
|| !write
|| (last_ftrace_enabled
== !!ftrace_enabled
))
4595 last_ftrace_enabled
= !!ftrace_enabled
;
4597 if (ftrace_enabled
) {
4599 ftrace_startup_sysctl();
4601 /* we are starting ftrace again */
4602 if (ftrace_ops_list
!= &ftrace_list_end
) {
4603 if (ftrace_ops_list
->next
== &ftrace_list_end
)
4604 ftrace_trace_function
= ftrace_ops_list
->func
;
4606 ftrace_trace_function
= ftrace_ops_list_func
;
4610 /* stopping ftrace calls (just send to ftrace_stub) */
4611 ftrace_trace_function
= ftrace_stub
;
4613 ftrace_shutdown_sysctl();
4617 mutex_unlock(&ftrace_lock
);
4621 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
4623 static int ftrace_graph_active
;
4624 static struct notifier_block ftrace_suspend_notifier
;
4626 int ftrace_graph_entry_stub(struct ftrace_graph_ent
*trace
)
4631 /* The callbacks that hook a function */
4632 trace_func_graph_ret_t ftrace_graph_return
=
4633 (trace_func_graph_ret_t
)ftrace_stub
;
4634 trace_func_graph_ent_t ftrace_graph_entry
= ftrace_graph_entry_stub
;
4636 /* Try to assign a return stack array on FTRACE_RETSTACK_ALLOC_SIZE tasks. */
4637 static int alloc_retstack_tasklist(struct ftrace_ret_stack
**ret_stack_list
)
4641 unsigned long flags
;
4642 int start
= 0, end
= FTRACE_RETSTACK_ALLOC_SIZE
;
4643 struct task_struct
*g
, *t
;
4645 for (i
= 0; i
< FTRACE_RETSTACK_ALLOC_SIZE
; i
++) {
4646 ret_stack_list
[i
] = kmalloc(FTRACE_RETFUNC_DEPTH
4647 * sizeof(struct ftrace_ret_stack
),
4649 if (!ret_stack_list
[i
]) {
4657 read_lock_irqsave(&tasklist_lock
, flags
);
4658 do_each_thread(g
, t
) {
4664 if (t
->ret_stack
== NULL
) {
4665 atomic_set(&t
->tracing_graph_pause
, 0);
4666 atomic_set(&t
->trace_overrun
, 0);
4667 t
->curr_ret_stack
= -1;
4668 /* Make sure the tasks see the -1 first: */
4670 t
->ret_stack
= ret_stack_list
[start
++];
4672 } while_each_thread(g
, t
);
4675 read_unlock_irqrestore(&tasklist_lock
, flags
);
4677 for (i
= start
; i
< end
; i
++)
4678 kfree(ret_stack_list
[i
]);
4683 ftrace_graph_probe_sched_switch(void *ignore
,
4684 struct task_struct
*prev
, struct task_struct
*next
)
4686 unsigned long long timestamp
;
4690 * Does the user want to count the time a function was asleep.
4691 * If so, do not update the time stamps.
4693 if (trace_flags
& TRACE_ITER_SLEEP_TIME
)
4696 timestamp
= trace_clock_local();
4698 prev
->ftrace_timestamp
= timestamp
;
4700 /* only process tasks that we timestamped */
4701 if (!next
->ftrace_timestamp
)
4705 * Update all the counters in next to make up for the
4706 * time next was sleeping.
4708 timestamp
-= next
->ftrace_timestamp
;
4710 for (index
= next
->curr_ret_stack
; index
>= 0; index
--)
4711 next
->ret_stack
[index
].calltime
+= timestamp
;
4714 /* Allocate a return stack for each task */
4715 static int start_graph_tracing(void)
4717 struct ftrace_ret_stack
**ret_stack_list
;
4720 ret_stack_list
= kmalloc(FTRACE_RETSTACK_ALLOC_SIZE
*
4721 sizeof(struct ftrace_ret_stack
*),
4724 if (!ret_stack_list
)
4727 /* The cpu_boot init_task->ret_stack will never be freed */
4728 for_each_online_cpu(cpu
) {
4729 if (!idle_task(cpu
)->ret_stack
)
4730 ftrace_graph_init_idle_task(idle_task(cpu
), cpu
);
4734 ret
= alloc_retstack_tasklist(ret_stack_list
);
4735 } while (ret
== -EAGAIN
);
4738 ret
= register_trace_sched_switch(ftrace_graph_probe_sched_switch
, NULL
);
4740 pr_info("ftrace_graph: Couldn't activate tracepoint"
4741 " probe to kernel_sched_switch\n");
4744 kfree(ret_stack_list
);
4749 * Hibernation protection.
4750 * The state of the current task is too much unstable during
4751 * suspend/restore to disk. We want to protect against that.
4754 ftrace_suspend_notifier_call(struct notifier_block
*bl
, unsigned long state
,
4758 case PM_HIBERNATION_PREPARE
:
4759 pause_graph_tracing();
4762 case PM_POST_HIBERNATION
:
4763 unpause_graph_tracing();
4769 int register_ftrace_graph(trace_func_graph_ret_t retfunc
,
4770 trace_func_graph_ent_t entryfunc
)
4774 mutex_lock(&ftrace_lock
);
4776 /* we currently allow only one tracer registered at a time */
4777 if (ftrace_graph_active
) {
4782 ftrace_suspend_notifier
.notifier_call
= ftrace_suspend_notifier_call
;
4783 register_pm_notifier(&ftrace_suspend_notifier
);
4785 ftrace_graph_active
++;
4786 ret
= start_graph_tracing();
4788 ftrace_graph_active
--;
4792 ftrace_graph_return
= retfunc
;
4793 ftrace_graph_entry
= entryfunc
;
4795 ret
= ftrace_startup(&global_ops
, FTRACE_START_FUNC_RET
);
4798 mutex_unlock(&ftrace_lock
);
4802 void unregister_ftrace_graph(void)
4804 mutex_lock(&ftrace_lock
);
4806 if (unlikely(!ftrace_graph_active
))
4809 ftrace_graph_active
--;
4810 ftrace_graph_return
= (trace_func_graph_ret_t
)ftrace_stub
;
4811 ftrace_graph_entry
= ftrace_graph_entry_stub
;
4812 ftrace_shutdown(&global_ops
, FTRACE_STOP_FUNC_RET
);
4813 unregister_pm_notifier(&ftrace_suspend_notifier
);
4814 unregister_trace_sched_switch(ftrace_graph_probe_sched_switch
, NULL
);
4817 mutex_unlock(&ftrace_lock
);
4820 static DEFINE_PER_CPU(struct ftrace_ret_stack
*, idle_ret_stack
);
4823 graph_init_task(struct task_struct
*t
, struct ftrace_ret_stack
*ret_stack
)
4825 atomic_set(&t
->tracing_graph_pause
, 0);
4826 atomic_set(&t
->trace_overrun
, 0);
4827 t
->ftrace_timestamp
= 0;
4828 /* make curr_ret_stack visible before we add the ret_stack */
4830 t
->ret_stack
= ret_stack
;
4834 * Allocate a return stack for the idle task. May be the first
4835 * time through, or it may be done by CPU hotplug online.
4837 void ftrace_graph_init_idle_task(struct task_struct
*t
, int cpu
)
4839 t
->curr_ret_stack
= -1;
4841 * The idle task has no parent, it either has its own
4842 * stack or no stack at all.
4845 WARN_ON(t
->ret_stack
!= per_cpu(idle_ret_stack
, cpu
));
4847 if (ftrace_graph_active
) {
4848 struct ftrace_ret_stack
*ret_stack
;
4850 ret_stack
= per_cpu(idle_ret_stack
, cpu
);
4852 ret_stack
= kmalloc(FTRACE_RETFUNC_DEPTH
4853 * sizeof(struct ftrace_ret_stack
),
4857 per_cpu(idle_ret_stack
, cpu
) = ret_stack
;
4859 graph_init_task(t
, ret_stack
);
4863 /* Allocate a return stack for newly created task */
4864 void ftrace_graph_init_task(struct task_struct
*t
)
4866 /* Make sure we do not use the parent ret_stack */
4867 t
->ret_stack
= NULL
;
4868 t
->curr_ret_stack
= -1;
4870 if (ftrace_graph_active
) {
4871 struct ftrace_ret_stack
*ret_stack
;
4873 ret_stack
= kmalloc(FTRACE_RETFUNC_DEPTH
4874 * sizeof(struct ftrace_ret_stack
),
4878 graph_init_task(t
, ret_stack
);
4882 void ftrace_graph_exit_task(struct task_struct
*t
)
4884 struct ftrace_ret_stack
*ret_stack
= t
->ret_stack
;
4886 t
->ret_stack
= NULL
;
4887 /* NULL must become visible to IRQs before we free it: */
4893 void ftrace_graph_stop(void)