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