xref: /linux-6.15/kernel/trace/trace_functions.c (revision c132be2c)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * ring buffer based function tracer
4  *
5  * Copyright (C) 2007-2008 Steven Rostedt <[email protected]>
6  * Copyright (C) 2008 Ingo Molnar <[email protected]>
7  *
8  * Based on code from the latency_tracer, that is:
9  *
10  *  Copyright (C) 2004-2006 Ingo Molnar
11  *  Copyright (C) 2004 Nadia Yvette Chambers
12  */
13 #include <linux/ring_buffer.h>
14 #include <linux/debugfs.h>
15 #include <linux/uaccess.h>
16 #include <linux/ftrace.h>
17 #include <linux/slab.h>
18 #include <linux/fs.h>
19 
20 #include "trace.h"
21 
22 static void tracing_start_function_trace(struct trace_array *tr);
23 static void tracing_stop_function_trace(struct trace_array *tr);
24 static void
25 function_trace_call(unsigned long ip, unsigned long parent_ip,
26 		    struct ftrace_ops *op, struct ftrace_regs *fregs);
27 static void
28 function_stack_trace_call(unsigned long ip, unsigned long parent_ip,
29 			  struct ftrace_ops *op, struct ftrace_regs *fregs);
30 static void
31 function_no_repeats_trace_call(unsigned long ip, unsigned long parent_ip,
32 			       struct ftrace_ops *op, struct ftrace_regs *fregs);
33 static void
34 function_stack_no_repeats_trace_call(unsigned long ip, unsigned long parent_ip,
35 				     struct ftrace_ops *op,
36 				     struct ftrace_regs *fregs);
37 static struct tracer_flags func_flags;
38 
39 /* Our option */
40 enum {
41 
42 	TRACE_FUNC_NO_OPTS		= 0x0, /* No flags set. */
43 	TRACE_FUNC_OPT_STACK		= 0x1,
44 	TRACE_FUNC_OPT_NO_REPEATS	= 0x2,
45 
46 	/* Update this to next highest bit. */
47 	TRACE_FUNC_OPT_HIGHEST_BIT	= 0x4
48 };
49 
50 #define TRACE_FUNC_OPT_MASK	(TRACE_FUNC_OPT_HIGHEST_BIT - 1)
51 
52 int ftrace_allocate_ftrace_ops(struct trace_array *tr)
53 {
54 	struct ftrace_ops *ops;
55 
56 	/* The top level array uses the "global_ops" */
57 	if (tr->flags & TRACE_ARRAY_FL_GLOBAL)
58 		return 0;
59 
60 	ops = kzalloc(sizeof(*ops), GFP_KERNEL);
61 	if (!ops)
62 		return -ENOMEM;
63 
64 	/* Currently only the non stack version is supported */
65 	ops->func = function_trace_call;
66 	ops->flags = FTRACE_OPS_FL_PID;
67 
68 	tr->ops = ops;
69 	ops->private = tr;
70 
71 	return 0;
72 }
73 
74 void ftrace_free_ftrace_ops(struct trace_array *tr)
75 {
76 	kfree(tr->ops);
77 	tr->ops = NULL;
78 }
79 
80 int ftrace_create_function_files(struct trace_array *tr,
81 				 struct dentry *parent)
82 {
83 	int ret;
84 	/*
85 	 * The top level array uses the "global_ops", and the files are
86 	 * created on boot up.
87 	 */
88 	if (tr->flags & TRACE_ARRAY_FL_GLOBAL)
89 		return 0;
90 
91 	if (!tr->ops)
92 		return -EINVAL;
93 
94 	ret = allocate_fgraph_ops(tr, tr->ops);
95 	if (ret) {
96 		kfree(tr->ops);
97 		return ret;
98 	}
99 
100 	ftrace_create_filter_files(tr->ops, parent);
101 
102 	return 0;
103 }
104 
105 void ftrace_destroy_function_files(struct trace_array *tr)
106 {
107 	ftrace_destroy_filter_files(tr->ops);
108 	ftrace_free_ftrace_ops(tr);
109 	free_fgraph_ops(tr);
110 }
111 
112 static ftrace_func_t select_trace_function(u32 flags_val)
113 {
114 	switch (flags_val & TRACE_FUNC_OPT_MASK) {
115 	case TRACE_FUNC_NO_OPTS:
116 		return function_trace_call;
117 	case TRACE_FUNC_OPT_STACK:
118 		return function_stack_trace_call;
119 	case TRACE_FUNC_OPT_NO_REPEATS:
120 		return function_no_repeats_trace_call;
121 	case TRACE_FUNC_OPT_STACK | TRACE_FUNC_OPT_NO_REPEATS:
122 		return function_stack_no_repeats_trace_call;
123 	default:
124 		return NULL;
125 	}
126 }
127 
128 static bool handle_func_repeats(struct trace_array *tr, u32 flags_val)
129 {
130 	if (!tr->last_func_repeats &&
131 	    (flags_val & TRACE_FUNC_OPT_NO_REPEATS)) {
132 		tr->last_func_repeats = alloc_percpu(struct trace_func_repeats);
133 		if (!tr->last_func_repeats)
134 			return false;
135 	}
136 
137 	return true;
138 }
139 
140 static int function_trace_init(struct trace_array *tr)
141 {
142 	ftrace_func_t func;
143 	/*
144 	 * Instance trace_arrays get their ops allocated
145 	 * at instance creation. Unless it failed
146 	 * the allocation.
147 	 */
148 	if (!tr->ops)
149 		return -ENOMEM;
150 
151 	func = select_trace_function(func_flags.val);
152 	if (!func)
153 		return -EINVAL;
154 
155 	if (!handle_func_repeats(tr, func_flags.val))
156 		return -ENOMEM;
157 
158 	ftrace_init_array_ops(tr, func);
159 
160 	tr->array_buffer.cpu = raw_smp_processor_id();
161 
162 	tracing_start_cmdline_record();
163 	tracing_start_function_trace(tr);
164 	return 0;
165 }
166 
167 static void function_trace_reset(struct trace_array *tr)
168 {
169 	tracing_stop_function_trace(tr);
170 	tracing_stop_cmdline_record();
171 	ftrace_reset_array_ops(tr);
172 }
173 
174 static void function_trace_start(struct trace_array *tr)
175 {
176 	tracing_reset_online_cpus(&tr->array_buffer);
177 }
178 
179 static void
180 function_trace_call(unsigned long ip, unsigned long parent_ip,
181 		    struct ftrace_ops *op, struct ftrace_regs *fregs)
182 {
183 	struct trace_array *tr = op->private;
184 	struct trace_array_cpu *data;
185 	unsigned int trace_ctx;
186 	int bit;
187 	int cpu;
188 
189 	if (unlikely(!tr->function_enabled))
190 		return;
191 
192 	bit = ftrace_test_recursion_trylock(ip, parent_ip);
193 	if (bit < 0)
194 		return;
195 
196 	trace_ctx = tracing_gen_ctx();
197 
198 	cpu = smp_processor_id();
199 	data = per_cpu_ptr(tr->array_buffer.data, cpu);
200 	if (!atomic_read(&data->disabled))
201 		trace_function(tr, ip, parent_ip, trace_ctx);
202 
203 	ftrace_test_recursion_unlock(bit);
204 }
205 
206 #ifdef CONFIG_UNWINDER_ORC
207 /*
208  * Skip 2:
209  *
210  *   function_stack_trace_call()
211  *   ftrace_call()
212  */
213 #define STACK_SKIP 2
214 #else
215 /*
216  * Skip 3:
217  *   __trace_stack()
218  *   function_stack_trace_call()
219  *   ftrace_call()
220  */
221 #define STACK_SKIP 3
222 #endif
223 
224 static void
225 function_stack_trace_call(unsigned long ip, unsigned long parent_ip,
226 			  struct ftrace_ops *op, struct ftrace_regs *fregs)
227 {
228 	struct trace_array *tr = op->private;
229 	struct trace_array_cpu *data;
230 	unsigned long flags;
231 	long disabled;
232 	int cpu;
233 	unsigned int trace_ctx;
234 
235 	if (unlikely(!tr->function_enabled))
236 		return;
237 
238 	/*
239 	 * Need to use raw, since this must be called before the
240 	 * recursive protection is performed.
241 	 */
242 	local_irq_save(flags);
243 	cpu = raw_smp_processor_id();
244 	data = per_cpu_ptr(tr->array_buffer.data, cpu);
245 	disabled = atomic_inc_return(&data->disabled);
246 
247 	if (likely(disabled == 1)) {
248 		trace_ctx = tracing_gen_ctx_flags(flags);
249 		trace_function(tr, ip, parent_ip, trace_ctx);
250 		__trace_stack(tr, trace_ctx, STACK_SKIP);
251 	}
252 
253 	atomic_dec(&data->disabled);
254 	local_irq_restore(flags);
255 }
256 
257 static inline bool is_repeat_check(struct trace_array *tr,
258 				   struct trace_func_repeats *last_info,
259 				   unsigned long ip, unsigned long parent_ip)
260 {
261 	if (last_info->ip == ip &&
262 	    last_info->parent_ip == parent_ip &&
263 	    last_info->count < U16_MAX) {
264 		last_info->ts_last_call =
265 			ring_buffer_time_stamp(tr->array_buffer.buffer);
266 		last_info->count++;
267 		return true;
268 	}
269 
270 	return false;
271 }
272 
273 static inline void process_repeats(struct trace_array *tr,
274 				   unsigned long ip, unsigned long parent_ip,
275 				   struct trace_func_repeats *last_info,
276 				   unsigned int trace_ctx)
277 {
278 	if (last_info->count) {
279 		trace_last_func_repeats(tr, last_info, trace_ctx);
280 		last_info->count = 0;
281 	}
282 
283 	last_info->ip = ip;
284 	last_info->parent_ip = parent_ip;
285 }
286 
287 static void
288 function_no_repeats_trace_call(unsigned long ip, unsigned long parent_ip,
289 			       struct ftrace_ops *op,
290 			       struct ftrace_regs *fregs)
291 {
292 	struct trace_func_repeats *last_info;
293 	struct trace_array *tr = op->private;
294 	struct trace_array_cpu *data;
295 	unsigned int trace_ctx;
296 	unsigned long flags;
297 	int bit;
298 	int cpu;
299 
300 	if (unlikely(!tr->function_enabled))
301 		return;
302 
303 	bit = ftrace_test_recursion_trylock(ip, parent_ip);
304 	if (bit < 0)
305 		return;
306 
307 	cpu = smp_processor_id();
308 	data = per_cpu_ptr(tr->array_buffer.data, cpu);
309 	if (atomic_read(&data->disabled))
310 		goto out;
311 
312 	/*
313 	 * An interrupt may happen at any place here. But as far as I can see,
314 	 * the only damage that this can cause is to mess up the repetition
315 	 * counter without valuable data being lost.
316 	 * TODO: think about a solution that is better than just hoping to be
317 	 * lucky.
318 	 */
319 	last_info = per_cpu_ptr(tr->last_func_repeats, cpu);
320 	if (is_repeat_check(tr, last_info, ip, parent_ip))
321 		goto out;
322 
323 	local_save_flags(flags);
324 	trace_ctx = tracing_gen_ctx_flags(flags);
325 	process_repeats(tr, ip, parent_ip, last_info, trace_ctx);
326 
327 	trace_function(tr, ip, parent_ip, trace_ctx);
328 
329 out:
330 	ftrace_test_recursion_unlock(bit);
331 }
332 
333 static void
334 function_stack_no_repeats_trace_call(unsigned long ip, unsigned long parent_ip,
335 				     struct ftrace_ops *op,
336 				     struct ftrace_regs *fregs)
337 {
338 	struct trace_func_repeats *last_info;
339 	struct trace_array *tr = op->private;
340 	struct trace_array_cpu *data;
341 	unsigned long flags;
342 	long disabled;
343 	int cpu;
344 	unsigned int trace_ctx;
345 
346 	if (unlikely(!tr->function_enabled))
347 		return;
348 
349 	/*
350 	 * Need to use raw, since this must be called before the
351 	 * recursive protection is performed.
352 	 */
353 	local_irq_save(flags);
354 	cpu = raw_smp_processor_id();
355 	data = per_cpu_ptr(tr->array_buffer.data, cpu);
356 	disabled = atomic_inc_return(&data->disabled);
357 
358 	if (likely(disabled == 1)) {
359 		last_info = per_cpu_ptr(tr->last_func_repeats, cpu);
360 		if (is_repeat_check(tr, last_info, ip, parent_ip))
361 			goto out;
362 
363 		trace_ctx = tracing_gen_ctx_flags(flags);
364 		process_repeats(tr, ip, parent_ip, last_info, trace_ctx);
365 
366 		trace_function(tr, ip, parent_ip, trace_ctx);
367 		__trace_stack(tr, trace_ctx, STACK_SKIP);
368 	}
369 
370  out:
371 	atomic_dec(&data->disabled);
372 	local_irq_restore(flags);
373 }
374 
375 static struct tracer_opt func_opts[] = {
376 #ifdef CONFIG_STACKTRACE
377 	{ TRACER_OPT(func_stack_trace, TRACE_FUNC_OPT_STACK) },
378 #endif
379 	{ TRACER_OPT(func-no-repeats, TRACE_FUNC_OPT_NO_REPEATS) },
380 	{ } /* Always set a last empty entry */
381 };
382 
383 static struct tracer_flags func_flags = {
384 	.val = TRACE_FUNC_NO_OPTS, /* By default: all flags disabled */
385 	.opts = func_opts
386 };
387 
388 static void tracing_start_function_trace(struct trace_array *tr)
389 {
390 	tr->function_enabled = 0;
391 	register_ftrace_function(tr->ops);
392 	tr->function_enabled = 1;
393 }
394 
395 static void tracing_stop_function_trace(struct trace_array *tr)
396 {
397 	tr->function_enabled = 0;
398 	unregister_ftrace_function(tr->ops);
399 }
400 
401 static struct tracer function_trace;
402 
403 static int
404 func_set_flag(struct trace_array *tr, u32 old_flags, u32 bit, int set)
405 {
406 	ftrace_func_t func;
407 	u32 new_flags;
408 
409 	/* Do nothing if already set. */
410 	if (!!set == !!(func_flags.val & bit))
411 		return 0;
412 
413 	/* We can change this flag only when not running. */
414 	if (tr->current_trace != &function_trace)
415 		return 0;
416 
417 	new_flags = (func_flags.val & ~bit) | (set ? bit : 0);
418 	func = select_trace_function(new_flags);
419 	if (!func)
420 		return -EINVAL;
421 
422 	/* Check if there's anything to change. */
423 	if (tr->ops->func == func)
424 		return 0;
425 
426 	if (!handle_func_repeats(tr, new_flags))
427 		return -ENOMEM;
428 
429 	unregister_ftrace_function(tr->ops);
430 	tr->ops->func = func;
431 	register_ftrace_function(tr->ops);
432 
433 	return 0;
434 }
435 
436 static struct tracer function_trace __tracer_data =
437 {
438 	.name		= "function",
439 	.init		= function_trace_init,
440 	.reset		= function_trace_reset,
441 	.start		= function_trace_start,
442 	.flags		= &func_flags,
443 	.set_flag	= func_set_flag,
444 	.allow_instances = true,
445 #ifdef CONFIG_FTRACE_SELFTEST
446 	.selftest	= trace_selftest_startup_function,
447 #endif
448 };
449 
450 #ifdef CONFIG_DYNAMIC_FTRACE
451 static void update_traceon_count(struct ftrace_probe_ops *ops,
452 				 unsigned long ip,
453 				 struct trace_array *tr, bool on,
454 				 void *data)
455 {
456 	struct ftrace_func_mapper *mapper = data;
457 	long *count;
458 	long old_count;
459 
460 	/*
461 	 * Tracing gets disabled (or enabled) once per count.
462 	 * This function can be called at the same time on multiple CPUs.
463 	 * It is fine if both disable (or enable) tracing, as disabling
464 	 * (or enabling) the second time doesn't do anything as the
465 	 * state of the tracer is already disabled (or enabled).
466 	 * What needs to be synchronized in this case is that the count
467 	 * only gets decremented once, even if the tracer is disabled
468 	 * (or enabled) twice, as the second one is really a nop.
469 	 *
470 	 * The memory barriers guarantee that we only decrement the
471 	 * counter once. First the count is read to a local variable
472 	 * and a read barrier is used to make sure that it is loaded
473 	 * before checking if the tracer is in the state we want.
474 	 * If the tracer is not in the state we want, then the count
475 	 * is guaranteed to be the old count.
476 	 *
477 	 * Next the tracer is set to the state we want (disabled or enabled)
478 	 * then a write memory barrier is used to make sure that
479 	 * the new state is visible before changing the counter by
480 	 * one minus the old counter. This guarantees that another CPU
481 	 * executing this code will see the new state before seeing
482 	 * the new counter value, and would not do anything if the new
483 	 * counter is seen.
484 	 *
485 	 * Note, there is no synchronization between this and a user
486 	 * setting the tracing_on file. But we currently don't care
487 	 * about that.
488 	 */
489 	count = (long *)ftrace_func_mapper_find_ip(mapper, ip);
490 	old_count = *count;
491 
492 	if (old_count <= 0)
493 		return;
494 
495 	/* Make sure we see count before checking tracing state */
496 	smp_rmb();
497 
498 	if (on == !!tracer_tracing_is_on(tr))
499 		return;
500 
501 	if (on)
502 		tracer_tracing_on(tr);
503 	else
504 		tracer_tracing_off(tr);
505 
506 	/* Make sure tracing state is visible before updating count */
507 	smp_wmb();
508 
509 	*count = old_count - 1;
510 }
511 
512 static void
513 ftrace_traceon_count(unsigned long ip, unsigned long parent_ip,
514 		     struct trace_array *tr, struct ftrace_probe_ops *ops,
515 		     void *data)
516 {
517 	update_traceon_count(ops, ip, tr, 1, data);
518 }
519 
520 static void
521 ftrace_traceoff_count(unsigned long ip, unsigned long parent_ip,
522 		      struct trace_array *tr, struct ftrace_probe_ops *ops,
523 		      void *data)
524 {
525 	update_traceon_count(ops, ip, tr, 0, data);
526 }
527 
528 static void
529 ftrace_traceon(unsigned long ip, unsigned long parent_ip,
530 	       struct trace_array *tr, struct ftrace_probe_ops *ops,
531 	       void *data)
532 {
533 	if (tracer_tracing_is_on(tr))
534 		return;
535 
536 	tracer_tracing_on(tr);
537 }
538 
539 static void
540 ftrace_traceoff(unsigned long ip, unsigned long parent_ip,
541 		struct trace_array *tr, struct ftrace_probe_ops *ops,
542 		void *data)
543 {
544 	if (!tracer_tracing_is_on(tr))
545 		return;
546 
547 	tracer_tracing_off(tr);
548 }
549 
550 #ifdef CONFIG_UNWINDER_ORC
551 /*
552  * Skip 3:
553  *
554  *   function_trace_probe_call()
555  *   ftrace_ops_assist_func()
556  *   ftrace_call()
557  */
558 #define FTRACE_STACK_SKIP 3
559 #else
560 /*
561  * Skip 5:
562  *
563  *   __trace_stack()
564  *   ftrace_stacktrace()
565  *   function_trace_probe_call()
566  *   ftrace_ops_assist_func()
567  *   ftrace_call()
568  */
569 #define FTRACE_STACK_SKIP 5
570 #endif
571 
572 static __always_inline void trace_stack(struct trace_array *tr)
573 {
574 	unsigned int trace_ctx;
575 
576 	trace_ctx = tracing_gen_ctx();
577 
578 	__trace_stack(tr, trace_ctx, FTRACE_STACK_SKIP);
579 }
580 
581 static void
582 ftrace_stacktrace(unsigned long ip, unsigned long parent_ip,
583 		  struct trace_array *tr, struct ftrace_probe_ops *ops,
584 		  void *data)
585 {
586 	trace_stack(tr);
587 }
588 
589 static void
590 ftrace_stacktrace_count(unsigned long ip, unsigned long parent_ip,
591 			struct trace_array *tr, struct ftrace_probe_ops *ops,
592 			void *data)
593 {
594 	struct ftrace_func_mapper *mapper = data;
595 	long *count;
596 	long old_count;
597 	long new_count;
598 
599 	if (!tracing_is_on())
600 		return;
601 
602 	/* unlimited? */
603 	if (!mapper) {
604 		trace_stack(tr);
605 		return;
606 	}
607 
608 	count = (long *)ftrace_func_mapper_find_ip(mapper, ip);
609 
610 	/*
611 	 * Stack traces should only execute the number of times the
612 	 * user specified in the counter.
613 	 */
614 	do {
615 		old_count = *count;
616 
617 		if (!old_count)
618 			return;
619 
620 		new_count = old_count - 1;
621 		new_count = cmpxchg(count, old_count, new_count);
622 		if (new_count == old_count)
623 			trace_stack(tr);
624 
625 		if (!tracing_is_on())
626 			return;
627 
628 	} while (new_count != old_count);
629 }
630 
631 static int update_count(struct ftrace_probe_ops *ops, unsigned long ip,
632 			void *data)
633 {
634 	struct ftrace_func_mapper *mapper = data;
635 	long *count = NULL;
636 
637 	if (mapper)
638 		count = (long *)ftrace_func_mapper_find_ip(mapper, ip);
639 
640 	if (count) {
641 		if (*count <= 0)
642 			return 0;
643 		(*count)--;
644 	}
645 
646 	return 1;
647 }
648 
649 static void
650 ftrace_dump_probe(unsigned long ip, unsigned long parent_ip,
651 		  struct trace_array *tr, struct ftrace_probe_ops *ops,
652 		  void *data)
653 {
654 	if (update_count(ops, ip, data))
655 		ftrace_dump(DUMP_ALL);
656 }
657 
658 /* Only dump the current CPU buffer. */
659 static void
660 ftrace_cpudump_probe(unsigned long ip, unsigned long parent_ip,
661 		     struct trace_array *tr, struct ftrace_probe_ops *ops,
662 		     void *data)
663 {
664 	if (update_count(ops, ip, data))
665 		ftrace_dump(DUMP_ORIG);
666 }
667 
668 static int
669 ftrace_probe_print(const char *name, struct seq_file *m,
670 		   unsigned long ip, struct ftrace_probe_ops *ops,
671 		   void *data)
672 {
673 	struct ftrace_func_mapper *mapper = data;
674 	long *count = NULL;
675 
676 	seq_printf(m, "%ps:%s", (void *)ip, name);
677 
678 	if (mapper)
679 		count = (long *)ftrace_func_mapper_find_ip(mapper, ip);
680 
681 	if (count)
682 		seq_printf(m, ":count=%ld\n", *count);
683 	else
684 		seq_puts(m, ":unlimited\n");
685 
686 	return 0;
687 }
688 
689 static int
690 ftrace_traceon_print(struct seq_file *m, unsigned long ip,
691 		     struct ftrace_probe_ops *ops,
692 		     void *data)
693 {
694 	return ftrace_probe_print("traceon", m, ip, ops, data);
695 }
696 
697 static int
698 ftrace_traceoff_print(struct seq_file *m, unsigned long ip,
699 			 struct ftrace_probe_ops *ops, void *data)
700 {
701 	return ftrace_probe_print("traceoff", m, ip, ops, data);
702 }
703 
704 static int
705 ftrace_stacktrace_print(struct seq_file *m, unsigned long ip,
706 			struct ftrace_probe_ops *ops, void *data)
707 {
708 	return ftrace_probe_print("stacktrace", m, ip, ops, data);
709 }
710 
711 static int
712 ftrace_dump_print(struct seq_file *m, unsigned long ip,
713 			struct ftrace_probe_ops *ops, void *data)
714 {
715 	return ftrace_probe_print("dump", m, ip, ops, data);
716 }
717 
718 static int
719 ftrace_cpudump_print(struct seq_file *m, unsigned long ip,
720 			struct ftrace_probe_ops *ops, void *data)
721 {
722 	return ftrace_probe_print("cpudump", m, ip, ops, data);
723 }
724 
725 
726 static int
727 ftrace_count_init(struct ftrace_probe_ops *ops, struct trace_array *tr,
728 		  unsigned long ip, void *init_data, void **data)
729 {
730 	struct ftrace_func_mapper *mapper = *data;
731 
732 	if (!mapper) {
733 		mapper = allocate_ftrace_func_mapper();
734 		if (!mapper)
735 			return -ENOMEM;
736 		*data = mapper;
737 	}
738 
739 	return ftrace_func_mapper_add_ip(mapper, ip, init_data);
740 }
741 
742 static void
743 ftrace_count_free(struct ftrace_probe_ops *ops, struct trace_array *tr,
744 		  unsigned long ip, void *data)
745 {
746 	struct ftrace_func_mapper *mapper = data;
747 
748 	if (!ip) {
749 		free_ftrace_func_mapper(mapper, NULL);
750 		return;
751 	}
752 
753 	ftrace_func_mapper_remove_ip(mapper, ip);
754 }
755 
756 static struct ftrace_probe_ops traceon_count_probe_ops = {
757 	.func			= ftrace_traceon_count,
758 	.print			= ftrace_traceon_print,
759 	.init			= ftrace_count_init,
760 	.free			= ftrace_count_free,
761 };
762 
763 static struct ftrace_probe_ops traceoff_count_probe_ops = {
764 	.func			= ftrace_traceoff_count,
765 	.print			= ftrace_traceoff_print,
766 	.init			= ftrace_count_init,
767 	.free			= ftrace_count_free,
768 };
769 
770 static struct ftrace_probe_ops stacktrace_count_probe_ops = {
771 	.func			= ftrace_stacktrace_count,
772 	.print			= ftrace_stacktrace_print,
773 	.init			= ftrace_count_init,
774 	.free			= ftrace_count_free,
775 };
776 
777 static struct ftrace_probe_ops dump_probe_ops = {
778 	.func			= ftrace_dump_probe,
779 	.print			= ftrace_dump_print,
780 	.init			= ftrace_count_init,
781 	.free			= ftrace_count_free,
782 };
783 
784 static struct ftrace_probe_ops cpudump_probe_ops = {
785 	.func			= ftrace_cpudump_probe,
786 	.print			= ftrace_cpudump_print,
787 };
788 
789 static struct ftrace_probe_ops traceon_probe_ops = {
790 	.func			= ftrace_traceon,
791 	.print			= ftrace_traceon_print,
792 };
793 
794 static struct ftrace_probe_ops traceoff_probe_ops = {
795 	.func			= ftrace_traceoff,
796 	.print			= ftrace_traceoff_print,
797 };
798 
799 static struct ftrace_probe_ops stacktrace_probe_ops = {
800 	.func			= ftrace_stacktrace,
801 	.print			= ftrace_stacktrace_print,
802 };
803 
804 static int
805 ftrace_trace_probe_callback(struct trace_array *tr,
806 			    struct ftrace_probe_ops *ops,
807 			    struct ftrace_hash *hash, char *glob,
808 			    char *cmd, char *param, int enable)
809 {
810 	void *count = (void *)-1;
811 	char *number;
812 	int ret;
813 
814 	/* hash funcs only work with set_ftrace_filter */
815 	if (!enable)
816 		return -EINVAL;
817 
818 	if (glob[0] == '!')
819 		return unregister_ftrace_function_probe_func(glob+1, tr, ops);
820 
821 	if (!param)
822 		goto out_reg;
823 
824 	number = strsep(&param, ":");
825 
826 	if (!strlen(number))
827 		goto out_reg;
828 
829 	/*
830 	 * We use the callback data field (which is a pointer)
831 	 * as our counter.
832 	 */
833 	ret = kstrtoul(number, 0, (unsigned long *)&count);
834 	if (ret)
835 		return ret;
836 
837  out_reg:
838 	ret = register_ftrace_function_probe(glob, tr, ops, count);
839 
840 	return ret < 0 ? ret : 0;
841 }
842 
843 static int
844 ftrace_trace_onoff_callback(struct trace_array *tr, struct ftrace_hash *hash,
845 			    char *glob, char *cmd, char *param, int enable)
846 {
847 	struct ftrace_probe_ops *ops;
848 
849 	if (!tr)
850 		return -ENODEV;
851 
852 	/* we register both traceon and traceoff to this callback */
853 	if (strcmp(cmd, "traceon") == 0)
854 		ops = param ? &traceon_count_probe_ops : &traceon_probe_ops;
855 	else
856 		ops = param ? &traceoff_count_probe_ops : &traceoff_probe_ops;
857 
858 	return ftrace_trace_probe_callback(tr, ops, hash, glob, cmd,
859 					   param, enable);
860 }
861 
862 static int
863 ftrace_stacktrace_callback(struct trace_array *tr, struct ftrace_hash *hash,
864 			   char *glob, char *cmd, char *param, int enable)
865 {
866 	struct ftrace_probe_ops *ops;
867 
868 	if (!tr)
869 		return -ENODEV;
870 
871 	ops = param ? &stacktrace_count_probe_ops : &stacktrace_probe_ops;
872 
873 	return ftrace_trace_probe_callback(tr, ops, hash, glob, cmd,
874 					   param, enable);
875 }
876 
877 static int
878 ftrace_dump_callback(struct trace_array *tr, struct ftrace_hash *hash,
879 			   char *glob, char *cmd, char *param, int enable)
880 {
881 	struct ftrace_probe_ops *ops;
882 
883 	if (!tr)
884 		return -ENODEV;
885 
886 	ops = &dump_probe_ops;
887 
888 	/* Only dump once. */
889 	return ftrace_trace_probe_callback(tr, ops, hash, glob, cmd,
890 					   "1", enable);
891 }
892 
893 static int
894 ftrace_cpudump_callback(struct trace_array *tr, struct ftrace_hash *hash,
895 			   char *glob, char *cmd, char *param, int enable)
896 {
897 	struct ftrace_probe_ops *ops;
898 
899 	if (!tr)
900 		return -ENODEV;
901 
902 	ops = &cpudump_probe_ops;
903 
904 	/* Only dump once. */
905 	return ftrace_trace_probe_callback(tr, ops, hash, glob, cmd,
906 					   "1", enable);
907 }
908 
909 static struct ftrace_func_command ftrace_traceon_cmd = {
910 	.name			= "traceon",
911 	.func			= ftrace_trace_onoff_callback,
912 };
913 
914 static struct ftrace_func_command ftrace_traceoff_cmd = {
915 	.name			= "traceoff",
916 	.func			= ftrace_trace_onoff_callback,
917 };
918 
919 static struct ftrace_func_command ftrace_stacktrace_cmd = {
920 	.name			= "stacktrace",
921 	.func			= ftrace_stacktrace_callback,
922 };
923 
924 static struct ftrace_func_command ftrace_dump_cmd = {
925 	.name			= "dump",
926 	.func			= ftrace_dump_callback,
927 };
928 
929 static struct ftrace_func_command ftrace_cpudump_cmd = {
930 	.name			= "cpudump",
931 	.func			= ftrace_cpudump_callback,
932 };
933 
934 static int __init init_func_cmd_traceon(void)
935 {
936 	int ret;
937 
938 	ret = register_ftrace_command(&ftrace_traceoff_cmd);
939 	if (ret)
940 		return ret;
941 
942 	ret = register_ftrace_command(&ftrace_traceon_cmd);
943 	if (ret)
944 		goto out_free_traceoff;
945 
946 	ret = register_ftrace_command(&ftrace_stacktrace_cmd);
947 	if (ret)
948 		goto out_free_traceon;
949 
950 	ret = register_ftrace_command(&ftrace_dump_cmd);
951 	if (ret)
952 		goto out_free_stacktrace;
953 
954 	ret = register_ftrace_command(&ftrace_cpudump_cmd);
955 	if (ret)
956 		goto out_free_dump;
957 
958 	return 0;
959 
960  out_free_dump:
961 	unregister_ftrace_command(&ftrace_dump_cmd);
962  out_free_stacktrace:
963 	unregister_ftrace_command(&ftrace_stacktrace_cmd);
964  out_free_traceon:
965 	unregister_ftrace_command(&ftrace_traceon_cmd);
966  out_free_traceoff:
967 	unregister_ftrace_command(&ftrace_traceoff_cmd);
968 
969 	return ret;
970 }
971 #else
972 static inline int init_func_cmd_traceon(void)
973 {
974 	return 0;
975 }
976 #endif /* CONFIG_DYNAMIC_FTRACE */
977 
978 __init int init_function_trace(void)
979 {
980 	init_func_cmd_traceon();
981 	return register_tracer(&function_trace);
982 }
983