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