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