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