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