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