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