xref: /linux/kernel/trace/ftrace.c (revision 41dc27e3b9bd41b900f5aea06f86669e54a2cdd6)
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/kallsyms.h>
19 #include <linux/seq_file.h>
20 #include <linux/suspend.h>
21 #include <linux/debugfs.h>
22 #include <linux/hardirq.h>
23 #include <linux/kthread.h>
24 #include <linux/uaccess.h>
25 #include <linux/bsearch.h>
26 #include <linux/module.h>
27 #include <linux/ftrace.h>
28 #include <linux/sysctl.h>
29 #include <linux/slab.h>
30 #include <linux/ctype.h>
31 #include <linux/sort.h>
32 #include <linux/list.h>
33 #include <linux/hash.h>
34 #include <linux/rcupdate.h>
35 
36 #include <trace/events/sched.h>
37 
38 #include <asm/setup.h>
39 
40 #include "trace_output.h"
41 #include "trace_stat.h"
42 
43 #define FTRACE_WARN_ON(cond)			\
44 	({					\
45 		int ___r = cond;		\
46 		if (WARN_ON(___r))		\
47 			ftrace_kill();		\
48 		___r;				\
49 	})
50 
51 #define FTRACE_WARN_ON_ONCE(cond)		\
52 	({					\
53 		int ___r = cond;		\
54 		if (WARN_ON_ONCE(___r))		\
55 			ftrace_kill();		\
56 		___r;				\
57 	})
58 
59 /* hash bits for specific function selection */
60 #define FTRACE_HASH_BITS 7
61 #define FTRACE_FUNC_HASHSIZE (1 << FTRACE_HASH_BITS)
62 #define FTRACE_HASH_DEFAULT_BITS 10
63 #define FTRACE_HASH_MAX_BITS 12
64 
65 #define FL_GLOBAL_CONTROL_MASK (FTRACE_OPS_FL_CONTROL)
66 
67 #ifdef CONFIG_DYNAMIC_FTRACE
68 #define INIT_REGEX_LOCK(opsname)	\
69 	.regex_lock	= __MUTEX_INITIALIZER(opsname.regex_lock),
70 #else
71 #define INIT_REGEX_LOCK(opsname)
72 #endif
73 
74 static struct ftrace_ops ftrace_list_end __read_mostly = {
75 	.func		= ftrace_stub,
76 	.flags		= FTRACE_OPS_FL_RECURSION_SAFE | FTRACE_OPS_FL_STUB,
77 };
78 
79 /* ftrace_enabled is a method to turn ftrace on or off */
80 int ftrace_enabled __read_mostly;
81 static int last_ftrace_enabled;
82 
83 /* Quick disabling of function tracer. */
84 int function_trace_stop __read_mostly;
85 
86 /* Current function tracing op */
87 struct ftrace_ops *function_trace_op __read_mostly = &ftrace_list_end;
88 /* What to set function_trace_op to */
89 static struct ftrace_ops *set_function_trace_op;
90 
91 /* List for set_ftrace_pid's pids. */
92 LIST_HEAD(ftrace_pids);
93 struct ftrace_pid {
94 	struct list_head list;
95 	struct pid *pid;
96 };
97 
98 /*
99  * ftrace_disabled is set when an anomaly is discovered.
100  * ftrace_disabled is much stronger than ftrace_enabled.
101  */
102 static int ftrace_disabled __read_mostly;
103 
104 static DEFINE_MUTEX(ftrace_lock);
105 
106 static struct ftrace_ops *ftrace_control_list __read_mostly = &ftrace_list_end;
107 static struct ftrace_ops *ftrace_ops_list __read_mostly = &ftrace_list_end;
108 ftrace_func_t ftrace_trace_function __read_mostly = ftrace_stub;
109 ftrace_func_t ftrace_pid_function __read_mostly = ftrace_stub;
110 static struct ftrace_ops global_ops;
111 static struct ftrace_ops control_ops;
112 
113 #if ARCH_SUPPORTS_FTRACE_OPS
114 static void ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip,
115 				 struct ftrace_ops *op, struct pt_regs *regs);
116 #else
117 /* See comment below, where ftrace_ops_list_func is defined */
118 static void ftrace_ops_no_ops(unsigned long ip, unsigned long parent_ip);
119 #define ftrace_ops_list_func ((ftrace_func_t)ftrace_ops_no_ops)
120 #endif
121 
122 /*
123  * Traverse the ftrace_global_list, invoking all entries.  The reason that we
124  * can use rcu_dereference_raw_notrace() is that elements removed from this list
125  * are simply leaked, so there is no need to interact with a grace-period
126  * mechanism.  The rcu_dereference_raw_notrace() calls are needed to handle
127  * concurrent insertions into the ftrace_global_list.
128  *
129  * Silly Alpha and silly pointer-speculation compiler optimizations!
130  */
131 #define do_for_each_ftrace_op(op, list)			\
132 	op = rcu_dereference_raw_notrace(list);			\
133 	do
134 
135 /*
136  * Optimized for just a single item in the list (as that is the normal case).
137  */
138 #define while_for_each_ftrace_op(op)				\
139 	while (likely(op = rcu_dereference_raw_notrace((op)->next)) &&	\
140 	       unlikely((op) != &ftrace_list_end))
141 
142 static inline void ftrace_ops_init(struct ftrace_ops *ops)
143 {
144 #ifdef CONFIG_DYNAMIC_FTRACE
145 	if (!(ops->flags & FTRACE_OPS_FL_INITIALIZED)) {
146 		mutex_init(&ops->regex_lock);
147 		ops->flags |= FTRACE_OPS_FL_INITIALIZED;
148 	}
149 #endif
150 }
151 
152 /**
153  * ftrace_nr_registered_ops - return number of ops registered
154  *
155  * Returns the number of ftrace_ops registered and tracing functions
156  */
157 int ftrace_nr_registered_ops(void)
158 {
159 	struct ftrace_ops *ops;
160 	int cnt = 0;
161 
162 	mutex_lock(&ftrace_lock);
163 
164 	for (ops = ftrace_ops_list;
165 	     ops != &ftrace_list_end; ops = ops->next)
166 		cnt++;
167 
168 	mutex_unlock(&ftrace_lock);
169 
170 	return cnt;
171 }
172 
173 static void ftrace_pid_func(unsigned long ip, unsigned long parent_ip,
174 			    struct ftrace_ops *op, struct pt_regs *regs)
175 {
176 	if (!test_tsk_trace_trace(current))
177 		return;
178 
179 	ftrace_pid_function(ip, parent_ip, op, regs);
180 }
181 
182 static void set_ftrace_pid_function(ftrace_func_t func)
183 {
184 	/* do not set ftrace_pid_function to itself! */
185 	if (func != ftrace_pid_func)
186 		ftrace_pid_function = func;
187 }
188 
189 /**
190  * clear_ftrace_function - reset the ftrace function
191  *
192  * This NULLs the ftrace function and in essence stops
193  * tracing.  There may be lag
194  */
195 void clear_ftrace_function(void)
196 {
197 	ftrace_trace_function = ftrace_stub;
198 	ftrace_pid_function = ftrace_stub;
199 }
200 
201 static void control_ops_disable_all(struct ftrace_ops *ops)
202 {
203 	int cpu;
204 
205 	for_each_possible_cpu(cpu)
206 		*per_cpu_ptr(ops->disabled, cpu) = 1;
207 }
208 
209 static int control_ops_alloc(struct ftrace_ops *ops)
210 {
211 	int __percpu *disabled;
212 
213 	disabled = alloc_percpu(int);
214 	if (!disabled)
215 		return -ENOMEM;
216 
217 	ops->disabled = disabled;
218 	control_ops_disable_all(ops);
219 	return 0;
220 }
221 
222 static void ftrace_sync(struct work_struct *work)
223 {
224 	/*
225 	 * This function is just a stub to implement a hard force
226 	 * of synchronize_sched(). This requires synchronizing
227 	 * tasks even in userspace and idle.
228 	 *
229 	 * Yes, function tracing is rude.
230 	 */
231 }
232 
233 static void ftrace_sync_ipi(void *data)
234 {
235 	/* Probably not needed, but do it anyway */
236 	smp_rmb();
237 }
238 
239 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
240 static void update_function_graph_func(void);
241 #else
242 static inline void update_function_graph_func(void) { }
243 #endif
244 
245 static void update_ftrace_function(void)
246 {
247 	ftrace_func_t func;
248 
249 	/*
250 	 * If we are at the end of the list and this ops is
251 	 * recursion safe and not dynamic and the arch supports passing ops,
252 	 * then have the mcount trampoline call the function directly.
253 	 */
254 	if (ftrace_ops_list == &ftrace_list_end ||
255 	    (ftrace_ops_list->next == &ftrace_list_end &&
256 	     !(ftrace_ops_list->flags & FTRACE_OPS_FL_DYNAMIC) &&
257 	     (ftrace_ops_list->flags & FTRACE_OPS_FL_RECURSION_SAFE) &&
258 	     !FTRACE_FORCE_LIST_FUNC)) {
259 		/* Set the ftrace_ops that the arch callback uses */
260 		set_function_trace_op = ftrace_ops_list;
261 		func = ftrace_ops_list->func;
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 	/* If there's no change, then do nothing more here */
269 	if (ftrace_trace_function == func)
270 		return;
271 
272 	update_function_graph_func();
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 **list, struct ftrace_ops *ops)
322 {
323 	ops->next = *list;
324 	/*
325 	 * We are entering ops into the list but another
326 	 * CPU might be walking that list. We need to make sure
327 	 * the ops->next pointer is valid before another CPU sees
328 	 * the ops pointer included into the list.
329 	 */
330 	rcu_assign_pointer(*list, ops);
331 }
332 
333 static int remove_ftrace_ops(struct ftrace_ops **list, struct ftrace_ops *ops)
334 {
335 	struct ftrace_ops **p;
336 
337 	/*
338 	 * If we are removing the last function, then simply point
339 	 * to the ftrace_stub.
340 	 */
341 	if (*list == ops && ops->next == &ftrace_list_end) {
342 		*list = &ftrace_list_end;
343 		return 0;
344 	}
345 
346 	for (p = list; *p != &ftrace_list_end; p = &(*p)->next)
347 		if (*p == ops)
348 			break;
349 
350 	if (*p != ops)
351 		return -1;
352 
353 	*p = (*p)->next;
354 	return 0;
355 }
356 
357 static void add_ftrace_list_ops(struct ftrace_ops **list,
358 				struct ftrace_ops *main_ops,
359 				struct ftrace_ops *ops)
360 {
361 	int first = *list == &ftrace_list_end;
362 	add_ftrace_ops(list, ops);
363 	if (first)
364 		add_ftrace_ops(&ftrace_ops_list, main_ops);
365 }
366 
367 static int remove_ftrace_list_ops(struct ftrace_ops **list,
368 				  struct ftrace_ops *main_ops,
369 				  struct ftrace_ops *ops)
370 {
371 	int ret = remove_ftrace_ops(list, ops);
372 	if (!ret && *list == &ftrace_list_end)
373 		ret = remove_ftrace_ops(&ftrace_ops_list, main_ops);
374 	return ret;
375 }
376 
377 static int __register_ftrace_function(struct ftrace_ops *ops)
378 {
379 	if (ops->flags & FTRACE_OPS_FL_DELETED)
380 		return -EINVAL;
381 
382 	if (WARN_ON(ops->flags & FTRACE_OPS_FL_ENABLED))
383 		return -EBUSY;
384 
385 #ifndef CONFIG_DYNAMIC_FTRACE_WITH_REGS
386 	/*
387 	 * If the ftrace_ops specifies SAVE_REGS, then it only can be used
388 	 * if the arch supports it, or SAVE_REGS_IF_SUPPORTED is also set.
389 	 * Setting SAVE_REGS_IF_SUPPORTED makes SAVE_REGS irrelevant.
390 	 */
391 	if (ops->flags & FTRACE_OPS_FL_SAVE_REGS &&
392 	    !(ops->flags & FTRACE_OPS_FL_SAVE_REGS_IF_SUPPORTED))
393 		return -EINVAL;
394 
395 	if (ops->flags & FTRACE_OPS_FL_SAVE_REGS_IF_SUPPORTED)
396 		ops->flags |= FTRACE_OPS_FL_SAVE_REGS;
397 #endif
398 
399 	if (!core_kernel_data((unsigned long)ops))
400 		ops->flags |= FTRACE_OPS_FL_DYNAMIC;
401 
402 	if (ops->flags & FTRACE_OPS_FL_CONTROL) {
403 		if (control_ops_alloc(ops))
404 			return -ENOMEM;
405 		add_ftrace_list_ops(&ftrace_control_list, &control_ops, ops);
406 	} else
407 		add_ftrace_ops(&ftrace_ops_list, ops);
408 
409 	if (ftrace_enabled)
410 		update_ftrace_function();
411 
412 	return 0;
413 }
414 
415 static int __unregister_ftrace_function(struct ftrace_ops *ops)
416 {
417 	int ret;
418 
419 	if (WARN_ON(!(ops->flags & FTRACE_OPS_FL_ENABLED)))
420 		return -EBUSY;
421 
422 	if (ops->flags & FTRACE_OPS_FL_CONTROL) {
423 		ret = remove_ftrace_list_ops(&ftrace_control_list,
424 					     &control_ops, ops);
425 	} else
426 		ret = remove_ftrace_ops(&ftrace_ops_list, ops);
427 
428 	if (ret < 0)
429 		return ret;
430 
431 	if (ftrace_enabled)
432 		update_ftrace_function();
433 
434 	return 0;
435 }
436 
437 static void ftrace_update_pid_func(void)
438 {
439 	/* Only do something if we are tracing something */
440 	if (ftrace_trace_function == ftrace_stub)
441 		return;
442 
443 	update_ftrace_function();
444 }
445 
446 #ifdef CONFIG_FUNCTION_PROFILER
447 struct ftrace_profile {
448 	struct hlist_node		node;
449 	unsigned long			ip;
450 	unsigned long			counter;
451 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
452 	unsigned long long		time;
453 	unsigned long long		time_squared;
454 #endif
455 };
456 
457 struct ftrace_profile_page {
458 	struct ftrace_profile_page	*next;
459 	unsigned long			index;
460 	struct ftrace_profile		records[];
461 };
462 
463 struct ftrace_profile_stat {
464 	atomic_t			disabled;
465 	struct hlist_head		*hash;
466 	struct ftrace_profile_page	*pages;
467 	struct ftrace_profile_page	*start;
468 	struct tracer_stat		stat;
469 };
470 
471 #define PROFILE_RECORDS_SIZE						\
472 	(PAGE_SIZE - offsetof(struct ftrace_profile_page, records))
473 
474 #define PROFILES_PER_PAGE					\
475 	(PROFILE_RECORDS_SIZE / sizeof(struct ftrace_profile))
476 
477 static int ftrace_profile_enabled __read_mostly;
478 
479 /* ftrace_profile_lock - synchronize the enable and disable of the profiler */
480 static DEFINE_MUTEX(ftrace_profile_lock);
481 
482 static DEFINE_PER_CPU(struct ftrace_profile_stat, ftrace_profile_stats);
483 
484 #define FTRACE_PROFILE_HASH_BITS 10
485 #define FTRACE_PROFILE_HASH_SIZE (1 << FTRACE_PROFILE_HASH_BITS)
486 
487 static void *
488 function_stat_next(void *v, int idx)
489 {
490 	struct ftrace_profile *rec = v;
491 	struct ftrace_profile_page *pg;
492 
493 	pg = (struct ftrace_profile_page *)((unsigned long)rec & PAGE_MASK);
494 
495  again:
496 	if (idx != 0)
497 		rec++;
498 
499 	if ((void *)rec >= (void *)&pg->records[pg->index]) {
500 		pg = pg->next;
501 		if (!pg)
502 			return NULL;
503 		rec = &pg->records[0];
504 		if (!rec->counter)
505 			goto again;
506 	}
507 
508 	return rec;
509 }
510 
511 static void *function_stat_start(struct tracer_stat *trace)
512 {
513 	struct ftrace_profile_stat *stat =
514 		container_of(trace, struct ftrace_profile_stat, stat);
515 
516 	if (!stat || !stat->start)
517 		return NULL;
518 
519 	return function_stat_next(&stat->start->records[0], 0);
520 }
521 
522 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
523 /* function graph compares on total time */
524 static int function_stat_cmp(void *p1, void *p2)
525 {
526 	struct ftrace_profile *a = p1;
527 	struct ftrace_profile *b = p2;
528 
529 	if (a->time < b->time)
530 		return -1;
531 	if (a->time > b->time)
532 		return 1;
533 	else
534 		return 0;
535 }
536 #else
537 /* not function graph compares against hits */
538 static int function_stat_cmp(void *p1, void *p2)
539 {
540 	struct ftrace_profile *a = p1;
541 	struct ftrace_profile *b = p2;
542 
543 	if (a->counter < b->counter)
544 		return -1;
545 	if (a->counter > b->counter)
546 		return 1;
547 	else
548 		return 0;
549 }
550 #endif
551 
552 static int function_stat_headers(struct seq_file *m)
553 {
554 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
555 	seq_printf(m, "  Function                               "
556 		   "Hit    Time            Avg             s^2\n"
557 		      "  --------                               "
558 		   "---    ----            ---             ---\n");
559 #else
560 	seq_printf(m, "  Function                               Hit\n"
561 		      "  --------                               ---\n");
562 #endif
563 	return 0;
564 }
565 
566 static int function_stat_show(struct seq_file *m, void *v)
567 {
568 	struct ftrace_profile *rec = v;
569 	char str[KSYM_SYMBOL_LEN];
570 	int ret = 0;
571 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
572 	static struct trace_seq s;
573 	unsigned long long avg;
574 	unsigned long long stddev;
575 #endif
576 	mutex_lock(&ftrace_profile_lock);
577 
578 	/* we raced with function_profile_reset() */
579 	if (unlikely(rec->counter == 0)) {
580 		ret = -EBUSY;
581 		goto out;
582 	}
583 
584 	kallsyms_lookup(rec->ip, NULL, NULL, NULL, str);
585 	seq_printf(m, "  %-30.30s  %10lu", str, rec->counter);
586 
587 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
588 	seq_printf(m, "    ");
589 	avg = rec->time;
590 	do_div(avg, rec->counter);
591 
592 	/* Sample standard deviation (s^2) */
593 	if (rec->counter <= 1)
594 		stddev = 0;
595 	else {
596 		/*
597 		 * Apply Welford's method:
598 		 * s^2 = 1 / (n * (n-1)) * (n * \Sum (x_i)^2 - (\Sum x_i)^2)
599 		 */
600 		stddev = rec->counter * rec->time_squared -
601 			 rec->time * rec->time;
602 
603 		/*
604 		 * Divide only 1000 for ns^2 -> us^2 conversion.
605 		 * trace_print_graph_duration will divide 1000 again.
606 		 */
607 		do_div(stddev, rec->counter * (rec->counter - 1) * 1000);
608 	}
609 
610 	trace_seq_init(&s);
611 	trace_print_graph_duration(rec->time, &s);
612 	trace_seq_puts(&s, "    ");
613 	trace_print_graph_duration(avg, &s);
614 	trace_seq_puts(&s, "    ");
615 	trace_print_graph_duration(stddev, &s);
616 	trace_print_seq(m, &s);
617 #endif
618 	seq_putc(m, '\n');
619 out:
620 	mutex_unlock(&ftrace_profile_lock);
621 
622 	return ret;
623 }
624 
625 static void ftrace_profile_reset(struct ftrace_profile_stat *stat)
626 {
627 	struct ftrace_profile_page *pg;
628 
629 	pg = stat->pages = stat->start;
630 
631 	while (pg) {
632 		memset(pg->records, 0, PROFILE_RECORDS_SIZE);
633 		pg->index = 0;
634 		pg = pg->next;
635 	}
636 
637 	memset(stat->hash, 0,
638 	       FTRACE_PROFILE_HASH_SIZE * sizeof(struct hlist_head));
639 }
640 
641 int ftrace_profile_pages_init(struct ftrace_profile_stat *stat)
642 {
643 	struct ftrace_profile_page *pg;
644 	int functions;
645 	int pages;
646 	int i;
647 
648 	/* If we already allocated, do nothing */
649 	if (stat->pages)
650 		return 0;
651 
652 	stat->pages = (void *)get_zeroed_page(GFP_KERNEL);
653 	if (!stat->pages)
654 		return -ENOMEM;
655 
656 #ifdef CONFIG_DYNAMIC_FTRACE
657 	functions = ftrace_update_tot_cnt;
658 #else
659 	/*
660 	 * We do not know the number of functions that exist because
661 	 * dynamic tracing is what counts them. With past experience
662 	 * we have around 20K functions. That should be more than enough.
663 	 * It is highly unlikely we will execute every function in
664 	 * the kernel.
665 	 */
666 	functions = 20000;
667 #endif
668 
669 	pg = stat->start = stat->pages;
670 
671 	pages = DIV_ROUND_UP(functions, PROFILES_PER_PAGE);
672 
673 	for (i = 1; i < pages; i++) {
674 		pg->next = (void *)get_zeroed_page(GFP_KERNEL);
675 		if (!pg->next)
676 			goto out_free;
677 		pg = pg->next;
678 	}
679 
680 	return 0;
681 
682  out_free:
683 	pg = stat->start;
684 	while (pg) {
685 		unsigned long tmp = (unsigned long)pg;
686 
687 		pg = pg->next;
688 		free_page(tmp);
689 	}
690 
691 	stat->pages = NULL;
692 	stat->start = NULL;
693 
694 	return -ENOMEM;
695 }
696 
697 static int ftrace_profile_init_cpu(int cpu)
698 {
699 	struct ftrace_profile_stat *stat;
700 	int size;
701 
702 	stat = &per_cpu(ftrace_profile_stats, cpu);
703 
704 	if (stat->hash) {
705 		/* If the profile is already created, simply reset it */
706 		ftrace_profile_reset(stat);
707 		return 0;
708 	}
709 
710 	/*
711 	 * We are profiling all functions, but usually only a few thousand
712 	 * functions are hit. We'll make a hash of 1024 items.
713 	 */
714 	size = FTRACE_PROFILE_HASH_SIZE;
715 
716 	stat->hash = kzalloc(sizeof(struct hlist_head) * size, GFP_KERNEL);
717 
718 	if (!stat->hash)
719 		return -ENOMEM;
720 
721 	/* Preallocate the function profiling pages */
722 	if (ftrace_profile_pages_init(stat) < 0) {
723 		kfree(stat->hash);
724 		stat->hash = NULL;
725 		return -ENOMEM;
726 	}
727 
728 	return 0;
729 }
730 
731 static int ftrace_profile_init(void)
732 {
733 	int cpu;
734 	int ret = 0;
735 
736 	for_each_possible_cpu(cpu) {
737 		ret = ftrace_profile_init_cpu(cpu);
738 		if (ret)
739 			break;
740 	}
741 
742 	return ret;
743 }
744 
745 /* interrupts must be disabled */
746 static struct ftrace_profile *
747 ftrace_find_profiled_func(struct ftrace_profile_stat *stat, unsigned long ip)
748 {
749 	struct ftrace_profile *rec;
750 	struct hlist_head *hhd;
751 	unsigned long key;
752 
753 	key = hash_long(ip, FTRACE_PROFILE_HASH_BITS);
754 	hhd = &stat->hash[key];
755 
756 	if (hlist_empty(hhd))
757 		return NULL;
758 
759 	hlist_for_each_entry_rcu_notrace(rec, hhd, node) {
760 		if (rec->ip == ip)
761 			return rec;
762 	}
763 
764 	return NULL;
765 }
766 
767 static void ftrace_add_profile(struct ftrace_profile_stat *stat,
768 			       struct ftrace_profile *rec)
769 {
770 	unsigned long key;
771 
772 	key = hash_long(rec->ip, FTRACE_PROFILE_HASH_BITS);
773 	hlist_add_head_rcu(&rec->node, &stat->hash[key]);
774 }
775 
776 /*
777  * The memory is already allocated, this simply finds a new record to use.
778  */
779 static struct ftrace_profile *
780 ftrace_profile_alloc(struct ftrace_profile_stat *stat, unsigned long ip)
781 {
782 	struct ftrace_profile *rec = NULL;
783 
784 	/* prevent recursion (from NMIs) */
785 	if (atomic_inc_return(&stat->disabled) != 1)
786 		goto out;
787 
788 	/*
789 	 * Try to find the function again since an NMI
790 	 * could have added it
791 	 */
792 	rec = ftrace_find_profiled_func(stat, ip);
793 	if (rec)
794 		goto out;
795 
796 	if (stat->pages->index == PROFILES_PER_PAGE) {
797 		if (!stat->pages->next)
798 			goto out;
799 		stat->pages = stat->pages->next;
800 	}
801 
802 	rec = &stat->pages->records[stat->pages->index++];
803 	rec->ip = ip;
804 	ftrace_add_profile(stat, rec);
805 
806  out:
807 	atomic_dec(&stat->disabled);
808 
809 	return rec;
810 }
811 
812 static void
813 function_profile_call(unsigned long ip, unsigned long parent_ip,
814 		      struct ftrace_ops *ops, struct pt_regs *regs)
815 {
816 	struct ftrace_profile_stat *stat;
817 	struct ftrace_profile *rec;
818 	unsigned long flags;
819 
820 	if (!ftrace_profile_enabled)
821 		return;
822 
823 	local_irq_save(flags);
824 
825 	stat = this_cpu_ptr(&ftrace_profile_stats);
826 	if (!stat->hash || !ftrace_profile_enabled)
827 		goto out;
828 
829 	rec = ftrace_find_profiled_func(stat, ip);
830 	if (!rec) {
831 		rec = ftrace_profile_alloc(stat, ip);
832 		if (!rec)
833 			goto out;
834 	}
835 
836 	rec->counter++;
837  out:
838 	local_irq_restore(flags);
839 }
840 
841 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
842 static int profile_graph_entry(struct ftrace_graph_ent *trace)
843 {
844 	function_profile_call(trace->func, 0, NULL, NULL);
845 	return 1;
846 }
847 
848 static void profile_graph_return(struct ftrace_graph_ret *trace)
849 {
850 	struct ftrace_profile_stat *stat;
851 	unsigned long long calltime;
852 	struct ftrace_profile *rec;
853 	unsigned long flags;
854 
855 	local_irq_save(flags);
856 	stat = this_cpu_ptr(&ftrace_profile_stats);
857 	if (!stat->hash || !ftrace_profile_enabled)
858 		goto out;
859 
860 	/* If the calltime was zero'd ignore it */
861 	if (!trace->calltime)
862 		goto out;
863 
864 	calltime = trace->rettime - trace->calltime;
865 
866 	if (!(trace_flags & TRACE_ITER_GRAPH_TIME)) {
867 		int index;
868 
869 		index = trace->depth;
870 
871 		/* Append this call time to the parent time to subtract */
872 		if (index)
873 			current->ret_stack[index - 1].subtime += calltime;
874 
875 		if (current->ret_stack[index].subtime < calltime)
876 			calltime -= current->ret_stack[index].subtime;
877 		else
878 			calltime = 0;
879 	}
880 
881 	rec = ftrace_find_profiled_func(stat, trace->func);
882 	if (rec) {
883 		rec->time += calltime;
884 		rec->time_squared += calltime * calltime;
885 	}
886 
887  out:
888 	local_irq_restore(flags);
889 }
890 
891 static int register_ftrace_profiler(void)
892 {
893 	return register_ftrace_graph(&profile_graph_return,
894 				     &profile_graph_entry);
895 }
896 
897 static void unregister_ftrace_profiler(void)
898 {
899 	unregister_ftrace_graph();
900 }
901 #else
902 static struct ftrace_ops ftrace_profile_ops __read_mostly = {
903 	.func		= function_profile_call,
904 	.flags		= FTRACE_OPS_FL_RECURSION_SAFE | FTRACE_OPS_FL_INITIALIZED,
905 	INIT_REGEX_LOCK(ftrace_profile_ops)
906 };
907 
908 static int register_ftrace_profiler(void)
909 {
910 	return register_ftrace_function(&ftrace_profile_ops);
911 }
912 
913 static void unregister_ftrace_profiler(void)
914 {
915 	unregister_ftrace_function(&ftrace_profile_ops);
916 }
917 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
918 
919 static ssize_t
920 ftrace_profile_write(struct file *filp, const char __user *ubuf,
921 		     size_t cnt, loff_t *ppos)
922 {
923 	unsigned long val;
924 	int ret;
925 
926 	ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
927 	if (ret)
928 		return ret;
929 
930 	val = !!val;
931 
932 	mutex_lock(&ftrace_profile_lock);
933 	if (ftrace_profile_enabled ^ val) {
934 		if (val) {
935 			ret = ftrace_profile_init();
936 			if (ret < 0) {
937 				cnt = ret;
938 				goto out;
939 			}
940 
941 			ret = register_ftrace_profiler();
942 			if (ret < 0) {
943 				cnt = ret;
944 				goto out;
945 			}
946 			ftrace_profile_enabled = 1;
947 		} else {
948 			ftrace_profile_enabled = 0;
949 			/*
950 			 * unregister_ftrace_profiler calls stop_machine
951 			 * so this acts like an synchronize_sched.
952 			 */
953 			unregister_ftrace_profiler();
954 		}
955 	}
956  out:
957 	mutex_unlock(&ftrace_profile_lock);
958 
959 	*ppos += cnt;
960 
961 	return cnt;
962 }
963 
964 static ssize_t
965 ftrace_profile_read(struct file *filp, char __user *ubuf,
966 		     size_t cnt, loff_t *ppos)
967 {
968 	char buf[64];		/* big enough to hold a number */
969 	int r;
970 
971 	r = sprintf(buf, "%u\n", ftrace_profile_enabled);
972 	return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
973 }
974 
975 static const struct file_operations ftrace_profile_fops = {
976 	.open		= tracing_open_generic,
977 	.read		= ftrace_profile_read,
978 	.write		= ftrace_profile_write,
979 	.llseek		= default_llseek,
980 };
981 
982 /* used to initialize the real stat files */
983 static struct tracer_stat function_stats __initdata = {
984 	.name		= "functions",
985 	.stat_start	= function_stat_start,
986 	.stat_next	= function_stat_next,
987 	.stat_cmp	= function_stat_cmp,
988 	.stat_headers	= function_stat_headers,
989 	.stat_show	= function_stat_show
990 };
991 
992 static __init void ftrace_profile_debugfs(struct dentry *d_tracer)
993 {
994 	struct ftrace_profile_stat *stat;
995 	struct dentry *entry;
996 	char *name;
997 	int ret;
998 	int cpu;
999 
1000 	for_each_possible_cpu(cpu) {
1001 		stat = &per_cpu(ftrace_profile_stats, cpu);
1002 
1003 		/* allocate enough for function name + cpu number */
1004 		name = kmalloc(32, GFP_KERNEL);
1005 		if (!name) {
1006 			/*
1007 			 * The files created are permanent, if something happens
1008 			 * we still do not free memory.
1009 			 */
1010 			WARN(1,
1011 			     "Could not allocate stat file for cpu %d\n",
1012 			     cpu);
1013 			return;
1014 		}
1015 		stat->stat = function_stats;
1016 		snprintf(name, 32, "function%d", cpu);
1017 		stat->stat.name = name;
1018 		ret = register_stat_tracer(&stat->stat);
1019 		if (ret) {
1020 			WARN(1,
1021 			     "Could not register function stat for cpu %d\n",
1022 			     cpu);
1023 			kfree(name);
1024 			return;
1025 		}
1026 	}
1027 
1028 	entry = debugfs_create_file("function_profile_enabled", 0644,
1029 				    d_tracer, NULL, &ftrace_profile_fops);
1030 	if (!entry)
1031 		pr_warning("Could not create debugfs "
1032 			   "'function_profile_enabled' entry\n");
1033 }
1034 
1035 #else /* CONFIG_FUNCTION_PROFILER */
1036 static __init void ftrace_profile_debugfs(struct dentry *d_tracer)
1037 {
1038 }
1039 #endif /* CONFIG_FUNCTION_PROFILER */
1040 
1041 static struct pid * const ftrace_swapper_pid = &init_struct_pid;
1042 
1043 #ifdef CONFIG_DYNAMIC_FTRACE
1044 
1045 static struct ftrace_ops *removed_ops;
1046 
1047 #ifndef CONFIG_FTRACE_MCOUNT_RECORD
1048 # error Dynamic ftrace depends on MCOUNT_RECORD
1049 #endif
1050 
1051 static struct hlist_head ftrace_func_hash[FTRACE_FUNC_HASHSIZE] __read_mostly;
1052 
1053 struct ftrace_func_probe {
1054 	struct hlist_node	node;
1055 	struct ftrace_probe_ops	*ops;
1056 	unsigned long		flags;
1057 	unsigned long		ip;
1058 	void			*data;
1059 	struct list_head	free_list;
1060 };
1061 
1062 struct ftrace_func_entry {
1063 	struct hlist_node hlist;
1064 	unsigned long ip;
1065 };
1066 
1067 struct ftrace_hash {
1068 	unsigned long		size_bits;
1069 	struct hlist_head	*buckets;
1070 	unsigned long		count;
1071 	struct rcu_head		rcu;
1072 };
1073 
1074 /*
1075  * We make these constant because no one should touch them,
1076  * but they are used as the default "empty hash", to avoid allocating
1077  * it all the time. These are in a read only section such that if
1078  * anyone does try to modify it, it will cause an exception.
1079  */
1080 static const struct hlist_head empty_buckets[1];
1081 static const struct ftrace_hash empty_hash = {
1082 	.buckets = (struct hlist_head *)empty_buckets,
1083 };
1084 #define EMPTY_HASH	((struct ftrace_hash *)&empty_hash)
1085 
1086 static struct ftrace_ops global_ops = {
1087 	.func			= ftrace_stub,
1088 	.notrace_hash		= EMPTY_HASH,
1089 	.filter_hash		= EMPTY_HASH,
1090 	.flags			= FTRACE_OPS_FL_RECURSION_SAFE | FTRACE_OPS_FL_INITIALIZED,
1091 	INIT_REGEX_LOCK(global_ops)
1092 };
1093 
1094 struct ftrace_page {
1095 	struct ftrace_page	*next;
1096 	struct dyn_ftrace	*records;
1097 	int			index;
1098 	int			size;
1099 };
1100 
1101 #define ENTRY_SIZE sizeof(struct dyn_ftrace)
1102 #define ENTRIES_PER_PAGE (PAGE_SIZE / ENTRY_SIZE)
1103 
1104 /* estimate from running different kernels */
1105 #define NR_TO_INIT		10000
1106 
1107 static struct ftrace_page	*ftrace_pages_start;
1108 static struct ftrace_page	*ftrace_pages;
1109 
1110 static bool __always_inline ftrace_hash_empty(struct ftrace_hash *hash)
1111 {
1112 	return !hash || !hash->count;
1113 }
1114 
1115 static struct ftrace_func_entry *
1116 ftrace_lookup_ip(struct ftrace_hash *hash, unsigned long ip)
1117 {
1118 	unsigned long key;
1119 	struct ftrace_func_entry *entry;
1120 	struct hlist_head *hhd;
1121 
1122 	if (ftrace_hash_empty(hash))
1123 		return NULL;
1124 
1125 	if (hash->size_bits > 0)
1126 		key = hash_long(ip, hash->size_bits);
1127 	else
1128 		key = 0;
1129 
1130 	hhd = &hash->buckets[key];
1131 
1132 	hlist_for_each_entry_rcu_notrace(entry, hhd, hlist) {
1133 		if (entry->ip == ip)
1134 			return entry;
1135 	}
1136 	return NULL;
1137 }
1138 
1139 static void __add_hash_entry(struct ftrace_hash *hash,
1140 			     struct ftrace_func_entry *entry)
1141 {
1142 	struct hlist_head *hhd;
1143 	unsigned long key;
1144 
1145 	if (hash->size_bits)
1146 		key = hash_long(entry->ip, hash->size_bits);
1147 	else
1148 		key = 0;
1149 
1150 	hhd = &hash->buckets[key];
1151 	hlist_add_head(&entry->hlist, hhd);
1152 	hash->count++;
1153 }
1154 
1155 static int add_hash_entry(struct ftrace_hash *hash, unsigned long ip)
1156 {
1157 	struct ftrace_func_entry *entry;
1158 
1159 	entry = kmalloc(sizeof(*entry), GFP_KERNEL);
1160 	if (!entry)
1161 		return -ENOMEM;
1162 
1163 	entry->ip = ip;
1164 	__add_hash_entry(hash, entry);
1165 
1166 	return 0;
1167 }
1168 
1169 static void
1170 free_hash_entry(struct ftrace_hash *hash,
1171 		  struct ftrace_func_entry *entry)
1172 {
1173 	hlist_del(&entry->hlist);
1174 	kfree(entry);
1175 	hash->count--;
1176 }
1177 
1178 static void
1179 remove_hash_entry(struct ftrace_hash *hash,
1180 		  struct ftrace_func_entry *entry)
1181 {
1182 	hlist_del(&entry->hlist);
1183 	hash->count--;
1184 }
1185 
1186 static void ftrace_hash_clear(struct ftrace_hash *hash)
1187 {
1188 	struct hlist_head *hhd;
1189 	struct hlist_node *tn;
1190 	struct ftrace_func_entry *entry;
1191 	int size = 1 << hash->size_bits;
1192 	int i;
1193 
1194 	if (!hash->count)
1195 		return;
1196 
1197 	for (i = 0; i < size; i++) {
1198 		hhd = &hash->buckets[i];
1199 		hlist_for_each_entry_safe(entry, tn, hhd, hlist)
1200 			free_hash_entry(hash, entry);
1201 	}
1202 	FTRACE_WARN_ON(hash->count);
1203 }
1204 
1205 static void free_ftrace_hash(struct ftrace_hash *hash)
1206 {
1207 	if (!hash || hash == EMPTY_HASH)
1208 		return;
1209 	ftrace_hash_clear(hash);
1210 	kfree(hash->buckets);
1211 	kfree(hash);
1212 }
1213 
1214 static void __free_ftrace_hash_rcu(struct rcu_head *rcu)
1215 {
1216 	struct ftrace_hash *hash;
1217 
1218 	hash = container_of(rcu, struct ftrace_hash, rcu);
1219 	free_ftrace_hash(hash);
1220 }
1221 
1222 static void free_ftrace_hash_rcu(struct ftrace_hash *hash)
1223 {
1224 	if (!hash || hash == EMPTY_HASH)
1225 		return;
1226 	call_rcu_sched(&hash->rcu, __free_ftrace_hash_rcu);
1227 }
1228 
1229 void ftrace_free_filter(struct ftrace_ops *ops)
1230 {
1231 	ftrace_ops_init(ops);
1232 	free_ftrace_hash(ops->filter_hash);
1233 	free_ftrace_hash(ops->notrace_hash);
1234 }
1235 
1236 static struct ftrace_hash *alloc_ftrace_hash(int size_bits)
1237 {
1238 	struct ftrace_hash *hash;
1239 	int size;
1240 
1241 	hash = kzalloc(sizeof(*hash), GFP_KERNEL);
1242 	if (!hash)
1243 		return NULL;
1244 
1245 	size = 1 << size_bits;
1246 	hash->buckets = kcalloc(size, sizeof(*hash->buckets), GFP_KERNEL);
1247 
1248 	if (!hash->buckets) {
1249 		kfree(hash);
1250 		return NULL;
1251 	}
1252 
1253 	hash->size_bits = size_bits;
1254 
1255 	return hash;
1256 }
1257 
1258 static struct ftrace_hash *
1259 alloc_and_copy_ftrace_hash(int size_bits, struct ftrace_hash *hash)
1260 {
1261 	struct ftrace_func_entry *entry;
1262 	struct ftrace_hash *new_hash;
1263 	int size;
1264 	int ret;
1265 	int i;
1266 
1267 	new_hash = alloc_ftrace_hash(size_bits);
1268 	if (!new_hash)
1269 		return NULL;
1270 
1271 	/* Empty hash? */
1272 	if (ftrace_hash_empty(hash))
1273 		return new_hash;
1274 
1275 	size = 1 << hash->size_bits;
1276 	for (i = 0; i < size; i++) {
1277 		hlist_for_each_entry(entry, &hash->buckets[i], hlist) {
1278 			ret = add_hash_entry(new_hash, entry->ip);
1279 			if (ret < 0)
1280 				goto free_hash;
1281 		}
1282 	}
1283 
1284 	FTRACE_WARN_ON(new_hash->count != hash->count);
1285 
1286 	return new_hash;
1287 
1288  free_hash:
1289 	free_ftrace_hash(new_hash);
1290 	return NULL;
1291 }
1292 
1293 static void
1294 ftrace_hash_rec_disable(struct ftrace_ops *ops, int filter_hash);
1295 static void
1296 ftrace_hash_rec_enable(struct ftrace_ops *ops, int filter_hash);
1297 
1298 static int
1299 ftrace_hash_move(struct ftrace_ops *ops, int enable,
1300 		 struct ftrace_hash **dst, struct ftrace_hash *src)
1301 {
1302 	struct ftrace_func_entry *entry;
1303 	struct hlist_node *tn;
1304 	struct hlist_head *hhd;
1305 	struct ftrace_hash *old_hash;
1306 	struct ftrace_hash *new_hash;
1307 	int size = src->count;
1308 	int bits = 0;
1309 	int i;
1310 
1311 	/*
1312 	 * If the new source is empty, just free dst and assign it
1313 	 * the empty_hash.
1314 	 */
1315 	if (!src->count) {
1316 		new_hash = EMPTY_HASH;
1317 		goto update;
1318 	}
1319 
1320 	/*
1321 	 * Make the hash size about 1/2 the # found
1322 	 */
1323 	for (size /= 2; size; size >>= 1)
1324 		bits++;
1325 
1326 	/* Don't allocate too much */
1327 	if (bits > FTRACE_HASH_MAX_BITS)
1328 		bits = FTRACE_HASH_MAX_BITS;
1329 
1330 	new_hash = alloc_ftrace_hash(bits);
1331 	if (!new_hash)
1332 		return -ENOMEM;
1333 
1334 	size = 1 << src->size_bits;
1335 	for (i = 0; i < size; i++) {
1336 		hhd = &src->buckets[i];
1337 		hlist_for_each_entry_safe(entry, tn, hhd, hlist) {
1338 			remove_hash_entry(src, entry);
1339 			__add_hash_entry(new_hash, entry);
1340 		}
1341 	}
1342 
1343 update:
1344 	/*
1345 	 * Remove the current set, update the hash and add
1346 	 * them back.
1347 	 */
1348 	ftrace_hash_rec_disable(ops, enable);
1349 
1350 	old_hash = *dst;
1351 	rcu_assign_pointer(*dst, new_hash);
1352 	free_ftrace_hash_rcu(old_hash);
1353 
1354 	ftrace_hash_rec_enable(ops, enable);
1355 
1356 	return 0;
1357 }
1358 
1359 /*
1360  * Test the hashes for this ops to see if we want to call
1361  * the ops->func or not.
1362  *
1363  * It's a match if the ip is in the ops->filter_hash or
1364  * the filter_hash does not exist or is empty,
1365  *  AND
1366  * the ip is not in the ops->notrace_hash.
1367  *
1368  * This needs to be called with preemption disabled as
1369  * the hashes are freed with call_rcu_sched().
1370  */
1371 static int
1372 ftrace_ops_test(struct ftrace_ops *ops, unsigned long ip, void *regs)
1373 {
1374 	struct ftrace_hash *filter_hash;
1375 	struct ftrace_hash *notrace_hash;
1376 	int ret;
1377 
1378 #ifdef CONFIG_DYNAMIC_FTRACE_WITH_REGS
1379 	/*
1380 	 * There's a small race when adding ops that the ftrace handler
1381 	 * that wants regs, may be called without them. We can not
1382 	 * allow that handler to be called if regs is NULL.
1383 	 */
1384 	if (regs == NULL && (ops->flags & FTRACE_OPS_FL_SAVE_REGS))
1385 		return 0;
1386 #endif
1387 
1388 	filter_hash = rcu_dereference_raw_notrace(ops->filter_hash);
1389 	notrace_hash = rcu_dereference_raw_notrace(ops->notrace_hash);
1390 
1391 	if ((ftrace_hash_empty(filter_hash) ||
1392 	     ftrace_lookup_ip(filter_hash, ip)) &&
1393 	    (ftrace_hash_empty(notrace_hash) ||
1394 	     !ftrace_lookup_ip(notrace_hash, ip)))
1395 		ret = 1;
1396 	else
1397 		ret = 0;
1398 
1399 	return ret;
1400 }
1401 
1402 /*
1403  * This is a double for. Do not use 'break' to break out of the loop,
1404  * you must use a goto.
1405  */
1406 #define do_for_each_ftrace_rec(pg, rec)					\
1407 	for (pg = ftrace_pages_start; pg; pg = pg->next) {		\
1408 		int _____i;						\
1409 		for (_____i = 0; _____i < pg->index; _____i++) {	\
1410 			rec = &pg->records[_____i];
1411 
1412 #define while_for_each_ftrace_rec()		\
1413 		}				\
1414 	}
1415 
1416 
1417 static int ftrace_cmp_recs(const void *a, const void *b)
1418 {
1419 	const struct dyn_ftrace *key = a;
1420 	const struct dyn_ftrace *rec = b;
1421 
1422 	if (key->flags < rec->ip)
1423 		return -1;
1424 	if (key->ip >= rec->ip + MCOUNT_INSN_SIZE)
1425 		return 1;
1426 	return 0;
1427 }
1428 
1429 static unsigned long ftrace_location_range(unsigned long start, unsigned long end)
1430 {
1431 	struct ftrace_page *pg;
1432 	struct dyn_ftrace *rec;
1433 	struct dyn_ftrace key;
1434 
1435 	key.ip = start;
1436 	key.flags = end;	/* overload flags, as it is unsigned long */
1437 
1438 	for (pg = ftrace_pages_start; pg; pg = pg->next) {
1439 		if (end < pg->records[0].ip ||
1440 		    start >= (pg->records[pg->index - 1].ip + MCOUNT_INSN_SIZE))
1441 			continue;
1442 		rec = bsearch(&key, pg->records, pg->index,
1443 			      sizeof(struct dyn_ftrace),
1444 			      ftrace_cmp_recs);
1445 		if (rec)
1446 			return rec->ip;
1447 	}
1448 
1449 	return 0;
1450 }
1451 
1452 /**
1453  * ftrace_location - return true if the ip giving is a traced location
1454  * @ip: the instruction pointer to check
1455  *
1456  * Returns rec->ip if @ip given is a pointer to a ftrace location.
1457  * That is, the instruction that is either a NOP or call to
1458  * the function tracer. It checks the ftrace internal tables to
1459  * determine if the address belongs or not.
1460  */
1461 unsigned long ftrace_location(unsigned long ip)
1462 {
1463 	return ftrace_location_range(ip, ip);
1464 }
1465 
1466 /**
1467  * ftrace_text_reserved - return true if range contains an ftrace location
1468  * @start: start of range to search
1469  * @end: end of range to search (inclusive). @end points to the last byte to check.
1470  *
1471  * Returns 1 if @start and @end contains a ftrace location.
1472  * That is, the instruction that is either a NOP or call to
1473  * the function tracer. It checks the ftrace internal tables to
1474  * determine if the address belongs or not.
1475  */
1476 int ftrace_text_reserved(const void *start, const void *end)
1477 {
1478 	unsigned long ret;
1479 
1480 	ret = ftrace_location_range((unsigned long)start,
1481 				    (unsigned long)end);
1482 
1483 	return (int)!!ret;
1484 }
1485 
1486 /* Test if ops registered to this rec needs regs */
1487 static bool test_rec_ops_needs_regs(struct dyn_ftrace *rec)
1488 {
1489 	struct ftrace_ops *ops;
1490 	bool keep_regs = false;
1491 
1492 	for (ops = ftrace_ops_list;
1493 	     ops != &ftrace_list_end; ops = ops->next) {
1494 		/* pass rec in as regs to have non-NULL val */
1495 		if (ftrace_ops_test(ops, rec->ip, rec)) {
1496 			if (ops->flags & FTRACE_OPS_FL_SAVE_REGS) {
1497 				keep_regs = true;
1498 				break;
1499 			}
1500 		}
1501 	}
1502 
1503 	return  keep_regs;
1504 }
1505 
1506 static void ftrace_remove_tramp(struct ftrace_ops *ops,
1507 				struct dyn_ftrace *rec)
1508 {
1509 	struct ftrace_func_entry *entry;
1510 
1511 	entry = ftrace_lookup_ip(ops->tramp_hash, rec->ip);
1512 	if (!entry)
1513 		return;
1514 
1515 	/*
1516 	 * The tramp_hash entry will be removed at time
1517 	 * of update.
1518 	 */
1519 	ops->trampolines--;
1520 	rec->flags &= ~FTRACE_FL_TRAMP;
1521 }
1522 
1523 static void ftrace_clear_tramps(struct dyn_ftrace *rec)
1524 {
1525 	struct ftrace_ops *op;
1526 
1527 	do_for_each_ftrace_op(op, ftrace_ops_list) {
1528 		if (op->trampolines)
1529 			ftrace_remove_tramp(op, rec);
1530 	} while_for_each_ftrace_op(op);
1531 }
1532 
1533 static void __ftrace_hash_rec_update(struct ftrace_ops *ops,
1534 				     int filter_hash,
1535 				     bool inc)
1536 {
1537 	struct ftrace_hash *hash;
1538 	struct ftrace_hash *other_hash;
1539 	struct ftrace_page *pg;
1540 	struct dyn_ftrace *rec;
1541 	int count = 0;
1542 	int all = 0;
1543 
1544 	/* Only update if the ops has been registered */
1545 	if (!(ops->flags & FTRACE_OPS_FL_ENABLED))
1546 		return;
1547 
1548 	/*
1549 	 * In the filter_hash case:
1550 	 *   If the count is zero, we update all records.
1551 	 *   Otherwise we just update the items in the hash.
1552 	 *
1553 	 * In the notrace_hash case:
1554 	 *   We enable the update in the hash.
1555 	 *   As disabling notrace means enabling the tracing,
1556 	 *   and enabling notrace means disabling, the inc variable
1557 	 *   gets inversed.
1558 	 */
1559 	if (filter_hash) {
1560 		hash = ops->filter_hash;
1561 		other_hash = ops->notrace_hash;
1562 		if (ftrace_hash_empty(hash))
1563 			all = 1;
1564 	} else {
1565 		inc = !inc;
1566 		hash = ops->notrace_hash;
1567 		other_hash = ops->filter_hash;
1568 		/*
1569 		 * If the notrace hash has no items,
1570 		 * then there's nothing to do.
1571 		 */
1572 		if (ftrace_hash_empty(hash))
1573 			return;
1574 	}
1575 
1576 	do_for_each_ftrace_rec(pg, rec) {
1577 		int in_other_hash = 0;
1578 		int in_hash = 0;
1579 		int match = 0;
1580 
1581 		if (all) {
1582 			/*
1583 			 * Only the filter_hash affects all records.
1584 			 * Update if the record is not in the notrace hash.
1585 			 */
1586 			if (!other_hash || !ftrace_lookup_ip(other_hash, rec->ip))
1587 				match = 1;
1588 		} else {
1589 			in_hash = !!ftrace_lookup_ip(hash, rec->ip);
1590 			in_other_hash = !!ftrace_lookup_ip(other_hash, rec->ip);
1591 
1592 			/*
1593 			 * If filter_hash is set, we want to match all functions
1594 			 * that are in the hash but not in the other hash.
1595 			 *
1596 			 * If filter_hash is not set, then we are decrementing.
1597 			 * That means we match anything that is in the hash
1598 			 * and also in the other_hash. That is, we need to turn
1599 			 * off functions in the other hash because they are disabled
1600 			 * by this hash.
1601 			 */
1602 			if (filter_hash && in_hash && !in_other_hash)
1603 				match = 1;
1604 			else if (!filter_hash && in_hash &&
1605 				 (in_other_hash || ftrace_hash_empty(other_hash)))
1606 				match = 1;
1607 		}
1608 		if (!match)
1609 			continue;
1610 
1611 		if (inc) {
1612 			rec->flags++;
1613 			if (FTRACE_WARN_ON(ftrace_rec_count(rec) == FTRACE_REF_MAX))
1614 				return;
1615 
1616 			/*
1617 			 * If there's only a single callback registered to a
1618 			 * function, and the ops has a trampoline registered
1619 			 * for it, then we can call it directly.
1620 			 */
1621 			if (ftrace_rec_count(rec) == 1 && ops->trampoline) {
1622 				rec->flags |= FTRACE_FL_TRAMP;
1623 				ops->trampolines++;
1624 			} else {
1625 				/*
1626 				 * If we are adding another function callback
1627 				 * to this function, and the previous had a
1628 				 * trampoline used, then we need to go back to
1629 				 * the default trampoline.
1630 				 */
1631 				rec->flags &= ~FTRACE_FL_TRAMP;
1632 
1633 				/* remove trampolines from any ops for this rec */
1634 				ftrace_clear_tramps(rec);
1635 			}
1636 
1637 			/*
1638 			 * If any ops wants regs saved for this function
1639 			 * then all ops will get saved regs.
1640 			 */
1641 			if (ops->flags & FTRACE_OPS_FL_SAVE_REGS)
1642 				rec->flags |= FTRACE_FL_REGS;
1643 		} else {
1644 			if (FTRACE_WARN_ON(ftrace_rec_count(rec) == 0))
1645 				return;
1646 			rec->flags--;
1647 
1648 			if (ops->trampoline && !ftrace_rec_count(rec))
1649 				ftrace_remove_tramp(ops, rec);
1650 
1651 			/*
1652 			 * If the rec had REGS enabled and the ops that is
1653 			 * being removed had REGS set, then see if there is
1654 			 * still any ops for this record that wants regs.
1655 			 * If not, we can stop recording them.
1656 			 */
1657 			if (ftrace_rec_count(rec) > 0 &&
1658 			    rec->flags & FTRACE_FL_REGS &&
1659 			    ops->flags & FTRACE_OPS_FL_SAVE_REGS) {
1660 				if (!test_rec_ops_needs_regs(rec))
1661 					rec->flags &= ~FTRACE_FL_REGS;
1662 			}
1663 
1664 			/*
1665 			 * flags will be cleared in ftrace_check_record()
1666 			 * if rec count is zero.
1667 			 */
1668 		}
1669 		count++;
1670 		/* Shortcut, if we handled all records, we are done. */
1671 		if (!all && count == hash->count)
1672 			return;
1673 	} while_for_each_ftrace_rec();
1674 }
1675 
1676 static void ftrace_hash_rec_disable(struct ftrace_ops *ops,
1677 				    int filter_hash)
1678 {
1679 	__ftrace_hash_rec_update(ops, filter_hash, 0);
1680 }
1681 
1682 static void ftrace_hash_rec_enable(struct ftrace_ops *ops,
1683 				   int filter_hash)
1684 {
1685 	__ftrace_hash_rec_update(ops, filter_hash, 1);
1686 }
1687 
1688 static void print_ip_ins(const char *fmt, unsigned char *p)
1689 {
1690 	int i;
1691 
1692 	printk(KERN_CONT "%s", fmt);
1693 
1694 	for (i = 0; i < MCOUNT_INSN_SIZE; i++)
1695 		printk(KERN_CONT "%s%02x", i ? ":" : "", p[i]);
1696 }
1697 
1698 /**
1699  * ftrace_bug - report and shutdown function tracer
1700  * @failed: The failed type (EFAULT, EINVAL, EPERM)
1701  * @ip: The address that failed
1702  *
1703  * The arch code that enables or disables the function tracing
1704  * can call ftrace_bug() when it has detected a problem in
1705  * modifying the code. @failed should be one of either:
1706  * EFAULT - if the problem happens on reading the @ip address
1707  * EINVAL - if what is read at @ip is not what was expected
1708  * EPERM - if the problem happens on writting to the @ip address
1709  */
1710 void ftrace_bug(int failed, unsigned long ip)
1711 {
1712 	switch (failed) {
1713 	case -EFAULT:
1714 		FTRACE_WARN_ON_ONCE(1);
1715 		pr_info("ftrace faulted on modifying ");
1716 		print_ip_sym(ip);
1717 		break;
1718 	case -EINVAL:
1719 		FTRACE_WARN_ON_ONCE(1);
1720 		pr_info("ftrace failed to modify ");
1721 		print_ip_sym(ip);
1722 		print_ip_ins(" actual: ", (unsigned char *)ip);
1723 		printk(KERN_CONT "\n");
1724 		break;
1725 	case -EPERM:
1726 		FTRACE_WARN_ON_ONCE(1);
1727 		pr_info("ftrace faulted on writing ");
1728 		print_ip_sym(ip);
1729 		break;
1730 	default:
1731 		FTRACE_WARN_ON_ONCE(1);
1732 		pr_info("ftrace faulted on unknown error ");
1733 		print_ip_sym(ip);
1734 	}
1735 }
1736 
1737 static int ftrace_check_record(struct dyn_ftrace *rec, int enable, int update)
1738 {
1739 	unsigned long flag = 0UL;
1740 
1741 	/*
1742 	 * If we are updating calls:
1743 	 *
1744 	 *   If the record has a ref count, then we need to enable it
1745 	 *   because someone is using it.
1746 	 *
1747 	 *   Otherwise we make sure its disabled.
1748 	 *
1749 	 * If we are disabling calls, then disable all records that
1750 	 * are enabled.
1751 	 */
1752 	if (enable && ftrace_rec_count(rec))
1753 		flag = FTRACE_FL_ENABLED;
1754 
1755 	/*
1756 	 * If enabling and the REGS flag does not match the REGS_EN, or
1757 	 * the TRAMP flag doesn't match the TRAMP_EN, then do not ignore
1758 	 * this record. Set flags to fail the compare against ENABLED.
1759 	 */
1760 	if (flag) {
1761 		if (!(rec->flags & FTRACE_FL_REGS) !=
1762 		    !(rec->flags & FTRACE_FL_REGS_EN))
1763 			flag |= FTRACE_FL_REGS;
1764 
1765 		if (!(rec->flags & FTRACE_FL_TRAMP) !=
1766 		    !(rec->flags & FTRACE_FL_TRAMP_EN))
1767 			flag |= FTRACE_FL_TRAMP;
1768 	}
1769 
1770 	/* If the state of this record hasn't changed, then do nothing */
1771 	if ((rec->flags & FTRACE_FL_ENABLED) == flag)
1772 		return FTRACE_UPDATE_IGNORE;
1773 
1774 	if (flag) {
1775 		/* Save off if rec is being enabled (for return value) */
1776 		flag ^= rec->flags & FTRACE_FL_ENABLED;
1777 
1778 		if (update) {
1779 			rec->flags |= FTRACE_FL_ENABLED;
1780 			if (flag & FTRACE_FL_REGS) {
1781 				if (rec->flags & FTRACE_FL_REGS)
1782 					rec->flags |= FTRACE_FL_REGS_EN;
1783 				else
1784 					rec->flags &= ~FTRACE_FL_REGS_EN;
1785 			}
1786 			if (flag & FTRACE_FL_TRAMP) {
1787 				if (rec->flags & FTRACE_FL_TRAMP)
1788 					rec->flags |= FTRACE_FL_TRAMP_EN;
1789 				else
1790 					rec->flags &= ~FTRACE_FL_TRAMP_EN;
1791 			}
1792 		}
1793 
1794 		/*
1795 		 * If this record is being updated from a nop, then
1796 		 *   return UPDATE_MAKE_CALL.
1797 		 * Otherwise,
1798 		 *   return UPDATE_MODIFY_CALL to tell the caller to convert
1799 		 *   from the save regs, to a non-save regs function or
1800 		 *   vice versa, or from a trampoline call.
1801 		 */
1802 		if (flag & FTRACE_FL_ENABLED)
1803 			return FTRACE_UPDATE_MAKE_CALL;
1804 
1805 		return FTRACE_UPDATE_MODIFY_CALL;
1806 	}
1807 
1808 	if (update) {
1809 		/* If there's no more users, clear all flags */
1810 		if (!ftrace_rec_count(rec))
1811 			rec->flags = 0;
1812 		else
1813 			/* Just disable the record (keep REGS state) */
1814 			rec->flags &= ~FTRACE_FL_ENABLED;
1815 	}
1816 
1817 	return FTRACE_UPDATE_MAKE_NOP;
1818 }
1819 
1820 /**
1821  * ftrace_update_record, set a record that now is tracing or not
1822  * @rec: the record to update
1823  * @enable: set to 1 if the record is tracing, zero to force disable
1824  *
1825  * The records that represent all functions that can be traced need
1826  * to be updated when tracing has been enabled.
1827  */
1828 int ftrace_update_record(struct dyn_ftrace *rec, int enable)
1829 {
1830 	return ftrace_check_record(rec, enable, 1);
1831 }
1832 
1833 /**
1834  * ftrace_test_record, check if the record has been enabled or not
1835  * @rec: the record to test
1836  * @enable: set to 1 to check if enabled, 0 if it is disabled
1837  *
1838  * The arch code may need to test if a record is already set to
1839  * tracing to determine how to modify the function code that it
1840  * represents.
1841  */
1842 int ftrace_test_record(struct dyn_ftrace *rec, int enable)
1843 {
1844 	return ftrace_check_record(rec, enable, 0);
1845 }
1846 
1847 static struct ftrace_ops *
1848 ftrace_find_tramp_ops_curr(struct dyn_ftrace *rec)
1849 {
1850 	struct ftrace_ops *op;
1851 
1852 	/* Removed ops need to be tested first */
1853 	if (removed_ops && removed_ops->tramp_hash) {
1854 		if (ftrace_lookup_ip(removed_ops->tramp_hash, rec->ip))
1855 			return removed_ops;
1856 	}
1857 
1858 	do_for_each_ftrace_op(op, ftrace_ops_list) {
1859 		if (!op->tramp_hash)
1860 			continue;
1861 
1862 		if (ftrace_lookup_ip(op->tramp_hash, rec->ip))
1863 			return op;
1864 
1865 	} while_for_each_ftrace_op(op);
1866 
1867 	return NULL;
1868 }
1869 
1870 static struct ftrace_ops *
1871 ftrace_find_tramp_ops_new(struct dyn_ftrace *rec)
1872 {
1873 	struct ftrace_ops *op;
1874 
1875 	do_for_each_ftrace_op(op, ftrace_ops_list) {
1876 		/* pass rec in as regs to have non-NULL val */
1877 		if (ftrace_ops_test(op, rec->ip, rec))
1878 			return op;
1879 	} while_for_each_ftrace_op(op);
1880 
1881 	return NULL;
1882 }
1883 
1884 /**
1885  * ftrace_get_addr_new - Get the call address to set to
1886  * @rec:  The ftrace record descriptor
1887  *
1888  * If the record has the FTRACE_FL_REGS set, that means that it
1889  * wants to convert to a callback that saves all regs. If FTRACE_FL_REGS
1890  * is not not set, then it wants to convert to the normal callback.
1891  *
1892  * Returns the address of the trampoline to set to
1893  */
1894 unsigned long ftrace_get_addr_new(struct dyn_ftrace *rec)
1895 {
1896 	struct ftrace_ops *ops;
1897 
1898 	/* Trampolines take precedence over regs */
1899 	if (rec->flags & FTRACE_FL_TRAMP) {
1900 		ops = ftrace_find_tramp_ops_new(rec);
1901 		if (FTRACE_WARN_ON(!ops || !ops->trampoline)) {
1902 			pr_warning("Bad trampoline accounting at: %p (%pS)\n",
1903 				    (void *)rec->ip, (void *)rec->ip);
1904 			/* Ftrace is shutting down, return anything */
1905 			return (unsigned long)FTRACE_ADDR;
1906 		}
1907 		return ops->trampoline;
1908 	}
1909 
1910 	if (rec->flags & FTRACE_FL_REGS)
1911 		return (unsigned long)FTRACE_REGS_ADDR;
1912 	else
1913 		return (unsigned long)FTRACE_ADDR;
1914 }
1915 
1916 /**
1917  * ftrace_get_addr_curr - Get the call address that is already there
1918  * @rec:  The ftrace record descriptor
1919  *
1920  * The FTRACE_FL_REGS_EN is set when the record already points to
1921  * a function that saves all the regs. Basically the '_EN' version
1922  * represents the current state of the function.
1923  *
1924  * Returns the address of the trampoline that is currently being called
1925  */
1926 unsigned long ftrace_get_addr_curr(struct dyn_ftrace *rec)
1927 {
1928 	struct ftrace_ops *ops;
1929 
1930 	/* Trampolines take precedence over regs */
1931 	if (rec->flags & FTRACE_FL_TRAMP_EN) {
1932 		ops = ftrace_find_tramp_ops_curr(rec);
1933 		if (FTRACE_WARN_ON(!ops)) {
1934 			pr_warning("Bad trampoline accounting at: %p (%pS)\n",
1935 				    (void *)rec->ip, (void *)rec->ip);
1936 			/* Ftrace is shutting down, return anything */
1937 			return (unsigned long)FTRACE_ADDR;
1938 		}
1939 		return ops->trampoline;
1940 	}
1941 
1942 	if (rec->flags & FTRACE_FL_REGS_EN)
1943 		return (unsigned long)FTRACE_REGS_ADDR;
1944 	else
1945 		return (unsigned long)FTRACE_ADDR;
1946 }
1947 
1948 static int
1949 __ftrace_replace_code(struct dyn_ftrace *rec, int enable)
1950 {
1951 	unsigned long ftrace_old_addr;
1952 	unsigned long ftrace_addr;
1953 	int ret;
1954 
1955 	ftrace_addr = ftrace_get_addr_new(rec);
1956 
1957 	/* This needs to be done before we call ftrace_update_record */
1958 	ftrace_old_addr = ftrace_get_addr_curr(rec);
1959 
1960 	ret = ftrace_update_record(rec, enable);
1961 
1962 	switch (ret) {
1963 	case FTRACE_UPDATE_IGNORE:
1964 		return 0;
1965 
1966 	case FTRACE_UPDATE_MAKE_CALL:
1967 		return ftrace_make_call(rec, ftrace_addr);
1968 
1969 	case FTRACE_UPDATE_MAKE_NOP:
1970 		return ftrace_make_nop(NULL, rec, ftrace_addr);
1971 
1972 	case FTRACE_UPDATE_MODIFY_CALL:
1973 		return ftrace_modify_call(rec, ftrace_old_addr, ftrace_addr);
1974 	}
1975 
1976 	return -1; /* unknow ftrace bug */
1977 }
1978 
1979 void __weak ftrace_replace_code(int enable)
1980 {
1981 	struct dyn_ftrace *rec;
1982 	struct ftrace_page *pg;
1983 	int failed;
1984 
1985 	if (unlikely(ftrace_disabled))
1986 		return;
1987 
1988 	do_for_each_ftrace_rec(pg, rec) {
1989 		failed = __ftrace_replace_code(rec, enable);
1990 		if (failed) {
1991 			ftrace_bug(failed, rec->ip);
1992 			/* Stop processing */
1993 			return;
1994 		}
1995 	} while_for_each_ftrace_rec();
1996 }
1997 
1998 struct ftrace_rec_iter {
1999 	struct ftrace_page	*pg;
2000 	int			index;
2001 };
2002 
2003 /**
2004  * ftrace_rec_iter_start, start up iterating over traced functions
2005  *
2006  * Returns an iterator handle that is used to iterate over all
2007  * the records that represent address locations where functions
2008  * are traced.
2009  *
2010  * May return NULL if no records are available.
2011  */
2012 struct ftrace_rec_iter *ftrace_rec_iter_start(void)
2013 {
2014 	/*
2015 	 * We only use a single iterator.
2016 	 * Protected by the ftrace_lock mutex.
2017 	 */
2018 	static struct ftrace_rec_iter ftrace_rec_iter;
2019 	struct ftrace_rec_iter *iter = &ftrace_rec_iter;
2020 
2021 	iter->pg = ftrace_pages_start;
2022 	iter->index = 0;
2023 
2024 	/* Could have empty pages */
2025 	while (iter->pg && !iter->pg->index)
2026 		iter->pg = iter->pg->next;
2027 
2028 	if (!iter->pg)
2029 		return NULL;
2030 
2031 	return iter;
2032 }
2033 
2034 /**
2035  * ftrace_rec_iter_next, get the next record to process.
2036  * @iter: The handle to the iterator.
2037  *
2038  * Returns the next iterator after the given iterator @iter.
2039  */
2040 struct ftrace_rec_iter *ftrace_rec_iter_next(struct ftrace_rec_iter *iter)
2041 {
2042 	iter->index++;
2043 
2044 	if (iter->index >= iter->pg->index) {
2045 		iter->pg = iter->pg->next;
2046 		iter->index = 0;
2047 
2048 		/* Could have empty pages */
2049 		while (iter->pg && !iter->pg->index)
2050 			iter->pg = iter->pg->next;
2051 	}
2052 
2053 	if (!iter->pg)
2054 		return NULL;
2055 
2056 	return iter;
2057 }
2058 
2059 /**
2060  * ftrace_rec_iter_record, get the record at the iterator location
2061  * @iter: The current iterator location
2062  *
2063  * Returns the record that the current @iter is at.
2064  */
2065 struct dyn_ftrace *ftrace_rec_iter_record(struct ftrace_rec_iter *iter)
2066 {
2067 	return &iter->pg->records[iter->index];
2068 }
2069 
2070 static int
2071 ftrace_code_disable(struct module *mod, struct dyn_ftrace *rec)
2072 {
2073 	unsigned long ip;
2074 	int ret;
2075 
2076 	ip = rec->ip;
2077 
2078 	if (unlikely(ftrace_disabled))
2079 		return 0;
2080 
2081 	ret = ftrace_make_nop(mod, rec, MCOUNT_ADDR);
2082 	if (ret) {
2083 		ftrace_bug(ret, ip);
2084 		return 0;
2085 	}
2086 	return 1;
2087 }
2088 
2089 /*
2090  * archs can override this function if they must do something
2091  * before the modifying code is performed.
2092  */
2093 int __weak ftrace_arch_code_modify_prepare(void)
2094 {
2095 	return 0;
2096 }
2097 
2098 /*
2099  * archs can override this function if they must do something
2100  * after the modifying code is performed.
2101  */
2102 int __weak ftrace_arch_code_modify_post_process(void)
2103 {
2104 	return 0;
2105 }
2106 
2107 void ftrace_modify_all_code(int command)
2108 {
2109 	int update = command & FTRACE_UPDATE_TRACE_FUNC;
2110 	int err = 0;
2111 
2112 	/*
2113 	 * If the ftrace_caller calls a ftrace_ops func directly,
2114 	 * we need to make sure that it only traces functions it
2115 	 * expects to trace. When doing the switch of functions,
2116 	 * we need to update to the ftrace_ops_list_func first
2117 	 * before the transition between old and new calls are set,
2118 	 * as the ftrace_ops_list_func will check the ops hashes
2119 	 * to make sure the ops are having the right functions
2120 	 * traced.
2121 	 */
2122 	if (update) {
2123 		err = ftrace_update_ftrace_func(ftrace_ops_list_func);
2124 		if (FTRACE_WARN_ON(err))
2125 			return;
2126 	}
2127 
2128 	if (command & FTRACE_UPDATE_CALLS)
2129 		ftrace_replace_code(1);
2130 	else if (command & FTRACE_DISABLE_CALLS)
2131 		ftrace_replace_code(0);
2132 
2133 	if (update && ftrace_trace_function != ftrace_ops_list_func) {
2134 		function_trace_op = set_function_trace_op;
2135 		smp_wmb();
2136 		/* If irqs are disabled, we are in stop machine */
2137 		if (!irqs_disabled())
2138 			smp_call_function(ftrace_sync_ipi, NULL, 1);
2139 		err = ftrace_update_ftrace_func(ftrace_trace_function);
2140 		if (FTRACE_WARN_ON(err))
2141 			return;
2142 	}
2143 
2144 	if (command & FTRACE_START_FUNC_RET)
2145 		err = ftrace_enable_ftrace_graph_caller();
2146 	else if (command & FTRACE_STOP_FUNC_RET)
2147 		err = ftrace_disable_ftrace_graph_caller();
2148 	FTRACE_WARN_ON(err);
2149 }
2150 
2151 static int __ftrace_modify_code(void *data)
2152 {
2153 	int *command = data;
2154 
2155 	ftrace_modify_all_code(*command);
2156 
2157 	return 0;
2158 }
2159 
2160 /**
2161  * ftrace_run_stop_machine, go back to the stop machine method
2162  * @command: The command to tell ftrace what to do
2163  *
2164  * If an arch needs to fall back to the stop machine method, the
2165  * it can call this function.
2166  */
2167 void ftrace_run_stop_machine(int command)
2168 {
2169 	stop_machine(__ftrace_modify_code, &command, NULL);
2170 }
2171 
2172 /**
2173  * arch_ftrace_update_code, modify the code to trace or not trace
2174  * @command: The command that needs to be done
2175  *
2176  * Archs can override this function if it does not need to
2177  * run stop_machine() to modify code.
2178  */
2179 void __weak arch_ftrace_update_code(int command)
2180 {
2181 	ftrace_run_stop_machine(command);
2182 }
2183 
2184 static int ftrace_save_ops_tramp_hash(struct ftrace_ops *ops)
2185 {
2186 	struct ftrace_page *pg;
2187 	struct dyn_ftrace *rec;
2188 	int size, bits;
2189 	int ret;
2190 
2191 	size = ops->trampolines;
2192 	bits = 0;
2193 	/*
2194 	 * Make the hash size about 1/2 the # found
2195 	 */
2196 	for (size /= 2; size; size >>= 1)
2197 		bits++;
2198 
2199 	ops->tramp_hash = alloc_ftrace_hash(bits);
2200 	/*
2201 	 * TODO: a failed allocation is going to screw up
2202 	 * the accounting of what needs to be modified
2203 	 * and not. For now, we kill ftrace if we fail
2204 	 * to allocate here. But there are ways around this,
2205 	 * but that will take a little more work.
2206 	 */
2207 	if (!ops->tramp_hash)
2208 		return -ENOMEM;
2209 
2210 	do_for_each_ftrace_rec(pg, rec) {
2211 		if (ftrace_rec_count(rec) == 1 &&
2212 		    ftrace_ops_test(ops, rec->ip, rec)) {
2213 
2214 			/* This record had better have a trampoline */
2215 			if (FTRACE_WARN_ON(!(rec->flags & FTRACE_FL_TRAMP_EN)))
2216 				return -1;
2217 
2218 			ret = add_hash_entry(ops->tramp_hash, rec->ip);
2219 			if (ret < 0)
2220 				return ret;
2221 		}
2222 	} while_for_each_ftrace_rec();
2223 
2224 	return 0;
2225 }
2226 
2227 static int ftrace_save_tramp_hashes(void)
2228 {
2229 	struct ftrace_ops *op;
2230 	int ret;
2231 
2232 	/*
2233 	 * Now that any trampoline is being used, we need to save the
2234 	 * hashes for the ops that have them. This allows the mapping
2235 	 * back from the record to the ops that has the trampoline to
2236 	 * know what code is being replaced. Modifying code must always
2237 	 * verify what it is changing.
2238 	 */
2239 	do_for_each_ftrace_op(op, ftrace_ops_list) {
2240 
2241 		/* The tramp_hash is recreated each time. */
2242 		free_ftrace_hash(op->tramp_hash);
2243 		op->tramp_hash = NULL;
2244 
2245 		if (op->trampolines) {
2246 			ret = ftrace_save_ops_tramp_hash(op);
2247 			if (ret)
2248 				return ret;
2249 		}
2250 
2251 	} while_for_each_ftrace_op(op);
2252 
2253 	return 0;
2254 }
2255 
2256 static void ftrace_run_update_code(int command)
2257 {
2258 	int ret;
2259 
2260 	ret = ftrace_arch_code_modify_prepare();
2261 	FTRACE_WARN_ON(ret);
2262 	if (ret)
2263 		return;
2264 
2265 	/*
2266 	 * By default we use stop_machine() to modify the code.
2267 	 * But archs can do what ever they want as long as it
2268 	 * is safe. The stop_machine() is the safest, but also
2269 	 * produces the most overhead.
2270 	 */
2271 	arch_ftrace_update_code(command);
2272 
2273 	ret = ftrace_arch_code_modify_post_process();
2274 	FTRACE_WARN_ON(ret);
2275 
2276 	ret = ftrace_save_tramp_hashes();
2277 	FTRACE_WARN_ON(ret);
2278 }
2279 
2280 static ftrace_func_t saved_ftrace_func;
2281 static int ftrace_start_up;
2282 
2283 static void control_ops_free(struct ftrace_ops *ops)
2284 {
2285 	free_percpu(ops->disabled);
2286 }
2287 
2288 static void ftrace_startup_enable(int command)
2289 {
2290 	if (saved_ftrace_func != ftrace_trace_function) {
2291 		saved_ftrace_func = ftrace_trace_function;
2292 		command |= FTRACE_UPDATE_TRACE_FUNC;
2293 	}
2294 
2295 	if (!command || !ftrace_enabled)
2296 		return;
2297 
2298 	ftrace_run_update_code(command);
2299 }
2300 
2301 static int ftrace_startup(struct ftrace_ops *ops, int command)
2302 {
2303 	int ret;
2304 
2305 	if (unlikely(ftrace_disabled))
2306 		return -ENODEV;
2307 
2308 	ret = __register_ftrace_function(ops);
2309 	if (ret)
2310 		return ret;
2311 
2312 	ftrace_start_up++;
2313 	command |= FTRACE_UPDATE_CALLS;
2314 
2315 	ops->flags |= FTRACE_OPS_FL_ENABLED;
2316 
2317 	ftrace_hash_rec_enable(ops, 1);
2318 
2319 	ftrace_startup_enable(command);
2320 
2321 	return 0;
2322 }
2323 
2324 static int ftrace_shutdown(struct ftrace_ops *ops, int command)
2325 {
2326 	int ret;
2327 
2328 	if (unlikely(ftrace_disabled))
2329 		return -ENODEV;
2330 
2331 	ret = __unregister_ftrace_function(ops);
2332 	if (ret)
2333 		return ret;
2334 
2335 	ftrace_start_up--;
2336 	/*
2337 	 * Just warn in case of unbalance, no need to kill ftrace, it's not
2338 	 * critical but the ftrace_call callers may be never nopped again after
2339 	 * further ftrace uses.
2340 	 */
2341 	WARN_ON_ONCE(ftrace_start_up < 0);
2342 
2343 	ftrace_hash_rec_disable(ops, 1);
2344 
2345 	ops->flags &= ~FTRACE_OPS_FL_ENABLED;
2346 
2347 	command |= FTRACE_UPDATE_CALLS;
2348 
2349 	if (saved_ftrace_func != ftrace_trace_function) {
2350 		saved_ftrace_func = ftrace_trace_function;
2351 		command |= FTRACE_UPDATE_TRACE_FUNC;
2352 	}
2353 
2354 	if (!command || !ftrace_enabled) {
2355 		/*
2356 		 * If these are control ops, they still need their
2357 		 * per_cpu field freed. Since, function tracing is
2358 		 * not currently active, we can just free them
2359 		 * without synchronizing all CPUs.
2360 		 */
2361 		if (ops->flags & FTRACE_OPS_FL_CONTROL)
2362 			control_ops_free(ops);
2363 		return 0;
2364 	}
2365 
2366 	/*
2367 	 * If the ops uses a trampoline, then it needs to be
2368 	 * tested first on update.
2369 	 */
2370 	removed_ops = ops;
2371 
2372 	ftrace_run_update_code(command);
2373 
2374 	removed_ops = NULL;
2375 
2376 	/*
2377 	 * Dynamic ops may be freed, we must make sure that all
2378 	 * callers are done before leaving this function.
2379 	 * The same goes for freeing the per_cpu data of the control
2380 	 * ops.
2381 	 *
2382 	 * Again, normal synchronize_sched() is not good enough.
2383 	 * We need to do a hard force of sched synchronization.
2384 	 * This is because we use preempt_disable() to do RCU, but
2385 	 * the function tracers can be called where RCU is not watching
2386 	 * (like before user_exit()). We can not rely on the RCU
2387 	 * infrastructure to do the synchronization, thus we must do it
2388 	 * ourselves.
2389 	 */
2390 	if (ops->flags & (FTRACE_OPS_FL_DYNAMIC | FTRACE_OPS_FL_CONTROL)) {
2391 		schedule_on_each_cpu(ftrace_sync);
2392 
2393 		if (ops->flags & FTRACE_OPS_FL_CONTROL)
2394 			control_ops_free(ops);
2395 	}
2396 
2397 	return 0;
2398 }
2399 
2400 static void ftrace_startup_sysctl(void)
2401 {
2402 	if (unlikely(ftrace_disabled))
2403 		return;
2404 
2405 	/* Force update next time */
2406 	saved_ftrace_func = NULL;
2407 	/* ftrace_start_up is true if we want ftrace running */
2408 	if (ftrace_start_up)
2409 		ftrace_run_update_code(FTRACE_UPDATE_CALLS);
2410 }
2411 
2412 static void ftrace_shutdown_sysctl(void)
2413 {
2414 	if (unlikely(ftrace_disabled))
2415 		return;
2416 
2417 	/* ftrace_start_up is true if ftrace is running */
2418 	if (ftrace_start_up)
2419 		ftrace_run_update_code(FTRACE_DISABLE_CALLS);
2420 }
2421 
2422 static cycle_t		ftrace_update_time;
2423 unsigned long		ftrace_update_tot_cnt;
2424 
2425 static inline int ops_traces_mod(struct ftrace_ops *ops)
2426 {
2427 	/*
2428 	 * Filter_hash being empty will default to trace module.
2429 	 * But notrace hash requires a test of individual module functions.
2430 	 */
2431 	return ftrace_hash_empty(ops->filter_hash) &&
2432 		ftrace_hash_empty(ops->notrace_hash);
2433 }
2434 
2435 /*
2436  * Check if the current ops references the record.
2437  *
2438  * If the ops traces all functions, then it was already accounted for.
2439  * If the ops does not trace the current record function, skip it.
2440  * If the ops ignores the function via notrace filter, skip it.
2441  */
2442 static inline bool
2443 ops_references_rec(struct ftrace_ops *ops, struct dyn_ftrace *rec)
2444 {
2445 	/* If ops isn't enabled, ignore it */
2446 	if (!(ops->flags & FTRACE_OPS_FL_ENABLED))
2447 		return 0;
2448 
2449 	/* If ops traces all mods, we already accounted for it */
2450 	if (ops_traces_mod(ops))
2451 		return 0;
2452 
2453 	/* The function must be in the filter */
2454 	if (!ftrace_hash_empty(ops->filter_hash) &&
2455 	    !ftrace_lookup_ip(ops->filter_hash, rec->ip))
2456 		return 0;
2457 
2458 	/* If in notrace hash, we ignore it too */
2459 	if (ftrace_lookup_ip(ops->notrace_hash, rec->ip))
2460 		return 0;
2461 
2462 	return 1;
2463 }
2464 
2465 static int referenced_filters(struct dyn_ftrace *rec)
2466 {
2467 	struct ftrace_ops *ops;
2468 	int cnt = 0;
2469 
2470 	for (ops = ftrace_ops_list; ops != &ftrace_list_end; ops = ops->next) {
2471 		if (ops_references_rec(ops, rec))
2472 		    cnt++;
2473 	}
2474 
2475 	return cnt;
2476 }
2477 
2478 static int ftrace_update_code(struct module *mod, struct ftrace_page *new_pgs)
2479 {
2480 	struct ftrace_page *pg;
2481 	struct dyn_ftrace *p;
2482 	cycle_t start, stop;
2483 	unsigned long update_cnt = 0;
2484 	unsigned long ref = 0;
2485 	bool test = false;
2486 	int i;
2487 
2488 	/*
2489 	 * When adding a module, we need to check if tracers are
2490 	 * currently enabled and if they are set to trace all functions.
2491 	 * If they are, we need to enable the module functions as well
2492 	 * as update the reference counts for those function records.
2493 	 */
2494 	if (mod) {
2495 		struct ftrace_ops *ops;
2496 
2497 		for (ops = ftrace_ops_list;
2498 		     ops != &ftrace_list_end; ops = ops->next) {
2499 			if (ops->flags & FTRACE_OPS_FL_ENABLED) {
2500 				if (ops_traces_mod(ops))
2501 					ref++;
2502 				else
2503 					test = true;
2504 			}
2505 		}
2506 	}
2507 
2508 	start = ftrace_now(raw_smp_processor_id());
2509 
2510 	for (pg = new_pgs; pg; pg = pg->next) {
2511 
2512 		for (i = 0; i < pg->index; i++) {
2513 			int cnt = ref;
2514 
2515 			/* If something went wrong, bail without enabling anything */
2516 			if (unlikely(ftrace_disabled))
2517 				return -1;
2518 
2519 			p = &pg->records[i];
2520 			if (test)
2521 				cnt += referenced_filters(p);
2522 			p->flags = cnt;
2523 
2524 			/*
2525 			 * Do the initial record conversion from mcount jump
2526 			 * to the NOP instructions.
2527 			 */
2528 			if (!ftrace_code_disable(mod, p))
2529 				break;
2530 
2531 			update_cnt++;
2532 
2533 			/*
2534 			 * If the tracing is enabled, go ahead and enable the record.
2535 			 *
2536 			 * The reason not to enable the record immediatelly is the
2537 			 * inherent check of ftrace_make_nop/ftrace_make_call for
2538 			 * correct previous instructions.  Making first the NOP
2539 			 * conversion puts the module to the correct state, thus
2540 			 * passing the ftrace_make_call check.
2541 			 */
2542 			if (ftrace_start_up && cnt) {
2543 				int failed = __ftrace_replace_code(p, 1);
2544 				if (failed)
2545 					ftrace_bug(failed, p->ip);
2546 			}
2547 		}
2548 	}
2549 
2550 	stop = ftrace_now(raw_smp_processor_id());
2551 	ftrace_update_time = stop - start;
2552 	ftrace_update_tot_cnt += update_cnt;
2553 
2554 	return 0;
2555 }
2556 
2557 static int ftrace_allocate_records(struct ftrace_page *pg, int count)
2558 {
2559 	int order;
2560 	int cnt;
2561 
2562 	if (WARN_ON(!count))
2563 		return -EINVAL;
2564 
2565 	order = get_count_order(DIV_ROUND_UP(count, ENTRIES_PER_PAGE));
2566 
2567 	/*
2568 	 * We want to fill as much as possible. No more than a page
2569 	 * may be empty.
2570 	 */
2571 	while ((PAGE_SIZE << order) / ENTRY_SIZE >= count + ENTRIES_PER_PAGE)
2572 		order--;
2573 
2574  again:
2575 	pg->records = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, order);
2576 
2577 	if (!pg->records) {
2578 		/* if we can't allocate this size, try something smaller */
2579 		if (!order)
2580 			return -ENOMEM;
2581 		order >>= 1;
2582 		goto again;
2583 	}
2584 
2585 	cnt = (PAGE_SIZE << order) / ENTRY_SIZE;
2586 	pg->size = cnt;
2587 
2588 	if (cnt > count)
2589 		cnt = count;
2590 
2591 	return cnt;
2592 }
2593 
2594 static struct ftrace_page *
2595 ftrace_allocate_pages(unsigned long num_to_init)
2596 {
2597 	struct ftrace_page *start_pg;
2598 	struct ftrace_page *pg;
2599 	int order;
2600 	int cnt;
2601 
2602 	if (!num_to_init)
2603 		return 0;
2604 
2605 	start_pg = pg = kzalloc(sizeof(*pg), GFP_KERNEL);
2606 	if (!pg)
2607 		return NULL;
2608 
2609 	/*
2610 	 * Try to allocate as much as possible in one continues
2611 	 * location that fills in all of the space. We want to
2612 	 * waste as little space as possible.
2613 	 */
2614 	for (;;) {
2615 		cnt = ftrace_allocate_records(pg, num_to_init);
2616 		if (cnt < 0)
2617 			goto free_pages;
2618 
2619 		num_to_init -= cnt;
2620 		if (!num_to_init)
2621 			break;
2622 
2623 		pg->next = kzalloc(sizeof(*pg), GFP_KERNEL);
2624 		if (!pg->next)
2625 			goto free_pages;
2626 
2627 		pg = pg->next;
2628 	}
2629 
2630 	return start_pg;
2631 
2632  free_pages:
2633 	pg = start_pg;
2634 	while (pg) {
2635 		order = get_count_order(pg->size / ENTRIES_PER_PAGE);
2636 		free_pages((unsigned long)pg->records, order);
2637 		start_pg = pg->next;
2638 		kfree(pg);
2639 		pg = start_pg;
2640 	}
2641 	pr_info("ftrace: FAILED to allocate memory for functions\n");
2642 	return NULL;
2643 }
2644 
2645 #define FTRACE_BUFF_MAX (KSYM_SYMBOL_LEN+4) /* room for wildcards */
2646 
2647 struct ftrace_iterator {
2648 	loff_t				pos;
2649 	loff_t				func_pos;
2650 	struct ftrace_page		*pg;
2651 	struct dyn_ftrace		*func;
2652 	struct ftrace_func_probe	*probe;
2653 	struct trace_parser		parser;
2654 	struct ftrace_hash		*hash;
2655 	struct ftrace_ops		*ops;
2656 	int				hidx;
2657 	int				idx;
2658 	unsigned			flags;
2659 };
2660 
2661 static void *
2662 t_hash_next(struct seq_file *m, loff_t *pos)
2663 {
2664 	struct ftrace_iterator *iter = m->private;
2665 	struct hlist_node *hnd = NULL;
2666 	struct hlist_head *hhd;
2667 
2668 	(*pos)++;
2669 	iter->pos = *pos;
2670 
2671 	if (iter->probe)
2672 		hnd = &iter->probe->node;
2673  retry:
2674 	if (iter->hidx >= FTRACE_FUNC_HASHSIZE)
2675 		return NULL;
2676 
2677 	hhd = &ftrace_func_hash[iter->hidx];
2678 
2679 	if (hlist_empty(hhd)) {
2680 		iter->hidx++;
2681 		hnd = NULL;
2682 		goto retry;
2683 	}
2684 
2685 	if (!hnd)
2686 		hnd = hhd->first;
2687 	else {
2688 		hnd = hnd->next;
2689 		if (!hnd) {
2690 			iter->hidx++;
2691 			goto retry;
2692 		}
2693 	}
2694 
2695 	if (WARN_ON_ONCE(!hnd))
2696 		return NULL;
2697 
2698 	iter->probe = hlist_entry(hnd, struct ftrace_func_probe, node);
2699 
2700 	return iter;
2701 }
2702 
2703 static void *t_hash_start(struct seq_file *m, loff_t *pos)
2704 {
2705 	struct ftrace_iterator *iter = m->private;
2706 	void *p = NULL;
2707 	loff_t l;
2708 
2709 	if (!(iter->flags & FTRACE_ITER_DO_HASH))
2710 		return NULL;
2711 
2712 	if (iter->func_pos > *pos)
2713 		return NULL;
2714 
2715 	iter->hidx = 0;
2716 	for (l = 0; l <= (*pos - iter->func_pos); ) {
2717 		p = t_hash_next(m, &l);
2718 		if (!p)
2719 			break;
2720 	}
2721 	if (!p)
2722 		return NULL;
2723 
2724 	/* Only set this if we have an item */
2725 	iter->flags |= FTRACE_ITER_HASH;
2726 
2727 	return iter;
2728 }
2729 
2730 static int
2731 t_hash_show(struct seq_file *m, struct ftrace_iterator *iter)
2732 {
2733 	struct ftrace_func_probe *rec;
2734 
2735 	rec = iter->probe;
2736 	if (WARN_ON_ONCE(!rec))
2737 		return -EIO;
2738 
2739 	if (rec->ops->print)
2740 		return rec->ops->print(m, rec->ip, rec->ops, rec->data);
2741 
2742 	seq_printf(m, "%ps:%ps", (void *)rec->ip, (void *)rec->ops->func);
2743 
2744 	if (rec->data)
2745 		seq_printf(m, ":%p", rec->data);
2746 	seq_putc(m, '\n');
2747 
2748 	return 0;
2749 }
2750 
2751 static void *
2752 t_next(struct seq_file *m, void *v, loff_t *pos)
2753 {
2754 	struct ftrace_iterator *iter = m->private;
2755 	struct ftrace_ops *ops = iter->ops;
2756 	struct dyn_ftrace *rec = NULL;
2757 
2758 	if (unlikely(ftrace_disabled))
2759 		return NULL;
2760 
2761 	if (iter->flags & FTRACE_ITER_HASH)
2762 		return t_hash_next(m, pos);
2763 
2764 	(*pos)++;
2765 	iter->pos = iter->func_pos = *pos;
2766 
2767 	if (iter->flags & FTRACE_ITER_PRINTALL)
2768 		return t_hash_start(m, pos);
2769 
2770  retry:
2771 	if (iter->idx >= iter->pg->index) {
2772 		if (iter->pg->next) {
2773 			iter->pg = iter->pg->next;
2774 			iter->idx = 0;
2775 			goto retry;
2776 		}
2777 	} else {
2778 		rec = &iter->pg->records[iter->idx++];
2779 		if (((iter->flags & FTRACE_ITER_FILTER) &&
2780 		     !(ftrace_lookup_ip(ops->filter_hash, rec->ip))) ||
2781 
2782 		    ((iter->flags & FTRACE_ITER_NOTRACE) &&
2783 		     !ftrace_lookup_ip(ops->notrace_hash, rec->ip)) ||
2784 
2785 		    ((iter->flags & FTRACE_ITER_ENABLED) &&
2786 		     !(rec->flags & FTRACE_FL_ENABLED))) {
2787 
2788 			rec = NULL;
2789 			goto retry;
2790 		}
2791 	}
2792 
2793 	if (!rec)
2794 		return t_hash_start(m, pos);
2795 
2796 	iter->func = rec;
2797 
2798 	return iter;
2799 }
2800 
2801 static void reset_iter_read(struct ftrace_iterator *iter)
2802 {
2803 	iter->pos = 0;
2804 	iter->func_pos = 0;
2805 	iter->flags &= ~(FTRACE_ITER_PRINTALL | FTRACE_ITER_HASH);
2806 }
2807 
2808 static void *t_start(struct seq_file *m, loff_t *pos)
2809 {
2810 	struct ftrace_iterator *iter = m->private;
2811 	struct ftrace_ops *ops = iter->ops;
2812 	void *p = NULL;
2813 	loff_t l;
2814 
2815 	mutex_lock(&ftrace_lock);
2816 
2817 	if (unlikely(ftrace_disabled))
2818 		return NULL;
2819 
2820 	/*
2821 	 * If an lseek was done, then reset and start from beginning.
2822 	 */
2823 	if (*pos < iter->pos)
2824 		reset_iter_read(iter);
2825 
2826 	/*
2827 	 * For set_ftrace_filter reading, if we have the filter
2828 	 * off, we can short cut and just print out that all
2829 	 * functions are enabled.
2830 	 */
2831 	if ((iter->flags & FTRACE_ITER_FILTER &&
2832 	     ftrace_hash_empty(ops->filter_hash)) ||
2833 	    (iter->flags & FTRACE_ITER_NOTRACE &&
2834 	     ftrace_hash_empty(ops->notrace_hash))) {
2835 		if (*pos > 0)
2836 			return t_hash_start(m, pos);
2837 		iter->flags |= FTRACE_ITER_PRINTALL;
2838 		/* reset in case of seek/pread */
2839 		iter->flags &= ~FTRACE_ITER_HASH;
2840 		return iter;
2841 	}
2842 
2843 	if (iter->flags & FTRACE_ITER_HASH)
2844 		return t_hash_start(m, pos);
2845 
2846 	/*
2847 	 * Unfortunately, we need to restart at ftrace_pages_start
2848 	 * every time we let go of the ftrace_mutex. This is because
2849 	 * those pointers can change without the lock.
2850 	 */
2851 	iter->pg = ftrace_pages_start;
2852 	iter->idx = 0;
2853 	for (l = 0; l <= *pos; ) {
2854 		p = t_next(m, p, &l);
2855 		if (!p)
2856 			break;
2857 	}
2858 
2859 	if (!p)
2860 		return t_hash_start(m, pos);
2861 
2862 	return iter;
2863 }
2864 
2865 static void t_stop(struct seq_file *m, void *p)
2866 {
2867 	mutex_unlock(&ftrace_lock);
2868 }
2869 
2870 static int t_show(struct seq_file *m, void *v)
2871 {
2872 	struct ftrace_iterator *iter = m->private;
2873 	struct dyn_ftrace *rec;
2874 
2875 	if (iter->flags & FTRACE_ITER_HASH)
2876 		return t_hash_show(m, iter);
2877 
2878 	if (iter->flags & FTRACE_ITER_PRINTALL) {
2879 		if (iter->flags & FTRACE_ITER_NOTRACE)
2880 			seq_printf(m, "#### no functions disabled ####\n");
2881 		else
2882 			seq_printf(m, "#### all functions enabled ####\n");
2883 		return 0;
2884 	}
2885 
2886 	rec = iter->func;
2887 
2888 	if (!rec)
2889 		return 0;
2890 
2891 	seq_printf(m, "%ps", (void *)rec->ip);
2892 	if (iter->flags & FTRACE_ITER_ENABLED) {
2893 		seq_printf(m, " (%ld)%s",
2894 			   ftrace_rec_count(rec),
2895 			   rec->flags & FTRACE_FL_REGS ? " R" : "  ");
2896 		if (rec->flags & FTRACE_FL_TRAMP_EN) {
2897 			struct ftrace_ops *ops;
2898 
2899 			ops = ftrace_find_tramp_ops_curr(rec);
2900 			if (ops && ops->trampoline)
2901 				seq_printf(m, "\ttramp: %pS",
2902 					   (void *)ops->trampoline);
2903 			else
2904 				seq_printf(m, "\ttramp: ERROR!");
2905 		}
2906 	}
2907 
2908 	seq_printf(m, "\n");
2909 
2910 	return 0;
2911 }
2912 
2913 static const struct seq_operations show_ftrace_seq_ops = {
2914 	.start = t_start,
2915 	.next = t_next,
2916 	.stop = t_stop,
2917 	.show = t_show,
2918 };
2919 
2920 static int
2921 ftrace_avail_open(struct inode *inode, struct file *file)
2922 {
2923 	struct ftrace_iterator *iter;
2924 
2925 	if (unlikely(ftrace_disabled))
2926 		return -ENODEV;
2927 
2928 	iter = __seq_open_private(file, &show_ftrace_seq_ops, sizeof(*iter));
2929 	if (iter) {
2930 		iter->pg = ftrace_pages_start;
2931 		iter->ops = &global_ops;
2932 	}
2933 
2934 	return iter ? 0 : -ENOMEM;
2935 }
2936 
2937 static int
2938 ftrace_enabled_open(struct inode *inode, struct file *file)
2939 {
2940 	struct ftrace_iterator *iter;
2941 
2942 	if (unlikely(ftrace_disabled))
2943 		return -ENODEV;
2944 
2945 	iter = __seq_open_private(file, &show_ftrace_seq_ops, sizeof(*iter));
2946 	if (iter) {
2947 		iter->pg = ftrace_pages_start;
2948 		iter->flags = FTRACE_ITER_ENABLED;
2949 		iter->ops = &global_ops;
2950 	}
2951 
2952 	return iter ? 0 : -ENOMEM;
2953 }
2954 
2955 static void ftrace_filter_reset(struct ftrace_hash *hash)
2956 {
2957 	mutex_lock(&ftrace_lock);
2958 	ftrace_hash_clear(hash);
2959 	mutex_unlock(&ftrace_lock);
2960 }
2961 
2962 /**
2963  * ftrace_regex_open - initialize function tracer filter files
2964  * @ops: The ftrace_ops that hold the hash filters
2965  * @flag: The type of filter to process
2966  * @inode: The inode, usually passed in to your open routine
2967  * @file: The file, usually passed in to your open routine
2968  *
2969  * ftrace_regex_open() initializes the filter files for the
2970  * @ops. Depending on @flag it may process the filter hash or
2971  * the notrace hash of @ops. With this called from the open
2972  * routine, you can use ftrace_filter_write() for the write
2973  * routine if @flag has FTRACE_ITER_FILTER set, or
2974  * ftrace_notrace_write() if @flag has FTRACE_ITER_NOTRACE set.
2975  * tracing_lseek() should be used as the lseek routine, and
2976  * release must call ftrace_regex_release().
2977  */
2978 int
2979 ftrace_regex_open(struct ftrace_ops *ops, int flag,
2980 		  struct inode *inode, struct file *file)
2981 {
2982 	struct ftrace_iterator *iter;
2983 	struct ftrace_hash *hash;
2984 	int ret = 0;
2985 
2986 	ftrace_ops_init(ops);
2987 
2988 	if (unlikely(ftrace_disabled))
2989 		return -ENODEV;
2990 
2991 	iter = kzalloc(sizeof(*iter), GFP_KERNEL);
2992 	if (!iter)
2993 		return -ENOMEM;
2994 
2995 	if (trace_parser_get_init(&iter->parser, FTRACE_BUFF_MAX)) {
2996 		kfree(iter);
2997 		return -ENOMEM;
2998 	}
2999 
3000 	iter->ops = ops;
3001 	iter->flags = flag;
3002 
3003 	mutex_lock(&ops->regex_lock);
3004 
3005 	if (flag & FTRACE_ITER_NOTRACE)
3006 		hash = ops->notrace_hash;
3007 	else
3008 		hash = ops->filter_hash;
3009 
3010 	if (file->f_mode & FMODE_WRITE) {
3011 		const int size_bits = FTRACE_HASH_DEFAULT_BITS;
3012 
3013 		if (file->f_flags & O_TRUNC)
3014 			iter->hash = alloc_ftrace_hash(size_bits);
3015 		else
3016 			iter->hash = alloc_and_copy_ftrace_hash(size_bits, hash);
3017 
3018 		if (!iter->hash) {
3019 			trace_parser_put(&iter->parser);
3020 			kfree(iter);
3021 			ret = -ENOMEM;
3022 			goto out_unlock;
3023 		}
3024 	}
3025 
3026 	if (file->f_mode & FMODE_READ) {
3027 		iter->pg = ftrace_pages_start;
3028 
3029 		ret = seq_open(file, &show_ftrace_seq_ops);
3030 		if (!ret) {
3031 			struct seq_file *m = file->private_data;
3032 			m->private = iter;
3033 		} else {
3034 			/* Failed */
3035 			free_ftrace_hash(iter->hash);
3036 			trace_parser_put(&iter->parser);
3037 			kfree(iter);
3038 		}
3039 	} else
3040 		file->private_data = iter;
3041 
3042  out_unlock:
3043 	mutex_unlock(&ops->regex_lock);
3044 
3045 	return ret;
3046 }
3047 
3048 static int
3049 ftrace_filter_open(struct inode *inode, struct file *file)
3050 {
3051 	struct ftrace_ops *ops = inode->i_private;
3052 
3053 	return ftrace_regex_open(ops,
3054 			FTRACE_ITER_FILTER | FTRACE_ITER_DO_HASH,
3055 			inode, file);
3056 }
3057 
3058 static int
3059 ftrace_notrace_open(struct inode *inode, struct file *file)
3060 {
3061 	struct ftrace_ops *ops = inode->i_private;
3062 
3063 	return ftrace_regex_open(ops, FTRACE_ITER_NOTRACE,
3064 				 inode, file);
3065 }
3066 
3067 static int ftrace_match(char *str, char *regex, int len, int type)
3068 {
3069 	int matched = 0;
3070 	int slen;
3071 
3072 	switch (type) {
3073 	case MATCH_FULL:
3074 		if (strcmp(str, regex) == 0)
3075 			matched = 1;
3076 		break;
3077 	case MATCH_FRONT_ONLY:
3078 		if (strncmp(str, regex, len) == 0)
3079 			matched = 1;
3080 		break;
3081 	case MATCH_MIDDLE_ONLY:
3082 		if (strstr(str, regex))
3083 			matched = 1;
3084 		break;
3085 	case MATCH_END_ONLY:
3086 		slen = strlen(str);
3087 		if (slen >= len && memcmp(str + slen - len, regex, len) == 0)
3088 			matched = 1;
3089 		break;
3090 	}
3091 
3092 	return matched;
3093 }
3094 
3095 static int
3096 enter_record(struct ftrace_hash *hash, struct dyn_ftrace *rec, int not)
3097 {
3098 	struct ftrace_func_entry *entry;
3099 	int ret = 0;
3100 
3101 	entry = ftrace_lookup_ip(hash, rec->ip);
3102 	if (not) {
3103 		/* Do nothing if it doesn't exist */
3104 		if (!entry)
3105 			return 0;
3106 
3107 		free_hash_entry(hash, entry);
3108 	} else {
3109 		/* Do nothing if it exists */
3110 		if (entry)
3111 			return 0;
3112 
3113 		ret = add_hash_entry(hash, rec->ip);
3114 	}
3115 	return ret;
3116 }
3117 
3118 static int
3119 ftrace_match_record(struct dyn_ftrace *rec, char *mod,
3120 		    char *regex, int len, int type)
3121 {
3122 	char str[KSYM_SYMBOL_LEN];
3123 	char *modname;
3124 
3125 	kallsyms_lookup(rec->ip, NULL, NULL, &modname, str);
3126 
3127 	if (mod) {
3128 		/* module lookup requires matching the module */
3129 		if (!modname || strcmp(modname, mod))
3130 			return 0;
3131 
3132 		/* blank search means to match all funcs in the mod */
3133 		if (!len)
3134 			return 1;
3135 	}
3136 
3137 	return ftrace_match(str, regex, len, type);
3138 }
3139 
3140 static int
3141 match_records(struct ftrace_hash *hash, char *buff,
3142 	      int len, char *mod, int not)
3143 {
3144 	unsigned search_len = 0;
3145 	struct ftrace_page *pg;
3146 	struct dyn_ftrace *rec;
3147 	int type = MATCH_FULL;
3148 	char *search = buff;
3149 	int found = 0;
3150 	int ret;
3151 
3152 	if (len) {
3153 		type = filter_parse_regex(buff, len, &search, &not);
3154 		search_len = strlen(search);
3155 	}
3156 
3157 	mutex_lock(&ftrace_lock);
3158 
3159 	if (unlikely(ftrace_disabled))
3160 		goto out_unlock;
3161 
3162 	do_for_each_ftrace_rec(pg, rec) {
3163 		if (ftrace_match_record(rec, mod, search, search_len, type)) {
3164 			ret = enter_record(hash, rec, not);
3165 			if (ret < 0) {
3166 				found = ret;
3167 				goto out_unlock;
3168 			}
3169 			found = 1;
3170 		}
3171 	} while_for_each_ftrace_rec();
3172  out_unlock:
3173 	mutex_unlock(&ftrace_lock);
3174 
3175 	return found;
3176 }
3177 
3178 static int
3179 ftrace_match_records(struct ftrace_hash *hash, char *buff, int len)
3180 {
3181 	return match_records(hash, buff, len, NULL, 0);
3182 }
3183 
3184 static int
3185 ftrace_match_module_records(struct ftrace_hash *hash, char *buff, char *mod)
3186 {
3187 	int not = 0;
3188 
3189 	/* blank or '*' mean the same */
3190 	if (strcmp(buff, "*") == 0)
3191 		buff[0] = 0;
3192 
3193 	/* handle the case of 'dont filter this module' */
3194 	if (strcmp(buff, "!") == 0 || strcmp(buff, "!*") == 0) {
3195 		buff[0] = 0;
3196 		not = 1;
3197 	}
3198 
3199 	return match_records(hash, buff, strlen(buff), mod, not);
3200 }
3201 
3202 /*
3203  * We register the module command as a template to show others how
3204  * to register the a command as well.
3205  */
3206 
3207 static int
3208 ftrace_mod_callback(struct ftrace_hash *hash,
3209 		    char *func, char *cmd, char *param, int enable)
3210 {
3211 	char *mod;
3212 	int ret = -EINVAL;
3213 
3214 	/*
3215 	 * cmd == 'mod' because we only registered this func
3216 	 * for the 'mod' ftrace_func_command.
3217 	 * But if you register one func with multiple commands,
3218 	 * you can tell which command was used by the cmd
3219 	 * parameter.
3220 	 */
3221 
3222 	/* we must have a module name */
3223 	if (!param)
3224 		return ret;
3225 
3226 	mod = strsep(&param, ":");
3227 	if (!strlen(mod))
3228 		return ret;
3229 
3230 	ret = ftrace_match_module_records(hash, func, mod);
3231 	if (!ret)
3232 		ret = -EINVAL;
3233 	if (ret < 0)
3234 		return ret;
3235 
3236 	return 0;
3237 }
3238 
3239 static struct ftrace_func_command ftrace_mod_cmd = {
3240 	.name			= "mod",
3241 	.func			= ftrace_mod_callback,
3242 };
3243 
3244 static int __init ftrace_mod_cmd_init(void)
3245 {
3246 	return register_ftrace_command(&ftrace_mod_cmd);
3247 }
3248 core_initcall(ftrace_mod_cmd_init);
3249 
3250 static void function_trace_probe_call(unsigned long ip, unsigned long parent_ip,
3251 				      struct ftrace_ops *op, struct pt_regs *pt_regs)
3252 {
3253 	struct ftrace_func_probe *entry;
3254 	struct hlist_head *hhd;
3255 	unsigned long key;
3256 
3257 	key = hash_long(ip, FTRACE_HASH_BITS);
3258 
3259 	hhd = &ftrace_func_hash[key];
3260 
3261 	if (hlist_empty(hhd))
3262 		return;
3263 
3264 	/*
3265 	 * Disable preemption for these calls to prevent a RCU grace
3266 	 * period. This syncs the hash iteration and freeing of items
3267 	 * on the hash. rcu_read_lock is too dangerous here.
3268 	 */
3269 	preempt_disable_notrace();
3270 	hlist_for_each_entry_rcu_notrace(entry, hhd, node) {
3271 		if (entry->ip == ip)
3272 			entry->ops->func(ip, parent_ip, &entry->data);
3273 	}
3274 	preempt_enable_notrace();
3275 }
3276 
3277 static struct ftrace_ops trace_probe_ops __read_mostly =
3278 {
3279 	.func		= function_trace_probe_call,
3280 	.flags		= FTRACE_OPS_FL_INITIALIZED,
3281 	INIT_REGEX_LOCK(trace_probe_ops)
3282 };
3283 
3284 static int ftrace_probe_registered;
3285 
3286 static void __enable_ftrace_function_probe(void)
3287 {
3288 	int ret;
3289 	int i;
3290 
3291 	if (ftrace_probe_registered) {
3292 		/* still need to update the function call sites */
3293 		if (ftrace_enabled)
3294 			ftrace_run_update_code(FTRACE_UPDATE_CALLS);
3295 		return;
3296 	}
3297 
3298 	for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
3299 		struct hlist_head *hhd = &ftrace_func_hash[i];
3300 		if (hhd->first)
3301 			break;
3302 	}
3303 	/* Nothing registered? */
3304 	if (i == FTRACE_FUNC_HASHSIZE)
3305 		return;
3306 
3307 	ret = ftrace_startup(&trace_probe_ops, 0);
3308 
3309 	ftrace_probe_registered = 1;
3310 }
3311 
3312 static void __disable_ftrace_function_probe(void)
3313 {
3314 	int i;
3315 
3316 	if (!ftrace_probe_registered)
3317 		return;
3318 
3319 	for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
3320 		struct hlist_head *hhd = &ftrace_func_hash[i];
3321 		if (hhd->first)
3322 			return;
3323 	}
3324 
3325 	/* no more funcs left */
3326 	ftrace_shutdown(&trace_probe_ops, 0);
3327 
3328 	ftrace_probe_registered = 0;
3329 }
3330 
3331 
3332 static void ftrace_free_entry(struct ftrace_func_probe *entry)
3333 {
3334 	if (entry->ops->free)
3335 		entry->ops->free(entry->ops, entry->ip, &entry->data);
3336 	kfree(entry);
3337 }
3338 
3339 int
3340 register_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
3341 			      void *data)
3342 {
3343 	struct ftrace_func_probe *entry;
3344 	struct ftrace_hash **orig_hash = &trace_probe_ops.filter_hash;
3345 	struct ftrace_hash *hash;
3346 	struct ftrace_page *pg;
3347 	struct dyn_ftrace *rec;
3348 	int type, len, not;
3349 	unsigned long key;
3350 	int count = 0;
3351 	char *search;
3352 	int ret;
3353 
3354 	type = filter_parse_regex(glob, strlen(glob), &search, &not);
3355 	len = strlen(search);
3356 
3357 	/* we do not support '!' for function probes */
3358 	if (WARN_ON(not))
3359 		return -EINVAL;
3360 
3361 	mutex_lock(&trace_probe_ops.regex_lock);
3362 
3363 	hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, *orig_hash);
3364 	if (!hash) {
3365 		count = -ENOMEM;
3366 		goto out;
3367 	}
3368 
3369 	if (unlikely(ftrace_disabled)) {
3370 		count = -ENODEV;
3371 		goto out;
3372 	}
3373 
3374 	mutex_lock(&ftrace_lock);
3375 
3376 	do_for_each_ftrace_rec(pg, rec) {
3377 
3378 		if (!ftrace_match_record(rec, NULL, search, len, type))
3379 			continue;
3380 
3381 		entry = kmalloc(sizeof(*entry), GFP_KERNEL);
3382 		if (!entry) {
3383 			/* If we did not process any, then return error */
3384 			if (!count)
3385 				count = -ENOMEM;
3386 			goto out_unlock;
3387 		}
3388 
3389 		count++;
3390 
3391 		entry->data = data;
3392 
3393 		/*
3394 		 * The caller might want to do something special
3395 		 * for each function we find. We call the callback
3396 		 * to give the caller an opportunity to do so.
3397 		 */
3398 		if (ops->init) {
3399 			if (ops->init(ops, rec->ip, &entry->data) < 0) {
3400 				/* caller does not like this func */
3401 				kfree(entry);
3402 				continue;
3403 			}
3404 		}
3405 
3406 		ret = enter_record(hash, rec, 0);
3407 		if (ret < 0) {
3408 			kfree(entry);
3409 			count = ret;
3410 			goto out_unlock;
3411 		}
3412 
3413 		entry->ops = ops;
3414 		entry->ip = rec->ip;
3415 
3416 		key = hash_long(entry->ip, FTRACE_HASH_BITS);
3417 		hlist_add_head_rcu(&entry->node, &ftrace_func_hash[key]);
3418 
3419 	} while_for_each_ftrace_rec();
3420 
3421 	ret = ftrace_hash_move(&trace_probe_ops, 1, orig_hash, hash);
3422 	if (ret < 0)
3423 		count = ret;
3424 
3425 	__enable_ftrace_function_probe();
3426 
3427  out_unlock:
3428 	mutex_unlock(&ftrace_lock);
3429  out:
3430 	mutex_unlock(&trace_probe_ops.regex_lock);
3431 	free_ftrace_hash(hash);
3432 
3433 	return count;
3434 }
3435 
3436 enum {
3437 	PROBE_TEST_FUNC		= 1,
3438 	PROBE_TEST_DATA		= 2
3439 };
3440 
3441 static void
3442 __unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
3443 				  void *data, int flags)
3444 {
3445 	struct ftrace_func_entry *rec_entry;
3446 	struct ftrace_func_probe *entry;
3447 	struct ftrace_func_probe *p;
3448 	struct ftrace_hash **orig_hash = &trace_probe_ops.filter_hash;
3449 	struct list_head free_list;
3450 	struct ftrace_hash *hash;
3451 	struct hlist_node *tmp;
3452 	char str[KSYM_SYMBOL_LEN];
3453 	int type = MATCH_FULL;
3454 	int i, len = 0;
3455 	char *search;
3456 
3457 	if (glob && (strcmp(glob, "*") == 0 || !strlen(glob)))
3458 		glob = NULL;
3459 	else if (glob) {
3460 		int not;
3461 
3462 		type = filter_parse_regex(glob, strlen(glob), &search, &not);
3463 		len = strlen(search);
3464 
3465 		/* we do not support '!' for function probes */
3466 		if (WARN_ON(not))
3467 			return;
3468 	}
3469 
3470 	mutex_lock(&trace_probe_ops.regex_lock);
3471 
3472 	hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, *orig_hash);
3473 	if (!hash)
3474 		/* Hmm, should report this somehow */
3475 		goto out_unlock;
3476 
3477 	INIT_LIST_HEAD(&free_list);
3478 
3479 	for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
3480 		struct hlist_head *hhd = &ftrace_func_hash[i];
3481 
3482 		hlist_for_each_entry_safe(entry, tmp, hhd, node) {
3483 
3484 			/* break up if statements for readability */
3485 			if ((flags & PROBE_TEST_FUNC) && entry->ops != ops)
3486 				continue;
3487 
3488 			if ((flags & PROBE_TEST_DATA) && entry->data != data)
3489 				continue;
3490 
3491 			/* do this last, since it is the most expensive */
3492 			if (glob) {
3493 				kallsyms_lookup(entry->ip, NULL, NULL,
3494 						NULL, str);
3495 				if (!ftrace_match(str, glob, len, type))
3496 					continue;
3497 			}
3498 
3499 			rec_entry = ftrace_lookup_ip(hash, entry->ip);
3500 			/* It is possible more than one entry had this ip */
3501 			if (rec_entry)
3502 				free_hash_entry(hash, rec_entry);
3503 
3504 			hlist_del_rcu(&entry->node);
3505 			list_add(&entry->free_list, &free_list);
3506 		}
3507 	}
3508 	mutex_lock(&ftrace_lock);
3509 	__disable_ftrace_function_probe();
3510 	/*
3511 	 * Remove after the disable is called. Otherwise, if the last
3512 	 * probe is removed, a null hash means *all enabled*.
3513 	 */
3514 	ftrace_hash_move(&trace_probe_ops, 1, orig_hash, hash);
3515 	synchronize_sched();
3516 	list_for_each_entry_safe(entry, p, &free_list, free_list) {
3517 		list_del(&entry->free_list);
3518 		ftrace_free_entry(entry);
3519 	}
3520 	mutex_unlock(&ftrace_lock);
3521 
3522  out_unlock:
3523 	mutex_unlock(&trace_probe_ops.regex_lock);
3524 	free_ftrace_hash(hash);
3525 }
3526 
3527 void
3528 unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
3529 				void *data)
3530 {
3531 	__unregister_ftrace_function_probe(glob, ops, data,
3532 					  PROBE_TEST_FUNC | PROBE_TEST_DATA);
3533 }
3534 
3535 void
3536 unregister_ftrace_function_probe_func(char *glob, struct ftrace_probe_ops *ops)
3537 {
3538 	__unregister_ftrace_function_probe(glob, ops, NULL, PROBE_TEST_FUNC);
3539 }
3540 
3541 void unregister_ftrace_function_probe_all(char *glob)
3542 {
3543 	__unregister_ftrace_function_probe(glob, NULL, NULL, 0);
3544 }
3545 
3546 static LIST_HEAD(ftrace_commands);
3547 static DEFINE_MUTEX(ftrace_cmd_mutex);
3548 
3549 /*
3550  * Currently we only register ftrace commands from __init, so mark this
3551  * __init too.
3552  */
3553 __init int register_ftrace_command(struct ftrace_func_command *cmd)
3554 {
3555 	struct ftrace_func_command *p;
3556 	int ret = 0;
3557 
3558 	mutex_lock(&ftrace_cmd_mutex);
3559 	list_for_each_entry(p, &ftrace_commands, list) {
3560 		if (strcmp(cmd->name, p->name) == 0) {
3561 			ret = -EBUSY;
3562 			goto out_unlock;
3563 		}
3564 	}
3565 	list_add(&cmd->list, &ftrace_commands);
3566  out_unlock:
3567 	mutex_unlock(&ftrace_cmd_mutex);
3568 
3569 	return ret;
3570 }
3571 
3572 /*
3573  * Currently we only unregister ftrace commands from __init, so mark
3574  * this __init too.
3575  */
3576 __init int unregister_ftrace_command(struct ftrace_func_command *cmd)
3577 {
3578 	struct ftrace_func_command *p, *n;
3579 	int ret = -ENODEV;
3580 
3581 	mutex_lock(&ftrace_cmd_mutex);
3582 	list_for_each_entry_safe(p, n, &ftrace_commands, list) {
3583 		if (strcmp(cmd->name, p->name) == 0) {
3584 			ret = 0;
3585 			list_del_init(&p->list);
3586 			goto out_unlock;
3587 		}
3588 	}
3589  out_unlock:
3590 	mutex_unlock(&ftrace_cmd_mutex);
3591 
3592 	return ret;
3593 }
3594 
3595 static int ftrace_process_regex(struct ftrace_hash *hash,
3596 				char *buff, int len, int enable)
3597 {
3598 	char *func, *command, *next = buff;
3599 	struct ftrace_func_command *p;
3600 	int ret = -EINVAL;
3601 
3602 	func = strsep(&next, ":");
3603 
3604 	if (!next) {
3605 		ret = ftrace_match_records(hash, func, len);
3606 		if (!ret)
3607 			ret = -EINVAL;
3608 		if (ret < 0)
3609 			return ret;
3610 		return 0;
3611 	}
3612 
3613 	/* command found */
3614 
3615 	command = strsep(&next, ":");
3616 
3617 	mutex_lock(&ftrace_cmd_mutex);
3618 	list_for_each_entry(p, &ftrace_commands, list) {
3619 		if (strcmp(p->name, command) == 0) {
3620 			ret = p->func(hash, func, command, next, enable);
3621 			goto out_unlock;
3622 		}
3623 	}
3624  out_unlock:
3625 	mutex_unlock(&ftrace_cmd_mutex);
3626 
3627 	return ret;
3628 }
3629 
3630 static ssize_t
3631 ftrace_regex_write(struct file *file, const char __user *ubuf,
3632 		   size_t cnt, loff_t *ppos, int enable)
3633 {
3634 	struct ftrace_iterator *iter;
3635 	struct trace_parser *parser;
3636 	ssize_t ret, read;
3637 
3638 	if (!cnt)
3639 		return 0;
3640 
3641 	if (file->f_mode & FMODE_READ) {
3642 		struct seq_file *m = file->private_data;
3643 		iter = m->private;
3644 	} else
3645 		iter = file->private_data;
3646 
3647 	if (unlikely(ftrace_disabled))
3648 		return -ENODEV;
3649 
3650 	/* iter->hash is a local copy, so we don't need regex_lock */
3651 
3652 	parser = &iter->parser;
3653 	read = trace_get_user(parser, ubuf, cnt, ppos);
3654 
3655 	if (read >= 0 && trace_parser_loaded(parser) &&
3656 	    !trace_parser_cont(parser)) {
3657 		ret = ftrace_process_regex(iter->hash, parser->buffer,
3658 					   parser->idx, enable);
3659 		trace_parser_clear(parser);
3660 		if (ret < 0)
3661 			goto out;
3662 	}
3663 
3664 	ret = read;
3665  out:
3666 	return ret;
3667 }
3668 
3669 ssize_t
3670 ftrace_filter_write(struct file *file, const char __user *ubuf,
3671 		    size_t cnt, loff_t *ppos)
3672 {
3673 	return ftrace_regex_write(file, ubuf, cnt, ppos, 1);
3674 }
3675 
3676 ssize_t
3677 ftrace_notrace_write(struct file *file, const char __user *ubuf,
3678 		     size_t cnt, loff_t *ppos)
3679 {
3680 	return ftrace_regex_write(file, ubuf, cnt, ppos, 0);
3681 }
3682 
3683 static int
3684 ftrace_match_addr(struct ftrace_hash *hash, unsigned long ip, int remove)
3685 {
3686 	struct ftrace_func_entry *entry;
3687 
3688 	if (!ftrace_location(ip))
3689 		return -EINVAL;
3690 
3691 	if (remove) {
3692 		entry = ftrace_lookup_ip(hash, ip);
3693 		if (!entry)
3694 			return -ENOENT;
3695 		free_hash_entry(hash, entry);
3696 		return 0;
3697 	}
3698 
3699 	return add_hash_entry(hash, ip);
3700 }
3701 
3702 static void ftrace_ops_update_code(struct ftrace_ops *ops)
3703 {
3704 	if (ops->flags & FTRACE_OPS_FL_ENABLED && ftrace_enabled)
3705 		ftrace_run_update_code(FTRACE_UPDATE_CALLS);
3706 }
3707 
3708 static int
3709 ftrace_set_hash(struct ftrace_ops *ops, unsigned char *buf, int len,
3710 		unsigned long ip, int remove, int reset, int enable)
3711 {
3712 	struct ftrace_hash **orig_hash;
3713 	struct ftrace_hash *hash;
3714 	int ret;
3715 
3716 	if (unlikely(ftrace_disabled))
3717 		return -ENODEV;
3718 
3719 	mutex_lock(&ops->regex_lock);
3720 
3721 	if (enable)
3722 		orig_hash = &ops->filter_hash;
3723 	else
3724 		orig_hash = &ops->notrace_hash;
3725 
3726 	hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, *orig_hash);
3727 	if (!hash) {
3728 		ret = -ENOMEM;
3729 		goto out_regex_unlock;
3730 	}
3731 
3732 	if (reset)
3733 		ftrace_filter_reset(hash);
3734 	if (buf && !ftrace_match_records(hash, buf, len)) {
3735 		ret = -EINVAL;
3736 		goto out_regex_unlock;
3737 	}
3738 	if (ip) {
3739 		ret = ftrace_match_addr(hash, ip, remove);
3740 		if (ret < 0)
3741 			goto out_regex_unlock;
3742 	}
3743 
3744 	mutex_lock(&ftrace_lock);
3745 	ret = ftrace_hash_move(ops, enable, orig_hash, hash);
3746 	if (!ret)
3747 		ftrace_ops_update_code(ops);
3748 
3749 	mutex_unlock(&ftrace_lock);
3750 
3751  out_regex_unlock:
3752 	mutex_unlock(&ops->regex_lock);
3753 
3754 	free_ftrace_hash(hash);
3755 	return ret;
3756 }
3757 
3758 static int
3759 ftrace_set_addr(struct ftrace_ops *ops, unsigned long ip, int remove,
3760 		int reset, int enable)
3761 {
3762 	return ftrace_set_hash(ops, 0, 0, ip, remove, reset, enable);
3763 }
3764 
3765 /**
3766  * ftrace_set_filter_ip - set a function to filter on in ftrace by address
3767  * @ops - the ops to set the filter with
3768  * @ip - the address to add to or remove from the filter.
3769  * @remove - non zero to remove the ip from the filter
3770  * @reset - non zero to reset all filters before applying this filter.
3771  *
3772  * Filters denote which functions should be enabled when tracing is enabled
3773  * If @ip is NULL, it failes to update filter.
3774  */
3775 int ftrace_set_filter_ip(struct ftrace_ops *ops, unsigned long ip,
3776 			 int remove, int reset)
3777 {
3778 	ftrace_ops_init(ops);
3779 	return ftrace_set_addr(ops, ip, remove, reset, 1);
3780 }
3781 EXPORT_SYMBOL_GPL(ftrace_set_filter_ip);
3782 
3783 static int
3784 ftrace_set_regex(struct ftrace_ops *ops, unsigned char *buf, int len,
3785 		 int reset, int enable)
3786 {
3787 	return ftrace_set_hash(ops, buf, len, 0, 0, reset, enable);
3788 }
3789 
3790 /**
3791  * ftrace_set_filter - set a function to filter on in ftrace
3792  * @ops - the ops to set the filter with
3793  * @buf - the string that holds the function filter text.
3794  * @len - the length of the string.
3795  * @reset - non zero to reset all filters before applying this filter.
3796  *
3797  * Filters denote which functions should be enabled when tracing is enabled.
3798  * If @buf is NULL and reset is set, all functions will be enabled for tracing.
3799  */
3800 int ftrace_set_filter(struct ftrace_ops *ops, unsigned char *buf,
3801 		       int len, int reset)
3802 {
3803 	ftrace_ops_init(ops);
3804 	return ftrace_set_regex(ops, buf, len, reset, 1);
3805 }
3806 EXPORT_SYMBOL_GPL(ftrace_set_filter);
3807 
3808 /**
3809  * ftrace_set_notrace - set a function to not trace in ftrace
3810  * @ops - the ops to set the notrace filter with
3811  * @buf - the string that holds the function notrace text.
3812  * @len - the length of the string.
3813  * @reset - non zero to reset all filters before applying this filter.
3814  *
3815  * Notrace Filters denote which functions should not be enabled when tracing
3816  * is enabled. If @buf is NULL and reset is set, all functions will be enabled
3817  * for tracing.
3818  */
3819 int ftrace_set_notrace(struct ftrace_ops *ops, unsigned char *buf,
3820 			int len, int reset)
3821 {
3822 	ftrace_ops_init(ops);
3823 	return ftrace_set_regex(ops, buf, len, reset, 0);
3824 }
3825 EXPORT_SYMBOL_GPL(ftrace_set_notrace);
3826 /**
3827  * ftrace_set_global_filter - set a function to filter on with global tracers
3828  * @buf - the string that holds the function filter text.
3829  * @len - the length of the string.
3830  * @reset - non zero to reset all filters before applying this filter.
3831  *
3832  * Filters denote which functions should be enabled when tracing is enabled.
3833  * If @buf is NULL and reset is set, all functions will be enabled for tracing.
3834  */
3835 void ftrace_set_global_filter(unsigned char *buf, int len, int reset)
3836 {
3837 	ftrace_set_regex(&global_ops, buf, len, reset, 1);
3838 }
3839 EXPORT_SYMBOL_GPL(ftrace_set_global_filter);
3840 
3841 /**
3842  * ftrace_set_global_notrace - set a function to not trace with global tracers
3843  * @buf - the string that holds the function notrace text.
3844  * @len - the length of the string.
3845  * @reset - non zero to reset all filters before applying this filter.
3846  *
3847  * Notrace Filters denote which functions should not be enabled when tracing
3848  * is enabled. If @buf is NULL and reset is set, all functions will be enabled
3849  * for tracing.
3850  */
3851 void ftrace_set_global_notrace(unsigned char *buf, int len, int reset)
3852 {
3853 	ftrace_set_regex(&global_ops, buf, len, reset, 0);
3854 }
3855 EXPORT_SYMBOL_GPL(ftrace_set_global_notrace);
3856 
3857 /*
3858  * command line interface to allow users to set filters on boot up.
3859  */
3860 #define FTRACE_FILTER_SIZE		COMMAND_LINE_SIZE
3861 static char ftrace_notrace_buf[FTRACE_FILTER_SIZE] __initdata;
3862 static char ftrace_filter_buf[FTRACE_FILTER_SIZE] __initdata;
3863 
3864 /* Used by function selftest to not test if filter is set */
3865 bool ftrace_filter_param __initdata;
3866 
3867 static int __init set_ftrace_notrace(char *str)
3868 {
3869 	ftrace_filter_param = true;
3870 	strlcpy(ftrace_notrace_buf, str, FTRACE_FILTER_SIZE);
3871 	return 1;
3872 }
3873 __setup("ftrace_notrace=", set_ftrace_notrace);
3874 
3875 static int __init set_ftrace_filter(char *str)
3876 {
3877 	ftrace_filter_param = true;
3878 	strlcpy(ftrace_filter_buf, str, FTRACE_FILTER_SIZE);
3879 	return 1;
3880 }
3881 __setup("ftrace_filter=", set_ftrace_filter);
3882 
3883 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
3884 static char ftrace_graph_buf[FTRACE_FILTER_SIZE] __initdata;
3885 static char ftrace_graph_notrace_buf[FTRACE_FILTER_SIZE] __initdata;
3886 static int ftrace_set_func(unsigned long *array, int *idx, int size, char *buffer);
3887 
3888 static int __init set_graph_function(char *str)
3889 {
3890 	strlcpy(ftrace_graph_buf, str, FTRACE_FILTER_SIZE);
3891 	return 1;
3892 }
3893 __setup("ftrace_graph_filter=", set_graph_function);
3894 
3895 static int __init set_graph_notrace_function(char *str)
3896 {
3897 	strlcpy(ftrace_graph_notrace_buf, str, FTRACE_FILTER_SIZE);
3898 	return 1;
3899 }
3900 __setup("ftrace_graph_notrace=", set_graph_notrace_function);
3901 
3902 static void __init set_ftrace_early_graph(char *buf, int enable)
3903 {
3904 	int ret;
3905 	char *func;
3906 	unsigned long *table = ftrace_graph_funcs;
3907 	int *count = &ftrace_graph_count;
3908 
3909 	if (!enable) {
3910 		table = ftrace_graph_notrace_funcs;
3911 		count = &ftrace_graph_notrace_count;
3912 	}
3913 
3914 	while (buf) {
3915 		func = strsep(&buf, ",");
3916 		/* we allow only one expression at a time */
3917 		ret = ftrace_set_func(table, count, FTRACE_GRAPH_MAX_FUNCS, func);
3918 		if (ret)
3919 			printk(KERN_DEBUG "ftrace: function %s not "
3920 					  "traceable\n", func);
3921 	}
3922 }
3923 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
3924 
3925 void __init
3926 ftrace_set_early_filter(struct ftrace_ops *ops, char *buf, int enable)
3927 {
3928 	char *func;
3929 
3930 	ftrace_ops_init(ops);
3931 
3932 	while (buf) {
3933 		func = strsep(&buf, ",");
3934 		ftrace_set_regex(ops, func, strlen(func), 0, enable);
3935 	}
3936 }
3937 
3938 static void __init set_ftrace_early_filters(void)
3939 {
3940 	if (ftrace_filter_buf[0])
3941 		ftrace_set_early_filter(&global_ops, ftrace_filter_buf, 1);
3942 	if (ftrace_notrace_buf[0])
3943 		ftrace_set_early_filter(&global_ops, ftrace_notrace_buf, 0);
3944 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
3945 	if (ftrace_graph_buf[0])
3946 		set_ftrace_early_graph(ftrace_graph_buf, 1);
3947 	if (ftrace_graph_notrace_buf[0])
3948 		set_ftrace_early_graph(ftrace_graph_notrace_buf, 0);
3949 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
3950 }
3951 
3952 int ftrace_regex_release(struct inode *inode, struct file *file)
3953 {
3954 	struct seq_file *m = (struct seq_file *)file->private_data;
3955 	struct ftrace_iterator *iter;
3956 	struct ftrace_hash **orig_hash;
3957 	struct trace_parser *parser;
3958 	int filter_hash;
3959 	int ret;
3960 
3961 	if (file->f_mode & FMODE_READ) {
3962 		iter = m->private;
3963 		seq_release(inode, file);
3964 	} else
3965 		iter = file->private_data;
3966 
3967 	parser = &iter->parser;
3968 	if (trace_parser_loaded(parser)) {
3969 		parser->buffer[parser->idx] = 0;
3970 		ftrace_match_records(iter->hash, parser->buffer, parser->idx);
3971 	}
3972 
3973 	trace_parser_put(parser);
3974 
3975 	mutex_lock(&iter->ops->regex_lock);
3976 
3977 	if (file->f_mode & FMODE_WRITE) {
3978 		filter_hash = !!(iter->flags & FTRACE_ITER_FILTER);
3979 
3980 		if (filter_hash)
3981 			orig_hash = &iter->ops->filter_hash;
3982 		else
3983 			orig_hash = &iter->ops->notrace_hash;
3984 
3985 		mutex_lock(&ftrace_lock);
3986 		ret = ftrace_hash_move(iter->ops, filter_hash,
3987 				       orig_hash, iter->hash);
3988 		if (!ret)
3989 			ftrace_ops_update_code(iter->ops);
3990 
3991 		mutex_unlock(&ftrace_lock);
3992 	}
3993 
3994 	mutex_unlock(&iter->ops->regex_lock);
3995 	free_ftrace_hash(iter->hash);
3996 	kfree(iter);
3997 
3998 	return 0;
3999 }
4000 
4001 static const struct file_operations ftrace_avail_fops = {
4002 	.open = ftrace_avail_open,
4003 	.read = seq_read,
4004 	.llseek = seq_lseek,
4005 	.release = seq_release_private,
4006 };
4007 
4008 static const struct file_operations ftrace_enabled_fops = {
4009 	.open = ftrace_enabled_open,
4010 	.read = seq_read,
4011 	.llseek = seq_lseek,
4012 	.release = seq_release_private,
4013 };
4014 
4015 static const struct file_operations ftrace_filter_fops = {
4016 	.open = ftrace_filter_open,
4017 	.read = seq_read,
4018 	.write = ftrace_filter_write,
4019 	.llseek = tracing_lseek,
4020 	.release = ftrace_regex_release,
4021 };
4022 
4023 static const struct file_operations ftrace_notrace_fops = {
4024 	.open = ftrace_notrace_open,
4025 	.read = seq_read,
4026 	.write = ftrace_notrace_write,
4027 	.llseek = tracing_lseek,
4028 	.release = ftrace_regex_release,
4029 };
4030 
4031 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
4032 
4033 static DEFINE_MUTEX(graph_lock);
4034 
4035 int ftrace_graph_count;
4036 int ftrace_graph_notrace_count;
4037 unsigned long ftrace_graph_funcs[FTRACE_GRAPH_MAX_FUNCS] __read_mostly;
4038 unsigned long ftrace_graph_notrace_funcs[FTRACE_GRAPH_MAX_FUNCS] __read_mostly;
4039 
4040 struct ftrace_graph_data {
4041 	unsigned long *table;
4042 	size_t size;
4043 	int *count;
4044 	const struct seq_operations *seq_ops;
4045 };
4046 
4047 static void *
4048 __g_next(struct seq_file *m, loff_t *pos)
4049 {
4050 	struct ftrace_graph_data *fgd = m->private;
4051 
4052 	if (*pos >= *fgd->count)
4053 		return NULL;
4054 	return &fgd->table[*pos];
4055 }
4056 
4057 static void *
4058 g_next(struct seq_file *m, void *v, loff_t *pos)
4059 {
4060 	(*pos)++;
4061 	return __g_next(m, pos);
4062 }
4063 
4064 static void *g_start(struct seq_file *m, loff_t *pos)
4065 {
4066 	struct ftrace_graph_data *fgd = m->private;
4067 
4068 	mutex_lock(&graph_lock);
4069 
4070 	/* Nothing, tell g_show to print all functions are enabled */
4071 	if (!*fgd->count && !*pos)
4072 		return (void *)1;
4073 
4074 	return __g_next(m, pos);
4075 }
4076 
4077 static void g_stop(struct seq_file *m, void *p)
4078 {
4079 	mutex_unlock(&graph_lock);
4080 }
4081 
4082 static int g_show(struct seq_file *m, void *v)
4083 {
4084 	unsigned long *ptr = v;
4085 
4086 	if (!ptr)
4087 		return 0;
4088 
4089 	if (ptr == (unsigned long *)1) {
4090 		struct ftrace_graph_data *fgd = m->private;
4091 
4092 		if (fgd->table == ftrace_graph_funcs)
4093 			seq_printf(m, "#### all functions enabled ####\n");
4094 		else
4095 			seq_printf(m, "#### no functions disabled ####\n");
4096 		return 0;
4097 	}
4098 
4099 	seq_printf(m, "%ps\n", (void *)*ptr);
4100 
4101 	return 0;
4102 }
4103 
4104 static const struct seq_operations ftrace_graph_seq_ops = {
4105 	.start = g_start,
4106 	.next = g_next,
4107 	.stop = g_stop,
4108 	.show = g_show,
4109 };
4110 
4111 static int
4112 __ftrace_graph_open(struct inode *inode, struct file *file,
4113 		    struct ftrace_graph_data *fgd)
4114 {
4115 	int ret = 0;
4116 
4117 	mutex_lock(&graph_lock);
4118 	if ((file->f_mode & FMODE_WRITE) &&
4119 	    (file->f_flags & O_TRUNC)) {
4120 		*fgd->count = 0;
4121 		memset(fgd->table, 0, fgd->size * sizeof(*fgd->table));
4122 	}
4123 	mutex_unlock(&graph_lock);
4124 
4125 	if (file->f_mode & FMODE_READ) {
4126 		ret = seq_open(file, fgd->seq_ops);
4127 		if (!ret) {
4128 			struct seq_file *m = file->private_data;
4129 			m->private = fgd;
4130 		}
4131 	} else
4132 		file->private_data = fgd;
4133 
4134 	return ret;
4135 }
4136 
4137 static int
4138 ftrace_graph_open(struct inode *inode, struct file *file)
4139 {
4140 	struct ftrace_graph_data *fgd;
4141 
4142 	if (unlikely(ftrace_disabled))
4143 		return -ENODEV;
4144 
4145 	fgd = kmalloc(sizeof(*fgd), GFP_KERNEL);
4146 	if (fgd == NULL)
4147 		return -ENOMEM;
4148 
4149 	fgd->table = ftrace_graph_funcs;
4150 	fgd->size = FTRACE_GRAPH_MAX_FUNCS;
4151 	fgd->count = &ftrace_graph_count;
4152 	fgd->seq_ops = &ftrace_graph_seq_ops;
4153 
4154 	return __ftrace_graph_open(inode, file, fgd);
4155 }
4156 
4157 static int
4158 ftrace_graph_notrace_open(struct inode *inode, struct file *file)
4159 {
4160 	struct ftrace_graph_data *fgd;
4161 
4162 	if (unlikely(ftrace_disabled))
4163 		return -ENODEV;
4164 
4165 	fgd = kmalloc(sizeof(*fgd), GFP_KERNEL);
4166 	if (fgd == NULL)
4167 		return -ENOMEM;
4168 
4169 	fgd->table = ftrace_graph_notrace_funcs;
4170 	fgd->size = FTRACE_GRAPH_MAX_FUNCS;
4171 	fgd->count = &ftrace_graph_notrace_count;
4172 	fgd->seq_ops = &ftrace_graph_seq_ops;
4173 
4174 	return __ftrace_graph_open(inode, file, fgd);
4175 }
4176 
4177 static int
4178 ftrace_graph_release(struct inode *inode, struct file *file)
4179 {
4180 	if (file->f_mode & FMODE_READ) {
4181 		struct seq_file *m = file->private_data;
4182 
4183 		kfree(m->private);
4184 		seq_release(inode, file);
4185 	} else {
4186 		kfree(file->private_data);
4187 	}
4188 
4189 	return 0;
4190 }
4191 
4192 static int
4193 ftrace_set_func(unsigned long *array, int *idx, int size, char *buffer)
4194 {
4195 	struct dyn_ftrace *rec;
4196 	struct ftrace_page *pg;
4197 	int search_len;
4198 	int fail = 1;
4199 	int type, not;
4200 	char *search;
4201 	bool exists;
4202 	int i;
4203 
4204 	/* decode regex */
4205 	type = filter_parse_regex(buffer, strlen(buffer), &search, &not);
4206 	if (!not && *idx >= size)
4207 		return -EBUSY;
4208 
4209 	search_len = strlen(search);
4210 
4211 	mutex_lock(&ftrace_lock);
4212 
4213 	if (unlikely(ftrace_disabled)) {
4214 		mutex_unlock(&ftrace_lock);
4215 		return -ENODEV;
4216 	}
4217 
4218 	do_for_each_ftrace_rec(pg, rec) {
4219 
4220 		if (ftrace_match_record(rec, NULL, search, search_len, type)) {
4221 			/* if it is in the array */
4222 			exists = false;
4223 			for (i = 0; i < *idx; i++) {
4224 				if (array[i] == rec->ip) {
4225 					exists = true;
4226 					break;
4227 				}
4228 			}
4229 
4230 			if (!not) {
4231 				fail = 0;
4232 				if (!exists) {
4233 					array[(*idx)++] = rec->ip;
4234 					if (*idx >= size)
4235 						goto out;
4236 				}
4237 			} else {
4238 				if (exists) {
4239 					array[i] = array[--(*idx)];
4240 					array[*idx] = 0;
4241 					fail = 0;
4242 				}
4243 			}
4244 		}
4245 	} while_for_each_ftrace_rec();
4246 out:
4247 	mutex_unlock(&ftrace_lock);
4248 
4249 	if (fail)
4250 		return -EINVAL;
4251 
4252 	return 0;
4253 }
4254 
4255 static ssize_t
4256 ftrace_graph_write(struct file *file, const char __user *ubuf,
4257 		   size_t cnt, loff_t *ppos)
4258 {
4259 	struct trace_parser parser;
4260 	ssize_t read, ret = 0;
4261 	struct ftrace_graph_data *fgd = file->private_data;
4262 
4263 	if (!cnt)
4264 		return 0;
4265 
4266 	if (trace_parser_get_init(&parser, FTRACE_BUFF_MAX))
4267 		return -ENOMEM;
4268 
4269 	read = trace_get_user(&parser, ubuf, cnt, ppos);
4270 
4271 	if (read >= 0 && trace_parser_loaded((&parser))) {
4272 		parser.buffer[parser.idx] = 0;
4273 
4274 		mutex_lock(&graph_lock);
4275 
4276 		/* we allow only one expression at a time */
4277 		ret = ftrace_set_func(fgd->table, fgd->count, fgd->size,
4278 				      parser.buffer);
4279 
4280 		mutex_unlock(&graph_lock);
4281 	}
4282 
4283 	if (!ret)
4284 		ret = read;
4285 
4286 	trace_parser_put(&parser);
4287 
4288 	return ret;
4289 }
4290 
4291 static const struct file_operations ftrace_graph_fops = {
4292 	.open		= ftrace_graph_open,
4293 	.read		= seq_read,
4294 	.write		= ftrace_graph_write,
4295 	.llseek		= tracing_lseek,
4296 	.release	= ftrace_graph_release,
4297 };
4298 
4299 static const struct file_operations ftrace_graph_notrace_fops = {
4300 	.open		= ftrace_graph_notrace_open,
4301 	.read		= seq_read,
4302 	.write		= ftrace_graph_write,
4303 	.llseek		= tracing_lseek,
4304 	.release	= ftrace_graph_release,
4305 };
4306 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
4307 
4308 void ftrace_create_filter_files(struct ftrace_ops *ops,
4309 				struct dentry *parent)
4310 {
4311 
4312 	trace_create_file("set_ftrace_filter", 0644, parent,
4313 			  ops, &ftrace_filter_fops);
4314 
4315 	trace_create_file("set_ftrace_notrace", 0644, parent,
4316 			  ops, &ftrace_notrace_fops);
4317 }
4318 
4319 /*
4320  * The name "destroy_filter_files" is really a misnomer. Although
4321  * in the future, it may actualy delete the files, but this is
4322  * really intended to make sure the ops passed in are disabled
4323  * and that when this function returns, the caller is free to
4324  * free the ops.
4325  *
4326  * The "destroy" name is only to match the "create" name that this
4327  * should be paired with.
4328  */
4329 void ftrace_destroy_filter_files(struct ftrace_ops *ops)
4330 {
4331 	mutex_lock(&ftrace_lock);
4332 	if (ops->flags & FTRACE_OPS_FL_ENABLED)
4333 		ftrace_shutdown(ops, 0);
4334 	ops->flags |= FTRACE_OPS_FL_DELETED;
4335 	mutex_unlock(&ftrace_lock);
4336 }
4337 
4338 static __init int ftrace_init_dyn_debugfs(struct dentry *d_tracer)
4339 {
4340 
4341 	trace_create_file("available_filter_functions", 0444,
4342 			d_tracer, NULL, &ftrace_avail_fops);
4343 
4344 	trace_create_file("enabled_functions", 0444,
4345 			d_tracer, NULL, &ftrace_enabled_fops);
4346 
4347 	ftrace_create_filter_files(&global_ops, d_tracer);
4348 
4349 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
4350 	trace_create_file("set_graph_function", 0444, d_tracer,
4351 				    NULL,
4352 				    &ftrace_graph_fops);
4353 	trace_create_file("set_graph_notrace", 0444, d_tracer,
4354 				    NULL,
4355 				    &ftrace_graph_notrace_fops);
4356 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
4357 
4358 	return 0;
4359 }
4360 
4361 static int ftrace_cmp_ips(const void *a, const void *b)
4362 {
4363 	const unsigned long *ipa = a;
4364 	const unsigned long *ipb = b;
4365 
4366 	if (*ipa > *ipb)
4367 		return 1;
4368 	if (*ipa < *ipb)
4369 		return -1;
4370 	return 0;
4371 }
4372 
4373 static void ftrace_swap_ips(void *a, void *b, int size)
4374 {
4375 	unsigned long *ipa = a;
4376 	unsigned long *ipb = b;
4377 	unsigned long t;
4378 
4379 	t = *ipa;
4380 	*ipa = *ipb;
4381 	*ipb = t;
4382 }
4383 
4384 static int ftrace_process_locs(struct module *mod,
4385 			       unsigned long *start,
4386 			       unsigned long *end)
4387 {
4388 	struct ftrace_page *start_pg;
4389 	struct ftrace_page *pg;
4390 	struct dyn_ftrace *rec;
4391 	unsigned long count;
4392 	unsigned long *p;
4393 	unsigned long addr;
4394 	unsigned long flags = 0; /* Shut up gcc */
4395 	int ret = -ENOMEM;
4396 
4397 	count = end - start;
4398 
4399 	if (!count)
4400 		return 0;
4401 
4402 	sort(start, count, sizeof(*start),
4403 	     ftrace_cmp_ips, ftrace_swap_ips);
4404 
4405 	start_pg = ftrace_allocate_pages(count);
4406 	if (!start_pg)
4407 		return -ENOMEM;
4408 
4409 	mutex_lock(&ftrace_lock);
4410 
4411 	/*
4412 	 * Core and each module needs their own pages, as
4413 	 * modules will free them when they are removed.
4414 	 * Force a new page to be allocated for modules.
4415 	 */
4416 	if (!mod) {
4417 		WARN_ON(ftrace_pages || ftrace_pages_start);
4418 		/* First initialization */
4419 		ftrace_pages = ftrace_pages_start = start_pg;
4420 	} else {
4421 		if (!ftrace_pages)
4422 			goto out;
4423 
4424 		if (WARN_ON(ftrace_pages->next)) {
4425 			/* Hmm, we have free pages? */
4426 			while (ftrace_pages->next)
4427 				ftrace_pages = ftrace_pages->next;
4428 		}
4429 
4430 		ftrace_pages->next = start_pg;
4431 	}
4432 
4433 	p = start;
4434 	pg = start_pg;
4435 	while (p < end) {
4436 		addr = ftrace_call_adjust(*p++);
4437 		/*
4438 		 * Some architecture linkers will pad between
4439 		 * the different mcount_loc sections of different
4440 		 * object files to satisfy alignments.
4441 		 * Skip any NULL pointers.
4442 		 */
4443 		if (!addr)
4444 			continue;
4445 
4446 		if (pg->index == pg->size) {
4447 			/* We should have allocated enough */
4448 			if (WARN_ON(!pg->next))
4449 				break;
4450 			pg = pg->next;
4451 		}
4452 
4453 		rec = &pg->records[pg->index++];
4454 		rec->ip = addr;
4455 	}
4456 
4457 	/* We should have used all pages */
4458 	WARN_ON(pg->next);
4459 
4460 	/* Assign the last page to ftrace_pages */
4461 	ftrace_pages = pg;
4462 
4463 	/*
4464 	 * We only need to disable interrupts on start up
4465 	 * because we are modifying code that an interrupt
4466 	 * may execute, and the modification is not atomic.
4467 	 * But for modules, nothing runs the code we modify
4468 	 * until we are finished with it, and there's no
4469 	 * reason to cause large interrupt latencies while we do it.
4470 	 */
4471 	if (!mod)
4472 		local_irq_save(flags);
4473 	ftrace_update_code(mod, start_pg);
4474 	if (!mod)
4475 		local_irq_restore(flags);
4476 	ret = 0;
4477  out:
4478 	mutex_unlock(&ftrace_lock);
4479 
4480 	return ret;
4481 }
4482 
4483 #ifdef CONFIG_MODULES
4484 
4485 #define next_to_ftrace_page(p) container_of(p, struct ftrace_page, next)
4486 
4487 void ftrace_release_mod(struct module *mod)
4488 {
4489 	struct dyn_ftrace *rec;
4490 	struct ftrace_page **last_pg;
4491 	struct ftrace_page *pg;
4492 	int order;
4493 
4494 	mutex_lock(&ftrace_lock);
4495 
4496 	if (ftrace_disabled)
4497 		goto out_unlock;
4498 
4499 	/*
4500 	 * Each module has its own ftrace_pages, remove
4501 	 * them from the list.
4502 	 */
4503 	last_pg = &ftrace_pages_start;
4504 	for (pg = ftrace_pages_start; pg; pg = *last_pg) {
4505 		rec = &pg->records[0];
4506 		if (within_module_core(rec->ip, mod)) {
4507 			/*
4508 			 * As core pages are first, the first
4509 			 * page should never be a module page.
4510 			 */
4511 			if (WARN_ON(pg == ftrace_pages_start))
4512 				goto out_unlock;
4513 
4514 			/* Check if we are deleting the last page */
4515 			if (pg == ftrace_pages)
4516 				ftrace_pages = next_to_ftrace_page(last_pg);
4517 
4518 			*last_pg = pg->next;
4519 			order = get_count_order(pg->size / ENTRIES_PER_PAGE);
4520 			free_pages((unsigned long)pg->records, order);
4521 			kfree(pg);
4522 		} else
4523 			last_pg = &pg->next;
4524 	}
4525  out_unlock:
4526 	mutex_unlock(&ftrace_lock);
4527 }
4528 
4529 static void ftrace_init_module(struct module *mod,
4530 			       unsigned long *start, unsigned long *end)
4531 {
4532 	if (ftrace_disabled || start == end)
4533 		return;
4534 	ftrace_process_locs(mod, start, end);
4535 }
4536 
4537 void ftrace_module_init(struct module *mod)
4538 {
4539 	ftrace_init_module(mod, mod->ftrace_callsites,
4540 			   mod->ftrace_callsites +
4541 			   mod->num_ftrace_callsites);
4542 }
4543 
4544 static int ftrace_module_notify_exit(struct notifier_block *self,
4545 				     unsigned long val, void *data)
4546 {
4547 	struct module *mod = data;
4548 
4549 	if (val == MODULE_STATE_GOING)
4550 		ftrace_release_mod(mod);
4551 
4552 	return 0;
4553 }
4554 #else
4555 static int ftrace_module_notify_exit(struct notifier_block *self,
4556 				     unsigned long val, void *data)
4557 {
4558 	return 0;
4559 }
4560 #endif /* CONFIG_MODULES */
4561 
4562 struct notifier_block ftrace_module_exit_nb = {
4563 	.notifier_call = ftrace_module_notify_exit,
4564 	.priority = INT_MIN,	/* Run after anything that can remove kprobes */
4565 };
4566 
4567 void __init ftrace_init(void)
4568 {
4569 	extern unsigned long __start_mcount_loc[];
4570 	extern unsigned long __stop_mcount_loc[];
4571 	unsigned long count, flags;
4572 	int ret;
4573 
4574 	local_irq_save(flags);
4575 	ret = ftrace_dyn_arch_init();
4576 	local_irq_restore(flags);
4577 	if (ret)
4578 		goto failed;
4579 
4580 	count = __stop_mcount_loc - __start_mcount_loc;
4581 	if (!count) {
4582 		pr_info("ftrace: No functions to be traced?\n");
4583 		goto failed;
4584 	}
4585 
4586 	pr_info("ftrace: allocating %ld entries in %ld pages\n",
4587 		count, count / ENTRIES_PER_PAGE + 1);
4588 
4589 	last_ftrace_enabled = ftrace_enabled = 1;
4590 
4591 	ret = ftrace_process_locs(NULL,
4592 				  __start_mcount_loc,
4593 				  __stop_mcount_loc);
4594 
4595 	ret = register_module_notifier(&ftrace_module_exit_nb);
4596 	if (ret)
4597 		pr_warning("Failed to register trace ftrace module exit notifier\n");
4598 
4599 	set_ftrace_early_filters();
4600 
4601 	return;
4602  failed:
4603 	ftrace_disabled = 1;
4604 }
4605 
4606 #else
4607 
4608 static struct ftrace_ops global_ops = {
4609 	.func			= ftrace_stub,
4610 	.flags			= FTRACE_OPS_FL_RECURSION_SAFE | FTRACE_OPS_FL_INITIALIZED,
4611 	INIT_REGEX_LOCK(global_ops)
4612 };
4613 
4614 static int __init ftrace_nodyn_init(void)
4615 {
4616 	ftrace_enabled = 1;
4617 	return 0;
4618 }
4619 core_initcall(ftrace_nodyn_init);
4620 
4621 static inline int ftrace_init_dyn_debugfs(struct dentry *d_tracer) { return 0; }
4622 static inline void ftrace_startup_enable(int command) { }
4623 /* Keep as macros so we do not need to define the commands */
4624 # define ftrace_startup(ops, command)					\
4625 	({								\
4626 		int ___ret = __register_ftrace_function(ops);		\
4627 		if (!___ret)						\
4628 			(ops)->flags |= FTRACE_OPS_FL_ENABLED;		\
4629 		___ret;							\
4630 	})
4631 # define ftrace_shutdown(ops, command)					\
4632 	({								\
4633 		int ___ret = __unregister_ftrace_function(ops);		\
4634 		if (!___ret)						\
4635 			(ops)->flags &= ~FTRACE_OPS_FL_ENABLED;		\
4636 		___ret;							\
4637 	})
4638 
4639 # define ftrace_startup_sysctl()	do { } while (0)
4640 # define ftrace_shutdown_sysctl()	do { } while (0)
4641 
4642 static inline int
4643 ftrace_ops_test(struct ftrace_ops *ops, unsigned long ip, void *regs)
4644 {
4645 	return 1;
4646 }
4647 
4648 #endif /* CONFIG_DYNAMIC_FTRACE */
4649 
4650 __init void ftrace_init_global_array_ops(struct trace_array *tr)
4651 {
4652 	tr->ops = &global_ops;
4653 	tr->ops->private = tr;
4654 }
4655 
4656 void ftrace_init_array_ops(struct trace_array *tr, ftrace_func_t func)
4657 {
4658 	/* If we filter on pids, update to use the pid function */
4659 	if (tr->flags & TRACE_ARRAY_FL_GLOBAL) {
4660 		if (WARN_ON(tr->ops->func != ftrace_stub))
4661 			printk("ftrace ops had %pS for function\n",
4662 			       tr->ops->func);
4663 		/* Only the top level instance does pid tracing */
4664 		if (!list_empty(&ftrace_pids)) {
4665 			set_ftrace_pid_function(func);
4666 			func = ftrace_pid_func;
4667 		}
4668 	}
4669 	tr->ops->func = func;
4670 	tr->ops->private = tr;
4671 }
4672 
4673 void ftrace_reset_array_ops(struct trace_array *tr)
4674 {
4675 	tr->ops->func = ftrace_stub;
4676 }
4677 
4678 static void
4679 ftrace_ops_control_func(unsigned long ip, unsigned long parent_ip,
4680 			struct ftrace_ops *op, struct pt_regs *regs)
4681 {
4682 	if (unlikely(trace_recursion_test(TRACE_CONTROL_BIT)))
4683 		return;
4684 
4685 	/*
4686 	 * Some of the ops may be dynamically allocated,
4687 	 * they must be freed after a synchronize_sched().
4688 	 */
4689 	preempt_disable_notrace();
4690 	trace_recursion_set(TRACE_CONTROL_BIT);
4691 
4692 	/*
4693 	 * Control funcs (perf) uses RCU. Only trace if
4694 	 * RCU is currently active.
4695 	 */
4696 	if (!rcu_is_watching())
4697 		goto out;
4698 
4699 	do_for_each_ftrace_op(op, ftrace_control_list) {
4700 		if (!(op->flags & FTRACE_OPS_FL_STUB) &&
4701 		    !ftrace_function_local_disabled(op) &&
4702 		    ftrace_ops_test(op, ip, regs))
4703 			op->func(ip, parent_ip, op, regs);
4704 	} while_for_each_ftrace_op(op);
4705  out:
4706 	trace_recursion_clear(TRACE_CONTROL_BIT);
4707 	preempt_enable_notrace();
4708 }
4709 
4710 static struct ftrace_ops control_ops = {
4711 	.func	= ftrace_ops_control_func,
4712 	.flags	= FTRACE_OPS_FL_RECURSION_SAFE | FTRACE_OPS_FL_INITIALIZED,
4713 	INIT_REGEX_LOCK(control_ops)
4714 };
4715 
4716 static inline void
4717 __ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip,
4718 		       struct ftrace_ops *ignored, struct pt_regs *regs)
4719 {
4720 	struct ftrace_ops *op;
4721 	int bit;
4722 
4723 	bit = trace_test_and_set_recursion(TRACE_LIST_START, TRACE_LIST_MAX);
4724 	if (bit < 0)
4725 		return;
4726 
4727 	/*
4728 	 * Some of the ops may be dynamically allocated,
4729 	 * they must be freed after a synchronize_sched().
4730 	 */
4731 	preempt_disable_notrace();
4732 	do_for_each_ftrace_op(op, ftrace_ops_list) {
4733 		if (ftrace_ops_test(op, ip, regs)) {
4734 			if (FTRACE_WARN_ON(!op->func)) {
4735 				pr_warn("op=%p %pS\n", op, op);
4736 				goto out;
4737 			}
4738 			op->func(ip, parent_ip, op, regs);
4739 		}
4740 	} while_for_each_ftrace_op(op);
4741 out:
4742 	preempt_enable_notrace();
4743 	trace_clear_recursion(bit);
4744 }
4745 
4746 /*
4747  * Some archs only support passing ip and parent_ip. Even though
4748  * the list function ignores the op parameter, we do not want any
4749  * C side effects, where a function is called without the caller
4750  * sending a third parameter.
4751  * Archs are to support both the regs and ftrace_ops at the same time.
4752  * If they support ftrace_ops, it is assumed they support regs.
4753  * If call backs want to use regs, they must either check for regs
4754  * being NULL, or CONFIG_DYNAMIC_FTRACE_WITH_REGS.
4755  * Note, CONFIG_DYNAMIC_FTRACE_WITH_REGS expects a full regs to be saved.
4756  * An architecture can pass partial regs with ftrace_ops and still
4757  * set the ARCH_SUPPORT_FTARCE_OPS.
4758  */
4759 #if ARCH_SUPPORTS_FTRACE_OPS
4760 static void ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip,
4761 				 struct ftrace_ops *op, struct pt_regs *regs)
4762 {
4763 	__ftrace_ops_list_func(ip, parent_ip, NULL, regs);
4764 }
4765 #else
4766 static void ftrace_ops_no_ops(unsigned long ip, unsigned long parent_ip)
4767 {
4768 	__ftrace_ops_list_func(ip, parent_ip, NULL, NULL);
4769 }
4770 #endif
4771 
4772 static void clear_ftrace_swapper(void)
4773 {
4774 	struct task_struct *p;
4775 	int cpu;
4776 
4777 	get_online_cpus();
4778 	for_each_online_cpu(cpu) {
4779 		p = idle_task(cpu);
4780 		clear_tsk_trace_trace(p);
4781 	}
4782 	put_online_cpus();
4783 }
4784 
4785 static void set_ftrace_swapper(void)
4786 {
4787 	struct task_struct *p;
4788 	int cpu;
4789 
4790 	get_online_cpus();
4791 	for_each_online_cpu(cpu) {
4792 		p = idle_task(cpu);
4793 		set_tsk_trace_trace(p);
4794 	}
4795 	put_online_cpus();
4796 }
4797 
4798 static void clear_ftrace_pid(struct pid *pid)
4799 {
4800 	struct task_struct *p;
4801 
4802 	rcu_read_lock();
4803 	do_each_pid_task(pid, PIDTYPE_PID, p) {
4804 		clear_tsk_trace_trace(p);
4805 	} while_each_pid_task(pid, PIDTYPE_PID, p);
4806 	rcu_read_unlock();
4807 
4808 	put_pid(pid);
4809 }
4810 
4811 static void set_ftrace_pid(struct pid *pid)
4812 {
4813 	struct task_struct *p;
4814 
4815 	rcu_read_lock();
4816 	do_each_pid_task(pid, PIDTYPE_PID, p) {
4817 		set_tsk_trace_trace(p);
4818 	} while_each_pid_task(pid, PIDTYPE_PID, p);
4819 	rcu_read_unlock();
4820 }
4821 
4822 static void clear_ftrace_pid_task(struct pid *pid)
4823 {
4824 	if (pid == ftrace_swapper_pid)
4825 		clear_ftrace_swapper();
4826 	else
4827 		clear_ftrace_pid(pid);
4828 }
4829 
4830 static void set_ftrace_pid_task(struct pid *pid)
4831 {
4832 	if (pid == ftrace_swapper_pid)
4833 		set_ftrace_swapper();
4834 	else
4835 		set_ftrace_pid(pid);
4836 }
4837 
4838 static int ftrace_pid_add(int p)
4839 {
4840 	struct pid *pid;
4841 	struct ftrace_pid *fpid;
4842 	int ret = -EINVAL;
4843 
4844 	mutex_lock(&ftrace_lock);
4845 
4846 	if (!p)
4847 		pid = ftrace_swapper_pid;
4848 	else
4849 		pid = find_get_pid(p);
4850 
4851 	if (!pid)
4852 		goto out;
4853 
4854 	ret = 0;
4855 
4856 	list_for_each_entry(fpid, &ftrace_pids, list)
4857 		if (fpid->pid == pid)
4858 			goto out_put;
4859 
4860 	ret = -ENOMEM;
4861 
4862 	fpid = kmalloc(sizeof(*fpid), GFP_KERNEL);
4863 	if (!fpid)
4864 		goto out_put;
4865 
4866 	list_add(&fpid->list, &ftrace_pids);
4867 	fpid->pid = pid;
4868 
4869 	set_ftrace_pid_task(pid);
4870 
4871 	ftrace_update_pid_func();
4872 	ftrace_startup_enable(0);
4873 
4874 	mutex_unlock(&ftrace_lock);
4875 	return 0;
4876 
4877 out_put:
4878 	if (pid != ftrace_swapper_pid)
4879 		put_pid(pid);
4880 
4881 out:
4882 	mutex_unlock(&ftrace_lock);
4883 	return ret;
4884 }
4885 
4886 static void ftrace_pid_reset(void)
4887 {
4888 	struct ftrace_pid *fpid, *safe;
4889 
4890 	mutex_lock(&ftrace_lock);
4891 	list_for_each_entry_safe(fpid, safe, &ftrace_pids, list) {
4892 		struct pid *pid = fpid->pid;
4893 
4894 		clear_ftrace_pid_task(pid);
4895 
4896 		list_del(&fpid->list);
4897 		kfree(fpid);
4898 	}
4899 
4900 	ftrace_update_pid_func();
4901 	ftrace_startup_enable(0);
4902 
4903 	mutex_unlock(&ftrace_lock);
4904 }
4905 
4906 static void *fpid_start(struct seq_file *m, loff_t *pos)
4907 {
4908 	mutex_lock(&ftrace_lock);
4909 
4910 	if (list_empty(&ftrace_pids) && (!*pos))
4911 		return (void *) 1;
4912 
4913 	return seq_list_start(&ftrace_pids, *pos);
4914 }
4915 
4916 static void *fpid_next(struct seq_file *m, void *v, loff_t *pos)
4917 {
4918 	if (v == (void *)1)
4919 		return NULL;
4920 
4921 	return seq_list_next(v, &ftrace_pids, pos);
4922 }
4923 
4924 static void fpid_stop(struct seq_file *m, void *p)
4925 {
4926 	mutex_unlock(&ftrace_lock);
4927 }
4928 
4929 static int fpid_show(struct seq_file *m, void *v)
4930 {
4931 	const struct ftrace_pid *fpid = list_entry(v, struct ftrace_pid, list);
4932 
4933 	if (v == (void *)1) {
4934 		seq_printf(m, "no pid\n");
4935 		return 0;
4936 	}
4937 
4938 	if (fpid->pid == ftrace_swapper_pid)
4939 		seq_printf(m, "swapper tasks\n");
4940 	else
4941 		seq_printf(m, "%u\n", pid_vnr(fpid->pid));
4942 
4943 	return 0;
4944 }
4945 
4946 static const struct seq_operations ftrace_pid_sops = {
4947 	.start = fpid_start,
4948 	.next = fpid_next,
4949 	.stop = fpid_stop,
4950 	.show = fpid_show,
4951 };
4952 
4953 static int
4954 ftrace_pid_open(struct inode *inode, struct file *file)
4955 {
4956 	int ret = 0;
4957 
4958 	if ((file->f_mode & FMODE_WRITE) &&
4959 	    (file->f_flags & O_TRUNC))
4960 		ftrace_pid_reset();
4961 
4962 	if (file->f_mode & FMODE_READ)
4963 		ret = seq_open(file, &ftrace_pid_sops);
4964 
4965 	return ret;
4966 }
4967 
4968 static ssize_t
4969 ftrace_pid_write(struct file *filp, const char __user *ubuf,
4970 		   size_t cnt, loff_t *ppos)
4971 {
4972 	char buf[64], *tmp;
4973 	long val;
4974 	int ret;
4975 
4976 	if (cnt >= sizeof(buf))
4977 		return -EINVAL;
4978 
4979 	if (copy_from_user(&buf, ubuf, cnt))
4980 		return -EFAULT;
4981 
4982 	buf[cnt] = 0;
4983 
4984 	/*
4985 	 * Allow "echo > set_ftrace_pid" or "echo -n '' > set_ftrace_pid"
4986 	 * to clean the filter quietly.
4987 	 */
4988 	tmp = strstrip(buf);
4989 	if (strlen(tmp) == 0)
4990 		return 1;
4991 
4992 	ret = kstrtol(tmp, 10, &val);
4993 	if (ret < 0)
4994 		return ret;
4995 
4996 	ret = ftrace_pid_add(val);
4997 
4998 	return ret ? ret : cnt;
4999 }
5000 
5001 static int
5002 ftrace_pid_release(struct inode *inode, struct file *file)
5003 {
5004 	if (file->f_mode & FMODE_READ)
5005 		seq_release(inode, file);
5006 
5007 	return 0;
5008 }
5009 
5010 static const struct file_operations ftrace_pid_fops = {
5011 	.open		= ftrace_pid_open,
5012 	.write		= ftrace_pid_write,
5013 	.read		= seq_read,
5014 	.llseek		= tracing_lseek,
5015 	.release	= ftrace_pid_release,
5016 };
5017 
5018 static __init int ftrace_init_debugfs(void)
5019 {
5020 	struct dentry *d_tracer;
5021 
5022 	d_tracer = tracing_init_dentry();
5023 	if (!d_tracer)
5024 		return 0;
5025 
5026 	ftrace_init_dyn_debugfs(d_tracer);
5027 
5028 	trace_create_file("set_ftrace_pid", 0644, d_tracer,
5029 			    NULL, &ftrace_pid_fops);
5030 
5031 	ftrace_profile_debugfs(d_tracer);
5032 
5033 	return 0;
5034 }
5035 fs_initcall(ftrace_init_debugfs);
5036 
5037 /**
5038  * ftrace_kill - kill ftrace
5039  *
5040  * This function should be used by panic code. It stops ftrace
5041  * but in a not so nice way. If you need to simply kill ftrace
5042  * from a non-atomic section, use ftrace_kill.
5043  */
5044 void ftrace_kill(void)
5045 {
5046 	ftrace_disabled = 1;
5047 	ftrace_enabled = 0;
5048 	clear_ftrace_function();
5049 }
5050 
5051 /**
5052  * Test if ftrace is dead or not.
5053  */
5054 int ftrace_is_dead(void)
5055 {
5056 	return ftrace_disabled;
5057 }
5058 
5059 /**
5060  * register_ftrace_function - register a function for profiling
5061  * @ops - ops structure that holds the function for profiling.
5062  *
5063  * Register a function to be called by all functions in the
5064  * kernel.
5065  *
5066  * Note: @ops->func and all the functions it calls must be labeled
5067  *       with "notrace", otherwise it will go into a
5068  *       recursive loop.
5069  */
5070 int register_ftrace_function(struct ftrace_ops *ops)
5071 {
5072 	int ret = -1;
5073 
5074 	ftrace_ops_init(ops);
5075 
5076 	mutex_lock(&ftrace_lock);
5077 
5078 	ret = ftrace_startup(ops, 0);
5079 
5080 	mutex_unlock(&ftrace_lock);
5081 
5082 	return ret;
5083 }
5084 EXPORT_SYMBOL_GPL(register_ftrace_function);
5085 
5086 /**
5087  * unregister_ftrace_function - unregister a function for profiling.
5088  * @ops - ops structure that holds the function to unregister
5089  *
5090  * Unregister a function that was added to be called by ftrace profiling.
5091  */
5092 int unregister_ftrace_function(struct ftrace_ops *ops)
5093 {
5094 	int ret;
5095 
5096 	mutex_lock(&ftrace_lock);
5097 	ret = ftrace_shutdown(ops, 0);
5098 	mutex_unlock(&ftrace_lock);
5099 
5100 	return ret;
5101 }
5102 EXPORT_SYMBOL_GPL(unregister_ftrace_function);
5103 
5104 int
5105 ftrace_enable_sysctl(struct ctl_table *table, int write,
5106 		     void __user *buffer, size_t *lenp,
5107 		     loff_t *ppos)
5108 {
5109 	int ret = -ENODEV;
5110 
5111 	mutex_lock(&ftrace_lock);
5112 
5113 	if (unlikely(ftrace_disabled))
5114 		goto out;
5115 
5116 	ret = proc_dointvec(table, write, buffer, lenp, ppos);
5117 
5118 	if (ret || !write || (last_ftrace_enabled == !!ftrace_enabled))
5119 		goto out;
5120 
5121 	last_ftrace_enabled = !!ftrace_enabled;
5122 
5123 	if (ftrace_enabled) {
5124 
5125 		ftrace_startup_sysctl();
5126 
5127 		/* we are starting ftrace again */
5128 		if (ftrace_ops_list != &ftrace_list_end)
5129 			update_ftrace_function();
5130 
5131 	} else {
5132 		/* stopping ftrace calls (just send to ftrace_stub) */
5133 		ftrace_trace_function = ftrace_stub;
5134 
5135 		ftrace_shutdown_sysctl();
5136 	}
5137 
5138  out:
5139 	mutex_unlock(&ftrace_lock);
5140 	return ret;
5141 }
5142 
5143 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
5144 
5145 static int ftrace_graph_active;
5146 
5147 int ftrace_graph_entry_stub(struct ftrace_graph_ent *trace)
5148 {
5149 	return 0;
5150 }
5151 
5152 /* The callbacks that hook a function */
5153 trace_func_graph_ret_t ftrace_graph_return =
5154 			(trace_func_graph_ret_t)ftrace_stub;
5155 trace_func_graph_ent_t ftrace_graph_entry = ftrace_graph_entry_stub;
5156 static trace_func_graph_ent_t __ftrace_graph_entry = ftrace_graph_entry_stub;
5157 
5158 /* Try to assign a return stack array on FTRACE_RETSTACK_ALLOC_SIZE tasks. */
5159 static int alloc_retstack_tasklist(struct ftrace_ret_stack **ret_stack_list)
5160 {
5161 	int i;
5162 	int ret = 0;
5163 	unsigned long flags;
5164 	int start = 0, end = FTRACE_RETSTACK_ALLOC_SIZE;
5165 	struct task_struct *g, *t;
5166 
5167 	for (i = 0; i < FTRACE_RETSTACK_ALLOC_SIZE; i++) {
5168 		ret_stack_list[i] = kmalloc(FTRACE_RETFUNC_DEPTH
5169 					* sizeof(struct ftrace_ret_stack),
5170 					GFP_KERNEL);
5171 		if (!ret_stack_list[i]) {
5172 			start = 0;
5173 			end = i;
5174 			ret = -ENOMEM;
5175 			goto free;
5176 		}
5177 	}
5178 
5179 	read_lock_irqsave(&tasklist_lock, flags);
5180 	do_each_thread(g, t) {
5181 		if (start == end) {
5182 			ret = -EAGAIN;
5183 			goto unlock;
5184 		}
5185 
5186 		if (t->ret_stack == NULL) {
5187 			atomic_set(&t->tracing_graph_pause, 0);
5188 			atomic_set(&t->trace_overrun, 0);
5189 			t->curr_ret_stack = -1;
5190 			/* Make sure the tasks see the -1 first: */
5191 			smp_wmb();
5192 			t->ret_stack = ret_stack_list[start++];
5193 		}
5194 	} while_each_thread(g, t);
5195 
5196 unlock:
5197 	read_unlock_irqrestore(&tasklist_lock, flags);
5198 free:
5199 	for (i = start; i < end; i++)
5200 		kfree(ret_stack_list[i]);
5201 	return ret;
5202 }
5203 
5204 static void
5205 ftrace_graph_probe_sched_switch(void *ignore,
5206 			struct task_struct *prev, struct task_struct *next)
5207 {
5208 	unsigned long long timestamp;
5209 	int index;
5210 
5211 	/*
5212 	 * Does the user want to count the time a function was asleep.
5213 	 * If so, do not update the time stamps.
5214 	 */
5215 	if (trace_flags & TRACE_ITER_SLEEP_TIME)
5216 		return;
5217 
5218 	timestamp = trace_clock_local();
5219 
5220 	prev->ftrace_timestamp = timestamp;
5221 
5222 	/* only process tasks that we timestamped */
5223 	if (!next->ftrace_timestamp)
5224 		return;
5225 
5226 	/*
5227 	 * Update all the counters in next to make up for the
5228 	 * time next was sleeping.
5229 	 */
5230 	timestamp -= next->ftrace_timestamp;
5231 
5232 	for (index = next->curr_ret_stack; index >= 0; index--)
5233 		next->ret_stack[index].calltime += timestamp;
5234 }
5235 
5236 /* Allocate a return stack for each task */
5237 static int start_graph_tracing(void)
5238 {
5239 	struct ftrace_ret_stack **ret_stack_list;
5240 	int ret, cpu;
5241 
5242 	ret_stack_list = kmalloc(FTRACE_RETSTACK_ALLOC_SIZE *
5243 				sizeof(struct ftrace_ret_stack *),
5244 				GFP_KERNEL);
5245 
5246 	if (!ret_stack_list)
5247 		return -ENOMEM;
5248 
5249 	/* The cpu_boot init_task->ret_stack will never be freed */
5250 	for_each_online_cpu(cpu) {
5251 		if (!idle_task(cpu)->ret_stack)
5252 			ftrace_graph_init_idle_task(idle_task(cpu), cpu);
5253 	}
5254 
5255 	do {
5256 		ret = alloc_retstack_tasklist(ret_stack_list);
5257 	} while (ret == -EAGAIN);
5258 
5259 	if (!ret) {
5260 		ret = register_trace_sched_switch(ftrace_graph_probe_sched_switch, NULL);
5261 		if (ret)
5262 			pr_info("ftrace_graph: Couldn't activate tracepoint"
5263 				" probe to kernel_sched_switch\n");
5264 	}
5265 
5266 	kfree(ret_stack_list);
5267 	return ret;
5268 }
5269 
5270 /*
5271  * Hibernation protection.
5272  * The state of the current task is too much unstable during
5273  * suspend/restore to disk. We want to protect against that.
5274  */
5275 static int
5276 ftrace_suspend_notifier_call(struct notifier_block *bl, unsigned long state,
5277 							void *unused)
5278 {
5279 	switch (state) {
5280 	case PM_HIBERNATION_PREPARE:
5281 		pause_graph_tracing();
5282 		break;
5283 
5284 	case PM_POST_HIBERNATION:
5285 		unpause_graph_tracing();
5286 		break;
5287 	}
5288 	return NOTIFY_DONE;
5289 }
5290 
5291 static int ftrace_graph_entry_test(struct ftrace_graph_ent *trace)
5292 {
5293 	if (!ftrace_ops_test(&global_ops, trace->func, NULL))
5294 		return 0;
5295 	return __ftrace_graph_entry(trace);
5296 }
5297 
5298 /*
5299  * The function graph tracer should only trace the functions defined
5300  * by set_ftrace_filter and set_ftrace_notrace. If another function
5301  * tracer ops is registered, the graph tracer requires testing the
5302  * function against the global ops, and not just trace any function
5303  * that any ftrace_ops registered.
5304  */
5305 static void update_function_graph_func(void)
5306 {
5307 	if (ftrace_ops_list == &ftrace_list_end ||
5308 	    (ftrace_ops_list == &global_ops &&
5309 	     global_ops.next == &ftrace_list_end))
5310 		ftrace_graph_entry = __ftrace_graph_entry;
5311 	else
5312 		ftrace_graph_entry = ftrace_graph_entry_test;
5313 }
5314 
5315 static struct notifier_block ftrace_suspend_notifier = {
5316 	.notifier_call = ftrace_suspend_notifier_call,
5317 };
5318 
5319 int register_ftrace_graph(trace_func_graph_ret_t retfunc,
5320 			trace_func_graph_ent_t entryfunc)
5321 {
5322 	int ret = 0;
5323 
5324 	mutex_lock(&ftrace_lock);
5325 
5326 	/* we currently allow only one tracer registered at a time */
5327 	if (ftrace_graph_active) {
5328 		ret = -EBUSY;
5329 		goto out;
5330 	}
5331 
5332 	register_pm_notifier(&ftrace_suspend_notifier);
5333 
5334 	ftrace_graph_active++;
5335 	ret = start_graph_tracing();
5336 	if (ret) {
5337 		ftrace_graph_active--;
5338 		goto out;
5339 	}
5340 
5341 	ftrace_graph_return = retfunc;
5342 
5343 	/*
5344 	 * Update the indirect function to the entryfunc, and the
5345 	 * function that gets called to the entry_test first. Then
5346 	 * call the update fgraph entry function to determine if
5347 	 * the entryfunc should be called directly or not.
5348 	 */
5349 	__ftrace_graph_entry = entryfunc;
5350 	ftrace_graph_entry = ftrace_graph_entry_test;
5351 	update_function_graph_func();
5352 
5353 	/* Function graph doesn't use the .func field of global_ops */
5354 	global_ops.flags |= FTRACE_OPS_FL_STUB;
5355 
5356 #ifdef CONFIG_DYNAMIC_FTRACE
5357 	/* Optimize function graph calling (if implemented by arch) */
5358 	if (FTRACE_GRAPH_TRAMP_ADDR != 0)
5359 		global_ops.trampoline = FTRACE_GRAPH_TRAMP_ADDR;
5360 #endif
5361 
5362 	ret = ftrace_startup(&global_ops, FTRACE_START_FUNC_RET);
5363 
5364 out:
5365 	mutex_unlock(&ftrace_lock);
5366 	return ret;
5367 }
5368 
5369 void unregister_ftrace_graph(void)
5370 {
5371 	mutex_lock(&ftrace_lock);
5372 
5373 	if (unlikely(!ftrace_graph_active))
5374 		goto out;
5375 
5376 	ftrace_graph_active--;
5377 	ftrace_graph_return = (trace_func_graph_ret_t)ftrace_stub;
5378 	ftrace_graph_entry = ftrace_graph_entry_stub;
5379 	__ftrace_graph_entry = ftrace_graph_entry_stub;
5380 	ftrace_shutdown(&global_ops, FTRACE_STOP_FUNC_RET);
5381 	global_ops.flags &= ~FTRACE_OPS_FL_STUB;
5382 #ifdef CONFIG_DYNAMIC_FTRACE
5383 	if (FTRACE_GRAPH_TRAMP_ADDR != 0)
5384 		global_ops.trampoline = 0;
5385 #endif
5386 	unregister_pm_notifier(&ftrace_suspend_notifier);
5387 	unregister_trace_sched_switch(ftrace_graph_probe_sched_switch, NULL);
5388 
5389  out:
5390 	mutex_unlock(&ftrace_lock);
5391 }
5392 
5393 static DEFINE_PER_CPU(struct ftrace_ret_stack *, idle_ret_stack);
5394 
5395 static void
5396 graph_init_task(struct task_struct *t, struct ftrace_ret_stack *ret_stack)
5397 {
5398 	atomic_set(&t->tracing_graph_pause, 0);
5399 	atomic_set(&t->trace_overrun, 0);
5400 	t->ftrace_timestamp = 0;
5401 	/* make curr_ret_stack visible before we add the ret_stack */
5402 	smp_wmb();
5403 	t->ret_stack = ret_stack;
5404 }
5405 
5406 /*
5407  * Allocate a return stack for the idle task. May be the first
5408  * time through, or it may be done by CPU hotplug online.
5409  */
5410 void ftrace_graph_init_idle_task(struct task_struct *t, int cpu)
5411 {
5412 	t->curr_ret_stack = -1;
5413 	/*
5414 	 * The idle task has no parent, it either has its own
5415 	 * stack or no stack at all.
5416 	 */
5417 	if (t->ret_stack)
5418 		WARN_ON(t->ret_stack != per_cpu(idle_ret_stack, cpu));
5419 
5420 	if (ftrace_graph_active) {
5421 		struct ftrace_ret_stack *ret_stack;
5422 
5423 		ret_stack = per_cpu(idle_ret_stack, cpu);
5424 		if (!ret_stack) {
5425 			ret_stack = kmalloc(FTRACE_RETFUNC_DEPTH
5426 					    * sizeof(struct ftrace_ret_stack),
5427 					    GFP_KERNEL);
5428 			if (!ret_stack)
5429 				return;
5430 			per_cpu(idle_ret_stack, cpu) = ret_stack;
5431 		}
5432 		graph_init_task(t, ret_stack);
5433 	}
5434 }
5435 
5436 /* Allocate a return stack for newly created task */
5437 void ftrace_graph_init_task(struct task_struct *t)
5438 {
5439 	/* Make sure we do not use the parent ret_stack */
5440 	t->ret_stack = NULL;
5441 	t->curr_ret_stack = -1;
5442 
5443 	if (ftrace_graph_active) {
5444 		struct ftrace_ret_stack *ret_stack;
5445 
5446 		ret_stack = kmalloc(FTRACE_RETFUNC_DEPTH
5447 				* sizeof(struct ftrace_ret_stack),
5448 				GFP_KERNEL);
5449 		if (!ret_stack)
5450 			return;
5451 		graph_init_task(t, ret_stack);
5452 	}
5453 }
5454 
5455 void ftrace_graph_exit_task(struct task_struct *t)
5456 {
5457 	struct ftrace_ret_stack	*ret_stack = t->ret_stack;
5458 
5459 	t->ret_stack = NULL;
5460 	/* NULL must become visible to IRQs before we free it: */
5461 	barrier();
5462 
5463 	kfree(ret_stack);
5464 }
5465 #endif
5466