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