tracing: create generic trace parser
[safe/jmp/linux-2.6] / kernel / trace / trace.c
1 /*
2  * ring buffer based function tracer
3  *
4  * Copyright (C) 2007-2008 Steven Rostedt <srostedt@redhat.com>
5  * Copyright (C) 2008 Ingo Molnar <mingo@redhat.com>
6  *
7  * Originally taken from the RT patch by:
8  *    Arnaldo Carvalho de Melo <acme@redhat.com>
9  *
10  * Based on code from the latency_tracer, that is:
11  *  Copyright (C) 2004-2006 Ingo Molnar
12  *  Copyright (C) 2004 William Lee Irwin III
13  */
14 #include <linux/ring_buffer.h>
15 #include <linux/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/smp_lock.h>
21 #include <linux/notifier.h>
22 #include <linux/irqflags.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/ctype.h>
36 #include <linux/init.h>
37 #include <linux/poll.h>
38 #include <linux/gfp.h>
39 #include <linux/fs.h>
40
41 #include "trace.h"
42 #include "trace_output.h"
43
44 #define TRACE_BUFFER_FLAGS      (RB_FL_OVERWRITE)
45
46 /*
47  * On boot up, the ring buffer is set to the minimum size, so that
48  * we do not waste memory on systems that are not using tracing.
49  */
50 int ring_buffer_expanded;
51
52 /*
53  * We need to change this state when a selftest is running.
54  * A selftest will lurk into the ring-buffer to count the
55  * entries inserted during the selftest although some concurrent
56  * insertions into the ring-buffer such as trace_printk could occurred
57  * at the same time, giving false positive or negative results.
58  */
59 static bool __read_mostly tracing_selftest_running;
60
61 /*
62  * If a tracer is running, we do not want to run SELFTEST.
63  */
64 bool __read_mostly tracing_selftest_disabled;
65
66 /* For tracers that don't implement custom flags */
67 static struct tracer_opt dummy_tracer_opt[] = {
68         { }
69 };
70
71 static struct tracer_flags dummy_tracer_flags = {
72         .val = 0,
73         .opts = dummy_tracer_opt
74 };
75
76 static int dummy_set_flag(u32 old_flags, u32 bit, int set)
77 {
78         return 0;
79 }
80
81 /*
82  * Kill all tracing for good (never come back).
83  * It is initialized to 1 but will turn to zero if the initialization
84  * of the tracer is successful. But that is the only place that sets
85  * this back to zero.
86  */
87 static int tracing_disabled = 1;
88
89 DEFINE_PER_CPU(local_t, ftrace_cpu_disabled);
90
91 static inline void ftrace_disable_cpu(void)
92 {
93         preempt_disable();
94         local_inc(&__get_cpu_var(ftrace_cpu_disabled));
95 }
96
97 static inline void ftrace_enable_cpu(void)
98 {
99         local_dec(&__get_cpu_var(ftrace_cpu_disabled));
100         preempt_enable();
101 }
102
103 static cpumask_var_t __read_mostly      tracing_buffer_mask;
104
105 /* Define which cpu buffers are currently read in trace_pipe */
106 static cpumask_var_t                    tracing_reader_cpumask;
107
108 #define for_each_tracing_cpu(cpu)       \
109         for_each_cpu(cpu, tracing_buffer_mask)
110
111 /*
112  * ftrace_dump_on_oops - variable to dump ftrace buffer on oops
113  *
114  * If there is an oops (or kernel panic) and the ftrace_dump_on_oops
115  * is set, then ftrace_dump is called. This will output the contents
116  * of the ftrace buffers to the console.  This is very useful for
117  * capturing traces that lead to crashes and outputing it to a
118  * serial console.
119  *
120  * It is default off, but you can enable it with either specifying
121  * "ftrace_dump_on_oops" in the kernel command line, or setting
122  * /proc/sys/kernel/ftrace_dump_on_oops to true.
123  */
124 int ftrace_dump_on_oops;
125
126 static int tracing_set_tracer(const char *buf);
127
128 #define BOOTUP_TRACER_SIZE              100
129 static char bootup_tracer_buf[BOOTUP_TRACER_SIZE] __initdata;
130 static char *default_bootup_tracer;
131
132 static int __init set_ftrace(char *str)
133 {
134         strncpy(bootup_tracer_buf, str, BOOTUP_TRACER_SIZE);
135         default_bootup_tracer = bootup_tracer_buf;
136         /* We are using ftrace early, expand it */
137         ring_buffer_expanded = 1;
138         return 1;
139 }
140 __setup("ftrace=", set_ftrace);
141
142 static int __init set_ftrace_dump_on_oops(char *str)
143 {
144         ftrace_dump_on_oops = 1;
145         return 1;
146 }
147 __setup("ftrace_dump_on_oops", set_ftrace_dump_on_oops);
148
149 unsigned long long ns2usecs(cycle_t nsec)
150 {
151         nsec += 500;
152         do_div(nsec, 1000);
153         return nsec;
154 }
155
156 /*
157  * The global_trace is the descriptor that holds the tracing
158  * buffers for the live tracing. For each CPU, it contains
159  * a link list of pages that will store trace entries. The
160  * page descriptor of the pages in the memory is used to hold
161  * the link list by linking the lru item in the page descriptor
162  * to each of the pages in the buffer per CPU.
163  *
164  * For each active CPU there is a data field that holds the
165  * pages for the buffer for that CPU. Each CPU has the same number
166  * of pages allocated for its buffer.
167  */
168 static struct trace_array       global_trace;
169
170 static DEFINE_PER_CPU(struct trace_array_cpu, global_trace_cpu);
171
172 int filter_current_check_discard(struct ring_buffer *buffer,
173                                  struct ftrace_event_call *call, void *rec,
174                                  struct ring_buffer_event *event)
175 {
176         return filter_check_discard(call, rec, buffer, event);
177 }
178 EXPORT_SYMBOL_GPL(filter_current_check_discard);
179
180 cycle_t ftrace_now(int cpu)
181 {
182         u64 ts;
183
184         /* Early boot up does not have a buffer yet */
185         if (!global_trace.buffer)
186                 return trace_clock_local();
187
188         ts = ring_buffer_time_stamp(global_trace.buffer, cpu);
189         ring_buffer_normalize_time_stamp(global_trace.buffer, cpu, &ts);
190
191         return ts;
192 }
193
194 /*
195  * The max_tr is used to snapshot the global_trace when a maximum
196  * latency is reached. Some tracers will use this to store a maximum
197  * trace while it continues examining live traces.
198  *
199  * The buffers for the max_tr are set up the same as the global_trace.
200  * When a snapshot is taken, the link list of the max_tr is swapped
201  * with the link list of the global_trace and the buffers are reset for
202  * the global_trace so the tracing can continue.
203  */
204 static struct trace_array       max_tr;
205
206 static DEFINE_PER_CPU(struct trace_array_cpu, max_data);
207
208 /* tracer_enabled is used to toggle activation of a tracer */
209 static int                      tracer_enabled = 1;
210
211 /**
212  * tracing_is_enabled - return tracer_enabled status
213  *
214  * This function is used by other tracers to know the status
215  * of the tracer_enabled flag.  Tracers may use this function
216  * to know if it should enable their features when starting
217  * up. See irqsoff tracer for an example (start_irqsoff_tracer).
218  */
219 int tracing_is_enabled(void)
220 {
221         return tracer_enabled;
222 }
223
224 /*
225  * trace_buf_size is the size in bytes that is allocated
226  * for a buffer. Note, the number of bytes is always rounded
227  * to page size.
228  *
229  * This number is purposely set to a low number of 16384.
230  * If the dump on oops happens, it will be much appreciated
231  * to not have to wait for all that output. Anyway this can be
232  * boot time and run time configurable.
233  */
234 #define TRACE_BUF_SIZE_DEFAULT  1441792UL /* 16384 * 88 (sizeof(entry)) */
235
236 static unsigned long            trace_buf_size = TRACE_BUF_SIZE_DEFAULT;
237
238 /* trace_types holds a link list of available tracers. */
239 static struct tracer            *trace_types __read_mostly;
240
241 /* current_trace points to the tracer that is currently active */
242 static struct tracer            *current_trace __read_mostly;
243
244 /*
245  * max_tracer_type_len is used to simplify the allocating of
246  * buffers to read userspace tracer names. We keep track of
247  * the longest tracer name registered.
248  */
249 static int                      max_tracer_type_len;
250
251 /*
252  * trace_types_lock is used to protect the trace_types list.
253  * This lock is also used to keep user access serialized.
254  * Accesses from userspace will grab this lock while userspace
255  * activities happen inside the kernel.
256  */
257 static DEFINE_MUTEX(trace_types_lock);
258
259 /* trace_wait is a waitqueue for tasks blocked on trace_poll */
260 static DECLARE_WAIT_QUEUE_HEAD(trace_wait);
261
262 /* trace_flags holds trace_options default values */
263 unsigned long trace_flags = TRACE_ITER_PRINT_PARENT | TRACE_ITER_PRINTK |
264         TRACE_ITER_ANNOTATE | TRACE_ITER_CONTEXT_INFO | TRACE_ITER_SLEEP_TIME |
265         TRACE_ITER_GRAPH_TIME;
266
267 static int trace_stop_count;
268 static DEFINE_SPINLOCK(tracing_start_lock);
269
270 /**
271  * trace_wake_up - wake up tasks waiting for trace input
272  *
273  * Simply wakes up any task that is blocked on the trace_wait
274  * queue. These is used with trace_poll for tasks polling the trace.
275  */
276 void trace_wake_up(void)
277 {
278         /*
279          * The runqueue_is_locked() can fail, but this is the best we
280          * have for now:
281          */
282         if (!(trace_flags & TRACE_ITER_BLOCK) && !runqueue_is_locked())
283                 wake_up(&trace_wait);
284 }
285
286 static int __init set_buf_size(char *str)
287 {
288         unsigned long buf_size;
289
290         if (!str)
291                 return 0;
292         buf_size = memparse(str, &str);
293         /* nr_entries can not be zero */
294         if (buf_size == 0)
295                 return 0;
296         trace_buf_size = buf_size;
297         return 1;
298 }
299 __setup("trace_buf_size=", set_buf_size);
300
301 unsigned long nsecs_to_usecs(unsigned long nsecs)
302 {
303         return nsecs / 1000;
304 }
305
306 /* These must match the bit postions in trace_iterator_flags */
307 static const char *trace_options[] = {
308         "print-parent",
309         "sym-offset",
310         "sym-addr",
311         "verbose",
312         "raw",
313         "hex",
314         "bin",
315         "block",
316         "stacktrace",
317         "sched-tree",
318         "trace_printk",
319         "ftrace_preempt",
320         "branch",
321         "annotate",
322         "userstacktrace",
323         "sym-userobj",
324         "printk-msg-only",
325         "context-info",
326         "latency-format",
327         "sleep-time",
328         "graph-time",
329         NULL
330 };
331
332 static struct {
333         u64 (*func)(void);
334         const char *name;
335 } trace_clocks[] = {
336         { trace_clock_local,    "local" },
337         { trace_clock_global,   "global" },
338 };
339
340 int trace_clock_id;
341
342 /*
343  * trace_parser_get_init - gets the buffer for trace parser
344  */
345 int trace_parser_get_init(struct trace_parser *parser, int size)
346 {
347         memset(parser, 0, sizeof(*parser));
348
349         parser->buffer = kmalloc(size, GFP_KERNEL);
350         if (!parser->buffer)
351                 return 1;
352
353         parser->size = size;
354         return 0;
355 }
356
357 /*
358  * trace_parser_put - frees the buffer for trace parser
359  */
360 void trace_parser_put(struct trace_parser *parser)
361 {
362         kfree(parser->buffer);
363 }
364
365 /*
366  * trace_get_user - reads the user input string separated by  space
367  * (matched by isspace(ch))
368  *
369  * For each string found the 'struct trace_parser' is updated,
370  * and the function returns.
371  *
372  * Returns number of bytes read.
373  *
374  * See kernel/trace/trace.h for 'struct trace_parser' details.
375  */
376 int trace_get_user(struct trace_parser *parser, const char __user *ubuf,
377         size_t cnt, loff_t *ppos)
378 {
379         char ch;
380         size_t read = 0;
381         ssize_t ret;
382
383         if (!*ppos)
384                 trace_parser_clear(parser);
385
386         ret = get_user(ch, ubuf++);
387         if (ret)
388                 goto out;
389
390         read++;
391         cnt--;
392
393         /*
394          * The parser is not finished with the last write,
395          * continue reading the user input without skipping spaces.
396          */
397         if (!parser->cont) {
398                 /* skip white space */
399                 while (cnt && isspace(ch)) {
400                         ret = get_user(ch, ubuf++);
401                         if (ret)
402                                 goto out;
403                         read++;
404                         cnt--;
405                 }
406
407                 /* only spaces were written */
408                 if (isspace(ch)) {
409                         *ppos += read;
410                         ret = read;
411                         goto out;
412                 }
413
414                 parser->idx = 0;
415         }
416
417         /* read the non-space input */
418         while (cnt && !isspace(ch)) {
419                 if (parser->idx < parser->size)
420                         parser->buffer[parser->idx++] = ch;
421                 else {
422                         ret = -EINVAL;
423                         goto out;
424                 }
425                 ret = get_user(ch, ubuf++);
426                 if (ret)
427                         goto out;
428                 read++;
429                 cnt--;
430         }
431
432         /* We either got finished input or we have to wait for another call. */
433         if (isspace(ch)) {
434                 parser->buffer[parser->idx] = 0;
435                 parser->cont = false;
436         } else {
437                 parser->cont = true;
438                 parser->buffer[parser->idx++] = ch;
439         }
440
441         *ppos += read;
442         ret = read;
443
444 out:
445         return ret;
446 }
447
448 ssize_t trace_seq_to_user(struct trace_seq *s, char __user *ubuf, size_t cnt)
449 {
450         int len;
451         int ret;
452
453         if (!cnt)
454                 return 0;
455
456         if (s->len <= s->readpos)
457                 return -EBUSY;
458
459         len = s->len - s->readpos;
460         if (cnt > len)
461                 cnt = len;
462         ret = copy_to_user(ubuf, s->buffer + s->readpos, cnt);
463         if (ret == cnt)
464                 return -EFAULT;
465
466         cnt -= ret;
467
468         s->readpos += cnt;
469         return cnt;
470 }
471
472 static ssize_t trace_seq_to_buffer(struct trace_seq *s, void *buf, size_t cnt)
473 {
474         int len;
475         void *ret;
476
477         if (s->len <= s->readpos)
478                 return -EBUSY;
479
480         len = s->len - s->readpos;
481         if (cnt > len)
482                 cnt = len;
483         ret = memcpy(buf, s->buffer + s->readpos, cnt);
484         if (!ret)
485                 return -EFAULT;
486
487         s->readpos += cnt;
488         return cnt;
489 }
490
491 /*
492  * ftrace_max_lock is used to protect the swapping of buffers
493  * when taking a max snapshot. The buffers themselves are
494  * protected by per_cpu spinlocks. But the action of the swap
495  * needs its own lock.
496  *
497  * This is defined as a raw_spinlock_t in order to help
498  * with performance when lockdep debugging is enabled.
499  *
500  * It is also used in other places outside the update_max_tr
501  * so it needs to be defined outside of the
502  * CONFIG_TRACER_MAX_TRACE.
503  */
504 static raw_spinlock_t ftrace_max_lock =
505         (raw_spinlock_t)__RAW_SPIN_LOCK_UNLOCKED;
506
507 #ifdef CONFIG_TRACER_MAX_TRACE
508 unsigned long __read_mostly     tracing_max_latency;
509 unsigned long __read_mostly     tracing_thresh;
510
511 /*
512  * Copy the new maximum trace into the separate maximum-trace
513  * structure. (this way the maximum trace is permanently saved,
514  * for later retrieval via /sys/kernel/debug/tracing/latency_trace)
515  */
516 static void
517 __update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
518 {
519         struct trace_array_cpu *data = tr->data[cpu];
520         struct trace_array_cpu *max_data = tr->data[cpu];
521
522         max_tr.cpu = cpu;
523         max_tr.time_start = data->preempt_timestamp;
524
525         max_data = max_tr.data[cpu];
526         max_data->saved_latency = tracing_max_latency;
527         max_data->critical_start = data->critical_start;
528         max_data->critical_end = data->critical_end;
529
530         memcpy(data->comm, tsk->comm, TASK_COMM_LEN);
531         max_data->pid = tsk->pid;
532         max_data->uid = task_uid(tsk);
533         max_data->nice = tsk->static_prio - 20 - MAX_RT_PRIO;
534         max_data->policy = tsk->policy;
535         max_data->rt_priority = tsk->rt_priority;
536
537         /* record this tasks comm */
538         tracing_record_cmdline(tsk);
539 }
540
541 /**
542  * update_max_tr - snapshot all trace buffers from global_trace to max_tr
543  * @tr: tracer
544  * @tsk: the task with the latency
545  * @cpu: The cpu that initiated the trace.
546  *
547  * Flip the buffers between the @tr and the max_tr and record information
548  * about which task was the cause of this latency.
549  */
550 void
551 update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
552 {
553         struct ring_buffer *buf = tr->buffer;
554
555         if (trace_stop_count)
556                 return;
557
558         WARN_ON_ONCE(!irqs_disabled());
559         __raw_spin_lock(&ftrace_max_lock);
560
561         tr->buffer = max_tr.buffer;
562         max_tr.buffer = buf;
563
564         __update_max_tr(tr, tsk, cpu);
565         __raw_spin_unlock(&ftrace_max_lock);
566 }
567
568 /**
569  * update_max_tr_single - only copy one trace over, and reset the rest
570  * @tr - tracer
571  * @tsk - task with the latency
572  * @cpu - the cpu of the buffer to copy.
573  *
574  * Flip the trace of a single CPU buffer between the @tr and the max_tr.
575  */
576 void
577 update_max_tr_single(struct trace_array *tr, struct task_struct *tsk, int cpu)
578 {
579         int ret;
580
581         if (trace_stop_count)
582                 return;
583
584         WARN_ON_ONCE(!irqs_disabled());
585         __raw_spin_lock(&ftrace_max_lock);
586
587         ftrace_disable_cpu();
588
589         ret = ring_buffer_swap_cpu(max_tr.buffer, tr->buffer, cpu);
590
591         if (ret == -EBUSY) {
592                 /*
593                  * We failed to swap the buffer due to a commit taking
594                  * place on this CPU. We fail to record, but we reset
595                  * the max trace buffer (no one writes directly to it)
596                  * and flag that it failed.
597                  */
598                 trace_array_printk(&max_tr, _THIS_IP_,
599                         "Failed to swap buffers due to commit in progress\n");
600         }
601
602         ftrace_enable_cpu();
603
604         WARN_ON_ONCE(ret && ret != -EAGAIN && ret != -EBUSY);
605
606         __update_max_tr(tr, tsk, cpu);
607         __raw_spin_unlock(&ftrace_max_lock);
608 }
609 #endif /* CONFIG_TRACER_MAX_TRACE */
610
611 /**
612  * register_tracer - register a tracer with the ftrace system.
613  * @type - the plugin for the tracer
614  *
615  * Register a new plugin tracer.
616  */
617 int register_tracer(struct tracer *type)
618 __releases(kernel_lock)
619 __acquires(kernel_lock)
620 {
621         struct tracer *t;
622         int len;
623         int ret = 0;
624
625         if (!type->name) {
626                 pr_info("Tracer must have a name\n");
627                 return -1;
628         }
629
630         /*
631          * When this gets called we hold the BKL which means that
632          * preemption is disabled. Various trace selftests however
633          * need to disable and enable preemption for successful tests.
634          * So we drop the BKL here and grab it after the tests again.
635          */
636         unlock_kernel();
637         mutex_lock(&trace_types_lock);
638
639         tracing_selftest_running = true;
640
641         for (t = trace_types; t; t = t->next) {
642                 if (strcmp(type->name, t->name) == 0) {
643                         /* already found */
644                         pr_info("Trace %s already registered\n",
645                                 type->name);
646                         ret = -1;
647                         goto out;
648                 }
649         }
650
651         if (!type->set_flag)
652                 type->set_flag = &dummy_set_flag;
653         if (!type->flags)
654                 type->flags = &dummy_tracer_flags;
655         else
656                 if (!type->flags->opts)
657                         type->flags->opts = dummy_tracer_opt;
658         if (!type->wait_pipe)
659                 type->wait_pipe = default_wait_pipe;
660
661
662 #ifdef CONFIG_FTRACE_STARTUP_TEST
663         if (type->selftest && !tracing_selftest_disabled) {
664                 struct tracer *saved_tracer = current_trace;
665                 struct trace_array *tr = &global_trace;
666
667                 /*
668                  * Run a selftest on this tracer.
669                  * Here we reset the trace buffer, and set the current
670                  * tracer to be this tracer. The tracer can then run some
671                  * internal tracing to verify that everything is in order.
672                  * If we fail, we do not register this tracer.
673                  */
674                 tracing_reset_online_cpus(tr);
675
676                 current_trace = type;
677                 /* the test is responsible for initializing and enabling */
678                 pr_info("Testing tracer %s: ", type->name);
679                 ret = type->selftest(type, tr);
680                 /* the test is responsible for resetting too */
681                 current_trace = saved_tracer;
682                 if (ret) {
683                         printk(KERN_CONT "FAILED!\n");
684                         goto out;
685                 }
686                 /* Only reset on passing, to avoid touching corrupted buffers */
687                 tracing_reset_online_cpus(tr);
688
689                 printk(KERN_CONT "PASSED\n");
690         }
691 #endif
692
693         type->next = trace_types;
694         trace_types = type;
695         len = strlen(type->name);
696         if (len > max_tracer_type_len)
697                 max_tracer_type_len = len;
698
699  out:
700         tracing_selftest_running = false;
701         mutex_unlock(&trace_types_lock);
702
703         if (ret || !default_bootup_tracer)
704                 goto out_unlock;
705
706         if (strncmp(default_bootup_tracer, type->name, BOOTUP_TRACER_SIZE))
707                 goto out_unlock;
708
709         printk(KERN_INFO "Starting tracer '%s'\n", type->name);
710         /* Do we want this tracer to start on bootup? */
711         tracing_set_tracer(type->name);
712         default_bootup_tracer = NULL;
713         /* disable other selftests, since this will break it. */
714         tracing_selftest_disabled = 1;
715 #ifdef CONFIG_FTRACE_STARTUP_TEST
716         printk(KERN_INFO "Disabling FTRACE selftests due to running tracer '%s'\n",
717                type->name);
718 #endif
719
720  out_unlock:
721         lock_kernel();
722         return ret;
723 }
724
725 void unregister_tracer(struct tracer *type)
726 {
727         struct tracer **t;
728         int len;
729
730         mutex_lock(&trace_types_lock);
731         for (t = &trace_types; *t; t = &(*t)->next) {
732                 if (*t == type)
733                         goto found;
734         }
735         pr_info("Trace %s not registered\n", type->name);
736         goto out;
737
738  found:
739         *t = (*t)->next;
740
741         if (type == current_trace && tracer_enabled) {
742                 tracer_enabled = 0;
743                 tracing_stop();
744                 if (current_trace->stop)
745                         current_trace->stop(&global_trace);
746                 current_trace = &nop_trace;
747         }
748
749         if (strlen(type->name) != max_tracer_type_len)
750                 goto out;
751
752         max_tracer_type_len = 0;
753         for (t = &trace_types; *t; t = &(*t)->next) {
754                 len = strlen((*t)->name);
755                 if (len > max_tracer_type_len)
756                         max_tracer_type_len = len;
757         }
758  out:
759         mutex_unlock(&trace_types_lock);
760 }
761
762 static void __tracing_reset(struct trace_array *tr, int cpu)
763 {
764         ftrace_disable_cpu();
765         ring_buffer_reset_cpu(tr->buffer, cpu);
766         ftrace_enable_cpu();
767 }
768
769 void tracing_reset(struct trace_array *tr, int cpu)
770 {
771         struct ring_buffer *buffer = tr->buffer;
772
773         ring_buffer_record_disable(buffer);
774
775         /* Make sure all commits have finished */
776         synchronize_sched();
777         __tracing_reset(tr, cpu);
778
779         ring_buffer_record_enable(buffer);
780 }
781
782 void tracing_reset_online_cpus(struct trace_array *tr)
783 {
784         struct ring_buffer *buffer = tr->buffer;
785         int cpu;
786
787         ring_buffer_record_disable(buffer);
788
789         /* Make sure all commits have finished */
790         synchronize_sched();
791
792         tr->time_start = ftrace_now(tr->cpu);
793
794         for_each_online_cpu(cpu)
795                 __tracing_reset(tr, cpu);
796
797         ring_buffer_record_enable(buffer);
798 }
799
800 void tracing_reset_current(int cpu)
801 {
802         tracing_reset(&global_trace, cpu);
803 }
804
805 void tracing_reset_current_online_cpus(void)
806 {
807         tracing_reset_online_cpus(&global_trace);
808 }
809
810 #define SAVED_CMDLINES 128
811 #define NO_CMDLINE_MAP UINT_MAX
812 static unsigned map_pid_to_cmdline[PID_MAX_DEFAULT+1];
813 static unsigned map_cmdline_to_pid[SAVED_CMDLINES];
814 static char saved_cmdlines[SAVED_CMDLINES][TASK_COMM_LEN];
815 static int cmdline_idx;
816 static raw_spinlock_t trace_cmdline_lock = __RAW_SPIN_LOCK_UNLOCKED;
817
818 /* temporary disable recording */
819 static atomic_t trace_record_cmdline_disabled __read_mostly;
820
821 static void trace_init_cmdlines(void)
822 {
823         memset(&map_pid_to_cmdline, NO_CMDLINE_MAP, sizeof(map_pid_to_cmdline));
824         memset(&map_cmdline_to_pid, NO_CMDLINE_MAP, sizeof(map_cmdline_to_pid));
825         cmdline_idx = 0;
826 }
827
828 /**
829  * ftrace_off_permanent - disable all ftrace code permanently
830  *
831  * This should only be called when a serious anomally has
832  * been detected.  This will turn off the function tracing,
833  * ring buffers, and other tracing utilites. It takes no
834  * locks and can be called from any context.
835  */
836 void ftrace_off_permanent(void)
837 {
838         tracing_disabled = 1;
839         ftrace_stop();
840         tracing_off_permanent();
841 }
842
843 /**
844  * tracing_start - quick start of the tracer
845  *
846  * If tracing is enabled but was stopped by tracing_stop,
847  * this will start the tracer back up.
848  */
849 void tracing_start(void)
850 {
851         struct ring_buffer *buffer;
852         unsigned long flags;
853
854         if (tracing_disabled)
855                 return;
856
857         spin_lock_irqsave(&tracing_start_lock, flags);
858         if (--trace_stop_count) {
859                 if (trace_stop_count < 0) {
860                         /* Someone screwed up their debugging */
861                         WARN_ON_ONCE(1);
862                         trace_stop_count = 0;
863                 }
864                 goto out;
865         }
866
867
868         buffer = global_trace.buffer;
869         if (buffer)
870                 ring_buffer_record_enable(buffer);
871
872         buffer = max_tr.buffer;
873         if (buffer)
874                 ring_buffer_record_enable(buffer);
875
876         ftrace_start();
877  out:
878         spin_unlock_irqrestore(&tracing_start_lock, flags);
879 }
880
881 /**
882  * tracing_stop - quick stop of the tracer
883  *
884  * Light weight way to stop tracing. Use in conjunction with
885  * tracing_start.
886  */
887 void tracing_stop(void)
888 {
889         struct ring_buffer *buffer;
890         unsigned long flags;
891
892         ftrace_stop();
893         spin_lock_irqsave(&tracing_start_lock, flags);
894         if (trace_stop_count++)
895                 goto out;
896
897         buffer = global_trace.buffer;
898         if (buffer)
899                 ring_buffer_record_disable(buffer);
900
901         buffer = max_tr.buffer;
902         if (buffer)
903                 ring_buffer_record_disable(buffer);
904
905  out:
906         spin_unlock_irqrestore(&tracing_start_lock, flags);
907 }
908
909 void trace_stop_cmdline_recording(void);
910
911 static void trace_save_cmdline(struct task_struct *tsk)
912 {
913         unsigned pid, idx;
914
915         if (!tsk->pid || unlikely(tsk->pid > PID_MAX_DEFAULT))
916                 return;
917
918         /*
919          * It's not the end of the world if we don't get
920          * the lock, but we also don't want to spin
921          * nor do we want to disable interrupts,
922          * so if we miss here, then better luck next time.
923          */
924         if (!__raw_spin_trylock(&trace_cmdline_lock))
925                 return;
926
927         idx = map_pid_to_cmdline[tsk->pid];
928         if (idx == NO_CMDLINE_MAP) {
929                 idx = (cmdline_idx + 1) % SAVED_CMDLINES;
930
931                 /*
932                  * Check whether the cmdline buffer at idx has a pid
933                  * mapped. We are going to overwrite that entry so we
934                  * need to clear the map_pid_to_cmdline. Otherwise we
935                  * would read the new comm for the old pid.
936                  */
937                 pid = map_cmdline_to_pid[idx];
938                 if (pid != NO_CMDLINE_MAP)
939                         map_pid_to_cmdline[pid] = NO_CMDLINE_MAP;
940
941                 map_cmdline_to_pid[idx] = tsk->pid;
942                 map_pid_to_cmdline[tsk->pid] = idx;
943
944                 cmdline_idx = idx;
945         }
946
947         memcpy(&saved_cmdlines[idx], tsk->comm, TASK_COMM_LEN);
948
949         __raw_spin_unlock(&trace_cmdline_lock);
950 }
951
952 void trace_find_cmdline(int pid, char comm[])
953 {
954         unsigned map;
955
956         if (!pid) {
957                 strcpy(comm, "<idle>");
958                 return;
959         }
960
961         if (pid > PID_MAX_DEFAULT) {
962                 strcpy(comm, "<...>");
963                 return;
964         }
965
966         preempt_disable();
967         __raw_spin_lock(&trace_cmdline_lock);
968         map = map_pid_to_cmdline[pid];
969         if (map != NO_CMDLINE_MAP)
970                 strcpy(comm, saved_cmdlines[map]);
971         else
972                 strcpy(comm, "<...>");
973
974         __raw_spin_unlock(&trace_cmdline_lock);
975         preempt_enable();
976 }
977
978 void tracing_record_cmdline(struct task_struct *tsk)
979 {
980         if (atomic_read(&trace_record_cmdline_disabled) || !tracer_enabled ||
981             !tracing_is_on())
982                 return;
983
984         trace_save_cmdline(tsk);
985 }
986
987 void
988 tracing_generic_entry_update(struct trace_entry *entry, unsigned long flags,
989                              int pc)
990 {
991         struct task_struct *tsk = current;
992
993         entry->preempt_count            = pc & 0xff;
994         entry->pid                      = (tsk) ? tsk->pid : 0;
995         entry->lock_depth               = (tsk) ? tsk->lock_depth : 0;
996         entry->flags =
997 #ifdef CONFIG_TRACE_IRQFLAGS_SUPPORT
998                 (irqs_disabled_flags(flags) ? TRACE_FLAG_IRQS_OFF : 0) |
999 #else
1000                 TRACE_FLAG_IRQS_NOSUPPORT |
1001 #endif
1002                 ((pc & HARDIRQ_MASK) ? TRACE_FLAG_HARDIRQ : 0) |
1003                 ((pc & SOFTIRQ_MASK) ? TRACE_FLAG_SOFTIRQ : 0) |
1004                 (need_resched() ? TRACE_FLAG_NEED_RESCHED : 0);
1005 }
1006 EXPORT_SYMBOL_GPL(tracing_generic_entry_update);
1007
1008 struct ring_buffer_event *
1009 trace_buffer_lock_reserve(struct ring_buffer *buffer,
1010                           int type,
1011                           unsigned long len,
1012                           unsigned long flags, int pc)
1013 {
1014         struct ring_buffer_event *event;
1015
1016         event = ring_buffer_lock_reserve(buffer, len);
1017         if (event != NULL) {
1018                 struct trace_entry *ent = ring_buffer_event_data(event);
1019
1020                 tracing_generic_entry_update(ent, flags, pc);
1021                 ent->type = type;
1022         }
1023
1024         return event;
1025 }
1026
1027 static inline void
1028 __trace_buffer_unlock_commit(struct ring_buffer *buffer,
1029                              struct ring_buffer_event *event,
1030                              unsigned long flags, int pc,
1031                              int wake)
1032 {
1033         ring_buffer_unlock_commit(buffer, event);
1034
1035         ftrace_trace_stack(buffer, flags, 6, pc);
1036         ftrace_trace_userstack(buffer, flags, pc);
1037
1038         if (wake)
1039                 trace_wake_up();
1040 }
1041
1042 void trace_buffer_unlock_commit(struct ring_buffer *buffer,
1043                                 struct ring_buffer_event *event,
1044                                 unsigned long flags, int pc)
1045 {
1046         __trace_buffer_unlock_commit(buffer, event, flags, pc, 1);
1047 }
1048
1049 struct ring_buffer_event *
1050 trace_current_buffer_lock_reserve(struct ring_buffer **current_rb,
1051                                   int type, unsigned long len,
1052                                   unsigned long flags, int pc)
1053 {
1054         *current_rb = global_trace.buffer;
1055         return trace_buffer_lock_reserve(*current_rb,
1056                                          type, len, flags, pc);
1057 }
1058 EXPORT_SYMBOL_GPL(trace_current_buffer_lock_reserve);
1059
1060 void trace_current_buffer_unlock_commit(struct ring_buffer *buffer,
1061                                         struct ring_buffer_event *event,
1062                                         unsigned long flags, int pc)
1063 {
1064         __trace_buffer_unlock_commit(buffer, event, flags, pc, 1);
1065 }
1066 EXPORT_SYMBOL_GPL(trace_current_buffer_unlock_commit);
1067
1068 void trace_nowake_buffer_unlock_commit(struct ring_buffer *buffer,
1069                                        struct ring_buffer_event *event,
1070                                        unsigned long flags, int pc)
1071 {
1072         __trace_buffer_unlock_commit(buffer, event, flags, pc, 0);
1073 }
1074 EXPORT_SYMBOL_GPL(trace_nowake_buffer_unlock_commit);
1075
1076 void trace_current_buffer_discard_commit(struct ring_buffer *buffer,
1077                                          struct ring_buffer_event *event)
1078 {
1079         ring_buffer_discard_commit(buffer, event);
1080 }
1081 EXPORT_SYMBOL_GPL(trace_current_buffer_discard_commit);
1082
1083 void
1084 trace_function(struct trace_array *tr,
1085                unsigned long ip, unsigned long parent_ip, unsigned long flags,
1086                int pc)
1087 {
1088         struct ftrace_event_call *call = &event_function;
1089         struct ring_buffer *buffer = tr->buffer;
1090         struct ring_buffer_event *event;
1091         struct ftrace_entry *entry;
1092
1093         /* If we are reading the ring buffer, don't trace */
1094         if (unlikely(local_read(&__get_cpu_var(ftrace_cpu_disabled))))
1095                 return;
1096
1097         event = trace_buffer_lock_reserve(buffer, TRACE_FN, sizeof(*entry),
1098                                           flags, pc);
1099         if (!event)
1100                 return;
1101         entry   = ring_buffer_event_data(event);
1102         entry->ip                       = ip;
1103         entry->parent_ip                = parent_ip;
1104
1105         if (!filter_check_discard(call, entry, buffer, event))
1106                 ring_buffer_unlock_commit(buffer, event);
1107 }
1108
1109 void
1110 ftrace(struct trace_array *tr, struct trace_array_cpu *data,
1111        unsigned long ip, unsigned long parent_ip, unsigned long flags,
1112        int pc)
1113 {
1114         if (likely(!atomic_read(&data->disabled)))
1115                 trace_function(tr, ip, parent_ip, flags, pc);
1116 }
1117
1118 #ifdef CONFIG_STACKTRACE
1119 static void __ftrace_trace_stack(struct ring_buffer *buffer,
1120                                  unsigned long flags,
1121                                  int skip, int pc)
1122 {
1123         struct ftrace_event_call *call = &event_kernel_stack;
1124         struct ring_buffer_event *event;
1125         struct stack_entry *entry;
1126         struct stack_trace trace;
1127
1128         event = trace_buffer_lock_reserve(buffer, TRACE_STACK,
1129                                           sizeof(*entry), flags, pc);
1130         if (!event)
1131                 return;
1132         entry   = ring_buffer_event_data(event);
1133         memset(&entry->caller, 0, sizeof(entry->caller));
1134
1135         trace.nr_entries        = 0;
1136         trace.max_entries       = FTRACE_STACK_ENTRIES;
1137         trace.skip              = skip;
1138         trace.entries           = entry->caller;
1139
1140         save_stack_trace(&trace);
1141         if (!filter_check_discard(call, entry, buffer, event))
1142                 ring_buffer_unlock_commit(buffer, event);
1143 }
1144
1145 void ftrace_trace_stack(struct ring_buffer *buffer, unsigned long flags,
1146                         int skip, int pc)
1147 {
1148         if (!(trace_flags & TRACE_ITER_STACKTRACE))
1149                 return;
1150
1151         __ftrace_trace_stack(buffer, flags, skip, pc);
1152 }
1153
1154 void __trace_stack(struct trace_array *tr, unsigned long flags, int skip,
1155                    int pc)
1156 {
1157         __ftrace_trace_stack(tr->buffer, flags, skip, pc);
1158 }
1159
1160 void
1161 ftrace_trace_userstack(struct ring_buffer *buffer, unsigned long flags, int pc)
1162 {
1163         struct ftrace_event_call *call = &event_user_stack;
1164         struct ring_buffer_event *event;
1165         struct userstack_entry *entry;
1166         struct stack_trace trace;
1167
1168         if (!(trace_flags & TRACE_ITER_USERSTACKTRACE))
1169                 return;
1170
1171         event = trace_buffer_lock_reserve(buffer, TRACE_USER_STACK,
1172                                           sizeof(*entry), flags, pc);
1173         if (!event)
1174                 return;
1175         entry   = ring_buffer_event_data(event);
1176
1177         entry->tgid             = current->tgid;
1178         memset(&entry->caller, 0, sizeof(entry->caller));
1179
1180         trace.nr_entries        = 0;
1181         trace.max_entries       = FTRACE_STACK_ENTRIES;
1182         trace.skip              = 0;
1183         trace.entries           = entry->caller;
1184
1185         save_stack_trace_user(&trace);
1186         if (!filter_check_discard(call, entry, buffer, event))
1187                 ring_buffer_unlock_commit(buffer, event);
1188 }
1189
1190 #ifdef UNUSED
1191 static void __trace_userstack(struct trace_array *tr, unsigned long flags)
1192 {
1193         ftrace_trace_userstack(tr, flags, preempt_count());
1194 }
1195 #endif /* UNUSED */
1196
1197 #endif /* CONFIG_STACKTRACE */
1198
1199 static void
1200 ftrace_trace_special(void *__tr,
1201                      unsigned long arg1, unsigned long arg2, unsigned long arg3,
1202                      int pc)
1203 {
1204         struct ring_buffer_event *event;
1205         struct trace_array *tr = __tr;
1206         struct ring_buffer *buffer = tr->buffer;
1207         struct special_entry *entry;
1208
1209         event = trace_buffer_lock_reserve(buffer, TRACE_SPECIAL,
1210                                           sizeof(*entry), 0, pc);
1211         if (!event)
1212                 return;
1213         entry   = ring_buffer_event_data(event);
1214         entry->arg1                     = arg1;
1215         entry->arg2                     = arg2;
1216         entry->arg3                     = arg3;
1217         trace_buffer_unlock_commit(buffer, event, 0, pc);
1218 }
1219
1220 void
1221 __trace_special(void *__tr, void *__data,
1222                 unsigned long arg1, unsigned long arg2, unsigned long arg3)
1223 {
1224         ftrace_trace_special(__tr, arg1, arg2, arg3, preempt_count());
1225 }
1226
1227 void
1228 ftrace_special(unsigned long arg1, unsigned long arg2, unsigned long arg3)
1229 {
1230         struct trace_array *tr = &global_trace;
1231         struct trace_array_cpu *data;
1232         unsigned long flags;
1233         int cpu;
1234         int pc;
1235
1236         if (tracing_disabled)
1237                 return;
1238
1239         pc = preempt_count();
1240         local_irq_save(flags);
1241         cpu = raw_smp_processor_id();
1242         data = tr->data[cpu];
1243
1244         if (likely(atomic_inc_return(&data->disabled) == 1))
1245                 ftrace_trace_special(tr, arg1, arg2, arg3, pc);
1246
1247         atomic_dec(&data->disabled);
1248         local_irq_restore(flags);
1249 }
1250
1251 /**
1252  * trace_vbprintk - write binary msg to tracing buffer
1253  *
1254  */
1255 int trace_vbprintk(unsigned long ip, const char *fmt, va_list args)
1256 {
1257         static raw_spinlock_t trace_buf_lock =
1258                 (raw_spinlock_t)__RAW_SPIN_LOCK_UNLOCKED;
1259         static u32 trace_buf[TRACE_BUF_SIZE];
1260
1261         struct ftrace_event_call *call = &event_bprint;
1262         struct ring_buffer_event *event;
1263         struct ring_buffer *buffer;
1264         struct trace_array *tr = &global_trace;
1265         struct trace_array_cpu *data;
1266         struct bprint_entry *entry;
1267         unsigned long flags;
1268         int disable;
1269         int resched;
1270         int cpu, len = 0, size, pc;
1271
1272         if (unlikely(tracing_selftest_running || tracing_disabled))
1273                 return 0;
1274
1275         /* Don't pollute graph traces with trace_vprintk internals */
1276         pause_graph_tracing();
1277
1278         pc = preempt_count();
1279         resched = ftrace_preempt_disable();
1280         cpu = raw_smp_processor_id();
1281         data = tr->data[cpu];
1282
1283         disable = atomic_inc_return(&data->disabled);
1284         if (unlikely(disable != 1))
1285                 goto out;
1286
1287         /* Lockdep uses trace_printk for lock tracing */
1288         local_irq_save(flags);
1289         __raw_spin_lock(&trace_buf_lock);
1290         len = vbin_printf(trace_buf, TRACE_BUF_SIZE, fmt, args);
1291
1292         if (len > TRACE_BUF_SIZE || len < 0)
1293                 goto out_unlock;
1294
1295         size = sizeof(*entry) + sizeof(u32) * len;
1296         buffer = tr->buffer;
1297         event = trace_buffer_lock_reserve(buffer, TRACE_BPRINT, size,
1298                                           flags, pc);
1299         if (!event)
1300                 goto out_unlock;
1301         entry = ring_buffer_event_data(event);
1302         entry->ip                       = ip;
1303         entry->fmt                      = fmt;
1304
1305         memcpy(entry->buf, trace_buf, sizeof(u32) * len);
1306         if (!filter_check_discard(call, entry, buffer, event))
1307                 ring_buffer_unlock_commit(buffer, event);
1308
1309 out_unlock:
1310         __raw_spin_unlock(&trace_buf_lock);
1311         local_irq_restore(flags);
1312
1313 out:
1314         atomic_dec_return(&data->disabled);
1315         ftrace_preempt_enable(resched);
1316         unpause_graph_tracing();
1317
1318         return len;
1319 }
1320 EXPORT_SYMBOL_GPL(trace_vbprintk);
1321
1322 int trace_array_printk(struct trace_array *tr,
1323                        unsigned long ip, const char *fmt, ...)
1324 {
1325         int ret;
1326         va_list ap;
1327
1328         if (!(trace_flags & TRACE_ITER_PRINTK))
1329                 return 0;
1330
1331         va_start(ap, fmt);
1332         ret = trace_array_vprintk(tr, ip, fmt, ap);
1333         va_end(ap);
1334         return ret;
1335 }
1336
1337 int trace_array_vprintk(struct trace_array *tr,
1338                         unsigned long ip, const char *fmt, va_list args)
1339 {
1340         static raw_spinlock_t trace_buf_lock = __RAW_SPIN_LOCK_UNLOCKED;
1341         static char trace_buf[TRACE_BUF_SIZE];
1342
1343         struct ftrace_event_call *call = &event_print;
1344         struct ring_buffer_event *event;
1345         struct ring_buffer *buffer;
1346         struct trace_array_cpu *data;
1347         int cpu, len = 0, size, pc;
1348         struct print_entry *entry;
1349         unsigned long irq_flags;
1350         int disable;
1351
1352         if (tracing_disabled || tracing_selftest_running)
1353                 return 0;
1354
1355         pc = preempt_count();
1356         preempt_disable_notrace();
1357         cpu = raw_smp_processor_id();
1358         data = tr->data[cpu];
1359
1360         disable = atomic_inc_return(&data->disabled);
1361         if (unlikely(disable != 1))
1362                 goto out;
1363
1364         pause_graph_tracing();
1365         raw_local_irq_save(irq_flags);
1366         __raw_spin_lock(&trace_buf_lock);
1367         len = vsnprintf(trace_buf, TRACE_BUF_SIZE, fmt, args);
1368
1369         len = min(len, TRACE_BUF_SIZE-1);
1370         trace_buf[len] = 0;
1371
1372         size = sizeof(*entry) + len + 1;
1373         buffer = tr->buffer;
1374         event = trace_buffer_lock_reserve(buffer, TRACE_PRINT, size,
1375                                           irq_flags, pc);
1376         if (!event)
1377                 goto out_unlock;
1378         entry = ring_buffer_event_data(event);
1379         entry->ip                       = ip;
1380
1381         memcpy(&entry->buf, trace_buf, len);
1382         entry->buf[len] = 0;
1383         if (!filter_check_discard(call, entry, buffer, event))
1384                 ring_buffer_unlock_commit(buffer, event);
1385
1386  out_unlock:
1387         __raw_spin_unlock(&trace_buf_lock);
1388         raw_local_irq_restore(irq_flags);
1389         unpause_graph_tracing();
1390  out:
1391         atomic_dec_return(&data->disabled);
1392         preempt_enable_notrace();
1393
1394         return len;
1395 }
1396
1397 int trace_vprintk(unsigned long ip, const char *fmt, va_list args)
1398 {
1399         return trace_array_printk(&global_trace, ip, fmt, args);
1400 }
1401 EXPORT_SYMBOL_GPL(trace_vprintk);
1402
1403 enum trace_file_type {
1404         TRACE_FILE_LAT_FMT      = 1,
1405         TRACE_FILE_ANNOTATE     = 2,
1406 };
1407
1408 static void trace_iterator_increment(struct trace_iterator *iter)
1409 {
1410         /* Don't allow ftrace to trace into the ring buffers */
1411         ftrace_disable_cpu();
1412
1413         iter->idx++;
1414         if (iter->buffer_iter[iter->cpu])
1415                 ring_buffer_read(iter->buffer_iter[iter->cpu], NULL);
1416
1417         ftrace_enable_cpu();
1418 }
1419
1420 static struct trace_entry *
1421 peek_next_entry(struct trace_iterator *iter, int cpu, u64 *ts)
1422 {
1423         struct ring_buffer_event *event;
1424         struct ring_buffer_iter *buf_iter = iter->buffer_iter[cpu];
1425
1426         /* Don't allow ftrace to trace into the ring buffers */
1427         ftrace_disable_cpu();
1428
1429         if (buf_iter)
1430                 event = ring_buffer_iter_peek(buf_iter, ts);
1431         else
1432                 event = ring_buffer_peek(iter->tr->buffer, cpu, ts);
1433
1434         ftrace_enable_cpu();
1435
1436         return event ? ring_buffer_event_data(event) : NULL;
1437 }
1438
1439 static struct trace_entry *
1440 __find_next_entry(struct trace_iterator *iter, int *ent_cpu, u64 *ent_ts)
1441 {
1442         struct ring_buffer *buffer = iter->tr->buffer;
1443         struct trace_entry *ent, *next = NULL;
1444         int cpu_file = iter->cpu_file;
1445         u64 next_ts = 0, ts;
1446         int next_cpu = -1;
1447         int cpu;
1448
1449         /*
1450          * If we are in a per_cpu trace file, don't bother by iterating over
1451          * all cpu and peek directly.
1452          */
1453         if (cpu_file > TRACE_PIPE_ALL_CPU) {
1454                 if (ring_buffer_empty_cpu(buffer, cpu_file))
1455                         return NULL;
1456                 ent = peek_next_entry(iter, cpu_file, ent_ts);
1457                 if (ent_cpu)
1458                         *ent_cpu = cpu_file;
1459
1460                 return ent;
1461         }
1462
1463         for_each_tracing_cpu(cpu) {
1464
1465                 if (ring_buffer_empty_cpu(buffer, cpu))
1466                         continue;
1467
1468                 ent = peek_next_entry(iter, cpu, &ts);
1469
1470                 /*
1471                  * Pick the entry with the smallest timestamp:
1472                  */
1473                 if (ent && (!next || ts < next_ts)) {
1474                         next = ent;
1475                         next_cpu = cpu;
1476                         next_ts = ts;
1477                 }
1478         }
1479
1480         if (ent_cpu)
1481                 *ent_cpu = next_cpu;
1482
1483         if (ent_ts)
1484                 *ent_ts = next_ts;
1485
1486         return next;
1487 }
1488
1489 /* Find the next real entry, without updating the iterator itself */
1490 struct trace_entry *trace_find_next_entry(struct trace_iterator *iter,
1491                                           int *ent_cpu, u64 *ent_ts)
1492 {
1493         return __find_next_entry(iter, ent_cpu, ent_ts);
1494 }
1495
1496 /* Find the next real entry, and increment the iterator to the next entry */
1497 static void *find_next_entry_inc(struct trace_iterator *iter)
1498 {
1499         iter->ent = __find_next_entry(iter, &iter->cpu, &iter->ts);
1500
1501         if (iter->ent)
1502                 trace_iterator_increment(iter);
1503
1504         return iter->ent ? iter : NULL;
1505 }
1506
1507 static void trace_consume(struct trace_iterator *iter)
1508 {
1509         /* Don't allow ftrace to trace into the ring buffers */
1510         ftrace_disable_cpu();
1511         ring_buffer_consume(iter->tr->buffer, iter->cpu, &iter->ts);
1512         ftrace_enable_cpu();
1513 }
1514
1515 static void *s_next(struct seq_file *m, void *v, loff_t *pos)
1516 {
1517         struct trace_iterator *iter = m->private;
1518         int i = (int)*pos;
1519         void *ent;
1520
1521         (*pos)++;
1522
1523         /* can't go backwards */
1524         if (iter->idx > i)
1525                 return NULL;
1526
1527         if (iter->idx < 0)
1528                 ent = find_next_entry_inc(iter);
1529         else
1530                 ent = iter;
1531
1532         while (ent && iter->idx < i)
1533                 ent = find_next_entry_inc(iter);
1534
1535         iter->pos = *pos;
1536
1537         return ent;
1538 }
1539
1540 static void tracing_iter_reset(struct trace_iterator *iter, int cpu)
1541 {
1542         struct trace_array *tr = iter->tr;
1543         struct ring_buffer_event *event;
1544         struct ring_buffer_iter *buf_iter;
1545         unsigned long entries = 0;
1546         u64 ts;
1547
1548         tr->data[cpu]->skipped_entries = 0;
1549
1550         if (!iter->buffer_iter[cpu])
1551                 return;
1552
1553         buf_iter = iter->buffer_iter[cpu];
1554         ring_buffer_iter_reset(buf_iter);
1555
1556         /*
1557          * We could have the case with the max latency tracers
1558          * that a reset never took place on a cpu. This is evident
1559          * by the timestamp being before the start of the buffer.
1560          */
1561         while ((event = ring_buffer_iter_peek(buf_iter, &ts))) {
1562                 if (ts >= iter->tr->time_start)
1563                         break;
1564                 entries++;
1565                 ring_buffer_read(buf_iter, NULL);
1566         }
1567
1568         tr->data[cpu]->skipped_entries = entries;
1569 }
1570
1571 /*
1572  * No necessary locking here. The worst thing which can
1573  * happen is loosing events consumed at the same time
1574  * by a trace_pipe reader.
1575  * Other than that, we don't risk to crash the ring buffer
1576  * because it serializes the readers.
1577  *
1578  * The current tracer is copied to avoid a global locking
1579  * all around.
1580  */
1581 static void *s_start(struct seq_file *m, loff_t *pos)
1582 {
1583         struct trace_iterator *iter = m->private;
1584         static struct tracer *old_tracer;
1585         int cpu_file = iter->cpu_file;
1586         void *p = NULL;
1587         loff_t l = 0;
1588         int cpu;
1589
1590         /* copy the tracer to avoid using a global lock all around */
1591         mutex_lock(&trace_types_lock);
1592         if (unlikely(old_tracer != current_trace && current_trace)) {
1593                 old_tracer = current_trace;
1594                 *iter->trace = *current_trace;
1595         }
1596         mutex_unlock(&trace_types_lock);
1597
1598         atomic_inc(&trace_record_cmdline_disabled);
1599
1600         if (*pos != iter->pos) {
1601                 iter->ent = NULL;
1602                 iter->cpu = 0;
1603                 iter->idx = -1;
1604
1605                 ftrace_disable_cpu();
1606
1607                 if (cpu_file == TRACE_PIPE_ALL_CPU) {
1608                         for_each_tracing_cpu(cpu)
1609                                 tracing_iter_reset(iter, cpu);
1610                 } else
1611                         tracing_iter_reset(iter, cpu_file);
1612
1613                 ftrace_enable_cpu();
1614
1615                 for (p = iter; p && l < *pos; p = s_next(m, p, &l))
1616                         ;
1617
1618         } else {
1619                 l = *pos - 1;
1620                 p = s_next(m, p, &l);
1621         }
1622
1623         trace_event_read_lock();
1624         return p;
1625 }
1626
1627 static void s_stop(struct seq_file *m, void *p)
1628 {
1629         atomic_dec(&trace_record_cmdline_disabled);
1630         trace_event_read_unlock();
1631 }
1632
1633 static void print_lat_help_header(struct seq_file *m)
1634 {
1635         seq_puts(m, "#                  _------=> CPU#            \n");
1636         seq_puts(m, "#                 / _-----=> irqs-off        \n");
1637         seq_puts(m, "#                | / _----=> need-resched    \n");
1638         seq_puts(m, "#                || / _---=> hardirq/softirq \n");
1639         seq_puts(m, "#                ||| / _--=> preempt-depth   \n");
1640         seq_puts(m, "#                |||| /_--=> lock-depth       \n");
1641         seq_puts(m, "#                |||||/     delay             \n");
1642         seq_puts(m, "#  cmd     pid   |||||| time  |   caller      \n");
1643         seq_puts(m, "#     \\   /      ||||||   \\   |   /           \n");
1644 }
1645
1646 static void print_func_help_header(struct seq_file *m)
1647 {
1648         seq_puts(m, "#           TASK-PID    CPU#    TIMESTAMP  FUNCTION\n");
1649         seq_puts(m, "#              | |       |          |         |\n");
1650 }
1651
1652
1653 static void
1654 print_trace_header(struct seq_file *m, struct trace_iterator *iter)
1655 {
1656         unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
1657         struct trace_array *tr = iter->tr;
1658         struct trace_array_cpu *data = tr->data[tr->cpu];
1659         struct tracer *type = current_trace;
1660         unsigned long entries = 0;
1661         unsigned long total = 0;
1662         unsigned long count;
1663         const char *name = "preemption";
1664         int cpu;
1665
1666         if (type)
1667                 name = type->name;
1668
1669
1670         for_each_tracing_cpu(cpu) {
1671                 count = ring_buffer_entries_cpu(tr->buffer, cpu);
1672                 /*
1673                  * If this buffer has skipped entries, then we hold all
1674                  * entries for the trace and we need to ignore the
1675                  * ones before the time stamp.
1676                  */
1677                 if (tr->data[cpu]->skipped_entries) {
1678                         count -= tr->data[cpu]->skipped_entries;
1679                         /* total is the same as the entries */
1680                         total += count;
1681                 } else
1682                         total += count +
1683                                 ring_buffer_overrun_cpu(tr->buffer, cpu);
1684                 entries += count;
1685         }
1686
1687         seq_printf(m, "# %s latency trace v1.1.5 on %s\n",
1688                    name, UTS_RELEASE);
1689         seq_puts(m, "# -----------------------------------"
1690                  "---------------------------------\n");
1691         seq_printf(m, "# latency: %lu us, #%lu/%lu, CPU#%d |"
1692                    " (M:%s VP:%d, KP:%d, SP:%d HP:%d",
1693                    nsecs_to_usecs(data->saved_latency),
1694                    entries,
1695                    total,
1696                    tr->cpu,
1697 #if defined(CONFIG_PREEMPT_NONE)
1698                    "server",
1699 #elif defined(CONFIG_PREEMPT_VOLUNTARY)
1700                    "desktop",
1701 #elif defined(CONFIG_PREEMPT)
1702                    "preempt",
1703 #else
1704                    "unknown",
1705 #endif
1706                    /* These are reserved for later use */
1707                    0, 0, 0, 0);
1708 #ifdef CONFIG_SMP
1709         seq_printf(m, " #P:%d)\n", num_online_cpus());
1710 #else
1711         seq_puts(m, ")\n");
1712 #endif
1713         seq_puts(m, "#    -----------------\n");
1714         seq_printf(m, "#    | task: %.16s-%d "
1715                    "(uid:%d nice:%ld policy:%ld rt_prio:%ld)\n",
1716                    data->comm, data->pid, data->uid, data->nice,
1717                    data->policy, data->rt_priority);
1718         seq_puts(m, "#    -----------------\n");
1719
1720         if (data->critical_start) {
1721                 seq_puts(m, "#  => started at: ");
1722                 seq_print_ip_sym(&iter->seq, data->critical_start, sym_flags);
1723                 trace_print_seq(m, &iter->seq);
1724                 seq_puts(m, "\n#  => ended at:   ");
1725                 seq_print_ip_sym(&iter->seq, data->critical_end, sym_flags);
1726                 trace_print_seq(m, &iter->seq);
1727                 seq_puts(m, "\n#\n");
1728         }
1729
1730         seq_puts(m, "#\n");
1731 }
1732
1733 static void test_cpu_buff_start(struct trace_iterator *iter)
1734 {
1735         struct trace_seq *s = &iter->seq;
1736
1737         if (!(trace_flags & TRACE_ITER_ANNOTATE))
1738                 return;
1739
1740         if (!(iter->iter_flags & TRACE_FILE_ANNOTATE))
1741                 return;
1742
1743         if (cpumask_test_cpu(iter->cpu, iter->started))
1744                 return;
1745
1746         if (iter->tr->data[iter->cpu]->skipped_entries)
1747                 return;
1748
1749         cpumask_set_cpu(iter->cpu, iter->started);
1750
1751         /* Don't print started cpu buffer for the first entry of the trace */
1752         if (iter->idx > 1)
1753                 trace_seq_printf(s, "##### CPU %u buffer started ####\n",
1754                                 iter->cpu);
1755 }
1756
1757 static enum print_line_t print_trace_fmt(struct trace_iterator *iter)
1758 {
1759         struct trace_seq *s = &iter->seq;
1760         unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
1761         struct trace_entry *entry;
1762         struct trace_event *event;
1763
1764         entry = iter->ent;
1765
1766         test_cpu_buff_start(iter);
1767
1768         event = ftrace_find_event(entry->type);
1769
1770         if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
1771                 if (iter->iter_flags & TRACE_FILE_LAT_FMT) {
1772                         if (!trace_print_lat_context(iter))
1773                                 goto partial;
1774                 } else {
1775                         if (!trace_print_context(iter))
1776                                 goto partial;
1777                 }
1778         }
1779
1780         if (event)
1781                 return event->trace(iter, sym_flags);
1782
1783         if (!trace_seq_printf(s, "Unknown type %d\n", entry->type))
1784                 goto partial;
1785
1786         return TRACE_TYPE_HANDLED;
1787 partial:
1788         return TRACE_TYPE_PARTIAL_LINE;
1789 }
1790
1791 static enum print_line_t print_raw_fmt(struct trace_iterator *iter)
1792 {
1793         struct trace_seq *s = &iter->seq;
1794         struct trace_entry *entry;
1795         struct trace_event *event;
1796
1797         entry = iter->ent;
1798
1799         if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
1800                 if (!trace_seq_printf(s, "%d %d %llu ",
1801                                       entry->pid, iter->cpu, iter->ts))
1802                         goto partial;
1803         }
1804
1805         event = ftrace_find_event(entry->type);
1806         if (event)
1807                 return event->raw(iter, 0);
1808
1809         if (!trace_seq_printf(s, "%d ?\n", entry->type))
1810                 goto partial;
1811
1812         return TRACE_TYPE_HANDLED;
1813 partial:
1814         return TRACE_TYPE_PARTIAL_LINE;
1815 }
1816
1817 static enum print_line_t print_hex_fmt(struct trace_iterator *iter)
1818 {
1819         struct trace_seq *s = &iter->seq;
1820         unsigned char newline = '\n';
1821         struct trace_entry *entry;
1822         struct trace_event *event;
1823
1824         entry = iter->ent;
1825
1826         if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
1827                 SEQ_PUT_HEX_FIELD_RET(s, entry->pid);
1828                 SEQ_PUT_HEX_FIELD_RET(s, iter->cpu);
1829                 SEQ_PUT_HEX_FIELD_RET(s, iter->ts);
1830         }
1831
1832         event = ftrace_find_event(entry->type);
1833         if (event) {
1834                 enum print_line_t ret = event->hex(iter, 0);
1835                 if (ret != TRACE_TYPE_HANDLED)
1836                         return ret;
1837         }
1838
1839         SEQ_PUT_FIELD_RET(s, newline);
1840
1841         return TRACE_TYPE_HANDLED;
1842 }
1843
1844 static enum print_line_t print_bin_fmt(struct trace_iterator *iter)
1845 {
1846         struct trace_seq *s = &iter->seq;
1847         struct trace_entry *entry;
1848         struct trace_event *event;
1849
1850         entry = iter->ent;
1851
1852         if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
1853                 SEQ_PUT_FIELD_RET(s, entry->pid);
1854                 SEQ_PUT_FIELD_RET(s, iter->cpu);
1855                 SEQ_PUT_FIELD_RET(s, iter->ts);
1856         }
1857
1858         event = ftrace_find_event(entry->type);
1859         return event ? event->binary(iter, 0) : TRACE_TYPE_HANDLED;
1860 }
1861
1862 static int trace_empty(struct trace_iterator *iter)
1863 {
1864         int cpu;
1865
1866         /* If we are looking at one CPU buffer, only check that one */
1867         if (iter->cpu_file != TRACE_PIPE_ALL_CPU) {
1868                 cpu = iter->cpu_file;
1869                 if (iter->buffer_iter[cpu]) {
1870                         if (!ring_buffer_iter_empty(iter->buffer_iter[cpu]))
1871                                 return 0;
1872                 } else {
1873                         if (!ring_buffer_empty_cpu(iter->tr->buffer, cpu))
1874                                 return 0;
1875                 }
1876                 return 1;
1877         }
1878
1879         for_each_tracing_cpu(cpu) {
1880                 if (iter->buffer_iter[cpu]) {
1881                         if (!ring_buffer_iter_empty(iter->buffer_iter[cpu]))
1882                                 return 0;
1883                 } else {
1884                         if (!ring_buffer_empty_cpu(iter->tr->buffer, cpu))
1885                                 return 0;
1886                 }
1887         }
1888
1889         return 1;
1890 }
1891
1892 /*  Called with trace_event_read_lock() held. */
1893 static enum print_line_t print_trace_line(struct trace_iterator *iter)
1894 {
1895         enum print_line_t ret;
1896
1897         if (iter->trace && iter->trace->print_line) {
1898                 ret = iter->trace->print_line(iter);
1899                 if (ret != TRACE_TYPE_UNHANDLED)
1900                         return ret;
1901         }
1902
1903         if (iter->ent->type == TRACE_BPRINT &&
1904                         trace_flags & TRACE_ITER_PRINTK &&
1905                         trace_flags & TRACE_ITER_PRINTK_MSGONLY)
1906                 return trace_print_bprintk_msg_only(iter);
1907
1908         if (iter->ent->type == TRACE_PRINT &&
1909                         trace_flags & TRACE_ITER_PRINTK &&
1910                         trace_flags & TRACE_ITER_PRINTK_MSGONLY)
1911                 return trace_print_printk_msg_only(iter);
1912
1913         if (trace_flags & TRACE_ITER_BIN)
1914                 return print_bin_fmt(iter);
1915
1916         if (trace_flags & TRACE_ITER_HEX)
1917                 return print_hex_fmt(iter);
1918
1919         if (trace_flags & TRACE_ITER_RAW)
1920                 return print_raw_fmt(iter);
1921
1922         return print_trace_fmt(iter);
1923 }
1924
1925 static int s_show(struct seq_file *m, void *v)
1926 {
1927         struct trace_iterator *iter = v;
1928
1929         if (iter->ent == NULL) {
1930                 if (iter->tr) {
1931                         seq_printf(m, "# tracer: %s\n", iter->trace->name);
1932                         seq_puts(m, "#\n");
1933                 }
1934                 if (iter->trace && iter->trace->print_header)
1935                         iter->trace->print_header(m);
1936                 else if (iter->iter_flags & TRACE_FILE_LAT_FMT) {
1937                         /* print nothing if the buffers are empty */
1938                         if (trace_empty(iter))
1939                                 return 0;
1940                         print_trace_header(m, iter);
1941                         if (!(trace_flags & TRACE_ITER_VERBOSE))
1942                                 print_lat_help_header(m);
1943                 } else {
1944                         if (!(trace_flags & TRACE_ITER_VERBOSE))
1945                                 print_func_help_header(m);
1946                 }
1947         } else {
1948                 print_trace_line(iter);
1949                 trace_print_seq(m, &iter->seq);
1950         }
1951
1952         return 0;
1953 }
1954
1955 static struct seq_operations tracer_seq_ops = {
1956         .start          = s_start,
1957         .next           = s_next,
1958         .stop           = s_stop,
1959         .show           = s_show,
1960 };
1961
1962 static struct trace_iterator *
1963 __tracing_open(struct inode *inode, struct file *file)
1964 {
1965         long cpu_file = (long) inode->i_private;
1966         void *fail_ret = ERR_PTR(-ENOMEM);
1967         struct trace_iterator *iter;
1968         struct seq_file *m;
1969         int cpu, ret;
1970
1971         if (tracing_disabled)
1972                 return ERR_PTR(-ENODEV);
1973
1974         iter = kzalloc(sizeof(*iter), GFP_KERNEL);
1975         if (!iter)
1976                 return ERR_PTR(-ENOMEM);
1977
1978         /*
1979          * We make a copy of the current tracer to avoid concurrent
1980          * changes on it while we are reading.
1981          */
1982         mutex_lock(&trace_types_lock);
1983         iter->trace = kzalloc(sizeof(*iter->trace), GFP_KERNEL);
1984         if (!iter->trace)
1985                 goto fail;
1986
1987         if (current_trace)
1988                 *iter->trace = *current_trace;
1989
1990         if (!alloc_cpumask_var(&iter->started, GFP_KERNEL))
1991                 goto fail;
1992
1993         cpumask_clear(iter->started);
1994
1995         if (current_trace && current_trace->print_max)
1996                 iter->tr = &max_tr;
1997         else
1998                 iter->tr = &global_trace;
1999         iter->pos = -1;
2000         mutex_init(&iter->mutex);
2001         iter->cpu_file = cpu_file;
2002
2003         /* Notify the tracer early; before we stop tracing. */
2004         if (iter->trace && iter->trace->open)
2005                 iter->trace->open(iter);
2006
2007         /* Annotate start of buffers if we had overruns */
2008         if (ring_buffer_overruns(iter->tr->buffer))
2009                 iter->iter_flags |= TRACE_FILE_ANNOTATE;
2010
2011         /* stop the trace while dumping */
2012         tracing_stop();
2013
2014         if (iter->cpu_file == TRACE_PIPE_ALL_CPU) {
2015                 for_each_tracing_cpu(cpu) {
2016
2017                         iter->buffer_iter[cpu] =
2018                                 ring_buffer_read_start(iter->tr->buffer, cpu);
2019                         tracing_iter_reset(iter, cpu);
2020                 }
2021         } else {
2022                 cpu = iter->cpu_file;
2023                 iter->buffer_iter[cpu] =
2024                                 ring_buffer_read_start(iter->tr->buffer, cpu);
2025                 tracing_iter_reset(iter, cpu);
2026         }
2027
2028         ret = seq_open(file, &tracer_seq_ops);
2029         if (ret < 0) {
2030                 fail_ret = ERR_PTR(ret);
2031                 goto fail_buffer;
2032         }
2033
2034         m = file->private_data;
2035         m->private = iter;
2036
2037         mutex_unlock(&trace_types_lock);
2038
2039         return iter;
2040
2041  fail_buffer:
2042         for_each_tracing_cpu(cpu) {
2043                 if (iter->buffer_iter[cpu])
2044                         ring_buffer_read_finish(iter->buffer_iter[cpu]);
2045         }
2046         free_cpumask_var(iter->started);
2047         tracing_start();
2048  fail:
2049         mutex_unlock(&trace_types_lock);
2050         kfree(iter->trace);
2051         kfree(iter);
2052
2053         return fail_ret;
2054 }
2055
2056 int tracing_open_generic(struct inode *inode, struct file *filp)
2057 {
2058         if (tracing_disabled)
2059                 return -ENODEV;
2060
2061         filp->private_data = inode->i_private;
2062         return 0;
2063 }
2064
2065 static int tracing_release(struct inode *inode, struct file *file)
2066 {
2067         struct seq_file *m = (struct seq_file *)file->private_data;
2068         struct trace_iterator *iter;
2069         int cpu;
2070
2071         if (!(file->f_mode & FMODE_READ))
2072                 return 0;
2073
2074         iter = m->private;
2075
2076         mutex_lock(&trace_types_lock);
2077         for_each_tracing_cpu(cpu) {
2078                 if (iter->buffer_iter[cpu])
2079                         ring_buffer_read_finish(iter->buffer_iter[cpu]);
2080         }
2081
2082         if (iter->trace && iter->trace->close)
2083                 iter->trace->close(iter);
2084
2085         /* reenable tracing if it was previously enabled */
2086         tracing_start();
2087         mutex_unlock(&trace_types_lock);
2088
2089         seq_release(inode, file);
2090         mutex_destroy(&iter->mutex);
2091         free_cpumask_var(iter->started);
2092         kfree(iter->trace);
2093         kfree(iter);
2094         return 0;
2095 }
2096
2097 static int tracing_open(struct inode *inode, struct file *file)
2098 {
2099         struct trace_iterator *iter;
2100         int ret = 0;
2101
2102         /* If this file was open for write, then erase contents */
2103         if ((file->f_mode & FMODE_WRITE) &&
2104             (file->f_flags & O_TRUNC)) {
2105                 long cpu = (long) inode->i_private;
2106
2107                 if (cpu == TRACE_PIPE_ALL_CPU)
2108                         tracing_reset_online_cpus(&global_trace);
2109                 else
2110                         tracing_reset(&global_trace, cpu);
2111         }
2112
2113         if (file->f_mode & FMODE_READ) {
2114                 iter = __tracing_open(inode, file);
2115                 if (IS_ERR(iter))
2116                         ret = PTR_ERR(iter);
2117                 else if (trace_flags & TRACE_ITER_LATENCY_FMT)
2118                         iter->iter_flags |= TRACE_FILE_LAT_FMT;
2119         }
2120         return ret;
2121 }
2122
2123 static void *
2124 t_next(struct seq_file *m, void *v, loff_t *pos)
2125 {
2126         struct tracer *t = v;
2127
2128         (*pos)++;
2129
2130         if (t)
2131                 t = t->next;
2132
2133         return t;
2134 }
2135
2136 static void *t_start(struct seq_file *m, loff_t *pos)
2137 {
2138         struct tracer *t;
2139         loff_t l = 0;
2140
2141         mutex_lock(&trace_types_lock);
2142         for (t = trace_types; t && l < *pos; t = t_next(m, t, &l))
2143                 ;
2144
2145         return t;
2146 }
2147
2148 static void t_stop(struct seq_file *m, void *p)
2149 {
2150         mutex_unlock(&trace_types_lock);
2151 }
2152
2153 static int t_show(struct seq_file *m, void *v)
2154 {
2155         struct tracer *t = v;
2156
2157         if (!t)
2158                 return 0;
2159
2160         seq_printf(m, "%s", t->name);
2161         if (t->next)
2162                 seq_putc(m, ' ');
2163         else
2164                 seq_putc(m, '\n');
2165
2166         return 0;
2167 }
2168
2169 static struct seq_operations show_traces_seq_ops = {
2170         .start          = t_start,
2171         .next           = t_next,
2172         .stop           = t_stop,
2173         .show           = t_show,
2174 };
2175
2176 static int show_traces_open(struct inode *inode, struct file *file)
2177 {
2178         if (tracing_disabled)
2179                 return -ENODEV;
2180
2181         return seq_open(file, &show_traces_seq_ops);
2182 }
2183
2184 static ssize_t
2185 tracing_write_stub(struct file *filp, const char __user *ubuf,
2186                    size_t count, loff_t *ppos)
2187 {
2188         return count;
2189 }
2190
2191 static const struct file_operations tracing_fops = {
2192         .open           = tracing_open,
2193         .read           = seq_read,
2194         .write          = tracing_write_stub,
2195         .llseek         = seq_lseek,
2196         .release        = tracing_release,
2197 };
2198
2199 static const struct file_operations show_traces_fops = {
2200         .open           = show_traces_open,
2201         .read           = seq_read,
2202         .release        = seq_release,
2203 };
2204
2205 /*
2206  * Only trace on a CPU if the bitmask is set:
2207  */
2208 static cpumask_var_t tracing_cpumask;
2209
2210 /*
2211  * The tracer itself will not take this lock, but still we want
2212  * to provide a consistent cpumask to user-space:
2213  */
2214 static DEFINE_MUTEX(tracing_cpumask_update_lock);
2215
2216 /*
2217  * Temporary storage for the character representation of the
2218  * CPU bitmask (and one more byte for the newline):
2219  */
2220 static char mask_str[NR_CPUS + 1];
2221
2222 static ssize_t
2223 tracing_cpumask_read(struct file *filp, char __user *ubuf,
2224                      size_t count, loff_t *ppos)
2225 {
2226         int len;
2227
2228         mutex_lock(&tracing_cpumask_update_lock);
2229
2230         len = cpumask_scnprintf(mask_str, count, tracing_cpumask);
2231         if (count - len < 2) {
2232                 count = -EINVAL;
2233                 goto out_err;
2234         }
2235         len += sprintf(mask_str + len, "\n");
2236         count = simple_read_from_buffer(ubuf, count, ppos, mask_str, NR_CPUS+1);
2237
2238 out_err:
2239         mutex_unlock(&tracing_cpumask_update_lock);
2240
2241         return count;
2242 }
2243
2244 static ssize_t
2245 tracing_cpumask_write(struct file *filp, const char __user *ubuf,
2246                       size_t count, loff_t *ppos)
2247 {
2248         int err, cpu;
2249         cpumask_var_t tracing_cpumask_new;
2250
2251         if (!alloc_cpumask_var(&tracing_cpumask_new, GFP_KERNEL))
2252                 return -ENOMEM;
2253
2254         err = cpumask_parse_user(ubuf, count, tracing_cpumask_new);
2255         if (err)
2256                 goto err_unlock;
2257
2258         mutex_lock(&tracing_cpumask_update_lock);
2259
2260         local_irq_disable();
2261         __raw_spin_lock(&ftrace_max_lock);
2262         for_each_tracing_cpu(cpu) {
2263                 /*
2264                  * Increase/decrease the disabled counter if we are
2265                  * about to flip a bit in the cpumask:
2266                  */
2267                 if (cpumask_test_cpu(cpu, tracing_cpumask) &&
2268                                 !cpumask_test_cpu(cpu, tracing_cpumask_new)) {
2269                         atomic_inc(&global_trace.data[cpu]->disabled);
2270                 }
2271                 if (!cpumask_test_cpu(cpu, tracing_cpumask) &&
2272                                 cpumask_test_cpu(cpu, tracing_cpumask_new)) {
2273                         atomic_dec(&global_trace.data[cpu]->disabled);
2274                 }
2275         }
2276         __raw_spin_unlock(&ftrace_max_lock);
2277         local_irq_enable();
2278
2279         cpumask_copy(tracing_cpumask, tracing_cpumask_new);
2280
2281         mutex_unlock(&tracing_cpumask_update_lock);
2282         free_cpumask_var(tracing_cpumask_new);
2283
2284         return count;
2285
2286 err_unlock:
2287         free_cpumask_var(tracing_cpumask_new);
2288
2289         return err;
2290 }
2291
2292 static const struct file_operations tracing_cpumask_fops = {
2293         .open           = tracing_open_generic,
2294         .read           = tracing_cpumask_read,
2295         .write          = tracing_cpumask_write,
2296 };
2297
2298 static ssize_t
2299 tracing_trace_options_read(struct file *filp, char __user *ubuf,
2300                        size_t cnt, loff_t *ppos)
2301 {
2302         struct tracer_opt *trace_opts;
2303         u32 tracer_flags;
2304         int len = 0;
2305         char *buf;
2306         int r = 0;
2307         int i;
2308
2309
2310         /* calculate max size */
2311         for (i = 0; trace_options[i]; i++) {
2312                 len += strlen(trace_options[i]);
2313                 len += 3; /* "no" and newline */
2314         }
2315
2316         mutex_lock(&trace_types_lock);
2317         tracer_flags = current_trace->flags->val;
2318         trace_opts = current_trace->flags->opts;
2319
2320         /*
2321          * Increase the size with names of options specific
2322          * of the current tracer.
2323          */
2324         for (i = 0; trace_opts[i].name; i++) {
2325                 len += strlen(trace_opts[i].name);
2326                 len += 3; /* "no" and newline */
2327         }
2328
2329         /* +1 for \0 */
2330         buf = kmalloc(len + 1, GFP_KERNEL);
2331         if (!buf) {
2332                 mutex_unlock(&trace_types_lock);
2333                 return -ENOMEM;
2334         }
2335
2336         for (i = 0; trace_options[i]; i++) {
2337                 if (trace_flags & (1 << i))
2338                         r += sprintf(buf + r, "%s\n", trace_options[i]);
2339                 else
2340                         r += sprintf(buf + r, "no%s\n", trace_options[i]);
2341         }
2342
2343         for (i = 0; trace_opts[i].name; i++) {
2344                 if (tracer_flags & trace_opts[i].bit)
2345                         r += sprintf(buf + r, "%s\n",
2346                                 trace_opts[i].name);
2347                 else
2348                         r += sprintf(buf + r, "no%s\n",
2349                                 trace_opts[i].name);
2350         }
2351         mutex_unlock(&trace_types_lock);
2352
2353         WARN_ON(r >= len + 1);
2354
2355         r = simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2356
2357         kfree(buf);
2358         return r;
2359 }
2360
2361 /* Try to assign a tracer specific option */
2362 static int set_tracer_option(struct tracer *trace, char *cmp, int neg)
2363 {
2364         struct tracer_flags *tracer_flags = trace->flags;
2365         struct tracer_opt *opts = NULL;
2366         int ret = 0, i = 0;
2367         int len;
2368
2369         for (i = 0; tracer_flags->opts[i].name; i++) {
2370                 opts = &tracer_flags->opts[i];
2371                 len = strlen(opts->name);
2372
2373                 if (strncmp(cmp, opts->name, len) == 0) {
2374                         ret = trace->set_flag(tracer_flags->val,
2375                                 opts->bit, !neg);
2376                         break;
2377                 }
2378         }
2379         /* Not found */
2380         if (!tracer_flags->opts[i].name)
2381                 return -EINVAL;
2382
2383         /* Refused to handle */
2384         if (ret)
2385                 return ret;
2386
2387         if (neg)
2388                 tracer_flags->val &= ~opts->bit;
2389         else
2390                 tracer_flags->val |= opts->bit;
2391
2392         return 0;
2393 }
2394
2395 static void set_tracer_flags(unsigned int mask, int enabled)
2396 {
2397         /* do nothing if flag is already set */
2398         if (!!(trace_flags & mask) == !!enabled)
2399                 return;
2400
2401         if (enabled)
2402                 trace_flags |= mask;
2403         else
2404                 trace_flags &= ~mask;
2405 }
2406
2407 static ssize_t
2408 tracing_trace_options_write(struct file *filp, const char __user *ubuf,
2409                         size_t cnt, loff_t *ppos)
2410 {
2411         char buf[64];
2412         char *cmp = buf;
2413         int neg = 0;
2414         int ret;
2415         int i;
2416
2417         if (cnt >= sizeof(buf))
2418                 return -EINVAL;
2419
2420         if (copy_from_user(&buf, ubuf, cnt))
2421                 return -EFAULT;
2422
2423         buf[cnt] = 0;
2424
2425         if (strncmp(buf, "no", 2) == 0) {
2426                 neg = 1;
2427                 cmp += 2;
2428         }
2429
2430         for (i = 0; trace_options[i]; i++) {
2431                 int len = strlen(trace_options[i]);
2432
2433                 if (strncmp(cmp, trace_options[i], len) == 0) {
2434                         set_tracer_flags(1 << i, !neg);
2435                         break;
2436                 }
2437         }
2438
2439         /* If no option could be set, test the specific tracer options */
2440         if (!trace_options[i]) {
2441                 mutex_lock(&trace_types_lock);
2442                 ret = set_tracer_option(current_trace, cmp, neg);
2443                 mutex_unlock(&trace_types_lock);
2444                 if (ret)
2445                         return ret;
2446         }
2447
2448         filp->f_pos += cnt;
2449
2450         return cnt;
2451 }
2452
2453 static const struct file_operations tracing_iter_fops = {
2454         .open           = tracing_open_generic,
2455         .read           = tracing_trace_options_read,
2456         .write          = tracing_trace_options_write,
2457 };
2458
2459 static const char readme_msg[] =
2460         "tracing mini-HOWTO:\n\n"
2461         "# mount -t debugfs nodev /sys/kernel/debug\n\n"
2462         "# cat /sys/kernel/debug/tracing/available_tracers\n"
2463         "wakeup preemptirqsoff preemptoff irqsoff function sched_switch nop\n\n"
2464         "# cat /sys/kernel/debug/tracing/current_tracer\n"
2465         "nop\n"
2466         "# echo sched_switch > /sys/kernel/debug/tracing/current_tracer\n"
2467         "# cat /sys/kernel/debug/tracing/current_tracer\n"
2468         "sched_switch\n"
2469         "# cat /sys/kernel/debug/tracing/trace_options\n"
2470         "noprint-parent nosym-offset nosym-addr noverbose\n"
2471         "# echo print-parent > /sys/kernel/debug/tracing/trace_options\n"
2472         "# echo 1 > /sys/kernel/debug/tracing/tracing_enabled\n"
2473         "# cat /sys/kernel/debug/tracing/trace > /tmp/trace.txt\n"
2474         "# echo 0 > /sys/kernel/debug/tracing/tracing_enabled\n"
2475 ;
2476
2477 static ssize_t
2478 tracing_readme_read(struct file *filp, char __user *ubuf,
2479                        size_t cnt, loff_t *ppos)
2480 {
2481         return simple_read_from_buffer(ubuf, cnt, ppos,
2482                                         readme_msg, strlen(readme_msg));
2483 }
2484
2485 static const struct file_operations tracing_readme_fops = {
2486         .open           = tracing_open_generic,
2487         .read           = tracing_readme_read,
2488 };
2489
2490 static ssize_t
2491 tracing_saved_cmdlines_read(struct file *file, char __user *ubuf,
2492                                 size_t cnt, loff_t *ppos)
2493 {
2494         char *buf_comm;
2495         char *file_buf;
2496         char *buf;
2497         int len = 0;
2498         int pid;
2499         int i;
2500
2501         file_buf = kmalloc(SAVED_CMDLINES*(16+TASK_COMM_LEN), GFP_KERNEL);
2502         if (!file_buf)
2503                 return -ENOMEM;
2504
2505         buf_comm = kmalloc(TASK_COMM_LEN, GFP_KERNEL);
2506         if (!buf_comm) {
2507                 kfree(file_buf);
2508                 return -ENOMEM;
2509         }
2510
2511         buf = file_buf;
2512
2513         for (i = 0; i < SAVED_CMDLINES; i++) {
2514                 int r;
2515
2516                 pid = map_cmdline_to_pid[i];
2517                 if (pid == -1 || pid == NO_CMDLINE_MAP)
2518                         continue;
2519
2520                 trace_find_cmdline(pid, buf_comm);
2521                 r = sprintf(buf, "%d %s\n", pid, buf_comm);
2522                 buf += r;
2523                 len += r;
2524         }
2525
2526         len = simple_read_from_buffer(ubuf, cnt, ppos,
2527                                       file_buf, len);
2528
2529         kfree(file_buf);
2530         kfree(buf_comm);
2531
2532         return len;
2533 }
2534
2535 static const struct file_operations tracing_saved_cmdlines_fops = {
2536     .open       = tracing_open_generic,
2537     .read       = tracing_saved_cmdlines_read,
2538 };
2539
2540 static ssize_t
2541 tracing_ctrl_read(struct file *filp, char __user *ubuf,
2542                   size_t cnt, loff_t *ppos)
2543 {
2544         char buf[64];
2545         int r;
2546
2547         r = sprintf(buf, "%u\n", tracer_enabled);
2548         return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2549 }
2550
2551 static ssize_t
2552 tracing_ctrl_write(struct file *filp, const char __user *ubuf,
2553                    size_t cnt, loff_t *ppos)
2554 {
2555         struct trace_array *tr = filp->private_data;
2556         char buf[64];
2557         unsigned long val;
2558         int ret;
2559
2560         if (cnt >= sizeof(buf))
2561                 return -EINVAL;
2562
2563         if (copy_from_user(&buf, ubuf, cnt))
2564                 return -EFAULT;
2565
2566         buf[cnt] = 0;
2567
2568         ret = strict_strtoul(buf, 10, &val);
2569         if (ret < 0)
2570                 return ret;
2571
2572         val = !!val;
2573
2574         mutex_lock(&trace_types_lock);
2575         if (tracer_enabled ^ val) {
2576                 if (val) {
2577                         tracer_enabled = 1;
2578                         if (current_trace->start)
2579                                 current_trace->start(tr);
2580                         tracing_start();
2581                 } else {
2582                         tracer_enabled = 0;
2583                         tracing_stop();
2584                         if (current_trace->stop)
2585                                 current_trace->stop(tr);
2586                 }
2587         }
2588         mutex_unlock(&trace_types_lock);
2589
2590         filp->f_pos += cnt;
2591
2592         return cnt;
2593 }
2594
2595 static ssize_t
2596 tracing_set_trace_read(struct file *filp, char __user *ubuf,
2597                        size_t cnt, loff_t *ppos)
2598 {
2599         char buf[max_tracer_type_len+2];
2600         int r;
2601
2602         mutex_lock(&trace_types_lock);
2603         if (current_trace)
2604                 r = sprintf(buf, "%s\n", current_trace->name);
2605         else
2606                 r = sprintf(buf, "\n");
2607         mutex_unlock(&trace_types_lock);
2608
2609         return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2610 }
2611
2612 int tracer_init(struct tracer *t, struct trace_array *tr)
2613 {
2614         tracing_reset_online_cpus(tr);
2615         return t->init(tr);
2616 }
2617
2618 static int tracing_resize_ring_buffer(unsigned long size)
2619 {
2620         int ret;
2621
2622         /*
2623          * If kernel or user changes the size of the ring buffer
2624          * we use the size that was given, and we can forget about
2625          * expanding it later.
2626          */
2627         ring_buffer_expanded = 1;
2628
2629         ret = ring_buffer_resize(global_trace.buffer, size);
2630         if (ret < 0)
2631                 return ret;
2632
2633         ret = ring_buffer_resize(max_tr.buffer, size);
2634         if (ret < 0) {
2635                 int r;
2636
2637                 r = ring_buffer_resize(global_trace.buffer,
2638                                        global_trace.entries);
2639                 if (r < 0) {
2640                         /*
2641                          * AARGH! We are left with different
2642                          * size max buffer!!!!
2643                          * The max buffer is our "snapshot" buffer.
2644                          * When a tracer needs a snapshot (one of the
2645                          * latency tracers), it swaps the max buffer
2646                          * with the saved snap shot. We succeeded to
2647                          * update the size of the main buffer, but failed to
2648                          * update the size of the max buffer. But when we tried
2649                          * to reset the main buffer to the original size, we
2650                          * failed there too. This is very unlikely to
2651                          * happen, but if it does, warn and kill all
2652                          * tracing.
2653                          */
2654                         WARN_ON(1);
2655                         tracing_disabled = 1;
2656                 }
2657                 return ret;
2658         }
2659
2660         global_trace.entries = size;
2661
2662         return ret;
2663 }
2664
2665 /**
2666  * tracing_update_buffers - used by tracing facility to expand ring buffers
2667  *
2668  * To save on memory when the tracing is never used on a system with it
2669  * configured in. The ring buffers are set to a minimum size. But once
2670  * a user starts to use the tracing facility, then they need to grow
2671  * to their default size.
2672  *
2673  * This function is to be called when a tracer is about to be used.
2674  */
2675 int tracing_update_buffers(void)
2676 {
2677         int ret = 0;
2678
2679         mutex_lock(&trace_types_lock);
2680         if (!ring_buffer_expanded)
2681                 ret = tracing_resize_ring_buffer(trace_buf_size);
2682         mutex_unlock(&trace_types_lock);
2683
2684         return ret;
2685 }
2686
2687 struct trace_option_dentry;
2688
2689 static struct trace_option_dentry *
2690 create_trace_option_files(struct tracer *tracer);
2691
2692 static void
2693 destroy_trace_option_files(struct trace_option_dentry *topts);
2694
2695 static int tracing_set_tracer(const char *buf)
2696 {
2697         static struct trace_option_dentry *topts;
2698         struct trace_array *tr = &global_trace;
2699         struct tracer *t;
2700         int ret = 0;
2701
2702         mutex_lock(&trace_types_lock);
2703
2704         if (!ring_buffer_expanded) {
2705                 ret = tracing_resize_ring_buffer(trace_buf_size);
2706                 if (ret < 0)
2707                         goto out;
2708                 ret = 0;
2709         }
2710
2711         for (t = trace_types; t; t = t->next) {
2712                 if (strcmp(t->name, buf) == 0)
2713                         break;
2714         }
2715         if (!t) {
2716                 ret = -EINVAL;
2717                 goto out;
2718         }
2719         if (t == current_trace)
2720                 goto out;
2721
2722         trace_branch_disable();
2723         if (current_trace && current_trace->reset)
2724                 current_trace->reset(tr);
2725
2726         destroy_trace_option_files(topts);
2727
2728         current_trace = t;
2729
2730         topts = create_trace_option_files(current_trace);
2731
2732         if (t->init) {
2733                 ret = tracer_init(t, tr);
2734                 if (ret)
2735                         goto out;
2736         }
2737
2738         trace_branch_enable(tr);
2739  out:
2740         mutex_unlock(&trace_types_lock);
2741
2742         return ret;
2743 }
2744
2745 static ssize_t
2746 tracing_set_trace_write(struct file *filp, const char __user *ubuf,
2747                         size_t cnt, loff_t *ppos)
2748 {
2749         char buf[max_tracer_type_len+1];
2750         int i;
2751         size_t ret;
2752         int err;
2753
2754         ret = cnt;
2755
2756         if (cnt > max_tracer_type_len)
2757                 cnt = max_tracer_type_len;
2758
2759         if (copy_from_user(&buf, ubuf, cnt))
2760                 return -EFAULT;
2761
2762         buf[cnt] = 0;
2763
2764         /* strip ending whitespace. */
2765         for (i = cnt - 1; i > 0 && isspace(buf[i]); i--)
2766                 buf[i] = 0;
2767
2768         err = tracing_set_tracer(buf);
2769         if (err)
2770                 return err;
2771
2772         filp->f_pos += ret;
2773
2774         return ret;
2775 }
2776
2777 static ssize_t
2778 tracing_max_lat_read(struct file *filp, char __user *ubuf,
2779                      size_t cnt, loff_t *ppos)
2780 {
2781         unsigned long *ptr = filp->private_data;
2782         char buf[64];
2783         int r;
2784
2785         r = snprintf(buf, sizeof(buf), "%ld\n",
2786                      *ptr == (unsigned long)-1 ? -1 : nsecs_to_usecs(*ptr));
2787         if (r > sizeof(buf))
2788                 r = sizeof(buf);
2789         return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2790 }
2791
2792 static ssize_t
2793 tracing_max_lat_write(struct file *filp, const char __user *ubuf,
2794                       size_t cnt, loff_t *ppos)
2795 {
2796         unsigned long *ptr = filp->private_data;
2797         char buf[64];
2798         unsigned long val;
2799         int ret;
2800
2801         if (cnt >= sizeof(buf))
2802                 return -EINVAL;
2803
2804         if (copy_from_user(&buf, ubuf, cnt))
2805                 return -EFAULT;
2806
2807         buf[cnt] = 0;
2808
2809         ret = strict_strtoul(buf, 10, &val);
2810         if (ret < 0)
2811                 return ret;
2812
2813         *ptr = val * 1000;
2814
2815         return cnt;
2816 }
2817
2818 static int tracing_open_pipe(struct inode *inode, struct file *filp)
2819 {
2820         long cpu_file = (long) inode->i_private;
2821         struct trace_iterator *iter;
2822         int ret = 0;
2823
2824         if (tracing_disabled)
2825                 return -ENODEV;
2826
2827         mutex_lock(&trace_types_lock);
2828
2829         /* We only allow one reader per cpu */
2830         if (cpu_file == TRACE_PIPE_ALL_CPU) {
2831                 if (!cpumask_empty(tracing_reader_cpumask)) {
2832                         ret = -EBUSY;
2833                         goto out;
2834                 }
2835                 cpumask_setall(tracing_reader_cpumask);
2836         } else {
2837                 if (!cpumask_test_cpu(cpu_file, tracing_reader_cpumask))
2838                         cpumask_set_cpu(cpu_file, tracing_reader_cpumask);
2839                 else {
2840                         ret = -EBUSY;
2841                         goto out;
2842                 }
2843         }
2844
2845         /* create a buffer to store the information to pass to userspace */
2846         iter = kzalloc(sizeof(*iter), GFP_KERNEL);
2847         if (!iter) {
2848                 ret = -ENOMEM;
2849                 goto out;
2850         }
2851
2852         /*
2853          * We make a copy of the current tracer to avoid concurrent
2854          * changes on it while we are reading.
2855          */
2856         iter->trace = kmalloc(sizeof(*iter->trace), GFP_KERNEL);
2857         if (!iter->trace) {
2858                 ret = -ENOMEM;
2859                 goto fail;
2860         }
2861         if (current_trace)
2862                 *iter->trace = *current_trace;
2863
2864         if (!alloc_cpumask_var(&iter->started, GFP_KERNEL)) {
2865                 ret = -ENOMEM;
2866                 goto fail;
2867         }
2868
2869         /* trace pipe does not show start of buffer */
2870         cpumask_setall(iter->started);
2871
2872         if (trace_flags & TRACE_ITER_LATENCY_FMT)
2873                 iter->iter_flags |= TRACE_FILE_LAT_FMT;
2874
2875         iter->cpu_file = cpu_file;
2876         iter->tr = &global_trace;
2877         mutex_init(&iter->mutex);
2878         filp->private_data = iter;
2879
2880         if (iter->trace->pipe_open)
2881                 iter->trace->pipe_open(iter);
2882
2883 out:
2884         mutex_unlock(&trace_types_lock);
2885         return ret;
2886
2887 fail:
2888         kfree(iter->trace);
2889         kfree(iter);
2890         mutex_unlock(&trace_types_lock);
2891         return ret;
2892 }
2893
2894 static int tracing_release_pipe(struct inode *inode, struct file *file)
2895 {
2896         struct trace_iterator *iter = file->private_data;
2897
2898         mutex_lock(&trace_types_lock);
2899
2900         if (iter->cpu_file == TRACE_PIPE_ALL_CPU)
2901                 cpumask_clear(tracing_reader_cpumask);
2902         else
2903                 cpumask_clear_cpu(iter->cpu_file, tracing_reader_cpumask);
2904
2905         mutex_unlock(&trace_types_lock);
2906
2907         free_cpumask_var(iter->started);
2908         mutex_destroy(&iter->mutex);
2909         kfree(iter->trace);
2910         kfree(iter);
2911
2912         return 0;
2913 }
2914
2915 static unsigned int
2916 tracing_poll_pipe(struct file *filp, poll_table *poll_table)
2917 {
2918         struct trace_iterator *iter = filp->private_data;
2919
2920         if (trace_flags & TRACE_ITER_BLOCK) {
2921                 /*
2922                  * Always select as readable when in blocking mode
2923                  */
2924                 return POLLIN | POLLRDNORM;
2925         } else {
2926                 if (!trace_empty(iter))
2927                         return POLLIN | POLLRDNORM;
2928                 poll_wait(filp, &trace_wait, poll_table);
2929                 if (!trace_empty(iter))
2930                         return POLLIN | POLLRDNORM;
2931
2932                 return 0;
2933         }
2934 }
2935
2936
2937 void default_wait_pipe(struct trace_iterator *iter)
2938 {
2939         DEFINE_WAIT(wait);
2940
2941         prepare_to_wait(&trace_wait, &wait, TASK_INTERRUPTIBLE);
2942
2943         if (trace_empty(iter))
2944                 schedule();
2945
2946         finish_wait(&trace_wait, &wait);
2947 }
2948
2949 /*
2950  * This is a make-shift waitqueue.
2951  * A tracer might use this callback on some rare cases:
2952  *
2953  *  1) the current tracer might hold the runqueue lock when it wakes up
2954  *     a reader, hence a deadlock (sched, function, and function graph tracers)
2955  *  2) the function tracers, trace all functions, we don't want
2956  *     the overhead of calling wake_up and friends
2957  *     (and tracing them too)
2958  *
2959  *     Anyway, this is really very primitive wakeup.
2960  */
2961 void poll_wait_pipe(struct trace_iterator *iter)
2962 {
2963         set_current_state(TASK_INTERRUPTIBLE);
2964         /* sleep for 100 msecs, and try again. */
2965         schedule_timeout(HZ / 10);
2966 }
2967
2968 /* Must be called with trace_types_lock mutex held. */
2969 static int tracing_wait_pipe(struct file *filp)
2970 {
2971         struct trace_iterator *iter = filp->private_data;
2972
2973         while (trace_empty(iter)) {
2974
2975                 if ((filp->f_flags & O_NONBLOCK)) {
2976                         return -EAGAIN;
2977                 }
2978
2979                 mutex_unlock(&iter->mutex);
2980
2981                 iter->trace->wait_pipe(iter);
2982
2983                 mutex_lock(&iter->mutex);
2984
2985                 if (signal_pending(current))
2986                         return -EINTR;
2987
2988                 /*
2989                  * We block until we read something and tracing is disabled.
2990                  * We still block if tracing is disabled, but we have never
2991                  * read anything. This allows a user to cat this file, and
2992                  * then enable tracing. But after we have read something,
2993                  * we give an EOF when tracing is again disabled.
2994                  *
2995                  * iter->pos will be 0 if we haven't read anything.
2996                  */
2997                 if (!tracer_enabled && iter->pos)
2998                         break;
2999         }
3000
3001         return 1;
3002 }
3003
3004 /*
3005  * Consumer reader.
3006  */
3007 static ssize_t
3008 tracing_read_pipe(struct file *filp, char __user *ubuf,
3009                   size_t cnt, loff_t *ppos)
3010 {
3011         struct trace_iterator *iter = filp->private_data;
3012         static struct tracer *old_tracer;
3013         ssize_t sret;
3014
3015         /* return any leftover data */
3016         sret = trace_seq_to_user(&iter->seq, ubuf, cnt);
3017         if (sret != -EBUSY)
3018                 return sret;
3019
3020         trace_seq_init(&iter->seq);
3021
3022         /* copy the tracer to avoid using a global lock all around */
3023         mutex_lock(&trace_types_lock);
3024         if (unlikely(old_tracer != current_trace && current_trace)) {
3025                 old_tracer = current_trace;
3026                 *iter->trace = *current_trace;
3027         }
3028         mutex_unlock(&trace_types_lock);
3029
3030         /*
3031          * Avoid more than one consumer on a single file descriptor
3032          * This is just a matter of traces coherency, the ring buffer itself
3033          * is protected.
3034          */
3035         mutex_lock(&iter->mutex);
3036         if (iter->trace->read) {
3037                 sret = iter->trace->read(iter, filp, ubuf, cnt, ppos);
3038                 if (sret)
3039                         goto out;
3040         }
3041
3042 waitagain:
3043         sret = tracing_wait_pipe(filp);
3044         if (sret <= 0)
3045                 goto out;
3046
3047         /* stop when tracing is finished */
3048         if (trace_empty(iter)) {
3049                 sret = 0;
3050                 goto out;
3051         }
3052
3053         if (cnt >= PAGE_SIZE)
3054                 cnt = PAGE_SIZE - 1;
3055
3056         /* reset all but tr, trace, and overruns */
3057         memset(&iter->seq, 0,
3058                sizeof(struct trace_iterator) -
3059                offsetof(struct trace_iterator, seq));
3060         iter->pos = -1;
3061
3062         trace_event_read_lock();
3063         while (find_next_entry_inc(iter) != NULL) {
3064                 enum print_line_t ret;
3065                 int len = iter->seq.len;
3066
3067                 ret = print_trace_line(iter);
3068                 if (ret == TRACE_TYPE_PARTIAL_LINE) {
3069                         /* don't print partial lines */
3070                         iter->seq.len = len;
3071                         break;
3072                 }
3073                 if (ret != TRACE_TYPE_NO_CONSUME)
3074                         trace_consume(iter);
3075
3076                 if (iter->seq.len >= cnt)
3077                         break;
3078         }
3079         trace_event_read_unlock();
3080
3081         /* Now copy what we have to the user */
3082         sret = trace_seq_to_user(&iter->seq, ubuf, cnt);
3083         if (iter->seq.readpos >= iter->seq.len)
3084                 trace_seq_init(&iter->seq);
3085
3086         /*
3087          * If there was nothing to send to user, inspite of consuming trace
3088          * entries, go back to wait for more entries.
3089          */
3090         if (sret == -EBUSY)
3091                 goto waitagain;
3092
3093 out:
3094         mutex_unlock(&iter->mutex);
3095
3096         return sret;
3097 }
3098
3099 static void tracing_pipe_buf_release(struct pipe_inode_info *pipe,
3100                                      struct pipe_buffer *buf)
3101 {
3102         __free_page(buf->page);
3103 }
3104
3105 static void tracing_spd_release_pipe(struct splice_pipe_desc *spd,
3106                                      unsigned int idx)
3107 {
3108         __free_page(spd->pages[idx]);
3109 }
3110
3111 static struct pipe_buf_operations tracing_pipe_buf_ops = {
3112         .can_merge              = 0,
3113         .map                    = generic_pipe_buf_map,
3114         .unmap                  = generic_pipe_buf_unmap,
3115         .confirm                = generic_pipe_buf_confirm,
3116         .release                = tracing_pipe_buf_release,
3117         .steal                  = generic_pipe_buf_steal,
3118         .get                    = generic_pipe_buf_get,
3119 };
3120
3121 static size_t
3122 tracing_fill_pipe_page(size_t rem, struct trace_iterator *iter)
3123 {
3124         size_t count;
3125         int ret;
3126
3127         /* Seq buffer is page-sized, exactly what we need. */
3128         for (;;) {
3129                 count = iter->seq.len;
3130                 ret = print_trace_line(iter);
3131                 count = iter->seq.len - count;
3132                 if (rem < count) {
3133                         rem = 0;
3134                         iter->seq.len -= count;
3135                         break;
3136                 }
3137                 if (ret == TRACE_TYPE_PARTIAL_LINE) {
3138                         iter->seq.len -= count;
3139                         break;
3140                 }
3141
3142                 if (ret != TRACE_TYPE_NO_CONSUME)
3143                         trace_consume(iter);
3144                 rem -= count;
3145                 if (!find_next_entry_inc(iter)) {
3146                         rem = 0;
3147                         iter->ent = NULL;
3148                         break;
3149                 }
3150         }
3151
3152         return rem;
3153 }
3154
3155 static ssize_t tracing_splice_read_pipe(struct file *filp,
3156                                         loff_t *ppos,
3157                                         struct pipe_inode_info *pipe,
3158                                         size_t len,
3159                                         unsigned int flags)
3160 {
3161         struct page *pages[PIPE_BUFFERS];
3162         struct partial_page partial[PIPE_BUFFERS];
3163         struct trace_iterator *iter = filp->private_data;
3164         struct splice_pipe_desc spd = {
3165                 .pages          = pages,
3166                 .partial        = partial,
3167                 .nr_pages       = 0, /* This gets updated below. */
3168                 .flags          = flags,
3169                 .ops            = &tracing_pipe_buf_ops,
3170                 .spd_release    = tracing_spd_release_pipe,
3171         };
3172         static struct tracer *old_tracer;
3173         ssize_t ret;
3174         size_t rem;
3175         unsigned int i;
3176
3177         /* copy the tracer to avoid using a global lock all around */
3178         mutex_lock(&trace_types_lock);
3179         if (unlikely(old_tracer != current_trace && current_trace)) {
3180                 old_tracer = current_trace;
3181                 *iter->trace = *current_trace;
3182         }
3183         mutex_unlock(&trace_types_lock);
3184
3185         mutex_lock(&iter->mutex);
3186
3187         if (iter->trace->splice_read) {
3188                 ret = iter->trace->splice_read(iter, filp,
3189                                                ppos, pipe, len, flags);
3190                 if (ret)
3191                         goto out_err;
3192         }
3193
3194         ret = tracing_wait_pipe(filp);
3195         if (ret <= 0)
3196                 goto out_err;
3197
3198         if (!iter->ent && !find_next_entry_inc(iter)) {
3199                 ret = -EFAULT;
3200                 goto out_err;
3201         }
3202
3203         trace_event_read_lock();
3204
3205         /* Fill as many pages as possible. */
3206         for (i = 0, rem = len; i < PIPE_BUFFERS && rem; i++) {
3207                 pages[i] = alloc_page(GFP_KERNEL);
3208                 if (!pages[i])
3209                         break;
3210
3211                 rem = tracing_fill_pipe_page(rem, iter);
3212
3213                 /* Copy the data into the page, so we can start over. */
3214                 ret = trace_seq_to_buffer(&iter->seq,
3215                                           page_address(pages[i]),
3216                                           iter->seq.len);
3217                 if (ret < 0) {
3218                         __free_page(pages[i]);
3219                         break;
3220                 }
3221                 partial[i].offset = 0;
3222                 partial[i].len = iter->seq.len;
3223
3224                 trace_seq_init(&iter->seq);
3225         }
3226
3227         trace_event_read_unlock();
3228         mutex_unlock(&iter->mutex);
3229
3230         spd.nr_pages = i;
3231
3232         return splice_to_pipe(pipe, &spd);
3233
3234 out_err:
3235         mutex_unlock(&iter->mutex);
3236
3237         return ret;
3238 }
3239
3240 static ssize_t
3241 tracing_entries_read(struct file *filp, char __user *ubuf,
3242                      size_t cnt, loff_t *ppos)
3243 {
3244         struct trace_array *tr = filp->private_data;
3245         char buf[96];
3246         int r;
3247
3248         mutex_lock(&trace_types_lock);
3249         if (!ring_buffer_expanded)
3250                 r = sprintf(buf, "%lu (expanded: %lu)\n",
3251                             tr->entries >> 10,
3252                             trace_buf_size >> 10);
3253         else
3254                 r = sprintf(buf, "%lu\n", tr->entries >> 10);
3255         mutex_unlock(&trace_types_lock);
3256
3257         return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
3258 }
3259
3260 static ssize_t
3261 tracing_entries_write(struct file *filp, const char __user *ubuf,
3262                       size_t cnt, loff_t *ppos)
3263 {
3264         unsigned long val;
3265         char buf[64];
3266         int ret, cpu;
3267
3268         if (cnt >= sizeof(buf))
3269                 return -EINVAL;
3270
3271         if (copy_from_user(&buf, ubuf, cnt))
3272                 return -EFAULT;
3273
3274         buf[cnt] = 0;
3275
3276         ret = strict_strtoul(buf, 10, &val);
3277         if (ret < 0)
3278                 return ret;
3279
3280         /* must have at least 1 entry */
3281         if (!val)
3282                 return -EINVAL;
3283
3284         mutex_lock(&trace_types_lock);
3285
3286         tracing_stop();
3287
3288         /* disable all cpu buffers */
3289         for_each_tracing_cpu(cpu) {
3290                 if (global_trace.data[cpu])
3291                         atomic_inc(&global_trace.data[cpu]->disabled);
3292                 if (max_tr.data[cpu])
3293                         atomic_inc(&max_tr.data[cpu]->disabled);
3294         }
3295
3296         /* value is in KB */
3297         val <<= 10;
3298
3299         if (val != global_trace.entries) {
3300                 ret = tracing_resize_ring_buffer(val);
3301                 if (ret < 0) {
3302                         cnt = ret;
3303                         goto out;
3304                 }
3305         }
3306
3307         filp->f_pos += cnt;
3308
3309         /* If check pages failed, return ENOMEM */
3310         if (tracing_disabled)
3311                 cnt = -ENOMEM;
3312  out:
3313         for_each_tracing_cpu(cpu) {
3314                 if (global_trace.data[cpu])
3315                         atomic_dec(&global_trace.data[cpu]->disabled);
3316                 if (max_tr.data[cpu])
3317                         atomic_dec(&max_tr.data[cpu]->disabled);
3318         }
3319
3320         tracing_start();
3321         max_tr.entries = global_trace.entries;
3322         mutex_unlock(&trace_types_lock);
3323
3324         return cnt;
3325 }
3326
3327 static int mark_printk(const char *fmt, ...)
3328 {
3329         int ret;
3330         va_list args;
3331         va_start(args, fmt);
3332         ret = trace_vprintk(0, fmt, args);
3333         va_end(args);
3334         return ret;
3335 }
3336
3337 static ssize_t
3338 tracing_mark_write(struct file *filp, const char __user *ubuf,
3339                                         size_t cnt, loff_t *fpos)
3340 {
3341         char *buf;
3342         char *end;
3343
3344         if (tracing_disabled)
3345                 return -EINVAL;
3346
3347         if (cnt > TRACE_BUF_SIZE)
3348                 cnt = TRACE_BUF_SIZE;
3349
3350         buf = kmalloc(cnt + 1, GFP_KERNEL);
3351         if (buf == NULL)
3352                 return -ENOMEM;
3353
3354         if (copy_from_user(buf, ubuf, cnt)) {
3355                 kfree(buf);
3356                 return -EFAULT;
3357         }
3358
3359         /* Cut from the first nil or newline. */
3360         buf[cnt] = '\0';
3361         end = strchr(buf, '\n');
3362         if (end)
3363                 *end = '\0';
3364
3365         cnt = mark_printk("%s\n", buf);
3366         kfree(buf);
3367         *fpos += cnt;
3368
3369         return cnt;
3370 }
3371
3372 static ssize_t tracing_clock_read(struct file *filp, char __user *ubuf,
3373                                   size_t cnt, loff_t *ppos)
3374 {
3375         char buf[64];
3376         int bufiter = 0;
3377         int i;
3378
3379         for (i = 0; i < ARRAY_SIZE(trace_clocks); i++)
3380                 bufiter += snprintf(buf + bufiter, sizeof(buf) - bufiter,
3381                         "%s%s%s%s", i ? " " : "",
3382                         i == trace_clock_id ? "[" : "", trace_clocks[i].name,
3383                         i == trace_clock_id ? "]" : "");
3384         bufiter += snprintf(buf + bufiter, sizeof(buf) - bufiter, "\n");
3385
3386         return simple_read_from_buffer(ubuf, cnt, ppos, buf, bufiter);
3387 }
3388
3389 static ssize_t tracing_clock_write(struct file *filp, const char __user *ubuf,
3390                                    size_t cnt, loff_t *fpos)
3391 {
3392         char buf[64];
3393         const char *clockstr;
3394         int i;
3395
3396         if (cnt >= sizeof(buf))
3397                 return -EINVAL;
3398
3399         if (copy_from_user(&buf, ubuf, cnt))
3400                 return -EFAULT;
3401
3402         buf[cnt] = 0;
3403
3404         clockstr = strstrip(buf);
3405
3406         for (i = 0; i < ARRAY_SIZE(trace_clocks); i++) {
3407                 if (strcmp(trace_clocks[i].name, clockstr) == 0)
3408                         break;
3409         }
3410         if (i == ARRAY_SIZE(trace_clocks))
3411                 return -EINVAL;
3412
3413         trace_clock_id = i;
3414
3415         mutex_lock(&trace_types_lock);
3416
3417         ring_buffer_set_clock(global_trace.buffer, trace_clocks[i].func);
3418         if (max_tr.buffer)
3419                 ring_buffer_set_clock(max_tr.buffer, trace_clocks[i].func);
3420
3421         mutex_unlock(&trace_types_lock);
3422
3423         *fpos += cnt;
3424
3425         return cnt;
3426 }
3427
3428 static const struct file_operations tracing_max_lat_fops = {
3429         .open           = tracing_open_generic,
3430         .read           = tracing_max_lat_read,
3431         .write          = tracing_max_lat_write,
3432 };
3433
3434 static const struct file_operations tracing_ctrl_fops = {
3435         .open           = tracing_open_generic,
3436         .read           = tracing_ctrl_read,
3437         .write          = tracing_ctrl_write,
3438 };
3439
3440 static const struct file_operations set_tracer_fops = {
3441         .open           = tracing_open_generic,
3442         .read           = tracing_set_trace_read,
3443         .write          = tracing_set_trace_write,
3444 };
3445
3446 static const struct file_operations tracing_pipe_fops = {
3447         .open           = tracing_open_pipe,
3448         .poll           = tracing_poll_pipe,
3449         .read           = tracing_read_pipe,
3450         .splice_read    = tracing_splice_read_pipe,
3451         .release        = tracing_release_pipe,
3452 };
3453
3454 static const struct file_operations tracing_entries_fops = {
3455         .open           = tracing_open_generic,
3456         .read           = tracing_entries_read,
3457         .write          = tracing_entries_write,
3458 };
3459
3460 static const struct file_operations tracing_mark_fops = {
3461         .open           = tracing_open_generic,
3462         .write          = tracing_mark_write,
3463 };
3464
3465 static const struct file_operations trace_clock_fops = {
3466         .open           = tracing_open_generic,
3467         .read           = tracing_clock_read,
3468         .write          = tracing_clock_write,
3469 };
3470
3471 struct ftrace_buffer_info {
3472         struct trace_array      *tr;
3473         void                    *spare;
3474         int                     cpu;
3475         unsigned int            read;
3476 };
3477
3478 static int tracing_buffers_open(struct inode *inode, struct file *filp)
3479 {
3480         int cpu = (int)(long)inode->i_private;
3481         struct ftrace_buffer_info *info;
3482
3483         if (tracing_disabled)
3484                 return -ENODEV;
3485
3486         info = kzalloc(sizeof(*info), GFP_KERNEL);
3487         if (!info)
3488                 return -ENOMEM;
3489
3490         info->tr        = &global_trace;
3491         info->cpu       = cpu;
3492         info->spare     = NULL;
3493         /* Force reading ring buffer for first read */
3494         info->read      = (unsigned int)-1;
3495
3496         filp->private_data = info;
3497
3498         return nonseekable_open(inode, filp);
3499 }
3500
3501 static ssize_t
3502 tracing_buffers_read(struct file *filp, char __user *ubuf,
3503                      size_t count, loff_t *ppos)
3504 {
3505         struct ftrace_buffer_info *info = filp->private_data;
3506         unsigned int pos;
3507         ssize_t ret;
3508         size_t size;
3509
3510         if (!count)
3511                 return 0;
3512
3513         if (!info->spare)
3514                 info->spare = ring_buffer_alloc_read_page(info->tr->buffer);
3515         if (!info->spare)
3516                 return -ENOMEM;
3517
3518         /* Do we have previous read data to read? */
3519         if (info->read < PAGE_SIZE)
3520                 goto read;
3521
3522         info->read = 0;
3523
3524         ret = ring_buffer_read_page(info->tr->buffer,
3525                                     &info->spare,
3526                                     count,
3527                                     info->cpu, 0);
3528         if (ret < 0)
3529                 return 0;
3530
3531         pos = ring_buffer_page_len(info->spare);
3532
3533         if (pos < PAGE_SIZE)
3534                 memset(info->spare + pos, 0, PAGE_SIZE - pos);
3535
3536 read:
3537         size = PAGE_SIZE - info->read;
3538         if (size > count)
3539                 size = count;
3540
3541         ret = copy_to_user(ubuf, info->spare + info->read, size);
3542         if (ret == size)
3543                 return -EFAULT;
3544         size -= ret;
3545
3546         *ppos += size;
3547         info->read += size;
3548
3549         return size;
3550 }
3551
3552 static int tracing_buffers_release(struct inode *inode, struct file *file)
3553 {
3554         struct ftrace_buffer_info *info = file->private_data;
3555
3556         if (info->spare)
3557                 ring_buffer_free_read_page(info->tr->buffer, info->spare);
3558         kfree(info);
3559
3560         return 0;
3561 }
3562
3563 struct buffer_ref {
3564         struct ring_buffer      *buffer;
3565         void                    *page;
3566         int                     ref;
3567 };
3568
3569 static void buffer_pipe_buf_release(struct pipe_inode_info *pipe,
3570                                     struct pipe_buffer *buf)
3571 {
3572         struct buffer_ref *ref = (struct buffer_ref *)buf->private;
3573
3574         if (--ref->ref)
3575                 return;
3576
3577         ring_buffer_free_read_page(ref->buffer, ref->page);
3578         kfree(ref);
3579         buf->private = 0;
3580 }
3581
3582 static int buffer_pipe_buf_steal(struct pipe_inode_info *pipe,
3583                                  struct pipe_buffer *buf)
3584 {
3585         return 1;
3586 }
3587
3588 static void buffer_pipe_buf_get(struct pipe_inode_info *pipe,
3589                                 struct pipe_buffer *buf)
3590 {
3591         struct buffer_ref *ref = (struct buffer_ref *)buf->private;
3592
3593         ref->ref++;
3594 }
3595
3596 /* Pipe buffer operations for a buffer. */
3597 static struct pipe_buf_operations buffer_pipe_buf_ops = {
3598         .can_merge              = 0,
3599         .map                    = generic_pipe_buf_map,
3600         .unmap                  = generic_pipe_buf_unmap,
3601         .confirm                = generic_pipe_buf_confirm,
3602         .release                = buffer_pipe_buf_release,
3603         .steal                  = buffer_pipe_buf_steal,
3604         .get                    = buffer_pipe_buf_get,
3605 };
3606
3607 /*
3608  * Callback from splice_to_pipe(), if we need to release some pages
3609  * at the end of the spd in case we error'ed out in filling the pipe.
3610  */
3611 static void buffer_spd_release(struct splice_pipe_desc *spd, unsigned int i)
3612 {
3613         struct buffer_ref *ref =
3614                 (struct buffer_ref *)spd->partial[i].private;
3615
3616         if (--ref->ref)
3617                 return;
3618
3619         ring_buffer_free_read_page(ref->buffer, ref->page);
3620         kfree(ref);
3621         spd->partial[i].private = 0;
3622 }
3623
3624 static ssize_t
3625 tracing_buffers_splice_read(struct file *file, loff_t *ppos,
3626                             struct pipe_inode_info *pipe, size_t len,
3627                             unsigned int flags)
3628 {
3629         struct ftrace_buffer_info *info = file->private_data;
3630         struct partial_page partial[PIPE_BUFFERS];
3631         struct page *pages[PIPE_BUFFERS];
3632         struct splice_pipe_desc spd = {
3633                 .pages          = pages,
3634                 .partial        = partial,
3635                 .flags          = flags,
3636                 .ops            = &buffer_pipe_buf_ops,
3637                 .spd_release    = buffer_spd_release,
3638         };
3639         struct buffer_ref *ref;
3640         int entries, size, i;
3641         size_t ret;
3642
3643         if (*ppos & (PAGE_SIZE - 1)) {
3644                 WARN_ONCE(1, "Ftrace: previous read must page-align\n");
3645                 return -EINVAL;
3646         }
3647
3648         if (len & (PAGE_SIZE - 1)) {
3649                 WARN_ONCE(1, "Ftrace: splice_read should page-align\n");
3650                 if (len < PAGE_SIZE)
3651                         return -EINVAL;
3652                 len &= PAGE_MASK;
3653         }
3654
3655         entries = ring_buffer_entries_cpu(info->tr->buffer, info->cpu);
3656
3657         for (i = 0; i < PIPE_BUFFERS && len && entries; i++, len -= PAGE_SIZE) {
3658                 struct page *page;
3659                 int r;
3660
3661                 ref = kzalloc(sizeof(*ref), GFP_KERNEL);
3662                 if (!ref)
3663                         break;
3664
3665                 ref->ref = 1;
3666                 ref->buffer = info->tr->buffer;
3667                 ref->page = ring_buffer_alloc_read_page(ref->buffer);
3668                 if (!ref->page) {
3669                         kfree(ref);
3670                         break;
3671                 }
3672
3673                 r = ring_buffer_read_page(ref->buffer, &ref->page,
3674                                           len, info->cpu, 1);
3675                 if (r < 0) {
3676                         ring_buffer_free_read_page(ref->buffer,
3677                                                    ref->page);
3678                         kfree(ref);
3679                         break;
3680                 }
3681
3682                 /*
3683                  * zero out any left over data, this is going to
3684                  * user land.
3685                  */
3686                 size = ring_buffer_page_len(ref->page);
3687                 if (size < PAGE_SIZE)
3688                         memset(ref->page + size, 0, PAGE_SIZE - size);
3689
3690                 page = virt_to_page(ref->page);
3691
3692                 spd.pages[i] = page;
3693                 spd.partial[i].len = PAGE_SIZE;
3694                 spd.partial[i].offset = 0;
3695                 spd.partial[i].private = (unsigned long)ref;
3696                 spd.nr_pages++;
3697                 *ppos += PAGE_SIZE;
3698
3699                 entries = ring_buffer_entries_cpu(info->tr->buffer, info->cpu);
3700         }
3701
3702         spd.nr_pages = i;
3703
3704         /* did we read anything? */
3705         if (!spd.nr_pages) {
3706                 if (flags & SPLICE_F_NONBLOCK)
3707                         ret = -EAGAIN;
3708                 else
3709                         ret = 0;
3710                 /* TODO: block */
3711                 return ret;
3712         }
3713
3714         ret = splice_to_pipe(pipe, &spd);
3715
3716         return ret;
3717 }
3718
3719 static const struct file_operations tracing_buffers_fops = {
3720         .open           = tracing_buffers_open,
3721         .read           = tracing_buffers_read,
3722         .release        = tracing_buffers_release,
3723         .splice_read    = tracing_buffers_splice_read,
3724         .llseek         = no_llseek,
3725 };
3726
3727 static ssize_t
3728 tracing_stats_read(struct file *filp, char __user *ubuf,
3729                    size_t count, loff_t *ppos)
3730 {
3731         unsigned long cpu = (unsigned long)filp->private_data;
3732         struct trace_array *tr = &global_trace;
3733         struct trace_seq *s;
3734         unsigned long cnt;
3735
3736         s = kmalloc(sizeof(*s), GFP_KERNEL);
3737         if (!s)
3738                 return ENOMEM;
3739
3740         trace_seq_init(s);
3741
3742         cnt = ring_buffer_entries_cpu(tr->buffer, cpu);
3743         trace_seq_printf(s, "entries: %ld\n", cnt);
3744
3745         cnt = ring_buffer_overrun_cpu(tr->buffer, cpu);
3746         trace_seq_printf(s, "overrun: %ld\n", cnt);
3747
3748         cnt = ring_buffer_commit_overrun_cpu(tr->buffer, cpu);
3749         trace_seq_printf(s, "commit overrun: %ld\n", cnt);
3750
3751         count = simple_read_from_buffer(ubuf, count, ppos, s->buffer, s->len);
3752
3753         kfree(s);
3754
3755         return count;
3756 }
3757
3758 static const struct file_operations tracing_stats_fops = {
3759         .open           = tracing_open_generic,
3760         .read           = tracing_stats_read,
3761 };
3762
3763 #ifdef CONFIG_DYNAMIC_FTRACE
3764
3765 int __weak ftrace_arch_read_dyn_info(char *buf, int size)
3766 {
3767         return 0;
3768 }
3769
3770 static ssize_t
3771 tracing_read_dyn_info(struct file *filp, char __user *ubuf,
3772                   size_t cnt, loff_t *ppos)
3773 {
3774         static char ftrace_dyn_info_buffer[1024];
3775         static DEFINE_MUTEX(dyn_info_mutex);
3776         unsigned long *p = filp->private_data;
3777         char *buf = ftrace_dyn_info_buffer;
3778         int size = ARRAY_SIZE(ftrace_dyn_info_buffer);
3779         int r;
3780
3781         mutex_lock(&dyn_info_mutex);
3782         r = sprintf(buf, "%ld ", *p);
3783
3784         r += ftrace_arch_read_dyn_info(buf+r, (size-1)-r);
3785         buf[r++] = '\n';
3786
3787         r = simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
3788
3789         mutex_unlock(&dyn_info_mutex);
3790
3791         return r;
3792 }
3793
3794 static const struct file_operations tracing_dyn_info_fops = {
3795         .open           = tracing_open_generic,
3796         .read           = tracing_read_dyn_info,
3797 };
3798 #endif
3799
3800 static struct dentry *d_tracer;
3801
3802 struct dentry *tracing_init_dentry(void)
3803 {
3804         static int once;
3805
3806         if (d_tracer)
3807                 return d_tracer;
3808
3809         if (!debugfs_initialized())
3810                 return NULL;
3811
3812         d_tracer = debugfs_create_dir("tracing", NULL);
3813
3814         if (!d_tracer && !once) {
3815                 once = 1;
3816                 pr_warning("Could not create debugfs directory 'tracing'\n");
3817                 return NULL;
3818         }
3819
3820         return d_tracer;
3821 }
3822
3823 static struct dentry *d_percpu;
3824
3825 struct dentry *tracing_dentry_percpu(void)
3826 {
3827         static int once;
3828         struct dentry *d_tracer;
3829
3830         if (d_percpu)
3831                 return d_percpu;
3832
3833         d_tracer = tracing_init_dentry();
3834
3835         if (!d_tracer)
3836                 return NULL;
3837
3838         d_percpu = debugfs_create_dir("per_cpu", d_tracer);
3839
3840         if (!d_percpu && !once) {
3841                 once = 1;
3842                 pr_warning("Could not create debugfs directory 'per_cpu'\n");
3843                 return NULL;
3844         }
3845
3846         return d_percpu;
3847 }
3848
3849 static void tracing_init_debugfs_percpu(long cpu)
3850 {
3851         struct dentry *d_percpu = tracing_dentry_percpu();
3852         struct dentry *d_cpu;
3853         /* strlen(cpu) + MAX(log10(cpu)) + '\0' */
3854         char cpu_dir[7];
3855
3856         if (cpu > 999 || cpu < 0)
3857                 return;
3858
3859         sprintf(cpu_dir, "cpu%ld", cpu);
3860         d_cpu = debugfs_create_dir(cpu_dir, d_percpu);
3861         if (!d_cpu) {
3862                 pr_warning("Could not create debugfs '%s' entry\n", cpu_dir);
3863                 return;
3864         }
3865
3866         /* per cpu trace_pipe */
3867         trace_create_file("trace_pipe", 0444, d_cpu,
3868                         (void *) cpu, &tracing_pipe_fops);
3869
3870         /* per cpu trace */
3871         trace_create_file("trace", 0644, d_cpu,
3872                         (void *) cpu, &tracing_fops);
3873
3874         trace_create_file("trace_pipe_raw", 0444, d_cpu,
3875                         (void *) cpu, &tracing_buffers_fops);
3876
3877         trace_create_file("stats", 0444, d_cpu,
3878                         (void *) cpu, &tracing_stats_fops);
3879 }
3880
3881 #ifdef CONFIG_FTRACE_SELFTEST
3882 /* Let selftest have access to static functions in this file */
3883 #include "trace_selftest.c"
3884 #endif
3885
3886 struct trace_option_dentry {
3887         struct tracer_opt               *opt;
3888         struct tracer_flags             *flags;
3889         struct dentry                   *entry;
3890 };
3891
3892 static ssize_t
3893 trace_options_read(struct file *filp, char __user *ubuf, size_t cnt,
3894                         loff_t *ppos)
3895 {
3896         struct trace_option_dentry *topt = filp->private_data;
3897         char *buf;
3898
3899         if (topt->flags->val & topt->opt->bit)
3900                 buf = "1\n";
3901         else
3902                 buf = "0\n";
3903
3904         return simple_read_from_buffer(ubuf, cnt, ppos, buf, 2);
3905 }
3906
3907 static ssize_t
3908 trace_options_write(struct file *filp, const char __user *ubuf, size_t cnt,
3909                          loff_t *ppos)
3910 {
3911         struct trace_option_dentry *topt = filp->private_data;
3912         unsigned long val;
3913         char buf[64];
3914         int ret;
3915
3916         if (cnt >= sizeof(buf))
3917                 return -EINVAL;
3918
3919         if (copy_from_user(&buf, ubuf, cnt))
3920                 return -EFAULT;
3921
3922         buf[cnt] = 0;
3923
3924         ret = strict_strtoul(buf, 10, &val);
3925         if (ret < 0)
3926                 return ret;
3927
3928         ret = 0;
3929         switch (val) {
3930         case 0:
3931                 /* do nothing if already cleared */
3932                 if (!(topt->flags->val & topt->opt->bit))
3933                         break;
3934
3935                 mutex_lock(&trace_types_lock);
3936                 if (current_trace->set_flag)
3937                         ret = current_trace->set_flag(topt->flags->val,
3938                                                       topt->opt->bit, 0);
3939                 mutex_unlock(&trace_types_lock);
3940                 if (ret)
3941                         return ret;
3942                 topt->flags->val &= ~topt->opt->bit;
3943                 break;
3944         case 1:
3945                 /* do nothing if already set */
3946                 if (topt->flags->val & topt->opt->bit)
3947                         break;
3948
3949                 mutex_lock(&trace_types_lock);
3950                 if (current_trace->set_flag)
3951                         ret = current_trace->set_flag(topt->flags->val,
3952                                                       topt->opt->bit, 1);
3953                 mutex_unlock(&trace_types_lock);
3954                 if (ret)
3955                         return ret;
3956                 topt->flags->val |= topt->opt->bit;
3957                 break;
3958
3959         default:
3960                 return -EINVAL;
3961         }
3962
3963         *ppos += cnt;
3964
3965         return cnt;
3966 }
3967
3968
3969 static const struct file_operations trace_options_fops = {
3970         .open = tracing_open_generic,
3971         .read = trace_options_read,
3972         .write = trace_options_write,
3973 };
3974
3975 static ssize_t
3976 trace_options_core_read(struct file *filp, char __user *ubuf, size_t cnt,
3977                         loff_t *ppos)
3978 {
3979         long index = (long)filp->private_data;
3980         char *buf;
3981
3982         if (trace_flags & (1 << index))
3983                 buf = "1\n";
3984         else
3985                 buf = "0\n";
3986
3987         return simple_read_from_buffer(ubuf, cnt, ppos, buf, 2);
3988 }
3989
3990 static ssize_t
3991 trace_options_core_write(struct file *filp, const char __user *ubuf, size_t cnt,
3992                          loff_t *ppos)
3993 {
3994         long index = (long)filp->private_data;
3995         char buf[64];
3996         unsigned long val;
3997         int ret;
3998
3999         if (cnt >= sizeof(buf))
4000                 return -EINVAL;
4001
4002         if (copy_from_user(&buf, ubuf, cnt))
4003                 return -EFAULT;
4004
4005         buf[cnt] = 0;
4006
4007         ret = strict_strtoul(buf, 10, &val);
4008         if (ret < 0)
4009                 return ret;
4010
4011         if (val != 0 && val != 1)
4012                 return -EINVAL;
4013         set_tracer_flags(1 << index, val);
4014
4015         *ppos += cnt;
4016
4017         return cnt;
4018 }
4019
4020 static const struct file_operations trace_options_core_fops = {
4021         .open = tracing_open_generic,
4022         .read = trace_options_core_read,
4023         .write = trace_options_core_write,
4024 };
4025
4026 struct dentry *trace_create_file(const char *name,
4027                                  mode_t mode,
4028                                  struct dentry *parent,
4029                                  void *data,
4030                                  const struct file_operations *fops)
4031 {
4032         struct dentry *ret;
4033
4034         ret = debugfs_create_file(name, mode, parent, data, fops);
4035         if (!ret)
4036                 pr_warning("Could not create debugfs '%s' entry\n", name);
4037
4038         return ret;
4039 }
4040
4041
4042 static struct dentry *trace_options_init_dentry(void)
4043 {
4044         struct dentry *d_tracer;
4045         static struct dentry *t_options;
4046
4047         if (t_options)
4048                 return t_options;
4049
4050         d_tracer = tracing_init_dentry();
4051         if (!d_tracer)
4052                 return NULL;
4053
4054         t_options = debugfs_create_dir("options", d_tracer);
4055         if (!t_options) {
4056                 pr_warning("Could not create debugfs directory 'options'\n");
4057                 return NULL;
4058         }
4059
4060         return t_options;
4061 }
4062
4063 static void
4064 create_trace_option_file(struct trace_option_dentry *topt,
4065                          struct tracer_flags *flags,
4066                          struct tracer_opt *opt)
4067 {
4068         struct dentry *t_options;
4069
4070         t_options = trace_options_init_dentry();
4071         if (!t_options)
4072                 return;
4073
4074         topt->flags = flags;
4075         topt->opt = opt;
4076
4077         topt->entry = trace_create_file(opt->name, 0644, t_options, topt,
4078                                     &trace_options_fops);
4079
4080 }
4081
4082 static struct trace_option_dentry *
4083 create_trace_option_files(struct tracer *tracer)
4084 {
4085         struct trace_option_dentry *topts;
4086         struct tracer_flags *flags;
4087         struct tracer_opt *opts;
4088         int cnt;
4089
4090         if (!tracer)
4091                 return NULL;
4092
4093         flags = tracer->flags;
4094
4095         if (!flags || !flags->opts)
4096                 return NULL;
4097
4098         opts = flags->opts;
4099
4100         for (cnt = 0; opts[cnt].name; cnt++)
4101                 ;
4102
4103         topts = kcalloc(cnt + 1, sizeof(*topts), GFP_KERNEL);
4104         if (!topts)
4105                 return NULL;
4106
4107         for (cnt = 0; opts[cnt].name; cnt++)
4108                 create_trace_option_file(&topts[cnt], flags,
4109                                          &opts[cnt]);
4110
4111         return topts;
4112 }
4113
4114 static void
4115 destroy_trace_option_files(struct trace_option_dentry *topts)
4116 {
4117         int cnt;
4118
4119         if (!topts)
4120                 return;
4121
4122         for (cnt = 0; topts[cnt].opt; cnt++) {
4123                 if (topts[cnt].entry)
4124                         debugfs_remove(topts[cnt].entry);
4125         }
4126
4127         kfree(topts);
4128 }
4129
4130 static struct dentry *
4131 create_trace_option_core_file(const char *option, long index)
4132 {
4133         struct dentry *t_options;
4134
4135         t_options = trace_options_init_dentry();
4136         if (!t_options)
4137                 return NULL;
4138
4139         return trace_create_file(option, 0644, t_options, (void *)index,
4140                                     &trace_options_core_fops);
4141 }
4142
4143 static __init void create_trace_options_dir(void)
4144 {
4145         struct dentry *t_options;
4146         int i;
4147
4148         t_options = trace_options_init_dentry();
4149         if (!t_options)
4150                 return;
4151
4152         for (i = 0; trace_options[i]; i++)
4153                 create_trace_option_core_file(trace_options[i], i);
4154 }
4155
4156 static __init int tracer_init_debugfs(void)
4157 {
4158         struct dentry *d_tracer;
4159         int cpu;
4160
4161         d_tracer = tracing_init_dentry();
4162
4163         trace_create_file("tracing_enabled", 0644, d_tracer,
4164                         &global_trace, &tracing_ctrl_fops);
4165
4166         trace_create_file("trace_options", 0644, d_tracer,
4167                         NULL, &tracing_iter_fops);
4168
4169         trace_create_file("tracing_cpumask", 0644, d_tracer,
4170                         NULL, &tracing_cpumask_fops);
4171
4172         trace_create_file("trace", 0644, d_tracer,
4173                         (void *) TRACE_PIPE_ALL_CPU, &tracing_fops);
4174
4175         trace_create_file("available_tracers", 0444, d_tracer,
4176                         &global_trace, &show_traces_fops);
4177
4178         trace_create_file("current_tracer", 0644, d_tracer,
4179                         &global_trace, &set_tracer_fops);
4180
4181 #ifdef CONFIG_TRACER_MAX_TRACE
4182         trace_create_file("tracing_max_latency", 0644, d_tracer,
4183                         &tracing_max_latency, &tracing_max_lat_fops);
4184
4185         trace_create_file("tracing_thresh", 0644, d_tracer,
4186                         &tracing_thresh, &tracing_max_lat_fops);
4187 #endif
4188
4189         trace_create_file("README", 0444, d_tracer,
4190                         NULL, &tracing_readme_fops);
4191
4192         trace_create_file("trace_pipe", 0444, d_tracer,
4193                         (void *) TRACE_PIPE_ALL_CPU, &tracing_pipe_fops);
4194
4195         trace_create_file("buffer_size_kb", 0644, d_tracer,
4196                         &global_trace, &tracing_entries_fops);
4197
4198         trace_create_file("trace_marker", 0220, d_tracer,
4199                         NULL, &tracing_mark_fops);
4200
4201         trace_create_file("saved_cmdlines", 0444, d_tracer,
4202                         NULL, &tracing_saved_cmdlines_fops);
4203
4204         trace_create_file("trace_clock", 0644, d_tracer, NULL,
4205                           &trace_clock_fops);
4206
4207 #ifdef CONFIG_DYNAMIC_FTRACE
4208         trace_create_file("dyn_ftrace_total_info", 0444, d_tracer,
4209                         &ftrace_update_tot_cnt, &tracing_dyn_info_fops);
4210 #endif
4211 #ifdef CONFIG_SYSPROF_TRACER
4212         init_tracer_sysprof_debugfs(d_tracer);
4213 #endif
4214
4215         create_trace_options_dir();
4216
4217         for_each_tracing_cpu(cpu)
4218                 tracing_init_debugfs_percpu(cpu);
4219
4220         return 0;
4221 }
4222
4223 static int trace_panic_handler(struct notifier_block *this,
4224                                unsigned long event, void *unused)
4225 {
4226         if (ftrace_dump_on_oops)
4227                 ftrace_dump();
4228         return NOTIFY_OK;
4229 }
4230
4231 static struct notifier_block trace_panic_notifier = {
4232         .notifier_call  = trace_panic_handler,
4233         .next           = NULL,
4234         .priority       = 150   /* priority: INT_MAX >= x >= 0 */
4235 };
4236
4237 static int trace_die_handler(struct notifier_block *self,
4238                              unsigned long val,
4239                              void *data)
4240 {
4241         switch (val) {
4242         case DIE_OOPS:
4243                 if (ftrace_dump_on_oops)
4244                         ftrace_dump();
4245                 break;
4246         default:
4247                 break;
4248         }
4249         return NOTIFY_OK;
4250 }
4251
4252 static struct notifier_block trace_die_notifier = {
4253         .notifier_call = trace_die_handler,
4254         .priority = 200
4255 };
4256
4257 /*
4258  * printk is set to max of 1024, we really don't need it that big.
4259  * Nothing should be printing 1000 characters anyway.
4260  */
4261 #define TRACE_MAX_PRINT         1000
4262
4263 /*
4264  * Define here KERN_TRACE so that we have one place to modify
4265  * it if we decide to change what log level the ftrace dump
4266  * should be at.
4267  */
4268 #define KERN_TRACE              KERN_EMERG
4269
4270 static void
4271 trace_printk_seq(struct trace_seq *s)
4272 {
4273         /* Probably should print a warning here. */
4274         if (s->len >= 1000)
4275                 s->len = 1000;
4276
4277         /* should be zero ended, but we are paranoid. */
4278         s->buffer[s->len] = 0;
4279
4280         printk(KERN_TRACE "%s", s->buffer);
4281
4282         trace_seq_init(s);
4283 }
4284
4285 static void __ftrace_dump(bool disable_tracing)
4286 {
4287         static raw_spinlock_t ftrace_dump_lock =
4288                 (raw_spinlock_t)__RAW_SPIN_LOCK_UNLOCKED;
4289         /* use static because iter can be a bit big for the stack */
4290         static struct trace_iterator iter;
4291         unsigned int old_userobj;
4292         static int dump_ran;
4293         unsigned long flags;
4294         int cnt = 0, cpu;
4295
4296         /* only one dump */
4297         local_irq_save(flags);
4298         __raw_spin_lock(&ftrace_dump_lock);
4299         if (dump_ran)
4300                 goto out;
4301
4302         dump_ran = 1;
4303
4304         tracing_off();
4305
4306         if (disable_tracing)
4307                 ftrace_kill();
4308
4309         for_each_tracing_cpu(cpu) {
4310                 atomic_inc(&global_trace.data[cpu]->disabled);
4311         }
4312
4313         old_userobj = trace_flags & TRACE_ITER_SYM_USEROBJ;
4314
4315         /* don't look at user memory in panic mode */
4316         trace_flags &= ~TRACE_ITER_SYM_USEROBJ;
4317
4318         printk(KERN_TRACE "Dumping ftrace buffer:\n");
4319
4320         /* Simulate the iterator */
4321         iter.tr = &global_trace;
4322         iter.trace = current_trace;
4323         iter.cpu_file = TRACE_PIPE_ALL_CPU;
4324
4325         /*
4326          * We need to stop all tracing on all CPUS to read the
4327          * the next buffer. This is a bit expensive, but is
4328          * not done often. We fill all what we can read,
4329          * and then release the locks again.
4330          */
4331
4332         while (!trace_empty(&iter)) {
4333
4334                 if (!cnt)
4335                         printk(KERN_TRACE "---------------------------------\n");
4336
4337                 cnt++;
4338
4339                 /* reset all but tr, trace, and overruns */
4340                 memset(&iter.seq, 0,
4341                        sizeof(struct trace_iterator) -
4342                        offsetof(struct trace_iterator, seq));
4343                 iter.iter_flags |= TRACE_FILE_LAT_FMT;
4344                 iter.pos = -1;
4345
4346                 if (find_next_entry_inc(&iter) != NULL) {
4347                         int ret;
4348
4349                         ret = print_trace_line(&iter);
4350                         if (ret != TRACE_TYPE_NO_CONSUME)
4351                                 trace_consume(&iter);
4352                 }
4353
4354                 trace_printk_seq(&iter.seq);
4355         }
4356
4357         if (!cnt)
4358                 printk(KERN_TRACE "   (ftrace buffer empty)\n");
4359         else
4360                 printk(KERN_TRACE "---------------------------------\n");
4361
4362         /* Re-enable tracing if requested */
4363         if (!disable_tracing) {
4364                 trace_flags |= old_userobj;
4365
4366                 for_each_tracing_cpu(cpu) {
4367                         atomic_dec(&global_trace.data[cpu]->disabled);
4368                 }
4369                 tracing_on();
4370         }
4371
4372  out:
4373         __raw_spin_unlock(&ftrace_dump_lock);
4374         local_irq_restore(flags);
4375 }
4376
4377 /* By default: disable tracing after the dump */
4378 void ftrace_dump(void)
4379 {
4380         __ftrace_dump(true);
4381 }
4382
4383 __init static int tracer_alloc_buffers(void)
4384 {
4385         int ring_buf_size;
4386         int i;
4387         int ret = -ENOMEM;
4388
4389         if (!alloc_cpumask_var(&tracing_buffer_mask, GFP_KERNEL))
4390                 goto out;
4391
4392         if (!alloc_cpumask_var(&tracing_cpumask, GFP_KERNEL))
4393                 goto out_free_buffer_mask;
4394
4395         if (!alloc_cpumask_var(&tracing_reader_cpumask, GFP_KERNEL))
4396                 goto out_free_tracing_cpumask;
4397
4398         /* To save memory, keep the ring buffer size to its minimum */
4399         if (ring_buffer_expanded)
4400                 ring_buf_size = trace_buf_size;
4401         else
4402                 ring_buf_size = 1;
4403
4404         cpumask_copy(tracing_buffer_mask, cpu_possible_mask);
4405         cpumask_copy(tracing_cpumask, cpu_all_mask);
4406         cpumask_clear(tracing_reader_cpumask);
4407
4408         /* TODO: make the number of buffers hot pluggable with CPUS */
4409         global_trace.buffer = ring_buffer_alloc(ring_buf_size,
4410                                                    TRACE_BUFFER_FLAGS);
4411         if (!global_trace.buffer) {
4412                 printk(KERN_ERR "tracer: failed to allocate ring buffer!\n");
4413                 WARN_ON(1);
4414                 goto out_free_cpumask;
4415         }
4416         global_trace.entries = ring_buffer_size(global_trace.buffer);
4417
4418
4419 #ifdef CONFIG_TRACER_MAX_TRACE
4420         max_tr.buffer = ring_buffer_alloc(ring_buf_size,
4421                                              TRACE_BUFFER_FLAGS);
4422         if (!max_tr.buffer) {
4423                 printk(KERN_ERR "tracer: failed to allocate max ring buffer!\n");
4424                 WARN_ON(1);
4425                 ring_buffer_free(global_trace.buffer);
4426                 goto out_free_cpumask;
4427         }
4428         max_tr.entries = ring_buffer_size(max_tr.buffer);
4429         WARN_ON(max_tr.entries != global_trace.entries);
4430 #endif
4431
4432         /* Allocate the first page for all buffers */
4433         for_each_tracing_cpu(i) {
4434                 global_trace.data[i] = &per_cpu(global_trace_cpu, i);
4435                 max_tr.data[i] = &per_cpu(max_data, i);
4436         }
4437
4438         trace_init_cmdlines();
4439
4440         register_tracer(&nop_trace);
4441         current_trace = &nop_trace;
4442 #ifdef CONFIG_BOOT_TRACER
4443         register_tracer(&boot_tracer);
4444 #endif
4445         /* All seems OK, enable tracing */
4446         tracing_disabled = 0;
4447
4448         atomic_notifier_chain_register(&panic_notifier_list,
4449                                        &trace_panic_notifier);
4450
4451         register_die_notifier(&trace_die_notifier);
4452
4453         return 0;
4454
4455 out_free_cpumask:
4456         free_cpumask_var(tracing_reader_cpumask);
4457 out_free_tracing_cpumask:
4458         free_cpumask_var(tracing_cpumask);
4459 out_free_buffer_mask:
4460         free_cpumask_var(tracing_buffer_mask);
4461 out:
4462         return ret;
4463 }
4464
4465 __init static int clear_boot_tracer(void)
4466 {
4467         /*
4468          * The default tracer at boot buffer is an init section.
4469          * This function is called in lateinit. If we did not
4470          * find the boot tracer, then clear it out, to prevent
4471          * later registration from accessing the buffer that is
4472          * about to be freed.
4473          */
4474         if (!default_bootup_tracer)
4475                 return 0;
4476
4477         printk(KERN_INFO "ftrace bootup tracer '%s' not registered.\n",
4478                default_bootup_tracer);
4479         default_bootup_tracer = NULL;
4480
4481         return 0;
4482 }
4483
4484 early_initcall(tracer_alloc_buffers);
4485 fs_initcall(tracer_init_debugfs);
4486 late_initcall(clear_boot_tracer);