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_CONTROL)
67 #ifdef CONFIG_DYNAMIC_FTRACE
68 #define INIT_REGEX_LOCK(opsname) \
69 .regex_lock = __MUTEX_INITIALIZER(opsname.regex_lock),
71 #define INIT_REGEX_LOCK(opsname)
74 static struct ftrace_ops ftrace_list_end __read_mostly
= {
76 .flags
= FTRACE_OPS_FL_RECURSION_SAFE
| FTRACE_OPS_FL_STUB
,
79 /* ftrace_enabled is a method to turn ftrace on or off */
80 int ftrace_enabled __read_mostly
;
81 static int last_ftrace_enabled
;
83 /* Quick disabling of function tracer. */
84 int function_trace_stop __read_mostly
;
86 /* Current function tracing op */
87 struct ftrace_ops
*function_trace_op __read_mostly
= &ftrace_list_end
;
88 /* What to set function_trace_op to */
89 static struct ftrace_ops
*set_function_trace_op
;
91 /* List for set_ftrace_pid's pids. */
92 LIST_HEAD(ftrace_pids
);
94 struct list_head list
;
99 * ftrace_disabled is set when an anomaly is discovered.
100 * ftrace_disabled is much stronger than ftrace_enabled.
102 static int ftrace_disabled __read_mostly
;
104 static DEFINE_MUTEX(ftrace_lock
);
106 static struct ftrace_ops
*ftrace_control_list __read_mostly
= &ftrace_list_end
;
107 static struct ftrace_ops
*ftrace_ops_list __read_mostly
= &ftrace_list_end
;
108 ftrace_func_t ftrace_trace_function __read_mostly
= ftrace_stub
;
109 ftrace_func_t ftrace_pid_function __read_mostly
= ftrace_stub
;
110 static struct ftrace_ops global_ops
;
111 static struct ftrace_ops control_ops
;
113 #if ARCH_SUPPORTS_FTRACE_OPS
114 static void ftrace_ops_list_func(unsigned long ip
, unsigned long parent_ip
,
115 struct ftrace_ops
*op
, struct pt_regs
*regs
);
117 /* See comment below, where ftrace_ops_list_func is defined */
118 static void ftrace_ops_no_ops(unsigned long ip
, unsigned long parent_ip
);
119 #define ftrace_ops_list_func ((ftrace_func_t)ftrace_ops_no_ops)
123 * Traverse the ftrace_global_list, invoking all entries. The reason that we
124 * can use rcu_dereference_raw_notrace() is that elements removed from this list
125 * are simply leaked, so there is no need to interact with a grace-period
126 * mechanism. The rcu_dereference_raw_notrace() calls are needed to handle
127 * concurrent insertions into the ftrace_global_list.
129 * Silly Alpha and silly pointer-speculation compiler optimizations!
131 #define do_for_each_ftrace_op(op, list) \
132 op = rcu_dereference_raw_notrace(list); \
136 * Optimized for just a single item in the list (as that is the normal case).
138 #define while_for_each_ftrace_op(op) \
139 while (likely(op = rcu_dereference_raw_notrace((op)->next)) && \
140 unlikely((op) != &ftrace_list_end))
142 static inline void ftrace_ops_init(struct ftrace_ops
*ops
)
144 #ifdef CONFIG_DYNAMIC_FTRACE
145 if (!(ops
->flags
& FTRACE_OPS_FL_INITIALIZED
)) {
146 mutex_init(&ops
->regex_lock
);
147 ops
->flags
|= FTRACE_OPS_FL_INITIALIZED
;
153 * ftrace_nr_registered_ops - return number of ops registered
155 * Returns the number of ftrace_ops registered and tracing functions
157 int ftrace_nr_registered_ops(void)
159 struct ftrace_ops
*ops
;
162 mutex_lock(&ftrace_lock
);
164 for (ops
= ftrace_ops_list
;
165 ops
!= &ftrace_list_end
; ops
= ops
->next
)
168 mutex_unlock(&ftrace_lock
);
173 static void ftrace_pid_func(unsigned long ip
, unsigned long parent_ip
,
174 struct ftrace_ops
*op
, struct pt_regs
*regs
)
176 if (!test_tsk_trace_trace(current
))
179 ftrace_pid_function(ip
, parent_ip
, op
, regs
);
182 static void set_ftrace_pid_function(ftrace_func_t func
)
184 /* do not set ftrace_pid_function to itself! */
185 if (func
!= ftrace_pid_func
)
186 ftrace_pid_function
= func
;
190 * clear_ftrace_function - reset the ftrace function
192 * This NULLs the ftrace function and in essence stops
193 * tracing. There may be lag
195 void clear_ftrace_function(void)
197 ftrace_trace_function
= ftrace_stub
;
198 ftrace_pid_function
= ftrace_stub
;
201 static void control_ops_disable_all(struct ftrace_ops
*ops
)
205 for_each_possible_cpu(cpu
)
206 *per_cpu_ptr(ops
->disabled
, cpu
) = 1;
209 static int control_ops_alloc(struct ftrace_ops
*ops
)
211 int __percpu
*disabled
;
213 disabled
= alloc_percpu(int);
217 ops
->disabled
= disabled
;
218 control_ops_disable_all(ops
);
222 static void ftrace_sync(struct work_struct
*work
)
225 * This function is just a stub to implement a hard force
226 * of synchronize_sched(). This requires synchronizing
227 * tasks even in userspace and idle.
229 * Yes, function tracing is rude.
233 static void ftrace_sync_ipi(void *data
)
235 /* Probably not needed, but do it anyway */
239 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
240 static void update_function_graph_func(void);
242 static inline void update_function_graph_func(void) { }
245 static void update_ftrace_function(void)
250 * If we are at the end of the list and this ops is
251 * recursion safe and not dynamic and the arch supports passing ops,
252 * then have the mcount trampoline call the function directly.
254 if (ftrace_ops_list
== &ftrace_list_end
||
255 (ftrace_ops_list
->next
== &ftrace_list_end
&&
256 !(ftrace_ops_list
->flags
& FTRACE_OPS_FL_DYNAMIC
) &&
257 (ftrace_ops_list
->flags
& FTRACE_OPS_FL_RECURSION_SAFE
) &&
258 !FTRACE_FORCE_LIST_FUNC
)) {
259 /* Set the ftrace_ops that the arch callback uses */
260 set_function_trace_op
= ftrace_ops_list
;
261 func
= ftrace_ops_list
->func
;
263 /* Just use the default ftrace_ops */
264 set_function_trace_op
= &ftrace_list_end
;
265 func
= ftrace_ops_list_func
;
268 /* If there's no change, then do nothing more here */
269 if (ftrace_trace_function
== func
)
272 update_function_graph_func();
275 * If we are using the list function, it doesn't care
276 * about the function_trace_ops.
278 if (func
== ftrace_ops_list_func
) {
279 ftrace_trace_function
= func
;
281 * Don't even bother setting function_trace_ops,
282 * it would be racy to do so anyway.
287 #ifndef CONFIG_DYNAMIC_FTRACE
289 * For static tracing, we need to be a bit more careful.
290 * The function change takes affect immediately. Thus,
291 * we need to coorditate the setting of the function_trace_ops
292 * with the setting of the ftrace_trace_function.
294 * Set the function to the list ops, which will call the
295 * function we want, albeit indirectly, but it handles the
296 * ftrace_ops and doesn't depend on function_trace_op.
298 ftrace_trace_function
= ftrace_ops_list_func
;
300 * Make sure all CPUs see this. Yes this is slow, but static
301 * tracing is slow and nasty to have enabled.
303 schedule_on_each_cpu(ftrace_sync
);
304 /* Now all cpus are using the list ops. */
305 function_trace_op
= set_function_trace_op
;
306 /* Make sure the function_trace_op is visible on all CPUs */
308 /* Nasty way to force a rmb on all cpus */
309 smp_call_function(ftrace_sync_ipi
, NULL
, 1);
310 /* OK, we are all set to update the ftrace_trace_function now! */
311 #endif /* !CONFIG_DYNAMIC_FTRACE */
313 ftrace_trace_function
= func
;
316 static void add_ftrace_ops(struct ftrace_ops
**list
, struct ftrace_ops
*ops
)
320 * We are entering ops into the list but another
321 * CPU might be walking that list. We need to make sure
322 * the ops->next pointer is valid before another CPU sees
323 * the ops pointer included into the list.
325 rcu_assign_pointer(*list
, ops
);
328 static int remove_ftrace_ops(struct ftrace_ops
**list
, struct ftrace_ops
*ops
)
330 struct ftrace_ops
**p
;
333 * If we are removing the last function, then simply point
334 * to the ftrace_stub.
336 if (*list
== ops
&& ops
->next
== &ftrace_list_end
) {
337 *list
= &ftrace_list_end
;
341 for (p
= list
; *p
!= &ftrace_list_end
; p
= &(*p
)->next
)
352 static void add_ftrace_list_ops(struct ftrace_ops
**list
,
353 struct ftrace_ops
*main_ops
,
354 struct ftrace_ops
*ops
)
356 int first
= *list
== &ftrace_list_end
;
357 add_ftrace_ops(list
, ops
);
359 add_ftrace_ops(&ftrace_ops_list
, main_ops
);
362 static int remove_ftrace_list_ops(struct ftrace_ops
**list
,
363 struct ftrace_ops
*main_ops
,
364 struct ftrace_ops
*ops
)
366 int ret
= remove_ftrace_ops(list
, ops
);
367 if (!ret
&& *list
== &ftrace_list_end
)
368 ret
= remove_ftrace_ops(&ftrace_ops_list
, main_ops
);
372 static int __register_ftrace_function(struct ftrace_ops
*ops
)
374 if (ops
->flags
& FTRACE_OPS_FL_DELETED
)
377 if (WARN_ON(ops
->flags
& FTRACE_OPS_FL_ENABLED
))
380 #ifndef CONFIG_DYNAMIC_FTRACE_WITH_REGS
382 * If the ftrace_ops specifies SAVE_REGS, then it only can be used
383 * if the arch supports it, or SAVE_REGS_IF_SUPPORTED is also set.
384 * Setting SAVE_REGS_IF_SUPPORTED makes SAVE_REGS irrelevant.
386 if (ops
->flags
& FTRACE_OPS_FL_SAVE_REGS
&&
387 !(ops
->flags
& FTRACE_OPS_FL_SAVE_REGS_IF_SUPPORTED
))
390 if (ops
->flags
& FTRACE_OPS_FL_SAVE_REGS_IF_SUPPORTED
)
391 ops
->flags
|= FTRACE_OPS_FL_SAVE_REGS
;
394 if (!core_kernel_data((unsigned long)ops
))
395 ops
->flags
|= FTRACE_OPS_FL_DYNAMIC
;
397 if (ops
->flags
& FTRACE_OPS_FL_CONTROL
) {
398 if (control_ops_alloc(ops
))
400 add_ftrace_list_ops(&ftrace_control_list
, &control_ops
, ops
);
402 add_ftrace_ops(&ftrace_ops_list
, ops
);
405 update_ftrace_function();
410 static int __unregister_ftrace_function(struct ftrace_ops
*ops
)
414 if (WARN_ON(!(ops
->flags
& FTRACE_OPS_FL_ENABLED
)))
417 if (ops
->flags
& FTRACE_OPS_FL_CONTROL
) {
418 ret
= remove_ftrace_list_ops(&ftrace_control_list
,
421 ret
= remove_ftrace_ops(&ftrace_ops_list
, ops
);
427 update_ftrace_function();
432 static void ftrace_update_pid_func(void)
434 /* Only do something if we are tracing something */
435 if (ftrace_trace_function
== ftrace_stub
)
438 update_ftrace_function();
441 #ifdef CONFIG_FUNCTION_PROFILER
442 struct ftrace_profile
{
443 struct hlist_node node
;
445 unsigned long counter
;
446 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
447 unsigned long long time
;
448 unsigned long long time_squared
;
452 struct ftrace_profile_page
{
453 struct ftrace_profile_page
*next
;
455 struct ftrace_profile records
[];
458 struct ftrace_profile_stat
{
460 struct hlist_head
*hash
;
461 struct ftrace_profile_page
*pages
;
462 struct ftrace_profile_page
*start
;
463 struct tracer_stat stat
;
466 #define PROFILE_RECORDS_SIZE \
467 (PAGE_SIZE - offsetof(struct ftrace_profile_page, records))
469 #define PROFILES_PER_PAGE \
470 (PROFILE_RECORDS_SIZE / sizeof(struct ftrace_profile))
472 static int ftrace_profile_enabled __read_mostly
;
474 /* ftrace_profile_lock - synchronize the enable and disable of the profiler */
475 static DEFINE_MUTEX(ftrace_profile_lock
);
477 static DEFINE_PER_CPU(struct ftrace_profile_stat
, ftrace_profile_stats
);
479 #define FTRACE_PROFILE_HASH_BITS 10
480 #define FTRACE_PROFILE_HASH_SIZE (1 << FTRACE_PROFILE_HASH_BITS)
483 function_stat_next(void *v
, int idx
)
485 struct ftrace_profile
*rec
= v
;
486 struct ftrace_profile_page
*pg
;
488 pg
= (struct ftrace_profile_page
*)((unsigned long)rec
& PAGE_MASK
);
494 if ((void *)rec
>= (void *)&pg
->records
[pg
->index
]) {
498 rec
= &pg
->records
[0];
506 static void *function_stat_start(struct tracer_stat
*trace
)
508 struct ftrace_profile_stat
*stat
=
509 container_of(trace
, struct ftrace_profile_stat
, stat
);
511 if (!stat
|| !stat
->start
)
514 return function_stat_next(&stat
->start
->records
[0], 0);
517 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
518 /* function graph compares on total time */
519 static int function_stat_cmp(void *p1
, void *p2
)
521 struct ftrace_profile
*a
= p1
;
522 struct ftrace_profile
*b
= p2
;
524 if (a
->time
< b
->time
)
526 if (a
->time
> b
->time
)
532 /* not function graph compares against hits */
533 static int function_stat_cmp(void *p1
, void *p2
)
535 struct ftrace_profile
*a
= p1
;
536 struct ftrace_profile
*b
= p2
;
538 if (a
->counter
< b
->counter
)
540 if (a
->counter
> b
->counter
)
547 static int function_stat_headers(struct seq_file
*m
)
549 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
550 seq_printf(m
, " Function "
553 "--- ---- --- ---\n");
555 seq_printf(m
, " Function Hit\n"
561 static int function_stat_show(struct seq_file
*m
, void *v
)
563 struct ftrace_profile
*rec
= v
;
564 char str
[KSYM_SYMBOL_LEN
];
566 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
567 static struct trace_seq s
;
568 unsigned long long avg
;
569 unsigned long long stddev
;
571 mutex_lock(&ftrace_profile_lock
);
573 /* we raced with function_profile_reset() */
574 if (unlikely(rec
->counter
== 0)) {
579 kallsyms_lookup(rec
->ip
, NULL
, NULL
, NULL
, str
);
580 seq_printf(m
, " %-30.30s %10lu", str
, rec
->counter
);
582 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
585 do_div(avg
, rec
->counter
);
587 /* Sample standard deviation (s^2) */
588 if (rec
->counter
<= 1)
592 * Apply Welford's method:
593 * s^2 = 1 / (n * (n-1)) * (n * \Sum (x_i)^2 - (\Sum x_i)^2)
595 stddev
= rec
->counter
* rec
->time_squared
-
596 rec
->time
* rec
->time
;
599 * Divide only 1000 for ns^2 -> us^2 conversion.
600 * trace_print_graph_duration will divide 1000 again.
602 do_div(stddev
, rec
->counter
* (rec
->counter
- 1) * 1000);
606 trace_print_graph_duration(rec
->time
, &s
);
607 trace_seq_puts(&s
, " ");
608 trace_print_graph_duration(avg
, &s
);
609 trace_seq_puts(&s
, " ");
610 trace_print_graph_duration(stddev
, &s
);
611 trace_print_seq(m
, &s
);
615 mutex_unlock(&ftrace_profile_lock
);
620 static void ftrace_profile_reset(struct ftrace_profile_stat
*stat
)
622 struct ftrace_profile_page
*pg
;
624 pg
= stat
->pages
= stat
->start
;
627 memset(pg
->records
, 0, PROFILE_RECORDS_SIZE
);
632 memset(stat
->hash
, 0,
633 FTRACE_PROFILE_HASH_SIZE
* sizeof(struct hlist_head
));
636 int ftrace_profile_pages_init(struct ftrace_profile_stat
*stat
)
638 struct ftrace_profile_page
*pg
;
643 /* If we already allocated, do nothing */
647 stat
->pages
= (void *)get_zeroed_page(GFP_KERNEL
);
651 #ifdef CONFIG_DYNAMIC_FTRACE
652 functions
= ftrace_update_tot_cnt
;
655 * We do not know the number of functions that exist because
656 * dynamic tracing is what counts them. With past experience
657 * we have around 20K functions. That should be more than enough.
658 * It is highly unlikely we will execute every function in
664 pg
= stat
->start
= stat
->pages
;
666 pages
= DIV_ROUND_UP(functions
, PROFILES_PER_PAGE
);
668 for (i
= 1; i
< pages
; i
++) {
669 pg
->next
= (void *)get_zeroed_page(GFP_KERNEL
);
680 unsigned long tmp
= (unsigned long)pg
;
692 static int ftrace_profile_init_cpu(int cpu
)
694 struct ftrace_profile_stat
*stat
;
697 stat
= &per_cpu(ftrace_profile_stats
, cpu
);
700 /* If the profile is already created, simply reset it */
701 ftrace_profile_reset(stat
);
706 * We are profiling all functions, but usually only a few thousand
707 * functions are hit. We'll make a hash of 1024 items.
709 size
= FTRACE_PROFILE_HASH_SIZE
;
711 stat
->hash
= kzalloc(sizeof(struct hlist_head
) * size
, GFP_KERNEL
);
716 /* Preallocate the function profiling pages */
717 if (ftrace_profile_pages_init(stat
) < 0) {
726 static int ftrace_profile_init(void)
731 for_each_possible_cpu(cpu
) {
732 ret
= ftrace_profile_init_cpu(cpu
);
740 /* interrupts must be disabled */
741 static struct ftrace_profile
*
742 ftrace_find_profiled_func(struct ftrace_profile_stat
*stat
, unsigned long ip
)
744 struct ftrace_profile
*rec
;
745 struct hlist_head
*hhd
;
748 key
= hash_long(ip
, FTRACE_PROFILE_HASH_BITS
);
749 hhd
= &stat
->hash
[key
];
751 if (hlist_empty(hhd
))
754 hlist_for_each_entry_rcu_notrace(rec
, hhd
, node
) {
762 static void ftrace_add_profile(struct ftrace_profile_stat
*stat
,
763 struct ftrace_profile
*rec
)
767 key
= hash_long(rec
->ip
, FTRACE_PROFILE_HASH_BITS
);
768 hlist_add_head_rcu(&rec
->node
, &stat
->hash
[key
]);
772 * The memory is already allocated, this simply finds a new record to use.
774 static struct ftrace_profile
*
775 ftrace_profile_alloc(struct ftrace_profile_stat
*stat
, unsigned long ip
)
777 struct ftrace_profile
*rec
= NULL
;
779 /* prevent recursion (from NMIs) */
780 if (atomic_inc_return(&stat
->disabled
) != 1)
784 * Try to find the function again since an NMI
785 * could have added it
787 rec
= ftrace_find_profiled_func(stat
, ip
);
791 if (stat
->pages
->index
== PROFILES_PER_PAGE
) {
792 if (!stat
->pages
->next
)
794 stat
->pages
= stat
->pages
->next
;
797 rec
= &stat
->pages
->records
[stat
->pages
->index
++];
799 ftrace_add_profile(stat
, rec
);
802 atomic_dec(&stat
->disabled
);
808 function_profile_call(unsigned long ip
, unsigned long parent_ip
,
809 struct ftrace_ops
*ops
, struct pt_regs
*regs
)
811 struct ftrace_profile_stat
*stat
;
812 struct ftrace_profile
*rec
;
815 if (!ftrace_profile_enabled
)
818 local_irq_save(flags
);
820 stat
= &__get_cpu_var(ftrace_profile_stats
);
821 if (!stat
->hash
|| !ftrace_profile_enabled
)
824 rec
= ftrace_find_profiled_func(stat
, ip
);
826 rec
= ftrace_profile_alloc(stat
, ip
);
833 local_irq_restore(flags
);
836 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
837 static int profile_graph_entry(struct ftrace_graph_ent
*trace
)
839 function_profile_call(trace
->func
, 0, NULL
, NULL
);
843 static void profile_graph_return(struct ftrace_graph_ret
*trace
)
845 struct ftrace_profile_stat
*stat
;
846 unsigned long long calltime
;
847 struct ftrace_profile
*rec
;
850 local_irq_save(flags
);
851 stat
= &__get_cpu_var(ftrace_profile_stats
);
852 if (!stat
->hash
|| !ftrace_profile_enabled
)
855 /* If the calltime was zero'd ignore it */
856 if (!trace
->calltime
)
859 calltime
= trace
->rettime
- trace
->calltime
;
861 if (!(trace_flags
& TRACE_ITER_GRAPH_TIME
)) {
864 index
= trace
->depth
;
866 /* Append this call time to the parent time to subtract */
868 current
->ret_stack
[index
- 1].subtime
+= calltime
;
870 if (current
->ret_stack
[index
].subtime
< calltime
)
871 calltime
-= current
->ret_stack
[index
].subtime
;
876 rec
= ftrace_find_profiled_func(stat
, trace
->func
);
878 rec
->time
+= calltime
;
879 rec
->time_squared
+= calltime
* calltime
;
883 local_irq_restore(flags
);
886 static int register_ftrace_profiler(void)
888 return register_ftrace_graph(&profile_graph_return
,
889 &profile_graph_entry
);
892 static void unregister_ftrace_profiler(void)
894 unregister_ftrace_graph();
897 static struct ftrace_ops ftrace_profile_ops __read_mostly
= {
898 .func
= function_profile_call
,
899 .flags
= FTRACE_OPS_FL_RECURSION_SAFE
| FTRACE_OPS_FL_INITIALIZED
,
900 INIT_REGEX_LOCK(ftrace_profile_ops
)
903 static int register_ftrace_profiler(void)
905 return register_ftrace_function(&ftrace_profile_ops
);
908 static void unregister_ftrace_profiler(void)
910 unregister_ftrace_function(&ftrace_profile_ops
);
912 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
915 ftrace_profile_write(struct file
*filp
, const char __user
*ubuf
,
916 size_t cnt
, loff_t
*ppos
)
921 ret
= kstrtoul_from_user(ubuf
, cnt
, 10, &val
);
927 mutex_lock(&ftrace_profile_lock
);
928 if (ftrace_profile_enabled
^ val
) {
930 ret
= ftrace_profile_init();
936 ret
= register_ftrace_profiler();
941 ftrace_profile_enabled
= 1;
943 ftrace_profile_enabled
= 0;
945 * unregister_ftrace_profiler calls stop_machine
946 * so this acts like an synchronize_sched.
948 unregister_ftrace_profiler();
952 mutex_unlock(&ftrace_profile_lock
);
960 ftrace_profile_read(struct file
*filp
, char __user
*ubuf
,
961 size_t cnt
, loff_t
*ppos
)
963 char buf
[64]; /* big enough to hold a number */
966 r
= sprintf(buf
, "%u\n", ftrace_profile_enabled
);
967 return simple_read_from_buffer(ubuf
, cnt
, ppos
, buf
, r
);
970 static const struct file_operations ftrace_profile_fops
= {
971 .open
= tracing_open_generic
,
972 .read
= ftrace_profile_read
,
973 .write
= ftrace_profile_write
,
974 .llseek
= default_llseek
,
977 /* used to initialize the real stat files */
978 static struct tracer_stat function_stats __initdata
= {
980 .stat_start
= function_stat_start
,
981 .stat_next
= function_stat_next
,
982 .stat_cmp
= function_stat_cmp
,
983 .stat_headers
= function_stat_headers
,
984 .stat_show
= function_stat_show
987 static __init
void ftrace_profile_debugfs(struct dentry
*d_tracer
)
989 struct ftrace_profile_stat
*stat
;
990 struct dentry
*entry
;
995 for_each_possible_cpu(cpu
) {
996 stat
= &per_cpu(ftrace_profile_stats
, cpu
);
998 /* allocate enough for function name + cpu number */
999 name
= kmalloc(32, GFP_KERNEL
);
1002 * The files created are permanent, if something happens
1003 * we still do not free memory.
1006 "Could not allocate stat file for cpu %d\n",
1010 stat
->stat
= function_stats
;
1011 snprintf(name
, 32, "function%d", cpu
);
1012 stat
->stat
.name
= name
;
1013 ret
= register_stat_tracer(&stat
->stat
);
1016 "Could not register function stat for cpu %d\n",
1023 entry
= debugfs_create_file("function_profile_enabled", 0644,
1024 d_tracer
, NULL
, &ftrace_profile_fops
);
1026 pr_warning("Could not create debugfs "
1027 "'function_profile_enabled' entry\n");
1030 #else /* CONFIG_FUNCTION_PROFILER */
1031 static __init
void ftrace_profile_debugfs(struct dentry
*d_tracer
)
1034 #endif /* CONFIG_FUNCTION_PROFILER */
1036 static struct pid
* const ftrace_swapper_pid
= &init_struct_pid
;
1038 #ifdef CONFIG_DYNAMIC_FTRACE
1040 #ifndef CONFIG_FTRACE_MCOUNT_RECORD
1041 # error Dynamic ftrace depends on MCOUNT_RECORD
1044 static struct hlist_head ftrace_func_hash
[FTRACE_FUNC_HASHSIZE
] __read_mostly
;
1046 struct ftrace_func_probe
{
1047 struct hlist_node node
;
1048 struct ftrace_probe_ops
*ops
;
1049 unsigned long flags
;
1052 struct list_head free_list
;
1055 struct ftrace_func_entry
{
1056 struct hlist_node hlist
;
1060 struct ftrace_hash
{
1061 unsigned long size_bits
;
1062 struct hlist_head
*buckets
;
1063 unsigned long count
;
1064 struct rcu_head rcu
;
1068 * We make these constant because no one should touch them,
1069 * but they are used as the default "empty hash", to avoid allocating
1070 * it all the time. These are in a read only section such that if
1071 * anyone does try to modify it, it will cause an exception.
1073 static const struct hlist_head empty_buckets
[1];
1074 static const struct ftrace_hash empty_hash
= {
1075 .buckets
= (struct hlist_head
*)empty_buckets
,
1077 #define EMPTY_HASH ((struct ftrace_hash *)&empty_hash)
1079 static struct ftrace_ops global_ops
= {
1080 .func
= ftrace_stub
,
1081 .notrace_hash
= EMPTY_HASH
,
1082 .filter_hash
= EMPTY_HASH
,
1083 .flags
= FTRACE_OPS_FL_RECURSION_SAFE
| FTRACE_OPS_FL_INITIALIZED
,
1084 INIT_REGEX_LOCK(global_ops
)
1087 struct ftrace_page
{
1088 struct ftrace_page
*next
;
1089 struct dyn_ftrace
*records
;
1094 #define ENTRY_SIZE sizeof(struct dyn_ftrace)
1095 #define ENTRIES_PER_PAGE (PAGE_SIZE / ENTRY_SIZE)
1097 /* estimate from running different kernels */
1098 #define NR_TO_INIT 10000
1100 static struct ftrace_page
*ftrace_pages_start
;
1101 static struct ftrace_page
*ftrace_pages
;
1103 static bool ftrace_hash_empty(struct ftrace_hash
*hash
)
1105 return !hash
|| !hash
->count
;
1108 static struct ftrace_func_entry
*
1109 ftrace_lookup_ip(struct ftrace_hash
*hash
, unsigned long ip
)
1112 struct ftrace_func_entry
*entry
;
1113 struct hlist_head
*hhd
;
1115 if (ftrace_hash_empty(hash
))
1118 if (hash
->size_bits
> 0)
1119 key
= hash_long(ip
, hash
->size_bits
);
1123 hhd
= &hash
->buckets
[key
];
1125 hlist_for_each_entry_rcu_notrace(entry
, hhd
, hlist
) {
1126 if (entry
->ip
== ip
)
1132 static void __add_hash_entry(struct ftrace_hash
*hash
,
1133 struct ftrace_func_entry
*entry
)
1135 struct hlist_head
*hhd
;
1138 if (hash
->size_bits
)
1139 key
= hash_long(entry
->ip
, hash
->size_bits
);
1143 hhd
= &hash
->buckets
[key
];
1144 hlist_add_head(&entry
->hlist
, hhd
);
1148 static int add_hash_entry(struct ftrace_hash
*hash
, unsigned long ip
)
1150 struct ftrace_func_entry
*entry
;
1152 entry
= kmalloc(sizeof(*entry
), GFP_KERNEL
);
1157 __add_hash_entry(hash
, entry
);
1163 free_hash_entry(struct ftrace_hash
*hash
,
1164 struct ftrace_func_entry
*entry
)
1166 hlist_del(&entry
->hlist
);
1172 remove_hash_entry(struct ftrace_hash
*hash
,
1173 struct ftrace_func_entry
*entry
)
1175 hlist_del(&entry
->hlist
);
1179 static void ftrace_hash_clear(struct ftrace_hash
*hash
)
1181 struct hlist_head
*hhd
;
1182 struct hlist_node
*tn
;
1183 struct ftrace_func_entry
*entry
;
1184 int size
= 1 << hash
->size_bits
;
1190 for (i
= 0; i
< size
; i
++) {
1191 hhd
= &hash
->buckets
[i
];
1192 hlist_for_each_entry_safe(entry
, tn
, hhd
, hlist
)
1193 free_hash_entry(hash
, entry
);
1195 FTRACE_WARN_ON(hash
->count
);
1198 static void free_ftrace_hash(struct ftrace_hash
*hash
)
1200 if (!hash
|| hash
== EMPTY_HASH
)
1202 ftrace_hash_clear(hash
);
1203 kfree(hash
->buckets
);
1207 static void __free_ftrace_hash_rcu(struct rcu_head
*rcu
)
1209 struct ftrace_hash
*hash
;
1211 hash
= container_of(rcu
, struct ftrace_hash
, rcu
);
1212 free_ftrace_hash(hash
);
1215 static void free_ftrace_hash_rcu(struct ftrace_hash
*hash
)
1217 if (!hash
|| hash
== EMPTY_HASH
)
1219 call_rcu_sched(&hash
->rcu
, __free_ftrace_hash_rcu
);
1222 void ftrace_free_filter(struct ftrace_ops
*ops
)
1224 ftrace_ops_init(ops
);
1225 free_ftrace_hash(ops
->filter_hash
);
1226 free_ftrace_hash(ops
->notrace_hash
);
1229 static struct ftrace_hash
*alloc_ftrace_hash(int size_bits
)
1231 struct ftrace_hash
*hash
;
1234 hash
= kzalloc(sizeof(*hash
), GFP_KERNEL
);
1238 size
= 1 << size_bits
;
1239 hash
->buckets
= kcalloc(size
, sizeof(*hash
->buckets
), GFP_KERNEL
);
1241 if (!hash
->buckets
) {
1246 hash
->size_bits
= size_bits
;
1251 static struct ftrace_hash
*
1252 alloc_and_copy_ftrace_hash(int size_bits
, struct ftrace_hash
*hash
)
1254 struct ftrace_func_entry
*entry
;
1255 struct ftrace_hash
*new_hash
;
1260 new_hash
= alloc_ftrace_hash(size_bits
);
1265 if (ftrace_hash_empty(hash
))
1268 size
= 1 << hash
->size_bits
;
1269 for (i
= 0; i
< size
; i
++) {
1270 hlist_for_each_entry(entry
, &hash
->buckets
[i
], hlist
) {
1271 ret
= add_hash_entry(new_hash
, entry
->ip
);
1277 FTRACE_WARN_ON(new_hash
->count
!= hash
->count
);
1282 free_ftrace_hash(new_hash
);
1287 ftrace_hash_rec_disable(struct ftrace_ops
*ops
, int filter_hash
);
1289 ftrace_hash_rec_enable(struct ftrace_ops
*ops
, int filter_hash
);
1292 ftrace_hash_move(struct ftrace_ops
*ops
, int enable
,
1293 struct ftrace_hash
**dst
, struct ftrace_hash
*src
)
1295 struct ftrace_func_entry
*entry
;
1296 struct hlist_node
*tn
;
1297 struct hlist_head
*hhd
;
1298 struct ftrace_hash
*old_hash
;
1299 struct ftrace_hash
*new_hash
;
1300 int size
= src
->count
;
1306 * Remove the current set, update the hash and add
1309 ftrace_hash_rec_disable(ops
, enable
);
1312 * If the new source is empty, just free dst and assign it
1316 free_ftrace_hash_rcu(*dst
);
1317 rcu_assign_pointer(*dst
, EMPTY_HASH
);
1318 /* still need to update the function records */
1324 * Make the hash size about 1/2 the # found
1326 for (size
/= 2; size
; size
>>= 1)
1329 /* Don't allocate too much */
1330 if (bits
> FTRACE_HASH_MAX_BITS
)
1331 bits
= FTRACE_HASH_MAX_BITS
;
1334 new_hash
= alloc_ftrace_hash(bits
);
1338 size
= 1 << src
->size_bits
;
1339 for (i
= 0; i
< size
; i
++) {
1340 hhd
= &src
->buckets
[i
];
1341 hlist_for_each_entry_safe(entry
, tn
, hhd
, hlist
) {
1342 remove_hash_entry(src
, entry
);
1343 __add_hash_entry(new_hash
, entry
);
1348 rcu_assign_pointer(*dst
, new_hash
);
1349 free_ftrace_hash_rcu(old_hash
);
1354 * Enable regardless of ret:
1355 * On success, we enable the new hash.
1356 * On failure, we re-enable the original hash.
1358 ftrace_hash_rec_enable(ops
, enable
);
1364 * Test the hashes for this ops to see if we want to call
1365 * the ops->func or not.
1367 * It's a match if the ip is in the ops->filter_hash or
1368 * the filter_hash does not exist or is empty,
1370 * the ip is not in the ops->notrace_hash.
1372 * This needs to be called with preemption disabled as
1373 * the hashes are freed with call_rcu_sched().
1376 ftrace_ops_test(struct ftrace_ops
*ops
, unsigned long ip
, void *regs
)
1378 struct ftrace_hash
*filter_hash
;
1379 struct ftrace_hash
*notrace_hash
;
1382 #ifdef CONFIG_DYNAMIC_FTRACE_WITH_REGS
1384 * There's a small race when adding ops that the ftrace handler
1385 * that wants regs, may be called without them. We can not
1386 * allow that handler to be called if regs is NULL.
1388 if (regs
== NULL
&& (ops
->flags
& FTRACE_OPS_FL_SAVE_REGS
))
1392 filter_hash
= rcu_dereference_raw_notrace(ops
->filter_hash
);
1393 notrace_hash
= rcu_dereference_raw_notrace(ops
->notrace_hash
);
1395 if ((ftrace_hash_empty(filter_hash
) ||
1396 ftrace_lookup_ip(filter_hash
, ip
)) &&
1397 (ftrace_hash_empty(notrace_hash
) ||
1398 !ftrace_lookup_ip(notrace_hash
, ip
)))
1407 * This is a double for. Do not use 'break' to break out of the loop,
1408 * you must use a goto.
1410 #define do_for_each_ftrace_rec(pg, rec) \
1411 for (pg = ftrace_pages_start; pg; pg = pg->next) { \
1413 for (_____i = 0; _____i < pg->index; _____i++) { \
1414 rec = &pg->records[_____i];
1416 #define while_for_each_ftrace_rec() \
1421 static int ftrace_cmp_recs(const void *a
, const void *b
)
1423 const struct dyn_ftrace
*key
= a
;
1424 const struct dyn_ftrace
*rec
= b
;
1426 if (key
->flags
< rec
->ip
)
1428 if (key
->ip
>= rec
->ip
+ MCOUNT_INSN_SIZE
)
1433 static unsigned long ftrace_location_range(unsigned long start
, unsigned long end
)
1435 struct ftrace_page
*pg
;
1436 struct dyn_ftrace
*rec
;
1437 struct dyn_ftrace key
;
1440 key
.flags
= end
; /* overload flags, as it is unsigned long */
1442 for (pg
= ftrace_pages_start
; pg
; pg
= pg
->next
) {
1443 if (end
< pg
->records
[0].ip
||
1444 start
>= (pg
->records
[pg
->index
- 1].ip
+ MCOUNT_INSN_SIZE
))
1446 rec
= bsearch(&key
, pg
->records
, pg
->index
,
1447 sizeof(struct dyn_ftrace
),
1457 * ftrace_location - return true if the ip giving is a traced location
1458 * @ip: the instruction pointer to check
1460 * Returns rec->ip if @ip given is a pointer to a ftrace location.
1461 * That is, the instruction that is either a NOP or call to
1462 * the function tracer. It checks the ftrace internal tables to
1463 * determine if the address belongs or not.
1465 unsigned long ftrace_location(unsigned long ip
)
1467 return ftrace_location_range(ip
, ip
);
1471 * ftrace_text_reserved - return true if range contains an ftrace location
1472 * @start: start of range to search
1473 * @end: end of range to search (inclusive). @end points to the last byte to check.
1475 * Returns 1 if @start and @end contains a ftrace location.
1476 * That is, the instruction that is either a NOP or call to
1477 * the function tracer. It checks the ftrace internal tables to
1478 * determine if the address belongs or not.
1480 int ftrace_text_reserved(const void *start
, const void *end
)
1484 ret
= ftrace_location_range((unsigned long)start
,
1485 (unsigned long)end
);
1490 static void __ftrace_hash_rec_update(struct ftrace_ops
*ops
,
1494 struct ftrace_hash
*hash
;
1495 struct ftrace_hash
*other_hash
;
1496 struct ftrace_page
*pg
;
1497 struct dyn_ftrace
*rec
;
1501 /* Only update if the ops has been registered */
1502 if (!(ops
->flags
& FTRACE_OPS_FL_ENABLED
))
1506 * In the filter_hash case:
1507 * If the count is zero, we update all records.
1508 * Otherwise we just update the items in the hash.
1510 * In the notrace_hash case:
1511 * We enable the update in the hash.
1512 * As disabling notrace means enabling the tracing,
1513 * and enabling notrace means disabling, the inc variable
1517 hash
= ops
->filter_hash
;
1518 other_hash
= ops
->notrace_hash
;
1519 if (ftrace_hash_empty(hash
))
1523 hash
= ops
->notrace_hash
;
1524 other_hash
= ops
->filter_hash
;
1526 * If the notrace hash has no items,
1527 * then there's nothing to do.
1529 if (ftrace_hash_empty(hash
))
1533 do_for_each_ftrace_rec(pg
, rec
) {
1534 int in_other_hash
= 0;
1540 * Only the filter_hash affects all records.
1541 * Update if the record is not in the notrace hash.
1543 if (!other_hash
|| !ftrace_lookup_ip(other_hash
, rec
->ip
))
1546 in_hash
= !!ftrace_lookup_ip(hash
, rec
->ip
);
1547 in_other_hash
= !!ftrace_lookup_ip(other_hash
, rec
->ip
);
1552 if (filter_hash
&& in_hash
&& !in_other_hash
)
1554 else if (!filter_hash
&& in_hash
&&
1555 (in_other_hash
|| ftrace_hash_empty(other_hash
)))
1563 if (FTRACE_WARN_ON((rec
->flags
& ~FTRACE_FL_MASK
) == FTRACE_REF_MAX
))
1566 * If any ops wants regs saved for this function
1567 * then all ops will get saved regs.
1569 if (ops
->flags
& FTRACE_OPS_FL_SAVE_REGS
)
1570 rec
->flags
|= FTRACE_FL_REGS
;
1572 if (FTRACE_WARN_ON((rec
->flags
& ~FTRACE_FL_MASK
) == 0))
1577 /* Shortcut, if we handled all records, we are done. */
1578 if (!all
&& count
== hash
->count
)
1580 } while_for_each_ftrace_rec();
1583 static void ftrace_hash_rec_disable(struct ftrace_ops
*ops
,
1586 __ftrace_hash_rec_update(ops
, filter_hash
, 0);
1589 static void ftrace_hash_rec_enable(struct ftrace_ops
*ops
,
1592 __ftrace_hash_rec_update(ops
, filter_hash
, 1);
1595 static void print_ip_ins(const char *fmt
, unsigned char *p
)
1599 printk(KERN_CONT
"%s", fmt
);
1601 for (i
= 0; i
< MCOUNT_INSN_SIZE
; i
++)
1602 printk(KERN_CONT
"%s%02x", i
? ":" : "", p
[i
]);
1606 * ftrace_bug - report and shutdown function tracer
1607 * @failed: The failed type (EFAULT, EINVAL, EPERM)
1608 * @ip: The address that failed
1610 * The arch code that enables or disables the function tracing
1611 * can call ftrace_bug() when it has detected a problem in
1612 * modifying the code. @failed should be one of either:
1613 * EFAULT - if the problem happens on reading the @ip address
1614 * EINVAL - if what is read at @ip is not what was expected
1615 * EPERM - if the problem happens on writting to the @ip address
1617 void ftrace_bug(int failed
, unsigned long ip
)
1621 FTRACE_WARN_ON_ONCE(1);
1622 pr_info("ftrace faulted on modifying ");
1626 FTRACE_WARN_ON_ONCE(1);
1627 pr_info("ftrace failed to modify ");
1629 print_ip_ins(" actual: ", (unsigned char *)ip
);
1630 printk(KERN_CONT
"\n");
1633 FTRACE_WARN_ON_ONCE(1);
1634 pr_info("ftrace faulted on writing ");
1638 FTRACE_WARN_ON_ONCE(1);
1639 pr_info("ftrace faulted on unknown error ");
1644 static int ftrace_check_record(struct dyn_ftrace
*rec
, int enable
, int update
)
1646 unsigned long flag
= 0UL;
1649 * If we are updating calls:
1651 * If the record has a ref count, then we need to enable it
1652 * because someone is using it.
1654 * Otherwise we make sure its disabled.
1656 * If we are disabling calls, then disable all records that
1659 if (enable
&& (rec
->flags
& ~FTRACE_FL_MASK
))
1660 flag
= FTRACE_FL_ENABLED
;
1663 * If enabling and the REGS flag does not match the REGS_EN, then
1664 * do not ignore this record. Set flags to fail the compare against
1668 (!(rec
->flags
& FTRACE_FL_REGS
) != !(rec
->flags
& FTRACE_FL_REGS_EN
)))
1669 flag
|= FTRACE_FL_REGS
;
1671 /* If the state of this record hasn't changed, then do nothing */
1672 if ((rec
->flags
& FTRACE_FL_ENABLED
) == flag
)
1673 return FTRACE_UPDATE_IGNORE
;
1676 /* Save off if rec is being enabled (for return value) */
1677 flag
^= rec
->flags
& FTRACE_FL_ENABLED
;
1680 rec
->flags
|= FTRACE_FL_ENABLED
;
1681 if (flag
& FTRACE_FL_REGS
) {
1682 if (rec
->flags
& FTRACE_FL_REGS
)
1683 rec
->flags
|= FTRACE_FL_REGS_EN
;
1685 rec
->flags
&= ~FTRACE_FL_REGS_EN
;
1690 * If this record is being updated from a nop, then
1691 * return UPDATE_MAKE_CALL.
1692 * Otherwise, if the EN flag is set, then return
1693 * UPDATE_MODIFY_CALL_REGS to tell the caller to convert
1694 * from the non-save regs, to a save regs function.
1696 * return UPDATE_MODIFY_CALL to tell the caller to convert
1697 * from the save regs, to a non-save regs function.
1699 if (flag
& FTRACE_FL_ENABLED
)
1700 return FTRACE_UPDATE_MAKE_CALL
;
1701 else if (rec
->flags
& FTRACE_FL_REGS_EN
)
1702 return FTRACE_UPDATE_MODIFY_CALL_REGS
;
1704 return FTRACE_UPDATE_MODIFY_CALL
;
1708 /* If there's no more users, clear all flags */
1709 if (!(rec
->flags
& ~FTRACE_FL_MASK
))
1712 /* Just disable the record (keep REGS state) */
1713 rec
->flags
&= ~FTRACE_FL_ENABLED
;
1716 return FTRACE_UPDATE_MAKE_NOP
;
1720 * ftrace_update_record, set a record that now is tracing or not
1721 * @rec: the record to update
1722 * @enable: set to 1 if the record is tracing, zero to force disable
1724 * The records that represent all functions that can be traced need
1725 * to be updated when tracing has been enabled.
1727 int ftrace_update_record(struct dyn_ftrace
*rec
, int enable
)
1729 return ftrace_check_record(rec
, enable
, 1);
1733 * ftrace_test_record, check if the record has been enabled or not
1734 * @rec: the record to test
1735 * @enable: set to 1 to check if enabled, 0 if it is disabled
1737 * The arch code may need to test if a record is already set to
1738 * tracing to determine how to modify the function code that it
1741 int ftrace_test_record(struct dyn_ftrace
*rec
, int enable
)
1743 return ftrace_check_record(rec
, enable
, 0);
1747 __ftrace_replace_code(struct dyn_ftrace
*rec
, int enable
)
1749 unsigned long ftrace_old_addr
;
1750 unsigned long ftrace_addr
;
1753 ret
= ftrace_update_record(rec
, enable
);
1755 if (rec
->flags
& FTRACE_FL_REGS
)
1756 ftrace_addr
= (unsigned long)FTRACE_REGS_ADDR
;
1758 ftrace_addr
= (unsigned long)FTRACE_ADDR
;
1761 case FTRACE_UPDATE_IGNORE
:
1764 case FTRACE_UPDATE_MAKE_CALL
:
1765 return ftrace_make_call(rec
, ftrace_addr
);
1767 case FTRACE_UPDATE_MAKE_NOP
:
1768 return ftrace_make_nop(NULL
, rec
, ftrace_addr
);
1770 case FTRACE_UPDATE_MODIFY_CALL_REGS
:
1771 case FTRACE_UPDATE_MODIFY_CALL
:
1772 if (rec
->flags
& FTRACE_FL_REGS
)
1773 ftrace_old_addr
= (unsigned long)FTRACE_ADDR
;
1775 ftrace_old_addr
= (unsigned long)FTRACE_REGS_ADDR
;
1777 return ftrace_modify_call(rec
, ftrace_old_addr
, ftrace_addr
);
1780 return -1; /* unknow ftrace bug */
1783 void __weak
ftrace_replace_code(int enable
)
1785 struct dyn_ftrace
*rec
;
1786 struct ftrace_page
*pg
;
1789 if (unlikely(ftrace_disabled
))
1792 do_for_each_ftrace_rec(pg
, rec
) {
1793 failed
= __ftrace_replace_code(rec
, enable
);
1795 ftrace_bug(failed
, rec
->ip
);
1796 /* Stop processing */
1799 } while_for_each_ftrace_rec();
1802 struct ftrace_rec_iter
{
1803 struct ftrace_page
*pg
;
1808 * ftrace_rec_iter_start, start up iterating over traced functions
1810 * Returns an iterator handle that is used to iterate over all
1811 * the records that represent address locations where functions
1814 * May return NULL if no records are available.
1816 struct ftrace_rec_iter
*ftrace_rec_iter_start(void)
1819 * We only use a single iterator.
1820 * Protected by the ftrace_lock mutex.
1822 static struct ftrace_rec_iter ftrace_rec_iter
;
1823 struct ftrace_rec_iter
*iter
= &ftrace_rec_iter
;
1825 iter
->pg
= ftrace_pages_start
;
1828 /* Could have empty pages */
1829 while (iter
->pg
&& !iter
->pg
->index
)
1830 iter
->pg
= iter
->pg
->next
;
1839 * ftrace_rec_iter_next, get the next record to process.
1840 * @iter: The handle to the iterator.
1842 * Returns the next iterator after the given iterator @iter.
1844 struct ftrace_rec_iter
*ftrace_rec_iter_next(struct ftrace_rec_iter
*iter
)
1848 if (iter
->index
>= iter
->pg
->index
) {
1849 iter
->pg
= iter
->pg
->next
;
1852 /* Could have empty pages */
1853 while (iter
->pg
&& !iter
->pg
->index
)
1854 iter
->pg
= iter
->pg
->next
;
1864 * ftrace_rec_iter_record, get the record at the iterator location
1865 * @iter: The current iterator location
1867 * Returns the record that the current @iter is at.
1869 struct dyn_ftrace
*ftrace_rec_iter_record(struct ftrace_rec_iter
*iter
)
1871 return &iter
->pg
->records
[iter
->index
];
1875 ftrace_code_disable(struct module
*mod
, struct dyn_ftrace
*rec
)
1882 if (unlikely(ftrace_disabled
))
1885 ret
= ftrace_make_nop(mod
, rec
, MCOUNT_ADDR
);
1887 ftrace_bug(ret
, ip
);
1894 * archs can override this function if they must do something
1895 * before the modifying code is performed.
1897 int __weak
ftrace_arch_code_modify_prepare(void)
1903 * archs can override this function if they must do something
1904 * after the modifying code is performed.
1906 int __weak
ftrace_arch_code_modify_post_process(void)
1911 void ftrace_modify_all_code(int command
)
1913 int update
= command
& FTRACE_UPDATE_TRACE_FUNC
;
1917 * If the ftrace_caller calls a ftrace_ops func directly,
1918 * we need to make sure that it only traces functions it
1919 * expects to trace. When doing the switch of functions,
1920 * we need to update to the ftrace_ops_list_func first
1921 * before the transition between old and new calls are set,
1922 * as the ftrace_ops_list_func will check the ops hashes
1923 * to make sure the ops are having the right functions
1927 err
= ftrace_update_ftrace_func(ftrace_ops_list_func
);
1928 if (FTRACE_WARN_ON(err
))
1932 if (command
& FTRACE_UPDATE_CALLS
)
1933 ftrace_replace_code(1);
1934 else if (command
& FTRACE_DISABLE_CALLS
)
1935 ftrace_replace_code(0);
1937 if (update
&& ftrace_trace_function
!= ftrace_ops_list_func
) {
1938 function_trace_op
= set_function_trace_op
;
1940 /* If irqs are disabled, we are in stop machine */
1941 if (!irqs_disabled())
1942 smp_call_function(ftrace_sync_ipi
, NULL
, 1);
1943 err
= ftrace_update_ftrace_func(ftrace_trace_function
);
1944 if (FTRACE_WARN_ON(err
))
1948 if (command
& FTRACE_START_FUNC_RET
)
1949 err
= ftrace_enable_ftrace_graph_caller();
1950 else if (command
& FTRACE_STOP_FUNC_RET
)
1951 err
= ftrace_disable_ftrace_graph_caller();
1952 FTRACE_WARN_ON(err
);
1955 static int __ftrace_modify_code(void *data
)
1957 int *command
= data
;
1959 ftrace_modify_all_code(*command
);
1965 * ftrace_run_stop_machine, go back to the stop machine method
1966 * @command: The command to tell ftrace what to do
1968 * If an arch needs to fall back to the stop machine method, the
1969 * it can call this function.
1971 void ftrace_run_stop_machine(int command
)
1973 stop_machine(__ftrace_modify_code
, &command
, NULL
);
1977 * arch_ftrace_update_code, modify the code to trace or not trace
1978 * @command: The command that needs to be done
1980 * Archs can override this function if it does not need to
1981 * run stop_machine() to modify code.
1983 void __weak
arch_ftrace_update_code(int command
)
1985 ftrace_run_stop_machine(command
);
1988 static void ftrace_run_update_code(int command
)
1992 ret
= ftrace_arch_code_modify_prepare();
1993 FTRACE_WARN_ON(ret
);
1997 * Do not call function tracer while we update the code.
1998 * We are in stop machine.
2000 function_trace_stop
++;
2003 * By default we use stop_machine() to modify the code.
2004 * But archs can do what ever they want as long as it
2005 * is safe. The stop_machine() is the safest, but also
2006 * produces the most overhead.
2008 arch_ftrace_update_code(command
);
2010 function_trace_stop
--;
2012 ret
= ftrace_arch_code_modify_post_process();
2013 FTRACE_WARN_ON(ret
);
2016 static ftrace_func_t saved_ftrace_func
;
2017 static int ftrace_start_up
;
2018 static int global_start_up
;
2020 static void control_ops_free(struct ftrace_ops
*ops
)
2022 free_percpu(ops
->disabled
);
2025 static void ftrace_startup_enable(int command
)
2027 if (saved_ftrace_func
!= ftrace_trace_function
) {
2028 saved_ftrace_func
= ftrace_trace_function
;
2029 command
|= FTRACE_UPDATE_TRACE_FUNC
;
2032 if (!command
|| !ftrace_enabled
)
2035 ftrace_run_update_code(command
);
2038 static int ftrace_startup(struct ftrace_ops
*ops
, int command
)
2040 bool hash_enable
= true;
2043 if (unlikely(ftrace_disabled
))
2046 ret
= __register_ftrace_function(ops
);
2051 command
|= FTRACE_UPDATE_CALLS
;
2053 ops
->flags
|= FTRACE_OPS_FL_ENABLED
;
2055 ftrace_hash_rec_enable(ops
, 1);
2057 ftrace_startup_enable(command
);
2062 static int ftrace_shutdown(struct ftrace_ops
*ops
, int command
)
2064 bool hash_disable
= true;
2067 if (unlikely(ftrace_disabled
))
2070 ret
= __unregister_ftrace_function(ops
);
2076 * Just warn in case of unbalance, no need to kill ftrace, it's not
2077 * critical but the ftrace_call callers may be never nopped again after
2078 * further ftrace uses.
2080 WARN_ON_ONCE(ftrace_start_up
< 0);
2083 ftrace_hash_rec_disable(ops
, 1);
2085 if (!global_start_up
)
2086 ops
->flags
&= ~FTRACE_OPS_FL_ENABLED
;
2088 command
|= FTRACE_UPDATE_CALLS
;
2090 if (saved_ftrace_func
!= ftrace_trace_function
) {
2091 saved_ftrace_func
= ftrace_trace_function
;
2092 command
|= FTRACE_UPDATE_TRACE_FUNC
;
2095 if (!command
|| !ftrace_enabled
) {
2097 * If these are control ops, they still need their
2098 * per_cpu field freed. Since, function tracing is
2099 * not currently active, we can just free them
2100 * without synchronizing all CPUs.
2102 if (ops
->flags
& FTRACE_OPS_FL_CONTROL
)
2103 control_ops_free(ops
);
2107 ftrace_run_update_code(command
);
2110 * Dynamic ops may be freed, we must make sure that all
2111 * callers are done before leaving this function.
2112 * The same goes for freeing the per_cpu data of the control
2115 * Again, normal synchronize_sched() is not good enough.
2116 * We need to do a hard force of sched synchronization.
2117 * This is because we use preempt_disable() to do RCU, but
2118 * the function tracers can be called where RCU is not watching
2119 * (like before user_exit()). We can not rely on the RCU
2120 * infrastructure to do the synchronization, thus we must do it
2123 if (ops
->flags
& (FTRACE_OPS_FL_DYNAMIC
| FTRACE_OPS_FL_CONTROL
)) {
2124 schedule_on_each_cpu(ftrace_sync
);
2126 if (ops
->flags
& FTRACE_OPS_FL_CONTROL
)
2127 control_ops_free(ops
);
2133 static void ftrace_startup_sysctl(void)
2135 if (unlikely(ftrace_disabled
))
2138 /* Force update next time */
2139 saved_ftrace_func
= NULL
;
2140 /* ftrace_start_up is true if we want ftrace running */
2141 if (ftrace_start_up
)
2142 ftrace_run_update_code(FTRACE_UPDATE_CALLS
);
2145 static void ftrace_shutdown_sysctl(void)
2147 if (unlikely(ftrace_disabled
))
2150 /* ftrace_start_up is true if ftrace is running */
2151 if (ftrace_start_up
)
2152 ftrace_run_update_code(FTRACE_DISABLE_CALLS
);
2155 static cycle_t ftrace_update_time
;
2156 unsigned long ftrace_update_tot_cnt
;
2158 static inline int ops_traces_mod(struct ftrace_ops
*ops
)
2161 * Filter_hash being empty will default to trace module.
2162 * But notrace hash requires a test of individual module functions.
2164 return ftrace_hash_empty(ops
->filter_hash
) &&
2165 ftrace_hash_empty(ops
->notrace_hash
);
2169 * Check if the current ops references the record.
2171 * If the ops traces all functions, then it was already accounted for.
2172 * If the ops does not trace the current record function, skip it.
2173 * If the ops ignores the function via notrace filter, skip it.
2176 ops_references_rec(struct ftrace_ops
*ops
, struct dyn_ftrace
*rec
)
2178 /* If ops isn't enabled, ignore it */
2179 if (!(ops
->flags
& FTRACE_OPS_FL_ENABLED
))
2182 /* If ops traces all mods, we already accounted for it */
2183 if (ops_traces_mod(ops
))
2186 /* The function must be in the filter */
2187 if (!ftrace_hash_empty(ops
->filter_hash
) &&
2188 !ftrace_lookup_ip(ops
->filter_hash
, rec
->ip
))
2191 /* If in notrace hash, we ignore it too */
2192 if (ftrace_lookup_ip(ops
->notrace_hash
, rec
->ip
))
2198 static int referenced_filters(struct dyn_ftrace
*rec
)
2200 struct ftrace_ops
*ops
;
2203 for (ops
= ftrace_ops_list
; ops
!= &ftrace_list_end
; ops
= ops
->next
) {
2204 if (ops_references_rec(ops
, rec
))
2211 static int ftrace_update_code(struct module
*mod
, struct ftrace_page
*new_pgs
)
2213 struct ftrace_page
*pg
;
2214 struct dyn_ftrace
*p
;
2215 cycle_t start
, stop
;
2216 unsigned long update_cnt
= 0;
2217 unsigned long ref
= 0;
2222 * When adding a module, we need to check if tracers are
2223 * currently enabled and if they are set to trace all functions.
2224 * If they are, we need to enable the module functions as well
2225 * as update the reference counts for those function records.
2228 struct ftrace_ops
*ops
;
2230 for (ops
= ftrace_ops_list
;
2231 ops
!= &ftrace_list_end
; ops
= ops
->next
) {
2232 if (ops
->flags
& FTRACE_OPS_FL_ENABLED
) {
2233 if (ops_traces_mod(ops
))
2241 start
= ftrace_now(raw_smp_processor_id());
2243 for (pg
= new_pgs
; pg
; pg
= pg
->next
) {
2245 for (i
= 0; i
< pg
->index
; i
++) {
2248 /* If something went wrong, bail without enabling anything */
2249 if (unlikely(ftrace_disabled
))
2252 p
= &pg
->records
[i
];
2254 cnt
+= referenced_filters(p
);
2258 * Do the initial record conversion from mcount jump
2259 * to the NOP instructions.
2261 if (!ftrace_code_disable(mod
, p
))
2267 * If the tracing is enabled, go ahead and enable the record.
2269 * The reason not to enable the record immediatelly is the
2270 * inherent check of ftrace_make_nop/ftrace_make_call for
2271 * correct previous instructions. Making first the NOP
2272 * conversion puts the module to the correct state, thus
2273 * passing the ftrace_make_call check.
2275 if (ftrace_start_up
&& cnt
) {
2276 int failed
= __ftrace_replace_code(p
, 1);
2278 ftrace_bug(failed
, p
->ip
);
2283 stop
= ftrace_now(raw_smp_processor_id());
2284 ftrace_update_time
= stop
- start
;
2285 ftrace_update_tot_cnt
+= update_cnt
;
2290 static int ftrace_allocate_records(struct ftrace_page
*pg
, int count
)
2295 if (WARN_ON(!count
))
2298 order
= get_count_order(DIV_ROUND_UP(count
, ENTRIES_PER_PAGE
));
2301 * We want to fill as much as possible. No more than a page
2304 while ((PAGE_SIZE
<< order
) / ENTRY_SIZE
>= count
+ ENTRIES_PER_PAGE
)
2308 pg
->records
= (void *)__get_free_pages(GFP_KERNEL
| __GFP_ZERO
, order
);
2311 /* if we can't allocate this size, try something smaller */
2318 cnt
= (PAGE_SIZE
<< order
) / ENTRY_SIZE
;
2327 static struct ftrace_page
*
2328 ftrace_allocate_pages(unsigned long num_to_init
)
2330 struct ftrace_page
*start_pg
;
2331 struct ftrace_page
*pg
;
2338 start_pg
= pg
= kzalloc(sizeof(*pg
), GFP_KERNEL
);
2343 * Try to allocate as much as possible in one continues
2344 * location that fills in all of the space. We want to
2345 * waste as little space as possible.
2348 cnt
= ftrace_allocate_records(pg
, num_to_init
);
2356 pg
->next
= kzalloc(sizeof(*pg
), GFP_KERNEL
);
2367 order
= get_count_order(pg
->size
/ ENTRIES_PER_PAGE
);
2368 free_pages((unsigned long)pg
->records
, order
);
2369 start_pg
= pg
->next
;
2373 pr_info("ftrace: FAILED to allocate memory for functions\n");
2377 #define FTRACE_BUFF_MAX (KSYM_SYMBOL_LEN+4) /* room for wildcards */
2379 struct ftrace_iterator
{
2382 struct ftrace_page
*pg
;
2383 struct dyn_ftrace
*func
;
2384 struct ftrace_func_probe
*probe
;
2385 struct trace_parser parser
;
2386 struct ftrace_hash
*hash
;
2387 struct ftrace_ops
*ops
;
2394 t_hash_next(struct seq_file
*m
, loff_t
*pos
)
2396 struct ftrace_iterator
*iter
= m
->private;
2397 struct hlist_node
*hnd
= NULL
;
2398 struct hlist_head
*hhd
;
2404 hnd
= &iter
->probe
->node
;
2406 if (iter
->hidx
>= FTRACE_FUNC_HASHSIZE
)
2409 hhd
= &ftrace_func_hash
[iter
->hidx
];
2411 if (hlist_empty(hhd
)) {
2427 if (WARN_ON_ONCE(!hnd
))
2430 iter
->probe
= hlist_entry(hnd
, struct ftrace_func_probe
, node
);
2435 static void *t_hash_start(struct seq_file
*m
, loff_t
*pos
)
2437 struct ftrace_iterator
*iter
= m
->private;
2441 if (!(iter
->flags
& FTRACE_ITER_DO_HASH
))
2444 if (iter
->func_pos
> *pos
)
2448 for (l
= 0; l
<= (*pos
- iter
->func_pos
); ) {
2449 p
= t_hash_next(m
, &l
);
2456 /* Only set this if we have an item */
2457 iter
->flags
|= FTRACE_ITER_HASH
;
2463 t_hash_show(struct seq_file
*m
, struct ftrace_iterator
*iter
)
2465 struct ftrace_func_probe
*rec
;
2468 if (WARN_ON_ONCE(!rec
))
2471 if (rec
->ops
->print
)
2472 return rec
->ops
->print(m
, rec
->ip
, rec
->ops
, rec
->data
);
2474 seq_printf(m
, "%ps:%ps", (void *)rec
->ip
, (void *)rec
->ops
->func
);
2477 seq_printf(m
, ":%p", rec
->data
);
2484 t_next(struct seq_file
*m
, void *v
, loff_t
*pos
)
2486 struct ftrace_iterator
*iter
= m
->private;
2487 struct ftrace_ops
*ops
= iter
->ops
;
2488 struct dyn_ftrace
*rec
= NULL
;
2490 if (unlikely(ftrace_disabled
))
2493 if (iter
->flags
& FTRACE_ITER_HASH
)
2494 return t_hash_next(m
, pos
);
2497 iter
->pos
= iter
->func_pos
= *pos
;
2499 if (iter
->flags
& FTRACE_ITER_PRINTALL
)
2500 return t_hash_start(m
, pos
);
2503 if (iter
->idx
>= iter
->pg
->index
) {
2504 if (iter
->pg
->next
) {
2505 iter
->pg
= iter
->pg
->next
;
2510 rec
= &iter
->pg
->records
[iter
->idx
++];
2511 if (((iter
->flags
& FTRACE_ITER_FILTER
) &&
2512 !(ftrace_lookup_ip(ops
->filter_hash
, rec
->ip
))) ||
2514 ((iter
->flags
& FTRACE_ITER_NOTRACE
) &&
2515 !ftrace_lookup_ip(ops
->notrace_hash
, rec
->ip
)) ||
2517 ((iter
->flags
& FTRACE_ITER_ENABLED
) &&
2518 !(rec
->flags
& FTRACE_FL_ENABLED
))) {
2526 return t_hash_start(m
, pos
);
2533 static void reset_iter_read(struct ftrace_iterator
*iter
)
2537 iter
->flags
&= ~(FTRACE_ITER_PRINTALL
| FTRACE_ITER_HASH
);
2540 static void *t_start(struct seq_file
*m
, loff_t
*pos
)
2542 struct ftrace_iterator
*iter
= m
->private;
2543 struct ftrace_ops
*ops
= iter
->ops
;
2547 mutex_lock(&ftrace_lock
);
2549 if (unlikely(ftrace_disabled
))
2553 * If an lseek was done, then reset and start from beginning.
2555 if (*pos
< iter
->pos
)
2556 reset_iter_read(iter
);
2559 * For set_ftrace_filter reading, if we have the filter
2560 * off, we can short cut and just print out that all
2561 * functions are enabled.
2563 if (iter
->flags
& FTRACE_ITER_FILTER
&&
2564 ftrace_hash_empty(ops
->filter_hash
)) {
2566 return t_hash_start(m
, pos
);
2567 iter
->flags
|= FTRACE_ITER_PRINTALL
;
2568 /* reset in case of seek/pread */
2569 iter
->flags
&= ~FTRACE_ITER_HASH
;
2573 if (iter
->flags
& FTRACE_ITER_HASH
)
2574 return t_hash_start(m
, pos
);
2577 * Unfortunately, we need to restart at ftrace_pages_start
2578 * every time we let go of the ftrace_mutex. This is because
2579 * those pointers can change without the lock.
2581 iter
->pg
= ftrace_pages_start
;
2583 for (l
= 0; l
<= *pos
; ) {
2584 p
= t_next(m
, p
, &l
);
2590 return t_hash_start(m
, pos
);
2595 static void t_stop(struct seq_file
*m
, void *p
)
2597 mutex_unlock(&ftrace_lock
);
2600 static int t_show(struct seq_file
*m
, void *v
)
2602 struct ftrace_iterator
*iter
= m
->private;
2603 struct dyn_ftrace
*rec
;
2605 if (iter
->flags
& FTRACE_ITER_HASH
)
2606 return t_hash_show(m
, iter
);
2608 if (iter
->flags
& FTRACE_ITER_PRINTALL
) {
2609 seq_printf(m
, "#### all functions enabled ####\n");
2618 seq_printf(m
, "%ps", (void *)rec
->ip
);
2619 if (iter
->flags
& FTRACE_ITER_ENABLED
)
2620 seq_printf(m
, " (%ld)%s",
2621 rec
->flags
& ~FTRACE_FL_MASK
,
2622 rec
->flags
& FTRACE_FL_REGS
? " R" : "");
2623 seq_printf(m
, "\n");
2628 static const struct seq_operations show_ftrace_seq_ops
= {
2636 ftrace_avail_open(struct inode
*inode
, struct file
*file
)
2638 struct ftrace_iterator
*iter
;
2640 if (unlikely(ftrace_disabled
))
2643 iter
= __seq_open_private(file
, &show_ftrace_seq_ops
, sizeof(*iter
));
2645 iter
->pg
= ftrace_pages_start
;
2646 iter
->ops
= &global_ops
;
2649 return iter
? 0 : -ENOMEM
;
2653 ftrace_enabled_open(struct inode
*inode
, struct file
*file
)
2655 struct ftrace_iterator
*iter
;
2657 if (unlikely(ftrace_disabled
))
2660 iter
= __seq_open_private(file
, &show_ftrace_seq_ops
, sizeof(*iter
));
2662 iter
->pg
= ftrace_pages_start
;
2663 iter
->flags
= FTRACE_ITER_ENABLED
;
2664 iter
->ops
= &global_ops
;
2667 return iter
? 0 : -ENOMEM
;
2670 static void ftrace_filter_reset(struct ftrace_hash
*hash
)
2672 mutex_lock(&ftrace_lock
);
2673 ftrace_hash_clear(hash
);
2674 mutex_unlock(&ftrace_lock
);
2678 * ftrace_regex_open - initialize function tracer filter files
2679 * @ops: The ftrace_ops that hold the hash filters
2680 * @flag: The type of filter to process
2681 * @inode: The inode, usually passed in to your open routine
2682 * @file: The file, usually passed in to your open routine
2684 * ftrace_regex_open() initializes the filter files for the
2685 * @ops. Depending on @flag it may process the filter hash or
2686 * the notrace hash of @ops. With this called from the open
2687 * routine, you can use ftrace_filter_write() for the write
2688 * routine if @flag has FTRACE_ITER_FILTER set, or
2689 * ftrace_notrace_write() if @flag has FTRACE_ITER_NOTRACE set.
2690 * tracing_lseek() should be used as the lseek routine, and
2691 * release must call ftrace_regex_release().
2694 ftrace_regex_open(struct ftrace_ops
*ops
, int flag
,
2695 struct inode
*inode
, struct file
*file
)
2697 struct ftrace_iterator
*iter
;
2698 struct ftrace_hash
*hash
;
2701 ftrace_ops_init(ops
);
2703 if (unlikely(ftrace_disabled
))
2706 iter
= kzalloc(sizeof(*iter
), GFP_KERNEL
);
2710 if (trace_parser_get_init(&iter
->parser
, FTRACE_BUFF_MAX
)) {
2718 mutex_lock(&ops
->regex_lock
);
2720 if (flag
& FTRACE_ITER_NOTRACE
)
2721 hash
= ops
->notrace_hash
;
2723 hash
= ops
->filter_hash
;
2725 if (file
->f_mode
& FMODE_WRITE
) {
2726 iter
->hash
= alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS
, hash
);
2728 trace_parser_put(&iter
->parser
);
2735 if ((file
->f_mode
& FMODE_WRITE
) &&
2736 (file
->f_flags
& O_TRUNC
))
2737 ftrace_filter_reset(iter
->hash
);
2739 if (file
->f_mode
& FMODE_READ
) {
2740 iter
->pg
= ftrace_pages_start
;
2742 ret
= seq_open(file
, &show_ftrace_seq_ops
);
2744 struct seq_file
*m
= file
->private_data
;
2748 free_ftrace_hash(iter
->hash
);
2749 trace_parser_put(&iter
->parser
);
2753 file
->private_data
= iter
;
2756 mutex_unlock(&ops
->regex_lock
);
2762 ftrace_filter_open(struct inode
*inode
, struct file
*file
)
2764 struct ftrace_ops
*ops
= inode
->i_private
;
2766 return ftrace_regex_open(ops
,
2767 FTRACE_ITER_FILTER
| FTRACE_ITER_DO_HASH
,
2772 ftrace_notrace_open(struct inode
*inode
, struct file
*file
)
2774 struct ftrace_ops
*ops
= inode
->i_private
;
2776 return ftrace_regex_open(ops
, FTRACE_ITER_NOTRACE
,
2780 static int ftrace_match(char *str
, char *regex
, int len
, int type
)
2787 if (strcmp(str
, regex
) == 0)
2790 case MATCH_FRONT_ONLY
:
2791 if (strncmp(str
, regex
, len
) == 0)
2794 case MATCH_MIDDLE_ONLY
:
2795 if (strstr(str
, regex
))
2798 case MATCH_END_ONLY
:
2800 if (slen
>= len
&& memcmp(str
+ slen
- len
, regex
, len
) == 0)
2809 enter_record(struct ftrace_hash
*hash
, struct dyn_ftrace
*rec
, int not)
2811 struct ftrace_func_entry
*entry
;
2814 entry
= ftrace_lookup_ip(hash
, rec
->ip
);
2816 /* Do nothing if it doesn't exist */
2820 free_hash_entry(hash
, entry
);
2822 /* Do nothing if it exists */
2826 ret
= add_hash_entry(hash
, rec
->ip
);
2832 ftrace_match_record(struct dyn_ftrace
*rec
, char *mod
,
2833 char *regex
, int len
, int type
)
2835 char str
[KSYM_SYMBOL_LEN
];
2838 kallsyms_lookup(rec
->ip
, NULL
, NULL
, &modname
, str
);
2841 /* module lookup requires matching the module */
2842 if (!modname
|| strcmp(modname
, mod
))
2845 /* blank search means to match all funcs in the mod */
2850 return ftrace_match(str
, regex
, len
, type
);
2854 match_records(struct ftrace_hash
*hash
, char *buff
,
2855 int len
, char *mod
, int not)
2857 unsigned search_len
= 0;
2858 struct ftrace_page
*pg
;
2859 struct dyn_ftrace
*rec
;
2860 int type
= MATCH_FULL
;
2861 char *search
= buff
;
2866 type
= filter_parse_regex(buff
, len
, &search
, ¬);
2867 search_len
= strlen(search
);
2870 mutex_lock(&ftrace_lock
);
2872 if (unlikely(ftrace_disabled
))
2875 do_for_each_ftrace_rec(pg
, rec
) {
2876 if (ftrace_match_record(rec
, mod
, search
, search_len
, type
)) {
2877 ret
= enter_record(hash
, rec
, not);
2884 } while_for_each_ftrace_rec();
2886 mutex_unlock(&ftrace_lock
);
2892 ftrace_match_records(struct ftrace_hash
*hash
, char *buff
, int len
)
2894 return match_records(hash
, buff
, len
, NULL
, 0);
2898 ftrace_match_module_records(struct ftrace_hash
*hash
, char *buff
, char *mod
)
2902 /* blank or '*' mean the same */
2903 if (strcmp(buff
, "*") == 0)
2906 /* handle the case of 'dont filter this module' */
2907 if (strcmp(buff
, "!") == 0 || strcmp(buff
, "!*") == 0) {
2912 return match_records(hash
, buff
, strlen(buff
), mod
, not);
2916 * We register the module command as a template to show others how
2917 * to register the a command as well.
2921 ftrace_mod_callback(struct ftrace_hash
*hash
,
2922 char *func
, char *cmd
, char *param
, int enable
)
2928 * cmd == 'mod' because we only registered this func
2929 * for the 'mod' ftrace_func_command.
2930 * But if you register one func with multiple commands,
2931 * you can tell which command was used by the cmd
2935 /* we must have a module name */
2939 mod
= strsep(¶m
, ":");
2943 ret
= ftrace_match_module_records(hash
, func
, mod
);
2952 static struct ftrace_func_command ftrace_mod_cmd
= {
2954 .func
= ftrace_mod_callback
,
2957 static int __init
ftrace_mod_cmd_init(void)
2959 return register_ftrace_command(&ftrace_mod_cmd
);
2961 core_initcall(ftrace_mod_cmd_init
);
2963 static void function_trace_probe_call(unsigned long ip
, unsigned long parent_ip
,
2964 struct ftrace_ops
*op
, struct pt_regs
*pt_regs
)
2966 struct ftrace_func_probe
*entry
;
2967 struct hlist_head
*hhd
;
2970 key
= hash_long(ip
, FTRACE_HASH_BITS
);
2972 hhd
= &ftrace_func_hash
[key
];
2974 if (hlist_empty(hhd
))
2978 * Disable preemption for these calls to prevent a RCU grace
2979 * period. This syncs the hash iteration and freeing of items
2980 * on the hash. rcu_read_lock is too dangerous here.
2982 preempt_disable_notrace();
2983 hlist_for_each_entry_rcu_notrace(entry
, hhd
, node
) {
2984 if (entry
->ip
== ip
)
2985 entry
->ops
->func(ip
, parent_ip
, &entry
->data
);
2987 preempt_enable_notrace();
2990 static struct ftrace_ops trace_probe_ops __read_mostly
=
2992 .func
= function_trace_probe_call
,
2993 .flags
= FTRACE_OPS_FL_INITIALIZED
,
2994 INIT_REGEX_LOCK(trace_probe_ops
)
2997 static int ftrace_probe_registered
;
2999 static void __enable_ftrace_function_probe(void)
3004 if (ftrace_probe_registered
) {
3005 /* still need to update the function call sites */
3007 ftrace_run_update_code(FTRACE_UPDATE_CALLS
);
3011 for (i
= 0; i
< FTRACE_FUNC_HASHSIZE
; i
++) {
3012 struct hlist_head
*hhd
= &ftrace_func_hash
[i
];
3016 /* Nothing registered? */
3017 if (i
== FTRACE_FUNC_HASHSIZE
)
3020 ret
= ftrace_startup(&trace_probe_ops
, 0);
3022 ftrace_probe_registered
= 1;
3025 static void __disable_ftrace_function_probe(void)
3029 if (!ftrace_probe_registered
)
3032 for (i
= 0; i
< FTRACE_FUNC_HASHSIZE
; i
++) {
3033 struct hlist_head
*hhd
= &ftrace_func_hash
[i
];
3038 /* no more funcs left */
3039 ftrace_shutdown(&trace_probe_ops
, 0);
3041 ftrace_probe_registered
= 0;
3045 static void ftrace_free_entry(struct ftrace_func_probe
*entry
)
3047 if (entry
->ops
->free
)
3048 entry
->ops
->free(entry
->ops
, entry
->ip
, &entry
->data
);
3053 register_ftrace_function_probe(char *glob
, struct ftrace_probe_ops
*ops
,
3056 struct ftrace_func_probe
*entry
;
3057 struct ftrace_hash
**orig_hash
= &trace_probe_ops
.filter_hash
;
3058 struct ftrace_hash
*hash
;
3059 struct ftrace_page
*pg
;
3060 struct dyn_ftrace
*rec
;
3067 type
= filter_parse_regex(glob
, strlen(glob
), &search
, ¬);
3068 len
= strlen(search
);
3070 /* we do not support '!' for function probes */
3074 mutex_lock(&trace_probe_ops
.regex_lock
);
3076 hash
= alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS
, *orig_hash
);
3082 if (unlikely(ftrace_disabled
)) {
3087 mutex_lock(&ftrace_lock
);
3089 do_for_each_ftrace_rec(pg
, rec
) {
3091 if (!ftrace_match_record(rec
, NULL
, search
, len
, type
))
3094 entry
= kmalloc(sizeof(*entry
), GFP_KERNEL
);
3096 /* If we did not process any, then return error */
3107 * The caller might want to do something special
3108 * for each function we find. We call the callback
3109 * to give the caller an opportunity to do so.
3112 if (ops
->init(ops
, rec
->ip
, &entry
->data
) < 0) {
3113 /* caller does not like this func */
3119 ret
= enter_record(hash
, rec
, 0);
3127 entry
->ip
= rec
->ip
;
3129 key
= hash_long(entry
->ip
, FTRACE_HASH_BITS
);
3130 hlist_add_head_rcu(&entry
->node
, &ftrace_func_hash
[key
]);
3132 } while_for_each_ftrace_rec();
3134 ret
= ftrace_hash_move(&trace_probe_ops
, 1, orig_hash
, hash
);
3138 __enable_ftrace_function_probe();
3141 mutex_unlock(&ftrace_lock
);
3143 mutex_unlock(&trace_probe_ops
.regex_lock
);
3144 free_ftrace_hash(hash
);
3150 PROBE_TEST_FUNC
= 1,
3155 __unregister_ftrace_function_probe(char *glob
, struct ftrace_probe_ops
*ops
,
3156 void *data
, int flags
)
3158 struct ftrace_func_entry
*rec_entry
;
3159 struct ftrace_func_probe
*entry
;
3160 struct ftrace_func_probe
*p
;
3161 struct ftrace_hash
**orig_hash
= &trace_probe_ops
.filter_hash
;
3162 struct list_head free_list
;
3163 struct ftrace_hash
*hash
;
3164 struct hlist_node
*tmp
;
3165 char str
[KSYM_SYMBOL_LEN
];
3166 int type
= MATCH_FULL
;
3170 if (glob
&& (strcmp(glob
, "*") == 0 || !strlen(glob
)))
3175 type
= filter_parse_regex(glob
, strlen(glob
), &search
, ¬);
3176 len
= strlen(search
);
3178 /* we do not support '!' for function probes */
3183 mutex_lock(&trace_probe_ops
.regex_lock
);
3185 hash
= alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS
, *orig_hash
);
3187 /* Hmm, should report this somehow */
3190 INIT_LIST_HEAD(&free_list
);
3192 for (i
= 0; i
< FTRACE_FUNC_HASHSIZE
; i
++) {
3193 struct hlist_head
*hhd
= &ftrace_func_hash
[i
];
3195 hlist_for_each_entry_safe(entry
, tmp
, hhd
, node
) {
3197 /* break up if statements for readability */
3198 if ((flags
& PROBE_TEST_FUNC
) && entry
->ops
!= ops
)
3201 if ((flags
& PROBE_TEST_DATA
) && entry
->data
!= data
)
3204 /* do this last, since it is the most expensive */
3206 kallsyms_lookup(entry
->ip
, NULL
, NULL
,
3208 if (!ftrace_match(str
, glob
, len
, type
))
3212 rec_entry
= ftrace_lookup_ip(hash
, entry
->ip
);
3213 /* It is possible more than one entry had this ip */
3215 free_hash_entry(hash
, rec_entry
);
3217 hlist_del_rcu(&entry
->node
);
3218 list_add(&entry
->free_list
, &free_list
);
3221 mutex_lock(&ftrace_lock
);
3222 __disable_ftrace_function_probe();
3224 * Remove after the disable is called. Otherwise, if the last
3225 * probe is removed, a null hash means *all enabled*.
3227 ftrace_hash_move(&trace_probe_ops
, 1, orig_hash
, hash
);
3228 synchronize_sched();
3229 list_for_each_entry_safe(entry
, p
, &free_list
, free_list
) {
3230 list_del(&entry
->free_list
);
3231 ftrace_free_entry(entry
);
3233 mutex_unlock(&ftrace_lock
);
3236 mutex_unlock(&trace_probe_ops
.regex_lock
);
3237 free_ftrace_hash(hash
);
3241 unregister_ftrace_function_probe(char *glob
, struct ftrace_probe_ops
*ops
,
3244 __unregister_ftrace_function_probe(glob
, ops
, data
,
3245 PROBE_TEST_FUNC
| PROBE_TEST_DATA
);
3249 unregister_ftrace_function_probe_func(char *glob
, struct ftrace_probe_ops
*ops
)
3251 __unregister_ftrace_function_probe(glob
, ops
, NULL
, PROBE_TEST_FUNC
);
3254 void unregister_ftrace_function_probe_all(char *glob
)
3256 __unregister_ftrace_function_probe(glob
, NULL
, NULL
, 0);
3259 static LIST_HEAD(ftrace_commands
);
3260 static DEFINE_MUTEX(ftrace_cmd_mutex
);
3263 * Currently we only register ftrace commands from __init, so mark this
3266 __init
int register_ftrace_command(struct ftrace_func_command
*cmd
)
3268 struct ftrace_func_command
*p
;
3271 mutex_lock(&ftrace_cmd_mutex
);
3272 list_for_each_entry(p
, &ftrace_commands
, list
) {
3273 if (strcmp(cmd
->name
, p
->name
) == 0) {
3278 list_add(&cmd
->list
, &ftrace_commands
);
3280 mutex_unlock(&ftrace_cmd_mutex
);
3286 * Currently we only unregister ftrace commands from __init, so mark
3289 __init
int unregister_ftrace_command(struct ftrace_func_command
*cmd
)
3291 struct ftrace_func_command
*p
, *n
;
3294 mutex_lock(&ftrace_cmd_mutex
);
3295 list_for_each_entry_safe(p
, n
, &ftrace_commands
, list
) {
3296 if (strcmp(cmd
->name
, p
->name
) == 0) {
3298 list_del_init(&p
->list
);
3303 mutex_unlock(&ftrace_cmd_mutex
);
3308 static int ftrace_process_regex(struct ftrace_hash
*hash
,
3309 char *buff
, int len
, int enable
)
3311 char *func
, *command
, *next
= buff
;
3312 struct ftrace_func_command
*p
;
3315 func
= strsep(&next
, ":");
3318 ret
= ftrace_match_records(hash
, func
, len
);
3328 command
= strsep(&next
, ":");
3330 mutex_lock(&ftrace_cmd_mutex
);
3331 list_for_each_entry(p
, &ftrace_commands
, list
) {
3332 if (strcmp(p
->name
, command
) == 0) {
3333 ret
= p
->func(hash
, func
, command
, next
, enable
);
3338 mutex_unlock(&ftrace_cmd_mutex
);
3344 ftrace_regex_write(struct file
*file
, const char __user
*ubuf
,
3345 size_t cnt
, loff_t
*ppos
, int enable
)
3347 struct ftrace_iterator
*iter
;
3348 struct trace_parser
*parser
;
3354 if (file
->f_mode
& FMODE_READ
) {
3355 struct seq_file
*m
= file
->private_data
;
3358 iter
= file
->private_data
;
3360 if (unlikely(ftrace_disabled
))
3363 /* iter->hash is a local copy, so we don't need regex_lock */
3365 parser
= &iter
->parser
;
3366 read
= trace_get_user(parser
, ubuf
, cnt
, ppos
);
3368 if (read
>= 0 && trace_parser_loaded(parser
) &&
3369 !trace_parser_cont(parser
)) {
3370 ret
= ftrace_process_regex(iter
->hash
, parser
->buffer
,
3371 parser
->idx
, enable
);
3372 trace_parser_clear(parser
);
3383 ftrace_filter_write(struct file
*file
, const char __user
*ubuf
,
3384 size_t cnt
, loff_t
*ppos
)
3386 return ftrace_regex_write(file
, ubuf
, cnt
, ppos
, 1);
3390 ftrace_notrace_write(struct file
*file
, const char __user
*ubuf
,
3391 size_t cnt
, loff_t
*ppos
)
3393 return ftrace_regex_write(file
, ubuf
, cnt
, ppos
, 0);
3397 ftrace_match_addr(struct ftrace_hash
*hash
, unsigned long ip
, int remove
)
3399 struct ftrace_func_entry
*entry
;
3401 if (!ftrace_location(ip
))
3405 entry
= ftrace_lookup_ip(hash
, ip
);
3408 free_hash_entry(hash
, entry
);
3412 return add_hash_entry(hash
, ip
);
3415 static void ftrace_ops_update_code(struct ftrace_ops
*ops
)
3417 if (ops
->flags
& FTRACE_OPS_FL_ENABLED
&& ftrace_enabled
)
3418 ftrace_run_update_code(FTRACE_UPDATE_CALLS
);
3422 ftrace_set_hash(struct ftrace_ops
*ops
, unsigned char *buf
, int len
,
3423 unsigned long ip
, int remove
, int reset
, int enable
)
3425 struct ftrace_hash
**orig_hash
;
3426 struct ftrace_hash
*hash
;
3429 if (unlikely(ftrace_disabled
))
3432 mutex_lock(&ops
->regex_lock
);
3435 orig_hash
= &ops
->filter_hash
;
3437 orig_hash
= &ops
->notrace_hash
;
3439 hash
= alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS
, *orig_hash
);
3442 goto out_regex_unlock
;
3446 ftrace_filter_reset(hash
);
3447 if (buf
&& !ftrace_match_records(hash
, buf
, len
)) {
3449 goto out_regex_unlock
;
3452 ret
= ftrace_match_addr(hash
, ip
, remove
);
3454 goto out_regex_unlock
;
3457 mutex_lock(&ftrace_lock
);
3458 ret
= ftrace_hash_move(ops
, enable
, orig_hash
, hash
);
3460 ftrace_ops_update_code(ops
);
3462 mutex_unlock(&ftrace_lock
);
3465 mutex_unlock(&ops
->regex_lock
);
3467 free_ftrace_hash(hash
);
3472 ftrace_set_addr(struct ftrace_ops
*ops
, unsigned long ip
, int remove
,
3473 int reset
, int enable
)
3475 return ftrace_set_hash(ops
, 0, 0, ip
, remove
, reset
, enable
);
3479 * ftrace_set_filter_ip - set a function to filter on in ftrace by address
3480 * @ops - the ops to set the filter with
3481 * @ip - the address to add to or remove from the filter.
3482 * @remove - non zero to remove the ip from the filter
3483 * @reset - non zero to reset all filters before applying this filter.
3485 * Filters denote which functions should be enabled when tracing is enabled
3486 * If @ip is NULL, it failes to update filter.
3488 int ftrace_set_filter_ip(struct ftrace_ops
*ops
, unsigned long ip
,
3489 int remove
, int reset
)
3491 ftrace_ops_init(ops
);
3492 return ftrace_set_addr(ops
, ip
, remove
, reset
, 1);
3494 EXPORT_SYMBOL_GPL(ftrace_set_filter_ip
);
3497 ftrace_set_regex(struct ftrace_ops
*ops
, unsigned char *buf
, int len
,
3498 int reset
, int enable
)
3500 return ftrace_set_hash(ops
, buf
, len
, 0, 0, reset
, enable
);
3504 * ftrace_set_filter - set a function to filter on in ftrace
3505 * @ops - the ops to set the filter with
3506 * @buf - the string that holds the function filter text.
3507 * @len - the length of the string.
3508 * @reset - non zero to reset all filters before applying this filter.
3510 * Filters denote which functions should be enabled when tracing is enabled.
3511 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
3513 int ftrace_set_filter(struct ftrace_ops
*ops
, unsigned char *buf
,
3516 ftrace_ops_init(ops
);
3517 return ftrace_set_regex(ops
, buf
, len
, reset
, 1);
3519 EXPORT_SYMBOL_GPL(ftrace_set_filter
);
3522 * ftrace_set_notrace - set a function to not trace in ftrace
3523 * @ops - the ops to set the notrace filter with
3524 * @buf - the string that holds the function notrace text.
3525 * @len - the length of the string.
3526 * @reset - non zero to reset all filters before applying this filter.
3528 * Notrace Filters denote which functions should not be enabled when tracing
3529 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
3532 int ftrace_set_notrace(struct ftrace_ops
*ops
, unsigned char *buf
,
3535 ftrace_ops_init(ops
);
3536 return ftrace_set_regex(ops
, buf
, len
, reset
, 0);
3538 EXPORT_SYMBOL_GPL(ftrace_set_notrace
);
3540 * ftrace_set_filter - set a function to filter on in ftrace
3541 * @ops - the ops to set the filter with
3542 * @buf - the string that holds the function filter text.
3543 * @len - the length of the string.
3544 * @reset - non zero to reset all filters before applying this filter.
3546 * Filters denote which functions should be enabled when tracing is enabled.
3547 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
3549 void ftrace_set_global_filter(unsigned char *buf
, int len
, int reset
)
3551 ftrace_set_regex(&global_ops
, buf
, len
, reset
, 1);
3553 EXPORT_SYMBOL_GPL(ftrace_set_global_filter
);
3556 * ftrace_set_notrace - set a function to not trace in ftrace
3557 * @ops - the ops to set the notrace filter with
3558 * @buf - the string that holds the function notrace text.
3559 * @len - the length of the string.
3560 * @reset - non zero to reset all filters before applying this filter.
3562 * Notrace Filters denote which functions should not be enabled when tracing
3563 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
3566 void ftrace_set_global_notrace(unsigned char *buf
, int len
, int reset
)
3568 ftrace_set_regex(&global_ops
, buf
, len
, reset
, 0);
3570 EXPORT_SYMBOL_GPL(ftrace_set_global_notrace
);
3573 * command line interface to allow users to set filters on boot up.
3575 #define FTRACE_FILTER_SIZE COMMAND_LINE_SIZE
3576 static char ftrace_notrace_buf
[FTRACE_FILTER_SIZE
] __initdata
;
3577 static char ftrace_filter_buf
[FTRACE_FILTER_SIZE
] __initdata
;
3579 /* Used by function selftest to not test if filter is set */
3580 bool ftrace_filter_param __initdata
;
3582 static int __init
set_ftrace_notrace(char *str
)
3584 ftrace_filter_param
= true;
3585 strlcpy(ftrace_notrace_buf
, str
, FTRACE_FILTER_SIZE
);
3588 __setup("ftrace_notrace=", set_ftrace_notrace
);
3590 static int __init
set_ftrace_filter(char *str
)
3592 ftrace_filter_param
= true;
3593 strlcpy(ftrace_filter_buf
, str
, FTRACE_FILTER_SIZE
);
3596 __setup("ftrace_filter=", set_ftrace_filter
);
3598 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
3599 static char ftrace_graph_buf
[FTRACE_FILTER_SIZE
] __initdata
;
3600 static int ftrace_set_func(unsigned long *array
, int *idx
, int size
, char *buffer
);
3602 static int __init
set_graph_function(char *str
)
3604 strlcpy(ftrace_graph_buf
, str
, FTRACE_FILTER_SIZE
);
3607 __setup("ftrace_graph_filter=", set_graph_function
);
3609 static void __init
set_ftrace_early_graph(char *buf
)
3615 func
= strsep(&buf
, ",");
3616 /* we allow only one expression at a time */
3617 ret
= ftrace_set_func(ftrace_graph_funcs
, &ftrace_graph_count
,
3618 FTRACE_GRAPH_MAX_FUNCS
, func
);
3620 printk(KERN_DEBUG
"ftrace: function %s not "
3621 "traceable\n", func
);
3624 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
3627 ftrace_set_early_filter(struct ftrace_ops
*ops
, char *buf
, int enable
)
3631 ftrace_ops_init(ops
);
3634 func
= strsep(&buf
, ",");
3635 ftrace_set_regex(ops
, func
, strlen(func
), 0, enable
);
3639 static void __init
set_ftrace_early_filters(void)
3641 if (ftrace_filter_buf
[0])
3642 ftrace_set_early_filter(&global_ops
, ftrace_filter_buf
, 1);
3643 if (ftrace_notrace_buf
[0])
3644 ftrace_set_early_filter(&global_ops
, ftrace_notrace_buf
, 0);
3645 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
3646 if (ftrace_graph_buf
[0])
3647 set_ftrace_early_graph(ftrace_graph_buf
);
3648 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
3651 int ftrace_regex_release(struct inode
*inode
, struct file
*file
)
3653 struct seq_file
*m
= (struct seq_file
*)file
->private_data
;
3654 struct ftrace_iterator
*iter
;
3655 struct ftrace_hash
**orig_hash
;
3656 struct trace_parser
*parser
;
3660 if (file
->f_mode
& FMODE_READ
) {
3662 seq_release(inode
, file
);
3664 iter
= file
->private_data
;
3666 parser
= &iter
->parser
;
3667 if (trace_parser_loaded(parser
)) {
3668 parser
->buffer
[parser
->idx
] = 0;
3669 ftrace_match_records(iter
->hash
, parser
->buffer
, parser
->idx
);
3672 trace_parser_put(parser
);
3674 mutex_lock(&iter
->ops
->regex_lock
);
3676 if (file
->f_mode
& FMODE_WRITE
) {
3677 filter_hash
= !!(iter
->flags
& FTRACE_ITER_FILTER
);
3680 orig_hash
= &iter
->ops
->filter_hash
;
3682 orig_hash
= &iter
->ops
->notrace_hash
;
3684 mutex_lock(&ftrace_lock
);
3685 ret
= ftrace_hash_move(iter
->ops
, filter_hash
,
3686 orig_hash
, iter
->hash
);
3688 ftrace_ops_update_code(iter
->ops
);
3690 mutex_unlock(&ftrace_lock
);
3693 mutex_unlock(&iter
->ops
->regex_lock
);
3694 free_ftrace_hash(iter
->hash
);
3700 static const struct file_operations ftrace_avail_fops
= {
3701 .open
= ftrace_avail_open
,
3703 .llseek
= seq_lseek
,
3704 .release
= seq_release_private
,
3707 static const struct file_operations ftrace_enabled_fops
= {
3708 .open
= ftrace_enabled_open
,
3710 .llseek
= seq_lseek
,
3711 .release
= seq_release_private
,
3714 static const struct file_operations ftrace_filter_fops
= {
3715 .open
= ftrace_filter_open
,
3717 .write
= ftrace_filter_write
,
3718 .llseek
= tracing_lseek
,
3719 .release
= ftrace_regex_release
,
3722 static const struct file_operations ftrace_notrace_fops
= {
3723 .open
= ftrace_notrace_open
,
3725 .write
= ftrace_notrace_write
,
3726 .llseek
= tracing_lseek
,
3727 .release
= ftrace_regex_release
,
3730 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
3732 static DEFINE_MUTEX(graph_lock
);
3734 int ftrace_graph_count
;
3735 int ftrace_graph_notrace_count
;
3736 unsigned long ftrace_graph_funcs
[FTRACE_GRAPH_MAX_FUNCS
] __read_mostly
;
3737 unsigned long ftrace_graph_notrace_funcs
[FTRACE_GRAPH_MAX_FUNCS
] __read_mostly
;
3739 struct ftrace_graph_data
{
3740 unsigned long *table
;
3743 const struct seq_operations
*seq_ops
;
3747 __g_next(struct seq_file
*m
, loff_t
*pos
)
3749 struct ftrace_graph_data
*fgd
= m
->private;
3751 if (*pos
>= *fgd
->count
)
3753 return &fgd
->table
[*pos
];
3757 g_next(struct seq_file
*m
, void *v
, loff_t
*pos
)
3760 return __g_next(m
, pos
);
3763 static void *g_start(struct seq_file
*m
, loff_t
*pos
)
3765 struct ftrace_graph_data
*fgd
= m
->private;
3767 mutex_lock(&graph_lock
);
3769 /* Nothing, tell g_show to print all functions are enabled */
3770 if (!*fgd
->count
&& !*pos
)
3773 return __g_next(m
, pos
);
3776 static void g_stop(struct seq_file
*m
, void *p
)
3778 mutex_unlock(&graph_lock
);
3781 static int g_show(struct seq_file
*m
, void *v
)
3783 unsigned long *ptr
= v
;
3788 if (ptr
== (unsigned long *)1) {
3789 seq_printf(m
, "#### all functions enabled ####\n");
3793 seq_printf(m
, "%ps\n", (void *)*ptr
);
3798 static const struct seq_operations ftrace_graph_seq_ops
= {
3806 __ftrace_graph_open(struct inode
*inode
, struct file
*file
,
3807 struct ftrace_graph_data
*fgd
)
3811 mutex_lock(&graph_lock
);
3812 if ((file
->f_mode
& FMODE_WRITE
) &&
3813 (file
->f_flags
& O_TRUNC
)) {
3815 memset(fgd
->table
, 0, fgd
->size
* sizeof(*fgd
->table
));
3817 mutex_unlock(&graph_lock
);
3819 if (file
->f_mode
& FMODE_READ
) {
3820 ret
= seq_open(file
, fgd
->seq_ops
);
3822 struct seq_file
*m
= file
->private_data
;
3826 file
->private_data
= fgd
;
3832 ftrace_graph_open(struct inode
*inode
, struct file
*file
)
3834 struct ftrace_graph_data
*fgd
;
3836 if (unlikely(ftrace_disabled
))
3839 fgd
= kmalloc(sizeof(*fgd
), GFP_KERNEL
);
3843 fgd
->table
= ftrace_graph_funcs
;
3844 fgd
->size
= FTRACE_GRAPH_MAX_FUNCS
;
3845 fgd
->count
= &ftrace_graph_count
;
3846 fgd
->seq_ops
= &ftrace_graph_seq_ops
;
3848 return __ftrace_graph_open(inode
, file
, fgd
);
3852 ftrace_graph_notrace_open(struct inode
*inode
, struct file
*file
)
3854 struct ftrace_graph_data
*fgd
;
3856 if (unlikely(ftrace_disabled
))
3859 fgd
= kmalloc(sizeof(*fgd
), GFP_KERNEL
);
3863 fgd
->table
= ftrace_graph_notrace_funcs
;
3864 fgd
->size
= FTRACE_GRAPH_MAX_FUNCS
;
3865 fgd
->count
= &ftrace_graph_notrace_count
;
3866 fgd
->seq_ops
= &ftrace_graph_seq_ops
;
3868 return __ftrace_graph_open(inode
, file
, fgd
);
3872 ftrace_graph_release(struct inode
*inode
, struct file
*file
)
3874 if (file
->f_mode
& FMODE_READ
) {
3875 struct seq_file
*m
= file
->private_data
;
3878 seq_release(inode
, file
);
3880 kfree(file
->private_data
);
3887 ftrace_set_func(unsigned long *array
, int *idx
, int size
, char *buffer
)
3889 struct dyn_ftrace
*rec
;
3890 struct ftrace_page
*pg
;
3899 type
= filter_parse_regex(buffer
, strlen(buffer
), &search
, ¬);
3900 if (!not && *idx
>= size
)
3903 search_len
= strlen(search
);
3905 mutex_lock(&ftrace_lock
);
3907 if (unlikely(ftrace_disabled
)) {
3908 mutex_unlock(&ftrace_lock
);
3912 do_for_each_ftrace_rec(pg
, rec
) {
3914 if (ftrace_match_record(rec
, NULL
, search
, search_len
, type
)) {
3915 /* if it is in the array */
3917 for (i
= 0; i
< *idx
; i
++) {
3918 if (array
[i
] == rec
->ip
) {
3927 array
[(*idx
)++] = rec
->ip
;
3933 array
[i
] = array
[--(*idx
)];
3939 } while_for_each_ftrace_rec();
3941 mutex_unlock(&ftrace_lock
);
3950 ftrace_graph_write(struct file
*file
, const char __user
*ubuf
,
3951 size_t cnt
, loff_t
*ppos
)
3953 struct trace_parser parser
;
3954 ssize_t read
, ret
= 0;
3955 struct ftrace_graph_data
*fgd
= file
->private_data
;
3960 if (trace_parser_get_init(&parser
, FTRACE_BUFF_MAX
))
3963 read
= trace_get_user(&parser
, ubuf
, cnt
, ppos
);
3965 if (read
>= 0 && trace_parser_loaded((&parser
))) {
3966 parser
.buffer
[parser
.idx
] = 0;
3968 mutex_lock(&graph_lock
);
3970 /* we allow only one expression at a time */
3971 ret
= ftrace_set_func(fgd
->table
, fgd
->count
, fgd
->size
,
3974 mutex_unlock(&graph_lock
);
3980 trace_parser_put(&parser
);
3985 static const struct file_operations ftrace_graph_fops
= {
3986 .open
= ftrace_graph_open
,
3988 .write
= ftrace_graph_write
,
3989 .llseek
= tracing_lseek
,
3990 .release
= ftrace_graph_release
,
3993 static const struct file_operations ftrace_graph_notrace_fops
= {
3994 .open
= ftrace_graph_notrace_open
,
3996 .write
= ftrace_graph_write
,
3997 .llseek
= tracing_lseek
,
3998 .release
= ftrace_graph_release
,
4000 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
4002 void ftrace_create_filter_files(struct ftrace_ops
*ops
,
4003 struct dentry
*parent
)
4006 trace_create_file("set_ftrace_filter", 0644, parent
,
4007 ops
, &ftrace_filter_fops
);
4009 trace_create_file("set_ftrace_notrace", 0644, parent
,
4010 ops
, &ftrace_notrace_fops
);
4014 * The name "destroy_filter_files" is really a misnomer. Although
4015 * in the future, it may actualy delete the files, but this is
4016 * really intended to make sure the ops passed in are disabled
4017 * and that when this function returns, the caller is free to
4020 * The "destroy" name is only to match the "create" name that this
4021 * should be paired with.
4023 void ftrace_destroy_filter_files(struct ftrace_ops
*ops
)
4025 mutex_lock(&ftrace_lock
);
4026 if (ops
->flags
& FTRACE_OPS_FL_ENABLED
)
4027 ftrace_shutdown(ops
, 0);
4028 ops
->flags
|= FTRACE_OPS_FL_DELETED
;
4029 mutex_unlock(&ftrace_lock
);
4032 static __init
int ftrace_init_dyn_debugfs(struct dentry
*d_tracer
)
4035 trace_create_file("available_filter_functions", 0444,
4036 d_tracer
, NULL
, &ftrace_avail_fops
);
4038 trace_create_file("enabled_functions", 0444,
4039 d_tracer
, NULL
, &ftrace_enabled_fops
);
4041 ftrace_create_filter_files(&global_ops
, d_tracer
);
4043 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
4044 trace_create_file("set_graph_function", 0444, d_tracer
,
4046 &ftrace_graph_fops
);
4047 trace_create_file("set_graph_notrace", 0444, d_tracer
,
4049 &ftrace_graph_notrace_fops
);
4050 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
4055 static int ftrace_cmp_ips(const void *a
, const void *b
)
4057 const unsigned long *ipa
= a
;
4058 const unsigned long *ipb
= b
;
4067 static void ftrace_swap_ips(void *a
, void *b
, int size
)
4069 unsigned long *ipa
= a
;
4070 unsigned long *ipb
= b
;
4078 static int ftrace_process_locs(struct module
*mod
,
4079 unsigned long *start
,
4082 struct ftrace_page
*start_pg
;
4083 struct ftrace_page
*pg
;
4084 struct dyn_ftrace
*rec
;
4085 unsigned long count
;
4088 unsigned long flags
= 0; /* Shut up gcc */
4091 count
= end
- start
;
4096 sort(start
, count
, sizeof(*start
),
4097 ftrace_cmp_ips
, ftrace_swap_ips
);
4099 start_pg
= ftrace_allocate_pages(count
);
4103 mutex_lock(&ftrace_lock
);
4106 * Core and each module needs their own pages, as
4107 * modules will free them when they are removed.
4108 * Force a new page to be allocated for modules.
4111 WARN_ON(ftrace_pages
|| ftrace_pages_start
);
4112 /* First initialization */
4113 ftrace_pages
= ftrace_pages_start
= start_pg
;
4118 if (WARN_ON(ftrace_pages
->next
)) {
4119 /* Hmm, we have free pages? */
4120 while (ftrace_pages
->next
)
4121 ftrace_pages
= ftrace_pages
->next
;
4124 ftrace_pages
->next
= start_pg
;
4130 addr
= ftrace_call_adjust(*p
++);
4132 * Some architecture linkers will pad between
4133 * the different mcount_loc sections of different
4134 * object files to satisfy alignments.
4135 * Skip any NULL pointers.
4140 if (pg
->index
== pg
->size
) {
4141 /* We should have allocated enough */
4142 if (WARN_ON(!pg
->next
))
4147 rec
= &pg
->records
[pg
->index
++];
4151 /* We should have used all pages */
4154 /* Assign the last page to ftrace_pages */
4158 * We only need to disable interrupts on start up
4159 * because we are modifying code that an interrupt
4160 * may execute, and the modification is not atomic.
4161 * But for modules, nothing runs the code we modify
4162 * until we are finished with it, and there's no
4163 * reason to cause large interrupt latencies while we do it.
4166 local_irq_save(flags
);
4167 ftrace_update_code(mod
, start_pg
);
4169 local_irq_restore(flags
);
4172 mutex_unlock(&ftrace_lock
);
4177 #ifdef CONFIG_MODULES
4179 #define next_to_ftrace_page(p) container_of(p, struct ftrace_page, next)
4181 void ftrace_release_mod(struct module
*mod
)
4183 struct dyn_ftrace
*rec
;
4184 struct ftrace_page
**last_pg
;
4185 struct ftrace_page
*pg
;
4188 mutex_lock(&ftrace_lock
);
4190 if (ftrace_disabled
)
4194 * Each module has its own ftrace_pages, remove
4195 * them from the list.
4197 last_pg
= &ftrace_pages_start
;
4198 for (pg
= ftrace_pages_start
; pg
; pg
= *last_pg
) {
4199 rec
= &pg
->records
[0];
4200 if (within_module_core(rec
->ip
, mod
)) {
4202 * As core pages are first, the first
4203 * page should never be a module page.
4205 if (WARN_ON(pg
== ftrace_pages_start
))
4208 /* Check if we are deleting the last page */
4209 if (pg
== ftrace_pages
)
4210 ftrace_pages
= next_to_ftrace_page(last_pg
);
4212 *last_pg
= pg
->next
;
4213 order
= get_count_order(pg
->size
/ ENTRIES_PER_PAGE
);
4214 free_pages((unsigned long)pg
->records
, order
);
4217 last_pg
= &pg
->next
;
4220 mutex_unlock(&ftrace_lock
);
4223 static void ftrace_init_module(struct module
*mod
,
4224 unsigned long *start
, unsigned long *end
)
4226 if (ftrace_disabled
|| start
== end
)
4228 ftrace_process_locs(mod
, start
, end
);
4231 static int ftrace_module_notify_enter(struct notifier_block
*self
,
4232 unsigned long val
, void *data
)
4234 struct module
*mod
= data
;
4236 if (val
== MODULE_STATE_COMING
)
4237 ftrace_init_module(mod
, mod
->ftrace_callsites
,
4238 mod
->ftrace_callsites
+
4239 mod
->num_ftrace_callsites
);
4243 static int ftrace_module_notify_exit(struct notifier_block
*self
,
4244 unsigned long val
, void *data
)
4246 struct module
*mod
= data
;
4248 if (val
== MODULE_STATE_GOING
)
4249 ftrace_release_mod(mod
);
4254 static int ftrace_module_notify_enter(struct notifier_block
*self
,
4255 unsigned long val
, void *data
)
4259 static int ftrace_module_notify_exit(struct notifier_block
*self
,
4260 unsigned long val
, void *data
)
4264 #endif /* CONFIG_MODULES */
4266 struct notifier_block ftrace_module_enter_nb
= {
4267 .notifier_call
= ftrace_module_notify_enter
,
4268 .priority
= INT_MAX
, /* Run before anything that can use kprobes */
4271 struct notifier_block ftrace_module_exit_nb
= {
4272 .notifier_call
= ftrace_module_notify_exit
,
4273 .priority
= INT_MIN
, /* Run after anything that can remove kprobes */
4276 void __init
ftrace_init(void)
4278 extern unsigned long __start_mcount_loc
[];
4279 extern unsigned long __stop_mcount_loc
[];
4280 unsigned long count
, flags
;
4283 local_irq_save(flags
);
4284 ret
= ftrace_dyn_arch_init();
4285 local_irq_restore(flags
);
4289 count
= __stop_mcount_loc
- __start_mcount_loc
;
4291 pr_info("ftrace: No functions to be traced?\n");
4295 pr_info("ftrace: allocating %ld entries in %ld pages\n",
4296 count
, count
/ ENTRIES_PER_PAGE
+ 1);
4298 last_ftrace_enabled
= ftrace_enabled
= 1;
4300 ret
= ftrace_process_locs(NULL
,
4304 ret
= register_module_notifier(&ftrace_module_enter_nb
);
4306 pr_warning("Failed to register trace ftrace module enter notifier\n");
4308 ret
= register_module_notifier(&ftrace_module_exit_nb
);
4310 pr_warning("Failed to register trace ftrace module exit notifier\n");
4312 set_ftrace_early_filters();
4316 ftrace_disabled
= 1;
4321 static struct ftrace_ops global_ops
= {
4322 .func
= ftrace_stub
,
4323 .flags
= FTRACE_OPS_FL_RECURSION_SAFE
| FTRACE_OPS_FL_INITIALIZED
,
4324 INIT_REGEX_LOCK(global_ops
)
4327 static int __init
ftrace_nodyn_init(void)
4332 core_initcall(ftrace_nodyn_init
);
4334 static inline int ftrace_init_dyn_debugfs(struct dentry
*d_tracer
) { return 0; }
4335 static inline void ftrace_startup_enable(int command
) { }
4336 /* Keep as macros so we do not need to define the commands */
4337 # define ftrace_startup(ops, command) \
4339 int ___ret = __register_ftrace_function(ops); \
4341 (ops)->flags |= FTRACE_OPS_FL_ENABLED; \
4344 # define ftrace_shutdown(ops, command) \
4346 int ___ret = __unregister_ftrace_function(ops); \
4348 (ops)->flags &= ~FTRACE_OPS_FL_ENABLED; \
4352 # define ftrace_startup_sysctl() do { } while (0)
4353 # define ftrace_shutdown_sysctl() do { } while (0)
4356 ftrace_ops_test(struct ftrace_ops
*ops
, unsigned long ip
, void *regs
)
4361 #endif /* CONFIG_DYNAMIC_FTRACE */
4363 __init
void ftrace_init_global_array_ops(struct trace_array
*tr
)
4365 tr
->ops
= &global_ops
;
4366 tr
->ops
->private = tr
;
4369 void ftrace_init_array_ops(struct trace_array
*tr
, ftrace_func_t func
)
4371 /* If we filter on pids, update to use the pid function */
4372 if (tr
->flags
& TRACE_ARRAY_FL_GLOBAL
) {
4373 if (WARN_ON(tr
->ops
->func
!= ftrace_stub
))
4374 printk("ftrace ops had %pS for function\n",
4376 /* Only the top level instance does pid tracing */
4377 if (!list_empty(&ftrace_pids
)) {
4378 set_ftrace_pid_function(func
);
4379 func
= ftrace_pid_func
;
4382 tr
->ops
->func
= func
;
4383 tr
->ops
->private = tr
;
4386 void ftrace_reset_array_ops(struct trace_array
*tr
)
4388 tr
->ops
->func
= ftrace_stub
;
4392 ftrace_ops_control_func(unsigned long ip
, unsigned long parent_ip
,
4393 struct ftrace_ops
*op
, struct pt_regs
*regs
)
4395 if (unlikely(trace_recursion_test(TRACE_CONTROL_BIT
)))
4399 * Some of the ops may be dynamically allocated,
4400 * they must be freed after a synchronize_sched().
4402 preempt_disable_notrace();
4403 trace_recursion_set(TRACE_CONTROL_BIT
);
4406 * Control funcs (perf) uses RCU. Only trace if
4407 * RCU is currently active.
4409 if (!rcu_is_watching())
4412 do_for_each_ftrace_op(op
, ftrace_control_list
) {
4413 if (!(op
->flags
& FTRACE_OPS_FL_STUB
) &&
4414 !ftrace_function_local_disabled(op
) &&
4415 ftrace_ops_test(op
, ip
, regs
))
4416 op
->func(ip
, parent_ip
, op
, regs
);
4417 } while_for_each_ftrace_op(op
);
4419 trace_recursion_clear(TRACE_CONTROL_BIT
);
4420 preempt_enable_notrace();
4423 static struct ftrace_ops control_ops
= {
4424 .func
= ftrace_ops_control_func
,
4425 .flags
= FTRACE_OPS_FL_RECURSION_SAFE
| FTRACE_OPS_FL_INITIALIZED
,
4426 INIT_REGEX_LOCK(control_ops
)
4430 __ftrace_ops_list_func(unsigned long ip
, unsigned long parent_ip
,
4431 struct ftrace_ops
*ignored
, struct pt_regs
*regs
)
4433 struct ftrace_ops
*op
;
4436 if (function_trace_stop
)
4439 bit
= trace_test_and_set_recursion(TRACE_LIST_START
, TRACE_LIST_MAX
);
4444 * Some of the ops may be dynamically allocated,
4445 * they must be freed after a synchronize_sched().
4447 preempt_disable_notrace();
4448 do_for_each_ftrace_op(op
, ftrace_ops_list
) {
4449 if (ftrace_ops_test(op
, ip
, regs
)) {
4450 if (WARN_ON(!op
->func
)) {
4451 function_trace_stop
= 1;
4452 printk("op=%p %pS\n", op
, op
);
4455 op
->func(ip
, parent_ip
, op
, regs
);
4457 } while_for_each_ftrace_op(op
);
4459 preempt_enable_notrace();
4460 trace_clear_recursion(bit
);
4464 * Some archs only support passing ip and parent_ip. Even though
4465 * the list function ignores the op parameter, we do not want any
4466 * C side effects, where a function is called without the caller
4467 * sending a third parameter.
4468 * Archs are to support both the regs and ftrace_ops at the same time.
4469 * If they support ftrace_ops, it is assumed they support regs.
4470 * If call backs want to use regs, they must either check for regs
4471 * being NULL, or CONFIG_DYNAMIC_FTRACE_WITH_REGS.
4472 * Note, CONFIG_DYNAMIC_FTRACE_WITH_REGS expects a full regs to be saved.
4473 * An architecture can pass partial regs with ftrace_ops and still
4474 * set the ARCH_SUPPORT_FTARCE_OPS.
4476 #if ARCH_SUPPORTS_FTRACE_OPS
4477 static void ftrace_ops_list_func(unsigned long ip
, unsigned long parent_ip
,
4478 struct ftrace_ops
*op
, struct pt_regs
*regs
)
4480 __ftrace_ops_list_func(ip
, parent_ip
, NULL
, regs
);
4483 static void ftrace_ops_no_ops(unsigned long ip
, unsigned long parent_ip
)
4485 __ftrace_ops_list_func(ip
, parent_ip
, NULL
, NULL
);
4489 static void clear_ftrace_swapper(void)
4491 struct task_struct
*p
;
4495 for_each_online_cpu(cpu
) {
4497 clear_tsk_trace_trace(p
);
4502 static void set_ftrace_swapper(void)
4504 struct task_struct
*p
;
4508 for_each_online_cpu(cpu
) {
4510 set_tsk_trace_trace(p
);
4515 static void clear_ftrace_pid(struct pid
*pid
)
4517 struct task_struct
*p
;
4520 do_each_pid_task(pid
, PIDTYPE_PID
, p
) {
4521 clear_tsk_trace_trace(p
);
4522 } while_each_pid_task(pid
, PIDTYPE_PID
, p
);
4528 static void set_ftrace_pid(struct pid
*pid
)
4530 struct task_struct
*p
;
4533 do_each_pid_task(pid
, PIDTYPE_PID
, p
) {
4534 set_tsk_trace_trace(p
);
4535 } while_each_pid_task(pid
, PIDTYPE_PID
, p
);
4539 static void clear_ftrace_pid_task(struct pid
*pid
)
4541 if (pid
== ftrace_swapper_pid
)
4542 clear_ftrace_swapper();
4544 clear_ftrace_pid(pid
);
4547 static void set_ftrace_pid_task(struct pid
*pid
)
4549 if (pid
== ftrace_swapper_pid
)
4550 set_ftrace_swapper();
4552 set_ftrace_pid(pid
);
4555 static int ftrace_pid_add(int p
)
4558 struct ftrace_pid
*fpid
;
4561 mutex_lock(&ftrace_lock
);
4564 pid
= ftrace_swapper_pid
;
4566 pid
= find_get_pid(p
);
4573 list_for_each_entry(fpid
, &ftrace_pids
, list
)
4574 if (fpid
->pid
== pid
)
4579 fpid
= kmalloc(sizeof(*fpid
), GFP_KERNEL
);
4583 list_add(&fpid
->list
, &ftrace_pids
);
4586 set_ftrace_pid_task(pid
);
4588 ftrace_update_pid_func();
4589 ftrace_startup_enable(0);
4591 mutex_unlock(&ftrace_lock
);
4595 if (pid
!= ftrace_swapper_pid
)
4599 mutex_unlock(&ftrace_lock
);
4603 static void ftrace_pid_reset(void)
4605 struct ftrace_pid
*fpid
, *safe
;
4607 mutex_lock(&ftrace_lock
);
4608 list_for_each_entry_safe(fpid
, safe
, &ftrace_pids
, list
) {
4609 struct pid
*pid
= fpid
->pid
;
4611 clear_ftrace_pid_task(pid
);
4613 list_del(&fpid
->list
);
4617 ftrace_update_pid_func();
4618 ftrace_startup_enable(0);
4620 mutex_unlock(&ftrace_lock
);
4623 static void *fpid_start(struct seq_file
*m
, loff_t
*pos
)
4625 mutex_lock(&ftrace_lock
);
4627 if (list_empty(&ftrace_pids
) && (!*pos
))
4630 return seq_list_start(&ftrace_pids
, *pos
);
4633 static void *fpid_next(struct seq_file
*m
, void *v
, loff_t
*pos
)
4638 return seq_list_next(v
, &ftrace_pids
, pos
);
4641 static void fpid_stop(struct seq_file
*m
, void *p
)
4643 mutex_unlock(&ftrace_lock
);
4646 static int fpid_show(struct seq_file
*m
, void *v
)
4648 const struct ftrace_pid
*fpid
= list_entry(v
, struct ftrace_pid
, list
);
4650 if (v
== (void *)1) {
4651 seq_printf(m
, "no pid\n");
4655 if (fpid
->pid
== ftrace_swapper_pid
)
4656 seq_printf(m
, "swapper tasks\n");
4658 seq_printf(m
, "%u\n", pid_vnr(fpid
->pid
));
4663 static const struct seq_operations ftrace_pid_sops
= {
4664 .start
= fpid_start
,
4671 ftrace_pid_open(struct inode
*inode
, struct file
*file
)
4675 if ((file
->f_mode
& FMODE_WRITE
) &&
4676 (file
->f_flags
& O_TRUNC
))
4679 if (file
->f_mode
& FMODE_READ
)
4680 ret
= seq_open(file
, &ftrace_pid_sops
);
4686 ftrace_pid_write(struct file
*filp
, const char __user
*ubuf
,
4687 size_t cnt
, loff_t
*ppos
)
4693 if (cnt
>= sizeof(buf
))
4696 if (copy_from_user(&buf
, ubuf
, cnt
))
4702 * Allow "echo > set_ftrace_pid" or "echo -n '' > set_ftrace_pid"
4703 * to clean the filter quietly.
4705 tmp
= strstrip(buf
);
4706 if (strlen(tmp
) == 0)
4709 ret
= kstrtol(tmp
, 10, &val
);
4713 ret
= ftrace_pid_add(val
);
4715 return ret
? ret
: cnt
;
4719 ftrace_pid_release(struct inode
*inode
, struct file
*file
)
4721 if (file
->f_mode
& FMODE_READ
)
4722 seq_release(inode
, file
);
4727 static const struct file_operations ftrace_pid_fops
= {
4728 .open
= ftrace_pid_open
,
4729 .write
= ftrace_pid_write
,
4731 .llseek
= tracing_lseek
,
4732 .release
= ftrace_pid_release
,
4735 static __init
int ftrace_init_debugfs(void)
4737 struct dentry
*d_tracer
;
4739 d_tracer
= tracing_init_dentry();
4743 ftrace_init_dyn_debugfs(d_tracer
);
4745 trace_create_file("set_ftrace_pid", 0644, d_tracer
,
4746 NULL
, &ftrace_pid_fops
);
4748 ftrace_profile_debugfs(d_tracer
);
4752 fs_initcall(ftrace_init_debugfs
);
4755 * ftrace_kill - kill ftrace
4757 * This function should be used by panic code. It stops ftrace
4758 * but in a not so nice way. If you need to simply kill ftrace
4759 * from a non-atomic section, use ftrace_kill.
4761 void ftrace_kill(void)
4763 ftrace_disabled
= 1;
4765 clear_ftrace_function();
4769 * Test if ftrace is dead or not.
4771 int ftrace_is_dead(void)
4773 return ftrace_disabled
;
4777 * register_ftrace_function - register a function for profiling
4778 * @ops - ops structure that holds the function for profiling.
4780 * Register a function to be called by all functions in the
4783 * Note: @ops->func and all the functions it calls must be labeled
4784 * with "notrace", otherwise it will go into a
4787 int register_ftrace_function(struct ftrace_ops
*ops
)
4791 ftrace_ops_init(ops
);
4793 mutex_lock(&ftrace_lock
);
4795 ret
= ftrace_startup(ops
, 0);
4797 mutex_unlock(&ftrace_lock
);
4801 EXPORT_SYMBOL_GPL(register_ftrace_function
);
4804 * unregister_ftrace_function - unregister a function for profiling.
4805 * @ops - ops structure that holds the function to unregister
4807 * Unregister a function that was added to be called by ftrace profiling.
4809 int unregister_ftrace_function(struct ftrace_ops
*ops
)
4813 mutex_lock(&ftrace_lock
);
4814 ret
= ftrace_shutdown(ops
, 0);
4815 mutex_unlock(&ftrace_lock
);
4819 EXPORT_SYMBOL_GPL(unregister_ftrace_function
);
4822 ftrace_enable_sysctl(struct ctl_table
*table
, int write
,
4823 void __user
*buffer
, size_t *lenp
,
4828 mutex_lock(&ftrace_lock
);
4830 if (unlikely(ftrace_disabled
))
4833 ret
= proc_dointvec(table
, write
, buffer
, lenp
, ppos
);
4835 if (ret
|| !write
|| (last_ftrace_enabled
== !!ftrace_enabled
))
4838 last_ftrace_enabled
= !!ftrace_enabled
;
4840 if (ftrace_enabled
) {
4842 ftrace_startup_sysctl();
4844 /* we are starting ftrace again */
4845 if (ftrace_ops_list
!= &ftrace_list_end
)
4846 update_ftrace_function();
4849 /* stopping ftrace calls (just send to ftrace_stub) */
4850 ftrace_trace_function
= ftrace_stub
;
4852 ftrace_shutdown_sysctl();
4856 mutex_unlock(&ftrace_lock
);
4860 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
4862 static int ftrace_graph_active
;
4863 static struct notifier_block ftrace_suspend_notifier
;
4865 int ftrace_graph_entry_stub(struct ftrace_graph_ent
*trace
)
4870 /* The callbacks that hook a function */
4871 trace_func_graph_ret_t ftrace_graph_return
=
4872 (trace_func_graph_ret_t
)ftrace_stub
;
4873 trace_func_graph_ent_t ftrace_graph_entry
= ftrace_graph_entry_stub
;
4874 static trace_func_graph_ent_t __ftrace_graph_entry
= ftrace_graph_entry_stub
;
4876 /* Try to assign a return stack array on FTRACE_RETSTACK_ALLOC_SIZE tasks. */
4877 static int alloc_retstack_tasklist(struct ftrace_ret_stack
**ret_stack_list
)
4881 unsigned long flags
;
4882 int start
= 0, end
= FTRACE_RETSTACK_ALLOC_SIZE
;
4883 struct task_struct
*g
, *t
;
4885 for (i
= 0; i
< FTRACE_RETSTACK_ALLOC_SIZE
; i
++) {
4886 ret_stack_list
[i
] = kmalloc(FTRACE_RETFUNC_DEPTH
4887 * sizeof(struct ftrace_ret_stack
),
4889 if (!ret_stack_list
[i
]) {
4897 read_lock_irqsave(&tasklist_lock
, flags
);
4898 do_each_thread(g
, t
) {
4904 if (t
->ret_stack
== NULL
) {
4905 atomic_set(&t
->tracing_graph_pause
, 0);
4906 atomic_set(&t
->trace_overrun
, 0);
4907 t
->curr_ret_stack
= -1;
4908 /* Make sure the tasks see the -1 first: */
4910 t
->ret_stack
= ret_stack_list
[start
++];
4912 } while_each_thread(g
, t
);
4915 read_unlock_irqrestore(&tasklist_lock
, flags
);
4917 for (i
= start
; i
< end
; i
++)
4918 kfree(ret_stack_list
[i
]);
4923 ftrace_graph_probe_sched_switch(void *ignore
,
4924 struct task_struct
*prev
, struct task_struct
*next
)
4926 unsigned long long timestamp
;
4930 * Does the user want to count the time a function was asleep.
4931 * If so, do not update the time stamps.
4933 if (trace_flags
& TRACE_ITER_SLEEP_TIME
)
4936 timestamp
= trace_clock_local();
4938 prev
->ftrace_timestamp
= timestamp
;
4940 /* only process tasks that we timestamped */
4941 if (!next
->ftrace_timestamp
)
4945 * Update all the counters in next to make up for the
4946 * time next was sleeping.
4948 timestamp
-= next
->ftrace_timestamp
;
4950 for (index
= next
->curr_ret_stack
; index
>= 0; index
--)
4951 next
->ret_stack
[index
].calltime
+= timestamp
;
4954 /* Allocate a return stack for each task */
4955 static int start_graph_tracing(void)
4957 struct ftrace_ret_stack
**ret_stack_list
;
4960 ret_stack_list
= kmalloc(FTRACE_RETSTACK_ALLOC_SIZE
*
4961 sizeof(struct ftrace_ret_stack
*),
4964 if (!ret_stack_list
)
4967 /* The cpu_boot init_task->ret_stack will never be freed */
4968 for_each_online_cpu(cpu
) {
4969 if (!idle_task(cpu
)->ret_stack
)
4970 ftrace_graph_init_idle_task(idle_task(cpu
), cpu
);
4974 ret
= alloc_retstack_tasklist(ret_stack_list
);
4975 } while (ret
== -EAGAIN
);
4978 ret
= register_trace_sched_switch(ftrace_graph_probe_sched_switch
, NULL
);
4980 pr_info("ftrace_graph: Couldn't activate tracepoint"
4981 " probe to kernel_sched_switch\n");
4984 kfree(ret_stack_list
);
4989 * Hibernation protection.
4990 * The state of the current task is too much unstable during
4991 * suspend/restore to disk. We want to protect against that.
4994 ftrace_suspend_notifier_call(struct notifier_block
*bl
, unsigned long state
,
4998 case PM_HIBERNATION_PREPARE
:
4999 pause_graph_tracing();
5002 case PM_POST_HIBERNATION
:
5003 unpause_graph_tracing();
5009 /* Just a place holder for function graph */
5010 static struct ftrace_ops fgraph_ops __read_mostly
= {
5011 .func
= ftrace_stub
,
5012 .flags
= FTRACE_OPS_FL_STUB
| FTRACE_OPS_FL_RECURSION_SAFE
,
5015 static int ftrace_graph_entry_test(struct ftrace_graph_ent
*trace
)
5017 if (!ftrace_ops_test(&global_ops
, trace
->func
, NULL
))
5019 return __ftrace_graph_entry(trace
);
5023 * The function graph tracer should only trace the functions defined
5024 * by set_ftrace_filter and set_ftrace_notrace. If another function
5025 * tracer ops is registered, the graph tracer requires testing the
5026 * function against the global ops, and not just trace any function
5027 * that any ftrace_ops registered.
5029 static void update_function_graph_func(void)
5031 if (ftrace_ops_list
== &ftrace_list_end
||
5032 (ftrace_ops_list
== &global_ops
&&
5033 global_ops
.next
== &ftrace_list_end
))
5034 ftrace_graph_entry
= __ftrace_graph_entry
;
5036 ftrace_graph_entry
= ftrace_graph_entry_test
;
5039 int register_ftrace_graph(trace_func_graph_ret_t retfunc
,
5040 trace_func_graph_ent_t entryfunc
)
5044 mutex_lock(&ftrace_lock
);
5046 /* we currently allow only one tracer registered at a time */
5047 if (ftrace_graph_active
) {
5052 ftrace_suspend_notifier
.notifier_call
= ftrace_suspend_notifier_call
;
5053 register_pm_notifier(&ftrace_suspend_notifier
);
5055 ftrace_graph_active
++;
5056 ret
= start_graph_tracing();
5058 ftrace_graph_active
--;
5062 ftrace_graph_return
= retfunc
;
5065 * Update the indirect function to the entryfunc, and the
5066 * function that gets called to the entry_test first. Then
5067 * call the update fgraph entry function to determine if
5068 * the entryfunc should be called directly or not.
5070 __ftrace_graph_entry
= entryfunc
;
5071 ftrace_graph_entry
= ftrace_graph_entry_test
;
5072 update_function_graph_func();
5074 ret
= ftrace_startup(&fgraph_ops
, FTRACE_START_FUNC_RET
);
5077 mutex_unlock(&ftrace_lock
);
5081 void unregister_ftrace_graph(void)
5083 mutex_lock(&ftrace_lock
);
5085 if (unlikely(!ftrace_graph_active
))
5088 ftrace_graph_active
--;
5089 ftrace_graph_return
= (trace_func_graph_ret_t
)ftrace_stub
;
5090 ftrace_graph_entry
= ftrace_graph_entry_stub
;
5091 __ftrace_graph_entry
= ftrace_graph_entry_stub
;
5092 ftrace_shutdown(&fgraph_ops
, FTRACE_STOP_FUNC_RET
);
5093 unregister_pm_notifier(&ftrace_suspend_notifier
);
5094 unregister_trace_sched_switch(ftrace_graph_probe_sched_switch
, NULL
);
5097 mutex_unlock(&ftrace_lock
);
5100 static DEFINE_PER_CPU(struct ftrace_ret_stack
*, idle_ret_stack
);
5103 graph_init_task(struct task_struct
*t
, struct ftrace_ret_stack
*ret_stack
)
5105 atomic_set(&t
->tracing_graph_pause
, 0);
5106 atomic_set(&t
->trace_overrun
, 0);
5107 t
->ftrace_timestamp
= 0;
5108 /* make curr_ret_stack visible before we add the ret_stack */
5110 t
->ret_stack
= ret_stack
;
5114 * Allocate a return stack for the idle task. May be the first
5115 * time through, or it may be done by CPU hotplug online.
5117 void ftrace_graph_init_idle_task(struct task_struct
*t
, int cpu
)
5119 t
->curr_ret_stack
= -1;
5121 * The idle task has no parent, it either has its own
5122 * stack or no stack at all.
5125 WARN_ON(t
->ret_stack
!= per_cpu(idle_ret_stack
, cpu
));
5127 if (ftrace_graph_active
) {
5128 struct ftrace_ret_stack
*ret_stack
;
5130 ret_stack
= per_cpu(idle_ret_stack
, cpu
);
5132 ret_stack
= kmalloc(FTRACE_RETFUNC_DEPTH
5133 * sizeof(struct ftrace_ret_stack
),
5137 per_cpu(idle_ret_stack
, cpu
) = ret_stack
;
5139 graph_init_task(t
, ret_stack
);
5143 /* Allocate a return stack for newly created task */
5144 void ftrace_graph_init_task(struct task_struct
*t
)
5146 /* Make sure we do not use the parent ret_stack */
5147 t
->ret_stack
= NULL
;
5148 t
->curr_ret_stack
= -1;
5150 if (ftrace_graph_active
) {
5151 struct ftrace_ret_stack
*ret_stack
;
5153 ret_stack
= kmalloc(FTRACE_RETFUNC_DEPTH
5154 * sizeof(struct ftrace_ret_stack
),
5158 graph_init_task(t
, ret_stack
);
5162 void ftrace_graph_exit_task(struct task_struct
*t
)
5164 struct ftrace_ret_stack
*ret_stack
= t
->ret_stack
;
5166 t
->ret_stack
= NULL
;
5167 /* NULL must become visible to IRQs before we free it: */
5173 void ftrace_graph_stop(void)