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