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