]> nv-tegra.nvidia Code Review - linux-2.6.git/blobdiff - kernel/trace/trace.c
tracing: make trace_seq_reset global and rename to trace_seq_init
[linux-2.6.git] / kernel / trace / trace.c
index 2f8ac1f008f50aa6af5483c97a45ea6cdd1c5d7e..ea055aa21cd977e78cc6bbbce03479a18ad8cfaf 100644 (file)
@@ -31,6 +31,7 @@
 #include <linux/fs.h>
 #include <linux/kprobes.h>
 #include <linux/writeback.h>
+#include <linux/splice.h>
 
 #include <linux/stacktrace.h>
 #include <linux/ring_buffer.h>
@@ -53,6 +54,11 @@ unsigned long __read_mostly  tracing_thresh;
  */
 static bool __read_mostly tracing_selftest_running;
 
+/*
+ * If a tracer is running, we do not want to run SELFTEST.
+ */
+static bool __read_mostly tracing_selftest_disabled;
+
 /* For tracers that don't implement custom flags */
 static struct tracer_opt dummy_tracer_opt[] = {
        { }
@@ -74,7 +80,7 @@ static int dummy_set_flag(u32 old_flags, u32 bit, int set)
  * of the tracer is successful. But that is the only place that sets
  * this back to zero.
  */
-int tracing_disabled = 1;
+static int tracing_disabled = 1;
 
 static DEFINE_PER_CPU(local_t, ftrace_cpu_disabled);
 
@@ -92,6 +98,9 @@ static inline void ftrace_enable_cpu(void)
 
 static cpumask_var_t __read_mostly     tracing_buffer_mask;
 
+/* Define which cpu buffers are currently read in trace_pipe */
+static cpumask_var_t                   tracing_reader_cpumask;
+
 #define for_each_tracing_cpu(cpu)      \
        for_each_cpu(cpu, tracing_buffer_mask)
 
@@ -110,14 +119,19 @@ static cpumask_var_t __read_mostly        tracing_buffer_mask;
  */
 int ftrace_dump_on_oops;
 
-static int tracing_set_tracer(char *buf);
+static int tracing_set_tracer(const char *buf);
+
+#define BOOTUP_TRACER_SIZE             100
+static char bootup_tracer_buf[BOOTUP_TRACER_SIZE] __initdata;
+static char *default_bootup_tracer;
 
 static int __init set_ftrace(char *str)
 {
-       tracing_set_tracer(str);
+       strncpy(bootup_tracer_buf, str, BOOTUP_TRACER_SIZE);
+       default_bootup_tracer = bootup_tracer_buf;
        return 1;
 }
-__setup("ftrace", set_ftrace);
+__setup("ftrace=", set_ftrace);
 
 static int __init set_ftrace_dump_on_oops(char *str)
 {
@@ -227,7 +241,7 @@ static DECLARE_WAIT_QUEUE_HEAD(trace_wait);
 
 /* trace_flags holds trace_options default values */
 unsigned long trace_flags = TRACE_ITER_PRINT_PARENT | TRACE_ITER_PRINTK |
-       TRACE_ITER_ANNOTATE;
+       TRACE_ITER_ANNOTATE | TRACE_ITER_CONTEXT_INFO;
 
 /**
  * trace_wake_up - wake up tasks waiting for trace input
@@ -285,6 +299,7 @@ static const char *trace_options[] = {
        "userstacktrace",
        "sym-userobj",
        "printk-msg-only",
+       "context-info",
        NULL
 };
 
@@ -324,14 +339,7 @@ __update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
        data->rt_priority = tsk->rt_priority;
 
        /* record this tasks comm */
-       tracing_record_cmdline(current);
-}
-
-static void
-trace_seq_reset(struct trace_seq *s)
-{
-       s->len = 0;
-       s->readpos = 0;
+       tracing_record_cmdline(tsk);
 }
 
 ssize_t trace_seq_to_user(struct trace_seq *s, char __user *ubuf, size_t cnt)
@@ -353,6 +361,25 @@ ssize_t trace_seq_to_user(struct trace_seq *s, char __user *ubuf, size_t cnt)
        return cnt;
 }
 
+ssize_t trace_seq_to_buffer(struct trace_seq *s, void *buf, size_t cnt)
+{
+       int len;
+       void *ret;
+
+       if (s->len <= s->readpos)
+               return -EBUSY;
+
+       len = s->len - s->readpos;
+       if (cnt > len)
+               cnt = len;
+       ret = memcpy(buf, s->buffer + s->readpos, cnt);
+       if (!ret)
+               return -EFAULT;
+
+       s->readpos += len;
+       return cnt;
+}
+
 static void
 trace_print_seq(struct seq_file *m, struct trace_seq *s)
 {
@@ -361,7 +388,7 @@ trace_print_seq(struct seq_file *m, struct trace_seq *s)
        s->buffer[len] = 0;
        seq_puts(m, s->buffer);
 
-       trace_seq_reset(s);
+       trace_seq_init(s);
 }
 
 /**
@@ -428,6 +455,8 @@ update_max_tr_single(struct trace_array *tr, struct task_struct *tsk, int cpu)
  * Register a new plugin tracer.
  */
 int register_tracer(struct tracer *type)
