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