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