+__releases(kernel_lock)
+__acquires(kernel_lock)
 {
        struct tracer *t;
        int len;
@@ -466,9 +495,12 @@ int register_tracer(struct tracer *type)
        else
                if (!type->flags->opts)
                        type->flags->opts = dummy_tracer_opt;
+       if (!type->wait_pipe)
+               type->wait_pipe = default_wait_pipe;
+
 
 #ifdef CONFIG_FTRACE_STARTUP_TEST
-       if (type->selftest) {
+       if (type->selftest && !tracing_selftest_disabled) {
                struct tracer *saved_tracer = current_trace;
                struct trace_array *tr = &global_trace;
                int i;
@@ -510,8 +542,26 @@ int register_tracer(struct tracer *type)
  out:
        tracing_selftest_running = false;
        mutex_unlock(&trace_types_lock);
-       lock_kernel();
 
+       if (ret || !default_bootup_tracer)
+               goto out_unlock;
+
+       if (strncmp(default_bootup_tracer, type->name, BOOTUP_TRACER_SIZE))
+               goto out_unlock;
+
+       printk(KERN_INFO "Starting tracer '%s'\n", type->name);
+       /* Do we want this tracer to start on bootup? */
+       tracing_set_tracer(type->name);
+       default_bootup_tracer = NULL;
+       /* disable other selftests, since this will break it. */
+       tracing_selftest_disabled = 1;
+#ifdef CONFIG_FTRACE_STARTUP_TEST
+       printk(KERN_INFO "Disabling FTRACE selftests due to running tracer '%s'\n",
+              type->name);
+#endif
+
+ out_unlock:
+       lock_kernel();
        return ret;
 }
 
@@ -530,6 +580,15 @@ void unregister_tracer(struct tracer *type)
 
  found:
        *t = (*t)->next;
+
+       if (type == current_trace && tracer_enabled) {
+               tracer_enabled = 0;
+               tracing_stop();
+               if (current_trace->stop)
+                       current_trace->stop(&global_trace);
+               current_trace = &nop_trace;
+       }
+
        if (strlen(type->name) != max_tracer_type_len)
                goto out;
 
@@ -568,7 +627,7 @@ static int cmdline_idx;
 static DEFINE_SPINLOCK(trace_cmdline_lock);
 
 /* temporary disable recording */
-atomic_t trace_record_cmdline_disabled __read_mostly;
+static atomic_t trace_record_cmdline_disabled __read_mostly;
 
 static void trace_init_cmdlines(void)
 {
@@ -747,78 +806,114 @@ tracing_generic_entry_update(struct trace_entry *entry, unsigned long flags,
                (need_resched() ? TRACE_FLAG_NEED_RESCHED : 0);
 }
 
+struct ring_buffer_event *trace_buffer_lock_reserve(struct trace_array *tr,
+                                                   unsigned char type,
+                                                   unsigned long len,
+                                                   unsigned long flags, int pc)
+{
+       struct ring_buffer_event *event;
+
+       event = ring_buffer_lock_reserve(tr->buffer, len);
+       if (event != NULL) {
+               struct trace_entry *ent = ring_buffer_event_data(event);
+
+               tracing_generic_entry_update(ent, flags, pc);
+               ent->type = type;
+       }
+
+       return event;
+}
+static void ftrace_trace_stack(struct trace_array *tr,
+                              unsigned long flags, int skip, int pc);
+static void ftrace_trace_userstack(struct trace_array *tr,
+                                  unsigned long flags, int pc);
+
+void trace_buffer_unlock_commit(struct trace_array *tr,
+                               struct ring_buffer_event *event,
+                               unsigned long flags, int pc)
+{
+       ring_buffer_unlock_commit(tr->buffer, event);
+
+       ftrace_trace_stack(tr, flags, 6, pc);
+       ftrace_trace_userstack(tr, flags, pc);
+       trace_wake_up();
+}
+
+struct ring_buffer_event *
+trace_current_buffer_lock_reserve(unsigned char type, unsigned long len,
+                                 unsigned long flags, int pc)
+{
+       return trace_buffer_lock_reserve(&global_trace,
+                                        type, len, flags, pc);
+}
+
+void trace_current_buffer_unlock_commit(struct ring_buffer_event *event,
+                                       unsigned long flags, int pc)
+{
+       return trace_buffer_unlock_commit(&global_trace, event, flags, pc);
+}
+
 void
-trace_function(struct trace_array *tr, struct trace_array_cpu *data,
+trace_function(struct trace_array *tr,
               unsigned long ip, unsigned long parent_ip, unsigned long flags,
               int pc)
 {
        struct ring_buffer_event *event;
        struct ftrace_entry *entry;
-       unsigned long irq_flags;
 
        /* If we are reading the ring buffer, don't trace */
        if (unlikely(local_read(&__get_cpu_var(ftrace_cpu_disabled))))
                return;
 
-       event = ring_buffer_lock_reserve(tr->buffer, sizeof(*entry),
-                                        &irq_flags);
+       event = trace_buffer_lock_reserve(tr, TRACE_FN, sizeof(*entry),
+                                         flags, pc);
        if (!event)
                return;
        entry   = ring_buffer_event_data(event);
-       tracing_generic_entry_update(&entry->ent, flags, pc);
-       entry->ent.type                 = TRACE_FN;
        entry->ip                       = ip;
        entry->parent_ip                = parent_ip;
-       ring_buffer_unlock_commit(tr->buffer, event, irq_flags);
+       ring_buffer_unlock_commit(tr->buffer, event);
 }
 
 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
 static void __trace_graph_entry(struct trace_array *tr,
-                               struct trace_array_cpu *data,
                                struct ftrace_graph_ent *trace,
                                unsigned long flags,
                                int pc)
 {
        struct ring_buffer_event *event;
        struct ftrace_graph_ent_entry *entry;
-       unsigned long irq_flags;
 
        if (unlikely(local_read(&__get_cpu_var(ftrace_cpu_disabled))))
                return;
 
-       event = ring_buffer_lock_reserve(global_trace.buffer, sizeof(*entry),
-                                        &irq_flags);
+       event = trace_buffer_lock_reserve(&global_trace, TRACE_GRAPH_ENT,
+                                         sizeof(*entry), flags, pc);
        if (!event)
                return;
        entry   = ring_buffer_event_data(event);
-       tracing_generic_entry_update(&entry->ent, flags, pc);
-       entry->ent.type                 = TRACE_GRAPH_ENT;
        entry->graph_ent                        = *trace;
-       ring_buffer_unlock_commit(global_trace.buffer, event, irq_flags);
+       ring_buffer_unlock_commit(global_trace.buffer, event);
 }
 
 static void __trace_graph_return(struct trace_array *tr,
-                               struct trace_array_cpu *data,
                                struct ftrace_graph_ret *trace,
                                unsigned long flags,
                                int pc)
 {
        struct ring_buffer_event *event;
        struct ftrace_graph_ret_entry *entry;
-       unsigned long irq_flags;
 
        if (unlikely(local_read(&__get_cpu_var(ftrace_cpu_disabled))))
                return;
 
-       event = ring_buffer_lock_reserve(global_trace.buffer, sizeof(*entry),
-                                        &irq_flags);
+       event = trace_buffer_lock_reserve(&global_trace, TRACE_GRAPH_RET,
+                                         sizeof(*entry), flags, pc);
        if (!event)
                return;
        entry   = ring_buffer_event_data(event);
-       tracing_generic_entry_update(&entry->ent, flags, pc);
-       entry->ent.type                 = TRACE_GRAPH_RET;
        entry->ret                              = *trace;
-       ring_buffer_unlock_commit(global_trace.buffer, event, irq_flags);
+       ring_buffer_unlock_commit(global_trace.buffer, event);
 }
 #endif
 
@@ -828,11 +923,10 @@ ftrace(struct trace_array *tr, struct trace_array_cpu *data,
        int pc)
 {
        if (likely(!atomic_read(&data->disabled)))
-               trace_function(tr, data, ip, parent_ip, flags, pc);
+               trace_function(tr, ip, parent_ip, flags, pc);
 }
 
 static void __ftrace_trace_stack(struct trace_array *tr,
-                                struct trace_array_cpu *data,
                                 unsigned long flags,
                                 int skip, int pc)
 {
@@ -840,16 +934,12 @@ static void __ftrace_trace_stack(struct trace_array *tr,
        struct ring_buffer_event *event;
        struct stack_entry *entry;
        struct stack_trace trace;
-       unsigned long irq_flags;
 
-       event = ring_buffer_lock_reserve(tr->buffer, sizeof(*entry),
-                                        &irq_flags);
+       event = trace_buffer_lock_reserve(tr, TRACE_STACK,
+                                         sizeof(*entry), flags, pc);
        if (!event)
                return;
        entry   = ring_buffer_event_data(event);
-       tracing_generic_entry_update(&entry->ent, flags, pc);
-       entry->ent.type         = TRACE_STACK;
-
        memset(&entry->caller, 0, sizeof(entry->caller));
 
        trace.nr_entries        = 0;
@@ -858,49 +948,43 @@ static void __ftrace_trace_stack(struct trace_array *tr,
        trace.entries           = entry->caller;
 
        save_stack_trace(&trace);
-       ring_buffer_unlock_commit(tr->buffer, event, irq_flags);
+       ring_buffer_unlock_commit(tr->buffer, event);
 #endif
 }
 
 static void ftrace_trace_stack(struct trace_array *tr,
-                              struct trace_array_cpu *data,
                               unsigned long flags,
                               int skip, int pc)
 {
        if (!(trace_flags & TRACE_ITER_STACKTRACE))
                return;
 
-       __ftrace_trace_stack(tr, data, flags, skip, pc);
+       __ftrace_trace_stack(tr, flags, skip, pc);
 }
 
 void __trace_stack(struct trace_array *tr,
-                  struct trace_array_cpu *data,
                   unsigned long flags,
                   int skip, int pc)
 {
-       __ftrace_trace_stack(tr, data, flags, skip, pc);
+       __ftrace_trace_stack(tr, flags, skip, pc);
 }
 
 static void ftrace_trace_userstack(struct trace_array *tr,
-                  struct trace_array_cpu *data,
-                  unsigned long flags, int pc)
+                                  unsigned long flags, int pc)
 {
 #ifdef CONFIG_STACKTRACE
        struct ring_buffer_event *event;
        struct userstack_entry *entry;
        struct stack_trace trace;
-       unsigned long irq_flags;
 
        if (!(trace_flags & TRACE_ITER_USERSTACKTRACE))
                return;
 
-       event = ring_buffer_lock_reserve(tr->buffer, sizeof(*entry),
-                                        &irq_flags);
+       event = trace_buffer_lock_reserve(tr, TRACE_USER_STACK,
+                                         sizeof(*entry), flags, pc);
        if (!event)
                return;
        entry   = ring_buffer_event_data(event);
-       tracing_generic_entry_update(&entry->ent, flags, pc);
-       entry->ent.type         = TRACE_USER_STACK;
 
        memset(&entry->caller, 0, sizeof(entry->caller));
 
@@ -910,70 +994,58 @@ static void ftrace_trace_userstack(struct trace_array *tr,
        trace.entries           = entry->caller;
 
        save_stack_trace_user(&trace);
-       ring_buffer_unlock_commit(tr->buffer, event, irq_flags);
+       ring_buffer_unlock_commit(tr->buffer, event);
 #endif
 }
 
-void __trace_userstack(struct trace_array *tr,
-                  struct trace_array_cpu *data,
-                  unsigned long flags)
+#ifdef UNUSED
+static void __trace_userstack(struct trace_array *tr, unsigned long flags)
 {
-       ftrace_trace_userstack(tr, data, flags, preempt_count());
+       ftrace_trace_userstack(tr, flags, preempt_count());
 }
+#endif /* UNUSED */
 
 static void
-ftrace_trace_special(void *__tr, void *__data,
+ftrace_trace_special(void *__tr,
                     unsigned long arg1, unsigned long arg2, unsigned long arg3,
                     int pc)
 {
        struct ring_buffer_event *event;
-       struct trace_array_cpu *data = __data;
        struct trace_array *tr = __tr;
        struct special_entry *entry;
-       unsigned long irq_flags;
 
-       event = ring_buffer_lock_reserve(tr->buffer, sizeof(*entry),
-                                        &irq_flags);
+       event = trace_buffer_lock_reserve(tr, TRACE_SPECIAL,
+                                         sizeof(*entry), 0, pc);
        if (!event)
                return;
        entry   = ring_buffer_event_data(event);
-       tracing_generic_entry_update(&entry->ent, 0, pc);
-       entry->ent.type                 = TRACE_SPECIAL;
        entry->arg1                     = arg1;
        entry->arg2                     = arg2;
        entry->arg3                     = arg3;
-       ring_buffer_unlock_commit(tr->buffer, event, irq_flags);
-       ftrace_trace_stack(tr, data, irq_flags, 4, pc);
-       ftrace_trace_userstack(tr, data, irq_flags, pc);
-
-       trace_wake_up();
+       trace_buffer_unlock_commit(tr, event, 0, pc);
 }
 
 void
 __trace_special(void *__tr, void *__data,
                unsigned long arg1, unsigned long arg2, unsigned long arg3)
 {
-       ftrace_trace_special(__tr, __data, arg1, arg2, arg3, preempt_count());
+       ftrace_trace_special(__tr, arg1, arg2, arg3, preempt_count());
 }
 
 void
 tracing_sched_switch_trace(struct trace_array *tr,
-                          struct trace_array_cpu *data,
                           struct task_struct *prev,
                           struct task_struct *next,
                           unsigned long flags, int pc)
 {
        struct ring_buffer_event *event;
        struct ctx_switch_entry *entry;
-       unsigned long irq_flags;
 
-       event = ring_buffer_lock_reserve(tr->buffer, sizeof(*entry),
-                                          &irq_flags);
+       event = trace_buffer_lock_reserve(tr, TRACE_CTX,
+                                         sizeof(*entry), flags, pc);
        if (!event)
                return;
        entry   = ring_buffer_event_data(event);
-       tracing_generic_entry_update(&entry->ent, flags, pc);
-       entry->ent.type                 = TRACE_CTX;
        entry->prev_pid                 = prev->pid;
        entry->prev_prio                = prev->prio;
        entry->prev_state               = prev->state;
@@ -981,29 +1053,23 @@ tracing_sched_switch_trace(struct trace_array *tr,
        entry->next_prio                = next->prio;
        entry->next_state               = next->state;
        entry->next_cpu = task_cpu(next);
-       ring_buffer_unlock_commit(tr->buffer, event, irq_flags);
-       ftrace_trace_stack(tr, data, flags, 5, pc);
-       ftrace_trace_userstack(tr, data, flags, pc);
+       trace_buffer_unlock_commit(tr, event, flags, pc);
 }
 
 void
 tracing_sched_wakeup_trace(struct trace_array *tr,
-                          struct trace_array_cpu *data,
                           struct task_struct *wakee,
                           struct task_struct *curr,
                           unsigned long flags, int pc)
 {
        struct ring_buffer_event *event;
        struct ctx_switch_entry *entry;
-       unsigned long irq_flags;
 
-       event = ring_buffer_lock_reserve(tr->buffer, sizeof(*entry),
-                                          &irq_flags);
+       event = trace_buffer_lock_reserve(tr, TRACE_WAKE,
+                                         sizeof(*entry), flags, pc);
        if (!event)
                return;
        entry   = ring_buffer_event_data(event);
-       tracing_generic_entry_update(&entry->ent, flags, pc);
-       entry->ent.type                 = TRACE_WAKE;
        entry->prev_pid                 = curr->pid;
        entry->prev_prio                = curr->prio;
        entry->prev_state               = curr->state;
@@ -1011,11 +1077,10 @@ tracing_sched_wakeup_trace(struct trace_array *tr,
        entry->next_prio                = wakee->prio;
        entry->next_state               = wakee->state;
        entry->next_cpu                 = task_cpu(wakee);
-       ring_buffer_unlock_commit(tr->buffer, event, irq_flags);
-       ftrace_trace_stack(tr, data, flags, 6, pc);
-       ftrace_trace_userstack(tr, data, flags, pc);
 
-       trace_wake_up();
+       ring_buffer_unlock_commit(tr->buffer, event);
+       ftrace_trace_stack(tr, flags, 6, pc);
+       ftrace_trace_userstack(tr, flags, pc);
 }
 
 void
@@ -1036,7 +1101,7 @@ ftrace_special(unsigned long arg1, unsigned long arg2, unsigned long arg3)
        data = tr->data[cpu];
 
        if (likely(atomic_inc_return(&data->disabled) == 1))
-               ftrace_trace_special(tr, data, arg1, arg2, arg3, pc);
+               ftrace_trace_special(tr, arg1, arg2, arg3, pc);
 
        atomic_dec(&data->disabled);
        local_irq_restore(flags);
@@ -1064,7 +1129,7 @@ int trace_graph_entry(struct ftrace_graph_ent *trace)
        disabled = atomic_inc_return(&data->disabled);
        if (likely(disabled == 1)) {
                pc = preempt_count();
-               __trace_graph_entry(tr, data, trace, flags, pc);
+               __trace_graph_entry(tr, trace, flags, pc);
        }
        /* Only do the atomic if it is not already set */
        if (!test_tsk_trace_graph(current))
@@ -1090,7 +1155,7 @@ void trace_graph_return(struct ftrace_graph_ret *trace)
        disabled = atomic_inc_return(&data->disabled);
        if (likely(disabled == 1)) {
                pc = preempt_count();
-               __trace_graph_return(tr, data, trace, flags, pc);
+               __trace_graph_return(tr, trace, flags, pc);
        }
        if (!trace->depth)
                clear_tsk_trace_graph(current);
@@ -1140,10 +1205,25 @@ __find_next_entry(struct trace_iterator *iter, int *ent_cpu, u64 *ent_ts)
 {
        struct ring_buffer *buffer = iter->tr->buffer;
        struct trace_entry *ent, *next = NULL;
+       int cpu_file = iter->cpu_file;
        u64 next_ts = 0, ts;
        int next_cpu = -1;
        int cpu;
 
+       /*
+        * If we are in a per_cpu trace file, don't bother by iterating over
+        * all cpu and peek directly.
+        */
+       if (cpu_file > TRACE_PIPE_ALL_CPU) {
+               if (ring_buffer_empty_cpu(buffer, cpu_file))
+                       return NULL;
+               ent = peek_next_entry(iter, cpu_file, ent_ts);
+               if (ent_cpu)
+                       *ent_cpu = cpu_file;
+
+               return ent;
+       }
+
        for_each_tracing_cpu(cpu) {
 
                if (ring_buffer_empty_cpu(buffer, cpu))
@@ -1171,8 +1251,8 @@ __find_next_entry(struct trace_iterator *iter, int *ent_cpu, u64 *ent_ts)
 }
 
 /* Find the next real entry, without updating the iterator itself */
-static struct trace_entry *
-find_next_entry(struct trace_iterator *iter, int *ent_cpu, u64 *ent_ts)
+struct trace_entry *trace_find_next_entry(struct trace_iterator *iter,
+                                         int *ent_cpu, u64 *ent_ts)
 {
        return __find_next_entry(iter, ent_cpu, ent_ts);
 }
@@ -1221,19 +1301,32 @@ static void *s_next(struct seq_file *m, void *v, loff_t *pos)
        return ent;
 }
 
+/*
+ * No necessary locking here. The worst thing which can
+ * happen is loosing events consumed at the same time
+ * by a trace_pipe reader.
+ * Other than that, we don't risk to crash the ring buffer
+ * because it serializes the readers.
+ *
+ * The current tracer is copied to avoid a global locking
+ * all around.
+ */
 static void *s_start(struct seq_file *m, loff_t *pos)
 {
        struct trace_iterator *iter = m->private;
+       static struct tracer *old_tracer;
+       int cpu_file = iter->cpu_file;
        void *p = NULL;
        loff_t l = 0;
        int cpu;
 
+       /* copy the tracer to avoid using a global lock all around */
        mutex_lock(&trace_types_lock);
-
-       if (!current_trace || current_trace != iter->trace) {
-               mutex_unlock(&trace_types_lock);
-               return NULL;
+       if (unlikely(old_tracer != current_trace && current_trace)) {
+               old_tracer = current_trace;
+               *iter->trace = *current_trace;
        }
+       mutex_unlock(&trace_types_lock);
 
        atomic_inc(&trace_record_cmdline_disabled);
 
@@ -1244,9 +1337,12 @@ static void *s_start(struct seq_file *m, loff_t *pos)
 
                ftrace_disable_cpu();
 
-               for_each_tracing_cpu(cpu) {
-                       ring_buffer_iter_reset(iter->buffer_iter[cpu]);
-               }
+               if (cpu_file == TRACE_PIPE_ALL_CPU) {
+                       for_each_tracing_cpu(cpu)
+                               ring_buffer_iter_reset(iter->buffer_iter[cpu]);
+               } else
+                       ring_buffer_iter_reset(iter->buffer_iter[cpu_file]);
+
 
                ftrace_enable_cpu();
 
@@ -1264,7 +1360,6 @@ static void *s_start(struct seq_file *m, loff_t *pos)
 static void s_stop(struct seq_file *m, void *p)
 {
        atomic_dec(&trace_record_cmdline_disabled);
-       mutex_unlock(&trace_types_lock);
 }
 
 static void print_lat_help_header(struct seq_file *m)
@@ -1351,57 +1446,6 @@ print_trace_header(struct seq_file *m, struct trace_iterator *iter)
        seq_puts(m, "\n");
 }
 
-static void
-lat_print_generic(struct trace_seq *s, struct trace_entry *entry, int cpu)
-{
-       int hardirq, softirq;
-       char *comm;
-
-       comm = trace_find_cmdline(entry->pid);
-
-       trace_seq_printf(s, "%8.8s-%-5d ", comm, entry->pid);
-       trace_seq_printf(s, "%3d", cpu);
-       trace_seq_printf(s, "%c%c",
-                       (entry->flags & TRACE_FLAG_IRQS_OFF) ? 'd' :
-                        (entry->flags & TRACE_FLAG_IRQS_NOSUPPORT) ? 'X' : '.',
-                       ((entry->flags & TRACE_FLAG_NEED_RESCHED) ? 'N' : '.'));
-
-       hardirq = entry->flags & TRACE_FLAG_HARDIRQ;
-       softirq = entry->flags & TRACE_FLAG_SOFTIRQ;
-       if (hardirq && softirq) {
-               trace_seq_putc(s, 'H');
-       } else {
-               if (hardirq) {
-                       trace_seq_putc(s, 'h');
-               } else {
-                       if (softirq)
-                               trace_seq_putc(s, 's');
-                       else
-                               trace_seq_putc(s, '.');
-               }
-       }
-
-       if (entry->preempt_count)
-               trace_seq_printf(s, "%x", entry->preempt_count);
-       else
-               trace_seq_puts(s, ".");
-}
-
-unsigned long preempt_mark_thresh = 100;
-
-static void
-lat_print_timestamp(struct trace_seq *s, u64 abs_usecs,
-                   unsigned long rel_usecs)
-{
-       trace_seq_printf(s, " %4lldus", abs_usecs);
-       if (rel_usecs > preempt_mark_thresh)
-               trace_seq_puts(s, "!: ");
-       else if (rel_usecs > 1)
-               trace_seq_puts(s, "+: ");
-       else
-               trace_seq_puts(s, " : ");
-}
-
 static void test_cpu_buff_start(struct trace_iterator *iter)
 {
        struct trace_seq *s = &iter->seq;
@@ -1419,55 +1463,31 @@ static void test_cpu_buff_start(struct trace_iterator *iter)
        trace_seq_printf(s, "##### CPU %u buffer started ####\n", iter->cpu);
 }
 
-static enum print_line_t
-print_lat_fmt(struct trace_iterator *iter, unsigned int trace_idx, int cpu)
+static enum print_line_t print_lat_fmt(struct trace_iterator *iter)
 {
        struct trace_seq *s = &iter->seq;
        unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
-       struct trace_entry *next_entry;
        struct trace_event *event;
-       unsigned long verbose = (trace_flags & TRACE_ITER_VERBOSE);
        struct trace_entry *entry = iter->ent;
-       unsigned long abs_usecs;
-       unsigned long rel_usecs;
-       u64 next_ts;
-       char *comm;
-       int ret;
 
        test_cpu_buff_start(iter);
 
-       next_entry = find_next_entry(iter, NULL, &next_ts);
-       if (!next_entry)
-               next_ts = iter->ts;
-       rel_usecs = ns2usecs(next_ts - iter->ts);
-       abs_usecs = ns2usecs(iter->ts - iter->tr->time_start);
-
-       if (verbose) {
-               comm = trace_find_cmdline(entry->pid);
-               trace_seq_printf(s, "%16s %5d %3d %d %08x %08x [%08lx]"
-                                " %ld.%03ldms (+%ld.%03ldms): ",
-                                comm,
-                                entry->pid, cpu, entry->flags,
-                                entry->preempt_count, trace_idx,
-                                ns2usecs(iter->ts),
-                                abs_usecs/1000,
-                                abs_usecs % 1000, rel_usecs/1000,
-                                rel_usecs % 1000);
-       } else {
-               lat_print_generic(s, entry, cpu);
-               lat_print_timestamp(s, abs_usecs, rel_usecs);
-       }
-
        event = ftrace_find_event(entry->type);
-       if (event && event->latency_trace) {
-               ret = event->latency_trace(s, entry, sym_flags);
-               if (ret)
-                       return ret;
-               return TRACE_TYPE_HANDLED;
+
+       if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
+               if (!trace_print_lat_context(iter))
+                       goto partial;
        }
 
-       trace_seq_printf(s, "Unknown type %d\n", entry->type);
+       if (event)
+               return event->latency_trace(iter, sym_flags);
+
+       if (!trace_seq_printf(s, "Unknown type %d\n", entry->type))
+               goto partial;
+
        return TRACE_TYPE_HANDLED;
+partial:
+       return TRACE_TYPE_PARTIAL_LINE;
 }
 
 static enum print_line_t print_trace_fmt(struct trace_iterator *iter)
@@ -1476,44 +1496,27 @@ static enum print_line_t print_trace_fmt(struct trace_iterator *iter)
        unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
        struct trace_entry *entry;
        struct trace_event *event;
-       unsigned long usec_rem;
-       unsigned long long t;
-       unsigned long secs;
-       char *comm;
-       int ret;
 
        entry = iter->ent;
 
        test_cpu_buff_start(iter);
 
-       comm = trace_find_cmdline(iter->ent->pid);
+       event = ftrace_find_event(entry->type);
 
-       t = ns2usecs(iter->ts);
-       usec_rem = do_div(t, 1000000ULL);
-       secs = (unsigned long)t;
+       if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
+               if (!trace_print_context(iter))
+                       goto partial;
+       }
 
-       ret = trace_seq_printf(s, "%16s-%-5d ", comm, entry->pid);
-       if (!ret)
-               return TRACE_TYPE_PARTIAL_LINE;
-       ret = trace_seq_printf(s, "[%03d] ", iter->cpu);
-       if (!ret)
-               return TRACE_TYPE_PARTIAL_LINE;
-       ret = trace_seq_printf(s, "%5lu.%06lu: ", secs, usec_rem);
-       if (!ret)
-               return TRACE_TYPE_PARTIAL_LINE;
+       if (event)
+               return event->trace(iter, sym_flags);
 
-       event = ftrace_find_event(entry->type);
-       if (event && event->trace) {
-               ret = event->trace(s, entry, sym_flags);
-               if (ret)
-                       return ret;
-               return TRACE_TYPE_HANDLED;
-       }
-       ret = trace_seq_printf(s, "Unknown type %d\n", entry->type);
-       if (!ret)
-               return TRACE_TYPE_PARTIAL_LINE;
+       if (!trace_seq_printf(s, "Unknown type %d\n", entry->type))
+               goto partial;
 
        return TRACE_TYPE_HANDLED;
+partial:
+       return TRACE_TYPE_PARTIAL_LINE;
 }
 
 static enum print_line_t print_raw_fmt(struct trace_iterator *iter)
@@ -1521,27 +1524,25 @@ static enum print_line_t print_raw_fmt(struct trace_iterator *iter)
        struct trace_seq *s = &iter->seq;
        struct trace_entry *entry;
        struct trace_event *event;
-       int ret;
 
        entry = iter->ent;
 
-       ret = trace_seq_printf(s, "%d %d %llu ",
-               entry->pid, iter->cpu, iter->ts);
-       if (!ret)
-               return TRACE_TYPE_PARTIAL_LINE;
+       if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
+               if (!trace_seq_printf(s, "%d %d %llu ",
+                                     entry->pid, iter->cpu, iter->ts))
+                       goto partial;
+       }
 
        event = ftrace_find_event(entry->type);
-       if (event && event->raw) {
-               ret = event->raw(s, entry, 0);
-               if (ret)
-                       return ret;
-               return TRACE_TYPE_HANDLED;
-       }
-       ret = trace_seq_printf(s, "%d ?\n", entry->type);
-       if (!ret)
-               return TRACE_TYPE_PARTIAL_LINE;
+       if (event)
+               return event->raw(iter, 0);
+
+       if (!trace_seq_printf(s, "%d ?\n", entry->type))
+               goto partial;
 
        return TRACE_TYPE_HANDLED;
+partial:
+       return TRACE_TYPE_PARTIAL_LINE;
 }
 
 static enum print_line_t print_hex_fmt(struct trace_iterator *iter)
@@ -1553,13 +1554,18 @@ static enum print_line_t print_hex_fmt(struct trace_iterator *iter)
 
        entry = iter->ent;
 
-       SEQ_PUT_HEX_FIELD_RET(s, entry->pid);
-       SEQ_PUT_HEX_FIELD_RET(s, iter->cpu);
-       SEQ_PUT_HEX_FIELD_RET(s, iter->ts);
+       if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
+               SEQ_PUT_HEX_FIELD_RET(s, entry->pid);
+               SEQ_PUT_HEX_FIELD_RET(s, iter->cpu);
+               SEQ_PUT_HEX_FIELD_RET(s, iter->ts);
+       }
 
        event = ftrace_find_event(entry->type);
-       if (event && event->hex)
-               event->hex(s, entry, 0);
+       if (event) {
+               enum print_line_t ret = event->hex(iter, 0);
+               if (ret != TRACE_TYPE_HANDLED)
+                       return ret;
+       }
 
        SEQ_PUT_FIELD_RET(s, newline);
 
@@ -1575,7 +1581,7 @@ static enum print_line_t print_printk_msg_only(struct trace_iterator *iter)
 
        trace_assign_type(field, entry);
 
-       ret = trace_seq_printf(s, field->buf);
+       ret = trace_seq_printf(s, "%s", field->buf);
        if (!ret)
                return TRACE_TYPE_PARTIAL_LINE;
 
@@ -1590,15 +1596,14 @@ static enum print_line_t print_bin_fmt(struct trace_iterator *iter)
 
        entry = iter->ent;
 
-       SEQ_PUT_FIELD_RET(s, entry->pid);
-       SEQ_PUT_FIELD_RET(s, entry->cpu);
-       SEQ_PUT_FIELD_RET(s, iter->ts);
+       if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
+               SEQ_PUT_FIELD_RET(s, entry->pid);
+               SEQ_PUT_FIELD_RET(s, iter->cpu);
+               SEQ_PUT_FIELD_RET(s, iter->ts);
+       }
 
        event = ftrace_find_event(entry->type);
-       if (event && event->binary)
-               event->binary(s, entry, 0);
-
-       return TRACE_TYPE_HANDLED;
+       return event ? event->binary(iter, 0) : TRACE_TYPE_HANDLED;
 }
 
 static int trace_empty(struct trace_iterator *iter)
@@ -1643,7 +1648,7 @@ static enum print_line_t print_trace_line(struct trace_iterator *iter)
                return print_raw_fmt(iter);
 
        if (iter->iter_flags & TRACE_FILE_LAT_FMT)
-               return print_lat_fmt(iter, iter->idx, iter->cpu);
+               return print_lat_fmt(iter);
 
        return print_trace_fmt(iter);
 }
@@ -1686,30 +1691,40 @@ static struct seq_operations tracer_seq_ops = {
 };
 
 static struct trace_iterator *
-__tracing_open(struct inode *inode, struct file *file, int *ret)
+__tracing_open(struct inode *inode, struct file *file)
 {
+       long cpu_file = (long) inode->i_private;
+       void *fail_ret = ERR_PTR(-ENOMEM);
        struct trace_iterator *iter;
        struct seq_file *m;
-       int cpu;
+       int cpu, ret;
 
-       if (tracing_disabled) {
-               *ret = -ENODEV;
-               return NULL;
-       }
+       if (tracing_disabled)
+               return ERR_PTR(-ENODEV);
 
        iter = kzalloc(sizeof(*iter), GFP_KERNEL);
-       if (!iter) {
-               *ret = -ENOMEM;
-               goto out;
-       }
+       if (!iter)
+               return ERR_PTR(-ENOMEM);
 
+       /*
+        * We make a copy of the current tracer to avoid concurrent
+        * changes on it while we are reading.
+        */
        mutex_lock(&trace_types_lock);
+       iter->trace = kzalloc(sizeof(*iter->trace), GFP_KERNEL);
+       if (!iter->trace)
+               goto fail;
+
+       if (current_trace)
+               *iter->trace = *current_trace;
+
        if (current_trace && current_trace->print_max)
                iter->tr = &max_tr;
        else
-               iter->tr = inode->i_private;
-       iter->trace = current_trace;
+               iter->tr = &global_trace;
        iter->pos = -1;
+       mutex_init(&iter->mutex);
+       iter->cpu_file = cpu_file;
 
        /* Notify the tracer early; before we stop tracing. */
        if (iter->trace && iter->trace->open)
@@ -1719,20 +1734,30 @@ __tracing_open(struct inode *inode, struct file *file, int *ret)
        if (ring_buffer_overruns(iter->tr->buffer))
                iter->iter_flags |= TRACE_FILE_ANNOTATE;
 
+       if (iter->cpu_file == TRACE_PIPE_ALL_CPU) {
+               for_each_tracing_cpu(cpu) {
 
-       for_each_tracing_cpu(cpu) {
+                       iter->buffer_iter[cpu] =
+                               ring_buffer_read_start(iter->tr->buffer, cpu);
 
+                       if (!iter->buffer_iter[cpu])
+                               goto fail_buffer;
+               }
+       } else {
+               cpu = iter->cpu_file;
                iter->buffer_iter[cpu] =
-                       ring_buffer_read_start(iter->tr->buffer, cpu);
+                               ring_buffer_read_start(iter->tr->buffer, cpu);
 
                if (!iter->buffer_iter[cpu])
-                       goto fail_buffer;
+                       goto fail;
        }
 
        /* TODO stop tracer */
-       *ret = seq_open(file, &tracer_seq_ops);
-       if (*ret)
+       ret = seq_open(file, &tracer_seq_ops);
+       if (ret < 0) {
+               fail_ret = ERR_PTR(ret);
                goto fail_buffer;
+       }
 
        m = file->private_data;
        m->private = iter;
@@ -1742,7 +1767,6 @@ __tracing_open(struct inode *inode, struct file *file, int *ret)
 
        mutex_unlock(&trace_types_lock);
 
- out:
        return iter;
 
  fail_buffer:
@@ -1750,10 +1774,12 @@ __tracing_open(struct inode *inode, struct file *file, int *ret)
                if (iter->buffer_iter[cpu])
                        ring_buffer_read_finish(iter->buffer_iter[cpu]);
        }
+ fail:
        mutex_unlock(&trace_types_lock);
+       kfree(iter->trace);
        kfree(iter);
 
-       return ERR_PTR(-ENOMEM);
+       return fail_ret;
 }
 
 int tracing_open_generic(struct inode *inode, struct file *filp)
@@ -1765,7 +1791,7 @@ int tracing_open_generic(struct inode *inode, struct file *filp)
        return 0;
 }
 
-int tracing_release(struct inode *inode, struct file *file)
+static int tracing_release(struct inode *inode, struct file *file)
 {
        struct seq_file *m = (struct seq_file *)file->private_data;
        struct trace_iterator *iter = m->private;
@@ -1785,15 +1811,20 @@ int tracing_release(struct inode *inode, struct file *file)
        mutex_unlock(&trace_types_lock);
 
        seq_release(inode, file);
+       mutex_destroy(&iter->mutex);
+       kfree(iter->trace);
        kfree(iter);
        return 0;
 }
 
 static int tracing_open(struct inode *inode, struct file *file)
 {
-       int ret;
+       struct trace_iterator *iter;
+       int ret = 0;
 
-       __tracing_open(inode, file, &ret);
+       iter = __tracing_open(inode, file);
+       if (IS_ERR(iter))
+               ret = PTR_ERR(iter);
 
        return ret;
 }
@@ -1801,11 +1832,13 @@ static int tracing_open(struct inode *inode, struct file *file)
 static int tracing_lt_open(struct inode *inode, struct file *file)
 {
        struct trace_iterator *iter;
-       int ret;
+       int ret = 0;
 
-       iter = __tracing_open(inode, file, &ret);
+       iter = __tracing_open(inode, file);
 
-       if (!ret)
+       if (IS_ERR(iter))
+               ret = PTR_ERR(iter);
+       else
                iter->iter_flags |= TRACE_FILE_LAT_FMT;
 
        return ret;
@@ -2000,57 +2033,62 @@ static ssize_t
 tracing_trace_options_read(struct file *filp, char __user *ubuf,
                       size_t cnt, loff_t *ppos)
 {
-       int i;
+       struct tracer_opt *trace_opts;
+       u32 tracer_flags;
+       int len = 0;
        char *buf;
        int r = 0;
-       int len = 0;
-       u32 tracer_flags = current_trace->flags->val;
-       struct tracer_opt *trace_opts = current_trace->flags->opts;
+       int i;
 
 
-       /* calulate max size */
+       /* calculate max size */
        for (i = 0; trace_options[i]; i++) {
                len += strlen(trace_options[i]);
-               len += 3; /* "no" and space */
+               len += 3; /* "no" and newline */
        }
 
+       mutex_lock(&trace_types_lock);
+       tracer_flags = current_trace->flags->val;
+       trace_opts = current_trace->flags->opts;
+
        /*
         * Increase the size with names of options specific
         * of the current tracer.
         */
        for (i = 0; trace_opts[i].name; i++) {
                len += strlen(trace_opts[i].name);
-               len += 3; /* "no" and space */
+               len += 3; /* "no" and newline */
        }
 
        /* +2 for \n and \0 */
        buf = kmalloc(len + 2, GFP_KERNEL);
-       if (!buf)
+       if (!buf) {
+               mutex_unlock(&trace_types_lock);
                return -ENOMEM;
+       }
 
        for (i = 0; trace_options[i]; i++) {
                if (trace_flags & (1 << i))
-                       r += sprintf(buf + r, "%s ", trace_options[i]);
+                       r += sprintf(buf + r, "%s\n", trace_options[i]);
                else
-                       r += sprintf(buf + r, "no%s ", trace_options[i]);
+                       r += sprintf(buf + r, "no%s\n", trace_options[i]);
        }
 
        for (i = 0; trace_opts[i].name; i++) {
                if (tracer_flags & trace_opts[i].bit)
-                       r += sprintf(buf + r, "%s ",
+                       r += sprintf(buf + r, "%s\n",
                                trace_opts[i].name);
                else
-                       r += sprintf(buf + r, "no%s ",
+                       r += sprintf(buf + r, "no%s\n",
                                trace_opts[i].name);
        }
+       mutex_unlock(&trace_types_lock);
 
-       r += sprintf(buf + r, "\n");
        WARN_ON(r >= len + 2);
 
        r = simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
 
        kfree(buf);
-
        return r;
 }
 
@@ -2125,7 +2163,9 @@ tracing_trace_options_write(struct file *filp, const char __user *ubuf,
 
        /* If no option could be set, test the specific tracer options */
        if (!trace_options[i]) {
+               mutex_lock(&trace_types_lock);
                ret = set_tracer_option(current_trace, cmp, neg);
+               mutex_unlock(&trace_types_lock);
                if (ret)
                        return ret;
        }
@@ -2190,7 +2230,7 @@ tracing_ctrl_write(struct file *filp, const char __user *ubuf,
 {
        struct trace_array *tr = filp->private_data;
        char buf[64];
-       long val;
+       unsigned long val;
        int ret;
 
        if (cnt >= sizeof(buf))
@@ -2245,8 +2285,23 @@ tracing_set_trace_read(struct file *filp, char __user *ubuf,
        return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
 }
 
-static int tracing_set_tracer(char *buf)
+int tracer_init(struct tracer *t, struct trace_array *tr)
+{
+       tracing_reset_online_cpus(tr);
+       return t->init(tr);
+}
+
+struct trace_option_dentry;
+
+static struct trace_option_dentry *
+create_trace_option_files(struct tracer *tracer);
+
+static void
+destroy_trace_option_files(struct trace_option_dentry *topts);
+
+static int tracing_set_tracer(const char *buf)
 {
+       static struct trace_option_dentry *topts;
        struct trace_array *tr = &global_trace;
        struct tracer *t;
        int ret = 0;
@@ -2267,9 +2322,14 @@ static int tracing_set_tracer(char *buf)
        if (current_trace && current_trace->reset)
                current_trace->reset(tr);
 
+       destroy_trace_option_files(topts);
+
        current_trace = t;
+
+       topts = create_trace_option_files(current_trace);
+
        if (t->init) {
-               ret = t->init(tr);
+               ret = tracer_init(t, tr);
                if (ret)
                        goto out;
        }
@@ -2332,9 +2392,9 @@ static ssize_t
 tracing_max_lat_write(struct file *filp, const char __user *ubuf,
                      size_t cnt, loff_t *ppos)
 {
-       long *ptr = filp->private_data;
+       unsigned long *ptr = filp->private_data;
        char buf[64];
-       long val;
+       unsigned long val;
        int ret;
 
        if (cnt >= sizeof(buf))
@@ -2354,54 +2414,96 @@ tracing_max_lat_write(struct file *filp, const char __user *ubuf,
        return cnt;
 }
 
-static atomic_t tracing_reader;
-
 static int tracing_open_pipe(struct inode *inode, struct file *filp)
 {
+       long cpu_file = (long) inode->i_private;
        struct trace_iterator *iter;
+       int ret = 0;
 
        if (tracing_disabled)
                return -ENODEV;
 
-       /* We only allow for reader of the pipe */
-       if (atomic_inc_return(&tracing_reader) != 1) {
-               atomic_dec(&tracing_reader);
-               return -EBUSY;
+       mutex_lock(&trace_types_lock);
+
+       /* We only allow one reader per cpu */
+       if (cpu_file == TRACE_PIPE_ALL_CPU) {
+               if (!cpumask_empty(tracing_reader_cpumask)) {
+                       ret = -EBUSY;
+                       goto out;
+               }
+               cpumask_setall(tracing_reader_cpumask);
+       } else {
+               if (!cpumask_test_cpu(cpu_file, tracing_reader_cpumask))
+                       cpumask_set_cpu(cpu_file, tracing_reader_cpumask);
+               else {
+                       ret = -EBUSY;
+                       goto out;
+               }
        }
 
        /* create a buffer to store the information to pass to userspace */
        iter = kzalloc(sizeof(*iter), GFP_KERNEL);
-       if (!iter)
-               return -ENOMEM;
+       if (!iter) {
+               ret = -ENOMEM;
+               goto out;
+       }
 
-       if (!alloc_cpumask_var(&iter->started, GFP_KERNEL)) {
-               kfree(iter);
-               return -ENOMEM;
+       /*
+        * We make a copy of the current tracer to avoid concurrent
+        * changes on it while we are reading.
+        */
+       iter->trace = kmalloc(sizeof(*iter->trace), GFP_KERNEL);
+       if (!iter->trace) {
+               ret = -ENOMEM;
+               goto fail;
        }
+       if (current_trace)
+               *iter->trace = *current_trace;
 
-       mutex_lock(&trace_types_lock);
+       if (!alloc_cpumask_var(&iter->started, GFP_KERNEL)) {
+               ret = -ENOMEM;
+               goto fail;
+       }
 
        /* trace pipe does not show start of buffer */
        cpumask_setall(iter->started);
 
+       iter->cpu_file = cpu_file;
        iter->tr = &global_trace;
-       iter->trace = current_trace;
+       mutex_init(&iter->mutex);
        filp->private_data = iter;
 
        if (iter->trace->pipe_open)
                iter->trace->pipe_open(iter);
+
+out:
        mutex_unlock(&trace_types_lock);
+       return ret;
 
-       return 0;
+fail:
+       kfree(iter->trace);
+       kfree(iter);
+       mutex_unlock(&trace_types_lock);
+       return ret;
 }
 
 static int tracing_release_pipe(struct inode *inode, struct file *file)
 {
        struct trace_iterator *iter = file->private_data;
 
+       mutex_lock(&trace_types_lock);
+
+       if (iter->cpu_file == TRACE_PIPE_ALL_CPU)
+               cpumask_clear(tracing_reader_cpumask);
+       else
+               cpumask_clear_cpu(iter->cpu_file, tracing_reader_cpumask);
+
+       mutex_unlock(&trace_types_lock);
+
        free_cpumask_var(iter->started);
+       mutex_destroy(&iter->mutex);
+       kfree(iter->trace);
        kfree(iter);
-       atomic_dec(&tracing_reader);
 
        return 0;
 }
@@ -2427,67 +2529,57 @@ tracing_poll_pipe(struct file *filp, poll_table *poll_table)
        }
 }
 
-/*
- * Consumer reader.
- */
-static ssize_t
-tracing_read_pipe(struct file *filp, char __user *ubuf,
-                 size_t cnt, loff_t *ppos)
+
+void default_wait_pipe(struct trace_iterator *iter)
 {
-       struct trace_iterator *iter = filp->private_data;
-       ssize_t sret;
+       DEFINE_WAIT(wait);
 
-       /* return any leftover data */
-       sret = trace_seq_to_user(&iter->seq, ubuf, cnt);
-       if (sret != -EBUSY)
-               return sret;
+       prepare_to_wait(&trace_wait, &wait, TASK_INTERRUPTIBLE);
 
-       trace_seq_reset(&iter->seq);
+       if (trace_empty(iter))
+               schedule();
 
-       mutex_lock(&trace_types_lock);
-       if (iter->trace->read) {
-               sret = iter->trace->read(iter, filp, ubuf, cnt, ppos);
-               if (sret)
-                       goto out;
-       }
+       finish_wait(&trace_wait, &wait);
+}
+
+/*
+ * This is a make-shift waitqueue.
+ * A tracer might use this callback on some rare cases:
+ *
+ *  1) the current tracer might hold the runqueue lock when it wakes up
+ *     a reader, hence a deadlock (sched, function, and function graph tracers)
+ *  2) the function tracers, trace all functions, we don't want
+ *     the overhead of calling wake_up and friends
+ *     (and tracing them too)
+ *
+ *     Anyway, this is really very primitive wakeup.
+ */
+void poll_wait_pipe(struct trace_iterator *iter)
+{
+       set_current_state(TASK_INTERRUPTIBLE);
+       /* sleep for 100 msecs, and try again. */
+       schedule_timeout(HZ / 10);
+}
+
+/* Must be called with trace_types_lock mutex held. */
+static int tracing_wait_pipe(struct file *filp)
+{
+       struct trace_iterator *iter = filp->private_data;
 
-waitagain:
-       sret = 0;
        while (trace_empty(iter)) {
 
                if ((filp->f_flags & O_NONBLOCK)) {
-                       sret = -EAGAIN;
-                       goto out;
+                       return -EAGAIN;
                }
 
-               /*
-                * This is a make-shift waitqueue. The reason we don't use
-                * an actual wait queue is because:
-                *  1) we only ever have one waiter
-                *  2) the tracing, traces all functions, we don't want
-                *     the overhead of calling wake_up and friends
-                *     (and tracing them too)
-                *     Anyway, this is really very primitive wakeup.
-                */
-               set_current_state(TASK_INTERRUPTIBLE);
-               iter->tr->waiter = current;
-
-               mutex_unlock(&trace_types_lock);
-
-               /* sleep for 100 msecs, and try again. */
-               schedule_timeout(HZ/10);
+               mutex_unlock(&iter->mutex);
 
-               mutex_lock(&trace_types_lock);
+               iter->trace->wait_pipe(iter);
 
-               iter->tr->waiter = NULL;
+               mutex_lock(&iter->mutex);
 
-               if (signal_pending(current)) {
-                       sret = -EINTR;
-                       goto out;
-               }
-
-               if (iter->trace != current_trace)
-                       goto out;
+               if (signal_pending(current))
+                       return -EINTR;
 
                /*
                 * We block until we read something and tracing is disabled.
@@ -2500,13 +2592,59 @@ waitagain:
                 */
                if (!tracer_enabled && iter->pos)
                        break;
+       }
+
+       return 1;
+}
+
+/*
+ * Consumer reader.
+ */
+static ssize_t
+tracing_read_pipe(struct file *filp, char __user *ubuf,
+                 size_t cnt, loff_t *ppos)
+{
+       struct trace_iterator *iter = filp->private_data;
+       static struct tracer *old_tracer;
+       ssize_t sret;
+
+       /* return any leftover data */
+       sret = trace_seq_to_user(&iter->seq, ubuf, cnt);
+       if (sret != -EBUSY)
+               return sret;
+
+       trace_seq_init(&iter->seq);
 
-               continue;
+       /* copy the tracer to avoid using a global lock all around */
+       mutex_lock(&trace_types_lock);
+       if (unlikely(old_tracer != current_trace && current_trace)) {
+               old_tracer = current_trace;
+               *iter->trace = *current_trace;
        }
+       mutex_unlock(&trace_types_lock);
+
+       /*
+        * Avoid more than one consumer on a single file descriptor
+        * This is just a matter of traces coherency, the ring buffer itself
+        * is protected.
+        */
+       mutex_lock(&iter->mutex);
+       if (iter->trace->read) {
+               sret = iter->trace->read(iter, filp, ubuf, cnt, ppos);
+               if (sret)
+                       goto out;
+       }
+
+waitagain:
+       sret = tracing_wait_pipe(filp);
+       if (sret <= 0)
+               goto out;
 
        /* stop when tracing is finished */
-       if (trace_empty(iter))
+       if (trace_empty(iter)) {
+               sret = 0;
                goto out;
+       }
 
        if (cnt >= PAGE_SIZE)
                cnt = PAGE_SIZE - 1;
@@ -2527,8 +2665,8 @@ waitagain:
                        iter->seq.len = len;
                        break;
                }
-
-               trace_consume(iter);
+               if (ret != TRACE_TYPE_NO_CONSUME)
+                       trace_consume(iter);
 
                if (iter->seq.len >= cnt)
                        break;
@@ -2537,7 +2675,7 @@ waitagain:
        /* Now copy what we have to the user */
        sret = trace_seq_to_user(&iter->seq, ubuf, cnt);
        if (iter->seq.readpos >= iter->seq.len)
-               trace_seq_reset(&iter->seq);
+               trace_seq_init(&iter->seq);
 
        /*
         * If there was nothing to send to user, inspite of consuming trace
@@ -2547,11 +2685,148 @@ waitagain:
                goto waitagain;
 
 out:
-       mutex_unlock(&trace_types_lock);
+       mutex_unlock(&iter->mutex);
 
        return sret;
 }
 
+static void tracing_pipe_buf_release(struct pipe_inode_info *pipe,
+                                    struct pipe_buffer *buf)
+{
+       __free_page(buf->page);
+}
+
+static void tracing_spd_release_pipe(struct splice_pipe_desc *spd,
+                                    unsigned int idx)
+{
+       __free_page(spd->pages[idx]);
+}
+
+static struct pipe_buf_operations tracing_pipe_buf_ops = {
+       .can_merge              = 0,
+       .map                    = generic_pipe_buf_map,
+       .unmap                  = generic_pipe_buf_unmap,
+       .confirm                = generic_pipe_buf_confirm,
+       .release                = tracing_pipe_buf_release,
+       .steal                  = generic_pipe_buf_steal,
+       .get                    = generic_pipe_buf_get,
+};
+
+static size_t
+tracing_fill_pipe_page(size_t rem, struct trace_iterator *iter)
+{
+       size_t count;
+       int ret;
+
+       /* Seq buffer is page-sized, exactly what we need. */
+       for (;;) {
+               count = iter->seq.len;
+               ret = print_trace_line(iter);
+               count = iter->seq.len - count;
+               if (rem < count) {
+                       rem = 0;
+                       iter->seq.len -= count;
+                       break;
+               }
+               if (ret == TRACE_TYPE_PARTIAL_LINE) {
+                       iter->seq.len -= count;
+                       break;
+               }
+
+               trace_consume(iter);
+               rem -= count;
+               if (!find_next_entry_inc(iter)) {
+                       rem = 0;
+                       iter->ent = NULL;
+                       break;
+               }
+       }
+
+       return rem;
+}
+
+static ssize_t tracing_splice_read_pipe(struct file *filp,
+                                       loff_t *ppos,
+                                       struct pipe_inode_info *pipe,
+                                       size_t len,
+                                       unsigned int flags)
+{
+       struct page *pages[PIPE_BUFFERS];
+       struct partial_page partial[PIPE_BUFFERS];
+       struct trace_iterator *iter = filp->private_data;
+       struct splice_pipe_desc spd = {
+               .pages          = pages,
+               .partial        = partial,
+               .nr_pages       = 0, /* This gets updated below. */
+               .flags          = flags,
+               .ops            = &tracing_pipe_buf_ops,
+               .spd_release    = tracing_spd_release_pipe,
+       };
+       static struct tracer *old_tracer;
+       ssize_t ret;
+       size_t rem;
+       unsigned int i;
+
+       /* copy the tracer to avoid using a global lock all around */
+       mutex_lock(&trace_types_lock);
+       if (unlikely(old_tracer != current_trace && current_trace)) {
+               old_tracer = current_trace;
+               *iter->trace = *current_trace;
+       }
+       mutex_unlock(&trace_types_lock);
+
+       mutex_lock(&iter->mutex);
+
+       if (iter->trace->splice_read) {
+               ret = iter->trace->splice_read(iter, filp,
+                                              ppos, pipe, len, flags);
+               if (ret)
+                       goto out_err;
+       }
+
+       ret = tracing_wait_pipe(filp);
+       if (ret <= 0)
+               goto out_err;
+
+       if (!iter->ent && !find_next_entry_inc(iter)) {
+               ret = -EFAULT;
+               goto out_err;
+       }
+
+       /* Fill as many pages as possible. */
+       for (i = 0, rem = len; i < PIPE_BUFFERS && rem; i++) {
+               pages[i] = alloc_page(GFP_KERNEL);
+               if (!pages[i])
+                       break;
+
+               rem = tracing_fill_pipe_page(rem, iter);
+
+               /* Copy the data into the page, so we can start over. */
+               ret = trace_seq_to_buffer(&iter->seq,
+                                         page_address(pages[i]),
+                                         iter->seq.len);
+               if (ret < 0) {
+                       __free_page(pages[i]);
+                       break;
+               }
+               partial[i].offset = 0;
+               partial[i].len = iter->seq.len;
+
+               trace_seq_init(&iter->seq);
+       }
+
+       mutex_unlock(&iter->mutex);
+
+       spd.nr_pages = i;
+
+       return splice_to_pipe(pipe, &spd);
+
+out_err:
+       mutex_unlock(&iter->mutex);
+
+       return ret;
+}
+
 static ssize_t
 tracing_entries_read(struct file *filp, char __user *ubuf,
                     size_t cnt, loff_t *ppos)
@@ -2715,6 +2990,7 @@ static struct file_operations tracing_pipe_fops = {
        .open           = tracing_open_pipe,
        .poll           = tracing_poll_pipe,
        .read           = tracing_read_pipe,
+       .splice_read    = tracing_splice_read_pipe,
        .release        = tracing_release_pipe,
 };
 
@@ -2786,15 +3062,345 @@ struct dentry *tracing_init_dentry(void)
        return d_tracer;
 }
 
+static struct dentry *d_percpu;
+
+struct dentry *tracing_dentry_percpu(void)
+{
+       static int once;
+       struct dentry *d_tracer;
+
+       if (d_percpu)
+               return d_percpu;
+
+       d_tracer = tracing_init_dentry();
+
+       if (!d_tracer)
+               return NULL;
+
+       d_percpu = debugfs_create_dir("per_cpu", d_tracer);
+
+       if (!d_percpu && !once) {
+               once = 1;
+               pr_warning("Could not create debugfs directory 'per_cpu'\n");
+               return NULL;
+       }
+
+       return d_percpu;
+}
+
+static void tracing_init_debugfs_percpu(long cpu)
+{
+       struct dentry *d_percpu = tracing_dentry_percpu();
+       struct dentry *entry, *d_cpu;
+       /* strlen(cpu) + MAX(log10(cpu)) + '\0' */
+       char cpu_dir[7];
+
+       if (cpu > 999 || cpu < 0)
+               return;
+
+       sprintf(cpu_dir, "cpu%ld", cpu);
+       d_cpu = debugfs_create_dir(cpu_dir, d_percpu);
+       if (!d_cpu) {
+               pr_warning("Could not create debugfs '%s' entry\n", cpu_dir);
+               return;
+       }
+
+       /* per cpu trace_pipe */
+       entry = debugfs_create_file("trace_pipe", 0444, d_cpu,
+                               (void *) cpu, &tracing_pipe_fops);
+       if (!entry)
+               pr_warning("Could not create debugfs 'trace_pipe' entry\n");
+
+       /* per cpu trace */
+       entry = debugfs_create_file("trace", 0444, d_cpu,
+                               (void *) cpu, &tracing_fops);
+       if (!entry)
+               pr_warning("Could not create debugfs 'trace' entry\n");
+}
+
 #ifdef CONFIG_FTRACE_SELFTEST
 /* Let selftest have access to static functions in this file */
 #include "trace_selftest.c"
 #endif
 
+struct trace_option_dentry {
+       struct tracer_opt               *opt;
+       struct tracer_flags             *flags;
+       struct dentry                   *entry;
+};
+
+static ssize_t
+trace_options_read(struct file *filp, char __user *ubuf, size_t cnt,
+                       loff_t *ppos)
+{
+       struct trace_option_dentry *topt = filp->private_data;
+       char *buf;
+
+       if (topt->flags->val & topt->opt->bit)
+               buf = "1\n";
+       else
+               buf = "0\n";
+
+       return simple_read_from_buffer(ubuf, cnt, ppos, buf, 2);
+}
+
+static ssize_t
+trace_options_write(struct file *filp, const char __user *ubuf, size_t cnt,
+                        loff_t *ppos)
+{
+       struct trace_option_dentry *topt = filp->private_data;
+       unsigned long val;
+       char buf[64];
+       int ret;
+
+       if (cnt >= sizeof(buf))
+               return -EINVAL;
+
+       if (copy_from_user(&buf, ubuf, cnt))
+               return -EFAULT;
+
+       buf[cnt] = 0;
+
+       ret = strict_strtoul(buf, 10, &val);
+       if (ret < 0)
+               return ret;
+
+       ret = 0;
+       switch (val) {
+       case 0:
+               /* do nothing if already cleared */
+               if (!(topt->flags->val & topt->opt->bit))
+                       break;
+
+               mutex_lock(&trace_types_lock);
+               if (current_trace->set_flag)
+                       ret = current_trace->set_flag(topt->flags->val,
+                                                     topt->opt->bit, 0);
+               mutex_unlock(&trace_types_lock);
+               if (ret)
+                       return ret;
+               topt->flags->val &= ~topt->opt->bit;
+               break;
+       case 1:
+               /* do nothing if already set */
+               if (topt->flags->val & topt->opt->bit)
+                       break;
+
+               mutex_lock(&trace_types_lock);
+               if (current_trace->set_flag)
+                       ret = current_trace->set_flag(topt->flags->val,
+                                                     topt->opt->bit, 1);
+               mutex_unlock(&trace_types_lock);
+               if (ret)
+                       return ret;
+               topt->flags->val |= topt->opt->bit;
+               break;
+
+       default:
+               return -EINVAL;
+       }
+
+       *ppos += cnt;
+
+       return cnt;
+}
+
+
+static const struct file_operations trace_options_fops = {
+       .open = tracing_open_generic,
+       .read = trace_options_read,
+       .write = trace_options_write,
+};
+
+static ssize_t
+trace_options_core_read(struct file *filp, char __user *ubuf, size_t cnt,
+                       loff_t *ppos)
+{
+       long index = (long)filp->private_data;
+       char *buf;
+
+       if (trace_flags & (1 << index))
+               buf = "1\n";
+       else
+               buf = "0\n";
+
+       return simple_read_from_buffer(ubuf, cnt, ppos, buf, 2);
+}
+
+static ssize_t
+trace_options_core_write(struct file *filp, const char __user *ubuf, size_t cnt,
+                        loff_t *ppos)
+{
+       long index = (long)filp->private_data;
+       char buf[64];
+       unsigned long val;
+       int ret;
+
+       if (cnt >= sizeof(buf))
+               return -EINVAL;
+
+       if (copy_from_user(&buf, ubuf, cnt))
+               return -EFAULT;
+
+       buf[cnt] = 0;
+
+       ret = strict_strtoul(buf, 10, &val);
+       if (ret < 0)
+               return ret;
+
+       switch (val) {
+       case 0:
+               trace_flags &= ~(1 << index);
+               break;
+       case 1:
+               trace_flags |= 1 << index;
+               break;
+
+       default:
+               return -EINVAL;
+       }
+
+       *ppos += cnt;
+
+       return cnt;
+}
+
+static const struct file_operations trace_options_core_fops = {
+       .open = tracing_open_generic,
+       .read = trace_options_core_read,
+       .write = trace_options_core_write,
+};
+
+static struct dentry *trace_options_init_dentry(void)
+{
+       struct dentry *d_tracer;
+       static struct dentry *t_options;
+
+       if (t_options)
+               return t_options;
+
+       d_tracer = tracing_init_dentry();
+       if (!d_tracer)
+               return NULL;
+
+       t_options = debugfs_create_dir("options", d_tracer);
+       if (!t_options) {
+               pr_warning("Could not create debugfs directory 'options'\n");
+               return NULL;
+       }
+
+       return t_options;
+}
+
+static void
+create_trace_option_file(struct trace_option_dentry *topt,
+                        struct tracer_flags *flags,
+                        struct tracer_opt *opt)
+{
+       struct dentry *t_options;
+       struct dentry *entry;
+
+       t_options = trace_options_init_dentry();
+       if (!t_options)
+               return;
+
+       topt->flags = flags;
+       topt->opt = opt;
+
+       entry = debugfs_create_file(opt->name, 0644, t_options, topt,
+                                   &trace_options_fops);
+
+       topt->entry = entry;
+
+}
+
+static struct trace_option_dentry *
+create_trace_option_files(struct tracer *tracer)
+{
+       struct trace_option_dentry *topts;
+       struct tracer_flags *flags;
+       struct tracer_opt *opts;
+       int cnt;
+
+       if (!tracer)
+               return NULL;
+
+       flags = tracer->flags;
+
+       if (!flags || !flags->opts)
+               return NULL;
+
+       opts = flags->opts;
+
+       for (cnt = 0; opts[cnt].name; cnt++)
+               ;
+
+       topts = kcalloc(cnt + 1, sizeof(*topts), GFP_KERNEL);
+       if (!topts)
+               return NULL;
+
+       for (cnt = 0; opts[cnt].name; cnt++)
+               create_trace_option_file(&topts[cnt], flags,
+                                        &opts[cnt]);
+
+       return topts;
+}
+
+static void
+destroy_trace_option_files(struct trace_option_dentry *topts)
+{
+       int cnt;
+
+       if (!topts)
+               return;
+
+       for (cnt = 0; topts[cnt].opt; cnt++) {
+               if (topts[cnt].entry)
+                       debugfs_remove(topts[cnt].entry);
+       }
+
+       kfree(topts);
+}
+
+static struct dentry *
+create_trace_option_core_file(const char *option, long index)
+{
+       struct dentry *t_options;
+       struct dentry *entry;
+
+       t_options = trace_options_init_dentry();
+       if (!t_options)
+               return NULL;
+
+       entry = debugfs_create_file(option, 0644, t_options, (void *)index,
+                                   &trace_options_core_fops);
+
+       return entry;
+}
+
+static __init void create_trace_options_dir(void)
+{
+       struct dentry *t_options;
+       struct dentry *entry;
+       int i;
+
+       t_options = trace_options_init_dentry();
+       if (!t_options)
+               return;
+
+       for (i = 0; trace_options[i]; i++) {
+               entry = create_trace_option_core_file(trace_options[i], i);
+               if (!entry)
+                       pr_warning("Could not create debugfs %s entry\n",
+                                  trace_options[i]);
+       }
+}
+
 static __init int tracer_init_debugfs(void)
 {
        struct dentry *d_tracer;
        struct dentry *entry;
+       int cpu;
 
        d_tracer = tracing_init_dentry();
 
@@ -2808,18 +3414,15 @@ static __init int tracer_init_debugfs(void)
        if (!entry)
                pr_warning("Could not create debugfs 'trace_options' entry\n");
 
+       create_trace_options_dir();
+
        entry = debugfs_create_file("tracing_cpumask", 0644, d_tracer,
                                    NULL, &tracing_cpumask_fops);
        if (!entry)
                pr_warning("Could not create debugfs 'tracing_cpumask' entry\n");
 
-       entry = debugfs_create_file("latency_trace", 0444, d_tracer,
-                                   &global_trace, &tracing_lt_fops);
-       if (!entry)
-               pr_warning("Could not create debugfs 'latency_trace' entry\n");
-
        entry = debugfs_create_file("trace", 0444, d_tracer,
-                                   &global_trace, &tracing_fops);
+                                (void *) TRACE_PIPE_ALL_CPU, &tracing_fops);
        if (!entry)
                pr_warning("Could not create debugfs 'trace' entry\n");
 
@@ -2850,8 +3453,8 @@ static __init int tracer_init_debugfs(void)
        if (!entry)
                pr_warning("Could not create debugfs 'README' entry\n");
 
-       entry = debugfs_create_file("trace_pipe", 0644, d_tracer,
-                                   NULL, &tracing_pipe_fops);
+       entry = debugfs_create_file("trace_pipe", 0444, d_tracer,
+                       (void *) TRACE_PIPE_ALL_CPU, &tracing_pipe_fops);
        if (!entry)
                pr_warning("Could not create debugfs "
                           "'trace_pipe' entry\n");
@@ -2879,6 +3482,10 @@ static __init int tracer_init_debugfs(void)
 #ifdef CONFIG_SYSPROF_TRACER
        init_tracer_sysprof_debugfs(d_tracer);
 #endif
+
+       for_each_tracing_cpu(cpu)
+               tracing_init_debugfs_percpu(cpu);
+
        return 0;
 }
 
@@ -2913,18 +3520,16 @@ int trace_vprintk(unsigned long ip, int depth, const char *fmt, va_list args)
        trace_buf[len] = 0;
 
        size = sizeof(*entry) + len + 1;
-       event = ring_buffer_lock_reserve(tr->buffer, size, &irq_flags);
+       event = trace_buffer_lock_reserve(tr, TRACE_PRINT, size, irq_flags, pc);
        if (!event)
                goto out_unlock;
        entry = ring_buffer_event_data(event);
-       tracing_generic_entry_update(&entry->ent, irq_flags, pc);
-       entry->ent.type                 = TRACE_PRINT;
        entry->ip                       = ip;
        entry->depth                    = depth;
 
        memcpy(&entry->buf, trace_buf, len);
        entry->buf[len] = 0;
-       ring_buffer_unlock_commit(tr->buffer, event, irq_flags);
+       ring_buffer_unlock_commit(tr->buffer, event);
 
  out_unlock:
        spin_unlock_irqrestore(&trace_buf_lock, irq_flags);
@@ -3019,7 +3624,7 @@ trace_printk_seq(struct trace_seq *s)
 
        printk(KERN_TRACE "%s", s->buffer);
 
-       trace_seq_reset(s);
+       trace_seq_init(s);
 }
 
 void ftrace_dump(void)
@@ -3104,8 +3709,12 @@ __init static int tracer_alloc_buffers(void)
        if (!alloc_cpumask_var(&tracing_cpumask, GFP_KERNEL))
                goto out_free_buffer_mask;
 
+       if (!alloc_cpumask_var(&tracing_reader_cpumask, GFP_KERNEL))
+               goto out_free_tracing_cpumask;
+
        cpumask_copy(tracing_buffer_mask, cpu_possible_mask);
        cpumask_copy(tracing_cpumask, cpu_all_mask);
+       cpumask_clear(tracing_reader_cpumask);
 
        /* TODO: make the number of buffers hot pluggable with CPUS */
        global_trace.buffer = ring_buffer_alloc(trace_buf_size,
@@ -3140,12 +3749,9 @@ __init static int tracer_alloc_buffers(void)
        trace_init_cmdlines();
 
        register_tracer(&nop_trace);
+       current_trace = &nop_trace;
 #ifdef CONFIG_BOOT_TRACER
        register_tracer(&boot_tracer);
-       current_trace = &boot_tracer;
-       current_trace->init(&global_trace);
-#else
-       current_trace = &nop_trace;
 #endif
        /* All seems OK, enable tracing */
        tracing_disabled = 0;
@@ -3157,11 +3763,34 @@ __init static int tracer_alloc_buffers(void)
        ret = 0;
 
 out_free_cpumask:
+       free_cpumask_var(tracing_reader_cpumask);
+out_free_tracing_cpumask:
        free_cpumask_var(tracing_cpumask);
 out_free_buffer_mask:
        free_cpumask_var(tracing_buffer_mask);
 out:
        return ret;
 }
+
+__init static int clear_boot_tracer(void)
+{
+       /*
+        * The default tracer at boot buffer is an init section.
+        * This function is called in lateinit. If we did not
+        * find the boot tracer, then clear it out, to prevent
+        * later registration from accessing the buffer that is
+        * about to be freed.
+        */
+       if (!default_bootup_tracer)
+               return 0;
+
+       printk(KERN_INFO "ftrace bootup tracer '%s' not registered.\n",
+              default_bootup_tracer);
+       default_bootup_tracer = NULL;
+
+       return 0;
+}
+
 early_initcall(tracer_alloc_buffers);
 fs_initcall(tracer_init_debugfs);
+late_initcall(clear_boot_tracer);