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