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