xref: /openbmc/linux/kernel/trace/ftrace.c (revision 7b73a9c8)
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 = div64_ul(rec->time, rec->counter);
530 	if (tracing_thresh && (avg < tracing_thresh))
531 		goto out;
532 #endif
533 
534 	kallsyms_lookup(rec->ip, NULL, NULL, NULL, str);
535 	seq_printf(m, "  %-30.30s  %10lu", str, rec->counter);
536 
537 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
538 	seq_puts(m, "    ");
539 
540 	/* Sample standard deviation (s^2) */
541 	if (rec->counter <= 1)
542 		stddev = 0;
543 	else {
544 		/*
545 		 * Apply Welford's method:
546 		 * s^2 = 1 / (n * (n-1)) * (n * \Sum (x_i)^2 - (\Sum x_i)^2)
547 		 */
548 		stddev = rec->counter * rec->time_squared -
549 			 rec->time * rec->time;
550 
551 		/*
552 		 * Divide only 1000 for ns^2 -> us^2 conversion.
553 		 * trace_print_graph_duration will divide 1000 again.
554 		 */
555 		stddev = div64_ul(stddev,
556 				  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 unsigned long ftrace_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 = ftrace_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 #endif /* CONFIG_DYNAMIC_FTRACE_WITH_DIRECT_CALLS */
2397 
2398 /**
2399  * ftrace_get_addr_new - Get the call address to set to
2400  * @rec:  The ftrace record descriptor
2401  *
2402  * If the record has the FTRACE_FL_REGS set, that means that it
2403  * wants to convert to a callback that saves all regs. If FTRACE_FL_REGS
2404  * is not not set, then it wants to convert to the normal callback.
2405  *
2406  * Returns the address of the trampoline to set to
2407  */
2408 unsigned long ftrace_get_addr_new(struct dyn_ftrace *rec)
2409 {
2410 	struct ftrace_ops *ops;
2411 	unsigned long addr;
2412 
2413 	if ((rec->flags & FTRACE_FL_DIRECT) &&
2414 	    (ftrace_rec_count(rec) == 1)) {
2415 		addr = ftrace_find_rec_direct(rec->ip);
2416 		if (addr)
2417 			return addr;
2418 		WARN_ON_ONCE(1);
2419 	}
2420 
2421 	/* Trampolines take precedence over regs */
2422 	if (rec->flags & FTRACE_FL_TRAMP) {
2423 		ops = ftrace_find_tramp_ops_new(rec);
2424 		if (FTRACE_WARN_ON(!ops || !ops->trampoline)) {
2425 			pr_warn("Bad trampoline accounting at: %p (%pS) (%lx)\n",
2426 				(void *)rec->ip, (void *)rec->ip, rec->flags);
2427 			/* Ftrace is shutting down, return anything */
2428 			return (unsigned long)FTRACE_ADDR;
2429 		}
2430 		return ops->trampoline;
2431 	}
2432 
2433 	if (rec->flags & FTRACE_FL_REGS)
2434 		return (unsigned long)FTRACE_REGS_ADDR;
2435 	else
2436 		return (unsigned long)FTRACE_ADDR;
2437 }
2438 
2439 /**
2440  * ftrace_get_addr_curr - Get the call address that is already there
2441  * @rec:  The ftrace record descriptor
2442  *
2443  * The FTRACE_FL_REGS_EN is set when the record already points to
2444  * a function that saves all the regs. Basically the '_EN' version
2445  * represents the current state of the function.
2446  *
2447  * Returns the address of the trampoline that is currently being called
2448  */
2449 unsigned long ftrace_get_addr_curr(struct dyn_ftrace *rec)
2450 {
2451 	struct ftrace_ops *ops;
2452 	unsigned long addr;
2453 
2454 	/* Direct calls take precedence over trampolines */
2455 	if (rec->flags & FTRACE_FL_DIRECT_EN) {
2456 		addr = ftrace_find_rec_direct(rec->ip);
2457 		if (addr)
2458 			return addr;
2459 		WARN_ON_ONCE(1);
2460 	}
2461 
2462 	/* Trampolines take precedence over regs */
2463 	if (rec->flags & FTRACE_FL_TRAMP_EN) {
2464 		ops = ftrace_find_tramp_ops_curr(rec);
2465 		if (FTRACE_WARN_ON(!ops)) {
2466 			pr_warn("Bad trampoline accounting at: %p (%pS)\n",
2467 				(void *)rec->ip, (void *)rec->ip);
2468 			/* Ftrace is shutting down, return anything */
2469 			return (unsigned long)FTRACE_ADDR;
2470 		}
2471 		return ops->trampoline;
2472 	}
2473 
2474 	if (rec->flags & FTRACE_FL_REGS_EN)
2475 		return (unsigned long)FTRACE_REGS_ADDR;
2476 	else
2477 		return (unsigned long)FTRACE_ADDR;
2478 }
2479 
2480 static int
2481 __ftrace_replace_code(struct dyn_ftrace *rec, bool enable)
2482 {
2483 	unsigned long ftrace_old_addr;
2484 	unsigned long ftrace_addr;
2485 	int ret;
2486 
2487 	ftrace_addr = ftrace_get_addr_new(rec);
2488 
2489 	/* This needs to be done before we call ftrace_update_record */
2490 	ftrace_old_addr = ftrace_get_addr_curr(rec);
2491 
2492 	ret = ftrace_update_record(rec, enable);
2493 
2494 	ftrace_bug_type = FTRACE_BUG_UNKNOWN;
2495 
2496 	switch (ret) {
2497 	case FTRACE_UPDATE_IGNORE:
2498 		return 0;
2499 
2500 	case FTRACE_UPDATE_MAKE_CALL:
2501 		ftrace_bug_type = FTRACE_BUG_CALL;
2502 		return ftrace_make_call(rec, ftrace_addr);
2503 
2504 	case FTRACE_UPDATE_MAKE_NOP:
2505 		ftrace_bug_type = FTRACE_BUG_NOP;
2506 		return ftrace_make_nop(NULL, rec, ftrace_old_addr);
2507 
2508 	case FTRACE_UPDATE_MODIFY_CALL:
2509 		ftrace_bug_type = FTRACE_BUG_UPDATE;
2510 		return ftrace_modify_call(rec, ftrace_old_addr, ftrace_addr);
2511 	}
2512 
2513 	return -1; /* unknown ftrace bug */
2514 }
2515 
2516 void __weak ftrace_replace_code(int mod_flags)
2517 {
2518 	struct dyn_ftrace *rec;
2519 	struct ftrace_page *pg;
2520 	bool enable = mod_flags & FTRACE_MODIFY_ENABLE_FL;
2521 	int schedulable = mod_flags & FTRACE_MODIFY_MAY_SLEEP_FL;
2522 	int failed;
2523 
2524 	if (unlikely(ftrace_disabled))
2525 		return;
2526 
2527 	do_for_each_ftrace_rec(pg, rec) {
2528 
2529 		if (rec->flags & FTRACE_FL_DISABLED)
2530 			continue;
2531 
2532 		failed = __ftrace_replace_code(rec, enable);
2533 		if (failed) {
2534 			ftrace_bug(failed, rec);
2535 			/* Stop processing */
2536 			return;
2537 		}
2538 		if (schedulable)
2539 			cond_resched();
2540 	} while_for_each_ftrace_rec();
2541 }
2542 
2543 struct ftrace_rec_iter {
2544 	struct ftrace_page	*pg;
2545 	int			index;
2546 };
2547 
2548 /**
2549  * ftrace_rec_iter_start, start up iterating over traced functions
2550  *
2551  * Returns an iterator handle that is used to iterate over all
2552  * the records that represent address locations where functions
2553  * are traced.
2554  *
2555  * May return NULL if no records are available.
2556  */
2557 struct ftrace_rec_iter *ftrace_rec_iter_start(void)
2558 {
2559 	/*
2560 	 * We only use a single iterator.
2561 	 * Protected by the ftrace_lock mutex.
2562 	 */
2563 	static struct ftrace_rec_iter ftrace_rec_iter;
2564 	struct ftrace_rec_iter *iter = &ftrace_rec_iter;
2565 
2566 	iter->pg = ftrace_pages_start;
2567 	iter->index = 0;
2568 
2569 	/* Could have empty pages */
2570 	while (iter->pg && !iter->pg->index)
2571 		iter->pg = iter->pg->next;
2572 
2573 	if (!iter->pg)
2574 		return NULL;
2575 
2576 	return iter;
2577 }
2578 
2579 /**
2580  * ftrace_rec_iter_next, get the next record to process.
2581  * @iter: The handle to the iterator.
2582  *
2583  * Returns the next iterator after the given iterator @iter.
2584  */
2585 struct ftrace_rec_iter *ftrace_rec_iter_next(struct ftrace_rec_iter *iter)
2586 {
2587 	iter->index++;
2588 
2589 	if (iter->index >= iter->pg->index) {
2590 		iter->pg = iter->pg->next;
2591 		iter->index = 0;
2592 
2593 		/* Could have empty pages */
2594 		while (iter->pg && !iter->pg->index)
2595 			iter->pg = iter->pg->next;
2596 	}
2597 
2598 	if (!iter->pg)
2599 		return NULL;
2600 
2601 	return iter;
2602 }
2603 
2604 /**
2605  * ftrace_rec_iter_record, get the record at the iterator location
2606  * @iter: The current iterator location
2607  *
2608  * Returns the record that the current @iter is at.
2609  */
2610 struct dyn_ftrace *ftrace_rec_iter_record(struct ftrace_rec_iter *iter)
2611 {
2612 	return &iter->pg->records[iter->index];
2613 }
2614 
2615 static int
2616 ftrace_nop_initialize(struct module *mod, struct dyn_ftrace *rec)
2617 {
2618 	int ret;
2619 
2620 	if (unlikely(ftrace_disabled))
2621 		return 0;
2622 
2623 	ret = ftrace_init_nop(mod, rec);
2624 	if (ret) {
2625 		ftrace_bug_type = FTRACE_BUG_INIT;
2626 		ftrace_bug(ret, rec);
2627 		return 0;
2628 	}
2629 	return 1;
2630 }
2631 
2632 /*
2633  * archs can override this function if they must do something
2634  * before the modifying code is performed.
2635  */
2636 int __weak ftrace_arch_code_modify_prepare(void)
2637 {
2638 	return 0;
2639 }
2640 
2641 /*
2642  * archs can override this function if they must do something
2643  * after the modifying code is performed.
2644  */
2645 int __weak ftrace_arch_code_modify_post_process(void)
2646 {
2647 	return 0;
2648 }
2649 
2650 void ftrace_modify_all_code(int command)
2651 {
2652 	int update = command & FTRACE_UPDATE_TRACE_FUNC;
2653 	int mod_flags = 0;
2654 	int err = 0;
2655 
2656 	if (command & FTRACE_MAY_SLEEP)
2657 		mod_flags = FTRACE_MODIFY_MAY_SLEEP_FL;
2658 
2659 	/*
2660 	 * If the ftrace_caller calls a ftrace_ops func directly,
2661 	 * we need to make sure that it only traces functions it
2662 	 * expects to trace. When doing the switch of functions,
2663 	 * we need to update to the ftrace_ops_list_func first
2664 	 * before the transition between old and new calls are set,
2665 	 * as the ftrace_ops_list_func will check the ops hashes
2666 	 * to make sure the ops are having the right functions
2667 	 * traced.
2668 	 */
2669 	if (update) {
2670 		err = ftrace_update_ftrace_func(ftrace_ops_list_func);
2671 		if (FTRACE_WARN_ON(err))
2672 			return;
2673 	}
2674 
2675 	if (command & FTRACE_UPDATE_CALLS)
2676 		ftrace_replace_code(mod_flags | FTRACE_MODIFY_ENABLE_FL);
2677 	else if (command & FTRACE_DISABLE_CALLS)
2678 		ftrace_replace_code(mod_flags);
2679 
2680 	if (update && ftrace_trace_function != ftrace_ops_list_func) {
2681 		function_trace_op = set_function_trace_op;
2682 		smp_wmb();
2683 		/* If irqs are disabled, we are in stop machine */
2684 		if (!irqs_disabled())
2685 			smp_call_function(ftrace_sync_ipi, NULL, 1);
2686 		err = ftrace_update_ftrace_func(ftrace_trace_function);
2687 		if (FTRACE_WARN_ON(err))
2688 			return;
2689 	}
2690 
2691 	if (command & FTRACE_START_FUNC_RET)
2692 		err = ftrace_enable_ftrace_graph_caller();
2693 	else if (command & FTRACE_STOP_FUNC_RET)
2694 		err = ftrace_disable_ftrace_graph_caller();
2695 	FTRACE_WARN_ON(err);
2696 }
2697 
2698 static int __ftrace_modify_code(void *data)
2699 {
2700 	int *command = data;
2701 
2702 	ftrace_modify_all_code(*command);
2703 
2704 	return 0;
2705 }
2706 
2707 /**
2708  * ftrace_run_stop_machine, go back to the stop machine method
2709  * @command: The command to tell ftrace what to do
2710  *
2711  * If an arch needs to fall back to the stop machine method, the
2712  * it can call this function.
2713  */
2714 void ftrace_run_stop_machine(int command)
2715 {
2716 	stop_machine(__ftrace_modify_code, &command, NULL);
2717 }
2718 
2719 /**
2720  * arch_ftrace_update_code, modify the code to trace or not trace
2721  * @command: The command that needs to be done
2722  *
2723  * Archs can override this function if it does not need to
2724  * run stop_machine() to modify code.
2725  */
2726 void __weak arch_ftrace_update_code(int command)
2727 {
2728 	ftrace_run_stop_machine(command);
2729 }
2730 
2731 static void ftrace_run_update_code(int command)
2732 {
2733 	int ret;
2734 
2735 	ret = ftrace_arch_code_modify_prepare();
2736 	FTRACE_WARN_ON(ret);
2737 	if (ret)
2738 		return;
2739 
2740 	/*
2741 	 * By default we use stop_machine() to modify the code.
2742 	 * But archs can do what ever they want as long as it
2743 	 * is safe. The stop_machine() is the safest, but also
2744 	 * produces the most overhead.
2745 	 */
2746 	arch_ftrace_update_code(command);
2747 
2748 	ret = ftrace_arch_code_modify_post_process();
2749 	FTRACE_WARN_ON(ret);
2750 }
2751 
2752 static void ftrace_run_modify_code(struct ftrace_ops *ops, int command,
2753 				   struct ftrace_ops_hash *old_hash)
2754 {
2755 	ops->flags |= FTRACE_OPS_FL_MODIFYING;
2756 	ops->old_hash.filter_hash = old_hash->filter_hash;
2757 	ops->old_hash.notrace_hash = old_hash->notrace_hash;
2758 	ftrace_run_update_code(command);
2759 	ops->old_hash.filter_hash = NULL;
2760 	ops->old_hash.notrace_hash = NULL;
2761 	ops->flags &= ~FTRACE_OPS_FL_MODIFYING;
2762 }
2763 
2764 static ftrace_func_t saved_ftrace_func;
2765 static int ftrace_start_up;
2766 
2767 void __weak arch_ftrace_trampoline_free(struct ftrace_ops *ops)
2768 {
2769 }
2770 
2771 static void ftrace_startup_enable(int command)
2772 {
2773 	if (saved_ftrace_func != ftrace_trace_function) {
2774 		saved_ftrace_func = ftrace_trace_function;
2775 		command |= FTRACE_UPDATE_TRACE_FUNC;
2776 	}
2777 
2778 	if (!command || !ftrace_enabled)
2779 		return;
2780 
2781 	ftrace_run_update_code(command);
2782 }
2783 
2784 static void ftrace_startup_all(int command)
2785 {
2786 	update_all_ops = true;
2787 	ftrace_startup_enable(command);
2788 	update_all_ops = false;
2789 }
2790 
2791 int ftrace_startup(struct ftrace_ops *ops, int command)
2792 {
2793 	int ret;
2794 
2795 	if (unlikely(ftrace_disabled))
2796 		return -ENODEV;
2797 
2798 	ret = __register_ftrace_function(ops);
2799 	if (ret)
2800 		return ret;
2801 
2802 	ftrace_start_up++;
2803 
2804 	/*
2805 	 * Note that ftrace probes uses this to start up
2806 	 * and modify functions it will probe. But we still
2807 	 * set the ADDING flag for modification, as probes
2808 	 * do not have trampolines. If they add them in the
2809 	 * future, then the probes will need to distinguish
2810 	 * between adding and updating probes.
2811 	 */
2812 	ops->flags |= FTRACE_OPS_FL_ENABLED | FTRACE_OPS_FL_ADDING;
2813 
2814 	ret = ftrace_hash_ipmodify_enable(ops);
2815 	if (ret < 0) {
2816 		/* Rollback registration process */
2817 		__unregister_ftrace_function(ops);
2818 		ftrace_start_up--;
2819 		ops->flags &= ~FTRACE_OPS_FL_ENABLED;
2820 		return ret;
2821 	}
2822 
2823 	if (ftrace_hash_rec_enable(ops, 1))
2824 		command |= FTRACE_UPDATE_CALLS;
2825 
2826 	ftrace_startup_enable(command);
2827 
2828 	ops->flags &= ~FTRACE_OPS_FL_ADDING;
2829 
2830 	return 0;
2831 }
2832 
2833 int ftrace_shutdown(struct ftrace_ops *ops, int command)
2834 {
2835 	int ret;
2836 
2837 	if (unlikely(ftrace_disabled))
2838 		return -ENODEV;
2839 
2840 	ret = __unregister_ftrace_function(ops);
2841 	if (ret)
2842 		return ret;
2843 
2844 	ftrace_start_up--;
2845 	/*
2846 	 * Just warn in case of unbalance, no need to kill ftrace, it's not
2847 	 * critical but the ftrace_call callers may be never nopped again after
2848 	 * further ftrace uses.
2849 	 */
2850 	WARN_ON_ONCE(ftrace_start_up < 0);
2851 
2852 	/* Disabling ipmodify never fails */
2853 	ftrace_hash_ipmodify_disable(ops);
2854 
2855 	if (ftrace_hash_rec_disable(ops, 1))
2856 		command |= FTRACE_UPDATE_CALLS;
2857 
2858 	ops->flags &= ~FTRACE_OPS_FL_ENABLED;
2859 
2860 	if (saved_ftrace_func != ftrace_trace_function) {
2861 		saved_ftrace_func = ftrace_trace_function;
2862 		command |= FTRACE_UPDATE_TRACE_FUNC;
2863 	}
2864 
2865 	if (!command || !ftrace_enabled) {
2866 		/*
2867 		 * If these are dynamic or per_cpu ops, they still
2868 		 * need their data freed. Since, function tracing is
2869 		 * not currently active, we can just free them
2870 		 * without synchronizing all CPUs.
2871 		 */
2872 		if (ops->flags & FTRACE_OPS_FL_DYNAMIC)
2873 			goto free_ops;
2874 
2875 		return 0;
2876 	}
2877 
2878 	/*
2879 	 * If the ops uses a trampoline, then it needs to be
2880 	 * tested first on update.
2881 	 */
2882 	ops->flags |= FTRACE_OPS_FL_REMOVING;
2883 	removed_ops = ops;
2884 
2885 	/* The trampoline logic checks the old hashes */
2886 	ops->old_hash.filter_hash = ops->func_hash->filter_hash;
2887 	ops->old_hash.notrace_hash = ops->func_hash->notrace_hash;
2888 
2889 	ftrace_run_update_code(command);
2890 
2891 	/*
2892 	 * If there's no more ops registered with ftrace, run a
2893 	 * sanity check to make sure all rec flags are cleared.
2894 	 */
2895 	if (rcu_dereference_protected(ftrace_ops_list,
2896 			lockdep_is_held(&ftrace_lock)) == &ftrace_list_end) {
2897 		struct ftrace_page *pg;
2898 		struct dyn_ftrace *rec;
2899 
2900 		do_for_each_ftrace_rec(pg, rec) {
2901 			if (FTRACE_WARN_ON_ONCE(rec->flags & ~FTRACE_FL_DISABLED))
2902 				pr_warn("  %pS flags:%lx\n",
2903 					(void *)rec->ip, rec->flags);
2904 		} while_for_each_ftrace_rec();
2905 	}
2906 
2907 	ops->old_hash.filter_hash = NULL;
2908 	ops->old_hash.notrace_hash = NULL;
2909 
2910 	removed_ops = NULL;
2911 	ops->flags &= ~FTRACE_OPS_FL_REMOVING;
2912 
2913 	/*
2914 	 * Dynamic ops may be freed, we must make sure that all
2915 	 * callers are done before leaving this function.
2916 	 * The same goes for freeing the per_cpu data of the per_cpu
2917 	 * ops.
2918 	 */
2919 	if (ops->flags & FTRACE_OPS_FL_DYNAMIC) {
2920 		/*
2921 		 * We need to do a hard force of sched synchronization.
2922 		 * This is because we use preempt_disable() to do RCU, but
2923 		 * the function tracers can be called where RCU is not watching
2924 		 * (like before user_exit()). We can not rely on the RCU
2925 		 * infrastructure to do the synchronization, thus we must do it
2926 		 * ourselves.
2927 		 */
2928 		schedule_on_each_cpu(ftrace_sync);
2929 
2930 		/*
2931 		 * When the kernel is preeptive, tasks can be preempted
2932 		 * while on a ftrace trampoline. Just scheduling a task on
2933 		 * a CPU is not good enough to flush them. Calling
2934 		 * synchornize_rcu_tasks() will wait for those tasks to
2935 		 * execute and either schedule voluntarily or enter user space.
2936 		 */
2937 		if (IS_ENABLED(CONFIG_PREEMPTION))
2938 			synchronize_rcu_tasks();
2939 
2940  free_ops:
2941 		arch_ftrace_trampoline_free(ops);
2942 	}
2943 
2944 	return 0;
2945 }
2946 
2947 static void ftrace_startup_sysctl(void)
2948 {
2949 	int command;
2950 
2951 	if (unlikely(ftrace_disabled))
2952 		return;
2953 
2954 	/* Force update next time */
2955 	saved_ftrace_func = NULL;
2956 	/* ftrace_start_up is true if we want ftrace running */
2957 	if (ftrace_start_up) {
2958 		command = FTRACE_UPDATE_CALLS;
2959 		if (ftrace_graph_active)
2960 			command |= FTRACE_START_FUNC_RET;
2961 		ftrace_startup_enable(command);
2962 	}
2963 }
2964 
2965 static void ftrace_shutdown_sysctl(void)
2966 {
2967 	int command;
2968 
2969 	if (unlikely(ftrace_disabled))
2970 		return;
2971 
2972 	/* ftrace_start_up is true if ftrace is running */
2973 	if (ftrace_start_up) {
2974 		command = FTRACE_DISABLE_CALLS;
2975 		if (ftrace_graph_active)
2976 			command |= FTRACE_STOP_FUNC_RET;
2977 		ftrace_run_update_code(command);
2978 	}
2979 }
2980 
2981 static u64		ftrace_update_time;
2982 unsigned long		ftrace_update_tot_cnt;
2983 unsigned long		ftrace_number_of_pages;
2984 unsigned long		ftrace_number_of_groups;
2985 
2986 static inline int ops_traces_mod(struct ftrace_ops *ops)
2987 {
2988 	/*
2989 	 * Filter_hash being empty will default to trace module.
2990 	 * But notrace hash requires a test of individual module functions.
2991 	 */
2992 	return ftrace_hash_empty(ops->func_hash->filter_hash) &&
2993 		ftrace_hash_empty(ops->func_hash->notrace_hash);
2994 }
2995 
2996 /*
2997  * Check if the current ops references the record.
2998  *
2999  * If the ops traces all functions, then it was already accounted for.
3000  * If the ops does not trace the current record function, skip it.
3001  * If the ops ignores the function via notrace filter, skip it.
3002  */
3003 static inline bool
3004 ops_references_rec(struct ftrace_ops *ops, struct dyn_ftrace *rec)
3005 {
3006 	/* If ops isn't enabled, ignore it */
3007 	if (!(ops->flags & FTRACE_OPS_FL_ENABLED))
3008 		return false;
3009 
3010 	/* If ops traces all then it includes this function */
3011 	if (ops_traces_mod(ops))
3012 		return true;
3013 
3014 	/* The function must be in the filter */
3015 	if (!ftrace_hash_empty(ops->func_hash->filter_hash) &&
3016 	    !__ftrace_lookup_ip(ops->func_hash->filter_hash, rec->ip))
3017 		return false;
3018 
3019 	/* If in notrace hash, we ignore it too */
3020 	if (ftrace_lookup_ip(ops->func_hash->notrace_hash, rec->ip))
3021 		return false;
3022 
3023 	return true;
3024 }
3025 
3026 static int ftrace_update_code(struct module *mod, struct ftrace_page *new_pgs)
3027 {
3028 	struct ftrace_page *pg;
3029 	struct dyn_ftrace *p;
3030 	u64 start, stop;
3031 	unsigned long update_cnt = 0;
3032 	unsigned long rec_flags = 0;
3033 	int i;
3034 
3035 	start = ftrace_now(raw_smp_processor_id());
3036 
3037 	/*
3038 	 * When a module is loaded, this function is called to convert
3039 	 * the calls to mcount in its text to nops, and also to create
3040 	 * an entry in the ftrace data. Now, if ftrace is activated
3041 	 * after this call, but before the module sets its text to
3042 	 * read-only, the modification of enabling ftrace can fail if
3043 	 * the read-only is done while ftrace is converting the calls.
3044 	 * To prevent this, the module's records are set as disabled
3045 	 * and will be enabled after the call to set the module's text
3046 	 * to read-only.
3047 	 */
3048 	if (mod)
3049 		rec_flags |= FTRACE_FL_DISABLED;
3050 
3051 	for (pg = new_pgs; pg; pg = pg->next) {
3052 
3053 		for (i = 0; i < pg->index; i++) {
3054 
3055 			/* If something went wrong, bail without enabling anything */
3056 			if (unlikely(ftrace_disabled))
3057 				return -1;
3058 
3059 			p = &pg->records[i];
3060 			p->flags = rec_flags;
3061 
3062 			/*
3063 			 * Do the initial record conversion from mcount jump
3064 			 * to the NOP instructions.
3065 			 */
3066 			if (!__is_defined(CC_USING_NOP_MCOUNT) &&
3067 			    !ftrace_nop_initialize(mod, p))
3068 				break;
3069 
3070 			update_cnt++;
3071 		}
3072 	}
3073 
3074 	stop = ftrace_now(raw_smp_processor_id());
3075 	ftrace_update_time = stop - start;
3076 	ftrace_update_tot_cnt += update_cnt;
3077 
3078 	return 0;
3079 }
3080 
3081 static int ftrace_allocate_records(struct ftrace_page *pg, int count)
3082 {
3083 	int order;
3084 	int cnt;
3085 
3086 	if (WARN_ON(!count))
3087 		return -EINVAL;
3088 
3089 	order = get_count_order(DIV_ROUND_UP(count, ENTRIES_PER_PAGE));
3090 
3091 	/*
3092 	 * We want to fill as much as possible. No more than a page
3093 	 * may be empty.
3094 	 */
3095 	while ((PAGE_SIZE << order) / ENTRY_SIZE >= count + ENTRIES_PER_PAGE)
3096 		order--;
3097 
3098  again:
3099 	pg->records = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, order);
3100 
3101 	if (!pg->records) {
3102 		/* if we can't allocate this size, try something smaller */
3103 		if (!order)
3104 			return -ENOMEM;
3105 		order >>= 1;
3106 		goto again;
3107 	}
3108 
3109 	ftrace_number_of_pages += 1 << order;
3110 	ftrace_number_of_groups++;
3111 
3112 	cnt = (PAGE_SIZE << order) / ENTRY_SIZE;
3113 	pg->size = cnt;
3114 
3115 	if (cnt > count)
3116 		cnt = count;
3117 
3118 	return cnt;
3119 }
3120 
3121 static struct ftrace_page *
3122 ftrace_allocate_pages(unsigned long num_to_init)
3123 {
3124 	struct ftrace_page *start_pg;
3125 	struct ftrace_page *pg;
3126 	int order;
3127 	int cnt;
3128 
3129 	if (!num_to_init)
3130 		return NULL;
3131 
3132 	start_pg = pg = kzalloc(sizeof(*pg), GFP_KERNEL);
3133 	if (!pg)
3134 		return NULL;
3135 
3136 	/*
3137 	 * Try to allocate as much as possible in one continues
3138 	 * location that fills in all of the space. We want to
3139 	 * waste as little space as possible.
3140 	 */
3141 	for (;;) {
3142 		cnt = ftrace_allocate_records(pg, num_to_init);
3143 		if (cnt < 0)
3144 			goto free_pages;
3145 
3146 		num_to_init -= cnt;
3147 		if (!num_to_init)
3148 			break;
3149 
3150 		pg->next = kzalloc(sizeof(*pg), GFP_KERNEL);
3151 		if (!pg->next)
3152 			goto free_pages;
3153 
3154 		pg = pg->next;
3155 	}
3156 
3157 	return start_pg;
3158 
3159  free_pages:
3160 	pg = start_pg;
3161 	while (pg) {
3162 		order = get_count_order(pg->size / ENTRIES_PER_PAGE);
3163 		free_pages((unsigned long)pg->records, order);
3164 		start_pg = pg->next;
3165 		kfree(pg);
3166 		pg = start_pg;
3167 		ftrace_number_of_pages -= 1 << order;
3168 		ftrace_number_of_groups--;
3169 	}
3170 	pr_info("ftrace: FAILED to allocate memory for functions\n");
3171 	return NULL;
3172 }
3173 
3174 #define FTRACE_BUFF_MAX (KSYM_SYMBOL_LEN+4) /* room for wildcards */
3175 
3176 struct ftrace_iterator {
3177 	loff_t				pos;
3178 	loff_t				func_pos;
3179 	loff_t				mod_pos;
3180 	struct ftrace_page		*pg;
3181 	struct dyn_ftrace		*func;
3182 	struct ftrace_func_probe	*probe;
3183 	struct ftrace_func_entry	*probe_entry;
3184 	struct trace_parser		parser;
3185 	struct ftrace_hash		*hash;
3186 	struct ftrace_ops		*ops;
3187 	struct trace_array		*tr;
3188 	struct list_head		*mod_list;
3189 	int				pidx;
3190 	int				idx;
3191 	unsigned			flags;
3192 };
3193 
3194 static void *
3195 t_probe_next(struct seq_file *m, loff_t *pos)
3196 {
3197 	struct ftrace_iterator *iter = m->private;
3198 	struct trace_array *tr = iter->ops->private;
3199 	struct list_head *func_probes;
3200 	struct ftrace_hash *hash;
3201 	struct list_head *next;
3202 	struct hlist_node *hnd = NULL;
3203 	struct hlist_head *hhd;
3204 	int size;
3205 
3206 	(*pos)++;
3207 	iter->pos = *pos;
3208 
3209 	if (!tr)
3210 		return NULL;
3211 
3212 	func_probes = &tr->func_probes;
3213 	if (list_empty(func_probes))
3214 		return NULL;
3215 
3216 	if (!iter->probe) {
3217 		next = func_probes->next;
3218 		iter->probe = list_entry(next, struct ftrace_func_probe, list);
3219 	}
3220 
3221 	if (iter->probe_entry)
3222 		hnd = &iter->probe_entry->hlist;
3223 
3224 	hash = iter->probe->ops.func_hash->filter_hash;
3225 
3226 	/*
3227 	 * A probe being registered may temporarily have an empty hash
3228 	 * and it's at the end of the func_probes list.
3229 	 */
3230 	if (!hash || hash == EMPTY_HASH)
3231 		return NULL;
3232 
3233 	size = 1 << hash->size_bits;
3234 
3235  retry:
3236 	if (iter->pidx >= size) {
3237 		if (iter->probe->list.next == func_probes)
3238 			return NULL;
3239 		next = iter->probe->list.next;
3240 		iter->probe = list_entry(next, struct ftrace_func_probe, list);
3241 		hash = iter->probe->ops.func_hash->filter_hash;
3242 		size = 1 << hash->size_bits;
3243 		iter->pidx = 0;
3244 	}
3245 
3246 	hhd = &hash->buckets[iter->pidx];
3247 
3248 	if (hlist_empty(hhd)) {
3249 		iter->pidx++;
3250 		hnd = NULL;
3251 		goto retry;
3252 	}
3253 
3254 	if (!hnd)
3255 		hnd = hhd->first;
3256 	else {
3257 		hnd = hnd->next;
3258 		if (!hnd) {
3259 			iter->pidx++;
3260 			goto retry;
3261 		}
3262 	}
3263 
3264 	if (WARN_ON_ONCE(!hnd))
3265 		return NULL;
3266 
3267 	iter->probe_entry = hlist_entry(hnd, struct ftrace_func_entry, hlist);
3268 
3269 	return iter;
3270 }
3271 
3272 static void *t_probe_start(struct seq_file *m, loff_t *pos)
3273 {
3274 	struct ftrace_iterator *iter = m->private;
3275 	void *p = NULL;
3276 	loff_t l;
3277 
3278 	if (!(iter->flags & FTRACE_ITER_DO_PROBES))
3279 		return NULL;
3280 
3281 	if (iter->mod_pos > *pos)
3282 		return NULL;
3283 
3284 	iter->probe = NULL;
3285 	iter->probe_entry = NULL;
3286 	iter->pidx = 0;
3287 	for (l = 0; l <= (*pos - iter->mod_pos); ) {
3288 		p = t_probe_next(m, &l);
3289 		if (!p)
3290 			break;
3291 	}
3292 	if (!p)
3293 		return NULL;
3294 
3295 	/* Only set this if we have an item */
3296 	iter->flags |= FTRACE_ITER_PROBE;
3297 
3298 	return iter;
3299 }
3300 
3301 static int
3302 t_probe_show(struct seq_file *m, struct ftrace_iterator *iter)
3303 {
3304 	struct ftrace_func_entry *probe_entry;
3305 	struct ftrace_probe_ops *probe_ops;
3306 	struct ftrace_func_probe *probe;
3307 
3308 	probe = iter->probe;
3309 	probe_entry = iter->probe_entry;
3310 
3311 	if (WARN_ON_ONCE(!probe || !probe_entry))
3312 		return -EIO;
3313 
3314 	probe_ops = probe->probe_ops;
3315 
3316 	if (probe_ops->print)
3317 		return probe_ops->print(m, probe_entry->ip, probe_ops, probe->data);
3318 
3319 	seq_printf(m, "%ps:%ps\n", (void *)probe_entry->ip,
3320 		   (void *)probe_ops->func);
3321 
3322 	return 0;
3323 }
3324 
3325 static void *
3326 t_mod_next(struct seq_file *m, loff_t *pos)
3327 {
3328 	struct ftrace_iterator *iter = m->private;
3329 	struct trace_array *tr = iter->tr;
3330 
3331 	(*pos)++;
3332 	iter->pos = *pos;
3333 
3334 	iter->mod_list = iter->mod_list->next;
3335 
3336 	if (iter->mod_list == &tr->mod_trace ||
3337 	    iter->mod_list == &tr->mod_notrace) {
3338 		iter->flags &= ~FTRACE_ITER_MOD;
3339 		return NULL;
3340 	}
3341 
3342 	iter->mod_pos = *pos;
3343 
3344 	return iter;
3345 }
3346 
3347 static void *t_mod_start(struct seq_file *m, loff_t *pos)
3348 {
3349 	struct ftrace_iterator *iter = m->private;
3350 	void *p = NULL;
3351 	loff_t l;
3352 
3353 	if (iter->func_pos > *pos)
3354 		return NULL;
3355 
3356 	iter->mod_pos = iter->func_pos;
3357 
3358 	/* probes are only available if tr is set */
3359 	if (!iter->tr)
3360 		return NULL;
3361 
3362 	for (l = 0; l <= (*pos - iter->func_pos); ) {
3363 		p = t_mod_next(m, &l);
3364 		if (!p)
3365 			break;
3366 	}
3367 	if (!p) {
3368 		iter->flags &= ~FTRACE_ITER_MOD;
3369 		return t_probe_start(m, pos);
3370 	}
3371 
3372 	/* Only set this if we have an item */
3373 	iter->flags |= FTRACE_ITER_MOD;
3374 
3375 	return iter;
3376 }
3377 
3378 static int
3379 t_mod_show(struct seq_file *m, struct ftrace_iterator *iter)
3380 {
3381 	struct ftrace_mod_load *ftrace_mod;
3382 	struct trace_array *tr = iter->tr;
3383 
3384 	if (WARN_ON_ONCE(!iter->mod_list) ||
3385 			 iter->mod_list == &tr->mod_trace ||
3386 			 iter->mod_list == &tr->mod_notrace)
3387 		return -EIO;
3388 
3389 	ftrace_mod = list_entry(iter->mod_list, struct ftrace_mod_load, list);
3390 
3391 	if (ftrace_mod->func)
3392 		seq_printf(m, "%s", ftrace_mod->func);
3393 	else
3394 		seq_putc(m, '*');
3395 
3396 	seq_printf(m, ":mod:%s\n", ftrace_mod->module);
3397 
3398 	return 0;
3399 }
3400 
3401 static void *
3402 t_func_next(struct seq_file *m, loff_t *pos)
3403 {
3404 	struct ftrace_iterator *iter = m->private;
3405 	struct dyn_ftrace *rec = NULL;
3406 
3407 	(*pos)++;
3408 
3409  retry:
3410 	if (iter->idx >= iter->pg->index) {
3411 		if (iter->pg->next) {
3412 			iter->pg = iter->pg->next;
3413 			iter->idx = 0;
3414 			goto retry;
3415 		}
3416 	} else {
3417 		rec = &iter->pg->records[iter->idx++];
3418 		if (((iter->flags & (FTRACE_ITER_FILTER | FTRACE_ITER_NOTRACE)) &&
3419 		     !ftrace_lookup_ip(iter->hash, rec->ip)) ||
3420 
3421 		    ((iter->flags & FTRACE_ITER_ENABLED) &&
3422 		     !(rec->flags & FTRACE_FL_ENABLED))) {
3423 
3424 			rec = NULL;
3425 			goto retry;
3426 		}
3427 	}
3428 
3429 	if (!rec)
3430 		return NULL;
3431 
3432 	iter->pos = iter->func_pos = *pos;
3433 	iter->func = rec;
3434 
3435 	return iter;
3436 }
3437 
3438 static void *
3439 t_next(struct seq_file *m, void *v, loff_t *pos)
3440 {
3441 	struct ftrace_iterator *iter = m->private;
3442 	loff_t l = *pos; /* t_probe_start() must use original pos */
3443 	void *ret;
3444 
3445 	if (unlikely(ftrace_disabled))
3446 		return NULL;
3447 
3448 	if (iter->flags & FTRACE_ITER_PROBE)
3449 		return t_probe_next(m, pos);
3450 
3451 	if (iter->flags & FTRACE_ITER_MOD)
3452 		return t_mod_next(m, pos);
3453 
3454 	if (iter->flags & FTRACE_ITER_PRINTALL) {
3455 		/* next must increment pos, and t_probe_start does not */
3456 		(*pos)++;
3457 		return t_mod_start(m, &l);
3458 	}
3459 
3460 	ret = t_func_next(m, pos);
3461 
3462 	if (!ret)
3463 		return t_mod_start(m, &l);
3464 
3465 	return ret;
3466 }
3467 
3468 static void reset_iter_read(struct ftrace_iterator *iter)
3469 {
3470 	iter->pos = 0;
3471 	iter->func_pos = 0;
3472 	iter->flags &= ~(FTRACE_ITER_PRINTALL | FTRACE_ITER_PROBE | FTRACE_ITER_MOD);
3473 }
3474 
3475 static void *t_start(struct seq_file *m, loff_t *pos)
3476 {
3477 	struct ftrace_iterator *iter = m->private;
3478 	void *p = NULL;
3479 	loff_t l;
3480 
3481 	mutex_lock(&ftrace_lock);
3482 
3483 	if (unlikely(ftrace_disabled))
3484 		return NULL;
3485 
3486 	/*
3487 	 * If an lseek was done, then reset and start from beginning.
3488 	 */
3489 	if (*pos < iter->pos)
3490 		reset_iter_read(iter);
3491 
3492 	/*
3493 	 * For set_ftrace_filter reading, if we have the filter
3494 	 * off, we can short cut and just print out that all
3495 	 * functions are enabled.
3496 	 */
3497 	if ((iter->flags & (FTRACE_ITER_FILTER | FTRACE_ITER_NOTRACE)) &&
3498 	    ftrace_hash_empty(iter->hash)) {
3499 		iter->func_pos = 1; /* Account for the message */
3500 		if (*pos > 0)
3501 			return t_mod_start(m, pos);
3502 		iter->flags |= FTRACE_ITER_PRINTALL;
3503 		/* reset in case of seek/pread */
3504 		iter->flags &= ~FTRACE_ITER_PROBE;
3505 		return iter;
3506 	}
3507 
3508 	if (iter->flags & FTRACE_ITER_MOD)
3509 		return t_mod_start(m, pos);
3510 
3511 	/*
3512 	 * Unfortunately, we need to restart at ftrace_pages_start
3513 	 * every time we let go of the ftrace_mutex. This is because
3514 	 * those pointers can change without the lock.
3515 	 */
3516 	iter->pg = ftrace_pages_start;
3517 	iter->idx = 0;
3518 	for (l = 0; l <= *pos; ) {
3519 		p = t_func_next(m, &l);
3520 		if (!p)
3521 			break;
3522 	}
3523 
3524 	if (!p)
3525 		return t_mod_start(m, pos);
3526 
3527 	return iter;
3528 }
3529 
3530 static void t_stop(struct seq_file *m, void *p)
3531 {
3532 	mutex_unlock(&ftrace_lock);
3533 }
3534 
3535 void * __weak
3536 arch_ftrace_trampoline_func(struct ftrace_ops *ops, struct dyn_ftrace *rec)
3537 {
3538 	return NULL;
3539 }
3540 
3541 static void add_trampoline_func(struct seq_file *m, struct ftrace_ops *ops,
3542 				struct dyn_ftrace *rec)
3543 {
3544 	void *ptr;
3545 
3546 	ptr = arch_ftrace_trampoline_func(ops, rec);
3547 	if (ptr)
3548 		seq_printf(m, " ->%pS", ptr);
3549 }
3550 
3551 static int t_show(struct seq_file *m, void *v)
3552 {
3553 	struct ftrace_iterator *iter = m->private;
3554 	struct dyn_ftrace *rec;
3555 
3556 	if (iter->flags & FTRACE_ITER_PROBE)
3557 		return t_probe_show(m, iter);
3558 
3559 	if (iter->flags & FTRACE_ITER_MOD)
3560 		return t_mod_show(m, iter);
3561 
3562 	if (iter->flags & FTRACE_ITER_PRINTALL) {
3563 		if (iter->flags & FTRACE_ITER_NOTRACE)
3564 			seq_puts(m, "#### no functions disabled ####\n");
3565 		else
3566 			seq_puts(m, "#### all functions enabled ####\n");
3567 		return 0;
3568 	}
3569 
3570 	rec = iter->func;
3571 
3572 	if (!rec)
3573 		return 0;
3574 
3575 	seq_printf(m, "%ps", (void *)rec->ip);
3576 	if (iter->flags & FTRACE_ITER_ENABLED) {
3577 		struct ftrace_ops *ops;
3578 
3579 		seq_printf(m, " (%ld)%s%s%s",
3580 			   ftrace_rec_count(rec),
3581 			   rec->flags & FTRACE_FL_REGS ? " R" : "  ",
3582 			   rec->flags & FTRACE_FL_IPMODIFY ? " I" : "  ",
3583 			   rec->flags & FTRACE_FL_DIRECT ? " D" : "  ");
3584 		if (rec->flags & FTRACE_FL_TRAMP_EN) {
3585 			ops = ftrace_find_tramp_ops_any(rec);
3586 			if (ops) {
3587 				do {
3588 					seq_printf(m, "\ttramp: %pS (%pS)",
3589 						   (void *)ops->trampoline,
3590 						   (void *)ops->func);
3591 					add_trampoline_func(m, ops, rec);
3592 					ops = ftrace_find_tramp_ops_next(rec, ops);
3593 				} while (ops);
3594 			} else
3595 				seq_puts(m, "\ttramp: ERROR!");
3596 		} else {
3597 			add_trampoline_func(m, NULL, rec);
3598 		}
3599 		if (rec->flags & FTRACE_FL_DIRECT) {
3600 			unsigned long direct;
3601 
3602 			direct = ftrace_find_rec_direct(rec->ip);
3603 			if (direct)
3604 				seq_printf(m, "\n\tdirect-->%pS", (void *)direct);
3605 		}
3606 	}
3607 
3608 	seq_putc(m, '\n');
3609 
3610 	return 0;
3611 }
3612 
3613 static const struct seq_operations show_ftrace_seq_ops = {
3614 	.start = t_start,
3615 	.next = t_next,
3616 	.stop = t_stop,
3617 	.show = t_show,
3618 };
3619 
3620 static int
3621 ftrace_avail_open(struct inode *inode, struct file *file)
3622 {
3623 	struct ftrace_iterator *iter;
3624 	int ret;
3625 
3626 	ret = security_locked_down(LOCKDOWN_TRACEFS);
3627 	if (ret)
3628 		return ret;
3629 
3630 	if (unlikely(ftrace_disabled))
3631 		return -ENODEV;
3632 
3633 	iter = __seq_open_private(file, &show_ftrace_seq_ops, sizeof(*iter));
3634 	if (!iter)
3635 		return -ENOMEM;
3636 
3637 	iter->pg = ftrace_pages_start;
3638 	iter->ops = &global_ops;
3639 
3640 	return 0;
3641 }
3642 
3643 static int
3644 ftrace_enabled_open(struct inode *inode, struct file *file)
3645 {
3646 	struct ftrace_iterator *iter;
3647 
3648 	/*
3649 	 * This shows us what functions are currently being
3650 	 * traced and by what. Not sure if we want lockdown
3651 	 * to hide such critical information for an admin.
3652 	 * Although, perhaps it can show information we don't
3653 	 * want people to see, but if something is tracing
3654 	 * something, we probably want to know about it.
3655 	 */
3656 
3657 	iter = __seq_open_private(file, &show_ftrace_seq_ops, sizeof(*iter));
3658 	if (!iter)
3659 		return -ENOMEM;
3660 
3661 	iter->pg = ftrace_pages_start;
3662 	iter->flags = FTRACE_ITER_ENABLED;
3663 	iter->ops = &global_ops;
3664 
3665 	return 0;
3666 }
3667 
3668 /**
3669  * ftrace_regex_open - initialize function tracer filter files
3670  * @ops: The ftrace_ops that hold the hash filters
3671  * @flag: The type of filter to process
3672  * @inode: The inode, usually passed in to your open routine
3673  * @file: The file, usually passed in to your open routine
3674  *
3675  * ftrace_regex_open() initializes the filter files for the
3676  * @ops. Depending on @flag it may process the filter hash or
3677  * the notrace hash of @ops. With this called from the open
3678  * routine, you can use ftrace_filter_write() for the write
3679  * routine if @flag has FTRACE_ITER_FILTER set, or
3680  * ftrace_notrace_write() if @flag has FTRACE_ITER_NOTRACE set.
3681  * tracing_lseek() should be used as the lseek routine, and
3682  * release must call ftrace_regex_release().
3683  */
3684 int
3685 ftrace_regex_open(struct ftrace_ops *ops, int flag,
3686 		  struct inode *inode, struct file *file)
3687 {
3688 	struct ftrace_iterator *iter;
3689 	struct ftrace_hash *hash;
3690 	struct list_head *mod_head;
3691 	struct trace_array *tr = ops->private;
3692 	int ret = -ENOMEM;
3693 
3694 	ftrace_ops_init(ops);
3695 
3696 	if (unlikely(ftrace_disabled))
3697 		return -ENODEV;
3698 
3699 	if (tracing_check_open_get_tr(tr))
3700 		return -ENODEV;
3701 
3702 	iter = kzalloc(sizeof(*iter), GFP_KERNEL);
3703 	if (!iter)
3704 		goto out;
3705 
3706 	if (trace_parser_get_init(&iter->parser, FTRACE_BUFF_MAX))
3707 		goto out;
3708 
3709 	iter->ops = ops;
3710 	iter->flags = flag;
3711 	iter->tr = tr;
3712 
3713 	mutex_lock(&ops->func_hash->regex_lock);
3714 
3715 	if (flag & FTRACE_ITER_NOTRACE) {
3716 		hash = ops->func_hash->notrace_hash;
3717 		mod_head = tr ? &tr->mod_notrace : NULL;
3718 	} else {
3719 		hash = ops->func_hash->filter_hash;
3720 		mod_head = tr ? &tr->mod_trace : NULL;
3721 	}
3722 
3723 	iter->mod_list = mod_head;
3724 
3725 	if (file->f_mode & FMODE_WRITE) {
3726 		const int size_bits = FTRACE_HASH_DEFAULT_BITS;
3727 
3728 		if (file->f_flags & O_TRUNC) {
3729 			iter->hash = alloc_ftrace_hash(size_bits);
3730 			clear_ftrace_mod_list(mod_head);
3731 	        } else {
3732 			iter->hash = alloc_and_copy_ftrace_hash(size_bits, hash);
3733 		}
3734 
3735 		if (!iter->hash) {
3736 			trace_parser_put(&iter->parser);
3737 			goto out_unlock;
3738 		}
3739 	} else
3740 		iter->hash = hash;
3741 
3742 	ret = 0;
3743 
3744 	if (file->f_mode & FMODE_READ) {
3745 		iter->pg = ftrace_pages_start;
3746 
3747 		ret = seq_open(file, &show_ftrace_seq_ops);
3748 		if (!ret) {
3749 			struct seq_file *m = file->private_data;
3750 			m->private = iter;
3751 		} else {
3752 			/* Failed */
3753 			free_ftrace_hash(iter->hash);
3754 			trace_parser_put(&iter->parser);
3755 		}
3756 	} else
3757 		file->private_data = iter;
3758 
3759  out_unlock:
3760 	mutex_unlock(&ops->func_hash->regex_lock);
3761 
3762  out:
3763 	if (ret) {
3764 		kfree(iter);
3765 		if (tr)
3766 			trace_array_put(tr);
3767 	}
3768 
3769 	return ret;
3770 }
3771 
3772 static int
3773 ftrace_filter_open(struct inode *inode, struct file *file)
3774 {
3775 	struct ftrace_ops *ops = inode->i_private;
3776 
3777 	/* Checks for tracefs lockdown */
3778 	return ftrace_regex_open(ops,
3779 			FTRACE_ITER_FILTER | FTRACE_ITER_DO_PROBES,
3780 			inode, file);
3781 }
3782 
3783 static int
3784 ftrace_notrace_open(struct inode *inode, struct file *file)
3785 {
3786 	struct ftrace_ops *ops = inode->i_private;
3787 
3788 	/* Checks for tracefs lockdown */
3789 	return ftrace_regex_open(ops, FTRACE_ITER_NOTRACE,
3790 				 inode, file);
3791 }
3792 
3793 /* Type for quick search ftrace basic regexes (globs) from filter_parse_regex */
3794 struct ftrace_glob {
3795 	char *search;
3796 	unsigned len;
3797 	int type;
3798 };
3799 
3800 /*
3801  * If symbols in an architecture don't correspond exactly to the user-visible
3802  * name of what they represent, it is possible to define this function to
3803  * perform the necessary adjustments.
3804 */
3805 char * __weak arch_ftrace_match_adjust(char *str, const char *search)
3806 {
3807 	return str;
3808 }
3809 
3810 static int ftrace_match(char *str, struct ftrace_glob *g)
3811 {
3812 	int matched = 0;
3813 	int slen;
3814 
3815 	str = arch_ftrace_match_adjust(str, g->search);
3816 
3817 	switch (g->type) {
3818 	case MATCH_FULL:
3819 		if (strcmp(str, g->search) == 0)
3820 			matched = 1;
3821 		break;
3822 	case MATCH_FRONT_ONLY:
3823 		if (strncmp(str, g->search, g->len) == 0)
3824 			matched = 1;
3825 		break;
3826 	case MATCH_MIDDLE_ONLY:
3827 		if (strstr(str, g->search))
3828 			matched = 1;
3829 		break;
3830 	case MATCH_END_ONLY:
3831 		slen = strlen(str);
3832 		if (slen >= g->len &&
3833 		    memcmp(str + slen - g->len, g->search, g->len) == 0)
3834 			matched = 1;
3835 		break;
3836 	case MATCH_GLOB:
3837 		if (glob_match(g->search, str))
3838 			matched = 1;
3839 		break;
3840 	}
3841 
3842 	return matched;
3843 }
3844 
3845 static int
3846 enter_record(struct ftrace_hash *hash, struct dyn_ftrace *rec, int clear_filter)
3847 {
3848 	struct ftrace_func_entry *entry;
3849 	int ret = 0;
3850 
3851 	entry = ftrace_lookup_ip(hash, rec->ip);
3852 	if (clear_filter) {
3853 		/* Do nothing if it doesn't exist */
3854 		if (!entry)
3855 			return 0;
3856 
3857 		free_hash_entry(hash, entry);
3858 	} else {
3859 		/* Do nothing if it exists */
3860 		if (entry)
3861 			return 0;
3862 
3863 		ret = add_hash_entry(hash, rec->ip);
3864 	}
3865 	return ret;
3866 }
3867 
3868 static int
3869 add_rec_by_index(struct ftrace_hash *hash, struct ftrace_glob *func_g,
3870 		 int clear_filter)
3871 {
3872 	long index = simple_strtoul(func_g->search, NULL, 0);
3873 	struct ftrace_page *pg;
3874 	struct dyn_ftrace *rec;
3875 
3876 	/* The index starts at 1 */
3877 	if (--index < 0)
3878 		return 0;
3879 
3880 	do_for_each_ftrace_rec(pg, rec) {
3881 		if (pg->index <= index) {
3882 			index -= pg->index;
3883 			/* this is a double loop, break goes to the next page */
3884 			break;
3885 		}
3886 		rec = &pg->records[index];
3887 		enter_record(hash, rec, clear_filter);
3888 		return 1;
3889 	} while_for_each_ftrace_rec();
3890 	return 0;
3891 }
3892 
3893 static int
3894 ftrace_match_record(struct dyn_ftrace *rec, struct ftrace_glob *func_g,
3895 		struct ftrace_glob *mod_g, int exclude_mod)
3896 {
3897 	char str[KSYM_SYMBOL_LEN];
3898 	char *modname;
3899 
3900 	kallsyms_lookup(rec->ip, NULL, NULL, &modname, str);
3901 
3902 	if (mod_g) {
3903 		int mod_matches = (modname) ? ftrace_match(modname, mod_g) : 0;
3904 
3905 		/* blank module name to match all modules */
3906 		if (!mod_g->len) {
3907 			/* blank module globbing: modname xor exclude_mod */
3908 			if (!exclude_mod != !modname)
3909 				goto func_match;
3910 			return 0;
3911 		}
3912 
3913 		/*
3914 		 * exclude_mod is set to trace everything but the given
3915 		 * module. If it is set and the module matches, then
3916 		 * return 0. If it is not set, and the module doesn't match
3917 		 * also return 0. Otherwise, check the function to see if
3918 		 * that matches.
3919 		 */
3920 		if (!mod_matches == !exclude_mod)
3921 			return 0;
3922 func_match:
3923 		/* blank search means to match all funcs in the mod */
3924 		if (!func_g->len)
3925 			return 1;
3926 	}
3927 
3928 	return ftrace_match(str, func_g);
3929 }
3930 
3931 static int
3932 match_records(struct ftrace_hash *hash, char *func, int len, char *mod)
3933 {
3934 	struct ftrace_page *pg;
3935 	struct dyn_ftrace *rec;
3936 	struct ftrace_glob func_g = { .type = MATCH_FULL };
3937 	struct ftrace_glob mod_g = { .type = MATCH_FULL };
3938 	struct ftrace_glob *mod_match = (mod) ? &mod_g : NULL;
3939 	int exclude_mod = 0;
3940 	int found = 0;
3941 	int ret;
3942 	int clear_filter = 0;
3943 
3944 	if (func) {
3945 		func_g.type = filter_parse_regex(func, len, &func_g.search,
3946 						 &clear_filter);
3947 		func_g.len = strlen(func_g.search);
3948 	}
3949 
3950 	if (mod) {
3951 		mod_g.type = filter_parse_regex(mod, strlen(mod),
3952 				&mod_g.search, &exclude_mod);
3953 		mod_g.len = strlen(mod_g.search);
3954 	}
3955 
3956 	mutex_lock(&ftrace_lock);
3957 
3958 	if (unlikely(ftrace_disabled))
3959 		goto out_unlock;
3960 
3961 	if (func_g.type == MATCH_INDEX) {
3962 		found = add_rec_by_index(hash, &func_g, clear_filter);
3963 		goto out_unlock;
3964 	}
3965 
3966 	do_for_each_ftrace_rec(pg, rec) {
3967 
3968 		if (rec->flags & FTRACE_FL_DISABLED)
3969 			continue;
3970 
3971 		if (ftrace_match_record(rec, &func_g, mod_match, exclude_mod)) {
3972 			ret = enter_record(hash, rec, clear_filter);
3973 			if (ret < 0) {
3974 				found = ret;
3975 				goto out_unlock;
3976 			}
3977 			found = 1;
3978 		}
3979 	} while_for_each_ftrace_rec();
3980  out_unlock:
3981 	mutex_unlock(&ftrace_lock);
3982 
3983 	return found;
3984 }
3985 
3986 static int
3987 ftrace_match_records(struct ftrace_hash *hash, char *buff, int len)
3988 {
3989 	return match_records(hash, buff, len, NULL);
3990 }
3991 
3992 static void ftrace_ops_update_code(struct ftrace_ops *ops,
3993 				   struct ftrace_ops_hash *old_hash)
3994 {
3995 	struct ftrace_ops *op;
3996 
3997 	if (!ftrace_enabled)
3998 		return;
3999 
4000 	if (ops->flags & FTRACE_OPS_FL_ENABLED) {
4001 		ftrace_run_modify_code(ops, FTRACE_UPDATE_CALLS, old_hash);
4002 		return;
4003 	}
4004 
4005 	/*
4006 	 * If this is the shared global_ops filter, then we need to
4007 	 * check if there is another ops that shares it, is enabled.
4008 	 * If so, we still need to run the modify code.
4009 	 */
4010 	if (ops->func_hash != &global_ops.local_hash)
4011 		return;
4012 
4013 	do_for_each_ftrace_op(op, ftrace_ops_list) {
4014 		if (op->func_hash == &global_ops.local_hash &&
4015 		    op->flags & FTRACE_OPS_FL_ENABLED) {
4016 			ftrace_run_modify_code(op, FTRACE_UPDATE_CALLS, old_hash);
4017 			/* Only need to do this once */
4018 			return;
4019 		}
4020 	} while_for_each_ftrace_op(op);
4021 }
4022 
4023 static int ftrace_hash_move_and_update_ops(struct ftrace_ops *ops,
4024 					   struct ftrace_hash **orig_hash,
4025 					   struct ftrace_hash *hash,
4026 					   int enable)
4027 {
4028 	struct ftrace_ops_hash old_hash_ops;
4029 	struct ftrace_hash *old_hash;
4030 	int ret;
4031 
4032 	old_hash = *orig_hash;
4033 	old_hash_ops.filter_hash = ops->func_hash->filter_hash;
4034 	old_hash_ops.notrace_hash = ops->func_hash->notrace_hash;
4035 	ret = ftrace_hash_move(ops, enable, orig_hash, hash);
4036 	if (!ret) {
4037 		ftrace_ops_update_code(ops, &old_hash_ops);
4038 		free_ftrace_hash_rcu(old_hash);
4039 	}
4040 	return ret;
4041 }
4042 
4043 static bool module_exists(const char *module)
4044 {
4045 	/* All modules have the symbol __this_module */
4046 	static const char this_mod[] = "__this_module";
4047 	char modname[MAX_PARAM_PREFIX_LEN + sizeof(this_mod) + 2];
4048 	unsigned long val;
4049 	int n;
4050 
4051 	n = snprintf(modname, sizeof(modname), "%s:%s", module, this_mod);
4052 
4053 	if (n > sizeof(modname) - 1)
4054 		return false;
4055 
4056 	val = module_kallsyms_lookup_name(modname);
4057 	return val != 0;
4058 }
4059 
4060 static int cache_mod(struct trace_array *tr,
4061 		     const char *func, char *module, int enable)
4062 {
4063 	struct ftrace_mod_load *ftrace_mod, *n;
4064 	struct list_head *head = enable ? &tr->mod_trace : &tr->mod_notrace;
4065 	int ret;
4066 
4067 	mutex_lock(&ftrace_lock);
4068 
4069 	/* We do not cache inverse filters */
4070 	if (func[0] == '!') {
4071 		func++;
4072 		ret = -EINVAL;
4073 
4074 		/* Look to remove this hash */
4075 		list_for_each_entry_safe(ftrace_mod, n, head, list) {
4076 			if (strcmp(ftrace_mod->module, module) != 0)
4077 				continue;
4078 
4079 			/* no func matches all */
4080 			if (strcmp(func, "*") == 0 ||
4081 			    (ftrace_mod->func &&
4082 			     strcmp(ftrace_mod->func, func) == 0)) {
4083 				ret = 0;
4084 				free_ftrace_mod(ftrace_mod);
4085 				continue;
4086 			}
4087 		}
4088 		goto out;
4089 	}
4090 
4091 	ret = -EINVAL;
4092 	/* We only care about modules that have not been loaded yet */
4093 	if (module_exists(module))
4094 		goto out;
4095 
4096 	/* Save this string off, and execute it when the module is loaded */
4097 	ret = ftrace_add_mod(tr, func, module, enable);
4098  out:
4099 	mutex_unlock(&ftrace_lock);
4100 
4101 	return ret;
4102 }
4103 
4104 static int
4105 ftrace_set_regex(struct ftrace_ops *ops, unsigned char *buf, int len,
4106 		 int reset, int enable);
4107 
4108 #ifdef CONFIG_MODULES
4109 static void process_mod_list(struct list_head *head, struct ftrace_ops *ops,
4110 			     char *mod, bool enable)
4111 {
4112 	struct ftrace_mod_load *ftrace_mod, *n;
4113 	struct ftrace_hash **orig_hash, *new_hash;
4114 	LIST_HEAD(process_mods);
4115 	char *func;
4116 	int ret;
4117 
4118 	mutex_lock(&ops->func_hash->regex_lock);
4119 
4120 	if (enable)
4121 		orig_hash = &ops->func_hash->filter_hash;
4122 	else
4123 		orig_hash = &ops->func_hash->notrace_hash;
4124 
4125 	new_hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS,
4126 					      *orig_hash);
4127 	if (!new_hash)
4128 		goto out; /* warn? */
4129 
4130 	mutex_lock(&ftrace_lock);
4131 
4132 	list_for_each_entry_safe(ftrace_mod, n, head, list) {
4133 
4134 		if (strcmp(ftrace_mod->module, mod) != 0)
4135 			continue;
4136 
4137 		if (ftrace_mod->func)
4138 			func = kstrdup(ftrace_mod->func, GFP_KERNEL);
4139 		else
4140 			func = kstrdup("*", GFP_KERNEL);
4141 
4142 		if (!func) /* warn? */
4143 			continue;
4144 
4145 		list_del(&ftrace_mod->list);
4146 		list_add(&ftrace_mod->list, &process_mods);
4147 
4148 		/* Use the newly allocated func, as it may be "*" */
4149 		kfree(ftrace_mod->func);
4150 		ftrace_mod->func = func;
4151 	}
4152 
4153 	mutex_unlock(&ftrace_lock);
4154 
4155 	list_for_each_entry_safe(ftrace_mod, n, &process_mods, list) {
4156 
4157 		func = ftrace_mod->func;
4158 
4159 		/* Grabs ftrace_lock, which is why we have this extra step */
4160 		match_records(new_hash, func, strlen(func), mod);
4161 		free_ftrace_mod(ftrace_mod);
4162 	}
4163 
4164 	if (enable && list_empty(head))
4165 		new_hash->flags &= ~FTRACE_HASH_FL_MOD;
4166 
4167 	mutex_lock(&ftrace_lock);
4168 
4169 	ret = ftrace_hash_move_and_update_ops(ops, orig_hash,
4170 					      new_hash, enable);
4171 	mutex_unlock(&ftrace_lock);
4172 
4173  out:
4174 	mutex_unlock(&ops->func_hash->regex_lock);
4175 
4176 	free_ftrace_hash(new_hash);
4177 }
4178 
4179 static void process_cached_mods(const char *mod_name)
4180 {
4181 	struct trace_array *tr;
4182 	char *mod;
4183 
4184 	mod = kstrdup(mod_name, GFP_KERNEL);
4185 	if (!mod)
4186 		return;
4187 
4188 	mutex_lock(&trace_types_lock);
4189 	list_for_each_entry(tr, &ftrace_trace_arrays, list) {
4190 		if (!list_empty(&tr->mod_trace))
4191 			process_mod_list(&tr->mod_trace, tr->ops, mod, true);
4192 		if (!list_empty(&tr->mod_notrace))
4193 			process_mod_list(&tr->mod_notrace, tr->ops, mod, false);
4194 	}
4195 	mutex_unlock(&trace_types_lock);
4196 
4197 	kfree(mod);
4198 }
4199 #endif
4200 
4201 /*
4202  * We register the module command as a template to show others how
4203  * to register the a command as well.
4204  */
4205 
4206 static int
4207 ftrace_mod_callback(struct trace_array *tr, struct ftrace_hash *hash,
4208 		    char *func_orig, char *cmd, char *module, int enable)
4209 {
4210 	char *func;
4211 	int ret;
4212 
4213 	/* match_records() modifies func, and we need the original */
4214 	func = kstrdup(func_orig, GFP_KERNEL);
4215 	if (!func)
4216 		return -ENOMEM;
4217 
4218 	/*
4219 	 * cmd == 'mod' because we only registered this func
4220 	 * for the 'mod' ftrace_func_command.
4221 	 * But if you register one func with multiple commands,
4222 	 * you can tell which command was used by the cmd
4223 	 * parameter.
4224 	 */
4225 	ret = match_records(hash, func, strlen(func), module);
4226 	kfree(func);
4227 
4228 	if (!ret)
4229 		return cache_mod(tr, func_orig, module, enable);
4230 	if (ret < 0)
4231 		return ret;
4232 	return 0;
4233 }
4234 
4235 static struct ftrace_func_command ftrace_mod_cmd = {
4236 	.name			= "mod",
4237 	.func			= ftrace_mod_callback,
4238 };
4239 
4240 static int __init ftrace_mod_cmd_init(void)
4241 {
4242 	return register_ftrace_command(&ftrace_mod_cmd);
4243 }
4244 core_initcall(ftrace_mod_cmd_init);
4245 
4246 static void function_trace_probe_call(unsigned long ip, unsigned long parent_ip,
4247 				      struct ftrace_ops *op, struct pt_regs *pt_regs)
4248 {
4249 	struct ftrace_probe_ops *probe_ops;
4250 	struct ftrace_func_probe *probe;
4251 
4252 	probe = container_of(op, struct ftrace_func_probe, ops);
4253 	probe_ops = probe->probe_ops;
4254 
4255 	/*
4256 	 * Disable preemption for these calls to prevent a RCU grace
4257 	 * period. This syncs the hash iteration and freeing of items
4258 	 * on the hash. rcu_read_lock is too dangerous here.
4259 	 */
4260 	preempt_disable_notrace();
4261 	probe_ops->func(ip, parent_ip, probe->tr, probe_ops, probe->data);
4262 	preempt_enable_notrace();
4263 }
4264 
4265 struct ftrace_func_map {
4266 	struct ftrace_func_entry	entry;
4267 	void				*data;
4268 };
4269 
4270 struct ftrace_func_mapper {
4271 	struct ftrace_hash		hash;
4272 };
4273 
4274 /**
4275  * allocate_ftrace_func_mapper - allocate a new ftrace_func_mapper
4276  *
4277  * Returns a ftrace_func_mapper descriptor that can be used to map ips to data.
4278  */
4279 struct ftrace_func_mapper *allocate_ftrace_func_mapper(void)
4280 {
4281 	struct ftrace_hash *hash;
4282 
4283 	/*
4284 	 * The mapper is simply a ftrace_hash, but since the entries
4285 	 * in the hash are not ftrace_func_entry type, we define it
4286 	 * as a separate structure.
4287 	 */
4288 	hash = alloc_ftrace_hash(FTRACE_HASH_DEFAULT_BITS);
4289 	return (struct ftrace_func_mapper *)hash;
4290 }
4291 
4292 /**
4293  * ftrace_func_mapper_find_ip - Find some data mapped to an ip
4294  * @mapper: The mapper that has the ip maps
4295  * @ip: the instruction pointer to find the data for
4296  *
4297  * Returns the data mapped to @ip if found otherwise NULL. The return
4298  * is actually the address of the mapper data pointer. The address is
4299  * returned for use cases where the data is no bigger than a long, and
4300  * the user can use the data pointer as its data instead of having to
4301  * allocate more memory for the reference.
4302  */
4303 void **ftrace_func_mapper_find_ip(struct ftrace_func_mapper *mapper,
4304 				  unsigned long ip)
4305 {
4306 	struct ftrace_func_entry *entry;
4307 	struct ftrace_func_map *map;
4308 
4309 	entry = ftrace_lookup_ip(&mapper->hash, ip);
4310 	if (!entry)
4311 		return NULL;
4312 
4313 	map = (struct ftrace_func_map *)entry;
4314 	return &map->data;
4315 }
4316 
4317 /**
4318  * ftrace_func_mapper_add_ip - Map some data to an ip
4319  * @mapper: The mapper that has the ip maps
4320  * @ip: The instruction pointer address to map @data to
4321  * @data: The data to map to @ip
4322  *
4323  * Returns 0 on succes otherwise an error.
4324  */
4325 int ftrace_func_mapper_add_ip(struct ftrace_func_mapper *mapper,
4326 			      unsigned long ip, void *data)
4327 {
4328 	struct ftrace_func_entry *entry;
4329 	struct ftrace_func_map *map;
4330 
4331 	entry = ftrace_lookup_ip(&mapper->hash, ip);
4332 	if (entry)
4333 		return -EBUSY;
4334 
4335 	map = kmalloc(sizeof(*map), GFP_KERNEL);
4336 	if (!map)
4337 		return -ENOMEM;
4338 
4339 	map->entry.ip = ip;
4340 	map->data = data;
4341 
4342 	__add_hash_entry(&mapper->hash, &map->entry);
4343 
4344 	return 0;
4345 }
4346 
4347 /**
4348  * ftrace_func_mapper_remove_ip - Remove an ip from the mapping
4349  * @mapper: The mapper that has the ip maps
4350  * @ip: The instruction pointer address to remove the data from
4351  *
4352  * Returns the data if it is found, otherwise NULL.
4353  * Note, if the data pointer is used as the data itself, (see
4354  * ftrace_func_mapper_find_ip(), then the return value may be meaningless,
4355  * if the data pointer was set to zero.
4356  */
4357 void *ftrace_func_mapper_remove_ip(struct ftrace_func_mapper *mapper,
4358 				   unsigned long ip)
4359 {
4360 	struct ftrace_func_entry *entry;
4361 	struct ftrace_func_map *map;
4362 	void *data;
4363 
4364 	entry = ftrace_lookup_ip(&mapper->hash, ip);
4365 	if (!entry)
4366 		return NULL;
4367 
4368 	map = (struct ftrace_func_map *)entry;
4369 	data = map->data;
4370 
4371 	remove_hash_entry(&mapper->hash, entry);
4372 	kfree(entry);
4373 
4374 	return data;
4375 }
4376 
4377 /**
4378  * free_ftrace_func_mapper - free a mapping of ips and data
4379  * @mapper: The mapper that has the ip maps
4380  * @free_func: A function to be called on each data item.
4381  *
4382  * This is used to free the function mapper. The @free_func is optional
4383  * and can be used if the data needs to be freed as well.
4384  */
4385 void free_ftrace_func_mapper(struct ftrace_func_mapper *mapper,
4386 			     ftrace_mapper_func free_func)
4387 {
4388 	struct ftrace_func_entry *entry;
4389 	struct ftrace_func_map *map;
4390 	struct hlist_head *hhd;
4391 	int size, i;
4392 
4393 	if (!mapper)
4394 		return;
4395 
4396 	if (free_func && mapper->hash.count) {
4397 		size = 1 << mapper->hash.size_bits;
4398 		for (i = 0; i < size; i++) {
4399 			hhd = &mapper->hash.buckets[i];
4400 			hlist_for_each_entry(entry, hhd, hlist) {
4401 				map = (struct ftrace_func_map *)entry;
4402 				free_func(map);
4403 			}
4404 		}
4405 	}
4406 	free_ftrace_hash(&mapper->hash);
4407 }
4408 
4409 static void release_probe(struct ftrace_func_probe *probe)
4410 {
4411 	struct ftrace_probe_ops *probe_ops;
4412 
4413 	mutex_lock(&ftrace_lock);
4414 
4415 	WARN_ON(probe->ref <= 0);
4416 
4417 	/* Subtract the ref that was used to protect this instance */
4418 	probe->ref--;
4419 
4420 	if (!probe->ref) {
4421 		probe_ops = probe->probe_ops;
4422 		/*
4423 		 * Sending zero as ip tells probe_ops to free
4424 		 * the probe->data itself
4425 		 */
4426 		if (probe_ops->free)
4427 			probe_ops->free(probe_ops, probe->tr, 0, probe->data);
4428 		list_del(&probe->list);
4429 		kfree(probe);
4430 	}
4431 	mutex_unlock(&ftrace_lock);
4432 }
4433 
4434 static void acquire_probe_locked(struct ftrace_func_probe *probe)
4435 {
4436 	/*
4437 	 * Add one ref to keep it from being freed when releasing the
4438 	 * ftrace_lock mutex.
4439 	 */
4440 	probe->ref++;
4441 }
4442 
4443 int
4444 register_ftrace_function_probe(char *glob, struct trace_array *tr,
4445 			       struct ftrace_probe_ops *probe_ops,
4446 			       void *data)
4447 {
4448 	struct ftrace_func_entry *entry;
4449 	struct ftrace_func_probe *probe;
4450 	struct ftrace_hash **orig_hash;
4451 	struct ftrace_hash *old_hash;
4452 	struct ftrace_hash *hash;
4453 	int count = 0;
4454 	int size;
4455 	int ret;
4456 	int i;
4457 
4458 	if (WARN_ON(!tr))
4459 		return -EINVAL;
4460 
4461 	/* We do not support '!' for function probes */
4462 	if (WARN_ON(glob[0] == '!'))
4463 		return -EINVAL;
4464 
4465 
4466 	mutex_lock(&ftrace_lock);
4467 	/* Check if the probe_ops is already registered */
4468 	list_for_each_entry(probe, &tr->func_probes, list) {
4469 		if (probe->probe_ops == probe_ops)
4470 			break;
4471 	}
4472 	if (&probe->list == &tr->func_probes) {
4473 		probe = kzalloc(sizeof(*probe), GFP_KERNEL);
4474 		if (!probe) {
4475 			mutex_unlock(&ftrace_lock);
4476 			return -ENOMEM;
4477 		}
4478 		probe->probe_ops = probe_ops;
4479 		probe->ops.func = function_trace_probe_call;
4480 		probe->tr = tr;
4481 		ftrace_ops_init(&probe->ops);
4482 		list_add(&probe->list, &tr->func_probes);
4483 	}
4484 
4485 	acquire_probe_locked(probe);
4486 
4487 	mutex_unlock(&ftrace_lock);
4488 
4489 	/*
4490 	 * Note, there's a small window here that the func_hash->filter_hash
4491 	 * may be NULL or empty. Need to be carefule when reading the loop.
4492 	 */
4493 	mutex_lock(&probe->ops.func_hash->regex_lock);
4494 
4495 	orig_hash = &probe->ops.func_hash->filter_hash;
4496 	old_hash = *orig_hash;
4497 	hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, old_hash);
4498 
4499 	if (!hash) {
4500 		ret = -ENOMEM;
4501 		goto out;
4502 	}
4503 
4504 	ret = ftrace_match_records(hash, glob, strlen(glob));
4505 
4506 	/* Nothing found? */
4507 	if (!ret)
4508 		ret = -EINVAL;
4509 
4510 	if (ret < 0)
4511 		goto out;
4512 
4513 	size = 1 << hash->size_bits;
4514 	for (i = 0; i < size; i++) {
4515 		hlist_for_each_entry(entry, &hash->buckets[i], hlist) {
4516 			if (ftrace_lookup_ip(old_hash, entry->ip))
4517 				continue;
4518 			/*
4519 			 * The caller might want to do something special
4520 			 * for each function we find. We call the callback
4521 			 * to give the caller an opportunity to do so.
4522 			 */
4523 			if (probe_ops->init) {
4524 				ret = probe_ops->init(probe_ops, tr,
4525 						      entry->ip, data,
4526 						      &probe->data);
4527 				if (ret < 0) {
4528 					if (probe_ops->free && count)
4529 						probe_ops->free(probe_ops, tr,
4530 								0, probe->data);
4531 					probe->data = NULL;
4532 					goto out;
4533 				}
4534 			}
4535 			count++;
4536 		}
4537 	}
4538 
4539 	mutex_lock(&ftrace_lock);
4540 
4541 	if (!count) {
4542 		/* Nothing was added? */
4543 		ret = -EINVAL;
4544 		goto out_unlock;
4545 	}
4546 
4547 	ret = ftrace_hash_move_and_update_ops(&probe->ops, orig_hash,
4548 					      hash, 1);
4549 	if (ret < 0)
4550 		goto err_unlock;
4551 
4552 	/* One ref for each new function traced */
4553 	probe->ref += count;
4554 
4555 	if (!(probe->ops.flags & FTRACE_OPS_FL_ENABLED))
4556 		ret = ftrace_startup(&probe->ops, 0);
4557 
4558  out_unlock:
4559 	mutex_unlock(&ftrace_lock);
4560 
4561 	if (!ret)
4562 		ret = count;
4563  out:
4564 	mutex_unlock(&probe->ops.func_hash->regex_lock);
4565 	free_ftrace_hash(hash);
4566 
4567 	release_probe(probe);
4568 
4569 	return ret;
4570 
4571  err_unlock:
4572 	if (!probe_ops->free || !count)
4573 		goto out_unlock;
4574 
4575 	/* Failed to do the move, need to call the free functions */
4576 	for (i = 0; i < size; i++) {
4577 		hlist_for_each_entry(entry, &hash->buckets[i], hlist) {
4578 			if (ftrace_lookup_ip(old_hash, entry->ip))
4579 				continue;
4580 			probe_ops->free(probe_ops, tr, entry->ip, probe->data);
4581 		}
4582 	}
4583 	goto out_unlock;
4584 }
4585 
4586 int
4587 unregister_ftrace_function_probe_func(char *glob, struct trace_array *tr,
4588 				      struct ftrace_probe_ops *probe_ops)
4589 {
4590 	struct ftrace_ops_hash old_hash_ops;
4591 	struct ftrace_func_entry *entry;
4592 	struct ftrace_func_probe *probe;
4593 	struct ftrace_glob func_g;
4594 	struct ftrace_hash **orig_hash;
4595 	struct ftrace_hash *old_hash;
4596 	struct ftrace_hash *hash = NULL;
4597 	struct hlist_node *tmp;
4598 	struct hlist_head hhd;
4599 	char str[KSYM_SYMBOL_LEN];
4600 	int count = 0;
4601 	int i, ret = -ENODEV;
4602 	int size;
4603 
4604 	if (!glob || !strlen(glob) || !strcmp(glob, "*"))
4605 		func_g.search = NULL;
4606 	else {
4607 		int not;
4608 
4609 		func_g.type = filter_parse_regex(glob, strlen(glob),
4610 						 &func_g.search, &not);
4611 		func_g.len = strlen(func_g.search);
4612 
4613 		/* we do not support '!' for function probes */
4614 		if (WARN_ON(not))
4615 			return -EINVAL;
4616 	}
4617 
4618 	mutex_lock(&ftrace_lock);
4619 	/* Check if the probe_ops is already registered */
4620 	list_for_each_entry(probe, &tr->func_probes, list) {
4621 		if (probe->probe_ops == probe_ops)
4622 			break;
4623 	}
4624 	if (&probe->list == &tr->func_probes)
4625 		goto err_unlock_ftrace;
4626 
4627 	ret = -EINVAL;
4628 	if (!(probe->ops.flags & FTRACE_OPS_FL_INITIALIZED))
4629 		goto err_unlock_ftrace;
4630 
4631 	acquire_probe_locked(probe);
4632 
4633 	mutex_unlock(&ftrace_lock);
4634 
4635 	mutex_lock(&probe->ops.func_hash->regex_lock);
4636 
4637 	orig_hash = &probe->ops.func_hash->filter_hash;
4638 	old_hash = *orig_hash;
4639 
4640 	if (ftrace_hash_empty(old_hash))
4641 		goto out_unlock;
4642 
4643 	old_hash_ops.filter_hash = old_hash;
4644 	/* Probes only have filters */
4645 	old_hash_ops.notrace_hash = NULL;
4646 
4647 	ret = -ENOMEM;
4648 	hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, old_hash);
4649 	if (!hash)
4650 		goto out_unlock;
4651 
4652 	INIT_HLIST_HEAD(&hhd);
4653 
4654 	size = 1 << hash->size_bits;
4655 	for (i = 0; i < size; i++) {
4656 		hlist_for_each_entry_safe(entry, tmp, &hash->buckets[i], hlist) {
4657 
4658 			if (func_g.search) {
4659 				kallsyms_lookup(entry->ip, NULL, NULL,
4660 						NULL, str);
4661 				if (!ftrace_match(str, &func_g))
4662 					continue;
4663 			}
4664 			count++;
4665 			remove_hash_entry(hash, entry);
4666 			hlist_add_head(&entry->hlist, &hhd);
4667 		}
4668 	}
4669 
4670 	/* Nothing found? */
4671 	if (!count) {
4672 		ret = -EINVAL;
4673 		goto out_unlock;
4674 	}
4675 
4676 	mutex_lock(&ftrace_lock);
4677 
4678 	WARN_ON(probe->ref < count);
4679 
4680 	probe->ref -= count;
4681 
4682 	if (ftrace_hash_empty(hash))
4683 		ftrace_shutdown(&probe->ops, 0);
4684 
4685 	ret = ftrace_hash_move_and_update_ops(&probe->ops, orig_hash,
4686 					      hash, 1);
4687 
4688 	/* still need to update the function call sites */
4689 	if (ftrace_enabled && !ftrace_hash_empty(hash))
4690 		ftrace_run_modify_code(&probe->ops, FTRACE_UPDATE_CALLS,
4691 				       &old_hash_ops);
4692 	synchronize_rcu();
4693 
4694 	hlist_for_each_entry_safe(entry, tmp, &hhd, hlist) {
4695 		hlist_del(&entry->hlist);
4696 		if (probe_ops->free)
4697 			probe_ops->free(probe_ops, tr, entry->ip, probe->data);
4698 		kfree(entry);
4699 	}
4700 	mutex_unlock(&ftrace_lock);
4701 
4702  out_unlock:
4703 	mutex_unlock(&probe->ops.func_hash->regex_lock);
4704 	free_ftrace_hash(hash);
4705 
4706 	release_probe(probe);
4707 
4708 	return ret;
4709 
4710  err_unlock_ftrace:
4711 	mutex_unlock(&ftrace_lock);
4712 	return ret;
4713 }
4714 
4715 void clear_ftrace_function_probes(struct trace_array *tr)
4716 {
4717 	struct ftrace_func_probe *probe, *n;
4718 
4719 	list_for_each_entry_safe(probe, n, &tr->func_probes, list)
4720 		unregister_ftrace_function_probe_func(NULL, tr, probe->probe_ops);
4721 }
4722 
4723 static LIST_HEAD(ftrace_commands);
4724 static DEFINE_MUTEX(ftrace_cmd_mutex);
4725 
4726 /*
4727  * Currently we only register ftrace commands from __init, so mark this
4728  * __init too.
4729  */
4730 __init int register_ftrace_command(struct ftrace_func_command *cmd)
4731 {
4732 	struct ftrace_func_command *p;
4733 	int ret = 0;
4734 
4735 	mutex_lock(&ftrace_cmd_mutex);
4736 	list_for_each_entry(p, &ftrace_commands, list) {
4737 		if (strcmp(cmd->name, p->name) == 0) {
4738 			ret = -EBUSY;
4739 			goto out_unlock;
4740 		}
4741 	}
4742 	list_add(&cmd->list, &ftrace_commands);
4743  out_unlock:
4744 	mutex_unlock(&ftrace_cmd_mutex);
4745 
4746 	return ret;
4747 }
4748 
4749 /*
4750  * Currently we only unregister ftrace commands from __init, so mark
4751  * this __init too.
4752  */
4753 __init int unregister_ftrace_command(struct ftrace_func_command *cmd)
4754 {
4755 	struct ftrace_func_command *p, *n;
4756 	int ret = -ENODEV;
4757 
4758 	mutex_lock(&ftrace_cmd_mutex);
4759 	list_for_each_entry_safe(p, n, &ftrace_commands, list) {
4760 		if (strcmp(cmd->name, p->name) == 0) {
4761 			ret = 0;
4762 			list_del_init(&p->list);
4763 			goto out_unlock;
4764 		}
4765 	}
4766  out_unlock:
4767 	mutex_unlock(&ftrace_cmd_mutex);
4768 
4769 	return ret;
4770 }
4771 
4772 static int ftrace_process_regex(struct ftrace_iterator *iter,
4773 				char *buff, int len, int enable)
4774 {
4775 	struct ftrace_hash *hash = iter->hash;
4776 	struct trace_array *tr = iter->ops->private;
4777 	char *func, *command, *next = buff;
4778 	struct ftrace_func_command *p;
4779 	int ret = -EINVAL;
4780 
4781 	func = strsep(&next, ":");
4782 
4783 	if (!next) {
4784 		ret = ftrace_match_records(hash, func, len);
4785 		if (!ret)
4786 			ret = -EINVAL;
4787 		if (ret < 0)
4788 			return ret;
4789 		return 0;
4790 	}
4791 
4792 	/* command found */
4793 
4794 	command = strsep(&next, ":");
4795 
4796 	mutex_lock(&ftrace_cmd_mutex);
4797 	list_for_each_entry(p, &ftrace_commands, list) {
4798 		if (strcmp(p->name, command) == 0) {
4799 			ret = p->func(tr, hash, func, command, next, enable);
4800 			goto out_unlock;
4801 		}
4802 	}
4803  out_unlock:
4804 	mutex_unlock(&ftrace_cmd_mutex);
4805 
4806 	return ret;
4807 }
4808 
4809 static ssize_t
4810 ftrace_regex_write(struct file *file, const char __user *ubuf,
4811 		   size_t cnt, loff_t *ppos, int enable)
4812 {
4813 	struct ftrace_iterator *iter;
4814 	struct trace_parser *parser;
4815 	ssize_t ret, read;
4816 
4817 	if (!cnt)
4818 		return 0;
4819 
4820 	if (file->f_mode & FMODE_READ) {
4821 		struct seq_file *m = file->private_data;
4822 		iter = m->private;
4823 	} else
4824 		iter = file->private_data;
4825 
4826 	if (unlikely(ftrace_disabled))
4827 		return -ENODEV;
4828 
4829 	/* iter->hash is a local copy, so we don't need regex_lock */
4830 
4831 	parser = &iter->parser;
4832 	read = trace_get_user(parser, ubuf, cnt, ppos);
4833 
4834 	if (read >= 0 && trace_parser_loaded(parser) &&
4835 	    !trace_parser_cont(parser)) {
4836 		ret = ftrace_process_regex(iter, parser->buffer,
4837 					   parser->idx, enable);
4838 		trace_parser_clear(parser);
4839 		if (ret < 0)
4840 			goto out;
4841 	}
4842 
4843 	ret = read;
4844  out:
4845 	return ret;
4846 }
4847 
4848 ssize_t
4849 ftrace_filter_write(struct file *file, const char __user *ubuf,
4850 		    size_t cnt, loff_t *ppos)
4851 {
4852 	return ftrace_regex_write(file, ubuf, cnt, ppos, 1);
4853 }
4854 
4855 ssize_t
4856 ftrace_notrace_write(struct file *file, const char __user *ubuf,
4857 		     size_t cnt, loff_t *ppos)
4858 {
4859 	return ftrace_regex_write(file, ubuf, cnt, ppos, 0);
4860 }
4861 
4862 static int
4863 ftrace_match_addr(struct ftrace_hash *hash, unsigned long ip, int remove)
4864 {
4865 	struct ftrace_func_entry *entry;
4866 
4867 	if (!ftrace_location(ip))
4868 		return -EINVAL;
4869 
4870 	if (remove) {
4871 		entry = ftrace_lookup_ip(hash, ip);
4872 		if (!entry)
4873 			return -ENOENT;
4874 		free_hash_entry(hash, entry);
4875 		return 0;
4876 	}
4877 
4878 	return add_hash_entry(hash, ip);
4879 }
4880 
4881 static int
4882 ftrace_set_hash(struct ftrace_ops *ops, unsigned char *buf, int len,
4883 		unsigned long ip, int remove, int reset, int enable)
4884 {
4885 	struct ftrace_hash **orig_hash;
4886 	struct ftrace_hash *hash;
4887 	int ret;
4888 
4889 	if (unlikely(ftrace_disabled))
4890 		return -ENODEV;
4891 
4892 	mutex_lock(&ops->func_hash->regex_lock);
4893 
4894 	if (enable)
4895 		orig_hash = &ops->func_hash->filter_hash;
4896 	else
4897 		orig_hash = &ops->func_hash->notrace_hash;
4898 
4899 	if (reset)
4900 		hash = alloc_ftrace_hash(FTRACE_HASH_DEFAULT_BITS);
4901 	else
4902 		hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, *orig_hash);
4903 
4904 	if (!hash) {
4905 		ret = -ENOMEM;
4906 		goto out_regex_unlock;
4907 	}
4908 
4909 	if (buf && !ftrace_match_records(hash, buf, len)) {
4910 		ret = -EINVAL;
4911 		goto out_regex_unlock;
4912 	}
4913 	if (ip) {
4914 		ret = ftrace_match_addr(hash, ip, remove);
4915 		if (ret < 0)
4916 			goto out_regex_unlock;
4917 	}
4918 
4919 	mutex_lock(&ftrace_lock);
4920 	ret = ftrace_hash_move_and_update_ops(ops, orig_hash, hash, enable);
4921 	mutex_unlock(&ftrace_lock);
4922 
4923  out_regex_unlock:
4924 	mutex_unlock(&ops->func_hash->regex_lock);
4925 
4926 	free_ftrace_hash(hash);
4927 	return ret;
4928 }
4929 
4930 static int
4931 ftrace_set_addr(struct ftrace_ops *ops, unsigned long ip, int remove,
4932 		int reset, int enable)
4933 {
4934 	return ftrace_set_hash(ops, NULL, 0, ip, remove, reset, enable);
4935 }
4936 
4937 #ifdef CONFIG_DYNAMIC_FTRACE_WITH_DIRECT_CALLS
4938 
4939 struct ftrace_direct_func {
4940 	struct list_head	next;
4941 	unsigned long		addr;
4942 	int			count;
4943 };
4944 
4945 static LIST_HEAD(ftrace_direct_funcs);
4946 
4947 /**
4948  * ftrace_find_direct_func - test an address if it is a registered direct caller
4949  * @addr: The address of a registered direct caller
4950  *
4951  * This searches to see if a ftrace direct caller has been registered
4952  * at a specific address, and if so, it returns a descriptor for it.
4953  *
4954  * This can be used by architecture code to see if an address is
4955  * a direct caller (trampoline) attached to a fentry/mcount location.
4956  * This is useful for the function_graph tracer, as it may need to
4957  * do adjustments if it traced a location that also has a direct
4958  * trampoline attached to it.
4959  */
4960 struct ftrace_direct_func *ftrace_find_direct_func(unsigned long addr)
4961 {
4962 	struct ftrace_direct_func *entry;
4963 	bool found = false;
4964 
4965 	/* May be called by fgraph trampoline (protected by rcu tasks) */
4966 	list_for_each_entry_rcu(entry, &ftrace_direct_funcs, next) {
4967 		if (entry->addr == addr) {
4968 			found = true;
4969 			break;
4970 		}
4971 	}
4972 	if (found)
4973 		return entry;
4974 
4975 	return NULL;
4976 }
4977 
4978 /**
4979  * register_ftrace_direct - Call a custom trampoline directly
4980  * @ip: The address of the nop at the beginning of a function
4981  * @addr: The address of the trampoline to call at @ip
4982  *
4983  * This is used to connect a direct call from the nop location (@ip)
4984  * at the start of ftrace traced functions. The location that it calls
4985  * (@addr) must be able to handle a direct call, and save the parameters
4986  * of the function being traced, and restore them (or inject new ones
4987  * if needed), before returning.
4988  *
4989  * Returns:
4990  *  0 on success
4991  *  -EBUSY - Another direct function is already attached (there can be only one)
4992  *  -ENODEV - @ip does not point to a ftrace nop location (or not supported)
4993  *  -ENOMEM - There was an allocation failure.
4994  */
4995 int register_ftrace_direct(unsigned long ip, unsigned long addr)
4996 {
4997 	struct ftrace_direct_func *direct;
4998 	struct ftrace_func_entry *entry;
4999 	struct ftrace_hash *free_hash = NULL;
5000 	struct dyn_ftrace *rec;
5001 	int ret = -EBUSY;
5002 
5003 	mutex_lock(&direct_mutex);
5004 
5005 	/* See if there's a direct function at @ip already */
5006 	if (ftrace_find_rec_direct(ip))
5007 		goto out_unlock;
5008 
5009 	ret = -ENODEV;
5010 	rec = lookup_rec(ip, ip);
5011 	if (!rec)
5012 		goto out_unlock;
5013 
5014 	/*
5015 	 * Check if the rec says it has a direct call but we didn't
5016 	 * find one earlier?
5017 	 */
5018 	if (WARN_ON(rec->flags & FTRACE_FL_DIRECT))
5019 		goto out_unlock;
5020 
5021 	/* Make sure the ip points to the exact record */
5022 	if (ip != rec->ip) {
5023 		ip = rec->ip;
5024 		/* Need to check this ip for a direct. */
5025 		if (ftrace_find_rec_direct(ip))
5026 			goto out_unlock;
5027 	}
5028 
5029 	ret = -ENOMEM;
5030 	if (ftrace_hash_empty(direct_functions) ||
5031 	    direct_functions->count > 2 * (1 << direct_functions->size_bits)) {
5032 		struct ftrace_hash *new_hash;
5033 		int size = ftrace_hash_empty(direct_functions) ? 0 :
5034 			direct_functions->count + 1;
5035 
5036 		if (size < 32)
5037 			size = 32;
5038 
5039 		new_hash = dup_hash(direct_functions, size);
5040 		if (!new_hash)
5041 			goto out_unlock;
5042 
5043 		free_hash = direct_functions;
5044 		direct_functions = new_hash;
5045 	}
5046 
5047 	entry = kmalloc(sizeof(*entry), GFP_KERNEL);
5048 	if (!entry)
5049 		goto out_unlock;
5050 
5051 	direct = ftrace_find_direct_func(addr);
5052 	if (!direct) {
5053 		direct = kmalloc(sizeof(*direct), GFP_KERNEL);
5054 		if (!direct) {
5055 			kfree(entry);
5056 			goto out_unlock;
5057 		}
5058 		direct->addr = addr;
5059 		direct->count = 0;
5060 		list_add_rcu(&direct->next, &ftrace_direct_funcs);
5061 		ftrace_direct_func_count++;
5062 	}
5063 
5064 	entry->ip = ip;
5065 	entry->direct = addr;
5066 	__add_hash_entry(direct_functions, entry);
5067 
5068 	ret = ftrace_set_filter_ip(&direct_ops, ip, 0, 0);
5069 	if (ret)
5070 		remove_hash_entry(direct_functions, entry);
5071 
5072 	if (!ret && !(direct_ops.flags & FTRACE_OPS_FL_ENABLED)) {
5073 		ret = register_ftrace_function(&direct_ops);
5074 		if (ret)
5075 			ftrace_set_filter_ip(&direct_ops, ip, 1, 0);
5076 	}
5077 
5078 	if (ret) {
5079 		kfree(entry);
5080 		if (!direct->count) {
5081 			list_del_rcu(&direct->next);
5082 			synchronize_rcu_tasks();
5083 			kfree(direct);
5084 			if (free_hash)
5085 				free_ftrace_hash(free_hash);
5086 			free_hash = NULL;
5087 			ftrace_direct_func_count--;
5088 		}
5089 	} else {
5090 		direct->count++;
5091 	}
5092  out_unlock:
5093 	mutex_unlock(&direct_mutex);
5094 
5095 	if (free_hash) {
5096 		synchronize_rcu_tasks();
5097 		free_ftrace_hash(free_hash);
5098 	}
5099 
5100 	return ret;
5101 }
5102 EXPORT_SYMBOL_GPL(register_ftrace_direct);
5103 
5104 static struct ftrace_func_entry *find_direct_entry(unsigned long *ip,
5105 						   struct dyn_ftrace **recp)
5106 {
5107 	struct ftrace_func_entry *entry;
5108 	struct dyn_ftrace *rec;
5109 
5110 	rec = lookup_rec(*ip, *ip);
5111 	if (!rec)
5112 		return NULL;
5113 
5114 	entry = __ftrace_lookup_ip(direct_functions, rec->ip);
5115 	if (!entry) {
5116 		WARN_ON(rec->flags & FTRACE_FL_DIRECT);
5117 		return NULL;
5118 	}
5119 
5120 	WARN_ON(!(rec->flags & FTRACE_FL_DIRECT));
5121 
5122 	/* Passed in ip just needs to be on the call site */
5123 	*ip = rec->ip;
5124 
5125 	if (recp)
5126 		*recp = rec;
5127 
5128 	return entry;
5129 }
5130 
5131 int unregister_ftrace_direct(unsigned long ip, unsigned long addr)
5132 {
5133 	struct ftrace_direct_func *direct;
5134 	struct ftrace_func_entry *entry;
5135 	int ret = -ENODEV;
5136 
5137 	mutex_lock(&direct_mutex);
5138 
5139 	entry = find_direct_entry(&ip, NULL);
5140 	if (!entry)
5141 		goto out_unlock;
5142 
5143 	if (direct_functions->count == 1)
5144 		unregister_ftrace_function(&direct_ops);
5145 
5146 	ret = ftrace_set_filter_ip(&direct_ops, ip, 1, 0);
5147 
5148 	WARN_ON(ret);
5149 
5150 	remove_hash_entry(direct_functions, entry);
5151 
5152 	direct = ftrace_find_direct_func(addr);
5153 	if (!WARN_ON(!direct)) {
5154 		/* This is the good path (see the ! before WARN) */
5155 		direct->count--;
5156 		WARN_ON(direct->count < 0);
5157 		if (!direct->count) {
5158 			list_del_rcu(&direct->next);
5159 			synchronize_rcu_tasks();
5160 			kfree(direct);
5161 			ftrace_direct_func_count--;
5162 		}
5163 	}
5164  out_unlock:
5165 	mutex_unlock(&direct_mutex);
5166 
5167 	return ret;
5168 }
5169 EXPORT_SYMBOL_GPL(unregister_ftrace_direct);
5170 
5171 static struct ftrace_ops stub_ops = {
5172 	.func		= ftrace_stub,
5173 };
5174 
5175 /**
5176  * ftrace_modify_direct_caller - modify ftrace nop directly
5177  * @entry: The ftrace hash entry of the direct helper for @rec
5178  * @rec: The record representing the function site to patch
5179  * @old_addr: The location that the site at @rec->ip currently calls
5180  * @new_addr: The location that the site at @rec->ip should call
5181  *
5182  * An architecture may overwrite this function to optimize the
5183  * changing of the direct callback on an ftrace nop location.
5184  * This is called with the ftrace_lock mutex held, and no other
5185  * ftrace callbacks are on the associated record (@rec). Thus,
5186  * it is safe to modify the ftrace record, where it should be
5187  * currently calling @old_addr directly, to call @new_addr.
5188  *
5189  * Safety checks should be made to make sure that the code at
5190  * @rec->ip is currently calling @old_addr. And this must
5191  * also update entry->direct to @new_addr.
5192  */
5193 int __weak ftrace_modify_direct_caller(struct ftrace_func_entry *entry,
5194 				       struct dyn_ftrace *rec,
5195 				       unsigned long old_addr,
5196 				       unsigned long new_addr)
5197 {
5198 	unsigned long ip = rec->ip;
5199 	int ret;
5200 
5201 	/*
5202 	 * The ftrace_lock was used to determine if the record
5203 	 * had more than one registered user to it. If it did,
5204 	 * we needed to prevent that from changing to do the quick
5205 	 * switch. But if it did not (only a direct caller was attached)
5206 	 * then this function is called. But this function can deal
5207 	 * with attached callers to the rec that we care about, and
5208 	 * since this function uses standard ftrace calls that take
5209 	 * the ftrace_lock mutex, we need to release it.
5210 	 */
5211 	mutex_unlock(&ftrace_lock);
5212 
5213 	/*
5214 	 * By setting a stub function at the same address, we force
5215 	 * the code to call the iterator and the direct_ops helper.
5216 	 * This means that @ip does not call the direct call, and
5217 	 * we can simply modify it.
5218 	 */
5219 	ret = ftrace_set_filter_ip(&stub_ops, ip, 0, 0);
5220 	if (ret)
5221 		goto out_lock;
5222 
5223 	ret = register_ftrace_function(&stub_ops);
5224 	if (ret) {
5225 		ftrace_set_filter_ip(&stub_ops, ip, 1, 0);
5226 		goto out_lock;
5227 	}
5228 
5229 	entry->direct = new_addr;
5230 
5231 	/*
5232 	 * By removing the stub, we put back the direct call, calling
5233 	 * the @new_addr.
5234 	 */
5235 	unregister_ftrace_function(&stub_ops);
5236 	ftrace_set_filter_ip(&stub_ops, ip, 1, 0);
5237 
5238  out_lock:
5239 	mutex_lock(&ftrace_lock);
5240 
5241 	return ret;
5242 }
5243 
5244 /**
5245  * modify_ftrace_direct - Modify an existing direct call to call something else
5246  * @ip: The instruction pointer to modify
5247  * @old_addr: The address that the current @ip calls directly
5248  * @new_addr: The address that the @ip should call
5249  *
5250  * This modifies a ftrace direct caller at an instruction pointer without
5251  * having to disable it first. The direct call will switch over to the
5252  * @new_addr without missing anything.
5253  *
5254  * Returns: zero on success. Non zero on error, which includes:
5255  *  -ENODEV : the @ip given has no direct caller attached
5256  *  -EINVAL : the @old_addr does not match the current direct caller
5257  */
5258 int modify_ftrace_direct(unsigned long ip,
5259 			 unsigned long old_addr, unsigned long new_addr)
5260 {
5261 	struct ftrace_func_entry *entry;
5262 	struct dyn_ftrace *rec;
5263 	int ret = -ENODEV;
5264 
5265 	mutex_lock(&direct_mutex);
5266 
5267 	mutex_lock(&ftrace_lock);
5268 	entry = find_direct_entry(&ip, &rec);
5269 	if (!entry)
5270 		goto out_unlock;
5271 
5272 	ret = -EINVAL;
5273 	if (entry->direct != old_addr)
5274 		goto out_unlock;
5275 
5276 	/*
5277 	 * If there's no other ftrace callback on the rec->ip location,
5278 	 * then it can be changed directly by the architecture.
5279 	 * If there is another caller, then we just need to change the
5280 	 * direct caller helper to point to @new_addr.
5281 	 */
5282 	if (ftrace_rec_count(rec) == 1) {
5283 		ret = ftrace_modify_direct_caller(entry, rec, old_addr, new_addr);
5284 	} else {
5285 		entry->direct = new_addr;
5286 		ret = 0;
5287 	}
5288 
5289  out_unlock:
5290 	mutex_unlock(&ftrace_lock);
5291 	mutex_unlock(&direct_mutex);
5292 	return ret;
5293 }
5294 EXPORT_SYMBOL_GPL(modify_ftrace_direct);
5295 #endif /* CONFIG_DYNAMIC_FTRACE_WITH_DIRECT_CALLS */
5296 
5297 /**
5298  * ftrace_set_filter_ip - set a function to filter on in ftrace by address
5299  * @ops - the ops to set the filter with
5300  * @ip - the address to add to or remove from the filter.
5301  * @remove - non zero to remove the ip from the filter
5302  * @reset - non zero to reset all filters before applying this filter.
5303  *
5304  * Filters denote which functions should be enabled when tracing is enabled
5305  * If @ip is NULL, it failes to update filter.
5306  */
5307 int ftrace_set_filter_ip(struct ftrace_ops *ops, unsigned long ip,
5308 			 int remove, int reset)
5309 {
5310 	ftrace_ops_init(ops);
5311 	return ftrace_set_addr(ops, ip, remove, reset, 1);
5312 }
5313 EXPORT_SYMBOL_GPL(ftrace_set_filter_ip);
5314 
5315 /**
5316  * ftrace_ops_set_global_filter - setup ops to use global filters
5317  * @ops - the ops which will use the global filters
5318  *
5319  * ftrace users who need global function trace filtering should call this.
5320  * It can set the global filter only if ops were not initialized before.
5321  */
5322 void ftrace_ops_set_global_filter(struct ftrace_ops *ops)
5323 {
5324 	if (ops->flags & FTRACE_OPS_FL_INITIALIZED)
5325 		return;
5326 
5327 	ftrace_ops_init(ops);
5328 	ops->func_hash = &global_ops.local_hash;
5329 }
5330 EXPORT_SYMBOL_GPL(ftrace_ops_set_global_filter);
5331 
5332 static int
5333 ftrace_set_regex(struct ftrace_ops *ops, unsigned char *buf, int len,
5334 		 int reset, int enable)
5335 {
5336 	return ftrace_set_hash(ops, buf, len, 0, 0, reset, enable);
5337 }
5338 
5339 /**
5340  * ftrace_set_filter - set a function to filter on in ftrace
5341  * @ops - the ops to set the filter with
5342  * @buf - the string that holds the function filter text.
5343  * @len - the length of the string.
5344  * @reset - non zero to reset all filters before applying this filter.
5345  *
5346  * Filters denote which functions should be enabled when tracing is enabled.
5347  * If @buf is NULL and reset is set, all functions will be enabled for tracing.
5348  */
5349 int ftrace_set_filter(struct ftrace_ops *ops, unsigned char *buf,
5350 		       int len, int reset)
5351 {
5352 	ftrace_ops_init(ops);
5353 	return ftrace_set_regex(ops, buf, len, reset, 1);
5354 }
5355 EXPORT_SYMBOL_GPL(ftrace_set_filter);
5356 
5357 /**
5358  * ftrace_set_notrace - set a function to not trace in ftrace
5359  * @ops - the ops to set the notrace filter with
5360  * @buf - the string that holds the function notrace text.
5361  * @len - the length of the string.
5362  * @reset - non zero to reset all filters before applying this filter.
5363  *
5364  * Notrace Filters denote which functions should not be enabled when tracing
5365  * is enabled. If @buf is NULL and reset is set, all functions will be enabled
5366  * for tracing.
5367  */
5368 int ftrace_set_notrace(struct ftrace_ops *ops, unsigned char *buf,
5369 			int len, int reset)
5370 {
5371 	ftrace_ops_init(ops);
5372 	return ftrace_set_regex(ops, buf, len, reset, 0);
5373 }
5374 EXPORT_SYMBOL_GPL(ftrace_set_notrace);
5375 /**
5376  * ftrace_set_global_filter - set a function to filter on with global tracers
5377  * @buf - the string that holds the function filter text.
5378  * @len - the length of the string.
5379  * @reset - non zero to reset all filters before applying this filter.
5380  *
5381  * Filters denote which functions should be enabled when tracing is enabled.
5382  * If @buf is NULL and reset is set, all functions will be enabled for tracing.
5383  */
5384 void ftrace_set_global_filter(unsigned char *buf, int len, int reset)
5385 {
5386 	ftrace_set_regex(&global_ops, buf, len, reset, 1);
5387 }
5388 EXPORT_SYMBOL_GPL(ftrace_set_global_filter);
5389 
5390 /**
5391  * ftrace_set_global_notrace - set a function to not trace with global tracers
5392  * @buf - the string that holds the function notrace text.
5393  * @len - the length of the string.
5394  * @reset - non zero to reset all filters before applying this filter.
5395  *
5396  * Notrace Filters denote which functions should not be enabled when tracing
5397  * is enabled. If @buf is NULL and reset is set, all functions will be enabled
5398  * for tracing.
5399  */
5400 void ftrace_set_global_notrace(unsigned char *buf, int len, int reset)
5401 {
5402 	ftrace_set_regex(&global_ops, buf, len, reset, 0);
5403 }
5404 EXPORT_SYMBOL_GPL(ftrace_set_global_notrace);
5405 
5406 /*
5407  * command line interface to allow users to set filters on boot up.
5408  */
5409 #define FTRACE_FILTER_SIZE		COMMAND_LINE_SIZE
5410 static char ftrace_notrace_buf[FTRACE_FILTER_SIZE] __initdata;
5411 static char ftrace_filter_buf[FTRACE_FILTER_SIZE] __initdata;
5412 
5413 /* Used by function selftest to not test if filter is set */
5414 bool ftrace_filter_param __initdata;
5415 
5416 static int __init set_ftrace_notrace(char *str)
5417 {
5418 	ftrace_filter_param = true;
5419 	strlcpy(ftrace_notrace_buf, str, FTRACE_FILTER_SIZE);
5420 	return 1;
5421 }
5422 __setup("ftrace_notrace=", set_ftrace_notrace);
5423 
5424 static int __init set_ftrace_filter(char *str)
5425 {
5426 	ftrace_filter_param = true;
5427 	strlcpy(ftrace_filter_buf, str, FTRACE_FILTER_SIZE);
5428 	return 1;
5429 }
5430 __setup("ftrace_filter=", set_ftrace_filter);
5431 
5432 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
5433 static char ftrace_graph_buf[FTRACE_FILTER_SIZE] __initdata;
5434 static char ftrace_graph_notrace_buf[FTRACE_FILTER_SIZE] __initdata;
5435 static int ftrace_graph_set_hash(struct ftrace_hash *hash, char *buffer);
5436 
5437 static int __init set_graph_function(char *str)
5438 {
5439 	strlcpy(ftrace_graph_buf, str, FTRACE_FILTER_SIZE);
5440 	return 1;
5441 }
5442 __setup("ftrace_graph_filter=", set_graph_function);
5443 
5444 static int __init set_graph_notrace_function(char *str)
5445 {
5446 	strlcpy(ftrace_graph_notrace_buf, str, FTRACE_FILTER_SIZE);
5447 	return 1;
5448 }
5449 __setup("ftrace_graph_notrace=", set_graph_notrace_function);
5450 
5451 static int __init set_graph_max_depth_function(char *str)
5452 {
5453 	if (!str)
5454 		return 0;
5455 	fgraph_max_depth = simple_strtoul(str, NULL, 0);
5456 	return 1;
5457 }
5458 __setup("ftrace_graph_max_depth=", set_graph_max_depth_function);
5459 
5460 static void __init set_ftrace_early_graph(char *buf, int enable)
5461 {
5462 	int ret;
5463 	char *func;
5464 	struct ftrace_hash *hash;
5465 
5466 	hash = alloc_ftrace_hash(FTRACE_HASH_DEFAULT_BITS);
5467 	if (WARN_ON(!hash))
5468 		return;
5469 
5470 	while (buf) {
5471 		func = strsep(&buf, ",");
5472 		/* we allow only one expression at a time */
5473 		ret = ftrace_graph_set_hash(hash, func);
5474 		if (ret)
5475 			printk(KERN_DEBUG "ftrace: function %s not "
5476 					  "traceable\n", func);
5477 	}
5478 
5479 	if (enable)
5480 		ftrace_graph_hash = hash;
5481 	else
5482 		ftrace_graph_notrace_hash = hash;
5483 }
5484 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
5485 
5486 void __init
5487 ftrace_set_early_filter(struct ftrace_ops *ops, char *buf, int enable)
5488 {
5489 	char *func;
5490 
5491 	ftrace_ops_init(ops);
5492 
5493 	while (buf) {
5494 		func = strsep(&buf, ",");
5495 		ftrace_set_regex(ops, func, strlen(func), 0, enable);
5496 	}
5497 }
5498 
5499 static void __init set_ftrace_early_filters(void)
5500 {
5501 	if (ftrace_filter_buf[0])
5502 		ftrace_set_early_filter(&global_ops, ftrace_filter_buf, 1);
5503 	if (ftrace_notrace_buf[0])
5504 		ftrace_set_early_filter(&global_ops, ftrace_notrace_buf, 0);
5505 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
5506 	if (ftrace_graph_buf[0])
5507 		set_ftrace_early_graph(ftrace_graph_buf, 1);
5508 	if (ftrace_graph_notrace_buf[0])
5509 		set_ftrace_early_graph(ftrace_graph_notrace_buf, 0);
5510 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
5511 }
5512 
5513 int ftrace_regex_release(struct inode *inode, struct file *file)
5514 {
5515 	struct seq_file *m = (struct seq_file *)file->private_data;
5516 	struct ftrace_iterator *iter;
5517 	struct ftrace_hash **orig_hash;
5518 	struct trace_parser *parser;
5519 	int filter_hash;
5520 	int ret;
5521 
5522 	if (file->f_mode & FMODE_READ) {
5523 		iter = m->private;
5524 		seq_release(inode, file);
5525 	} else
5526 		iter = file->private_data;
5527 
5528 	parser = &iter->parser;
5529 	if (trace_parser_loaded(parser)) {
5530 		ftrace_match_records(iter->hash, parser->buffer, parser->idx);
5531 	}
5532 
5533 	trace_parser_put(parser);
5534 
5535 	mutex_lock(&iter->ops->func_hash->regex_lock);
5536 
5537 	if (file->f_mode & FMODE_WRITE) {
5538 		filter_hash = !!(iter->flags & FTRACE_ITER_FILTER);
5539 
5540 		if (filter_hash) {
5541 			orig_hash = &iter->ops->func_hash->filter_hash;
5542 			if (iter->tr && !list_empty(&iter->tr->mod_trace))
5543 				iter->hash->flags |= FTRACE_HASH_FL_MOD;
5544 		} else
5545 			orig_hash = &iter->ops->func_hash->notrace_hash;
5546 
5547 		mutex_lock(&ftrace_lock);
5548 		ret = ftrace_hash_move_and_update_ops(iter->ops, orig_hash,
5549 						      iter->hash, filter_hash);
5550 		mutex_unlock(&ftrace_lock);
5551 	} else {
5552 		/* For read only, the hash is the ops hash */
5553 		iter->hash = NULL;
5554 	}
5555 
5556 	mutex_unlock(&iter->ops->func_hash->regex_lock);
5557 	free_ftrace_hash(iter->hash);
5558 	if (iter->tr)
5559 		trace_array_put(iter->tr);
5560 	kfree(iter);
5561 
5562 	return 0;
5563 }
5564 
5565 static const struct file_operations ftrace_avail_fops = {
5566 	.open = ftrace_avail_open,
5567 	.read = seq_read,
5568 	.llseek = seq_lseek,
5569 	.release = seq_release_private,
5570 };
5571 
5572 static const struct file_operations ftrace_enabled_fops = {
5573 	.open = ftrace_enabled_open,
5574 	.read = seq_read,
5575 	.llseek = seq_lseek,
5576 	.release = seq_release_private,
5577 };
5578 
5579 static const struct file_operations ftrace_filter_fops = {
5580 	.open = ftrace_filter_open,
5581 	.read = seq_read,
5582 	.write = ftrace_filter_write,
5583 	.llseek = tracing_lseek,
5584 	.release = ftrace_regex_release,
5585 };
5586 
5587 static const struct file_operations ftrace_notrace_fops = {
5588 	.open = ftrace_notrace_open,
5589 	.read = seq_read,
5590 	.write = ftrace_notrace_write,
5591 	.llseek = tracing_lseek,
5592 	.release = ftrace_regex_release,
5593 };
5594 
5595 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
5596 
5597 static DEFINE_MUTEX(graph_lock);
5598 
5599 struct ftrace_hash *ftrace_graph_hash = EMPTY_HASH;
5600 struct ftrace_hash *ftrace_graph_notrace_hash = EMPTY_HASH;
5601 
5602 enum graph_filter_type {
5603 	GRAPH_FILTER_NOTRACE	= 0,
5604 	GRAPH_FILTER_FUNCTION,
5605 };
5606 
5607 #define FTRACE_GRAPH_EMPTY	((void *)1)
5608 
5609 struct ftrace_graph_data {
5610 	struct ftrace_hash		*hash;
5611 	struct ftrace_func_entry	*entry;
5612 	int				idx;   /* for hash table iteration */
5613 	enum graph_filter_type		type;
5614 	struct ftrace_hash		*new_hash;
5615 	const struct seq_operations	*seq_ops;
5616 	struct trace_parser		parser;
5617 };
5618 
5619 static void *
5620 __g_next(struct seq_file *m, loff_t *pos)
5621 {
5622 	struct ftrace_graph_data *fgd = m->private;
5623 	struct ftrace_func_entry *entry = fgd->entry;
5624 	struct hlist_head *head;
5625 	int i, idx = fgd->idx;
5626 
5627 	if (*pos >= fgd->hash->count)
5628 		return NULL;
5629 
5630 	if (entry) {
5631 		hlist_for_each_entry_continue(entry, hlist) {
5632 			fgd->entry = entry;
5633 			return entry;
5634 		}
5635 
5636 		idx++;
5637 	}
5638 
5639 	for (i = idx; i < 1 << fgd->hash->size_bits; i++) {
5640 		head = &fgd->hash->buckets[i];
5641 		hlist_for_each_entry(entry, head, hlist) {
5642 			fgd->entry = entry;
5643 			fgd->idx = i;
5644 			return entry;
5645 		}
5646 	}
5647 	return NULL;
5648 }
5649 
5650 static void *
5651 g_next(struct seq_file *m, void *v, loff_t *pos)
5652 {
5653 	(*pos)++;
5654 	return __g_next(m, pos);
5655 }
5656 
5657 static void *g_start(struct seq_file *m, loff_t *pos)
5658 {
5659 	struct ftrace_graph_data *fgd = m->private;
5660 
5661 	mutex_lock(&graph_lock);
5662 
5663 	if (fgd->type == GRAPH_FILTER_FUNCTION)
5664 		fgd->hash = rcu_dereference_protected(ftrace_graph_hash,
5665 					lockdep_is_held(&graph_lock));
5666 	else
5667 		fgd->hash = rcu_dereference_protected(ftrace_graph_notrace_hash,
5668 					lockdep_is_held(&graph_lock));
5669 
5670 	/* Nothing, tell g_show to print all functions are enabled */
5671 	if (ftrace_hash_empty(fgd->hash) && !*pos)
5672 		return FTRACE_GRAPH_EMPTY;
5673 
5674 	fgd->idx = 0;
5675 	fgd->entry = NULL;
5676 	return __g_next(m, pos);
5677 }
5678 
5679 static void g_stop(struct seq_file *m, void *p)
5680 {
5681 	mutex_unlock(&graph_lock);
5682 }
5683 
5684 static int g_show(struct seq_file *m, void *v)
5685 {
5686 	struct ftrace_func_entry *entry = v;
5687 
5688 	if (!entry)
5689 		return 0;
5690 
5691 	if (entry == FTRACE_GRAPH_EMPTY) {
5692 		struct ftrace_graph_data *fgd = m->private;
5693 
5694 		if (fgd->type == GRAPH_FILTER_FUNCTION)
5695 			seq_puts(m, "#### all functions enabled ####\n");
5696 		else
5697 			seq_puts(m, "#### no functions disabled ####\n");
5698 		return 0;
5699 	}
5700 
5701 	seq_printf(m, "%ps\n", (void *)entry->ip);
5702 
5703 	return 0;
5704 }
5705 
5706 static const struct seq_operations ftrace_graph_seq_ops = {
5707 	.start = g_start,
5708 	.next = g_next,
5709 	.stop = g_stop,
5710 	.show = g_show,
5711 };
5712 
5713 static int
5714 __ftrace_graph_open(struct inode *inode, struct file *file,
5715 		    struct ftrace_graph_data *fgd)
5716 {
5717 	int ret;
5718 	struct ftrace_hash *new_hash = NULL;
5719 
5720 	ret = security_locked_down(LOCKDOWN_TRACEFS);
5721 	if (ret)
5722 		return ret;
5723 
5724 	if (file->f_mode & FMODE_WRITE) {
5725 		const int size_bits = FTRACE_HASH_DEFAULT_BITS;
5726 
5727 		if (trace_parser_get_init(&fgd->parser, FTRACE_BUFF_MAX))
5728 			return -ENOMEM;
5729 
5730 		if (file->f_flags & O_TRUNC)
5731 			new_hash = alloc_ftrace_hash(size_bits);
5732 		else
5733 			new_hash = alloc_and_copy_ftrace_hash(size_bits,
5734 							      fgd->hash);
5735 		if (!new_hash) {
5736 			ret = -ENOMEM;
5737 			goto out;
5738 		}
5739 	}
5740 
5741 	if (file->f_mode & FMODE_READ) {
5742 		ret = seq_open(file, &ftrace_graph_seq_ops);
5743 		if (!ret) {
5744 			struct seq_file *m = file->private_data;
5745 			m->private = fgd;
5746 		} else {
5747 			/* Failed */
5748 			free_ftrace_hash(new_hash);
5749 			new_hash = NULL;
5750 		}
5751 	} else
5752 		file->private_data = fgd;
5753 
5754 out:
5755 	if (ret < 0 && file->f_mode & FMODE_WRITE)
5756 		trace_parser_put(&fgd->parser);
5757 
5758 	fgd->new_hash = new_hash;
5759 
5760 	/*
5761 	 * All uses of fgd->hash must be taken with the graph_lock
5762 	 * held. The graph_lock is going to be released, so force
5763 	 * fgd->hash to be reinitialized when it is taken again.
5764 	 */
5765 	fgd->hash = NULL;
5766 
5767 	return ret;
5768 }
5769 
5770 static int
5771 ftrace_graph_open(struct inode *inode, struct file *file)
5772 {
5773 	struct ftrace_graph_data *fgd;
5774 	int ret;
5775 
5776 	if (unlikely(ftrace_disabled))
5777 		return -ENODEV;
5778 
5779 	fgd = kmalloc(sizeof(*fgd), GFP_KERNEL);
5780 	if (fgd == NULL)
5781 		return -ENOMEM;
5782 
5783 	mutex_lock(&graph_lock);
5784 
5785 	fgd->hash = rcu_dereference_protected(ftrace_graph_hash,
5786 					lockdep_is_held(&graph_lock));
5787 	fgd->type = GRAPH_FILTER_FUNCTION;
5788 	fgd->seq_ops = &ftrace_graph_seq_ops;
5789 
5790 	ret = __ftrace_graph_open(inode, file, fgd);
5791 	if (ret < 0)
5792 		kfree(fgd);
5793 
5794 	mutex_unlock(&graph_lock);
5795 	return ret;
5796 }
5797 
5798 static int
5799 ftrace_graph_notrace_open(struct inode *inode, struct file *file)
5800 {
5801 	struct ftrace_graph_data *fgd;
5802 	int ret;
5803 
5804 	if (unlikely(ftrace_disabled))
5805 		return -ENODEV;
5806 
5807 	fgd = kmalloc(sizeof(*fgd), GFP_KERNEL);
5808 	if (fgd == NULL)
5809 		return -ENOMEM;
5810 
5811 	mutex_lock(&graph_lock);
5812 
5813 	fgd->hash = rcu_dereference_protected(ftrace_graph_notrace_hash,
5814 					lockdep_is_held(&graph_lock));
5815 	fgd->type = GRAPH_FILTER_NOTRACE;
5816 	fgd->seq_ops = &ftrace_graph_seq_ops;
5817 
5818 	ret = __ftrace_graph_open(inode, file, fgd);
5819 	if (ret < 0)
5820 		kfree(fgd);
5821 
5822 	mutex_unlock(&graph_lock);
5823 	return ret;
5824 }
5825 
5826 static int
5827 ftrace_graph_release(struct inode *inode, struct file *file)
5828 {
5829 	struct ftrace_graph_data *fgd;
5830 	struct ftrace_hash *old_hash, *new_hash;
5831 	struct trace_parser *parser;
5832 	int ret = 0;
5833 
5834 	if (file->f_mode & FMODE_READ) {
5835 		struct seq_file *m = file->private_data;
5836 
5837 		fgd = m->private;
5838 		seq_release(inode, file);
5839 	} else {
5840 		fgd = file->private_data;
5841 	}
5842 
5843 
5844 	if (file->f_mode & FMODE_WRITE) {
5845 
5846 		parser = &fgd->parser;
5847 
5848 		if (trace_parser_loaded((parser))) {
5849 			ret = ftrace_graph_set_hash(fgd->new_hash,
5850 						    parser->buffer);
5851 		}
5852 
5853 		trace_parser_put(parser);
5854 
5855 		new_hash = __ftrace_hash_move(fgd->new_hash);
5856 		if (!new_hash) {
5857 			ret = -ENOMEM;
5858 			goto out;
5859 		}
5860 
5861 		mutex_lock(&graph_lock);
5862 
5863 		if (fgd->type == GRAPH_FILTER_FUNCTION) {
5864 			old_hash = rcu_dereference_protected(ftrace_graph_hash,
5865 					lockdep_is_held(&graph_lock));
5866 			rcu_assign_pointer(ftrace_graph_hash, new_hash);
5867 		} else {
5868 			old_hash = rcu_dereference_protected(ftrace_graph_notrace_hash,
5869 					lockdep_is_held(&graph_lock));
5870 			rcu_assign_pointer(ftrace_graph_notrace_hash, new_hash);
5871 		}
5872 
5873 		mutex_unlock(&graph_lock);
5874 
5875 		/* Wait till all users are no longer using the old hash */
5876 		synchronize_rcu();
5877 
5878 		free_ftrace_hash(old_hash);
5879 	}
5880 
5881  out:
5882 	free_ftrace_hash(fgd->new_hash);
5883 	kfree(fgd);
5884 
5885 	return ret;
5886 }
5887 
5888 static int
5889 ftrace_graph_set_hash(struct ftrace_hash *hash, char *buffer)
5890 {
5891 	struct ftrace_glob func_g;
5892 	struct dyn_ftrace *rec;
5893 	struct ftrace_page *pg;
5894 	struct ftrace_func_entry *entry;
5895 	int fail = 1;
5896 	int not;
5897 
5898 	/* decode regex */
5899 	func_g.type = filter_parse_regex(buffer, strlen(buffer),
5900 					 &func_g.search, &not);
5901 
5902 	func_g.len = strlen(func_g.search);
5903 
5904 	mutex_lock(&ftrace_lock);
5905 
5906 	if (unlikely(ftrace_disabled)) {
5907 		mutex_unlock(&ftrace_lock);
5908 		return -ENODEV;
5909 	}
5910 
5911 	do_for_each_ftrace_rec(pg, rec) {
5912 
5913 		if (rec->flags & FTRACE_FL_DISABLED)
5914 			continue;
5915 
5916 		if (ftrace_match_record(rec, &func_g, NULL, 0)) {
5917 			entry = ftrace_lookup_ip(hash, rec->ip);
5918 
5919 			if (!not) {
5920 				fail = 0;
5921 
5922 				if (entry)
5923 					continue;
5924 				if (add_hash_entry(hash, rec->ip) < 0)
5925 					goto out;
5926 			} else {
5927 				if (entry) {
5928 					free_hash_entry(hash, entry);
5929 					fail = 0;
5930 				}
5931 			}
5932 		}
5933 	} while_for_each_ftrace_rec();
5934 out:
5935 	mutex_unlock(&ftrace_lock);
5936 
5937 	if (fail)
5938 		return -EINVAL;
5939 
5940 	return 0;
5941 }
5942 
5943 static ssize_t
5944 ftrace_graph_write(struct file *file, const char __user *ubuf,
5945 		   size_t cnt, loff_t *ppos)
5946 {
5947 	ssize_t read, ret = 0;
5948 	struct ftrace_graph_data *fgd = file->private_data;
5949 	struct trace_parser *parser;
5950 
5951 	if (!cnt)
5952 		return 0;
5953 
5954 	/* Read mode uses seq functions */
5955 	if (file->f_mode & FMODE_READ) {
5956 		struct seq_file *m = file->private_data;
5957 		fgd = m->private;
5958 	}
5959 
5960 	parser = &fgd->parser;
5961 
5962 	read = trace_get_user(parser, ubuf, cnt, ppos);
5963 
5964 	if (read >= 0 && trace_parser_loaded(parser) &&
5965 	    !trace_parser_cont(parser)) {
5966 
5967 		ret = ftrace_graph_set_hash(fgd->new_hash,
5968 					    parser->buffer);
5969 		trace_parser_clear(parser);
5970 	}
5971 
5972 	if (!ret)
5973 		ret = read;
5974 
5975 	return ret;
5976 }
5977 
5978 static const struct file_operations ftrace_graph_fops = {
5979 	.open		= ftrace_graph_open,
5980 	.read		= seq_read,
5981 	.write		= ftrace_graph_write,
5982 	.llseek		= tracing_lseek,
5983 	.release	= ftrace_graph_release,
5984 };
5985 
5986 static const struct file_operations ftrace_graph_notrace_fops = {
5987 	.open		= ftrace_graph_notrace_open,
5988 	.read		= seq_read,
5989 	.write		= ftrace_graph_write,
5990 	.llseek		= tracing_lseek,
5991 	.release	= ftrace_graph_release,
5992 };
5993 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
5994 
5995 void ftrace_create_filter_files(struct ftrace_ops *ops,
5996 				struct dentry *parent)
5997 {
5998 
5999 	trace_create_file("set_ftrace_filter", 0644, parent,
6000 			  ops, &ftrace_filter_fops);
6001 
6002 	trace_create_file("set_ftrace_notrace", 0644, parent,
6003 			  ops, &ftrace_notrace_fops);
6004 }
6005 
6006 /*
6007  * The name "destroy_filter_files" is really a misnomer. Although
6008  * in the future, it may actually delete the files, but this is
6009  * really intended to make sure the ops passed in are disabled
6010  * and that when this function returns, the caller is free to
6011  * free the ops.
6012  *
6013  * The "destroy" name is only to match the "create" name that this
6014  * should be paired with.
6015  */
6016 void ftrace_destroy_filter_files(struct ftrace_ops *ops)
6017 {
6018 	mutex_lock(&ftrace_lock);
6019 	if (ops->flags & FTRACE_OPS_FL_ENABLED)
6020 		ftrace_shutdown(ops, 0);
6021 	ops->flags |= FTRACE_OPS_FL_DELETED;
6022 	ftrace_free_filter(ops);
6023 	mutex_unlock(&ftrace_lock);
6024 }
6025 
6026 static __init int ftrace_init_dyn_tracefs(struct dentry *d_tracer)
6027 {
6028 
6029 	trace_create_file("available_filter_functions", 0444,
6030 			d_tracer, NULL, &ftrace_avail_fops);
6031 
6032 	trace_create_file("enabled_functions", 0444,
6033 			d_tracer, NULL, &ftrace_enabled_fops);
6034 
6035 	ftrace_create_filter_files(&global_ops, d_tracer);
6036 
6037 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
6038 	trace_create_file("set_graph_function", 0644, d_tracer,
6039 				    NULL,
6040 				    &ftrace_graph_fops);
6041 	trace_create_file("set_graph_notrace", 0644, d_tracer,
6042 				    NULL,
6043 				    &ftrace_graph_notrace_fops);
6044 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
6045 
6046 	return 0;
6047 }
6048 
6049 static int ftrace_cmp_ips(const void *a, const void *b)
6050 {
6051 	const unsigned long *ipa = a;
6052 	const unsigned long *ipb = b;
6053 
6054 	if (*ipa > *ipb)
6055 		return 1;
6056 	if (*ipa < *ipb)
6057 		return -1;
6058 	return 0;
6059 }
6060 
6061 static int ftrace_process_locs(struct module *mod,
6062 			       unsigned long *start,
6063 			       unsigned long *end)
6064 {
6065 	struct ftrace_page *start_pg;
6066 	struct ftrace_page *pg;
6067 	struct dyn_ftrace *rec;
6068 	unsigned long count;
6069 	unsigned long *p;
6070 	unsigned long addr;
6071 	unsigned long flags = 0; /* Shut up gcc */
6072 	int ret = -ENOMEM;
6073 
6074 	count = end - start;
6075 
6076 	if (!count)
6077 		return 0;
6078 
6079 	sort(start, count, sizeof(*start),
6080 	     ftrace_cmp_ips, NULL);
6081 
6082 	start_pg = ftrace_allocate_pages(count);
6083 	if (!start_pg)
6084 		return -ENOMEM;
6085 
6086 	mutex_lock(&ftrace_lock);
6087 
6088 	/*
6089 	 * Core and each module needs their own pages, as
6090 	 * modules will free them when they are removed.
6091 	 * Force a new page to be allocated for modules.
6092 	 */
6093 	if (!mod) {
6094 		WARN_ON(ftrace_pages || ftrace_pages_start);
6095 		/* First initialization */
6096 		ftrace_pages = ftrace_pages_start = start_pg;
6097 	} else {
6098 		if (!ftrace_pages)
6099 			goto out;
6100 
6101 		if (WARN_ON(ftrace_pages->next)) {
6102 			/* Hmm, we have free pages? */
6103 			while (ftrace_pages->next)
6104 				ftrace_pages = ftrace_pages->next;
6105 		}
6106 
6107 		ftrace_pages->next = start_pg;
6108 	}
6109 
6110 	p = start;
6111 	pg = start_pg;
6112 	while (p < end) {
6113 		addr = ftrace_call_adjust(*p++);
6114 		/*
6115 		 * Some architecture linkers will pad between
6116 		 * the different mcount_loc sections of different
6117 		 * object files to satisfy alignments.
6118 		 * Skip any NULL pointers.
6119 		 */
6120 		if (!addr)
6121 			continue;
6122 
6123 		if (pg->index == pg->size) {
6124 			/* We should have allocated enough */
6125 			if (WARN_ON(!pg->next))
6126 				break;
6127 			pg = pg->next;
6128 		}
6129 
6130 		rec = &pg->records[pg->index++];
6131 		rec->ip = addr;
6132 	}
6133 
6134 	/* We should have used all pages */
6135 	WARN_ON(pg->next);
6136 
6137 	/* Assign the last page to ftrace_pages */
6138 	ftrace_pages = pg;
6139 
6140 	/*
6141 	 * We only need to disable interrupts on start up
6142 	 * because we are modifying code that an interrupt
6143 	 * may execute, and the modification is not atomic.
6144 	 * But for modules, nothing runs the code we modify
6145 	 * until we are finished with it, and there's no
6146 	 * reason to cause large interrupt latencies while we do it.
6147 	 */
6148 	if (!mod)
6149 		local_irq_save(flags);
6150 	ftrace_update_code(mod, start_pg);
6151 	if (!mod)
6152 		local_irq_restore(flags);
6153 	ret = 0;
6154  out:
6155 	mutex_unlock(&ftrace_lock);
6156 
6157 	return ret;
6158 }
6159 
6160 struct ftrace_mod_func {
6161 	struct list_head	list;
6162 	char			*name;
6163 	unsigned long		ip;
6164 	unsigned int		size;
6165 };
6166 
6167 struct ftrace_mod_map {
6168 	struct rcu_head		rcu;
6169 	struct list_head	list;
6170 	struct module		*mod;
6171 	unsigned long		start_addr;
6172 	unsigned long		end_addr;
6173 	struct list_head	funcs;
6174 	unsigned int		num_funcs;
6175 };
6176 
6177 #ifdef CONFIG_MODULES
6178 
6179 #define next_to_ftrace_page(p) container_of(p, struct ftrace_page, next)
6180 
6181 static LIST_HEAD(ftrace_mod_maps);
6182 
6183 static int referenced_filters(struct dyn_ftrace *rec)
6184 {
6185 	struct ftrace_ops *ops;
6186 	int cnt = 0;
6187 
6188 	for (ops = ftrace_ops_list; ops != &ftrace_list_end; ops = ops->next) {
6189 		if (ops_references_rec(ops, rec))
6190 		    cnt++;
6191 	}
6192 
6193 	return cnt;
6194 }
6195 
6196 static void
6197 clear_mod_from_hash(struct ftrace_page *pg, struct ftrace_hash *hash)
6198 {
6199 	struct ftrace_func_entry *entry;
6200 	struct dyn_ftrace *rec;
6201 	int i;
6202 
6203 	if (ftrace_hash_empty(hash))
6204 		return;
6205 
6206 	for (i = 0; i < pg->index; i++) {
6207 		rec = &pg->records[i];
6208 		entry = __ftrace_lookup_ip(hash, rec->ip);
6209 		/*
6210 		 * Do not allow this rec to match again.
6211 		 * Yeah, it may waste some memory, but will be removed
6212 		 * if/when the hash is modified again.
6213 		 */
6214 		if (entry)
6215 			entry->ip = 0;
6216 	}
6217 }
6218 
6219 /* Clear any records from hashs */
6220 static void clear_mod_from_hashes(struct ftrace_page *pg)
6221 {
6222 	struct trace_array *tr;
6223 
6224 	mutex_lock(&trace_types_lock);
6225 	list_for_each_entry(tr, &ftrace_trace_arrays, list) {
6226 		if (!tr->ops || !tr->ops->func_hash)
6227 			continue;
6228 		mutex_lock(&tr->ops->func_hash->regex_lock);
6229 		clear_mod_from_hash(pg, tr->ops->func_hash->filter_hash);
6230 		clear_mod_from_hash(pg, tr->ops->func_hash->notrace_hash);
6231 		mutex_unlock(&tr->ops->func_hash->regex_lock);
6232 	}
6233 	mutex_unlock(&trace_types_lock);
6234 }
6235 
6236 static void ftrace_free_mod_map(struct rcu_head *rcu)
6237 {
6238 	struct ftrace_mod_map *mod_map = container_of(rcu, struct ftrace_mod_map, rcu);
6239 	struct ftrace_mod_func *mod_func;
6240 	struct ftrace_mod_func *n;
6241 
6242 	/* All the contents of mod_map are now not visible to readers */
6243 	list_for_each_entry_safe(mod_func, n, &mod_map->funcs, list) {
6244 		kfree(mod_func->name);
6245 		list_del(&mod_func->list);
6246 		kfree(mod_func);
6247 	}
6248 
6249 	kfree(mod_map);
6250 }
6251 
6252 void ftrace_release_mod(struct module *mod)
6253 {
6254 	struct ftrace_mod_map *mod_map;
6255 	struct ftrace_mod_map *n;
6256 	struct dyn_ftrace *rec;
6257 	struct ftrace_page **last_pg;
6258 	struct ftrace_page *tmp_page = NULL;
6259 	struct ftrace_page *pg;
6260 	int order;
6261 
6262 	mutex_lock(&ftrace_lock);
6263 
6264 	if (ftrace_disabled)
6265 		goto out_unlock;
6266 
6267 	list_for_each_entry_safe(mod_map, n, &ftrace_mod_maps, list) {
6268 		if (mod_map->mod == mod) {
6269 			list_del_rcu(&mod_map->list);
6270 			call_rcu(&mod_map->rcu, ftrace_free_mod_map);
6271 			break;
6272 		}
6273 	}
6274 
6275 	/*
6276 	 * Each module has its own ftrace_pages, remove
6277 	 * them from the list.
6278 	 */
6279 	last_pg = &ftrace_pages_start;
6280 	for (pg = ftrace_pages_start; pg; pg = *last_pg) {
6281 		rec = &pg->records[0];
6282 		if (within_module_core(rec->ip, mod) ||
6283 		    within_module_init(rec->ip, mod)) {
6284 			/*
6285 			 * As core pages are first, the first
6286 			 * page should never be a module page.
6287 			 */
6288 			if (WARN_ON(pg == ftrace_pages_start))
6289 				goto out_unlock;
6290 
6291 			/* Check if we are deleting the last page */
6292 			if (pg == ftrace_pages)
6293 				ftrace_pages = next_to_ftrace_page(last_pg);
6294 
6295 			ftrace_update_tot_cnt -= pg->index;
6296 			*last_pg = pg->next;
6297 
6298 			pg->next = tmp_page;
6299 			tmp_page = pg;
6300 		} else
6301 			last_pg = &pg->next;
6302 	}
6303  out_unlock:
6304 	mutex_unlock(&ftrace_lock);
6305 
6306 	for (pg = tmp_page; pg; pg = tmp_page) {
6307 
6308 		/* Needs to be called outside of ftrace_lock */
6309 		clear_mod_from_hashes(pg);
6310 
6311 		order = get_count_order(pg->size / ENTRIES_PER_PAGE);
6312 		free_pages((unsigned long)pg->records, order);
6313 		tmp_page = pg->next;
6314 		kfree(pg);
6315 		ftrace_number_of_pages -= 1 << order;
6316 		ftrace_number_of_groups--;
6317 	}
6318 }
6319 
6320 void ftrace_module_enable(struct module *mod)
6321 {
6322 	struct dyn_ftrace *rec;
6323 	struct ftrace_page *pg;
6324 
6325 	mutex_lock(&ftrace_lock);
6326 
6327 	if (ftrace_disabled)
6328 		goto out_unlock;
6329 
6330 	/*
6331 	 * If the tracing is enabled, go ahead and enable the record.
6332 	 *
6333 	 * The reason not to enable the record immediately is the
6334 	 * inherent check of ftrace_make_nop/ftrace_make_call for
6335 	 * correct previous instructions.  Making first the NOP
6336 	 * conversion puts the module to the correct state, thus
6337 	 * passing the ftrace_make_call check.
6338 	 *
6339 	 * We also delay this to after the module code already set the
6340 	 * text to read-only, as we now need to set it back to read-write
6341 	 * so that we can modify the text.
6342 	 */
6343 	if (ftrace_start_up)
6344 		ftrace_arch_code_modify_prepare();
6345 
6346 	do_for_each_ftrace_rec(pg, rec) {
6347 		int cnt;
6348 		/*
6349 		 * do_for_each_ftrace_rec() is a double loop.
6350 		 * module text shares the pg. If a record is
6351 		 * not part of this module, then skip this pg,
6352 		 * which the "break" will do.
6353 		 */
6354 		if (!within_module_core(rec->ip, mod) &&
6355 		    !within_module_init(rec->ip, mod))
6356 			break;
6357 
6358 		cnt = 0;
6359 
6360 		/*
6361 		 * When adding a module, we need to check if tracers are
6362 		 * currently enabled and if they are, and can trace this record,
6363 		 * we need to enable the module functions as well as update the
6364 		 * reference counts for those function records.
6365 		 */
6366 		if (ftrace_start_up)
6367 			cnt += referenced_filters(rec);
6368 
6369 		/* This clears FTRACE_FL_DISABLED */
6370 		rec->flags = cnt;
6371 
6372 		if (ftrace_start_up && cnt) {
6373 			int failed = __ftrace_replace_code(rec, 1);
6374 			if (failed) {
6375 				ftrace_bug(failed, rec);
6376 				goto out_loop;
6377 			}
6378 		}
6379 
6380 	} while_for_each_ftrace_rec();
6381 
6382  out_loop:
6383 	if (ftrace_start_up)
6384 		ftrace_arch_code_modify_post_process();
6385 
6386  out_unlock:
6387 	mutex_unlock(&ftrace_lock);
6388 
6389 	process_cached_mods(mod->name);
6390 }
6391 
6392 void ftrace_module_init(struct module *mod)
6393 {
6394 	if (ftrace_disabled || !mod->num_ftrace_callsites)
6395 		return;
6396 
6397 	ftrace_process_locs(mod, mod->ftrace_callsites,
6398 			    mod->ftrace_callsites + mod->num_ftrace_callsites);
6399 }
6400 
6401 static void save_ftrace_mod_rec(struct ftrace_mod_map *mod_map,
6402 				struct dyn_ftrace *rec)
6403 {
6404 	struct ftrace_mod_func *mod_func;
6405 	unsigned long symsize;
6406 	unsigned long offset;
6407 	char str[KSYM_SYMBOL_LEN];
6408 	char *modname;
6409 	const char *ret;
6410 
6411 	ret = kallsyms_lookup(rec->ip, &symsize, &offset, &modname, str);
6412 	if (!ret)
6413 		return;
6414 
6415 	mod_func = kmalloc(sizeof(*mod_func), GFP_KERNEL);
6416 	if (!mod_func)
6417 		return;
6418 
6419 	mod_func->name = kstrdup(str, GFP_KERNEL);
6420 	if (!mod_func->name) {
6421 		kfree(mod_func);
6422 		return;
6423 	}
6424 
6425 	mod_func->ip = rec->ip - offset;
6426 	mod_func->size = symsize;
6427 
6428 	mod_map->num_funcs++;
6429 
6430 	list_add_rcu(&mod_func->list, &mod_map->funcs);
6431 }
6432 
6433 static struct ftrace_mod_map *
6434 allocate_ftrace_mod_map(struct module *mod,
6435 			unsigned long start, unsigned long end)
6436 {
6437 	struct ftrace_mod_map *mod_map;
6438 
6439 	mod_map = kmalloc(sizeof(*mod_map), GFP_KERNEL);
6440 	if (!mod_map)
6441 		return NULL;
6442 
6443 	mod_map->mod = mod;
6444 	mod_map->start_addr = start;
6445 	mod_map->end_addr = end;
6446 	mod_map->num_funcs = 0;
6447 
6448 	INIT_LIST_HEAD_RCU(&mod_map->funcs);
6449 
6450 	list_add_rcu(&mod_map->list, &ftrace_mod_maps);
6451 
6452 	return mod_map;
6453 }
6454 
6455 static const char *
6456 ftrace_func_address_lookup(struct ftrace_mod_map *mod_map,
6457 			   unsigned long addr, unsigned long *size,
6458 			   unsigned long *off, char *sym)
6459 {
6460 	struct ftrace_mod_func *found_func =  NULL;
6461 	struct ftrace_mod_func *mod_func;
6462 
6463 	list_for_each_entry_rcu(mod_func, &mod_map->funcs, list) {
6464 		if (addr >= mod_func->ip &&
6465 		    addr < mod_func->ip + mod_func->size) {
6466 			found_func = mod_func;
6467 			break;
6468 		}
6469 	}
6470 
6471 	if (found_func) {
6472 		if (size)
6473 			*size = found_func->size;
6474 		if (off)
6475 			*off = addr - found_func->ip;
6476 		if (sym)
6477 			strlcpy(sym, found_func->name, KSYM_NAME_LEN);
6478 
6479 		return found_func->name;
6480 	}
6481 
6482 	return NULL;
6483 }
6484 
6485 const char *
6486 ftrace_mod_address_lookup(unsigned long addr, unsigned long *size,
6487 		   unsigned long *off, char **modname, char *sym)
6488 {
6489 	struct ftrace_mod_map *mod_map;
6490 	const char *ret = NULL;
6491 
6492 	/* mod_map is freed via call_rcu() */
6493 	preempt_disable();
6494 	list_for_each_entry_rcu(mod_map, &ftrace_mod_maps, list) {
6495 		ret = ftrace_func_address_lookup(mod_map, addr, size, off, sym);
6496 		if (ret) {
6497 			if (modname)
6498 				*modname = mod_map->mod->name;
6499 			break;
6500 		}
6501 	}
6502 	preempt_enable();
6503 
6504 	return ret;
6505 }
6506 
6507 int ftrace_mod_get_kallsym(unsigned int symnum, unsigned long *value,
6508 			   char *type, char *name,
6509 			   char *module_name, int *exported)
6510 {
6511 	struct ftrace_mod_map *mod_map;
6512 	struct ftrace_mod_func *mod_func;
6513 
6514 	preempt_disable();
6515 	list_for_each_entry_rcu(mod_map, &ftrace_mod_maps, list) {
6516 
6517 		if (symnum >= mod_map->num_funcs) {
6518 			symnum -= mod_map->num_funcs;
6519 			continue;
6520 		}
6521 
6522 		list_for_each_entry_rcu(mod_func, &mod_map->funcs, list) {
6523 			if (symnum > 1) {
6524 				symnum--;
6525 				continue;
6526 			}
6527 
6528 			*value = mod_func->ip;
6529 			*type = 'T';
6530 			strlcpy(name, mod_func->name, KSYM_NAME_LEN);
6531 			strlcpy(module_name, mod_map->mod->name, MODULE_NAME_LEN);
6532 			*exported = 1;
6533 			preempt_enable();
6534 			return 0;
6535 		}
6536 		WARN_ON(1);
6537 		break;
6538 	}
6539 	preempt_enable();
6540 	return -ERANGE;
6541 }
6542 
6543 #else
6544 static void save_ftrace_mod_rec(struct ftrace_mod_map *mod_map,
6545 				struct dyn_ftrace *rec) { }
6546 static inline struct ftrace_mod_map *
6547 allocate_ftrace_mod_map(struct module *mod,
6548 			unsigned long start, unsigned long end)
6549 {
6550 	return NULL;
6551 }
6552 #endif /* CONFIG_MODULES */
6553 
6554 struct ftrace_init_func {
6555 	struct list_head list;
6556 	unsigned long ip;
6557 };
6558 
6559 /* Clear any init ips from hashes */
6560 static void
6561 clear_func_from_hash(struct ftrace_init_func *func, struct ftrace_hash *hash)
6562 {
6563 	struct ftrace_func_entry *entry;
6564 
6565 	entry = ftrace_lookup_ip(hash, func->ip);
6566 	/*
6567 	 * Do not allow this rec to match again.
6568 	 * Yeah, it may waste some memory, but will be removed
6569 	 * if/when the hash is modified again.
6570 	 */
6571 	if (entry)
6572 		entry->ip = 0;
6573 }
6574 
6575 static void
6576 clear_func_from_hashes(struct ftrace_init_func *func)
6577 {
6578 	struct trace_array *tr;
6579 
6580 	mutex_lock(&trace_types_lock);
6581 	list_for_each_entry(tr, &ftrace_trace_arrays, list) {
6582 		if (!tr->ops || !tr->ops->func_hash)
6583 			continue;
6584 		mutex_lock(&tr->ops->func_hash->regex_lock);
6585 		clear_func_from_hash(func, tr->ops->func_hash->filter_hash);
6586 		clear_func_from_hash(func, tr->ops->func_hash->notrace_hash);
6587 		mutex_unlock(&tr->ops->func_hash->regex_lock);
6588 	}
6589 	mutex_unlock(&trace_types_lock);
6590 }
6591 
6592 static void add_to_clear_hash_list(struct list_head *clear_list,
6593 				   struct dyn_ftrace *rec)
6594 {
6595 	struct ftrace_init_func *func;
6596 
6597 	func = kmalloc(sizeof(*func), GFP_KERNEL);
6598 	if (!func) {
6599 		WARN_ONCE(1, "alloc failure, ftrace filter could be stale\n");
6600 		return;
6601 	}
6602 
6603 	func->ip = rec->ip;
6604 	list_add(&func->list, clear_list);
6605 }
6606 
6607 void ftrace_free_mem(struct module *mod, void *start_ptr, void *end_ptr)
6608 {
6609 	unsigned long start = (unsigned long)(start_ptr);
6610 	unsigned long end = (unsigned long)(end_ptr);
6611 	struct ftrace_page **last_pg = &ftrace_pages_start;
6612 	struct ftrace_page *pg;
6613 	struct dyn_ftrace *rec;
6614 	struct dyn_ftrace key;
6615 	struct ftrace_mod_map *mod_map = NULL;
6616 	struct ftrace_init_func *func, *func_next;
6617 	struct list_head clear_hash;
6618 	int order;
6619 
6620 	INIT_LIST_HEAD(&clear_hash);
6621 
6622 	key.ip = start;
6623 	key.flags = end;	/* overload flags, as it is unsigned long */
6624 
6625 	mutex_lock(&ftrace_lock);
6626 
6627 	/*
6628 	 * If we are freeing module init memory, then check if
6629 	 * any tracer is active. If so, we need to save a mapping of
6630 	 * the module functions being freed with the address.
6631 	 */
6632 	if (mod && ftrace_ops_list != &ftrace_list_end)
6633 		mod_map = allocate_ftrace_mod_map(mod, start, end);
6634 
6635 	for (pg = ftrace_pages_start; pg; last_pg = &pg->next, pg = *last_pg) {
6636 		if (end < pg->records[0].ip ||
6637 		    start >= (pg->records[pg->index - 1].ip + MCOUNT_INSN_SIZE))
6638 			continue;
6639  again:
6640 		rec = bsearch(&key, pg->records, pg->index,
6641 			      sizeof(struct dyn_ftrace),
6642 			      ftrace_cmp_recs);
6643 		if (!rec)
6644 			continue;
6645 
6646 		/* rec will be cleared from hashes after ftrace_lock unlock */
6647 		add_to_clear_hash_list(&clear_hash, rec);
6648 
6649 		if (mod_map)
6650 			save_ftrace_mod_rec(mod_map, rec);
6651 
6652 		pg->index--;
6653 		ftrace_update_tot_cnt--;
6654 		if (!pg->index) {
6655 			*last_pg = pg->next;
6656 			order = get_count_order(pg->size / ENTRIES_PER_PAGE);
6657 			free_pages((unsigned long)pg->records, order);
6658 			ftrace_number_of_pages -= 1 << order;
6659 			ftrace_number_of_groups--;
6660 			kfree(pg);
6661 			pg = container_of(last_pg, struct ftrace_page, next);
6662 			if (!(*last_pg))
6663 				ftrace_pages = pg;
6664 			continue;
6665 		}
6666 		memmove(rec, rec + 1,
6667 			(pg->index - (rec - pg->records)) * sizeof(*rec));
6668 		/* More than one function may be in this block */
6669 		goto again;
6670 	}
6671 	mutex_unlock(&ftrace_lock);
6672 
6673 	list_for_each_entry_safe(func, func_next, &clear_hash, list) {
6674 		clear_func_from_hashes(func);
6675 		kfree(func);
6676 	}
6677 }
6678 
6679 void __init ftrace_free_init_mem(void)
6680 {
6681 	void *start = (void *)(&__init_begin);
6682 	void *end = (void *)(&__init_end);
6683 
6684 	ftrace_free_mem(NULL, start, end);
6685 }
6686 
6687 void __init ftrace_init(void)
6688 {
6689 	extern unsigned long __start_mcount_loc[];
6690 	extern unsigned long __stop_mcount_loc[];
6691 	unsigned long count, flags;
6692 	int ret;
6693 
6694 	local_irq_save(flags);
6695 	ret = ftrace_dyn_arch_init();
6696 	local_irq_restore(flags);
6697 	if (ret)
6698 		goto failed;
6699 
6700 	count = __stop_mcount_loc - __start_mcount_loc;
6701 	if (!count) {
6702 		pr_info("ftrace: No functions to be traced?\n");
6703 		goto failed;
6704 	}
6705 
6706 	pr_info("ftrace: allocating %ld entries in %ld pages\n",
6707 		count, count / ENTRIES_PER_PAGE + 1);
6708 
6709 	last_ftrace_enabled = ftrace_enabled = 1;
6710 
6711 	ret = ftrace_process_locs(NULL,
6712 				  __start_mcount_loc,
6713 				  __stop_mcount_loc);
6714 
6715 	pr_info("ftrace: allocated %ld pages with %ld groups\n",
6716 		ftrace_number_of_pages, ftrace_number_of_groups);
6717 
6718 	set_ftrace_early_filters();
6719 
6720 	return;
6721  failed:
6722 	ftrace_disabled = 1;
6723 }
6724 
6725 /* Do nothing if arch does not support this */
6726 void __weak arch_ftrace_update_trampoline(struct ftrace_ops *ops)
6727 {
6728 }
6729 
6730 static void ftrace_update_trampoline(struct ftrace_ops *ops)
6731 {
6732 	arch_ftrace_update_trampoline(ops);
6733 }
6734 
6735 void ftrace_init_trace_array(struct trace_array *tr)
6736 {
6737 	INIT_LIST_HEAD(&tr->func_probes);
6738 	INIT_LIST_HEAD(&tr->mod_trace);
6739 	INIT_LIST_HEAD(&tr->mod_notrace);
6740 }
6741 #else
6742 
6743 struct ftrace_ops global_ops = {
6744 	.func			= ftrace_stub,
6745 	.flags			= FTRACE_OPS_FL_RECURSION_SAFE |
6746 				  FTRACE_OPS_FL_INITIALIZED |
6747 				  FTRACE_OPS_FL_PID,
6748 };
6749 
6750 static int __init ftrace_nodyn_init(void)
6751 {
6752 	ftrace_enabled = 1;
6753 	return 0;
6754 }
6755 core_initcall(ftrace_nodyn_init);
6756 
6757 static inline int ftrace_init_dyn_tracefs(struct dentry *d_tracer) { return 0; }
6758 static inline void ftrace_startup_enable(int command) { }
6759 static inline void ftrace_startup_all(int command) { }
6760 
6761 # define ftrace_startup_sysctl()	do { } while (0)
6762 # define ftrace_shutdown_sysctl()	do { } while (0)
6763 
6764 static void ftrace_update_trampoline(struct ftrace_ops *ops)
6765 {
6766 }
6767 
6768 #endif /* CONFIG_DYNAMIC_FTRACE */
6769 
6770 __init void ftrace_init_global_array_ops(struct trace_array *tr)
6771 {
6772 	tr->ops = &global_ops;
6773 	tr->ops->private = tr;
6774 	ftrace_init_trace_array(tr);
6775 }
6776 
6777 void ftrace_init_array_ops(struct trace_array *tr, ftrace_func_t func)
6778 {
6779 	/* If we filter on pids, update to use the pid function */
6780 	if (tr->flags & TRACE_ARRAY_FL_GLOBAL) {
6781 		if (WARN_ON(tr->ops->func != ftrace_stub))
6782 			printk("ftrace ops had %pS for function\n",
6783 			       tr->ops->func);
6784 	}
6785 	tr->ops->func = func;
6786 	tr->ops->private = tr;
6787 }
6788 
6789 void ftrace_reset_array_ops(struct trace_array *tr)
6790 {
6791 	tr->ops->func = ftrace_stub;
6792 }
6793 
6794 static nokprobe_inline void
6795 __ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip,
6796 		       struct ftrace_ops *ignored, struct pt_regs *regs)
6797 {
6798 	struct ftrace_ops *op;
6799 	int bit;
6800 
6801 	bit = trace_test_and_set_recursion(TRACE_LIST_START, TRACE_LIST_MAX);
6802 	if (bit < 0)
6803 		return;
6804 
6805 	/*
6806 	 * Some of the ops may be dynamically allocated,
6807 	 * they must be freed after a synchronize_rcu().
6808 	 */
6809 	preempt_disable_notrace();
6810 
6811 	do_for_each_ftrace_op(op, ftrace_ops_list) {
6812 		/* Stub functions don't need to be called nor tested */
6813 		if (op->flags & FTRACE_OPS_FL_STUB)
6814 			continue;
6815 		/*
6816 		 * Check the following for each ops before calling their func:
6817 		 *  if RCU flag is set, then rcu_is_watching() must be true
6818 		 *  if PER_CPU is set, then ftrace_function_local_disable()
6819 		 *                          must be false
6820 		 *  Otherwise test if the ip matches the ops filter
6821 		 *
6822 		 * If any of the above fails then the op->func() is not executed.
6823 		 */
6824 		if ((!(op->flags & FTRACE_OPS_FL_RCU) || rcu_is_watching()) &&
6825 		    ftrace_ops_test(op, ip, regs)) {
6826 			if (FTRACE_WARN_ON(!op->func)) {
6827 				pr_warn("op=%p %pS\n", op, op);
6828 				goto out;
6829 			}
6830 			op->func(ip, parent_ip, op, regs);
6831 		}
6832 	} while_for_each_ftrace_op(op);
6833 out:
6834 	preempt_enable_notrace();
6835 	trace_clear_recursion(bit);
6836 }
6837 
6838 /*
6839  * Some archs only support passing ip and parent_ip. Even though
6840  * the list function ignores the op parameter, we do not want any
6841  * C side effects, where a function is called without the caller
6842  * sending a third parameter.
6843  * Archs are to support both the regs and ftrace_ops at the same time.
6844  * If they support ftrace_ops, it is assumed they support regs.
6845  * If call backs want to use regs, they must either check for regs
6846  * being NULL, or CONFIG_DYNAMIC_FTRACE_WITH_REGS.
6847  * Note, CONFIG_DYNAMIC_FTRACE_WITH_REGS expects a full regs to be saved.
6848  * An architecture can pass partial regs with ftrace_ops and still
6849  * set the ARCH_SUPPORTS_FTRACE_OPS.
6850  */
6851 #if ARCH_SUPPORTS_FTRACE_OPS
6852 static void ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip,
6853 				 struct ftrace_ops *op, struct pt_regs *regs)
6854 {
6855 	__ftrace_ops_list_func(ip, parent_ip, NULL, regs);
6856 }
6857 NOKPROBE_SYMBOL(ftrace_ops_list_func);
6858 #else
6859 static void ftrace_ops_no_ops(unsigned long ip, unsigned long parent_ip)
6860 {
6861 	__ftrace_ops_list_func(ip, parent_ip, NULL, NULL);
6862 }
6863 NOKPROBE_SYMBOL(ftrace_ops_no_ops);
6864 #endif
6865 
6866 /*
6867  * If there's only one function registered but it does not support
6868  * recursion, needs RCU protection and/or requires per cpu handling, then
6869  * this function will be called by the mcount trampoline.
6870  */
6871 static void ftrace_ops_assist_func(unsigned long ip, unsigned long parent_ip,
6872 				   struct ftrace_ops *op, struct pt_regs *regs)
6873 {
6874 	int bit;
6875 
6876 	if ((op->flags & FTRACE_OPS_FL_RCU) && !rcu_is_watching())
6877 		return;
6878 
6879 	bit = trace_test_and_set_recursion(TRACE_LIST_START, TRACE_LIST_MAX);
6880 	if (bit < 0)
6881 		return;
6882 
6883 	preempt_disable_notrace();
6884 
6885 	op->func(ip, parent_ip, op, regs);
6886 
6887 	preempt_enable_notrace();
6888 	trace_clear_recursion(bit);
6889 }
6890 NOKPROBE_SYMBOL(ftrace_ops_assist_func);
6891 
6892 /**
6893  * ftrace_ops_get_func - get the function a trampoline should call
6894  * @ops: the ops to get the function for
6895  *
6896  * Normally the mcount trampoline will call the ops->func, but there
6897  * are times that it should not. For example, if the ops does not
6898  * have its own recursion protection, then it should call the
6899  * ftrace_ops_assist_func() instead.
6900  *
6901  * Returns the function that the trampoline should call for @ops.
6902  */
6903 ftrace_func_t ftrace_ops_get_func(struct ftrace_ops *ops)
6904 {
6905 	/*
6906 	 * If the function does not handle recursion, needs to be RCU safe,
6907 	 * or does per cpu logic, then we need to call the assist handler.
6908 	 */
6909 	if (!(ops->flags & FTRACE_OPS_FL_RECURSION_SAFE) ||
6910 	    ops->flags & FTRACE_OPS_FL_RCU)
6911 		return ftrace_ops_assist_func;
6912 
6913 	return ops->func;
6914 }
6915 
6916 static void
6917 ftrace_filter_pid_sched_switch_probe(void *data, bool preempt,
6918 		    struct task_struct *prev, struct task_struct *next)
6919 {
6920 	struct trace_array *tr = data;
6921 	struct trace_pid_list *pid_list;
6922 
6923 	pid_list = rcu_dereference_sched(tr->function_pids);
6924 
6925 	this_cpu_write(tr->trace_buffer.data->ftrace_ignore_pid,
6926 		       trace_ignore_this_task(pid_list, next));
6927 }
6928 
6929 static void
6930 ftrace_pid_follow_sched_process_fork(void *data,
6931 				     struct task_struct *self,
6932 				     struct task_struct *task)
6933 {
6934 	struct trace_pid_list *pid_list;
6935 	struct trace_array *tr = data;
6936 
6937 	pid_list = rcu_dereference_sched(tr->function_pids);
6938 	trace_filter_add_remove_task(pid_list, self, task);
6939 }
6940 
6941 static void
6942 ftrace_pid_follow_sched_process_exit(void *data, struct task_struct *task)
6943 {
6944 	struct trace_pid_list *pid_list;
6945 	struct trace_array *tr = data;
6946 
6947 	pid_list = rcu_dereference_sched(tr->function_pids);
6948 	trace_filter_add_remove_task(pid_list, NULL, task);
6949 }
6950 
6951 void ftrace_pid_follow_fork(struct trace_array *tr, bool enable)
6952 {
6953 	if (enable) {
6954 		register_trace_sched_process_fork(ftrace_pid_follow_sched_process_fork,
6955 						  tr);
6956 		register_trace_sched_process_exit(ftrace_pid_follow_sched_process_exit,
6957 						  tr);
6958 	} else {
6959 		unregister_trace_sched_process_fork(ftrace_pid_follow_sched_process_fork,
6960 						    tr);
6961 		unregister_trace_sched_process_exit(ftrace_pid_follow_sched_process_exit,
6962 						    tr);
6963 	}
6964 }
6965 
6966 static void clear_ftrace_pids(struct trace_array *tr)
6967 {
6968 	struct trace_pid_list *pid_list;
6969 	int cpu;
6970 
6971 	pid_list = rcu_dereference_protected(tr->function_pids,
6972 					     lockdep_is_held(&ftrace_lock));
6973 	if (!pid_list)
6974 		return;
6975 
6976 	unregister_trace_sched_switch(ftrace_filter_pid_sched_switch_probe, tr);
6977 
6978 	for_each_possible_cpu(cpu)
6979 		per_cpu_ptr(tr->trace_buffer.data, cpu)->ftrace_ignore_pid = false;
6980 
6981 	rcu_assign_pointer(tr->function_pids, NULL);
6982 
6983 	/* Wait till all users are no longer using pid filtering */
6984 	synchronize_rcu();
6985 
6986 	trace_free_pid_list(pid_list);
6987 }
6988 
6989 void ftrace_clear_pids(struct trace_array *tr)
6990 {
6991 	mutex_lock(&ftrace_lock);
6992 
6993 	clear_ftrace_pids(tr);
6994 
6995 	mutex_unlock(&ftrace_lock);
6996 }
6997 
6998 static void ftrace_pid_reset(struct trace_array *tr)
6999 {
7000 	mutex_lock(&ftrace_lock);
7001 	clear_ftrace_pids(tr);
7002 
7003 	ftrace_update_pid_func();
7004 	ftrace_startup_all(0);
7005 
7006 	mutex_unlock(&ftrace_lock);
7007 }
7008 
7009 /* Greater than any max PID */
7010 #define FTRACE_NO_PIDS		(void *)(PID_MAX_LIMIT + 1)
7011 
7012 static void *fpid_start(struct seq_file *m, loff_t *pos)
7013 	__acquires(RCU)
7014 {
7015 	struct trace_pid_list *pid_list;
7016 	struct trace_array *tr = m->private;
7017 
7018 	mutex_lock(&ftrace_lock);
7019 	rcu_read_lock_sched();
7020 
7021 	pid_list = rcu_dereference_sched(tr->function_pids);
7022 
7023 	if (!pid_list)
7024 		return !(*pos) ? FTRACE_NO_PIDS : NULL;
7025 
7026 	return trace_pid_start(pid_list, pos);
7027 }
7028 
7029 static void *fpid_next(struct seq_file *m, void *v, loff_t *pos)
7030 {
7031 	struct trace_array *tr = m->private;
7032 	struct trace_pid_list *pid_list = rcu_dereference_sched(tr->function_pids);
7033 
7034 	if (v == FTRACE_NO_PIDS)
7035 		return NULL;
7036 
7037 	return trace_pid_next(pid_list, v, pos);
7038 }
7039 
7040 static void fpid_stop(struct seq_file *m, void *p)
7041 	__releases(RCU)
7042 {
7043 	rcu_read_unlock_sched();
7044 	mutex_unlock(&ftrace_lock);
7045 }
7046 
7047 static int fpid_show(struct seq_file *m, void *v)
7048 {
7049 	if (v == FTRACE_NO_PIDS) {
7050 		seq_puts(m, "no pid\n");
7051 		return 0;
7052 	}
7053 
7054 	return trace_pid_show(m, v);
7055 }
7056 
7057 static const struct seq_operations ftrace_pid_sops = {
7058 	.start = fpid_start,
7059 	.next = fpid_next,
7060 	.stop = fpid_stop,
7061 	.show = fpid_show,
7062 };
7063 
7064 static int
7065 ftrace_pid_open(struct inode *inode, struct file *file)
7066 {
7067 	struct trace_array *tr = inode->i_private;
7068 	struct seq_file *m;
7069 	int ret = 0;
7070 
7071 	ret = tracing_check_open_get_tr(tr);
7072 	if (ret)
7073 		return ret;
7074 
7075 	if ((file->f_mode & FMODE_WRITE) &&
7076 	    (file->f_flags & O_TRUNC))
7077 		ftrace_pid_reset(tr);
7078 
7079 	ret = seq_open(file, &ftrace_pid_sops);
7080 	if (ret < 0) {
7081 		trace_array_put(tr);
7082 	} else {
7083 		m = file->private_data;
7084 		/* copy tr over to seq ops */
7085 		m->private = tr;
7086 	}
7087 
7088 	return ret;
7089 }
7090 
7091 static void ignore_task_cpu(void *data)
7092 {
7093 	struct trace_array *tr = data;
7094 	struct trace_pid_list *pid_list;
7095 
7096 	/*
7097 	 * This function is called by on_each_cpu() while the
7098 	 * event_mutex is held.
7099 	 */
7100 	pid_list = rcu_dereference_protected(tr->function_pids,
7101 					     mutex_is_locked(&ftrace_lock));
7102 
7103 	this_cpu_write(tr->trace_buffer.data->ftrace_ignore_pid,
7104 		       trace_ignore_this_task(pid_list, current));
7105 }
7106 
7107 static ssize_t
7108 ftrace_pid_write(struct file *filp, const char __user *ubuf,
7109 		   size_t cnt, loff_t *ppos)
7110 {
7111 	struct seq_file *m = filp->private_data;
7112 	struct trace_array *tr = m->private;
7113 	struct trace_pid_list *filtered_pids = NULL;
7114 	struct trace_pid_list *pid_list;
7115 	ssize_t ret;
7116 
7117 	if (!cnt)
7118 		return 0;
7119 
7120 	mutex_lock(&ftrace_lock);
7121 
7122 	filtered_pids = rcu_dereference_protected(tr->function_pids,
7123 					     lockdep_is_held(&ftrace_lock));
7124 
7125 	ret = trace_pid_write(filtered_pids, &pid_list, ubuf, cnt);
7126 	if (ret < 0)
7127 		goto out;
7128 
7129 	rcu_assign_pointer(tr->function_pids, pid_list);
7130 
7131 	if (filtered_pids) {
7132 		synchronize_rcu();
7133 		trace_free_pid_list(filtered_pids);
7134 	} else if (pid_list) {
7135 		/* Register a probe to set whether to ignore the tracing of a task */
7136 		register_trace_sched_switch(ftrace_filter_pid_sched_switch_probe, tr);
7137 	}
7138 
7139 	/*
7140 	 * Ignoring of pids is done at task switch. But we have to
7141 	 * check for those tasks that are currently running.
7142 	 * Always do this in case a pid was appended or removed.
7143 	 */
7144 	on_each_cpu(ignore_task_cpu, tr, 1);
7145 
7146 	ftrace_update_pid_func();
7147 	ftrace_startup_all(0);
7148  out:
7149 	mutex_unlock(&ftrace_lock);
7150 
7151 	if (ret > 0)
7152 		*ppos += ret;
7153 
7154 	return ret;
7155 }
7156 
7157 static int
7158 ftrace_pid_release(struct inode *inode, struct file *file)
7159 {
7160 	struct trace_array *tr = inode->i_private;
7161 
7162 	trace_array_put(tr);
7163 
7164 	return seq_release(inode, file);
7165 }
7166 
7167 static const struct file_operations ftrace_pid_fops = {
7168 	.open		= ftrace_pid_open,
7169 	.write		= ftrace_pid_write,
7170 	.read		= seq_read,
7171 	.llseek		= tracing_lseek,
7172 	.release	= ftrace_pid_release,
7173 };
7174 
7175 void ftrace_init_tracefs(struct trace_array *tr, struct dentry *d_tracer)
7176 {
7177 	trace_create_file("set_ftrace_pid", 0644, d_tracer,
7178 			    tr, &ftrace_pid_fops);
7179 }
7180 
7181 void __init ftrace_init_tracefs_toplevel(struct trace_array *tr,
7182 					 struct dentry *d_tracer)
7183 {
7184 	/* Only the top level directory has the dyn_tracefs and profile */
7185 	WARN_ON(!(tr->flags & TRACE_ARRAY_FL_GLOBAL));
7186 
7187 	ftrace_init_dyn_tracefs(d_tracer);
7188 	ftrace_profile_tracefs(d_tracer);
7189 }
7190 
7191 /**
7192  * ftrace_kill - kill ftrace
7193  *
7194  * This function should be used by panic code. It stops ftrace
7195  * but in a not so nice way. If you need to simply kill ftrace
7196  * from a non-atomic section, use ftrace_kill.
7197  */
7198 void ftrace_kill(void)
7199 {
7200 	ftrace_disabled = 1;
7201 	ftrace_enabled = 0;
7202 	ftrace_trace_function = ftrace_stub;
7203 }
7204 
7205 /**
7206  * Test if ftrace is dead or not.
7207  */
7208 int ftrace_is_dead(void)
7209 {
7210 	return ftrace_disabled;
7211 }
7212 
7213 /**
7214  * register_ftrace_function - register a function for profiling
7215  * @ops - ops structure that holds the function for profiling.
7216  *
7217  * Register a function to be called by all functions in the
7218  * kernel.
7219  *
7220  * Note: @ops->func and all the functions it calls must be labeled
7221  *       with "notrace", otherwise it will go into a
7222  *       recursive loop.
7223  */
7224 int register_ftrace_function(struct ftrace_ops *ops)
7225 {
7226 	int ret = -1;
7227 
7228 	ftrace_ops_init(ops);
7229 
7230 	mutex_lock(&ftrace_lock);
7231 
7232 	ret = ftrace_startup(ops, 0);
7233 
7234 	mutex_unlock(&ftrace_lock);
7235 
7236 	return ret;
7237 }
7238 EXPORT_SYMBOL_GPL(register_ftrace_function);
7239 
7240 /**
7241  * unregister_ftrace_function - unregister a function for profiling.
7242  * @ops - ops structure that holds the function to unregister
7243  *
7244  * Unregister a function that was added to be called by ftrace profiling.
7245  */
7246 int unregister_ftrace_function(struct ftrace_ops *ops)
7247 {
7248 	int ret;
7249 
7250 	mutex_lock(&ftrace_lock);
7251 	ret = ftrace_shutdown(ops, 0);
7252 	mutex_unlock(&ftrace_lock);
7253 
7254 	return ret;
7255 }
7256 EXPORT_SYMBOL_GPL(unregister_ftrace_function);
7257 
7258 static bool is_permanent_ops_registered(void)
7259 {
7260 	struct ftrace_ops *op;
7261 
7262 	do_for_each_ftrace_op(op, ftrace_ops_list) {
7263 		if (op->flags & FTRACE_OPS_FL_PERMANENT)
7264 			return true;
7265 	} while_for_each_ftrace_op(op);
7266 
7267 	return false;
7268 }
7269 
7270 int
7271 ftrace_enable_sysctl(struct ctl_table *table, int write,
7272 		     void __user *buffer, size_t *lenp,
7273 		     loff_t *ppos)
7274 {
7275 	int ret = -ENODEV;
7276 
7277 	mutex_lock(&ftrace_lock);
7278 
7279 	if (unlikely(ftrace_disabled))
7280 		goto out;
7281 
7282 	ret = proc_dointvec(table, write, buffer, lenp, ppos);
7283 
7284 	if (ret || !write || (last_ftrace_enabled == !!ftrace_enabled))
7285 		goto out;
7286 
7287 	if (ftrace_enabled) {
7288 
7289 		/* we are starting ftrace again */
7290 		if (rcu_dereference_protected(ftrace_ops_list,
7291 			lockdep_is_held(&ftrace_lock)) != &ftrace_list_end)
7292 			update_ftrace_function();
7293 
7294 		ftrace_startup_sysctl();
7295 
7296 	} else {
7297 		if (is_permanent_ops_registered()) {
7298 			ftrace_enabled = true;
7299 			ret = -EBUSY;
7300 			goto out;
7301 		}
7302 
7303 		/* stopping ftrace calls (just send to ftrace_stub) */
7304 		ftrace_trace_function = ftrace_stub;
7305 
7306 		ftrace_shutdown_sysctl();
7307 	}
7308 
7309 	last_ftrace_enabled = !!ftrace_enabled;
7310  out:
7311 	mutex_unlock(&ftrace_lock);
7312 	return ret;
7313 }
7314