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