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