tracing: Fix polling on trace_pipe_raw
[firefly-linux-kernel-4.4.55.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 trace_poll(struct trace_iterator *iter, struct file *filp, poll_table *poll_table)
3559 {
3560         if (trace_flags & TRACE_ITER_BLOCK) {
3561                 /*
3562                  * Always select as readable when in blocking mode
3563                  */
3564                 return POLLIN | POLLRDNORM;
3565         } else {
3566                 if (!trace_empty(iter))
3567                         return POLLIN | POLLRDNORM;
3568                 trace_wakeup_needed = true;
3569                 poll_wait(filp, &trace_wait, poll_table);
3570                 if (!trace_empty(iter))
3571                         return POLLIN | POLLRDNORM;
3572
3573                 return 0;
3574         }
3575 }
3576
3577 static unsigned int
3578 tracing_poll_pipe(struct file *filp, poll_table *poll_table)
3579 {
3580         struct trace_iterator *iter = filp->private_data;
3581
3582         return trace_poll(iter, filp, poll_table);
3583 }
3584
3585 /*
3586  * This is a make-shift waitqueue.
3587  * A tracer might use this callback on some rare cases:
3588  *
3589  *  1) the current tracer might hold the runqueue lock when it wakes up
3590  *     a reader, hence a deadlock (sched, function, and function graph tracers)
3591  *  2) the function tracers, trace all functions, we don't want
3592  *     the overhead of calling wake_up and friends
3593  *     (and tracing them too)
3594  *
3595  *     Anyway, this is really very primitive wakeup.
3596  */
3597 void poll_wait_pipe(struct trace_iterator *iter)
3598 {
3599         set_current_state(TASK_INTERRUPTIBLE);
3600         /* sleep for 100 msecs, and try again. */
3601         schedule_timeout(HZ / 10);
3602 }
3603
3604 /* Must be called with trace_types_lock mutex held. */
3605 static int tracing_wait_pipe(struct file *filp)
3606 {
3607         struct trace_iterator *iter = filp->private_data;
3608
3609         while (trace_empty(iter)) {
3610
3611                 if ((filp->f_flags & O_NONBLOCK)) {
3612                         return -EAGAIN;
3613                 }
3614
3615                 mutex_unlock(&iter->mutex);
3616
3617                 iter->trace->wait_pipe(iter);
3618
3619                 mutex_lock(&iter->mutex);
3620
3621                 if (signal_pending(current))
3622                         return -EINTR;
3623
3624                 /*
3625                  * We block until we read something and tracing is disabled.
3626                  * We still block if tracing is disabled, but we have never
3627                  * read anything. This allows a user to cat this file, and
3628                  * then enable tracing. But after we have read something,
3629                  * we give an EOF when tracing is again disabled.
3630                  *
3631                  * iter->pos will be 0 if we haven't read anything.
3632                  */
3633                 if (!tracing_is_enabled() && iter->pos)
3634                         break;
3635         }
3636
3637         return 1;
3638 }
3639
3640 /*
3641  * Consumer reader.
3642  */
3643 static ssize_t
3644 tracing_read_pipe(struct file *filp, char __user *ubuf,
3645                   size_t cnt, loff_t *ppos)
3646 {
3647         struct trace_iterator *iter = filp->private_data;
3648         struct trace_array *tr = iter->tr;
3649         ssize_t sret;
3650
3651         /* return any leftover data */
3652         sret = trace_seq_to_user(&iter->seq, ubuf, cnt);
3653         if (sret != -EBUSY)
3654                 return sret;
3655
3656         trace_seq_init(&iter->seq);
3657
3658         /* copy the tracer to avoid using a global lock all around */
3659         mutex_lock(&trace_types_lock);
3660         if (unlikely(iter->trace->name != tr->current_trace->name))
3661                 *iter->trace = *tr->current_trace;
3662         mutex_unlock(&trace_types_lock);
3663
3664         /*
3665          * Avoid more than one consumer on a single file descriptor
3666          * This is just a matter of traces coherency, the ring buffer itself
3667          * is protected.
3668          */
3669         mutex_lock(&iter->mutex);
3670         if (iter->trace->read) {
3671                 sret = iter->trace->read(iter, filp, ubuf, cnt, ppos);
3672                 if (sret)
3673                         goto out;
3674         }
3675
3676 waitagain:
3677         sret = tracing_wait_pipe(filp);
3678         if (sret <= 0)
3679                 goto out;
3680
3681         /* stop when tracing is finished */
3682         if (trace_empty(iter)) {
3683                 sret = 0;
3684                 goto out;
3685         }
3686
3687         if (cnt >= PAGE_SIZE)
3688                 cnt = PAGE_SIZE - 1;
3689
3690         /* reset all but tr, trace, and overruns */
3691         memset(&iter->seq, 0,
3692                sizeof(struct trace_iterator) -
3693                offsetof(struct trace_iterator, seq));
3694         iter->pos = -1;
3695
3696         trace_event_read_lock();
3697         trace_access_lock(iter->cpu_file);
3698         while (trace_find_next_entry_inc(iter) != NULL) {
3699                 enum print_line_t ret;
3700                 int len = iter->seq.len;
3701
3702                 ret = print_trace_line(iter);
3703                 if (ret == TRACE_TYPE_PARTIAL_LINE) {
3704                         /* don't print partial lines */
3705                         iter->seq.len = len;
3706                         break;
3707                 }
3708                 if (ret != TRACE_TYPE_NO_CONSUME)
3709                         trace_consume(iter);
3710
3711                 if (iter->seq.len >= cnt)
3712                         break;
3713
3714                 /*
3715                  * Setting the full flag means we reached the trace_seq buffer
3716                  * size and we should leave by partial output condition above.
3717                  * One of the trace_seq_* functions is not used properly.
3718                  */
3719                 WARN_ONCE(iter->seq.full, "full flag set for trace type %d",
3720                           iter->ent->type);
3721         }
3722         trace_access_unlock(iter->cpu_file);
3723         trace_event_read_unlock();
3724
3725         /* Now copy what we have to the user */
3726         sret = trace_seq_to_user(&iter->seq, ubuf, cnt);
3727         if (iter->seq.readpos >= iter->seq.len)
3728                 trace_seq_init(&iter->seq);
3729
3730         /*
3731          * If there was nothing to send to user, in spite of consuming trace
3732          * entries, go back to wait for more entries.
3733          */
3734         if (sret == -EBUSY)
3735                 goto waitagain;
3736
3737 out:
3738         mutex_unlock(&iter->mutex);
3739
3740         return sret;
3741 }
3742
3743 static void tracing_pipe_buf_release(struct pipe_inode_info *pipe,
3744                                      struct pipe_buffer *buf)
3745 {
3746         __free_page(buf->page);
3747 }
3748
3749 static void tracing_spd_release_pipe(struct splice_pipe_desc *spd,
3750                                      unsigned int idx)
3751 {
3752         __free_page(spd->pages[idx]);
3753 }
3754
3755 static const struct pipe_buf_operations tracing_pipe_buf_ops = {
3756         .can_merge              = 0,
3757         .map                    = generic_pipe_buf_map,
3758         .unmap                  = generic_pipe_buf_unmap,
3759         .confirm                = generic_pipe_buf_confirm,
3760         .release                = tracing_pipe_buf_release,
3761         .steal                  = generic_pipe_buf_steal,
3762         .get                    = generic_pipe_buf_get,
3763 };
3764
3765 static size_t
3766 tracing_fill_pipe_page(size_t rem, struct trace_iterator *iter)
3767 {
3768         size_t count;
3769         int ret;
3770
3771         /* Seq buffer is page-sized, exactly what we need. */
3772         for (;;) {
3773                 count = iter->seq.len;
3774                 ret = print_trace_line(iter);
3775                 count = iter->seq.len - count;
3776                 if (rem < count) {
3777                         rem = 0;
3778                         iter->seq.len -= count;
3779                         break;
3780                 }
3781                 if (ret == TRACE_TYPE_PARTIAL_LINE) {
3782                         iter->seq.len -= count;
3783                         break;
3784                 }
3785
3786                 if (ret != TRACE_TYPE_NO_CONSUME)
3787                         trace_consume(iter);
3788                 rem -= count;
3789                 if (!trace_find_next_entry_inc(iter))   {
3790                         rem = 0;
3791                         iter->ent = NULL;
3792                         break;
3793                 }
3794         }
3795
3796         return rem;
3797 }
3798
3799 static ssize_t tracing_splice_read_pipe(struct file *filp,
3800                                         loff_t *ppos,
3801                                         struct pipe_inode_info *pipe,
3802                                         size_t len,
3803                                         unsigned int flags)
3804 {
3805         struct page *pages_def[PIPE_DEF_BUFFERS];
3806         struct partial_page partial_def[PIPE_DEF_BUFFERS];
3807         struct trace_iterator *iter = filp->private_data;
3808         struct splice_pipe_desc spd = {
3809                 .pages          = pages_def,
3810                 .partial        = partial_def,
3811                 .nr_pages       = 0, /* This gets updated below. */
3812                 .nr_pages_max   = PIPE_DEF_BUFFERS,
3813                 .flags          = flags,
3814                 .ops            = &tracing_pipe_buf_ops,
3815                 .spd_release    = tracing_spd_release_pipe,
3816         };
3817         struct trace_array *tr = iter->tr;
3818         ssize_t ret;
3819         size_t rem;
3820         unsigned int i;
3821
3822         if (splice_grow_spd(pipe, &spd))
3823                 return -ENOMEM;
3824
3825         /* copy the tracer to avoid using a global lock all around */
3826         mutex_lock(&trace_types_lock);
3827         if (unlikely(iter->trace->name != tr->current_trace->name))
3828                 *iter->trace = *tr->current_trace;
3829         mutex_unlock(&trace_types_lock);
3830
3831         mutex_lock(&iter->mutex);
3832
3833         if (iter->trace->splice_read) {
3834                 ret = iter->trace->splice_read(iter, filp,
3835                                                ppos, pipe, len, flags);
3836                 if (ret)
3837                         goto out_err;
3838         }
3839
3840         ret = tracing_wait_pipe(filp);
3841         if (ret <= 0)
3842                 goto out_err;
3843
3844         if (!iter->ent && !trace_find_next_entry_inc(iter)) {
3845                 ret = -EFAULT;
3846                 goto out_err;
3847         }
3848
3849         trace_event_read_lock();
3850         trace_access_lock(iter->cpu_file);
3851
3852         /* Fill as many pages as possible. */
3853         for (i = 0, rem = len; i < pipe->buffers && rem; i++) {
3854                 spd.pages[i] = alloc_page(GFP_KERNEL);
3855                 if (!spd.pages[i])
3856                         break;
3857
3858                 rem = tracing_fill_pipe_page(rem, iter);
3859
3860                 /* Copy the data into the page, so we can start over. */
3861                 ret = trace_seq_to_buffer(&iter->seq,
3862                                           page_address(spd.pages[i]),
3863                                           iter->seq.len);
3864                 if (ret < 0) {
3865                         __free_page(spd.pages[i]);
3866                         break;
3867                 }
3868                 spd.partial[i].offset = 0;
3869                 spd.partial[i].len = iter->seq.len;
3870
3871                 trace_seq_init(&iter->seq);
3872         }
3873
3874         trace_access_unlock(iter->cpu_file);
3875         trace_event_read_unlock();
3876         mutex_unlock(&iter->mutex);
3877
3878         spd.nr_pages = i;
3879
3880         ret = splice_to_pipe(pipe, &spd);
3881 out:
3882         splice_shrink_spd(&spd);
3883         return ret;
3884
3885 out_err:
3886         mutex_unlock(&iter->mutex);
3887         goto out;
3888 }
3889
3890 static ssize_t
3891 tracing_entries_read(struct file *filp, char __user *ubuf,
3892                      size_t cnt, loff_t *ppos)
3893 {
3894         struct trace_cpu *tc = filp->private_data;
3895         struct trace_array *tr = tc->tr;
3896         char buf[64];
3897         int r = 0;
3898         ssize_t ret;
3899
3900         mutex_lock(&trace_types_lock);
3901
3902         if (tc->cpu == RING_BUFFER_ALL_CPUS) {
3903                 int cpu, buf_size_same;
3904                 unsigned long size;
3905
3906                 size = 0;
3907                 buf_size_same = 1;
3908                 /* check if all cpu sizes are same */
3909                 for_each_tracing_cpu(cpu) {
3910                         /* fill in the size from first enabled cpu */
3911                         if (size == 0)
3912                                 size = per_cpu_ptr(tr->data, cpu)->entries;
3913                         if (size != per_cpu_ptr(tr->data, cpu)->entries) {
3914                                 buf_size_same = 0;
3915                                 break;
3916                         }
3917                 }
3918
3919                 if (buf_size_same) {
3920                         if (!ring_buffer_expanded)
3921                                 r = sprintf(buf, "%lu (expanded: %lu)\n",
3922                                             size >> 10,
3923                                             trace_buf_size >> 10);
3924                         else
3925                                 r = sprintf(buf, "%lu\n", size >> 10);
3926                 } else
3927                         r = sprintf(buf, "X\n");
3928         } else
3929                 r = sprintf(buf, "%lu\n", per_cpu_ptr(tr->data, tc->cpu)->entries >> 10);
3930
3931         mutex_unlock(&trace_types_lock);
3932
3933         ret = simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
3934         return ret;
3935 }
3936
3937 static ssize_t
3938 tracing_entries_write(struct file *filp, const char __user *ubuf,
3939                       size_t cnt, loff_t *ppos)
3940 {
3941         struct trace_cpu *tc = filp->private_data;
3942         unsigned long val;
3943         int ret;
3944
3945         ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
3946         if (ret)
3947                 return ret;
3948
3949         /* must have at least 1 entry */
3950         if (!val)
3951                 return -EINVAL;
3952
3953         /* value is in KB */
3954         val <<= 10;
3955
3956         ret = tracing_resize_ring_buffer(tc->tr, val, tc->cpu);
3957         if (ret < 0)
3958                 return ret;
3959
3960         *ppos += cnt;
3961
3962         return cnt;
3963 }
3964
3965 static ssize_t
3966 tracing_total_entries_read(struct file *filp, char __user *ubuf,
3967                                 size_t cnt, loff_t *ppos)
3968 {
3969         struct trace_array *tr = filp->private_data;
3970         char buf[64];
3971         int r, cpu;
3972         unsigned long size = 0, expanded_size = 0;
3973
3974         mutex_lock(&trace_types_lock);
3975         for_each_tracing_cpu(cpu) {
3976                 size += per_cpu_ptr(tr->data, cpu)->entries >> 10;
3977                 if (!ring_buffer_expanded)
3978                         expanded_size += trace_buf_size >> 10;
3979         }
3980         if (ring_buffer_expanded)
3981                 r = sprintf(buf, "%lu\n", size);
3982         else
3983                 r = sprintf(buf, "%lu (expanded: %lu)\n", size, expanded_size);
3984         mutex_unlock(&trace_types_lock);
3985
3986         return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
3987 }
3988
3989 static ssize_t
3990 tracing_free_buffer_write(struct file *filp, const char __user *ubuf,
3991                           size_t cnt, loff_t *ppos)
3992 {
3993         /*
3994          * There is no need to read what the user has written, this function
3995          * is just to make sure that there is no error when "echo" is used
3996          */
3997
3998         *ppos += cnt;
3999
4000         return cnt;
4001 }
4002
4003 static int
4004 tracing_free_buffer_release(struct inode *inode, struct file *filp)
4005 {
4006         struct trace_array *tr = inode->i_private;
4007
4008         /* disable tracing ? */
4009         if (trace_flags & TRACE_ITER_STOP_ON_FREE)
4010                 tracing_off();
4011         /* resize the ring buffer to 0 */
4012         tracing_resize_ring_buffer(tr, 0, RING_BUFFER_ALL_CPUS);
4013
4014         return 0;
4015 }
4016
4017 static ssize_t
4018 tracing_mark_write(struct file *filp, const char __user *ubuf,
4019                                         size_t cnt, loff_t *fpos)
4020 {
4021         unsigned long addr = (unsigned long)ubuf;
4022         struct ring_buffer_event *event;
4023         struct ring_buffer *buffer;
4024         struct print_entry *entry;
4025         unsigned long irq_flags;
4026         struct page *pages[2];
4027         void *map_page[2];
4028         int nr_pages = 1;
4029         ssize_t written;
4030         int offset;
4031         int size;
4032         int len;
4033         int ret;
4034         int i;
4035
4036         if (tracing_disabled)
4037                 return -EINVAL;
4038
4039         if (!(trace_flags & TRACE_ITER_MARKERS))
4040                 return -EINVAL;
4041
4042         if (cnt > TRACE_BUF_SIZE)
4043                 cnt = TRACE_BUF_SIZE;
4044
4045         /*
4046          * Userspace is injecting traces into the kernel trace buffer.
4047          * We want to be as non intrusive as possible.
4048          * To do so, we do not want to allocate any special buffers
4049          * or take any locks, but instead write the userspace data
4050          * straight into the ring buffer.
4051          *
4052          * First we need to pin the userspace buffer into memory,
4053          * which, most likely it is, because it just referenced it.
4054          * But there's no guarantee that it is. By using get_user_pages_fast()
4055          * and kmap_atomic/kunmap_atomic() we can get access to the
4056          * pages directly. We then write the data directly into the
4057          * ring buffer.
4058          */
4059         BUILD_BUG_ON(TRACE_BUF_SIZE >= PAGE_SIZE);
4060
4061         /* check if we cross pages */
4062         if ((addr & PAGE_MASK) != ((addr + cnt) & PAGE_MASK))
4063                 nr_pages = 2;
4064
4065         offset = addr & (PAGE_SIZE - 1);
4066         addr &= PAGE_MASK;
4067
4068         ret = get_user_pages_fast(addr, nr_pages, 0, pages);
4069         if (ret < nr_pages) {
4070                 while (--ret >= 0)
4071                         put_page(pages[ret]);
4072                 written = -EFAULT;
4073                 goto out;
4074         }
4075
4076         for (i = 0; i < nr_pages; i++)
4077                 map_page[i] = kmap_atomic(pages[i]);
4078
4079         local_save_flags(irq_flags);
4080         size = sizeof(*entry) + cnt + 2; /* possible \n added */
4081         buffer = global_trace.buffer;
4082         event = trace_buffer_lock_reserve(buffer, TRACE_PRINT, size,
4083                                           irq_flags, preempt_count());
4084         if (!event) {
4085                 /* Ring buffer disabled, return as if not open for write */
4086                 written = -EBADF;
4087                 goto out_unlock;
4088         }
4089
4090         entry = ring_buffer_event_data(event);
4091         entry->ip = _THIS_IP_;
4092
4093         if (nr_pages == 2) {
4094                 len = PAGE_SIZE - offset;
4095                 memcpy(&entry->buf, map_page[0] + offset, len);
4096                 memcpy(&entry->buf[len], map_page[1], cnt - len);
4097         } else
4098                 memcpy(&entry->buf, map_page[0] + offset, cnt);
4099
4100         if (entry->buf[cnt - 1] != '\n') {
4101                 entry->buf[cnt] = '\n';
4102                 entry->buf[cnt + 1] = '\0';
4103         } else
4104                 entry->buf[cnt] = '\0';
4105
4106         __buffer_unlock_commit(buffer, event);
4107
4108         written = cnt;
4109
4110         *fpos += written;
4111
4112  out_unlock:
4113         for (i = 0; i < nr_pages; i++){
4114                 kunmap_atomic(map_page[i]);
4115                 put_page(pages[i]);
4116         }
4117  out:
4118         return written;
4119 }
4120
4121 static int tracing_clock_show(struct seq_file *m, void *v)
4122 {
4123         struct trace_array *tr = m->private;
4124         int i;
4125
4126         for (i = 0; i < ARRAY_SIZE(trace_clocks); i++)
4127                 seq_printf(m,
4128                         "%s%s%s%s", i ? " " : "",
4129                         i == tr->clock_id ? "[" : "", trace_clocks[i].name,
4130                         i == tr->clock_id ? "]" : "");
4131         seq_putc(m, '\n');
4132
4133         return 0;
4134 }
4135
4136 static ssize_t tracing_clock_write(struct file *filp, const char __user *ubuf,
4137                                    size_t cnt, loff_t *fpos)
4138 {
4139         struct seq_file *m = filp->private_data;
4140         struct trace_array *tr = m->private;
4141         char buf[64];
4142         const char *clockstr;
4143         int i;
4144
4145         if (cnt >= sizeof(buf))
4146                 return -EINVAL;
4147
4148         if (copy_from_user(&buf, ubuf, cnt))
4149                 return -EFAULT;
4150
4151         buf[cnt] = 0;
4152
4153         clockstr = strstrip(buf);
4154
4155         for (i = 0; i < ARRAY_SIZE(trace_clocks); i++) {
4156                 if (strcmp(trace_clocks[i].name, clockstr) == 0)
4157                         break;
4158         }
4159         if (i == ARRAY_SIZE(trace_clocks))
4160                 return -EINVAL;
4161
4162         mutex_lock(&trace_types_lock);
4163
4164         tr->clock_id = i;
4165
4166         ring_buffer_set_clock(tr->buffer, trace_clocks[i].func);
4167         if (tr->flags & TRACE_ARRAY_FL_GLOBAL && max_tr.buffer)
4168                 ring_buffer_set_clock(max_tr.buffer, trace_clocks[i].func);
4169
4170         /*
4171          * New clock may not be consistent with the previous clock.
4172          * Reset the buffer so that it doesn't have incomparable timestamps.
4173          */
4174         tracing_reset_online_cpus(&global_trace);
4175         tracing_reset_online_cpus(&max_tr);
4176
4177         mutex_unlock(&trace_types_lock);
4178
4179         *fpos += cnt;
4180
4181         return cnt;
4182 }
4183
4184 static int tracing_clock_open(struct inode *inode, struct file *file)
4185 {
4186         if (tracing_disabled)
4187                 return -ENODEV;
4188
4189         return single_open(file, tracing_clock_show, inode->i_private);
4190 }
4191
4192 #ifdef CONFIG_TRACER_SNAPSHOT
4193 static int tracing_snapshot_open(struct inode *inode, struct file *file)
4194 {
4195         struct trace_cpu *tc = inode->i_private;
4196         struct trace_iterator *iter;
4197         struct seq_file *m;
4198         int ret = 0;
4199
4200         if (file->f_mode & FMODE_READ) {
4201                 iter = __tracing_open(inode, file, true);
4202                 if (IS_ERR(iter))
4203                         ret = PTR_ERR(iter);
4204         } else {
4205                 /* Writes still need the seq_file to hold the private data */
4206                 m = kzalloc(sizeof(*m), GFP_KERNEL);
4207                 if (!m)
4208                         return -ENOMEM;
4209                 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
4210                 if (!iter) {
4211                         kfree(m);
4212                         return -ENOMEM;
4213                 }
4214                 iter->tr = tc->tr;
4215                 m->private = iter;
4216                 file->private_data = m;
4217         }
4218
4219         return ret;
4220 }
4221
4222 static ssize_t
4223 tracing_snapshot_write(struct file *filp, const char __user *ubuf, size_t cnt,
4224                        loff_t *ppos)
4225 {
4226         struct seq_file *m = filp->private_data;
4227         struct trace_iterator *iter = m->private;
4228         struct trace_array *tr = iter->tr;
4229         unsigned long val;
4230         int ret;
4231
4232         ret = tracing_update_buffers();
4233         if (ret < 0)
4234                 return ret;
4235
4236         ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
4237         if (ret)
4238                 return ret;
4239
4240         mutex_lock(&trace_types_lock);
4241
4242         if (tr->current_trace->use_max_tr) {
4243                 ret = -EBUSY;
4244                 goto out;
4245         }
4246
4247         switch (val) {
4248         case 0:
4249                 if (tr->current_trace->allocated_snapshot) {
4250                         /* free spare buffer */
4251                         ring_buffer_resize(max_tr.buffer, 1,
4252                                            RING_BUFFER_ALL_CPUS);
4253                         set_buffer_entries(&max_tr, 1);
4254                         tracing_reset_online_cpus(&max_tr);
4255                         tr->current_trace->allocated_snapshot = false;
4256                 }
4257                 break;
4258         case 1:
4259                 if (!tr->current_trace->allocated_snapshot) {
4260                         /* allocate spare buffer */
4261                         ret = resize_buffer_duplicate_size(&max_tr,
4262                                         &global_trace, RING_BUFFER_ALL_CPUS);
4263                         if (ret < 0)
4264                                 break;
4265                         tr->current_trace->allocated_snapshot = true;
4266                 }
4267
4268                 local_irq_disable();
4269                 /* Now, we're going to swap */
4270                 update_max_tr(&global_trace, current, smp_processor_id());
4271                 local_irq_enable();
4272                 break;
4273         default:
4274                 if (tr->current_trace->allocated_snapshot)
4275                         tracing_reset_online_cpus(&max_tr);
4276                 break;
4277         }
4278
4279         if (ret >= 0) {
4280                 *ppos += cnt;
4281                 ret = cnt;
4282         }
4283 out:
4284         mutex_unlock(&trace_types_lock);
4285         return ret;
4286 }
4287
4288 static int tracing_snapshot_release(struct inode *inode, struct file *file)
4289 {
4290         struct seq_file *m = file->private_data;
4291
4292         if (file->f_mode & FMODE_READ)
4293                 return tracing_release(inode, file);
4294
4295         /* If write only, the seq_file is just a stub */
4296         if (m)
4297                 kfree(m->private);
4298         kfree(m);
4299
4300         return 0;
4301 }
4302
4303 #endif /* CONFIG_TRACER_SNAPSHOT */
4304
4305
4306 static const struct file_operations tracing_max_lat_fops = {
4307         .open           = tracing_open_generic,
4308         .read           = tracing_max_lat_read,
4309         .write          = tracing_max_lat_write,
4310         .llseek         = generic_file_llseek,
4311 };
4312
4313 static const struct file_operations set_tracer_fops = {
4314         .open           = tracing_open_generic,
4315         .read           = tracing_set_trace_read,
4316         .write          = tracing_set_trace_write,
4317         .llseek         = generic_file_llseek,
4318 };
4319
4320 static const struct file_operations tracing_pipe_fops = {
4321         .open           = tracing_open_pipe,
4322         .poll           = tracing_poll_pipe,
4323         .read           = tracing_read_pipe,
4324         .splice_read    = tracing_splice_read_pipe,
4325         .release        = tracing_release_pipe,
4326         .llseek         = no_llseek,
4327 };
4328
4329 static const struct file_operations tracing_entries_fops = {
4330         .open           = tracing_open_generic,
4331         .read           = tracing_entries_read,
4332         .write          = tracing_entries_write,
4333         .llseek         = generic_file_llseek,
4334 };
4335
4336 static const struct file_operations tracing_total_entries_fops = {
4337         .open           = tracing_open_generic,
4338         .read           = tracing_total_entries_read,
4339         .llseek         = generic_file_llseek,
4340 };
4341
4342 static const struct file_operations tracing_free_buffer_fops = {
4343         .write          = tracing_free_buffer_write,
4344         .release        = tracing_free_buffer_release,
4345 };
4346
4347 static const struct file_operations tracing_mark_fops = {
4348         .open           = tracing_open_generic,
4349         .write          = tracing_mark_write,
4350         .llseek         = generic_file_llseek,
4351 };
4352
4353 static const struct file_operations trace_clock_fops = {
4354         .open           = tracing_clock_open,
4355         .read           = seq_read,
4356         .llseek         = seq_lseek,
4357         .release        = single_release,
4358         .write          = tracing_clock_write,
4359 };
4360
4361 #ifdef CONFIG_TRACER_SNAPSHOT
4362 static const struct file_operations snapshot_fops = {
4363         .open           = tracing_snapshot_open,
4364         .read           = seq_read,
4365         .write          = tracing_snapshot_write,
4366         .llseek         = tracing_seek,
4367         .release        = tracing_snapshot_release,
4368 };
4369 #endif /* CONFIG_TRACER_SNAPSHOT */
4370
4371 struct ftrace_buffer_info {
4372         struct trace_iterator   iter;
4373         void                    *spare;
4374         unsigned int            read;
4375 };
4376
4377 static int tracing_buffers_open(struct inode *inode, struct file *filp)
4378 {
4379         struct trace_cpu *tc = inode->i_private;
4380         struct trace_array *tr = tc->tr;
4381         struct ftrace_buffer_info *info;
4382
4383         if (tracing_disabled)
4384                 return -ENODEV;
4385
4386         info = kzalloc(sizeof(*info), GFP_KERNEL);
4387         if (!info)
4388                 return -ENOMEM;
4389
4390         info->iter.tr           = tr;
4391         info->iter.cpu_file     = tc->cpu;
4392         info->spare             = NULL;
4393         /* Force reading ring buffer for first read */
4394         info->read              = (unsigned int)-1;
4395
4396         filp->private_data = info;
4397
4398         return nonseekable_open(inode, filp);
4399 }
4400
4401 static unsigned int
4402 tracing_buffers_poll(struct file *filp, poll_table *poll_table)
4403 {
4404         struct ftrace_buffer_info *info = filp->private_data;
4405         struct trace_iterator *iter = &info->iter;
4406
4407         return trace_poll(iter, filp, poll_table);
4408 }
4409
4410 static ssize_t
4411 tracing_buffers_read(struct file *filp, char __user *ubuf,
4412                      size_t count, loff_t *ppos)
4413 {
4414         struct ftrace_buffer_info *info = filp->private_data;
4415         struct trace_iterator *iter = &info->iter;
4416         ssize_t ret;
4417         size_t size;
4418
4419         if (!count)
4420                 return 0;
4421
4422         if (!info->spare)
4423                 info->spare = ring_buffer_alloc_read_page(iter->tr->buffer, iter->cpu_file);
4424         if (!info->spare)
4425                 return -ENOMEM;
4426
4427         /* Do we have previous read data to read? */
4428         if (info->read < PAGE_SIZE)
4429                 goto read;
4430
4431         trace_access_lock(iter->cpu_file);
4432         ret = ring_buffer_read_page(iter->tr->buffer,
4433                                     &info->spare,
4434                                     count,
4435                                     iter->cpu_file, 0);
4436         trace_access_unlock(iter->cpu_file);
4437         if (ret < 0)
4438                 return 0;
4439
4440         info->read = 0;
4441
4442 read:
4443         size = PAGE_SIZE - info->read;
4444         if (size > count)
4445                 size = count;
4446
4447         ret = copy_to_user(ubuf, info->spare + info->read, size);
4448         if (ret == size)
4449                 return -EFAULT;
4450         size -= ret;
4451
4452         *ppos += size;
4453         info->read += size;
4454
4455         return size;
4456 }
4457
4458 static int tracing_buffers_release(struct inode *inode, struct file *file)
4459 {
4460         struct ftrace_buffer_info *info = file->private_data;
4461         struct trace_iterator *iter = &info->iter;
4462
4463         if (info->spare)
4464                 ring_buffer_free_read_page(iter->tr->buffer, info->spare);
4465         kfree(info);
4466
4467         return 0;
4468 }
4469
4470 struct buffer_ref {
4471         struct ring_buffer      *buffer;
4472         void                    *page;
4473         int                     ref;
4474 };
4475
4476 static void buffer_pipe_buf_release(struct pipe_inode_info *pipe,
4477                                     struct pipe_buffer *buf)
4478 {
4479         struct buffer_ref *ref = (struct buffer_ref *)buf->private;
4480
4481         if (--ref->ref)
4482                 return;
4483
4484         ring_buffer_free_read_page(ref->buffer, ref->page);
4485         kfree(ref);
4486         buf->private = 0;
4487 }
4488
4489 static void buffer_pipe_buf_get(struct pipe_inode_info *pipe,
4490                                 struct pipe_buffer *buf)
4491 {
4492         struct buffer_ref *ref = (struct buffer_ref *)buf->private;
4493
4494         ref->ref++;
4495 }
4496
4497 /* Pipe buffer operations for a buffer. */
4498 static const struct pipe_buf_operations buffer_pipe_buf_ops = {
4499         .can_merge              = 0,
4500         .map                    = generic_pipe_buf_map,
4501         .unmap                  = generic_pipe_buf_unmap,
4502         .confirm                = generic_pipe_buf_confirm,
4503         .release                = buffer_pipe_buf_release,
4504         .steal                  = generic_pipe_buf_steal,
4505         .get                    = buffer_pipe_buf_get,
4506 };
4507
4508 /*
4509  * Callback from splice_to_pipe(), if we need to release some pages
4510  * at the end of the spd in case we error'ed out in filling the pipe.
4511  */
4512 static void buffer_spd_release(struct splice_pipe_desc *spd, unsigned int i)
4513 {
4514         struct buffer_ref *ref =
4515                 (struct buffer_ref *)spd->partial[i].private;
4516
4517         if (--ref->ref)
4518                 return;
4519
4520         ring_buffer_free_read_page(ref->buffer, ref->page);
4521         kfree(ref);
4522         spd->partial[i].private = 0;
4523 }
4524
4525 static ssize_t
4526 tracing_buffers_splice_read(struct file *file, loff_t *ppos,
4527                             struct pipe_inode_info *pipe, size_t len,
4528                             unsigned int flags)
4529 {
4530         struct ftrace_buffer_info *info = file->private_data;
4531         struct trace_iterator *iter = &info->iter;
4532         struct partial_page partial_def[PIPE_DEF_BUFFERS];
4533         struct page *pages_def[PIPE_DEF_BUFFERS];
4534         struct splice_pipe_desc spd = {
4535                 .pages          = pages_def,
4536                 .partial        = partial_def,
4537                 .nr_pages_max   = PIPE_DEF_BUFFERS,
4538                 .flags          = flags,
4539                 .ops            = &buffer_pipe_buf_ops,
4540                 .spd_release    = buffer_spd_release,
4541         };
4542         struct buffer_ref *ref;
4543         int entries, size, i;
4544         size_t ret;
4545
4546         if (splice_grow_spd(pipe, &spd))
4547                 return -ENOMEM;
4548
4549         if (*ppos & (PAGE_SIZE - 1)) {
4550                 ret = -EINVAL;
4551                 goto out;
4552         }
4553
4554         if (len & (PAGE_SIZE - 1)) {
4555                 if (len < PAGE_SIZE) {
4556                         ret = -EINVAL;
4557                         goto out;
4558                 }
4559                 len &= PAGE_MASK;
4560         }
4561
4562  again:
4563         trace_access_lock(iter->cpu_file);
4564         entries = ring_buffer_entries_cpu(iter->tr->buffer, iter->cpu_file);
4565
4566         for (i = 0; i < pipe->buffers && len && entries; i++, len -= PAGE_SIZE) {
4567                 struct page *page;
4568                 int r;
4569
4570                 ref = kzalloc(sizeof(*ref), GFP_KERNEL);
4571                 if (!ref)
4572                         break;
4573
4574                 ref->ref = 1;
4575                 ref->buffer = iter->tr->buffer;
4576                 ref->page = ring_buffer_alloc_read_page(ref->buffer, iter->cpu_file);
4577                 if (!ref->page) {
4578                         kfree(ref);
4579                         break;
4580                 }
4581
4582                 r = ring_buffer_read_page(ref->buffer, &ref->page,
4583                                           len, iter->cpu_file, 1);
4584                 if (r < 0) {
4585                         ring_buffer_free_read_page(ref->buffer, ref->page);
4586                         kfree(ref);
4587                         break;
4588                 }
4589
4590                 /*
4591                  * zero out any left over data, this is going to
4592                  * user land.
4593                  */
4594                 size = ring_buffer_page_len(ref->page);
4595                 if (size < PAGE_SIZE)
4596                         memset(ref->page + size, 0, PAGE_SIZE - size);
4597
4598                 page = virt_to_page(ref->page);
4599
4600                 spd.pages[i] = page;
4601                 spd.partial[i].len = PAGE_SIZE;
4602                 spd.partial[i].offset = 0;
4603                 spd.partial[i].private = (unsigned long)ref;
4604                 spd.nr_pages++;
4605                 *ppos += PAGE_SIZE;
4606
4607                 entries = ring_buffer_entries_cpu(iter->tr->buffer, iter->cpu_file);
4608         }
4609
4610         trace_access_unlock(iter->cpu_file);
4611         spd.nr_pages = i;
4612
4613         /* did we read anything? */
4614         if (!spd.nr_pages) {
4615                 if ((file->f_flags & O_NONBLOCK) || (flags & SPLICE_F_NONBLOCK)) {
4616                         ret = -EAGAIN;
4617                         goto out;
4618                 }
4619                 default_wait_pipe(iter);
4620                 if (signal_pending(current)) {
4621                         ret = -EINTR;
4622                         goto out;
4623                 }
4624                 goto again;
4625         }
4626
4627         ret = splice_to_pipe(pipe, &spd);
4628         splice_shrink_spd(&spd);
4629 out:
4630         return ret;
4631 }
4632
4633 static const struct file_operations tracing_buffers_fops = {
4634         .open           = tracing_buffers_open,
4635         .read           = tracing_buffers_read,
4636         .poll           = tracing_buffers_poll,
4637         .release        = tracing_buffers_release,
4638         .splice_read    = tracing_buffers_splice_read,
4639         .llseek         = no_llseek,
4640 };
4641
4642 static ssize_t
4643 tracing_stats_read(struct file *filp, char __user *ubuf,
4644                    size_t count, loff_t *ppos)
4645 {
4646         struct trace_cpu *tc = filp->private_data;
4647         struct trace_array *tr = tc->tr;
4648         struct trace_seq *s;
4649         unsigned long cnt;
4650         unsigned long long t;
4651         unsigned long usec_rem;
4652         int cpu = tc->cpu;
4653
4654         s = kmalloc(sizeof(*s), GFP_KERNEL);
4655         if (!s)
4656                 return -ENOMEM;
4657
4658         trace_seq_init(s);
4659
4660         cnt = ring_buffer_entries_cpu(tr->buffer, cpu);
4661         trace_seq_printf(s, "entries: %ld\n", cnt);
4662
4663         cnt = ring_buffer_overrun_cpu(tr->buffer, cpu);
4664         trace_seq_printf(s, "overrun: %ld\n", cnt);
4665
4666         cnt = ring_buffer_commit_overrun_cpu(tr->buffer, cpu);
4667         trace_seq_printf(s, "commit overrun: %ld\n", cnt);
4668
4669         cnt = ring_buffer_bytes_cpu(tr->buffer, cpu);
4670         trace_seq_printf(s, "bytes: %ld\n", cnt);
4671
4672         if (trace_clocks[trace_clock_id].in_ns) {
4673                 /* local or global for trace_clock */
4674                 t = ns2usecs(ring_buffer_oldest_event_ts(tr->buffer, cpu));
4675                 usec_rem = do_div(t, USEC_PER_SEC);
4676                 trace_seq_printf(s, "oldest event ts: %5llu.%06lu\n",
4677                                                                 t, usec_rem);
4678
4679                 t = ns2usecs(ring_buffer_time_stamp(tr->buffer, cpu));
4680                 usec_rem = do_div(t, USEC_PER_SEC);
4681                 trace_seq_printf(s, "now ts: %5llu.%06lu\n", t, usec_rem);
4682         } else {
4683                 /* counter or tsc mode for trace_clock */
4684                 trace_seq_printf(s, "oldest event ts: %llu\n",
4685                                 ring_buffer_oldest_event_ts(tr->buffer, cpu));
4686
4687                 trace_seq_printf(s, "now ts: %llu\n",
4688                                 ring_buffer_time_stamp(tr->buffer, cpu));
4689         }
4690
4691         cnt = ring_buffer_dropped_events_cpu(tr->buffer, cpu);
4692         trace_seq_printf(s, "dropped events: %ld\n", cnt);
4693
4694         cnt = ring_buffer_read_events_cpu(tr->buffer, cpu);
4695         trace_seq_printf(s, "read events: %ld\n", cnt);
4696
4697         count = simple_read_from_buffer(ubuf, count, ppos, s->buffer, s->len);
4698
4699         kfree(s);
4700
4701         return count;
4702 }
4703
4704 static const struct file_operations tracing_stats_fops = {
4705         .open           = tracing_open_generic,
4706         .read           = tracing_stats_read,
4707         .llseek         = generic_file_llseek,
4708 };
4709
4710 #ifdef CONFIG_DYNAMIC_FTRACE
4711
4712 int __weak ftrace_arch_read_dyn_info(char *buf, int size)
4713 {
4714         return 0;
4715 }
4716
4717 static ssize_t
4718 tracing_read_dyn_info(struct file *filp, char __user *ubuf,
4719                   size_t cnt, loff_t *ppos)
4720 {
4721         static char ftrace_dyn_info_buffer[1024];
4722         static DEFINE_MUTEX(dyn_info_mutex);
4723         unsigned long *p = filp->private_data;
4724         char *buf = ftrace_dyn_info_buffer;
4725         int size = ARRAY_SIZE(ftrace_dyn_info_buffer);
4726         int r;
4727
4728         mutex_lock(&dyn_info_mutex);
4729         r = sprintf(buf, "%ld ", *p);
4730
4731         r += ftrace_arch_read_dyn_info(buf+r, (size-1)-r);
4732         buf[r++] = '\n';
4733
4734         r = simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
4735
4736         mutex_unlock(&dyn_info_mutex);
4737
4738         return r;
4739 }
4740
4741 static const struct file_operations tracing_dyn_info_fops = {
4742         .open           = tracing_open_generic,
4743         .read           = tracing_read_dyn_info,
4744         .llseek         = generic_file_llseek,
4745 };
4746 #endif
4747
4748 struct dentry *tracing_init_dentry_tr(struct trace_array *tr)
4749 {
4750         static int once;
4751
4752         if (tr->dir)
4753                 return tr->dir;
4754
4755         if (!debugfs_initialized())
4756                 return NULL;
4757
4758         if (tr->flags & TRACE_ARRAY_FL_GLOBAL)
4759                 tr->dir = debugfs_create_dir("tracing", NULL);
4760
4761         if (!tr->dir && !once) {
4762                 once = 1;
4763                 pr_warning("Could not create debugfs directory 'tracing'\n");
4764                 return NULL;
4765         }
4766
4767         return tr->dir;
4768 }
4769
4770 struct dentry *tracing_init_dentry(void)
4771 {
4772         return tracing_init_dentry_tr(&global_trace);
4773 }
4774
4775 static struct dentry *tracing_dentry_percpu(struct trace_array *tr, int cpu)
4776 {
4777         struct dentry *d_tracer;
4778
4779         if (tr->percpu_dir)
4780                 return tr->percpu_dir;
4781
4782         d_tracer = tracing_init_dentry_tr(tr);
4783         if (!d_tracer)
4784                 return NULL;
4785
4786         tr->percpu_dir = debugfs_create_dir("per_cpu", d_tracer);
4787
4788         WARN_ONCE(!tr->percpu_dir,
4789                   "Could not create debugfs directory 'per_cpu/%d'\n", cpu);
4790
4791         return tr->percpu_dir;
4792 }
4793
4794 static void
4795 tracing_init_debugfs_percpu(struct trace_array *tr, long cpu)
4796 {
4797         struct trace_array_cpu *data = per_cpu_ptr(tr->data, cpu);
4798         struct dentry *d_percpu = tracing_dentry_percpu(tr, cpu);
4799         struct dentry *d_cpu;
4800         char cpu_dir[30]; /* 30 characters should be more than enough */
4801
4802         if (!d_percpu)
4803                 return;
4804
4805         snprintf(cpu_dir, 30, "cpu%ld", cpu);
4806         d_cpu = debugfs_create_dir(cpu_dir, d_percpu);
4807         if (!d_cpu) {
4808                 pr_warning("Could not create debugfs '%s' entry\n", cpu_dir);
4809                 return;
4810         }
4811
4812         /* per cpu trace_pipe */
4813         trace_create_file("trace_pipe", 0444, d_cpu,
4814                         (void *)&data->trace_cpu, &tracing_pipe_fops);
4815
4816         /* per cpu trace */
4817         trace_create_file("trace", 0644, d_cpu,
4818                         (void *)&data->trace_cpu, &tracing_fops);
4819
4820         trace_create_file("trace_pipe_raw", 0444, d_cpu,
4821                         (void *)&data->trace_cpu, &tracing_buffers_fops);
4822
4823         trace_create_file("stats", 0444, d_cpu,
4824                         (void *)&data->trace_cpu, &tracing_stats_fops);
4825
4826         trace_create_file("buffer_size_kb", 0444, d_cpu,
4827                         (void *)&data->trace_cpu, &tracing_entries_fops);
4828 }
4829
4830 #ifdef CONFIG_FTRACE_SELFTEST
4831 /* Let selftest have access to static functions in this file */
4832 #include "trace_selftest.c"
4833 #endif
4834
4835 struct trace_option_dentry {
4836         struct tracer_opt               *opt;
4837         struct tracer_flags             *flags;
4838         struct trace_array              *tr;
4839         struct dentry                   *entry;
4840 };
4841
4842 static ssize_t
4843 trace_options_read(struct file *filp, char __user *ubuf, size_t cnt,
4844                         loff_t *ppos)
4845 {
4846         struct trace_option_dentry *topt = filp->private_data;
4847         char *buf;
4848
4849         if (topt->flags->val & topt->opt->bit)
4850                 buf = "1\n";
4851         else
4852                 buf = "0\n";
4853
4854         return simple_read_from_buffer(ubuf, cnt, ppos, buf, 2);
4855 }
4856
4857 static ssize_t
4858 trace_options_write(struct file *filp, const char __user *ubuf, size_t cnt,
4859                          loff_t *ppos)
4860 {
4861         struct trace_option_dentry *topt = filp->private_data;
4862         unsigned long val;
4863         int ret;
4864
4865         ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
4866         if (ret)
4867                 return ret;
4868
4869         if (val != 0 && val != 1)
4870                 return -EINVAL;
4871
4872         if (!!(topt->flags->val & topt->opt->bit) != val) {
4873                 mutex_lock(&trace_types_lock);
4874                 ret = __set_tracer_option(topt->tr->current_trace, topt->flags,
4875                                           topt->opt, !val);
4876                 mutex_unlock(&trace_types_lock);
4877                 if (ret)
4878                         return ret;
4879         }
4880
4881         *ppos += cnt;
4882
4883         return cnt;
4884 }
4885
4886
4887 static const struct file_operations trace_options_fops = {
4888         .open = tracing_open_generic,
4889         .read = trace_options_read,
4890         .write = trace_options_write,
4891         .llseek = generic_file_llseek,
4892 };
4893
4894 static ssize_t
4895 trace_options_core_read(struct file *filp, char __user *ubuf, size_t cnt,
4896                         loff_t *ppos)
4897 {
4898         long index = (long)filp->private_data;
4899         char *buf;
4900
4901         if (trace_flags & (1 << index))
4902                 buf = "1\n";
4903         else
4904                 buf = "0\n";
4905
4906         return simple_read_from_buffer(ubuf, cnt, ppos, buf, 2);
4907 }
4908
4909 static ssize_t
4910 trace_options_core_write(struct file *filp, const char __user *ubuf, size_t cnt,
4911                          loff_t *ppos)
4912 {
4913         struct trace_array *tr = &global_trace;
4914         long index = (long)filp->private_data;
4915         unsigned long val;
4916         int ret;
4917
4918         ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
4919         if (ret)
4920                 return ret;
4921
4922         if (val != 0 && val != 1)
4923                 return -EINVAL;
4924
4925         mutex_lock(&trace_types_lock);
4926         ret = set_tracer_flag(tr, 1 << index, val);
4927         mutex_unlock(&trace_types_lock);
4928
4929         if (ret < 0)
4930                 return ret;
4931
4932         *ppos += cnt;
4933
4934         return cnt;
4935 }
4936
4937 static const struct file_operations trace_options_core_fops = {
4938         .open = tracing_open_generic,
4939         .read = trace_options_core_read,
4940         .write = trace_options_core_write,
4941         .llseek = generic_file_llseek,
4942 };
4943
4944 struct dentry *trace_create_file(const char *name,
4945                                  umode_t mode,
4946                                  struct dentry *parent,
4947                                  void *data,
4948                                  const struct file_operations *fops)
4949 {
4950         struct dentry *ret;
4951
4952         ret = debugfs_create_file(name, mode, parent, data, fops);
4953         if (!ret)
4954                 pr_warning("Could not create debugfs '%s' entry\n", name);
4955
4956         return ret;
4957 }
4958
4959
4960 static struct dentry *trace_options_init_dentry(struct trace_array *tr)
4961 {
4962         struct dentry *d_tracer;
4963
4964         if (tr->options)
4965                 return tr->options;
4966
4967         d_tracer = tracing_init_dentry_tr(tr);
4968         if (!d_tracer)
4969                 return NULL;
4970
4971         tr->options = debugfs_create_dir("options", d_tracer);
4972         if (!tr->options) {
4973                 pr_warning("Could not create debugfs directory 'options'\n");
4974                 return NULL;
4975         }
4976
4977         return tr->options;
4978 }
4979
4980 static void
4981 create_trace_option_file(struct trace_array *tr,
4982                          struct trace_option_dentry *topt,
4983                          struct tracer_flags *flags,
4984                          struct tracer_opt *opt)
4985 {
4986         struct dentry *t_options;
4987
4988         t_options = trace_options_init_dentry(tr);
4989         if (!t_options)
4990                 return;
4991
4992         topt->flags = flags;
4993         topt->opt = opt;
4994         topt->tr = tr;
4995
4996         topt->entry = trace_create_file(opt->name, 0644, t_options, topt,
4997                                     &trace_options_fops);
4998
4999 }
5000
5001 static struct trace_option_dentry *
5002 create_trace_option_files(struct trace_array *tr, struct tracer *tracer)
5003 {
5004         struct trace_option_dentry *topts;
5005         struct tracer_flags *flags;
5006         struct tracer_opt *opts;
5007         int cnt;
5008
5009         if (!tracer)
5010                 return NULL;
5011
5012         flags = tracer->flags;
5013
5014         if (!flags || !flags->opts)
5015                 return NULL;
5016
5017         opts = flags->opts;
5018
5019         for (cnt = 0; opts[cnt].name; cnt++)
5020                 ;
5021
5022         topts = kcalloc(cnt + 1, sizeof(*topts), GFP_KERNEL);
5023         if (!topts)
5024                 return NULL;
5025
5026         for (cnt = 0; opts[cnt].name; cnt++)
5027                 create_trace_option_file(tr, &topts[cnt], flags,
5028                                          &opts[cnt]);
5029
5030         return topts;
5031 }
5032
5033 static void
5034 destroy_trace_option_files(struct trace_option_dentry *topts)
5035 {
5036         int cnt;
5037
5038         if (!topts)
5039                 return;
5040
5041         for (cnt = 0; topts[cnt].opt; cnt++) {
5042                 if (topts[cnt].entry)
5043                         debugfs_remove(topts[cnt].entry);
5044         }
5045
5046         kfree(topts);
5047 }
5048
5049 static struct dentry *
5050 create_trace_option_core_file(struct trace_array *tr,
5051                               const char *option, long index)
5052 {
5053         struct dentry *t_options;
5054
5055         t_options = trace_options_init_dentry(tr);
5056         if (!t_options)
5057                 return NULL;
5058
5059         return trace_create_file(option, 0644, t_options, (void *)index,
5060                                     &trace_options_core_fops);
5061 }
5062
5063 static __init void create_trace_options_dir(struct trace_array *tr)
5064 {
5065         struct dentry *t_options;
5066         int i;
5067
5068         t_options = trace_options_init_dentry(tr);
5069         if (!t_options)
5070                 return;
5071
5072         for (i = 0; trace_options[i]; i++)
5073                 create_trace_option_core_file(tr, trace_options[i], i);
5074 }
5075
5076 static ssize_t
5077 rb_simple_read(struct file *filp, char __user *ubuf,
5078                size_t cnt, loff_t *ppos)
5079 {
5080         struct trace_array *tr = filp->private_data;
5081         struct ring_buffer *buffer = tr->buffer;
5082         char buf[64];
5083         int r;
5084
5085         if (buffer)
5086                 r = ring_buffer_record_is_on(buffer);
5087         else
5088                 r = 0;
5089
5090         r = sprintf(buf, "%d\n", r);
5091
5092         return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
5093 }
5094
5095 static ssize_t
5096 rb_simple_write(struct file *filp, const char __user *ubuf,
5097                 size_t cnt, loff_t *ppos)
5098 {
5099         struct trace_array *tr = filp->private_data;
5100         struct ring_buffer *buffer = tr->buffer;
5101         unsigned long val;
5102         int ret;
5103
5104         ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
5105         if (ret)
5106                 return ret;
5107
5108         if (buffer) {
5109                 mutex_lock(&trace_types_lock);
5110                 if (val) {
5111                         ring_buffer_record_on(buffer);
5112                         if (tr->current_trace->start)
5113                                 tr->current_trace->start(tr);
5114                 } else {
5115                         ring_buffer_record_off(buffer);
5116                         if (tr->current_trace->stop)
5117                                 tr->current_trace->stop(tr);
5118                 }
5119                 mutex_unlock(&trace_types_lock);
5120         }
5121
5122         (*ppos)++;
5123
5124         return cnt;
5125 }
5126
5127 static const struct file_operations rb_simple_fops = {
5128         .open           = tracing_open_generic,
5129         .read           = rb_simple_read,
5130         .write          = rb_simple_write,
5131         .llseek         = default_llseek,
5132 };
5133
5134 struct dentry *trace_instance_dir;
5135
5136 static void
5137 init_tracer_debugfs(struct trace_array *tr, struct dentry *d_tracer);
5138
5139 static int new_instance_create(const char *name)
5140 {
5141         enum ring_buffer_flags rb_flags;
5142         struct trace_array *tr;
5143         int ret;
5144         int i;
5145
5146         mutex_lock(&trace_types_lock);
5147
5148         ret = -EEXIST;
5149         list_for_each_entry(tr, &ftrace_trace_arrays, list) {
5150                 if (tr->name && strcmp(tr->name, name) == 0)
5151                         goto out_unlock;
5152         }
5153
5154         ret = -ENOMEM;
5155         tr = kzalloc(sizeof(*tr), GFP_KERNEL);
5156         if (!tr)
5157                 goto out_unlock;
5158
5159         tr->name = kstrdup(name, GFP_KERNEL);
5160         if (!tr->name)
5161                 goto out_free_tr;
5162
5163         raw_spin_lock_init(&tr->start_lock);
5164
5165         tr->current_trace = &nop_trace;
5166
5167         INIT_LIST_HEAD(&tr->systems);
5168         INIT_LIST_HEAD(&tr->events);
5169
5170         rb_flags = trace_flags & TRACE_ITER_OVERWRITE ? RB_FL_OVERWRITE : 0;
5171
5172         tr->buffer = ring_buffer_alloc(trace_buf_size, rb_flags);
5173         if (!tr->buffer)
5174                 goto out_free_tr;
5175
5176         tr->data = alloc_percpu(struct trace_array_cpu);
5177         if (!tr->data)
5178                 goto out_free_tr;
5179
5180         for_each_tracing_cpu(i) {
5181                 memset(per_cpu_ptr(tr->data, i), 0, sizeof(struct trace_array_cpu));
5182                 per_cpu_ptr(tr->data, i)->trace_cpu.cpu = i;
5183                 per_cpu_ptr(tr->data, i)->trace_cpu.tr = tr;
5184         }
5185
5186         /* Holder for file callbacks */
5187         tr->trace_cpu.cpu = RING_BUFFER_ALL_CPUS;
5188         tr->trace_cpu.tr = tr;
5189
5190         tr->dir = debugfs_create_dir(name, trace_instance_dir);
5191         if (!tr->dir)
5192                 goto out_free_tr;
5193
5194         ret = event_trace_add_tracer(tr->dir, tr);
5195         if (ret)
5196                 goto out_free_tr;
5197
5198         init_tracer_debugfs(tr, tr->dir);
5199
5200         list_add(&tr->list, &ftrace_trace_arrays);
5201
5202         mutex_unlock(&trace_types_lock);
5203
5204         return 0;
5205
5206  out_free_tr:
5207         if (tr->buffer)
5208                 ring_buffer_free(tr->buffer);
5209         kfree(tr->name);
5210         kfree(tr);
5211
5212  out_unlock:
5213         mutex_unlock(&trace_types_lock);
5214
5215         return ret;
5216
5217 }
5218
5219 static int instance_delete(const char *name)
5220 {
5221         struct trace_array *tr;
5222         int found = 0;
5223         int ret;
5224
5225         mutex_lock(&trace_types_lock);
5226
5227         ret = -ENODEV;
5228         list_for_each_entry(tr, &ftrace_trace_arrays, list) {
5229                 if (tr->name && strcmp(tr->name, name) == 0) {
5230                         found = 1;
5231                         break;
5232                 }
5233         }
5234         if (!found)
5235                 goto out_unlock;
5236
5237         list_del(&tr->list);
5238
5239         event_trace_del_tracer(tr);
5240         debugfs_remove_recursive(tr->dir);
5241         free_percpu(tr->data);
5242         ring_buffer_free(tr->buffer);
5243
5244         kfree(tr->name);
5245         kfree(tr);
5246
5247         ret = 0;
5248
5249  out_unlock:
5250         mutex_unlock(&trace_types_lock);
5251
5252         return ret;
5253 }
5254
5255 static int instance_mkdir (struct inode *inode, struct dentry *dentry, umode_t mode)
5256 {
5257         struct dentry *parent;
5258         int ret;
5259
5260         /* Paranoid: Make sure the parent is the "instances" directory */
5261         parent = hlist_entry(inode->i_dentry.first, struct dentry, d_alias);
5262         if (WARN_ON_ONCE(parent != trace_instance_dir))
5263                 return -ENOENT;
5264
5265         /*
5266          * The inode mutex is locked, but debugfs_create_dir() will also
5267          * take the mutex. As the instances directory can not be destroyed
5268          * or changed in any other way, it is safe to unlock it, and
5269          * let the dentry try. If two users try to make the same dir at
5270          * the same time, then the new_instance_create() will determine the
5271          * winner.
5272          */
5273         mutex_unlock(&inode->i_mutex);
5274
5275         ret = new_instance_create(dentry->d_iname);
5276
5277         mutex_lock(&inode->i_mutex);
5278
5279         return ret;
5280 }
5281
5282 static int instance_rmdir(struct inode *inode, struct dentry *dentry)
5283 {
5284         struct dentry *parent;
5285         int ret;
5286
5287         /* Paranoid: Make sure the parent is the "instances" directory */
5288         parent = hlist_entry(inode->i_dentry.first, struct dentry, d_alias);
5289         if (WARN_ON_ONCE(parent != trace_instance_dir))
5290                 return -ENOENT;
5291
5292         /* The caller did a dget() on dentry */
5293         mutex_unlock(&dentry->d_inode->i_mutex);
5294
5295         /*
5296          * The inode mutex is locked, but debugfs_create_dir() will also
5297          * take the mutex. As the instances directory can not be destroyed
5298          * or changed in any other way, it is safe to unlock it, and
5299          * let the dentry try. If two users try to make the same dir at
5300          * the same time, then the instance_delete() will determine the
5301          * winner.
5302          */
5303         mutex_unlock(&inode->i_mutex);
5304
5305         ret = instance_delete(dentry->d_iname);
5306
5307         mutex_lock_nested(&inode->i_mutex, I_MUTEX_PARENT);
5308         mutex_lock(&dentry->d_inode->i_mutex);
5309
5310         return ret;
5311 }
5312
5313 static const struct inode_operations instance_dir_inode_operations = {
5314         .lookup         = simple_lookup,
5315         .mkdir          = instance_mkdir,
5316         .rmdir          = instance_rmdir,
5317 };
5318
5319 static __init void create_trace_instances(struct dentry *d_tracer)
5320 {
5321         trace_instance_dir = debugfs_create_dir("instances", d_tracer);
5322         if (WARN_ON(!trace_instance_dir))
5323                 return;
5324
5325         /* Hijack the dir inode operations, to allow mkdir */
5326         trace_instance_dir->d_inode->i_op = &instance_dir_inode_operations;
5327 }
5328
5329 static void
5330 init_tracer_debugfs(struct trace_array *tr, struct dentry *d_tracer)
5331 {
5332
5333         trace_create_file("trace_options", 0644, d_tracer,
5334                           tr, &tracing_iter_fops);
5335
5336         trace_create_file("trace", 0644, d_tracer,
5337                         (void *)&tr->trace_cpu, &tracing_fops);
5338
5339         trace_create_file("trace_pipe", 0444, d_tracer,
5340                         (void *)&tr->trace_cpu, &tracing_pipe_fops);
5341
5342         trace_create_file("buffer_size_kb", 0644, d_tracer,
5343                         (void *)&tr->trace_cpu, &tracing_entries_fops);
5344
5345         trace_create_file("buffer_total_size_kb", 0444, d_tracer,
5346                           tr, &tracing_total_entries_fops);
5347
5348         trace_create_file("free_buffer", 0644, d_tracer,
5349                           tr, &tracing_free_buffer_fops);
5350
5351         trace_create_file("trace_marker", 0220, d_tracer,
5352                           tr, &tracing_mark_fops);
5353
5354         trace_create_file("trace_clock", 0644, d_tracer, tr,
5355                           &trace_clock_fops);
5356
5357         trace_create_file("tracing_on", 0644, d_tracer,
5358                             tr, &rb_simple_fops);
5359 }
5360
5361 static __init int tracer_init_debugfs(void)
5362 {
5363         struct dentry *d_tracer;
5364         int cpu;
5365
5366         trace_access_lock_init();
5367
5368         d_tracer = tracing_init_dentry();
5369
5370         init_tracer_debugfs(&global_trace, d_tracer);
5371
5372         trace_create_file("tracing_cpumask", 0644, d_tracer,
5373                         &global_trace, &tracing_cpumask_fops);
5374
5375         trace_create_file("available_tracers", 0444, d_tracer,
5376                         &global_trace, &show_traces_fops);
5377
5378         trace_create_file("current_tracer", 0644, d_tracer,
5379                         &global_trace, &set_tracer_fops);
5380
5381 #ifdef CONFIG_TRACER_MAX_TRACE
5382         trace_create_file("tracing_max_latency", 0644, d_tracer,
5383                         &tracing_max_latency, &tracing_max_lat_fops);
5384 #endif
5385
5386         trace_create_file("tracing_thresh", 0644, d_tracer,
5387                         &tracing_thresh, &tracing_max_lat_fops);
5388
5389         trace_create_file("README", 0444, d_tracer,
5390                         NULL, &tracing_readme_fops);
5391
5392         trace_create_file("saved_cmdlines", 0444, d_tracer,
5393                         NULL, &tracing_saved_cmdlines_fops);
5394
5395 #ifdef CONFIG_DYNAMIC_FTRACE
5396         trace_create_file("dyn_ftrace_total_info", 0444, d_tracer,
5397                         &ftrace_update_tot_cnt, &tracing_dyn_info_fops);
5398 #endif
5399
5400 #ifdef CONFIG_TRACER_SNAPSHOT
5401         trace_create_file("snapshot", 0644, d_tracer,
5402                           (void *)&global_trace.trace_cpu, &snapshot_fops);
5403 #endif
5404
5405         create_trace_instances(d_tracer);
5406
5407         create_trace_options_dir(&global_trace);
5408
5409         for_each_tracing_cpu(cpu)
5410                 tracing_init_debugfs_percpu(&global_trace, cpu);
5411
5412         return 0;
5413 }
5414
5415 static int trace_panic_handler(struct notifier_block *this,
5416                                unsigned long event, void *unused)
5417 {
5418         if (ftrace_dump_on_oops)
5419                 ftrace_dump(ftrace_dump_on_oops);
5420         return NOTIFY_OK;
5421 }
5422
5423 static struct notifier_block trace_panic_notifier = {
5424         .notifier_call  = trace_panic_handler,
5425         .next           = NULL,
5426         .priority       = 150   /* priority: INT_MAX >= x >= 0 */
5427 };
5428
5429 static int trace_die_handler(struct notifier_block *self,
5430                              unsigned long val,
5431                              void *data)
5432 {
5433         switch (val) {
5434         case DIE_OOPS:
5435                 if (ftrace_dump_on_oops)
5436                         ftrace_dump(ftrace_dump_on_oops);
5437                 break;
5438         default:
5439                 break;
5440         }
5441         return NOTIFY_OK;
5442 }
5443
5444 static struct notifier_block trace_die_notifier = {
5445         .notifier_call = trace_die_handler,
5446         .priority = 200
5447 };
5448
5449 /*
5450  * printk is set to max of 1024, we really don't need it that big.
5451  * Nothing should be printing 1000 characters anyway.
5452  */
5453 #define TRACE_MAX_PRINT         1000
5454
5455 /*
5456  * Define here KERN_TRACE so that we have one place to modify
5457  * it if we decide to change what log level the ftrace dump
5458  * should be at.
5459  */
5460 #define KERN_TRACE              KERN_EMERG
5461
5462 void
5463 trace_printk_seq(struct trace_seq *s)
5464 {
5465         /* Probably should print a warning here. */
5466         if (s->len >= 1000)
5467                 s->len = 1000;
5468
5469         /* should be zero ended, but we are paranoid. */
5470         s->buffer[s->len] = 0;
5471
5472         printk(KERN_TRACE "%s", s->buffer);
5473
5474         trace_seq_init(s);
5475 }
5476
5477 void trace_init_global_iter(struct trace_iterator *iter)
5478 {
5479         iter->tr = &global_trace;
5480         iter->trace = iter->tr->current_trace;
5481         iter->cpu_file = RING_BUFFER_ALL_CPUS;
5482 }
5483
5484 static void
5485 __ftrace_dump(bool disable_tracing, enum ftrace_dump_mode oops_dump_mode)
5486 {
5487         static arch_spinlock_t ftrace_dump_lock =
5488                 (arch_spinlock_t)__ARCH_SPIN_LOCK_UNLOCKED;
5489         /* use static because iter can be a bit big for the stack */
5490         static struct trace_iterator iter;
5491         unsigned int old_userobj;
5492         static int dump_ran;
5493         unsigned long flags;
5494         int cnt = 0, cpu;
5495
5496         /* only one dump */
5497         local_irq_save(flags);
5498         arch_spin_lock(&ftrace_dump_lock);
5499         if (dump_ran)
5500                 goto out;
5501
5502         dump_ran = 1;
5503
5504         tracing_off();
5505
5506         /* Did function tracer already get disabled? */
5507         if (ftrace_is_dead()) {
5508                 printk("# WARNING: FUNCTION TRACING IS CORRUPTED\n");
5509                 printk("#          MAY BE MISSING FUNCTION EVENTS\n");
5510         }
5511
5512         if (disable_tracing)
5513                 ftrace_kill();
5514
5515         /* Simulate the iterator */
5516         trace_init_global_iter(&iter);
5517
5518         for_each_tracing_cpu(cpu) {
5519                 atomic_inc(&per_cpu_ptr(iter.tr->data, cpu)->disabled);
5520         }
5521
5522         old_userobj = trace_flags & TRACE_ITER_SYM_USEROBJ;
5523
5524         /* don't look at user memory in panic mode */
5525         trace_flags &= ~TRACE_ITER_SYM_USEROBJ;
5526
5527         switch (oops_dump_mode) {
5528         case DUMP_ALL:
5529                 iter.cpu_file = RING_BUFFER_ALL_CPUS;
5530                 break;
5531         case DUMP_ORIG:
5532                 iter.cpu_file = raw_smp_processor_id();
5533                 break;
5534         case DUMP_NONE:
5535                 goto out_enable;
5536         default:
5537                 printk(KERN_TRACE "Bad dumping mode, switching to all CPUs dump\n");
5538                 iter.cpu_file = RING_BUFFER_ALL_CPUS;
5539         }
5540
5541         printk(KERN_TRACE "Dumping ftrace buffer:\n");
5542
5543         /*
5544          * We need to stop all tracing on all CPUS to read the
5545          * the next buffer. This is a bit expensive, but is
5546          * not done often. We fill all what we can read,
5547          * and then release the locks again.
5548          */
5549
5550         while (!trace_empty(&iter)) {
5551
5552                 if (!cnt)
5553                         printk(KERN_TRACE "---------------------------------\n");
5554
5555                 cnt++;
5556
5557                 /* reset all but tr, trace, and overruns */
5558                 memset(&iter.seq, 0,
5559                        sizeof(struct trace_iterator) -
5560                        offsetof(struct trace_iterator, seq));
5561                 iter.iter_flags |= TRACE_FILE_LAT_FMT;
5562                 iter.pos = -1;
5563
5564                 if (trace_find_next_entry_inc(&iter) != NULL) {
5565                         int ret;
5566
5567                         ret = print_trace_line(&iter);
5568                         if (ret != TRACE_TYPE_NO_CONSUME)
5569                                 trace_consume(&iter);
5570                 }
5571                 touch_nmi_watchdog();
5572
5573                 trace_printk_seq(&iter.seq);
5574         }
5575
5576         if (!cnt)
5577                 printk(KERN_TRACE "   (ftrace buffer empty)\n");
5578         else
5579                 printk(KERN_TRACE "---------------------------------\n");
5580
5581  out_enable:
5582         /* Re-enable tracing if requested */
5583         if (!disable_tracing) {
5584                 trace_flags |= old_userobj;
5585
5586                 for_each_tracing_cpu(cpu) {
5587                         atomic_dec(&per_cpu_ptr(iter.tr->data, cpu)->disabled);
5588                 }
5589                 tracing_on();
5590         }
5591
5592  out:
5593         arch_spin_unlock(&ftrace_dump_lock);
5594         local_irq_restore(flags);
5595 }
5596
5597 /* By default: disable tracing after the dump */
5598 void ftrace_dump(enum ftrace_dump_mode oops_dump_mode)
5599 {
5600         __ftrace_dump(true, oops_dump_mode);
5601 }
5602 EXPORT_SYMBOL_GPL(ftrace_dump);
5603
5604 __init static int tracer_alloc_buffers(void)
5605 {
5606         int ring_buf_size;
5607         enum ring_buffer_flags rb_flags;
5608         int i;
5609         int ret = -ENOMEM;
5610
5611
5612         if (!alloc_cpumask_var(&tracing_buffer_mask, GFP_KERNEL))
5613                 goto out;
5614
5615         if (!alloc_cpumask_var(&tracing_cpumask, GFP_KERNEL))
5616                 goto out_free_buffer_mask;
5617
5618         /* Only allocate trace_printk buffers if a trace_printk exists */
5619         if (__stop___trace_bprintk_fmt != __start___trace_bprintk_fmt)
5620                 /* Must be called before global_trace.buffer is allocated */
5621                 trace_printk_init_buffers();
5622
5623         /* To save memory, keep the ring buffer size to its minimum */
5624         if (ring_buffer_expanded)
5625                 ring_buf_size = trace_buf_size;
5626         else
5627                 ring_buf_size = 1;
5628
5629         rb_flags = trace_flags & TRACE_ITER_OVERWRITE ? RB_FL_OVERWRITE : 0;
5630
5631         cpumask_copy(tracing_buffer_mask, cpu_possible_mask);
5632         cpumask_copy(tracing_cpumask, cpu_all_mask);
5633
5634         raw_spin_lock_init(&global_trace.start_lock);
5635
5636         /* TODO: make the number of buffers hot pluggable with CPUS */
5637         global_trace.buffer = ring_buffer_alloc(ring_buf_size, rb_flags);
5638         if (!global_trace.buffer) {
5639                 printk(KERN_ERR "tracer: failed to allocate ring buffer!\n");
5640                 WARN_ON(1);
5641                 goto out_free_cpumask;
5642         }
5643
5644         global_trace.data = alloc_percpu(struct trace_array_cpu);
5645
5646         if (!global_trace.data) {
5647                 printk(KERN_ERR "tracer: failed to allocate percpu memory!\n");
5648                 WARN_ON(1);
5649                 goto out_free_cpumask;
5650         }
5651
5652         for_each_tracing_cpu(i) {
5653                 memset(per_cpu_ptr(global_trace.data, i), 0, sizeof(struct trace_array_cpu));
5654                 per_cpu_ptr(global_trace.data, i)->trace_cpu.cpu = i;
5655                 per_cpu_ptr(global_trace.data, i)->trace_cpu.tr = &global_trace;
5656         }
5657
5658         if (global_trace.buffer_disabled)
5659                 tracing_off();
5660
5661 #ifdef CONFIG_TRACER_MAX_TRACE
5662         max_tr.data = alloc_percpu(struct trace_array_cpu);
5663         if (!max_tr.data) {
5664                 printk(KERN_ERR "tracer: failed to allocate percpu memory!\n");
5665                 WARN_ON(1);
5666                 goto out_free_cpumask;
5667         }
5668         max_tr.buffer = ring_buffer_alloc(1, rb_flags);
5669         raw_spin_lock_init(&max_tr.start_lock);
5670         if (!max_tr.buffer) {
5671                 printk(KERN_ERR "tracer: failed to allocate max ring buffer!\n");
5672                 WARN_ON(1);
5673                 ring_buffer_free(global_trace.buffer);
5674                 goto out_free_cpumask;
5675         }
5676
5677         for_each_tracing_cpu(i) {
5678                 memset(per_cpu_ptr(max_tr.data, i), 0, sizeof(struct trace_array_cpu));
5679                 per_cpu_ptr(max_tr.data, i)->trace_cpu.cpu = i;
5680                 per_cpu_ptr(max_tr.data, i)->trace_cpu.tr = &max_tr;
5681         }
5682 #endif
5683
5684         /* Allocate the first page for all buffers */
5685         set_buffer_entries(&global_trace,
5686                            ring_buffer_size(global_trace.buffer, 0));
5687 #ifdef CONFIG_TRACER_MAX_TRACE
5688         set_buffer_entries(&max_tr, 1);
5689 #endif
5690
5691         trace_init_cmdlines();
5692         init_irq_work(&trace_work_wakeup, trace_wake_up);
5693
5694         register_tracer(&nop_trace);
5695
5696         global_trace.current_trace = &nop_trace;
5697
5698         /* All seems OK, enable tracing */
5699         tracing_disabled = 0;
5700
5701         atomic_notifier_chain_register(&panic_notifier_list,
5702                                        &trace_panic_notifier);
5703
5704         register_die_notifier(&trace_die_notifier);
5705
5706         global_trace.flags = TRACE_ARRAY_FL_GLOBAL;
5707
5708         /* Holder for file callbacks */
5709         global_trace.trace_cpu.cpu = RING_BUFFER_ALL_CPUS;
5710         global_trace.trace_cpu.tr = &global_trace;
5711
5712         INIT_LIST_HEAD(&global_trace.systems);
5713         INIT_LIST_HEAD(&global_trace.events);
5714         list_add(&global_trace.list, &ftrace_trace_arrays);
5715
5716         while (trace_boot_options) {
5717                 char *option;
5718
5719                 option = strsep(&trace_boot_options, ",");
5720                 trace_set_options(&global_trace, option);
5721         }
5722
5723         return 0;
5724
5725 out_free_cpumask:
5726         free_percpu(global_trace.data);
5727         free_percpu(max_tr.data);
5728         free_cpumask_var(tracing_cpumask);
5729 out_free_buffer_mask:
5730         free_cpumask_var(tracing_buffer_mask);
5731 out:
5732         return ret;
5733 }
5734
5735 __init static int clear_boot_tracer(void)
5736 {
5737         /*
5738          * The default tracer at boot buffer is an init section.
5739          * This function is called in lateinit. If we did not
5740          * find the boot tracer, then clear it out, to prevent
5741          * later registration from accessing the buffer that is
5742          * about to be freed.
5743          */
5744         if (!default_bootup_tracer)
5745                 return 0;
5746
5747         printk(KERN_INFO "ftrace bootup tracer '%s' not registered.\n",
5748                default_bootup_tracer);
5749         default_bootup_tracer = NULL;
5750
5751         return 0;
5752 }
5753
5754 early_initcall(tracer_alloc_buffers);
5755 fs_initcall(tracer_init_debugfs);
5756 late_initcall(clear_boot_tracer);