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