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