GNU Linux-libre 4.14.302-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 (end < pg->records[0].ip ||
1650                     start >= (pg->records[pg->index - 1].ip + MCOUNT_INSN_SIZE))
1651                         continue;
1652                 rec = bsearch(&key, pg->records, pg->index,
1653                               sizeof(struct dyn_ftrace),
1654                               ftrace_cmp_recs);
1655                 if (rec)
1656                         return rec->ip;
1657         }
1658
1659         return 0;
1660 }
1661
1662 /**
1663  * ftrace_location - return true if the ip giving is a traced location
1664  * @ip: the instruction pointer to check
1665  *
1666  * Returns rec->ip if @ip given is a pointer to a ftrace location.
1667  * That is, the instruction that is either a NOP or call to
1668  * the function tracer. It checks the ftrace internal tables to
1669  * determine if the address belongs or not.
1670  */
1671 unsigned long ftrace_location(unsigned long ip)
1672 {
1673         return ftrace_location_range(ip, ip);
1674 }
1675
1676 /**
1677  * ftrace_text_reserved - return true if range contains an ftrace location
1678  * @start: start of range to search
1679  * @end: end of range to search (inclusive). @end points to the last byte to check.
1680  *
1681  * Returns 1 if @start and @end contains a ftrace location.
1682  * That is, the instruction that is either a NOP or call to
1683  * the function tracer. It checks the ftrace internal tables to
1684  * determine if the address belongs or not.
1685  */
1686 int ftrace_text_reserved(const void *start, const void *end)
1687 {
1688         unsigned long ret;
1689
1690         ret = ftrace_location_range((unsigned long)start,
1691                                     (unsigned long)end);
1692
1693         return (int)!!ret;
1694 }
1695
1696 /* Test if ops registered to this rec needs regs */
1697 static bool test_rec_ops_needs_regs(struct dyn_ftrace *rec)
1698 {
1699         struct ftrace_ops *ops;
1700         bool keep_regs = false;
1701
1702         for (ops = ftrace_ops_list;
1703              ops != &ftrace_list_end; ops = ops->next) {
1704                 /* pass rec in as regs to have non-NULL val */
1705                 if (ftrace_ops_test(ops, rec->ip, rec)) {
1706                         if (ops->flags & FTRACE_OPS_FL_SAVE_REGS) {
1707                                 keep_regs = true;
1708                                 break;
1709                         }
1710                 }
1711         }
1712
1713         return  keep_regs;
1714 }
1715
1716 static struct ftrace_ops *
1717 ftrace_find_tramp_ops_any(struct dyn_ftrace *rec);
1718 static struct ftrace_ops *
1719 ftrace_find_tramp_ops_any_other(struct dyn_ftrace *rec, struct ftrace_ops *op_exclude);
1720 static struct ftrace_ops *
1721 ftrace_find_tramp_ops_next(struct dyn_ftrace *rec, struct ftrace_ops *ops);
1722
1723 static bool __ftrace_hash_rec_update(struct ftrace_ops *ops,
1724                                      int filter_hash,
1725                                      bool inc)
1726 {
1727         struct ftrace_hash *hash;
1728         struct ftrace_hash *other_hash;
1729         struct ftrace_page *pg;
1730         struct dyn_ftrace *rec;
1731         bool update = false;
1732         int count = 0;
1733         int all = false;
1734
1735         /* Only update if the ops has been registered */
1736         if (!(ops->flags & FTRACE_OPS_FL_ENABLED))
1737                 return false;
1738
1739         /*
1740          * In the filter_hash case:
1741          *   If the count is zero, we update all records.
1742          *   Otherwise we just update the items in the hash.
1743          *
1744          * In the notrace_hash case:
1745          *   We enable the update in the hash.
1746          *   As disabling notrace means enabling the tracing,
1747          *   and enabling notrace means disabling, the inc variable
1748          *   gets inversed.
1749          */
1750         if (filter_hash) {
1751                 hash = ops->func_hash->filter_hash;
1752                 other_hash = ops->func_hash->notrace_hash;
1753                 if (ftrace_hash_empty(hash))
1754                         all = true;
1755         } else {
1756                 inc = !inc;
1757                 hash = ops->func_hash->notrace_hash;
1758                 other_hash = ops->func_hash->filter_hash;
1759                 /*
1760                  * If the notrace hash has no items,
1761                  * then there's nothing to do.
1762                  */
1763                 if (ftrace_hash_empty(hash))
1764                         return false;
1765         }
1766
1767         do_for_each_ftrace_rec(pg, rec) {
1768                 int in_other_hash = 0;
1769                 int in_hash = 0;
1770                 int match = 0;
1771
1772                 if (rec->flags & FTRACE_FL_DISABLED)
1773                         continue;
1774
1775                 if (all) {
1776                         /*
1777                          * Only the filter_hash affects all records.
1778                          * Update if the record is not in the notrace hash.
1779                          */
1780                         if (!other_hash || !ftrace_lookup_ip(other_hash, rec->ip))
1781                                 match = 1;
1782                 } else {
1783                         in_hash = !!ftrace_lookup_ip(hash, rec->ip);
1784                         in_other_hash = !!ftrace_lookup_ip(other_hash, rec->ip);
1785
1786                         /*
1787                          * If filter_hash is set, we want to match all functions
1788                          * that are in the hash but not in the other hash.
1789                          *
1790                          * If filter_hash is not set, then we are decrementing.
1791                          * That means we match anything that is in the hash
1792                          * and also in the other_hash. That is, we need to turn
1793                          * off functions in the other hash because they are disabled
1794                          * by this hash.
1795                          */
1796                         if (filter_hash && in_hash && !in_other_hash)
1797                                 match = 1;
1798                         else if (!filter_hash && in_hash &&
1799                                  (in_other_hash || ftrace_hash_empty(other_hash)))
1800                                 match = 1;
1801                 }
1802                 if (!match)
1803                         continue;
1804
1805                 if (inc) {
1806                         rec->flags++;
1807                         if (FTRACE_WARN_ON(ftrace_rec_count(rec) == FTRACE_REF_MAX))
1808                                 return false;
1809
1810                         /*
1811                          * If there's only a single callback registered to a
1812                          * function, and the ops has a trampoline registered
1813                          * for it, then we can call it directly.
1814                          */
1815                         if (ftrace_rec_count(rec) == 1 && ops->trampoline)
1816                                 rec->flags |= FTRACE_FL_TRAMP;
1817                         else
1818                                 /*
1819                                  * If we are adding another function callback
1820                                  * to this function, and the previous had a
1821                                  * custom trampoline in use, then we need to go
1822                                  * back to the default trampoline.
1823                                  */
1824                                 rec->flags &= ~FTRACE_FL_TRAMP;
1825
1826                         /*
1827                          * If any ops wants regs saved for this function
1828                          * then all ops will get saved regs.
1829                          */
1830                         if (ops->flags & FTRACE_OPS_FL_SAVE_REGS)
1831                                 rec->flags |= FTRACE_FL_REGS;
1832                 } else {
1833                         if (FTRACE_WARN_ON(ftrace_rec_count(rec) == 0))
1834                                 return false;
1835                         rec->flags--;
1836
1837                         /*
1838                          * If the rec had REGS enabled and the ops that is
1839                          * being removed had REGS set, then see if there is
1840                          * still any ops for this record that wants regs.
1841                          * If not, we can stop recording them.
1842                          */
1843                         if (ftrace_rec_count(rec) > 0 &&
1844                             rec->flags & FTRACE_FL_REGS &&
1845                             ops->flags & FTRACE_OPS_FL_SAVE_REGS) {
1846                                 if (!test_rec_ops_needs_regs(rec))
1847                                         rec->flags &= ~FTRACE_FL_REGS;
1848                         }
1849
1850                         /*
1851                          * The TRAMP needs to be set only if rec count
1852                          * is decremented to one, and the ops that is
1853                          * left has a trampoline. As TRAMP can only be
1854                          * enabled if there is only a single ops attached
1855                          * to it.
1856                          */
1857                         if (ftrace_rec_count(rec) == 1 &&
1858                             ftrace_find_tramp_ops_any_other(rec, ops))
1859                                 rec->flags |= FTRACE_FL_TRAMP;
1860                         else
1861                                 rec->flags &= ~FTRACE_FL_TRAMP;
1862
1863                         /*
1864                          * flags will be cleared in ftrace_check_record()
1865                          * if rec count is zero.
1866                          */
1867                 }
1868                 count++;
1869
1870                 /* Must match FTRACE_UPDATE_CALLS in ftrace_modify_all_code() */
1871                 update |= ftrace_test_record(rec, 1) != FTRACE_UPDATE_IGNORE;
1872
1873                 /* Shortcut, if we handled all records, we are done. */
1874                 if (!all && count == hash->count)
1875                         return update;
1876         } while_for_each_ftrace_rec();
1877
1878         return update;
1879 }
1880
1881 static bool ftrace_hash_rec_disable(struct ftrace_ops *ops,
1882                                     int filter_hash)
1883 {
1884         return __ftrace_hash_rec_update(ops, filter_hash, 0);
1885 }
1886
1887 static bool ftrace_hash_rec_enable(struct ftrace_ops *ops,
1888                                    int filter_hash)
1889 {
1890         return __ftrace_hash_rec_update(ops, filter_hash, 1);
1891 }
1892
1893 static void ftrace_hash_rec_update_modify(struct ftrace_ops *ops,
1894                                           int filter_hash, int inc)
1895 {
1896         struct ftrace_ops *op;
1897
1898         __ftrace_hash_rec_update(ops, filter_hash, inc);
1899
1900         if (ops->func_hash != &global_ops.local_hash)
1901                 return;
1902
1903         /*
1904          * If the ops shares the global_ops hash, then we need to update
1905          * all ops that are enabled and use this hash.
1906          */
1907         do_for_each_ftrace_op(op, ftrace_ops_list) {
1908                 /* Already done */
1909                 if (op == ops)
1910                         continue;
1911                 if (op->func_hash == &global_ops.local_hash)
1912                         __ftrace_hash_rec_update(op, filter_hash, inc);
1913         } while_for_each_ftrace_op(op);
1914 }
1915
1916 static void ftrace_hash_rec_disable_modify(struct ftrace_ops *ops,
1917                                            int filter_hash)
1918 {
1919         ftrace_hash_rec_update_modify(ops, filter_hash, 0);
1920 }
1921
1922 static void ftrace_hash_rec_enable_modify(struct ftrace_ops *ops,
1923                                           int filter_hash)
1924 {
1925         ftrace_hash_rec_update_modify(ops, filter_hash, 1);
1926 }
1927
1928 /*
1929  * Try to update IPMODIFY flag on each ftrace_rec. Return 0 if it is OK
1930  * or no-needed to update, -EBUSY if it detects a conflict of the flag
1931  * on a ftrace_rec, and -EINVAL if the new_hash tries to trace all recs.
1932  * Note that old_hash and new_hash has below meanings
1933  *  - If the hash is NULL, it hits all recs (if IPMODIFY is set, this is rejected)
1934  *  - If the hash is EMPTY_HASH, it hits nothing
1935  *  - Anything else hits the recs which match the hash entries.
1936  */
1937 static int __ftrace_hash_update_ipmodify(struct ftrace_ops *ops,
1938                                          struct ftrace_hash *old_hash,
1939                                          struct ftrace_hash *new_hash)
1940 {
1941         struct ftrace_page *pg;
1942         struct dyn_ftrace *rec, *end = NULL;
1943         int in_old, in_new;
1944
1945         /* Only update if the ops has been registered */
1946         if (!(ops->flags & FTRACE_OPS_FL_ENABLED))
1947                 return 0;
1948
1949         if (!(ops->flags & FTRACE_OPS_FL_IPMODIFY))
1950                 return 0;
1951
1952         /*
1953          * Since the IPMODIFY is a very address sensitive action, we do not
1954          * allow ftrace_ops to set all functions to new hash.
1955          */
1956         if (!new_hash || !old_hash)
1957                 return -EINVAL;
1958
1959         /* Update rec->flags */
1960         do_for_each_ftrace_rec(pg, rec) {
1961
1962                 if (rec->flags & FTRACE_FL_DISABLED)
1963                         continue;
1964
1965                 /* We need to update only differences of filter_hash */
1966                 in_old = !!ftrace_lookup_ip(old_hash, rec->ip);
1967                 in_new = !!ftrace_lookup_ip(new_hash, rec->ip);
1968                 if (in_old == in_new)
1969                         continue;
1970
1971                 if (in_new) {
1972                         /* New entries must ensure no others are using it */
1973                         if (rec->flags & FTRACE_FL_IPMODIFY)
1974                                 goto rollback;
1975                         rec->flags |= FTRACE_FL_IPMODIFY;
1976                 } else /* Removed entry */
1977                         rec->flags &= ~FTRACE_FL_IPMODIFY;
1978         } while_for_each_ftrace_rec();
1979
1980         return 0;
1981
1982 rollback:
1983         end = rec;
1984
1985         /* Roll back what we did above */
1986         do_for_each_ftrace_rec(pg, rec) {
1987
1988                 if (rec->flags & FTRACE_FL_DISABLED)
1989                         continue;
1990
1991                 if (rec == end)
1992                         goto err_out;
1993
1994                 in_old = !!ftrace_lookup_ip(old_hash, rec->ip);
1995                 in_new = !!ftrace_lookup_ip(new_hash, rec->ip);
1996                 if (in_old == in_new)
1997                         continue;
1998
1999                 if (in_new)
2000                         rec->flags &= ~FTRACE_FL_IPMODIFY;
2001                 else
2002                         rec->flags |= FTRACE_FL_IPMODIFY;
2003         } while_for_each_ftrace_rec();
2004
2005 err_out:
2006         return -EBUSY;
2007 }
2008
2009 static int ftrace_hash_ipmodify_enable(struct ftrace_ops *ops)
2010 {
2011         struct ftrace_hash *hash = ops->func_hash->filter_hash;
2012
2013         if (ftrace_hash_empty(hash))
2014                 hash = NULL;
2015
2016         return __ftrace_hash_update_ipmodify(ops, EMPTY_HASH, hash);
2017 }
2018
2019 /* Disabling always succeeds */
2020 static void ftrace_hash_ipmodify_disable(struct ftrace_ops *ops)
2021 {
2022         struct ftrace_hash *hash = ops->func_hash->filter_hash;
2023
2024         if (ftrace_hash_empty(hash))
2025                 hash = NULL;
2026
2027         __ftrace_hash_update_ipmodify(ops, hash, EMPTY_HASH);
2028 }
2029
2030 static int ftrace_hash_ipmodify_update(struct ftrace_ops *ops,
2031                                        struct ftrace_hash *new_hash)
2032 {
2033         struct ftrace_hash *old_hash = ops->func_hash->filter_hash;
2034
2035         if (ftrace_hash_empty(old_hash))
2036                 old_hash = NULL;
2037
2038         if (ftrace_hash_empty(new_hash))
2039                 new_hash = NULL;
2040
2041         return __ftrace_hash_update_ipmodify(ops, old_hash, new_hash);
2042 }
2043
2044 static void print_ip_ins(const char *fmt, const unsigned char *p)
2045 {
2046         char ins[MCOUNT_INSN_SIZE];
2047         int i;
2048
2049         if (probe_kernel_read(ins, p, MCOUNT_INSN_SIZE)) {
2050                 printk(KERN_CONT "%s[FAULT] %px\n", fmt, p);
2051                 return;
2052         }
2053
2054         printk(KERN_CONT "%s", fmt);
2055
2056         for (i = 0; i < MCOUNT_INSN_SIZE; i++)
2057                 printk(KERN_CONT "%s%02x", i ? ":" : "", ins[i]);
2058 }
2059
2060 enum ftrace_bug_type ftrace_bug_type;
2061 const void *ftrace_expected;
2062
2063 static void print_bug_type(void)
2064 {
2065         switch (ftrace_bug_type) {
2066         case FTRACE_BUG_UNKNOWN:
2067                 break;
2068         case FTRACE_BUG_INIT:
2069                 pr_info("Initializing ftrace call sites\n");
2070                 break;
2071         case FTRACE_BUG_NOP:
2072                 pr_info("Setting ftrace call site to NOP\n");
2073                 break;
2074         case FTRACE_BUG_CALL:
2075                 pr_info("Setting ftrace call site to call ftrace function\n");
2076                 break;
2077         case FTRACE_BUG_UPDATE:
2078                 pr_info("Updating ftrace call site to call a different ftrace function\n");
2079                 break;
2080         }
2081 }
2082
2083 /**
2084  * ftrace_bug - report and shutdown function tracer
2085  * @failed: The failed type (EFAULT, EINVAL, EPERM)
2086  * @rec: The record that failed
2087  *
2088  * The arch code that enables or disables the function tracing
2089  * can call ftrace_bug() when it has detected a problem in
2090  * modifying the code. @failed should be one of either:
2091  * EFAULT - if the problem happens on reading the @ip address
2092  * EINVAL - if what is read at @ip is not what was expected
2093  * EPERM - if the problem happens on writting to the @ip address
2094  */
2095 void ftrace_bug(int failed, struct dyn_ftrace *rec)
2096 {
2097         unsigned long ip = rec ? rec->ip : 0;
2098
2099         switch (failed) {
2100         case -EFAULT:
2101                 FTRACE_WARN_ON_ONCE(1);
2102                 pr_info("ftrace faulted on modifying ");
2103                 print_ip_sym(ip);
2104                 break;
2105         case -EINVAL:
2106                 FTRACE_WARN_ON_ONCE(1);
2107                 pr_info("ftrace failed to modify ");
2108                 print_ip_sym(ip);
2109                 print_ip_ins(" actual:   ", (unsigned char *)ip);
2110                 pr_cont("\n");
2111                 if (ftrace_expected) {
2112                         print_ip_ins(" expected: ", ftrace_expected);
2113                         pr_cont("\n");
2114                 }
2115                 break;
2116         case -EPERM:
2117                 FTRACE_WARN_ON_ONCE(1);
2118                 pr_info("ftrace faulted on writing ");
2119                 print_ip_sym(ip);
2120                 break;
2121         default:
2122                 FTRACE_WARN_ON_ONCE(1);
2123                 pr_info("ftrace faulted on unknown error ");
2124                 print_ip_sym(ip);
2125         }
2126         print_bug_type();
2127         if (rec) {
2128                 struct ftrace_ops *ops = NULL;
2129
2130                 pr_info("ftrace record flags: %lx\n", rec->flags);
2131                 pr_cont(" (%ld)%s", ftrace_rec_count(rec),
2132                         rec->flags & FTRACE_FL_REGS ? " R" : "  ");
2133                 if (rec->flags & FTRACE_FL_TRAMP_EN) {
2134                         ops = ftrace_find_tramp_ops_any(rec);
2135                         if (ops) {
2136                                 do {
2137                                         pr_cont("\ttramp: %pS (%pS)",
2138                                                 (void *)ops->trampoline,
2139                                                 (void *)ops->func);
2140                                         ops = ftrace_find_tramp_ops_next(rec, ops);
2141                                 } while (ops);
2142                         } else
2143                                 pr_cont("\ttramp: ERROR!");
2144
2145                 }
2146                 ip = ftrace_get_addr_curr(rec);
2147                 pr_cont("\n expected tramp: %lx\n", ip);
2148         }
2149 }
2150
2151 static int ftrace_check_record(struct dyn_ftrace *rec, int enable, int update)
2152 {
2153         unsigned long flag = 0UL;
2154
2155         ftrace_bug_type = FTRACE_BUG_UNKNOWN;
2156
2157         if (rec->flags & FTRACE_FL_DISABLED)
2158                 return FTRACE_UPDATE_IGNORE;
2159
2160         /*
2161          * If we are updating calls:
2162          *
2163          *   If the record has a ref count, then we need to enable it
2164          *   because someone is using it.
2165          *
2166          *   Otherwise we make sure its disabled.
2167          *
2168          * If we are disabling calls, then disable all records that
2169          * are enabled.
2170          */
2171         if (enable && ftrace_rec_count(rec))
2172                 flag = FTRACE_FL_ENABLED;
2173
2174         /*
2175          * If enabling and the REGS flag does not match the REGS_EN, or
2176          * the TRAMP flag doesn't match the TRAMP_EN, then do not ignore
2177          * this record. Set flags to fail the compare against ENABLED.
2178          */
2179         if (flag) {
2180                 if (!(rec->flags & FTRACE_FL_REGS) != 
2181                     !(rec->flags & FTRACE_FL_REGS_EN))
2182                         flag |= FTRACE_FL_REGS;
2183
2184                 if (!(rec->flags & FTRACE_FL_TRAMP) != 
2185                     !(rec->flags & FTRACE_FL_TRAMP_EN))
2186                         flag |= FTRACE_FL_TRAMP;
2187         }
2188
2189         /* If the state of this record hasn't changed, then do nothing */
2190         if ((rec->flags & FTRACE_FL_ENABLED) == flag)
2191                 return FTRACE_UPDATE_IGNORE;
2192
2193         if (flag) {
2194                 /* Save off if rec is being enabled (for return value) */
2195                 flag ^= rec->flags & FTRACE_FL_ENABLED;
2196
2197                 if (update) {
2198                         rec->flags |= FTRACE_FL_ENABLED;
2199                         if (flag & FTRACE_FL_REGS) {
2200                                 if (rec->flags & FTRACE_FL_REGS)
2201                                         rec->flags |= FTRACE_FL_REGS_EN;
2202                                 else
2203                                         rec->flags &= ~FTRACE_FL_REGS_EN;
2204                         }
2205                         if (flag & FTRACE_FL_TRAMP) {
2206                                 if (rec->flags & FTRACE_FL_TRAMP)
2207                                         rec->flags |= FTRACE_FL_TRAMP_EN;
2208                                 else
2209                                         rec->flags &= ~FTRACE_FL_TRAMP_EN;
2210                         }
2211                 }
2212
2213                 /*
2214                  * If this record is being updated from a nop, then
2215                  *   return UPDATE_MAKE_CALL.
2216                  * Otherwise,
2217                  *   return UPDATE_MODIFY_CALL to tell the caller to convert
2218                  *   from the save regs, to a non-save regs function or
2219                  *   vice versa, or from a trampoline call.
2220                  */
2221                 if (flag & FTRACE_FL_ENABLED) {
2222                         ftrace_bug_type = FTRACE_BUG_CALL;
2223                         return FTRACE_UPDATE_MAKE_CALL;
2224                 }
2225
2226                 ftrace_bug_type = FTRACE_BUG_UPDATE;
2227                 return FTRACE_UPDATE_MODIFY_CALL;
2228         }
2229
2230         if (update) {
2231                 /* If there's no more users, clear all flags */
2232                 if (!ftrace_rec_count(rec))
2233                         rec->flags = 0;
2234                 else
2235                         /*
2236                          * Just disable the record, but keep the ops TRAMP
2237                          * and REGS states. The _EN flags must be disabled though.
2238                          */
2239                         rec->flags &= ~(FTRACE_FL_ENABLED | FTRACE_FL_TRAMP_EN |
2240                                         FTRACE_FL_REGS_EN);
2241         }
2242
2243         ftrace_bug_type = FTRACE_BUG_NOP;
2244         return FTRACE_UPDATE_MAKE_NOP;
2245 }
2246
2247 /**
2248  * ftrace_update_record, set a record that now is tracing or not
2249  * @rec: the record to update
2250  * @enable: set to 1 if the record is tracing, zero to force disable
2251  *
2252  * The records that represent all functions that can be traced need
2253  * to be updated when tracing has been enabled.
2254  */
2255 int ftrace_update_record(struct dyn_ftrace *rec, int enable)
2256 {
2257         return ftrace_check_record(rec, enable, 1);
2258 }
2259
2260 /**
2261  * ftrace_test_record, check if the record has been enabled or not
2262  * @rec: the record to test
2263  * @enable: set to 1 to check if enabled, 0 if it is disabled
2264  *
2265  * The arch code may need to test if a record is already set to
2266  * tracing to determine how to modify the function code that it
2267  * represents.
2268  */
2269 int ftrace_test_record(struct dyn_ftrace *rec, int enable)
2270 {
2271         return ftrace_check_record(rec, enable, 0);
2272 }
2273
2274 static struct ftrace_ops *
2275 ftrace_find_tramp_ops_any(struct dyn_ftrace *rec)
2276 {
2277         struct ftrace_ops *op;
2278         unsigned long ip = rec->ip;
2279
2280         do_for_each_ftrace_op(op, ftrace_ops_list) {
2281
2282                 if (!op->trampoline)
2283                         continue;
2284
2285                 if (hash_contains_ip(ip, op->func_hash))
2286                         return op;
2287         } while_for_each_ftrace_op(op);
2288
2289         return NULL;
2290 }
2291
2292 static struct ftrace_ops *
2293 ftrace_find_tramp_ops_any_other(struct dyn_ftrace *rec, struct ftrace_ops *op_exclude)
2294 {
2295         struct ftrace_ops *op;
2296         unsigned long ip = rec->ip;
2297
2298         do_for_each_ftrace_op(op, ftrace_ops_list) {
2299
2300                 if (op == op_exclude || !op->trampoline)
2301                         continue;
2302
2303                 if (hash_contains_ip(ip, op->func_hash))
2304                         return op;
2305         } while_for_each_ftrace_op(op);
2306
2307         return NULL;
2308 }
2309
2310 static struct ftrace_ops *
2311 ftrace_find_tramp_ops_next(struct dyn_ftrace *rec,
2312                            struct ftrace_ops *op)
2313 {
2314         unsigned long ip = rec->ip;
2315
2316         while_for_each_ftrace_op(op) {
2317
2318                 if (!op->trampoline)
2319                         continue;
2320
2321                 if (hash_contains_ip(ip, op->func_hash))
2322                         return op;
2323         } 
2324
2325         return NULL;
2326 }
2327
2328 static struct ftrace_ops *
2329 ftrace_find_tramp_ops_curr(struct dyn_ftrace *rec)
2330 {
2331         struct ftrace_ops *op;
2332         unsigned long ip = rec->ip;
2333
2334         /*
2335          * Need to check removed ops first.
2336          * If they are being removed, and this rec has a tramp,
2337          * and this rec is in the ops list, then it would be the
2338          * one with the tramp.
2339          */
2340         if (removed_ops) {
2341                 if (hash_contains_ip(ip, &removed_ops->old_hash))
2342                         return removed_ops;
2343         }
2344
2345         /*
2346          * Need to find the current trampoline for a rec.
2347          * Now, a trampoline is only attached to a rec if there
2348          * was a single 'ops' attached to it. But this can be called
2349          * when we are adding another op to the rec or removing the
2350          * current one. Thus, if the op is being added, we can
2351          * ignore it because it hasn't attached itself to the rec
2352          * yet.
2353          *
2354          * If an ops is being modified (hooking to different functions)
2355          * then we don't care about the new functions that are being
2356          * added, just the old ones (that are probably being removed).
2357          *
2358          * If we are adding an ops to a function that already is using
2359          * a trampoline, it needs to be removed (trampolines are only
2360          * for single ops connected), then an ops that is not being
2361          * modified also needs to be checked.
2362          */
2363         do_for_each_ftrace_op(op, ftrace_ops_list) {
2364
2365                 if (!op->trampoline)
2366                         continue;
2367
2368                 /*
2369                  * If the ops is being added, it hasn't gotten to
2370                  * the point to be removed from this tree yet.
2371                  */
2372                 if (op->flags & FTRACE_OPS_FL_ADDING)
2373                         continue;
2374
2375
2376                 /*
2377                  * If the ops is being modified and is in the old
2378                  * hash, then it is probably being removed from this
2379                  * function.
2380                  */
2381                 if ((op->flags & FTRACE_OPS_FL_MODIFYING) &&
2382                     hash_contains_ip(ip, &op->old_hash))
2383                         return op;
2384                 /*
2385                  * If the ops is not being added or modified, and it's
2386                  * in its normal filter hash, then this must be the one
2387                  * we want!
2388                  */
2389                 if (!(op->flags & FTRACE_OPS_FL_MODIFYING) &&
2390                     hash_contains_ip(ip, op->func_hash))
2391                         return op;
2392
2393         } while_for_each_ftrace_op(op);
2394
2395         return NULL;
2396 }
2397
2398 static struct ftrace_ops *
2399 ftrace_find_tramp_ops_new(struct dyn_ftrace *rec)
2400 {
2401         struct ftrace_ops *op;
2402         unsigned long ip = rec->ip;
2403
2404         do_for_each_ftrace_op(op, ftrace_ops_list) {
2405                 /* pass rec in as regs to have non-NULL val */
2406                 if (hash_contains_ip(ip, op->func_hash))
2407                         return op;
2408         } while_for_each_ftrace_op(op);
2409
2410         return NULL;
2411 }
2412
2413 /**
2414  * ftrace_get_addr_new - Get the call address to set to
2415  * @rec:  The ftrace record descriptor
2416  *
2417  * If the record has the FTRACE_FL_REGS set, that means that it
2418  * wants to convert to a callback that saves all regs. If FTRACE_FL_REGS
2419  * is not not set, then it wants to convert to the normal callback.
2420  *
2421  * Returns the address of the trampoline to set to
2422  */
2423 unsigned long ftrace_get_addr_new(struct dyn_ftrace *rec)
2424 {
2425         struct ftrace_ops *ops;
2426
2427         /* Trampolines take precedence over regs */
2428         if (rec->flags & FTRACE_FL_TRAMP) {
2429                 ops = ftrace_find_tramp_ops_new(rec);
2430                 if (FTRACE_WARN_ON(!ops || !ops->trampoline)) {
2431                         pr_warn("Bad trampoline accounting at: %p (%pS) (%lx)\n",
2432                                 (void *)rec->ip, (void *)rec->ip, rec->flags);
2433                         /* Ftrace is shutting down, return anything */
2434                         return (unsigned long)FTRACE_ADDR;
2435                 }
2436                 return ops->trampoline;
2437         }
2438
2439         if (rec->flags & FTRACE_FL_REGS)
2440                 return (unsigned long)FTRACE_REGS_ADDR;
2441         else
2442                 return (unsigned long)FTRACE_ADDR;
2443 }
2444
2445 /**
2446  * ftrace_get_addr_curr - Get the call address that is already there
2447  * @rec:  The ftrace record descriptor
2448  *
2449  * The FTRACE_FL_REGS_EN is set when the record already points to
2450  * a function that saves all the regs. Basically the '_EN' version
2451  * represents the current state of the function.
2452  *
2453  * Returns the address of the trampoline that is currently being called
2454  */
2455 unsigned long ftrace_get_addr_curr(struct dyn_ftrace *rec)
2456 {
2457         struct ftrace_ops *ops;
2458
2459         /* Trampolines take precedence over regs */
2460         if (rec->flags & FTRACE_FL_TRAMP_EN) {
2461                 ops = ftrace_find_tramp_ops_curr(rec);
2462                 if (FTRACE_WARN_ON(!ops)) {
2463                         pr_warn("Bad trampoline accounting at: %p (%pS)\n",
2464                                 (void *)rec->ip, (void *)rec->ip);
2465                         /* Ftrace is shutting down, return anything */
2466                         return (unsigned long)FTRACE_ADDR;
2467                 }
2468                 return ops->trampoline;
2469         }
2470
2471         if (rec->flags & FTRACE_FL_REGS_EN)
2472                 return (unsigned long)FTRACE_REGS_ADDR;
2473         else
2474                 return (unsigned long)FTRACE_ADDR;
2475 }
2476
2477 static int
2478 __ftrace_replace_code(struct dyn_ftrace *rec, int enable)
2479 {
2480         unsigned long ftrace_old_addr;
2481         unsigned long ftrace_addr;
2482         int ret;
2483
2484         ftrace_addr = ftrace_get_addr_new(rec);
2485
2486         /* This needs to be done before we call ftrace_update_record */
2487         ftrace_old_addr = ftrace_get_addr_curr(rec);
2488
2489         ret = ftrace_update_record(rec, enable);
2490
2491         ftrace_bug_type = FTRACE_BUG_UNKNOWN;
2492
2493         switch (ret) {
2494         case FTRACE_UPDATE_IGNORE:
2495                 return 0;
2496
2497         case FTRACE_UPDATE_MAKE_CALL:
2498                 ftrace_bug_type = FTRACE_BUG_CALL;
2499                 return ftrace_make_call(rec, ftrace_addr);
2500
2501         case FTRACE_UPDATE_MAKE_NOP:
2502                 ftrace_bug_type = FTRACE_BUG_NOP;
2503                 return ftrace_make_nop(NULL, rec, ftrace_old_addr);
2504
2505         case FTRACE_UPDATE_MODIFY_CALL:
2506                 ftrace_bug_type = FTRACE_BUG_UPDATE;
2507                 return ftrace_modify_call(rec, ftrace_old_addr, ftrace_addr);
2508         }
2509
2510         return -1; /* unknow ftrace bug */
2511 }
2512
2513 void __weak ftrace_replace_code(int enable)
2514 {
2515         struct dyn_ftrace *rec;
2516         struct ftrace_page *pg;
2517         int failed;
2518
2519         if (unlikely(ftrace_disabled))
2520                 return;
2521
2522         do_for_each_ftrace_rec(pg, rec) {
2523
2524                 if (rec->flags & FTRACE_FL_DISABLED)
2525                         continue;
2526
2527                 failed = __ftrace_replace_code(rec, enable);
2528                 if (failed) {
2529                         ftrace_bug(failed, rec);
2530                         /* Stop processing */
2531                         return;
2532                 }
2533         } while_for_each_ftrace_rec();
2534 }
2535
2536 struct ftrace_rec_iter {
2537         struct ftrace_page      *pg;
2538         int                     index;
2539 };
2540
2541 /**
2542  * ftrace_rec_iter_start, start up iterating over traced functions
2543  *
2544  * Returns an iterator handle that is used to iterate over all
2545  * the records that represent address locations where functions
2546  * are traced.
2547  *
2548  * May return NULL if no records are available.
2549  */
2550 struct ftrace_rec_iter *ftrace_rec_iter_start(void)
2551 {
2552         /*
2553          * We only use a single iterator.
2554          * Protected by the ftrace_lock mutex.
2555          */
2556         static struct ftrace_rec_iter ftrace_rec_iter;
2557         struct ftrace_rec_iter *iter = &ftrace_rec_iter;
2558
2559         iter->pg = ftrace_pages_start;
2560         iter->index = 0;
2561
2562         /* Could have empty pages */
2563         while (iter->pg && !iter->pg->index)
2564                 iter->pg = iter->pg->next;
2565
2566         if (!iter->pg)
2567                 return NULL;
2568
2569         return iter;
2570 }
2571
2572 /**
2573  * ftrace_rec_iter_next, get the next record to process.
2574  * @iter: The handle to the iterator.
2575  *
2576  * Returns the next iterator after the given iterator @iter.
2577  */
2578 struct ftrace_rec_iter *ftrace_rec_iter_next(struct ftrace_rec_iter *iter)
2579 {
2580         iter->index++;
2581
2582         if (iter->index >= iter->pg->index) {
2583                 iter->pg = iter->pg->next;
2584                 iter->index = 0;
2585
2586                 /* Could have empty pages */
2587                 while (iter->pg && !iter->pg->index)
2588                         iter->pg = iter->pg->next;
2589         }
2590
2591         if (!iter->pg)
2592                 return NULL;
2593
2594         return iter;
2595 }
2596
2597 /**
2598  * ftrace_rec_iter_record, get the record at the iterator location
2599  * @iter: The current iterator location
2600  *
2601  * Returns the record that the current @iter is at.
2602  */
2603 struct dyn_ftrace *ftrace_rec_iter_record(struct ftrace_rec_iter *iter)
2604 {
2605         return &iter->pg->records[iter->index];
2606 }
2607
2608 static int
2609 ftrace_code_disable(struct module *mod, struct dyn_ftrace *rec)
2610 {
2611         int ret;
2612
2613         if (unlikely(ftrace_disabled))
2614                 return 0;
2615
2616         ret = ftrace_make_nop(mod, rec, MCOUNT_ADDR);
2617         if (ret) {
2618                 ftrace_bug_type = FTRACE_BUG_INIT;
2619                 ftrace_bug(ret, rec);
2620                 return 0;
2621         }
2622         return 1;
2623 }
2624
2625 /*
2626  * archs can override this function if they must do something
2627  * before the modifying code is performed.
2628  */
2629 int __weak ftrace_arch_code_modify_prepare(void)
2630 {
2631         return 0;
2632 }
2633
2634 /*
2635  * archs can override this function if they must do something
2636  * after the modifying code is performed.
2637  */
2638 int __weak ftrace_arch_code_modify_post_process(void)
2639 {
2640         return 0;
2641 }
2642
2643 void ftrace_modify_all_code(int command)
2644 {
2645         int update = command & FTRACE_UPDATE_TRACE_FUNC;
2646         int err = 0;
2647
2648         /*
2649          * If the ftrace_caller calls a ftrace_ops func directly,
2650          * we need to make sure that it only traces functions it
2651          * expects to trace. When doing the switch of functions,
2652          * we need to update to the ftrace_ops_list_func first
2653          * before the transition between old and new calls are set,
2654          * as the ftrace_ops_list_func will check the ops hashes
2655          * to make sure the ops are having the right functions
2656          * traced.
2657          */
2658         if (update) {
2659                 err = ftrace_update_ftrace_func(ftrace_ops_list_func);
2660                 if (FTRACE_WARN_ON(err))
2661                         return;
2662         }
2663
2664         if (command & FTRACE_UPDATE_CALLS)
2665                 ftrace_replace_code(1);
2666         else if (command & FTRACE_DISABLE_CALLS)
2667                 ftrace_replace_code(0);
2668
2669         if (update && ftrace_trace_function != ftrace_ops_list_func) {
2670                 function_trace_op = set_function_trace_op;
2671                 smp_wmb();
2672                 /* If irqs are disabled, we are in stop machine */
2673                 if (!irqs_disabled())
2674                         smp_call_function(ftrace_sync_ipi, NULL, 1);
2675                 err = ftrace_update_ftrace_func(ftrace_trace_function);
2676                 if (FTRACE_WARN_ON(err))
2677                         return;
2678         }
2679
2680         if (command & FTRACE_START_FUNC_RET)
2681                 err = ftrace_enable_ftrace_graph_caller();
2682         else if (command & FTRACE_STOP_FUNC_RET)
2683                 err = ftrace_disable_ftrace_graph_caller();
2684         FTRACE_WARN_ON(err);
2685 }
2686
2687 static int __ftrace_modify_code(void *data)
2688 {
2689         int *command = data;
2690
2691         ftrace_modify_all_code(*command);
2692
2693         return 0;
2694 }
2695
2696 /**
2697  * ftrace_run_stop_machine, go back to the stop machine method
2698  * @command: The command to tell ftrace what to do
2699  *
2700  * If an arch needs to fall back to the stop machine method, the
2701  * it can call this function.
2702  */
2703 void ftrace_run_stop_machine(int command)
2704 {
2705         stop_machine(__ftrace_modify_code, &command, NULL);
2706 }
2707
2708 /**
2709  * arch_ftrace_update_code, modify the code to trace or not trace
2710  * @command: The command that needs to be done
2711  *
2712  * Archs can override this function if it does not need to
2713  * run stop_machine() to modify code.
2714  */
2715 void __weak arch_ftrace_update_code(int command)
2716 {
2717         ftrace_run_stop_machine(command);
2718 }
2719
2720 static void ftrace_run_update_code(int command)
2721 {
2722         int ret;
2723
2724         ret = ftrace_arch_code_modify_prepare();
2725         FTRACE_WARN_ON(ret);
2726         if (ret)
2727                 return;
2728
2729         /*
2730          * By default we use stop_machine() to modify the code.
2731          * But archs can do what ever they want as long as it
2732          * is safe. The stop_machine() is the safest, but also
2733          * produces the most overhead.
2734          */
2735         arch_ftrace_update_code(command);
2736
2737         ret = ftrace_arch_code_modify_post_process();
2738         FTRACE_WARN_ON(ret);
2739 }
2740
2741 static void ftrace_run_modify_code(struct ftrace_ops *ops, int command,
2742                                    struct ftrace_ops_hash *old_hash)
2743 {
2744         ops->flags |= FTRACE_OPS_FL_MODIFYING;
2745         ops->old_hash.filter_hash = old_hash->filter_hash;
2746         ops->old_hash.notrace_hash = old_hash->notrace_hash;
2747         ftrace_run_update_code(command);
2748         ops->old_hash.filter_hash = NULL;
2749         ops->old_hash.notrace_hash = NULL;
2750         ops->flags &= ~FTRACE_OPS_FL_MODIFYING;
2751 }
2752
2753 static ftrace_func_t saved_ftrace_func;
2754 static int ftrace_start_up;
2755
2756 void __weak arch_ftrace_trampoline_free(struct ftrace_ops *ops)
2757 {
2758 }
2759
2760 static void per_cpu_ops_free(struct ftrace_ops *ops)
2761 {
2762         free_percpu(ops->disabled);
2763 }
2764
2765 static void ftrace_startup_enable(int command)
2766 {
2767         if (saved_ftrace_func != ftrace_trace_function) {
2768                 saved_ftrace_func = ftrace_trace_function;
2769                 command |= FTRACE_UPDATE_TRACE_FUNC;
2770         }
2771
2772         if (!command || !ftrace_enabled)
2773                 return;
2774
2775         ftrace_run_update_code(command);
2776 }
2777
2778 static void ftrace_startup_all(int command)
2779 {
2780         update_all_ops = true;
2781         ftrace_startup_enable(command);
2782         update_all_ops = false;
2783 }
2784
2785 static int ftrace_startup(struct ftrace_ops *ops, int command)
2786 {
2787         int ret;
2788
2789         if (unlikely(ftrace_disabled))
2790                 return -ENODEV;
2791
2792         ret = __register_ftrace_function(ops);
2793         if (ret)
2794                 return ret;
2795
2796         ftrace_start_up++;
2797
2798         /*
2799          * Note that ftrace probes uses this to start up
2800          * and modify functions it will probe. But we still
2801          * set the ADDING flag for modification, as probes
2802          * do not have trampolines. If they add them in the
2803          * future, then the probes will need to distinguish
2804          * between adding and updating probes.
2805          */
2806         ops->flags |= FTRACE_OPS_FL_ENABLED | FTRACE_OPS_FL_ADDING;
2807
2808         ret = ftrace_hash_ipmodify_enable(ops);
2809         if (ret < 0) {
2810                 /* Rollback registration process */
2811                 __unregister_ftrace_function(ops);
2812                 ftrace_start_up--;
2813                 ops->flags &= ~FTRACE_OPS_FL_ENABLED;
2814                 return ret;
2815         }
2816
2817         if (ftrace_hash_rec_enable(ops, 1))
2818                 command |= FTRACE_UPDATE_CALLS;
2819
2820         ftrace_startup_enable(command);
2821
2822         /*
2823          * If ftrace is in an undefined state, we just remove ops from list
2824          * to prevent the NULL pointer, instead of totally rolling it back and
2825          * free trampoline, because those actions could cause further damage.
2826          */
2827         if (unlikely(ftrace_disabled)) {
2828                 __unregister_ftrace_function(ops);
2829                 return -ENODEV;
2830         }
2831
2832         ops->flags &= ~FTRACE_OPS_FL_ADDING;
2833
2834         return 0;
2835 }
2836
2837 static int ftrace_shutdown(struct ftrace_ops *ops, int command)
2838 {
2839         int ret;
2840
2841         if (unlikely(ftrace_disabled))
2842                 return -ENODEV;
2843
2844         ret = __unregister_ftrace_function(ops);
2845         if (ret)
2846                 return ret;
2847
2848         ftrace_start_up--;
2849         /*
2850          * Just warn in case of unbalance, no need to kill ftrace, it's not
2851          * critical but the ftrace_call callers may be never nopped again after
2852          * further ftrace uses.
2853          */
2854         WARN_ON_ONCE(ftrace_start_up < 0);
2855
2856         /* Disabling ipmodify never fails */
2857         ftrace_hash_ipmodify_disable(ops);
2858
2859         if (ftrace_hash_rec_disable(ops, 1))
2860                 command |= FTRACE_UPDATE_CALLS;
2861
2862         ops->flags &= ~FTRACE_OPS_FL_ENABLED;
2863
2864         if (saved_ftrace_func != ftrace_trace_function) {
2865                 saved_ftrace_func = ftrace_trace_function;
2866                 command |= FTRACE_UPDATE_TRACE_FUNC;
2867         }
2868
2869         if (!command || !ftrace_enabled) {
2870                 /*
2871                  * If these are dynamic or per_cpu ops, they still
2872                  * need their data freed. Since, function tracing is
2873                  * not currently active, we can just free them
2874                  * without synchronizing all CPUs.
2875                  */
2876                 if (ops->flags & (FTRACE_OPS_FL_DYNAMIC | FTRACE_OPS_FL_PER_CPU))
2877                         goto free_ops;
2878
2879                 return 0;
2880         }
2881
2882         /*
2883          * If the ops uses a trampoline, then it needs to be
2884          * tested first on update.
2885          */
2886         ops->flags |= FTRACE_OPS_FL_REMOVING;
2887         removed_ops = ops;
2888
2889         /* The trampoline logic checks the old hashes */
2890         ops->old_hash.filter_hash = ops->func_hash->filter_hash;
2891         ops->old_hash.notrace_hash = ops->func_hash->notrace_hash;
2892
2893         ftrace_run_update_code(command);
2894
2895         /*
2896          * If there's no more ops registered with ftrace, run a
2897          * sanity check to make sure all rec flags are cleared.
2898          */
2899         if (rcu_dereference_protected(ftrace_ops_list,
2900                         lockdep_is_held(&ftrace_lock)) == &ftrace_list_end) {
2901                 struct ftrace_page *pg;
2902                 struct dyn_ftrace *rec;
2903
2904                 do_for_each_ftrace_rec(pg, rec) {
2905                         if (FTRACE_WARN_ON_ONCE(rec->flags & ~FTRACE_FL_DISABLED))
2906                                 pr_warn("  %pS flags:%lx\n",
2907                                         (void *)rec->ip, rec->flags);
2908                 } while_for_each_ftrace_rec();
2909         }
2910
2911         ops->old_hash.filter_hash = NULL;
2912         ops->old_hash.notrace_hash = NULL;
2913
2914         removed_ops = NULL;
2915         ops->flags &= ~FTRACE_OPS_FL_REMOVING;
2916
2917         /*
2918          * Dynamic ops may be freed, we must make sure that all
2919          * callers are done before leaving this function.
2920          * The same goes for freeing the per_cpu data of the per_cpu
2921          * ops.
2922          */
2923         if (ops->flags & (FTRACE_OPS_FL_DYNAMIC | FTRACE_OPS_FL_PER_CPU)) {
2924                 /*
2925                  * We need to do a hard force of sched synchronization.
2926                  * This is because we use preempt_disable() to do RCU, but
2927                  * the function tracers can be called where RCU is not watching
2928                  * (like before user_exit()). We can not rely on the RCU
2929                  * infrastructure to do the synchronization, thus we must do it
2930                  * ourselves.
2931                  */
2932                 schedule_on_each_cpu(ftrace_sync);
2933
2934                 /*
2935                  * When the kernel is preeptive, tasks can be preempted
2936                  * while on a ftrace trampoline. Just scheduling a task on
2937                  * a CPU is not good enough to flush them. Calling
2938                  * synchornize_rcu_tasks() will wait for those tasks to
2939                  * execute and either schedule voluntarily or enter user space.
2940                  */
2941                 if (IS_ENABLED(CONFIG_PREEMPT))
2942                         synchronize_rcu_tasks();
2943
2944  free_ops:
2945                 arch_ftrace_trampoline_free(ops);
2946
2947                 if (ops->flags & FTRACE_OPS_FL_PER_CPU)
2948                         per_cpu_ops_free(ops);
2949         }
2950
2951         return 0;
2952 }
2953
2954 static void ftrace_startup_sysctl(void)
2955 {
2956         int command;
2957
2958         if (unlikely(ftrace_disabled))
2959                 return;
2960
2961         /* Force update next time */
2962         saved_ftrace_func = NULL;
2963         /* ftrace_start_up is true if we want ftrace running */
2964         if (ftrace_start_up) {
2965                 command = FTRACE_UPDATE_CALLS;
2966                 if (ftrace_graph_active)
2967                         command |= FTRACE_START_FUNC_RET;
2968                 ftrace_startup_enable(command);
2969         }
2970 }
2971
2972 static void ftrace_shutdown_sysctl(void)
2973 {
2974         int command;
2975
2976         if (unlikely(ftrace_disabled))
2977                 return;
2978
2979         /* ftrace_start_up is true if ftrace is running */
2980         if (ftrace_start_up) {
2981                 command = FTRACE_DISABLE_CALLS;
2982                 if (ftrace_graph_active)
2983                         command |= FTRACE_STOP_FUNC_RET;
2984                 ftrace_run_update_code(command);
2985         }
2986 }
2987
2988 static u64              ftrace_update_time;
2989 unsigned long           ftrace_update_tot_cnt;
2990
2991 static inline int ops_traces_mod(struct ftrace_ops *ops)
2992 {
2993         /*
2994          * Filter_hash being empty will default to trace module.
2995          * But notrace hash requires a test of individual module functions.
2996          */
2997         return ftrace_hash_empty(ops->func_hash->filter_hash) &&
2998                 ftrace_hash_empty(ops->func_hash->notrace_hash);
2999 }
3000
3001 /*
3002  * Check if the current ops references the record.
3003  *
3004  * If the ops traces all functions, then it was already accounted for.
3005  * If the ops does not trace the current record function, skip it.
3006  * If the ops ignores the function via notrace filter, skip it.
3007  */
3008 static inline bool
3009 ops_references_rec(struct ftrace_ops *ops, struct dyn_ftrace *rec)
3010 {
3011         /* If ops isn't enabled, ignore it */
3012         if (!(ops->flags & FTRACE_OPS_FL_ENABLED))
3013                 return 0;
3014
3015         /* If ops traces all then it includes this function */
3016         if (ops_traces_mod(ops))
3017                 return 1;
3018
3019         /* The function must be in the filter */
3020         if (!ftrace_hash_empty(ops->func_hash->filter_hash) &&
3021             !__ftrace_lookup_ip(ops->func_hash->filter_hash, rec->ip))
3022                 return 0;
3023
3024         /* If in notrace hash, we ignore it too */
3025         if (ftrace_lookup_ip(ops->func_hash->notrace_hash, rec->ip))
3026                 return 0;
3027
3028         return 1;
3029 }
3030
3031 static int ftrace_update_code(struct module *mod, struct ftrace_page *new_pgs)
3032 {
3033         struct ftrace_page *pg;
3034         struct dyn_ftrace *p;
3035         u64 start, stop;
3036         unsigned long update_cnt = 0;
3037         unsigned long rec_flags = 0;
3038         int i;
3039
3040         start = ftrace_now(raw_smp_processor_id());
3041
3042         /*
3043          * When a module is loaded, this function is called to convert
3044          * the calls to mcount in its text to nops, and also to create
3045          * an entry in the ftrace data. Now, if ftrace is activated
3046          * after this call, but before the module sets its text to
3047          * read-only, the modification of enabling ftrace can fail if
3048          * the read-only is done while ftrace is converting the calls.
3049          * To prevent this, the module's records are set as disabled
3050          * and will be enabled after the call to set the module's text
3051          * to read-only.
3052          */
3053         if (mod)
3054                 rec_flags |= FTRACE_FL_DISABLED;
3055
3056         for (pg = new_pgs; pg; pg = pg->next) {
3057
3058                 for (i = 0; i < pg->index; i++) {
3059
3060                         /* If something went wrong, bail without enabling anything */
3061                         if (unlikely(ftrace_disabled))
3062                                 return -1;
3063
3064                         p = &pg->records[i];
3065                         p->flags = rec_flags;
3066
3067                         /*
3068                          * Do the initial record conversion from mcount jump
3069                          * to the NOP instructions.
3070                          */
3071                         if (!ftrace_code_disable(mod, p))
3072                                 break;
3073
3074                         update_cnt++;
3075                 }
3076         }
3077
3078         stop = ftrace_now(raw_smp_processor_id());
3079         ftrace_update_time = stop - start;
3080         ftrace_update_tot_cnt += update_cnt;
3081
3082         return 0;
3083 }
3084
3085 static int ftrace_allocate_records(struct ftrace_page *pg, int count)
3086 {
3087         int order;
3088         int cnt;
3089
3090         if (WARN_ON(!count))
3091                 return -EINVAL;
3092
3093         order = get_count_order(DIV_ROUND_UP(count, ENTRIES_PER_PAGE));
3094
3095         /*
3096          * We want to fill as much as possible. No more than a page
3097          * may be empty.
3098          */
3099         while ((PAGE_SIZE << order) / ENTRY_SIZE >= count + ENTRIES_PER_PAGE)
3100                 order--;
3101
3102  again:
3103         pg->records = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, order);
3104
3105         if (!pg->records) {
3106                 /* if we can't allocate this size, try something smaller */
3107                 if (!order)
3108                         return -ENOMEM;
3109                 order--;
3110                 goto again;
3111         }
3112
3113         cnt = (PAGE_SIZE << order) / ENTRY_SIZE;
3114         pg->size = cnt;
3115
3116         if (cnt > count)
3117                 cnt = count;
3118
3119         return cnt;
3120 }
3121
3122 static struct ftrace_page *
3123 ftrace_allocate_pages(unsigned long num_to_init)
3124 {
3125         struct ftrace_page *start_pg;
3126         struct ftrace_page *pg;
3127         int order;
3128         int cnt;
3129
3130         if (!num_to_init)
3131                 return 0;
3132
3133         start_pg = pg = kzalloc(sizeof(*pg), GFP_KERNEL);
3134         if (!pg)
3135                 return NULL;
3136
3137         /*
3138          * Try to allocate as much as possible in one continues
3139          * location that fills in all of the space. We want to
3140          * waste as little space as possible.
3141          */
3142         for (;;) {
3143                 cnt = ftrace_allocate_records(pg, num_to_init);
3144                 if (cnt < 0)
3145                         goto free_pages;
3146
3147                 num_to_init -= cnt;
3148                 if (!num_to_init)
3149                         break;
3150
3151                 pg->next = kzalloc(sizeof(*pg), GFP_KERNEL);
3152                 if (!pg->next)
3153                         goto free_pages;
3154
3155                 pg = pg->next;
3156         }
3157
3158         return start_pg;
3159
3160  free_pages:
3161         pg = start_pg;
3162         while (pg) {
3163                 order = get_count_order(pg->size / ENTRIES_PER_PAGE);
3164                 free_pages((unsigned long)pg->records, order);
3165                 start_pg = pg->next;
3166                 kfree(pg);
3167                 pg = start_pg;
3168         }
3169         pr_info("ftrace: FAILED to allocate memory for functions\n");
3170         return NULL;
3171 }
3172
3173 #define FTRACE_BUFF_MAX (KSYM_SYMBOL_LEN+4) /* room for wildcards */
3174
3175 struct ftrace_iterator {
3176         loff_t                          pos;
3177         loff_t                          func_pos;
3178         loff_t                          mod_pos;
3179         struct ftrace_page              *pg;
3180         struct dyn_ftrace               *func;
3181         struct ftrace_func_probe        *probe;
3182         struct ftrace_func_entry        *probe_entry;
3183         struct trace_parser             parser;
3184         struct ftrace_hash              *hash;
3185         struct ftrace_ops               *ops;
3186         struct trace_array              *tr;
3187         struct list_head                *mod_list;
3188         int                             pidx;
3189         int                             idx;
3190         unsigned                        flags;
3191 };
3192
3193 static void *
3194 t_probe_next(struct seq_file *m, loff_t *pos)
3195 {
3196         struct ftrace_iterator *iter = m->private;
3197         struct trace_array *tr = iter->ops->private;
3198         struct list_head *func_probes;
3199         struct ftrace_hash *hash;
3200         struct list_head *next;
3201         struct hlist_node *hnd = NULL;
3202         struct hlist_head *hhd;
3203         int size;
3204
3205         (*pos)++;
3206         iter->pos = *pos;
3207
3208         if (!tr)
3209                 return NULL;
3210
3211         func_probes = &tr->func_probes;
3212         if (list_empty(func_probes))
3213                 return NULL;
3214
3215         if (!iter->probe) {
3216                 next = func_probes->next;
3217                 iter->probe = list_entry(next, struct ftrace_func_probe, list);
3218         }
3219
3220         if (iter->probe_entry)
3221                 hnd = &iter->probe_entry->hlist;
3222
3223         hash = iter->probe->ops.func_hash->filter_hash;
3224
3225         /*
3226          * A probe being registered may temporarily have an empty hash
3227          * and it's at the end of the func_probes list.
3228          */
3229         if (!hash || hash == EMPTY_HASH)
3230                 return NULL;
3231
3232         size = 1 << hash->size_bits;
3233
3234  retry:
3235         if (iter->pidx >= size) {
3236                 if (iter->probe->list.next == func_probes)
3237                         return NULL;
3238                 next = iter->probe->list.next;
3239                 iter->probe = list_entry(next, struct ftrace_func_probe, list);
3240                 hash = iter->probe->ops.func_hash->filter_hash;
3241                 size = 1 << hash->size_bits;
3242                 iter->pidx = 0;
3243         }
3244
3245         hhd = &hash->buckets[iter->pidx];
3246
3247         if (hlist_empty(hhd)) {
3248                 iter->pidx++;
3249                 hnd = NULL;
3250                 goto retry;
3251         }
3252
3253         if (!hnd)
3254                 hnd = hhd->first;
3255         else {
3256                 hnd = hnd->next;
3257                 if (!hnd) {
3258                         iter->pidx++;
3259                         goto retry;
3260                 }
3261         }
3262
3263         if (WARN_ON_ONCE(!hnd))
3264                 return NULL;
3265
3266         iter->probe_entry = hlist_entry(hnd, struct ftrace_func_entry, hlist);
3267
3268         return iter;
3269 }
3270
3271 static void *t_probe_start(struct seq_file *m, loff_t *pos)
3272 {
3273         struct ftrace_iterator *iter = m->private;
3274         void *p = NULL;
3275         loff_t l;
3276
3277         if (!(iter->flags & FTRACE_ITER_DO_PROBES))
3278                 return NULL;
3279
3280         if (iter->mod_pos > *pos)
3281                 return NULL;
3282
3283         iter->probe = NULL;
3284         iter->probe_entry = NULL;
3285         iter->pidx = 0;
3286         for (l = 0; l <= (*pos - iter->mod_pos); ) {
3287                 p = t_probe_next(m, &l);
3288                 if (!p)
3289                         break;
3290         }
3291         if (!p)
3292                 return NULL;
3293
3294         /* Only set this if we have an item */
3295         iter->flags |= FTRACE_ITER_PROBE;
3296
3297         return iter;
3298 }
3299
3300 static int
3301 t_probe_show(struct seq_file *m, struct ftrace_iterator *iter)
3302 {
3303         struct ftrace_func_entry *probe_entry;
3304         struct ftrace_probe_ops *probe_ops;
3305         struct ftrace_func_probe *probe;
3306
3307         probe = iter->probe;
3308         probe_entry = iter->probe_entry;
3309
3310         if (WARN_ON_ONCE(!probe || !probe_entry))
3311                 return -EIO;
3312
3313         probe_ops = probe->probe_ops;
3314
3315         if (probe_ops->print)
3316                 return probe_ops->print(m, probe_entry->ip, probe_ops, probe->data);
3317
3318         seq_printf(m, "%ps:%ps\n", (void *)probe_entry->ip,
3319                    (void *)probe_ops->func);
3320
3321         return 0;
3322 }
3323
3324 static void *
3325 t_mod_next(struct seq_file *m, loff_t *pos)
3326 {
3327         struct ftrace_iterator *iter = m->private;
3328         struct trace_array *tr = iter->tr;
3329
3330         (*pos)++;
3331         iter->pos = *pos;
3332
3333         iter->mod_list = iter->mod_list->next;
3334
3335         if (iter->mod_list == &tr->mod_trace ||
3336             iter->mod_list == &tr->mod_notrace) {
3337                 iter->flags &= ~FTRACE_ITER_MOD;
3338                 return NULL;
3339         }
3340
3341         iter->mod_pos = *pos;
3342
3343         return iter;
3344 }
3345
3346 static void *t_mod_start(struct seq_file *m, loff_t *pos)
3347 {
3348         struct ftrace_iterator *iter = m->private;
3349         void *p = NULL;
3350         loff_t l;
3351
3352         if (iter->func_pos > *pos)
3353                 return NULL;
3354
3355         iter->mod_pos = iter->func_pos;
3356
3357         /* probes are only available if tr is set */
3358         if (!iter->tr)
3359                 return NULL;
3360
3361         for (l = 0; l <= (*pos - iter->func_pos); ) {
3362                 p = t_mod_next(m, &l);
3363                 if (!p)
3364                         break;
3365         }
3366         if (!p) {
3367                 iter->flags &= ~FTRACE_ITER_MOD;
3368                 return t_probe_start(m, pos);
3369         }
3370
3371         /* Only set this if we have an item */
3372         iter->flags |= FTRACE_ITER_MOD;
3373
3374         return iter;
3375 }
3376
3377 static int
3378 t_mod_show(struct seq_file *m, struct ftrace_iterator *iter)
3379 {
3380         struct ftrace_mod_load *ftrace_mod;
3381         struct trace_array *tr = iter->tr;
3382
3383         if (WARN_ON_ONCE(!iter->mod_list) ||
3384                          iter->mod_list == &tr->mod_trace ||
3385                          iter->mod_list == &tr->mod_notrace)
3386                 return -EIO;
3387
3388         ftrace_mod = list_entry(iter->mod_list, struct ftrace_mod_load, list);
3389
3390         if (ftrace_mod->func)
3391                 seq_printf(m, "%s", ftrace_mod->func);
3392         else
3393                 seq_putc(m, '*');
3394
3395         seq_printf(m, ":mod:%s\n", ftrace_mod->module);
3396
3397         return 0;
3398 }
3399
3400 static void *
3401 t_func_next(struct seq_file *m, loff_t *pos)
3402 {
3403         struct ftrace_iterator *iter = m->private;
3404         struct dyn_ftrace *rec = NULL;
3405
3406         (*pos)++;
3407
3408  retry:
3409         if (iter->idx >= iter->pg->index) {
3410                 if (iter->pg->next) {
3411                         iter->pg = iter->pg->next;
3412                         iter->idx = 0;
3413                         goto retry;
3414                 }
3415         } else {
3416                 rec = &iter->pg->records[iter->idx++];
3417                 if (((iter->flags & (FTRACE_ITER_FILTER | FTRACE_ITER_NOTRACE)) &&
3418                      !ftrace_lookup_ip(iter->hash, rec->ip)) ||
3419
3420                     ((iter->flags & FTRACE_ITER_ENABLED) &&
3421                      !(rec->flags & FTRACE_FL_ENABLED))) {
3422
3423                         rec = NULL;
3424                         goto retry;
3425                 }
3426         }
3427
3428         if (!rec)
3429                 return NULL;
3430
3431         iter->pos = iter->func_pos = *pos;
3432         iter->func = rec;
3433
3434         return iter;
3435 }
3436
3437 static void *
3438 t_next(struct seq_file *m, void *v, loff_t *pos)
3439 {
3440         struct ftrace_iterator *iter = m->private;
3441         loff_t l = *pos; /* t_probe_start() must use original pos */
3442         void *ret;
3443
3444         if (unlikely(ftrace_disabled))
3445                 return NULL;
3446
3447         if (iter->flags & FTRACE_ITER_PROBE)
3448                 return t_probe_next(m, pos);
3449
3450         if (iter->flags & FTRACE_ITER_MOD)
3451                 return t_mod_next(m, pos);
3452
3453         if (iter->flags & FTRACE_ITER_PRINTALL) {
3454                 /* next must increment pos, and t_probe_start does not */
3455                 (*pos)++;
3456                 return t_mod_start(m, &l);
3457         }
3458
3459         ret = t_func_next(m, pos);
3460
3461         if (!ret)
3462                 return t_mod_start(m, &l);
3463
3464         return ret;
3465 }
3466
3467 static void reset_iter_read(struct ftrace_iterator *iter)
3468 {
3469         iter->pos = 0;
3470         iter->func_pos = 0;
3471         iter->flags &= ~(FTRACE_ITER_PRINTALL | FTRACE_ITER_PROBE | FTRACE_ITER_MOD);
3472 }
3473
3474 static void *t_start(struct seq_file *m, loff_t *pos)
3475 {
3476         struct ftrace_iterator *iter = m->private;
3477         void *p = NULL;
3478         loff_t l;
3479
3480         mutex_lock(&ftrace_lock);
3481
3482         if (unlikely(ftrace_disabled))
3483                 return NULL;
3484
3485         /*
3486          * If an lseek was done, then reset and start from beginning.
3487          */
3488         if (*pos < iter->pos)
3489                 reset_iter_read(iter);
3490
3491         /*
3492          * For set_ftrace_filter reading, if we have the filter
3493          * off, we can short cut and just print out that all
3494          * functions are enabled.
3495          */
3496         if ((iter->flags & (FTRACE_ITER_FILTER | FTRACE_ITER_NOTRACE)) &&
3497             ftrace_hash_empty(iter->hash)) {
3498                 iter->func_pos = 1; /* Account for the message */
3499                 if (*pos > 0)
3500                         return t_mod_start(m, pos);
3501                 iter->flags |= FTRACE_ITER_PRINTALL;
3502                 /* reset in case of seek/pread */
3503                 iter->flags &= ~FTRACE_ITER_PROBE;
3504                 return iter;
3505         }
3506
3507         if (iter->flags & FTRACE_ITER_MOD)
3508                 return t_mod_start(m, pos);
3509
3510         /*
3511          * Unfortunately, we need to restart at ftrace_pages_start
3512          * every time we let go of the ftrace_mutex. This is because
3513          * those pointers can change without the lock.
3514          */
3515         iter->pg = ftrace_pages_start;
3516         iter->idx = 0;
3517         for (l = 0; l <= *pos; ) {
3518                 p = t_func_next(m, &l);
3519                 if (!p)
3520                         break;
3521         }
3522
3523         if (!p)
3524                 return t_mod_start(m, pos);
3525
3526         return iter;
3527 }
3528
3529 static void t_stop(struct seq_file *m, void *p)
3530 {
3531         mutex_unlock(&ftrace_lock);
3532 }
3533
3534 void * __weak
3535 arch_ftrace_trampoline_func(struct ftrace_ops *ops, struct dyn_ftrace *rec)
3536 {
3537         return NULL;
3538 }
3539
3540 static void add_trampoline_func(struct seq_file *m, struct ftrace_ops *ops,
3541                                 struct dyn_ftrace *rec)
3542 {
3543         void *ptr;
3544
3545         ptr = arch_ftrace_trampoline_func(ops, rec);
3546         if (ptr)
3547                 seq_printf(m, " ->%pS", ptr);
3548 }
3549
3550 static int t_show(struct seq_file *m, void *v)
3551 {
3552         struct ftrace_iterator *iter = m->private;
3553         struct dyn_ftrace *rec;
3554
3555         if (iter->flags & FTRACE_ITER_PROBE)
3556                 return t_probe_show(m, iter);
3557
3558         if (iter->flags & FTRACE_ITER_MOD)
3559                 return t_mod_show(m, iter);
3560
3561         if (iter->flags & FTRACE_ITER_PRINTALL) {
3562                 if (iter->flags & FTRACE_ITER_NOTRACE)
3563                         seq_puts(m, "#### no functions disabled ####\n");
3564                 else
3565                         seq_puts(m, "#### all functions enabled ####\n");
3566                 return 0;
3567         }
3568
3569         rec = iter->func;
3570
3571         if (!rec)
3572                 return 0;
3573
3574         seq_printf(m, "%ps", (void *)rec->ip);
3575         if (iter->flags & FTRACE_ITER_ENABLED) {
3576                 struct ftrace_ops *ops;
3577
3578                 seq_printf(m, " (%ld)%s%s",
3579                            ftrace_rec_count(rec),
3580                            rec->flags & FTRACE_FL_REGS ? " R" : "  ",
3581                            rec->flags & FTRACE_FL_IPMODIFY ? " I" : "  ");
3582                 if (rec->flags & FTRACE_FL_TRAMP_EN) {
3583                         ops = ftrace_find_tramp_ops_any(rec);
3584                         if (ops) {
3585                                 do {
3586                                         seq_printf(m, "\ttramp: %pS (%pS)",
3587                                                    (void *)ops->trampoline,
3588                                                    (void *)ops->func);
3589                                         add_trampoline_func(m, ops, rec);
3590                                         ops = ftrace_find_tramp_ops_next(rec, ops);
3591                                 } while (ops);
3592                         } else
3593                                 seq_puts(m, "\ttramp: ERROR!");
3594                 } else {
3595                         add_trampoline_func(m, NULL, rec);
3596                 }
3597         }       
3598
3599         seq_putc(m, '\n');
3600
3601         return 0;
3602 }
3603
3604 static const struct seq_operations show_ftrace_seq_ops = {
3605         .start = t_start,
3606         .next = t_next,
3607         .stop = t_stop,
3608         .show = t_show,
3609 };
3610
3611 static int
3612 ftrace_avail_open(struct inode *inode, struct file *file)
3613 {
3614         struct ftrace_iterator *iter;
3615
3616         if (unlikely(ftrace_disabled))
3617                 return -ENODEV;
3618
3619         iter = __seq_open_private(file, &show_ftrace_seq_ops, sizeof(*iter));
3620         if (!iter)
3621                 return -ENOMEM;
3622
3623         iter->pg = ftrace_pages_start;
3624         iter->ops = &global_ops;
3625
3626         return 0;
3627 }
3628
3629 static int
3630 ftrace_enabled_open(struct inode *inode, struct file *file)
3631 {
3632         struct ftrace_iterator *iter;
3633
3634         iter = __seq_open_private(file, &show_ftrace_seq_ops, sizeof(*iter));
3635         if (!iter)
3636                 return -ENOMEM;
3637
3638         iter->pg = ftrace_pages_start;
3639         iter->flags = FTRACE_ITER_ENABLED;
3640         iter->ops = &global_ops;
3641
3642         return 0;
3643 }
3644
3645 /**
3646  * ftrace_regex_open - initialize function tracer filter files
3647  * @ops: The ftrace_ops that hold the hash filters
3648  * @flag: The type of filter to process
3649  * @inode: The inode, usually passed in to your open routine
3650  * @file: The file, usually passed in to your open routine
3651  *
3652  * ftrace_regex_open() initializes the filter files for the
3653  * @ops. Depending on @flag it may process the filter hash or
3654  * the notrace hash of @ops. With this called from the open
3655  * routine, you can use ftrace_filter_write() for the write
3656  * routine if @flag has FTRACE_ITER_FILTER set, or
3657  * ftrace_notrace_write() if @flag has FTRACE_ITER_NOTRACE set.
3658  * tracing_lseek() should be used as the lseek routine, and
3659  * release must call ftrace_regex_release().
3660  */
3661 int
3662 ftrace_regex_open(struct ftrace_ops *ops, int flag,
3663                   struct inode *inode, struct file *file)
3664 {
3665         struct ftrace_iterator *iter;
3666         struct ftrace_hash *hash;
3667         struct list_head *mod_head;
3668         struct trace_array *tr = ops->private;
3669         int ret = -ENOMEM;
3670
3671         ftrace_ops_init(ops);
3672
3673         if (unlikely(ftrace_disabled))
3674                 return -ENODEV;
3675
3676         if (tr && trace_array_get(tr) < 0)
3677                 return -ENODEV;
3678
3679         iter = kzalloc(sizeof(*iter), GFP_KERNEL);
3680         if (!iter)
3681                 goto out;
3682
3683         if (trace_parser_get_init(&iter->parser, FTRACE_BUFF_MAX))
3684                 goto out;
3685
3686         iter->ops = ops;
3687         iter->flags = flag;
3688         iter->tr = tr;
3689
3690         mutex_lock(&ops->func_hash->regex_lock);
3691
3692         if (flag & FTRACE_ITER_NOTRACE) {
3693                 hash = ops->func_hash->notrace_hash;
3694                 mod_head = tr ? &tr->mod_notrace : NULL;
3695         } else {
3696                 hash = ops->func_hash->filter_hash;
3697                 mod_head = tr ? &tr->mod_trace : NULL;
3698         }
3699
3700         iter->mod_list = mod_head;
3701
3702         if (file->f_mode & FMODE_WRITE) {
3703                 const int size_bits = FTRACE_HASH_DEFAULT_BITS;
3704
3705                 if (file->f_flags & O_TRUNC) {
3706                         iter->hash = alloc_ftrace_hash(size_bits);
3707                         clear_ftrace_mod_list(mod_head);
3708                 } else {
3709                         iter->hash = alloc_and_copy_ftrace_hash(size_bits, hash);
3710                 }
3711
3712                 if (!iter->hash) {
3713                         trace_parser_put(&iter->parser);
3714                         goto out_unlock;
3715                 }
3716         } else
3717                 iter->hash = hash;
3718
3719         ret = 0;
3720
3721         if (file->f_mode & FMODE_READ) {
3722                 iter->pg = ftrace_pages_start;
3723
3724                 ret = seq_open(file, &show_ftrace_seq_ops);
3725                 if (!ret) {
3726                         struct seq_file *m = file->private_data;
3727                         m->private = iter;
3728                 } else {
3729                         /* Failed */
3730                         free_ftrace_hash(iter->hash);
3731                         trace_parser_put(&iter->parser);
3732                 }
3733         } else
3734                 file->private_data = iter;
3735
3736  out_unlock:
3737         mutex_unlock(&ops->func_hash->regex_lock);
3738
3739  out:
3740         if (ret) {
3741                 kfree(iter);
3742                 if (tr)
3743                         trace_array_put(tr);
3744         }
3745
3746         return ret;
3747 }
3748
3749 static int
3750 ftrace_filter_open(struct inode *inode, struct file *file)
3751 {
3752         struct ftrace_ops *ops = inode->i_private;
3753
3754         return ftrace_regex_open(ops,
3755                         FTRACE_ITER_FILTER | FTRACE_ITER_DO_PROBES,
3756                         inode, file);
3757 }
3758
3759 static int
3760 ftrace_notrace_open(struct inode *inode, struct file *file)
3761 {
3762         struct ftrace_ops *ops = inode->i_private;
3763
3764         return ftrace_regex_open(ops, FTRACE_ITER_NOTRACE,
3765                                  inode, file);
3766 }
3767
3768 /* Type for quick search ftrace basic regexes (globs) from filter_parse_regex */
3769 struct ftrace_glob {
3770         char *search;
3771         unsigned len;
3772         int type;
3773 };
3774
3775 /*
3776  * If symbols in an architecture don't correspond exactly to the user-visible
3777  * name of what they represent, it is possible to define this function to
3778  * perform the necessary adjustments.
3779 */
3780 char * __weak arch_ftrace_match_adjust(char *str, const char *search)
3781 {
3782         return str;
3783 }
3784
3785 static int ftrace_match(char *str, struct ftrace_glob *g)
3786 {
3787         int matched = 0;
3788         int slen;
3789
3790         str = arch_ftrace_match_adjust(str, g->search);
3791
3792         switch (g->type) {
3793         case MATCH_FULL:
3794                 if (strcmp(str, g->search) == 0)
3795                         matched = 1;
3796                 break;
3797         case MATCH_FRONT_ONLY:
3798                 if (strncmp(str, g->search, g->len) == 0)
3799                         matched = 1;
3800                 break;
3801         case MATCH_MIDDLE_ONLY:
3802                 if (strstr(str, g->search))
3803                         matched = 1;
3804                 break;
3805         case MATCH_END_ONLY:
3806                 slen = strlen(str);
3807                 if (slen >= g->len &&
3808                     memcmp(str + slen - g->len, g->search, g->len) == 0)
3809                         matched = 1;
3810                 break;
3811         case MATCH_GLOB:
3812                 if (glob_match(g->search, str))
3813                         matched = 1;
3814                 break;
3815         }
3816
3817         return matched;
3818 }
3819
3820 static int
3821 enter_record(struct ftrace_hash *hash, struct dyn_ftrace *rec, int clear_filter)
3822 {
3823         struct ftrace_func_entry *entry;
3824         int ret = 0;
3825
3826         entry = ftrace_lookup_ip(hash, rec->ip);
3827         if (clear_filter) {
3828                 /* Do nothing if it doesn't exist */
3829                 if (!entry)
3830                         return 0;
3831
3832                 free_hash_entry(hash, entry);
3833         } else {
3834                 /* Do nothing if it exists */
3835                 if (entry)
3836                         return 0;
3837
3838                 ret = add_hash_entry(hash, rec->ip);
3839         }
3840         return ret;
3841 }
3842
3843 static int
3844 ftrace_match_record(struct dyn_ftrace *rec, struct ftrace_glob *func_g,
3845                 struct ftrace_glob *mod_g, int exclude_mod)
3846 {
3847         char str[KSYM_SYMBOL_LEN];
3848         char *modname;
3849
3850         kallsyms_lookup(rec->ip, NULL, NULL, &modname, str);
3851
3852         if (mod_g) {
3853                 int mod_matches = (modname) ? ftrace_match(modname, mod_g) : 0;
3854
3855                 /* blank module name to match all modules */
3856                 if (!mod_g->len) {
3857                         /* blank module globbing: modname xor exclude_mod */
3858                         if (!exclude_mod != !modname)
3859                                 goto func_match;
3860                         return 0;
3861                 }
3862
3863                 /*
3864                  * exclude_mod is set to trace everything but the given
3865                  * module. If it is set and the module matches, then
3866                  * return 0. If it is not set, and the module doesn't match
3867                  * also return 0. Otherwise, check the function to see if
3868                  * that matches.
3869                  */
3870                 if (!mod_matches == !exclude_mod)
3871                         return 0;
3872 func_match:
3873                 /* blank search means to match all funcs in the mod */
3874                 if (!func_g->len)
3875                         return 1;
3876         }
3877
3878         return ftrace_match(str, func_g);
3879 }
3880
3881 static int
3882 match_records(struct ftrace_hash *hash, char *func, int len, char *mod)
3883 {
3884         struct ftrace_page *pg;
3885         struct dyn_ftrace *rec;
3886         struct ftrace_glob func_g = { .type = MATCH_FULL };
3887         struct ftrace_glob mod_g = { .type = MATCH_FULL };
3888         struct ftrace_glob *mod_match = (mod) ? &mod_g : NULL;
3889         int exclude_mod = 0;
3890         int found = 0;
3891         int ret;
3892         int clear_filter = 0;
3893
3894         if (func) {
3895                 func_g.type = filter_parse_regex(func, len, &func_g.search,
3896                                                  &clear_filter);
3897                 func_g.len = strlen(func_g.search);
3898         }
3899
3900         if (mod) {
3901                 mod_g.type = filter_parse_regex(mod, strlen(mod),
3902                                 &mod_g.search, &exclude_mod);
3903                 mod_g.len = strlen(mod_g.search);
3904         }
3905
3906         mutex_lock(&ftrace_lock);
3907
3908         if (unlikely(ftrace_disabled))
3909                 goto out_unlock;
3910
3911         do_for_each_ftrace_rec(pg, rec) {
3912
3913                 if (rec->flags & FTRACE_FL_DISABLED)
3914                         continue;
3915
3916                 if (ftrace_match_record(rec, &func_g, mod_match, exclude_mod)) {
3917                         ret = enter_record(hash, rec, clear_filter);
3918                         if (ret < 0) {
3919                                 found = ret;
3920                                 goto out_unlock;
3921                         }
3922                         found = 1;
3923                 }
3924         } while_for_each_ftrace_rec();
3925  out_unlock:
3926         mutex_unlock(&ftrace_lock);
3927
3928         return found;
3929 }
3930
3931 static int
3932 ftrace_match_records(struct ftrace_hash *hash, char *buff, int len)
3933 {
3934         return match_records(hash, buff, len, NULL);
3935 }
3936
3937 static void ftrace_ops_update_code(struct ftrace_ops *ops,
3938                                    struct ftrace_ops_hash *old_hash)
3939 {
3940         struct ftrace_ops *op;
3941
3942         if (!ftrace_enabled)
3943                 return;
3944
3945         if (ops->flags & FTRACE_OPS_FL_ENABLED) {
3946                 ftrace_run_modify_code(ops, FTRACE_UPDATE_CALLS, old_hash);
3947                 return;
3948         }
3949
3950         /*
3951          * If this is the shared global_ops filter, then we need to
3952          * check if there is another ops that shares it, is enabled.
3953          * If so, we still need to run the modify code.
3954          */
3955         if (ops->func_hash != &global_ops.local_hash)
3956                 return;
3957
3958         do_for_each_ftrace_op(op, ftrace_ops_list) {
3959                 if (op->func_hash == &global_ops.local_hash &&
3960                     op->flags & FTRACE_OPS_FL_ENABLED) {
3961                         ftrace_run_modify_code(op, FTRACE_UPDATE_CALLS, old_hash);
3962                         /* Only need to do this once */
3963                         return;
3964                 }
3965         } while_for_each_ftrace_op(op);
3966 }
3967
3968 static int ftrace_hash_move_and_update_ops(struct ftrace_ops *ops,
3969                                            struct ftrace_hash **orig_hash,
3970                                            struct ftrace_hash *hash,
3971                                            int enable)
3972 {
3973         struct ftrace_ops_hash old_hash_ops;
3974         struct ftrace_hash *old_hash;
3975         int ret;
3976
3977         old_hash = *orig_hash;
3978         old_hash_ops.filter_hash = ops->func_hash->filter_hash;
3979         old_hash_ops.notrace_hash = ops->func_hash->notrace_hash;
3980         ret = ftrace_hash_move(ops, enable, orig_hash, hash);
3981         if (!ret) {
3982                 ftrace_ops_update_code(ops, &old_hash_ops);
3983                 free_ftrace_hash_rcu(old_hash);
3984         }
3985         return ret;
3986 }
3987
3988 static bool module_exists(const char *module)
3989 {
3990         /* All modules have the symbol __this_module */
3991         const char this_mod[] = "__this_module";
3992         const int modname_size = MAX_PARAM_PREFIX_LEN + sizeof(this_mod) + 1;
3993         char modname[modname_size + 1];
3994         unsigned long val;
3995         int n;
3996
3997         n = snprintf(modname, modname_size + 1, "%s:%s", module, this_mod);
3998
3999         if (n > modname_size)
4000                 return false;
4001
4002         val = module_kallsyms_lookup_name(modname);
4003         return val != 0;
4004 }
4005
4006 static int cache_mod(struct trace_array *tr,
4007                      const char *func, char *module, int enable)
4008 {
4009         struct ftrace_mod_load *ftrace_mod, *n;
4010         struct list_head *head = enable ? &tr->mod_trace : &tr->mod_notrace;
4011         int ret;
4012
4013         mutex_lock(&ftrace_lock);
4014
4015         /* We do not cache inverse filters */
4016         if (func[0] == '!') {
4017                 func++;
4018                 ret = -EINVAL;
4019
4020                 /* Look to remove this hash */
4021                 list_for_each_entry_safe(ftrace_mod, n, head, list) {
4022                         if (strcmp(ftrace_mod->module, module) != 0)
4023                                 continue;
4024
4025                         /* no func matches all */
4026                         if (strcmp(func, "*") == 0 ||
4027                             (ftrace_mod->func &&
4028                              strcmp(ftrace_mod->func, func) == 0)) {
4029                                 ret = 0;
4030                                 free_ftrace_mod(ftrace_mod);
4031                                 continue;
4032                         }
4033                 }
4034                 goto out;
4035         }
4036
4037         ret = -EINVAL;
4038         /* We only care about modules that have not been loaded yet */
4039         if (module_exists(module))
4040                 goto out;
4041
4042         /* Save this string off, and execute it when the module is loaded */
4043         ret = ftrace_add_mod(tr, func, module, enable);
4044  out:
4045         mutex_unlock(&ftrace_lock);
4046
4047         return ret;
4048 }
4049
4050 static int
4051 ftrace_set_regex(struct ftrace_ops *ops, unsigned char *buf, int len,
4052                  int reset, int enable);
4053
4054 #ifdef CONFIG_MODULES
4055 static void process_mod_list(struct list_head *head, struct ftrace_ops *ops,
4056                              char *mod, bool enable)
4057 {
4058         struct ftrace_mod_load *ftrace_mod, *n;
4059         struct ftrace_hash **orig_hash, *new_hash;
4060         LIST_HEAD(process_mods);
4061         char *func;
4062         int ret;
4063
4064         mutex_lock(&ops->func_hash->regex_lock);
4065
4066         if (enable)
4067                 orig_hash = &ops->func_hash->filter_hash;
4068         else
4069                 orig_hash = &ops->func_hash->notrace_hash;
4070
4071         new_hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS,
4072                                               *orig_hash);
4073         if (!new_hash)
4074                 goto out; /* warn? */
4075
4076         mutex_lock(&ftrace_lock);
4077
4078         list_for_each_entry_safe(ftrace_mod, n, head, list) {
4079
4080                 if (strcmp(ftrace_mod->module, mod) != 0)
4081                         continue;
4082
4083                 if (ftrace_mod->func)
4084                         func = kstrdup(ftrace_mod->func, GFP_KERNEL);
4085                 else
4086                         func = kstrdup("*", GFP_KERNEL);
4087
4088                 if (!func) /* warn? */
4089                         continue;
4090
4091                 list_del(&ftrace_mod->list);
4092                 list_add(&ftrace_mod->list, &process_mods);
4093
4094                 /* Use the newly allocated func, as it may be "*" */
4095                 kfree(ftrace_mod->func);
4096                 ftrace_mod->func = func;
4097         }
4098
4099         mutex_unlock(&ftrace_lock);
4100
4101         list_for_each_entry_safe(ftrace_mod, n, &process_mods, list) {
4102
4103                 func = ftrace_mod->func;
4104
4105                 /* Grabs ftrace_lock, which is why we have this extra step */
4106                 match_records(new_hash, func, strlen(func), mod);
4107                 free_ftrace_mod(ftrace_mod);
4108         }
4109
4110         if (enable && list_empty(head))
4111                 new_hash->flags &= ~FTRACE_HASH_FL_MOD;
4112
4113         mutex_lock(&ftrace_lock);
4114
4115         ret = ftrace_hash_move_and_update_ops(ops, orig_hash,
4116                                               new_hash, enable);
4117         mutex_unlock(&ftrace_lock);
4118
4119  out:
4120         mutex_unlock(&ops->func_hash->regex_lock);
4121
4122         free_ftrace_hash(new_hash);
4123 }
4124
4125 static void process_cached_mods(const char *mod_name)
4126 {
4127         struct trace_array *tr;
4128         char *mod;
4129
4130         mod = kstrdup(mod_name, GFP_KERNEL);
4131         if (!mod)
4132                 return;
4133
4134         mutex_lock(&trace_types_lock);
4135         list_for_each_entry(tr, &ftrace_trace_arrays, list) {
4136                 if (!list_empty(&tr->mod_trace))
4137                         process_mod_list(&tr->mod_trace, tr->ops, mod, true);
4138                 if (!list_empty(&tr->mod_notrace))
4139                         process_mod_list(&tr->mod_notrace, tr->ops, mod, false);
4140         }
4141         mutex_unlock(&trace_types_lock);
4142
4143         kfree(mod);
4144 }
4145 #endif
4146
4147 /*
4148  * We register the module command as a template to show others how
4149  * to register the a command as well.
4150  */
4151
4152 static int
4153 ftrace_mod_callback(struct trace_array *tr, struct ftrace_hash *hash,
4154                     char *func_orig, char *cmd, char *module, int enable)
4155 {
4156         char *func;
4157         int ret;
4158
4159         /* match_records() modifies func, and we need the original */
4160         func = kstrdup(func_orig, GFP_KERNEL);
4161         if (!func)
4162                 return -ENOMEM;
4163
4164         /*
4165          * cmd == 'mod' because we only registered this func
4166          * for the 'mod' ftrace_func_command.
4167          * But if you register one func with multiple commands,
4168          * you can tell which command was used by the cmd
4169          * parameter.
4170          */
4171         ret = match_records(hash, func, strlen(func), module);
4172         kfree(func);
4173
4174         if (!ret)
4175                 return cache_mod(tr, func_orig, module, enable);
4176         if (ret < 0)
4177                 return ret;
4178         return 0;
4179 }
4180
4181 static struct ftrace_func_command ftrace_mod_cmd = {
4182         .name                   = "mod",
4183         .func                   = ftrace_mod_callback,
4184 };
4185
4186 static int __init ftrace_mod_cmd_init(void)
4187 {
4188         return register_ftrace_command(&ftrace_mod_cmd);
4189 }
4190 core_initcall(ftrace_mod_cmd_init);
4191
4192 static void function_trace_probe_call(unsigned long ip, unsigned long parent_ip,
4193                                       struct ftrace_ops *op, struct pt_regs *pt_regs)
4194 {
4195         struct ftrace_probe_ops *probe_ops;
4196         struct ftrace_func_probe *probe;
4197
4198         probe = container_of(op, struct ftrace_func_probe, ops);
4199         probe_ops = probe->probe_ops;
4200
4201         /*
4202          * Disable preemption for these calls to prevent a RCU grace
4203          * period. This syncs the hash iteration and freeing of items
4204          * on the hash. rcu_read_lock is too dangerous here.
4205          */
4206         preempt_disable_notrace();
4207         probe_ops->func(ip, parent_ip, probe->tr, probe_ops, probe->data);
4208         preempt_enable_notrace();
4209 }
4210
4211 struct ftrace_func_map {
4212         struct ftrace_func_entry        entry;
4213         void                            *data;
4214 };
4215
4216 struct ftrace_func_mapper {
4217         struct ftrace_hash              hash;
4218 };
4219
4220 /**
4221  * allocate_ftrace_func_mapper - allocate a new ftrace_func_mapper
4222  *
4223  * Returns a ftrace_func_mapper descriptor that can be used to map ips to data.
4224  */
4225 struct ftrace_func_mapper *allocate_ftrace_func_mapper(void)
4226 {
4227         struct ftrace_hash *hash;
4228
4229         /*
4230          * The mapper is simply a ftrace_hash, but since the entries
4231          * in the hash are not ftrace_func_entry type, we define it
4232          * as a separate structure.
4233          */
4234         hash = alloc_ftrace_hash(FTRACE_HASH_DEFAULT_BITS);
4235         return (struct ftrace_func_mapper *)hash;
4236 }
4237
4238 /**
4239  * ftrace_func_mapper_find_ip - Find some data mapped to an ip
4240  * @mapper: The mapper that has the ip maps
4241  * @ip: the instruction pointer to find the data for
4242  *
4243  * Returns the data mapped to @ip if found otherwise NULL. The return
4244  * is actually the address of the mapper data pointer. The address is
4245  * returned for use cases where the data is no bigger than a long, and
4246  * the user can use the data pointer as its data instead of having to
4247  * allocate more memory for the reference.
4248  */
4249 void **ftrace_func_mapper_find_ip(struct ftrace_func_mapper *mapper,
4250                                   unsigned long ip)
4251 {
4252         struct ftrace_func_entry *entry;
4253         struct ftrace_func_map *map;
4254
4255         entry = ftrace_lookup_ip(&mapper->hash, ip);
4256         if (!entry)
4257                 return NULL;
4258
4259         map = (struct ftrace_func_map *)entry;
4260         return &map->data;
4261 }
4262
4263 /**
4264  * ftrace_func_mapper_add_ip - Map some data to an ip
4265  * @mapper: The mapper that has the ip maps
4266  * @ip: The instruction pointer address to map @data to
4267  * @data: The data to map to @ip
4268  *
4269  * Returns 0 on succes otherwise an error.
4270  */
4271 int ftrace_func_mapper_add_ip(struct ftrace_func_mapper *mapper,
4272                               unsigned long ip, void *data)
4273 {
4274         struct ftrace_func_entry *entry;
4275         struct ftrace_func_map *map;
4276
4277         entry = ftrace_lookup_ip(&mapper->hash, ip);
4278         if (entry)
4279                 return -EBUSY;
4280
4281         map = kmalloc(sizeof(*map), GFP_KERNEL);
4282         if (!map)
4283                 return -ENOMEM;
4284
4285         map->entry.ip = ip;
4286         map->data = data;
4287
4288         __add_hash_entry(&mapper->hash, &map->entry);
4289
4290         return 0;
4291 }
4292
4293 /**
4294  * ftrace_func_mapper_remove_ip - Remove an ip from the mapping
4295  * @mapper: The mapper that has the ip maps
4296  * @ip: The instruction pointer address to remove the data from
4297  *
4298  * Returns the data if it is found, otherwise NULL.
4299  * Note, if the data pointer is used as the data itself, (see 
4300  * ftrace_func_mapper_find_ip(), then the return value may be meaningless,
4301  * if the data pointer was set to zero.
4302  */
4303 void *ftrace_func_mapper_remove_ip(struct ftrace_func_mapper *mapper,
4304                                    unsigned long ip)
4305 {
4306         struct ftrace_func_entry *entry;
4307         struct ftrace_func_map *map;
4308         void *data;
4309
4310         entry = ftrace_lookup_ip(&mapper->hash, ip);
4311         if (!entry)
4312                 return NULL;
4313
4314         map = (struct ftrace_func_map *)entry;
4315         data = map->data;
4316
4317         remove_hash_entry(&mapper->hash, entry);
4318         kfree(entry);
4319
4320         return data;
4321 }
4322
4323 /**
4324  * free_ftrace_func_mapper - free a mapping of ips and data
4325  * @mapper: The mapper that has the ip maps
4326  * @free_func: A function to be called on each data item.
4327  *
4328  * This is used to free the function mapper. The @free_func is optional
4329  * and can be used if the data needs to be freed as well.
4330  */
4331 void free_ftrace_func_mapper(struct ftrace_func_mapper *mapper,
4332                              ftrace_mapper_func free_func)
4333 {
4334         struct ftrace_func_entry *entry;
4335         struct ftrace_func_map *map;
4336         struct hlist_head *hhd;
4337         int size, i;
4338
4339         if (!mapper)
4340                 return;
4341
4342         if (free_func && mapper->hash.count) {
4343                 size = 1 << mapper->hash.size_bits;
4344                 for (i = 0; i < size; i++) {
4345                         hhd = &mapper->hash.buckets[i];
4346                         hlist_for_each_entry(entry, hhd, hlist) {
4347                                 map = (struct ftrace_func_map *)entry;
4348                                 free_func(map);
4349                         }
4350                 }
4351         }
4352         free_ftrace_hash(&mapper->hash);
4353 }
4354
4355 static void release_probe(struct ftrace_func_probe *probe)
4356 {
4357         struct ftrace_probe_ops *probe_ops;
4358
4359         mutex_lock(&ftrace_lock);
4360
4361         WARN_ON(probe->ref <= 0);
4362
4363         /* Subtract the ref that was used to protect this instance */
4364         probe->ref--;
4365
4366         if (!probe->ref) {
4367                 probe_ops = probe->probe_ops;
4368                 /*
4369                  * Sending zero as ip tells probe_ops to free
4370                  * the probe->data itself
4371                  */
4372                 if (probe_ops->free)
4373                         probe_ops->free(probe_ops, probe->tr, 0, probe->data);
4374                 list_del(&probe->list);
4375                 kfree(probe);
4376         }
4377         mutex_unlock(&ftrace_lock);
4378 }
4379
4380 static void acquire_probe_locked(struct ftrace_func_probe *probe)
4381 {
4382         /*
4383          * Add one ref to keep it from being freed when releasing the
4384          * ftrace_lock mutex.
4385          */
4386         probe->ref++;
4387 }
4388
4389 int
4390 register_ftrace_function_probe(char *glob, struct trace_array *tr,
4391                                struct ftrace_probe_ops *probe_ops,
4392                                void *data)
4393 {
4394         struct ftrace_func_entry *entry;
4395         struct ftrace_func_probe *probe;
4396         struct ftrace_hash **orig_hash;
4397         struct ftrace_hash *old_hash;
4398         struct ftrace_hash *hash;
4399         int count = 0;
4400         int size;
4401         int ret;
4402         int i;
4403
4404         if (WARN_ON(!tr))
4405                 return -EINVAL;
4406
4407         /* We do not support '!' for function probes */
4408         if (WARN_ON(glob[0] == '!'))
4409                 return -EINVAL;
4410
4411
4412         mutex_lock(&ftrace_lock);
4413         /* Check if the probe_ops is already registered */
4414         list_for_each_entry(probe, &tr->func_probes, list) {
4415                 if (probe->probe_ops == probe_ops)
4416                         break;
4417         }
4418         if (&probe->list == &tr->func_probes) {
4419                 probe = kzalloc(sizeof(*probe), GFP_KERNEL);
4420                 if (!probe) {
4421                         mutex_unlock(&ftrace_lock);
4422                         return -ENOMEM;
4423                 }
4424                 probe->probe_ops = probe_ops;
4425                 probe->ops.func = function_trace_probe_call;
4426                 probe->tr = tr;
4427                 ftrace_ops_init(&probe->ops);
4428                 list_add(&probe->list, &tr->func_probes);
4429         }
4430
4431         acquire_probe_locked(probe);
4432
4433         mutex_unlock(&ftrace_lock);
4434
4435         /*
4436          * Note, there's a small window here that the func_hash->filter_hash
4437          * may be NULL or empty. Need to be carefule when reading the loop.
4438          */
4439         mutex_lock(&probe->ops.func_hash->regex_lock);
4440
4441         orig_hash = &probe->ops.func_hash->filter_hash;
4442         old_hash = *orig_hash;
4443         hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, old_hash);
4444
4445         if (!hash) {
4446                 ret = -ENOMEM;
4447                 goto out;
4448         }
4449
4450         ret = ftrace_match_records(hash, glob, strlen(glob));
4451
4452         /* Nothing found? */
4453         if (!ret)
4454                 ret = -EINVAL;
4455
4456         if (ret < 0)
4457                 goto out;
4458
4459         size = 1 << hash->size_bits;
4460         for (i = 0; i < size; i++) {
4461                 hlist_for_each_entry(entry, &hash->buckets[i], hlist) {
4462                         if (ftrace_lookup_ip(old_hash, entry->ip))
4463                                 continue;
4464                         /*
4465                          * The caller might want to do something special
4466                          * for each function we find. We call the callback
4467                          * to give the caller an opportunity to do so.
4468                          */
4469                         if (probe_ops->init) {
4470                                 ret = probe_ops->init(probe_ops, tr,
4471                                                       entry->ip, data,
4472                                                       &probe->data);
4473                                 if (ret < 0) {
4474                                         if (probe_ops->free && count)
4475                                                 probe_ops->free(probe_ops, tr,
4476                                                                 0, probe->data);
4477                                         probe->data = NULL;
4478                                         goto out;
4479                                 }
4480                         }
4481                         count++;
4482                 }
4483         }
4484
4485         mutex_lock(&ftrace_lock);
4486
4487         if (!count) {
4488                 /* Nothing was added? */
4489                 ret = -EINVAL;
4490                 goto out_unlock;
4491         }
4492
4493         ret = ftrace_hash_move_and_update_ops(&probe->ops, orig_hash,
4494                                               hash, 1);
4495         if (ret < 0)
4496                 goto err_unlock;
4497
4498         /* One ref for each new function traced */
4499         probe->ref += count;
4500
4501         if (!(probe->ops.flags & FTRACE_OPS_FL_ENABLED))
4502                 ret = ftrace_startup(&probe->ops, 0);
4503
4504  out_unlock:
4505         mutex_unlock(&ftrace_lock);
4506
4507         if (!ret)
4508                 ret = count;
4509  out:
4510         mutex_unlock(&probe->ops.func_hash->regex_lock);
4511         free_ftrace_hash(hash);
4512
4513         release_probe(probe);
4514
4515         return ret;
4516
4517  err_unlock:
4518         if (!probe_ops->free || !count)
4519                 goto out_unlock;
4520
4521         /* Failed to do the move, need to call the free functions */
4522         for (i = 0; i < size; i++) {
4523                 hlist_for_each_entry(entry, &hash->buckets[i], hlist) {
4524                         if (ftrace_lookup_ip(old_hash, entry->ip))
4525                                 continue;
4526                         probe_ops->free(probe_ops, tr, entry->ip, probe->data);
4527                 }
4528         }
4529         goto out_unlock;
4530 }
4531
4532 int
4533 unregister_ftrace_function_probe_func(char *glob, struct trace_array *tr,
4534                                       struct ftrace_probe_ops *probe_ops)
4535 {
4536         struct ftrace_ops_hash old_hash_ops;
4537         struct ftrace_func_entry *entry;
4538         struct ftrace_func_probe *probe;
4539         struct ftrace_glob func_g;
4540         struct ftrace_hash **orig_hash;
4541         struct ftrace_hash *old_hash;
4542         struct ftrace_hash *hash = NULL;
4543         struct hlist_node *tmp;
4544         struct hlist_head hhd;
4545         char str[KSYM_SYMBOL_LEN];
4546         int count = 0;
4547         int i, ret = -ENODEV;
4548         int size;
4549
4550         if (!glob || !strlen(glob) || !strcmp(glob, "*"))
4551                 func_g.search = NULL;
4552         else {
4553                 int not;
4554
4555                 func_g.type = filter_parse_regex(glob, strlen(glob),
4556                                                  &func_g.search, &not);
4557                 func_g.len = strlen(func_g.search);
4558
4559                 /* we do not support '!' for function probes */
4560                 if (WARN_ON(not))
4561                         return -EINVAL;
4562         }
4563
4564         mutex_lock(&ftrace_lock);
4565         /* Check if the probe_ops is already registered */
4566         list_for_each_entry(probe, &tr->func_probes, list) {
4567                 if (probe->probe_ops == probe_ops)
4568                         break;
4569         }
4570         if (&probe->list == &tr->func_probes)
4571                 goto err_unlock_ftrace;
4572
4573         ret = -EINVAL;
4574         if (!(probe->ops.flags & FTRACE_OPS_FL_INITIALIZED))
4575                 goto err_unlock_ftrace;
4576
4577         acquire_probe_locked(probe);
4578
4579         mutex_unlock(&ftrace_lock);
4580
4581         mutex_lock(&probe->ops.func_hash->regex_lock);
4582
4583         orig_hash = &probe->ops.func_hash->filter_hash;
4584         old_hash = *orig_hash;
4585
4586         if (ftrace_hash_empty(old_hash))
4587                 goto out_unlock;
4588
4589         old_hash_ops.filter_hash = old_hash;
4590         /* Probes only have filters */
4591         old_hash_ops.notrace_hash = NULL;
4592
4593         ret = -ENOMEM;
4594         hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, old_hash);
4595         if (!hash)
4596                 goto out_unlock;
4597
4598         INIT_HLIST_HEAD(&hhd);
4599
4600         size = 1 << hash->size_bits;
4601         for (i = 0; i < size; i++) {
4602                 hlist_for_each_entry_safe(entry, tmp, &hash->buckets[i], hlist) {
4603
4604                         if (func_g.search) {
4605                                 kallsyms_lookup(entry->ip, NULL, NULL,
4606                                                 NULL, str);
4607                                 if (!ftrace_match(str, &func_g))
4608                                         continue;
4609                         }
4610                         count++;
4611                         remove_hash_entry(hash, entry);
4612                         hlist_add_head(&entry->hlist, &hhd);
4613                 }
4614         }
4615
4616         /* Nothing found? */
4617         if (!count) {
4618                 ret = -EINVAL;
4619                 goto out_unlock;
4620         }
4621
4622         mutex_lock(&ftrace_lock);
4623
4624         WARN_ON(probe->ref < count);
4625
4626         probe->ref -= count;
4627
4628         if (ftrace_hash_empty(hash))
4629                 ftrace_shutdown(&probe->ops, 0);
4630
4631         ret = ftrace_hash_move_and_update_ops(&probe->ops, orig_hash,
4632                                               hash, 1);
4633
4634         /* still need to update the function call sites */
4635         if (ftrace_enabled && !ftrace_hash_empty(hash))
4636                 ftrace_run_modify_code(&probe->ops, FTRACE_UPDATE_CALLS,
4637                                        &old_hash_ops);
4638         synchronize_sched();
4639
4640         hlist_for_each_entry_safe(entry, tmp, &hhd, hlist) {
4641                 hlist_del(&entry->hlist);
4642                 if (probe_ops->free)
4643                         probe_ops->free(probe_ops, tr, entry->ip, probe->data);
4644                 kfree(entry);
4645         }
4646         mutex_unlock(&ftrace_lock);
4647
4648  out_unlock:
4649         mutex_unlock(&probe->ops.func_hash->regex_lock);
4650         free_ftrace_hash(hash);
4651
4652         release_probe(probe);
4653
4654         return ret;
4655
4656  err_unlock_ftrace:
4657         mutex_unlock(&ftrace_lock);
4658         return ret;
4659 }
4660
4661 void clear_ftrace_function_probes(struct trace_array *tr)
4662 {
4663         struct ftrace_func_probe *probe, *n;
4664
4665         list_for_each_entry_safe(probe, n, &tr->func_probes, list)
4666                 unregister_ftrace_function_probe_func(NULL, tr, probe->probe_ops);
4667 }
4668
4669 static LIST_HEAD(ftrace_commands);
4670 static DEFINE_MUTEX(ftrace_cmd_mutex);
4671
4672 /*
4673  * Currently we only register ftrace commands from __init, so mark this
4674  * __init too.
4675  */
4676 __init int register_ftrace_command(struct ftrace_func_command *cmd)
4677 {
4678         struct ftrace_func_command *p;
4679         int ret = 0;
4680
4681         mutex_lock(&ftrace_cmd_mutex);
4682         list_for_each_entry(p, &ftrace_commands, list) {
4683                 if (strcmp(cmd->name, p->name) == 0) {
4684                         ret = -EBUSY;
4685                         goto out_unlock;
4686                 }
4687         }
4688         list_add(&cmd->list, &ftrace_commands);
4689  out_unlock:
4690         mutex_unlock(&ftrace_cmd_mutex);
4691
4692         return ret;
4693 }
4694
4695 /*
4696  * Currently we only unregister ftrace commands from __init, so mark
4697  * this __init too.
4698  */
4699 __init int unregister_ftrace_command(struct ftrace_func_command *cmd)
4700 {
4701         struct ftrace_func_command *p, *n;
4702         int ret = -ENODEV;
4703
4704         mutex_lock(&ftrace_cmd_mutex);
4705         list_for_each_entry_safe(p, n, &ftrace_commands, list) {
4706                 if (strcmp(cmd->name, p->name) == 0) {
4707                         ret = 0;
4708                         list_del_init(&p->list);
4709                         goto out_unlock;
4710                 }
4711         }
4712  out_unlock:
4713         mutex_unlock(&ftrace_cmd_mutex);
4714
4715         return ret;
4716 }
4717
4718 static int ftrace_process_regex(struct ftrace_iterator *iter,
4719                                 char *buff, int len, int enable)
4720 {
4721         struct ftrace_hash *hash = iter->hash;
4722         struct trace_array *tr = iter->ops->private;
4723         char *func, *command, *next = buff;
4724         struct ftrace_func_command *p;
4725         int ret = -EINVAL;
4726
4727         func = strsep(&next, ":");
4728
4729         if (!next) {
4730                 ret = ftrace_match_records(hash, func, len);
4731                 if (!ret)
4732                         ret = -EINVAL;
4733                 if (ret < 0)
4734                         return ret;
4735                 return 0;
4736         }
4737
4738         /* command found */
4739
4740         command = strsep(&next, ":");
4741
4742         mutex_lock(&ftrace_cmd_mutex);
4743         list_for_each_entry(p, &ftrace_commands, list) {
4744                 if (strcmp(p->name, command) == 0) {
4745                         ret = p->func(tr, hash, func, command, next, enable);
4746                         goto out_unlock;
4747                 }
4748         }
4749  out_unlock:
4750         mutex_unlock(&ftrace_cmd_mutex);
4751
4752         return ret;
4753 }
4754
4755 static ssize_t
4756 ftrace_regex_write(struct file *file, const char __user *ubuf,
4757                    size_t cnt, loff_t *ppos, int enable)
4758 {
4759         struct ftrace_iterator *iter;
4760         struct trace_parser *parser;
4761         ssize_t ret, read;
4762
4763         if (!cnt)
4764                 return 0;
4765
4766         if (file->f_mode & FMODE_READ) {
4767                 struct seq_file *m = file->private_data;
4768                 iter = m->private;
4769         } else
4770                 iter = file->private_data;
4771
4772         if (unlikely(ftrace_disabled))
4773                 return -ENODEV;
4774
4775         /* iter->hash is a local copy, so we don't need regex_lock */
4776
4777         parser = &iter->parser;
4778         read = trace_get_user(parser, ubuf, cnt, ppos);
4779
4780         if (read >= 0 && trace_parser_loaded(parser) &&
4781             !trace_parser_cont(parser)) {
4782                 ret = ftrace_process_regex(iter, parser->buffer,
4783                                            parser->idx, enable);
4784                 trace_parser_clear(parser);
4785                 if (ret < 0)
4786                         goto out;
4787         }
4788
4789         ret = read;
4790  out:
4791         return ret;
4792 }
4793
4794 ssize_t
4795 ftrace_filter_write(struct file *file, const char __user *ubuf,
4796                     size_t cnt, loff_t *ppos)
4797 {
4798         return ftrace_regex_write(file, ubuf, cnt, ppos, 1);
4799 }
4800
4801 ssize_t
4802 ftrace_notrace_write(struct file *file, const char __user *ubuf,
4803                      size_t cnt, loff_t *ppos)
4804 {
4805         return ftrace_regex_write(file, ubuf, cnt, ppos, 0);
4806 }
4807
4808 static int
4809 ftrace_match_addr(struct ftrace_hash *hash, unsigned long ip, int remove)
4810 {
4811         struct ftrace_func_entry *entry;
4812
4813         if (!ftrace_location(ip))
4814                 return -EINVAL;
4815
4816         if (remove) {
4817                 entry = ftrace_lookup_ip(hash, ip);
4818                 if (!entry)
4819                         return -ENOENT;
4820                 free_hash_entry(hash, entry);
4821                 return 0;
4822         }
4823
4824         return add_hash_entry(hash, ip);
4825 }
4826
4827 static int
4828 ftrace_set_hash(struct ftrace_ops *ops, unsigned char *buf, int len,
4829                 unsigned long ip, int remove, int reset, int enable)
4830 {
4831         struct ftrace_hash **orig_hash;
4832         struct ftrace_hash *hash;
4833         int ret;
4834
4835         if (unlikely(ftrace_disabled))
4836                 return -ENODEV;
4837
4838         mutex_lock(&ops->func_hash->regex_lock);
4839
4840         if (enable)
4841                 orig_hash = &ops->func_hash->filter_hash;
4842         else
4843                 orig_hash = &ops->func_hash->notrace_hash;
4844
4845         if (reset)
4846                 hash = alloc_ftrace_hash(FTRACE_HASH_DEFAULT_BITS);
4847         else
4848                 hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, *orig_hash);
4849
4850         if (!hash) {
4851                 ret = -ENOMEM;
4852                 goto out_regex_unlock;
4853         }
4854
4855         if (buf && !ftrace_match_records(hash, buf, len)) {
4856                 ret = -EINVAL;
4857                 goto out_regex_unlock;
4858         }
4859         if (ip) {
4860                 ret = ftrace_match_addr(hash, ip, remove);
4861                 if (ret < 0)
4862                         goto out_regex_unlock;
4863         }
4864
4865         mutex_lock(&ftrace_lock);
4866         ret = ftrace_hash_move_and_update_ops(ops, orig_hash, hash, enable);
4867         mutex_unlock(&ftrace_lock);
4868
4869  out_regex_unlock:
4870         mutex_unlock(&ops->func_hash->regex_lock);
4871
4872         free_ftrace_hash(hash);
4873         return ret;
4874 }
4875
4876 static int
4877 ftrace_set_addr(struct ftrace_ops *ops, unsigned long ip, int remove,
4878                 int reset, int enable)
4879 {
4880         return ftrace_set_hash(ops, 0, 0, ip, remove, reset, enable);
4881 }
4882
4883 /**
4884  * ftrace_set_filter_ip - set a function to filter on in ftrace by address
4885  * @ops - the ops to set the filter with
4886  * @ip - the address to add to or remove from the filter.
4887  * @remove - non zero to remove the ip from the filter
4888  * @reset - non zero to reset all filters before applying this filter.
4889  *
4890  * Filters denote which functions should be enabled when tracing is enabled
4891  * If @ip is NULL, it failes to update filter.
4892  */
4893 int ftrace_set_filter_ip(struct ftrace_ops *ops, unsigned long ip,
4894                          int remove, int reset)
4895 {
4896         ftrace_ops_init(ops);
4897         return ftrace_set_addr(ops, ip, remove, reset, 1);
4898 }
4899 EXPORT_SYMBOL_GPL(ftrace_set_filter_ip);
4900
4901 /**
4902  * ftrace_ops_set_global_filter - setup ops to use global filters
4903  * @ops - the ops which will use the global filters
4904  *
4905  * ftrace users who need global function trace filtering should call this.
4906  * It can set the global filter only if ops were not initialized before.
4907  */
4908 void ftrace_ops_set_global_filter(struct ftrace_ops *ops)
4909 {
4910         if (ops->flags & FTRACE_OPS_FL_INITIALIZED)
4911                 return;
4912
4913         ftrace_ops_init(ops);
4914         ops->func_hash = &global_ops.local_hash;
4915 }
4916 EXPORT_SYMBOL_GPL(ftrace_ops_set_global_filter);
4917
4918 static int
4919 ftrace_set_regex(struct ftrace_ops *ops, unsigned char *buf, int len,
4920                  int reset, int enable)
4921 {
4922         return ftrace_set_hash(ops, buf, len, 0, 0, reset, enable);
4923 }
4924
4925 /**
4926  * ftrace_set_filter - set a function to filter on in ftrace
4927  * @ops - the ops to set the filter with
4928  * @buf - the string that holds the function filter text.
4929  * @len - the length of the string.
4930  * @reset - non zero to reset all filters before applying this filter.
4931  *
4932  * Filters denote which functions should be enabled when tracing is enabled.
4933  * If @buf is NULL and reset is set, all functions will be enabled for tracing.
4934  */
4935 int ftrace_set_filter(struct ftrace_ops *ops, unsigned char *buf,
4936                        int len, int reset)
4937 {
4938         ftrace_ops_init(ops);
4939         return ftrace_set_regex(ops, buf, len, reset, 1);
4940 }
4941 EXPORT_SYMBOL_GPL(ftrace_set_filter);
4942
4943 /**
4944  * ftrace_set_notrace - set a function to not trace in ftrace
4945  * @ops - the ops to set the notrace filter with
4946  * @buf - the string that holds the function notrace text.
4947  * @len - the length of the string.
4948  * @reset - non zero to reset all filters before applying this filter.
4949  *
4950  * Notrace Filters denote which functions should not be enabled when tracing
4951  * is enabled. If @buf is NULL and reset is set, all functions will be enabled
4952  * for tracing.
4953  */
4954 int ftrace_set_notrace(struct ftrace_ops *ops, unsigned char *buf,
4955                         int len, int reset)
4956 {
4957         ftrace_ops_init(ops);
4958         return ftrace_set_regex(ops, buf, len, reset, 0);
4959 }
4960 EXPORT_SYMBOL_GPL(ftrace_set_notrace);
4961 /**
4962  * ftrace_set_global_filter - set a function to filter on with global tracers
4963  * @buf - the string that holds the function filter text.
4964  * @len - the length of the string.
4965  * @reset - non zero to reset all filters before applying this filter.
4966  *
4967  * Filters denote which functions should be enabled when tracing is enabled.
4968  * If @buf is NULL and reset is set, all functions will be enabled for tracing.
4969  */
4970 void ftrace_set_global_filter(unsigned char *buf, int len, int reset)
4971 {
4972         ftrace_set_regex(&global_ops, buf, len, reset, 1);
4973 }
4974 EXPORT_SYMBOL_GPL(ftrace_set_global_filter);
4975
4976 /**
4977  * ftrace_set_global_notrace - set a function to not trace with global tracers
4978  * @buf - the string that holds the function notrace text.
4979  * @len - the length of the string.
4980  * @reset - non zero to reset all filters before applying this filter.
4981  *
4982  * Notrace Filters denote which functions should not be enabled when tracing
4983  * is enabled. If @buf is NULL and reset is set, all functions will be enabled
4984  * for tracing.
4985  */
4986 void ftrace_set_global_notrace(unsigned char *buf, int len, int reset)
4987 {
4988         ftrace_set_regex(&global_ops, buf, len, reset, 0);
4989 }
4990 EXPORT_SYMBOL_GPL(ftrace_set_global_notrace);
4991
4992 /*
4993  * command line interface to allow users to set filters on boot up.
4994  */
4995 #define FTRACE_FILTER_SIZE              COMMAND_LINE_SIZE
4996 static char ftrace_notrace_buf[FTRACE_FILTER_SIZE] __initdata;
4997 static char ftrace_filter_buf[FTRACE_FILTER_SIZE] __initdata;
4998
4999 /* Used by function selftest to not test if filter is set */
5000 bool ftrace_filter_param __initdata;
5001
5002 static int __init set_ftrace_notrace(char *str)
5003 {
5004         ftrace_filter_param = true;
5005         strlcpy(ftrace_notrace_buf, str, FTRACE_FILTER_SIZE);
5006         return 1;
5007 }
5008 __setup("ftrace_notrace=", set_ftrace_notrace);
5009
5010 static int __init set_ftrace_filter(char *str)
5011 {
5012         ftrace_filter_param = true;
5013         strlcpy(ftrace_filter_buf, str, FTRACE_FILTER_SIZE);
5014         return 1;
5015 }
5016 __setup("ftrace_filter=", set_ftrace_filter);
5017
5018 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
5019 static char ftrace_graph_buf[FTRACE_FILTER_SIZE] __initdata;
5020 static char ftrace_graph_notrace_buf[FTRACE_FILTER_SIZE] __initdata;
5021 static int ftrace_graph_set_hash(struct ftrace_hash *hash, char *buffer);
5022
5023 static int __init set_graph_function(char *str)
5024 {
5025         strlcpy(ftrace_graph_buf, str, FTRACE_FILTER_SIZE);
5026         return 1;
5027 }
5028 __setup("ftrace_graph_filter=", set_graph_function);
5029
5030 static int __init set_graph_notrace_function(char *str)
5031 {
5032         strlcpy(ftrace_graph_notrace_buf, str, FTRACE_FILTER_SIZE);
5033         return 1;
5034 }
5035 __setup("ftrace_graph_notrace=", set_graph_notrace_function);
5036
5037 static int __init set_graph_max_depth_function(char *str)
5038 {
5039         if (!str)
5040                 return 0;
5041         fgraph_max_depth = simple_strtoul(str, NULL, 0);
5042         return 1;
5043 }
5044 __setup("ftrace_graph_max_depth=", set_graph_max_depth_function);
5045
5046 static void __init set_ftrace_early_graph(char *buf, int enable)
5047 {
5048         int ret;
5049         char *func;
5050         struct ftrace_hash *hash;
5051
5052         hash = alloc_ftrace_hash(FTRACE_HASH_DEFAULT_BITS);
5053         if (WARN_ON(!hash))
5054                 return;
5055
5056         while (buf) {
5057                 func = strsep(&buf, ",");
5058                 /* we allow only one expression at a time */
5059                 ret = ftrace_graph_set_hash(hash, func);
5060                 if (ret)
5061                         printk(KERN_DEBUG "ftrace: function %s not "
5062                                           "traceable\n", func);
5063         }
5064
5065         if (enable)
5066                 ftrace_graph_hash = hash;
5067         else
5068                 ftrace_graph_notrace_hash = hash;
5069 }
5070 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
5071
5072 void __init
5073 ftrace_set_early_filter(struct ftrace_ops *ops, char *buf, int enable)
5074 {
5075         char *func;
5076
5077         ftrace_ops_init(ops);
5078
5079         while (buf) {
5080                 func = strsep(&buf, ",");
5081                 ftrace_set_regex(ops, func, strlen(func), 0, enable);
5082         }
5083 }
5084
5085 static void __init set_ftrace_early_filters(void)
5086 {
5087         if (ftrace_filter_buf[0])
5088                 ftrace_set_early_filter(&global_ops, ftrace_filter_buf, 1);
5089         if (ftrace_notrace_buf[0])
5090                 ftrace_set_early_filter(&global_ops, ftrace_notrace_buf, 0);
5091 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
5092         if (ftrace_graph_buf[0])
5093                 set_ftrace_early_graph(ftrace_graph_buf, 1);
5094         if (ftrace_graph_notrace_buf[0])
5095                 set_ftrace_early_graph(ftrace_graph_notrace_buf, 0);
5096 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
5097 }
5098
5099 int ftrace_regex_release(struct inode *inode, struct file *file)
5100 {
5101         struct seq_file *m = (struct seq_file *)file->private_data;
5102         struct ftrace_iterator *iter;
5103         struct ftrace_hash **orig_hash;
5104         struct trace_parser *parser;
5105         int filter_hash;
5106         int ret;
5107
5108         if (file->f_mode & FMODE_READ) {
5109                 iter = m->private;
5110                 seq_release(inode, file);
5111         } else
5112                 iter = file->private_data;
5113
5114         parser = &iter->parser;
5115         if (trace_parser_loaded(parser)) {
5116                 int enable = !(iter->flags & FTRACE_ITER_NOTRACE);
5117
5118                 parser->buffer[parser->idx] = 0;
5119                 ftrace_process_regex(iter, parser->buffer,
5120                                      parser->idx, enable);
5121         }
5122
5123         trace_parser_put(parser);
5124
5125         mutex_lock(&iter->ops->func_hash->regex_lock);
5126
5127         if (file->f_mode & FMODE_WRITE) {
5128                 filter_hash = !!(iter->flags & FTRACE_ITER_FILTER);
5129
5130                 if (filter_hash) {
5131                         orig_hash = &iter->ops->func_hash->filter_hash;
5132                         if (iter->tr) {
5133                                 if (list_empty(&iter->tr->mod_trace))
5134                                         iter->hash->flags &= ~FTRACE_HASH_FL_MOD;
5135                                 else
5136                                         iter->hash->flags |= FTRACE_HASH_FL_MOD;
5137                         }
5138                 } else
5139                         orig_hash = &iter->ops->func_hash->notrace_hash;
5140
5141                 mutex_lock(&ftrace_lock);
5142                 ret = ftrace_hash_move_and_update_ops(iter->ops, orig_hash,
5143                                                       iter->hash, filter_hash);
5144                 mutex_unlock(&ftrace_lock);
5145         } else {
5146                 /* For read only, the hash is the ops hash */
5147                 iter->hash = NULL;
5148         }
5149
5150         mutex_unlock(&iter->ops->func_hash->regex_lock);
5151         free_ftrace_hash(iter->hash);
5152         if (iter->tr)
5153                 trace_array_put(iter->tr);
5154         kfree(iter);
5155
5156         return 0;
5157 }
5158
5159 static const struct file_operations ftrace_avail_fops = {
5160         .open = ftrace_avail_open,
5161         .read = seq_read,
5162         .llseek = seq_lseek,
5163         .release = seq_release_private,
5164 };
5165
5166 static const struct file_operations ftrace_enabled_fops = {
5167         .open = ftrace_enabled_open,
5168         .read = seq_read,
5169         .llseek = seq_lseek,
5170         .release = seq_release_private,
5171 };
5172
5173 static const struct file_operations ftrace_filter_fops = {
5174         .open = ftrace_filter_open,
5175         .read = seq_read,
5176         .write = ftrace_filter_write,
5177         .llseek = tracing_lseek,
5178         .release = ftrace_regex_release,
5179 };
5180
5181 static const struct file_operations ftrace_notrace_fops = {
5182         .open = ftrace_notrace_open,
5183         .read = seq_read,
5184         .write = ftrace_notrace_write,
5185         .llseek = tracing_lseek,
5186         .release = ftrace_regex_release,
5187 };
5188
5189 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
5190
5191 static DEFINE_MUTEX(graph_lock);
5192
5193 struct ftrace_hash __rcu *ftrace_graph_hash = EMPTY_HASH;
5194 struct ftrace_hash __rcu *ftrace_graph_notrace_hash = EMPTY_HASH;
5195
5196 enum graph_filter_type {
5197         GRAPH_FILTER_NOTRACE    = 0,
5198         GRAPH_FILTER_FUNCTION,
5199 };
5200
5201 #define FTRACE_GRAPH_EMPTY      ((void *)1)
5202
5203 struct ftrace_graph_data {
5204         struct ftrace_hash              *hash;
5205         struct ftrace_func_entry        *entry;
5206         int                             idx;   /* for hash table iteration */
5207         enum graph_filter_type          type;
5208         struct ftrace_hash              *new_hash;
5209         const struct seq_operations     *seq_ops;
5210         struct trace_parser             parser;
5211 };
5212
5213 static void *
5214 __g_next(struct seq_file *m, loff_t *pos)
5215 {
5216         struct ftrace_graph_data *fgd = m->private;
5217         struct ftrace_func_entry *entry = fgd->entry;
5218         struct hlist_head *head;
5219         int i, idx = fgd->idx;
5220
5221         if (*pos >= fgd->hash->count)
5222                 return NULL;
5223
5224         if (entry) {
5225                 hlist_for_each_entry_continue(entry, hlist) {
5226                         fgd->entry = entry;
5227                         return entry;
5228                 }
5229
5230                 idx++;
5231         }
5232
5233         for (i = idx; i < 1 << fgd->hash->size_bits; i++) {
5234                 head = &fgd->hash->buckets[i];
5235                 hlist_for_each_entry(entry, head, hlist) {
5236                         fgd->entry = entry;
5237                         fgd->idx = i;
5238                         return entry;
5239                 }
5240         }
5241         return NULL;
5242 }
5243
5244 static void *
5245 g_next(struct seq_file *m, void *v, loff_t *pos)
5246 {
5247         (*pos)++;
5248         return __g_next(m, pos);
5249 }
5250
5251 static void *g_start(struct seq_file *m, loff_t *pos)
5252 {
5253         struct ftrace_graph_data *fgd = m->private;
5254
5255         mutex_lock(&graph_lock);
5256
5257         if (fgd->type == GRAPH_FILTER_FUNCTION)
5258                 fgd->hash = rcu_dereference_protected(ftrace_graph_hash,
5259                                         lockdep_is_held(&graph_lock));
5260         else
5261                 fgd->hash = rcu_dereference_protected(ftrace_graph_notrace_hash,
5262                                         lockdep_is_held(&graph_lock));
5263
5264         /* Nothing, tell g_show to print all functions are enabled */
5265         if (ftrace_hash_empty(fgd->hash) && !*pos)
5266                 return FTRACE_GRAPH_EMPTY;
5267
5268         fgd->idx = 0;
5269         fgd->entry = NULL;
5270         return __g_next(m, pos);
5271 }
5272
5273 static void g_stop(struct seq_file *m, void *p)
5274 {
5275         mutex_unlock(&graph_lock);
5276 }
5277
5278 static int g_show(struct seq_file *m, void *v)
5279 {
5280         struct ftrace_func_entry *entry = v;
5281
5282         if (!entry)
5283                 return 0;
5284
5285         if (entry == FTRACE_GRAPH_EMPTY) {
5286                 struct ftrace_graph_data *fgd = m->private;
5287
5288                 if (fgd->type == GRAPH_FILTER_FUNCTION)
5289                         seq_puts(m, "#### all functions enabled ####\n");
5290                 else
5291                         seq_puts(m, "#### no functions disabled ####\n");
5292                 return 0;
5293         }
5294
5295         seq_printf(m, "%ps\n", (void *)entry->ip);
5296
5297         return 0;
5298 }
5299
5300 static const struct seq_operations ftrace_graph_seq_ops = {
5301         .start = g_start,
5302         .next = g_next,
5303         .stop = g_stop,
5304         .show = g_show,
5305 };
5306
5307 static int
5308 __ftrace_graph_open(struct inode *inode, struct file *file,
5309                     struct ftrace_graph_data *fgd)
5310 {
5311         int ret = 0;
5312         struct ftrace_hash *new_hash = NULL;
5313
5314         if (file->f_mode & FMODE_WRITE) {
5315                 const int size_bits = FTRACE_HASH_DEFAULT_BITS;
5316
5317                 if (trace_parser_get_init(&fgd->parser, FTRACE_BUFF_MAX))
5318                         return -ENOMEM;
5319
5320                 if (file->f_flags & O_TRUNC)
5321                         new_hash = alloc_ftrace_hash(size_bits);
5322                 else
5323                         new_hash = alloc_and_copy_ftrace_hash(size_bits,
5324                                                               fgd->hash);
5325                 if (!new_hash) {
5326                         ret = -ENOMEM;
5327                         goto out;
5328                 }
5329         }
5330
5331         if (file->f_mode & FMODE_READ) {
5332                 ret = seq_open(file, &ftrace_graph_seq_ops);
5333                 if (!ret) {
5334                         struct seq_file *m = file->private_data;
5335                         m->private = fgd;
5336                 } else {
5337                         /* Failed */
5338                         free_ftrace_hash(new_hash);
5339                         new_hash = NULL;
5340                 }
5341         } else
5342                 file->private_data = fgd;
5343
5344 out:
5345         if (ret < 0 && file->f_mode & FMODE_WRITE)
5346                 trace_parser_put(&fgd->parser);
5347
5348         fgd->new_hash = new_hash;
5349
5350         /*
5351          * All uses of fgd->hash must be taken with the graph_lock
5352          * held. The graph_lock is going to be released, so force
5353          * fgd->hash to be reinitialized when it is taken again.
5354          */
5355         fgd->hash = NULL;
5356
5357         return ret;
5358 }
5359
5360 static int
5361 ftrace_graph_open(struct inode *inode, struct file *file)
5362 {
5363         struct ftrace_graph_data *fgd;
5364         int ret;
5365
5366         if (unlikely(ftrace_disabled))
5367                 return -ENODEV;
5368
5369         fgd = kmalloc(sizeof(*fgd), GFP_KERNEL);
5370         if (fgd == NULL)
5371                 return -ENOMEM;
5372
5373         mutex_lock(&graph_lock);
5374
5375         fgd->hash = rcu_dereference_protected(ftrace_graph_hash,
5376                                         lockdep_is_held(&graph_lock));
5377         fgd->type = GRAPH_FILTER_FUNCTION;
5378         fgd->seq_ops = &ftrace_graph_seq_ops;
5379
5380         ret = __ftrace_graph_open(inode, file, fgd);
5381         if (ret < 0)
5382                 kfree(fgd);
5383
5384         mutex_unlock(&graph_lock);
5385         return ret;
5386 }
5387
5388 static int
5389 ftrace_graph_notrace_open(struct inode *inode, struct file *file)
5390 {
5391         struct ftrace_graph_data *fgd;
5392         int ret;
5393
5394         if (unlikely(ftrace_disabled))
5395                 return -ENODEV;
5396
5397         fgd = kmalloc(sizeof(*fgd), GFP_KERNEL);
5398         if (fgd == NULL)
5399                 return -ENOMEM;
5400
5401         mutex_lock(&graph_lock);
5402
5403         fgd->hash = rcu_dereference_protected(ftrace_graph_notrace_hash,
5404                                         lockdep_is_held(&graph_lock));
5405         fgd->type = GRAPH_FILTER_NOTRACE;
5406         fgd->seq_ops = &ftrace_graph_seq_ops;
5407
5408         ret = __ftrace_graph_open(inode, file, fgd);
5409         if (ret < 0)
5410                 kfree(fgd);
5411
5412         mutex_unlock(&graph_lock);
5413         return ret;
5414 }
5415
5416 static int
5417 ftrace_graph_release(struct inode *inode, struct file *file)
5418 {
5419         struct ftrace_graph_data *fgd;
5420         struct ftrace_hash *old_hash, *new_hash;
5421         struct trace_parser *parser;
5422         int ret = 0;
5423
5424         if (file->f_mode & FMODE_READ) {
5425                 struct seq_file *m = file->private_data;
5426
5427                 fgd = m->private;
5428                 seq_release(inode, file);
5429         } else {
5430                 fgd = file->private_data;
5431         }
5432
5433
5434         if (file->f_mode & FMODE_WRITE) {
5435
5436                 parser = &fgd->parser;
5437
5438                 if (trace_parser_loaded((parser))) {
5439                         parser->buffer[parser->idx] = 0;
5440                         ret = ftrace_graph_set_hash(fgd->new_hash,
5441                                                     parser->buffer);
5442                 }
5443
5444                 trace_parser_put(parser);
5445
5446                 new_hash = __ftrace_hash_move(fgd->new_hash);
5447                 if (!new_hash) {
5448                         ret = -ENOMEM;
5449                         goto out;
5450                 }
5451
5452                 mutex_lock(&graph_lock);
5453
5454                 if (fgd->type == GRAPH_FILTER_FUNCTION) {
5455                         old_hash = rcu_dereference_protected(ftrace_graph_hash,
5456                                         lockdep_is_held(&graph_lock));
5457                         rcu_assign_pointer(ftrace_graph_hash, new_hash);
5458                 } else {
5459                         old_hash = rcu_dereference_protected(ftrace_graph_notrace_hash,
5460                                         lockdep_is_held(&graph_lock));
5461                         rcu_assign_pointer(ftrace_graph_notrace_hash, new_hash);
5462                 }
5463
5464                 mutex_unlock(&graph_lock);
5465
5466                 /*
5467                  * We need to do a hard force of sched synchronization.
5468                  * This is because we use preempt_disable() to do RCU, but
5469                  * the function tracers can be called where RCU is not watching
5470                  * (like before user_exit()). We can not rely on the RCU
5471                  * infrastructure to do the synchronization, thus we must do it
5472                  * ourselves.
5473                  */
5474                 schedule_on_each_cpu(ftrace_sync);
5475
5476                 free_ftrace_hash(old_hash);
5477         }
5478
5479  out:
5480         free_ftrace_hash(fgd->new_hash);
5481         kfree(fgd);
5482
5483         return ret;
5484 }
5485
5486 static int
5487 ftrace_graph_set_hash(struct ftrace_hash *hash, char *buffer)
5488 {
5489         struct ftrace_glob func_g;
5490         struct dyn_ftrace *rec;
5491         struct ftrace_page *pg;
5492         struct ftrace_func_entry *entry;
5493         int fail = 1;
5494         int not;
5495
5496         /* decode regex */
5497         func_g.type = filter_parse_regex(buffer, strlen(buffer),
5498                                          &func_g.search, &not);
5499
5500         func_g.len = strlen(func_g.search);
5501
5502         mutex_lock(&ftrace_lock);
5503
5504         if (unlikely(ftrace_disabled)) {
5505                 mutex_unlock(&ftrace_lock);
5506                 return -ENODEV;
5507         }
5508
5509         do_for_each_ftrace_rec(pg, rec) {
5510
5511                 if (rec->flags & FTRACE_FL_DISABLED)
5512                         continue;
5513
5514                 if (ftrace_match_record(rec, &func_g, NULL, 0)) {
5515                         entry = ftrace_lookup_ip(hash, rec->ip);
5516
5517                         if (!not) {
5518                                 fail = 0;
5519
5520                                 if (entry)
5521                                         continue;
5522                                 if (add_hash_entry(hash, rec->ip) < 0)
5523                                         goto out;
5524                         } else {
5525                                 if (entry) {
5526                                         free_hash_entry(hash, entry);
5527                                         fail = 0;
5528                                 }
5529                         }
5530                 }
5531         } while_for_each_ftrace_rec();
5532 out:
5533         mutex_unlock(&ftrace_lock);
5534
5535         if (fail)
5536                 return -EINVAL;
5537
5538         return 0;
5539 }
5540
5541 static ssize_t
5542 ftrace_graph_write(struct file *file, const char __user *ubuf,
5543                    size_t cnt, loff_t *ppos)
5544 {
5545         ssize_t read, ret = 0;
5546         struct ftrace_graph_data *fgd = file->private_data;
5547         struct trace_parser *parser;
5548
5549         if (!cnt)
5550                 return 0;
5551
5552         /* Read mode uses seq functions */
5553         if (file->f_mode & FMODE_READ) {
5554                 struct seq_file *m = file->private_data;
5555                 fgd = m->private;
5556         }
5557
5558         parser = &fgd->parser;
5559
5560         read = trace_get_user(parser, ubuf, cnt, ppos);
5561
5562         if (read >= 0 && trace_parser_loaded(parser) &&
5563             !trace_parser_cont(parser)) {
5564
5565                 ret = ftrace_graph_set_hash(fgd->new_hash,
5566                                             parser->buffer);
5567                 trace_parser_clear(parser);
5568         }
5569
5570         if (!ret)
5571                 ret = read;
5572
5573         return ret;
5574 }
5575
5576 static const struct file_operations ftrace_graph_fops = {
5577         .open           = ftrace_graph_open,
5578         .read           = seq_read,
5579         .write          = ftrace_graph_write,
5580         .llseek         = tracing_lseek,
5581         .release        = ftrace_graph_release,
5582 };
5583
5584 static const struct file_operations ftrace_graph_notrace_fops = {
5585         .open           = ftrace_graph_notrace_open,
5586         .read           = seq_read,
5587         .write          = ftrace_graph_write,
5588         .llseek         = tracing_lseek,
5589         .release        = ftrace_graph_release,
5590 };
5591 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
5592
5593 void ftrace_create_filter_files(struct ftrace_ops *ops,
5594                                 struct dentry *parent)
5595 {
5596
5597         trace_create_file("set_ftrace_filter", 0644, parent,
5598                           ops, &ftrace_filter_fops);
5599
5600         trace_create_file("set_ftrace_notrace", 0644, parent,
5601                           ops, &ftrace_notrace_fops);
5602 }
5603
5604 /*
5605  * The name "destroy_filter_files" is really a misnomer. Although
5606  * in the future, it may actualy delete the files, but this is
5607  * really intended to make sure the ops passed in are disabled
5608  * and that when this function returns, the caller is free to
5609  * free the ops.
5610  *
5611  * The "destroy" name is only to match the "create" name that this
5612  * should be paired with.
5613  */
5614 void ftrace_destroy_filter_files(struct ftrace_ops *ops)
5615 {
5616         mutex_lock(&ftrace_lock);
5617         if (ops->flags & FTRACE_OPS_FL_ENABLED)
5618                 ftrace_shutdown(ops, 0);
5619         ops->flags |= FTRACE_OPS_FL_DELETED;
5620         ftrace_free_filter(ops);
5621         mutex_unlock(&ftrace_lock);
5622 }
5623
5624 static __init int ftrace_init_dyn_tracefs(struct dentry *d_tracer)
5625 {
5626
5627         trace_create_file("available_filter_functions", 0444,
5628                         d_tracer, NULL, &ftrace_avail_fops);
5629
5630         trace_create_file("enabled_functions", 0444,
5631                         d_tracer, NULL, &ftrace_enabled_fops);
5632
5633         ftrace_create_filter_files(&global_ops, d_tracer);
5634
5635 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
5636         trace_create_file("set_graph_function", 0444, d_tracer,
5637                                     NULL,
5638                                     &ftrace_graph_fops);
5639         trace_create_file("set_graph_notrace", 0444, d_tracer,
5640                                     NULL,
5641                                     &ftrace_graph_notrace_fops);
5642 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
5643
5644         return 0;
5645 }
5646
5647 static int ftrace_cmp_ips(const void *a, const void *b)
5648 {
5649         const unsigned long *ipa = a;
5650         const unsigned long *ipb = b;
5651
5652         if (*ipa > *ipb)
5653                 return 1;
5654         if (*ipa < *ipb)
5655                 return -1;
5656         return 0;
5657 }
5658
5659 static int ftrace_process_locs(struct module *mod,
5660                                unsigned long *start,
5661                                unsigned long *end)
5662 {
5663         struct ftrace_page *start_pg;
5664         struct ftrace_page *pg;
5665         struct dyn_ftrace *rec;
5666         unsigned long count;
5667         unsigned long *p;
5668         unsigned long addr;
5669         unsigned long flags = 0; /* Shut up gcc */
5670         int ret = -ENOMEM;
5671
5672         count = end - start;
5673
5674         if (!count)
5675                 return 0;
5676
5677         sort(start, count, sizeof(*start),
5678              ftrace_cmp_ips, NULL);
5679
5680         start_pg = ftrace_allocate_pages(count);
5681         if (!start_pg)
5682                 return -ENOMEM;
5683
5684         mutex_lock(&ftrace_lock);
5685
5686         /*
5687          * Core and each module needs their own pages, as
5688          * modules will free them when they are removed.
5689          * Force a new page to be allocated for modules.
5690          */
5691         if (!mod) {
5692                 WARN_ON(ftrace_pages || ftrace_pages_start);
5693                 /* First initialization */
5694                 ftrace_pages = ftrace_pages_start = start_pg;
5695         } else {
5696                 if (!ftrace_pages)
5697                         goto out;
5698
5699                 if (WARN_ON(ftrace_pages->next)) {
5700                         /* Hmm, we have free pages? */
5701                         while (ftrace_pages->next)
5702                                 ftrace_pages = ftrace_pages->next;
5703                 }
5704
5705                 ftrace_pages->next = start_pg;
5706         }
5707
5708         p = start;
5709         pg = start_pg;
5710         while (p < end) {
5711                 addr = ftrace_call_adjust(*p++);
5712                 /*
5713                  * Some architecture linkers will pad between
5714                  * the different mcount_loc sections of different
5715                  * object files to satisfy alignments.
5716                  * Skip any NULL pointers.
5717                  */
5718                 if (!addr)
5719                         continue;
5720
5721                 if (pg->index == pg->size) {
5722                         /* We should have allocated enough */
5723                         if (WARN_ON(!pg->next))
5724                                 break;
5725                         pg = pg->next;
5726                 }
5727
5728                 rec = &pg->records[pg->index++];
5729                 rec->ip = addr;
5730         }
5731
5732         /* We should have used all pages */
5733         WARN_ON(pg->next);
5734
5735         /* Assign the last page to ftrace_pages */
5736         ftrace_pages = pg;
5737
5738         /*
5739          * We only need to disable interrupts on start up
5740          * because we are modifying code that an interrupt
5741          * may execute, and the modification is not atomic.
5742          * But for modules, nothing runs the code we modify
5743          * until we are finished with it, and there's no
5744          * reason to cause large interrupt latencies while we do it.
5745          */
5746         if (!mod)
5747                 local_irq_save(flags);
5748         ftrace_update_code(mod, start_pg);
5749         if (!mod)
5750                 local_irq_restore(flags);
5751         ret = 0;
5752  out:
5753         mutex_unlock(&ftrace_lock);
5754
5755         return ret;
5756 }
5757
5758 #ifdef CONFIG_MODULES
5759
5760 #define next_to_ftrace_page(p) container_of(p, struct ftrace_page, next)
5761
5762 static int referenced_filters(struct dyn_ftrace *rec)
5763 {
5764         struct ftrace_ops *ops;
5765         int cnt = 0;
5766
5767         for (ops = ftrace_ops_list; ops != &ftrace_list_end; ops = ops->next) {
5768                 if (ops_references_rec(ops, rec)) {
5769                         cnt++;
5770                         if (ops->flags & FTRACE_OPS_FL_SAVE_REGS)
5771                                 rec->flags |= FTRACE_FL_REGS;
5772                 }
5773         }
5774
5775         return cnt;
5776 }
5777
5778 static void
5779 clear_mod_from_hash(struct ftrace_page *pg, struct ftrace_hash *hash)
5780 {
5781         struct ftrace_func_entry *entry;
5782         struct dyn_ftrace *rec;
5783         int i;
5784
5785         if (ftrace_hash_empty(hash))
5786                 return;
5787
5788         for (i = 0; i < pg->index; i++) {
5789                 rec = &pg->records[i];
5790                 entry = __ftrace_lookup_ip(hash, rec->ip);
5791                 /*
5792                  * Do not allow this rec to match again.
5793                  * Yeah, it may waste some memory, but will be removed
5794                  * if/when the hash is modified again.
5795                  */
5796                 if (entry)
5797                         entry->ip = 0;
5798         }
5799 }
5800
5801 /* Clear any records from hashs */
5802 static void clear_mod_from_hashes(struct ftrace_page *pg)
5803 {
5804         struct trace_array *tr;
5805
5806         mutex_lock(&trace_types_lock);
5807         list_for_each_entry(tr, &ftrace_trace_arrays, list) {
5808                 if (!tr->ops || !tr->ops->func_hash)
5809                         continue;
5810                 mutex_lock(&tr->ops->func_hash->regex_lock);
5811                 clear_mod_from_hash(pg, tr->ops->func_hash->filter_hash);
5812                 clear_mod_from_hash(pg, tr->ops->func_hash->notrace_hash);
5813                 mutex_unlock(&tr->ops->func_hash->regex_lock);
5814         }
5815         mutex_unlock(&trace_types_lock);
5816 }
5817
5818 void ftrace_release_mod(struct module *mod)
5819 {
5820         struct dyn_ftrace *rec;
5821         struct ftrace_page **last_pg;
5822         struct ftrace_page *tmp_page = NULL;
5823         struct ftrace_page *pg;
5824         int order;
5825
5826         mutex_lock(&ftrace_lock);
5827
5828         if (ftrace_disabled)
5829                 goto out_unlock;
5830
5831         /*
5832          * Each module has its own ftrace_pages, remove
5833          * them from the list.
5834          */
5835         last_pg = &ftrace_pages_start;
5836         for (pg = ftrace_pages_start; pg; pg = *last_pg) {
5837                 rec = &pg->records[0];
5838                 if (within_module_core(rec->ip, mod)) {
5839                         /*
5840                          * As core pages are first, the first
5841                          * page should never be a module page.
5842                          */
5843                         if (WARN_ON(pg == ftrace_pages_start))
5844                                 goto out_unlock;
5845
5846                         /* Check if we are deleting the last page */
5847                         if (pg == ftrace_pages)
5848                                 ftrace_pages = next_to_ftrace_page(last_pg);
5849
5850                         ftrace_update_tot_cnt -= pg->index;
5851                         *last_pg = pg->next;
5852
5853                         pg->next = tmp_page;
5854                         tmp_page = pg;
5855                 } else
5856                         last_pg = &pg->next;
5857         }
5858  out_unlock:
5859         mutex_unlock(&ftrace_lock);
5860
5861         for (pg = tmp_page; pg; pg = tmp_page) {
5862
5863                 /* Needs to be called outside of ftrace_lock */
5864                 clear_mod_from_hashes(pg);
5865
5866                 order = get_count_order(pg->size / ENTRIES_PER_PAGE);
5867                 free_pages((unsigned long)pg->records, order);
5868                 tmp_page = pg->next;
5869                 kfree(pg);
5870         }
5871 }
5872
5873 void ftrace_module_enable(struct module *mod)
5874 {
5875         struct dyn_ftrace *rec;
5876         struct ftrace_page *pg;
5877
5878         mutex_lock(&ftrace_lock);
5879
5880         if (ftrace_disabled)
5881                 goto out_unlock;
5882
5883         /*
5884          * If the tracing is enabled, go ahead and enable the record.
5885          *
5886          * The reason not to enable the record immediatelly is the
5887          * inherent check of ftrace_make_nop/ftrace_make_call for
5888          * correct previous instructions.  Making first the NOP
5889          * conversion puts the module to the correct state, thus
5890          * passing the ftrace_make_call check.
5891          *
5892          * We also delay this to after the module code already set the
5893          * text to read-only, as we now need to set it back to read-write
5894          * so that we can modify the text.
5895          */
5896         if (ftrace_start_up)
5897                 ftrace_arch_code_modify_prepare();
5898
5899         do_for_each_ftrace_rec(pg, rec) {
5900                 int cnt;
5901                 /*
5902                  * do_for_each_ftrace_rec() is a double loop.
5903                  * module text shares the pg. If a record is
5904                  * not part of this module, then skip this pg,
5905                  * which the "break" will do.
5906                  */
5907                 if (!within_module_core(rec->ip, mod))
5908                         break;
5909
5910                 cnt = 0;
5911
5912                 /*
5913                  * When adding a module, we need to check if tracers are
5914                  * currently enabled and if they are, and can trace this record,
5915                  * we need to enable the module functions as well as update the
5916                  * reference counts for those function records.
5917                  */
5918                 if (ftrace_start_up)
5919                         cnt += referenced_filters(rec);
5920
5921                 rec->flags &= ~FTRACE_FL_DISABLED;
5922                 rec->flags += cnt;
5923
5924                 if (ftrace_start_up && cnt) {
5925                         int failed = __ftrace_replace_code(rec, 1);
5926                         if (failed) {
5927                                 ftrace_bug(failed, rec);
5928                                 goto out_loop;
5929                         }
5930                 }
5931
5932         } while_for_each_ftrace_rec();
5933
5934  out_loop:
5935         if (ftrace_start_up)
5936                 ftrace_arch_code_modify_post_process();
5937
5938  out_unlock:
5939         mutex_unlock(&ftrace_lock);
5940
5941         process_cached_mods(mod->name);
5942 }
5943
5944 void ftrace_module_init(struct module *mod)
5945 {
5946         if (ftrace_disabled || !mod->num_ftrace_callsites)
5947                 return;
5948
5949         ftrace_process_locs(mod, mod->ftrace_callsites,
5950                             mod->ftrace_callsites + mod->num_ftrace_callsites);
5951 }
5952 #endif /* CONFIG_MODULES */
5953
5954 void __init ftrace_free_init_mem(void)
5955 {
5956         unsigned long start = (unsigned long)(&__init_begin);
5957         unsigned long end = (unsigned long)(&__init_end);
5958         struct ftrace_page **last_pg = &ftrace_pages_start;
5959         struct ftrace_page *pg;
5960         struct dyn_ftrace *rec;
5961         struct dyn_ftrace key;
5962         int order;
5963
5964         key.ip = start;
5965         key.flags = end;        /* overload flags, as it is unsigned long */
5966
5967         mutex_lock(&ftrace_lock);
5968
5969         for (pg = ftrace_pages_start; pg; last_pg = &pg->next, pg = *last_pg) {
5970                 if (end < pg->records[0].ip ||
5971                     start >= (pg->records[pg->index - 1].ip + MCOUNT_INSN_SIZE))
5972                         continue;
5973  again:
5974                 rec = bsearch(&key, pg->records, pg->index,
5975                               sizeof(struct dyn_ftrace),
5976                               ftrace_cmp_recs);
5977                 if (!rec)
5978                         continue;
5979                 pg->index--;
5980                 ftrace_update_tot_cnt--;
5981                 if (!pg->index) {
5982                         *last_pg = pg->next;
5983                         order = get_count_order(pg->size / ENTRIES_PER_PAGE);
5984                         free_pages((unsigned long)pg->records, order);
5985                         kfree(pg);
5986                         pg = container_of(last_pg, struct ftrace_page, next);
5987                         if (!(*last_pg))
5988                                 ftrace_pages = pg;
5989                         continue;
5990                 }
5991                 memmove(rec, rec + 1,
5992                         (pg->index - (rec - pg->records)) * sizeof(*rec));
5993                 /* More than one function may be in this block */
5994                 goto again;
5995         }
5996         mutex_unlock(&ftrace_lock);
5997 }
5998
5999 void __init ftrace_init(void)
6000 {
6001         extern unsigned long __start_mcount_loc[];
6002         extern unsigned long __stop_mcount_loc[];
6003         unsigned long count, flags;
6004         int ret;
6005
6006         local_irq_save(flags);
6007         ret = ftrace_dyn_arch_init();
6008         local_irq_restore(flags);
6009         if (ret)
6010                 goto failed;
6011
6012         count = __stop_mcount_loc - __start_mcount_loc;
6013         if (!count) {
6014                 pr_info("ftrace: No functions to be traced?\n");
6015                 goto failed;
6016         }
6017
6018         pr_info("ftrace: allocating %ld entries in %ld pages\n",
6019                 count, DIV_ROUND_UP(count, ENTRIES_PER_PAGE));
6020
6021         last_ftrace_enabled = ftrace_enabled = 1;
6022
6023         ret = ftrace_process_locs(NULL,
6024                                   __start_mcount_loc,
6025                                   __stop_mcount_loc);
6026
6027         set_ftrace_early_filters();
6028
6029         return;
6030  failed:
6031         ftrace_disabled = 1;
6032 }
6033
6034 /* Do nothing if arch does not support this */
6035 void __weak arch_ftrace_update_trampoline(struct ftrace_ops *ops)
6036 {
6037 }
6038
6039 static void ftrace_update_trampoline(struct ftrace_ops *ops)
6040 {
6041         arch_ftrace_update_trampoline(ops);
6042 }
6043
6044 void ftrace_init_trace_array(struct trace_array *tr)
6045 {
6046         INIT_LIST_HEAD(&tr->func_probes);
6047         INIT_LIST_HEAD(&tr->mod_trace);
6048         INIT_LIST_HEAD(&tr->mod_notrace);
6049 }
6050 #else
6051
6052 static struct ftrace_ops global_ops = {
6053         .func                   = ftrace_stub,
6054         .flags                  = FTRACE_OPS_FL_RECURSION_SAFE |
6055                                   FTRACE_OPS_FL_INITIALIZED |
6056                                   FTRACE_OPS_FL_PID,
6057 };
6058
6059 static int __init ftrace_nodyn_init(void)
6060 {
6061         ftrace_enabled = 1;
6062         return 0;
6063 }
6064 core_initcall(ftrace_nodyn_init);
6065
6066 static inline int ftrace_init_dyn_tracefs(struct dentry *d_tracer) { return 0; }
6067 static inline void ftrace_startup_enable(int command) { }
6068 static inline void ftrace_startup_all(int command) { }
6069 /* Keep as macros so we do not need to define the commands */
6070 # define ftrace_startup(ops, command)                                   \
6071         ({                                                              \
6072                 int ___ret = __register_ftrace_function(ops);           \
6073                 if (!___ret)                                            \
6074                         (ops)->flags |= FTRACE_OPS_FL_ENABLED;          \
6075                 ___ret;                                                 \
6076         })
6077 # define ftrace_shutdown(ops, command)                                  \
6078         ({                                                              \
6079                 int ___ret = __unregister_ftrace_function(ops);         \
6080                 if (!___ret)                                            \
6081                         (ops)->flags &= ~FTRACE_OPS_FL_ENABLED;         \
6082                 ___ret;                                                 \
6083         })
6084
6085 # define ftrace_startup_sysctl()        do { } while (0)
6086 # define ftrace_shutdown_sysctl()       do { } while (0)
6087
6088 static inline int
6089 ftrace_ops_test(struct ftrace_ops *ops, unsigned long ip, void *regs)
6090 {
6091         return 1;
6092 }
6093
6094 static void ftrace_update_trampoline(struct ftrace_ops *ops)
6095 {
6096 }
6097
6098 #endif /* CONFIG_DYNAMIC_FTRACE */
6099
6100 __init void ftrace_init_global_array_ops(struct trace_array *tr)
6101 {
6102         tr->ops = &global_ops;
6103         tr->ops->private = tr;
6104         ftrace_init_trace_array(tr);
6105 }
6106
6107 void ftrace_init_array_ops(struct trace_array *tr, ftrace_func_t func)
6108 {
6109         /* If we filter on pids, update to use the pid function */
6110         if (tr->flags & TRACE_ARRAY_FL_GLOBAL) {
6111                 if (WARN_ON(tr->ops->func != ftrace_stub))
6112                         printk("ftrace ops had %pS for function\n",
6113                                tr->ops->func);
6114         }
6115         tr->ops->func = func;
6116         tr->ops->private = tr;
6117 }
6118
6119 void ftrace_reset_array_ops(struct trace_array *tr)
6120 {
6121         tr->ops->func = ftrace_stub;
6122 }
6123
6124 static nokprobe_inline void
6125 __ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip,
6126                        struct ftrace_ops *ignored, struct pt_regs *regs)
6127 {
6128         struct ftrace_ops *op;
6129         int bit;
6130
6131         bit = trace_test_and_set_recursion(TRACE_LIST_START);
6132         if (bit < 0)
6133                 return;
6134
6135         /*
6136          * Some of the ops may be dynamically allocated,
6137          * they must be freed after a synchronize_sched().
6138          */
6139         preempt_disable_notrace();
6140
6141         do_for_each_ftrace_op(op, ftrace_ops_list) {
6142                 /*
6143                  * Check the following for each ops before calling their func:
6144                  *  if RCU flag is set, then rcu_is_watching() must be true
6145                  *  if PER_CPU is set, then ftrace_function_local_disable()
6146                  *                          must be false
6147                  *  Otherwise test if the ip matches the ops filter
6148                  *
6149                  * If any of the above fails then the op->func() is not executed.
6150                  */
6151                 if ((!(op->flags & FTRACE_OPS_FL_RCU) || rcu_is_watching()) &&
6152                     (!(op->flags & FTRACE_OPS_FL_PER_CPU) ||
6153                      !ftrace_function_local_disabled(op)) &&
6154                     ftrace_ops_test(op, ip, regs)) {
6155                     
6156                         if (FTRACE_WARN_ON(!op->func)) {
6157                                 pr_warn("op=%p %pS\n", op, op);
6158                                 goto out;
6159                         }
6160                         op->func(ip, parent_ip, op, regs);
6161                 }
6162         } while_for_each_ftrace_op(op);
6163 out:
6164         preempt_enable_notrace();
6165         trace_clear_recursion(bit);
6166 }
6167
6168 /*
6169  * Some archs only support passing ip and parent_ip. Even though
6170  * the list function ignores the op parameter, we do not want any
6171  * C side effects, where a function is called without the caller
6172  * sending a third parameter.
6173  * Archs are to support both the regs and ftrace_ops at the same time.
6174  * If they support ftrace_ops, it is assumed they support regs.
6175  * If call backs want to use regs, they must either check for regs
6176  * being NULL, or CONFIG_DYNAMIC_FTRACE_WITH_REGS.
6177  * Note, CONFIG_DYNAMIC_FTRACE_WITH_REGS expects a full regs to be saved.
6178  * An architecture can pass partial regs with ftrace_ops and still
6179  * set the ARCH_SUPPORTS_FTRACE_OPS.
6180  */
6181 #if ARCH_SUPPORTS_FTRACE_OPS
6182 static void ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip,
6183                                  struct ftrace_ops *op, struct pt_regs *regs)
6184 {
6185         __ftrace_ops_list_func(ip, parent_ip, NULL, regs);
6186 }
6187 NOKPROBE_SYMBOL(ftrace_ops_list_func);
6188 #else
6189 static void ftrace_ops_no_ops(unsigned long ip, unsigned long parent_ip)
6190 {
6191         __ftrace_ops_list_func(ip, parent_ip, NULL, NULL);
6192 }
6193 NOKPROBE_SYMBOL(ftrace_ops_no_ops);
6194 #endif
6195
6196 /*
6197  * If there's only one function registered but it does not support
6198  * recursion, needs RCU protection and/or requires per cpu handling, then
6199  * this function will be called by the mcount trampoline.
6200  */
6201 static void ftrace_ops_assist_func(unsigned long ip, unsigned long parent_ip,
6202                                    struct ftrace_ops *op, struct pt_regs *regs)
6203 {
6204         int bit;
6205
6206         bit = trace_test_and_set_recursion(TRACE_LIST_START);
6207         if (bit < 0)
6208                 return;
6209
6210         preempt_disable_notrace();
6211
6212         if ((!(op->flags & FTRACE_OPS_FL_RCU) || rcu_is_watching()) &&
6213             (!(op->flags & FTRACE_OPS_FL_PER_CPU) ||
6214              !ftrace_function_local_disabled(op))) {
6215                 op->func(ip, parent_ip, op, regs);
6216         }
6217
6218         preempt_enable_notrace();
6219         trace_clear_recursion(bit);
6220 }
6221 NOKPROBE_SYMBOL(ftrace_ops_assist_func);
6222
6223 /**
6224  * ftrace_ops_get_func - get the function a trampoline should call
6225  * @ops: the ops to get the function for
6226  *
6227  * Normally the mcount trampoline will call the ops->func, but there
6228  * are times that it should not. For example, if the ops does not
6229  * have its own recursion protection, then it should call the
6230  * ftrace_ops_assist_func() instead.
6231  *
6232  * Returns the function that the trampoline should call for @ops.
6233  */
6234 ftrace_func_t ftrace_ops_get_func(struct ftrace_ops *ops)
6235 {
6236         /*
6237          * If the function does not handle recursion, needs to be RCU safe,
6238          * or does per cpu logic, then we need to call the assist handler.
6239          */
6240         if (!(ops->flags & FTRACE_OPS_FL_RECURSION_SAFE) ||
6241             ops->flags & (FTRACE_OPS_FL_RCU | FTRACE_OPS_FL_PER_CPU))
6242                 return ftrace_ops_assist_func;
6243
6244         return ops->func;
6245 }
6246
6247 static void
6248 ftrace_filter_pid_sched_switch_probe(void *data, bool preempt,
6249                     struct task_struct *prev, struct task_struct *next)
6250 {
6251         struct trace_array *tr = data;
6252         struct trace_pid_list *pid_list;
6253
6254         pid_list = rcu_dereference_sched(tr->function_pids);
6255
6256         this_cpu_write(tr->trace_buffer.data->ftrace_ignore_pid,
6257                        trace_ignore_this_task(pid_list, next));
6258 }
6259
6260 static void
6261 ftrace_pid_follow_sched_process_fork(void *data,
6262                                      struct task_struct *self,
6263                                      struct task_struct *task)
6264 {
6265         struct trace_pid_list *pid_list;
6266         struct trace_array *tr = data;
6267
6268         pid_list = rcu_dereference_sched(tr->function_pids);
6269         trace_filter_add_remove_task(pid_list, self, task);
6270 }
6271
6272 static void
6273 ftrace_pid_follow_sched_process_exit(void *data, struct task_struct *task)
6274 {
6275         struct trace_pid_list *pid_list;
6276         struct trace_array *tr = data;
6277
6278         pid_list = rcu_dereference_sched(tr->function_pids);
6279         trace_filter_add_remove_task(pid_list, NULL, task);
6280 }
6281
6282 void ftrace_pid_follow_fork(struct trace_array *tr, bool enable)
6283 {
6284         if (enable) {
6285                 register_trace_sched_process_fork(ftrace_pid_follow_sched_process_fork,
6286                                                   tr);
6287                 register_trace_sched_process_free(ftrace_pid_follow_sched_process_exit,
6288                                                   tr);
6289         } else {
6290                 unregister_trace_sched_process_fork(ftrace_pid_follow_sched_process_fork,
6291                                                     tr);
6292                 unregister_trace_sched_process_free(ftrace_pid_follow_sched_process_exit,
6293                                                     tr);
6294         }
6295 }
6296
6297 static void clear_ftrace_pids(struct trace_array *tr)
6298 {
6299         struct trace_pid_list *pid_list;
6300         int cpu;
6301
6302         pid_list = rcu_dereference_protected(tr->function_pids,
6303                                              lockdep_is_held(&ftrace_lock));
6304         if (!pid_list)
6305                 return;
6306
6307         unregister_trace_sched_switch(ftrace_filter_pid_sched_switch_probe, tr);
6308
6309         for_each_possible_cpu(cpu)
6310                 per_cpu_ptr(tr->trace_buffer.data, cpu)->ftrace_ignore_pid = false;
6311
6312         rcu_assign_pointer(tr->function_pids, NULL);
6313
6314         /* Wait till all users are no longer using pid filtering */
6315         synchronize_sched();
6316
6317         trace_free_pid_list(pid_list);
6318 }
6319
6320 void ftrace_clear_pids(struct trace_array *tr)
6321 {
6322         mutex_lock(&ftrace_lock);
6323
6324         clear_ftrace_pids(tr);
6325
6326         mutex_unlock(&ftrace_lock);
6327 }
6328
6329 static void ftrace_pid_reset(struct trace_array *tr)
6330 {
6331         mutex_lock(&ftrace_lock);
6332         clear_ftrace_pids(tr);
6333
6334         ftrace_update_pid_func();
6335         ftrace_startup_all(0);
6336
6337         mutex_unlock(&ftrace_lock);
6338 }
6339
6340 /* Greater than any max PID */
6341 #define FTRACE_NO_PIDS          (void *)(PID_MAX_LIMIT + 1)
6342
6343 static void *fpid_start(struct seq_file *m, loff_t *pos)
6344         __acquires(RCU)
6345 {
6346         struct trace_pid_list *pid_list;
6347         struct trace_array *tr = m->private;
6348
6349         mutex_lock(&ftrace_lock);
6350         rcu_read_lock_sched();
6351
6352         pid_list = rcu_dereference_sched(tr->function_pids);
6353
6354         if (!pid_list)
6355                 return !(*pos) ? FTRACE_NO_PIDS : NULL;
6356
6357         return trace_pid_start(pid_list, pos);
6358 }
6359
6360 static void *fpid_next(struct seq_file *m, void *v, loff_t *pos)
6361 {
6362         struct trace_array *tr = m->private;
6363         struct trace_pid_list *pid_list = rcu_dereference_sched(tr->function_pids);
6364
6365         if (v == FTRACE_NO_PIDS) {
6366                 (*pos)++;
6367                 return NULL;
6368         }
6369         return trace_pid_next(pid_list, v, pos);
6370 }
6371
6372 static void fpid_stop(struct seq_file *m, void *p)
6373         __releases(RCU)
6374 {
6375         rcu_read_unlock_sched();
6376         mutex_unlock(&ftrace_lock);
6377 }
6378
6379 static int fpid_show(struct seq_file *m, void *v)
6380 {
6381         if (v == FTRACE_NO_PIDS) {
6382                 seq_puts(m, "no pid\n");
6383                 return 0;
6384         }
6385
6386         return trace_pid_show(m, v);
6387 }
6388
6389 static const struct seq_operations ftrace_pid_sops = {
6390         .start = fpid_start,
6391         .next = fpid_next,
6392         .stop = fpid_stop,
6393         .show = fpid_show,
6394 };
6395
6396 static int
6397 ftrace_pid_open(struct inode *inode, struct file *file)
6398 {
6399         struct trace_array *tr = inode->i_private;
6400         struct seq_file *m;
6401         int ret = 0;
6402
6403         if (trace_array_get(tr) < 0)
6404                 return -ENODEV;
6405
6406         if ((file->f_mode & FMODE_WRITE) &&
6407             (file->f_flags & O_TRUNC))
6408                 ftrace_pid_reset(tr);
6409
6410         ret = seq_open(file, &ftrace_pid_sops);
6411         if (ret < 0) {
6412                 trace_array_put(tr);
6413         } else {
6414                 m = file->private_data;
6415                 /* copy tr over to seq ops */
6416                 m->private = tr;
6417         }
6418
6419         return ret;
6420 }
6421
6422 static void ignore_task_cpu(void *data)
6423 {
6424         struct trace_array *tr = data;
6425         struct trace_pid_list *pid_list;
6426
6427         /*
6428          * This function is called by on_each_cpu() while the
6429          * event_mutex is held.
6430          */
6431         pid_list = rcu_dereference_protected(tr->function_pids,
6432                                              mutex_is_locked(&ftrace_lock));
6433
6434         this_cpu_write(tr->trace_buffer.data->ftrace_ignore_pid,
6435                        trace_ignore_this_task(pid_list, current));
6436 }
6437
6438 static ssize_t
6439 ftrace_pid_write(struct file *filp, const char __user *ubuf,
6440                    size_t cnt, loff_t *ppos)
6441 {
6442         struct seq_file *m = filp->private_data;
6443         struct trace_array *tr = m->private;
6444         struct trace_pid_list *filtered_pids = NULL;
6445         struct trace_pid_list *pid_list;
6446         ssize_t ret;
6447
6448         if (!cnt)
6449                 return 0;
6450
6451         mutex_lock(&ftrace_lock);
6452
6453         filtered_pids = rcu_dereference_protected(tr->function_pids,
6454                                              lockdep_is_held(&ftrace_lock));
6455
6456         ret = trace_pid_write(filtered_pids, &pid_list, ubuf, cnt);
6457         if (ret < 0)
6458                 goto out;
6459
6460         rcu_assign_pointer(tr->function_pids, pid_list);
6461
6462         if (filtered_pids) {
6463                 synchronize_sched();
6464                 trace_free_pid_list(filtered_pids);
6465         } else if (pid_list) {
6466                 /* Register a probe to set whether to ignore the tracing of a task */
6467                 register_trace_sched_switch(ftrace_filter_pid_sched_switch_probe, tr);
6468         }
6469
6470         /*
6471          * Ignoring of pids is done at task switch. But we have to
6472          * check for those tasks that are currently running.
6473          * Always do this in case a pid was appended or removed.
6474          */
6475         on_each_cpu(ignore_task_cpu, tr, 1);
6476
6477         ftrace_update_pid_func();
6478         ftrace_startup_all(0);
6479  out:
6480         mutex_unlock(&ftrace_lock);
6481
6482         if (ret > 0)
6483                 *ppos += ret;
6484
6485         return ret;
6486 }
6487
6488 static int
6489 ftrace_pid_release(struct inode *inode, struct file *file)
6490 {
6491         struct trace_array *tr = inode->i_private;
6492
6493         trace_array_put(tr);
6494
6495         return seq_release(inode, file);
6496 }
6497
6498 static const struct file_operations ftrace_pid_fops = {
6499         .open           = ftrace_pid_open,
6500         .write          = ftrace_pid_write,
6501         .read           = seq_read,
6502         .llseek         = tracing_lseek,
6503         .release        = ftrace_pid_release,
6504 };
6505
6506 void ftrace_init_tracefs(struct trace_array *tr, struct dentry *d_tracer)
6507 {
6508         trace_create_file("set_ftrace_pid", 0644, d_tracer,
6509                             tr, &ftrace_pid_fops);
6510 }
6511
6512 void __init ftrace_init_tracefs_toplevel(struct trace_array *tr,
6513                                          struct dentry *d_tracer)
6514 {
6515         /* Only the top level directory has the dyn_tracefs and profile */
6516         WARN_ON(!(tr->flags & TRACE_ARRAY_FL_GLOBAL));
6517
6518         ftrace_init_dyn_tracefs(d_tracer);
6519         ftrace_profile_tracefs(d_tracer);
6520 }
6521
6522 /**
6523  * ftrace_kill - kill ftrace
6524  *
6525  * This function should be used by panic code. It stops ftrace
6526  * but in a not so nice way. If you need to simply kill ftrace
6527  * from a non-atomic section, use ftrace_kill.
6528  */
6529 void ftrace_kill(void)
6530 {
6531         ftrace_disabled = 1;
6532         ftrace_enabled = 0;
6533         clear_ftrace_function();
6534 }
6535
6536 /**
6537  * Test if ftrace is dead or not.
6538  */
6539 int ftrace_is_dead(void)
6540 {
6541         return ftrace_disabled;
6542 }
6543
6544 /**
6545  * register_ftrace_function - register a function for profiling
6546  * @ops - ops structure that holds the function for profiling.
6547  *
6548  * Register a function to be called by all functions in the
6549  * kernel.
6550  *
6551  * Note: @ops->func and all the functions it calls must be labeled
6552  *       with "notrace", otherwise it will go into a
6553  *       recursive loop.
6554  */
6555 int register_ftrace_function(struct ftrace_ops *ops)
6556 {
6557         int ret = -1;
6558
6559         ftrace_ops_init(ops);
6560
6561         mutex_lock(&ftrace_lock);
6562
6563         ret = ftrace_startup(ops, 0);
6564
6565         mutex_unlock(&ftrace_lock);
6566
6567         return ret;
6568 }
6569 EXPORT_SYMBOL_GPL(register_ftrace_function);
6570
6571 /**
6572  * unregister_ftrace_function - unregister a function for profiling.
6573  * @ops - ops structure that holds the function to unregister
6574  *
6575  * Unregister a function that was added to be called by ftrace profiling.
6576  */
6577 int unregister_ftrace_function(struct ftrace_ops *ops)
6578 {
6579         int ret;
6580
6581         mutex_lock(&ftrace_lock);
6582         ret = ftrace_shutdown(ops, 0);
6583         mutex_unlock(&ftrace_lock);
6584
6585         return ret;
6586 }
6587 EXPORT_SYMBOL_GPL(unregister_ftrace_function);
6588
6589 int
6590 ftrace_enable_sysctl(struct ctl_table *table, int write,
6591                      void __user *buffer, size_t *lenp,
6592                      loff_t *ppos)
6593 {
6594         int ret = -ENODEV;
6595
6596         mutex_lock(&ftrace_lock);
6597
6598         if (unlikely(ftrace_disabled))
6599                 goto out;
6600
6601         ret = proc_dointvec(table, write, buffer, lenp, ppos);
6602
6603         if (ret || !write || (last_ftrace_enabled == !!ftrace_enabled))
6604                 goto out;
6605
6606         last_ftrace_enabled = !!ftrace_enabled;
6607
6608         if (ftrace_enabled) {
6609
6610                 /* we are starting ftrace again */
6611                 if (rcu_dereference_protected(ftrace_ops_list,
6612                         lockdep_is_held(&ftrace_lock)) != &ftrace_list_end)
6613                         update_ftrace_function();
6614
6615                 ftrace_startup_sysctl();
6616
6617         } else {
6618                 /* stopping ftrace calls (just send to ftrace_stub) */
6619                 ftrace_trace_function = ftrace_stub;
6620
6621                 ftrace_shutdown_sysctl();
6622         }
6623
6624  out:
6625         mutex_unlock(&ftrace_lock);
6626         return ret;
6627 }
6628
6629 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
6630
6631 static struct ftrace_ops graph_ops = {
6632         .func                   = ftrace_stub,
6633         .flags                  = FTRACE_OPS_FL_RECURSION_SAFE |
6634                                    FTRACE_OPS_FL_INITIALIZED |
6635                                    FTRACE_OPS_FL_PID |
6636                                    FTRACE_OPS_FL_STUB,
6637 #ifdef FTRACE_GRAPH_TRAMP_ADDR
6638         .trampoline             = FTRACE_GRAPH_TRAMP_ADDR,
6639         /* trampoline_size is only needed for dynamically allocated tramps */
6640 #endif
6641         ASSIGN_OPS_HASH(graph_ops, &global_ops.local_hash)
6642 };
6643
6644 void ftrace_graph_sleep_time_control(bool enable)
6645 {
6646         fgraph_sleep_time = enable;
6647 }
6648
6649 void ftrace_graph_graph_time_control(bool enable)
6650 {
6651         fgraph_graph_time = enable;
6652 }
6653
6654 int ftrace_graph_entry_stub(struct ftrace_graph_ent *trace)
6655 {
6656         return 0;
6657 }
6658
6659 /* The callbacks that hook a function */
6660 trace_func_graph_ret_t ftrace_graph_return =
6661                         (trace_func_graph_ret_t)ftrace_stub;
6662 trace_func_graph_ent_t ftrace_graph_entry = ftrace_graph_entry_stub;
6663 static trace_func_graph_ent_t __ftrace_graph_entry = ftrace_graph_entry_stub;
6664
6665 /* Try to assign a return stack array on FTRACE_RETSTACK_ALLOC_SIZE tasks. */
6666 static int alloc_retstack_tasklist(struct ftrace_ret_stack **ret_stack_list)
6667 {
6668         int i;
6669         int ret = 0;
6670         int start = 0, end = FTRACE_RETSTACK_ALLOC_SIZE;
6671         struct task_struct *g, *t;
6672
6673         for (i = 0; i < FTRACE_RETSTACK_ALLOC_SIZE; i++) {
6674                 ret_stack_list[i] = kmalloc(FTRACE_RETFUNC_DEPTH
6675                                         * sizeof(struct ftrace_ret_stack),
6676                                         GFP_KERNEL);
6677                 if (!ret_stack_list[i]) {
6678                         start = 0;
6679                         end = i;
6680                         ret = -ENOMEM;
6681                         goto free;
6682                 }
6683         }
6684
6685         read_lock(&tasklist_lock);
6686         do_each_thread(g, t) {
6687                 if (start == end) {
6688                         ret = -EAGAIN;
6689                         goto unlock;
6690                 }
6691
6692                 if (t->ret_stack == NULL) {
6693                         atomic_set(&t->trace_overrun, 0);
6694                         t->curr_ret_stack = -1;
6695                         /* Make sure the tasks see the -1 first: */
6696                         smp_wmb();
6697                         t->ret_stack = ret_stack_list[start++];
6698                 }
6699         } while_each_thread(g, t);
6700
6701 unlock:
6702         read_unlock(&tasklist_lock);
6703 free:
6704         for (i = start; i < end; i++)
6705                 kfree(ret_stack_list[i]);
6706         return ret;
6707 }
6708
6709 static void
6710 ftrace_graph_probe_sched_switch(void *ignore, bool preempt,
6711                         struct task_struct *prev, struct task_struct *next)
6712 {
6713         unsigned long long timestamp;
6714         int index;
6715
6716         /*
6717          * Does the user want to count the time a function was asleep.
6718          * If so, do not update the time stamps.
6719          */
6720         if (fgraph_sleep_time)
6721                 return;
6722
6723         timestamp = trace_clock_local();
6724
6725         prev->ftrace_timestamp = timestamp;
6726
6727         /* only process tasks that we timestamped */
6728         if (!next->ftrace_timestamp)
6729                 return;
6730
6731         /*
6732          * Update all the counters in next to make up for the
6733          * time next was sleeping.
6734          */
6735         timestamp -= next->ftrace_timestamp;
6736
6737         for (index = next->curr_ret_stack; index >= 0; index--)
6738                 next->ret_stack[index].calltime += timestamp;
6739 }
6740
6741 /* Allocate a return stack for each task */
6742 static int start_graph_tracing(void)
6743 {
6744         struct ftrace_ret_stack **ret_stack_list;
6745         int ret, cpu;
6746
6747         ret_stack_list = kmalloc(FTRACE_RETSTACK_ALLOC_SIZE *
6748                                 sizeof(struct ftrace_ret_stack *),
6749                                 GFP_KERNEL);
6750
6751         if (!ret_stack_list)
6752                 return -ENOMEM;
6753
6754         /* The cpu_boot init_task->ret_stack will never be freed */
6755         for_each_online_cpu(cpu) {
6756                 if (!idle_task(cpu)->ret_stack)
6757                         ftrace_graph_init_idle_task(idle_task(cpu), cpu);
6758         }
6759
6760         do {
6761                 ret = alloc_retstack_tasklist(ret_stack_list);
6762         } while (ret == -EAGAIN);
6763
6764         if (!ret) {
6765                 ret = register_trace_sched_switch(ftrace_graph_probe_sched_switch, NULL);
6766                 if (ret)
6767                         pr_info("ftrace_graph: Couldn't activate tracepoint"
6768                                 " probe to kernel_sched_switch\n");
6769         }
6770
6771         kfree(ret_stack_list);
6772         return ret;
6773 }
6774
6775 /*
6776  * Hibernation protection.
6777  * The state of the current task is too much unstable during
6778  * suspend/restore to disk. We want to protect against that.
6779  */
6780 static int
6781 ftrace_suspend_notifier_call(struct notifier_block *bl, unsigned long state,
6782                                                         void *unused)
6783 {
6784         switch (state) {
6785         case PM_HIBERNATION_PREPARE:
6786                 pause_graph_tracing();
6787                 break;
6788
6789         case PM_POST_HIBERNATION:
6790                 unpause_graph_tracing();
6791                 break;
6792         }
6793         return NOTIFY_DONE;
6794 }
6795
6796 static int ftrace_graph_entry_test(struct ftrace_graph_ent *trace)
6797 {
6798         if (!ftrace_ops_test(&global_ops, trace->func, NULL))
6799                 return 0;
6800         return __ftrace_graph_entry(trace);
6801 }
6802
6803 /*
6804  * The function graph tracer should only trace the functions defined
6805  * by set_ftrace_filter and set_ftrace_notrace. If another function
6806  * tracer ops is registered, the graph tracer requires testing the
6807  * function against the global ops, and not just trace any function
6808  * that any ftrace_ops registered.
6809  */
6810 static void update_function_graph_func(void)
6811 {
6812         struct ftrace_ops *op;
6813         bool do_test = false;
6814
6815         /*
6816          * The graph and global ops share the same set of functions
6817          * to test. If any other ops is on the list, then
6818          * the graph tracing needs to test if its the function
6819          * it should call.
6820          */
6821         do_for_each_ftrace_op(op, ftrace_ops_list) {
6822                 if (op != &global_ops && op != &graph_ops &&
6823                     op != &ftrace_list_end) {
6824                         do_test = true;
6825                         /* in double loop, break out with goto */
6826                         goto out;
6827                 }
6828         } while_for_each_ftrace_op(op);
6829  out:
6830         if (do_test)
6831                 ftrace_graph_entry = ftrace_graph_entry_test;
6832         else
6833                 ftrace_graph_entry = __ftrace_graph_entry;
6834 }
6835
6836 static struct notifier_block ftrace_suspend_notifier = {
6837         .notifier_call = ftrace_suspend_notifier_call,
6838 };
6839
6840 int register_ftrace_graph(trace_func_graph_ret_t retfunc,
6841                         trace_func_graph_ent_t entryfunc)
6842 {
6843         int ret = 0;
6844
6845         mutex_lock(&ftrace_lock);
6846
6847         /* we currently allow only one tracer registered at a time */
6848         if (ftrace_graph_active) {
6849                 ret = -EBUSY;
6850                 goto out;
6851         }
6852
6853         register_pm_notifier(&ftrace_suspend_notifier);
6854
6855         ftrace_graph_active++;
6856         ret = start_graph_tracing();
6857         if (ret) {
6858                 ftrace_graph_active--;
6859                 goto out;
6860         }
6861
6862         ftrace_graph_return = retfunc;
6863
6864         /*
6865          * Update the indirect function to the entryfunc, and the
6866          * function that gets called to the entry_test first. Then
6867          * call the update fgraph entry function to determine if
6868          * the entryfunc should be called directly or not.
6869          */
6870         __ftrace_graph_entry = entryfunc;
6871         ftrace_graph_entry = ftrace_graph_entry_test;
6872         update_function_graph_func();
6873
6874         ret = ftrace_startup(&graph_ops, FTRACE_START_FUNC_RET);
6875 out:
6876         mutex_unlock(&ftrace_lock);
6877         return ret;
6878 }
6879
6880 void unregister_ftrace_graph(void)
6881 {
6882         mutex_lock(&ftrace_lock);
6883
6884         if (unlikely(!ftrace_graph_active))
6885                 goto out;
6886
6887         ftrace_graph_active--;
6888         ftrace_graph_return = (trace_func_graph_ret_t)ftrace_stub;
6889         ftrace_graph_entry = ftrace_graph_entry_stub;
6890         __ftrace_graph_entry = ftrace_graph_entry_stub;
6891         ftrace_shutdown(&graph_ops, FTRACE_STOP_FUNC_RET);
6892         unregister_pm_notifier(&ftrace_suspend_notifier);
6893         unregister_trace_sched_switch(ftrace_graph_probe_sched_switch, NULL);
6894
6895  out:
6896         mutex_unlock(&ftrace_lock);
6897 }
6898
6899 static DEFINE_PER_CPU(struct ftrace_ret_stack *, idle_ret_stack);
6900
6901 static void
6902 graph_init_task(struct task_struct *t, struct ftrace_ret_stack *ret_stack)
6903 {
6904         atomic_set(&t->trace_overrun, 0);
6905         t->ftrace_timestamp = 0;
6906         /* make curr_ret_stack visible before we add the ret_stack */
6907         smp_wmb();
6908         t->ret_stack = ret_stack;
6909 }
6910
6911 /*
6912  * Allocate a return stack for the idle task. May be the first
6913  * time through, or it may be done by CPU hotplug online.
6914  */
6915 void ftrace_graph_init_idle_task(struct task_struct *t, int cpu)
6916 {
6917         t->curr_ret_stack = -1;
6918         /*
6919          * The idle task has no parent, it either has its own
6920          * stack or no stack at all.
6921          */
6922         if (t->ret_stack)
6923                 WARN_ON(t->ret_stack != per_cpu(idle_ret_stack, cpu));
6924
6925         if (ftrace_graph_active) {
6926                 struct ftrace_ret_stack *ret_stack;
6927
6928                 ret_stack = per_cpu(idle_ret_stack, cpu);
6929                 if (!ret_stack) {
6930                         ret_stack = kmalloc(FTRACE_RETFUNC_DEPTH
6931                                             * sizeof(struct ftrace_ret_stack),
6932                                             GFP_KERNEL);
6933                         if (!ret_stack)
6934                                 return;
6935                         per_cpu(idle_ret_stack, cpu) = ret_stack;
6936                 }
6937                 graph_init_task(t, ret_stack);
6938         }
6939 }
6940
6941 /* Allocate a return stack for newly created task */
6942 void ftrace_graph_init_task(struct task_struct *t)
6943 {
6944         /* Make sure we do not use the parent ret_stack */
6945         t->ret_stack = NULL;
6946         t->curr_ret_stack = -1;
6947
6948         if (ftrace_graph_active) {
6949                 struct ftrace_ret_stack *ret_stack;
6950
6951                 ret_stack = kmalloc(FTRACE_RETFUNC_DEPTH
6952                                 * sizeof(struct ftrace_ret_stack),
6953                                 GFP_KERNEL);
6954                 if (!ret_stack)
6955                         return;
6956                 graph_init_task(t, ret_stack);
6957         }
6958 }
6959
6960 void ftrace_graph_exit_task(struct task_struct *t)
6961 {
6962         struct ftrace_ret_stack *ret_stack = t->ret_stack;
6963
6964         t->ret_stack = NULL;
6965         /* NULL must become visible to IRQs before we free it: */
6966         barrier();
6967
6968         kfree(ret_stack);
6969 }
6970 #endif