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