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