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