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