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