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