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