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