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