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