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