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