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