tracing: Replace typecasted void pointer in set_ftrace_filter code
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / kernel / trace / ftrace.c
blobc8db0dbb984ed36b0e359acb0c3a6a5c48a238b5
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/ftrace.h>
26 #include <linux/sysctl.h>
27 #include <linux/slab.h>
28 #include <linux/ctype.h>
29 #include <linux/list.h>
30 #include <linux/hash.h>
31 #include <linux/rcupdate.h>
33 #include <trace/events/sched.h>
35 #include <asm/ftrace.h>
36 #include <asm/setup.h>
38 #include "trace_output.h"
39 #include "trace_stat.h"
41 #define FTRACE_WARN_ON(cond) \
42 do { \
43 if (WARN_ON(cond)) \
44 ftrace_kill(); \
45 } while (0)
47 #define FTRACE_WARN_ON_ONCE(cond) \
48 do { \
49 if (WARN_ON_ONCE(cond)) \
50 ftrace_kill(); \
51 } while (0)
53 /* hash bits for specific function selection */
54 #define FTRACE_HASH_BITS 7
55 #define FTRACE_FUNC_HASHSIZE (1 << FTRACE_HASH_BITS)
57 /* ftrace_enabled is a method to turn ftrace on or off */
58 int ftrace_enabled __read_mostly;
59 static int last_ftrace_enabled;
61 /* Quick disabling of function tracer. */
62 int function_trace_stop;
64 /* List for set_ftrace_pid's pids. */
65 LIST_HEAD(ftrace_pids);
66 struct ftrace_pid {
67 struct list_head list;
68 struct pid *pid;
72 * ftrace_disabled is set when an anomaly is discovered.
73 * ftrace_disabled is much stronger than ftrace_enabled.
75 static int ftrace_disabled __read_mostly;
77 static DEFINE_MUTEX(ftrace_lock);
79 static struct ftrace_ops ftrace_list_end __read_mostly =
81 .func = ftrace_stub,
84 static struct ftrace_ops *ftrace_list __read_mostly = &ftrace_list_end;
85 ftrace_func_t ftrace_trace_function __read_mostly = ftrace_stub;
86 ftrace_func_t __ftrace_trace_function __read_mostly = ftrace_stub;
87 ftrace_func_t ftrace_pid_function __read_mostly = ftrace_stub;
90 * Traverse the ftrace_list, invoking all entries. The reason that we
91 * can use rcu_dereference_raw() is that elements removed from this list
92 * are simply leaked, so there is no need to interact with a grace-period
93 * mechanism. The rcu_dereference_raw() calls are needed to handle
94 * concurrent insertions into the ftrace_list.
96 * Silly Alpha and silly pointer-speculation compiler optimizations!
98 static void ftrace_list_func(unsigned long ip, unsigned long parent_ip)
100 struct ftrace_ops *op = rcu_dereference_raw(ftrace_list); /*see above*/
102 while (op != &ftrace_list_end) {
103 op->func(ip, parent_ip);
104 op = rcu_dereference_raw(op->next); /*see above*/
108 static void ftrace_pid_func(unsigned long ip, unsigned long parent_ip)
110 if (!test_tsk_trace_trace(current))
111 return;
113 ftrace_pid_function(ip, parent_ip);
116 static void set_ftrace_pid_function(ftrace_func_t func)
118 /* do not set ftrace_pid_function to itself! */
119 if (func != ftrace_pid_func)
120 ftrace_pid_function = func;
124 * clear_ftrace_function - reset the ftrace function
126 * This NULLs the ftrace function and in essence stops
127 * tracing. There may be lag
129 void clear_ftrace_function(void)
131 ftrace_trace_function = ftrace_stub;
132 __ftrace_trace_function = ftrace_stub;
133 ftrace_pid_function = ftrace_stub;
136 #ifndef CONFIG_HAVE_FUNCTION_TRACE_MCOUNT_TEST
138 * For those archs that do not test ftrace_trace_stop in their
139 * mcount call site, we need to do it from C.
141 static void ftrace_test_stop_func(unsigned long ip, unsigned long parent_ip)
143 if (function_trace_stop)
144 return;
146 __ftrace_trace_function(ip, parent_ip);
148 #endif
150 static int __register_ftrace_function(struct ftrace_ops *ops)
152 ops->next = ftrace_list;
154 * We are entering ops into the ftrace_list but another
155 * CPU might be walking that list. We need to make sure
156 * the ops->next pointer is valid before another CPU sees
157 * the ops pointer included into the ftrace_list.
159 rcu_assign_pointer(ftrace_list, ops);
161 if (ftrace_enabled) {
162 ftrace_func_t func;
164 if (ops->next == &ftrace_list_end)
165 func = ops->func;
166 else
167 func = ftrace_list_func;
169 if (!list_empty(&ftrace_pids)) {
170 set_ftrace_pid_function(func);
171 func = ftrace_pid_func;
175 * For one func, simply call it directly.
176 * For more than one func, call the chain.
178 #ifdef CONFIG_HAVE_FUNCTION_TRACE_MCOUNT_TEST
179 ftrace_trace_function = func;
180 #else
181 __ftrace_trace_function = func;
182 ftrace_trace_function = ftrace_test_stop_func;
183 #endif
186 return 0;
189 static int __unregister_ftrace_function(struct ftrace_ops *ops)
191 struct ftrace_ops **p;
194 * If we are removing the last function, then simply point
195 * to the ftrace_stub.
197 if (ftrace_list == ops && ops->next == &ftrace_list_end) {
198 ftrace_trace_function = ftrace_stub;
199 ftrace_list = &ftrace_list_end;
200 return 0;
203 for (p = &ftrace_list; *p != &ftrace_list_end; p = &(*p)->next)
204 if (*p == ops)
205 break;
207 if (*p != ops)
208 return -1;
210 *p = (*p)->next;
212 if (ftrace_enabled) {
213 /* If we only have one func left, then call that directly */
214 if (ftrace_list->next == &ftrace_list_end) {
215 ftrace_func_t func = ftrace_list->func;
217 if (!list_empty(&ftrace_pids)) {
218 set_ftrace_pid_function(func);
219 func = ftrace_pid_func;
221 #ifdef CONFIG_HAVE_FUNCTION_TRACE_MCOUNT_TEST
222 ftrace_trace_function = func;
223 #else
224 __ftrace_trace_function = func;
225 #endif
229 return 0;
232 static void ftrace_update_pid_func(void)
234 ftrace_func_t func;
236 if (ftrace_trace_function == ftrace_stub)
237 return;
239 #ifdef CONFIG_HAVE_FUNCTION_TRACE_MCOUNT_TEST
240 func = ftrace_trace_function;
241 #else
242 func = __ftrace_trace_function;
243 #endif
245 if (!list_empty(&ftrace_pids)) {
246 set_ftrace_pid_function(func);
247 func = ftrace_pid_func;
248 } else {
249 if (func == ftrace_pid_func)
250 func = ftrace_pid_function;
253 #ifdef CONFIG_HAVE_FUNCTION_TRACE_MCOUNT_TEST
254 ftrace_trace_function = func;
255 #else
256 __ftrace_trace_function = func;
257 #endif
260 #ifdef CONFIG_FUNCTION_PROFILER
261 struct ftrace_profile {
262 struct hlist_node node;
263 unsigned long ip;
264 unsigned long counter;
265 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
266 unsigned long long time;
267 unsigned long long time_squared;
268 #endif
271 struct ftrace_profile_page {
272 struct ftrace_profile_page *next;
273 unsigned long index;
274 struct ftrace_profile records[];
277 struct ftrace_profile_stat {
278 atomic_t disabled;
279 struct hlist_head *hash;
280 struct ftrace_profile_page *pages;
281 struct ftrace_profile_page *start;
282 struct tracer_stat stat;
285 #define PROFILE_RECORDS_SIZE \
286 (PAGE_SIZE - offsetof(struct ftrace_profile_page, records))
288 #define PROFILES_PER_PAGE \
289 (PROFILE_RECORDS_SIZE / sizeof(struct ftrace_profile))
291 static int ftrace_profile_bits __read_mostly;
292 static int ftrace_profile_enabled __read_mostly;
294 /* ftrace_profile_lock - synchronize the enable and disable of the profiler */
295 static DEFINE_MUTEX(ftrace_profile_lock);
297 static DEFINE_PER_CPU(struct ftrace_profile_stat, ftrace_profile_stats);
299 #define FTRACE_PROFILE_HASH_SIZE 1024 /* must be power of 2 */
301 static void *
302 function_stat_next(void *v, int idx)
304 struct ftrace_profile *rec = v;
305 struct ftrace_profile_page *pg;
307 pg = (struct ftrace_profile_page *)((unsigned long)rec & PAGE_MASK);
309 again:
310 if (idx != 0)
311 rec++;
313 if ((void *)rec >= (void *)&pg->records[pg->index]) {
314 pg = pg->next;
315 if (!pg)
316 return NULL;
317 rec = &pg->records[0];
318 if (!rec->counter)
319 goto again;
322 return rec;
325 static void *function_stat_start(struct tracer_stat *trace)
327 struct ftrace_profile_stat *stat =
328 container_of(trace, struct ftrace_profile_stat, stat);
330 if (!stat || !stat->start)
331 return NULL;
333 return function_stat_next(&stat->start->records[0], 0);
336 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
337 /* function graph compares on total time */
338 static int function_stat_cmp(void *p1, void *p2)
340 struct ftrace_profile *a = p1;
341 struct ftrace_profile *b = p2;
343 if (a->time < b->time)
344 return -1;
345 if (a->time > b->time)
346 return 1;
347 else
348 return 0;
350 #else
351 /* not function graph compares against hits */
352 static int function_stat_cmp(void *p1, void *p2)
354 struct ftrace_profile *a = p1;
355 struct ftrace_profile *b = p2;
357 if (a->counter < b->counter)
358 return -1;
359 if (a->counter > b->counter)
360 return 1;
361 else
362 return 0;
364 #endif
366 static int function_stat_headers(struct seq_file *m)
368 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
369 seq_printf(m, " Function "
370 "Hit Time Avg s^2\n"
371 " -------- "
372 "--- ---- --- ---\n");
373 #else
374 seq_printf(m, " Function Hit\n"
375 " -------- ---\n");
376 #endif
377 return 0;
380 static int function_stat_show(struct seq_file *m, void *v)
382 struct ftrace_profile *rec = v;
383 char str[KSYM_SYMBOL_LEN];
384 int ret = 0;
385 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
386 static struct trace_seq s;
387 unsigned long long avg;
388 unsigned long long stddev;
389 #endif
390 mutex_lock(&ftrace_profile_lock);
392 /* we raced with function_profile_reset() */
393 if (unlikely(rec->counter == 0)) {
394 ret = -EBUSY;
395 goto out;
398 kallsyms_lookup(rec->ip, NULL, NULL, NULL, str);
399 seq_printf(m, " %-30.30s %10lu", str, rec->counter);
401 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
402 seq_printf(m, " ");
403 avg = rec->time;
404 do_div(avg, rec->counter);
406 /* Sample standard deviation (s^2) */
407 if (rec->counter <= 1)
408 stddev = 0;
409 else {
410 stddev = rec->time_squared - rec->counter * avg * avg;
412 * Divide only 1000 for ns^2 -> us^2 conversion.
413 * trace_print_graph_duration will divide 1000 again.
415 do_div(stddev, (rec->counter - 1) * 1000);
418 trace_seq_init(&s);
419 trace_print_graph_duration(rec->time, &s);
420 trace_seq_puts(&s, " ");
421 trace_print_graph_duration(avg, &s);
422 trace_seq_puts(&s, " ");
423 trace_print_graph_duration(stddev, &s);
424 trace_print_seq(m, &s);
425 #endif
426 seq_putc(m, '\n');
427 out:
428 mutex_unlock(&ftrace_profile_lock);
430 return ret;
433 static void ftrace_profile_reset(struct ftrace_profile_stat *stat)
435 struct ftrace_profile_page *pg;
437 pg = stat->pages = stat->start;
439 while (pg) {
440 memset(pg->records, 0, PROFILE_RECORDS_SIZE);
441 pg->index = 0;
442 pg = pg->next;
445 memset(stat->hash, 0,
446 FTRACE_PROFILE_HASH_SIZE * sizeof(struct hlist_head));
449 int ftrace_profile_pages_init(struct ftrace_profile_stat *stat)
451 struct ftrace_profile_page *pg;
452 int functions;
453 int pages;
454 int i;
456 /* If we already allocated, do nothing */
457 if (stat->pages)
458 return 0;
460 stat->pages = (void *)get_zeroed_page(GFP_KERNEL);
461 if (!stat->pages)
462 return -ENOMEM;
464 #ifdef CONFIG_DYNAMIC_FTRACE
465 functions = ftrace_update_tot_cnt;
466 #else
468 * We do not know the number of functions that exist because
469 * dynamic tracing is what counts them. With past experience
470 * we have around 20K functions. That should be more than enough.
471 * It is highly unlikely we will execute every function in
472 * the kernel.
474 functions = 20000;
475 #endif
477 pg = stat->start = stat->pages;
479 pages = DIV_ROUND_UP(functions, PROFILES_PER_PAGE);
481 for (i = 0; i < pages; i++) {
482 pg->next = (void *)get_zeroed_page(GFP_KERNEL);
483 if (!pg->next)
484 goto out_free;
485 pg = pg->next;
488 return 0;
490 out_free:
491 pg = stat->start;
492 while (pg) {
493 unsigned long tmp = (unsigned long)pg;
495 pg = pg->next;
496 free_page(tmp);
499 free_page((unsigned long)stat->pages);
500 stat->pages = NULL;
501 stat->start = NULL;
503 return -ENOMEM;
506 static int ftrace_profile_init_cpu(int cpu)
508 struct ftrace_profile_stat *stat;
509 int size;
511 stat = &per_cpu(ftrace_profile_stats, cpu);
513 if (stat->hash) {
514 /* If the profile is already created, simply reset it */
515 ftrace_profile_reset(stat);
516 return 0;
520 * We are profiling all functions, but usually only a few thousand
521 * functions are hit. We'll make a hash of 1024 items.
523 size = FTRACE_PROFILE_HASH_SIZE;
525 stat->hash = kzalloc(sizeof(struct hlist_head) * size, GFP_KERNEL);
527 if (!stat->hash)
528 return -ENOMEM;
530 if (!ftrace_profile_bits) {
531 size--;
533 for (; size; size >>= 1)
534 ftrace_profile_bits++;
537 /* Preallocate the function profiling pages */
538 if (ftrace_profile_pages_init(stat) < 0) {
539 kfree(stat->hash);
540 stat->hash = NULL;
541 return -ENOMEM;
544 return 0;
547 static int ftrace_profile_init(void)
549 int cpu;
550 int ret = 0;
552 for_each_online_cpu(cpu) {
553 ret = ftrace_profile_init_cpu(cpu);
554 if (ret)
555 break;
558 return ret;
561 /* interrupts must be disabled */
562 static struct ftrace_profile *
563 ftrace_find_profiled_func(struct ftrace_profile_stat *stat, unsigned long ip)
565 struct ftrace_profile *rec;
566 struct hlist_head *hhd;
567 struct hlist_node *n;
568 unsigned long key;
570 key = hash_long(ip, ftrace_profile_bits);
571 hhd = &stat->hash[key];
573 if (hlist_empty(hhd))
574 return NULL;
576 hlist_for_each_entry_rcu(rec, n, hhd, node) {
577 if (rec->ip == ip)
578 return rec;
581 return NULL;
584 static void ftrace_add_profile(struct ftrace_profile_stat *stat,
585 struct ftrace_profile *rec)
587 unsigned long key;
589 key = hash_long(rec->ip, ftrace_profile_bits);
590 hlist_add_head_rcu(&rec->node, &stat->hash[key]);
594 * The memory is already allocated, this simply finds a new record to use.
596 static struct ftrace_profile *
597 ftrace_profile_alloc(struct ftrace_profile_stat *stat, unsigned long ip)
599 struct ftrace_profile *rec = NULL;
601 /* prevent recursion (from NMIs) */
602 if (atomic_inc_return(&stat->disabled) != 1)
603 goto out;
606 * Try to find the function again since an NMI
607 * could have added it
609 rec = ftrace_find_profiled_func(stat, ip);
610 if (rec)
611 goto out;
613 if (stat->pages->index == PROFILES_PER_PAGE) {
614 if (!stat->pages->next)
615 goto out;
616 stat->pages = stat->pages->next;
619 rec = &stat->pages->records[stat->pages->index++];
620 rec->ip = ip;
621 ftrace_add_profile(stat, rec);
623 out:
624 atomic_dec(&stat->disabled);
626 return rec;
629 static void
630 function_profile_call(unsigned long ip, unsigned long parent_ip)
632 struct ftrace_profile_stat *stat;
633 struct ftrace_profile *rec;
634 unsigned long flags;
636 if (!ftrace_profile_enabled)
637 return;
639 local_irq_save(flags);
641 stat = &__get_cpu_var(ftrace_profile_stats);
642 if (!stat->hash || !ftrace_profile_enabled)
643 goto out;
645 rec = ftrace_find_profiled_func(stat, ip);
646 if (!rec) {
647 rec = ftrace_profile_alloc(stat, ip);
648 if (!rec)
649 goto out;
652 rec->counter++;
653 out:
654 local_irq_restore(flags);
657 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
658 static int profile_graph_entry(struct ftrace_graph_ent *trace)
660 function_profile_call(trace->func, 0);
661 return 1;
664 static void profile_graph_return(struct ftrace_graph_ret *trace)
666 struct ftrace_profile_stat *stat;
667 unsigned long long calltime;
668 struct ftrace_profile *rec;
669 unsigned long flags;
671 local_irq_save(flags);
672 stat = &__get_cpu_var(ftrace_profile_stats);
673 if (!stat->hash || !ftrace_profile_enabled)
674 goto out;
676 /* If the calltime was zero'd ignore it */
677 if (!trace->calltime)
678 goto out;
680 calltime = trace->rettime - trace->calltime;
682 if (!(trace_flags & TRACE_ITER_GRAPH_TIME)) {
683 int index;
685 index = trace->depth;
687 /* Append this call time to the parent time to subtract */
688 if (index)
689 current->ret_stack[index - 1].subtime += calltime;
691 if (current->ret_stack[index].subtime < calltime)
692 calltime -= current->ret_stack[index].subtime;
693 else
694 calltime = 0;
697 rec = ftrace_find_profiled_func(stat, trace->func);
698 if (rec) {
699 rec->time += calltime;
700 rec->time_squared += calltime * calltime;
703 out:
704 local_irq_restore(flags);
707 static int register_ftrace_profiler(void)
709 return register_ftrace_graph(&profile_graph_return,
710 &profile_graph_entry);
713 static void unregister_ftrace_profiler(void)
715 unregister_ftrace_graph();
717 #else
718 static struct ftrace_ops ftrace_profile_ops __read_mostly =
720 .func = function_profile_call,
723 static int register_ftrace_profiler(void)
725 return register_ftrace_function(&ftrace_profile_ops);
728 static void unregister_ftrace_profiler(void)
730 unregister_ftrace_function(&ftrace_profile_ops);
732 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
734 static ssize_t
735 ftrace_profile_write(struct file *filp, const char __user *ubuf,
736 size_t cnt, loff_t *ppos)
738 unsigned long val;
739 char buf[64]; /* big enough to hold a number */
740 int ret;
742 if (cnt >= sizeof(buf))
743 return -EINVAL;
745 if (copy_from_user(&buf, ubuf, cnt))
746 return -EFAULT;
748 buf[cnt] = 0;
750 ret = strict_strtoul(buf, 10, &val);
751 if (ret < 0)
752 return ret;
754 val = !!val;
756 mutex_lock(&ftrace_profile_lock);
757 if (ftrace_profile_enabled ^ val) {
758 if (val) {
759 ret = ftrace_profile_init();
760 if (ret < 0) {
761 cnt = ret;
762 goto out;
765 ret = register_ftrace_profiler();
766 if (ret < 0) {
767 cnt = ret;
768 goto out;
770 ftrace_profile_enabled = 1;
771 } else {
772 ftrace_profile_enabled = 0;
774 * unregister_ftrace_profiler calls stop_machine
775 * so this acts like an synchronize_sched.
777 unregister_ftrace_profiler();
780 out:
781 mutex_unlock(&ftrace_profile_lock);
783 *ppos += cnt;
785 return cnt;
788 static ssize_t
789 ftrace_profile_read(struct file *filp, char __user *ubuf,
790 size_t cnt, loff_t *ppos)
792 char buf[64]; /* big enough to hold a number */
793 int r;
795 r = sprintf(buf, "%u\n", ftrace_profile_enabled);
796 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
799 static const struct file_operations ftrace_profile_fops = {
800 .open = tracing_open_generic,
801 .read = ftrace_profile_read,
802 .write = ftrace_profile_write,
805 /* used to initialize the real stat files */
806 static struct tracer_stat function_stats __initdata = {
807 .name = "functions",
808 .stat_start = function_stat_start,
809 .stat_next = function_stat_next,
810 .stat_cmp = function_stat_cmp,
811 .stat_headers = function_stat_headers,
812 .stat_show = function_stat_show
815 static __init void ftrace_profile_debugfs(struct dentry *d_tracer)
817 struct ftrace_profile_stat *stat;
818 struct dentry *entry;
819 char *name;
820 int ret;
821 int cpu;
823 for_each_possible_cpu(cpu) {
824 stat = &per_cpu(ftrace_profile_stats, cpu);
826 /* allocate enough for function name + cpu number */
827 name = kmalloc(32, GFP_KERNEL);
828 if (!name) {
830 * The files created are permanent, if something happens
831 * we still do not free memory.
833 WARN(1,
834 "Could not allocate stat file for cpu %d\n",
835 cpu);
836 return;
838 stat->stat = function_stats;
839 snprintf(name, 32, "function%d", cpu);
840 stat->stat.name = name;
841 ret = register_stat_tracer(&stat->stat);
842 if (ret) {
843 WARN(1,
844 "Could not register function stat for cpu %d\n",
845 cpu);
846 kfree(name);
847 return;
851 entry = debugfs_create_file("function_profile_enabled", 0644,
852 d_tracer, NULL, &ftrace_profile_fops);
853 if (!entry)
854 pr_warning("Could not create debugfs "
855 "'function_profile_enabled' entry\n");
858 #else /* CONFIG_FUNCTION_PROFILER */
859 static __init void ftrace_profile_debugfs(struct dentry *d_tracer)
862 #endif /* CONFIG_FUNCTION_PROFILER */
864 static struct pid * const ftrace_swapper_pid = &init_struct_pid;
866 #ifdef CONFIG_DYNAMIC_FTRACE
868 #ifndef CONFIG_FTRACE_MCOUNT_RECORD
869 # error Dynamic ftrace depends on MCOUNT_RECORD
870 #endif
872 static struct hlist_head ftrace_func_hash[FTRACE_FUNC_HASHSIZE] __read_mostly;
874 struct ftrace_func_probe {
875 struct hlist_node node;
876 struct ftrace_probe_ops *ops;
877 unsigned long flags;
878 unsigned long ip;
879 void *data;
880 struct rcu_head rcu;
883 enum {
884 FTRACE_ENABLE_CALLS = (1 << 0),
885 FTRACE_DISABLE_CALLS = (1 << 1),
886 FTRACE_UPDATE_TRACE_FUNC = (1 << 2),
887 FTRACE_ENABLE_MCOUNT = (1 << 3),
888 FTRACE_DISABLE_MCOUNT = (1 << 4),
889 FTRACE_START_FUNC_RET = (1 << 5),
890 FTRACE_STOP_FUNC_RET = (1 << 6),
893 static int ftrace_filtered;
895 static struct dyn_ftrace *ftrace_new_addrs;
897 static DEFINE_MUTEX(ftrace_regex_lock);
899 struct ftrace_page {
900 struct ftrace_page *next;
901 int index;
902 struct dyn_ftrace records[];
905 #define ENTRIES_PER_PAGE \
906 ((PAGE_SIZE - sizeof(struct ftrace_page)) / sizeof(struct dyn_ftrace))
908 /* estimate from running different kernels */
909 #define NR_TO_INIT 10000
911 static struct ftrace_page *ftrace_pages_start;
912 static struct ftrace_page *ftrace_pages;
914 static struct dyn_ftrace *ftrace_free_records;
917 * This is a double for. Do not use 'break' to break out of the loop,
918 * you must use a goto.
920 #define do_for_each_ftrace_rec(pg, rec) \
921 for (pg = ftrace_pages_start; pg; pg = pg->next) { \
922 int _____i; \
923 for (_____i = 0; _____i < pg->index; _____i++) { \
924 rec = &pg->records[_____i];
926 #define while_for_each_ftrace_rec() \
930 static void ftrace_free_rec(struct dyn_ftrace *rec)
932 rec->freelist = ftrace_free_records;
933 ftrace_free_records = rec;
934 rec->flags |= FTRACE_FL_FREE;
937 static struct dyn_ftrace *ftrace_alloc_dyn_node(unsigned long ip)
939 struct dyn_ftrace *rec;
941 /* First check for freed records */
942 if (ftrace_free_records) {
943 rec = ftrace_free_records;
945 if (unlikely(!(rec->flags & FTRACE_FL_FREE))) {
946 FTRACE_WARN_ON_ONCE(1);
947 ftrace_free_records = NULL;
948 return NULL;
951 ftrace_free_records = rec->freelist;
952 memset(rec, 0, sizeof(*rec));
953 return rec;
956 if (ftrace_pages->index == ENTRIES_PER_PAGE) {
957 if (!ftrace_pages->next) {
958 /* allocate another page */
959 ftrace_pages->next =
960 (void *)get_zeroed_page(GFP_KERNEL);
961 if (!ftrace_pages->next)
962 return NULL;
964 ftrace_pages = ftrace_pages->next;
967 return &ftrace_pages->records[ftrace_pages->index++];
970 static struct dyn_ftrace *
971 ftrace_record_ip(unsigned long ip)
973 struct dyn_ftrace *rec;
975 if (ftrace_disabled)
976 return NULL;
978 rec = ftrace_alloc_dyn_node(ip);
979 if (!rec)
980 return NULL;
982 rec->ip = ip;
983 rec->newlist = ftrace_new_addrs;
984 ftrace_new_addrs = rec;
986 return rec;
989 static void print_ip_ins(const char *fmt, unsigned char *p)
991 int i;
993 printk(KERN_CONT "%s", fmt);
995 for (i = 0; i < MCOUNT_INSN_SIZE; i++)
996 printk(KERN_CONT "%s%02x", i ? ":" : "", p[i]);
999 static void ftrace_bug(int failed, unsigned long ip)
1001 switch (failed) {
1002 case -EFAULT:
1003 FTRACE_WARN_ON_ONCE(1);
1004 pr_info("ftrace faulted on modifying ");
1005 print_ip_sym(ip);
1006 break;
1007 case -EINVAL:
1008 FTRACE_WARN_ON_ONCE(1);
1009 pr_info("ftrace failed to modify ");
1010 print_ip_sym(ip);
1011 print_ip_ins(" actual: ", (unsigned char *)ip);
1012 printk(KERN_CONT "\n");
1013 break;
1014 case -EPERM:
1015 FTRACE_WARN_ON_ONCE(1);
1016 pr_info("ftrace faulted on writing ");
1017 print_ip_sym(ip);
1018 break;
1019 default:
1020 FTRACE_WARN_ON_ONCE(1);
1021 pr_info("ftrace faulted on unknown error ");
1022 print_ip_sym(ip);
1027 /* Return 1 if the address range is reserved for ftrace */
1028 int ftrace_text_reserved(void *start, void *end)
1030 struct dyn_ftrace *rec;
1031 struct ftrace_page *pg;
1033 do_for_each_ftrace_rec(pg, rec) {
1034 if (rec->ip <= (unsigned long)end &&
1035 rec->ip + MCOUNT_INSN_SIZE > (unsigned long)start)
1036 return 1;
1037 } while_for_each_ftrace_rec();
1038 return 0;
1042 static int
1043 __ftrace_replace_code(struct dyn_ftrace *rec, int enable)
1045 unsigned long ftrace_addr;
1046 unsigned long flag = 0UL;
1048 ftrace_addr = (unsigned long)FTRACE_ADDR;
1051 * If this record is not to be traced or we want to disable it,
1052 * then disable it.
1054 * If we want to enable it and filtering is off, then enable it.
1056 * If we want to enable it and filtering is on, enable it only if
1057 * it's filtered
1059 if (enable && !(rec->flags & FTRACE_FL_NOTRACE)) {
1060 if (!ftrace_filtered || (rec->flags & FTRACE_FL_FILTER))
1061 flag = FTRACE_FL_ENABLED;
1064 /* If the state of this record hasn't changed, then do nothing */
1065 if ((rec->flags & FTRACE_FL_ENABLED) == flag)
1066 return 0;
1068 if (flag) {
1069 rec->flags |= FTRACE_FL_ENABLED;
1070 return ftrace_make_call(rec, ftrace_addr);
1073 rec->flags &= ~FTRACE_FL_ENABLED;
1074 return ftrace_make_nop(NULL, rec, ftrace_addr);
1077 static void ftrace_replace_code(int enable)
1079 struct dyn_ftrace *rec;
1080 struct ftrace_page *pg;
1081 int failed;
1083 do_for_each_ftrace_rec(pg, rec) {
1085 * Skip over free records, records that have
1086 * failed and not converted.
1088 if (rec->flags & FTRACE_FL_FREE ||
1089 rec->flags & FTRACE_FL_FAILED ||
1090 !(rec->flags & FTRACE_FL_CONVERTED))
1091 continue;
1093 failed = __ftrace_replace_code(rec, enable);
1094 if (failed) {
1095 rec->flags |= FTRACE_FL_FAILED;
1096 ftrace_bug(failed, rec->ip);
1097 /* Stop processing */
1098 return;
1100 } while_for_each_ftrace_rec();
1103 static int
1104 ftrace_code_disable(struct module *mod, struct dyn_ftrace *rec)
1106 unsigned long ip;
1107 int ret;
1109 ip = rec->ip;
1111 ret = ftrace_make_nop(mod, rec, MCOUNT_ADDR);
1112 if (ret) {
1113 ftrace_bug(ret, ip);
1114 rec->flags |= FTRACE_FL_FAILED;
1115 return 0;
1117 return 1;
1121 * archs can override this function if they must do something
1122 * before the modifying code is performed.
1124 int __weak ftrace_arch_code_modify_prepare(void)
1126 return 0;
1130 * archs can override this function if they must do something
1131 * after the modifying code is performed.
1133 int __weak ftrace_arch_code_modify_post_process(void)
1135 return 0;
1138 static int __ftrace_modify_code(void *data)
1140 int *command = data;
1142 if (*command & FTRACE_ENABLE_CALLS)
1143 ftrace_replace_code(1);
1144 else if (*command & FTRACE_DISABLE_CALLS)
1145 ftrace_replace_code(0);
1147 if (*command & FTRACE_UPDATE_TRACE_FUNC)
1148 ftrace_update_ftrace_func(ftrace_trace_function);
1150 if (*command & FTRACE_START_FUNC_RET)
1151 ftrace_enable_ftrace_graph_caller();
1152 else if (*command & FTRACE_STOP_FUNC_RET)
1153 ftrace_disable_ftrace_graph_caller();
1155 return 0;
1158 static void ftrace_run_update_code(int command)
1160 int ret;
1162 ret = ftrace_arch_code_modify_prepare();
1163 FTRACE_WARN_ON(ret);
1164 if (ret)
1165 return;
1167 stop_machine(__ftrace_modify_code, &command, NULL);
1169 ret = ftrace_arch_code_modify_post_process();
1170 FTRACE_WARN_ON(ret);
1173 static ftrace_func_t saved_ftrace_func;
1174 static int ftrace_start_up;
1176 static void ftrace_startup_enable(int command)
1178 if (saved_ftrace_func != ftrace_trace_function) {
1179 saved_ftrace_func = ftrace_trace_function;
1180 command |= FTRACE_UPDATE_TRACE_FUNC;
1183 if (!command || !ftrace_enabled)
1184 return;
1186 ftrace_run_update_code(command);
1189 static void ftrace_startup(int command)
1191 if (unlikely(ftrace_disabled))
1192 return;
1194 ftrace_start_up++;
1195 command |= FTRACE_ENABLE_CALLS;
1197 ftrace_startup_enable(command);
1200 static void ftrace_shutdown(int command)
1202 if (unlikely(ftrace_disabled))
1203 return;
1205 ftrace_start_up--;
1207 * Just warn in case of unbalance, no need to kill ftrace, it's not
1208 * critical but the ftrace_call callers may be never nopped again after
1209 * further ftrace uses.
1211 WARN_ON_ONCE(ftrace_start_up < 0);
1213 if (!ftrace_start_up)
1214 command |= FTRACE_DISABLE_CALLS;
1216 if (saved_ftrace_func != ftrace_trace_function) {
1217 saved_ftrace_func = ftrace_trace_function;
1218 command |= FTRACE_UPDATE_TRACE_FUNC;
1221 if (!command || !ftrace_enabled)
1222 return;
1224 ftrace_run_update_code(command);
1227 static void ftrace_startup_sysctl(void)
1229 int command = FTRACE_ENABLE_MCOUNT;
1231 if (unlikely(ftrace_disabled))
1232 return;
1234 /* Force update next time */
1235 saved_ftrace_func = NULL;
1236 /* ftrace_start_up is true if we want ftrace running */
1237 if (ftrace_start_up)
1238 command |= FTRACE_ENABLE_CALLS;
1240 ftrace_run_update_code(command);
1243 static void ftrace_shutdown_sysctl(void)
1245 int command = FTRACE_DISABLE_MCOUNT;
1247 if (unlikely(ftrace_disabled))
1248 return;
1250 /* ftrace_start_up is true if ftrace is running */
1251 if (ftrace_start_up)
1252 command |= FTRACE_DISABLE_CALLS;
1254 ftrace_run_update_code(command);
1257 static cycle_t ftrace_update_time;
1258 static unsigned long ftrace_update_cnt;
1259 unsigned long ftrace_update_tot_cnt;
1261 static int ftrace_update_code(struct module *mod)
1263 struct dyn_ftrace *p;
1264 cycle_t start, stop;
1266 start = ftrace_now(raw_smp_processor_id());
1267 ftrace_update_cnt = 0;
1269 while (ftrace_new_addrs) {
1271 /* If something went wrong, bail without enabling anything */
1272 if (unlikely(ftrace_disabled))
1273 return -1;
1275 p = ftrace_new_addrs;
1276 ftrace_new_addrs = p->newlist;
1277 p->flags = 0L;
1280 * Do the initial record convertion from mcount jump
1281 * to the NOP instructions.
1283 if (!ftrace_code_disable(mod, p)) {
1284 ftrace_free_rec(p);
1285 continue;
1288 p->flags |= FTRACE_FL_CONVERTED;
1289 ftrace_update_cnt++;
1292 * If the tracing is enabled, go ahead and enable the record.
1294 * The reason not to enable the record immediatelly is the
1295 * inherent check of ftrace_make_nop/ftrace_make_call for
1296 * correct previous instructions. Making first the NOP
1297 * conversion puts the module to the correct state, thus
1298 * passing the ftrace_make_call check.
1300 if (ftrace_start_up) {
1301 int failed = __ftrace_replace_code(p, 1);
1302 if (failed) {
1303 ftrace_bug(failed, p->ip);
1304 ftrace_free_rec(p);
1309 stop = ftrace_now(raw_smp_processor_id());
1310 ftrace_update_time = stop - start;
1311 ftrace_update_tot_cnt += ftrace_update_cnt;
1313 return 0;
1316 static int __init ftrace_dyn_table_alloc(unsigned long num_to_init)
1318 struct ftrace_page *pg;
1319 int cnt;
1320 int i;
1322 /* allocate a few pages */
1323 ftrace_pages_start = (void *)get_zeroed_page(GFP_KERNEL);
1324 if (!ftrace_pages_start)
1325 return -1;
1328 * Allocate a few more pages.
1330 * TODO: have some parser search vmlinux before
1331 * final linking to find all calls to ftrace.
1332 * Then we can:
1333 * a) know how many pages to allocate.
1334 * and/or
1335 * b) set up the table then.
1337 * The dynamic code is still necessary for
1338 * modules.
1341 pg = ftrace_pages = ftrace_pages_start;
1343 cnt = num_to_init / ENTRIES_PER_PAGE;
1344 pr_info("ftrace: allocating %ld entries in %d pages\n",
1345 num_to_init, cnt + 1);
1347 for (i = 0; i < cnt; i++) {
1348 pg->next = (void *)get_zeroed_page(GFP_KERNEL);
1350 /* If we fail, we'll try later anyway */
1351 if (!pg->next)
1352 break;
1354 pg = pg->next;
1357 return 0;
1360 enum {
1361 FTRACE_ITER_FILTER = (1 << 0),
1362 FTRACE_ITER_NOTRACE = (1 << 1),
1363 FTRACE_ITER_FAILURES = (1 << 2),
1364 FTRACE_ITER_PRINTALL = (1 << 3),
1365 FTRACE_ITER_HASH = (1 << 4),
1368 #define FTRACE_BUFF_MAX (KSYM_SYMBOL_LEN+4) /* room for wildcards */
1370 struct ftrace_iterator {
1371 loff_t func_pos;
1372 struct ftrace_page *pg;
1373 struct dyn_ftrace *func;
1374 struct ftrace_func_probe *probe;
1375 struct trace_parser parser;
1376 int hidx;
1377 int idx;
1378 unsigned flags;
1381 static void *
1382 t_hash_next(struct seq_file *m, loff_t *pos)
1384 struct ftrace_iterator *iter = m->private;
1385 struct hlist_node *hnd = NULL;
1386 struct hlist_head *hhd;
1388 WARN_ON(!(iter->flags & FTRACE_ITER_HASH));
1390 (*pos)++;
1392 if (iter->probe)
1393 hnd = &iter->probe->node;
1394 retry:
1395 if (iter->hidx >= FTRACE_FUNC_HASHSIZE)
1396 return NULL;
1398 hhd = &ftrace_func_hash[iter->hidx];
1400 if (hlist_empty(hhd)) {
1401 iter->hidx++;
1402 hnd = NULL;
1403 goto retry;
1406 if (!hnd)
1407 hnd = hhd->first;
1408 else {
1409 hnd = hnd->next;
1410 if (!hnd) {
1411 iter->hidx++;
1412 goto retry;
1416 if (WARN_ON_ONCE(!hnd))
1417 return NULL;
1419 iter->probe = hlist_entry(hnd, struct ftrace_func_probe, node);
1421 return iter;
1424 static void *t_hash_start(struct seq_file *m, loff_t *pos)
1426 struct ftrace_iterator *iter = m->private;
1427 void *p = NULL;
1428 loff_t l;
1430 if (!(iter->flags & FTRACE_ITER_HASH))
1431 iter->func_pos = *pos;
1433 if (iter->func_pos > *pos)
1434 return NULL;
1436 iter->flags |= FTRACE_ITER_HASH;
1438 iter->hidx = 0;
1439 for (l = 0; l <= (*pos - iter->func_pos); ) {
1440 p = t_hash_next(m, &l);
1441 if (!p)
1442 break;
1444 if (!p)
1445 return NULL;
1447 return iter;
1450 static int
1451 t_hash_show(struct seq_file *m, struct ftrace_iterator *iter)
1453 struct ftrace_func_probe *rec;
1455 rec = iter->probe;
1456 if (WARN_ON_ONCE(!rec))
1457 return -EIO;
1459 if (rec->ops->print)
1460 return rec->ops->print(m, rec->ip, rec->ops, rec->data);
1462 seq_printf(m, "%ps:%ps", (void *)rec->ip, (void *)rec->ops->func);
1464 if (rec->data)
1465 seq_printf(m, ":%p", rec->data);
1466 seq_putc(m, '\n');
1468 return 0;
1471 static void *
1472 t_next(struct seq_file *m, void *v, loff_t *pos)
1474 struct ftrace_iterator *iter = m->private;
1475 struct dyn_ftrace *rec = NULL;
1477 if (iter->flags & FTRACE_ITER_HASH)
1478 return t_hash_next(m, pos);
1480 (*pos)++;
1482 if (iter->flags & FTRACE_ITER_PRINTALL)
1483 return NULL;
1485 retry:
1486 if (iter->idx >= iter->pg->index) {
1487 if (iter->pg->next) {
1488 iter->pg = iter->pg->next;
1489 iter->idx = 0;
1490 goto retry;
1492 } else {
1493 rec = &iter->pg->records[iter->idx++];
1494 if ((rec->flags & FTRACE_FL_FREE) ||
1496 (!(iter->flags & FTRACE_ITER_FAILURES) &&
1497 (rec->flags & FTRACE_FL_FAILED)) ||
1499 ((iter->flags & FTRACE_ITER_FAILURES) &&
1500 !(rec->flags & FTRACE_FL_FAILED)) ||
1502 ((iter->flags & FTRACE_ITER_FILTER) &&
1503 !(rec->flags & FTRACE_FL_FILTER)) ||
1505 ((iter->flags & FTRACE_ITER_NOTRACE) &&
1506 !(rec->flags & FTRACE_FL_NOTRACE))) {
1507 rec = NULL;
1508 goto retry;
1512 if (!rec)
1513 return NULL;
1515 iter->func = rec;
1517 return iter;
1520 static void *t_start(struct seq_file *m, loff_t *pos)
1522 struct ftrace_iterator *iter = m->private;
1523 void *p = NULL;
1524 loff_t l;
1526 mutex_lock(&ftrace_lock);
1528 * For set_ftrace_filter reading, if we have the filter
1529 * off, we can short cut and just print out that all
1530 * functions are enabled.
1532 if (iter->flags & FTRACE_ITER_FILTER && !ftrace_filtered) {
1533 if (*pos > 0)
1534 return t_hash_start(m, pos);
1535 iter->flags |= FTRACE_ITER_PRINTALL;
1536 /* reset in case of seek/pread */
1537 iter->flags &= ~FTRACE_ITER_HASH;
1538 return iter;
1541 if (iter->flags & FTRACE_ITER_HASH)
1542 return t_hash_start(m, pos);
1544 iter->pg = ftrace_pages_start;
1545 iter->idx = 0;
1546 for (l = 0; l <= *pos; ) {
1547 p = t_next(m, p, &l);
1548 if (!p)
1549 break;
1552 if (!p) {
1553 if (iter->flags & FTRACE_ITER_FILTER)
1554 return t_hash_start(m, pos);
1556 return NULL;
1559 return iter;
1562 static void t_stop(struct seq_file *m, void *p)
1564 mutex_unlock(&ftrace_lock);
1567 static int t_show(struct seq_file *m, void *v)
1569 struct ftrace_iterator *iter = m->private;
1570 struct dyn_ftrace *rec;
1572 if (iter->flags & FTRACE_ITER_HASH)
1573 return t_hash_show(m, iter);
1575 if (iter->flags & FTRACE_ITER_PRINTALL) {
1576 seq_printf(m, "#### all functions enabled ####\n");
1577 return 0;
1580 rec = iter->func;
1582 if (!rec)
1583 return 0;
1585 seq_printf(m, "%ps\n", (void *)rec->ip);
1587 return 0;
1590 static const struct seq_operations show_ftrace_seq_ops = {
1591 .start = t_start,
1592 .next = t_next,
1593 .stop = t_stop,
1594 .show = t_show,
1597 static int
1598 ftrace_avail_open(struct inode *inode, struct file *file)
1600 struct ftrace_iterator *iter;
1601 int ret;
1603 if (unlikely(ftrace_disabled))
1604 return -ENODEV;
1606 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
1607 if (!iter)
1608 return -ENOMEM;
1610 iter->pg = ftrace_pages_start;
1612 ret = seq_open(file, &show_ftrace_seq_ops);
1613 if (!ret) {
1614 struct seq_file *m = file->private_data;
1616 m->private = iter;
1617 } else {
1618 kfree(iter);
1621 return ret;
1624 static int
1625 ftrace_failures_open(struct inode *inode, struct file *file)
1627 int ret;
1628 struct seq_file *m;
1629 struct ftrace_iterator *iter;
1631 ret = ftrace_avail_open(inode, file);
1632 if (!ret) {
1633 m = (struct seq_file *)file->private_data;
1634 iter = (struct ftrace_iterator *)m->private;
1635 iter->flags = FTRACE_ITER_FAILURES;
1638 return ret;
1642 static void ftrace_filter_reset(int enable)
1644 struct ftrace_page *pg;
1645 struct dyn_ftrace *rec;
1646 unsigned long type = enable ? FTRACE_FL_FILTER : FTRACE_FL_NOTRACE;
1648 mutex_lock(&ftrace_lock);
1649 if (enable)
1650 ftrace_filtered = 0;
1651 do_for_each_ftrace_rec(pg, rec) {
1652 if (rec->flags & FTRACE_FL_FAILED)
1653 continue;
1654 rec->flags &= ~type;
1655 } while_for_each_ftrace_rec();
1656 mutex_unlock(&ftrace_lock);
1659 static int
1660 ftrace_regex_open(struct inode *inode, struct file *file, int enable)
1662 struct ftrace_iterator *iter;
1663 int ret = 0;
1665 if (unlikely(ftrace_disabled))
1666 return -ENODEV;
1668 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
1669 if (!iter)
1670 return -ENOMEM;
1672 if (trace_parser_get_init(&iter->parser, FTRACE_BUFF_MAX)) {
1673 kfree(iter);
1674 return -ENOMEM;
1677 mutex_lock(&ftrace_regex_lock);
1678 if ((file->f_mode & FMODE_WRITE) &&
1679 (file->f_flags & O_TRUNC))
1680 ftrace_filter_reset(enable);
1682 if (file->f_mode & FMODE_READ) {
1683 iter->pg = ftrace_pages_start;
1684 iter->flags = enable ? FTRACE_ITER_FILTER :
1685 FTRACE_ITER_NOTRACE;
1687 ret = seq_open(file, &show_ftrace_seq_ops);
1688 if (!ret) {
1689 struct seq_file *m = file->private_data;
1690 m->private = iter;
1691 } else {
1692 trace_parser_put(&iter->parser);
1693 kfree(iter);
1695 } else
1696 file->private_data = iter;
1697 mutex_unlock(&ftrace_regex_lock);
1699 return ret;
1702 static int
1703 ftrace_filter_open(struct inode *inode, struct file *file)
1705 return ftrace_regex_open(inode, file, 1);
1708 static int
1709 ftrace_notrace_open(struct inode *inode, struct file *file)
1711 return ftrace_regex_open(inode, file, 0);
1714 static loff_t
1715 ftrace_regex_lseek(struct file *file, loff_t offset, int origin)
1717 loff_t ret;
1719 if (file->f_mode & FMODE_READ)
1720 ret = seq_lseek(file, offset, origin);
1721 else
1722 file->f_pos = ret = 1;
1724 return ret;
1727 static int ftrace_match(char *str, char *regex, int len, int type)
1729 int matched = 0;
1730 int slen;
1732 switch (type) {
1733 case MATCH_FULL:
1734 if (strcmp(str, regex) == 0)
1735 matched = 1;
1736 break;
1737 case MATCH_FRONT_ONLY:
1738 if (strncmp(str, regex, len) == 0)
1739 matched = 1;
1740 break;
1741 case MATCH_MIDDLE_ONLY:
1742 if (strstr(str, regex))
1743 matched = 1;
1744 break;
1745 case MATCH_END_ONLY:
1746 slen = strlen(str);
1747 if (slen >= len && memcmp(str + slen - len, regex, len) == 0)
1748 matched = 1;
1749 break;
1752 return matched;
1755 static int
1756 ftrace_match_record(struct dyn_ftrace *rec, char *regex, int len, int type)
1758 char str[KSYM_SYMBOL_LEN];
1760 kallsyms_lookup(rec->ip, NULL, NULL, NULL, str);
1761 return ftrace_match(str, regex, len, type);
1764 static int ftrace_match_records(char *buff, int len, int enable)
1766 unsigned int search_len;
1767 struct ftrace_page *pg;
1768 struct dyn_ftrace *rec;
1769 unsigned long flag;
1770 char *search;
1771 int type;
1772 int not;
1773 int found = 0;
1775 flag = enable ? FTRACE_FL_FILTER : FTRACE_FL_NOTRACE;
1776 type = filter_parse_regex(buff, len, &search, &not);
1778 search_len = strlen(search);
1780 mutex_lock(&ftrace_lock);
1781 do_for_each_ftrace_rec(pg, rec) {
1783 if (rec->flags & FTRACE_FL_FAILED)
1784 continue;
1786 if (ftrace_match_record(rec, search, search_len, type)) {
1787 if (not)
1788 rec->flags &= ~flag;
1789 else
1790 rec->flags |= flag;
1791 found = 1;
1794 * Only enable filtering if we have a function that
1795 * is filtered on.
1797 if (enable && (rec->flags & FTRACE_FL_FILTER))
1798 ftrace_filtered = 1;
1799 } while_for_each_ftrace_rec();
1800 mutex_unlock(&ftrace_lock);
1802 return found;
1805 static int
1806 ftrace_match_module_record(struct dyn_ftrace *rec, char *mod,
1807 char *regex, int len, int type)
1809 char str[KSYM_SYMBOL_LEN];
1810 char *modname;
1812 kallsyms_lookup(rec->ip, NULL, NULL, &modname, str);
1814 if (!modname || strcmp(modname, mod))
1815 return 0;
1817 /* blank search means to match all funcs in the mod */
1818 if (len)
1819 return ftrace_match(str, regex, len, type);
1820 else
1821 return 1;
1824 static int ftrace_match_module_records(char *buff, char *mod, int enable)
1826 unsigned search_len = 0;
1827 struct ftrace_page *pg;
1828 struct dyn_ftrace *rec;
1829 int type = MATCH_FULL;
1830 char *search = buff;
1831 unsigned long flag;
1832 int not = 0;
1833 int found = 0;
1835 flag = enable ? FTRACE_FL_FILTER : FTRACE_FL_NOTRACE;
1837 /* blank or '*' mean the same */
1838 if (strcmp(buff, "*") == 0)
1839 buff[0] = 0;
1841 /* handle the case of 'dont filter this module' */
1842 if (strcmp(buff, "!") == 0 || strcmp(buff, "!*") == 0) {
1843 buff[0] = 0;
1844 not = 1;
1847 if (strlen(buff)) {
1848 type = filter_parse_regex(buff, strlen(buff), &search, &not);
1849 search_len = strlen(search);
1852 mutex_lock(&ftrace_lock);
1853 do_for_each_ftrace_rec(pg, rec) {
1855 if (rec->flags & FTRACE_FL_FAILED)
1856 continue;
1858 if (ftrace_match_module_record(rec, mod,
1859 search, search_len, type)) {
1860 if (not)
1861 rec->flags &= ~flag;
1862 else
1863 rec->flags |= flag;
1864 found = 1;
1866 if (enable && (rec->flags & FTRACE_FL_FILTER))
1867 ftrace_filtered = 1;
1869 } while_for_each_ftrace_rec();
1870 mutex_unlock(&ftrace_lock);
1872 return found;
1876 * We register the module command as a template to show others how
1877 * to register the a command as well.
1880 static int
1881 ftrace_mod_callback(char *func, char *cmd, char *param, int enable)
1883 char *mod;
1886 * cmd == 'mod' because we only registered this func
1887 * for the 'mod' ftrace_func_command.
1888 * But if you register one func with multiple commands,
1889 * you can tell which command was used by the cmd
1890 * parameter.
1893 /* we must have a module name */
1894 if (!param)
1895 return -EINVAL;
1897 mod = strsep(&param, ":");
1898 if (!strlen(mod))
1899 return -EINVAL;
1901 if (ftrace_match_module_records(func, mod, enable))
1902 return 0;
1903 return -EINVAL;
1906 static struct ftrace_func_command ftrace_mod_cmd = {
1907 .name = "mod",
1908 .func = ftrace_mod_callback,
1911 static int __init ftrace_mod_cmd_init(void)
1913 return register_ftrace_command(&ftrace_mod_cmd);
1915 device_initcall(ftrace_mod_cmd_init);
1917 static void
1918 function_trace_probe_call(unsigned long ip, unsigned long parent_ip)
1920 struct ftrace_func_probe *entry;
1921 struct hlist_head *hhd;
1922 struct hlist_node *n;
1923 unsigned long key;
1925 key = hash_long(ip, FTRACE_HASH_BITS);
1927 hhd = &ftrace_func_hash[key];
1929 if (hlist_empty(hhd))
1930 return;
1933 * Disable preemption for these calls to prevent a RCU grace
1934 * period. This syncs the hash iteration and freeing of items
1935 * on the hash. rcu_read_lock is too dangerous here.
1937 preempt_disable_notrace();
1938 hlist_for_each_entry_rcu(entry, n, hhd, node) {
1939 if (entry->ip == ip)
1940 entry->ops->func(ip, parent_ip, &entry->data);
1942 preempt_enable_notrace();
1945 static struct ftrace_ops trace_probe_ops __read_mostly =
1947 .func = function_trace_probe_call,
1950 static int ftrace_probe_registered;
1952 static void __enable_ftrace_function_probe(void)
1954 int i;
1956 if (ftrace_probe_registered)
1957 return;
1959 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
1960 struct hlist_head *hhd = &ftrace_func_hash[i];
1961 if (hhd->first)
1962 break;
1964 /* Nothing registered? */
1965 if (i == FTRACE_FUNC_HASHSIZE)
1966 return;
1968 __register_ftrace_function(&trace_probe_ops);
1969 ftrace_startup(0);
1970 ftrace_probe_registered = 1;
1973 static void __disable_ftrace_function_probe(void)
1975 int i;
1977 if (!ftrace_probe_registered)
1978 return;
1980 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
1981 struct hlist_head *hhd = &ftrace_func_hash[i];
1982 if (hhd->first)
1983 return;
1986 /* no more funcs left */
1987 __unregister_ftrace_function(&trace_probe_ops);
1988 ftrace_shutdown(0);
1989 ftrace_probe_registered = 0;
1993 static void ftrace_free_entry_rcu(struct rcu_head *rhp)
1995 struct ftrace_func_probe *entry =
1996 container_of(rhp, struct ftrace_func_probe, rcu);
1998 if (entry->ops->free)
1999 entry->ops->free(&entry->data);
2000 kfree(entry);
2005 register_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
2006 void *data)
2008 struct ftrace_func_probe *entry;
2009 struct ftrace_page *pg;
2010 struct dyn_ftrace *rec;
2011 int type, len, not;
2012 unsigned long key;
2013 int count = 0;
2014 char *search;
2016 type = filter_parse_regex(glob, strlen(glob), &search, &not);
2017 len = strlen(search);
2019 /* we do not support '!' for function probes */
2020 if (WARN_ON(not))
2021 return -EINVAL;
2023 mutex_lock(&ftrace_lock);
2024 do_for_each_ftrace_rec(pg, rec) {
2026 if (rec->flags & FTRACE_FL_FAILED)
2027 continue;
2029 if (!ftrace_match_record(rec, search, len, type))
2030 continue;
2032 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
2033 if (!entry) {
2034 /* If we did not process any, then return error */
2035 if (!count)
2036 count = -ENOMEM;
2037 goto out_unlock;
2040 count++;
2042 entry->data = data;
2045 * The caller might want to do something special
2046 * for each function we find. We call the callback
2047 * to give the caller an opportunity to do so.
2049 if (ops->callback) {
2050 if (ops->callback(rec->ip, &entry->data) < 0) {
2051 /* caller does not like this func */
2052 kfree(entry);
2053 continue;
2057 entry->ops = ops;
2058 entry->ip = rec->ip;
2060 key = hash_long(entry->ip, FTRACE_HASH_BITS);
2061 hlist_add_head_rcu(&entry->node, &ftrace_func_hash[key]);
2063 } while_for_each_ftrace_rec();
2064 __enable_ftrace_function_probe();
2066 out_unlock:
2067 mutex_unlock(&ftrace_lock);
2069 return count;
2072 enum {
2073 PROBE_TEST_FUNC = 1,
2074 PROBE_TEST_DATA = 2
2077 static void
2078 __unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
2079 void *data, int flags)
2081 struct ftrace_func_probe *entry;
2082 struct hlist_node *n, *tmp;
2083 char str[KSYM_SYMBOL_LEN];
2084 int type = MATCH_FULL;
2085 int i, len = 0;
2086 char *search;
2088 if (glob && (strcmp(glob, "*") == 0 || !strlen(glob)))
2089 glob = NULL;
2090 else if (glob) {
2091 int not;
2093 type = filter_parse_regex(glob, strlen(glob), &search, &not);
2094 len = strlen(search);
2096 /* we do not support '!' for function probes */
2097 if (WARN_ON(not))
2098 return;
2101 mutex_lock(&ftrace_lock);
2102 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
2103 struct hlist_head *hhd = &ftrace_func_hash[i];
2105 hlist_for_each_entry_safe(entry, n, tmp, hhd, node) {
2107 /* break up if statements for readability */
2108 if ((flags & PROBE_TEST_FUNC) && entry->ops != ops)
2109 continue;
2111 if ((flags & PROBE_TEST_DATA) && entry->data != data)
2112 continue;
2114 /* do this last, since it is the most expensive */
2115 if (glob) {
2116 kallsyms_lookup(entry->ip, NULL, NULL,
2117 NULL, str);
2118 if (!ftrace_match(str, glob, len, type))
2119 continue;
2122 hlist_del(&entry->node);
2123 call_rcu(&entry->rcu, ftrace_free_entry_rcu);
2126 __disable_ftrace_function_probe();
2127 mutex_unlock(&ftrace_lock);
2130 void
2131 unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
2132 void *data)
2134 __unregister_ftrace_function_probe(glob, ops, data,
2135 PROBE_TEST_FUNC | PROBE_TEST_DATA);
2138 void
2139 unregister_ftrace_function_probe_func(char *glob, struct ftrace_probe_ops *ops)
2141 __unregister_ftrace_function_probe(glob, ops, NULL, PROBE_TEST_FUNC);
2144 void unregister_ftrace_function_probe_all(char *glob)
2146 __unregister_ftrace_function_probe(glob, NULL, NULL, 0);
2149 static LIST_HEAD(ftrace_commands);
2150 static DEFINE_MUTEX(ftrace_cmd_mutex);
2152 int register_ftrace_command(struct ftrace_func_command *cmd)
2154 struct ftrace_func_command *p;
2155 int ret = 0;
2157 mutex_lock(&ftrace_cmd_mutex);
2158 list_for_each_entry(p, &ftrace_commands, list) {
2159 if (strcmp(cmd->name, p->name) == 0) {
2160 ret = -EBUSY;
2161 goto out_unlock;
2164 list_add(&cmd->list, &ftrace_commands);
2165 out_unlock:
2166 mutex_unlock(&ftrace_cmd_mutex);
2168 return ret;
2171 int unregister_ftrace_command(struct ftrace_func_command *cmd)
2173 struct ftrace_func_command *p, *n;
2174 int ret = -ENODEV;
2176 mutex_lock(&ftrace_cmd_mutex);
2177 list_for_each_entry_safe(p, n, &ftrace_commands, list) {
2178 if (strcmp(cmd->name, p->name) == 0) {
2179 ret = 0;
2180 list_del_init(&p->list);
2181 goto out_unlock;
2184 out_unlock:
2185 mutex_unlock(&ftrace_cmd_mutex);
2187 return ret;
2190 static int ftrace_process_regex(char *buff, int len, int enable)
2192 char *func, *command, *next = buff;
2193 struct ftrace_func_command *p;
2194 int ret = -EINVAL;
2196 func = strsep(&next, ":");
2198 if (!next) {
2199 if (ftrace_match_records(func, len, enable))
2200 return 0;
2201 return ret;
2204 /* command found */
2206 command = strsep(&next, ":");
2208 mutex_lock(&ftrace_cmd_mutex);
2209 list_for_each_entry(p, &ftrace_commands, list) {
2210 if (strcmp(p->name, command) == 0) {
2211 ret = p->func(func, command, next, enable);
2212 goto out_unlock;
2215 out_unlock:
2216 mutex_unlock(&ftrace_cmd_mutex);
2218 return ret;
2221 static ssize_t
2222 ftrace_regex_write(struct file *file, const char __user *ubuf,
2223 size_t cnt, loff_t *ppos, int enable)
2225 struct ftrace_iterator *iter;
2226 struct trace_parser *parser;
2227 ssize_t ret, read;
2229 if (!cnt)
2230 return 0;
2232 mutex_lock(&ftrace_regex_lock);
2234 if (file->f_mode & FMODE_READ) {
2235 struct seq_file *m = file->private_data;
2236 iter = m->private;
2237 } else
2238 iter = file->private_data;
2240 parser = &iter->parser;
2241 read = trace_get_user(parser, ubuf, cnt, ppos);
2243 if (read >= 0 && trace_parser_loaded(parser) &&
2244 !trace_parser_cont(parser)) {
2245 ret = ftrace_process_regex(parser->buffer,
2246 parser->idx, enable);
2247 trace_parser_clear(parser);
2248 if (ret)
2249 goto out_unlock;
2252 ret = read;
2253 out_unlock:
2254 mutex_unlock(&ftrace_regex_lock);
2256 return ret;
2259 static ssize_t
2260 ftrace_filter_write(struct file *file, const char __user *ubuf,
2261 size_t cnt, loff_t *ppos)
2263 return ftrace_regex_write(file, ubuf, cnt, ppos, 1);
2266 static ssize_t
2267 ftrace_notrace_write(struct file *file, const char __user *ubuf,
2268 size_t cnt, loff_t *ppos)
2270 return ftrace_regex_write(file, ubuf, cnt, ppos, 0);
2273 static void
2274 ftrace_set_regex(unsigned char *buf, int len, int reset, int enable)
2276 if (unlikely(ftrace_disabled))
2277 return;
2279 mutex_lock(&ftrace_regex_lock);
2280 if (reset)
2281 ftrace_filter_reset(enable);
2282 if (buf)
2283 ftrace_match_records(buf, len, enable);
2284 mutex_unlock(&ftrace_regex_lock);
2288 * ftrace_set_filter - set a function to filter on in ftrace
2289 * @buf - the string that holds the function filter text.
2290 * @len - the length of the string.
2291 * @reset - non zero to reset all filters before applying this filter.
2293 * Filters denote which functions should be enabled when tracing is enabled.
2294 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
2296 void ftrace_set_filter(unsigned char *buf, int len, int reset)
2298 ftrace_set_regex(buf, len, reset, 1);
2302 * ftrace_set_notrace - set a function to not trace in ftrace
2303 * @buf - the string that holds the function notrace text.
2304 * @len - the length of the string.
2305 * @reset - non zero to reset all filters before applying this filter.
2307 * Notrace Filters denote which functions should not be enabled when tracing
2308 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
2309 * for tracing.
2311 void ftrace_set_notrace(unsigned char *buf, int len, int reset)
2313 ftrace_set_regex(buf, len, reset, 0);
2317 * command line interface to allow users to set filters on boot up.
2319 #define FTRACE_FILTER_SIZE COMMAND_LINE_SIZE
2320 static char ftrace_notrace_buf[FTRACE_FILTER_SIZE] __initdata;
2321 static char ftrace_filter_buf[FTRACE_FILTER_SIZE] __initdata;
2323 static int __init set_ftrace_notrace(char *str)
2325 strncpy(ftrace_notrace_buf, str, FTRACE_FILTER_SIZE);
2326 return 1;
2328 __setup("ftrace_notrace=", set_ftrace_notrace);
2330 static int __init set_ftrace_filter(char *str)
2332 strncpy(ftrace_filter_buf, str, FTRACE_FILTER_SIZE);
2333 return 1;
2335 __setup("ftrace_filter=", set_ftrace_filter);
2337 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
2338 static char ftrace_graph_buf[FTRACE_FILTER_SIZE] __initdata;
2339 static int ftrace_set_func(unsigned long *array, int *idx, char *buffer);
2341 static int __init set_graph_function(char *str)
2343 strlcpy(ftrace_graph_buf, str, FTRACE_FILTER_SIZE);
2344 return 1;
2346 __setup("ftrace_graph_filter=", set_graph_function);
2348 static void __init set_ftrace_early_graph(char *buf)
2350 int ret;
2351 char *func;
2353 while (buf) {
2354 func = strsep(&buf, ",");
2355 /* we allow only one expression at a time */
2356 ret = ftrace_set_func(ftrace_graph_funcs, &ftrace_graph_count,
2357 func);
2358 if (ret)
2359 printk(KERN_DEBUG "ftrace: function %s not "
2360 "traceable\n", func);
2363 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
2365 static void __init set_ftrace_early_filter(char *buf, int enable)
2367 char *func;
2369 while (buf) {
2370 func = strsep(&buf, ",");
2371 ftrace_set_regex(func, strlen(func), 0, enable);
2375 static void __init set_ftrace_early_filters(void)
2377 if (ftrace_filter_buf[0])
2378 set_ftrace_early_filter(ftrace_filter_buf, 1);
2379 if (ftrace_notrace_buf[0])
2380 set_ftrace_early_filter(ftrace_notrace_buf, 0);
2381 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
2382 if (ftrace_graph_buf[0])
2383 set_ftrace_early_graph(ftrace_graph_buf);
2384 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
2387 static int
2388 ftrace_regex_release(struct inode *inode, struct file *file, int enable)
2390 struct seq_file *m = (struct seq_file *)file->private_data;
2391 struct ftrace_iterator *iter;
2392 struct trace_parser *parser;
2394 mutex_lock(&ftrace_regex_lock);
2395 if (file->f_mode & FMODE_READ) {
2396 iter = m->private;
2398 seq_release(inode, file);
2399 } else
2400 iter = file->private_data;
2402 parser = &iter->parser;
2403 if (trace_parser_loaded(parser)) {
2404 parser->buffer[parser->idx] = 0;
2405 ftrace_match_records(parser->buffer, parser->idx, enable);
2408 mutex_lock(&ftrace_lock);
2409 if (ftrace_start_up && ftrace_enabled)
2410 ftrace_run_update_code(FTRACE_ENABLE_CALLS);
2411 mutex_unlock(&ftrace_lock);
2413 trace_parser_put(parser);
2414 kfree(iter);
2416 mutex_unlock(&ftrace_regex_lock);
2417 return 0;
2420 static int
2421 ftrace_filter_release(struct inode *inode, struct file *file)
2423 return ftrace_regex_release(inode, file, 1);
2426 static int
2427 ftrace_notrace_release(struct inode *inode, struct file *file)
2429 return ftrace_regex_release(inode, file, 0);
2432 static const struct file_operations ftrace_avail_fops = {
2433 .open = ftrace_avail_open,
2434 .read = seq_read,
2435 .llseek = seq_lseek,
2436 .release = seq_release_private,
2439 static const struct file_operations ftrace_failures_fops = {
2440 .open = ftrace_failures_open,
2441 .read = seq_read,
2442 .llseek = seq_lseek,
2443 .release = seq_release_private,
2446 static const struct file_operations ftrace_filter_fops = {
2447 .open = ftrace_filter_open,
2448 .read = seq_read,
2449 .write = ftrace_filter_write,
2450 .llseek = no_llseek,
2451 .release = ftrace_filter_release,
2454 static const struct file_operations ftrace_notrace_fops = {
2455 .open = ftrace_notrace_open,
2456 .read = seq_read,
2457 .write = ftrace_notrace_write,
2458 .llseek = ftrace_regex_lseek,
2459 .release = ftrace_notrace_release,
2462 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
2464 static DEFINE_MUTEX(graph_lock);
2466 int ftrace_graph_count;
2467 int ftrace_graph_filter_enabled;
2468 unsigned long ftrace_graph_funcs[FTRACE_GRAPH_MAX_FUNCS] __read_mostly;
2470 static void *
2471 __g_next(struct seq_file *m, loff_t *pos)
2473 if (*pos >= ftrace_graph_count)
2474 return NULL;
2475 return &ftrace_graph_funcs[*pos];
2478 static void *
2479 g_next(struct seq_file *m, void *v, loff_t *pos)
2481 (*pos)++;
2482 return __g_next(m, pos);
2485 static void *g_start(struct seq_file *m, loff_t *pos)
2487 mutex_lock(&graph_lock);
2489 /* Nothing, tell g_show to print all functions are enabled */
2490 if (!ftrace_graph_filter_enabled && !*pos)
2491 return (void *)1;
2493 return __g_next(m, pos);
2496 static void g_stop(struct seq_file *m, void *p)
2498 mutex_unlock(&graph_lock);
2501 static int g_show(struct seq_file *m, void *v)
2503 unsigned long *ptr = v;
2505 if (!ptr)
2506 return 0;
2508 if (ptr == (unsigned long *)1) {
2509 seq_printf(m, "#### all functions enabled ####\n");
2510 return 0;
2513 seq_printf(m, "%ps\n", (void *)*ptr);
2515 return 0;
2518 static const struct seq_operations ftrace_graph_seq_ops = {
2519 .start = g_start,
2520 .next = g_next,
2521 .stop = g_stop,
2522 .show = g_show,
2525 static int
2526 ftrace_graph_open(struct inode *inode, struct file *file)
2528 int ret = 0;
2530 if (unlikely(ftrace_disabled))
2531 return -ENODEV;
2533 mutex_lock(&graph_lock);
2534 if ((file->f_mode & FMODE_WRITE) &&
2535 (file->f_flags & O_TRUNC)) {
2536 ftrace_graph_filter_enabled = 0;
2537 ftrace_graph_count = 0;
2538 memset(ftrace_graph_funcs, 0, sizeof(ftrace_graph_funcs));
2540 mutex_unlock(&graph_lock);
2542 if (file->f_mode & FMODE_READ)
2543 ret = seq_open(file, &ftrace_graph_seq_ops);
2545 return ret;
2548 static int
2549 ftrace_graph_release(struct inode *inode, struct file *file)
2551 if (file->f_mode & FMODE_READ)
2552 seq_release(inode, file);
2553 return 0;
2556 static int
2557 ftrace_set_func(unsigned long *array, int *idx, char *buffer)
2559 struct dyn_ftrace *rec;
2560 struct ftrace_page *pg;
2561 int search_len;
2562 int fail = 1;
2563 int type, not;
2564 char *search;
2565 bool exists;
2566 int i;
2568 if (ftrace_disabled)
2569 return -ENODEV;
2571 /* decode regex */
2572 type = filter_parse_regex(buffer, strlen(buffer), &search, &not);
2573 if (!not && *idx >= FTRACE_GRAPH_MAX_FUNCS)
2574 return -EBUSY;
2576 search_len = strlen(search);
2578 mutex_lock(&ftrace_lock);
2579 do_for_each_ftrace_rec(pg, rec) {
2581 if (rec->flags & (FTRACE_FL_FAILED | FTRACE_FL_FREE))
2582 continue;
2584 if (ftrace_match_record(rec, search, search_len, type)) {
2585 /* if it is in the array */
2586 exists = false;
2587 for (i = 0; i < *idx; i++) {
2588 if (array[i] == rec->ip) {
2589 exists = true;
2590 break;
2594 if (!not) {
2595 fail = 0;
2596 if (!exists) {
2597 array[(*idx)++] = rec->ip;
2598 if (*idx >= FTRACE_GRAPH_MAX_FUNCS)
2599 goto out;
2601 } else {
2602 if (exists) {
2603 array[i] = array[--(*idx)];
2604 array[*idx] = 0;
2605 fail = 0;
2609 } while_for_each_ftrace_rec();
2610 out:
2611 mutex_unlock(&ftrace_lock);
2613 if (fail)
2614 return -EINVAL;
2616 ftrace_graph_filter_enabled = 1;
2617 return 0;
2620 static ssize_t
2621 ftrace_graph_write(struct file *file, const char __user *ubuf,
2622 size_t cnt, loff_t *ppos)
2624 struct trace_parser parser;
2625 ssize_t read, ret;
2627 if (!cnt)
2628 return 0;
2630 mutex_lock(&graph_lock);
2632 if (trace_parser_get_init(&parser, FTRACE_BUFF_MAX)) {
2633 ret = -ENOMEM;
2634 goto out_unlock;
2637 read = trace_get_user(&parser, ubuf, cnt, ppos);
2639 if (read >= 0 && trace_parser_loaded((&parser))) {
2640 parser.buffer[parser.idx] = 0;
2642 /* we allow only one expression at a time */
2643 ret = ftrace_set_func(ftrace_graph_funcs, &ftrace_graph_count,
2644 parser.buffer);
2645 if (ret)
2646 goto out_free;
2649 ret = read;
2651 out_free:
2652 trace_parser_put(&parser);
2653 out_unlock:
2654 mutex_unlock(&graph_lock);
2656 return ret;
2659 static const struct file_operations ftrace_graph_fops = {
2660 .open = ftrace_graph_open,
2661 .read = seq_read,
2662 .write = ftrace_graph_write,
2663 .release = ftrace_graph_release,
2665 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
2667 static __init int ftrace_init_dyn_debugfs(struct dentry *d_tracer)
2670 trace_create_file("available_filter_functions", 0444,
2671 d_tracer, NULL, &ftrace_avail_fops);
2673 trace_create_file("failures", 0444,
2674 d_tracer, NULL, &ftrace_failures_fops);
2676 trace_create_file("set_ftrace_filter", 0644, d_tracer,
2677 NULL, &ftrace_filter_fops);
2679 trace_create_file("set_ftrace_notrace", 0644, d_tracer,
2680 NULL, &ftrace_notrace_fops);
2682 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
2683 trace_create_file("set_graph_function", 0444, d_tracer,
2684 NULL,
2685 &ftrace_graph_fops);
2686 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
2688 return 0;
2691 static int ftrace_process_locs(struct module *mod,
2692 unsigned long *start,
2693 unsigned long *end)
2695 unsigned long *p;
2696 unsigned long addr;
2697 unsigned long flags;
2699 mutex_lock(&ftrace_lock);
2700 p = start;
2701 while (p < end) {
2702 addr = ftrace_call_adjust(*p++);
2704 * Some architecture linkers will pad between
2705 * the different mcount_loc sections of different
2706 * object files to satisfy alignments.
2707 * Skip any NULL pointers.
2709 if (!addr)
2710 continue;
2711 ftrace_record_ip(addr);
2714 /* disable interrupts to prevent kstop machine */
2715 local_irq_save(flags);
2716 ftrace_update_code(mod);
2717 local_irq_restore(flags);
2718 mutex_unlock(&ftrace_lock);
2720 return 0;
2723 #ifdef CONFIG_MODULES
2724 void ftrace_release_mod(struct module *mod)
2726 struct dyn_ftrace *rec;
2727 struct ftrace_page *pg;
2729 if (ftrace_disabled)
2730 return;
2732 mutex_lock(&ftrace_lock);
2733 do_for_each_ftrace_rec(pg, rec) {
2734 if (within_module_core(rec->ip, mod)) {
2736 * rec->ip is changed in ftrace_free_rec()
2737 * It should not between s and e if record was freed.
2739 FTRACE_WARN_ON(rec->flags & FTRACE_FL_FREE);
2740 ftrace_free_rec(rec);
2742 } while_for_each_ftrace_rec();
2743 mutex_unlock(&ftrace_lock);
2746 static void ftrace_init_module(struct module *mod,
2747 unsigned long *start, unsigned long *end)
2749 if (ftrace_disabled || start == end)
2750 return;
2751 ftrace_process_locs(mod, start, end);
2754 static int ftrace_module_notify(struct notifier_block *self,
2755 unsigned long val, void *data)
2757 struct module *mod = data;
2759 switch (val) {
2760 case MODULE_STATE_COMING:
2761 ftrace_init_module(mod, mod->ftrace_callsites,
2762 mod->ftrace_callsites +
2763 mod->num_ftrace_callsites);
2764 break;
2765 case MODULE_STATE_GOING:
2766 ftrace_release_mod(mod);
2767 break;
2770 return 0;
2772 #else
2773 static int ftrace_module_notify(struct notifier_block *self,
2774 unsigned long val, void *data)
2776 return 0;
2778 #endif /* CONFIG_MODULES */
2780 struct notifier_block ftrace_module_nb = {
2781 .notifier_call = ftrace_module_notify,
2782 .priority = 0,
2785 extern unsigned long __start_mcount_loc[];
2786 extern unsigned long __stop_mcount_loc[];
2788 void __init ftrace_init(void)
2790 unsigned long count, addr, flags;
2791 int ret;
2793 /* Keep the ftrace pointer to the stub */
2794 addr = (unsigned long)ftrace_stub;
2796 local_irq_save(flags);
2797 ftrace_dyn_arch_init(&addr);
2798 local_irq_restore(flags);
2800 /* ftrace_dyn_arch_init places the return code in addr */
2801 if (addr)
2802 goto failed;
2804 count = __stop_mcount_loc - __start_mcount_loc;
2806 ret = ftrace_dyn_table_alloc(count);
2807 if (ret)
2808 goto failed;
2810 last_ftrace_enabled = ftrace_enabled = 1;
2812 ret = ftrace_process_locs(NULL,
2813 __start_mcount_loc,
2814 __stop_mcount_loc);
2816 ret = register_module_notifier(&ftrace_module_nb);
2817 if (ret)
2818 pr_warning("Failed to register trace ftrace module notifier\n");
2820 set_ftrace_early_filters();
2822 return;
2823 failed:
2824 ftrace_disabled = 1;
2827 #else
2829 static int __init ftrace_nodyn_init(void)
2831 ftrace_enabled = 1;
2832 return 0;
2834 device_initcall(ftrace_nodyn_init);
2836 static inline int ftrace_init_dyn_debugfs(struct dentry *d_tracer) { return 0; }
2837 static inline void ftrace_startup_enable(int command) { }
2838 /* Keep as macros so we do not need to define the commands */
2839 # define ftrace_startup(command) do { } while (0)
2840 # define ftrace_shutdown(command) do { } while (0)
2841 # define ftrace_startup_sysctl() do { } while (0)
2842 # define ftrace_shutdown_sysctl() do { } while (0)
2843 #endif /* CONFIG_DYNAMIC_FTRACE */
2845 static void clear_ftrace_swapper(void)
2847 struct task_struct *p;
2848 int cpu;
2850 get_online_cpus();
2851 for_each_online_cpu(cpu) {
2852 p = idle_task(cpu);
2853 clear_tsk_trace_trace(p);
2855 put_online_cpus();
2858 static void set_ftrace_swapper(void)
2860 struct task_struct *p;
2861 int cpu;
2863 get_online_cpus();
2864 for_each_online_cpu(cpu) {
2865 p = idle_task(cpu);
2866 set_tsk_trace_trace(p);
2868 put_online_cpus();
2871 static void clear_ftrace_pid(struct pid *pid)
2873 struct task_struct *p;
2875 rcu_read_lock();
2876 do_each_pid_task(pid, PIDTYPE_PID, p) {
2877 clear_tsk_trace_trace(p);
2878 } while_each_pid_task(pid, PIDTYPE_PID, p);
2879 rcu_read_unlock();
2881 put_pid(pid);
2884 static void set_ftrace_pid(struct pid *pid)
2886 struct task_struct *p;
2888 rcu_read_lock();
2889 do_each_pid_task(pid, PIDTYPE_PID, p) {
2890 set_tsk_trace_trace(p);
2891 } while_each_pid_task(pid, PIDTYPE_PID, p);
2892 rcu_read_unlock();
2895 static void clear_ftrace_pid_task(struct pid *pid)
2897 if (pid == ftrace_swapper_pid)
2898 clear_ftrace_swapper();
2899 else
2900 clear_ftrace_pid(pid);
2903 static void set_ftrace_pid_task(struct pid *pid)
2905 if (pid == ftrace_swapper_pid)
2906 set_ftrace_swapper();
2907 else
2908 set_ftrace_pid(pid);
2911 static int ftrace_pid_add(int p)
2913 struct pid *pid;
2914 struct ftrace_pid *fpid;
2915 int ret = -EINVAL;
2917 mutex_lock(&ftrace_lock);
2919 if (!p)
2920 pid = ftrace_swapper_pid;
2921 else
2922 pid = find_get_pid(p);
2924 if (!pid)
2925 goto out;
2927 ret = 0;
2929 list_for_each_entry(fpid, &ftrace_pids, list)
2930 if (fpid->pid == pid)
2931 goto out_put;
2933 ret = -ENOMEM;
2935 fpid = kmalloc(sizeof(*fpid), GFP_KERNEL);
2936 if (!fpid)
2937 goto out_put;
2939 list_add(&fpid->list, &ftrace_pids);
2940 fpid->pid = pid;
2942 set_ftrace_pid_task(pid);
2944 ftrace_update_pid_func();
2945 ftrace_startup_enable(0);
2947 mutex_unlock(&ftrace_lock);
2948 return 0;
2950 out_put:
2951 if (pid != ftrace_swapper_pid)
2952 put_pid(pid);
2954 out:
2955 mutex_unlock(&ftrace_lock);
2956 return ret;
2959 static void ftrace_pid_reset(void)
2961 struct ftrace_pid *fpid, *safe;
2963 mutex_lock(&ftrace_lock);
2964 list_for_each_entry_safe(fpid, safe, &ftrace_pids, list) {
2965 struct pid *pid = fpid->pid;
2967 clear_ftrace_pid_task(pid);
2969 list_del(&fpid->list);
2970 kfree(fpid);
2973 ftrace_update_pid_func();
2974 ftrace_startup_enable(0);
2976 mutex_unlock(&ftrace_lock);
2979 static void *fpid_start(struct seq_file *m, loff_t *pos)
2981 mutex_lock(&ftrace_lock);
2983 if (list_empty(&ftrace_pids) && (!*pos))
2984 return (void *) 1;
2986 return seq_list_start(&ftrace_pids, *pos);
2989 static void *fpid_next(struct seq_file *m, void *v, loff_t *pos)
2991 if (v == (void *)1)
2992 return NULL;
2994 return seq_list_next(v, &ftrace_pids, pos);
2997 static void fpid_stop(struct seq_file *m, void *p)
2999 mutex_unlock(&ftrace_lock);
3002 static int fpid_show(struct seq_file *m, void *v)
3004 const struct ftrace_pid *fpid = list_entry(v, struct ftrace_pid, list);
3006 if (v == (void *)1) {
3007 seq_printf(m, "no pid\n");
3008 return 0;
3011 if (fpid->pid == ftrace_swapper_pid)
3012 seq_printf(m, "swapper tasks\n");
3013 else
3014 seq_printf(m, "%u\n", pid_vnr(fpid->pid));
3016 return 0;
3019 static const struct seq_operations ftrace_pid_sops = {
3020 .start = fpid_start,
3021 .next = fpid_next,
3022 .stop = fpid_stop,
3023 .show = fpid_show,
3026 static int
3027 ftrace_pid_open(struct inode *inode, struct file *file)
3029 int ret = 0;
3031 if ((file->f_mode & FMODE_WRITE) &&
3032 (file->f_flags & O_TRUNC))
3033 ftrace_pid_reset();
3035 if (file->f_mode & FMODE_READ)
3036 ret = seq_open(file, &ftrace_pid_sops);
3038 return ret;
3041 static ssize_t
3042 ftrace_pid_write(struct file *filp, const char __user *ubuf,
3043 size_t cnt, loff_t *ppos)
3045 char buf[64], *tmp;
3046 long val;
3047 int ret;
3049 if (cnt >= sizeof(buf))
3050 return -EINVAL;
3052 if (copy_from_user(&buf, ubuf, cnt))
3053 return -EFAULT;
3055 buf[cnt] = 0;
3058 * Allow "echo > set_ftrace_pid" or "echo -n '' > set_ftrace_pid"
3059 * to clean the filter quietly.
3061 tmp = strstrip(buf);
3062 if (strlen(tmp) == 0)
3063 return 1;
3065 ret = strict_strtol(tmp, 10, &val);
3066 if (ret < 0)
3067 return ret;
3069 ret = ftrace_pid_add(val);
3071 return ret ? ret : cnt;
3074 static int
3075 ftrace_pid_release(struct inode *inode, struct file *file)
3077 if (file->f_mode & FMODE_READ)
3078 seq_release(inode, file);
3080 return 0;
3083 static const struct file_operations ftrace_pid_fops = {
3084 .open = ftrace_pid_open,
3085 .write = ftrace_pid_write,
3086 .read = seq_read,
3087 .llseek = seq_lseek,
3088 .release = ftrace_pid_release,
3091 static __init int ftrace_init_debugfs(void)
3093 struct dentry *d_tracer;
3095 d_tracer = tracing_init_dentry();
3096 if (!d_tracer)
3097 return 0;
3099 ftrace_init_dyn_debugfs(d_tracer);
3101 trace_create_file("set_ftrace_pid", 0644, d_tracer,
3102 NULL, &ftrace_pid_fops);
3104 ftrace_profile_debugfs(d_tracer);
3106 return 0;
3108 fs_initcall(ftrace_init_debugfs);
3111 * ftrace_kill - kill ftrace
3113 * This function should be used by panic code. It stops ftrace
3114 * but in a not so nice way. If you need to simply kill ftrace
3115 * from a non-atomic section, use ftrace_kill.
3117 void ftrace_kill(void)
3119 ftrace_disabled = 1;
3120 ftrace_enabled = 0;
3121 clear_ftrace_function();
3125 * register_ftrace_function - register a function for profiling
3126 * @ops - ops structure that holds the function for profiling.
3128 * Register a function to be called by all functions in the
3129 * kernel.
3131 * Note: @ops->func and all the functions it calls must be labeled
3132 * with "notrace", otherwise it will go into a
3133 * recursive loop.
3135 int register_ftrace_function(struct ftrace_ops *ops)
3137 int ret;
3139 if (unlikely(ftrace_disabled))
3140 return -1;
3142 mutex_lock(&ftrace_lock);
3144 ret = __register_ftrace_function(ops);
3145 ftrace_startup(0);
3147 mutex_unlock(&ftrace_lock);
3148 return ret;
3152 * unregister_ftrace_function - unregister a function for profiling.
3153 * @ops - ops structure that holds the function to unregister
3155 * Unregister a function that was added to be called by ftrace profiling.
3157 int unregister_ftrace_function(struct ftrace_ops *ops)
3159 int ret;
3161 mutex_lock(&ftrace_lock);
3162 ret = __unregister_ftrace_function(ops);
3163 ftrace_shutdown(0);
3164 mutex_unlock(&ftrace_lock);
3166 return ret;
3170 ftrace_enable_sysctl(struct ctl_table *table, int write,
3171 void __user *buffer, size_t *lenp,
3172 loff_t *ppos)
3174 int ret;
3176 if (unlikely(ftrace_disabled))
3177 return -ENODEV;
3179 mutex_lock(&ftrace_lock);
3181 ret = proc_dointvec(table, write, buffer, lenp, ppos);
3183 if (ret || !write || (last_ftrace_enabled == !!ftrace_enabled))
3184 goto out;
3186 last_ftrace_enabled = !!ftrace_enabled;
3188 if (ftrace_enabled) {
3190 ftrace_startup_sysctl();
3192 /* we are starting ftrace again */
3193 if (ftrace_list != &ftrace_list_end) {
3194 if (ftrace_list->next == &ftrace_list_end)
3195 ftrace_trace_function = ftrace_list->func;
3196 else
3197 ftrace_trace_function = ftrace_list_func;
3200 } else {
3201 /* stopping ftrace calls (just send to ftrace_stub) */
3202 ftrace_trace_function = ftrace_stub;
3204 ftrace_shutdown_sysctl();
3207 out:
3208 mutex_unlock(&ftrace_lock);
3209 return ret;
3212 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
3214 static int ftrace_graph_active;
3215 static struct notifier_block ftrace_suspend_notifier;
3217 int ftrace_graph_entry_stub(struct ftrace_graph_ent *trace)
3219 return 0;
3222 /* The callbacks that hook a function */
3223 trace_func_graph_ret_t ftrace_graph_return =
3224 (trace_func_graph_ret_t)ftrace_stub;
3225 trace_func_graph_ent_t ftrace_graph_entry = ftrace_graph_entry_stub;
3227 /* Try to assign a return stack array on FTRACE_RETSTACK_ALLOC_SIZE tasks. */
3228 static int alloc_retstack_tasklist(struct ftrace_ret_stack **ret_stack_list)
3230 int i;
3231 int ret = 0;
3232 unsigned long flags;
3233 int start = 0, end = FTRACE_RETSTACK_ALLOC_SIZE;
3234 struct task_struct *g, *t;
3236 for (i = 0; i < FTRACE_RETSTACK_ALLOC_SIZE; i++) {
3237 ret_stack_list[i] = kmalloc(FTRACE_RETFUNC_DEPTH
3238 * sizeof(struct ftrace_ret_stack),
3239 GFP_KERNEL);
3240 if (!ret_stack_list[i]) {
3241 start = 0;
3242 end = i;
3243 ret = -ENOMEM;
3244 goto free;
3248 read_lock_irqsave(&tasklist_lock, flags);
3249 do_each_thread(g, t) {
3250 if (start == end) {
3251 ret = -EAGAIN;
3252 goto unlock;
3255 if (t->ret_stack == NULL) {
3256 atomic_set(&t->tracing_graph_pause, 0);
3257 atomic_set(&t->trace_overrun, 0);
3258 t->curr_ret_stack = -1;
3259 /* Make sure the tasks see the -1 first: */
3260 smp_wmb();
3261 t->ret_stack = ret_stack_list[start++];
3263 } while_each_thread(g, t);
3265 unlock:
3266 read_unlock_irqrestore(&tasklist_lock, flags);
3267 free:
3268 for (i = start; i < end; i++)
3269 kfree(ret_stack_list[i]);
3270 return ret;
3273 static void
3274 ftrace_graph_probe_sched_switch(void *ignore,
3275 struct task_struct *prev, struct task_struct *next)
3277 unsigned long long timestamp;
3278 int index;
3281 * Does the user want to count the time a function was asleep.
3282 * If so, do not update the time stamps.
3284 if (trace_flags & TRACE_ITER_SLEEP_TIME)
3285 return;
3287 timestamp = trace_clock_local();
3289 prev->ftrace_timestamp = timestamp;
3291 /* only process tasks that we timestamped */
3292 if (!next->ftrace_timestamp)
3293 return;
3296 * Update all the counters in next to make up for the
3297 * time next was sleeping.
3299 timestamp -= next->ftrace_timestamp;
3301 for (index = next->curr_ret_stack; index >= 0; index--)
3302 next->ret_stack[index].calltime += timestamp;
3305 /* Allocate a return stack for each task */
3306 static int start_graph_tracing(void)
3308 struct ftrace_ret_stack **ret_stack_list;
3309 int ret, cpu;
3311 ret_stack_list = kmalloc(FTRACE_RETSTACK_ALLOC_SIZE *
3312 sizeof(struct ftrace_ret_stack *),
3313 GFP_KERNEL);
3315 if (!ret_stack_list)
3316 return -ENOMEM;
3318 /* The cpu_boot init_task->ret_stack will never be freed */
3319 for_each_online_cpu(cpu) {
3320 if (!idle_task(cpu)->ret_stack)
3321 ftrace_graph_init_task(idle_task(cpu));
3324 do {
3325 ret = alloc_retstack_tasklist(ret_stack_list);
3326 } while (ret == -EAGAIN);
3328 if (!ret) {
3329 ret = register_trace_sched_switch(ftrace_graph_probe_sched_switch, NULL);
3330 if (ret)
3331 pr_info("ftrace_graph: Couldn't activate tracepoint"
3332 " probe to kernel_sched_switch\n");
3335 kfree(ret_stack_list);
3336 return ret;
3340 * Hibernation protection.
3341 * The state of the current task is too much unstable during
3342 * suspend/restore to disk. We want to protect against that.
3344 static int
3345 ftrace_suspend_notifier_call(struct notifier_block *bl, unsigned long state,
3346 void *unused)
3348 switch (state) {
3349 case PM_HIBERNATION_PREPARE:
3350 pause_graph_tracing();
3351 break;
3353 case PM_POST_HIBERNATION:
3354 unpause_graph_tracing();
3355 break;
3357 return NOTIFY_DONE;
3360 int register_ftrace_graph(trace_func_graph_ret_t retfunc,
3361 trace_func_graph_ent_t entryfunc)
3363 int ret = 0;
3365 mutex_lock(&ftrace_lock);
3367 /* we currently allow only one tracer registered at a time */
3368 if (ftrace_graph_active) {
3369 ret = -EBUSY;
3370 goto out;
3373 ftrace_suspend_notifier.notifier_call = ftrace_suspend_notifier_call;
3374 register_pm_notifier(&ftrace_suspend_notifier);
3376 ftrace_graph_active++;
3377 ret = start_graph_tracing();
3378 if (ret) {
3379 ftrace_graph_active--;
3380 goto out;
3383 ftrace_graph_return = retfunc;
3384 ftrace_graph_entry = entryfunc;
3386 ftrace_startup(FTRACE_START_FUNC_RET);
3388 out:
3389 mutex_unlock(&ftrace_lock);
3390 return ret;
3393 void unregister_ftrace_graph(void)
3395 mutex_lock(&ftrace_lock);
3397 if (unlikely(!ftrace_graph_active))
3398 goto out;
3400 ftrace_graph_active--;
3401 ftrace_graph_return = (trace_func_graph_ret_t)ftrace_stub;
3402 ftrace_graph_entry = ftrace_graph_entry_stub;
3403 ftrace_shutdown(FTRACE_STOP_FUNC_RET);
3404 unregister_pm_notifier(&ftrace_suspend_notifier);
3405 unregister_trace_sched_switch(ftrace_graph_probe_sched_switch, NULL);
3407 out:
3408 mutex_unlock(&ftrace_lock);
3411 /* Allocate a return stack for newly created task */
3412 void ftrace_graph_init_task(struct task_struct *t)
3414 /* Make sure we do not use the parent ret_stack */
3415 t->ret_stack = NULL;
3416 t->curr_ret_stack = -1;
3418 if (ftrace_graph_active) {
3419 struct ftrace_ret_stack *ret_stack;
3421 ret_stack = kmalloc(FTRACE_RETFUNC_DEPTH
3422 * sizeof(struct ftrace_ret_stack),
3423 GFP_KERNEL);
3424 if (!ret_stack)
3425 return;
3426 atomic_set(&t->tracing_graph_pause, 0);
3427 atomic_set(&t->trace_overrun, 0);
3428 t->ftrace_timestamp = 0;
3429 /* make curr_ret_stack visable before we add the ret_stack */
3430 smp_wmb();
3431 t->ret_stack = ret_stack;
3435 void ftrace_graph_exit_task(struct task_struct *t)
3437 struct ftrace_ret_stack *ret_stack = t->ret_stack;
3439 t->ret_stack = NULL;
3440 /* NULL must become visible to IRQs before we free it: */
3441 barrier();
3443 kfree(ret_stack);
3446 void ftrace_graph_stop(void)
3448 ftrace_stop();
3450 #endif