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