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