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