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