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