tracing: Fix the branch tracer that broke with buffer change
[linux-2.6-block.git] / kernel / trace / trace.c
CommitLineData
bc0c38d1
SR
1/*
2 * ring buffer based function tracer
3 *
2b6080f2 4 * Copyright (C) 2007-2012 Steven Rostedt <srostedt@redhat.com>
bc0c38d1
SR
5 * Copyright (C) 2008 Ingo Molnar <mingo@redhat.com>
6 *
7 * Originally taken from the RT patch by:
8 * Arnaldo Carvalho de Melo <acme@redhat.com>
9 *
10 * Based on code from the latency_tracer, that is:
11 * Copyright (C) 2004-2006 Ingo Molnar
6d49e352 12 * Copyright (C) 2004 Nadia Yvette Chambers
bc0c38d1 13 */
2cadf913 14#include <linux/ring_buffer.h>
273b281f 15#include <generated/utsrelease.h>
2cadf913
SR
16#include <linux/stacktrace.h>
17#include <linux/writeback.h>
bc0c38d1
SR
18#include <linux/kallsyms.h>
19#include <linux/seq_file.h>
3f5a54e3 20#include <linux/notifier.h>
2cadf913 21#include <linux/irqflags.h>
bc0c38d1 22#include <linux/debugfs.h>
4c11d7ae 23#include <linux/pagemap.h>
bc0c38d1
SR
24#include <linux/hardirq.h>
25#include <linux/linkage.h>
26#include <linux/uaccess.h>
2cadf913 27#include <linux/kprobes.h>
bc0c38d1
SR
28#include <linux/ftrace.h>
29#include <linux/module.h>
30#include <linux/percpu.h>
2cadf913 31#include <linux/splice.h>
3f5a54e3 32#include <linux/kdebug.h>
5f0c6c03 33#include <linux/string.h>
7e53bd42 34#include <linux/rwsem.h>
5a0e3ad6 35#include <linux/slab.h>
bc0c38d1
SR
36#include <linux/ctype.h>
37#include <linux/init.h>
2a2cc8f7 38#include <linux/poll.h>
b892e5c8 39#include <linux/nmi.h>
bc0c38d1 40#include <linux/fs.h>
8bd75c77 41#include <linux/sched/rt.h>
86387f7e 42
bc0c38d1 43#include "trace.h"
f0868d1e 44#include "trace_output.h"
bc0c38d1 45
73c5162a
SR
46/*
47 * On boot up, the ring buffer is set to the minimum size, so that
48 * we do not waste memory on systems that are not using tracing.
49 */
55034cd6 50bool ring_buffer_expanded;
73c5162a 51
8e1b82e0
FW
52/*
53 * We need to change this state when a selftest is running.
ff32504f
FW
54 * A selftest will lurk into the ring-buffer to count the
55 * entries inserted during the selftest although some concurrent
5e1607a0 56 * insertions into the ring-buffer such as trace_printk could occurred
ff32504f
FW
57 * at the same time, giving false positive or negative results.
58 */
8e1b82e0 59static bool __read_mostly tracing_selftest_running;
ff32504f 60
b2821ae6
SR
61/*
62 * If a tracer is running, we do not want to run SELFTEST.
63 */
020e5f85 64bool __read_mostly tracing_selftest_disabled;
b2821ae6 65
adf9f195
FW
66/* For tracers that don't implement custom flags */
67static struct tracer_opt dummy_tracer_opt[] = {
68 { }
69};
70
71static struct tracer_flags dummy_tracer_flags = {
72 .val = 0,
73 .opts = dummy_tracer_opt
74};
75
76static int dummy_set_flag(u32 old_flags, u32 bit, int set)
77{
78 return 0;
79}
0f048701 80
7ffbd48d
SR
81/*
82 * To prevent the comm cache from being overwritten when no
83 * tracing is active, only save the comm when a trace event
84 * occurred.
85 */
86static DEFINE_PER_CPU(bool, trace_cmdline_save);
87
0f048701
SR
88/*
89 * Kill all tracing for good (never come back).
90 * It is initialized to 1 but will turn to zero if the initialization
91 * of the tracer is successful. But that is the only place that sets
92 * this back to zero.
93 */
4fd27358 94static int tracing_disabled = 1;
0f048701 95
9288f99a 96DEFINE_PER_CPU(int, ftrace_cpu_disabled);
d769041f 97
955b61e5 98cpumask_var_t __read_mostly tracing_buffer_mask;
ab46428c 99
944ac425
SR
100/*
101 * ftrace_dump_on_oops - variable to dump ftrace buffer on oops
102 *
103 * If there is an oops (or kernel panic) and the ftrace_dump_on_oops
104 * is set, then ftrace_dump is called. This will output the contents
105 * of the ftrace buffers to the console. This is very useful for
106 * capturing traces that lead to crashes and outputing it to a
107 * serial console.
108 *
109 * It is default off, but you can enable it with either specifying
110 * "ftrace_dump_on_oops" in the kernel command line, or setting
cecbca96
FW
111 * /proc/sys/kernel/ftrace_dump_on_oops
112 * Set 1 if you want to dump buffers of all CPUs
113 * Set 2 if you want to dump the buffer of the CPU that triggered oops
944ac425 114 */
cecbca96
FW
115
116enum ftrace_dump_mode ftrace_dump_on_oops;
944ac425 117
b2821ae6
SR
118static int tracing_set_tracer(const char *buf);
119
ee6c2c1b
LZ
120#define MAX_TRACER_SIZE 100
121static char bootup_tracer_buf[MAX_TRACER_SIZE] __initdata;
b2821ae6 122static char *default_bootup_tracer;
d9e54076 123
55034cd6
SRRH
124static bool allocate_snapshot;
125
1beee96b 126static int __init set_cmdline_ftrace(char *str)
d9e54076 127{
ee6c2c1b 128 strncpy(bootup_tracer_buf, str, MAX_TRACER_SIZE);
b2821ae6 129 default_bootup_tracer = bootup_tracer_buf;
73c5162a 130 /* We are using ftrace early, expand it */
55034cd6 131 ring_buffer_expanded = true;
d9e54076
PZ
132 return 1;
133}
1beee96b 134__setup("ftrace=", set_cmdline_ftrace);
d9e54076 135
944ac425
SR
136static int __init set_ftrace_dump_on_oops(char *str)
137{
cecbca96
FW
138 if (*str++ != '=' || !*str) {
139 ftrace_dump_on_oops = DUMP_ALL;
140 return 1;
141 }
142
143 if (!strcmp("orig_cpu", str)) {
144 ftrace_dump_on_oops = DUMP_ORIG;
145 return 1;
146 }
147
148 return 0;
944ac425
SR
149}
150__setup("ftrace_dump_on_oops", set_ftrace_dump_on_oops);
60a11774 151
55034cd6
SRRH
152static int __init alloc_snapshot(char *str)
153{
154 allocate_snapshot = true;
155 /* We also need the main ring buffer expanded */
156 ring_buffer_expanded = true;
157 return 1;
158}
159__setup("alloc_snapshot", alloc_snapshot);
160
7bcfaf54
SR
161
162static char trace_boot_options_buf[MAX_TRACER_SIZE] __initdata;
163static char *trace_boot_options __initdata;
164
165static int __init set_trace_boot_options(char *str)
166{
167 strncpy(trace_boot_options_buf, str, MAX_TRACER_SIZE);
168 trace_boot_options = trace_boot_options_buf;
169 return 0;
170}
171__setup("trace_options=", set_trace_boot_options);
172
cf8e3474 173unsigned long long ns2usecs(cycle_t nsec)
bc0c38d1
SR
174{
175 nsec += 500;
176 do_div(nsec, 1000);
177 return nsec;
178}
179
4fcdae83
SR
180/*
181 * The global_trace is the descriptor that holds the tracing
182 * buffers for the live tracing. For each CPU, it contains
183 * a link list of pages that will store trace entries. The
184 * page descriptor of the pages in the memory is used to hold
185 * the link list by linking the lru item in the page descriptor
186 * to each of the pages in the buffer per CPU.
187 *
188 * For each active CPU there is a data field that holds the
189 * pages for the buffer for that CPU. Each CPU has the same number
190 * of pages allocated for its buffer.
191 */
bc0c38d1
SR
192static struct trace_array global_trace;
193
ae63b31e
SR
194LIST_HEAD(ftrace_trace_arrays);
195
e77405ad
SR
196int filter_current_check_discard(struct ring_buffer *buffer,
197 struct ftrace_event_call *call, void *rec,
eb02ce01
TZ
198 struct ring_buffer_event *event)
199{
e77405ad 200 return filter_check_discard(call, rec, buffer, event);
eb02ce01 201}
17c873ec 202EXPORT_SYMBOL_GPL(filter_current_check_discard);
eb02ce01 203
37886f6a
SR
204cycle_t ftrace_now(int cpu)
205{
206 u64 ts;
207
208 /* Early boot up does not have a buffer yet */
12883efb 209 if (!global_trace.trace_buffer.buffer)
37886f6a
SR
210 return trace_clock_local();
211
12883efb
SRRH
212 ts = ring_buffer_time_stamp(global_trace.trace_buffer.buffer, cpu);
213 ring_buffer_normalize_time_stamp(global_trace.trace_buffer.buffer, cpu, &ts);
37886f6a
SR
214
215 return ts;
216}
bc0c38d1 217
9036990d
SR
218int tracing_is_enabled(void)
219{
0fb9656d 220 return tracing_is_on();
9036990d
SR
221}
222
4fcdae83 223/*
3928a8a2
SR
224 * trace_buf_size is the size in bytes that is allocated
225 * for a buffer. Note, the number of bytes is always rounded
226 * to page size.
3f5a54e3
SR
227 *
228 * This number is purposely set to a low number of 16384.
229 * If the dump on oops happens, it will be much appreciated
230 * to not have to wait for all that output. Anyway this can be
231 * boot time and run time configurable.
4fcdae83 232 */
3928a8a2 233#define TRACE_BUF_SIZE_DEFAULT 1441792UL /* 16384 * 88 (sizeof(entry)) */
3f5a54e3 234
3928a8a2 235static unsigned long trace_buf_size = TRACE_BUF_SIZE_DEFAULT;
bc0c38d1 236
4fcdae83 237/* trace_types holds a link list of available tracers. */
bc0c38d1 238static struct tracer *trace_types __read_mostly;
4fcdae83 239
4fcdae83
SR
240/*
241 * trace_types_lock is used to protect the trace_types list.
4fcdae83 242 */
bc0c38d1 243static DEFINE_MUTEX(trace_types_lock);
4fcdae83 244
7e53bd42
LJ
245/*
246 * serialize the access of the ring buffer
247 *
248 * ring buffer serializes readers, but it is low level protection.
249 * The validity of the events (which returns by ring_buffer_peek() ..etc)
250 * are not protected by ring buffer.
251 *
252 * The content of events may become garbage if we allow other process consumes
253 * these events concurrently:
254 * A) the page of the consumed events may become a normal page
255 * (not reader page) in ring buffer, and this page will be rewrited
256 * by events producer.
257 * B) The page of the consumed events may become a page for splice_read,
258 * and this page will be returned to system.
259 *
260 * These primitives allow multi process access to different cpu ring buffer
261 * concurrently.
262 *
263 * These primitives don't distinguish read-only and read-consume access.
264 * Multi read-only access are also serialized.
265 */
266
267#ifdef CONFIG_SMP
268static DECLARE_RWSEM(all_cpu_access_lock);
269static DEFINE_PER_CPU(struct mutex, cpu_access_lock);
270
271static inline void trace_access_lock(int cpu)
272{
ae3b5093 273 if (cpu == RING_BUFFER_ALL_CPUS) {
7e53bd42
LJ
274 /* gain it for accessing the whole ring buffer. */
275 down_write(&all_cpu_access_lock);
276 } else {
277 /* gain it for accessing a cpu ring buffer. */
278
ae3b5093 279 /* Firstly block other trace_access_lock(RING_BUFFER_ALL_CPUS). */
7e53bd42
LJ
280 down_read(&all_cpu_access_lock);
281
282 /* Secondly block other access to this @cpu ring buffer. */
283 mutex_lock(&per_cpu(cpu_access_lock, cpu));
284 }
285}
286
287static inline void trace_access_unlock(int cpu)
288{
ae3b5093 289 if (cpu == RING_BUFFER_ALL_CPUS) {
7e53bd42
LJ
290 up_write(&all_cpu_access_lock);
291 } else {
292 mutex_unlock(&per_cpu(cpu_access_lock, cpu));
293 up_read(&all_cpu_access_lock);
294 }
295}
296
297static inline void trace_access_lock_init(void)
298{
299 int cpu;
300
301 for_each_possible_cpu(cpu)
302 mutex_init(&per_cpu(cpu_access_lock, cpu));
303}
304
305#else
306
307static DEFINE_MUTEX(access_lock);
308
309static inline void trace_access_lock(int cpu)
310{
311 (void)cpu;
312 mutex_lock(&access_lock);
313}
314
315static inline void trace_access_unlock(int cpu)
316{
317 (void)cpu;
318 mutex_unlock(&access_lock);
319}
320
321static inline void trace_access_lock_init(void)
322{
323}
324
325#endif
326
ee6bce52 327/* trace_flags holds trace_options default values */
12ef7d44 328unsigned long trace_flags = TRACE_ITER_PRINT_PARENT | TRACE_ITER_PRINTK |
a2a16d6a 329 TRACE_ITER_ANNOTATE | TRACE_ITER_CONTEXT_INFO | TRACE_ITER_SLEEP_TIME |
77271ce4 330 TRACE_ITER_GRAPH_TIME | TRACE_ITER_RECORD_CMD | TRACE_ITER_OVERWRITE |
5224c3a3 331 TRACE_ITER_IRQ_INFO | TRACE_ITER_MARKERS;
4e655519 332
499e5470
SR
333/**
334 * tracing_on - enable tracing buffers
335 *
336 * This function enables tracing buffers that may have been
337 * disabled with tracing_off.
338 */
339void tracing_on(void)
340{
12883efb
SRRH
341 if (global_trace.trace_buffer.buffer)
342 ring_buffer_record_on(global_trace.trace_buffer.buffer);
499e5470
SR
343 /*
344 * This flag is only looked at when buffers haven't been
345 * allocated yet. We don't really care about the race
346 * between setting this flag and actually turning
347 * on the buffer.
348 */
349 global_trace.buffer_disabled = 0;
350}
351EXPORT_SYMBOL_GPL(tracing_on);
352
ad909e21
SRRH
353#ifdef CONFIG_TRACER_SNAPSHOT
354/**
355 * trace_snapshot - take a snapshot of the current buffer.
356 *
357 * This causes a swap between the snapshot buffer and the current live
358 * tracing buffer. You can use this to take snapshots of the live
359 * trace when some condition is triggered, but continue to trace.
360 *
361 * Note, make sure to allocate the snapshot with either
362 * a tracing_snapshot_alloc(), or by doing it manually
363 * with: echo 1 > /sys/kernel/debug/tracing/snapshot
364 *
365 * If the snapshot buffer is not allocated, it will stop tracing.
366 * Basically making a permanent snapshot.
367 */
368void tracing_snapshot(void)
369{
370 struct trace_array *tr = &global_trace;
371 struct tracer *tracer = tr->current_trace;
372 unsigned long flags;
373
374 if (!tr->allocated_snapshot) {
375 trace_printk("*** SNAPSHOT NOT ALLOCATED ***\n");
376 trace_printk("*** stopping trace here! ***\n");
377 tracing_off();
378 return;
379 }
380
381 /* Note, snapshot can not be used when the tracer uses it */
382 if (tracer->use_max_tr) {
383 trace_printk("*** LATENCY TRACER ACTIVE ***\n");
384 trace_printk("*** Can not use snapshot (sorry) ***\n");
385 return;
386 }
387
388 local_irq_save(flags);
389 update_max_tr(tr, current, smp_processor_id());
390 local_irq_restore(flags);
391}
392
393static int resize_buffer_duplicate_size(struct trace_buffer *trace_buf,
394 struct trace_buffer *size_buf, int cpu_id);
395
396/**
397 * trace_snapshot_alloc - allocate and take a snapshot of the current buffer.
398 *
399 * This is similar to trace_snapshot(), but it will allocate the
400 * snapshot buffer if it isn't already allocated. Use this only
401 * where it is safe to sleep, as the allocation may sleep.
402 *
403 * This causes a swap between the snapshot buffer and the current live
404 * tracing buffer. You can use this to take snapshots of the live
405 * trace when some condition is triggered, but continue to trace.
406 */
407void tracing_snapshot_alloc(void)
408{
409 struct trace_array *tr = &global_trace;
410 int ret;
411
412 if (!tr->allocated_snapshot) {
413
414 /* allocate spare buffer */
415 ret = resize_buffer_duplicate_size(&tr->max_buffer,
416 &tr->trace_buffer, RING_BUFFER_ALL_CPUS);
417 if (WARN_ON(ret < 0))
418 return;
419
420 tr->allocated_snapshot = true;
421 }
422
423 tracing_snapshot();
424}
425#else
426void tracing_snapshot(void)
427{
428 WARN_ONCE(1, "Snapshot feature not enabled, but internal snapshot used");
429}
430void tracing_snapshot_alloc(void)
431{
432 /* Give warning */
433 tracing_snapshot();
434}
435#endif /* CONFIG_TRACER_SNAPSHOT */
436
499e5470
SR
437/**
438 * tracing_off - turn off tracing buffers
439 *
440 * This function stops the tracing buffers from recording data.
441 * It does not disable any overhead the tracers themselves may
442 * be causing. This function simply causes all recording to
443 * the ring buffers to fail.
444 */
445void tracing_off(void)
446{
12883efb
SRRH
447 if (global_trace.trace_buffer.buffer)
448 ring_buffer_record_off(global_trace.trace_buffer.buffer);
499e5470
SR
449 /*
450 * This flag is only looked at when buffers haven't been
451 * allocated yet. We don't really care about the race
452 * between setting this flag and actually turning
453 * on the buffer.
454 */
455 global_trace.buffer_disabled = 1;
456}
457EXPORT_SYMBOL_GPL(tracing_off);
458
459/**
460 * tracing_is_on - show state of ring buffers enabled
461 */
462int tracing_is_on(void)
463{
12883efb
SRRH
464 if (global_trace.trace_buffer.buffer)
465 return ring_buffer_record_is_on(global_trace.trace_buffer.buffer);
499e5470
SR
466 return !global_trace.buffer_disabled;
467}
468EXPORT_SYMBOL_GPL(tracing_is_on);
469
3928a8a2 470static int __init set_buf_size(char *str)
bc0c38d1 471{
3928a8a2 472 unsigned long buf_size;
c6caeeb1 473
bc0c38d1
SR
474 if (!str)
475 return 0;
9d612bef 476 buf_size = memparse(str, &str);
c6caeeb1 477 /* nr_entries can not be zero */
9d612bef 478 if (buf_size == 0)
c6caeeb1 479 return 0;
3928a8a2 480 trace_buf_size = buf_size;
bc0c38d1
SR
481 return 1;
482}
3928a8a2 483__setup("trace_buf_size=", set_buf_size);
bc0c38d1 484
0e950173
TB
485static int __init set_tracing_thresh(char *str)
486{
87abb3b1 487 unsigned long threshold;
0e950173
TB
488 int ret;
489
490 if (!str)
491 return 0;
bcd83ea6 492 ret = kstrtoul(str, 0, &threshold);
0e950173
TB
493 if (ret < 0)
494 return 0;
87abb3b1 495 tracing_thresh = threshold * 1000;
0e950173
TB
496 return 1;
497}
498__setup("tracing_thresh=", set_tracing_thresh);
499
57f50be1
SR
500unsigned long nsecs_to_usecs(unsigned long nsecs)
501{
502 return nsecs / 1000;
503}
504
4fcdae83 505/* These must match the bit postions in trace_iterator_flags */
bc0c38d1
SR
506static const char *trace_options[] = {
507 "print-parent",
508 "sym-offset",
509 "sym-addr",
510 "verbose",
f9896bf3 511 "raw",
5e3ca0ec 512 "hex",
cb0f12aa 513 "bin",
2a2cc8f7 514 "block",
86387f7e 515 "stacktrace",
5e1607a0 516 "trace_printk",
b2a866f9 517 "ftrace_preempt",
9f029e83 518 "branch",
12ef7d44 519 "annotate",
02b67518 520 "userstacktrace",
b54d3de9 521 "sym-userobj",
66896a85 522 "printk-msg-only",
c4a8e8be 523 "context-info",
c032ef64 524 "latency-format",
be6f164a 525 "sleep-time",
a2a16d6a 526 "graph-time",
e870e9a1 527 "record-cmd",
750912fa 528 "overwrite",
cf30cf67 529 "disable_on_free",
77271ce4 530 "irq-info",
5224c3a3 531 "markers",
bc0c38d1
SR
532 NULL
533};
534
5079f326
Z
535static struct {
536 u64 (*func)(void);
537 const char *name;
8be0709f 538 int in_ns; /* is this clock in nanoseconds? */
5079f326 539} trace_clocks[] = {
8be0709f
DS
540 { trace_clock_local, "local", 1 },
541 { trace_clock_global, "global", 1 },
542 { trace_clock_counter, "counter", 0 },
8cbd9cc6 543 ARCH_TRACE_CLOCKS
5079f326
Z
544};
545
546int trace_clock_id;
547
b63f39ea 548/*
549 * trace_parser_get_init - gets the buffer for trace parser
550 */
551int trace_parser_get_init(struct trace_parser *parser, int size)
552{
553 memset(parser, 0, sizeof(*parser));
554
555 parser->buffer = kmalloc(size, GFP_KERNEL);
556 if (!parser->buffer)
557 return 1;
558
559 parser->size = size;
560 return 0;
561}
562
563/*
564 * trace_parser_put - frees the buffer for trace parser
565 */
566void trace_parser_put(struct trace_parser *parser)
567{
568 kfree(parser->buffer);
569}
570
571/*
572 * trace_get_user - reads the user input string separated by space
573 * (matched by isspace(ch))
574 *
575 * For each string found the 'struct trace_parser' is updated,
576 * and the function returns.
577 *
578 * Returns number of bytes read.
579 *
580 * See kernel/trace/trace.h for 'struct trace_parser' details.
581 */
582int trace_get_user(struct trace_parser *parser, const char __user *ubuf,
583 size_t cnt, loff_t *ppos)
584{
585 char ch;
586 size_t read = 0;
587 ssize_t ret;
588
589 if (!*ppos)
590 trace_parser_clear(parser);
591
592 ret = get_user(ch, ubuf++);
593 if (ret)
594 goto out;
595
596 read++;
597 cnt--;
598
599 /*
600 * The parser is not finished with the last write,
601 * continue reading the user input without skipping spaces.
602 */
603 if (!parser->cont) {
604 /* skip white space */
605 while (cnt && isspace(ch)) {
606 ret = get_user(ch, ubuf++);
607 if (ret)
608 goto out;
609 read++;
610 cnt--;
611 }
612
613 /* only spaces were written */
614 if (isspace(ch)) {
615 *ppos += read;
616 ret = read;
617 goto out;
618 }
619
620 parser->idx = 0;
621 }
622
623 /* read the non-space input */
624 while (cnt && !isspace(ch)) {
3c235a33 625 if (parser->idx < parser->size - 1)
b63f39ea 626 parser->buffer[parser->idx++] = ch;
627 else {
628 ret = -EINVAL;
629 goto out;
630 }
631 ret = get_user(ch, ubuf++);
632 if (ret)
633 goto out;
634 read++;
635 cnt--;
636 }
637
638 /* We either got finished input or we have to wait for another call. */
639 if (isspace(ch)) {
640 parser->buffer[parser->idx] = 0;
641 parser->cont = false;
642 } else {
643 parser->cont = true;
644 parser->buffer[parser->idx++] = ch;
645 }
646
647 *ppos += read;
648 ret = read;
649
650out:
651 return ret;
652}
653
6c6c2796
PP
654ssize_t trace_seq_to_user(struct trace_seq *s, char __user *ubuf, size_t cnt)
655{
656 int len;
657 int ret;
658
2dc5d12b
SR
659 if (!cnt)
660 return 0;
661
6c6c2796
PP
662 if (s->len <= s->readpos)
663 return -EBUSY;
664
665 len = s->len - s->readpos;
666 if (cnt > len)
667 cnt = len;
668 ret = copy_to_user(ubuf, s->buffer + s->readpos, cnt);
2dc5d12b 669 if (ret == cnt)
6c6c2796
PP
670 return -EFAULT;
671
2dc5d12b
SR
672 cnt -= ret;
673
e74da523 674 s->readpos += cnt;
6c6c2796 675 return cnt;
214023c3
SR
676}
677
b8b94265 678static ssize_t trace_seq_to_buffer(struct trace_seq *s, void *buf, size_t cnt)
3c56819b
EGM
679{
680 int len;
3c56819b
EGM
681
682 if (s->len <= s->readpos)
683 return -EBUSY;
684
685 len = s->len - s->readpos;
686 if (cnt > len)
687 cnt = len;
5a26c8f0 688 memcpy(buf, s->buffer + s->readpos, cnt);
3c56819b 689
e74da523 690 s->readpos += cnt;
3c56819b
EGM
691 return cnt;
692}
693
5d4a9dba
SR
694/*
695 * ftrace_max_lock is used to protect the swapping of buffers
696 * when taking a max snapshot. The buffers themselves are
697 * protected by per_cpu spinlocks. But the action of the swap
698 * needs its own lock.
699 *
445c8951 700 * This is defined as a arch_spinlock_t in order to help
5d4a9dba
SR
701 * with performance when lockdep debugging is enabled.
702 *
703 * It is also used in other places outside the update_max_tr
704 * so it needs to be defined outside of the
705 * CONFIG_TRACER_MAX_TRACE.
706 */
445c8951 707static arch_spinlock_t ftrace_max_lock =
edc35bd7 708 (arch_spinlock_t)__ARCH_SPIN_LOCK_UNLOCKED;
5d4a9dba 709
0e950173
TB
710unsigned long __read_mostly tracing_thresh;
711
5d4a9dba
SR
712#ifdef CONFIG_TRACER_MAX_TRACE
713unsigned long __read_mostly tracing_max_latency;
5d4a9dba
SR
714
715/*
716 * Copy the new maximum trace into the separate maximum-trace
717 * structure. (this way the maximum trace is permanently saved,
718 * for later retrieval via /sys/kernel/debug/tracing/latency_trace)
719 */
720static void
721__update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
722{
12883efb
SRRH
723 struct trace_buffer *trace_buf = &tr->trace_buffer;
724 struct trace_buffer *max_buf = &tr->max_buffer;
725 struct trace_array_cpu *data = per_cpu_ptr(trace_buf->data, cpu);
726 struct trace_array_cpu *max_data = per_cpu_ptr(max_buf->data, cpu);
5d4a9dba 727
12883efb
SRRH
728 max_buf->cpu = cpu;
729 max_buf->time_start = data->preempt_timestamp;
5d4a9dba 730
8248ac05
SR
731 max_data->saved_latency = tracing_max_latency;
732 max_data->critical_start = data->critical_start;
733 max_data->critical_end = data->critical_end;
5d4a9dba 734
1acaa1b2 735 memcpy(max_data->comm, tsk->comm, TASK_COMM_LEN);
8248ac05
SR
736 max_data->pid = tsk->pid;
737 max_data->uid = task_uid(tsk);
738 max_data->nice = tsk->static_prio - 20 - MAX_RT_PRIO;
739 max_data->policy = tsk->policy;
740 max_data->rt_priority = tsk->rt_priority;
5d4a9dba
SR
741
742 /* record this tasks comm */
743 tracing_record_cmdline(tsk);
744}
745
4fcdae83
SR
746/**
747 * update_max_tr - snapshot all trace buffers from global_trace to max_tr
748 * @tr: tracer
749 * @tsk: the task with the latency
750 * @cpu: The cpu that initiated the trace.
751 *
752 * Flip the buffers between the @tr and the max_tr and record information
753 * about which task was the cause of this latency.
754 */
e309b41d 755void
bc0c38d1
SR
756update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
757{
2721e72d 758 struct ring_buffer *buf;
bc0c38d1 759
2b6080f2 760 if (tr->stop_count)
b8de7bd1
SR
761 return;
762
4c11d7ae 763 WARN_ON_ONCE(!irqs_disabled());
34600f0e 764
45ad21ca 765 if (!tr->allocated_snapshot) {
debdd57f 766 /* Only the nop tracer should hit this when disabling */
2b6080f2 767 WARN_ON_ONCE(tr->current_trace != &nop_trace);
34600f0e 768 return;
debdd57f 769 }
34600f0e 770
0199c4e6 771 arch_spin_lock(&ftrace_max_lock);
3928a8a2 772
12883efb
SRRH
773 buf = tr->trace_buffer.buffer;
774 tr->trace_buffer.buffer = tr->max_buffer.buffer;
775 tr->max_buffer.buffer = buf;
3928a8a2 776
bc0c38d1 777 __update_max_tr(tr, tsk, cpu);
0199c4e6 778 arch_spin_unlock(&ftrace_max_lock);
bc0c38d1
SR
779}
780
781/**
782 * update_max_tr_single - only copy one trace over, and reset the rest
783 * @tr - tracer
784 * @tsk - task with the latency
785 * @cpu - the cpu of the buffer to copy.
4fcdae83
SR
786 *
787 * Flip the trace of a single CPU buffer between the @tr and the max_tr.
bc0c38d1 788 */
e309b41d 789void
bc0c38d1
SR
790update_max_tr_single(struct trace_array *tr, struct task_struct *tsk, int cpu)
791{
3928a8a2 792 int ret;
bc0c38d1 793
2b6080f2 794 if (tr->stop_count)
b8de7bd1
SR
795 return;
796
4c11d7ae 797 WARN_ON_ONCE(!irqs_disabled());
45ad21ca 798 if (WARN_ON_ONCE(!tr->allocated_snapshot))
ef710e10 799 return;
ef710e10 800
0199c4e6 801 arch_spin_lock(&ftrace_max_lock);
bc0c38d1 802
12883efb 803 ret = ring_buffer_swap_cpu(tr->max_buffer.buffer, tr->trace_buffer.buffer, cpu);
3928a8a2 804
e8165dbb
SR
805 if (ret == -EBUSY) {
806 /*
807 * We failed to swap the buffer due to a commit taking
808 * place on this CPU. We fail to record, but we reset
809 * the max trace buffer (no one writes directly to it)
810 * and flag that it failed.
811 */
12883efb 812 trace_array_printk_buf(tr->max_buffer.buffer, _THIS_IP_,
e8165dbb
SR
813 "Failed to swap buffers due to commit in progress\n");
814 }
815
e8165dbb 816 WARN_ON_ONCE(ret && ret != -EAGAIN && ret != -EBUSY);
bc0c38d1
SR
817
818 __update_max_tr(tr, tsk, cpu);
0199c4e6 819 arch_spin_unlock(&ftrace_max_lock);
bc0c38d1 820}
5d4a9dba 821#endif /* CONFIG_TRACER_MAX_TRACE */
bc0c38d1 822
0d5c6e1c
SR
823static void default_wait_pipe(struct trace_iterator *iter)
824{
15693458
SRRH
825 /* Iterators are static, they should be filled or empty */
826 if (trace_buffer_iter(iter, iter->cpu_file))
827 return;
0d5c6e1c 828
12883efb 829 ring_buffer_wait(iter->trace_buffer->buffer, iter->cpu_file);
0d5c6e1c
SR
830}
831
f4e781c0
SRRH
832#ifdef CONFIG_FTRACE_STARTUP_TEST
833static int run_tracer_selftest(struct tracer *type)
834{
835 struct trace_array *tr = &global_trace;
836 struct tracer *saved_tracer = tr->current_trace;
837 int ret;
838
839 if (!type->selftest || tracing_selftest_disabled)
840 return 0;
841
842 /*
843 * Run a selftest on this tracer.
844 * Here we reset the trace buffer, and set the current
845 * tracer to be this tracer. The tracer can then run some
846 * internal tracing to verify that everything is in order.
847 * If we fail, we do not register this tracer.
848 */
849 tracing_reset_online_cpus(&tr->trace_buffer);
850
851 tr->current_trace = type;
852
853#ifdef CONFIG_TRACER_MAX_TRACE
854 if (type->use_max_tr) {
855 /* If we expanded the buffers, make sure the max is expanded too */
856 if (ring_buffer_expanded)
857 ring_buffer_resize(tr->max_buffer.buffer, trace_buf_size,
858 RING_BUFFER_ALL_CPUS);
859 tr->allocated_snapshot = true;
860 }
861#endif
862
863 /* the test is responsible for initializing and enabling */
864 pr_info("Testing tracer %s: ", type->name);
865 ret = type->selftest(type, tr);
866 /* the test is responsible for resetting too */
867 tr->current_trace = saved_tracer;
868 if (ret) {
869 printk(KERN_CONT "FAILED!\n");
870 /* Add the warning after printing 'FAILED' */
871 WARN_ON(1);
872 return -1;
873 }
874 /* Only reset on passing, to avoid touching corrupted buffers */
875 tracing_reset_online_cpus(&tr->trace_buffer);
876
877#ifdef CONFIG_TRACER_MAX_TRACE
878 if (type->use_max_tr) {
879 tr->allocated_snapshot = false;
880
881 /* Shrink the max buffer again */
882 if (ring_buffer_expanded)
883 ring_buffer_resize(tr->max_buffer.buffer, 1,
884 RING_BUFFER_ALL_CPUS);
885 }
886#endif
887
888 printk(KERN_CONT "PASSED\n");
889 return 0;
890}
891#else
892static inline int run_tracer_selftest(struct tracer *type)
893{
894 return 0;
895}
896#endif /* CONFIG_FTRACE_STARTUP_TEST */
897
4fcdae83
SR
898/**
899 * register_tracer - register a tracer with the ftrace system.
900 * @type - the plugin for the tracer
901 *
902 * Register a new plugin tracer.
903 */
bc0c38d1
SR
904int register_tracer(struct tracer *type)
905{
906 struct tracer *t;
bc0c38d1
SR
907 int ret = 0;
908
909 if (!type->name) {
910 pr_info("Tracer must have a name\n");
911 return -1;
912 }
913
24a461d5 914 if (strlen(type->name) >= MAX_TRACER_SIZE) {
ee6c2c1b
LZ
915 pr_info("Tracer has a name longer than %d\n", MAX_TRACER_SIZE);
916 return -1;
917 }
918
bc0c38d1 919 mutex_lock(&trace_types_lock);
86fa2f60 920
8e1b82e0
FW
921 tracing_selftest_running = true;
922
bc0c38d1
SR
923 for (t = trace_types; t; t = t->next) {
924 if (strcmp(type->name, t->name) == 0) {
925 /* already found */
ee6c2c1b 926 pr_info("Tracer %s already registered\n",
bc0c38d1
SR
927 type->name);
928 ret = -1;
929 goto out;
930 }
931 }
932
adf9f195
FW
933 if (!type->set_flag)
934 type->set_flag = &dummy_set_flag;
935 if (!type->flags)
936 type->flags = &dummy_tracer_flags;
937 else
938 if (!type->flags->opts)
939 type->flags->opts = dummy_tracer_opt;
6eaaa5d5
FW
940 if (!type->wait_pipe)
941 type->wait_pipe = default_wait_pipe;
942
f4e781c0
SRRH
943 ret = run_tracer_selftest(type);
944 if (ret < 0)
945 goto out;
60a11774 946
bc0c38d1
SR
947 type->next = trace_types;
948 trace_types = type;
60a11774 949
bc0c38d1 950 out:
8e1b82e0 951 tracing_selftest_running = false;
bc0c38d1
SR
952 mutex_unlock(&trace_types_lock);
953
dac74940
SR
954 if (ret || !default_bootup_tracer)
955 goto out_unlock;
956
ee6c2c1b 957 if (strncmp(default_bootup_tracer, type->name, MAX_TRACER_SIZE))
dac74940
SR
958 goto out_unlock;
959
960 printk(KERN_INFO "Starting tracer '%s'\n", type->name);
961 /* Do we want this tracer to start on bootup? */
962 tracing_set_tracer(type->name);
963 default_bootup_tracer = NULL;
964 /* disable other selftests, since this will break it. */
55034cd6 965 tracing_selftest_disabled = true;
b2821ae6 966#ifdef CONFIG_FTRACE_STARTUP_TEST
dac74940
SR
967 printk(KERN_INFO "Disabling FTRACE selftests due to running tracer '%s'\n",
968 type->name);
b2821ae6 969#endif
b2821ae6 970
dac74940 971 out_unlock:
bc0c38d1
SR
972 return ret;
973}
974
12883efb 975void tracing_reset(struct trace_buffer *buf, int cpu)
f633903a 976{
12883efb 977 struct ring_buffer *buffer = buf->buffer;
f633903a 978
a5416411
HT
979 if (!buffer)
980 return;
981
f633903a
SR
982 ring_buffer_record_disable(buffer);
983
984 /* Make sure all commits have finished */
985 synchronize_sched();
68179686 986 ring_buffer_reset_cpu(buffer, cpu);
f633903a
SR
987
988 ring_buffer_record_enable(buffer);
989}
990
12883efb 991void tracing_reset_online_cpus(struct trace_buffer *buf)
213cc060 992{
12883efb 993 struct ring_buffer *buffer = buf->buffer;
213cc060
PE
994 int cpu;
995
a5416411
HT
996 if (!buffer)
997 return;
998
621968cd
SR
999 ring_buffer_record_disable(buffer);
1000
1001 /* Make sure all commits have finished */
1002 synchronize_sched();
1003
12883efb 1004 buf->time_start = ftrace_now(buf->cpu);
213cc060
PE
1005
1006 for_each_online_cpu(cpu)
68179686 1007 ring_buffer_reset_cpu(buffer, cpu);
621968cd
SR
1008
1009 ring_buffer_record_enable(buffer);
213cc060
PE
1010}
1011
9456f0fa
SR
1012void tracing_reset_current(int cpu)
1013{
12883efb 1014 tracing_reset(&global_trace.trace_buffer, cpu);
9456f0fa
SR
1015}
1016
873c642f 1017void tracing_reset_all_online_cpus(void)
9456f0fa 1018{
873c642f
SRRH
1019 struct trace_array *tr;
1020
1021 mutex_lock(&trace_types_lock);
1022 list_for_each_entry(tr, &ftrace_trace_arrays, list) {
12883efb
SRRH
1023 tracing_reset_online_cpus(&tr->trace_buffer);
1024#ifdef CONFIG_TRACER_MAX_TRACE
1025 tracing_reset_online_cpus(&tr->max_buffer);
1026#endif
873c642f
SRRH
1027 }
1028 mutex_unlock(&trace_types_lock);
9456f0fa
SR
1029}
1030
bc0c38d1 1031#define SAVED_CMDLINES 128
2c7eea4c 1032#define NO_CMDLINE_MAP UINT_MAX
bc0c38d1
SR
1033static unsigned map_pid_to_cmdline[PID_MAX_DEFAULT+1];
1034static unsigned map_cmdline_to_pid[SAVED_CMDLINES];
1035static char saved_cmdlines[SAVED_CMDLINES][TASK_COMM_LEN];
1036static int cmdline_idx;
edc35bd7 1037static arch_spinlock_t trace_cmdline_lock = __ARCH_SPIN_LOCK_UNLOCKED;
25b0b44a 1038
25b0b44a 1039/* temporary disable recording */
4fd27358 1040static atomic_t trace_record_cmdline_disabled __read_mostly;
bc0c38d1
SR
1041
1042static void trace_init_cmdlines(void)
1043{
2c7eea4c
TG
1044 memset(&map_pid_to_cmdline, NO_CMDLINE_MAP, sizeof(map_pid_to_cmdline));
1045 memset(&map_cmdline_to_pid, NO_CMDLINE_MAP, sizeof(map_cmdline_to_pid));
bc0c38d1
SR
1046 cmdline_idx = 0;
1047}
1048
b5130b1e
CE
1049int is_tracing_stopped(void)
1050{
2b6080f2 1051 return global_trace.stop_count;
b5130b1e
CE
1052}
1053
69bb54ec
SR
1054/**
1055 * ftrace_off_permanent - disable all ftrace code permanently
1056 *
1057 * This should only be called when a serious anomally has
1058 * been detected. This will turn off the function tracing,
1059 * ring buffers, and other tracing utilites. It takes no
1060 * locks and can be called from any context.
1061 */
1062void ftrace_off_permanent(void)
1063{
1064 tracing_disabled = 1;
1065 ftrace_stop();
1066 tracing_off_permanent();
1067}
1068
0f048701
SR
1069/**
1070 * tracing_start - quick start of the tracer
1071 *
1072 * If tracing is enabled but was stopped by tracing_stop,
1073 * this will start the tracer back up.
1074 */
1075void tracing_start(void)
1076{
1077 struct ring_buffer *buffer;
1078 unsigned long flags;
1079
1080 if (tracing_disabled)
1081 return;
1082
2b6080f2
SR
1083 raw_spin_lock_irqsave(&global_trace.start_lock, flags);
1084 if (--global_trace.stop_count) {
1085 if (global_trace.stop_count < 0) {
b06a8301
SR
1086 /* Someone screwed up their debugging */
1087 WARN_ON_ONCE(1);
2b6080f2 1088 global_trace.stop_count = 0;
b06a8301 1089 }
0f048701
SR
1090 goto out;
1091 }
1092
a2f80714
SR
1093 /* Prevent the buffers from switching */
1094 arch_spin_lock(&ftrace_max_lock);
0f048701 1095
12883efb 1096 buffer = global_trace.trace_buffer.buffer;
0f048701
SR
1097 if (buffer)
1098 ring_buffer_record_enable(buffer);
1099
12883efb
SRRH
1100#ifdef CONFIG_TRACER_MAX_TRACE
1101 buffer = global_trace.max_buffer.buffer;
0f048701
SR
1102 if (buffer)
1103 ring_buffer_record_enable(buffer);
12883efb 1104#endif
0f048701 1105
a2f80714
SR
1106 arch_spin_unlock(&ftrace_max_lock);
1107
0f048701
SR
1108 ftrace_start();
1109 out:
2b6080f2
SR
1110 raw_spin_unlock_irqrestore(&global_trace.start_lock, flags);
1111}
1112
1113static void tracing_start_tr(struct trace_array *tr)
1114{
1115 struct ring_buffer *buffer;
1116 unsigned long flags;
1117
1118 if (tracing_disabled)
1119 return;
1120
1121 /* If global, we need to also start the max tracer */
1122 if (tr->flags & TRACE_ARRAY_FL_GLOBAL)
1123 return tracing_start();
1124
1125 raw_spin_lock_irqsave(&tr->start_lock, flags);
1126
1127 if (--tr->stop_count) {
1128 if (tr->stop_count < 0) {
1129 /* Someone screwed up their debugging */
1130 WARN_ON_ONCE(1);
1131 tr->stop_count = 0;
1132 }
1133 goto out;
1134 }
1135
12883efb 1136 buffer = tr->trace_buffer.buffer;
2b6080f2
SR
1137 if (buffer)
1138 ring_buffer_record_enable(buffer);
1139
1140 out:
1141 raw_spin_unlock_irqrestore(&tr->start_lock, flags);
0f048701
SR
1142}
1143
1144/**
1145 * tracing_stop - quick stop of the tracer
1146 *
1147 * Light weight way to stop tracing. Use in conjunction with
1148 * tracing_start.
1149 */
1150void tracing_stop(void)
1151{
1152 struct ring_buffer *buffer;
1153 unsigned long flags;
1154
1155 ftrace_stop();
2b6080f2
SR
1156 raw_spin_lock_irqsave(&global_trace.start_lock, flags);
1157 if (global_trace.stop_count++)
0f048701
SR
1158 goto out;
1159
a2f80714
SR
1160 /* Prevent the buffers from switching */
1161 arch_spin_lock(&ftrace_max_lock);
1162
12883efb 1163 buffer = global_trace.trace_buffer.buffer;
0f048701
SR
1164 if (buffer)
1165 ring_buffer_record_disable(buffer);
1166
12883efb
SRRH
1167#ifdef CONFIG_TRACER_MAX_TRACE
1168 buffer = global_trace.max_buffer.buffer;
0f048701
SR
1169 if (buffer)
1170 ring_buffer_record_disable(buffer);
12883efb 1171#endif
0f048701 1172
a2f80714
SR
1173 arch_spin_unlock(&ftrace_max_lock);
1174
0f048701 1175 out:
2b6080f2
SR
1176 raw_spin_unlock_irqrestore(&global_trace.start_lock, flags);
1177}
1178
1179static void tracing_stop_tr(struct trace_array *tr)
1180{
1181 struct ring_buffer *buffer;
1182 unsigned long flags;
1183
1184 /* If global, we need to also stop the max tracer */
1185 if (tr->flags & TRACE_ARRAY_FL_GLOBAL)
1186 return tracing_stop();
1187
1188 raw_spin_lock_irqsave(&tr->start_lock, flags);
1189 if (tr->stop_count++)
1190 goto out;
1191
12883efb 1192 buffer = tr->trace_buffer.buffer;
2b6080f2
SR
1193 if (buffer)
1194 ring_buffer_record_disable(buffer);
1195
1196 out:
1197 raw_spin_unlock_irqrestore(&tr->start_lock, flags);
0f048701
SR
1198}
1199
e309b41d 1200void trace_stop_cmdline_recording(void);
bc0c38d1 1201
e309b41d 1202static void trace_save_cmdline(struct task_struct *tsk)
bc0c38d1 1203{
a635cf04 1204 unsigned pid, idx;
bc0c38d1
SR
1205
1206 if (!tsk->pid || unlikely(tsk->pid > PID_MAX_DEFAULT))
1207 return;
1208
1209 /*
1210 * It's not the end of the world if we don't get
1211 * the lock, but we also don't want to spin
1212 * nor do we want to disable interrupts,
1213 * so if we miss here, then better luck next time.
1214 */
0199c4e6 1215 if (!arch_spin_trylock(&trace_cmdline_lock))
bc0c38d1
SR
1216 return;
1217
1218 idx = map_pid_to_cmdline[tsk->pid];
2c7eea4c 1219 if (idx == NO_CMDLINE_MAP) {
bc0c38d1
SR
1220 idx = (cmdline_idx + 1) % SAVED_CMDLINES;
1221
a635cf04
CE
1222 /*
1223 * Check whether the cmdline buffer at idx has a pid
1224 * mapped. We are going to overwrite that entry so we
1225 * need to clear the map_pid_to_cmdline. Otherwise we
1226 * would read the new comm for the old pid.
1227 */
1228 pid = map_cmdline_to_pid[idx];
1229 if (pid != NO_CMDLINE_MAP)
1230 map_pid_to_cmdline[pid] = NO_CMDLINE_MAP;
bc0c38d1 1231
a635cf04 1232 map_cmdline_to_pid[idx] = tsk->pid;
bc0c38d1
SR
1233 map_pid_to_cmdline[tsk->pid] = idx;
1234
1235 cmdline_idx = idx;
1236 }
1237
1238 memcpy(&saved_cmdlines[idx], tsk->comm, TASK_COMM_LEN);
1239
0199c4e6 1240 arch_spin_unlock(&trace_cmdline_lock);
bc0c38d1
SR
1241}
1242
4ca53085 1243void trace_find_cmdline(int pid, char comm[])
bc0c38d1 1244{
bc0c38d1
SR
1245 unsigned map;
1246
4ca53085
SR
1247 if (!pid) {
1248 strcpy(comm, "<idle>");
1249 return;
1250 }
bc0c38d1 1251
74bf4076
SR
1252 if (WARN_ON_ONCE(pid < 0)) {
1253 strcpy(comm, "<XXX>");
1254 return;
1255 }
1256
4ca53085
SR
1257 if (pid > PID_MAX_DEFAULT) {
1258 strcpy(comm, "<...>");
1259 return;
1260 }
bc0c38d1 1261
5b6045a9 1262 preempt_disable();
0199c4e6 1263 arch_spin_lock(&trace_cmdline_lock);
bc0c38d1 1264 map = map_pid_to_cmdline[pid];
50d88758
TG
1265 if (map != NO_CMDLINE_MAP)
1266 strcpy(comm, saved_cmdlines[map]);
1267 else
1268 strcpy(comm, "<...>");
bc0c38d1 1269
0199c4e6 1270 arch_spin_unlock(&trace_cmdline_lock);
5b6045a9 1271 preempt_enable();
bc0c38d1
SR
1272}
1273
e309b41d 1274void tracing_record_cmdline(struct task_struct *tsk)
bc0c38d1 1275{
0fb9656d 1276 if (atomic_read(&trace_record_cmdline_disabled) || !tracing_is_on())
bc0c38d1
SR
1277 return;
1278
7ffbd48d
SR
1279 if (!__this_cpu_read(trace_cmdline_save))
1280 return;
1281
1282 __this_cpu_write(trace_cmdline_save, false);
1283
bc0c38d1
SR
1284 trace_save_cmdline(tsk);
1285}
1286
45dcd8b8 1287void
38697053
SR
1288tracing_generic_entry_update(struct trace_entry *entry, unsigned long flags,
1289 int pc)
bc0c38d1
SR
1290{
1291 struct task_struct *tsk = current;
bc0c38d1 1292
777e208d
SR
1293 entry->preempt_count = pc & 0xff;
1294 entry->pid = (tsk) ? tsk->pid : 0;
1295 entry->flags =
9244489a 1296#ifdef CONFIG_TRACE_IRQFLAGS_SUPPORT
2e2ca155 1297 (irqs_disabled_flags(flags) ? TRACE_FLAG_IRQS_OFF : 0) |
9244489a
SR
1298#else
1299 TRACE_FLAG_IRQS_NOSUPPORT |
1300#endif
bc0c38d1
SR
1301 ((pc & HARDIRQ_MASK) ? TRACE_FLAG_HARDIRQ : 0) |
1302 ((pc & SOFTIRQ_MASK) ? TRACE_FLAG_SOFTIRQ : 0) |
1303 (need_resched() ? TRACE_FLAG_NEED_RESCHED : 0);
1304}
f413cdb8 1305EXPORT_SYMBOL_GPL(tracing_generic_entry_update);
bc0c38d1 1306
e77405ad
SR
1307struct ring_buffer_event *
1308trace_buffer_lock_reserve(struct ring_buffer *buffer,
1309 int type,
1310 unsigned long len,
1311 unsigned long flags, int pc)
51a763dd
ACM
1312{
1313 struct ring_buffer_event *event;
1314
e77405ad 1315 event = ring_buffer_lock_reserve(buffer, len);
51a763dd
ACM
1316 if (event != NULL) {
1317 struct trace_entry *ent = ring_buffer_event_data(event);
1318
1319 tracing_generic_entry_update(ent, flags, pc);
1320 ent->type = type;
1321 }
1322
1323 return event;
1324}
51a763dd 1325
7ffbd48d
SR
1326void
1327__buffer_unlock_commit(struct ring_buffer *buffer, struct ring_buffer_event *event)
1328{
1329 __this_cpu_write(trace_cmdline_save, true);
1330 ring_buffer_unlock_commit(buffer, event);
1331}
1332
e77405ad
SR
1333static inline void
1334__trace_buffer_unlock_commit(struct ring_buffer *buffer,
1335 struct ring_buffer_event *event,
0d5c6e1c 1336 unsigned long flags, int pc)
51a763dd 1337{
7ffbd48d 1338 __buffer_unlock_commit(buffer, event);
51a763dd 1339
e77405ad
SR
1340 ftrace_trace_stack(buffer, flags, 6, pc);
1341 ftrace_trace_userstack(buffer, flags, pc);
07edf712
FW
1342}
1343
e77405ad
SR
1344void trace_buffer_unlock_commit(struct ring_buffer *buffer,
1345 struct ring_buffer_event *event,
1346 unsigned long flags, int pc)
07edf712 1347{
0d5c6e1c 1348 __trace_buffer_unlock_commit(buffer, event, flags, pc);
51a763dd 1349}
0d5c6e1c 1350EXPORT_SYMBOL_GPL(trace_buffer_unlock_commit);
51a763dd 1351
ccb469a1
SR
1352struct ring_buffer_event *
1353trace_event_buffer_lock_reserve(struct ring_buffer **current_rb,
1354 struct ftrace_event_file *ftrace_file,
1355 int type, unsigned long len,
1356 unsigned long flags, int pc)
1357{
12883efb 1358 *current_rb = ftrace_file->tr->trace_buffer.buffer;
ccb469a1
SR
1359 return trace_buffer_lock_reserve(*current_rb,
1360 type, len, flags, pc);
1361}
1362EXPORT_SYMBOL_GPL(trace_event_buffer_lock_reserve);
1363
ef5580d0 1364struct ring_buffer_event *
e77405ad
SR
1365trace_current_buffer_lock_reserve(struct ring_buffer **current_rb,
1366 int type, unsigned long len,
ef5580d0
SR
1367 unsigned long flags, int pc)
1368{
12883efb 1369 *current_rb = global_trace.trace_buffer.buffer;
e77405ad 1370 return trace_buffer_lock_reserve(*current_rb,
ef5580d0
SR
1371 type, len, flags, pc);
1372}
94487d6d 1373EXPORT_SYMBOL_GPL(trace_current_buffer_lock_reserve);
ef5580d0 1374
e77405ad
SR
1375void trace_current_buffer_unlock_commit(struct ring_buffer *buffer,
1376 struct ring_buffer_event *event,
ef5580d0
SR
1377 unsigned long flags, int pc)
1378{
0d5c6e1c 1379 __trace_buffer_unlock_commit(buffer, event, flags, pc);
07edf712 1380}
94487d6d 1381EXPORT_SYMBOL_GPL(trace_current_buffer_unlock_commit);
07edf712 1382
0d5c6e1c
SR
1383void trace_buffer_unlock_commit_regs(struct ring_buffer *buffer,
1384 struct ring_buffer_event *event,
1385 unsigned long flags, int pc,
1386 struct pt_regs *regs)
1fd8df2c 1387{
7ffbd48d 1388 __buffer_unlock_commit(buffer, event);
1fd8df2c
MH
1389
1390 ftrace_trace_stack_regs(buffer, flags, 0, pc, regs);
1391 ftrace_trace_userstack(buffer, flags, pc);
1392}
0d5c6e1c 1393EXPORT_SYMBOL_GPL(trace_buffer_unlock_commit_regs);
1fd8df2c 1394
e77405ad
SR
1395void trace_current_buffer_discard_commit(struct ring_buffer *buffer,
1396 struct ring_buffer_event *event)
77d9f465 1397{
e77405ad 1398 ring_buffer_discard_commit(buffer, event);
ef5580d0 1399}
12acd473 1400EXPORT_SYMBOL_GPL(trace_current_buffer_discard_commit);
ef5580d0 1401
e309b41d 1402void
7be42151 1403trace_function(struct trace_array *tr,
38697053
SR
1404 unsigned long ip, unsigned long parent_ip, unsigned long flags,
1405 int pc)
bc0c38d1 1406{
e1112b4d 1407 struct ftrace_event_call *call = &event_function;
12883efb 1408 struct ring_buffer *buffer = tr->trace_buffer.buffer;
3928a8a2 1409 struct ring_buffer_event *event;
777e208d 1410 struct ftrace_entry *entry;
bc0c38d1 1411
d769041f 1412 /* If we are reading the ring buffer, don't trace */
dd17c8f7 1413 if (unlikely(__this_cpu_read(ftrace_cpu_disabled)))
d769041f
SR
1414 return;
1415
e77405ad 1416 event = trace_buffer_lock_reserve(buffer, TRACE_FN, sizeof(*entry),
51a763dd 1417 flags, pc);
3928a8a2
SR
1418 if (!event)
1419 return;
1420 entry = ring_buffer_event_data(event);
777e208d
SR
1421 entry->ip = ip;
1422 entry->parent_ip = parent_ip;
e1112b4d 1423
e77405ad 1424 if (!filter_check_discard(call, entry, buffer, event))
7ffbd48d 1425 __buffer_unlock_commit(buffer, event);
bc0c38d1
SR
1426}
1427
e309b41d 1428void
2e0f5761 1429ftrace(struct trace_array *tr, struct trace_array_cpu *data,
38697053
SR
1430 unsigned long ip, unsigned long parent_ip, unsigned long flags,
1431 int pc)
2e0f5761
IM
1432{
1433 if (likely(!atomic_read(&data->disabled)))
7be42151 1434 trace_function(tr, ip, parent_ip, flags, pc);
2e0f5761
IM
1435}
1436
c0a0d0d3 1437#ifdef CONFIG_STACKTRACE
4a9bd3f1
SR
1438
1439#define FTRACE_STACK_MAX_ENTRIES (PAGE_SIZE / sizeof(unsigned long))
1440struct ftrace_stack {
1441 unsigned long calls[FTRACE_STACK_MAX_ENTRIES];
1442};
1443
1444static DEFINE_PER_CPU(struct ftrace_stack, ftrace_stack);
1445static DEFINE_PER_CPU(int, ftrace_stack_reserve);
1446
e77405ad 1447static void __ftrace_trace_stack(struct ring_buffer *buffer,
53614991 1448 unsigned long flags,
1fd8df2c 1449 int skip, int pc, struct pt_regs *regs)
86387f7e 1450{
e1112b4d 1451 struct ftrace_event_call *call = &event_kernel_stack;
3928a8a2 1452 struct ring_buffer_event *event;
777e208d 1453 struct stack_entry *entry;
86387f7e 1454 struct stack_trace trace;
4a9bd3f1
SR
1455 int use_stack;
1456 int size = FTRACE_STACK_ENTRIES;
1457
1458 trace.nr_entries = 0;
1459 trace.skip = skip;
1460
1461 /*
1462 * Since events can happen in NMIs there's no safe way to
1463 * use the per cpu ftrace_stacks. We reserve it and if an interrupt
1464 * or NMI comes in, it will just have to use the default
1465 * FTRACE_STACK_SIZE.
1466 */
1467 preempt_disable_notrace();
1468
82146529 1469 use_stack = __this_cpu_inc_return(ftrace_stack_reserve);
4a9bd3f1
SR
1470 /*
1471 * We don't need any atomic variables, just a barrier.
1472 * If an interrupt comes in, we don't care, because it would
1473 * have exited and put the counter back to what we want.
1474 * We just need a barrier to keep gcc from moving things
1475 * around.
1476 */
1477 barrier();
1478 if (use_stack == 1) {
1479 trace.entries = &__get_cpu_var(ftrace_stack).calls[0];
1480 trace.max_entries = FTRACE_STACK_MAX_ENTRIES;
1481
1482 if (regs)
1483 save_stack_trace_regs(regs, &trace);
1484 else
1485 save_stack_trace(&trace);
1486
1487 if (trace.nr_entries > size)
1488 size = trace.nr_entries;
1489 } else
1490 /* From now on, use_stack is a boolean */
1491 use_stack = 0;
1492
1493 size *= sizeof(unsigned long);
86387f7e 1494
e77405ad 1495 event = trace_buffer_lock_reserve(buffer, TRACE_STACK,
4a9bd3f1 1496 sizeof(*entry) + size, flags, pc);
3928a8a2 1497 if (!event)
4a9bd3f1
SR
1498 goto out;
1499 entry = ring_buffer_event_data(event);
86387f7e 1500
4a9bd3f1
SR
1501 memset(&entry->caller, 0, size);
1502
1503 if (use_stack)
1504 memcpy(&entry->caller, trace.entries,
1505 trace.nr_entries * sizeof(unsigned long));
1506 else {
1507 trace.max_entries = FTRACE_STACK_ENTRIES;
1508 trace.entries = entry->caller;
1509 if (regs)
1510 save_stack_trace_regs(regs, &trace);
1511 else
1512 save_stack_trace(&trace);
1513 }
1514
1515 entry->size = trace.nr_entries;
86387f7e 1516
e77405ad 1517 if (!filter_check_discard(call, entry, buffer, event))
7ffbd48d 1518 __buffer_unlock_commit(buffer, event);
4a9bd3f1
SR
1519
1520 out:
1521 /* Again, don't let gcc optimize things here */
1522 barrier();
82146529 1523 __this_cpu_dec(ftrace_stack_reserve);
4a9bd3f1
SR
1524 preempt_enable_notrace();
1525
f0a920d5
IM
1526}
1527
1fd8df2c
MH
1528void ftrace_trace_stack_regs(struct ring_buffer *buffer, unsigned long flags,
1529 int skip, int pc, struct pt_regs *regs)
1530{
1531 if (!(trace_flags & TRACE_ITER_STACKTRACE))
1532 return;
1533
1534 __ftrace_trace_stack(buffer, flags, skip, pc, regs);
1535}
1536
e77405ad
SR
1537void ftrace_trace_stack(struct ring_buffer *buffer, unsigned long flags,
1538 int skip, int pc)
53614991
SR
1539{
1540 if (!(trace_flags & TRACE_ITER_STACKTRACE))
1541 return;
1542
1fd8df2c 1543 __ftrace_trace_stack(buffer, flags, skip, pc, NULL);
53614991
SR
1544}
1545
c0a0d0d3
FW
1546void __trace_stack(struct trace_array *tr, unsigned long flags, int skip,
1547 int pc)
38697053 1548{
12883efb 1549 __ftrace_trace_stack(tr->trace_buffer.buffer, flags, skip, pc, NULL);
38697053
SR
1550}
1551
03889384
SR
1552/**
1553 * trace_dump_stack - record a stack back trace in the trace buffer
1554 */
1555void trace_dump_stack(void)
1556{
1557 unsigned long flags;
1558
1559 if (tracing_disabled || tracing_selftest_running)
e36c5458 1560 return;
03889384
SR
1561
1562 local_save_flags(flags);
1563
1564 /* skipping 3 traces, seems to get us at the caller of this function */
12883efb
SRRH
1565 __ftrace_trace_stack(global_trace.trace_buffer.buffer, flags, 3,
1566 preempt_count(), NULL);
03889384
SR
1567}
1568
91e86e56
SR
1569static DEFINE_PER_CPU(int, user_stack_count);
1570
e77405ad
SR
1571void
1572ftrace_trace_userstack(struct ring_buffer *buffer, unsigned long flags, int pc)
02b67518 1573{
e1112b4d 1574 struct ftrace_event_call *call = &event_user_stack;
8d7c6a96 1575 struct ring_buffer_event *event;
02b67518
TE
1576 struct userstack_entry *entry;
1577 struct stack_trace trace;
02b67518
TE
1578
1579 if (!(trace_flags & TRACE_ITER_USERSTACKTRACE))
1580 return;
1581
b6345879
SR
1582 /*
1583 * NMIs can not handle page faults, even with fix ups.
1584 * The save user stack can (and often does) fault.
1585 */
1586 if (unlikely(in_nmi()))
1587 return;
02b67518 1588
91e86e56
SR
1589 /*
1590 * prevent recursion, since the user stack tracing may
1591 * trigger other kernel events.
1592 */
1593 preempt_disable();
1594 if (__this_cpu_read(user_stack_count))
1595 goto out;
1596
1597 __this_cpu_inc(user_stack_count);
1598
e77405ad 1599 event = trace_buffer_lock_reserve(buffer, TRACE_USER_STACK,
51a763dd 1600 sizeof(*entry), flags, pc);
02b67518 1601 if (!event)
1dbd1951 1602 goto out_drop_count;
02b67518 1603 entry = ring_buffer_event_data(event);
02b67518 1604
48659d31 1605 entry->tgid = current->tgid;
02b67518
TE
1606 memset(&entry->caller, 0, sizeof(entry->caller));
1607
1608 trace.nr_entries = 0;
1609 trace.max_entries = FTRACE_STACK_ENTRIES;
1610 trace.skip = 0;
1611 trace.entries = entry->caller;
1612
1613 save_stack_trace_user(&trace);
e77405ad 1614 if (!filter_check_discard(call, entry, buffer, event))
7ffbd48d 1615 __buffer_unlock_commit(buffer, event);
91e86e56 1616
1dbd1951 1617 out_drop_count:
91e86e56 1618 __this_cpu_dec(user_stack_count);
91e86e56
SR
1619 out:
1620 preempt_enable();
02b67518
TE
1621}
1622
4fd27358
HE
1623#ifdef UNUSED
1624static void __trace_userstack(struct trace_array *tr, unsigned long flags)
02b67518 1625{
7be42151 1626 ftrace_trace_userstack(tr, flags, preempt_count());
02b67518 1627}
4fd27358 1628#endif /* UNUSED */
02b67518 1629
c0a0d0d3
FW
1630#endif /* CONFIG_STACKTRACE */
1631
07d777fe
SR
1632/* created for use with alloc_percpu */
1633struct trace_buffer_struct {
1634 char buffer[TRACE_BUF_SIZE];
1635};
1636
1637static struct trace_buffer_struct *trace_percpu_buffer;
1638static struct trace_buffer_struct *trace_percpu_sirq_buffer;
1639static struct trace_buffer_struct *trace_percpu_irq_buffer;
1640static struct trace_buffer_struct *trace_percpu_nmi_buffer;
1641
1642/*
1643 * The buffer used is dependent on the context. There is a per cpu
1644 * buffer for normal context, softirq contex, hard irq context and
1645 * for NMI context. Thise allows for lockless recording.
1646 *
1647 * Note, if the buffers failed to be allocated, then this returns NULL
1648 */
1649static char *get_trace_buf(void)
1650{
1651 struct trace_buffer_struct *percpu_buffer;
07d777fe
SR
1652
1653 /*
1654 * If we have allocated per cpu buffers, then we do not
1655 * need to do any locking.
1656 */
1657 if (in_nmi())
1658 percpu_buffer = trace_percpu_nmi_buffer;
1659 else if (in_irq())
1660 percpu_buffer = trace_percpu_irq_buffer;
1661 else if (in_softirq())
1662 percpu_buffer = trace_percpu_sirq_buffer;
1663 else
1664 percpu_buffer = trace_percpu_buffer;
1665
1666 if (!percpu_buffer)
1667 return NULL;
1668
d8a0349c 1669 return this_cpu_ptr(&percpu_buffer->buffer[0]);
07d777fe
SR
1670}
1671
1672static int alloc_percpu_trace_buffer(void)
1673{
1674 struct trace_buffer_struct *buffers;
1675 struct trace_buffer_struct *sirq_buffers;
1676 struct trace_buffer_struct *irq_buffers;
1677 struct trace_buffer_struct *nmi_buffers;
1678
1679 buffers = alloc_percpu(struct trace_buffer_struct);
1680 if (!buffers)
1681 goto err_warn;
1682
1683 sirq_buffers = alloc_percpu(struct trace_buffer_struct);
1684 if (!sirq_buffers)
1685 goto err_sirq;
1686
1687 irq_buffers = alloc_percpu(struct trace_buffer_struct);
1688 if (!irq_buffers)
1689 goto err_irq;
1690
1691 nmi_buffers = alloc_percpu(struct trace_buffer_struct);
1692 if (!nmi_buffers)
1693 goto err_nmi;
1694
1695 trace_percpu_buffer = buffers;
1696 trace_percpu_sirq_buffer = sirq_buffers;
1697 trace_percpu_irq_buffer = irq_buffers;
1698 trace_percpu_nmi_buffer = nmi_buffers;
1699
1700 return 0;
1701
1702 err_nmi:
1703 free_percpu(irq_buffers);
1704 err_irq:
1705 free_percpu(sirq_buffers);
1706 err_sirq:
1707 free_percpu(buffers);
1708 err_warn:
1709 WARN(1, "Could not allocate percpu trace_printk buffer");
1710 return -ENOMEM;
1711}
1712
81698831
SR
1713static int buffers_allocated;
1714
07d777fe
SR
1715void trace_printk_init_buffers(void)
1716{
07d777fe
SR
1717 if (buffers_allocated)
1718 return;
1719
1720 if (alloc_percpu_trace_buffer())
1721 return;
1722
1723 pr_info("ftrace: Allocated trace_printk buffers\n");
1724
b382ede6
SR
1725 /* Expand the buffers to set size */
1726 tracing_update_buffers();
1727
07d777fe 1728 buffers_allocated = 1;
81698831
SR
1729
1730 /*
1731 * trace_printk_init_buffers() can be called by modules.
1732 * If that happens, then we need to start cmdline recording
1733 * directly here. If the global_trace.buffer is already
1734 * allocated here, then this was called by module code.
1735 */
12883efb 1736 if (global_trace.trace_buffer.buffer)
81698831
SR
1737 tracing_start_cmdline_record();
1738}
1739
1740void trace_printk_start_comm(void)
1741{
1742 /* Start tracing comms if trace printk is set */
1743 if (!buffers_allocated)
1744 return;
1745 tracing_start_cmdline_record();
1746}
1747
1748static void trace_printk_start_stop_comm(int enabled)
1749{
1750 if (!buffers_allocated)
1751 return;
1752
1753 if (enabled)
1754 tracing_start_cmdline_record();
1755 else
1756 tracing_stop_cmdline_record();
07d777fe
SR
1757}
1758
769b0441 1759/**
48ead020 1760 * trace_vbprintk - write binary msg to tracing buffer
769b0441
FW
1761 *
1762 */
40ce74f1 1763int trace_vbprintk(unsigned long ip, const char *fmt, va_list args)
769b0441 1764{
e1112b4d 1765 struct ftrace_event_call *call = &event_bprint;
769b0441 1766 struct ring_buffer_event *event;
e77405ad 1767 struct ring_buffer *buffer;
769b0441 1768 struct trace_array *tr = &global_trace;
48ead020 1769 struct bprint_entry *entry;
769b0441 1770 unsigned long flags;
07d777fe
SR
1771 char *tbuffer;
1772 int len = 0, size, pc;
769b0441
FW
1773
1774 if (unlikely(tracing_selftest_running || tracing_disabled))
1775 return 0;
1776
1777 /* Don't pollute graph traces with trace_vprintk internals */
1778 pause_graph_tracing();
1779
1780 pc = preempt_count();
5168ae50 1781 preempt_disable_notrace();
769b0441 1782
07d777fe
SR
1783 tbuffer = get_trace_buf();
1784 if (!tbuffer) {
1785 len = 0;
769b0441 1786 goto out;
07d777fe 1787 }
769b0441 1788
07d777fe 1789 len = vbin_printf((u32 *)tbuffer, TRACE_BUF_SIZE/sizeof(int), fmt, args);
769b0441 1790
07d777fe
SR
1791 if (len > TRACE_BUF_SIZE/sizeof(int) || len < 0)
1792 goto out;
769b0441 1793
07d777fe 1794 local_save_flags(flags);
769b0441 1795 size = sizeof(*entry) + sizeof(u32) * len;
12883efb 1796 buffer = tr->trace_buffer.buffer;
e77405ad
SR
1797 event = trace_buffer_lock_reserve(buffer, TRACE_BPRINT, size,
1798 flags, pc);
769b0441 1799 if (!event)
07d777fe 1800 goto out;
769b0441
FW
1801 entry = ring_buffer_event_data(event);
1802 entry->ip = ip;
769b0441
FW
1803 entry->fmt = fmt;
1804
07d777fe 1805 memcpy(entry->buf, tbuffer, sizeof(u32) * len);
d931369b 1806 if (!filter_check_discard(call, entry, buffer, event)) {
7ffbd48d 1807 __buffer_unlock_commit(buffer, event);
d931369b
SR
1808 ftrace_trace_stack(buffer, flags, 6, pc);
1809 }
769b0441 1810
769b0441 1811out:
5168ae50 1812 preempt_enable_notrace();
769b0441
FW
1813 unpause_graph_tracing();
1814
1815 return len;
1816}
48ead020
FW
1817EXPORT_SYMBOL_GPL(trace_vbprintk);
1818
12883efb
SRRH
1819static int
1820__trace_array_vprintk(struct ring_buffer *buffer,
1821 unsigned long ip, const char *fmt, va_list args)
48ead020 1822{
e1112b4d 1823 struct ftrace_event_call *call = &event_print;
48ead020 1824 struct ring_buffer_event *event;
07d777fe 1825 int len = 0, size, pc;
48ead020 1826 struct print_entry *entry;
07d777fe
SR
1827 unsigned long flags;
1828 char *tbuffer;
48ead020
FW
1829
1830 if (tracing_disabled || tracing_selftest_running)
1831 return 0;
1832
07d777fe
SR
1833 /* Don't pollute graph traces with trace_vprintk internals */
1834 pause_graph_tracing();
1835
48ead020
FW
1836 pc = preempt_count();
1837 preempt_disable_notrace();
48ead020 1838
07d777fe
SR
1839
1840 tbuffer = get_trace_buf();
1841 if (!tbuffer) {
1842 len = 0;
48ead020 1843 goto out;
07d777fe 1844 }
48ead020 1845
07d777fe
SR
1846 len = vsnprintf(tbuffer, TRACE_BUF_SIZE, fmt, args);
1847 if (len > TRACE_BUF_SIZE)
1848 goto out;
48ead020 1849
07d777fe 1850 local_save_flags(flags);
48ead020 1851 size = sizeof(*entry) + len + 1;
e77405ad 1852 event = trace_buffer_lock_reserve(buffer, TRACE_PRINT, size,
07d777fe 1853 flags, pc);
48ead020 1854 if (!event)
07d777fe 1855 goto out;
48ead020 1856 entry = ring_buffer_event_data(event);
c13d2f7c 1857 entry->ip = ip;
48ead020 1858
07d777fe 1859 memcpy(&entry->buf, tbuffer, len);
c13d2f7c 1860 entry->buf[len] = '\0';
d931369b 1861 if (!filter_check_discard(call, entry, buffer, event)) {
7ffbd48d 1862 __buffer_unlock_commit(buffer, event);
07d777fe 1863 ftrace_trace_stack(buffer, flags, 6, pc);
d931369b 1864 }
48ead020
FW
1865 out:
1866 preempt_enable_notrace();
07d777fe 1867 unpause_graph_tracing();
48ead020
FW
1868
1869 return len;
1870}
659372d3 1871
12883efb
SRRH
1872int trace_array_vprintk(struct trace_array *tr,
1873 unsigned long ip, const char *fmt, va_list args)
1874{
1875 return __trace_array_vprintk(tr->trace_buffer.buffer, ip, fmt, args);
1876}
1877
1878int trace_array_printk(struct trace_array *tr,
1879 unsigned long ip, const char *fmt, ...)
1880{
1881 int ret;
1882 va_list ap;
1883
1884 if (!(trace_flags & TRACE_ITER_PRINTK))
1885 return 0;
1886
1887 va_start(ap, fmt);
1888 ret = trace_array_vprintk(tr, ip, fmt, ap);
1889 va_end(ap);
1890 return ret;
1891}
1892
1893int trace_array_printk_buf(struct ring_buffer *buffer,
1894 unsigned long ip, const char *fmt, ...)
1895{
1896 int ret;
1897 va_list ap;
1898
1899 if (!(trace_flags & TRACE_ITER_PRINTK))
1900 return 0;
1901
1902 va_start(ap, fmt);
1903 ret = __trace_array_vprintk(buffer, ip, fmt, ap);
1904 va_end(ap);
1905 return ret;
1906}
1907
659372d3
SR
1908int trace_vprintk(unsigned long ip, const char *fmt, va_list args)
1909{
a813a159 1910 return trace_array_vprintk(&global_trace, ip, fmt, args);
659372d3 1911}
769b0441
FW
1912EXPORT_SYMBOL_GPL(trace_vprintk);
1913
e2ac8ef5 1914static void trace_iterator_increment(struct trace_iterator *iter)
5a90f577 1915{
6d158a81
SR
1916 struct ring_buffer_iter *buf_iter = trace_buffer_iter(iter, iter->cpu);
1917
5a90f577 1918 iter->idx++;
6d158a81
SR
1919 if (buf_iter)
1920 ring_buffer_read(buf_iter, NULL);
5a90f577
SR
1921}
1922
e309b41d 1923static struct trace_entry *
bc21b478
SR
1924peek_next_entry(struct trace_iterator *iter, int cpu, u64 *ts,
1925 unsigned long *lost_events)
dd0e545f 1926{
3928a8a2 1927 struct ring_buffer_event *event;
6d158a81 1928 struct ring_buffer_iter *buf_iter = trace_buffer_iter(iter, cpu);
dd0e545f 1929
d769041f
SR
1930 if (buf_iter)
1931 event = ring_buffer_iter_peek(buf_iter, ts);
1932 else
12883efb 1933 event = ring_buffer_peek(iter->trace_buffer->buffer, cpu, ts,
bc21b478 1934 lost_events);
d769041f 1935
4a9bd3f1
SR
1936 if (event) {
1937 iter->ent_size = ring_buffer_event_length(event);
1938 return ring_buffer_event_data(event);
1939 }
1940 iter->ent_size = 0;
1941 return NULL;
dd0e545f 1942}
d769041f 1943
dd0e545f 1944static struct trace_entry *
bc21b478
SR
1945__find_next_entry(struct trace_iterator *iter, int *ent_cpu,
1946 unsigned long *missing_events, u64 *ent_ts)
bc0c38d1 1947{
12883efb 1948 struct ring_buffer *buffer = iter->trace_buffer->buffer;
bc0c38d1 1949 struct trace_entry *ent, *next = NULL;
aa27497c 1950 unsigned long lost_events = 0, next_lost = 0;
b04cc6b1 1951 int cpu_file = iter->cpu_file;
3928a8a2 1952 u64 next_ts = 0, ts;
bc0c38d1 1953 int next_cpu = -1;
12b5da34 1954 int next_size = 0;
bc0c38d1
SR
1955 int cpu;
1956
b04cc6b1
FW
1957 /*
1958 * If we are in a per_cpu trace file, don't bother by iterating over
1959 * all cpu and peek directly.
1960 */
ae3b5093 1961 if (cpu_file > RING_BUFFER_ALL_CPUS) {
b04cc6b1
FW
1962 if (ring_buffer_empty_cpu(buffer, cpu_file))
1963 return NULL;
bc21b478 1964 ent = peek_next_entry(iter, cpu_file, ent_ts, missing_events);
b04cc6b1
FW
1965 if (ent_cpu)
1966 *ent_cpu = cpu_file;
1967
1968 return ent;
1969 }
1970
ab46428c 1971 for_each_tracing_cpu(cpu) {
dd0e545f 1972
3928a8a2
SR
1973 if (ring_buffer_empty_cpu(buffer, cpu))
1974 continue;
dd0e545f 1975
bc21b478 1976 ent = peek_next_entry(iter, cpu, &ts, &lost_events);
dd0e545f 1977
cdd31cd2
IM
1978 /*
1979 * Pick the entry with the smallest timestamp:
1980 */
3928a8a2 1981 if (ent && (!next || ts < next_ts)) {
bc0c38d1
SR
1982 next = ent;
1983 next_cpu = cpu;
3928a8a2 1984 next_ts = ts;
bc21b478 1985 next_lost = lost_events;
12b5da34 1986 next_size = iter->ent_size;
bc0c38d1
SR
1987 }
1988 }
1989
12b5da34
SR
1990 iter->ent_size = next_size;
1991
bc0c38d1
SR
1992 if (ent_cpu)
1993 *ent_cpu = next_cpu;
1994
3928a8a2
SR
1995 if (ent_ts)
1996 *ent_ts = next_ts;
1997
bc21b478
SR
1998 if (missing_events)
1999 *missing_events = next_lost;
2000
bc0c38d1
SR
2001 return next;
2002}
2003
dd0e545f 2004/* Find the next real entry, without updating the iterator itself */
c4a8e8be
FW
2005struct trace_entry *trace_find_next_entry(struct trace_iterator *iter,
2006 int *ent_cpu, u64 *ent_ts)
bc0c38d1 2007{
bc21b478 2008 return __find_next_entry(iter, ent_cpu, NULL, ent_ts);
dd0e545f
SR
2009}
2010
2011/* Find the next real entry, and increment the iterator to the next entry */
955b61e5 2012void *trace_find_next_entry_inc(struct trace_iterator *iter)
dd0e545f 2013{
bc21b478
SR
2014 iter->ent = __find_next_entry(iter, &iter->cpu,
2015 &iter->lost_events, &iter->ts);
dd0e545f 2016
3928a8a2 2017 if (iter->ent)
e2ac8ef5 2018 trace_iterator_increment(iter);
dd0e545f 2019
3928a8a2 2020 return iter->ent ? iter : NULL;
b3806b43 2021}
bc0c38d1 2022
e309b41d 2023static void trace_consume(struct trace_iterator *iter)
b3806b43 2024{
12883efb 2025 ring_buffer_consume(iter->trace_buffer->buffer, iter->cpu, &iter->ts,
bc21b478 2026 &iter->lost_events);
bc0c38d1
SR
2027}
2028
e309b41d 2029static void *s_next(struct seq_file *m, void *v, loff_t *pos)
bc0c38d1
SR
2030{
2031 struct trace_iterator *iter = m->private;
bc0c38d1 2032 int i = (int)*pos;
4e3c3333 2033 void *ent;
bc0c38d1 2034
a63ce5b3
SR
2035 WARN_ON_ONCE(iter->leftover);
2036
bc0c38d1
SR
2037 (*pos)++;
2038
2039 /* can't go backwards */
2040 if (iter->idx > i)
2041 return NULL;
2042
2043 if (iter->idx < 0)
955b61e5 2044 ent = trace_find_next_entry_inc(iter);
bc0c38d1
SR
2045 else
2046 ent = iter;
2047
2048 while (ent && iter->idx < i)
955b61e5 2049 ent = trace_find_next_entry_inc(iter);
bc0c38d1
SR
2050
2051 iter->pos = *pos;
2052
bc0c38d1
SR
2053 return ent;
2054}
2055
955b61e5 2056void tracing_iter_reset(struct trace_iterator *iter, int cpu)
2f26ebd5 2057{
2f26ebd5
SR
2058 struct ring_buffer_event *event;
2059 struct ring_buffer_iter *buf_iter;
2060 unsigned long entries = 0;
2061 u64 ts;
2062
12883efb 2063 per_cpu_ptr(iter->trace_buffer->data, cpu)->skipped_entries = 0;
2f26ebd5 2064
6d158a81
SR
2065 buf_iter = trace_buffer_iter(iter, cpu);
2066 if (!buf_iter)
2f26ebd5
SR
2067 return;
2068
2f26ebd5
SR
2069 ring_buffer_iter_reset(buf_iter);
2070
2071 /*
2072 * We could have the case with the max latency tracers
2073 * that a reset never took place on a cpu. This is evident
2074 * by the timestamp being before the start of the buffer.
2075 */
2076 while ((event = ring_buffer_iter_peek(buf_iter, &ts))) {
12883efb 2077 if (ts >= iter->trace_buffer->time_start)
2f26ebd5
SR
2078 break;
2079 entries++;
2080 ring_buffer_read(buf_iter, NULL);
2081 }
2082
12883efb 2083 per_cpu_ptr(iter->trace_buffer->data, cpu)->skipped_entries = entries;
2f26ebd5
SR
2084}
2085
d7350c3f 2086/*
d7350c3f
FW
2087 * The current tracer is copied to avoid a global locking
2088 * all around.
2089 */
bc0c38d1
SR
2090static void *s_start(struct seq_file *m, loff_t *pos)
2091{
2092 struct trace_iterator *iter = m->private;
2b6080f2 2093 struct trace_array *tr = iter->tr;
b04cc6b1 2094 int cpu_file = iter->cpu_file;
bc0c38d1
SR
2095 void *p = NULL;
2096 loff_t l = 0;
3928a8a2 2097 int cpu;
bc0c38d1 2098
2fd196ec
HT
2099 /*
2100 * copy the tracer to avoid using a global lock all around.
2101 * iter->trace is a copy of current_trace, the pointer to the
2102 * name may be used instead of a strcmp(), as iter->trace->name
2103 * will point to the same string as current_trace->name.
2104 */
bc0c38d1 2105 mutex_lock(&trace_types_lock);
2b6080f2
SR
2106 if (unlikely(tr->current_trace && iter->trace->name != tr->current_trace->name))
2107 *iter->trace = *tr->current_trace;
d7350c3f 2108 mutex_unlock(&trace_types_lock);
bc0c38d1 2109
12883efb 2110#ifdef CONFIG_TRACER_MAX_TRACE
debdd57f
HT
2111 if (iter->snapshot && iter->trace->use_max_tr)
2112 return ERR_PTR(-EBUSY);
12883efb 2113#endif
debdd57f
HT
2114
2115 if (!iter->snapshot)
2116 atomic_inc(&trace_record_cmdline_disabled);
bc0c38d1 2117
bc0c38d1
SR
2118 if (*pos != iter->pos) {
2119 iter->ent = NULL;
2120 iter->cpu = 0;
2121 iter->idx = -1;
2122
ae3b5093 2123 if (cpu_file == RING_BUFFER_ALL_CPUS) {
b04cc6b1 2124 for_each_tracing_cpu(cpu)
2f26ebd5 2125 tracing_iter_reset(iter, cpu);
b04cc6b1 2126 } else
2f26ebd5 2127 tracing_iter_reset(iter, cpu_file);
bc0c38d1 2128
ac91d854 2129 iter->leftover = 0;
bc0c38d1
SR
2130 for (p = iter; p && l < *pos; p = s_next(m, p, &l))
2131 ;
2132
2133 } else {
a63ce5b3
SR
2134 /*
2135 * If we overflowed the seq_file before, then we want
2136 * to just reuse the trace_seq buffer again.
2137 */
2138 if (iter->leftover)
2139 p = iter;
2140 else {
2141 l = *pos - 1;
2142 p = s_next(m, p, &l);
2143 }
bc0c38d1
SR
2144 }
2145
4f535968 2146 trace_event_read_lock();
7e53bd42 2147 trace_access_lock(cpu_file);
bc0c38d1
SR
2148 return p;
2149}
2150
2151static void s_stop(struct seq_file *m, void *p)
2152{
7e53bd42
LJ
2153 struct trace_iterator *iter = m->private;
2154
12883efb 2155#ifdef CONFIG_TRACER_MAX_TRACE
debdd57f
HT
2156 if (iter->snapshot && iter->trace->use_max_tr)
2157 return;
12883efb 2158#endif
debdd57f
HT
2159
2160 if (!iter->snapshot)
2161 atomic_dec(&trace_record_cmdline_disabled);
12883efb 2162
7e53bd42 2163 trace_access_unlock(iter->cpu_file);
4f535968 2164 trace_event_read_unlock();
bc0c38d1
SR
2165}
2166
39eaf7ef 2167static void
12883efb
SRRH
2168get_total_entries(struct trace_buffer *buf,
2169 unsigned long *total, unsigned long *entries)
39eaf7ef
SR
2170{
2171 unsigned long count;
2172 int cpu;
2173
2174 *total = 0;
2175 *entries = 0;
2176
2177 for_each_tracing_cpu(cpu) {
12883efb 2178 count = ring_buffer_entries_cpu(buf->buffer, cpu);
39eaf7ef
SR
2179 /*
2180 * If this buffer has skipped entries, then we hold all
2181 * entries for the trace and we need to ignore the
2182 * ones before the time stamp.
2183 */
12883efb
SRRH
2184 if (per_cpu_ptr(buf->data, cpu)->skipped_entries) {
2185 count -= per_cpu_ptr(buf->data, cpu)->skipped_entries;
39eaf7ef
SR
2186 /* total is the same as the entries */
2187 *total += count;
2188 } else
2189 *total += count +
12883efb 2190 ring_buffer_overrun_cpu(buf->buffer, cpu);
39eaf7ef
SR
2191 *entries += count;
2192 }
2193}
2194
e309b41d 2195static void print_lat_help_header(struct seq_file *m)
bc0c38d1 2196{
a6168353
ME
2197 seq_puts(m, "# _------=> CPU# \n");
2198 seq_puts(m, "# / _-----=> irqs-off \n");
2199 seq_puts(m, "# | / _----=> need-resched \n");
2200 seq_puts(m, "# || / _---=> hardirq/softirq \n");
2201 seq_puts(m, "# ||| / _--=> preempt-depth \n");
e6e1e259
SR
2202 seq_puts(m, "# |||| / delay \n");
2203 seq_puts(m, "# cmd pid ||||| time | caller \n");
2204 seq_puts(m, "# \\ / ||||| \\ | / \n");
bc0c38d1
SR
2205}
2206
12883efb 2207static void print_event_info(struct trace_buffer *buf, struct seq_file *m)
bc0c38d1 2208{
39eaf7ef
SR
2209 unsigned long total;
2210 unsigned long entries;
2211
12883efb 2212 get_total_entries(buf, &total, &entries);
39eaf7ef
SR
2213 seq_printf(m, "# entries-in-buffer/entries-written: %lu/%lu #P:%d\n",
2214 entries, total, num_online_cpus());
2215 seq_puts(m, "#\n");
2216}
2217
12883efb 2218static void print_func_help_header(struct trace_buffer *buf, struct seq_file *m)
39eaf7ef 2219{
12883efb 2220 print_event_info(buf, m);
77271ce4 2221 seq_puts(m, "# TASK-PID CPU# TIMESTAMP FUNCTION\n");
a6168353 2222 seq_puts(m, "# | | | | |\n");
bc0c38d1
SR
2223}
2224
12883efb 2225static void print_func_help_header_irq(struct trace_buffer *buf, struct seq_file *m)
77271ce4 2226{
12883efb 2227 print_event_info(buf, m);
77271ce4
SR
2228 seq_puts(m, "# _-----=> irqs-off\n");
2229 seq_puts(m, "# / _----=> need-resched\n");
2230 seq_puts(m, "# | / _---=> hardirq/softirq\n");
2231 seq_puts(m, "# || / _--=> preempt-depth\n");
2232 seq_puts(m, "# ||| / delay\n");
2233 seq_puts(m, "# TASK-PID CPU# |||| TIMESTAMP FUNCTION\n");
2234 seq_puts(m, "# | | | |||| | |\n");
2235}
bc0c38d1 2236
62b915f1 2237void
bc0c38d1
SR
2238print_trace_header(struct seq_file *m, struct trace_iterator *iter)
2239{
2240 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
12883efb
SRRH
2241 struct trace_buffer *buf = iter->trace_buffer;
2242 struct trace_array_cpu *data = per_cpu_ptr(buf->data, buf->cpu);
2b6080f2 2243 struct tracer *type = iter->trace;
39eaf7ef
SR
2244 unsigned long entries;
2245 unsigned long total;
bc0c38d1
SR
2246 const char *name = "preemption";
2247
d840f718 2248 name = type->name;
bc0c38d1 2249
12883efb 2250 get_total_entries(buf, &total, &entries);
bc0c38d1 2251
888b55dc 2252 seq_printf(m, "# %s latency trace v1.1.5 on %s\n",
bc0c38d1 2253 name, UTS_RELEASE);
888b55dc 2254 seq_puts(m, "# -----------------------------------"
bc0c38d1 2255 "---------------------------------\n");
888b55dc 2256 seq_printf(m, "# latency: %lu us, #%lu/%lu, CPU#%d |"
bc0c38d1 2257 " (M:%s VP:%d, KP:%d, SP:%d HP:%d",
57f50be1 2258 nsecs_to_usecs(data->saved_latency),
bc0c38d1 2259 entries,
4c11d7ae 2260 total,
12883efb 2261 buf->cpu,
bc0c38d1
SR
2262#if defined(CONFIG_PREEMPT_NONE)
2263 "server",
2264#elif defined(CONFIG_PREEMPT_VOLUNTARY)
2265 "desktop",
b5c21b45 2266#elif defined(CONFIG_PREEMPT)
bc0c38d1
SR
2267 "preempt",
2268#else
2269 "unknown",
2270#endif
2271 /* These are reserved for later use */
2272 0, 0, 0, 0);
2273#ifdef CONFIG_SMP
2274 seq_printf(m, " #P:%d)\n", num_online_cpus());
2275#else
2276 seq_puts(m, ")\n");
2277#endif
888b55dc
KM
2278 seq_puts(m, "# -----------------\n");
2279 seq_printf(m, "# | task: %.16s-%d "
bc0c38d1 2280 "(uid:%d nice:%ld policy:%ld rt_prio:%ld)\n",
d20b92ab
EB
2281 data->comm, data->pid,
2282 from_kuid_munged(seq_user_ns(m), data->uid), data->nice,
bc0c38d1 2283 data->policy, data->rt_priority);
888b55dc 2284 seq_puts(m, "# -----------------\n");
bc0c38d1
SR
2285
2286 if (data->critical_start) {
888b55dc 2287 seq_puts(m, "# => started at: ");
214023c3
SR
2288 seq_print_ip_sym(&iter->seq, data->critical_start, sym_flags);
2289 trace_print_seq(m, &iter->seq);
888b55dc 2290 seq_puts(m, "\n# => ended at: ");
214023c3
SR
2291 seq_print_ip_sym(&iter->seq, data->critical_end, sym_flags);
2292 trace_print_seq(m, &iter->seq);
8248ac05 2293 seq_puts(m, "\n#\n");
bc0c38d1
SR
2294 }
2295
888b55dc 2296 seq_puts(m, "#\n");
bc0c38d1
SR
2297}
2298
a309720c
SR
2299static void test_cpu_buff_start(struct trace_iterator *iter)
2300{
2301 struct trace_seq *s = &iter->seq;
2302
12ef7d44
SR
2303 if (!(trace_flags & TRACE_ITER_ANNOTATE))
2304 return;
2305
2306 if (!(iter->iter_flags & TRACE_FILE_ANNOTATE))
2307 return;
2308
4462344e 2309 if (cpumask_test_cpu(iter->cpu, iter->started))
a309720c
SR
2310 return;
2311
12883efb 2312 if (per_cpu_ptr(iter->trace_buffer->data, iter->cpu)->skipped_entries)
2f26ebd5
SR
2313 return;
2314
4462344e 2315 cpumask_set_cpu(iter->cpu, iter->started);
b0dfa978
FW
2316
2317 /* Don't print started cpu buffer for the first entry of the trace */
2318 if (iter->idx > 1)
2319 trace_seq_printf(s, "##### CPU %u buffer started ####\n",
2320 iter->cpu);
a309720c
SR
2321}
2322
2c4f035f 2323static enum print_line_t print_trace_fmt(struct trace_iterator *iter)
bc0c38d1 2324{
214023c3 2325 struct trace_seq *s = &iter->seq;
bc0c38d1 2326 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
4e3c3333 2327 struct trace_entry *entry;
f633cef0 2328 struct trace_event *event;
bc0c38d1 2329
4e3c3333 2330 entry = iter->ent;
dd0e545f 2331
a309720c
SR
2332 test_cpu_buff_start(iter);
2333
c4a8e8be 2334 event = ftrace_find_event(entry->type);
bc0c38d1 2335
c4a8e8be 2336 if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
27d48be8
SR
2337 if (iter->iter_flags & TRACE_FILE_LAT_FMT) {
2338 if (!trace_print_lat_context(iter))
2339 goto partial;
2340 } else {
2341 if (!trace_print_context(iter))
2342 goto partial;
2343 }
c4a8e8be 2344 }
bc0c38d1 2345
268ccda0 2346 if (event)
a9a57763 2347 return event->funcs->trace(iter, sym_flags, event);
d9793bd8
ACM
2348
2349 if (!trace_seq_printf(s, "Unknown type %d\n", entry->type))
2350 goto partial;
02b67518 2351
2c4f035f 2352 return TRACE_TYPE_HANDLED;
d9793bd8
ACM
2353partial:
2354 return TRACE_TYPE_PARTIAL_LINE;
bc0c38d1
SR
2355}
2356
2c4f035f 2357static enum print_line_t print_raw_fmt(struct trace_iterator *iter)
f9896bf3
IM
2358{
2359 struct trace_seq *s = &iter->seq;
2360 struct trace_entry *entry;
f633cef0 2361 struct trace_event *event;
f9896bf3
IM
2362
2363 entry = iter->ent;
dd0e545f 2364
c4a8e8be 2365 if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
d9793bd8
ACM
2366 if (!trace_seq_printf(s, "%d %d %llu ",
2367 entry->pid, iter->cpu, iter->ts))
2368 goto partial;
c4a8e8be 2369 }
f9896bf3 2370
f633cef0 2371 event = ftrace_find_event(entry->type);
268ccda0 2372 if (event)
a9a57763 2373 return event->funcs->raw(iter, 0, event);
d9793bd8
ACM
2374
2375 if (!trace_seq_printf(s, "%d ?\n", entry->type))
2376 goto partial;
777e208d 2377
2c4f035f 2378 return TRACE_TYPE_HANDLED;
d9793bd8
ACM
2379partial:
2380 return TRACE_TYPE_PARTIAL_LINE;
f9896bf3
IM
2381}
2382
2c4f035f 2383static enum print_line_t print_hex_fmt(struct trace_iterator *iter)
5e3ca0ec
IM
2384{
2385 struct trace_seq *s = &iter->seq;
2386 unsigned char newline = '\n';
2387 struct trace_entry *entry;
f633cef0 2388 struct trace_event *event;
5e3ca0ec
IM
2389
2390 entry = iter->ent;
dd0e545f 2391
c4a8e8be
FW
2392 if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
2393 SEQ_PUT_HEX_FIELD_RET(s, entry->pid);
2394 SEQ_PUT_HEX_FIELD_RET(s, iter->cpu);
2395 SEQ_PUT_HEX_FIELD_RET(s, iter->ts);
2396 }
5e3ca0ec 2397
f633cef0 2398 event = ftrace_find_event(entry->type);
268ccda0 2399 if (event) {
a9a57763 2400 enum print_line_t ret = event->funcs->hex(iter, 0, event);
d9793bd8
ACM
2401 if (ret != TRACE_TYPE_HANDLED)
2402 return ret;
2403 }
7104f300 2404
5e3ca0ec
IM
2405 SEQ_PUT_FIELD_RET(s, newline);
2406
2c4f035f 2407 return TRACE_TYPE_HANDLED;
5e3ca0ec
IM
2408}
2409
2c4f035f 2410static enum print_line_t print_bin_fmt(struct trace_iterator *iter)
cb0f12aa
IM
2411{
2412 struct trace_seq *s = &iter->seq;
2413 struct trace_entry *entry;
f633cef0 2414 struct trace_event *event;
cb0f12aa
IM
2415
2416 entry = iter->ent;
dd0e545f 2417
c4a8e8be
FW
2418 if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
2419 SEQ_PUT_FIELD_RET(s, entry->pid);
1830b52d 2420 SEQ_PUT_FIELD_RET(s, iter->cpu);
c4a8e8be
FW
2421 SEQ_PUT_FIELD_RET(s, iter->ts);
2422 }
cb0f12aa 2423
f633cef0 2424 event = ftrace_find_event(entry->type);
a9a57763
SR
2425 return event ? event->funcs->binary(iter, 0, event) :
2426 TRACE_TYPE_HANDLED;
cb0f12aa
IM
2427}
2428
62b915f1 2429int trace_empty(struct trace_iterator *iter)
bc0c38d1 2430{
6d158a81 2431 struct ring_buffer_iter *buf_iter;
bc0c38d1
SR
2432 int cpu;
2433
9aba60fe 2434 /* If we are looking at one CPU buffer, only check that one */
ae3b5093 2435 if (iter->cpu_file != RING_BUFFER_ALL_CPUS) {
9aba60fe 2436 cpu = iter->cpu_file;
6d158a81
SR
2437 buf_iter = trace_buffer_iter(iter, cpu);
2438 if (buf_iter) {
2439 if (!ring_buffer_iter_empty(buf_iter))
9aba60fe
SR
2440 return 0;
2441 } else {
12883efb 2442 if (!ring_buffer_empty_cpu(iter->trace_buffer->buffer, cpu))
9aba60fe
SR
2443 return 0;
2444 }
2445 return 1;
2446 }
2447
ab46428c 2448 for_each_tracing_cpu(cpu) {
6d158a81
SR
2449 buf_iter = trace_buffer_iter(iter, cpu);
2450 if (buf_iter) {
2451 if (!ring_buffer_iter_empty(buf_iter))
d769041f
SR
2452 return 0;
2453 } else {
12883efb 2454 if (!ring_buffer_empty_cpu(iter->trace_buffer->buffer, cpu))
d769041f
SR
2455 return 0;
2456 }
bc0c38d1 2457 }
d769041f 2458
797d3712 2459 return 1;
bc0c38d1
SR
2460}
2461
4f535968 2462/* Called with trace_event_read_lock() held. */
955b61e5 2463enum print_line_t print_trace_line(struct trace_iterator *iter)
f9896bf3 2464{
2c4f035f
FW
2465 enum print_line_t ret;
2466
ee5e51f5
JO
2467 if (iter->lost_events &&
2468 !trace_seq_printf(&iter->seq, "CPU:%d [LOST %lu EVENTS]\n",
2469 iter->cpu, iter->lost_events))
2470 return TRACE_TYPE_PARTIAL_LINE;
bc21b478 2471
2c4f035f
FW
2472 if (iter->trace && iter->trace->print_line) {
2473 ret = iter->trace->print_line(iter);
2474 if (ret != TRACE_TYPE_UNHANDLED)
2475 return ret;
2476 }
72829bc3 2477
48ead020
FW
2478 if (iter->ent->type == TRACE_BPRINT &&
2479 trace_flags & TRACE_ITER_PRINTK &&
2480 trace_flags & TRACE_ITER_PRINTK_MSGONLY)
5ef841f6 2481 return trace_print_bprintk_msg_only(iter);
48ead020 2482
66896a85
FW
2483 if (iter->ent->type == TRACE_PRINT &&
2484 trace_flags & TRACE_ITER_PRINTK &&
2485 trace_flags & TRACE_ITER_PRINTK_MSGONLY)
5ef841f6 2486 return trace_print_printk_msg_only(iter);
66896a85 2487
cb0f12aa
IM
2488 if (trace_flags & TRACE_ITER_BIN)
2489 return print_bin_fmt(iter);
2490
5e3ca0ec
IM
2491 if (trace_flags & TRACE_ITER_HEX)
2492 return print_hex_fmt(iter);
2493
f9896bf3
IM
2494 if (trace_flags & TRACE_ITER_RAW)
2495 return print_raw_fmt(iter);
2496
f9896bf3
IM
2497 return print_trace_fmt(iter);
2498}
2499
7e9a49ef
JO
2500void trace_latency_header(struct seq_file *m)
2501{
2502 struct trace_iterator *iter = m->private;
2503
2504 /* print nothing if the buffers are empty */
2505 if (trace_empty(iter))
2506 return;
2507
2508 if (iter->iter_flags & TRACE_FILE_LAT_FMT)
2509 print_trace_header(m, iter);
2510
2511 if (!(trace_flags & TRACE_ITER_VERBOSE))
2512 print_lat_help_header(m);
2513}
2514
62b915f1
JO
2515void trace_default_header(struct seq_file *m)
2516{
2517 struct trace_iterator *iter = m->private;
2518
f56e7f8e
JO
2519 if (!(trace_flags & TRACE_ITER_CONTEXT_INFO))
2520 return;
2521
62b915f1
JO
2522 if (iter->iter_flags & TRACE_FILE_LAT_FMT) {
2523 /* print nothing if the buffers are empty */
2524 if (trace_empty(iter))
2525 return;
2526 print_trace_header(m, iter);
2527 if (!(trace_flags & TRACE_ITER_VERBOSE))
2528 print_lat_help_header(m);
2529 } else {
77271ce4
SR
2530 if (!(trace_flags & TRACE_ITER_VERBOSE)) {
2531 if (trace_flags & TRACE_ITER_IRQ_INFO)
12883efb 2532 print_func_help_header_irq(iter->trace_buffer, m);
77271ce4 2533 else
12883efb 2534 print_func_help_header(iter->trace_buffer, m);
77271ce4 2535 }
62b915f1
JO
2536 }
2537}
2538
e0a413f6
SR
2539static void test_ftrace_alive(struct seq_file *m)
2540{
2541 if (!ftrace_is_dead())
2542 return;
2543 seq_printf(m, "# WARNING: FUNCTION TRACING IS CORRUPTED\n");
2544 seq_printf(m, "# MAY BE MISSING FUNCTION EVENTS\n");
2545}
2546
d8741e2e 2547#ifdef CONFIG_TRACER_MAX_TRACE
f1affcaa
SRRH
2548static void show_snapshot_main_help(struct seq_file *m)
2549{
2550 seq_printf(m, "# echo 0 > snapshot : Clears and frees snapshot buffer\n");
2551 seq_printf(m, "# echo 1 > snapshot : Allocates snapshot buffer, if not already allocated.\n");
2552 seq_printf(m, "# Takes a snapshot of the main buffer.\n");
2553 seq_printf(m, "# echo 2 > snapshot : Clears snapshot buffer (but does not allocate)\n");
2554 seq_printf(m, "# (Doesn't have to be '2' works with any number that\n");
2555 seq_printf(m, "# is not a '0' or '1')\n");
2556}
2557
2558static void show_snapshot_percpu_help(struct seq_file *m)
2559{
2560 seq_printf(m, "# echo 0 > snapshot : Invalid for per_cpu snapshot file.\n");
2561#ifdef CONFIG_RING_BUFFER_ALLOW_SWAP
2562 seq_printf(m, "# echo 1 > snapshot : Allocates snapshot buffer, if not already allocated.\n");
2563 seq_printf(m, "# Takes a snapshot of the main buffer for this cpu.\n");
2564#else
2565 seq_printf(m, "# echo 1 > snapshot : Not supported with this kernel.\n");
2566 seq_printf(m, "# Must use main snapshot file to allocate.\n");
2567#endif
2568 seq_printf(m, "# echo 2 > snapshot : Clears this cpu's snapshot buffer (but does not allocate)\n");
2569 seq_printf(m, "# (Doesn't have to be '2' works with any number that\n");
2570 seq_printf(m, "# is not a '0' or '1')\n");
2571}
2572
d8741e2e
SRRH
2573static void print_snapshot_help(struct seq_file *m, struct trace_iterator *iter)
2574{
45ad21ca 2575 if (iter->tr->allocated_snapshot)
d8741e2e
SRRH
2576 seq_printf(m, "#\n# * Snapshot is allocated *\n#\n");
2577 else
2578 seq_printf(m, "#\n# * Snapshot is freed *\n#\n");
2579
2580 seq_printf(m, "# Snapshot commands:\n");
f1affcaa
SRRH
2581 if (iter->cpu_file == RING_BUFFER_ALL_CPUS)
2582 show_snapshot_main_help(m);
2583 else
2584 show_snapshot_percpu_help(m);
d8741e2e
SRRH
2585}
2586#else
2587/* Should never be called */
2588static inline void print_snapshot_help(struct seq_file *m, struct trace_iterator *iter) { }
2589#endif
2590
bc0c38d1
SR
2591static int s_show(struct seq_file *m, void *v)
2592{
2593 struct trace_iterator *iter = v;
a63ce5b3 2594 int ret;
bc0c38d1
SR
2595
2596 if (iter->ent == NULL) {
2597 if (iter->tr) {
2598 seq_printf(m, "# tracer: %s\n", iter->trace->name);
2599 seq_puts(m, "#\n");
e0a413f6 2600 test_ftrace_alive(m);
bc0c38d1 2601 }
d8741e2e
SRRH
2602 if (iter->snapshot && trace_empty(iter))
2603 print_snapshot_help(m, iter);
2604 else if (iter->trace && iter->trace->print_header)
8bba1bf5 2605 iter->trace->print_header(m);
62b915f1
JO
2606 else
2607 trace_default_header(m);
2608
a63ce5b3
SR
2609 } else if (iter->leftover) {
2610 /*
2611 * If we filled the seq_file buffer earlier, we
2612 * want to just show it now.
2613 */
2614 ret = trace_print_seq(m, &iter->seq);
2615
2616 /* ret should this time be zero, but you never know */
2617 iter->leftover = ret;
2618
bc0c38d1 2619 } else {
f9896bf3 2620 print_trace_line(iter);
a63ce5b3
SR
2621 ret = trace_print_seq(m, &iter->seq);
2622 /*
2623 * If we overflow the seq_file buffer, then it will
2624 * ask us for this data again at start up.
2625 * Use that instead.
2626 * ret is 0 if seq_file write succeeded.
2627 * -1 otherwise.
2628 */
2629 iter->leftover = ret;
bc0c38d1
SR
2630 }
2631
2632 return 0;
2633}
2634
88e9d34c 2635static const struct seq_operations tracer_seq_ops = {
4bf39a94
IM
2636 .start = s_start,
2637 .next = s_next,
2638 .stop = s_stop,
2639 .show = s_show,
bc0c38d1
SR
2640};
2641
e309b41d 2642static struct trace_iterator *
debdd57f 2643__tracing_open(struct inode *inode, struct file *file, bool snapshot)
bc0c38d1 2644{
2b6080f2
SR
2645 struct trace_cpu *tc = inode->i_private;
2646 struct trace_array *tr = tc->tr;
bc0c38d1 2647 struct trace_iterator *iter;
50e18b94 2648 int cpu;
bc0c38d1 2649
85a2f9b4
SR
2650 if (tracing_disabled)
2651 return ERR_PTR(-ENODEV);
60a11774 2652
50e18b94 2653 iter = __seq_open_private(file, &tracer_seq_ops, sizeof(*iter));
85a2f9b4
SR
2654 if (!iter)
2655 return ERR_PTR(-ENOMEM);
bc0c38d1 2656
6d158a81
SR
2657 iter->buffer_iter = kzalloc(sizeof(*iter->buffer_iter) * num_possible_cpus(),
2658 GFP_KERNEL);
93574fcc
DC
2659 if (!iter->buffer_iter)
2660 goto release;
2661
d7350c3f
FW
2662 /*
2663 * We make a copy of the current tracer to avoid concurrent
2664 * changes on it while we are reading.
2665 */
bc0c38d1 2666 mutex_lock(&trace_types_lock);
d7350c3f 2667 iter->trace = kzalloc(sizeof(*iter->trace), GFP_KERNEL);
85a2f9b4 2668 if (!iter->trace)
d7350c3f 2669 goto fail;
85a2f9b4 2670
2b6080f2 2671 *iter->trace = *tr->current_trace;
d7350c3f 2672
79f55997 2673 if (!zalloc_cpumask_var(&iter->started, GFP_KERNEL))
b0dfa978
FW
2674 goto fail;
2675
12883efb
SRRH
2676 iter->tr = tr;
2677
2678#ifdef CONFIG_TRACER_MAX_TRACE
2b6080f2
SR
2679 /* Currently only the top directory has a snapshot */
2680 if (tr->current_trace->print_max || snapshot)
12883efb 2681 iter->trace_buffer = &tr->max_buffer;
bc0c38d1 2682 else
12883efb
SRRH
2683#endif
2684 iter->trace_buffer = &tr->trace_buffer;
debdd57f 2685 iter->snapshot = snapshot;
bc0c38d1 2686 iter->pos = -1;
d7350c3f 2687 mutex_init(&iter->mutex);
2b6080f2 2688 iter->cpu_file = tc->cpu;
bc0c38d1 2689
8bba1bf5
MM
2690 /* Notify the tracer early; before we stop tracing. */
2691 if (iter->trace && iter->trace->open)
a93751ca 2692 iter->trace->open(iter);
8bba1bf5 2693
12ef7d44 2694 /* Annotate start of buffers if we had overruns */
12883efb 2695 if (ring_buffer_overruns(iter->trace_buffer->buffer))
12ef7d44
SR
2696 iter->iter_flags |= TRACE_FILE_ANNOTATE;
2697
8be0709f
DS
2698 /* Output in nanoseconds only if we are using a clock in nanoseconds. */
2699 if (trace_clocks[trace_clock_id].in_ns)
2700 iter->iter_flags |= TRACE_FILE_TIME_IN_NS;
2701
debdd57f
HT
2702 /* stop the trace while dumping if we are not opening "snapshot" */
2703 if (!iter->snapshot)
2b6080f2 2704 tracing_stop_tr(tr);
2f26ebd5 2705
ae3b5093 2706 if (iter->cpu_file == RING_BUFFER_ALL_CPUS) {
b04cc6b1 2707 for_each_tracing_cpu(cpu) {
b04cc6b1 2708 iter->buffer_iter[cpu] =
12883efb 2709 ring_buffer_read_prepare(iter->trace_buffer->buffer, cpu);
72c9ddfd
DM
2710 }
2711 ring_buffer_read_prepare_sync();
2712 for_each_tracing_cpu(cpu) {
2713 ring_buffer_read_start(iter->buffer_iter[cpu]);
2f26ebd5 2714 tracing_iter_reset(iter, cpu);
b04cc6b1
FW
2715 }
2716 } else {
2717 cpu = iter->cpu_file;
3928a8a2 2718 iter->buffer_iter[cpu] =
12883efb 2719 ring_buffer_read_prepare(iter->trace_buffer->buffer, cpu);
72c9ddfd
DM
2720 ring_buffer_read_prepare_sync();
2721 ring_buffer_read_start(iter->buffer_iter[cpu]);
2f26ebd5 2722 tracing_iter_reset(iter, cpu);
3928a8a2
SR
2723 }
2724
a695cb58
SRRH
2725 tr->ref++;
2726
bc0c38d1
SR
2727 mutex_unlock(&trace_types_lock);
2728
bc0c38d1 2729 return iter;
3928a8a2 2730
d7350c3f 2731 fail:
3928a8a2 2732 mutex_unlock(&trace_types_lock);
d7350c3f 2733 kfree(iter->trace);
6d158a81 2734 kfree(iter->buffer_iter);
93574fcc 2735release:
50e18b94
JO
2736 seq_release_private(inode, file);
2737 return ERR_PTR(-ENOMEM);
bc0c38d1
SR
2738}
2739
2740int tracing_open_generic(struct inode *inode, struct file *filp)
2741{
60a11774
SR
2742 if (tracing_disabled)
2743 return -ENODEV;
2744
bc0c38d1
SR
2745 filp->private_data = inode->i_private;
2746 return 0;
2747}
2748
4fd27358 2749static int tracing_release(struct inode *inode, struct file *file)
bc0c38d1 2750{
907f2784 2751 struct seq_file *m = file->private_data;
4acd4d00 2752 struct trace_iterator *iter;
2b6080f2 2753 struct trace_array *tr;
3928a8a2 2754 int cpu;
bc0c38d1 2755
4acd4d00
SR
2756 if (!(file->f_mode & FMODE_READ))
2757 return 0;
2758
2759 iter = m->private;
12883efb 2760 tr = iter->tr;
2b6080f2 2761
bc0c38d1 2762 mutex_lock(&trace_types_lock);
a695cb58
SRRH
2763
2764 WARN_ON(!tr->ref);
2765 tr->ref--;
2766
3928a8a2
SR
2767 for_each_tracing_cpu(cpu) {
2768 if (iter->buffer_iter[cpu])
2769 ring_buffer_read_finish(iter->buffer_iter[cpu]);
2770 }
2771
bc0c38d1
SR
2772 if (iter->trace && iter->trace->close)
2773 iter->trace->close(iter);
2774
debdd57f
HT
2775 if (!iter->snapshot)
2776 /* reenable tracing if it was previously enabled */
2b6080f2 2777 tracing_start_tr(tr);
bc0c38d1
SR
2778 mutex_unlock(&trace_types_lock);
2779
d7350c3f 2780 mutex_destroy(&iter->mutex);
b0dfa978 2781 free_cpumask_var(iter->started);
d7350c3f 2782 kfree(iter->trace);
6d158a81 2783 kfree(iter->buffer_iter);
50e18b94 2784 seq_release_private(inode, file);
bc0c38d1
SR
2785 return 0;
2786}
2787
2788static int tracing_open(struct inode *inode, struct file *file)
2789{
85a2f9b4
SR
2790 struct trace_iterator *iter;
2791 int ret = 0;
bc0c38d1 2792
4acd4d00
SR
2793 /* If this file was open for write, then erase contents */
2794 if ((file->f_mode & FMODE_WRITE) &&
8650ae32 2795 (file->f_flags & O_TRUNC)) {
2b6080f2
SR
2796 struct trace_cpu *tc = inode->i_private;
2797 struct trace_array *tr = tc->tr;
bc0c38d1 2798
2b6080f2 2799 if (tc->cpu == RING_BUFFER_ALL_CPUS)
12883efb 2800 tracing_reset_online_cpus(&tr->trace_buffer);
4acd4d00 2801 else
12883efb 2802 tracing_reset(&tr->trace_buffer, tc->cpu);
4acd4d00 2803 }
bc0c38d1 2804
4acd4d00 2805 if (file->f_mode & FMODE_READ) {
debdd57f 2806 iter = __tracing_open(inode, file, false);
4acd4d00
SR
2807 if (IS_ERR(iter))
2808 ret = PTR_ERR(iter);
2809 else if (trace_flags & TRACE_ITER_LATENCY_FMT)
2810 iter->iter_flags |= TRACE_FILE_LAT_FMT;
2811 }
bc0c38d1
SR
2812 return ret;
2813}
2814
e309b41d 2815static void *
bc0c38d1
SR
2816t_next(struct seq_file *m, void *v, loff_t *pos)
2817{
f129e965 2818 struct tracer *t = v;
bc0c38d1
SR
2819
2820 (*pos)++;
2821
2822 if (t)
2823 t = t->next;
2824
bc0c38d1
SR
2825 return t;
2826}
2827
2828static void *t_start(struct seq_file *m, loff_t *pos)
2829{
f129e965 2830 struct tracer *t;
bc0c38d1
SR
2831 loff_t l = 0;
2832
2833 mutex_lock(&trace_types_lock);
f129e965 2834 for (t = trace_types; t && l < *pos; t = t_next(m, t, &l))
bc0c38d1
SR
2835 ;
2836
2837 return t;
2838}
2839
2840static void t_stop(struct seq_file *m, void *p)
2841{
2842 mutex_unlock(&trace_types_lock);
2843}
2844
2845static int t_show(struct seq_file *m, void *v)
2846{
2847 struct tracer *t = v;
2848
2849 if (!t)
2850 return 0;
2851
2852 seq_printf(m, "%s", t->name);
2853 if (t->next)
2854 seq_putc(m, ' ');
2855 else
2856 seq_putc(m, '\n');
2857
2858 return 0;
2859}
2860
88e9d34c 2861static const struct seq_operations show_traces_seq_ops = {
4bf39a94
IM
2862 .start = t_start,
2863 .next = t_next,
2864 .stop = t_stop,
2865 .show = t_show,
bc0c38d1
SR
2866};
2867
2868static int show_traces_open(struct inode *inode, struct file *file)
2869{
60a11774
SR
2870 if (tracing_disabled)
2871 return -ENODEV;
2872
f129e965 2873 return seq_open(file, &show_traces_seq_ops);
bc0c38d1
SR
2874}
2875
4acd4d00
SR
2876static ssize_t
2877tracing_write_stub(struct file *filp, const char __user *ubuf,
2878 size_t count, loff_t *ppos)
2879{
2880 return count;
2881}
2882
364829b1
SP
2883static loff_t tracing_seek(struct file *file, loff_t offset, int origin)
2884{
2885 if (file->f_mode & FMODE_READ)
2886 return seq_lseek(file, offset, origin);
2887 else
2888 return 0;
2889}
2890
5e2336a0 2891static const struct file_operations tracing_fops = {
4bf39a94
IM
2892 .open = tracing_open,
2893 .read = seq_read,
4acd4d00 2894 .write = tracing_write_stub,
364829b1 2895 .llseek = tracing_seek,
4bf39a94 2896 .release = tracing_release,
bc0c38d1
SR
2897};
2898
5e2336a0 2899static const struct file_operations show_traces_fops = {
c7078de1
IM
2900 .open = show_traces_open,
2901 .read = seq_read,
2902 .release = seq_release,
b444786f 2903 .llseek = seq_lseek,
c7078de1
IM
2904};
2905
36dfe925
IM
2906/*
2907 * Only trace on a CPU if the bitmask is set:
2908 */
9e01c1b7 2909static cpumask_var_t tracing_cpumask;
36dfe925
IM
2910
2911/*
2912 * The tracer itself will not take this lock, but still we want
2913 * to provide a consistent cpumask to user-space:
2914 */
2915static DEFINE_MUTEX(tracing_cpumask_update_lock);
2916
2917/*
2918 * Temporary storage for the character representation of the
2919 * CPU bitmask (and one more byte for the newline):
2920 */
2921static char mask_str[NR_CPUS + 1];
2922
c7078de1
IM
2923static ssize_t
2924tracing_cpumask_read(struct file *filp, char __user *ubuf,
2925 size_t count, loff_t *ppos)
2926{
36dfe925 2927 int len;
c7078de1
IM
2928
2929 mutex_lock(&tracing_cpumask_update_lock);
36dfe925 2930
9e01c1b7 2931 len = cpumask_scnprintf(mask_str, count, tracing_cpumask);
36dfe925
IM
2932 if (count - len < 2) {
2933 count = -EINVAL;
2934 goto out_err;
2935 }
2936 len += sprintf(mask_str + len, "\n");
2937 count = simple_read_from_buffer(ubuf, count, ppos, mask_str, NR_CPUS+1);
2938
2939out_err:
c7078de1
IM
2940 mutex_unlock(&tracing_cpumask_update_lock);
2941
2942 return count;
2943}
2944
2945static ssize_t
2946tracing_cpumask_write(struct file *filp, const char __user *ubuf,
2947 size_t count, loff_t *ppos)
2948{
2b6080f2 2949 struct trace_array *tr = filp->private_data;
9e01c1b7 2950 cpumask_var_t tracing_cpumask_new;
2b6080f2 2951 int err, cpu;
9e01c1b7
RR
2952
2953 if (!alloc_cpumask_var(&tracing_cpumask_new, GFP_KERNEL))
2954 return -ENOMEM;
c7078de1 2955
9e01c1b7 2956 err = cpumask_parse_user(ubuf, count, tracing_cpumask_new);
c7078de1 2957 if (err)
36dfe925
IM
2958 goto err_unlock;
2959
215368e8
LZ
2960 mutex_lock(&tracing_cpumask_update_lock);
2961
a5e25883 2962 local_irq_disable();
0199c4e6 2963 arch_spin_lock(&ftrace_max_lock);
ab46428c 2964 for_each_tracing_cpu(cpu) {
36dfe925
IM
2965 /*
2966 * Increase/decrease the disabled counter if we are
2967 * about to flip a bit in the cpumask:
2968 */
9e01c1b7
RR
2969 if (cpumask_test_cpu(cpu, tracing_cpumask) &&
2970 !cpumask_test_cpu(cpu, tracing_cpumask_new)) {
12883efb
SRRH
2971 atomic_inc(&per_cpu_ptr(tr->trace_buffer.data, cpu)->disabled);
2972 ring_buffer_record_disable_cpu(tr->trace_buffer.buffer, cpu);
36dfe925 2973 }
9e01c1b7
RR
2974 if (!cpumask_test_cpu(cpu, tracing_cpumask) &&
2975 cpumask_test_cpu(cpu, tracing_cpumask_new)) {
12883efb
SRRH
2976 atomic_dec(&per_cpu_ptr(tr->trace_buffer.data, cpu)->disabled);
2977 ring_buffer_record_enable_cpu(tr->trace_buffer.buffer, cpu);
36dfe925
IM
2978 }
2979 }
0199c4e6 2980 arch_spin_unlock(&ftrace_max_lock);
a5e25883 2981 local_irq_enable();
36dfe925 2982
9e01c1b7 2983 cpumask_copy(tracing_cpumask, tracing_cpumask_new);
36dfe925
IM
2984
2985 mutex_unlock(&tracing_cpumask_update_lock);
9e01c1b7 2986 free_cpumask_var(tracing_cpumask_new);
c7078de1
IM
2987
2988 return count;
36dfe925
IM
2989
2990err_unlock:
215368e8 2991 free_cpumask_var(tracing_cpumask_new);
36dfe925
IM
2992
2993 return err;
c7078de1
IM
2994}
2995
5e2336a0 2996static const struct file_operations tracing_cpumask_fops = {
c7078de1
IM
2997 .open = tracing_open_generic,
2998 .read = tracing_cpumask_read,
2999 .write = tracing_cpumask_write,
b444786f 3000 .llseek = generic_file_llseek,
bc0c38d1
SR
3001};
3002
fdb372ed 3003static int tracing_trace_options_show(struct seq_file *m, void *v)
bc0c38d1 3004{
d8e83d26 3005 struct tracer_opt *trace_opts;
2b6080f2 3006 struct trace_array *tr = m->private;
d8e83d26 3007 u32 tracer_flags;
d8e83d26 3008 int i;
adf9f195 3009
d8e83d26 3010 mutex_lock(&trace_types_lock);
2b6080f2
SR
3011 tracer_flags = tr->current_trace->flags->val;
3012 trace_opts = tr->current_trace->flags->opts;
d8e83d26 3013
bc0c38d1
SR
3014 for (i = 0; trace_options[i]; i++) {
3015 if (trace_flags & (1 << i))
fdb372ed 3016 seq_printf(m, "%s\n", trace_options[i]);
bc0c38d1 3017 else
fdb372ed 3018 seq_printf(m, "no%s\n", trace_options[i]);
bc0c38d1
SR
3019 }
3020
adf9f195
FW
3021 for (i = 0; trace_opts[i].name; i++) {
3022 if (tracer_flags & trace_opts[i].bit)
fdb372ed 3023 seq_printf(m, "%s\n", trace_opts[i].name);
adf9f195 3024 else
fdb372ed 3025 seq_printf(m, "no%s\n", trace_opts[i].name);
adf9f195 3026 }
d8e83d26 3027 mutex_unlock(&trace_types_lock);
adf9f195 3028
fdb372ed 3029 return 0;
bc0c38d1 3030}
bc0c38d1 3031
8d18eaaf
LZ
3032static int __set_tracer_option(struct tracer *trace,
3033 struct tracer_flags *tracer_flags,
3034 struct tracer_opt *opts, int neg)
3035{
3036 int ret;
bc0c38d1 3037
8d18eaaf
LZ
3038 ret = trace->set_flag(tracer_flags->val, opts->bit, !neg);
3039 if (ret)
3040 return ret;
3041
3042 if (neg)
3043 tracer_flags->val &= ~opts->bit;
3044 else
3045 tracer_flags->val |= opts->bit;
3046 return 0;
bc0c38d1
SR
3047}
3048
adf9f195
FW
3049/* Try to assign a tracer specific option */
3050static int set_tracer_option(struct tracer *trace, char *cmp, int neg)
3051{
7770841e 3052 struct tracer_flags *tracer_flags = trace->flags;
adf9f195 3053 struct tracer_opt *opts = NULL;
8d18eaaf 3054 int i;
adf9f195 3055
7770841e
Z
3056 for (i = 0; tracer_flags->opts[i].name; i++) {
3057 opts = &tracer_flags->opts[i];
adf9f195 3058
8d18eaaf
LZ
3059 if (strcmp(cmp, opts->name) == 0)
3060 return __set_tracer_option(trace, trace->flags,
3061 opts, neg);
adf9f195 3062 }
adf9f195 3063
8d18eaaf 3064 return -EINVAL;
adf9f195
FW
3065}
3066
613f04a0
SRRH
3067/* Some tracers require overwrite to stay enabled */
3068int trace_keep_overwrite(struct tracer *tracer, u32 mask, int set)
3069{
3070 if (tracer->enabled && (mask & TRACE_ITER_OVERWRITE) && !set)
3071 return -1;
3072
3073 return 0;
3074}
3075
2b6080f2 3076int set_tracer_flag(struct trace_array *tr, unsigned int mask, int enabled)
af4617bd
SR
3077{
3078 /* do nothing if flag is already set */
3079 if (!!(trace_flags & mask) == !!enabled)
613f04a0
SRRH
3080 return 0;
3081
3082 /* Give the tracer a chance to approve the change */
2b6080f2
SR
3083 if (tr->current_trace->flag_changed)
3084 if (tr->current_trace->flag_changed(tr->current_trace, mask, !!enabled))
613f04a0 3085 return -EINVAL;
af4617bd
SR
3086
3087 if (enabled)
3088 trace_flags |= mask;
3089 else
3090 trace_flags &= ~mask;
e870e9a1
LZ
3091
3092 if (mask == TRACE_ITER_RECORD_CMD)
3093 trace_event_enable_cmd_record(enabled);
750912fa 3094
80902822 3095 if (mask == TRACE_ITER_OVERWRITE) {
12883efb 3096 ring_buffer_change_overwrite(tr->trace_buffer.buffer, enabled);
80902822 3097#ifdef CONFIG_TRACER_MAX_TRACE
12883efb 3098 ring_buffer_change_overwrite(tr->max_buffer.buffer, enabled);
80902822
SRRH
3099#endif
3100 }
81698831
SR
3101
3102 if (mask == TRACE_ITER_PRINTK)
3103 trace_printk_start_stop_comm(enabled);
613f04a0
SRRH
3104
3105 return 0;
af4617bd
SR
3106}
3107
2b6080f2 3108static int trace_set_options(struct trace_array *tr, char *option)
bc0c38d1 3109{
8d18eaaf 3110 char *cmp;
bc0c38d1 3111 int neg = 0;
613f04a0 3112 int ret = -ENODEV;
bc0c38d1
SR
3113 int i;
3114
7bcfaf54 3115 cmp = strstrip(option);
bc0c38d1 3116
8d18eaaf 3117 if (strncmp(cmp, "no", 2) == 0) {
bc0c38d1
SR
3118 neg = 1;
3119 cmp += 2;
3120 }
3121
69d34da2
SRRH
3122 mutex_lock(&trace_types_lock);
3123
bc0c38d1 3124 for (i = 0; trace_options[i]; i++) {
8d18eaaf 3125 if (strcmp(cmp, trace_options[i]) == 0) {
2b6080f2 3126 ret = set_tracer_flag(tr, 1 << i, !neg);
bc0c38d1
SR
3127 break;
3128 }
3129 }
adf9f195
FW
3130
3131 /* If no option could be set, test the specific tracer options */
69d34da2 3132 if (!trace_options[i])
2b6080f2 3133 ret = set_tracer_option(tr->current_trace, cmp, neg);
69d34da2
SRRH
3134
3135 mutex_unlock(&trace_types_lock);
bc0c38d1 3136
7bcfaf54
SR
3137 return ret;
3138}
3139
3140static ssize_t
3141tracing_trace_options_write(struct file *filp, const char __user *ubuf,
3142 size_t cnt, loff_t *ppos)
3143{
2b6080f2
SR
3144 struct seq_file *m = filp->private_data;
3145 struct trace_array *tr = m->private;
7bcfaf54 3146 char buf[64];
613f04a0 3147 int ret;
7bcfaf54
SR
3148
3149 if (cnt >= sizeof(buf))
3150 return -EINVAL;
3151
3152 if (copy_from_user(&buf, ubuf, cnt))
3153 return -EFAULT;
3154
a8dd2176
SR
3155 buf[cnt] = 0;
3156
2b6080f2 3157 ret = trace_set_options(tr, buf);
613f04a0
SRRH
3158 if (ret < 0)
3159 return ret;
7bcfaf54 3160
cf8517cf 3161 *ppos += cnt;
bc0c38d1
SR
3162
3163 return cnt;
3164}
3165
fdb372ed
LZ
3166static int tracing_trace_options_open(struct inode *inode, struct file *file)
3167{
3168 if (tracing_disabled)
3169 return -ENODEV;
2b6080f2
SR
3170
3171 return single_open(file, tracing_trace_options_show, inode->i_private);
fdb372ed
LZ
3172}
3173
5e2336a0 3174static const struct file_operations tracing_iter_fops = {
fdb372ed
LZ
3175 .open = tracing_trace_options_open,
3176 .read = seq_read,
3177 .llseek = seq_lseek,
3178 .release = single_release,
ee6bce52 3179 .write = tracing_trace_options_write,
bc0c38d1
SR
3180};
3181
7bd2f24c
IM
3182static const char readme_msg[] =
3183 "tracing mini-HOWTO:\n\n"
156f5a78
GL
3184 "# mount -t debugfs nodev /sys/kernel/debug\n\n"
3185 "# cat /sys/kernel/debug/tracing/available_tracers\n"
1e42e83f 3186 "wakeup wakeup_rt preemptirqsoff preemptoff irqsoff function nop\n\n"
156f5a78 3187 "# cat /sys/kernel/debug/tracing/current_tracer\n"
bc2b6871 3188 "nop\n"
1e42e83f 3189 "# echo wakeup > /sys/kernel/debug/tracing/current_tracer\n"
156f5a78 3190 "# cat /sys/kernel/debug/tracing/current_tracer\n"
1e42e83f 3191 "wakeup\n"
156f5a78 3192 "# cat /sys/kernel/debug/tracing/trace_options\n"
7bd2f24c 3193 "noprint-parent nosym-offset nosym-addr noverbose\n"
156f5a78 3194 "# echo print-parent > /sys/kernel/debug/tracing/trace_options\n"
9b5f8b31 3195 "# echo 1 > /sys/kernel/debug/tracing/tracing_on\n"
156f5a78 3196 "# cat /sys/kernel/debug/tracing/trace > /tmp/trace.txt\n"
9b5f8b31 3197 "# echo 0 > /sys/kernel/debug/tracing/tracing_on\n"
7bd2f24c
IM
3198;
3199
3200static ssize_t
3201tracing_readme_read(struct file *filp, char __user *ubuf,
3202 size_t cnt, loff_t *ppos)
3203{
3204 return simple_read_from_buffer(ubuf, cnt, ppos,
3205 readme_msg, strlen(readme_msg));
3206}
3207
5e2336a0 3208static const struct file_operations tracing_readme_fops = {
c7078de1
IM
3209 .open = tracing_open_generic,
3210 .read = tracing_readme_read,
b444786f 3211 .llseek = generic_file_llseek,
7bd2f24c
IM
3212};
3213
69abe6a5
AP
3214static ssize_t
3215tracing_saved_cmdlines_read(struct file *file, char __user *ubuf,
3216 size_t cnt, loff_t *ppos)
3217{
3218 char *buf_comm;
3219 char *file_buf;
3220 char *buf;
3221 int len = 0;
3222 int pid;
3223 int i;
3224
3225 file_buf = kmalloc(SAVED_CMDLINES*(16+TASK_COMM_LEN), GFP_KERNEL);
3226 if (!file_buf)
3227 return -ENOMEM;
3228
3229 buf_comm = kmalloc(TASK_COMM_LEN, GFP_KERNEL);
3230 if (!buf_comm) {
3231 kfree(file_buf);
3232 return -ENOMEM;
3233 }
3234
3235 buf = file_buf;
3236
3237 for (i = 0; i < SAVED_CMDLINES; i++) {
3238 int r;
3239
3240 pid = map_cmdline_to_pid[i];
3241 if (pid == -1 || pid == NO_CMDLINE_MAP)
3242 continue;
3243
3244 trace_find_cmdline(pid, buf_comm);
3245 r = sprintf(buf, "%d %s\n", pid, buf_comm);
3246 buf += r;
3247 len += r;
3248 }
3249
3250 len = simple_read_from_buffer(ubuf, cnt, ppos,
3251 file_buf, len);
3252
3253 kfree(file_buf);
3254 kfree(buf_comm);
3255
3256 return len;
3257}
3258
3259static const struct file_operations tracing_saved_cmdlines_fops = {
3260 .open = tracing_open_generic,
3261 .read = tracing_saved_cmdlines_read,
b444786f 3262 .llseek = generic_file_llseek,
69abe6a5
AP
3263};
3264
bc0c38d1
SR
3265static ssize_t
3266tracing_set_trace_read(struct file *filp, char __user *ubuf,
3267 size_t cnt, loff_t *ppos)
3268{
2b6080f2 3269 struct trace_array *tr = filp->private_data;
ee6c2c1b 3270 char buf[MAX_TRACER_SIZE+2];
bc0c38d1
SR
3271 int r;
3272
3273 mutex_lock(&trace_types_lock);
2b6080f2 3274 r = sprintf(buf, "%s\n", tr->current_trace->name);
bc0c38d1
SR
3275 mutex_unlock(&trace_types_lock);
3276
4bf39a94 3277 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
bc0c38d1
SR
3278}
3279
b6f11df2
ACM
3280int tracer_init(struct tracer *t, struct trace_array *tr)
3281{
12883efb 3282 tracing_reset_online_cpus(&tr->trace_buffer);
b6f11df2
ACM
3283 return t->init(tr);
3284}
3285
12883efb 3286static void set_buffer_entries(struct trace_buffer *buf, unsigned long val)
438ced17
VN
3287{
3288 int cpu;
737223fb 3289
438ced17 3290 for_each_tracing_cpu(cpu)
12883efb 3291 per_cpu_ptr(buf->data, cpu)->entries = val;
438ced17
VN
3292}
3293
12883efb 3294#ifdef CONFIG_TRACER_MAX_TRACE
d60da506 3295/* resize @tr's buffer to the size of @size_tr's entries */
12883efb
SRRH
3296static int resize_buffer_duplicate_size(struct trace_buffer *trace_buf,
3297 struct trace_buffer *size_buf, int cpu_id)
d60da506
HT
3298{
3299 int cpu, ret = 0;
3300
3301 if (cpu_id == RING_BUFFER_ALL_CPUS) {
3302 for_each_tracing_cpu(cpu) {
12883efb
SRRH
3303 ret = ring_buffer_resize(trace_buf->buffer,
3304 per_cpu_ptr(size_buf->data, cpu)->entries, cpu);
d60da506
HT
3305 if (ret < 0)
3306 break;
12883efb
SRRH
3307 per_cpu_ptr(trace_buf->data, cpu)->entries =
3308 per_cpu_ptr(size_buf->data, cpu)->entries;
d60da506
HT
3309 }
3310 } else {
12883efb
SRRH
3311 ret = ring_buffer_resize(trace_buf->buffer,
3312 per_cpu_ptr(size_buf->data, cpu_id)->entries, cpu_id);
d60da506 3313 if (ret == 0)
12883efb
SRRH
3314 per_cpu_ptr(trace_buf->data, cpu_id)->entries =
3315 per_cpu_ptr(size_buf->data, cpu_id)->entries;
d60da506
HT
3316 }
3317
3318 return ret;
3319}
12883efb 3320#endif /* CONFIG_TRACER_MAX_TRACE */
d60da506 3321
2b6080f2
SR
3322static int __tracing_resize_ring_buffer(struct trace_array *tr,
3323 unsigned long size, int cpu)
73c5162a
SR
3324{
3325 int ret;
3326
3327 /*
3328 * If kernel or user changes the size of the ring buffer
a123c52b
SR
3329 * we use the size that was given, and we can forget about
3330 * expanding it later.
73c5162a 3331 */
55034cd6 3332 ring_buffer_expanded = true;
73c5162a 3333
b382ede6 3334 /* May be called before buffers are initialized */
12883efb 3335 if (!tr->trace_buffer.buffer)
b382ede6
SR
3336 return 0;
3337
12883efb 3338 ret = ring_buffer_resize(tr->trace_buffer.buffer, size, cpu);
73c5162a
SR
3339 if (ret < 0)
3340 return ret;
3341
12883efb 3342#ifdef CONFIG_TRACER_MAX_TRACE
2b6080f2
SR
3343 if (!(tr->flags & TRACE_ARRAY_FL_GLOBAL) ||
3344 !tr->current_trace->use_max_tr)
ef710e10
KM
3345 goto out;
3346
12883efb 3347 ret = ring_buffer_resize(tr->max_buffer.buffer, size, cpu);
73c5162a 3348 if (ret < 0) {
12883efb
SRRH
3349 int r = resize_buffer_duplicate_size(&tr->trace_buffer,
3350 &tr->trace_buffer, cpu);
73c5162a 3351 if (r < 0) {
a123c52b
SR
3352 /*
3353 * AARGH! We are left with different
3354 * size max buffer!!!!
3355 * The max buffer is our "snapshot" buffer.
3356 * When a tracer needs a snapshot (one of the
3357 * latency tracers), it swaps the max buffer
3358 * with the saved snap shot. We succeeded to
3359 * update the size of the main buffer, but failed to
3360 * update the size of the max buffer. But when we tried
3361 * to reset the main buffer to the original size, we
3362 * failed there too. This is very unlikely to
3363 * happen, but if it does, warn and kill all
3364 * tracing.
3365 */
73c5162a
SR
3366 WARN_ON(1);
3367 tracing_disabled = 1;
3368 }
3369 return ret;
3370 }
3371
438ced17 3372 if (cpu == RING_BUFFER_ALL_CPUS)
12883efb 3373 set_buffer_entries(&tr->max_buffer, size);
438ced17 3374 else
12883efb 3375 per_cpu_ptr(tr->max_buffer.data, cpu)->entries = size;
438ced17 3376
ef710e10 3377 out:
12883efb
SRRH
3378#endif /* CONFIG_TRACER_MAX_TRACE */
3379
438ced17 3380 if (cpu == RING_BUFFER_ALL_CPUS)
12883efb 3381 set_buffer_entries(&tr->trace_buffer, size);
438ced17 3382 else
12883efb 3383 per_cpu_ptr(tr->trace_buffer.data, cpu)->entries = size;
73c5162a
SR
3384
3385 return ret;
3386}
3387
2b6080f2
SR
3388static ssize_t tracing_resize_ring_buffer(struct trace_array *tr,
3389 unsigned long size, int cpu_id)
4f271a2a 3390{
83f40318 3391 int ret = size;
4f271a2a
VN
3392
3393 mutex_lock(&trace_types_lock);
3394
438ced17
VN
3395 if (cpu_id != RING_BUFFER_ALL_CPUS) {
3396 /* make sure, this cpu is enabled in the mask */
3397 if (!cpumask_test_cpu(cpu_id, tracing_buffer_mask)) {
3398 ret = -EINVAL;
3399 goto out;
3400 }
3401 }
4f271a2a 3402
2b6080f2 3403 ret = __tracing_resize_ring_buffer(tr, size, cpu_id);
4f271a2a
VN
3404 if (ret < 0)
3405 ret = -ENOMEM;
3406
438ced17 3407out:
4f271a2a
VN
3408 mutex_unlock(&trace_types_lock);
3409
3410 return ret;
3411}
3412
ef710e10 3413
1852fcce
SR
3414/**
3415 * tracing_update_buffers - used by tracing facility to expand ring buffers
3416 *
3417 * To save on memory when the tracing is never used on a system with it
3418 * configured in. The ring buffers are set to a minimum size. But once
3419 * a user starts to use the tracing facility, then they need to grow
3420 * to their default size.
3421 *
3422 * This function is to be called when a tracer is about to be used.
3423 */
3424int tracing_update_buffers(void)
3425{
3426 int ret = 0;
3427
1027fcb2 3428 mutex_lock(&trace_types_lock);
1852fcce 3429 if (!ring_buffer_expanded)
2b6080f2 3430 ret = __tracing_resize_ring_buffer(&global_trace, trace_buf_size,
438ced17 3431 RING_BUFFER_ALL_CPUS);
1027fcb2 3432 mutex_unlock(&trace_types_lock);
1852fcce
SR
3433
3434 return ret;
3435}
3436
577b785f
SR
3437struct trace_option_dentry;
3438
3439static struct trace_option_dentry *
2b6080f2 3440create_trace_option_files(struct trace_array *tr, struct tracer *tracer);
577b785f
SR
3441
3442static void
3443destroy_trace_option_files(struct trace_option_dentry *topts);
3444
b2821ae6 3445static int tracing_set_tracer(const char *buf)
bc0c38d1 3446{
577b785f 3447 static struct trace_option_dentry *topts;
bc0c38d1
SR
3448 struct trace_array *tr = &global_trace;
3449 struct tracer *t;
12883efb 3450#ifdef CONFIG_TRACER_MAX_TRACE
34600f0e 3451 bool had_max_tr;
12883efb 3452#endif
d9e54076 3453 int ret = 0;
bc0c38d1 3454
1027fcb2
SR
3455 mutex_lock(&trace_types_lock);
3456
73c5162a 3457 if (!ring_buffer_expanded) {
2b6080f2 3458 ret = __tracing_resize_ring_buffer(tr, trace_buf_size,
438ced17 3459 RING_BUFFER_ALL_CPUS);
73c5162a 3460 if (ret < 0)
59f586db 3461 goto out;
73c5162a
SR
3462 ret = 0;
3463 }
3464
bc0c38d1
SR
3465 for (t = trace_types; t; t = t->next) {
3466 if (strcmp(t->name, buf) == 0)
3467 break;
3468 }
c2931e05
FW
3469 if (!t) {
3470 ret = -EINVAL;
3471 goto out;
3472 }
2b6080f2 3473 if (t == tr->current_trace)
bc0c38d1
SR
3474 goto out;
3475
9f029e83 3476 trace_branch_disable();
613f04a0 3477
2b6080f2 3478 tr->current_trace->enabled = false;
613f04a0 3479
2b6080f2
SR
3480 if (tr->current_trace->reset)
3481 tr->current_trace->reset(tr);
34600f0e 3482
12883efb 3483 /* Current trace needs to be nop_trace before synchronize_sched */
2b6080f2 3484 tr->current_trace = &nop_trace;
34600f0e 3485
45ad21ca
SRRH
3486#ifdef CONFIG_TRACER_MAX_TRACE
3487 had_max_tr = tr->allocated_snapshot;
3488
34600f0e
SR
3489 if (had_max_tr && !t->use_max_tr) {
3490 /*
3491 * We need to make sure that the update_max_tr sees that
3492 * current_trace changed to nop_trace to keep it from
3493 * swapping the buffers after we resize it.
3494 * The update_max_tr is called from interrupts disabled
3495 * so a synchronized_sched() is sufficient.
3496 */
3497 synchronize_sched();
ef710e10
KM
3498 /*
3499 * We don't free the ring buffer. instead, resize it because
3500 * The max_tr ring buffer has some state (e.g. ring->clock) and
3501 * we want preserve it.
3502 */
12883efb
SRRH
3503 ring_buffer_resize(tr->max_buffer.buffer, 1, RING_BUFFER_ALL_CPUS);
3504 set_buffer_entries(&tr->max_buffer, 1);
3505 tracing_reset_online_cpus(&tr->max_buffer);
45ad21ca 3506 tr->allocated_snapshot = false;
ef710e10 3507 }
12883efb 3508#endif
577b785f
SR
3509 destroy_trace_option_files(topts);
3510
2b6080f2 3511 topts = create_trace_option_files(tr, t);
12883efb
SRRH
3512
3513#ifdef CONFIG_TRACER_MAX_TRACE
34600f0e 3514 if (t->use_max_tr && !had_max_tr) {
438ced17 3515 /* we need to make per cpu buffer sizes equivalent */
12883efb 3516 ret = resize_buffer_duplicate_size(&tr->max_buffer, &tr->trace_buffer,
d60da506
HT
3517 RING_BUFFER_ALL_CPUS);
3518 if (ret < 0)
3519 goto out;
45ad21ca 3520 tr->allocated_snapshot = true;
ef710e10 3521 }
12883efb 3522#endif
577b785f 3523
1c80025a 3524 if (t->init) {
b6f11df2 3525 ret = tracer_init(t, tr);
1c80025a
FW
3526 if (ret)
3527 goto out;
3528 }
bc0c38d1 3529
2b6080f2
SR
3530 tr->current_trace = t;
3531 tr->current_trace->enabled = true;
9f029e83 3532 trace_branch_enable(tr);
bc0c38d1
SR
3533 out:
3534 mutex_unlock(&trace_types_lock);
3535
d9e54076
PZ
3536 return ret;
3537}
3538
3539static ssize_t
3540tracing_set_trace_write(struct file *filp, const char __user *ubuf,
3541 size_t cnt, loff_t *ppos)
3542{
ee6c2c1b 3543 char buf[MAX_TRACER_SIZE+1];
d9e54076
PZ
3544 int i;
3545 size_t ret;
e6e7a65a
FW
3546 int err;
3547
3548 ret = cnt;
d9e54076 3549
ee6c2c1b
LZ
3550 if (cnt > MAX_TRACER_SIZE)
3551 cnt = MAX_TRACER_SIZE;
d9e54076
PZ
3552
3553 if (copy_from_user(&buf, ubuf, cnt))
3554 return -EFAULT;
3555
3556 buf[cnt] = 0;
3557
3558 /* strip ending whitespace. */
3559 for (i = cnt - 1; i > 0 && isspace(buf[i]); i--)
3560 buf[i] = 0;
3561
e6e7a65a
FW
3562 err = tracing_set_tracer(buf);
3563 if (err)
3564 return err;
d9e54076 3565
cf8517cf 3566 *ppos += ret;
bc0c38d1 3567
c2931e05 3568 return ret;
bc0c38d1
SR
3569}
3570
3571static ssize_t
3572tracing_max_lat_read(struct file *filp, char __user *ubuf,
3573 size_t cnt, loff_t *ppos)
3574{
3575 unsigned long *ptr = filp->private_data;
3576 char buf[64];
3577 int r;
3578
cffae437 3579 r = snprintf(buf, sizeof(buf), "%ld\n",
bc0c38d1 3580 *ptr == (unsigned long)-1 ? -1 : nsecs_to_usecs(*ptr));
cffae437
SR
3581 if (r > sizeof(buf))
3582 r = sizeof(buf);
4bf39a94 3583 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
bc0c38d1
SR
3584}
3585
3586static ssize_t
3587tracing_max_lat_write(struct file *filp, const char __user *ubuf,
3588 size_t cnt, loff_t *ppos)
3589{
5e39841c 3590 unsigned long *ptr = filp->private_data;
5e39841c 3591 unsigned long val;
c6caeeb1 3592 int ret;
bc0c38d1 3593
22fe9b54
PH
3594 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
3595 if (ret)
c6caeeb1 3596 return ret;
bc0c38d1
SR
3597
3598 *ptr = val * 1000;
3599
3600 return cnt;
3601}
3602
b3806b43
SR
3603static int tracing_open_pipe(struct inode *inode, struct file *filp)
3604{
2b6080f2
SR
3605 struct trace_cpu *tc = inode->i_private;
3606 struct trace_array *tr = tc->tr;
b3806b43 3607 struct trace_iterator *iter;
b04cc6b1 3608 int ret = 0;
b3806b43
SR
3609
3610 if (tracing_disabled)
3611 return -ENODEV;
3612
b04cc6b1
FW
3613 mutex_lock(&trace_types_lock);
3614
b3806b43
SR
3615 /* create a buffer to store the information to pass to userspace */
3616 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
b04cc6b1
FW
3617 if (!iter) {
3618 ret = -ENOMEM;
3619 goto out;
3620 }
b3806b43 3621
d7350c3f
FW
3622 /*
3623 * We make a copy of the current tracer to avoid concurrent
3624 * changes on it while we are reading.
3625 */
3626 iter->trace = kmalloc(sizeof(*iter->trace), GFP_KERNEL);
3627 if (!iter->trace) {
3628 ret = -ENOMEM;
3629 goto fail;
3630 }
2b6080f2 3631 *iter->trace = *tr->current_trace;
d7350c3f 3632
4462344e 3633 if (!alloc_cpumask_var(&iter->started, GFP_KERNEL)) {
b04cc6b1 3634 ret = -ENOMEM;
d7350c3f 3635 goto fail;
4462344e
RR
3636 }
3637
a309720c 3638 /* trace pipe does not show start of buffer */
4462344e 3639 cpumask_setall(iter->started);
a309720c 3640
112f38a7
SR
3641 if (trace_flags & TRACE_ITER_LATENCY_FMT)
3642 iter->iter_flags |= TRACE_FILE_LAT_FMT;
3643
8be0709f
DS
3644 /* Output in nanoseconds only if we are using a clock in nanoseconds. */
3645 if (trace_clocks[trace_clock_id].in_ns)
3646 iter->iter_flags |= TRACE_FILE_TIME_IN_NS;
3647
2b6080f2
SR
3648 iter->cpu_file = tc->cpu;
3649 iter->tr = tc->tr;
12883efb 3650 iter->trace_buffer = &tc->tr->trace_buffer;
d7350c3f 3651 mutex_init(&iter->mutex);
b3806b43
SR
3652 filp->private_data = iter;
3653
107bad8b
SR
3654 if (iter->trace->pipe_open)
3655 iter->trace->pipe_open(iter);
107bad8b 3656
b444786f 3657 nonseekable_open(inode, filp);
b04cc6b1
FW
3658out:
3659 mutex_unlock(&trace_types_lock);
3660 return ret;
d7350c3f
FW
3661
3662fail:
3663 kfree(iter->trace);
3664 kfree(iter);
3665 mutex_unlock(&trace_types_lock);
3666 return ret;
b3806b43
SR
3667}
3668
3669static int tracing_release_pipe(struct inode *inode, struct file *file)
3670{
3671 struct trace_iterator *iter = file->private_data;
3672
b04cc6b1
FW
3673 mutex_lock(&trace_types_lock);
3674
29bf4a5e 3675 if (iter->trace->pipe_close)
c521efd1
SR
3676 iter->trace->pipe_close(iter);
3677
b04cc6b1
FW
3678 mutex_unlock(&trace_types_lock);
3679
4462344e 3680 free_cpumask_var(iter->started);
d7350c3f
FW
3681 mutex_destroy(&iter->mutex);
3682 kfree(iter->trace);
b3806b43 3683 kfree(iter);
b3806b43
SR
3684
3685 return 0;
3686}
3687
2a2cc8f7 3688static unsigned int
cc60cdc9 3689trace_poll(struct trace_iterator *iter, struct file *filp, poll_table *poll_table)
2a2cc8f7 3690{
15693458
SRRH
3691 /* Iterators are static, they should be filled or empty */
3692 if (trace_buffer_iter(iter, iter->cpu_file))
3693 return POLLIN | POLLRDNORM;
3694
3695 if (trace_flags & TRACE_ITER_BLOCK)
2a2cc8f7
SSP
3696 /*
3697 * Always select as readable when in blocking mode
3698 */
3699 return POLLIN | POLLRDNORM;
15693458 3700 else
12883efb 3701 return ring_buffer_poll_wait(iter->trace_buffer->buffer, iter->cpu_file,
15693458 3702 filp, poll_table);
2a2cc8f7
SSP
3703}
3704
cc60cdc9
SR
3705static unsigned int
3706tracing_poll_pipe(struct file *filp, poll_table *poll_table)
3707{
3708 struct trace_iterator *iter = filp->private_data;
3709
3710 return trace_poll(iter, filp, poll_table);
3711}
3712
6eaaa5d5
FW
3713/*
3714 * This is a make-shift waitqueue.
3715 * A tracer might use this callback on some rare cases:
3716 *
3717 * 1) the current tracer might hold the runqueue lock when it wakes up
3718 * a reader, hence a deadlock (sched, function, and function graph tracers)
3719 * 2) the function tracers, trace all functions, we don't want
3720 * the overhead of calling wake_up and friends
3721 * (and tracing them too)
3722 *
3723 * Anyway, this is really very primitive wakeup.
3724 */
3725void poll_wait_pipe(struct trace_iterator *iter)
3726{
3727 set_current_state(TASK_INTERRUPTIBLE);
3728 /* sleep for 100 msecs, and try again. */
3729 schedule_timeout(HZ / 10);
3730}
3731
ff98781b
EGM
3732/* Must be called with trace_types_lock mutex held. */
3733static int tracing_wait_pipe(struct file *filp)
b3806b43
SR
3734{
3735 struct trace_iterator *iter = filp->private_data;
b3806b43 3736
b3806b43 3737 while (trace_empty(iter)) {
2dc8f095 3738
107bad8b 3739 if ((filp->f_flags & O_NONBLOCK)) {
ff98781b 3740 return -EAGAIN;
107bad8b 3741 }
2dc8f095 3742
d7350c3f 3743 mutex_unlock(&iter->mutex);
107bad8b 3744
6eaaa5d5 3745 iter->trace->wait_pipe(iter);
b3806b43 3746
d7350c3f 3747 mutex_lock(&iter->mutex);
107bad8b 3748
6eaaa5d5 3749 if (signal_pending(current))
ff98781b 3750 return -EINTR;
b3806b43
SR
3751
3752 /*
250bfd3d 3753 * We block until we read something and tracing is disabled.
b3806b43
SR
3754 * We still block if tracing is disabled, but we have never
3755 * read anything. This allows a user to cat this file, and
3756 * then enable tracing. But after we have read something,
3757 * we give an EOF when tracing is again disabled.
3758 *
3759 * iter->pos will be 0 if we haven't read anything.
3760 */
250bfd3d 3761 if (!tracing_is_enabled() && iter->pos)
b3806b43 3762 break;
b3806b43
SR
3763 }
3764
ff98781b
EGM
3765 return 1;
3766}
3767
3768/*
3769 * Consumer reader.
3770 */
3771static ssize_t
3772tracing_read_pipe(struct file *filp, char __user *ubuf,
3773 size_t cnt, loff_t *ppos)
3774{
3775 struct trace_iterator *iter = filp->private_data;
2b6080f2 3776 struct trace_array *tr = iter->tr;
ff98781b
EGM
3777 ssize_t sret;
3778
3779 /* return any leftover data */
3780 sret = trace_seq_to_user(&iter->seq, ubuf, cnt);
3781 if (sret != -EBUSY)
3782 return sret;
3783
f9520750 3784 trace_seq_init(&iter->seq);
ff98781b 3785
d7350c3f 3786 /* copy the tracer to avoid using a global lock all around */
ff98781b 3787 mutex_lock(&trace_types_lock);
2b6080f2
SR
3788 if (unlikely(iter->trace->name != tr->current_trace->name))
3789 *iter->trace = *tr->current_trace;
d7350c3f
FW
3790 mutex_unlock(&trace_types_lock);
3791
3792 /*
3793 * Avoid more than one consumer on a single file descriptor
3794 * This is just a matter of traces coherency, the ring buffer itself
3795 * is protected.
3796 */
3797 mutex_lock(&iter->mutex);
ff98781b
EGM
3798 if (iter->trace->read) {
3799 sret = iter->trace->read(iter, filp, ubuf, cnt, ppos);
3800 if (sret)
3801 goto out;
3802 }
3803
3804waitagain:
3805 sret = tracing_wait_pipe(filp);
3806 if (sret <= 0)
3807 goto out;
3808
b3806b43 3809 /* stop when tracing is finished */
ff98781b
EGM
3810 if (trace_empty(iter)) {
3811 sret = 0;
107bad8b 3812 goto out;
ff98781b 3813 }
b3806b43
SR
3814
3815 if (cnt >= PAGE_SIZE)
3816 cnt = PAGE_SIZE - 1;
3817
53d0aa77 3818 /* reset all but tr, trace, and overruns */
53d0aa77
SR
3819 memset(&iter->seq, 0,
3820 sizeof(struct trace_iterator) -
3821 offsetof(struct trace_iterator, seq));
4823ed7e 3822 iter->pos = -1;
b3806b43 3823
4f535968 3824 trace_event_read_lock();
7e53bd42 3825 trace_access_lock(iter->cpu_file);
955b61e5 3826 while (trace_find_next_entry_inc(iter) != NULL) {
2c4f035f 3827 enum print_line_t ret;
088b1e42
SR
3828 int len = iter->seq.len;
3829
f9896bf3 3830 ret = print_trace_line(iter);
2c4f035f 3831 if (ret == TRACE_TYPE_PARTIAL_LINE) {
088b1e42
SR
3832 /* don't print partial lines */
3833 iter->seq.len = len;
b3806b43 3834 break;
088b1e42 3835 }
b91facc3
FW
3836 if (ret != TRACE_TYPE_NO_CONSUME)
3837 trace_consume(iter);
b3806b43
SR
3838
3839 if (iter->seq.len >= cnt)
3840 break;
ee5e51f5
JO
3841
3842 /*
3843 * Setting the full flag means we reached the trace_seq buffer
3844 * size and we should leave by partial output condition above.
3845 * One of the trace_seq_* functions is not used properly.
3846 */
3847 WARN_ONCE(iter->seq.full, "full flag set for trace type %d",
3848 iter->ent->type);
b3806b43 3849 }
7e53bd42 3850 trace_access_unlock(iter->cpu_file);
4f535968 3851 trace_event_read_unlock();
b3806b43 3852
b3806b43 3853 /* Now copy what we have to the user */
6c6c2796
PP
3854 sret = trace_seq_to_user(&iter->seq, ubuf, cnt);
3855 if (iter->seq.readpos >= iter->seq.len)
f9520750 3856 trace_seq_init(&iter->seq);
9ff4b974
PP
3857
3858 /*
25985edc 3859 * If there was nothing to send to user, in spite of consuming trace
9ff4b974
PP
3860 * entries, go back to wait for more entries.
3861 */
6c6c2796 3862 if (sret == -EBUSY)
9ff4b974 3863 goto waitagain;
b3806b43 3864
107bad8b 3865out:
d7350c3f 3866 mutex_unlock(&iter->mutex);
107bad8b 3867
6c6c2796 3868 return sret;
b3806b43
SR
3869}
3870
3c56819b
EGM
3871static void tracing_pipe_buf_release(struct pipe_inode_info *pipe,
3872 struct pipe_buffer *buf)
3873{
3874 __free_page(buf->page);
3875}
3876
3877static void tracing_spd_release_pipe(struct splice_pipe_desc *spd,
3878 unsigned int idx)
3879{
3880 __free_page(spd->pages[idx]);
3881}
3882
28dfef8f 3883static const struct pipe_buf_operations tracing_pipe_buf_ops = {
34cd4998
SR
3884 .can_merge = 0,
3885 .map = generic_pipe_buf_map,
3886 .unmap = generic_pipe_buf_unmap,
3887 .confirm = generic_pipe_buf_confirm,
3888 .release = tracing_pipe_buf_release,
3889 .steal = generic_pipe_buf_steal,
3890 .get = generic_pipe_buf_get,
3c56819b
EGM
3891};
3892
34cd4998 3893static size_t
fa7c7f6e 3894tracing_fill_pipe_page(size_t rem, struct trace_iterator *iter)
34cd4998
SR
3895{
3896 size_t count;
3897 int ret;
3898
3899 /* Seq buffer is page-sized, exactly what we need. */
3900 for (;;) {
3901 count = iter->seq.len;
3902 ret = print_trace_line(iter);
3903 count = iter->seq.len - count;
3904 if (rem < count) {
3905 rem = 0;
3906 iter->seq.len -= count;
3907 break;
3908 }
3909 if (ret == TRACE_TYPE_PARTIAL_LINE) {
3910 iter->seq.len -= count;
3911 break;
3912 }
3913
74e7ff8c
LJ
3914 if (ret != TRACE_TYPE_NO_CONSUME)
3915 trace_consume(iter);
34cd4998 3916 rem -= count;
955b61e5 3917 if (!trace_find_next_entry_inc(iter)) {
34cd4998
SR
3918 rem = 0;
3919 iter->ent = NULL;
3920 break;
3921 }
3922 }
3923
3924 return rem;
3925}
3926
3c56819b
EGM
3927static ssize_t tracing_splice_read_pipe(struct file *filp,
3928 loff_t *ppos,
3929 struct pipe_inode_info *pipe,
3930 size_t len,
3931 unsigned int flags)
3932{
35f3d14d
JA
3933 struct page *pages_def[PIPE_DEF_BUFFERS];
3934 struct partial_page partial_def[PIPE_DEF_BUFFERS];
3c56819b
EGM
3935 struct trace_iterator *iter = filp->private_data;
3936 struct splice_pipe_desc spd = {
35f3d14d
JA
3937 .pages = pages_def,
3938 .partial = partial_def,
34cd4998 3939 .nr_pages = 0, /* This gets updated below. */
047fe360 3940 .nr_pages_max = PIPE_DEF_BUFFERS,
34cd4998
SR
3941 .flags = flags,
3942 .ops = &tracing_pipe_buf_ops,
3943 .spd_release = tracing_spd_release_pipe,
3c56819b 3944 };
2b6080f2 3945 struct trace_array *tr = iter->tr;
3c56819b 3946 ssize_t ret;
34cd4998 3947 size_t rem;
3c56819b
EGM
3948 unsigned int i;
3949
35f3d14d
JA
3950 if (splice_grow_spd(pipe, &spd))
3951 return -ENOMEM;
3952
d7350c3f 3953 /* copy the tracer to avoid using a global lock all around */
3c56819b 3954 mutex_lock(&trace_types_lock);
2b6080f2
SR
3955 if (unlikely(iter->trace->name != tr->current_trace->name))
3956 *iter->trace = *tr->current_trace;
d7350c3f
FW
3957 mutex_unlock(&trace_types_lock);
3958
3959 mutex_lock(&iter->mutex);
3c56819b
EGM
3960
3961 if (iter->trace->splice_read) {
3962 ret = iter->trace->splice_read(iter, filp,
3963 ppos, pipe, len, flags);
3964 if (ret)
34cd4998 3965 goto out_err;
3c56819b
EGM
3966 }
3967
3968 ret = tracing_wait_pipe(filp);
3969 if (ret <= 0)
34cd4998 3970 goto out_err;
3c56819b 3971
955b61e5 3972 if (!iter->ent && !trace_find_next_entry_inc(iter)) {
3c56819b 3973 ret = -EFAULT;
34cd4998 3974 goto out_err;
3c56819b
EGM
3975 }
3976
4f535968 3977 trace_event_read_lock();
7e53bd42 3978 trace_access_lock(iter->cpu_file);
4f535968 3979
3c56819b 3980 /* Fill as many pages as possible. */
35f3d14d
JA
3981 for (i = 0, rem = len; i < pipe->buffers && rem; i++) {
3982 spd.pages[i] = alloc_page(GFP_KERNEL);
3983 if (!spd.pages[i])
34cd4998 3984 break;
3c56819b 3985
fa7c7f6e 3986 rem = tracing_fill_pipe_page(rem, iter);
3c56819b
EGM
3987
3988 /* Copy the data into the page, so we can start over. */
3989 ret = trace_seq_to_buffer(&iter->seq,
35f3d14d 3990 page_address(spd.pages[i]),
3c56819b
EGM
3991 iter->seq.len);
3992 if (ret < 0) {
35f3d14d 3993 __free_page(spd.pages[i]);
3c56819b
EGM
3994 break;
3995 }
35f3d14d
JA
3996 spd.partial[i].offset = 0;
3997 spd.partial[i].len = iter->seq.len;
3c56819b 3998
f9520750 3999 trace_seq_init(&iter->seq);
3c56819b
EGM
4000 }
4001
7e53bd42 4002 trace_access_unlock(iter->cpu_file);
4f535968 4003 trace_event_read_unlock();
d7350c3f 4004 mutex_unlock(&iter->mutex);
3c56819b
EGM
4005
4006 spd.nr_pages = i;
4007
35f3d14d
JA
4008 ret = splice_to_pipe(pipe, &spd);
4009out:
047fe360 4010 splice_shrink_spd(&spd);
35f3d14d 4011 return ret;
3c56819b 4012
34cd4998 4013out_err:
d7350c3f 4014 mutex_unlock(&iter->mutex);
35f3d14d 4015 goto out;
3c56819b
EGM
4016}
4017
a98a3c3f
SR
4018static ssize_t
4019tracing_entries_read(struct file *filp, char __user *ubuf,
4020 size_t cnt, loff_t *ppos)
4021{
2b6080f2
SR
4022 struct trace_cpu *tc = filp->private_data;
4023 struct trace_array *tr = tc->tr;
438ced17
VN
4024 char buf[64];
4025 int r = 0;
4026 ssize_t ret;
a98a3c3f 4027
db526ca3 4028 mutex_lock(&trace_types_lock);
438ced17 4029
2b6080f2 4030 if (tc->cpu == RING_BUFFER_ALL_CPUS) {
438ced17
VN
4031 int cpu, buf_size_same;
4032 unsigned long size;
4033
4034 size = 0;
4035 buf_size_same = 1;
4036 /* check if all cpu sizes are same */
4037 for_each_tracing_cpu(cpu) {
4038 /* fill in the size from first enabled cpu */
4039 if (size == 0)
12883efb
SRRH
4040 size = per_cpu_ptr(tr->trace_buffer.data, cpu)->entries;
4041 if (size != per_cpu_ptr(tr->trace_buffer.data, cpu)->entries) {
438ced17
VN
4042 buf_size_same = 0;
4043 break;
4044 }
4045 }
4046
4047 if (buf_size_same) {
4048 if (!ring_buffer_expanded)
4049 r = sprintf(buf, "%lu (expanded: %lu)\n",
4050 size >> 10,
4051 trace_buf_size >> 10);
4052 else
4053 r = sprintf(buf, "%lu\n", size >> 10);
4054 } else
4055 r = sprintf(buf, "X\n");
4056 } else
12883efb 4057 r = sprintf(buf, "%lu\n", per_cpu_ptr(tr->trace_buffer.data, tc->cpu)->entries >> 10);
438ced17 4058
db526ca3
SR
4059 mutex_unlock(&trace_types_lock);
4060
438ced17
VN
4061 ret = simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
4062 return ret;
a98a3c3f
SR
4063}
4064
4065static ssize_t
4066tracing_entries_write(struct file *filp, const char __user *ubuf,
4067 size_t cnt, loff_t *ppos)
4068{
2b6080f2 4069 struct trace_cpu *tc = filp->private_data;
a98a3c3f 4070 unsigned long val;
4f271a2a 4071 int ret;
a98a3c3f 4072
22fe9b54
PH
4073 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
4074 if (ret)
c6caeeb1 4075 return ret;
a98a3c3f
SR
4076
4077 /* must have at least 1 entry */
4078 if (!val)
4079 return -EINVAL;
4080
1696b2b0
SR
4081 /* value is in KB */
4082 val <<= 10;
4083
2b6080f2 4084 ret = tracing_resize_ring_buffer(tc->tr, val, tc->cpu);
4f271a2a
VN
4085 if (ret < 0)
4086 return ret;
a98a3c3f 4087
cf8517cf 4088 *ppos += cnt;
a98a3c3f 4089
4f271a2a
VN
4090 return cnt;
4091}
bf5e6519 4092
f81ab074
VN
4093static ssize_t
4094tracing_total_entries_read(struct file *filp, char __user *ubuf,
4095 size_t cnt, loff_t *ppos)
4096{
4097 struct trace_array *tr = filp->private_data;
4098 char buf[64];
4099 int r, cpu;
4100 unsigned long size = 0, expanded_size = 0;
4101
4102 mutex_lock(&trace_types_lock);
4103 for_each_tracing_cpu(cpu) {
12883efb 4104 size += per_cpu_ptr(tr->trace_buffer.data, cpu)->entries >> 10;
f81ab074
VN
4105 if (!ring_buffer_expanded)
4106 expanded_size += trace_buf_size >> 10;
4107 }
4108 if (ring_buffer_expanded)
4109 r = sprintf(buf, "%lu\n", size);
4110 else
4111 r = sprintf(buf, "%lu (expanded: %lu)\n", size, expanded_size);
4112 mutex_unlock(&trace_types_lock);
4113
4114 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
4115}
4116
4f271a2a
VN
4117static ssize_t
4118tracing_free_buffer_write(struct file *filp, const char __user *ubuf,
4119 size_t cnt, loff_t *ppos)
4120{
4121 /*
4122 * There is no need to read what the user has written, this function
4123 * is just to make sure that there is no error when "echo" is used
4124 */
4125
4126 *ppos += cnt;
a98a3c3f
SR
4127
4128 return cnt;
4129}
4130
4f271a2a
VN
4131static int
4132tracing_free_buffer_release(struct inode *inode, struct file *filp)
4133{
2b6080f2
SR
4134 struct trace_array *tr = inode->i_private;
4135
cf30cf67
SR
4136 /* disable tracing ? */
4137 if (trace_flags & TRACE_ITER_STOP_ON_FREE)
4138 tracing_off();
4f271a2a 4139 /* resize the ring buffer to 0 */
2b6080f2 4140 tracing_resize_ring_buffer(tr, 0, RING_BUFFER_ALL_CPUS);
4f271a2a
VN
4141
4142 return 0;
4143}
4144
5bf9a1ee
PP
4145static ssize_t
4146tracing_mark_write(struct file *filp, const char __user *ubuf,
4147 size_t cnt, loff_t *fpos)
4148{
d696b58c
SR
4149 unsigned long addr = (unsigned long)ubuf;
4150 struct ring_buffer_event *event;
4151 struct ring_buffer *buffer;
4152 struct print_entry *entry;
4153 unsigned long irq_flags;
4154 struct page *pages[2];
6edb2a8a 4155 void *map_page[2];
d696b58c
SR
4156 int nr_pages = 1;
4157 ssize_t written;
d696b58c
SR
4158 int offset;
4159 int size;
4160 int len;
4161 int ret;
6edb2a8a 4162 int i;
5bf9a1ee 4163
c76f0694 4164 if (tracing_disabled)
5bf9a1ee
PP
4165 return -EINVAL;
4166
5224c3a3
MSB
4167 if (!(trace_flags & TRACE_ITER_MARKERS))
4168 return -EINVAL;
4169
5bf9a1ee
PP
4170 if (cnt > TRACE_BUF_SIZE)
4171 cnt = TRACE_BUF_SIZE;
4172
d696b58c
SR
4173 /*
4174 * Userspace is injecting traces into the kernel trace buffer.
4175 * We want to be as non intrusive as possible.
4176 * To do so, we do not want to allocate any special buffers
4177 * or take any locks, but instead write the userspace data
4178 * straight into the ring buffer.
4179 *
4180 * First we need to pin the userspace buffer into memory,
4181 * which, most likely it is, because it just referenced it.
4182 * But there's no guarantee that it is. By using get_user_pages_fast()
4183 * and kmap_atomic/kunmap_atomic() we can get access to the
4184 * pages directly. We then write the data directly into the
4185 * ring buffer.
4186 */
4187 BUILD_BUG_ON(TRACE_BUF_SIZE >= PAGE_SIZE);
5bf9a1ee 4188
d696b58c
SR
4189 /* check if we cross pages */
4190 if ((addr & PAGE_MASK) != ((addr + cnt) & PAGE_MASK))
4191 nr_pages = 2;
4192
4193 offset = addr & (PAGE_SIZE - 1);
4194 addr &= PAGE_MASK;
4195
4196 ret = get_user_pages_fast(addr, nr_pages, 0, pages);
4197 if (ret < nr_pages) {
4198 while (--ret >= 0)
4199 put_page(pages[ret]);
4200 written = -EFAULT;
4201 goto out;
5bf9a1ee 4202 }
d696b58c 4203
6edb2a8a
SR
4204 for (i = 0; i < nr_pages; i++)
4205 map_page[i] = kmap_atomic(pages[i]);
d696b58c
SR
4206
4207 local_save_flags(irq_flags);
4208 size = sizeof(*entry) + cnt + 2; /* possible \n added */
12883efb 4209 buffer = global_trace.trace_buffer.buffer;
d696b58c
SR
4210 event = trace_buffer_lock_reserve(buffer, TRACE_PRINT, size,
4211 irq_flags, preempt_count());
4212 if (!event) {
4213 /* Ring buffer disabled, return as if not open for write */
4214 written = -EBADF;
4215 goto out_unlock;
5bf9a1ee 4216 }
d696b58c
SR
4217
4218 entry = ring_buffer_event_data(event);
4219 entry->ip = _THIS_IP_;
4220
4221 if (nr_pages == 2) {
4222 len = PAGE_SIZE - offset;
6edb2a8a
SR
4223 memcpy(&entry->buf, map_page[0] + offset, len);
4224 memcpy(&entry->buf[len], map_page[1], cnt - len);
c13d2f7c 4225 } else
6edb2a8a 4226 memcpy(&entry->buf, map_page[0] + offset, cnt);
5bf9a1ee 4227
d696b58c
SR
4228 if (entry->buf[cnt - 1] != '\n') {
4229 entry->buf[cnt] = '\n';
4230 entry->buf[cnt + 1] = '\0';
4231 } else
4232 entry->buf[cnt] = '\0';
4233
7ffbd48d 4234 __buffer_unlock_commit(buffer, event);
5bf9a1ee 4235
d696b58c 4236 written = cnt;
5bf9a1ee 4237
d696b58c 4238 *fpos += written;
1aa54bca 4239
d696b58c 4240 out_unlock:
6edb2a8a
SR
4241 for (i = 0; i < nr_pages; i++){
4242 kunmap_atomic(map_page[i]);
4243 put_page(pages[i]);
4244 }
d696b58c 4245 out:
1aa54bca 4246 return written;
5bf9a1ee
PP
4247}
4248
13f16d20 4249static int tracing_clock_show(struct seq_file *m, void *v)
5079f326 4250{
2b6080f2 4251 struct trace_array *tr = m->private;
5079f326
Z
4252 int i;
4253
4254 for (i = 0; i < ARRAY_SIZE(trace_clocks); i++)
13f16d20 4255 seq_printf(m,
5079f326 4256 "%s%s%s%s", i ? " " : "",
2b6080f2
SR
4257 i == tr->clock_id ? "[" : "", trace_clocks[i].name,
4258 i == tr->clock_id ? "]" : "");
13f16d20 4259 seq_putc(m, '\n');
5079f326 4260
13f16d20 4261 return 0;
5079f326
Z
4262}
4263
4264static ssize_t tracing_clock_write(struct file *filp, const char __user *ubuf,
4265 size_t cnt, loff_t *fpos)
4266{
2b6080f2
SR
4267 struct seq_file *m = filp->private_data;
4268 struct trace_array *tr = m->private;
5079f326
Z
4269 char buf[64];
4270 const char *clockstr;
4271 int i;
4272
4273 if (cnt >= sizeof(buf))
4274 return -EINVAL;
4275
4276 if (copy_from_user(&buf, ubuf, cnt))
4277 return -EFAULT;
4278
4279 buf[cnt] = 0;
4280
4281 clockstr = strstrip(buf);
4282
4283 for (i = 0; i < ARRAY_SIZE(trace_clocks); i++) {
4284 if (strcmp(trace_clocks[i].name, clockstr) == 0)
4285 break;
4286 }
4287 if (i == ARRAY_SIZE(trace_clocks))
4288 return -EINVAL;
4289
5079f326
Z
4290 mutex_lock(&trace_types_lock);
4291
2b6080f2
SR
4292 tr->clock_id = i;
4293
12883efb 4294 ring_buffer_set_clock(tr->trace_buffer.buffer, trace_clocks[i].func);
5079f326 4295
60303ed3
DS
4296 /*
4297 * New clock may not be consistent with the previous clock.
4298 * Reset the buffer so that it doesn't have incomparable timestamps.
4299 */
12883efb
SRRH
4300 tracing_reset_online_cpus(&global_trace.trace_buffer);
4301
4302#ifdef CONFIG_TRACER_MAX_TRACE
4303 if (tr->flags & TRACE_ARRAY_FL_GLOBAL && tr->max_buffer.buffer)
4304 ring_buffer_set_clock(tr->max_buffer.buffer, trace_clocks[i].func);
4305 tracing_reset_online_cpus(&global_trace.max_buffer);
4306#endif
60303ed3 4307
5079f326
Z
4308 mutex_unlock(&trace_types_lock);
4309
4310 *fpos += cnt;
4311
4312 return cnt;
4313}
4314
13f16d20
LZ
4315static int tracing_clock_open(struct inode *inode, struct file *file)
4316{
4317 if (tracing_disabled)
4318 return -ENODEV;
2b6080f2
SR
4319
4320 return single_open(file, tracing_clock_show, inode->i_private);
13f16d20
LZ
4321}
4322
6de58e62
SRRH
4323struct ftrace_buffer_info {
4324 struct trace_iterator iter;
4325 void *spare;
4326 unsigned int read;
4327};
4328
debdd57f
HT
4329#ifdef CONFIG_TRACER_SNAPSHOT
4330static int tracing_snapshot_open(struct inode *inode, struct file *file)
4331{
2b6080f2 4332 struct trace_cpu *tc = inode->i_private;
debdd57f 4333 struct trace_iterator *iter;
2b6080f2 4334 struct seq_file *m;
debdd57f
HT
4335 int ret = 0;
4336
4337 if (file->f_mode & FMODE_READ) {
4338 iter = __tracing_open(inode, file, true);
4339 if (IS_ERR(iter))
4340 ret = PTR_ERR(iter);
2b6080f2
SR
4341 } else {
4342 /* Writes still need the seq_file to hold the private data */
4343 m = kzalloc(sizeof(*m), GFP_KERNEL);
4344 if (!m)
4345 return -ENOMEM;
4346 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
4347 if (!iter) {
4348 kfree(m);
4349 return -ENOMEM;
4350 }
4351 iter->tr = tc->tr;
12883efb 4352 iter->trace_buffer = &tc->tr->max_buffer;
f1affcaa 4353 iter->cpu_file = tc->cpu;
2b6080f2
SR
4354 m->private = iter;
4355 file->private_data = m;
debdd57f 4356 }
2b6080f2 4357
debdd57f
HT
4358 return ret;
4359}
4360
4361static ssize_t
4362tracing_snapshot_write(struct file *filp, const char __user *ubuf, size_t cnt,
4363 loff_t *ppos)
4364{
2b6080f2
SR
4365 struct seq_file *m = filp->private_data;
4366 struct trace_iterator *iter = m->private;
4367 struct trace_array *tr = iter->tr;
debdd57f
HT
4368 unsigned long val;
4369 int ret;
4370
4371 ret = tracing_update_buffers();
4372 if (ret < 0)
4373 return ret;
4374
4375 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
4376 if (ret)
4377 return ret;
4378
4379 mutex_lock(&trace_types_lock);
4380
2b6080f2 4381 if (tr->current_trace->use_max_tr) {
debdd57f
HT
4382 ret = -EBUSY;
4383 goto out;
4384 }
4385
4386 switch (val) {
4387 case 0:
f1affcaa
SRRH
4388 if (iter->cpu_file != RING_BUFFER_ALL_CPUS) {
4389 ret = -EINVAL;
4390 break;
4391 }
45ad21ca 4392 if (tr->allocated_snapshot) {
debdd57f 4393 /* free spare buffer */
12883efb 4394 ring_buffer_resize(tr->max_buffer.buffer, 1,
debdd57f 4395 RING_BUFFER_ALL_CPUS);
12883efb
SRRH
4396 set_buffer_entries(&tr->max_buffer, 1);
4397 tracing_reset_online_cpus(&tr->max_buffer);
45ad21ca 4398 tr->allocated_snapshot = false;
debdd57f
HT
4399 }
4400 break;
4401 case 1:
f1affcaa
SRRH
4402/* Only allow per-cpu swap if the ring buffer supports it */
4403#ifndef CONFIG_RING_BUFFER_ALLOW_SWAP
4404 if (iter->cpu_file != RING_BUFFER_ALL_CPUS) {
4405 ret = -EINVAL;
4406 break;
4407 }
4408#endif
45ad21ca 4409 if (!tr->allocated_snapshot) {
debdd57f 4410 /* allocate spare buffer */
12883efb
SRRH
4411 ret = resize_buffer_duplicate_size(&tr->max_buffer,
4412 &tr->trace_buffer, RING_BUFFER_ALL_CPUS);
debdd57f
HT
4413 if (ret < 0)
4414 break;
45ad21ca 4415 tr->allocated_snapshot = true;
debdd57f 4416 }
debdd57f
HT
4417 local_irq_disable();
4418 /* Now, we're going to swap */
f1affcaa 4419 if (iter->cpu_file == RING_BUFFER_ALL_CPUS)
ce9bae55 4420 update_max_tr(tr, current, smp_processor_id());
f1affcaa 4421 else
ce9bae55 4422 update_max_tr_single(tr, current, iter->cpu_file);
debdd57f
HT
4423 local_irq_enable();
4424 break;
4425 default:
45ad21ca 4426 if (tr->allocated_snapshot) {
f1affcaa
SRRH
4427 if (iter->cpu_file == RING_BUFFER_ALL_CPUS)
4428 tracing_reset_online_cpus(&tr->max_buffer);
4429 else
4430 tracing_reset(&tr->max_buffer, iter->cpu_file);
4431 }
debdd57f
HT
4432 break;
4433 }
4434
4435 if (ret >= 0) {
4436 *ppos += cnt;
4437 ret = cnt;
4438 }
4439out:
4440 mutex_unlock(&trace_types_lock);
4441 return ret;
4442}
2b6080f2
SR
4443
4444static int tracing_snapshot_release(struct inode *inode, struct file *file)
4445{
4446 struct seq_file *m = file->private_data;
4447
4448 if (file->f_mode & FMODE_READ)
4449 return tracing_release(inode, file);
4450
4451 /* If write only, the seq_file is just a stub */
4452 if (m)
4453 kfree(m->private);
4454 kfree(m);
4455
4456 return 0;
4457}
4458
6de58e62
SRRH
4459static int tracing_buffers_open(struct inode *inode, struct file *filp);
4460static ssize_t tracing_buffers_read(struct file *filp, char __user *ubuf,
4461 size_t count, loff_t *ppos);
4462static int tracing_buffers_release(struct inode *inode, struct file *file);
4463static ssize_t tracing_buffers_splice_read(struct file *file, loff_t *ppos,
4464 struct pipe_inode_info *pipe, size_t len, unsigned int flags);
4465
4466static int snapshot_raw_open(struct inode *inode, struct file *filp)
4467{
4468 struct ftrace_buffer_info *info;
4469 int ret;
4470
4471 ret = tracing_buffers_open(inode, filp);
4472 if (ret < 0)
4473 return ret;
4474
4475 info = filp->private_data;
4476
4477 if (info->iter.trace->use_max_tr) {
4478 tracing_buffers_release(inode, filp);
4479 return -EBUSY;
4480 }
4481
4482 info->iter.snapshot = true;
4483 info->iter.trace_buffer = &info->iter.tr->max_buffer;
4484
4485 return ret;
4486}
4487
debdd57f
HT
4488#endif /* CONFIG_TRACER_SNAPSHOT */
4489
4490
5e2336a0 4491static const struct file_operations tracing_max_lat_fops = {
4bf39a94
IM
4492 .open = tracing_open_generic,
4493 .read = tracing_max_lat_read,
4494 .write = tracing_max_lat_write,
b444786f 4495 .llseek = generic_file_llseek,
bc0c38d1
SR
4496};
4497
5e2336a0 4498static const struct file_operations set_tracer_fops = {
4bf39a94
IM
4499 .open = tracing_open_generic,
4500 .read = tracing_set_trace_read,
4501 .write = tracing_set_trace_write,
b444786f 4502 .llseek = generic_file_llseek,
bc0c38d1
SR
4503};
4504
5e2336a0 4505static const struct file_operations tracing_pipe_fops = {
4bf39a94 4506 .open = tracing_open_pipe,
2a2cc8f7 4507 .poll = tracing_poll_pipe,
4bf39a94 4508 .read = tracing_read_pipe,
3c56819b 4509 .splice_read = tracing_splice_read_pipe,
4bf39a94 4510 .release = tracing_release_pipe,
b444786f 4511 .llseek = no_llseek,
b3806b43
SR
4512};
4513
5e2336a0 4514static const struct file_operations tracing_entries_fops = {
2b6080f2 4515 .open = tracing_open_generic,
a98a3c3f
SR
4516 .read = tracing_entries_read,
4517 .write = tracing_entries_write,
b444786f 4518 .llseek = generic_file_llseek,
a98a3c3f
SR
4519};
4520
f81ab074
VN
4521static const struct file_operations tracing_total_entries_fops = {
4522 .open = tracing_open_generic,
4523 .read = tracing_total_entries_read,
4524 .llseek = generic_file_llseek,
4525};
4526
4f271a2a
VN
4527static const struct file_operations tracing_free_buffer_fops = {
4528 .write = tracing_free_buffer_write,
4529 .release = tracing_free_buffer_release,
4530};
4531
5e2336a0 4532static const struct file_operations tracing_mark_fops = {
43a15386 4533 .open = tracing_open_generic,
5bf9a1ee 4534 .write = tracing_mark_write,
b444786f 4535 .llseek = generic_file_llseek,
5bf9a1ee
PP
4536};
4537
5079f326 4538static const struct file_operations trace_clock_fops = {
13f16d20
LZ
4539 .open = tracing_clock_open,
4540 .read = seq_read,
4541 .llseek = seq_lseek,
4542 .release = single_release,
5079f326
Z
4543 .write = tracing_clock_write,
4544};
4545
debdd57f
HT
4546#ifdef CONFIG_TRACER_SNAPSHOT
4547static const struct file_operations snapshot_fops = {
4548 .open = tracing_snapshot_open,
4549 .read = seq_read,
4550 .write = tracing_snapshot_write,
4551 .llseek = tracing_seek,
2b6080f2 4552 .release = tracing_snapshot_release,
debdd57f 4553};
debdd57f 4554
6de58e62
SRRH
4555static const struct file_operations snapshot_raw_fops = {
4556 .open = snapshot_raw_open,
4557 .read = tracing_buffers_read,
4558 .release = tracing_buffers_release,
4559 .splice_read = tracing_buffers_splice_read,
4560 .llseek = no_llseek,
2cadf913
SR
4561};
4562
6de58e62
SRRH
4563#endif /* CONFIG_TRACER_SNAPSHOT */
4564
2cadf913
SR
4565static int tracing_buffers_open(struct inode *inode, struct file *filp)
4566{
2b6080f2
SR
4567 struct trace_cpu *tc = inode->i_private;
4568 struct trace_array *tr = tc->tr;
2cadf913
SR
4569 struct ftrace_buffer_info *info;
4570
4571 if (tracing_disabled)
4572 return -ENODEV;
4573
4574 info = kzalloc(sizeof(*info), GFP_KERNEL);
4575 if (!info)
4576 return -ENOMEM;
4577
a695cb58
SRRH
4578 mutex_lock(&trace_types_lock);
4579
4580 tr->ref++;
4581
cc60cdc9
SR
4582 info->iter.tr = tr;
4583 info->iter.cpu_file = tc->cpu;
b627344f 4584 info->iter.trace = tr->current_trace;
12883efb 4585 info->iter.trace_buffer = &tr->trace_buffer;
cc60cdc9 4586 info->spare = NULL;
2cadf913 4587 /* Force reading ring buffer for first read */
cc60cdc9 4588 info->read = (unsigned int)-1;
2cadf913
SR
4589
4590 filp->private_data = info;
4591
a695cb58
SRRH
4592 mutex_unlock(&trace_types_lock);
4593
d1e7e02f 4594 return nonseekable_open(inode, filp);
2cadf913
SR
4595}
4596
cc60cdc9
SR
4597static unsigned int
4598tracing_buffers_poll(struct file *filp, poll_table *poll_table)
4599{
4600 struct ftrace_buffer_info *info = filp->private_data;
4601 struct trace_iterator *iter = &info->iter;
4602
4603 return trace_poll(iter, filp, poll_table);
4604}
4605
2cadf913
SR
4606static ssize_t
4607tracing_buffers_read(struct file *filp, char __user *ubuf,
4608 size_t count, loff_t *ppos)
4609{
4610 struct ftrace_buffer_info *info = filp->private_data;
cc60cdc9 4611 struct trace_iterator *iter = &info->iter;
2cadf913 4612 ssize_t ret;
6de58e62 4613 ssize_t size;
2cadf913 4614
2dc5d12b
SR
4615 if (!count)
4616 return 0;
4617
6de58e62
SRRH
4618 mutex_lock(&trace_types_lock);
4619
4620#ifdef CONFIG_TRACER_MAX_TRACE
4621 if (iter->snapshot && iter->tr->current_trace->use_max_tr) {
4622 size = -EBUSY;
4623 goto out_unlock;
4624 }
4625#endif
4626
ddd538f3 4627 if (!info->spare)
12883efb
SRRH
4628 info->spare = ring_buffer_alloc_read_page(iter->trace_buffer->buffer,
4629 iter->cpu_file);
6de58e62 4630 size = -ENOMEM;
ddd538f3 4631 if (!info->spare)
6de58e62 4632 goto out_unlock;
ddd538f3 4633
2cadf913
SR
4634 /* Do we have previous read data to read? */
4635 if (info->read < PAGE_SIZE)
4636 goto read;
4637
b627344f 4638 again:
cc60cdc9 4639 trace_access_lock(iter->cpu_file);
12883efb 4640 ret = ring_buffer_read_page(iter->trace_buffer->buffer,
2cadf913
SR
4641 &info->spare,
4642 count,
cc60cdc9
SR
4643 iter->cpu_file, 0);
4644 trace_access_unlock(iter->cpu_file);
b627344f
SR
4645
4646 if (ret < 0) {
4647 if (trace_empty(iter)) {
6de58e62
SRRH
4648 if ((filp->f_flags & O_NONBLOCK)) {
4649 size = -EAGAIN;
4650 goto out_unlock;
4651 }
4652 mutex_unlock(&trace_types_lock);
b627344f 4653 iter->trace->wait_pipe(iter);
6de58e62
SRRH
4654 mutex_lock(&trace_types_lock);
4655 if (signal_pending(current)) {
4656 size = -EINTR;
4657 goto out_unlock;
4658 }
b627344f
SR
4659 goto again;
4660 }
6de58e62
SRRH
4661 size = 0;
4662 goto out_unlock;
b627344f 4663 }
2cadf913 4664
436fc280 4665 info->read = 0;
b627344f 4666 read:
2cadf913
SR
4667 size = PAGE_SIZE - info->read;
4668 if (size > count)
4669 size = count;
4670
4671 ret = copy_to_user(ubuf, info->spare + info->read, size);
6de58e62
SRRH
4672 if (ret == size) {
4673 size = -EFAULT;
4674 goto out_unlock;
4675 }
2dc5d12b
SR
4676 size -= ret;
4677
2cadf913
SR
4678 *ppos += size;
4679 info->read += size;
4680
6de58e62
SRRH
4681 out_unlock:
4682 mutex_unlock(&trace_types_lock);
4683
2cadf913
SR
4684 return size;
4685}
4686
4687static int tracing_buffers_release(struct inode *inode, struct file *file)
4688{
4689 struct ftrace_buffer_info *info = file->private_data;
cc60cdc9 4690 struct trace_iterator *iter = &info->iter;
2cadf913 4691
a695cb58
SRRH
4692 mutex_lock(&trace_types_lock);
4693
4694 WARN_ON(!iter->tr->ref);
4695 iter->tr->ref--;
4696
ddd538f3 4697 if (info->spare)
12883efb 4698 ring_buffer_free_read_page(iter->trace_buffer->buffer, info->spare);
2cadf913
SR
4699 kfree(info);
4700
a695cb58
SRRH
4701 mutex_unlock(&trace_types_lock);
4702
2cadf913
SR
4703 return 0;
4704}
4705
4706struct buffer_ref {
4707 struct ring_buffer *buffer;
4708 void *page;
4709 int ref;
4710};
4711
4712static void buffer_pipe_buf_release(struct pipe_inode_info *pipe,
4713 struct pipe_buffer *buf)
4714{
4715 struct buffer_ref *ref = (struct buffer_ref *)buf->private;
4716
4717 if (--ref->ref)
4718 return;
4719
4720 ring_buffer_free_read_page(ref->buffer, ref->page);
4721 kfree(ref);
4722 buf->private = 0;
4723}
4724
2cadf913
SR
4725static void buffer_pipe_buf_get(struct pipe_inode_info *pipe,
4726 struct pipe_buffer *buf)
4727{
4728 struct buffer_ref *ref = (struct buffer_ref *)buf->private;
4729
4730 ref->ref++;
4731}
4732
4733/* Pipe buffer operations for a buffer. */
28dfef8f 4734static const struct pipe_buf_operations buffer_pipe_buf_ops = {
2cadf913
SR
4735 .can_merge = 0,
4736 .map = generic_pipe_buf_map,
4737 .unmap = generic_pipe_buf_unmap,
4738 .confirm = generic_pipe_buf_confirm,
4739 .release = buffer_pipe_buf_release,
d55cb6cf 4740 .steal = generic_pipe_buf_steal,
2cadf913
SR
4741 .get = buffer_pipe_buf_get,
4742};
4743
4744/*
4745 * Callback from splice_to_pipe(), if we need to release some pages
4746 * at the end of the spd in case we error'ed out in filling the pipe.
4747 */
4748static void buffer_spd_release(struct splice_pipe_desc *spd, unsigned int i)
4749{
4750 struct buffer_ref *ref =
4751 (struct buffer_ref *)spd->partial[i].private;
4752
4753 if (--ref->ref)
4754 return;
4755
4756 ring_buffer_free_read_page(ref->buffer, ref->page);
4757 kfree(ref);
4758 spd->partial[i].private = 0;
4759}
4760
4761static ssize_t
4762tracing_buffers_splice_read(struct file *file, loff_t *ppos,
4763 struct pipe_inode_info *pipe, size_t len,
4764 unsigned int flags)
4765{
4766 struct ftrace_buffer_info *info = file->private_data;
cc60cdc9 4767 struct trace_iterator *iter = &info->iter;
35f3d14d
JA
4768 struct partial_page partial_def[PIPE_DEF_BUFFERS];
4769 struct page *pages_def[PIPE_DEF_BUFFERS];
2cadf913 4770 struct splice_pipe_desc spd = {
35f3d14d
JA
4771 .pages = pages_def,
4772 .partial = partial_def,
047fe360 4773 .nr_pages_max = PIPE_DEF_BUFFERS,
2cadf913
SR
4774 .flags = flags,
4775 .ops = &buffer_pipe_buf_ops,
4776 .spd_release = buffer_spd_release,
4777 };
4778 struct buffer_ref *ref;
93459c6c 4779 int entries, size, i;
6de58e62 4780 ssize_t ret;
2cadf913 4781
6de58e62
SRRH
4782 mutex_lock(&trace_types_lock);
4783
4784#ifdef CONFIG_TRACER_MAX_TRACE
4785 if (iter->snapshot && iter->tr->current_trace->use_max_tr) {
4786 ret = -EBUSY;
4787 goto out;
4788 }
4789#endif
4790
4791 if (splice_grow_spd(pipe, &spd)) {
4792 ret = -ENOMEM;
4793 goto out;
4794 }
35f3d14d 4795
93cfb3c9 4796 if (*ppos & (PAGE_SIZE - 1)) {
35f3d14d
JA
4797 ret = -EINVAL;
4798 goto out;
93cfb3c9
LJ
4799 }
4800
4801 if (len & (PAGE_SIZE - 1)) {
35f3d14d
JA
4802 if (len < PAGE_SIZE) {
4803 ret = -EINVAL;
4804 goto out;
4805 }
93cfb3c9
LJ
4806 len &= PAGE_MASK;
4807 }
4808
cc60cdc9
SR
4809 again:
4810 trace_access_lock(iter->cpu_file);
12883efb 4811 entries = ring_buffer_entries_cpu(iter->trace_buffer->buffer, iter->cpu_file);
93459c6c 4812
35f3d14d 4813 for (i = 0; i < pipe->buffers && len && entries; i++, len -= PAGE_SIZE) {
2cadf913
SR
4814 struct page *page;
4815 int r;
4816
4817 ref = kzalloc(sizeof(*ref), GFP_KERNEL);
4818 if (!ref)
4819 break;
4820
7267fa68 4821 ref->ref = 1;
12883efb 4822 ref->buffer = iter->trace_buffer->buffer;
cc60cdc9 4823 ref->page = ring_buffer_alloc_read_page(ref->buffer, iter->cpu_file);
2cadf913
SR
4824 if (!ref->page) {
4825 kfree(ref);
4826 break;
4827 }
4828
4829 r = ring_buffer_read_page(ref->buffer, &ref->page,
cc60cdc9 4830 len, iter->cpu_file, 1);
2cadf913 4831 if (r < 0) {
7ea59064 4832 ring_buffer_free_read_page(ref->buffer, ref->page);
2cadf913
SR
4833 kfree(ref);
4834 break;
4835 }
4836
4837 /*
4838 * zero out any left over data, this is going to
4839 * user land.
4840 */
4841 size = ring_buffer_page_len(ref->page);
4842 if (size < PAGE_SIZE)
4843 memset(ref->page + size, 0, PAGE_SIZE - size);
4844
4845 page = virt_to_page(ref->page);
4846
4847 spd.pages[i] = page;
4848 spd.partial[i].len = PAGE_SIZE;
4849 spd.partial[i].offset = 0;
4850 spd.partial[i].private = (unsigned long)ref;
4851 spd.nr_pages++;
93cfb3c9 4852 *ppos += PAGE_SIZE;
93459c6c 4853
12883efb 4854 entries = ring_buffer_entries_cpu(iter->trace_buffer->buffer, iter->cpu_file);
2cadf913
SR
4855 }
4856
cc60cdc9 4857 trace_access_unlock(iter->cpu_file);
2cadf913
SR
4858 spd.nr_pages = i;
4859
4860 /* did we read anything? */
4861 if (!spd.nr_pages) {
cc60cdc9 4862 if ((file->f_flags & O_NONBLOCK) || (flags & SPLICE_F_NONBLOCK)) {
2cadf913 4863 ret = -EAGAIN;
cc60cdc9
SR
4864 goto out;
4865 }
6de58e62 4866 mutex_unlock(&trace_types_lock);
b627344f 4867 iter->trace->wait_pipe(iter);
6de58e62 4868 mutex_lock(&trace_types_lock);
cc60cdc9
SR
4869 if (signal_pending(current)) {
4870 ret = -EINTR;
4871 goto out;
4872 }
4873 goto again;
2cadf913
SR
4874 }
4875
4876 ret = splice_to_pipe(pipe, &spd);
047fe360 4877 splice_shrink_spd(&spd);
35f3d14d 4878out:
6de58e62
SRRH
4879 mutex_unlock(&trace_types_lock);
4880
2cadf913
SR
4881 return ret;
4882}
4883
4884static const struct file_operations tracing_buffers_fops = {
4885 .open = tracing_buffers_open,
4886 .read = tracing_buffers_read,
cc60cdc9 4887 .poll = tracing_buffers_poll,
2cadf913
SR
4888 .release = tracing_buffers_release,
4889 .splice_read = tracing_buffers_splice_read,
4890 .llseek = no_llseek,
4891};
4892
c8d77183
SR
4893static ssize_t
4894tracing_stats_read(struct file *filp, char __user *ubuf,
4895 size_t count, loff_t *ppos)
4896{
2b6080f2
SR
4897 struct trace_cpu *tc = filp->private_data;
4898 struct trace_array *tr = tc->tr;
12883efb 4899 struct trace_buffer *trace_buf = &tr->trace_buffer;
c8d77183
SR
4900 struct trace_seq *s;
4901 unsigned long cnt;
c64e148a
VN
4902 unsigned long long t;
4903 unsigned long usec_rem;
2b6080f2 4904 int cpu = tc->cpu;
c8d77183 4905
e4f2d10f 4906 s = kmalloc(sizeof(*s), GFP_KERNEL);
c8d77183 4907 if (!s)
a646365c 4908 return -ENOMEM;
c8d77183
SR
4909
4910 trace_seq_init(s);
4911
12883efb 4912 cnt = ring_buffer_entries_cpu(trace_buf->buffer, cpu);
c8d77183
SR
4913 trace_seq_printf(s, "entries: %ld\n", cnt);
4914
12883efb 4915 cnt = ring_buffer_overrun_cpu(trace_buf->buffer, cpu);
c8d77183
SR
4916 trace_seq_printf(s, "overrun: %ld\n", cnt);
4917
12883efb 4918 cnt = ring_buffer_commit_overrun_cpu(trace_buf->buffer, cpu);
c8d77183
SR
4919 trace_seq_printf(s, "commit overrun: %ld\n", cnt);
4920
12883efb 4921 cnt = ring_buffer_bytes_cpu(trace_buf->buffer, cpu);
c64e148a
VN
4922 trace_seq_printf(s, "bytes: %ld\n", cnt);
4923
11043d8b
YY
4924 if (trace_clocks[trace_clock_id].in_ns) {
4925 /* local or global for trace_clock */
12883efb 4926 t = ns2usecs(ring_buffer_oldest_event_ts(trace_buf->buffer, cpu));
11043d8b
YY
4927 usec_rem = do_div(t, USEC_PER_SEC);
4928 trace_seq_printf(s, "oldest event ts: %5llu.%06lu\n",
4929 t, usec_rem);
4930
12883efb 4931 t = ns2usecs(ring_buffer_time_stamp(trace_buf->buffer, cpu));
11043d8b
YY
4932 usec_rem = do_div(t, USEC_PER_SEC);
4933 trace_seq_printf(s, "now ts: %5llu.%06lu\n", t, usec_rem);
4934 } else {
4935 /* counter or tsc mode for trace_clock */
4936 trace_seq_printf(s, "oldest event ts: %llu\n",
12883efb 4937 ring_buffer_oldest_event_ts(trace_buf->buffer, cpu));
c64e148a 4938
11043d8b 4939 trace_seq_printf(s, "now ts: %llu\n",
12883efb 4940 ring_buffer_time_stamp(trace_buf->buffer, cpu));
11043d8b 4941 }
c64e148a 4942
12883efb 4943 cnt = ring_buffer_dropped_events_cpu(trace_buf->buffer, cpu);
884bfe89
SP
4944 trace_seq_printf(s, "dropped events: %ld\n", cnt);
4945
12883efb 4946 cnt = ring_buffer_read_events_cpu(trace_buf->buffer, cpu);
ad964704
SRRH
4947 trace_seq_printf(s, "read events: %ld\n", cnt);
4948
c8d77183
SR
4949 count = simple_read_from_buffer(ubuf, count, ppos, s->buffer, s->len);
4950
4951 kfree(s);
4952
4953 return count;
4954}
4955
4956static const struct file_operations tracing_stats_fops = {
4957 .open = tracing_open_generic,
4958 .read = tracing_stats_read,
b444786f 4959 .llseek = generic_file_llseek,
c8d77183
SR
4960};
4961
bc0c38d1
SR
4962#ifdef CONFIG_DYNAMIC_FTRACE
4963
b807c3d0
SR
4964int __weak ftrace_arch_read_dyn_info(char *buf, int size)
4965{
4966 return 0;
4967}
4968
bc0c38d1 4969static ssize_t
b807c3d0 4970tracing_read_dyn_info(struct file *filp, char __user *ubuf,
bc0c38d1
SR
4971 size_t cnt, loff_t *ppos)
4972{
a26a2a27
SR
4973 static char ftrace_dyn_info_buffer[1024];
4974 static DEFINE_MUTEX(dyn_info_mutex);
bc0c38d1 4975 unsigned long *p = filp->private_data;
b807c3d0 4976 char *buf = ftrace_dyn_info_buffer;
a26a2a27 4977 int size = ARRAY_SIZE(ftrace_dyn_info_buffer);
bc0c38d1
SR
4978 int r;
4979
b807c3d0
SR
4980 mutex_lock(&dyn_info_mutex);
4981 r = sprintf(buf, "%ld ", *p);
4bf39a94 4982
a26a2a27 4983 r += ftrace_arch_read_dyn_info(buf+r, (size-1)-r);
b807c3d0
SR
4984 buf[r++] = '\n';
4985
4986 r = simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
4987
4988 mutex_unlock(&dyn_info_mutex);
4989
4990 return r;
bc0c38d1
SR
4991}
4992
5e2336a0 4993static const struct file_operations tracing_dyn_info_fops = {
4bf39a94 4994 .open = tracing_open_generic,
b807c3d0 4995 .read = tracing_read_dyn_info,
b444786f 4996 .llseek = generic_file_llseek,
bc0c38d1
SR
4997};
4998#endif
4999
2b6080f2 5000struct dentry *tracing_init_dentry_tr(struct trace_array *tr)
bc0c38d1
SR
5001{
5002 static int once;
5003
2b6080f2
SR
5004 if (tr->dir)
5005 return tr->dir;
bc0c38d1 5006
3e1f60b8
FW
5007 if (!debugfs_initialized())
5008 return NULL;
5009
2b6080f2
SR
5010 if (tr->flags & TRACE_ARRAY_FL_GLOBAL)
5011 tr->dir = debugfs_create_dir("tracing", NULL);
bc0c38d1 5012
2b6080f2 5013 if (!tr->dir && !once) {
bc0c38d1
SR
5014 once = 1;
5015 pr_warning("Could not create debugfs directory 'tracing'\n");
5016 return NULL;
5017 }
5018
2b6080f2 5019 return tr->dir;
bc0c38d1
SR
5020}
5021
2b6080f2
SR
5022struct dentry *tracing_init_dentry(void)
5023{
5024 return tracing_init_dentry_tr(&global_trace);
5025}
b04cc6b1 5026
2b6080f2 5027static struct dentry *tracing_dentry_percpu(struct trace_array *tr, int cpu)
b04cc6b1 5028{
b04cc6b1
FW
5029 struct dentry *d_tracer;
5030
2b6080f2
SR
5031 if (tr->percpu_dir)
5032 return tr->percpu_dir;
b04cc6b1 5033
2b6080f2 5034 d_tracer = tracing_init_dentry_tr(tr);
b04cc6b1
FW
5035 if (!d_tracer)
5036 return NULL;
5037
2b6080f2 5038 tr->percpu_dir = debugfs_create_dir("per_cpu", d_tracer);
b04cc6b1 5039
2b6080f2
SR
5040 WARN_ONCE(!tr->percpu_dir,
5041 "Could not create debugfs directory 'per_cpu/%d'\n", cpu);
b04cc6b1 5042
2b6080f2 5043 return tr->percpu_dir;
b04cc6b1
FW
5044}
5045
2b6080f2
SR
5046static void
5047tracing_init_debugfs_percpu(struct trace_array *tr, long cpu)
b04cc6b1 5048{
12883efb 5049 struct trace_array_cpu *data = per_cpu_ptr(tr->trace_buffer.data, cpu);
2b6080f2 5050 struct dentry *d_percpu = tracing_dentry_percpu(tr, cpu);
5452af66 5051 struct dentry *d_cpu;
dd49a38c 5052 char cpu_dir[30]; /* 30 characters should be more than enough */
b04cc6b1 5053
0a3d7ce7
NK
5054 if (!d_percpu)
5055 return;
5056
dd49a38c 5057 snprintf(cpu_dir, 30, "cpu%ld", cpu);
8656e7a2
FW
5058 d_cpu = debugfs_create_dir(cpu_dir, d_percpu);
5059 if (!d_cpu) {
5060 pr_warning("Could not create debugfs '%s' entry\n", cpu_dir);
5061 return;
5062 }
b04cc6b1 5063
8656e7a2 5064 /* per cpu trace_pipe */
5452af66 5065 trace_create_file("trace_pipe", 0444, d_cpu,
2b6080f2 5066 (void *)&data->trace_cpu, &tracing_pipe_fops);
b04cc6b1
FW
5067
5068 /* per cpu trace */
5452af66 5069 trace_create_file("trace", 0644, d_cpu,
2b6080f2 5070 (void *)&data->trace_cpu, &tracing_fops);
7f96f93f 5071
5452af66 5072 trace_create_file("trace_pipe_raw", 0444, d_cpu,
2b6080f2 5073 (void *)&data->trace_cpu, &tracing_buffers_fops);
7f96f93f 5074
c8d77183 5075 trace_create_file("stats", 0444, d_cpu,
2b6080f2 5076 (void *)&data->trace_cpu, &tracing_stats_fops);
438ced17
VN
5077
5078 trace_create_file("buffer_size_kb", 0444, d_cpu,
2b6080f2 5079 (void *)&data->trace_cpu, &tracing_entries_fops);
f1affcaa
SRRH
5080
5081#ifdef CONFIG_TRACER_SNAPSHOT
5082 trace_create_file("snapshot", 0644, d_cpu,
5083 (void *)&data->trace_cpu, &snapshot_fops);
6de58e62
SRRH
5084
5085 trace_create_file("snapshot_raw", 0444, d_cpu,
5086 (void *)&data->trace_cpu, &snapshot_raw_fops);
f1affcaa 5087#endif
b04cc6b1
FW
5088}
5089
60a11774
SR
5090#ifdef CONFIG_FTRACE_SELFTEST
5091/* Let selftest have access to static functions in this file */
5092#include "trace_selftest.c"
5093#endif
5094
577b785f
SR
5095struct trace_option_dentry {
5096 struct tracer_opt *opt;
5097 struct tracer_flags *flags;
2b6080f2 5098 struct trace_array *tr;
577b785f
SR
5099 struct dentry *entry;
5100};
5101
5102static ssize_t
5103trace_options_read(struct file *filp, char __user *ubuf, size_t cnt,
5104 loff_t *ppos)
5105{
5106 struct trace_option_dentry *topt = filp->private_data;
5107 char *buf;
5108
5109 if (topt->flags->val & topt->opt->bit)
5110 buf = "1\n";
5111 else
5112 buf = "0\n";
5113
5114 return simple_read_from_buffer(ubuf, cnt, ppos, buf, 2);
5115}
5116
5117static ssize_t
5118trace_options_write(struct file *filp, const char __user *ubuf, size_t cnt,
5119 loff_t *ppos)
5120{
5121 struct trace_option_dentry *topt = filp->private_data;
5122 unsigned long val;
577b785f
SR
5123 int ret;
5124
22fe9b54
PH
5125 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
5126 if (ret)
577b785f
SR
5127 return ret;
5128
8d18eaaf
LZ
5129 if (val != 0 && val != 1)
5130 return -EINVAL;
577b785f 5131
8d18eaaf 5132 if (!!(topt->flags->val & topt->opt->bit) != val) {
577b785f 5133 mutex_lock(&trace_types_lock);
2b6080f2 5134 ret = __set_tracer_option(topt->tr->current_trace, topt->flags,
c757bea9 5135 topt->opt, !val);
577b785f
SR
5136 mutex_unlock(&trace_types_lock);
5137 if (ret)
5138 return ret;
577b785f
SR
5139 }
5140
5141 *ppos += cnt;
5142
5143 return cnt;
5144}
5145
5146
5147static const struct file_operations trace_options_fops = {
5148 .open = tracing_open_generic,
5149 .read = trace_options_read,
5150 .write = trace_options_write,
b444786f 5151 .llseek = generic_file_llseek,
577b785f
SR
5152};
5153
a8259075
SR
5154static ssize_t
5155trace_options_core_read(struct file *filp, char __user *ubuf, size_t cnt,
5156 loff_t *ppos)
5157{
5158 long index = (long)filp->private_data;
5159 char *buf;
5160
5161 if (trace_flags & (1 << index))
5162 buf = "1\n";
5163 else
5164 buf = "0\n";
5165
5166 return simple_read_from_buffer(ubuf, cnt, ppos, buf, 2);
5167}
5168
5169static ssize_t
5170trace_options_core_write(struct file *filp, const char __user *ubuf, size_t cnt,
5171 loff_t *ppos)
5172{
2b6080f2 5173 struct trace_array *tr = &global_trace;
a8259075 5174 long index = (long)filp->private_data;
a8259075
SR
5175 unsigned long val;
5176 int ret;
5177
22fe9b54
PH
5178 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
5179 if (ret)
a8259075
SR
5180 return ret;
5181
f2d84b65 5182 if (val != 0 && val != 1)
a8259075 5183 return -EINVAL;
69d34da2
SRRH
5184
5185 mutex_lock(&trace_types_lock);
2b6080f2 5186 ret = set_tracer_flag(tr, 1 << index, val);
69d34da2 5187 mutex_unlock(&trace_types_lock);
a8259075 5188
613f04a0
SRRH
5189 if (ret < 0)
5190 return ret;
5191
a8259075
SR
5192 *ppos += cnt;
5193
5194 return cnt;
5195}
5196
a8259075
SR
5197static const struct file_operations trace_options_core_fops = {
5198 .open = tracing_open_generic,
5199 .read = trace_options_core_read,
5200 .write = trace_options_core_write,
b444786f 5201 .llseek = generic_file_llseek,
a8259075
SR
5202};
5203
5452af66 5204struct dentry *trace_create_file(const char *name,
f4ae40a6 5205 umode_t mode,
5452af66
FW
5206 struct dentry *parent,
5207 void *data,
5208 const struct file_operations *fops)
5209{
5210 struct dentry *ret;
5211
5212 ret = debugfs_create_file(name, mode, parent, data, fops);
5213 if (!ret)
5214 pr_warning("Could not create debugfs '%s' entry\n", name);
5215
5216 return ret;
5217}
5218
5219
2b6080f2 5220static struct dentry *trace_options_init_dentry(struct trace_array *tr)
a8259075
SR
5221{
5222 struct dentry *d_tracer;
a8259075 5223
2b6080f2
SR
5224 if (tr->options)
5225 return tr->options;
a8259075 5226
2b6080f2 5227 d_tracer = tracing_init_dentry_tr(tr);
a8259075
SR
5228 if (!d_tracer)
5229 return NULL;
5230
2b6080f2
SR
5231 tr->options = debugfs_create_dir("options", d_tracer);
5232 if (!tr->options) {
a8259075
SR
5233 pr_warning("Could not create debugfs directory 'options'\n");
5234 return NULL;
5235 }
5236
2b6080f2 5237 return tr->options;
a8259075
SR
5238}
5239
577b785f 5240static void
2b6080f2
SR
5241create_trace_option_file(struct trace_array *tr,
5242 struct trace_option_dentry *topt,
577b785f
SR
5243 struct tracer_flags *flags,
5244 struct tracer_opt *opt)
5245{
5246 struct dentry *t_options;
577b785f 5247
2b6080f2 5248 t_options = trace_options_init_dentry(tr);
577b785f
SR
5249 if (!t_options)
5250 return;
5251
5252 topt->flags = flags;
5253 topt->opt = opt;
2b6080f2 5254 topt->tr = tr;
577b785f 5255
5452af66 5256 topt->entry = trace_create_file(opt->name, 0644, t_options, topt,
577b785f
SR
5257 &trace_options_fops);
5258
577b785f
SR
5259}
5260
5261static struct trace_option_dentry *
2b6080f2 5262create_trace_option_files(struct trace_array *tr, struct tracer *tracer)
577b785f
SR
5263{
5264 struct trace_option_dentry *topts;
5265 struct tracer_flags *flags;
5266 struct tracer_opt *opts;
5267 int cnt;
5268
5269 if (!tracer)
5270 return NULL;
5271
5272 flags = tracer->flags;
5273
5274 if (!flags || !flags->opts)
5275 return NULL;
5276
5277 opts = flags->opts;
5278
5279 for (cnt = 0; opts[cnt].name; cnt++)
5280 ;
5281
0cfe8245 5282 topts = kcalloc(cnt + 1, sizeof(*topts), GFP_KERNEL);
577b785f
SR
5283 if (!topts)
5284 return NULL;
5285
5286 for (cnt = 0; opts[cnt].name; cnt++)
2b6080f2 5287 create_trace_option_file(tr, &topts[cnt], flags,
577b785f
SR
5288 &opts[cnt]);
5289
5290 return topts;
5291}
5292
5293static void
5294destroy_trace_option_files(struct trace_option_dentry *topts)
5295{
5296 int cnt;
5297
5298 if (!topts)
5299 return;
5300
5301 for (cnt = 0; topts[cnt].opt; cnt++) {
5302 if (topts[cnt].entry)
5303 debugfs_remove(topts[cnt].entry);
5304 }
5305
5306 kfree(topts);
5307}
5308
a8259075 5309static struct dentry *
2b6080f2
SR
5310create_trace_option_core_file(struct trace_array *tr,
5311 const char *option, long index)
a8259075
SR
5312{
5313 struct dentry *t_options;
a8259075 5314
2b6080f2 5315 t_options = trace_options_init_dentry(tr);
a8259075
SR
5316 if (!t_options)
5317 return NULL;
5318
5452af66 5319 return trace_create_file(option, 0644, t_options, (void *)index,
a8259075 5320 &trace_options_core_fops);
a8259075
SR
5321}
5322
2b6080f2 5323static __init void create_trace_options_dir(struct trace_array *tr)
a8259075
SR
5324{
5325 struct dentry *t_options;
a8259075
SR
5326 int i;
5327
2b6080f2 5328 t_options = trace_options_init_dentry(tr);
a8259075
SR
5329 if (!t_options)
5330 return;
5331
5452af66 5332 for (i = 0; trace_options[i]; i++)
2b6080f2 5333 create_trace_option_core_file(tr, trace_options[i], i);
a8259075
SR
5334}
5335
499e5470
SR
5336static ssize_t
5337rb_simple_read(struct file *filp, char __user *ubuf,
5338 size_t cnt, loff_t *ppos)
5339{
348f0fc2 5340 struct trace_array *tr = filp->private_data;
12883efb 5341 struct ring_buffer *buffer = tr->trace_buffer.buffer;
499e5470
SR
5342 char buf[64];
5343 int r;
5344
5345 if (buffer)
5346 r = ring_buffer_record_is_on(buffer);
5347 else
5348 r = 0;
5349
5350 r = sprintf(buf, "%d\n", r);
5351
5352 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
5353}
5354
5355static ssize_t
5356rb_simple_write(struct file *filp, const char __user *ubuf,
5357 size_t cnt, loff_t *ppos)
5358{
348f0fc2 5359 struct trace_array *tr = filp->private_data;
12883efb 5360 struct ring_buffer *buffer = tr->trace_buffer.buffer;
499e5470
SR
5361 unsigned long val;
5362 int ret;
5363
5364 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
5365 if (ret)
5366 return ret;
5367
5368 if (buffer) {
2df8f8a6
SR
5369 mutex_lock(&trace_types_lock);
5370 if (val) {
499e5470 5371 ring_buffer_record_on(buffer);
2b6080f2
SR
5372 if (tr->current_trace->start)
5373 tr->current_trace->start(tr);
2df8f8a6 5374 } else {
499e5470 5375 ring_buffer_record_off(buffer);
2b6080f2
SR
5376 if (tr->current_trace->stop)
5377 tr->current_trace->stop(tr);
2df8f8a6
SR
5378 }
5379 mutex_unlock(&trace_types_lock);
499e5470
SR
5380 }
5381
5382 (*ppos)++;
5383
5384 return cnt;
5385}
5386
5387static const struct file_operations rb_simple_fops = {
5388 .open = tracing_open_generic,
5389 .read = rb_simple_read,
5390 .write = rb_simple_write,
5391 .llseek = default_llseek,
5392};
5393
277ba044
SR
5394struct dentry *trace_instance_dir;
5395
5396static void
5397init_tracer_debugfs(struct trace_array *tr, struct dentry *d_tracer);
5398
737223fb
SRRH
5399static void init_trace_buffers(struct trace_array *tr, struct trace_buffer *buf)
5400{
5401 int cpu;
5402
5403 for_each_tracing_cpu(cpu) {
5404 memset(per_cpu_ptr(buf->data, cpu), 0, sizeof(struct trace_array_cpu));
5405 per_cpu_ptr(buf->data, cpu)->trace_cpu.cpu = cpu;
5406 per_cpu_ptr(buf->data, cpu)->trace_cpu.tr = tr;
5407 }
5408}
5409
55034cd6
SRRH
5410static int
5411allocate_trace_buffer(struct trace_array *tr, struct trace_buffer *buf, int size)
277ba044
SR
5412{
5413 enum ring_buffer_flags rb_flags;
737223fb
SRRH
5414
5415 rb_flags = trace_flags & TRACE_ITER_OVERWRITE ? RB_FL_OVERWRITE : 0;
5416
55034cd6
SRRH
5417 buf->buffer = ring_buffer_alloc(size, rb_flags);
5418 if (!buf->buffer)
5419 return -ENOMEM;
737223fb 5420
55034cd6
SRRH
5421 buf->data = alloc_percpu(struct trace_array_cpu);
5422 if (!buf->data) {
5423 ring_buffer_free(buf->buffer);
5424 return -ENOMEM;
5425 }
737223fb 5426
55034cd6 5427 init_trace_buffers(tr, buf);
737223fb
SRRH
5428
5429 /* Allocate the first page for all buffers */
5430 set_buffer_entries(&tr->trace_buffer,
5431 ring_buffer_size(tr->trace_buffer.buffer, 0));
5432
55034cd6
SRRH
5433 return 0;
5434}
737223fb 5435
55034cd6
SRRH
5436static int allocate_trace_buffers(struct trace_array *tr, int size)
5437{
5438 int ret;
737223fb 5439
55034cd6
SRRH
5440 ret = allocate_trace_buffer(tr, &tr->trace_buffer, size);
5441 if (ret)
5442 return ret;
737223fb 5443
55034cd6
SRRH
5444#ifdef CONFIG_TRACER_MAX_TRACE
5445 ret = allocate_trace_buffer(tr, &tr->max_buffer,
5446 allocate_snapshot ? size : 1);
5447 if (WARN_ON(ret)) {
737223fb 5448 ring_buffer_free(tr->trace_buffer.buffer);
55034cd6
SRRH
5449 free_percpu(tr->trace_buffer.data);
5450 return -ENOMEM;
5451 }
5452 tr->allocated_snapshot = allocate_snapshot;
737223fb 5453
55034cd6
SRRH
5454 /*
5455 * Only the top level trace array gets its snapshot allocated
5456 * from the kernel command line.
5457 */
5458 allocate_snapshot = false;
737223fb 5459#endif
55034cd6 5460 return 0;
737223fb
SRRH
5461}
5462
5463static int new_instance_create(const char *name)
5464{
277ba044
SR
5465 struct trace_array *tr;
5466 int ret;
277ba044
SR
5467
5468 mutex_lock(&trace_types_lock);
5469
5470 ret = -EEXIST;
5471 list_for_each_entry(tr, &ftrace_trace_arrays, list) {
5472 if (tr->name && strcmp(tr->name, name) == 0)
5473 goto out_unlock;
5474 }
5475
5476 ret = -ENOMEM;
5477 tr = kzalloc(sizeof(*tr), GFP_KERNEL);
5478 if (!tr)
5479 goto out_unlock;
5480
5481 tr->name = kstrdup(name, GFP_KERNEL);
5482 if (!tr->name)
5483 goto out_free_tr;
5484
5485 raw_spin_lock_init(&tr->start_lock);
5486
5487 tr->current_trace = &nop_trace;
5488
5489 INIT_LIST_HEAD(&tr->systems);
5490 INIT_LIST_HEAD(&tr->events);
5491
737223fb 5492 if (allocate_trace_buffers(tr, trace_buf_size) < 0)
277ba044
SR
5493 goto out_free_tr;
5494
277ba044
SR
5495 /* Holder for file callbacks */
5496 tr->trace_cpu.cpu = RING_BUFFER_ALL_CPUS;
5497 tr->trace_cpu.tr = tr;
5498
5499 tr->dir = debugfs_create_dir(name, trace_instance_dir);
5500 if (!tr->dir)
5501 goto out_free_tr;
5502
5503 ret = event_trace_add_tracer(tr->dir, tr);
5504 if (ret)
5505 goto out_free_tr;
5506
5507 init_tracer_debugfs(tr, tr->dir);
5508
5509 list_add(&tr->list, &ftrace_trace_arrays);
5510
5511 mutex_unlock(&trace_types_lock);
5512
5513 return 0;
5514
5515 out_free_tr:
12883efb
SRRH
5516 if (tr->trace_buffer.buffer)
5517 ring_buffer_free(tr->trace_buffer.buffer);
277ba044
SR
5518 kfree(tr->name);
5519 kfree(tr);
5520
5521 out_unlock:
5522 mutex_unlock(&trace_types_lock);
5523
5524 return ret;
5525
5526}
5527
0c8916c3
SR
5528static int instance_delete(const char *name)
5529{
5530 struct trace_array *tr;
5531 int found = 0;
5532 int ret;
5533
5534 mutex_lock(&trace_types_lock);
5535
5536 ret = -ENODEV;
5537 list_for_each_entry(tr, &ftrace_trace_arrays, list) {
5538 if (tr->name && strcmp(tr->name, name) == 0) {
5539 found = 1;
5540 break;
5541 }
5542 }
5543 if (!found)
5544 goto out_unlock;
5545
a695cb58
SRRH
5546 ret = -EBUSY;
5547 if (tr->ref)
5548 goto out_unlock;
5549
0c8916c3
SR
5550 list_del(&tr->list);
5551
5552 event_trace_del_tracer(tr);
5553 debugfs_remove_recursive(tr->dir);
12883efb
SRRH
5554 free_percpu(tr->trace_buffer.data);
5555 ring_buffer_free(tr->trace_buffer.buffer);
0c8916c3
SR
5556
5557 kfree(tr->name);
5558 kfree(tr);
5559
5560 ret = 0;
5561
5562 out_unlock:
5563 mutex_unlock(&trace_types_lock);
5564
5565 return ret;
5566}
5567
277ba044
SR
5568static int instance_mkdir (struct inode *inode, struct dentry *dentry, umode_t mode)
5569{
5570 struct dentry *parent;
5571 int ret;
5572
5573 /* Paranoid: Make sure the parent is the "instances" directory */
5574 parent = hlist_entry(inode->i_dentry.first, struct dentry, d_alias);
5575 if (WARN_ON_ONCE(parent != trace_instance_dir))
5576 return -ENOENT;
5577
5578 /*
5579 * The inode mutex is locked, but debugfs_create_dir() will also
5580 * take the mutex. As the instances directory can not be destroyed
5581 * or changed in any other way, it is safe to unlock it, and
5582 * let the dentry try. If two users try to make the same dir at
5583 * the same time, then the new_instance_create() will determine the
5584 * winner.
5585 */
5586 mutex_unlock(&inode->i_mutex);
5587
5588 ret = new_instance_create(dentry->d_iname);
5589
5590 mutex_lock(&inode->i_mutex);
5591
5592 return ret;
5593}
5594
0c8916c3
SR
5595static int instance_rmdir(struct inode *inode, struct dentry *dentry)
5596{
5597 struct dentry *parent;
5598 int ret;
5599
5600 /* Paranoid: Make sure the parent is the "instances" directory */
5601 parent = hlist_entry(inode->i_dentry.first, struct dentry, d_alias);
5602 if (WARN_ON_ONCE(parent != trace_instance_dir))
5603 return -ENOENT;
5604
5605 /* The caller did a dget() on dentry */
5606 mutex_unlock(&dentry->d_inode->i_mutex);
5607
5608 /*
5609 * The inode mutex is locked, but debugfs_create_dir() will also
5610 * take the mutex. As the instances directory can not be destroyed
5611 * or changed in any other way, it is safe to unlock it, and
5612 * let the dentry try. If two users try to make the same dir at
5613 * the same time, then the instance_delete() will determine the
5614 * winner.
5615 */
5616 mutex_unlock(&inode->i_mutex);
5617
5618 ret = instance_delete(dentry->d_iname);
5619
5620 mutex_lock_nested(&inode->i_mutex, I_MUTEX_PARENT);
5621 mutex_lock(&dentry->d_inode->i_mutex);
5622
5623 return ret;
5624}
5625
277ba044
SR
5626static const struct inode_operations instance_dir_inode_operations = {
5627 .lookup = simple_lookup,
5628 .mkdir = instance_mkdir,
0c8916c3 5629 .rmdir = instance_rmdir,
277ba044
SR
5630};
5631
5632static __init void create_trace_instances(struct dentry *d_tracer)
5633{
5634 trace_instance_dir = debugfs_create_dir("instances", d_tracer);
5635 if (WARN_ON(!trace_instance_dir))
5636 return;
5637
5638 /* Hijack the dir inode operations, to allow mkdir */
5639 trace_instance_dir->d_inode->i_op = &instance_dir_inode_operations;
5640}
5641
2b6080f2
SR
5642static void
5643init_tracer_debugfs(struct trace_array *tr, struct dentry *d_tracer)
5644{
121aaee7 5645 int cpu;
2b6080f2
SR
5646
5647 trace_create_file("trace_options", 0644, d_tracer,
5648 tr, &tracing_iter_fops);
5649
5650 trace_create_file("trace", 0644, d_tracer,
5651 (void *)&tr->trace_cpu, &tracing_fops);
5652
5653 trace_create_file("trace_pipe", 0444, d_tracer,
5654 (void *)&tr->trace_cpu, &tracing_pipe_fops);
5655
5656 trace_create_file("buffer_size_kb", 0644, d_tracer,
5657 (void *)&tr->trace_cpu, &tracing_entries_fops);
5658
5659 trace_create_file("buffer_total_size_kb", 0444, d_tracer,
5660 tr, &tracing_total_entries_fops);
5661
5662 trace_create_file("free_buffer", 0644, d_tracer,
5663 tr, &tracing_free_buffer_fops);
5664
5665 trace_create_file("trace_marker", 0220, d_tracer,
5666 tr, &tracing_mark_fops);
5667
5668 trace_create_file("trace_clock", 0644, d_tracer, tr,
5669 &trace_clock_fops);
5670
5671 trace_create_file("tracing_on", 0644, d_tracer,
5672 tr, &rb_simple_fops);
ce9bae55
SRRH
5673
5674#ifdef CONFIG_TRACER_SNAPSHOT
5675 trace_create_file("snapshot", 0644, d_tracer,
5676 (void *)&tr->trace_cpu, &snapshot_fops);
5677#endif
121aaee7
SRRH
5678
5679 for_each_tracing_cpu(cpu)
5680 tracing_init_debugfs_percpu(tr, cpu);
5681
2b6080f2
SR
5682}
5683
b5ad384e 5684static __init int tracer_init_debugfs(void)
bc0c38d1
SR
5685{
5686 struct dentry *d_tracer;
bc0c38d1 5687
7e53bd42
LJ
5688 trace_access_lock_init();
5689
bc0c38d1
SR
5690 d_tracer = tracing_init_dentry();
5691
2b6080f2 5692 init_tracer_debugfs(&global_trace, d_tracer);
bc0c38d1 5693
5452af66 5694 trace_create_file("tracing_cpumask", 0644, d_tracer,
2b6080f2 5695 &global_trace, &tracing_cpumask_fops);
a8259075 5696
5452af66
FW
5697 trace_create_file("available_tracers", 0444, d_tracer,
5698 &global_trace, &show_traces_fops);
5699
339ae5d3 5700 trace_create_file("current_tracer", 0644, d_tracer,
5452af66
FW
5701 &global_trace, &set_tracer_fops);
5702
5d4a9dba 5703#ifdef CONFIG_TRACER_MAX_TRACE
5452af66
FW
5704 trace_create_file("tracing_max_latency", 0644, d_tracer,
5705 &tracing_max_latency, &tracing_max_lat_fops);
0e950173 5706#endif
5452af66
FW
5707
5708 trace_create_file("tracing_thresh", 0644, d_tracer,
5709 &tracing_thresh, &tracing_max_lat_fops);
a8259075 5710
339ae5d3 5711 trace_create_file("README", 0444, d_tracer,
5452af66
FW
5712 NULL, &tracing_readme_fops);
5713
69abe6a5
AP
5714 trace_create_file("saved_cmdlines", 0444, d_tracer,
5715 NULL, &tracing_saved_cmdlines_fops);
5bf9a1ee 5716
bc0c38d1 5717#ifdef CONFIG_DYNAMIC_FTRACE
5452af66
FW
5718 trace_create_file("dyn_ftrace_total_info", 0444, d_tracer,
5719 &ftrace_update_tot_cnt, &tracing_dyn_info_fops);
bc0c38d1 5720#endif
b04cc6b1 5721
277ba044
SR
5722 create_trace_instances(d_tracer);
5723
2b6080f2 5724 create_trace_options_dir(&global_trace);
5452af66 5725
b5ad384e 5726 return 0;
bc0c38d1
SR
5727}
5728
3f5a54e3
SR
5729static int trace_panic_handler(struct notifier_block *this,
5730 unsigned long event, void *unused)
5731{
944ac425 5732 if (ftrace_dump_on_oops)
cecbca96 5733 ftrace_dump(ftrace_dump_on_oops);
3f5a54e3
SR
5734 return NOTIFY_OK;
5735}
5736
5737static struct notifier_block trace_panic_notifier = {
5738 .notifier_call = trace_panic_handler,
5739 .next = NULL,
5740 .priority = 150 /* priority: INT_MAX >= x >= 0 */
5741};
5742
5743static int trace_die_handler(struct notifier_block *self,
5744 unsigned long val,
5745 void *data)
5746{
5747 switch (val) {
5748 case DIE_OOPS:
944ac425 5749 if (ftrace_dump_on_oops)
cecbca96 5750 ftrace_dump(ftrace_dump_on_oops);
3f5a54e3
SR
5751 break;
5752 default:
5753 break;
5754 }
5755 return NOTIFY_OK;
5756}
5757
5758static struct notifier_block trace_die_notifier = {
5759 .notifier_call = trace_die_handler,
5760 .priority = 200
5761};
5762
5763/*
5764 * printk is set to max of 1024, we really don't need it that big.
5765 * Nothing should be printing 1000 characters anyway.
5766 */
5767#define TRACE_MAX_PRINT 1000
5768
5769/*
5770 * Define here KERN_TRACE so that we have one place to modify
5771 * it if we decide to change what log level the ftrace dump
5772 * should be at.
5773 */
428aee14 5774#define KERN_TRACE KERN_EMERG
3f5a54e3 5775
955b61e5 5776void
3f5a54e3
SR
5777trace_printk_seq(struct trace_seq *s)
5778{
5779 /* Probably should print a warning here. */
5780 if (s->len >= 1000)
5781 s->len = 1000;
5782
5783 /* should be zero ended, but we are paranoid. */
5784 s->buffer[s->len] = 0;
5785
5786 printk(KERN_TRACE "%s", s->buffer);
5787
f9520750 5788 trace_seq_init(s);
3f5a54e3
SR
5789}
5790
955b61e5
JW
5791void trace_init_global_iter(struct trace_iterator *iter)
5792{
5793 iter->tr = &global_trace;
2b6080f2 5794 iter->trace = iter->tr->current_trace;
ae3b5093 5795 iter->cpu_file = RING_BUFFER_ALL_CPUS;
12883efb 5796 iter->trace_buffer = &global_trace.trace_buffer;
955b61e5
JW
5797}
5798
cecbca96
FW
5799static void
5800__ftrace_dump(bool disable_tracing, enum ftrace_dump_mode oops_dump_mode)
3f5a54e3 5801{
445c8951 5802 static arch_spinlock_t ftrace_dump_lock =
edc35bd7 5803 (arch_spinlock_t)__ARCH_SPIN_LOCK_UNLOCKED;
3f5a54e3
SR
5804 /* use static because iter can be a bit big for the stack */
5805 static struct trace_iterator iter;
cf586b61 5806 unsigned int old_userobj;
3f5a54e3 5807 static int dump_ran;
d769041f
SR
5808 unsigned long flags;
5809 int cnt = 0, cpu;
3f5a54e3
SR
5810
5811 /* only one dump */
cd891ae0 5812 local_irq_save(flags);
0199c4e6 5813 arch_spin_lock(&ftrace_dump_lock);
3f5a54e3
SR
5814 if (dump_ran)
5815 goto out;
5816
5817 dump_ran = 1;
5818
0ee6b6cf 5819 tracing_off();
cf586b61 5820
e0a413f6
SR
5821 /* Did function tracer already get disabled? */
5822 if (ftrace_is_dead()) {
5823 printk("# WARNING: FUNCTION TRACING IS CORRUPTED\n");
5824 printk("# MAY BE MISSING FUNCTION EVENTS\n");
5825 }
5826
cf586b61
FW
5827 if (disable_tracing)
5828 ftrace_kill();
3f5a54e3 5829
38dbe0b1 5830 /* Simulate the iterator */
955b61e5
JW
5831 trace_init_global_iter(&iter);
5832
d769041f 5833 for_each_tracing_cpu(cpu) {
12883efb 5834 atomic_inc(&per_cpu_ptr(iter.tr->trace_buffer.data, cpu)->disabled);
d769041f
SR
5835 }
5836
cf586b61
FW
5837 old_userobj = trace_flags & TRACE_ITER_SYM_USEROBJ;
5838
b54d3de9
TE
5839 /* don't look at user memory in panic mode */
5840 trace_flags &= ~TRACE_ITER_SYM_USEROBJ;
5841
cecbca96
FW
5842 switch (oops_dump_mode) {
5843 case DUMP_ALL:
ae3b5093 5844 iter.cpu_file = RING_BUFFER_ALL_CPUS;
cecbca96
FW
5845 break;
5846 case DUMP_ORIG:
5847 iter.cpu_file = raw_smp_processor_id();
5848 break;
5849 case DUMP_NONE:
5850 goto out_enable;
5851 default:
5852 printk(KERN_TRACE "Bad dumping mode, switching to all CPUs dump\n");
ae3b5093 5853 iter.cpu_file = RING_BUFFER_ALL_CPUS;
cecbca96
FW
5854 }
5855
5856 printk(KERN_TRACE "Dumping ftrace buffer:\n");
3f5a54e3
SR
5857
5858 /*
5859 * We need to stop all tracing on all CPUS to read the
5860 * the next buffer. This is a bit expensive, but is
5861 * not done often. We fill all what we can read,
5862 * and then release the locks again.
5863 */
5864
3f5a54e3
SR
5865 while (!trace_empty(&iter)) {
5866
5867 if (!cnt)
5868 printk(KERN_TRACE "---------------------------------\n");
5869
5870 cnt++;
5871
5872 /* reset all but tr, trace, and overruns */
5873 memset(&iter.seq, 0,
5874 sizeof(struct trace_iterator) -
5875 offsetof(struct trace_iterator, seq));
5876 iter.iter_flags |= TRACE_FILE_LAT_FMT;
5877 iter.pos = -1;
5878
955b61e5 5879 if (trace_find_next_entry_inc(&iter) != NULL) {
74e7ff8c
LJ
5880 int ret;
5881
5882 ret = print_trace_line(&iter);
5883 if (ret != TRACE_TYPE_NO_CONSUME)
5884 trace_consume(&iter);
3f5a54e3 5885 }
b892e5c8 5886 touch_nmi_watchdog();
3f5a54e3
SR
5887
5888 trace_printk_seq(&iter.seq);
5889 }
5890
5891 if (!cnt)
5892 printk(KERN_TRACE " (ftrace buffer empty)\n");
5893 else
5894 printk(KERN_TRACE "---------------------------------\n");
5895
cecbca96 5896 out_enable:
cf586b61
FW
5897 /* Re-enable tracing if requested */
5898 if (!disable_tracing) {
5899 trace_flags |= old_userobj;
5900
5901 for_each_tracing_cpu(cpu) {
12883efb 5902 atomic_dec(&per_cpu_ptr(iter.trace_buffer->data, cpu)->disabled);
cf586b61
FW
5903 }
5904 tracing_on();
5905 }
5906
3f5a54e3 5907 out:
0199c4e6 5908 arch_spin_unlock(&ftrace_dump_lock);
cd891ae0 5909 local_irq_restore(flags);
3f5a54e3
SR
5910}
5911
cf586b61 5912/* By default: disable tracing after the dump */
cecbca96 5913void ftrace_dump(enum ftrace_dump_mode oops_dump_mode)
cf586b61 5914{
cecbca96 5915 __ftrace_dump(true, oops_dump_mode);
cf586b61 5916}
a8eecf22 5917EXPORT_SYMBOL_GPL(ftrace_dump);
cf586b61 5918
3928a8a2 5919__init static int tracer_alloc_buffers(void)
bc0c38d1 5920{
73c5162a 5921 int ring_buf_size;
9e01c1b7 5922 int ret = -ENOMEM;
4c11d7ae 5923
750912fa 5924
9e01c1b7
RR
5925 if (!alloc_cpumask_var(&tracing_buffer_mask, GFP_KERNEL))
5926 goto out;
5927
5928 if (!alloc_cpumask_var(&tracing_cpumask, GFP_KERNEL))
5929 goto out_free_buffer_mask;
4c11d7ae 5930
07d777fe
SR
5931 /* Only allocate trace_printk buffers if a trace_printk exists */
5932 if (__stop___trace_bprintk_fmt != __start___trace_bprintk_fmt)
81698831 5933 /* Must be called before global_trace.buffer is allocated */
07d777fe
SR
5934 trace_printk_init_buffers();
5935
73c5162a
SR
5936 /* To save memory, keep the ring buffer size to its minimum */
5937 if (ring_buffer_expanded)
5938 ring_buf_size = trace_buf_size;
5939 else
5940 ring_buf_size = 1;
5941
9e01c1b7
RR
5942 cpumask_copy(tracing_buffer_mask, cpu_possible_mask);
5943 cpumask_copy(tracing_cpumask, cpu_all_mask);
5944
2b6080f2
SR
5945 raw_spin_lock_init(&global_trace.start_lock);
5946
9e01c1b7 5947 /* TODO: make the number of buffers hot pluggable with CPUS */
737223fb 5948 if (allocate_trace_buffers(&global_trace, ring_buf_size) < 0) {
3928a8a2
SR
5949 printk(KERN_ERR "tracer: failed to allocate ring buffer!\n");
5950 WARN_ON(1);
9e01c1b7 5951 goto out_free_cpumask;
4c11d7ae 5952 }
a7603ff4 5953
499e5470
SR
5954 if (global_trace.buffer_disabled)
5955 tracing_off();
4c11d7ae 5956
bc0c38d1
SR
5957 trace_init_cmdlines();
5958
43a15386 5959 register_tracer(&nop_trace);
d840f718 5960
2b6080f2
SR
5961 global_trace.current_trace = &nop_trace;
5962
60a11774
SR
5963 /* All seems OK, enable tracing */
5964 tracing_disabled = 0;
3928a8a2 5965
3f5a54e3
SR
5966 atomic_notifier_chain_register(&panic_notifier_list,
5967 &trace_panic_notifier);
5968
5969 register_die_notifier(&trace_die_notifier);
2fc1dfbe 5970
ae63b31e
SR
5971 global_trace.flags = TRACE_ARRAY_FL_GLOBAL;
5972
2b6080f2
SR
5973 /* Holder for file callbacks */
5974 global_trace.trace_cpu.cpu = RING_BUFFER_ALL_CPUS;
5975 global_trace.trace_cpu.tr = &global_trace;
5976
ae63b31e
SR
5977 INIT_LIST_HEAD(&global_trace.systems);
5978 INIT_LIST_HEAD(&global_trace.events);
5979 list_add(&global_trace.list, &ftrace_trace_arrays);
5980
7bcfaf54
SR
5981 while (trace_boot_options) {
5982 char *option;
5983
5984 option = strsep(&trace_boot_options, ",");
2b6080f2 5985 trace_set_options(&global_trace, option);
7bcfaf54
SR
5986 }
5987
2fc1dfbe 5988 return 0;
3f5a54e3 5989
9e01c1b7 5990out_free_cpumask:
12883efb
SRRH
5991 free_percpu(global_trace.trace_buffer.data);
5992#ifdef CONFIG_TRACER_MAX_TRACE
5993 free_percpu(global_trace.max_buffer.data);
5994#endif
9e01c1b7
RR
5995 free_cpumask_var(tracing_cpumask);
5996out_free_buffer_mask:
5997 free_cpumask_var(tracing_buffer_mask);
5998out:
5999 return ret;
bc0c38d1 6000}
b2821ae6
SR
6001
6002__init static int clear_boot_tracer(void)
6003{
6004 /*
6005 * The default tracer at boot buffer is an init section.
6006 * This function is called in lateinit. If we did not
6007 * find the boot tracer, then clear it out, to prevent
6008 * later registration from accessing the buffer that is
6009 * about to be freed.
6010 */
6011 if (!default_bootup_tracer)
6012 return 0;
6013
6014 printk(KERN_INFO "ftrace bootup tracer '%s' not registered.\n",
6015 default_bootup_tracer);
6016 default_bootup_tracer = NULL;
6017
6018 return 0;
6019}
6020
b5ad384e
FW
6021early_initcall(tracer_alloc_buffers);
6022fs_initcall(tracer_init_debugfs);
b2821ae6 6023late_initcall(clear_boot_tracer);