Merge tag 'pci-v6.16-fixes-3' of git://git.kernel.org/pub/scm/linux/kernel/git/pci/pci
[linux-2.6-block.git] / kernel / trace / trace.c
CommitLineData
bcea3f96 1// SPDX-License-Identifier: GPL-2.0
bc0c38d1
SR
2/*
3 * ring buffer based function tracer
4 *
2b6080f2 5 * Copyright (C) 2007-2012 Steven Rostedt <srostedt@redhat.com>
bc0c38d1
SR
6 * Copyright (C) 2008 Ingo Molnar <mingo@redhat.com>
7 *
8 * Originally taken from the RT patch by:
9 * Arnaldo Carvalho de Melo <acme@redhat.com>
10 *
11 * Based on code from the latency_tracer, that is:
12 * Copyright (C) 2004-2006 Ingo Molnar
6d49e352 13 * Copyright (C) 2004 Nadia Yvette Chambers
bc0c38d1 14 */
2cadf913 15#include <linux/ring_buffer.h>
ed896837 16#include <linux/utsname.h>
2cadf913
SR
17#include <linux/stacktrace.h>
18#include <linux/writeback.h>
bc0c38d1 19#include <linux/kallsyms.h>
17911ff3 20#include <linux/security.h>
bc0c38d1 21#include <linux/seq_file.h>
2cadf913 22#include <linux/irqflags.h>
bc0c38d1 23#include <linux/debugfs.h>
8434dc93 24#include <linux/tracefs.h>
4c11d7ae 25#include <linux/pagemap.h>
bc0c38d1
SR
26#include <linux/hardirq.h>
27#include <linux/linkage.h>
28#include <linux/uaccess.h>
d33b10c0 29#include <linux/cleanup.h>
76c813e2 30#include <linux/vmalloc.h>
bc0c38d1
SR
31#include <linux/ftrace.h>
32#include <linux/module.h>
33#include <linux/percpu.h>
2cadf913 34#include <linux/splice.h>
3f5a54e3 35#include <linux/kdebug.h>
5f0c6c03 36#include <linux/string.h>
f76180bc 37#include <linux/mount.h>
7e53bd42 38#include <linux/rwsem.h>
5a0e3ad6 39#include <linux/slab.h>
bc0c38d1
SR
40#include <linux/ctype.h>
41#include <linux/init.h>
f39650de 42#include <linux/panic_notifier.h>
2a2cc8f7 43#include <linux/poll.h>
b892e5c8 44#include <linux/nmi.h>
bc0c38d1 45#include <linux/fs.h>
478409dd 46#include <linux/trace.h>
3fd49c9e 47#include <linux/sched/clock.h>
8bd75c77 48#include <linux/sched/rt.h>
91edde2e
VRB
49#include <linux/fsnotify.h>
50#include <linux/irq_work.h>
51#include <linux/workqueue.h>
35a380dd 52#include <linux/sort.h>
394f3f02 53#include <linux/io.h> /* vmap_page_range() */
2dbf6e0d 54#include <linux/fs_context.h>
86387f7e 55
cb1f98c5
SRG
56#include <asm/setup.h> /* COMMAND_LINE_SIZE */
57
bc0c38d1 58#include "trace.h"
f0868d1e 59#include "trace_output.h"
bc0c38d1 60
a3ae76d7 61#ifdef CONFIG_FTRACE_STARTUP_TEST
8e1b82e0
FW
62/*
63 * We need to change this state when a selftest is running.
ff32504f
FW
64 * A selftest will lurk into the ring-buffer to count the
65 * entries inserted during the selftest although some concurrent
5e1607a0 66 * insertions into the ring-buffer such as trace_printk could occurred
ff32504f
FW
67 * at the same time, giving false positive or negative results.
68 */
8e1b82e0 69static bool __read_mostly tracing_selftest_running;
ff32504f 70
b2821ae6 71/*
60efe21e
MH
72 * If boot-time tracing including tracers/events via kernel cmdline
73 * is running, we do not want to run SELFTEST.
b2821ae6 74 */
020e5f85 75bool __read_mostly tracing_selftest_disabled;
b2821ae6 76
60efe21e
MH
77void __init disable_tracing_selftest(const char *reason)
78{
79 if (!tracing_selftest_disabled) {
80 tracing_selftest_disabled = true;
81 pr_info("Ftrace startup test is disabled due to %s\n", reason);
82 }
83}
a3ae76d7
SRG
84#else
85#define tracing_selftest_running 0
86#define tracing_selftest_disabled 0
60efe21e
MH
87#endif
88
0daa2302 89/* Pipe tracepoints to printk */
a76d4648 90static struct trace_iterator *tracepoint_print_iter;
0daa2302 91int tracepoint_printk;
f3860136 92static bool tracepoint_printk_stop_on_boot __initdata;
937fbf11 93static bool traceoff_after_boot __initdata;
42391745 94static DEFINE_STATIC_KEY_FALSE(tracepoint_printk_key);
0daa2302 95
adf9f195
FW
96/* For tracers that don't implement custom flags */
97static struct tracer_opt dummy_tracer_opt[] = {
98 { }
99};
100
8c1a49ae
SRRH
101static int
102dummy_set_flag(struct trace_array *tr, u32 old_flags, u32 bit, int set)
adf9f195
FW
103{
104 return 0;
105}
0f048701 106
7ffbd48d
SR
107/*
108 * To prevent the comm cache from being overwritten when no
109 * tracing is active, only save the comm when a trace event
110 * occurred.
111 */
2cc621fd 112DEFINE_PER_CPU(bool, trace_taskinfo_save);
7ffbd48d 113
0f048701
SR
114/*
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
118 * this back to zero.
119 */
4fd27358 120static int tracing_disabled = 1;
0f048701 121
955b61e5 122cpumask_var_t __read_mostly tracing_buffer_mask;
ab46428c 123
dd293df6 124#define MAX_TRACER_SIZE 100
944ac425
SR
125/*
126 * ftrace_dump_on_oops - variable to dump ftrace buffer on oops
127 *
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
132 * serial console.
133 *
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
cecbca96
FW
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
19f0423f
HY
139 * Set instance name if you want to dump the specific trace instance
140 * Multiple instance dump is also supported, and instances are seperated
141 * by commas.
944ac425 142 */
19f0423f
HY
143/* Set to string format zero to disable by default */
144char ftrace_dump_on_oops[MAX_TRACER_SIZE] = "0";
944ac425 145
de7edd31 146/* When set, tracing will stop when a WARN*() is hit */
dd293df6
JG
147static int __disable_trace_on_warning;
148
149int tracepoint_printk_sysctl(const struct ctl_table *table, int write,
150 void *buffer, size_t *lenp, loff_t *ppos);
151static const struct ctl_table trace_sysctl_table[] = {
152 {
153 .procname = "ftrace_dump_on_oops",
154 .data = &ftrace_dump_on_oops,
155 .maxlen = MAX_TRACER_SIZE,
156 .mode = 0644,
157 .proc_handler = proc_dostring,
158 },
159 {
160 .procname = "traceoff_on_warning",
161 .data = &__disable_trace_on_warning,
162 .maxlen = sizeof(__disable_trace_on_warning),
163 .mode = 0644,
164 .proc_handler = proc_dointvec,
165 },
166 {
167 .procname = "tracepoint_printk",
168 .data = &tracepoint_printk,
169 .maxlen = sizeof(tracepoint_printk),
170 .mode = 0644,
171 .proc_handler = tracepoint_printk_sysctl,
172 },
173};
174
175static int __init init_trace_sysctls(void)
176{
177 register_sysctl_init("kernel", trace_sysctl_table);
178 return 0;
179}
180subsys_initcall(init_trace_sysctls);
de7edd31 181
681bec03
JL
182#ifdef CONFIG_TRACE_EVAL_MAP_FILE
183/* Map of enums to their values, for "eval_map" file */
23bf8cb8 184struct trace_eval_map_head {
9828413d
SRRH
185 struct module *mod;
186 unsigned long length;
187};
188
23bf8cb8 189union trace_eval_map_item;
9828413d 190
23bf8cb8 191struct trace_eval_map_tail {
9828413d
SRRH
192 /*
193 * "end" is first and points to NULL as it must be different
00f4b652 194 * than "mod" or "eval_string"
9828413d 195 */
23bf8cb8 196 union trace_eval_map_item *next;
9828413d
SRRH
197 const char *end; /* points to NULL */
198};
199
1793ed93 200static DEFINE_MUTEX(trace_eval_mutex);
9828413d
SRRH
201
202/*
23bf8cb8 203 * The trace_eval_maps are saved in an array with two extra elements,
9828413d
SRRH
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
681bec03 207 * pointer to the next array of saved eval_map items.
9828413d 208 */
23bf8cb8 209union trace_eval_map_item {
00f4b652 210 struct trace_eval_map map;
23bf8cb8
JL
211 struct trace_eval_map_head head;
212 struct trace_eval_map_tail tail;
9828413d
SRRH
213};
214
23bf8cb8 215static union trace_eval_map_item *trace_eval_maps;
681bec03 216#endif /* CONFIG_TRACE_EVAL_MAP_FILE */
9828413d 217
9c5b9d3d 218int tracing_set_tracer(struct trace_array *tr, const char *buf);
bcee5278
SRV
219static void ftrace_trace_userstack(struct trace_array *tr,
220 struct trace_buffer *buffer,
36590c50 221 unsigned int trace_ctx);
b2821ae6 222
ee6c2c1b 223static char bootup_tracer_buf[MAX_TRACER_SIZE] __initdata;
b2821ae6 224static char *default_bootup_tracer;
d9e54076 225
55034cd6 226static bool allocate_snapshot;
380af29b 227static bool snapshot_at_boot;
55034cd6 228
cb1f98c5
SRG
229static char boot_instance_info[COMMAND_LINE_SIZE] __initdata;
230static int boot_instance_index;
231
9c1c251d
SRG
232static char boot_snapshot_info[COMMAND_LINE_SIZE] __initdata;
233static int boot_snapshot_index;
234
1beee96b 235static int __init set_cmdline_ftrace(char *str)
d9e54076 236{
c7dce4c5 237 strscpy(bootup_tracer_buf, str, MAX_TRACER_SIZE);
b2821ae6 238 default_bootup_tracer = bootup_tracer_buf;
73c5162a 239 /* We are using ftrace early, expand it */
a1f157c7 240 trace_set_ring_buffer_expanded(NULL);
d9e54076
PZ
241 return 1;
242}
1beee96b 243__setup("ftrace=", set_cmdline_ftrace);
d9e54076 244
19f0423f
HY
245int ftrace_dump_on_oops_enabled(void)
246{
247 if (!strcmp("0", ftrace_dump_on_oops))
248 return 0;
249 else
250 return 1;
251}
252
944ac425
SR
253static int __init set_ftrace_dump_on_oops(char *str)
254{
19f0423f
HY
255 if (!*str) {
256 strscpy(ftrace_dump_on_oops, "1", MAX_TRACER_SIZE);
cecbca96
FW
257 return 1;
258 }
259
19f0423f
HY
260 if (*str == ',') {
261 strscpy(ftrace_dump_on_oops, "1", MAX_TRACER_SIZE);
262 strscpy(ftrace_dump_on_oops + 1, str, MAX_TRACER_SIZE - 1);
263 return 1;
264 }
265
266 if (*str++ == '=') {
267 strscpy(ftrace_dump_on_oops, str, MAX_TRACER_SIZE);
268 return 1;
269 }
cecbca96 270
19f0423f 271 return 0;
944ac425
SR
272}
273__setup("ftrace_dump_on_oops", set_ftrace_dump_on_oops);
60a11774 274
de7edd31
SRRH
275static int __init stop_trace_on_warning(char *str)
276{
933ff9f2
LCG
277 if ((strcmp(str, "=0") != 0 && strcmp(str, "=off") != 0))
278 __disable_trace_on_warning = 1;
de7edd31
SRRH
279 return 1;
280}
933ff9f2 281__setup("traceoff_on_warning", stop_trace_on_warning);
de7edd31 282
3209cff4 283static int __init boot_alloc_snapshot(char *str)
55034cd6 284{
9c1c251d
SRG
285 char *slot = boot_snapshot_info + boot_snapshot_index;
286 int left = sizeof(boot_snapshot_info) - boot_snapshot_index;
287 int ret;
288
289 if (str[0] == '=') {
290 str++;
291 if (strlen(str) >= left)
292 return -1;
293
294 ret = snprintf(slot, left, "%s\t", str);
295 boot_snapshot_index += ret;
296 } else {
297 allocate_snapshot = true;
298 /* We also need the main ring buffer expanded */
a1f157c7 299 trace_set_ring_buffer_expanded(NULL);
9c1c251d 300 }
55034cd6
SRRH
301 return 1;
302}
3209cff4 303__setup("alloc_snapshot", boot_alloc_snapshot);
55034cd6 304
7bcfaf54 305
380af29b
SRG
306static int __init boot_snapshot(char *str)
307{
308 snapshot_at_boot = true;
309 boot_alloc_snapshot(str);
310 return 1;
311}
312__setup("ftrace_boot_snapshot", boot_snapshot);
313
314
cb1f98c5
SRG
315static int __init boot_instance(char *str)
316{
317 char *slot = boot_instance_info + boot_instance_index;
318 int left = sizeof(boot_instance_info) - boot_instance_index;
319 int ret;
320
321 if (strlen(str) >= left)
322 return -1;
323
324 ret = snprintf(slot, left, "%s\t", str);
325 boot_instance_index += ret;
326
327 return 1;
328}
329__setup("trace_instance=", boot_instance);
330
331
7bcfaf54 332static char trace_boot_options_buf[MAX_TRACER_SIZE] __initdata;
7bcfaf54
SR
333
334static int __init set_trace_boot_options(char *str)
335{
c7dce4c5 336 strscpy(trace_boot_options_buf, str, MAX_TRACER_SIZE);
1d02b444 337 return 1;
7bcfaf54
SR
338}
339__setup("trace_options=", set_trace_boot_options);
340
e1e232ca
SR
341static char trace_boot_clock_buf[MAX_TRACER_SIZE] __initdata;
342static char *trace_boot_clock __initdata;
343
344static int __init set_trace_boot_clock(char *str)
345{
c7dce4c5 346 strscpy(trace_boot_clock_buf, str, MAX_TRACER_SIZE);
e1e232ca 347 trace_boot_clock = trace_boot_clock_buf;
1d02b444 348 return 1;
e1e232ca
SR
349}
350__setup("trace_clock=", set_trace_boot_clock);
351
0daa2302
SRRH
352static int __init set_tracepoint_printk(char *str)
353{
3203ce39
JY
354 /* Ignore the "tp_printk_stop_on_boot" param */
355 if (*str == '_')
356 return 0;
357
0daa2302
SRRH
358 if ((strcmp(str, "=0") != 0 && strcmp(str, "=off") != 0))
359 tracepoint_printk = 1;
360 return 1;
361}
362__setup("tp_printk", set_tracepoint_printk);
de7edd31 363
f3860136
SRV
364static int __init set_tracepoint_printk_stop(char *str)
365{
366 tracepoint_printk_stop_on_boot = true;
367 return 1;
368}
369__setup("tp_printk_stop_on_boot", set_tracepoint_printk_stop);
370
937fbf11
SR
371static int __init set_traceoff_after_boot(char *str)
372{
373 traceoff_after_boot = true;
374 return 1;
375}
376__setup("traceoff_after_boot", set_traceoff_after_boot);
377
a5a1d1c2 378unsigned long long ns2usecs(u64 nsec)
bc0c38d1
SR
379{
380 nsec += 500;
381 do_div(nsec, 1000);
382 return nsec;
383}
384
8ab7a2b7
TZ
385static void
386trace_process_export(struct trace_export *export,
387 struct ring_buffer_event *event, int flag)
388{
389 struct trace_entry *entry;
390 unsigned int size = 0;
391
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);
396 }
397}
398
399static DEFINE_MUTEX(ftrace_export_lock);
400
401static struct trace_export __rcu *ftrace_exports_list __read_mostly;
402
403static DEFINE_STATIC_KEY_FALSE(trace_function_exports_enabled);
404static DEFINE_STATIC_KEY_FALSE(trace_event_exports_enabled);
458999c6 405static DEFINE_STATIC_KEY_FALSE(trace_marker_exports_enabled);
8ab7a2b7
TZ
406
407static inline void ftrace_exports_enable(struct trace_export *export)
408{
409 if (export->flags & TRACE_EXPORT_FUNCTION)
410 static_branch_inc(&trace_function_exports_enabled);
411
412 if (export->flags & TRACE_EXPORT_EVENT)
413 static_branch_inc(&trace_event_exports_enabled);
458999c6
TZ
414
415 if (export->flags & TRACE_EXPORT_MARKER)
416 static_branch_inc(&trace_marker_exports_enabled);
8ab7a2b7
TZ
417}
418
419static inline void ftrace_exports_disable(struct trace_export *export)
420{
421 if (export->flags & TRACE_EXPORT_FUNCTION)
422 static_branch_dec(&trace_function_exports_enabled);
423
424 if (export->flags & TRACE_EXPORT_EVENT)
425 static_branch_dec(&trace_event_exports_enabled);
458999c6
TZ
426
427 if (export->flags & TRACE_EXPORT_MARKER)
428 static_branch_dec(&trace_marker_exports_enabled);
8ab7a2b7
TZ
429}
430
431static void ftrace_exports(struct ring_buffer_event *event, int flag)
432{
433 struct trace_export *export;
434
435 preempt_disable_notrace();
436
437 export = rcu_dereference_raw_check(ftrace_exports_list);
438 while (export) {
439 trace_process_export(export, event, flag);
440 export = rcu_dereference_raw_check(export->next);
441 }
442
443 preempt_enable_notrace();
444}
445
446static inline void
447add_trace_export(struct trace_export **list, struct trace_export *export)
448{
449 rcu_assign_pointer(export->next, *list);
450 /*
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.
455 */
456 rcu_assign_pointer(*list, export);
457}
458
459static inline int
460rm_trace_export(struct trace_export **list, struct trace_export *export)
461{
462 struct trace_export **p;
463
464 for (p = list; *p != NULL; p = &(*p)->next)
465 if (*p == export)
466 break;
467
468 if (*p != export)
469 return -1;
470
471 rcu_assign_pointer(*p, (*p)->next);
472
473 return 0;
474}
475
476static inline void
477add_ftrace_export(struct trace_export **list, struct trace_export *export)
478{
479 ftrace_exports_enable(export);
480
481 add_trace_export(list, export);
482}
483
484static inline int
485rm_ftrace_export(struct trace_export **list, struct trace_export *export)
486{
487 int ret;
488
489 ret = rm_trace_export(list, export);
490 ftrace_exports_disable(export);
491
492 return ret;
493}
494
495int register_ftrace_export(struct trace_export *export)
496{
497 if (WARN_ON_ONCE(!export->write))
498 return -1;
499
500 mutex_lock(&ftrace_export_lock);
501
502 add_ftrace_export(&ftrace_exports_list, export);
503
504 mutex_unlock(&ftrace_export_lock);
505
506 return 0;
507}
508EXPORT_SYMBOL_GPL(register_ftrace_export);
509
510int unregister_ftrace_export(struct trace_export *export)
511{
512 int ret;
513
514 mutex_lock(&ftrace_export_lock);
515
516 ret = rm_ftrace_export(&ftrace_exports_list, export);
517
518 mutex_unlock(&ftrace_export_lock);
519
520 return ret;
521}
522EXPORT_SYMBOL_GPL(unregister_ftrace_export);
523
983f938a
SRRH
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 | \
99e22ce7 530 TRACE_ITER_IRQ_INFO | TRACE_ITER_MARKERS | \
7b382efd
SR
531 TRACE_ITER_HASH_PTR | TRACE_ITER_TRACE_PRINTK | \
532 TRACE_ITER_COPY_MARKER)
983f938a 533
16270145
SRRH
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)
537
20550622
SRRH
538/* trace_flags that are default zero for instances */
539#define ZEROED_TRACE_FLAGS \
7b382efd
SR
540 (TRACE_ITER_EVENT_FORK | TRACE_ITER_FUNC_FORK | TRACE_ITER_TRACE_PRINTK | \
541 TRACE_ITER_COPY_MARKER)
16270145 542
4fcdae83 543/*
67d04bb2
JF
544 * The global_trace is the descriptor that holds the top-level tracing
545 * buffers for the live tracing.
4fcdae83 546 */
983f938a
SRRH
547static struct trace_array global_trace = {
548 .trace_flags = TRACE_DEFAULT_FLAGS,
549};
bc0c38d1 550
ddb8ea9e
SR
551static struct trace_array *printk_trace = &global_trace;
552
7b382efd
SR
553/* List of trace_arrays interested in the top level trace_marker */
554static LIST_HEAD(marker_copies);
555
9b7bdf6f
SR
556static __always_inline bool printk_binsafe(struct trace_array *tr)
557{
558 /*
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.
563 */
564 return !(tr->flags & TRACE_ARRAY_FL_BOOT);
565}
566
ef2bd81d
SR
567static void update_printk_trace(struct trace_array *tr)
568{
569 if (printk_trace == tr)
570 return;
571
572 printk_trace->trace_flags &= ~TRACE_ITER_TRACE_PRINTK;
573 printk_trace = tr;
574 tr->trace_flags |= TRACE_ITER_TRACE_PRINTK;
575}
576
7b382efd
SR
577/* Returns true if the status of tr changed */
578static bool update_marker_trace(struct trace_array *tr, int enabled)
579{
580 lockdep_assert_held(&event_mutex);
581
582 if (enabled) {
583 if (!list_empty(&tr->marker_list))
584 return false;
585
586 list_add_rcu(&tr->marker_list, &marker_copies);
587 tr->trace_flags |= TRACE_ITER_COPY_MARKER;
588 return true;
589 }
590
591 if (list_empty(&tr->marker_list))
592 return false;
593
594 list_del_init(&tr->marker_list);
595 tr->trace_flags &= ~TRACE_ITER_COPY_MARKER;
596 return true;
597}
598
a1f157c7
ZY
599void trace_set_ring_buffer_expanded(struct trace_array *tr)
600{
601 if (!tr)
602 tr = &global_trace;
603 tr->ring_buffer_expanded = true;
604}
605
ae63b31e 606LIST_HEAD(ftrace_trace_arrays);
bc0c38d1 607
ff451961
SRRH
608int trace_array_get(struct trace_array *this_tr)
609{
610 struct trace_array *tr;
ff451961 611
d33b10c0 612 guard(mutex)(&trace_types_lock);
ff451961
SRRH
613 list_for_each_entry(tr, &ftrace_trace_arrays, list) {
614 if (tr == this_tr) {
615 tr->ref++;
d33b10c0 616 return 0;
ff451961
SRRH
617 }
618 }
ff451961 619
d33b10c0 620 return -ENODEV;
ff451961
SRRH
621}
622
623static void __trace_array_put(struct trace_array *this_tr)
624{
625 WARN_ON(!this_tr->ref);
626 this_tr->ref--;
627}
628
28879787
DI
629/**
630 * trace_array_put - Decrement the reference counter for this trace array.
557d50e7 631 * @this_tr : pointer to the trace array
28879787
DI
632 *
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
635 * destroyed.
636 *
637 */
ff451961
SRRH
638void trace_array_put(struct trace_array *this_tr)
639{
28879787
DI
640 if (!this_tr)
641 return;
642
ff451961
SRRH
643 mutex_lock(&trace_types_lock);
644 __trace_array_put(this_tr);
645 mutex_unlock(&trace_types_lock);
646}
28879787 647EXPORT_SYMBOL_GPL(trace_array_put);
ff451961 648
8530dec6
SRV
649int tracing_check_open_get_tr(struct trace_array *tr)
650{
17911ff3
SRV
651 int ret;
652
653 ret = security_locked_down(LOCKDOWN_TRACEFS);
654 if (ret)
655 return ret;
656
8530dec6
SRV
657 if (tracing_disabled)
658 return -ENODEV;
659
660 if (tr && trace_array_get(tr) < 0)
661 return -ENODEV;
662
663 return 0;
664}
665
d8275c45
SR
666/**
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
670 *
f2cc020d 671 * Returns true if @search_pid is found in @filtered_pids, and false otherwise.
d8275c45
SR
672 */
673bool
674trace_find_filtered_pid(struct trace_pid_list *filtered_pids, pid_t search_pid)
675{
6954e415 676 return trace_pid_list_is_set(filtered_pids, search_pid);
d8275c45
SR
677}
678
679/**
680 * trace_ignore_this_task - should a task be ignored for tracing
681 * @filtered_pids: The list of pids to check
b3ca59f6 682 * @filtered_no_pids: The list of pids not to be traced
d8275c45
SR
683 * @task: The task that should be ignored if not filtered
684 *
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.
688 */
689bool
b3b1e6ed
SRV
690trace_ignore_this_task(struct trace_pid_list *filtered_pids,
691 struct trace_pid_list *filtered_no_pids,
692 struct task_struct *task)
d8275c45
SR
693{
694 /*
f2cc020d 695 * If filtered_no_pids is not empty, and the task's pid is listed
b3b1e6ed
SRV
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.
d8275c45 700 */
d8275c45 701
b3b1e6ed
SRV
702 return (filtered_pids &&
703 !trace_find_filtered_pid(filtered_pids, task->pid)) ||
704 (filtered_no_pids &&
705 trace_find_filtered_pid(filtered_no_pids, task->pid));
d8275c45
SR
706}
707
708/**
f08367b3 709 * trace_filter_add_remove_task - Add or remove a task from a pid_list
d8275c45
SR
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
713 *
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
718 * of a task.
719 */
720void trace_filter_add_remove_task(struct trace_pid_list *pid_list,
721 struct task_struct *self,
722 struct task_struct *task)
723{
724 if (!pid_list)
725 return;
726
727 /* For forks, we only add if the forking task is listed */
728 if (self) {
729 if (!trace_find_filtered_pid(pid_list, self->pid))
730 return;
731 }
732
d8275c45
SR
733 /* "self" is set for forks, and NULL for exits */
734 if (self)
6954e415 735 trace_pid_list_set(pid_list, task->pid);
d8275c45 736 else
6954e415 737 trace_pid_list_clear(pid_list, task->pid);
d8275c45
SR
738}
739
5cc8976b
SRRH
740/**
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
745 *
746 * This is used by the seq_file "next" operation to iterate the pids
747 * listed in a trace_pid_list structure.
748 *
749 * Returns the pid+1 as we want to display pid of zero, but NULL would
750 * stop the iteration.
751 */
752void *trace_pid_next(struct trace_pid_list *pid_list, void *v, loff_t *pos)
753{
6954e415
SRV
754 long pid = (unsigned long)v;
755 unsigned int next;
5cc8976b
SRRH
756
757 (*pos)++;
758
f2cc020d 759 /* pid already is +1 of the actual previous bit */
6954e415
SRV
760 if (trace_pid_list_next(pid_list, pid, &next) < 0)
761 return NULL;
5cc8976b 762
6954e415 763 pid = next;
5cc8976b 764
6954e415
SRV
765 /* Return pid + 1 to allow zero to be represented */
766 return (void *)(pid + 1);
5cc8976b
SRRH
767}
768
769/**
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
773 *
774 * This is used by seq_file "start" operation to start the iteration
775 * of listing pids.
776 *
777 * Returns the pid+1 as we want to display pid of zero, but NULL would
778 * stop the iteration.
779 */
780void *trace_pid_start(struct trace_pid_list *pid_list, loff_t *pos)
781{
782 unsigned long pid;
6954e415 783 unsigned int first;
5cc8976b
SRRH
784 loff_t l = 0;
785
6954e415 786 if (trace_pid_list_first(pid_list, &first) < 0)
5cc8976b
SRRH
787 return NULL;
788
6954e415
SRV
789 pid = first;
790
5cc8976b
SRRH
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))
794 ;
795 return (void *)pid;
796}
797
798/**
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
802 *
803 * Can be directly used by seq_file operations to display the current
804 * pid value.
805 */
806int trace_pid_show(struct seq_file *m, void *v)
807{
808 unsigned long pid = (unsigned long)v - 1;
809
810 seq_printf(m, "%lu\n", pid);
811 return 0;
812}
813
76c813e2
SRRH
814/* 128 should be much more than enough */
815#define PID_BUF_SIZE 127
816
817int 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)
820{
821 struct trace_pid_list *pid_list;
822 struct trace_parser parser;
823 unsigned long val;
824 int nr_pids = 0;
825 ssize_t read = 0;
6954e415 826 ssize_t ret;
76c813e2
SRRH
827 loff_t pos;
828 pid_t pid;
829
830 if (trace_parser_get_init(&parser, PID_BUF_SIZE + 1))
831 return -ENOMEM;
832
833 /*
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
837 * not modified.
838 */
6954e415 839 pid_list = trace_pid_list_alloc();
91862cc7
WW
840 if (!pid_list) {
841 trace_parser_put(&parser);
76c813e2 842 return -ENOMEM;
91862cc7 843 }
76c813e2 844
76c813e2
SRRH
845 if (filtered_pids) {
846 /* copy the current bits to the new max */
6954e415
SRV
847 ret = trace_pid_list_first(filtered_pids, &pid);
848 while (!ret) {
849 trace_pid_list_set(pid_list, pid);
850 ret = trace_pid_list_next(filtered_pids, pid + 1, &pid);
76c813e2
SRRH
851 nr_pids++;
852 }
853 }
854
6954e415 855 ret = 0;
76c813e2
SRRH
856 while (cnt > 0) {
857
858 pos = 0;
859
860 ret = trace_get_user(&parser, ubuf, cnt, &pos);
b27f266f 861 if (ret < 0)
76c813e2
SRRH
862 break;
863
864 read += ret;
865 ubuf += ret;
866 cnt -= ret;
867
b27f266f
WY
868 if (!trace_parser_loaded(&parser))
869 break;
870
76c813e2
SRRH
871 ret = -EINVAL;
872 if (kstrtoul(parser.buffer, 0, &val))
873 break;
76c813e2
SRRH
874
875 pid = (pid_t)val;
876
6954e415
SRV
877 if (trace_pid_list_set(pid_list, pid) < 0) {
878 ret = -1;
879 break;
880 }
76c813e2
SRRH
881 nr_pids++;
882
883 trace_parser_clear(&parser);
884 ret = 0;
885 }
886 trace_parser_put(&parser);
887
888 if (ret < 0) {
6954e415 889 trace_pid_list_free(pid_list);
76c813e2
SRRH
890 return ret;
891 }
892
893 if (!nr_pids) {
894 /* Cleared the list of pids */
6954e415 895 trace_pid_list_free(pid_list);
76c813e2
SRRH
896 pid_list = NULL;
897 }
898
899 *new_pid_list = pid_list;
900
901 return read;
902}
903
1c5eb448 904static u64 buffer_ftrace_now(struct array_buffer *buf, int cpu)
37886f6a
SR
905{
906 u64 ts;
907
908 /* Early boot up does not have a buffer yet */
9457158b 909 if (!buf->buffer)
37886f6a
SR
910 return trace_clock_local();
911
f3ef7202 912 ts = ring_buffer_time_stamp(buf->buffer);
9457158b 913 ring_buffer_normalize_time_stamp(buf->buffer, cpu, &ts);
37886f6a
SR
914
915 return ts;
916}
bc0c38d1 917
a5a1d1c2 918u64 ftrace_now(int cpu)
9457158b 919{
1c5eb448 920 return buffer_ftrace_now(&global_trace.array_buffer, cpu);
9457158b
AL
921}
922
10246fa3 923/**
b3ca59f6 924 * tracing_is_enabled - Show if global_trace has been enabled
10246fa3
SRRH
925 *
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.
931 */
9036990d
SR
932int tracing_is_enabled(void)
933{
10246fa3
SRRH
934 /*
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.
938 */
939 smp_rmb();
940 return !global_trace.buffer_disabled;
9036990d
SR
941}
942
4fcdae83 943/*
3928a8a2
SR
944 * trace_buf_size is the size in bytes that is allocated
945 * for a buffer. Note, the number of bytes is always rounded
946 * to page size.
3f5a54e3
SR
947 *
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.
4fcdae83 952 */
3928a8a2 953#define TRACE_BUF_SIZE_DEFAULT 1441792UL /* 16384 * 88 (sizeof(entry)) */
3f5a54e3 954
3928a8a2 955static unsigned long trace_buf_size = TRACE_BUF_SIZE_DEFAULT;
bc0c38d1 956
4fcdae83 957/* trace_types holds a link list of available tracers. */
bc0c38d1 958static struct tracer *trace_types __read_mostly;
4fcdae83 959
4fcdae83
SR
960/*
961 * trace_types_lock is used to protect the trace_types list.
4fcdae83 962 */
a8227415 963DEFINE_MUTEX(trace_types_lock);
4fcdae83 964
7e53bd42
LJ
965/*
966 * serialize the access of the ring buffer
967 *
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.
971 *
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
f2cc020d 975 * (not reader page) in ring buffer, and this page will be rewritten
7e53bd42
LJ
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.
979 *
980 * These primitives allow multi process access to different cpu ring buffer
981 * concurrently.
982 *
983 * These primitives don't distinguish read-only and read-consume access.
984 * Multi read-only access are also serialized.
985 */
986
987#ifdef CONFIG_SMP
988static DECLARE_RWSEM(all_cpu_access_lock);
989static DEFINE_PER_CPU(struct mutex, cpu_access_lock);
990
991static inline void trace_access_lock(int cpu)
992{
ae3b5093 993 if (cpu == RING_BUFFER_ALL_CPUS) {
7e53bd42
LJ
994 /* gain it for accessing the whole ring buffer. */
995 down_write(&all_cpu_access_lock);
996 } else {
997 /* gain it for accessing a cpu ring buffer. */
998
ae3b5093 999 /* Firstly block other trace_access_lock(RING_BUFFER_ALL_CPUS). */
7e53bd42
LJ
1000 down_read(&all_cpu_access_lock);
1001
1002 /* Secondly block other access to this @cpu ring buffer. */
1003 mutex_lock(&per_cpu(cpu_access_lock, cpu));
1004 }
1005}
1006
1007static inline void trace_access_unlock(int cpu)
1008{
ae3b5093 1009 if (cpu == RING_BUFFER_ALL_CPUS) {
7e53bd42
LJ
1010 up_write(&all_cpu_access_lock);
1011 } else {
1012 mutex_unlock(&per_cpu(cpu_access_lock, cpu));
1013 up_read(&all_cpu_access_lock);
1014 }
1015}
1016
1017static inline void trace_access_lock_init(void)
1018{
1019 int cpu;
1020
1021 for_each_possible_cpu(cpu)
1022 mutex_init(&per_cpu(cpu_access_lock, cpu));
1023}
1024
1025#else
1026
1027static DEFINE_MUTEX(access_lock);
1028
1029static inline void trace_access_lock(int cpu)
1030{
1031 (void)cpu;
1032 mutex_lock(&access_lock);
1033}
1034
1035static inline void trace_access_unlock(int cpu)
1036{
1037 (void)cpu;
1038 mutex_unlock(&access_lock);
1039}
1040
1041static inline void trace_access_lock_init(void)
1042{
1043}
1044
1045#endif
1046
d78a4614 1047#ifdef CONFIG_STACKTRACE
6ce5a6f0
T
1048static void __ftrace_trace_stack(struct trace_array *tr,
1049 struct trace_buffer *buffer,
36590c50
SAS
1050 unsigned int trace_ctx,
1051 int skip, struct pt_regs *regs);
2d34f489 1052static inline void ftrace_trace_stack(struct trace_array *tr,
13292494 1053 struct trace_buffer *buffer,
36590c50
SAS
1054 unsigned int trace_ctx,
1055 int skip, struct pt_regs *regs);
ca475e83 1056
d78a4614 1057#else
6ce5a6f0
T
1058static inline void __ftrace_trace_stack(struct trace_array *tr,
1059 struct trace_buffer *buffer,
36590c50
SAS
1060 unsigned int trace_ctx,
1061 int skip, struct pt_regs *regs)
d78a4614
SRRH
1062{
1063}
2d34f489 1064static inline void ftrace_trace_stack(struct trace_array *tr,
13292494 1065 struct trace_buffer *buffer,
36590c50
SAS
1066 unsigned long trace_ctx,
1067 int skip, struct pt_regs *regs)
ca475e83
SRRH
1068{
1069}
1070
d78a4614
SRRH
1071#endif
1072
3e9a8aad
SRRH
1073static __always_inline void
1074trace_event_setup(struct ring_buffer_event *event,
36590c50 1075 int type, unsigned int trace_ctx)
3e9a8aad
SRRH
1076{
1077 struct trace_entry *ent = ring_buffer_event_data(event);
1078
36590c50 1079 tracing_generic_entry_update(ent, type, trace_ctx);
3e9a8aad
SRRH
1080}
1081
1082static __always_inline struct ring_buffer_event *
13292494 1083__trace_buffer_lock_reserve(struct trace_buffer *buffer,
3e9a8aad
SRRH
1084 int type,
1085 unsigned long len,
36590c50 1086 unsigned int trace_ctx)
3e9a8aad
SRRH
1087{
1088 struct ring_buffer_event *event;
1089
1090 event = ring_buffer_lock_reserve(buffer, len);
1091 if (event != NULL)
36590c50 1092 trace_event_setup(event, type, trace_ctx);
3e9a8aad
SRRH
1093
1094 return event;
1095}
1096
2290f2c5 1097void tracer_tracing_on(struct trace_array *tr)
10246fa3 1098{
1c5eb448
SRV
1099 if (tr->array_buffer.buffer)
1100 ring_buffer_record_on(tr->array_buffer.buffer);
10246fa3
SRRH
1101 /*
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.
1108 */
1109 tr->buffer_disabled = 0;
1110 /* Make the flag seen by readers */
1111 smp_wmb();
1112}
1113
499e5470
SR
1114/**
1115 * tracing_on - enable tracing buffers
1116 *
1117 * This function enables tracing buffers that may have been
1118 * disabled with tracing_off.
1119 */
1120void tracing_on(void)
1121{
10246fa3 1122 tracer_tracing_on(&global_trace);
499e5470
SR
1123}
1124EXPORT_SYMBOL_GPL(tracing_on);
1125
52ffabe3
SRRH
1126
1127static __always_inline void
13292494 1128__buffer_unlock_commit(struct trace_buffer *buffer, struct ring_buffer_event *event)
52ffabe3 1129{
d914ba37 1130 __this_cpu_write(trace_taskinfo_save, true);
52ffabe3
SRRH
1131
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);
6c536d76
SRV
1138 /* ring_buffer_unlock_commit() enables preemption */
1139 preempt_enable_notrace();
52ffabe3 1140 } else
04aabc32 1141 ring_buffer_unlock_commit(buffer);
52ffabe3
SRRH
1142}
1143
d503b8f7
SRG
1144int __trace_array_puts(struct trace_array *tr, unsigned long ip,
1145 const char *str, int size)
09ae7234
SRRH
1146{
1147 struct ring_buffer_event *event;
13292494 1148 struct trace_buffer *buffer;
09ae7234 1149 struct print_entry *entry;
36590c50 1150 unsigned int trace_ctx;
09ae7234 1151 int alloc;
8abfb872 1152
d503b8f7 1153 if (!(tr->trace_flags & TRACE_ITER_PRINTK))
f0160a5a
J
1154 return 0;
1155
ac9d2cb1
SRG
1156 if (unlikely(tracing_selftest_running && tr == &global_trace))
1157 return 0;
1158
1159 if (unlikely(tracing_disabled))
3132e107
SRRH
1160 return 0;
1161
09ae7234
SRRH
1162 alloc = sizeof(*entry) + size + 2; /* possible \n added */
1163
36590c50 1164 trace_ctx = tracing_gen_ctx();
d503b8f7 1165 buffer = tr->array_buffer.buffer;
82d1b815 1166 ring_buffer_nest_start(buffer);
36590c50
SAS
1167 event = __trace_buffer_lock_reserve(buffer, TRACE_PRINT, alloc,
1168 trace_ctx);
82d1b815
SRV
1169 if (!event) {
1170 size = 0;
1171 goto out;
1172 }
09ae7234
SRRH
1173
1174 entry = ring_buffer_event_data(event);
1175 entry->ip = ip;
1176
1177 memcpy(&entry->buf, str, size);
1178
1179 /* Add a newline if necessary */
1180 if (entry->buf[size - 1] != '\n') {
1181 entry->buf[size] = '\n';
1182 entry->buf[size + 1] = '\0';
1183 } else
1184 entry->buf[size] = '\0';
1185
1186 __buffer_unlock_commit(buffer, event);
d503b8f7 1187 ftrace_trace_stack(tr, buffer, trace_ctx, 4, NULL);
82d1b815
SRV
1188 out:
1189 ring_buffer_nest_end(buffer);
09ae7234
SRRH
1190 return size;
1191}
d503b8f7
SRG
1192EXPORT_SYMBOL_GPL(__trace_array_puts);
1193
1194/**
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.
1199 */
1200int __trace_puts(unsigned long ip, const char *str, int size)
1201{
ddb8ea9e 1202 return __trace_array_puts(printk_trace, ip, str, size);
d503b8f7 1203}
09ae7234
SRRH
1204EXPORT_SYMBOL_GPL(__trace_puts);
1205
1206/**
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
1210 */
1211int __trace_bputs(unsigned long ip, const char *str)
1212{
9b7bdf6f 1213 struct trace_array *tr = READ_ONCE(printk_trace);
09ae7234 1214 struct ring_buffer_event *event;
13292494 1215 struct trace_buffer *buffer;
09ae7234 1216 struct bputs_entry *entry;
36590c50 1217 unsigned int trace_ctx;
09ae7234 1218 int size = sizeof(struct bputs_entry);
82d1b815 1219 int ret = 0;
8abfb872 1220
9b7bdf6f
SR
1221 if (!printk_binsafe(tr))
1222 return __trace_puts(ip, str, strlen(str));
1223
ddb8ea9e 1224 if (!(tr->trace_flags & TRACE_ITER_PRINTK))
f0160a5a
J
1225 return 0;
1226
3132e107
SRRH
1227 if (unlikely(tracing_selftest_running || tracing_disabled))
1228 return 0;
1229
36590c50 1230 trace_ctx = tracing_gen_ctx();
ddb8ea9e 1231 buffer = tr->array_buffer.buffer;
82d1b815
SRV
1232
1233 ring_buffer_nest_start(buffer);
3e9a8aad 1234 event = __trace_buffer_lock_reserve(buffer, TRACE_BPUTS, size,
36590c50 1235 trace_ctx);
09ae7234 1236 if (!event)
82d1b815 1237 goto out;
09ae7234
SRRH
1238
1239 entry = ring_buffer_event_data(event);
1240 entry->ip = ip;
1241 entry->str = str;
1242
1243 __buffer_unlock_commit(buffer, event);
ddb8ea9e 1244 ftrace_trace_stack(tr, buffer, trace_ctx, 4, NULL);
09ae7234 1245
82d1b815
SRV
1246 ret = 1;
1247 out:
1248 ring_buffer_nest_end(buffer);
1249 return ret;
09ae7234
SRRH
1250}
1251EXPORT_SYMBOL_GPL(__trace_bputs);
1252
ad909e21 1253#ifdef CONFIG_TRACER_SNAPSHOT
192b7993
ZW
1254static void tracing_snapshot_instance_cond(struct trace_array *tr,
1255 void *cond_data)
ad909e21 1256{
ad909e21
SRRH
1257 struct tracer *tracer = tr->current_trace;
1258 unsigned long flags;
1259
1b22e382 1260 if (in_nmi()) {
9d52727f
SRG
1261 trace_array_puts(tr, "*** SNAPSHOT CALLED FROM NMI CONTEXT ***\n");
1262 trace_array_puts(tr, "*** snapshot is being ignored ***\n");
1b22e382
SRRH
1263 return;
1264 }
1265
ad909e21 1266 if (!tr->allocated_snapshot) {
9d52727f
SRG
1267 trace_array_puts(tr, "*** SNAPSHOT NOT ALLOCATED ***\n");
1268 trace_array_puts(tr, "*** stopping trace here! ***\n");
1269 tracer_tracing_off(tr);
ad909e21
SRRH
1270 return;
1271 }
1272
1273 /* Note, snapshot can not be used when the tracer uses it */
1274 if (tracer->use_max_tr) {
9d52727f
SRG
1275 trace_array_puts(tr, "*** LATENCY TRACER ACTIVE ***\n");
1276 trace_array_puts(tr, "*** Can not use snapshot (sorry) ***\n");
ad909e21
SRRH
1277 return;
1278 }
1279
cf9f0f7c
VD
1280 if (tr->mapped) {
1281 trace_array_puts(tr, "*** BUFFER MEMORY MAPPED ***\n");
1282 trace_array_puts(tr, "*** Can not use snapshot (sorry) ***\n");
1283 return;
1284 }
1285
ad909e21 1286 local_irq_save(flags);
a35873a0 1287 update_max_tr(tr, current, smp_processor_id(), cond_data);
ad909e21
SRRH
1288 local_irq_restore(flags);
1289}
cab50379 1290
a35873a0
TZ
1291void tracing_snapshot_instance(struct trace_array *tr)
1292{
1293 tracing_snapshot_instance_cond(tr, NULL);
1294}
1295
cab50379 1296/**
5a93bae2 1297 * tracing_snapshot - take a snapshot of the current buffer.
cab50379
SRV
1298 *
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.
1302 *
1303 * Note, make sure to allocate the snapshot with either
1304 * a tracing_snapshot_alloc(), or by doing it manually
2455f0e1 1305 * with: echo 1 > /sys/kernel/tracing/snapshot
cab50379
SRV
1306 *
1307 * If the snapshot buffer is not allocated, it will stop tracing.
1308 * Basically making a permanent snapshot.
1309 */
1310void tracing_snapshot(void)
1311{
1312 struct trace_array *tr = &global_trace;
1313
1314 tracing_snapshot_instance(tr);
1315}
1b22e382 1316EXPORT_SYMBOL_GPL(tracing_snapshot);
ad909e21 1317
a35873a0
TZ
1318/**
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
1322 *
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.
1330 */
1331void tracing_snapshot_cond(struct trace_array *tr, void *cond_data)
1332{
1333 tracing_snapshot_instance_cond(tr, cond_data);
1334}
1335EXPORT_SYMBOL_GPL(tracing_snapshot_cond);
1336
1337/**
3b57d847 1338 * tracing_cond_snapshot_data - get the user data associated with a snapshot
a35873a0
TZ
1339 * @tr: The tracing instance
1340 *
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.
1344 *
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.
1348 *
1349 * Returns the cond_data associated with the trace array's snapshot.
1350 */
1351void *tracing_cond_snapshot_data(struct trace_array *tr)
1352{
1353 void *cond_data = NULL;
1354
c0a581d7 1355 local_irq_disable();
a35873a0
TZ
1356 arch_spin_lock(&tr->max_lock);
1357
1358 if (tr->cond_snapshot)
1359 cond_data = tr->cond_snapshot->cond_data;
1360
1361 arch_spin_unlock(&tr->max_lock);
c0a581d7 1362 local_irq_enable();
a35873a0
TZ
1363
1364 return cond_data;
1365}
1366EXPORT_SYMBOL_GPL(tracing_cond_snapshot_data);
1367
1c5eb448
SRV
1368static int resize_buffer_duplicate_size(struct array_buffer *trace_buf,
1369 struct array_buffer *size_buf, int cpu_id);
1370static void set_buffer_entries(struct array_buffer *buf, unsigned long val);
3209cff4 1371
2824f503 1372int tracing_alloc_snapshot_instance(struct trace_array *tr)
3209cff4 1373{
aa067682 1374 int order;
3209cff4
SRRH
1375 int ret;
1376
1377 if (!tr->allocated_snapshot) {
1378
aa067682
SRG
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);
1382 if (ret < 0)
1383 return ret;
1384
3209cff4
SRRH
1385 /* allocate spare buffer */
1386 ret = resize_buffer_duplicate_size(&tr->max_buffer,
1c5eb448 1387 &tr->array_buffer, RING_BUFFER_ALL_CPUS);
3209cff4
SRRH
1388 if (ret < 0)
1389 return ret;
1390
1391 tr->allocated_snapshot = true;
1392 }
1393
1394 return 0;
1395}
1396
ad1438a0 1397static void free_snapshot(struct trace_array *tr)
3209cff4
SRRH
1398{
1399 /*
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.
1403 */
aa067682 1404 ring_buffer_subbuf_order_set(tr->max_buffer.buffer, 0);
3209cff4
SRRH
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;
1409}
ad909e21 1410
180e4e39
VD
1411static int tracing_arm_snapshot_locked(struct trace_array *tr)
1412{
1413 int ret;
1414
1415 lockdep_assert_held(&trace_types_lock);
1416
1417 spin_lock(&tr->snapshot_trigger_lock);
cf9f0f7c 1418 if (tr->snapshot == UINT_MAX || tr->mapped) {
180e4e39
VD
1419 spin_unlock(&tr->snapshot_trigger_lock);
1420 return -EBUSY;
1421 }
1422
1423 tr->snapshot++;
1424 spin_unlock(&tr->snapshot_trigger_lock);
1425
1426 ret = tracing_alloc_snapshot_instance(tr);
1427 if (ret) {
1428 spin_lock(&tr->snapshot_trigger_lock);
1429 tr->snapshot--;
1430 spin_unlock(&tr->snapshot_trigger_lock);
1431 }
1432
1433 return ret;
1434}
1435
1436int tracing_arm_snapshot(struct trace_array *tr)
1437{
1438 int ret;
1439
1440 mutex_lock(&trace_types_lock);
1441 ret = tracing_arm_snapshot_locked(tr);
1442 mutex_unlock(&trace_types_lock);
1443
1444 return ret;
1445}
1446
1447void tracing_disarm_snapshot(struct trace_array *tr)
1448{
1449 spin_lock(&tr->snapshot_trigger_lock);
1450 if (!WARN_ON(!tr->snapshot))
1451 tr->snapshot--;
1452 spin_unlock(&tr->snapshot_trigger_lock);
1453}
1454
93e31ffb
TZ
1455/**
1456 * tracing_alloc_snapshot - allocate snapshot buffer.
1457 *
1458 * This only allocates the snapshot buffer if it isn't already
1459 * allocated - it doesn't also take a snapshot.
1460 *
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.
1464 */
1465int tracing_alloc_snapshot(void)
1466{
1467 struct trace_array *tr = &global_trace;
1468 int ret;
1469
2824f503 1470 ret = tracing_alloc_snapshot_instance(tr);
93e31ffb
TZ
1471 WARN_ON(ret < 0);
1472
1473 return ret;
1474}
1475EXPORT_SYMBOL_GPL(tracing_alloc_snapshot);
1476
ad909e21 1477/**
5a93bae2 1478 * tracing_snapshot_alloc - allocate and take a snapshot of the current buffer.
ad909e21 1479 *
5a93bae2 1480 * This is similar to tracing_snapshot(), but it will allocate the
ad909e21
SRRH
1481 * snapshot buffer if it isn't already allocated. Use this only
1482 * where it is safe to sleep, as the allocation may sleep.
1483 *
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.
1487 */
1488void tracing_snapshot_alloc(void)
1489{
ad909e21
SRRH
1490 int ret;
1491
93e31ffb
TZ
1492 ret = tracing_alloc_snapshot();
1493 if (ret < 0)
3209cff4 1494 return;
ad909e21
SRRH
1495
1496 tracing_snapshot();
1497}
1b22e382 1498EXPORT_SYMBOL_GPL(tracing_snapshot_alloc);
a35873a0
TZ
1499
1500/**
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
1505 *
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.
1510 *
1511 * Returns 0 if successful, error otherwise.
1512 */
1513int tracing_snapshot_cond_enable(struct trace_array *tr, void *cond_data,
1514 cond_update_fn_t update)
1515{
d33b10c0
SR
1516 struct cond_snapshot *cond_snapshot __free(kfree) =
1517 kzalloc(sizeof(*cond_snapshot), GFP_KERNEL);
1518 int ret;
a35873a0 1519
a35873a0
TZ
1520 if (!cond_snapshot)
1521 return -ENOMEM;
1522
1523 cond_snapshot->cond_data = cond_data;
1524 cond_snapshot->update = update;
1525
d33b10c0 1526 guard(mutex)(&trace_types_lock);
a35873a0 1527
d33b10c0
SR
1528 if (tr->current_trace->use_max_tr)
1529 return -EBUSY;
a35873a0 1530
1c347a94
SRV
1531 /*
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.
1538 */
d33b10c0
SR
1539 if (tr->cond_snapshot)
1540 return -EBUSY;
a35873a0 1541
180e4e39
VD
1542 ret = tracing_arm_snapshot_locked(tr);
1543 if (ret)
d33b10c0 1544 return ret;
180e4e39 1545
c0a581d7 1546 local_irq_disable();
a35873a0 1547 arch_spin_lock(&tr->max_lock);
d33b10c0 1548 tr->cond_snapshot = no_free_ptr(cond_snapshot);
a35873a0 1549 arch_spin_unlock(&tr->max_lock);
c0a581d7 1550 local_irq_enable();
a35873a0 1551
d33b10c0 1552 return 0;
a35873a0
TZ
1553}
1554EXPORT_SYMBOL_GPL(tracing_snapshot_cond_enable);
1555
1556/**
1557 * tracing_snapshot_cond_disable - disable conditional snapshot for an instance
1558 * @tr: The tracing instance
1559 *
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.
1563 *
1564 * Returns 0 if successful, error otherwise.
1565 */
1566int tracing_snapshot_cond_disable(struct trace_array *tr)
1567{
1568 int ret = 0;
1569
c0a581d7 1570 local_irq_disable();
a35873a0
TZ
1571 arch_spin_lock(&tr->max_lock);
1572
1573 if (!tr->cond_snapshot)
1574 ret = -EINVAL;
1575 else {
1576 kfree(tr->cond_snapshot);
1577 tr->cond_snapshot = NULL;
1578 }
1579
1580 arch_spin_unlock(&tr->max_lock);
c0a581d7 1581 local_irq_enable();
a35873a0 1582
180e4e39
VD
1583 tracing_disarm_snapshot(tr);
1584
a35873a0
TZ
1585 return ret;
1586}
1587EXPORT_SYMBOL_GPL(tracing_snapshot_cond_disable);
ad909e21
SRRH
1588#else
1589void tracing_snapshot(void)
1590{
1591 WARN_ONCE(1, "Snapshot feature not enabled, but internal snapshot used");
1592}
1b22e382 1593EXPORT_SYMBOL_GPL(tracing_snapshot);
a35873a0
TZ
1594void tracing_snapshot_cond(struct trace_array *tr, void *cond_data)
1595{
1596 WARN_ONCE(1, "Snapshot feature not enabled, but internal conditional snapshot used");
1597}
1598EXPORT_SYMBOL_GPL(tracing_snapshot_cond);
93e31ffb
TZ
1599int tracing_alloc_snapshot(void)
1600{
1601 WARN_ONCE(1, "Snapshot feature not enabled, but snapshot allocation used");
1602 return -ENODEV;
1603}
1604EXPORT_SYMBOL_GPL(tracing_alloc_snapshot);
ad909e21
SRRH
1605void tracing_snapshot_alloc(void)
1606{
1607 /* Give warning */
1608 tracing_snapshot();
1609}
1b22e382 1610EXPORT_SYMBOL_GPL(tracing_snapshot_alloc);
a35873a0
TZ
1611void *tracing_cond_snapshot_data(struct trace_array *tr)
1612{
1613 return NULL;
1614}
1615EXPORT_SYMBOL_GPL(tracing_cond_snapshot_data);
1616int tracing_snapshot_cond_enable(struct trace_array *tr, void *cond_data, cond_update_fn_t update)
1617{
1618 return -ENODEV;
1619}
1620EXPORT_SYMBOL_GPL(tracing_snapshot_cond_enable);
1621int tracing_snapshot_cond_disable(struct trace_array *tr)
1622{
1623 return false;
1624}
1625EXPORT_SYMBOL_GPL(tracing_snapshot_cond_disable);
e25e43a4 1626#define free_snapshot(tr) do { } while (0)
180e4e39 1627#define tracing_arm_snapshot_locked(tr) ({ -EBUSY; })
ad909e21
SRRH
1628#endif /* CONFIG_TRACER_SNAPSHOT */
1629
2290f2c5 1630void tracer_tracing_off(struct trace_array *tr)
10246fa3 1631{
1c5eb448
SRV
1632 if (tr->array_buffer.buffer)
1633 ring_buffer_record_off(tr->array_buffer.buffer);
10246fa3
SRRH
1634 /*
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.
1641 */
1642 tr->buffer_disabled = 1;
1643 /* Make the flag seen by readers */
1644 smp_wmb();
1645}
1646
dbecef68
SR
1647/**
1648 * tracer_tracing_disable() - temporary disable the buffer from write
1649 * @tr: The trace array to disable its buffer for
1650 *
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
1655 * enable it.
1656 */
1657void tracer_tracing_disable(struct trace_array *tr)
1658{
1659 if (WARN_ON_ONCE(!tr->array_buffer.buffer))
1660 return;
1661
1662 ring_buffer_record_disable(tr->array_buffer.buffer);
1663}
1664
1665/**
1666 * tracer_tracing_enable() - counter part of tracer_tracing_disable()
1667 * @tr: The trace array that had tracer_tracincg_disable() called on it
1668 *
1669 * This is called after tracer_tracing_disable() has been called on @tr,
1670 * when it's safe to re-enable tracing.
1671 */
1672void tracer_tracing_enable(struct trace_array *tr)
1673{
1674 if (WARN_ON_ONCE(!tr->array_buffer.buffer))
1675 return;
1676
1677 ring_buffer_record_enable(tr->array_buffer.buffer);
1678}
1679
499e5470
SR
1680/**
1681 * tracing_off - turn off tracing buffers
1682 *
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.
1687 */
1688void tracing_off(void)
1689{
10246fa3 1690 tracer_tracing_off(&global_trace);
499e5470
SR
1691}
1692EXPORT_SYMBOL_GPL(tracing_off);
1693
de7edd31
SRRH
1694void disable_trace_on_warning(void)
1695{
c200784a
SRV
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");
de7edd31 1699 tracing_off();
c200784a 1700 }
de7edd31
SRRH
1701}
1702
10246fa3
SRRH
1703/**
1704 * tracer_tracing_is_on - show real state of ring buffer enabled
1705 * @tr : the trace array to know if ring buffer is enabled
1706 *
1707 * Shows real state of the ring buffer if it is enabled or not.
1708 */
ec573508 1709bool tracer_tracing_is_on(struct trace_array *tr)
10246fa3 1710{
1c5eb448 1711 if (tr->array_buffer.buffer)
a6eaa24f 1712 return ring_buffer_record_is_set_on(tr->array_buffer.buffer);
10246fa3
SRRH
1713 return !tr->buffer_disabled;
1714}
1715
499e5470
SR
1716/**
1717 * tracing_is_on - show state of ring buffers enabled
1718 */
1719int tracing_is_on(void)
1720{
10246fa3 1721 return tracer_tracing_is_on(&global_trace);
499e5470
SR
1722}
1723EXPORT_SYMBOL_GPL(tracing_is_on);
1724
3928a8a2 1725static int __init set_buf_size(char *str)
bc0c38d1 1726{
3928a8a2 1727 unsigned long buf_size;
c6caeeb1 1728
bc0c38d1
SR
1729 if (!str)
1730 return 0;
9d612bef 1731 buf_size = memparse(str, &str);
7acf3a12
SS
1732 /*
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.
1736 */
1737 trace_buf_size = max(4096UL, buf_size);
bc0c38d1
SR
1738 return 1;
1739}
3928a8a2 1740__setup("trace_buf_size=", set_buf_size);
bc0c38d1 1741
0e950173
TB
1742static int __init set_tracing_thresh(char *str)
1743{
87abb3b1 1744 unsigned long threshold;
0e950173
TB
1745 int ret;
1746
1747 if (!str)
1748 return 0;
bcd83ea6 1749 ret = kstrtoul(str, 0, &threshold);
0e950173
TB
1750 if (ret < 0)
1751 return 0;
87abb3b1 1752 tracing_thresh = threshold * 1000;
0e950173
TB
1753 return 1;
1754}
1755__setup("tracing_thresh=", set_tracing_thresh);
1756
57f50be1
SR
1757unsigned long nsecs_to_usecs(unsigned long nsecs)
1758{
1759 return nsecs / 1000;
1760}
1761
a3418a36
SRRH
1762/*
1763 * TRACE_FLAGS is defined as a tuple matching bit masks with strings.
f57a4143 1764 * It uses C(a, b) where 'a' is the eval (enum) name and 'b' is the string that
a3418a36 1765 * matches it. By defining "C(a, b) b", TRACE_FLAGS becomes a list
f57a4143 1766 * of strings in the order that the evals (enum) were defined.
a3418a36
SRRH
1767 */
1768#undef C
1769#define C(a, b) b
1770
f2cc020d 1771/* These must match the bit positions in trace_iterator_flags */
bc0c38d1 1772static const char *trace_options[] = {
a3418a36 1773 TRACE_FLAGS
bc0c38d1
SR
1774 NULL
1775};
1776
5079f326
Z
1777static struct {
1778 u64 (*func)(void);
1779 const char *name;
8be0709f 1780 int in_ns; /* is this clock in nanoseconds? */
5079f326 1781} trace_clocks[] = {
1b3e5c09
TG
1782 { trace_clock_local, "local", 1 },
1783 { trace_clock_global, "global", 1 },
1784 { trace_clock_counter, "counter", 0 },
e7fda6c4 1785 { trace_clock_jiffies, "uptime", 0 },
1b3e5c09
TG
1786 { trace_clock, "perf", 1 },
1787 { ktime_get_mono_fast_ns, "mono", 1 },
aabfa5f2 1788 { ktime_get_raw_fast_ns, "mono_raw", 1 },
a3ed0e43 1789 { ktime_get_boot_fast_ns, "boot", 1 },
c575afe2 1790 { ktime_get_tai_fast_ns, "tai", 1 },
8cbd9cc6 1791 ARCH_TRACE_CLOCKS
5079f326
Z
1792};
1793
860f9f6b
TZ
1794bool trace_clock_in_ns(struct trace_array *tr)
1795{
1796 if (trace_clocks[tr->clock_id].in_ns)
1797 return true;
1798
1799 return false;
1800}
1801
b63f39ea 1802/*
1803 * trace_parser_get_init - gets the buffer for trace parser
1804 */
1805int trace_parser_get_init(struct trace_parser *parser, int size)
1806{
1807 memset(parser, 0, sizeof(*parser));
1808
1809 parser->buffer = kmalloc(size, GFP_KERNEL);
1810 if (!parser->buffer)
1811 return 1;
1812
1813 parser->size = size;
1814 return 0;
1815}
1816
1817/*
1818 * trace_parser_put - frees the buffer for trace parser
1819 */
1820void trace_parser_put(struct trace_parser *parser)
1821{
1822 kfree(parser->buffer);
0e684b65 1823 parser->buffer = NULL;
b63f39ea 1824}
1825
1826/*
1827 * trace_get_user - reads the user input string separated by space
1828 * (matched by isspace(ch))
1829 *
1830 * For each string found the 'struct trace_parser' is updated,
1831 * and the function returns.
1832 *
1833 * Returns number of bytes read.
1834 *
1835 * See kernel/trace/trace.h for 'struct trace_parser' details.
1836 */
1837int trace_get_user(struct trace_parser *parser, const char __user *ubuf,
1838 size_t cnt, loff_t *ppos)
1839{
1840 char ch;
1841 size_t read = 0;
1842 ssize_t ret;
1843
1844 if (!*ppos)
1845 trace_parser_clear(parser);
1846
1847 ret = get_user(ch, ubuf++);
1848 if (ret)
1849 goto out;
1850
1851 read++;
1852 cnt--;
1853
1854 /*
1855 * The parser is not finished with the last write,
1856 * continue reading the user input without skipping spaces.
1857 */
1858 if (!parser->cont) {
1859 /* skip white space */
1860 while (cnt && isspace(ch)) {
1861 ret = get_user(ch, ubuf++);
1862 if (ret)
1863 goto out;
1864 read++;
1865 cnt--;
1866 }
1867
76638d96
CD
1868 parser->idx = 0;
1869
b63f39ea 1870 /* only spaces were written */
921a7acd 1871 if (isspace(ch) || !ch) {
b63f39ea 1872 *ppos += read;
1873 ret = read;
1874 goto out;
1875 }
b63f39ea 1876 }
1877
1878 /* read the non-space input */
921a7acd 1879 while (cnt && !isspace(ch) && ch) {
3c235a33 1880 if (parser->idx < parser->size - 1)
b63f39ea 1881 parser->buffer[parser->idx++] = ch;
1882 else {
1883 ret = -EINVAL;
1884 goto out;
1885 }
1886 ret = get_user(ch, ubuf++);
1887 if (ret)
1888 goto out;
1889 read++;
1890 cnt--;
1891 }
1892
1893 /* We either got finished input or we have to wait for another call. */
921a7acd 1894 if (isspace(ch) || !ch) {
b63f39ea 1895 parser->buffer[parser->idx] = 0;
1896 parser->cont = false;
057db848 1897 } else if (parser->idx < parser->size - 1) {
b63f39ea 1898 parser->cont = true;
1899 parser->buffer[parser->idx++] = ch;
f4d0706c
CD
1900 /* Make sure the parsed string always terminates with '\0'. */
1901 parser->buffer[parser->idx] = 0;
057db848
SR
1902 } else {
1903 ret = -EINVAL;
1904 goto out;
b63f39ea 1905 }
1906
1907 *ppos += read;
1908 ret = read;
1909
1910out:
1911 return ret;
1912}
1913
3a161d99 1914/* TODO add a seq_buf_to_buffer() */
b8b94265 1915static ssize_t trace_seq_to_buffer(struct trace_seq *s, void *buf, size_t cnt)
3c56819b
EGM
1916{
1917 int len;
3c56819b 1918
d0ed46b6 1919 if (trace_seq_used(s) <= s->readpos)
3c56819b
EGM
1920 return -EBUSY;
1921
d0ed46b6 1922 len = trace_seq_used(s) - s->readpos;
3c56819b
EGM
1923 if (cnt > len)
1924 cnt = len;
d0ed46b6 1925 memcpy(buf, s->buffer + s->readpos, cnt);
3c56819b 1926
d0ed46b6 1927 s->readpos += cnt;
3c56819b
EGM
1928 return cnt;
1929}
1930
0e950173 1931unsigned long __read_mostly tracing_thresh;
e25e43a4
MHG
1932
1933#ifdef CONFIG_TRACER_MAX_TRACE
91edde2e
VRB
1934static const struct file_operations tracing_max_lat_fops;
1935
6880c987 1936#ifdef LATENCY_FS_NOTIFY
91edde2e
VRB
1937
1938static struct workqueue_struct *fsnotify_wq;
1939
1940static void latency_fsnotify_workfn(struct work_struct *work)
1941{
1942 struct trace_array *tr = container_of(work, struct trace_array,
1943 fsnotify_work);
82ace1ef 1944 fsnotify_inode(tr->d_max_latency->d_inode, FS_MODIFY);
91edde2e
VRB
1945}
1946
1947static void latency_fsnotify_workfn_irq(struct irq_work *iwork)
1948{
1949 struct trace_array *tr = container_of(iwork, struct trace_array,
1950 fsnotify_irqwork);
1951 queue_work(fsnotify_wq, &tr->fsnotify_work);
1952}
1953
1954static void trace_create_maxlat_file(struct trace_array *tr,
1955 struct dentry *d_tracer)
1956{
1957 INIT_WORK(&tr->fsnotify_work, latency_fsnotify_workfn);
1958 init_irq_work(&tr->fsnotify_irqwork, latency_fsnotify_workfn_irq);
21ccc9cd
SRV
1959 tr->d_max_latency = trace_create_file("tracing_max_latency",
1960 TRACE_MODE_WRITE,
7d660c9b 1961 d_tracer, tr,
91edde2e
VRB
1962 &tracing_max_lat_fops);
1963}
1964
1965__init static int latency_fsnotify_init(void)
1966{
1967 fsnotify_wq = alloc_workqueue("tr_max_lat_wq",
1968 WQ_UNBOUND | WQ_HIGHPRI, 0);
1969 if (!fsnotify_wq) {
1970 pr_err("Unable to allocate tr_max_lat_wq\n");
1971 return -ENOMEM;
1972 }
1973 return 0;
1974}
1975
1976late_initcall_sync(latency_fsnotify_init);
1977
1978void latency_fsnotify(struct trace_array *tr)
1979{
1980 if (!fsnotify_wq)
1981 return;
1982 /*
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.
1986 */
1987 irq_work_queue(&tr->fsnotify_irqwork);
1988}
1989
e25e43a4 1990#else /* !LATENCY_FS_NOTIFY */
91edde2e
VRB
1991
1992#define trace_create_maxlat_file(tr, d_tracer) \
21ccc9cd 1993 trace_create_file("tracing_max_latency", TRACE_MODE_WRITE, \
7d660c9b 1994 d_tracer, tr, &tracing_max_lat_fops)
91edde2e
VRB
1995
1996#endif
0e950173 1997
5d4a9dba
SR
1998/*
1999 * Copy the new maximum trace into the separate maximum-trace
2000 * structure. (this way the maximum trace is permanently saved,
5a93bae2 2001 * for later retrieval via /sys/kernel/tracing/tracing_max_latency)
5d4a9dba
SR
2002 */
2003static void
2004__update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
2005{
1c5eb448
SRV
2006 struct array_buffer *trace_buf = &tr->array_buffer;
2007 struct array_buffer *max_buf = &tr->max_buffer;
12883efb
SRRH
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);
5d4a9dba 2010
12883efb
SRRH
2011 max_buf->cpu = cpu;
2012 max_buf->time_start = data->preempt_timestamp;
5d4a9dba 2013
6d9b3fa5 2014 max_data->saved_latency = tr->max_latency;
8248ac05
SR
2015 max_data->critical_start = data->critical_start;
2016 max_data->critical_end = data->critical_end;
5d4a9dba 2017
242b32d8 2018 strscpy(max_data->comm, tsk->comm);
8248ac05 2019 max_data->pid = tsk->pid;
f17a5194
SRRH
2020 /*
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.
2023 */
2024 if (tsk == current)
2025 max_data->uid = current_uid();
2026 else
2027 max_data->uid = task_uid(tsk);
2028
8248ac05
SR
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;
5d4a9dba
SR
2032
2033 /* record this tasks comm */
2034 tracing_record_cmdline(tsk);
91edde2e 2035 latency_fsnotify(tr);
5d4a9dba
SR
2036}
2037
4fcdae83
SR
2038/**
2039 * update_max_tr - snapshot all trace buffers from global_trace to max_tr
2040 * @tr: tracer
2041 * @tsk: the task with the latency
2042 * @cpu: The cpu that initiated the trace.
a35873a0 2043 * @cond_data: User data associated with a conditional snapshot
4fcdae83
SR
2044 *
2045 * Flip the buffers between the @tr and the max_tr and record information
2046 * about which task was the cause of this latency.
2047 */
e309b41d 2048void
a35873a0
TZ
2049update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu,
2050 void *cond_data)
bc0c38d1 2051{
2b6080f2 2052 if (tr->stop_count)
b8de7bd1
SR
2053 return;
2054
4c11d7ae 2055 WARN_ON_ONCE(!irqs_disabled());
34600f0e 2056
45ad21ca 2057 if (!tr->allocated_snapshot) {
debdd57f 2058 /* Only the nop tracer should hit this when disabling */
2b6080f2 2059 WARN_ON_ONCE(tr->current_trace != &nop_trace);
34600f0e 2060 return;
debdd57f 2061 }
34600f0e 2062
0b9b12c1 2063 arch_spin_lock(&tr->max_lock);
3928a8a2 2064
1c5eb448
SRV
2065 /* Inherit the recordable setting from array_buffer */
2066 if (ring_buffer_record_is_set_on(tr->array_buffer.buffer))
73c8d894
MH
2067 ring_buffer_record_on(tr->max_buffer.buffer);
2068 else
2069 ring_buffer_record_off(tr->max_buffer.buffer);
2070
a35873a0 2071#ifdef CONFIG_TRACER_SNAPSHOT
e25e43a4
MHG
2072 if (tr->cond_snapshot && !tr->cond_snapshot->update(tr, cond_data)) {
2073 arch_spin_unlock(&tr->max_lock);
2074 return;
2075 }
a35873a0 2076#endif
1c5eb448 2077 swap(tr->array_buffer.buffer, tr->max_buffer.buffer);
3928a8a2 2078
bc0c38d1 2079 __update_max_tr(tr, tsk, cpu);
a35873a0 2080
0b9b12c1 2081 arch_spin_unlock(&tr->max_lock);
39a7dc23
SRG
2082
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);
bc0c38d1
SR
2085}
2086
2087/**
2088 * update_max_tr_single - only copy one trace over, and reset the rest
c68c9ec1
JK
2089 * @tr: tracer
2090 * @tsk: task with the latency
2091 * @cpu: the cpu of the buffer to copy.
4fcdae83
SR
2092 *
2093 * Flip the trace of a single CPU buffer between the @tr and the max_tr.
bc0c38d1 2094 */
e309b41d 2095void
bc0c38d1
SR
2096update_max_tr_single(struct trace_array *tr, struct task_struct *tsk, int cpu)
2097{
3928a8a2 2098 int ret;
bc0c38d1 2099
2b6080f2 2100 if (tr->stop_count)
b8de7bd1
SR
2101 return;
2102
4c11d7ae 2103 WARN_ON_ONCE(!irqs_disabled());
6c24499f 2104 if (!tr->allocated_snapshot) {
2930e04d 2105 /* Only the nop tracer should hit this when disabling */
9e8529af 2106 WARN_ON_ONCE(tr->current_trace != &nop_trace);
ef710e10 2107 return;
2930e04d 2108 }
ef710e10 2109
0b9b12c1 2110 arch_spin_lock(&tr->max_lock);
bc0c38d1 2111
1c5eb448 2112 ret = ring_buffer_swap_cpu(tr->max_buffer.buffer, tr->array_buffer.buffer, cpu);
3928a8a2 2113
e8165dbb
SR
2114 if (ret == -EBUSY) {
2115 /*
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.
8a96c028 2120 * Another reason is resize is in progress.
e8165dbb 2121 */
12883efb 2122 trace_array_printk_buf(tr->max_buffer.buffer, _THIS_IP_,
8a96c028 2123 "Failed to swap buffers due to commit or resize in progress\n");
e8165dbb
SR
2124 }
2125
e8165dbb 2126 WARN_ON_ONCE(ret && ret != -EAGAIN && ret != -EBUSY);
bc0c38d1
SR
2127
2128 __update_max_tr(tr, tsk, cpu);
0b9b12c1 2129 arch_spin_unlock(&tr->max_lock);
bc0c38d1 2130}
e25e43a4 2131
5d4a9dba 2132#endif /* CONFIG_TRACER_MAX_TRACE */
bc0c38d1 2133
2aa043a5
SRG
2134struct pipe_wait {
2135 struct trace_iterator *iter;
2136 int wait_index;
2137};
2138
2139static bool wait_pipe_cond(void *data)
2140{
2141 struct pipe_wait *pwait = data;
2142 struct trace_iterator *iter = pwait->iter;
2143
2144 if (atomic_read_acquire(&iter->wait_index) != pwait->wait_index)
2145 return true;
2146
2147 return iter->closed;
2148}
2149
2c2b0a78 2150static int wait_on_pipe(struct trace_iterator *iter, int full)
0d5c6e1c 2151{
2aa043a5 2152 struct pipe_wait pwait;
39a7dc23
SRG
2153 int ret;
2154
15693458
SRRH
2155 /* Iterators are static, they should be filled or empty */
2156 if (trace_buffer_iter(iter, iter->cpu_file))
8b8b3683 2157 return 0;
0d5c6e1c 2158
2aa043a5
SRG
2159 pwait.wait_index = atomic_read_acquire(&iter->wait_index);
2160 pwait.iter = iter;
2161
2162 ret = ring_buffer_wait(iter->array_buffer->buffer, iter->cpu_file, full,
2163 wait_pipe_cond, &pwait);
39a7dc23
SRG
2164
2165#ifdef CONFIG_TRACER_MAX_TRACE
2166 /*
2167 * Make sure this is still the snapshot buffer, as if a snapshot were
2168 * to happen, this would now be the main buffer.
2169 */
2170 if (iter->snapshot)
2171 iter->array_buffer = &iter->tr->max_buffer;
2172#endif
2173 return ret;
0d5c6e1c
SR
2174}
2175
f4e781c0 2176#ifdef CONFIG_FTRACE_STARTUP_TEST
9afecfbb
SRV
2177static bool selftests_can_run;
2178
2179struct trace_selftests {
2180 struct list_head list;
2181 struct tracer *type;
2182};
2183
2184static LIST_HEAD(postponed_selftests);
2185
2186static int save_selftest(struct tracer *type)
2187{
2188 struct trace_selftests *selftest;
2189
2190 selftest = kmalloc(sizeof(*selftest), GFP_KERNEL);
2191 if (!selftest)
2192 return -ENOMEM;
2193
2194 selftest->type = type;
2195 list_add(&selftest->list, &postponed_selftests);
2196 return 0;
2197}
2198
f4e781c0
SRRH
2199static int run_tracer_selftest(struct tracer *type)
2200{
2201 struct trace_array *tr = &global_trace;
2202 struct tracer *saved_tracer = tr->current_trace;
2203 int ret;
0d5c6e1c 2204
f4e781c0
SRRH
2205 if (!type->selftest || tracing_selftest_disabled)
2206 return 0;
0d5c6e1c 2207
9afecfbb
SRV
2208 /*
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.
2212 */
2213 if (!selftests_can_run)
2214 return save_selftest(type);
2215
ee666a18
SRV
2216 if (!tracing_is_on()) {
2217 pr_warn("Selftest for tracer %s skipped due to tracing disabled\n",
2218 type->name);
2219 return 0;
2220 }
2221
0d5c6e1c 2222 /*
f4e781c0
SRRH
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.
0d5c6e1c 2228 */
1c5eb448 2229 tracing_reset_online_cpus(&tr->array_buffer);
0d5c6e1c 2230
f4e781c0
SRRH
2231 tr->current_trace = type;
2232
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 */
a1f157c7 2236 if (tr->ring_buffer_expanded)
f4e781c0
SRRH
2237 ring_buffer_resize(tr->max_buffer.buffer, trace_buf_size,
2238 RING_BUFFER_ALL_CPUS);
2239 tr->allocated_snapshot = true;
2240 }
2241#endif
2242
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;
2248 if (ret) {
2249 printk(KERN_CONT "FAILED!\n");
2250 /* Add the warning after printing 'FAILED' */
2251 WARN_ON(1);
2252 return -1;
2253 }
2254 /* Only reset on passing, to avoid touching corrupted buffers */
1c5eb448 2255 tracing_reset_online_cpus(&tr->array_buffer);
f4e781c0
SRRH
2256
2257#ifdef CONFIG_TRACER_MAX_TRACE
2258 if (type->use_max_tr) {
2259 tr->allocated_snapshot = false;
0d5c6e1c 2260
f4e781c0 2261 /* Shrink the max buffer again */
a1f157c7 2262 if (tr->ring_buffer_expanded)
f4e781c0
SRRH
2263 ring_buffer_resize(tr->max_buffer.buffer, 1,
2264 RING_BUFFER_ALL_CPUS);
2265 }
2266#endif
2267
2268 printk(KERN_CONT "PASSED\n");
2269 return 0;
2270}
9afecfbb 2271
e8352cf5
SRG
2272static int do_run_tracer_selftest(struct tracer *type)
2273{
2274 int ret;
2275
9da705d4
SRG
2276 /*
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.
2280 */
2281 cond_resched();
2282
e8352cf5
SRG
2283 tracing_selftest_running = true;
2284 ret = run_tracer_selftest(type);
2285 tracing_selftest_running = false;
2286
2287 return ret;
2288}
2289
9afecfbb
SRV
2290static __init int init_trace_selftests(void)
2291{
2292 struct trace_selftests *p, *n;
2293 struct tracer *t, **last;
2294 int ret;
2295
2296 selftests_can_run = true;
2297
d33b10c0 2298 guard(mutex)(&trace_types_lock);
9afecfbb
SRV
2299
2300 if (list_empty(&postponed_selftests))
d33b10c0 2301 return 0;
9afecfbb
SRV
2302
2303 pr_info("Running postponed tracer tests:\n");
2304
78041c0c 2305 tracing_selftest_running = true;
9afecfbb 2306 list_for_each_entry_safe(p, n, &postponed_selftests, list) {
6fc2171c
AR
2307 /* This loop can take minutes when sanitizers are enabled, so
2308 * lets make sure we allow RCU processing.
2309 */
2310 cond_resched();
9afecfbb
SRV
2311 ret = run_tracer_selftest(p->type);
2312 /* If the test fails, then warn and remove from available_tracers */
2313 if (ret < 0) {
2314 WARN(1, "tracer: %s failed selftest, disabling\n",
2315 p->type->name);
2316 last = &trace_types;
2317 for (t = trace_types; t; t = t->next) {
2318 if (t == p->type) {
2319 *last = t->next;
2320 break;
2321 }
2322 last = &t->next;
2323 }
2324 }
2325 list_del(&p->list);
2326 kfree(p);
2327 }
78041c0c 2328 tracing_selftest_running = false;
9afecfbb 2329
9afecfbb
SRV
2330 return 0;
2331}
b9ef0326 2332core_initcall(init_trace_selftests);
f4e781c0 2333#else
e8352cf5
SRG
2334static inline int do_run_tracer_selftest(struct tracer *type)
2335{
2336 return 0;
2337}
f4e781c0 2338#endif /* CONFIG_FTRACE_STARTUP_TEST */
0d5c6e1c 2339
41d9c0be
SRRH
2340static void add_tracer_options(struct trace_array *tr, struct tracer *t);
2341
a4d1e688
JW
2342static void __init apply_trace_boot_options(void);
2343
4fcdae83
SR
2344/**
2345 * register_tracer - register a tracer with the ftrace system.
c68c9ec1 2346 * @type: the plugin for the tracer
4fcdae83
SR
2347 *
2348 * Register a new plugin tracer.
2349 */
a4d1e688 2350int __init register_tracer(struct tracer *type)
bc0c38d1
SR
2351{
2352 struct tracer *t;
bc0c38d1
SR
2353 int ret = 0;
2354
2355 if (!type->name) {
2356 pr_info("Tracer must have a name\n");
2357 return -1;
2358 }
2359
24a461d5 2360 if (strlen(type->name) >= MAX_TRACER_SIZE) {
ee6c2c1b
LZ
2361 pr_info("Tracer has a name longer than %d\n", MAX_TRACER_SIZE);
2362 return -1;
2363 }
2364
a356646a 2365 if (security_locked_down(LOCKDOWN_TRACEFS)) {
ee195452 2366 pr_warn("Can not register tracer %s due to lockdown\n",
a356646a
SRV
2367 type->name);
2368 return -EPERM;
2369 }
2370
bc0c38d1 2371 mutex_lock(&trace_types_lock);
86fa2f60 2372
bc0c38d1
SR
2373 for (t = trace_types; t; t = t->next) {
2374 if (strcmp(type->name, t->name) == 0) {
2375 /* already found */
ee6c2c1b 2376 pr_info("Tracer %s already registered\n",
bc0c38d1
SR
2377 type->name);
2378 ret = -1;
2379 goto out;
2380 }
2381 }
2382
adf9f195
FW
2383 if (!type->set_flag)
2384 type->set_flag = &dummy_set_flag;
d39cdd20
CH
2385 if (!type->flags) {
2386 /*allocate a dummy tracer_flags*/
2387 type->flags = kmalloc(sizeof(*type->flags), GFP_KERNEL);
c8ca003b
CH
2388 if (!type->flags) {
2389 ret = -ENOMEM;
2390 goto out;
2391 }
d39cdd20
CH
2392 type->flags->val = 0;
2393 type->flags->opts = dummy_tracer_opt;
2394 } else
adf9f195
FW
2395 if (!type->flags->opts)
2396 type->flags->opts = dummy_tracer_opt;
6eaaa5d5 2397
d39cdd20
CH
2398 /* store the tracer for __set_tracer_option */
2399 type->flags->trace = type;
2400
e8352cf5 2401 ret = do_run_tracer_selftest(type);
f4e781c0
SRRH
2402 if (ret < 0)
2403 goto out;
60a11774 2404
bc0c38d1
SR
2405 type->next = trace_types;
2406 trace_types = type;
41d9c0be 2407 add_tracer_options(&global_trace, type);
60a11774 2408
bc0c38d1
SR
2409 out:
2410 mutex_unlock(&trace_types_lock);
2411
dac74940
SR
2412 if (ret || !default_bootup_tracer)
2413 goto out_unlock;
2414
ee6c2c1b 2415 if (strncmp(default_bootup_tracer, type->name, MAX_TRACER_SIZE))
dac74940
SR
2416 goto out_unlock;
2417
2418 printk(KERN_INFO "Starting tracer '%s'\n", type->name);
2419 /* Do we want this tracer to start on bootup? */
607e2ea1 2420 tracing_set_tracer(&global_trace, type->name);
dac74940 2421 default_bootup_tracer = NULL;
a4d1e688
JW
2422
2423 apply_trace_boot_options();
2424
dac74940 2425 /* disable other selftests, since this will break it. */
60efe21e 2426 disable_tracing_selftest("running a tracer");
b2821ae6 2427
dac74940 2428 out_unlock:
bc0c38d1
SR
2429 return ret;
2430}
2431
1c5eb448 2432static void tracing_reset_cpu(struct array_buffer *buf, int cpu)
f633903a 2433{
13292494 2434 struct trace_buffer *buffer = buf->buffer;
f633903a 2435
a5416411
HT
2436 if (!buffer)
2437 return;
2438
f633903a
SR
2439 ring_buffer_record_disable(buffer);
2440
2441 /* Make sure all commits have finished */
74401729 2442 synchronize_rcu();
68179686 2443 ring_buffer_reset_cpu(buffer, cpu);
f633903a
SR
2444
2445 ring_buffer_record_enable(buffer);
2446}
2447
1c5eb448 2448void tracing_reset_online_cpus(struct array_buffer *buf)
213cc060 2449{
13292494 2450 struct trace_buffer *buffer = buf->buffer;
213cc060 2451
a5416411
HT
2452 if (!buffer)
2453 return;
2454
621968cd
SR
2455 ring_buffer_record_disable(buffer);
2456
2457 /* Make sure all commits have finished */
74401729 2458 synchronize_rcu();
621968cd 2459
9457158b 2460 buf->time_start = buffer_ftrace_now(buf, buf->cpu);
213cc060 2461
b23d7a5f 2462 ring_buffer_reset_online_cpus(buffer);
621968cd
SR
2463
2464 ring_buffer_record_enable(buffer);
213cc060
PE
2465}
2466
09663753
SR
2467static void tracing_reset_all_cpus(struct array_buffer *buf)
2468{
2469 struct trace_buffer *buffer = buf->buffer;
2470
2471 if (!buffer)
2472 return;
2473
2474 ring_buffer_record_disable(buffer);
2475
2476 /* Make sure all commits have finished */
2477 synchronize_rcu();
2478
2479 buf->time_start = buffer_ftrace_now(buf, buf->cpu);
2480
2481 ring_buffer_reset(buffer);
2482
2483 ring_buffer_record_enable(buffer);
2484}
2485
09d8091c 2486/* Must have trace_types_lock held */
e18eb878 2487void tracing_reset_all_online_cpus_unlocked(void)
9456f0fa 2488{
873c642f
SRRH
2489 struct trace_array *tr;
2490
e18eb878
SRG
2491 lockdep_assert_held(&trace_types_lock);
2492
873c642f 2493 list_for_each_entry(tr, &ftrace_trace_arrays, list) {
065e63f9
SRV
2494 if (!tr->clear_trace)
2495 continue;
2496 tr->clear_trace = false;
1c5eb448 2497 tracing_reset_online_cpus(&tr->array_buffer);
12883efb
SRRH
2498#ifdef CONFIG_TRACER_MAX_TRACE
2499 tracing_reset_online_cpus(&tr->max_buffer);
2500#endif
873c642f 2501 }
9456f0fa
SR
2502}
2503
e18eb878
SRG
2504void tracing_reset_all_online_cpus(void)
2505{
2506 mutex_lock(&trace_types_lock);
2507 tracing_reset_all_online_cpus_unlocked();
2508 mutex_unlock(&trace_types_lock);
2509}
2510
b5130b1e
CE
2511int is_tracing_stopped(void)
2512{
2b6080f2 2513 return global_trace.stop_count;
b5130b1e
CE
2514}
2515
b538bf7d 2516static void tracing_start_tr(struct trace_array *tr)
0f048701 2517{
13292494 2518 struct trace_buffer *buffer;
0f048701
SR
2519 unsigned long flags;
2520
2521 if (tracing_disabled)
2522 return;
2523
b538bf7d
SRG
2524 raw_spin_lock_irqsave(&tr->start_lock, flags);
2525 if (--tr->stop_count) {
2526 if (WARN_ON_ONCE(tr->stop_count < 0)) {
b06a8301 2527 /* Someone screwed up their debugging */
b538bf7d 2528 tr->stop_count = 0;
b06a8301 2529 }
0f048701
SR
2530 goto out;
2531 }
2532
a2f80714 2533 /* Prevent the buffers from switching */
b538bf7d 2534 arch_spin_lock(&tr->max_lock);
0f048701 2535
b538bf7d 2536 buffer = tr->array_buffer.buffer;
0f048701
SR
2537 if (buffer)
2538 ring_buffer_record_enable(buffer);
2539
12883efb 2540#ifdef CONFIG_TRACER_MAX_TRACE
b538bf7d 2541 buffer = tr->max_buffer.buffer;
0f048701
SR
2542 if (buffer)
2543 ring_buffer_record_enable(buffer);
12883efb 2544#endif
0f048701 2545
b538bf7d 2546 arch_spin_unlock(&tr->max_lock);
2b6080f2
SR
2547
2548 out:
2549 raw_spin_unlock_irqrestore(&tr->start_lock, flags);
0f048701
SR
2550}
2551
2552/**
b538bf7d 2553 * tracing_start - quick start of the tracer
0f048701 2554 *
b538bf7d
SRG
2555 * If tracing is enabled but was stopped by tracing_stop,
2556 * this will start the tracer back up.
0f048701 2557 */
b538bf7d
SRG
2558void tracing_start(void)
2559
2560{
2561 return tracing_start_tr(&global_trace);
2562}
2563
2564static void tracing_stop_tr(struct trace_array *tr)
0f048701 2565{
13292494 2566 struct trace_buffer *buffer;
0f048701
SR
2567 unsigned long flags;
2568
b538bf7d
SRG
2569 raw_spin_lock_irqsave(&tr->start_lock, flags);
2570 if (tr->stop_count++)
0f048701
SR
2571 goto out;
2572
a2f80714 2573 /* Prevent the buffers from switching */
b538bf7d 2574 arch_spin_lock(&tr->max_lock);
a2f80714 2575
b538bf7d 2576 buffer = tr->array_buffer.buffer;
0f048701
SR
2577 if (buffer)
2578 ring_buffer_record_disable(buffer);
2579
12883efb 2580#ifdef CONFIG_TRACER_MAX_TRACE
b538bf7d 2581 buffer = tr->max_buffer.buffer;
0f048701
SR
2582 if (buffer)
2583 ring_buffer_record_disable(buffer);
12883efb 2584#endif
0f048701 2585
b538bf7d 2586 arch_spin_unlock(&tr->max_lock);
a2f80714 2587
0f048701 2588 out:
b538bf7d 2589 raw_spin_unlock_irqrestore(&tr->start_lock, flags);
2b6080f2
SR
2590}
2591
b538bf7d
SRG
2592/**
2593 * tracing_stop - quick stop of the tracer
2594 *
2595 * Light weight way to stop tracing. Use in conjunction with
2596 * tracing_start.
2597 */
2598void tracing_stop(void)
2b6080f2 2599{
b538bf7d 2600 return tracing_stop_tr(&global_trace);
0f048701
SR
2601}
2602
af0009fc
SRV
2603/*
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.
2607 */
2608enum print_line_t trace_handle_return(struct trace_seq *s)
2609{
2610 return trace_seq_has_overflowed(s) ?
2611 TRACE_TYPE_PARTIAL_LINE : TRACE_TYPE_HANDLED;
2612}
2613EXPORT_SYMBOL_GPL(trace_handle_return);
2614
54357f0c
TG
2615static unsigned short migration_disable_value(void)
2616{
2617#if defined(CONFIG_SMP)
2618 return current->migration_disabled;
2619#else
2620 return 0;
2621#endif
2622}
2623
0c02006e 2624unsigned int tracing_gen_ctx_irq_test(unsigned int irqs_status)
bc0c38d1 2625{
0c02006e 2626 unsigned int trace_flags = irqs_status;
36590c50 2627 unsigned int pc;
bc0c38d1 2628
36590c50 2629 pc = preempt_count();
bc0c38d1 2630
36590c50
SAS
2631 if (pc & NMI_MASK)
2632 trace_flags |= TRACE_FLAG_NMI;
2633 if (pc & HARDIRQ_MASK)
2634 trace_flags |= TRACE_FLAG_HARDIRQ;
fe427886 2635 if (in_serving_softirq())
36590c50 2636 trace_flags |= TRACE_FLAG_SOFTIRQ;
289e7b0f
SAS
2637 if (softirq_count() >> (SOFTIRQ_SHIFT + 1))
2638 trace_flags |= TRACE_FLAG_BH_OFF;
36590c50
SAS
2639
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;
0172afef
TG
2644 if (IS_ENABLED(CONFIG_ARCH_HAS_PREEMPT_LAZY) && tif_test_bit(TIF_NEED_RESCHED_LAZY))
2645 trace_flags |= TRACE_FLAG_NEED_RESCHED_LAZY;
54357f0c
TG
2646 return (trace_flags << 16) | (min_t(unsigned int, pc & 0xff, 0xf)) |
2647 (min_t(unsigned int, migration_disable_value(), 0xf)) << 4;
bc0c38d1
SR
2648}
2649
e77405ad 2650struct ring_buffer_event *
13292494 2651trace_buffer_lock_reserve(struct trace_buffer *buffer,
e77405ad
SR
2652 int type,
2653 unsigned long len,
36590c50 2654 unsigned int trace_ctx)
51a763dd 2655{
36590c50 2656 return __trace_buffer_lock_reserve(buffer, type, len, trace_ctx);
0fc1b09f
SRRH
2657}
2658
2659DEFINE_PER_CPU(struct ring_buffer_event *, trace_buffered_event);
2660DEFINE_PER_CPU(int, trace_buffered_event_cnt);
2661static int trace_buffered_event_ref;
2662
2663/**
2664 * trace_buffered_event_enable - enable buffering events
2665 *
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
2670 * a commit.
2671 *
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
2675 * in one shot.
2676 */
2677void trace_buffered_event_enable(void)
2678{
2679 struct ring_buffer_event *event;
2680 struct page *page;
2681 int cpu;
51a763dd 2682
0fc1b09f
SRRH
2683 WARN_ON_ONCE(!mutex_is_locked(&event_mutex));
2684
2685 if (trace_buffered_event_ref++)
2686 return;
2687
2688 for_each_tracing_cpu(cpu) {
2689 page = alloc_pages_node(cpu_to_node(cpu),
2690 GFP_KERNEL | __GFP_NORETRY, 0);
34209fe8
PP
2691 /* This is just an optimization and can handle failures */
2692 if (!page) {
2693 pr_err("Failed to allocate event buffer\n");
2694 break;
2695 }
0fc1b09f
SRRH
2696
2697 event = page_address(page);
2698 memset(event, 0, sizeof(*event));
2699
2700 per_cpu(trace_buffered_event, cpu) = event;
2701
2702 preempt_disable();
2703 if (cpu == smp_processor_id() &&
b427e765 2704 __this_cpu_read(trace_buffered_event) !=
0fc1b09f
SRRH
2705 per_cpu(trace_buffered_event, cpu))
2706 WARN_ON_ONCE(1);
2707 preempt_enable();
51a763dd 2708 }
0fc1b09f
SRRH
2709}
2710
2711static void enable_trace_buffered_event(void *data)
2712{
2713 /* Probably not needed, but do it anyway */
2714 smp_rmb();
2715 this_cpu_dec(trace_buffered_event_cnt);
2716}
2717
2718static void disable_trace_buffered_event(void *data)
2719{
2720 this_cpu_inc(trace_buffered_event_cnt);
2721}
2722
2723/**
2724 * trace_buffered_event_disable - disable buffering events
2725 *
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.
2730 */
2731void trace_buffered_event_disable(void)
2732{
2733 int cpu;
2734
2735 WARN_ON_ONCE(!mutex_is_locked(&event_mutex));
2736
2737 if (WARN_ON_ONCE(!trace_buffered_event_ref))
2738 return;
2739
2740 if (--trace_buffered_event_ref)
2741 return;
2742
0fc1b09f 2743 /* For each CPU, set the buffer as used. */
7fed14f7
PP
2744 on_each_cpu_mask(tracing_buffer_mask, disable_trace_buffered_event,
2745 NULL, true);
0fc1b09f
SRRH
2746
2747 /* Wait for all current users to finish */
74401729 2748 synchronize_rcu();
0fc1b09f
SRRH
2749
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;
2753 }
c0591b1c 2754
0fc1b09f 2755 /*
c0591b1c
PP
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.
0fc1b09f 2761 */
c0591b1c 2762 synchronize_rcu();
0fc1b09f 2763
c0591b1c 2764 /* For each CPU, relinquish the buffer */
7fed14f7
PP
2765 on_each_cpu_mask(tracing_buffer_mask, enable_trace_buffered_event, NULL,
2766 true);
51a763dd 2767}
51a763dd 2768
13292494 2769static struct trace_buffer *temp_buffer;
2c4a33ab 2770
ccb469a1 2771struct ring_buffer_event *
13292494 2772trace_event_buffer_lock_reserve(struct trace_buffer **current_rb,
7f1d2f82 2773 struct trace_event_file *trace_file,
ccb469a1 2774 int type, unsigned long len,
36590c50 2775 unsigned int trace_ctx)
ccb469a1 2776{
2c4a33ab 2777 struct ring_buffer_event *entry;
b94bc80d 2778 struct trace_array *tr = trace_file->tr;
0fc1b09f 2779 int val;
2c4a33ab 2780
b94bc80d 2781 *current_rb = tr->array_buffer.buffer;
0fc1b09f 2782
b94bc80d 2783 if (!tr->no_filter_buffering_ref &&
6c536d76
SRV
2784 (trace_file->flags & (EVENT_FILE_FL_SOFT_DISABLED | EVENT_FILE_FL_FILTERED))) {
2785 preempt_disable_notrace();
8f0901cd
SRV
2786 /*
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).
2793 *
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.
2802 */
6c536d76
SRV
2803 if ((entry = __this_cpu_read(trace_buffered_event))) {
2804 int max_len = PAGE_SIZE - struct_size(entry, array, 1);
faa76a6c 2805
6c536d76 2806 val = this_cpu_inc_return(trace_buffered_event_cnt);
8f0901cd 2807
6c536d76
SRV
2808 /*
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()
2815 * to remove it.
2816 *
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.
2824 */
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 */
2829 return entry;
2830 }
2831 this_cpu_dec(trace_buffered_event_cnt);
0fc1b09f 2832 }
6c536d76
SRV
2833 /* __trace_buffer_lock_reserve() disables preemption */
2834 preempt_enable_notrace();
0fc1b09f
SRRH
2835 }
2836
36590c50
SAS
2837 entry = __trace_buffer_lock_reserve(*current_rb, type, len,
2838 trace_ctx);
2c4a33ab
SRRH
2839 /*
2840 * If tracing is off, but we have triggers enabled
2841 * we still need to look at the event data. Use the temp_buffer
906695e5 2842 * to store the trace event for the trigger to use. It's recursive
2c4a33ab
SRRH
2843 * safe and will not be recorded anywhere.
2844 */
5d6ad960 2845 if (!entry && trace_file->flags & EVENT_FILE_FL_TRIGGER_COND) {
2c4a33ab 2846 *current_rb = temp_buffer;
36590c50
SAS
2847 entry = __trace_buffer_lock_reserve(*current_rb, type, len,
2848 trace_ctx);
2c4a33ab
SRRH
2849 }
2850 return entry;
ccb469a1
SR
2851}
2852EXPORT_SYMBOL_GPL(trace_event_buffer_lock_reserve);
2853
12025abd 2854static DEFINE_RAW_SPINLOCK(tracepoint_iter_lock);
42391745
SRRH
2855static DEFINE_MUTEX(tracepoint_printk_mutex);
2856
2857static void output_printk(struct trace_event_buffer *fbuffer)
2858{
2859 struct trace_event_call *event_call;
d8d0c245 2860 struct trace_event_file *file;
42391745
SRRH
2861 struct trace_event *event;
2862 unsigned long flags;
2863 struct trace_iterator *iter = tracepoint_print_iter;
2864
2865 /* We should never get here if iter is NULL */
2866 if (WARN_ON_ONCE(!iter))
2867 return;
2868
2869 event_call = fbuffer->trace_file->event_call;
2870 if (!event_call || !event_call->event.funcs ||
2871 !event_call->event.funcs->trace)
2872 return;
2873
d8d0c245
MH
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)))
2878 return;
2879
42391745
SRRH
2880 event = &fbuffer->trace_file->event_call->event;
2881
12025abd 2882 raw_spin_lock_irqsave(&tracepoint_iter_lock, flags);
42391745
SRRH
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);
2888
12025abd 2889 raw_spin_unlock_irqrestore(&tracepoint_iter_lock, flags);
42391745
SRRH
2890}
2891
78eb4ea2 2892int tracepoint_printk_sysctl(const struct ctl_table *table, int write,
32927393 2893 void *buffer, size_t *lenp,
42391745
SRRH
2894 loff_t *ppos)
2895{
2896 int save_tracepoint_printk;
2897 int ret;
2898
d33b10c0 2899 guard(mutex)(&tracepoint_printk_mutex);
42391745
SRRH
2900 save_tracepoint_printk = tracepoint_printk;
2901
2902 ret = proc_dointvec(table, write, buffer, lenp, ppos);
2903
2904 /*
2905 * This will force exiting early, as tracepoint_printk
2906 * is always zero when tracepoint_printk_iter is not allocated
2907 */
2908 if (!tracepoint_print_iter)
2909 tracepoint_printk = 0;
2910
2911 if (save_tracepoint_printk == tracepoint_printk)
d33b10c0 2912 return ret;
42391745
SRRH
2913
2914 if (tracepoint_printk)
2915 static_key_enable(&tracepoint_printk_key.key);
2916 else
2917 static_key_disable(&tracepoint_printk_key.key);
2918
42391745
SRRH
2919 return ret;
2920}
2921
2922void trace_event_buffer_commit(struct trace_event_buffer *fbuffer)
2923{
6c34df6f
PL
2924 enum event_trigger_type tt = ETT_NONE;
2925 struct trace_event_file *file = fbuffer->trace_file;
2926
2927 if (__event_trigger_test_discard(file, fbuffer->buffer, fbuffer->event,
2928 fbuffer->entry, &tt))
2929 goto discard;
2930
42391745
SRRH
2931 if (static_key_false(&tracepoint_printk_key.key))
2932 output_printk(fbuffer);
2933
8ab7a2b7
TZ
2934 if (static_branch_unlikely(&trace_event_exports_enabled))
2935 ftrace_exports(fbuffer->event, TRACE_EXPORT_EVENT);
6c34df6f
PL
2936
2937 trace_buffer_unlock_commit_regs(file->tr, fbuffer->buffer,
2938 fbuffer->event, fbuffer->trace_ctx, fbuffer->regs);
2939
2940discard:
2941 if (tt)
2942 event_triggers_post_call(file, tt);
2943
42391745
SRRH
2944}
2945EXPORT_SYMBOL_GPL(trace_event_buffer_commit);
2946
2ee5b92a
SRV
2947/*
2948 * Skip 3:
2949 *
2950 * trace_buffer_unlock_commit_regs()
2951 * trace_event_buffer_commit()
2952 * trace_event_raw_event_xxx()
13cf912b 2953 */
2ee5b92a
SRV
2954# define STACK_SKIP 3
2955
b7f0c959 2956void trace_buffer_unlock_commit_regs(struct trace_array *tr,
13292494 2957 struct trace_buffer *buffer,
0d5c6e1c 2958 struct ring_buffer_event *event,
36590c50 2959 unsigned int trace_ctx,
0d5c6e1c 2960 struct pt_regs *regs)
1fd8df2c 2961{
7ffbd48d 2962 __buffer_unlock_commit(buffer, event);
1fd8df2c 2963
be54f69c 2964 /*
2ee5b92a 2965 * If regs is not set, then skip the necessary functions.
be54f69c
SRRH
2966 * Note, we can still get here via blktrace, wakeup tracer
2967 * and mmiotrace, but that's ok if they lose a function or
2ee5b92a 2968 * two. They are not that meaningful.
be54f69c 2969 */
36590c50
SAS
2970 ftrace_trace_stack(tr, buffer, trace_ctx, regs ? 0 : STACK_SKIP, regs);
2971 ftrace_trace_userstack(tr, buffer, trace_ctx);
1fd8df2c 2972}
1fd8df2c 2973
52ffabe3
SRRH
2974/*
2975 * Similar to trace_buffer_unlock_commit_regs() but do not dump stack.
2976 */
2977void
13292494 2978trace_buffer_unlock_commit_nostack(struct trace_buffer *buffer,
52ffabe3
SRRH
2979 struct ring_buffer_event *event)
2980{
2981 __buffer_unlock_commit(buffer, event);
2982}
2983
e309b41d 2984void
36590c50 2985trace_function(struct trace_array *tr, unsigned long ip, unsigned long
76fe0337 2986 parent_ip, unsigned int trace_ctx, struct ftrace_regs *fregs)
bc0c38d1 2987{
13292494 2988 struct trace_buffer *buffer = tr->array_buffer.buffer;
3928a8a2 2989 struct ring_buffer_event *event;
777e208d 2990 struct ftrace_entry *entry;
76fe0337 2991 int size = sizeof(*entry);
bc0c38d1 2992
76fe0337
SS
2993 size += FTRACE_REGS_MAX_ARGS * !!fregs * sizeof(long);
2994
2995 event = __trace_buffer_lock_reserve(buffer, TRACE_FN, size,
36590c50 2996 trace_ctx);
3928a8a2
SR
2997 if (!event)
2998 return;
2999 entry = ring_buffer_event_data(event);
777e208d
SR
3000 entry->ip = ip;
3001 entry->parent_ip = parent_ip;
e1112b4d 3002
76fe0337
SS
3003#ifdef CONFIG_HAVE_FUNCTION_ARG_ACCESS_API
3004 if (fregs) {
3005 for (int i = 0; i < FTRACE_REGS_MAX_ARGS; i++)
3006 entry->args[i] = ftrace_regs_get_argument(fregs, i);
3007 }
3008#endif
3009
49e4154f
ZY
3010 if (static_branch_unlikely(&trace_function_exports_enabled))
3011 ftrace_exports(event, TRACE_EXPORT_FUNCTION);
3012 __buffer_unlock_commit(buffer, event);
bc0c38d1
SR
3013}
3014
c0a0d0d3 3015#ifdef CONFIG_STACKTRACE
4a9bd3f1 3016
2a820bf7
TG
3017/* Allow 4 levels of nesting: normal, softirq, irq, NMI */
3018#define FTRACE_KSTACK_NESTING 4
3019
2c33155e 3020#define FTRACE_KSTACK_ENTRIES (SZ_4K / FTRACE_KSTACK_NESTING)
2a820bf7 3021
4a9bd3f1 3022struct ftrace_stack {
2a820bf7 3023 unsigned long calls[FTRACE_KSTACK_ENTRIES];
4a9bd3f1
SR
3024};
3025
2a820bf7
TG
3026
3027struct ftrace_stacks {
3028 struct ftrace_stack stacks[FTRACE_KSTACK_NESTING];
4a9bd3f1
SR
3029};
3030
2a820bf7 3031static DEFINE_PER_CPU(struct ftrace_stacks, ftrace_stacks);
4a9bd3f1
SR
3032static DEFINE_PER_CPU(int, ftrace_stack_reserve);
3033
6ce5a6f0
T
3034static void __ftrace_trace_stack(struct trace_array *tr,
3035 struct trace_buffer *buffer,
36590c50
SAS
3036 unsigned int trace_ctx,
3037 int skip, struct pt_regs *regs)
86387f7e 3038{
3928a8a2 3039 struct ring_buffer_event *event;
ee6dd0db 3040 unsigned int size, nr_entries;
2a820bf7 3041 struct ftrace_stack *fstack;
777e208d 3042 struct stack_entry *entry;
2a820bf7 3043 int stackidx;
4a9bd3f1 3044
be54f69c 3045 /*
2ee5b92a 3046 * Add one, for this function and the call to save_stack_trace()
be54f69c
SRRH
3047 * If regs is set, then these functions will not be in the way.
3048 */
2ee5b92a 3049#ifndef CONFIG_UNWINDER_ORC
be54f69c 3050 if (!regs)
ee6dd0db 3051 skip++;
2ee5b92a 3052#endif
be54f69c 3053
4a9bd3f1
SR
3054 preempt_disable_notrace();
3055
2a820bf7
TG
3056 stackidx = __this_cpu_inc_return(ftrace_stack_reserve) - 1;
3057
3058 /* This should never happen. If it does, yell once and skip */
906695e5 3059 if (WARN_ON_ONCE(stackidx >= FTRACE_KSTACK_NESTING))
2a820bf7
TG
3060 goto out;
3061
4a9bd3f1 3062 /*
2a820bf7
TG
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.
4a9bd3f1
SR
3068 */
3069 barrier();
4a9bd3f1 3070
2a820bf7 3071 fstack = this_cpu_ptr(ftrace_stacks.stacks) + stackidx;
ee6dd0db 3072 size = ARRAY_SIZE(fstack->calls);
4a9bd3f1 3073
ee6dd0db
TG
3074 if (regs) {
3075 nr_entries = stack_trace_save_regs(regs, fstack->calls,
3076 size, skip);
3077 } else {
3078 nr_entries = stack_trace_save(fstack->calls, size, skip);
3079 }
86387f7e 3080
6ce5a6f0
T
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;
3087
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;
3091 }
3092 }
3093#endif
3094
3e9a8aad 3095 event = __trace_buffer_lock_reserve(buffer, TRACE_STACK,
e7186af7 3096 struct_size(entry, caller, nr_entries),
9deb193a 3097 trace_ctx);
3928a8a2 3098 if (!event)
4a9bd3f1 3099 goto out;
e7186af7 3100 entry = ring_buffer_event_data(event);
86387f7e 3101
ee6dd0db 3102 entry->size = nr_entries;
e7186af7
SRG
3103 memcpy(&entry->caller, fstack->calls,
3104 flex_array_size(entry, caller, nr_entries));
86387f7e 3105
49e4154f 3106 __buffer_unlock_commit(buffer, event);
4a9bd3f1
SR
3107
3108 out:
3109 /* Again, don't let gcc optimize things here */
3110 barrier();
82146529 3111 __this_cpu_dec(ftrace_stack_reserve);
4a9bd3f1
SR
3112 preempt_enable_notrace();
3113
f0a920d5
IM
3114}
3115
2d34f489 3116static inline void ftrace_trace_stack(struct trace_array *tr,
13292494 3117 struct trace_buffer *buffer,
36590c50
SAS
3118 unsigned int trace_ctx,
3119 int skip, struct pt_regs *regs)
53614991 3120{
2d34f489 3121 if (!(tr->trace_flags & TRACE_ITER_STACKTRACE))
53614991
SR
3122 return;
3123
6ce5a6f0 3124 __ftrace_trace_stack(tr, buffer, trace_ctx, skip, regs);
53614991
SR
3125}
3126
36590c50
SAS
3127void __trace_stack(struct trace_array *tr, unsigned int trace_ctx,
3128 int skip)
38697053 3129{
13292494 3130 struct trace_buffer *buffer = tr->array_buffer.buffer;
a33d7d94
SRV
3131
3132 if (rcu_is_watching()) {
6ce5a6f0 3133 __ftrace_trace_stack(tr, buffer, trace_ctx, skip, NULL);
a33d7d94
SRV
3134 return;
3135 }
3136
408b9611
PZ
3137 if (WARN_ON_ONCE(IS_ENABLED(CONFIG_GENERIC_ENTRY)))
3138 return;
3139
a33d7d94 3140 /*
493c1822 3141 * When an NMI triggers, RCU is enabled via ct_nmi_enter(),
a33d7d94 3142 * but if the above rcu_is_watching() failed, then the NMI
6f0e6c15 3143 * triggered someplace critical, and ct_irq_enter() should
a33d7d94
SRV
3144 * not be called from NMI.
3145 */
3146 if (unlikely(in_nmi()))
3147 return;
3148
6f0e6c15 3149 ct_irq_enter_irqson();
6ce5a6f0 3150 __ftrace_trace_stack(tr, buffer, trace_ctx, skip, NULL);
6f0e6c15 3151 ct_irq_exit_irqson();
38697053
SR
3152}
3153
03889384
SR
3154/**
3155 * trace_dump_stack - record a stack back trace in the trace buffer
c142be8e 3156 * @skip: Number of functions to skip (helper handlers)
03889384 3157 */
c142be8e 3158void trace_dump_stack(int skip)
03889384 3159{
03889384 3160 if (tracing_disabled || tracing_selftest_running)
e36c5458 3161 return;
03889384 3162
2ee5b92a
SRV
3163#ifndef CONFIG_UNWINDER_ORC
3164 /* Skip 1 to skip this function. */
3165 skip++;
3166#endif
6ce5a6f0
T
3167 __ftrace_trace_stack(printk_trace, printk_trace->array_buffer.buffer,
3168 tracing_gen_ctx(), skip, NULL);
03889384 3169}
da387e5c 3170EXPORT_SYMBOL_GPL(trace_dump_stack);
03889384 3171
c438f140 3172#ifdef CONFIG_USER_STACKTRACE_SUPPORT
91e86e56
SR
3173static DEFINE_PER_CPU(int, user_stack_count);
3174
c438f140 3175static void
bcee5278 3176ftrace_trace_userstack(struct trace_array *tr,
36590c50 3177 struct trace_buffer *buffer, unsigned int trace_ctx)
02b67518 3178{
8d7c6a96 3179 struct ring_buffer_event *event;
02b67518 3180 struct userstack_entry *entry;
02b67518 3181
bcee5278 3182 if (!(tr->trace_flags & TRACE_ITER_USERSTACKTRACE))
02b67518
TE
3183 return;
3184
b6345879
SR
3185 /*
3186 * NMIs can not handle page faults, even with fix ups.
3187 * The save user stack can (and often does) fault.
3188 */
3189 if (unlikely(in_nmi()))
3190 return;
02b67518 3191
91e86e56
SR
3192 /*
3193 * prevent recursion, since the user stack tracing may
3194 * trigger other kernel events.
3195 */
3196 preempt_disable();
3197 if (__this_cpu_read(user_stack_count))
3198 goto out;
3199
3200 __this_cpu_inc(user_stack_count);
3201
3e9a8aad 3202 event = __trace_buffer_lock_reserve(buffer, TRACE_USER_STACK,
36590c50 3203 sizeof(*entry), trace_ctx);
02b67518 3204 if (!event)
1dbd1951 3205 goto out_drop_count;
02b67518 3206 entry = ring_buffer_event_data(event);
02b67518 3207
48659d31 3208 entry->tgid = current->tgid;
02b67518
TE
3209 memset(&entry->caller, 0, sizeof(entry->caller));
3210
ee6dd0db 3211 stack_trace_save_user(entry->caller, FTRACE_STACK_ENTRIES);
49e4154f 3212 __buffer_unlock_commit(buffer, event);
91e86e56 3213
1dbd1951 3214 out_drop_count:
91e86e56 3215 __this_cpu_dec(user_stack_count);
91e86e56
SR
3216 out:
3217 preempt_enable();
02b67518 3218}
c438f140 3219#else /* CONFIG_USER_STACKTRACE_SUPPORT */
bcee5278
SRV
3220static void ftrace_trace_userstack(struct trace_array *tr,
3221 struct trace_buffer *buffer,
36590c50 3222 unsigned int trace_ctx)
02b67518 3223{
02b67518 3224}
c438f140 3225#endif /* !CONFIG_USER_STACKTRACE_SUPPORT */
02b67518 3226
c0a0d0d3
FW
3227#endif /* CONFIG_STACKTRACE */
3228
c658797f
YKV
3229static inline void
3230func_repeats_set_delta_ts(struct func_repeats_entry *entry,
3231 unsigned long long delta)
3232{
3233 entry->bottom_delta_ts = delta & U32_MAX;
3234 entry->top_delta_ts = (delta >> 32);
3235}
3236
3237void trace_last_func_repeats(struct trace_array *tr,
3238 struct trace_func_repeats *last_info,
3239 unsigned int trace_ctx)
3240{
3241 struct trace_buffer *buffer = tr->array_buffer.buffer;
3242 struct func_repeats_entry *entry;
3243 struct ring_buffer_event *event;
3244 u64 delta;
3245
3246 event = __trace_buffer_lock_reserve(buffer, TRACE_FUNC_REPEATS,
3247 sizeof(*entry), trace_ctx);
3248 if (!event)
3249 return;
3250
3251 delta = ring_buffer_event_time_stamp(buffer, event) -
3252 last_info->ts_last_call;
3253
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);
3259
3260 __buffer_unlock_commit(buffer, event);
3261}
3262
07d777fe
SR
3263/* created for use with alloc_percpu */
3264struct trace_buffer_struct {
e2ace001
AL
3265 int nesting;
3266 char buffer[4][TRACE_BUF_SIZE];
07d777fe
SR
3267};
3268
f28439db 3269static struct trace_buffer_struct __percpu *trace_percpu_buffer;
07d777fe
SR
3270
3271/*
2b5894cc 3272 * This allows for lockless recording. If we're nested too deeply, then
e2ace001 3273 * this returns NULL.
07d777fe
SR
3274 */
3275static char *get_trace_buf(void)
3276{
e2ace001 3277 struct trace_buffer_struct *buffer = this_cpu_ptr(trace_percpu_buffer);
07d777fe 3278
823e670f 3279 if (!trace_percpu_buffer || buffer->nesting >= 4)
07d777fe
SR
3280 return NULL;
3281
3d9622c1
SRV
3282 buffer->nesting++;
3283
3284 /* Interrupts must see nesting incremented before we use the buffer */
3285 barrier();
c1acb4ac 3286 return &buffer->buffer[buffer->nesting - 1][0];
e2ace001
AL
3287}
3288
3289static void put_trace_buf(void)
3290{
3d9622c1
SRV
3291 /* Don't let the decrement of nesting leak before this */
3292 barrier();
e2ace001 3293 this_cpu_dec(trace_percpu_buffer->nesting);
07d777fe
SR
3294}
3295
3296static int alloc_percpu_trace_buffer(void)
3297{
f28439db 3298 struct trace_buffer_struct __percpu *buffers;
07d777fe 3299
38ce2a9e
SRV
3300 if (trace_percpu_buffer)
3301 return 0;
3302
07d777fe 3303 buffers = alloc_percpu(struct trace_buffer_struct);
24589e3a 3304 if (MEM_FAIL(!buffers, "Could not allocate percpu trace_printk buffer"))
e2ace001 3305 return -ENOMEM;
07d777fe
SR
3306
3307 trace_percpu_buffer = buffers;
07d777fe 3308 return 0;
07d777fe
SR
3309}
3310
81698831
SR
3311static int buffers_allocated;
3312
07d777fe
SR
3313void trace_printk_init_buffers(void)
3314{
07d777fe
SR
3315 if (buffers_allocated)
3316 return;
3317
3318 if (alloc_percpu_trace_buffer())
3319 return;
3320
2184db46
SR
3321 /* trace_printk() is for debug use only. Don't use it in production. */
3322
a395d6a7
JP
3323 pr_warn("\n");
3324 pr_warn("**********************************************************\n");
3325 pr_warn("** NOTICE NOTICE NOTICE NOTICE NOTICE NOTICE NOTICE **\n");
3326 pr_warn("** **\n");
3327 pr_warn("** trace_printk() being used. Allocating extra memory. **\n");
3328 pr_warn("** **\n");
3329 pr_warn("** This means that this is a DEBUG kernel and it is **\n");
3330 pr_warn("** unsafe for production use. **\n");
3331 pr_warn("** **\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");
3334 pr_warn("** **\n");
3335 pr_warn("** NOTICE NOTICE NOTICE NOTICE NOTICE NOTICE NOTICE **\n");
3336 pr_warn("**********************************************************\n");
07d777fe 3337
b382ede6 3338 /* Expand the buffers to set size */
a1f157c7 3339 tracing_update_buffers(&global_trace);
b382ede6 3340
07d777fe 3341 buffers_allocated = 1;
81698831
SR
3342
3343 /*
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.
3348 */
1c5eb448 3349 if (global_trace.array_buffer.buffer)
81698831
SR
3350 tracing_start_cmdline_record();
3351}
f45d1225 3352EXPORT_SYMBOL_GPL(trace_printk_init_buffers);
81698831
SR
3353
3354void trace_printk_start_comm(void)
3355{
3356 /* Start tracing comms if trace printk is set */
3357 if (!buffers_allocated)
3358 return;
3359 tracing_start_cmdline_record();
3360}
3361
3362static void trace_printk_start_stop_comm(int enabled)
3363{
3364 if (!buffers_allocated)
3365 return;
3366
3367 if (enabled)
3368 tracing_start_cmdline_record();
3369 else
3370 tracing_stop_cmdline_record();
07d777fe
SR
3371}
3372
769b0441 3373/**
48ead020 3374 * trace_vbprintk - write binary msg to tracing buffer
c68c9ec1
JK
3375 * @ip: The address of the caller
3376 * @fmt: The string format to write to the buffer
3377 * @args: Arguments for @fmt
769b0441 3378 */
40ce74f1 3379int trace_vbprintk(unsigned long ip, const char *fmt, va_list args)
769b0441 3380{
769b0441 3381 struct ring_buffer_event *event;
13292494 3382 struct trace_buffer *buffer;
9b7bdf6f 3383 struct trace_array *tr = READ_ONCE(printk_trace);
48ead020 3384 struct bprint_entry *entry;
36590c50 3385 unsigned int trace_ctx;
07d777fe 3386 char *tbuffer;
36590c50 3387 int len = 0, size;
769b0441 3388
9b7bdf6f
SR
3389 if (!printk_binsafe(tr))
3390 return trace_vprintk(ip, fmt, args);
3391
769b0441
FW
3392 if (unlikely(tracing_selftest_running || tracing_disabled))
3393 return 0;
3394
3395 /* Don't pollute graph traces with trace_vprintk internals */
3396 pause_graph_tracing();
3397
36590c50 3398 trace_ctx = tracing_gen_ctx();
5168ae50 3399 preempt_disable_notrace();
769b0441 3400
07d777fe
SR
3401 tbuffer = get_trace_buf();
3402 if (!tbuffer) {
3403 len = 0;
e2ace001 3404 goto out_nobuffer;
07d777fe 3405 }
769b0441 3406
07d777fe 3407 len = vbin_printf((u32 *)tbuffer, TRACE_BUF_SIZE/sizeof(int), fmt, args);
769b0441 3408
07d777fe 3409 if (len > TRACE_BUF_SIZE/sizeof(int) || len < 0)
34423f25 3410 goto out_put;
769b0441
FW
3411
3412 size = sizeof(*entry) + sizeof(u32) * len;
1c5eb448 3413 buffer = tr->array_buffer.buffer;
82d1b815 3414 ring_buffer_nest_start(buffer);
3e9a8aad 3415 event = __trace_buffer_lock_reserve(buffer, TRACE_BPRINT, size,
36590c50 3416 trace_ctx);
769b0441 3417 if (!event)
07d777fe 3418 goto out;
769b0441
FW
3419 entry = ring_buffer_event_data(event);
3420 entry->ip = ip;
769b0441
FW
3421 entry->fmt = fmt;
3422
07d777fe 3423 memcpy(entry->buf, tbuffer, sizeof(u32) * len);
49e4154f
ZY
3424 __buffer_unlock_commit(buffer, event);
3425 ftrace_trace_stack(tr, buffer, trace_ctx, 6, NULL);
769b0441 3426
769b0441 3427out:
82d1b815 3428 ring_buffer_nest_end(buffer);
34423f25 3429out_put:
e2ace001
AL
3430 put_trace_buf();
3431
3432out_nobuffer:
5168ae50 3433 preempt_enable_notrace();
769b0441
FW
3434 unpause_graph_tracing();
3435
3436 return len;
3437}
48ead020
FW
3438EXPORT_SYMBOL_GPL(trace_vbprintk);
3439
196a0626
AS
3440static __printf(3, 0)
3441int __trace_array_vprintk(struct trace_buffer *buffer,
3442 unsigned long ip, const char *fmt, va_list args)
48ead020 3443{
48ead020 3444 struct ring_buffer_event *event;
36590c50 3445 int len = 0, size;
48ead020 3446 struct print_entry *entry;
36590c50 3447 unsigned int trace_ctx;
07d777fe 3448 char *tbuffer;
48ead020 3449
ac9d2cb1 3450 if (tracing_disabled)
48ead020
FW
3451 return 0;
3452
07d777fe
SR
3453 /* Don't pollute graph traces with trace_vprintk internals */
3454 pause_graph_tracing();
3455
36590c50 3456 trace_ctx = tracing_gen_ctx();
48ead020 3457 preempt_disable_notrace();
48ead020 3458
07d777fe
SR
3459
3460 tbuffer = get_trace_buf();
3461 if (!tbuffer) {
3462 len = 0;
e2ace001 3463 goto out_nobuffer;
07d777fe 3464 }
48ead020 3465
3558a5ac 3466 len = vscnprintf(tbuffer, TRACE_BUF_SIZE, fmt, args);
48ead020
FW
3467
3468 size = sizeof(*entry) + len + 1;
82d1b815 3469 ring_buffer_nest_start(buffer);
3e9a8aad 3470 event = __trace_buffer_lock_reserve(buffer, TRACE_PRINT, size,
36590c50 3471 trace_ctx);
48ead020 3472 if (!event)
07d777fe 3473 goto out;
48ead020 3474 entry = ring_buffer_event_data(event);
c13d2f7c 3475 entry->ip = ip;
48ead020 3476
3558a5ac 3477 memcpy(&entry->buf, tbuffer, len + 1);
49e4154f
ZY
3478 __buffer_unlock_commit(buffer, event);
3479 ftrace_trace_stack(printk_trace, buffer, trace_ctx, 6, NULL);
e2ace001
AL
3480
3481out:
82d1b815 3482 ring_buffer_nest_end(buffer);
e2ace001
AL
3483 put_trace_buf();
3484
3485out_nobuffer:
48ead020 3486 preempt_enable_notrace();
07d777fe 3487 unpause_graph_tracing();
48ead020
FW
3488
3489 return len;
3490}
659372d3 3491
12883efb
SRRH
3492int trace_array_vprintk(struct trace_array *tr,
3493 unsigned long ip, const char *fmt, va_list args)
3494{
ac9d2cb1
SRG
3495 if (tracing_selftest_running && tr == &global_trace)
3496 return 0;
3497
1c5eb448 3498 return __trace_array_vprintk(tr->array_buffer.buffer, ip, fmt, args);
12883efb
SRRH
3499}
3500
38ce2a9e
SRV
3501/**
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)
3506 *
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
f2cc020d 3513 * be ever incorporated in normal use.
38ce2a9e
SRV
3514 *
3515 * trace_array_printk() can be used, as it will not add noise to the
3516 * top level tracing buffer.
3517 *
3518 * Note, trace_array_init_printk() must be called on @tr before this
3519 * can be used.
3520 */
12883efb
SRRH
3521int trace_array_printk(struct trace_array *tr,
3522 unsigned long ip, const char *fmt, ...)
3523{
3524 int ret;
3525 va_list ap;
3526
953ae45a
DI
3527 if (!tr)
3528 return -ENOENT;
3529
c791cc4b
SRV
3530 /* This is only allowed for created instances */
3531 if (tr == &global_trace)
3532 return 0;
3533
3534 if (!(tr->trace_flags & TRACE_ITER_PRINTK))
3535 return 0;
3536
12883efb
SRRH
3537 va_start(ap, fmt);
3538 ret = trace_array_vprintk(tr, ip, fmt, ap);
3539 va_end(ap);
3540 return ret;
3541}
f45d1225 3542EXPORT_SYMBOL_GPL(trace_array_printk);
12883efb 3543
38ce2a9e
SRV
3544/**
3545 * trace_array_init_printk - Initialize buffers for trace_array_printk()
3546 * @tr: The trace array to initialize the buffers for
3547 *
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.
3551 */
3552int trace_array_init_printk(struct trace_array *tr)
3553{
3554 if (!tr)
3555 return -ENOENT;
3556
3557 /* This is only allowed for created instances */
3558 if (tr == &global_trace)
3559 return -EINVAL;
3560
3561 return alloc_percpu_trace_buffer();
3562}
3563EXPORT_SYMBOL_GPL(trace_array_init_printk);
3564
13292494 3565int trace_array_printk_buf(struct trace_buffer *buffer,
12883efb
SRRH
3566 unsigned long ip, const char *fmt, ...)
3567{
3568 int ret;
3569 va_list ap;
3570
ddb8ea9e 3571 if (!(printk_trace->trace_flags & TRACE_ITER_PRINTK))
12883efb
SRRH
3572 return 0;
3573
3574 va_start(ap, fmt);
3575 ret = __trace_array_vprintk(buffer, ip, fmt, ap);
3576 va_end(ap);
3577 return ret;
3578}
3579
659372d3
SR
3580int trace_vprintk(unsigned long ip, const char *fmt, va_list args)
3581{
ddb8ea9e 3582 return trace_array_vprintk(printk_trace, ip, fmt, args);
659372d3 3583}
769b0441
FW
3584EXPORT_SYMBOL_GPL(trace_vprintk);
3585
e2ac8ef5 3586static void trace_iterator_increment(struct trace_iterator *iter)
5a90f577 3587{
6d158a81
SR
3588 struct ring_buffer_iter *buf_iter = trace_buffer_iter(iter, iter->cpu);
3589
5a90f577 3590 iter->idx++;
6d158a81 3591 if (buf_iter)
bc1a72af 3592 ring_buffer_iter_advance(buf_iter);
5a90f577
SR
3593}
3594
e309b41d 3595static struct trace_entry *
bc21b478
SR
3596peek_next_entry(struct trace_iterator *iter, int cpu, u64 *ts,
3597 unsigned long *lost_events)
dd0e545f 3598{
3928a8a2 3599 struct ring_buffer_event *event;
6d158a81 3600 struct ring_buffer_iter *buf_iter = trace_buffer_iter(iter, cpu);
dd0e545f 3601
c9b7a4a7 3602 if (buf_iter) {
d769041f 3603 event = ring_buffer_iter_peek(buf_iter, ts);
c9b7a4a7
SRV
3604 if (lost_events)
3605 *lost_events = ring_buffer_iter_dropped(buf_iter) ?
3606 (unsigned long)-1 : 0;
3607 } else {
1c5eb448 3608 event = ring_buffer_peek(iter->array_buffer->buffer, cpu, ts,
bc21b478 3609 lost_events);
c9b7a4a7 3610 }
d769041f 3611
4a9bd3f1
SR
3612 if (event) {
3613 iter->ent_size = ring_buffer_event_length(event);
3614 return ring_buffer_event_data(event);
3615 }
3616 iter->ent_size = 0;
3617 return NULL;
dd0e545f 3618}
d769041f 3619
dd0e545f 3620static struct trace_entry *
bc21b478
SR
3621__find_next_entry(struct trace_iterator *iter, int *ent_cpu,
3622 unsigned long *missing_events, u64 *ent_ts)
bc0c38d1 3623{
13292494 3624 struct trace_buffer *buffer = iter->array_buffer->buffer;
bc0c38d1 3625 struct trace_entry *ent, *next = NULL;
aa27497c 3626 unsigned long lost_events = 0, next_lost = 0;
b04cc6b1 3627 int cpu_file = iter->cpu_file;
3928a8a2 3628 u64 next_ts = 0, ts;
bc0c38d1 3629 int next_cpu = -1;
12b5da34 3630 int next_size = 0;
bc0c38d1
SR
3631 int cpu;
3632
b04cc6b1
FW
3633 /*
3634 * If we are in a per_cpu trace file, don't bother by iterating over
3635 * all cpu and peek directly.
3636 */
ae3b5093 3637 if (cpu_file > RING_BUFFER_ALL_CPUS) {
b04cc6b1
FW
3638 if (ring_buffer_empty_cpu(buffer, cpu_file))
3639 return NULL;
bc21b478 3640 ent = peek_next_entry(iter, cpu_file, ent_ts, missing_events);
b04cc6b1
FW
3641 if (ent_cpu)
3642 *ent_cpu = cpu_file;
3643
3644 return ent;
3645 }
3646
ab46428c 3647 for_each_tracing_cpu(cpu) {
dd0e545f 3648
3928a8a2
SR
3649 if (ring_buffer_empty_cpu(buffer, cpu))
3650 continue;
dd0e545f 3651
bc21b478 3652 ent = peek_next_entry(iter, cpu, &ts, &lost_events);
dd0e545f 3653
cdd31cd2
IM
3654 /*
3655 * Pick the entry with the smallest timestamp:
3656 */
3928a8a2 3657 if (ent && (!next || ts < next_ts)) {
bc0c38d1
SR
3658 next = ent;
3659 next_cpu = cpu;
3928a8a2 3660 next_ts = ts;
bc21b478 3661 next_lost = lost_events;
12b5da34 3662 next_size = iter->ent_size;
bc0c38d1
SR
3663 }
3664 }
3665
12b5da34
SR
3666 iter->ent_size = next_size;
3667
bc0c38d1
SR
3668 if (ent_cpu)
3669 *ent_cpu = next_cpu;
3670
3928a8a2
SR
3671 if (ent_ts)
3672 *ent_ts = next_ts;
3673
bc21b478
SR
3674 if (missing_events)
3675 *missing_events = next_lost;
3676
bc0c38d1
SR
3677 return next;
3678}
3679
efbbdaa2
MH
3680#define STATIC_FMT_BUF_SIZE 128
3681static char static_fmt_buf[STATIC_FMT_BUF_SIZE];
3682
80a76994 3683char *trace_iter_expand_format(struct trace_iterator *iter)
efbbdaa2
MH
3684{
3685 char *tmp;
3686
0e1e71d3
SRV
3687 /*
3688 * iter->tr is NULL when used with tp_printk, which makes
3689 * this get called where it is not safe to call krealloc().
3690 */
3691 if (!iter->tr || iter->fmt == static_fmt_buf)
efbbdaa2
MH
3692 return NULL;
3693
3694 tmp = krealloc(iter->fmt, iter->fmt_size + STATIC_FMT_BUF_SIZE,
3695 GFP_KERNEL);
3696 if (tmp) {
3697 iter->fmt_size += STATIC_FMT_BUF_SIZE;
3698 iter->fmt = tmp;
3699 }
3700
3701 return tmp;
3702}
3703
9a6944fe 3704/* Returns true if the string is safe to dereference from an event */
afd2627f 3705static bool trace_safe_str(struct trace_iterator *iter, const char *str)
9a6944fe
SRV
3706{
3707 unsigned long addr = (unsigned long)str;
3708 struct trace_event *trace_event;
3709 struct trace_event_call *event;
3710
3711 /* OK if part of the event data */
3712 if ((addr >= (unsigned long)iter->ent) &&
3713 (addr < (unsigned long)iter->ent + iter->ent_size))
3714 return true;
3715
3716 /* OK if part of the temp seq buffer */
3717 if ((addr >= (unsigned long)iter->tmp_seq.buffer) &&
40fc60e3 3718 (addr < (unsigned long)iter->tmp_seq.buffer + TRACE_SEQ_BUFFER_SIZE))
9a6944fe
SRV
3719 return true;
3720
3721 /* Core rodata can not be freed */
3722 if (is_kernel_rodata(addr))
3723 return true;
3724
3725 if (trace_is_tracepoint_string(str))
3726 return true;
3727
3728 /*
3729 * Now this could be a module event, referencing core module
3730 * data, which is OK.
3731 */
3732 if (!iter->ent)
3733 return false;
3734
3735 trace_event = ftrace_find_event(iter->ent->type);
3736 if (!trace_event)
3737 return false;
3738
3739 event = container_of(trace_event, struct trace_event_call, event);
1d18538e 3740 if ((event->flags & TRACE_EVENT_FL_DYNAMIC) || !event->module)
9a6944fe
SRV
3741 return false;
3742
3743 /* Would rather have rodata, but this will suffice */
1d18538e 3744 if (within_module_core(addr, event->module))
9a6944fe
SRV
3745 return true;
3746
3747 return false;
3748}
3749
9a6944fe 3750/**
afd2627f 3751 * ignore_event - Check dereferenced fields while writing to the seq buffer
9a6944fe 3752 * @iter: The iterator that holds the seq buffer and the event being printed
9a6944fe 3753 *
afd2627f
SR
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.
3761 *
3762 * This function will check if the event has any fields flagged as needing
3763 * to be checked at runtime and perform those checks.
3764 *
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.
3767 *
3768 * @return: true if the event is unsafe and should be ignored,
3769 * false otherwise.
9a6944fe 3770 */
afd2627f 3771bool ignore_event(struct trace_iterator *iter)
9a6944fe 3772{
afd2627f
SR
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;
3778 const void *ptr;
9a6944fe 3779
afd2627f 3780 trace_event = ftrace_find_event(iter->ent->type);
9a6944fe 3781
afd2627f 3782 seq = &iter->seq;
9a6944fe 3783
afd2627f
SR
3784 if (!trace_event) {
3785 trace_seq_printf(seq, "EVENT ID %d NOT FOUND?\n", iter->ent->type);
3786 return true;
50a3242d
SR
3787 }
3788
afd2627f
SR
3789 event = container_of(trace_event, struct trace_event_call, event);
3790 if (!(event->flags & TRACE_EVENT_FL_TEST_STR))
3791 return false;
9a6944fe 3792
afd2627f
SR
3793 head = trace_get_fields(event);
3794 if (!head) {
3795 trace_seq_printf(seq, "FIELDS FOR EVENT '%s' NOT FOUND?\n",
3796 trace_event_name(event));
3797 return true;
3798 }
07714b4b 3799
afd2627f
SR
3800 /* Offsets are from the iter->ent that points to the raw event */
3801 ptr = iter->ent;
07714b4b 3802
afd2627f
SR
3803 list_for_each_entry(field, head, link) {
3804 const char *str;
3805 bool good;
07714b4b 3806
afd2627f 3807 if (!field->needs_test)
07714b4b 3808 continue;
07714b4b 3809
afd2627f 3810 str = *(const char **)(ptr + field->offset);
9a6944fe 3811
afd2627f 3812 good = trace_safe_str(iter, str);
07714b4b 3813
9a6944fe
SRV
3814 /*
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
3821 * for reference.
3822 */
afd2627f
SR
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);
3827 return true;
9a6944fe 3828 }
9a6944fe 3829 }
afd2627f 3830 return false;
9a6944fe
SRV
3831}
3832
efbbdaa2
MH
3833const char *trace_event_format(struct trace_iterator *iter, const char *fmt)
3834{
3835 const char *p, *new_fmt;
3836 char *q;
3837
3838 if (WARN_ON_ONCE(!fmt))
3839 return fmt;
3840
0e1e71d3 3841 if (!iter->tr || iter->tr->trace_flags & TRACE_ITER_HASH_PTR)
a345a671
MH
3842 return fmt;
3843
efbbdaa2
MH
3844 p = fmt;
3845 new_fmt = q = iter->fmt;
3846 while (*p) {
3847 if (unlikely(q - new_fmt + 3 > iter->fmt_size)) {
3848 if (!trace_iter_expand_format(iter))
3849 return fmt;
3850
3851 q += iter->fmt - new_fmt;
3852 new_fmt = iter->fmt;
3853 }
3854
3855 *q++ = *p++;
3856
3857 /* Replace %p with %px */
3858 if (p[-1] == '%') {
3859 if (p[0] == '%') {
3860 *q++ = *p++;
3861 } else if (p[0] == 'p' && !isalnum(p[1])) {
3862 *q++ = *p++;
3863 *q++ = 'x';
3864 }
3865 }
3866 }
3867 *q = '\0';
3868
3869 return new_fmt;
3870}
3871
8e99cf91 3872#define STATIC_TEMP_BUF_SIZE 128
8fa655a3 3873static char static_temp_buf[STATIC_TEMP_BUF_SIZE] __aligned(4);
8e99cf91 3874
dd0e545f 3875/* Find the next real entry, without updating the iterator itself */
c4a8e8be
FW
3876struct trace_entry *trace_find_next_entry(struct trace_iterator *iter,
3877 int *ent_cpu, u64 *ent_ts)
bc0c38d1 3878{
ff895103
SRV
3879 /* __find_next_entry will reset ent_size */
3880 int ent_size = iter->ent_size;
3881 struct trace_entry *entry;
3882
8e99cf91
SRV
3883 /*
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()
3890 */
3891 if (iter->temp == static_temp_buf &&
3892 STATIC_TEMP_BUF_SIZE < ent_size)
3893 return NULL;
3894
ff895103
SRV
3895 /*
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.
3899 */
3900 if (iter->ent && iter->ent != iter->temp) {
8e99cf91
SRV
3901 if ((!iter->temp || iter->temp_size < iter->ent_size) &&
3902 !WARN_ON_ONCE(iter->temp == static_temp_buf)) {
851e6f61
SRV
3903 void *temp;
3904 temp = kmalloc(iter->ent_size, GFP_KERNEL);
3905 if (!temp)
ff895103 3906 return NULL;
851e6f61
SRV
3907 kfree(iter->temp);
3908 iter->temp = temp;
3909 iter->temp_size = iter->ent_size;
ff895103
SRV
3910 }
3911 memcpy(iter->temp, iter->ent, iter->ent_size);
ff895103
SRV
3912 iter->ent = iter->temp;
3913 }
3914 entry = __find_next_entry(iter, ent_cpu, NULL, ent_ts);
3915 /* Put back the original ent_size */
3916 iter->ent_size = ent_size;
3917
3918 return entry;
dd0e545f
SR
3919}
3920
3921/* Find the next real entry, and increment the iterator to the next entry */
955b61e5 3922void *trace_find_next_entry_inc(struct trace_iterator *iter)
dd0e545f 3923{
bc21b478
SR
3924 iter->ent = __find_next_entry(iter, &iter->cpu,
3925 &iter->lost_events, &iter->ts);
dd0e545f 3926
3928a8a2 3927 if (iter->ent)
e2ac8ef5 3928 trace_iterator_increment(iter);
dd0e545f 3929
3928a8a2 3930 return iter->ent ? iter : NULL;
b3806b43 3931}
bc0c38d1 3932
e309b41d 3933static void trace_consume(struct trace_iterator *iter)
b3806b43 3934{
1c5eb448 3935 ring_buffer_consume(iter->array_buffer->buffer, iter->cpu, &iter->ts,
bc21b478 3936 &iter->lost_events);
bc0c38d1
SR
3937}
3938
e309b41d 3939static void *s_next(struct seq_file *m, void *v, loff_t *pos)
bc0c38d1
SR
3940{
3941 struct trace_iterator *iter = m->private;
bc0c38d1 3942 int i = (int)*pos;
4e3c3333 3943 void *ent;
bc0c38d1 3944
a63ce5b3
SR
3945 WARN_ON_ONCE(iter->leftover);
3946
bc0c38d1
SR
3947 (*pos)++;
3948
3949 /* can't go backwards */
3950 if (iter->idx > i)
3951 return NULL;
3952
3953 if (iter->idx < 0)
955b61e5 3954 ent = trace_find_next_entry_inc(iter);
bc0c38d1
SR
3955 else
3956 ent = iter;
3957
3958 while (ent && iter->idx < i)
955b61e5 3959 ent = trace_find_next_entry_inc(iter);
bc0c38d1
SR
3960
3961 iter->pos = *pos;
3962
bc0c38d1
SR
3963 return ent;
3964}
3965
955b61e5 3966void tracing_iter_reset(struct trace_iterator *iter, int cpu)
2f26ebd5 3967{
2f26ebd5
SR
3968 struct ring_buffer_iter *buf_iter;
3969 unsigned long entries = 0;
3970 u64 ts;
3971
1c5eb448 3972 per_cpu_ptr(iter->array_buffer->data, cpu)->skipped_entries = 0;
2f26ebd5 3973
6d158a81
SR
3974 buf_iter = trace_buffer_iter(iter, cpu);
3975 if (!buf_iter)
2f26ebd5
SR
3976 return;
3977
2f26ebd5
SR
3978 ring_buffer_iter_reset(buf_iter);
3979
3980 /*
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.
3984 */
69243720 3985 while (ring_buffer_iter_peek(buf_iter, &ts)) {
1c5eb448 3986 if (ts >= iter->array_buffer->time_start)
2f26ebd5
SR
3987 break;
3988 entries++;
bc1a72af 3989 ring_buffer_iter_advance(buf_iter);
49aa8a1f
ZY
3990 /* This could be a big loop */
3991 cond_resched();
2f26ebd5
SR
3992 }
3993
1c5eb448 3994 per_cpu_ptr(iter->array_buffer->data, cpu)->skipped_entries = entries;
2f26ebd5
SR
3995}
3996
d7350c3f 3997/*
d7350c3f
FW
3998 * The current tracer is copied to avoid a global locking
3999 * all around.
4000 */
bc0c38d1
SR
4001static void *s_start(struct seq_file *m, loff_t *pos)
4002{
4003 struct trace_iterator *iter = m->private;
2b6080f2 4004 struct trace_array *tr = iter->tr;
b04cc6b1 4005 int cpu_file = iter->cpu_file;
bc0c38d1
SR
4006 void *p = NULL;
4007 loff_t l = 0;
3928a8a2 4008 int cpu;
bc0c38d1
SR
4009
4010 mutex_lock(&trace_types_lock);
34232fcf 4011 if (unlikely(tr->current_trace != iter->trace)) {
eecb91b9
ZY
4012 /* Close iter->trace before switching to the new current tracer */
4013 if (iter->trace->close)
4014 iter->trace->close(iter);
9182b519 4015 iter->trace = tr->current_trace;
eecb91b9
ZY
4016 /* Reopen the new current tracer */
4017 if (iter->trace->open)
4018 iter->trace->open(iter);
4019 }
d7350c3f 4020 mutex_unlock(&trace_types_lock);
bc0c38d1 4021
12883efb 4022#ifdef CONFIG_TRACER_MAX_TRACE
debdd57f
HT
4023 if (iter->snapshot && iter->trace->use_max_tr)
4024 return ERR_PTR(-EBUSY);
12883efb 4025#endif
debdd57f 4026
bc0c38d1
SR
4027 if (*pos != iter->pos) {
4028 iter->ent = NULL;
4029 iter->cpu = 0;
4030 iter->idx = -1;
4031
ae3b5093 4032 if (cpu_file == RING_BUFFER_ALL_CPUS) {
b04cc6b1 4033 for_each_tracing_cpu(cpu)
2f26ebd5 4034 tracing_iter_reset(iter, cpu);
b04cc6b1 4035 } else
2f26ebd5 4036 tracing_iter_reset(iter, cpu_file);
bc0c38d1 4037
ac91d854 4038 iter->leftover = 0;
bc0c38d1
SR
4039 for (p = iter; p && l < *pos; p = s_next(m, p, &l))
4040 ;
4041
4042 } else {
a63ce5b3
SR
4043 /*
4044 * If we overflowed the seq_file before, then we want
4045 * to just reuse the trace_seq buffer again.
4046 */
4047 if (iter->leftover)
4048 p = iter;
4049 else {
4050 l = *pos - 1;
4051 p = s_next(m, p, &l);
4052 }
bc0c38d1
SR
4053 }
4054
4f535968 4055 trace_event_read_lock();
7e53bd42 4056 trace_access_lock(cpu_file);
bc0c38d1
SR
4057 return p;
4058}
4059
4060static void s_stop(struct seq_file *m, void *p)
4061{
7e53bd42
LJ
4062 struct trace_iterator *iter = m->private;
4063
12883efb 4064#ifdef CONFIG_TRACER_MAX_TRACE
debdd57f
HT
4065 if (iter->snapshot && iter->trace->use_max_tr)
4066 return;
12883efb 4067#endif
debdd57f 4068
7e53bd42 4069 trace_access_unlock(iter->cpu_file);
4f535968 4070 trace_event_read_unlock();
bc0c38d1
SR
4071}
4072
ecffc8a8 4073static void
1c5eb448 4074get_total_entries_cpu(struct array_buffer *buf, unsigned long *total,
ecffc8a8
DA
4075 unsigned long *entries, int cpu)
4076{
4077 unsigned long count;
4078
4079 count = ring_buffer_entries_cpu(buf->buffer, cpu);
4080 /*
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.
4084 */
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 */
4088 *total = count;
4089 } else
4090 *total = count +
4091 ring_buffer_overrun_cpu(buf->buffer, cpu);
4092 *entries = count;
4093}
4094
39eaf7ef 4095static void
1c5eb448 4096get_total_entries(struct array_buffer *buf,
12883efb 4097 unsigned long *total, unsigned long *entries)
39eaf7ef 4098{
ecffc8a8 4099 unsigned long t, e;
39eaf7ef
SR
4100 int cpu;
4101
4102 *total = 0;
4103 *entries = 0;
4104
4105 for_each_tracing_cpu(cpu) {
ecffc8a8
DA
4106 get_total_entries_cpu(buf, &t, &e, cpu);
4107 *total += t;
4108 *entries += e;
39eaf7ef
SR
4109 }
4110}
4111
ecffc8a8
DA
4112unsigned long trace_total_entries_cpu(struct trace_array *tr, int cpu)
4113{
4114 unsigned long total, entries;
4115
4116 if (!tr)
4117 tr = &global_trace;
4118
1c5eb448 4119 get_total_entries_cpu(&tr->array_buffer, &total, &entries, cpu);
ecffc8a8
DA
4120
4121 return entries;
4122}
4123
4124unsigned long trace_total_entries(struct trace_array *tr)
4125{
4126 unsigned long total, entries;
4127
4128 if (!tr)
4129 tr = &global_trace;
4130
1c5eb448 4131 get_total_entries(&tr->array_buffer, &total, &entries);
ecffc8a8
DA
4132
4133 return entries;
4134}
4135
e309b41d 4136static void print_lat_help_header(struct seq_file *m)
bc0c38d1 4137{
795d6379 4138 seq_puts(m, "# _------=> CPU# \n"
289e7b0f 4139 "# / _-----=> irqs-off/BH-disabled\n"
795d6379
SAS
4140 "# | / _----=> need-resched \n"
4141 "# || / _---=> hardirq/softirq \n"
4142 "# ||| / _--=> preempt-depth \n"
54357f0c
TG
4143 "# |||| / _-=> migrate-disable \n"
4144 "# ||||| / delay \n"
4145 "# cmd pid |||||| time | caller \n"
4146 "# \\ / |||||| \\ | / \n");
bc0c38d1
SR
4147}
4148
1c5eb448 4149static void print_event_info(struct array_buffer *buf, struct seq_file *m)
bc0c38d1 4150{
39eaf7ef
SR
4151 unsigned long total;
4152 unsigned long entries;
4153
12883efb 4154 get_total_entries(buf, &total, &entries);
39eaf7ef
SR
4155 seq_printf(m, "# entries-in-buffer/entries-written: %lu/%lu #P:%d\n",
4156 entries, total, num_online_cpus());
4157 seq_puts(m, "#\n");
4158}
4159
1c5eb448 4160static void print_func_help_header(struct array_buffer *buf, struct seq_file *m,
441dae8f 4161 unsigned int flags)
39eaf7ef 4162{
441dae8f
JF
4163 bool tgid = flags & TRACE_ITER_RECORD_TGID;
4164
12883efb 4165 print_event_info(buf, m);
441dae8f 4166
795d6379
SAS
4167 seq_printf(m, "# TASK-PID %s CPU# TIMESTAMP FUNCTION\n", tgid ? " TGID " : "");
4168 seq_printf(m, "# | | %s | | |\n", tgid ? " | " : "");
bc0c38d1
SR
4169}
4170
1c5eb448 4171static void print_func_help_header_irq(struct array_buffer *buf, struct seq_file *m,
441dae8f 4172 unsigned int flags)
77271ce4 4173{
441dae8f 4174 bool tgid = flags & TRACE_ITER_RECORD_TGID;
2d601b98 4175 static const char space[] = " ";
795d6379 4176 int prec = tgid ? 12 : 2;
b11fb737 4177
9e738215
QP
4178 print_event_info(buf, m);
4179
289e7b0f 4180 seq_printf(m, "# %.*s _-----=> irqs-off/BH-disabled\n", prec, space);
795d6379
SAS
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);
54357f0c
TG
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, " | ");
77271ce4 4188}
bc0c38d1 4189
62b915f1 4190void
bc0c38d1
SR
4191print_trace_header(struct seq_file *m, struct trace_iterator *iter)
4192{
983f938a 4193 unsigned long sym_flags = (global_trace.trace_flags & TRACE_ITER_SYM_MASK);
1c5eb448 4194 struct array_buffer *buf = iter->array_buffer;
12883efb 4195 struct trace_array_cpu *data = per_cpu_ptr(buf->data, buf->cpu);
2b6080f2 4196 struct tracer *type = iter->trace;
39eaf7ef
SR
4197 unsigned long entries;
4198 unsigned long total;
2decd16f 4199 const char *name = type->name;
bc0c38d1 4200
12883efb 4201 get_total_entries(buf, &total, &entries);
bc0c38d1 4202
888b55dc 4203 seq_printf(m, "# %s latency trace v1.1.5 on %s\n",
ed896837 4204 name, init_utsname()->release);
888b55dc 4205 seq_puts(m, "# -----------------------------------"
bc0c38d1 4206 "---------------------------------\n");
888b55dc 4207 seq_printf(m, "# latency: %lu us, #%lu/%lu, CPU#%d |"
bc0c38d1 4208 " (M:%s VP:%d, KP:%d, SP:%d HP:%d",
57f50be1 4209 nsecs_to_usecs(data->saved_latency),
bc0c38d1 4210 entries,
4c11d7ae 4211 total,
12883efb 4212 buf->cpu,
3bffa47a 4213 preempt_model_str(),
bc0c38d1
SR
4214 /* These are reserved for later use */
4215 0, 0, 0, 0);
4216#ifdef CONFIG_SMP
4217 seq_printf(m, " #P:%d)\n", num_online_cpus());
4218#else
4219 seq_puts(m, ")\n");
4220#endif
888b55dc
KM
4221 seq_puts(m, "# -----------------\n");
4222 seq_printf(m, "# | task: %.16s-%d "
bc0c38d1 4223 "(uid:%d nice:%ld policy:%ld rt_prio:%ld)\n",
d20b92ab
EB
4224 data->comm, data->pid,
4225 from_kuid_munged(seq_user_ns(m), data->uid), data->nice,
bc0c38d1 4226 data->policy, data->rt_priority);
888b55dc 4227 seq_puts(m, "# -----------------\n");
bc0c38d1
SR
4228
4229 if (data->critical_start) {
888b55dc 4230 seq_puts(m, "# => started at: ");
214023c3
SR
4231 seq_print_ip_sym(&iter->seq, data->critical_start, sym_flags);
4232 trace_print_seq(m, &iter->seq);
888b55dc 4233 seq_puts(m, "\n# => ended at: ");
214023c3
SR
4234 seq_print_ip_sym(&iter->seq, data->critical_end, sym_flags);
4235 trace_print_seq(m, &iter->seq);
8248ac05 4236 seq_puts(m, "\n#\n");
bc0c38d1
SR
4237 }
4238
888b55dc 4239 seq_puts(m, "#\n");
bc0c38d1
SR
4240}
4241
a309720c
SR
4242static void test_cpu_buff_start(struct trace_iterator *iter)
4243{
4244 struct trace_seq *s = &iter->seq;
983f938a 4245 struct trace_array *tr = iter->tr;
a309720c 4246
983f938a 4247 if (!(tr->trace_flags & TRACE_ITER_ANNOTATE))
12ef7d44
SR
4248 return;
4249
4250 if (!(iter->iter_flags & TRACE_FILE_ANNOTATE))
4251 return;
4252
4dbbe2d8
MK
4253 if (cpumask_available(iter->started) &&
4254 cpumask_test_cpu(iter->cpu, iter->started))
a309720c
SR
4255 return;
4256
1c5eb448 4257 if (per_cpu_ptr(iter->array_buffer->data, iter->cpu)->skipped_entries)
2f26ebd5
SR
4258 return;
4259
4dbbe2d8 4260 if (cpumask_available(iter->started))
919cd979 4261 cpumask_set_cpu(iter->cpu, iter->started);
b0dfa978
FW
4262
4263 /* Don't print started cpu buffer for the first entry of the trace */
4264 if (iter->idx > 1)
4265 trace_seq_printf(s, "##### CPU %u buffer started ####\n",
4266 iter->cpu);
a309720c
SR
4267}
4268
2c4f035f 4269static enum print_line_t print_trace_fmt(struct trace_iterator *iter)
bc0c38d1 4270{
983f938a 4271 struct trace_array *tr = iter->tr;
214023c3 4272 struct trace_seq *s = &iter->seq;
983f938a 4273 unsigned long sym_flags = (tr->trace_flags & TRACE_ITER_SYM_MASK);
4e3c3333 4274 struct trace_entry *entry;
f633cef0 4275 struct trace_event *event;
bc0c38d1 4276
4e3c3333 4277 entry = iter->ent;
dd0e545f 4278
a309720c
SR
4279 test_cpu_buff_start(iter);
4280
c4a8e8be 4281 event = ftrace_find_event(entry->type);
bc0c38d1 4282
983f938a 4283 if (tr->trace_flags & TRACE_ITER_CONTEXT_INFO) {
19a7fe20
SRRH
4284 if (iter->iter_flags & TRACE_FILE_LAT_FMT)
4285 trace_print_lat_context(iter);
4286 else
4287 trace_print_context(iter);
c4a8e8be 4288 }
bc0c38d1 4289
19a7fe20
SRRH
4290 if (trace_seq_has_overflowed(s))
4291 return TRACE_TYPE_PARTIAL_LINE;
4292
80a76994
SRG
4293 if (event) {
4294 if (tr->trace_flags & TRACE_ITER_FIELDS)
4295 return print_event_fields(iter, event);
8cd63406
SR
4296 /*
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.
4300 */
bcba8d4d 4301 if ((tr->text_delta) &&
8cd63406
SR
4302 event->type > __TRACE_LAST_TYPE)
4303 return print_event_fields(iter, event);
4304
a9a57763 4305 return event->funcs->trace(iter, sym_flags, event);
80a76994 4306 }
d9793bd8 4307
19a7fe20 4308 trace_seq_printf(s, "Unknown type %d\n", entry->type);
02b67518 4309
19a7fe20 4310 return trace_handle_return(s);
bc0c38d1
SR
4311}
4312
2c4f035f 4313static enum print_line_t print_raw_fmt(struct trace_iterator *iter)
f9896bf3 4314{
983f938a 4315 struct trace_array *tr = iter->tr;
f9896bf3
IM
4316 struct trace_seq *s = &iter->seq;
4317 struct trace_entry *entry;
f633cef0 4318 struct trace_event *event;
f9896bf3
IM
4319
4320 entry = iter->ent;
dd0e545f 4321
983f938a 4322 if (tr->trace_flags & TRACE_ITER_CONTEXT_INFO)
19a7fe20
SRRH
4323 trace_seq_printf(s, "%d %d %llu ",
4324 entry->pid, iter->cpu, iter->ts);
4325
4326 if (trace_seq_has_overflowed(s))
4327 return TRACE_TYPE_PARTIAL_LINE;
f9896bf3 4328
f633cef0 4329 event = ftrace_find_event(entry->type);
268ccda0 4330 if (event)
a9a57763 4331 return event->funcs->raw(iter, 0, event);
d9793bd8 4332
19a7fe20 4333 trace_seq_printf(s, "%d ?\n", entry->type);
777e208d 4334
19a7fe20 4335 return trace_handle_return(s);
f9896bf3
IM
4336}
4337
2c4f035f 4338static enum print_line_t print_hex_fmt(struct trace_iterator *iter)
5e3ca0ec 4339{
983f938a 4340 struct trace_array *tr = iter->tr;
5e3ca0ec
IM
4341 struct trace_seq *s = &iter->seq;
4342 unsigned char newline = '\n';
4343 struct trace_entry *entry;
f633cef0 4344 struct trace_event *event;
5e3ca0ec
IM
4345
4346 entry = iter->ent;
dd0e545f 4347
983f938a 4348 if (tr->trace_flags & TRACE_ITER_CONTEXT_INFO) {
19a7fe20
SRRH
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;
c4a8e8be 4354 }
5e3ca0ec 4355
f633cef0 4356 event = ftrace_find_event(entry->type);
268ccda0 4357 if (event) {
a9a57763 4358 enum print_line_t ret = event->funcs->hex(iter, 0, event);
d9793bd8
ACM
4359 if (ret != TRACE_TYPE_HANDLED)
4360 return ret;
4361 }
7104f300 4362
19a7fe20 4363 SEQ_PUT_FIELD(s, newline);
5e3ca0ec 4364
19a7fe20 4365 return trace_handle_return(s);
5e3ca0ec
IM
4366}
4367
2c4f035f 4368static enum print_line_t print_bin_fmt(struct trace_iterator *iter)
cb0f12aa 4369{
983f938a 4370 struct trace_array *tr = iter->tr;
cb0f12aa
IM
4371 struct trace_seq *s = &iter->seq;
4372 struct trace_entry *entry;
f633cef0 4373 struct trace_event *event;
cb0f12aa
IM
4374
4375 entry = iter->ent;
dd0e545f 4376
983f938a 4377 if (tr->trace_flags & TRACE_ITER_CONTEXT_INFO) {
19a7fe20
SRRH
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;
c4a8e8be 4383 }
cb0f12aa 4384
f633cef0 4385 event = ftrace_find_event(entry->type);
a9a57763
SR
4386 return event ? event->funcs->binary(iter, 0, event) :
4387 TRACE_TYPE_HANDLED;
cb0f12aa
IM
4388}
4389
62b915f1 4390int trace_empty(struct trace_iterator *iter)
bc0c38d1 4391{
6d158a81 4392 struct ring_buffer_iter *buf_iter;
bc0c38d1
SR
4393 int cpu;
4394
9aba60fe 4395 /* If we are looking at one CPU buffer, only check that one */
ae3b5093 4396 if (iter->cpu_file != RING_BUFFER_ALL_CPUS) {
9aba60fe 4397 cpu = iter->cpu_file;
6d158a81
SR
4398 buf_iter = trace_buffer_iter(iter, cpu);
4399 if (buf_iter) {
4400 if (!ring_buffer_iter_empty(buf_iter))
9aba60fe
SR
4401 return 0;
4402 } else {
1c5eb448 4403 if (!ring_buffer_empty_cpu(iter->array_buffer->buffer, cpu))
9aba60fe
SR
4404 return 0;
4405 }
4406 return 1;
4407 }
4408
ab46428c 4409 for_each_tracing_cpu(cpu) {
6d158a81
SR
4410 buf_iter = trace_buffer_iter(iter, cpu);
4411 if (buf_iter) {
4412 if (!ring_buffer_iter_empty(buf_iter))
d769041f
SR
4413 return 0;
4414 } else {
1c5eb448 4415 if (!ring_buffer_empty_cpu(iter->array_buffer->buffer, cpu))
d769041f
SR
4416 return 0;
4417 }
bc0c38d1 4418 }
d769041f 4419
797d3712 4420 return 1;
bc0c38d1
SR
4421}
4422
4f535968 4423/* Called with trace_event_read_lock() held. */
955b61e5 4424enum print_line_t print_trace_line(struct trace_iterator *iter)
f9896bf3 4425{
983f938a
SRRH
4426 struct trace_array *tr = iter->tr;
4427 unsigned long trace_flags = tr->trace_flags;
2c4f035f
FW
4428 enum print_line_t ret;
4429
19a7fe20 4430 if (iter->lost_events) {
c9b7a4a7
SRV
4431 if (iter->lost_events == (unsigned long)-1)
4432 trace_seq_printf(&iter->seq, "CPU:%d [LOST EVENTS]\n",
4433 iter->cpu);
4434 else
4435 trace_seq_printf(&iter->seq, "CPU:%d [LOST %lu EVENTS]\n",
4436 iter->cpu, iter->lost_events);
19a7fe20
SRRH
4437 if (trace_seq_has_overflowed(&iter->seq))
4438 return TRACE_TYPE_PARTIAL_LINE;
4439 }
bc21b478 4440
2c4f035f
FW
4441 if (iter->trace && iter->trace->print_line) {
4442 ret = iter->trace->print_line(iter);
4443 if (ret != TRACE_TYPE_UNHANDLED)
4444 return ret;
4445 }
72829bc3 4446
09ae7234
SRRH
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);
4451
48ead020
FW
4452 if (iter->ent->type == TRACE_BPRINT &&
4453 trace_flags & TRACE_ITER_PRINTK &&
4454 trace_flags & TRACE_ITER_PRINTK_MSGONLY)
5ef841f6 4455 return trace_print_bprintk_msg_only(iter);
48ead020 4456
66896a85
FW
4457 if (iter->ent->type == TRACE_PRINT &&
4458 trace_flags & TRACE_ITER_PRINTK &&
4459 trace_flags & TRACE_ITER_PRINTK_MSGONLY)
5ef841f6 4460 return trace_print_printk_msg_only(iter);
66896a85 4461
cb0f12aa
IM
4462 if (trace_flags & TRACE_ITER_BIN)
4463 return print_bin_fmt(iter);
4464
5e3ca0ec
IM
4465 if (trace_flags & TRACE_ITER_HEX)
4466 return print_hex_fmt(iter);
4467
f9896bf3
IM
4468 if (trace_flags & TRACE_ITER_RAW)
4469 return print_raw_fmt(iter);
4470
f9896bf3
IM
4471 return print_trace_fmt(iter);
4472}
4473
7e9a49ef
JO
4474void trace_latency_header(struct seq_file *m)
4475{
4476 struct trace_iterator *iter = m->private;
983f938a 4477 struct trace_array *tr = iter->tr;
7e9a49ef
JO
4478
4479 /* print nothing if the buffers are empty */
4480 if (trace_empty(iter))
4481 return;
4482
4483 if (iter->iter_flags & TRACE_FILE_LAT_FMT)
4484 print_trace_header(m, iter);
4485
983f938a 4486 if (!(tr->trace_flags & TRACE_ITER_VERBOSE))
7e9a49ef
JO
4487 print_lat_help_header(m);
4488}
4489
62b915f1
JO
4490void trace_default_header(struct seq_file *m)
4491{
4492 struct trace_iterator *iter = m->private;
983f938a
SRRH
4493 struct trace_array *tr = iter->tr;
4494 unsigned long trace_flags = tr->trace_flags;
62b915f1 4495
f56e7f8e
JO
4496 if (!(trace_flags & TRACE_ITER_CONTEXT_INFO))
4497 return;
4498
62b915f1
JO
4499 if (iter->iter_flags & TRACE_FILE_LAT_FMT) {
4500 /* print nothing if the buffers are empty */
4501 if (trace_empty(iter))
4502 return;
4503 print_trace_header(m, iter);
4504 if (!(trace_flags & TRACE_ITER_VERBOSE))
4505 print_lat_help_header(m);
4506 } else {
77271ce4
SR
4507 if (!(trace_flags & TRACE_ITER_VERBOSE)) {
4508 if (trace_flags & TRACE_ITER_IRQ_INFO)
1c5eb448 4509 print_func_help_header_irq(iter->array_buffer,
441dae8f 4510 m, trace_flags);
77271ce4 4511 else
1c5eb448 4512 print_func_help_header(iter->array_buffer, m,
441dae8f 4513 trace_flags);
77271ce4 4514 }
62b915f1
JO
4515 }
4516}
4517
e0a413f6
SR
4518static void test_ftrace_alive(struct seq_file *m)
4519{
4520 if (!ftrace_is_dead())
4521 return;
d79ac28f
RV
4522 seq_puts(m, "# WARNING: FUNCTION TRACING IS CORRUPTED\n"
4523 "# MAY BE MISSING FUNCTION EVENTS\n");
e0a413f6
SR
4524}
4525
d8741e2e 4526#ifdef CONFIG_TRACER_MAX_TRACE
f1affcaa 4527static void show_snapshot_main_help(struct seq_file *m)
d8741e2e 4528{
d79ac28f
RV
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");
d8741e2e 4535}
f1affcaa
SRRH
4536
4537static void show_snapshot_percpu_help(struct seq_file *m)
4538{
fa6f0cc7 4539 seq_puts(m, "# echo 0 > snapshot : Invalid for per_cpu snapshot file.\n");
f1affcaa 4540#ifdef CONFIG_RING_BUFFER_ALLOW_SWAP
d79ac28f
RV
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");
f1affcaa 4543#else
d79ac28f
RV
4544 seq_puts(m, "# echo 1 > snapshot : Not supported with this kernel.\n"
4545 "# Must use main snapshot file to allocate.\n");
f1affcaa 4546#endif
d79ac28f
RV
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");
f1affcaa
SRRH
4550}
4551
d8741e2e
SRRH
4552static void print_snapshot_help(struct seq_file *m, struct trace_iterator *iter)
4553{
45ad21ca 4554 if (iter->tr->allocated_snapshot)
fa6f0cc7 4555 seq_puts(m, "#\n# * Snapshot is allocated *\n#\n");
d8741e2e 4556 else
fa6f0cc7 4557 seq_puts(m, "#\n# * Snapshot is freed *\n#\n");
d8741e2e 4558
fa6f0cc7 4559 seq_puts(m, "# Snapshot commands:\n");
f1affcaa
SRRH
4560 if (iter->cpu_file == RING_BUFFER_ALL_CPUS)
4561 show_snapshot_main_help(m);
4562 else
4563 show_snapshot_percpu_help(m);
d8741e2e
SRRH
4564}
4565#else
4566/* Should never be called */
4567static inline void print_snapshot_help(struct seq_file *m, struct trace_iterator *iter) { }
4568#endif
4569
bc0c38d1
SR
4570static int s_show(struct seq_file *m, void *v)
4571{
4572 struct trace_iterator *iter = v;
a63ce5b3 4573 int ret;
bc0c38d1
SR
4574
4575 if (iter->ent == NULL) {
4576 if (iter->tr) {
4577 seq_printf(m, "# tracer: %s\n", iter->trace->name);
4578 seq_puts(m, "#\n");
e0a413f6 4579 test_ftrace_alive(m);
bc0c38d1 4580 }
d8741e2e
SRRH
4581 if (iter->snapshot && trace_empty(iter))
4582 print_snapshot_help(m, iter);
4583 else if (iter->trace && iter->trace->print_header)
8bba1bf5 4584 iter->trace->print_header(m);
62b915f1
JO
4585 else
4586 trace_default_header(m);
4587
a63ce5b3
SR
4588 } else if (iter->leftover) {
4589 /*
4590 * If we filled the seq_file buffer earlier, we
4591 * want to just show it now.
4592 */
4593 ret = trace_print_seq(m, &iter->seq);
4594
4595 /* ret should this time be zero, but you never know */
4596 iter->leftover = ret;
4597
bc0c38d1 4598 } else {
b55b0a0d
SRG
4599 ret = print_trace_line(iter);
4600 if (ret == TRACE_TYPE_PARTIAL_LINE) {
4601 iter->seq.full = 0;
4602 trace_seq_puts(&iter->seq, "[LINE TOO BIG]\n");
4603 }
a63ce5b3
SR
4604 ret = trace_print_seq(m, &iter->seq);
4605 /*
4606 * If we overflow the seq_file buffer, then it will
4607 * ask us for this data again at start up.
4608 * Use that instead.
4609 * ret is 0 if seq_file write succeeded.
4610 * -1 otherwise.
4611 */
4612 iter->leftover = ret;
bc0c38d1
SR
4613 }
4614
4615 return 0;
4616}
4617
649e9c70
ON
4618/*
4619 * Should be used after trace_array_get(), trace_types_lock
4620 * ensures that i_cdev was already initialized.
4621 */
4622static inline int tracing_get_cpu(struct inode *inode)
4623{
4624 if (inode->i_cdev) /* See trace_create_cpu_file() */
4625 return (long)inode->i_cdev - 1;
4626 return RING_BUFFER_ALL_CPUS;
4627}
4628
88e9d34c 4629static const struct seq_operations tracer_seq_ops = {
4bf39a94
IM
4630 .start = s_start,
4631 .next = s_next,
4632 .stop = s_stop,
4633 .show = s_show,
bc0c38d1
SR
4634};
4635
6bba9288
SRG
4636/*
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.
4642 */
4643static void free_trace_iter_content(struct trace_iterator *iter)
4644{
4645 /* The fmt is either NULL, allocated or points to static_fmt_buf */
4646 if (iter->fmt != static_fmt_buf)
4647 kfree(iter->fmt);
4648
4649 kfree(iter->temp);
4650 kfree(iter->buffer_iter);
4651 mutex_destroy(&iter->mutex);
4652 free_cpumask_var(iter->started);
4653}
4654
e309b41d 4655static struct trace_iterator *
6484c71c 4656__tracing_open(struct inode *inode, struct file *file, bool snapshot)
bc0c38d1 4657{
6484c71c 4658 struct trace_array *tr = inode->i_private;
bc0c38d1 4659 struct trace_iterator *iter;
50e18b94 4660 int cpu;
bc0c38d1 4661
85a2f9b4
SR
4662 if (tracing_disabled)
4663 return ERR_PTR(-ENODEV);
60a11774 4664
50e18b94 4665 iter = __seq_open_private(file, &tracer_seq_ops, sizeof(*iter));
85a2f9b4
SR
4666 if (!iter)
4667 return ERR_PTR(-ENOMEM);
bc0c38d1 4668
72917235 4669 iter->buffer_iter = kcalloc(nr_cpu_ids, sizeof(*iter->buffer_iter),
6d158a81 4670 GFP_KERNEL);
93574fcc
DC
4671 if (!iter->buffer_iter)
4672 goto release;
4673
ff895103
SRV
4674 /*
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.
4681 */
4682 iter->temp = kmalloc(128, GFP_KERNEL);
4683 if (iter->temp)
4684 iter->temp_size = 128;
4685
efbbdaa2
MH
4686 /*
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.
4692 */
4693 iter->fmt = NULL;
4694 iter->fmt_size = 0;
4695
bc0c38d1 4696 mutex_lock(&trace_types_lock);
9182b519 4697 iter->trace = tr->current_trace;
d7350c3f 4698
79f55997 4699 if (!zalloc_cpumask_var(&iter->started, GFP_KERNEL))
b0dfa978
FW
4700 goto fail;
4701
12883efb
SRRH
4702 iter->tr = tr;
4703
4704#ifdef CONFIG_TRACER_MAX_TRACE
2b6080f2
SR
4705 /* Currently only the top directory has a snapshot */
4706 if (tr->current_trace->print_max || snapshot)
1c5eb448 4707 iter->array_buffer = &tr->max_buffer;
bc0c38d1 4708 else
12883efb 4709#endif
1c5eb448 4710 iter->array_buffer = &tr->array_buffer;
debdd57f 4711 iter->snapshot = snapshot;
bc0c38d1 4712 iter->pos = -1;
6484c71c 4713 iter->cpu_file = tracing_get_cpu(inode);
d7350c3f 4714 mutex_init(&iter->mutex);
bc0c38d1 4715
8bba1bf5 4716 /* Notify the tracer early; before we stop tracing. */
b3f7a6cd 4717 if (iter->trace->open)
a93751ca 4718 iter->trace->open(iter);
8bba1bf5 4719
12ef7d44 4720 /* Annotate start of buffers if we had overruns */
1c5eb448 4721 if (ring_buffer_overruns(iter->array_buffer->buffer))
12ef7d44
SR
4722 iter->iter_flags |= TRACE_FILE_ANNOTATE;
4723
8be0709f 4724 /* Output in nanoseconds only if we are using a clock in nanoseconds. */
58e8eedf 4725 if (trace_clocks[tr->clock_id].in_ns)
8be0709f
DS
4726 iter->iter_flags |= TRACE_FILE_TIME_IN_NS;
4727
06e0a548
SRV
4728 /*
4729 * If pause-on-trace is enabled, then stop the trace while
4730 * dumping, unless this is the "snapshot" file
4731 */
4732 if (!iter->snapshot && (tr->trace_flags & TRACE_ITER_PAUSE_ON_TRACE))
2b6080f2 4733 tracing_stop_tr(tr);
2f26ebd5 4734
ae3b5093 4735 if (iter->cpu_file == RING_BUFFER_ALL_CPUS) {
b04cc6b1 4736 for_each_tracing_cpu(cpu) {
b04cc6b1 4737 iter->buffer_iter[cpu] =
1c5eb448 4738 ring_buffer_read_prepare(iter->array_buffer->buffer,
31b265b3 4739 cpu, GFP_KERNEL);
72c9ddfd
DM
4740 }
4741 ring_buffer_read_prepare_sync();
4742 for_each_tracing_cpu(cpu) {
4743 ring_buffer_read_start(iter->buffer_iter[cpu]);
2f26ebd5 4744 tracing_iter_reset(iter, cpu);
b04cc6b1
FW
4745 }
4746 } else {
4747 cpu = iter->cpu_file;
3928a8a2 4748 iter->buffer_iter[cpu] =
1c5eb448 4749 ring_buffer_read_prepare(iter->array_buffer->buffer,
31b265b3 4750 cpu, GFP_KERNEL);
72c9ddfd
DM
4751 ring_buffer_read_prepare_sync();
4752 ring_buffer_read_start(iter->buffer_iter[cpu]);
2f26ebd5 4753 tracing_iter_reset(iter, cpu);
3928a8a2
SR
4754 }
4755
bc0c38d1
SR
4756 mutex_unlock(&trace_types_lock);
4757
bc0c38d1 4758 return iter;
3928a8a2 4759
d7350c3f 4760 fail:
3928a8a2 4761 mutex_unlock(&trace_types_lock);
6bba9288 4762 free_trace_iter_content(iter);
93574fcc 4763release:
50e18b94
JO
4764 seq_release_private(inode, file);
4765 return ERR_PTR(-ENOMEM);
bc0c38d1
SR
4766}
4767
4768int tracing_open_generic(struct inode *inode, struct file *filp)
4769{
8530dec6
SRV
4770 int ret;
4771
4772 ret = tracing_check_open_get_tr(NULL);
4773 if (ret)
4774 return ret;
60a11774 4775
bc0c38d1
SR
4776 filp->private_data = inode->i_private;
4777 return 0;
4778}
4779
2e86421d
GB
4780bool tracing_is_disabled(void)
4781{
4782 return (tracing_disabled) ? true: false;
4783}
4784
7b85af63
SRRH
4785/*
4786 * Open and update trace_array ref count.
4787 * Must have the current trace_array passed to it.
4788 */
aa07d71f 4789int tracing_open_generic_tr(struct inode *inode, struct file *filp)
7b85af63
SRRH
4790{
4791 struct trace_array *tr = inode->i_private;
8530dec6 4792 int ret;
7b85af63 4793
8530dec6
SRV
4794 ret = tracing_check_open_get_tr(tr);
4795 if (ret)
4796 return ret;
7b85af63
SRRH
4797
4798 filp->private_data = inode->i_private;
4799
4800 return 0;
7b85af63
SRRH
4801}
4802
f5ca233e
SRG
4803/*
4804 * The private pointer of the inode is the trace_event_file.
4805 * Update the tr ref count associated to it.
4806 */
4807int tracing_open_file_tr(struct inode *inode, struct file *filp)
4808{
4809 struct trace_event_file *file = inode->i_private;
4810 int ret;
4811
4812 ret = tracing_check_open_get_tr(file->tr);
4813 if (ret)
4814 return ret;
4815
bb32500f
SRG
4816 mutex_lock(&event_mutex);
4817
4818 /* Fail if the file is marked for removal */
4819 if (file->flags & EVENT_FILE_FL_FREED) {
4820 trace_array_put(file->tr);
4821 ret = -ENODEV;
4822 } else {
4823 event_file_get(file);
4824 }
4825
4826 mutex_unlock(&event_mutex);
4827 if (ret)
4828 return ret;
4829
f5ca233e
SRG
4830 filp->private_data = inode->i_private;
4831
4832 return 0;
4833}
4834
4835int tracing_release_file_tr(struct inode *inode, struct file *filp)
4836{
4837 struct trace_event_file *file = inode->i_private;
4838
4839 trace_array_put(file->tr);
bb32500f 4840 event_file_put(file);
f5ca233e
SRG
4841
4842 return 0;
4843}
4844
1cc111b9
ZY
4845int tracing_single_release_file_tr(struct inode *inode, struct file *filp)
4846{
4847 tracing_release_file_tr(inode, filp);
4848 return single_release(inode, filp);
4849}
4850
2972e305
JK
4851static int tracing_mark_open(struct inode *inode, struct file *filp)
4852{
4853 stream_open(inode, filp);
4854 return tracing_open_generic_tr(inode, filp);
4855}
4856
4fd27358 4857static int tracing_release(struct inode *inode, struct file *file)
bc0c38d1 4858{
6484c71c 4859 struct trace_array *tr = inode->i_private;
907f2784 4860 struct seq_file *m = file->private_data;
4acd4d00 4861 struct trace_iterator *iter;
3928a8a2 4862 int cpu;
bc0c38d1 4863
ff451961 4864 if (!(file->f_mode & FMODE_READ)) {
6484c71c 4865 trace_array_put(tr);
4acd4d00 4866 return 0;
ff451961 4867 }
4acd4d00 4868
6484c71c 4869 /* Writes do not use seq_file */
4acd4d00 4870 iter = m->private;
bc0c38d1 4871 mutex_lock(&trace_types_lock);
a695cb58 4872
3928a8a2
SR
4873 for_each_tracing_cpu(cpu) {
4874 if (iter->buffer_iter[cpu])
4875 ring_buffer_read_finish(iter->buffer_iter[cpu]);
4876 }
4877
bc0c38d1
SR
4878 if (iter->trace && iter->trace->close)
4879 iter->trace->close(iter);
4880
06e0a548 4881 if (!iter->snapshot && tr->stop_count)
debdd57f 4882 /* reenable tracing if it was previously enabled */
2b6080f2 4883 tracing_start_tr(tr);
f77d09a3
AL
4884
4885 __trace_array_put(tr);
4886
bc0c38d1
SR
4887 mutex_unlock(&trace_types_lock);
4888
6bba9288 4889 free_trace_iter_content(iter);
50e18b94 4890 seq_release_private(inode, file);
ff451961 4891
bc0c38d1
SR
4892 return 0;
4893}
4894
139f8400 4895int tracing_release_generic_tr(struct inode *inode, struct file *file)
7b85af63
SRRH
4896{
4897 struct trace_array *tr = inode->i_private;
4898
4899 trace_array_put(tr);
bc0c38d1
SR
4900 return 0;
4901}
4902
7b85af63
SRRH
4903static int tracing_single_release_tr(struct inode *inode, struct file *file)
4904{
4905 struct trace_array *tr = inode->i_private;
4906
4907 trace_array_put(tr);
4908
4909 return single_release(inode, file);
4910}
4911
bc0c38d1
SR
4912static int tracing_open(struct inode *inode, struct file *file)
4913{
6484c71c 4914 struct trace_array *tr = inode->i_private;
85a2f9b4 4915 struct trace_iterator *iter;
8530dec6 4916 int ret;
bc0c38d1 4917
8530dec6
SRV
4918 ret = tracing_check_open_get_tr(tr);
4919 if (ret)
4920 return ret;
ff451961 4921
4acd4d00 4922 /* If this file was open for write, then erase contents */
6484c71c
ON
4923 if ((file->f_mode & FMODE_WRITE) && (file->f_flags & O_TRUNC)) {
4924 int cpu = tracing_get_cpu(inode);
1c5eb448 4925 struct array_buffer *trace_buf = &tr->array_buffer;
8dd33bcb
BY
4926
4927#ifdef CONFIG_TRACER_MAX_TRACE
4928 if (tr->current_trace->print_max)
4929 trace_buf = &tr->max_buffer;
4930#endif
6484c71c
ON
4931
4932 if (cpu == RING_BUFFER_ALL_CPUS)
8dd33bcb 4933 tracing_reset_online_cpus(trace_buf);
4acd4d00 4934 else
a47b53e9 4935 tracing_reset_cpu(trace_buf, cpu);
4acd4d00 4936 }
bc0c38d1 4937
4acd4d00 4938 if (file->f_mode & FMODE_READ) {
6484c71c 4939 iter = __tracing_open(inode, file, false);
4acd4d00
SR
4940 if (IS_ERR(iter))
4941 ret = PTR_ERR(iter);
983f938a 4942 else if (tr->trace_flags & TRACE_ITER_LATENCY_FMT)
4acd4d00
SR
4943 iter->iter_flags |= TRACE_FILE_LAT_FMT;
4944 }
ff451961
SRRH
4945
4946 if (ret < 0)
4947 trace_array_put(tr);
4948
bc0c38d1
SR
4949 return ret;
4950}
4951
607e2ea1
SRRH
4952/*
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.
4956 */
4957static bool
4958trace_ok_for_array(struct tracer *t, struct trace_array *tr)
4959{
2124de79
SRG
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)
4963 return false;
4964#endif
607e2ea1
SRRH
4965 return (tr->flags & TRACE_ARRAY_FL_GLOBAL) || t->allow_instances;
4966}
4967
4968/* Find the next tracer that this trace array may use */
4969static struct tracer *
4970get_tracer_for_array(struct trace_array *tr, struct tracer *t)
4971{
4972 while (t && !trace_ok_for_array(t, tr))
4973 t = t->next;
4974
4975 return t;
4976}
4977
e309b41d 4978static void *
bc0c38d1
SR
4979t_next(struct seq_file *m, void *v, loff_t *pos)
4980{
607e2ea1 4981 struct trace_array *tr = m->private;
f129e965 4982 struct tracer *t = v;
bc0c38d1
SR
4983
4984 (*pos)++;
4985
4986 if (t)
607e2ea1 4987 t = get_tracer_for_array(tr, t->next);
bc0c38d1 4988
bc0c38d1
SR
4989 return t;
4990}
4991
4992static void *t_start(struct seq_file *m, loff_t *pos)
4993{
607e2ea1 4994 struct trace_array *tr = m->private;
f129e965 4995 struct tracer *t;
bc0c38d1
SR
4996 loff_t l = 0;
4997
4998 mutex_lock(&trace_types_lock);
607e2ea1
SRRH
4999
5000 t = get_tracer_for_array(tr, trace_types);
5001 for (; t && l < *pos; t = t_next(m, t, &l))
5002 ;
bc0c38d1
SR
5003
5004 return t;
5005}
5006
5007static void t_stop(struct seq_file *m, void *p)
5008{
5009 mutex_unlock(&trace_types_lock);
5010}
5011
5012static int t_show(struct seq_file *m, void *v)
5013{
5014 struct tracer *t = v;
5015
5016 if (!t)
5017 return 0;
5018
fa6f0cc7 5019 seq_puts(m, t->name);
bc0c38d1
SR
5020 if (t->next)
5021 seq_putc(m, ' ');
5022 else
5023 seq_putc(m, '\n');
5024
5025 return 0;
5026}
5027
88e9d34c 5028static const struct seq_operations show_traces_seq_ops = {
4bf39a94
IM
5029 .start = t_start,
5030 .next = t_next,
5031 .stop = t_stop,
5032 .show = t_show,
bc0c38d1
SR
5033};
5034
5035static int show_traces_open(struct inode *inode, struct file *file)
5036{
607e2ea1
SRRH
5037 struct trace_array *tr = inode->i_private;
5038 struct seq_file *m;
5039 int ret;
5040
8530dec6
SRV
5041 ret = tracing_check_open_get_tr(tr);
5042 if (ret)
5043 return ret;
194c2c74 5044
607e2ea1 5045 ret = seq_open(file, &show_traces_seq_ops);
194c2c74
SRV
5046 if (ret) {
5047 trace_array_put(tr);
607e2ea1 5048 return ret;
194c2c74 5049 }
607e2ea1
SRRH
5050
5051 m = file->private_data;
5052 m->private = tr;
5053
5054 return 0;
bc0c38d1
SR
5055}
5056
950032ff 5057static int tracing_seq_release(struct inode *inode, struct file *file)
194c2c74
SRV
5058{
5059 struct trace_array *tr = inode->i_private;
5060
5061 trace_array_put(tr);
5062 return seq_release(inode, file);
5063}
5064
4acd4d00
SR
5065static ssize_t
5066tracing_write_stub(struct file *filp, const char __user *ubuf,
5067 size_t count, loff_t *ppos)
5068{
5069 return count;
5070}
5071
098c879e 5072loff_t tracing_lseek(struct file *file, loff_t offset, int whence)
364829b1 5073{
098c879e
SRRH
5074 int ret;
5075
364829b1 5076 if (file->f_mode & FMODE_READ)
098c879e 5077 ret = seq_lseek(file, offset, whence);
364829b1 5078 else
098c879e
SRRH
5079 file->f_pos = ret = 0;
5080
5081 return ret;
364829b1
SP
5082}
5083
5e2336a0 5084static const struct file_operations tracing_fops = {
4bf39a94
IM
5085 .open = tracing_open,
5086 .read = seq_read,
e400be67 5087 .read_iter = seq_read_iter,
5bd4990f 5088 .splice_read = copy_splice_read,
4acd4d00 5089 .write = tracing_write_stub,
098c879e 5090 .llseek = tracing_lseek,
4bf39a94 5091 .release = tracing_release,
bc0c38d1
SR
5092};
5093
5e2336a0 5094static const struct file_operations show_traces_fops = {
c7078de1
IM
5095 .open = show_traces_open,
5096 .read = seq_read,
b444786f 5097 .llseek = seq_lseek,
950032ff 5098 .release = tracing_seq_release,
c7078de1
IM
5099};
5100
5101static ssize_t
5102tracing_cpumask_read(struct file *filp, char __user *ubuf,
5103 size_t count, loff_t *ppos)
5104{
ccfe9e42 5105 struct trace_array *tr = file_inode(filp)->i_private;
90e406f9 5106 char *mask_str;
36dfe925 5107 int len;
c7078de1 5108
90e406f9
CD
5109 len = snprintf(NULL, 0, "%*pb\n",
5110 cpumask_pr_args(tr->tracing_cpumask)) + 1;
5111 mask_str = kmalloc(len, GFP_KERNEL);
5112 if (!mask_str)
5113 return -ENOMEM;
36dfe925 5114
90e406f9 5115 len = snprintf(mask_str, len, "%*pb\n",
1a40243b
TH
5116 cpumask_pr_args(tr->tracing_cpumask));
5117 if (len >= count) {
36dfe925
IM
5118 count = -EINVAL;
5119 goto out_err;
5120 }
90e406f9 5121 count = simple_read_from_buffer(ubuf, count, ppos, mask_str, len);
36dfe925
IM
5122
5123out_err:
90e406f9 5124 kfree(mask_str);
c7078de1
IM
5125
5126 return count;
5127}
5128
9d15dbbd
MH
5129int tracing_set_cpumask(struct trace_array *tr,
5130 cpumask_var_t tracing_cpumask_new)
c7078de1 5131{
9d15dbbd 5132 int cpu;
c7078de1 5133
9d15dbbd
MH
5134 if (!tr)
5135 return -EINVAL;
36dfe925 5136
a5e25883 5137 local_irq_disable();
0b9b12c1 5138 arch_spin_lock(&tr->max_lock);
ab46428c 5139 for_each_tracing_cpu(cpu) {
36dfe925
IM
5140 /*
5141 * Increase/decrease the disabled counter if we are
5142 * about to flip a bit in the cpumask:
5143 */
ccfe9e42 5144 if (cpumask_test_cpu(cpu, tr->tracing_cpumask) &&
9e01c1b7 5145 !cpumask_test_cpu(cpu, tracing_cpumask_new)) {
1c5eb448 5146 ring_buffer_record_disable_cpu(tr->array_buffer.buffer, cpu);
b71645d6
ZY
5147#ifdef CONFIG_TRACER_MAX_TRACE
5148 ring_buffer_record_disable_cpu(tr->max_buffer.buffer, cpu);
5149#endif
36dfe925 5150 }
ccfe9e42 5151 if (!cpumask_test_cpu(cpu, tr->tracing_cpumask) &&
9e01c1b7 5152 cpumask_test_cpu(cpu, tracing_cpumask_new)) {
1c5eb448 5153 ring_buffer_record_enable_cpu(tr->array_buffer.buffer, cpu);
b71645d6
ZY
5154#ifdef CONFIG_TRACER_MAX_TRACE
5155 ring_buffer_record_enable_cpu(tr->max_buffer.buffer, cpu);
5156#endif
36dfe925
IM
5157 }
5158 }
0b9b12c1 5159 arch_spin_unlock(&tr->max_lock);
a5e25883 5160 local_irq_enable();
36dfe925 5161
ccfe9e42 5162 cpumask_copy(tr->tracing_cpumask, tracing_cpumask_new);
9d15dbbd
MH
5163
5164 return 0;
5165}
5166
5167static ssize_t
5168tracing_cpumask_write(struct file *filp, const char __user *ubuf,
5169 size_t count, loff_t *ppos)
5170{
5171 struct trace_array *tr = file_inode(filp)->i_private;
5172 cpumask_var_t tracing_cpumask_new;
5173 int err;
5174
98feccbf
LX
5175 if (count == 0 || count > KMALLOC_MAX_SIZE)
5176 return -EINVAL;
5177
c5e3a411 5178 if (!zalloc_cpumask_var(&tracing_cpumask_new, GFP_KERNEL))
9d15dbbd
MH
5179 return -ENOMEM;
5180
5181 err = cpumask_parse_user(ubuf, count, tracing_cpumask_new);
5182 if (err)
5183 goto err_free;
5184
5185 err = tracing_set_cpumask(tr, tracing_cpumask_new);
5186 if (err)
5187 goto err_free;
5188
9e01c1b7 5189 free_cpumask_var(tracing_cpumask_new);
c7078de1
IM
5190
5191 return count;
36dfe925 5192
9d15dbbd 5193err_free:
215368e8 5194 free_cpumask_var(tracing_cpumask_new);
36dfe925
IM
5195
5196 return err;
c7078de1
IM
5197}
5198
5e2336a0 5199static const struct file_operations tracing_cpumask_fops = {
ccfe9e42 5200 .open = tracing_open_generic_tr,
c7078de1
IM
5201 .read = tracing_cpumask_read,
5202 .write = tracing_cpumask_write,
ccfe9e42 5203 .release = tracing_release_generic_tr,
b444786f 5204 .llseek = generic_file_llseek,
bc0c38d1
SR
5205};
5206
fdb372ed 5207static int tracing_trace_options_show(struct seq_file *m, void *v)
bc0c38d1 5208{
d8e83d26 5209 struct tracer_opt *trace_opts;
2b6080f2 5210 struct trace_array *tr = m->private;
d8e83d26 5211 u32 tracer_flags;
d8e83d26 5212 int i;
adf9f195 5213
d33b10c0
SR
5214 guard(mutex)(&trace_types_lock);
5215
2b6080f2
SR
5216 tracer_flags = tr->current_trace->flags->val;
5217 trace_opts = tr->current_trace->flags->opts;
d8e83d26 5218
bc0c38d1 5219 for (i = 0; trace_options[i]; i++) {
983f938a 5220 if (tr->trace_flags & (1 << i))
fdb372ed 5221 seq_printf(m, "%s\n", trace_options[i]);
bc0c38d1 5222 else
fdb372ed 5223 seq_printf(m, "no%s\n", trace_options[i]);
bc0c38d1
SR
5224 }
5225
adf9f195
FW
5226 for (i = 0; trace_opts[i].name; i++) {
5227 if (tracer_flags & trace_opts[i].bit)
fdb372ed 5228 seq_printf(m, "%s\n", trace_opts[i].name);
adf9f195 5229 else
fdb372ed 5230 seq_printf(m, "no%s\n", trace_opts[i].name);
adf9f195
FW
5231 }
5232
fdb372ed 5233 return 0;
bc0c38d1 5234}
bc0c38d1 5235
8c1a49ae 5236static int __set_tracer_option(struct trace_array *tr,
8d18eaaf
LZ
5237 struct tracer_flags *tracer_flags,
5238 struct tracer_opt *opts, int neg)
5239{
d39cdd20 5240 struct tracer *trace = tracer_flags->trace;
8d18eaaf 5241 int ret;
bc0c38d1 5242
8c1a49ae 5243 ret = trace->set_flag(tr, tracer_flags->val, opts->bit, !neg);
8d18eaaf
LZ
5244 if (ret)
5245 return ret;
5246
5247 if (neg)
5248 tracer_flags->val &= ~opts->bit;
5249 else
5250 tracer_flags->val |= opts->bit;
5251 return 0;
bc0c38d1
SR
5252}
5253
adf9f195 5254/* Try to assign a tracer specific option */
8c1a49ae 5255static int set_tracer_option(struct trace_array *tr, char *cmp, int neg)
adf9f195 5256{
8c1a49ae 5257 struct tracer *trace = tr->current_trace;
7770841e 5258 struct tracer_flags *tracer_flags = trace->flags;
adf9f195 5259 struct tracer_opt *opts = NULL;
8d18eaaf 5260 int i;
adf9f195 5261
7770841e
Z
5262 for (i = 0; tracer_flags->opts[i].name; i++) {
5263 opts = &tracer_flags->opts[i];
adf9f195 5264
8d18eaaf 5265 if (strcmp(cmp, opts->name) == 0)
8c1a49ae 5266 return __set_tracer_option(tr, trace->flags, opts, neg);
adf9f195 5267 }
adf9f195 5268
8d18eaaf 5269 return -EINVAL;
adf9f195
FW
5270}
5271
613f04a0
SRRH
5272/* Some tracers require overwrite to stay enabled */
5273int trace_keep_overwrite(struct tracer *tracer, u32 mask, int set)
5274{
5275 if (tracer->enabled && (mask & TRACE_ITER_OVERWRITE) && !set)
5276 return -1;
5277
5278 return 0;
5279}
5280
e85d471c
SRG
5281int set_tracer_flag(struct trace_array *tr, unsigned int mask, int enabled)
5282{
3a53acf1 5283 if ((mask == TRACE_ITER_RECORD_TGID) ||
ef2bd81d 5284 (mask == TRACE_ITER_RECORD_CMD) ||
7b382efd
SR
5285 (mask == TRACE_ITER_TRACE_PRINTK) ||
5286 (mask == TRACE_ITER_COPY_MARKER))
3a53acf1
PS
5287 lockdep_assert_held(&event_mutex);
5288
af4617bd 5289 /* do nothing if flag is already set */
983f938a 5290 if (!!(tr->trace_flags & mask) == !!enabled)
613f04a0
SRRH
5291 return 0;
5292
5293 /* Give the tracer a chance to approve the change */
2b6080f2 5294 if (tr->current_trace->flag_changed)
bf6065b5 5295 if (tr->current_trace->flag_changed(tr, mask, !!enabled))
613f04a0 5296 return -EINVAL;
af4617bd 5297
ef2bd81d
SR
5298 if (mask == TRACE_ITER_TRACE_PRINTK) {
5299 if (enabled) {
5300 update_printk_trace(tr);
5301 } else {
5302 /*
5303 * The global_trace cannot clear this.
5304 * It's flag only gets cleared if another instance sets it.
5305 */
5306 if (printk_trace == &global_trace)
5307 return -EINVAL;
5308 /*
5309 * An instance must always have it set.
5310 * by default, that's the global_trace instane.
5311 */
5312 if (printk_trace == tr)
5313 update_printk_trace(&global_trace);
5314 }
5315 }
5316
7b382efd
SR
5317 if (mask == TRACE_ITER_COPY_MARKER)
5318 update_marker_trace(tr, enabled);
5319
af4617bd 5320 if (enabled)
983f938a 5321 tr->trace_flags |= mask;
af4617bd 5322 else
983f938a 5323 tr->trace_flags &= ~mask;
e870e9a1
LZ
5324
5325 if (mask == TRACE_ITER_RECORD_CMD)
5326 trace_event_enable_cmd_record(enabled);
750912fa 5327
d914ba37 5328 if (mask == TRACE_ITER_RECORD_TGID) {
2cc621fd 5329
e85d471c 5330 if (trace_alloc_tgid_map() < 0) {
d914ba37
JF
5331 tr->trace_flags &= ~TRACE_ITER_RECORD_TGID;
5332 return -ENOMEM;
5333 }
5334
5335 trace_event_enable_tgid_record(enabled);
5336 }
5337
c37775d5
SR
5338 if (mask == TRACE_ITER_EVENT_FORK)
5339 trace_event_follow_fork(tr, enabled);
5340
1e10486f
NK
5341 if (mask == TRACE_ITER_FUNC_FORK)
5342 ftrace_pid_follow_fork(tr, enabled);
5343
80902822 5344 if (mask == TRACE_ITER_OVERWRITE) {
1c5eb448 5345 ring_buffer_change_overwrite(tr->array_buffer.buffer, enabled);
80902822 5346#ifdef CONFIG_TRACER_MAX_TRACE
12883efb 5347 ring_buffer_change_overwrite(tr->max_buffer.buffer, enabled);
80902822
SRRH
5348#endif
5349 }
81698831 5350
b9f9108c 5351 if (mask == TRACE_ITER_PRINTK) {
81698831 5352 trace_printk_start_stop_comm(enabled);
b9f9108c
SRRH
5353 trace_printk_control(enabled);
5354 }
613f04a0
SRRH
5355
5356 return 0;
af4617bd
SR
5357}
5358
9c5b9d3d 5359int trace_set_options(struct trace_array *tr, char *option)
bc0c38d1 5360{
8d18eaaf 5361 char *cmp;
bc0c38d1 5362 int neg = 0;
591a033d 5363 int ret;
a4d1e688 5364 size_t orig_len = strlen(option);
3d739c1f 5365 int len;
bc0c38d1 5366
7bcfaf54 5367 cmp = strstrip(option);
bc0c38d1 5368
3d739c1f
SRV
5369 len = str_has_prefix(cmp, "no");
5370 if (len)
bc0c38d1 5371 neg = 1;
3d739c1f
SRV
5372
5373 cmp += len;
bc0c38d1 5374
3a53acf1 5375 mutex_lock(&event_mutex);
69d34da2
SRRH
5376 mutex_lock(&trace_types_lock);
5377
591a033d 5378 ret = match_string(trace_options, -1, cmp);
adf9f195 5379 /* If no option could be set, test the specific tracer options */
591a033d 5380 if (ret < 0)
8c1a49ae 5381 ret = set_tracer_option(tr, cmp, neg);
591a033d
YX
5382 else
5383 ret = set_tracer_flag(tr, 1 << ret, !neg);
69d34da2
SRRH
5384
5385 mutex_unlock(&trace_types_lock);
3a53acf1 5386 mutex_unlock(&event_mutex);
bc0c38d1 5387
a4d1e688
JW
5388 /*
5389 * If the first trailing whitespace is replaced with '\0' by strstrip,
5390 * turn it back into a space.
5391 */
5392 if (orig_len > strlen(option))
5393 option[strlen(option)] = ' ';
5394
7bcfaf54
SR
5395 return ret;
5396}
5397
a4d1e688
JW
5398static void __init apply_trace_boot_options(void)
5399{
5400 char *buf = trace_boot_options_buf;
5401 char *option;
5402
5403 while (true) {
5404 option = strsep(&buf, ",");
5405
5406 if (!option)
5407 break;
a4d1e688 5408
43ed3843
SRRH
5409 if (*option)
5410 trace_set_options(&global_trace, option);
a4d1e688
JW
5411
5412 /* Put back the comma to allow this to be called again */
5413 if (buf)
5414 *(buf - 1) = ',';
5415 }
5416}
5417
7bcfaf54
SR
5418static ssize_t
5419tracing_trace_options_write(struct file *filp, const char __user *ubuf,
5420 size_t cnt, loff_t *ppos)
5421{
2b6080f2
SR
5422 struct seq_file *m = filp->private_data;
5423 struct trace_array *tr = m->private;
7bcfaf54 5424 char buf[64];
613f04a0 5425 int ret;
7bcfaf54
SR
5426
5427 if (cnt >= sizeof(buf))
5428 return -EINVAL;
5429
4afe6495 5430 if (copy_from_user(buf, ubuf, cnt))
7bcfaf54
SR
5431 return -EFAULT;
5432
a8dd2176
SR
5433 buf[cnt] = 0;
5434
2b6080f2 5435 ret = trace_set_options(tr, buf);
613f04a0
SRRH
5436 if (ret < 0)
5437 return ret;
7bcfaf54 5438
cf8517cf 5439 *ppos += cnt;
bc0c38d1
SR
5440
5441 return cnt;
5442}
5443
fdb372ed
LZ
5444static int tracing_trace_options_open(struct inode *inode, struct file *file)
5445{
7b85af63 5446 struct trace_array *tr = inode->i_private;
f77d09a3 5447 int ret;
7b85af63 5448
8530dec6
SRV
5449 ret = tracing_check_open_get_tr(tr);
5450 if (ret)
5451 return ret;
7b85af63 5452
f77d09a3
AL
5453 ret = single_open(file, tracing_trace_options_show, inode->i_private);
5454 if (ret < 0)
5455 trace_array_put(tr);
5456
5457 return ret;
fdb372ed
LZ
5458}
5459
5e2336a0 5460static const struct file_operations tracing_iter_fops = {
fdb372ed
LZ
5461 .open = tracing_trace_options_open,
5462 .read = seq_read,
5463 .llseek = seq_lseek,
7b85af63 5464 .release = tracing_single_release_tr,
ee6bce52 5465 .write = tracing_trace_options_write,
bc0c38d1
SR
5466};
5467
7bd2f24c
IM
5468static const char readme_msg[] =
5469 "tracing mini-HOWTO:\n\n"
fa17cb4b
KS
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"
22f45649
SRRH
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"
a8d65579 5482 " error_log\t- error log for failed commands (that support it)\n"
22f45649
SRRH
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"
cf2adec7 5485 " trace_clock\t\t- change the clock used to order events\n"
22f45649
SRRH
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"
5493#endif
cf2adec7 5494 "\n timestamp_mode\t- view the mode used to timestamp events\n"
2c1ea60b
TZ
5495 " delta: Delta difference against a buffer-wide timestamp\n"
5496 " absolute: Absolute (standalone) timestamp\n"
22f45649 5497 "\n trace_marker\t\t- Writes into this file writes into the kernel buffer\n"
fa32e855 5498 "\n trace_marker_raw\t\t- Writes into this file writes binary data into the kernel buffer\n"
22f45649
SRRH
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"
b9416997 5503 "\t\t\t Disable an option by prefixing 'no' to the\n"
71485c45 5504 "\t\t\t option name\n"
939c7a4f 5505 " saved_cmdlines_size\t- echo command number in here to store comm-pid list\n"
22f45649
SRRH
5506#ifdef CONFIG_DYNAMIC_FTRACE
5507 "\n available_filter_functions - list of functions that can be filtered on\n"
71485c45
SRRH
5508 " set_ftrace_filter\t- echo function name in here to only trace these\n"
5509 "\t\t\t functions\n"
60f1d5e3 5510 "\t accepts: func_full_name or glob-matching-pattern\n"
71485c45
SRRH
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"
22f45649 5519#ifdef CONFIG_STACKTRACE
71485c45 5520 "\t\t stacktrace\n"
22f45649
SRRH
5521#endif
5522#ifdef CONFIG_TRACER_SNAPSHOT
71485c45 5523 "\t\t snapshot\n"
22f45649 5524#endif
17a280ea
SRRH
5525 "\t\t dump\n"
5526 "\t\t cpudump\n"
71485c45
SRRH
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"
22f45649 5539 " set_ftrace_notrace\t- echo function name in here to never trace.\n"
71485c45
SRRH
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"
22f45649
SRRH
5543#endif /* CONFIG_DYNAMIC_FTRACE */
5544#ifdef CONFIG_FUNCTION_TRACER
71485c45
SRRH
5545 " set_ftrace_pid\t- Write pid(s) to only function trace those pids\n"
5546 "\t\t (function)\n"
b3b1e6ed
SRV
5547 " set_ftrace_notrace_pid\t- Write pid(s) to not function trace those pids\n"
5548 "\t\t (function)\n"
22f45649
SRRH
5549#endif
5550#ifdef CONFIG_FUNCTION_GRAPH_TRACER
5551 " set_graph_function\t- Trace the nested calls of a function (function_graph)\n"
d048a8c7 5552 " set_graph_notrace\t- Do not trace the nested calls of a function (function_graph)\n"
22f45649
SRRH
5553 " max_graph_depth\t- Trace a limited depth of nested calls (0 is unlimited)\n"
5554#endif
5555#ifdef CONFIG_TRACER_SNAPSHOT
71485c45
SRRH
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"
22f45649 5559#endif
991821c8 5560#ifdef CONFIG_STACK_TRACER
22f45649
SRRH
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"
71485c45
SRRH
5563 "\t\t\t Write into this file to reset the max size (trigger a\n"
5564 "\t\t\t new trace)\n"
22f45649 5565#ifdef CONFIG_DYNAMIC_FTRACE
71485c45
SRRH
5566 " stack_trace_filter\t- Like set_ftrace_filter but limits what stack_trace\n"
5567 "\t\t\t traces\n"
22f45649 5568#endif
991821c8 5569#endif /* CONFIG_STACK_TRACER */
5448d44c 5570#ifdef CONFIG_DYNAMIC_EVENTS
ca89bc07 5571 " dynamic_events\t\t- Create/append/remove/show the generic dynamic events\n"
5448d44c
MH
5572 "\t\t\t Write into this file to define/undefine new trace events.\n"
5573#endif
6b0b7551 5574#ifdef CONFIG_KPROBE_EVENTS
ca89bc07 5575 " kprobe_events\t\t- Create/append/remove/show the kernel dynamic events\n"
86425625
MH
5576 "\t\t\t Write into this file to define/undefine new trace events.\n"
5577#endif
6b0b7551 5578#ifdef CONFIG_UPROBE_EVENTS
41af3cf5 5579 " uprobe_events\t\t- Create/append/remove/show the userspace dynamic events\n"
86425625
MH
5580 "\t\t\t Write into this file to define/undefine new trace events.\n"
5581#endif
334e5519
MHG
5582#if defined(CONFIG_KPROBE_EVENTS) || defined(CONFIG_UPROBE_EVENTS) || \
5583 defined(CONFIG_FPROBE_EVENTS)
86425625 5584 "\t accepts: event-definitions (one definition per line)\n"
334e5519 5585#if defined(CONFIG_KPROBE_EVENTS) || defined(CONFIG_UPROBE_EVENTS)
95c104c3
LY
5586 "\t Format: p[:[<group>/][<event>]] <place> [<args>]\n"
5587 "\t r[maxactive][:[<group>/][<event>]] <place> [<args>]\n"
334e5519
MHG
5588#endif
5589#ifdef CONFIG_FPROBE_EVENTS
5590 "\t f[:[<group>/][<event>]] <func-name>[%return] [<args>]\n"
e2d0d7b2 5591 "\t t[:[<group>/][<event>]] <tracepoint> [<args>]\n"
334e5519 5592#endif
7bbab38d
MH
5593#ifdef CONFIG_HIST_TRIGGERS
5594 "\t s:[synthetic/]<event> <field> [<field>]\n"
5595#endif
13392153 5596 "\t e[:[<group>/][<event>]] <attached-group>.<attached-event> [<args>] [if <filter>]\n"
95c104c3 5597 "\t -:[<group>/][<event>]\n"
6b0b7551 5598#ifdef CONFIG_KPROBE_EVENTS
86425625 5599 "\t place: [<module>:]<symbol>[+<offset>]|<memaddr>\n"
4725cd89 5600 "place (kretprobe): [<module>:]<symbol>[+<offset>]%return|<memaddr>\n"
86425625 5601#endif
6b0b7551 5602#ifdef CONFIG_UPROBE_EVENTS
3dd3aae3 5603 " place (uprobe): <path>:<offset>[%return][(ref_ctr_offset)]\n"
86425625
MH
5604#endif
5605 "\t args: <name>=fetcharg[:type]\n"
7491e2c4 5606 "\t fetcharg: (%<register>|$<efield>), @<address>, @<symbol>[+|-<offset>],\n"
a1303af5 5607#ifdef CONFIG_HAVE_FUNCTION_ARG_ACCESS_API
c440adfb 5608 "\t $stack<index>, $stack, $retval, $comm, $arg<N>,\n"
c18f9eab 5609#ifdef CONFIG_PROBE_EVENTS_BTF_ARGS
c440adfb 5610 "\t <argname>[->field[->field|.field...]],\n"
b576e097 5611#endif
a1303af5 5612#else
e65f7ae7 5613 "\t $stack<index>, $stack, $retval, $comm,\n"
a1303af5 5614#endif
a42e3c4d 5615 "\t +|-[u]<offset>(<fetcharg>), \\imm-value, \\\"imm-string\"\n"
25f00e40 5616 "\t kernel return probes support: $retval, $arg<N>, $comm\n"
8478cca1 5617 "\t type: s8/16/32/64, u8/16/32/64, x8/16/32/64, char, string, symbol,\n"
88903c46 5618 "\t b<bit-width>@<bit-offset>/<container-size>, ustring,\n"
20fe4d07 5619 "\t symstr, %pd/%pD, <type>\\[<array-size>\\]\n"
7bbab38d
MH
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"
5624#endif
7491e2c4
TSV
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"
86425625 5627#endif
4c86bc53
SR
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"
26f25564
TZ
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"
71485c45
SRRH
5633 " enable\t\t- Write 0/1 to enable/disable tracing of all <system>\n"
5634 "\t\t\t events\n"
26f25564 5635 " filter\t\t- If set, only events passing filter are traced\n"
71485c45
SRRH
5636 " events/<system>/<event>/\t- Directory containing control files for\n"
5637 "\t\t\t <event>:\n"
26f25564
TZ
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"
71485c45
SRRH
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"
d0bad49b
TZ
5645#ifdef CONFIG_HIST_TRIGGERS
5646 "\t enable_hist:<system>:<event>\n"
5647 "\t disable_hist:<system>:<event>\n"
5648#endif
26f25564 5649#ifdef CONFIG_STACKTRACE
71485c45 5650 "\t\t stacktrace\n"
26f25564
TZ
5651#endif
5652#ifdef CONFIG_TRACER_SNAPSHOT
71485c45 5653 "\t\t snapshot\n"
7ef224d1
TZ
5654#endif
5655#ifdef CONFIG_HIST_TRIGGERS
5656 "\t\t hist (see below)\n"
26f25564 5657#endif
71485c45
SRRH
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"
7ef224d1
TZ
5673#ifdef CONFIG_HIST_TRIGGERS
5674 " hist trigger\t- If set, event hits are aggregated into a hash table\n"
76a3b0c8 5675 "\t Format: hist:keys=<field1[,field2,...]>\n"
6a6e5ef2 5676 "\t [:<var1>=<field|var_ref|numeric_literal>[,<var2>=...]]\n"
f2606835 5677 "\t [:values=<field1[,field2,...]>]\n"
e62347d2 5678 "\t [:sort=<field1[,field2,...]>]\n"
7ef224d1 5679 "\t [:size=#entries]\n"
e86ae9ba 5680 "\t [:pause][:continue][:clear]\n"
5463bfda 5681 "\t [:name=histname1]\n"
ccf47f5c 5682 "\t [:nohitcount]\n"
c3e49506 5683 "\t [:<handler>.<action>]\n"
7ef224d1 5684 "\t [if <filter>]\n\n"
1e3bac71
SRV
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"
5688 "\n"
6a6e5ef2
KS
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"
5694 "\n"
f2b20c66 5695 "\t hist trigger arithmetic expressions support addition(+), subtraction(-),\n"
6a6e5ef2
KS
5696 "\t multiplication(*) and division(/) operators. An operand can be either a\n"
5697 "\t variable reference, field or numeric literal.\n"
5698 "\n"
7ef224d1 5699 "\t When a matching event is hit, an entry is added to a hash\n"
f2606835
TZ
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"
4b512860 5703 "\t can be any field, or the special string 'common_stacktrace'.\n"
69a0200c
TZ
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"
5463bfda
TZ
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"
7ef224d1 5714 "\t Reading the 'hist' file for the event will dump the hash\n"
52a7f16d
TZ
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"
5463bfda
TZ
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"
c6afad49
TZ
5722 "\t .hex display a number as a hex value\n"
5723 "\t .sym display an address as a symbol\n"
6b4827ad 5724 "\t .sym-offset display an address as a symbol and offset\n"
31696198 5725 "\t .execname display a common_pid as a program name\n"
860f9f6b
TZ
5726 "\t .syscall display a syscall id as a syscall name\n"
5727 "\t .log2 display log2 value rather than raw number\n"
37036435 5728 "\t .buckets=size display values in groups of size rather than raw number\n"
abaa5258 5729 "\t .usecs display a common_timestamp in microseconds\n"
a2c54256
MHG
5730 "\t .percent display a number of percentage value\n"
5731 "\t .graph display a bar-graph of a value\n\n"
83e99914
TZ
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"
e86ae9ba
TZ
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"
5738 "\t unchanged.\n\n"
ccf47f5c
MHG
5739 "\t The 'nohitcount' (or NOHC) parameter will suppress display of\n"
5740 "\t raw hitcount in the histogram.\n\n"
d0bad49b
TZ
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"
9e5a36a3 5743 "\t already-attached hist trigger. The syntax is analogous to\n"
c3e49506
TZ
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"
dff81f55
TZ
5750 "\t onmax(var) - invoke if var exceeds current max\n"
5751 "\t onchange(var) - invoke action if var changes\n\n"
c3e49506 5752 "\t The available actions are:\n\n"
e91eefd7 5753 "\t trace(<synthetic_event>,param list) - generate synthetic event\n"
c3e49506 5754 "\t save(field,...) - save current event fields\n"
a3785b7e 5755#ifdef CONFIG_TRACER_SNAPSHOT
1bc36bd4
TZ
5756 "\t snapshot() - snapshot the trace buffer\n\n"
5757#endif
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"
b81a3a10 5761 "\t example: echo 'myevent u64 lat; char name[]; long[] stack' >> synthetic_events\n"
a3785b7e 5762#endif
7ef224d1 5763#endif
7bd2f24c
IM
5764;
5765
5766static ssize_t
5767tracing_readme_read(struct file *filp, char __user *ubuf,
5768 size_t cnt, loff_t *ppos)
5769{
5770 return simple_read_from_buffer(ubuf, cnt, ppos,
5771 readme_msg, strlen(readme_msg));
5772}
5773
5e2336a0 5774static const struct file_operations tracing_readme_fops = {
c7078de1
IM
5775 .open = tracing_open_generic,
5776 .read = tracing_readme_read,
b444786f 5777 .llseek = generic_file_llseek,
7bd2f24c
IM
5778};
5779
681bec03 5780#ifdef CONFIG_TRACE_EVAL_MAP_FILE
23bf8cb8 5781static union trace_eval_map_item *
f57a4143 5782update_eval_map(union trace_eval_map_item *ptr)
9828413d 5783{
00f4b652 5784 if (!ptr->map.eval_string) {
9828413d
SRRH
5785 if (ptr->tail.next) {
5786 ptr = ptr->tail.next;
5787 /* Set ptr to the next real item (skip head) */
5788 ptr++;
5789 } else
5790 return NULL;
5791 }
5792 return ptr;
5793}
5794
f57a4143 5795static void *eval_map_next(struct seq_file *m, void *v, loff_t *pos)
9828413d 5796{
23bf8cb8 5797 union trace_eval_map_item *ptr = v;
9828413d
SRRH
5798
5799 /*
5800 * Paranoid! If ptr points to end, we don't want to increment past it.
5801 * This really should never happen.
5802 */
039958a5 5803 (*pos)++;
f57a4143 5804 ptr = update_eval_map(ptr);
9828413d
SRRH
5805 if (WARN_ON_ONCE(!ptr))
5806 return NULL;
5807
5808 ptr++;
f57a4143 5809 ptr = update_eval_map(ptr);
9828413d
SRRH
5810
5811 return ptr;
5812}
5813
f57a4143 5814static void *eval_map_start(struct seq_file *m, loff_t *pos)
9828413d 5815{
23bf8cb8 5816 union trace_eval_map_item *v;
9828413d
SRRH
5817 loff_t l = 0;
5818
1793ed93 5819 mutex_lock(&trace_eval_mutex);
9828413d 5820
23bf8cb8 5821 v = trace_eval_maps;
9828413d
SRRH
5822 if (v)
5823 v++;
5824
5825 while (v && l < *pos) {
f57a4143 5826 v = eval_map_next(m, v, &l);
9828413d
SRRH
5827 }
5828
5829 return v;
5830}
5831
f57a4143 5832static void eval_map_stop(struct seq_file *m, void *v)
9828413d 5833{
1793ed93 5834 mutex_unlock(&trace_eval_mutex);
9828413d
SRRH
5835}
5836
f57a4143 5837static int eval_map_show(struct seq_file *m, void *v)
9828413d 5838{
23bf8cb8 5839 union trace_eval_map_item *ptr = v;
9828413d
SRRH
5840
5841 seq_printf(m, "%s %ld (%s)\n",
00f4b652 5842 ptr->map.eval_string, ptr->map.eval_value,
9828413d
SRRH
5843 ptr->map.system);
5844
5845 return 0;
5846}
5847
f57a4143
JL
5848static 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,
9828413d
SRRH
5853};
5854
f57a4143 5855static int tracing_eval_map_open(struct inode *inode, struct file *filp)
9828413d 5856{
8530dec6
SRV
5857 int ret;
5858
5859 ret = tracing_check_open_get_tr(NULL);
5860 if (ret)
5861 return ret;
9828413d 5862
f57a4143 5863 return seq_open(filp, &tracing_eval_map_seq_ops);
9828413d
SRRH
5864}
5865
f57a4143
JL
5866static const struct file_operations tracing_eval_map_fops = {
5867 .open = tracing_eval_map_open,
9828413d
SRRH
5868 .read = seq_read,
5869 .llseek = seq_lseek,
5870 .release = seq_release,
5871};
5872
23bf8cb8 5873static inline union trace_eval_map_item *
5f60b351 5874trace_eval_jmp_to_tail(union trace_eval_map_item *ptr)
9828413d
SRRH
5875{
5876 /* Return tail of array given the head */
5877 return ptr + ptr->head.length + 1;
5878}
5879
5880static void
f57a4143 5881trace_insert_eval_map_file(struct module *mod, struct trace_eval_map **start,
9828413d
SRRH
5882 int len)
5883{
00f4b652
JL
5884 struct trace_eval_map **stop;
5885 struct trace_eval_map **map;
23bf8cb8
JL
5886 union trace_eval_map_item *map_array;
5887 union trace_eval_map_item *ptr;
9828413d
SRRH
5888
5889 stop = start + len;
5890
5891 /*
23bf8cb8 5892 * The trace_eval_maps contains the map plus a head and tail item,
9828413d
SRRH
5893 * where the head holds the module and length of array, and the
5894 * tail holds a pointer to the next list.
5895 */
6da2ec56 5896 map_array = kmalloc_array(len + 2, sizeof(*map_array), GFP_KERNEL);
9828413d 5897 if (!map_array) {
f57a4143 5898 pr_warn("Unable to allocate trace eval mapping\n");
9828413d
SRRH
5899 return;
5900 }
5901
d33b10c0 5902 guard(mutex)(&trace_eval_mutex);
9828413d 5903
23bf8cb8
JL
5904 if (!trace_eval_maps)
5905 trace_eval_maps = map_array;
9828413d 5906 else {
23bf8cb8 5907 ptr = trace_eval_maps;
9828413d 5908 for (;;) {
5f60b351 5909 ptr = trace_eval_jmp_to_tail(ptr);
9828413d
SRRH
5910 if (!ptr->tail.next)
5911 break;
5912 ptr = ptr->tail.next;
5913
5914 }
5915 ptr->tail.next = map_array;
5916 }
5917 map_array->head.mod = mod;
5918 map_array->head.length = len;
5919 map_array++;
5920
5921 for (map = start; (unsigned long)map < (unsigned long)stop; map++) {
5922 map_array->map = **map;
5923 map_array++;
5924 }
5925 memset(map_array, 0, sizeof(*map_array));
9828413d
SRRH
5926}
5927
f57a4143 5928static void trace_create_eval_file(struct dentry *d_tracer)
9828413d 5929{
21ccc9cd 5930 trace_create_file("eval_map", TRACE_MODE_READ, d_tracer,
f57a4143 5931 NULL, &tracing_eval_map_fops);
9828413d
SRRH
5932}
5933
681bec03 5934#else /* CONFIG_TRACE_EVAL_MAP_FILE */
f57a4143
JL
5935static inline void trace_create_eval_file(struct dentry *d_tracer) { }
5936static inline void trace_insert_eval_map_file(struct module *mod,
00f4b652 5937 struct trace_eval_map **start, int len) { }
681bec03 5938#endif /* !CONFIG_TRACE_EVAL_MAP_FILE */
9828413d 5939
f57a4143 5940static void trace_insert_eval_map(struct module *mod,
00f4b652 5941 struct trace_eval_map **start, int len)
0c564a53 5942{
00f4b652 5943 struct trace_eval_map **map;
0c564a53
SRRH
5944
5945 if (len <= 0)
5946 return;
5947
5948 map = start;
5949
f57a4143 5950 trace_event_eval_update(map, len);
9828413d 5951
f57a4143 5952 trace_insert_eval_map_file(mod, start, len);
0c564a53
SRRH
5953}
5954
bc0c38d1
SR
5955static ssize_t
5956tracing_set_trace_read(struct file *filp, char __user *ubuf,
5957 size_t cnt, loff_t *ppos)
5958{
2b6080f2 5959 struct trace_array *tr = filp->private_data;
ee6c2c1b 5960 char buf[MAX_TRACER_SIZE+2];
bc0c38d1
SR
5961 int r;
5962
5963 mutex_lock(&trace_types_lock);
2b6080f2 5964 r = sprintf(buf, "%s\n", tr->current_trace->name);
bc0c38d1
SR
5965 mutex_unlock(&trace_types_lock);
5966
4bf39a94 5967 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
bc0c38d1
SR
5968}
5969
b6f11df2
ACM
5970int tracer_init(struct tracer *t, struct trace_array *tr)
5971{
1c5eb448 5972 tracing_reset_online_cpus(&tr->array_buffer);
b6f11df2
ACM
5973 return t->init(tr);
5974}
5975
1c5eb448 5976static void set_buffer_entries(struct array_buffer *buf, unsigned long val)
438ced17
VN
5977{
5978 int cpu;
737223fb 5979
438ced17 5980 for_each_tracing_cpu(cpu)
12883efb 5981 per_cpu_ptr(buf->data, cpu)->entries = val;
438ced17
VN
5982}
5983
6d98a0f2
ZY
5984static void update_buffer_entries(struct array_buffer *buf, int cpu)
5985{
5986 if (cpu == RING_BUFFER_ALL_CPUS) {
5987 set_buffer_entries(buf, ring_buffer_size(buf->buffer, 0));
5988 } else {
5989 per_cpu_ptr(buf->data, cpu)->entries = ring_buffer_size(buf->buffer, cpu);
5990 }
5991}
5992
12883efb 5993#ifdef CONFIG_TRACER_MAX_TRACE
d60da506 5994/* resize @tr's buffer to the size of @size_tr's entries */
1c5eb448
SRV
5995static int resize_buffer_duplicate_size(struct array_buffer *trace_buf,
5996 struct array_buffer *size_buf, int cpu_id)
d60da506
HT
5997{
5998 int cpu, ret = 0;
5999
6000 if (cpu_id == RING_BUFFER_ALL_CPUS) {
6001 for_each_tracing_cpu(cpu) {
12883efb
SRRH
6002 ret = ring_buffer_resize(trace_buf->buffer,
6003 per_cpu_ptr(size_buf->data, cpu)->entries, cpu);
d60da506
HT
6004 if (ret < 0)
6005 break;
12883efb
SRRH
6006 per_cpu_ptr(trace_buf->data, cpu)->entries =
6007 per_cpu_ptr(size_buf->data, cpu)->entries;
d60da506
HT
6008 }
6009 } else {
12883efb
SRRH
6010 ret = ring_buffer_resize(trace_buf->buffer,
6011 per_cpu_ptr(size_buf->data, cpu_id)->entries, cpu_id);
d60da506 6012 if (ret == 0)
12883efb
SRRH
6013 per_cpu_ptr(trace_buf->data, cpu_id)->entries =
6014 per_cpu_ptr(size_buf->data, cpu_id)->entries;
d60da506
HT
6015 }
6016
6017 return ret;
6018}
12883efb 6019#endif /* CONFIG_TRACER_MAX_TRACE */
d60da506 6020
2b6080f2
SR
6021static int __tracing_resize_ring_buffer(struct trace_array *tr,
6022 unsigned long size, int cpu)
73c5162a
SR
6023{
6024 int ret;
6025
6026 /*
6027 * If kernel or user changes the size of the ring buffer
a123c52b
SR
6028 * we use the size that was given, and we can forget about
6029 * expanding it later.
73c5162a 6030 */
a1f157c7 6031 trace_set_ring_buffer_expanded(tr);
73c5162a 6032
b382ede6 6033 /* May be called before buffers are initialized */
1c5eb448 6034 if (!tr->array_buffer.buffer)
b382ede6
SR
6035 return 0;
6036
d06aff1c 6037 /* Do not allow tracing while resizing ring buffer */
d78ab792
SRG
6038 tracing_stop_tr(tr);
6039
1c5eb448 6040 ret = ring_buffer_resize(tr->array_buffer.buffer, size, cpu);
73c5162a 6041 if (ret < 0)
d78ab792 6042 goto out_start;
73c5162a 6043
12883efb 6044#ifdef CONFIG_TRACER_MAX_TRACE
d06aff1c 6045 if (!tr->allocated_snapshot)
ef710e10
KM
6046 goto out;
6047
12883efb 6048 ret = ring_buffer_resize(tr->max_buffer.buffer, size, cpu);
73c5162a 6049 if (ret < 0) {
1c5eb448
SRV
6050 int r = resize_buffer_duplicate_size(&tr->array_buffer,
6051 &tr->array_buffer, cpu);
73c5162a 6052 if (r < 0) {
a123c52b
SR
6053 /*
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
6065 * tracing.
6066 */
73c5162a
SR
6067 WARN_ON(1);
6068 tracing_disabled = 1;
6069 }
d78ab792 6070 goto out_start;
73c5162a
SR
6071 }
6072
6d98a0f2 6073 update_buffer_entries(&tr->max_buffer, cpu);
438ced17 6074
ef710e10 6075 out:
12883efb
SRRH
6076#endif /* CONFIG_TRACER_MAX_TRACE */
6077
6d98a0f2 6078 update_buffer_entries(&tr->array_buffer, cpu);
d78ab792
SRG
6079 out_start:
6080 tracing_start_tr(tr);
73c5162a
SR
6081 return ret;
6082}
6083
9c5b9d3d
MH
6084ssize_t tracing_resize_ring_buffer(struct trace_array *tr,
6085 unsigned long size, int cpu_id)
4f271a2a 6086{
d33b10c0 6087 guard(mutex)(&trace_types_lock);
4f271a2a 6088
438ced17
VN
6089 if (cpu_id != RING_BUFFER_ALL_CPUS) {
6090 /* make sure, this cpu is enabled in the mask */
d33b10c0
SR
6091 if (!cpumask_test_cpu(cpu_id, tracing_buffer_mask))
6092 return -EINVAL;
438ced17 6093 }
4f271a2a 6094
60b8f711 6095 return __tracing_resize_ring_buffer(tr, size, cpu_id);
4f271a2a
VN
6096}
6097
fd39e48f
SR
6098struct trace_mod_entry {
6099 unsigned long mod_addr;
6100 char mod_name[MODULE_NAME_LEN];
6101};
6102
b6533482 6103struct trace_scratch {
2632a201 6104 unsigned int clock_id;
028a58ec 6105 unsigned long text_addr;
fd39e48f
SR
6106 unsigned long nr_entries;
6107 struct trace_mod_entry entries[];
b6533482
SR
6108};
6109
1bd25a6f
SR
6110static DEFINE_MUTEX(scratch_mutex);
6111
35a380dd
MHG
6112static int cmp_mod_entry(const void *key, const void *pivot)
6113{
6114 unsigned long addr = (unsigned long)key;
6115 const struct trace_mod_entry *ent = pivot;
6116
6117 if (addr >= ent[0].mod_addr && addr < ent[1].mod_addr)
6118 return 0;
6119 else
6120 return addr - ent->mod_addr;
6121}
6122
6123/**
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.
6127 */
6128unsigned long trace_adjust_address(struct trace_array *tr, unsigned long addr)
6129{
6130 struct trace_module_delta *module_delta;
6131 struct trace_scratch *tscratch;
6132 struct trace_mod_entry *entry;
00d872dd 6133 unsigned long raddr;
35a380dd
MHG
6134 int idx = 0, nr_entries;
6135
6136 /* If we don't have last boot delta, return the address */
6137 if (!(tr->flags & TRACE_ARRAY_FL_LAST_BOOT))
6138 return addr;
6139
6140 /* tr->module_delta must be protected by rcu. */
6141 guard(rcu)();
6142 tscratch = tr->scratch;
6143 /* if there is no tscrach, module_delta must be NULL. */
6144 module_delta = READ_ONCE(tr->module_delta);
1be8e54a
SR
6145 if (!module_delta || !tscratch->nr_entries ||
6146 tscratch->entries[0].mod_addr > addr) {
00d872dd
SR
6147 raddr = addr + tr->text_delta;
6148 return __is_kernel(raddr) || is_kernel_core_data(raddr) ||
6149 is_kernel_rodata(raddr) ? raddr : addr;
1be8e54a 6150 }
35a380dd
MHG
6151
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;
6157 else {
6158 entry = __inline_bsearch((void *)addr,
6159 tscratch->entries,
6160 nr_entries - 1,
6161 sizeof(tscratch->entries[0]),
6162 cmp_mod_entry);
6163 if (entry)
6164 idx = entry - tscratch->entries;
6165 }
6166
6167 return addr + module_delta->delta[idx];
6168}
6169
f00c9201 6170#ifdef CONFIG_MODULES
fd39e48f
SR
6171static int save_mod(struct module *mod, void *data)
6172{
6173 struct trace_array *tr = data;
6174 struct trace_scratch *tscratch;
6175 struct trace_mod_entry *entry;
6176 unsigned int size;
6177
6178 tscratch = tr->scratch;
6179 if (!tscratch)
6180 return -1;
6181 size = tr->scratch_size;
6182
6183 if (struct_size(tscratch, entries, tscratch->nr_entries + 1) > size)
6184 return -1;
6185
6186 entry = &tscratch->entries[tscratch->nr_entries];
6187
6188 tscratch->nr_entries++;
6189
6190 entry->mod_addr = (unsigned long)mod->mem[MOD_TEXT].base;
6191 strscpy(entry->mod_name, mod->name);
6192
6193 return 0;
6194}
f00c9201
MHG
6195#else
6196static int save_mod(struct module *mod, void *data)
6197{
6198 return 0;
6199}
6200#endif
fd39e48f 6201
7a1d1e4b
SRG
6202static void update_last_data(struct trace_array *tr)
6203{
35a380dd 6204 struct trace_module_delta *module_delta;
b6533482
SR
6205 struct trace_scratch *tscratch;
6206
fd39e48f
SR
6207 if (!(tr->flags & TRACE_ARRAY_FL_BOOT))
6208 return;
6209
486fbcb3 6210 if (!(tr->flags & TRACE_ARRAY_FL_LAST_BOOT))
7a1d1e4b
SRG
6211 return;
6212
486fbcb3
MHG
6213 /* Only if the buffer has previous boot data clear and update it. */
6214 tr->flags &= ~TRACE_ARRAY_FL_LAST_BOOT;
6215
fd39e48f
SR
6216 /* Reset the module list and reload them */
6217 if (tr->scratch) {
6218 struct trace_scratch *tscratch = tr->scratch;
6219
2632a201 6220 tscratch->clock_id = tr->clock_id;
fd39e48f
SR
6221 memset(tscratch->entries, 0,
6222 flex_array_size(tscratch, entries, tscratch->nr_entries));
6223 tscratch->nr_entries = 0;
6224
1bd25a6f 6225 guard(mutex)(&scratch_mutex);
fd39e48f
SR
6226 module_for_each_mod(save_mod, tr);
6227 }
6228
09663753
SR
6229 /*
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.
6234 */
6235 tracing_reset_all_cpus(&tr->array_buffer);
7a1d1e4b
SRG
6236
6237 /* Using current data now */
6238 tr->text_delta = 0;
bcba8d4d 6239
b6533482
SR
6240 if (!tr->scratch)
6241 return;
6242
6243 tscratch = tr->scratch;
35a380dd
MHG
6244 module_delta = READ_ONCE(tr->module_delta);
6245 WRITE_ONCE(tr->module_delta, NULL);
6246 kfree_rcu(module_delta, rcu);
b6533482
SR
6247
6248 /* Set the persistent ring buffer meta data to this address */
028a58ec 6249 tscratch->text_addr = (unsigned long)_text;
7a1d1e4b 6250}
ef710e10 6251
1852fcce
SR
6252/**
6253 * tracing_update_buffers - used by tracing facility to expand ring buffers
a1f157c7 6254 * @tr: The tracing instance
1852fcce
SR
6255 *
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.
6260 *
6261 * This function is to be called when a tracer is about to be used.
6262 */
a1f157c7 6263int tracing_update_buffers(struct trace_array *tr)
1852fcce
SR
6264{
6265 int ret = 0;
6266
1027fcb2 6267 mutex_lock(&trace_types_lock);
7a1d1e4b
SRG
6268
6269 update_last_data(tr);
6270
a1f157c7
ZY
6271 if (!tr->ring_buffer_expanded)
6272 ret = __tracing_resize_ring_buffer(tr, trace_buf_size,
438ced17 6273 RING_BUFFER_ALL_CPUS);
1027fcb2 6274 mutex_unlock(&trace_types_lock);
1852fcce
SR
6275
6276 return ret;
6277}
6278
577b785f
SR
6279struct trace_option_dentry;
6280
37aea98b 6281static void
2b6080f2 6282create_trace_option_files(struct trace_array *tr, struct tracer *tracer);
577b785f 6283
6b450d25
SRRH
6284/*
6285 * Used to clear out the tracer before deletion of an instance.
6286 * Must have trace_types_lock held.
6287 */
6288static void tracing_set_nop(struct trace_array *tr)
6289{
6290 if (tr->current_trace == &nop_trace)
6291 return;
cf9f0f7c 6292
50512ab5 6293 tr->current_trace->enabled--;
6b450d25
SRRH
6294
6295 if (tr->current_trace->reset)
6296 tr->current_trace->reset(tr);
6297
6298 tr->current_trace = &nop_trace;
6299}
6300
ef9188bc
MPT
6301static bool tracer_options_updated;
6302
41d9c0be 6303static void add_tracer_options(struct trace_array *tr, struct tracer *t)
bc0c38d1 6304{
09d23a1d
SRRH
6305 /* Only enable if the directory has been created already. */
6306 if (!tr->dir)
6307 return;
6308
ef9188bc
MPT
6309 /* Only create trace option files after update_tracer_options finish */
6310 if (!tracer_options_updated)
6311 return;
6312
37aea98b 6313 create_trace_option_files(tr, t);
09d23a1d
SRRH
6314}
6315
9c5b9d3d 6316int tracing_set_tracer(struct trace_array *tr, const char *buf)
09d23a1d 6317{
bc0c38d1 6318 struct tracer *t;
12883efb 6319#ifdef CONFIG_TRACER_MAX_TRACE
34600f0e 6320 bool had_max_tr;
12883efb 6321#endif
d33b10c0 6322 int ret;
bc0c38d1 6323
d33b10c0 6324 guard(mutex)(&trace_types_lock);
1027fcb2 6325
7a1d1e4b
SRG
6326 update_last_data(tr);
6327
a1f157c7 6328 if (!tr->ring_buffer_expanded) {
2b6080f2 6329 ret = __tracing_resize_ring_buffer(tr, trace_buf_size,
438ced17 6330 RING_BUFFER_ALL_CPUS);
73c5162a 6331 if (ret < 0)
d33b10c0 6332 return ret;
73c5162a
SR
6333 ret = 0;
6334 }
6335
bc0c38d1
SR
6336 for (t = trace_types; t; t = t->next) {
6337 if (strcmp(t->name, buf) == 0)
6338 break;
6339 }
d33b10c0
SR
6340 if (!t)
6341 return -EINVAL;
6342
2b6080f2 6343 if (t == tr->current_trace)
d33b10c0 6344 return 0;
bc0c38d1 6345
a35873a0
TZ
6346#ifdef CONFIG_TRACER_SNAPSHOT
6347 if (t->use_max_tr) {
c0a581d7 6348 local_irq_disable();
a35873a0 6349 arch_spin_lock(&tr->max_lock);
22bec11a 6350 ret = tr->cond_snapshot ? -EBUSY : 0;
a35873a0 6351 arch_spin_unlock(&tr->max_lock);
c0a581d7 6352 local_irq_enable();
a35873a0 6353 if (ret)
d33b10c0 6354 return ret;
a35873a0
TZ
6355 }
6356#endif
c7b3ae0b
ZSZ
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",
6360 t->name);
d1e27ee9 6361 return -EINVAL;
c7b3ae0b
ZSZ
6362 }
6363
607e2ea1 6364 /* Some tracers are only allowed for the top level buffer */
d33b10c0
SR
6365 if (!trace_ok_for_array(t, tr))
6366 return -EINVAL;
607e2ea1 6367
cf6ab6d9 6368 /* If trace pipe files are being read, we can't change the tracer */
d33b10c0
SR
6369 if (tr->trace_ref)
6370 return -EBUSY;
cf6ab6d9 6371
9f029e83 6372 trace_branch_disable();
613f04a0 6373
50512ab5 6374 tr->current_trace->enabled--;
613f04a0 6375
2b6080f2
SR
6376 if (tr->current_trace->reset)
6377 tr->current_trace->reset(tr);
34600f0e 6378
a541a955
SRG
6379#ifdef CONFIG_TRACER_MAX_TRACE
6380 had_max_tr = tr->current_trace->use_max_tr;
6381
74401729 6382 /* Current trace needs to be nop_trace before synchronize_rcu */
2b6080f2 6383 tr->current_trace = &nop_trace;
34600f0e
SR
6384
6385 if (had_max_tr && !t->use_max_tr) {
6386 /*
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.
6392 */
74401729 6393 synchronize_rcu();
3209cff4 6394 free_snapshot(tr);
180e4e39 6395 tracing_disarm_snapshot(tr);
ef710e10 6396 }
12883efb 6397
cca990c7 6398 if (!had_max_tr && t->use_max_tr) {
180e4e39
VD
6399 ret = tracing_arm_snapshot_locked(tr);
6400 if (ret)
d33b10c0 6401 return ret;
ef710e10 6402 }
a541a955
SRG
6403#else
6404 tr->current_trace = &nop_trace;
12883efb 6405#endif
577b785f 6406
1c80025a 6407 if (t->init) {
b6f11df2 6408 ret = tracer_init(t, tr);
180e4e39
VD
6409 if (ret) {
6410#ifdef CONFIG_TRACER_MAX_TRACE
6411 if (t->use_max_tr)
6412 tracing_disarm_snapshot(tr);
6413#endif
d33b10c0 6414 return ret;
180e4e39 6415 }
1c80025a 6416 }
bc0c38d1 6417
2b6080f2 6418 tr->current_trace = t;
50512ab5 6419 tr->current_trace->enabled++;
9f029e83 6420 trace_branch_enable(tr);
bc0c38d1 6421
d33b10c0 6422 return 0;
d9e54076
PZ
6423}
6424
6425static ssize_t
6426tracing_set_trace_write(struct file *filp, const char __user *ubuf,
6427 size_t cnt, loff_t *ppos)
6428{
607e2ea1 6429 struct trace_array *tr = filp->private_data;
ee6c2c1b 6430 char buf[MAX_TRACER_SIZE+1];
cb24693d 6431 char *name;
d9e54076 6432 size_t ret;
e6e7a65a
FW
6433 int err;
6434
6435 ret = cnt;
d9e54076 6436
ee6c2c1b
LZ
6437 if (cnt > MAX_TRACER_SIZE)
6438 cnt = MAX_TRACER_SIZE;
d9e54076 6439
4afe6495 6440 if (copy_from_user(buf, ubuf, cnt))
d9e54076
PZ
6441 return -EFAULT;
6442
6443 buf[cnt] = 0;
6444
cb24693d 6445 name = strim(buf);
d9e54076 6446
cb24693d 6447 err = tracing_set_tracer(tr, name);
e6e7a65a
FW
6448 if (err)
6449 return err;
d9e54076 6450
cf8517cf 6451 *ppos += ret;
bc0c38d1 6452
c2931e05 6453 return ret;
bc0c38d1
SR
6454}
6455
6456static ssize_t
6508fa76
SF
6457tracing_nsecs_read(unsigned long *ptr, char __user *ubuf,
6458 size_t cnt, loff_t *ppos)
bc0c38d1 6459{
bc0c38d1
SR
6460 char buf[64];
6461 int r;
6462
cffae437 6463 r = snprintf(buf, sizeof(buf), "%ld\n",
bc0c38d1 6464 *ptr == (unsigned long)-1 ? -1 : nsecs_to_usecs(*ptr));
cffae437
SR
6465 if (r > sizeof(buf))
6466 r = sizeof(buf);
4bf39a94 6467 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
bc0c38d1
SR
6468}
6469
6470static ssize_t
6508fa76
SF
6471tracing_nsecs_write(unsigned long *ptr, const char __user *ubuf,
6472 size_t cnt, loff_t *ppos)
bc0c38d1 6473{
5e39841c 6474 unsigned long val;
c6caeeb1 6475 int ret;
bc0c38d1 6476
22fe9b54
PH
6477 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
6478 if (ret)
c6caeeb1 6479 return ret;
bc0c38d1
SR
6480
6481 *ptr = val * 1000;
6482
6483 return cnt;
6484}
6485
6508fa76
SF
6486static ssize_t
6487tracing_thresh_read(struct file *filp, char __user *ubuf,
6488 size_t cnt, loff_t *ppos)
6489{
6490 return tracing_nsecs_read(&tracing_thresh, ubuf, cnt, ppos);
6491}
6492
6493static ssize_t
6494tracing_thresh_write(struct file *filp, const char __user *ubuf,
6495 size_t cnt, loff_t *ppos)
6496{
6497 struct trace_array *tr = filp->private_data;
6498 int ret;
6499
d33b10c0 6500 guard(mutex)(&trace_types_lock);
6508fa76
SF
6501 ret = tracing_nsecs_write(&tracing_thresh, ubuf, cnt, ppos);
6502 if (ret < 0)
d33b10c0 6503 return ret;
6508fa76
SF
6504
6505 if (tr->current_trace->update_thresh) {
6506 ret = tr->current_trace->update_thresh(tr);
6507 if (ret < 0)
d33b10c0 6508 return ret;
6508fa76
SF
6509 }
6510
d33b10c0 6511 return cnt;
6508fa76
SF
6512}
6513
e25e43a4 6514#ifdef CONFIG_TRACER_MAX_TRACE
e428abbb 6515
6508fa76
SF
6516static ssize_t
6517tracing_max_lat_read(struct file *filp, char __user *ubuf,
6518 size_t cnt, loff_t *ppos)
6519{
7d660c9b
SRG
6520 struct trace_array *tr = filp->private_data;
6521
6522 return tracing_nsecs_read(&tr->max_latency, ubuf, cnt, ppos);
6508fa76
SF
6523}
6524
6525static ssize_t
6526tracing_max_lat_write(struct file *filp, const char __user *ubuf,
6527 size_t cnt, loff_t *ppos)
6528{
7d660c9b
SRG
6529 struct trace_array *tr = filp->private_data;
6530
6531 return tracing_nsecs_write(&tr->max_latency, ubuf, cnt, ppos);
6508fa76
SF
6532}
6533
e428abbb
CG
6534#endif
6535
c2489bb7
ZY
6536static int open_pipe_on_cpu(struct trace_array *tr, int cpu)
6537{
6538 if (cpu == RING_BUFFER_ALL_CPUS) {
6539 if (cpumask_empty(tr->pipe_cpumask)) {
6540 cpumask_setall(tr->pipe_cpumask);
6541 return 0;
6542 }
6543 } else if (!cpumask_test_cpu(cpu, tr->pipe_cpumask)) {
6544 cpumask_set_cpu(cpu, tr->pipe_cpumask);
6545 return 0;
6546 }
6547 return -EBUSY;
6548}
6549
6550static void close_pipe_on_cpu(struct trace_array *tr, int cpu)
6551{
6552 if (cpu == RING_BUFFER_ALL_CPUS) {
6553 WARN_ON(!cpumask_full(tr->pipe_cpumask));
6554 cpumask_clear(tr->pipe_cpumask);
6555 } else {
6556 WARN_ON(!cpumask_test_cpu(cpu, tr->pipe_cpumask));
6557 cpumask_clear_cpu(cpu, tr->pipe_cpumask);
6558 }
6559}
6560
b3806b43
SR
6561static int tracing_open_pipe(struct inode *inode, struct file *filp)
6562{
15544209 6563 struct trace_array *tr = inode->i_private;
b3806b43 6564 struct trace_iterator *iter;
c2489bb7 6565 int cpu;
8530dec6 6566 int ret;
b3806b43 6567
8530dec6
SRV
6568 ret = tracing_check_open_get_tr(tr);
6569 if (ret)
6570 return ret;
7b85af63 6571
b04cc6b1 6572 mutex_lock(&trace_types_lock);
c2489bb7
ZY
6573 cpu = tracing_get_cpu(inode);
6574 ret = open_pipe_on_cpu(tr, cpu);
6575 if (ret)
6576 goto fail_pipe_on_cpu;
b04cc6b1 6577
b3806b43
SR
6578 /* create a buffer to store the information to pass to userspace */
6579 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
b04cc6b1
FW
6580 if (!iter) {
6581 ret = -ENOMEM;
c2489bb7 6582 goto fail_alloc_iter;
b04cc6b1 6583 }
b3806b43 6584
3a161d99 6585 trace_seq_init(&iter->seq);
d716ff71 6586 iter->trace = tr->current_trace;
d7350c3f 6587
4462344e 6588 if (!alloc_cpumask_var(&iter->started, GFP_KERNEL)) {
b04cc6b1 6589 ret = -ENOMEM;
d7350c3f 6590 goto fail;
4462344e
RR
6591 }
6592
a309720c 6593 /* trace pipe does not show start of buffer */
4462344e 6594 cpumask_setall(iter->started);
a309720c 6595
983f938a 6596 if (tr->trace_flags & TRACE_ITER_LATENCY_FMT)
112f38a7
SR
6597 iter->iter_flags |= TRACE_FILE_LAT_FMT;
6598
8be0709f 6599 /* Output in nanoseconds only if we are using a clock in nanoseconds. */
58e8eedf 6600 if (trace_clocks[tr->clock_id].in_ns)
8be0709f
DS
6601 iter->iter_flags |= TRACE_FILE_TIME_IN_NS;
6602
15544209 6603 iter->tr = tr;
1c5eb448 6604 iter->array_buffer = &tr->array_buffer;
c2489bb7 6605 iter->cpu_file = cpu;
d7350c3f 6606 mutex_init(&iter->mutex);
b3806b43
SR
6607 filp->private_data = iter;
6608
107bad8b
SR
6609 if (iter->trace->pipe_open)
6610 iter->trace->pipe_open(iter);
107bad8b 6611
b444786f 6612 nonseekable_open(inode, filp);
cf6ab6d9 6613
7ef282e0 6614 tr->trace_ref++;
c2489bb7 6615
b04cc6b1
FW
6616 mutex_unlock(&trace_types_lock);
6617 return ret;
d7350c3f
FW
6618
6619fail:
d7350c3f 6620 kfree(iter);
c2489bb7
ZY
6621fail_alloc_iter:
6622 close_pipe_on_cpu(tr, cpu);
6623fail_pipe_on_cpu:
7b85af63 6624 __trace_array_put(tr);
d7350c3f
FW
6625 mutex_unlock(&trace_types_lock);
6626 return ret;
b3806b43
SR
6627}
6628
6629static int tracing_release_pipe(struct inode *inode, struct file *file)
6630{
6631 struct trace_iterator *iter = file->private_data;
15544209 6632 struct trace_array *tr = inode->i_private;
b3806b43 6633
b04cc6b1
FW
6634 mutex_lock(&trace_types_lock);
6635
7ef282e0 6636 tr->trace_ref--;
cf6ab6d9 6637
29bf4a5e 6638 if (iter->trace->pipe_close)
c521efd1 6639 iter->trace->pipe_close(iter);
c2489bb7 6640 close_pipe_on_cpu(tr, iter->cpu_file);
b04cc6b1
FW
6641 mutex_unlock(&trace_types_lock);
6642
6bba9288 6643 free_trace_iter_content(iter);
b3806b43 6644 kfree(iter);
b3806b43 6645
7b85af63
SRRH
6646 trace_array_put(tr);
6647
b3806b43
SR
6648 return 0;
6649}
6650
9dd95748 6651static __poll_t
cc60cdc9 6652trace_poll(struct trace_iterator *iter, struct file *filp, poll_table *poll_table)
2a2cc8f7 6653{
983f938a
SRRH
6654 struct trace_array *tr = iter->tr;
6655
15693458
SRRH
6656 /* Iterators are static, they should be filled or empty */
6657 if (trace_buffer_iter(iter, iter->cpu_file))
a9a08845 6658 return EPOLLIN | EPOLLRDNORM;
2a2cc8f7 6659
983f938a 6660 if (tr->trace_flags & TRACE_ITER_BLOCK)
2a2cc8f7
SSP
6661 /*
6662 * Always select as readable when in blocking mode
6663 */
a9a08845 6664 return EPOLLIN | EPOLLRDNORM;
15693458 6665 else
1c5eb448 6666 return ring_buffer_poll_wait(iter->array_buffer->buffer, iter->cpu_file,
42fb0a1e 6667 filp, poll_table, iter->tr->buffer_percent);
2a2cc8f7 6668}
2a2cc8f7 6669
9dd95748 6670static __poll_t
cc60cdc9
SR
6671tracing_poll_pipe(struct file *filp, poll_table *poll_table)
6672{
6673 struct trace_iterator *iter = filp->private_data;
6674
6675 return trace_poll(iter, filp, poll_table);
2a2cc8f7
SSP
6676}
6677
d716ff71 6678/* Must be called with iter->mutex held. */
ff98781b 6679static int tracing_wait_pipe(struct file *filp)
b3806b43
SR
6680{
6681 struct trace_iterator *iter = filp->private_data;
8b8b3683 6682 int ret;
b3806b43 6683
b3806b43 6684 while (trace_empty(iter)) {
2dc8f095 6685
107bad8b 6686 if ((filp->f_flags & O_NONBLOCK)) {
ff98781b 6687 return -EAGAIN;
107bad8b 6688 }
2dc8f095 6689
b3806b43 6690 /*
250bfd3d 6691 * We block until we read something and tracing is disabled.
b3806b43
SR
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.
6696 *
6697 * iter->pos will be 0 if we haven't read anything.
6698 */
75df6e68 6699 if (!tracer_tracing_is_on(iter->tr) && iter->pos)
b3806b43 6700 break;
f4874261
SRRH
6701
6702 mutex_unlock(&iter->mutex);
6703
2c2b0a78 6704 ret = wait_on_pipe(iter, 0);
f4874261
SRRH
6705
6706 mutex_lock(&iter->mutex);
6707
8b8b3683
SRRH
6708 if (ret)
6709 return ret;
b3806b43
SR
6710 }
6711
ff98781b
EGM
6712 return 1;
6713}
6714
32dc0042
MHG
6715static bool update_last_data_if_empty(struct trace_array *tr)
6716{
6717 if (!(tr->flags & TRACE_ARRAY_FL_LAST_BOOT))
6718 return false;
6719
6720 if (!ring_buffer_empty(tr->array_buffer.buffer))
6721 return false;
6722
6723 /*
6724 * If the buffer contains the last boot data and all per-cpu
6725 * buffers are empty, reset it from the kernel side.
6726 */
6727 update_last_data(tr);
6728 return true;
6729}
6730
ff98781b
EGM
6731/*
6732 * Consumer reader.
6733 */
6734static ssize_t
6735tracing_read_pipe(struct file *filp, char __user *ubuf,
6736 size_t cnt, loff_t *ppos)
6737{
6738 struct trace_iterator *iter = filp->private_data;
6739 ssize_t sret;
6740
d7350c3f
FW
6741 /*
6742 * Avoid more than one consumer on a single file descriptor
6743 * This is just a matter of traces coherency, the ring buffer itself
6744 * is protected.
6745 */
d33b10c0 6746 guard(mutex)(&iter->mutex);
1245800c
SRRH
6747
6748 /* return any leftover data */
6749 sret = trace_seq_to_user(&iter->seq, ubuf, cnt);
6750 if (sret != -EBUSY)
d33b10c0 6751 return sret;
1245800c
SRRH
6752
6753 trace_seq_init(&iter->seq);
6754
ff98781b
EGM
6755 if (iter->trace->read) {
6756 sret = iter->trace->read(iter, filp, ubuf, cnt, ppos);
6757 if (sret)
d33b10c0 6758 return sret;
ff98781b
EGM
6759 }
6760
6761waitagain:
32dc0042
MHG
6762 if (update_last_data_if_empty(iter->tr))
6763 return 0;
6764
ff98781b
EGM
6765 sret = tracing_wait_pipe(filp);
6766 if (sret <= 0)
d33b10c0 6767 return sret;
ff98781b 6768
b3806b43 6769 /* stop when tracing is finished */
d33b10c0
SR
6770 if (trace_empty(iter))
6771 return 0;
b3806b43 6772
40fc60e3
SRG
6773 if (cnt >= TRACE_SEQ_BUFFER_SIZE)
6774 cnt = TRACE_SEQ_BUFFER_SIZE - 1;
b3806b43 6775
53d0aa77 6776 /* reset all but tr, trace, and overruns */
2768c1e7 6777 trace_iterator_reset(iter);
ed5467da 6778 cpumask_clear(iter->started);
d303de1f 6779 trace_seq_init(&iter->seq);
b3806b43 6780
4f535968 6781 trace_event_read_lock();
7e53bd42 6782 trace_access_lock(iter->cpu_file);
955b61e5 6783 while (trace_find_next_entry_inc(iter) != NULL) {
2c4f035f 6784 enum print_line_t ret;
5ac48378 6785 int save_len = iter->seq.seq.len;
088b1e42 6786
f9896bf3 6787 ret = print_trace_line(iter);
2c4f035f 6788 if (ret == TRACE_TYPE_PARTIAL_LINE) {
c1ac03af
YJ
6789 /*
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.
6794 */
6795 if (save_len == 0) {
6796 iter->seq.full = 0;
6797 trace_seq_puts(&iter->seq, "[LINE TOO BIG]\n");
6798 trace_consume(iter);
6799 break;
6800 }
6801
6802 /* In other cases, don't print partial lines */
5ac48378 6803 iter->seq.seq.len = save_len;
b3806b43 6804 break;
088b1e42 6805 }
b91facc3
FW
6806 if (ret != TRACE_TYPE_NO_CONSUME)
6807 trace_consume(iter);
b3806b43 6808
5ac48378 6809 if (trace_seq_used(&iter->seq) >= cnt)
b3806b43 6810 break;
ee5e51f5
JO
6811
6812 /*
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.
6816 */
6817 WARN_ONCE(iter->seq.full, "full flag set for trace type %d",
6818 iter->ent->type);
b3806b43 6819 }
7e53bd42 6820 trace_access_unlock(iter->cpu_file);
4f535968 6821 trace_event_read_unlock();
b3806b43 6822
b3806b43 6823 /* Now copy what we have to the user */
6c6c2796 6824 sret = trace_seq_to_user(&iter->seq, ubuf, cnt);
d0ed46b6 6825 if (iter->seq.readpos >= trace_seq_used(&iter->seq))
f9520750 6826 trace_seq_init(&iter->seq);
9ff4b974
PP
6827
6828 /*
25985edc 6829 * If there was nothing to send to user, in spite of consuming trace
9ff4b974
PP
6830 * entries, go back to wait for more entries.
6831 */
6c6c2796 6832 if (sret == -EBUSY)
9ff4b974 6833 goto waitagain;
b3806b43 6834
6c6c2796 6835 return sret;
b3806b43
SR
6836}
6837
3c56819b
EGM
6838static void tracing_spd_release_pipe(struct splice_pipe_desc *spd,
6839 unsigned int idx)
6840{
6841 __free_page(spd->pages[idx]);
6842}
6843
34cd4998 6844static size_t
fa7c7f6e 6845tracing_fill_pipe_page(size_t rem, struct trace_iterator *iter)
34cd4998
SR
6846{
6847 size_t count;
74f06bb7 6848 int save_len;
34cd4998
SR
6849 int ret;
6850
6851 /* Seq buffer is page-sized, exactly what we need. */
6852 for (;;) {
74f06bb7 6853 save_len = iter->seq.seq.len;
34cd4998 6854 ret = print_trace_line(iter);
74f06bb7
SRRH
6855
6856 if (trace_seq_has_overflowed(&iter->seq)) {
6857 iter->seq.seq.len = save_len;
34cd4998
SR
6858 break;
6859 }
74f06bb7
SRRH
6860
6861 /*
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.
6865 */
34cd4998 6866 if (ret == TRACE_TYPE_PARTIAL_LINE) {
74f06bb7
SRRH
6867 iter->seq.seq.len = save_len;
6868 break;
6869 }
6870
5ac48378 6871 count = trace_seq_used(&iter->seq) - save_len;
74f06bb7
SRRH
6872 if (rem < count) {
6873 rem = 0;
6874 iter->seq.seq.len = save_len;
34cd4998
SR
6875 break;
6876 }
6877
74e7ff8c
LJ
6878 if (ret != TRACE_TYPE_NO_CONSUME)
6879 trace_consume(iter);
34cd4998 6880 rem -= count;
955b61e5 6881 if (!trace_find_next_entry_inc(iter)) {
34cd4998
SR
6882 rem = 0;
6883 iter->ent = NULL;
6884 break;
6885 }
6886 }
6887
6888 return rem;
6889}
6890
3c56819b
EGM
6891static ssize_t tracing_splice_read_pipe(struct file *filp,
6892 loff_t *ppos,
6893 struct pipe_inode_info *pipe,
6894 size_t len,
6895 unsigned int flags)
6896{
35f3d14d
JA
6897 struct page *pages_def[PIPE_DEF_BUFFERS];
6898 struct partial_page partial_def[PIPE_DEF_BUFFERS];
3c56819b
EGM
6899 struct trace_iterator *iter = filp->private_data;
6900 struct splice_pipe_desc spd = {
35f3d14d
JA
6901 .pages = pages_def,
6902 .partial = partial_def,
34cd4998 6903 .nr_pages = 0, /* This gets updated below. */
047fe360 6904 .nr_pages_max = PIPE_DEF_BUFFERS,
6797d97a 6905 .ops = &default_pipe_buf_ops,
34cd4998 6906 .spd_release = tracing_spd_release_pipe,
3c56819b
EGM
6907 };
6908 ssize_t ret;
34cd4998 6909 size_t rem;
3c56819b
EGM
6910 unsigned int i;
6911
35f3d14d
JA
6912 if (splice_grow_spd(pipe, &spd))
6913 return -ENOMEM;
6914
d7350c3f 6915 mutex_lock(&iter->mutex);
3c56819b
EGM
6916
6917 if (iter->trace->splice_read) {
6918 ret = iter->trace->splice_read(iter, filp,
6919 ppos, pipe, len, flags);
6920 if (ret)
34cd4998 6921 goto out_err;
3c56819b
EGM
6922 }
6923
6924 ret = tracing_wait_pipe(filp);
6925 if (ret <= 0)
34cd4998 6926 goto out_err;
3c56819b 6927
955b61e5 6928 if (!iter->ent && !trace_find_next_entry_inc(iter)) {
3c56819b 6929 ret = -EFAULT;
34cd4998 6930 goto out_err;
3c56819b
EGM
6931 }
6932
4f535968 6933 trace_event_read_lock();
7e53bd42 6934 trace_access_lock(iter->cpu_file);
4f535968 6935
3c56819b 6936 /* Fill as many pages as possible. */
a786c06d 6937 for (i = 0, rem = len; i < spd.nr_pages_max && rem; i++) {
35f3d14d
JA
6938 spd.pages[i] = alloc_page(GFP_KERNEL);
6939 if (!spd.pages[i])
34cd4998 6940 break;
3c56819b 6941
fa7c7f6e 6942 rem = tracing_fill_pipe_page(rem, iter);
3c56819b
EGM
6943
6944 /* Copy the data into the page, so we can start over. */
6945 ret = trace_seq_to_buffer(&iter->seq,
35f3d14d 6946 page_address(spd.pages[i]),
f5178c41 6947 min((size_t)trace_seq_used(&iter->seq),
2fbdb6d8 6948 (size_t)PAGE_SIZE));
3c56819b 6949 if (ret < 0) {
35f3d14d 6950 __free_page(spd.pages[i]);
3c56819b
EGM
6951 break;
6952 }
35f3d14d 6953 spd.partial[i].offset = 0;
f5178c41 6954 spd.partial[i].len = ret;
3c56819b 6955
f9520750 6956 trace_seq_init(&iter->seq);
3c56819b
EGM
6957 }
6958
7e53bd42 6959 trace_access_unlock(iter->cpu_file);
4f535968 6960 trace_event_read_unlock();
d7350c3f 6961 mutex_unlock(&iter->mutex);
3c56819b
EGM
6962
6963 spd.nr_pages = i;
6964
a29054d9
SRRH
6965 if (i)
6966 ret = splice_to_pipe(pipe, &spd);
6967 else
6968 ret = 0;
35f3d14d 6969out:
047fe360 6970 splice_shrink_spd(&spd);
35f3d14d 6971 return ret;
3c56819b 6972
34cd4998 6973out_err:
d7350c3f 6974 mutex_unlock(&iter->mutex);
35f3d14d 6975 goto out;
3c56819b
EGM
6976}
6977
a98a3c3f
SR
6978static ssize_t
6979tracing_entries_read(struct file *filp, char __user *ubuf,
6980 size_t cnt, loff_t *ppos)
6981{
0bc392ee
ON
6982 struct inode *inode = file_inode(filp);
6983 struct trace_array *tr = inode->i_private;
6984 int cpu = tracing_get_cpu(inode);
438ced17
VN
6985 char buf[64];
6986 int r = 0;
6987 ssize_t ret;
a98a3c3f 6988
db526ca3 6989 mutex_lock(&trace_types_lock);
438ced17 6990
0bc392ee 6991 if (cpu == RING_BUFFER_ALL_CPUS) {
438ced17
VN
6992 int cpu, buf_size_same;
6993 unsigned long size;
6994
6995 size = 0;
6996 buf_size_same = 1;
6997 /* check if all cpu sizes are same */
6998 for_each_tracing_cpu(cpu) {
6999 /* fill in the size from first enabled cpu */
7000 if (size == 0)
1c5eb448
SRV
7001 size = per_cpu_ptr(tr->array_buffer.data, cpu)->entries;
7002 if (size != per_cpu_ptr(tr->array_buffer.data, cpu)->entries) {
438ced17
VN
7003 buf_size_same = 0;
7004 break;
7005 }
7006 }
7007
7008 if (buf_size_same) {
a1f157c7 7009 if (!tr->ring_buffer_expanded)
438ced17
VN
7010 r = sprintf(buf, "%lu (expanded: %lu)\n",
7011 size >> 10,
7012 trace_buf_size >> 10);
7013 else
7014 r = sprintf(buf, "%lu\n", size >> 10);
7015 } else
7016 r = sprintf(buf, "X\n");
7017 } else
1c5eb448 7018 r = sprintf(buf, "%lu\n", per_cpu_ptr(tr->array_buffer.data, cpu)->entries >> 10);
438ced17 7019
db526ca3
SR
7020 mutex_unlock(&trace_types_lock);
7021
438ced17
VN
7022 ret = simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
7023 return ret;
a98a3c3f
SR
7024}
7025
7026static ssize_t
7027tracing_entries_write(struct file *filp, const char __user *ubuf,
7028 size_t cnt, loff_t *ppos)
7029{
0bc392ee
ON
7030 struct inode *inode = file_inode(filp);
7031 struct trace_array *tr = inode->i_private;
a98a3c3f 7032 unsigned long val;
4f271a2a 7033 int ret;
a98a3c3f 7034
22fe9b54
PH
7035 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
7036 if (ret)
c6caeeb1 7037 return ret;
a98a3c3f
SR
7038
7039 /* must have at least 1 entry */
7040 if (!val)
7041 return -EINVAL;
7042
1696b2b0
SR
7043 /* value is in KB */
7044 val <<= 10;
0bc392ee 7045 ret = tracing_resize_ring_buffer(tr, val, tracing_get_cpu(inode));
4f271a2a
VN
7046 if (ret < 0)
7047 return ret;
a98a3c3f 7048
cf8517cf 7049 *ppos += cnt;
a98a3c3f 7050
4f271a2a
VN
7051 return cnt;
7052}
bf5e6519 7053
f81ab074
VN
7054static ssize_t
7055tracing_total_entries_read(struct file *filp, char __user *ubuf,
7056 size_t cnt, loff_t *ppos)
7057{
7058 struct trace_array *tr = filp->private_data;
7059 char buf[64];
7060 int r, cpu;
7061 unsigned long size = 0, expanded_size = 0;
7062
7063 mutex_lock(&trace_types_lock);
7064 for_each_tracing_cpu(cpu) {
1c5eb448 7065 size += per_cpu_ptr(tr->array_buffer.data, cpu)->entries >> 10;
a1f157c7 7066 if (!tr->ring_buffer_expanded)
f81ab074
VN
7067 expanded_size += trace_buf_size >> 10;
7068 }
a1f157c7 7069 if (tr->ring_buffer_expanded)
f81ab074
VN
7070 r = sprintf(buf, "%lu\n", size);
7071 else
7072 r = sprintf(buf, "%lu (expanded: %lu)\n", size, expanded_size);
7073 mutex_unlock(&trace_types_lock);
7074
7075 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
7076}
7077
1bd25a6f
SR
7078#define LAST_BOOT_HEADER ((void *)1)
7079
7080static void *l_next(struct seq_file *m, void *v, loff_t *pos)
7a1d1e4b 7081{
1bd25a6f 7082 struct trace_array *tr = m->private;
b6533482 7083 struct trace_scratch *tscratch = tr->scratch;
1bd25a6f
SR
7084 unsigned int index = *pos;
7085
7086 (*pos)++;
7a1d1e4b 7087
1bd25a6f
SR
7088 if (*pos == 1)
7089 return LAST_BOOT_HEADER;
7090
7091 /* Only show offsets of the last boot data */
7092 if (!tscratch || !(tr->flags & TRACE_ARRAY_FL_LAST_BOOT))
7093 return NULL;
7094
7095 /* *pos 0 is for the header, 1 is for the first module */
7096 index--;
7097
7098 if (index >= tscratch->nr_entries)
7099 return NULL;
7100
7101 return &tscratch->entries[index];
7102}
7103
7104static void *l_start(struct seq_file *m, loff_t *pos)
7105{
7106 mutex_lock(&scratch_mutex);
7107
7108 return l_next(m, NULL, pos);
7109}
7110
7111static void l_stop(struct seq_file *m, void *p)
7112{
7113 mutex_unlock(&scratch_mutex);
7114}
7115
7116static void show_last_boot_header(struct seq_file *m, struct trace_array *tr)
7117{
7118 struct trace_scratch *tscratch = tr->scratch;
7a1d1e4b 7119
bcba8d4d
SR
7120 /*
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.
7126 */
b6533482 7127 if (tscratch && (tr->flags & TRACE_ARRAY_FL_LAST_BOOT))
028a58ec 7128 seq_printf(m, "%lx\t[kernel]\n", tscratch->text_addr);
bcba8d4d 7129 else
1bd25a6f
SR
7130 seq_puts(m, "# Current\n");
7131}
7a1d1e4b 7132
1bd25a6f
SR
7133static int l_show(struct seq_file *m, void *v)
7134{
7135 struct trace_array *tr = m->private;
7136 struct trace_mod_entry *entry = v;
7137
7138 if (v == LAST_BOOT_HEADER) {
7139 show_last_boot_header(m, tr);
7140 return 0;
7141 }
7142
7143 seq_printf(m, "%lx\t%s\n", entry->mod_addr, entry->mod_name);
7144 return 0;
7145}
7a1d1e4b 7146
1bd25a6f
SR
7147static const struct seq_operations last_boot_seq_ops = {
7148 .start = l_start,
7149 .next = l_next,
7150 .stop = l_stop,
7151 .show = l_show,
7152};
7a1d1e4b 7153
1bd25a6f
SR
7154static int tracing_last_boot_open(struct inode *inode, struct file *file)
7155{
7156 struct trace_array *tr = inode->i_private;
7157 struct seq_file *m;
7158 int ret;
7159
7160 ret = tracing_check_open_get_tr(tr);
7161 if (ret)
7162 return ret;
7163
7164 ret = seq_open(file, &last_boot_seq_ops);
7165 if (ret) {
7166 trace_array_put(tr);
7167 return ret;
7168 }
7169
7170 m = file->private_data;
7171 m->private = tr;
7a1d1e4b 7172
1bd25a6f 7173 return 0;
7a1d1e4b
SRG
7174}
7175
950032ff
SRG
7176static int tracing_buffer_meta_open(struct inode *inode, struct file *filp)
7177{
7178 struct trace_array *tr = inode->i_private;
7179 int cpu = tracing_get_cpu(inode);
7180 int ret;
7181
7182 ret = tracing_check_open_get_tr(tr);
7183 if (ret)
7184 return ret;
7185
7186 ret = ring_buffer_meta_seq_init(filp, tr->array_buffer.buffer, cpu);
7187 if (ret < 0)
7188 __trace_array_put(tr);
7189 return ret;
7190}
7191
4f271a2a
VN
7192static ssize_t
7193tracing_free_buffer_write(struct file *filp, const char __user *ubuf,
7194 size_t cnt, loff_t *ppos)
7195{
7196 /*
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
7199 */
7200
7201 *ppos += cnt;
a98a3c3f
SR
7202
7203 return cnt;
7204}
7205
4f271a2a
VN
7206static int
7207tracing_free_buffer_release(struct inode *inode, struct file *filp)
7208{
2b6080f2
SR
7209 struct trace_array *tr = inode->i_private;
7210
cf30cf67 7211 /* disable tracing ? */
983f938a 7212 if (tr->trace_flags & TRACE_ITER_STOP_ON_FREE)
711e1243 7213 tracer_tracing_off(tr);
4f271a2a 7214 /* resize the ring buffer to 0 */
2b6080f2 7215 tracing_resize_ring_buffer(tr, 0, RING_BUFFER_ALL_CPUS);
4f271a2a 7216
7b85af63
SRRH
7217 trace_array_put(tr);
7218
4f271a2a
VN
7219 return 0;
7220}
7221
095fe489
SRG
7222#define TRACE_MARKER_MAX_SIZE 4096
7223
7b382efd
SR
7224static ssize_t write_marker_to_buffer(struct trace_array *tr, const char __user *ubuf,
7225 size_t cnt, unsigned long ip)
5bf9a1ee 7226{
d696b58c 7227 struct ring_buffer_event *event;
3dd80953 7228 enum event_trigger_type tt = ETT_NONE;
13292494 7229 struct trace_buffer *buffer;
d696b58c 7230 struct print_entry *entry;
8ec90be7 7231 int meta_size;
d696b58c 7232 ssize_t written;
8ec90be7 7233 size_t size;
d696b58c 7234 int len;
fa32e855 7235
656c7f0d 7236/* Used in tracing_mark_raw_write() as well */
0f5e5a3a
RV
7237#define FAULTED_STR "<faulted>"
7238#define FAULTED_SIZE (sizeof(FAULTED_STR) - 1) /* '\0' is already accounted for */
5bf9a1ee 7239
8ec90be7
SRG
7240 meta_size = sizeof(*entry) + 2; /* add '\0' and possible '\n' */
7241 again:
7242 size = cnt + meta_size;
d696b58c 7243
656c7f0d
SRRH
7244 /* If less than "<faulted>", then make sure we can still add that */
7245 if (cnt < FAULTED_SIZE)
7246 size += FAULTED_SIZE - cnt;
d696b58c 7247
1c5eb448 7248 buffer = tr->array_buffer.buffer;
3e9a8aad 7249 event = __trace_buffer_lock_reserve(buffer, TRACE_PRINT, size,
36590c50 7250 tracing_gen_ctx());
8ec90be7
SRG
7251 if (unlikely(!event)) {
7252 /*
7253 * If the size was greater than what was allowed, then
7254 * make it smaller and try again.
7255 */
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))
7259 return -EBADF;
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))
7263 return -EBADF;
7264 goto again;
7265 }
7266
d696b58c 7267 /* Ring buffer disabled, return as if not open for write */
656c7f0d 7268 return -EBADF;
8ec90be7 7269 }
d696b58c
SR
7270
7271 entry = ring_buffer_event_data(event);
7b382efd 7272 entry->ip = ip;
d696b58c 7273
656c7f0d
SRRH
7274 len = __copy_from_user_inatomic(&entry->buf, ubuf, cnt);
7275 if (len) {
0f5e5a3a 7276 memcpy(&entry->buf, FAULTED_STR, FAULTED_SIZE);
656c7f0d
SRRH
7277 cnt = FAULTED_SIZE;
7278 written = -EFAULT;
c13d2f7c 7279 } else
656c7f0d 7280 written = cnt;
5bf9a1ee 7281
3dd80953
SRV
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';
b47e3302 7285 tt = event_triggers_call(tr->trace_marker_file, buffer, entry, event);
3dd80953
SRV
7286 }
7287
d696b58c
SR
7288 if (entry->buf[cnt - 1] != '\n') {
7289 entry->buf[cnt] = '\n';
7290 entry->buf[cnt + 1] = '\0';
7291 } else
7292 entry->buf[cnt] = '\0';
7293
458999c6
TZ
7294 if (static_branch_unlikely(&trace_marker_exports_enabled))
7295 ftrace_exports(event, TRACE_EXPORT_MARKER);
7ffbd48d 7296 __buffer_unlock_commit(buffer, event);
5bf9a1ee 7297
3dd80953
SRV
7298 if (tt)
7299 event_triggers_post_call(tr->trace_marker_file, tt);
7300
fa32e855
SR
7301 return written;
7302}
7303
fa32e855 7304static ssize_t
7b382efd 7305tracing_mark_write(struct file *filp, const char __user *ubuf,
fa32e855
SR
7306 size_t cnt, loff_t *fpos)
7307{
7308 struct trace_array *tr = filp->private_data;
7b382efd
SR
7309 ssize_t written = -ENODEV;
7310 unsigned long ip;
656c7f0d 7311
fa32e855
SR
7312 if (tracing_disabled)
7313 return -EINVAL;
7314
7315 if (!(tr->trace_flags & TRACE_ITER_MARKERS))
7316 return -EINVAL;
7317
7b382efd 7318 if ((ssize_t)cnt < 0)
fa32e855
SR
7319 return -EINVAL;
7320
7b382efd
SR
7321 if (cnt > TRACE_MARKER_MAX_SIZE)
7322 cnt = TRACE_MARKER_MAX_SIZE;
7323
7324 /* The selftests expect this function to be the IP address */
7325 ip = _THIS_IP_;
7326
7327 /* The global trace_marker can go to multiple instances */
7328 if (tr == &global_trace) {
7329 guard(rcu)();
7330 list_for_each_entry_rcu(tr, &marker_copies, marker_list) {
7331 written = write_marker_to_buffer(tr, ubuf, cnt, ip);
7332 if (written < 0)
7333 break;
7334 }
7335 } else {
7336 written = write_marker_to_buffer(tr, ubuf, cnt, ip);
7337 }
7338
7339 return written;
7340}
7341
7342static ssize_t write_raw_marker_to_buffer(struct trace_array *tr,
7343 const char __user *ubuf, size_t cnt)
7344{
7345 struct ring_buffer_event *event;
7346 struct trace_buffer *buffer;
7347 struct raw_data_entry *entry;
7348 ssize_t written;
7349 int size;
7350 int len;
7351
7352#define FAULT_SIZE_ID (FAULTED_SIZE + sizeof(int))
7353
fa32e855 7354 size = sizeof(*entry) + cnt;
656c7f0d
SRRH
7355 if (cnt < FAULT_SIZE_ID)
7356 size += FAULT_SIZE_ID - cnt;
7357
1c5eb448 7358 buffer = tr->array_buffer.buffer;
76ca20c7
SRG
7359
7360 if (size > ring_buffer_max_event_size(buffer))
7361 return -EINVAL;
7362
3e9a8aad 7363 event = __trace_buffer_lock_reserve(buffer, TRACE_RAW_DATA, size,
36590c50 7364 tracing_gen_ctx());
656c7f0d 7365 if (!event)
fa32e855 7366 /* Ring buffer disabled, return as if not open for write */
656c7f0d 7367 return -EBADF;
fa32e855
SR
7368
7369 entry = ring_buffer_event_data(event);
7370
656c7f0d
SRRH
7371 len = __copy_from_user_inatomic(&entry->id, ubuf, cnt);
7372 if (len) {
7373 entry->id = -1;
0f5e5a3a 7374 memcpy(&entry->buf, FAULTED_STR, FAULTED_SIZE);
656c7f0d 7375 written = -EFAULT;
fa32e855 7376 } else
656c7f0d 7377 written = cnt;
fa32e855
SR
7378
7379 __buffer_unlock_commit(buffer, event);
7380
1aa54bca 7381 return written;
5bf9a1ee
PP
7382}
7383
7b382efd
SR
7384static ssize_t
7385tracing_mark_raw_write(struct file *filp, const char __user *ubuf,
7386 size_t cnt, loff_t *fpos)
7387{
7388 struct trace_array *tr = filp->private_data;
7389 ssize_t written = -ENODEV;
7390
7391#define FAULT_SIZE_ID (FAULTED_SIZE + sizeof(int))
7392
7393 if (tracing_disabled)
7394 return -EINVAL;
7395
7396 if (!(tr->trace_flags & TRACE_ITER_MARKERS))
7397 return -EINVAL;
7398
7399 /* The marker must at least have a tag id */
7400 if (cnt < sizeof(unsigned int))
7401 return -EINVAL;
7402
7403 /* The global trace_marker_raw can go to multiple instances */
7404 if (tr == &global_trace) {
7405 guard(rcu)();
7406 list_for_each_entry_rcu(tr, &marker_copies, marker_list) {
7407 written = write_raw_marker_to_buffer(tr, ubuf, cnt);
7408 if (written < 0)
7409 break;
7410 }
7411 } else {
7412 written = write_raw_marker_to_buffer(tr, ubuf, cnt);
7413 }
7414
7415 return written;
7416}
7417
13f16d20 7418static int tracing_clock_show(struct seq_file *m, void *v)
5079f326 7419{
2b6080f2 7420 struct trace_array *tr = m->private;
5079f326
Z
7421 int i;
7422
7423 for (i = 0; i < ARRAY_SIZE(trace_clocks); i++)
13f16d20 7424 seq_printf(m,
5079f326 7425 "%s%s%s%s", i ? " " : "",
2b6080f2
SR
7426 i == tr->clock_id ? "[" : "", trace_clocks[i].name,
7427 i == tr->clock_id ? "]" : "");
13f16d20 7428 seq_putc(m, '\n');
5079f326 7429
13f16d20 7430 return 0;
5079f326
Z
7431}
7432
d71bd34d 7433int tracing_set_clock(struct trace_array *tr, const char *clockstr)
5079f326 7434{
5079f326
Z
7435 int i;
7436
5079f326
Z
7437 for (i = 0; i < ARRAY_SIZE(trace_clocks); i++) {
7438 if (strcmp(trace_clocks[i].name, clockstr) == 0)
7439 break;
7440 }
7441 if (i == ARRAY_SIZE(trace_clocks))
7442 return -EINVAL;
7443
5079f326
Z
7444 mutex_lock(&trace_types_lock);
7445
2b6080f2
SR
7446 tr->clock_id = i;
7447
1c5eb448 7448 ring_buffer_set_clock(tr->array_buffer.buffer, trace_clocks[i].func);
5079f326 7449
60303ed3
DS
7450 /*
7451 * New clock may not be consistent with the previous clock.
7452 * Reset the buffer so that it doesn't have incomparable timestamps.
7453 */
1c5eb448 7454 tracing_reset_online_cpus(&tr->array_buffer);
12883efb
SRRH
7455
7456#ifdef CONFIG_TRACER_MAX_TRACE
170b3b10 7457 if (tr->max_buffer.buffer)
12883efb 7458 ring_buffer_set_clock(tr->max_buffer.buffer, trace_clocks[i].func);
9457158b 7459 tracing_reset_online_cpus(&tr->max_buffer);
12883efb 7460#endif
60303ed3 7461
2632a201
MHG
7462 if (tr->scratch && !(tr->flags & TRACE_ARRAY_FL_LAST_BOOT)) {
7463 struct trace_scratch *tscratch = tr->scratch;
7464
7465 tscratch->clock_id = i;
7466 }
7467
5079f326
Z
7468 mutex_unlock(&trace_types_lock);
7469
e1e232ca
SR
7470 return 0;
7471}
7472
7473static ssize_t tracing_clock_write(struct file *filp, const char __user *ubuf,
7474 size_t cnt, loff_t *fpos)
7475{
7476 struct seq_file *m = filp->private_data;
7477 struct trace_array *tr = m->private;
7478 char buf[64];
7479 const char *clockstr;
7480 int ret;
7481
7482 if (cnt >= sizeof(buf))
7483 return -EINVAL;
7484
4afe6495 7485 if (copy_from_user(buf, ubuf, cnt))
e1e232ca
SR
7486 return -EFAULT;
7487
7488 buf[cnt] = 0;
7489
7490 clockstr = strstrip(buf);
7491
7492 ret = tracing_set_clock(tr, clockstr);
7493 if (ret)
7494 return ret;
7495
5079f326
Z
7496 *fpos += cnt;
7497
7498 return cnt;
7499}
7500
13f16d20
LZ
7501static int tracing_clock_open(struct inode *inode, struct file *file)
7502{
7b85af63
SRRH
7503 struct trace_array *tr = inode->i_private;
7504 int ret;
7505
8530dec6
SRV
7506 ret = tracing_check_open_get_tr(tr);
7507 if (ret)
7508 return ret;
7b85af63
SRRH
7509
7510 ret = single_open(file, tracing_clock_show, inode->i_private);
7511 if (ret < 0)
7512 trace_array_put(tr);
7513
7514 return ret;
13f16d20
LZ
7515}
7516
2c1ea60b
TZ
7517static int tracing_time_stamp_mode_show(struct seq_file *m, void *v)
7518{
7519 struct trace_array *tr = m->private;
7520
7521 mutex_lock(&trace_types_lock);
7522
1c5eb448 7523 if (ring_buffer_time_stamp_abs(tr->array_buffer.buffer))
2c1ea60b
TZ
7524 seq_puts(m, "delta [absolute]\n");
7525 else
7526 seq_puts(m, "[delta] absolute\n");
7527
7528 mutex_unlock(&trace_types_lock);
7529
7530 return 0;
7531}
7532
7533static int tracing_time_stamp_mode_open(struct inode *inode, struct file *file)
7534{
7535 struct trace_array *tr = inode->i_private;
7536 int ret;
7537
8530dec6
SRV
7538 ret = tracing_check_open_get_tr(tr);
7539 if (ret)
7540 return ret;
2c1ea60b
TZ
7541
7542 ret = single_open(file, tracing_time_stamp_mode_show, inode->i_private);
7543 if (ret < 0)
7544 trace_array_put(tr);
7545
7546 return ret;
7547}
7548
d8279bfc
SRV
7549u64 tracing_event_time_stamp(struct trace_buffer *buffer, struct ring_buffer_event *rbe)
7550{
7551 if (rbe == this_cpu_read(trace_buffered_event))
f3ef7202 7552 return ring_buffer_time_stamp(buffer);
d8279bfc
SRV
7553
7554 return ring_buffer_event_time_stamp(buffer, rbe);
7555}
7556
b94bc80d
SRV
7557/*
7558 * Set or disable using the per CPU trace_buffer_event when possible.
7559 */
7560int tracing_set_filter_buffering(struct trace_array *tr, bool set)
00b41452 7561{
d33b10c0 7562 guard(mutex)(&trace_types_lock);
00b41452 7563
b94bc80d 7564 if (set && tr->no_filter_buffering_ref++)
d33b10c0 7565 return 0;
00b41452 7566
b94bc80d 7567 if (!set) {
d33b10c0
SR
7568 if (WARN_ON_ONCE(!tr->no_filter_buffering_ref))
7569 return -EINVAL;
00b41452 7570
b94bc80d 7571 --tr->no_filter_buffering_ref;
00b41452 7572 }
00b41452 7573
d33b10c0 7574 return 0;
00b41452
TZ
7575}
7576
6de58e62
SRRH
7577struct ftrace_buffer_info {
7578 struct trace_iterator iter;
7579 void *spare;
73a757e6 7580 unsigned int spare_cpu;
4e958db3 7581 unsigned int spare_size;
6de58e62
SRRH
7582 unsigned int read;
7583};
7584
debdd57f
HT
7585#ifdef CONFIG_TRACER_SNAPSHOT
7586static int tracing_snapshot_open(struct inode *inode, struct file *file)
7587{
6484c71c 7588 struct trace_array *tr = inode->i_private;
debdd57f 7589 struct trace_iterator *iter;
2b6080f2 7590 struct seq_file *m;
8530dec6 7591 int ret;
debdd57f 7592
8530dec6
SRV
7593 ret = tracing_check_open_get_tr(tr);
7594 if (ret)
7595 return ret;
ff451961 7596
debdd57f 7597 if (file->f_mode & FMODE_READ) {
6484c71c 7598 iter = __tracing_open(inode, file, true);
debdd57f
HT
7599 if (IS_ERR(iter))
7600 ret = PTR_ERR(iter);
2b6080f2
SR
7601 } else {
7602 /* Writes still need the seq_file to hold the private data */
f77d09a3 7603 ret = -ENOMEM;
2b6080f2
SR
7604 m = kzalloc(sizeof(*m), GFP_KERNEL);
7605 if (!m)
f77d09a3 7606 goto out;
2b6080f2
SR
7607 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
7608 if (!iter) {
7609 kfree(m);
f77d09a3 7610 goto out;
2b6080f2 7611 }
f77d09a3
AL
7612 ret = 0;
7613
ff451961 7614 iter->tr = tr;
1c5eb448 7615 iter->array_buffer = &tr->max_buffer;
6484c71c 7616 iter->cpu_file = tracing_get_cpu(inode);
2b6080f2
SR
7617 m->private = iter;
7618 file->private_data = m;
debdd57f 7619 }
f77d09a3 7620out:
ff451961
SRRH
7621 if (ret < 0)
7622 trace_array_put(tr);
7623
debdd57f
HT
7624 return ret;
7625}
7626
3163f635
ZY
7627static void tracing_swap_cpu_buffer(void *tr)
7628{
7629 update_max_tr_single((struct trace_array *)tr, current, smp_processor_id());
7630}
7631
debdd57f
HT
7632static ssize_t
7633tracing_snapshot_write(struct file *filp, const char __user *ubuf, size_t cnt,
7634 loff_t *ppos)
7635{
2b6080f2
SR
7636 struct seq_file *m = filp->private_data;
7637 struct trace_iterator *iter = m->private;
7638 struct trace_array *tr = iter->tr;
debdd57f
HT
7639 unsigned long val;
7640 int ret;
7641
a1f157c7 7642 ret = tracing_update_buffers(tr);
debdd57f
HT
7643 if (ret < 0)
7644 return ret;
7645
7646 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
7647 if (ret)
7648 return ret;
7649
d33b10c0 7650 guard(mutex)(&trace_types_lock);
debdd57f 7651
d33b10c0
SR
7652 if (tr->current_trace->use_max_tr)
7653 return -EBUSY;
debdd57f 7654
c0a581d7 7655 local_irq_disable();
a35873a0
TZ
7656 arch_spin_lock(&tr->max_lock);
7657 if (tr->cond_snapshot)
7658 ret = -EBUSY;
7659 arch_spin_unlock(&tr->max_lock);
c0a581d7 7660 local_irq_enable();
a35873a0 7661 if (ret)
d33b10c0 7662 return ret;
a35873a0 7663
debdd57f
HT
7664 switch (val) {
7665 case 0:
d33b10c0
SR
7666 if (iter->cpu_file != RING_BUFFER_ALL_CPUS)
7667 return -EINVAL;
3209cff4
SRRH
7668 if (tr->allocated_snapshot)
7669 free_snapshot(tr);
debdd57f
HT
7670 break;
7671 case 1:
f1affcaa
SRRH
7672/* Only allow per-cpu swap if the ring buffer supports it */
7673#ifndef CONFIG_RING_BUFFER_ALLOW_SWAP
d33b10c0
SR
7674 if (iter->cpu_file != RING_BUFFER_ALL_CPUS)
7675 return -EINVAL;
f1affcaa 7676#endif
46cc0b44
ET
7677 if (tr->allocated_snapshot)
7678 ret = resize_buffer_duplicate_size(&tr->max_buffer,
1c5eb448 7679 &tr->array_buffer, iter->cpu_file);
180e4e39
VD
7680
7681 ret = tracing_arm_snapshot_locked(tr);
7682 if (ret)
d33b10c0 7683 return ret;
180e4e39 7684
debdd57f 7685 /* Now, we're going to swap */
3163f635
ZY
7686 if (iter->cpu_file == RING_BUFFER_ALL_CPUS) {
7687 local_irq_disable();
a35873a0 7688 update_max_tr(tr, current, smp_processor_id(), NULL);
3163f635
ZY
7689 local_irq_enable();
7690 } else {
7691 smp_call_function_single(iter->cpu_file, tracing_swap_cpu_buffer,
7692 (void *)tr, 1);
7693 }
180e4e39 7694 tracing_disarm_snapshot(tr);
debdd57f
HT
7695 break;
7696 default:
45ad21ca 7697 if (tr->allocated_snapshot) {
f1affcaa
SRRH
7698 if (iter->cpu_file == RING_BUFFER_ALL_CPUS)
7699 tracing_reset_online_cpus(&tr->max_buffer);
7700 else
a47b53e9 7701 tracing_reset_cpu(&tr->max_buffer, iter->cpu_file);
f1affcaa 7702 }
debdd57f
HT
7703 break;
7704 }
7705
7706 if (ret >= 0) {
7707 *ppos += cnt;
7708 ret = cnt;
7709 }
d33b10c0 7710
debdd57f
HT
7711 return ret;
7712}
2b6080f2
SR
7713
7714static int tracing_snapshot_release(struct inode *inode, struct file *file)
7715{
7716 struct seq_file *m = file->private_data;
ff451961
SRRH
7717 int ret;
7718
7719 ret = tracing_release(inode, file);
2b6080f2
SR
7720
7721 if (file->f_mode & FMODE_READ)
ff451961 7722 return ret;
2b6080f2
SR
7723
7724 /* If write only, the seq_file is just a stub */
7725 if (m)
7726 kfree(m->private);
7727 kfree(m);
7728
7729 return 0;
7730}
7731
6de58e62
SRRH
7732static int tracing_buffers_open(struct inode *inode, struct file *filp);
7733static ssize_t tracing_buffers_read(struct file *filp, char __user *ubuf,
7734 size_t count, loff_t *ppos);
7735static int tracing_buffers_release(struct inode *inode, struct file *file);
7736static ssize_t tracing_buffers_splice_read(struct file *file, loff_t *ppos,
7737 struct pipe_inode_info *pipe, size_t len, unsigned int flags);
7738
7739static int snapshot_raw_open(struct inode *inode, struct file *filp)
7740{
7741 struct ftrace_buffer_info *info;
7742 int ret;
7743
17911ff3 7744 /* The following checks for tracefs lockdown */
6de58e62
SRRH
7745 ret = tracing_buffers_open(inode, filp);
7746 if (ret < 0)
7747 return ret;
7748
7749 info = filp->private_data;
7750
7751 if (info->iter.trace->use_max_tr) {
7752 tracing_buffers_release(inode, filp);
7753 return -EBUSY;
7754 }
7755
7756 info->iter.snapshot = true;
1c5eb448 7757 info->iter.array_buffer = &info->iter.tr->max_buffer;
6de58e62
SRRH
7758
7759 return ret;
7760}
7761
debdd57f
HT
7762#endif /* CONFIG_TRACER_SNAPSHOT */
7763
7764
6508fa76
SF
7765static 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,
7770};
7771
e25e43a4 7772#ifdef CONFIG_TRACER_MAX_TRACE
5e2336a0 7773static const struct file_operations tracing_max_lat_fops = {
7d660c9b 7774 .open = tracing_open_generic_tr,
4bf39a94
IM
7775 .read = tracing_max_lat_read,
7776 .write = tracing_max_lat_write,
b444786f 7777 .llseek = generic_file_llseek,
7d660c9b 7778 .release = tracing_release_generic_tr,
bc0c38d1 7779};
e428abbb 7780#endif
bc0c38d1 7781
5e2336a0 7782static const struct file_operations set_tracer_fops = {
9b37febc 7783 .open = tracing_open_generic_tr,
4bf39a94
IM
7784 .read = tracing_set_trace_read,
7785 .write = tracing_set_trace_write,
b444786f 7786 .llseek = generic_file_llseek,
9b37febc 7787 .release = tracing_release_generic_tr,
bc0c38d1
SR
7788};
7789
5e2336a0 7790static const struct file_operations tracing_pipe_fops = {
4bf39a94 7791 .open = tracing_open_pipe,
2a2cc8f7 7792 .poll = tracing_poll_pipe,
4bf39a94 7793 .read = tracing_read_pipe,
3c56819b 7794 .splice_read = tracing_splice_read_pipe,
4bf39a94 7795 .release = tracing_release_pipe,
b3806b43
SR
7796};
7797
5e2336a0 7798static const struct file_operations tracing_entries_fops = {
0bc392ee 7799 .open = tracing_open_generic_tr,
a98a3c3f
SR
7800 .read = tracing_entries_read,
7801 .write = tracing_entries_write,
b444786f 7802 .llseek = generic_file_llseek,
0bc392ee 7803 .release = tracing_release_generic_tr,
a98a3c3f
SR
7804};
7805
950032ff
SRG
7806static const struct file_operations tracing_buffer_meta_fops = {
7807 .open = tracing_buffer_meta_open,
7808 .read = seq_read,
7809 .llseek = seq_lseek,
7810 .release = tracing_seq_release,
7811};
7812
f81ab074 7813static const struct file_operations tracing_total_entries_fops = {
7b85af63 7814 .open = tracing_open_generic_tr,
f81ab074
VN
7815 .read = tracing_total_entries_read,
7816 .llseek = generic_file_llseek,
7b85af63 7817 .release = tracing_release_generic_tr,
f81ab074
VN
7818};
7819
4f271a2a 7820static const struct file_operations tracing_free_buffer_fops = {
7b85af63 7821 .open = tracing_open_generic_tr,
4f271a2a
VN
7822 .write = tracing_free_buffer_write,
7823 .release = tracing_free_buffer_release,
7824};
7825
5e2336a0 7826static const struct file_operations tracing_mark_fops = {
2972e305 7827 .open = tracing_mark_open,
5bf9a1ee 7828 .write = tracing_mark_write,
7b85af63 7829 .release = tracing_release_generic_tr,
5bf9a1ee
PP
7830};
7831
fa32e855 7832static const struct file_operations tracing_mark_raw_fops = {
2972e305 7833 .open = tracing_mark_open,
fa32e855 7834 .write = tracing_mark_raw_write,
fa32e855
SR
7835 .release = tracing_release_generic_tr,
7836};
7837
5079f326 7838static const struct file_operations trace_clock_fops = {
13f16d20
LZ
7839 .open = tracing_clock_open,
7840 .read = seq_read,
7841 .llseek = seq_lseek,
7b85af63 7842 .release = tracing_single_release_tr,
5079f326
Z
7843 .write = tracing_clock_write,
7844};
7845
2c1ea60b
TZ
7846static const struct file_operations trace_time_stamp_mode_fops = {
7847 .open = tracing_time_stamp_mode_open,
7848 .read = seq_read,
7849 .llseek = seq_lseek,
7850 .release = tracing_single_release_tr,
7851};
7852
7a1d1e4b 7853static const struct file_operations last_boot_fops = {
1bd25a6f
SR
7854 .open = tracing_last_boot_open,
7855 .read = seq_read,
7856 .llseek = seq_lseek,
7857 .release = tracing_seq_release,
7a1d1e4b
SRG
7858};
7859
debdd57f
HT
7860#ifdef CONFIG_TRACER_SNAPSHOT
7861static const struct file_operations snapshot_fops = {
7862 .open = tracing_snapshot_open,
7863 .read = seq_read,
7864 .write = tracing_snapshot_write,
098c879e 7865 .llseek = tracing_lseek,
2b6080f2 7866 .release = tracing_snapshot_release,
debdd57f 7867};
debdd57f 7868
6de58e62
SRRH
7869static 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,
2cadf913
SR
7874};
7875
6de58e62
SRRH
7876#endif /* CONFIG_TRACER_SNAPSHOT */
7877
bc87cf0a
DBO
7878/*
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
7884 *
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.
7889 */
7890static ssize_t
7891trace_min_max_write(struct file *filp, const char __user *ubuf, size_t cnt, loff_t *ppos)
7892{
7893 struct trace_min_max_param *param = filp->private_data;
7894 u64 val;
7895 int err;
7896
7897 if (!param)
7898 return -EFAULT;
7899
7900 err = kstrtoull_from_user(ubuf, cnt, 10, &val);
7901 if (err)
7902 return err;
7903
7904 if (param->lock)
7905 mutex_lock(param->lock);
7906
7907 if (param->min && val < *param->min)
7908 err = -EINVAL;
7909
7910 if (param->max && val > *param->max)
7911 err = -EINVAL;
7912
7913 if (!err)
7914 *param->val = val;
7915
7916 if (param->lock)
7917 mutex_unlock(param->lock);
7918
7919 if (err)
7920 return err;
7921
7922 return cnt;
7923}
7924
7925/*
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
7931 *
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
7934 * data.
7935 */
7936static ssize_t
7937trace_min_max_read(struct file *filp, char __user *ubuf, size_t cnt, loff_t *ppos)
7938{
7939 struct trace_min_max_param *param = filp->private_data;
7940 char buf[U64_STR_SIZE];
7941 int len;
7942 u64 val;
7943
7944 if (!param)
7945 return -EFAULT;
7946
7947 val = *param->val;
7948
7949 if (cnt > sizeof(buf))
7950 cnt = sizeof(buf);
7951
7952 len = snprintf(buf, sizeof(buf), "%llu\n", val);
7953
7954 return simple_read_from_buffer(ubuf, cnt, ppos, buf, len);
7955}
7956
7957const struct file_operations trace_min_max_fops = {
7958 .open = tracing_open_generic,
7959 .read = trace_min_max_read,
7960 .write = trace_min_max_write,
7961};
7962
8a062902
TZ
7963#define TRACING_LOG_ERRS_MAX 8
7964#define TRACING_LOG_LOC_MAX 128
7965
7966#define CMD_PREFIX " Command: "
7967
7968struct err_info {
7969 const char **errs; /* ptr to loc-specific array of err strings */
7970 u8 type; /* index into errs -> specific err string */
1581a884 7971 u16 pos; /* caret position */
8a062902
TZ
7972 u64 ts;
7973};
7974
7975struct tracing_log_err {
7976 struct list_head list;
7977 struct err_info info;
7978 char loc[TRACING_LOG_LOC_MAX]; /* err location */
1581a884 7979 char *cmd; /* what caused err */
8a062902
TZ
7980};
7981
8a062902
TZ
7982static DEFINE_MUTEX(tracing_err_log_lock);
7983
1581a884
TZ
7984static struct tracing_log_err *alloc_tracing_log_err(int len)
7985{
7986 struct tracing_log_err *err;
7987
7988 err = kzalloc(sizeof(*err), GFP_KERNEL);
7989 if (!err)
7990 return ERR_PTR(-ENOMEM);
7991
7992 err->cmd = kzalloc(len, GFP_KERNEL);
7993 if (!err->cmd) {
7994 kfree(err);
7995 return ERR_PTR(-ENOMEM);
7996 }
7997
7998 return err;
7999}
8000
8001static void free_tracing_log_err(struct tracing_log_err *err)
8002{
8003 kfree(err->cmd);
8004 kfree(err);
8005}
8006
8007static struct tracing_log_err *get_tracing_log_err(struct trace_array *tr,
8008 int len)
8a062902
TZ
8009{
8010 struct tracing_log_err *err;
067df9e0 8011 char *cmd;
8a062902 8012
2f754e77 8013 if (tr->n_err_log_entries < TRACING_LOG_ERRS_MAX) {
1581a884
TZ
8014 err = alloc_tracing_log_err(len);
8015 if (PTR_ERR(err) != -ENOMEM)
67ab5eb7 8016 tr->n_err_log_entries++;
8a062902
TZ
8017
8018 return err;
8019 }
067df9e0
ZY
8020 cmd = kzalloc(len, GFP_KERNEL);
8021 if (!cmd)
8022 return ERR_PTR(-ENOMEM);
2f754e77 8023 err = list_first_entry(&tr->err_log, struct tracing_log_err, list);
1581a884 8024 kfree(err->cmd);
067df9e0 8025 err->cmd = cmd;
8a062902
TZ
8026 list_del(&err->list);
8027
8028 return err;
8029}
8030
8031/**
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
8035 *
f2cc020d 8036 * Finds the position of the first occurrence of @str within @cmd. The
8a062902
TZ
8037 * return value can be passed to tracing_log_err() for caret placement
8038 * within @cmd.
8039 *
f2cc020d 8040 * Returns the index within @cmd of the first occurrence of @str or 0
8a062902
TZ
8041 * if @str was not found.
8042 */
8043unsigned int err_pos(char *cmd, const char *str)
8044{
8045 char *found;
8046
8047 if (WARN_ON(!strlen(cmd)))
8048 return 0;
8049
8050 found = strstr(cmd, str);
8051 if (found)
8052 return found - cmd;
8053
8054 return 0;
8055}
8056
8057/**
8058 * tracing_log_err - write an error to the tracing error log
2f754e77 8059 * @tr: The associated trace array for the error (NULL for top level array)
8a062902
TZ
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
8065 *
8066 * Writes an error into tracing/error_log of the form:
8067 *
8068 * <loc>: error: <text>
8069 * Command: <cmd>
8070 * ^
8071 *
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.
8077 *
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().
8083 */
2f754e77
SRV
8084void tracing_log_err(struct trace_array *tr,
8085 const char *loc, const char *cmd,
1581a884 8086 const char **errs, u8 type, u16 pos)
8a062902
TZ
8087{
8088 struct tracing_log_err *err;
1581a884 8089 int len = 0;
8a062902 8090
2f754e77
SRV
8091 if (!tr)
8092 tr = &global_trace;
8093
1581a884
TZ
8094 len += sizeof(CMD_PREFIX) + 2 * sizeof("\n") + strlen(cmd) + 1;
8095
d33b10c0
SR
8096 guard(mutex)(&tracing_err_log_lock);
8097
1581a884 8098 err = get_tracing_log_err(tr, len);
d33b10c0 8099 if (PTR_ERR(err) == -ENOMEM)
8a062902 8100 return;
8a062902
TZ
8101
8102 snprintf(err->loc, TRACING_LOG_LOC_MAX, "%s: error: ", loc);
1581a884 8103 snprintf(err->cmd, len, "\n" CMD_PREFIX "%s\n", cmd);
8a062902
TZ
8104
8105 err->info.errs = errs;
8106 err->info.type = type;
8107 err->info.pos = pos;
8108 err->info.ts = local_clock();
8109
2f754e77 8110 list_add_tail(&err->list, &tr->err_log);
8a062902
TZ
8111}
8112
2f754e77 8113static void clear_tracing_err_log(struct trace_array *tr)
8a062902
TZ
8114{
8115 struct tracing_log_err *err, *next;
8116
8117 mutex_lock(&tracing_err_log_lock);
2f754e77 8118 list_for_each_entry_safe(err, next, &tr->err_log, list) {
8a062902 8119 list_del(&err->list);
1581a884 8120 free_tracing_log_err(err);
8a062902
TZ
8121 }
8122
2f754e77 8123 tr->n_err_log_entries = 0;
8a062902
TZ
8124 mutex_unlock(&tracing_err_log_lock);
8125}
8126
8127static void *tracing_err_log_seq_start(struct seq_file *m, loff_t *pos)
8128{
2f754e77
SRV
8129 struct trace_array *tr = m->private;
8130
8a062902
TZ
8131 mutex_lock(&tracing_err_log_lock);
8132
2f754e77 8133 return seq_list_start(&tr->err_log, *pos);
8a062902
TZ
8134}
8135
8136static void *tracing_err_log_seq_next(struct seq_file *m, void *v, loff_t *pos)
8137{
2f754e77
SRV
8138 struct trace_array *tr = m->private;
8139
8140 return seq_list_next(v, &tr->err_log, pos);
8a062902
TZ
8141}
8142
8143static void tracing_err_log_seq_stop(struct seq_file *m, void *v)
8144{
8145 mutex_unlock(&tracing_err_log_lock);
8146}
8147
1581a884 8148static void tracing_err_log_show_pos(struct seq_file *m, u16 pos)
8a062902 8149{
1581a884 8150 u16 i;
8a062902
TZ
8151
8152 for (i = 0; i < sizeof(CMD_PREFIX) - 1; i++)
8153 seq_putc(m, ' ');
8154 for (i = 0; i < pos; i++)
8155 seq_putc(m, ' ');
8156 seq_puts(m, "^\n");
8157}
8158
8159static int tracing_err_log_seq_show(struct seq_file *m, void *v)
8160{
8161 struct tracing_log_err *err = v;
8162
8163 if (err) {
8164 const char *err_text = err->info.errs[err->info.type];
8165 u64 sec = err->info.ts;
8166 u32 nsec;
8167
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);
8173 }
8174
8175 return 0;
8176}
8177
8178static 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
8183};
8184
8185static int tracing_err_log_open(struct inode *inode, struct file *file)
8186{
2f754e77 8187 struct trace_array *tr = inode->i_private;
8a062902
TZ
8188 int ret = 0;
8189
8530dec6
SRV
8190 ret = tracing_check_open_get_tr(tr);
8191 if (ret)
8192 return ret;
2f754e77 8193
8a062902
TZ
8194 /* If this file was opened for write, then erase contents */
8195 if ((file->f_mode & FMODE_WRITE) && (file->f_flags & O_TRUNC))
2f754e77 8196 clear_tracing_err_log(tr);
8a062902 8197
2f754e77 8198 if (file->f_mode & FMODE_READ) {
8a062902 8199 ret = seq_open(file, &tracing_err_log_seq_ops);
2f754e77
SRV
8200 if (!ret) {
8201 struct seq_file *m = file->private_data;
8202 m->private = tr;
8203 } else {
8204 trace_array_put(tr);
8205 }
8206 }
8a062902
TZ
8207 return ret;
8208}
8209
8210static ssize_t tracing_err_log_write(struct file *file,
8211 const char __user *buffer,
8212 size_t count, loff_t *ppos)
8213{
8214 return count;
8215}
8216
d122ed62
TM
8217static int tracing_err_log_release(struct inode *inode, struct file *file)
8218{
8219 struct trace_array *tr = inode->i_private;
8220
8221 trace_array_put(tr);
8222
8223 if (file->f_mode & FMODE_READ)
8224 seq_release(inode, file);
8225
8226 return 0;
8227}
8228
8a062902
TZ
8229static const struct file_operations tracing_err_log_fops = {
8230 .open = tracing_err_log_open,
8231 .write = tracing_err_log_write,
8232 .read = seq_read,
02b0095e 8233 .llseek = tracing_lseek,
d122ed62 8234 .release = tracing_err_log_release,
8a062902
TZ
8235};
8236
2cadf913
SR
8237static int tracing_buffers_open(struct inode *inode, struct file *filp)
8238{
46ef2be0 8239 struct trace_array *tr = inode->i_private;
2cadf913 8240 struct ftrace_buffer_info *info;
7b85af63 8241 int ret;
2cadf913 8242
8530dec6
SRV
8243 ret = tracing_check_open_get_tr(tr);
8244 if (ret)
8245 return ret;
7b85af63 8246
0f69dae4 8247 info = kvzalloc(sizeof(*info), GFP_KERNEL);
7b85af63
SRRH
8248 if (!info) {
8249 trace_array_put(tr);
2cadf913 8250 return -ENOMEM;
7b85af63 8251 }
2cadf913 8252
a695cb58
SRRH
8253 mutex_lock(&trace_types_lock);
8254
cc60cdc9 8255 info->iter.tr = tr;
46ef2be0 8256 info->iter.cpu_file = tracing_get_cpu(inode);
b627344f 8257 info->iter.trace = tr->current_trace;
1c5eb448 8258 info->iter.array_buffer = &tr->array_buffer;
cc60cdc9 8259 info->spare = NULL;
2cadf913 8260 /* Force reading ring buffer for first read */
cc60cdc9 8261 info->read = (unsigned int)-1;
2cadf913
SR
8262
8263 filp->private_data = info;
8264
7ef282e0 8265 tr->trace_ref++;
cf6ab6d9 8266
a695cb58
SRRH
8267 mutex_unlock(&trace_types_lock);
8268
7b85af63
SRRH
8269 ret = nonseekable_open(inode, filp);
8270 if (ret < 0)
8271 trace_array_put(tr);
8272
8273 return ret;
2cadf913
SR
8274}
8275
9dd95748 8276static __poll_t
cc60cdc9
SR
8277tracing_buffers_poll(struct file *filp, poll_table *poll_table)
8278{
8279 struct ftrace_buffer_info *info = filp->private_data;
8280 struct trace_iterator *iter = &info->iter;
8281
8282 return trace_poll(iter, filp, poll_table);
8283}
8284
2cadf913
SR
8285static ssize_t
8286tracing_buffers_read(struct file *filp, char __user *ubuf,
8287 size_t count, loff_t *ppos)
8288{
8289 struct ftrace_buffer_info *info = filp->private_data;
cc60cdc9 8290 struct trace_iterator *iter = &info->iter;
bce761d7
TSV
8291 void *trace_data;
8292 int page_size;
a7e52ad7 8293 ssize_t ret = 0;
6de58e62 8294 ssize_t size;
2cadf913 8295
2dc5d12b
SR
8296 if (!count)
8297 return 0;
8298
6de58e62 8299#ifdef CONFIG_TRACER_MAX_TRACE
d716ff71
SRRH
8300 if (iter->snapshot && iter->tr->current_trace->use_max_tr)
8301 return -EBUSY;
6de58e62
SRRH
8302#endif
8303
bce761d7
TSV
8304 page_size = ring_buffer_subbuf_size_get(iter->array_buffer->buffer);
8305
4e958db3
SRG
8306 /* Make sure the spare matches the current sub buffer size */
8307 if (info->spare) {
8308 if (page_size != info->spare_size) {
8309 ring_buffer_free_read_page(iter->array_buffer->buffer,
8310 info->spare_cpu, info->spare);
8311 info->spare = NULL;
8312 }
8313 }
8314
73a757e6 8315 if (!info->spare) {
1c5eb448 8316 info->spare = ring_buffer_alloc_read_page(iter->array_buffer->buffer,
12883efb 8317 iter->cpu_file);
a7e52ad7
SRV
8318 if (IS_ERR(info->spare)) {
8319 ret = PTR_ERR(info->spare);
8320 info->spare = NULL;
8321 } else {
8322 info->spare_cpu = iter->cpu_file;
4e958db3 8323 info->spare_size = page_size;
a7e52ad7 8324 }
73a757e6 8325 }
ddd538f3 8326 if (!info->spare)
a7e52ad7 8327 return ret;
ddd538f3 8328
2cadf913 8329 /* Do we have previous read data to read? */
bce761d7 8330 if (info->read < page_size)
2cadf913
SR
8331 goto read;
8332
b627344f 8333 again:
cc60cdc9 8334 trace_access_lock(iter->cpu_file);
1c5eb448 8335 ret = ring_buffer_read_page(iter->array_buffer->buffer,
bce761d7 8336 info->spare,
2cadf913 8337 count,
cc60cdc9
SR
8338 iter->cpu_file, 0);
8339 trace_access_unlock(iter->cpu_file);
2cadf913 8340
b627344f 8341 if (ret < 0) {
d0949cd4 8342 if (trace_empty(iter) && !iter->closed) {
32dc0042
MHG
8343 if (update_last_data_if_empty(iter->tr))
8344 return 0;
8345
d716ff71
SRRH
8346 if ((filp->f_flags & O_NONBLOCK))
8347 return -EAGAIN;
8348
2c2b0a78 8349 ret = wait_on_pipe(iter, 0);
d716ff71
SRRH
8350 if (ret)
8351 return ret;
8352
b627344f
SR
8353 goto again;
8354 }
d716ff71 8355 return 0;
b627344f 8356 }
436fc280 8357
436fc280 8358 info->read = 0;
b627344f 8359 read:
bce761d7 8360 size = page_size - info->read;
2cadf913
SR
8361 if (size > count)
8362 size = count;
bce761d7
TSV
8363 trace_data = ring_buffer_read_page_data(info->spare);
8364 ret = copy_to_user(ubuf, trace_data + info->read, size);
d716ff71
SRRH
8365 if (ret == size)
8366 return -EFAULT;
8367
2dc5d12b
SR
8368 size -= ret;
8369
2cadf913
SR
8370 *ppos += size;
8371 info->read += size;
8372
8373 return size;
8374}
8375
e5d7c191
SRG
8376static int tracing_buffers_flush(struct file *file, fl_owner_t id)
8377{
8378 struct ftrace_buffer_info *info = file->private_data;
8379 struct trace_iterator *iter = &info->iter;
8380
2aa043a5 8381 iter->closed = true;
e5d7c191 8382 /* Make sure the waiters see the new wait_index */
2aa043a5 8383 (void)atomic_fetch_inc_release(&iter->wait_index);
e5d7c191
SRG
8384
8385 ring_buffer_wake_waiters(iter->array_buffer->buffer, iter->cpu_file);
8386
8387 return 0;
8388}
8389
2cadf913
SR
8390static int tracing_buffers_release(struct inode *inode, struct file *file)
8391{
8392 struct ftrace_buffer_info *info = file->private_data;
cc60cdc9 8393 struct trace_iterator *iter = &info->iter;
2cadf913 8394
a695cb58
SRRH
8395 mutex_lock(&trace_types_lock);
8396
7ef282e0 8397 iter->tr->trace_ref--;
cf6ab6d9 8398
ff451961 8399 __trace_array_put(iter->tr);
2cadf913 8400
ddd538f3 8401 if (info->spare)
1c5eb448 8402 ring_buffer_free_read_page(iter->array_buffer->buffer,
73a757e6 8403 info->spare_cpu, info->spare);
0f69dae4 8404 kvfree(info);
2cadf913 8405
a695cb58
SRRH
8406 mutex_unlock(&trace_types_lock);
8407
2cadf913
SR
8408 return 0;
8409}
8410
8411struct buffer_ref {
13292494 8412 struct trace_buffer *buffer;
2cadf913 8413 void *page;
73a757e6 8414 int cpu;
b9872226 8415 refcount_t refcount;
2cadf913
SR
8416};
8417
b9872226
JH
8418static void buffer_ref_release(struct buffer_ref *ref)
8419{
8420 if (!refcount_dec_and_test(&ref->refcount))
8421 return;
8422 ring_buffer_free_read_page(ref->buffer, ref->cpu, ref->page);
8423 kfree(ref);
8424}
8425
2cadf913
SR
8426static void buffer_pipe_buf_release(struct pipe_inode_info *pipe,
8427 struct pipe_buffer *buf)
8428{
8429 struct buffer_ref *ref = (struct buffer_ref *)buf->private;
8430
b9872226 8431 buffer_ref_release(ref);
2cadf913
SR
8432 buf->private = 0;
8433}
8434
15fab63e 8435static bool buffer_pipe_buf_get(struct pipe_inode_info *pipe,
2cadf913
SR
8436 struct pipe_buffer *buf)
8437{
8438 struct buffer_ref *ref = (struct buffer_ref *)buf->private;
8439
e9e1a2e7 8440 if (refcount_read(&ref->refcount) > INT_MAX/2)
15fab63e
MW
8441 return false;
8442
b9872226 8443 refcount_inc(&ref->refcount);
15fab63e 8444 return true;
2cadf913
SR
8445}
8446
8447/* Pipe buffer operations for a buffer. */
28dfef8f 8448static const struct pipe_buf_operations buffer_pipe_buf_ops = {
2cadf913 8449 .release = buffer_pipe_buf_release,
2cadf913
SR
8450 .get = buffer_pipe_buf_get,
8451};
8452
8453/*
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.
8456 */
8457static void buffer_spd_release(struct splice_pipe_desc *spd, unsigned int i)
8458{
8459 struct buffer_ref *ref =
8460 (struct buffer_ref *)spd->partial[i].private;
8461
b9872226 8462 buffer_ref_release(ref);
2cadf913
SR
8463 spd->partial[i].private = 0;
8464}
8465
8466static ssize_t
8467tracing_buffers_splice_read(struct file *file, loff_t *ppos,
8468 struct pipe_inode_info *pipe, size_t len,
8469 unsigned int flags)
8470{
8471 struct ftrace_buffer_info *info = file->private_data;
cc60cdc9 8472 struct trace_iterator *iter = &info->iter;
35f3d14d
JA
8473 struct partial_page partial_def[PIPE_DEF_BUFFERS];
8474 struct page *pages_def[PIPE_DEF_BUFFERS];
2cadf913 8475 struct splice_pipe_desc spd = {
35f3d14d
JA
8476 .pages = pages_def,
8477 .partial = partial_def,
047fe360 8478 .nr_pages_max = PIPE_DEF_BUFFERS,
2cadf913
SR
8479 .ops = &buffer_pipe_buf_ops,
8480 .spd_release = buffer_spd_release,
8481 };
8482 struct buffer_ref *ref;
2aa043a5 8483 bool woken = false;
bce761d7 8484 int page_size;
6b7e633f 8485 int entries, i;
07906da7 8486 ssize_t ret = 0;
2cadf913 8487
6de58e62 8488#ifdef CONFIG_TRACER_MAX_TRACE
d716ff71
SRRH
8489 if (iter->snapshot && iter->tr->current_trace->use_max_tr)
8490 return -EBUSY;
6de58e62
SRRH
8491#endif
8492
bce761d7
TSV
8493 page_size = ring_buffer_subbuf_size_get(iter->array_buffer->buffer);
8494 if (*ppos & (page_size - 1))
d716ff71 8495 return -EINVAL;
93cfb3c9 8496
bce761d7
TSV
8497 if (len & (page_size - 1)) {
8498 if (len < page_size)
d716ff71 8499 return -EINVAL;
bce761d7 8500 len &= (~(page_size - 1));
93cfb3c9
LJ
8501 }
8502
1ae2293d
AV
8503 if (splice_grow_spd(pipe, &spd))
8504 return -ENOMEM;
8505
cc60cdc9
SR
8506 again:
8507 trace_access_lock(iter->cpu_file);
1c5eb448 8508 entries = ring_buffer_entries_cpu(iter->array_buffer->buffer, iter->cpu_file);
93459c6c 8509
bce761d7 8510 for (i = 0; i < spd.nr_pages_max && len && entries; i++, len -= page_size) {
2cadf913
SR
8511 struct page *page;
8512 int r;
8513
8514 ref = kzalloc(sizeof(*ref), GFP_KERNEL);
07906da7
RV
8515 if (!ref) {
8516 ret = -ENOMEM;
2cadf913 8517 break;
07906da7 8518 }
2cadf913 8519
b9872226 8520 refcount_set(&ref->refcount, 1);
1c5eb448 8521 ref->buffer = iter->array_buffer->buffer;
cc60cdc9 8522 ref->page = ring_buffer_alloc_read_page(ref->buffer, iter->cpu_file);
a7e52ad7
SRV
8523 if (IS_ERR(ref->page)) {
8524 ret = PTR_ERR(ref->page);
8525 ref->page = NULL;
2cadf913
SR
8526 kfree(ref);
8527 break;
8528 }
73a757e6 8529 ref->cpu = iter->cpu_file;
2cadf913 8530
bce761d7 8531 r = ring_buffer_read_page(ref->buffer, ref->page,
cc60cdc9 8532 len, iter->cpu_file, 1);
2cadf913 8533 if (r < 0) {
73a757e6
SRV
8534 ring_buffer_free_read_page(ref->buffer, ref->cpu,
8535 ref->page);
2cadf913
SR
8536 kfree(ref);
8537 break;
8538 }
8539
bce761d7 8540 page = virt_to_page(ring_buffer_read_page_data(ref->page));
2cadf913
SR
8541
8542 spd.pages[i] = page;
bce761d7 8543 spd.partial[i].len = page_size;
2cadf913
SR
8544 spd.partial[i].offset = 0;
8545 spd.partial[i].private = (unsigned long)ref;
8546 spd.nr_pages++;
bce761d7 8547 *ppos += page_size;
93459c6c 8548
1c5eb448 8549 entries = ring_buffer_entries_cpu(iter->array_buffer->buffer, iter->cpu_file);
2cadf913
SR
8550 }
8551
cc60cdc9 8552 trace_access_unlock(iter->cpu_file);
2cadf913
SR
8553 spd.nr_pages = i;
8554
8555 /* did we read anything? */
8556 if (!spd.nr_pages) {
f3ddb74a 8557
07906da7 8558 if (ret)
1ae2293d 8559 goto out;
d716ff71 8560
2aa043a5
SRG
8561 if (woken)
8562 goto out;
8563
1ae2293d 8564 ret = -EAGAIN;
d716ff71 8565 if ((file->f_flags & O_NONBLOCK) || (flags & SPLICE_F_NONBLOCK))
1ae2293d 8566 goto out;
07906da7 8567
39a7dc23 8568 ret = wait_on_pipe(iter, iter->snapshot ? 0 : iter->tr->buffer_percent);
8b8b3683 8569 if (ret)
1ae2293d 8570 goto out;
e30f53aa 8571
2b0fd9a5
SRG
8572 /* No need to wait after waking up when tracing is off */
8573 if (!tracer_tracing_is_on(iter->tr))
8574 goto out;
8575
2aa043a5
SRG
8576 /* Iterate one more time to collect any new data then exit */
8577 woken = true;
f3ddb74a 8578
cc60cdc9 8579 goto again;
2cadf913
SR
8580 }
8581
8582 ret = splice_to_pipe(pipe, &spd);
1ae2293d 8583out:
047fe360 8584 splice_shrink_spd(&spd);
6de58e62 8585
2cadf913
SR
8586 return ret;
8587}
8588
01b2a521
SRG
8589static long tracing_buffers_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
8590{
8591 struct ftrace_buffer_info *info = file->private_data;
8592 struct trace_iterator *iter = &info->iter;
cf9f0f7c
VD
8593 int err;
8594
8595 if (cmd == TRACE_MMAP_IOCTL_GET_READER) {
8596 if (!(file->f_flags & O_NONBLOCK)) {
8597 err = ring_buffer_wait(iter->array_buffer->buffer,
8598 iter->cpu_file,
8599 iter->tr->buffer_percent,
8600 NULL, NULL);
8601 if (err)
8602 return err;
8603 }
01b2a521 8604
cf9f0f7c
VD
8605 return ring_buffer_map_get_reader(iter->array_buffer->buffer,
8606 iter->cpu_file);
8607 } else if (cmd) {
8608 return -ENOTTY;
8609 }
01b2a521 8610
cf9f0f7c
VD
8611 /*
8612 * An ioctl call with cmd 0 to the ring buffer file will wake up all
8613 * waiters
8614 */
01b2a521
SRG
8615 mutex_lock(&trace_types_lock);
8616
01b2a521 8617 /* Make sure the waiters see the new wait_index */
2aa043a5 8618 (void)atomic_fetch_inc_release(&iter->wait_index);
01b2a521
SRG
8619
8620 ring_buffer_wake_waiters(iter->array_buffer->buffer, iter->cpu_file);
8621
8622 mutex_unlock(&trace_types_lock);
8623 return 0;
8624}
8625
cf9f0f7c
VD
8626#ifdef CONFIG_TRACER_MAX_TRACE
8627static int get_snapshot_map(struct trace_array *tr)
8628{
8629 int err = 0;
8630
8631 /*
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.
8635 */
8636 spin_lock(&tr->snapshot_trigger_lock);
8637
8638 if (tr->snapshot || tr->mapped == UINT_MAX)
8639 err = -EBUSY;
8640 else
8641 tr->mapped++;
8642
8643 spin_unlock(&tr->snapshot_trigger_lock);
8644
8645 /* Wait for update_max_tr() to observe iter->tr->mapped */
8646 if (tr->mapped == 1)
8647 synchronize_rcu();
8648
8649 return err;
8650
8651}
8652static void put_snapshot_map(struct trace_array *tr)
8653{
8654 spin_lock(&tr->snapshot_trigger_lock);
8655 if (!WARN_ON(!tr->mapped))
8656 tr->mapped--;
8657 spin_unlock(&tr->snapshot_trigger_lock);
8658}
8659#else
8660static inline int get_snapshot_map(struct trace_array *tr) { return 0; }
8661static inline void put_snapshot_map(struct trace_array *tr) { }
8662#endif
8663
8664static void tracing_buffers_mmap_close(struct vm_area_struct *vma)
8665{
8666 struct ftrace_buffer_info *info = vma->vm_file->private_data;
8667 struct trace_iterator *iter = &info->iter;
8668
8669 WARN_ON(ring_buffer_unmap(iter->array_buffer->buffer, iter->cpu_file));
8670 put_snapshot_map(iter->tr);
8671}
8672
8673static const struct vm_operations_struct tracing_buffers_vmops = {
8674 .close = tracing_buffers_mmap_close,
8675};
8676
8677static int tracing_buffers_mmap(struct file *filp, struct vm_area_struct *vma)
8678{
8679 struct ftrace_buffer_info *info = filp->private_data;
8680 struct trace_iterator *iter = &info->iter;
8681 int ret = 0;
8682
34ea8fa0
SR
8683 /* A memmap'ed buffer is not supported for user space mmap */
8684 if (iter->tr->flags & TRACE_ARRAY_FL_MEMMAP)
8685 return -ENODEV;
8686
cf9f0f7c
VD
8687 ret = get_snapshot_map(iter->tr);
8688 if (ret)
8689 return ret;
8690
8691 ret = ring_buffer_map(iter->array_buffer->buffer, iter->cpu_file, vma);
8692 if (ret)
8693 put_snapshot_map(iter->tr);
8694
8695 vma->vm_ops = &tracing_buffers_vmops;
8696
8697 return ret;
8698}
8699
2cadf913
SR
8700static const struct file_operations tracing_buffers_fops = {
8701 .open = tracing_buffers_open,
8702 .read = tracing_buffers_read,
cc60cdc9 8703 .poll = tracing_buffers_poll,
2cadf913 8704 .release = tracing_buffers_release,
e5d7c191 8705 .flush = tracing_buffers_flush,
2cadf913 8706 .splice_read = tracing_buffers_splice_read,
01b2a521 8707 .unlocked_ioctl = tracing_buffers_ioctl,
cf9f0f7c 8708 .mmap = tracing_buffers_mmap,
2cadf913
SR
8709};
8710
c8d77183
SR
8711static ssize_t
8712tracing_stats_read(struct file *filp, char __user *ubuf,
8713 size_t count, loff_t *ppos)
8714{
4d3435b8
ON
8715 struct inode *inode = file_inode(filp);
8716 struct trace_array *tr = inode->i_private;
1c5eb448 8717 struct array_buffer *trace_buf = &tr->array_buffer;
4d3435b8 8718 int cpu = tracing_get_cpu(inode);
c8d77183
SR
8719 struct trace_seq *s;
8720 unsigned long cnt;
c64e148a
VN
8721 unsigned long long t;
8722 unsigned long usec_rem;
c8d77183 8723
e4f2d10f 8724 s = kmalloc(sizeof(*s), GFP_KERNEL);
c8d77183 8725 if (!s)
a646365c 8726 return -ENOMEM;
c8d77183
SR
8727
8728 trace_seq_init(s);
8729
12883efb 8730 cnt = ring_buffer_entries_cpu(trace_buf->buffer, cpu);
c8d77183
SR
8731 trace_seq_printf(s, "entries: %ld\n", cnt);
8732
12883efb 8733 cnt = ring_buffer_overrun_cpu(trace_buf->buffer, cpu);
c8d77183
SR
8734 trace_seq_printf(s, "overrun: %ld\n", cnt);
8735
12883efb 8736 cnt = ring_buffer_commit_overrun_cpu(trace_buf->buffer, cpu);
c8d77183
SR
8737 trace_seq_printf(s, "commit overrun: %ld\n", cnt);
8738
12883efb 8739 cnt = ring_buffer_bytes_cpu(trace_buf->buffer, cpu);
c64e148a
VN
8740 trace_seq_printf(s, "bytes: %ld\n", cnt);
8741
58e8eedf 8742 if (trace_clocks[tr->clock_id].in_ns) {
11043d8b 8743 /* local or global for trace_clock */
12883efb 8744 t = ns2usecs(ring_buffer_oldest_event_ts(trace_buf->buffer, cpu));
11043d8b
YY
8745 usec_rem = do_div(t, USEC_PER_SEC);
8746 trace_seq_printf(s, "oldest event ts: %5llu.%06lu\n",
8747 t, usec_rem);
8748
f3ef7202 8749 t = ns2usecs(ring_buffer_time_stamp(trace_buf->buffer));
11043d8b
YY
8750 usec_rem = do_div(t, USEC_PER_SEC);
8751 trace_seq_printf(s, "now ts: %5llu.%06lu\n", t, usec_rem);
8752 } else {
8753 /* counter or tsc mode for trace_clock */
8754 trace_seq_printf(s, "oldest event ts: %llu\n",
12883efb 8755 ring_buffer_oldest_event_ts(trace_buf->buffer, cpu));
c64e148a 8756
11043d8b 8757 trace_seq_printf(s, "now ts: %llu\n",
f3ef7202 8758 ring_buffer_time_stamp(trace_buf->buffer));
11043d8b 8759 }
c64e148a 8760
12883efb 8761 cnt = ring_buffer_dropped_events_cpu(trace_buf->buffer, cpu);
884bfe89
SP
8762 trace_seq_printf(s, "dropped events: %ld\n", cnt);
8763
12883efb 8764 cnt = ring_buffer_read_events_cpu(trace_buf->buffer, cpu);
ad964704
SRRH
8765 trace_seq_printf(s, "read events: %ld\n", cnt);
8766
5ac48378
SRRH
8767 count = simple_read_from_buffer(ubuf, count, ppos,
8768 s->buffer, trace_seq_used(s));
c8d77183
SR
8769
8770 kfree(s);
8771
8772 return count;
8773}
8774
8775static const struct file_operations tracing_stats_fops = {
4d3435b8 8776 .open = tracing_open_generic_tr,
c8d77183 8777 .read = tracing_stats_read,
b444786f 8778 .llseek = generic_file_llseek,
4d3435b8 8779 .release = tracing_release_generic_tr,
c8d77183
SR
8780};
8781
bc0c38d1
SR
8782#ifdef CONFIG_DYNAMIC_FTRACE
8783
8784static ssize_t
b807c3d0 8785tracing_read_dyn_info(struct file *filp, char __user *ubuf,
bc0c38d1
SR
8786 size_t cnt, loff_t *ppos)
8787{
da537f0a
SRV
8788 ssize_t ret;
8789 char *buf;
bc0c38d1
SR
8790 int r;
8791
36a367b8
SR
8792 /* 512 should be plenty to hold the amount needed */
8793#define DYN_INFO_BUF_SIZE 512
8794
8795 buf = kmalloc(DYN_INFO_BUF_SIZE, GFP_KERNEL);
da537f0a
SRV
8796 if (!buf)
8797 return -ENOMEM;
b807c3d0 8798
36a367b8
SR
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",
da537f0a
SRV
8803 ftrace_update_tot_cnt,
8804 ftrace_number_of_pages,
36a367b8
SR
8805 ftrace_number_of_groups,
8806 ftrace_update_time,
8807 ftrace_total_mod_time);
da537f0a
SRV
8808
8809 ret = simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
8810 kfree(buf);
8811 return ret;
bc0c38d1
SR
8812}
8813
5e2336a0 8814static const struct file_operations tracing_dyn_info_fops = {
4bf39a94 8815 .open = tracing_open_generic,
b807c3d0 8816 .read = tracing_read_dyn_info,
b444786f 8817 .llseek = generic_file_llseek,
bc0c38d1 8818};
77fd5c15 8819#endif /* CONFIG_DYNAMIC_FTRACE */
bc0c38d1 8820
77fd5c15
SRRH
8821#if defined(CONFIG_TRACER_SNAPSHOT) && defined(CONFIG_DYNAMIC_FTRACE)
8822static void
bca6c8d0 8823ftrace_snapshot(unsigned long ip, unsigned long parent_ip,
b5f081b5 8824 struct trace_array *tr, struct ftrace_probe_ops *ops,
6e444319 8825 void *data)
77fd5c15 8826{
cab50379 8827 tracing_snapshot_instance(tr);
77fd5c15 8828}
bc0c38d1 8829
77fd5c15 8830static void
bca6c8d0 8831ftrace_count_snapshot(unsigned long ip, unsigned long parent_ip,
b5f081b5 8832 struct trace_array *tr, struct ftrace_probe_ops *ops,
6e444319 8833 void *data)
bc0c38d1 8834{
6e444319 8835 struct ftrace_func_mapper *mapper = data;
1a93f8bd 8836 long *count = NULL;
77fd5c15 8837
1a93f8bd
SRV
8838 if (mapper)
8839 count = (long *)ftrace_func_mapper_find_ip(mapper, ip);
8840
8841 if (count) {
8842
8843 if (*count <= 0)
8844 return;
bc0c38d1 8845
77fd5c15 8846 (*count)--;
1a93f8bd 8847 }
77fd5c15 8848
cab50379 8849 tracing_snapshot_instance(tr);
77fd5c15
SRRH
8850}
8851
8852static int
8853ftrace_snapshot_print(struct seq_file *m, unsigned long ip,
8854 struct ftrace_probe_ops *ops, void *data)
8855{
6e444319 8856 struct ftrace_func_mapper *mapper = data;
1a93f8bd 8857 long *count = NULL;
77fd5c15
SRRH
8858
8859 seq_printf(m, "%ps:", (void *)ip);
8860
fa6f0cc7 8861 seq_puts(m, "snapshot");
77fd5c15 8862
1a93f8bd
SRV
8863 if (mapper)
8864 count = (long *)ftrace_func_mapper_find_ip(mapper, ip);
8865
8866 if (count)
8867 seq_printf(m, ":count=%ld\n", *count);
77fd5c15 8868 else
1a93f8bd 8869 seq_puts(m, ":unlimited\n");
77fd5c15
SRRH
8870
8871 return 0;
8872}
8873
1a93f8bd 8874static int
b5f081b5 8875ftrace_snapshot_init(struct ftrace_probe_ops *ops, struct trace_array *tr,
6e444319 8876 unsigned long ip, void *init_data, void **data)
1a93f8bd 8877{
6e444319
SRV
8878 struct ftrace_func_mapper *mapper = *data;
8879
8880 if (!mapper) {
8881 mapper = allocate_ftrace_func_mapper();
8882 if (!mapper)
8883 return -ENOMEM;
8884 *data = mapper;
8885 }
1a93f8bd 8886
6e444319 8887 return ftrace_func_mapper_add_ip(mapper, ip, init_data);
1a93f8bd
SRV
8888}
8889
8890static void
b5f081b5 8891ftrace_snapshot_free(struct ftrace_probe_ops *ops, struct trace_array *tr,
6e444319 8892 unsigned long ip, void *data)
1a93f8bd 8893{
6e444319
SRV
8894 struct ftrace_func_mapper *mapper = data;
8895
8896 if (!ip) {
8897 if (!mapper)
8898 return;
8899 free_ftrace_func_mapper(mapper, NULL);
8900 return;
8901 }
1a93f8bd
SRV
8902
8903 ftrace_func_mapper_remove_ip(mapper, ip);
8904}
8905
77fd5c15
SRRH
8906static struct ftrace_probe_ops snapshot_probe_ops = {
8907 .func = ftrace_snapshot,
8908 .print = ftrace_snapshot_print,
8909};
8910
8911static struct ftrace_probe_ops snapshot_count_probe_ops = {
8912 .func = ftrace_count_snapshot,
8913 .print = ftrace_snapshot_print,
1a93f8bd
SRV
8914 .init = ftrace_snapshot_init,
8915 .free = ftrace_snapshot_free,
77fd5c15
SRRH
8916};
8917
8918static int
04ec7bb6 8919ftrace_trace_snapshot_callback(struct trace_array *tr, struct ftrace_hash *hash,
77fd5c15
SRRH
8920 char *glob, char *cmd, char *param, int enable)
8921{
8922 struct ftrace_probe_ops *ops;
8923 void *count = (void *)-1;
8924 char *number;
8925 int ret;
8926
0f179765
SRV
8927 if (!tr)
8928 return -ENODEV;
8929
77fd5c15
SRRH
8930 /* hash funcs only work with set_ftrace_filter */
8931 if (!enable)
8932 return -EINVAL;
8933
8934 ops = param ? &snapshot_count_probe_ops : &snapshot_probe_ops;
8935
180e4e39
VD
8936 if (glob[0] == '!') {
8937 ret = unregister_ftrace_function_probe_func(glob+1, tr, ops);
8938 if (!ret)
8939 tracing_disarm_snapshot(tr);
8940
8941 return ret;
8942 }
77fd5c15
SRRH
8943
8944 if (!param)
8945 goto out_reg;
8946
8947 number = strsep(&param, ":");
8948
8949 if (!strlen(number))
8950 goto out_reg;
8951
8952 /*
8953 * We use the callback data field (which is a pointer)
8954 * as our counter.
8955 */
8956 ret = kstrtoul(number, 0, (unsigned long *)&count);
8957 if (ret)
8958 return ret;
8959
8960 out_reg:
180e4e39 8961 ret = tracing_arm_snapshot(tr);
df62db5b
SRV
8962 if (ret < 0)
8963 goto out;
77fd5c15 8964
4c174688 8965 ret = register_ftrace_function_probe(glob, tr, ops, count);
180e4e39
VD
8966 if (ret < 0)
8967 tracing_disarm_snapshot(tr);
df62db5b 8968 out:
77fd5c15
SRRH
8969 return ret < 0 ? ret : 0;
8970}
8971
8972static struct ftrace_func_command ftrace_snapshot_cmd = {
8973 .name = "snapshot",
8974 .func = ftrace_trace_snapshot_callback,
8975};
8976
38de93ab 8977static __init int register_snapshot_cmd(void)
77fd5c15
SRRH
8978{
8979 return register_ftrace_command(&ftrace_snapshot_cmd);
8980}
8981#else
38de93ab 8982static inline __init int register_snapshot_cmd(void) { return 0; }
77fd5c15 8983#endif /* defined(CONFIG_TRACER_SNAPSHOT) && defined(CONFIG_DYNAMIC_FTRACE) */
bc0c38d1 8984
7eeafbca 8985static struct dentry *tracing_get_dentry(struct trace_array *tr)
bc0c38d1 8986{
8434dc93
SRRH
8987 if (WARN_ON(!tr->dir))
8988 return ERR_PTR(-ENODEV);
8989
8990 /* Top directory uses NULL as the parent */
8991 if (tr->flags & TRACE_ARRAY_FL_GLOBAL)
8992 return NULL;
8993
8994 /* All sub buffers have a descriptor */
2b6080f2 8995 return tr->dir;
bc0c38d1
SR
8996}
8997
2b6080f2 8998static struct dentry *tracing_dentry_percpu(struct trace_array *tr, int cpu)
b04cc6b1 8999{
b04cc6b1
FW
9000 struct dentry *d_tracer;
9001
2b6080f2
SR
9002 if (tr->percpu_dir)
9003 return tr->percpu_dir;
b04cc6b1 9004
7eeafbca 9005 d_tracer = tracing_get_dentry(tr);
14a5ae40 9006 if (IS_ERR(d_tracer))
b04cc6b1
FW
9007 return NULL;
9008
8434dc93 9009 tr->percpu_dir = tracefs_create_dir("per_cpu", d_tracer);
b04cc6b1 9010
24589e3a 9011 MEM_FAIL(!tr->percpu_dir,
8434dc93 9012 "Could not create tracefs directory 'per_cpu/%d'\n", cpu);
b04cc6b1 9013
2b6080f2 9014 return tr->percpu_dir;
b04cc6b1
FW
9015}
9016
649e9c70
ON
9017static struct dentry *
9018trace_create_cpu_file(const char *name, umode_t mode, struct dentry *parent,
9019 void *data, long cpu, const struct file_operations *fops)
9020{
9021 struct dentry *ret = trace_create_file(name, mode, parent, data, fops);
9022
9023 if (ret) /* See tracing_get_cpu() */
7682c918 9024 d_inode(ret)->i_cdev = (void *)(cpu + 1);
649e9c70
ON
9025 return ret;
9026}
9027
2b6080f2 9028static void
8434dc93 9029tracing_init_tracefs_percpu(struct trace_array *tr, long cpu)
b04cc6b1 9030{
2b6080f2 9031 struct dentry *d_percpu = tracing_dentry_percpu(tr, cpu);
5452af66 9032 struct dentry *d_cpu;
dd49a38c 9033 char cpu_dir[30]; /* 30 characters should be more than enough */
b04cc6b1 9034
0a3d7ce7
NK
9035 if (!d_percpu)
9036 return;
9037
dd49a38c 9038 snprintf(cpu_dir, 30, "cpu%ld", cpu);
8434dc93 9039 d_cpu = tracefs_create_dir(cpu_dir, d_percpu);
8656e7a2 9040 if (!d_cpu) {
a395d6a7 9041 pr_warn("Could not create tracefs '%s' entry\n", cpu_dir);
8656e7a2
FW
9042 return;
9043 }
b04cc6b1 9044
8656e7a2 9045 /* per cpu trace_pipe */
21ccc9cd 9046 trace_create_cpu_file("trace_pipe", TRACE_MODE_READ, d_cpu,
15544209 9047 tr, cpu, &tracing_pipe_fops);
b04cc6b1
FW
9048
9049 /* per cpu trace */
21ccc9cd 9050 trace_create_cpu_file("trace", TRACE_MODE_WRITE, d_cpu,
6484c71c 9051 tr, cpu, &tracing_fops);
7f96f93f 9052
21ccc9cd 9053 trace_create_cpu_file("trace_pipe_raw", TRACE_MODE_READ, d_cpu,
46ef2be0 9054 tr, cpu, &tracing_buffers_fops);
7f96f93f 9055
21ccc9cd 9056 trace_create_cpu_file("stats", TRACE_MODE_READ, d_cpu,
4d3435b8 9057 tr, cpu, &tracing_stats_fops);
438ced17 9058
21ccc9cd 9059 trace_create_cpu_file("buffer_size_kb", TRACE_MODE_READ, d_cpu,
0bc392ee 9060 tr, cpu, &tracing_entries_fops);
f1affcaa 9061
950032ff
SRG
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);
f1affcaa 9065#ifdef CONFIG_TRACER_SNAPSHOT
2124de79
SRG
9066 if (!tr->range_addr_start) {
9067 trace_create_cpu_file("snapshot", TRACE_MODE_WRITE, d_cpu,
9068 tr, cpu, &snapshot_fops);
6de58e62 9069
2124de79
SRG
9070 trace_create_cpu_file("snapshot_raw", TRACE_MODE_READ, d_cpu,
9071 tr, cpu, &snapshot_raw_fops);
9072 }
f1affcaa 9073#endif
b04cc6b1
FW
9074}
9075
60a11774
SR
9076#ifdef CONFIG_FTRACE_SELFTEST
9077/* Let selftest have access to static functions in this file */
9078#include "trace_selftest.c"
9079#endif
9080
577b785f
SR
9081static ssize_t
9082trace_options_read(struct file *filp, char __user *ubuf, size_t cnt,
9083 loff_t *ppos)
9084{
9085 struct trace_option_dentry *topt = filp->private_data;
9086 char *buf;
9087
9088 if (topt->flags->val & topt->opt->bit)
9089 buf = "1\n";
9090 else
9091 buf = "0\n";
9092
9093 return simple_read_from_buffer(ubuf, cnt, ppos, buf, 2);
9094}
9095
9096static ssize_t
9097trace_options_write(struct file *filp, const char __user *ubuf, size_t cnt,
9098 loff_t *ppos)
9099{
9100 struct trace_option_dentry *topt = filp->private_data;
9101 unsigned long val;
577b785f
SR
9102 int ret;
9103
22fe9b54
PH
9104 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
9105 if (ret)
577b785f
SR
9106 return ret;
9107
8d18eaaf
LZ
9108 if (val != 0 && val != 1)
9109 return -EINVAL;
577b785f 9110
8d18eaaf 9111 if (!!(topt->flags->val & topt->opt->bit) != val) {
577b785f 9112 mutex_lock(&trace_types_lock);
8c1a49ae 9113 ret = __set_tracer_option(topt->tr, topt->flags,
c757bea9 9114 topt->opt, !val);
577b785f
SR
9115 mutex_unlock(&trace_types_lock);
9116 if (ret)
9117 return ret;
577b785f
SR
9118 }
9119
9120 *ppos += cnt;
9121
9122 return cnt;
9123}
9124
7e2cfbd2
SRG
9125static int tracing_open_options(struct inode *inode, struct file *filp)
9126{
9127 struct trace_option_dentry *topt = inode->i_private;
9128 int ret;
9129
9130 ret = tracing_check_open_get_tr(topt->tr);
9131 if (ret)
9132 return ret;
9133
9134 filp->private_data = inode->i_private;
9135 return 0;
9136}
9137
9138static int tracing_release_options(struct inode *inode, struct file *file)
9139{
9140 struct trace_option_dentry *topt = file->private_data;
9141
9142 trace_array_put(topt->tr);
9143 return 0;
9144}
577b785f
SR
9145
9146static const struct file_operations trace_options_fops = {
7e2cfbd2 9147 .open = tracing_open_options,
577b785f
SR
9148 .read = trace_options_read,
9149 .write = trace_options_write,
b444786f 9150 .llseek = generic_file_llseek,
7e2cfbd2 9151 .release = tracing_release_options,
577b785f
SR
9152};
9153
9a38a885
SRRH
9154/*
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.
9161 *
9162 * In order to extract both the index and the trace_array descriptor,
9163 * get_tr_index() uses the following algorithm.
9164 *
9165 * idx = *ptr;
9166 *
9167 * As the pointer itself contains the address of the index (remember
9168 * index[1] == 1).
9169 *
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.
9172 *
9173 * ptr - idx == &index[0]
9174 *
9175 * Then a simple container_of() from that pointer gets us to the
9176 * trace_array descriptor.
9177 */
9178static void get_tr_index(void *data, struct trace_array **ptr,
9179 unsigned int *pindex)
9180{
9181 *pindex = *(unsigned char *)data;
9182
9183 *ptr = container_of(data - *pindex, struct trace_array,
9184 trace_flags_index);
9185}
9186
a8259075
SR
9187static ssize_t
9188trace_options_core_read(struct file *filp, char __user *ubuf, size_t cnt,
9189 loff_t *ppos)
9190{
9a38a885
SRRH
9191 void *tr_index = filp->private_data;
9192 struct trace_array *tr;
9193 unsigned int index;
a8259075
SR
9194 char *buf;
9195
9a38a885
SRRH
9196 get_tr_index(tr_index, &tr, &index);
9197
9198 if (tr->trace_flags & (1 << index))
a8259075
SR
9199 buf = "1\n";
9200 else
9201 buf = "0\n";
9202
9203 return simple_read_from_buffer(ubuf, cnt, ppos, buf, 2);
9204}
9205
9206static ssize_t
9207trace_options_core_write(struct file *filp, const char __user *ubuf, size_t cnt,
9208 loff_t *ppos)
9209{
9a38a885
SRRH
9210 void *tr_index = filp->private_data;
9211 struct trace_array *tr;
9212 unsigned int index;
a8259075
SR
9213 unsigned long val;
9214 int ret;
9215
9a38a885
SRRH
9216 get_tr_index(tr_index, &tr, &index);
9217
22fe9b54
PH
9218 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
9219 if (ret)
a8259075
SR
9220 return ret;
9221
f2d84b65 9222 if (val != 0 && val != 1)
a8259075 9223 return -EINVAL;
69d34da2 9224
3a53acf1 9225 mutex_lock(&event_mutex);
69d34da2 9226 mutex_lock(&trace_types_lock);
2b6080f2 9227 ret = set_tracer_flag(tr, 1 << index, val);
69d34da2 9228 mutex_unlock(&trace_types_lock);
3a53acf1 9229 mutex_unlock(&event_mutex);
a8259075 9230
613f04a0
SRRH
9231 if (ret < 0)
9232 return ret;
9233
a8259075
SR
9234 *ppos += cnt;
9235
9236 return cnt;
9237}
9238
a8259075
SR
9239static 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,
b444786f 9243 .llseek = generic_file_llseek,
a8259075
SR
9244};
9245
5452af66 9246struct dentry *trace_create_file(const char *name,
f4ae40a6 9247 umode_t mode,
5452af66
FW
9248 struct dentry *parent,
9249 void *data,
9250 const struct file_operations *fops)
9251{
9252 struct dentry *ret;
9253
8434dc93 9254 ret = tracefs_create_file(name, mode, parent, data, fops);
5452af66 9255 if (!ret)
a395d6a7 9256 pr_warn("Could not create tracefs '%s' entry\n", name);
5452af66
FW
9257
9258 return ret;
9259}
9260
9261
2b6080f2 9262static struct dentry *trace_options_init_dentry(struct trace_array *tr)
a8259075
SR
9263{
9264 struct dentry *d_tracer;
a8259075 9265
2b6080f2
SR
9266 if (tr->options)
9267 return tr->options;
a8259075 9268
7eeafbca 9269 d_tracer = tracing_get_dentry(tr);
14a5ae40 9270 if (IS_ERR(d_tracer))
a8259075
SR
9271 return NULL;
9272
8434dc93 9273 tr->options = tracefs_create_dir("options", d_tracer);
2b6080f2 9274 if (!tr->options) {
a395d6a7 9275 pr_warn("Could not create tracefs directory 'options'\n");
a8259075
SR
9276 return NULL;
9277 }
9278
2b6080f2 9279 return tr->options;
a8259075
SR
9280}
9281
577b785f 9282static void
2b6080f2
SR
9283create_trace_option_file(struct trace_array *tr,
9284 struct trace_option_dentry *topt,
577b785f
SR
9285 struct tracer_flags *flags,
9286 struct tracer_opt *opt)
9287{
9288 struct dentry *t_options;
577b785f 9289
2b6080f2 9290 t_options = trace_options_init_dentry(tr);
577b785f
SR
9291 if (!t_options)
9292 return;
9293
9294 topt->flags = flags;
9295 topt->opt = opt;
2b6080f2 9296 topt->tr = tr;
577b785f 9297
21ccc9cd
SRV
9298 topt->entry = trace_create_file(opt->name, TRACE_MODE_WRITE,
9299 t_options, topt, &trace_options_fops);
577b785f 9300
577b785f
SR
9301}
9302
37aea98b 9303static void
2b6080f2 9304create_trace_option_files(struct trace_array *tr, struct tracer *tracer)
577b785f
SR
9305{
9306 struct trace_option_dentry *topts;
37aea98b 9307 struct trace_options *tr_topts;
577b785f
SR
9308 struct tracer_flags *flags;
9309 struct tracer_opt *opts;
9310 int cnt;
37aea98b 9311 int i;
577b785f
SR
9312
9313 if (!tracer)
37aea98b 9314 return;
577b785f
SR
9315
9316 flags = tracer->flags;
9317
9318 if (!flags || !flags->opts)
37aea98b
SRRH
9319 return;
9320
9321 /*
9322 * If this is an instance, only create flags for tracers
9323 * the instance may have.
9324 */
9325 if (!trace_ok_for_array(tracer, tr))
9326 return;
9327
9328 for (i = 0; i < tr->nr_topts; i++) {
d39cdd20
CH
9329 /* Make sure there's no duplicate flags. */
9330 if (WARN_ON_ONCE(tr->topts[i].tracer->flags == tracer->flags))
37aea98b
SRRH
9331 return;
9332 }
577b785f
SR
9333
9334 opts = flags->opts;
9335
9336 for (cnt = 0; opts[cnt].name; cnt++)
9337 ;
9338
0cfe8245 9339 topts = kcalloc(cnt + 1, sizeof(*topts), GFP_KERNEL);
577b785f 9340 if (!topts)
37aea98b
SRRH
9341 return;
9342
9343 tr_topts = krealloc(tr->topts, sizeof(*tr->topts) * (tr->nr_topts + 1),
9344 GFP_KERNEL);
9345 if (!tr_topts) {
9346 kfree(topts);
9347 return;
9348 }
9349
9350 tr->topts = tr_topts;
9351 tr->topts[tr->nr_topts].tracer = tracer;
9352 tr->topts[tr->nr_topts].topts = topts;
9353 tr->nr_topts++;
577b785f 9354
41d9c0be 9355 for (cnt = 0; opts[cnt].name; cnt++) {
2b6080f2 9356 create_trace_option_file(tr, &topts[cnt], flags,
577b785f 9357 &opts[cnt]);
24589e3a 9358 MEM_FAIL(topts[cnt].entry == NULL,
41d9c0be
SRRH
9359 "Failed to create trace option: %s",
9360 opts[cnt].name);
9361 }
577b785f
SR
9362}
9363
a8259075 9364static struct dentry *
2b6080f2
SR
9365create_trace_option_core_file(struct trace_array *tr,
9366 const char *option, long index)
a8259075
SR
9367{
9368 struct dentry *t_options;
a8259075 9369
2b6080f2 9370 t_options = trace_options_init_dentry(tr);
a8259075
SR
9371 if (!t_options)
9372 return NULL;
9373
21ccc9cd 9374 return trace_create_file(option, TRACE_MODE_WRITE, t_options,
9a38a885
SRRH
9375 (void *)&tr->trace_flags_index[index],
9376 &trace_options_core_fops);
a8259075
SR
9377}
9378
16270145 9379static void create_trace_options_dir(struct trace_array *tr)
a8259075
SR
9380{
9381 struct dentry *t_options;
16270145 9382 bool top_level = tr == &global_trace;
a8259075
SR
9383 int i;
9384
2b6080f2 9385 t_options = trace_options_init_dentry(tr);
a8259075
SR
9386 if (!t_options)
9387 return;
9388
16270145
SRRH
9389 for (i = 0; trace_options[i]; i++) {
9390 if (top_level ||
9391 !((1 << i) & TOP_LEVEL_TRACE_FLAGS))
9392 create_trace_option_core_file(tr, trace_options[i], i);
9393 }
a8259075
SR
9394}
9395
499e5470
SR
9396static ssize_t
9397rb_simple_read(struct file *filp, char __user *ubuf,
9398 size_t cnt, loff_t *ppos)
9399{
348f0fc2 9400 struct trace_array *tr = filp->private_data;
499e5470
SR
9401 char buf[64];
9402 int r;
9403
10246fa3 9404 r = tracer_tracing_is_on(tr);
499e5470
SR
9405 r = sprintf(buf, "%d\n", r);
9406
9407 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
9408}
9409
9410static ssize_t
9411rb_simple_write(struct file *filp, const char __user *ubuf,
9412 size_t cnt, loff_t *ppos)
9413{
348f0fc2 9414 struct trace_array *tr = filp->private_data;
13292494 9415 struct trace_buffer *buffer = tr->array_buffer.buffer;
499e5470
SR
9416 unsigned long val;
9417 int ret;
9418
9419 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
9420 if (ret)
9421 return ret;
9422
9423 if (buffer) {
2df8f8a6 9424 mutex_lock(&trace_types_lock);
f143641b
SRV
9425 if (!!val == tracer_tracing_is_on(tr)) {
9426 val = 0; /* do nothing */
9427 } else if (val) {
10246fa3 9428 tracer_tracing_on(tr);
2b6080f2
SR
9429 if (tr->current_trace->start)
9430 tr->current_trace->start(tr);
2df8f8a6 9431 } else {
10246fa3 9432 tracer_tracing_off(tr);
2b6080f2
SR
9433 if (tr->current_trace->stop)
9434 tr->current_trace->stop(tr);
2b0fd9a5
SRG
9435 /* Wake up any waiters */
9436 ring_buffer_wake_waiters(buffer, RING_BUFFER_ALL_CPUS);
2df8f8a6
SR
9437 }
9438 mutex_unlock(&trace_types_lock);
499e5470
SR
9439 }
9440
9441 (*ppos)++;
9442
9443 return cnt;
9444}
9445
9446static const struct file_operations rb_simple_fops = {
7b85af63 9447 .open = tracing_open_generic_tr,
499e5470
SR
9448 .read = rb_simple_read,
9449 .write = rb_simple_write,
7b85af63 9450 .release = tracing_release_generic_tr,
499e5470
SR
9451 .llseek = default_llseek,
9452};
9453
03329f99
SRV
9454static ssize_t
9455buffer_percent_read(struct file *filp, char __user *ubuf,
9456 size_t cnt, loff_t *ppos)
9457{
9458 struct trace_array *tr = filp->private_data;
9459 char buf[64];
9460 int r;
9461
9462 r = tr->buffer_percent;
9463 r = sprintf(buf, "%d\n", r);
9464
9465 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
9466}
9467
9468static ssize_t
9469buffer_percent_write(struct file *filp, const char __user *ubuf,
9470 size_t cnt, loff_t *ppos)
9471{
9472 struct trace_array *tr = filp->private_data;
9473 unsigned long val;
9474 int ret;
9475
9476 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
9477 if (ret)
9478 return ret;
9479
9480 if (val > 100)
9481 return -EINVAL;
9482
03329f99
SRV
9483 tr->buffer_percent = val;
9484
9485 (*ppos)++;
9486
9487 return cnt;
9488}
9489
9490static 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,
9496};
9497
2808e31e 9498static ssize_t
2f84b39f 9499buffer_subbuf_size_read(struct file *filp, char __user *ubuf, size_t cnt, loff_t *ppos)
2808e31e
TSV
9500{
9501 struct trace_array *tr = filp->private_data;
2f84b39f 9502 size_t size;
2808e31e 9503 char buf[64];
2f84b39f 9504 int order;
2808e31e
TSV
9505 int r;
9506
2f84b39f
SRG
9507 order = ring_buffer_subbuf_order_get(tr->array_buffer.buffer);
9508 size = (PAGE_SIZE << order) / 1024;
9509
9510 r = sprintf(buf, "%zd\n", size);
2808e31e
TSV
9511
9512 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
9513}
9514
9515static ssize_t
2f84b39f
SRG
9516buffer_subbuf_size_write(struct file *filp, const char __user *ubuf,
9517 size_t cnt, loff_t *ppos)
2808e31e
TSV
9518{
9519 struct trace_array *tr = filp->private_data;
9520 unsigned long val;
aa067682 9521 int old_order;
2f84b39f
SRG
9522 int order;
9523 int pages;
2808e31e
TSV
9524 int ret;
9525
9526 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
9527 if (ret)
9528 return ret;
9529
2f84b39f
SRG
9530 val *= 1024; /* value passed in is in KB */
9531
9532 pages = DIV_ROUND_UP(val, PAGE_SIZE);
9533 order = fls(pages - 1);
9534
2808e31e 9535 /* limit between 1 and 128 system pages */
2f84b39f 9536 if (order < 0 || order > 7)
2808e31e
TSV
9537 return -EINVAL;
9538
fa4b54af
SRG
9539 /* Do not allow tracing while changing the order of the ring buffer */
9540 tracing_stop_tr(tr);
9541
aa067682 9542 old_order = ring_buffer_subbuf_order_get(tr->array_buffer.buffer);
2f84b39f 9543 if (old_order == order)
fa4b54af 9544 goto out;
aa067682 9545
2f84b39f 9546 ret = ring_buffer_subbuf_order_set(tr->array_buffer.buffer, order);
2808e31e 9547 if (ret)
fa4b54af 9548 goto out;
2808e31e 9549
aa067682
SRG
9550#ifdef CONFIG_TRACER_MAX_TRACE
9551
9552 if (!tr->allocated_snapshot)
9553 goto out_max;
2808e31e 9554
2f84b39f 9555 ret = ring_buffer_subbuf_order_set(tr->max_buffer.buffer, order);
aa067682
SRG
9556 if (ret) {
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)) {
9560 /*
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
9571 * tracing.
9572 */
9573 tracing_disabled = 1;
9574 }
fa4b54af 9575 goto out;
aa067682
SRG
9576 }
9577 out_max:
9578#endif
9579 (*ppos)++;
fa4b54af
SRG
9580 out:
9581 if (ret)
9582 cnt = ret;
9583 tracing_start_tr(tr);
2808e31e
TSV
9584 return cnt;
9585}
9586
2f84b39f 9587static const struct file_operations buffer_subbuf_size_fops = {
2808e31e 9588 .open = tracing_open_generic_tr,
2f84b39f
SRG
9589 .read = buffer_subbuf_size_read,
9590 .write = buffer_subbuf_size_write,
2808e31e
TSV
9591 .release = tracing_release_generic_tr,
9592 .llseek = default_llseek,
9593};
9594
ff585c5b 9595static struct dentry *trace_instance_dir;
277ba044
SR
9596
9597static void
8434dc93 9598init_tracer_tracefs(struct trace_array *tr, struct dentry *d_tracer);
277ba044 9599
35a380dd
MHG
9600#ifdef CONFIG_MODULES
9601static int make_mod_delta(struct module *mod, void *data)
9602{
9603 struct trace_module_delta *module_delta;
9604 struct trace_scratch *tscratch;
9605 struct trace_mod_entry *entry;
9606 struct trace_array *tr = data;
9607 int i;
9608
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))
9614 continue;
9615 if (mod->state == MODULE_STATE_GOING)
9616 module_delta->delta[i] = 0;
9617 else
9618 module_delta->delta[i] = (unsigned long)mod->mem[MOD_TEXT].base
9619 - entry->mod_addr;
9620 break;
9621 }
9622 return 0;
9623}
9624#else
9625static int make_mod_delta(struct module *mod, void *data)
9626{
9627 return 0;
9628}
9629#endif
9630
9631static int mod_addr_comp(const void *a, const void *b, const void *data)
9632{
9633 const struct trace_mod_entry *e1 = a;
9634 const struct trace_mod_entry *e2 = b;
9635
9636 return e1->mod_addr > e2->mod_addr ? 1 : -1;
9637}
9638
fd39e48f
SR
9639static void setup_trace_scratch(struct trace_array *tr,
9640 struct trace_scratch *tscratch, unsigned int size)
9641{
35a380dd 9642 struct trace_module_delta *module_delta;
fd39e48f 9643 struct trace_mod_entry *entry;
35a380dd 9644 int i, nr_entries;
fd39e48f
SR
9645
9646 if (!tscratch)
9647 return;
9648
9649 tr->scratch = tscratch;
9650 tr->scratch_size = size;
9651
028a58ec
SR
9652 if (tscratch->text_addr)
9653 tr->text_delta = (unsigned long)_text - tscratch->text_addr;
fd39e48f
SR
9654
9655 if (struct_size(tscratch, entries, tscratch->nr_entries) > size)
9656 goto reset;
9657
9658 /* Check if each module name is a valid string */
35a380dd 9659 for (i = 0; i < tscratch->nr_entries; i++) {
fd39e48f
SR
9660 int n;
9661
9662 entry = &tscratch->entries[i];
9663
9664 for (n = 0; n < MODULE_NAME_LEN; n++) {
9665 if (entry->mod_name[n] == '\0')
9666 break;
9667 if (!isprint(entry->mod_name[n]))
9668 goto reset;
9669 }
9670 if (n == MODULE_NAME_LEN)
9671 goto reset;
9672 }
35a380dd
MHG
9673
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);
9678
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.");
9683 goto reset;
9684 }
9685 init_rcu_head(&module_delta->rcu);
9686 } else
9687 module_delta = NULL;
9688 WRITE_ONCE(tr->module_delta, module_delta);
9689
9690 /* Scan modules to make text delta for modules. */
9691 module_for_each_mod(make_mod_delta, tr);
2632a201
MHG
9692
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.");
9698 goto reset;
9699 }
9700 }
fd39e48f
SR
9701 return;
9702 reset:
9703 /* Invalid trace modules */
9704 memset(tscratch, 0, size);
9705}
9706
55034cd6 9707static int
1c5eb448 9708allocate_trace_buffer(struct trace_array *tr, struct array_buffer *buf, int size)
277ba044
SR
9709{
9710 enum ring_buffer_flags rb_flags;
b6533482 9711 struct trace_scratch *tscratch;
5dbeb56b 9712 unsigned int scratch_size = 0;
737223fb 9713
983f938a 9714 rb_flags = tr->trace_flags & TRACE_ITER_OVERWRITE ? RB_FL_OVERWRITE : 0;
737223fb 9715
dced341b
SRRH
9716 buf->tr = tr;
9717
2124de79 9718 if (tr->range_addr_start && tr->range_addr_size) {
fd39e48f 9719 /* Add scratch buffer to handle 128 modules */
2124de79
SRG
9720 buf->buffer = ring_buffer_alloc_range(size, rb_flags, 0,
9721 tr->range_addr_start,
b6533482 9722 tr->range_addr_size,
fd39e48f 9723 struct_size(tscratch, entries, 128));
b6533482
SR
9724
9725 tscratch = ring_buffer_meta_scratch(buf->buffer, &scratch_size);
fd39e48f 9726 setup_trace_scratch(tr, tscratch, scratch_size);
7a1d1e4b 9727
2124de79
SRG
9728 /*
9729 * This is basically the same as a mapped buffer,
9730 * with the same restrictions.
9731 */
9732 tr->mapped++;
9733 } else {
9734 buf->buffer = ring_buffer_alloc(size, rb_flags);
9735 }
55034cd6
SRRH
9736 if (!buf->buffer)
9737 return -ENOMEM;
737223fb 9738
55034cd6
SRRH
9739 buf->data = alloc_percpu(struct trace_array_cpu);
9740 if (!buf->data) {
9741 ring_buffer_free(buf->buffer);
4397f045 9742 buf->buffer = NULL;
55034cd6
SRRH
9743 return -ENOMEM;
9744 }
737223fb 9745
737223fb 9746 /* Allocate the first page for all buffers */
1c5eb448
SRV
9747 set_buffer_entries(&tr->array_buffer,
9748 ring_buffer_size(tr->array_buffer.buffer, 0));
737223fb 9749
55034cd6
SRRH
9750 return 0;
9751}
737223fb 9752
59927cbe
ZL
9753static void free_trace_buffer(struct array_buffer *buf)
9754{
9755 if (buf->buffer) {
9756 ring_buffer_free(buf->buffer);
9757 buf->buffer = NULL;
9758 free_percpu(buf->data);
9759 buf->data = NULL;
9760 }
9761}
9762
55034cd6
SRRH
9763static int allocate_trace_buffers(struct trace_array *tr, int size)
9764{
9765 int ret;
737223fb 9766
1c5eb448 9767 ret = allocate_trace_buffer(tr, &tr->array_buffer, size);
55034cd6
SRRH
9768 if (ret)
9769 return ret;
737223fb 9770
55034cd6 9771#ifdef CONFIG_TRACER_MAX_TRACE
2124de79
SRG
9772 /* Fix mapped buffer trace arrays do not have snapshot buffers */
9773 if (tr->range_addr_start)
9774 return 0;
9775
55034cd6
SRRH
9776 ret = allocate_trace_buffer(tr, &tr->max_buffer,
9777 allocate_snapshot ? size : 1);
24589e3a 9778 if (MEM_FAIL(ret, "Failed to allocate trace buffer\n")) {
59927cbe 9779 free_trace_buffer(&tr->array_buffer);
55034cd6
SRRH
9780 return -ENOMEM;
9781 }
9782 tr->allocated_snapshot = allocate_snapshot;
737223fb 9783
55034cd6 9784 allocate_snapshot = false;
737223fb 9785#endif
11f5efc3 9786
55034cd6 9787 return 0;
737223fb
SRRH
9788}
9789
23aaa3c1
SRRH
9790static void free_trace_buffers(struct trace_array *tr)
9791{
9792 if (!tr)
9793 return;
9794
1c5eb448 9795 free_trace_buffer(&tr->array_buffer);
2c9ee74a 9796 kfree(tr->module_delta);
23aaa3c1
SRRH
9797
9798#ifdef CONFIG_TRACER_MAX_TRACE
f0b70cc4 9799 free_trace_buffer(&tr->max_buffer);
23aaa3c1
SRRH
9800#endif
9801}
9802
9a38a885
SRRH
9803static void init_trace_flags_index(struct trace_array *tr)
9804{
9805 int i;
9806
9807 /* Used by the trace options files */
9808 for (i = 0; i < TRACE_FLAGS_MAX_SIZE; i++)
9809 tr->trace_flags_index[i] = i;
9810}
9811
37aea98b
SRRH
9812static void __update_tracer_options(struct trace_array *tr)
9813{
9814 struct tracer *t;
9815
9816 for (t = trace_types; t; t = t->next)
9817 add_tracer_options(tr, t);
9818}
9819
9820static void update_tracer_options(struct trace_array *tr)
9821{
9822 mutex_lock(&trace_types_lock);
ef9188bc 9823 tracer_options_updated = true;
37aea98b
SRRH
9824 __update_tracer_options(tr);
9825 mutex_unlock(&trace_types_lock);
9826}
9827
89c95fce
TZ
9828/* Must have trace_types_lock held */
9829struct trace_array *trace_array_find(const char *instance)
9830{
9831 struct trace_array *tr, *found = NULL;
9832
9833 list_for_each_entry(tr, &ftrace_trace_arrays, list) {
9834 if (tr->name && strcmp(tr->name, instance) == 0) {
9835 found = tr;
9836 break;
9837 }
9838 }
9839
9840 return found;
9841}
9842
9843struct trace_array *trace_array_find_get(const char *instance)
9844{
9845 struct trace_array *tr;
9846
9847 mutex_lock(&trace_types_lock);
9848 tr = trace_array_find(instance);
9849 if (tr)
9850 tr->ref++;
9851 mutex_unlock(&trace_types_lock);
9852
9853 return tr;
9854}
9855
4114fbfd
MH
9856static int trace_array_create_dir(struct trace_array *tr)
9857{
9858 int ret;
9859
9860 tr->dir = tracefs_create_dir(tr->name, trace_instance_dir);
9861 if (!tr->dir)
9862 return -EINVAL;
9863
9864 ret = event_trace_add_tracer(tr->dir, tr);
ff41c28c 9865 if (ret) {
4114fbfd 9866 tracefs_remove(tr->dir);
ff41c28c
KA
9867 return ret;
9868 }
4114fbfd
MH
9869
9870 init_tracer_tracefs(tr, tr->dir);
9871 __update_tracer_options(tr);
9872
9873 return ret;
9874}
9875
d2356997 9876static struct trace_array *
2124de79
SRG
9877trace_array_create_systems(const char *name, const char *systems,
9878 unsigned long range_addr_start,
9879 unsigned long range_addr_size)
737223fb 9880{
277ba044
SR
9881 struct trace_array *tr;
9882 int ret;
277ba044 9883
277ba044
SR
9884 ret = -ENOMEM;
9885 tr = kzalloc(sizeof(*tr), GFP_KERNEL);
9886 if (!tr)
28879787 9887 return ERR_PTR(ret);
277ba044
SR
9888
9889 tr->name = kstrdup(name, GFP_KERNEL);
9890 if (!tr->name)
9891 goto out_free_tr;
9892
ccfe9e42
AL
9893 if (!alloc_cpumask_var(&tr->tracing_cpumask, GFP_KERNEL))
9894 goto out_free_tr;
9895
3d07fa1d 9896 if (!zalloc_cpumask_var(&tr->pipe_cpumask, GFP_KERNEL))
c2489bb7
ZY
9897 goto out_free_tr;
9898
d2356997
SRG
9899 if (systems) {
9900 tr->system_names = kstrdup_const(systems, GFP_KERNEL);
9901 if (!tr->system_names)
9902 goto out_free_tr;
9903 }
9904
2124de79
SRG
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;
9908
20550622 9909 tr->trace_flags = global_trace.trace_flags & ~ZEROED_TRACE_FLAGS;
983f938a 9910
ccfe9e42
AL
9911 cpumask_copy(tr->tracing_cpumask, cpu_all_mask);
9912
277ba044
SR
9913 raw_spin_lock_init(&tr->start_lock);
9914
0b9b12c1 9915 tr->max_lock = (arch_spinlock_t)__ARCH_SPIN_LOCK_UNLOCKED;
180e4e39
VD
9916#ifdef CONFIG_TRACER_MAX_TRACE
9917 spin_lock_init(&tr->snapshot_trigger_lock);
9918#endif
277ba044
SR
9919 tr->current_trace = &nop_trace;
9920
9921 INIT_LIST_HEAD(&tr->systems);
9922 INIT_LIST_HEAD(&tr->events);
067fe038 9923 INIT_LIST_HEAD(&tr->hist_vars);
2f754e77 9924 INIT_LIST_HEAD(&tr->err_log);
7b382efd 9925 INIT_LIST_HEAD(&tr->marker_list);
277ba044 9926
b355247d
SR
9927#ifdef CONFIG_MODULES
9928 INIT_LIST_HEAD(&tr->mod_events);
9929#endif
9930
737223fb 9931 if (allocate_trace_buffers(tr, trace_buf_size) < 0)
277ba044
SR
9932 goto out_free_tr;
9933
a1f157c7
ZY
9934 /* The ring buffer is defaultly expanded */
9935 trace_set_ring_buffer_expanded(tr);
9936
4114fbfd 9937 if (ftrace_allocate_ftrace_ops(tr) < 0)
277ba044
SR
9938 goto out_free_tr;
9939
04ec7bb6
SRV
9940 ftrace_init_trace_array(tr);
9941
9a38a885 9942 init_trace_flags_index(tr);
4114fbfd
MH
9943
9944 if (trace_instance_dir) {
9945 ret = trace_array_create_dir(tr);
9946 if (ret)
9947 goto out_free_tr;
720dee53
MH
9948 } else
9949 __trace_early_add_events(tr);
277ba044
SR
9950
9951 list_add(&tr->list, &ftrace_trace_arrays);
9952
28879787
DI
9953 tr->ref++;
9954
f45d1225 9955 return tr;
277ba044
SR
9956
9957 out_free_tr:
4114fbfd 9958 ftrace_free_ftrace_ops(tr);
23aaa3c1 9959 free_trace_buffers(tr);
c2489bb7 9960 free_cpumask_var(tr->pipe_cpumask);
ccfe9e42 9961 free_cpumask_var(tr->tracing_cpumask);
d2356997 9962 kfree_const(tr->system_names);
fb6d0323 9963 kfree(tr->range_name);
277ba044
SR
9964 kfree(tr->name);
9965 kfree(tr);
9966
f45d1225
DI
9967 return ERR_PTR(ret);
9968}
277ba044 9969
d2356997
SRG
9970static struct trace_array *trace_array_create(const char *name)
9971{
2124de79 9972 return trace_array_create_systems(name, NULL, 0, 0);
d2356997
SRG
9973}
9974
f45d1225
DI
9975static int instance_mkdir(const char *name)
9976{
28879787
DI
9977 struct trace_array *tr;
9978 int ret;
9979
d33b10c0
SR
9980 guard(mutex)(&event_mutex);
9981 guard(mutex)(&trace_types_lock);
28879787
DI
9982
9983 ret = -EEXIST;
89c95fce 9984 if (trace_array_find(name))
d33b10c0 9985 return -EEXIST;
28879787
DI
9986
9987 tr = trace_array_create(name);
9988
9989 ret = PTR_ERR_OR_ZERO(tr);
9990
28879787
DI
9991 return ret;
9992}
9993
4808595a 9994#ifdef CONFIG_MMU
394f3f02 9995static u64 map_pages(unsigned long start, unsigned long size)
e645535a 9996{
394f3f02
SR
9997 unsigned long vmap_start, vmap_end;
9998 struct vm_struct *area;
9999 int ret;
e645535a 10000
394f3f02
SR
10001 area = get_vm_area(size, VM_IOREMAP);
10002 if (!area)
e645535a
SRG
10003 return 0;
10004
394f3f02
SR
10005 vmap_start = (unsigned long) area->addr;
10006 vmap_end = vmap_start + size;
10007
10008 ret = vmap_page_range(vmap_start, vmap_end,
10009 start, pgprot_nx(PAGE_KERNEL));
10010 if (ret < 0) {
10011 free_vm_area(area);
10012 return 0;
e645535a 10013 }
e645535a 10014
394f3f02 10015 return (u64)vmap_start;
e645535a 10016}
4808595a
SR
10017#else
10018static inline u64 map_pages(unsigned long start, unsigned long size)
10019{
10020 return 0;
10021}
10022#endif
e645535a 10023
28879787
DI
10024/**
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.
d2356997 10027 * @systems: A list of systems to create event directories for (NULL for all)
28879787
DI
10028 *
10029 * Returns pointer to trace array with given name.
10030 * NULL, if it cannot be created.
10031 *
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.
28394da2
SRV
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.
28879787
DI
10039 *
10040 */
d2356997 10041struct trace_array *trace_array_get_by_name(const char *name, const char *systems)
28879787
DI
10042{
10043 struct trace_array *tr;
10044
d33b10c0
SR
10045 guard(mutex)(&event_mutex);
10046 guard(mutex)(&trace_types_lock);
28879787
DI
10047
10048 list_for_each_entry(tr, &ftrace_trace_arrays, list) {
d33b10c0
SR
10049 if (tr->name && strcmp(tr->name, name) == 0) {
10050 tr->ref++;
10051 return tr;
10052 }
28879787
DI
10053 }
10054
2124de79 10055 tr = trace_array_create_systems(name, systems, 0, 0);
28879787
DI
10056
10057 if (IS_ERR(tr))
10058 tr = NULL;
d33b10c0 10059 else
28879787
DI
10060 tr->ref++;
10061
28879787 10062 return tr;
277ba044 10063}
28879787 10064EXPORT_SYMBOL_GPL(trace_array_get_by_name);
277ba044 10065
f45d1225 10066static int __remove_instance(struct trace_array *tr)
0c8916c3 10067{
37aea98b 10068 int i;
0c8916c3 10069
28879787 10070 /* Reference counter for a newly created trace array = 1. */
7ef282e0 10071 if (tr->ref > 1 || (tr->current_trace && tr->trace_ref))
f45d1225 10072 return -EBUSY;
a695cb58 10073
0c8916c3
SR
10074 list_del(&tr->list);
10075
20550622
SRRH
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);
10080 }
10081
ddb8ea9e 10082 if (printk_trace == tr)
ef2bd81d 10083 update_printk_trace(&global_trace);
ddb8ea9e 10084
7b382efd
SR
10085 if (update_marker_trace(tr, 0))
10086 synchronize_rcu();
10087
6b450d25 10088 tracing_set_nop(tr);
a0e6369e 10089 clear_ftrace_function_probes(tr);
0c8916c3 10090 event_trace_del_tracer(tr);
d879d0b8 10091 ftrace_clear_pids(tr);
591dffda 10092 ftrace_destroy_function_files(tr);
a3d1e7eb 10093 tracefs_remove(tr->dir);
20344c54 10094 free_percpu(tr->last_func_repeats);
a9fcaaac 10095 free_trace_buffers(tr);
3357c6e4 10096 clear_tracing_err_log(tr);
0c8916c3 10097
fb6d0323
MHG
10098 if (tr->range_name) {
10099 reserve_mem_release_by_name(tr->range_name);
10100 kfree(tr->range_name);
10101 }
10102
37aea98b
SRRH
10103 for (i = 0; i < tr->nr_topts; i++) {
10104 kfree(tr->topts[i].topts);
10105 }
10106 kfree(tr->topts);
10107
c2489bb7 10108 free_cpumask_var(tr->pipe_cpumask);
db9108e0 10109 free_cpumask_var(tr->tracing_cpumask);
d2356997 10110 kfree_const(tr->system_names);
0c8916c3
SR
10111 kfree(tr->name);
10112 kfree(tr);
10113
f45d1225
DI
10114 return 0;
10115}
10116
e585e646 10117int trace_array_destroy(struct trace_array *this_tr)
f45d1225 10118{
e585e646 10119 struct trace_array *tr;
f45d1225 10120
e585e646 10121 if (!this_tr)
f45d1225
DI
10122 return -EINVAL;
10123
d33b10c0
SR
10124 guard(mutex)(&event_mutex);
10125 guard(mutex)(&trace_types_lock);
f45d1225 10126
e585e646
DI
10127
10128 /* Making sure trace array exists before destroying it. */
10129 list_for_each_entry(tr, &ftrace_trace_arrays, list) {
d33b10c0
SR
10130 if (tr == this_tr)
10131 return __remove_instance(tr);
e585e646 10132 }
f45d1225 10133
d33b10c0 10134 return -ENODEV;
f45d1225
DI
10135}
10136EXPORT_SYMBOL_GPL(trace_array_destroy);
10137
10138static int instance_rmdir(const char *name)
10139{
10140 struct trace_array *tr;
f45d1225 10141
d33b10c0
SR
10142 guard(mutex)(&event_mutex);
10143 guard(mutex)(&trace_types_lock);
f45d1225 10144
89c95fce 10145 tr = trace_array_find(name);
d33b10c0
SR
10146 if (!tr)
10147 return -ENODEV;
0c8916c3 10148
d33b10c0 10149 return __remove_instance(tr);
0c8916c3
SR
10150}
10151
277ba044
SR
10152static __init void create_trace_instances(struct dentry *d_tracer)
10153{
4114fbfd
MH
10154 struct trace_array *tr;
10155
eae47358
SRRH
10156 trace_instance_dir = tracefs_create_instance_dir("instances", d_tracer,
10157 instance_mkdir,
10158 instance_rmdir);
24589e3a 10159 if (MEM_FAIL(!trace_instance_dir, "Failed to create instances directory\n"))
277ba044 10160 return;
4114fbfd 10161
d33b10c0
SR
10162 guard(mutex)(&event_mutex);
10163 guard(mutex)(&trace_types_lock);
4114fbfd
MH
10164
10165 list_for_each_entry(tr, &ftrace_trace_arrays, list) {
10166 if (!tr->name)
10167 continue;
10168 if (MEM_FAIL(trace_array_create_dir(tr) < 0,
10169 "Failed to create instance directory\n"))
d33b10c0 10170 return;
4114fbfd 10171 }
277ba044
SR
10172}
10173
2b6080f2 10174static void
8434dc93 10175init_tracer_tracefs(struct trace_array *tr, struct dentry *d_tracer)
2b6080f2 10176{
121aaee7 10177 int cpu;
2b6080f2 10178
21ccc9cd 10179 trace_create_file("available_tracers", TRACE_MODE_READ, d_tracer,
607e2ea1
SRRH
10180 tr, &show_traces_fops);
10181
21ccc9cd 10182 trace_create_file("current_tracer", TRACE_MODE_WRITE, d_tracer,
607e2ea1
SRRH
10183 tr, &set_tracer_fops);
10184
21ccc9cd 10185 trace_create_file("tracing_cpumask", TRACE_MODE_WRITE, d_tracer,
ccfe9e42
AL
10186 tr, &tracing_cpumask_fops);
10187
21ccc9cd 10188 trace_create_file("trace_options", TRACE_MODE_WRITE, d_tracer,
2b6080f2
SR
10189 tr, &tracing_iter_fops);
10190
21ccc9cd 10191 trace_create_file("trace", TRACE_MODE_WRITE, d_tracer,
6484c71c 10192 tr, &tracing_fops);
2b6080f2 10193
21ccc9cd 10194 trace_create_file("trace_pipe", TRACE_MODE_READ, d_tracer,
15544209 10195 tr, &tracing_pipe_fops);
2b6080f2 10196
21ccc9cd 10197 trace_create_file("buffer_size_kb", TRACE_MODE_WRITE, d_tracer,
0bc392ee 10198 tr, &tracing_entries_fops);
2b6080f2 10199
21ccc9cd 10200 trace_create_file("buffer_total_size_kb", TRACE_MODE_READ, d_tracer,
2b6080f2
SR
10201 tr, &tracing_total_entries_fops);
10202
238ae93d 10203 trace_create_file("free_buffer", 0200, d_tracer,
2b6080f2
SR
10204 tr, &tracing_free_buffer_fops);
10205
10206 trace_create_file("trace_marker", 0220, d_tracer,
10207 tr, &tracing_mark_fops);
10208
5790b1fb 10209 tr->trace_marker_file = __find_event_file(tr, "ftrace", "print");
3dd80953 10210
fa32e855
SR
10211 trace_create_file("trace_marker_raw", 0220, d_tracer,
10212 tr, &tracing_mark_raw_fops);
10213
21ccc9cd 10214 trace_create_file("trace_clock", TRACE_MODE_WRITE, d_tracer, tr,
2b6080f2
SR
10215 &trace_clock_fops);
10216
21ccc9cd 10217 trace_create_file("tracing_on", TRACE_MODE_WRITE, d_tracer,
6484c71c 10218 tr, &rb_simple_fops);
ce9bae55 10219
21ccc9cd 10220 trace_create_file("timestamp_mode", TRACE_MODE_READ, d_tracer, tr,
2c1ea60b
TZ
10221 &trace_time_stamp_mode_fops);
10222
a7b1d74e 10223 tr->buffer_percent = 50;
03329f99 10224
4f94559f 10225 trace_create_file("buffer_percent", TRACE_MODE_WRITE, d_tracer,
03329f99
SRV
10226 tr, &buffer_percent_fops);
10227
2f84b39f
SRG
10228 trace_create_file("buffer_subbuf_size_kb", TRACE_MODE_WRITE, d_tracer,
10229 tr, &buffer_subbuf_size_fops);
2808e31e 10230
16270145
SRRH
10231 create_trace_options_dir(tr);
10232
e25e43a4 10233#ifdef CONFIG_TRACER_MAX_TRACE
91edde2e 10234 trace_create_maxlat_file(tr, d_tracer);
e25e43a4 10235#endif
6d9b3fa5 10236
591dffda 10237 if (ftrace_create_function_files(tr, d_tracer))
24589e3a 10238 MEM_FAIL(1, "Could not allocate function filter files");
591dffda 10239
7a1d1e4b
SRG
10240 if (tr->range_addr_start) {
10241 trace_create_file("last_boot_info", TRACE_MODE_READ, d_tracer,
10242 tr, &last_boot_fops);
ce9bae55 10243#ifdef CONFIG_TRACER_SNAPSHOT
7a1d1e4b 10244 } else {
2124de79
SRG
10245 trace_create_file("snapshot", TRACE_MODE_WRITE, d_tracer,
10246 tr, &snapshot_fops);
ce9bae55 10247#endif
6d02eefe 10248 }
121aaee7 10249
21ccc9cd 10250 trace_create_file("error_log", TRACE_MODE_WRITE, d_tracer,
8a062902
TZ
10251 tr, &tracing_err_log_fops);
10252
121aaee7 10253 for_each_tracing_cpu(cpu)
8434dc93 10254 tracing_init_tracefs_percpu(tr, cpu);
121aaee7 10255
345ddcc8 10256 ftrace_init_tracefs(tr, d_tracer);
2b6080f2
SR
10257}
10258
93faccbb 10259static struct vfsmount *trace_automount(struct dentry *mntpt, void *ingore)
f76180bc
SRRH
10260{
10261 struct vfsmount *mnt;
10262 struct file_system_type *type;
2dbf6e0d
AV
10263 struct fs_context *fc;
10264 int ret;
f76180bc
SRRH
10265
10266 /*
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.
10270 */
10271 type = get_fs_type("tracefs");
10272 if (!type)
10273 return NULL;
2dbf6e0d
AV
10274
10275 fc = fs_context_for_submount(type, mntpt);
f76180bc 10276 put_filesystem(type);
2dbf6e0d
AV
10277 if (IS_ERR(fc))
10278 return ERR_CAST(fc);
10279
10280 ret = vfs_parse_fs_string(fc, "source",
10281 "tracefs", strlen("tracefs"));
10282 if (!ret)
10283 mnt = fc_mount(fc);
10284 else
10285 mnt = ERR_PTR(ret);
f76180bc 10286
2dbf6e0d 10287 put_fs_context(fc);
f76180bc
SRRH
10288 return mnt;
10289}
10290
7eeafbca
SRRH
10291/**
10292 * tracing_init_dentry - initialize top level trace array
10293 *
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.
10297 */
22c36b18 10298int tracing_init_dentry(void)
7eeafbca
SRRH
10299{
10300 struct trace_array *tr = &global_trace;
10301
a356646a 10302 if (security_locked_down(LOCKDOWN_TRACEFS)) {
ee195452 10303 pr_warn("Tracing disabled due to lockdown\n");
22c36b18 10304 return -EPERM;
a356646a
SRV
10305 }
10306
f76180bc 10307 /* The top level trace array uses NULL as parent */
7eeafbca 10308 if (tr->dir)
22c36b18 10309 return 0;
7eeafbca 10310
072e133d 10311 if (WARN_ON(!tracefs_initialized()))
22c36b18 10312 return -ENODEV;
7eeafbca 10313
f76180bc
SRRH
10314 /*
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
f2cc020d 10318 * work with the newer kernel.
f76180bc
SRRH
10319 */
10320 tr->dir = debugfs_create_automount("tracing", NULL,
10321 trace_automount, NULL);
7eeafbca 10322
22c36b18 10323 return 0;
7eeafbca
SRRH
10324}
10325
00f4b652
JL
10326extern struct trace_eval_map *__start_ftrace_eval_maps[];
10327extern struct trace_eval_map *__stop_ftrace_eval_maps[];
0c564a53 10328
f6a69466
SRV
10329static struct workqueue_struct *eval_map_wq __initdata;
10330static struct work_struct eval_map_work __initdata;
6621a700 10331static struct work_struct tracerfs_init_work __initdata;
f6a69466
SRV
10332
10333static void __init eval_map_work_func(struct work_struct *work)
0c564a53 10334{
3673b8e4
SRRH
10335 int len;
10336
02fd7f68 10337 len = __stop_ftrace_eval_maps - __start_ftrace_eval_maps;
f57a4143 10338 trace_insert_eval_map(NULL, __start_ftrace_eval_maps, len);
3673b8e4
SRRH
10339}
10340
f6a69466
SRV
10341static int __init trace_eval_init(void)
10342{
10343 INIT_WORK(&eval_map_work, eval_map_work_func);
10344
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");
10348 /* Do work here */
10349 eval_map_work_func(&eval_map_work);
10350 return -ENOMEM;
10351 }
10352
10353 queue_work(eval_map_wq, &eval_map_work);
10354 return 0;
10355}
10356
6621a700
MPT
10357subsys_initcall(trace_eval_init);
10358
f6a69466
SRV
10359static int __init trace_eval_sync(void)
10360{
10361 /* Make sure the eval map updates are finished */
10362 if (eval_map_wq)
10363 destroy_workqueue(eval_map_wq);
10364 return 0;
10365}
10366
10367late_initcall_sync(trace_eval_sync);
10368
10369
3673b8e4 10370#ifdef CONFIG_MODULES
b355247d
SR
10371
10372bool module_exists(const char *module)
10373{
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];
10377 unsigned long val;
10378 int n;
10379
10380 n = snprintf(modname, sizeof(modname), "%s:%s", module, this_mod);
10381
10382 if (n > sizeof(modname) - 1)
10383 return false;
10384
10385 val = module_kallsyms_lookup_name(modname);
10386 return val != 0;
10387}
10388
f57a4143 10389static void trace_module_add_evals(struct module *mod)
3673b8e4 10390{
99be647c 10391 if (!mod->num_trace_evals)
3673b8e4
SRRH
10392 return;
10393
10394 /*
10395 * Modules with bad taint do not have events created, do
10396 * not bother with enums either.
10397 */
10398 if (trace_module_has_bad_taint(mod))
10399 return;
10400
f57a4143 10401 trace_insert_eval_map(mod, mod->trace_evals, mod->num_trace_evals);
3673b8e4
SRRH
10402}
10403
681bec03 10404#ifdef CONFIG_TRACE_EVAL_MAP_FILE
f57a4143 10405static void trace_module_remove_evals(struct module *mod)
9828413d 10406{
23bf8cb8
JL
10407 union trace_eval_map_item *map;
10408 union trace_eval_map_item **last = &trace_eval_maps;
9828413d 10409
99be647c 10410 if (!mod->num_trace_evals)
9828413d
SRRH
10411 return;
10412
d33b10c0 10413 guard(mutex)(&trace_eval_mutex);
9828413d 10414
23bf8cb8 10415 map = trace_eval_maps;
9828413d
SRRH
10416
10417 while (map) {
10418 if (map->head.mod == mod)
10419 break;
5f60b351 10420 map = trace_eval_jmp_to_tail(map);
9828413d
SRRH
10421 last = &map->tail.next;
10422 map = map->tail.next;
10423 }
10424 if (!map)
d33b10c0 10425 return;
9828413d 10426
5f60b351 10427 *last = trace_eval_jmp_to_tail(map)->tail.next;
9828413d 10428 kfree(map);
9828413d
SRRH
10429}
10430#else
f57a4143 10431static inline void trace_module_remove_evals(struct module *mod) { }
681bec03 10432#endif /* CONFIG_TRACE_EVAL_MAP_FILE */
9828413d 10433
35a380dd 10434static void trace_module_record(struct module *mod, bool add)
5f3719f6
SR
10435{
10436 struct trace_array *tr;
35a380dd 10437 unsigned long flags;
5f3719f6
SR
10438
10439 list_for_each_entry(tr, &ftrace_trace_arrays, list) {
35a380dd 10440 flags = tr->flags & (TRACE_ARRAY_FL_BOOT | TRACE_ARRAY_FL_LAST_BOOT);
5f3719f6 10441 /* Update any persistent trace array that has already been started */
35a380dd 10442 if (flags == TRACE_ARRAY_FL_BOOT && add) {
486fbcb3
MHG
10443 guard(mutex)(&scratch_mutex);
10444 save_mod(mod, tr);
35a380dd
MHG
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);
5f3719f6
SR
10448 }
10449 }
10450}
10451
3673b8e4
SRRH
10452static int trace_module_notify(struct notifier_block *self,
10453 unsigned long val, void *data)
10454{
10455 struct module *mod = data;
10456
10457 switch (val) {
10458 case MODULE_STATE_COMING:
f57a4143 10459 trace_module_add_evals(mod);
35a380dd 10460 trace_module_record(mod, true);
3673b8e4 10461 break;
9828413d 10462 case MODULE_STATE_GOING:
f57a4143 10463 trace_module_remove_evals(mod);
35a380dd 10464 trace_module_record(mod, false);
9828413d 10465 break;
3673b8e4
SRRH
10466 }
10467
0340a6b7 10468 return NOTIFY_OK;
0c564a53
SRRH
10469}
10470
3673b8e4
SRRH
10471static struct notifier_block trace_module_nb = {
10472 .notifier_call = trace_module_notify,
10473 .priority = 0,
10474};
9828413d 10475#endif /* CONFIG_MODULES */
3673b8e4 10476
6621a700 10477static __init void tracer_init_tracefs_work_func(struct work_struct *work)
bc0c38d1 10478{
bc0c38d1 10479
58b92547
SRV
10480 event_trace_init();
10481
22c36b18
WY
10482 init_tracer_tracefs(&global_trace, NULL);
10483 ftrace_init_tracefs_toplevel(&global_trace, NULL);
bc0c38d1 10484
21ccc9cd 10485 trace_create_file("tracing_thresh", TRACE_MODE_WRITE, NULL,
6508fa76 10486 &global_trace, &tracing_thresh_fops);
a8259075 10487
21ccc9cd 10488 trace_create_file("README", TRACE_MODE_READ, NULL,
5452af66
FW
10489 NULL, &tracing_readme_fops);
10490
21ccc9cd 10491 trace_create_file("saved_cmdlines", TRACE_MODE_READ, NULL,
69abe6a5 10492 NULL, &tracing_saved_cmdlines_fops);
5bf9a1ee 10493
21ccc9cd 10494 trace_create_file("saved_cmdlines_size", TRACE_MODE_WRITE, NULL,
939c7a4f
YY
10495 NULL, &tracing_saved_cmdlines_size_fops);
10496
21ccc9cd 10497 trace_create_file("saved_tgids", TRACE_MODE_READ, NULL,
99c621d7
MS
10498 NULL, &tracing_saved_tgids_fops);
10499
22c36b18 10500 trace_create_eval_file(NULL);
9828413d 10501
3673b8e4
SRRH
10502#ifdef CONFIG_MODULES
10503 register_module_notifier(&trace_module_nb);
10504#endif
10505
bc0c38d1 10506#ifdef CONFIG_DYNAMIC_FTRACE
21ccc9cd 10507 trace_create_file("dyn_ftrace_total_info", TRACE_MODE_READ, NULL,
da537f0a 10508 NULL, &tracing_dyn_info_fops);
bc0c38d1 10509#endif
b04cc6b1 10510
22c36b18 10511 create_trace_instances(NULL);
5452af66 10512
37aea98b 10513 update_tracer_options(&global_trace);
6621a700
MPT
10514}
10515
10516static __init int tracer_init_tracefs(void)
10517{
10518 int ret;
10519
10520 trace_access_lock_init();
10521
10522 ret = tracing_init_dentry();
10523 if (ret)
10524 return 0;
10525
10526 if (eval_map_wq) {
10527 INIT_WORK(&tracerfs_init_work, tracer_init_tracefs_work_func);
10528 queue_work(eval_map_wq, &tracerfs_init_work);
10529 } else {
10530 tracer_init_tracefs_work_func(NULL);
10531 }
09d23a1d 10532
102227b9
DBO
10533 rv_init_interface();
10534
b5ad384e 10535 return 0;
bc0c38d1
SR
10536}
10537
f3860136
SRV
10538fs_initcall(tracer_init_tracefs);
10539
ea47666c
GP
10540static int trace_die_panic_handler(struct notifier_block *self,
10541 unsigned long ev, void *unused);
3f5a54e3
SR
10542
10543static struct notifier_block trace_panic_notifier = {
ea47666c
GP
10544 .notifier_call = trace_die_panic_handler,
10545 .priority = INT_MAX - 1,
3f5a54e3
SR
10546};
10547
3f5a54e3 10548static struct notifier_block trace_die_notifier = {
ea47666c
GP
10549 .notifier_call = trace_die_panic_handler,
10550 .priority = INT_MAX - 1,
3f5a54e3
SR
10551};
10552
ea47666c
GP
10553/*
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).
10558 */
10559static int trace_die_panic_handler(struct notifier_block *self,
10560 unsigned long ev, void *unused)
10561{
19f0423f 10562 if (!ftrace_dump_on_oops_enabled())
ea47666c
GP
10563 return NOTIFY_DONE;
10564
10565 /* The die notifier requires DIE_OOPS to trigger */
10566 if (self == &trace_die_notifier && ev != DIE_OOPS)
10567 return NOTIFY_DONE;
10568
19f0423f 10569 ftrace_dump(DUMP_PARAM);
ea47666c
GP
10570
10571 return NOTIFY_DONE;
10572}
10573
3f5a54e3
SR
10574/*
10575 * printk is set to max of 1024, we really don't need it that big.
10576 * Nothing should be printing 1000 characters anyway.
10577 */
10578#define TRACE_MAX_PRINT 1000
10579
10580/*
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
10583 * should be at.
10584 */
428aee14 10585#define KERN_TRACE KERN_EMERG
3f5a54e3 10586
955b61e5 10587void
3f5a54e3
SR
10588trace_printk_seq(struct trace_seq *s)
10589{
10590 /* Probably should print a warning here. */
3a161d99
SRRH
10591 if (s->seq.len >= TRACE_MAX_PRINT)
10592 s->seq.len = TRACE_MAX_PRINT;
3f5a54e3 10593
820b75f6
SRRH
10594 /*
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.
10598 */
10599 if (WARN_ON_ONCE(s->seq.len >= s->seq.size))
10600 s->seq.len = s->seq.size - 1;
3f5a54e3
SR
10601
10602 /* should be zero ended, but we are paranoid. */
3a161d99 10603 s->buffer[s->seq.len] = 0;
3f5a54e3
SR
10604
10605 printk(KERN_TRACE "%s", s->buffer);
10606
f9520750 10607 trace_seq_init(s);
3f5a54e3
SR
10608}
10609
19f0423f 10610static void trace_init_iter(struct trace_iterator *iter, struct trace_array *tr)
955b61e5 10611{
19f0423f 10612 iter->tr = tr;
2b6080f2 10613 iter->trace = iter->tr->current_trace;
ae3b5093 10614 iter->cpu_file = RING_BUFFER_ALL_CPUS;
19f0423f 10615 iter->array_buffer = &tr->array_buffer;
b2f974d6
CS
10616
10617 if (iter->trace && iter->trace->open)
10618 iter->trace->open(iter);
10619
10620 /* Annotate start of buffers if we had overruns */
1c5eb448 10621 if (ring_buffer_overruns(iter->array_buffer->buffer))
b2f974d6
CS
10622 iter->iter_flags |= TRACE_FILE_ANNOTATE;
10623
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;
495fcec8
DA
10627
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;
955b61e5
JW
10633}
10634
19f0423f
HY
10635void trace_init_global_iter(struct trace_iterator *iter)
10636{
10637 trace_init_iter(iter, &global_trace);
10638}
10639
10640static void ftrace_dump_one(struct trace_array *tr, enum ftrace_dump_mode dump_mode)
3f5a54e3 10641{
3f5a54e3
SR
10642 /* use static because iter can be a bit big for the stack */
10643 static struct trace_iterator iter;
cf586b61 10644 unsigned int old_userobj;
d769041f 10645 unsigned long flags;
6ba3e053 10646 int cnt = 0;
3f5a54e3 10647
7fe70b57
SRRH
10648 /*
10649 * Always turn off tracing when we dump.
10650 * We don't need to show trace output of what happens
10651 * between multiple crashes.
10652 *
10653 * If the user does a sysrq-z, then they can re-enable
10654 * tracing with echo 1 > tracing_on.
10655 */
19f0423f 10656 tracer_tracing_off(tr);
cf586b61 10657
7fe70b57 10658 local_irq_save(flags);
3f5a54e3 10659
38dbe0b1 10660 /* Simulate the iterator */
19f0423f 10661 trace_init_iter(&iter, tr);
955b61e5 10662
6ba3e053
SR
10663 /* While dumping, do not allow the buffer to be enable */
10664 tracer_tracing_disable(tr);
d769041f 10665
983f938a 10666 old_userobj = tr->trace_flags & TRACE_ITER_SYM_USEROBJ;
cf586b61 10667
b54d3de9 10668 /* don't look at user memory in panic mode */
983f938a 10669 tr->trace_flags &= ~TRACE_ITER_SYM_USEROBJ;
b54d3de9 10670
19f0423f 10671 if (dump_mode == DUMP_ORIG)
cecbca96 10672 iter.cpu_file = raw_smp_processor_id();
19f0423f 10673 else
ae3b5093 10674 iter.cpu_file = RING_BUFFER_ALL_CPUS;
cecbca96 10675
19f0423f
HY
10676 if (tr == &global_trace)
10677 printk(KERN_TRACE "Dumping ftrace buffer:\n");
10678 else
10679 printk(KERN_TRACE "Dumping ftrace instance %s buffer:\n", tr->name);
3f5a54e3 10680
7fe70b57
SRRH
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");
10685 }
10686
3f5a54e3 10687 /*
5c8c206e 10688 * We need to stop all tracing on all CPUS to read
3f5a54e3
SR
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.
10692 */
10693
3f5a54e3
SR
10694 while (!trace_empty(&iter)) {
10695
10696 if (!cnt)
10697 printk(KERN_TRACE "---------------------------------\n");
10698
10699 cnt++;
10700
0c97bf86 10701 trace_iterator_reset(&iter);
3f5a54e3 10702 iter.iter_flags |= TRACE_FILE_LAT_FMT;
3f5a54e3 10703
955b61e5 10704 if (trace_find_next_entry_inc(&iter) != NULL) {
74e7ff8c
LJ
10705 int ret;
10706
10707 ret = print_trace_line(&iter);
10708 if (ret != TRACE_TYPE_NO_CONSUME)
10709 trace_consume(&iter);
3f5a54e3 10710 }
b892e5c8 10711 touch_nmi_watchdog();
3f5a54e3
SR
10712
10713 trace_printk_seq(&iter.seq);
10714 }
10715
10716 if (!cnt)
10717 printk(KERN_TRACE " (ftrace buffer empty)\n");
10718 else
10719 printk(KERN_TRACE "---------------------------------\n");
10720
983f938a 10721 tr->trace_flags |= old_userobj;
cf586b61 10722
6ba3e053 10723 tracer_tracing_enable(tr);
cd891ae0 10724 local_irq_restore(flags);
3f5a54e3 10725}
19f0423f
HY
10726
10727static void ftrace_dump_by_param(void)
10728{
10729 bool first_param = true;
10730 char dump_param[MAX_TRACER_SIZE];
10731 char *buf, *token, *inst_name;
10732 struct trace_array *tr;
10733
10734 strscpy(dump_param, ftrace_dump_on_oops, MAX_TRACER_SIZE);
10735 buf = dump_param;
10736
10737 while ((token = strsep(&buf, ",")) != NULL) {
10738 if (first_param) {
10739 first_param = false;
10740 if (!strcmp("0", token))
10741 continue;
10742 else if (!strcmp("1", token)) {
10743 ftrace_dump_one(&global_trace, DUMP_ALL);
10744 continue;
10745 }
10746 else if (!strcmp("2", token) ||
10747 !strcmp("orig_cpu", token)) {
10748 ftrace_dump_one(&global_trace, DUMP_ORIG);
10749 continue;
10750 }
10751 }
10752
10753 inst_name = strsep(&token, "=");
10754 tr = trace_array_find(inst_name);
10755 if (!tr) {
10756 printk(KERN_TRACE "Instance %s not found\n", inst_name);
10757 continue;
10758 }
10759
10760 if (token && (!strcmp("2", token) ||
10761 !strcmp("orig_cpu", token)))
10762 ftrace_dump_one(tr, DUMP_ORIG);
10763 else
10764 ftrace_dump_one(tr, DUMP_ALL);
10765 }
10766}
10767
10768void ftrace_dump(enum ftrace_dump_mode oops_dump_mode)
10769{
10770 static atomic_t dump_running;
10771
10772 /* Only allow one dump user at a time. */
10773 if (atomic_inc_return(&dump_running) != 1) {
10774 atomic_dec(&dump_running);
10775 return;
10776 }
10777
10778 switch (oops_dump_mode) {
10779 case DUMP_ALL:
10780 ftrace_dump_one(&global_trace, DUMP_ALL);
10781 break;
10782 case DUMP_ORIG:
10783 ftrace_dump_one(&global_trace, DUMP_ORIG);
10784 break;
10785 case DUMP_PARAM:
10786 ftrace_dump_by_param();
10787 break;
10788 case DUMP_NONE:
10789 break;
10790 default:
10791 printk(KERN_TRACE "Bad dumping mode, switching to all CPUs dump\n");
10792 ftrace_dump_one(&global_trace, DUMP_ALL);
10793 }
10794
10795 atomic_dec(&dump_running);
10796}
a8eecf22 10797EXPORT_SYMBOL_GPL(ftrace_dump);
cf586b61 10798
7e465baa
TZ
10799#define WRITE_BUFSIZE 4096
10800
10801ssize_t trace_parse_run_command(struct file *file, const char __user *buffer,
10802 size_t count, loff_t *ppos,
d262271d 10803 int (*createfn)(const char *))
7e465baa
TZ
10804{
10805 char *kbuf, *buf, *tmp;
10806 int ret = 0;
10807 size_t done = 0;
10808 size_t size;
10809
10810 kbuf = kmalloc(WRITE_BUFSIZE, GFP_KERNEL);
10811 if (!kbuf)
10812 return -ENOMEM;
10813
10814 while (done < count) {
10815 size = count - done;
10816
10817 if (size >= WRITE_BUFSIZE)
10818 size = WRITE_BUFSIZE - 1;
10819
10820 if (copy_from_user(kbuf, buffer + done, size)) {
10821 ret = -EFAULT;
10822 goto out;
10823 }
10824 kbuf[size] = '\0';
10825 buf = kbuf;
10826 do {
10827 tmp = strchr(buf, '\n');
10828 if (tmp) {
10829 *tmp = '\0';
10830 size = tmp - buf + 1;
10831 } else {
10832 size = strlen(buf);
10833 if (done + size < count) {
10834 if (buf != kbuf)
10835 break;
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);
10839 ret = -EINVAL;
10840 goto out;
10841 }
10842 }
10843 done += size;
10844
10845 /* Remove comments */
10846 tmp = strchr(buf, '#');
10847
10848 if (tmp)
10849 *tmp = '\0';
10850
d262271d 10851 ret = createfn(buf);
7e465baa
TZ
10852 if (ret)
10853 goto out;
10854 buf += size;
10855
10856 } while (done < count);
10857 }
10858 ret = done;
10859
10860out:
10861 kfree(kbuf);
10862
10863 return ret;
10864}
10865
9c1c251d
SRG
10866#ifdef CONFIG_TRACER_MAX_TRACE
10867__init static bool tr_needs_alloc_snapshot(const char *name)
10868{
10869 char *test;
10870 int len = strlen(name);
10871 bool ret;
10872
10873 if (!boot_snapshot_index)
10874 return false;
10875
10876 if (strncmp(name, boot_snapshot_info, len) == 0 &&
10877 boot_snapshot_info[len] == '\t')
10878 return true;
10879
10880 test = kmalloc(strlen(name) + 3, GFP_KERNEL);
10881 if (!test)
10882 return false;
10883
10884 sprintf(test, "\t%s\t", name);
10885 ret = strstr(boot_snapshot_info, test) == NULL;
10886 kfree(test);
10887 return ret;
10888}
10889
10890__init static void do_allocate_snapshot(const char *name)
10891{
10892 if (!tr_needs_alloc_snapshot(name))
10893 return;
10894
10895 /*
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
10899 * this flag.
10900 */
10901 allocate_snapshot = true;
10902}
10903#else
10904static inline void do_allocate_snapshot(const char *name) { }
10905#endif
10906
cb1f98c5
SRG
10907__init static void enable_instances(void)
10908{
10909 struct trace_array *tr;
34ea8fa0 10910 bool memmap_area = false;
cb1f98c5 10911 char *curr_str;
e645535a 10912 char *name;
cb1f98c5
SRG
10913 char *str;
10914 char *tok;
10915
10916 /* A tab is always appended */
10917 boot_instance_info[boot_instance_index - 1] = '\0';
10918 str = boot_instance_info;
10919
10920 while ((curr_str = strsep(&str, "\t"))) {
29a02ec6
SRG
10921 phys_addr_t start = 0;
10922 phys_addr_t size = 0;
e645535a 10923 unsigned long addr = 0;
ddb8ea9e 10924 bool traceprintk = false;
b6fc31b6
SR
10925 bool traceoff = false;
10926 char *flag_delim;
10927 char *addr_delim;
fb6d0323 10928 char *rname __free(kfree) = NULL;
cb1f98c5
SRG
10929
10930 tok = strsep(&curr_str, ",");
10931
b6fc31b6
SR
10932 flag_delim = strchr(tok, '^');
10933 addr_delim = strchr(tok, '@');
9c1c251d 10934
b6fc31b6
SR
10935 if (addr_delim)
10936 *addr_delim++ = '\0';
10937
10938 if (flag_delim)
10939 *flag_delim++ = '\0';
10940
10941 name = tok;
10942
10943 if (flag_delim) {
10944 char *flag;
10945
10946 while ((flag = strsep(&flag_delim, "^"))) {
ddb8ea9e 10947 if (strcmp(flag, "traceoff") == 0) {
b6fc31b6 10948 traceoff = true;
ddb8ea9e
SR
10949 } else if ((strcmp(flag, "printk") == 0) ||
10950 (strcmp(flag, "traceprintk") == 0) ||
10951 (strcmp(flag, "trace_printk") == 0)) {
10952 traceprintk = true;
10953 } else {
b6fc31b6
SR
10954 pr_info("Tracing: Invalid instance flag '%s' for %s\n",
10955 flag, name);
ddb8ea9e 10956 }
b6fc31b6
SR
10957 }
10958 }
10959
10960 tok = addr_delim;
29a02ec6 10961 if (tok && isdigit(*tok)) {
e645535a
SRG
10962 start = memparse(tok, &tok);
10963 if (!start) {
10964 pr_warn("Tracing: Invalid boot instance address for %s\n",
10965 name);
10966 continue;
10967 }
e645535a
SRG
10968 if (*tok != ':') {
10969 pr_warn("Tracing: No size specified for instance %s\n", name);
10970 continue;
10971 }
10972 tok++;
10973 size = memparse(tok, &tok);
10974 if (!size) {
10975 pr_warn("Tracing: Invalid boot instance size for %s\n",
10976 name);
10977 continue;
10978 }
34ea8fa0 10979 memmap_area = true;
29a02ec6
SRG
10980 } else if (tok) {
10981 if (!reserve_mem_find_by_name(tok, &start, &size)) {
10982 start = 0;
10983 pr_warn("Failed to map boot instance %s to %s\n", name, tok);
10984 continue;
10985 }
fb6d0323 10986 rname = kstrdup(tok, GFP_KERNEL);
29a02ec6
SRG
10987 }
10988
10989 if (start) {
c44a14f2
SR
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);
10993 continue;
10994 }
10995 if (size & ~PAGE_MASK) {
10996 pr_warn("Tracing: mapping size %pa is not page aligned\n", &size);
10997 continue;
10998 }
10999
34ea8fa0
SR
11000 if (memmap_area)
11001 addr = map_pages(start, size);
11002 else
11003 addr = (unsigned long)phys_to_virt(start);
e645535a 11004 if (addr) {
29a02ec6
SRG
11005 pr_info("Tracing: mapped boot instance %s at physical memory %pa of size 0x%lx\n",
11006 name, &start, (unsigned long)size);
e645535a
SRG
11007 } else {
11008 pr_warn("Tracing: Failed to map boot instance %s\n", name);
11009 continue;
11010 }
11011 } else {
11012 /* Only non mapped buffers have snapshot buffers */
11013 if (IS_ENABLED(CONFIG_TRACER_MAX_TRACE))
11014 do_allocate_snapshot(name);
11015 }
9c1c251d 11016
e645535a 11017 tr = trace_array_create_systems(name, NULL, addr, size);
94dfa500 11018 if (IS_ERR(tr)) {
e645535a 11019 pr_warn("Tracing: Failed to create instance buffer %s\n", curr_str);
cb1f98c5
SRG
11020 continue;
11021 }
e645535a 11022
b6fc31b6
SR
11023 if (traceoff)
11024 tracer_tracing_off(tr);
11025
9b7bdf6f 11026 if (traceprintk)
ef2bd81d 11027 update_printk_trace(tr);
ddb8ea9e 11028
9b7bdf6f 11029 /*
34ea8fa0 11030 * memmap'd buffers can not be freed.
9b7bdf6f 11031 */
34ea8fa0
SR
11032 if (memmap_area) {
11033 tr->flags |= TRACE_ARRAY_FL_MEMMAP;
11034 tr->ref++;
11035 }
11036
2cf97338 11037 if (start) {
bcba8d4d 11038 tr->flags |= TRACE_ARRAY_FL_BOOT | TRACE_ARRAY_FL_LAST_BOOT;
fb6d0323 11039 tr->range_name = no_free_ptr(rname);
2cf97338 11040 }
c4846480
SRG
11041
11042 while ((tok = strsep(&curr_str, ","))) {
11043 early_enable_events(tr, tok, true);
11044 }
cb1f98c5
SRG
11045 }
11046}
11047
3928a8a2 11048__init static int tracer_alloc_buffers(void)
bc0c38d1 11049{
73c5162a 11050 int ring_buf_size;
9e01c1b7 11051 int ret = -ENOMEM;
4c11d7ae 11052
a356646a
SRV
11053
11054 if (security_locked_down(LOCKDOWN_TRACEFS)) {
ee195452 11055 pr_warn("Tracing disabled due to lockdown\n");
a356646a
SRV
11056 return -EPERM;
11057 }
11058
b5e87c05 11059 /*
499f7bb0 11060 * Make sure we don't accidentally add more trace options
b5e87c05
SRRH
11061 * than we have bits for.
11062 */
9a38a885 11063 BUILD_BUG_ON(TRACE_ITER_LAST_BIT > TRACE_FLAGS_MAX_SIZE);
b5e87c05 11064
9e01c1b7
RR
11065 if (!alloc_cpumask_var(&tracing_buffer_mask, GFP_KERNEL))
11066 goto out;
11067
ccfe9e42 11068 if (!alloc_cpumask_var(&global_trace.tracing_cpumask, GFP_KERNEL))
9e01c1b7 11069 goto out_free_buffer_mask;
4c11d7ae 11070
07d777fe 11071 /* Only allocate trace_printk buffers if a trace_printk exists */
bf2cbe04 11072 if (&__stop___trace_bprintk_fmt != &__start___trace_bprintk_fmt)
81698831 11073 /* Must be called before global_trace.buffer is allocated */
07d777fe
SR
11074 trace_printk_init_buffers();
11075
73c5162a 11076 /* To save memory, keep the ring buffer size to its minimum */
a1f157c7 11077 if (global_trace.ring_buffer_expanded)
73c5162a
SR
11078 ring_buf_size = trace_buf_size;
11079 else
11080 ring_buf_size = 1;
11081
9e01c1b7 11082 cpumask_copy(tracing_buffer_mask, cpu_possible_mask);
ccfe9e42 11083 cpumask_copy(global_trace.tracing_cpumask, cpu_all_mask);
9e01c1b7 11084
2b6080f2
SR
11085 raw_spin_lock_init(&global_trace.start_lock);
11086
b32614c0
SAS
11087 /*
11088 * The prepare callbacks allocates some memory for the ring buffer. We
499f7bb0 11089 * don't free the buffer if the CPU goes down. If we were to free
b32614c0
SAS
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.
11092 */
11093 ret = cpuhp_setup_state_multi(CPUHP_TRACE_RB_PREPARE,
e841e8bf 11094 "trace/RB:prepare", trace_rb_cpu_prepare,
b32614c0
SAS
11095 NULL);
11096 if (ret < 0)
11097 goto out_free_cpumask;
2c4a33ab 11098 /* Used for event triggers */
147d88e0 11099 ret = -ENOMEM;
2c4a33ab
SRRH
11100 temp_buffer = ring_buffer_alloc(PAGE_SIZE, RB_FL_OVERWRITE);
11101 if (!temp_buffer)
b32614c0 11102 goto out_rm_hp_state;
2c4a33ab 11103
939c7a4f
YY
11104 if (trace_create_savedcmd() < 0)
11105 goto out_free_temp_buffer;
11106
3d07fa1d 11107 if (!zalloc_cpumask_var(&global_trace.pipe_cpumask, GFP_KERNEL))
c2489bb7
ZY
11108 goto out_free_savedcmd;
11109
9e01c1b7 11110 /* TODO: make the number of buffers hot pluggable with CPUS */
737223fb 11111 if (allocate_trace_buffers(&global_trace, ring_buf_size) < 0) {
24589e3a 11112 MEM_FAIL(1, "tracer: failed to allocate ring buffer!\n");
c2489bb7 11113 goto out_free_pipe_cpumask;
4c11d7ae 11114 }
499e5470
SR
11115 if (global_trace.buffer_disabled)
11116 tracing_off();
4c11d7ae 11117
e1e232ca
SR
11118 if (trace_boot_clock) {
11119 ret = tracing_set_clock(&global_trace, trace_boot_clock);
11120 if (ret < 0)
a395d6a7
JP
11121 pr_warn("Trace clock %s not defined, going back to default\n",
11122 trace_boot_clock);
e1e232ca
SR
11123 }
11124
ca164318
SRRH
11125 /*
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.
11129 */
2b6080f2
SR
11130 global_trace.current_trace = &nop_trace;
11131
0b9b12c1 11132 global_trace.max_lock = (arch_spinlock_t)__ARCH_SPIN_LOCK_UNLOCKED;
180e4e39
VD
11133#ifdef CONFIG_TRACER_MAX_TRACE
11134 spin_lock_init(&global_trace.snapshot_trigger_lock);
11135#endif
4104d326
SRRH
11136 ftrace_init_global_array_ops(&global_trace);
11137
b355247d
SR
11138#ifdef CONFIG_MODULES
11139 INIT_LIST_HEAD(&global_trace.mod_events);
11140#endif
11141
9a38a885
SRRH
11142 init_trace_flags_index(&global_trace);
11143
ca164318
SRRH
11144 register_tracer(&nop_trace);
11145
dbeafd0d
SRV
11146 /* Function tracing may start here (via kernel command line) */
11147 init_function_trace();
11148
60a11774
SR
11149 /* All seems OK, enable tracing */
11150 tracing_disabled = 0;
3928a8a2 11151
3f5a54e3
SR
11152 atomic_notifier_chain_register(&panic_notifier_list,
11153 &trace_panic_notifier);
11154
11155 register_die_notifier(&trace_die_notifier);
2fc1dfbe 11156
ae63b31e
SR
11157 global_trace.flags = TRACE_ARRAY_FL_GLOBAL;
11158
11159 INIT_LIST_HEAD(&global_trace.systems);
11160 INIT_LIST_HEAD(&global_trace.events);
067fe038 11161 INIT_LIST_HEAD(&global_trace.hist_vars);
2f754e77 11162 INIT_LIST_HEAD(&global_trace.err_log);
7b382efd 11163 list_add(&global_trace.marker_list, &marker_copies);
ae63b31e
SR
11164 list_add(&global_trace.list, &ftrace_trace_arrays);
11165
a4d1e688 11166 apply_trace_boot_options();
7bcfaf54 11167
77fd5c15
SRRH
11168 register_snapshot_cmd();
11169
2fc1dfbe 11170 return 0;
3f5a54e3 11171
c2489bb7
ZY
11172out_free_pipe_cpumask:
11173 free_cpumask_var(global_trace.pipe_cpumask);
939c7a4f 11174out_free_savedcmd:
2cc621fd 11175 trace_free_saved_cmdlines_buffer();
2c4a33ab
SRRH
11176out_free_temp_buffer:
11177 ring_buffer_free(temp_buffer);
b32614c0
SAS
11178out_rm_hp_state:
11179 cpuhp_remove_multi_state(CPUHP_TRACE_RB_PREPARE);
9e01c1b7 11180out_free_cpumask:
ccfe9e42 11181 free_cpumask_var(global_trace.tracing_cpumask);
9e01c1b7
RR
11182out_free_buffer_mask:
11183 free_cpumask_var(tracing_buffer_mask);
11184out:
11185 return ret;
bc0c38d1 11186}
b2821ae6 11187
31f505dc
SR
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)
11191{
11192 return &global_trace;
11193}
11194#endif
11195
380af29b
SRG
11196void __init ftrace_boot_snapshot(void)
11197{
e9489164 11198#ifdef CONFIG_TRACER_MAX_TRACE
9c1c251d
SRG
11199 struct trace_array *tr;
11200
e9489164
SRG
11201 if (!snapshot_at_boot)
11202 return;
9c1c251d
SRG
11203
11204 list_for_each_entry(tr, &ftrace_trace_arrays, list) {
e9489164 11205 if (!tr->allocated_snapshot)
9c1c251d 11206 continue;
e9489164 11207
9c1c251d 11208 tracing_snapshot_instance(tr);
e9489164 11209 trace_array_puts(tr, "** Boot snapshot taken **\n");
9c1c251d 11210 }
e9489164 11211#endif
380af29b
SRG
11212}
11213
e725c731 11214void __init early_trace_init(void)
5f893b26 11215{
0daa2302
SRRH
11216 if (tracepoint_printk) {
11217 tracepoint_print_iter =
0e1e71d3 11218 kzalloc(sizeof(*tracepoint_print_iter), GFP_KERNEL);
24589e3a
SRV
11219 if (MEM_FAIL(!tracepoint_print_iter,
11220 "Failed to allocate trace iterator\n"))
0daa2302 11221 tracepoint_printk = 0;
42391745
SRRH
11222 else
11223 static_key_enable(&tracepoint_printk_key.key);
0daa2302 11224 }
5f893b26 11225 tracer_alloc_buffers();
3bb06eb6
SRG
11226
11227 init_events();
e725c731
SRV
11228}
11229
11230void __init trace_init(void)
11231{
0c564a53 11232 trace_event_init();
cb1f98c5
SRG
11233
11234 if (boot_instance_index)
11235 enable_instances();
5f893b26
SRRH
11236}
11237
f3860136 11238__init static void clear_boot_tracer(void)
b2821ae6
SR
11239{
11240 /*
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.
11246 */
11247 if (!default_bootup_tracer)
f3860136 11248 return;
b2821ae6
SR
11249
11250 printk(KERN_INFO "ftrace bootup tracer '%s' not registered.\n",
11251 default_bootup_tracer);
11252 default_bootup_tracer = NULL;
b2821ae6
SR
11253}
11254
3fd49c9e 11255#ifdef CONFIG_HAVE_UNSTABLE_SCHED_CLOCK
f3860136 11256__init static void tracing_set_default_clock(void)
3fd49c9e
CW
11257{
11258 /* sched_clock_stable() is determined in late_initcall */
5125eee4 11259 if (!trace_boot_clock && !sched_clock_stable()) {
bf24daac
MI
11260 if (security_locked_down(LOCKDOWN_TRACEFS)) {
11261 pr_warn("Can not set tracing clock due to lockdown\n");
f3860136 11262 return;
bf24daac
MI
11263 }
11264
3fd49c9e
CW
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");
11271 }
f3860136
SRV
11272}
11273#else
11274static inline void tracing_set_default_clock(void) { }
11275#endif
3fd49c9e 11276
f3860136
SRV
11277__init static int late_trace_init(void)
11278{
11279 if (tracepoint_printk && tracepoint_printk_stop_on_boot) {
11280 static_key_disable(&tracepoint_printk_key.key);
11281 tracepoint_printk = 0;
11282 }
3fd49c9e 11283
937fbf11
SR
11284 if (traceoff_after_boot)
11285 tracing_off();
11286
f3860136
SRV
11287 tracing_set_default_clock();
11288 clear_boot_tracer();
3fd49c9e
CW
11289 return 0;
11290}
f3860136
SRV
11291
11292late_initcall_sync(late_trace_init);