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