On Tue, Nov 06, 2007 at 02:33:53AM -0800, akpm@linux-foundation.org wrote:
[mmotm.git] / kernel / trace / ftrace.c
blob1a7f6abd06bf4c0f6c16d8e0be0bd52be9316549
1 /*
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 William Lee Irwin III
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/kprobes.h>
26 #include <linux/ftrace.h>
27 #include <linux/sysctl.h>
28 #include <linux/ctype.h>
29 #include <linux/list.h>
30 #include <linux/hash.h>
32 #include <trace/events/sched.h>
34 #include <asm/ftrace.h>
35 #include <asm/setup.h>
37 #include "trace_output.h"
38 #include "trace_stat.h"
40 #define FTRACE_WARN_ON(cond) \
41 do { \
42 if (WARN_ON(cond)) \
43 ftrace_kill(); \
44 } while (0)
46 #define FTRACE_WARN_ON_ONCE(cond) \
47 do { \
48 if (WARN_ON_ONCE(cond)) \
49 ftrace_kill(); \
50 } while (0)
52 /* hash bits for specific function selection */
53 #define FTRACE_HASH_BITS 7
54 #define FTRACE_FUNC_HASHSIZE (1 << FTRACE_HASH_BITS)
56 /* ftrace_enabled is a method to turn ftrace on or off */
57 int ftrace_enabled __read_mostly;
58 static int last_ftrace_enabled;
60 /* Quick disabling of function tracer. */
61 int function_trace_stop;
64 * ftrace_disabled is set when an anomaly is discovered.
65 * ftrace_disabled is much stronger than ftrace_enabled.
67 static int ftrace_disabled __read_mostly;
69 static DEFINE_MUTEX(ftrace_lock);
71 static struct ftrace_ops ftrace_list_end __read_mostly =
73 .func = ftrace_stub,
76 static struct ftrace_ops *ftrace_list __read_mostly = &ftrace_list_end;
77 ftrace_func_t ftrace_trace_function __read_mostly = ftrace_stub;
78 ftrace_func_t __ftrace_trace_function __read_mostly = ftrace_stub;
79 ftrace_func_t ftrace_pid_function __read_mostly = ftrace_stub;
81 static void ftrace_list_func(unsigned long ip, unsigned long parent_ip)
83 struct ftrace_ops *op = ftrace_list;
85 /* in case someone actually ports this to alpha! */
86 read_barrier_depends();
88 while (op != &ftrace_list_end) {
89 /* silly alpha */
90 read_barrier_depends();
91 op->func(ip, parent_ip);
92 op = op->next;
96 static void ftrace_pid_func(unsigned long ip, unsigned long parent_ip)
98 if (!test_tsk_trace_trace(current))
99 return;
101 ftrace_pid_function(ip, parent_ip);
104 static void set_ftrace_pid_function(ftrace_func_t func)
106 /* do not set ftrace_pid_function to itself! */
107 if (func != ftrace_pid_func)
108 ftrace_pid_function = func;
112 * clear_ftrace_function - reset the ftrace function
114 * This NULLs the ftrace function and in essence stops
115 * tracing. There may be lag
117 void clear_ftrace_function(void)
119 ftrace_trace_function = ftrace_stub;
120 __ftrace_trace_function = ftrace_stub;
121 ftrace_pid_function = ftrace_stub;
124 #ifndef CONFIG_HAVE_FUNCTION_TRACE_MCOUNT_TEST
126 * For those archs that do not test ftrace_trace_stop in their
127 * mcount call site, we need to do it from C.
129 static void ftrace_test_stop_func(unsigned long ip, unsigned long parent_ip)
131 if (function_trace_stop)
132 return;
134 __ftrace_trace_function(ip, parent_ip);
136 #endif
138 static int __register_ftrace_function(struct ftrace_ops *ops)
140 ops->next = ftrace_list;
142 * We are entering ops into the ftrace_list but another
143 * CPU might be walking that list. We need to make sure
144 * the ops->next pointer is valid before another CPU sees
145 * the ops pointer included into the ftrace_list.
147 smp_wmb();
148 ftrace_list = ops;
150 if (ftrace_enabled) {
151 ftrace_func_t func;
153 if (ops->next == &ftrace_list_end)
154 func = ops->func;
155 else
156 func = ftrace_list_func;
158 if (ftrace_pid_trace) {
159 set_ftrace_pid_function(func);
160 func = ftrace_pid_func;
164 * For one func, simply call it directly.
165 * For more than one func, call the chain.
167 #ifdef CONFIG_HAVE_FUNCTION_TRACE_MCOUNT_TEST
168 ftrace_trace_function = func;
169 #else
170 __ftrace_trace_function = func;
171 ftrace_trace_function = ftrace_test_stop_func;
172 #endif
175 return 0;
178 static int __unregister_ftrace_function(struct ftrace_ops *ops)
180 struct ftrace_ops **p;
183 * If we are removing the last function, then simply point
184 * to the ftrace_stub.
186 if (ftrace_list == ops && ops->next == &ftrace_list_end) {
187 ftrace_trace_function = ftrace_stub;
188 ftrace_list = &ftrace_list_end;
189 return 0;
192 for (p = &ftrace_list; *p != &ftrace_list_end; p = &(*p)->next)
193 if (*p == ops)
194 break;
196 if (*p != ops)
197 return -1;
199 *p = (*p)->next;
201 if (ftrace_enabled) {
202 /* If we only have one func left, then call that directly */
203 if (ftrace_list->next == &ftrace_list_end) {
204 ftrace_func_t func = ftrace_list->func;
206 if (ftrace_pid_trace) {
207 set_ftrace_pid_function(func);
208 func = ftrace_pid_func;
210 #ifdef CONFIG_HAVE_FUNCTION_TRACE_MCOUNT_TEST
211 ftrace_trace_function = func;
212 #else
213 __ftrace_trace_function = func;
214 #endif
218 return 0;
221 static void ftrace_update_pid_func(void)
223 ftrace_func_t func;
225 if (ftrace_trace_function == ftrace_stub)
226 return;
228 #ifdef CONFIG_HAVE_FUNCTION_TRACE_MCOUNT_TEST
229 func = ftrace_trace_function;
230 #else
231 func = __ftrace_trace_function;
232 #endif
234 if (ftrace_pid_trace) {
235 set_ftrace_pid_function(func);
236 func = ftrace_pid_func;
237 } else {
238 if (func == ftrace_pid_func)
239 func = ftrace_pid_function;
242 #ifdef CONFIG_HAVE_FUNCTION_TRACE_MCOUNT_TEST
243 ftrace_trace_function = func;
244 #else
245 __ftrace_trace_function = func;
246 #endif
249 #ifdef CONFIG_FUNCTION_PROFILER
250 struct ftrace_profile {
251 struct hlist_node node;
252 unsigned long ip;
253 unsigned long counter;
254 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
255 unsigned long long time;
256 #endif
259 struct ftrace_profile_page {
260 struct ftrace_profile_page *next;
261 unsigned long index;
262 struct ftrace_profile records[];
265 struct ftrace_profile_stat {
266 atomic_t disabled;
267 struct hlist_head *hash;
268 struct ftrace_profile_page *pages;
269 struct ftrace_profile_page *start;
270 struct tracer_stat stat;
273 #define PROFILE_RECORDS_SIZE \
274 (PAGE_SIZE - offsetof(struct ftrace_profile_page, records))
276 #define PROFILES_PER_PAGE \
277 (PROFILE_RECORDS_SIZE / sizeof(struct ftrace_profile))
279 static int ftrace_profile_bits __read_mostly;
280 static int ftrace_profile_enabled __read_mostly;
282 /* ftrace_profile_lock - synchronize the enable and disable of the profiler */
283 static DEFINE_MUTEX(ftrace_profile_lock);
285 static DEFINE_PER_CPU(struct ftrace_profile_stat, ftrace_profile_stats);
287 #define FTRACE_PROFILE_HASH_SIZE 1024 /* must be power of 2 */
289 static void *
290 function_stat_next(void *v, int idx)
292 struct ftrace_profile *rec = v;
293 struct ftrace_profile_page *pg;
295 pg = (struct ftrace_profile_page *)((unsigned long)rec & PAGE_MASK);
297 again:
298 if (idx != 0)
299 rec++;
301 if ((void *)rec >= (void *)&pg->records[pg->index]) {
302 pg = pg->next;
303 if (!pg)
304 return NULL;
305 rec = &pg->records[0];
306 if (!rec->counter)
307 goto again;
310 return rec;
313 static void *function_stat_start(struct tracer_stat *trace)
315 struct ftrace_profile_stat *stat =
316 container_of(trace, struct ftrace_profile_stat, stat);
318 if (!stat || !stat->start)
319 return NULL;
321 return function_stat_next(&stat->start->records[0], 0);
324 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
325 /* function graph compares on total time */
326 static int function_stat_cmp(void *p1, void *p2)
328 struct ftrace_profile *a = p1;
329 struct ftrace_profile *b = p2;
331 if (a->time < b->time)
332 return -1;
333 if (a->time > b->time)
334 return 1;
335 else
336 return 0;
338 #else
339 /* not function graph compares against hits */
340 static int function_stat_cmp(void *p1, void *p2)
342 struct ftrace_profile *a = p1;
343 struct ftrace_profile *b = p2;
345 if (a->counter < b->counter)
346 return -1;
347 if (a->counter > b->counter)
348 return 1;
349 else
350 return 0;
352 #endif
354 static int function_stat_headers(struct seq_file *m)
356 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
357 seq_printf(m, " Function "
358 "Hit Time Avg\n"
359 " -------- "
360 "--- ---- ---\n");
361 #else
362 seq_printf(m, " Function Hit\n"
363 " -------- ---\n");
364 #endif
365 return 0;
368 static int function_stat_show(struct seq_file *m, void *v)
370 struct ftrace_profile *rec = v;
371 char str[KSYM_SYMBOL_LEN];
372 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
373 static DEFINE_MUTEX(mutex);
374 static struct trace_seq s;
375 unsigned long long avg;
376 #endif
378 kallsyms_lookup(rec->ip, NULL, NULL, NULL, str);
379 seq_printf(m, " %-30.30s %10lu", str, rec->counter);
381 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
382 seq_printf(m, " ");
383 avg = rec->time;
384 do_div(avg, rec->counter);
386 mutex_lock(&mutex);
387 trace_seq_init(&s);
388 trace_print_graph_duration(rec->time, &s);
389 trace_seq_puts(&s, " ");
390 trace_print_graph_duration(avg, &s);
391 trace_print_seq(m, &s);
392 mutex_unlock(&mutex);
393 #endif
394 seq_putc(m, '\n');
396 return 0;
399 static void ftrace_profile_reset(struct ftrace_profile_stat *stat)
401 struct ftrace_profile_page *pg;
403 pg = stat->pages = stat->start;
405 while (pg) {
406 memset(pg->records, 0, PROFILE_RECORDS_SIZE);
407 pg->index = 0;
408 pg = pg->next;
411 memset(stat->hash, 0,
412 FTRACE_PROFILE_HASH_SIZE * sizeof(struct hlist_head));
415 int ftrace_profile_pages_init(struct ftrace_profile_stat *stat)
417 struct ftrace_profile_page *pg;
418 int functions;
419 int pages;
420 int i;
422 /* If we already allocated, do nothing */
423 if (stat->pages)
424 return 0;
426 stat->pages = (void *)get_zeroed_page(GFP_KERNEL);
427 if (!stat->pages)
428 return -ENOMEM;
430 #ifdef CONFIG_DYNAMIC_FTRACE
431 functions = ftrace_update_tot_cnt;
432 #else
434 * We do not know the number of functions that exist because
435 * dynamic tracing is what counts them. With past experience
436 * we have around 20K functions. That should be more than enough.
437 * It is highly unlikely we will execute every function in
438 * the kernel.
440 functions = 20000;
441 #endif
443 pg = stat->start = stat->pages;
445 pages = DIV_ROUND_UP(functions, PROFILES_PER_PAGE);
447 for (i = 0; i < pages; i++) {
448 pg->next = (void *)get_zeroed_page(GFP_KERNEL);
449 if (!pg->next)
450 goto out_free;
451 pg = pg->next;
454 return 0;
456 out_free:
457 pg = stat->start;
458 while (pg) {
459 unsigned long tmp = (unsigned long)pg;
461 pg = pg->next;
462 free_page(tmp);
465 free_page((unsigned long)stat->pages);
466 stat->pages = NULL;
467 stat->start = NULL;
469 return -ENOMEM;
472 static int ftrace_profile_init_cpu(int cpu)
474 struct ftrace_profile_stat *stat;
475 int size;
477 stat = &per_cpu(ftrace_profile_stats, cpu);
479 if (stat->hash) {
480 /* If the profile is already created, simply reset it */
481 ftrace_profile_reset(stat);
482 return 0;
486 * We are profiling all functions, but usually only a few thousand
487 * functions are hit. We'll make a hash of 1024 items.
489 size = FTRACE_PROFILE_HASH_SIZE;
491 stat->hash = kzalloc(sizeof(struct hlist_head) * size, GFP_KERNEL);
493 if (!stat->hash)
494 return -ENOMEM;
496 if (!ftrace_profile_bits) {
497 size--;
499 for (; size; size >>= 1)
500 ftrace_profile_bits++;
503 /* Preallocate the function profiling pages */
504 if (ftrace_profile_pages_init(stat) < 0) {
505 kfree(stat->hash);
506 stat->hash = NULL;
507 return -ENOMEM;
510 return 0;
513 static int ftrace_profile_init(void)
515 int cpu;
516 int ret = 0;
518 for_each_online_cpu(cpu) {
519 ret = ftrace_profile_init_cpu(cpu);
520 if (ret)
521 break;
524 return ret;
527 /* interrupts must be disabled */
528 static struct ftrace_profile *
529 ftrace_find_profiled_func(struct ftrace_profile_stat *stat, unsigned long ip)
531 struct ftrace_profile *rec;
532 struct hlist_head *hhd;
533 struct hlist_node *n;
534 unsigned long key;
536 key = hash_long(ip, ftrace_profile_bits);
537 hhd = &stat->hash[key];
539 if (hlist_empty(hhd))
540 return NULL;
542 hlist_for_each_entry_rcu(rec, n, hhd, node) {
543 if (rec->ip == ip)
544 return rec;
547 return NULL;
550 static void ftrace_add_profile(struct ftrace_profile_stat *stat,
551 struct ftrace_profile *rec)
553 unsigned long key;
555 key = hash_long(rec->ip, ftrace_profile_bits);
556 hlist_add_head_rcu(&rec->node, &stat->hash[key]);
560 * The memory is already allocated, this simply finds a new record to use.
562 static struct ftrace_profile *
563 ftrace_profile_alloc(struct ftrace_profile_stat *stat, unsigned long ip)
565 struct ftrace_profile *rec = NULL;
567 /* prevent recursion (from NMIs) */
568 if (atomic_inc_return(&stat->disabled) != 1)
569 goto out;
572 * Try to find the function again since an NMI
573 * could have added it
575 rec = ftrace_find_profiled_func(stat, ip);
576 if (rec)
577 goto out;
579 if (stat->pages->index == PROFILES_PER_PAGE) {
580 if (!stat->pages->next)
581 goto out;
582 stat->pages = stat->pages->next;
585 rec = &stat->pages->records[stat->pages->index++];
586 rec->ip = ip;
587 ftrace_add_profile(stat, rec);
589 out:
590 atomic_dec(&stat->disabled);
592 return rec;
595 static void
596 function_profile_call(unsigned long ip, unsigned long parent_ip)
598 struct ftrace_profile_stat *stat;
599 struct ftrace_profile *rec;
600 unsigned long flags;
602 if (!ftrace_profile_enabled)
603 return;
605 local_irq_save(flags);
607 stat = &__get_cpu_var(ftrace_profile_stats);
608 if (!stat->hash || !ftrace_profile_enabled)
609 goto out;
611 rec = ftrace_find_profiled_func(stat, ip);
612 if (!rec) {
613 rec = ftrace_profile_alloc(stat, ip);
614 if (!rec)
615 goto out;
618 rec->counter++;
619 out:
620 local_irq_restore(flags);
623 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
624 static int profile_graph_entry(struct ftrace_graph_ent *trace)
626 function_profile_call(trace->func, 0);
627 return 1;
630 static void profile_graph_return(struct ftrace_graph_ret *trace)
632 struct ftrace_profile_stat *stat;
633 unsigned long long calltime;
634 struct ftrace_profile *rec;
635 unsigned long flags;
637 local_irq_save(flags);
638 stat = &__get_cpu_var(ftrace_profile_stats);
639 if (!stat->hash || !ftrace_profile_enabled)
640 goto out;
642 calltime = trace->rettime - trace->calltime;
644 if (!(trace_flags & TRACE_ITER_GRAPH_TIME)) {
645 int index;
647 index = trace->depth;
649 /* Append this call time to the parent time to subtract */
650 if (index)
651 current->ret_stack[index - 1].subtime += calltime;
653 if (current->ret_stack[index].subtime < calltime)
654 calltime -= current->ret_stack[index].subtime;
655 else
656 calltime = 0;
659 rec = ftrace_find_profiled_func(stat, trace->func);
660 if (rec)
661 rec->time += calltime;
663 out:
664 local_irq_restore(flags);
667 static int register_ftrace_profiler(void)
669 return register_ftrace_graph(&profile_graph_return,
670 &profile_graph_entry);
673 static void unregister_ftrace_profiler(void)
675 unregister_ftrace_graph();
677 #else
678 static struct ftrace_ops ftrace_profile_ops __read_mostly =
680 .func = function_profile_call,
683 static int register_ftrace_profiler(void)
685 return register_ftrace_function(&ftrace_profile_ops);
688 static void unregister_ftrace_profiler(void)
690 unregister_ftrace_function(&ftrace_profile_ops);
692 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
694 static ssize_t
695 ftrace_profile_write(struct file *filp, const char __user *ubuf,
696 size_t cnt, loff_t *ppos)
698 unsigned long val;
699 char buf[64]; /* big enough to hold a number */
700 int ret;
702 if (cnt >= sizeof(buf))
703 return -EINVAL;
705 if (copy_from_user(&buf, ubuf, cnt))
706 return -EFAULT;
708 buf[cnt] = 0;
710 ret = strict_strtoul(buf, 10, &val);
711 if (ret < 0)
712 return ret;
714 val = !!val;
716 mutex_lock(&ftrace_profile_lock);
717 if (ftrace_profile_enabled ^ val) {
718 if (val) {
719 ret = ftrace_profile_init();
720 if (ret < 0) {
721 cnt = ret;
722 goto out;
725 ret = register_ftrace_profiler();
726 if (ret < 0) {
727 cnt = ret;
728 goto out;
730 ftrace_profile_enabled = 1;
731 } else {
732 ftrace_profile_enabled = 0;
734 * unregister_ftrace_profiler calls stop_machine
735 * so this acts like an synchronize_sched.
737 unregister_ftrace_profiler();
740 out:
741 mutex_unlock(&ftrace_profile_lock);
743 filp->f_pos += cnt;
745 return cnt;
748 static ssize_t
749 ftrace_profile_read(struct file *filp, char __user *ubuf,
750 size_t cnt, loff_t *ppos)
752 char buf[64]; /* big enough to hold a number */
753 int r;
755 r = sprintf(buf, "%u\n", ftrace_profile_enabled);
756 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
759 static const struct file_operations ftrace_profile_fops = {
760 .open = tracing_open_generic,
761 .read = ftrace_profile_read,
762 .write = ftrace_profile_write,
765 /* used to initialize the real stat files */
766 static struct tracer_stat function_stats __initdata = {
767 .name = "functions",
768 .stat_start = function_stat_start,
769 .stat_next = function_stat_next,
770 .stat_cmp = function_stat_cmp,
771 .stat_headers = function_stat_headers,
772 .stat_show = function_stat_show
775 static __init void ftrace_profile_debugfs(struct dentry *d_tracer)
777 struct ftrace_profile_stat *stat;
778 struct dentry *entry;
779 char *name;
780 int ret;
781 int cpu;
783 for_each_possible_cpu(cpu) {
784 stat = &per_cpu(ftrace_profile_stats, cpu);
786 /* allocate enough for function name + cpu number */
787 name = kmalloc(32, GFP_KERNEL);
788 if (!name) {
790 * The files created are permanent, if something happens
791 * we still do not free memory.
793 WARN(1,
794 "Could not allocate stat file for cpu %d\n",
795 cpu);
796 return;
798 stat->stat = function_stats;
799 snprintf(name, 32, "function%d", cpu);
800 stat->stat.name = name;
801 ret = register_stat_tracer(&stat->stat);
802 if (ret) {
803 WARN(1,
804 "Could not register function stat for cpu %d\n",
805 cpu);
806 kfree(name);
807 return;
811 entry = debugfs_create_file("function_profile_enabled", 0644,
812 d_tracer, NULL, &ftrace_profile_fops);
813 if (!entry)
814 pr_warning("Could not create debugfs "
815 "'function_profile_enabled' entry\n");
818 #else /* CONFIG_FUNCTION_PROFILER */
819 static __init void ftrace_profile_debugfs(struct dentry *d_tracer)
822 #endif /* CONFIG_FUNCTION_PROFILER */
824 /* set when tracing only a pid */
825 struct pid *ftrace_pid_trace;
826 static struct pid * const ftrace_swapper_pid = &init_struct_pid;
828 #ifdef CONFIG_DYNAMIC_FTRACE
830 #ifndef CONFIG_FTRACE_MCOUNT_RECORD
831 # error Dynamic ftrace depends on MCOUNT_RECORD
832 #endif
834 static struct hlist_head ftrace_func_hash[FTRACE_FUNC_HASHSIZE] __read_mostly;
836 struct ftrace_func_probe {
837 struct hlist_node node;
838 struct ftrace_probe_ops *ops;
839 unsigned long flags;
840 unsigned long ip;
841 void *data;
842 struct rcu_head rcu;
845 enum {
846 FTRACE_ENABLE_CALLS = (1 << 0),
847 FTRACE_DISABLE_CALLS = (1 << 1),
848 FTRACE_UPDATE_TRACE_FUNC = (1 << 2),
849 FTRACE_ENABLE_MCOUNT = (1 << 3),
850 FTRACE_DISABLE_MCOUNT = (1 << 4),
851 FTRACE_START_FUNC_RET = (1 << 5),
852 FTRACE_STOP_FUNC_RET = (1 << 6),
855 static int ftrace_filtered;
857 static struct dyn_ftrace *ftrace_new_addrs;
859 static DEFINE_MUTEX(ftrace_regex_lock);
861 struct ftrace_page {
862 struct ftrace_page *next;
863 int index;
864 struct dyn_ftrace records[];
867 #define ENTRIES_PER_PAGE \
868 ((PAGE_SIZE - sizeof(struct ftrace_page)) / sizeof(struct dyn_ftrace))
870 /* estimate from running different kernels */
871 #define NR_TO_INIT 10000
873 static struct ftrace_page *ftrace_pages_start;
874 static struct ftrace_page *ftrace_pages;
876 static struct dyn_ftrace *ftrace_free_records;
879 * This is a double for. Do not use 'break' to break out of the loop,
880 * you must use a goto.
882 #define do_for_each_ftrace_rec(pg, rec) \
883 for (pg = ftrace_pages_start; pg; pg = pg->next) { \
884 int _____i; \
885 for (_____i = 0; _____i < pg->index; _____i++) { \
886 rec = &pg->records[_____i];
888 #define while_for_each_ftrace_rec() \
892 #ifdef CONFIG_KPROBES
894 static int frozen_record_count;
896 static inline void freeze_record(struct dyn_ftrace *rec)
898 if (!(rec->flags & FTRACE_FL_FROZEN)) {
899 rec->flags |= FTRACE_FL_FROZEN;
900 frozen_record_count++;
904 static inline void unfreeze_record(struct dyn_ftrace *rec)
906 if (rec->flags & FTRACE_FL_FROZEN) {
907 rec->flags &= ~FTRACE_FL_FROZEN;
908 frozen_record_count--;
912 static inline int record_frozen(struct dyn_ftrace *rec)
914 return rec->flags & FTRACE_FL_FROZEN;
916 #else
917 # define freeze_record(rec) ({ 0; })
918 # define unfreeze_record(rec) ({ 0; })
919 # define record_frozen(rec) ({ 0; })
920 #endif /* CONFIG_KPROBES */
922 static void ftrace_free_rec(struct dyn_ftrace *rec)
924 rec->freelist = ftrace_free_records;
925 ftrace_free_records = rec;
926 rec->flags |= FTRACE_FL_FREE;
929 static struct dyn_ftrace *ftrace_alloc_dyn_node(unsigned long ip)
931 struct dyn_ftrace *rec;
933 /* First check for freed records */
934 if (ftrace_free_records) {
935 rec = ftrace_free_records;
937 if (unlikely(!(rec->flags & FTRACE_FL_FREE))) {
938 FTRACE_WARN_ON_ONCE(1);
939 ftrace_free_records = NULL;
940 return NULL;
943 ftrace_free_records = rec->freelist;
944 memset(rec, 0, sizeof(*rec));
945 return rec;
948 if (ftrace_pages->index == ENTRIES_PER_PAGE) {
949 if (!ftrace_pages->next) {
950 /* allocate another page */
951 ftrace_pages->next =
952 (void *)get_zeroed_page(GFP_KERNEL);
953 if (!ftrace_pages->next)
954 return NULL;
956 ftrace_pages = ftrace_pages->next;
959 return &ftrace_pages->records[ftrace_pages->index++];
962 static struct dyn_ftrace *
963 ftrace_record_ip(unsigned long ip)
965 struct dyn_ftrace *rec;
967 if (ftrace_disabled)
968 return NULL;
970 rec = ftrace_alloc_dyn_node(ip);
971 if (!rec)
972 return NULL;
974 rec->ip = ip;
975 rec->newlist = ftrace_new_addrs;
976 ftrace_new_addrs = rec;
978 return rec;
981 static void print_ip_ins(const char *fmt, unsigned char *p)
983 int i;
985 printk(KERN_CONT "%s", fmt);
987 for (i = 0; i < MCOUNT_INSN_SIZE; i++)
988 printk(KERN_CONT "%s%02x", i ? ":" : "", p[i]);
991 static void ftrace_bug(int failed, unsigned long ip)
993 switch (failed) {
994 case -EFAULT:
995 FTRACE_WARN_ON_ONCE(1);
996 pr_info("ftrace faulted on modifying ");
997 print_ip_sym(ip);
998 break;
999 case -EINVAL:
1000 FTRACE_WARN_ON_ONCE(1);
1001 pr_info("ftrace failed to modify ");
1002 print_ip_sym(ip);
1003 print_ip_ins(" actual: ", (unsigned char *)ip);
1004 printk(KERN_CONT "\n");
1005 break;
1006 case -EPERM:
1007 FTRACE_WARN_ON_ONCE(1);
1008 pr_info("ftrace faulted on writing ");
1009 print_ip_sym(ip);
1010 break;
1011 default:
1012 FTRACE_WARN_ON_ONCE(1);
1013 pr_info("ftrace faulted on unknown error ");
1014 print_ip_sym(ip);
1019 static int
1020 __ftrace_replace_code(struct dyn_ftrace *rec, int enable)
1022 unsigned long ftrace_addr;
1023 unsigned long flag = 0UL;
1025 ftrace_addr = (unsigned long)FTRACE_ADDR;
1028 * If this record is not to be traced or we want to disable it,
1029 * then disable it.
1031 * If we want to enable it and filtering is off, then enable it.
1033 * If we want to enable it and filtering is on, enable it only if
1034 * it's filtered
1036 if (enable && !(rec->flags & FTRACE_FL_NOTRACE)) {
1037 if (!ftrace_filtered || (rec->flags & FTRACE_FL_FILTER))
1038 flag = FTRACE_FL_ENABLED;
1041 /* If the state of this record hasn't changed, then do nothing */
1042 if ((rec->flags & FTRACE_FL_ENABLED) == flag)
1043 return 0;
1045 if (flag) {
1046 rec->flags |= FTRACE_FL_ENABLED;
1047 return ftrace_make_call(rec, ftrace_addr);
1050 rec->flags &= ~FTRACE_FL_ENABLED;
1051 return ftrace_make_nop(NULL, rec, ftrace_addr);
1054 static void ftrace_replace_code(int enable)
1056 struct dyn_ftrace *rec;
1057 struct ftrace_page *pg;
1058 int failed;
1060 do_for_each_ftrace_rec(pg, rec) {
1062 * Skip over free records, records that have
1063 * failed and not converted.
1065 if (rec->flags & FTRACE_FL_FREE ||
1066 rec->flags & FTRACE_FL_FAILED ||
1067 !(rec->flags & FTRACE_FL_CONVERTED))
1068 continue;
1070 /* ignore updates to this record's mcount site */
1071 if (get_kprobe((void *)rec->ip)) {
1072 freeze_record(rec);
1073 continue;
1074 } else {
1075 unfreeze_record(rec);
1078 failed = __ftrace_replace_code(rec, enable);
1079 if (failed) {
1080 rec->flags |= FTRACE_FL_FAILED;
1081 ftrace_bug(failed, rec->ip);
1082 /* Stop processing */
1083 return;
1085 } while_for_each_ftrace_rec();
1088 static int
1089 ftrace_code_disable(struct module *mod, struct dyn_ftrace *rec)
1091 unsigned long ip;
1092 int ret;
1094 ip = rec->ip;
1096 ret = ftrace_make_nop(mod, rec, MCOUNT_ADDR);
1097 if (ret) {
1098 ftrace_bug(ret, ip);
1099 rec->flags |= FTRACE_FL_FAILED;
1100 return 0;
1102 return 1;
1106 * archs can override this function if they must do something
1107 * before the modifying code is performed.
1109 int __weak ftrace_arch_code_modify_prepare(void)
1111 return 0;
1115 * archs can override this function if they must do something
1116 * after the modifying code is performed.
1118 int __weak ftrace_arch_code_modify_post_process(void)
1120 return 0;
1123 static int __ftrace_modify_code(void *data)
1125 int *command = data;
1127 if (*command & FTRACE_ENABLE_CALLS)
1128 ftrace_replace_code(1);
1129 else if (*command & FTRACE_DISABLE_CALLS)
1130 ftrace_replace_code(0);
1132 if (*command & FTRACE_UPDATE_TRACE_FUNC)
1133 ftrace_update_ftrace_func(ftrace_trace_function);
1135 if (*command & FTRACE_START_FUNC_RET)
1136 ftrace_enable_ftrace_graph_caller();
1137 else if (*command & FTRACE_STOP_FUNC_RET)
1138 ftrace_disable_ftrace_graph_caller();
1140 return 0;
1143 static void ftrace_run_update_code(int command)
1145 int ret;
1147 ret = ftrace_arch_code_modify_prepare();
1148 FTRACE_WARN_ON(ret);
1149 if (ret)
1150 return;
1152 stop_machine(__ftrace_modify_code, &command, NULL);
1154 ret = ftrace_arch_code_modify_post_process();
1155 FTRACE_WARN_ON(ret);
1158 static ftrace_func_t saved_ftrace_func;
1159 static int ftrace_start_up;
1161 static void ftrace_startup_enable(int command)
1163 if (saved_ftrace_func != ftrace_trace_function) {
1164 saved_ftrace_func = ftrace_trace_function;
1165 command |= FTRACE_UPDATE_TRACE_FUNC;
1168 if (!command || !ftrace_enabled)
1169 return;
1171 ftrace_run_update_code(command);
1174 static void ftrace_startup(int command)
1176 if (unlikely(ftrace_disabled))
1177 return;
1179 ftrace_start_up++;
1180 command |= FTRACE_ENABLE_CALLS;
1182 ftrace_startup_enable(command);
1185 static void ftrace_shutdown(int command)
1187 if (unlikely(ftrace_disabled))
1188 return;
1190 ftrace_start_up--;
1192 * Just warn in case of unbalance, no need to kill ftrace, it's not
1193 * critical but the ftrace_call callers may be never nopped again after
1194 * further ftrace uses.
1196 WARN_ON_ONCE(ftrace_start_up < 0);
1198 if (!ftrace_start_up)
1199 command |= FTRACE_DISABLE_CALLS;
1201 if (saved_ftrace_func != ftrace_trace_function) {
1202 saved_ftrace_func = ftrace_trace_function;
1203 command |= FTRACE_UPDATE_TRACE_FUNC;
1206 if (!command || !ftrace_enabled)
1207 return;
1209 ftrace_run_update_code(command);
1212 static void ftrace_startup_sysctl(void)
1214 int command = FTRACE_ENABLE_MCOUNT;
1216 if (unlikely(ftrace_disabled))
1217 return;
1219 /* Force update next time */
1220 saved_ftrace_func = NULL;
1221 /* ftrace_start_up is true if we want ftrace running */
1222 if (ftrace_start_up)
1223 command |= FTRACE_ENABLE_CALLS;
1225 ftrace_run_update_code(command);
1228 static void ftrace_shutdown_sysctl(void)
1230 int command = FTRACE_DISABLE_MCOUNT;
1232 if (unlikely(ftrace_disabled))
1233 return;
1235 /* ftrace_start_up is true if ftrace is running */
1236 if (ftrace_start_up)
1237 command |= FTRACE_DISABLE_CALLS;
1239 ftrace_run_update_code(command);
1242 static cycle_t ftrace_update_time;
1243 static unsigned long ftrace_update_cnt;
1244 unsigned long ftrace_update_tot_cnt;
1246 static int ftrace_update_code(struct module *mod)
1248 struct dyn_ftrace *p;
1249 cycle_t start, stop;
1251 start = ftrace_now(raw_smp_processor_id());
1252 ftrace_update_cnt = 0;
1254 while (ftrace_new_addrs) {
1256 /* If something went wrong, bail without enabling anything */
1257 if (unlikely(ftrace_disabled))
1258 return -1;
1260 p = ftrace_new_addrs;
1261 ftrace_new_addrs = p->newlist;
1262 p->flags = 0L;
1264 /* convert record (i.e, patch mcount-call with NOP) */
1265 if (ftrace_code_disable(mod, p)) {
1266 p->flags |= FTRACE_FL_CONVERTED;
1267 ftrace_update_cnt++;
1268 } else
1269 ftrace_free_rec(p);
1272 stop = ftrace_now(raw_smp_processor_id());
1273 ftrace_update_time = stop - start;
1274 ftrace_update_tot_cnt += ftrace_update_cnt;
1276 return 0;
1279 static int __init ftrace_dyn_table_alloc(unsigned long num_to_init)
1281 struct ftrace_page *pg;
1282 int cnt;
1283 int i;
1285 /* allocate a few pages */
1286 ftrace_pages_start = (void *)get_zeroed_page(GFP_KERNEL);
1287 if (!ftrace_pages_start)
1288 return -1;
1291 * Allocate a few more pages.
1293 * TODO: have some parser search vmlinux before
1294 * final linking to find all calls to ftrace.
1295 * Then we can:
1296 * a) know how many pages to allocate.
1297 * and/or
1298 * b) set up the table then.
1300 * The dynamic code is still necessary for
1301 * modules.
1304 pg = ftrace_pages = ftrace_pages_start;
1306 cnt = num_to_init / ENTRIES_PER_PAGE;
1307 pr_info("ftrace: allocating %ld entries in %d pages\n",
1308 num_to_init, cnt + 1);
1310 for (i = 0; i < cnt; i++) {
1311 pg->next = (void *)get_zeroed_page(GFP_KERNEL);
1313 /* If we fail, we'll try later anyway */
1314 if (!pg->next)
1315 break;
1317 pg = pg->next;
1320 return 0;
1323 enum {
1324 FTRACE_ITER_FILTER = (1 << 0),
1325 FTRACE_ITER_NOTRACE = (1 << 1),
1326 FTRACE_ITER_FAILURES = (1 << 2),
1327 FTRACE_ITER_PRINTALL = (1 << 3),
1328 FTRACE_ITER_HASH = (1 << 4),
1331 #define FTRACE_BUFF_MAX (KSYM_SYMBOL_LEN+4) /* room for wildcards */
1333 struct ftrace_iterator {
1334 struct ftrace_page *pg;
1335 int hidx;
1336 int idx;
1337 unsigned flags;
1338 struct trace_parser parser;
1341 static void *
1342 t_hash_next(struct seq_file *m, void *v, loff_t *pos)
1344 struct ftrace_iterator *iter = m->private;
1345 struct hlist_node *hnd = v;
1346 struct hlist_head *hhd;
1348 WARN_ON(!(iter->flags & FTRACE_ITER_HASH));
1350 (*pos)++;
1352 retry:
1353 if (iter->hidx >= FTRACE_FUNC_HASHSIZE)
1354 return NULL;
1356 hhd = &ftrace_func_hash[iter->hidx];
1358 if (hlist_empty(hhd)) {
1359 iter->hidx++;
1360 hnd = NULL;
1361 goto retry;
1364 if (!hnd)
1365 hnd = hhd->first;
1366 else {
1367 hnd = hnd->next;
1368 if (!hnd) {
1369 iter->hidx++;
1370 goto retry;
1374 return hnd;
1377 static void *t_hash_start(struct seq_file *m, loff_t *pos)
1379 struct ftrace_iterator *iter = m->private;
1380 void *p = NULL;
1381 loff_t l;
1383 if (!(iter->flags & FTRACE_ITER_HASH))
1384 *pos = 0;
1386 iter->flags |= FTRACE_ITER_HASH;
1388 iter->hidx = 0;
1389 for (l = 0; l <= *pos; ) {
1390 p = t_hash_next(m, p, &l);
1391 if (!p)
1392 break;
1394 return p;
1397 static int t_hash_show(struct seq_file *m, void *v)
1399 struct ftrace_func_probe *rec;
1400 struct hlist_node *hnd = v;
1402 rec = hlist_entry(hnd, struct ftrace_func_probe, node);
1404 if (rec->ops->print)
1405 return rec->ops->print(m, rec->ip, rec->ops, rec->data);
1407 seq_printf(m, "%ps:%ps", (void *)rec->ip, (void *)rec->ops->func);
1409 if (rec->data)
1410 seq_printf(m, ":%p", rec->data);
1411 seq_putc(m, '\n');
1413 return 0;
1416 static void *
1417 t_next(struct seq_file *m, void *v, loff_t *pos)
1419 struct ftrace_iterator *iter = m->private;
1420 struct dyn_ftrace *rec = NULL;
1422 if (iter->flags & FTRACE_ITER_HASH)
1423 return t_hash_next(m, v, pos);
1425 (*pos)++;
1427 if (iter->flags & FTRACE_ITER_PRINTALL)
1428 return NULL;
1430 retry:
1431 if (iter->idx >= iter->pg->index) {
1432 if (iter->pg->next) {
1433 iter->pg = iter->pg->next;
1434 iter->idx = 0;
1435 goto retry;
1437 } else {
1438 rec = &iter->pg->records[iter->idx++];
1439 if ((rec->flags & FTRACE_FL_FREE) ||
1441 (!(iter->flags & FTRACE_ITER_FAILURES) &&
1442 (rec->flags & FTRACE_FL_FAILED)) ||
1444 ((iter->flags & FTRACE_ITER_FAILURES) &&
1445 !(rec->flags & FTRACE_FL_FAILED)) ||
1447 ((iter->flags & FTRACE_ITER_FILTER) &&
1448 !(rec->flags & FTRACE_FL_FILTER)) ||
1450 ((iter->flags & FTRACE_ITER_NOTRACE) &&
1451 !(rec->flags & FTRACE_FL_NOTRACE))) {
1452 rec = NULL;
1453 goto retry;
1457 return rec;
1460 static void *t_start(struct seq_file *m, loff_t *pos)
1462 struct ftrace_iterator *iter = m->private;
1463 void *p = NULL;
1464 loff_t l;
1466 mutex_lock(&ftrace_lock);
1468 * For set_ftrace_filter reading, if we have the filter
1469 * off, we can short cut and just print out that all
1470 * functions are enabled.
1472 if (iter->flags & FTRACE_ITER_FILTER && !ftrace_filtered) {
1473 if (*pos > 0)
1474 return t_hash_start(m, pos);
1475 iter->flags |= FTRACE_ITER_PRINTALL;
1476 return iter;
1479 if (iter->flags & FTRACE_ITER_HASH)
1480 return t_hash_start(m, pos);
1482 iter->pg = ftrace_pages_start;
1483 iter->idx = 0;
1484 for (l = 0; l <= *pos; ) {
1485 p = t_next(m, p, &l);
1486 if (!p)
1487 break;
1490 if (!p && iter->flags & FTRACE_ITER_FILTER)
1491 return t_hash_start(m, pos);
1493 return p;
1496 static void t_stop(struct seq_file *m, void *p)
1498 mutex_unlock(&ftrace_lock);
1501 static int t_show(struct seq_file *m, void *v)
1503 struct ftrace_iterator *iter = m->private;
1504 struct dyn_ftrace *rec = v;
1506 if (iter->flags & FTRACE_ITER_HASH)
1507 return t_hash_show(m, v);
1509 if (iter->flags & FTRACE_ITER_PRINTALL) {
1510 seq_printf(m, "#### all functions enabled ####\n");
1511 return 0;
1514 if (!rec)
1515 return 0;
1517 seq_printf(m, "%ps\n", (void *)rec->ip);
1519 return 0;
1522 static const struct seq_operations show_ftrace_seq_ops = {
1523 .start = t_start,
1524 .next = t_next,
1525 .stop = t_stop,
1526 .show = t_show,
1529 static int
1530 ftrace_avail_open(struct inode *inode, struct file *file)
1532 struct ftrace_iterator *iter;
1533 int ret;
1535 if (unlikely(ftrace_disabled))
1536 return -ENODEV;
1538 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
1539 if (!iter)
1540 return -ENOMEM;
1542 iter->pg = ftrace_pages_start;
1544 ret = seq_open(file, &show_ftrace_seq_ops);
1545 if (!ret) {
1546 struct seq_file *m = file->private_data;
1548 m->private = iter;
1549 } else {
1550 kfree(iter);
1553 return ret;
1556 static int
1557 ftrace_failures_open(struct inode *inode, struct file *file)
1559 int ret;
1560 struct seq_file *m;
1561 struct ftrace_iterator *iter;
1563 ret = ftrace_avail_open(inode, file);
1564 if (!ret) {
1565 m = (struct seq_file *)file->private_data;
1566 iter = (struct ftrace_iterator *)m->private;
1567 iter->flags = FTRACE_ITER_FAILURES;
1570 return ret;
1574 static void ftrace_filter_reset(int enable)
1576 struct ftrace_page *pg;
1577 struct dyn_ftrace *rec;
1578 unsigned long type = enable ? FTRACE_FL_FILTER : FTRACE_FL_NOTRACE;
1580 mutex_lock(&ftrace_lock);
1581 if (enable)
1582 ftrace_filtered = 0;
1583 do_for_each_ftrace_rec(pg, rec) {
1584 if (rec->flags & FTRACE_FL_FAILED)
1585 continue;
1586 rec->flags &= ~type;
1587 } while_for_each_ftrace_rec();
1588 mutex_unlock(&ftrace_lock);
1591 static int
1592 ftrace_regex_open(struct inode *inode, struct file *file, int enable)
1594 struct ftrace_iterator *iter;
1595 int ret = 0;
1597 if (unlikely(ftrace_disabled))
1598 return -ENODEV;
1600 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
1601 if (!iter)
1602 return -ENOMEM;
1604 if (trace_parser_get_init(&iter->parser, FTRACE_BUFF_MAX)) {
1605 kfree(iter);
1606 return -ENOMEM;
1609 mutex_lock(&ftrace_regex_lock);
1610 if ((file->f_mode & FMODE_WRITE) &&
1611 (file->f_flags & O_TRUNC))
1612 ftrace_filter_reset(enable);
1614 if (file->f_mode & FMODE_READ) {
1615 iter->pg = ftrace_pages_start;
1616 iter->flags = enable ? FTRACE_ITER_FILTER :
1617 FTRACE_ITER_NOTRACE;
1619 ret = seq_open(file, &show_ftrace_seq_ops);
1620 if (!ret) {
1621 struct seq_file *m = file->private_data;
1622 m->private = iter;
1623 } else {
1624 trace_parser_put(&iter->parser);
1625 kfree(iter);
1627 } else
1628 file->private_data = iter;
1629 mutex_unlock(&ftrace_regex_lock);
1631 return ret;
1634 static int
1635 ftrace_filter_open(struct inode *inode, struct file *file)
1637 return ftrace_regex_open(inode, file, 1);
1640 static int
1641 ftrace_notrace_open(struct inode *inode, struct file *file)
1643 return ftrace_regex_open(inode, file, 0);
1646 static loff_t
1647 ftrace_regex_lseek(struct file *file, loff_t offset, int origin)
1649 loff_t ret;
1651 if (file->f_mode & FMODE_READ)
1652 ret = seq_lseek(file, offset, origin);
1653 else
1654 file->f_pos = ret = 1;
1656 return ret;
1659 static int ftrace_match(char *str, char *regex, int len, int type)
1661 int matched = 0;
1662 char *ptr;
1664 switch (type) {
1665 case MATCH_FULL:
1666 if (strcmp(str, regex) == 0)
1667 matched = 1;
1668 break;
1669 case MATCH_FRONT_ONLY:
1670 if (strncmp(str, regex, len) == 0)
1671 matched = 1;
1672 break;
1673 case MATCH_MIDDLE_ONLY:
1674 if (strstr(str, regex))
1675 matched = 1;
1676 break;
1677 case MATCH_END_ONLY:
1678 ptr = strstr(str, regex);
1679 if (ptr && (ptr[len] == 0))
1680 matched = 1;
1681 break;
1684 return matched;
1687 static int
1688 ftrace_match_record(struct dyn_ftrace *rec, char *regex, int len, int type)
1690 char str[KSYM_SYMBOL_LEN];
1692 kallsyms_lookup(rec->ip, NULL, NULL, NULL, str);
1693 return ftrace_match(str, regex, len, type);
1696 static void ftrace_match_records(char *buff, int len, int enable)
1698 unsigned int search_len;
1699 struct ftrace_page *pg;
1700 struct dyn_ftrace *rec;
1701 unsigned long flag;
1702 char *search;
1703 int type;
1704 int not;
1706 flag = enable ? FTRACE_FL_FILTER : FTRACE_FL_NOTRACE;
1707 type = filter_parse_regex(buff, len, &search, &not);
1709 search_len = strlen(search);
1711 mutex_lock(&ftrace_lock);
1712 do_for_each_ftrace_rec(pg, rec) {
1714 if (rec->flags & FTRACE_FL_FAILED)
1715 continue;
1717 if (ftrace_match_record(rec, search, search_len, type)) {
1718 if (not)
1719 rec->flags &= ~flag;
1720 else
1721 rec->flags |= flag;
1724 * Only enable filtering if we have a function that
1725 * is filtered on.
1727 if (enable && (rec->flags & FTRACE_FL_FILTER))
1728 ftrace_filtered = 1;
1729 } while_for_each_ftrace_rec();
1730 mutex_unlock(&ftrace_lock);
1733 static int
1734 ftrace_match_module_record(struct dyn_ftrace *rec, char *mod,
1735 char *regex, int len, int type)
1737 char str[KSYM_SYMBOL_LEN];
1738 char *modname;
1740 kallsyms_lookup(rec->ip, NULL, NULL, &modname, str);
1742 if (!modname || strcmp(modname, mod))
1743 return 0;
1745 /* blank search means to match all funcs in the mod */
1746 if (len)
1747 return ftrace_match(str, regex, len, type);
1748 else
1749 return 1;
1752 static void ftrace_match_module_records(char *buff, char *mod, int enable)
1754 unsigned search_len = 0;
1755 struct ftrace_page *pg;
1756 struct dyn_ftrace *rec;
1757 int type = MATCH_FULL;
1758 char *search = buff;
1759 unsigned long flag;
1760 int not = 0;
1762 flag = enable ? FTRACE_FL_FILTER : FTRACE_FL_NOTRACE;
1764 /* blank or '*' mean the same */
1765 if (strcmp(buff, "*") == 0)
1766 buff[0] = 0;
1768 /* handle the case of 'dont filter this module' */
1769 if (strcmp(buff, "!") == 0 || strcmp(buff, "!*") == 0) {
1770 buff[0] = 0;
1771 not = 1;
1774 if (strlen(buff)) {
1775 type = filter_parse_regex(buff, strlen(buff), &search, &not);
1776 search_len = strlen(search);
1779 mutex_lock(&ftrace_lock);
1780 do_for_each_ftrace_rec(pg, rec) {
1782 if (rec->flags & FTRACE_FL_FAILED)
1783 continue;
1785 if (ftrace_match_module_record(rec, mod,
1786 search, search_len, type)) {
1787 if (not)
1788 rec->flags &= ~flag;
1789 else
1790 rec->flags |= flag;
1792 if (enable && (rec->flags & FTRACE_FL_FILTER))
1793 ftrace_filtered = 1;
1795 } while_for_each_ftrace_rec();
1796 mutex_unlock(&ftrace_lock);
1800 * We register the module command as a template to show others how
1801 * to register the a command as well.
1804 static int
1805 ftrace_mod_callback(char *func, char *cmd, char *param, int enable)
1807 char *mod;
1810 * cmd == 'mod' because we only registered this func
1811 * for the 'mod' ftrace_func_command.
1812 * But if you register one func with multiple commands,
1813 * you can tell which command was used by the cmd
1814 * parameter.
1817 /* we must have a module name */
1818 if (!param)
1819 return -EINVAL;
1821 mod = strsep(&param, ":");
1822 if (!strlen(mod))
1823 return -EINVAL;
1825 ftrace_match_module_records(func, mod, enable);
1826 return 0;
1829 static struct ftrace_func_command ftrace_mod_cmd = {
1830 .name = "mod",
1831 .func = ftrace_mod_callback,
1834 static int __init ftrace_mod_cmd_init(void)
1836 return register_ftrace_command(&ftrace_mod_cmd);
1838 device_initcall(ftrace_mod_cmd_init);
1840 static void
1841 function_trace_probe_call(unsigned long ip, unsigned long parent_ip)
1843 struct ftrace_func_probe *entry;
1844 struct hlist_head *hhd;
1845 struct hlist_node *n;
1846 unsigned long key;
1847 int resched;
1849 key = hash_long(ip, FTRACE_HASH_BITS);
1851 hhd = &ftrace_func_hash[key];
1853 if (hlist_empty(hhd))
1854 return;
1857 * Disable preemption for these calls to prevent a RCU grace
1858 * period. This syncs the hash iteration and freeing of items
1859 * on the hash. rcu_read_lock is too dangerous here.
1861 resched = ftrace_preempt_disable();
1862 hlist_for_each_entry_rcu(entry, n, hhd, node) {
1863 if (entry->ip == ip)
1864 entry->ops->func(ip, parent_ip, &entry->data);
1866 ftrace_preempt_enable(resched);
1869 static struct ftrace_ops trace_probe_ops __read_mostly =
1871 .func = function_trace_probe_call,
1874 static int ftrace_probe_registered;
1876 static void __enable_ftrace_function_probe(void)
1878 int i;
1880 if (ftrace_probe_registered)
1881 return;
1883 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
1884 struct hlist_head *hhd = &ftrace_func_hash[i];
1885 if (hhd->first)
1886 break;
1888 /* Nothing registered? */
1889 if (i == FTRACE_FUNC_HASHSIZE)
1890 return;
1892 __register_ftrace_function(&trace_probe_ops);
1893 ftrace_startup(0);
1894 ftrace_probe_registered = 1;
1897 static void __disable_ftrace_function_probe(void)
1899 int i;
1901 if (!ftrace_probe_registered)
1902 return;
1904 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
1905 struct hlist_head *hhd = &ftrace_func_hash[i];
1906 if (hhd->first)
1907 return;
1910 /* no more funcs left */
1911 __unregister_ftrace_function(&trace_probe_ops);
1912 ftrace_shutdown(0);
1913 ftrace_probe_registered = 0;
1917 static void ftrace_free_entry_rcu(struct rcu_head *rhp)
1919 struct ftrace_func_probe *entry =
1920 container_of(rhp, struct ftrace_func_probe, rcu);
1922 if (entry->ops->free)
1923 entry->ops->free(&entry->data);
1924 kfree(entry);
1929 register_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
1930 void *data)
1932 struct ftrace_func_probe *entry;
1933 struct ftrace_page *pg;
1934 struct dyn_ftrace *rec;
1935 int type, len, not;
1936 unsigned long key;
1937 int count = 0;
1938 char *search;
1940 type = filter_parse_regex(glob, strlen(glob), &search, &not);
1941 len = strlen(search);
1943 /* we do not support '!' for function probes */
1944 if (WARN_ON(not))
1945 return -EINVAL;
1947 mutex_lock(&ftrace_lock);
1948 do_for_each_ftrace_rec(pg, rec) {
1950 if (rec->flags & FTRACE_FL_FAILED)
1951 continue;
1953 if (!ftrace_match_record(rec, search, len, type))
1954 continue;
1956 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
1957 if (!entry) {
1958 /* If we did not process any, then return error */
1959 if (!count)
1960 count = -ENOMEM;
1961 goto out_unlock;
1964 count++;
1966 entry->data = data;
1969 * The caller might want to do something special
1970 * for each function we find. We call the callback
1971 * to give the caller an opportunity to do so.
1973 if (ops->callback) {
1974 if (ops->callback(rec->ip, &entry->data) < 0) {
1975 /* caller does not like this func */
1976 kfree(entry);
1977 continue;
1981 entry->ops = ops;
1982 entry->ip = rec->ip;
1984 key = hash_long(entry->ip, FTRACE_HASH_BITS);
1985 hlist_add_head_rcu(&entry->node, &ftrace_func_hash[key]);
1987 } while_for_each_ftrace_rec();
1988 __enable_ftrace_function_probe();
1990 out_unlock:
1991 mutex_unlock(&ftrace_lock);
1993 return count;
1996 enum {
1997 PROBE_TEST_FUNC = 1,
1998 PROBE_TEST_DATA = 2
2001 static void
2002 __unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
2003 void *data, int flags)
2005 struct ftrace_func_probe *entry;
2006 struct hlist_node *n, *tmp;
2007 char str[KSYM_SYMBOL_LEN];
2008 int type = MATCH_FULL;
2009 int i, len = 0;
2010 char *search;
2012 if (glob && (strcmp(glob, "*") == 0 || !strlen(glob)))
2013 glob = NULL;
2014 else if (glob) {
2015 int not;
2017 type = filter_parse_regex(glob, strlen(glob), &search, &not);
2018 len = strlen(search);
2020 /* we do not support '!' for function probes */
2021 if (WARN_ON(not))
2022 return;
2025 mutex_lock(&ftrace_lock);
2026 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
2027 struct hlist_head *hhd = &ftrace_func_hash[i];
2029 hlist_for_each_entry_safe(entry, n, tmp, hhd, node) {
2031 /* break up if statements for readability */
2032 if ((flags & PROBE_TEST_FUNC) && entry->ops != ops)
2033 continue;
2035 if ((flags & PROBE_TEST_DATA) && entry->data != data)
2036 continue;
2038 /* do this last, since it is the most expensive */
2039 if (glob) {
2040 kallsyms_lookup(entry->ip, NULL, NULL,
2041 NULL, str);
2042 if (!ftrace_match(str, glob, len, type))
2043 continue;
2046 hlist_del(&entry->node);
2047 call_rcu(&entry->rcu, ftrace_free_entry_rcu);
2050 __disable_ftrace_function_probe();
2051 mutex_unlock(&ftrace_lock);
2054 void
2055 unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
2056 void *data)
2058 __unregister_ftrace_function_probe(glob, ops, data,
2059 PROBE_TEST_FUNC | PROBE_TEST_DATA);
2062 void
2063 unregister_ftrace_function_probe_func(char *glob, struct ftrace_probe_ops *ops)
2065 __unregister_ftrace_function_probe(glob, ops, NULL, PROBE_TEST_FUNC);
2068 void unregister_ftrace_function_probe_all(char *glob)
2070 __unregister_ftrace_function_probe(glob, NULL, NULL, 0);
2073 static LIST_HEAD(ftrace_commands);
2074 static DEFINE_MUTEX(ftrace_cmd_mutex);
2076 int register_ftrace_command(struct ftrace_func_command *cmd)
2078 struct ftrace_func_command *p;
2079 int ret = 0;
2081 mutex_lock(&ftrace_cmd_mutex);
2082 list_for_each_entry(p, &ftrace_commands, list) {
2083 if (strcmp(cmd->name, p->name) == 0) {
2084 ret = -EBUSY;
2085 goto out_unlock;
2088 list_add(&cmd->list, &ftrace_commands);
2089 out_unlock:
2090 mutex_unlock(&ftrace_cmd_mutex);
2092 return ret;
2095 int unregister_ftrace_command(struct ftrace_func_command *cmd)
2097 struct ftrace_func_command *p, *n;
2098 int ret = -ENODEV;
2100 mutex_lock(&ftrace_cmd_mutex);
2101 list_for_each_entry_safe(p, n, &ftrace_commands, list) {
2102 if (strcmp(cmd->name, p->name) == 0) {
2103 ret = 0;
2104 list_del_init(&p->list);
2105 goto out_unlock;
2108 out_unlock:
2109 mutex_unlock(&ftrace_cmd_mutex);
2111 return ret;
2114 static int ftrace_process_regex(char *buff, int len, int enable)
2116 char *func, *command, *next = buff;
2117 struct ftrace_func_command *p;
2118 int ret = -EINVAL;
2120 func = strsep(&next, ":");
2122 if (!next) {
2123 ftrace_match_records(func, len, enable);
2124 return 0;
2127 /* command found */
2129 command = strsep(&next, ":");
2131 mutex_lock(&ftrace_cmd_mutex);
2132 list_for_each_entry(p, &ftrace_commands, list) {
2133 if (strcmp(p->name, command) == 0) {
2134 ret = p->func(func, command, next, enable);
2135 goto out_unlock;
2138 out_unlock:
2139 mutex_unlock(&ftrace_cmd_mutex);
2141 return ret;
2144 static ssize_t
2145 ftrace_regex_write(struct file *file, const char __user *ubuf,
2146 size_t cnt, loff_t *ppos, int enable)
2148 struct ftrace_iterator *iter;
2149 struct trace_parser *parser;
2150 ssize_t ret, read;
2152 if (!cnt)
2153 return 0;
2155 mutex_lock(&ftrace_regex_lock);
2157 if (file->f_mode & FMODE_READ) {
2158 struct seq_file *m = file->private_data;
2159 iter = m->private;
2160 } else
2161 iter = file->private_data;
2163 parser = &iter->parser;
2164 read = trace_get_user(parser, ubuf, cnt, ppos);
2166 if (read >= 0 && trace_parser_loaded(parser) &&
2167 !trace_parser_cont(parser)) {
2168 ret = ftrace_process_regex(parser->buffer,
2169 parser->idx, enable);
2170 if (ret)
2171 goto out;
2173 trace_parser_clear(parser);
2176 ret = read;
2178 mutex_unlock(&ftrace_regex_lock);
2179 out:
2180 return ret;
2183 static ssize_t
2184 ftrace_filter_write(struct file *file, const char __user *ubuf,
2185 size_t cnt, loff_t *ppos)
2187 return ftrace_regex_write(file, ubuf, cnt, ppos, 1);
2190 static ssize_t
2191 ftrace_notrace_write(struct file *file, const char __user *ubuf,
2192 size_t cnt, loff_t *ppos)
2194 return ftrace_regex_write(file, ubuf, cnt, ppos, 0);
2197 static void
2198 ftrace_set_regex(unsigned char *buf, int len, int reset, int enable)
2200 if (unlikely(ftrace_disabled))
2201 return;
2203 mutex_lock(&ftrace_regex_lock);
2204 if (reset)
2205 ftrace_filter_reset(enable);
2206 if (buf)
2207 ftrace_match_records(buf, len, enable);
2208 mutex_unlock(&ftrace_regex_lock);
2212 * ftrace_set_filter - set a function to filter on in ftrace
2213 * @buf - the string that holds the function filter text.
2214 * @len - the length of the string.
2215 * @reset - non zero to reset all filters before applying this filter.
2217 * Filters denote which functions should be enabled when tracing is enabled.
2218 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
2220 void ftrace_set_filter(unsigned char *buf, int len, int reset)
2222 ftrace_set_regex(buf, len, reset, 1);
2226 * ftrace_set_notrace - set a function to not trace in ftrace
2227 * @buf - the string that holds the function notrace text.
2228 * @len - the length of the string.
2229 * @reset - non zero to reset all filters before applying this filter.
2231 * Notrace Filters denote which functions should not be enabled when tracing
2232 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
2233 * for tracing.
2235 void ftrace_set_notrace(unsigned char *buf, int len, int reset)
2237 ftrace_set_regex(buf, len, reset, 0);
2241 * command line interface to allow users to set filters on boot up.
2243 #define FTRACE_FILTER_SIZE COMMAND_LINE_SIZE
2244 static char ftrace_notrace_buf[FTRACE_FILTER_SIZE] __initdata;
2245 static char ftrace_filter_buf[FTRACE_FILTER_SIZE] __initdata;
2247 static int __init set_ftrace_notrace(char *str)
2249 strncpy(ftrace_notrace_buf, str, FTRACE_FILTER_SIZE);
2250 return 1;
2252 __setup("ftrace_notrace=", set_ftrace_notrace);
2254 static int __init set_ftrace_filter(char *str)
2256 strncpy(ftrace_filter_buf, str, FTRACE_FILTER_SIZE);
2257 return 1;
2259 __setup("ftrace_filter=", set_ftrace_filter);
2261 static void __init set_ftrace_early_filter(char *buf, int enable)
2263 char *func;
2265 while (buf) {
2266 func = strsep(&buf, ",");
2267 ftrace_set_regex(func, strlen(func), 0, enable);
2271 static void __init set_ftrace_early_filters(void)
2273 if (ftrace_filter_buf[0])
2274 set_ftrace_early_filter(ftrace_filter_buf, 1);
2275 if (ftrace_notrace_buf[0])
2276 set_ftrace_early_filter(ftrace_notrace_buf, 0);
2279 static int
2280 ftrace_regex_release(struct inode *inode, struct file *file, int enable)
2282 struct seq_file *m = (struct seq_file *)file->private_data;
2283 struct ftrace_iterator *iter;
2284 struct trace_parser *parser;
2286 mutex_lock(&ftrace_regex_lock);
2287 if (file->f_mode & FMODE_READ) {
2288 iter = m->private;
2290 seq_release(inode, file);
2291 } else
2292 iter = file->private_data;
2294 parser = &iter->parser;
2295 if (trace_parser_loaded(parser)) {
2296 parser->buffer[parser->idx] = 0;
2297 ftrace_match_records(parser->buffer, parser->idx, enable);
2300 mutex_lock(&ftrace_lock);
2301 if (ftrace_start_up && ftrace_enabled)
2302 ftrace_run_update_code(FTRACE_ENABLE_CALLS);
2303 mutex_unlock(&ftrace_lock);
2305 trace_parser_put(parser);
2306 kfree(iter);
2308 mutex_unlock(&ftrace_regex_lock);
2309 return 0;
2312 static int
2313 ftrace_filter_release(struct inode *inode, struct file *file)
2315 return ftrace_regex_release(inode, file, 1);
2318 static int
2319 ftrace_notrace_release(struct inode *inode, struct file *file)
2321 return ftrace_regex_release(inode, file, 0);
2324 static const struct file_operations ftrace_avail_fops = {
2325 .open = ftrace_avail_open,
2326 .read = seq_read,
2327 .llseek = seq_lseek,
2328 .release = seq_release_private,
2331 static const struct file_operations ftrace_failures_fops = {
2332 .open = ftrace_failures_open,
2333 .read = seq_read,
2334 .llseek = seq_lseek,
2335 .release = seq_release_private,
2338 static const struct file_operations ftrace_filter_fops = {
2339 .open = ftrace_filter_open,
2340 .read = seq_read,
2341 .write = ftrace_filter_write,
2342 .llseek = ftrace_regex_lseek,
2343 .release = ftrace_filter_release,
2346 static const struct file_operations ftrace_notrace_fops = {
2347 .open = ftrace_notrace_open,
2348 .read = seq_read,
2349 .write = ftrace_notrace_write,
2350 .llseek = ftrace_regex_lseek,
2351 .release = ftrace_notrace_release,
2354 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
2356 static DEFINE_MUTEX(graph_lock);
2358 int ftrace_graph_count;
2359 unsigned long ftrace_graph_funcs[FTRACE_GRAPH_MAX_FUNCS] __read_mostly;
2361 static void *
2362 __g_next(struct seq_file *m, loff_t *pos)
2364 if (*pos >= ftrace_graph_count)
2365 return NULL;
2366 return &ftrace_graph_funcs[*pos];
2369 static void *
2370 g_next(struct seq_file *m, void *v, loff_t *pos)
2372 (*pos)++;
2373 return __g_next(m, pos);
2376 static void *g_start(struct seq_file *m, loff_t *pos)
2378 mutex_lock(&graph_lock);
2380 /* Nothing, tell g_show to print all functions are enabled */
2381 if (!ftrace_graph_count && !*pos)
2382 return (void *)1;
2384 return __g_next(m, pos);
2387 static void g_stop(struct seq_file *m, void *p)
2389 mutex_unlock(&graph_lock);
2392 static int g_show(struct seq_file *m, void *v)
2394 unsigned long *ptr = v;
2396 if (!ptr)
2397 return 0;
2399 if (ptr == (unsigned long *)1) {
2400 seq_printf(m, "#### all functions enabled ####\n");
2401 return 0;
2404 seq_printf(m, "%ps\n", (void *)*ptr);
2406 return 0;
2409 static const struct seq_operations ftrace_graph_seq_ops = {
2410 .start = g_start,
2411 .next = g_next,
2412 .stop = g_stop,
2413 .show = g_show,
2416 static int
2417 ftrace_graph_open(struct inode *inode, struct file *file)
2419 int ret = 0;
2421 if (unlikely(ftrace_disabled))
2422 return -ENODEV;
2424 mutex_lock(&graph_lock);
2425 if ((file->f_mode & FMODE_WRITE) &&
2426 (file->f_flags & O_TRUNC)) {
2427 ftrace_graph_count = 0;
2428 memset(ftrace_graph_funcs, 0, sizeof(ftrace_graph_funcs));
2430 mutex_unlock(&graph_lock);
2432 if (file->f_mode & FMODE_READ)
2433 ret = seq_open(file, &ftrace_graph_seq_ops);
2435 return ret;
2438 static int
2439 ftrace_graph_release(struct inode *inode, struct file *file)
2441 if (file->f_mode & FMODE_READ)
2442 seq_release(inode, file);
2443 return 0;
2446 static int
2447 ftrace_set_func(unsigned long *array, int *idx, char *buffer)
2449 struct dyn_ftrace *rec;
2450 struct ftrace_page *pg;
2451 int search_len;
2452 int found = 0;
2453 int type, not;
2454 char *search;
2455 bool exists;
2456 int i;
2458 if (ftrace_disabled)
2459 return -ENODEV;
2461 /* decode regex */
2462 type = filter_parse_regex(buffer, strlen(buffer), &search, &not);
2463 if (not)
2464 return -EINVAL;
2466 search_len = strlen(search);
2468 mutex_lock(&ftrace_lock);
2469 do_for_each_ftrace_rec(pg, rec) {
2471 if (*idx >= FTRACE_GRAPH_MAX_FUNCS)
2472 break;
2474 if (rec->flags & (FTRACE_FL_FAILED | FTRACE_FL_FREE))
2475 continue;
2477 if (ftrace_match_record(rec, search, search_len, type)) {
2478 /* ensure it is not already in the array */
2479 exists = false;
2480 for (i = 0; i < *idx; i++)
2481 if (array[i] == rec->ip) {
2482 exists = true;
2483 break;
2485 if (!exists) {
2486 array[(*idx)++] = rec->ip;
2487 found = 1;
2490 } while_for_each_ftrace_rec();
2492 mutex_unlock(&ftrace_lock);
2494 return found ? 0 : -EINVAL;
2497 static ssize_t
2498 ftrace_graph_write(struct file *file, const char __user *ubuf,
2499 size_t cnt, loff_t *ppos)
2501 struct trace_parser parser;
2502 ssize_t read, ret;
2504 if (!cnt || cnt < 0)
2505 return 0;
2507 mutex_lock(&graph_lock);
2509 if (ftrace_graph_count >= FTRACE_GRAPH_MAX_FUNCS) {
2510 ret = -EBUSY;
2511 goto out_unlock;
2514 if (trace_parser_get_init(&parser, FTRACE_BUFF_MAX)) {
2515 ret = -ENOMEM;
2516 goto out_unlock;
2519 read = trace_get_user(&parser, ubuf, cnt, ppos);
2521 if (read >= 0 && trace_parser_loaded((&parser))) {
2522 parser.buffer[parser.idx] = 0;
2524 /* we allow only one expression at a time */
2525 ret = ftrace_set_func(ftrace_graph_funcs, &ftrace_graph_count,
2526 parser.buffer);
2527 if (ret)
2528 goto out_free;
2531 ret = read;
2533 out_free:
2534 trace_parser_put(&parser);
2535 out_unlock:
2536 mutex_unlock(&graph_lock);
2538 return ret;
2541 static const struct file_operations ftrace_graph_fops = {
2542 .open = ftrace_graph_open,
2543 .read = seq_read,
2544 .write = ftrace_graph_write,
2545 .release = ftrace_graph_release,
2547 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
2549 static __init int ftrace_init_dyn_debugfs(struct dentry *d_tracer)
2552 trace_create_file("available_filter_functions", 0444,
2553 d_tracer, NULL, &ftrace_avail_fops);
2555 trace_create_file("failures", 0444,
2556 d_tracer, NULL, &ftrace_failures_fops);
2558 trace_create_file("set_ftrace_filter", 0644, d_tracer,
2559 NULL, &ftrace_filter_fops);
2561 trace_create_file("set_ftrace_notrace", 0644, d_tracer,
2562 NULL, &ftrace_notrace_fops);
2564 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
2565 trace_create_file("set_graph_function", 0444, d_tracer,
2566 NULL,
2567 &ftrace_graph_fops);
2568 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
2570 return 0;
2573 static int ftrace_convert_nops(struct module *mod,
2574 unsigned long *start,
2575 unsigned long *end)
2577 unsigned long *p;
2578 unsigned long addr;
2579 unsigned long flags;
2581 mutex_lock(&ftrace_lock);
2582 p = start;
2583 while (p < end) {
2584 addr = ftrace_call_adjust(*p++);
2586 * Some architecture linkers will pad between
2587 * the different mcount_loc sections of different
2588 * object files to satisfy alignments.
2589 * Skip any NULL pointers.
2591 if (!addr)
2592 continue;
2593 ftrace_record_ip(addr);
2596 /* disable interrupts to prevent kstop machine */
2597 local_irq_save(flags);
2598 ftrace_update_code(mod);
2599 local_irq_restore(flags);
2600 mutex_unlock(&ftrace_lock);
2602 return 0;
2605 #ifdef CONFIG_MODULES
2606 void ftrace_release_mod(struct module *mod)
2608 struct dyn_ftrace *rec;
2609 struct ftrace_page *pg;
2611 if (ftrace_disabled)
2612 return;
2614 mutex_lock(&ftrace_lock);
2615 do_for_each_ftrace_rec(pg, rec) {
2616 if (within_module_core(rec->ip, mod)) {
2618 * rec->ip is changed in ftrace_free_rec()
2619 * It should not between s and e if record was freed.
2621 FTRACE_WARN_ON(rec->flags & FTRACE_FL_FREE);
2622 ftrace_free_rec(rec);
2624 } while_for_each_ftrace_rec();
2625 mutex_unlock(&ftrace_lock);
2628 static void ftrace_init_module(struct module *mod,
2629 unsigned long *start, unsigned long *end)
2631 if (ftrace_disabled || start == end)
2632 return;
2633 ftrace_convert_nops(mod, start, end);
2636 static int ftrace_module_notify(struct notifier_block *self,
2637 unsigned long val, void *data)
2639 struct module *mod = data;
2641 switch (val) {
2642 case MODULE_STATE_COMING:
2643 ftrace_init_module(mod, mod->ftrace_callsites,
2644 mod->ftrace_callsites +
2645 mod->num_ftrace_callsites);
2646 break;
2647 case MODULE_STATE_GOING:
2648 ftrace_release_mod(mod);
2649 break;
2652 return 0;
2654 #else
2655 static int ftrace_module_notify(struct notifier_block *self,
2656 unsigned long val, void *data)
2658 return 0;
2660 #endif /* CONFIG_MODULES */
2662 struct notifier_block ftrace_module_nb = {
2663 .notifier_call = ftrace_module_notify,
2664 .priority = 0,
2667 extern unsigned long __start_mcount_loc[];
2668 extern unsigned long __stop_mcount_loc[];
2670 void __init ftrace_init(void)
2672 unsigned long count, addr, flags;
2673 int ret;
2675 /* Keep the ftrace pointer to the stub */
2676 addr = (unsigned long)ftrace_stub;
2678 local_irq_save(flags);
2679 ftrace_dyn_arch_init(&addr);
2680 local_irq_restore(flags);
2682 /* ftrace_dyn_arch_init places the return code in addr */
2683 if (addr)
2684 goto failed;
2686 count = __stop_mcount_loc - __start_mcount_loc;
2688 ret = ftrace_dyn_table_alloc(count);
2689 if (ret)
2690 goto failed;
2692 last_ftrace_enabled = ftrace_enabled = 1;
2694 ret = ftrace_convert_nops(NULL,
2695 __start_mcount_loc,
2696 __stop_mcount_loc);
2698 ret = register_module_notifier(&ftrace_module_nb);
2699 if (ret)
2700 pr_warning("Failed to register trace ftrace module notifier\n");
2702 set_ftrace_early_filters();
2704 return;
2705 failed:
2706 ftrace_disabled = 1;
2709 #else
2711 static int __init ftrace_nodyn_init(void)
2713 ftrace_enabled = 1;
2714 return 0;
2716 device_initcall(ftrace_nodyn_init);
2718 static inline int ftrace_init_dyn_debugfs(struct dentry *d_tracer) { return 0; }
2719 static inline void ftrace_startup_enable(int command) { }
2720 /* Keep as macros so we do not need to define the commands */
2721 # define ftrace_startup(command) do { } while (0)
2722 # define ftrace_shutdown(command) do { } while (0)
2723 # define ftrace_startup_sysctl() do { } while (0)
2724 # define ftrace_shutdown_sysctl() do { } while (0)
2725 #endif /* CONFIG_DYNAMIC_FTRACE */
2727 static ssize_t
2728 ftrace_pid_read(struct file *file, char __user *ubuf,
2729 size_t cnt, loff_t *ppos)
2731 char buf[64];
2732 int r;
2734 if (ftrace_pid_trace == ftrace_swapper_pid)
2735 r = sprintf(buf, "swapper tasks\n");
2736 else if (ftrace_pid_trace)
2737 r = sprintf(buf, "%u\n", pid_vnr(ftrace_pid_trace));
2738 else
2739 r = sprintf(buf, "no pid\n");
2741 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2744 static void clear_ftrace_swapper(void)
2746 struct task_struct *p;
2747 int cpu;
2749 get_online_cpus();
2750 for_each_online_cpu(cpu) {
2751 p = idle_task(cpu);
2752 clear_tsk_trace_trace(p);
2754 put_online_cpus();
2757 static void set_ftrace_swapper(void)
2759 struct task_struct *p;
2760 int cpu;
2762 get_online_cpus();
2763 for_each_online_cpu(cpu) {
2764 p = idle_task(cpu);
2765 set_tsk_trace_trace(p);
2767 put_online_cpus();
2770 static void clear_ftrace_pid(struct pid *pid)
2772 struct task_struct *p;
2774 rcu_read_lock();
2775 do_each_pid_task(pid, PIDTYPE_PID, p) {
2776 clear_tsk_trace_trace(p);
2777 } while_each_pid_task(pid, PIDTYPE_PID, p);
2778 rcu_read_unlock();
2780 put_pid(pid);
2783 static void set_ftrace_pid(struct pid *pid)
2785 struct task_struct *p;
2787 rcu_read_lock();
2788 do_each_pid_task(pid, PIDTYPE_PID, p) {
2789 set_tsk_trace_trace(p);
2790 } while_each_pid_task(pid, PIDTYPE_PID, p);
2791 rcu_read_unlock();
2794 static void clear_ftrace_pid_task(struct pid **pid)
2796 if (*pid == ftrace_swapper_pid)
2797 clear_ftrace_swapper();
2798 else
2799 clear_ftrace_pid(*pid);
2801 *pid = NULL;
2804 static void set_ftrace_pid_task(struct pid *pid)
2806 if (pid == ftrace_swapper_pid)
2807 set_ftrace_swapper();
2808 else
2809 set_ftrace_pid(pid);
2812 static ssize_t
2813 ftrace_pid_write(struct file *filp, const char __user *ubuf,
2814 size_t cnt, loff_t *ppos)
2816 struct pid *pid;
2817 char buf[64];
2818 long val;
2819 int ret;
2821 if (cnt >= sizeof(buf))
2822 return -EINVAL;
2824 if (copy_from_user(&buf, ubuf, cnt))
2825 return -EFAULT;
2827 buf[cnt] = 0;
2829 ret = strict_strtol(buf, 10, &val);
2830 if (ret < 0)
2831 return ret;
2833 mutex_lock(&ftrace_lock);
2834 if (val < 0) {
2835 /* disable pid tracing */
2836 if (!ftrace_pid_trace)
2837 goto out;
2839 clear_ftrace_pid_task(&ftrace_pid_trace);
2841 } else {
2842 /* swapper task is special */
2843 if (!val) {
2844 pid = ftrace_swapper_pid;
2845 if (pid == ftrace_pid_trace)
2846 goto out;
2847 } else {
2848 pid = find_get_pid(val);
2850 if (pid == ftrace_pid_trace) {
2851 put_pid(pid);
2852 goto out;
2856 if (ftrace_pid_trace)
2857 clear_ftrace_pid_task(&ftrace_pid_trace);
2859 if (!pid)
2860 goto out;
2862 ftrace_pid_trace = pid;
2864 set_ftrace_pid_task(ftrace_pid_trace);
2867 /* update the function call */
2868 ftrace_update_pid_func();
2869 ftrace_startup_enable(0);
2871 out:
2872 mutex_unlock(&ftrace_lock);
2874 return cnt;
2877 static const struct file_operations ftrace_pid_fops = {
2878 .read = ftrace_pid_read,
2879 .write = ftrace_pid_write,
2882 static __init int ftrace_init_debugfs(void)
2884 struct dentry *d_tracer;
2886 d_tracer = tracing_init_dentry();
2887 if (!d_tracer)
2888 return 0;
2890 ftrace_init_dyn_debugfs(d_tracer);
2892 trace_create_file("set_ftrace_pid", 0644, d_tracer,
2893 NULL, &ftrace_pid_fops);
2895 ftrace_profile_debugfs(d_tracer);
2897 return 0;
2899 fs_initcall(ftrace_init_debugfs);
2902 * ftrace_kill - kill ftrace
2904 * This function should be used by panic code. It stops ftrace
2905 * but in a not so nice way. If you need to simply kill ftrace
2906 * from a non-atomic section, use ftrace_kill.
2908 void ftrace_kill(void)
2910 ftrace_disabled = 1;
2911 ftrace_enabled = 0;
2912 clear_ftrace_function();
2916 * register_ftrace_function - register a function for profiling
2917 * @ops - ops structure that holds the function for profiling.
2919 * Register a function to be called by all functions in the
2920 * kernel.
2922 * Note: @ops->func and all the functions it calls must be labeled
2923 * with "notrace", otherwise it will go into a
2924 * recursive loop.
2926 int register_ftrace_function(struct ftrace_ops *ops)
2928 int ret;
2930 if (unlikely(ftrace_disabled))
2931 return -1;
2933 mutex_lock(&ftrace_lock);
2935 ret = __register_ftrace_function(ops);
2936 ftrace_startup(0);
2938 mutex_unlock(&ftrace_lock);
2939 return ret;
2943 * unregister_ftrace_function - unregister a function for profiling.
2944 * @ops - ops structure that holds the function to unregister
2946 * Unregister a function that was added to be called by ftrace profiling.
2948 int unregister_ftrace_function(struct ftrace_ops *ops)
2950 int ret;
2952 mutex_lock(&ftrace_lock);
2953 ret = __unregister_ftrace_function(ops);
2954 ftrace_shutdown(0);
2955 mutex_unlock(&ftrace_lock);
2957 return ret;
2961 ftrace_enable_sysctl(struct ctl_table *table, int write,
2962 void __user *buffer, size_t *lenp,
2963 loff_t *ppos)
2965 int ret;
2967 if (unlikely(ftrace_disabled))
2968 return -ENODEV;
2970 mutex_lock(&ftrace_lock);
2972 ret = proc_dointvec(table, write, buffer, lenp, ppos);
2974 if (ret || !write || (last_ftrace_enabled == !!ftrace_enabled))
2975 goto out;
2977 last_ftrace_enabled = !!ftrace_enabled;
2979 if (ftrace_enabled) {
2981 ftrace_startup_sysctl();
2983 /* we are starting ftrace again */
2984 if (ftrace_list != &ftrace_list_end) {
2985 if (ftrace_list->next == &ftrace_list_end)
2986 ftrace_trace_function = ftrace_list->func;
2987 else
2988 ftrace_trace_function = ftrace_list_func;
2991 } else {
2992 /* stopping ftrace calls (just send to ftrace_stub) */
2993 ftrace_trace_function = ftrace_stub;
2995 ftrace_shutdown_sysctl();
2998 out:
2999 mutex_unlock(&ftrace_lock);
3000 return ret;
3003 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
3005 static int ftrace_graph_active;
3006 static struct notifier_block ftrace_suspend_notifier;
3008 int ftrace_graph_entry_stub(struct ftrace_graph_ent *trace)
3010 return 0;
3013 /* The callbacks that hook a function */
3014 trace_func_graph_ret_t ftrace_graph_return =
3015 (trace_func_graph_ret_t)ftrace_stub;
3016 trace_func_graph_ent_t ftrace_graph_entry = ftrace_graph_entry_stub;
3018 /* Try to assign a return stack array on FTRACE_RETSTACK_ALLOC_SIZE tasks. */
3019 static int alloc_retstack_tasklist(struct ftrace_ret_stack **ret_stack_list)
3021 int i;
3022 int ret = 0;
3023 unsigned long flags;
3024 int start = 0, end = FTRACE_RETSTACK_ALLOC_SIZE;
3025 struct task_struct *g, *t;
3027 for (i = 0; i < FTRACE_RETSTACK_ALLOC_SIZE; i++) {
3028 ret_stack_list[i] = kmalloc(FTRACE_RETFUNC_DEPTH
3029 * sizeof(struct ftrace_ret_stack),
3030 GFP_KERNEL);
3031 if (!ret_stack_list[i]) {
3032 start = 0;
3033 end = i;
3034 ret = -ENOMEM;
3035 goto free;
3039 read_lock_irqsave(&tasklist_lock, flags);
3040 do_each_thread(g, t) {
3041 if (start == end) {
3042 ret = -EAGAIN;
3043 goto unlock;
3046 if (t->ret_stack == NULL) {
3047 atomic_set(&t->tracing_graph_pause, 0);
3048 atomic_set(&t->trace_overrun, 0);
3049 t->curr_ret_stack = -1;
3050 /* Make sure the tasks see the -1 first: */
3051 smp_wmb();
3052 t->ret_stack = ret_stack_list[start++];
3054 } while_each_thread(g, t);
3056 unlock:
3057 read_unlock_irqrestore(&tasklist_lock, flags);
3058 free:
3059 for (i = start; i < end; i++)
3060 kfree(ret_stack_list[i]);
3061 return ret;
3064 static void
3065 ftrace_graph_probe_sched_switch(struct rq *__rq, struct task_struct *prev,
3066 struct task_struct *next)
3068 unsigned long long timestamp;
3069 int index;
3072 * Does the user want to count the time a function was asleep.
3073 * If so, do not update the time stamps.
3075 if (trace_flags & TRACE_ITER_SLEEP_TIME)
3076 return;
3078 timestamp = trace_clock_local();
3080 prev->ftrace_timestamp = timestamp;
3082 /* only process tasks that we timestamped */
3083 if (!next->ftrace_timestamp)
3084 return;
3087 * Update all the counters in next to make up for the
3088 * time next was sleeping.
3090 timestamp -= next->ftrace_timestamp;
3092 for (index = next->curr_ret_stack; index >= 0; index--)
3093 next->ret_stack[index].calltime += timestamp;
3096 /* Allocate a return stack for each task */
3097 static int start_graph_tracing(void)
3099 struct ftrace_ret_stack **ret_stack_list;
3100 int ret, cpu;
3102 ret_stack_list = kmalloc(FTRACE_RETSTACK_ALLOC_SIZE *
3103 sizeof(struct ftrace_ret_stack *),
3104 GFP_KERNEL);
3106 if (!ret_stack_list)
3107 return -ENOMEM;
3109 /* The cpu_boot init_task->ret_stack will never be freed */
3110 for_each_online_cpu(cpu) {
3111 if (!idle_task(cpu)->ret_stack)
3112 ftrace_graph_init_task(idle_task(cpu));
3115 do {
3116 ret = alloc_retstack_tasklist(ret_stack_list);
3117 } while (ret == -EAGAIN);
3119 if (!ret) {
3120 ret = register_trace_sched_switch(ftrace_graph_probe_sched_switch);
3121 if (ret)
3122 pr_info("ftrace_graph: Couldn't activate tracepoint"
3123 " probe to kernel_sched_switch\n");
3126 kfree(ret_stack_list);
3127 return ret;
3131 * Hibernation protection.
3132 * The state of the current task is too much unstable during
3133 * suspend/restore to disk. We want to protect against that.
3135 static int
3136 ftrace_suspend_notifier_call(struct notifier_block *bl, unsigned long state,
3137 void *unused)
3139 switch (state) {
3140 case PM_HIBERNATION_PREPARE:
3141 pause_graph_tracing();
3142 break;
3144 case PM_POST_HIBERNATION:
3145 unpause_graph_tracing();
3146 break;
3148 return NOTIFY_DONE;
3151 int register_ftrace_graph(trace_func_graph_ret_t retfunc,
3152 trace_func_graph_ent_t entryfunc)
3154 int ret = 0;
3156 mutex_lock(&ftrace_lock);
3158 /* we currently allow only one tracer registered at a time */
3159 if (ftrace_graph_active) {
3160 ret = -EBUSY;
3161 goto out;
3164 ftrace_suspend_notifier.notifier_call = ftrace_suspend_notifier_call;
3165 register_pm_notifier(&ftrace_suspend_notifier);
3167 ftrace_graph_active++;
3168 ret = start_graph_tracing();
3169 if (ret) {
3170 ftrace_graph_active--;
3171 goto out;
3174 ftrace_graph_return = retfunc;
3175 ftrace_graph_entry = entryfunc;
3177 ftrace_startup(FTRACE_START_FUNC_RET);
3179 out:
3180 mutex_unlock(&ftrace_lock);
3181 return ret;
3184 void unregister_ftrace_graph(void)
3186 mutex_lock(&ftrace_lock);
3188 if (unlikely(!ftrace_graph_active))
3189 goto out;
3191 ftrace_graph_active--;
3192 unregister_trace_sched_switch(ftrace_graph_probe_sched_switch);
3193 ftrace_graph_return = (trace_func_graph_ret_t)ftrace_stub;
3194 ftrace_graph_entry = ftrace_graph_entry_stub;
3195 ftrace_shutdown(FTRACE_STOP_FUNC_RET);
3196 unregister_pm_notifier(&ftrace_suspend_notifier);
3198 out:
3199 mutex_unlock(&ftrace_lock);
3202 /* Allocate a return stack for newly created task */
3203 void ftrace_graph_init_task(struct task_struct *t)
3205 /* Make sure we do not use the parent ret_stack */
3206 t->ret_stack = NULL;
3208 if (ftrace_graph_active) {
3209 struct ftrace_ret_stack *ret_stack;
3211 ret_stack = kmalloc(FTRACE_RETFUNC_DEPTH
3212 * sizeof(struct ftrace_ret_stack),
3213 GFP_KERNEL);
3214 if (!ret_stack)
3215 return;
3216 t->curr_ret_stack = -1;
3217 atomic_set(&t->tracing_graph_pause, 0);
3218 atomic_set(&t->trace_overrun, 0);
3219 t->ftrace_timestamp = 0;
3220 /* make curr_ret_stack visable before we add the ret_stack */
3221 smp_wmb();
3222 t->ret_stack = ret_stack;
3226 void ftrace_graph_exit_task(struct task_struct *t)
3228 struct ftrace_ret_stack *ret_stack = t->ret_stack;
3230 t->ret_stack = NULL;
3231 /* NULL must become visible to IRQs before we free it: */
3232 barrier();
3234 kfree(ret_stack);
3237 void ftrace_graph_stop(void)
3239 ftrace_stop();
3241 #endif