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