Merge branch 'tracing/core' of git://git.kernel.org/pub/scm/linux/kernel/git/frederic...
[linux-2.6/x86.git] / kernel / trace / ftrace.c
blob45c965919cffe2685ad88fb7de0fa5d2b3756860
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 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
82 static int ftrace_set_func(unsigned long *array, int *idx, char *buffer);
83 #endif
85 static void ftrace_list_func(unsigned long ip, unsigned long parent_ip)
87 struct ftrace_ops *op = ftrace_list;
89 /* in case someone actually ports this to alpha! */
90 read_barrier_depends();
92 while (op != &ftrace_list_end) {
93 /* silly alpha */
94 read_barrier_depends();
95 op->func(ip, parent_ip);
96 op = op->next;
100 static void ftrace_pid_func(unsigned long ip, unsigned long parent_ip)
102 if (!test_tsk_trace_trace(current))
103 return;
105 ftrace_pid_function(ip, parent_ip);
108 static void set_ftrace_pid_function(ftrace_func_t func)
110 /* do not set ftrace_pid_function to itself! */
111 if (func != ftrace_pid_func)
112 ftrace_pid_function = func;
116 * clear_ftrace_function - reset the ftrace function
118 * This NULLs the ftrace function and in essence stops
119 * tracing. There may be lag
121 void clear_ftrace_function(void)
123 ftrace_trace_function = ftrace_stub;
124 __ftrace_trace_function = ftrace_stub;
125 ftrace_pid_function = ftrace_stub;
128 #ifndef CONFIG_HAVE_FUNCTION_TRACE_MCOUNT_TEST
130 * For those archs that do not test ftrace_trace_stop in their
131 * mcount call site, we need to do it from C.
133 static void ftrace_test_stop_func(unsigned long ip, unsigned long parent_ip)
135 if (function_trace_stop)
136 return;
138 __ftrace_trace_function(ip, parent_ip);
140 #endif
142 static int __register_ftrace_function(struct ftrace_ops *ops)
144 ops->next = ftrace_list;
146 * We are entering ops into the ftrace_list but another
147 * CPU might be walking that list. We need to make sure
148 * the ops->next pointer is valid before another CPU sees
149 * the ops pointer included into the ftrace_list.
151 smp_wmb();
152 ftrace_list = ops;
154 if (ftrace_enabled) {
155 ftrace_func_t func;
157 if (ops->next == &ftrace_list_end)
158 func = ops->func;
159 else
160 func = ftrace_list_func;
162 if (ftrace_pid_trace) {
163 set_ftrace_pid_function(func);
164 func = ftrace_pid_func;
168 * For one func, simply call it directly.
169 * For more than one func, call the chain.
171 #ifdef CONFIG_HAVE_FUNCTION_TRACE_MCOUNT_TEST
172 ftrace_trace_function = func;
173 #else
174 __ftrace_trace_function = func;
175 ftrace_trace_function = ftrace_test_stop_func;
176 #endif
179 return 0;
182 static int __unregister_ftrace_function(struct ftrace_ops *ops)
184 struct ftrace_ops **p;
187 * If we are removing the last function, then simply point
188 * to the ftrace_stub.
190 if (ftrace_list == ops && ops->next == &ftrace_list_end) {
191 ftrace_trace_function = ftrace_stub;
192 ftrace_list = &ftrace_list_end;
193 return 0;
196 for (p = &ftrace_list; *p != &ftrace_list_end; p = &(*p)->next)
197 if (*p == ops)
198 break;
200 if (*p != ops)
201 return -1;
203 *p = (*p)->next;
205 if (ftrace_enabled) {
206 /* If we only have one func left, then call that directly */
207 if (ftrace_list->next == &ftrace_list_end) {
208 ftrace_func_t func = ftrace_list->func;
210 if (ftrace_pid_trace) {
211 set_ftrace_pid_function(func);
212 func = ftrace_pid_func;
214 #ifdef CONFIG_HAVE_FUNCTION_TRACE_MCOUNT_TEST
215 ftrace_trace_function = func;
216 #else
217 __ftrace_trace_function = func;
218 #endif
222 return 0;
225 static void ftrace_update_pid_func(void)
227 ftrace_func_t func;
229 if (ftrace_trace_function == ftrace_stub)
230 return;
232 #ifdef CONFIG_HAVE_FUNCTION_TRACE_MCOUNT_TEST
233 func = ftrace_trace_function;
234 #else
235 func = __ftrace_trace_function;
236 #endif
238 if (ftrace_pid_trace) {
239 set_ftrace_pid_function(func);
240 func = ftrace_pid_func;
241 } else {
242 if (func == ftrace_pid_func)
243 func = ftrace_pid_function;
246 #ifdef CONFIG_HAVE_FUNCTION_TRACE_MCOUNT_TEST
247 ftrace_trace_function = func;
248 #else
249 __ftrace_trace_function = func;
250 #endif
253 #ifdef CONFIG_FUNCTION_PROFILER
254 struct ftrace_profile {
255 struct hlist_node node;
256 unsigned long ip;
257 unsigned long counter;
258 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
259 unsigned long long time;
260 #endif
263 struct ftrace_profile_page {
264 struct ftrace_profile_page *next;
265 unsigned long index;
266 struct ftrace_profile records[];
269 struct ftrace_profile_stat {
270 atomic_t disabled;
271 struct hlist_head *hash;
272 struct ftrace_profile_page *pages;
273 struct ftrace_profile_page *start;
274 struct tracer_stat stat;
277 #define PROFILE_RECORDS_SIZE \
278 (PAGE_SIZE - offsetof(struct ftrace_profile_page, records))
280 #define PROFILES_PER_PAGE \
281 (PROFILE_RECORDS_SIZE / sizeof(struct ftrace_profile))
283 static int ftrace_profile_bits __read_mostly;
284 static int ftrace_profile_enabled __read_mostly;
286 /* ftrace_profile_lock - synchronize the enable and disable of the profiler */
287 static DEFINE_MUTEX(ftrace_profile_lock);
289 static DEFINE_PER_CPU(struct ftrace_profile_stat, ftrace_profile_stats);
291 #define FTRACE_PROFILE_HASH_SIZE 1024 /* must be power of 2 */
293 static void *
294 function_stat_next(void *v, int idx)
296 struct ftrace_profile *rec = v;
297 struct ftrace_profile_page *pg;
299 pg = (struct ftrace_profile_page *)((unsigned long)rec & PAGE_MASK);
301 again:
302 if (idx != 0)
303 rec++;
305 if ((void *)rec >= (void *)&pg->records[pg->index]) {
306 pg = pg->next;
307 if (!pg)
308 return NULL;
309 rec = &pg->records[0];
310 if (!rec->counter)
311 goto again;
314 return rec;
317 static void *function_stat_start(struct tracer_stat *trace)
319 struct ftrace_profile_stat *stat =
320 container_of(trace, struct ftrace_profile_stat, stat);
322 if (!stat || !stat->start)
323 return NULL;
325 return function_stat_next(&stat->start->records[0], 0);
328 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
329 /* function graph compares on total time */
330 static int function_stat_cmp(void *p1, void *p2)
332 struct ftrace_profile *a = p1;
333 struct ftrace_profile *b = p2;
335 if (a->time < b->time)
336 return -1;
337 if (a->time > b->time)
338 return 1;
339 else
340 return 0;
342 #else
343 /* not function graph compares against hits */
344 static int function_stat_cmp(void *p1, void *p2)
346 struct ftrace_profile *a = p1;
347 struct ftrace_profile *b = p2;
349 if (a->counter < b->counter)
350 return -1;
351 if (a->counter > b->counter)
352 return 1;
353 else
354 return 0;
356 #endif
358 static int function_stat_headers(struct seq_file *m)
360 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
361 seq_printf(m, " Function "
362 "Hit Time Avg\n"
363 " -------- "
364 "--- ---- ---\n");
365 #else
366 seq_printf(m, " Function Hit\n"
367 " -------- ---\n");
368 #endif
369 return 0;
372 static int function_stat_show(struct seq_file *m, void *v)
374 struct ftrace_profile *rec = v;
375 char str[KSYM_SYMBOL_LEN];
376 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
377 static DEFINE_MUTEX(mutex);
378 static struct trace_seq s;
379 unsigned long long avg;
380 #endif
382 kallsyms_lookup(rec->ip, NULL, NULL, NULL, str);
383 seq_printf(m, " %-30.30s %10lu", str, rec->counter);
385 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
386 seq_printf(m, " ");
387 avg = rec->time;
388 do_div(avg, rec->counter);
390 mutex_lock(&mutex);
391 trace_seq_init(&s);
392 trace_print_graph_duration(rec->time, &s);
393 trace_seq_puts(&s, " ");
394 trace_print_graph_duration(avg, &s);
395 trace_print_seq(m, &s);
396 mutex_unlock(&mutex);
397 #endif
398 seq_putc(m, '\n');
400 return 0;
403 static void ftrace_profile_reset(struct ftrace_profile_stat *stat)
405 struct ftrace_profile_page *pg;
407 pg = stat->pages = stat->start;
409 while (pg) {
410 memset(pg->records, 0, PROFILE_RECORDS_SIZE);
411 pg->index = 0;
412 pg = pg->next;
415 memset(stat->hash, 0,
416 FTRACE_PROFILE_HASH_SIZE * sizeof(struct hlist_head));
419 int ftrace_profile_pages_init(struct ftrace_profile_stat *stat)
421 struct ftrace_profile_page *pg;
422 int functions;
423 int pages;
424 int i;
426 /* If we already allocated, do nothing */
427 if (stat->pages)
428 return 0;
430 stat->pages = (void *)get_zeroed_page(GFP_KERNEL);
431 if (!stat->pages)
432 return -ENOMEM;
434 #ifdef CONFIG_DYNAMIC_FTRACE
435 functions = ftrace_update_tot_cnt;
436 #else
438 * We do not know the number of functions that exist because
439 * dynamic tracing is what counts them. With past experience
440 * we have around 20K functions. That should be more than enough.
441 * It is highly unlikely we will execute every function in
442 * the kernel.
444 functions = 20000;
445 #endif
447 pg = stat->start = stat->pages;
449 pages = DIV_ROUND_UP(functions, PROFILES_PER_PAGE);
451 for (i = 0; i < pages; i++) {
452 pg->next = (void *)get_zeroed_page(GFP_KERNEL);
453 if (!pg->next)
454 goto out_free;
455 pg = pg->next;
458 return 0;
460 out_free:
461 pg = stat->start;
462 while (pg) {
463 unsigned long tmp = (unsigned long)pg;
465 pg = pg->next;
466 free_page(tmp);
469 free_page((unsigned long)stat->pages);
470 stat->pages = NULL;
471 stat->start = NULL;
473 return -ENOMEM;
476 static int ftrace_profile_init_cpu(int cpu)
478 struct ftrace_profile_stat *stat;
479 int size;
481 stat = &per_cpu(ftrace_profile_stats, cpu);
483 if (stat->hash) {
484 /* If the profile is already created, simply reset it */
485 ftrace_profile_reset(stat);
486 return 0;
490 * We are profiling all functions, but usually only a few thousand
491 * functions are hit. We'll make a hash of 1024 items.
493 size = FTRACE_PROFILE_HASH_SIZE;
495 stat->hash = kzalloc(sizeof(struct hlist_head) * size, GFP_KERNEL);
497 if (!stat->hash)
498 return -ENOMEM;
500 if (!ftrace_profile_bits) {
501 size--;
503 for (; size; size >>= 1)
504 ftrace_profile_bits++;
507 /* Preallocate the function profiling pages */
508 if (ftrace_profile_pages_init(stat) < 0) {
509 kfree(stat->hash);
510 stat->hash = NULL;
511 return -ENOMEM;
514 return 0;
517 static int ftrace_profile_init(void)
519 int cpu;
520 int ret = 0;
522 for_each_online_cpu(cpu) {
523 ret = ftrace_profile_init_cpu(cpu);
524 if (ret)
525 break;
528 return ret;
531 /* interrupts must be disabled */
532 static struct ftrace_profile *
533 ftrace_find_profiled_func(struct ftrace_profile_stat *stat, unsigned long ip)
535 struct ftrace_profile *rec;
536 struct hlist_head *hhd;
537 struct hlist_node *n;
538 unsigned long key;
540 key = hash_long(ip, ftrace_profile_bits);
541 hhd = &stat->hash[key];
543 if (hlist_empty(hhd))
544 return NULL;
546 hlist_for_each_entry_rcu(rec, n, hhd, node) {
547 if (rec->ip == ip)
548 return rec;
551 return NULL;
554 static void ftrace_add_profile(struct ftrace_profile_stat *stat,
555 struct ftrace_profile *rec)
557 unsigned long key;
559 key = hash_long(rec->ip, ftrace_profile_bits);
560 hlist_add_head_rcu(&rec->node, &stat->hash[key]);
564 * The memory is already allocated, this simply finds a new record to use.
566 static struct ftrace_profile *
567 ftrace_profile_alloc(struct ftrace_profile_stat *stat, unsigned long ip)
569 struct ftrace_profile *rec = NULL;
571 /* prevent recursion (from NMIs) */
572 if (atomic_inc_return(&stat->disabled) != 1)
573 goto out;
576 * Try to find the function again since an NMI
577 * could have added it
579 rec = ftrace_find_profiled_func(stat, ip);
580 if (rec)
581 goto out;
583 if (stat->pages->index == PROFILES_PER_PAGE) {
584 if (!stat->pages->next)
585 goto out;
586 stat->pages = stat->pages->next;
589 rec = &stat->pages->records[stat->pages->index++];
590 rec->ip = ip;
591 ftrace_add_profile(stat, rec);
593 out:
594 atomic_dec(&stat->disabled);
596 return rec;
599 static void
600 function_profile_call(unsigned long ip, unsigned long parent_ip)
602 struct ftrace_profile_stat *stat;
603 struct ftrace_profile *rec;
604 unsigned long flags;
606 if (!ftrace_profile_enabled)
607 return;
609 local_irq_save(flags);
611 stat = &__get_cpu_var(ftrace_profile_stats);
612 if (!stat->hash || !ftrace_profile_enabled)
613 goto out;
615 rec = ftrace_find_profiled_func(stat, ip);
616 if (!rec) {
617 rec = ftrace_profile_alloc(stat, ip);
618 if (!rec)
619 goto out;
622 rec->counter++;
623 out:
624 local_irq_restore(flags);
627 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
628 static int profile_graph_entry(struct ftrace_graph_ent *trace)
630 function_profile_call(trace->func, 0);
631 return 1;
634 static void profile_graph_return(struct ftrace_graph_ret *trace)
636 struct ftrace_profile_stat *stat;
637 unsigned long long calltime;
638 struct ftrace_profile *rec;
639 unsigned long flags;
641 local_irq_save(flags);
642 stat = &__get_cpu_var(ftrace_profile_stats);
643 if (!stat->hash || !ftrace_profile_enabled)
644 goto out;
646 calltime = trace->rettime - trace->calltime;
648 if (!(trace_flags & TRACE_ITER_GRAPH_TIME)) {
649 int index;
651 index = trace->depth;
653 /* Append this call time to the parent time to subtract */
654 if (index)
655 current->ret_stack[index - 1].subtime += calltime;
657 if (current->ret_stack[index].subtime < calltime)
658 calltime -= current->ret_stack[index].subtime;
659 else
660 calltime = 0;
663 rec = ftrace_find_profiled_func(stat, trace->func);
664 if (rec)
665 rec->time += calltime;
667 out:
668 local_irq_restore(flags);
671 static int register_ftrace_profiler(void)
673 return register_ftrace_graph(&profile_graph_return,
674 &profile_graph_entry);
677 static void unregister_ftrace_profiler(void)
679 unregister_ftrace_graph();
681 #else
682 static struct ftrace_ops ftrace_profile_ops __read_mostly =
684 .func = function_profile_call,
687 static int register_ftrace_profiler(void)
689 return register_ftrace_function(&ftrace_profile_ops);
692 static void unregister_ftrace_profiler(void)
694 unregister_ftrace_function(&ftrace_profile_ops);
696 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
698 static ssize_t
699 ftrace_profile_write(struct file *filp, const char __user *ubuf,
700 size_t cnt, loff_t *ppos)
702 unsigned long val;
703 char buf[64]; /* big enough to hold a number */
704 int ret;
706 if (cnt >= sizeof(buf))
707 return -EINVAL;
709 if (copy_from_user(&buf, ubuf, cnt))
710 return -EFAULT;
712 buf[cnt] = 0;
714 ret = strict_strtoul(buf, 10, &val);
715 if (ret < 0)
716 return ret;
718 val = !!val;
720 mutex_lock(&ftrace_profile_lock);
721 if (ftrace_profile_enabled ^ val) {
722 if (val) {
723 ret = ftrace_profile_init();
724 if (ret < 0) {
725 cnt = ret;
726 goto out;
729 ret = register_ftrace_profiler();
730 if (ret < 0) {
731 cnt = ret;
732 goto out;
734 ftrace_profile_enabled = 1;
735 } else {
736 ftrace_profile_enabled = 0;
738 * unregister_ftrace_profiler calls stop_machine
739 * so this acts like an synchronize_sched.
741 unregister_ftrace_profiler();
744 out:
745 mutex_unlock(&ftrace_profile_lock);
747 filp->f_pos += cnt;
749 return cnt;
752 static ssize_t
753 ftrace_profile_read(struct file *filp, char __user *ubuf,
754 size_t cnt, loff_t *ppos)
756 char buf[64]; /* big enough to hold a number */
757 int r;
759 r = sprintf(buf, "%u\n", ftrace_profile_enabled);
760 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
763 static const struct file_operations ftrace_profile_fops = {
764 .open = tracing_open_generic,
765 .read = ftrace_profile_read,
766 .write = ftrace_profile_write,
769 /* used to initialize the real stat files */
770 static struct tracer_stat function_stats __initdata = {
771 .name = "functions",
772 .stat_start = function_stat_start,
773 .stat_next = function_stat_next,
774 .stat_cmp = function_stat_cmp,
775 .stat_headers = function_stat_headers,
776 .stat_show = function_stat_show
779 static __init void ftrace_profile_debugfs(struct dentry *d_tracer)
781 struct ftrace_profile_stat *stat;
782 struct dentry *entry;
783 char *name;
784 int ret;
785 int cpu;
787 for_each_possible_cpu(cpu) {
788 stat = &per_cpu(ftrace_profile_stats, cpu);
790 /* allocate enough for function name + cpu number */
791 name = kmalloc(32, GFP_KERNEL);
792 if (!name) {
794 * The files created are permanent, if something happens
795 * we still do not free memory.
797 WARN(1,
798 "Could not allocate stat file for cpu %d\n",
799 cpu);
800 return;
802 stat->stat = function_stats;
803 snprintf(name, 32, "function%d", cpu);
804 stat->stat.name = name;
805 ret = register_stat_tracer(&stat->stat);
806 if (ret) {
807 WARN(1,
808 "Could not register function stat for cpu %d\n",
809 cpu);
810 kfree(name);
811 return;
815 entry = debugfs_create_file("function_profile_enabled", 0644,
816 d_tracer, NULL, &ftrace_profile_fops);
817 if (!entry)
818 pr_warning("Could not create debugfs "
819 "'function_profile_enabled' entry\n");
822 #else /* CONFIG_FUNCTION_PROFILER */
823 static __init void ftrace_profile_debugfs(struct dentry *d_tracer)
826 #endif /* CONFIG_FUNCTION_PROFILER */
828 /* set when tracing only a pid */
829 struct pid *ftrace_pid_trace;
830 static struct pid * const ftrace_swapper_pid = &init_struct_pid;
832 #ifdef CONFIG_DYNAMIC_FTRACE
834 #ifndef CONFIG_FTRACE_MCOUNT_RECORD
835 # error Dynamic ftrace depends on MCOUNT_RECORD
836 #endif
838 static struct hlist_head ftrace_func_hash[FTRACE_FUNC_HASHSIZE] __read_mostly;
840 struct ftrace_func_probe {
841 struct hlist_node node;
842 struct ftrace_probe_ops *ops;
843 unsigned long flags;
844 unsigned long ip;
845 void *data;
846 struct rcu_head rcu;
849 enum {
850 FTRACE_ENABLE_CALLS = (1 << 0),
851 FTRACE_DISABLE_CALLS = (1 << 1),
852 FTRACE_UPDATE_TRACE_FUNC = (1 << 2),
853 FTRACE_ENABLE_MCOUNT = (1 << 3),
854 FTRACE_DISABLE_MCOUNT = (1 << 4),
855 FTRACE_START_FUNC_RET = (1 << 5),
856 FTRACE_STOP_FUNC_RET = (1 << 6),
859 static int ftrace_filtered;
861 static struct dyn_ftrace *ftrace_new_addrs;
863 static DEFINE_MUTEX(ftrace_regex_lock);
865 struct ftrace_page {
866 struct ftrace_page *next;
867 int index;
868 struct dyn_ftrace records[];
871 #define ENTRIES_PER_PAGE \
872 ((PAGE_SIZE - sizeof(struct ftrace_page)) / sizeof(struct dyn_ftrace))
874 /* estimate from running different kernels */
875 #define NR_TO_INIT 10000
877 static struct ftrace_page *ftrace_pages_start;
878 static struct ftrace_page *ftrace_pages;
880 static struct dyn_ftrace *ftrace_free_records;
883 * This is a double for. Do not use 'break' to break out of the loop,
884 * you must use a goto.
886 #define do_for_each_ftrace_rec(pg, rec) \
887 for (pg = ftrace_pages_start; pg; pg = pg->next) { \
888 int _____i; \
889 for (_____i = 0; _____i < pg->index; _____i++) { \
890 rec = &pg->records[_____i];
892 #define while_for_each_ftrace_rec() \
896 #ifdef CONFIG_KPROBES
898 static int frozen_record_count;
900 static inline void freeze_record(struct dyn_ftrace *rec)
902 if (!(rec->flags & FTRACE_FL_FROZEN)) {
903 rec->flags |= FTRACE_FL_FROZEN;
904 frozen_record_count++;
908 static inline void unfreeze_record(struct dyn_ftrace *rec)
910 if (rec->flags & FTRACE_FL_FROZEN) {
911 rec->flags &= ~FTRACE_FL_FROZEN;
912 frozen_record_count--;
916 static inline int record_frozen(struct dyn_ftrace *rec)
918 return rec->flags & FTRACE_FL_FROZEN;
920 #else
921 # define freeze_record(rec) ({ 0; })
922 # define unfreeze_record(rec) ({ 0; })
923 # define record_frozen(rec) ({ 0; })
924 #endif /* CONFIG_KPROBES */
926 static void ftrace_free_rec(struct dyn_ftrace *rec)
928 rec->freelist = ftrace_free_records;
929 ftrace_free_records = rec;
930 rec->flags |= FTRACE_FL_FREE;
933 static struct dyn_ftrace *ftrace_alloc_dyn_node(unsigned long ip)
935 struct dyn_ftrace *rec;
937 /* First check for freed records */
938 if (ftrace_free_records) {
939 rec = ftrace_free_records;
941 if (unlikely(!(rec->flags & FTRACE_FL_FREE))) {
942 FTRACE_WARN_ON_ONCE(1);
943 ftrace_free_records = NULL;
944 return NULL;
947 ftrace_free_records = rec->freelist;
948 memset(rec, 0, sizeof(*rec));
949 return rec;
952 if (ftrace_pages->index == ENTRIES_PER_PAGE) {
953 if (!ftrace_pages->next) {
954 /* allocate another page */
955 ftrace_pages->next =
956 (void *)get_zeroed_page(GFP_KERNEL);
957 if (!ftrace_pages->next)
958 return NULL;
960 ftrace_pages = ftrace_pages->next;
963 return &ftrace_pages->records[ftrace_pages->index++];
966 static struct dyn_ftrace *
967 ftrace_record_ip(unsigned long ip)
969 struct dyn_ftrace *rec;
971 if (ftrace_disabled)
972 return NULL;
974 rec = ftrace_alloc_dyn_node(ip);
975 if (!rec)
976 return NULL;
978 rec->ip = ip;
979 rec->newlist = ftrace_new_addrs;
980 ftrace_new_addrs = rec;
982 return rec;
985 static void print_ip_ins(const char *fmt, unsigned char *p)
987 int i;
989 printk(KERN_CONT "%s", fmt);
991 for (i = 0; i < MCOUNT_INSN_SIZE; i++)
992 printk(KERN_CONT "%s%02x", i ? ":" : "", p[i]);
995 static void ftrace_bug(int failed, unsigned long ip)
997 switch (failed) {
998 case -EFAULT:
999 FTRACE_WARN_ON_ONCE(1);
1000 pr_info("ftrace faulted on modifying ");
1001 print_ip_sym(ip);
1002 break;
1003 case -EINVAL:
1004 FTRACE_WARN_ON_ONCE(1);
1005 pr_info("ftrace failed to modify ");
1006 print_ip_sym(ip);
1007 print_ip_ins(" actual: ", (unsigned char *)ip);
1008 printk(KERN_CONT "\n");
1009 break;
1010 case -EPERM:
1011 FTRACE_WARN_ON_ONCE(1);
1012 pr_info("ftrace faulted on writing ");
1013 print_ip_sym(ip);
1014 break;
1015 default:
1016 FTRACE_WARN_ON_ONCE(1);
1017 pr_info("ftrace faulted on unknown error ");
1018 print_ip_sym(ip);
1023 static int
1024 __ftrace_replace_code(struct dyn_ftrace *rec, int enable)
1026 unsigned long ftrace_addr;
1027 unsigned long flag = 0UL;
1029 ftrace_addr = (unsigned long)FTRACE_ADDR;
1032 * If this record is not to be traced or we want to disable it,
1033 * then disable it.
1035 * If we want to enable it and filtering is off, then enable it.
1037 * If we want to enable it and filtering is on, enable it only if
1038 * it's filtered
1040 if (enable && !(rec->flags & FTRACE_FL_NOTRACE)) {
1041 if (!ftrace_filtered || (rec->flags & FTRACE_FL_FILTER))
1042 flag = FTRACE_FL_ENABLED;
1045 /* If the state of this record hasn't changed, then do nothing */
1046 if ((rec->flags & FTRACE_FL_ENABLED) == flag)
1047 return 0;
1049 if (flag) {
1050 rec->flags |= FTRACE_FL_ENABLED;
1051 return ftrace_make_call(rec, ftrace_addr);
1054 rec->flags &= ~FTRACE_FL_ENABLED;
1055 return ftrace_make_nop(NULL, rec, ftrace_addr);
1058 static void ftrace_replace_code(int enable)
1060 struct dyn_ftrace *rec;
1061 struct ftrace_page *pg;
1062 int failed;
1064 do_for_each_ftrace_rec(pg, rec) {
1066 * Skip over free records, records that have
1067 * failed and not converted.
1069 if (rec->flags & FTRACE_FL_FREE ||
1070 rec->flags & FTRACE_FL_FAILED ||
1071 !(rec->flags & FTRACE_FL_CONVERTED))
1072 continue;
1074 /* ignore updates to this record's mcount site */
1075 if (get_kprobe((void *)rec->ip)) {
1076 freeze_record(rec);
1077 continue;
1078 } else {
1079 unfreeze_record(rec);
1082 failed = __ftrace_replace_code(rec, enable);
1083 if (failed) {
1084 rec->flags |= FTRACE_FL_FAILED;
1085 ftrace_bug(failed, rec->ip);
1086 /* Stop processing */
1087 return;
1089 } while_for_each_ftrace_rec();
1092 static int
1093 ftrace_code_disable(struct module *mod, struct dyn_ftrace *rec)
1095 unsigned long ip;
1096 int ret;
1098 ip = rec->ip;
1100 ret = ftrace_make_nop(mod, rec, MCOUNT_ADDR);
1101 if (ret) {
1102 ftrace_bug(ret, ip);
1103 rec->flags |= FTRACE_FL_FAILED;
1104 return 0;
1106 return 1;
1110 * archs can override this function if they must do something
1111 * before the modifying code is performed.
1113 int __weak ftrace_arch_code_modify_prepare(void)
1115 return 0;
1119 * archs can override this function if they must do something
1120 * after the modifying code is performed.
1122 int __weak ftrace_arch_code_modify_post_process(void)
1124 return 0;
1127 static int __ftrace_modify_code(void *data)
1129 int *command = data;
1131 if (*command & FTRACE_ENABLE_CALLS)
1132 ftrace_replace_code(1);
1133 else if (*command & FTRACE_DISABLE_CALLS)
1134 ftrace_replace_code(0);
1136 if (*command & FTRACE_UPDATE_TRACE_FUNC)
1137 ftrace_update_ftrace_func(ftrace_trace_function);
1139 if (*command & FTRACE_START_FUNC_RET)
1140 ftrace_enable_ftrace_graph_caller();
1141 else if (*command & FTRACE_STOP_FUNC_RET)
1142 ftrace_disable_ftrace_graph_caller();
1144 return 0;
1147 static void ftrace_run_update_code(int command)
1149 int ret;
1151 ret = ftrace_arch_code_modify_prepare();
1152 FTRACE_WARN_ON(ret);
1153 if (ret)
1154 return;
1156 stop_machine(__ftrace_modify_code, &command, NULL);
1158 ret = ftrace_arch_code_modify_post_process();
1159 FTRACE_WARN_ON(ret);
1162 static ftrace_func_t saved_ftrace_func;
1163 static int ftrace_start_up;
1165 static void ftrace_startup_enable(int command)
1167 if (saved_ftrace_func != ftrace_trace_function) {
1168 saved_ftrace_func = ftrace_trace_function;
1169 command |= FTRACE_UPDATE_TRACE_FUNC;
1172 if (!command || !ftrace_enabled)
1173 return;
1175 ftrace_run_update_code(command);
1178 static void ftrace_startup(int command)
1180 if (unlikely(ftrace_disabled))
1181 return;
1183 ftrace_start_up++;
1184 command |= FTRACE_ENABLE_CALLS;
1186 ftrace_startup_enable(command);
1189 static void ftrace_shutdown(int command)
1191 if (unlikely(ftrace_disabled))
1192 return;
1194 ftrace_start_up--;
1196 * Just warn in case of unbalance, no need to kill ftrace, it's not
1197 * critical but the ftrace_call callers may be never nopped again after
1198 * further ftrace uses.
1200 WARN_ON_ONCE(ftrace_start_up < 0);
1202 if (!ftrace_start_up)
1203 command |= FTRACE_DISABLE_CALLS;
1205 if (saved_ftrace_func != ftrace_trace_function) {
1206 saved_ftrace_func = ftrace_trace_function;
1207 command |= FTRACE_UPDATE_TRACE_FUNC;
1210 if (!command || !ftrace_enabled)
1211 return;
1213 ftrace_run_update_code(command);
1216 static void ftrace_startup_sysctl(void)
1218 int command = FTRACE_ENABLE_MCOUNT;
1220 if (unlikely(ftrace_disabled))
1221 return;
1223 /* Force update next time */
1224 saved_ftrace_func = NULL;
1225 /* ftrace_start_up is true if we want ftrace running */
1226 if (ftrace_start_up)
1227 command |= FTRACE_ENABLE_CALLS;
1229 ftrace_run_update_code(command);
1232 static void ftrace_shutdown_sysctl(void)
1234 int command = FTRACE_DISABLE_MCOUNT;
1236 if (unlikely(ftrace_disabled))
1237 return;
1239 /* ftrace_start_up is true if ftrace is running */
1240 if (ftrace_start_up)
1241 command |= FTRACE_DISABLE_CALLS;
1243 ftrace_run_update_code(command);
1246 static cycle_t ftrace_update_time;
1247 static unsigned long ftrace_update_cnt;
1248 unsigned long ftrace_update_tot_cnt;
1250 static int ftrace_update_code(struct module *mod)
1252 struct dyn_ftrace *p;
1253 cycle_t start, stop;
1255 start = ftrace_now(raw_smp_processor_id());
1256 ftrace_update_cnt = 0;
1258 while (ftrace_new_addrs) {
1260 /* If something went wrong, bail without enabling anything */
1261 if (unlikely(ftrace_disabled))
1262 return -1;
1264 p = ftrace_new_addrs;
1265 ftrace_new_addrs = p->newlist;
1266 p->flags = 0L;
1268 /* convert record (i.e, patch mcount-call with NOP) */
1269 if (ftrace_code_disable(mod, p)) {
1270 p->flags |= FTRACE_FL_CONVERTED;
1271 ftrace_update_cnt++;
1272 } else
1273 ftrace_free_rec(p);
1276 stop = ftrace_now(raw_smp_processor_id());
1277 ftrace_update_time = stop - start;
1278 ftrace_update_tot_cnt += ftrace_update_cnt;
1280 return 0;
1283 static int __init ftrace_dyn_table_alloc(unsigned long num_to_init)
1285 struct ftrace_page *pg;
1286 int cnt;
1287 int i;
1289 /* allocate a few pages */
1290 ftrace_pages_start = (void *)get_zeroed_page(GFP_KERNEL);
1291 if (!ftrace_pages_start)
1292 return -1;
1295 * Allocate a few more pages.
1297 * TODO: have some parser search vmlinux before
1298 * final linking to find all calls to ftrace.
1299 * Then we can:
1300 * a) know how many pages to allocate.
1301 * and/or
1302 * b) set up the table then.
1304 * The dynamic code is still necessary for
1305 * modules.
1308 pg = ftrace_pages = ftrace_pages_start;
1310 cnt = num_to_init / ENTRIES_PER_PAGE;
1311 pr_info("ftrace: allocating %ld entries in %d pages\n",
1312 num_to_init, cnt + 1);
1314 for (i = 0; i < cnt; i++) {
1315 pg->next = (void *)get_zeroed_page(GFP_KERNEL);
1317 /* If we fail, we'll try later anyway */
1318 if (!pg->next)
1319 break;
1321 pg = pg->next;
1324 return 0;
1327 enum {
1328 FTRACE_ITER_FILTER = (1 << 0),
1329 FTRACE_ITER_NOTRACE = (1 << 1),
1330 FTRACE_ITER_FAILURES = (1 << 2),
1331 FTRACE_ITER_PRINTALL = (1 << 3),
1332 FTRACE_ITER_HASH = (1 << 4),
1335 #define FTRACE_BUFF_MAX (KSYM_SYMBOL_LEN+4) /* room for wildcards */
1337 struct ftrace_iterator {
1338 struct ftrace_page *pg;
1339 int hidx;
1340 int idx;
1341 unsigned flags;
1342 struct trace_parser parser;
1345 static void *
1346 t_hash_next(struct seq_file *m, void *v, loff_t *pos)
1348 struct ftrace_iterator *iter = m->private;
1349 struct hlist_node *hnd = v;
1350 struct hlist_head *hhd;
1352 WARN_ON(!(iter->flags & FTRACE_ITER_HASH));
1354 (*pos)++;
1356 retry:
1357 if (iter->hidx >= FTRACE_FUNC_HASHSIZE)
1358 return NULL;
1360 hhd = &ftrace_func_hash[iter->hidx];
1362 if (hlist_empty(hhd)) {
1363 iter->hidx++;
1364 hnd = NULL;
1365 goto retry;
1368 if (!hnd)
1369 hnd = hhd->first;
1370 else {
1371 hnd = hnd->next;
1372 if (!hnd) {
1373 iter->hidx++;
1374 goto retry;
1378 return hnd;
1381 static void *t_hash_start(struct seq_file *m, loff_t *pos)
1383 struct ftrace_iterator *iter = m->private;
1384 void *p = NULL;
1385 loff_t l;
1387 if (!(iter->flags & FTRACE_ITER_HASH))
1388 *pos = 0;
1390 iter->flags |= FTRACE_ITER_HASH;
1392 iter->hidx = 0;
1393 for (l = 0; l <= *pos; ) {
1394 p = t_hash_next(m, p, &l);
1395 if (!p)
1396 break;
1398 return p;
1401 static int t_hash_show(struct seq_file *m, void *v)
1403 struct ftrace_func_probe *rec;
1404 struct hlist_node *hnd = v;
1406 rec = hlist_entry(hnd, struct ftrace_func_probe, node);
1408 if (rec->ops->print)
1409 return rec->ops->print(m, rec->ip, rec->ops, rec->data);
1411 seq_printf(m, "%ps:%ps", (void *)rec->ip, (void *)rec->ops->func);
1413 if (rec->data)
1414 seq_printf(m, ":%p", rec->data);
1415 seq_putc(m, '\n');
1417 return 0;
1420 static void *
1421 t_next(struct seq_file *m, void *v, loff_t *pos)
1423 struct ftrace_iterator *iter = m->private;
1424 struct dyn_ftrace *rec = NULL;
1426 if (iter->flags & FTRACE_ITER_HASH)
1427 return t_hash_next(m, v, pos);
1429 (*pos)++;
1431 if (iter->flags & FTRACE_ITER_PRINTALL)
1432 return NULL;
1434 retry:
1435 if (iter->idx >= iter->pg->index) {
1436 if (iter->pg->next) {
1437 iter->pg = iter->pg->next;
1438 iter->idx = 0;
1439 goto retry;
1441 } else {
1442 rec = &iter->pg->records[iter->idx++];
1443 if ((rec->flags & FTRACE_FL_FREE) ||
1445 (!(iter->flags & FTRACE_ITER_FAILURES) &&
1446 (rec->flags & FTRACE_FL_FAILED)) ||
1448 ((iter->flags & FTRACE_ITER_FAILURES) &&
1449 !(rec->flags & FTRACE_FL_FAILED)) ||
1451 ((iter->flags & FTRACE_ITER_FILTER) &&
1452 !(rec->flags & FTRACE_FL_FILTER)) ||
1454 ((iter->flags & FTRACE_ITER_NOTRACE) &&
1455 !(rec->flags & FTRACE_FL_NOTRACE))) {
1456 rec = NULL;
1457 goto retry;
1461 return rec;
1464 static void *t_start(struct seq_file *m, loff_t *pos)
1466 struct ftrace_iterator *iter = m->private;
1467 void *p = NULL;
1468 loff_t l;
1470 mutex_lock(&ftrace_lock);
1472 * For set_ftrace_filter reading, if we have the filter
1473 * off, we can short cut and just print out that all
1474 * functions are enabled.
1476 if (iter->flags & FTRACE_ITER_FILTER && !ftrace_filtered) {
1477 if (*pos > 0)
1478 return t_hash_start(m, pos);
1479 iter->flags |= FTRACE_ITER_PRINTALL;
1480 return iter;
1483 if (iter->flags & FTRACE_ITER_HASH)
1484 return t_hash_start(m, pos);
1486 iter->pg = ftrace_pages_start;
1487 iter->idx = 0;
1488 for (l = 0; l <= *pos; ) {
1489 p = t_next(m, p, &l);
1490 if (!p)
1491 break;
1494 if (!p && iter->flags & FTRACE_ITER_FILTER)
1495 return t_hash_start(m, pos);
1497 return p;
1500 static void t_stop(struct seq_file *m, void *p)
1502 mutex_unlock(&ftrace_lock);
1505 static int t_show(struct seq_file *m, void *v)
1507 struct ftrace_iterator *iter = m->private;
1508 struct dyn_ftrace *rec = v;
1510 if (iter->flags & FTRACE_ITER_HASH)
1511 return t_hash_show(m, v);
1513 if (iter->flags & FTRACE_ITER_PRINTALL) {
1514 seq_printf(m, "#### all functions enabled ####\n");
1515 return 0;
1518 if (!rec)
1519 return 0;
1521 seq_printf(m, "%ps\n", (void *)rec->ip);
1523 return 0;
1526 static const struct seq_operations show_ftrace_seq_ops = {
1527 .start = t_start,
1528 .next = t_next,
1529 .stop = t_stop,
1530 .show = t_show,
1533 static int
1534 ftrace_avail_open(struct inode *inode, struct file *file)
1536 struct ftrace_iterator *iter;
1537 int ret;
1539 if (unlikely(ftrace_disabled))
1540 return -ENODEV;
1542 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
1543 if (!iter)
1544 return -ENOMEM;
1546 iter->pg = ftrace_pages_start;
1548 ret = seq_open(file, &show_ftrace_seq_ops);
1549 if (!ret) {
1550 struct seq_file *m = file->private_data;
1552 m->private = iter;
1553 } else {
1554 kfree(iter);
1557 return ret;
1560 static int
1561 ftrace_failures_open(struct inode *inode, struct file *file)
1563 int ret;
1564 struct seq_file *m;
1565 struct ftrace_iterator *iter;
1567 ret = ftrace_avail_open(inode, file);
1568 if (!ret) {
1569 m = (struct seq_file *)file->private_data;
1570 iter = (struct ftrace_iterator *)m->private;
1571 iter->flags = FTRACE_ITER_FAILURES;
1574 return ret;
1578 static void ftrace_filter_reset(int enable)
1580 struct ftrace_page *pg;
1581 struct dyn_ftrace *rec;
1582 unsigned long type = enable ? FTRACE_FL_FILTER : FTRACE_FL_NOTRACE;
1584 mutex_lock(&ftrace_lock);
1585 if (enable)
1586 ftrace_filtered = 0;
1587 do_for_each_ftrace_rec(pg, rec) {
1588 if (rec->flags & FTRACE_FL_FAILED)
1589 continue;
1590 rec->flags &= ~type;
1591 } while_for_each_ftrace_rec();
1592 mutex_unlock(&ftrace_lock);
1595 static int
1596 ftrace_regex_open(struct inode *inode, struct file *file, int enable)
1598 struct ftrace_iterator *iter;
1599 int ret = 0;
1601 if (unlikely(ftrace_disabled))
1602 return -ENODEV;
1604 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
1605 if (!iter)
1606 return -ENOMEM;
1608 if (trace_parser_get_init(&iter->parser, FTRACE_BUFF_MAX)) {
1609 kfree(iter);
1610 return -ENOMEM;
1613 mutex_lock(&ftrace_regex_lock);
1614 if ((file->f_mode & FMODE_WRITE) &&
1615 (file->f_flags & O_TRUNC))
1616 ftrace_filter_reset(enable);
1618 if (file->f_mode & FMODE_READ) {
1619 iter->pg = ftrace_pages_start;
1620 iter->flags = enable ? FTRACE_ITER_FILTER :
1621 FTRACE_ITER_NOTRACE;
1623 ret = seq_open(file, &show_ftrace_seq_ops);
1624 if (!ret) {
1625 struct seq_file *m = file->private_data;
1626 m->private = iter;
1627 } else {
1628 trace_parser_put(&iter->parser);
1629 kfree(iter);
1631 } else
1632 file->private_data = iter;
1633 mutex_unlock(&ftrace_regex_lock);
1635 return ret;
1638 static int
1639 ftrace_filter_open(struct inode *inode, struct file *file)
1641 return ftrace_regex_open(inode, file, 1);
1644 static int
1645 ftrace_notrace_open(struct inode *inode, struct file *file)
1647 return ftrace_regex_open(inode, file, 0);
1650 static loff_t
1651 ftrace_regex_lseek(struct file *file, loff_t offset, int origin)
1653 loff_t ret;
1655 if (file->f_mode & FMODE_READ)
1656 ret = seq_lseek(file, offset, origin);
1657 else
1658 file->f_pos = ret = 1;
1660 return ret;
1663 static int ftrace_match(char *str, char *regex, int len, int type)
1665 int matched = 0;
1666 char *ptr;
1668 switch (type) {
1669 case MATCH_FULL:
1670 if (strcmp(str, regex) == 0)
1671 matched = 1;
1672 break;
1673 case MATCH_FRONT_ONLY:
1674 if (strncmp(str, regex, len) == 0)
1675 matched = 1;
1676 break;
1677 case MATCH_MIDDLE_ONLY:
1678 if (strstr(str, regex))
1679 matched = 1;
1680 break;
1681 case MATCH_END_ONLY:
1682 ptr = strstr(str, regex);
1683 if (ptr && (ptr[len] == 0))
1684 matched = 1;
1685 break;
1688 return matched;
1691 static int
1692 ftrace_match_record(struct dyn_ftrace *rec, char *regex, int len, int type)
1694 char str[KSYM_SYMBOL_LEN];
1696 kallsyms_lookup(rec->ip, NULL, NULL, NULL, str);
1697 return ftrace_match(str, regex, len, type);
1700 static void ftrace_match_records(char *buff, int len, int enable)
1702 unsigned int search_len;
1703 struct ftrace_page *pg;
1704 struct dyn_ftrace *rec;
1705 unsigned long flag;
1706 char *search;
1707 int type;
1708 int not;
1710 flag = enable ? FTRACE_FL_FILTER : FTRACE_FL_NOTRACE;
1711 type = filter_parse_regex(buff, len, &search, &not);
1713 search_len = strlen(search);
1715 mutex_lock(&ftrace_lock);
1716 do_for_each_ftrace_rec(pg, rec) {
1718 if (rec->flags & FTRACE_FL_FAILED)
1719 continue;
1721 if (ftrace_match_record(rec, search, search_len, type)) {
1722 if (not)
1723 rec->flags &= ~flag;
1724 else
1725 rec->flags |= flag;
1728 * Only enable filtering if we have a function that
1729 * is filtered on.
1731 if (enable && (rec->flags & FTRACE_FL_FILTER))
1732 ftrace_filtered = 1;
1733 } while_for_each_ftrace_rec();
1734 mutex_unlock(&ftrace_lock);
1737 static int
1738 ftrace_match_module_record(struct dyn_ftrace *rec, char *mod,
1739 char *regex, int len, int type)
1741 char str[KSYM_SYMBOL_LEN];
1742 char *modname;
1744 kallsyms_lookup(rec->ip, NULL, NULL, &modname, str);
1746 if (!modname || strcmp(modname, mod))
1747 return 0;
1749 /* blank search means to match all funcs in the mod */
1750 if (len)
1751 return ftrace_match(str, regex, len, type);
1752 else
1753 return 1;
1756 static void ftrace_match_module_records(char *buff, char *mod, int enable)
1758 unsigned search_len = 0;
1759 struct ftrace_page *pg;
1760 struct dyn_ftrace *rec;
1761 int type = MATCH_FULL;
1762 char *search = buff;
1763 unsigned long flag;
1764 int not = 0;
1766 flag = enable ? FTRACE_FL_FILTER : FTRACE_FL_NOTRACE;
1768 /* blank or '*' mean the same */
1769 if (strcmp(buff, "*") == 0)
1770 buff[0] = 0;
1772 /* handle the case of 'dont filter this module' */
1773 if (strcmp(buff, "!") == 0 || strcmp(buff, "!*") == 0) {
1774 buff[0] = 0;
1775 not = 1;
1778 if (strlen(buff)) {
1779 type = filter_parse_regex(buff, strlen(buff), &search, &not);
1780 search_len = strlen(search);
1783 mutex_lock(&ftrace_lock);
1784 do_for_each_ftrace_rec(pg, rec) {
1786 if (rec->flags & FTRACE_FL_FAILED)
1787 continue;
1789 if (ftrace_match_module_record(rec, mod,
1790 search, search_len, type)) {
1791 if (not)
1792 rec->flags &= ~flag;
1793 else
1794 rec->flags |= flag;
1796 if (enable && (rec->flags & FTRACE_FL_FILTER))
1797 ftrace_filtered = 1;
1799 } while_for_each_ftrace_rec();
1800 mutex_unlock(&ftrace_lock);
1804 * We register the module command as a template to show others how
1805 * to register the a command as well.
1808 static int
1809 ftrace_mod_callback(char *func, char *cmd, char *param, int enable)
1811 char *mod;
1814 * cmd == 'mod' because we only registered this func
1815 * for the 'mod' ftrace_func_command.
1816 * But if you register one func with multiple commands,
1817 * you can tell which command was used by the cmd
1818 * parameter.
1821 /* we must have a module name */
1822 if (!param)
1823 return -EINVAL;
1825 mod = strsep(&param, ":");
1826 if (!strlen(mod))
1827 return -EINVAL;
1829 ftrace_match_module_records(func, mod, enable);
1830 return 0;
1833 static struct ftrace_func_command ftrace_mod_cmd = {
1834 .name = "mod",
1835 .func = ftrace_mod_callback,
1838 static int __init ftrace_mod_cmd_init(void)
1840 return register_ftrace_command(&ftrace_mod_cmd);
1842 device_initcall(ftrace_mod_cmd_init);
1844 static void
1845 function_trace_probe_call(unsigned long ip, unsigned long parent_ip)
1847 struct ftrace_func_probe *entry;
1848 struct hlist_head *hhd;
1849 struct hlist_node *n;
1850 unsigned long key;
1851 int resched;
1853 key = hash_long(ip, FTRACE_HASH_BITS);
1855 hhd = &ftrace_func_hash[key];
1857 if (hlist_empty(hhd))
1858 return;
1861 * Disable preemption for these calls to prevent a RCU grace
1862 * period. This syncs the hash iteration and freeing of items
1863 * on the hash. rcu_read_lock is too dangerous here.
1865 resched = ftrace_preempt_disable();
1866 hlist_for_each_entry_rcu(entry, n, hhd, node) {
1867 if (entry->ip == ip)
1868 entry->ops->func(ip, parent_ip, &entry->data);
1870 ftrace_preempt_enable(resched);
1873 static struct ftrace_ops trace_probe_ops __read_mostly =
1875 .func = function_trace_probe_call,
1878 static int ftrace_probe_registered;
1880 static void __enable_ftrace_function_probe(void)
1882 int i;
1884 if (ftrace_probe_registered)
1885 return;
1887 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
1888 struct hlist_head *hhd = &ftrace_func_hash[i];
1889 if (hhd->first)
1890 break;
1892 /* Nothing registered? */
1893 if (i == FTRACE_FUNC_HASHSIZE)
1894 return;
1896 __register_ftrace_function(&trace_probe_ops);
1897 ftrace_startup(0);
1898 ftrace_probe_registered = 1;
1901 static void __disable_ftrace_function_probe(void)
1903 int i;
1905 if (!ftrace_probe_registered)
1906 return;
1908 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
1909 struct hlist_head *hhd = &ftrace_func_hash[i];
1910 if (hhd->first)
1911 return;
1914 /* no more funcs left */
1915 __unregister_ftrace_function(&trace_probe_ops);
1916 ftrace_shutdown(0);
1917 ftrace_probe_registered = 0;
1921 static void ftrace_free_entry_rcu(struct rcu_head *rhp)
1923 struct ftrace_func_probe *entry =
1924 container_of(rhp, struct ftrace_func_probe, rcu);
1926 if (entry->ops->free)
1927 entry->ops->free(&entry->data);
1928 kfree(entry);
1933 register_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
1934 void *data)
1936 struct ftrace_func_probe *entry;
1937 struct ftrace_page *pg;
1938 struct dyn_ftrace *rec;
1939 int type, len, not;
1940 unsigned long key;
1941 int count = 0;
1942 char *search;
1944 type = filter_parse_regex(glob, strlen(glob), &search, &not);
1945 len = strlen(search);
1947 /* we do not support '!' for function probes */
1948 if (WARN_ON(not))
1949 return -EINVAL;
1951 mutex_lock(&ftrace_lock);
1952 do_for_each_ftrace_rec(pg, rec) {
1954 if (rec->flags & FTRACE_FL_FAILED)
1955 continue;
1957 if (!ftrace_match_record(rec, search, len, type))
1958 continue;
1960 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
1961 if (!entry) {
1962 /* If we did not process any, then return error */
1963 if (!count)
1964 count = -ENOMEM;
1965 goto out_unlock;
1968 count++;
1970 entry->data = data;
1973 * The caller might want to do something special
1974 * for each function we find. We call the callback
1975 * to give the caller an opportunity to do so.
1977 if (ops->callback) {
1978 if (ops->callback(rec->ip, &entry->data) < 0) {
1979 /* caller does not like this func */
1980 kfree(entry);
1981 continue;
1985 entry->ops = ops;
1986 entry->ip = rec->ip;
1988 key = hash_long(entry->ip, FTRACE_HASH_BITS);
1989 hlist_add_head_rcu(&entry->node, &ftrace_func_hash[key]);
1991 } while_for_each_ftrace_rec();
1992 __enable_ftrace_function_probe();
1994 out_unlock:
1995 mutex_unlock(&ftrace_lock);
1997 return count;
2000 enum {
2001 PROBE_TEST_FUNC = 1,
2002 PROBE_TEST_DATA = 2
2005 static void
2006 __unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
2007 void *data, int flags)
2009 struct ftrace_func_probe *entry;
2010 struct hlist_node *n, *tmp;
2011 char str[KSYM_SYMBOL_LEN];
2012 int type = MATCH_FULL;
2013 int i, len = 0;
2014 char *search;
2016 if (glob && (strcmp(glob, "*") == 0 || !strlen(glob)))
2017 glob = NULL;
2018 else if (glob) {
2019 int not;
2021 type = filter_parse_regex(glob, strlen(glob), &search, &not);
2022 len = strlen(search);
2024 /* we do not support '!' for function probes */
2025 if (WARN_ON(not))
2026 return;
2029 mutex_lock(&ftrace_lock);
2030 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
2031 struct hlist_head *hhd = &ftrace_func_hash[i];
2033 hlist_for_each_entry_safe(entry, n, tmp, hhd, node) {
2035 /* break up if statements for readability */
2036 if ((flags & PROBE_TEST_FUNC) && entry->ops != ops)
2037 continue;
2039 if ((flags & PROBE_TEST_DATA) && entry->data != data)
2040 continue;
2042 /* do this last, since it is the most expensive */
2043 if (glob) {
2044 kallsyms_lookup(entry->ip, NULL, NULL,
2045 NULL, str);
2046 if (!ftrace_match(str, glob, len, type))
2047 continue;
2050 hlist_del(&entry->node);
2051 call_rcu(&entry->rcu, ftrace_free_entry_rcu);
2054 __disable_ftrace_function_probe();
2055 mutex_unlock(&ftrace_lock);
2058 void
2059 unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
2060 void *data)
2062 __unregister_ftrace_function_probe(glob, ops, data,
2063 PROBE_TEST_FUNC | PROBE_TEST_DATA);
2066 void
2067 unregister_ftrace_function_probe_func(char *glob, struct ftrace_probe_ops *ops)
2069 __unregister_ftrace_function_probe(glob, ops, NULL, PROBE_TEST_FUNC);
2072 void unregister_ftrace_function_probe_all(char *glob)
2074 __unregister_ftrace_function_probe(glob, NULL, NULL, 0);
2077 static LIST_HEAD(ftrace_commands);
2078 static DEFINE_MUTEX(ftrace_cmd_mutex);
2080 int register_ftrace_command(struct ftrace_func_command *cmd)
2082 struct ftrace_func_command *p;
2083 int ret = 0;
2085 mutex_lock(&ftrace_cmd_mutex);
2086 list_for_each_entry(p, &ftrace_commands, list) {
2087 if (strcmp(cmd->name, p->name) == 0) {
2088 ret = -EBUSY;
2089 goto out_unlock;
2092 list_add(&cmd->list, &ftrace_commands);
2093 out_unlock:
2094 mutex_unlock(&ftrace_cmd_mutex);
2096 return ret;
2099 int unregister_ftrace_command(struct ftrace_func_command *cmd)
2101 struct ftrace_func_command *p, *n;
2102 int ret = -ENODEV;
2104 mutex_lock(&ftrace_cmd_mutex);
2105 list_for_each_entry_safe(p, n, &ftrace_commands, list) {
2106 if (strcmp(cmd->name, p->name) == 0) {
2107 ret = 0;
2108 list_del_init(&p->list);
2109 goto out_unlock;
2112 out_unlock:
2113 mutex_unlock(&ftrace_cmd_mutex);
2115 return ret;
2118 static int ftrace_process_regex(char *buff, int len, int enable)
2120 char *func, *command, *next = buff;
2121 struct ftrace_func_command *p;
2122 int ret = -EINVAL;
2124 func = strsep(&next, ":");
2126 if (!next) {
2127 ftrace_match_records(func, len, enable);
2128 return 0;
2131 /* command found */
2133 command = strsep(&next, ":");
2135 mutex_lock(&ftrace_cmd_mutex);
2136 list_for_each_entry(p, &ftrace_commands, list) {
2137 if (strcmp(p->name, command) == 0) {
2138 ret = p->func(func, command, next, enable);
2139 goto out_unlock;
2142 out_unlock:
2143 mutex_unlock(&ftrace_cmd_mutex);
2145 return ret;
2148 static ssize_t
2149 ftrace_regex_write(struct file *file, const char __user *ubuf,
2150 size_t cnt, loff_t *ppos, int enable)
2152 struct ftrace_iterator *iter;
2153 struct trace_parser *parser;
2154 ssize_t ret, read;
2156 if (!cnt)
2157 return 0;
2159 mutex_lock(&ftrace_regex_lock);
2161 if (file->f_mode & FMODE_READ) {
2162 struct seq_file *m = file->private_data;
2163 iter = m->private;
2164 } else
2165 iter = file->private_data;
2167 parser = &iter->parser;
2168 read = trace_get_user(parser, ubuf, cnt, ppos);
2170 if (read >= 0 && trace_parser_loaded(parser) &&
2171 !trace_parser_cont(parser)) {
2172 ret = ftrace_process_regex(parser->buffer,
2173 parser->idx, enable);
2174 if (ret)
2175 goto out;
2177 trace_parser_clear(parser);
2180 ret = read;
2182 mutex_unlock(&ftrace_regex_lock);
2183 out:
2184 return ret;
2187 static ssize_t
2188 ftrace_filter_write(struct file *file, const char __user *ubuf,
2189 size_t cnt, loff_t *ppos)
2191 return ftrace_regex_write(file, ubuf, cnt, ppos, 1);
2194 static ssize_t
2195 ftrace_notrace_write(struct file *file, const char __user *ubuf,
2196 size_t cnt, loff_t *ppos)
2198 return ftrace_regex_write(file, ubuf, cnt, ppos, 0);
2201 static void
2202 ftrace_set_regex(unsigned char *buf, int len, int reset, int enable)
2204 if (unlikely(ftrace_disabled))
2205 return;
2207 mutex_lock(&ftrace_regex_lock);
2208 if (reset)
2209 ftrace_filter_reset(enable);
2210 if (buf)
2211 ftrace_match_records(buf, len, enable);
2212 mutex_unlock(&ftrace_regex_lock);
2216 * ftrace_set_filter - set a function to filter on in ftrace
2217 * @buf - the string that holds the function filter text.
2218 * @len - the length of the string.
2219 * @reset - non zero to reset all filters before applying this filter.
2221 * Filters denote which functions should be enabled when tracing is enabled.
2222 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
2224 void ftrace_set_filter(unsigned char *buf, int len, int reset)
2226 ftrace_set_regex(buf, len, reset, 1);
2230 * ftrace_set_notrace - set a function to not trace in ftrace
2231 * @buf - the string that holds the function notrace text.
2232 * @len - the length of the string.
2233 * @reset - non zero to reset all filters before applying this filter.
2235 * Notrace Filters denote which functions should not be enabled when tracing
2236 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
2237 * for tracing.
2239 void ftrace_set_notrace(unsigned char *buf, int len, int reset)
2241 ftrace_set_regex(buf, len, reset, 0);
2245 * command line interface to allow users to set filters on boot up.
2247 #define FTRACE_FILTER_SIZE COMMAND_LINE_SIZE
2248 static char ftrace_notrace_buf[FTRACE_FILTER_SIZE] __initdata;
2249 static char ftrace_filter_buf[FTRACE_FILTER_SIZE] __initdata;
2250 static char ftrace_graph_buf[FTRACE_FILTER_SIZE] __initdata;
2252 static int __init set_ftrace_notrace(char *str)
2254 strncpy(ftrace_notrace_buf, str, FTRACE_FILTER_SIZE);
2255 return 1;
2257 __setup("ftrace_notrace=", set_ftrace_notrace);
2259 static int __init set_ftrace_filter(char *str)
2261 strncpy(ftrace_filter_buf, str, FTRACE_FILTER_SIZE);
2262 return 1;
2264 __setup("ftrace_filter=", set_ftrace_filter);
2266 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
2267 static int __init set_graph_function(char *str)
2269 strncpy(ftrace_graph_buf, str, FTRACE_FILTER_SIZE);
2270 return 1;
2272 __setup("ftrace_graph_filter=", set_graph_function);
2274 static void __init set_ftrace_early_graph(char *buf)
2276 int ret;
2277 char *func;
2279 while (buf) {
2280 func = strsep(&buf, ",");
2281 /* we allow only one expression at a time */
2282 ret = ftrace_set_func(ftrace_graph_funcs, &ftrace_graph_count,
2283 func);
2284 if (ret)
2285 printk(KERN_DEBUG "ftrace: function %s not "
2286 "traceable\n", func);
2289 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
2291 static void __init set_ftrace_early_filter(char *buf, int enable)
2293 char *func;
2295 while (buf) {
2296 func = strsep(&buf, ",");
2297 ftrace_set_regex(func, strlen(func), 0, enable);
2301 static void __init set_ftrace_early_filters(void)
2303 if (ftrace_filter_buf[0])
2304 set_ftrace_early_filter(ftrace_filter_buf, 1);
2305 if (ftrace_notrace_buf[0])
2306 set_ftrace_early_filter(ftrace_notrace_buf, 0);
2307 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
2308 if (ftrace_graph_buf[0])
2309 set_ftrace_early_graph(ftrace_graph_buf);
2310 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
2313 static int
2314 ftrace_regex_release(struct inode *inode, struct file *file, int enable)
2316 struct seq_file *m = (struct seq_file *)file->private_data;
2317 struct ftrace_iterator *iter;
2318 struct trace_parser *parser;
2320 mutex_lock(&ftrace_regex_lock);
2321 if (file->f_mode & FMODE_READ) {
2322 iter = m->private;
2324 seq_release(inode, file);
2325 } else
2326 iter = file->private_data;
2328 parser = &iter->parser;
2329 if (trace_parser_loaded(parser)) {
2330 parser->buffer[parser->idx] = 0;
2331 ftrace_match_records(parser->buffer, parser->idx, enable);
2334 mutex_lock(&ftrace_lock);
2335 if (ftrace_start_up && ftrace_enabled)
2336 ftrace_run_update_code(FTRACE_ENABLE_CALLS);
2337 mutex_unlock(&ftrace_lock);
2339 trace_parser_put(parser);
2340 kfree(iter);
2342 mutex_unlock(&ftrace_regex_lock);
2343 return 0;
2346 static int
2347 ftrace_filter_release(struct inode *inode, struct file *file)
2349 return ftrace_regex_release(inode, file, 1);
2352 static int
2353 ftrace_notrace_release(struct inode *inode, struct file *file)
2355 return ftrace_regex_release(inode, file, 0);
2358 static const struct file_operations ftrace_avail_fops = {
2359 .open = ftrace_avail_open,
2360 .read = seq_read,
2361 .llseek = seq_lseek,
2362 .release = seq_release_private,
2365 static const struct file_operations ftrace_failures_fops = {
2366 .open = ftrace_failures_open,
2367 .read = seq_read,
2368 .llseek = seq_lseek,
2369 .release = seq_release_private,
2372 static const struct file_operations ftrace_filter_fops = {
2373 .open = ftrace_filter_open,
2374 .read = seq_read,
2375 .write = ftrace_filter_write,
2376 .llseek = ftrace_regex_lseek,
2377 .release = ftrace_filter_release,
2380 static const struct file_operations ftrace_notrace_fops = {
2381 .open = ftrace_notrace_open,
2382 .read = seq_read,
2383 .write = ftrace_notrace_write,
2384 .llseek = ftrace_regex_lseek,
2385 .release = ftrace_notrace_release,
2388 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
2390 static DEFINE_MUTEX(graph_lock);
2392 int ftrace_graph_count;
2393 unsigned long ftrace_graph_funcs[FTRACE_GRAPH_MAX_FUNCS] __read_mostly;
2395 static void *
2396 __g_next(struct seq_file *m, loff_t *pos)
2398 if (*pos >= ftrace_graph_count)
2399 return NULL;
2400 return &ftrace_graph_funcs[*pos];
2403 static void *
2404 g_next(struct seq_file *m, void *v, loff_t *pos)
2406 (*pos)++;
2407 return __g_next(m, pos);
2410 static void *g_start(struct seq_file *m, loff_t *pos)
2412 mutex_lock(&graph_lock);
2414 /* Nothing, tell g_show to print all functions are enabled */
2415 if (!ftrace_graph_count && !*pos)
2416 return (void *)1;
2418 return __g_next(m, pos);
2421 static void g_stop(struct seq_file *m, void *p)
2423 mutex_unlock(&graph_lock);
2426 static int g_show(struct seq_file *m, void *v)
2428 unsigned long *ptr = v;
2430 if (!ptr)
2431 return 0;
2433 if (ptr == (unsigned long *)1) {
2434 seq_printf(m, "#### all functions enabled ####\n");
2435 return 0;
2438 seq_printf(m, "%ps\n", (void *)*ptr);
2440 return 0;
2443 static const struct seq_operations ftrace_graph_seq_ops = {
2444 .start = g_start,
2445 .next = g_next,
2446 .stop = g_stop,
2447 .show = g_show,
2450 static int
2451 ftrace_graph_open(struct inode *inode, struct file *file)
2453 int ret = 0;
2455 if (unlikely(ftrace_disabled))
2456 return -ENODEV;
2458 mutex_lock(&graph_lock);
2459 if ((file->f_mode & FMODE_WRITE) &&
2460 (file->f_flags & O_TRUNC)) {
2461 ftrace_graph_count = 0;
2462 memset(ftrace_graph_funcs, 0, sizeof(ftrace_graph_funcs));
2464 mutex_unlock(&graph_lock);
2466 if (file->f_mode & FMODE_READ)
2467 ret = seq_open(file, &ftrace_graph_seq_ops);
2469 return ret;
2472 static int
2473 ftrace_graph_release(struct inode *inode, struct file *file)
2475 if (file->f_mode & FMODE_READ)
2476 seq_release(inode, file);
2477 return 0;
2480 static int
2481 ftrace_set_func(unsigned long *array, int *idx, char *buffer)
2483 struct dyn_ftrace *rec;
2484 struct ftrace_page *pg;
2485 int search_len;
2486 int found = 0;
2487 int type, not;
2488 char *search;
2489 bool exists;
2490 int i;
2492 if (ftrace_disabled)
2493 return -ENODEV;
2495 /* decode regex */
2496 type = filter_parse_regex(buffer, strlen(buffer), &search, &not);
2497 if (not)
2498 return -EINVAL;
2500 search_len = strlen(search);
2502 mutex_lock(&ftrace_lock);
2503 do_for_each_ftrace_rec(pg, rec) {
2505 if (*idx >= FTRACE_GRAPH_MAX_FUNCS)
2506 break;
2508 if (rec->flags & (FTRACE_FL_FAILED | FTRACE_FL_FREE))
2509 continue;
2511 if (ftrace_match_record(rec, search, search_len, type)) {
2512 /* ensure it is not already in the array */
2513 exists = false;
2514 for (i = 0; i < *idx; i++)
2515 if (array[i] == rec->ip) {
2516 exists = true;
2517 break;
2519 if (!exists) {
2520 array[(*idx)++] = rec->ip;
2521 found = 1;
2524 } while_for_each_ftrace_rec();
2526 mutex_unlock(&ftrace_lock);
2528 return found ? 0 : -EINVAL;
2531 static ssize_t
2532 ftrace_graph_write(struct file *file, const char __user *ubuf,
2533 size_t cnt, loff_t *ppos)
2535 struct trace_parser parser;
2536 ssize_t read, ret;
2538 if (!cnt || cnt < 0)
2539 return 0;
2541 mutex_lock(&graph_lock);
2543 if (ftrace_graph_count >= FTRACE_GRAPH_MAX_FUNCS) {
2544 ret = -EBUSY;
2545 goto out_unlock;
2548 if (trace_parser_get_init(&parser, FTRACE_BUFF_MAX)) {
2549 ret = -ENOMEM;
2550 goto out_unlock;
2553 read = trace_get_user(&parser, ubuf, cnt, ppos);
2555 if (read >= 0 && trace_parser_loaded((&parser))) {
2556 parser.buffer[parser.idx] = 0;
2558 /* we allow only one expression at a time */
2559 ret = ftrace_set_func(ftrace_graph_funcs, &ftrace_graph_count,
2560 parser.buffer);
2561 if (ret)
2562 goto out_free;
2565 ret = read;
2567 out_free:
2568 trace_parser_put(&parser);
2569 out_unlock:
2570 mutex_unlock(&graph_lock);
2572 return ret;
2575 static const struct file_operations ftrace_graph_fops = {
2576 .open = ftrace_graph_open,
2577 .read = seq_read,
2578 .write = ftrace_graph_write,
2579 .release = ftrace_graph_release,
2581 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
2583 static __init int ftrace_init_dyn_debugfs(struct dentry *d_tracer)
2586 trace_create_file("available_filter_functions", 0444,
2587 d_tracer, NULL, &ftrace_avail_fops);
2589 trace_create_file("failures", 0444,
2590 d_tracer, NULL, &ftrace_failures_fops);
2592 trace_create_file("set_ftrace_filter", 0644, d_tracer,
2593 NULL, &ftrace_filter_fops);
2595 trace_create_file("set_ftrace_notrace", 0644, d_tracer,
2596 NULL, &ftrace_notrace_fops);
2598 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
2599 trace_create_file("set_graph_function", 0444, d_tracer,
2600 NULL,
2601 &ftrace_graph_fops);
2602 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
2604 return 0;
2607 static int ftrace_convert_nops(struct module *mod,
2608 unsigned long *start,
2609 unsigned long *end)
2611 unsigned long *p;
2612 unsigned long addr;
2613 unsigned long flags;
2615 mutex_lock(&ftrace_lock);
2616 p = start;
2617 while (p < end) {
2618 addr = ftrace_call_adjust(*p++);
2620 * Some architecture linkers will pad between
2621 * the different mcount_loc sections of different
2622 * object files to satisfy alignments.
2623 * Skip any NULL pointers.
2625 if (!addr)
2626 continue;
2627 ftrace_record_ip(addr);
2630 /* disable interrupts to prevent kstop machine */
2631 local_irq_save(flags);
2632 ftrace_update_code(mod);
2633 local_irq_restore(flags);
2634 mutex_unlock(&ftrace_lock);
2636 return 0;
2639 #ifdef CONFIG_MODULES
2640 void ftrace_release_mod(struct module *mod)
2642 struct dyn_ftrace *rec;
2643 struct ftrace_page *pg;
2645 if (ftrace_disabled)
2646 return;
2648 mutex_lock(&ftrace_lock);
2649 do_for_each_ftrace_rec(pg, rec) {
2650 if (within_module_core(rec->ip, mod)) {
2652 * rec->ip is changed in ftrace_free_rec()
2653 * It should not between s and e if record was freed.
2655 FTRACE_WARN_ON(rec->flags & FTRACE_FL_FREE);
2656 ftrace_free_rec(rec);
2658 } while_for_each_ftrace_rec();
2659 mutex_unlock(&ftrace_lock);
2662 static void ftrace_init_module(struct module *mod,
2663 unsigned long *start, unsigned long *end)
2665 if (ftrace_disabled || start == end)
2666 return;
2667 ftrace_convert_nops(mod, start, end);
2670 static int ftrace_module_notify(struct notifier_block *self,
2671 unsigned long val, void *data)
2673 struct module *mod = data;
2675 switch (val) {
2676 case MODULE_STATE_COMING:
2677 ftrace_init_module(mod, mod->ftrace_callsites,
2678 mod->ftrace_callsites +
2679 mod->num_ftrace_callsites);
2680 break;
2681 case MODULE_STATE_GOING:
2682 ftrace_release_mod(mod);
2683 break;
2686 return 0;
2688 #else
2689 static int ftrace_module_notify(struct notifier_block *self,
2690 unsigned long val, void *data)
2692 return 0;
2694 #endif /* CONFIG_MODULES */
2696 struct notifier_block ftrace_module_nb = {
2697 .notifier_call = ftrace_module_notify,
2698 .priority = 0,
2701 extern unsigned long __start_mcount_loc[];
2702 extern unsigned long __stop_mcount_loc[];
2704 void __init ftrace_init(void)
2706 unsigned long count, addr, flags;
2707 int ret;
2709 /* Keep the ftrace pointer to the stub */
2710 addr = (unsigned long)ftrace_stub;
2712 local_irq_save(flags);
2713 ftrace_dyn_arch_init(&addr);
2714 local_irq_restore(flags);
2716 /* ftrace_dyn_arch_init places the return code in addr */
2717 if (addr)
2718 goto failed;
2720 count = __stop_mcount_loc - __start_mcount_loc;
2722 ret = ftrace_dyn_table_alloc(count);
2723 if (ret)
2724 goto failed;
2726 last_ftrace_enabled = ftrace_enabled = 1;
2728 ret = ftrace_convert_nops(NULL,
2729 __start_mcount_loc,
2730 __stop_mcount_loc);
2732 ret = register_module_notifier(&ftrace_module_nb);
2733 if (ret)
2734 pr_warning("Failed to register trace ftrace module notifier\n");
2736 set_ftrace_early_filters();
2738 return;
2739 failed:
2740 ftrace_disabled = 1;
2743 #else
2745 static int __init ftrace_nodyn_init(void)
2747 ftrace_enabled = 1;
2748 return 0;
2750 device_initcall(ftrace_nodyn_init);
2752 static inline int ftrace_init_dyn_debugfs(struct dentry *d_tracer) { return 0; }
2753 static inline void ftrace_startup_enable(int command) { }
2754 /* Keep as macros so we do not need to define the commands */
2755 # define ftrace_startup(command) do { } while (0)
2756 # define ftrace_shutdown(command) do { } while (0)
2757 # define ftrace_startup_sysctl() do { } while (0)
2758 # define ftrace_shutdown_sysctl() do { } while (0)
2759 #endif /* CONFIG_DYNAMIC_FTRACE */
2761 static ssize_t
2762 ftrace_pid_read(struct file *file, char __user *ubuf,
2763 size_t cnt, loff_t *ppos)
2765 char buf[64];
2766 int r;
2768 if (ftrace_pid_trace == ftrace_swapper_pid)
2769 r = sprintf(buf, "swapper tasks\n");
2770 else if (ftrace_pid_trace)
2771 r = sprintf(buf, "%u\n", pid_vnr(ftrace_pid_trace));
2772 else
2773 r = sprintf(buf, "no pid\n");
2775 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2778 static void clear_ftrace_swapper(void)
2780 struct task_struct *p;
2781 int cpu;
2783 get_online_cpus();
2784 for_each_online_cpu(cpu) {
2785 p = idle_task(cpu);
2786 clear_tsk_trace_trace(p);
2788 put_online_cpus();
2791 static void set_ftrace_swapper(void)
2793 struct task_struct *p;
2794 int cpu;
2796 get_online_cpus();
2797 for_each_online_cpu(cpu) {
2798 p = idle_task(cpu);
2799 set_tsk_trace_trace(p);
2801 put_online_cpus();
2804 static void clear_ftrace_pid(struct pid *pid)
2806 struct task_struct *p;
2808 rcu_read_lock();
2809 do_each_pid_task(pid, PIDTYPE_PID, p) {
2810 clear_tsk_trace_trace(p);
2811 } while_each_pid_task(pid, PIDTYPE_PID, p);
2812 rcu_read_unlock();
2814 put_pid(pid);
2817 static void set_ftrace_pid(struct pid *pid)
2819 struct task_struct *p;
2821 rcu_read_lock();
2822 do_each_pid_task(pid, PIDTYPE_PID, p) {
2823 set_tsk_trace_trace(p);
2824 } while_each_pid_task(pid, PIDTYPE_PID, p);
2825 rcu_read_unlock();
2828 static void clear_ftrace_pid_task(struct pid **pid)
2830 if (*pid == ftrace_swapper_pid)
2831 clear_ftrace_swapper();
2832 else
2833 clear_ftrace_pid(*pid);
2835 *pid = NULL;
2838 static void set_ftrace_pid_task(struct pid *pid)
2840 if (pid == ftrace_swapper_pid)
2841 set_ftrace_swapper();
2842 else
2843 set_ftrace_pid(pid);
2846 static ssize_t
2847 ftrace_pid_write(struct file *filp, const char __user *ubuf,
2848 size_t cnt, loff_t *ppos)
2850 struct pid *pid;
2851 char buf[64];
2852 long val;
2853 int ret;
2855 if (cnt >= sizeof(buf))
2856 return -EINVAL;
2858 if (copy_from_user(&buf, ubuf, cnt))
2859 return -EFAULT;
2861 buf[cnt] = 0;
2863 ret = strict_strtol(buf, 10, &val);
2864 if (ret < 0)
2865 return ret;
2867 mutex_lock(&ftrace_lock);
2868 if (val < 0) {
2869 /* disable pid tracing */
2870 if (!ftrace_pid_trace)
2871 goto out;
2873 clear_ftrace_pid_task(&ftrace_pid_trace);
2875 } else {
2876 /* swapper task is special */
2877 if (!val) {
2878 pid = ftrace_swapper_pid;
2879 if (pid == ftrace_pid_trace)
2880 goto out;
2881 } else {
2882 pid = find_get_pid(val);
2884 if (pid == ftrace_pid_trace) {
2885 put_pid(pid);
2886 goto out;
2890 if (ftrace_pid_trace)
2891 clear_ftrace_pid_task(&ftrace_pid_trace);
2893 if (!pid)
2894 goto out;
2896 ftrace_pid_trace = pid;
2898 set_ftrace_pid_task(ftrace_pid_trace);
2901 /* update the function call */
2902 ftrace_update_pid_func();
2903 ftrace_startup_enable(0);
2905 out:
2906 mutex_unlock(&ftrace_lock);
2908 return cnt;
2911 static const struct file_operations ftrace_pid_fops = {
2912 .read = ftrace_pid_read,
2913 .write = ftrace_pid_write,
2916 static __init int ftrace_init_debugfs(void)
2918 struct dentry *d_tracer;
2920 d_tracer = tracing_init_dentry();
2921 if (!d_tracer)
2922 return 0;
2924 ftrace_init_dyn_debugfs(d_tracer);
2926 trace_create_file("set_ftrace_pid", 0644, d_tracer,
2927 NULL, &ftrace_pid_fops);
2929 ftrace_profile_debugfs(d_tracer);
2931 return 0;
2933 fs_initcall(ftrace_init_debugfs);
2936 * ftrace_kill - kill ftrace
2938 * This function should be used by panic code. It stops ftrace
2939 * but in a not so nice way. If you need to simply kill ftrace
2940 * from a non-atomic section, use ftrace_kill.
2942 void ftrace_kill(void)
2944 ftrace_disabled = 1;
2945 ftrace_enabled = 0;
2946 clear_ftrace_function();
2950 * register_ftrace_function - register a function for profiling
2951 * @ops - ops structure that holds the function for profiling.
2953 * Register a function to be called by all functions in the
2954 * kernel.
2956 * Note: @ops->func and all the functions it calls must be labeled
2957 * with "notrace", otherwise it will go into a
2958 * recursive loop.
2960 int register_ftrace_function(struct ftrace_ops *ops)
2962 int ret;
2964 if (unlikely(ftrace_disabled))
2965 return -1;
2967 mutex_lock(&ftrace_lock);
2969 ret = __register_ftrace_function(ops);
2970 ftrace_startup(0);
2972 mutex_unlock(&ftrace_lock);
2973 return ret;
2977 * unregister_ftrace_function - unregister a function for profiling.
2978 * @ops - ops structure that holds the function to unregister
2980 * Unregister a function that was added to be called by ftrace profiling.
2982 int unregister_ftrace_function(struct ftrace_ops *ops)
2984 int ret;
2986 mutex_lock(&ftrace_lock);
2987 ret = __unregister_ftrace_function(ops);
2988 ftrace_shutdown(0);
2989 mutex_unlock(&ftrace_lock);
2991 return ret;
2995 ftrace_enable_sysctl(struct ctl_table *table, int write,
2996 void __user *buffer, size_t *lenp,
2997 loff_t *ppos)
2999 int ret;
3001 if (unlikely(ftrace_disabled))
3002 return -ENODEV;
3004 mutex_lock(&ftrace_lock);
3006 ret = proc_dointvec(table, write, buffer, lenp, ppos);
3008 if (ret || !write || (last_ftrace_enabled == !!ftrace_enabled))
3009 goto out;
3011 last_ftrace_enabled = !!ftrace_enabled;
3013 if (ftrace_enabled) {
3015 ftrace_startup_sysctl();
3017 /* we are starting ftrace again */
3018 if (ftrace_list != &ftrace_list_end) {
3019 if (ftrace_list->next == &ftrace_list_end)
3020 ftrace_trace_function = ftrace_list->func;
3021 else
3022 ftrace_trace_function = ftrace_list_func;
3025 } else {
3026 /* stopping ftrace calls (just send to ftrace_stub) */
3027 ftrace_trace_function = ftrace_stub;
3029 ftrace_shutdown_sysctl();
3032 out:
3033 mutex_unlock(&ftrace_lock);
3034 return ret;
3037 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
3039 static int ftrace_graph_active;
3040 static struct notifier_block ftrace_suspend_notifier;
3042 int ftrace_graph_entry_stub(struct ftrace_graph_ent *trace)
3044 return 0;
3047 /* The callbacks that hook a function */
3048 trace_func_graph_ret_t ftrace_graph_return =
3049 (trace_func_graph_ret_t)ftrace_stub;
3050 trace_func_graph_ent_t ftrace_graph_entry = ftrace_graph_entry_stub;
3052 /* Try to assign a return stack array on FTRACE_RETSTACK_ALLOC_SIZE tasks. */
3053 static int alloc_retstack_tasklist(struct ftrace_ret_stack **ret_stack_list)
3055 int i;
3056 int ret = 0;
3057 unsigned long flags;
3058 int start = 0, end = FTRACE_RETSTACK_ALLOC_SIZE;
3059 struct task_struct *g, *t;
3061 for (i = 0; i < FTRACE_RETSTACK_ALLOC_SIZE; i++) {
3062 ret_stack_list[i] = kmalloc(FTRACE_RETFUNC_DEPTH
3063 * sizeof(struct ftrace_ret_stack),
3064 GFP_KERNEL);
3065 if (!ret_stack_list[i]) {
3066 start = 0;
3067 end = i;
3068 ret = -ENOMEM;
3069 goto free;
3073 read_lock_irqsave(&tasklist_lock, flags);
3074 do_each_thread(g, t) {
3075 if (start == end) {
3076 ret = -EAGAIN;
3077 goto unlock;
3080 if (t->ret_stack == NULL) {
3081 atomic_set(&t->tracing_graph_pause, 0);
3082 atomic_set(&t->trace_overrun, 0);
3083 t->curr_ret_stack = -1;
3084 /* Make sure the tasks see the -1 first: */
3085 smp_wmb();
3086 t->ret_stack = ret_stack_list[start++];
3088 } while_each_thread(g, t);
3090 unlock:
3091 read_unlock_irqrestore(&tasklist_lock, flags);
3092 free:
3093 for (i = start; i < end; i++)
3094 kfree(ret_stack_list[i]);
3095 return ret;
3098 static void
3099 ftrace_graph_probe_sched_switch(struct rq *__rq, struct task_struct *prev,
3100 struct task_struct *next)
3102 unsigned long long timestamp;
3103 int index;
3106 * Does the user want to count the time a function was asleep.
3107 * If so, do not update the time stamps.
3109 if (trace_flags & TRACE_ITER_SLEEP_TIME)
3110 return;
3112 timestamp = trace_clock_local();
3114 prev->ftrace_timestamp = timestamp;
3116 /* only process tasks that we timestamped */
3117 if (!next->ftrace_timestamp)
3118 return;
3121 * Update all the counters in next to make up for the
3122 * time next was sleeping.
3124 timestamp -= next->ftrace_timestamp;
3126 for (index = next->curr_ret_stack; index >= 0; index--)
3127 next->ret_stack[index].calltime += timestamp;
3130 /* Allocate a return stack for each task */
3131 static int start_graph_tracing(void)
3133 struct ftrace_ret_stack **ret_stack_list;
3134 int ret, cpu;
3136 ret_stack_list = kmalloc(FTRACE_RETSTACK_ALLOC_SIZE *
3137 sizeof(struct ftrace_ret_stack *),
3138 GFP_KERNEL);
3140 if (!ret_stack_list)
3141 return -ENOMEM;
3143 /* The cpu_boot init_task->ret_stack will never be freed */
3144 for_each_online_cpu(cpu) {
3145 if (!idle_task(cpu)->ret_stack)
3146 ftrace_graph_init_task(idle_task(cpu));
3149 do {
3150 ret = alloc_retstack_tasklist(ret_stack_list);
3151 } while (ret == -EAGAIN);
3153 if (!ret) {
3154 ret = register_trace_sched_switch(ftrace_graph_probe_sched_switch);
3155 if (ret)
3156 pr_info("ftrace_graph: Couldn't activate tracepoint"
3157 " probe to kernel_sched_switch\n");
3160 kfree(ret_stack_list);
3161 return ret;
3165 * Hibernation protection.
3166 * The state of the current task is too much unstable during
3167 * suspend/restore to disk. We want to protect against that.
3169 static int
3170 ftrace_suspend_notifier_call(struct notifier_block *bl, unsigned long state,
3171 void *unused)
3173 switch (state) {
3174 case PM_HIBERNATION_PREPARE:
3175 pause_graph_tracing();
3176 break;
3178 case PM_POST_HIBERNATION:
3179 unpause_graph_tracing();
3180 break;
3182 return NOTIFY_DONE;
3185 int register_ftrace_graph(trace_func_graph_ret_t retfunc,
3186 trace_func_graph_ent_t entryfunc)
3188 int ret = 0;
3190 mutex_lock(&ftrace_lock);
3192 /* we currently allow only one tracer registered at a time */
3193 if (ftrace_graph_active) {
3194 ret = -EBUSY;
3195 goto out;
3198 ftrace_suspend_notifier.notifier_call = ftrace_suspend_notifier_call;
3199 register_pm_notifier(&ftrace_suspend_notifier);
3201 ftrace_graph_active++;
3202 ret = start_graph_tracing();
3203 if (ret) {
3204 ftrace_graph_active--;
3205 goto out;
3208 ftrace_graph_return = retfunc;
3209 ftrace_graph_entry = entryfunc;
3211 ftrace_startup(FTRACE_START_FUNC_RET);
3213 out:
3214 mutex_unlock(&ftrace_lock);
3215 return ret;
3218 void unregister_ftrace_graph(void)
3220 mutex_lock(&ftrace_lock);
3222 if (unlikely(!ftrace_graph_active))
3223 goto out;
3225 ftrace_graph_active--;
3226 unregister_trace_sched_switch(ftrace_graph_probe_sched_switch);
3227 ftrace_graph_return = (trace_func_graph_ret_t)ftrace_stub;
3228 ftrace_graph_entry = ftrace_graph_entry_stub;
3229 ftrace_shutdown(FTRACE_STOP_FUNC_RET);
3230 unregister_pm_notifier(&ftrace_suspend_notifier);
3232 out:
3233 mutex_unlock(&ftrace_lock);
3236 /* Allocate a return stack for newly created task */
3237 void ftrace_graph_init_task(struct task_struct *t)
3239 /* Make sure we do not use the parent ret_stack */
3240 t->ret_stack = NULL;
3242 if (ftrace_graph_active) {
3243 struct ftrace_ret_stack *ret_stack;
3245 ret_stack = kmalloc(FTRACE_RETFUNC_DEPTH
3246 * sizeof(struct ftrace_ret_stack),
3247 GFP_KERNEL);
3248 if (!ret_stack)
3249 return;
3250 t->curr_ret_stack = -1;
3251 atomic_set(&t->tracing_graph_pause, 0);
3252 atomic_set(&t->trace_overrun, 0);
3253 t->ftrace_timestamp = 0;
3254 /* make curr_ret_stack visable before we add the ret_stack */
3255 smp_wmb();
3256 t->ret_stack = ret_stack;
3260 void ftrace_graph_exit_task(struct task_struct *t)
3262 struct ftrace_ret_stack *ret_stack = t->ret_stack;
3264 t->ret_stack = NULL;
3265 /* NULL must become visible to IRQs before we free it: */
3266 barrier();
3268 kfree(ret_stack);
3271 void ftrace_graph_stop(void)
3273 ftrace_stop();
3275 #endif