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