pohmelfs: Use __generic_file_aio_write instead of generic_file_aio_write_nolock
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / kernel / trace / ftrace.c
blob8c804e24f96fcbb0c0674e84e0efd62b463071b4
1 /*
2 * Infrastructure for profiling code inserted by 'gcc -pg'.
4 * Copyright (C) 2007-2008 Steven Rostedt <srostedt@redhat.com>
5 * Copyright (C) 2004-2008 Ingo Molnar <mingo@redhat.com>
7 * Originally ported from the -rt patch by:
8 * Copyright (C) 2007 Arnaldo Carvalho de Melo <acme@redhat.com>
10 * Based on code in the latency_tracer, that is:
12 * Copyright (C) 2004-2006 Ingo Molnar
13 * Copyright (C) 2004 William Lee Irwin III
16 #include <linux/stop_machine.h>
17 #include <linux/clocksource.h>
18 #include <linux/kallsyms.h>
19 #include <linux/seq_file.h>
20 #include <linux/suspend.h>
21 #include <linux/debugfs.h>
22 #include <linux/hardirq.h>
23 #include <linux/kthread.h>
24 #include <linux/uaccess.h>
25 #include <linux/kprobes.h>
26 #include <linux/ftrace.h>
27 #include <linux/sysctl.h>
28 #include <linux/ctype.h>
29 #include <linux/list.h>
30 #include <linux/hash.h>
32 #include <trace/events/sched.h>
34 #include <asm/ftrace.h>
35 #include <asm/setup.h>
37 #include "trace_output.h"
38 #include "trace_stat.h"
40 #define FTRACE_WARN_ON(cond) \
41 do { \
42 if (WARN_ON(cond)) \
43 ftrace_kill(); \
44 } while (0)
46 #define FTRACE_WARN_ON_ONCE(cond) \
47 do { \
48 if (WARN_ON_ONCE(cond)) \
49 ftrace_kill(); \
50 } while (0)
52 /* hash bits for specific function selection */
53 #define FTRACE_HASH_BITS 7
54 #define FTRACE_FUNC_HASHSIZE (1 << FTRACE_HASH_BITS)
56 /* ftrace_enabled is a method to turn ftrace on or off */
57 int ftrace_enabled __read_mostly;
58 static int last_ftrace_enabled;
60 /* Quick disabling of function tracer. */
61 int function_trace_stop;
64 * ftrace_disabled is set when an anomaly is discovered.
65 * ftrace_disabled is much stronger than ftrace_enabled.
67 static int ftrace_disabled __read_mostly;
69 static DEFINE_MUTEX(ftrace_lock);
71 static struct ftrace_ops ftrace_list_end __read_mostly =
73 .func = ftrace_stub,
76 static struct ftrace_ops *ftrace_list __read_mostly = &ftrace_list_end;
77 ftrace_func_t ftrace_trace_function __read_mostly = ftrace_stub;
78 ftrace_func_t __ftrace_trace_function __read_mostly = ftrace_stub;
79 ftrace_func_t ftrace_pid_function __read_mostly = ftrace_stub;
81 static void ftrace_list_func(unsigned long ip, unsigned long parent_ip)
83 struct ftrace_ops *op = ftrace_list;
85 /* in case someone actually ports this to alpha! */
86 read_barrier_depends();
88 while (op != &ftrace_list_end) {
89 /* silly alpha */
90 read_barrier_depends();
91 op->func(ip, parent_ip);
92 op = op->next;
96 static void ftrace_pid_func(unsigned long ip, unsigned long parent_ip)
98 if (!test_tsk_trace_trace(current))
99 return;
101 ftrace_pid_function(ip, parent_ip);
104 static void set_ftrace_pid_function(ftrace_func_t func)
106 /* do not set ftrace_pid_function to itself! */
107 if (func != ftrace_pid_func)
108 ftrace_pid_function = func;
112 * clear_ftrace_function - reset the ftrace function
114 * This NULLs the ftrace function and in essence stops
115 * tracing. There may be lag
117 void clear_ftrace_function(void)
119 ftrace_trace_function = ftrace_stub;
120 __ftrace_trace_function = ftrace_stub;
121 ftrace_pid_function = ftrace_stub;
124 #ifndef CONFIG_HAVE_FUNCTION_TRACE_MCOUNT_TEST
126 * For those archs that do not test ftrace_trace_stop in their
127 * mcount call site, we need to do it from C.
129 static void ftrace_test_stop_func(unsigned long ip, unsigned long parent_ip)
131 if (function_trace_stop)
132 return;
134 __ftrace_trace_function(ip, parent_ip);
136 #endif
138 static int __register_ftrace_function(struct ftrace_ops *ops)
140 ops->next = ftrace_list;
142 * We are entering ops into the ftrace_list but another
143 * CPU might be walking that list. We need to make sure
144 * the ops->next pointer is valid before another CPU sees
145 * the ops pointer included into the ftrace_list.
147 smp_wmb();
148 ftrace_list = ops;
150 if (ftrace_enabled) {
151 ftrace_func_t func;
153 if (ops->next == &ftrace_list_end)
154 func = ops->func;
155 else
156 func = ftrace_list_func;
158 if (ftrace_pid_trace) {
159 set_ftrace_pid_function(func);
160 func = ftrace_pid_func;
164 * For one func, simply call it directly.
165 * For more than one func, call the chain.
167 #ifdef CONFIG_HAVE_FUNCTION_TRACE_MCOUNT_TEST
168 ftrace_trace_function = func;
169 #else
170 __ftrace_trace_function = func;
171 ftrace_trace_function = ftrace_test_stop_func;
172 #endif
175 return 0;
178 static int __unregister_ftrace_function(struct ftrace_ops *ops)
180 struct ftrace_ops **p;
183 * If we are removing the last function, then simply point
184 * to the ftrace_stub.
186 if (ftrace_list == ops && ops->next == &ftrace_list_end) {
187 ftrace_trace_function = ftrace_stub;
188 ftrace_list = &ftrace_list_end;
189 return 0;
192 for (p = &ftrace_list; *p != &ftrace_list_end; p = &(*p)->next)
193 if (*p == ops)
194 break;
196 if (*p != ops)
197 return -1;
199 *p = (*p)->next;
201 if (ftrace_enabled) {
202 /* If we only have one func left, then call that directly */
203 if (ftrace_list->next == &ftrace_list_end) {
204 ftrace_func_t func = ftrace_list->func;
206 if (ftrace_pid_trace) {
207 set_ftrace_pid_function(func);
208 func = ftrace_pid_func;
210 #ifdef CONFIG_HAVE_FUNCTION_TRACE_MCOUNT_TEST
211 ftrace_trace_function = func;
212 #else
213 __ftrace_trace_function = func;
214 #endif
218 return 0;
221 static void ftrace_update_pid_func(void)
223 ftrace_func_t func;
225 if (ftrace_trace_function == ftrace_stub)
226 return;
228 func = ftrace_trace_function;
230 if (ftrace_pid_trace) {
231 set_ftrace_pid_function(func);
232 func = ftrace_pid_func;
233 } else {
234 if (func == ftrace_pid_func)
235 func = ftrace_pid_function;
238 #ifdef CONFIG_HAVE_FUNCTION_TRACE_MCOUNT_TEST
239 ftrace_trace_function = func;
240 #else
241 __ftrace_trace_function = func;
242 #endif
245 #ifdef CONFIG_FUNCTION_PROFILER
246 struct ftrace_profile {
247 struct hlist_node node;
248 unsigned long ip;
249 unsigned long counter;
250 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
251 unsigned long long time;
252 #endif
255 struct ftrace_profile_page {
256 struct ftrace_profile_page *next;
257 unsigned long index;
258 struct ftrace_profile records[];
261 struct ftrace_profile_stat {
262 atomic_t disabled;
263 struct hlist_head *hash;
264 struct ftrace_profile_page *pages;
265 struct ftrace_profile_page *start;
266 struct tracer_stat stat;
269 #define PROFILE_RECORDS_SIZE \
270 (PAGE_SIZE - offsetof(struct ftrace_profile_page, records))
272 #define PROFILES_PER_PAGE \
273 (PROFILE_RECORDS_SIZE / sizeof(struct ftrace_profile))
275 static int ftrace_profile_bits __read_mostly;
276 static int ftrace_profile_enabled __read_mostly;
278 /* ftrace_profile_lock - synchronize the enable and disable of the profiler */
279 static DEFINE_MUTEX(ftrace_profile_lock);
281 static DEFINE_PER_CPU(struct ftrace_profile_stat, ftrace_profile_stats);
283 #define FTRACE_PROFILE_HASH_SIZE 1024 /* must be power of 2 */
285 static void *
286 function_stat_next(void *v, int idx)
288 struct ftrace_profile *rec = v;
289 struct ftrace_profile_page *pg;
291 pg = (struct ftrace_profile_page *)((unsigned long)rec & PAGE_MASK);
293 again:
294 if (idx != 0)
295 rec++;
297 if ((void *)rec >= (void *)&pg->records[pg->index]) {
298 pg = pg->next;
299 if (!pg)
300 return NULL;
301 rec = &pg->records[0];
302 if (!rec->counter)
303 goto again;
306 return rec;
309 static void *function_stat_start(struct tracer_stat *trace)
311 struct ftrace_profile_stat *stat =
312 container_of(trace, struct ftrace_profile_stat, stat);
314 if (!stat || !stat->start)
315 return NULL;
317 return function_stat_next(&stat->start->records[0], 0);
320 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
321 /* function graph compares on total time */
322 static int function_stat_cmp(void *p1, void *p2)
324 struct ftrace_profile *a = p1;
325 struct ftrace_profile *b = p2;
327 if (a->time < b->time)
328 return -1;
329 if (a->time > b->time)
330 return 1;
331 else
332 return 0;
334 #else
335 /* not function graph compares against hits */
336 static int function_stat_cmp(void *p1, void *p2)
338 struct ftrace_profile *a = p1;
339 struct ftrace_profile *b = p2;
341 if (a->counter < b->counter)
342 return -1;
343 if (a->counter > b->counter)
344 return 1;
345 else
346 return 0;
348 #endif
350 static int function_stat_headers(struct seq_file *m)
352 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
353 seq_printf(m, " Function "
354 "Hit Time Avg\n"
355 " -------- "
356 "--- ---- ---\n");
357 #else
358 seq_printf(m, " Function Hit\n"
359 " -------- ---\n");
360 #endif
361 return 0;
364 static int function_stat_show(struct seq_file *m, void *v)
366 struct ftrace_profile *rec = v;
367 char str[KSYM_SYMBOL_LEN];
368 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
369 static DEFINE_MUTEX(mutex);
370 static struct trace_seq s;
371 unsigned long long avg;
372 #endif
374 kallsyms_lookup(rec->ip, NULL, NULL, NULL, str);
375 seq_printf(m, " %-30.30s %10lu", str, rec->counter);
377 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
378 seq_printf(m, " ");
379 avg = rec->time;
380 do_div(avg, rec->counter);
382 mutex_lock(&mutex);
383 trace_seq_init(&s);
384 trace_print_graph_duration(rec->time, &s);
385 trace_seq_puts(&s, " ");
386 trace_print_graph_duration(avg, &s);
387 trace_print_seq(m, &s);
388 mutex_unlock(&mutex);
389 #endif
390 seq_putc(m, '\n');
392 return 0;
395 static void ftrace_profile_reset(struct ftrace_profile_stat *stat)
397 struct ftrace_profile_page *pg;
399 pg = stat->pages = stat->start;
401 while (pg) {
402 memset(pg->records, 0, PROFILE_RECORDS_SIZE);
403 pg->index = 0;
404 pg = pg->next;
407 memset(stat->hash, 0,
408 FTRACE_PROFILE_HASH_SIZE * sizeof(struct hlist_head));
411 int ftrace_profile_pages_init(struct ftrace_profile_stat *stat)
413 struct ftrace_profile_page *pg;
414 int functions;
415 int pages;
416 int i;
418 /* If we already allocated, do nothing */
419 if (stat->pages)
420 return 0;
422 stat->pages = (void *)get_zeroed_page(GFP_KERNEL);
423 if (!stat->pages)
424 return -ENOMEM;
426 #ifdef CONFIG_DYNAMIC_FTRACE
427 functions = ftrace_update_tot_cnt;
428 #else
430 * We do not know the number of functions that exist because
431 * dynamic tracing is what counts them. With past experience
432 * we have around 20K functions. That should be more than enough.
433 * It is highly unlikely we will execute every function in
434 * the kernel.
436 functions = 20000;
437 #endif
439 pg = stat->start = stat->pages;
441 pages = DIV_ROUND_UP(functions, PROFILES_PER_PAGE);
443 for (i = 0; i < pages; i++) {
444 pg->next = (void *)get_zeroed_page(GFP_KERNEL);
445 if (!pg->next)
446 goto out_free;
447 pg = pg->next;
450 return 0;
452 out_free:
453 pg = stat->start;
454 while (pg) {
455 unsigned long tmp = (unsigned long)pg;
457 pg = pg->next;
458 free_page(tmp);
461 free_page((unsigned long)stat->pages);
462 stat->pages = NULL;
463 stat->start = NULL;
465 return -ENOMEM;
468 static int ftrace_profile_init_cpu(int cpu)
470 struct ftrace_profile_stat *stat;
471 int size;
473 stat = &per_cpu(ftrace_profile_stats, cpu);
475 if (stat->hash) {
476 /* If the profile is already created, simply reset it */
477 ftrace_profile_reset(stat);
478 return 0;
482 * We are profiling all functions, but usually only a few thousand
483 * functions are hit. We'll make a hash of 1024 items.
485 size = FTRACE_PROFILE_HASH_SIZE;
487 stat->hash = kzalloc(sizeof(struct hlist_head) * size, GFP_KERNEL);
489 if (!stat->hash)
490 return -ENOMEM;
492 if (!ftrace_profile_bits) {
493 size--;
495 for (; size; size >>= 1)
496 ftrace_profile_bits++;
499 /* Preallocate the function profiling pages */
500 if (ftrace_profile_pages_init(stat) < 0) {
501 kfree(stat->hash);
502 stat->hash = NULL;
503 return -ENOMEM;
506 return 0;
509 static int ftrace_profile_init(void)
511 int cpu;
512 int ret = 0;
514 for_each_online_cpu(cpu) {
515 ret = ftrace_profile_init_cpu(cpu);
516 if (ret)
517 break;
520 return ret;
523 /* interrupts must be disabled */
524 static struct ftrace_profile *
525 ftrace_find_profiled_func(struct ftrace_profile_stat *stat, unsigned long ip)
527 struct ftrace_profile *rec;
528 struct hlist_head *hhd;
529 struct hlist_node *n;
530 unsigned long key;
532 key = hash_long(ip, ftrace_profile_bits);
533 hhd = &stat->hash[key];
535 if (hlist_empty(hhd))
536 return NULL;
538 hlist_for_each_entry_rcu(rec, n, hhd, node) {
539 if (rec->ip == ip)
540 return rec;
543 return NULL;
546 static void ftrace_add_profile(struct ftrace_profile_stat *stat,
547 struct ftrace_profile *rec)
549 unsigned long key;
551 key = hash_long(rec->ip, ftrace_profile_bits);
552 hlist_add_head_rcu(&rec->node, &stat->hash[key]);
556 * The memory is already allocated, this simply finds a new record to use.
558 static struct ftrace_profile *
559 ftrace_profile_alloc(struct ftrace_profile_stat *stat, unsigned long ip)
561 struct ftrace_profile *rec = NULL;
563 /* prevent recursion (from NMIs) */
564 if (atomic_inc_return(&stat->disabled) != 1)
565 goto out;
568 * Try to find the function again since an NMI
569 * could have added it
571 rec = ftrace_find_profiled_func(stat, ip);
572 if (rec)
573 goto out;
575 if (stat->pages->index == PROFILES_PER_PAGE) {
576 if (!stat->pages->next)
577 goto out;
578 stat->pages = stat->pages->next;
581 rec = &stat->pages->records[stat->pages->index++];
582 rec->ip = ip;
583 ftrace_add_profile(stat, rec);
585 out:
586 atomic_dec(&stat->disabled);
588 return rec;
591 static void
592 function_profile_call(unsigned long ip, unsigned long parent_ip)
594 struct ftrace_profile_stat *stat;
595 struct ftrace_profile *rec;
596 unsigned long flags;
598 if (!ftrace_profile_enabled)
599 return;
601 local_irq_save(flags);
603 stat = &__get_cpu_var(ftrace_profile_stats);
604 if (!stat->hash || !ftrace_profile_enabled)
605 goto out;
607 rec = ftrace_find_profiled_func(stat, ip);
608 if (!rec) {
609 rec = ftrace_profile_alloc(stat, ip);
610 if (!rec)
611 goto out;
614 rec->counter++;
615 out:
616 local_irq_restore(flags);
619 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
620 static int profile_graph_entry(struct ftrace_graph_ent *trace)
622 function_profile_call(trace->func, 0);
623 return 1;
626 static void profile_graph_return(struct ftrace_graph_ret *trace)
628 struct ftrace_profile_stat *stat;
629 unsigned long long calltime;
630 struct ftrace_profile *rec;
631 unsigned long flags;
633 local_irq_save(flags);
634 stat = &__get_cpu_var(ftrace_profile_stats);
635 if (!stat->hash || !ftrace_profile_enabled)
636 goto out;
638 calltime = trace->rettime - trace->calltime;
640 if (!(trace_flags & TRACE_ITER_GRAPH_TIME)) {
641 int index;
643 index = trace->depth;
645 /* Append this call time to the parent time to subtract */
646 if (index)
647 current->ret_stack[index - 1].subtime += calltime;
649 if (current->ret_stack[index].subtime < calltime)
650 calltime -= current->ret_stack[index].subtime;
651 else
652 calltime = 0;
655 rec = ftrace_find_profiled_func(stat, trace->func);
656 if (rec)
657 rec->time += calltime;
659 out:
660 local_irq_restore(flags);
663 static int register_ftrace_profiler(void)
665 return register_ftrace_graph(&profile_graph_return,
666 &profile_graph_entry);
669 static void unregister_ftrace_profiler(void)
671 unregister_ftrace_graph();
673 #else
674 static struct ftrace_ops ftrace_profile_ops __read_mostly =
676 .func = function_profile_call,
679 static int register_ftrace_profiler(void)
681 return register_ftrace_function(&ftrace_profile_ops);
684 static void unregister_ftrace_profiler(void)
686 unregister_ftrace_function(&ftrace_profile_ops);
688 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
690 static ssize_t
691 ftrace_profile_write(struct file *filp, const char __user *ubuf,
692 size_t cnt, loff_t *ppos)
694 unsigned long val;
695 char buf[64]; /* big enough to hold a number */
696 int ret;
698 if (cnt >= sizeof(buf))
699 return -EINVAL;
701 if (copy_from_user(&buf, ubuf, cnt))
702 return -EFAULT;
704 buf[cnt] = 0;
706 ret = strict_strtoul(buf, 10, &val);
707 if (ret < 0)
708 return ret;
710 val = !!val;
712 mutex_lock(&ftrace_profile_lock);
713 if (ftrace_profile_enabled ^ val) {
714 if (val) {
715 ret = ftrace_profile_init();
716 if (ret < 0) {
717 cnt = ret;
718 goto out;
721 ret = register_ftrace_profiler();
722 if (ret < 0) {
723 cnt = ret;
724 goto out;
726 ftrace_profile_enabled = 1;
727 } else {
728 ftrace_profile_enabled = 0;
730 * unregister_ftrace_profiler calls stop_machine
731 * so this acts like an synchronize_sched.
733 unregister_ftrace_profiler();
736 out:
737 mutex_unlock(&ftrace_profile_lock);
739 filp->f_pos += cnt;
741 return cnt;
744 static ssize_t
745 ftrace_profile_read(struct file *filp, char __user *ubuf,
746 size_t cnt, loff_t *ppos)
748 char buf[64]; /* big enough to hold a number */
749 int r;
751 r = sprintf(buf, "%u\n", ftrace_profile_enabled);
752 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
755 static const struct file_operations ftrace_profile_fops = {
756 .open = tracing_open_generic,
757 .read = ftrace_profile_read,
758 .write = ftrace_profile_write,
761 /* used to initialize the real stat files */
762 static struct tracer_stat function_stats __initdata = {
763 .name = "functions",
764 .stat_start = function_stat_start,
765 .stat_next = function_stat_next,
766 .stat_cmp = function_stat_cmp,
767 .stat_headers = function_stat_headers,
768 .stat_show = function_stat_show
771 static __init void ftrace_profile_debugfs(struct dentry *d_tracer)
773 struct ftrace_profile_stat *stat;
774 struct dentry *entry;
775 char *name;
776 int ret;
777 int cpu;
779 for_each_possible_cpu(cpu) {
780 stat = &per_cpu(ftrace_profile_stats, cpu);
782 /* allocate enough for function name + cpu number */
783 name = kmalloc(32, GFP_KERNEL);
784 if (!name) {
786 * The files created are permanent, if something happens
787 * we still do not free memory.
789 WARN(1,
790 "Could not allocate stat file for cpu %d\n",
791 cpu);
792 return;
794 stat->stat = function_stats;
795 snprintf(name, 32, "function%d", cpu);
796 stat->stat.name = name;
797 ret = register_stat_tracer(&stat->stat);
798 if (ret) {
799 WARN(1,
800 "Could not register function stat for cpu %d\n",
801 cpu);
802 kfree(name);
803 return;
807 entry = debugfs_create_file("function_profile_enabled", 0644,
808 d_tracer, NULL, &ftrace_profile_fops);
809 if (!entry)
810 pr_warning("Could not create debugfs "
811 "'function_profile_enabled' entry\n");
814 #else /* CONFIG_FUNCTION_PROFILER */
815 static __init void ftrace_profile_debugfs(struct dentry *d_tracer)
818 #endif /* CONFIG_FUNCTION_PROFILER */
820 /* set when tracing only a pid */
821 struct pid *ftrace_pid_trace;
822 static struct pid * const ftrace_swapper_pid = &init_struct_pid;
824 #ifdef CONFIG_DYNAMIC_FTRACE
826 #ifndef CONFIG_FTRACE_MCOUNT_RECORD
827 # error Dynamic ftrace depends on MCOUNT_RECORD
828 #endif
830 static struct hlist_head ftrace_func_hash[FTRACE_FUNC_HASHSIZE] __read_mostly;
832 struct ftrace_func_probe {
833 struct hlist_node node;
834 struct ftrace_probe_ops *ops;
835 unsigned long flags;
836 unsigned long ip;
837 void *data;
838 struct rcu_head rcu;
841 enum {
842 FTRACE_ENABLE_CALLS = (1 << 0),
843 FTRACE_DISABLE_CALLS = (1 << 1),
844 FTRACE_UPDATE_TRACE_FUNC = (1 << 2),
845 FTRACE_ENABLE_MCOUNT = (1 << 3),
846 FTRACE_DISABLE_MCOUNT = (1 << 4),
847 FTRACE_START_FUNC_RET = (1 << 5),
848 FTRACE_STOP_FUNC_RET = (1 << 6),
851 static int ftrace_filtered;
853 static struct dyn_ftrace *ftrace_new_addrs;
855 static DEFINE_MUTEX(ftrace_regex_lock);
857 struct ftrace_page {
858 struct ftrace_page *next;
859 int index;
860 struct dyn_ftrace records[];
863 #define ENTRIES_PER_PAGE \
864 ((PAGE_SIZE - sizeof(struct ftrace_page)) / sizeof(struct dyn_ftrace))
866 /* estimate from running different kernels */
867 #define NR_TO_INIT 10000
869 static struct ftrace_page *ftrace_pages_start;
870 static struct ftrace_page *ftrace_pages;
872 static struct dyn_ftrace *ftrace_free_records;
875 * This is a double for. Do not use 'break' to break out of the loop,
876 * you must use a goto.
878 #define do_for_each_ftrace_rec(pg, rec) \
879 for (pg = ftrace_pages_start; pg; pg = pg->next) { \
880 int _____i; \
881 for (_____i = 0; _____i < pg->index; _____i++) { \
882 rec = &pg->records[_____i];
884 #define while_for_each_ftrace_rec() \
888 #ifdef CONFIG_KPROBES
890 static int frozen_record_count;
892 static inline void freeze_record(struct dyn_ftrace *rec)
894 if (!(rec->flags & FTRACE_FL_FROZEN)) {
895 rec->flags |= FTRACE_FL_FROZEN;
896 frozen_record_count++;
900 static inline void unfreeze_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 int record_frozen(struct dyn_ftrace *rec)
910 return rec->flags & FTRACE_FL_FROZEN;
912 #else
913 # define freeze_record(rec) ({ 0; })
914 # define unfreeze_record(rec) ({ 0; })
915 # define record_frozen(rec) ({ 0; })
916 #endif /* CONFIG_KPROBES */
918 static void ftrace_free_rec(struct dyn_ftrace *rec)
920 rec->freelist = ftrace_free_records;
921 ftrace_free_records = rec;
922 rec->flags |= FTRACE_FL_FREE;
925 static struct dyn_ftrace *ftrace_alloc_dyn_node(unsigned long ip)
927 struct dyn_ftrace *rec;
929 /* First check for freed records */
930 if (ftrace_free_records) {
931 rec = ftrace_free_records;
933 if (unlikely(!(rec->flags & FTRACE_FL_FREE))) {
934 FTRACE_WARN_ON_ONCE(1);
935 ftrace_free_records = NULL;
936 return NULL;
939 ftrace_free_records = rec->freelist;
940 memset(rec, 0, sizeof(*rec));
941 return rec;
944 if (ftrace_pages->index == ENTRIES_PER_PAGE) {
945 if (!ftrace_pages->next) {
946 /* allocate another page */
947 ftrace_pages->next =
948 (void *)get_zeroed_page(GFP_KERNEL);
949 if (!ftrace_pages->next)
950 return NULL;
952 ftrace_pages = ftrace_pages->next;
955 return &ftrace_pages->records[ftrace_pages->index++];
958 static struct dyn_ftrace *
959 ftrace_record_ip(unsigned long ip)
961 struct dyn_ftrace *rec;
963 if (ftrace_disabled)
964 return NULL;
966 rec = ftrace_alloc_dyn_node(ip);
967 if (!rec)
968 return NULL;
970 rec->ip = ip;
971 rec->newlist = ftrace_new_addrs;
972 ftrace_new_addrs = rec;
974 return rec;
977 static void print_ip_ins(const char *fmt, unsigned char *p)
979 int i;
981 printk(KERN_CONT "%s", fmt);
983 for (i = 0; i < MCOUNT_INSN_SIZE; i++)
984 printk(KERN_CONT "%s%02x", i ? ":" : "", p[i]);
987 static void ftrace_bug(int failed, unsigned long ip)
989 switch (failed) {
990 case -EFAULT:
991 FTRACE_WARN_ON_ONCE(1);
992 pr_info("ftrace faulted on modifying ");
993 print_ip_sym(ip);
994 break;
995 case -EINVAL:
996 FTRACE_WARN_ON_ONCE(1);
997 pr_info("ftrace failed to modify ");
998 print_ip_sym(ip);
999 print_ip_ins(" actual: ", (unsigned char *)ip);
1000 printk(KERN_CONT "\n");
1001 break;
1002 case -EPERM:
1003 FTRACE_WARN_ON_ONCE(1);
1004 pr_info("ftrace faulted on writing ");
1005 print_ip_sym(ip);
1006 break;
1007 default:
1008 FTRACE_WARN_ON_ONCE(1);
1009 pr_info("ftrace faulted on unknown error ");
1010 print_ip_sym(ip);
1015 static int
1016 __ftrace_replace_code(struct dyn_ftrace *rec, int enable)
1018 unsigned long ftrace_addr;
1019 unsigned long flag = 0UL;
1021 ftrace_addr = (unsigned long)FTRACE_ADDR;
1024 * If this record is not to be traced or we want to disable it,
1025 * then disable it.
1027 * If we want to enable it and filtering is off, then enable it.
1029 * If we want to enable it and filtering is on, enable it only if
1030 * it's filtered
1032 if (enable && !(rec->flags & FTRACE_FL_NOTRACE)) {
1033 if (!ftrace_filtered || (rec->flags & FTRACE_FL_FILTER))
1034 flag = FTRACE_FL_ENABLED;
1037 /* If the state of this record hasn't changed, then do nothing */
1038 if ((rec->flags & FTRACE_FL_ENABLED) == flag)
1039 return 0;
1041 if (flag) {
1042 rec->flags |= FTRACE_FL_ENABLED;
1043 return ftrace_make_call(rec, ftrace_addr);
1046 rec->flags &= ~FTRACE_FL_ENABLED;
1047 return ftrace_make_nop(NULL, rec, ftrace_addr);
1050 static void ftrace_replace_code(int enable)
1052 struct dyn_ftrace *rec;
1053 struct ftrace_page *pg;
1054 int failed;
1056 do_for_each_ftrace_rec(pg, rec) {
1058 * Skip over free records, records that have
1059 * failed and not converted.
1061 if (rec->flags & FTRACE_FL_FREE ||
1062 rec->flags & FTRACE_FL_FAILED ||
1063 !(rec->flags & FTRACE_FL_CONVERTED))
1064 continue;
1066 /* ignore updates to this record's mcount site */
1067 if (get_kprobe((void *)rec->ip)) {
1068 freeze_record(rec);
1069 continue;
1070 } else {
1071 unfreeze_record(rec);
1074 failed = __ftrace_replace_code(rec, enable);
1075 if (failed) {
1076 rec->flags |= FTRACE_FL_FAILED;
1077 if ((system_state == SYSTEM_BOOTING) ||
1078 !core_kernel_text(rec->ip)) {
1079 ftrace_free_rec(rec);
1080 } else {
1081 ftrace_bug(failed, rec->ip);
1082 /* Stop processing */
1083 return;
1086 } while_for_each_ftrace_rec();
1089 static int
1090 ftrace_code_disable(struct module *mod, struct dyn_ftrace *rec)
1092 unsigned long ip;
1093 int ret;
1095 ip = rec->ip;
1097 ret = ftrace_make_nop(mod, rec, MCOUNT_ADDR);
1098 if (ret) {
1099 ftrace_bug(ret, ip);
1100 rec->flags |= FTRACE_FL_FAILED;
1101 return 0;
1103 return 1;
1107 * archs can override this function if they must do something
1108 * before the modifying code is performed.
1110 int __weak ftrace_arch_code_modify_prepare(void)
1112 return 0;
1116 * archs can override this function if they must do something
1117 * after the modifying code is performed.
1119 int __weak ftrace_arch_code_modify_post_process(void)
1121 return 0;
1124 static int __ftrace_modify_code(void *data)
1126 int *command = data;
1128 if (*command & FTRACE_ENABLE_CALLS)
1129 ftrace_replace_code(1);
1130 else if (*command & FTRACE_DISABLE_CALLS)
1131 ftrace_replace_code(0);
1133 if (*command & FTRACE_UPDATE_TRACE_FUNC)
1134 ftrace_update_ftrace_func(ftrace_trace_function);
1136 if (*command & FTRACE_START_FUNC_RET)
1137 ftrace_enable_ftrace_graph_caller();
1138 else if (*command & FTRACE_STOP_FUNC_RET)
1139 ftrace_disable_ftrace_graph_caller();
1141 return 0;
1144 static void ftrace_run_update_code(int command)
1146 int ret;
1148 ret = ftrace_arch_code_modify_prepare();
1149 FTRACE_WARN_ON(ret);
1150 if (ret)
1151 return;
1153 stop_machine(__ftrace_modify_code, &command, NULL);
1155 ret = ftrace_arch_code_modify_post_process();
1156 FTRACE_WARN_ON(ret);
1159 static ftrace_func_t saved_ftrace_func;
1160 static int ftrace_start_up;
1162 static void ftrace_startup_enable(int command)
1164 if (saved_ftrace_func != ftrace_trace_function) {
1165 saved_ftrace_func = ftrace_trace_function;
1166 command |= FTRACE_UPDATE_TRACE_FUNC;
1169 if (!command || !ftrace_enabled)
1170 return;
1172 ftrace_run_update_code(command);
1175 static void ftrace_startup(int command)
1177 if (unlikely(ftrace_disabled))
1178 return;
1180 ftrace_start_up++;
1181 command |= FTRACE_ENABLE_CALLS;
1183 ftrace_startup_enable(command);
1186 static void ftrace_shutdown(int command)
1188 if (unlikely(ftrace_disabled))
1189 return;
1191 ftrace_start_up--;
1193 * Just warn in case of unbalance, no need to kill ftrace, it's not
1194 * critical but the ftrace_call callers may be never nopped again after
1195 * further ftrace uses.
1197 WARN_ON_ONCE(ftrace_start_up < 0);
1199 if (!ftrace_start_up)
1200 command |= FTRACE_DISABLE_CALLS;
1202 if (saved_ftrace_func != ftrace_trace_function) {
1203 saved_ftrace_func = ftrace_trace_function;
1204 command |= FTRACE_UPDATE_TRACE_FUNC;
1207 if (!command || !ftrace_enabled)
1208 return;
1210 ftrace_run_update_code(command);
1213 static void ftrace_startup_sysctl(void)
1215 int command = FTRACE_ENABLE_MCOUNT;
1217 if (unlikely(ftrace_disabled))
1218 return;
1220 /* Force update next time */
1221 saved_ftrace_func = NULL;
1222 /* ftrace_start_up is true if we want ftrace running */
1223 if (ftrace_start_up)
1224 command |= FTRACE_ENABLE_CALLS;
1226 ftrace_run_update_code(command);
1229 static void ftrace_shutdown_sysctl(void)
1231 int command = FTRACE_DISABLE_MCOUNT;
1233 if (unlikely(ftrace_disabled))
1234 return;
1236 /* ftrace_start_up is true if ftrace is running */
1237 if (ftrace_start_up)
1238 command |= FTRACE_DISABLE_CALLS;
1240 ftrace_run_update_code(command);
1243 static cycle_t ftrace_update_time;
1244 static unsigned long ftrace_update_cnt;
1245 unsigned long ftrace_update_tot_cnt;
1247 static int ftrace_update_code(struct module *mod)
1249 struct dyn_ftrace *p;
1250 cycle_t start, stop;
1252 start = ftrace_now(raw_smp_processor_id());
1253 ftrace_update_cnt = 0;
1255 while (ftrace_new_addrs) {
1257 /* If something went wrong, bail without enabling anything */
1258 if (unlikely(ftrace_disabled))
1259 return -1;
1261 p = ftrace_new_addrs;
1262 ftrace_new_addrs = p->newlist;
1263 p->flags = 0L;
1265 /* convert record (i.e, patch mcount-call with NOP) */
1266 if (ftrace_code_disable(mod, p)) {
1267 p->flags |= FTRACE_FL_CONVERTED;
1268 ftrace_update_cnt++;
1269 } else
1270 ftrace_free_rec(p);
1273 stop = ftrace_now(raw_smp_processor_id());
1274 ftrace_update_time = stop - start;
1275 ftrace_update_tot_cnt += ftrace_update_cnt;
1277 return 0;
1280 static int __init ftrace_dyn_table_alloc(unsigned long num_to_init)
1282 struct ftrace_page *pg;
1283 int cnt;
1284 int i;
1286 /* allocate a few pages */
1287 ftrace_pages_start = (void *)get_zeroed_page(GFP_KERNEL);
1288 if (!ftrace_pages_start)
1289 return -1;
1292 * Allocate a few more pages.
1294 * TODO: have some parser search vmlinux before
1295 * final linking to find all calls to ftrace.
1296 * Then we can:
1297 * a) know how many pages to allocate.
1298 * and/or
1299 * b) set up the table then.
1301 * The dynamic code is still necessary for
1302 * modules.
1305 pg = ftrace_pages = ftrace_pages_start;
1307 cnt = num_to_init / ENTRIES_PER_PAGE;
1308 pr_info("ftrace: allocating %ld entries in %d pages\n",
1309 num_to_init, cnt + 1);
1311 for (i = 0; i < cnt; i++) {
1312 pg->next = (void *)get_zeroed_page(GFP_KERNEL);
1314 /* If we fail, we'll try later anyway */
1315 if (!pg->next)
1316 break;
1318 pg = pg->next;
1321 return 0;
1324 enum {
1325 FTRACE_ITER_FILTER = (1 << 0),
1326 FTRACE_ITER_CONT = (1 << 1),
1327 FTRACE_ITER_NOTRACE = (1 << 2),
1328 FTRACE_ITER_FAILURES = (1 << 3),
1329 FTRACE_ITER_PRINTALL = (1 << 4),
1330 FTRACE_ITER_HASH = (1 << 5),
1333 #define FTRACE_BUFF_MAX (KSYM_SYMBOL_LEN+4) /* room for wildcards */
1335 struct ftrace_iterator {
1336 struct ftrace_page *pg;
1337 int hidx;
1338 int idx;
1339 unsigned flags;
1340 unsigned char buffer[FTRACE_BUFF_MAX+1];
1341 unsigned buffer_idx;
1344 static void *
1345 t_hash_next(struct seq_file *m, void *v, loff_t *pos)
1347 struct ftrace_iterator *iter = m->private;
1348 struct hlist_node *hnd = v;
1349 struct hlist_head *hhd;
1351 WARN_ON(!(iter->flags & FTRACE_ITER_HASH));
1353 (*pos)++;
1355 retry:
1356 if (iter->hidx >= FTRACE_FUNC_HASHSIZE)
1357 return NULL;
1359 hhd = &ftrace_func_hash[iter->hidx];
1361 if (hlist_empty(hhd)) {
1362 iter->hidx++;
1363 hnd = NULL;
1364 goto retry;
1367 if (!hnd)
1368 hnd = hhd->first;
1369 else {
1370 hnd = hnd->next;
1371 if (!hnd) {
1372 iter->hidx++;
1373 goto retry;
1377 return hnd;
1380 static void *t_hash_start(struct seq_file *m, loff_t *pos)
1382 struct ftrace_iterator *iter = m->private;
1383 void *p = NULL;
1384 loff_t l;
1386 if (!(iter->flags & FTRACE_ITER_HASH))
1387 *pos = 0;
1389 iter->flags |= FTRACE_ITER_HASH;
1391 iter->hidx = 0;
1392 for (l = 0; l <= *pos; ) {
1393 p = t_hash_next(m, p, &l);
1394 if (!p)
1395 break;
1397 return p;
1400 static int t_hash_show(struct seq_file *m, void *v)
1402 struct ftrace_func_probe *rec;
1403 struct hlist_node *hnd = v;
1405 rec = hlist_entry(hnd, struct ftrace_func_probe, node);
1407 if (rec->ops->print)
1408 return rec->ops->print(m, rec->ip, rec->ops, rec->data);
1410 seq_printf(m, "%pf:%pf", (void *)rec->ip, (void *)rec->ops->func);
1412 if (rec->data)
1413 seq_printf(m, ":%p", rec->data);
1414 seq_putc(m, '\n');
1416 return 0;
1419 static void *
1420 t_next(struct seq_file *m, void *v, loff_t *pos)
1422 struct ftrace_iterator *iter = m->private;
1423 struct dyn_ftrace *rec = NULL;
1425 if (iter->flags & FTRACE_ITER_HASH)
1426 return t_hash_next(m, v, pos);
1428 (*pos)++;
1430 if (iter->flags & FTRACE_ITER_PRINTALL)
1431 return NULL;
1433 retry:
1434 if (iter->idx >= iter->pg->index) {
1435 if (iter->pg->next) {
1436 iter->pg = iter->pg->next;
1437 iter->idx = 0;
1438 goto retry;
1440 } else {
1441 rec = &iter->pg->records[iter->idx++];
1442 if ((rec->flags & FTRACE_FL_FREE) ||
1444 (!(iter->flags & FTRACE_ITER_FAILURES) &&
1445 (rec->flags & FTRACE_FL_FAILED)) ||
1447 ((iter->flags & FTRACE_ITER_FAILURES) &&
1448 !(rec->flags & FTRACE_FL_FAILED)) ||
1450 ((iter->flags & FTRACE_ITER_FILTER) &&
1451 !(rec->flags & FTRACE_FL_FILTER)) ||
1453 ((iter->flags & FTRACE_ITER_NOTRACE) &&
1454 !(rec->flags & FTRACE_FL_NOTRACE))) {
1455 rec = NULL;
1456 goto retry;
1460 return rec;
1463 static void *t_start(struct seq_file *m, loff_t *pos)
1465 struct ftrace_iterator *iter = m->private;
1466 void *p = NULL;
1467 loff_t l;
1469 mutex_lock(&ftrace_lock);
1471 * For set_ftrace_filter reading, if we have the filter
1472 * off, we can short cut and just print out that all
1473 * functions are enabled.
1475 if (iter->flags & FTRACE_ITER_FILTER && !ftrace_filtered) {
1476 if (*pos > 0)
1477 return t_hash_start(m, pos);
1478 iter->flags |= FTRACE_ITER_PRINTALL;
1479 return iter;
1482 if (iter->flags & FTRACE_ITER_HASH)
1483 return t_hash_start(m, pos);
1485 iter->pg = ftrace_pages_start;
1486 iter->idx = 0;
1487 for (l = 0; l <= *pos; ) {
1488 p = t_next(m, p, &l);
1489 if (!p)
1490 break;
1493 if (!p && iter->flags & FTRACE_ITER_FILTER)
1494 return t_hash_start(m, pos);
1496 return p;
1499 static void t_stop(struct seq_file *m, void *p)
1501 mutex_unlock(&ftrace_lock);
1504 static int t_show(struct seq_file *m, void *v)
1506 struct ftrace_iterator *iter = m->private;
1507 struct dyn_ftrace *rec = v;
1509 if (iter->flags & FTRACE_ITER_HASH)
1510 return t_hash_show(m, v);
1512 if (iter->flags & FTRACE_ITER_PRINTALL) {
1513 seq_printf(m, "#### all functions enabled ####\n");
1514 return 0;
1517 if (!rec)
1518 return 0;
1520 seq_printf(m, "%pf\n", (void *)rec->ip);
1522 return 0;
1525 static struct seq_operations show_ftrace_seq_ops = {
1526 .start = t_start,
1527 .next = t_next,
1528 .stop = t_stop,
1529 .show = t_show,
1532 static int
1533 ftrace_avail_open(struct inode *inode, struct file *file)
1535 struct ftrace_iterator *iter;
1536 int ret;
1538 if (unlikely(ftrace_disabled))
1539 return -ENODEV;
1541 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
1542 if (!iter)
1543 return -ENOMEM;
1545 iter->pg = ftrace_pages_start;
1547 ret = seq_open(file, &show_ftrace_seq_ops);
1548 if (!ret) {
1549 struct seq_file *m = file->private_data;
1551 m->private = iter;
1552 } else {
1553 kfree(iter);
1556 return ret;
1559 static int
1560 ftrace_failures_open(struct inode *inode, struct file *file)
1562 int ret;
1563 struct seq_file *m;
1564 struct ftrace_iterator *iter;
1566 ret = ftrace_avail_open(inode, file);
1567 if (!ret) {
1568 m = (struct seq_file *)file->private_data;
1569 iter = (struct ftrace_iterator *)m->private;
1570 iter->flags = FTRACE_ITER_FAILURES;
1573 return ret;
1577 static void ftrace_filter_reset(int enable)
1579 struct ftrace_page *pg;
1580 struct dyn_ftrace *rec;
1581 unsigned long type = enable ? FTRACE_FL_FILTER : FTRACE_FL_NOTRACE;
1583 mutex_lock(&ftrace_lock);
1584 if (enable)
1585 ftrace_filtered = 0;
1586 do_for_each_ftrace_rec(pg, rec) {
1587 if (rec->flags & FTRACE_FL_FAILED)
1588 continue;
1589 rec->flags &= ~type;
1590 } while_for_each_ftrace_rec();
1591 mutex_unlock(&ftrace_lock);
1594 static int
1595 ftrace_regex_open(struct inode *inode, struct file *file, int enable)
1597 struct ftrace_iterator *iter;
1598 int ret = 0;
1600 if (unlikely(ftrace_disabled))
1601 return -ENODEV;
1603 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
1604 if (!iter)
1605 return -ENOMEM;
1607 mutex_lock(&ftrace_regex_lock);
1608 if ((file->f_mode & FMODE_WRITE) &&
1609 (file->f_flags & O_TRUNC))
1610 ftrace_filter_reset(enable);
1612 if (file->f_mode & FMODE_READ) {
1613 iter->pg = ftrace_pages_start;
1614 iter->flags = enable ? FTRACE_ITER_FILTER :
1615 FTRACE_ITER_NOTRACE;
1617 ret = seq_open(file, &show_ftrace_seq_ops);
1618 if (!ret) {
1619 struct seq_file *m = file->private_data;
1620 m->private = iter;
1621 } else
1622 kfree(iter);
1623 } else
1624 file->private_data = iter;
1625 mutex_unlock(&ftrace_regex_lock);
1627 return ret;
1630 static int
1631 ftrace_filter_open(struct inode *inode, struct file *file)
1633 return ftrace_regex_open(inode, file, 1);
1636 static int
1637 ftrace_notrace_open(struct inode *inode, struct file *file)
1639 return ftrace_regex_open(inode, file, 0);
1642 static loff_t
1643 ftrace_regex_lseek(struct file *file, loff_t offset, int origin)
1645 loff_t ret;
1647 if (file->f_mode & FMODE_READ)
1648 ret = seq_lseek(file, offset, origin);
1649 else
1650 file->f_pos = ret = 1;
1652 return ret;
1655 enum {
1656 MATCH_FULL,
1657 MATCH_FRONT_ONLY,
1658 MATCH_MIDDLE_ONLY,
1659 MATCH_END_ONLY,
1663 * (static function - no need for kernel doc)
1665 * Pass in a buffer containing a glob and this function will
1666 * set search to point to the search part of the buffer and
1667 * return the type of search it is (see enum above).
1668 * This does modify buff.
1670 * Returns enum type.
1671 * search returns the pointer to use for comparison.
1672 * not returns 1 if buff started with a '!'
1673 * 0 otherwise.
1675 static int
1676 ftrace_setup_glob(char *buff, int len, char **search, int *not)
1678 int type = MATCH_FULL;
1679 int i;
1681 if (buff[0] == '!') {
1682 *not = 1;
1683 buff++;
1684 len--;
1685 } else
1686 *not = 0;
1688 *search = buff;
1690 for (i = 0; i < len; i++) {
1691 if (buff[i] == '*') {
1692 if (!i) {
1693 *search = buff + 1;
1694 type = MATCH_END_ONLY;
1695 } else {
1696 if (type == MATCH_END_ONLY)
1697 type = MATCH_MIDDLE_ONLY;
1698 else
1699 type = MATCH_FRONT_ONLY;
1700 buff[i] = 0;
1701 break;
1706 return type;
1709 static int ftrace_match(char *str, char *regex, int len, int type)
1711 int matched = 0;
1712 char *ptr;
1714 switch (type) {
1715 case MATCH_FULL:
1716 if (strcmp(str, regex) == 0)
1717 matched = 1;
1718 break;
1719 case MATCH_FRONT_ONLY:
1720 if (strncmp(str, regex, len) == 0)
1721 matched = 1;
1722 break;
1723 case MATCH_MIDDLE_ONLY:
1724 if (strstr(str, regex))
1725 matched = 1;
1726 break;
1727 case MATCH_END_ONLY:
1728 ptr = strstr(str, regex);
1729 if (ptr && (ptr[len] == 0))
1730 matched = 1;
1731 break;
1734 return matched;
1737 static int
1738 ftrace_match_record(struct dyn_ftrace *rec, char *regex, int len, int type)
1740 char str[KSYM_SYMBOL_LEN];
1742 kallsyms_lookup(rec->ip, NULL, NULL, NULL, str);
1743 return ftrace_match(str, regex, len, type);
1746 static void ftrace_match_records(char *buff, int len, int enable)
1748 unsigned int search_len;
1749 struct ftrace_page *pg;
1750 struct dyn_ftrace *rec;
1751 unsigned long flag;
1752 char *search;
1753 int type;
1754 int not;
1756 flag = enable ? FTRACE_FL_FILTER : FTRACE_FL_NOTRACE;
1757 type = ftrace_setup_glob(buff, len, &search, &not);
1759 search_len = strlen(search);
1761 mutex_lock(&ftrace_lock);
1762 do_for_each_ftrace_rec(pg, rec) {
1764 if (rec->flags & FTRACE_FL_FAILED)
1765 continue;
1767 if (ftrace_match_record(rec, search, search_len, type)) {
1768 if (not)
1769 rec->flags &= ~flag;
1770 else
1771 rec->flags |= flag;
1774 * Only enable filtering if we have a function that
1775 * is filtered on.
1777 if (enable && (rec->flags & FTRACE_FL_FILTER))
1778 ftrace_filtered = 1;
1779 } while_for_each_ftrace_rec();
1780 mutex_unlock(&ftrace_lock);
1783 static int
1784 ftrace_match_module_record(struct dyn_ftrace *rec, char *mod,
1785 char *regex, int len, int type)
1787 char str[KSYM_SYMBOL_LEN];
1788 char *modname;
1790 kallsyms_lookup(rec->ip, NULL, NULL, &modname, str);
1792 if (!modname || strcmp(modname, mod))
1793 return 0;
1795 /* blank search means to match all funcs in the mod */
1796 if (len)
1797 return ftrace_match(str, regex, len, type);
1798 else
1799 return 1;
1802 static void ftrace_match_module_records(char *buff, char *mod, int enable)
1804 unsigned search_len = 0;
1805 struct ftrace_page *pg;
1806 struct dyn_ftrace *rec;
1807 int type = MATCH_FULL;
1808 char *search = buff;
1809 unsigned long flag;
1810 int not = 0;
1812 flag = enable ? FTRACE_FL_FILTER : FTRACE_FL_NOTRACE;
1814 /* blank or '*' mean the same */
1815 if (strcmp(buff, "*") == 0)
1816 buff[0] = 0;
1818 /* handle the case of 'dont filter this module' */
1819 if (strcmp(buff, "!") == 0 || strcmp(buff, "!*") == 0) {
1820 buff[0] = 0;
1821 not = 1;
1824 if (strlen(buff)) {
1825 type = ftrace_setup_glob(buff, strlen(buff), &search, &not);
1826 search_len = strlen(search);
1829 mutex_lock(&ftrace_lock);
1830 do_for_each_ftrace_rec(pg, rec) {
1832 if (rec->flags & FTRACE_FL_FAILED)
1833 continue;
1835 if (ftrace_match_module_record(rec, mod,
1836 search, search_len, type)) {
1837 if (not)
1838 rec->flags &= ~flag;
1839 else
1840 rec->flags |= flag;
1842 if (enable && (rec->flags & FTRACE_FL_FILTER))
1843 ftrace_filtered = 1;
1845 } while_for_each_ftrace_rec();
1846 mutex_unlock(&ftrace_lock);
1850 * We register the module command as a template to show others how
1851 * to register the a command as well.
1854 static int
1855 ftrace_mod_callback(char *func, char *cmd, char *param, int enable)
1857 char *mod;
1860 * cmd == 'mod' because we only registered this func
1861 * for the 'mod' ftrace_func_command.
1862 * But if you register one func with multiple commands,
1863 * you can tell which command was used by the cmd
1864 * parameter.
1867 /* we must have a module name */
1868 if (!param)
1869 return -EINVAL;
1871 mod = strsep(&param, ":");
1872 if (!strlen(mod))
1873 return -EINVAL;
1875 ftrace_match_module_records(func, mod, enable);
1876 return 0;
1879 static struct ftrace_func_command ftrace_mod_cmd = {
1880 .name = "mod",
1881 .func = ftrace_mod_callback,
1884 static int __init ftrace_mod_cmd_init(void)
1886 return register_ftrace_command(&ftrace_mod_cmd);
1888 device_initcall(ftrace_mod_cmd_init);
1890 static void
1891 function_trace_probe_call(unsigned long ip, unsigned long parent_ip)
1893 struct ftrace_func_probe *entry;
1894 struct hlist_head *hhd;
1895 struct hlist_node *n;
1896 unsigned long key;
1897 int resched;
1899 key = hash_long(ip, FTRACE_HASH_BITS);
1901 hhd = &ftrace_func_hash[key];
1903 if (hlist_empty(hhd))
1904 return;
1907 * Disable preemption for these calls to prevent a RCU grace
1908 * period. This syncs the hash iteration and freeing of items
1909 * on the hash. rcu_read_lock is too dangerous here.
1911 resched = ftrace_preempt_disable();
1912 hlist_for_each_entry_rcu(entry, n, hhd, node) {
1913 if (entry->ip == ip)
1914 entry->ops->func(ip, parent_ip, &entry->data);
1916 ftrace_preempt_enable(resched);
1919 static struct ftrace_ops trace_probe_ops __read_mostly =
1921 .func = function_trace_probe_call,
1924 static int ftrace_probe_registered;
1926 static void __enable_ftrace_function_probe(void)
1928 int i;
1930 if (ftrace_probe_registered)
1931 return;
1933 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
1934 struct hlist_head *hhd = &ftrace_func_hash[i];
1935 if (hhd->first)
1936 break;
1938 /* Nothing registered? */
1939 if (i == FTRACE_FUNC_HASHSIZE)
1940 return;
1942 __register_ftrace_function(&trace_probe_ops);
1943 ftrace_startup(0);
1944 ftrace_probe_registered = 1;
1947 static void __disable_ftrace_function_probe(void)
1949 int i;
1951 if (!ftrace_probe_registered)
1952 return;
1954 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
1955 struct hlist_head *hhd = &ftrace_func_hash[i];
1956 if (hhd->first)
1957 return;
1960 /* no more funcs left */
1961 __unregister_ftrace_function(&trace_probe_ops);
1962 ftrace_shutdown(0);
1963 ftrace_probe_registered = 0;
1967 static void ftrace_free_entry_rcu(struct rcu_head *rhp)
1969 struct ftrace_func_probe *entry =
1970 container_of(rhp, struct ftrace_func_probe, rcu);
1972 if (entry->ops->free)
1973 entry->ops->free(&entry->data);
1974 kfree(entry);
1979 register_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
1980 void *data)
1982 struct ftrace_func_probe *entry;
1983 struct ftrace_page *pg;
1984 struct dyn_ftrace *rec;
1985 int type, len, not;
1986 unsigned long key;
1987 int count = 0;
1988 char *search;
1990 type = ftrace_setup_glob(glob, strlen(glob), &search, &not);
1991 len = strlen(search);
1993 /* we do not support '!' for function probes */
1994 if (WARN_ON(not))
1995 return -EINVAL;
1997 mutex_lock(&ftrace_lock);
1998 do_for_each_ftrace_rec(pg, rec) {
2000 if (rec->flags & FTRACE_FL_FAILED)
2001 continue;
2003 if (!ftrace_match_record(rec, search, len, type))
2004 continue;
2006 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
2007 if (!entry) {
2008 /* If we did not process any, then return error */
2009 if (!count)
2010 count = -ENOMEM;
2011 goto out_unlock;
2014 count++;
2016 entry->data = data;
2019 * The caller might want to do something special
2020 * for each function we find. We call the callback
2021 * to give the caller an opportunity to do so.
2023 if (ops->callback) {
2024 if (ops->callback(rec->ip, &entry->data) < 0) {
2025 /* caller does not like this func */
2026 kfree(entry);
2027 continue;
2031 entry->ops = ops;
2032 entry->ip = rec->ip;
2034 key = hash_long(entry->ip, FTRACE_HASH_BITS);
2035 hlist_add_head_rcu(&entry->node, &ftrace_func_hash[key]);
2037 } while_for_each_ftrace_rec();
2038 __enable_ftrace_function_probe();
2040 out_unlock:
2041 mutex_unlock(&ftrace_lock);
2043 return count;
2046 enum {
2047 PROBE_TEST_FUNC = 1,
2048 PROBE_TEST_DATA = 2
2051 static void
2052 __unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
2053 void *data, int flags)
2055 struct ftrace_func_probe *entry;
2056 struct hlist_node *n, *tmp;
2057 char str[KSYM_SYMBOL_LEN];
2058 int type = MATCH_FULL;
2059 int i, len = 0;
2060 char *search;
2062 if (glob && (strcmp(glob, "*") || !strlen(glob)))
2063 glob = NULL;
2064 else {
2065 int not;
2067 type = ftrace_setup_glob(glob, strlen(glob), &search, &not);
2068 len = strlen(search);
2070 /* we do not support '!' for function probes */
2071 if (WARN_ON(not))
2072 return;
2075 mutex_lock(&ftrace_lock);
2076 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
2077 struct hlist_head *hhd = &ftrace_func_hash[i];
2079 hlist_for_each_entry_safe(entry, n, tmp, hhd, node) {
2081 /* break up if statements for readability */
2082 if ((flags & PROBE_TEST_FUNC) && entry->ops != ops)
2083 continue;
2085 if ((flags & PROBE_TEST_DATA) && entry->data != data)
2086 continue;
2088 /* do this last, since it is the most expensive */
2089 if (glob) {
2090 kallsyms_lookup(entry->ip, NULL, NULL,
2091 NULL, str);
2092 if (!ftrace_match(str, glob, len, type))
2093 continue;
2096 hlist_del(&entry->node);
2097 call_rcu(&entry->rcu, ftrace_free_entry_rcu);
2100 __disable_ftrace_function_probe();
2101 mutex_unlock(&ftrace_lock);
2104 void
2105 unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
2106 void *data)
2108 __unregister_ftrace_function_probe(glob, ops, data,
2109 PROBE_TEST_FUNC | PROBE_TEST_DATA);
2112 void
2113 unregister_ftrace_function_probe_func(char *glob, struct ftrace_probe_ops *ops)
2115 __unregister_ftrace_function_probe(glob, ops, NULL, PROBE_TEST_FUNC);
2118 void unregister_ftrace_function_probe_all(char *glob)
2120 __unregister_ftrace_function_probe(glob, NULL, NULL, 0);
2123 static LIST_HEAD(ftrace_commands);
2124 static DEFINE_MUTEX(ftrace_cmd_mutex);
2126 int register_ftrace_command(struct ftrace_func_command *cmd)
2128 struct ftrace_func_command *p;
2129 int ret = 0;
2131 mutex_lock(&ftrace_cmd_mutex);
2132 list_for_each_entry(p, &ftrace_commands, list) {
2133 if (strcmp(cmd->name, p->name) == 0) {
2134 ret = -EBUSY;
2135 goto out_unlock;
2138 list_add(&cmd->list, &ftrace_commands);
2139 out_unlock:
2140 mutex_unlock(&ftrace_cmd_mutex);
2142 return ret;
2145 int unregister_ftrace_command(struct ftrace_func_command *cmd)
2147 struct ftrace_func_command *p, *n;
2148 int ret = -ENODEV;
2150 mutex_lock(&ftrace_cmd_mutex);
2151 list_for_each_entry_safe(p, n, &ftrace_commands, list) {
2152 if (strcmp(cmd->name, p->name) == 0) {
2153 ret = 0;
2154 list_del_init(&p->list);
2155 goto out_unlock;
2158 out_unlock:
2159 mutex_unlock(&ftrace_cmd_mutex);
2161 return ret;
2164 static int ftrace_process_regex(char *buff, int len, int enable)
2166 char *func, *command, *next = buff;
2167 struct ftrace_func_command *p;
2168 int ret = -EINVAL;
2170 func = strsep(&next, ":");
2172 if (!next) {
2173 ftrace_match_records(func, len, enable);
2174 return 0;
2177 /* command found */
2179 command = strsep(&next, ":");
2181 mutex_lock(&ftrace_cmd_mutex);
2182 list_for_each_entry(p, &ftrace_commands, list) {
2183 if (strcmp(p->name, command) == 0) {
2184 ret = p->func(func, command, next, enable);
2185 goto out_unlock;
2188 out_unlock:
2189 mutex_unlock(&ftrace_cmd_mutex);
2191 return ret;
2194 static ssize_t
2195 ftrace_regex_write(struct file *file, const char __user *ubuf,
2196 size_t cnt, loff_t *ppos, int enable)
2198 struct ftrace_iterator *iter;
2199 char ch;
2200 size_t read = 0;
2201 ssize_t ret;
2203 if (!cnt || cnt < 0)
2204 return 0;
2206 mutex_lock(&ftrace_regex_lock);
2208 if (file->f_mode & FMODE_READ) {
2209 struct seq_file *m = file->private_data;
2210 iter = m->private;
2211 } else
2212 iter = file->private_data;
2214 if (!*ppos) {
2215 iter->flags &= ~FTRACE_ITER_CONT;
2216 iter->buffer_idx = 0;
2219 ret = get_user(ch, ubuf++);
2220 if (ret)
2221 goto out;
2222 read++;
2223 cnt--;
2226 * If the parser haven't finished with the last write,
2227 * continue reading the user input without skipping spaces.
2229 if (!(iter->flags & FTRACE_ITER_CONT)) {
2230 /* skip white space */
2231 while (cnt && isspace(ch)) {
2232 ret = get_user(ch, ubuf++);
2233 if (ret)
2234 goto out;
2235 read++;
2236 cnt--;
2239 /* only spaces were written */
2240 if (isspace(ch)) {
2241 *ppos += read;
2242 ret = read;
2243 goto out;
2246 iter->buffer_idx = 0;
2249 while (cnt && !isspace(ch)) {
2250 if (iter->buffer_idx < FTRACE_BUFF_MAX)
2251 iter->buffer[iter->buffer_idx++] = ch;
2252 else {
2253 ret = -EINVAL;
2254 goto out;
2256 ret = get_user(ch, ubuf++);
2257 if (ret)
2258 goto out;
2259 read++;
2260 cnt--;
2263 if (isspace(ch)) {
2264 iter->buffer[iter->buffer_idx] = 0;
2265 ret = ftrace_process_regex(iter->buffer,
2266 iter->buffer_idx, enable);
2267 if (ret)
2268 goto out;
2269 iter->buffer_idx = 0;
2270 } else {
2271 iter->flags |= FTRACE_ITER_CONT;
2272 iter->buffer[iter->buffer_idx++] = ch;
2275 *ppos += read;
2276 ret = read;
2277 out:
2278 mutex_unlock(&ftrace_regex_lock);
2280 return ret;
2283 static ssize_t
2284 ftrace_filter_write(struct file *file, const char __user *ubuf,
2285 size_t cnt, loff_t *ppos)
2287 return ftrace_regex_write(file, ubuf, cnt, ppos, 1);
2290 static ssize_t
2291 ftrace_notrace_write(struct file *file, const char __user *ubuf,
2292 size_t cnt, loff_t *ppos)
2294 return ftrace_regex_write(file, ubuf, cnt, ppos, 0);
2297 static void
2298 ftrace_set_regex(unsigned char *buf, int len, int reset, int enable)
2300 if (unlikely(ftrace_disabled))
2301 return;
2303 mutex_lock(&ftrace_regex_lock);
2304 if (reset)
2305 ftrace_filter_reset(enable);
2306 if (buf)
2307 ftrace_match_records(buf, len, enable);
2308 mutex_unlock(&ftrace_regex_lock);
2312 * ftrace_set_filter - set a function to filter on in ftrace
2313 * @buf - the string that holds the function filter text.
2314 * @len - the length of the string.
2315 * @reset - non zero to reset all filters before applying this filter.
2317 * Filters denote which functions should be enabled when tracing is enabled.
2318 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
2320 void ftrace_set_filter(unsigned char *buf, int len, int reset)
2322 ftrace_set_regex(buf, len, reset, 1);
2326 * ftrace_set_notrace - set a function to not trace in ftrace
2327 * @buf - the string that holds the function notrace text.
2328 * @len - the length of the string.
2329 * @reset - non zero to reset all filters before applying this filter.
2331 * Notrace Filters denote which functions should not be enabled when tracing
2332 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
2333 * for tracing.
2335 void ftrace_set_notrace(unsigned char *buf, int len, int reset)
2337 ftrace_set_regex(buf, len, reset, 0);
2341 * command line interface to allow users to set filters on boot up.
2343 #define FTRACE_FILTER_SIZE COMMAND_LINE_SIZE
2344 static char ftrace_notrace_buf[FTRACE_FILTER_SIZE] __initdata;
2345 static char ftrace_filter_buf[FTRACE_FILTER_SIZE] __initdata;
2347 static int __init set_ftrace_notrace(char *str)
2349 strncpy(ftrace_notrace_buf, str, FTRACE_FILTER_SIZE);
2350 return 1;
2352 __setup("ftrace_notrace=", set_ftrace_notrace);
2354 static int __init set_ftrace_filter(char *str)
2356 strncpy(ftrace_filter_buf, str, FTRACE_FILTER_SIZE);
2357 return 1;
2359 __setup("ftrace_filter=", set_ftrace_filter);
2361 static void __init set_ftrace_early_filter(char *buf, int enable)
2363 char *func;
2365 while (buf) {
2366 func = strsep(&buf, ",");
2367 ftrace_set_regex(func, strlen(func), 0, enable);
2371 static void __init set_ftrace_early_filters(void)
2373 if (ftrace_filter_buf[0])
2374 set_ftrace_early_filter(ftrace_filter_buf, 1);
2375 if (ftrace_notrace_buf[0])
2376 set_ftrace_early_filter(ftrace_notrace_buf, 0);
2379 static int
2380 ftrace_regex_release(struct inode *inode, struct file *file, int enable)
2382 struct seq_file *m = (struct seq_file *)file->private_data;
2383 struct ftrace_iterator *iter;
2385 mutex_lock(&ftrace_regex_lock);
2386 if (file->f_mode & FMODE_READ) {
2387 iter = m->private;
2389 seq_release(inode, file);
2390 } else
2391 iter = file->private_data;
2393 if (iter->buffer_idx) {
2394 iter->buffer[iter->buffer_idx] = 0;
2395 ftrace_match_records(iter->buffer, iter->buffer_idx, enable);
2398 mutex_lock(&ftrace_lock);
2399 if (ftrace_start_up && ftrace_enabled)
2400 ftrace_run_update_code(FTRACE_ENABLE_CALLS);
2401 mutex_unlock(&ftrace_lock);
2403 kfree(iter);
2404 mutex_unlock(&ftrace_regex_lock);
2405 return 0;
2408 static int
2409 ftrace_filter_release(struct inode *inode, struct file *file)
2411 return ftrace_regex_release(inode, file, 1);
2414 static int
2415 ftrace_notrace_release(struct inode *inode, struct file *file)
2417 return ftrace_regex_release(inode, file, 0);
2420 static const struct file_operations ftrace_avail_fops = {
2421 .open = ftrace_avail_open,
2422 .read = seq_read,
2423 .llseek = seq_lseek,
2424 .release = seq_release_private,
2427 static const struct file_operations ftrace_failures_fops = {
2428 .open = ftrace_failures_open,
2429 .read = seq_read,
2430 .llseek = seq_lseek,
2431 .release = seq_release_private,
2434 static const struct file_operations ftrace_filter_fops = {
2435 .open = ftrace_filter_open,
2436 .read = seq_read,
2437 .write = ftrace_filter_write,
2438 .llseek = ftrace_regex_lseek,
2439 .release = ftrace_filter_release,
2442 static const struct file_operations ftrace_notrace_fops = {
2443 .open = ftrace_notrace_open,
2444 .read = seq_read,
2445 .write = ftrace_notrace_write,
2446 .llseek = ftrace_regex_lseek,
2447 .release = ftrace_notrace_release,
2450 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
2452 static DEFINE_MUTEX(graph_lock);
2454 int ftrace_graph_count;
2455 unsigned long ftrace_graph_funcs[FTRACE_GRAPH_MAX_FUNCS] __read_mostly;
2457 static void *
2458 __g_next(struct seq_file *m, loff_t *pos)
2460 unsigned long *array = m->private;
2462 if (*pos >= ftrace_graph_count)
2463 return NULL;
2464 return &array[*pos];
2467 static void *
2468 g_next(struct seq_file *m, void *v, loff_t *pos)
2470 (*pos)++;
2471 return __g_next(m, pos);
2474 static void *g_start(struct seq_file *m, loff_t *pos)
2476 mutex_lock(&graph_lock);
2478 /* Nothing, tell g_show to print all functions are enabled */
2479 if (!ftrace_graph_count && !*pos)
2480 return (void *)1;
2482 return __g_next(m, pos);
2485 static void g_stop(struct seq_file *m, void *p)
2487 mutex_unlock(&graph_lock);
2490 static int g_show(struct seq_file *m, void *v)
2492 unsigned long *ptr = v;
2494 if (!ptr)
2495 return 0;
2497 if (ptr == (unsigned long *)1) {
2498 seq_printf(m, "#### all functions enabled ####\n");
2499 return 0;
2502 seq_printf(m, "%pf\n", v);
2504 return 0;
2507 static struct seq_operations ftrace_graph_seq_ops = {
2508 .start = g_start,
2509 .next = g_next,
2510 .stop = g_stop,
2511 .show = g_show,
2514 static int
2515 ftrace_graph_open(struct inode *inode, struct file *file)
2517 int ret = 0;
2519 if (unlikely(ftrace_disabled))
2520 return -ENODEV;
2522 mutex_lock(&graph_lock);
2523 if ((file->f_mode & FMODE_WRITE) &&
2524 (file->f_flags & O_TRUNC)) {
2525 ftrace_graph_count = 0;
2526 memset(ftrace_graph_funcs, 0, sizeof(ftrace_graph_funcs));
2529 if (file->f_mode & FMODE_READ) {
2530 ret = seq_open(file, &ftrace_graph_seq_ops);
2531 if (!ret) {
2532 struct seq_file *m = file->private_data;
2533 m->private = ftrace_graph_funcs;
2535 } else
2536 file->private_data = ftrace_graph_funcs;
2537 mutex_unlock(&graph_lock);
2539 return ret;
2542 static int
2543 ftrace_graph_release(struct inode *inode, struct file *file)
2545 if (file->f_mode & FMODE_READ)
2546 seq_release(inode, file);
2547 return 0;
2550 static int
2551 ftrace_set_func(unsigned long *array, int *idx, char *buffer)
2553 struct dyn_ftrace *rec;
2554 struct ftrace_page *pg;
2555 int search_len;
2556 int found = 0;
2557 int type, not;
2558 char *search;
2559 bool exists;
2560 int i;
2562 if (ftrace_disabled)
2563 return -ENODEV;
2565 /* decode regex */
2566 type = ftrace_setup_glob(buffer, strlen(buffer), &search, &not);
2567 if (not)
2568 return -EINVAL;
2570 search_len = strlen(search);
2572 mutex_lock(&ftrace_lock);
2573 do_for_each_ftrace_rec(pg, rec) {
2575 if (*idx >= FTRACE_GRAPH_MAX_FUNCS)
2576 break;
2578 if (rec->flags & (FTRACE_FL_FAILED | FTRACE_FL_FREE))
2579 continue;
2581 if (ftrace_match_record(rec, search, search_len, type)) {
2582 /* ensure it is not already in the array */
2583 exists = false;
2584 for (i = 0; i < *idx; i++)
2585 if (array[i] == rec->ip) {
2586 exists = true;
2587 break;
2589 if (!exists) {
2590 array[(*idx)++] = rec->ip;
2591 found = 1;
2594 } while_for_each_ftrace_rec();
2596 mutex_unlock(&ftrace_lock);
2598 return found ? 0 : -EINVAL;
2601 static ssize_t
2602 ftrace_graph_write(struct file *file, const char __user *ubuf,
2603 size_t cnt, loff_t *ppos)
2605 unsigned char buffer[FTRACE_BUFF_MAX+1];
2606 unsigned long *array;
2607 size_t read = 0;
2608 ssize_t ret;
2609 int index = 0;
2610 char ch;
2612 if (!cnt || cnt < 0)
2613 return 0;
2615 mutex_lock(&graph_lock);
2617 if (ftrace_graph_count >= FTRACE_GRAPH_MAX_FUNCS) {
2618 ret = -EBUSY;
2619 goto out;
2622 if (file->f_mode & FMODE_READ) {
2623 struct seq_file *m = file->private_data;
2624 array = m->private;
2625 } else
2626 array = file->private_data;
2628 ret = get_user(ch, ubuf++);
2629 if (ret)
2630 goto out;
2631 read++;
2632 cnt--;
2634 /* skip white space */
2635 while (cnt && isspace(ch)) {
2636 ret = get_user(ch, ubuf++);
2637 if (ret)
2638 goto out;
2639 read++;
2640 cnt--;
2643 if (isspace(ch)) {
2644 *ppos += read;
2645 ret = read;
2646 goto out;
2649 while (cnt && !isspace(ch)) {
2650 if (index < FTRACE_BUFF_MAX)
2651 buffer[index++] = ch;
2652 else {
2653 ret = -EINVAL;
2654 goto out;
2656 ret = get_user(ch, ubuf++);
2657 if (ret)
2658 goto out;
2659 read++;
2660 cnt--;
2662 buffer[index] = 0;
2664 /* we allow only one expression at a time */
2665 ret = ftrace_set_func(array, &ftrace_graph_count, buffer);
2666 if (ret)
2667 goto out;
2669 file->f_pos += read;
2671 ret = read;
2672 out:
2673 mutex_unlock(&graph_lock);
2675 return ret;
2678 static const struct file_operations ftrace_graph_fops = {
2679 .open = ftrace_graph_open,
2680 .read = seq_read,
2681 .write = ftrace_graph_write,
2682 .release = ftrace_graph_release,
2684 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
2686 static __init int ftrace_init_dyn_debugfs(struct dentry *d_tracer)
2689 trace_create_file("available_filter_functions", 0444,
2690 d_tracer, NULL, &ftrace_avail_fops);
2692 trace_create_file("failures", 0444,
2693 d_tracer, NULL, &ftrace_failures_fops);
2695 trace_create_file("set_ftrace_filter", 0644, d_tracer,
2696 NULL, &ftrace_filter_fops);
2698 trace_create_file("set_ftrace_notrace", 0644, d_tracer,
2699 NULL, &ftrace_notrace_fops);
2701 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
2702 trace_create_file("set_graph_function", 0444, d_tracer,
2703 NULL,
2704 &ftrace_graph_fops);
2705 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
2707 return 0;
2710 static int ftrace_convert_nops(struct module *mod,
2711 unsigned long *start,
2712 unsigned long *end)
2714 unsigned long *p;
2715 unsigned long addr;
2716 unsigned long flags;
2718 mutex_lock(&ftrace_lock);
2719 p = start;
2720 while (p < end) {
2721 addr = ftrace_call_adjust(*p++);
2723 * Some architecture linkers will pad between
2724 * the different mcount_loc sections of different
2725 * object files to satisfy alignments.
2726 * Skip any NULL pointers.
2728 if (!addr)
2729 continue;
2730 ftrace_record_ip(addr);
2733 /* disable interrupts to prevent kstop machine */
2734 local_irq_save(flags);
2735 ftrace_update_code(mod);
2736 local_irq_restore(flags);
2737 mutex_unlock(&ftrace_lock);
2739 return 0;
2742 #ifdef CONFIG_MODULES
2743 void ftrace_release(void *start, void *end)
2745 struct dyn_ftrace *rec;
2746 struct ftrace_page *pg;
2747 unsigned long s = (unsigned long)start;
2748 unsigned long e = (unsigned long)end;
2750 if (ftrace_disabled || !start || start == end)
2751 return;
2753 mutex_lock(&ftrace_lock);
2754 do_for_each_ftrace_rec(pg, rec) {
2755 if ((rec->ip >= s) && (rec->ip < e)) {
2757 * rec->ip is changed in ftrace_free_rec()
2758 * It should not between s and e if record was freed.
2760 FTRACE_WARN_ON(rec->flags & FTRACE_FL_FREE);
2761 ftrace_free_rec(rec);
2763 } while_for_each_ftrace_rec();
2764 mutex_unlock(&ftrace_lock);
2767 static void ftrace_init_module(struct module *mod,
2768 unsigned long *start, unsigned long *end)
2770 if (ftrace_disabled || start == end)
2771 return;
2772 ftrace_convert_nops(mod, start, end);
2775 static int ftrace_module_notify(struct notifier_block *self,
2776 unsigned long val, void *data)
2778 struct module *mod = data;
2780 switch (val) {
2781 case MODULE_STATE_COMING:
2782 ftrace_init_module(mod, mod->ftrace_callsites,
2783 mod->ftrace_callsites +
2784 mod->num_ftrace_callsites);
2785 break;
2786 case MODULE_STATE_GOING:
2787 ftrace_release(mod->ftrace_callsites,
2788 mod->ftrace_callsites +
2789 mod->num_ftrace_callsites);
2790 break;
2793 return 0;
2795 #else
2796 static int ftrace_module_notify(struct notifier_block *self,
2797 unsigned long val, void *data)
2799 return 0;
2801 #endif /* CONFIG_MODULES */
2803 struct notifier_block ftrace_module_nb = {
2804 .notifier_call = ftrace_module_notify,
2805 .priority = 0,
2808 extern unsigned long __start_mcount_loc[];
2809 extern unsigned long __stop_mcount_loc[];
2811 void __init ftrace_init(void)
2813 unsigned long count, addr, flags;
2814 int ret;
2816 /* Keep the ftrace pointer to the stub */
2817 addr = (unsigned long)ftrace_stub;
2819 local_irq_save(flags);
2820 ftrace_dyn_arch_init(&addr);
2821 local_irq_restore(flags);
2823 /* ftrace_dyn_arch_init places the return code in addr */
2824 if (addr)
2825 goto failed;
2827 count = __stop_mcount_loc - __start_mcount_loc;
2829 ret = ftrace_dyn_table_alloc(count);
2830 if (ret)
2831 goto failed;
2833 last_ftrace_enabled = ftrace_enabled = 1;
2835 ret = ftrace_convert_nops(NULL,
2836 __start_mcount_loc,
2837 __stop_mcount_loc);
2839 ret = register_module_notifier(&ftrace_module_nb);
2840 if (ret)
2841 pr_warning("Failed to register trace ftrace module notifier\n");
2843 set_ftrace_early_filters();
2845 return;
2846 failed:
2847 ftrace_disabled = 1;
2850 #else
2852 static int __init ftrace_nodyn_init(void)
2854 ftrace_enabled = 1;
2855 return 0;
2857 device_initcall(ftrace_nodyn_init);
2859 static inline int ftrace_init_dyn_debugfs(struct dentry *d_tracer) { return 0; }
2860 static inline void ftrace_startup_enable(int command) { }
2861 /* Keep as macros so we do not need to define the commands */
2862 # define ftrace_startup(command) do { } while (0)
2863 # define ftrace_shutdown(command) do { } while (0)
2864 # define ftrace_startup_sysctl() do { } while (0)
2865 # define ftrace_shutdown_sysctl() do { } while (0)
2866 #endif /* CONFIG_DYNAMIC_FTRACE */
2868 static ssize_t
2869 ftrace_pid_read(struct file *file, char __user *ubuf,
2870 size_t cnt, loff_t *ppos)
2872 char buf[64];
2873 int r;
2875 if (ftrace_pid_trace == ftrace_swapper_pid)
2876 r = sprintf(buf, "swapper tasks\n");
2877 else if (ftrace_pid_trace)
2878 r = sprintf(buf, "%u\n", pid_vnr(ftrace_pid_trace));
2879 else
2880 r = sprintf(buf, "no pid\n");
2882 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2885 static void clear_ftrace_swapper(void)
2887 struct task_struct *p;
2888 int cpu;
2890 get_online_cpus();
2891 for_each_online_cpu(cpu) {
2892 p = idle_task(cpu);
2893 clear_tsk_trace_trace(p);
2895 put_online_cpus();
2898 static void set_ftrace_swapper(void)
2900 struct task_struct *p;
2901 int cpu;
2903 get_online_cpus();
2904 for_each_online_cpu(cpu) {
2905 p = idle_task(cpu);
2906 set_tsk_trace_trace(p);
2908 put_online_cpus();
2911 static void clear_ftrace_pid(struct pid *pid)
2913 struct task_struct *p;
2915 rcu_read_lock();
2916 do_each_pid_task(pid, PIDTYPE_PID, p) {
2917 clear_tsk_trace_trace(p);
2918 } while_each_pid_task(pid, PIDTYPE_PID, p);
2919 rcu_read_unlock();
2921 put_pid(pid);
2924 static void set_ftrace_pid(struct pid *pid)
2926 struct task_struct *p;
2928 rcu_read_lock();
2929 do_each_pid_task(pid, PIDTYPE_PID, p) {
2930 set_tsk_trace_trace(p);
2931 } while_each_pid_task(pid, PIDTYPE_PID, p);
2932 rcu_read_unlock();
2935 static void clear_ftrace_pid_task(struct pid **pid)
2937 if (*pid == ftrace_swapper_pid)
2938 clear_ftrace_swapper();
2939 else
2940 clear_ftrace_pid(*pid);
2942 *pid = NULL;
2945 static void set_ftrace_pid_task(struct pid *pid)
2947 if (pid == ftrace_swapper_pid)
2948 set_ftrace_swapper();
2949 else
2950 set_ftrace_pid(pid);
2953 static ssize_t
2954 ftrace_pid_write(struct file *filp, const char __user *ubuf,
2955 size_t cnt, loff_t *ppos)
2957 struct pid *pid;
2958 char buf[64];
2959 long val;
2960 int ret;
2962 if (cnt >= sizeof(buf))
2963 return -EINVAL;
2965 if (copy_from_user(&buf, ubuf, cnt))
2966 return -EFAULT;
2968 buf[cnt] = 0;
2970 ret = strict_strtol(buf, 10, &val);
2971 if (ret < 0)
2972 return ret;
2974 mutex_lock(&ftrace_lock);
2975 if (val < 0) {
2976 /* disable pid tracing */
2977 if (!ftrace_pid_trace)
2978 goto out;
2980 clear_ftrace_pid_task(&ftrace_pid_trace);
2982 } else {
2983 /* swapper task is special */
2984 if (!val) {
2985 pid = ftrace_swapper_pid;
2986 if (pid == ftrace_pid_trace)
2987 goto out;
2988 } else {
2989 pid = find_get_pid(val);
2991 if (pid == ftrace_pid_trace) {
2992 put_pid(pid);
2993 goto out;
2997 if (ftrace_pid_trace)
2998 clear_ftrace_pid_task(&ftrace_pid_trace);
3000 if (!pid)
3001 goto out;
3003 ftrace_pid_trace = pid;
3005 set_ftrace_pid_task(ftrace_pid_trace);
3008 /* update the function call */
3009 ftrace_update_pid_func();
3010 ftrace_startup_enable(0);
3012 out:
3013 mutex_unlock(&ftrace_lock);
3015 return cnt;
3018 static const struct file_operations ftrace_pid_fops = {
3019 .read = ftrace_pid_read,
3020 .write = ftrace_pid_write,
3023 static __init int ftrace_init_debugfs(void)
3025 struct dentry *d_tracer;
3027 d_tracer = tracing_init_dentry();
3028 if (!d_tracer)
3029 return 0;
3031 ftrace_init_dyn_debugfs(d_tracer);
3033 trace_create_file("set_ftrace_pid", 0644, d_tracer,
3034 NULL, &ftrace_pid_fops);
3036 ftrace_profile_debugfs(d_tracer);
3038 return 0;
3040 fs_initcall(ftrace_init_debugfs);
3043 * ftrace_kill - kill ftrace
3045 * This function should be used by panic code. It stops ftrace
3046 * but in a not so nice way. If you need to simply kill ftrace
3047 * from a non-atomic section, use ftrace_kill.
3049 void ftrace_kill(void)
3051 ftrace_disabled = 1;
3052 ftrace_enabled = 0;
3053 clear_ftrace_function();
3057 * register_ftrace_function - register a function for profiling
3058 * @ops - ops structure that holds the function for profiling.
3060 * Register a function to be called by all functions in the
3061 * kernel.
3063 * Note: @ops->func and all the functions it calls must be labeled
3064 * with "notrace", otherwise it will go into a
3065 * recursive loop.
3067 int register_ftrace_function(struct ftrace_ops *ops)
3069 int ret;
3071 if (unlikely(ftrace_disabled))
3072 return -1;
3074 mutex_lock(&ftrace_lock);
3076 ret = __register_ftrace_function(ops);
3077 ftrace_startup(0);
3079 mutex_unlock(&ftrace_lock);
3080 return ret;
3084 * unregister_ftrace_function - unregister a function for profiling.
3085 * @ops - ops structure that holds the function to unregister
3087 * Unregister a function that was added to be called by ftrace profiling.
3089 int unregister_ftrace_function(struct ftrace_ops *ops)
3091 int ret;
3093 mutex_lock(&ftrace_lock);
3094 ret = __unregister_ftrace_function(ops);
3095 ftrace_shutdown(0);
3096 mutex_unlock(&ftrace_lock);
3098 return ret;
3102 ftrace_enable_sysctl(struct ctl_table *table, int write,
3103 struct file *file, void __user *buffer, size_t *lenp,
3104 loff_t *ppos)
3106 int ret;
3108 if (unlikely(ftrace_disabled))
3109 return -ENODEV;
3111 mutex_lock(&ftrace_lock);
3113 ret = proc_dointvec(table, write, file, buffer, lenp, ppos);
3115 if (ret || !write || (last_ftrace_enabled == !!ftrace_enabled))
3116 goto out;
3118 last_ftrace_enabled = !!ftrace_enabled;
3120 if (ftrace_enabled) {
3122 ftrace_startup_sysctl();
3124 /* we are starting ftrace again */
3125 if (ftrace_list != &ftrace_list_end) {
3126 if (ftrace_list->next == &ftrace_list_end)
3127 ftrace_trace_function = ftrace_list->func;
3128 else
3129 ftrace_trace_function = ftrace_list_func;
3132 } else {
3133 /* stopping ftrace calls (just send to ftrace_stub) */
3134 ftrace_trace_function = ftrace_stub;
3136 ftrace_shutdown_sysctl();
3139 out:
3140 mutex_unlock(&ftrace_lock);
3141 return ret;
3144 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
3146 static int ftrace_graph_active;
3147 static struct notifier_block ftrace_suspend_notifier;
3149 int ftrace_graph_entry_stub(struct ftrace_graph_ent *trace)
3151 return 0;
3154 /* The callbacks that hook a function */
3155 trace_func_graph_ret_t ftrace_graph_return =
3156 (trace_func_graph_ret_t)ftrace_stub;
3157 trace_func_graph_ent_t ftrace_graph_entry = ftrace_graph_entry_stub;
3159 /* Try to assign a return stack array on FTRACE_RETSTACK_ALLOC_SIZE tasks. */
3160 static int alloc_retstack_tasklist(struct ftrace_ret_stack **ret_stack_list)
3162 int i;
3163 int ret = 0;
3164 unsigned long flags;
3165 int start = 0, end = FTRACE_RETSTACK_ALLOC_SIZE;
3166 struct task_struct *g, *t;
3168 for (i = 0; i < FTRACE_RETSTACK_ALLOC_SIZE; i++) {
3169 ret_stack_list[i] = kmalloc(FTRACE_RETFUNC_DEPTH
3170 * sizeof(struct ftrace_ret_stack),
3171 GFP_KERNEL);
3172 if (!ret_stack_list[i]) {
3173 start = 0;
3174 end = i;
3175 ret = -ENOMEM;
3176 goto free;
3180 read_lock_irqsave(&tasklist_lock, flags);
3181 do_each_thread(g, t) {
3182 if (start == end) {
3183 ret = -EAGAIN;
3184 goto unlock;
3187 if (t->ret_stack == NULL) {
3188 atomic_set(&t->tracing_graph_pause, 0);
3189 atomic_set(&t->trace_overrun, 0);
3190 t->curr_ret_stack = -1;
3191 /* Make sure the tasks see the -1 first: */
3192 smp_wmb();
3193 t->ret_stack = ret_stack_list[start++];
3195 } while_each_thread(g, t);
3197 unlock:
3198 read_unlock_irqrestore(&tasklist_lock, flags);
3199 free:
3200 for (i = start; i < end; i++)
3201 kfree(ret_stack_list[i]);
3202 return ret;
3205 static void
3206 ftrace_graph_probe_sched_switch(struct rq *__rq, struct task_struct *prev,
3207 struct task_struct *next)
3209 unsigned long long timestamp;
3210 int index;
3213 * Does the user want to count the time a function was asleep.
3214 * If so, do not update the time stamps.
3216 if (trace_flags & TRACE_ITER_SLEEP_TIME)
3217 return;
3219 timestamp = trace_clock_local();
3221 prev->ftrace_timestamp = timestamp;
3223 /* only process tasks that we timestamped */
3224 if (!next->ftrace_timestamp)
3225 return;
3228 * Update all the counters in next to make up for the
3229 * time next was sleeping.
3231 timestamp -= next->ftrace_timestamp;
3233 for (index = next->curr_ret_stack; index >= 0; index--)
3234 next->ret_stack[index].calltime += timestamp;
3237 /* Allocate a return stack for each task */
3238 static int start_graph_tracing(void)
3240 struct ftrace_ret_stack **ret_stack_list;
3241 int ret, cpu;
3243 ret_stack_list = kmalloc(FTRACE_RETSTACK_ALLOC_SIZE *
3244 sizeof(struct ftrace_ret_stack *),
3245 GFP_KERNEL);
3247 if (!ret_stack_list)
3248 return -ENOMEM;
3250 /* The cpu_boot init_task->ret_stack will never be freed */
3251 for_each_online_cpu(cpu) {
3252 if (!idle_task(cpu)->ret_stack)
3253 ftrace_graph_init_task(idle_task(cpu));
3256 do {
3257 ret = alloc_retstack_tasklist(ret_stack_list);
3258 } while (ret == -EAGAIN);
3260 if (!ret) {
3261 ret = register_trace_sched_switch(ftrace_graph_probe_sched_switch);
3262 if (ret)
3263 pr_info("ftrace_graph: Couldn't activate tracepoint"
3264 " probe to kernel_sched_switch\n");
3267 kfree(ret_stack_list);
3268 return ret;
3272 * Hibernation protection.
3273 * The state of the current task is too much unstable during
3274 * suspend/restore to disk. We want to protect against that.
3276 static int
3277 ftrace_suspend_notifier_call(struct notifier_block *bl, unsigned long state,
3278 void *unused)
3280 switch (state) {
3281 case PM_HIBERNATION_PREPARE:
3282 pause_graph_tracing();
3283 break;
3285 case PM_POST_HIBERNATION:
3286 unpause_graph_tracing();
3287 break;
3289 return NOTIFY_DONE;
3292 int register_ftrace_graph(trace_func_graph_ret_t retfunc,
3293 trace_func_graph_ent_t entryfunc)
3295 int ret = 0;
3297 mutex_lock(&ftrace_lock);
3299 /* we currently allow only one tracer registered at a time */
3300 if (ftrace_graph_active) {
3301 ret = -EBUSY;
3302 goto out;
3305 ftrace_suspend_notifier.notifier_call = ftrace_suspend_notifier_call;
3306 register_pm_notifier(&ftrace_suspend_notifier);
3308 ftrace_graph_active++;
3309 ret = start_graph_tracing();
3310 if (ret) {
3311 ftrace_graph_active--;
3312 goto out;
3315 ftrace_graph_return = retfunc;
3316 ftrace_graph_entry = entryfunc;
3318 ftrace_startup(FTRACE_START_FUNC_RET);
3320 out:
3321 mutex_unlock(&ftrace_lock);
3322 return ret;
3325 void unregister_ftrace_graph(void)
3327 mutex_lock(&ftrace_lock);
3329 if (unlikely(!ftrace_graph_active))
3330 goto out;
3332 ftrace_graph_active--;
3333 unregister_trace_sched_switch(ftrace_graph_probe_sched_switch);
3334 ftrace_graph_return = (trace_func_graph_ret_t)ftrace_stub;
3335 ftrace_graph_entry = ftrace_graph_entry_stub;
3336 ftrace_shutdown(FTRACE_STOP_FUNC_RET);
3337 unregister_pm_notifier(&ftrace_suspend_notifier);
3339 out:
3340 mutex_unlock(&ftrace_lock);
3343 /* Allocate a return stack for newly created task */
3344 void ftrace_graph_init_task(struct task_struct *t)
3346 /* Make sure we do not use the parent ret_stack */
3347 t->ret_stack = NULL;
3349 if (ftrace_graph_active) {
3350 struct ftrace_ret_stack *ret_stack;
3352 ret_stack = kmalloc(FTRACE_RETFUNC_DEPTH
3353 * sizeof(struct ftrace_ret_stack),
3354 GFP_KERNEL);
3355 if (!ret_stack)
3356 return;
3357 t->curr_ret_stack = -1;
3358 atomic_set(&t->tracing_graph_pause, 0);
3359 atomic_set(&t->trace_overrun, 0);
3360 t->ftrace_timestamp = 0;
3361 /* make curr_ret_stack visable before we add the ret_stack */
3362 smp_wmb();
3363 t->ret_stack = ret_stack;
3367 void ftrace_graph_exit_task(struct task_struct *t)
3369 struct ftrace_ret_stack *ret_stack = t->ret_stack;
3371 t->ret_stack = NULL;
3372 /* NULL must become visible to IRQs before we free it: */
3373 barrier();
3375 kfree(ret_stack);
3378 void ftrace_graph_stop(void)
3380 ftrace_stop();
3382 #endif