1 // SPDX-License-Identifier: GPL-2.0
3 * ring buffer based function tracer
5 * Copyright (C) 2007-2012 Steven Rostedt <srostedt@redhat.com>
6 * Copyright (C) 2008 Ingo Molnar <mingo@redhat.com>
8 * Originally taken from the RT patch by:
9 * Arnaldo Carvalho de Melo <acme@redhat.com>
11 * Based on code from the latency_tracer, that is:
12 * Copyright (C) 2004-2006 Ingo Molnar
13 * Copyright (C) 2004 Nadia Yvette Chambers
15 #include <linux/ring_buffer.h>
16 #include <linux/utsname.h>
17 #include <linux/stacktrace.h>
18 #include <linux/writeback.h>
19 #include <linux/kallsyms.h>
20 #include <linux/security.h>
21 #include <linux/seq_file.h>
22 #include <linux/irqflags.h>
23 #include <linux/debugfs.h>
24 #include <linux/tracefs.h>
25 #include <linux/pagemap.h>
26 #include <linux/hardirq.h>
27 #include <linux/linkage.h>
28 #include <linux/uaccess.h>
29 #include <linux/cleanup.h>
30 #include <linux/vmalloc.h>
31 #include <linux/ftrace.h>
32 #include <linux/module.h>
33 #include <linux/percpu.h>
34 #include <linux/splice.h>
35 #include <linux/kdebug.h>
36 #include <linux/string.h>
37 #include <linux/mount.h>
38 #include <linux/rwsem.h>
39 #include <linux/slab.h>
40 #include <linux/ctype.h>
41 #include <linux/init.h>
42 #include <linux/panic_notifier.h>
43 #include <linux/poll.h>
44 #include <linux/nmi.h>
46 #include <linux/trace.h>
47 #include <linux/sched/clock.h>
48 #include <linux/sched/rt.h>
49 #include <linux/fsnotify.h>
50 #include <linux/irq_work.h>
51 #include <linux/workqueue.h>
52 #include <linux/sort.h>
53 #include <linux/io.h> /* vmap_page_range() */
54 #include <linux/fs_context.h>
56 #include <asm/setup.h> /* COMMAND_LINE_SIZE */
59 #include "trace_output.h"
61 #ifdef CONFIG_FTRACE_STARTUP_TEST
63 * We need to change this state when a selftest is running.
64 * A selftest will lurk into the ring-buffer to count the
65 * entries inserted during the selftest although some concurrent
66 * insertions into the ring-buffer such as trace_printk could occurred
67 * at the same time, giving false positive or negative results.
69 static bool __read_mostly tracing_selftest_running;
72 * If boot-time tracing including tracers/events via kernel cmdline
73 * is running, we do not want to run SELFTEST.
75 bool __read_mostly tracing_selftest_disabled;
77 void __init disable_tracing_selftest(const char *reason)
79 if (!tracing_selftest_disabled) {
80 tracing_selftest_disabled = true;
81 pr_info("Ftrace startup test is disabled due to %s\n", reason);
85 #define tracing_selftest_running 0
86 #define tracing_selftest_disabled 0
89 /* Pipe tracepoints to printk */
90 static struct trace_iterator *tracepoint_print_iter;
91 int tracepoint_printk;
92 static bool tracepoint_printk_stop_on_boot __initdata;
93 static bool traceoff_after_boot __initdata;
94 static DEFINE_STATIC_KEY_FALSE(tracepoint_printk_key);
96 /* For tracers that don't implement custom flags */
97 static struct tracer_opt dummy_tracer_opt[] = {
102 dummy_set_flag(struct trace_array *tr, u32 old_flags, u32 bit, int set)
108 * To prevent the comm cache from being overwritten when no
109 * tracing is active, only save the comm when a trace event
112 DEFINE_PER_CPU(bool, trace_taskinfo_save);
115 * Kill all tracing for good (never come back).
116 * It is initialized to 1 but will turn to zero if the initialization
117 * of the tracer is successful. But that is the only place that sets
120 static int tracing_disabled = 1;
122 cpumask_var_t __read_mostly tracing_buffer_mask;
124 #define MAX_TRACER_SIZE 100
126 * ftrace_dump_on_oops - variable to dump ftrace buffer on oops
128 * If there is an oops (or kernel panic) and the ftrace_dump_on_oops
129 * is set, then ftrace_dump is called. This will output the contents
130 * of the ftrace buffers to the console. This is very useful for
131 * capturing traces that lead to crashes and outputing it to a
134 * It is default off, but you can enable it with either specifying
135 * "ftrace_dump_on_oops" in the kernel command line, or setting
136 * /proc/sys/kernel/ftrace_dump_on_oops
137 * Set 1 if you want to dump buffers of all CPUs
138 * Set 2 if you want to dump the buffer of the CPU that triggered oops
139 * Set instance name if you want to dump the specific trace instance
140 * Multiple instance dump is also supported, and instances are seperated
143 /* Set to string format zero to disable by default */
144 char ftrace_dump_on_oops[MAX_TRACER_SIZE] = "0";
146 /* When set, tracing will stop when a WARN*() is hit */
147 static int __disable_trace_on_warning;
149 int tracepoint_printk_sysctl(const struct ctl_table *table, int write,
150 void *buffer, size_t *lenp, loff_t *ppos);
151 static const struct ctl_table trace_sysctl_table[] = {
153 .procname = "ftrace_dump_on_oops",
154 .data = &ftrace_dump_on_oops,
155 .maxlen = MAX_TRACER_SIZE,
157 .proc_handler = proc_dostring,
160 .procname = "traceoff_on_warning",
161 .data = &__disable_trace_on_warning,
162 .maxlen = sizeof(__disable_trace_on_warning),
164 .proc_handler = proc_dointvec,
167 .procname = "tracepoint_printk",
168 .data = &tracepoint_printk,
169 .maxlen = sizeof(tracepoint_printk),
171 .proc_handler = tracepoint_printk_sysctl,
175 static int __init init_trace_sysctls(void)
177 register_sysctl_init("kernel", trace_sysctl_table);
180 subsys_initcall(init_trace_sysctls);
182 #ifdef CONFIG_TRACE_EVAL_MAP_FILE
183 /* Map of enums to their values, for "eval_map" file */
184 struct trace_eval_map_head {
186 unsigned long length;
189 union trace_eval_map_item;
191 struct trace_eval_map_tail {
193 * "end" is first and points to NULL as it must be different
194 * than "mod" or "eval_string"
196 union trace_eval_map_item *next;
197 const char *end; /* points to NULL */
200 static DEFINE_MUTEX(trace_eval_mutex);
203 * The trace_eval_maps are saved in an array with two extra elements,
204 * one at the beginning, and one at the end. The beginning item contains
205 * the count of the saved maps (head.length), and the module they
206 * belong to if not built in (head.mod). The ending item contains a
207 * pointer to the next array of saved eval_map items.
209 union trace_eval_map_item {
210 struct trace_eval_map map;
211 struct trace_eval_map_head head;
212 struct trace_eval_map_tail tail;
215 static union trace_eval_map_item *trace_eval_maps;
216 #endif /* CONFIG_TRACE_EVAL_MAP_FILE */
218 int tracing_set_tracer(struct trace_array *tr, const char *buf);
219 static void ftrace_trace_userstack(struct trace_array *tr,
220 struct trace_buffer *buffer,
221 unsigned int trace_ctx);
223 static char bootup_tracer_buf[MAX_TRACER_SIZE] __initdata;
224 static char *default_bootup_tracer;
226 static bool allocate_snapshot;
227 static bool snapshot_at_boot;
229 static char boot_instance_info[COMMAND_LINE_SIZE] __initdata;
230 static int boot_instance_index;
232 static char boot_snapshot_info[COMMAND_LINE_SIZE] __initdata;
233 static int boot_snapshot_index;
235 static int __init set_cmdline_ftrace(char *str)
237 strscpy(bootup_tracer_buf, str, MAX_TRACER_SIZE);
238 default_bootup_tracer = bootup_tracer_buf;
239 /* We are using ftrace early, expand it */
240 trace_set_ring_buffer_expanded(NULL);
243 __setup("ftrace=", set_cmdline_ftrace);
245 int ftrace_dump_on_oops_enabled(void)
247 if (!strcmp("0", ftrace_dump_on_oops))
253 static int __init set_ftrace_dump_on_oops(char *str)
256 strscpy(ftrace_dump_on_oops, "1", MAX_TRACER_SIZE);
261 strscpy(ftrace_dump_on_oops, "1", MAX_TRACER_SIZE);
262 strscpy(ftrace_dump_on_oops + 1, str, MAX_TRACER_SIZE - 1);
267 strscpy(ftrace_dump_on_oops, str, MAX_TRACER_SIZE);
273 __setup("ftrace_dump_on_oops", set_ftrace_dump_on_oops);
275 static int __init stop_trace_on_warning(char *str)
277 if ((strcmp(str, "=0") != 0 && strcmp(str, "=off") != 0))
278 __disable_trace_on_warning = 1;
281 __setup("traceoff_on_warning", stop_trace_on_warning);
283 static int __init boot_alloc_snapshot(char *str)
285 char *slot = boot_snapshot_info + boot_snapshot_index;
286 int left = sizeof(boot_snapshot_info) - boot_snapshot_index;
291 if (strlen(str) >= left)
294 ret = snprintf(slot, left, "%s\t", str);
295 boot_snapshot_index += ret;
297 allocate_snapshot = true;
298 /* We also need the main ring buffer expanded */
299 trace_set_ring_buffer_expanded(NULL);
303 __setup("alloc_snapshot", boot_alloc_snapshot);
306 static int __init boot_snapshot(char *str)
308 snapshot_at_boot = true;
309 boot_alloc_snapshot(str);
312 __setup("ftrace_boot_snapshot", boot_snapshot);
315 static int __init boot_instance(char *str)
317 char *slot = boot_instance_info + boot_instance_index;
318 int left = sizeof(boot_instance_info) - boot_instance_index;
321 if (strlen(str) >= left)
324 ret = snprintf(slot, left, "%s\t", str);
325 boot_instance_index += ret;
329 __setup("trace_instance=", boot_instance);
332 static char trace_boot_options_buf[MAX_TRACER_SIZE] __initdata;
334 static int __init set_trace_boot_options(char *str)
336 strscpy(trace_boot_options_buf, str, MAX_TRACER_SIZE);
339 __setup("trace_options=", set_trace_boot_options);
341 static char trace_boot_clock_buf[MAX_TRACER_SIZE] __initdata;
342 static char *trace_boot_clock __initdata;
344 static int __init set_trace_boot_clock(char *str)
346 strscpy(trace_boot_clock_buf, str, MAX_TRACER_SIZE);
347 trace_boot_clock = trace_boot_clock_buf;
350 __setup("trace_clock=", set_trace_boot_clock);
352 static int __init set_tracepoint_printk(char *str)
354 /* Ignore the "tp_printk_stop_on_boot" param */
358 if ((strcmp(str, "=0") != 0 && strcmp(str, "=off") != 0))
359 tracepoint_printk = 1;
362 __setup("tp_printk", set_tracepoint_printk);
364 static int __init set_tracepoint_printk_stop(char *str)
366 tracepoint_printk_stop_on_boot = true;
369 __setup("tp_printk_stop_on_boot", set_tracepoint_printk_stop);
371 static int __init set_traceoff_after_boot(char *str)
373 traceoff_after_boot = true;
376 __setup("traceoff_after_boot", set_traceoff_after_boot);
378 unsigned long long ns2usecs(u64 nsec)
386 trace_process_export(struct trace_export *export,
387 struct ring_buffer_event *event, int flag)
389 struct trace_entry *entry;
390 unsigned int size = 0;
392 if (export->flags & flag) {
393 entry = ring_buffer_event_data(event);
394 size = ring_buffer_event_length(event);
395 export->write(export, entry, size);
399 static DEFINE_MUTEX(ftrace_export_lock);
401 static struct trace_export __rcu *ftrace_exports_list __read_mostly;
403 static DEFINE_STATIC_KEY_FALSE(trace_function_exports_enabled);
404 static DEFINE_STATIC_KEY_FALSE(trace_event_exports_enabled);
405 static DEFINE_STATIC_KEY_FALSE(trace_marker_exports_enabled);
407 static inline void ftrace_exports_enable(struct trace_export *export)
409 if (export->flags & TRACE_EXPORT_FUNCTION)
410 static_branch_inc(&trace_function_exports_enabled);
412 if (export->flags & TRACE_EXPORT_EVENT)
413 static_branch_inc(&trace_event_exports_enabled);
415 if (export->flags & TRACE_EXPORT_MARKER)
416 static_branch_inc(&trace_marker_exports_enabled);
419 static inline void ftrace_exports_disable(struct trace_export *export)
421 if (export->flags & TRACE_EXPORT_FUNCTION)
422 static_branch_dec(&trace_function_exports_enabled);
424 if (export->flags & TRACE_EXPORT_EVENT)
425 static_branch_dec(&trace_event_exports_enabled);
427 if (export->flags & TRACE_EXPORT_MARKER)
428 static_branch_dec(&trace_marker_exports_enabled);
431 static void ftrace_exports(struct ring_buffer_event *event, int flag)
433 struct trace_export *export;
435 preempt_disable_notrace();
437 export = rcu_dereference_raw_check(ftrace_exports_list);
439 trace_process_export(export, event, flag);
440 export = rcu_dereference_raw_check(export->next);
443 preempt_enable_notrace();
447 add_trace_export(struct trace_export **list, struct trace_export *export)
449 rcu_assign_pointer(export->next, *list);
451 * We are entering export into the list but another
452 * CPU might be walking that list. We need to make sure
453 * the export->next pointer is valid before another CPU sees
454 * the export pointer included into the list.
456 rcu_assign_pointer(*list, export);
460 rm_trace_export(struct trace_export **list, struct trace_export *export)
462 struct trace_export **p;
464 for (p = list; *p != NULL; p = &(*p)->next)
471 rcu_assign_pointer(*p, (*p)->next);
477 add_ftrace_export(struct trace_export **list, struct trace_export *export)
479 ftrace_exports_enable(export);
481 add_trace_export(list, export);
485 rm_ftrace_export(struct trace_export **list, struct trace_export *export)
489 ret = rm_trace_export(list, export);
490 ftrace_exports_disable(export);
495 int register_ftrace_export(struct trace_export *export)
497 if (WARN_ON_ONCE(!export->write))
500 mutex_lock(&ftrace_export_lock);
502 add_ftrace_export(&ftrace_exports_list, export);
504 mutex_unlock(&ftrace_export_lock);
508 EXPORT_SYMBOL_GPL(register_ftrace_export);
510 int unregister_ftrace_export(struct trace_export *export)
514 mutex_lock(&ftrace_export_lock);
516 ret = rm_ftrace_export(&ftrace_exports_list, export);
518 mutex_unlock(&ftrace_export_lock);
522 EXPORT_SYMBOL_GPL(unregister_ftrace_export);
524 /* trace_flags holds trace_options default values */
525 #define TRACE_DEFAULT_FLAGS \
526 (FUNCTION_DEFAULT_FLAGS | \
527 TRACE_ITER_PRINT_PARENT | TRACE_ITER_PRINTK | \
528 TRACE_ITER_ANNOTATE | TRACE_ITER_CONTEXT_INFO | \
529 TRACE_ITER_RECORD_CMD | TRACE_ITER_OVERWRITE | \
530 TRACE_ITER_IRQ_INFO | TRACE_ITER_MARKERS | \
531 TRACE_ITER_HASH_PTR | TRACE_ITER_TRACE_PRINTK | \
532 TRACE_ITER_COPY_MARKER)
534 /* trace_options that are only supported by global_trace */
535 #define TOP_LEVEL_TRACE_FLAGS (TRACE_ITER_PRINTK | \
536 TRACE_ITER_PRINTK_MSGONLY | TRACE_ITER_RECORD_CMD)
538 /* trace_flags that are default zero for instances */
539 #define ZEROED_TRACE_FLAGS \
540 (TRACE_ITER_EVENT_FORK | TRACE_ITER_FUNC_FORK | TRACE_ITER_TRACE_PRINTK | \
541 TRACE_ITER_COPY_MARKER)
544 * The global_trace is the descriptor that holds the top-level tracing
545 * buffers for the live tracing.
547 static struct trace_array global_trace = {
548 .trace_flags = TRACE_DEFAULT_FLAGS,
551 static struct trace_array *printk_trace = &global_trace;
553 /* List of trace_arrays interested in the top level trace_marker */
554 static LIST_HEAD(marker_copies);
556 static __always_inline bool printk_binsafe(struct trace_array *tr)
559 * The binary format of traceprintk can cause a crash if used
560 * by a buffer from another boot. Force the use of the
561 * non binary version of trace_printk if the trace_printk
562 * buffer is a boot mapped ring buffer.
564 return !(tr->flags & TRACE_ARRAY_FL_BOOT);
567 static void update_printk_trace(struct trace_array *tr)
569 if (printk_trace == tr)
572 printk_trace->trace_flags &= ~TRACE_ITER_TRACE_PRINTK;
574 tr->trace_flags |= TRACE_ITER_TRACE_PRINTK;
577 /* Returns true if the status of tr changed */
578 static bool update_marker_trace(struct trace_array *tr, int enabled)
580 lockdep_assert_held(&event_mutex);
583 if (!list_empty(&tr->marker_list))
586 list_add_rcu(&tr->marker_list, &marker_copies);
587 tr->trace_flags |= TRACE_ITER_COPY_MARKER;
591 if (list_empty(&tr->marker_list))
594 list_del_init(&tr->marker_list);
595 tr->trace_flags &= ~TRACE_ITER_COPY_MARKER;
599 void trace_set_ring_buffer_expanded(struct trace_array *tr)
603 tr->ring_buffer_expanded = true;
606 LIST_HEAD(ftrace_trace_arrays);
608 int trace_array_get(struct trace_array *this_tr)
610 struct trace_array *tr;
612 guard(mutex)(&trace_types_lock);
613 list_for_each_entry(tr, &ftrace_trace_arrays, list) {
623 static void __trace_array_put(struct trace_array *this_tr)
625 WARN_ON(!this_tr->ref);
630 * trace_array_put - Decrement the reference counter for this trace array.
631 * @this_tr : pointer to the trace array
633 * NOTE: Use this when we no longer need the trace array returned by
634 * trace_array_get_by_name(). This ensures the trace array can be later
638 void trace_array_put(struct trace_array *this_tr)
643 mutex_lock(&trace_types_lock);
644 __trace_array_put(this_tr);
645 mutex_unlock(&trace_types_lock);
647 EXPORT_SYMBOL_GPL(trace_array_put);
649 int tracing_check_open_get_tr(struct trace_array *tr)
653 ret = security_locked_down(LOCKDOWN_TRACEFS);
657 if (tracing_disabled)
660 if (tr && trace_array_get(tr) < 0)
667 * trace_find_filtered_pid - check if a pid exists in a filtered_pid list
668 * @filtered_pids: The list of pids to check
669 * @search_pid: The PID to find in @filtered_pids
671 * Returns true if @search_pid is found in @filtered_pids, and false otherwise.
674 trace_find_filtered_pid(struct trace_pid_list *filtered_pids, pid_t search_pid)
676 return trace_pid_list_is_set(filtered_pids, search_pid);
680 * trace_ignore_this_task - should a task be ignored for tracing
681 * @filtered_pids: The list of pids to check
682 * @filtered_no_pids: The list of pids not to be traced
683 * @task: The task that should be ignored if not filtered
685 * Checks if @task should be traced or not from @filtered_pids.
686 * Returns true if @task should *NOT* be traced.
687 * Returns false if @task should be traced.
690 trace_ignore_this_task(struct trace_pid_list *filtered_pids,
691 struct trace_pid_list *filtered_no_pids,
692 struct task_struct *task)
695 * If filtered_no_pids is not empty, and the task's pid is listed
696 * in filtered_no_pids, then return true.
697 * Otherwise, if filtered_pids is empty, that means we can
698 * trace all tasks. If it has content, then only trace pids
699 * within filtered_pids.
702 return (filtered_pids &&
703 !trace_find_filtered_pid(filtered_pids, task->pid)) ||
705 trace_find_filtered_pid(filtered_no_pids, task->pid));
709 * trace_filter_add_remove_task - Add or remove a task from a pid_list
710 * @pid_list: The list to modify
711 * @self: The current task for fork or NULL for exit
712 * @task: The task to add or remove
714 * If adding a task, if @self is defined, the task is only added if @self
715 * is also included in @pid_list. This happens on fork and tasks should
716 * only be added when the parent is listed. If @self is NULL, then the
717 * @task pid will be removed from the list, which would happen on exit
720 void trace_filter_add_remove_task(struct trace_pid_list *pid_list,
721 struct task_struct *self,
722 struct task_struct *task)
727 /* For forks, we only add if the forking task is listed */
729 if (!trace_find_filtered_pid(pid_list, self->pid))
733 /* "self" is set for forks, and NULL for exits */
735 trace_pid_list_set(pid_list, task->pid);
737 trace_pid_list_clear(pid_list, task->pid);
741 * trace_pid_next - Used for seq_file to get to the next pid of a pid_list
742 * @pid_list: The pid list to show
743 * @v: The last pid that was shown (+1 the actual pid to let zero be displayed)
744 * @pos: The position of the file
746 * This is used by the seq_file "next" operation to iterate the pids
747 * listed in a trace_pid_list structure.
749 * Returns the pid+1 as we want to display pid of zero, but NULL would
750 * stop the iteration.
752 void *trace_pid_next(struct trace_pid_list *pid_list, void *v, loff_t *pos)
754 long pid = (unsigned long)v;
759 /* pid already is +1 of the actual previous bit */
760 if (trace_pid_list_next(pid_list, pid, &next) < 0)
765 /* Return pid + 1 to allow zero to be represented */
766 return (void *)(pid + 1);
770 * trace_pid_start - Used for seq_file to start reading pid lists
771 * @pid_list: The pid list to show
772 * @pos: The position of the file
774 * This is used by seq_file "start" operation to start the iteration
777 * Returns the pid+1 as we want to display pid of zero, but NULL would
778 * stop the iteration.
780 void *trace_pid_start(struct trace_pid_list *pid_list, loff_t *pos)
786 if (trace_pid_list_first(pid_list, &first) < 0)
791 /* Return pid + 1 so that zero can be the exit value */
792 for (pid++; pid && l < *pos;
793 pid = (unsigned long)trace_pid_next(pid_list, (void *)pid, &l))
799 * trace_pid_show - show the current pid in seq_file processing
800 * @m: The seq_file structure to write into
801 * @v: A void pointer of the pid (+1) value to display
803 * Can be directly used by seq_file operations to display the current
806 int trace_pid_show(struct seq_file *m, void *v)
808 unsigned long pid = (unsigned long)v - 1;
810 seq_printf(m, "%lu\n", pid);
814 /* 128 should be much more than enough */
815 #define PID_BUF_SIZE 127
817 int trace_pid_write(struct trace_pid_list *filtered_pids,
818 struct trace_pid_list **new_pid_list,
819 const char __user *ubuf, size_t cnt)
821 struct trace_pid_list *pid_list;
822 struct trace_parser parser;
830 if (trace_parser_get_init(&parser, PID_BUF_SIZE + 1))
834 * Always recreate a new array. The write is an all or nothing
835 * operation. Always create a new array when adding new pids by
836 * the user. If the operation fails, then the current list is
839 pid_list = trace_pid_list_alloc();
841 trace_parser_put(&parser);
846 /* copy the current bits to the new max */
847 ret = trace_pid_list_first(filtered_pids, &pid);
849 trace_pid_list_set(pid_list, pid);
850 ret = trace_pid_list_next(filtered_pids, pid + 1, &pid);
860 ret = trace_get_user(&parser, ubuf, cnt, &pos);
868 if (!trace_parser_loaded(&parser))
872 if (kstrtoul(parser.buffer, 0, &val))
877 if (trace_pid_list_set(pid_list, pid) < 0) {
883 trace_parser_clear(&parser);
886 trace_parser_put(&parser);
889 trace_pid_list_free(pid_list);
894 /* Cleared the list of pids */
895 trace_pid_list_free(pid_list);
899 *new_pid_list = pid_list;
904 static u64 buffer_ftrace_now(struct array_buffer *buf, int cpu)
908 /* Early boot up does not have a buffer yet */
910 return trace_clock_local();
912 ts = ring_buffer_time_stamp(buf->buffer);
913 ring_buffer_normalize_time_stamp(buf->buffer, cpu, &ts);
918 u64 ftrace_now(int cpu)
920 return buffer_ftrace_now(&global_trace.array_buffer, cpu);
924 * tracing_is_enabled - Show if global_trace has been enabled
926 * Shows if the global trace has been enabled or not. It uses the
927 * mirror flag "buffer_disabled" to be used in fast paths such as for
928 * the irqsoff tracer. But it may be inaccurate due to races. If you
929 * need to know the accurate state, use tracing_is_on() which is a little
930 * slower, but accurate.
932 int tracing_is_enabled(void)
935 * For quick access (irqsoff uses this in fast path), just
936 * return the mirror variable of the state of the ring buffer.
937 * It's a little racy, but we don't really care.
940 return !global_trace.buffer_disabled;
944 * trace_buf_size is the size in bytes that is allocated
945 * for a buffer. Note, the number of bytes is always rounded
948 * This number is purposely set to a low number of 16384.
949 * If the dump on oops happens, it will be much appreciated
950 * to not have to wait for all that output. Anyway this can be
951 * boot time and run time configurable.
953 #define TRACE_BUF_SIZE_DEFAULT 1441792UL /* 16384 * 88 (sizeof(entry)) */
955 static unsigned long trace_buf_size = TRACE_BUF_SIZE_DEFAULT;
957 /* trace_types holds a link list of available tracers. */
958 static struct tracer *trace_types __read_mostly;
961 * trace_types_lock is used to protect the trace_types list.
963 DEFINE_MUTEX(trace_types_lock);
966 * serialize the access of the ring buffer
968 * ring buffer serializes readers, but it is low level protection.
969 * The validity of the events (which returns by ring_buffer_peek() ..etc)
970 * are not protected by ring buffer.
972 * The content of events may become garbage if we allow other process consumes
973 * these events concurrently:
974 * A) the page of the consumed events may become a normal page
975 * (not reader page) in ring buffer, and this page will be rewritten
976 * by events producer.
977 * B) The page of the consumed events may become a page for splice_read,
978 * and this page will be returned to system.
980 * These primitives allow multi process access to different cpu ring buffer
983 * These primitives don't distinguish read-only and read-consume access.
984 * Multi read-only access are also serialized.
988 static DECLARE_RWSEM(all_cpu_access_lock);
989 static DEFINE_PER_CPU(struct mutex, cpu_access_lock);
991 static inline void trace_access_lock(int cpu)
993 if (cpu == RING_BUFFER_ALL_CPUS) {
994 /* gain it for accessing the whole ring buffer. */
995 down_write(&all_cpu_access_lock);
997 /* gain it for accessing a cpu ring buffer. */
999 /* Firstly block other trace_access_lock(RING_BUFFER_ALL_CPUS). */
1000 down_read(&all_cpu_access_lock);
1002 /* Secondly block other access to this @cpu ring buffer. */
1003 mutex_lock(&per_cpu(cpu_access_lock, cpu));
1007 static inline void trace_access_unlock(int cpu)
1009 if (cpu == RING_BUFFER_ALL_CPUS) {
1010 up_write(&all_cpu_access_lock);
1012 mutex_unlock(&per_cpu(cpu_access_lock, cpu));
1013 up_read(&all_cpu_access_lock);
1017 static inline void trace_access_lock_init(void)
1021 for_each_possible_cpu(cpu)
1022 mutex_init(&per_cpu(cpu_access_lock, cpu));
1027 static DEFINE_MUTEX(access_lock);
1029 static inline void trace_access_lock(int cpu)
1032 mutex_lock(&access_lock);
1035 static inline void trace_access_unlock(int cpu)
1038 mutex_unlock(&access_lock);
1041 static inline void trace_access_lock_init(void)
1047 #ifdef CONFIG_STACKTRACE
1048 static void __ftrace_trace_stack(struct trace_array *tr,
1049 struct trace_buffer *buffer,
1050 unsigned int trace_ctx,
1051 int skip, struct pt_regs *regs);
1052 static inline void ftrace_trace_stack(struct trace_array *tr,
1053 struct trace_buffer *buffer,
1054 unsigned int trace_ctx,
1055 int skip, struct pt_regs *regs);
1058 static inline void __ftrace_trace_stack(struct trace_array *tr,
1059 struct trace_buffer *buffer,
1060 unsigned int trace_ctx,
1061 int skip, struct pt_regs *regs)
1064 static inline void ftrace_trace_stack(struct trace_array *tr,
1065 struct trace_buffer *buffer,
1066 unsigned long trace_ctx,
1067 int skip, struct pt_regs *regs)
1073 static __always_inline void
1074 trace_event_setup(struct ring_buffer_event *event,
1075 int type, unsigned int trace_ctx)
1077 struct trace_entry *ent = ring_buffer_event_data(event);
1079 tracing_generic_entry_update(ent, type, trace_ctx);
1082 static __always_inline struct ring_buffer_event *
1083 __trace_buffer_lock_reserve(struct trace_buffer *buffer,
1086 unsigned int trace_ctx)
1088 struct ring_buffer_event *event;
1090 event = ring_buffer_lock_reserve(buffer, len);
1092 trace_event_setup(event, type, trace_ctx);
1097 void tracer_tracing_on(struct trace_array *tr)
1099 if (tr->array_buffer.buffer)
1100 ring_buffer_record_on(tr->array_buffer.buffer);
1102 * This flag is looked at when buffers haven't been allocated
1103 * yet, or by some tracers (like irqsoff), that just want to
1104 * know if the ring buffer has been disabled, but it can handle
1105 * races of where it gets disabled but we still do a record.
1106 * As the check is in the fast path of the tracers, it is more
1107 * important to be fast than accurate.
1109 tr->buffer_disabled = 0;
1110 /* Make the flag seen by readers */
1115 * tracing_on - enable tracing buffers
1117 * This function enables tracing buffers that may have been
1118 * disabled with tracing_off.
1120 void tracing_on(void)
1122 tracer_tracing_on(&global_trace);
1124 EXPORT_SYMBOL_GPL(tracing_on);
1127 static __always_inline void
1128 __buffer_unlock_commit(struct trace_buffer *buffer, struct ring_buffer_event *event)
1130 __this_cpu_write(trace_taskinfo_save, true);
1132 /* If this is the temp buffer, we need to commit fully */
1133 if (this_cpu_read(trace_buffered_event) == event) {
1134 /* Length is in event->array[0] */
1135 ring_buffer_write(buffer, event->array[0], &event->array[1]);
1136 /* Release the temp buffer */
1137 this_cpu_dec(trace_buffered_event_cnt);
1138 /* ring_buffer_unlock_commit() enables preemption */
1139 preempt_enable_notrace();
1141 ring_buffer_unlock_commit(buffer);
1144 int __trace_array_puts(struct trace_array *tr, unsigned long ip,
1145 const char *str, int size)
1147 struct ring_buffer_event *event;
1148 struct trace_buffer *buffer;
1149 struct print_entry *entry;
1150 unsigned int trace_ctx;
1153 if (!(tr->trace_flags & TRACE_ITER_PRINTK))
1156 if (unlikely(tracing_selftest_running && tr == &global_trace))
1159 if (unlikely(tracing_disabled))
1162 alloc = sizeof(*entry) + size + 2; /* possible \n added */
1164 trace_ctx = tracing_gen_ctx();
1165 buffer = tr->array_buffer.buffer;
1166 ring_buffer_nest_start(buffer);
1167 event = __trace_buffer_lock_reserve(buffer, TRACE_PRINT, alloc,
1174 entry = ring_buffer_event_data(event);
1177 memcpy(&entry->buf, str, size);
1179 /* Add a newline if necessary */
1180 if (entry->buf[size - 1] != '\n') {
1181 entry->buf[size] = '\n';
1182 entry->buf[size + 1] = '\0';
1184 entry->buf[size] = '\0';
1186 __buffer_unlock_commit(buffer, event);
1187 ftrace_trace_stack(tr, buffer, trace_ctx, 4, NULL);
1189 ring_buffer_nest_end(buffer);
1192 EXPORT_SYMBOL_GPL(__trace_array_puts);
1195 * __trace_puts - write a constant string into the trace buffer.
1196 * @ip: The address of the caller
1197 * @str: The constant string to write
1198 * @size: The size of the string.
1200 int __trace_puts(unsigned long ip, const char *str, int size)
1202 return __trace_array_puts(printk_trace, ip, str, size);
1204 EXPORT_SYMBOL_GPL(__trace_puts);
1207 * __trace_bputs - write the pointer to a constant string into trace buffer
1208 * @ip: The address of the caller
1209 * @str: The constant string to write to the buffer to
1211 int __trace_bputs(unsigned long ip, const char *str)
1213 struct trace_array *tr = READ_ONCE(printk_trace);
1214 struct ring_buffer_event *event;
1215 struct trace_buffer *buffer;
1216 struct bputs_entry *entry;
1217 unsigned int trace_ctx;
1218 int size = sizeof(struct bputs_entry);
1221 if (!printk_binsafe(tr))
1222 return __trace_puts(ip, str, strlen(str));
1224 if (!(tr->trace_flags & TRACE_ITER_PRINTK))
1227 if (unlikely(tracing_selftest_running || tracing_disabled))
1230 trace_ctx = tracing_gen_ctx();
1231 buffer = tr->array_buffer.buffer;
1233 ring_buffer_nest_start(buffer);
1234 event = __trace_buffer_lock_reserve(buffer, TRACE_BPUTS, size,
1239 entry = ring_buffer_event_data(event);
1243 __buffer_unlock_commit(buffer, event);
1244 ftrace_trace_stack(tr, buffer, trace_ctx, 4, NULL);
1248 ring_buffer_nest_end(buffer);
1251 EXPORT_SYMBOL_GPL(__trace_bputs);
1253 #ifdef CONFIG_TRACER_SNAPSHOT
1254 static void tracing_snapshot_instance_cond(struct trace_array *tr,
1257 struct tracer *tracer = tr->current_trace;
1258 unsigned long flags;
1261 trace_array_puts(tr, "*** SNAPSHOT CALLED FROM NMI CONTEXT ***\n");
1262 trace_array_puts(tr, "*** snapshot is being ignored ***\n");
1266 if (!tr->allocated_snapshot) {
1267 trace_array_puts(tr, "*** SNAPSHOT NOT ALLOCATED ***\n");
1268 trace_array_puts(tr, "*** stopping trace here! ***\n");
1269 tracer_tracing_off(tr);
1273 /* Note, snapshot can not be used when the tracer uses it */
1274 if (tracer->use_max_tr) {
1275 trace_array_puts(tr, "*** LATENCY TRACER ACTIVE ***\n");
1276 trace_array_puts(tr, "*** Can not use snapshot (sorry) ***\n");
1281 trace_array_puts(tr, "*** BUFFER MEMORY MAPPED ***\n");
1282 trace_array_puts(tr, "*** Can not use snapshot (sorry) ***\n");
1286 local_irq_save(flags);
1287 update_max_tr(tr, current, smp_processor_id(), cond_data);
1288 local_irq_restore(flags);
1291 void tracing_snapshot_instance(struct trace_array *tr)
1293 tracing_snapshot_instance_cond(tr, NULL);
1297 * tracing_snapshot - take a snapshot of the current buffer.
1299 * This causes a swap between the snapshot buffer and the current live
1300 * tracing buffer. You can use this to take snapshots of the live
1301 * trace when some condition is triggered, but continue to trace.
1303 * Note, make sure to allocate the snapshot with either
1304 * a tracing_snapshot_alloc(), or by doing it manually
1305 * with: echo 1 > /sys/kernel/tracing/snapshot
1307 * If the snapshot buffer is not allocated, it will stop tracing.
1308 * Basically making a permanent snapshot.
1310 void tracing_snapshot(void)
1312 struct trace_array *tr = &global_trace;
1314 tracing_snapshot_instance(tr);
1316 EXPORT_SYMBOL_GPL(tracing_snapshot);
1319 * tracing_snapshot_cond - conditionally take a snapshot of the current buffer.
1320 * @tr: The tracing instance to snapshot
1321 * @cond_data: The data to be tested conditionally, and possibly saved
1323 * This is the same as tracing_snapshot() except that the snapshot is
1324 * conditional - the snapshot will only happen if the
1325 * cond_snapshot.update() implementation receiving the cond_data
1326 * returns true, which means that the trace array's cond_snapshot
1327 * update() operation used the cond_data to determine whether the
1328 * snapshot should be taken, and if it was, presumably saved it along
1329 * with the snapshot.
1331 void tracing_snapshot_cond(struct trace_array *tr, void *cond_data)
1333 tracing_snapshot_instance_cond(tr, cond_data);
1335 EXPORT_SYMBOL_GPL(tracing_snapshot_cond);
1338 * tracing_cond_snapshot_data - get the user data associated with a snapshot
1339 * @tr: The tracing instance
1341 * When the user enables a conditional snapshot using
1342 * tracing_snapshot_cond_enable(), the user-defined cond_data is saved
1343 * with the snapshot. This accessor is used to retrieve it.
1345 * Should not be called from cond_snapshot.update(), since it takes
1346 * the tr->max_lock lock, which the code calling
1347 * cond_snapshot.update() has already done.
1349 * Returns the cond_data associated with the trace array's snapshot.
1351 void *tracing_cond_snapshot_data(struct trace_array *tr)
1353 void *cond_data = NULL;
1355 local_irq_disable();
1356 arch_spin_lock(&tr->max_lock);
1358 if (tr->cond_snapshot)
1359 cond_data = tr->cond_snapshot->cond_data;
1361 arch_spin_unlock(&tr->max_lock);
1366 EXPORT_SYMBOL_GPL(tracing_cond_snapshot_data);
1368 static int resize_buffer_duplicate_size(struct array_buffer *trace_buf,
1369 struct array_buffer *size_buf, int cpu_id);
1370 static void set_buffer_entries(struct array_buffer *buf, unsigned long val);
1372 int tracing_alloc_snapshot_instance(struct trace_array *tr)
1377 if (!tr->allocated_snapshot) {
1379 /* Make the snapshot buffer have the same order as main buffer */
1380 order = ring_buffer_subbuf_order_get(tr->array_buffer.buffer);
1381 ret = ring_buffer_subbuf_order_set(tr->max_buffer.buffer, order);
1385 /* allocate spare buffer */
1386 ret = resize_buffer_duplicate_size(&tr->max_buffer,
1387 &tr->array_buffer, RING_BUFFER_ALL_CPUS);
1391 tr->allocated_snapshot = true;
1397 static void free_snapshot(struct trace_array *tr)
1400 * We don't free the ring buffer. instead, resize it because
1401 * The max_tr ring buffer has some state (e.g. ring->clock) and
1402 * we want preserve it.
1404 ring_buffer_subbuf_order_set(tr->max_buffer.buffer, 0);
1405 ring_buffer_resize(tr->max_buffer.buffer, 1, RING_BUFFER_ALL_CPUS);
1406 set_buffer_entries(&tr->max_buffer, 1);
1407 tracing_reset_online_cpus(&tr->max_buffer);
1408 tr->allocated_snapshot = false;
1411 static int tracing_arm_snapshot_locked(struct trace_array *tr)
1415 lockdep_assert_held(&trace_types_lock);
1417 spin_lock(&tr->snapshot_trigger_lock);
1418 if (tr->snapshot == UINT_MAX || tr->mapped) {
1419 spin_unlock(&tr->snapshot_trigger_lock);
1424 spin_unlock(&tr->snapshot_trigger_lock);
1426 ret = tracing_alloc_snapshot_instance(tr);
1428 spin_lock(&tr->snapshot_trigger_lock);
1430 spin_unlock(&tr->snapshot_trigger_lock);
1436 int tracing_arm_snapshot(struct trace_array *tr)
1440 mutex_lock(&trace_types_lock);
1441 ret = tracing_arm_snapshot_locked(tr);
1442 mutex_unlock(&trace_types_lock);
1447 void tracing_disarm_snapshot(struct trace_array *tr)
1449 spin_lock(&tr->snapshot_trigger_lock);
1450 if (!WARN_ON(!tr->snapshot))
1452 spin_unlock(&tr->snapshot_trigger_lock);
1456 * tracing_alloc_snapshot - allocate snapshot buffer.
1458 * This only allocates the snapshot buffer if it isn't already
1459 * allocated - it doesn't also take a snapshot.
1461 * This is meant to be used in cases where the snapshot buffer needs
1462 * to be set up for events that can't sleep but need to be able to
1463 * trigger a snapshot.
1465 int tracing_alloc_snapshot(void)
1467 struct trace_array *tr = &global_trace;
1470 ret = tracing_alloc_snapshot_instance(tr);
1475 EXPORT_SYMBOL_GPL(tracing_alloc_snapshot);
1478 * tracing_snapshot_alloc - allocate and take a snapshot of the current buffer.
1480 * This is similar to tracing_snapshot(), but it will allocate the
1481 * snapshot buffer if it isn't already allocated. Use this only
1482 * where it is safe to sleep, as the allocation may sleep.
1484 * This causes a swap between the snapshot buffer and the current live
1485 * tracing buffer. You can use this to take snapshots of the live
1486 * trace when some condition is triggered, but continue to trace.
1488 void tracing_snapshot_alloc(void)
1492 ret = tracing_alloc_snapshot();
1498 EXPORT_SYMBOL_GPL(tracing_snapshot_alloc);
1501 * tracing_snapshot_cond_enable - enable conditional snapshot for an instance
1502 * @tr: The tracing instance
1503 * @cond_data: User data to associate with the snapshot
1504 * @update: Implementation of the cond_snapshot update function
1506 * Check whether the conditional snapshot for the given instance has
1507 * already been enabled, or if the current tracer is already using a
1508 * snapshot; if so, return -EBUSY, else create a cond_snapshot and
1509 * save the cond_data and update function inside.
1511 * Returns 0 if successful, error otherwise.
1513 int tracing_snapshot_cond_enable(struct trace_array *tr, void *cond_data,
1514 cond_update_fn_t update)
1516 struct cond_snapshot *cond_snapshot __free(kfree) =
1517 kzalloc(sizeof(*cond_snapshot), GFP_KERNEL);
1523 cond_snapshot->cond_data = cond_data;
1524 cond_snapshot->update = update;
1526 guard(mutex)(&trace_types_lock);
1528 if (tr->current_trace->use_max_tr)
1532 * The cond_snapshot can only change to NULL without the
1533 * trace_types_lock. We don't care if we race with it going
1534 * to NULL, but we want to make sure that it's not set to
1535 * something other than NULL when we get here, which we can
1536 * do safely with only holding the trace_types_lock and not
1537 * having to take the max_lock.
1539 if (tr->cond_snapshot)
1542 ret = tracing_arm_snapshot_locked(tr);
1546 local_irq_disable();
1547 arch_spin_lock(&tr->max_lock);
1548 tr->cond_snapshot = no_free_ptr(cond_snapshot);
1549 arch_spin_unlock(&tr->max_lock);
1554 EXPORT_SYMBOL_GPL(tracing_snapshot_cond_enable);
1557 * tracing_snapshot_cond_disable - disable conditional snapshot for an instance
1558 * @tr: The tracing instance
1560 * Check whether the conditional snapshot for the given instance is
1561 * enabled; if so, free the cond_snapshot associated with it,
1562 * otherwise return -EINVAL.
1564 * Returns 0 if successful, error otherwise.
1566 int tracing_snapshot_cond_disable(struct trace_array *tr)
1570 local_irq_disable();
1571 arch_spin_lock(&tr->max_lock);
1573 if (!tr->cond_snapshot)
1576 kfree(tr->cond_snapshot);
1577 tr->cond_snapshot = NULL;
1580 arch_spin_unlock(&tr->max_lock);
1583 tracing_disarm_snapshot(tr);
1587 EXPORT_SYMBOL_GPL(tracing_snapshot_cond_disable);
1589 void tracing_snapshot(void)
1591 WARN_ONCE(1, "Snapshot feature not enabled, but internal snapshot used");
1593 EXPORT_SYMBOL_GPL(tracing_snapshot);
1594 void tracing_snapshot_cond(struct trace_array *tr, void *cond_data)
1596 WARN_ONCE(1, "Snapshot feature not enabled, but internal conditional snapshot used");
1598 EXPORT_SYMBOL_GPL(tracing_snapshot_cond);
1599 int tracing_alloc_snapshot(void)
1601 WARN_ONCE(1, "Snapshot feature not enabled, but snapshot allocation used");
1604 EXPORT_SYMBOL_GPL(tracing_alloc_snapshot);
1605 void tracing_snapshot_alloc(void)
1610 EXPORT_SYMBOL_GPL(tracing_snapshot_alloc);
1611 void *tracing_cond_snapshot_data(struct trace_array *tr)
1615 EXPORT_SYMBOL_GPL(tracing_cond_snapshot_data);
1616 int tracing_snapshot_cond_enable(struct trace_array *tr, void *cond_data, cond_update_fn_t update)
1620 EXPORT_SYMBOL_GPL(tracing_snapshot_cond_enable);
1621 int tracing_snapshot_cond_disable(struct trace_array *tr)
1625 EXPORT_SYMBOL_GPL(tracing_snapshot_cond_disable);
1626 #define free_snapshot(tr) do { } while (0)
1627 #define tracing_arm_snapshot_locked(tr) ({ -EBUSY; })
1628 #endif /* CONFIG_TRACER_SNAPSHOT */
1630 void tracer_tracing_off(struct trace_array *tr)
1632 if (tr->array_buffer.buffer)
1633 ring_buffer_record_off(tr->array_buffer.buffer);
1635 * This flag is looked at when buffers haven't been allocated
1636 * yet, or by some tracers (like irqsoff), that just want to
1637 * know if the ring buffer has been disabled, but it can handle
1638 * races of where it gets disabled but we still do a record.
1639 * As the check is in the fast path of the tracers, it is more
1640 * important to be fast than accurate.
1642 tr->buffer_disabled = 1;
1643 /* Make the flag seen by readers */
1648 * tracer_tracing_disable() - temporary disable the buffer from write
1649 * @tr: The trace array to disable its buffer for
1651 * Expects trace_tracing_enable() to re-enable tracing.
1652 * The difference between this and tracer_tracing_off() is that this
1653 * is a counter and can nest, whereas, tracer_tracing_off() can
1654 * be called multiple times and a single trace_tracing_on() will
1657 void tracer_tracing_disable(struct trace_array *tr)
1659 if (WARN_ON_ONCE(!tr->array_buffer.buffer))
1662 ring_buffer_record_disable(tr->array_buffer.buffer);
1666 * tracer_tracing_enable() - counter part of tracer_tracing_disable()
1667 * @tr: The trace array that had tracer_tracincg_disable() called on it
1669 * This is called after tracer_tracing_disable() has been called on @tr,
1670 * when it's safe to re-enable tracing.
1672 void tracer_tracing_enable(struct trace_array *tr)
1674 if (WARN_ON_ONCE(!tr->array_buffer.buffer))
1677 ring_buffer_record_enable(tr->array_buffer.buffer);
1681 * tracing_off - turn off tracing buffers
1683 * This function stops the tracing buffers from recording data.
1684 * It does not disable any overhead the tracers themselves may
1685 * be causing. This function simply causes all recording to
1686 * the ring buffers to fail.
1688 void tracing_off(void)
1690 tracer_tracing_off(&global_trace);
1692 EXPORT_SYMBOL_GPL(tracing_off);
1694 void disable_trace_on_warning(void)
1696 if (__disable_trace_on_warning) {
1697 trace_array_printk_buf(global_trace.array_buffer.buffer, _THIS_IP_,
1698 "Disabling tracing due to warning\n");
1704 * tracer_tracing_is_on - show real state of ring buffer enabled
1705 * @tr : the trace array to know if ring buffer is enabled
1707 * Shows real state of the ring buffer if it is enabled or not.
1709 bool tracer_tracing_is_on(struct trace_array *tr)
1711 if (tr->array_buffer.buffer)
1712 return ring_buffer_record_is_set_on(tr->array_buffer.buffer);
1713 return !tr->buffer_disabled;
1717 * tracing_is_on - show state of ring buffers enabled
1719 int tracing_is_on(void)
1721 return tracer_tracing_is_on(&global_trace);
1723 EXPORT_SYMBOL_GPL(tracing_is_on);
1725 static int __init set_buf_size(char *str)
1727 unsigned long buf_size;
1731 buf_size = memparse(str, &str);
1733 * nr_entries can not be zero and the startup
1734 * tests require some buffer space. Therefore
1735 * ensure we have at least 4096 bytes of buffer.
1737 trace_buf_size = max(4096UL, buf_size);
1740 __setup("trace_buf_size=", set_buf_size);
1742 static int __init set_tracing_thresh(char *str)
1744 unsigned long threshold;
1749 ret = kstrtoul(str, 0, &threshold);
1752 tracing_thresh = threshold * 1000;
1755 __setup("tracing_thresh=", set_tracing_thresh);
1757 unsigned long nsecs_to_usecs(unsigned long nsecs)
1759 return nsecs / 1000;
1763 * TRACE_FLAGS is defined as a tuple matching bit masks with strings.
1764 * It uses C(a, b) where 'a' is the eval (enum) name and 'b' is the string that
1765 * matches it. By defining "C(a, b) b", TRACE_FLAGS becomes a list
1766 * of strings in the order that the evals (enum) were defined.
1771 /* These must match the bit positions in trace_iterator_flags */
1772 static const char *trace_options[] = {
1780 int in_ns; /* is this clock in nanoseconds? */
1781 } trace_clocks[] = {
1782 { trace_clock_local, "local", 1 },
1783 { trace_clock_global, "global", 1 },
1784 { trace_clock_counter, "counter", 0 },
1785 { trace_clock_jiffies, "uptime", 0 },
1786 { trace_clock, "perf", 1 },
1787 { ktime_get_mono_fast_ns, "mono", 1 },
1788 { ktime_get_raw_fast_ns, "mono_raw", 1 },
1789 { ktime_get_boot_fast_ns, "boot", 1 },
1790 { ktime_get_tai_fast_ns, "tai", 1 },
1794 bool trace_clock_in_ns(struct trace_array *tr)
1796 if (trace_clocks[tr->clock_id].in_ns)
1803 * trace_parser_get_init - gets the buffer for trace parser
1805 int trace_parser_get_init(struct trace_parser *parser, int size)
1807 memset(parser, 0, sizeof(*parser));
1809 parser->buffer = kmalloc(size, GFP_KERNEL);
1810 if (!parser->buffer)
1813 parser->size = size;
1818 * trace_parser_put - frees the buffer for trace parser
1820 void trace_parser_put(struct trace_parser *parser)
1822 kfree(parser->buffer);
1823 parser->buffer = NULL;
1827 * trace_get_user - reads the user input string separated by space
1828 * (matched by isspace(ch))
1830 * For each string found the 'struct trace_parser' is updated,
1831 * and the function returns.
1833 * Returns number of bytes read.
1835 * See kernel/trace/trace.h for 'struct trace_parser' details.
1837 int trace_get_user(struct trace_parser *parser, const char __user *ubuf,
1838 size_t cnt, loff_t *ppos)
1845 trace_parser_clear(parser);
1847 ret = get_user(ch, ubuf++);
1855 * The parser is not finished with the last write,
1856 * continue reading the user input without skipping spaces.
1858 if (!parser->cont) {
1859 /* skip white space */
1860 while (cnt && isspace(ch)) {
1861 ret = get_user(ch, ubuf++);
1870 /* only spaces were written */
1871 if (isspace(ch) || !ch) {
1878 /* read the non-space input */
1879 while (cnt && !isspace(ch) && ch) {
1880 if (parser->idx < parser->size - 1)
1881 parser->buffer[parser->idx++] = ch;
1886 ret = get_user(ch, ubuf++);
1893 /* We either got finished input or we have to wait for another call. */
1894 if (isspace(ch) || !ch) {
1895 parser->buffer[parser->idx] = 0;
1896 parser->cont = false;
1897 } else if (parser->idx < parser->size - 1) {
1898 parser->cont = true;
1899 parser->buffer[parser->idx++] = ch;
1900 /* Make sure the parsed string always terminates with '\0'. */
1901 parser->buffer[parser->idx] = 0;
1914 /* TODO add a seq_buf_to_buffer() */
1915 static ssize_t trace_seq_to_buffer(struct trace_seq *s, void *buf, size_t cnt)
1919 if (trace_seq_used(s) <= s->readpos)
1922 len = trace_seq_used(s) - s->readpos;
1925 memcpy(buf, s->buffer + s->readpos, cnt);
1931 unsigned long __read_mostly tracing_thresh;
1933 #ifdef CONFIG_TRACER_MAX_TRACE
1934 static const struct file_operations tracing_max_lat_fops;
1936 #ifdef LATENCY_FS_NOTIFY
1938 static struct workqueue_struct *fsnotify_wq;
1940 static void latency_fsnotify_workfn(struct work_struct *work)
1942 struct trace_array *tr = container_of(work, struct trace_array,
1944 fsnotify_inode(tr->d_max_latency->d_inode, FS_MODIFY);
1947 static void latency_fsnotify_workfn_irq(struct irq_work *iwork)
1949 struct trace_array *tr = container_of(iwork, struct trace_array,
1951 queue_work(fsnotify_wq, &tr->fsnotify_work);
1954 static void trace_create_maxlat_file(struct trace_array *tr,
1955 struct dentry *d_tracer)
1957 INIT_WORK(&tr->fsnotify_work, latency_fsnotify_workfn);
1958 init_irq_work(&tr->fsnotify_irqwork, latency_fsnotify_workfn_irq);
1959 tr->d_max_latency = trace_create_file("tracing_max_latency",
1962 &tracing_max_lat_fops);
1965 __init static int latency_fsnotify_init(void)
1967 fsnotify_wq = alloc_workqueue("tr_max_lat_wq",
1968 WQ_UNBOUND | WQ_HIGHPRI, 0);
1970 pr_err("Unable to allocate tr_max_lat_wq\n");
1976 late_initcall_sync(latency_fsnotify_init);
1978 void latency_fsnotify(struct trace_array *tr)
1983 * We cannot call queue_work(&tr->fsnotify_work) from here because it's
1984 * possible that we are called from __schedule() or do_idle(), which
1985 * could cause a deadlock.
1987 irq_work_queue(&tr->fsnotify_irqwork);
1990 #else /* !LATENCY_FS_NOTIFY */
1992 #define trace_create_maxlat_file(tr, d_tracer) \
1993 trace_create_file("tracing_max_latency", TRACE_MODE_WRITE, \
1994 d_tracer, tr, &tracing_max_lat_fops)
1999 * Copy the new maximum trace into the separate maximum-trace
2000 * structure. (this way the maximum trace is permanently saved,
2001 * for later retrieval via /sys/kernel/tracing/tracing_max_latency)
2004 __update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
2006 struct array_buffer *trace_buf = &tr->array_buffer;
2007 struct array_buffer *max_buf = &tr->max_buffer;
2008 struct trace_array_cpu *data = per_cpu_ptr(trace_buf->data, cpu);
2009 struct trace_array_cpu *max_data = per_cpu_ptr(max_buf->data, cpu);
2012 max_buf->time_start = data->preempt_timestamp;
2014 max_data->saved_latency = tr->max_latency;
2015 max_data->critical_start = data->critical_start;
2016 max_data->critical_end = data->critical_end;
2018 strscpy(max_data->comm, tsk->comm);
2019 max_data->pid = tsk->pid;
2021 * If tsk == current, then use current_uid(), as that does not use
2022 * RCU. The irq tracer can be called out of RCU scope.
2025 max_data->uid = current_uid();
2027 max_data->uid = task_uid(tsk);
2029 max_data->nice = tsk->static_prio - 20 - MAX_RT_PRIO;
2030 max_data->policy = tsk->policy;
2031 max_data->rt_priority = tsk->rt_priority;
2033 /* record this tasks comm */
2034 tracing_record_cmdline(tsk);
2035 latency_fsnotify(tr);
2039 * update_max_tr - snapshot all trace buffers from global_trace to max_tr
2041 * @tsk: the task with the latency
2042 * @cpu: The cpu that initiated the trace.
2043 * @cond_data: User data associated with a conditional snapshot
2045 * Flip the buffers between the @tr and the max_tr and record information
2046 * about which task was the cause of this latency.
2049 update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu,
2055 WARN_ON_ONCE(!irqs_disabled());
2057 if (!tr->allocated_snapshot) {
2058 /* Only the nop tracer should hit this when disabling */
2059 WARN_ON_ONCE(tr->current_trace != &nop_trace);
2063 arch_spin_lock(&tr->max_lock);
2065 /* Inherit the recordable setting from array_buffer */
2066 if (ring_buffer_record_is_set_on(tr->array_buffer.buffer))
2067 ring_buffer_record_on(tr->max_buffer.buffer);
2069 ring_buffer_record_off(tr->max_buffer.buffer);
2071 #ifdef CONFIG_TRACER_SNAPSHOT
2072 if (tr->cond_snapshot && !tr->cond_snapshot->update(tr, cond_data)) {
2073 arch_spin_unlock(&tr->max_lock);
2077 swap(tr->array_buffer.buffer, tr->max_buffer.buffer);
2079 __update_max_tr(tr, tsk, cpu);
2081 arch_spin_unlock(&tr->max_lock);
2083 /* Any waiters on the old snapshot buffer need to wake up */
2084 ring_buffer_wake_waiters(tr->array_buffer.buffer, RING_BUFFER_ALL_CPUS);
2088 * update_max_tr_single - only copy one trace over, and reset the rest
2090 * @tsk: task with the latency
2091 * @cpu: the cpu of the buffer to copy.
2093 * Flip the trace of a single CPU buffer between the @tr and the max_tr.
2096 update_max_tr_single(struct trace_array *tr, struct task_struct *tsk, int cpu)
2103 WARN_ON_ONCE(!irqs_disabled());
2104 if (!tr->allocated_snapshot) {
2105 /* Only the nop tracer should hit this when disabling */
2106 WARN_ON_ONCE(tr->current_trace != &nop_trace);
2110 arch_spin_lock(&tr->max_lock);
2112 ret = ring_buffer_swap_cpu(tr->max_buffer.buffer, tr->array_buffer.buffer, cpu);
2114 if (ret == -EBUSY) {
2116 * We failed to swap the buffer due to a commit taking
2117 * place on this CPU. We fail to record, but we reset
2118 * the max trace buffer (no one writes directly to it)
2119 * and flag that it failed.
2120 * Another reason is resize is in progress.
2122 trace_array_printk_buf(tr->max_buffer.buffer, _THIS_IP_,
2123 "Failed to swap buffers due to commit or resize in progress\n");
2126 WARN_ON_ONCE(ret && ret != -EAGAIN && ret != -EBUSY);
2128 __update_max_tr(tr, tsk, cpu);
2129 arch_spin_unlock(&tr->max_lock);
2132 #endif /* CONFIG_TRACER_MAX_TRACE */
2135 struct trace_iterator *iter;
2139 static bool wait_pipe_cond(void *data)
2141 struct pipe_wait *pwait = data;
2142 struct trace_iterator *iter = pwait->iter;
2144 if (atomic_read_acquire(&iter->wait_index) != pwait->wait_index)
2147 return iter->closed;
2150 static int wait_on_pipe(struct trace_iterator *iter, int full)
2152 struct pipe_wait pwait;
2155 /* Iterators are static, they should be filled or empty */
2156 if (trace_buffer_iter(iter, iter->cpu_file))
2159 pwait.wait_index = atomic_read_acquire(&iter->wait_index);
2162 ret = ring_buffer_wait(iter->array_buffer->buffer, iter->cpu_file, full,
2163 wait_pipe_cond, &pwait);
2165 #ifdef CONFIG_TRACER_MAX_TRACE
2167 * Make sure this is still the snapshot buffer, as if a snapshot were
2168 * to happen, this would now be the main buffer.
2171 iter->array_buffer = &iter->tr->max_buffer;
2176 #ifdef CONFIG_FTRACE_STARTUP_TEST
2177 static bool selftests_can_run;
2179 struct trace_selftests {
2180 struct list_head list;
2181 struct tracer *type;
2184 static LIST_HEAD(postponed_selftests);
2186 static int save_selftest(struct tracer *type)
2188 struct trace_selftests *selftest;
2190 selftest = kmalloc(sizeof(*selftest), GFP_KERNEL);
2194 selftest->type = type;
2195 list_add(&selftest->list, &postponed_selftests);
2199 static int run_tracer_selftest(struct tracer *type)
2201 struct trace_array *tr = &global_trace;
2202 struct tracer *saved_tracer = tr->current_trace;
2205 if (!type->selftest || tracing_selftest_disabled)
2209 * If a tracer registers early in boot up (before scheduling is
2210 * initialized and such), then do not run its selftests yet.
2211 * Instead, run it a little later in the boot process.
2213 if (!selftests_can_run)
2214 return save_selftest(type);
2216 if (!tracing_is_on()) {
2217 pr_warn("Selftest for tracer %s skipped due to tracing disabled\n",
2223 * Run a selftest on this tracer.
2224 * Here we reset the trace buffer, and set the current
2225 * tracer to be this tracer. The tracer can then run some
2226 * internal tracing to verify that everything is in order.
2227 * If we fail, we do not register this tracer.
2229 tracing_reset_online_cpus(&tr->array_buffer);
2231 tr->current_trace = type;
2233 #ifdef CONFIG_TRACER_MAX_TRACE
2234 if (type->use_max_tr) {
2235 /* If we expanded the buffers, make sure the max is expanded too */
2236 if (tr->ring_buffer_expanded)
2237 ring_buffer_resize(tr->max_buffer.buffer, trace_buf_size,
2238 RING_BUFFER_ALL_CPUS);
2239 tr->allocated_snapshot = true;
2243 /* the test is responsible for initializing and enabling */
2244 pr_info("Testing tracer %s: ", type->name);
2245 ret = type->selftest(type, tr);
2246 /* the test is responsible for resetting too */
2247 tr->current_trace = saved_tracer;
2249 printk(KERN_CONT "FAILED!\n");
2250 /* Add the warning after printing 'FAILED' */
2254 /* Only reset on passing, to avoid touching corrupted buffers */
2255 tracing_reset_online_cpus(&tr->array_buffer);
2257 #ifdef CONFIG_TRACER_MAX_TRACE
2258 if (type->use_max_tr) {
2259 tr->allocated_snapshot = false;
2261 /* Shrink the max buffer again */
2262 if (tr->ring_buffer_expanded)
2263 ring_buffer_resize(tr->max_buffer.buffer, 1,
2264 RING_BUFFER_ALL_CPUS);
2268 printk(KERN_CONT "PASSED\n");
2272 static int do_run_tracer_selftest(struct tracer *type)
2277 * Tests can take a long time, especially if they are run one after the
2278 * other, as does happen during bootup when all the tracers are
2279 * registered. This could cause the soft lockup watchdog to trigger.
2283 tracing_selftest_running = true;
2284 ret = run_tracer_selftest(type);
2285 tracing_selftest_running = false;
2290 static __init int init_trace_selftests(void)
2292 struct trace_selftests *p, *n;
2293 struct tracer *t, **last;
2296 selftests_can_run = true;
2298 guard(mutex)(&trace_types_lock);
2300 if (list_empty(&postponed_selftests))
2303 pr_info("Running postponed tracer tests:\n");
2305 tracing_selftest_running = true;
2306 list_for_each_entry_safe(p, n, &postponed_selftests, list) {
2307 /* This loop can take minutes when sanitizers are enabled, so
2308 * lets make sure we allow RCU processing.
2311 ret = run_tracer_selftest(p->type);
2312 /* If the test fails, then warn and remove from available_tracers */
2314 WARN(1, "tracer: %s failed selftest, disabling\n",
2316 last = &trace_types;
2317 for (t = trace_types; t; t = t->next) {
2328 tracing_selftest_running = false;
2332 core_initcall(init_trace_selftests);
2334 static inline int do_run_tracer_selftest(struct tracer *type)
2338 #endif /* CONFIG_FTRACE_STARTUP_TEST */
2340 static void add_tracer_options(struct trace_array *tr, struct tracer *t);
2342 static void __init apply_trace_boot_options(void);
2345 * register_tracer - register a tracer with the ftrace system.
2346 * @type: the plugin for the tracer
2348 * Register a new plugin tracer.
2350 int __init register_tracer(struct tracer *type)
2356 pr_info("Tracer must have a name\n");
2360 if (strlen(type->name) >= MAX_TRACER_SIZE) {
2361 pr_info("Tracer has a name longer than %d\n", MAX_TRACER_SIZE);
2365 if (security_locked_down(LOCKDOWN_TRACEFS)) {
2366 pr_warn("Can not register tracer %s due to lockdown\n",
2371 mutex_lock(&trace_types_lock);
2373 for (t = trace_types; t; t = t->next) {
2374 if (strcmp(type->name, t->name) == 0) {
2376 pr_info("Tracer %s already registered\n",
2383 if (!type->set_flag)
2384 type->set_flag = &dummy_set_flag;
2386 /*allocate a dummy tracer_flags*/
2387 type->flags = kmalloc(sizeof(*type->flags), GFP_KERNEL);
2392 type->flags->val = 0;
2393 type->flags->opts = dummy_tracer_opt;
2395 if (!type->flags->opts)
2396 type->flags->opts = dummy_tracer_opt;
2398 /* store the tracer for __set_tracer_option */
2399 type->flags->trace = type;
2401 ret = do_run_tracer_selftest(type);
2405 type->next = trace_types;
2407 add_tracer_options(&global_trace, type);
2410 mutex_unlock(&trace_types_lock);
2412 if (ret || !default_bootup_tracer)
2415 if (strncmp(default_bootup_tracer, type->name, MAX_TRACER_SIZE))
2418 printk(KERN_INFO "Starting tracer '%s'\n", type->name);
2419 /* Do we want this tracer to start on bootup? */
2420 tracing_set_tracer(&global_trace, type->name);
2421 default_bootup_tracer = NULL;
2423 apply_trace_boot_options();
2425 /* disable other selftests, since this will break it. */
2426 disable_tracing_selftest("running a tracer");
2432 static void tracing_reset_cpu(struct array_buffer *buf, int cpu)
2434 struct trace_buffer *buffer = buf->buffer;
2439 ring_buffer_record_disable(buffer);
2441 /* Make sure all commits have finished */
2443 ring_buffer_reset_cpu(buffer, cpu);
2445 ring_buffer_record_enable(buffer);
2448 void tracing_reset_online_cpus(struct array_buffer *buf)
2450 struct trace_buffer *buffer = buf->buffer;
2455 ring_buffer_record_disable(buffer);
2457 /* Make sure all commits have finished */
2460 buf->time_start = buffer_ftrace_now(buf, buf->cpu);
2462 ring_buffer_reset_online_cpus(buffer);
2464 ring_buffer_record_enable(buffer);
2467 static void tracing_reset_all_cpus(struct array_buffer *buf)
2469 struct trace_buffer *buffer = buf->buffer;
2474 ring_buffer_record_disable(buffer);
2476 /* Make sure all commits have finished */
2479 buf->time_start = buffer_ftrace_now(buf, buf->cpu);
2481 ring_buffer_reset(buffer);
2483 ring_buffer_record_enable(buffer);
2486 /* Must have trace_types_lock held */
2487 void tracing_reset_all_online_cpus_unlocked(void)
2489 struct trace_array *tr;
2491 lockdep_assert_held(&trace_types_lock);
2493 list_for_each_entry(tr, &ftrace_trace_arrays, list) {
2494 if (!tr->clear_trace)
2496 tr->clear_trace = false;
2497 tracing_reset_online_cpus(&tr->array_buffer);
2498 #ifdef CONFIG_TRACER_MAX_TRACE
2499 tracing_reset_online_cpus(&tr->max_buffer);
2504 void tracing_reset_all_online_cpus(void)
2506 mutex_lock(&trace_types_lock);
2507 tracing_reset_all_online_cpus_unlocked();
2508 mutex_unlock(&trace_types_lock);
2511 int is_tracing_stopped(void)
2513 return global_trace.stop_count;
2516 static void tracing_start_tr(struct trace_array *tr)
2518 struct trace_buffer *buffer;
2519 unsigned long flags;
2521 if (tracing_disabled)
2524 raw_spin_lock_irqsave(&tr->start_lock, flags);
2525 if (--tr->stop_count) {
2526 if (WARN_ON_ONCE(tr->stop_count < 0)) {
2527 /* Someone screwed up their debugging */
2533 /* Prevent the buffers from switching */
2534 arch_spin_lock(&tr->max_lock);
2536 buffer = tr->array_buffer.buffer;
2538 ring_buffer_record_enable(buffer);
2540 #ifdef CONFIG_TRACER_MAX_TRACE
2541 buffer = tr->max_buffer.buffer;
2543 ring_buffer_record_enable(buffer);
2546 arch_spin_unlock(&tr->max_lock);
2549 raw_spin_unlock_irqrestore(&tr->start_lock, flags);
2553 * tracing_start - quick start of the tracer
2555 * If tracing is enabled but was stopped by tracing_stop,
2556 * this will start the tracer back up.
2558 void tracing_start(void)
2561 return tracing_start_tr(&global_trace);
2564 static void tracing_stop_tr(struct trace_array *tr)
2566 struct trace_buffer *buffer;
2567 unsigned long flags;
2569 raw_spin_lock_irqsave(&tr->start_lock, flags);
2570 if (tr->stop_count++)
2573 /* Prevent the buffers from switching */
2574 arch_spin_lock(&tr->max_lock);
2576 buffer = tr->array_buffer.buffer;
2578 ring_buffer_record_disable(buffer);
2580 #ifdef CONFIG_TRACER_MAX_TRACE
2581 buffer = tr->max_buffer.buffer;
2583 ring_buffer_record_disable(buffer);
2586 arch_spin_unlock(&tr->max_lock);
2589 raw_spin_unlock_irqrestore(&tr->start_lock, flags);
2593 * tracing_stop - quick stop of the tracer
2595 * Light weight way to stop tracing. Use in conjunction with
2598 void tracing_stop(void)
2600 return tracing_stop_tr(&global_trace);
2604 * Several functions return TRACE_TYPE_PARTIAL_LINE if the trace_seq
2605 * overflowed, and TRACE_TYPE_HANDLED otherwise. This helper function
2606 * simplifies those functions and keeps them in sync.
2608 enum print_line_t trace_handle_return(struct trace_seq *s)
2610 return trace_seq_has_overflowed(s) ?
2611 TRACE_TYPE_PARTIAL_LINE : TRACE_TYPE_HANDLED;
2613 EXPORT_SYMBOL_GPL(trace_handle_return);
2615 static unsigned short migration_disable_value(void)
2617 #if defined(CONFIG_SMP)
2618 return current->migration_disabled;
2624 unsigned int tracing_gen_ctx_irq_test(unsigned int irqs_status)
2626 unsigned int trace_flags = irqs_status;
2629 pc = preempt_count();
2632 trace_flags |= TRACE_FLAG_NMI;
2633 if (pc & HARDIRQ_MASK)
2634 trace_flags |= TRACE_FLAG_HARDIRQ;
2635 if (in_serving_softirq())
2636 trace_flags |= TRACE_FLAG_SOFTIRQ;
2637 if (softirq_count() >> (SOFTIRQ_SHIFT + 1))
2638 trace_flags |= TRACE_FLAG_BH_OFF;
2640 if (tif_need_resched())
2641 trace_flags |= TRACE_FLAG_NEED_RESCHED;
2642 if (test_preempt_need_resched())
2643 trace_flags |= TRACE_FLAG_PREEMPT_RESCHED;
2644 if (IS_ENABLED(CONFIG_ARCH_HAS_PREEMPT_LAZY) && tif_test_bit(TIF_NEED_RESCHED_LAZY))
2645 trace_flags |= TRACE_FLAG_NEED_RESCHED_LAZY;
2646 return (trace_flags << 16) | (min_t(unsigned int, pc & 0xff, 0xf)) |
2647 (min_t(unsigned int, migration_disable_value(), 0xf)) << 4;
2650 struct ring_buffer_event *
2651 trace_buffer_lock_reserve(struct trace_buffer *buffer,
2654 unsigned int trace_ctx)
2656 return __trace_buffer_lock_reserve(buffer, type, len, trace_ctx);
2659 DEFINE_PER_CPU(struct ring_buffer_event *, trace_buffered_event);
2660 DEFINE_PER_CPU(int, trace_buffered_event_cnt);
2661 static int trace_buffered_event_ref;
2664 * trace_buffered_event_enable - enable buffering events
2666 * When events are being filtered, it is quicker to use a temporary
2667 * buffer to write the event data into if there's a likely chance
2668 * that it will not be committed. The discard of the ring buffer
2669 * is not as fast as committing, and is much slower than copying
2672 * When an event is to be filtered, allocate per cpu buffers to
2673 * write the event data into, and if the event is filtered and discarded
2674 * it is simply dropped, otherwise, the entire data is to be committed
2677 void trace_buffered_event_enable(void)
2679 struct ring_buffer_event *event;
2683 WARN_ON_ONCE(!mutex_is_locked(&event_mutex));
2685 if (trace_buffered_event_ref++)
2688 for_each_tracing_cpu(cpu) {
2689 page = alloc_pages_node(cpu_to_node(cpu),
2690 GFP_KERNEL | __GFP_NORETRY, 0);
2691 /* This is just an optimization and can handle failures */
2693 pr_err("Failed to allocate event buffer\n");
2697 event = page_address(page);
2698 memset(event, 0, sizeof(*event));
2700 per_cpu(trace_buffered_event, cpu) = event;
2703 if (cpu == smp_processor_id() &&
2704 __this_cpu_read(trace_buffered_event) !=
2705 per_cpu(trace_buffered_event, cpu))
2711 static void enable_trace_buffered_event(void *data)
2713 /* Probably not needed, but do it anyway */
2715 this_cpu_dec(trace_buffered_event_cnt);
2718 static void disable_trace_buffered_event(void *data)
2720 this_cpu_inc(trace_buffered_event_cnt);
2724 * trace_buffered_event_disable - disable buffering events
2726 * When a filter is removed, it is faster to not use the buffered
2727 * events, and to commit directly into the ring buffer. Free up
2728 * the temp buffers when there are no more users. This requires
2729 * special synchronization with current events.
2731 void trace_buffered_event_disable(void)
2735 WARN_ON_ONCE(!mutex_is_locked(&event_mutex));
2737 if (WARN_ON_ONCE(!trace_buffered_event_ref))
2740 if (--trace_buffered_event_ref)
2743 /* For each CPU, set the buffer as used. */
2744 on_each_cpu_mask(tracing_buffer_mask, disable_trace_buffered_event,
2747 /* Wait for all current users to finish */
2750 for_each_tracing_cpu(cpu) {
2751 free_page((unsigned long)per_cpu(trace_buffered_event, cpu));
2752 per_cpu(trace_buffered_event, cpu) = NULL;
2756 * Wait for all CPUs that potentially started checking if they can use
2757 * their event buffer only after the previous synchronize_rcu() call and
2758 * they still read a valid pointer from trace_buffered_event. It must be
2759 * ensured they don't see cleared trace_buffered_event_cnt else they
2760 * could wrongly decide to use the pointed-to buffer which is now freed.
2764 /* For each CPU, relinquish the buffer */
2765 on_each_cpu_mask(tracing_buffer_mask, enable_trace_buffered_event, NULL,
2769 static struct trace_buffer *temp_buffer;
2771 struct ring_buffer_event *
2772 trace_event_buffer_lock_reserve(struct trace_buffer **current_rb,
2773 struct trace_event_file *trace_file,
2774 int type, unsigned long len,
2775 unsigned int trace_ctx)
2777 struct ring_buffer_event *entry;
2778 struct trace_array *tr = trace_file->tr;
2781 *current_rb = tr->array_buffer.buffer;
2783 if (!tr->no_filter_buffering_ref &&
2784 (trace_file->flags & (EVENT_FILE_FL_SOFT_DISABLED | EVENT_FILE_FL_FILTERED))) {
2785 preempt_disable_notrace();
2787 * Filtering is on, so try to use the per cpu buffer first.
2788 * This buffer will simulate a ring_buffer_event,
2789 * where the type_len is zero and the array[0] will
2790 * hold the full length.
2791 * (see include/linux/ring-buffer.h for details on
2792 * how the ring_buffer_event is structured).
2794 * Using a temp buffer during filtering and copying it
2795 * on a matched filter is quicker than writing directly
2796 * into the ring buffer and then discarding it when
2797 * it doesn't match. That is because the discard
2798 * requires several atomic operations to get right.
2799 * Copying on match and doing nothing on a failed match
2800 * is still quicker than no copy on match, but having
2801 * to discard out of the ring buffer on a failed match.
2803 if ((entry = __this_cpu_read(trace_buffered_event))) {
2804 int max_len = PAGE_SIZE - struct_size(entry, array, 1);
2806 val = this_cpu_inc_return(trace_buffered_event_cnt);
2809 * Preemption is disabled, but interrupts and NMIs
2810 * can still come in now. If that happens after
2811 * the above increment, then it will have to go
2812 * back to the old method of allocating the event
2813 * on the ring buffer, and if the filter fails, it
2814 * will have to call ring_buffer_discard_commit()
2817 * Need to also check the unlikely case that the
2818 * length is bigger than the temp buffer size.
2819 * If that happens, then the reserve is pretty much
2820 * guaranteed to fail, as the ring buffer currently
2821 * only allows events less than a page. But that may
2822 * change in the future, so let the ring buffer reserve
2823 * handle the failure in that case.
2825 if (val == 1 && likely(len <= max_len)) {
2826 trace_event_setup(entry, type, trace_ctx);
2827 entry->array[0] = len;
2828 /* Return with preemption disabled */
2831 this_cpu_dec(trace_buffered_event_cnt);
2833 /* __trace_buffer_lock_reserve() disables preemption */
2834 preempt_enable_notrace();
2837 entry = __trace_buffer_lock_reserve(*current_rb, type, len,
2840 * If tracing is off, but we have triggers enabled
2841 * we still need to look at the event data. Use the temp_buffer
2842 * to store the trace event for the trigger to use. It's recursive
2843 * safe and will not be recorded anywhere.
2845 if (!entry && trace_file->flags & EVENT_FILE_FL_TRIGGER_COND) {
2846 *current_rb = temp_buffer;
2847 entry = __trace_buffer_lock_reserve(*current_rb, type, len,
2852 EXPORT_SYMBOL_GPL(trace_event_buffer_lock_reserve);
2854 static DEFINE_RAW_SPINLOCK(tracepoint_iter_lock);
2855 static DEFINE_MUTEX(tracepoint_printk_mutex);
2857 static void output_printk(struct trace_event_buffer *fbuffer)
2859 struct trace_event_call *event_call;
2860 struct trace_event_file *file;
2861 struct trace_event *event;
2862 unsigned long flags;
2863 struct trace_iterator *iter = tracepoint_print_iter;
2865 /* We should never get here if iter is NULL */
2866 if (WARN_ON_ONCE(!iter))
2869 event_call = fbuffer->trace_file->event_call;
2870 if (!event_call || !event_call->event.funcs ||
2871 !event_call->event.funcs->trace)
2874 file = fbuffer->trace_file;
2875 if (test_bit(EVENT_FILE_FL_SOFT_DISABLED_BIT, &file->flags) ||
2876 (unlikely(file->flags & EVENT_FILE_FL_FILTERED) &&
2877 !filter_match_preds(file->filter, fbuffer->entry)))
2880 event = &fbuffer->trace_file->event_call->event;
2882 raw_spin_lock_irqsave(&tracepoint_iter_lock, flags);
2883 trace_seq_init(&iter->seq);
2884 iter->ent = fbuffer->entry;
2885 event_call->event.funcs->trace(iter, 0, event);
2886 trace_seq_putc(&iter->seq, 0);
2887 printk("%s", iter->seq.buffer);
2889 raw_spin_unlock_irqrestore(&tracepoint_iter_lock, flags);
2892 int tracepoint_printk_sysctl(const struct ctl_table *table, int write,
2893 void *buffer, size_t *lenp,
2896 int save_tracepoint_printk;
2899 guard(mutex)(&tracepoint_printk_mutex);
2900 save_tracepoint_printk = tracepoint_printk;
2902 ret = proc_dointvec(table, write, buffer, lenp, ppos);
2905 * This will force exiting early, as tracepoint_printk
2906 * is always zero when tracepoint_printk_iter is not allocated
2908 if (!tracepoint_print_iter)
2909 tracepoint_printk = 0;
2911 if (save_tracepoint_printk == tracepoint_printk)
2914 if (tracepoint_printk)
2915 static_key_enable(&tracepoint_printk_key.key);
2917 static_key_disable(&tracepoint_printk_key.key);
2922 void trace_event_buffer_commit(struct trace_event_buffer *fbuffer)
2924 enum event_trigger_type tt = ETT_NONE;
2925 struct trace_event_file *file = fbuffer->trace_file;
2927 if (__event_trigger_test_discard(file, fbuffer->buffer, fbuffer->event,
2928 fbuffer->entry, &tt))
2931 if (static_key_false(&tracepoint_printk_key.key))
2932 output_printk(fbuffer);
2934 if (static_branch_unlikely(&trace_event_exports_enabled))
2935 ftrace_exports(fbuffer->event, TRACE_EXPORT_EVENT);
2937 trace_buffer_unlock_commit_regs(file->tr, fbuffer->buffer,
2938 fbuffer->event, fbuffer->trace_ctx, fbuffer->regs);
2942 event_triggers_post_call(file, tt);
2945 EXPORT_SYMBOL_GPL(trace_event_buffer_commit);
2950 * trace_buffer_unlock_commit_regs()
2951 * trace_event_buffer_commit()
2952 * trace_event_raw_event_xxx()
2954 # define STACK_SKIP 3
2956 void trace_buffer_unlock_commit_regs(struct trace_array *tr,
2957 struct trace_buffer *buffer,
2958 struct ring_buffer_event *event,
2959 unsigned int trace_ctx,
2960 struct pt_regs *regs)
2962 __buffer_unlock_commit(buffer, event);
2965 * If regs is not set, then skip the necessary functions.
2966 * Note, we can still get here via blktrace, wakeup tracer
2967 * and mmiotrace, but that's ok if they lose a function or
2968 * two. They are not that meaningful.
2970 ftrace_trace_stack(tr, buffer, trace_ctx, regs ? 0 : STACK_SKIP, regs);
2971 ftrace_trace_userstack(tr, buffer, trace_ctx);
2975 * Similar to trace_buffer_unlock_commit_regs() but do not dump stack.
2978 trace_buffer_unlock_commit_nostack(struct trace_buffer *buffer,
2979 struct ring_buffer_event *event)
2981 __buffer_unlock_commit(buffer, event);
2985 trace_function(struct trace_array *tr, unsigned long ip, unsigned long
2986 parent_ip, unsigned int trace_ctx, struct ftrace_regs *fregs)
2988 struct trace_buffer *buffer = tr->array_buffer.buffer;
2989 struct ring_buffer_event *event;
2990 struct ftrace_entry *entry;
2991 int size = sizeof(*entry);
2993 size += FTRACE_REGS_MAX_ARGS * !!fregs * sizeof(long);
2995 event = __trace_buffer_lock_reserve(buffer, TRACE_FN, size,
2999 entry = ring_buffer_event_data(event);
3001 entry->parent_ip = parent_ip;
3003 #ifdef CONFIG_HAVE_FUNCTION_ARG_ACCESS_API
3005 for (int i = 0; i < FTRACE_REGS_MAX_ARGS; i++)
3006 entry->args[i] = ftrace_regs_get_argument(fregs, i);
3010 if (static_branch_unlikely(&trace_function_exports_enabled))
3011 ftrace_exports(event, TRACE_EXPORT_FUNCTION);
3012 __buffer_unlock_commit(buffer, event);
3015 #ifdef CONFIG_STACKTRACE
3017 /* Allow 4 levels of nesting: normal, softirq, irq, NMI */
3018 #define FTRACE_KSTACK_NESTING 4
3020 #define FTRACE_KSTACK_ENTRIES (SZ_4K / FTRACE_KSTACK_NESTING)
3022 struct ftrace_stack {
3023 unsigned long calls[FTRACE_KSTACK_ENTRIES];
3027 struct ftrace_stacks {
3028 struct ftrace_stack stacks[FTRACE_KSTACK_NESTING];
3031 static DEFINE_PER_CPU(struct ftrace_stacks, ftrace_stacks);
3032 static DEFINE_PER_CPU(int, ftrace_stack_reserve);
3034 static void __ftrace_trace_stack(struct trace_array *tr,
3035 struct trace_buffer *buffer,
3036 unsigned int trace_ctx,
3037 int skip, struct pt_regs *regs)
3039 struct ring_buffer_event *event;
3040 unsigned int size, nr_entries;
3041 struct ftrace_stack *fstack;
3042 struct stack_entry *entry;
3046 * Add one, for this function and the call to save_stack_trace()
3047 * If regs is set, then these functions will not be in the way.
3049 #ifndef CONFIG_UNWINDER_ORC
3054 preempt_disable_notrace();
3056 stackidx = __this_cpu_inc_return(ftrace_stack_reserve) - 1;
3058 /* This should never happen. If it does, yell once and skip */
3059 if (WARN_ON_ONCE(stackidx >= FTRACE_KSTACK_NESTING))
3063 * The above __this_cpu_inc_return() is 'atomic' cpu local. An
3064 * interrupt will either see the value pre increment or post
3065 * increment. If the interrupt happens pre increment it will have
3066 * restored the counter when it returns. We just need a barrier to
3067 * keep gcc from moving things around.
3071 fstack = this_cpu_ptr(ftrace_stacks.stacks) + stackidx;
3072 size = ARRAY_SIZE(fstack->calls);
3075 nr_entries = stack_trace_save_regs(regs, fstack->calls,
3078 nr_entries = stack_trace_save(fstack->calls, size, skip);
3081 #ifdef CONFIG_DYNAMIC_FTRACE
3082 /* Mark entry of stack trace as trampoline code */
3083 if (tr->ops && tr->ops->trampoline) {
3084 unsigned long tramp_start = tr->ops->trampoline;
3085 unsigned long tramp_end = tramp_start + tr->ops->trampoline_size;
3086 unsigned long *calls = fstack->calls;
3088 for (int i = 0; i < nr_entries; i++) {
3089 if (calls[i] >= tramp_start && calls[i] < tramp_end)
3090 calls[i] = FTRACE_TRAMPOLINE_MARKER;
3095 event = __trace_buffer_lock_reserve(buffer, TRACE_STACK,
3096 struct_size(entry, caller, nr_entries),
3100 entry = ring_buffer_event_data(event);
3102 entry->size = nr_entries;
3103 memcpy(&entry->caller, fstack->calls,
3104 flex_array_size(entry, caller, nr_entries));
3106 __buffer_unlock_commit(buffer, event);
3109 /* Again, don't let gcc optimize things here */
3111 __this_cpu_dec(ftrace_stack_reserve);
3112 preempt_enable_notrace();
3116 static inline void ftrace_trace_stack(struct trace_array *tr,
3117 struct trace_buffer *buffer,
3118 unsigned int trace_ctx,
3119 int skip, struct pt_regs *regs)
3121 if (!(tr->trace_flags & TRACE_ITER_STACKTRACE))
3124 __ftrace_trace_stack(tr, buffer, trace_ctx, skip, regs);
3127 void __trace_stack(struct trace_array *tr, unsigned int trace_ctx,
3130 struct trace_buffer *buffer = tr->array_buffer.buffer;
3132 if (rcu_is_watching()) {
3133 __ftrace_trace_stack(tr, buffer, trace_ctx, skip, NULL);
3137 if (WARN_ON_ONCE(IS_ENABLED(CONFIG_GENERIC_ENTRY)))
3141 * When an NMI triggers, RCU is enabled via ct_nmi_enter(),
3142 * but if the above rcu_is_watching() failed, then the NMI
3143 * triggered someplace critical, and ct_irq_enter() should
3144 * not be called from NMI.
3146 if (unlikely(in_nmi()))
3149 ct_irq_enter_irqson();
3150 __ftrace_trace_stack(tr, buffer, trace_ctx, skip, NULL);
3151 ct_irq_exit_irqson();
3155 * trace_dump_stack - record a stack back trace in the trace buffer
3156 * @skip: Number of functions to skip (helper handlers)
3158 void trace_dump_stack(int skip)
3160 if (tracing_disabled || tracing_selftest_running)
3163 #ifndef CONFIG_UNWINDER_ORC
3164 /* Skip 1 to skip this function. */
3167 __ftrace_trace_stack(printk_trace, printk_trace->array_buffer.buffer,
3168 tracing_gen_ctx(), skip, NULL);
3170 EXPORT_SYMBOL_GPL(trace_dump_stack);
3172 #ifdef CONFIG_USER_STACKTRACE_SUPPORT
3173 static DEFINE_PER_CPU(int, user_stack_count);
3176 ftrace_trace_userstack(struct trace_array *tr,
3177 struct trace_buffer *buffer, unsigned int trace_ctx)
3179 struct ring_buffer_event *event;
3180 struct userstack_entry *entry;
3182 if (!(tr->trace_flags & TRACE_ITER_USERSTACKTRACE))
3186 * NMIs can not handle page faults, even with fix ups.
3187 * The save user stack can (and often does) fault.
3189 if (unlikely(in_nmi()))
3193 * prevent recursion, since the user stack tracing may
3194 * trigger other kernel events.
3197 if (__this_cpu_read(user_stack_count))
3200 __this_cpu_inc(user_stack_count);
3202 event = __trace_buffer_lock_reserve(buffer, TRACE_USER_STACK,
3203 sizeof(*entry), trace_ctx);
3205 goto out_drop_count;
3206 entry = ring_buffer_event_data(event);
3208 entry->tgid = current->tgid;
3209 memset(&entry->caller, 0, sizeof(entry->caller));
3211 stack_trace_save_user(entry->caller, FTRACE_STACK_ENTRIES);
3212 __buffer_unlock_commit(buffer, event);
3215 __this_cpu_dec(user_stack_count);
3219 #else /* CONFIG_USER_STACKTRACE_SUPPORT */
3220 static void ftrace_trace_userstack(struct trace_array *tr,
3221 struct trace_buffer *buffer,
3222 unsigned int trace_ctx)
3225 #endif /* !CONFIG_USER_STACKTRACE_SUPPORT */
3227 #endif /* CONFIG_STACKTRACE */
3230 func_repeats_set_delta_ts(struct func_repeats_entry *entry,
3231 unsigned long long delta)
3233 entry->bottom_delta_ts = delta & U32_MAX;
3234 entry->top_delta_ts = (delta >> 32);
3237 void trace_last_func_repeats(struct trace_array *tr,
3238 struct trace_func_repeats *last_info,
3239 unsigned int trace_ctx)
3241 struct trace_buffer *buffer = tr->array_buffer.buffer;
3242 struct func_repeats_entry *entry;
3243 struct ring_buffer_event *event;
3246 event = __trace_buffer_lock_reserve(buffer, TRACE_FUNC_REPEATS,
3247 sizeof(*entry), trace_ctx);
3251 delta = ring_buffer_event_time_stamp(buffer, event) -
3252 last_info->ts_last_call;
3254 entry = ring_buffer_event_data(event);
3255 entry->ip = last_info->ip;
3256 entry->parent_ip = last_info->parent_ip;
3257 entry->count = last_info->count;
3258 func_repeats_set_delta_ts(entry, delta);
3260 __buffer_unlock_commit(buffer, event);
3263 /* created for use with alloc_percpu */
3264 struct trace_buffer_struct {
3266 char buffer[4][TRACE_BUF_SIZE];
3269 static struct trace_buffer_struct __percpu *trace_percpu_buffer;
3272 * This allows for lockless recording. If we're nested too deeply, then
3273 * this returns NULL.
3275 static char *get_trace_buf(void)
3277 struct trace_buffer_struct *buffer = this_cpu_ptr(trace_percpu_buffer);
3279 if (!trace_percpu_buffer || buffer->nesting >= 4)
3284 /* Interrupts must see nesting incremented before we use the buffer */
3286 return &buffer->buffer[buffer->nesting - 1][0];
3289 static void put_trace_buf(void)
3291 /* Don't let the decrement of nesting leak before this */
3293 this_cpu_dec(trace_percpu_buffer->nesting);
3296 static int alloc_percpu_trace_buffer(void)
3298 struct trace_buffer_struct __percpu *buffers;
3300 if (trace_percpu_buffer)
3303 buffers = alloc_percpu(struct trace_buffer_struct);
3304 if (MEM_FAIL(!buffers, "Could not allocate percpu trace_printk buffer"))
3307 trace_percpu_buffer = buffers;
3311 static int buffers_allocated;
3313 void trace_printk_init_buffers(void)
3315 if (buffers_allocated)
3318 if (alloc_percpu_trace_buffer())
3321 /* trace_printk() is for debug use only. Don't use it in production. */
3324 pr_warn("**********************************************************\n");
3325 pr_warn("** NOTICE NOTICE NOTICE NOTICE NOTICE NOTICE NOTICE **\n");
3327 pr_warn("** trace_printk() being used. Allocating extra memory. **\n");
3329 pr_warn("** This means that this is a DEBUG kernel and it is **\n");
3330 pr_warn("** unsafe for production use. **\n");
3332 pr_warn("** If you see this message and you are not debugging **\n");
3333 pr_warn("** the kernel, report this immediately to your vendor! **\n");
3335 pr_warn("** NOTICE NOTICE NOTICE NOTICE NOTICE NOTICE NOTICE **\n");
3336 pr_warn("**********************************************************\n");
3338 /* Expand the buffers to set size */
3339 tracing_update_buffers(&global_trace);
3341 buffers_allocated = 1;
3344 * trace_printk_init_buffers() can be called by modules.
3345 * If that happens, then we need to start cmdline recording
3346 * directly here. If the global_trace.buffer is already
3347 * allocated here, then this was called by module code.
3349 if (global_trace.array_buffer.buffer)
3350 tracing_start_cmdline_record();
3352 EXPORT_SYMBOL_GPL(trace_printk_init_buffers);
3354 void trace_printk_start_comm(void)
3356 /* Start tracing comms if trace printk is set */
3357 if (!buffers_allocated)
3359 tracing_start_cmdline_record();
3362 static void trace_printk_start_stop_comm(int enabled)
3364 if (!buffers_allocated)
3368 tracing_start_cmdline_record();
3370 tracing_stop_cmdline_record();
3374 * trace_vbprintk - write binary msg to tracing buffer
3375 * @ip: The address of the caller
3376 * @fmt: The string format to write to the buffer
3377 * @args: Arguments for @fmt
3379 int trace_vbprintk(unsigned long ip, const char *fmt, va_list args)
3381 struct ring_buffer_event *event;
3382 struct trace_buffer *buffer;
3383 struct trace_array *tr = READ_ONCE(printk_trace);
3384 struct bprint_entry *entry;
3385 unsigned int trace_ctx;
3389 if (!printk_binsafe(tr))
3390 return trace_vprintk(ip, fmt, args);
3392 if (unlikely(tracing_selftest_running || tracing_disabled))
3395 /* Don't pollute graph traces with trace_vprintk internals */
3396 pause_graph_tracing();
3398 trace_ctx = tracing_gen_ctx();
3399 preempt_disable_notrace();
3401 tbuffer = get_trace_buf();
3407 len = vbin_printf((u32 *)tbuffer, TRACE_BUF_SIZE/sizeof(int), fmt, args);
3409 if (len > TRACE_BUF_SIZE/sizeof(int) || len < 0)
3412 size = sizeof(*entry) + sizeof(u32) * len;
3413 buffer = tr->array_buffer.buffer;
3414 ring_buffer_nest_start(buffer);
3415 event = __trace_buffer_lock_reserve(buffer, TRACE_BPRINT, size,
3419 entry = ring_buffer_event_data(event);
3423 memcpy(entry->buf, tbuffer, sizeof(u32) * len);
3424 __buffer_unlock_commit(buffer, event);
3425 ftrace_trace_stack(tr, buffer, trace_ctx, 6, NULL);
3428 ring_buffer_nest_end(buffer);
3433 preempt_enable_notrace();
3434 unpause_graph_tracing();
3438 EXPORT_SYMBOL_GPL(trace_vbprintk);
3440 static __printf(3, 0)
3441 int __trace_array_vprintk(struct trace_buffer *buffer,
3442 unsigned long ip, const char *fmt, va_list args)
3444 struct ring_buffer_event *event;
3446 struct print_entry *entry;
3447 unsigned int trace_ctx;
3450 if (tracing_disabled)
3453 /* Don't pollute graph traces with trace_vprintk internals */
3454 pause_graph_tracing();
3456 trace_ctx = tracing_gen_ctx();
3457 preempt_disable_notrace();
3460 tbuffer = get_trace_buf();
3466 len = vscnprintf(tbuffer, TRACE_BUF_SIZE, fmt, args);
3468 size = sizeof(*entry) + len + 1;
3469 ring_buffer_nest_start(buffer);
3470 event = __trace_buffer_lock_reserve(buffer, TRACE_PRINT, size,
3474 entry = ring_buffer_event_data(event);
3477 memcpy(&entry->buf, tbuffer, len + 1);
3478 __buffer_unlock_commit(buffer, event);
3479 ftrace_trace_stack(printk_trace, buffer, trace_ctx, 6, NULL);
3482 ring_buffer_nest_end(buffer);
3486 preempt_enable_notrace();
3487 unpause_graph_tracing();
3492 int trace_array_vprintk(struct trace_array *tr,
3493 unsigned long ip, const char *fmt, va_list args)
3495 if (tracing_selftest_running && tr == &global_trace)
3498 return __trace_array_vprintk(tr->array_buffer.buffer, ip, fmt, args);
3502 * trace_array_printk - Print a message to a specific instance
3503 * @tr: The instance trace_array descriptor
3504 * @ip: The instruction pointer that this is called from.
3505 * @fmt: The format to print (printf format)
3507 * If a subsystem sets up its own instance, they have the right to
3508 * printk strings into their tracing instance buffer using this
3509 * function. Note, this function will not write into the top level
3510 * buffer (use trace_printk() for that), as writing into the top level
3511 * buffer should only have events that can be individually disabled.
3512 * trace_printk() is only used for debugging a kernel, and should not
3513 * be ever incorporated in normal use.
3515 * trace_array_printk() can be used, as it will not add noise to the
3516 * top level tracing buffer.
3518 * Note, trace_array_init_printk() must be called on @tr before this
3521 int trace_array_printk(struct trace_array *tr,
3522 unsigned long ip, const char *fmt, ...)
3530 /* This is only allowed for created instances */
3531 if (tr == &global_trace)
3534 if (!(tr->trace_flags & TRACE_ITER_PRINTK))
3538 ret = trace_array_vprintk(tr, ip, fmt, ap);
3542 EXPORT_SYMBOL_GPL(trace_array_printk);
3545 * trace_array_init_printk - Initialize buffers for trace_array_printk()
3546 * @tr: The trace array to initialize the buffers for
3548 * As trace_array_printk() only writes into instances, they are OK to
3549 * have in the kernel (unlike trace_printk()). This needs to be called
3550 * before trace_array_printk() can be used on a trace_array.
3552 int trace_array_init_printk(struct trace_array *tr)
3557 /* This is only allowed for created instances */
3558 if (tr == &global_trace)
3561 return alloc_percpu_trace_buffer();
3563 EXPORT_SYMBOL_GPL(trace_array_init_printk);
3565 int trace_array_printk_buf(struct trace_buffer *buffer,
3566 unsigned long ip, const char *fmt, ...)
3571 if (!(printk_trace->trace_flags & TRACE_ITER_PRINTK))
3575 ret = __trace_array_vprintk(buffer, ip, fmt, ap);
3580 int trace_vprintk(unsigned long ip, const char *fmt, va_list args)
3582 return trace_array_vprintk(printk_trace, ip, fmt, args);
3584 EXPORT_SYMBOL_GPL(trace_vprintk);
3586 static void trace_iterator_increment(struct trace_iterator *iter)
3588 struct ring_buffer_iter *buf_iter = trace_buffer_iter(iter, iter->cpu);
3592 ring_buffer_iter_advance(buf_iter);
3595 static struct trace_entry *
3596 peek_next_entry(struct trace_iterator *iter, int cpu, u64 *ts,
3597 unsigned long *lost_events)
3599 struct ring_buffer_event *event;
3600 struct ring_buffer_iter *buf_iter = trace_buffer_iter(iter, cpu);
3603 event = ring_buffer_iter_peek(buf_iter, ts);
3605 *lost_events = ring_buffer_iter_dropped(buf_iter) ?
3606 (unsigned long)-1 : 0;
3608 event = ring_buffer_peek(iter->array_buffer->buffer, cpu, ts,
3613 iter->ent_size = ring_buffer_event_length(event);
3614 return ring_buffer_event_data(event);
3620 static struct trace_entry *
3621 __find_next_entry(struct trace_iterator *iter, int *ent_cpu,
3622 unsigned long *missing_events, u64 *ent_ts)
3624 struct trace_buffer *buffer = iter->array_buffer->buffer;
3625 struct trace_entry *ent, *next = NULL;
3626 unsigned long lost_events = 0, next_lost = 0;
3627 int cpu_file = iter->cpu_file;
3628 u64 next_ts = 0, ts;
3634 * If we are in a per_cpu trace file, don't bother by iterating over
3635 * all cpu and peek directly.
3637 if (cpu_file > RING_BUFFER_ALL_CPUS) {
3638 if (ring_buffer_empty_cpu(buffer, cpu_file))
3640 ent = peek_next_entry(iter, cpu_file, ent_ts, missing_events);
3642 *ent_cpu = cpu_file;
3647 for_each_tracing_cpu(cpu) {
3649 if (ring_buffer_empty_cpu(buffer, cpu))
3652 ent = peek_next_entry(iter, cpu, &ts, &lost_events);
3655 * Pick the entry with the smallest timestamp:
3657 if (ent && (!next || ts < next_ts)) {
3661 next_lost = lost_events;
3662 next_size = iter->ent_size;
3666 iter->ent_size = next_size;
3669 *ent_cpu = next_cpu;
3675 *missing_events = next_lost;
3680 #define STATIC_FMT_BUF_SIZE 128
3681 static char static_fmt_buf[STATIC_FMT_BUF_SIZE];
3683 char *trace_iter_expand_format(struct trace_iterator *iter)
3688 * iter->tr is NULL when used with tp_printk, which makes
3689 * this get called where it is not safe to call krealloc().
3691 if (!iter->tr || iter->fmt == static_fmt_buf)
3694 tmp = krealloc(iter->fmt, iter->fmt_size + STATIC_FMT_BUF_SIZE,
3697 iter->fmt_size += STATIC_FMT_BUF_SIZE;
3704 /* Returns true if the string is safe to dereference from an event */
3705 static bool trace_safe_str(struct trace_iterator *iter, const char *str)
3707 unsigned long addr = (unsigned long)str;
3708 struct trace_event *trace_event;
3709 struct trace_event_call *event;
3711 /* OK if part of the event data */
3712 if ((addr >= (unsigned long)iter->ent) &&
3713 (addr < (unsigned long)iter->ent + iter->ent_size))
3716 /* OK if part of the temp seq buffer */
3717 if ((addr >= (unsigned long)iter->tmp_seq.buffer) &&
3718 (addr < (unsigned long)iter->tmp_seq.buffer + TRACE_SEQ_BUFFER_SIZE))
3721 /* Core rodata can not be freed */
3722 if (is_kernel_rodata(addr))
3725 if (trace_is_tracepoint_string(str))
3729 * Now this could be a module event, referencing core module
3730 * data, which is OK.
3735 trace_event = ftrace_find_event(iter->ent->type);
3739 event = container_of(trace_event, struct trace_event_call, event);
3740 if ((event->flags & TRACE_EVENT_FL_DYNAMIC) || !event->module)
3743 /* Would rather have rodata, but this will suffice */
3744 if (within_module_core(addr, event->module))
3751 * ignore_event - Check dereferenced fields while writing to the seq buffer
3752 * @iter: The iterator that holds the seq buffer and the event being printed
3754 * At boot up, test_event_printk() will flag any event that dereferences
3755 * a string with "%s" that does exist in the ring buffer. It may still
3756 * be valid, as the string may point to a static string in the kernel
3757 * rodata that never gets freed. But if the string pointer is pointing
3758 * to something that was allocated, there's a chance that it can be freed
3759 * by the time the user reads the trace. This would cause a bad memory
3760 * access by the kernel and possibly crash the system.
3762 * This function will check if the event has any fields flagged as needing
3763 * to be checked at runtime and perform those checks.
3765 * If it is found that a field is unsafe, it will write into the @iter->seq
3766 * a message stating what was found to be unsafe.
3768 * @return: true if the event is unsafe and should be ignored,
3771 bool ignore_event(struct trace_iterator *iter)
3773 struct ftrace_event_field *field;
3774 struct trace_event *trace_event;
3775 struct trace_event_call *event;
3776 struct list_head *head;
3777 struct trace_seq *seq;
3780 trace_event = ftrace_find_event(iter->ent->type);
3785 trace_seq_printf(seq, "EVENT ID %d NOT FOUND?\n", iter->ent->type);
3789 event = container_of(trace_event, struct trace_event_call, event);
3790 if (!(event->flags & TRACE_EVENT_FL_TEST_STR))
3793 head = trace_get_fields(event);
3795 trace_seq_printf(seq, "FIELDS FOR EVENT '%s' NOT FOUND?\n",
3796 trace_event_name(event));
3800 /* Offsets are from the iter->ent that points to the raw event */
3803 list_for_each_entry(field, head, link) {
3807 if (!field->needs_test)
3810 str = *(const char **)(ptr + field->offset);
3812 good = trace_safe_str(iter, str);
3815 * If you hit this warning, it is likely that the
3816 * trace event in question used %s on a string that
3817 * was saved at the time of the event, but may not be
3818 * around when the trace is read. Use __string(),
3819 * __assign_str() and __get_str() helpers in the TRACE_EVENT()
3820 * instead. See samples/trace_events/trace-events-sample.h
3823 if (WARN_ONCE(!good, "event '%s' has unsafe pointer field '%s'",
3824 trace_event_name(event), field->name)) {
3825 trace_seq_printf(seq, "EVENT %s: HAS UNSAFE POINTER FIELD '%s'\n",
3826 trace_event_name(event), field->name);
3833 const char *trace_event_format(struct trace_iterator *iter, const char *fmt)
3835 const char *p, *new_fmt;
3838 if (WARN_ON_ONCE(!fmt))
3841 if (!iter->tr || iter->tr->trace_flags & TRACE_ITER_HASH_PTR)
3845 new_fmt = q = iter->fmt;
3847 if (unlikely(q - new_fmt + 3 > iter->fmt_size)) {
3848 if (!trace_iter_expand_format(iter))
3851 q += iter->fmt - new_fmt;
3852 new_fmt = iter->fmt;
3857 /* Replace %p with %px */
3861 } else if (p[0] == 'p' && !isalnum(p[1])) {
3872 #define STATIC_TEMP_BUF_SIZE 128
3873 static char static_temp_buf[STATIC_TEMP_BUF_SIZE] __aligned(4);
3875 /* Find the next real entry, without updating the iterator itself */
3876 struct trace_entry *trace_find_next_entry(struct trace_iterator *iter,
3877 int *ent_cpu, u64 *ent_ts)
3879 /* __find_next_entry will reset ent_size */
3880 int ent_size = iter->ent_size;
3881 struct trace_entry *entry;
3884 * If called from ftrace_dump(), then the iter->temp buffer
3885 * will be the static_temp_buf and not created from kmalloc.
3886 * If the entry size is greater than the buffer, we can
3887 * not save it. Just return NULL in that case. This is only
3888 * used to add markers when two consecutive events' time
3889 * stamps have a large delta. See trace_print_lat_context()
3891 if (iter->temp == static_temp_buf &&
3892 STATIC_TEMP_BUF_SIZE < ent_size)
3896 * The __find_next_entry() may call peek_next_entry(), which may
3897 * call ring_buffer_peek() that may make the contents of iter->ent
3898 * undefined. Need to copy iter->ent now.
3900 if (iter->ent && iter->ent != iter->temp) {
3901 if ((!iter->temp || iter->temp_size < iter->ent_size) &&
3902 !WARN_ON_ONCE(iter->temp == static_temp_buf)) {
3904 temp = kmalloc(iter->ent_size, GFP_KERNEL);
3909 iter->temp_size = iter->ent_size;
3911 memcpy(iter->temp, iter->ent, iter->ent_size);
3912 iter->ent = iter->temp;
3914 entry = __find_next_entry(iter, ent_cpu, NULL, ent_ts);
3915 /* Put back the original ent_size */
3916 iter->ent_size = ent_size;
3921 /* Find the next real entry, and increment the iterator to the next entry */
3922 void *trace_find_next_entry_inc(struct trace_iterator *iter)
3924 iter->ent = __find_next_entry(iter, &iter->cpu,
3925 &iter->lost_events, &iter->ts);
3928 trace_iterator_increment(iter);
3930 return iter->ent ? iter : NULL;
3933 static void trace_consume(struct trace_iterator *iter)
3935 ring_buffer_consume(iter->array_buffer->buffer, iter->cpu, &iter->ts,
3936 &iter->lost_events);
3939 static void *s_next(struct seq_file *m, void *v, loff_t *pos)
3941 struct trace_iterator *iter = m->private;
3945 WARN_ON_ONCE(iter->leftover);
3949 /* can't go backwards */
3954 ent = trace_find_next_entry_inc(iter);
3958 while (ent && iter->idx < i)
3959 ent = trace_find_next_entry_inc(iter);
3966 void tracing_iter_reset(struct trace_iterator *iter, int cpu)
3968 struct ring_buffer_iter *buf_iter;
3969 unsigned long entries = 0;
3972 per_cpu_ptr(iter->array_buffer->data, cpu)->skipped_entries = 0;
3974 buf_iter = trace_buffer_iter(iter, cpu);
3978 ring_buffer_iter_reset(buf_iter);
3981 * We could have the case with the max latency tracers
3982 * that a reset never took place on a cpu. This is evident
3983 * by the timestamp being before the start of the buffer.
3985 while (ring_buffer_iter_peek(buf_iter, &ts)) {
3986 if (ts >= iter->array_buffer->time_start)
3989 ring_buffer_iter_advance(buf_iter);
3990 /* This could be a big loop */
3994 per_cpu_ptr(iter->array_buffer->data, cpu)->skipped_entries = entries;
3998 * The current tracer is copied to avoid a global locking
4001 static void *s_start(struct seq_file *m, loff_t *pos)
4003 struct trace_iterator *iter = m->private;
4004 struct trace_array *tr = iter->tr;
4005 int cpu_file = iter->cpu_file;
4010 mutex_lock(&trace_types_lock);
4011 if (unlikely(tr->current_trace != iter->trace)) {
4012 /* Close iter->trace before switching to the new current tracer */
4013 if (iter->trace->close)
4014 iter->trace->close(iter);
4015 iter->trace = tr->current_trace;
4016 /* Reopen the new current tracer */
4017 if (iter->trace->open)
4018 iter->trace->open(iter);
4020 mutex_unlock(&trace_types_lock);
4022 #ifdef CONFIG_TRACER_MAX_TRACE
4023 if (iter->snapshot && iter->trace->use_max_tr)
4024 return ERR_PTR(-EBUSY);
4027 if (*pos != iter->pos) {
4032 if (cpu_file == RING_BUFFER_ALL_CPUS) {
4033 for_each_tracing_cpu(cpu)
4034 tracing_iter_reset(iter, cpu);
4036 tracing_iter_reset(iter, cpu_file);
4039 for (p = iter; p && l < *pos; p = s_next(m, p, &l))
4044 * If we overflowed the seq_file before, then we want
4045 * to just reuse the trace_seq buffer again.
4051 p = s_next(m, p, &l);
4055 trace_event_read_lock();
4056 trace_access_lock(cpu_file);
4060 static void s_stop(struct seq_file *m, void *p)
4062 struct trace_iterator *iter = m->private;
4064 #ifdef CONFIG_TRACER_MAX_TRACE
4065 if (iter->snapshot && iter->trace->use_max_tr)
4069 trace_access_unlock(iter->cpu_file);
4070 trace_event_read_unlock();
4074 get_total_entries_cpu(struct array_buffer *buf, unsigned long *total,
4075 unsigned long *entries, int cpu)
4077 unsigned long count;
4079 count = ring_buffer_entries_cpu(buf->buffer, cpu);
4081 * If this buffer has skipped entries, then we hold all
4082 * entries for the trace and we need to ignore the
4083 * ones before the time stamp.
4085 if (per_cpu_ptr(buf->data, cpu)->skipped_entries) {
4086 count -= per_cpu_ptr(buf->data, cpu)->skipped_entries;
4087 /* total is the same as the entries */
4091 ring_buffer_overrun_cpu(buf->buffer, cpu);
4096 get_total_entries(struct array_buffer *buf,
4097 unsigned long *total, unsigned long *entries)
4105 for_each_tracing_cpu(cpu) {
4106 get_total_entries_cpu(buf, &t, &e, cpu);
4112 unsigned long trace_total_entries_cpu(struct trace_array *tr, int cpu)
4114 unsigned long total, entries;
4119 get_total_entries_cpu(&tr->array_buffer, &total, &entries, cpu);
4124 unsigned long trace_total_entries(struct trace_array *tr)
4126 unsigned long total, entries;
4131 get_total_entries(&tr->array_buffer, &total, &entries);
4136 static void print_lat_help_header(struct seq_file *m)
4138 seq_puts(m, "# _------=> CPU# \n"
4139 "# / _-----=> irqs-off/BH-disabled\n"
4140 "# | / _----=> need-resched \n"
4141 "# || / _---=> hardirq/softirq \n"
4142 "# ||| / _--=> preempt-depth \n"
4143 "# |||| / _-=> migrate-disable \n"
4144 "# ||||| / delay \n"
4145 "# cmd pid |||||| time | caller \n"
4146 "# \\ / |||||| \\ | / \n");
4149 static void print_event_info(struct array_buffer *buf, struct seq_file *m)
4151 unsigned long total;
4152 unsigned long entries;
4154 get_total_entries(buf, &total, &entries);
4155 seq_printf(m, "# entries-in-buffer/entries-written: %lu/%lu #P:%d\n",
4156 entries, total, num_online_cpus());
4160 static void print_func_help_header(struct array_buffer *buf, struct seq_file *m,
4163 bool tgid = flags & TRACE_ITER_RECORD_TGID;
4165 print_event_info(buf, m);
4167 seq_printf(m, "# TASK-PID %s CPU# TIMESTAMP FUNCTION\n", tgid ? " TGID " : "");
4168 seq_printf(m, "# | | %s | | |\n", tgid ? " | " : "");
4171 static void print_func_help_header_irq(struct array_buffer *buf, struct seq_file *m,
4174 bool tgid = flags & TRACE_ITER_RECORD_TGID;
4175 static const char space[] = " ";
4176 int prec = tgid ? 12 : 2;
4178 print_event_info(buf, m);
4180 seq_printf(m, "# %.*s _-----=> irqs-off/BH-disabled\n", prec, space);
4181 seq_printf(m, "# %.*s / _----=> need-resched\n", prec, space);
4182 seq_printf(m, "# %.*s| / _---=> hardirq/softirq\n", prec, space);
4183 seq_printf(m, "# %.*s|| / _--=> preempt-depth\n", prec, space);
4184 seq_printf(m, "# %.*s||| / _-=> migrate-disable\n", prec, space);
4185 seq_printf(m, "# %.*s|||| / delay\n", prec, space);
4186 seq_printf(m, "# TASK-PID %.*s CPU# ||||| TIMESTAMP FUNCTION\n", prec, " TGID ");
4187 seq_printf(m, "# | | %.*s | ||||| | |\n", prec, " | ");
4191 print_trace_header(struct seq_file *m, struct trace_iterator *iter)
4193 unsigned long sym_flags = (global_trace.trace_flags & TRACE_ITER_SYM_MASK);
4194 struct array_buffer *buf = iter->array_buffer;
4195 struct trace_array_cpu *data = per_cpu_ptr(buf->data, buf->cpu);
4196 struct tracer *type = iter->trace;
4197 unsigned long entries;
4198 unsigned long total;
4199 const char *name = type->name;
4201 get_total_entries(buf, &total, &entries);
4203 seq_printf(m, "# %s latency trace v1.1.5 on %s\n",
4204 name, init_utsname()->release);
4205 seq_puts(m, "# -----------------------------------"
4206 "---------------------------------\n");
4207 seq_printf(m, "# latency: %lu us, #%lu/%lu, CPU#%d |"
4208 " (M:%s VP:%d, KP:%d, SP:%d HP:%d",
4209 nsecs_to_usecs(data->saved_latency),
4213 preempt_model_str(),
4214 /* These are reserved for later use */
4217 seq_printf(m, " #P:%d)\n", num_online_cpus());
4221 seq_puts(m, "# -----------------\n");
4222 seq_printf(m, "# | task: %.16s-%d "
4223 "(uid:%d nice:%ld policy:%ld rt_prio:%ld)\n",
4224 data->comm, data->pid,
4225 from_kuid_munged(seq_user_ns(m), data->uid), data->nice,
4226 data->policy, data->rt_priority);
4227 seq_puts(m, "# -----------------\n");
4229 if (data->critical_start) {
4230 seq_puts(m, "# => started at: ");
4231 seq_print_ip_sym(&iter->seq, data->critical_start, sym_flags);
4232 trace_print_seq(m, &iter->seq);
4233 seq_puts(m, "\n# => ended at: ");
4234 seq_print_ip_sym(&iter->seq, data->critical_end, sym_flags);
4235 trace_print_seq(m, &iter->seq);
4236 seq_puts(m, "\n#\n");
4242 static void test_cpu_buff_start(struct trace_iterator *iter)
4244 struct trace_seq *s = &iter->seq;
4245 struct trace_array *tr = iter->tr;
4247 if (!(tr->trace_flags & TRACE_ITER_ANNOTATE))
4250 if (!(iter->iter_flags & TRACE_FILE_ANNOTATE))
4253 if (cpumask_available(iter->started) &&
4254 cpumask_test_cpu(iter->cpu, iter->started))
4257 if (per_cpu_ptr(iter->array_buffer->data, iter->cpu)->skipped_entries)
4260 if (cpumask_available(iter->started))
4261 cpumask_set_cpu(iter->cpu, iter->started);
4263 /* Don't print started cpu buffer for the first entry of the trace */
4265 trace_seq_printf(s, "##### CPU %u buffer started ####\n",
4269 static enum print_line_t print_trace_fmt(struct trace_iterator *iter)
4271 struct trace_array *tr = iter->tr;
4272 struct trace_seq *s = &iter->seq;
4273 unsigned long sym_flags = (tr->trace_flags & TRACE_ITER_SYM_MASK);
4274 struct trace_entry *entry;
4275 struct trace_event *event;
4279 test_cpu_buff_start(iter);
4281 event = ftrace_find_event(entry->type);
4283 if (tr->trace_flags & TRACE_ITER_CONTEXT_INFO) {
4284 if (iter->iter_flags & TRACE_FILE_LAT_FMT)
4285 trace_print_lat_context(iter);
4287 trace_print_context(iter);
4290 if (trace_seq_has_overflowed(s))
4291 return TRACE_TYPE_PARTIAL_LINE;
4294 if (tr->trace_flags & TRACE_ITER_FIELDS)
4295 return print_event_fields(iter, event);
4297 * For TRACE_EVENT() events, the print_fmt is not
4298 * safe to use if the array has delta offsets
4299 * Force printing via the fields.
4301 if ((tr->text_delta) &&
4302 event->type > __TRACE_LAST_TYPE)
4303 return print_event_fields(iter, event);
4305 return event->funcs->trace(iter, sym_flags, event);
4308 trace_seq_printf(s, "Unknown type %d\n", entry->type);
4310 return trace_handle_return(s);
4313 static enum print_line_t print_raw_fmt(struct trace_iterator *iter)
4315 struct trace_array *tr = iter->tr;
4316 struct trace_seq *s = &iter->seq;
4317 struct trace_entry *entry;
4318 struct trace_event *event;
4322 if (tr->trace_flags & TRACE_ITER_CONTEXT_INFO)
4323 trace_seq_printf(s, "%d %d %llu ",
4324 entry->pid, iter->cpu, iter->ts);
4326 if (trace_seq_has_overflowed(s))
4327 return TRACE_TYPE_PARTIAL_LINE;
4329 event = ftrace_find_event(entry->type);
4331 return event->funcs->raw(iter, 0, event);
4333 trace_seq_printf(s, "%d ?\n", entry->type);
4335 return trace_handle_return(s);
4338 static enum print_line_t print_hex_fmt(struct trace_iterator *iter)
4340 struct trace_array *tr = iter->tr;
4341 struct trace_seq *s = &iter->seq;
4342 unsigned char newline = '\n';
4343 struct trace_entry *entry;
4344 struct trace_event *event;
4348 if (tr->trace_flags & TRACE_ITER_CONTEXT_INFO) {
4349 SEQ_PUT_HEX_FIELD(s, entry->pid);
4350 SEQ_PUT_HEX_FIELD(s, iter->cpu);
4351 SEQ_PUT_HEX_FIELD(s, iter->ts);
4352 if (trace_seq_has_overflowed(s))
4353 return TRACE_TYPE_PARTIAL_LINE;
4356 event = ftrace_find_event(entry->type);
4358 enum print_line_t ret = event->funcs->hex(iter, 0, event);
4359 if (ret != TRACE_TYPE_HANDLED)
4363 SEQ_PUT_FIELD(s, newline);
4365 return trace_handle_return(s);
4368 static enum print_line_t print_bin_fmt(struct trace_iterator *iter)
4370 struct trace_array *tr = iter->tr;
4371 struct trace_seq *s = &iter->seq;
4372 struct trace_entry *entry;
4373 struct trace_event *event;
4377 if (tr->trace_flags & TRACE_ITER_CONTEXT_INFO) {
4378 SEQ_PUT_FIELD(s, entry->pid);
4379 SEQ_PUT_FIELD(s, iter->cpu);
4380 SEQ_PUT_FIELD(s, iter->ts);
4381 if (trace_seq_has_overflowed(s))
4382 return TRACE_TYPE_PARTIAL_LINE;
4385 event = ftrace_find_event(entry->type);
4386 return event ? event->funcs->binary(iter, 0, event) :
4390 int trace_empty(struct trace_iterator *iter)
4392 struct ring_buffer_iter *buf_iter;
4395 /* If we are looking at one CPU buffer, only check that one */
4396 if (iter->cpu_file != RING_BUFFER_ALL_CPUS) {
4397 cpu = iter->cpu_file;
4398 buf_iter = trace_buffer_iter(iter, cpu);
4400 if (!ring_buffer_iter_empty(buf_iter))
4403 if (!ring_buffer_empty_cpu(iter->array_buffer->buffer, cpu))
4409 for_each_tracing_cpu(cpu) {
4410 buf_iter = trace_buffer_iter(iter, cpu);
4412 if (!ring_buffer_iter_empty(buf_iter))
4415 if (!ring_buffer_empty_cpu(iter->array_buffer->buffer, cpu))
4423 /* Called with trace_event_read_lock() held. */
4424 enum print_line_t print_trace_line(struct trace_iterator *iter)
4426 struct trace_array *tr = iter->tr;
4427 unsigned long trace_flags = tr->trace_flags;
4428 enum print_line_t ret;
4430 if (iter->lost_events) {
4431 if (iter->lost_events == (unsigned long)-1)
4432 trace_seq_printf(&iter->seq, "CPU:%d [LOST EVENTS]\n",
4435 trace_seq_printf(&iter->seq, "CPU:%d [LOST %lu EVENTS]\n",
4436 iter->cpu, iter->lost_events);
4437 if (trace_seq_has_overflowed(&iter->seq))
4438 return TRACE_TYPE_PARTIAL_LINE;
4441 if (iter->trace && iter->trace->print_line) {
4442 ret = iter->trace->print_line(iter);
4443 if (ret != TRACE_TYPE_UNHANDLED)
4447 if (iter->ent->type == TRACE_BPUTS &&
4448 trace_flags & TRACE_ITER_PRINTK &&
4449 trace_flags & TRACE_ITER_PRINTK_MSGONLY)
4450 return trace_print_bputs_msg_only(iter);
4452 if (iter->ent->type == TRACE_BPRINT &&
4453 trace_flags & TRACE_ITER_PRINTK &&
4454 trace_flags & TRACE_ITER_PRINTK_MSGONLY)
4455 return trace_print_bprintk_msg_only(iter);
4457 if (iter->ent->type == TRACE_PRINT &&
4458 trace_flags & TRACE_ITER_PRINTK &&
4459 trace_flags & TRACE_ITER_PRINTK_MSGONLY)
4460 return trace_print_printk_msg_only(iter);
4462 if (trace_flags & TRACE_ITER_BIN)
4463 return print_bin_fmt(iter);
4465 if (trace_flags & TRACE_ITER_HEX)
4466 return print_hex_fmt(iter);
4468 if (trace_flags & TRACE_ITER_RAW)
4469 return print_raw_fmt(iter);
4471 return print_trace_fmt(iter);
4474 void trace_latency_header(struct seq_file *m)
4476 struct trace_iterator *iter = m->private;
4477 struct trace_array *tr = iter->tr;
4479 /* print nothing if the buffers are empty */
4480 if (trace_empty(iter))
4483 if (iter->iter_flags & TRACE_FILE_LAT_FMT)
4484 print_trace_header(m, iter);
4486 if (!(tr->trace_flags & TRACE_ITER_VERBOSE))
4487 print_lat_help_header(m);
4490 void trace_default_header(struct seq_file *m)
4492 struct trace_iterator *iter = m->private;
4493 struct trace_array *tr = iter->tr;
4494 unsigned long trace_flags = tr->trace_flags;
4496 if (!(trace_flags & TRACE_ITER_CONTEXT_INFO))
4499 if (iter->iter_flags & TRACE_FILE_LAT_FMT) {
4500 /* print nothing if the buffers are empty */
4501 if (trace_empty(iter))
4503 print_trace_header(m, iter);
4504 if (!(trace_flags & TRACE_ITER_VERBOSE))
4505 print_lat_help_header(m);
4507 if (!(trace_flags & TRACE_ITER_VERBOSE)) {
4508 if (trace_flags & TRACE_ITER_IRQ_INFO)
4509 print_func_help_header_irq(iter->array_buffer,
4512 print_func_help_header(iter->array_buffer, m,
4518 static void test_ftrace_alive(struct seq_file *m)
4520 if (!ftrace_is_dead())
4522 seq_puts(m, "# WARNING: FUNCTION TRACING IS CORRUPTED\n"
4523 "# MAY BE MISSING FUNCTION EVENTS\n");
4526 #ifdef CONFIG_TRACER_MAX_TRACE
4527 static void show_snapshot_main_help(struct seq_file *m)
4529 seq_puts(m, "# echo 0 > snapshot : Clears and frees snapshot buffer\n"
4530 "# echo 1 > snapshot : Allocates snapshot buffer, if not already allocated.\n"
4531 "# Takes a snapshot of the main buffer.\n"
4532 "# echo 2 > snapshot : Clears snapshot buffer (but does not allocate or free)\n"
4533 "# (Doesn't have to be '2' works with any number that\n"
4534 "# is not a '0' or '1')\n");
4537 static void show_snapshot_percpu_help(struct seq_file *m)
4539 seq_puts(m, "# echo 0 > snapshot : Invalid for per_cpu snapshot file.\n");
4540 #ifdef CONFIG_RING_BUFFER_ALLOW_SWAP
4541 seq_puts(m, "# echo 1 > snapshot : Allocates snapshot buffer, if not already allocated.\n"
4542 "# Takes a snapshot of the main buffer for this cpu.\n");
4544 seq_puts(m, "# echo 1 > snapshot : Not supported with this kernel.\n"
4545 "# Must use main snapshot file to allocate.\n");
4547 seq_puts(m, "# echo 2 > snapshot : Clears this cpu's snapshot buffer (but does not allocate)\n"
4548 "# (Doesn't have to be '2' works with any number that\n"
4549 "# is not a '0' or '1')\n");
4552 static void print_snapshot_help(struct seq_file *m, struct trace_iterator *iter)
4554 if (iter->tr->allocated_snapshot)
4555 seq_puts(m, "#\n# * Snapshot is allocated *\n#\n");
4557 seq_puts(m, "#\n# * Snapshot is freed *\n#\n");
4559 seq_puts(m, "# Snapshot commands:\n");
4560 if (iter->cpu_file == RING_BUFFER_ALL_CPUS)
4561 show_snapshot_main_help(m);
4563 show_snapshot_percpu_help(m);
4566 /* Should never be called */
4567 static inline void print_snapshot_help(struct seq_file *m, struct trace_iterator *iter) { }
4570 static int s_show(struct seq_file *m, void *v)
4572 struct trace_iterator *iter = v;
4575 if (iter->ent == NULL) {
4577 seq_printf(m, "# tracer: %s\n", iter->trace->name);
4579 test_ftrace_alive(m);
4581 if (iter->snapshot && trace_empty(iter))
4582 print_snapshot_help(m, iter);
4583 else if (iter->trace && iter->trace->print_header)
4584 iter->trace->print_header(m);
4586 trace_default_header(m);
4588 } else if (iter->leftover) {
4590 * If we filled the seq_file buffer earlier, we
4591 * want to just show it now.
4593 ret = trace_print_seq(m, &iter->seq);
4595 /* ret should this time be zero, but you never know */
4596 iter->leftover = ret;
4599 ret = print_trace_line(iter);
4600 if (ret == TRACE_TYPE_PARTIAL_LINE) {
4602 trace_seq_puts(&iter->seq, "[LINE TOO BIG]\n");
4604 ret = trace_print_seq(m, &iter->seq);
4606 * If we overflow the seq_file buffer, then it will
4607 * ask us for this data again at start up.
4609 * ret is 0 if seq_file write succeeded.
4612 iter->leftover = ret;
4619 * Should be used after trace_array_get(), trace_types_lock
4620 * ensures that i_cdev was already initialized.
4622 static inline int tracing_get_cpu(struct inode *inode)
4624 if (inode->i_cdev) /* See trace_create_cpu_file() */
4625 return (long)inode->i_cdev - 1;
4626 return RING_BUFFER_ALL_CPUS;
4629 static const struct seq_operations tracer_seq_ops = {
4637 * Note, as iter itself can be allocated and freed in different
4638 * ways, this function is only used to free its content, and not
4639 * the iterator itself. The only requirement to all the allocations
4640 * is that it must zero all fields (kzalloc), as freeing works with
4641 * ethier allocated content or NULL.
4643 static void free_trace_iter_content(struct trace_iterator *iter)
4645 /* The fmt is either NULL, allocated or points to static_fmt_buf */
4646 if (iter->fmt != static_fmt_buf)
4650 kfree(iter->buffer_iter);
4651 mutex_destroy(&iter->mutex);
4652 free_cpumask_var(iter->started);
4655 static struct trace_iterator *
4656 __tracing_open(struct inode *inode, struct file *file, bool snapshot)
4658 struct trace_array *tr = inode->i_private;
4659 struct trace_iterator *iter;
4662 if (tracing_disabled)
4663 return ERR_PTR(-ENODEV);
4665 iter = __seq_open_private(file, &tracer_seq_ops, sizeof(*iter));
4667 return ERR_PTR(-ENOMEM);
4669 iter->buffer_iter = kcalloc(nr_cpu_ids, sizeof(*iter->buffer_iter),
4671 if (!iter->buffer_iter)
4675 * trace_find_next_entry() may need to save off iter->ent.
4676 * It will place it into the iter->temp buffer. As most
4677 * events are less than 128, allocate a buffer of that size.
4678 * If one is greater, then trace_find_next_entry() will
4679 * allocate a new buffer to adjust for the bigger iter->ent.
4680 * It's not critical if it fails to get allocated here.
4682 iter->temp = kmalloc(128, GFP_KERNEL);
4684 iter->temp_size = 128;
4687 * trace_event_printf() may need to modify given format
4688 * string to replace %p with %px so that it shows real address
4689 * instead of hash value. However, that is only for the event
4690 * tracing, other tracer may not need. Defer the allocation
4691 * until it is needed.
4696 mutex_lock(&trace_types_lock);
4697 iter->trace = tr->current_trace;
4699 if (!zalloc_cpumask_var(&iter->started, GFP_KERNEL))
4704 #ifdef CONFIG_TRACER_MAX_TRACE
4705 /* Currently only the top directory has a snapshot */
4706 if (tr->current_trace->print_max || snapshot)
4707 iter->array_buffer = &tr->max_buffer;
4710 iter->array_buffer = &tr->array_buffer;
4711 iter->snapshot = snapshot;
4713 iter->cpu_file = tracing_get_cpu(inode);
4714 mutex_init(&iter->mutex);
4716 /* Notify the tracer early; before we stop tracing. */
4717 if (iter->trace->open)
4718 iter->trace->open(iter);
4720 /* Annotate start of buffers if we had overruns */
4721 if (ring_buffer_overruns(iter->array_buffer->buffer))
4722 iter->iter_flags |= TRACE_FILE_ANNOTATE;
4724 /* Output in nanoseconds only if we are using a clock in nanoseconds. */
4725 if (trace_clocks[tr->clock_id].in_ns)
4726 iter->iter_flags |= TRACE_FILE_TIME_IN_NS;
4729 * If pause-on-trace is enabled, then stop the trace while
4730 * dumping, unless this is the "snapshot" file
4732 if (!iter->snapshot && (tr->trace_flags & TRACE_ITER_PAUSE_ON_TRACE))
4733 tracing_stop_tr(tr);
4735 if (iter->cpu_file == RING_BUFFER_ALL_CPUS) {
4736 for_each_tracing_cpu(cpu) {
4737 iter->buffer_iter[cpu] =
4738 ring_buffer_read_prepare(iter->array_buffer->buffer,
4741 ring_buffer_read_prepare_sync();
4742 for_each_tracing_cpu(cpu) {
4743 ring_buffer_read_start(iter->buffer_iter[cpu]);
4744 tracing_iter_reset(iter, cpu);
4747 cpu = iter->cpu_file;
4748 iter->buffer_iter[cpu] =
4749 ring_buffer_read_prepare(iter->array_buffer->buffer,
4751 ring_buffer_read_prepare_sync();
4752 ring_buffer_read_start(iter->buffer_iter[cpu]);
4753 tracing_iter_reset(iter, cpu);
4756 mutex_unlock(&trace_types_lock);
4761 mutex_unlock(&trace_types_lock);
4762 free_trace_iter_content(iter);
4764 seq_release_private(inode, file);
4765 return ERR_PTR(-ENOMEM);
4768 int tracing_open_generic(struct inode *inode, struct file *filp)
4772 ret = tracing_check_open_get_tr(NULL);
4776 filp->private_data = inode->i_private;
4780 bool tracing_is_disabled(void)
4782 return (tracing_disabled) ? true: false;
4786 * Open and update trace_array ref count.
4787 * Must have the current trace_array passed to it.
4789 int tracing_open_generic_tr(struct inode *inode, struct file *filp)
4791 struct trace_array *tr = inode->i_private;
4794 ret = tracing_check_open_get_tr(tr);
4798 filp->private_data = inode->i_private;
4804 * The private pointer of the inode is the trace_event_file.
4805 * Update the tr ref count associated to it.
4807 int tracing_open_file_tr(struct inode *inode, struct file *filp)
4809 struct trace_event_file *file = inode->i_private;
4812 ret = tracing_check_open_get_tr(file->tr);
4816 mutex_lock(&event_mutex);
4818 /* Fail if the file is marked for removal */
4819 if (file->flags & EVENT_FILE_FL_FREED) {
4820 trace_array_put(file->tr);
4823 event_file_get(file);
4826 mutex_unlock(&event_mutex);
4830 filp->private_data = inode->i_private;
4835 int tracing_release_file_tr(struct inode *inode, struct file *filp)
4837 struct trace_event_file *file = inode->i_private;
4839 trace_array_put(file->tr);
4840 event_file_put(file);
4845 int tracing_single_release_file_tr(struct inode *inode, struct file *filp)
4847 tracing_release_file_tr(inode, filp);
4848 return single_release(inode, filp);
4851 static int tracing_mark_open(struct inode *inode, struct file *filp)
4853 stream_open(inode, filp);
4854 return tracing_open_generic_tr(inode, filp);
4857 static int tracing_release(struct inode *inode, struct file *file)
4859 struct trace_array *tr = inode->i_private;
4860 struct seq_file *m = file->private_data;
4861 struct trace_iterator *iter;
4864 if (!(file->f_mode & FMODE_READ)) {
4865 trace_array_put(tr);
4869 /* Writes do not use seq_file */
4871 mutex_lock(&trace_types_lock);
4873 for_each_tracing_cpu(cpu) {
4874 if (iter->buffer_iter[cpu])
4875 ring_buffer_read_finish(iter->buffer_iter[cpu]);
4878 if (iter->trace && iter->trace->close)
4879 iter->trace->close(iter);
4881 if (!iter->snapshot && tr->stop_count)
4882 /* reenable tracing if it was previously enabled */
4883 tracing_start_tr(tr);
4885 __trace_array_put(tr);
4887 mutex_unlock(&trace_types_lock);
4889 free_trace_iter_content(iter);
4890 seq_release_private(inode, file);
4895 int tracing_release_generic_tr(struct inode *inode, struct file *file)
4897 struct trace_array *tr = inode->i_private;
4899 trace_array_put(tr);
4903 static int tracing_single_release_tr(struct inode *inode, struct file *file)
4905 struct trace_array *tr = inode->i_private;
4907 trace_array_put(tr);
4909 return single_release(inode, file);
4912 static int tracing_open(struct inode *inode, struct file *file)
4914 struct trace_array *tr = inode->i_private;
4915 struct trace_iterator *iter;
4918 ret = tracing_check_open_get_tr(tr);
4922 /* If this file was open for write, then erase contents */
4923 if ((file->f_mode & FMODE_WRITE) && (file->f_flags & O_TRUNC)) {
4924 int cpu = tracing_get_cpu(inode);
4925 struct array_buffer *trace_buf = &tr->array_buffer;
4927 #ifdef CONFIG_TRACER_MAX_TRACE
4928 if (tr->current_trace->print_max)
4929 trace_buf = &tr->max_buffer;
4932 if (cpu == RING_BUFFER_ALL_CPUS)
4933 tracing_reset_online_cpus(trace_buf);
4935 tracing_reset_cpu(trace_buf, cpu);
4938 if (file->f_mode & FMODE_READ) {
4939 iter = __tracing_open(inode, file, false);
4941 ret = PTR_ERR(iter);
4942 else if (tr->trace_flags & TRACE_ITER_LATENCY_FMT)
4943 iter->iter_flags |= TRACE_FILE_LAT_FMT;
4947 trace_array_put(tr);
4953 * Some tracers are not suitable for instance buffers.
4954 * A tracer is always available for the global array (toplevel)
4955 * or if it explicitly states that it is.
4958 trace_ok_for_array(struct tracer *t, struct trace_array *tr)
4960 #ifdef CONFIG_TRACER_SNAPSHOT
4961 /* arrays with mapped buffer range do not have snapshots */
4962 if (tr->range_addr_start && t->use_max_tr)
4965 return (tr->flags & TRACE_ARRAY_FL_GLOBAL) || t->allow_instances;
4968 /* Find the next tracer that this trace array may use */
4969 static struct tracer *
4970 get_tracer_for_array(struct trace_array *tr, struct tracer *t)
4972 while (t && !trace_ok_for_array(t, tr))
4979 t_next(struct seq_file *m, void *v, loff_t *pos)
4981 struct trace_array *tr = m->private;
4982 struct tracer *t = v;
4987 t = get_tracer_for_array(tr, t->next);
4992 static void *t_start(struct seq_file *m, loff_t *pos)
4994 struct trace_array *tr = m->private;
4998 mutex_lock(&trace_types_lock);
5000 t = get_tracer_for_array(tr, trace_types);
5001 for (; t && l < *pos; t = t_next(m, t, &l))
5007 static void t_stop(struct seq_file *m, void *p)
5009 mutex_unlock(&trace_types_lock);
5012 static int t_show(struct seq_file *m, void *v)
5014 struct tracer *t = v;
5019 seq_puts(m, t->name);
5028 static const struct seq_operations show_traces_seq_ops = {
5035 static int show_traces_open(struct inode *inode, struct file *file)
5037 struct trace_array *tr = inode->i_private;
5041 ret = tracing_check_open_get_tr(tr);
5045 ret = seq_open(file, &show_traces_seq_ops);
5047 trace_array_put(tr);
5051 m = file->private_data;
5057 static int tracing_seq_release(struct inode *inode, struct file *file)
5059 struct trace_array *tr = inode->i_private;
5061 trace_array_put(tr);
5062 return seq_release(inode, file);
5066 tracing_write_stub(struct file *filp, const char __user *ubuf,
5067 size_t count, loff_t *ppos)
5072 loff_t tracing_lseek(struct file *file, loff_t offset, int whence)
5076 if (file->f_mode & FMODE_READ)
5077 ret = seq_lseek(file, offset, whence);
5079 file->f_pos = ret = 0;
5084 static const struct file_operations tracing_fops = {
5085 .open = tracing_open,
5087 .read_iter = seq_read_iter,
5088 .splice_read = copy_splice_read,
5089 .write = tracing_write_stub,
5090 .llseek = tracing_lseek,
5091 .release = tracing_release,
5094 static const struct file_operations show_traces_fops = {
5095 .open = show_traces_open,
5097 .llseek = seq_lseek,
5098 .release = tracing_seq_release,
5102 tracing_cpumask_read(struct file *filp, char __user *ubuf,
5103 size_t count, loff_t *ppos)
5105 struct trace_array *tr = file_inode(filp)->i_private;
5109 len = snprintf(NULL, 0, "%*pb\n",
5110 cpumask_pr_args(tr->tracing_cpumask)) + 1;
5111 mask_str = kmalloc(len, GFP_KERNEL);
5115 len = snprintf(mask_str, len, "%*pb\n",
5116 cpumask_pr_args(tr->tracing_cpumask));
5121 count = simple_read_from_buffer(ubuf, count, ppos, mask_str, len);
5129 int tracing_set_cpumask(struct trace_array *tr,
5130 cpumask_var_t tracing_cpumask_new)
5137 local_irq_disable();
5138 arch_spin_lock(&tr->max_lock);
5139 for_each_tracing_cpu(cpu) {
5141 * Increase/decrease the disabled counter if we are
5142 * about to flip a bit in the cpumask:
5144 if (cpumask_test_cpu(cpu, tr->tracing_cpumask) &&
5145 !cpumask_test_cpu(cpu, tracing_cpumask_new)) {
5146 ring_buffer_record_disable_cpu(tr->array_buffer.buffer, cpu);
5147 #ifdef CONFIG_TRACER_MAX_TRACE
5148 ring_buffer_record_disable_cpu(tr->max_buffer.buffer, cpu);
5151 if (!cpumask_test_cpu(cpu, tr->tracing_cpumask) &&
5152 cpumask_test_cpu(cpu, tracing_cpumask_new)) {
5153 ring_buffer_record_enable_cpu(tr->array_buffer.buffer, cpu);
5154 #ifdef CONFIG_TRACER_MAX_TRACE
5155 ring_buffer_record_enable_cpu(tr->max_buffer.buffer, cpu);
5159 arch_spin_unlock(&tr->max_lock);
5162 cpumask_copy(tr->tracing_cpumask, tracing_cpumask_new);
5168 tracing_cpumask_write(struct file *filp, const char __user *ubuf,
5169 size_t count, loff_t *ppos)
5171 struct trace_array *tr = file_inode(filp)->i_private;
5172 cpumask_var_t tracing_cpumask_new;
5175 if (count == 0 || count > KMALLOC_MAX_SIZE)
5178 if (!zalloc_cpumask_var(&tracing_cpumask_new, GFP_KERNEL))
5181 err = cpumask_parse_user(ubuf, count, tracing_cpumask_new);
5185 err = tracing_set_cpumask(tr, tracing_cpumask_new);
5189 free_cpumask_var(tracing_cpumask_new);
5194 free_cpumask_var(tracing_cpumask_new);
5199 static const struct file_operations tracing_cpumask_fops = {
5200 .open = tracing_open_generic_tr,
5201 .read = tracing_cpumask_read,
5202 .write = tracing_cpumask_write,
5203 .release = tracing_release_generic_tr,
5204 .llseek = generic_file_llseek,
5207 static int tracing_trace_options_show(struct seq_file *m, void *v)
5209 struct tracer_opt *trace_opts;
5210 struct trace_array *tr = m->private;
5214 guard(mutex)(&trace_types_lock);
5216 tracer_flags = tr->current_trace->flags->val;
5217 trace_opts = tr->current_trace->flags->opts;
5219 for (i = 0; trace_options[i]; i++) {
5220 if (tr->trace_flags & (1 << i))
5221 seq_printf(m, "%s\n", trace_options[i]);
5223 seq_printf(m, "no%s\n", trace_options[i]);
5226 for (i = 0; trace_opts[i].name; i++) {
5227 if (tracer_flags & trace_opts[i].bit)
5228 seq_printf(m, "%s\n", trace_opts[i].name);
5230 seq_printf(m, "no%s\n", trace_opts[i].name);
5236 static int __set_tracer_option(struct trace_array *tr,
5237 struct tracer_flags *tracer_flags,
5238 struct tracer_opt *opts, int neg)
5240 struct tracer *trace = tracer_flags->trace;
5243 ret = trace->set_flag(tr, tracer_flags->val, opts->bit, !neg);
5248 tracer_flags->val &= ~opts->bit;
5250 tracer_flags->val |= opts->bit;
5254 /* Try to assign a tracer specific option */
5255 static int set_tracer_option(struct trace_array *tr, char *cmp, int neg)
5257 struct tracer *trace = tr->current_trace;
5258 struct tracer_flags *tracer_flags = trace->flags;
5259 struct tracer_opt *opts = NULL;
5262 for (i = 0; tracer_flags->opts[i].name; i++) {
5263 opts = &tracer_flags->opts[i];
5265 if (strcmp(cmp, opts->name) == 0)
5266 return __set_tracer_option(tr, trace->flags, opts, neg);
5272 /* Some tracers require overwrite to stay enabled */
5273 int trace_keep_overwrite(struct tracer *tracer, u32 mask, int set)
5275 if (tracer->enabled && (mask & TRACE_ITER_OVERWRITE) && !set)
5281 int set_tracer_flag(struct trace_array *tr, unsigned int mask, int enabled)
5283 if ((mask == TRACE_ITER_RECORD_TGID) ||
5284 (mask == TRACE_ITER_RECORD_CMD) ||
5285 (mask == TRACE_ITER_TRACE_PRINTK) ||
5286 (mask == TRACE_ITER_COPY_MARKER))
5287 lockdep_assert_held(&event_mutex);
5289 /* do nothing if flag is already set */
5290 if (!!(tr->trace_flags & mask) == !!enabled)
5293 /* Give the tracer a chance to approve the change */
5294 if (tr->current_trace->flag_changed)
5295 if (tr->current_trace->flag_changed(tr, mask, !!enabled))
5298 if (mask == TRACE_ITER_TRACE_PRINTK) {
5300 update_printk_trace(tr);
5303 * The global_trace cannot clear this.
5304 * It's flag only gets cleared if another instance sets it.
5306 if (printk_trace == &global_trace)
5309 * An instance must always have it set.
5310 * by default, that's the global_trace instane.
5312 if (printk_trace == tr)
5313 update_printk_trace(&global_trace);
5317 if (mask == TRACE_ITER_COPY_MARKER)
5318 update_marker_trace(tr, enabled);
5321 tr->trace_flags |= mask;
5323 tr->trace_flags &= ~mask;
5325 if (mask == TRACE_ITER_RECORD_CMD)
5326 trace_event_enable_cmd_record(enabled);
5328 if (mask == TRACE_ITER_RECORD_TGID) {
5330 if (trace_alloc_tgid_map() < 0) {
5331 tr->trace_flags &= ~TRACE_ITER_RECORD_TGID;
5335 trace_event_enable_tgid_record(enabled);
5338 if (mask == TRACE_ITER_EVENT_FORK)
5339 trace_event_follow_fork(tr, enabled);
5341 if (mask == TRACE_ITER_FUNC_FORK)
5342 ftrace_pid_follow_fork(tr, enabled);
5344 if (mask == TRACE_ITER_OVERWRITE) {
5345 ring_buffer_change_overwrite(tr->array_buffer.buffer, enabled);
5346 #ifdef CONFIG_TRACER_MAX_TRACE
5347 ring_buffer_change_overwrite(tr->max_buffer.buffer, enabled);
5351 if (mask == TRACE_ITER_PRINTK) {
5352 trace_printk_start_stop_comm(enabled);
5353 trace_printk_control(enabled);
5359 int trace_set_options(struct trace_array *tr, char *option)
5364 size_t orig_len = strlen(option);
5367 cmp = strstrip(option);
5369 len = str_has_prefix(cmp, "no");
5375 mutex_lock(&event_mutex);
5376 mutex_lock(&trace_types_lock);
5378 ret = match_string(trace_options, -1, cmp);
5379 /* If no option could be set, test the specific tracer options */
5381 ret = set_tracer_option(tr, cmp, neg);
5383 ret = set_tracer_flag(tr, 1 << ret, !neg);
5385 mutex_unlock(&trace_types_lock);
5386 mutex_unlock(&event_mutex);
5389 * If the first trailing whitespace is replaced with '\0' by strstrip,
5390 * turn it back into a space.
5392 if (orig_len > strlen(option))
5393 option[strlen(option)] = ' ';
5398 static void __init apply_trace_boot_options(void)
5400 char *buf = trace_boot_options_buf;
5404 option = strsep(&buf, ",");
5410 trace_set_options(&global_trace, option);
5412 /* Put back the comma to allow this to be called again */
5419 tracing_trace_options_write(struct file *filp, const char __user *ubuf,
5420 size_t cnt, loff_t *ppos)
5422 struct seq_file *m = filp->private_data;
5423 struct trace_array *tr = m->private;
5427 if (cnt >= sizeof(buf))
5430 if (copy_from_user(buf, ubuf, cnt))
5435 ret = trace_set_options(tr, buf);
5444 static int tracing_trace_options_open(struct inode *inode, struct file *file)
5446 struct trace_array *tr = inode->i_private;
5449 ret = tracing_check_open_get_tr(tr);
5453 ret = single_open(file, tracing_trace_options_show, inode->i_private);
5455 trace_array_put(tr);
5460 static const struct file_operations tracing_iter_fops = {
5461 .open = tracing_trace_options_open,
5463 .llseek = seq_lseek,
5464 .release = tracing_single_release_tr,
5465 .write = tracing_trace_options_write,
5468 static const char readme_msg[] =
5469 "tracing mini-HOWTO:\n\n"
5470 "By default tracefs removes all OTH file permission bits.\n"
5471 "When mounting tracefs an optional group id can be specified\n"
5472 "which adds the group to every directory and file in tracefs:\n\n"
5473 "\t e.g. mount -t tracefs [-o [gid=<gid>]] nodev /sys/kernel/tracing\n\n"
5474 "# echo 0 > tracing_on : quick way to disable tracing\n"
5475 "# echo 1 > tracing_on : quick way to re-enable tracing\n\n"
5476 " Important files:\n"
5477 " trace\t\t\t- The static contents of the buffer\n"
5478 "\t\t\t To clear the buffer write into this file: echo > trace\n"
5479 " trace_pipe\t\t- A consuming read to see the contents of the buffer\n"
5480 " current_tracer\t- function and latency tracers\n"
5481 " available_tracers\t- list of configured tracers for current_tracer\n"
5482 " error_log\t- error log for failed commands (that support it)\n"
5483 " buffer_size_kb\t- view and modify size of per cpu buffer\n"
5484 " buffer_total_size_kb - view total size of all cpu buffers\n\n"
5485 " trace_clock\t\t- change the clock used to order events\n"
5486 " local: Per cpu clock but may not be synced across CPUs\n"
5487 " global: Synced across CPUs but slows tracing down.\n"
5488 " counter: Not a clock, but just an increment\n"
5489 " uptime: Jiffy counter from time of boot\n"
5490 " perf: Same clock that perf events use\n"
5491 #ifdef CONFIG_X86_64
5492 " x86-tsc: TSC cycle counter\n"
5494 "\n timestamp_mode\t- view the mode used to timestamp events\n"
5495 " delta: Delta difference against a buffer-wide timestamp\n"
5496 " absolute: Absolute (standalone) timestamp\n"
5497 "\n trace_marker\t\t- Writes into this file writes into the kernel buffer\n"
5498 "\n trace_marker_raw\t\t- Writes into this file writes binary data into the kernel buffer\n"
5499 " tracing_cpumask\t- Limit which CPUs to trace\n"
5500 " instances\t\t- Make sub-buffers with: mkdir instances/foo\n"
5501 "\t\t\t Remove sub-buffer with rmdir\n"
5502 " trace_options\t\t- Set format or modify how tracing happens\n"
5503 "\t\t\t Disable an option by prefixing 'no' to the\n"
5504 "\t\t\t option name\n"
5505 " saved_cmdlines_size\t- echo command number in here to store comm-pid list\n"
5506 #ifdef CONFIG_DYNAMIC_FTRACE
5507 "\n available_filter_functions - list of functions that can be filtered on\n"
5508 " set_ftrace_filter\t- echo function name in here to only trace these\n"
5509 "\t\t\t functions\n"
5510 "\t accepts: func_full_name or glob-matching-pattern\n"
5511 "\t modules: Can select a group via module\n"
5512 "\t Format: :mod:<module-name>\n"
5513 "\t example: echo :mod:ext3 > set_ftrace_filter\n"
5514 "\t triggers: a command to perform when function is hit\n"
5515 "\t Format: <function>:<trigger>[:count]\n"
5516 "\t trigger: traceon, traceoff\n"
5517 "\t\t enable_event:<system>:<event>\n"
5518 "\t\t disable_event:<system>:<event>\n"
5519 #ifdef CONFIG_STACKTRACE
5522 #ifdef CONFIG_TRACER_SNAPSHOT
5527 "\t example: echo do_fault:traceoff > set_ftrace_filter\n"
5528 "\t echo do_trap:traceoff:3 > set_ftrace_filter\n"
5529 "\t The first one will disable tracing every time do_fault is hit\n"
5530 "\t The second will disable tracing at most 3 times when do_trap is hit\n"
5531 "\t The first time do trap is hit and it disables tracing, the\n"
5532 "\t counter will decrement to 2. If tracing is already disabled,\n"
5533 "\t the counter will not decrement. It only decrements when the\n"
5534 "\t trigger did work\n"
5535 "\t To remove trigger without count:\n"
5536 "\t echo '!<function>:<trigger> > set_ftrace_filter\n"
5537 "\t To remove trigger with a count:\n"
5538 "\t echo '!<function>:<trigger>:0 > set_ftrace_filter\n"
5539 " set_ftrace_notrace\t- echo function name in here to never trace.\n"
5540 "\t accepts: func_full_name, *func_end, func_begin*, *func_middle*\n"
5541 "\t modules: Can select a group via module command :mod:\n"
5542 "\t Does not accept triggers\n"
5543 #endif /* CONFIG_DYNAMIC_FTRACE */
5544 #ifdef CONFIG_FUNCTION_TRACER
5545 " set_ftrace_pid\t- Write pid(s) to only function trace those pids\n"
5547 " set_ftrace_notrace_pid\t- Write pid(s) to not function trace those pids\n"
5550 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
5551 " set_graph_function\t- Trace the nested calls of a function (function_graph)\n"
5552 " set_graph_notrace\t- Do not trace the nested calls of a function (function_graph)\n"
5553 " max_graph_depth\t- Trace a limited depth of nested calls (0 is unlimited)\n"
5555 #ifdef CONFIG_TRACER_SNAPSHOT
5556 "\n snapshot\t\t- Like 'trace' but shows the content of the static\n"
5557 "\t\t\t snapshot buffer. Read the contents for more\n"
5558 "\t\t\t information\n"
5560 #ifdef CONFIG_STACK_TRACER
5561 " stack_trace\t\t- Shows the max stack trace when active\n"
5562 " stack_max_size\t- Shows current max stack size that was traced\n"
5563 "\t\t\t Write into this file to reset the max size (trigger a\n"
5564 "\t\t\t new trace)\n"
5565 #ifdef CONFIG_DYNAMIC_FTRACE
5566 " stack_trace_filter\t- Like set_ftrace_filter but limits what stack_trace\n"
5569 #endif /* CONFIG_STACK_TRACER */
5570 #ifdef CONFIG_DYNAMIC_EVENTS
5571 " dynamic_events\t\t- Create/append/remove/show the generic dynamic events\n"
5572 "\t\t\t Write into this file to define/undefine new trace events.\n"
5574 #ifdef CONFIG_KPROBE_EVENTS
5575 " kprobe_events\t\t- Create/append/remove/show the kernel dynamic events\n"
5576 "\t\t\t Write into this file to define/undefine new trace events.\n"
5578 #ifdef CONFIG_UPROBE_EVENTS
5579 " uprobe_events\t\t- Create/append/remove/show the userspace dynamic events\n"
5580 "\t\t\t Write into this file to define/undefine new trace events.\n"
5582 #if defined(CONFIG_KPROBE_EVENTS) || defined(CONFIG_UPROBE_EVENTS) || \
5583 defined(CONFIG_FPROBE_EVENTS)
5584 "\t accepts: event-definitions (one definition per line)\n"
5585 #if defined(CONFIG_KPROBE_EVENTS) || defined(CONFIG_UPROBE_EVENTS)
5586 "\t Format: p[:[<group>/][<event>]] <place> [<args>]\n"
5587 "\t r[maxactive][:[<group>/][<event>]] <place> [<args>]\n"
5589 #ifdef CONFIG_FPROBE_EVENTS
5590 "\t f[:[<group>/][<event>]] <func-name>[%return] [<args>]\n"
5591 "\t t[:[<group>/][<event>]] <tracepoint> [<args>]\n"
5593 #ifdef CONFIG_HIST_TRIGGERS
5594 "\t s:[synthetic/]<event> <field> [<field>]\n"
5596 "\t e[:[<group>/][<event>]] <attached-group>.<attached-event> [<args>] [if <filter>]\n"
5597 "\t -:[<group>/][<event>]\n"
5598 #ifdef CONFIG_KPROBE_EVENTS
5599 "\t place: [<module>:]<symbol>[+<offset>]|<memaddr>\n"
5600 "place (kretprobe): [<module>:]<symbol>[+<offset>]%return|<memaddr>\n"
5602 #ifdef CONFIG_UPROBE_EVENTS
5603 " place (uprobe): <path>:<offset>[%return][(ref_ctr_offset)]\n"
5605 "\t args: <name>=fetcharg[:type]\n"
5606 "\t fetcharg: (%<register>|$<efield>), @<address>, @<symbol>[+|-<offset>],\n"
5607 #ifdef CONFIG_HAVE_FUNCTION_ARG_ACCESS_API
5608 "\t $stack<index>, $stack, $retval, $comm, $arg<N>,\n"
5609 #ifdef CONFIG_PROBE_EVENTS_BTF_ARGS
5610 "\t <argname>[->field[->field|.field...]],\n"
5613 "\t $stack<index>, $stack, $retval, $comm,\n"
5615 "\t +|-[u]<offset>(<fetcharg>), \\imm-value, \\\"imm-string\"\n"
5616 "\t kernel return probes support: $retval, $arg<N>, $comm\n"
5617 "\t type: s8/16/32/64, u8/16/32/64, x8/16/32/64, char, string, symbol,\n"
5618 "\t b<bit-width>@<bit-offset>/<container-size>, ustring,\n"
5619 "\t symstr, %pd/%pD, <type>\\[<array-size>\\]\n"
5620 #ifdef CONFIG_HIST_TRIGGERS
5621 "\t field: <stype> <name>;\n"
5622 "\t stype: u8/u16/u32/u64, s8/s16/s32/s64, pid_t,\n"
5623 "\t [unsigned] char/int/long\n"
5625 "\t efield: For event probes ('e' types), the field is on of the fields\n"
5626 "\t of the <attached-group>/<attached-event>.\n"
5628 " set_event\t\t- Enables events by name written into it\n"
5629 "\t\t\t Can enable module events via: :mod:<module>\n"
5630 " events/\t\t- Directory containing all trace event subsystems:\n"
5631 " enable\t\t- Write 0/1 to enable/disable tracing of all events\n"
5632 " events/<system>/\t- Directory containing all trace events for <system>:\n"
5633 " enable\t\t- Write 0/1 to enable/disable tracing of all <system>\n"
5635 " filter\t\t- If set, only events passing filter are traced\n"
5636 " events/<system>/<event>/\t- Directory containing control files for\n"
5638 " enable\t\t- Write 0/1 to enable/disable tracing of <event>\n"
5639 " filter\t\t- If set, only events passing filter are traced\n"
5640 " trigger\t\t- If set, a command to perform when event is hit\n"
5641 "\t Format: <trigger>[:count][if <filter>]\n"
5642 "\t trigger: traceon, traceoff\n"
5643 "\t enable_event:<system>:<event>\n"
5644 "\t disable_event:<system>:<event>\n"
5645 #ifdef CONFIG_HIST_TRIGGERS
5646 "\t enable_hist:<system>:<event>\n"
5647 "\t disable_hist:<system>:<event>\n"
5649 #ifdef CONFIG_STACKTRACE
5652 #ifdef CONFIG_TRACER_SNAPSHOT
5655 #ifdef CONFIG_HIST_TRIGGERS
5656 "\t\t hist (see below)\n"
5658 "\t example: echo traceoff > events/block/block_unplug/trigger\n"
5659 "\t echo traceoff:3 > events/block/block_unplug/trigger\n"
5660 "\t echo 'enable_event:kmem:kmalloc:3 if nr_rq > 1' > \\\n"
5661 "\t events/block/block_unplug/trigger\n"
5662 "\t The first disables tracing every time block_unplug is hit.\n"
5663 "\t The second disables tracing the first 3 times block_unplug is hit.\n"
5664 "\t The third enables the kmalloc event the first 3 times block_unplug\n"
5665 "\t is hit and has value of greater than 1 for the 'nr_rq' event field.\n"
5666 "\t Like function triggers, the counter is only decremented if it\n"
5667 "\t enabled or disabled tracing.\n"
5668 "\t To remove a trigger without a count:\n"
5669 "\t echo '!<trigger> > <system>/<event>/trigger\n"
5670 "\t To remove a trigger with a count:\n"
5671 "\t echo '!<trigger>:0 > <system>/<event>/trigger\n"
5672 "\t Filters can be ignored when removing a trigger.\n"
5673 #ifdef CONFIG_HIST_TRIGGERS
5674 " hist trigger\t- If set, event hits are aggregated into a hash table\n"
5675 "\t Format: hist:keys=<field1[,field2,...]>\n"
5676 "\t [:<var1>=<field|var_ref|numeric_literal>[,<var2>=...]]\n"
5677 "\t [:values=<field1[,field2,...]>]\n"
5678 "\t [:sort=<field1[,field2,...]>]\n"
5679 "\t [:size=#entries]\n"
5680 "\t [:pause][:continue][:clear]\n"
5681 "\t [:name=histname1]\n"
5682 "\t [:nohitcount]\n"
5683 "\t [:<handler>.<action>]\n"
5684 "\t [if <filter>]\n\n"
5685 "\t Note, special fields can be used as well:\n"
5686 "\t common_timestamp - to record current timestamp\n"
5687 "\t common_cpu - to record the CPU the event happened on\n"
5689 "\t A hist trigger variable can be:\n"
5690 "\t - a reference to a field e.g. x=current_timestamp,\n"
5691 "\t - a reference to another variable e.g. y=$x,\n"
5692 "\t - a numeric literal: e.g. ms_per_sec=1000,\n"
5693 "\t - an arithmetic expression: e.g. time_secs=current_timestamp/1000\n"
5695 "\t hist trigger arithmetic expressions support addition(+), subtraction(-),\n"
5696 "\t multiplication(*) and division(/) operators. An operand can be either a\n"
5697 "\t variable reference, field or numeric literal.\n"
5699 "\t When a matching event is hit, an entry is added to a hash\n"
5700 "\t table using the key(s) and value(s) named, and the value of a\n"
5701 "\t sum called 'hitcount' is incremented. Keys and values\n"
5702 "\t correspond to fields in the event's format description. Keys\n"
5703 "\t can be any field, or the special string 'common_stacktrace'.\n"
5704 "\t Compound keys consisting of up to two fields can be specified\n"
5705 "\t by the 'keys' keyword. Values must correspond to numeric\n"
5706 "\t fields. Sort keys consisting of up to two fields can be\n"
5707 "\t specified using the 'sort' keyword. The sort direction can\n"
5708 "\t be modified by appending '.descending' or '.ascending' to a\n"
5709 "\t sort field. The 'size' parameter can be used to specify more\n"
5710 "\t or fewer than the default 2048 entries for the hashtable size.\n"
5711 "\t If a hist trigger is given a name using the 'name' parameter,\n"
5712 "\t its histogram data will be shared with other triggers of the\n"
5713 "\t same name, and trigger hits will update this common data.\n\n"
5714 "\t Reading the 'hist' file for the event will dump the hash\n"
5715 "\t table in its entirety to stdout. If there are multiple hist\n"
5716 "\t triggers attached to an event, there will be a table for each\n"
5717 "\t trigger in the output. The table displayed for a named\n"
5718 "\t trigger will be the same as any other instance having the\n"
5719 "\t same name. The default format used to display a given field\n"
5720 "\t can be modified by appending any of the following modifiers\n"
5721 "\t to the field name, as applicable:\n\n"
5722 "\t .hex display a number as a hex value\n"
5723 "\t .sym display an address as a symbol\n"
5724 "\t .sym-offset display an address as a symbol and offset\n"
5725 "\t .execname display a common_pid as a program name\n"
5726 "\t .syscall display a syscall id as a syscall name\n"
5727 "\t .log2 display log2 value rather than raw number\n"
5728 "\t .buckets=size display values in groups of size rather than raw number\n"
5729 "\t .usecs display a common_timestamp in microseconds\n"
5730 "\t .percent display a number of percentage value\n"
5731 "\t .graph display a bar-graph of a value\n\n"
5732 "\t The 'pause' parameter can be used to pause an existing hist\n"
5733 "\t trigger or to start a hist trigger but not log any events\n"
5734 "\t until told to do so. 'continue' can be used to start or\n"
5735 "\t restart a paused hist trigger.\n\n"
5736 "\t The 'clear' parameter will clear the contents of a running\n"
5737 "\t hist trigger and leave its current paused/active state\n"
5739 "\t The 'nohitcount' (or NOHC) parameter will suppress display of\n"
5740 "\t raw hitcount in the histogram.\n\n"
5741 "\t The enable_hist and disable_hist triggers can be used to\n"
5742 "\t have one event conditionally start and stop another event's\n"
5743 "\t already-attached hist trigger. The syntax is analogous to\n"
5744 "\t the enable_event and disable_event triggers.\n\n"
5745 "\t Hist trigger handlers and actions are executed whenever a\n"
5746 "\t a histogram entry is added or updated. They take the form:\n\n"
5747 "\t <handler>.<action>\n\n"
5748 "\t The available handlers are:\n\n"
5749 "\t onmatch(matching.event) - invoke on addition or update\n"
5750 "\t onmax(var) - invoke if var exceeds current max\n"
5751 "\t onchange(var) - invoke action if var changes\n\n"
5752 "\t The available actions are:\n\n"
5753 "\t trace(<synthetic_event>,param list) - generate synthetic event\n"
5754 "\t save(field,...) - save current event fields\n"
5755 #ifdef CONFIG_TRACER_SNAPSHOT
5756 "\t snapshot() - snapshot the trace buffer\n\n"
5758 #ifdef CONFIG_SYNTH_EVENTS
5759 " events/synthetic_events\t- Create/append/remove/show synthetic events\n"
5760 "\t Write into this file to define/undefine new synthetic events.\n"
5761 "\t example: echo 'myevent u64 lat; char name[]; long[] stack' >> synthetic_events\n"
5767 tracing_readme_read(struct file *filp, char __user *ubuf,
5768 size_t cnt, loff_t *ppos)
5770 return simple_read_from_buffer(ubuf, cnt, ppos,
5771 readme_msg, strlen(readme_msg));
5774 static const struct file_operations tracing_readme_fops = {
5775 .open = tracing_open_generic,
5776 .read = tracing_readme_read,
5777 .llseek = generic_file_llseek,
5780 #ifdef CONFIG_TRACE_EVAL_MAP_FILE
5781 static union trace_eval_map_item *
5782 update_eval_map(union trace_eval_map_item *ptr)
5784 if (!ptr->map.eval_string) {
5785 if (ptr->tail.next) {
5786 ptr = ptr->tail.next;
5787 /* Set ptr to the next real item (skip head) */
5795 static void *eval_map_next(struct seq_file *m, void *v, loff_t *pos)
5797 union trace_eval_map_item *ptr = v;
5800 * Paranoid! If ptr points to end, we don't want to increment past it.
5801 * This really should never happen.
5804 ptr = update_eval_map(ptr);
5805 if (WARN_ON_ONCE(!ptr))
5809 ptr = update_eval_map(ptr);
5814 static void *eval_map_start(struct seq_file *m, loff_t *pos)
5816 union trace_eval_map_item *v;
5819 mutex_lock(&trace_eval_mutex);
5821 v = trace_eval_maps;
5825 while (v && l < *pos) {
5826 v = eval_map_next(m, v, &l);
5832 static void eval_map_stop(struct seq_file *m, void *v)
5834 mutex_unlock(&trace_eval_mutex);
5837 static int eval_map_show(struct seq_file *m, void *v)
5839 union trace_eval_map_item *ptr = v;
5841 seq_printf(m, "%s %ld (%s)\n",
5842 ptr->map.eval_string, ptr->map.eval_value,
5848 static const struct seq_operations tracing_eval_map_seq_ops = {
5849 .start = eval_map_start,
5850 .next = eval_map_next,
5851 .stop = eval_map_stop,
5852 .show = eval_map_show,
5855 static int tracing_eval_map_open(struct inode *inode, struct file *filp)
5859 ret = tracing_check_open_get_tr(NULL);
5863 return seq_open(filp, &tracing_eval_map_seq_ops);
5866 static const struct file_operations tracing_eval_map_fops = {
5867 .open = tracing_eval_map_open,
5869 .llseek = seq_lseek,
5870 .release = seq_release,
5873 static inline union trace_eval_map_item *
5874 trace_eval_jmp_to_tail(union trace_eval_map_item *ptr)
5876 /* Return tail of array given the head */
5877 return ptr + ptr->head.length + 1;
5881 trace_insert_eval_map_file(struct module *mod, struct trace_eval_map **start,
5884 struct trace_eval_map **stop;
5885 struct trace_eval_map **map;
5886 union trace_eval_map_item *map_array;
5887 union trace_eval_map_item *ptr;
5892 * The trace_eval_maps contains the map plus a head and tail item,
5893 * where the head holds the module and length of array, and the
5894 * tail holds a pointer to the next list.
5896 map_array = kmalloc_array(len + 2, sizeof(*map_array), GFP_KERNEL);
5898 pr_warn("Unable to allocate trace eval mapping\n");
5902 guard(mutex)(&trace_eval_mutex);
5904 if (!trace_eval_maps)
5905 trace_eval_maps = map_array;
5907 ptr = trace_eval_maps;
5909 ptr = trace_eval_jmp_to_tail(ptr);
5910 if (!ptr->tail.next)
5912 ptr = ptr->tail.next;
5915 ptr->tail.next = map_array;
5917 map_array->head.mod = mod;
5918 map_array->head.length = len;
5921 for (map = start; (unsigned long)map < (unsigned long)stop; map++) {
5922 map_array->map = **map;
5925 memset(map_array, 0, sizeof(*map_array));
5928 static void trace_create_eval_file(struct dentry *d_tracer)
5930 trace_create_file("eval_map", TRACE_MODE_READ, d_tracer,
5931 NULL, &tracing_eval_map_fops);
5934 #else /* CONFIG_TRACE_EVAL_MAP_FILE */
5935 static inline void trace_create_eval_file(struct dentry *d_tracer) { }
5936 static inline void trace_insert_eval_map_file(struct module *mod,
5937 struct trace_eval_map **start, int len) { }
5938 #endif /* !CONFIG_TRACE_EVAL_MAP_FILE */
5940 static void trace_insert_eval_map(struct module *mod,
5941 struct trace_eval_map **start, int len)
5943 struct trace_eval_map **map;
5950 trace_event_eval_update(map, len);
5952 trace_insert_eval_map_file(mod, start, len);
5956 tracing_set_trace_read(struct file *filp, char __user *ubuf,
5957 size_t cnt, loff_t *ppos)
5959 struct trace_array *tr = filp->private_data;
5960 char buf[MAX_TRACER_SIZE+2];
5963 mutex_lock(&trace_types_lock);
5964 r = sprintf(buf, "%s\n", tr->current_trace->name);
5965 mutex_unlock(&trace_types_lock);
5967 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
5970 int tracer_init(struct tracer *t, struct trace_array *tr)
5972 tracing_reset_online_cpus(&tr->array_buffer);
5976 static void set_buffer_entries(struct array_buffer *buf, unsigned long val)
5980 for_each_tracing_cpu(cpu)
5981 per_cpu_ptr(buf->data, cpu)->entries = val;
5984 static void update_buffer_entries(struct array_buffer *buf, int cpu)
5986 if (cpu == RING_BUFFER_ALL_CPUS) {
5987 set_buffer_entries(buf, ring_buffer_size(buf->buffer, 0));
5989 per_cpu_ptr(buf->data, cpu)->entries = ring_buffer_size(buf->buffer, cpu);
5993 #ifdef CONFIG_TRACER_MAX_TRACE
5994 /* resize @tr's buffer to the size of @size_tr's entries */
5995 static int resize_buffer_duplicate_size(struct array_buffer *trace_buf,
5996 struct array_buffer *size_buf, int cpu_id)
6000 if (cpu_id == RING_BUFFER_ALL_CPUS) {
6001 for_each_tracing_cpu(cpu) {
6002 ret = ring_buffer_resize(trace_buf->buffer,
6003 per_cpu_ptr(size_buf->data, cpu)->entries, cpu);
6006 per_cpu_ptr(trace_buf->data, cpu)->entries =
6007 per_cpu_ptr(size_buf->data, cpu)->entries;
6010 ret = ring_buffer_resize(trace_buf->buffer,
6011 per_cpu_ptr(size_buf->data, cpu_id)->entries, cpu_id);
6013 per_cpu_ptr(trace_buf->data, cpu_id)->entries =
6014 per_cpu_ptr(size_buf->data, cpu_id)->entries;
6019 #endif /* CONFIG_TRACER_MAX_TRACE */
6021 static int __tracing_resize_ring_buffer(struct trace_array *tr,
6022 unsigned long size, int cpu)
6027 * If kernel or user changes the size of the ring buffer
6028 * we use the size that was given, and we can forget about
6029 * expanding it later.
6031 trace_set_ring_buffer_expanded(tr);
6033 /* May be called before buffers are initialized */
6034 if (!tr->array_buffer.buffer)
6037 /* Do not allow tracing while resizing ring buffer */
6038 tracing_stop_tr(tr);
6040 ret = ring_buffer_resize(tr->array_buffer.buffer, size, cpu);
6044 #ifdef CONFIG_TRACER_MAX_TRACE
6045 if (!tr->allocated_snapshot)
6048 ret = ring_buffer_resize(tr->max_buffer.buffer, size, cpu);
6050 int r = resize_buffer_duplicate_size(&tr->array_buffer,
6051 &tr->array_buffer, cpu);
6054 * AARGH! We are left with different
6055 * size max buffer!!!!
6056 * The max buffer is our "snapshot" buffer.
6057 * When a tracer needs a snapshot (one of the
6058 * latency tracers), it swaps the max buffer
6059 * with the saved snap shot. We succeeded to
6060 * update the size of the main buffer, but failed to
6061 * update the size of the max buffer. But when we tried
6062 * to reset the main buffer to the original size, we
6063 * failed there too. This is very unlikely to
6064 * happen, but if it does, warn and kill all
6068 tracing_disabled = 1;
6073 update_buffer_entries(&tr->max_buffer, cpu);
6076 #endif /* CONFIG_TRACER_MAX_TRACE */
6078 update_buffer_entries(&tr->array_buffer, cpu);
6080 tracing_start_tr(tr);
6084 ssize_t tracing_resize_ring_buffer(struct trace_array *tr,
6085 unsigned long size, int cpu_id)
6087 guard(mutex)(&trace_types_lock);
6089 if (cpu_id != RING_BUFFER_ALL_CPUS) {
6090 /* make sure, this cpu is enabled in the mask */
6091 if (!cpumask_test_cpu(cpu_id, tracing_buffer_mask))
6095 return __tracing_resize_ring_buffer(tr, size, cpu_id);
6098 struct trace_mod_entry {
6099 unsigned long mod_addr;
6100 char mod_name[MODULE_NAME_LEN];
6103 struct trace_scratch {
6104 unsigned int clock_id;
6105 unsigned long text_addr;
6106 unsigned long nr_entries;
6107 struct trace_mod_entry entries[];
6110 static DEFINE_MUTEX(scratch_mutex);
6112 static int cmp_mod_entry(const void *key, const void *pivot)
6114 unsigned long addr = (unsigned long)key;
6115 const struct trace_mod_entry *ent = pivot;
6117 if (addr >= ent[0].mod_addr && addr < ent[1].mod_addr)
6120 return addr - ent->mod_addr;
6124 * trace_adjust_address() - Adjust prev boot address to current address.
6125 * @tr: Persistent ring buffer's trace_array.
6126 * @addr: Address in @tr which is adjusted.
6128 unsigned long trace_adjust_address(struct trace_array *tr, unsigned long addr)
6130 struct trace_module_delta *module_delta;
6131 struct trace_scratch *tscratch;
6132 struct trace_mod_entry *entry;
6133 unsigned long raddr;
6134 int idx = 0, nr_entries;
6136 /* If we don't have last boot delta, return the address */
6137 if (!(tr->flags & TRACE_ARRAY_FL_LAST_BOOT))
6140 /* tr->module_delta must be protected by rcu. */
6142 tscratch = tr->scratch;
6143 /* if there is no tscrach, module_delta must be NULL. */
6144 module_delta = READ_ONCE(tr->module_delta);
6145 if (!module_delta || !tscratch->nr_entries ||
6146 tscratch->entries[0].mod_addr > addr) {
6147 raddr = addr + tr->text_delta;
6148 return __is_kernel(raddr) || is_kernel_core_data(raddr) ||
6149 is_kernel_rodata(raddr) ? raddr : addr;
6152 /* Note that entries must be sorted. */
6153 nr_entries = tscratch->nr_entries;
6154 if (nr_entries == 1 ||
6155 tscratch->entries[nr_entries - 1].mod_addr < addr)
6156 idx = nr_entries - 1;
6158 entry = __inline_bsearch((void *)addr,
6161 sizeof(tscratch->entries[0]),
6164 idx = entry - tscratch->entries;
6167 return addr + module_delta->delta[idx];
6170 #ifdef CONFIG_MODULES
6171 static int save_mod(struct module *mod, void *data)
6173 struct trace_array *tr = data;
6174 struct trace_scratch *tscratch;
6175 struct trace_mod_entry *entry;
6178 tscratch = tr->scratch;
6181 size = tr->scratch_size;
6183 if (struct_size(tscratch, entries, tscratch->nr_entries + 1) > size)
6186 entry = &tscratch->entries[tscratch->nr_entries];
6188 tscratch->nr_entries++;
6190 entry->mod_addr = (unsigned long)mod->mem[MOD_TEXT].base;
6191 strscpy(entry->mod_name, mod->name);
6196 static int save_mod(struct module *mod, void *data)
6202 static void update_last_data(struct trace_array *tr)
6204 struct trace_module_delta *module_delta;
6205 struct trace_scratch *tscratch;
6207 if (!(tr->flags & TRACE_ARRAY_FL_BOOT))
6210 if (!(tr->flags & TRACE_ARRAY_FL_LAST_BOOT))
6213 /* Only if the buffer has previous boot data clear and update it. */
6214 tr->flags &= ~TRACE_ARRAY_FL_LAST_BOOT;
6216 /* Reset the module list and reload them */
6218 struct trace_scratch *tscratch = tr->scratch;
6220 tscratch->clock_id = tr->clock_id;
6221 memset(tscratch->entries, 0,
6222 flex_array_size(tscratch, entries, tscratch->nr_entries));
6223 tscratch->nr_entries = 0;
6225 guard(mutex)(&scratch_mutex);
6226 module_for_each_mod(save_mod, tr);
6230 * Need to clear all CPU buffers as there cannot be events
6231 * from the previous boot mixed with events with this boot
6232 * as that will cause a confusing trace. Need to clear all
6233 * CPU buffers, even for those that may currently be offline.
6235 tracing_reset_all_cpus(&tr->array_buffer);
6237 /* Using current data now */
6243 tscratch = tr->scratch;
6244 module_delta = READ_ONCE(tr->module_delta);
6245 WRITE_ONCE(tr->module_delta, NULL);
6246 kfree_rcu(module_delta, rcu);
6248 /* Set the persistent ring buffer meta data to this address */
6249 tscratch->text_addr = (unsigned long)_text;
6253 * tracing_update_buffers - used by tracing facility to expand ring buffers
6254 * @tr: The tracing instance
6256 * To save on memory when the tracing is never used on a system with it
6257 * configured in. The ring buffers are set to a minimum size. But once
6258 * a user starts to use the tracing facility, then they need to grow
6259 * to their default size.
6261 * This function is to be called when a tracer is about to be used.
6263 int tracing_update_buffers(struct trace_array *tr)
6267 mutex_lock(&trace_types_lock);
6269 update_last_data(tr);
6271 if (!tr->ring_buffer_expanded)
6272 ret = __tracing_resize_ring_buffer(tr, trace_buf_size,
6273 RING_BUFFER_ALL_CPUS);
6274 mutex_unlock(&trace_types_lock);
6279 struct trace_option_dentry;
6282 create_trace_option_files(struct trace_array *tr, struct tracer *tracer);
6285 * Used to clear out the tracer before deletion of an instance.
6286 * Must have trace_types_lock held.
6288 static void tracing_set_nop(struct trace_array *tr)
6290 if (tr->current_trace == &nop_trace)
6293 tr->current_trace->enabled--;
6295 if (tr->current_trace->reset)
6296 tr->current_trace->reset(tr);
6298 tr->current_trace = &nop_trace;
6301 static bool tracer_options_updated;
6303 static void add_tracer_options(struct trace_array *tr, struct tracer *t)
6305 /* Only enable if the directory has been created already. */
6309 /* Only create trace option files after update_tracer_options finish */
6310 if (!tracer_options_updated)
6313 create_trace_option_files(tr, t);
6316 int tracing_set_tracer(struct trace_array *tr, const char *buf)
6319 #ifdef CONFIG_TRACER_MAX_TRACE
6324 guard(mutex)(&trace_types_lock);
6326 update_last_data(tr);
6328 if (!tr->ring_buffer_expanded) {
6329 ret = __tracing_resize_ring_buffer(tr, trace_buf_size,
6330 RING_BUFFER_ALL_CPUS);
6336 for (t = trace_types; t; t = t->next) {
6337 if (strcmp(t->name, buf) == 0)
6343 if (t == tr->current_trace)
6346 #ifdef CONFIG_TRACER_SNAPSHOT
6347 if (t->use_max_tr) {
6348 local_irq_disable();
6349 arch_spin_lock(&tr->max_lock);
6350 ret = tr->cond_snapshot ? -EBUSY : 0;
6351 arch_spin_unlock(&tr->max_lock);
6357 /* Some tracers won't work on kernel command line */
6358 if (system_state < SYSTEM_RUNNING && t->noboot) {
6359 pr_warn("Tracer '%s' is not allowed on command line, ignored\n",
6364 /* Some tracers are only allowed for the top level buffer */
6365 if (!trace_ok_for_array(t, tr))
6368 /* If trace pipe files are being read, we can't change the tracer */
6372 trace_branch_disable();
6374 tr->current_trace->enabled--;
6376 if (tr->current_trace->reset)
6377 tr->current_trace->reset(tr);
6379 #ifdef CONFIG_TRACER_MAX_TRACE
6380 had_max_tr = tr->current_trace->use_max_tr;
6382 /* Current trace needs to be nop_trace before synchronize_rcu */
6383 tr->current_trace = &nop_trace;
6385 if (had_max_tr && !t->use_max_tr) {
6387 * We need to make sure that the update_max_tr sees that
6388 * current_trace changed to nop_trace to keep it from
6389 * swapping the buffers after we resize it.
6390 * The update_max_tr is called from interrupts disabled
6391 * so a synchronized_sched() is sufficient.
6395 tracing_disarm_snapshot(tr);
6398 if (!had_max_tr && t->use_max_tr) {
6399 ret = tracing_arm_snapshot_locked(tr);
6404 tr->current_trace = &nop_trace;
6408 ret = tracer_init(t, tr);
6410 #ifdef CONFIG_TRACER_MAX_TRACE
6412 tracing_disarm_snapshot(tr);
6418 tr->current_trace = t;
6419 tr->current_trace->enabled++;
6420 trace_branch_enable(tr);
6426 tracing_set_trace_write(struct file *filp, const char __user *ubuf,
6427 size_t cnt, loff_t *ppos)
6429 struct trace_array *tr = filp->private_data;
6430 char buf[MAX_TRACER_SIZE+1];
6437 if (cnt > MAX_TRACER_SIZE)
6438 cnt = MAX_TRACER_SIZE;
6440 if (copy_from_user(buf, ubuf, cnt))
6447 err = tracing_set_tracer(tr, name);
6457 tracing_nsecs_read(unsigned long *ptr, char __user *ubuf,
6458 size_t cnt, loff_t *ppos)
6463 r = snprintf(buf, sizeof(buf), "%ld\n",
6464 *ptr == (unsigned long)-1 ? -1 : nsecs_to_usecs(*ptr));
6465 if (r > sizeof(buf))
6467 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
6471 tracing_nsecs_write(unsigned long *ptr, const char __user *ubuf,
6472 size_t cnt, loff_t *ppos)
6477 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
6487 tracing_thresh_read(struct file *filp, char __user *ubuf,
6488 size_t cnt, loff_t *ppos)
6490 return tracing_nsecs_read(&tracing_thresh, ubuf, cnt, ppos);
6494 tracing_thresh_write(struct file *filp, const char __user *ubuf,
6495 size_t cnt, loff_t *ppos)
6497 struct trace_array *tr = filp->private_data;
6500 guard(mutex)(&trace_types_lock);
6501 ret = tracing_nsecs_write(&tracing_thresh, ubuf, cnt, ppos);
6505 if (tr->current_trace->update_thresh) {
6506 ret = tr->current_trace->update_thresh(tr);
6514 #ifdef CONFIG_TRACER_MAX_TRACE
6517 tracing_max_lat_read(struct file *filp, char __user *ubuf,
6518 size_t cnt, loff_t *ppos)
6520 struct trace_array *tr = filp->private_data;
6522 return tracing_nsecs_read(&tr->max_latency, ubuf, cnt, ppos);
6526 tracing_max_lat_write(struct file *filp, const char __user *ubuf,
6527 size_t cnt, loff_t *ppos)
6529 struct trace_array *tr = filp->private_data;
6531 return tracing_nsecs_write(&tr->max_latency, ubuf, cnt, ppos);
6536 static int open_pipe_on_cpu(struct trace_array *tr, int cpu)
6538 if (cpu == RING_BUFFER_ALL_CPUS) {
6539 if (cpumask_empty(tr->pipe_cpumask)) {
6540 cpumask_setall(tr->pipe_cpumask);
6543 } else if (!cpumask_test_cpu(cpu, tr->pipe_cpumask)) {
6544 cpumask_set_cpu(cpu, tr->pipe_cpumask);
6550 static void close_pipe_on_cpu(struct trace_array *tr, int cpu)
6552 if (cpu == RING_BUFFER_ALL_CPUS) {
6553 WARN_ON(!cpumask_full(tr->pipe_cpumask));
6554 cpumask_clear(tr->pipe_cpumask);
6556 WARN_ON(!cpumask_test_cpu(cpu, tr->pipe_cpumask));
6557 cpumask_clear_cpu(cpu, tr->pipe_cpumask);
6561 static int tracing_open_pipe(struct inode *inode, struct file *filp)
6563 struct trace_array *tr = inode->i_private;
6564 struct trace_iterator *iter;
6568 ret = tracing_check_open_get_tr(tr);
6572 mutex_lock(&trace_types_lock);
6573 cpu = tracing_get_cpu(inode);
6574 ret = open_pipe_on_cpu(tr, cpu);
6576 goto fail_pipe_on_cpu;
6578 /* create a buffer to store the information to pass to userspace */
6579 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
6582 goto fail_alloc_iter;
6585 trace_seq_init(&iter->seq);
6586 iter->trace = tr->current_trace;
6588 if (!alloc_cpumask_var(&iter->started, GFP_KERNEL)) {
6593 /* trace pipe does not show start of buffer */
6594 cpumask_setall(iter->started);
6596 if (tr->trace_flags & TRACE_ITER_LATENCY_FMT)
6597 iter->iter_flags |= TRACE_FILE_LAT_FMT;
6599 /* Output in nanoseconds only if we are using a clock in nanoseconds. */
6600 if (trace_clocks[tr->clock_id].in_ns)
6601 iter->iter_flags |= TRACE_FILE_TIME_IN_NS;
6604 iter->array_buffer = &tr->array_buffer;
6605 iter->cpu_file = cpu;
6606 mutex_init(&iter->mutex);
6607 filp->private_data = iter;
6609 if (iter->trace->pipe_open)
6610 iter->trace->pipe_open(iter);
6612 nonseekable_open(inode, filp);
6616 mutex_unlock(&trace_types_lock);
6622 close_pipe_on_cpu(tr, cpu);
6624 __trace_array_put(tr);
6625 mutex_unlock(&trace_types_lock);
6629 static int tracing_release_pipe(struct inode *inode, struct file *file)
6631 struct trace_iterator *iter = file->private_data;
6632 struct trace_array *tr = inode->i_private;
6634 mutex_lock(&trace_types_lock);
6638 if (iter->trace->pipe_close)
6639 iter->trace->pipe_close(iter);
6640 close_pipe_on_cpu(tr, iter->cpu_file);
6641 mutex_unlock(&trace_types_lock);
6643 free_trace_iter_content(iter);
6646 trace_array_put(tr);
6652 trace_poll(struct trace_iterator *iter, struct file *filp, poll_table *poll_table)
6654 struct trace_array *tr = iter->tr;
6656 /* Iterators are static, they should be filled or empty */
6657 if (trace_buffer_iter(iter, iter->cpu_file))
6658 return EPOLLIN | EPOLLRDNORM;
6660 if (tr->trace_flags & TRACE_ITER_BLOCK)
6662 * Always select as readable when in blocking mode
6664 return EPOLLIN | EPOLLRDNORM;
6666 return ring_buffer_poll_wait(iter->array_buffer->buffer, iter->cpu_file,
6667 filp, poll_table, iter->tr->buffer_percent);
6671 tracing_poll_pipe(struct file *filp, poll_table *poll_table)
6673 struct trace_iterator *iter = filp->private_data;
6675 return trace_poll(iter, filp, poll_table);
6678 /* Must be called with iter->mutex held. */
6679 static int tracing_wait_pipe(struct file *filp)
6681 struct trace_iterator *iter = filp->private_data;
6684 while (trace_empty(iter)) {
6686 if ((filp->f_flags & O_NONBLOCK)) {
6691 * We block until we read something and tracing is disabled.
6692 * We still block if tracing is disabled, but we have never
6693 * read anything. This allows a user to cat this file, and
6694 * then enable tracing. But after we have read something,
6695 * we give an EOF when tracing is again disabled.
6697 * iter->pos will be 0 if we haven't read anything.
6699 if (!tracer_tracing_is_on(iter->tr) && iter->pos)
6702 mutex_unlock(&iter->mutex);
6704 ret = wait_on_pipe(iter, 0);
6706 mutex_lock(&iter->mutex);
6715 static bool update_last_data_if_empty(struct trace_array *tr)
6717 if (!(tr->flags & TRACE_ARRAY_FL_LAST_BOOT))
6720 if (!ring_buffer_empty(tr->array_buffer.buffer))
6724 * If the buffer contains the last boot data and all per-cpu
6725 * buffers are empty, reset it from the kernel side.
6727 update_last_data(tr);
6735 tracing_read_pipe(struct file *filp, char __user *ubuf,
6736 size_t cnt, loff_t *ppos)
6738 struct trace_iterator *iter = filp->private_data;
6742 * Avoid more than one consumer on a single file descriptor
6743 * This is just a matter of traces coherency, the ring buffer itself
6746 guard(mutex)(&iter->mutex);
6748 /* return any leftover data */
6749 sret = trace_seq_to_user(&iter->seq, ubuf, cnt);
6753 trace_seq_init(&iter->seq);
6755 if (iter->trace->read) {
6756 sret = iter->trace->read(iter, filp, ubuf, cnt, ppos);
6762 if (update_last_data_if_empty(iter->tr))
6765 sret = tracing_wait_pipe(filp);
6769 /* stop when tracing is finished */
6770 if (trace_empty(iter))
6773 if (cnt >= TRACE_SEQ_BUFFER_SIZE)
6774 cnt = TRACE_SEQ_BUFFER_SIZE - 1;
6776 /* reset all but tr, trace, and overruns */
6777 trace_iterator_reset(iter);
6778 cpumask_clear(iter->started);
6779 trace_seq_init(&iter->seq);
6781 trace_event_read_lock();
6782 trace_access_lock(iter->cpu_file);
6783 while (trace_find_next_entry_inc(iter) != NULL) {
6784 enum print_line_t ret;
6785 int save_len = iter->seq.seq.len;
6787 ret = print_trace_line(iter);
6788 if (ret == TRACE_TYPE_PARTIAL_LINE) {
6790 * If one print_trace_line() fills entire trace_seq in one shot,
6791 * trace_seq_to_user() will returns -EBUSY because save_len == 0,
6792 * In this case, we need to consume it, otherwise, loop will peek
6793 * this event next time, resulting in an infinite loop.
6795 if (save_len == 0) {
6797 trace_seq_puts(&iter->seq, "[LINE TOO BIG]\n");
6798 trace_consume(iter);
6802 /* In other cases, don't print partial lines */
6803 iter->seq.seq.len = save_len;
6806 if (ret != TRACE_TYPE_NO_CONSUME)
6807 trace_consume(iter);
6809 if (trace_seq_used(&iter->seq) >= cnt)
6813 * Setting the full flag means we reached the trace_seq buffer
6814 * size and we should leave by partial output condition above.
6815 * One of the trace_seq_* functions is not used properly.
6817 WARN_ONCE(iter->seq.full, "full flag set for trace type %d",
6820 trace_access_unlock(iter->cpu_file);
6821 trace_event_read_unlock();
6823 /* Now copy what we have to the user */
6824 sret = trace_seq_to_user(&iter->seq, ubuf, cnt);
6825 if (iter->seq.readpos >= trace_seq_used(&iter->seq))
6826 trace_seq_init(&iter->seq);
6829 * If there was nothing to send to user, in spite of consuming trace
6830 * entries, go back to wait for more entries.
6838 static void tracing_spd_release_pipe(struct splice_pipe_desc *spd,
6841 __free_page(spd->pages[idx]);
6845 tracing_fill_pipe_page(size_t rem, struct trace_iterator *iter)
6851 /* Seq buffer is page-sized, exactly what we need. */
6853 save_len = iter->seq.seq.len;
6854 ret = print_trace_line(iter);
6856 if (trace_seq_has_overflowed(&iter->seq)) {
6857 iter->seq.seq.len = save_len;
6862 * This should not be hit, because it should only
6863 * be set if the iter->seq overflowed. But check it
6864 * anyway to be safe.
6866 if (ret == TRACE_TYPE_PARTIAL_LINE) {
6867 iter->seq.seq.len = save_len;
6871 count = trace_seq_used(&iter->seq) - save_len;
6874 iter->seq.seq.len = save_len;
6878 if (ret != TRACE_TYPE_NO_CONSUME)
6879 trace_consume(iter);
6881 if (!trace_find_next_entry_inc(iter)) {
6891 static ssize_t tracing_splice_read_pipe(struct file *filp,
6893 struct pipe_inode_info *pipe,
6897 struct page *pages_def[PIPE_DEF_BUFFERS];
6898 struct partial_page partial_def[PIPE_DEF_BUFFERS];
6899 struct trace_iterator *iter = filp->private_data;
6900 struct splice_pipe_desc spd = {
6902 .partial = partial_def,
6903 .nr_pages = 0, /* This gets updated below. */
6904 .nr_pages_max = PIPE_DEF_BUFFERS,
6905 .ops = &default_pipe_buf_ops,
6906 .spd_release = tracing_spd_release_pipe,
6912 if (splice_grow_spd(pipe, &spd))
6915 mutex_lock(&iter->mutex);
6917 if (iter->trace->splice_read) {
6918 ret = iter->trace->splice_read(iter, filp,
6919 ppos, pipe, len, flags);
6924 ret = tracing_wait_pipe(filp);
6928 if (!iter->ent && !trace_find_next_entry_inc(iter)) {
6933 trace_event_read_lock();
6934 trace_access_lock(iter->cpu_file);
6936 /* Fill as many pages as possible. */
6937 for (i = 0, rem = len; i < spd.nr_pages_max && rem; i++) {
6938 spd.pages[i] = alloc_page(GFP_KERNEL);
6942 rem = tracing_fill_pipe_page(rem, iter);
6944 /* Copy the data into the page, so we can start over. */
6945 ret = trace_seq_to_buffer(&iter->seq,
6946 page_address(spd.pages[i]),
6947 min((size_t)trace_seq_used(&iter->seq),
6948 (size_t)PAGE_SIZE));
6950 __free_page(spd.pages[i]);
6953 spd.partial[i].offset = 0;
6954 spd.partial[i].len = ret;
6956 trace_seq_init(&iter->seq);
6959 trace_access_unlock(iter->cpu_file);
6960 trace_event_read_unlock();
6961 mutex_unlock(&iter->mutex);
6966 ret = splice_to_pipe(pipe, &spd);
6970 splice_shrink_spd(&spd);
6974 mutex_unlock(&iter->mutex);
6979 tracing_entries_read(struct file *filp, char __user *ubuf,
6980 size_t cnt, loff_t *ppos)
6982 struct inode *inode = file_inode(filp);
6983 struct trace_array *tr = inode->i_private;
6984 int cpu = tracing_get_cpu(inode);
6989 mutex_lock(&trace_types_lock);
6991 if (cpu == RING_BUFFER_ALL_CPUS) {
6992 int cpu, buf_size_same;
6997 /* check if all cpu sizes are same */
6998 for_each_tracing_cpu(cpu) {
6999 /* fill in the size from first enabled cpu */
7001 size = per_cpu_ptr(tr->array_buffer.data, cpu)->entries;
7002 if (size != per_cpu_ptr(tr->array_buffer.data, cpu)->entries) {
7008 if (buf_size_same) {
7009 if (!tr->ring_buffer_expanded)
7010 r = sprintf(buf, "%lu (expanded: %lu)\n",
7012 trace_buf_size >> 10);
7014 r = sprintf(buf, "%lu\n", size >> 10);
7016 r = sprintf(buf, "X\n");
7018 r = sprintf(buf, "%lu\n", per_cpu_ptr(tr->array_buffer.data, cpu)->entries >> 10);
7020 mutex_unlock(&trace_types_lock);
7022 ret = simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
7027 tracing_entries_write(struct file *filp, const char __user *ubuf,
7028 size_t cnt, loff_t *ppos)
7030 struct inode *inode = file_inode(filp);
7031 struct trace_array *tr = inode->i_private;
7035 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
7039 /* must have at least 1 entry */
7043 /* value is in KB */
7045 ret = tracing_resize_ring_buffer(tr, val, tracing_get_cpu(inode));
7055 tracing_total_entries_read(struct file *filp, char __user *ubuf,
7056 size_t cnt, loff_t *ppos)
7058 struct trace_array *tr = filp->private_data;
7061 unsigned long size = 0, expanded_size = 0;
7063 mutex_lock(&trace_types_lock);
7064 for_each_tracing_cpu(cpu) {
7065 size += per_cpu_ptr(tr->array_buffer.data, cpu)->entries >> 10;
7066 if (!tr->ring_buffer_expanded)
7067 expanded_size += trace_buf_size >> 10;
7069 if (tr->ring_buffer_expanded)
7070 r = sprintf(buf, "%lu\n", size);
7072 r = sprintf(buf, "%lu (expanded: %lu)\n", size, expanded_size);
7073 mutex_unlock(&trace_types_lock);
7075 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
7078 #define LAST_BOOT_HEADER ((void *)1)
7080 static void *l_next(struct seq_file *m, void *v, loff_t *pos)
7082 struct trace_array *tr = m->private;
7083 struct trace_scratch *tscratch = tr->scratch;
7084 unsigned int index = *pos;
7089 return LAST_BOOT_HEADER;
7091 /* Only show offsets of the last boot data */
7092 if (!tscratch || !(tr->flags & TRACE_ARRAY_FL_LAST_BOOT))
7095 /* *pos 0 is for the header, 1 is for the first module */
7098 if (index >= tscratch->nr_entries)
7101 return &tscratch->entries[index];
7104 static void *l_start(struct seq_file *m, loff_t *pos)
7106 mutex_lock(&scratch_mutex);
7108 return l_next(m, NULL, pos);
7111 static void l_stop(struct seq_file *m, void *p)
7113 mutex_unlock(&scratch_mutex);
7116 static void show_last_boot_header(struct seq_file *m, struct trace_array *tr)
7118 struct trace_scratch *tscratch = tr->scratch;
7121 * Do not leak KASLR address. This only shows the KASLR address of
7122 * the last boot. When the ring buffer is started, the LAST_BOOT
7123 * flag gets cleared, and this should only report "current".
7124 * Otherwise it shows the KASLR address from the previous boot which
7125 * should not be the same as the current boot.
7127 if (tscratch && (tr->flags & TRACE_ARRAY_FL_LAST_BOOT))
7128 seq_printf(m, "%lx\t[kernel]\n", tscratch->text_addr);
7130 seq_puts(m, "# Current\n");
7133 static int l_show(struct seq_file *m, void *v)
7135 struct trace_array *tr = m->private;
7136 struct trace_mod_entry *entry = v;
7138 if (v == LAST_BOOT_HEADER) {
7139 show_last_boot_header(m, tr);
7143 seq_printf(m, "%lx\t%s\n", entry->mod_addr, entry->mod_name);
7147 static const struct seq_operations last_boot_seq_ops = {
7154 static int tracing_last_boot_open(struct inode *inode, struct file *file)
7156 struct trace_array *tr = inode->i_private;
7160 ret = tracing_check_open_get_tr(tr);
7164 ret = seq_open(file, &last_boot_seq_ops);
7166 trace_array_put(tr);
7170 m = file->private_data;
7176 static int tracing_buffer_meta_open(struct inode *inode, struct file *filp)
7178 struct trace_array *tr = inode->i_private;
7179 int cpu = tracing_get_cpu(inode);
7182 ret = tracing_check_open_get_tr(tr);
7186 ret = ring_buffer_meta_seq_init(filp, tr->array_buffer.buffer, cpu);
7188 __trace_array_put(tr);
7193 tracing_free_buffer_write(struct file *filp, const char __user *ubuf,
7194 size_t cnt, loff_t *ppos)
7197 * There is no need to read what the user has written, this function
7198 * is just to make sure that there is no error when "echo" is used
7207 tracing_free_buffer_release(struct inode *inode, struct file *filp)
7209 struct trace_array *tr = inode->i_private;
7211 /* disable tracing ? */
7212 if (tr->trace_flags & TRACE_ITER_STOP_ON_FREE)
7213 tracer_tracing_off(tr);
7214 /* resize the ring buffer to 0 */
7215 tracing_resize_ring_buffer(tr, 0, RING_BUFFER_ALL_CPUS);
7217 trace_array_put(tr);
7222 #define TRACE_MARKER_MAX_SIZE 4096
7224 static ssize_t write_marker_to_buffer(struct trace_array *tr, const char __user *ubuf,
7225 size_t cnt, unsigned long ip)
7227 struct ring_buffer_event *event;
7228 enum event_trigger_type tt = ETT_NONE;
7229 struct trace_buffer *buffer;
7230 struct print_entry *entry;
7236 /* Used in tracing_mark_raw_write() as well */
7237 #define FAULTED_STR "<faulted>"
7238 #define FAULTED_SIZE (sizeof(FAULTED_STR) - 1) /* '\0' is already accounted for */
7240 meta_size = sizeof(*entry) + 2; /* add '\0' and possible '\n' */
7242 size = cnt + meta_size;
7244 /* If less than "<faulted>", then make sure we can still add that */
7245 if (cnt < FAULTED_SIZE)
7246 size += FAULTED_SIZE - cnt;
7248 buffer = tr->array_buffer.buffer;
7249 event = __trace_buffer_lock_reserve(buffer, TRACE_PRINT, size,
7251 if (unlikely(!event)) {
7253 * If the size was greater than what was allowed, then
7254 * make it smaller and try again.
7256 if (size > ring_buffer_max_event_size(buffer)) {
7257 /* cnt < FAULTED size should never be bigger than max */
7258 if (WARN_ON_ONCE(cnt < FAULTED_SIZE))
7260 cnt = ring_buffer_max_event_size(buffer) - meta_size;
7261 /* The above should only happen once */
7262 if (WARN_ON_ONCE(cnt + meta_size == size))
7267 /* Ring buffer disabled, return as if not open for write */
7271 entry = ring_buffer_event_data(event);
7274 len = __copy_from_user_inatomic(&entry->buf, ubuf, cnt);
7276 memcpy(&entry->buf, FAULTED_STR, FAULTED_SIZE);
7282 if (tr->trace_marker_file && !list_empty(&tr->trace_marker_file->triggers)) {
7283 /* do not add \n before testing triggers, but add \0 */
7284 entry->buf[cnt] = '\0';
7285 tt = event_triggers_call(tr->trace_marker_file, buffer, entry, event);
7288 if (entry->buf[cnt - 1] != '\n') {
7289 entry->buf[cnt] = '\n';
7290 entry->buf[cnt + 1] = '\0';
7292 entry->buf[cnt] = '\0';
7294 if (static_branch_unlikely(&trace_marker_exports_enabled))
7295 ftrace_exports(event, TRACE_EXPORT_MARKER);
7296 __buffer_unlock_commit(buffer, event);
7299 event_triggers_post_call(tr->trace_marker_file, tt);
7305 tracing_mark_write(struct file *filp, const char __user *ubuf,
7306 size_t cnt, loff_t *fpos)
7308 struct trace_array *tr = filp->private_data;
7309 ssize_t written = -ENODEV;
7312 if (tracing_disabled)
7315 if (!(tr->trace_flags & TRACE_ITER_MARKERS))
7318 if ((ssize_t)cnt < 0)
7321 if (cnt > TRACE_MARKER_MAX_SIZE)
7322 cnt = TRACE_MARKER_MAX_SIZE;
7324 /* The selftests expect this function to be the IP address */
7327 /* The global trace_marker can go to multiple instances */
7328 if (tr == &global_trace) {
7330 list_for_each_entry_rcu(tr, &marker_copies, marker_list) {
7331 written = write_marker_to_buffer(tr, ubuf, cnt, ip);
7336 written = write_marker_to_buffer(tr, ubuf, cnt, ip);
7342 static ssize_t write_raw_marker_to_buffer(struct trace_array *tr,
7343 const char __user *ubuf, size_t cnt)
7345 struct ring_buffer_event *event;
7346 struct trace_buffer *buffer;
7347 struct raw_data_entry *entry;
7352 #define FAULT_SIZE_ID (FAULTED_SIZE + sizeof(int))
7354 size = sizeof(*entry) + cnt;
7355 if (cnt < FAULT_SIZE_ID)
7356 size += FAULT_SIZE_ID - cnt;
7358 buffer = tr->array_buffer.buffer;
7360 if (size > ring_buffer_max_event_size(buffer))
7363 event = __trace_buffer_lock_reserve(buffer, TRACE_RAW_DATA, size,
7366 /* Ring buffer disabled, return as if not open for write */
7369 entry = ring_buffer_event_data(event);
7371 len = __copy_from_user_inatomic(&entry->id, ubuf, cnt);
7374 memcpy(&entry->buf, FAULTED_STR, FAULTED_SIZE);
7379 __buffer_unlock_commit(buffer, event);
7385 tracing_mark_raw_write(struct file *filp, const char __user *ubuf,
7386 size_t cnt, loff_t *fpos)
7388 struct trace_array *tr = filp->private_data;
7389 ssize_t written = -ENODEV;
7391 #define FAULT_SIZE_ID (FAULTED_SIZE + sizeof(int))
7393 if (tracing_disabled)
7396 if (!(tr->trace_flags & TRACE_ITER_MARKERS))
7399 /* The marker must at least have a tag id */
7400 if (cnt < sizeof(unsigned int))
7403 /* The global trace_marker_raw can go to multiple instances */
7404 if (tr == &global_trace) {
7406 list_for_each_entry_rcu(tr, &marker_copies, marker_list) {
7407 written = write_raw_marker_to_buffer(tr, ubuf, cnt);
7412 written = write_raw_marker_to_buffer(tr, ubuf, cnt);
7418 static int tracing_clock_show(struct seq_file *m, void *v)
7420 struct trace_array *tr = m->private;
7423 for (i = 0; i < ARRAY_SIZE(trace_clocks); i++)
7425 "%s%s%s%s", i ? " " : "",
7426 i == tr->clock_id ? "[" : "", trace_clocks[i].name,
7427 i == tr->clock_id ? "]" : "");
7433 int tracing_set_clock(struct trace_array *tr, const char *clockstr)
7437 for (i = 0; i < ARRAY_SIZE(trace_clocks); i++) {
7438 if (strcmp(trace_clocks[i].name, clockstr) == 0)
7441 if (i == ARRAY_SIZE(trace_clocks))
7444 mutex_lock(&trace_types_lock);
7448 ring_buffer_set_clock(tr->array_buffer.buffer, trace_clocks[i].func);
7451 * New clock may not be consistent with the previous clock.
7452 * Reset the buffer so that it doesn't have incomparable timestamps.
7454 tracing_reset_online_cpus(&tr->array_buffer);
7456 #ifdef CONFIG_TRACER_MAX_TRACE
7457 if (tr->max_buffer.buffer)
7458 ring_buffer_set_clock(tr->max_buffer.buffer, trace_clocks[i].func);
7459 tracing_reset_online_cpus(&tr->max_buffer);
7462 if (tr->scratch && !(tr->flags & TRACE_ARRAY_FL_LAST_BOOT)) {
7463 struct trace_scratch *tscratch = tr->scratch;
7465 tscratch->clock_id = i;
7468 mutex_unlock(&trace_types_lock);
7473 static ssize_t tracing_clock_write(struct file *filp, const char __user *ubuf,
7474 size_t cnt, loff_t *fpos)
7476 struct seq_file *m = filp->private_data;
7477 struct trace_array *tr = m->private;
7479 const char *clockstr;
7482 if (cnt >= sizeof(buf))
7485 if (copy_from_user(buf, ubuf, cnt))
7490 clockstr = strstrip(buf);
7492 ret = tracing_set_clock(tr, clockstr);
7501 static int tracing_clock_open(struct inode *inode, struct file *file)
7503 struct trace_array *tr = inode->i_private;
7506 ret = tracing_check_open_get_tr(tr);
7510 ret = single_open(file, tracing_clock_show, inode->i_private);
7512 trace_array_put(tr);
7517 static int tracing_time_stamp_mode_show(struct seq_file *m, void *v)
7519 struct trace_array *tr = m->private;
7521 mutex_lock(&trace_types_lock);
7523 if (ring_buffer_time_stamp_abs(tr->array_buffer.buffer))
7524 seq_puts(m, "delta [absolute]\n");
7526 seq_puts(m, "[delta] absolute\n");
7528 mutex_unlock(&trace_types_lock);
7533 static int tracing_time_stamp_mode_open(struct inode *inode, struct file *file)
7535 struct trace_array *tr = inode->i_private;
7538 ret = tracing_check_open_get_tr(tr);
7542 ret = single_open(file, tracing_time_stamp_mode_show, inode->i_private);
7544 trace_array_put(tr);
7549 u64 tracing_event_time_stamp(struct trace_buffer *buffer, struct ring_buffer_event *rbe)
7551 if (rbe == this_cpu_read(trace_buffered_event))
7552 return ring_buffer_time_stamp(buffer);
7554 return ring_buffer_event_time_stamp(buffer, rbe);
7558 * Set or disable using the per CPU trace_buffer_event when possible.
7560 int tracing_set_filter_buffering(struct trace_array *tr, bool set)
7562 guard(mutex)(&trace_types_lock);
7564 if (set && tr->no_filter_buffering_ref++)
7568 if (WARN_ON_ONCE(!tr->no_filter_buffering_ref))
7571 --tr->no_filter_buffering_ref;
7577 struct ftrace_buffer_info {
7578 struct trace_iterator iter;
7580 unsigned int spare_cpu;
7581 unsigned int spare_size;
7585 #ifdef CONFIG_TRACER_SNAPSHOT
7586 static int tracing_snapshot_open(struct inode *inode, struct file *file)
7588 struct trace_array *tr = inode->i_private;
7589 struct trace_iterator *iter;
7593 ret = tracing_check_open_get_tr(tr);
7597 if (file->f_mode & FMODE_READ) {
7598 iter = __tracing_open(inode, file, true);
7600 ret = PTR_ERR(iter);
7602 /* Writes still need the seq_file to hold the private data */
7604 m = kzalloc(sizeof(*m), GFP_KERNEL);
7607 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
7615 iter->array_buffer = &tr->max_buffer;
7616 iter->cpu_file = tracing_get_cpu(inode);
7618 file->private_data = m;
7622 trace_array_put(tr);
7627 static void tracing_swap_cpu_buffer(void *tr)
7629 update_max_tr_single((struct trace_array *)tr, current, smp_processor_id());
7633 tracing_snapshot_write(struct file *filp, const char __user *ubuf, size_t cnt,
7636 struct seq_file *m = filp->private_data;
7637 struct trace_iterator *iter = m->private;
7638 struct trace_array *tr = iter->tr;
7642 ret = tracing_update_buffers(tr);
7646 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
7650 guard(mutex)(&trace_types_lock);
7652 if (tr->current_trace->use_max_tr)
7655 local_irq_disable();
7656 arch_spin_lock(&tr->max_lock);
7657 if (tr->cond_snapshot)
7659 arch_spin_unlock(&tr->max_lock);
7666 if (iter->cpu_file != RING_BUFFER_ALL_CPUS)
7668 if (tr->allocated_snapshot)
7672 /* Only allow per-cpu swap if the ring buffer supports it */
7673 #ifndef CONFIG_RING_BUFFER_ALLOW_SWAP
7674 if (iter->cpu_file != RING_BUFFER_ALL_CPUS)
7677 if (tr->allocated_snapshot)
7678 ret = resize_buffer_duplicate_size(&tr->max_buffer,
7679 &tr->array_buffer, iter->cpu_file);
7681 ret = tracing_arm_snapshot_locked(tr);
7685 /* Now, we're going to swap */
7686 if (iter->cpu_file == RING_BUFFER_ALL_CPUS) {
7687 local_irq_disable();
7688 update_max_tr(tr, current, smp_processor_id(), NULL);
7691 smp_call_function_single(iter->cpu_file, tracing_swap_cpu_buffer,
7694 tracing_disarm_snapshot(tr);
7697 if (tr->allocated_snapshot) {
7698 if (iter->cpu_file == RING_BUFFER_ALL_CPUS)
7699 tracing_reset_online_cpus(&tr->max_buffer);
7701 tracing_reset_cpu(&tr->max_buffer, iter->cpu_file);
7714 static int tracing_snapshot_release(struct inode *inode, struct file *file)
7716 struct seq_file *m = file->private_data;
7719 ret = tracing_release(inode, file);
7721 if (file->f_mode & FMODE_READ)
7724 /* If write only, the seq_file is just a stub */
7732 static int tracing_buffers_open(struct inode *inode, struct file *filp);
7733 static ssize_t tracing_buffers_read(struct file *filp, char __user *ubuf,
7734 size_t count, loff_t *ppos);
7735 static int tracing_buffers_release(struct inode *inode, struct file *file);
7736 static ssize_t tracing_buffers_splice_read(struct file *file, loff_t *ppos,
7737 struct pipe_inode_info *pipe, size_t len, unsigned int flags);
7739 static int snapshot_raw_open(struct inode *inode, struct file *filp)
7741 struct ftrace_buffer_info *info;
7744 /* The following checks for tracefs lockdown */
7745 ret = tracing_buffers_open(inode, filp);
7749 info = filp->private_data;
7751 if (info->iter.trace->use_max_tr) {
7752 tracing_buffers_release(inode, filp);
7756 info->iter.snapshot = true;
7757 info->iter.array_buffer = &info->iter.tr->max_buffer;
7762 #endif /* CONFIG_TRACER_SNAPSHOT */
7765 static const struct file_operations tracing_thresh_fops = {
7766 .open = tracing_open_generic,
7767 .read = tracing_thresh_read,
7768 .write = tracing_thresh_write,
7769 .llseek = generic_file_llseek,
7772 #ifdef CONFIG_TRACER_MAX_TRACE
7773 static const struct file_operations tracing_max_lat_fops = {
7774 .open = tracing_open_generic_tr,
7775 .read = tracing_max_lat_read,
7776 .write = tracing_max_lat_write,
7777 .llseek = generic_file_llseek,
7778 .release = tracing_release_generic_tr,
7782 static const struct file_operations set_tracer_fops = {
7783 .open = tracing_open_generic_tr,
7784 .read = tracing_set_trace_read,
7785 .write = tracing_set_trace_write,
7786 .llseek = generic_file_llseek,
7787 .release = tracing_release_generic_tr,
7790 static const struct file_operations tracing_pipe_fops = {
7791 .open = tracing_open_pipe,
7792 .poll = tracing_poll_pipe,
7793 .read = tracing_read_pipe,
7794 .splice_read = tracing_splice_read_pipe,
7795 .release = tracing_release_pipe,
7798 static const struct file_operations tracing_entries_fops = {
7799 .open = tracing_open_generic_tr,
7800 .read = tracing_entries_read,
7801 .write = tracing_entries_write,
7802 .llseek = generic_file_llseek,
7803 .release = tracing_release_generic_tr,
7806 static const struct file_operations tracing_buffer_meta_fops = {
7807 .open = tracing_buffer_meta_open,
7809 .llseek = seq_lseek,
7810 .release = tracing_seq_release,
7813 static const struct file_operations tracing_total_entries_fops = {
7814 .open = tracing_open_generic_tr,
7815 .read = tracing_total_entries_read,
7816 .llseek = generic_file_llseek,
7817 .release = tracing_release_generic_tr,
7820 static const struct file_operations tracing_free_buffer_fops = {
7821 .open = tracing_open_generic_tr,
7822 .write = tracing_free_buffer_write,
7823 .release = tracing_free_buffer_release,
7826 static const struct file_operations tracing_mark_fops = {
7827 .open = tracing_mark_open,
7828 .write = tracing_mark_write,
7829 .release = tracing_release_generic_tr,
7832 static const struct file_operations tracing_mark_raw_fops = {
7833 .open = tracing_mark_open,
7834 .write = tracing_mark_raw_write,
7835 .release = tracing_release_generic_tr,
7838 static const struct file_operations trace_clock_fops = {
7839 .open = tracing_clock_open,
7841 .llseek = seq_lseek,
7842 .release = tracing_single_release_tr,
7843 .write = tracing_clock_write,
7846 static const struct file_operations trace_time_stamp_mode_fops = {
7847 .open = tracing_time_stamp_mode_open,
7849 .llseek = seq_lseek,
7850 .release = tracing_single_release_tr,
7853 static const struct file_operations last_boot_fops = {
7854 .open = tracing_last_boot_open,
7856 .llseek = seq_lseek,
7857 .release = tracing_seq_release,
7860 #ifdef CONFIG_TRACER_SNAPSHOT
7861 static const struct file_operations snapshot_fops = {
7862 .open = tracing_snapshot_open,
7864 .write = tracing_snapshot_write,
7865 .llseek = tracing_lseek,
7866 .release = tracing_snapshot_release,
7869 static const struct file_operations snapshot_raw_fops = {
7870 .open = snapshot_raw_open,
7871 .read = tracing_buffers_read,
7872 .release = tracing_buffers_release,
7873 .splice_read = tracing_buffers_splice_read,
7876 #endif /* CONFIG_TRACER_SNAPSHOT */
7879 * trace_min_max_write - Write a u64 value to a trace_min_max_param struct
7880 * @filp: The active open file structure
7881 * @ubuf: The userspace provided buffer to read value into
7882 * @cnt: The maximum number of bytes to read
7883 * @ppos: The current "file" position
7885 * This function implements the write interface for a struct trace_min_max_param.
7886 * The filp->private_data must point to a trace_min_max_param structure that
7887 * defines where to write the value, the min and the max acceptable values,
7888 * and a lock to protect the write.
7891 trace_min_max_write(struct file *filp, const char __user *ubuf, size_t cnt, loff_t *ppos)
7893 struct trace_min_max_param *param = filp->private_data;
7900 err = kstrtoull_from_user(ubuf, cnt, 10, &val);
7905 mutex_lock(param->lock);
7907 if (param->min && val < *param->min)
7910 if (param->max && val > *param->max)
7917 mutex_unlock(param->lock);
7926 * trace_min_max_read - Read a u64 value from a trace_min_max_param struct
7927 * @filp: The active open file structure
7928 * @ubuf: The userspace provided buffer to read value into
7929 * @cnt: The maximum number of bytes to read
7930 * @ppos: The current "file" position
7932 * This function implements the read interface for a struct trace_min_max_param.
7933 * The filp->private_data must point to a trace_min_max_param struct with valid
7937 trace_min_max_read(struct file *filp, char __user *ubuf, size_t cnt, loff_t *ppos)
7939 struct trace_min_max_param *param = filp->private_data;
7940 char buf[U64_STR_SIZE];
7949 if (cnt > sizeof(buf))
7952 len = snprintf(buf, sizeof(buf), "%llu\n", val);
7954 return simple_read_from_buffer(ubuf, cnt, ppos, buf, len);
7957 const struct file_operations trace_min_max_fops = {
7958 .open = tracing_open_generic,
7959 .read = trace_min_max_read,
7960 .write = trace_min_max_write,
7963 #define TRACING_LOG_ERRS_MAX 8
7964 #define TRACING_LOG_LOC_MAX 128
7966 #define CMD_PREFIX " Command: "
7969 const char **errs; /* ptr to loc-specific array of err strings */
7970 u8 type; /* index into errs -> specific err string */
7971 u16 pos; /* caret position */
7975 struct tracing_log_err {
7976 struct list_head list;
7977 struct err_info info;
7978 char loc[TRACING_LOG_LOC_MAX]; /* err location */
7979 char *cmd; /* what caused err */
7982 static DEFINE_MUTEX(tracing_err_log_lock);
7984 static struct tracing_log_err *alloc_tracing_log_err(int len)
7986 struct tracing_log_err *err;
7988 err = kzalloc(sizeof(*err), GFP_KERNEL);
7990 return ERR_PTR(-ENOMEM);
7992 err->cmd = kzalloc(len, GFP_KERNEL);
7995 return ERR_PTR(-ENOMEM);
8001 static void free_tracing_log_err(struct tracing_log_err *err)
8007 static struct tracing_log_err *get_tracing_log_err(struct trace_array *tr,
8010 struct tracing_log_err *err;
8013 if (tr->n_err_log_entries < TRACING_LOG_ERRS_MAX) {
8014 err = alloc_tracing_log_err(len);
8015 if (PTR_ERR(err) != -ENOMEM)
8016 tr->n_err_log_entries++;
8020 cmd = kzalloc(len, GFP_KERNEL);
8022 return ERR_PTR(-ENOMEM);
8023 err = list_first_entry(&tr->err_log, struct tracing_log_err, list);
8026 list_del(&err->list);
8032 * err_pos - find the position of a string within a command for error careting
8033 * @cmd: The tracing command that caused the error
8034 * @str: The string to position the caret at within @cmd
8036 * Finds the position of the first occurrence of @str within @cmd. The
8037 * return value can be passed to tracing_log_err() for caret placement
8040 * Returns the index within @cmd of the first occurrence of @str or 0
8041 * if @str was not found.
8043 unsigned int err_pos(char *cmd, const char *str)
8047 if (WARN_ON(!strlen(cmd)))
8050 found = strstr(cmd, str);
8058 * tracing_log_err - write an error to the tracing error log
8059 * @tr: The associated trace array for the error (NULL for top level array)
8060 * @loc: A string describing where the error occurred
8061 * @cmd: The tracing command that caused the error
8062 * @errs: The array of loc-specific static error strings
8063 * @type: The index into errs[], which produces the specific static err string
8064 * @pos: The position the caret should be placed in the cmd
8066 * Writes an error into tracing/error_log of the form:
8068 * <loc>: error: <text>
8072 * tracing/error_log is a small log file containing the last
8073 * TRACING_LOG_ERRS_MAX errors (8). Memory for errors isn't allocated
8074 * unless there has been a tracing error, and the error log can be
8075 * cleared and have its memory freed by writing the empty string in
8076 * truncation mode to it i.e. echo > tracing/error_log.
8078 * NOTE: the @errs array along with the @type param are used to
8079 * produce a static error string - this string is not copied and saved
8080 * when the error is logged - only a pointer to it is saved. See
8081 * existing callers for examples of how static strings are typically
8082 * defined for use with tracing_log_err().
8084 void tracing_log_err(struct trace_array *tr,
8085 const char *loc, const char *cmd,
8086 const char **errs, u8 type, u16 pos)
8088 struct tracing_log_err *err;
8094 len += sizeof(CMD_PREFIX) + 2 * sizeof("\n") + strlen(cmd) + 1;
8096 guard(mutex)(&tracing_err_log_lock);
8098 err = get_tracing_log_err(tr, len);
8099 if (PTR_ERR(err) == -ENOMEM)
8102 snprintf(err->loc, TRACING_LOG_LOC_MAX, "%s: error: ", loc);
8103 snprintf(err->cmd, len, "\n" CMD_PREFIX "%s\n", cmd);
8105 err->info.errs = errs;
8106 err->info.type = type;
8107 err->info.pos = pos;
8108 err->info.ts = local_clock();
8110 list_add_tail(&err->list, &tr->err_log);
8113 static void clear_tracing_err_log(struct trace_array *tr)
8115 struct tracing_log_err *err, *next;
8117 mutex_lock(&tracing_err_log_lock);
8118 list_for_each_entry_safe(err, next, &tr->err_log, list) {
8119 list_del(&err->list);
8120 free_tracing_log_err(err);
8123 tr->n_err_log_entries = 0;
8124 mutex_unlock(&tracing_err_log_lock);
8127 static void *tracing_err_log_seq_start(struct seq_file *m, loff_t *pos)
8129 struct trace_array *tr = m->private;
8131 mutex_lock(&tracing_err_log_lock);
8133 return seq_list_start(&tr->err_log, *pos);
8136 static void *tracing_err_log_seq_next(struct seq_file *m, void *v, loff_t *pos)
8138 struct trace_array *tr = m->private;
8140 return seq_list_next(v, &tr->err_log, pos);
8143 static void tracing_err_log_seq_stop(struct seq_file *m, void *v)
8145 mutex_unlock(&tracing_err_log_lock);
8148 static void tracing_err_log_show_pos(struct seq_file *m, u16 pos)
8152 for (i = 0; i < sizeof(CMD_PREFIX) - 1; i++)
8154 for (i = 0; i < pos; i++)
8159 static int tracing_err_log_seq_show(struct seq_file *m, void *v)
8161 struct tracing_log_err *err = v;
8164 const char *err_text = err->info.errs[err->info.type];
8165 u64 sec = err->info.ts;
8168 nsec = do_div(sec, NSEC_PER_SEC);
8169 seq_printf(m, "[%5llu.%06u] %s%s", sec, nsec / 1000,
8170 err->loc, err_text);
8171 seq_printf(m, "%s", err->cmd);
8172 tracing_err_log_show_pos(m, err->info.pos);
8178 static const struct seq_operations tracing_err_log_seq_ops = {
8179 .start = tracing_err_log_seq_start,
8180 .next = tracing_err_log_seq_next,
8181 .stop = tracing_err_log_seq_stop,
8182 .show = tracing_err_log_seq_show
8185 static int tracing_err_log_open(struct inode *inode, struct file *file)
8187 struct trace_array *tr = inode->i_private;
8190 ret = tracing_check_open_get_tr(tr);
8194 /* If this file was opened for write, then erase contents */
8195 if ((file->f_mode & FMODE_WRITE) && (file->f_flags & O_TRUNC))
8196 clear_tracing_err_log(tr);
8198 if (file->f_mode & FMODE_READ) {
8199 ret = seq_open(file, &tracing_err_log_seq_ops);
8201 struct seq_file *m = file->private_data;
8204 trace_array_put(tr);
8210 static ssize_t tracing_err_log_write(struct file *file,
8211 const char __user *buffer,
8212 size_t count, loff_t *ppos)
8217 static int tracing_err_log_release(struct inode *inode, struct file *file)
8219 struct trace_array *tr = inode->i_private;
8221 trace_array_put(tr);
8223 if (file->f_mode & FMODE_READ)
8224 seq_release(inode, file);
8229 static const struct file_operations tracing_err_log_fops = {
8230 .open = tracing_err_log_open,
8231 .write = tracing_err_log_write,
8233 .llseek = tracing_lseek,
8234 .release = tracing_err_log_release,
8237 static int tracing_buffers_open(struct inode *inode, struct file *filp)
8239 struct trace_array *tr = inode->i_private;
8240 struct ftrace_buffer_info *info;
8243 ret = tracing_check_open_get_tr(tr);
8247 info = kvzalloc(sizeof(*info), GFP_KERNEL);
8249 trace_array_put(tr);
8253 mutex_lock(&trace_types_lock);
8256 info->iter.cpu_file = tracing_get_cpu(inode);
8257 info->iter.trace = tr->current_trace;
8258 info->iter.array_buffer = &tr->array_buffer;
8260 /* Force reading ring buffer for first read */
8261 info->read = (unsigned int)-1;
8263 filp->private_data = info;
8267 mutex_unlock(&trace_types_lock);
8269 ret = nonseekable_open(inode, filp);
8271 trace_array_put(tr);
8277 tracing_buffers_poll(struct file *filp, poll_table *poll_table)
8279 struct ftrace_buffer_info *info = filp->private_data;
8280 struct trace_iterator *iter = &info->iter;
8282 return trace_poll(iter, filp, poll_table);
8286 tracing_buffers_read(struct file *filp, char __user *ubuf,
8287 size_t count, loff_t *ppos)
8289 struct ftrace_buffer_info *info = filp->private_data;
8290 struct trace_iterator *iter = &info->iter;
8299 #ifdef CONFIG_TRACER_MAX_TRACE
8300 if (iter->snapshot && iter->tr->current_trace->use_max_tr)
8304 page_size = ring_buffer_subbuf_size_get(iter->array_buffer->buffer);
8306 /* Make sure the spare matches the current sub buffer size */
8308 if (page_size != info->spare_size) {
8309 ring_buffer_free_read_page(iter->array_buffer->buffer,
8310 info->spare_cpu, info->spare);
8316 info->spare = ring_buffer_alloc_read_page(iter->array_buffer->buffer,
8318 if (IS_ERR(info->spare)) {
8319 ret = PTR_ERR(info->spare);
8322 info->spare_cpu = iter->cpu_file;
8323 info->spare_size = page_size;
8329 /* Do we have previous read data to read? */
8330 if (info->read < page_size)
8334 trace_access_lock(iter->cpu_file);
8335 ret = ring_buffer_read_page(iter->array_buffer->buffer,
8339 trace_access_unlock(iter->cpu_file);
8342 if (trace_empty(iter) && !iter->closed) {
8343 if (update_last_data_if_empty(iter->tr))
8346 if ((filp->f_flags & O_NONBLOCK))
8349 ret = wait_on_pipe(iter, 0);
8360 size = page_size - info->read;
8363 trace_data = ring_buffer_read_page_data(info->spare);
8364 ret = copy_to_user(ubuf, trace_data + info->read, size);
8376 static int tracing_buffers_flush(struct file *file, fl_owner_t id)
8378 struct ftrace_buffer_info *info = file->private_data;
8379 struct trace_iterator *iter = &info->iter;
8381 iter->closed = true;
8382 /* Make sure the waiters see the new wait_index */
8383 (void)atomic_fetch_inc_release(&iter->wait_index);
8385 ring_buffer_wake_waiters(iter->array_buffer->buffer, iter->cpu_file);
8390 static int tracing_buffers_release(struct inode *inode, struct file *file)
8392 struct ftrace_buffer_info *info = file->private_data;
8393 struct trace_iterator *iter = &info->iter;
8395 mutex_lock(&trace_types_lock);
8397 iter->tr->trace_ref--;
8399 __trace_array_put(iter->tr);
8402 ring_buffer_free_read_page(iter->array_buffer->buffer,
8403 info->spare_cpu, info->spare);
8406 mutex_unlock(&trace_types_lock);
8412 struct trace_buffer *buffer;
8415 refcount_t refcount;
8418 static void buffer_ref_release(struct buffer_ref *ref)
8420 if (!refcount_dec_and_test(&ref->refcount))
8422 ring_buffer_free_read_page(ref->buffer, ref->cpu, ref->page);
8426 static void buffer_pipe_buf_release(struct pipe_inode_info *pipe,
8427 struct pipe_buffer *buf)
8429 struct buffer_ref *ref = (struct buffer_ref *)buf->private;
8431 buffer_ref_release(ref);
8435 static bool buffer_pipe_buf_get(struct pipe_inode_info *pipe,
8436 struct pipe_buffer *buf)
8438 struct buffer_ref *ref = (struct buffer_ref *)buf->private;
8440 if (refcount_read(&ref->refcount) > INT_MAX/2)
8443 refcount_inc(&ref->refcount);
8447 /* Pipe buffer operations for a buffer. */
8448 static const struct pipe_buf_operations buffer_pipe_buf_ops = {
8449 .release = buffer_pipe_buf_release,
8450 .get = buffer_pipe_buf_get,
8454 * Callback from splice_to_pipe(), if we need to release some pages
8455 * at the end of the spd in case we error'ed out in filling the pipe.
8457 static void buffer_spd_release(struct splice_pipe_desc *spd, unsigned int i)
8459 struct buffer_ref *ref =
8460 (struct buffer_ref *)spd->partial[i].private;
8462 buffer_ref_release(ref);
8463 spd->partial[i].private = 0;
8467 tracing_buffers_splice_read(struct file *file, loff_t *ppos,
8468 struct pipe_inode_info *pipe, size_t len,
8471 struct ftrace_buffer_info *info = file->private_data;
8472 struct trace_iterator *iter = &info->iter;
8473 struct partial_page partial_def[PIPE_DEF_BUFFERS];
8474 struct page *pages_def[PIPE_DEF_BUFFERS];
8475 struct splice_pipe_desc spd = {
8477 .partial = partial_def,
8478 .nr_pages_max = PIPE_DEF_BUFFERS,
8479 .ops = &buffer_pipe_buf_ops,
8480 .spd_release = buffer_spd_release,
8482 struct buffer_ref *ref;
8488 #ifdef CONFIG_TRACER_MAX_TRACE
8489 if (iter->snapshot && iter->tr->current_trace->use_max_tr)
8493 page_size = ring_buffer_subbuf_size_get(iter->array_buffer->buffer);
8494 if (*ppos & (page_size - 1))
8497 if (len & (page_size - 1)) {
8498 if (len < page_size)
8500 len &= (~(page_size - 1));
8503 if (splice_grow_spd(pipe, &spd))
8507 trace_access_lock(iter->cpu_file);
8508 entries = ring_buffer_entries_cpu(iter->array_buffer->buffer, iter->cpu_file);
8510 for (i = 0; i < spd.nr_pages_max && len && entries; i++, len -= page_size) {
8514 ref = kzalloc(sizeof(*ref), GFP_KERNEL);
8520 refcount_set(&ref->refcount, 1);
8521 ref->buffer = iter->array_buffer->buffer;
8522 ref->page = ring_buffer_alloc_read_page(ref->buffer, iter->cpu_file);
8523 if (IS_ERR(ref->page)) {
8524 ret = PTR_ERR(ref->page);
8529 ref->cpu = iter->cpu_file;
8531 r = ring_buffer_read_page(ref->buffer, ref->page,
8532 len, iter->cpu_file, 1);
8534 ring_buffer_free_read_page(ref->buffer, ref->cpu,
8540 page = virt_to_page(ring_buffer_read_page_data(ref->page));
8542 spd.pages[i] = page;
8543 spd.partial[i].len = page_size;
8544 spd.partial[i].offset = 0;
8545 spd.partial[i].private = (unsigned long)ref;
8549 entries = ring_buffer_entries_cpu(iter->array_buffer->buffer, iter->cpu_file);
8552 trace_access_unlock(iter->cpu_file);
8555 /* did we read anything? */
8556 if (!spd.nr_pages) {
8565 if ((file->f_flags & O_NONBLOCK) || (flags & SPLICE_F_NONBLOCK))
8568 ret = wait_on_pipe(iter, iter->snapshot ? 0 : iter->tr->buffer_percent);
8572 /* No need to wait after waking up when tracing is off */
8573 if (!tracer_tracing_is_on(iter->tr))
8576 /* Iterate one more time to collect any new data then exit */
8582 ret = splice_to_pipe(pipe, &spd);
8584 splice_shrink_spd(&spd);
8589 static long tracing_buffers_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
8591 struct ftrace_buffer_info *info = file->private_data;
8592 struct trace_iterator *iter = &info->iter;
8595 if (cmd == TRACE_MMAP_IOCTL_GET_READER) {
8596 if (!(file->f_flags & O_NONBLOCK)) {
8597 err = ring_buffer_wait(iter->array_buffer->buffer,
8599 iter->tr->buffer_percent,
8605 return ring_buffer_map_get_reader(iter->array_buffer->buffer,
8612 * An ioctl call with cmd 0 to the ring buffer file will wake up all
8615 mutex_lock(&trace_types_lock);
8617 /* Make sure the waiters see the new wait_index */
8618 (void)atomic_fetch_inc_release(&iter->wait_index);
8620 ring_buffer_wake_waiters(iter->array_buffer->buffer, iter->cpu_file);
8622 mutex_unlock(&trace_types_lock);
8626 #ifdef CONFIG_TRACER_MAX_TRACE
8627 static int get_snapshot_map(struct trace_array *tr)
8632 * Called with mmap_lock held. lockdep would be unhappy if we would now
8633 * take trace_types_lock. Instead use the specific
8634 * snapshot_trigger_lock.
8636 spin_lock(&tr->snapshot_trigger_lock);
8638 if (tr->snapshot || tr->mapped == UINT_MAX)
8643 spin_unlock(&tr->snapshot_trigger_lock);
8645 /* Wait for update_max_tr() to observe iter->tr->mapped */
8646 if (tr->mapped == 1)
8652 static void put_snapshot_map(struct trace_array *tr)
8654 spin_lock(&tr->snapshot_trigger_lock);
8655 if (!WARN_ON(!tr->mapped))
8657 spin_unlock(&tr->snapshot_trigger_lock);
8660 static inline int get_snapshot_map(struct trace_array *tr) { return 0; }
8661 static inline void put_snapshot_map(struct trace_array *tr) { }
8664 static void tracing_buffers_mmap_close(struct vm_area_struct *vma)
8666 struct ftrace_buffer_info *info = vma->vm_file->private_data;
8667 struct trace_iterator *iter = &info->iter;
8669 WARN_ON(ring_buffer_unmap(iter->array_buffer->buffer, iter->cpu_file));
8670 put_snapshot_map(iter->tr);
8673 static const struct vm_operations_struct tracing_buffers_vmops = {
8674 .close = tracing_buffers_mmap_close,
8677 static int tracing_buffers_mmap(struct file *filp, struct vm_area_struct *vma)
8679 struct ftrace_buffer_info *info = filp->private_data;
8680 struct trace_iterator *iter = &info->iter;
8683 /* A memmap'ed buffer is not supported for user space mmap */
8684 if (iter->tr->flags & TRACE_ARRAY_FL_MEMMAP)
8687 ret = get_snapshot_map(iter->tr);
8691 ret = ring_buffer_map(iter->array_buffer->buffer, iter->cpu_file, vma);
8693 put_snapshot_map(iter->tr);
8695 vma->vm_ops = &tracing_buffers_vmops;
8700 static const struct file_operations tracing_buffers_fops = {
8701 .open = tracing_buffers_open,
8702 .read = tracing_buffers_read,
8703 .poll = tracing_buffers_poll,
8704 .release = tracing_buffers_release,
8705 .flush = tracing_buffers_flush,
8706 .splice_read = tracing_buffers_splice_read,
8707 .unlocked_ioctl = tracing_buffers_ioctl,
8708 .mmap = tracing_buffers_mmap,
8712 tracing_stats_read(struct file *filp, char __user *ubuf,
8713 size_t count, loff_t *ppos)
8715 struct inode *inode = file_inode(filp);
8716 struct trace_array *tr = inode->i_private;
8717 struct array_buffer *trace_buf = &tr->array_buffer;
8718 int cpu = tracing_get_cpu(inode);
8719 struct trace_seq *s;
8721 unsigned long long t;
8722 unsigned long usec_rem;
8724 s = kmalloc(sizeof(*s), GFP_KERNEL);
8730 cnt = ring_buffer_entries_cpu(trace_buf->buffer, cpu);
8731 trace_seq_printf(s, "entries: %ld\n", cnt);
8733 cnt = ring_buffer_overrun_cpu(trace_buf->buffer, cpu);
8734 trace_seq_printf(s, "overrun: %ld\n", cnt);
8736 cnt = ring_buffer_commit_overrun_cpu(trace_buf->buffer, cpu);
8737 trace_seq_printf(s, "commit overrun: %ld\n", cnt);
8739 cnt = ring_buffer_bytes_cpu(trace_buf->buffer, cpu);
8740 trace_seq_printf(s, "bytes: %ld\n", cnt);
8742 if (trace_clocks[tr->clock_id].in_ns) {
8743 /* local or global for trace_clock */
8744 t = ns2usecs(ring_buffer_oldest_event_ts(trace_buf->buffer, cpu));
8745 usec_rem = do_div(t, USEC_PER_SEC);
8746 trace_seq_printf(s, "oldest event ts: %5llu.%06lu\n",
8749 t = ns2usecs(ring_buffer_time_stamp(trace_buf->buffer));
8750 usec_rem = do_div(t, USEC_PER_SEC);
8751 trace_seq_printf(s, "now ts: %5llu.%06lu\n", t, usec_rem);
8753 /* counter or tsc mode for trace_clock */
8754 trace_seq_printf(s, "oldest event ts: %llu\n",
8755 ring_buffer_oldest_event_ts(trace_buf->buffer, cpu));
8757 trace_seq_printf(s, "now ts: %llu\n",
8758 ring_buffer_time_stamp(trace_buf->buffer));
8761 cnt = ring_buffer_dropped_events_cpu(trace_buf->buffer, cpu);
8762 trace_seq_printf(s, "dropped events: %ld\n", cnt);
8764 cnt = ring_buffer_read_events_cpu(trace_buf->buffer, cpu);
8765 trace_seq_printf(s, "read events: %ld\n", cnt);
8767 count = simple_read_from_buffer(ubuf, count, ppos,
8768 s->buffer, trace_seq_used(s));
8775 static const struct file_operations tracing_stats_fops = {
8776 .open = tracing_open_generic_tr,
8777 .read = tracing_stats_read,
8778 .llseek = generic_file_llseek,
8779 .release = tracing_release_generic_tr,
8782 #ifdef CONFIG_DYNAMIC_FTRACE
8785 tracing_read_dyn_info(struct file *filp, char __user *ubuf,
8786 size_t cnt, loff_t *ppos)
8792 /* 512 should be plenty to hold the amount needed */
8793 #define DYN_INFO_BUF_SIZE 512
8795 buf = kmalloc(DYN_INFO_BUF_SIZE, GFP_KERNEL);
8799 r = scnprintf(buf, DYN_INFO_BUF_SIZE,
8800 "%ld pages:%ld groups: %ld\n"
8801 "ftrace boot update time = %llu (ns)\n"
8802 "ftrace module total update time = %llu (ns)\n",
8803 ftrace_update_tot_cnt,
8804 ftrace_number_of_pages,
8805 ftrace_number_of_groups,
8807 ftrace_total_mod_time);
8809 ret = simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
8814 static const struct file_operations tracing_dyn_info_fops = {
8815 .open = tracing_open_generic,
8816 .read = tracing_read_dyn_info,
8817 .llseek = generic_file_llseek,
8819 #endif /* CONFIG_DYNAMIC_FTRACE */
8821 #if defined(CONFIG_TRACER_SNAPSHOT) && defined(CONFIG_DYNAMIC_FTRACE)
8823 ftrace_snapshot(unsigned long ip, unsigned long parent_ip,
8824 struct trace_array *tr, struct ftrace_probe_ops *ops,
8827 tracing_snapshot_instance(tr);
8831 ftrace_count_snapshot(unsigned long ip, unsigned long parent_ip,
8832 struct trace_array *tr, struct ftrace_probe_ops *ops,
8835 struct ftrace_func_mapper *mapper = data;
8839 count = (long *)ftrace_func_mapper_find_ip(mapper, ip);
8849 tracing_snapshot_instance(tr);
8853 ftrace_snapshot_print(struct seq_file *m, unsigned long ip,
8854 struct ftrace_probe_ops *ops, void *data)
8856 struct ftrace_func_mapper *mapper = data;
8859 seq_printf(m, "%ps:", (void *)ip);
8861 seq_puts(m, "snapshot");
8864 count = (long *)ftrace_func_mapper_find_ip(mapper, ip);
8867 seq_printf(m, ":count=%ld\n", *count);
8869 seq_puts(m, ":unlimited\n");
8875 ftrace_snapshot_init(struct ftrace_probe_ops *ops, struct trace_array *tr,
8876 unsigned long ip, void *init_data, void **data)
8878 struct ftrace_func_mapper *mapper = *data;
8881 mapper = allocate_ftrace_func_mapper();
8887 return ftrace_func_mapper_add_ip(mapper, ip, init_data);
8891 ftrace_snapshot_free(struct ftrace_probe_ops *ops, struct trace_array *tr,
8892 unsigned long ip, void *data)
8894 struct ftrace_func_mapper *mapper = data;
8899 free_ftrace_func_mapper(mapper, NULL);
8903 ftrace_func_mapper_remove_ip(mapper, ip);
8906 static struct ftrace_probe_ops snapshot_probe_ops = {
8907 .func = ftrace_snapshot,
8908 .print = ftrace_snapshot_print,
8911 static struct ftrace_probe_ops snapshot_count_probe_ops = {
8912 .func = ftrace_count_snapshot,
8913 .print = ftrace_snapshot_print,
8914 .init = ftrace_snapshot_init,
8915 .free = ftrace_snapshot_free,
8919 ftrace_trace_snapshot_callback(struct trace_array *tr, struct ftrace_hash *hash,
8920 char *glob, char *cmd, char *param, int enable)
8922 struct ftrace_probe_ops *ops;
8923 void *count = (void *)-1;
8930 /* hash funcs only work with set_ftrace_filter */
8934 ops = param ? &snapshot_count_probe_ops : &snapshot_probe_ops;
8936 if (glob[0] == '!') {
8937 ret = unregister_ftrace_function_probe_func(glob+1, tr, ops);
8939 tracing_disarm_snapshot(tr);
8947 number = strsep(¶m, ":");
8949 if (!strlen(number))
8953 * We use the callback data field (which is a pointer)
8956 ret = kstrtoul(number, 0, (unsigned long *)&count);
8961 ret = tracing_arm_snapshot(tr);
8965 ret = register_ftrace_function_probe(glob, tr, ops, count);
8967 tracing_disarm_snapshot(tr);
8969 return ret < 0 ? ret : 0;
8972 static struct ftrace_func_command ftrace_snapshot_cmd = {
8974 .func = ftrace_trace_snapshot_callback,
8977 static __init int register_snapshot_cmd(void)
8979 return register_ftrace_command(&ftrace_snapshot_cmd);
8982 static inline __init int register_snapshot_cmd(void) { return 0; }
8983 #endif /* defined(CONFIG_TRACER_SNAPSHOT) && defined(CONFIG_DYNAMIC_FTRACE) */
8985 static struct dentry *tracing_get_dentry(struct trace_array *tr)
8987 if (WARN_ON(!tr->dir))
8988 return ERR_PTR(-ENODEV);
8990 /* Top directory uses NULL as the parent */
8991 if (tr->flags & TRACE_ARRAY_FL_GLOBAL)
8994 /* All sub buffers have a descriptor */
8998 static struct dentry *tracing_dentry_percpu(struct trace_array *tr, int cpu)
9000 struct dentry *d_tracer;
9003 return tr->percpu_dir;
9005 d_tracer = tracing_get_dentry(tr);
9006 if (IS_ERR(d_tracer))
9009 tr->percpu_dir = tracefs_create_dir("per_cpu", d_tracer);
9011 MEM_FAIL(!tr->percpu_dir,
9012 "Could not create tracefs directory 'per_cpu/%d'\n", cpu);
9014 return tr->percpu_dir;
9017 static struct dentry *
9018 trace_create_cpu_file(const char *name, umode_t mode, struct dentry *parent,
9019 void *data, long cpu, const struct file_operations *fops)
9021 struct dentry *ret = trace_create_file(name, mode, parent, data, fops);
9023 if (ret) /* See tracing_get_cpu() */
9024 d_inode(ret)->i_cdev = (void *)(cpu + 1);
9029 tracing_init_tracefs_percpu(struct trace_array *tr, long cpu)
9031 struct dentry *d_percpu = tracing_dentry_percpu(tr, cpu);
9032 struct dentry *d_cpu;
9033 char cpu_dir[30]; /* 30 characters should be more than enough */
9038 snprintf(cpu_dir, 30, "cpu%ld", cpu);
9039 d_cpu = tracefs_create_dir(cpu_dir, d_percpu);
9041 pr_warn("Could not create tracefs '%s' entry\n", cpu_dir);
9045 /* per cpu trace_pipe */
9046 trace_create_cpu_file("trace_pipe", TRACE_MODE_READ, d_cpu,
9047 tr, cpu, &tracing_pipe_fops);
9050 trace_create_cpu_file("trace", TRACE_MODE_WRITE, d_cpu,
9051 tr, cpu, &tracing_fops);
9053 trace_create_cpu_file("trace_pipe_raw", TRACE_MODE_READ, d_cpu,
9054 tr, cpu, &tracing_buffers_fops);
9056 trace_create_cpu_file("stats", TRACE_MODE_READ, d_cpu,
9057 tr, cpu, &tracing_stats_fops);
9059 trace_create_cpu_file("buffer_size_kb", TRACE_MODE_READ, d_cpu,
9060 tr, cpu, &tracing_entries_fops);
9062 if (tr->range_addr_start)
9063 trace_create_cpu_file("buffer_meta", TRACE_MODE_READ, d_cpu,
9064 tr, cpu, &tracing_buffer_meta_fops);
9065 #ifdef CONFIG_TRACER_SNAPSHOT
9066 if (!tr->range_addr_start) {
9067 trace_create_cpu_file("snapshot", TRACE_MODE_WRITE, d_cpu,
9068 tr, cpu, &snapshot_fops);
9070 trace_create_cpu_file("snapshot_raw", TRACE_MODE_READ, d_cpu,
9071 tr, cpu, &snapshot_raw_fops);
9076 #ifdef CONFIG_FTRACE_SELFTEST
9077 /* Let selftest have access to static functions in this file */
9078 #include "trace_selftest.c"
9082 trace_options_read(struct file *filp, char __user *ubuf, size_t cnt,
9085 struct trace_option_dentry *topt = filp->private_data;
9088 if (topt->flags->val & topt->opt->bit)
9093 return simple_read_from_buffer(ubuf, cnt, ppos, buf, 2);
9097 trace_options_write(struct file *filp, const char __user *ubuf, size_t cnt,
9100 struct trace_option_dentry *topt = filp->private_data;
9104 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
9108 if (val != 0 && val != 1)
9111 if (!!(topt->flags->val & topt->opt->bit) != val) {
9112 mutex_lock(&trace_types_lock);
9113 ret = __set_tracer_option(topt->tr, topt->flags,
9115 mutex_unlock(&trace_types_lock);
9125 static int tracing_open_options(struct inode *inode, struct file *filp)
9127 struct trace_option_dentry *topt = inode->i_private;
9130 ret = tracing_check_open_get_tr(topt->tr);
9134 filp->private_data = inode->i_private;
9138 static int tracing_release_options(struct inode *inode, struct file *file)
9140 struct trace_option_dentry *topt = file->private_data;
9142 trace_array_put(topt->tr);
9146 static const struct file_operations trace_options_fops = {
9147 .open = tracing_open_options,
9148 .read = trace_options_read,
9149 .write = trace_options_write,
9150 .llseek = generic_file_llseek,
9151 .release = tracing_release_options,
9155 * In order to pass in both the trace_array descriptor as well as the index
9156 * to the flag that the trace option file represents, the trace_array
9157 * has a character array of trace_flags_index[], which holds the index
9158 * of the bit for the flag it represents. index[0] == 0, index[1] == 1, etc.
9159 * The address of this character array is passed to the flag option file
9160 * read/write callbacks.
9162 * In order to extract both the index and the trace_array descriptor,
9163 * get_tr_index() uses the following algorithm.
9167 * As the pointer itself contains the address of the index (remember
9170 * Then to get the trace_array descriptor, by subtracting that index
9171 * from the ptr, we get to the start of the index itself.
9173 * ptr - idx == &index[0]
9175 * Then a simple container_of() from that pointer gets us to the
9176 * trace_array descriptor.
9178 static void get_tr_index(void *data, struct trace_array **ptr,
9179 unsigned int *pindex)
9181 *pindex = *(unsigned char *)data;
9183 *ptr = container_of(data - *pindex, struct trace_array,
9188 trace_options_core_read(struct file *filp, char __user *ubuf, size_t cnt,
9191 void *tr_index = filp->private_data;
9192 struct trace_array *tr;
9196 get_tr_index(tr_index, &tr, &index);
9198 if (tr->trace_flags & (1 << index))
9203 return simple_read_from_buffer(ubuf, cnt, ppos, buf, 2);
9207 trace_options_core_write(struct file *filp, const char __user *ubuf, size_t cnt,
9210 void *tr_index = filp->private_data;
9211 struct trace_array *tr;
9216 get_tr_index(tr_index, &tr, &index);
9218 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
9222 if (val != 0 && val != 1)
9225 mutex_lock(&event_mutex);
9226 mutex_lock(&trace_types_lock);
9227 ret = set_tracer_flag(tr, 1 << index, val);
9228 mutex_unlock(&trace_types_lock);
9229 mutex_unlock(&event_mutex);
9239 static const struct file_operations trace_options_core_fops = {
9240 .open = tracing_open_generic,
9241 .read = trace_options_core_read,
9242 .write = trace_options_core_write,
9243 .llseek = generic_file_llseek,
9246 struct dentry *trace_create_file(const char *name,
9248 struct dentry *parent,
9250 const struct file_operations *fops)
9254 ret = tracefs_create_file(name, mode, parent, data, fops);
9256 pr_warn("Could not create tracefs '%s' entry\n", name);
9262 static struct dentry *trace_options_init_dentry(struct trace_array *tr)
9264 struct dentry *d_tracer;
9269 d_tracer = tracing_get_dentry(tr);
9270 if (IS_ERR(d_tracer))
9273 tr->options = tracefs_create_dir("options", d_tracer);
9275 pr_warn("Could not create tracefs directory 'options'\n");
9283 create_trace_option_file(struct trace_array *tr,
9284 struct trace_option_dentry *topt,
9285 struct tracer_flags *flags,
9286 struct tracer_opt *opt)
9288 struct dentry *t_options;
9290 t_options = trace_options_init_dentry(tr);
9294 topt->flags = flags;
9298 topt->entry = trace_create_file(opt->name, TRACE_MODE_WRITE,
9299 t_options, topt, &trace_options_fops);
9304 create_trace_option_files(struct trace_array *tr, struct tracer *tracer)
9306 struct trace_option_dentry *topts;
9307 struct trace_options *tr_topts;
9308 struct tracer_flags *flags;
9309 struct tracer_opt *opts;
9316 flags = tracer->flags;
9318 if (!flags || !flags->opts)
9322 * If this is an instance, only create flags for tracers
9323 * the instance may have.
9325 if (!trace_ok_for_array(tracer, tr))
9328 for (i = 0; i < tr->nr_topts; i++) {
9329 /* Make sure there's no duplicate flags. */
9330 if (WARN_ON_ONCE(tr->topts[i].tracer->flags == tracer->flags))
9336 for (cnt = 0; opts[cnt].name; cnt++)
9339 topts = kcalloc(cnt + 1, sizeof(*topts), GFP_KERNEL);
9343 tr_topts = krealloc(tr->topts, sizeof(*tr->topts) * (tr->nr_topts + 1),
9350 tr->topts = tr_topts;
9351 tr->topts[tr->nr_topts].tracer = tracer;
9352 tr->topts[tr->nr_topts].topts = topts;
9355 for (cnt = 0; opts[cnt].name; cnt++) {
9356 create_trace_option_file(tr, &topts[cnt], flags,
9358 MEM_FAIL(topts[cnt].entry == NULL,
9359 "Failed to create trace option: %s",
9364 static struct dentry *
9365 create_trace_option_core_file(struct trace_array *tr,
9366 const char *option, long index)
9368 struct dentry *t_options;
9370 t_options = trace_options_init_dentry(tr);
9374 return trace_create_file(option, TRACE_MODE_WRITE, t_options,
9375 (void *)&tr->trace_flags_index[index],
9376 &trace_options_core_fops);
9379 static void create_trace_options_dir(struct trace_array *tr)
9381 struct dentry *t_options;
9382 bool top_level = tr == &global_trace;
9385 t_options = trace_options_init_dentry(tr);
9389 for (i = 0; trace_options[i]; i++) {
9391 !((1 << i) & TOP_LEVEL_TRACE_FLAGS))
9392 create_trace_option_core_file(tr, trace_options[i], i);
9397 rb_simple_read(struct file *filp, char __user *ubuf,
9398 size_t cnt, loff_t *ppos)
9400 struct trace_array *tr = filp->private_data;
9404 r = tracer_tracing_is_on(tr);
9405 r = sprintf(buf, "%d\n", r);
9407 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
9411 rb_simple_write(struct file *filp, const char __user *ubuf,
9412 size_t cnt, loff_t *ppos)
9414 struct trace_array *tr = filp->private_data;
9415 struct trace_buffer *buffer = tr->array_buffer.buffer;
9419 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
9424 mutex_lock(&trace_types_lock);
9425 if (!!val == tracer_tracing_is_on(tr)) {
9426 val = 0; /* do nothing */
9428 tracer_tracing_on(tr);
9429 if (tr->current_trace->start)
9430 tr->current_trace->start(tr);
9432 tracer_tracing_off(tr);
9433 if (tr->current_trace->stop)
9434 tr->current_trace->stop(tr);
9435 /* Wake up any waiters */
9436 ring_buffer_wake_waiters(buffer, RING_BUFFER_ALL_CPUS);
9438 mutex_unlock(&trace_types_lock);
9446 static const struct file_operations rb_simple_fops = {
9447 .open = tracing_open_generic_tr,
9448 .read = rb_simple_read,
9449 .write = rb_simple_write,
9450 .release = tracing_release_generic_tr,
9451 .llseek = default_llseek,
9455 buffer_percent_read(struct file *filp, char __user *ubuf,
9456 size_t cnt, loff_t *ppos)
9458 struct trace_array *tr = filp->private_data;
9462 r = tr->buffer_percent;
9463 r = sprintf(buf, "%d\n", r);
9465 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
9469 buffer_percent_write(struct file *filp, const char __user *ubuf,
9470 size_t cnt, loff_t *ppos)
9472 struct trace_array *tr = filp->private_data;
9476 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
9483 tr->buffer_percent = val;
9490 static const struct file_operations buffer_percent_fops = {
9491 .open = tracing_open_generic_tr,
9492 .read = buffer_percent_read,
9493 .write = buffer_percent_write,
9494 .release = tracing_release_generic_tr,
9495 .llseek = default_llseek,
9499 buffer_subbuf_size_read(struct file *filp, char __user *ubuf, size_t cnt, loff_t *ppos)
9501 struct trace_array *tr = filp->private_data;
9507 order = ring_buffer_subbuf_order_get(tr->array_buffer.buffer);
9508 size = (PAGE_SIZE << order) / 1024;
9510 r = sprintf(buf, "%zd\n", size);
9512 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
9516 buffer_subbuf_size_write(struct file *filp, const char __user *ubuf,
9517 size_t cnt, loff_t *ppos)
9519 struct trace_array *tr = filp->private_data;
9526 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
9530 val *= 1024; /* value passed in is in KB */
9532 pages = DIV_ROUND_UP(val, PAGE_SIZE);
9533 order = fls(pages - 1);
9535 /* limit between 1 and 128 system pages */
9536 if (order < 0 || order > 7)
9539 /* Do not allow tracing while changing the order of the ring buffer */
9540 tracing_stop_tr(tr);
9542 old_order = ring_buffer_subbuf_order_get(tr->array_buffer.buffer);
9543 if (old_order == order)
9546 ret = ring_buffer_subbuf_order_set(tr->array_buffer.buffer, order);
9550 #ifdef CONFIG_TRACER_MAX_TRACE
9552 if (!tr->allocated_snapshot)
9555 ret = ring_buffer_subbuf_order_set(tr->max_buffer.buffer, order);
9557 /* Put back the old order */
9558 cnt = ring_buffer_subbuf_order_set(tr->array_buffer.buffer, old_order);
9559 if (WARN_ON_ONCE(cnt)) {
9561 * AARGH! We are left with different orders!
9562 * The max buffer is our "snapshot" buffer.
9563 * When a tracer needs a snapshot (one of the
9564 * latency tracers), it swaps the max buffer
9565 * with the saved snap shot. We succeeded to
9566 * update the order of the main buffer, but failed to
9567 * update the order of the max buffer. But when we tried
9568 * to reset the main buffer to the original size, we
9569 * failed there too. This is very unlikely to
9570 * happen, but if it does, warn and kill all
9573 tracing_disabled = 1;
9583 tracing_start_tr(tr);
9587 static const struct file_operations buffer_subbuf_size_fops = {
9588 .open = tracing_open_generic_tr,
9589 .read = buffer_subbuf_size_read,
9590 .write = buffer_subbuf_size_write,
9591 .release = tracing_release_generic_tr,
9592 .llseek = default_llseek,
9595 static struct dentry *trace_instance_dir;
9598 init_tracer_tracefs(struct trace_array *tr, struct dentry *d_tracer);
9600 #ifdef CONFIG_MODULES
9601 static int make_mod_delta(struct module *mod, void *data)
9603 struct trace_module_delta *module_delta;
9604 struct trace_scratch *tscratch;
9605 struct trace_mod_entry *entry;
9606 struct trace_array *tr = data;
9609 tscratch = tr->scratch;
9610 module_delta = READ_ONCE(tr->module_delta);
9611 for (i = 0; i < tscratch->nr_entries; i++) {
9612 entry = &tscratch->entries[i];
9613 if (strcmp(mod->name, entry->mod_name))
9615 if (mod->state == MODULE_STATE_GOING)
9616 module_delta->delta[i] = 0;
9618 module_delta->delta[i] = (unsigned long)mod->mem[MOD_TEXT].base
9625 static int make_mod_delta(struct module *mod, void *data)
9631 static int mod_addr_comp(const void *a, const void *b, const void *data)
9633 const struct trace_mod_entry *e1 = a;
9634 const struct trace_mod_entry *e2 = b;
9636 return e1->mod_addr > e2->mod_addr ? 1 : -1;
9639 static void setup_trace_scratch(struct trace_array *tr,
9640 struct trace_scratch *tscratch, unsigned int size)
9642 struct trace_module_delta *module_delta;
9643 struct trace_mod_entry *entry;
9649 tr->scratch = tscratch;
9650 tr->scratch_size = size;
9652 if (tscratch->text_addr)
9653 tr->text_delta = (unsigned long)_text - tscratch->text_addr;
9655 if (struct_size(tscratch, entries, tscratch->nr_entries) > size)
9658 /* Check if each module name is a valid string */
9659 for (i = 0; i < tscratch->nr_entries; i++) {
9662 entry = &tscratch->entries[i];
9664 for (n = 0; n < MODULE_NAME_LEN; n++) {
9665 if (entry->mod_name[n] == '\0')
9667 if (!isprint(entry->mod_name[n]))
9670 if (n == MODULE_NAME_LEN)
9674 /* Sort the entries so that we can find appropriate module from address. */
9675 nr_entries = tscratch->nr_entries;
9676 sort_r(tscratch->entries, nr_entries, sizeof(struct trace_mod_entry),
9677 mod_addr_comp, NULL, NULL);
9679 if (IS_ENABLED(CONFIG_MODULES)) {
9680 module_delta = kzalloc(struct_size(module_delta, delta, nr_entries), GFP_KERNEL);
9681 if (!module_delta) {
9682 pr_info("module_delta allocation failed. Not able to decode module address.");
9685 init_rcu_head(&module_delta->rcu);
9687 module_delta = NULL;
9688 WRITE_ONCE(tr->module_delta, module_delta);
9690 /* Scan modules to make text delta for modules. */
9691 module_for_each_mod(make_mod_delta, tr);
9693 /* Set trace_clock as the same of the previous boot. */
9694 if (tscratch->clock_id != tr->clock_id) {
9695 if (tscratch->clock_id >= ARRAY_SIZE(trace_clocks) ||
9696 tracing_set_clock(tr, trace_clocks[tscratch->clock_id].name) < 0) {
9697 pr_info("the previous trace_clock info is not valid.");
9703 /* Invalid trace modules */
9704 memset(tscratch, 0, size);
9708 allocate_trace_buffer(struct trace_array *tr, struct array_buffer *buf, int size)
9710 enum ring_buffer_flags rb_flags;
9711 struct trace_scratch *tscratch;
9712 unsigned int scratch_size = 0;
9714 rb_flags = tr->trace_flags & TRACE_ITER_OVERWRITE ? RB_FL_OVERWRITE : 0;
9718 if (tr->range_addr_start && tr->range_addr_size) {
9719 /* Add scratch buffer to handle 128 modules */
9720 buf->buffer = ring_buffer_alloc_range(size, rb_flags, 0,
9721 tr->range_addr_start,
9722 tr->range_addr_size,
9723 struct_size(tscratch, entries, 128));
9725 tscratch = ring_buffer_meta_scratch(buf->buffer, &scratch_size);
9726 setup_trace_scratch(tr, tscratch, scratch_size);
9729 * This is basically the same as a mapped buffer,
9730 * with the same restrictions.
9734 buf->buffer = ring_buffer_alloc(size, rb_flags);
9739 buf->data = alloc_percpu(struct trace_array_cpu);
9741 ring_buffer_free(buf->buffer);
9746 /* Allocate the first page for all buffers */
9747 set_buffer_entries(&tr->array_buffer,
9748 ring_buffer_size(tr->array_buffer.buffer, 0));
9753 static void free_trace_buffer(struct array_buffer *buf)
9756 ring_buffer_free(buf->buffer);
9758 free_percpu(buf->data);
9763 static int allocate_trace_buffers(struct trace_array *tr, int size)
9767 ret = allocate_trace_buffer(tr, &tr->array_buffer, size);
9771 #ifdef CONFIG_TRACER_MAX_TRACE
9772 /* Fix mapped buffer trace arrays do not have snapshot buffers */
9773 if (tr->range_addr_start)
9776 ret = allocate_trace_buffer(tr, &tr->max_buffer,
9777 allocate_snapshot ? size : 1);
9778 if (MEM_FAIL(ret, "Failed to allocate trace buffer\n")) {
9779 free_trace_buffer(&tr->array_buffer);
9782 tr->allocated_snapshot = allocate_snapshot;
9784 allocate_snapshot = false;
9790 static void free_trace_buffers(struct trace_array *tr)
9795 free_trace_buffer(&tr->array_buffer);
9796 kfree(tr->module_delta);
9798 #ifdef CONFIG_TRACER_MAX_TRACE
9799 free_trace_buffer(&tr->max_buffer);
9803 static void init_trace_flags_index(struct trace_array *tr)
9807 /* Used by the trace options files */
9808 for (i = 0; i < TRACE_FLAGS_MAX_SIZE; i++)
9809 tr->trace_flags_index[i] = i;
9812 static void __update_tracer_options(struct trace_array *tr)
9816 for (t = trace_types; t; t = t->next)
9817 add_tracer_options(tr, t);
9820 static void update_tracer_options(struct trace_array *tr)
9822 mutex_lock(&trace_types_lock);
9823 tracer_options_updated = true;
9824 __update_tracer_options(tr);
9825 mutex_unlock(&trace_types_lock);
9828 /* Must have trace_types_lock held */
9829 struct trace_array *trace_array_find(const char *instance)
9831 struct trace_array *tr, *found = NULL;
9833 list_for_each_entry(tr, &ftrace_trace_arrays, list) {
9834 if (tr->name && strcmp(tr->name, instance) == 0) {
9843 struct trace_array *trace_array_find_get(const char *instance)
9845 struct trace_array *tr;
9847 mutex_lock(&trace_types_lock);
9848 tr = trace_array_find(instance);
9851 mutex_unlock(&trace_types_lock);
9856 static int trace_array_create_dir(struct trace_array *tr)
9860 tr->dir = tracefs_create_dir(tr->name, trace_instance_dir);
9864 ret = event_trace_add_tracer(tr->dir, tr);
9866 tracefs_remove(tr->dir);
9870 init_tracer_tracefs(tr, tr->dir);
9871 __update_tracer_options(tr);
9876 static struct trace_array *
9877 trace_array_create_systems(const char *name, const char *systems,
9878 unsigned long range_addr_start,
9879 unsigned long range_addr_size)
9881 struct trace_array *tr;
9885 tr = kzalloc(sizeof(*tr), GFP_KERNEL);
9887 return ERR_PTR(ret);
9889 tr->name = kstrdup(name, GFP_KERNEL);
9893 if (!alloc_cpumask_var(&tr->tracing_cpumask, GFP_KERNEL))
9896 if (!zalloc_cpumask_var(&tr->pipe_cpumask, GFP_KERNEL))
9900 tr->system_names = kstrdup_const(systems, GFP_KERNEL);
9901 if (!tr->system_names)
9905 /* Only for boot up memory mapped ring buffers */
9906 tr->range_addr_start = range_addr_start;
9907 tr->range_addr_size = range_addr_size;
9909 tr->trace_flags = global_trace.trace_flags & ~ZEROED_TRACE_FLAGS;
9911 cpumask_copy(tr->tracing_cpumask, cpu_all_mask);
9913 raw_spin_lock_init(&tr->start_lock);
9915 tr->max_lock = (arch_spinlock_t)__ARCH_SPIN_LOCK_UNLOCKED;
9916 #ifdef CONFIG_TRACER_MAX_TRACE
9917 spin_lock_init(&tr->snapshot_trigger_lock);
9919 tr->current_trace = &nop_trace;
9921 INIT_LIST_HEAD(&tr->systems);
9922 INIT_LIST_HEAD(&tr->events);
9923 INIT_LIST_HEAD(&tr->hist_vars);
9924 INIT_LIST_HEAD(&tr->err_log);
9925 INIT_LIST_HEAD(&tr->marker_list);
9927 #ifdef CONFIG_MODULES
9928 INIT_LIST_HEAD(&tr->mod_events);
9931 if (allocate_trace_buffers(tr, trace_buf_size) < 0)
9934 /* The ring buffer is defaultly expanded */
9935 trace_set_ring_buffer_expanded(tr);
9937 if (ftrace_allocate_ftrace_ops(tr) < 0)
9940 ftrace_init_trace_array(tr);
9942 init_trace_flags_index(tr);
9944 if (trace_instance_dir) {
9945 ret = trace_array_create_dir(tr);
9949 __trace_early_add_events(tr);
9951 list_add(&tr->list, &ftrace_trace_arrays);
9958 ftrace_free_ftrace_ops(tr);
9959 free_trace_buffers(tr);
9960 free_cpumask_var(tr->pipe_cpumask);
9961 free_cpumask_var(tr->tracing_cpumask);
9962 kfree_const(tr->system_names);
9963 kfree(tr->range_name);
9967 return ERR_PTR(ret);
9970 static struct trace_array *trace_array_create(const char *name)
9972 return trace_array_create_systems(name, NULL, 0, 0);
9975 static int instance_mkdir(const char *name)
9977 struct trace_array *tr;
9980 guard(mutex)(&event_mutex);
9981 guard(mutex)(&trace_types_lock);
9984 if (trace_array_find(name))
9987 tr = trace_array_create(name);
9989 ret = PTR_ERR_OR_ZERO(tr);
9995 static u64 map_pages(unsigned long start, unsigned long size)
9997 unsigned long vmap_start, vmap_end;
9998 struct vm_struct *area;
10001 area = get_vm_area(size, VM_IOREMAP);
10005 vmap_start = (unsigned long) area->addr;
10006 vmap_end = vmap_start + size;
10008 ret = vmap_page_range(vmap_start, vmap_end,
10009 start, pgprot_nx(PAGE_KERNEL));
10011 free_vm_area(area);
10015 return (u64)vmap_start;
10018 static inline u64 map_pages(unsigned long start, unsigned long size)
10025 * trace_array_get_by_name - Create/Lookup a trace array, given its name.
10026 * @name: The name of the trace array to be looked up/created.
10027 * @systems: A list of systems to create event directories for (NULL for all)
10029 * Returns pointer to trace array with given name.
10030 * NULL, if it cannot be created.
10032 * NOTE: This function increments the reference counter associated with the
10033 * trace array returned. This makes sure it cannot be freed while in use.
10034 * Use trace_array_put() once the trace array is no longer needed.
10035 * If the trace_array is to be freed, trace_array_destroy() needs to
10036 * be called after the trace_array_put(), or simply let user space delete
10037 * it from the tracefs instances directory. But until the
10038 * trace_array_put() is called, user space can not delete it.
10041 struct trace_array *trace_array_get_by_name(const char *name, const char *systems)
10043 struct trace_array *tr;
10045 guard(mutex)(&event_mutex);
10046 guard(mutex)(&trace_types_lock);
10048 list_for_each_entry(tr, &ftrace_trace_arrays, list) {
10049 if (tr->name && strcmp(tr->name, name) == 0) {
10055 tr = trace_array_create_systems(name, systems, 0, 0);
10064 EXPORT_SYMBOL_GPL(trace_array_get_by_name);
10066 static int __remove_instance(struct trace_array *tr)
10070 /* Reference counter for a newly created trace array = 1. */
10071 if (tr->ref > 1 || (tr->current_trace && tr->trace_ref))
10074 list_del(&tr->list);
10076 /* Disable all the flags that were enabled coming in */
10077 for (i = 0; i < TRACE_FLAGS_MAX_SIZE; i++) {
10078 if ((1 << i) & ZEROED_TRACE_FLAGS)
10079 set_tracer_flag(tr, 1 << i, 0);
10082 if (printk_trace == tr)
10083 update_printk_trace(&global_trace);
10085 if (update_marker_trace(tr, 0))
10088 tracing_set_nop(tr);
10089 clear_ftrace_function_probes(tr);
10090 event_trace_del_tracer(tr);
10091 ftrace_clear_pids(tr);
10092 ftrace_destroy_function_files(tr);
10093 tracefs_remove(tr->dir);
10094 free_percpu(tr->last_func_repeats);
10095 free_trace_buffers(tr);
10096 clear_tracing_err_log(tr);
10098 if (tr->range_name) {
10099 reserve_mem_release_by_name(tr->range_name);
10100 kfree(tr->range_name);
10103 for (i = 0; i < tr->nr_topts; i++) {
10104 kfree(tr->topts[i].topts);
10108 free_cpumask_var(tr->pipe_cpumask);
10109 free_cpumask_var(tr->tracing_cpumask);
10110 kfree_const(tr->system_names);
10117 int trace_array_destroy(struct trace_array *this_tr)
10119 struct trace_array *tr;
10124 guard(mutex)(&event_mutex);
10125 guard(mutex)(&trace_types_lock);
10128 /* Making sure trace array exists before destroying it. */
10129 list_for_each_entry(tr, &ftrace_trace_arrays, list) {
10131 return __remove_instance(tr);
10136 EXPORT_SYMBOL_GPL(trace_array_destroy);
10138 static int instance_rmdir(const char *name)
10140 struct trace_array *tr;
10142 guard(mutex)(&event_mutex);
10143 guard(mutex)(&trace_types_lock);
10145 tr = trace_array_find(name);
10149 return __remove_instance(tr);
10152 static __init void create_trace_instances(struct dentry *d_tracer)
10154 struct trace_array *tr;
10156 trace_instance_dir = tracefs_create_instance_dir("instances", d_tracer,
10159 if (MEM_FAIL(!trace_instance_dir, "Failed to create instances directory\n"))
10162 guard(mutex)(&event_mutex);
10163 guard(mutex)(&trace_types_lock);
10165 list_for_each_entry(tr, &ftrace_trace_arrays, list) {
10168 if (MEM_FAIL(trace_array_create_dir(tr) < 0,
10169 "Failed to create instance directory\n"))
10175 init_tracer_tracefs(struct trace_array *tr, struct dentry *d_tracer)
10179 trace_create_file("available_tracers", TRACE_MODE_READ, d_tracer,
10180 tr, &show_traces_fops);
10182 trace_create_file("current_tracer", TRACE_MODE_WRITE, d_tracer,
10183 tr, &set_tracer_fops);
10185 trace_create_file("tracing_cpumask", TRACE_MODE_WRITE, d_tracer,
10186 tr, &tracing_cpumask_fops);
10188 trace_create_file("trace_options", TRACE_MODE_WRITE, d_tracer,
10189 tr, &tracing_iter_fops);
10191 trace_create_file("trace", TRACE_MODE_WRITE, d_tracer,
10192 tr, &tracing_fops);
10194 trace_create_file("trace_pipe", TRACE_MODE_READ, d_tracer,
10195 tr, &tracing_pipe_fops);
10197 trace_create_file("buffer_size_kb", TRACE_MODE_WRITE, d_tracer,
10198 tr, &tracing_entries_fops);
10200 trace_create_file("buffer_total_size_kb", TRACE_MODE_READ, d_tracer,
10201 tr, &tracing_total_entries_fops);
10203 trace_create_file("free_buffer", 0200, d_tracer,
10204 tr, &tracing_free_buffer_fops);
10206 trace_create_file("trace_marker", 0220, d_tracer,
10207 tr, &tracing_mark_fops);
10209 tr->trace_marker_file = __find_event_file(tr, "ftrace", "print");
10211 trace_create_file("trace_marker_raw", 0220, d_tracer,
10212 tr, &tracing_mark_raw_fops);
10214 trace_create_file("trace_clock", TRACE_MODE_WRITE, d_tracer, tr,
10215 &trace_clock_fops);
10217 trace_create_file("tracing_on", TRACE_MODE_WRITE, d_tracer,
10218 tr, &rb_simple_fops);
10220 trace_create_file("timestamp_mode", TRACE_MODE_READ, d_tracer, tr,
10221 &trace_time_stamp_mode_fops);
10223 tr->buffer_percent = 50;
10225 trace_create_file("buffer_percent", TRACE_MODE_WRITE, d_tracer,
10226 tr, &buffer_percent_fops);
10228 trace_create_file("buffer_subbuf_size_kb", TRACE_MODE_WRITE, d_tracer,
10229 tr, &buffer_subbuf_size_fops);
10231 create_trace_options_dir(tr);
10233 #ifdef CONFIG_TRACER_MAX_TRACE
10234 trace_create_maxlat_file(tr, d_tracer);
10237 if (ftrace_create_function_files(tr, d_tracer))
10238 MEM_FAIL(1, "Could not allocate function filter files");
10240 if (tr->range_addr_start) {
10241 trace_create_file("last_boot_info", TRACE_MODE_READ, d_tracer,
10242 tr, &last_boot_fops);
10243 #ifdef CONFIG_TRACER_SNAPSHOT
10245 trace_create_file("snapshot", TRACE_MODE_WRITE, d_tracer,
10246 tr, &snapshot_fops);
10250 trace_create_file("error_log", TRACE_MODE_WRITE, d_tracer,
10251 tr, &tracing_err_log_fops);
10253 for_each_tracing_cpu(cpu)
10254 tracing_init_tracefs_percpu(tr, cpu);
10256 ftrace_init_tracefs(tr, d_tracer);
10259 static struct vfsmount *trace_automount(struct dentry *mntpt, void *ingore)
10261 struct vfsmount *mnt;
10262 struct file_system_type *type;
10263 struct fs_context *fc;
10267 * To maintain backward compatibility for tools that mount
10268 * debugfs to get to the tracing facility, tracefs is automatically
10269 * mounted to the debugfs/tracing directory.
10271 type = get_fs_type("tracefs");
10275 fc = fs_context_for_submount(type, mntpt);
10276 put_filesystem(type);
10278 return ERR_CAST(fc);
10280 ret = vfs_parse_fs_string(fc, "source",
10281 "tracefs", strlen("tracefs"));
10283 mnt = fc_mount(fc);
10285 mnt = ERR_PTR(ret);
10287 put_fs_context(fc);
10292 * tracing_init_dentry - initialize top level trace array
10294 * This is called when creating files or directories in the tracing
10295 * directory. It is called via fs_initcall() by any of the boot up code
10296 * and expects to return the dentry of the top level tracing directory.
10298 int tracing_init_dentry(void)
10300 struct trace_array *tr = &global_trace;
10302 if (security_locked_down(LOCKDOWN_TRACEFS)) {
10303 pr_warn("Tracing disabled due to lockdown\n");
10307 /* The top level trace array uses NULL as parent */
10311 if (WARN_ON(!tracefs_initialized()))
10315 * As there may still be users that expect the tracing
10316 * files to exist in debugfs/tracing, we must automount
10317 * the tracefs file system there, so older tools still
10318 * work with the newer kernel.
10320 tr->dir = debugfs_create_automount("tracing", NULL,
10321 trace_automount, NULL);
10326 extern struct trace_eval_map *__start_ftrace_eval_maps[];
10327 extern struct trace_eval_map *__stop_ftrace_eval_maps[];
10329 static struct workqueue_struct *eval_map_wq __initdata;
10330 static struct work_struct eval_map_work __initdata;
10331 static struct work_struct tracerfs_init_work __initdata;
10333 static void __init eval_map_work_func(struct work_struct *work)
10337 len = __stop_ftrace_eval_maps - __start_ftrace_eval_maps;
10338 trace_insert_eval_map(NULL, __start_ftrace_eval_maps, len);
10341 static int __init trace_eval_init(void)
10343 INIT_WORK(&eval_map_work, eval_map_work_func);
10345 eval_map_wq = alloc_workqueue("eval_map_wq", WQ_UNBOUND, 0);
10346 if (!eval_map_wq) {
10347 pr_err("Unable to allocate eval_map_wq\n");
10349 eval_map_work_func(&eval_map_work);
10353 queue_work(eval_map_wq, &eval_map_work);
10357 subsys_initcall(trace_eval_init);
10359 static int __init trace_eval_sync(void)
10361 /* Make sure the eval map updates are finished */
10363 destroy_workqueue(eval_map_wq);
10367 late_initcall_sync(trace_eval_sync);
10370 #ifdef CONFIG_MODULES
10372 bool module_exists(const char *module)
10374 /* All modules have the symbol __this_module */
10375 static const char this_mod[] = "__this_module";
10376 char modname[MAX_PARAM_PREFIX_LEN + sizeof(this_mod) + 2];
10380 n = snprintf(modname, sizeof(modname), "%s:%s", module, this_mod);
10382 if (n > sizeof(modname) - 1)
10385 val = module_kallsyms_lookup_name(modname);
10389 static void trace_module_add_evals(struct module *mod)
10391 if (!mod->num_trace_evals)
10395 * Modules with bad taint do not have events created, do
10396 * not bother with enums either.
10398 if (trace_module_has_bad_taint(mod))
10401 trace_insert_eval_map(mod, mod->trace_evals, mod->num_trace_evals);
10404 #ifdef CONFIG_TRACE_EVAL_MAP_FILE
10405 static void trace_module_remove_evals(struct module *mod)
10407 union trace_eval_map_item *map;
10408 union trace_eval_map_item **last = &trace_eval_maps;
10410 if (!mod->num_trace_evals)
10413 guard(mutex)(&trace_eval_mutex);
10415 map = trace_eval_maps;
10418 if (map->head.mod == mod)
10420 map = trace_eval_jmp_to_tail(map);
10421 last = &map->tail.next;
10422 map = map->tail.next;
10427 *last = trace_eval_jmp_to_tail(map)->tail.next;
10431 static inline void trace_module_remove_evals(struct module *mod) { }
10432 #endif /* CONFIG_TRACE_EVAL_MAP_FILE */
10434 static void trace_module_record(struct module *mod, bool add)
10436 struct trace_array *tr;
10437 unsigned long flags;
10439 list_for_each_entry(tr, &ftrace_trace_arrays, list) {
10440 flags = tr->flags & (TRACE_ARRAY_FL_BOOT | TRACE_ARRAY_FL_LAST_BOOT);
10441 /* Update any persistent trace array that has already been started */
10442 if (flags == TRACE_ARRAY_FL_BOOT && add) {
10443 guard(mutex)(&scratch_mutex);
10445 } else if (flags & TRACE_ARRAY_FL_LAST_BOOT) {
10446 /* Update delta if the module loaded in previous boot */
10447 make_mod_delta(mod, tr);
10452 static int trace_module_notify(struct notifier_block *self,
10453 unsigned long val, void *data)
10455 struct module *mod = data;
10458 case MODULE_STATE_COMING:
10459 trace_module_add_evals(mod);
10460 trace_module_record(mod, true);
10462 case MODULE_STATE_GOING:
10463 trace_module_remove_evals(mod);
10464 trace_module_record(mod, false);
10471 static struct notifier_block trace_module_nb = {
10472 .notifier_call = trace_module_notify,
10475 #endif /* CONFIG_MODULES */
10477 static __init void tracer_init_tracefs_work_func(struct work_struct *work)
10480 event_trace_init();
10482 init_tracer_tracefs(&global_trace, NULL);
10483 ftrace_init_tracefs_toplevel(&global_trace, NULL);
10485 trace_create_file("tracing_thresh", TRACE_MODE_WRITE, NULL,
10486 &global_trace, &tracing_thresh_fops);
10488 trace_create_file("README", TRACE_MODE_READ, NULL,
10489 NULL, &tracing_readme_fops);
10491 trace_create_file("saved_cmdlines", TRACE_MODE_READ, NULL,
10492 NULL, &tracing_saved_cmdlines_fops);
10494 trace_create_file("saved_cmdlines_size", TRACE_MODE_WRITE, NULL,
10495 NULL, &tracing_saved_cmdlines_size_fops);
10497 trace_create_file("saved_tgids", TRACE_MODE_READ, NULL,
10498 NULL, &tracing_saved_tgids_fops);
10500 trace_create_eval_file(NULL);
10502 #ifdef CONFIG_MODULES
10503 register_module_notifier(&trace_module_nb);
10506 #ifdef CONFIG_DYNAMIC_FTRACE
10507 trace_create_file("dyn_ftrace_total_info", TRACE_MODE_READ, NULL,
10508 NULL, &tracing_dyn_info_fops);
10511 create_trace_instances(NULL);
10513 update_tracer_options(&global_trace);
10516 static __init int tracer_init_tracefs(void)
10520 trace_access_lock_init();
10522 ret = tracing_init_dentry();
10527 INIT_WORK(&tracerfs_init_work, tracer_init_tracefs_work_func);
10528 queue_work(eval_map_wq, &tracerfs_init_work);
10530 tracer_init_tracefs_work_func(NULL);
10533 rv_init_interface();
10538 fs_initcall(tracer_init_tracefs);
10540 static int trace_die_panic_handler(struct notifier_block *self,
10541 unsigned long ev, void *unused);
10543 static struct notifier_block trace_panic_notifier = {
10544 .notifier_call = trace_die_panic_handler,
10545 .priority = INT_MAX - 1,
10548 static struct notifier_block trace_die_notifier = {
10549 .notifier_call = trace_die_panic_handler,
10550 .priority = INT_MAX - 1,
10554 * The idea is to execute the following die/panic callback early, in order
10555 * to avoid showing irrelevant information in the trace (like other panic
10556 * notifier functions); we are the 2nd to run, after hung_task/rcu_stall
10557 * warnings get disabled (to prevent potential log flooding).
10559 static int trace_die_panic_handler(struct notifier_block *self,
10560 unsigned long ev, void *unused)
10562 if (!ftrace_dump_on_oops_enabled())
10563 return NOTIFY_DONE;
10565 /* The die notifier requires DIE_OOPS to trigger */
10566 if (self == &trace_die_notifier && ev != DIE_OOPS)
10567 return NOTIFY_DONE;
10569 ftrace_dump(DUMP_PARAM);
10571 return NOTIFY_DONE;
10575 * printk is set to max of 1024, we really don't need it that big.
10576 * Nothing should be printing 1000 characters anyway.
10578 #define TRACE_MAX_PRINT 1000
10581 * Define here KERN_TRACE so that we have one place to modify
10582 * it if we decide to change what log level the ftrace dump
10585 #define KERN_TRACE KERN_EMERG
10588 trace_printk_seq(struct trace_seq *s)
10590 /* Probably should print a warning here. */
10591 if (s->seq.len >= TRACE_MAX_PRINT)
10592 s->seq.len = TRACE_MAX_PRINT;
10595 * More paranoid code. Although the buffer size is set to
10596 * PAGE_SIZE, and TRACE_MAX_PRINT is 1000, this is just
10597 * an extra layer of protection.
10599 if (WARN_ON_ONCE(s->seq.len >= s->seq.size))
10600 s->seq.len = s->seq.size - 1;
10602 /* should be zero ended, but we are paranoid. */
10603 s->buffer[s->seq.len] = 0;
10605 printk(KERN_TRACE "%s", s->buffer);
10610 static void trace_init_iter(struct trace_iterator *iter, struct trace_array *tr)
10613 iter->trace = iter->tr->current_trace;
10614 iter->cpu_file = RING_BUFFER_ALL_CPUS;
10615 iter->array_buffer = &tr->array_buffer;
10617 if (iter->trace && iter->trace->open)
10618 iter->trace->open(iter);
10620 /* Annotate start of buffers if we had overruns */
10621 if (ring_buffer_overruns(iter->array_buffer->buffer))
10622 iter->iter_flags |= TRACE_FILE_ANNOTATE;
10624 /* Output in nanoseconds only if we are using a clock in nanoseconds. */
10625 if (trace_clocks[iter->tr->clock_id].in_ns)
10626 iter->iter_flags |= TRACE_FILE_TIME_IN_NS;
10628 /* Can not use kmalloc for iter.temp and iter.fmt */
10629 iter->temp = static_temp_buf;
10630 iter->temp_size = STATIC_TEMP_BUF_SIZE;
10631 iter->fmt = static_fmt_buf;
10632 iter->fmt_size = STATIC_FMT_BUF_SIZE;
10635 void trace_init_global_iter(struct trace_iterator *iter)
10637 trace_init_iter(iter, &global_trace);
10640 static void ftrace_dump_one(struct trace_array *tr, enum ftrace_dump_mode dump_mode)
10642 /* use static because iter can be a bit big for the stack */
10643 static struct trace_iterator iter;
10644 unsigned int old_userobj;
10645 unsigned long flags;
10649 * Always turn off tracing when we dump.
10650 * We don't need to show trace output of what happens
10651 * between multiple crashes.
10653 * If the user does a sysrq-z, then they can re-enable
10654 * tracing with echo 1 > tracing_on.
10656 tracer_tracing_off(tr);
10658 local_irq_save(flags);
10660 /* Simulate the iterator */
10661 trace_init_iter(&iter, tr);
10663 /* While dumping, do not allow the buffer to be enable */
10664 tracer_tracing_disable(tr);
10666 old_userobj = tr->trace_flags & TRACE_ITER_SYM_USEROBJ;
10668 /* don't look at user memory in panic mode */
10669 tr->trace_flags &= ~TRACE_ITER_SYM_USEROBJ;
10671 if (dump_mode == DUMP_ORIG)
10672 iter.cpu_file = raw_smp_processor_id();
10674 iter.cpu_file = RING_BUFFER_ALL_CPUS;
10676 if (tr == &global_trace)
10677 printk(KERN_TRACE "Dumping ftrace buffer:\n");
10679 printk(KERN_TRACE "Dumping ftrace instance %s buffer:\n", tr->name);
10681 /* Did function tracer already get disabled? */
10682 if (ftrace_is_dead()) {
10683 printk("# WARNING: FUNCTION TRACING IS CORRUPTED\n");
10684 printk("# MAY BE MISSING FUNCTION EVENTS\n");
10688 * We need to stop all tracing on all CPUS to read
10689 * the next buffer. This is a bit expensive, but is
10690 * not done often. We fill all what we can read,
10691 * and then release the locks again.
10694 while (!trace_empty(&iter)) {
10697 printk(KERN_TRACE "---------------------------------\n");
10701 trace_iterator_reset(&iter);
10702 iter.iter_flags |= TRACE_FILE_LAT_FMT;
10704 if (trace_find_next_entry_inc(&iter) != NULL) {
10707 ret = print_trace_line(&iter);
10708 if (ret != TRACE_TYPE_NO_CONSUME)
10709 trace_consume(&iter);
10711 touch_nmi_watchdog();
10713 trace_printk_seq(&iter.seq);
10717 printk(KERN_TRACE " (ftrace buffer empty)\n");
10719 printk(KERN_TRACE "---------------------------------\n");
10721 tr->trace_flags |= old_userobj;
10723 tracer_tracing_enable(tr);
10724 local_irq_restore(flags);
10727 static void ftrace_dump_by_param(void)
10729 bool first_param = true;
10730 char dump_param[MAX_TRACER_SIZE];
10731 char *buf, *token, *inst_name;
10732 struct trace_array *tr;
10734 strscpy(dump_param, ftrace_dump_on_oops, MAX_TRACER_SIZE);
10737 while ((token = strsep(&buf, ",")) != NULL) {
10739 first_param = false;
10740 if (!strcmp("0", token))
10742 else if (!strcmp("1", token)) {
10743 ftrace_dump_one(&global_trace, DUMP_ALL);
10746 else if (!strcmp("2", token) ||
10747 !strcmp("orig_cpu", token)) {
10748 ftrace_dump_one(&global_trace, DUMP_ORIG);
10753 inst_name = strsep(&token, "=");
10754 tr = trace_array_find(inst_name);
10756 printk(KERN_TRACE "Instance %s not found\n", inst_name);
10760 if (token && (!strcmp("2", token) ||
10761 !strcmp("orig_cpu", token)))
10762 ftrace_dump_one(tr, DUMP_ORIG);
10764 ftrace_dump_one(tr, DUMP_ALL);
10768 void ftrace_dump(enum ftrace_dump_mode oops_dump_mode)
10770 static atomic_t dump_running;
10772 /* Only allow one dump user at a time. */
10773 if (atomic_inc_return(&dump_running) != 1) {
10774 atomic_dec(&dump_running);
10778 switch (oops_dump_mode) {
10780 ftrace_dump_one(&global_trace, DUMP_ALL);
10783 ftrace_dump_one(&global_trace, DUMP_ORIG);
10786 ftrace_dump_by_param();
10791 printk(KERN_TRACE "Bad dumping mode, switching to all CPUs dump\n");
10792 ftrace_dump_one(&global_trace, DUMP_ALL);
10795 atomic_dec(&dump_running);
10797 EXPORT_SYMBOL_GPL(ftrace_dump);
10799 #define WRITE_BUFSIZE 4096
10801 ssize_t trace_parse_run_command(struct file *file, const char __user *buffer,
10802 size_t count, loff_t *ppos,
10803 int (*createfn)(const char *))
10805 char *kbuf, *buf, *tmp;
10810 kbuf = kmalloc(WRITE_BUFSIZE, GFP_KERNEL);
10814 while (done < count) {
10815 size = count - done;
10817 if (size >= WRITE_BUFSIZE)
10818 size = WRITE_BUFSIZE - 1;
10820 if (copy_from_user(kbuf, buffer + done, size)) {
10827 tmp = strchr(buf, '\n');
10830 size = tmp - buf + 1;
10832 size = strlen(buf);
10833 if (done + size < count) {
10836 /* This can accept WRITE_BUFSIZE - 2 ('\n' + '\0') */
10837 pr_warn("Line length is too long: Should be less than %d\n",
10838 WRITE_BUFSIZE - 2);
10845 /* Remove comments */
10846 tmp = strchr(buf, '#');
10851 ret = createfn(buf);
10856 } while (done < count);
10866 #ifdef CONFIG_TRACER_MAX_TRACE
10867 __init static bool tr_needs_alloc_snapshot(const char *name)
10870 int len = strlen(name);
10873 if (!boot_snapshot_index)
10876 if (strncmp(name, boot_snapshot_info, len) == 0 &&
10877 boot_snapshot_info[len] == '\t')
10880 test = kmalloc(strlen(name) + 3, GFP_KERNEL);
10884 sprintf(test, "\t%s\t", name);
10885 ret = strstr(boot_snapshot_info, test) == NULL;
10890 __init static void do_allocate_snapshot(const char *name)
10892 if (!tr_needs_alloc_snapshot(name))
10896 * When allocate_snapshot is set, the next call to
10897 * allocate_trace_buffers() (called by trace_array_get_by_name())
10898 * will allocate the snapshot buffer. That will alse clear
10901 allocate_snapshot = true;
10904 static inline void do_allocate_snapshot(const char *name) { }
10907 __init static void enable_instances(void)
10909 struct trace_array *tr;
10910 bool memmap_area = false;
10916 /* A tab is always appended */
10917 boot_instance_info[boot_instance_index - 1] = '\0';
10918 str = boot_instance_info;
10920 while ((curr_str = strsep(&str, "\t"))) {
10921 phys_addr_t start = 0;
10922 phys_addr_t size = 0;
10923 unsigned long addr = 0;
10924 bool traceprintk = false;
10925 bool traceoff = false;
10928 char *rname __free(kfree) = NULL;
10930 tok = strsep(&curr_str, ",");
10932 flag_delim = strchr(tok, '^');
10933 addr_delim = strchr(tok, '@');
10936 *addr_delim++ = '\0';
10939 *flag_delim++ = '\0';
10946 while ((flag = strsep(&flag_delim, "^"))) {
10947 if (strcmp(flag, "traceoff") == 0) {
10949 } else if ((strcmp(flag, "printk") == 0) ||
10950 (strcmp(flag, "traceprintk") == 0) ||
10951 (strcmp(flag, "trace_printk") == 0)) {
10952 traceprintk = true;
10954 pr_info("Tracing: Invalid instance flag '%s' for %s\n",
10961 if (tok && isdigit(*tok)) {
10962 start = memparse(tok, &tok);
10964 pr_warn("Tracing: Invalid boot instance address for %s\n",
10969 pr_warn("Tracing: No size specified for instance %s\n", name);
10973 size = memparse(tok, &tok);
10975 pr_warn("Tracing: Invalid boot instance size for %s\n",
10979 memmap_area = true;
10981 if (!reserve_mem_find_by_name(tok, &start, &size)) {
10983 pr_warn("Failed to map boot instance %s to %s\n", name, tok);
10986 rname = kstrdup(tok, GFP_KERNEL);
10990 /* Start and size must be page aligned */
10991 if (start & ~PAGE_MASK) {
10992 pr_warn("Tracing: mapping start addr %pa is not page aligned\n", &start);
10995 if (size & ~PAGE_MASK) {
10996 pr_warn("Tracing: mapping size %pa is not page aligned\n", &size);
11001 addr = map_pages(start, size);
11003 addr = (unsigned long)phys_to_virt(start);
11005 pr_info("Tracing: mapped boot instance %s at physical memory %pa of size 0x%lx\n",
11006 name, &start, (unsigned long)size);
11008 pr_warn("Tracing: Failed to map boot instance %s\n", name);
11012 /* Only non mapped buffers have snapshot buffers */
11013 if (IS_ENABLED(CONFIG_TRACER_MAX_TRACE))
11014 do_allocate_snapshot(name);
11017 tr = trace_array_create_systems(name, NULL, addr, size);
11019 pr_warn("Tracing: Failed to create instance buffer %s\n", curr_str);
11024 tracer_tracing_off(tr);
11027 update_printk_trace(tr);
11030 * memmap'd buffers can not be freed.
11033 tr->flags |= TRACE_ARRAY_FL_MEMMAP;
11038 tr->flags |= TRACE_ARRAY_FL_BOOT | TRACE_ARRAY_FL_LAST_BOOT;
11039 tr->range_name = no_free_ptr(rname);
11042 while ((tok = strsep(&curr_str, ","))) {
11043 early_enable_events(tr, tok, true);
11048 __init static int tracer_alloc_buffers(void)
11054 if (security_locked_down(LOCKDOWN_TRACEFS)) {
11055 pr_warn("Tracing disabled due to lockdown\n");
11060 * Make sure we don't accidentally add more trace options
11061 * than we have bits for.
11063 BUILD_BUG_ON(TRACE_ITER_LAST_BIT > TRACE_FLAGS_MAX_SIZE);
11065 if (!alloc_cpumask_var(&tracing_buffer_mask, GFP_KERNEL))
11068 if (!alloc_cpumask_var(&global_trace.tracing_cpumask, GFP_KERNEL))
11069 goto out_free_buffer_mask;
11071 /* Only allocate trace_printk buffers if a trace_printk exists */
11072 if (&__stop___trace_bprintk_fmt != &__start___trace_bprintk_fmt)
11073 /* Must be called before global_trace.buffer is allocated */
11074 trace_printk_init_buffers();
11076 /* To save memory, keep the ring buffer size to its minimum */
11077 if (global_trace.ring_buffer_expanded)
11078 ring_buf_size = trace_buf_size;
11082 cpumask_copy(tracing_buffer_mask, cpu_possible_mask);
11083 cpumask_copy(global_trace.tracing_cpumask, cpu_all_mask);
11085 raw_spin_lock_init(&global_trace.start_lock);
11088 * The prepare callbacks allocates some memory for the ring buffer. We
11089 * don't free the buffer if the CPU goes down. If we were to free
11090 * the buffer, then the user would lose any trace that was in the
11091 * buffer. The memory will be removed once the "instance" is removed.
11093 ret = cpuhp_setup_state_multi(CPUHP_TRACE_RB_PREPARE,
11094 "trace/RB:prepare", trace_rb_cpu_prepare,
11097 goto out_free_cpumask;
11098 /* Used for event triggers */
11100 temp_buffer = ring_buffer_alloc(PAGE_SIZE, RB_FL_OVERWRITE);
11102 goto out_rm_hp_state;
11104 if (trace_create_savedcmd() < 0)
11105 goto out_free_temp_buffer;
11107 if (!zalloc_cpumask_var(&global_trace.pipe_cpumask, GFP_KERNEL))
11108 goto out_free_savedcmd;
11110 /* TODO: make the number of buffers hot pluggable with CPUS */
11111 if (allocate_trace_buffers(&global_trace, ring_buf_size) < 0) {
11112 MEM_FAIL(1, "tracer: failed to allocate ring buffer!\n");
11113 goto out_free_pipe_cpumask;
11115 if (global_trace.buffer_disabled)
11118 if (trace_boot_clock) {
11119 ret = tracing_set_clock(&global_trace, trace_boot_clock);
11121 pr_warn("Trace clock %s not defined, going back to default\n",
11126 * register_tracer() might reference current_trace, so it
11127 * needs to be set before we register anything. This is
11128 * just a bootstrap of current_trace anyway.
11130 global_trace.current_trace = &nop_trace;
11132 global_trace.max_lock = (arch_spinlock_t)__ARCH_SPIN_LOCK_UNLOCKED;
11133 #ifdef CONFIG_TRACER_MAX_TRACE
11134 spin_lock_init(&global_trace.snapshot_trigger_lock);
11136 ftrace_init_global_array_ops(&global_trace);
11138 #ifdef CONFIG_MODULES
11139 INIT_LIST_HEAD(&global_trace.mod_events);
11142 init_trace_flags_index(&global_trace);
11144 register_tracer(&nop_trace);
11146 /* Function tracing may start here (via kernel command line) */
11147 init_function_trace();
11149 /* All seems OK, enable tracing */
11150 tracing_disabled = 0;
11152 atomic_notifier_chain_register(&panic_notifier_list,
11153 &trace_panic_notifier);
11155 register_die_notifier(&trace_die_notifier);
11157 global_trace.flags = TRACE_ARRAY_FL_GLOBAL;
11159 INIT_LIST_HEAD(&global_trace.systems);
11160 INIT_LIST_HEAD(&global_trace.events);
11161 INIT_LIST_HEAD(&global_trace.hist_vars);
11162 INIT_LIST_HEAD(&global_trace.err_log);
11163 list_add(&global_trace.marker_list, &marker_copies);
11164 list_add(&global_trace.list, &ftrace_trace_arrays);
11166 apply_trace_boot_options();
11168 register_snapshot_cmd();
11172 out_free_pipe_cpumask:
11173 free_cpumask_var(global_trace.pipe_cpumask);
11175 trace_free_saved_cmdlines_buffer();
11176 out_free_temp_buffer:
11177 ring_buffer_free(temp_buffer);
11179 cpuhp_remove_multi_state(CPUHP_TRACE_RB_PREPARE);
11181 free_cpumask_var(global_trace.tracing_cpumask);
11182 out_free_buffer_mask:
11183 free_cpumask_var(tracing_buffer_mask);
11188 #ifdef CONFIG_FUNCTION_TRACER
11189 /* Used to set module cached ftrace filtering at boot up */
11190 __init struct trace_array *trace_get_global_array(void)
11192 return &global_trace;
11196 void __init ftrace_boot_snapshot(void)
11198 #ifdef CONFIG_TRACER_MAX_TRACE
11199 struct trace_array *tr;
11201 if (!snapshot_at_boot)
11204 list_for_each_entry(tr, &ftrace_trace_arrays, list) {
11205 if (!tr->allocated_snapshot)
11208 tracing_snapshot_instance(tr);
11209 trace_array_puts(tr, "** Boot snapshot taken **\n");
11214 void __init early_trace_init(void)
11216 if (tracepoint_printk) {
11217 tracepoint_print_iter =
11218 kzalloc(sizeof(*tracepoint_print_iter), GFP_KERNEL);
11219 if (MEM_FAIL(!tracepoint_print_iter,
11220 "Failed to allocate trace iterator\n"))
11221 tracepoint_printk = 0;
11223 static_key_enable(&tracepoint_printk_key.key);
11225 tracer_alloc_buffers();
11230 void __init trace_init(void)
11232 trace_event_init();
11234 if (boot_instance_index)
11235 enable_instances();
11238 __init static void clear_boot_tracer(void)
11241 * The default tracer at boot buffer is an init section.
11242 * This function is called in lateinit. If we did not
11243 * find the boot tracer, then clear it out, to prevent
11244 * later registration from accessing the buffer that is
11245 * about to be freed.
11247 if (!default_bootup_tracer)
11250 printk(KERN_INFO "ftrace bootup tracer '%s' not registered.\n",
11251 default_bootup_tracer);
11252 default_bootup_tracer = NULL;
11255 #ifdef CONFIG_HAVE_UNSTABLE_SCHED_CLOCK
11256 __init static void tracing_set_default_clock(void)
11258 /* sched_clock_stable() is determined in late_initcall */
11259 if (!trace_boot_clock && !sched_clock_stable()) {
11260 if (security_locked_down(LOCKDOWN_TRACEFS)) {
11261 pr_warn("Can not set tracing clock due to lockdown\n");
11265 printk(KERN_WARNING
11266 "Unstable clock detected, switching default tracing clock to \"global\"\n"
11267 "If you want to keep using the local clock, then add:\n"
11268 " \"trace_clock=local\"\n"
11269 "on the kernel command line\n");
11270 tracing_set_clock(&global_trace, "global");
11274 static inline void tracing_set_default_clock(void) { }
11277 __init static int late_trace_init(void)
11279 if (tracepoint_printk && tracepoint_printk_stop_on_boot) {
11280 static_key_disable(&tracepoint_printk_key.key);
11281 tracepoint_printk = 0;
11284 if (traceoff_after_boot)
11287 tracing_set_default_clock();
11288 clear_boot_tracer();
11292 late_initcall_sync(late_trace_init);