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