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