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