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