tracing/urgent: warn in case of ftrace_start_up inbalance
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / kernel / trace / ftrace.c
1 /*
2 * Infrastructure for profiling code inserted by 'gcc -pg'.
3 *
4 * Copyright (C) 2007-2008 Steven Rostedt <srostedt@redhat.com>
5 * Copyright (C) 2004-2008 Ingo Molnar <mingo@redhat.com>
6 *
7 * Originally ported from the -rt patch by:
8 * Copyright (C) 2007 Arnaldo Carvalho de Melo <acme@redhat.com>
9 *
10 * Based on code in the latency_tracer, that is:
11 *
12 * Copyright (C) 2004-2006 Ingo Molnar
13 * Copyright (C) 2004 William Lee Irwin III
14 */
15
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>
31
32 #include <trace/events/sched.h>
33
34 #include <asm/ftrace.h>
35 #include <asm/setup.h>
36
37 #include "trace_output.h"
38 #include "trace_stat.h"
39
40 #define FTRACE_WARN_ON(cond) \
41 do { \
42 if (WARN_ON(cond)) \
43 ftrace_kill(); \
44 } while (0)
45
46 #define FTRACE_WARN_ON_ONCE(cond) \
47 do { \
48 if (WARN_ON_ONCE(cond)) \
49 ftrace_kill(); \
50 } while (0)
51
52 /* hash bits for specific function selection */
53 #define FTRACE_HASH_BITS 7
54 #define FTRACE_FUNC_HASHSIZE (1 << FTRACE_HASH_BITS)
55
56 /* ftrace_enabled is a method to turn ftrace on or off */
57 int ftrace_enabled __read_mostly;
58 static int last_ftrace_enabled;
59
60 /* Quick disabling of function tracer. */
61 int function_trace_stop;
62
63 /*
64 * ftrace_disabled is set when an anomaly is discovered.
65 * ftrace_disabled is much stronger than ftrace_enabled.
66 */
67 static int ftrace_disabled __read_mostly;
68
69 static DEFINE_MUTEX(ftrace_lock);
70
71 static struct ftrace_ops ftrace_list_end __read_mostly =
72 {
73 .func = ftrace_stub,
74 };
75
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;
80
81 static void ftrace_list_func(unsigned long ip, unsigned long parent_ip)
82 {
83 struct ftrace_ops *op = ftrace_list;
84
85 /* in case someone actually ports this to alpha! */
86 read_barrier_depends();
87
88 while (op != &ftrace_list_end) {
89 /* silly alpha */
90 read_barrier_depends();
91 op->func(ip, parent_ip);
92 op = op->next;
93 };
94 }
95
96 static void ftrace_pid_func(unsigned long ip, unsigned long parent_ip)
97 {
98 if (!test_tsk_trace_trace(current))
99 return;
100
101 ftrace_pid_function(ip, parent_ip);
102 }
103
104 static void set_ftrace_pid_function(ftrace_func_t func)
105 {
106 /* do not set ftrace_pid_function to itself! */
107 if (func != ftrace_pid_func)
108 ftrace_pid_function = func;
109 }
110
111 /**
112 * clear_ftrace_function - reset the ftrace function
113 *
114 * This NULLs the ftrace function and in essence stops
115 * tracing. There may be lag
116 */
117 void clear_ftrace_function(void)
118 {
119 ftrace_trace_function = ftrace_stub;
120 __ftrace_trace_function = ftrace_stub;
121 ftrace_pid_function = ftrace_stub;
122 }
123
124 #ifndef CONFIG_HAVE_FUNCTION_TRACE_MCOUNT_TEST
125 /*
126 * For those archs that do not test ftrace_trace_stop in their
127 * mcount call site, we need to do it from C.
128 */
129 static void ftrace_test_stop_func(unsigned long ip, unsigned long parent_ip)
130 {
131 if (function_trace_stop)
132 return;
133
134 __ftrace_trace_function(ip, parent_ip);
135 }
136 #endif
137
138 static int __register_ftrace_function(struct ftrace_ops *ops)
139 {
140 ops->next = ftrace_list;
141 /*
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.
146 */
147 smp_wmb();
148 ftrace_list = ops;
149
150 if (ftrace_enabled) {
151 ftrace_func_t func;
152
153 if (ops->next == &ftrace_list_end)
154 func = ops->func;
155 else
156 func = ftrace_list_func;
157
158 if (ftrace_pid_trace) {
159 set_ftrace_pid_function(func);
160 func = ftrace_pid_func;
161 }
162
163 /*
164 * For one func, simply call it directly.
165 * For more than one func, call the chain.
166 */
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
173 }
174
175 return 0;
176 }
177
178 static int __unregister_ftrace_function(struct ftrace_ops *ops)
179 {
180 struct ftrace_ops **p;
181
182 /*
183 * If we are removing the last function, then simply point
184 * to the ftrace_stub.
185 */
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;
190 }
191
192 for (p = &ftrace_list; *p != &ftrace_list_end; p = &(*p)->next)
193 if (*p == ops)
194 break;
195
196 if (*p != ops)
197 return -1;
198
199 *p = (*p)->next;
200
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;
205
206 if (ftrace_pid_trace) {
207 set_ftrace_pid_function(func);
208 func = ftrace_pid_func;
209 }
210 #ifdef CONFIG_HAVE_FUNCTION_TRACE_MCOUNT_TEST
211 ftrace_trace_function = func;
212 #else
213 __ftrace_trace_function = func;
214 #endif
215 }
216 }
217
218 return 0;
219 }
220
221 static void ftrace_update_pid_func(void)
222 {
223 ftrace_func_t func;
224
225 if (ftrace_trace_function == ftrace_stub)
226 return;
227
228 func = ftrace_trace_function;
229
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;
236 }
237
238 #ifdef CONFIG_HAVE_FUNCTION_TRACE_MCOUNT_TEST
239 ftrace_trace_function = func;
240 #else
241 __ftrace_trace_function = func;
242 #endif
243 }
244
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
253 };
254
255 struct ftrace_profile_page {
256 struct ftrace_profile_page *next;
257 unsigned long index;
258 struct ftrace_profile records[];
259 };
260
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;
267 };
268
269 #define PROFILE_RECORDS_SIZE \
270 (PAGE_SIZE - offsetof(struct ftrace_profile_page, records))
271
272 #define PROFILES_PER_PAGE \
273 (PROFILE_RECORDS_SIZE / sizeof(struct ftrace_profile))
274
275 static int ftrace_profile_bits __read_mostly;
276 static int ftrace_profile_enabled __read_mostly;
277
278 /* ftrace_profile_lock - synchronize the enable and disable of the profiler */
279 static DEFINE_MUTEX(ftrace_profile_lock);
280
281 static DEFINE_PER_CPU(struct ftrace_profile_stat, ftrace_profile_stats);
282
283 #define FTRACE_PROFILE_HASH_SIZE 1024 /* must be power of 2 */
284
285 static void *
286 function_stat_next(void *v, int idx)
287 {
288 struct ftrace_profile *rec = v;
289 struct ftrace_profile_page *pg;
290
291 pg = (struct ftrace_profile_page *)((unsigned long)rec & PAGE_MASK);
292
293 again:
294 rec++;
295 if ((void *)rec >= (void *)&pg->records[pg->index]) {
296 pg = pg->next;
297 if (!pg)
298 return NULL;
299 rec = &pg->records[0];
300 if (!rec->counter)
301 goto again;
302 }
303
304 return rec;
305 }
306
307 static void *function_stat_start(struct tracer_stat *trace)
308 {
309 struct ftrace_profile_stat *stat =
310 container_of(trace, struct ftrace_profile_stat, stat);
311
312 if (!stat || !stat->start)
313 return NULL;
314
315 return function_stat_next(&stat->start->records[0], 0);
316 }
317
318 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
319 /* function graph compares on total time */
320 static int function_stat_cmp(void *p1, void *p2)
321 {
322 struct ftrace_profile *a = p1;
323 struct ftrace_profile *b = p2;
324
325 if (a->time < b->time)
326 return -1;
327 if (a->time > b->time)
328 return 1;
329 else
330 return 0;
331 }
332 #else
333 /* not function graph compares against hits */
334 static int function_stat_cmp(void *p1, void *p2)
335 {
336 struct ftrace_profile *a = p1;
337 struct ftrace_profile *b = p2;
338
339 if (a->counter < b->counter)
340 return -1;
341 if (a->counter > b->counter)
342 return 1;
343 else
344 return 0;
345 }
346 #endif
347
348 static int function_stat_headers(struct seq_file *m)
349 {
350 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
351 seq_printf(m, " Function "
352 "Hit Time Avg\n"
353 " -------- "
354 "--- ---- ---\n");
355 #else
356 seq_printf(m, " Function Hit\n"
357 " -------- ---\n");
358 #endif
359 return 0;
360 }
361
362 static int function_stat_show(struct seq_file *m, void *v)
363 {
364 struct ftrace_profile *rec = v;
365 char str[KSYM_SYMBOL_LEN];
366 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
367 static DEFINE_MUTEX(mutex);
368 static struct trace_seq s;
369 unsigned long long avg;
370 #endif
371
372 kallsyms_lookup(rec->ip, NULL, NULL, NULL, str);
373 seq_printf(m, " %-30.30s %10lu", str, rec->counter);
374
375 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
376 seq_printf(m, " ");
377 avg = rec->time;
378 do_div(avg, rec->counter);
379
380 mutex_lock(&mutex);
381 trace_seq_init(&s);
382 trace_print_graph_duration(rec->time, &s);
383 trace_seq_puts(&s, " ");
384 trace_print_graph_duration(avg, &s);
385 trace_print_seq(m, &s);
386 mutex_unlock(&mutex);
387 #endif
388 seq_putc(m, '\n');
389
390 return 0;
391 }
392
393 static void ftrace_profile_reset(struct ftrace_profile_stat *stat)
394 {
395 struct ftrace_profile_page *pg;
396
397 pg = stat->pages = stat->start;
398
399 while (pg) {
400 memset(pg->records, 0, PROFILE_RECORDS_SIZE);
401 pg->index = 0;
402 pg = pg->next;
403 }
404
405 memset(stat->hash, 0,
406 FTRACE_PROFILE_HASH_SIZE * sizeof(struct hlist_head));
407 }
408
409 int ftrace_profile_pages_init(struct ftrace_profile_stat *stat)
410 {
411 struct ftrace_profile_page *pg;
412 int functions;
413 int pages;
414 int i;
415
416 /* If we already allocated, do nothing */
417 if (stat->pages)
418 return 0;
419
420 stat->pages = (void *)get_zeroed_page(GFP_KERNEL);
421 if (!stat->pages)
422 return -ENOMEM;
423
424 #ifdef CONFIG_DYNAMIC_FTRACE
425 functions = ftrace_update_tot_cnt;
426 #else
427 /*
428 * We do not know the number of functions that exist because
429 * dynamic tracing is what counts them. With past experience
430 * we have around 20K functions. That should be more than enough.
431 * It is highly unlikely we will execute every function in
432 * the kernel.
433 */
434 functions = 20000;
435 #endif
436
437 pg = stat->start = stat->pages;
438
439 pages = DIV_ROUND_UP(functions, PROFILES_PER_PAGE);
440
441 for (i = 0; i < pages; i++) {
442 pg->next = (void *)get_zeroed_page(GFP_KERNEL);
443 if (!pg->next)
444 goto out_free;
445 pg = pg->next;
446 }
447
448 return 0;
449
450 out_free:
451 pg = stat->start;
452 while (pg) {
453 unsigned long tmp = (unsigned long)pg;
454
455 pg = pg->next;
456 free_page(tmp);
457 }
458
459 free_page((unsigned long)stat->pages);
460 stat->pages = NULL;
461 stat->start = NULL;
462
463 return -ENOMEM;
464 }
465
466 static int ftrace_profile_init_cpu(int cpu)
467 {
468 struct ftrace_profile_stat *stat;
469 int size;
470
471 stat = &per_cpu(ftrace_profile_stats, cpu);
472
473 if (stat->hash) {
474 /* If the profile is already created, simply reset it */
475 ftrace_profile_reset(stat);
476 return 0;
477 }
478
479 /*
480 * We are profiling all functions, but usually only a few thousand
481 * functions are hit. We'll make a hash of 1024 items.
482 */
483 size = FTRACE_PROFILE_HASH_SIZE;
484
485 stat->hash = kzalloc(sizeof(struct hlist_head) * size, GFP_KERNEL);
486
487 if (!stat->hash)
488 return -ENOMEM;
489
490 if (!ftrace_profile_bits) {
491 size--;
492
493 for (; size; size >>= 1)
494 ftrace_profile_bits++;
495 }
496
497 /* Preallocate the function profiling pages */
498 if (ftrace_profile_pages_init(stat) < 0) {
499 kfree(stat->hash);
500 stat->hash = NULL;
501 return -ENOMEM;
502 }
503
504 return 0;
505 }
506
507 static int ftrace_profile_init(void)
508 {
509 int cpu;
510 int ret = 0;
511
512 for_each_online_cpu(cpu) {
513 ret = ftrace_profile_init_cpu(cpu);
514 if (ret)
515 break;
516 }
517
518 return ret;
519 }
520
521 /* interrupts must be disabled */
522 static struct ftrace_profile *
523 ftrace_find_profiled_func(struct ftrace_profile_stat *stat, unsigned long ip)
524 {
525 struct ftrace_profile *rec;
526 struct hlist_head *hhd;
527 struct hlist_node *n;
528 unsigned long key;
529
530 key = hash_long(ip, ftrace_profile_bits);
531 hhd = &stat->hash[key];
532
533 if (hlist_empty(hhd))
534 return NULL;
535
536 hlist_for_each_entry_rcu(rec, n, hhd, node) {
537 if (rec->ip == ip)
538 return rec;
539 }
540
541 return NULL;
542 }
543
544 static void ftrace_add_profile(struct ftrace_profile_stat *stat,
545 struct ftrace_profile *rec)
546 {
547 unsigned long key;
548
549 key = hash_long(rec->ip, ftrace_profile_bits);
550 hlist_add_head_rcu(&rec->node, &stat->hash[key]);
551 }
552
553 /*
554 * The memory is already allocated, this simply finds a new record to use.
555 */
556 static struct ftrace_profile *
557 ftrace_profile_alloc(struct ftrace_profile_stat *stat, unsigned long ip)
558 {
559 struct ftrace_profile *rec = NULL;
560
561 /* prevent recursion (from NMIs) */
562 if (atomic_inc_return(&stat->disabled) != 1)
563 goto out;
564
565 /*
566 * Try to find the function again since an NMI
567 * could have added it
568 */
569 rec = ftrace_find_profiled_func(stat, ip);
570 if (rec)
571 goto out;
572
573 if (stat->pages->index == PROFILES_PER_PAGE) {
574 if (!stat->pages->next)
575 goto out;
576 stat->pages = stat->pages->next;
577 }
578
579 rec = &stat->pages->records[stat->pages->index++];
580 rec->ip = ip;
581 ftrace_add_profile(stat, rec);
582
583 out:
584 atomic_dec(&stat->disabled);
585
586 return rec;
587 }
588
589 static void
590 function_profile_call(unsigned long ip, unsigned long parent_ip)
591 {
592 struct ftrace_profile_stat *stat;
593 struct ftrace_profile *rec;
594 unsigned long flags;
595
596 if (!ftrace_profile_enabled)
597 return;
598
599 local_irq_save(flags);
600
601 stat = &__get_cpu_var(ftrace_profile_stats);
602 if (!stat->hash || !ftrace_profile_enabled)
603 goto out;
604
605 rec = ftrace_find_profiled_func(stat, ip);
606 if (!rec) {
607 rec = ftrace_profile_alloc(stat, ip);
608 if (!rec)
609 goto out;
610 }
611
612 rec->counter++;
613 out:
614 local_irq_restore(flags);
615 }
616
617 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
618 static int profile_graph_entry(struct ftrace_graph_ent *trace)
619 {
620 function_profile_call(trace->func, 0);
621 return 1;
622 }
623
624 static void profile_graph_return(struct ftrace_graph_ret *trace)
625 {
626 struct ftrace_profile_stat *stat;
627 unsigned long long calltime;
628 struct ftrace_profile *rec;
629 unsigned long flags;
630
631 local_irq_save(flags);
632 stat = &__get_cpu_var(ftrace_profile_stats);
633 if (!stat->hash || !ftrace_profile_enabled)
634 goto out;
635
636 calltime = trace->rettime - trace->calltime;
637
638 if (!(trace_flags & TRACE_ITER_GRAPH_TIME)) {
639 int index;
640
641 index = trace->depth;
642
643 /* Append this call time to the parent time to subtract */
644 if (index)
645 current->ret_stack[index - 1].subtime += calltime;
646
647 if (current->ret_stack[index].subtime < calltime)
648 calltime -= current->ret_stack[index].subtime;
649 else
650 calltime = 0;
651 }
652
653 rec = ftrace_find_profiled_func(stat, trace->func);
654 if (rec)
655 rec->time += calltime;
656
657 out:
658 local_irq_restore(flags);
659 }
660
661 static int register_ftrace_profiler(void)
662 {
663 return register_ftrace_graph(&profile_graph_return,
664 &profile_graph_entry);
665 }
666
667 static void unregister_ftrace_profiler(void)
668 {
669 unregister_ftrace_graph();
670 }
671 #else
672 static struct ftrace_ops ftrace_profile_ops __read_mostly =
673 {
674 .func = function_profile_call,
675 };
676
677 static int register_ftrace_profiler(void)
678 {
679 return register_ftrace_function(&ftrace_profile_ops);
680 }
681
682 static void unregister_ftrace_profiler(void)
683 {
684 unregister_ftrace_function(&ftrace_profile_ops);
685 }
686 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
687
688 static ssize_t
689 ftrace_profile_write(struct file *filp, const char __user *ubuf,
690 size_t cnt, loff_t *ppos)
691 {
692 unsigned long val;
693 char buf[64]; /* big enough to hold a number */
694 int ret;
695
696 if (cnt >= sizeof(buf))
697 return -EINVAL;
698
699 if (copy_from_user(&buf, ubuf, cnt))
700 return -EFAULT;
701
702 buf[cnt] = 0;
703
704 ret = strict_strtoul(buf, 10, &val);
705 if (ret < 0)
706 return ret;
707
708 val = !!val;
709
710 mutex_lock(&ftrace_profile_lock);
711 if (ftrace_profile_enabled ^ val) {
712 if (val) {
713 ret = ftrace_profile_init();
714 if (ret < 0) {
715 cnt = ret;
716 goto out;
717 }
718
719 ret = register_ftrace_profiler();
720 if (ret < 0) {
721 cnt = ret;
722 goto out;
723 }
724 ftrace_profile_enabled = 1;
725 } else {
726 ftrace_profile_enabled = 0;
727 /*
728 * unregister_ftrace_profiler calls stop_machine
729 * so this acts like an synchronize_sched.
730 */
731 unregister_ftrace_profiler();
732 }
733 }
734 out:
735 mutex_unlock(&ftrace_profile_lock);
736
737 filp->f_pos += cnt;
738
739 return cnt;
740 }
741
742 static ssize_t
743 ftrace_profile_read(struct file *filp, char __user *ubuf,
744 size_t cnt, loff_t *ppos)
745 {
746 char buf[64]; /* big enough to hold a number */
747 int r;
748
749 r = sprintf(buf, "%u\n", ftrace_profile_enabled);
750 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
751 }
752
753 static const struct file_operations ftrace_profile_fops = {
754 .open = tracing_open_generic,
755 .read = ftrace_profile_read,
756 .write = ftrace_profile_write,
757 };
758
759 /* used to initialize the real stat files */
760 static struct tracer_stat function_stats __initdata = {
761 .name = "functions",
762 .stat_start = function_stat_start,
763 .stat_next = function_stat_next,
764 .stat_cmp = function_stat_cmp,
765 .stat_headers = function_stat_headers,
766 .stat_show = function_stat_show
767 };
768
769 static void ftrace_profile_debugfs(struct dentry *d_tracer)
770 {
771 struct ftrace_profile_stat *stat;
772 struct dentry *entry;
773 char *name;
774 int ret;
775 int cpu;
776
777 for_each_possible_cpu(cpu) {
778 stat = &per_cpu(ftrace_profile_stats, cpu);
779
780 /* allocate enough for function name + cpu number */
781 name = kmalloc(32, GFP_KERNEL);
782 if (!name) {
783 /*
784 * The files created are permanent, if something happens
785 * we still do not free memory.
786 */
787 kfree(stat);
788 WARN(1,
789 "Could not allocate stat file for cpu %d\n",
790 cpu);
791 return;
792 }
793 stat->stat = function_stats;
794 snprintf(name, 32, "function%d", cpu);
795 stat->stat.name = name;
796 ret = register_stat_tracer(&stat->stat);
797 if (ret) {
798 WARN(1,
799 "Could not register function stat for cpu %d\n",
800 cpu);
801 kfree(name);
802 return;
803 }
804 }
805
806 entry = debugfs_create_file("function_profile_enabled", 0644,
807 d_tracer, NULL, &ftrace_profile_fops);
808 if (!entry)
809 pr_warning("Could not create debugfs "
810 "'function_profile_enabled' entry\n");
811 }
812
813 #else /* CONFIG_FUNCTION_PROFILER */
814 static void ftrace_profile_debugfs(struct dentry *d_tracer)
815 {
816 }
817 #endif /* CONFIG_FUNCTION_PROFILER */
818
819 /* set when tracing only a pid */
820 struct pid *ftrace_pid_trace;
821 static struct pid * const ftrace_swapper_pid = &init_struct_pid;
822
823 #ifdef CONFIG_DYNAMIC_FTRACE
824
825 #ifndef CONFIG_FTRACE_MCOUNT_RECORD
826 # error Dynamic ftrace depends on MCOUNT_RECORD
827 #endif
828
829 static struct hlist_head ftrace_func_hash[FTRACE_FUNC_HASHSIZE] __read_mostly;
830
831 struct ftrace_func_probe {
832 struct hlist_node node;
833 struct ftrace_probe_ops *ops;
834 unsigned long flags;
835 unsigned long ip;
836 void *data;
837 struct rcu_head rcu;
838 };
839
840 enum {
841 FTRACE_ENABLE_CALLS = (1 << 0),
842 FTRACE_DISABLE_CALLS = (1 << 1),
843 FTRACE_UPDATE_TRACE_FUNC = (1 << 2),
844 FTRACE_ENABLE_MCOUNT = (1 << 3),
845 FTRACE_DISABLE_MCOUNT = (1 << 4),
846 FTRACE_START_FUNC_RET = (1 << 5),
847 FTRACE_STOP_FUNC_RET = (1 << 6),
848 };
849
850 static int ftrace_filtered;
851
852 static struct dyn_ftrace *ftrace_new_addrs;
853
854 static DEFINE_MUTEX(ftrace_regex_lock);
855
856 struct ftrace_page {
857 struct ftrace_page *next;
858 int index;
859 struct dyn_ftrace records[];
860 };
861
862 #define ENTRIES_PER_PAGE \
863 ((PAGE_SIZE - sizeof(struct ftrace_page)) / sizeof(struct dyn_ftrace))
864
865 /* estimate from running different kernels */
866 #define NR_TO_INIT 10000
867
868 static struct ftrace_page *ftrace_pages_start;
869 static struct ftrace_page *ftrace_pages;
870
871 static struct dyn_ftrace *ftrace_free_records;
872
873 /*
874 * This is a double for. Do not use 'break' to break out of the loop,
875 * you must use a goto.
876 */
877 #define do_for_each_ftrace_rec(pg, rec) \
878 for (pg = ftrace_pages_start; pg; pg = pg->next) { \
879 int _____i; \
880 for (_____i = 0; _____i < pg->index; _____i++) { \
881 rec = &pg->records[_____i];
882
883 #define while_for_each_ftrace_rec() \
884 } \
885 }
886
887 #ifdef CONFIG_KPROBES
888
889 static int frozen_record_count;
890
891 static inline void freeze_record(struct dyn_ftrace *rec)
892 {
893 if (!(rec->flags & FTRACE_FL_FROZEN)) {
894 rec->flags |= FTRACE_FL_FROZEN;
895 frozen_record_count++;
896 }
897 }
898
899 static inline void unfreeze_record(struct dyn_ftrace *rec)
900 {
901 if (rec->flags & FTRACE_FL_FROZEN) {
902 rec->flags &= ~FTRACE_FL_FROZEN;
903 frozen_record_count--;
904 }
905 }
906
907 static inline int record_frozen(struct dyn_ftrace *rec)
908 {
909 return rec->flags & FTRACE_FL_FROZEN;
910 }
911 #else
912 # define freeze_record(rec) ({ 0; })
913 # define unfreeze_record(rec) ({ 0; })
914 # define record_frozen(rec) ({ 0; })
915 #endif /* CONFIG_KPROBES */
916
917 static void ftrace_free_rec(struct dyn_ftrace *rec)
918 {
919 rec->freelist = ftrace_free_records;
920 ftrace_free_records = rec;
921 rec->flags |= FTRACE_FL_FREE;
922 }
923
924 static struct dyn_ftrace *ftrace_alloc_dyn_node(unsigned long ip)
925 {
926 struct dyn_ftrace *rec;
927
928 /* First check for freed records */
929 if (ftrace_free_records) {
930 rec = ftrace_free_records;
931
932 if (unlikely(!(rec->flags & FTRACE_FL_FREE))) {
933 FTRACE_WARN_ON_ONCE(1);
934 ftrace_free_records = NULL;
935 return NULL;
936 }
937
938 ftrace_free_records = rec->freelist;
939 memset(rec, 0, sizeof(*rec));
940 return rec;
941 }
942
943 if (ftrace_pages->index == ENTRIES_PER_PAGE) {
944 if (!ftrace_pages->next) {
945 /* allocate another page */
946 ftrace_pages->next =
947 (void *)get_zeroed_page(GFP_KERNEL);
948 if (!ftrace_pages->next)
949 return NULL;
950 }
951 ftrace_pages = ftrace_pages->next;
952 }
953
954 return &ftrace_pages->records[ftrace_pages->index++];
955 }
956
957 static struct dyn_ftrace *
958 ftrace_record_ip(unsigned long ip)
959 {
960 struct dyn_ftrace *rec;
961
962 if (ftrace_disabled)
963 return NULL;
964
965 rec = ftrace_alloc_dyn_node(ip);
966 if (!rec)
967 return NULL;
968
969 rec->ip = ip;
970 rec->newlist = ftrace_new_addrs;
971 ftrace_new_addrs = rec;
972
973 return rec;
974 }
975
976 static void print_ip_ins(const char *fmt, unsigned char *p)
977 {
978 int i;
979
980 printk(KERN_CONT "%s", fmt);
981
982 for (i = 0; i < MCOUNT_INSN_SIZE; i++)
983 printk(KERN_CONT "%s%02x", i ? ":" : "", p[i]);
984 }
985
986 static void ftrace_bug(int failed, unsigned long ip)
987 {
988 switch (failed) {
989 case -EFAULT:
990 FTRACE_WARN_ON_ONCE(1);
991 pr_info("ftrace faulted on modifying ");
992 print_ip_sym(ip);
993 break;
994 case -EINVAL:
995 FTRACE_WARN_ON_ONCE(1);
996 pr_info("ftrace failed to modify ");
997 print_ip_sym(ip);
998 print_ip_ins(" actual: ", (unsigned char *)ip);
999 printk(KERN_CONT "\n");
1000 break;
1001 case -EPERM:
1002 FTRACE_WARN_ON_ONCE(1);
1003 pr_info("ftrace faulted on writing ");
1004 print_ip_sym(ip);
1005 break;
1006 default:
1007 FTRACE_WARN_ON_ONCE(1);
1008 pr_info("ftrace faulted on unknown error ");
1009 print_ip_sym(ip);
1010 }
1011 }
1012
1013
1014 static int
1015 __ftrace_replace_code(struct dyn_ftrace *rec, int enable)
1016 {
1017 unsigned long ftrace_addr;
1018 unsigned long ip, fl;
1019
1020 ftrace_addr = (unsigned long)FTRACE_ADDR;
1021
1022 ip = rec->ip;
1023
1024 /*
1025 * If this record is not to be traced and
1026 * it is not enabled then do nothing.
1027 *
1028 * If this record is not to be traced and
1029 * it is enabled then disable it.
1030 *
1031 */
1032 if (rec->flags & FTRACE_FL_NOTRACE) {
1033 if (rec->flags & FTRACE_FL_ENABLED)
1034 rec->flags &= ~FTRACE_FL_ENABLED;
1035 else
1036 return 0;
1037
1038 } else if (ftrace_filtered && enable) {
1039 /*
1040 * Filtering is on:
1041 */
1042
1043 fl = rec->flags & (FTRACE_FL_FILTER | FTRACE_FL_ENABLED);
1044
1045 /* Record is filtered and enabled, do nothing */
1046 if (fl == (FTRACE_FL_FILTER | FTRACE_FL_ENABLED))
1047 return 0;
1048
1049 /* Record is not filtered or enabled, do nothing */
1050 if (!fl)
1051 return 0;
1052
1053 /* Record is not filtered but enabled, disable it */
1054 if (fl == FTRACE_FL_ENABLED)
1055 rec->flags &= ~FTRACE_FL_ENABLED;
1056 else
1057 /* Otherwise record is filtered but not enabled, enable it */
1058 rec->flags |= FTRACE_FL_ENABLED;
1059 } else {
1060 /* Disable or not filtered */
1061
1062 if (enable) {
1063 /* if record is enabled, do nothing */
1064 if (rec->flags & FTRACE_FL_ENABLED)
1065 return 0;
1066
1067 rec->flags |= FTRACE_FL_ENABLED;
1068
1069 } else {
1070
1071 /* if record is not enabled, do nothing */
1072 if (!(rec->flags & FTRACE_FL_ENABLED))
1073 return 0;
1074
1075 rec->flags &= ~FTRACE_FL_ENABLED;
1076 }
1077 }
1078
1079 if (rec->flags & FTRACE_FL_ENABLED)
1080 return ftrace_make_call(rec, ftrace_addr);
1081 else
1082 return ftrace_make_nop(NULL, rec, ftrace_addr);
1083 }
1084
1085 static void ftrace_replace_code(int enable)
1086 {
1087 struct dyn_ftrace *rec;
1088 struct ftrace_page *pg;
1089 int failed;
1090
1091 do_for_each_ftrace_rec(pg, rec) {
1092 /*
1093 * Skip over free records, records that have
1094 * failed and not converted.
1095 */
1096 if (rec->flags & FTRACE_FL_FREE ||
1097 rec->flags & FTRACE_FL_FAILED ||
1098 !(rec->flags & FTRACE_FL_CONVERTED))
1099 continue;
1100
1101 /* ignore updates to this record's mcount site */
1102 if (get_kprobe((void *)rec->ip)) {
1103 freeze_record(rec);
1104 continue;
1105 } else {
1106 unfreeze_record(rec);
1107 }
1108
1109 failed = __ftrace_replace_code(rec, enable);
1110 if (failed) {
1111 rec->flags |= FTRACE_FL_FAILED;
1112 if ((system_state == SYSTEM_BOOTING) ||
1113 !core_kernel_text(rec->ip)) {
1114 ftrace_free_rec(rec);
1115 } else {
1116 ftrace_bug(failed, rec->ip);
1117 /* Stop processing */
1118 return;
1119 }
1120 }
1121 } while_for_each_ftrace_rec();
1122 }
1123
1124 static int
1125 ftrace_code_disable(struct module *mod, struct dyn_ftrace *rec)
1126 {
1127 unsigned long ip;
1128 int ret;
1129
1130 ip = rec->ip;
1131
1132 ret = ftrace_make_nop(mod, rec, MCOUNT_ADDR);
1133 if (ret) {
1134 ftrace_bug(ret, ip);
1135 rec->flags |= FTRACE_FL_FAILED;
1136 return 0;
1137 }
1138 return 1;
1139 }
1140
1141 /*
1142 * archs can override this function if they must do something
1143 * before the modifying code is performed.
1144 */
1145 int __weak ftrace_arch_code_modify_prepare(void)
1146 {
1147 return 0;
1148 }
1149
1150 /*
1151 * archs can override this function if they must do something
1152 * after the modifying code is performed.
1153 */
1154 int __weak ftrace_arch_code_modify_post_process(void)
1155 {
1156 return 0;
1157 }
1158
1159 static int __ftrace_modify_code(void *data)
1160 {
1161 int *command = data;
1162
1163 if (*command & FTRACE_ENABLE_CALLS)
1164 ftrace_replace_code(1);
1165 else if (*command & FTRACE_DISABLE_CALLS)
1166 ftrace_replace_code(0);
1167
1168 if (*command & FTRACE_UPDATE_TRACE_FUNC)
1169 ftrace_update_ftrace_func(ftrace_trace_function);
1170
1171 if (*command & FTRACE_START_FUNC_RET)
1172 ftrace_enable_ftrace_graph_caller();
1173 else if (*command & FTRACE_STOP_FUNC_RET)
1174 ftrace_disable_ftrace_graph_caller();
1175
1176 return 0;
1177 }
1178
1179 static void ftrace_run_update_code(int command)
1180 {
1181 int ret;
1182
1183 ret = ftrace_arch_code_modify_prepare();
1184 FTRACE_WARN_ON(ret);
1185 if (ret)
1186 return;
1187
1188 stop_machine(__ftrace_modify_code, &command, NULL);
1189
1190 ret = ftrace_arch_code_modify_post_process();
1191 FTRACE_WARN_ON(ret);
1192 }
1193
1194 static ftrace_func_t saved_ftrace_func;
1195 static int ftrace_start_up;
1196
1197 static void ftrace_startup_enable(int command)
1198 {
1199 if (saved_ftrace_func != ftrace_trace_function) {
1200 saved_ftrace_func = ftrace_trace_function;
1201 command |= FTRACE_UPDATE_TRACE_FUNC;
1202 }
1203
1204 if (!command || !ftrace_enabled)
1205 return;
1206
1207 ftrace_run_update_code(command);
1208 }
1209
1210 static void ftrace_startup(int command)
1211 {
1212 if (unlikely(ftrace_disabled))
1213 return;
1214
1215 ftrace_start_up++;
1216 command |= FTRACE_ENABLE_CALLS;
1217
1218 ftrace_startup_enable(command);
1219 }
1220
1221 static void ftrace_shutdown(int command)
1222 {
1223 if (unlikely(ftrace_disabled))
1224 return;
1225
1226 ftrace_start_up--;
1227 /*
1228 * Just warn in case of unbalance, no need to kill ftrace, it's not
1229 * critical but the ftrace_call callers may be never nopped again after
1230 * further ftrace uses.
1231 */
1232 WARN_ON_ONCE(ftrace_start_up < 0);
1233
1234 if (!ftrace_start_up)
1235 command |= FTRACE_DISABLE_CALLS;
1236
1237 if (saved_ftrace_func != ftrace_trace_function) {
1238 saved_ftrace_func = ftrace_trace_function;
1239 command |= FTRACE_UPDATE_TRACE_FUNC;
1240 }
1241
1242 if (!command || !ftrace_enabled)
1243 return;
1244
1245 ftrace_run_update_code(command);
1246 }
1247
1248 static void ftrace_startup_sysctl(void)
1249 {
1250 int command = FTRACE_ENABLE_MCOUNT;
1251
1252 if (unlikely(ftrace_disabled))
1253 return;
1254
1255 /* Force update next time */
1256 saved_ftrace_func = NULL;
1257 /* ftrace_start_up is true if we want ftrace running */
1258 if (ftrace_start_up)
1259 command |= FTRACE_ENABLE_CALLS;
1260
1261 ftrace_run_update_code(command);
1262 }
1263
1264 static void ftrace_shutdown_sysctl(void)
1265 {
1266 int command = FTRACE_DISABLE_MCOUNT;
1267
1268 if (unlikely(ftrace_disabled))
1269 return;
1270
1271 /* ftrace_start_up is true if ftrace is running */
1272 if (ftrace_start_up)
1273 command |= FTRACE_DISABLE_CALLS;
1274
1275 ftrace_run_update_code(command);
1276 }
1277
1278 static cycle_t ftrace_update_time;
1279 static unsigned long ftrace_update_cnt;
1280 unsigned long ftrace_update_tot_cnt;
1281
1282 static int ftrace_update_code(struct module *mod)
1283 {
1284 struct dyn_ftrace *p;
1285 cycle_t start, stop;
1286
1287 start = ftrace_now(raw_smp_processor_id());
1288 ftrace_update_cnt = 0;
1289
1290 while (ftrace_new_addrs) {
1291
1292 /* If something went wrong, bail without enabling anything */
1293 if (unlikely(ftrace_disabled))
1294 return -1;
1295
1296 p = ftrace_new_addrs;
1297 ftrace_new_addrs = p->newlist;
1298 p->flags = 0L;
1299
1300 /* convert record (i.e, patch mcount-call with NOP) */
1301 if (ftrace_code_disable(mod, p)) {
1302 p->flags |= FTRACE_FL_CONVERTED;
1303 ftrace_update_cnt++;
1304 } else
1305 ftrace_free_rec(p);
1306 }
1307
1308 stop = ftrace_now(raw_smp_processor_id());
1309 ftrace_update_time = stop - start;
1310 ftrace_update_tot_cnt += ftrace_update_cnt;
1311
1312 return 0;
1313 }
1314
1315 static int __init ftrace_dyn_table_alloc(unsigned long num_to_init)
1316 {
1317 struct ftrace_page *pg;
1318 int cnt;
1319 int i;
1320
1321 /* allocate a few pages */
1322 ftrace_pages_start = (void *)get_zeroed_page(GFP_KERNEL);
1323 if (!ftrace_pages_start)
1324 return -1;
1325
1326 /*
1327 * Allocate a few more pages.
1328 *
1329 * TODO: have some parser search vmlinux before
1330 * final linking to find all calls to ftrace.
1331 * Then we can:
1332 * a) know how many pages to allocate.
1333 * and/or
1334 * b) set up the table then.
1335 *
1336 * The dynamic code is still necessary for
1337 * modules.
1338 */
1339
1340 pg = ftrace_pages = ftrace_pages_start;
1341
1342 cnt = num_to_init / ENTRIES_PER_PAGE;
1343 pr_info("ftrace: allocating %ld entries in %d pages\n",
1344 num_to_init, cnt + 1);
1345
1346 for (i = 0; i < cnt; i++) {
1347 pg->next = (void *)get_zeroed_page(GFP_KERNEL);
1348
1349 /* If we fail, we'll try later anyway */
1350 if (!pg->next)
1351 break;
1352
1353 pg = pg->next;
1354 }
1355
1356 return 0;
1357 }
1358
1359 enum {
1360 FTRACE_ITER_FILTER = (1 << 0),
1361 FTRACE_ITER_CONT = (1 << 1),
1362 FTRACE_ITER_NOTRACE = (1 << 2),
1363 FTRACE_ITER_FAILURES = (1 << 3),
1364 FTRACE_ITER_PRINTALL = (1 << 4),
1365 FTRACE_ITER_HASH = (1 << 5),
1366 };
1367
1368 #define FTRACE_BUFF_MAX (KSYM_SYMBOL_LEN+4) /* room for wildcards */
1369
1370 struct ftrace_iterator {
1371 struct ftrace_page *pg;
1372 int hidx;
1373 int idx;
1374 unsigned flags;
1375 unsigned char buffer[FTRACE_BUFF_MAX+1];
1376 unsigned buffer_idx;
1377 unsigned filtered;
1378 };
1379
1380 static void *
1381 t_hash_next(struct seq_file *m, void *v, loff_t *pos)
1382 {
1383 struct ftrace_iterator *iter = m->private;
1384 struct hlist_node *hnd = v;
1385 struct hlist_head *hhd;
1386
1387 WARN_ON(!(iter->flags & FTRACE_ITER_HASH));
1388
1389 (*pos)++;
1390
1391 retry:
1392 if (iter->hidx >= FTRACE_FUNC_HASHSIZE)
1393 return NULL;
1394
1395 hhd = &ftrace_func_hash[iter->hidx];
1396
1397 if (hlist_empty(hhd)) {
1398 iter->hidx++;
1399 hnd = NULL;
1400 goto retry;
1401 }
1402
1403 if (!hnd)
1404 hnd = hhd->first;
1405 else {
1406 hnd = hnd->next;
1407 if (!hnd) {
1408 iter->hidx++;
1409 goto retry;
1410 }
1411 }
1412
1413 return hnd;
1414 }
1415
1416 static void *t_hash_start(struct seq_file *m, loff_t *pos)
1417 {
1418 struct ftrace_iterator *iter = m->private;
1419 void *p = NULL;
1420
1421 iter->flags |= FTRACE_ITER_HASH;
1422
1423 return t_hash_next(m, p, pos);
1424 }
1425
1426 static int t_hash_show(struct seq_file *m, void *v)
1427 {
1428 struct ftrace_func_probe *rec;
1429 struct hlist_node *hnd = v;
1430 char str[KSYM_SYMBOL_LEN];
1431
1432 rec = hlist_entry(hnd, struct ftrace_func_probe, node);
1433
1434 if (rec->ops->print)
1435 return rec->ops->print(m, rec->ip, rec->ops, rec->data);
1436
1437 kallsyms_lookup(rec->ip, NULL, NULL, NULL, str);
1438 seq_printf(m, "%s:", str);
1439
1440 kallsyms_lookup((unsigned long)rec->ops->func, NULL, NULL, NULL, str);
1441 seq_printf(m, "%s", str);
1442
1443 if (rec->data)
1444 seq_printf(m, ":%p", rec->data);
1445 seq_putc(m, '\n');
1446
1447 return 0;
1448 }
1449
1450 static void *
1451 t_next(struct seq_file *m, void *v, loff_t *pos)
1452 {
1453 struct ftrace_iterator *iter = m->private;
1454 struct dyn_ftrace *rec = NULL;
1455
1456 if (iter->flags & FTRACE_ITER_HASH)
1457 return t_hash_next(m, v, pos);
1458
1459 (*pos)++;
1460
1461 if (iter->flags & FTRACE_ITER_PRINTALL)
1462 return NULL;
1463
1464 retry:
1465 if (iter->idx >= iter->pg->index) {
1466 if (iter->pg->next) {
1467 iter->pg = iter->pg->next;
1468 iter->idx = 0;
1469 goto retry;
1470 } else {
1471 iter->idx = -1;
1472 }
1473 } else {
1474 rec = &iter->pg->records[iter->idx++];
1475 if ((rec->flags & FTRACE_FL_FREE) ||
1476
1477 (!(iter->flags & FTRACE_ITER_FAILURES) &&
1478 (rec->flags & FTRACE_FL_FAILED)) ||
1479
1480 ((iter->flags & FTRACE_ITER_FAILURES) &&
1481 !(rec->flags & FTRACE_FL_FAILED)) ||
1482
1483 ((iter->flags & FTRACE_ITER_FILTER) &&
1484 !(rec->flags & FTRACE_FL_FILTER)) ||
1485
1486 ((iter->flags & FTRACE_ITER_NOTRACE) &&
1487 !(rec->flags & FTRACE_FL_NOTRACE))) {
1488 rec = NULL;
1489 goto retry;
1490 }
1491 }
1492
1493 return rec;
1494 }
1495
1496 static void *t_start(struct seq_file *m, loff_t *pos)
1497 {
1498 struct ftrace_iterator *iter = m->private;
1499 void *p = NULL;
1500
1501 mutex_lock(&ftrace_lock);
1502 /*
1503 * For set_ftrace_filter reading, if we have the filter
1504 * off, we can short cut and just print out that all
1505 * functions are enabled.
1506 */
1507 if (iter->flags & FTRACE_ITER_FILTER && !ftrace_filtered) {
1508 if (*pos > 0)
1509 return t_hash_start(m, pos);
1510 iter->flags |= FTRACE_ITER_PRINTALL;
1511 (*pos)++;
1512 return iter;
1513 }
1514
1515 if (iter->flags & FTRACE_ITER_HASH)
1516 return t_hash_start(m, pos);
1517
1518 if (*pos > 0) {
1519 if (iter->idx < 0)
1520 return p;
1521 (*pos)--;
1522 iter->idx--;
1523 }
1524
1525 p = t_next(m, p, pos);
1526
1527 if (!p)
1528 return t_hash_start(m, pos);
1529
1530 return p;
1531 }
1532
1533 static void t_stop(struct seq_file *m, void *p)
1534 {
1535 mutex_unlock(&ftrace_lock);
1536 }
1537
1538 static int t_show(struct seq_file *m, void *v)
1539 {
1540 struct ftrace_iterator *iter = m->private;
1541 struct dyn_ftrace *rec = v;
1542 char str[KSYM_SYMBOL_LEN];
1543
1544 if (iter->flags & FTRACE_ITER_HASH)
1545 return t_hash_show(m, v);
1546
1547 if (iter->flags & FTRACE_ITER_PRINTALL) {
1548 seq_printf(m, "#### all functions enabled ####\n");
1549 return 0;
1550 }
1551
1552 if (!rec)
1553 return 0;
1554
1555 kallsyms_lookup(rec->ip, NULL, NULL, NULL, str);
1556
1557 seq_printf(m, "%s\n", str);
1558
1559 return 0;
1560 }
1561
1562 static struct seq_operations show_ftrace_seq_ops = {
1563 .start = t_start,
1564 .next = t_next,
1565 .stop = t_stop,
1566 .show = t_show,
1567 };
1568
1569 static int
1570 ftrace_avail_open(struct inode *inode, struct file *file)
1571 {
1572 struct ftrace_iterator *iter;
1573 int ret;
1574
1575 if (unlikely(ftrace_disabled))
1576 return -ENODEV;
1577
1578 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
1579 if (!iter)
1580 return -ENOMEM;
1581
1582 iter->pg = ftrace_pages_start;
1583
1584 ret = seq_open(file, &show_ftrace_seq_ops);
1585 if (!ret) {
1586 struct seq_file *m = file->private_data;
1587
1588 m->private = iter;
1589 } else {
1590 kfree(iter);
1591 }
1592
1593 return ret;
1594 }
1595
1596 int ftrace_avail_release(struct inode *inode, struct file *file)
1597 {
1598 struct seq_file *m = (struct seq_file *)file->private_data;
1599 struct ftrace_iterator *iter = m->private;
1600
1601 seq_release(inode, file);
1602 kfree(iter);
1603
1604 return 0;
1605 }
1606
1607 static int
1608 ftrace_failures_open(struct inode *inode, struct file *file)
1609 {
1610 int ret;
1611 struct seq_file *m;
1612 struct ftrace_iterator *iter;
1613
1614 ret = ftrace_avail_open(inode, file);
1615 if (!ret) {
1616 m = (struct seq_file *)file->private_data;
1617 iter = (struct ftrace_iterator *)m->private;
1618 iter->flags = FTRACE_ITER_FAILURES;
1619 }
1620
1621 return ret;
1622 }
1623
1624
1625 static void ftrace_filter_reset(int enable)
1626 {
1627 struct ftrace_page *pg;
1628 struct dyn_ftrace *rec;
1629 unsigned long type = enable ? FTRACE_FL_FILTER : FTRACE_FL_NOTRACE;
1630
1631 mutex_lock(&ftrace_lock);
1632 if (enable)
1633 ftrace_filtered = 0;
1634 do_for_each_ftrace_rec(pg, rec) {
1635 if (rec->flags & FTRACE_FL_FAILED)
1636 continue;
1637 rec->flags &= ~type;
1638 } while_for_each_ftrace_rec();
1639 mutex_unlock(&ftrace_lock);
1640 }
1641
1642 static int
1643 ftrace_regex_open(struct inode *inode, struct file *file, int enable)
1644 {
1645 struct ftrace_iterator *iter;
1646 int ret = 0;
1647
1648 if (unlikely(ftrace_disabled))
1649 return -ENODEV;
1650
1651 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
1652 if (!iter)
1653 return -ENOMEM;
1654
1655 mutex_lock(&ftrace_regex_lock);
1656 if ((file->f_mode & FMODE_WRITE) &&
1657 !(file->f_flags & O_APPEND))
1658 ftrace_filter_reset(enable);
1659
1660 if (file->f_mode & FMODE_READ) {
1661 iter->pg = ftrace_pages_start;
1662 iter->flags = enable ? FTRACE_ITER_FILTER :
1663 FTRACE_ITER_NOTRACE;
1664
1665 ret = seq_open(file, &show_ftrace_seq_ops);
1666 if (!ret) {
1667 struct seq_file *m = file->private_data;
1668 m->private = iter;
1669 } else
1670 kfree(iter);
1671 } else
1672 file->private_data = iter;
1673 mutex_unlock(&ftrace_regex_lock);
1674
1675 return ret;
1676 }
1677
1678 static int
1679 ftrace_filter_open(struct inode *inode, struct file *file)
1680 {
1681 return ftrace_regex_open(inode, file, 1);
1682 }
1683
1684 static int
1685 ftrace_notrace_open(struct inode *inode, struct file *file)
1686 {
1687 return ftrace_regex_open(inode, file, 0);
1688 }
1689
1690 static loff_t
1691 ftrace_regex_lseek(struct file *file, loff_t offset, int origin)
1692 {
1693 loff_t ret;
1694
1695 if (file->f_mode & FMODE_READ)
1696 ret = seq_lseek(file, offset, origin);
1697 else
1698 file->f_pos = ret = 1;
1699
1700 return ret;
1701 }
1702
1703 enum {
1704 MATCH_FULL,
1705 MATCH_FRONT_ONLY,
1706 MATCH_MIDDLE_ONLY,
1707 MATCH_END_ONLY,
1708 };
1709
1710 /*
1711 * (static function - no need for kernel doc)
1712 *
1713 * Pass in a buffer containing a glob and this function will
1714 * set search to point to the search part of the buffer and
1715 * return the type of search it is (see enum above).
1716 * This does modify buff.
1717 *
1718 * Returns enum type.
1719 * search returns the pointer to use for comparison.
1720 * not returns 1 if buff started with a '!'
1721 * 0 otherwise.
1722 */
1723 static int
1724 ftrace_setup_glob(char *buff, int len, char **search, int *not)
1725 {
1726 int type = MATCH_FULL;
1727 int i;
1728
1729 if (buff[0] == '!') {
1730 *not = 1;
1731 buff++;
1732 len--;
1733 } else
1734 *not = 0;
1735
1736 *search = buff;
1737
1738 for (i = 0; i < len; i++) {
1739 if (buff[i] == '*') {
1740 if (!i) {
1741 *search = buff + 1;
1742 type = MATCH_END_ONLY;
1743 } else {
1744 if (type == MATCH_END_ONLY)
1745 type = MATCH_MIDDLE_ONLY;
1746 else
1747 type = MATCH_FRONT_ONLY;
1748 buff[i] = 0;
1749 break;
1750 }
1751 }
1752 }
1753
1754 return type;
1755 }
1756
1757 static int ftrace_match(char *str, char *regex, int len, int type)
1758 {
1759 int matched = 0;
1760 char *ptr;
1761
1762 switch (type) {
1763 case MATCH_FULL:
1764 if (strcmp(str, regex) == 0)
1765 matched = 1;
1766 break;
1767 case MATCH_FRONT_ONLY:
1768 if (strncmp(str, regex, len) == 0)
1769 matched = 1;
1770 break;
1771 case MATCH_MIDDLE_ONLY:
1772 if (strstr(str, regex))
1773 matched = 1;
1774 break;
1775 case MATCH_END_ONLY:
1776 ptr = strstr(str, regex);
1777 if (ptr && (ptr[len] == 0))
1778 matched = 1;
1779 break;
1780 }
1781
1782 return matched;
1783 }
1784
1785 static int
1786 ftrace_match_record(struct dyn_ftrace *rec, char *regex, int len, int type)
1787 {
1788 char str[KSYM_SYMBOL_LEN];
1789
1790 kallsyms_lookup(rec->ip, NULL, NULL, NULL, str);
1791 return ftrace_match(str, regex, len, type);
1792 }
1793
1794 static void ftrace_match_records(char *buff, int len, int enable)
1795 {
1796 unsigned int search_len;
1797 struct ftrace_page *pg;
1798 struct dyn_ftrace *rec;
1799 unsigned long flag;
1800 char *search;
1801 int type;
1802 int not;
1803
1804 flag = enable ? FTRACE_FL_FILTER : FTRACE_FL_NOTRACE;
1805 type = ftrace_setup_glob(buff, len, &search, &not);
1806
1807 search_len = strlen(search);
1808
1809 mutex_lock(&ftrace_lock);
1810 do_for_each_ftrace_rec(pg, rec) {
1811
1812 if (rec->flags & FTRACE_FL_FAILED)
1813 continue;
1814
1815 if (ftrace_match_record(rec, search, search_len, type)) {
1816 if (not)
1817 rec->flags &= ~flag;
1818 else
1819 rec->flags |= flag;
1820 }
1821 /*
1822 * Only enable filtering if we have a function that
1823 * is filtered on.
1824 */
1825 if (enable && (rec->flags & FTRACE_FL_FILTER))
1826 ftrace_filtered = 1;
1827 } while_for_each_ftrace_rec();
1828 mutex_unlock(&ftrace_lock);
1829 }
1830
1831 static int
1832 ftrace_match_module_record(struct dyn_ftrace *rec, char *mod,
1833 char *regex, int len, int type)
1834 {
1835 char str[KSYM_SYMBOL_LEN];
1836 char *modname;
1837
1838 kallsyms_lookup(rec->ip, NULL, NULL, &modname, str);
1839
1840 if (!modname || strcmp(modname, mod))
1841 return 0;
1842
1843 /* blank search means to match all funcs in the mod */
1844 if (len)
1845 return ftrace_match(str, regex, len, type);
1846 else
1847 return 1;
1848 }
1849
1850 static void ftrace_match_module_records(char *buff, char *mod, int enable)
1851 {
1852 unsigned search_len = 0;
1853 struct ftrace_page *pg;
1854 struct dyn_ftrace *rec;
1855 int type = MATCH_FULL;
1856 char *search = buff;
1857 unsigned long flag;
1858 int not = 0;
1859
1860 flag = enable ? FTRACE_FL_FILTER : FTRACE_FL_NOTRACE;
1861
1862 /* blank or '*' mean the same */
1863 if (strcmp(buff, "*") == 0)
1864 buff[0] = 0;
1865
1866 /* handle the case of 'dont filter this module' */
1867 if (strcmp(buff, "!") == 0 || strcmp(buff, "!*") == 0) {
1868 buff[0] = 0;
1869 not = 1;
1870 }
1871
1872 if (strlen(buff)) {
1873 type = ftrace_setup_glob(buff, strlen(buff), &search, &not);
1874 search_len = strlen(search);
1875 }
1876
1877 mutex_lock(&ftrace_lock);
1878 do_for_each_ftrace_rec(pg, rec) {
1879
1880 if (rec->flags & FTRACE_FL_FAILED)
1881 continue;
1882
1883 if (ftrace_match_module_record(rec, mod,
1884 search, search_len, type)) {
1885 if (not)
1886 rec->flags &= ~flag;
1887 else
1888 rec->flags |= flag;
1889 }
1890 if (enable && (rec->flags & FTRACE_FL_FILTER))
1891 ftrace_filtered = 1;
1892
1893 } while_for_each_ftrace_rec();
1894 mutex_unlock(&ftrace_lock);
1895 }
1896
1897 /*
1898 * We register the module command as a template to show others how
1899 * to register the a command as well.
1900 */
1901
1902 static int
1903 ftrace_mod_callback(char *func, char *cmd, char *param, int enable)
1904 {
1905 char *mod;
1906
1907 /*
1908 * cmd == 'mod' because we only registered this func
1909 * for the 'mod' ftrace_func_command.
1910 * But if you register one func with multiple commands,
1911 * you can tell which command was used by the cmd
1912 * parameter.
1913 */
1914
1915 /* we must have a module name */
1916 if (!param)
1917 return -EINVAL;
1918
1919 mod = strsep(&param, ":");
1920 if (!strlen(mod))
1921 return -EINVAL;
1922
1923 ftrace_match_module_records(func, mod, enable);
1924 return 0;
1925 }
1926
1927 static struct ftrace_func_command ftrace_mod_cmd = {
1928 .name = "mod",
1929 .func = ftrace_mod_callback,
1930 };
1931
1932 static int __init ftrace_mod_cmd_init(void)
1933 {
1934 return register_ftrace_command(&ftrace_mod_cmd);
1935 }
1936 device_initcall(ftrace_mod_cmd_init);
1937
1938 static void
1939 function_trace_probe_call(unsigned long ip, unsigned long parent_ip)
1940 {
1941 struct ftrace_func_probe *entry;
1942 struct hlist_head *hhd;
1943 struct hlist_node *n;
1944 unsigned long key;
1945 int resched;
1946
1947 key = hash_long(ip, FTRACE_HASH_BITS);
1948
1949 hhd = &ftrace_func_hash[key];
1950
1951 if (hlist_empty(hhd))
1952 return;
1953
1954 /*
1955 * Disable preemption for these calls to prevent a RCU grace
1956 * period. This syncs the hash iteration and freeing of items
1957 * on the hash. rcu_read_lock is too dangerous here.
1958 */
1959 resched = ftrace_preempt_disable();
1960 hlist_for_each_entry_rcu(entry, n, hhd, node) {
1961 if (entry->ip == ip)
1962 entry->ops->func(ip, parent_ip, &entry->data);
1963 }
1964 ftrace_preempt_enable(resched);
1965 }
1966
1967 static struct ftrace_ops trace_probe_ops __read_mostly =
1968 {
1969 .func = function_trace_probe_call,
1970 };
1971
1972 static int ftrace_probe_registered;
1973
1974 static void __enable_ftrace_function_probe(void)
1975 {
1976 int i;
1977
1978 if (ftrace_probe_registered)
1979 return;
1980
1981 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
1982 struct hlist_head *hhd = &ftrace_func_hash[i];
1983 if (hhd->first)
1984 break;
1985 }
1986 /* Nothing registered? */
1987 if (i == FTRACE_FUNC_HASHSIZE)
1988 return;
1989
1990 __register_ftrace_function(&trace_probe_ops);
1991 ftrace_startup(0);
1992 ftrace_probe_registered = 1;
1993 }
1994
1995 static void __disable_ftrace_function_probe(void)
1996 {
1997 int i;
1998
1999 if (!ftrace_probe_registered)
2000 return;
2001
2002 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
2003 struct hlist_head *hhd = &ftrace_func_hash[i];
2004 if (hhd->first)
2005 return;
2006 }
2007
2008 /* no more funcs left */
2009 __unregister_ftrace_function(&trace_probe_ops);
2010 ftrace_shutdown(0);
2011 ftrace_probe_registered = 0;
2012 }
2013
2014
2015 static void ftrace_free_entry_rcu(struct rcu_head *rhp)
2016 {
2017 struct ftrace_func_probe *entry =
2018 container_of(rhp, struct ftrace_func_probe, rcu);
2019
2020 if (entry->ops->free)
2021 entry->ops->free(&entry->data);
2022 kfree(entry);
2023 }
2024
2025
2026 int
2027 register_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
2028 void *data)
2029 {
2030 struct ftrace_func_probe *entry;
2031 struct ftrace_page *pg;
2032 struct dyn_ftrace *rec;
2033 int type, len, not;
2034 unsigned long key;
2035 int count = 0;
2036 char *search;
2037
2038 type = ftrace_setup_glob(glob, strlen(glob), &search, &not);
2039 len = strlen(search);
2040
2041 /* we do not support '!' for function probes */
2042 if (WARN_ON(not))
2043 return -EINVAL;
2044
2045 mutex_lock(&ftrace_lock);
2046 do_for_each_ftrace_rec(pg, rec) {
2047
2048 if (rec->flags & FTRACE_FL_FAILED)
2049 continue;
2050
2051 if (!ftrace_match_record(rec, search, len, type))
2052 continue;
2053
2054 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
2055 if (!entry) {
2056 /* If we did not process any, then return error */
2057 if (!count)
2058 count = -ENOMEM;
2059 goto out_unlock;
2060 }
2061
2062 count++;
2063
2064 entry->data = data;
2065
2066 /*
2067 * The caller might want to do something special
2068 * for each function we find. We call the callback
2069 * to give the caller an opportunity to do so.
2070 */
2071 if (ops->callback) {
2072 if (ops->callback(rec->ip, &entry->data) < 0) {
2073 /* caller does not like this func */
2074 kfree(entry);
2075 continue;
2076 }
2077 }
2078
2079 entry->ops = ops;
2080 entry->ip = rec->ip;
2081
2082 key = hash_long(entry->ip, FTRACE_HASH_BITS);
2083 hlist_add_head_rcu(&entry->node, &ftrace_func_hash[key]);
2084
2085 } while_for_each_ftrace_rec();
2086 __enable_ftrace_function_probe();
2087
2088 out_unlock:
2089 mutex_unlock(&ftrace_lock);
2090
2091 return count;
2092 }
2093
2094 enum {
2095 PROBE_TEST_FUNC = 1,
2096 PROBE_TEST_DATA = 2
2097 };
2098
2099 static void
2100 __unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
2101 void *data, int flags)
2102 {
2103 struct ftrace_func_probe *entry;
2104 struct hlist_node *n, *tmp;
2105 char str[KSYM_SYMBOL_LEN];
2106 int type = MATCH_FULL;
2107 int i, len = 0;
2108 char *search;
2109
2110 if (glob && (strcmp(glob, "*") || !strlen(glob)))
2111 glob = NULL;
2112 else {
2113 int not;
2114
2115 type = ftrace_setup_glob(glob, strlen(glob), &search, &not);
2116 len = strlen(search);
2117
2118 /* we do not support '!' for function probes */
2119 if (WARN_ON(not))
2120 return;
2121 }
2122
2123 mutex_lock(&ftrace_lock);
2124 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
2125 struct hlist_head *hhd = &ftrace_func_hash[i];
2126
2127 hlist_for_each_entry_safe(entry, n, tmp, hhd, node) {
2128
2129 /* break up if statements for readability */
2130 if ((flags & PROBE_TEST_FUNC) && entry->ops != ops)
2131 continue;
2132
2133 if ((flags & PROBE_TEST_DATA) && entry->data != data)
2134 continue;
2135
2136 /* do this last, since it is the most expensive */
2137 if (glob) {
2138 kallsyms_lookup(entry->ip, NULL, NULL,
2139 NULL, str);
2140 if (!ftrace_match(str, glob, len, type))
2141 continue;
2142 }
2143
2144 hlist_del(&entry->node);
2145 call_rcu(&entry->rcu, ftrace_free_entry_rcu);
2146 }
2147 }
2148 __disable_ftrace_function_probe();
2149 mutex_unlock(&ftrace_lock);
2150 }
2151
2152 void
2153 unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
2154 void *data)
2155 {
2156 __unregister_ftrace_function_probe(glob, ops, data,
2157 PROBE_TEST_FUNC | PROBE_TEST_DATA);
2158 }
2159
2160 void
2161 unregister_ftrace_function_probe_func(char *glob, struct ftrace_probe_ops *ops)
2162 {
2163 __unregister_ftrace_function_probe(glob, ops, NULL, PROBE_TEST_FUNC);
2164 }
2165
2166 void unregister_ftrace_function_probe_all(char *glob)
2167 {
2168 __unregister_ftrace_function_probe(glob, NULL, NULL, 0);
2169 }
2170
2171 static LIST_HEAD(ftrace_commands);
2172 static DEFINE_MUTEX(ftrace_cmd_mutex);
2173
2174 int register_ftrace_command(struct ftrace_func_command *cmd)
2175 {
2176 struct ftrace_func_command *p;
2177 int ret = 0;
2178
2179 mutex_lock(&ftrace_cmd_mutex);
2180 list_for_each_entry(p, &ftrace_commands, list) {
2181 if (strcmp(cmd->name, p->name) == 0) {
2182 ret = -EBUSY;
2183 goto out_unlock;
2184 }
2185 }
2186 list_add(&cmd->list, &ftrace_commands);
2187 out_unlock:
2188 mutex_unlock(&ftrace_cmd_mutex);
2189
2190 return ret;
2191 }
2192
2193 int unregister_ftrace_command(struct ftrace_func_command *cmd)
2194 {
2195 struct ftrace_func_command *p, *n;
2196 int ret = -ENODEV;
2197
2198 mutex_lock(&ftrace_cmd_mutex);
2199 list_for_each_entry_safe(p, n, &ftrace_commands, list) {
2200 if (strcmp(cmd->name, p->name) == 0) {
2201 ret = 0;
2202 list_del_init(&p->list);
2203 goto out_unlock;
2204 }
2205 }
2206 out_unlock:
2207 mutex_unlock(&ftrace_cmd_mutex);
2208
2209 return ret;
2210 }
2211
2212 static int ftrace_process_regex(char *buff, int len, int enable)
2213 {
2214 char *func, *command, *next = buff;
2215 struct ftrace_func_command *p;
2216 int ret = -EINVAL;
2217
2218 func = strsep(&next, ":");
2219
2220 if (!next) {
2221 ftrace_match_records(func, len, enable);
2222 return 0;
2223 }
2224
2225 /* command found */
2226
2227 command = strsep(&next, ":");
2228
2229 mutex_lock(&ftrace_cmd_mutex);
2230 list_for_each_entry(p, &ftrace_commands, list) {
2231 if (strcmp(p->name, command) == 0) {
2232 ret = p->func(func, command, next, enable);
2233 goto out_unlock;
2234 }
2235 }
2236 out_unlock:
2237 mutex_unlock(&ftrace_cmd_mutex);
2238
2239 return ret;
2240 }
2241
2242 static ssize_t
2243 ftrace_regex_write(struct file *file, const char __user *ubuf,
2244 size_t cnt, loff_t *ppos, int enable)
2245 {
2246 struct ftrace_iterator *iter;
2247 char ch;
2248 size_t read = 0;
2249 ssize_t ret;
2250
2251 if (!cnt || cnt < 0)
2252 return 0;
2253
2254 mutex_lock(&ftrace_regex_lock);
2255
2256 if (file->f_mode & FMODE_READ) {
2257 struct seq_file *m = file->private_data;
2258 iter = m->private;
2259 } else
2260 iter = file->private_data;
2261
2262 if (!*ppos) {
2263 iter->flags &= ~FTRACE_ITER_CONT;
2264 iter->buffer_idx = 0;
2265 }
2266
2267 ret = get_user(ch, ubuf++);
2268 if (ret)
2269 goto out;
2270 read++;
2271 cnt--;
2272
2273 if (!(iter->flags & ~FTRACE_ITER_CONT)) {
2274 /* skip white space */
2275 while (cnt && isspace(ch)) {
2276 ret = get_user(ch, ubuf++);
2277 if (ret)
2278 goto out;
2279 read++;
2280 cnt--;
2281 }
2282
2283 if (isspace(ch)) {
2284 file->f_pos += read;
2285 ret = read;
2286 goto out;
2287 }
2288
2289 iter->buffer_idx = 0;
2290 }
2291
2292 while (cnt && !isspace(ch)) {
2293 if (iter->buffer_idx < FTRACE_BUFF_MAX)
2294 iter->buffer[iter->buffer_idx++] = ch;
2295 else {
2296 ret = -EINVAL;
2297 goto out;
2298 }
2299 ret = get_user(ch, ubuf++);
2300 if (ret)
2301 goto out;
2302 read++;
2303 cnt--;
2304 }
2305
2306 if (isspace(ch)) {
2307 iter->filtered++;
2308 iter->buffer[iter->buffer_idx] = 0;
2309 ret = ftrace_process_regex(iter->buffer,
2310 iter->buffer_idx, enable);
2311 if (ret)
2312 goto out;
2313 iter->buffer_idx = 0;
2314 } else
2315 iter->flags |= FTRACE_ITER_CONT;
2316
2317
2318 file->f_pos += read;
2319
2320 ret = read;
2321 out:
2322 mutex_unlock(&ftrace_regex_lock);
2323
2324 return ret;
2325 }
2326
2327 static ssize_t
2328 ftrace_filter_write(struct file *file, const char __user *ubuf,
2329 size_t cnt, loff_t *ppos)
2330 {
2331 return ftrace_regex_write(file, ubuf, cnt, ppos, 1);
2332 }
2333
2334 static ssize_t
2335 ftrace_notrace_write(struct file *file, const char __user *ubuf,
2336 size_t cnt, loff_t *ppos)
2337 {
2338 return ftrace_regex_write(file, ubuf, cnt, ppos, 0);
2339 }
2340
2341 static void
2342 ftrace_set_regex(unsigned char *buf, int len, int reset, int enable)
2343 {
2344 if (unlikely(ftrace_disabled))
2345 return;
2346
2347 mutex_lock(&ftrace_regex_lock);
2348 if (reset)
2349 ftrace_filter_reset(enable);
2350 if (buf)
2351 ftrace_match_records(buf, len, enable);
2352 mutex_unlock(&ftrace_regex_lock);
2353 }
2354
2355 /**
2356 * ftrace_set_filter - set a function to filter on in ftrace
2357 * @buf - the string that holds the function filter text.
2358 * @len - the length of the string.
2359 * @reset - non zero to reset all filters before applying this filter.
2360 *
2361 * Filters denote which functions should be enabled when tracing is enabled.
2362 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
2363 */
2364 void ftrace_set_filter(unsigned char *buf, int len, int reset)
2365 {
2366 ftrace_set_regex(buf, len, reset, 1);
2367 }
2368
2369 /**
2370 * ftrace_set_notrace - set a function to not trace in ftrace
2371 * @buf - the string that holds the function notrace text.
2372 * @len - the length of the string.
2373 * @reset - non zero to reset all filters before applying this filter.
2374 *
2375 * Notrace Filters denote which functions should not be enabled when tracing
2376 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
2377 * for tracing.
2378 */
2379 void ftrace_set_notrace(unsigned char *buf, int len, int reset)
2380 {
2381 ftrace_set_regex(buf, len, reset, 0);
2382 }
2383
2384 /*
2385 * command line interface to allow users to set filters on boot up.
2386 */
2387 #define FTRACE_FILTER_SIZE COMMAND_LINE_SIZE
2388 static char ftrace_notrace_buf[FTRACE_FILTER_SIZE] __initdata;
2389 static char ftrace_filter_buf[FTRACE_FILTER_SIZE] __initdata;
2390
2391 static int __init set_ftrace_notrace(char *str)
2392 {
2393 strncpy(ftrace_notrace_buf, str, FTRACE_FILTER_SIZE);
2394 return 1;
2395 }
2396 __setup("ftrace_notrace=", set_ftrace_notrace);
2397
2398 static int __init set_ftrace_filter(char *str)
2399 {
2400 strncpy(ftrace_filter_buf, str, FTRACE_FILTER_SIZE);
2401 return 1;
2402 }
2403 __setup("ftrace_filter=", set_ftrace_filter);
2404
2405 static void __init set_ftrace_early_filter(char *buf, int enable)
2406 {
2407 char *func;
2408
2409 while (buf) {
2410 func = strsep(&buf, ",");
2411 ftrace_set_regex(func, strlen(func), 0, enable);
2412 }
2413 }
2414
2415 static void __init set_ftrace_early_filters(void)
2416 {
2417 if (ftrace_filter_buf[0])
2418 set_ftrace_early_filter(ftrace_filter_buf, 1);
2419 if (ftrace_notrace_buf[0])
2420 set_ftrace_early_filter(ftrace_notrace_buf, 0);
2421 }
2422
2423 static int
2424 ftrace_regex_release(struct inode *inode, struct file *file, int enable)
2425 {
2426 struct seq_file *m = (struct seq_file *)file->private_data;
2427 struct ftrace_iterator *iter;
2428
2429 mutex_lock(&ftrace_regex_lock);
2430 if (file->f_mode & FMODE_READ) {
2431 iter = m->private;
2432
2433 seq_release(inode, file);
2434 } else
2435 iter = file->private_data;
2436
2437 if (iter->buffer_idx) {
2438 iter->filtered++;
2439 iter->buffer[iter->buffer_idx] = 0;
2440 ftrace_match_records(iter->buffer, iter->buffer_idx, enable);
2441 }
2442
2443 mutex_lock(&ftrace_lock);
2444 if (ftrace_start_up && ftrace_enabled)
2445 ftrace_run_update_code(FTRACE_ENABLE_CALLS);
2446 mutex_unlock(&ftrace_lock);
2447
2448 kfree(iter);
2449 mutex_unlock(&ftrace_regex_lock);
2450 return 0;
2451 }
2452
2453 static int
2454 ftrace_filter_release(struct inode *inode, struct file *file)
2455 {
2456 return ftrace_regex_release(inode, file, 1);
2457 }
2458
2459 static int
2460 ftrace_notrace_release(struct inode *inode, struct file *file)
2461 {
2462 return ftrace_regex_release(inode, file, 0);
2463 }
2464
2465 static const struct file_operations ftrace_avail_fops = {
2466 .open = ftrace_avail_open,
2467 .read = seq_read,
2468 .llseek = seq_lseek,
2469 .release = ftrace_avail_release,
2470 };
2471
2472 static const struct file_operations ftrace_failures_fops = {
2473 .open = ftrace_failures_open,
2474 .read = seq_read,
2475 .llseek = seq_lseek,
2476 .release = ftrace_avail_release,
2477 };
2478
2479 static const struct file_operations ftrace_filter_fops = {
2480 .open = ftrace_filter_open,
2481 .read = seq_read,
2482 .write = ftrace_filter_write,
2483 .llseek = ftrace_regex_lseek,
2484 .release = ftrace_filter_release,
2485 };
2486
2487 static const struct file_operations ftrace_notrace_fops = {
2488 .open = ftrace_notrace_open,
2489 .read = seq_read,
2490 .write = ftrace_notrace_write,
2491 .llseek = ftrace_regex_lseek,
2492 .release = ftrace_notrace_release,
2493 };
2494
2495 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
2496
2497 static DEFINE_MUTEX(graph_lock);
2498
2499 int ftrace_graph_count;
2500 unsigned long ftrace_graph_funcs[FTRACE_GRAPH_MAX_FUNCS] __read_mostly;
2501
2502 static void *
2503 g_next(struct seq_file *m, void *v, loff_t *pos)
2504 {
2505 unsigned long *array = m->private;
2506 int index = *pos;
2507
2508 (*pos)++;
2509
2510 if (index >= ftrace_graph_count)
2511 return NULL;
2512
2513 return &array[index];
2514 }
2515
2516 static void *g_start(struct seq_file *m, loff_t *pos)
2517 {
2518 void *p = NULL;
2519
2520 mutex_lock(&graph_lock);
2521
2522 /* Nothing, tell g_show to print all functions are enabled */
2523 if (!ftrace_graph_count && !*pos)
2524 return (void *)1;
2525
2526 p = g_next(m, p, pos);
2527
2528 return p;
2529 }
2530
2531 static void g_stop(struct seq_file *m, void *p)
2532 {
2533 mutex_unlock(&graph_lock);
2534 }
2535
2536 static int g_show(struct seq_file *m, void *v)
2537 {
2538 unsigned long *ptr = v;
2539 char str[KSYM_SYMBOL_LEN];
2540
2541 if (!ptr)
2542 return 0;
2543
2544 if (ptr == (unsigned long *)1) {
2545 seq_printf(m, "#### all functions enabled ####\n");
2546 return 0;
2547 }
2548
2549 kallsyms_lookup(*ptr, NULL, NULL, NULL, str);
2550
2551 seq_printf(m, "%s\n", str);
2552
2553 return 0;
2554 }
2555
2556 static struct seq_operations ftrace_graph_seq_ops = {
2557 .start = g_start,
2558 .next = g_next,
2559 .stop = g_stop,
2560 .show = g_show,
2561 };
2562
2563 static int
2564 ftrace_graph_open(struct inode *inode, struct file *file)
2565 {
2566 int ret = 0;
2567
2568 if (unlikely(ftrace_disabled))
2569 return -ENODEV;
2570
2571 mutex_lock(&graph_lock);
2572 if ((file->f_mode & FMODE_WRITE) &&
2573 !(file->f_flags & O_APPEND)) {
2574 ftrace_graph_count = 0;
2575 memset(ftrace_graph_funcs, 0, sizeof(ftrace_graph_funcs));
2576 }
2577
2578 if (file->f_mode & FMODE_READ) {
2579 ret = seq_open(file, &ftrace_graph_seq_ops);
2580 if (!ret) {
2581 struct seq_file *m = file->private_data;
2582 m->private = ftrace_graph_funcs;
2583 }
2584 } else
2585 file->private_data = ftrace_graph_funcs;
2586 mutex_unlock(&graph_lock);
2587
2588 return ret;
2589 }
2590
2591 static int
2592 ftrace_set_func(unsigned long *array, int *idx, char *buffer)
2593 {
2594 struct dyn_ftrace *rec;
2595 struct ftrace_page *pg;
2596 int search_len;
2597 int found = 0;
2598 int type, not;
2599 char *search;
2600 bool exists;
2601 int i;
2602
2603 if (ftrace_disabled)
2604 return -ENODEV;
2605
2606 /* decode regex */
2607 type = ftrace_setup_glob(buffer, strlen(buffer), &search, &not);
2608 if (not)
2609 return -EINVAL;
2610
2611 search_len = strlen(search);
2612
2613 mutex_lock(&ftrace_lock);
2614 do_for_each_ftrace_rec(pg, rec) {
2615
2616 if (*idx >= FTRACE_GRAPH_MAX_FUNCS)
2617 break;
2618
2619 if (rec->flags & (FTRACE_FL_FAILED | FTRACE_FL_FREE))
2620 continue;
2621
2622 if (ftrace_match_record(rec, search, search_len, type)) {
2623 /* ensure it is not already in the array */
2624 exists = false;
2625 for (i = 0; i < *idx; i++)
2626 if (array[i] == rec->ip) {
2627 exists = true;
2628 break;
2629 }
2630 if (!exists) {
2631 array[(*idx)++] = rec->ip;
2632 found = 1;
2633 }
2634 }
2635 } while_for_each_ftrace_rec();
2636
2637 mutex_unlock(&ftrace_lock);
2638
2639 return found ? 0 : -EINVAL;
2640 }
2641
2642 static ssize_t
2643 ftrace_graph_write(struct file *file, const char __user *ubuf,
2644 size_t cnt, loff_t *ppos)
2645 {
2646 unsigned char buffer[FTRACE_BUFF_MAX+1];
2647 unsigned long *array;
2648 size_t read = 0;
2649 ssize_t ret;
2650 int index = 0;
2651 char ch;
2652
2653 if (!cnt || cnt < 0)
2654 return 0;
2655
2656 mutex_lock(&graph_lock);
2657
2658 if (ftrace_graph_count >= FTRACE_GRAPH_MAX_FUNCS) {
2659 ret = -EBUSY;
2660 goto out;
2661 }
2662
2663 if (file->f_mode & FMODE_READ) {
2664 struct seq_file *m = file->private_data;
2665 array = m->private;
2666 } else
2667 array = file->private_data;
2668
2669 ret = get_user(ch, ubuf++);
2670 if (ret)
2671 goto out;
2672 read++;
2673 cnt--;
2674
2675 /* skip white space */
2676 while (cnt && isspace(ch)) {
2677 ret = get_user(ch, ubuf++);
2678 if (ret)
2679 goto out;
2680 read++;
2681 cnt--;
2682 }
2683
2684 if (isspace(ch)) {
2685 *ppos += read;
2686 ret = read;
2687 goto out;
2688 }
2689
2690 while (cnt && !isspace(ch)) {
2691 if (index < FTRACE_BUFF_MAX)
2692 buffer[index++] = ch;
2693 else {
2694 ret = -EINVAL;
2695 goto out;
2696 }
2697 ret = get_user(ch, ubuf++);
2698 if (ret)
2699 goto out;
2700 read++;
2701 cnt--;
2702 }
2703 buffer[index] = 0;
2704
2705 /* we allow only one expression at a time */
2706 ret = ftrace_set_func(array, &ftrace_graph_count, buffer);
2707 if (ret)
2708 goto out;
2709
2710 file->f_pos += read;
2711
2712 ret = read;
2713 out:
2714 mutex_unlock(&graph_lock);
2715
2716 return ret;
2717 }
2718
2719 static const struct file_operations ftrace_graph_fops = {
2720 .open = ftrace_graph_open,
2721 .read = seq_read,
2722 .write = ftrace_graph_write,
2723 };
2724 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
2725
2726 static __init int ftrace_init_dyn_debugfs(struct dentry *d_tracer)
2727 {
2728
2729 trace_create_file("available_filter_functions", 0444,
2730 d_tracer, NULL, &ftrace_avail_fops);
2731
2732 trace_create_file("failures", 0444,
2733 d_tracer, NULL, &ftrace_failures_fops);
2734
2735 trace_create_file("set_ftrace_filter", 0644, d_tracer,
2736 NULL, &ftrace_filter_fops);
2737
2738 trace_create_file("set_ftrace_notrace", 0644, d_tracer,
2739 NULL, &ftrace_notrace_fops);
2740
2741 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
2742 trace_create_file("set_graph_function", 0444, d_tracer,
2743 NULL,
2744 &ftrace_graph_fops);
2745 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
2746
2747 return 0;
2748 }
2749
2750 static int ftrace_convert_nops(struct module *mod,
2751 unsigned long *start,
2752 unsigned long *end)
2753 {
2754 unsigned long *p;
2755 unsigned long addr;
2756 unsigned long flags;
2757
2758 mutex_lock(&ftrace_lock);
2759 p = start;
2760 while (p < end) {
2761 addr = ftrace_call_adjust(*p++);
2762 /*
2763 * Some architecture linkers will pad between
2764 * the different mcount_loc sections of different
2765 * object files to satisfy alignments.
2766 * Skip any NULL pointers.
2767 */
2768 if (!addr)
2769 continue;
2770 ftrace_record_ip(addr);
2771 }
2772
2773 /* disable interrupts to prevent kstop machine */
2774 local_irq_save(flags);
2775 ftrace_update_code(mod);
2776 local_irq_restore(flags);
2777 mutex_unlock(&ftrace_lock);
2778
2779 return 0;
2780 }
2781
2782 #ifdef CONFIG_MODULES
2783 void ftrace_release(void *start, void *end)
2784 {
2785 struct dyn_ftrace *rec;
2786 struct ftrace_page *pg;
2787 unsigned long s = (unsigned long)start;
2788 unsigned long e = (unsigned long)end;
2789
2790 if (ftrace_disabled || !start || start == end)
2791 return;
2792
2793 mutex_lock(&ftrace_lock);
2794 do_for_each_ftrace_rec(pg, rec) {
2795 if ((rec->ip >= s) && (rec->ip < e)) {
2796 /*
2797 * rec->ip is changed in ftrace_free_rec()
2798 * It should not between s and e if record was freed.
2799 */
2800 FTRACE_WARN_ON(rec->flags & FTRACE_FL_FREE);
2801 ftrace_free_rec(rec);
2802 }
2803 } while_for_each_ftrace_rec();
2804 mutex_unlock(&ftrace_lock);
2805 }
2806
2807 static void ftrace_init_module(struct module *mod,
2808 unsigned long *start, unsigned long *end)
2809 {
2810 if (ftrace_disabled || start == end)
2811 return;
2812 ftrace_convert_nops(mod, start, end);
2813 }
2814
2815 static int ftrace_module_notify(struct notifier_block *self,
2816 unsigned long val, void *data)
2817 {
2818 struct module *mod = data;
2819
2820 switch (val) {
2821 case MODULE_STATE_COMING:
2822 ftrace_init_module(mod, mod->ftrace_callsites,
2823 mod->ftrace_callsites +
2824 mod->num_ftrace_callsites);
2825 break;
2826 case MODULE_STATE_GOING:
2827 ftrace_release(mod->ftrace_callsites,
2828 mod->ftrace_callsites +
2829 mod->num_ftrace_callsites);
2830 break;
2831 }
2832
2833 return 0;
2834 }
2835 #else
2836 static int ftrace_module_notify(struct notifier_block *self,
2837 unsigned long val, void *data)
2838 {
2839 return 0;
2840 }
2841 #endif /* CONFIG_MODULES */
2842
2843 struct notifier_block ftrace_module_nb = {
2844 .notifier_call = ftrace_module_notify,
2845 .priority = 0,
2846 };
2847
2848 extern unsigned long __start_mcount_loc[];
2849 extern unsigned long __stop_mcount_loc[];
2850
2851 void __init ftrace_init(void)
2852 {
2853 unsigned long count, addr, flags;
2854 int ret;
2855
2856 /* Keep the ftrace pointer to the stub */
2857 addr = (unsigned long)ftrace_stub;
2858
2859 local_irq_save(flags);
2860 ftrace_dyn_arch_init(&addr);
2861 local_irq_restore(flags);
2862
2863 /* ftrace_dyn_arch_init places the return code in addr */
2864 if (addr)
2865 goto failed;
2866
2867 count = __stop_mcount_loc - __start_mcount_loc;
2868
2869 ret = ftrace_dyn_table_alloc(count);
2870 if (ret)
2871 goto failed;
2872
2873 last_ftrace_enabled = ftrace_enabled = 1;
2874
2875 ret = ftrace_convert_nops(NULL,
2876 __start_mcount_loc,
2877 __stop_mcount_loc);
2878
2879 ret = register_module_notifier(&ftrace_module_nb);
2880 if (ret)
2881 pr_warning("Failed to register trace ftrace module notifier\n");
2882
2883 set_ftrace_early_filters();
2884
2885 return;
2886 failed:
2887 ftrace_disabled = 1;
2888 }
2889
2890 #else
2891
2892 static int __init ftrace_nodyn_init(void)
2893 {
2894 ftrace_enabled = 1;
2895 return 0;
2896 }
2897 device_initcall(ftrace_nodyn_init);
2898
2899 static inline int ftrace_init_dyn_debugfs(struct dentry *d_tracer) { return 0; }
2900 static inline void ftrace_startup_enable(int command) { }
2901 /* Keep as macros so we do not need to define the commands */
2902 # define ftrace_startup(command) do { } while (0)
2903 # define ftrace_shutdown(command) do { } while (0)
2904 # define ftrace_startup_sysctl() do { } while (0)
2905 # define ftrace_shutdown_sysctl() do { } while (0)
2906 #endif /* CONFIG_DYNAMIC_FTRACE */
2907
2908 static ssize_t
2909 ftrace_pid_read(struct file *file, char __user *ubuf,
2910 size_t cnt, loff_t *ppos)
2911 {
2912 char buf[64];
2913 int r;
2914
2915 if (ftrace_pid_trace == ftrace_swapper_pid)
2916 r = sprintf(buf, "swapper tasks\n");
2917 else if (ftrace_pid_trace)
2918 r = sprintf(buf, "%u\n", pid_vnr(ftrace_pid_trace));
2919 else
2920 r = sprintf(buf, "no pid\n");
2921
2922 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2923 }
2924
2925 static void clear_ftrace_swapper(void)
2926 {
2927 struct task_struct *p;
2928 int cpu;
2929
2930 get_online_cpus();
2931 for_each_online_cpu(cpu) {
2932 p = idle_task(cpu);
2933 clear_tsk_trace_trace(p);
2934 }
2935 put_online_cpus();
2936 }
2937
2938 static void set_ftrace_swapper(void)
2939 {
2940 struct task_struct *p;
2941 int cpu;
2942
2943 get_online_cpus();
2944 for_each_online_cpu(cpu) {
2945 p = idle_task(cpu);
2946 set_tsk_trace_trace(p);
2947 }
2948 put_online_cpus();
2949 }
2950
2951 static void clear_ftrace_pid(struct pid *pid)
2952 {
2953 struct task_struct *p;
2954
2955 rcu_read_lock();
2956 do_each_pid_task(pid, PIDTYPE_PID, p) {
2957 clear_tsk_trace_trace(p);
2958 } while_each_pid_task(pid, PIDTYPE_PID, p);
2959 rcu_read_unlock();
2960
2961 put_pid(pid);
2962 }
2963
2964 static void set_ftrace_pid(struct pid *pid)
2965 {
2966 struct task_struct *p;
2967
2968 rcu_read_lock();
2969 do_each_pid_task(pid, PIDTYPE_PID, p) {
2970 set_tsk_trace_trace(p);
2971 } while_each_pid_task(pid, PIDTYPE_PID, p);
2972 rcu_read_unlock();
2973 }
2974
2975 static void clear_ftrace_pid_task(struct pid **pid)
2976 {
2977 if (*pid == ftrace_swapper_pid)
2978 clear_ftrace_swapper();
2979 else
2980 clear_ftrace_pid(*pid);
2981
2982 *pid = NULL;
2983 }
2984
2985 static void set_ftrace_pid_task(struct pid *pid)
2986 {
2987 if (pid == ftrace_swapper_pid)
2988 set_ftrace_swapper();
2989 else
2990 set_ftrace_pid(pid);
2991 }
2992
2993 static ssize_t
2994 ftrace_pid_write(struct file *filp, const char __user *ubuf,
2995 size_t cnt, loff_t *ppos)
2996 {
2997 struct pid *pid;
2998 char buf[64];
2999 long val;
3000 int ret;
3001
3002 if (cnt >= sizeof(buf))
3003 return -EINVAL;
3004
3005 if (copy_from_user(&buf, ubuf, cnt))
3006 return -EFAULT;
3007
3008 buf[cnt] = 0;
3009
3010 ret = strict_strtol(buf, 10, &val);
3011 if (ret < 0)
3012 return ret;
3013
3014 mutex_lock(&ftrace_lock);
3015 if (val < 0) {
3016 /* disable pid tracing */
3017 if (!ftrace_pid_trace)
3018 goto out;
3019
3020 clear_ftrace_pid_task(&ftrace_pid_trace);
3021
3022 } else {
3023 /* swapper task is special */
3024 if (!val) {
3025 pid = ftrace_swapper_pid;
3026 if (pid == ftrace_pid_trace)
3027 goto out;
3028 } else {
3029 pid = find_get_pid(val);
3030
3031 if (pid == ftrace_pid_trace) {
3032 put_pid(pid);
3033 goto out;
3034 }
3035 }
3036
3037 if (ftrace_pid_trace)
3038 clear_ftrace_pid_task(&ftrace_pid_trace);
3039
3040 if (!pid)
3041 goto out;
3042
3043 ftrace_pid_trace = pid;
3044
3045 set_ftrace_pid_task(ftrace_pid_trace);
3046 }
3047
3048 /* update the function call */
3049 ftrace_update_pid_func();
3050 ftrace_startup_enable(0);
3051
3052 out:
3053 mutex_unlock(&ftrace_lock);
3054
3055 return cnt;
3056 }
3057
3058 static const struct file_operations ftrace_pid_fops = {
3059 .read = ftrace_pid_read,
3060 .write = ftrace_pid_write,
3061 };
3062
3063 static __init int ftrace_init_debugfs(void)
3064 {
3065 struct dentry *d_tracer;
3066
3067 d_tracer = tracing_init_dentry();
3068 if (!d_tracer)
3069 return 0;
3070
3071 ftrace_init_dyn_debugfs(d_tracer);
3072
3073 trace_create_file("set_ftrace_pid", 0644, d_tracer,
3074 NULL, &ftrace_pid_fops);
3075
3076 ftrace_profile_debugfs(d_tracer);
3077
3078 return 0;
3079 }
3080 fs_initcall(ftrace_init_debugfs);
3081
3082 /**
3083 * ftrace_kill - kill ftrace
3084 *
3085 * This function should be used by panic code. It stops ftrace
3086 * but in a not so nice way. If you need to simply kill ftrace
3087 * from a non-atomic section, use ftrace_kill.
3088 */
3089 void ftrace_kill(void)
3090 {
3091 ftrace_disabled = 1;
3092 ftrace_enabled = 0;
3093 clear_ftrace_function();
3094 }
3095
3096 /**
3097 * register_ftrace_function - register a function for profiling
3098 * @ops - ops structure that holds the function for profiling.
3099 *
3100 * Register a function to be called by all functions in the
3101 * kernel.
3102 *
3103 * Note: @ops->func and all the functions it calls must be labeled
3104 * with "notrace", otherwise it will go into a
3105 * recursive loop.
3106 */
3107 int register_ftrace_function(struct ftrace_ops *ops)
3108 {
3109 int ret;
3110
3111 if (unlikely(ftrace_disabled))
3112 return -1;
3113
3114 mutex_lock(&ftrace_lock);
3115
3116 ret = __register_ftrace_function(ops);
3117 ftrace_startup(0);
3118
3119 mutex_unlock(&ftrace_lock);
3120 return ret;
3121 }
3122
3123 /**
3124 * unregister_ftrace_function - unregister a function for profiling.
3125 * @ops - ops structure that holds the function to unregister
3126 *
3127 * Unregister a function that was added to be called by ftrace profiling.
3128 */
3129 int unregister_ftrace_function(struct ftrace_ops *ops)
3130 {
3131 int ret;
3132
3133 mutex_lock(&ftrace_lock);
3134 ret = __unregister_ftrace_function(ops);
3135 ftrace_shutdown(0);
3136 mutex_unlock(&ftrace_lock);
3137
3138 return ret;
3139 }
3140
3141 int
3142 ftrace_enable_sysctl(struct ctl_table *table, int write,
3143 struct file *file, void __user *buffer, size_t *lenp,
3144 loff_t *ppos)
3145 {
3146 int ret;
3147
3148 if (unlikely(ftrace_disabled))
3149 return -ENODEV;
3150
3151 mutex_lock(&ftrace_lock);
3152
3153 ret = proc_dointvec(table, write, file, buffer, lenp, ppos);
3154
3155 if (ret || !write || (last_ftrace_enabled == ftrace_enabled))
3156 goto out;
3157
3158 last_ftrace_enabled = ftrace_enabled;
3159
3160 if (ftrace_enabled) {
3161
3162 ftrace_startup_sysctl();
3163
3164 /* we are starting ftrace again */
3165 if (ftrace_list != &ftrace_list_end) {
3166 if (ftrace_list->next == &ftrace_list_end)
3167 ftrace_trace_function = ftrace_list->func;
3168 else
3169 ftrace_trace_function = ftrace_list_func;
3170 }
3171
3172 } else {
3173 /* stopping ftrace calls (just send to ftrace_stub) */
3174 ftrace_trace_function = ftrace_stub;
3175
3176 ftrace_shutdown_sysctl();
3177 }
3178
3179 out:
3180 mutex_unlock(&ftrace_lock);
3181 return ret;
3182 }
3183
3184 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
3185
3186 static int ftrace_graph_active;
3187 static struct notifier_block ftrace_suspend_notifier;
3188
3189 int ftrace_graph_entry_stub(struct ftrace_graph_ent *trace)
3190 {
3191 return 0;
3192 }
3193
3194 /* The callbacks that hook a function */
3195 trace_func_graph_ret_t ftrace_graph_return =
3196 (trace_func_graph_ret_t)ftrace_stub;
3197 trace_func_graph_ent_t ftrace_graph_entry = ftrace_graph_entry_stub;
3198
3199 /* Try to assign a return stack array on FTRACE_RETSTACK_ALLOC_SIZE tasks. */
3200 static int alloc_retstack_tasklist(struct ftrace_ret_stack **ret_stack_list)
3201 {
3202 int i;
3203 int ret = 0;
3204 unsigned long flags;
3205 int start = 0, end = FTRACE_RETSTACK_ALLOC_SIZE;
3206 struct task_struct *g, *t;
3207
3208 for (i = 0; i < FTRACE_RETSTACK_ALLOC_SIZE; i++) {
3209 ret_stack_list[i] = kmalloc(FTRACE_RETFUNC_DEPTH
3210 * sizeof(struct ftrace_ret_stack),
3211 GFP_KERNEL);
3212 if (!ret_stack_list[i]) {
3213 start = 0;
3214 end = i;
3215 ret = -ENOMEM;
3216 goto free;
3217 }
3218 }
3219
3220 read_lock_irqsave(&tasklist_lock, flags);
3221 do_each_thread(g, t) {
3222 if (start == end) {
3223 ret = -EAGAIN;
3224 goto unlock;
3225 }
3226
3227 if (t->ret_stack == NULL) {
3228 atomic_set(&t->tracing_graph_pause, 0);
3229 atomic_set(&t->trace_overrun, 0);
3230 t->curr_ret_stack = -1;
3231 /* Make sure the tasks see the -1 first: */
3232 smp_wmb();
3233 t->ret_stack = ret_stack_list[start++];
3234 }
3235 } while_each_thread(g, t);
3236
3237 unlock:
3238 read_unlock_irqrestore(&tasklist_lock, flags);
3239 free:
3240 for (i = start; i < end; i++)
3241 kfree(ret_stack_list[i]);
3242 return ret;
3243 }
3244
3245 static void
3246 ftrace_graph_probe_sched_switch(struct rq *__rq, struct task_struct *prev,
3247 struct task_struct *next)
3248 {
3249 unsigned long long timestamp;
3250 int index;
3251
3252 /*
3253 * Does the user want to count the time a function was asleep.
3254 * If so, do not update the time stamps.
3255 */
3256 if (trace_flags & TRACE_ITER_SLEEP_TIME)
3257 return;
3258
3259 timestamp = trace_clock_local();
3260
3261 prev->ftrace_timestamp = timestamp;
3262
3263 /* only process tasks that we timestamped */
3264 if (!next->ftrace_timestamp)
3265 return;
3266
3267 /*
3268 * Update all the counters in next to make up for the
3269 * time next was sleeping.
3270 */
3271 timestamp -= next->ftrace_timestamp;
3272
3273 for (index = next->curr_ret_stack; index >= 0; index--)
3274 next->ret_stack[index].calltime += timestamp;
3275 }
3276
3277 /* Allocate a return stack for each task */
3278 static int start_graph_tracing(void)
3279 {
3280 struct ftrace_ret_stack **ret_stack_list;
3281 int ret, cpu;
3282
3283 ret_stack_list = kmalloc(FTRACE_RETSTACK_ALLOC_SIZE *
3284 sizeof(struct ftrace_ret_stack *),
3285 GFP_KERNEL);
3286
3287 if (!ret_stack_list)
3288 return -ENOMEM;
3289
3290 /* The cpu_boot init_task->ret_stack will never be freed */
3291 for_each_online_cpu(cpu) {
3292 if (!idle_task(cpu)->ret_stack)
3293 ftrace_graph_init_task(idle_task(cpu));
3294 }
3295
3296 do {
3297 ret = alloc_retstack_tasklist(ret_stack_list);
3298 } while (ret == -EAGAIN);
3299
3300 if (!ret) {
3301 ret = register_trace_sched_switch(ftrace_graph_probe_sched_switch);
3302 if (ret)
3303 pr_info("ftrace_graph: Couldn't activate tracepoint"
3304 " probe to kernel_sched_switch\n");
3305 }
3306
3307 kfree(ret_stack_list);
3308 return ret;
3309 }
3310
3311 /*
3312 * Hibernation protection.
3313 * The state of the current task is too much unstable during
3314 * suspend/restore to disk. We want to protect against that.
3315 */
3316 static int
3317 ftrace_suspend_notifier_call(struct notifier_block *bl, unsigned long state,
3318 void *unused)
3319 {
3320 switch (state) {
3321 case PM_HIBERNATION_PREPARE:
3322 pause_graph_tracing();
3323 break;
3324
3325 case PM_POST_HIBERNATION:
3326 unpause_graph_tracing();
3327 break;
3328 }
3329 return NOTIFY_DONE;
3330 }
3331
3332 int register_ftrace_graph(trace_func_graph_ret_t retfunc,
3333 trace_func_graph_ent_t entryfunc)
3334 {
3335 int ret = 0;
3336
3337 mutex_lock(&ftrace_lock);
3338
3339 /* we currently allow only one tracer registered at a time */
3340 if (ftrace_graph_active) {
3341 ret = -EBUSY;
3342 goto out;
3343 }
3344
3345 ftrace_suspend_notifier.notifier_call = ftrace_suspend_notifier_call;
3346 register_pm_notifier(&ftrace_suspend_notifier);
3347
3348 ftrace_graph_active++;
3349 ret = start_graph_tracing();
3350 if (ret) {
3351 ftrace_graph_active--;
3352 goto out;
3353 }
3354
3355 ftrace_graph_return = retfunc;
3356 ftrace_graph_entry = entryfunc;
3357
3358 ftrace_startup(FTRACE_START_FUNC_RET);
3359
3360 out:
3361 mutex_unlock(&ftrace_lock);
3362 return ret;
3363 }
3364
3365 void unregister_ftrace_graph(void)
3366 {
3367 mutex_lock(&ftrace_lock);
3368
3369 if (unlikely(!ftrace_graph_active))
3370 goto out;
3371
3372 ftrace_graph_active--;
3373 unregister_trace_sched_switch(ftrace_graph_probe_sched_switch);
3374 ftrace_graph_return = (trace_func_graph_ret_t)ftrace_stub;
3375 ftrace_graph_entry = ftrace_graph_entry_stub;
3376 ftrace_shutdown(FTRACE_STOP_FUNC_RET);
3377 unregister_pm_notifier(&ftrace_suspend_notifier);
3378
3379 out:
3380 mutex_unlock(&ftrace_lock);
3381 }
3382
3383 /* Allocate a return stack for newly created task */
3384 void ftrace_graph_init_task(struct task_struct *t)
3385 {
3386 /* Make sure we do not use the parent ret_stack */
3387 t->ret_stack = NULL;
3388
3389 if (ftrace_graph_active) {
3390 struct ftrace_ret_stack *ret_stack;
3391
3392 ret_stack = kmalloc(FTRACE_RETFUNC_DEPTH
3393 * sizeof(struct ftrace_ret_stack),
3394 GFP_KERNEL);
3395 if (!ret_stack)
3396 return;
3397 t->curr_ret_stack = -1;
3398 atomic_set(&t->tracing_graph_pause, 0);
3399 atomic_set(&t->trace_overrun, 0);
3400 t->ftrace_timestamp = 0;
3401 /* make curr_ret_stack visable before we add the ret_stack */
3402 smp_wmb();
3403 t->ret_stack = ret_stack;
3404 }
3405 }
3406
3407 void ftrace_graph_exit_task(struct task_struct *t)
3408 {
3409 struct ftrace_ret_stack *ret_stack = t->ret_stack;
3410
3411 t->ret_stack = NULL;
3412 /* NULL must become visible to IRQs before we free it: */
3413 barrier();
3414
3415 kfree(ret_stack);
3416 }
3417
3418 void ftrace_graph_stop(void)
3419 {
3420 ftrace_stop();
3421 }
3422 #endif
3423