GNU Linux-libre 4.14.332-gnu1
[releases.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/sched/task.h>
19 #include <linux/kallsyms.h>
20 #include <linux/seq_file.h>
21 #include <linux/suspend.h>
22 #include <linux/tracefs.h>
23 #include <linux/hardirq.h>
24 #include <linux/kthread.h>
25 #include <linux/uaccess.h>
26 #include <linux/bsearch.h>
27 #include <linux/module.h>
28 #include <linux/ftrace.h>
29 #include <linux/sysctl.h>
30 #include <linux/slab.h>
31 #include <linux/ctype.h>
32 #include <linux/sort.h>
33 #include <linux/list.h>
34 #include <linux/hash.h>
35 #include <linux/rcupdate.h>
36 #include <linux/kprobes.h>
37
38 #include <trace/events/sched.h>
39
40 #include <asm/sections.h>
41 #include <asm/setup.h>
42
43 #include "trace_output.h"
44 #include "trace_stat.h"
45
46 #define FTRACE_WARN_ON(cond)                    \
47         ({                                      \
48                 int ___r = cond;                \
49                 if (WARN_ON(___r))              \
50                         ftrace_kill();          \
51                 ___r;                           \
52         })
53
54 #define FTRACE_WARN_ON_ONCE(cond)               \
55         ({                                      \
56                 int ___r = cond;                \
57                 if (WARN_ON_ONCE(___r))         \
58                         ftrace_kill();          \
59                 ___r;                           \
60         })
61
62 /* hash bits for specific function selection */
63 #define FTRACE_HASH_BITS 7
64 #define FTRACE_FUNC_HASHSIZE (1 << FTRACE_HASH_BITS)
65 #define FTRACE_HASH_DEFAULT_BITS 10
66 #define FTRACE_HASH_MAX_BITS 12
67
68 #ifdef CONFIG_DYNAMIC_FTRACE
69 #define INIT_OPS_HASH(opsname)  \
70         .func_hash              = &opsname.local_hash,                  \
71         .local_hash.regex_lock  = __MUTEX_INITIALIZER(opsname.local_hash.regex_lock),
72 #define ASSIGN_OPS_HASH(opsname, val) \
73         .func_hash              = val, \
74         .local_hash.regex_lock  = __MUTEX_INITIALIZER(opsname.local_hash.regex_lock),
75 #else
76 #define INIT_OPS_HASH(opsname)
77 #define ASSIGN_OPS_HASH(opsname, val)
78 #endif
79
80 static struct ftrace_ops ftrace_list_end __read_mostly = {
81         .func           = ftrace_stub,
82         .flags          = FTRACE_OPS_FL_RECURSION_SAFE | FTRACE_OPS_FL_STUB,
83         INIT_OPS_HASH(ftrace_list_end)
84 };
85
86 /* ftrace_enabled is a method to turn ftrace on or off */
87 int ftrace_enabled __read_mostly;
88 static int last_ftrace_enabled;
89
90 /* Current function tracing op */
91 struct ftrace_ops *function_trace_op __read_mostly = &ftrace_list_end;
92 /* What to set function_trace_op to */
93 static struct ftrace_ops *set_function_trace_op;
94
95 static bool ftrace_pids_enabled(struct ftrace_ops *ops)
96 {
97         struct trace_array *tr;
98
99         if (!(ops->flags & FTRACE_OPS_FL_PID) || !ops->private)
100                 return false;
101
102         tr = ops->private;
103
104         return tr->function_pids != NULL;
105 }
106
107 static void ftrace_update_trampoline(struct ftrace_ops *ops);
108
109 /*
110  * ftrace_disabled is set when an anomaly is discovered.
111  * ftrace_disabled is much stronger than ftrace_enabled.
112  */
113 static int ftrace_disabled __read_mostly;
114
115 static DEFINE_MUTEX(ftrace_lock);
116
117 static struct ftrace_ops __rcu *ftrace_ops_list __read_mostly = &ftrace_list_end;
118 ftrace_func_t ftrace_trace_function __read_mostly = ftrace_stub;
119 static struct ftrace_ops global_ops;
120
121 #if ARCH_SUPPORTS_FTRACE_OPS
122 static void ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip,
123                                  struct ftrace_ops *op, struct pt_regs *regs);
124 #else
125 /* See comment below, where ftrace_ops_list_func is defined */
126 static void ftrace_ops_no_ops(unsigned long ip, unsigned long parent_ip);
127 #define ftrace_ops_list_func ((ftrace_func_t)ftrace_ops_no_ops)
128 #endif
129
130 /*
131  * Traverse the ftrace_global_list, invoking all entries.  The reason that we
132  * can use rcu_dereference_raw_notrace() is that elements removed from this list
133  * are simply leaked, so there is no need to interact with a grace-period
134  * mechanism.  The rcu_dereference_raw_notrace() calls are needed to handle
135  * concurrent insertions into the ftrace_global_list.
136  *
137  * Silly Alpha and silly pointer-speculation compiler optimizations!
138  */
139 #define do_for_each_ftrace_op(op, list)                 \
140         op = rcu_dereference_raw_notrace(list);                 \
141         do
142
143 /*
144  * Optimized for just a single item in the list (as that is the normal case).
145  */
146 #define while_for_each_ftrace_op(op)                            \
147         while (likely(op = rcu_dereference_raw_notrace((op)->next)) &&  \
148                unlikely((op) != &ftrace_list_end))
149
150 static inline void ftrace_ops_init(struct ftrace_ops *ops)
151 {
152 #ifdef CONFIG_DYNAMIC_FTRACE
153         if (!(ops->flags & FTRACE_OPS_FL_INITIALIZED)) {
154                 mutex_init(&ops->local_hash.regex_lock);
155                 ops->func_hash = &ops->local_hash;
156                 ops->flags |= FTRACE_OPS_FL_INITIALIZED;
157         }
158 #endif
159 }
160
161 /**
162  * ftrace_nr_registered_ops - return number of ops registered
163  *
164  * Returns the number of ftrace_ops registered and tracing functions
165  */
166 int ftrace_nr_registered_ops(void)
167 {
168         struct ftrace_ops *ops;
169         int cnt = 0;
170
171         mutex_lock(&ftrace_lock);
172
173         for (ops = rcu_dereference_protected(ftrace_ops_list,
174                                              lockdep_is_held(&ftrace_lock));
175              ops != &ftrace_list_end;
176              ops = rcu_dereference_protected(ops->next,
177                                              lockdep_is_held(&ftrace_lock)))
178                 cnt++;
179
180         mutex_unlock(&ftrace_lock);
181
182         return cnt;
183 }
184
185 static void ftrace_pid_func(unsigned long ip, unsigned long parent_ip,
186                             struct ftrace_ops *op, struct pt_regs *regs)
187 {
188         struct trace_array *tr = op->private;
189
190         if (tr && this_cpu_read(tr->trace_buffer.data->ftrace_ignore_pid))
191                 return;
192
193         op->saved_func(ip, parent_ip, op, regs);
194 }
195
196 /**
197  * clear_ftrace_function - reset the ftrace function
198  *
199  * This NULLs the ftrace function and in essence stops
200  * tracing.  There may be lag
201  */
202 void clear_ftrace_function(void)
203 {
204         ftrace_trace_function = ftrace_stub;
205 }
206
207 static void per_cpu_ops_disable_all(struct ftrace_ops *ops)
208 {
209         int cpu;
210
211         for_each_possible_cpu(cpu)
212                 *per_cpu_ptr(ops->disabled, cpu) = 1;
213 }
214
215 static int per_cpu_ops_alloc(struct ftrace_ops *ops)
216 {
217         int __percpu *disabled;
218
219         if (WARN_ON_ONCE(!(ops->flags & FTRACE_OPS_FL_PER_CPU)))
220                 return -EINVAL;
221
222         disabled = alloc_percpu(int);
223         if (!disabled)
224                 return -ENOMEM;
225
226         ops->disabled = disabled;
227         per_cpu_ops_disable_all(ops);
228         return 0;
229 }
230
231 static void ftrace_sync(struct work_struct *work)
232 {
233         /*
234          * This function is just a stub to implement a hard force
235          * of synchronize_sched(). This requires synchronizing
236          * tasks even in userspace and idle.
237          *
238          * Yes, function tracing is rude.
239          */
240 }
241
242 static void ftrace_sync_ipi(void *data)
243 {
244         /* Probably not needed, but do it anyway */
245         smp_rmb();
246 }
247
248 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
249 static void update_function_graph_func(void);
250
251 /* Both enabled by default (can be cleared by function_graph tracer flags */
252 static bool fgraph_sleep_time = true;
253 static bool fgraph_graph_time = true;
254
255 #else
256 static inline void update_function_graph_func(void) { }
257 #endif
258
259
260 static ftrace_func_t ftrace_ops_get_list_func(struct ftrace_ops *ops)
261 {
262         /*
263          * If this is a dynamic, RCU, or per CPU ops, or we force list func,
264          * then it needs to call the list anyway.
265          */
266         if (ops->flags & (FTRACE_OPS_FL_DYNAMIC | FTRACE_OPS_FL_PER_CPU |
267                           FTRACE_OPS_FL_RCU) || FTRACE_FORCE_LIST_FUNC)
268                 return ftrace_ops_list_func;
269
270         return ftrace_ops_get_func(ops);
271 }
272
273 static void update_ftrace_function(void)
274 {
275         ftrace_func_t func;
276
277         /*
278          * Prepare the ftrace_ops that the arch callback will use.
279          * If there's only one ftrace_ops registered, the ftrace_ops_list
280          * will point to the ops we want.
281          */
282         set_function_trace_op = rcu_dereference_protected(ftrace_ops_list,
283                                                 lockdep_is_held(&ftrace_lock));
284
285         /* If there's no ftrace_ops registered, just call the stub function */
286         if (set_function_trace_op == &ftrace_list_end) {
287                 func = ftrace_stub;
288
289         /*
290          * If we are at the end of the list and this ops is
291          * recursion safe and not dynamic and the arch supports passing ops,
292          * then have the mcount trampoline call the function directly.
293          */
294         } else if (rcu_dereference_protected(ftrace_ops_list->next,
295                         lockdep_is_held(&ftrace_lock)) == &ftrace_list_end) {
296                 func = ftrace_ops_get_list_func(ftrace_ops_list);
297
298         } else {
299                 /* Just use the default ftrace_ops */
300                 set_function_trace_op = &ftrace_list_end;
301                 func = ftrace_ops_list_func;
302         }
303
304         update_function_graph_func();
305
306         /* If there's no change, then do nothing more here */
307         if (ftrace_trace_function == func)
308                 return;
309
310         /*
311          * If we are using the list function, it doesn't care
312          * about the function_trace_ops.
313          */
314         if (func == ftrace_ops_list_func) {
315                 ftrace_trace_function = func;
316                 /*
317                  * Don't even bother setting function_trace_ops,
318                  * it would be racy to do so anyway.
319                  */
320                 return;
321         }
322
323 #ifndef CONFIG_DYNAMIC_FTRACE
324         /*
325          * For static tracing, we need to be a bit more careful.
326          * The function change takes affect immediately. Thus,
327          * we need to coorditate the setting of the function_trace_ops
328          * with the setting of the ftrace_trace_function.
329          *
330          * Set the function to the list ops, which will call the
331          * function we want, albeit indirectly, but it handles the
332          * ftrace_ops and doesn't depend on function_trace_op.
333          */
334         ftrace_trace_function = ftrace_ops_list_func;
335         /*
336          * Make sure all CPUs see this. Yes this is slow, but static
337          * tracing is slow and nasty to have enabled.
338          */
339         schedule_on_each_cpu(ftrace_sync);
340         /* Now all cpus are using the list ops. */
341         function_trace_op = set_function_trace_op;
342         /* Make sure the function_trace_op is visible on all CPUs */
343         smp_wmb();
344         /* Nasty way to force a rmb on all cpus */
345         smp_call_function(ftrace_sync_ipi, NULL, 1);
346         /* OK, we are all set to update the ftrace_trace_function now! */
347 #endif /* !CONFIG_DYNAMIC_FTRACE */
348
349         ftrace_trace_function = func;
350 }
351
352 int using_ftrace_ops_list_func(void)
353 {
354         return ftrace_trace_function == ftrace_ops_list_func;
355 }
356
357 static void add_ftrace_ops(struct ftrace_ops __rcu **list,
358                            struct ftrace_ops *ops)
359 {
360         rcu_assign_pointer(ops->next, *list);
361
362         /*
363          * We are entering ops into the list but another
364          * CPU might be walking that list. We need to make sure
365          * the ops->next pointer is valid before another CPU sees
366          * the ops pointer included into the list.
367          */
368         rcu_assign_pointer(*list, ops);
369 }
370
371 static int remove_ftrace_ops(struct ftrace_ops __rcu **list,
372                              struct ftrace_ops *ops)
373 {
374         struct ftrace_ops **p;
375
376         /*
377          * If we are removing the last function, then simply point
378          * to the ftrace_stub.
379          */
380         if (rcu_dereference_protected(*list,
381                         lockdep_is_held(&ftrace_lock)) == ops &&
382             rcu_dereference_protected(ops->next,
383                         lockdep_is_held(&ftrace_lock)) == &ftrace_list_end) {
384                 *list = &ftrace_list_end;
385                 return 0;
386         }
387
388         for (p = list; *p != &ftrace_list_end; p = &(*p)->next)
389                 if (*p == ops)
390                         break;
391
392         if (*p != ops)
393                 return -1;
394
395         *p = (*p)->next;
396         return 0;
397 }
398
399 static void ftrace_update_trampoline(struct ftrace_ops *ops);
400
401 static int __register_ftrace_function(struct ftrace_ops *ops)
402 {
403         if (ops->flags & FTRACE_OPS_FL_DELETED)
404                 return -EINVAL;
405
406         if (WARN_ON(ops->flags & FTRACE_OPS_FL_ENABLED))
407                 return -EBUSY;
408
409 #ifndef CONFIG_DYNAMIC_FTRACE_WITH_REGS
410         /*
411          * If the ftrace_ops specifies SAVE_REGS, then it only can be used
412          * if the arch supports it, or SAVE_REGS_IF_SUPPORTED is also set.
413          * Setting SAVE_REGS_IF_SUPPORTED makes SAVE_REGS irrelevant.
414          */
415         if (ops->flags & FTRACE_OPS_FL_SAVE_REGS &&
416             !(ops->flags & FTRACE_OPS_FL_SAVE_REGS_IF_SUPPORTED))
417                 return -EINVAL;
418
419         if (ops->flags & FTRACE_OPS_FL_SAVE_REGS_IF_SUPPORTED)
420                 ops->flags |= FTRACE_OPS_FL_SAVE_REGS;
421 #endif
422
423         if (!core_kernel_data((unsigned long)ops))
424                 ops->flags |= FTRACE_OPS_FL_DYNAMIC;
425
426         if (ops->flags & FTRACE_OPS_FL_PER_CPU) {
427                 if (per_cpu_ops_alloc(ops))
428                         return -ENOMEM;
429         }
430
431         add_ftrace_ops(&ftrace_ops_list, ops);
432
433         /* Always save the function, and reset at unregistering */
434         ops->saved_func = ops->func;
435
436         if (ftrace_pids_enabled(ops))
437                 ops->func = ftrace_pid_func;
438
439         ftrace_update_trampoline(ops);
440
441         if (ftrace_enabled)
442                 update_ftrace_function();
443
444         return 0;
445 }
446
447 static int __unregister_ftrace_function(struct ftrace_ops *ops)
448 {
449         int ret;
450
451         if (WARN_ON(!(ops->flags & FTRACE_OPS_FL_ENABLED)))
452                 return -EBUSY;
453
454         ret = remove_ftrace_ops(&ftrace_ops_list, ops);
455
456         if (ret < 0)
457                 return ret;
458
459         if (ftrace_enabled)
460                 update_ftrace_function();
461
462         ops->func = ops->saved_func;
463
464         return 0;
465 }
466
467 static void ftrace_update_pid_func(void)
468 {
469         struct ftrace_ops *op;
470
471         /* Only do something if we are tracing something */
472         if (ftrace_trace_function == ftrace_stub)
473                 return;
474
475         do_for_each_ftrace_op(op, ftrace_ops_list) {
476                 if (op->flags & FTRACE_OPS_FL_PID) {
477                         op->func = ftrace_pids_enabled(op) ?
478                                 ftrace_pid_func : op->saved_func;
479                         ftrace_update_trampoline(op);
480                 }
481         } while_for_each_ftrace_op(op);
482
483         update_ftrace_function();
484 }
485
486 #ifdef CONFIG_FUNCTION_PROFILER
487 struct ftrace_profile {
488         struct hlist_node               node;
489         unsigned long                   ip;
490         unsigned long                   counter;
491 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
492         unsigned long long              time;
493         unsigned long long              time_squared;
494 #endif
495 };
496
497 struct ftrace_profile_page {
498         struct ftrace_profile_page      *next;
499         unsigned long                   index;
500         struct ftrace_profile           records[];
501 };
502
503 struct ftrace_profile_stat {
504         atomic_t                        disabled;
505         struct hlist_head               *hash;
506         struct ftrace_profile_page      *pages;
507         struct ftrace_profile_page      *start;
508         struct tracer_stat              stat;
509 };
510
511 #define PROFILE_RECORDS_SIZE                                            \
512         (PAGE_SIZE - offsetof(struct ftrace_profile_page, records))
513
514 #define PROFILES_PER_PAGE                                       \
515         (PROFILE_RECORDS_SIZE / sizeof(struct ftrace_profile))
516
517 static int ftrace_profile_enabled __read_mostly;
518
519 /* ftrace_profile_lock - synchronize the enable and disable of the profiler */
520 static DEFINE_MUTEX(ftrace_profile_lock);
521
522 static DEFINE_PER_CPU(struct ftrace_profile_stat, ftrace_profile_stats);
523
524 #define FTRACE_PROFILE_HASH_BITS 10
525 #define FTRACE_PROFILE_HASH_SIZE (1 << FTRACE_PROFILE_HASH_BITS)
526
527 static void *
528 function_stat_next(void *v, int idx)
529 {
530         struct ftrace_profile *rec = v;
531         struct ftrace_profile_page *pg;
532
533         pg = (struct ftrace_profile_page *)((unsigned long)rec & PAGE_MASK);
534
535  again:
536         if (idx != 0)
537                 rec++;
538
539         if ((void *)rec >= (void *)&pg->records[pg->index]) {
540                 pg = pg->next;
541                 if (!pg)
542                         return NULL;
543                 rec = &pg->records[0];
544                 if (!rec->counter)
545                         goto again;
546         }
547
548         return rec;
549 }
550
551 static void *function_stat_start(struct tracer_stat *trace)
552 {
553         struct ftrace_profile_stat *stat =
554                 container_of(trace, struct ftrace_profile_stat, stat);
555
556         if (!stat || !stat->start)
557                 return NULL;
558
559         return function_stat_next(&stat->start->records[0], 0);
560 }
561
562 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
563 /* function graph compares on total time */
564 static int function_stat_cmp(void *p1, void *p2)
565 {
566         struct ftrace_profile *a = p1;
567         struct ftrace_profile *b = p2;
568
569         if (a->time < b->time)
570                 return -1;
571         if (a->time > b->time)
572                 return 1;
573         else
574                 return 0;
575 }
576 #else
577 /* not function graph compares against hits */
578 static int function_stat_cmp(void *p1, void *p2)
579 {
580         struct ftrace_profile *a = p1;
581         struct ftrace_profile *b = p2;
582
583         if (a->counter < b->counter)
584                 return -1;
585         if (a->counter > b->counter)
586                 return 1;
587         else
588                 return 0;
589 }
590 #endif
591
592 static int function_stat_headers(struct seq_file *m)
593 {
594 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
595         seq_puts(m, "  Function                               "
596                  "Hit    Time            Avg             s^2\n"
597                     "  --------                               "
598                  "---    ----            ---             ---\n");
599 #else
600         seq_puts(m, "  Function                               Hit\n"
601                     "  --------                               ---\n");
602 #endif
603         return 0;
604 }
605
606 static int function_stat_show(struct seq_file *m, void *v)
607 {
608         struct ftrace_profile *rec = v;
609         char str[KSYM_SYMBOL_LEN];
610         int ret = 0;
611 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
612         static struct trace_seq s;
613         unsigned long long avg;
614         unsigned long long stddev;
615 #endif
616         mutex_lock(&ftrace_profile_lock);
617
618         /* we raced with function_profile_reset() */
619         if (unlikely(rec->counter == 0)) {
620                 ret = -EBUSY;
621                 goto out;
622         }
623
624 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
625         avg = div64_ul(rec->time, rec->counter);
626         if (tracing_thresh && (avg < tracing_thresh))
627                 goto out;
628 #endif
629
630         kallsyms_lookup(rec->ip, NULL, NULL, NULL, str);
631         seq_printf(m, "  %-30.30s  %10lu", str, rec->counter);
632
633 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
634         seq_puts(m, "    ");
635
636         /* Sample standard deviation (s^2) */
637         if (rec->counter <= 1)
638                 stddev = 0;
639         else {
640                 /*
641                  * Apply Welford's method:
642                  * s^2 = 1 / (n * (n-1)) * (n * \Sum (x_i)^2 - (\Sum x_i)^2)
643                  */
644                 stddev = rec->counter * rec->time_squared -
645                          rec->time * rec->time;
646
647                 /*
648                  * Divide only 1000 for ns^2 -> us^2 conversion.
649                  * trace_print_graph_duration will divide 1000 again.
650                  */
651                 stddev = div64_ul(stddev,
652                                   rec->counter * (rec->counter - 1) * 1000);
653         }
654
655         trace_seq_init(&s);
656         trace_print_graph_duration(rec->time, &s);
657         trace_seq_puts(&s, "    ");
658         trace_print_graph_duration(avg, &s);
659         trace_seq_puts(&s, "    ");
660         trace_print_graph_duration(stddev, &s);
661         trace_print_seq(m, &s);
662 #endif
663         seq_putc(m, '\n');
664 out:
665         mutex_unlock(&ftrace_profile_lock);
666
667         return ret;
668 }
669
670 static void ftrace_profile_reset(struct ftrace_profile_stat *stat)
671 {
672         struct ftrace_profile_page *pg;
673
674         pg = stat->pages = stat->start;
675
676         while (pg) {
677                 memset(pg->records, 0, PROFILE_RECORDS_SIZE);
678                 pg->index = 0;
679                 pg = pg->next;
680         }
681
682         memset(stat->hash, 0,
683                FTRACE_PROFILE_HASH_SIZE * sizeof(struct hlist_head));
684 }
685
686 int ftrace_profile_pages_init(struct ftrace_profile_stat *stat)
687 {
688         struct ftrace_profile_page *pg;
689         int functions;
690         int pages;
691         int i;
692
693         /* If we already allocated, do nothing */
694         if (stat->pages)
695                 return 0;
696
697         stat->pages = (void *)get_zeroed_page(GFP_KERNEL);
698         if (!stat->pages)
699                 return -ENOMEM;
700
701 #ifdef CONFIG_DYNAMIC_FTRACE
702         functions = ftrace_update_tot_cnt;
703 #else
704         /*
705          * We do not know the number of functions that exist because
706          * dynamic tracing is what counts them. With past experience
707          * we have around 20K functions. That should be more than enough.
708          * It is highly unlikely we will execute every function in
709          * the kernel.
710          */
711         functions = 20000;
712 #endif
713
714         pg = stat->start = stat->pages;
715
716         pages = DIV_ROUND_UP(functions, PROFILES_PER_PAGE);
717
718         for (i = 1; i < pages; i++) {
719                 pg->next = (void *)get_zeroed_page(GFP_KERNEL);
720                 if (!pg->next)
721                         goto out_free;
722                 pg = pg->next;
723         }
724
725         return 0;
726
727  out_free:
728         pg = stat->start;
729         while (pg) {
730                 unsigned long tmp = (unsigned long)pg;
731
732                 pg = pg->next;
733                 free_page(tmp);
734         }
735
736         stat->pages = NULL;
737         stat->start = NULL;
738
739         return -ENOMEM;
740 }
741
742 static int ftrace_profile_init_cpu(int cpu)
743 {
744         struct ftrace_profile_stat *stat;
745         int size;
746
747         stat = &per_cpu(ftrace_profile_stats, cpu);
748
749         if (stat->hash) {
750                 /* If the profile is already created, simply reset it */
751                 ftrace_profile_reset(stat);
752                 return 0;
753         }
754
755         /*
756          * We are profiling all functions, but usually only a few thousand
757          * functions are hit. We'll make a hash of 1024 items.
758          */
759         size = FTRACE_PROFILE_HASH_SIZE;
760
761         stat->hash = kzalloc(sizeof(struct hlist_head) * size, GFP_KERNEL);
762
763         if (!stat->hash)
764                 return -ENOMEM;
765
766         /* Preallocate the function profiling pages */
767         if (ftrace_profile_pages_init(stat) < 0) {
768                 kfree(stat->hash);
769                 stat->hash = NULL;
770                 return -ENOMEM;
771         }
772
773         return 0;
774 }
775
776 static int ftrace_profile_init(void)
777 {
778         int cpu;
779         int ret = 0;
780
781         for_each_possible_cpu(cpu) {
782                 ret = ftrace_profile_init_cpu(cpu);
783                 if (ret)
784                         break;
785         }
786
787         return ret;
788 }
789
790 /* interrupts must be disabled */
791 static struct ftrace_profile *
792 ftrace_find_profiled_func(struct ftrace_profile_stat *stat, unsigned long ip)
793 {
794         struct ftrace_profile *rec;
795         struct hlist_head *hhd;
796         unsigned long key;
797
798         key = hash_long(ip, FTRACE_PROFILE_HASH_BITS);
799         hhd = &stat->hash[key];
800
801         if (hlist_empty(hhd))
802                 return NULL;
803
804         hlist_for_each_entry_rcu_notrace(rec, hhd, node) {
805                 if (rec->ip == ip)
806                         return rec;
807         }
808
809         return NULL;
810 }
811
812 static void ftrace_add_profile(struct ftrace_profile_stat *stat,
813                                struct ftrace_profile *rec)
814 {
815         unsigned long key;
816
817         key = hash_long(rec->ip, FTRACE_PROFILE_HASH_BITS);
818         hlist_add_head_rcu(&rec->node, &stat->hash[key]);
819 }
820
821 /*
822  * The memory is already allocated, this simply finds a new record to use.
823  */
824 static struct ftrace_profile *
825 ftrace_profile_alloc(struct ftrace_profile_stat *stat, unsigned long ip)
826 {
827         struct ftrace_profile *rec = NULL;
828
829         /* prevent recursion (from NMIs) */
830         if (atomic_inc_return(&stat->disabled) != 1)
831                 goto out;
832
833         /*
834          * Try to find the function again since an NMI
835          * could have added it
836          */
837         rec = ftrace_find_profiled_func(stat, ip);
838         if (rec)
839                 goto out;
840
841         if (stat->pages->index == PROFILES_PER_PAGE) {
842                 if (!stat->pages->next)
843                         goto out;
844                 stat->pages = stat->pages->next;
845         }
846
847         rec = &stat->pages->records[stat->pages->index++];
848         rec->ip = ip;
849         ftrace_add_profile(stat, rec);
850
851  out:
852         atomic_dec(&stat->disabled);
853
854         return rec;
855 }
856
857 static void
858 function_profile_call(unsigned long ip, unsigned long parent_ip,
859                       struct ftrace_ops *ops, struct pt_regs *regs)
860 {
861         struct ftrace_profile_stat *stat;
862         struct ftrace_profile *rec;
863         unsigned long flags;
864
865         if (!ftrace_profile_enabled)
866                 return;
867
868         local_irq_save(flags);
869
870         stat = this_cpu_ptr(&ftrace_profile_stats);
871         if (!stat->hash || !ftrace_profile_enabled)
872                 goto out;
873
874         rec = ftrace_find_profiled_func(stat, ip);
875         if (!rec) {
876                 rec = ftrace_profile_alloc(stat, ip);
877                 if (!rec)
878                         goto out;
879         }
880
881         rec->counter++;
882  out:
883         local_irq_restore(flags);
884 }
885
886 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
887 static int profile_graph_entry(struct ftrace_graph_ent *trace)
888 {
889         int index = trace->depth;
890
891         function_profile_call(trace->func, 0, NULL, NULL);
892
893         /* If function graph is shutting down, ret_stack can be NULL */
894         if (!current->ret_stack)
895                 return 0;
896
897         if (index >= 0 && index < FTRACE_RETFUNC_DEPTH)
898                 current->ret_stack[index].subtime = 0;
899
900         return 1;
901 }
902
903 static void profile_graph_return(struct ftrace_graph_ret *trace)
904 {
905         struct ftrace_profile_stat *stat;
906         unsigned long long calltime;
907         struct ftrace_profile *rec;
908         unsigned long flags;
909
910         local_irq_save(flags);
911         stat = this_cpu_ptr(&ftrace_profile_stats);
912         if (!stat->hash || !ftrace_profile_enabled)
913                 goto out;
914
915         /* If the calltime was zero'd ignore it */
916         if (!trace->calltime)
917                 goto out;
918
919         calltime = trace->rettime - trace->calltime;
920
921         if (!fgraph_graph_time) {
922                 int index;
923
924                 index = trace->depth;
925
926                 /* Append this call time to the parent time to subtract */
927                 if (index)
928                         current->ret_stack[index - 1].subtime += calltime;
929
930                 if (current->ret_stack[index].subtime < calltime)
931                         calltime -= current->ret_stack[index].subtime;
932                 else
933                         calltime = 0;
934         }
935
936         rec = ftrace_find_profiled_func(stat, trace->func);
937         if (rec) {
938                 rec->time += calltime;
939                 rec->time_squared += calltime * calltime;
940         }
941
942  out:
943         local_irq_restore(flags);
944 }
945
946 static int register_ftrace_profiler(void)
947 {
948         return register_ftrace_graph(&profile_graph_return,
949                                      &profile_graph_entry);
950 }
951
952 static void unregister_ftrace_profiler(void)
953 {
954         unregister_ftrace_graph();
955 }
956 #else
957 static struct ftrace_ops ftrace_profile_ops __read_mostly = {
958         .func           = function_profile_call,
959         .flags          = FTRACE_OPS_FL_RECURSION_SAFE | FTRACE_OPS_FL_INITIALIZED,
960         INIT_OPS_HASH(ftrace_profile_ops)
961 };
962
963 static int register_ftrace_profiler(void)
964 {
965         return register_ftrace_function(&ftrace_profile_ops);
966 }
967
968 static void unregister_ftrace_profiler(void)
969 {
970         unregister_ftrace_function(&ftrace_profile_ops);
971 }
972 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
973
974 static ssize_t
975 ftrace_profile_write(struct file *filp, const char __user *ubuf,
976                      size_t cnt, loff_t *ppos)
977 {
978         unsigned long val;
979         int ret;
980
981         ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
982         if (ret)
983                 return ret;
984
985         val = !!val;
986
987         mutex_lock(&ftrace_profile_lock);
988         if (ftrace_profile_enabled ^ val) {
989                 if (val) {
990                         ret = ftrace_profile_init();
991                         if (ret < 0) {
992                                 cnt = ret;
993                                 goto out;
994                         }
995
996                         ret = register_ftrace_profiler();
997                         if (ret < 0) {
998                                 cnt = ret;
999                                 goto out;
1000                         }
1001                         ftrace_profile_enabled = 1;
1002                 } else {
1003                         ftrace_profile_enabled = 0;
1004                         /*
1005                          * unregister_ftrace_profiler calls stop_machine
1006                          * so this acts like an synchronize_sched.
1007                          */
1008                         unregister_ftrace_profiler();
1009                 }
1010         }
1011  out:
1012         mutex_unlock(&ftrace_profile_lock);
1013
1014         *ppos += cnt;
1015
1016         return cnt;
1017 }
1018
1019 static ssize_t
1020 ftrace_profile_read(struct file *filp, char __user *ubuf,
1021                      size_t cnt, loff_t *ppos)
1022 {
1023         char buf[64];           /* big enough to hold a number */
1024         int r;
1025
1026         r = sprintf(buf, "%u\n", ftrace_profile_enabled);
1027         return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
1028 }
1029
1030 static const struct file_operations ftrace_profile_fops = {
1031         .open           = tracing_open_generic,
1032         .read           = ftrace_profile_read,
1033         .write          = ftrace_profile_write,
1034         .llseek         = default_llseek,
1035 };
1036
1037 /* used to initialize the real stat files */
1038 static struct tracer_stat function_stats __initdata = {
1039         .name           = "functions",
1040         .stat_start     = function_stat_start,
1041         .stat_next      = function_stat_next,
1042         .stat_cmp       = function_stat_cmp,
1043         .stat_headers   = function_stat_headers,
1044         .stat_show      = function_stat_show
1045 };
1046
1047 static __init void ftrace_profile_tracefs(struct dentry *d_tracer)
1048 {
1049         struct ftrace_profile_stat *stat;
1050         struct dentry *entry;
1051         char *name;
1052         int ret;
1053         int cpu;
1054
1055         for_each_possible_cpu(cpu) {
1056                 stat = &per_cpu(ftrace_profile_stats, cpu);
1057
1058                 name = kasprintf(GFP_KERNEL, "function%d", cpu);
1059                 if (!name) {
1060                         /*
1061                          * The files created are permanent, if something happens
1062                          * we still do not free memory.
1063                          */
1064                         WARN(1,
1065                              "Could not allocate stat file for cpu %d\n",
1066                              cpu);
1067                         return;
1068                 }
1069                 stat->stat = function_stats;
1070                 stat->stat.name = name;
1071                 ret = register_stat_tracer(&stat->stat);
1072                 if (ret) {
1073                         WARN(1,
1074                              "Could not register function stat for cpu %d\n",
1075                              cpu);
1076                         kfree(name);
1077                         return;
1078                 }
1079         }
1080
1081         entry = tracefs_create_file("function_profile_enabled", 0644,
1082                                     d_tracer, NULL, &ftrace_profile_fops);
1083         if (!entry)
1084                 pr_warn("Could not create tracefs 'function_profile_enabled' entry\n");
1085 }
1086
1087 #else /* CONFIG_FUNCTION_PROFILER */
1088 static __init void ftrace_profile_tracefs(struct dentry *d_tracer)
1089 {
1090 }
1091 #endif /* CONFIG_FUNCTION_PROFILER */
1092
1093 static struct pid * const ftrace_swapper_pid = &init_struct_pid;
1094
1095 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
1096 static int ftrace_graph_active;
1097 #else
1098 # define ftrace_graph_active 0
1099 #endif
1100
1101 #ifdef CONFIG_DYNAMIC_FTRACE
1102
1103 static struct ftrace_ops *removed_ops;
1104
1105 /*
1106  * Set when doing a global update, like enabling all recs or disabling them.
1107  * It is not set when just updating a single ftrace_ops.
1108  */
1109 static bool update_all_ops;
1110
1111 #ifndef CONFIG_FTRACE_MCOUNT_RECORD
1112 # error Dynamic ftrace depends on MCOUNT_RECORD
1113 #endif
1114
1115 struct ftrace_func_entry {
1116         struct hlist_node hlist;
1117         unsigned long ip;
1118 };
1119
1120 struct ftrace_func_probe {
1121         struct ftrace_probe_ops *probe_ops;
1122         struct ftrace_ops       ops;
1123         struct trace_array      *tr;
1124         struct list_head        list;
1125         void                    *data;
1126         int                     ref;
1127 };
1128
1129 /*
1130  * We make these constant because no one should touch them,
1131  * but they are used as the default "empty hash", to avoid allocating
1132  * it all the time. These are in a read only section such that if
1133  * anyone does try to modify it, it will cause an exception.
1134  */
1135 static const struct hlist_head empty_buckets[1];
1136 static const struct ftrace_hash empty_hash = {
1137         .buckets = (struct hlist_head *)empty_buckets,
1138 };
1139 #define EMPTY_HASH      ((struct ftrace_hash *)&empty_hash)
1140
1141 static struct ftrace_ops global_ops = {
1142         .func                           = ftrace_stub,
1143         .local_hash.notrace_hash        = EMPTY_HASH,
1144         .local_hash.filter_hash         = EMPTY_HASH,
1145         INIT_OPS_HASH(global_ops)
1146         .flags                          = FTRACE_OPS_FL_RECURSION_SAFE |
1147                                           FTRACE_OPS_FL_INITIALIZED |
1148                                           FTRACE_OPS_FL_PID,
1149 };
1150
1151 /*
1152  * This is used by __kernel_text_address() to return true if the
1153  * address is on a dynamically allocated trampoline that would
1154  * not return true for either core_kernel_text() or
1155  * is_module_text_address().
1156  */
1157 bool is_ftrace_trampoline(unsigned long addr)
1158 {
1159         struct ftrace_ops *op;
1160         bool ret = false;
1161
1162         /*
1163          * Some of the ops may be dynamically allocated,
1164          * they are freed after a synchronize_sched().
1165          */
1166         preempt_disable_notrace();
1167
1168         do_for_each_ftrace_op(op, ftrace_ops_list) {
1169                 /*
1170                  * This is to check for dynamically allocated trampolines.
1171                  * Trampolines that are in kernel text will have
1172                  * core_kernel_text() return true.
1173                  */
1174                 if (op->trampoline && op->trampoline_size)
1175                         if (addr >= op->trampoline &&
1176                             addr < op->trampoline + op->trampoline_size) {
1177                                 ret = true;
1178                                 goto out;
1179                         }
1180         } while_for_each_ftrace_op(op);
1181
1182  out:
1183         preempt_enable_notrace();
1184
1185         return ret;
1186 }
1187
1188 struct ftrace_page {
1189         struct ftrace_page      *next;
1190         struct dyn_ftrace       *records;
1191         int                     index;
1192         int                     size;
1193 };
1194
1195 #define ENTRY_SIZE sizeof(struct dyn_ftrace)
1196 #define ENTRIES_PER_PAGE (PAGE_SIZE / ENTRY_SIZE)
1197
1198 /* estimate from running different kernels */
1199 #define NR_TO_INIT              10000
1200
1201 static struct ftrace_page       *ftrace_pages_start;
1202 static struct ftrace_page       *ftrace_pages;
1203
1204 static __always_inline unsigned long
1205 ftrace_hash_key(struct ftrace_hash *hash, unsigned long ip)
1206 {
1207         if (hash->size_bits > 0)
1208                 return hash_long(ip, hash->size_bits);
1209
1210         return 0;
1211 }
1212
1213 /* Only use this function if ftrace_hash_empty() has already been tested */
1214 static __always_inline struct ftrace_func_entry *
1215 __ftrace_lookup_ip(struct ftrace_hash *hash, unsigned long ip)
1216 {
1217         unsigned long key;
1218         struct ftrace_func_entry *entry;
1219         struct hlist_head *hhd;
1220
1221         key = ftrace_hash_key(hash, ip);
1222         hhd = &hash->buckets[key];
1223
1224         hlist_for_each_entry_rcu_notrace(entry, hhd, hlist) {
1225                 if (entry->ip == ip)
1226                         return entry;
1227         }
1228         return NULL;
1229 }
1230
1231 /**
1232  * ftrace_lookup_ip - Test to see if an ip exists in an ftrace_hash
1233  * @hash: The hash to look at
1234  * @ip: The instruction pointer to test
1235  *
1236  * Search a given @hash to see if a given instruction pointer (@ip)
1237  * exists in it.
1238  *
1239  * Returns the entry that holds the @ip if found. NULL otherwise.
1240  */
1241 struct ftrace_func_entry *
1242 ftrace_lookup_ip(struct ftrace_hash *hash, unsigned long ip)
1243 {
1244         if (ftrace_hash_empty(hash))
1245                 return NULL;
1246
1247         return __ftrace_lookup_ip(hash, ip);
1248 }
1249
1250 static void __add_hash_entry(struct ftrace_hash *hash,
1251                              struct ftrace_func_entry *entry)
1252 {
1253         struct hlist_head *hhd;
1254         unsigned long key;
1255
1256         key = ftrace_hash_key(hash, entry->ip);
1257         hhd = &hash->buckets[key];
1258         hlist_add_head(&entry->hlist, hhd);
1259         hash->count++;
1260 }
1261
1262 static int add_hash_entry(struct ftrace_hash *hash, unsigned long ip)
1263 {
1264         struct ftrace_func_entry *entry;
1265
1266         entry = kmalloc(sizeof(*entry), GFP_KERNEL);
1267         if (!entry)
1268                 return -ENOMEM;
1269
1270         entry->ip = ip;
1271         __add_hash_entry(hash, entry);
1272
1273         return 0;
1274 }
1275
1276 static void
1277 free_hash_entry(struct ftrace_hash *hash,
1278                   struct ftrace_func_entry *entry)
1279 {
1280         hlist_del(&entry->hlist);
1281         kfree(entry);
1282         hash->count--;
1283 }
1284
1285 static void
1286 remove_hash_entry(struct ftrace_hash *hash,
1287                   struct ftrace_func_entry *entry)
1288 {
1289         hlist_del_rcu(&entry->hlist);
1290         hash->count--;
1291 }
1292
1293 static void ftrace_hash_clear(struct ftrace_hash *hash)
1294 {
1295         struct hlist_head *hhd;
1296         struct hlist_node *tn;
1297         struct ftrace_func_entry *entry;
1298         int size = 1 << hash->size_bits;
1299         int i;
1300
1301         if (!hash->count)
1302                 return;
1303
1304         for (i = 0; i < size; i++) {
1305                 hhd = &hash->buckets[i];
1306                 hlist_for_each_entry_safe(entry, tn, hhd, hlist)
1307                         free_hash_entry(hash, entry);
1308         }
1309         FTRACE_WARN_ON(hash->count);
1310 }
1311
1312 static void free_ftrace_mod(struct ftrace_mod_load *ftrace_mod)
1313 {
1314         list_del(&ftrace_mod->list);
1315         kfree(ftrace_mod->module);
1316         kfree(ftrace_mod->func);
1317         kfree(ftrace_mod);
1318 }
1319
1320 static void clear_ftrace_mod_list(struct list_head *head)
1321 {
1322         struct ftrace_mod_load *p, *n;
1323
1324         /* stack tracer isn't supported yet */
1325         if (!head)
1326                 return;
1327
1328         mutex_lock(&ftrace_lock);
1329         list_for_each_entry_safe(p, n, head, list)
1330                 free_ftrace_mod(p);
1331         mutex_unlock(&ftrace_lock);
1332 }
1333
1334 static void free_ftrace_hash(struct ftrace_hash *hash)
1335 {
1336         if (!hash || hash == EMPTY_HASH)
1337                 return;
1338         ftrace_hash_clear(hash);
1339         kfree(hash->buckets);
1340         kfree(hash);
1341 }
1342
1343 static void __free_ftrace_hash_rcu(struct rcu_head *rcu)
1344 {
1345         struct ftrace_hash *hash;
1346
1347         hash = container_of(rcu, struct ftrace_hash, rcu);
1348         free_ftrace_hash(hash);
1349 }
1350
1351 static void free_ftrace_hash_rcu(struct ftrace_hash *hash)
1352 {
1353         if (!hash || hash == EMPTY_HASH)
1354                 return;
1355         call_rcu_sched(&hash->rcu, __free_ftrace_hash_rcu);
1356 }
1357
1358 void ftrace_free_filter(struct ftrace_ops *ops)
1359 {
1360         ftrace_ops_init(ops);
1361         free_ftrace_hash(ops->func_hash->filter_hash);
1362         free_ftrace_hash(ops->func_hash->notrace_hash);
1363 }
1364
1365 static struct ftrace_hash *alloc_ftrace_hash(int size_bits)
1366 {
1367         struct ftrace_hash *hash;
1368         int size;
1369
1370         hash = kzalloc(sizeof(*hash), GFP_KERNEL);
1371         if (!hash)
1372                 return NULL;
1373
1374         size = 1 << size_bits;
1375         hash->buckets = kcalloc(size, sizeof(*hash->buckets), GFP_KERNEL);
1376
1377         if (!hash->buckets) {
1378                 kfree(hash);
1379                 return NULL;
1380         }
1381
1382         hash->size_bits = size_bits;
1383
1384         return hash;
1385 }
1386
1387
1388 static int ftrace_add_mod(struct trace_array *tr,
1389                           const char *func, const char *module,
1390                           int enable)
1391 {
1392         struct ftrace_mod_load *ftrace_mod;
1393         struct list_head *mod_head = enable ? &tr->mod_trace : &tr->mod_notrace;
1394
1395         ftrace_mod = kzalloc(sizeof(*ftrace_mod), GFP_KERNEL);
1396         if (!ftrace_mod)
1397                 return -ENOMEM;
1398
1399         INIT_LIST_HEAD(&ftrace_mod->list);
1400         ftrace_mod->func = kstrdup(func, GFP_KERNEL);
1401         ftrace_mod->module = kstrdup(module, GFP_KERNEL);
1402         ftrace_mod->enable = enable;
1403
1404         if (!ftrace_mod->func || !ftrace_mod->module)
1405                 goto out_free;
1406
1407         list_add(&ftrace_mod->list, mod_head);
1408
1409         return 0;
1410
1411  out_free:
1412         free_ftrace_mod(ftrace_mod);
1413
1414         return -ENOMEM;
1415 }
1416
1417 static struct ftrace_hash *
1418 alloc_and_copy_ftrace_hash(int size_bits, struct ftrace_hash *hash)
1419 {
1420         struct ftrace_func_entry *entry;
1421         struct ftrace_hash *new_hash;
1422         int size;
1423         int ret;
1424         int i;
1425
1426         new_hash = alloc_ftrace_hash(size_bits);
1427         if (!new_hash)
1428                 return NULL;
1429
1430         if (hash)
1431                 new_hash->flags = hash->flags;
1432
1433         /* Empty hash? */
1434         if (ftrace_hash_empty(hash))
1435                 return new_hash;
1436
1437         size = 1 << hash->size_bits;
1438         for (i = 0; i < size; i++) {
1439                 hlist_for_each_entry(entry, &hash->buckets[i], hlist) {
1440                         ret = add_hash_entry(new_hash, entry->ip);
1441                         if (ret < 0)
1442                                 goto free_hash;
1443                 }
1444         }
1445
1446         FTRACE_WARN_ON(new_hash->count != hash->count);
1447
1448         return new_hash;
1449
1450  free_hash:
1451         free_ftrace_hash(new_hash);
1452         return NULL;
1453 }
1454
1455 static void
1456 ftrace_hash_rec_disable_modify(struct ftrace_ops *ops, int filter_hash);
1457 static void
1458 ftrace_hash_rec_enable_modify(struct ftrace_ops *ops, int filter_hash);
1459
1460 static int ftrace_hash_ipmodify_update(struct ftrace_ops *ops,
1461                                        struct ftrace_hash *new_hash);
1462
1463 static struct ftrace_hash *
1464 __ftrace_hash_move(struct ftrace_hash *src)
1465 {
1466         struct ftrace_func_entry *entry;
1467         struct hlist_node *tn;
1468         struct hlist_head *hhd;
1469         struct ftrace_hash *new_hash;
1470         int size = src->count;
1471         int bits = 0;
1472         int i;
1473
1474         /*
1475          * If the new source is empty, just return the empty_hash.
1476          */
1477         if (ftrace_hash_empty(src))
1478                 return EMPTY_HASH;
1479
1480         /*
1481          * Make the hash size about 1/2 the # found
1482          */
1483         for (size /= 2; size; size >>= 1)
1484                 bits++;
1485
1486         /* Don't allocate too much */
1487         if (bits > FTRACE_HASH_MAX_BITS)
1488                 bits = FTRACE_HASH_MAX_BITS;
1489
1490         new_hash = alloc_ftrace_hash(bits);
1491         if (!new_hash)
1492                 return NULL;
1493
1494         new_hash->flags = src->flags;
1495
1496         size = 1 << src->size_bits;
1497         for (i = 0; i < size; i++) {
1498                 hhd = &src->buckets[i];
1499                 hlist_for_each_entry_safe(entry, tn, hhd, hlist) {
1500                         remove_hash_entry(src, entry);
1501                         __add_hash_entry(new_hash, entry);
1502                 }
1503         }
1504
1505         return new_hash;
1506 }
1507
1508 static int
1509 ftrace_hash_move(struct ftrace_ops *ops, int enable,
1510                  struct ftrace_hash **dst, struct ftrace_hash *src)
1511 {
1512         struct ftrace_hash *new_hash;
1513         int ret;
1514
1515         /* Reject setting notrace hash on IPMODIFY ftrace_ops */
1516         if (ops->flags & FTRACE_OPS_FL_IPMODIFY && !enable)
1517                 return -EINVAL;
1518
1519         new_hash = __ftrace_hash_move(src);
1520         if (!new_hash)
1521                 return -ENOMEM;
1522
1523         /* Make sure this can be applied if it is IPMODIFY ftrace_ops */
1524         if (enable) {
1525                 /* IPMODIFY should be updated only when filter_hash updating */
1526                 ret = ftrace_hash_ipmodify_update(ops, new_hash);
1527                 if (ret < 0) {
1528                         free_ftrace_hash(new_hash);
1529                         return ret;
1530                 }
1531         }
1532
1533         /*
1534          * Remove the current set, update the hash and add
1535          * them back.
1536          */
1537         ftrace_hash_rec_disable_modify(ops, enable);
1538
1539         rcu_assign_pointer(*dst, new_hash);
1540
1541         ftrace_hash_rec_enable_modify(ops, enable);
1542
1543         return 0;
1544 }
1545
1546 static bool hash_contains_ip(unsigned long ip,
1547                              struct ftrace_ops_hash *hash)
1548 {
1549         /*
1550          * The function record is a match if it exists in the filter
1551          * hash and not in the notrace hash. Note, an emty hash is
1552          * considered a match for the filter hash, but an empty
1553          * notrace hash is considered not in the notrace hash.
1554          */
1555         return (ftrace_hash_empty(hash->filter_hash) ||
1556                 __ftrace_lookup_ip(hash->filter_hash, ip)) &&
1557                 (ftrace_hash_empty(hash->notrace_hash) ||
1558                  !__ftrace_lookup_ip(hash->notrace_hash, ip));
1559 }
1560
1561 /*
1562  * Test the hashes for this ops to see if we want to call
1563  * the ops->func or not.
1564  *
1565  * It's a match if the ip is in the ops->filter_hash or
1566  * the filter_hash does not exist or is empty,
1567  *  AND
1568  * the ip is not in the ops->notrace_hash.
1569  *
1570  * This needs to be called with preemption disabled as
1571  * the hashes are freed with call_rcu_sched().
1572  */
1573 static int
1574 ftrace_ops_test(struct ftrace_ops *ops, unsigned long ip, void *regs)
1575 {
1576         struct ftrace_ops_hash hash;
1577         int ret;
1578
1579 #ifdef CONFIG_DYNAMIC_FTRACE_WITH_REGS
1580         /*
1581          * There's a small race when adding ops that the ftrace handler
1582          * that wants regs, may be called without them. We can not
1583          * allow that handler to be called if regs is NULL.
1584          */
1585         if (regs == NULL && (ops->flags & FTRACE_OPS_FL_SAVE_REGS))
1586                 return 0;
1587 #endif
1588
1589         rcu_assign_pointer(hash.filter_hash, ops->func_hash->filter_hash);
1590         rcu_assign_pointer(hash.notrace_hash, ops->func_hash->notrace_hash);
1591
1592         if (hash_contains_ip(ip, &hash))
1593                 ret = 1;
1594         else
1595                 ret = 0;
1596
1597         return ret;
1598 }
1599
1600 /*
1601  * This is a double for. Do not use 'break' to break out of the loop,
1602  * you must use a goto.
1603  */
1604 #define do_for_each_ftrace_rec(pg, rec)                                 \
1605         for (pg = ftrace_pages_start; pg; pg = pg->next) {              \
1606                 int _____i;                                             \
1607                 for (_____i = 0; _____i < pg->index; _____i++) {        \
1608                         rec = &pg->records[_____i];
1609
1610 #define while_for_each_ftrace_rec()             \
1611                 }                               \
1612         }
1613
1614
1615 static int ftrace_cmp_recs(const void *a, const void *b)
1616 {
1617         const struct dyn_ftrace *key = a;
1618         const struct dyn_ftrace *rec = b;
1619
1620         if (key->flags < rec->ip)
1621                 return -1;
1622         if (key->ip >= rec->ip + MCOUNT_INSN_SIZE)
1623                 return 1;
1624         return 0;
1625 }
1626
1627 /**
1628  * ftrace_location_range - return the first address of a traced location
1629  *      if it touches the given ip range
1630  * @start: start of range to search.
1631  * @end: end of range to search (inclusive). @end points to the last byte
1632  *      to check.
1633  *
1634  * Returns rec->ip if the related ftrace location is a least partly within
1635  * the given address range. That is, the first address of the instruction
1636  * that is either a NOP or call to the function tracer. It checks the ftrace
1637  * internal tables to determine if the address belongs or not.
1638  */
1639 unsigned long ftrace_location_range(unsigned long start, unsigned long end)
1640 {
1641         struct ftrace_page *pg;
1642         struct dyn_ftrace *rec;
1643         struct dyn_ftrace key;
1644
1645         key.ip = start;
1646         key.flags = end;        /* overload flags, as it is unsigned long */
1647
1648         for (pg = ftrace_pages_start; pg; pg = pg->next) {
1649                 if (pg->index == 0 ||
1650                     end < pg->records[0].ip ||
1651                     start >= (pg->records[pg->index - 1].ip + MCOUNT_INSN_SIZE))
1652                         continue;
1653                 rec = bsearch(&key, pg->records, pg->index,
1654                               sizeof(struct dyn_ftrace),
1655                               ftrace_cmp_recs);
1656                 if (rec)
1657                         return rec->ip;
1658         }
1659
1660         return 0;
1661 }
1662
1663 /**
1664  * ftrace_location - return true if the ip giving is a traced location
1665  * @ip: the instruction pointer to check
1666  *
1667  * Returns rec->ip if @ip given is a pointer to a ftrace location.
1668  * That is, the instruction that is either a NOP or call to
1669  * the function tracer. It checks the ftrace internal tables to
1670  * determine if the address belongs or not.
1671  */
1672 unsigned long ftrace_location(unsigned long ip)
1673 {
1674         return ftrace_location_range(ip, ip);
1675 }
1676
1677 /**
1678  * ftrace_text_reserved - return true if range contains an ftrace location
1679  * @start: start of range to search
1680  * @end: end of range to search (inclusive). @end points to the last byte to check.
1681  *
1682  * Returns 1 if @start and @end contains a ftrace location.
1683  * That is, the instruction that is either a NOP or call to
1684  * the function tracer. It checks the ftrace internal tables to
1685  * determine if the address belongs or not.
1686  */
1687 int ftrace_text_reserved(const void *start, const void *end)
1688 {
1689         unsigned long ret;
1690
1691         ret = ftrace_location_range((unsigned long)start,
1692                                     (unsigned long)end);
1693
1694         return (int)!!ret;
1695 }
1696
1697 /* Test if ops registered to this rec needs regs */
1698 static bool test_rec_ops_needs_regs(struct dyn_ftrace *rec)
1699 {
1700         struct ftrace_ops *ops;
1701         bool keep_regs = false;
1702
1703         for (ops = ftrace_ops_list;
1704              ops != &ftrace_list_end; ops = ops->next) {
1705                 /* pass rec in as regs to have non-NULL val */
1706                 if (ftrace_ops_test(ops, rec->ip, rec)) {
1707                         if (ops->flags & FTRACE_OPS_FL_SAVE_REGS) {
1708                                 keep_regs = true;
1709                                 break;
1710                         }
1711                 }
1712         }
1713
1714         return  keep_regs;
1715 }
1716
1717 static struct ftrace_ops *
1718 ftrace_find_tramp_ops_any(struct dyn_ftrace *rec);
1719 static struct ftrace_ops *
1720 ftrace_find_tramp_ops_any_other(struct dyn_ftrace *rec, struct ftrace_ops *op_exclude);
1721 static struct ftrace_ops *
1722 ftrace_find_tramp_ops_next(struct dyn_ftrace *rec, struct ftrace_ops *ops);
1723
1724 static bool __ftrace_hash_rec_update(struct ftrace_ops *ops,
1725                                      int filter_hash,
1726                                      bool inc)
1727 {
1728         struct ftrace_hash *hash;
1729         struct ftrace_hash *other_hash;
1730         struct ftrace_page *pg;
1731         struct dyn_ftrace *rec;
1732         bool update = false;
1733         int count = 0;
1734         int all = false;
1735
1736         /* Only update if the ops has been registered */
1737         if (!(ops->flags & FTRACE_OPS_FL_ENABLED))
1738                 return false;
1739
1740         /*
1741          * In the filter_hash case:
1742          *   If the count is zero, we update all records.
1743          *   Otherwise we just update the items in the hash.
1744          *
1745          * In the notrace_hash case:
1746          *   We enable the update in the hash.
1747          *   As disabling notrace means enabling the tracing,
1748          *   and enabling notrace means disabling, the inc variable
1749          *   gets inversed.
1750          */
1751         if (filter_hash) {
1752                 hash = ops->func_hash->filter_hash;
1753                 other_hash = ops->func_hash->notrace_hash;
1754                 if (ftrace_hash_empty(hash))
1755                         all = true;
1756         } else {
1757                 inc = !inc;
1758                 hash = ops->func_hash->notrace_hash;
1759                 other_hash = ops->func_hash->filter_hash;
1760                 /*
1761                  * If the notrace hash has no items,
1762                  * then there's nothing to do.
1763                  */
1764                 if (ftrace_hash_empty(hash))
1765                         return false;
1766         }
1767
1768         do_for_each_ftrace_rec(pg, rec) {
1769                 int in_other_hash = 0;
1770                 int in_hash = 0;
1771                 int match = 0;
1772
1773                 if (rec->flags & FTRACE_FL_DISABLED)
1774                         continue;
1775
1776                 if (all) {
1777                         /*
1778                          * Only the filter_hash affects all records.
1779                          * Update if the record is not in the notrace hash.
1780                          */
1781                         if (!other_hash || !ftrace_lookup_ip(other_hash, rec->ip))
1782                                 match = 1;
1783                 } else {
1784                         in_hash = !!ftrace_lookup_ip(hash, rec->ip);
1785                         in_other_hash = !!ftrace_lookup_ip(other_hash, rec->ip);
1786
1787                         /*
1788                          * If filter_hash is set, we want to match all functions
1789                          * that are in the hash but not in the other hash.
1790                          *
1791                          * If filter_hash is not set, then we are decrementing.
1792                          * That means we match anything that is in the hash
1793                          * and also in the other_hash. That is, we need to turn
1794                          * off functions in the other hash because they are disabled
1795                          * by this hash.
1796                          */
1797                         if (filter_hash && in_hash && !in_other_hash)
1798                                 match = 1;
1799                         else if (!filter_hash && in_hash &&
1800                                  (in_other_hash || ftrace_hash_empty(other_hash)))
1801                                 match = 1;
1802                 }
1803                 if (!match)
1804                         continue;
1805
1806                 if (inc) {
1807                         rec->flags++;
1808                         if (FTRACE_WARN_ON(ftrace_rec_count(rec) == FTRACE_REF_MAX))
1809                                 return false;
1810
1811                         /*
1812                          * If there's only a single callback registered to a
1813                          * function, and the ops has a trampoline registered
1814                          * for it, then we can call it directly.
1815                          */
1816                         if (ftrace_rec_count(rec) == 1 && ops->trampoline)
1817                                 rec->flags |= FTRACE_FL_TRAMP;
1818                         else
1819                                 /*
1820                                  * If we are adding another function callback
1821                                  * to this function, and the previous had a
1822                                  * custom trampoline in use, then we need to go
1823                                  * back to the default trampoline.
1824                                  */
1825                                 rec->flags &= ~FTRACE_FL_TRAMP;
1826
1827                         /*
1828                          * If any ops wants regs saved for this function
1829                          * then all ops will get saved regs.
1830                          */
1831                         if (ops->flags & FTRACE_OPS_FL_SAVE_REGS)
1832                                 rec->flags |= FTRACE_FL_REGS;
1833                 } else {
1834                         if (FTRACE_WARN_ON(ftrace_rec_count(rec) == 0))
1835                                 return false;
1836                         rec->flags--;
1837
1838                         /*
1839                          * If the rec had REGS enabled and the ops that is
1840                          * being removed had REGS set, then see if there is
1841                          * still any ops for this record that wants regs.
1842                          * If not, we can stop recording them.
1843                          */
1844                         if (ftrace_rec_count(rec) > 0 &&
1845                             rec->flags & FTRACE_FL_REGS &&
1846                             ops->flags & FTRACE_OPS_FL_SAVE_REGS) {
1847                                 if (!test_rec_ops_needs_regs(rec))
1848                                         rec->flags &= ~FTRACE_FL_REGS;
1849                         }
1850
1851                         /*
1852                          * The TRAMP needs to be set only if rec count
1853                          * is decremented to one, and the ops that is
1854                          * left has a trampoline. As TRAMP can only be
1855                          * enabled if there is only a single ops attached
1856                          * to it.
1857                          */
1858                         if (ftrace_rec_count(rec) == 1 &&
1859                             ftrace_find_tramp_ops_any_other(rec, ops))
1860                                 rec->flags |= FTRACE_FL_TRAMP;
1861                         else
1862                                 rec->flags &= ~FTRACE_FL_TRAMP;
1863
1864                         /*
1865                          * flags will be cleared in ftrace_check_record()
1866                          * if rec count is zero.
1867                          */
1868                 }
1869                 count++;
1870
1871                 /* Must match FTRACE_UPDATE_CALLS in ftrace_modify_all_code() */
1872                 update |= ftrace_test_record(rec, 1) != FTRACE_UPDATE_IGNORE;
1873
1874                 /* Shortcut, if we handled all records, we are done. */
1875                 if (!all && count == hash->count)
1876                         return update;
1877         } while_for_each_ftrace_rec();
1878
1879         return update;
1880 }
1881
1882 static bool ftrace_hash_rec_disable(struct ftrace_ops *ops,
1883                                     int filter_hash)
1884 {
1885         return __ftrace_hash_rec_update(ops, filter_hash, 0);
1886 }
1887
1888 static bool ftrace_hash_rec_enable(struct ftrace_ops *ops,
1889                                    int filter_hash)
1890 {
1891         return __ftrace_hash_rec_update(ops, filter_hash, 1);
1892 }
1893
1894 static void ftrace_hash_rec_update_modify(struct ftrace_ops *ops,
1895                                           int filter_hash, int inc)
1896 {
1897         struct ftrace_ops *op;
1898
1899         __ftrace_hash_rec_update(ops, filter_hash, inc);
1900
1901         if (ops->func_hash != &global_ops.local_hash)
1902                 return;
1903
1904         /*
1905          * If the ops shares the global_ops hash, then we need to update
1906          * all ops that are enabled and use this hash.
1907          */
1908         do_for_each_ftrace_op(op, ftrace_ops_list) {
1909                 /* Already done */
1910                 if (op == ops)
1911                         continue;
1912                 if (op->func_hash == &global_ops.local_hash)
1913                         __ftrace_hash_rec_update(op, filter_hash, inc);
1914         } while_for_each_ftrace_op(op);
1915 }
1916
1917 static void ftrace_hash_rec_disable_modify(struct ftrace_ops *ops,
1918                                            int filter_hash)
1919 {
1920         ftrace_hash_rec_update_modify(ops, filter_hash, 0);
1921 }
1922
1923 static void ftrace_hash_rec_enable_modify(struct ftrace_ops *ops,
1924                                           int filter_hash)
1925 {
1926         ftrace_hash_rec_update_modify(ops, filter_hash, 1);
1927 }
1928
1929 /*
1930  * Try to update IPMODIFY flag on each ftrace_rec. Return 0 if it is OK
1931  * or no-needed to update, -EBUSY if it detects a conflict of the flag
1932  * on a ftrace_rec, and -EINVAL if the new_hash tries to trace all recs.
1933  * Note that old_hash and new_hash has below meanings
1934  *  - If the hash is NULL, it hits all recs (if IPMODIFY is set, this is rejected)
1935  *  - If the hash is EMPTY_HASH, it hits nothing
1936  *  - Anything else hits the recs which match the hash entries.
1937  */
1938 static int __ftrace_hash_update_ipmodify(struct ftrace_ops *ops,
1939                                          struct ftrace_hash *old_hash,
1940                                          struct ftrace_hash *new_hash)
1941 {
1942         struct ftrace_page *pg;
1943         struct dyn_ftrace *rec, *end = NULL;
1944         int in_old, in_new;
1945
1946         /* Only update if the ops has been registered */
1947         if (!(ops->flags & FTRACE_OPS_FL_ENABLED))
1948                 return 0;
1949
1950         if (!(ops->flags & FTRACE_OPS_FL_IPMODIFY))
1951                 return 0;
1952
1953         /*
1954          * Since the IPMODIFY is a very address sensitive action, we do not
1955          * allow ftrace_ops to set all functions to new hash.
1956          */
1957         if (!new_hash || !old_hash)
1958                 return -EINVAL;
1959
1960         /* Update rec->flags */
1961         do_for_each_ftrace_rec(pg, rec) {
1962
1963                 if (rec->flags & FTRACE_FL_DISABLED)
1964                         continue;
1965
1966                 /* We need to update only differences of filter_hash */
1967                 in_old = !!ftrace_lookup_ip(old_hash, rec->ip);
1968                 in_new = !!ftrace_lookup_ip(new_hash, rec->ip);
1969                 if (in_old == in_new)
1970                         continue;
1971
1972                 if (in_new) {
1973                         /* New entries must ensure no others are using it */
1974                         if (rec->flags & FTRACE_FL_IPMODIFY)
1975                                 goto rollback;
1976                         rec->flags |= FTRACE_FL_IPMODIFY;
1977                 } else /* Removed entry */
1978                         rec->flags &= ~FTRACE_FL_IPMODIFY;
1979         } while_for_each_ftrace_rec();
1980
1981         return 0;
1982
1983 rollback:
1984         end = rec;
1985
1986         /* Roll back what we did above */
1987         do_for_each_ftrace_rec(pg, rec) {
1988
1989                 if (rec->flags & FTRACE_FL_DISABLED)
1990                         continue;
1991
1992                 if (rec == end)
1993                         goto err_out;
1994
1995                 in_old = !!ftrace_lookup_ip(old_hash, rec->ip);
1996                 in_new = !!ftrace_lookup_ip(new_hash, rec->ip);
1997                 if (in_old == in_new)
1998                         continue;
1999
2000                 if (in_new)
2001                         rec->flags &= ~FTRACE_FL_IPMODIFY;
2002                 else
2003                         rec->flags |= FTRACE_FL_IPMODIFY;
2004         } while_for_each_ftrace_rec();
2005
2006 err_out:
2007         return -EBUSY;
2008 }
2009
2010 static int ftrace_hash_ipmodify_enable(struct ftrace_ops *ops)
2011 {
2012         struct ftrace_hash *hash = ops->func_hash->filter_hash;
2013
2014         if (ftrace_hash_empty(hash))
2015                 hash = NULL;
2016
2017         return __ftrace_hash_update_ipmodify(ops, EMPTY_HASH, hash);
2018 }
2019
2020 /* Disabling always succeeds */
2021 static void ftrace_hash_ipmodify_disable(struct ftrace_ops *ops)
2022 {
2023         struct ftrace_hash *hash = ops->func_hash->filter_hash;
2024
2025         if (ftrace_hash_empty(hash))
2026                 hash = NULL;
2027
2028         __ftrace_hash_update_ipmodify(ops, hash, EMPTY_HASH);
2029 }
2030
2031 static int ftrace_hash_ipmodify_update(struct ftrace_ops *ops,
2032                                        struct ftrace_hash *new_hash)
2033 {
2034         struct ftrace_hash *old_hash = ops->func_hash->filter_hash;
2035
2036         if (ftrace_hash_empty(old_hash))
2037                 old_hash = NULL;
2038
2039         if (ftrace_hash_empty(new_hash))
2040                 new_hash = NULL;
2041
2042         return __ftrace_hash_update_ipmodify(ops, old_hash, new_hash);
2043 }
2044
2045 static void print_ip_ins(const char *fmt, const unsigned char *p)
2046 {
2047         char ins[MCOUNT_INSN_SIZE];
2048         int i;
2049
2050         if (probe_kernel_read(ins, p, MCOUNT_INSN_SIZE)) {
2051                 printk(KERN_CONT "%s[FAULT] %px\n", fmt, p);
2052                 return;
2053         }
2054
2055         printk(KERN_CONT "%s", fmt);
2056
2057         for (i = 0; i < MCOUNT_INSN_SIZE; i++)
2058                 printk(KERN_CONT "%s%02x", i ? ":" : "", ins[i]);
2059 }
2060
2061 enum ftrace_bug_type ftrace_bug_type;
2062 const void *ftrace_expected;
2063
2064 static void print_bug_type(void)
2065 {
2066         switch (ftrace_bug_type) {
2067         case FTRACE_BUG_UNKNOWN:
2068                 break;
2069         case FTRACE_BUG_INIT:
2070                 pr_info("Initializing ftrace call sites\n");
2071                 break;
2072         case FTRACE_BUG_NOP:
2073                 pr_info("Setting ftrace call site to NOP\n");
2074                 break;
2075         case FTRACE_BUG_CALL:
2076                 pr_info("Setting ftrace call site to call ftrace function\n");
2077                 break;
2078         case FTRACE_BUG_UPDATE:
2079                 pr_info("Updating ftrace call site to call a different ftrace function\n");
2080                 break;
2081         }
2082 }
2083
2084 /**
2085  * ftrace_bug - report and shutdown function tracer
2086  * @failed: The failed type (EFAULT, EINVAL, EPERM)
2087  * @rec: The record that failed
2088  *
2089  * The arch code that enables or disables the function tracing
2090  * can call ftrace_bug() when it has detected a problem in
2091  * modifying the code. @failed should be one of either:
2092  * EFAULT - if the problem happens on reading the @ip address
2093  * EINVAL - if what is read at @ip is not what was expected
2094  * EPERM - if the problem happens on writting to the @ip address
2095  */
2096 void ftrace_bug(int failed, struct dyn_ftrace *rec)
2097 {
2098         unsigned long ip = rec ? rec->ip : 0;
2099
2100         switch (failed) {
2101         case -EFAULT:
2102                 FTRACE_WARN_ON_ONCE(1);
2103                 pr_info("ftrace faulted on modifying ");
2104                 print_ip_sym(ip);
2105                 break;
2106         case -EINVAL:
2107                 FTRACE_WARN_ON_ONCE(1);
2108                 pr_info("ftrace failed to modify ");
2109                 print_ip_sym(ip);
2110                 print_ip_ins(" actual:   ", (unsigned char *)ip);
2111                 pr_cont("\n");
2112                 if (ftrace_expected) {
2113                         print_ip_ins(" expected: ", ftrace_expected);
2114                         pr_cont("\n");
2115                 }
2116                 break;
2117         case -EPERM:
2118                 FTRACE_WARN_ON_ONCE(1);
2119                 pr_info("ftrace faulted on writing ");
2120                 print_ip_sym(ip);
2121                 break;
2122         default:
2123                 FTRACE_WARN_ON_ONCE(1);
2124                 pr_info("ftrace faulted on unknown error ");
2125                 print_ip_sym(ip);
2126         }
2127         print_bug_type();
2128         if (rec) {
2129                 struct ftrace_ops *ops = NULL;
2130
2131                 pr_info("ftrace record flags: %lx\n", rec->flags);
2132                 pr_cont(" (%ld)%s", ftrace_rec_count(rec),
2133                         rec->flags & FTRACE_FL_REGS ? " R" : "  ");
2134                 if (rec->flags & FTRACE_FL_TRAMP_EN) {
2135                         ops = ftrace_find_tramp_ops_any(rec);
2136                         if (ops) {
2137                                 do {
2138                                         pr_cont("\ttramp: %pS (%pS)",
2139                                                 (void *)ops->trampoline,
2140                                                 (void *)ops->func);
2141                                         ops = ftrace_find_tramp_ops_next(rec, ops);
2142                                 } while (ops);
2143                         } else
2144                                 pr_cont("\ttramp: ERROR!");
2145
2146                 }
2147                 ip = ftrace_get_addr_curr(rec);
2148                 pr_cont("\n expected tramp: %lx\n", ip);
2149         }
2150 }
2151
2152 static int ftrace_check_record(struct dyn_ftrace *rec, int enable, int update)
2153 {
2154         unsigned long flag = 0UL;
2155
2156         ftrace_bug_type = FTRACE_BUG_UNKNOWN;
2157
2158         if (rec->flags & FTRACE_FL_DISABLED)
2159                 return FTRACE_UPDATE_IGNORE;
2160
2161         /*
2162          * If we are updating calls:
2163          *
2164          *   If the record has a ref count, then we need to enable it
2165          *   because someone is using it.
2166          *
2167          *   Otherwise we make sure its disabled.
2168          *
2169          * If we are disabling calls, then disable all records that
2170          * are enabled.
2171          */
2172         if (enable && ftrace_rec_count(rec))
2173                 flag = FTRACE_FL_ENABLED;
2174
2175         /*
2176          * If enabling and the REGS flag does not match the REGS_EN, or
2177          * the TRAMP flag doesn't match the TRAMP_EN, then do not ignore
2178          * this record. Set flags to fail the compare against ENABLED.
2179          */
2180         if (flag) {
2181                 if (!(rec->flags & FTRACE_FL_REGS) != 
2182                     !(rec->flags & FTRACE_FL_REGS_EN))
2183                         flag |= FTRACE_FL_REGS;
2184
2185                 if (!(rec->flags & FTRACE_FL_TRAMP) != 
2186                     !(rec->flags & FTRACE_FL_TRAMP_EN))
2187                         flag |= FTRACE_FL_TRAMP;
2188         }
2189
2190         /* If the state of this record hasn't changed, then do nothing */
2191         if ((rec->flags & FTRACE_FL_ENABLED) == flag)
2192                 return FTRACE_UPDATE_IGNORE;
2193
2194         if (flag) {
2195                 /* Save off if rec is being enabled (for return value) */
2196                 flag ^= rec->flags & FTRACE_FL_ENABLED;
2197
2198                 if (update) {
2199                         rec->flags |= FTRACE_FL_ENABLED;
2200                         if (flag & FTRACE_FL_REGS) {
2201                                 if (rec->flags & FTRACE_FL_REGS)
2202                                         rec->flags |= FTRACE_FL_REGS_EN;
2203                                 else
2204                                         rec->flags &= ~FTRACE_FL_REGS_EN;
2205                         }
2206                         if (flag & FTRACE_FL_TRAMP) {
2207                                 if (rec->flags & FTRACE_FL_TRAMP)
2208                                         rec->flags |= FTRACE_FL_TRAMP_EN;
2209                                 else
2210                                         rec->flags &= ~FTRACE_FL_TRAMP_EN;
2211                         }
2212                 }
2213
2214                 /*
2215                  * If this record is being updated from a nop, then
2216                  *   return UPDATE_MAKE_CALL.
2217                  * Otherwise,
2218                  *   return UPDATE_MODIFY_CALL to tell the caller to convert
2219                  *   from the save regs, to a non-save regs function or
2220                  *   vice versa, or from a trampoline call.
2221                  */
2222                 if (flag & FTRACE_FL_ENABLED) {
2223                         ftrace_bug_type = FTRACE_BUG_CALL;
2224                         return FTRACE_UPDATE_MAKE_CALL;
2225                 }
2226
2227                 ftrace_bug_type = FTRACE_BUG_UPDATE;
2228                 return FTRACE_UPDATE_MODIFY_CALL;
2229         }
2230
2231         if (update) {
2232                 /* If there's no more users, clear all flags */
2233                 if (!ftrace_rec_count(rec))
2234                         rec->flags = 0;
2235                 else
2236                         /*
2237                          * Just disable the record, but keep the ops TRAMP
2238                          * and REGS states. The _EN flags must be disabled though.
2239                          */
2240                         rec->flags &= ~(FTRACE_FL_ENABLED | FTRACE_FL_TRAMP_EN |
2241                                         FTRACE_FL_REGS_EN);
2242         }
2243
2244         ftrace_bug_type = FTRACE_BUG_NOP;
2245         return FTRACE_UPDATE_MAKE_NOP;
2246 }
2247
2248 /**
2249  * ftrace_update_record, set a record that now is tracing or not
2250  * @rec: the record to update
2251  * @enable: set to 1 if the record is tracing, zero to force disable
2252  *
2253  * The records that represent all functions that can be traced need
2254  * to be updated when tracing has been enabled.
2255  */
2256 int ftrace_update_record(struct dyn_ftrace *rec, int enable)
2257 {
2258         return ftrace_check_record(rec, enable, 1);
2259 }
2260
2261 /**
2262  * ftrace_test_record, check if the record has been enabled or not
2263  * @rec: the record to test
2264  * @enable: set to 1 to check if enabled, 0 if it is disabled
2265  *
2266  * The arch code may need to test if a record is already set to
2267  * tracing to determine how to modify the function code that it
2268  * represents.
2269  */
2270 int ftrace_test_record(struct dyn_ftrace *rec, int enable)
2271 {
2272         return ftrace_check_record(rec, enable, 0);
2273 }
2274
2275 static struct ftrace_ops *
2276 ftrace_find_tramp_ops_any(struct dyn_ftrace *rec)
2277 {
2278         struct ftrace_ops *op;
2279         unsigned long ip = rec->ip;
2280
2281         do_for_each_ftrace_op(op, ftrace_ops_list) {
2282
2283                 if (!op->trampoline)
2284                         continue;
2285
2286                 if (hash_contains_ip(ip, op->func_hash))
2287                         return op;
2288         } while_for_each_ftrace_op(op);
2289
2290         return NULL;
2291 }
2292
2293 static struct ftrace_ops *
2294 ftrace_find_tramp_ops_any_other(struct dyn_ftrace *rec, struct ftrace_ops *op_exclude)
2295 {
2296         struct ftrace_ops *op;
2297         unsigned long ip = rec->ip;
2298
2299         do_for_each_ftrace_op(op, ftrace_ops_list) {
2300
2301                 if (op == op_exclude || !op->trampoline)
2302                         continue;
2303
2304                 if (hash_contains_ip(ip, op->func_hash))
2305                         return op;
2306         } while_for_each_ftrace_op(op);
2307
2308         return NULL;
2309 }
2310
2311 static struct ftrace_ops *
2312 ftrace_find_tramp_ops_next(struct dyn_ftrace *rec,
2313                            struct ftrace_ops *op)
2314 {
2315         unsigned long ip = rec->ip;
2316
2317         while_for_each_ftrace_op(op) {
2318
2319                 if (!op->trampoline)
2320                         continue;
2321
2322                 if (hash_contains_ip(ip, op->func_hash))
2323                         return op;
2324         } 
2325
2326         return NULL;
2327 }
2328
2329 static struct ftrace_ops *
2330 ftrace_find_tramp_ops_curr(struct dyn_ftrace *rec)
2331 {
2332         struct ftrace_ops *op;
2333         unsigned long ip = rec->ip;
2334
2335         /*
2336          * Need to check removed ops first.
2337          * If they are being removed, and this rec has a tramp,
2338          * and this rec is in the ops list, then it would be the
2339          * one with the tramp.
2340          */
2341         if (removed_ops) {
2342                 if (hash_contains_ip(ip, &removed_ops->old_hash))
2343                         return removed_ops;
2344         }
2345
2346         /*
2347          * Need to find the current trampoline for a rec.
2348          * Now, a trampoline is only attached to a rec if there
2349          * was a single 'ops' attached to it. But this can be called
2350          * when we are adding another op to the rec or removing the
2351          * current one. Thus, if the op is being added, we can
2352          * ignore it because it hasn't attached itself to the rec
2353          * yet.
2354          *
2355          * If an ops is being modified (hooking to different functions)
2356          * then we don't care about the new functions that are being
2357          * added, just the old ones (that are probably being removed).
2358          *
2359          * If we are adding an ops to a function that already is using
2360          * a trampoline, it needs to be removed (trampolines are only
2361          * for single ops connected), then an ops that is not being
2362          * modified also needs to be checked.
2363          */
2364         do_for_each_ftrace_op(op, ftrace_ops_list) {
2365
2366                 if (!op->trampoline)
2367                         continue;
2368
2369                 /*
2370                  * If the ops is being added, it hasn't gotten to
2371                  * the point to be removed from this tree yet.
2372                  */
2373                 if (op->flags & FTRACE_OPS_FL_ADDING)
2374                         continue;
2375
2376
2377                 /*
2378                  * If the ops is being modified and is in the old
2379                  * hash, then it is probably being removed from this
2380                  * function.
2381                  */
2382                 if ((op->flags & FTRACE_OPS_FL_MODIFYING) &&
2383                     hash_contains_ip(ip, &op->old_hash))
2384                         return op;
2385                 /*
2386                  * If the ops is not being added or modified, and it's
2387                  * in its normal filter hash, then this must be the one
2388                  * we want!
2389                  */
2390                 if (!(op->flags & FTRACE_OPS_FL_MODIFYING) &&
2391                     hash_contains_ip(ip, op->func_hash))
2392                         return op;
2393
2394         } while_for_each_ftrace_op(op);
2395
2396         return NULL;
2397 }
2398
2399 static struct ftrace_ops *
2400 ftrace_find_tramp_ops_new(struct dyn_ftrace *rec)
2401 {
2402         struct ftrace_ops *op;
2403         unsigned long ip = rec->ip;
2404
2405         do_for_each_ftrace_op(op, ftrace_ops_list) {
2406                 /* pass rec in as regs to have non-NULL val */
2407                 if (hash_contains_ip(ip, op->func_hash))
2408                         return op;
2409         } while_for_each_ftrace_op(op);
2410
2411         return NULL;
2412 }
2413
2414 /**
2415  * ftrace_get_addr_new - Get the call address to set to
2416  * @rec:  The ftrace record descriptor
2417  *
2418  * If the record has the FTRACE_FL_REGS set, that means that it
2419  * wants to convert to a callback that saves all regs. If FTRACE_FL_REGS
2420  * is not not set, then it wants to convert to the normal callback.
2421  *
2422  * Returns the address of the trampoline to set to
2423  */
2424 unsigned long ftrace_get_addr_new(struct dyn_ftrace *rec)
2425 {
2426         struct ftrace_ops *ops;
2427
2428         /* Trampolines take precedence over regs */
2429         if (rec->flags & FTRACE_FL_TRAMP) {
2430                 ops = ftrace_find_tramp_ops_new(rec);
2431                 if (FTRACE_WARN_ON(!ops || !ops->trampoline)) {
2432                         pr_warn("Bad trampoline accounting at: %p (%pS) (%lx)\n",
2433                                 (void *)rec->ip, (void *)rec->ip, rec->flags);
2434                         /* Ftrace is shutting down, return anything */
2435                         return (unsigned long)FTRACE_ADDR;
2436                 }
2437                 return ops->trampoline;
2438         }
2439
2440         if (rec->flags & FTRACE_FL_REGS)
2441                 return (unsigned long)FTRACE_REGS_ADDR;
2442         else
2443                 return (unsigned long)FTRACE_ADDR;
2444 }
2445
2446 /**
2447  * ftrace_get_addr_curr - Get the call address that is already there
2448  * @rec:  The ftrace record descriptor
2449  *
2450  * The FTRACE_FL_REGS_EN is set when the record already points to
2451  * a function that saves all the regs. Basically the '_EN' version
2452  * represents the current state of the function.
2453  *
2454  * Returns the address of the trampoline that is currently being called
2455  */
2456 unsigned long ftrace_get_addr_curr(struct dyn_ftrace *rec)
2457 {
2458         struct ftrace_ops *ops;
2459
2460         /* Trampolines take precedence over regs */
2461         if (rec->flags & FTRACE_FL_TRAMP_EN) {
2462                 ops = ftrace_find_tramp_ops_curr(rec);
2463                 if (FTRACE_WARN_ON(!ops)) {
2464                         pr_warn("Bad trampoline accounting at: %p (%pS)\n",
2465                                 (void *)rec->ip, (void *)rec->ip);
2466                         /* Ftrace is shutting down, return anything */
2467                         return (unsigned long)FTRACE_ADDR;
2468                 }
2469                 return ops->trampoline;
2470         }
2471
2472         if (rec->flags & FTRACE_FL_REGS_EN)
2473                 return (unsigned long)FTRACE_REGS_ADDR;
2474         else
2475                 return (unsigned long)FTRACE_ADDR;
2476 }
2477
2478 static int
2479 __ftrace_replace_code(struct dyn_ftrace *rec, int enable)
2480 {
2481         unsigned long ftrace_old_addr;
2482         unsigned long ftrace_addr;
2483         int ret;
2484
2485         ftrace_addr = ftrace_get_addr_new(rec);
2486
2487         /* This needs to be done before we call ftrace_update_record */
2488         ftrace_old_addr = ftrace_get_addr_curr(rec);
2489
2490         ret = ftrace_update_record(rec, enable);
2491
2492         ftrace_bug_type = FTRACE_BUG_UNKNOWN;
2493
2494         switch (ret) {
2495         case FTRACE_UPDATE_IGNORE:
2496                 return 0;
2497
2498         case FTRACE_UPDATE_MAKE_CALL:
2499                 ftrace_bug_type = FTRACE_BUG_CALL;
2500                 return ftrace_make_call(rec, ftrace_addr);
2501
2502         case FTRACE_UPDATE_MAKE_NOP:
2503                 ftrace_bug_type = FTRACE_BUG_NOP;
2504                 return ftrace_make_nop(NULL, rec, ftrace_old_addr);
2505
2506         case FTRACE_UPDATE_MODIFY_CALL:
2507                 ftrace_bug_type = FTRACE_BUG_UPDATE;
2508                 return ftrace_modify_call(rec, ftrace_old_addr, ftrace_addr);
2509         }
2510
2511         return -1; /* unknow ftrace bug */
2512 }
2513
2514 void __weak ftrace_replace_code(int enable)
2515 {
2516         struct dyn_ftrace *rec;
2517         struct ftrace_page *pg;
2518         int failed;
2519
2520         if (unlikely(ftrace_disabled))
2521                 return;
2522
2523         do_for_each_ftrace_rec(pg, rec) {
2524
2525                 if (rec->flags & FTRACE_FL_DISABLED)
2526                         continue;
2527
2528                 failed = __ftrace_replace_code(rec, enable);
2529                 if (failed) {
2530                         ftrace_bug(failed, rec);
2531                         /* Stop processing */
2532                         return;
2533                 }
2534         } while_for_each_ftrace_rec();
2535 }
2536
2537 struct ftrace_rec_iter {
2538         struct ftrace_page      *pg;
2539         int                     index;
2540 };
2541
2542 /**
2543  * ftrace_rec_iter_start, start up iterating over traced functions
2544  *
2545  * Returns an iterator handle that is used to iterate over all
2546  * the records that represent address locations where functions
2547  * are traced.
2548  *
2549  * May return NULL if no records are available.
2550  */
2551 struct ftrace_rec_iter *ftrace_rec_iter_start(void)
2552 {
2553         /*
2554          * We only use a single iterator.
2555          * Protected by the ftrace_lock mutex.
2556          */
2557         static struct ftrace_rec_iter ftrace_rec_iter;
2558         struct ftrace_rec_iter *iter = &ftrace_rec_iter;
2559
2560         iter->pg = ftrace_pages_start;
2561         iter->index = 0;
2562
2563         /* Could have empty pages */
2564         while (iter->pg && !iter->pg->index)
2565                 iter->pg = iter->pg->next;
2566
2567         if (!iter->pg)
2568                 return NULL;
2569
2570         return iter;
2571 }
2572
2573 /**
2574  * ftrace_rec_iter_next, get the next record to process.
2575  * @iter: The handle to the iterator.
2576  *
2577  * Returns the next iterator after the given iterator @iter.
2578  */
2579 struct ftrace_rec_iter *ftrace_rec_iter_next(struct ftrace_rec_iter *iter)
2580 {
2581         iter->index++;
2582
2583         if (iter->index >= iter->pg->index) {
2584                 iter->pg = iter->pg->next;
2585                 iter->index = 0;
2586
2587                 /* Could have empty pages */
2588                 while (iter->pg && !iter->pg->index)
2589                         iter->pg = iter->pg->next;
2590         }
2591
2592         if (!iter->pg)
2593                 return NULL;
2594
2595         return iter;
2596 }
2597
2598 /**
2599  * ftrace_rec_iter_record, get the record at the iterator location
2600  * @iter: The current iterator location
2601  *
2602  * Returns the record that the current @iter is at.
2603  */
2604 struct dyn_ftrace *ftrace_rec_iter_record(struct ftrace_rec_iter *iter)
2605 {
2606         return &iter->pg->records[iter->index];
2607 }
2608
2609 static int
2610 ftrace_code_disable(struct module *mod, struct dyn_ftrace *rec)
2611 {
2612         int ret;
2613
2614         if (unlikely(ftrace_disabled))
2615                 return 0;
2616
2617         ret = ftrace_make_nop(mod, rec, MCOUNT_ADDR);
2618         if (ret) {
2619                 ftrace_bug_type = FTRACE_BUG_INIT;
2620                 ftrace_bug(ret, rec);
2621                 return 0;
2622         }
2623         return 1;
2624 }
2625
2626 /*
2627  * archs can override this function if they must do something
2628  * before the modifying code is performed.
2629  */
2630 int __weak ftrace_arch_code_modify_prepare(void)
2631 {
2632         return 0;
2633 }
2634
2635 /*
2636  * archs can override this function if they must do something
2637  * after the modifying code is performed.
2638  */
2639 int __weak ftrace_arch_code_modify_post_process(void)
2640 {
2641         return 0;
2642 }
2643
2644 void ftrace_modify_all_code(int command)
2645 {
2646         int update = command & FTRACE_UPDATE_TRACE_FUNC;
2647         int err = 0;
2648
2649         /*
2650          * If the ftrace_caller calls a ftrace_ops func directly,
2651          * we need to make sure that it only traces functions it
2652          * expects to trace. When doing the switch of functions,
2653          * we need to update to the ftrace_ops_list_func first
2654          * before the transition between old and new calls are set,
2655          * as the ftrace_ops_list_func will check the ops hashes
2656          * to make sure the ops are having the right functions
2657          * traced.
2658          */
2659         if (update) {
2660                 err = ftrace_update_ftrace_func(ftrace_ops_list_func);
2661                 if (FTRACE_WARN_ON(err))
2662                         return;
2663         }
2664
2665         if (command & FTRACE_UPDATE_CALLS)
2666                 ftrace_replace_code(1);
2667         else if (command & FTRACE_DISABLE_CALLS)
2668                 ftrace_replace_code(0);
2669
2670         if (update && ftrace_trace_function != ftrace_ops_list_func) {
2671                 function_trace_op = set_function_trace_op;
2672                 smp_wmb();
2673                 /* If irqs are disabled, we are in stop machine */
2674                 if (!irqs_disabled())
2675                         smp_call_function(ftrace_sync_ipi, NULL, 1);
2676                 err = ftrace_update_ftrace_func(ftrace_trace_function);
2677                 if (FTRACE_WARN_ON(err))
2678                         return;
2679         }
2680
2681         if (command & FTRACE_START_FUNC_RET)
2682                 err = ftrace_enable_ftrace_graph_caller();
2683         else if (command & FTRACE_STOP_FUNC_RET)
2684                 err = ftrace_disable_ftrace_graph_caller();
2685         FTRACE_WARN_ON(err);
2686 }
2687
2688 static int __ftrace_modify_code(void *data)
2689 {
2690         int *command = data;
2691
2692         ftrace_modify_all_code(*command);
2693
2694         return 0;
2695 }
2696
2697 /**
2698  * ftrace_run_stop_machine, go back to the stop machine method
2699  * @command: The command to tell ftrace what to do
2700  *
2701  * If an arch needs to fall back to the stop machine method, the
2702  * it can call this function.
2703  */
2704 void ftrace_run_stop_machine(int command)
2705 {
2706         stop_machine(__ftrace_modify_code, &command, NULL);
2707 }
2708
2709 /**
2710  * arch_ftrace_update_code, modify the code to trace or not trace
2711  * @command: The command that needs to be done
2712  *
2713  * Archs can override this function if it does not need to
2714  * run stop_machine() to modify code.
2715  */
2716 void __weak arch_ftrace_update_code(int command)
2717 {
2718         ftrace_run_stop_machine(command);
2719 }
2720
2721 static void ftrace_run_update_code(int command)
2722 {
2723         int ret;
2724
2725         ret = ftrace_arch_code_modify_prepare();
2726         FTRACE_WARN_ON(ret);
2727         if (ret)
2728                 return;
2729
2730         /*
2731          * By default we use stop_machine() to modify the code.
2732          * But archs can do what ever they want as long as it
2733          * is safe. The stop_machine() is the safest, but also
2734          * produces the most overhead.
2735          */
2736         arch_ftrace_update_code(command);
2737
2738         ret = ftrace_arch_code_modify_post_process();
2739         FTRACE_WARN_ON(ret);
2740 }
2741
2742 static void ftrace_run_modify_code(struct ftrace_ops *ops, int command,
2743                                    struct ftrace_ops_hash *old_hash)
2744 {
2745         ops->flags |= FTRACE_OPS_FL_MODIFYING;
2746         ops->old_hash.filter_hash = old_hash->filter_hash;
2747         ops->old_hash.notrace_hash = old_hash->notrace_hash;
2748         ftrace_run_update_code(command);
2749         ops->old_hash.filter_hash = NULL;
2750         ops->old_hash.notrace_hash = NULL;
2751         ops->flags &= ~FTRACE_OPS_FL_MODIFYING;
2752 }
2753
2754 static ftrace_func_t saved_ftrace_func;
2755 static int ftrace_start_up;
2756
2757 void __weak arch_ftrace_trampoline_free(struct ftrace_ops *ops)
2758 {
2759 }
2760
2761 static void per_cpu_ops_free(struct ftrace_ops *ops)
2762 {
2763         free_percpu(ops->disabled);
2764 }
2765
2766 static void ftrace_startup_enable(int command)
2767 {
2768         if (saved_ftrace_func != ftrace_trace_function) {
2769                 saved_ftrace_func = ftrace_trace_function;
2770                 command |= FTRACE_UPDATE_TRACE_FUNC;
2771         }
2772
2773         if (!command || !ftrace_enabled)
2774                 return;
2775
2776         ftrace_run_update_code(command);
2777 }
2778
2779 static void ftrace_startup_all(int command)
2780 {
2781         update_all_ops = true;
2782         ftrace_startup_enable(command);
2783         update_all_ops = false;
2784 }
2785
2786 static int ftrace_startup(struct ftrace_ops *ops, int command)
2787 {
2788         int ret;
2789
2790         if (unlikely(ftrace_disabled))
2791                 return -ENODEV;
2792
2793         ret = __register_ftrace_function(ops);
2794         if (ret)
2795                 return ret;
2796
2797         ftrace_start_up++;
2798
2799         /*
2800          * Note that ftrace probes uses this to start up
2801          * and modify functions it will probe. But we still
2802          * set the ADDING flag for modification, as probes
2803          * do not have trampolines. If they add them in the
2804          * future, then the probes will need to distinguish
2805          * between adding and updating probes.
2806          */
2807         ops->flags |= FTRACE_OPS_FL_ENABLED | FTRACE_OPS_FL_ADDING;
2808
2809         ret = ftrace_hash_ipmodify_enable(ops);
2810         if (ret < 0) {
2811                 /* Rollback registration process */
2812                 __unregister_ftrace_function(ops);
2813                 ftrace_start_up--;
2814                 ops->flags &= ~FTRACE_OPS_FL_ENABLED;
2815                 return ret;
2816         }
2817
2818         if (ftrace_hash_rec_enable(ops, 1))
2819                 command |= FTRACE_UPDATE_CALLS;
2820
2821         ftrace_startup_enable(command);
2822
2823         /*
2824          * If ftrace is in an undefined state, we just remove ops from list
2825          * to prevent the NULL pointer, instead of totally rolling it back and
2826          * free trampoline, because those actions could cause further damage.
2827          */
2828         if (unlikely(ftrace_disabled)) {
2829                 __unregister_ftrace_function(ops);
2830                 return -ENODEV;
2831         }
2832
2833         ops->flags &= ~FTRACE_OPS_FL_ADDING;
2834
2835         return 0;
2836 }
2837
2838 static int ftrace_shutdown(struct ftrace_ops *ops, int command)
2839 {
2840         int ret;
2841
2842         if (unlikely(ftrace_disabled))
2843                 return -ENODEV;
2844
2845         ret = __unregister_ftrace_function(ops);
2846         if (ret)
2847                 return ret;
2848
2849         ftrace_start_up--;
2850         /*
2851          * Just warn in case of unbalance, no need to kill ftrace, it's not
2852          * critical but the ftrace_call callers may be never nopped again after
2853          * further ftrace uses.
2854          */
2855         WARN_ON_ONCE(ftrace_start_up < 0);
2856
2857         /* Disabling ipmodify never fails */
2858         ftrace_hash_ipmodify_disable(ops);
2859
2860         if (ftrace_hash_rec_disable(ops, 1))
2861                 command |= FTRACE_UPDATE_CALLS;
2862
2863         ops->flags &= ~FTRACE_OPS_FL_ENABLED;
2864
2865         if (saved_ftrace_func != ftrace_trace_function) {
2866                 saved_ftrace_func = ftrace_trace_function;
2867                 command |= FTRACE_UPDATE_TRACE_FUNC;
2868         }
2869
2870         if (!command || !ftrace_enabled) {
2871                 /*
2872                  * If these are dynamic or per_cpu ops, they still
2873                  * need their data freed. Since, function tracing is
2874                  * not currently active, we can just free them
2875                  * without synchronizing all CPUs.
2876                  */
2877                 if (ops->flags & (FTRACE_OPS_FL_DYNAMIC | FTRACE_OPS_FL_PER_CPU))
2878                         goto free_ops;
2879
2880                 return 0;
2881         }
2882
2883         /*
2884          * If the ops uses a trampoline, then it needs to be
2885          * tested first on update.
2886          */
2887         ops->flags |= FTRACE_OPS_FL_REMOVING;
2888         removed_ops = ops;
2889
2890         /* The trampoline logic checks the old hashes */
2891         ops->old_hash.filter_hash = ops->func_hash->filter_hash;
2892         ops->old_hash.notrace_hash = ops->func_hash->notrace_hash;
2893
2894         ftrace_run_update_code(command);
2895
2896         /*
2897          * If there's no more ops registered with ftrace, run a
2898          * sanity check to make sure all rec flags are cleared.
2899          */
2900         if (rcu_dereference_protected(ftrace_ops_list,
2901                         lockdep_is_held(&ftrace_lock)) == &ftrace_list_end) {
2902                 struct ftrace_page *pg;
2903                 struct dyn_ftrace *rec;
2904
2905                 do_for_each_ftrace_rec(pg, rec) {
2906                         if (FTRACE_WARN_ON_ONCE(rec->flags & ~FTRACE_FL_DISABLED))
2907                                 pr_warn("  %pS flags:%lx\n",
2908                                         (void *)rec->ip, rec->flags);
2909                 } while_for_each_ftrace_rec();
2910         }
2911
2912         ops->old_hash.filter_hash = NULL;
2913         ops->old_hash.notrace_hash = NULL;
2914
2915         removed_ops = NULL;
2916         ops->flags &= ~FTRACE_OPS_FL_REMOVING;
2917
2918         /*
2919          * Dynamic ops may be freed, we must make sure that all
2920          * callers are done before leaving this function.
2921          * The same goes for freeing the per_cpu data of the per_cpu
2922          * ops.
2923          */
2924         if (ops->flags & (FTRACE_OPS_FL_DYNAMIC | FTRACE_OPS_FL_PER_CPU)) {
2925                 /*
2926                  * We need to do a hard force of sched synchronization.
2927                  * This is because we use preempt_disable() to do RCU, but
2928                  * the function tracers can be called where RCU is not watching
2929                  * (like before user_exit()). We can not rely on the RCU
2930                  * infrastructure to do the synchronization, thus we must do it
2931                  * ourselves.
2932                  */
2933                 schedule_on_each_cpu(ftrace_sync);
2934
2935                 /*
2936                  * When the kernel is preeptive, tasks can be preempted
2937                  * while on a ftrace trampoline. Just scheduling a task on
2938                  * a CPU is not good enough to flush them. Calling
2939                  * synchornize_rcu_tasks() will wait for those tasks to
2940                  * execute and either schedule voluntarily or enter user space.
2941                  */
2942                 if (IS_ENABLED(CONFIG_PREEMPT))
2943                         synchronize_rcu_tasks();
2944
2945  free_ops:
2946                 arch_ftrace_trampoline_free(ops);
2947
2948                 if (ops->flags & FTRACE_OPS_FL_PER_CPU)
2949                         per_cpu_ops_free(ops);
2950         }
2951
2952         return 0;
2953 }
2954
2955 static void ftrace_startup_sysctl(void)
2956 {
2957         int command;
2958
2959         if (unlikely(ftrace_disabled))
2960                 return;
2961
2962         /* Force update next time */
2963         saved_ftrace_func = NULL;
2964         /* ftrace_start_up is true if we want ftrace running */
2965         if (ftrace_start_up) {
2966                 command = FTRACE_UPDATE_CALLS;
2967                 if (ftrace_graph_active)
2968                         command |= FTRACE_START_FUNC_RET;
2969                 ftrace_startup_enable(command);
2970         }
2971 }
2972
2973 static void ftrace_shutdown_sysctl(void)
2974 {
2975         int command;
2976
2977         if (unlikely(ftrace_disabled))
2978                 return;
2979
2980         /* ftrace_start_up is true if ftrace is running */
2981         if (ftrace_start_up) {
2982                 command = FTRACE_DISABLE_CALLS;
2983                 if (ftrace_graph_active)
2984                         command |= FTRACE_STOP_FUNC_RET;
2985                 ftrace_run_update_code(command);
2986         }
2987 }
2988
2989 static u64              ftrace_update_time;
2990 unsigned long           ftrace_update_tot_cnt;
2991
2992 static inline int ops_traces_mod(struct ftrace_ops *ops)
2993 {
2994         /*
2995          * Filter_hash being empty will default to trace module.
2996          * But notrace hash requires a test of individual module functions.
2997          */
2998         return ftrace_hash_empty(ops->func_hash->filter_hash) &&
2999                 ftrace_hash_empty(ops->func_hash->notrace_hash);
3000 }
3001
3002 /*
3003  * Check if the current ops references the record.
3004  *
3005  * If the ops traces all functions, then it was already accounted for.
3006  * If the ops does not trace the current record function, skip it.
3007  * If the ops ignores the function via notrace filter, skip it.
3008  */
3009 static inline bool
3010 ops_references_rec(struct ftrace_ops *ops, struct dyn_ftrace *rec)
3011 {
3012         /* If ops isn't enabled, ignore it */
3013         if (!(ops->flags & FTRACE_OPS_FL_ENABLED))
3014                 return 0;
3015
3016         /* If ops traces all then it includes this function */
3017         if (ops_traces_mod(ops))
3018                 return 1;
3019
3020         /* The function must be in the filter */
3021         if (!ftrace_hash_empty(ops->func_hash->filter_hash) &&
3022             !__ftrace_lookup_ip(ops->func_hash->filter_hash, rec->ip))
3023                 return 0;
3024
3025         /* If in notrace hash, we ignore it too */
3026         if (ftrace_lookup_ip(ops->func_hash->notrace_hash, rec->ip))
3027                 return 0;
3028
3029         return 1;
3030 }
3031
3032 static int ftrace_update_code(struct module *mod, struct ftrace_page *new_pgs)
3033 {
3034         struct ftrace_page *pg;
3035         struct dyn_ftrace *p;
3036         u64 start, stop;
3037         unsigned long update_cnt = 0;
3038         unsigned long rec_flags = 0;
3039         int i;
3040
3041         start = ftrace_now(raw_smp_processor_id());
3042
3043         /*
3044          * When a module is loaded, this function is called to convert
3045          * the calls to mcount in its text to nops, and also to create
3046          * an entry in the ftrace data. Now, if ftrace is activated
3047          * after this call, but before the module sets its text to
3048          * read-only, the modification of enabling ftrace can fail if
3049          * the read-only is done while ftrace is converting the calls.
3050          * To prevent this, the module's records are set as disabled
3051          * and will be enabled after the call to set the module's text
3052          * to read-only.
3053          */
3054         if (mod)
3055                 rec_flags |= FTRACE_FL_DISABLED;
3056
3057         for (pg = new_pgs; pg; pg = pg->next) {
3058
3059                 for (i = 0; i < pg->index; i++) {
3060
3061                         /* If something went wrong, bail without enabling anything */
3062                         if (unlikely(ftrace_disabled))
3063                                 return -1;
3064
3065                         p = &pg->records[i];
3066                         p->flags = rec_flags;
3067
3068                         /*
3069                          * Do the initial record conversion from mcount jump
3070                          * to the NOP instructions.
3071                          */
3072                         if (!ftrace_code_disable(mod, p))
3073                                 break;
3074
3075                         update_cnt++;
3076                 }
3077         }
3078
3079         stop = ftrace_now(raw_smp_processor_id());
3080         ftrace_update_time = stop - start;
3081         ftrace_update_tot_cnt += update_cnt;
3082
3083         return 0;
3084 }
3085
3086 static int ftrace_allocate_records(struct ftrace_page *pg, int count)
3087 {
3088         int order;
3089         int cnt;
3090
3091         if (WARN_ON(!count))
3092                 return -EINVAL;
3093
3094         order = get_count_order(DIV_ROUND_UP(count, ENTRIES_PER_PAGE));
3095
3096         /*
3097          * We want to fill as much as possible. No more than a page
3098          * may be empty.
3099          */
3100         while ((PAGE_SIZE << order) / ENTRY_SIZE >= count + ENTRIES_PER_PAGE)
3101                 order--;
3102
3103  again:
3104         pg->records = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, order);
3105
3106         if (!pg->records) {
3107                 /* if we can't allocate this size, try something smaller */
3108                 if (!order)
3109                         return -ENOMEM;
3110                 order--;
3111                 goto again;
3112         }
3113
3114         cnt = (PAGE_SIZE << order) / ENTRY_SIZE;
3115         pg->size = cnt;
3116
3117         if (cnt > count)
3118                 cnt = count;
3119
3120         return cnt;
3121 }
3122
3123 static struct ftrace_page *
3124 ftrace_allocate_pages(unsigned long num_to_init)
3125 {
3126         struct ftrace_page *start_pg;
3127         struct ftrace_page *pg;
3128         int order;
3129         int cnt;
3130
3131         if (!num_to_init)
3132                 return 0;
3133
3134         start_pg = pg = kzalloc(sizeof(*pg), GFP_KERNEL);
3135         if (!pg)
3136                 return NULL;
3137
3138         /*
3139          * Try to allocate as much as possible in one continues
3140          * location that fills in all of the space. We want to
3141          * waste as little space as possible.
3142          */
3143         for (;;) {
3144                 cnt = ftrace_allocate_records(pg, num_to_init);
3145                 if (cnt < 0)
3146                         goto free_pages;
3147
3148                 num_to_init -= cnt;
3149                 if (!num_to_init)
3150                         break;
3151
3152                 pg->next = kzalloc(sizeof(*pg), GFP_KERNEL);
3153                 if (!pg->next)
3154                         goto free_pages;
3155
3156                 pg = pg->next;
3157         }
3158
3159         return start_pg;
3160
3161  free_pages:
3162         pg = start_pg;
3163         while (pg) {
3164                 order = get_count_order(pg->size / ENTRIES_PER_PAGE);
3165                 free_pages((unsigned long)pg->records, order);
3166                 start_pg = pg->next;
3167                 kfree(pg);
3168                 pg = start_pg;
3169         }
3170         pr_info("ftrace: FAILED to allocate memory for functions\n");
3171         return NULL;
3172 }
3173
3174 #define FTRACE_BUFF_MAX (KSYM_SYMBOL_LEN+4) /* room for wildcards */
3175
3176 struct ftrace_iterator {
3177         loff_t                          pos;
3178         loff_t                          func_pos;
3179         loff_t                          mod_pos;
3180         struct ftrace_page              *pg;
3181         struct dyn_ftrace               *func;
3182         struct ftrace_func_probe        *probe;
3183         struct ftrace_func_entry        *probe_entry;
3184         struct trace_parser             parser;
3185         struct ftrace_hash              *hash;
3186         struct ftrace_ops               *ops;
3187         struct trace_array              *tr;
3188         struct list_head                *mod_list;
3189         int                             pidx;
3190         int                             idx;
3191         unsigned                        flags;
3192 };
3193
3194 static void *
3195 t_probe_next(struct seq_file *m, loff_t *pos)
3196 {
3197         struct ftrace_iterator *iter = m->private;
3198         struct trace_array *tr = iter->ops->private;
3199         struct list_head *func_probes;
3200         struct ftrace_hash *hash;
3201         struct list_head *next;
3202         struct hlist_node *hnd = NULL;
3203         struct hlist_head *hhd;
3204         int size;
3205
3206         (*pos)++;
3207         iter->pos = *pos;
3208
3209         if (!tr)
3210                 return NULL;
3211
3212         func_probes = &tr->func_probes;
3213         if (list_empty(func_probes))
3214                 return NULL;
3215
3216         if (!iter->probe) {
3217                 next = func_probes->next;
3218                 iter->probe = list_entry(next, struct ftrace_func_probe, list);
3219         }
3220
3221         if (iter->probe_entry)
3222                 hnd = &iter->probe_entry->hlist;
3223
3224         hash = iter->probe->ops.func_hash->filter_hash;
3225
3226         /*
3227          * A probe being registered may temporarily have an empty hash
3228          * and it's at the end of the func_probes list.
3229          */
3230         if (!hash || hash == EMPTY_HASH)
3231                 return NULL;
3232
3233         size = 1 << hash->size_bits;
3234
3235  retry:
3236         if (iter->pidx >= size) {
3237                 if (iter->probe->list.next == func_probes)
3238                         return NULL;
3239                 next = iter->probe->list.next;
3240                 iter->probe = list_entry(next, struct ftrace_func_probe, list);
3241                 hash = iter->probe->ops.func_hash->filter_hash;
3242                 size = 1 << hash->size_bits;
3243                 iter->pidx = 0;
3244         }
3245
3246         hhd = &hash->buckets[iter->pidx];
3247
3248         if (hlist_empty(hhd)) {
3249                 iter->pidx++;
3250                 hnd = NULL;
3251                 goto retry;
3252         }
3253
3254         if (!hnd)
3255                 hnd = hhd->first;
3256         else {
3257                 hnd = hnd->next;
3258                 if (!hnd) {
3259                         iter->pidx++;
3260                         goto retry;
3261                 }
3262         }
3263
3264         if (WARN_ON_ONCE(!hnd))
3265                 return NULL;
3266
3267         iter->probe_entry = hlist_entry(hnd, struct ftrace_func_entry, hlist);
3268
3269         return iter;
3270 }
3271
3272 static void *t_probe_start(struct seq_file *m, loff_t *pos)
3273 {
3274         struct ftrace_iterator *iter = m->private;
3275         void *p = NULL;
3276         loff_t l;
3277
3278         if (!(iter->flags & FTRACE_ITER_DO_PROBES))
3279                 return NULL;
3280
3281         if (iter->mod_pos > *pos)
3282                 return NULL;
3283
3284         iter->probe = NULL;
3285         iter->probe_entry = NULL;
3286         iter->pidx = 0;
3287         for (l = 0; l <= (*pos - iter->mod_pos); ) {
3288                 p = t_probe_next(m, &l);
3289                 if (!p)
3290                         break;
3291         }
3292         if (!p)
3293                 return NULL;
3294
3295         /* Only set this if we have an item */
3296         iter->flags |= FTRACE_ITER_PROBE;
3297
3298         return iter;
3299 }
3300
3301 static int
3302 t_probe_show(struct seq_file *m, struct ftrace_iterator *iter)
3303 {
3304         struct ftrace_func_entry *probe_entry;
3305         struct ftrace_probe_ops *probe_ops;
3306         struct ftrace_func_probe *probe;
3307
3308         probe = iter->probe;
3309         probe_entry = iter->probe_entry;
3310
3311         if (WARN_ON_ONCE(!probe || !probe_entry))
3312                 return -EIO;
3313
3314         probe_ops = probe->probe_ops;
3315
3316         if (probe_ops->print)
3317                 return probe_ops->print(m, probe_entry->ip, probe_ops, probe->data);
3318
3319         seq_printf(m, "%ps:%ps\n", (void *)probe_entry->ip,
3320                    (void *)probe_ops->func);
3321
3322         return 0;
3323 }
3324
3325 static void *
3326 t_mod_next(struct seq_file *m, loff_t *pos)
3327 {
3328         struct ftrace_iterator *iter = m->private;
3329         struct trace_array *tr = iter->tr;
3330
3331         (*pos)++;
3332         iter->pos = *pos;
3333
3334         iter->mod_list = iter->mod_list->next;
3335
3336         if (iter->mod_list == &tr->mod_trace ||
3337             iter->mod_list == &tr->mod_notrace) {
3338                 iter->flags &= ~FTRACE_ITER_MOD;
3339                 return NULL;
3340         }
3341
3342         iter->mod_pos = *pos;
3343
3344         return iter;
3345 }
3346
3347 static void *t_mod_start(struct seq_file *m, loff_t *pos)
3348 {
3349         struct ftrace_iterator *iter = m->private;
3350         void *p = NULL;
3351         loff_t l;
3352
3353         if (iter->func_pos > *pos)
3354                 return NULL;
3355
3356         iter->mod_pos = iter->func_pos;
3357
3358         /* probes are only available if tr is set */
3359         if (!iter->tr)
3360                 return NULL;
3361
3362         for (l = 0; l <= (*pos - iter->func_pos); ) {
3363                 p = t_mod_next(m, &l);
3364                 if (!p)
3365                         break;
3366         }
3367         if (!p) {
3368                 iter->flags &= ~FTRACE_ITER_MOD;
3369                 return t_probe_start(m, pos);
3370         }
3371
3372         /* Only set this if we have an item */
3373         iter->flags |= FTRACE_ITER_MOD;
3374
3375         return iter;
3376 }
3377
3378 static int
3379 t_mod_show(struct seq_file *m, struct ftrace_iterator *iter)
3380 {
3381         struct ftrace_mod_load *ftrace_mod;
3382         struct trace_array *tr = iter->tr;
3383
3384         if (WARN_ON_ONCE(!iter->mod_list) ||
3385                          iter->mod_list == &tr->mod_trace ||
3386                          iter->mod_list == &tr->mod_notrace)
3387                 return -EIO;
3388
3389         ftrace_mod = list_entry(iter->mod_list, struct ftrace_mod_load, list);
3390
3391         if (ftrace_mod->func)
3392                 seq_printf(m, "%s", ftrace_mod->func);
3393         else
3394                 seq_putc(m, '*');
3395
3396         seq_printf(m, ":mod:%s\n", ftrace_mod->module);
3397
3398         return 0;
3399 }
3400
3401 static void *
3402 t_func_next(struct seq_file *m, loff_t *pos)
3403 {
3404         struct ftrace_iterator *iter = m->private;
3405         struct dyn_ftrace *rec = NULL;
3406
3407         (*pos)++;
3408
3409  retry:
3410         if (iter->idx >= iter->pg->index) {
3411                 if (iter->pg->next) {
3412                         iter->pg = iter->pg->next;
3413                         iter->idx = 0;
3414                         goto retry;
3415                 }
3416         } else {
3417                 rec = &iter->pg->records[iter->idx++];
3418                 if (((iter->flags & (FTRACE_ITER_FILTER | FTRACE_ITER_NOTRACE)) &&
3419                      !ftrace_lookup_ip(iter->hash, rec->ip)) ||
3420
3421                     ((iter->flags & FTRACE_ITER_ENABLED) &&
3422                      !(rec->flags & FTRACE_FL_ENABLED))) {
3423
3424                         rec = NULL;
3425                         goto retry;
3426                 }
3427         }
3428
3429         if (!rec)
3430                 return NULL;
3431
3432         iter->pos = iter->func_pos = *pos;
3433         iter->func = rec;
3434
3435         return iter;
3436 }
3437
3438 static void *
3439 t_next(struct seq_file *m, void *v, loff_t *pos)
3440 {
3441         struct ftrace_iterator *iter = m->private;
3442         loff_t l = *pos; /* t_probe_start() must use original pos */
3443         void *ret;
3444
3445         if (unlikely(ftrace_disabled))
3446                 return NULL;
3447
3448         if (iter->flags & FTRACE_ITER_PROBE)
3449                 return t_probe_next(m, pos);
3450
3451         if (iter->flags & FTRACE_ITER_MOD)
3452                 return t_mod_next(m, pos);
3453
3454         if (iter->flags & FTRACE_ITER_PRINTALL) {
3455                 /* next must increment pos, and t_probe_start does not */
3456                 (*pos)++;
3457                 return t_mod_start(m, &l);
3458         }
3459
3460         ret = t_func_next(m, pos);
3461
3462         if (!ret)
3463                 return t_mod_start(m, &l);
3464
3465         return ret;
3466 }
3467
3468 static void reset_iter_read(struct ftrace_iterator *iter)
3469 {
3470         iter->pos = 0;
3471         iter->func_pos = 0;
3472         iter->flags &= ~(FTRACE_ITER_PRINTALL | FTRACE_ITER_PROBE | FTRACE_ITER_MOD);
3473 }
3474
3475 static void *t_start(struct seq_file *m, loff_t *pos)
3476 {
3477         struct ftrace_iterator *iter = m->private;
3478         void *p = NULL;
3479         loff_t l;
3480
3481         mutex_lock(&ftrace_lock);
3482
3483         if (unlikely(ftrace_disabled))
3484                 return NULL;
3485
3486         /*
3487          * If an lseek was done, then reset and start from beginning.
3488          */
3489         if (*pos < iter->pos)
3490                 reset_iter_read(iter);
3491
3492         /*
3493          * For set_ftrace_filter reading, if we have the filter
3494          * off, we can short cut and just print out that all
3495          * functions are enabled.
3496          */
3497         if ((iter->flags & (FTRACE_ITER_FILTER | FTRACE_ITER_NOTRACE)) &&
3498             ftrace_hash_empty(iter->hash)) {
3499                 iter->func_pos = 1; /* Account for the message */
3500                 if (*pos > 0)
3501                         return t_mod_start(m, pos);
3502                 iter->flags |= FTRACE_ITER_PRINTALL;
3503                 /* reset in case of seek/pread */
3504                 iter->flags &= ~FTRACE_ITER_PROBE;
3505                 return iter;
3506         }
3507
3508         if (iter->flags & FTRACE_ITER_MOD)
3509                 return t_mod_start(m, pos);
3510
3511         /*
3512          * Unfortunately, we need to restart at ftrace_pages_start
3513          * every time we let go of the ftrace_mutex. This is because
3514          * those pointers can change without the lock.
3515          */
3516         iter->pg = ftrace_pages_start;
3517         iter->idx = 0;
3518         for (l = 0; l <= *pos; ) {
3519                 p = t_func_next(m, &l);
3520                 if (!p)
3521                         break;
3522         }
3523
3524         if (!p)
3525                 return t_mod_start(m, pos);
3526
3527         return iter;
3528 }
3529
3530 static void t_stop(struct seq_file *m, void *p)
3531 {
3532         mutex_unlock(&ftrace_lock);
3533 }
3534
3535 void * __weak
3536 arch_ftrace_trampoline_func(struct ftrace_ops *ops, struct dyn_ftrace *rec)
3537 {
3538         return NULL;
3539 }
3540
3541 static void add_trampoline_func(struct seq_file *m, struct ftrace_ops *ops,
3542                                 struct dyn_ftrace *rec)
3543 {
3544         void *ptr;
3545
3546         ptr = arch_ftrace_trampoline_func(ops, rec);
3547         if (ptr)
3548                 seq_printf(m, " ->%pS", ptr);
3549 }
3550
3551 static int t_show(struct seq_file *m, void *v)
3552 {
3553         struct ftrace_iterator *iter = m->private;
3554         struct dyn_ftrace *rec;
3555
3556         if (iter->flags & FTRACE_ITER_PROBE)
3557                 return t_probe_show(m, iter);
3558
3559         if (iter->flags & FTRACE_ITER_MOD)
3560                 return t_mod_show(m, iter);
3561
3562         if (iter->flags & FTRACE_ITER_PRINTALL) {
3563                 if (iter->flags & FTRACE_ITER_NOTRACE)
3564                         seq_puts(m, "#### no functions disabled ####\n");
3565                 else
3566                         seq_puts(m, "#### all functions enabled ####\n");
3567                 return 0;
3568         }
3569
3570         rec = iter->func;
3571
3572         if (!rec)
3573                 return 0;
3574
3575         seq_printf(m, "%ps", (void *)rec->ip);
3576         if (iter->flags & FTRACE_ITER_ENABLED) {
3577                 struct ftrace_ops *ops;
3578
3579                 seq_printf(m, " (%ld)%s%s",
3580                            ftrace_rec_count(rec),
3581                            rec->flags & FTRACE_FL_REGS ? " R" : "  ",
3582                            rec->flags & FTRACE_FL_IPMODIFY ? " I" : "  ");
3583                 if (rec->flags & FTRACE_FL_TRAMP_EN) {
3584                         ops = ftrace_find_tramp_ops_any(rec);
3585                         if (ops) {
3586                                 do {
3587                                         seq_printf(m, "\ttramp: %pS (%pS)",
3588                                                    (void *)ops->trampoline,
3589                                                    (void *)ops->func);
3590                                         add_trampoline_func(m, ops, rec);
3591                                         ops = ftrace_find_tramp_ops_next(rec, ops);
3592                                 } while (ops);
3593                         } else
3594                                 seq_puts(m, "\ttramp: ERROR!");
3595                 } else {
3596                         add_trampoline_func(m, NULL, rec);
3597                 }
3598         }       
3599
3600         seq_putc(m, '\n');
3601
3602         return 0;
3603 }
3604
3605 static const struct seq_operations show_ftrace_seq_ops = {
3606         .start = t_start,
3607         .next = t_next,
3608         .stop = t_stop,
3609         .show = t_show,
3610 };
3611
3612 static int
3613 ftrace_avail_open(struct inode *inode, struct file *file)
3614 {
3615         struct ftrace_iterator *iter;
3616
3617         if (unlikely(ftrace_disabled))
3618                 return -ENODEV;
3619
3620         iter = __seq_open_private(file, &show_ftrace_seq_ops, sizeof(*iter));
3621         if (!iter)
3622                 return -ENOMEM;
3623
3624         iter->pg = ftrace_pages_start;
3625         iter->ops = &global_ops;
3626
3627         return 0;
3628 }
3629
3630 static int
3631 ftrace_enabled_open(struct inode *inode, struct file *file)
3632 {
3633         struct ftrace_iterator *iter;
3634
3635         iter = __seq_open_private(file, &show_ftrace_seq_ops, sizeof(*iter));
3636         if (!iter)
3637                 return -ENOMEM;
3638
3639         iter->pg = ftrace_pages_start;
3640         iter->flags = FTRACE_ITER_ENABLED;
3641         iter->ops = &global_ops;
3642
3643         return 0;
3644 }
3645
3646 /**
3647  * ftrace_regex_open - initialize function tracer filter files
3648  * @ops: The ftrace_ops that hold the hash filters
3649  * @flag: The type of filter to process
3650  * @inode: The inode, usually passed in to your open routine
3651  * @file: The file, usually passed in to your open routine
3652  *
3653  * ftrace_regex_open() initializes the filter files for the
3654  * @ops. Depending on @flag it may process the filter hash or
3655  * the notrace hash of @ops. With this called from the open
3656  * routine, you can use ftrace_filter_write() for the write
3657  * routine if @flag has FTRACE_ITER_FILTER set, or
3658  * ftrace_notrace_write() if @flag has FTRACE_ITER_NOTRACE set.
3659  * tracing_lseek() should be used as the lseek routine, and
3660  * release must call ftrace_regex_release().
3661  */
3662 int
3663 ftrace_regex_open(struct ftrace_ops *ops, int flag,
3664                   struct inode *inode, struct file *file)
3665 {
3666         struct ftrace_iterator *iter;
3667         struct ftrace_hash *hash;
3668         struct list_head *mod_head;
3669         struct trace_array *tr = ops->private;
3670         int ret = -ENOMEM;
3671
3672         ftrace_ops_init(ops);
3673
3674         if (unlikely(ftrace_disabled))
3675                 return -ENODEV;
3676
3677         if (tr && trace_array_get(tr) < 0)
3678                 return -ENODEV;
3679
3680         iter = kzalloc(sizeof(*iter), GFP_KERNEL);
3681         if (!iter)
3682                 goto out;
3683
3684         if (trace_parser_get_init(&iter->parser, FTRACE_BUFF_MAX))
3685                 goto out;
3686
3687         iter->ops = ops;
3688         iter->flags = flag;
3689         iter->tr = tr;
3690
3691         mutex_lock(&ops->func_hash->regex_lock);
3692
3693         if (flag & FTRACE_ITER_NOTRACE) {
3694                 hash = ops->func_hash->notrace_hash;
3695                 mod_head = tr ? &tr->mod_notrace : NULL;
3696         } else {
3697                 hash = ops->func_hash->filter_hash;
3698                 mod_head = tr ? &tr->mod_trace : NULL;
3699         }
3700
3701         iter->mod_list = mod_head;
3702
3703         if (file->f_mode & FMODE_WRITE) {
3704                 const int size_bits = FTRACE_HASH_DEFAULT_BITS;
3705
3706                 if (file->f_flags & O_TRUNC) {
3707                         iter->hash = alloc_ftrace_hash(size_bits);
3708                         clear_ftrace_mod_list(mod_head);
3709                 } else {
3710                         iter->hash = alloc_and_copy_ftrace_hash(size_bits, hash);
3711                 }
3712
3713                 if (!iter->hash) {
3714                         trace_parser_put(&iter->parser);
3715                         goto out_unlock;
3716                 }
3717         } else
3718                 iter->hash = hash;
3719
3720         ret = 0;
3721
3722         if (file->f_mode & FMODE_READ) {
3723                 iter->pg = ftrace_pages_start;
3724
3725                 ret = seq_open(file, &show_ftrace_seq_ops);
3726                 if (!ret) {
3727                         struct seq_file *m = file->private_data;
3728                         m->private = iter;
3729                 } else {
3730                         /* Failed */
3731                         free_ftrace_hash(iter->hash);
3732                         trace_parser_put(&iter->parser);
3733                 }
3734         } else
3735                 file->private_data = iter;
3736
3737  out_unlock:
3738         mutex_unlock(&ops->func_hash->regex_lock);
3739
3740  out:
3741         if (ret) {
3742                 kfree(iter);
3743                 if (tr)
3744                         trace_array_put(tr);
3745         }
3746
3747         return ret;
3748 }
3749
3750 static int
3751 ftrace_filter_open(struct inode *inode, struct file *file)
3752 {
3753         struct ftrace_ops *ops = inode->i_private;
3754
3755         return ftrace_regex_open(ops,
3756                         FTRACE_ITER_FILTER | FTRACE_ITER_DO_PROBES,
3757                         inode, file);
3758 }
3759
3760 static int
3761 ftrace_notrace_open(struct inode *inode, struct file *file)
3762 {
3763         struct ftrace_ops *ops = inode->i_private;
3764
3765         return ftrace_regex_open(ops, FTRACE_ITER_NOTRACE,
3766                                  inode, file);
3767 }
3768
3769 /* Type for quick search ftrace basic regexes (globs) from filter_parse_regex */
3770 struct ftrace_glob {
3771         char *search;
3772         unsigned len;
3773         int type;
3774 };
3775
3776 /*
3777  * If symbols in an architecture don't correspond exactly to the user-visible
3778  * name of what they represent, it is possible to define this function to
3779  * perform the necessary adjustments.
3780 */
3781 char * __weak arch_ftrace_match_adjust(char *str, const char *search)
3782 {
3783         return str;
3784 }
3785
3786 static int ftrace_match(char *str, struct ftrace_glob *g)
3787 {
3788         int matched = 0;
3789         int slen;
3790
3791         str = arch_ftrace_match_adjust(str, g->search);
3792
3793         switch (g->type) {
3794         case MATCH_FULL:
3795                 if (strcmp(str, g->search) == 0)
3796                         matched = 1;
3797                 break;
3798         case MATCH_FRONT_ONLY:
3799                 if (strncmp(str, g->search, g->len) == 0)
3800                         matched = 1;
3801                 break;
3802         case MATCH_MIDDLE_ONLY:
3803                 if (strstr(str, g->search))
3804                         matched = 1;
3805                 break;
3806         case MATCH_END_ONLY:
3807                 slen = strlen(str);
3808                 if (slen >= g->len &&
3809                     memcmp(str + slen - g->len, g->search, g->len) == 0)
3810                         matched = 1;
3811                 break;
3812         case MATCH_GLOB:
3813                 if (glob_match(g->search, str))
3814                         matched = 1;
3815                 break;
3816         }
3817
3818         return matched;
3819 }
3820
3821 static int
3822 enter_record(struct ftrace_hash *hash, struct dyn_ftrace *rec, int clear_filter)
3823 {
3824         struct ftrace_func_entry *entry;
3825         int ret = 0;
3826
3827         entry = ftrace_lookup_ip(hash, rec->ip);
3828         if (clear_filter) {
3829                 /* Do nothing if it doesn't exist */
3830                 if (!entry)
3831                         return 0;
3832
3833                 free_hash_entry(hash, entry);
3834         } else {
3835                 /* Do nothing if it exists */
3836                 if (entry)
3837                         return 0;
3838
3839                 ret = add_hash_entry(hash, rec->ip);
3840         }
3841         return ret;
3842 }
3843
3844 static int
3845 ftrace_match_record(struct dyn_ftrace *rec, struct ftrace_glob *func_g,
3846                 struct ftrace_glob *mod_g, int exclude_mod)
3847 {
3848         char str[KSYM_SYMBOL_LEN];
3849         char *modname;
3850
3851         kallsyms_lookup(rec->ip, NULL, NULL, &modname, str);
3852
3853         if (mod_g) {
3854                 int mod_matches = (modname) ? ftrace_match(modname, mod_g) : 0;
3855
3856                 /* blank module name to match all modules */
3857                 if (!mod_g->len) {
3858                         /* blank module globbing: modname xor exclude_mod */
3859                         if (!exclude_mod != !modname)
3860                                 goto func_match;
3861                         return 0;
3862                 }
3863
3864                 /*
3865                  * exclude_mod is set to trace everything but the given
3866                  * module. If it is set and the module matches, then
3867                  * return 0. If it is not set, and the module doesn't match
3868                  * also return 0. Otherwise, check the function to see if
3869                  * that matches.
3870                  */
3871                 if (!mod_matches == !exclude_mod)
3872                         return 0;
3873 func_match:
3874                 /* blank search means to match all funcs in the mod */
3875                 if (!func_g->len)
3876                         return 1;
3877         }
3878
3879         return ftrace_match(str, func_g);
3880 }
3881
3882 static int
3883 match_records(struct ftrace_hash *hash, char *func, int len, char *mod)
3884 {
3885         struct ftrace_page *pg;
3886         struct dyn_ftrace *rec;
3887         struct ftrace_glob func_g = { .type = MATCH_FULL };
3888         struct ftrace_glob mod_g = { .type = MATCH_FULL };
3889         struct ftrace_glob *mod_match = (mod) ? &mod_g : NULL;
3890         int exclude_mod = 0;
3891         int found = 0;
3892         int ret;
3893         int clear_filter = 0;
3894
3895         if (func) {
3896                 func_g.type = filter_parse_regex(func, len, &func_g.search,
3897                                                  &clear_filter);
3898                 func_g.len = strlen(func_g.search);
3899         }
3900
3901         if (mod) {
3902                 mod_g.type = filter_parse_regex(mod, strlen(mod),
3903                                 &mod_g.search, &exclude_mod);
3904                 mod_g.len = strlen(mod_g.search);
3905         }
3906
3907         mutex_lock(&ftrace_lock);
3908
3909         if (unlikely(ftrace_disabled))
3910                 goto out_unlock;
3911
3912         do_for_each_ftrace_rec(pg, rec) {
3913
3914                 if (rec->flags & FTRACE_FL_DISABLED)
3915                         continue;
3916
3917                 if (ftrace_match_record(rec, &func_g, mod_match, exclude_mod)) {
3918                         ret = enter_record(hash, rec, clear_filter);
3919                         if (ret < 0) {
3920                                 found = ret;
3921                                 goto out_unlock;
3922                         }
3923                         found = 1;
3924                 }
3925         } while_for_each_ftrace_rec();
3926  out_unlock:
3927         mutex_unlock(&ftrace_lock);
3928
3929         return found;
3930 }
3931
3932 static int
3933 ftrace_match_records(struct ftrace_hash *hash, char *buff, int len)
3934 {
3935         return match_records(hash, buff, len, NULL);
3936 }
3937
3938 static void ftrace_ops_update_code(struct ftrace_ops *ops,
3939                                    struct ftrace_ops_hash *old_hash)
3940 {
3941         struct ftrace_ops *op;
3942
3943         if (!ftrace_enabled)
3944                 return;
3945
3946         if (ops->flags & FTRACE_OPS_FL_ENABLED) {
3947                 ftrace_run_modify_code(ops, FTRACE_UPDATE_CALLS, old_hash);
3948                 return;
3949         }
3950
3951         /*
3952          * If this is the shared global_ops filter, then we need to
3953          * check if there is another ops that shares it, is enabled.
3954          * If so, we still need to run the modify code.
3955          */
3956         if (ops->func_hash != &global_ops.local_hash)
3957                 return;
3958
3959         do_for_each_ftrace_op(op, ftrace_ops_list) {
3960                 if (op->func_hash == &global_ops.local_hash &&
3961                     op->flags & FTRACE_OPS_FL_ENABLED) {
3962                         ftrace_run_modify_code(op, FTRACE_UPDATE_CALLS, old_hash);
3963                         /* Only need to do this once */
3964                         return;
3965                 }
3966         } while_for_each_ftrace_op(op);
3967 }
3968
3969 static int ftrace_hash_move_and_update_ops(struct ftrace_ops *ops,
3970                                            struct ftrace_hash **orig_hash,
3971                                            struct ftrace_hash *hash,
3972                                            int enable)
3973 {
3974         struct ftrace_ops_hash old_hash_ops;
3975         struct ftrace_hash *old_hash;
3976         int ret;
3977
3978         old_hash = *orig_hash;
3979         old_hash_ops.filter_hash = ops->func_hash->filter_hash;
3980         old_hash_ops.notrace_hash = ops->func_hash->notrace_hash;
3981         ret = ftrace_hash_move(ops, enable, orig_hash, hash);
3982         if (!ret) {
3983                 ftrace_ops_update_code(ops, &old_hash_ops);
3984                 free_ftrace_hash_rcu(old_hash);
3985         }
3986         return ret;
3987 }
3988
3989 static bool module_exists(const char *module)
3990 {
3991         /* All modules have the symbol __this_module */
3992         const char this_mod[] = "__this_module";
3993         const int modname_size = MAX_PARAM_PREFIX_LEN + sizeof(this_mod) + 1;
3994         char modname[modname_size + 1];
3995         unsigned long val;
3996         int n;
3997
3998         n = snprintf(modname, modname_size + 1, "%s:%s", module, this_mod);
3999
4000         if (n > modname_size)
4001                 return false;
4002
4003         val = module_kallsyms_lookup_name(modname);
4004         return val != 0;
4005 }
4006
4007 static int cache_mod(struct trace_array *tr,
4008                      const char *func, char *module, int enable)
4009 {
4010         struct ftrace_mod_load *ftrace_mod, *n;
4011         struct list_head *head = enable ? &tr->mod_trace : &tr->mod_notrace;
4012         int ret;
4013
4014         mutex_lock(&ftrace_lock);
4015
4016         /* We do not cache inverse filters */
4017         if (func[0] == '!') {
4018                 func++;
4019                 ret = -EINVAL;
4020
4021                 /* Look to remove this hash */
4022                 list_for_each_entry_safe(ftrace_mod, n, head, list) {
4023                         if (strcmp(ftrace_mod->module, module) != 0)
4024                                 continue;
4025
4026                         /* no func matches all */
4027                         if (strcmp(func, "*") == 0 ||
4028                             (ftrace_mod->func &&
4029                              strcmp(ftrace_mod->func, func) == 0)) {
4030                                 ret = 0;
4031                                 free_ftrace_mod(ftrace_mod);
4032                                 continue;
4033                         }
4034                 }
4035                 goto out;
4036         }
4037
4038         ret = -EINVAL;
4039         /* We only care about modules that have not been loaded yet */
4040         if (module_exists(module))
4041                 goto out;
4042
4043         /* Save this string off, and execute it when the module is loaded */
4044         ret = ftrace_add_mod(tr, func, module, enable);
4045  out:
4046         mutex_unlock(&ftrace_lock);
4047
4048         return ret;
4049 }
4050
4051 static int
4052 ftrace_set_regex(struct ftrace_ops *ops, unsigned char *buf, int len,
4053                  int reset, int enable);
4054
4055 #ifdef CONFIG_MODULES
4056 static void process_mod_list(struct list_head *head, struct ftrace_ops *ops,
4057                              char *mod, bool enable)
4058 {
4059         struct ftrace_mod_load *ftrace_mod, *n;
4060         struct ftrace_hash **orig_hash, *new_hash;
4061         LIST_HEAD(process_mods);
4062         char *func;
4063         int ret;
4064
4065         mutex_lock(&ops->func_hash->regex_lock);
4066
4067         if (enable)
4068                 orig_hash = &ops->func_hash->filter_hash;
4069         else
4070                 orig_hash = &ops->func_hash->notrace_hash;
4071
4072         new_hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS,
4073                                               *orig_hash);
4074         if (!new_hash)
4075                 goto out; /* warn? */
4076
4077         mutex_lock(&ftrace_lock);
4078
4079         list_for_each_entry_safe(ftrace_mod, n, head, list) {
4080
4081                 if (strcmp(ftrace_mod->module, mod) != 0)
4082                         continue;
4083
4084                 if (ftrace_mod->func)
4085                         func = kstrdup(ftrace_mod->func, GFP_KERNEL);
4086                 else
4087                         func = kstrdup("*", GFP_KERNEL);
4088
4089                 if (!func) /* warn? */
4090                         continue;
4091
4092                 list_del(&ftrace_mod->list);
4093                 list_add(&ftrace_mod->list, &process_mods);
4094
4095                 /* Use the newly allocated func, as it may be "*" */
4096                 kfree(ftrace_mod->func);
4097                 ftrace_mod->func = func;
4098         }
4099
4100         mutex_unlock(&ftrace_lock);
4101
4102         list_for_each_entry_safe(ftrace_mod, n, &process_mods, list) {
4103
4104                 func = ftrace_mod->func;
4105
4106                 /* Grabs ftrace_lock, which is why we have this extra step */
4107                 match_records(new_hash, func, strlen(func), mod);
4108                 free_ftrace_mod(ftrace_mod);
4109         }
4110
4111         if (enable && list_empty(head))
4112                 new_hash->flags &= ~FTRACE_HASH_FL_MOD;
4113
4114         mutex_lock(&ftrace_lock);
4115
4116         ret = ftrace_hash_move_and_update_ops(ops, orig_hash,
4117                                               new_hash, enable);
4118         mutex_unlock(&ftrace_lock);
4119
4120  out:
4121         mutex_unlock(&ops->func_hash->regex_lock);
4122
4123         free_ftrace_hash(new_hash);
4124 }
4125
4126 static void process_cached_mods(const char *mod_name)
4127 {
4128         struct trace_array *tr;
4129         char *mod;
4130
4131         mod = kstrdup(mod_name, GFP_KERNEL);
4132         if (!mod)
4133                 return;
4134
4135         mutex_lock(&trace_types_lock);
4136         list_for_each_entry(tr, &ftrace_trace_arrays, list) {
4137                 if (!list_empty(&tr->mod_trace))
4138                         process_mod_list(&tr->mod_trace, tr->ops, mod, true);
4139                 if (!list_empty(&tr->mod_notrace))
4140                         process_mod_list(&tr->mod_notrace, tr->ops, mod, false);
4141         }
4142         mutex_unlock(&trace_types_lock);
4143
4144         kfree(mod);
4145 }
4146 #endif
4147
4148 /*
4149  * We register the module command as a template to show others how
4150  * to register the a command as well.
4151  */
4152
4153 static int
4154 ftrace_mod_callback(struct trace_array *tr, struct ftrace_hash *hash,
4155                     char *func_orig, char *cmd, char *module, int enable)
4156 {
4157         char *func;
4158         int ret;
4159
4160         /* match_records() modifies func, and we need the original */
4161         func = kstrdup(func_orig, GFP_KERNEL);
4162         if (!func)
4163                 return -ENOMEM;
4164
4165         /*
4166          * cmd == 'mod' because we only registered this func
4167          * for the 'mod' ftrace_func_command.
4168          * But if you register one func with multiple commands,
4169          * you can tell which command was used by the cmd
4170          * parameter.
4171          */
4172         ret = match_records(hash, func, strlen(func), module);
4173         kfree(func);
4174
4175         if (!ret)
4176                 return cache_mod(tr, func_orig, module, enable);
4177         if (ret < 0)
4178                 return ret;
4179         return 0;
4180 }
4181
4182 static struct ftrace_func_command ftrace_mod_cmd = {
4183         .name                   = "mod",
4184         .func                   = ftrace_mod_callback,
4185 };
4186
4187 static int __init ftrace_mod_cmd_init(void)
4188 {
4189         return register_ftrace_command(&ftrace_mod_cmd);
4190 }
4191 core_initcall(ftrace_mod_cmd_init);
4192
4193 static void function_trace_probe_call(unsigned long ip, unsigned long parent_ip,
4194                                       struct ftrace_ops *op, struct pt_regs *pt_regs)
4195 {
4196         struct ftrace_probe_ops *probe_ops;
4197         struct ftrace_func_probe *probe;
4198
4199         probe = container_of(op, struct ftrace_func_probe, ops);
4200         probe_ops = probe->probe_ops;
4201
4202         /*
4203          * Disable preemption for these calls to prevent a RCU grace
4204          * period. This syncs the hash iteration and freeing of items
4205          * on the hash. rcu_read_lock is too dangerous here.
4206          */
4207         preempt_disable_notrace();
4208         probe_ops->func(ip, parent_ip, probe->tr, probe_ops, probe->data);
4209         preempt_enable_notrace();
4210 }
4211
4212 struct ftrace_func_map {
4213         struct ftrace_func_entry        entry;
4214         void                            *data;
4215 };
4216
4217 struct ftrace_func_mapper {
4218         struct ftrace_hash              hash;
4219 };
4220
4221 /**
4222  * allocate_ftrace_func_mapper - allocate a new ftrace_func_mapper
4223  *
4224  * Returns a ftrace_func_mapper descriptor that can be used to map ips to data.
4225  */
4226 struct ftrace_func_mapper *allocate_ftrace_func_mapper(void)
4227 {
4228         struct ftrace_hash *hash;
4229
4230         /*
4231          * The mapper is simply a ftrace_hash, but since the entries
4232          * in the hash are not ftrace_func_entry type, we define it
4233          * as a separate structure.
4234          */
4235         hash = alloc_ftrace_hash(FTRACE_HASH_DEFAULT_BITS);
4236         return (struct ftrace_func_mapper *)hash;
4237 }
4238
4239 /**
4240  * ftrace_func_mapper_find_ip - Find some data mapped to an ip
4241  * @mapper: The mapper that has the ip maps
4242  * @ip: the instruction pointer to find the data for
4243  *
4244  * Returns the data mapped to @ip if found otherwise NULL. The return
4245  * is actually the address of the mapper data pointer. The address is
4246  * returned for use cases where the data is no bigger than a long, and
4247  * the user can use the data pointer as its data instead of having to
4248  * allocate more memory for the reference.
4249  */
4250 void **ftrace_func_mapper_find_ip(struct ftrace_func_mapper *mapper,
4251                                   unsigned long ip)
4252 {
4253         struct ftrace_func_entry *entry;
4254         struct ftrace_func_map *map;
4255
4256         entry = ftrace_lookup_ip(&mapper->hash, ip);
4257         if (!entry)
4258                 return NULL;
4259
4260         map = (struct ftrace_func_map *)entry;
4261         return &map->data;
4262 }
4263
4264 /**
4265  * ftrace_func_mapper_add_ip - Map some data to an ip
4266  * @mapper: The mapper that has the ip maps
4267  * @ip: The instruction pointer address to map @data to
4268  * @data: The data to map to @ip
4269  *
4270  * Returns 0 on succes otherwise an error.
4271  */
4272 int ftrace_func_mapper_add_ip(struct ftrace_func_mapper *mapper,
4273                               unsigned long ip, void *data)
4274 {
4275         struct ftrace_func_entry *entry;
4276         struct ftrace_func_map *map;
4277
4278         entry = ftrace_lookup_ip(&mapper->hash, ip);
4279         if (entry)
4280                 return -EBUSY;
4281
4282         map = kmalloc(sizeof(*map), GFP_KERNEL);
4283         if (!map)
4284                 return -ENOMEM;
4285
4286         map->entry.ip = ip;
4287         map->data = data;
4288
4289         __add_hash_entry(&mapper->hash, &map->entry);
4290
4291         return 0;
4292 }
4293
4294 /**
4295  * ftrace_func_mapper_remove_ip - Remove an ip from the mapping
4296  * @mapper: The mapper that has the ip maps
4297  * @ip: The instruction pointer address to remove the data from
4298  *
4299  * Returns the data if it is found, otherwise NULL.
4300  * Note, if the data pointer is used as the data itself, (see 
4301  * ftrace_func_mapper_find_ip(), then the return value may be meaningless,
4302  * if the data pointer was set to zero.
4303  */
4304 void *ftrace_func_mapper_remove_ip(struct ftrace_func_mapper *mapper,
4305                                    unsigned long ip)
4306 {
4307         struct ftrace_func_entry *entry;
4308         struct ftrace_func_map *map;
4309         void *data;
4310
4311         entry = ftrace_lookup_ip(&mapper->hash, ip);
4312         if (!entry)
4313                 return NULL;
4314
4315         map = (struct ftrace_func_map *)entry;
4316         data = map->data;
4317
4318         remove_hash_entry(&mapper->hash, entry);
4319         kfree(entry);
4320
4321         return data;
4322 }
4323
4324 /**
4325  * free_ftrace_func_mapper - free a mapping of ips and data
4326  * @mapper: The mapper that has the ip maps
4327  * @free_func: A function to be called on each data item.
4328  *
4329  * This is used to free the function mapper. The @free_func is optional
4330  * and can be used if the data needs to be freed as well.
4331  */
4332 void free_ftrace_func_mapper(struct ftrace_func_mapper *mapper,
4333                              ftrace_mapper_func free_func)
4334 {
4335         struct ftrace_func_entry *entry;
4336         struct ftrace_func_map *map;
4337         struct hlist_head *hhd;
4338         int size, i;
4339
4340         if (!mapper)
4341                 return;
4342
4343         if (free_func && mapper->hash.count) {
4344                 size = 1 << mapper->hash.size_bits;
4345                 for (i = 0; i < size; i++) {
4346                         hhd = &mapper->hash.buckets[i];
4347                         hlist_for_each_entry(entry, hhd, hlist) {
4348                                 map = (struct ftrace_func_map *)entry;
4349                                 free_func(map);
4350                         }
4351                 }
4352         }
4353         free_ftrace_hash(&mapper->hash);
4354 }
4355
4356 static void release_probe(struct ftrace_func_probe *probe)
4357 {
4358         struct ftrace_probe_ops *probe_ops;
4359
4360         mutex_lock(&ftrace_lock);
4361
4362         WARN_ON(probe->ref <= 0);
4363
4364         /* Subtract the ref that was used to protect this instance */
4365         probe->ref--;
4366
4367         if (!probe->ref) {
4368                 probe_ops = probe->probe_ops;
4369                 /*
4370                  * Sending zero as ip tells probe_ops to free
4371                  * the probe->data itself
4372                  */
4373                 if (probe_ops->free)
4374                         probe_ops->free(probe_ops, probe->tr, 0, probe->data);
4375                 list_del(&probe->list);
4376                 kfree(probe);
4377         }
4378         mutex_unlock(&ftrace_lock);
4379 }
4380
4381 static void acquire_probe_locked(struct ftrace_func_probe *probe)
4382 {
4383         /*
4384          * Add one ref to keep it from being freed when releasing the
4385          * ftrace_lock mutex.
4386          */
4387         probe->ref++;
4388 }
4389
4390 int
4391 register_ftrace_function_probe(char *glob, struct trace_array *tr,
4392                                struct ftrace_probe_ops *probe_ops,
4393                                void *data)
4394 {
4395         struct ftrace_func_entry *entry;
4396         struct ftrace_func_probe *probe;
4397         struct ftrace_hash **orig_hash;
4398         struct ftrace_hash *old_hash;
4399         struct ftrace_hash *hash;
4400         int count = 0;
4401         int size;
4402         int ret;
4403         int i;
4404
4405         if (WARN_ON(!tr))
4406                 return -EINVAL;
4407
4408         /* We do not support '!' for function probes */
4409         if (WARN_ON(glob[0] == '!'))
4410                 return -EINVAL;
4411
4412
4413         mutex_lock(&ftrace_lock);
4414         /* Check if the probe_ops is already registered */
4415         list_for_each_entry(probe, &tr->func_probes, list) {
4416                 if (probe->probe_ops == probe_ops)
4417                         break;
4418         }
4419         if (&probe->list == &tr->func_probes) {
4420                 probe = kzalloc(sizeof(*probe), GFP_KERNEL);
4421                 if (!probe) {
4422                         mutex_unlock(&ftrace_lock);
4423                         return -ENOMEM;
4424                 }
4425                 probe->probe_ops = probe_ops;
4426                 probe->ops.func = function_trace_probe_call;
4427                 probe->tr = tr;
4428                 ftrace_ops_init(&probe->ops);
4429                 list_add(&probe->list, &tr->func_probes);
4430         }
4431
4432         acquire_probe_locked(probe);
4433
4434         mutex_unlock(&ftrace_lock);
4435
4436         /*
4437          * Note, there's a small window here that the func_hash->filter_hash
4438          * may be NULL or empty. Need to be carefule when reading the loop.
4439          */
4440         mutex_lock(&probe->ops.func_hash->regex_lock);
4441
4442         orig_hash = &probe->ops.func_hash->filter_hash;
4443         old_hash = *orig_hash;
4444         hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, old_hash);
4445
4446         if (!hash) {
4447                 ret = -ENOMEM;
4448                 goto out;
4449         }
4450
4451         ret = ftrace_match_records(hash, glob, strlen(glob));
4452
4453         /* Nothing found? */
4454         if (!ret)
4455                 ret = -EINVAL;
4456
4457         if (ret < 0)
4458                 goto out;
4459
4460         size = 1 << hash->size_bits;
4461         for (i = 0; i < size; i++) {
4462                 hlist_for_each_entry(entry, &hash->buckets[i], hlist) {
4463                         if (ftrace_lookup_ip(old_hash, entry->ip))
4464                                 continue;
4465                         /*
4466                          * The caller might want to do something special
4467                          * for each function we find. We call the callback
4468                          * to give the caller an opportunity to do so.
4469                          */
4470                         if (probe_ops->init) {
4471                                 ret = probe_ops->init(probe_ops, tr,
4472                                                       entry->ip, data,
4473                                                       &probe->data);
4474                                 if (ret < 0) {
4475                                         if (probe_ops->free && count)
4476                                                 probe_ops->free(probe_ops, tr,
4477                                                                 0, probe->data);
4478                                         probe->data = NULL;
4479                                         goto out;
4480                                 }
4481                         }
4482                         count++;
4483                 }
4484         }
4485
4486         mutex_lock(&ftrace_lock);
4487
4488         if (!count) {
4489                 /* Nothing was added? */
4490                 ret = -EINVAL;
4491                 goto out_unlock;
4492         }
4493
4494         ret = ftrace_hash_move_and_update_ops(&probe->ops, orig_hash,
4495                                               hash, 1);
4496         if (ret < 0)
4497                 goto err_unlock;
4498
4499         /* One ref for each new function traced */
4500         probe->ref += count;
4501
4502         if (!(probe->ops.flags & FTRACE_OPS_FL_ENABLED))
4503                 ret = ftrace_startup(&probe->ops, 0);
4504
4505  out_unlock:
4506         mutex_unlock(&ftrace_lock);
4507
4508         if (!ret)
4509                 ret = count;
4510  out:
4511         mutex_unlock(&probe->ops.func_hash->regex_lock);
4512         free_ftrace_hash(hash);
4513
4514         release_probe(probe);
4515
4516         return ret;
4517
4518  err_unlock:
4519         if (!probe_ops->free || !count)
4520                 goto out_unlock;
4521
4522         /* Failed to do the move, need to call the free functions */
4523         for (i = 0; i < size; i++) {
4524                 hlist_for_each_entry(entry, &hash->buckets[i], hlist) {
4525                         if (ftrace_lookup_ip(old_hash, entry->ip))
4526                                 continue;
4527                         probe_ops->free(probe_ops, tr, entry->ip, probe->data);
4528                 }
4529         }
4530         goto out_unlock;
4531 }
4532
4533 int
4534 unregister_ftrace_function_probe_func(char *glob, struct trace_array *tr,
4535                                       struct ftrace_probe_ops *probe_ops)
4536 {
4537         struct ftrace_ops_hash old_hash_ops;
4538         struct ftrace_func_entry *entry;
4539         struct ftrace_func_probe *probe;
4540         struct ftrace_glob func_g;
4541         struct ftrace_hash **orig_hash;
4542         struct ftrace_hash *old_hash;
4543         struct ftrace_hash *hash = NULL;
4544         struct hlist_node *tmp;
4545         struct hlist_head hhd;
4546         char str[KSYM_SYMBOL_LEN];
4547         int count = 0;
4548         int i, ret = -ENODEV;
4549         int size;
4550
4551         if (!glob || !strlen(glob) || !strcmp(glob, "*"))
4552                 func_g.search = NULL;
4553         else {
4554                 int not;
4555
4556                 func_g.type = filter_parse_regex(glob, strlen(glob),
4557                                                  &func_g.search, &not);
4558                 func_g.len = strlen(func_g.search);
4559
4560                 /* we do not support '!' for function probes */
4561                 if (WARN_ON(not))
4562                         return -EINVAL;
4563         }
4564
4565         mutex_lock(&ftrace_lock);
4566         /* Check if the probe_ops is already registered */
4567         list_for_each_entry(probe, &tr->func_probes, list) {
4568                 if (probe->probe_ops == probe_ops)
4569                         break;
4570         }
4571         if (&probe->list == &tr->func_probes)
4572                 goto err_unlock_ftrace;
4573
4574         ret = -EINVAL;
4575         if (!(probe->ops.flags & FTRACE_OPS_FL_INITIALIZED))
4576                 goto err_unlock_ftrace;
4577
4578         acquire_probe_locked(probe);
4579
4580         mutex_unlock(&ftrace_lock);
4581
4582         mutex_lock(&probe->ops.func_hash->regex_lock);
4583
4584         orig_hash = &probe->ops.func_hash->filter_hash;
4585         old_hash = *orig_hash;
4586
4587         if (ftrace_hash_empty(old_hash))
4588                 goto out_unlock;
4589
4590         old_hash_ops.filter_hash = old_hash;
4591         /* Probes only have filters */
4592         old_hash_ops.notrace_hash = NULL;
4593
4594         ret = -ENOMEM;
4595         hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, old_hash);
4596         if (!hash)
4597                 goto out_unlock;
4598
4599         INIT_HLIST_HEAD(&hhd);
4600
4601         size = 1 << hash->size_bits;
4602         for (i = 0; i < size; i++) {
4603                 hlist_for_each_entry_safe(entry, tmp, &hash->buckets[i], hlist) {
4604
4605                         if (func_g.search) {
4606                                 kallsyms_lookup(entry->ip, NULL, NULL,
4607                                                 NULL, str);
4608                                 if (!ftrace_match(str, &func_g))
4609                                         continue;
4610                         }
4611                         count++;
4612                         remove_hash_entry(hash, entry);
4613                         hlist_add_head(&entry->hlist, &hhd);
4614                 }
4615         }
4616
4617         /* Nothing found? */
4618         if (!count) {
4619                 ret = -EINVAL;
4620                 goto out_unlock;
4621         }
4622
4623         mutex_lock(&ftrace_lock);
4624
4625         WARN_ON(probe->ref < count);
4626
4627         probe->ref -= count;
4628
4629         if (ftrace_hash_empty(hash))
4630                 ftrace_shutdown(&probe->ops, 0);
4631
4632         ret = ftrace_hash_move_and_update_ops(&probe->ops, orig_hash,
4633                                               hash, 1);
4634
4635         /* still need to update the function call sites */
4636         if (ftrace_enabled && !ftrace_hash_empty(hash))
4637                 ftrace_run_modify_code(&probe->ops, FTRACE_UPDATE_CALLS,
4638                                        &old_hash_ops);
4639         synchronize_sched();
4640
4641         hlist_for_each_entry_safe(entry, tmp, &hhd, hlist) {
4642                 hlist_del(&entry->hlist);
4643                 if (probe_ops->free)
4644                         probe_ops->free(probe_ops, tr, entry->ip, probe->data);
4645                 kfree(entry);
4646         }
4647         mutex_unlock(&ftrace_lock);
4648
4649  out_unlock:
4650         mutex_unlock(&probe->ops.func_hash->regex_lock);
4651         free_ftrace_hash(hash);
4652
4653         release_probe(probe);
4654
4655         return ret;
4656
4657  err_unlock_ftrace:
4658         mutex_unlock(&ftrace_lock);
4659         return ret;
4660 }
4661
4662 void clear_ftrace_function_probes(struct trace_array *tr)
4663 {
4664         struct ftrace_func_probe *probe, *n;
4665
4666         list_for_each_entry_safe(probe, n, &tr->func_probes, list)
4667                 unregister_ftrace_function_probe_func(NULL, tr, probe->probe_ops);
4668 }
4669
4670 static LIST_HEAD(ftrace_commands);
4671 static DEFINE_MUTEX(ftrace_cmd_mutex);
4672
4673 /*
4674  * Currently we only register ftrace commands from __init, so mark this
4675  * __init too.
4676  */
4677 __init int register_ftrace_command(struct ftrace_func_command *cmd)
4678 {
4679         struct ftrace_func_command *p;
4680         int ret = 0;
4681
4682         mutex_lock(&ftrace_cmd_mutex);
4683         list_for_each_entry(p, &ftrace_commands, list) {
4684                 if (strcmp(cmd->name, p->name) == 0) {
4685                         ret = -EBUSY;
4686                         goto out_unlock;
4687                 }
4688         }
4689         list_add(&cmd->list, &ftrace_commands);
4690  out_unlock:
4691         mutex_unlock(&ftrace_cmd_mutex);
4692
4693         return ret;
4694 }
4695
4696 /*
4697  * Currently we only unregister ftrace commands from __init, so mark
4698  * this __init too.
4699  */
4700 __init int unregister_ftrace_command(struct ftrace_func_command *cmd)
4701 {
4702         struct ftrace_func_command *p, *n;
4703         int ret = -ENODEV;
4704
4705         mutex_lock(&ftrace_cmd_mutex);
4706         list_for_each_entry_safe(p, n, &ftrace_commands, list) {
4707                 if (strcmp(cmd->name, p->name) == 0) {
4708                         ret = 0;
4709                         list_del_init(&p->list);
4710                         goto out_unlock;
4711                 }
4712         }
4713  out_unlock:
4714         mutex_unlock(&ftrace_cmd_mutex);
4715
4716         return ret;
4717 }
4718
4719 static int ftrace_process_regex(struct ftrace_iterator *iter,
4720                                 char *buff, int len, int enable)
4721 {
4722         struct ftrace_hash *hash = iter->hash;
4723         struct trace_array *tr = iter->ops->private;
4724         char *func, *command, *next = buff;
4725         struct ftrace_func_command *p;
4726         int ret = -EINVAL;
4727
4728         func = strsep(&next, ":");
4729
4730         if (!next) {
4731                 ret = ftrace_match_records(hash, func, len);
4732                 if (!ret)
4733                         ret = -EINVAL;
4734                 if (ret < 0)
4735                         return ret;
4736                 return 0;
4737         }
4738
4739         /* command found */
4740
4741         command = strsep(&next, ":");
4742
4743         mutex_lock(&ftrace_cmd_mutex);
4744         list_for_each_entry(p, &ftrace_commands, list) {
4745                 if (strcmp(p->name, command) == 0) {
4746                         ret = p->func(tr, hash, func, command, next, enable);
4747                         goto out_unlock;
4748                 }
4749         }
4750  out_unlock:
4751         mutex_unlock(&ftrace_cmd_mutex);
4752
4753         return ret;
4754 }
4755
4756 static ssize_t
4757 ftrace_regex_write(struct file *file, const char __user *ubuf,
4758                    size_t cnt, loff_t *ppos, int enable)
4759 {
4760         struct ftrace_iterator *iter;
4761         struct trace_parser *parser;
4762         ssize_t ret, read;
4763
4764         if (!cnt)
4765                 return 0;
4766
4767         if (file->f_mode & FMODE_READ) {
4768                 struct seq_file *m = file->private_data;
4769                 iter = m->private;
4770         } else
4771                 iter = file->private_data;
4772
4773         if (unlikely(ftrace_disabled))
4774                 return -ENODEV;
4775
4776         /* iter->hash is a local copy, so we don't need regex_lock */
4777
4778         parser = &iter->parser;
4779         read = trace_get_user(parser, ubuf, cnt, ppos);
4780
4781         if (read >= 0 && trace_parser_loaded(parser) &&
4782             !trace_parser_cont(parser)) {
4783                 ret = ftrace_process_regex(iter, parser->buffer,
4784                                            parser->idx, enable);
4785                 trace_parser_clear(parser);
4786                 if (ret < 0)
4787                         goto out;
4788         }
4789
4790         ret = read;
4791  out:
4792         return ret;
4793 }
4794
4795 ssize_t
4796 ftrace_filter_write(struct file *file, const char __user *ubuf,
4797                     size_t cnt, loff_t *ppos)
4798 {
4799         return ftrace_regex_write(file, ubuf, cnt, ppos, 1);
4800 }
4801
4802 ssize_t
4803 ftrace_notrace_write(struct file *file, const char __user *ubuf,
4804                      size_t cnt, loff_t *ppos)
4805 {
4806         return ftrace_regex_write(file, ubuf, cnt, ppos, 0);
4807 }
4808
4809 static int
4810 ftrace_match_addr(struct ftrace_hash *hash, unsigned long ip, int remove)
4811 {
4812         struct ftrace_func_entry *entry;
4813
4814         if (!ftrace_location(ip))
4815                 return -EINVAL;
4816
4817         if (remove) {
4818                 entry = ftrace_lookup_ip(hash, ip);
4819                 if (!entry)
4820                         return -ENOENT;
4821                 free_hash_entry(hash, entry);
4822                 return 0;
4823         }
4824
4825         return add_hash_entry(hash, ip);
4826 }
4827
4828 static int
4829 ftrace_set_hash(struct ftrace_ops *ops, unsigned char *buf, int len,
4830                 unsigned long ip, int remove, int reset, int enable)
4831 {
4832         struct ftrace_hash **orig_hash;
4833         struct ftrace_hash *hash;
4834         int ret;
4835
4836         if (unlikely(ftrace_disabled))
4837                 return -ENODEV;
4838
4839         mutex_lock(&ops->func_hash->regex_lock);
4840
4841         if (enable)
4842                 orig_hash = &ops->func_hash->filter_hash;
4843         else
4844                 orig_hash = &ops->func_hash->notrace_hash;
4845
4846         if (reset)
4847                 hash = alloc_ftrace_hash(FTRACE_HASH_DEFAULT_BITS);
4848         else
4849                 hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, *orig_hash);
4850
4851         if (!hash) {
4852                 ret = -ENOMEM;
4853                 goto out_regex_unlock;
4854         }
4855
4856         if (buf && !ftrace_match_records(hash, buf, len)) {
4857                 ret = -EINVAL;
4858                 goto out_regex_unlock;
4859         }
4860         if (ip) {
4861                 ret = ftrace_match_addr(hash, ip, remove);
4862                 if (ret < 0)
4863                         goto out_regex_unlock;
4864         }
4865
4866         mutex_lock(&ftrace_lock);
4867         ret = ftrace_hash_move_and_update_ops(ops, orig_hash, hash, enable);
4868         mutex_unlock(&ftrace_lock);
4869
4870  out_regex_unlock:
4871         mutex_unlock(&ops->func_hash->regex_lock);
4872
4873         free_ftrace_hash(hash);
4874         return ret;
4875 }
4876
4877 static int
4878 ftrace_set_addr(struct ftrace_ops *ops, unsigned long ip, int remove,
4879                 int reset, int enable)
4880 {
4881         return ftrace_set_hash(ops, 0, 0, ip, remove, reset, enable);
4882 }
4883
4884 /**
4885  * ftrace_set_filter_ip - set a function to filter on in ftrace by address
4886  * @ops - the ops to set the filter with
4887  * @ip - the address to add to or remove from the filter.
4888  * @remove - non zero to remove the ip from the filter
4889  * @reset - non zero to reset all filters before applying this filter.
4890  *
4891  * Filters denote which functions should be enabled when tracing is enabled
4892  * If @ip is NULL, it failes to update filter.
4893  */
4894 int ftrace_set_filter_ip(struct ftrace_ops *ops, unsigned long ip,
4895                          int remove, int reset)
4896 {
4897         ftrace_ops_init(ops);
4898         return ftrace_set_addr(ops, ip, remove, reset, 1);
4899 }
4900 EXPORT_SYMBOL_GPL(ftrace_set_filter_ip);
4901
4902 /**
4903  * ftrace_ops_set_global_filter - setup ops to use global filters
4904  * @ops - the ops which will use the global filters
4905  *
4906  * ftrace users who need global function trace filtering should call this.
4907  * It can set the global filter only if ops were not initialized before.
4908  */
4909 void ftrace_ops_set_global_filter(struct ftrace_ops *ops)
4910 {
4911         if (ops->flags & FTRACE_OPS_FL_INITIALIZED)
4912                 return;
4913
4914         ftrace_ops_init(ops);
4915         ops->func_hash = &global_ops.local_hash;
4916 }
4917 EXPORT_SYMBOL_GPL(ftrace_ops_set_global_filter);
4918
4919 static int
4920 ftrace_set_regex(struct ftrace_ops *ops, unsigned char *buf, int len,
4921                  int reset, int enable)
4922 {
4923         return ftrace_set_hash(ops, buf, len, 0, 0, reset, enable);
4924 }
4925
4926 /**
4927  * ftrace_set_filter - set a function to filter on in ftrace
4928  * @ops - the ops to set the filter with
4929  * @buf - the string that holds the function filter text.
4930  * @len - the length of the string.
4931  * @reset - non zero to reset all filters before applying this filter.
4932  *
4933  * Filters denote which functions should be enabled when tracing is enabled.
4934  * If @buf is NULL and reset is set, all functions will be enabled for tracing.
4935  */
4936 int ftrace_set_filter(struct ftrace_ops *ops, unsigned char *buf,
4937                        int len, int reset)
4938 {
4939         ftrace_ops_init(ops);
4940         return ftrace_set_regex(ops, buf, len, reset, 1);
4941 }
4942 EXPORT_SYMBOL_GPL(ftrace_set_filter);
4943
4944 /**
4945  * ftrace_set_notrace - set a function to not trace in ftrace
4946  * @ops - the ops to set the notrace filter with
4947  * @buf - the string that holds the function notrace text.
4948  * @len - the length of the string.
4949  * @reset - non zero to reset all filters before applying this filter.
4950  *
4951  * Notrace Filters denote which functions should not be enabled when tracing
4952  * is enabled. If @buf is NULL and reset is set, all functions will be enabled
4953  * for tracing.
4954  */
4955 int ftrace_set_notrace(struct ftrace_ops *ops, unsigned char *buf,
4956                         int len, int reset)
4957 {
4958         ftrace_ops_init(ops);
4959         return ftrace_set_regex(ops, buf, len, reset, 0);
4960 }
4961 EXPORT_SYMBOL_GPL(ftrace_set_notrace);
4962 /**
4963  * ftrace_set_global_filter - set a function to filter on with global tracers
4964  * @buf - the string that holds the function filter text.
4965  * @len - the length of the string.
4966  * @reset - non zero to reset all filters before applying this filter.
4967  *
4968  * Filters denote which functions should be enabled when tracing is enabled.
4969  * If @buf is NULL and reset is set, all functions will be enabled for tracing.
4970  */
4971 void ftrace_set_global_filter(unsigned char *buf, int len, int reset)
4972 {
4973         ftrace_set_regex(&global_ops, buf, len, reset, 1);
4974 }
4975 EXPORT_SYMBOL_GPL(ftrace_set_global_filter);
4976
4977 /**
4978  * ftrace_set_global_notrace - set a function to not trace with global tracers
4979  * @buf - the string that holds the function notrace text.
4980  * @len - the length of the string.
4981  * @reset - non zero to reset all filters before applying this filter.
4982  *
4983  * Notrace Filters denote which functions should not be enabled when tracing
4984  * is enabled. If @buf is NULL and reset is set, all functions will be enabled
4985  * for tracing.
4986  */
4987 void ftrace_set_global_notrace(unsigned char *buf, int len, int reset)
4988 {
4989         ftrace_set_regex(&global_ops, buf, len, reset, 0);
4990 }
4991 EXPORT_SYMBOL_GPL(ftrace_set_global_notrace);
4992
4993 /*
4994  * command line interface to allow users to set filters on boot up.
4995  */
4996 #define FTRACE_FILTER_SIZE              COMMAND_LINE_SIZE
4997 static char ftrace_notrace_buf[FTRACE_FILTER_SIZE] __initdata;
4998 static char ftrace_filter_buf[FTRACE_FILTER_SIZE] __initdata;
4999
5000 /* Used by function selftest to not test if filter is set */
5001 bool ftrace_filter_param __initdata;
5002
5003 static int __init set_ftrace_notrace(char *str)
5004 {
5005         ftrace_filter_param = true;
5006         strlcpy(ftrace_notrace_buf, str, FTRACE_FILTER_SIZE);
5007         return 1;
5008 }
5009 __setup("ftrace_notrace=", set_ftrace_notrace);
5010
5011 static int __init set_ftrace_filter(char *str)
5012 {
5013         ftrace_filter_param = true;
5014         strlcpy(ftrace_filter_buf, str, FTRACE_FILTER_SIZE);
5015         return 1;
5016 }
5017 __setup("ftrace_filter=", set_ftrace_filter);
5018
5019 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
5020 static char ftrace_graph_buf[FTRACE_FILTER_SIZE] __initdata;
5021 static char ftrace_graph_notrace_buf[FTRACE_FILTER_SIZE] __initdata;
5022 static int ftrace_graph_set_hash(struct ftrace_hash *hash, char *buffer);
5023
5024 static int __init set_graph_function(char *str)
5025 {
5026         strlcpy(ftrace_graph_buf, str, FTRACE_FILTER_SIZE);
5027         return 1;
5028 }
5029 __setup("ftrace_graph_filter=", set_graph_function);
5030
5031 static int __init set_graph_notrace_function(char *str)
5032 {
5033         strlcpy(ftrace_graph_notrace_buf, str, FTRACE_FILTER_SIZE);
5034         return 1;
5035 }
5036 __setup("ftrace_graph_notrace=", set_graph_notrace_function);
5037
5038 static int __init set_graph_max_depth_function(char *str)
5039 {
5040         if (!str)
5041                 return 0;
5042         fgraph_max_depth = simple_strtoul(str, NULL, 0);
5043         return 1;
5044 }
5045 __setup("ftrace_graph_max_depth=", set_graph_max_depth_function);
5046
5047 static void __init set_ftrace_early_graph(char *buf, int enable)
5048 {
5049         int ret;
5050         char *func;
5051         struct ftrace_hash *hash;
5052
5053         hash = alloc_ftrace_hash(FTRACE_HASH_DEFAULT_BITS);
5054         if (WARN_ON(!hash))
5055                 return;
5056
5057         while (buf) {
5058                 func = strsep(&buf, ",");
5059                 /* we allow only one expression at a time */
5060                 ret = ftrace_graph_set_hash(hash, func);
5061                 if (ret)
5062                         printk(KERN_DEBUG "ftrace: function %s not "
5063                                           "traceable\n", func);
5064         }
5065
5066         if (enable)
5067                 ftrace_graph_hash = hash;
5068         else
5069                 ftrace_graph_notrace_hash = hash;
5070 }
5071 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
5072
5073 void __init
5074 ftrace_set_early_filter(struct ftrace_ops *ops, char *buf, int enable)
5075 {
5076         char *func;
5077
5078         ftrace_ops_init(ops);
5079
5080         while (buf) {
5081                 func = strsep(&buf, ",");
5082                 ftrace_set_regex(ops, func, strlen(func), 0, enable);
5083         }
5084 }
5085
5086 static void __init set_ftrace_early_filters(void)
5087 {
5088         if (ftrace_filter_buf[0])
5089                 ftrace_set_early_filter(&global_ops, ftrace_filter_buf, 1);
5090         if (ftrace_notrace_buf[0])
5091                 ftrace_set_early_filter(&global_ops, ftrace_notrace_buf, 0);
5092 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
5093         if (ftrace_graph_buf[0])
5094                 set_ftrace_early_graph(ftrace_graph_buf, 1);
5095         if (ftrace_graph_notrace_buf[0])
5096                 set_ftrace_early_graph(ftrace_graph_notrace_buf, 0);
5097 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
5098 }
5099
5100 int ftrace_regex_release(struct inode *inode, struct file *file)
5101 {
5102         struct seq_file *m = (struct seq_file *)file->private_data;
5103         struct ftrace_iterator *iter;
5104         struct ftrace_hash **orig_hash;
5105         struct trace_parser *parser;
5106         int filter_hash;
5107         int ret;
5108
5109         if (file->f_mode & FMODE_READ) {
5110                 iter = m->private;
5111                 seq_release(inode, file);
5112         } else
5113                 iter = file->private_data;
5114
5115         parser = &iter->parser;
5116         if (trace_parser_loaded(parser)) {
5117                 int enable = !(iter->flags & FTRACE_ITER_NOTRACE);
5118
5119                 parser->buffer[parser->idx] = 0;
5120                 ftrace_process_regex(iter, parser->buffer,
5121                                      parser->idx, enable);
5122         }
5123
5124         trace_parser_put(parser);
5125
5126         mutex_lock(&iter->ops->func_hash->regex_lock);
5127
5128         if (file->f_mode & FMODE_WRITE) {
5129                 filter_hash = !!(iter->flags & FTRACE_ITER_FILTER);
5130
5131                 if (filter_hash) {
5132                         orig_hash = &iter->ops->func_hash->filter_hash;
5133                         if (iter->tr) {
5134                                 if (list_empty(&iter->tr->mod_trace))
5135                                         iter->hash->flags &= ~FTRACE_HASH_FL_MOD;
5136                                 else
5137                                         iter->hash->flags |= FTRACE_HASH_FL_MOD;
5138                         }
5139                 } else
5140                         orig_hash = &iter->ops->func_hash->notrace_hash;
5141
5142                 mutex_lock(&ftrace_lock);
5143                 ret = ftrace_hash_move_and_update_ops(iter->ops, orig_hash,
5144                                                       iter->hash, filter_hash);
5145                 mutex_unlock(&ftrace_lock);
5146         } else {
5147                 /* For read only, the hash is the ops hash */
5148                 iter->hash = NULL;
5149         }
5150
5151         mutex_unlock(&iter->ops->func_hash->regex_lock);
5152         free_ftrace_hash(iter->hash);
5153         if (iter->tr)
5154                 trace_array_put(iter->tr);
5155         kfree(iter);
5156
5157         return 0;
5158 }
5159
5160 static const struct file_operations ftrace_avail_fops = {
5161         .open = ftrace_avail_open,
5162         .read = seq_read,
5163         .llseek = seq_lseek,
5164         .release = seq_release_private,
5165 };
5166
5167 static const struct file_operations ftrace_enabled_fops = {
5168         .open = ftrace_enabled_open,
5169         .read = seq_read,
5170         .llseek = seq_lseek,
5171         .release = seq_release_private,
5172 };
5173
5174 static const struct file_operations ftrace_filter_fops = {
5175         .open = ftrace_filter_open,
5176         .read = seq_read,
5177         .write = ftrace_filter_write,
5178         .llseek = tracing_lseek,
5179         .release = ftrace_regex_release,
5180 };
5181
5182 static const struct file_operations ftrace_notrace_fops = {
5183         .open = ftrace_notrace_open,
5184         .read = seq_read,
5185         .write = ftrace_notrace_write,
5186         .llseek = tracing_lseek,
5187         .release = ftrace_regex_release,
5188 };
5189
5190 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
5191
5192 static DEFINE_MUTEX(graph_lock);
5193
5194 struct ftrace_hash __rcu *ftrace_graph_hash = EMPTY_HASH;
5195 struct ftrace_hash __rcu *ftrace_graph_notrace_hash = EMPTY_HASH;
5196
5197 enum graph_filter_type {
5198         GRAPH_FILTER_NOTRACE    = 0,
5199         GRAPH_FILTER_FUNCTION,
5200 };
5201
5202 #define FTRACE_GRAPH_EMPTY      ((void *)1)
5203
5204 struct ftrace_graph_data {
5205         struct ftrace_hash              *hash;
5206         struct ftrace_func_entry        *entry;
5207         int                             idx;   /* for hash table iteration */
5208         enum graph_filter_type          type;
5209         struct ftrace_hash              *new_hash;
5210         const struct seq_operations     *seq_ops;
5211         struct trace_parser             parser;
5212 };
5213
5214 static void *
5215 __g_next(struct seq_file *m, loff_t *pos)
5216 {
5217         struct ftrace_graph_data *fgd = m->private;
5218         struct ftrace_func_entry *entry = fgd->entry;
5219         struct hlist_head *head;
5220         int i, idx = fgd->idx;
5221
5222         if (*pos >= fgd->hash->count)
5223                 return NULL;
5224
5225         if (entry) {
5226                 hlist_for_each_entry_continue(entry, hlist) {
5227                         fgd->entry = entry;
5228                         return entry;
5229                 }
5230
5231                 idx++;
5232         }
5233
5234         for (i = idx; i < 1 << fgd->hash->size_bits; i++) {
5235                 head = &fgd->hash->buckets[i];
5236                 hlist_for_each_entry(entry, head, hlist) {
5237                         fgd->entry = entry;
5238                         fgd->idx = i;
5239                         return entry;
5240                 }
5241         }
5242         return NULL;
5243 }
5244
5245 static void *
5246 g_next(struct seq_file *m, void *v, loff_t *pos)
5247 {
5248         (*pos)++;
5249         return __g_next(m, pos);
5250 }
5251
5252 static void *g_start(struct seq_file *m, loff_t *pos)
5253 {
5254         struct ftrace_graph_data *fgd = m->private;
5255
5256         mutex_lock(&graph_lock);
5257
5258         if (fgd->type == GRAPH_FILTER_FUNCTION)
5259                 fgd->hash = rcu_dereference_protected(ftrace_graph_hash,
5260                                         lockdep_is_held(&graph_lock));
5261         else
5262                 fgd->hash = rcu_dereference_protected(ftrace_graph_notrace_hash,
5263                                         lockdep_is_held(&graph_lock));
5264
5265         /* Nothing, tell g_show to print all functions are enabled */
5266         if (ftrace_hash_empty(fgd->hash) && !*pos)
5267                 return FTRACE_GRAPH_EMPTY;
5268
5269         fgd->idx = 0;
5270         fgd->entry = NULL;
5271         return __g_next(m, pos);
5272 }
5273
5274 static void g_stop(struct seq_file *m, void *p)
5275 {
5276         mutex_unlock(&graph_lock);
5277 }
5278
5279 static int g_show(struct seq_file *m, void *v)
5280 {
5281         struct ftrace_func_entry *entry = v;
5282
5283         if (!entry)
5284                 return 0;
5285
5286         if (entry == FTRACE_GRAPH_EMPTY) {
5287                 struct ftrace_graph_data *fgd = m->private;
5288
5289                 if (fgd->type == GRAPH_FILTER_FUNCTION)
5290                         seq_puts(m, "#### all functions enabled ####\n");
5291                 else
5292                         seq_puts(m, "#### no functions disabled ####\n");
5293                 return 0;
5294         }
5295
5296         seq_printf(m, "%ps\n", (void *)entry->ip);
5297
5298         return 0;
5299 }
5300
5301 static const struct seq_operations ftrace_graph_seq_ops = {
5302         .start = g_start,
5303         .next = g_next,
5304         .stop = g_stop,
5305         .show = g_show,
5306 };
5307
5308 static int
5309 __ftrace_graph_open(struct inode *inode, struct file *file,
5310                     struct ftrace_graph_data *fgd)
5311 {
5312         int ret = 0;
5313         struct ftrace_hash *new_hash = NULL;
5314
5315         if (file->f_mode & FMODE_WRITE) {
5316                 const int size_bits = FTRACE_HASH_DEFAULT_BITS;
5317
5318                 if (trace_parser_get_init(&fgd->parser, FTRACE_BUFF_MAX))
5319                         return -ENOMEM;
5320
5321                 if (file->f_flags & O_TRUNC)
5322                         new_hash = alloc_ftrace_hash(size_bits);
5323                 else
5324                         new_hash = alloc_and_copy_ftrace_hash(size_bits,
5325                                                               fgd->hash);
5326                 if (!new_hash) {
5327                         ret = -ENOMEM;
5328                         goto out;
5329                 }
5330         }
5331
5332         if (file->f_mode & FMODE_READ) {
5333                 ret = seq_open(file, &ftrace_graph_seq_ops);
5334                 if (!ret) {
5335                         struct seq_file *m = file->private_data;
5336                         m->private = fgd;
5337                 } else {
5338                         /* Failed */
5339                         free_ftrace_hash(new_hash);
5340                         new_hash = NULL;
5341                 }
5342         } else
5343                 file->private_data = fgd;
5344
5345 out:
5346         if (ret < 0 && file->f_mode & FMODE_WRITE)
5347                 trace_parser_put(&fgd->parser);
5348
5349         fgd->new_hash = new_hash;
5350
5351         /*
5352          * All uses of fgd->hash must be taken with the graph_lock
5353          * held. The graph_lock is going to be released, so force
5354          * fgd->hash to be reinitialized when it is taken again.
5355          */
5356         fgd->hash = NULL;
5357
5358         return ret;
5359 }
5360
5361 static int
5362 ftrace_graph_open(struct inode *inode, struct file *file)
5363 {
5364         struct ftrace_graph_data *fgd;
5365         int ret;
5366
5367         if (unlikely(ftrace_disabled))
5368                 return -ENODEV;
5369
5370         fgd = kmalloc(sizeof(*fgd), GFP_KERNEL);
5371         if (fgd == NULL)
5372                 return -ENOMEM;
5373
5374         mutex_lock(&graph_lock);
5375
5376         fgd->hash = rcu_dereference_protected(ftrace_graph_hash,
5377                                         lockdep_is_held(&graph_lock));
5378         fgd->type = GRAPH_FILTER_FUNCTION;
5379         fgd->seq_ops = &ftrace_graph_seq_ops;
5380
5381         ret = __ftrace_graph_open(inode, file, fgd);
5382         if (ret < 0)
5383                 kfree(fgd);
5384
5385         mutex_unlock(&graph_lock);
5386         return ret;
5387 }
5388
5389 static int
5390 ftrace_graph_notrace_open(struct inode *inode, struct file *file)
5391 {
5392         struct ftrace_graph_data *fgd;
5393         int ret;
5394
5395         if (unlikely(ftrace_disabled))
5396                 return -ENODEV;
5397
5398         fgd = kmalloc(sizeof(*fgd), GFP_KERNEL);
5399         if (fgd == NULL)
5400                 return -ENOMEM;
5401
5402         mutex_lock(&graph_lock);
5403
5404         fgd->hash = rcu_dereference_protected(ftrace_graph_notrace_hash,
5405                                         lockdep_is_held(&graph_lock));
5406         fgd->type = GRAPH_FILTER_NOTRACE;
5407         fgd->seq_ops = &ftrace_graph_seq_ops;
5408
5409         ret = __ftrace_graph_open(inode, file, fgd);
5410         if (ret < 0)
5411                 kfree(fgd);
5412
5413         mutex_unlock(&graph_lock);
5414         return ret;
5415 }
5416
5417 static int
5418 ftrace_graph_release(struct inode *inode, struct file *file)
5419 {
5420         struct ftrace_graph_data *fgd;
5421         struct ftrace_hash *old_hash, *new_hash;
5422         struct trace_parser *parser;
5423         int ret = 0;
5424
5425         if (file->f_mode & FMODE_READ) {
5426                 struct seq_file *m = file->private_data;
5427
5428                 fgd = m->private;
5429                 seq_release(inode, file);
5430         } else {
5431                 fgd = file->private_data;
5432         }
5433
5434
5435         if (file->f_mode & FMODE_WRITE) {
5436
5437                 parser = &fgd->parser;
5438
5439                 if (trace_parser_loaded((parser))) {
5440                         parser->buffer[parser->idx] = 0;
5441                         ret = ftrace_graph_set_hash(fgd->new_hash,
5442                                                     parser->buffer);
5443                 }
5444
5445                 trace_parser_put(parser);
5446
5447                 new_hash = __ftrace_hash_move(fgd->new_hash);
5448                 if (!new_hash) {
5449                         ret = -ENOMEM;
5450                         goto out;
5451                 }
5452
5453                 mutex_lock(&graph_lock);
5454
5455                 if (fgd->type == GRAPH_FILTER_FUNCTION) {
5456                         old_hash = rcu_dereference_protected(ftrace_graph_hash,
5457                                         lockdep_is_held(&graph_lock));
5458                         rcu_assign_pointer(ftrace_graph_hash, new_hash);
5459                 } else {
5460                         old_hash = rcu_dereference_protected(ftrace_graph_notrace_hash,
5461                                         lockdep_is_held(&graph_lock));
5462                         rcu_assign_pointer(ftrace_graph_notrace_hash, new_hash);
5463                 }
5464
5465                 mutex_unlock(&graph_lock);
5466
5467                 /*
5468                  * We need to do a hard force of sched synchronization.
5469                  * This is because we use preempt_disable() to do RCU, but
5470                  * the function tracers can be called where RCU is not watching
5471                  * (like before user_exit()). We can not rely on the RCU
5472                  * infrastructure to do the synchronization, thus we must do it
5473                  * ourselves.
5474                  */
5475                 schedule_on_each_cpu(ftrace_sync);
5476
5477                 free_ftrace_hash(old_hash);
5478         }
5479
5480  out:
5481         free_ftrace_hash(fgd->new_hash);
5482         kfree(fgd);
5483
5484         return ret;
5485 }
5486
5487 static int
5488 ftrace_graph_set_hash(struct ftrace_hash *hash, char *buffer)
5489 {
5490         struct ftrace_glob func_g;
5491         struct dyn_ftrace *rec;
5492         struct ftrace_page *pg;
5493         struct ftrace_func_entry *entry;
5494         int fail = 1;
5495         int not;
5496
5497         /* decode regex */
5498         func_g.type = filter_parse_regex(buffer, strlen(buffer),
5499                                          &func_g.search, &not);
5500
5501         func_g.len = strlen(func_g.search);
5502
5503         mutex_lock(&ftrace_lock);
5504
5505         if (unlikely(ftrace_disabled)) {
5506                 mutex_unlock(&ftrace_lock);
5507                 return -ENODEV;
5508         }
5509
5510         do_for_each_ftrace_rec(pg, rec) {
5511
5512                 if (rec->flags & FTRACE_FL_DISABLED)
5513                         continue;
5514
5515                 if (ftrace_match_record(rec, &func_g, NULL, 0)) {
5516                         entry = ftrace_lookup_ip(hash, rec->ip);
5517
5518                         if (!not) {
5519                                 fail = 0;
5520
5521                                 if (entry)
5522                                         continue;
5523                                 if (add_hash_entry(hash, rec->ip) < 0)
5524                                         goto out;
5525                         } else {
5526                                 if (entry) {
5527                                         free_hash_entry(hash, entry);
5528                                         fail = 0;
5529                                 }
5530                         }
5531                 }
5532         } while_for_each_ftrace_rec();
5533 out:
5534         mutex_unlock(&ftrace_lock);
5535
5536         if (fail)
5537                 return -EINVAL;
5538
5539         return 0;
5540 }
5541
5542 static ssize_t
5543 ftrace_graph_write(struct file *file, const char __user *ubuf,
5544                    size_t cnt, loff_t *ppos)
5545 {
5546         ssize_t read, ret = 0;
5547         struct ftrace_graph_data *fgd = file->private_data;
5548         struct trace_parser *parser;
5549
5550         if (!cnt)
5551                 return 0;
5552
5553         /* Read mode uses seq functions */
5554         if (file->f_mode & FMODE_READ) {
5555                 struct seq_file *m = file->private_data;
5556                 fgd = m->private;
5557         }
5558
5559         parser = &fgd->parser;
5560
5561         read = trace_get_user(parser, ubuf, cnt, ppos);
5562
5563         if (read >= 0 && trace_parser_loaded(parser) &&
5564             !trace_parser_cont(parser)) {
5565
5566                 ret = ftrace_graph_set_hash(fgd->new_hash,
5567                                             parser->buffer);
5568                 trace_parser_clear(parser);
5569         }
5570
5571         if (!ret)
5572                 ret = read;
5573
5574         return ret;
5575 }
5576
5577 static const struct file_operations ftrace_graph_fops = {
5578         .open           = ftrace_graph_open,
5579         .read           = seq_read,
5580         .write          = ftrace_graph_write,
5581         .llseek         = tracing_lseek,
5582         .release        = ftrace_graph_release,
5583 };
5584
5585 static const struct file_operations ftrace_graph_notrace_fops = {
5586         .open           = ftrace_graph_notrace_open,
5587         .read           = seq_read,
5588         .write          = ftrace_graph_write,
5589         .llseek         = tracing_lseek,
5590         .release        = ftrace_graph_release,
5591 };
5592 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
5593
5594 void ftrace_create_filter_files(struct ftrace_ops *ops,
5595                                 struct dentry *parent)
5596 {
5597
5598         trace_create_file("set_ftrace_filter", 0644, parent,
5599                           ops, &ftrace_filter_fops);
5600
5601         trace_create_file("set_ftrace_notrace", 0644, parent,
5602                           ops, &ftrace_notrace_fops);
5603 }
5604
5605 /*
5606  * The name "destroy_filter_files" is really a misnomer. Although
5607  * in the future, it may actualy delete the files, but this is
5608  * really intended to make sure the ops passed in are disabled
5609  * and that when this function returns, the caller is free to
5610  * free the ops.
5611  *
5612  * The "destroy" name is only to match the "create" name that this
5613  * should be paired with.
5614  */
5615 void ftrace_destroy_filter_files(struct ftrace_ops *ops)
5616 {
5617         mutex_lock(&ftrace_lock);
5618         if (ops->flags & FTRACE_OPS_FL_ENABLED)
5619                 ftrace_shutdown(ops, 0);
5620         ops->flags |= FTRACE_OPS_FL_DELETED;
5621         ftrace_free_filter(ops);
5622         mutex_unlock(&ftrace_lock);
5623 }
5624
5625 static __init int ftrace_init_dyn_tracefs(struct dentry *d_tracer)
5626 {
5627
5628         trace_create_file("available_filter_functions", 0444,
5629                         d_tracer, NULL, &ftrace_avail_fops);
5630
5631         trace_create_file("enabled_functions", 0444,
5632                         d_tracer, NULL, &ftrace_enabled_fops);
5633
5634         ftrace_create_filter_files(&global_ops, d_tracer);
5635
5636 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
5637         trace_create_file("set_graph_function", 0444, d_tracer,
5638                                     NULL,
5639                                     &ftrace_graph_fops);
5640         trace_create_file("set_graph_notrace", 0444, d_tracer,
5641                                     NULL,
5642                                     &ftrace_graph_notrace_fops);
5643 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
5644
5645         return 0;
5646 }
5647
5648 static int ftrace_cmp_ips(const void *a, const void *b)
5649 {
5650         const unsigned long *ipa = a;
5651         const unsigned long *ipb = b;
5652
5653         if (*ipa > *ipb)
5654                 return 1;
5655         if (*ipa < *ipb)
5656                 return -1;
5657         return 0;
5658 }
5659
5660 static int ftrace_process_locs(struct module *mod,
5661                                unsigned long *start,
5662                                unsigned long *end)
5663 {
5664         struct ftrace_page *start_pg;
5665         struct ftrace_page *pg;
5666         struct dyn_ftrace *rec;
5667         unsigned long count;
5668         unsigned long *p;
5669         unsigned long addr;
5670         unsigned long flags = 0; /* Shut up gcc */
5671         int ret = -ENOMEM;
5672
5673         count = end - start;
5674
5675         if (!count)
5676                 return 0;
5677
5678         sort(start, count, sizeof(*start),
5679              ftrace_cmp_ips, NULL);
5680
5681         start_pg = ftrace_allocate_pages(count);
5682         if (!start_pg)
5683                 return -ENOMEM;
5684
5685         mutex_lock(&ftrace_lock);
5686
5687         /*
5688          * Core and each module needs their own pages, as
5689          * modules will free them when they are removed.
5690          * Force a new page to be allocated for modules.
5691          */
5692         if (!mod) {
5693                 WARN_ON(ftrace_pages || ftrace_pages_start);
5694                 /* First initialization */
5695                 ftrace_pages = ftrace_pages_start = start_pg;
5696         } else {
5697                 if (!ftrace_pages)
5698                         goto out;
5699
5700                 if (WARN_ON(ftrace_pages->next)) {
5701                         /* Hmm, we have free pages? */
5702                         while (ftrace_pages->next)
5703                                 ftrace_pages = ftrace_pages->next;
5704                 }
5705
5706                 ftrace_pages->next = start_pg;
5707         }
5708
5709         p = start;
5710         pg = start_pg;
5711         while (p < end) {
5712                 addr = ftrace_call_adjust(*p++);
5713                 /*
5714                  * Some architecture linkers will pad between
5715                  * the different mcount_loc sections of different
5716                  * object files to satisfy alignments.
5717                  * Skip any NULL pointers.
5718                  */
5719                 if (!addr)
5720                         continue;
5721
5722                 if (pg->index == pg->size) {
5723                         /* We should have allocated enough */
5724                         if (WARN_ON(!pg->next))
5725                                 break;
5726                         pg = pg->next;
5727                 }
5728
5729                 rec = &pg->records[pg->index++];
5730                 rec->ip = addr;
5731         }
5732
5733         /* We should have used all pages */
5734         WARN_ON(pg->next);
5735
5736         /* Assign the last page to ftrace_pages */
5737         ftrace_pages = pg;
5738
5739         /*
5740          * We only need to disable interrupts on start up
5741          * because we are modifying code that an interrupt
5742          * may execute, and the modification is not atomic.
5743          * But for modules, nothing runs the code we modify
5744          * until we are finished with it, and there's no
5745          * reason to cause large interrupt latencies while we do it.
5746          */
5747         if (!mod)
5748                 local_irq_save(flags);
5749         ftrace_update_code(mod, start_pg);
5750         if (!mod)
5751                 local_irq_restore(flags);
5752         ret = 0;
5753  out:
5754         mutex_unlock(&ftrace_lock);
5755
5756         return ret;
5757 }
5758
5759 #ifdef CONFIG_MODULES
5760
5761 #define next_to_ftrace_page(p) container_of(p, struct ftrace_page, next)
5762
5763 static int referenced_filters(struct dyn_ftrace *rec)
5764 {
5765         struct ftrace_ops *ops;
5766         int cnt = 0;
5767
5768         for (ops = ftrace_ops_list; ops != &ftrace_list_end; ops = ops->next) {
5769                 if (ops_references_rec(ops, rec)) {
5770                         cnt++;
5771                         if (ops->flags & FTRACE_OPS_FL_SAVE_REGS)
5772                                 rec->flags |= FTRACE_FL_REGS;
5773                 }
5774         }
5775
5776         return cnt;
5777 }
5778
5779 static void
5780 clear_mod_from_hash(struct ftrace_page *pg, struct ftrace_hash *hash)
5781 {
5782         struct ftrace_func_entry *entry;
5783         struct dyn_ftrace *rec;
5784         int i;
5785
5786         if (ftrace_hash_empty(hash))
5787                 return;
5788
5789         for (i = 0; i < pg->index; i++) {
5790                 rec = &pg->records[i];
5791                 entry = __ftrace_lookup_ip(hash, rec->ip);
5792                 /*
5793                  * Do not allow this rec to match again.
5794                  * Yeah, it may waste some memory, but will be removed
5795                  * if/when the hash is modified again.
5796                  */
5797                 if (entry)
5798                         entry->ip = 0;
5799         }
5800 }
5801
5802 /* Clear any records from hashs */
5803 static void clear_mod_from_hashes(struct ftrace_page *pg)
5804 {
5805         struct trace_array *tr;
5806
5807         mutex_lock(&trace_types_lock);
5808         list_for_each_entry(tr, &ftrace_trace_arrays, list) {
5809                 if (!tr->ops || !tr->ops->func_hash)
5810                         continue;
5811                 mutex_lock(&tr->ops->func_hash->regex_lock);
5812                 clear_mod_from_hash(pg, tr->ops->func_hash->filter_hash);
5813                 clear_mod_from_hash(pg, tr->ops->func_hash->notrace_hash);
5814                 mutex_unlock(&tr->ops->func_hash->regex_lock);
5815         }
5816         mutex_unlock(&trace_types_lock);
5817 }
5818
5819 void ftrace_release_mod(struct module *mod)
5820 {
5821         struct dyn_ftrace *rec;
5822         struct ftrace_page **last_pg;
5823         struct ftrace_page *tmp_page = NULL;
5824         struct ftrace_page *pg;
5825         int order;
5826
5827         mutex_lock(&ftrace_lock);
5828
5829         if (ftrace_disabled)
5830                 goto out_unlock;
5831
5832         /*
5833          * Each module has its own ftrace_pages, remove
5834          * them from the list.
5835          */
5836         last_pg = &ftrace_pages_start;
5837         for (pg = ftrace_pages_start; pg; pg = *last_pg) {
5838                 rec = &pg->records[0];
5839                 if (within_module_core(rec->ip, mod)) {
5840                         /*
5841                          * As core pages are first, the first
5842                          * page should never be a module page.
5843                          */
5844                         if (WARN_ON(pg == ftrace_pages_start))
5845                                 goto out_unlock;
5846
5847                         /* Check if we are deleting the last page */
5848                         if (pg == ftrace_pages)
5849                                 ftrace_pages = next_to_ftrace_page(last_pg);
5850
5851                         ftrace_update_tot_cnt -= pg->index;
5852                         *last_pg = pg->next;
5853
5854                         pg->next = tmp_page;
5855                         tmp_page = pg;
5856                 } else
5857                         last_pg = &pg->next;
5858         }
5859  out_unlock:
5860         mutex_unlock(&ftrace_lock);
5861
5862         for (pg = tmp_page; pg; pg = tmp_page) {
5863
5864                 /* Needs to be called outside of ftrace_lock */
5865                 clear_mod_from_hashes(pg);
5866
5867                 order = get_count_order(pg->size / ENTRIES_PER_PAGE);
5868                 free_pages((unsigned long)pg->records, order);
5869                 tmp_page = pg->next;
5870                 kfree(pg);
5871         }
5872 }
5873
5874 void ftrace_module_enable(struct module *mod)
5875 {
5876         struct dyn_ftrace *rec;
5877         struct ftrace_page *pg;
5878
5879         mutex_lock(&ftrace_lock);
5880
5881         if (ftrace_disabled)
5882                 goto out_unlock;
5883
5884         /*
5885          * If the tracing is enabled, go ahead and enable the record.
5886          *
5887          * The reason not to enable the record immediatelly is the
5888          * inherent check of ftrace_make_nop/ftrace_make_call for
5889          * correct previous instructions.  Making first the NOP
5890          * conversion puts the module to the correct state, thus
5891          * passing the ftrace_make_call check.
5892          *
5893          * We also delay this to after the module code already set the
5894          * text to read-only, as we now need to set it back to read-write
5895          * so that we can modify the text.
5896          */
5897         if (ftrace_start_up)
5898                 ftrace_arch_code_modify_prepare();
5899
5900         do_for_each_ftrace_rec(pg, rec) {
5901                 int cnt;
5902                 /*
5903                  * do_for_each_ftrace_rec() is a double loop.
5904                  * module text shares the pg. If a record is
5905                  * not part of this module, then skip this pg,
5906                  * which the "break" will do.
5907                  */
5908                 if (!within_module_core(rec->ip, mod))
5909                         break;
5910
5911                 cnt = 0;
5912
5913                 /*
5914                  * When adding a module, we need to check if tracers are
5915                  * currently enabled and if they are, and can trace this record,
5916                  * we need to enable the module functions as well as update the
5917                  * reference counts for those function records.
5918                  */
5919                 if (ftrace_start_up)
5920                         cnt += referenced_filters(rec);
5921
5922                 rec->flags &= ~FTRACE_FL_DISABLED;
5923                 rec->flags += cnt;
5924
5925                 if (ftrace_start_up && cnt) {
5926                         int failed = __ftrace_replace_code(rec, 1);
5927                         if (failed) {
5928                                 ftrace_bug(failed, rec);
5929                                 goto out_loop;
5930                         }
5931                 }
5932
5933         } while_for_each_ftrace_rec();
5934
5935  out_loop:
5936         if (ftrace_start_up)
5937                 ftrace_arch_code_modify_post_process();
5938
5939  out_unlock:
5940         mutex_unlock(&ftrace_lock);
5941
5942         process_cached_mods(mod->name);
5943 }
5944
5945 void ftrace_module_init(struct module *mod)
5946 {
5947         if (ftrace_disabled || !mod->num_ftrace_callsites)
5948                 return;
5949
5950         ftrace_process_locs(mod, mod->ftrace_callsites,
5951                             mod->ftrace_callsites + mod->num_ftrace_callsites);
5952 }
5953 #endif /* CONFIG_MODULES */
5954
5955 void __init ftrace_free_init_mem(void)
5956 {
5957         unsigned long start = (unsigned long)(&__init_begin);
5958         unsigned long end = (unsigned long)(&__init_end);
5959         struct ftrace_page **last_pg = &ftrace_pages_start;
5960         struct ftrace_page *pg;
5961         struct dyn_ftrace *rec;
5962         struct dyn_ftrace key;
5963         int order;
5964
5965         key.ip = start;
5966         key.flags = end;        /* overload flags, as it is unsigned long */
5967
5968         mutex_lock(&ftrace_lock);
5969
5970         for (pg = ftrace_pages_start; pg; last_pg = &pg->next, pg = *last_pg) {
5971                 if (end < pg->records[0].ip ||
5972                     start >= (pg->records[pg->index - 1].ip + MCOUNT_INSN_SIZE))
5973                         continue;
5974  again:
5975                 rec = bsearch(&key, pg->records, pg->index,
5976                               sizeof(struct dyn_ftrace),
5977                               ftrace_cmp_recs);
5978                 if (!rec)
5979                         continue;
5980                 pg->index--;
5981                 ftrace_update_tot_cnt--;
5982                 if (!pg->index) {
5983                         *last_pg = pg->next;
5984                         order = get_count_order(pg->size / ENTRIES_PER_PAGE);
5985                         free_pages((unsigned long)pg->records, order);
5986                         kfree(pg);
5987                         pg = container_of(last_pg, struct ftrace_page, next);
5988                         if (!(*last_pg))
5989                                 ftrace_pages = pg;
5990                         continue;
5991                 }
5992                 memmove(rec, rec + 1,
5993                         (pg->index - (rec - pg->records)) * sizeof(*rec));
5994                 /* More than one function may be in this block */
5995                 goto again;
5996         }
5997         mutex_unlock(&ftrace_lock);
5998 }
5999
6000 void __init ftrace_init(void)
6001 {
6002         extern unsigned long __start_mcount_loc[];
6003         extern unsigned long __stop_mcount_loc[];
6004         unsigned long count, flags;
6005         int ret;
6006
6007         local_irq_save(flags);
6008         ret = ftrace_dyn_arch_init();
6009         local_irq_restore(flags);
6010         if (ret)
6011                 goto failed;
6012
6013         count = __stop_mcount_loc - __start_mcount_loc;
6014         if (!count) {
6015                 pr_info("ftrace: No functions to be traced?\n");
6016                 goto failed;
6017         }
6018
6019         pr_info("ftrace: allocating %ld entries in %ld pages\n",
6020                 count, DIV_ROUND_UP(count, ENTRIES_PER_PAGE));
6021
6022         last_ftrace_enabled = ftrace_enabled = 1;
6023
6024         ret = ftrace_process_locs(NULL,
6025                                   __start_mcount_loc,
6026                                   __stop_mcount_loc);
6027
6028         set_ftrace_early_filters();
6029
6030         return;
6031  failed:
6032         ftrace_disabled = 1;
6033 }
6034
6035 /* Do nothing if arch does not support this */
6036 void __weak arch_ftrace_update_trampoline(struct ftrace_ops *ops)
6037 {
6038 }
6039
6040 static void ftrace_update_trampoline(struct ftrace_ops *ops)
6041 {
6042         arch_ftrace_update_trampoline(ops);
6043 }
6044
6045 void ftrace_init_trace_array(struct trace_array *tr)
6046 {
6047         INIT_LIST_HEAD(&tr->func_probes);
6048         INIT_LIST_HEAD(&tr->mod_trace);
6049         INIT_LIST_HEAD(&tr->mod_notrace);
6050 }
6051 #else
6052
6053 static struct ftrace_ops global_ops = {
6054         .func                   = ftrace_stub,
6055         .flags                  = FTRACE_OPS_FL_RECURSION_SAFE |
6056                                   FTRACE_OPS_FL_INITIALIZED |
6057                                   FTRACE_OPS_FL_PID,
6058 };
6059
6060 static int __init ftrace_nodyn_init(void)
6061 {
6062         ftrace_enabled = 1;
6063         return 0;
6064 }
6065 core_initcall(ftrace_nodyn_init);
6066
6067 static inline int ftrace_init_dyn_tracefs(struct dentry *d_tracer) { return 0; }
6068 static inline void ftrace_startup_enable(int command) { }
6069 static inline void ftrace_startup_all(int command) { }
6070 /* Keep as macros so we do not need to define the commands */
6071 # define ftrace_startup(ops, command)                                   \
6072         ({                                                              \
6073                 int ___ret = __register_ftrace_function(ops);           \
6074                 if (!___ret)                                            \
6075                         (ops)->flags |= FTRACE_OPS_FL_ENABLED;          \
6076                 ___ret;                                                 \
6077         })
6078 # define ftrace_shutdown(ops, command)                                  \
6079         ({                                                              \
6080                 int ___ret = __unregister_ftrace_function(ops);         \
6081                 if (!___ret)                                            \
6082                         (ops)->flags &= ~FTRACE_OPS_FL_ENABLED;         \
6083                 ___ret;                                                 \
6084         })
6085
6086 # define ftrace_startup_sysctl()        do { } while (0)
6087 # define ftrace_shutdown_sysctl()       do { } while (0)
6088
6089 static inline int
6090 ftrace_ops_test(struct ftrace_ops *ops, unsigned long ip, void *regs)
6091 {
6092         return 1;
6093 }
6094
6095 static void ftrace_update_trampoline(struct ftrace_ops *ops)
6096 {
6097 }
6098
6099 #endif /* CONFIG_DYNAMIC_FTRACE */
6100
6101 __init void ftrace_init_global_array_ops(struct trace_array *tr)
6102 {
6103         tr->ops = &global_ops;
6104         tr->ops->private = tr;
6105         ftrace_init_trace_array(tr);
6106 }
6107
6108 void ftrace_init_array_ops(struct trace_array *tr, ftrace_func_t func)
6109 {
6110         /* If we filter on pids, update to use the pid function */
6111         if (tr->flags & TRACE_ARRAY_FL_GLOBAL) {
6112                 if (WARN_ON(tr->ops->func != ftrace_stub))
6113                         printk("ftrace ops had %pS for function\n",
6114                                tr->ops->func);
6115         }
6116         tr->ops->func = func;
6117         tr->ops->private = tr;
6118 }
6119
6120 void ftrace_reset_array_ops(struct trace_array *tr)
6121 {
6122         tr->ops->func = ftrace_stub;
6123 }
6124
6125 static nokprobe_inline void
6126 __ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip,
6127                        struct ftrace_ops *ignored, struct pt_regs *regs)
6128 {
6129         struct ftrace_ops *op;
6130         int bit;
6131
6132         bit = trace_test_and_set_recursion(TRACE_LIST_START);
6133         if (bit < 0)
6134                 return;
6135
6136         /*
6137          * Some of the ops may be dynamically allocated,
6138          * they must be freed after a synchronize_sched().
6139          */
6140         preempt_disable_notrace();
6141
6142         do_for_each_ftrace_op(op, ftrace_ops_list) {
6143                 /*
6144                  * Check the following for each ops before calling their func:
6145                  *  if RCU flag is set, then rcu_is_watching() must be true
6146                  *  if PER_CPU is set, then ftrace_function_local_disable()
6147                  *                          must be false
6148                  *  Otherwise test if the ip matches the ops filter
6149                  *
6150                  * If any of the above fails then the op->func() is not executed.
6151                  */
6152                 if ((!(op->flags & FTRACE_OPS_FL_RCU) || rcu_is_watching()) &&
6153                     (!(op->flags & FTRACE_OPS_FL_PER_CPU) ||
6154                      !ftrace_function_local_disabled(op)) &&
6155                     ftrace_ops_test(op, ip, regs)) {
6156                     
6157                         if (FTRACE_WARN_ON(!op->func)) {
6158                                 pr_warn("op=%p %pS\n", op, op);
6159                                 goto out;
6160                         }
6161                         op->func(ip, parent_ip, op, regs);
6162                 }
6163         } while_for_each_ftrace_op(op);
6164 out:
6165         preempt_enable_notrace();
6166         trace_clear_recursion(bit);
6167 }
6168
6169 /*
6170  * Some archs only support passing ip and parent_ip. Even though
6171  * the list function ignores the op parameter, we do not want any
6172  * C side effects, where a function is called without the caller
6173  * sending a third parameter.
6174  * Archs are to support both the regs and ftrace_ops at the same time.
6175  * If they support ftrace_ops, it is assumed they support regs.
6176  * If call backs want to use regs, they must either check for regs
6177  * being NULL, or CONFIG_DYNAMIC_FTRACE_WITH_REGS.
6178  * Note, CONFIG_DYNAMIC_FTRACE_WITH_REGS expects a full regs to be saved.
6179  * An architecture can pass partial regs with ftrace_ops and still
6180  * set the ARCH_SUPPORTS_FTRACE_OPS.
6181  */
6182 #if ARCH_SUPPORTS_FTRACE_OPS
6183 static void ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip,
6184                                  struct ftrace_ops *op, struct pt_regs *regs)
6185 {
6186         __ftrace_ops_list_func(ip, parent_ip, NULL, regs);
6187 }
6188 NOKPROBE_SYMBOL(ftrace_ops_list_func);
6189 #else
6190 static void ftrace_ops_no_ops(unsigned long ip, unsigned long parent_ip)
6191 {
6192         __ftrace_ops_list_func(ip, parent_ip, NULL, NULL);
6193 }
6194 NOKPROBE_SYMBOL(ftrace_ops_no_ops);
6195 #endif
6196
6197 /*
6198  * If there's only one function registered but it does not support
6199  * recursion, needs RCU protection and/or requires per cpu handling, then
6200  * this function will be called by the mcount trampoline.
6201  */
6202 static void ftrace_ops_assist_func(unsigned long ip, unsigned long parent_ip,
6203                                    struct ftrace_ops *op, struct pt_regs *regs)
6204 {
6205         int bit;
6206
6207         bit = trace_test_and_set_recursion(TRACE_LIST_START);
6208         if (bit < 0)
6209                 return;
6210
6211         preempt_disable_notrace();
6212
6213         if ((!(op->flags & FTRACE_OPS_FL_RCU) || rcu_is_watching()) &&
6214             (!(op->flags & FTRACE_OPS_FL_PER_CPU) ||
6215              !ftrace_function_local_disabled(op))) {
6216                 op->func(ip, parent_ip, op, regs);
6217         }
6218
6219         preempt_enable_notrace();
6220         trace_clear_recursion(bit);
6221 }
6222 NOKPROBE_SYMBOL(ftrace_ops_assist_func);
6223
6224 /**
6225  * ftrace_ops_get_func - get the function a trampoline should call
6226  * @ops: the ops to get the function for
6227  *
6228  * Normally the mcount trampoline will call the ops->func, but there
6229  * are times that it should not. For example, if the ops does not
6230  * have its own recursion protection, then it should call the
6231  * ftrace_ops_assist_func() instead.
6232  *
6233  * Returns the function that the trampoline should call for @ops.
6234  */
6235 ftrace_func_t ftrace_ops_get_func(struct ftrace_ops *ops)
6236 {
6237         /*
6238          * If the function does not handle recursion, needs to be RCU safe,
6239          * or does per cpu logic, then we need to call the assist handler.
6240          */
6241         if (!(ops->flags & FTRACE_OPS_FL_RECURSION_SAFE) ||
6242             ops->flags & (FTRACE_OPS_FL_RCU | FTRACE_OPS_FL_PER_CPU))
6243                 return ftrace_ops_assist_func;
6244
6245         return ops->func;
6246 }
6247
6248 static void
6249 ftrace_filter_pid_sched_switch_probe(void *data, bool preempt,
6250                     struct task_struct *prev, struct task_struct *next)
6251 {
6252         struct trace_array *tr = data;
6253         struct trace_pid_list *pid_list;
6254
6255         pid_list = rcu_dereference_sched(tr->function_pids);
6256
6257         this_cpu_write(tr->trace_buffer.data->ftrace_ignore_pid,
6258                        trace_ignore_this_task(pid_list, next));
6259 }
6260
6261 static void
6262 ftrace_pid_follow_sched_process_fork(void *data,
6263                                      struct task_struct *self,
6264                                      struct task_struct *task)
6265 {
6266         struct trace_pid_list *pid_list;
6267         struct trace_array *tr = data;
6268
6269         pid_list = rcu_dereference_sched(tr->function_pids);
6270         trace_filter_add_remove_task(pid_list, self, task);
6271 }
6272
6273 static void
6274 ftrace_pid_follow_sched_process_exit(void *data, struct task_struct *task)
6275 {
6276         struct trace_pid_list *pid_list;
6277         struct trace_array *tr = data;
6278
6279         pid_list = rcu_dereference_sched(tr->function_pids);
6280         trace_filter_add_remove_task(pid_list, NULL, task);
6281 }
6282
6283 void ftrace_pid_follow_fork(struct trace_array *tr, bool enable)
6284 {
6285         if (enable) {
6286                 register_trace_sched_process_fork(ftrace_pid_follow_sched_process_fork,
6287                                                   tr);
6288                 register_trace_sched_process_free(ftrace_pid_follow_sched_process_exit,
6289                                                   tr);
6290         } else {
6291                 unregister_trace_sched_process_fork(ftrace_pid_follow_sched_process_fork,
6292                                                     tr);
6293                 unregister_trace_sched_process_free(ftrace_pid_follow_sched_process_exit,
6294                                                     tr);
6295         }
6296 }
6297
6298 static void clear_ftrace_pids(struct trace_array *tr)
6299 {
6300         struct trace_pid_list *pid_list;
6301         int cpu;
6302
6303         pid_list = rcu_dereference_protected(tr->function_pids,
6304                                              lockdep_is_held(&ftrace_lock));
6305         if (!pid_list)
6306                 return;
6307
6308         unregister_trace_sched_switch(ftrace_filter_pid_sched_switch_probe, tr);
6309
6310         for_each_possible_cpu(cpu)
6311                 per_cpu_ptr(tr->trace_buffer.data, cpu)->ftrace_ignore_pid = false;
6312
6313         rcu_assign_pointer(tr->function_pids, NULL);
6314
6315         /* Wait till all users are no longer using pid filtering */
6316         synchronize_sched();
6317
6318         trace_free_pid_list(pid_list);
6319 }
6320
6321 void ftrace_clear_pids(struct trace_array *tr)
6322 {
6323         mutex_lock(&ftrace_lock);
6324
6325         clear_ftrace_pids(tr);
6326
6327         mutex_unlock(&ftrace_lock);
6328 }
6329
6330 static void ftrace_pid_reset(struct trace_array *tr)
6331 {
6332         mutex_lock(&ftrace_lock);
6333         clear_ftrace_pids(tr);
6334
6335         ftrace_update_pid_func();
6336         ftrace_startup_all(0);
6337
6338         mutex_unlock(&ftrace_lock);
6339 }
6340
6341 /* Greater than any max PID */
6342 #define FTRACE_NO_PIDS          (void *)(PID_MAX_LIMIT + 1)
6343
6344 static void *fpid_start(struct seq_file *m, loff_t *pos)
6345         __acquires(RCU)
6346 {
6347         struct trace_pid_list *pid_list;
6348         struct trace_array *tr = m->private;
6349
6350         mutex_lock(&ftrace_lock);
6351         rcu_read_lock_sched();
6352
6353         pid_list = rcu_dereference_sched(tr->function_pids);
6354
6355         if (!pid_list)
6356                 return !(*pos) ? FTRACE_NO_PIDS : NULL;
6357
6358         return trace_pid_start(pid_list, pos);
6359 }
6360
6361 static void *fpid_next(struct seq_file *m, void *v, loff_t *pos)
6362 {
6363         struct trace_array *tr = m->private;
6364         struct trace_pid_list *pid_list = rcu_dereference_sched(tr->function_pids);
6365
6366         if (v == FTRACE_NO_PIDS) {
6367                 (*pos)++;
6368                 return NULL;
6369         }
6370         return trace_pid_next(pid_list, v, pos);
6371 }
6372
6373 static void fpid_stop(struct seq_file *m, void *p)
6374         __releases(RCU)
6375 {
6376         rcu_read_unlock_sched();
6377         mutex_unlock(&ftrace_lock);
6378 }
6379
6380 static int fpid_show(struct seq_file *m, void *v)
6381 {
6382         if (v == FTRACE_NO_PIDS) {
6383                 seq_puts(m, "no pid\n");
6384                 return 0;
6385         }
6386
6387         return trace_pid_show(m, v);
6388 }
6389
6390 static const struct seq_operations ftrace_pid_sops = {
6391         .start = fpid_start,
6392         .next = fpid_next,
6393         .stop = fpid_stop,
6394         .show = fpid_show,
6395 };
6396
6397 static int
6398 ftrace_pid_open(struct inode *inode, struct file *file)
6399 {
6400         struct trace_array *tr = inode->i_private;
6401         struct seq_file *m;
6402         int ret = 0;
6403
6404         if (trace_array_get(tr) < 0)
6405                 return -ENODEV;
6406
6407         if ((file->f_mode & FMODE_WRITE) &&
6408             (file->f_flags & O_TRUNC))
6409                 ftrace_pid_reset(tr);
6410
6411         ret = seq_open(file, &ftrace_pid_sops);
6412         if (ret < 0) {
6413                 trace_array_put(tr);
6414         } else {
6415                 m = file->private_data;
6416                 /* copy tr over to seq ops */
6417                 m->private = tr;
6418         }
6419
6420         return ret;
6421 }
6422
6423 static void ignore_task_cpu(void *data)
6424 {
6425         struct trace_array *tr = data;
6426         struct trace_pid_list *pid_list;
6427
6428         /*
6429          * This function is called by on_each_cpu() while the
6430          * event_mutex is held.
6431          */
6432         pid_list = rcu_dereference_protected(tr->function_pids,
6433                                              mutex_is_locked(&ftrace_lock));
6434
6435         this_cpu_write(tr->trace_buffer.data->ftrace_ignore_pid,
6436                        trace_ignore_this_task(pid_list, current));
6437 }
6438
6439 static ssize_t
6440 ftrace_pid_write(struct file *filp, const char __user *ubuf,
6441                    size_t cnt, loff_t *ppos)
6442 {
6443         struct seq_file *m = filp->private_data;
6444         struct trace_array *tr = m->private;
6445         struct trace_pid_list *filtered_pids = NULL;
6446         struct trace_pid_list *pid_list;
6447         ssize_t ret;
6448
6449         if (!cnt)
6450                 return 0;
6451
6452         mutex_lock(&ftrace_lock);
6453
6454         filtered_pids = rcu_dereference_protected(tr->function_pids,
6455                                              lockdep_is_held(&ftrace_lock));
6456
6457         ret = trace_pid_write(filtered_pids, &pid_list, ubuf, cnt);
6458         if (ret < 0)
6459                 goto out;
6460
6461         rcu_assign_pointer(tr->function_pids, pid_list);
6462
6463         if (filtered_pids) {
6464                 synchronize_sched();
6465                 trace_free_pid_list(filtered_pids);
6466         } else if (pid_list) {
6467                 /* Register a probe to set whether to ignore the tracing of a task */
6468                 register_trace_sched_switch(ftrace_filter_pid_sched_switch_probe, tr);
6469         }
6470
6471         /*
6472          * Ignoring of pids is done at task switch. But we have to
6473          * check for those tasks that are currently running.
6474          * Always do this in case a pid was appended or removed.
6475          */
6476         on_each_cpu(ignore_task_cpu, tr, 1);
6477
6478         ftrace_update_pid_func();
6479         ftrace_startup_all(0);
6480  out:
6481         mutex_unlock(&ftrace_lock);
6482
6483         if (ret > 0)
6484                 *ppos += ret;
6485
6486         return ret;
6487 }
6488
6489 static int
6490 ftrace_pid_release(struct inode *inode, struct file *file)
6491 {
6492         struct trace_array *tr = inode->i_private;
6493
6494         trace_array_put(tr);
6495
6496         return seq_release(inode, file);
6497 }
6498
6499 static const struct file_operations ftrace_pid_fops = {
6500         .open           = ftrace_pid_open,
6501         .write          = ftrace_pid_write,
6502         .read           = seq_read,
6503         .llseek         = tracing_lseek,
6504         .release        = ftrace_pid_release,
6505 };
6506
6507 void ftrace_init_tracefs(struct trace_array *tr, struct dentry *d_tracer)
6508 {
6509         trace_create_file("set_ftrace_pid", 0644, d_tracer,
6510                             tr, &ftrace_pid_fops);
6511 }
6512
6513 void __init ftrace_init_tracefs_toplevel(struct trace_array *tr,
6514                                          struct dentry *d_tracer)
6515 {
6516         /* Only the top level directory has the dyn_tracefs and profile */
6517         WARN_ON(!(tr->flags & TRACE_ARRAY_FL_GLOBAL));
6518
6519         ftrace_init_dyn_tracefs(d_tracer);
6520         ftrace_profile_tracefs(d_tracer);
6521 }
6522
6523 /**
6524  * ftrace_kill - kill ftrace
6525  *
6526  * This function should be used by panic code. It stops ftrace
6527  * but in a not so nice way. If you need to simply kill ftrace
6528  * from a non-atomic section, use ftrace_kill.
6529  */
6530 void ftrace_kill(void)
6531 {
6532         ftrace_disabled = 1;
6533         ftrace_enabled = 0;
6534         clear_ftrace_function();
6535 }
6536
6537 /**
6538  * Test if ftrace is dead or not.
6539  */
6540 int ftrace_is_dead(void)
6541 {
6542         return ftrace_disabled;
6543 }
6544
6545 /**
6546  * register_ftrace_function - register a function for profiling
6547  * @ops - ops structure that holds the function for profiling.
6548  *
6549  * Register a function to be called by all functions in the
6550  * kernel.
6551  *
6552  * Note: @ops->func and all the functions it calls must be labeled
6553  *       with "notrace", otherwise it will go into a
6554  *       recursive loop.
6555  */
6556 int register_ftrace_function(struct ftrace_ops *ops)
6557 {
6558         int ret = -1;
6559
6560         ftrace_ops_init(ops);
6561
6562         mutex_lock(&ftrace_lock);
6563
6564         ret = ftrace_startup(ops, 0);
6565
6566         mutex_unlock(&ftrace_lock);
6567
6568         return ret;
6569 }
6570 EXPORT_SYMBOL_GPL(register_ftrace_function);
6571
6572 /**
6573  * unregister_ftrace_function - unregister a function for profiling.
6574  * @ops - ops structure that holds the function to unregister
6575  *
6576  * Unregister a function that was added to be called by ftrace profiling.
6577  */
6578 int unregister_ftrace_function(struct ftrace_ops *ops)
6579 {
6580         int ret;
6581
6582         mutex_lock(&ftrace_lock);
6583         ret = ftrace_shutdown(ops, 0);
6584         mutex_unlock(&ftrace_lock);
6585
6586         return ret;
6587 }
6588 EXPORT_SYMBOL_GPL(unregister_ftrace_function);
6589
6590 int
6591 ftrace_enable_sysctl(struct ctl_table *table, int write,
6592                      void __user *buffer, size_t *lenp,
6593                      loff_t *ppos)
6594 {
6595         int ret = -ENODEV;
6596
6597         mutex_lock(&ftrace_lock);
6598
6599         if (unlikely(ftrace_disabled))
6600                 goto out;
6601
6602         ret = proc_dointvec(table, write, buffer, lenp, ppos);
6603
6604         if (ret || !write || (last_ftrace_enabled == !!ftrace_enabled))
6605                 goto out;
6606
6607         last_ftrace_enabled = !!ftrace_enabled;
6608
6609         if (ftrace_enabled) {
6610
6611                 /* we are starting ftrace again */
6612                 if (rcu_dereference_protected(ftrace_ops_list,
6613                         lockdep_is_held(&ftrace_lock)) != &ftrace_list_end)
6614                         update_ftrace_function();
6615
6616                 ftrace_startup_sysctl();
6617
6618         } else {
6619                 /* stopping ftrace calls (just send to ftrace_stub) */
6620                 ftrace_trace_function = ftrace_stub;
6621
6622                 ftrace_shutdown_sysctl();
6623         }
6624
6625  out:
6626         mutex_unlock(&ftrace_lock);
6627         return ret;
6628 }
6629
6630 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
6631
6632 static struct ftrace_ops graph_ops = {
6633         .func                   = ftrace_stub,
6634         .flags                  = FTRACE_OPS_FL_RECURSION_SAFE |
6635                                    FTRACE_OPS_FL_INITIALIZED |
6636                                    FTRACE_OPS_FL_PID |
6637                                    FTRACE_OPS_FL_STUB,
6638 #ifdef FTRACE_GRAPH_TRAMP_ADDR
6639         .trampoline             = FTRACE_GRAPH_TRAMP_ADDR,
6640         /* trampoline_size is only needed for dynamically allocated tramps */
6641 #endif
6642         ASSIGN_OPS_HASH(graph_ops, &global_ops.local_hash)
6643 };
6644
6645 void ftrace_graph_sleep_time_control(bool enable)
6646 {
6647         fgraph_sleep_time = enable;
6648 }
6649
6650 void ftrace_graph_graph_time_control(bool enable)
6651 {
6652         fgraph_graph_time = enable;
6653 }
6654
6655 int ftrace_graph_entry_stub(struct ftrace_graph_ent *trace)
6656 {
6657         return 0;
6658 }
6659
6660 /* The callbacks that hook a function */
6661 trace_func_graph_ret_t ftrace_graph_return =
6662                         (trace_func_graph_ret_t)ftrace_stub;
6663 trace_func_graph_ent_t ftrace_graph_entry = ftrace_graph_entry_stub;
6664 static trace_func_graph_ent_t __ftrace_graph_entry = ftrace_graph_entry_stub;
6665
6666 /* Try to assign a return stack array on FTRACE_RETSTACK_ALLOC_SIZE tasks. */
6667 static int alloc_retstack_tasklist(struct ftrace_ret_stack **ret_stack_list)
6668 {
6669         int i;
6670         int ret = 0;
6671         int start = 0, end = FTRACE_RETSTACK_ALLOC_SIZE;
6672         struct task_struct *g, *t;
6673
6674         for (i = 0; i < FTRACE_RETSTACK_ALLOC_SIZE; i++) {
6675                 ret_stack_list[i] = kmalloc(FTRACE_RETFUNC_DEPTH
6676                                         * sizeof(struct ftrace_ret_stack),
6677                                         GFP_KERNEL);
6678                 if (!ret_stack_list[i]) {
6679                         start = 0;
6680                         end = i;
6681                         ret = -ENOMEM;
6682                         goto free;
6683                 }
6684         }
6685
6686         read_lock(&tasklist_lock);
6687         do_each_thread(g, t) {
6688                 if (start == end) {
6689                         ret = -EAGAIN;
6690                         goto unlock;
6691                 }
6692
6693                 if (t->ret_stack == NULL) {
6694                         atomic_set(&t->trace_overrun, 0);
6695                         t->curr_ret_stack = -1;
6696                         /* Make sure the tasks see the -1 first: */
6697                         smp_wmb();
6698                         t->ret_stack = ret_stack_list[start++];
6699                 }
6700         } while_each_thread(g, t);
6701
6702 unlock:
6703         read_unlock(&tasklist_lock);
6704 free:
6705         for (i = start; i < end; i++)
6706                 kfree(ret_stack_list[i]);
6707         return ret;
6708 }
6709
6710 static void
6711 ftrace_graph_probe_sched_switch(void *ignore, bool preempt,
6712                         struct task_struct *prev, struct task_struct *next)
6713 {
6714         unsigned long long timestamp;
6715         int index;
6716
6717         /*
6718          * Does the user want to count the time a function was asleep.
6719          * If so, do not update the time stamps.
6720          */
6721         if (fgraph_sleep_time)
6722                 return;
6723
6724         timestamp = trace_clock_local();
6725
6726         prev->ftrace_timestamp = timestamp;
6727
6728         /* only process tasks that we timestamped */
6729         if (!next->ftrace_timestamp)
6730                 return;
6731
6732         /*
6733          * Update all the counters in next to make up for the
6734          * time next was sleeping.
6735          */
6736         timestamp -= next->ftrace_timestamp;
6737
6738         for (index = next->curr_ret_stack; index >= 0; index--)
6739                 next->ret_stack[index].calltime += timestamp;
6740 }
6741
6742 /* Allocate a return stack for each task */
6743 static int start_graph_tracing(void)
6744 {
6745         struct ftrace_ret_stack **ret_stack_list;
6746         int ret, cpu;
6747
6748         ret_stack_list = kmalloc(FTRACE_RETSTACK_ALLOC_SIZE *
6749                                 sizeof(struct ftrace_ret_stack *),
6750                                 GFP_KERNEL);
6751
6752         if (!ret_stack_list)
6753                 return -ENOMEM;
6754
6755         /* The cpu_boot init_task->ret_stack will never be freed */
6756         for_each_online_cpu(cpu) {
6757                 if (!idle_task(cpu)->ret_stack)
6758                         ftrace_graph_init_idle_task(idle_task(cpu), cpu);
6759         }
6760
6761         do {
6762                 ret = alloc_retstack_tasklist(ret_stack_list);
6763         } while (ret == -EAGAIN);
6764
6765         if (!ret) {
6766                 ret = register_trace_sched_switch(ftrace_graph_probe_sched_switch, NULL);
6767                 if (ret)
6768                         pr_info("ftrace_graph: Couldn't activate tracepoint"
6769                                 " probe to kernel_sched_switch\n");
6770         }
6771
6772         kfree(ret_stack_list);
6773         return ret;
6774 }
6775
6776 /*
6777  * Hibernation protection.
6778  * The state of the current task is too much unstable during
6779  * suspend/restore to disk. We want to protect against that.
6780  */
6781 static int
6782 ftrace_suspend_notifier_call(struct notifier_block *bl, unsigned long state,
6783                                                         void *unused)
6784 {
6785         switch (state) {
6786         case PM_HIBERNATION_PREPARE:
6787                 pause_graph_tracing();
6788                 break;
6789
6790         case PM_POST_HIBERNATION:
6791                 unpause_graph_tracing();
6792                 break;
6793         }
6794         return NOTIFY_DONE;
6795 }
6796
6797 static int ftrace_graph_entry_test(struct ftrace_graph_ent *trace)
6798 {
6799         if (!ftrace_ops_test(&global_ops, trace->func, NULL))
6800                 return 0;
6801         return __ftrace_graph_entry(trace);
6802 }
6803
6804 /*
6805  * The function graph tracer should only trace the functions defined
6806  * by set_ftrace_filter and set_ftrace_notrace. If another function
6807  * tracer ops is registered, the graph tracer requires testing the
6808  * function against the global ops, and not just trace any function
6809  * that any ftrace_ops registered.
6810  */
6811 static void update_function_graph_func(void)
6812 {
6813         struct ftrace_ops *op;
6814         bool do_test = false;
6815
6816         /*
6817          * The graph and global ops share the same set of functions
6818          * to test. If any other ops is on the list, then
6819          * the graph tracing needs to test if its the function
6820          * it should call.
6821          */
6822         do_for_each_ftrace_op(op, ftrace_ops_list) {
6823                 if (op != &global_ops && op != &graph_ops &&
6824                     op != &ftrace_list_end) {
6825                         do_test = true;
6826                         /* in double loop, break out with goto */
6827                         goto out;
6828                 }
6829         } while_for_each_ftrace_op(op);
6830  out:
6831         if (do_test)
6832                 ftrace_graph_entry = ftrace_graph_entry_test;
6833         else
6834                 ftrace_graph_entry = __ftrace_graph_entry;
6835 }
6836
6837 static struct notifier_block ftrace_suspend_notifier = {
6838         .notifier_call = ftrace_suspend_notifier_call,
6839 };
6840
6841 int register_ftrace_graph(trace_func_graph_ret_t retfunc,
6842                         trace_func_graph_ent_t entryfunc)
6843 {
6844         int ret = 0;
6845
6846         mutex_lock(&ftrace_lock);
6847
6848         /* we currently allow only one tracer registered at a time */
6849         if (ftrace_graph_active) {
6850                 ret = -EBUSY;
6851                 goto out;
6852         }
6853
6854         register_pm_notifier(&ftrace_suspend_notifier);
6855
6856         ftrace_graph_active++;
6857         ret = start_graph_tracing();
6858         if (ret) {
6859                 ftrace_graph_active--;
6860                 goto out;
6861         }
6862
6863         ftrace_graph_return = retfunc;
6864
6865         /*
6866          * Update the indirect function to the entryfunc, and the
6867          * function that gets called to the entry_test first. Then
6868          * call the update fgraph entry function to determine if
6869          * the entryfunc should be called directly or not.
6870          */
6871         __ftrace_graph_entry = entryfunc;
6872         ftrace_graph_entry = ftrace_graph_entry_test;
6873         update_function_graph_func();
6874
6875         ret = ftrace_startup(&graph_ops, FTRACE_START_FUNC_RET);
6876 out:
6877         mutex_unlock(&ftrace_lock);
6878         return ret;
6879 }
6880
6881 void unregister_ftrace_graph(void)
6882 {
6883         mutex_lock(&ftrace_lock);
6884
6885         if (unlikely(!ftrace_graph_active))
6886                 goto out;
6887
6888         ftrace_graph_active--;
6889         ftrace_graph_return = (trace_func_graph_ret_t)ftrace_stub;
6890         ftrace_graph_entry = ftrace_graph_entry_stub;
6891         __ftrace_graph_entry = ftrace_graph_entry_stub;
6892         ftrace_shutdown(&graph_ops, FTRACE_STOP_FUNC_RET);
6893         unregister_pm_notifier(&ftrace_suspend_notifier);
6894         unregister_trace_sched_switch(ftrace_graph_probe_sched_switch, NULL);
6895
6896  out:
6897         mutex_unlock(&ftrace_lock);
6898 }
6899
6900 static DEFINE_PER_CPU(struct ftrace_ret_stack *, idle_ret_stack);
6901
6902 static void
6903 graph_init_task(struct task_struct *t, struct ftrace_ret_stack *ret_stack)
6904 {
6905         atomic_set(&t->trace_overrun, 0);
6906         t->ftrace_timestamp = 0;
6907         /* make curr_ret_stack visible before we add the ret_stack */
6908         smp_wmb();
6909         t->ret_stack = ret_stack;
6910 }
6911
6912 /*
6913  * Allocate a return stack for the idle task. May be the first
6914  * time through, or it may be done by CPU hotplug online.
6915  */
6916 void ftrace_graph_init_idle_task(struct task_struct *t, int cpu)
6917 {
6918         t->curr_ret_stack = -1;
6919         /*
6920          * The idle task has no parent, it either has its own
6921          * stack or no stack at all.
6922          */
6923         if (t->ret_stack)
6924                 WARN_ON(t->ret_stack != per_cpu(idle_ret_stack, cpu));
6925
6926         if (ftrace_graph_active) {
6927                 struct ftrace_ret_stack *ret_stack;
6928
6929                 ret_stack = per_cpu(idle_ret_stack, cpu);
6930                 if (!ret_stack) {
6931                         ret_stack = kmalloc(FTRACE_RETFUNC_DEPTH
6932                                             * sizeof(struct ftrace_ret_stack),
6933                                             GFP_KERNEL);
6934                         if (!ret_stack)
6935                                 return;
6936                         per_cpu(idle_ret_stack, cpu) = ret_stack;
6937                 }
6938                 graph_init_task(t, ret_stack);
6939         }
6940 }
6941
6942 /* Allocate a return stack for newly created task */
6943 void ftrace_graph_init_task(struct task_struct *t)
6944 {
6945         /* Make sure we do not use the parent ret_stack */
6946         t->ret_stack = NULL;
6947         t->curr_ret_stack = -1;
6948
6949         if (ftrace_graph_active) {
6950                 struct ftrace_ret_stack *ret_stack;
6951
6952                 ret_stack = kmalloc(FTRACE_RETFUNC_DEPTH
6953                                 * sizeof(struct ftrace_ret_stack),
6954                                 GFP_KERNEL);
6955                 if (!ret_stack)
6956                         return;
6957                 graph_init_task(t, ret_stack);
6958         }
6959 }
6960
6961 void ftrace_graph_exit_task(struct task_struct *t)
6962 {
6963         struct ftrace_ret_stack *ret_stack = t->ret_stack;
6964
6965         t->ret_stack = NULL;
6966         /* NULL must become visible to IRQs before we free it: */
6967         barrier();
6968
6969         kfree(ret_stack);
6970 }
6971 #endif