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