Merge tag 'usb-5.11-rc3' of git://git.kernel.org/pub/scm/linux/kernel/git/gregkh/usb
[linux/fpc-iii.git] / kernel / trace / trace_functions.c
blobc5095dd28e20c778369f07d15652fda6ecbe40ff
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * ring buffer based function tracer
5 * Copyright (C) 2007-2008 Steven Rostedt <srostedt@redhat.com>
6 * Copyright (C) 2008 Ingo Molnar <mingo@redhat.com>
8 * Based on code from the latency_tracer, that is:
10 * Copyright (C) 2004-2006 Ingo Molnar
11 * Copyright (C) 2004 Nadia Yvette Chambers
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>
20 #include "trace.h"
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 struct tracer_flags func_flags;
32 /* Our option */
33 enum {
34 TRACE_FUNC_OPT_STACK = 0x1,
37 int ftrace_allocate_ftrace_ops(struct trace_array *tr)
39 struct ftrace_ops *ops;
41 /* The top level array uses the "global_ops" */
42 if (tr->flags & TRACE_ARRAY_FL_GLOBAL)
43 return 0;
45 ops = kzalloc(sizeof(*ops), GFP_KERNEL);
46 if (!ops)
47 return -ENOMEM;
49 /* Currently only the non stack version is supported */
50 ops->func = function_trace_call;
51 ops->flags = FTRACE_OPS_FL_PID;
53 tr->ops = ops;
54 ops->private = tr;
56 return 0;
59 void ftrace_free_ftrace_ops(struct trace_array *tr)
61 kfree(tr->ops);
62 tr->ops = NULL;
65 int ftrace_create_function_files(struct trace_array *tr,
66 struct dentry *parent)
69 * The top level array uses the "global_ops", and the files are
70 * created on boot up.
72 if (tr->flags & TRACE_ARRAY_FL_GLOBAL)
73 return 0;
75 if (!tr->ops)
76 return -EINVAL;
78 ftrace_create_filter_files(tr->ops, parent);
80 return 0;
83 void ftrace_destroy_function_files(struct trace_array *tr)
85 ftrace_destroy_filter_files(tr->ops);
86 ftrace_free_ftrace_ops(tr);
89 static int function_trace_init(struct trace_array *tr)
91 ftrace_func_t func;
93 * Instance trace_arrays get their ops allocated
94 * at instance creation. Unless it failed
95 * the allocation.
97 if (!tr->ops)
98 return -ENOMEM;
100 /* Currently only the global instance can do stack tracing */
101 if (tr->flags & TRACE_ARRAY_FL_GLOBAL &&
102 func_flags.val & TRACE_FUNC_OPT_STACK)
103 func = function_stack_trace_call;
104 else
105 func = function_trace_call;
107 ftrace_init_array_ops(tr, func);
109 tr->array_buffer.cpu = get_cpu();
110 put_cpu();
112 tracing_start_cmdline_record();
113 tracing_start_function_trace(tr);
114 return 0;
117 static void function_trace_reset(struct trace_array *tr)
119 tracing_stop_function_trace(tr);
120 tracing_stop_cmdline_record();
121 ftrace_reset_array_ops(tr);
124 static void function_trace_start(struct trace_array *tr)
126 tracing_reset_online_cpus(&tr->array_buffer);
129 static void
130 function_trace_call(unsigned long ip, unsigned long parent_ip,
131 struct ftrace_ops *op, struct ftrace_regs *fregs)
133 struct trace_array *tr = op->private;
134 struct trace_array_cpu *data;
135 unsigned long flags;
136 int bit;
137 int cpu;
138 int pc;
140 if (unlikely(!tr->function_enabled))
141 return;
143 bit = ftrace_test_recursion_trylock(ip, parent_ip);
144 if (bit < 0)
145 return;
147 pc = preempt_count();
148 preempt_disable_notrace();
150 cpu = smp_processor_id();
151 data = per_cpu_ptr(tr->array_buffer.data, cpu);
152 if (!atomic_read(&data->disabled)) {
153 local_save_flags(flags);
154 trace_function(tr, ip, parent_ip, flags, pc);
156 ftrace_test_recursion_unlock(bit);
157 preempt_enable_notrace();
160 #ifdef CONFIG_UNWINDER_ORC
162 * Skip 2:
164 * function_stack_trace_call()
165 * ftrace_call()
167 #define STACK_SKIP 2
168 #else
170 * Skip 3:
171 * __trace_stack()
172 * function_stack_trace_call()
173 * ftrace_call()
175 #define STACK_SKIP 3
176 #endif
178 static void
179 function_stack_trace_call(unsigned long ip, unsigned long parent_ip,
180 struct ftrace_ops *op, struct ftrace_regs *fregs)
182 struct trace_array *tr = op->private;
183 struct trace_array_cpu *data;
184 unsigned long flags;
185 long disabled;
186 int cpu;
187 int pc;
189 if (unlikely(!tr->function_enabled))
190 return;
193 * Need to use raw, since this must be called before the
194 * recursive protection is performed.
196 local_irq_save(flags);
197 cpu = raw_smp_processor_id();
198 data = per_cpu_ptr(tr->array_buffer.data, cpu);
199 disabled = atomic_inc_return(&data->disabled);
201 if (likely(disabled == 1)) {
202 pc = preempt_count();
203 trace_function(tr, ip, parent_ip, flags, pc);
204 __trace_stack(tr, flags, STACK_SKIP, pc);
207 atomic_dec(&data->disabled);
208 local_irq_restore(flags);
211 static struct tracer_opt func_opts[] = {
212 #ifdef CONFIG_STACKTRACE
213 { TRACER_OPT(func_stack_trace, TRACE_FUNC_OPT_STACK) },
214 #endif
215 { } /* Always set a last empty entry */
218 static struct tracer_flags func_flags = {
219 .val = 0, /* By default: all flags disabled */
220 .opts = func_opts
223 static void tracing_start_function_trace(struct trace_array *tr)
225 tr->function_enabled = 0;
226 register_ftrace_function(tr->ops);
227 tr->function_enabled = 1;
230 static void tracing_stop_function_trace(struct trace_array *tr)
232 tr->function_enabled = 0;
233 unregister_ftrace_function(tr->ops);
236 static struct tracer function_trace;
238 static int
239 func_set_flag(struct trace_array *tr, u32 old_flags, u32 bit, int set)
241 switch (bit) {
242 case TRACE_FUNC_OPT_STACK:
243 /* do nothing if already set */
244 if (!!set == !!(func_flags.val & TRACE_FUNC_OPT_STACK))
245 break;
247 /* We can change this flag when not running. */
248 if (tr->current_trace != &function_trace)
249 break;
251 unregister_ftrace_function(tr->ops);
253 if (set) {
254 tr->ops->func = function_stack_trace_call;
255 register_ftrace_function(tr->ops);
256 } else {
257 tr->ops->func = function_trace_call;
258 register_ftrace_function(tr->ops);
261 break;
262 default:
263 return -EINVAL;
266 return 0;
269 static struct tracer function_trace __tracer_data =
271 .name = "function",
272 .init = function_trace_init,
273 .reset = function_trace_reset,
274 .start = function_trace_start,
275 .flags = &func_flags,
276 .set_flag = func_set_flag,
277 .allow_instances = true,
278 #ifdef CONFIG_FTRACE_SELFTEST
279 .selftest = trace_selftest_startup_function,
280 #endif
283 #ifdef CONFIG_DYNAMIC_FTRACE
284 static void update_traceon_count(struct ftrace_probe_ops *ops,
285 unsigned long ip,
286 struct trace_array *tr, bool on,
287 void *data)
289 struct ftrace_func_mapper *mapper = data;
290 long *count;
291 long old_count;
294 * Tracing gets disabled (or enabled) once per count.
295 * This function can be called at the same time on multiple CPUs.
296 * It is fine if both disable (or enable) tracing, as disabling
297 * (or enabling) the second time doesn't do anything as the
298 * state of the tracer is already disabled (or enabled).
299 * What needs to be synchronized in this case is that the count
300 * only gets decremented once, even if the tracer is disabled
301 * (or enabled) twice, as the second one is really a nop.
303 * The memory barriers guarantee that we only decrement the
304 * counter once. First the count is read to a local variable
305 * and a read barrier is used to make sure that it is loaded
306 * before checking if the tracer is in the state we want.
307 * If the tracer is not in the state we want, then the count
308 * is guaranteed to be the old count.
310 * Next the tracer is set to the state we want (disabled or enabled)
311 * then a write memory barrier is used to make sure that
312 * the new state is visible before changing the counter by
313 * one minus the old counter. This guarantees that another CPU
314 * executing this code will see the new state before seeing
315 * the new counter value, and would not do anything if the new
316 * counter is seen.
318 * Note, there is no synchronization between this and a user
319 * setting the tracing_on file. But we currently don't care
320 * about that.
322 count = (long *)ftrace_func_mapper_find_ip(mapper, ip);
323 old_count = *count;
325 if (old_count <= 0)
326 return;
328 /* Make sure we see count before checking tracing state */
329 smp_rmb();
331 if (on == !!tracer_tracing_is_on(tr))
332 return;
334 if (on)
335 tracer_tracing_on(tr);
336 else
337 tracer_tracing_off(tr);
339 /* Make sure tracing state is visible before updating count */
340 smp_wmb();
342 *count = old_count - 1;
345 static void
346 ftrace_traceon_count(unsigned long ip, unsigned long parent_ip,
347 struct trace_array *tr, struct ftrace_probe_ops *ops,
348 void *data)
350 update_traceon_count(ops, ip, tr, 1, data);
353 static void
354 ftrace_traceoff_count(unsigned long ip, unsigned long parent_ip,
355 struct trace_array *tr, struct ftrace_probe_ops *ops,
356 void *data)
358 update_traceon_count(ops, ip, tr, 0, data);
361 static void
362 ftrace_traceon(unsigned long ip, unsigned long parent_ip,
363 struct trace_array *tr, struct ftrace_probe_ops *ops,
364 void *data)
366 if (tracer_tracing_is_on(tr))
367 return;
369 tracer_tracing_on(tr);
372 static void
373 ftrace_traceoff(unsigned long ip, unsigned long parent_ip,
374 struct trace_array *tr, struct ftrace_probe_ops *ops,
375 void *data)
377 if (!tracer_tracing_is_on(tr))
378 return;
380 tracer_tracing_off(tr);
383 #ifdef CONFIG_UNWINDER_ORC
385 * Skip 3:
387 * function_trace_probe_call()
388 * ftrace_ops_assist_func()
389 * ftrace_call()
391 #define FTRACE_STACK_SKIP 3
392 #else
394 * Skip 5:
396 * __trace_stack()
397 * ftrace_stacktrace()
398 * function_trace_probe_call()
399 * ftrace_ops_assist_func()
400 * ftrace_call()
402 #define FTRACE_STACK_SKIP 5
403 #endif
405 static __always_inline void trace_stack(struct trace_array *tr)
407 unsigned long flags;
408 int pc;
410 local_save_flags(flags);
411 pc = preempt_count();
413 __trace_stack(tr, flags, FTRACE_STACK_SKIP, pc);
416 static void
417 ftrace_stacktrace(unsigned long ip, unsigned long parent_ip,
418 struct trace_array *tr, struct ftrace_probe_ops *ops,
419 void *data)
421 trace_stack(tr);
424 static void
425 ftrace_stacktrace_count(unsigned long ip, unsigned long parent_ip,
426 struct trace_array *tr, struct ftrace_probe_ops *ops,
427 void *data)
429 struct ftrace_func_mapper *mapper = data;
430 long *count;
431 long old_count;
432 long new_count;
434 if (!tracing_is_on())
435 return;
437 /* unlimited? */
438 if (!mapper) {
439 trace_stack(tr);
440 return;
443 count = (long *)ftrace_func_mapper_find_ip(mapper, ip);
446 * Stack traces should only execute the number of times the
447 * user specified in the counter.
449 do {
450 old_count = *count;
452 if (!old_count)
453 return;
455 new_count = old_count - 1;
456 new_count = cmpxchg(count, old_count, new_count);
457 if (new_count == old_count)
458 trace_stack(tr);
460 if (!tracing_is_on())
461 return;
463 } while (new_count != old_count);
466 static int update_count(struct ftrace_probe_ops *ops, unsigned long ip,
467 void *data)
469 struct ftrace_func_mapper *mapper = data;
470 long *count = NULL;
472 if (mapper)
473 count = (long *)ftrace_func_mapper_find_ip(mapper, ip);
475 if (count) {
476 if (*count <= 0)
477 return 0;
478 (*count)--;
481 return 1;
484 static void
485 ftrace_dump_probe(unsigned long ip, unsigned long parent_ip,
486 struct trace_array *tr, struct ftrace_probe_ops *ops,
487 void *data)
489 if (update_count(ops, ip, data))
490 ftrace_dump(DUMP_ALL);
493 /* Only dump the current CPU buffer. */
494 static void
495 ftrace_cpudump_probe(unsigned long ip, unsigned long parent_ip,
496 struct trace_array *tr, struct ftrace_probe_ops *ops,
497 void *data)
499 if (update_count(ops, ip, data))
500 ftrace_dump(DUMP_ORIG);
503 static int
504 ftrace_probe_print(const char *name, struct seq_file *m,
505 unsigned long ip, struct ftrace_probe_ops *ops,
506 void *data)
508 struct ftrace_func_mapper *mapper = data;
509 long *count = NULL;
511 seq_printf(m, "%ps:%s", (void *)ip, name);
513 if (mapper)
514 count = (long *)ftrace_func_mapper_find_ip(mapper, ip);
516 if (count)
517 seq_printf(m, ":count=%ld\n", *count);
518 else
519 seq_puts(m, ":unlimited\n");
521 return 0;
524 static int
525 ftrace_traceon_print(struct seq_file *m, unsigned long ip,
526 struct ftrace_probe_ops *ops,
527 void *data)
529 return ftrace_probe_print("traceon", m, ip, ops, data);
532 static int
533 ftrace_traceoff_print(struct seq_file *m, unsigned long ip,
534 struct ftrace_probe_ops *ops, void *data)
536 return ftrace_probe_print("traceoff", m, ip, ops, data);
539 static int
540 ftrace_stacktrace_print(struct seq_file *m, unsigned long ip,
541 struct ftrace_probe_ops *ops, void *data)
543 return ftrace_probe_print("stacktrace", m, ip, ops, data);
546 static int
547 ftrace_dump_print(struct seq_file *m, unsigned long ip,
548 struct ftrace_probe_ops *ops, void *data)
550 return ftrace_probe_print("dump", m, ip, ops, data);
553 static int
554 ftrace_cpudump_print(struct seq_file *m, unsigned long ip,
555 struct ftrace_probe_ops *ops, void *data)
557 return ftrace_probe_print("cpudump", m, ip, ops, data);
561 static int
562 ftrace_count_init(struct ftrace_probe_ops *ops, struct trace_array *tr,
563 unsigned long ip, void *init_data, void **data)
565 struct ftrace_func_mapper *mapper = *data;
567 if (!mapper) {
568 mapper = allocate_ftrace_func_mapper();
569 if (!mapper)
570 return -ENOMEM;
571 *data = mapper;
574 return ftrace_func_mapper_add_ip(mapper, ip, init_data);
577 static void
578 ftrace_count_free(struct ftrace_probe_ops *ops, struct trace_array *tr,
579 unsigned long ip, void *data)
581 struct ftrace_func_mapper *mapper = data;
583 if (!ip) {
584 free_ftrace_func_mapper(mapper, NULL);
585 return;
588 ftrace_func_mapper_remove_ip(mapper, ip);
591 static struct ftrace_probe_ops traceon_count_probe_ops = {
592 .func = ftrace_traceon_count,
593 .print = ftrace_traceon_print,
594 .init = ftrace_count_init,
595 .free = ftrace_count_free,
598 static struct ftrace_probe_ops traceoff_count_probe_ops = {
599 .func = ftrace_traceoff_count,
600 .print = ftrace_traceoff_print,
601 .init = ftrace_count_init,
602 .free = ftrace_count_free,
605 static struct ftrace_probe_ops stacktrace_count_probe_ops = {
606 .func = ftrace_stacktrace_count,
607 .print = ftrace_stacktrace_print,
608 .init = ftrace_count_init,
609 .free = ftrace_count_free,
612 static struct ftrace_probe_ops dump_probe_ops = {
613 .func = ftrace_dump_probe,
614 .print = ftrace_dump_print,
615 .init = ftrace_count_init,
616 .free = ftrace_count_free,
619 static struct ftrace_probe_ops cpudump_probe_ops = {
620 .func = ftrace_cpudump_probe,
621 .print = ftrace_cpudump_print,
624 static struct ftrace_probe_ops traceon_probe_ops = {
625 .func = ftrace_traceon,
626 .print = ftrace_traceon_print,
629 static struct ftrace_probe_ops traceoff_probe_ops = {
630 .func = ftrace_traceoff,
631 .print = ftrace_traceoff_print,
634 static struct ftrace_probe_ops stacktrace_probe_ops = {
635 .func = ftrace_stacktrace,
636 .print = ftrace_stacktrace_print,
639 static int
640 ftrace_trace_probe_callback(struct trace_array *tr,
641 struct ftrace_probe_ops *ops,
642 struct ftrace_hash *hash, char *glob,
643 char *cmd, char *param, int enable)
645 void *count = (void *)-1;
646 char *number;
647 int ret;
649 /* hash funcs only work with set_ftrace_filter */
650 if (!enable)
651 return -EINVAL;
653 if (glob[0] == '!')
654 return unregister_ftrace_function_probe_func(glob+1, tr, ops);
656 if (!param)
657 goto out_reg;
659 number = strsep(&param, ":");
661 if (!strlen(number))
662 goto out_reg;
665 * We use the callback data field (which is a pointer)
666 * as our counter.
668 ret = kstrtoul(number, 0, (unsigned long *)&count);
669 if (ret)
670 return ret;
672 out_reg:
673 ret = register_ftrace_function_probe(glob, tr, ops, count);
675 return ret < 0 ? ret : 0;
678 static int
679 ftrace_trace_onoff_callback(struct trace_array *tr, struct ftrace_hash *hash,
680 char *glob, char *cmd, char *param, int enable)
682 struct ftrace_probe_ops *ops;
684 if (!tr)
685 return -ENODEV;
687 /* we register both traceon and traceoff to this callback */
688 if (strcmp(cmd, "traceon") == 0)
689 ops = param ? &traceon_count_probe_ops : &traceon_probe_ops;
690 else
691 ops = param ? &traceoff_count_probe_ops : &traceoff_probe_ops;
693 return ftrace_trace_probe_callback(tr, ops, hash, glob, cmd,
694 param, enable);
697 static int
698 ftrace_stacktrace_callback(struct trace_array *tr, struct ftrace_hash *hash,
699 char *glob, char *cmd, char *param, int enable)
701 struct ftrace_probe_ops *ops;
703 if (!tr)
704 return -ENODEV;
706 ops = param ? &stacktrace_count_probe_ops : &stacktrace_probe_ops;
708 return ftrace_trace_probe_callback(tr, ops, hash, glob, cmd,
709 param, enable);
712 static int
713 ftrace_dump_callback(struct trace_array *tr, struct ftrace_hash *hash,
714 char *glob, char *cmd, char *param, int enable)
716 struct ftrace_probe_ops *ops;
718 if (!tr)
719 return -ENODEV;
721 ops = &dump_probe_ops;
723 /* Only dump once. */
724 return ftrace_trace_probe_callback(tr, ops, hash, glob, cmd,
725 "1", enable);
728 static int
729 ftrace_cpudump_callback(struct trace_array *tr, struct ftrace_hash *hash,
730 char *glob, char *cmd, char *param, int enable)
732 struct ftrace_probe_ops *ops;
734 if (!tr)
735 return -ENODEV;
737 ops = &cpudump_probe_ops;
739 /* Only dump once. */
740 return ftrace_trace_probe_callback(tr, ops, hash, glob, cmd,
741 "1", enable);
744 static struct ftrace_func_command ftrace_traceon_cmd = {
745 .name = "traceon",
746 .func = ftrace_trace_onoff_callback,
749 static struct ftrace_func_command ftrace_traceoff_cmd = {
750 .name = "traceoff",
751 .func = ftrace_trace_onoff_callback,
754 static struct ftrace_func_command ftrace_stacktrace_cmd = {
755 .name = "stacktrace",
756 .func = ftrace_stacktrace_callback,
759 static struct ftrace_func_command ftrace_dump_cmd = {
760 .name = "dump",
761 .func = ftrace_dump_callback,
764 static struct ftrace_func_command ftrace_cpudump_cmd = {
765 .name = "cpudump",
766 .func = ftrace_cpudump_callback,
769 static int __init init_func_cmd_traceon(void)
771 int ret;
773 ret = register_ftrace_command(&ftrace_traceoff_cmd);
774 if (ret)
775 return ret;
777 ret = register_ftrace_command(&ftrace_traceon_cmd);
778 if (ret)
779 goto out_free_traceoff;
781 ret = register_ftrace_command(&ftrace_stacktrace_cmd);
782 if (ret)
783 goto out_free_traceon;
785 ret = register_ftrace_command(&ftrace_dump_cmd);
786 if (ret)
787 goto out_free_stacktrace;
789 ret = register_ftrace_command(&ftrace_cpudump_cmd);
790 if (ret)
791 goto out_free_dump;
793 return 0;
795 out_free_dump:
796 unregister_ftrace_command(&ftrace_dump_cmd);
797 out_free_stacktrace:
798 unregister_ftrace_command(&ftrace_stacktrace_cmd);
799 out_free_traceon:
800 unregister_ftrace_command(&ftrace_traceon_cmd);
801 out_free_traceoff:
802 unregister_ftrace_command(&ftrace_traceoff_cmd);
804 return ret;
806 #else
807 static inline int init_func_cmd_traceon(void)
809 return 0;
811 #endif /* CONFIG_DYNAMIC_FTRACE */
813 __init int init_function_trace(void)
815 init_func_cmd_traceon();
816 return register_tracer(&function_trace);