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