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