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