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