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