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