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