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