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