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