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