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