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