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