Merge branch 'linus' into tracing/core
[linux-2.6.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 flag = 0UL;
1020
1021         ftrace_addr = (unsigned long)FTRACE_ADDR;
1022
1023         /*
1024          * If this record is not to be traced or we want to disable it,
1025          * then disable it.
1026          *
1027          * If we want to enable it and filtering is off, then enable it.
1028          *
1029          * If we want to enable it and filtering is on, enable it only if
1030          * it's filtered
1031          */
1032         if (enable && !(rec->flags & FTRACE_FL_NOTRACE)) {
1033                 if (!ftrace_filtered || (rec->flags & FTRACE_FL_FILTER))
1034                         flag = FTRACE_FL_ENABLED;
1035         }
1036
1037         /* If the state of this record hasn't changed, then do nothing */
1038         if ((rec->flags & FTRACE_FL_ENABLED) == flag)
1039                 return 0;
1040
1041         if (flag) {
1042                 rec->flags |= FTRACE_FL_ENABLED;
1043                 return ftrace_make_call(rec, ftrace_addr);
1044         }
1045
1046         rec->flags &= ~FTRACE_FL_ENABLED;
1047         return ftrace_make_nop(NULL, rec, ftrace_addr);
1048 }
1049
1050 static void ftrace_replace_code(int enable)
1051 {
1052         struct dyn_ftrace *rec;
1053         struct ftrace_page *pg;
1054         int failed;
1055
1056         do_for_each_ftrace_rec(pg, rec) {
1057                 /*
1058                  * Skip over free records, records that have
1059                  * failed and not converted.
1060                  */
1061                 if (rec->flags & FTRACE_FL_FREE ||
1062                     rec->flags & FTRACE_FL_FAILED ||
1063                     !(rec->flags & FTRACE_FL_CONVERTED))
1064                         continue;
1065
1066                 /* ignore updates to this record's mcount site */
1067                 if (get_kprobe((void *)rec->ip)) {
1068                         freeze_record(rec);
1069                         continue;
1070                 } else {
1071                         unfreeze_record(rec);
1072                 }
1073
1074                 failed = __ftrace_replace_code(rec, enable);
1075                 if (failed) {
1076                         rec->flags |= FTRACE_FL_FAILED;
1077                         if ((system_state == SYSTEM_BOOTING) ||
1078                             !core_kernel_text(rec->ip)) {
1079                                 ftrace_free_rec(rec);
1080                                 } else {
1081                                 ftrace_bug(failed, rec->ip);
1082                                         /* Stop processing */
1083                                         return;
1084                                 }
1085                 }
1086         } while_for_each_ftrace_rec();
1087 }
1088
1089 static int
1090 ftrace_code_disable(struct module *mod, struct dyn_ftrace *rec)
1091 {
1092         unsigned long ip;
1093         int ret;
1094
1095         ip = rec->ip;
1096
1097         ret = ftrace_make_nop(mod, rec, MCOUNT_ADDR);
1098         if (ret) {
1099                 ftrace_bug(ret, ip);
1100                 rec->flags |= FTRACE_FL_FAILED;
1101                 return 0;
1102         }
1103         return 1;
1104 }
1105
1106 /*
1107  * archs can override this function if they must do something
1108  * before the modifying code is performed.
1109  */
1110 int __weak ftrace_arch_code_modify_prepare(void)
1111 {
1112         return 0;
1113 }
1114
1115 /*
1116  * archs can override this function if they must do something
1117  * after the modifying code is performed.
1118  */
1119 int __weak ftrace_arch_code_modify_post_process(void)
1120 {
1121         return 0;
1122 }
1123
1124 static int __ftrace_modify_code(void *data)
1125 {
1126         int *command = data;
1127
1128         if (*command & FTRACE_ENABLE_CALLS)
1129                 ftrace_replace_code(1);
1130         else if (*command & FTRACE_DISABLE_CALLS)
1131                 ftrace_replace_code(0);
1132
1133         if (*command & FTRACE_UPDATE_TRACE_FUNC)
1134                 ftrace_update_ftrace_func(ftrace_trace_function);
1135
1136         if (*command & FTRACE_START_FUNC_RET)
1137                 ftrace_enable_ftrace_graph_caller();
1138         else if (*command & FTRACE_STOP_FUNC_RET)
1139                 ftrace_disable_ftrace_graph_caller();
1140
1141         return 0;
1142 }
1143
1144 static void ftrace_run_update_code(int command)
1145 {
1146         int ret;
1147
1148         ret = ftrace_arch_code_modify_prepare();
1149         FTRACE_WARN_ON(ret);
1150         if (ret)
1151                 return;
1152
1153         stop_machine(__ftrace_modify_code, &command, NULL);
1154
1155         ret = ftrace_arch_code_modify_post_process();
1156         FTRACE_WARN_ON(ret);
1157 }
1158
1159 static ftrace_func_t saved_ftrace_func;
1160 static int ftrace_start_up;
1161
1162 static void ftrace_startup_enable(int command)
1163 {
1164         if (saved_ftrace_func != ftrace_trace_function) {
1165                 saved_ftrace_func = ftrace_trace_function;
1166                 command |= FTRACE_UPDATE_TRACE_FUNC;
1167         }
1168
1169         if (!command || !ftrace_enabled)
1170                 return;
1171
1172         ftrace_run_update_code(command);
1173 }
1174
1175 static void ftrace_startup(int command)
1176 {
1177         if (unlikely(ftrace_disabled))
1178                 return;
1179
1180         ftrace_start_up++;
1181         command |= FTRACE_ENABLE_CALLS;
1182
1183         ftrace_startup_enable(command);
1184 }
1185
1186 static void ftrace_shutdown(int command)
1187 {
1188         if (unlikely(ftrace_disabled))
1189                 return;
1190
1191         ftrace_start_up--;
1192         /*
1193          * Just warn in case of unbalance, no need to kill ftrace, it's not
1194          * critical but the ftrace_call callers may be never nopped again after
1195          * further ftrace uses.
1196          */
1197         WARN_ON_ONCE(ftrace_start_up < 0);
1198
1199         if (!ftrace_start_up)
1200                 command |= FTRACE_DISABLE_CALLS;
1201
1202         if (saved_ftrace_func != ftrace_trace_function) {
1203                 saved_ftrace_func = ftrace_trace_function;
1204                 command |= FTRACE_UPDATE_TRACE_FUNC;
1205         }
1206
1207         if (!command || !ftrace_enabled)
1208                 return;
1209
1210         ftrace_run_update_code(command);
1211 }
1212
1213 static void ftrace_startup_sysctl(void)
1214 {
1215         int command = FTRACE_ENABLE_MCOUNT;
1216
1217         if (unlikely(ftrace_disabled))
1218                 return;
1219
1220         /* Force update next time */
1221         saved_ftrace_func = NULL;
1222         /* ftrace_start_up is true if we want ftrace running */
1223         if (ftrace_start_up)
1224                 command |= FTRACE_ENABLE_CALLS;
1225
1226         ftrace_run_update_code(command);
1227 }
1228
1229 static void ftrace_shutdown_sysctl(void)
1230 {
1231         int command = FTRACE_DISABLE_MCOUNT;
1232
1233         if (unlikely(ftrace_disabled))
1234                 return;
1235
1236         /* ftrace_start_up is true if ftrace is running */
1237         if (ftrace_start_up)
1238                 command |= FTRACE_DISABLE_CALLS;
1239
1240         ftrace_run_update_code(command);
1241 }
1242
1243 static cycle_t          ftrace_update_time;
1244 static unsigned long    ftrace_update_cnt;
1245 unsigned long           ftrace_update_tot_cnt;
1246
1247 static int ftrace_update_code(struct module *mod)
1248 {
1249         struct dyn_ftrace *p;
1250         cycle_t start, stop;
1251
1252         start = ftrace_now(raw_smp_processor_id());
1253         ftrace_update_cnt = 0;
1254
1255         while (ftrace_new_addrs) {
1256
1257                 /* If something went wrong, bail without enabling anything */
1258                 if (unlikely(ftrace_disabled))
1259                         return -1;
1260
1261                 p = ftrace_new_addrs;
1262                 ftrace_new_addrs = p->newlist;
1263                 p->flags = 0L;
1264
1265                 /* convert record (i.e, patch mcount-call with NOP) */
1266                 if (ftrace_code_disable(mod, p)) {
1267                         p->flags |= FTRACE_FL_CONVERTED;
1268                         ftrace_update_cnt++;
1269                 } else
1270                         ftrace_free_rec(p);
1271         }
1272
1273         stop = ftrace_now(raw_smp_processor_id());
1274         ftrace_update_time = stop - start;
1275         ftrace_update_tot_cnt += ftrace_update_cnt;
1276
1277         return 0;
1278 }
1279
1280 static int __init ftrace_dyn_table_alloc(unsigned long num_to_init)
1281 {
1282         struct ftrace_page *pg;
1283         int cnt;
1284         int i;
1285
1286         /* allocate a few pages */
1287         ftrace_pages_start = (void *)get_zeroed_page(GFP_KERNEL);
1288         if (!ftrace_pages_start)
1289                 return -1;
1290
1291         /*
1292          * Allocate a few more pages.
1293          *
1294          * TODO: have some parser search vmlinux before
1295          *   final linking to find all calls to ftrace.
1296          *   Then we can:
1297          *    a) know how many pages to allocate.
1298          *     and/or
1299          *    b) set up the table then.
1300          *
1301          *  The dynamic code is still necessary for
1302          *  modules.
1303          */
1304
1305         pg = ftrace_pages = ftrace_pages_start;
1306
1307         cnt = num_to_init / ENTRIES_PER_PAGE;
1308         pr_info("ftrace: allocating %ld entries in %d pages\n",
1309                 num_to_init, cnt + 1);
1310
1311         for (i = 0; i < cnt; i++) {
1312                 pg->next = (void *)get_zeroed_page(GFP_KERNEL);
1313
1314                 /* If we fail, we'll try later anyway */
1315                 if (!pg->next)
1316                         break;
1317
1318                 pg = pg->next;
1319         }
1320
1321         return 0;
1322 }
1323
1324 enum {
1325         FTRACE_ITER_FILTER      = (1 << 0),
1326         FTRACE_ITER_CONT        = (1 << 1),
1327         FTRACE_ITER_NOTRACE     = (1 << 2),
1328         FTRACE_ITER_FAILURES    = (1 << 3),
1329         FTRACE_ITER_PRINTALL    = (1 << 4),
1330         FTRACE_ITER_HASH        = (1 << 5),
1331 };
1332
1333 #define FTRACE_BUFF_MAX (KSYM_SYMBOL_LEN+4) /* room for wildcards */
1334
1335 struct ftrace_iterator {
1336         struct ftrace_page      *pg;
1337         int                     hidx;
1338         int                     idx;
1339         unsigned                flags;
1340         unsigned char           buffer[FTRACE_BUFF_MAX+1];
1341         unsigned                buffer_idx;
1342 };
1343
1344 static void *
1345 t_hash_next(struct seq_file *m, void *v, loff_t *pos)
1346 {
1347         struct ftrace_iterator *iter = m->private;
1348         struct hlist_node *hnd = v;
1349         struct hlist_head *hhd;
1350
1351         WARN_ON(!(iter->flags & FTRACE_ITER_HASH));
1352
1353         (*pos)++;
1354
1355  retry:
1356         if (iter->hidx >= FTRACE_FUNC_HASHSIZE)
1357                 return NULL;
1358
1359         hhd = &ftrace_func_hash[iter->hidx];
1360
1361         if (hlist_empty(hhd)) {
1362                 iter->hidx++;
1363                 hnd = NULL;
1364                 goto retry;
1365         }
1366
1367         if (!hnd)
1368                 hnd = hhd->first;
1369         else {
1370                 hnd = hnd->next;
1371                 if (!hnd) {
1372                         iter->hidx++;
1373                         goto retry;
1374                 }
1375         }
1376
1377         return hnd;
1378 }
1379
1380 static void *t_hash_start(struct seq_file *m, loff_t *pos)
1381 {
1382         struct ftrace_iterator *iter = m->private;
1383         void *p = NULL;
1384         loff_t l;
1385
1386         if (!(iter->flags & FTRACE_ITER_HASH))
1387                 *pos = 0;
1388
1389         iter->flags |= FTRACE_ITER_HASH;
1390
1391         iter->hidx = 0;
1392         for (l = 0; l <= *pos; ) {
1393                 p = t_hash_next(m, p, &l);
1394                 if (!p)
1395                         break;
1396         }
1397         return p;
1398 }
1399
1400 static int t_hash_show(struct seq_file *m, void *v)
1401 {
1402         struct ftrace_func_probe *rec;
1403         struct hlist_node *hnd = v;
1404
1405         rec = hlist_entry(hnd, struct ftrace_func_probe, node);
1406
1407         if (rec->ops->print)
1408                 return rec->ops->print(m, rec->ip, rec->ops, rec->data);
1409
1410         seq_printf(m, "%pf:%pf", (void *)rec->ip, (void *)rec->ops->func);
1411
1412         if (rec->data)
1413                 seq_printf(m, ":%p", rec->data);
1414         seq_putc(m, '\n');
1415
1416         return 0;
1417 }
1418
1419 static void *
1420 t_next(struct seq_file *m, void *v, loff_t *pos)
1421 {
1422         struct ftrace_iterator *iter = m->private;
1423         struct dyn_ftrace *rec = NULL;
1424
1425         if (iter->flags & FTRACE_ITER_HASH)
1426                 return t_hash_next(m, v, pos);
1427
1428         (*pos)++;
1429
1430         if (iter->flags & FTRACE_ITER_PRINTALL)
1431                 return NULL;
1432
1433  retry:
1434         if (iter->idx >= iter->pg->index) {
1435                 if (iter->pg->next) {
1436                         iter->pg = iter->pg->next;
1437                         iter->idx = 0;
1438                         goto retry;
1439                 }
1440         } else {
1441                 rec = &iter->pg->records[iter->idx++];
1442                 if ((rec->flags & FTRACE_FL_FREE) ||
1443
1444                     (!(iter->flags & FTRACE_ITER_FAILURES) &&
1445                      (rec->flags & FTRACE_FL_FAILED)) ||
1446
1447                     ((iter->flags & FTRACE_ITER_FAILURES) &&
1448                      !(rec->flags & FTRACE_FL_FAILED)) ||
1449
1450                     ((iter->flags & FTRACE_ITER_FILTER) &&
1451                      !(rec->flags & FTRACE_FL_FILTER)) ||
1452
1453                     ((iter->flags & FTRACE_ITER_NOTRACE) &&
1454                      !(rec->flags & FTRACE_FL_NOTRACE))) {
1455                         rec = NULL;
1456                         goto retry;
1457                 }
1458         }
1459
1460         return rec;
1461 }
1462
1463 static void *t_start(struct seq_file *m, loff_t *pos)
1464 {
1465         struct ftrace_iterator *iter = m->private;
1466         void *p = NULL;
1467         loff_t l;
1468
1469         mutex_lock(&ftrace_lock);
1470         /*
1471          * For set_ftrace_filter reading, if we have the filter
1472          * off, we can short cut and just print out that all
1473          * functions are enabled.
1474          */
1475         if (iter->flags & FTRACE_ITER_FILTER && !ftrace_filtered) {
1476                 if (*pos > 0)
1477                         return t_hash_start(m, pos);
1478                 iter->flags |= FTRACE_ITER_PRINTALL;
1479                 return iter;
1480         }
1481
1482         if (iter->flags & FTRACE_ITER_HASH)
1483                 return t_hash_start(m, pos);
1484
1485         iter->pg = ftrace_pages_start;
1486         iter->idx = 0;
1487         for (l = 0; l <= *pos; ) {
1488                 p = t_next(m, p, &l);
1489                 if (!p)
1490                         break;
1491         }
1492
1493         if (!p && iter->flags & FTRACE_ITER_FILTER)
1494                 return t_hash_start(m, pos);
1495
1496         return p;
1497 }
1498
1499 static void t_stop(struct seq_file *m, void *p)
1500 {
1501         mutex_unlock(&ftrace_lock);
1502 }
1503
1504 static int t_show(struct seq_file *m, void *v)
1505 {
1506         struct ftrace_iterator *iter = m->private;
1507         struct dyn_ftrace *rec = v;
1508
1509         if (iter->flags & FTRACE_ITER_HASH)
1510                 return t_hash_show(m, v);
1511
1512         if (iter->flags & FTRACE_ITER_PRINTALL) {
1513                 seq_printf(m, "#### all functions enabled ####\n");
1514                 return 0;
1515         }
1516
1517         if (!rec)
1518                 return 0;
1519
1520         seq_printf(m, "%pf\n", (void *)rec->ip);
1521
1522         return 0;
1523 }
1524
1525 static struct seq_operations show_ftrace_seq_ops = {
1526         .start = t_start,
1527         .next = t_next,
1528         .stop = t_stop,
1529         .show = t_show,
1530 };
1531
1532 static int
1533 ftrace_avail_open(struct inode *inode, struct file *file)
1534 {
1535         struct ftrace_iterator *iter;
1536         int ret;
1537
1538         if (unlikely(ftrace_disabled))
1539                 return -ENODEV;
1540
1541         iter = kzalloc(sizeof(*iter), GFP_KERNEL);
1542         if (!iter)
1543                 return -ENOMEM;
1544
1545         iter->pg = ftrace_pages_start;
1546
1547         ret = seq_open(file, &show_ftrace_seq_ops);
1548         if (!ret) {
1549                 struct seq_file *m = file->private_data;
1550
1551                 m->private = iter;
1552         } else {
1553                 kfree(iter);
1554         }
1555
1556         return ret;
1557 }
1558
1559 int ftrace_avail_release(struct inode *inode, struct file *file)
1560 {
1561         struct seq_file *m = (struct seq_file *)file->private_data;
1562         struct ftrace_iterator *iter = m->private;
1563
1564         seq_release(inode, file);
1565         kfree(iter);
1566
1567         return 0;
1568 }
1569
1570 static int
1571 ftrace_failures_open(struct inode *inode, struct file *file)
1572 {
1573         int ret;
1574         struct seq_file *m;
1575         struct ftrace_iterator *iter;
1576
1577         ret = ftrace_avail_open(inode, file);
1578         if (!ret) {
1579                 m = (struct seq_file *)file->private_data;
1580                 iter = (struct ftrace_iterator *)m->private;
1581                 iter->flags = FTRACE_ITER_FAILURES;
1582         }
1583
1584         return ret;
1585 }
1586
1587
1588 static void ftrace_filter_reset(int enable)
1589 {
1590         struct ftrace_page *pg;
1591         struct dyn_ftrace *rec;
1592         unsigned long type = enable ? FTRACE_FL_FILTER : FTRACE_FL_NOTRACE;
1593
1594         mutex_lock(&ftrace_lock);
1595         if (enable)
1596                 ftrace_filtered = 0;
1597         do_for_each_ftrace_rec(pg, rec) {
1598                 if (rec->flags & FTRACE_FL_FAILED)
1599                         continue;
1600                 rec->flags &= ~type;
1601         } while_for_each_ftrace_rec();
1602         mutex_unlock(&ftrace_lock);
1603 }
1604
1605 static int
1606 ftrace_regex_open(struct inode *inode, struct file *file, int enable)
1607 {
1608         struct ftrace_iterator *iter;
1609         int ret = 0;
1610
1611         if (unlikely(ftrace_disabled))
1612                 return -ENODEV;
1613
1614         iter = kzalloc(sizeof(*iter), GFP_KERNEL);
1615         if (!iter)
1616                 return -ENOMEM;
1617
1618         mutex_lock(&ftrace_regex_lock);
1619         if ((file->f_mode & FMODE_WRITE) &&
1620             (file->f_flags & O_TRUNC))
1621                 ftrace_filter_reset(enable);
1622
1623         if (file->f_mode & FMODE_READ) {
1624                 iter->pg = ftrace_pages_start;
1625                 iter->flags = enable ? FTRACE_ITER_FILTER :
1626                         FTRACE_ITER_NOTRACE;
1627
1628                 ret = seq_open(file, &show_ftrace_seq_ops);
1629                 if (!ret) {
1630                         struct seq_file *m = file->private_data;
1631                         m->private = iter;
1632                 } else
1633                         kfree(iter);
1634         } else
1635                 file->private_data = iter;
1636         mutex_unlock(&ftrace_regex_lock);
1637
1638         return ret;
1639 }
1640
1641 static int
1642 ftrace_filter_open(struct inode *inode, struct file *file)
1643 {
1644         return ftrace_regex_open(inode, file, 1);
1645 }
1646
1647 static int
1648 ftrace_notrace_open(struct inode *inode, struct file *file)
1649 {
1650         return ftrace_regex_open(inode, file, 0);
1651 }
1652
1653 static loff_t
1654 ftrace_regex_lseek(struct file *file, loff_t offset, int origin)
1655 {
1656         loff_t ret;
1657
1658         if (file->f_mode & FMODE_READ)
1659                 ret = seq_lseek(file, offset, origin);
1660         else
1661                 file->f_pos = ret = 1;
1662
1663         return ret;
1664 }
1665
1666 enum {
1667         MATCH_FULL,
1668         MATCH_FRONT_ONLY,
1669         MATCH_MIDDLE_ONLY,
1670         MATCH_END_ONLY,
1671 };
1672
1673 /*
1674  * (static function - no need for kernel doc)
1675  *
1676  * Pass in a buffer containing a glob and this function will
1677  * set search to point to the search part of the buffer and
1678  * return the type of search it is (see enum above).
1679  * This does modify buff.
1680  *
1681  * Returns enum type.
1682  *  search returns the pointer to use for comparison.
1683  *  not returns 1 if buff started with a '!'
1684  *     0 otherwise.
1685  */
1686 static int
1687 ftrace_setup_glob(char *buff, int len, char **search, int *not)
1688 {
1689         int type = MATCH_FULL;
1690         int i;
1691
1692         if (buff[0] == '!') {
1693                 *not = 1;
1694                 buff++;
1695                 len--;
1696         } else
1697                 *not = 0;
1698
1699         *search = buff;
1700
1701         for (i = 0; i < len; i++) {
1702                 if (buff[i] == '*') {
1703                         if (!i) {
1704                                 *search = buff + 1;
1705                                 type = MATCH_END_ONLY;
1706                         } else {
1707                                 if (type == MATCH_END_ONLY)
1708                                         type = MATCH_MIDDLE_ONLY;
1709                                 else
1710                                         type = MATCH_FRONT_ONLY;
1711                                 buff[i] = 0;
1712                                 break;
1713                         }
1714                 }
1715         }
1716
1717         return type;
1718 }
1719
1720 static int ftrace_match(char *str, char *regex, int len, int type)
1721 {
1722         int matched = 0;
1723         char *ptr;
1724
1725         switch (type) {
1726         case MATCH_FULL:
1727                 if (strcmp(str, regex) == 0)
1728                         matched = 1;
1729                 break;
1730         case MATCH_FRONT_ONLY:
1731                 if (strncmp(str, regex, len) == 0)
1732                         matched = 1;
1733                 break;
1734         case MATCH_MIDDLE_ONLY:
1735                 if (strstr(str, regex))
1736                         matched = 1;
1737                 break;
1738         case MATCH_END_ONLY:
1739                 ptr = strstr(str, regex);
1740                 if (ptr && (ptr[len] == 0))
1741                         matched = 1;
1742                 break;
1743         }
1744
1745         return matched;
1746 }
1747
1748 static int
1749 ftrace_match_record(struct dyn_ftrace *rec, char *regex, int len, int type)
1750 {
1751         char str[KSYM_SYMBOL_LEN];
1752
1753         kallsyms_lookup(rec->ip, NULL, NULL, NULL, str);
1754         return ftrace_match(str, regex, len, type);
1755 }
1756
1757 static void ftrace_match_records(char *buff, int len, int enable)
1758 {
1759         unsigned int search_len;
1760         struct ftrace_page *pg;
1761         struct dyn_ftrace *rec;
1762         unsigned long flag;
1763         char *search;
1764         int type;
1765         int not;
1766
1767         flag = enable ? FTRACE_FL_FILTER : FTRACE_FL_NOTRACE;
1768         type = ftrace_setup_glob(buff, len, &search, &not);
1769
1770         search_len = strlen(search);
1771
1772         mutex_lock(&ftrace_lock);
1773         do_for_each_ftrace_rec(pg, rec) {
1774
1775                 if (rec->flags & FTRACE_FL_FAILED)
1776                         continue;
1777
1778                 if (ftrace_match_record(rec, search, search_len, type)) {
1779                         if (not)
1780                                 rec->flags &= ~flag;
1781                         else
1782                                 rec->flags |= flag;
1783                 }
1784                 /*
1785                  * Only enable filtering if we have a function that
1786                  * is filtered on.
1787                  */
1788                 if (enable && (rec->flags & FTRACE_FL_FILTER))
1789                         ftrace_filtered = 1;
1790         } while_for_each_ftrace_rec();
1791         mutex_unlock(&ftrace_lock);
1792 }
1793
1794 static int
1795 ftrace_match_module_record(struct dyn_ftrace *rec, char *mod,
1796                            char *regex, int len, int type)
1797 {
1798         char str[KSYM_SYMBOL_LEN];
1799         char *modname;
1800
1801         kallsyms_lookup(rec->ip, NULL, NULL, &modname, str);
1802
1803         if (!modname || strcmp(modname, mod))
1804                 return 0;
1805
1806         /* blank search means to match all funcs in the mod */
1807         if (len)
1808                 return ftrace_match(str, regex, len, type);
1809         else
1810                 return 1;
1811 }
1812
1813 static void ftrace_match_module_records(char *buff, char *mod, int enable)
1814 {
1815         unsigned search_len = 0;
1816         struct ftrace_page *pg;
1817         struct dyn_ftrace *rec;
1818         int type = MATCH_FULL;
1819         char *search = buff;
1820         unsigned long flag;
1821         int not = 0;
1822
1823         flag = enable ? FTRACE_FL_FILTER : FTRACE_FL_NOTRACE;
1824
1825         /* blank or '*' mean the same */
1826         if (strcmp(buff, "*") == 0)
1827                 buff[0] = 0;
1828
1829         /* handle the case of 'dont filter this module' */
1830         if (strcmp(buff, "!") == 0 || strcmp(buff, "!*") == 0) {
1831                 buff[0] = 0;
1832                 not = 1;
1833         }
1834
1835         if (strlen(buff)) {
1836                 type = ftrace_setup_glob(buff, strlen(buff), &search, &not);
1837                 search_len = strlen(search);
1838         }
1839
1840         mutex_lock(&ftrace_lock);
1841         do_for_each_ftrace_rec(pg, rec) {
1842
1843                 if (rec->flags & FTRACE_FL_FAILED)
1844                         continue;
1845
1846                 if (ftrace_match_module_record(rec, mod,
1847                                                search, search_len, type)) {
1848                         if (not)
1849                                 rec->flags &= ~flag;
1850                         else
1851                                 rec->flags |= flag;
1852                 }
1853                 if (enable && (rec->flags & FTRACE_FL_FILTER))
1854                         ftrace_filtered = 1;
1855
1856         } while_for_each_ftrace_rec();
1857         mutex_unlock(&ftrace_lock);
1858 }
1859
1860 /*
1861  * We register the module command as a template to show others how
1862  * to register the a command as well.
1863  */
1864
1865 static int
1866 ftrace_mod_callback(char *func, char *cmd, char *param, int enable)
1867 {
1868         char *mod;
1869
1870         /*
1871          * cmd == 'mod' because we only registered this func
1872          * for the 'mod' ftrace_func_command.
1873          * But if you register one func with multiple commands,
1874          * you can tell which command was used by the cmd
1875          * parameter.
1876          */
1877
1878         /* we must have a module name */
1879         if (!param)
1880                 return -EINVAL;
1881
1882         mod = strsep(&param, ":");
1883         if (!strlen(mod))
1884                 return -EINVAL;
1885
1886         ftrace_match_module_records(func, mod, enable);
1887         return 0;
1888 }
1889
1890 static struct ftrace_func_command ftrace_mod_cmd = {
1891         .name                   = "mod",
1892         .func                   = ftrace_mod_callback,
1893 };
1894
1895 static int __init ftrace_mod_cmd_init(void)
1896 {
1897         return register_ftrace_command(&ftrace_mod_cmd);
1898 }
1899 device_initcall(ftrace_mod_cmd_init);
1900
1901 static void
1902 function_trace_probe_call(unsigned long ip, unsigned long parent_ip)
1903 {
1904         struct ftrace_func_probe *entry;
1905         struct hlist_head *hhd;
1906         struct hlist_node *n;
1907         unsigned long key;
1908         int resched;
1909
1910         key = hash_long(ip, FTRACE_HASH_BITS);
1911
1912         hhd = &ftrace_func_hash[key];
1913
1914         if (hlist_empty(hhd))
1915                 return;
1916
1917         /*
1918          * Disable preemption for these calls to prevent a RCU grace
1919          * period. This syncs the hash iteration and freeing of items
1920          * on the hash. rcu_read_lock is too dangerous here.
1921          */
1922         resched = ftrace_preempt_disable();
1923         hlist_for_each_entry_rcu(entry, n, hhd, node) {
1924                 if (entry->ip == ip)
1925                         entry->ops->func(ip, parent_ip, &entry->data);
1926         }
1927         ftrace_preempt_enable(resched);
1928 }
1929
1930 static struct ftrace_ops trace_probe_ops __read_mostly =
1931 {
1932         .func           = function_trace_probe_call,
1933 };
1934
1935 static int ftrace_probe_registered;
1936
1937 static void __enable_ftrace_function_probe(void)
1938 {
1939         int i;
1940
1941         if (ftrace_probe_registered)
1942                 return;
1943
1944         for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
1945                 struct hlist_head *hhd = &ftrace_func_hash[i];
1946                 if (hhd->first)
1947                         break;
1948         }
1949         /* Nothing registered? */
1950         if (i == FTRACE_FUNC_HASHSIZE)
1951                 return;
1952
1953         __register_ftrace_function(&trace_probe_ops);
1954         ftrace_startup(0);
1955         ftrace_probe_registered = 1;
1956 }
1957
1958 static void __disable_ftrace_function_probe(void)
1959 {
1960         int i;
1961
1962         if (!ftrace_probe_registered)
1963                 return;
1964
1965         for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
1966                 struct hlist_head *hhd = &ftrace_func_hash[i];
1967                 if (hhd->first)
1968                         return;
1969         }
1970
1971         /* no more funcs left */
1972         __unregister_ftrace_function(&trace_probe_ops);
1973         ftrace_shutdown(0);
1974         ftrace_probe_registered = 0;
1975 }
1976
1977
1978 static void ftrace_free_entry_rcu(struct rcu_head *rhp)
1979 {
1980         struct ftrace_func_probe *entry =
1981                 container_of(rhp, struct ftrace_func_probe, rcu);
1982
1983         if (entry->ops->free)
1984                 entry->ops->free(&entry->data);
1985         kfree(entry);
1986 }
1987
1988
1989 int
1990 register_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
1991                               void *data)
1992 {
1993         struct ftrace_func_probe *entry;
1994         struct ftrace_page *pg;
1995         struct dyn_ftrace *rec;
1996         int type, len, not;
1997         unsigned long key;
1998         int count = 0;
1999         char *search;
2000
2001         type = ftrace_setup_glob(glob, strlen(glob), &search, &not);
2002         len = strlen(search);
2003
2004         /* we do not support '!' for function probes */
2005         if (WARN_ON(not))
2006                 return -EINVAL;
2007
2008         mutex_lock(&ftrace_lock);
2009         do_for_each_ftrace_rec(pg, rec) {
2010
2011                 if (rec->flags & FTRACE_FL_FAILED)
2012                         continue;
2013
2014                 if (!ftrace_match_record(rec, search, len, type))
2015                         continue;
2016
2017                 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
2018                 if (!entry) {
2019                         /* If we did not process any, then return error */
2020                         if (!count)
2021                                 count = -ENOMEM;
2022                         goto out_unlock;
2023                 }
2024
2025                 count++;
2026
2027                 entry->data = data;
2028
2029                 /*
2030                  * The caller might want to do something special
2031                  * for each function we find. We call the callback
2032                  * to give the caller an opportunity to do so.
2033                  */
2034                 if (ops->callback) {
2035                         if (ops->callback(rec->ip, &entry->data) < 0) {
2036                                 /* caller does not like this func */
2037                                 kfree(entry);
2038                                 continue;
2039                         }
2040                 }
2041
2042                 entry->ops = ops;
2043                 entry->ip = rec->ip;
2044
2045                 key = hash_long(entry->ip, FTRACE_HASH_BITS);
2046                 hlist_add_head_rcu(&entry->node, &ftrace_func_hash[key]);
2047
2048         } while_for_each_ftrace_rec();
2049         __enable_ftrace_function_probe();
2050
2051  out_unlock:
2052         mutex_unlock(&ftrace_lock);
2053
2054         return count;
2055 }
2056
2057 enum {
2058         PROBE_TEST_FUNC         = 1,
2059         PROBE_TEST_DATA         = 2
2060 };
2061
2062 static void
2063 __unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
2064                                   void *data, int flags)
2065 {
2066         struct ftrace_func_probe *entry;
2067         struct hlist_node *n, *tmp;
2068         char str[KSYM_SYMBOL_LEN];
2069         int type = MATCH_FULL;
2070         int i, len = 0;
2071         char *search;
2072
2073         if (glob && (strcmp(glob, "*") || !strlen(glob)))
2074                 glob = NULL;
2075         else {
2076                 int not;
2077
2078                 type = ftrace_setup_glob(glob, strlen(glob), &search, &not);
2079                 len = strlen(search);
2080
2081                 /* we do not support '!' for function probes */
2082                 if (WARN_ON(not))
2083                         return;
2084         }
2085
2086         mutex_lock(&ftrace_lock);
2087         for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
2088                 struct hlist_head *hhd = &ftrace_func_hash[i];
2089
2090                 hlist_for_each_entry_safe(entry, n, tmp, hhd, node) {
2091
2092                         /* break up if statements for readability */
2093                         if ((flags & PROBE_TEST_FUNC) && entry->ops != ops)
2094                                 continue;
2095
2096                         if ((flags & PROBE_TEST_DATA) && entry->data != data)
2097                                 continue;
2098
2099                         /* do this last, since it is the most expensive */
2100                         if (glob) {
2101                                 kallsyms_lookup(entry->ip, NULL, NULL,
2102                                                 NULL, str);
2103                                 if (!ftrace_match(str, glob, len, type))
2104                                         continue;
2105                         }
2106
2107                         hlist_del(&entry->node);
2108                         call_rcu(&entry->rcu, ftrace_free_entry_rcu);
2109                 }
2110         }
2111         __disable_ftrace_function_probe();
2112         mutex_unlock(&ftrace_lock);
2113 }
2114
2115 void
2116 unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
2117                                 void *data)
2118 {
2119         __unregister_ftrace_function_probe(glob, ops, data,
2120                                           PROBE_TEST_FUNC | PROBE_TEST_DATA);
2121 }
2122
2123 void
2124 unregister_ftrace_function_probe_func(char *glob, struct ftrace_probe_ops *ops)
2125 {
2126         __unregister_ftrace_function_probe(glob, ops, NULL, PROBE_TEST_FUNC);
2127 }
2128
2129 void unregister_ftrace_function_probe_all(char *glob)
2130 {
2131         __unregister_ftrace_function_probe(glob, NULL, NULL, 0);
2132 }
2133
2134 static LIST_HEAD(ftrace_commands);
2135 static DEFINE_MUTEX(ftrace_cmd_mutex);
2136
2137 int register_ftrace_command(struct ftrace_func_command *cmd)
2138 {
2139         struct ftrace_func_command *p;
2140         int ret = 0;
2141
2142         mutex_lock(&ftrace_cmd_mutex);
2143         list_for_each_entry(p, &ftrace_commands, list) {
2144                 if (strcmp(cmd->name, p->name) == 0) {
2145                         ret = -EBUSY;
2146                         goto out_unlock;
2147                 }
2148         }
2149         list_add(&cmd->list, &ftrace_commands);
2150  out_unlock:
2151         mutex_unlock(&ftrace_cmd_mutex);
2152
2153         return ret;
2154 }
2155
2156 int unregister_ftrace_command(struct ftrace_func_command *cmd)
2157 {
2158         struct ftrace_func_command *p, *n;
2159         int ret = -ENODEV;
2160
2161         mutex_lock(&ftrace_cmd_mutex);
2162         list_for_each_entry_safe(p, n, &ftrace_commands, list) {
2163                 if (strcmp(cmd->name, p->name) == 0) {
2164                         ret = 0;
2165                         list_del_init(&p->list);
2166                         goto out_unlock;
2167                 }
2168         }
2169  out_unlock:
2170         mutex_unlock(&ftrace_cmd_mutex);
2171
2172         return ret;
2173 }
2174
2175 static int ftrace_process_regex(char *buff, int len, int enable)
2176 {
2177         char *func, *command, *next = buff;
2178         struct ftrace_func_command *p;
2179         int ret = -EINVAL;
2180
2181         func = strsep(&next, ":");
2182
2183         if (!next) {
2184                 ftrace_match_records(func, len, enable);
2185                 return 0;
2186         }
2187
2188         /* command found */
2189
2190         command = strsep(&next, ":");
2191
2192         mutex_lock(&ftrace_cmd_mutex);
2193         list_for_each_entry(p, &ftrace_commands, list) {
2194                 if (strcmp(p->name, command) == 0) {
2195                         ret = p->func(func, command, next, enable);
2196                         goto out_unlock;
2197                 }
2198         }
2199  out_unlock:
2200         mutex_unlock(&ftrace_cmd_mutex);
2201
2202         return ret;
2203 }
2204
2205 static ssize_t
2206 ftrace_regex_write(struct file *file, const char __user *ubuf,
2207                    size_t cnt, loff_t *ppos, int enable)
2208 {
2209         struct ftrace_iterator *iter;
2210         char ch;
2211         size_t read = 0;
2212         ssize_t ret;
2213
2214         if (!cnt || cnt < 0)
2215                 return 0;
2216
2217         mutex_lock(&ftrace_regex_lock);
2218
2219         if (file->f_mode & FMODE_READ) {
2220                 struct seq_file *m = file->private_data;
2221                 iter = m->private;
2222         } else
2223                 iter = file->private_data;
2224
2225         if (!*ppos) {
2226                 iter->flags &= ~FTRACE_ITER_CONT;
2227                 iter->buffer_idx = 0;
2228         }
2229
2230         ret = get_user(ch, ubuf++);
2231         if (ret)
2232                 goto out;
2233         read++;
2234         cnt--;
2235
2236         if (!(iter->flags & ~FTRACE_ITER_CONT)) {
2237                 /* skip white space */
2238                 while (cnt && isspace(ch)) {
2239                         ret = get_user(ch, ubuf++);
2240                         if (ret)
2241                                 goto out;
2242                         read++;
2243                         cnt--;
2244                 }
2245
2246                 if (isspace(ch)) {
2247                         file->f_pos += read;
2248                         ret = read;
2249                         goto out;
2250                 }
2251
2252                 iter->buffer_idx = 0;
2253         }
2254
2255         while (cnt && !isspace(ch)) {
2256                 if (iter->buffer_idx < FTRACE_BUFF_MAX)
2257                         iter->buffer[iter->buffer_idx++] = ch;
2258                 else {
2259                         ret = -EINVAL;
2260                         goto out;
2261                 }
2262                 ret = get_user(ch, ubuf++);
2263                 if (ret)
2264                         goto out;
2265                 read++;
2266                 cnt--;
2267         }
2268
2269         if (isspace(ch)) {
2270                 iter->buffer[iter->buffer_idx] = 0;
2271                 ret = ftrace_process_regex(iter->buffer,
2272                                            iter->buffer_idx, enable);
2273                 if (ret)
2274                         goto out;
2275                 iter->buffer_idx = 0;
2276         } else
2277                 iter->flags |= FTRACE_ITER_CONT;
2278
2279
2280         file->f_pos += read;
2281
2282         ret = read;
2283  out:
2284         mutex_unlock(&ftrace_regex_lock);
2285
2286         return ret;
2287 }
2288
2289 static ssize_t
2290 ftrace_filter_write(struct file *file, const char __user *ubuf,
2291                     size_t cnt, loff_t *ppos)
2292 {
2293         return ftrace_regex_write(file, ubuf, cnt, ppos, 1);
2294 }
2295
2296 static ssize_t
2297 ftrace_notrace_write(struct file *file, const char __user *ubuf,
2298                      size_t cnt, loff_t *ppos)
2299 {
2300         return ftrace_regex_write(file, ubuf, cnt, ppos, 0);
2301 }
2302
2303 static void
2304 ftrace_set_regex(unsigned char *buf, int len, int reset, int enable)
2305 {
2306         if (unlikely(ftrace_disabled))
2307                 return;
2308
2309         mutex_lock(&ftrace_regex_lock);
2310         if (reset)
2311                 ftrace_filter_reset(enable);
2312         if (buf)
2313                 ftrace_match_records(buf, len, enable);
2314         mutex_unlock(&ftrace_regex_lock);
2315 }
2316
2317 /**
2318  * ftrace_set_filter - set a function to filter on in ftrace
2319  * @buf - the string that holds the function filter text.
2320  * @len - the length of the string.
2321  * @reset - non zero to reset all filters before applying this filter.
2322  *
2323  * Filters denote which functions should be enabled when tracing is enabled.
2324  * If @buf is NULL and reset is set, all functions will be enabled for tracing.
2325  */
2326 void ftrace_set_filter(unsigned char *buf, int len, int reset)
2327 {
2328         ftrace_set_regex(buf, len, reset, 1);
2329 }
2330
2331 /**
2332  * ftrace_set_notrace - set a function to not trace in ftrace
2333  * @buf - the string that holds the function notrace text.
2334  * @len - the length of the string.
2335  * @reset - non zero to reset all filters before applying this filter.
2336  *
2337  * Notrace Filters denote which functions should not be enabled when tracing
2338  * is enabled. If @buf is NULL and reset is set, all functions will be enabled
2339  * for tracing.
2340  */
2341 void ftrace_set_notrace(unsigned char *buf, int len, int reset)
2342 {
2343         ftrace_set_regex(buf, len, reset, 0);
2344 }
2345
2346 /*
2347  * command line interface to allow users to set filters on boot up.
2348  */
2349 #define FTRACE_FILTER_SIZE              COMMAND_LINE_SIZE
2350 static char ftrace_notrace_buf[FTRACE_FILTER_SIZE] __initdata;
2351 static char ftrace_filter_buf[FTRACE_FILTER_SIZE] __initdata;
2352
2353 static int __init set_ftrace_notrace(char *str)
2354 {
2355         strncpy(ftrace_notrace_buf, str, FTRACE_FILTER_SIZE);
2356         return 1;
2357 }
2358 __setup("ftrace_notrace=", set_ftrace_notrace);
2359
2360 static int __init set_ftrace_filter(char *str)
2361 {
2362         strncpy(ftrace_filter_buf, str, FTRACE_FILTER_SIZE);
2363         return 1;
2364 }
2365 __setup("ftrace_filter=", set_ftrace_filter);
2366
2367 static void __init set_ftrace_early_filter(char *buf, int enable)
2368 {
2369         char *func;
2370
2371         while (buf) {
2372                 func = strsep(&buf, ",");
2373                 ftrace_set_regex(func, strlen(func), 0, enable);
2374         }
2375 }
2376
2377 static void __init set_ftrace_early_filters(void)
2378 {
2379         if (ftrace_filter_buf[0])
2380                 set_ftrace_early_filter(ftrace_filter_buf, 1);
2381         if (ftrace_notrace_buf[0])
2382                 set_ftrace_early_filter(ftrace_notrace_buf, 0);
2383 }
2384
2385 static int
2386 ftrace_regex_release(struct inode *inode, struct file *file, int enable)
2387 {
2388         struct seq_file *m = (struct seq_file *)file->private_data;
2389         struct ftrace_iterator *iter;
2390
2391         mutex_lock(&ftrace_regex_lock);
2392         if (file->f_mode & FMODE_READ) {
2393                 iter = m->private;
2394
2395                 seq_release(inode, file);
2396         } else
2397                 iter = file->private_data;
2398
2399         if (iter->buffer_idx) {
2400                 iter->buffer[iter->buffer_idx] = 0;
2401                 ftrace_match_records(iter->buffer, iter->buffer_idx, enable);
2402         }
2403
2404         mutex_lock(&ftrace_lock);
2405         if (ftrace_start_up && ftrace_enabled)
2406                 ftrace_run_update_code(FTRACE_ENABLE_CALLS);
2407         mutex_unlock(&ftrace_lock);
2408
2409         kfree(iter);
2410         mutex_unlock(&ftrace_regex_lock);
2411         return 0;
2412 }
2413
2414 static int
2415 ftrace_filter_release(struct inode *inode, struct file *file)
2416 {
2417         return ftrace_regex_release(inode, file, 1);
2418 }
2419
2420 static int
2421 ftrace_notrace_release(struct inode *inode, struct file *file)
2422 {
2423         return ftrace_regex_release(inode, file, 0);
2424 }
2425
2426 static const struct file_operations ftrace_avail_fops = {
2427         .open = ftrace_avail_open,
2428         .read = seq_read,
2429         .llseek = seq_lseek,
2430         .release = ftrace_avail_release,
2431 };
2432
2433 static const struct file_operations ftrace_failures_fops = {
2434         .open = ftrace_failures_open,
2435         .read = seq_read,
2436         .llseek = seq_lseek,
2437         .release = ftrace_avail_release,
2438 };
2439
2440 static const struct file_operations ftrace_filter_fops = {
2441         .open = ftrace_filter_open,
2442         .read = seq_read,
2443         .write = ftrace_filter_write,
2444         .llseek = ftrace_regex_lseek,
2445         .release = ftrace_filter_release,
2446 };
2447
2448 static const struct file_operations ftrace_notrace_fops = {
2449         .open = ftrace_notrace_open,
2450         .read = seq_read,
2451         .write = ftrace_notrace_write,
2452         .llseek = ftrace_regex_lseek,
2453         .release = ftrace_notrace_release,
2454 };
2455
2456 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
2457
2458 static DEFINE_MUTEX(graph_lock);
2459
2460 int ftrace_graph_count;
2461 unsigned long ftrace_graph_funcs[FTRACE_GRAPH_MAX_FUNCS] __read_mostly;
2462
2463 static void *
2464 __g_next(struct seq_file *m, loff_t *pos)
2465 {
2466         unsigned long *array = m->private;
2467
2468         if (*pos >= ftrace_graph_count)
2469                 return NULL;
2470         return &array[*pos];
2471 }
2472
2473 static void *
2474 g_next(struct seq_file *m, void *v, loff_t *pos)
2475 {
2476         (*pos)++;
2477         return __g_next(m, pos);
2478 }
2479
2480 static void *g_start(struct seq_file *m, loff_t *pos)
2481 {
2482         mutex_lock(&graph_lock);
2483
2484         /* Nothing, tell g_show to print all functions are enabled */
2485         if (!ftrace_graph_count && !*pos)
2486                 return (void *)1;
2487
2488         return __g_next(m, pos);
2489 }
2490
2491 static void g_stop(struct seq_file *m, void *p)
2492 {
2493         mutex_unlock(&graph_lock);
2494 }
2495
2496 static int g_show(struct seq_file *m, void *v)
2497 {
2498         unsigned long *ptr = v;
2499
2500         if (!ptr)
2501                 return 0;
2502
2503         if (ptr == (unsigned long *)1) {
2504                 seq_printf(m, "#### all functions enabled ####\n");
2505                 return 0;
2506         }
2507
2508         seq_printf(m, "%pf\n", v);
2509
2510         return 0;
2511 }
2512
2513 static struct seq_operations ftrace_graph_seq_ops = {
2514         .start = g_start,
2515         .next = g_next,
2516         .stop = g_stop,
2517         .show = g_show,
2518 };
2519
2520 static int
2521 ftrace_graph_open(struct inode *inode, struct file *file)
2522 {
2523         int ret = 0;
2524
2525         if (unlikely(ftrace_disabled))
2526                 return -ENODEV;
2527
2528         mutex_lock(&graph_lock);
2529         if ((file->f_mode & FMODE_WRITE) &&
2530             (file->f_flags & O_TRUNC)) {
2531                 ftrace_graph_count = 0;
2532                 memset(ftrace_graph_funcs, 0, sizeof(ftrace_graph_funcs));
2533         }
2534
2535         if (file->f_mode & FMODE_READ) {
2536                 ret = seq_open(file, &ftrace_graph_seq_ops);
2537                 if (!ret) {
2538                         struct seq_file *m = file->private_data;
2539                         m->private = ftrace_graph_funcs;
2540                 }
2541         } else
2542                 file->private_data = ftrace_graph_funcs;
2543         mutex_unlock(&graph_lock);
2544
2545         return ret;
2546 }
2547
2548 static int
2549 ftrace_graph_release(struct inode *inode, struct file *file)
2550 {
2551         if (file->f_mode & FMODE_READ)
2552                 seq_release(inode, file);
2553         return 0;
2554 }
2555
2556 static int
2557 ftrace_set_func(unsigned long *array, int *idx, char *buffer)
2558 {
2559         struct dyn_ftrace *rec;
2560         struct ftrace_page *pg;
2561         int search_len;
2562         int found = 0;
2563         int type, not;
2564         char *search;
2565         bool exists;
2566         int i;
2567
2568         if (ftrace_disabled)
2569                 return -ENODEV;
2570
2571         /* decode regex */
2572         type = ftrace_setup_glob(buffer, strlen(buffer), &search, &not);
2573         if (not)
2574                 return -EINVAL;
2575
2576         search_len = strlen(search);
2577
2578         mutex_lock(&ftrace_lock);
2579         do_for_each_ftrace_rec(pg, rec) {
2580
2581                 if (*idx >= FTRACE_GRAPH_MAX_FUNCS)
2582                         break;
2583
2584                 if (rec->flags & (FTRACE_FL_FAILED | FTRACE_FL_FREE))
2585                         continue;
2586
2587                 if (ftrace_match_record(rec, search, search_len, type)) {
2588                         /* ensure it is not already in the array */
2589                         exists = false;
2590                         for (i = 0; i < *idx; i++)
2591                                 if (array[i] == rec->ip) {
2592                                         exists = true;
2593                                         break;
2594                                 }
2595                         if (!exists) {
2596                                 array[(*idx)++] = rec->ip;
2597                                 found = 1;
2598                         }
2599                 }
2600         } while_for_each_ftrace_rec();
2601
2602         mutex_unlock(&ftrace_lock);
2603
2604         return found ? 0 : -EINVAL;
2605 }
2606
2607 static ssize_t
2608 ftrace_graph_write(struct file *file, const char __user *ubuf,
2609                    size_t cnt, loff_t *ppos)
2610 {
2611         unsigned char buffer[FTRACE_BUFF_MAX+1];
2612         unsigned long *array;
2613         size_t read = 0;
2614         ssize_t ret;
2615         int index = 0;
2616         char ch;
2617
2618         if (!cnt || cnt < 0)
2619                 return 0;
2620
2621         mutex_lock(&graph_lock);
2622
2623         if (ftrace_graph_count >= FTRACE_GRAPH_MAX_FUNCS) {
2624                 ret = -EBUSY;
2625                 goto out;
2626         }
2627
2628         if (file->f_mode & FMODE_READ) {
2629                 struct seq_file *m = file->private_data;
2630                 array = m->private;
2631         } else
2632                 array = file->private_data;
2633
2634         ret = get_user(ch, ubuf++);
2635         if (ret)
2636                 goto out;
2637         read++;
2638         cnt--;
2639
2640         /* skip white space */
2641         while (cnt && isspace(ch)) {
2642                 ret = get_user(ch, ubuf++);
2643                 if (ret)
2644                         goto out;
2645                 read++;
2646                 cnt--;
2647         }
2648
2649         if (isspace(ch)) {
2650                 *ppos += read;
2651                 ret = read;
2652                 goto out;
2653         }
2654
2655         while (cnt && !isspace(ch)) {
2656                 if (index < FTRACE_BUFF_MAX)
2657                         buffer[index++] = ch;
2658                 else {
2659                         ret = -EINVAL;
2660                         goto out;
2661                 }
2662                 ret = get_user(ch, ubuf++);
2663                 if (ret)
2664                         goto out;
2665                 read++;
2666                 cnt--;
2667         }
2668         buffer[index] = 0;
2669
2670         /* we allow only one expression at a time */
2671         ret = ftrace_set_func(array, &ftrace_graph_count, buffer);
2672         if (ret)
2673                 goto out;
2674
2675         file->f_pos += read;
2676
2677         ret = read;
2678  out:
2679         mutex_unlock(&graph_lock);
2680
2681         return ret;
2682 }
2683
2684 static const struct file_operations ftrace_graph_fops = {
2685         .open           = ftrace_graph_open,
2686         .read           = seq_read,
2687         .write          = ftrace_graph_write,
2688         .release        = ftrace_graph_release,
2689 };
2690 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
2691
2692 static __init int ftrace_init_dyn_debugfs(struct dentry *d_tracer)
2693 {
2694
2695         trace_create_file("available_filter_functions", 0444,
2696                         d_tracer, NULL, &ftrace_avail_fops);
2697
2698         trace_create_file("failures", 0444,
2699                         d_tracer, NULL, &ftrace_failures_fops);
2700
2701         trace_create_file("set_ftrace_filter", 0644, d_tracer,
2702                         NULL, &ftrace_filter_fops);
2703
2704         trace_create_file("set_ftrace_notrace", 0644, d_tracer,
2705                                     NULL, &ftrace_notrace_fops);
2706
2707 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
2708         trace_create_file("set_graph_function", 0444, d_tracer,
2709                                     NULL,
2710                                     &ftrace_graph_fops);
2711 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
2712
2713         return 0;
2714 }
2715
2716 static int ftrace_convert_nops(struct module *mod,
2717                                unsigned long *start,
2718                                unsigned long *end)
2719 {
2720         unsigned long *p;
2721         unsigned long addr;
2722         unsigned long flags;
2723
2724         mutex_lock(&ftrace_lock);
2725         p = start;
2726         while (p < end) {
2727                 addr = ftrace_call_adjust(*p++);
2728                 /*
2729                  * Some architecture linkers will pad between
2730                  * the different mcount_loc sections of different
2731                  * object files to satisfy alignments.
2732                  * Skip any NULL pointers.
2733                  */
2734                 if (!addr)
2735                         continue;
2736                 ftrace_record_ip(addr);
2737         }
2738
2739         /* disable interrupts to prevent kstop machine */
2740         local_irq_save(flags);
2741         ftrace_update_code(mod);
2742         local_irq_restore(flags);
2743         mutex_unlock(&ftrace_lock);
2744
2745         return 0;
2746 }
2747
2748 #ifdef CONFIG_MODULES
2749 void ftrace_release(void *start, void *end)
2750 {
2751         struct dyn_ftrace *rec;
2752         struct ftrace_page *pg;
2753         unsigned long s = (unsigned long)start;
2754         unsigned long e = (unsigned long)end;
2755
2756         if (ftrace_disabled || !start || start == end)
2757                 return;
2758
2759         mutex_lock(&ftrace_lock);
2760         do_for_each_ftrace_rec(pg, rec) {
2761                 if ((rec->ip >= s) && (rec->ip < e)) {
2762                         /*
2763                          * rec->ip is changed in ftrace_free_rec()
2764                          * It should not between s and e if record was freed.
2765                          */
2766                         FTRACE_WARN_ON(rec->flags & FTRACE_FL_FREE);
2767                         ftrace_free_rec(rec);
2768                 }
2769         } while_for_each_ftrace_rec();
2770         mutex_unlock(&ftrace_lock);
2771 }
2772
2773 static void ftrace_init_module(struct module *mod,
2774                                unsigned long *start, unsigned long *end)
2775 {
2776         if (ftrace_disabled || start == end)
2777                 return;
2778         ftrace_convert_nops(mod, start, end);
2779 }
2780
2781 static int ftrace_module_notify(struct notifier_block *self,
2782                                 unsigned long val, void *data)
2783 {
2784         struct module *mod = data;
2785
2786         switch (val) {
2787         case MODULE_STATE_COMING:
2788                 ftrace_init_module(mod, mod->ftrace_callsites,
2789                                    mod->ftrace_callsites +
2790                                    mod->num_ftrace_callsites);
2791                 break;
2792         case MODULE_STATE_GOING:
2793                 ftrace_release(mod->ftrace_callsites,
2794                                mod->ftrace_callsites +
2795                                mod->num_ftrace_callsites);
2796                 break;
2797         }
2798
2799         return 0;
2800 }
2801 #else
2802 static int ftrace_module_notify(struct notifier_block *self,
2803                                 unsigned long val, void *data)
2804 {
2805         return 0;
2806 }
2807 #endif /* CONFIG_MODULES */
2808
2809 struct notifier_block ftrace_module_nb = {
2810         .notifier_call = ftrace_module_notify,
2811         .priority = 0,
2812 };
2813
2814 extern unsigned long __start_mcount_loc[];
2815 extern unsigned long __stop_mcount_loc[];
2816
2817 void __init ftrace_init(void)
2818 {
2819         unsigned long count, addr, flags;
2820         int ret;
2821
2822         /* Keep the ftrace pointer to the stub */
2823         addr = (unsigned long)ftrace_stub;
2824
2825         local_irq_save(flags);
2826         ftrace_dyn_arch_init(&addr);
2827         local_irq_restore(flags);
2828
2829         /* ftrace_dyn_arch_init places the return code in addr */
2830         if (addr)
2831                 goto failed;
2832
2833         count = __stop_mcount_loc - __start_mcount_loc;
2834
2835         ret = ftrace_dyn_table_alloc(count);
2836         if (ret)
2837                 goto failed;
2838
2839         last_ftrace_enabled = ftrace_enabled = 1;
2840
2841         ret = ftrace_convert_nops(NULL,
2842                                   __start_mcount_loc,
2843                                   __stop_mcount_loc);
2844
2845         ret = register_module_notifier(&ftrace_module_nb);
2846         if (ret)
2847                 pr_warning("Failed to register trace ftrace module notifier\n");
2848
2849         set_ftrace_early_filters();
2850
2851         return;
2852  failed:
2853         ftrace_disabled = 1;
2854 }
2855
2856 #else
2857
2858 static int __init ftrace_nodyn_init(void)
2859 {
2860         ftrace_enabled = 1;
2861         return 0;
2862 }
2863 device_initcall(ftrace_nodyn_init);
2864
2865 static inline int ftrace_init_dyn_debugfs(struct dentry *d_tracer) { return 0; }
2866 static inline void ftrace_startup_enable(int command) { }
2867 /* Keep as macros so we do not need to define the commands */
2868 # define ftrace_startup(command)        do { } while (0)
2869 # define ftrace_shutdown(command)       do { } while (0)
2870 # define ftrace_startup_sysctl()        do { } while (0)
2871 # define ftrace_shutdown_sysctl()       do { } while (0)
2872 #endif /* CONFIG_DYNAMIC_FTRACE */
2873
2874 static ssize_t
2875 ftrace_pid_read(struct file *file, char __user *ubuf,
2876                        size_t cnt, loff_t *ppos)
2877 {
2878         char buf[64];
2879         int r;
2880
2881         if (ftrace_pid_trace == ftrace_swapper_pid)
2882                 r = sprintf(buf, "swapper tasks\n");
2883         else if (ftrace_pid_trace)
2884                 r = sprintf(buf, "%u\n", pid_vnr(ftrace_pid_trace));
2885         else
2886                 r = sprintf(buf, "no pid\n");
2887
2888         return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2889 }
2890
2891 static void clear_ftrace_swapper(void)
2892 {
2893         struct task_struct *p;
2894         int cpu;
2895
2896         get_online_cpus();
2897         for_each_online_cpu(cpu) {
2898                 p = idle_task(cpu);
2899                 clear_tsk_trace_trace(p);
2900         }
2901         put_online_cpus();
2902 }
2903
2904 static void set_ftrace_swapper(void)
2905 {
2906         struct task_struct *p;
2907         int cpu;
2908
2909         get_online_cpus();
2910         for_each_online_cpu(cpu) {
2911                 p = idle_task(cpu);
2912                 set_tsk_trace_trace(p);
2913         }
2914         put_online_cpus();
2915 }
2916
2917 static void clear_ftrace_pid(struct pid *pid)
2918 {
2919         struct task_struct *p;
2920
2921         rcu_read_lock();
2922         do_each_pid_task(pid, PIDTYPE_PID, p) {
2923                 clear_tsk_trace_trace(p);
2924         } while_each_pid_task(pid, PIDTYPE_PID, p);
2925         rcu_read_unlock();
2926
2927         put_pid(pid);
2928 }
2929
2930 static void set_ftrace_pid(struct pid *pid)
2931 {
2932         struct task_struct *p;
2933
2934         rcu_read_lock();
2935         do_each_pid_task(pid, PIDTYPE_PID, p) {
2936                 set_tsk_trace_trace(p);
2937         } while_each_pid_task(pid, PIDTYPE_PID, p);
2938         rcu_read_unlock();
2939 }
2940
2941 static void clear_ftrace_pid_task(struct pid **pid)
2942 {
2943         if (*pid == ftrace_swapper_pid)
2944                 clear_ftrace_swapper();
2945         else
2946                 clear_ftrace_pid(*pid);
2947
2948         *pid = NULL;
2949 }
2950
2951 static void set_ftrace_pid_task(struct pid *pid)
2952 {
2953         if (pid == ftrace_swapper_pid)
2954                 set_ftrace_swapper();
2955         else
2956                 set_ftrace_pid(pid);
2957 }
2958
2959 static ssize_t
2960 ftrace_pid_write(struct file *filp, const char __user *ubuf,
2961                    size_t cnt, loff_t *ppos)
2962 {
2963         struct pid *pid;
2964         char buf[64];
2965         long val;
2966         int ret;
2967
2968         if (cnt >= sizeof(buf))
2969                 return -EINVAL;
2970
2971         if (copy_from_user(&buf, ubuf, cnt))
2972                 return -EFAULT;
2973
2974         buf[cnt] = 0;
2975
2976         ret = strict_strtol(buf, 10, &val);
2977         if (ret < 0)
2978                 return ret;
2979
2980         mutex_lock(&ftrace_lock);
2981         if (val < 0) {
2982                 /* disable pid tracing */
2983                 if (!ftrace_pid_trace)
2984                         goto out;
2985
2986                 clear_ftrace_pid_task(&ftrace_pid_trace);
2987
2988         } else {
2989                 /* swapper task is special */
2990                 if (!val) {
2991                         pid = ftrace_swapper_pid;
2992                         if (pid == ftrace_pid_trace)
2993                                 goto out;
2994                 } else {
2995                         pid = find_get_pid(val);
2996
2997                         if (pid == ftrace_pid_trace) {
2998                                 put_pid(pid);
2999                                 goto out;
3000                         }
3001                 }
3002
3003                 if (ftrace_pid_trace)
3004                         clear_ftrace_pid_task(&ftrace_pid_trace);
3005
3006                 if (!pid)
3007                         goto out;
3008
3009                 ftrace_pid_trace = pid;
3010
3011                 set_ftrace_pid_task(ftrace_pid_trace);
3012         }
3013
3014         /* update the function call */
3015         ftrace_update_pid_func();
3016         ftrace_startup_enable(0);
3017
3018  out:
3019         mutex_unlock(&ftrace_lock);
3020
3021         return cnt;
3022 }
3023
3024 static const struct file_operations ftrace_pid_fops = {
3025         .read = ftrace_pid_read,
3026         .write = ftrace_pid_write,
3027 };
3028
3029 static __init int ftrace_init_debugfs(void)
3030 {
3031         struct dentry *d_tracer;
3032
3033         d_tracer = tracing_init_dentry();
3034         if (!d_tracer)
3035                 return 0;
3036
3037         ftrace_init_dyn_debugfs(d_tracer);
3038
3039         trace_create_file("set_ftrace_pid", 0644, d_tracer,
3040                             NULL, &ftrace_pid_fops);
3041
3042         ftrace_profile_debugfs(d_tracer);
3043
3044         return 0;
3045 }
3046 fs_initcall(ftrace_init_debugfs);
3047
3048 /**
3049  * ftrace_kill - kill ftrace
3050  *
3051  * This function should be used by panic code. It stops ftrace
3052  * but in a not so nice way. If you need to simply kill ftrace
3053  * from a non-atomic section, use ftrace_kill.
3054  */
3055 void ftrace_kill(void)
3056 {
3057         ftrace_disabled = 1;
3058         ftrace_enabled = 0;
3059         clear_ftrace_function();
3060 }
3061
3062 /**
3063  * register_ftrace_function - register a function for profiling
3064  * @ops - ops structure that holds the function for profiling.
3065  *
3066  * Register a function to be called by all functions in the
3067  * kernel.
3068  *
3069  * Note: @ops->func and all the functions it calls must be labeled
3070  *       with "notrace", otherwise it will go into a
3071  *       recursive loop.
3072  */
3073 int register_ftrace_function(struct ftrace_ops *ops)
3074 {
3075         int ret;
3076
3077         if (unlikely(ftrace_disabled))
3078                 return -1;
3079
3080         mutex_lock(&ftrace_lock);
3081
3082         ret = __register_ftrace_function(ops);
3083         ftrace_startup(0);
3084
3085         mutex_unlock(&ftrace_lock);
3086         return ret;
3087 }
3088
3089 /**
3090  * unregister_ftrace_function - unregister a function for profiling.
3091  * @ops - ops structure that holds the function to unregister
3092  *
3093  * Unregister a function that was added to be called by ftrace profiling.
3094  */
3095 int unregister_ftrace_function(struct ftrace_ops *ops)
3096 {
3097         int ret;
3098
3099         mutex_lock(&ftrace_lock);
3100         ret = __unregister_ftrace_function(ops);
3101         ftrace_shutdown(0);
3102         mutex_unlock(&ftrace_lock);
3103
3104         return ret;
3105 }
3106
3107 int
3108 ftrace_enable_sysctl(struct ctl_table *table, int write,
3109                      struct file *file, void __user *buffer, size_t *lenp,
3110                      loff_t *ppos)
3111 {
3112         int ret;
3113
3114         if (unlikely(ftrace_disabled))
3115                 return -ENODEV;
3116
3117         mutex_lock(&ftrace_lock);
3118
3119         ret  = proc_dointvec(table, write, file, buffer, lenp, ppos);
3120
3121         if (ret || !write || (last_ftrace_enabled == !!ftrace_enabled))
3122                 goto out;
3123
3124         last_ftrace_enabled = !!ftrace_enabled;
3125
3126         if (ftrace_enabled) {
3127
3128                 ftrace_startup_sysctl();
3129
3130                 /* we are starting ftrace again */
3131                 if (ftrace_list != &ftrace_list_end) {
3132                         if (ftrace_list->next == &ftrace_list_end)
3133                                 ftrace_trace_function = ftrace_list->func;
3134                         else
3135                                 ftrace_trace_function = ftrace_list_func;
3136                 }
3137
3138         } else {
3139                 /* stopping ftrace calls (just send to ftrace_stub) */
3140                 ftrace_trace_function = ftrace_stub;
3141
3142                 ftrace_shutdown_sysctl();
3143         }
3144
3145  out:
3146         mutex_unlock(&ftrace_lock);
3147         return ret;
3148 }
3149
3150 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
3151
3152 static int ftrace_graph_active;
3153 static struct notifier_block ftrace_suspend_notifier;
3154
3155 int ftrace_graph_entry_stub(struct ftrace_graph_ent *trace)
3156 {
3157         return 0;
3158 }
3159
3160 /* The callbacks that hook a function */
3161 trace_func_graph_ret_t ftrace_graph_return =
3162                         (trace_func_graph_ret_t)ftrace_stub;
3163 trace_func_graph_ent_t ftrace_graph_entry = ftrace_graph_entry_stub;
3164
3165 /* Try to assign a return stack array on FTRACE_RETSTACK_ALLOC_SIZE tasks. */
3166 static int alloc_retstack_tasklist(struct ftrace_ret_stack **ret_stack_list)
3167 {
3168         int i;
3169         int ret = 0;
3170         unsigned long flags;
3171         int start = 0, end = FTRACE_RETSTACK_ALLOC_SIZE;
3172         struct task_struct *g, *t;
3173
3174         for (i = 0; i < FTRACE_RETSTACK_ALLOC_SIZE; i++) {
3175                 ret_stack_list[i] = kmalloc(FTRACE_RETFUNC_DEPTH
3176                                         * sizeof(struct ftrace_ret_stack),
3177                                         GFP_KERNEL);
3178                 if (!ret_stack_list[i]) {
3179                         start = 0;
3180                         end = i;
3181                         ret = -ENOMEM;
3182                         goto free;
3183                 }
3184         }
3185
3186         read_lock_irqsave(&tasklist_lock, flags);
3187         do_each_thread(g, t) {
3188                 if (start == end) {
3189                         ret = -EAGAIN;
3190                         goto unlock;
3191                 }
3192
3193                 if (t->ret_stack == NULL) {
3194                         atomic_set(&t->tracing_graph_pause, 0);
3195                         atomic_set(&t->trace_overrun, 0);
3196                         t->curr_ret_stack = -1;
3197                         /* Make sure the tasks see the -1 first: */
3198                         smp_wmb();
3199                         t->ret_stack = ret_stack_list[start++];
3200                 }
3201         } while_each_thread(g, t);
3202
3203 unlock:
3204         read_unlock_irqrestore(&tasklist_lock, flags);
3205 free:
3206         for (i = start; i < end; i++)
3207                 kfree(ret_stack_list[i]);
3208         return ret;
3209 }
3210
3211 static void
3212 ftrace_graph_probe_sched_switch(struct rq *__rq, struct task_struct *prev,
3213                                 struct task_struct *next)
3214 {
3215         unsigned long long timestamp;
3216         int index;
3217
3218         /*
3219          * Does the user want to count the time a function was asleep.
3220          * If so, do not update the time stamps.
3221          */
3222         if (trace_flags & TRACE_ITER_SLEEP_TIME)
3223                 return;
3224
3225         timestamp = trace_clock_local();
3226
3227         prev->ftrace_timestamp = timestamp;
3228
3229         /* only process tasks that we timestamped */
3230         if (!next->ftrace_timestamp)
3231                 return;
3232
3233         /*
3234          * Update all the counters in next to make up for the
3235          * time next was sleeping.
3236          */
3237         timestamp -= next->ftrace_timestamp;
3238
3239         for (index = next->curr_ret_stack; index >= 0; index--)
3240                 next->ret_stack[index].calltime += timestamp;
3241 }
3242
3243 /* Allocate a return stack for each task */
3244 static int start_graph_tracing(void)
3245 {
3246         struct ftrace_ret_stack **ret_stack_list;
3247         int ret, cpu;
3248
3249         ret_stack_list = kmalloc(FTRACE_RETSTACK_ALLOC_SIZE *
3250                                 sizeof(struct ftrace_ret_stack *),
3251                                 GFP_KERNEL);
3252
3253         if (!ret_stack_list)
3254                 return -ENOMEM;
3255
3256         /* The cpu_boot init_task->ret_stack will never be freed */
3257         for_each_online_cpu(cpu) {
3258                 if (!idle_task(cpu)->ret_stack)
3259                         ftrace_graph_init_task(idle_task(cpu));
3260         }
3261
3262         do {
3263                 ret = alloc_retstack_tasklist(ret_stack_list);
3264         } while (ret == -EAGAIN);
3265
3266         if (!ret) {
3267                 ret = register_trace_sched_switch(ftrace_graph_probe_sched_switch);
3268                 if (ret)
3269                         pr_info("ftrace_graph: Couldn't activate tracepoint"
3270                                 " probe to kernel_sched_switch\n");
3271         }
3272
3273         kfree(ret_stack_list);
3274         return ret;
3275 }
3276
3277 /*
3278  * Hibernation protection.
3279  * The state of the current task is too much unstable during
3280  * suspend/restore to disk. We want to protect against that.
3281  */
3282 static int
3283 ftrace_suspend_notifier_call(struct notifier_block *bl, unsigned long state,
3284                                                         void *unused)
3285 {
3286         switch (state) {
3287         case PM_HIBERNATION_PREPARE:
3288                 pause_graph_tracing();
3289                 break;
3290
3291         case PM_POST_HIBERNATION:
3292                 unpause_graph_tracing();
3293                 break;
3294         }
3295         return NOTIFY_DONE;
3296 }
3297
3298 int register_ftrace_graph(trace_func_graph_ret_t retfunc,
3299                         trace_func_graph_ent_t entryfunc)
3300 {
3301         int ret = 0;
3302
3303         mutex_lock(&ftrace_lock);
3304
3305         /* we currently allow only one tracer registered at a time */
3306         if (ftrace_graph_active) {
3307                 ret = -EBUSY;
3308                 goto out;
3309         }
3310
3311         ftrace_suspend_notifier.notifier_call = ftrace_suspend_notifier_call;
3312         register_pm_notifier(&ftrace_suspend_notifier);
3313
3314         ftrace_graph_active++;
3315         ret = start_graph_tracing();
3316         if (ret) {
3317                 ftrace_graph_active--;
3318                 goto out;
3319         }
3320
3321         ftrace_graph_return = retfunc;
3322         ftrace_graph_entry = entryfunc;
3323
3324         ftrace_startup(FTRACE_START_FUNC_RET);
3325
3326 out:
3327         mutex_unlock(&ftrace_lock);
3328         return ret;
3329 }
3330
3331 void unregister_ftrace_graph(void)
3332 {
3333         mutex_lock(&ftrace_lock);
3334
3335         if (unlikely(!ftrace_graph_active))
3336                 goto out;
3337
3338         ftrace_graph_active--;
3339         unregister_trace_sched_switch(ftrace_graph_probe_sched_switch);
3340         ftrace_graph_return = (trace_func_graph_ret_t)ftrace_stub;
3341         ftrace_graph_entry = ftrace_graph_entry_stub;
3342         ftrace_shutdown(FTRACE_STOP_FUNC_RET);
3343         unregister_pm_notifier(&ftrace_suspend_notifier);
3344
3345  out:
3346         mutex_unlock(&ftrace_lock);
3347 }
3348
3349 /* Allocate a return stack for newly created task */
3350 void ftrace_graph_init_task(struct task_struct *t)
3351 {
3352         /* Make sure we do not use the parent ret_stack */
3353         t->ret_stack = NULL;
3354
3355         if (ftrace_graph_active) {
3356                 struct ftrace_ret_stack *ret_stack;
3357
3358                 ret_stack = kmalloc(FTRACE_RETFUNC_DEPTH
3359                                 * sizeof(struct ftrace_ret_stack),
3360                                 GFP_KERNEL);
3361                 if (!ret_stack)
3362                         return;
3363                 t->curr_ret_stack = -1;
3364                 atomic_set(&t->tracing_graph_pause, 0);
3365                 atomic_set(&t->trace_overrun, 0);
3366                 t->ftrace_timestamp = 0;
3367                 /* make curr_ret_stack visable before we add the ret_stack */
3368                 smp_wmb();
3369                 t->ret_stack = ret_stack;
3370         }
3371 }
3372
3373 void ftrace_graph_exit_task(struct task_struct *t)
3374 {
3375         struct ftrace_ret_stack *ret_stack = t->ret_stack;
3376
3377         t->ret_stack = NULL;
3378         /* NULL must become visible to IRQs before we free it: */
3379         barrier();
3380
3381         kfree(ret_stack);
3382 }
3383
3384 void ftrace_graph_stop(void)
3385 {
3386         ftrace_stop();
3387 }
3388 #endif
3389