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