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