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