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