trace: set max latency variable to zero on default
[safe/jmp/linux-2.6] / kernel / trace / ring_buffer.c
index 8e7392f..9c1e73d 100644 (file)
 #include <linux/list.h>
 #include <linux/fs.h>
 
+#include "trace.h"
+
+/*
+ * A fast way to enable or disable all ring buffers is to
+ * call tracing_on or tracing_off. Turning off the ring buffers
+ * prevents all ring buffers from being recorded to.
+ * Turning this switch on, makes it OK to write to the
+ * ring buffer, if the ring buffer is enabled itself.
+ *
+ * There's three layers that must be on in order to write
+ * to the ring buffer.
+ *
+ * 1) This global flag must be set.
+ * 2) The ring buffer must be enabled for recording.
+ * 3) The per cpu buffer must be enabled for recording.
+ *
+ * In case of an anomaly, this global flag has a bit set that
+ * will permantly disable all ring buffers.
+ */
+
+/*
+ * Global flag to disable all recording to ring buffers
+ *  This has two bits: ON, DISABLED
+ *
+ *  ON   DISABLED
+ * ---- ----------
+ *   0      0        : ring buffers are off
+ *   1      0        : ring buffers are on
+ *   X      1        : ring buffers are permanently disabled
+ */
+
+enum {
+       RB_BUFFERS_ON_BIT       = 0,
+       RB_BUFFERS_DISABLED_BIT = 1,
+};
+
+enum {
+       RB_BUFFERS_ON           = 1 << RB_BUFFERS_ON_BIT,
+       RB_BUFFERS_DISABLED     = 1 << RB_BUFFERS_DISABLED_BIT,
+};
+
+static long ring_buffer_flags __read_mostly = RB_BUFFERS_ON;
+
+/**
+ * tracing_on - enable all tracing buffers
+ *
+ * This function enables all tracing buffers that may have been
+ * disabled with tracing_off.
+ */
+void tracing_on(void)
+{
+       set_bit(RB_BUFFERS_ON_BIT, &ring_buffer_flags);
+}
+EXPORT_SYMBOL_GPL(tracing_on);
+
+/**
+ * tracing_off - turn off all tracing buffers
+ *
+ * This function stops all tracing buffers from recording data.
+ * It does not disable any overhead the tracers themselves may
+ * be causing. This function simply causes all recording to
+ * the ring buffers to fail.
+ */
+void tracing_off(void)
+{
+       clear_bit(RB_BUFFERS_ON_BIT, &ring_buffer_flags);
+}
+EXPORT_SYMBOL_GPL(tracing_off);
+
+/**
+ * tracing_off_permanent - permanently disable ring buffers
+ *
+ * This function, once called, will disable all ring buffers
+ * permanenty.
+ */
+void tracing_off_permanent(void)
+{
+       set_bit(RB_BUFFERS_DISABLED_BIT, &ring_buffer_flags);
+}
+
+#include "trace.h"
+
 /* Up this if you want to test the TIME_EXTENTS and normalization */
 #define DEBUG_SHIFT 0
 
 /* FIXME!!! */
 u64 ring_buffer_time_stamp(int cpu)
 {
+       u64 time;
+
+       preempt_disable_notrace();
        /* shift to debug/test normalization and TIME_EXTENTS */
-       return sched_clock() << DEBUG_SHIFT;
+       time = sched_clock() << DEBUG_SHIFT;
+       preempt_enable_no_resched_notrace();
+
+       return time;
 }
+EXPORT_SYMBOL_GPL(ring_buffer_time_stamp);
 
 void ring_buffer_normalize_time_stamp(int cpu, u64 *ts)
 {
        /* Just stupid testing the normalize function and deltas */
        *ts >>= DEBUG_SHIFT;
 }
+EXPORT_SYMBOL_GPL(ring_buffer_normalize_time_stamp);
 
 #define RB_EVNT_HDR_SIZE (sizeof(struct ring_buffer_event))
 #define RB_ALIGNMENT_SHIFT     2
@@ -78,8 +168,15 @@ rb_event_length(struct ring_buffer_event *event)
  */
 unsigned ring_buffer_event_length(struct ring_buffer_event *event)
 {
-       return rb_event_length(event);
+       unsigned length = rb_event_length(event);
+       if (event->type != RINGBUF_TYPE_DATA)
+               return length;
+       length -= RB_EVNT_HDR_SIZE;
+       if (length > RB_MAX_SMALL_DATA + sizeof(event->array[0]))
+                length -= sizeof(event->array[0]);
+       return length;
 }
+EXPORT_SYMBOL_GPL(ring_buffer_event_length);
 
 /* inline for ring buffer fast paths */
 static inline void *
@@ -101,41 +198,42 @@ void *ring_buffer_event_data(struct ring_buffer_event *event)
 {
        return rb_event_data(event);
 }
+EXPORT_SYMBOL_GPL(ring_buffer_event_data);
 
 #define for_each_buffer_cpu(buffer, cpu)               \
-       for_each_cpu_mask(cpu, buffer->cpumask)
+       for_each_cpu(cpu, buffer->cpumask)
 
 #define TS_SHIFT       27
 #define TS_MASK                ((1ULL << TS_SHIFT) - 1)
 #define TS_DELTA_TEST  (~TS_MASK)
 
-/*
- * This hack stolen from mm/slob.c.
- * We can store per page timing information in the page frame of the page.
- * Thanks to Peter Zijlstra for suggesting this idea.
- */
+struct buffer_data_page {
+       u64              time_stamp;    /* page time stamp */
+       local_t          commit;        /* write commited index */
+       unsigned char    data[];        /* data of buffer page */
+};
+
 struct buffer_page {
-       union {
-               struct {
-                       unsigned long    flags;         /* mandatory */
-                       atomic_t         _count;        /* mandatory */
-                       u64              time_stamp;    /* page time stamp */
-                       unsigned         size;          /* size of page data */
-                       struct list_head list;          /* list of free pages */
-               };
-               struct page page;
-       };
+       local_t          write;         /* index for next write */
+       unsigned         read;          /* index for next read */
+       struct list_head list;          /* list of free pages */
+       struct buffer_data_page *page;  /* Actual data page */
 };
 
+static void rb_init_page(struct buffer_data_page *bpage)
+{
+       local_set(&bpage->commit, 0);
+}
+
 /*
  * Also stolen from mm/slob.c. Thanks to Mathieu Desnoyers for pointing
  * this issue out.
  */
 static inline void free_buffer_page(struct buffer_page *bpage)
 {
-       reset_page_mapcount(&bpage->page);
-       bpage->page.mapping = NULL;
-       __free_page(&bpage->page);
+       if (bpage->page)
+               free_page((unsigned long)bpage->page);
+       kfree(bpage);
 }
 
 /*
@@ -148,7 +246,7 @@ static inline int test_time_stamp(u64 delta)
        return 0;
 }
 
-#define BUF_PAGE_SIZE PAGE_SIZE
+#define BUF_PAGE_SIZE (PAGE_SIZE - offsetof(struct buffer_data_page, data))
 
 /*
  * head_page == tail_page && head == tail then buffer is empty.
@@ -156,13 +254,14 @@ static inline int test_time_stamp(u64 delta)
 struct ring_buffer_per_cpu {
        int                             cpu;
        struct ring_buffer              *buffer;
-       spinlock_t                      lock;
+       spinlock_t                      reader_lock; /* serialize readers */
+       raw_spinlock_t                  lock;
        struct lock_class_key           lock_key;
        struct list_head                pages;
-       unsigned long                   head;   /* read from head */
-       unsigned long                   tail;   /* write to tail */
-       struct buffer_page              *head_page;
-       struct buffer_page              *tail_page;
+       struct buffer_page              *head_page;     /* read from head */
+       struct buffer_page              *tail_page;     /* write to tail */
+       struct buffer_page              *commit_page;   /* commited pages */
+       struct buffer_page              *reader_page;
        unsigned long                   overrun;
        unsigned long                   entries;
        u64                             write_stamp;
@@ -171,11 +270,10 @@ struct ring_buffer_per_cpu {
 };
 
 struct ring_buffer {
-       unsigned long                   size;
        unsigned                        pages;
        unsigned                        flags;
        int                             cpus;
-       cpumask_t                       cpumask;
+       cpumask_var_t                   cpumask;
        atomic_t                        record_disabled;
 
        struct mutex                    mutex;
@@ -190,12 +288,16 @@ struct ring_buffer_iter {
        u64                             read_stamp;
 };
 
-#define RB_WARN_ON(buffer, cond)                       \
-       if (unlikely(cond)) {                           \
-               atomic_inc(&buffer->record_disabled);   \
-               WARN_ON(1);                             \
-               return -1;                              \
-       }
+/* buffer may be either ring_buffer or ring_buffer_per_cpu */
+#define RB_WARN_ON(buffer, cond)                               \
+       ({                                                      \
+               int _____ret = unlikely(cond);                  \
+               if (_____ret) {                                 \
+                       atomic_inc(&buffer->record_disabled);   \
+                       WARN_ON(1);                             \
+               }                                               \
+               _____ret;                                       \
+       })
 
 /**
  * check_pages - integrity check of buffer pages
@@ -207,39 +309,46 @@ struct ring_buffer_iter {
 static int rb_check_pages(struct ring_buffer_per_cpu *cpu_buffer)
 {
        struct list_head *head = &cpu_buffer->pages;
-       struct buffer_page *page, *tmp;
+       struct buffer_page *bpage, *tmp;
 
-       RB_WARN_ON(cpu_buffer, head->next->prev != head);
-       RB_WARN_ON(cpu_buffer, head->prev->next != head);
+       if (RB_WARN_ON(cpu_buffer, head->next->prev != head))
+               return -1;
+       if (RB_WARN_ON(cpu_buffer, head->prev->next != head))
+               return -1;
 
-       list_for_each_entry_safe(page, tmp, head, list) {
-               RB_WARN_ON(cpu_buffer, page->list.next->prev != &page->list);
-               RB_WARN_ON(cpu_buffer, page->list.prev->next != &page->list);
+       list_for_each_entry_safe(bpage, tmp, head, list) {
+               if (RB_WARN_ON(cpu_buffer,
+                              bpage->list.next->prev != &bpage->list))
+                       return -1;
+               if (RB_WARN_ON(cpu_buffer,
+                              bpage->list.prev->next != &bpage->list))
+                       return -1;
        }
 
        return 0;
 }
 
-static unsigned rb_head_size(struct ring_buffer_per_cpu *cpu_buffer)
-{
-       return cpu_buffer->head_page->size;
-}
-
 static int rb_allocate_pages(struct ring_buffer_per_cpu *cpu_buffer,
                             unsigned nr_pages)
 {
        struct list_head *head = &cpu_buffer->pages;
-       struct buffer_page *page, *tmp;
+       struct buffer_page *bpage, *tmp;
        unsigned long addr;
        LIST_HEAD(pages);
        unsigned i;
 
        for (i = 0; i < nr_pages; i++) {
+               bpage = kzalloc_node(ALIGN(sizeof(*bpage), cache_line_size()),
+                                   GFP_KERNEL, cpu_to_node(cpu_buffer->cpu));
+               if (!bpage)
+                       goto free_pages;
+               list_add(&bpage->list, &pages);
+
                addr = __get_free_page(GFP_KERNEL);
                if (!addr)
                        goto free_pages;
-               page = (struct buffer_page *)virt_to_page(addr);
-               list_add(&page->list, &pages);
+               bpage->page = (void *)addr;
+               rb_init_page(bpage->page);
        }
 
        list_splice(&pages, head);
@@ -249,9 +358,9 @@ static int rb_allocate_pages(struct ring_buffer_per_cpu *cpu_buffer,
        return 0;
 
  free_pages:
-       list_for_each_entry_safe(page, tmp, &pages, list) {
-               list_del_init(&page->list);
-               free_buffer_page(page);
+       list_for_each_entry_safe(bpage, tmp, &pages, list) {
+               list_del_init(&bpage->list);
+               free_buffer_page(bpage);
        }
        return -ENOMEM;
 }
@@ -260,6 +369,8 @@ static struct ring_buffer_per_cpu *
 rb_allocate_cpu_buffer(struct ring_buffer *buffer, int cpu)
 {
        struct ring_buffer_per_cpu *cpu_buffer;
+       struct buffer_page *bpage;
+       unsigned long addr;
        int ret;
 
        cpu_buffer = kzalloc_node(ALIGN(sizeof(*cpu_buffer), cache_line_size()),
@@ -269,20 +380,37 @@ rb_allocate_cpu_buffer(struct ring_buffer *buffer, int cpu)
 
        cpu_buffer->cpu = cpu;
        cpu_buffer->buffer = buffer;
-       spin_lock_init(&cpu_buffer->lock);
+       spin_lock_init(&cpu_buffer->reader_lock);
+       cpu_buffer->lock = (raw_spinlock_t)__RAW_SPIN_LOCK_UNLOCKED;
        INIT_LIST_HEAD(&cpu_buffer->pages);
 
+       bpage = kzalloc_node(ALIGN(sizeof(*bpage), cache_line_size()),
+                           GFP_KERNEL, cpu_to_node(cpu));
+       if (!bpage)
+               goto fail_free_buffer;
+
+       cpu_buffer->reader_page = bpage;
+       addr = __get_free_page(GFP_KERNEL);
+       if (!addr)
+               goto fail_free_reader;
+       bpage->page = (void *)addr;
+       rb_init_page(bpage->page);
+
+       INIT_LIST_HEAD(&cpu_buffer->reader_page->list);
+
        ret = rb_allocate_pages(cpu_buffer, buffer->pages);
        if (ret < 0)
-               goto fail_free_buffer;
+               goto fail_free_reader;
 
        cpu_buffer->head_page
                = list_entry(cpu_buffer->pages.next, struct buffer_page, list);
-       cpu_buffer->tail_page
-               = list_entry(cpu_buffer->pages.next, struct buffer_page, list);
+       cpu_buffer->tail_page = cpu_buffer->commit_page = cpu_buffer->head_page;
 
        return cpu_buffer;
 
+ fail_free_reader:
+       free_buffer_page(cpu_buffer->reader_page);
+
  fail_free_buffer:
        kfree(cpu_buffer);
        return NULL;
@@ -291,11 +419,14 @@ rb_allocate_cpu_buffer(struct ring_buffer *buffer, int cpu)
 static void rb_free_cpu_buffer(struct ring_buffer_per_cpu *cpu_buffer)
 {
        struct list_head *head = &cpu_buffer->pages;
-       struct buffer_page *page, *tmp;
+       struct buffer_page *bpage, *tmp;
+
+       list_del_init(&cpu_buffer->reader_page->list);
+       free_buffer_page(cpu_buffer->reader_page);
 
-       list_for_each_entry_safe(page, tmp, head, list) {
-               list_del_init(&page->list);
-               free_buffer_page(page);
+       list_for_each_entry_safe(bpage, tmp, head, list) {
+               list_del_init(&bpage->list);
+               free_buffer_page(bpage);
        }
        kfree(cpu_buffer);
 }
@@ -308,7 +439,7 @@ extern int ring_buffer_page_too_big(void);
 
 /**
  * ring_buffer_alloc - allocate a new ring_buffer
- * @size: the size in bytes that is needed.
+ * @size: the size in bytes per cpu that is needed.
  * @flags: attributes to set for the ring buffer.
  *
  * Currently the only flag that is available is the RB_FL_OVERWRITE
@@ -333,6 +464,9 @@ struct ring_buffer *ring_buffer_alloc(unsigned long size, unsigned flags)
        if (!buffer)
                return NULL;
 
+       if (!alloc_cpumask_var(&buffer->cpumask, GFP_KERNEL))
+               goto fail_free_buffer;
+
        buffer->pages = DIV_ROUND_UP(size, BUF_PAGE_SIZE);
        buffer->flags = flags;
 
@@ -340,14 +474,14 @@ struct ring_buffer *ring_buffer_alloc(unsigned long size, unsigned flags)
        if (buffer->pages == 1)
                buffer->pages++;
 
-       buffer->cpumask = cpu_possible_map;
+       cpumask_copy(buffer->cpumask, cpu_possible_mask);
        buffer->cpus = nr_cpu_ids;
 
        bsize = sizeof(void *) * nr_cpu_ids;
        buffer->buffers = kzalloc(ALIGN(bsize, cache_line_size()),
                                  GFP_KERNEL);
        if (!buffer->buffers)
-               goto fail_free_buffer;
+               goto fail_free_cpumask;
 
        for_each_buffer_cpu(buffer, cpu) {
                buffer->buffers[cpu] =
@@ -367,10 +501,14 @@ struct ring_buffer *ring_buffer_alloc(unsigned long size, unsigned flags)
        }
        kfree(buffer->buffers);
 
+ fail_free_cpumask:
+       free_cpumask_var(buffer->cpumask);
+
  fail_free_buffer:
        kfree(buffer);
        return NULL;
 }
+EXPORT_SYMBOL_GPL(ring_buffer_alloc);
 
 /**
  * ring_buffer_free - free a ring buffer.
@@ -384,15 +522,18 @@ ring_buffer_free(struct ring_buffer *buffer)
        for_each_buffer_cpu(buffer, cpu)
                rb_free_cpu_buffer(buffer->buffers[cpu]);
 
+       free_cpumask_var(buffer->cpumask);
+
        kfree(buffer);
 }
+EXPORT_SYMBOL_GPL(ring_buffer_free);
 
 static void rb_reset_cpu(struct ring_buffer_per_cpu *cpu_buffer);
 
 static void
 rb_remove_pages(struct ring_buffer_per_cpu *cpu_buffer, unsigned nr_pages)
 {
-       struct buffer_page *page;
+       struct buffer_page *bpage;
        struct list_head *p;
        unsigned i;
 
@@ -400,13 +541,15 @@ rb_remove_pages(struct ring_buffer_per_cpu *cpu_buffer, unsigned nr_pages)
        synchronize_sched();
 
        for (i = 0; i < nr_pages; i++) {
-               BUG_ON(list_empty(&cpu_buffer->pages));
+               if (RB_WARN_ON(cpu_buffer, list_empty(&cpu_buffer->pages)))
+                       return;
                p = cpu_buffer->pages.next;
-               page = list_entry(p, struct buffer_page, list);
-               list_del_init(&page->list);
-               free_buffer_page(page);
+               bpage = list_entry(p, struct buffer_page, list);
+               list_del_init(&bpage->list);
+               free_buffer_page(bpage);
        }
-       BUG_ON(list_empty(&cpu_buffer->pages));
+       if (RB_WARN_ON(cpu_buffer, list_empty(&cpu_buffer->pages)))
+               return;
 
        rb_reset_cpu(cpu_buffer);
 
@@ -420,7 +563,7 @@ static void
 rb_insert_pages(struct ring_buffer_per_cpu *cpu_buffer,
                struct list_head *pages, unsigned nr_pages)
 {
-       struct buffer_page *page;
+       struct buffer_page *bpage;
        struct list_head *p;
        unsigned i;
 
@@ -428,11 +571,12 @@ rb_insert_pages(struct ring_buffer_per_cpu *cpu_buffer,
        synchronize_sched();
 
        for (i = 0; i < nr_pages; i++) {
-               BUG_ON(list_empty(pages));
+               if (RB_WARN_ON(cpu_buffer, list_empty(pages)))
+                       return;
                p = pages->next;
-               page = list_entry(p, struct buffer_page, list);
-               list_del_init(&page->list);
-               list_add_tail(&page->list, &cpu_buffer->pages);
+               bpage = list_entry(p, struct buffer_page, list);
+               list_del_init(&bpage->list);
+               list_add_tail(&bpage->list, &cpu_buffer->pages);
        }
        rb_reset_cpu(cpu_buffer);
 
@@ -459,12 +603,18 @@ int ring_buffer_resize(struct ring_buffer *buffer, unsigned long size)
 {
        struct ring_buffer_per_cpu *cpu_buffer;
        unsigned nr_pages, rm_pages, new_pages;
-       struct buffer_page *page, *tmp;
+       struct buffer_page *bpage, *tmp;
        unsigned long buffer_size;
        unsigned long addr;
        LIST_HEAD(pages);
        int i, cpu;
 
+       /*
+        * Always succeed at resizing a non-existent buffer:
+        */
+       if (!buffer)
+               return size;
+
        size = DIV_ROUND_UP(size, BUF_PAGE_SIZE);
        size *= BUF_PAGE_SIZE;
        buffer_size = buffer->pages * BUF_PAGE_SIZE;
@@ -483,7 +633,10 @@ int ring_buffer_resize(struct ring_buffer *buffer, unsigned long size)
        if (size < buffer_size) {
 
                /* easy case, just free pages */
-               BUG_ON(nr_pages >= buffer->pages);
+               if (RB_WARN_ON(buffer, nr_pages >= buffer->pages)) {
+                       mutex_unlock(&buffer->mutex);
+                       return -1;
+               }
 
                rm_pages = buffer->pages - nr_pages;
 
@@ -502,16 +655,26 @@ int ring_buffer_resize(struct ring_buffer *buffer, unsigned long size)
         * add these pages to the cpu_buffers. Otherwise we just free
         * them all and return -ENOMEM;
         */
-       BUG_ON(nr_pages <= buffer->pages);
+       if (RB_WARN_ON(buffer, nr_pages <= buffer->pages)) {
+               mutex_unlock(&buffer->mutex);
+               return -1;
+       }
+
        new_pages = nr_pages - buffer->pages;
 
        for_each_buffer_cpu(buffer, cpu) {
                for (i = 0; i < new_pages; i++) {
+                       bpage = kzalloc_node(ALIGN(sizeof(*bpage),
+                                                 cache_line_size()),
+                                           GFP_KERNEL, cpu_to_node(cpu));
+                       if (!bpage)
+                               goto free_pages;
+                       list_add(&bpage->list, &pages);
                        addr = __get_free_page(GFP_KERNEL);
                        if (!addr)
                                goto free_pages;
-                       page = (struct buffer_page *)virt_to_page(addr);
-                       list_add(&page->list, &pages);
+                       bpage->page = (void *)addr;
+                       rb_init_page(bpage->page);
                }
        }
 
@@ -520,7 +683,10 @@ int ring_buffer_resize(struct ring_buffer *buffer, unsigned long size)
                rb_insert_pages(cpu_buffer, &pages, new_pages);
        }
 
-       BUG_ON(!list_empty(&pages));
+       if (RB_WARN_ON(buffer, !list_empty(&pages))) {
+               mutex_unlock(&buffer->mutex);
+               return -1;
+       }
 
  out:
        buffer->pages = nr_pages;
@@ -529,43 +695,76 @@ int ring_buffer_resize(struct ring_buffer *buffer, unsigned long size)
        return size;
 
  free_pages:
-       list_for_each_entry_safe(page, tmp, &pages, list) {
-               list_del_init(&page->list);
-               free_buffer_page(page);
+       list_for_each_entry_safe(bpage, tmp, &pages, list) {
+               list_del_init(&bpage->list);
+               free_buffer_page(bpage);
        }
+       mutex_unlock(&buffer->mutex);
        return -ENOMEM;
 }
+EXPORT_SYMBOL_GPL(ring_buffer_resize);
 
-static inline int rb_per_cpu_empty(struct ring_buffer_per_cpu *cpu_buffer)
+static inline int rb_null_event(struct ring_buffer_event *event)
 {
-       return cpu_buffer->head_page == cpu_buffer->tail_page &&
-               cpu_buffer->head == cpu_buffer->tail;
+       return event->type == RINGBUF_TYPE_PADDING;
 }
 
-static inline int rb_null_event(struct ring_buffer_event *event)
+static inline void *
+__rb_data_page_index(struct buffer_data_page *bpage, unsigned index)
 {
-       return event->type == RINGBUF_TYPE_PADDING;
+       return bpage->data + index;
 }
 
-static inline void *rb_page_index(struct buffer_page *page, unsigned index)
+static inline void *__rb_page_index(struct buffer_page *bpage, unsigned index)
 {
-       void *addr = page_address(&page->page);
+       return bpage->page->data + index;
+}
 
-       return addr + index;
+static inline struct ring_buffer_event *
+rb_reader_event(struct ring_buffer_per_cpu *cpu_buffer)
+{
+       return __rb_page_index(cpu_buffer->reader_page,
+                              cpu_buffer->reader_page->read);
 }
 
 static inline struct ring_buffer_event *
 rb_head_event(struct ring_buffer_per_cpu *cpu_buffer)
 {
-       return rb_page_index(cpu_buffer->head_page,
-                            cpu_buffer->head);
+       return __rb_page_index(cpu_buffer->head_page,
+                              cpu_buffer->head_page->read);
 }
 
 static inline struct ring_buffer_event *
 rb_iter_head_event(struct ring_buffer_iter *iter)
 {
-       return rb_page_index(iter->head_page,
-                            iter->head);
+       return __rb_page_index(iter->head_page, iter->head);
+}
+
+static inline unsigned rb_page_write(struct buffer_page *bpage)
+{
+       return local_read(&bpage->write);
+}
+
+static inline unsigned rb_page_commit(struct buffer_page *bpage)
+{
+       return local_read(&bpage->page->commit);
+}
+
+/* Size is determined by what has been commited */
+static inline unsigned rb_page_size(struct buffer_page *bpage)
+{
+       return rb_page_commit(bpage);
+}
+
+static inline unsigned
+rb_commit_index(struct ring_buffer_per_cpu *cpu_buffer)
+{
+       return rb_page_commit(cpu_buffer->commit_page);
+}
+
+static inline unsigned rb_head_size(struct ring_buffer_per_cpu *cpu_buffer)
+{
+       return rb_page_commit(cpu_buffer->head_page);
 }
 
 /*
@@ -582,8 +781,9 @@ static void rb_update_overflow(struct ring_buffer_per_cpu *cpu_buffer)
        for (head = 0; head < rb_head_size(cpu_buffer);
             head += rb_event_length(event)) {
 
-               event = rb_page_index(cpu_buffer->head_page, head);
-               BUG_ON(rb_null_event(event));
+               event = __rb_page_index(cpu_buffer->head_page, head);
+               if (RB_WARN_ON(cpu_buffer, rb_null_event(event)))
+                       return;
                /* Only count data entries */
                if (event->type != RINGBUF_TYPE_DATA)
                        continue;
@@ -593,33 +793,125 @@ static void rb_update_overflow(struct ring_buffer_per_cpu *cpu_buffer)
 }
 
 static inline void rb_inc_page(struct ring_buffer_per_cpu *cpu_buffer,
-                              struct buffer_page **page)
+                              struct buffer_page **bpage)
 {
-       struct list_head *p = (*page)->list.next;
+       struct list_head *p = (*bpage)->list.next;
 
        if (p == &cpu_buffer->pages)
                p = p->next;
 
-       *page = list_entry(p, struct buffer_page, list);
+       *bpage = list_entry(p, struct buffer_page, list);
+}
+
+static inline unsigned
+rb_event_index(struct ring_buffer_event *event)
+{
+       unsigned long addr = (unsigned long)event;
+
+       return (addr & ~PAGE_MASK) - (PAGE_SIZE - BUF_PAGE_SIZE);
+}
+
+static inline int
+rb_is_commit(struct ring_buffer_per_cpu *cpu_buffer,
+            struct ring_buffer_event *event)
+{
+       unsigned long addr = (unsigned long)event;
+       unsigned long index;
+
+       index = rb_event_index(event);
+       addr &= PAGE_MASK;
+
+       return cpu_buffer->commit_page->page == (void *)addr &&
+               rb_commit_index(cpu_buffer) == index;
 }
 
 static inline void
-rb_add_stamp(struct ring_buffer_per_cpu *cpu_buffer, u64 *ts)
+rb_set_commit_event(struct ring_buffer_per_cpu *cpu_buffer,
+                   struct ring_buffer_event *event)
+{
+       unsigned long addr = (unsigned long)event;
+       unsigned long index;
+
+       index = rb_event_index(event);
+       addr &= PAGE_MASK;
+
+       while (cpu_buffer->commit_page->page != (void *)addr) {
+               if (RB_WARN_ON(cpu_buffer,
+                         cpu_buffer->commit_page == cpu_buffer->tail_page))
+                       return;
+               cpu_buffer->commit_page->page->commit =
+                       cpu_buffer->commit_page->write;
+               rb_inc_page(cpu_buffer, &cpu_buffer->commit_page);
+               cpu_buffer->write_stamp =
+                       cpu_buffer->commit_page->page->time_stamp;
+       }
+
+       /* Now set the commit to the event's index */
+       local_set(&cpu_buffer->commit_page->page->commit, index);
+}
+
+static inline void
+rb_set_commit_to_write(struct ring_buffer_per_cpu *cpu_buffer)
 {
-       cpu_buffer->tail_page->time_stamp = *ts;
-       cpu_buffer->write_stamp = *ts;
+       /*
+        * We only race with interrupts and NMIs on this CPU.
+        * If we own the commit event, then we can commit
+        * all others that interrupted us, since the interruptions
+        * are in stack format (they finish before they come
+        * back to us). This allows us to do a simple loop to
+        * assign the commit to the tail.
+        */
+ again:
+       while (cpu_buffer->commit_page != cpu_buffer->tail_page) {
+               cpu_buffer->commit_page->page->commit =
+                       cpu_buffer->commit_page->write;
+               rb_inc_page(cpu_buffer, &cpu_buffer->commit_page);
+               cpu_buffer->write_stamp =
+                       cpu_buffer->commit_page->page->time_stamp;
+               /* add barrier to keep gcc from optimizing too much */
+               barrier();
+       }
+       while (rb_commit_index(cpu_buffer) !=
+              rb_page_write(cpu_buffer->commit_page)) {
+               cpu_buffer->commit_page->page->commit =
+                       cpu_buffer->commit_page->write;
+               barrier();
+       }
+
+       /* again, keep gcc from optimizing */
+       barrier();
+
+       /*
+        * If an interrupt came in just after the first while loop
+        * and pushed the tail page forward, we will be left with
+        * a dangling commit that will never go forward.
+        */
+       if (unlikely(cpu_buffer->commit_page != cpu_buffer->tail_page))
+               goto again;
 }
 
-static void rb_reset_read_page(struct ring_buffer_per_cpu *cpu_buffer)
+static void rb_reset_reader_page(struct ring_buffer_per_cpu *cpu_buffer)
 {
-       cpu_buffer->read_stamp = cpu_buffer->head_page->time_stamp;
-       cpu_buffer->head = 0;
+       cpu_buffer->read_stamp = cpu_buffer->reader_page->page->time_stamp;
+       cpu_buffer->reader_page->read = 0;
 }
 
-static void
-rb_reset_iter_read_page(struct ring_buffer_iter *iter)
+static inline void rb_inc_iter(struct ring_buffer_iter *iter)
 {
-       iter->read_stamp = iter->head_page->time_stamp;
+       struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
+
+       /*
+        * The iterator could be on the reader page (it starts there).
+        * But the head could have moved, since the reader was
+        * found. Check for this case and assign the iterator
+        * to the head page instead of next.
+        */
+       if (iter->head_page == cpu_buffer->reader_page)
+               iter->head_page = cpu_buffer->head_page;
+       else
+               rb_inc_page(cpu_buffer, &iter->head_page);
+
+       iter->read_stamp = iter->head_page->page->time_stamp;
        iter->head = 0;
 }
 
@@ -693,53 +985,129 @@ static struct ring_buffer_event *
 __rb_reserve_next(struct ring_buffer_per_cpu *cpu_buffer,
                  unsigned type, unsigned long length, u64 *ts)
 {
-       struct buffer_page *head_page, *tail_page;
-       unsigned long tail;
+       struct buffer_page *tail_page, *head_page, *reader_page, *commit_page;
+       unsigned long tail, write;
        struct ring_buffer *buffer = cpu_buffer->buffer;
        struct ring_buffer_event *event;
+       unsigned long flags;
 
+       commit_page = cpu_buffer->commit_page;
+       /* we just need to protect against interrupts */
+       barrier();
        tail_page = cpu_buffer->tail_page;
-       head_page = cpu_buffer->head_page;
-       tail = cpu_buffer->tail;
+       write = local_add_return(length, &tail_page->write);
+       tail = write - length;
 
-       if (tail + length > BUF_PAGE_SIZE) {
+       /* See if we shot pass the end of this buffer page */
+       if (write > BUF_PAGE_SIZE) {
                struct buffer_page *next_page = tail_page;
 
+               local_irq_save(flags);
+               __raw_spin_lock(&cpu_buffer->lock);
+
                rb_inc_page(cpu_buffer, &next_page);
 
+               head_page = cpu_buffer->head_page;
+               reader_page = cpu_buffer->reader_page;
+
+               /* we grabbed the lock before incrementing */
+               if (RB_WARN_ON(cpu_buffer, next_page == reader_page))
+                       goto out_unlock;
+
+               /*
+                * If for some reason, we had an interrupt storm that made
+                * it all the way around the buffer, bail, and warn
+                * about it.
+                */
+               if (unlikely(next_page == commit_page)) {
+                       WARN_ON_ONCE(1);
+                       goto out_unlock;
+               }
+
                if (next_page == head_page) {
                        if (!(buffer->flags & RB_FL_OVERWRITE))
-                               return NULL;
+                               goto out_unlock;
+
+                       /* tail_page has not moved yet? */
+                       if (tail_page == cpu_buffer->tail_page) {
+                               /* count overflows */
+                               rb_update_overflow(cpu_buffer);
 
-                       /* count overflows */
-                       rb_update_overflow(cpu_buffer);
+                               rb_inc_page(cpu_buffer, &head_page);
+                               cpu_buffer->head_page = head_page;
+                               cpu_buffer->head_page->read = 0;
+                       }
+               }
 
-                       rb_inc_page(cpu_buffer, &head_page);
-                       cpu_buffer->head_page = head_page;
-                       rb_reset_read_page(cpu_buffer);
+               /*
+                * If the tail page is still the same as what we think
+                * it is, then it is up to us to update the tail
+                * pointer.
+                */
+               if (tail_page == cpu_buffer->tail_page) {
+                       local_set(&next_page->write, 0);
+                       local_set(&next_page->page->commit, 0);
+                       cpu_buffer->tail_page = next_page;
+
+                       /* reread the time stamp */
+                       *ts = ring_buffer_time_stamp(cpu_buffer->cpu);
+                       cpu_buffer->tail_page->page->time_stamp = *ts;
                }
 
-               if (tail != BUF_PAGE_SIZE) {
-                       event = rb_page_index(tail_page, tail);
-                       /* page padding */
+               /*
+                * The actual tail page has moved forward.
+                */
+               if (tail < BUF_PAGE_SIZE) {
+                       /* Mark the rest of the page with padding */
+                       event = __rb_page_index(tail_page, tail);
                        event->type = RINGBUF_TYPE_PADDING;
                }
 
-               tail_page->size = tail;
-               tail_page = next_page;
-               tail_page->size = 0;
-               tail = 0;
-               cpu_buffer->tail_page = tail_page;
-               cpu_buffer->tail = tail;
-               rb_add_stamp(cpu_buffer, ts);
+               if (tail <= BUF_PAGE_SIZE)
+                       /* Set the write back to the previous setting */
+                       local_set(&tail_page->write, tail);
+
+               /*
+                * If this was a commit entry that failed,
+                * increment that too
+                */
+               if (tail_page == cpu_buffer->commit_page &&
+                   tail == rb_commit_index(cpu_buffer)) {
+                       rb_set_commit_to_write(cpu_buffer);
+               }
+
+               __raw_spin_unlock(&cpu_buffer->lock);
+               local_irq_restore(flags);
+
+               /* fail and let the caller try again */
+               return ERR_PTR(-EAGAIN);
        }
 
-       BUG_ON(tail + length > BUF_PAGE_SIZE);
+       /* We reserved something on the buffer */
+
+       if (RB_WARN_ON(cpu_buffer, write > BUF_PAGE_SIZE))
+               return NULL;
 
-       event = rb_page_index(tail_page, tail);
+       event = __rb_page_index(tail_page, tail);
        rb_update_event(event, type, length);
 
+       /*
+        * If this is a commit and the tail is zero, then update
+        * this page's time stamp.
+        */
+       if (!tail && rb_is_commit(cpu_buffer, event))
+               cpu_buffer->commit_page->page->time_stamp = *ts;
+
        return event;
+
+ out_unlock:
+       /* reset write */
+       if (tail <= BUF_PAGE_SIZE)
+               local_set(&tail_page->write, tail);
+
+       __raw_spin_unlock(&cpu_buffer->lock);
+       local_irq_restore(flags);
+       return NULL;
 }
 
 static int
@@ -748,11 +1116,14 @@ rb_add_time_stamp(struct ring_buffer_per_cpu *cpu_buffer,
 {
        struct ring_buffer_event *event;
        static int once;
+       int ret;
 
        if (unlikely(*delta > (1ULL << 59) && !once++)) {
                printk(KERN_WARNING "Delta way too big! %llu"
                       " ts=%llu write stamp = %llu\n",
-                      *delta, *ts, cpu_buffer->write_stamp);
+                      (unsigned long long)*delta,
+                      (unsigned long long)*ts,
+                      (unsigned long long)cpu_buffer->write_stamp);
                WARN_ON(1);
        }
 
@@ -765,21 +1136,38 @@ rb_add_time_stamp(struct ring_buffer_per_cpu *cpu_buffer,
                                  RB_LEN_TIME_EXTEND,
                                  ts);
        if (!event)
-               return -1;
+               return -EBUSY;
 
-       /* check to see if we went to the next page */
-       if (cpu_buffer->tail) {
-               /* Still on same page, update timestamp */
-               event->time_delta = *delta & TS_MASK;
-               event->array[0] = *delta >> TS_SHIFT;
-               /* commit the time event */
-               cpu_buffer->tail +=
-                       rb_event_length(event);
+       if (PTR_ERR(event) == -EAGAIN)
+               return -EAGAIN;
+
+       /* Only a commited time event can update the write stamp */
+       if (rb_is_commit(cpu_buffer, event)) {
+               /*
+                * If this is the first on the page, then we need to
+                * update the page itself, and just put in a zero.
+                */
+               if (rb_event_index(event)) {
+                       event->time_delta = *delta & TS_MASK;
+                       event->array[0] = *delta >> TS_SHIFT;
+               } else {
+                       cpu_buffer->commit_page->page->time_stamp = *ts;
+                       event->time_delta = 0;
+                       event->array[0] = 0;
+               }
                cpu_buffer->write_stamp = *ts;
-               *delta = 0;
+               /* let the caller know this was the commit */
+               ret = 1;
+       } else {
+               /* Darn, this is just wasted space */
+               event->time_delta = 0;
+               event->array[0] = 0;
+               ret = 0;
        }
 
-       return 0;
+       *delta = 0;
+
+       return ret;
 }
 
 static struct ring_buffer_event *
@@ -788,30 +1176,82 @@ rb_reserve_next_event(struct ring_buffer_per_cpu *cpu_buffer,
 {
        struct ring_buffer_event *event;
        u64 ts, delta;
+       int commit = 0;
+       int nr_loops = 0;
+
+ again:
+       /*
+        * We allow for interrupts to reenter here and do a trace.
+        * If one does, it will cause this original code to loop
+        * back here. Even with heavy interrupts happening, this
+        * should only happen a few times in a row. If this happens
+        * 1000 times in a row, there must be either an interrupt
+        * storm or we have something buggy.
+        * Bail!
+        */
+       if (RB_WARN_ON(cpu_buffer, ++nr_loops > 1000))
+               return NULL;
 
        ts = ring_buffer_time_stamp(cpu_buffer->cpu);
 
-       if (cpu_buffer->tail) {
+       /*
+        * Only the first commit can update the timestamp.
+        * Yes there is a race here. If an interrupt comes in
+        * just after the conditional and it traces too, then it
+        * will also check the deltas. More than one timestamp may
+        * also be made. But only the entry that did the actual
+        * commit will be something other than zero.
+        */
+       if (cpu_buffer->tail_page == cpu_buffer->commit_page &&
+           rb_page_write(cpu_buffer->tail_page) ==
+           rb_commit_index(cpu_buffer)) {
+
                delta = ts - cpu_buffer->write_stamp;
 
+               /* make sure this delta is calculated here */
+               barrier();
+
+               /* Did the write stamp get updated already? */
+               if (unlikely(ts < cpu_buffer->write_stamp))
+                       delta = 0;
+
                if (test_time_stamp(delta)) {
-                       int ret;
 
-                       ret = rb_add_time_stamp(cpu_buffer, &ts, &delta);
-                       if (ret < 0)
+                       commit = rb_add_time_stamp(cpu_buffer, &ts, &delta);
+
+                       if (commit == -EBUSY)
                                return NULL;
+
+                       if (commit == -EAGAIN)
+                               goto again;
+
+                       RB_WARN_ON(cpu_buffer, commit < 0);
                }
-       } else {
-               rb_add_stamp(cpu_buffer, &ts);
+       } else
+               /* Non commits have zero deltas */
                delta = 0;
-       }
 
        event = __rb_reserve_next(cpu_buffer, type, length, &ts);
-       if (!event)
+       if (PTR_ERR(event) == -EAGAIN)
+               goto again;
+
+       if (!event) {
+               if (unlikely(commit))
+                       /*
+                        * Ouch! We needed a timestamp and it was commited. But
+                        * we didn't get our event reserved.
+                        */
+                       rb_set_commit_to_write(cpu_buffer);
                return NULL;
+       }
 
-       /* If the reserve went to the next page, our delta is zero */
-       if (!cpu_buffer->tail)
+       /*
+        * If the timestamp was commited, make the commit our entry
+        * now so that we will update it when needed.
+        */
+       if (commit)
+               rb_set_commit_event(cpu_buffer, event);
+       else if (!rb_is_commit(cpu_buffer, event))
                delta = 0;
 
        event->time_delta = delta;
@@ -819,6 +1259,8 @@ rb_reserve_next_event(struct ring_buffer_per_cpu *cpu_buffer,
        return event;
 }
 
+static DEFINE_PER_CPU(int, rb_need_resched);
+
 /**
  * ring_buffer_lock_reserve - reserve a part of the buffer
  * @buffer: the ring buffer to reserve from
@@ -842,47 +1284,63 @@ ring_buffer_lock_reserve(struct ring_buffer *buffer,
 {
        struct ring_buffer_per_cpu *cpu_buffer;
        struct ring_buffer_event *event;
-       int cpu;
+       int cpu, resched;
+
+       if (ring_buffer_flags != RB_BUFFERS_ON)
+               return NULL;
 
        if (atomic_read(&buffer->record_disabled))
                return NULL;
 
-       local_irq_save(*flags);
+       /* If we are tracing schedule, we don't want to recurse */
+       resched = ftrace_preempt_disable();
+
        cpu = raw_smp_processor_id();
 
-       if (!cpu_isset(cpu, buffer->cpumask))
-               goto out_irq;
+       if (!cpumask_test_cpu(cpu, buffer->cpumask))
+               goto out;
 
        cpu_buffer = buffer->buffers[cpu];
-       spin_lock(&cpu_buffer->lock);
 
        if (atomic_read(&cpu_buffer->record_disabled))
-               goto no_record;
+               goto out;
 
        length = rb_calculate_event_length(length);
        if (length > BUF_PAGE_SIZE)
-               return NULL;
+               goto out;
 
        event = rb_reserve_next_event(cpu_buffer, RINGBUF_TYPE_DATA, length);
        if (!event)
-               goto no_record;
+               goto out;
+
+       /*
+        * Need to store resched state on this cpu.
+        * Only the first needs to.
+        */
+
+       if (preempt_count() == 1)
+               per_cpu(rb_need_resched, cpu) = resched;
 
        return event;
 
- no_record:
-       spin_unlock(&cpu_buffer->lock);
- out_irq:
-       local_irq_restore(*flags);
+ out:
+       ftrace_preempt_enable(resched);
        return NULL;
 }
+EXPORT_SYMBOL_GPL(ring_buffer_lock_reserve);
 
 static void rb_commit(struct ring_buffer_per_cpu *cpu_buffer,
                      struct ring_buffer_event *event)
 {
-       cpu_buffer->tail += rb_event_length(event);
-       cpu_buffer->tail_page->size = cpu_buffer->tail;
-       cpu_buffer->write_stamp += event->time_delta;
        cpu_buffer->entries++;
+
+       /* Only process further if we own the commit */
+       if (!rb_is_commit(cpu_buffer, event))
+               return;
+
+       cpu_buffer->write_stamp += event->time_delta;
+
+       rb_set_commit_to_write(cpu_buffer);
 }
 
 /**
@@ -904,15 +1362,19 @@ int ring_buffer_unlock_commit(struct ring_buffer *buffer,
 
        cpu_buffer = buffer->buffers[cpu];
 
-       assert_spin_locked(&cpu_buffer->lock);
-
        rb_commit(cpu_buffer, event);
 
-       spin_unlock(&cpu_buffer->lock);
-       local_irq_restore(flags);
+       /*
+        * Only the last preempt count needs to restore preemption.
+        */
+       if (preempt_count() == 1)
+               ftrace_preempt_enable(per_cpu(rb_need_resched, cpu));
+       else
+               preempt_enable_no_resched_notrace();
 
        return 0;
 }
+EXPORT_SYMBOL_GPL(ring_buffer_unlock_commit);
 
 /**
  * ring_buffer_write - write data to the buffer without reserving
@@ -933,22 +1395,25 @@ int ring_buffer_write(struct ring_buffer *buffer,
 {
        struct ring_buffer_per_cpu *cpu_buffer;
        struct ring_buffer_event *event;
-       unsigned long event_length, flags;
+       unsigned long event_length;
        void *body;
        int ret = -EBUSY;
-       int cpu;
+       int cpu, resched;
+
+       if (ring_buffer_flags != RB_BUFFERS_ON)
+               return -EBUSY;
 
        if (atomic_read(&buffer->record_disabled))
                return -EBUSY;
 
-       local_irq_save(flags);
+       resched = ftrace_preempt_disable();
+
        cpu = raw_smp_processor_id();
 
-       if (!cpu_isset(cpu, buffer->cpumask))
-               goto out_irq;
+       if (!cpumask_test_cpu(cpu, buffer->cpumask))
+               goto out;
 
        cpu_buffer = buffer->buffers[cpu];
-       spin_lock(&cpu_buffer->lock);
 
        if (atomic_read(&cpu_buffer->record_disabled))
                goto out;
@@ -967,53 +1432,22 @@ int ring_buffer_write(struct ring_buffer *buffer,
 
        ret = 0;
  out:
-       spin_unlock(&cpu_buffer->lock);
- out_irq:
-       local_irq_restore(flags);
+       ftrace_preempt_enable(resched);
 
        return ret;
 }
+EXPORT_SYMBOL_GPL(ring_buffer_write);
 
-/**
- * ring_buffer_lock - lock the ring buffer
- * @buffer: The ring buffer to lock
- * @flags: The place to store the interrupt flags
- *
- * This locks all the per CPU buffers.
- *
- * Must be unlocked by ring_buffer_unlock.
- */
-void ring_buffer_lock(struct ring_buffer *buffer, unsigned long *flags)
-{
-       struct ring_buffer_per_cpu *cpu_buffer;
-       int cpu;
-
-       local_irq_save(*flags);
-
-       for_each_buffer_cpu(buffer, cpu) {
-               cpu_buffer = buffer->buffers[cpu];
-               spin_lock(&cpu_buffer->lock);
-       }
-}
-
-/**
- * ring_buffer_unlock - unlock a locked buffer
- * @buffer: The locked buffer to unlock
- * @flags: The interrupt flags received by ring_buffer_lock
- */
-void ring_buffer_unlock(struct ring_buffer *buffer, unsigned long flags)
+static inline int rb_per_cpu_empty(struct ring_buffer_per_cpu *cpu_buffer)
 {
-       struct ring_buffer_per_cpu *cpu_buffer;
-       int cpu;
+       struct buffer_page *reader = cpu_buffer->reader_page;
+       struct buffer_page *head = cpu_buffer->head_page;
+       struct buffer_page *commit = cpu_buffer->commit_page;
 
-       for (cpu = buffer->cpus - 1; cpu >= 0; cpu--) {
-               if (!cpu_isset(cpu, buffer->cpumask))
-                       continue;
-               cpu_buffer = buffer->buffers[cpu];
-               spin_unlock(&cpu_buffer->lock);
-       }
-
-       local_irq_restore(flags);
+       return reader->read == rb_page_commit(reader) &&
+               (commit == reader ||
+                (commit == head &&
+                 head->read == rb_page_commit(commit)));
 }
 
 /**
@@ -1029,6 +1463,7 @@ void ring_buffer_record_disable(struct ring_buffer *buffer)
 {
        atomic_inc(&buffer->record_disabled);
 }
+EXPORT_SYMBOL_GPL(ring_buffer_record_disable);
 
 /**
  * ring_buffer_record_enable - enable writes to the buffer
@@ -1041,6 +1476,7 @@ void ring_buffer_record_enable(struct ring_buffer *buffer)
 {
        atomic_dec(&buffer->record_disabled);
 }
+EXPORT_SYMBOL_GPL(ring_buffer_record_enable);
 
 /**
  * ring_buffer_record_disable_cpu - stop all writes into the cpu_buffer
@@ -1056,12 +1492,13 @@ void ring_buffer_record_disable_cpu(struct ring_buffer *buffer, int cpu)
 {
        struct ring_buffer_per_cpu *cpu_buffer;
 
-       if (!cpu_isset(cpu, buffer->cpumask))
+       if (!cpumask_test_cpu(cpu, buffer->cpumask))
                return;
 
        cpu_buffer = buffer->buffers[cpu];
        atomic_inc(&cpu_buffer->record_disabled);
 }
+EXPORT_SYMBOL_GPL(ring_buffer_record_disable_cpu);
 
 /**
  * ring_buffer_record_enable_cpu - enable writes to the buffer
@@ -1075,12 +1512,13 @@ void ring_buffer_record_enable_cpu(struct ring_buffer *buffer, int cpu)
 {
        struct ring_buffer_per_cpu *cpu_buffer;
 
-       if (!cpu_isset(cpu, buffer->cpumask))
+       if (!cpumask_test_cpu(cpu, buffer->cpumask))
                return;
 
        cpu_buffer = buffer->buffers[cpu];
        atomic_dec(&cpu_buffer->record_disabled);
 }
+EXPORT_SYMBOL_GPL(ring_buffer_record_enable_cpu);
 
 /**
  * ring_buffer_entries_cpu - get the number of entries in a cpu buffer
@@ -1091,12 +1529,13 @@ unsigned long ring_buffer_entries_cpu(struct ring_buffer *buffer, int cpu)
 {
        struct ring_buffer_per_cpu *cpu_buffer;
 
-       if (!cpu_isset(cpu, buffer->cpumask))
+       if (!cpumask_test_cpu(cpu, buffer->cpumask))
                return 0;
 
        cpu_buffer = buffer->buffers[cpu];
        return cpu_buffer->entries;
 }
+EXPORT_SYMBOL_GPL(ring_buffer_entries_cpu);
 
 /**
  * ring_buffer_overrun_cpu - get the number of overruns in a cpu_buffer
@@ -1107,12 +1546,13 @@ unsigned long ring_buffer_overrun_cpu(struct ring_buffer *buffer, int cpu)
 {
        struct ring_buffer_per_cpu *cpu_buffer;
 
-       if (!cpu_isset(cpu, buffer->cpumask))
+       if (!cpumask_test_cpu(cpu, buffer->cpumask))
                return 0;
 
        cpu_buffer = buffer->buffers[cpu];
        return cpu_buffer->overrun;
 }
+EXPORT_SYMBOL_GPL(ring_buffer_overrun_cpu);
 
 /**
  * ring_buffer_entries - get the number of entries in a buffer
@@ -1135,6 +1575,7 @@ unsigned long ring_buffer_entries(struct ring_buffer *buffer)
 
        return entries;
 }
+EXPORT_SYMBOL_GPL(ring_buffer_entries);
 
 /**
  * ring_buffer_overrun_cpu - get the number of overruns in buffer
@@ -1157,6 +1598,25 @@ unsigned long ring_buffer_overruns(struct ring_buffer *buffer)
 
        return overruns;
 }
+EXPORT_SYMBOL_GPL(ring_buffer_overruns);
+
+static void rb_iter_reset(struct ring_buffer_iter *iter)
+{
+       struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
+
+       /* Iterator usage is expected to have record disabled */
+       if (list_empty(&cpu_buffer->reader_page->list)) {
+               iter->head_page = cpu_buffer->head_page;
+               iter->head = cpu_buffer->head_page->read;
+       } else {
+               iter->head_page = cpu_buffer->reader_page;
+               iter->head = cpu_buffer->reader_page->read;
+       }
+       if (iter->head)
+               iter->read_stamp = cpu_buffer->read_stamp;
+       else
+               iter->read_stamp = iter->head_page->page->time_stamp;
+}
 
 /**
  * ring_buffer_iter_reset - reset an iterator
@@ -1168,11 +1628,13 @@ unsigned long ring_buffer_overruns(struct ring_buffer *buffer)
 void ring_buffer_iter_reset(struct ring_buffer_iter *iter)
 {
        struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
+       unsigned long flags;
 
-       iter->head_page = cpu_buffer->head_page;
-       iter->head = cpu_buffer->head;
-       rb_reset_iter_read_page(iter);
+       spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
+       rb_iter_reset(iter);
+       spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
 }
+EXPORT_SYMBOL_GPL(ring_buffer_iter_reset);
 
 /**
  * ring_buffer_iter_empty - check if an iterator has no more to read
@@ -1184,9 +1646,10 @@ int ring_buffer_iter_empty(struct ring_buffer_iter *iter)
 
        cpu_buffer = iter->cpu_buffer;
 
-       return iter->head_page == cpu_buffer->tail_page &&
-               iter->head == cpu_buffer->tail;
+       return iter->head_page == cpu_buffer->commit_page &&
+               iter->head == rb_commit_index(cpu_buffer);
 }
+EXPORT_SYMBOL_GPL(ring_buffer_iter_empty);
 
 static void
 rb_update_read_stamp(struct ring_buffer_per_cpu *cpu_buffer,
@@ -1250,43 +1713,103 @@ rb_update_iter_read_stamp(struct ring_buffer_iter *iter,
        return;
 }
 
-static void rb_advance_head(struct ring_buffer_per_cpu *cpu_buffer)
+static struct buffer_page *
+rb_get_reader_page(struct ring_buffer_per_cpu *cpu_buffer)
 {
-       struct ring_buffer_event *event;
-       unsigned length;
+       struct buffer_page *reader = NULL;
+       unsigned long flags;
+       int nr_loops = 0;
+
+       local_irq_save(flags);
+       __raw_spin_lock(&cpu_buffer->lock);
 
+ again:
        /*
-        * Check if we are at the end of the buffer.
+        * This should normally only loop twice. But because the
+        * start of the reader inserts an empty page, it causes
+        * a case where we will loop three times. There should be no
+        * reason to loop four times (that I know of).
         */
-       if (cpu_buffer->head >= cpu_buffer->head_page->size) {
-               BUG_ON(cpu_buffer->head_page == cpu_buffer->tail_page);
-               rb_inc_page(cpu_buffer, &cpu_buffer->head_page);
-               rb_reset_read_page(cpu_buffer);
-               return;
+       if (RB_WARN_ON(cpu_buffer, ++nr_loops > 3)) {
+               reader = NULL;
+               goto out;
        }
 
-       event = rb_head_event(cpu_buffer);
+       reader = cpu_buffer->reader_page;
 
-       if (event->type == RINGBUF_TYPE_DATA)
-               cpu_buffer->entries--;
+       /* If there's more to read, return this page */
+       if (cpu_buffer->reader_page->read < rb_page_size(reader))
+               goto out;
 
-       length = rb_event_length(event);
+       /* Never should we have an index greater than the size */
+       if (RB_WARN_ON(cpu_buffer,
+                      cpu_buffer->reader_page->read > rb_page_size(reader)))
+               goto out;
+
+       /* check if we caught up to the tail */
+       reader = NULL;
+       if (cpu_buffer->commit_page == cpu_buffer->reader_page)
+               goto out;
 
        /*
-        * This should not be called to advance the header if we are
-        * at the tail of the buffer.
+        * Splice the empty reader page into the list around the head.
+        * Reset the reader page to size zero.
         */
-       BUG_ON((cpu_buffer->head_page == cpu_buffer->tail_page) &&
-              (cpu_buffer->head + length > cpu_buffer->tail));
 
-       rb_update_read_stamp(cpu_buffer, event);
+       reader = cpu_buffer->head_page;
+       cpu_buffer->reader_page->list.next = reader->list.next;
+       cpu_buffer->reader_page->list.prev = reader->list.prev;
+
+       local_set(&cpu_buffer->reader_page->write, 0);
+       local_set(&cpu_buffer->reader_page->page->commit, 0);
+
+       /* Make the reader page now replace the head */
+       reader->list.prev->next = &cpu_buffer->reader_page->list;
+       reader->list.next->prev = &cpu_buffer->reader_page->list;
+
+       /*
+        * If the tail is on the reader, then we must set the head
+        * to the inserted page, otherwise we set it one before.
+        */
+       cpu_buffer->head_page = cpu_buffer->reader_page;
+
+       if (cpu_buffer->commit_page != reader)
+               rb_inc_page(cpu_buffer, &cpu_buffer->head_page);
+
+       /* Finally update the reader page to the new head */
+       cpu_buffer->reader_page = reader;
+       rb_reset_reader_page(cpu_buffer);
 
-       cpu_buffer->head += length;
+       goto again;
 
-       /* check for end of page */
-       if ((cpu_buffer->head >= cpu_buffer->head_page->size) &&
-           (cpu_buffer->head_page != cpu_buffer->tail_page))
-               rb_advance_head(cpu_buffer);
+ out:
+       __raw_spin_unlock(&cpu_buffer->lock);
+       local_irq_restore(flags);
+
+       return reader;
+}
+
+static void rb_advance_reader(struct ring_buffer_per_cpu *cpu_buffer)
+{
+       struct ring_buffer_event *event;
+       struct buffer_page *reader;
+       unsigned length;
+
+       reader = rb_get_reader_page(cpu_buffer);
+
+       /* This function should not be called when buffer is empty */
+       if (RB_WARN_ON(cpu_buffer, !reader))
+               return;
+
+       event = rb_reader_event(cpu_buffer);
+
+       if (event->type == RINGBUF_TYPE_DATA)
+               cpu_buffer->entries--;
+
+       rb_update_read_stamp(cpu_buffer, event);
+
+       length = rb_event_length(event);
+       cpu_buffer->reader_page->read += length;
 }
 
 static void rb_advance_iter(struct ring_buffer_iter *iter)
@@ -1302,10 +1825,11 @@ static void rb_advance_iter(struct ring_buffer_iter *iter)
        /*
         * Check if we are at the end of the buffer.
         */
-       if (iter->head >= iter->head_page->size) {
-               BUG_ON(iter->head_page == cpu_buffer->tail_page);
-               rb_inc_page(cpu_buffer, &iter->head_page);
-               rb_reset_iter_read_page(iter);
+       if (iter->head >= rb_page_size(iter->head_page)) {
+               if (RB_WARN_ON(buffer,
+                              iter->head_page == cpu_buffer->commit_page))
+                       return;
+               rb_inc_iter(iter);
                return;
        }
 
@@ -1317,59 +1841,66 @@ static void rb_advance_iter(struct ring_buffer_iter *iter)
         * This should not be called to advance the header if we are
         * at the tail of the buffer.
         */
-       BUG_ON((iter->head_page == cpu_buffer->tail_page) &&
-              (iter->head + length > cpu_buffer->tail));
+       if (RB_WARN_ON(cpu_buffer,
+                      (iter->head_page == cpu_buffer->commit_page) &&
+                      (iter->head + length > rb_commit_index(cpu_buffer))))
+               return;
 
        rb_update_iter_read_stamp(iter, event);
 
        iter->head += length;
 
        /* check for end of page padding */
-       if ((iter->head >= iter->head_page->size) &&
-           (iter->head_page != cpu_buffer->tail_page))
+       if ((iter->head >= rb_page_size(iter->head_page)) &&
+           (iter->head_page != cpu_buffer->commit_page))
                rb_advance_iter(iter);
 }
 
-/**
- * ring_buffer_peek - peek at the next event to be read
- * @buffer: The ring buffer to read
- * @cpu: The cpu to peak at
- * @ts: The timestamp counter of this event.
- *
- * This will return the event that will be read next, but does
- * not consume the data.
- */
-struct ring_buffer_event *
-ring_buffer_peek(struct ring_buffer *buffer, int cpu, u64 *ts)
+static struct ring_buffer_event *
+rb_buffer_peek(struct ring_buffer *buffer, int cpu, u64 *ts)
 {
        struct ring_buffer_per_cpu *cpu_buffer;
        struct ring_buffer_event *event;
+       struct buffer_page *reader;
+       int nr_loops = 0;
 
-       if (!cpu_isset(cpu, buffer->cpumask))
+       if (!cpumask_test_cpu(cpu, buffer->cpumask))
                return NULL;
 
        cpu_buffer = buffer->buffers[cpu];
 
  again:
-       if (rb_per_cpu_empty(cpu_buffer))
+       /*
+        * We repeat when a timestamp is encountered. It is possible
+        * to get multiple timestamps from an interrupt entering just
+        * as one timestamp is about to be written. The max times
+        * that this can happen is the number of nested interrupts we
+        * can have.  Nesting 10 deep of interrupts is clearly
+        * an anomaly.
+        */
+       if (RB_WARN_ON(cpu_buffer, ++nr_loops > 10))
+               return NULL;
+
+       reader = rb_get_reader_page(cpu_buffer);
+       if (!reader)
                return NULL;
 
-       event = rb_head_event(cpu_buffer);
+       event = rb_reader_event(cpu_buffer);
 
        switch (event->type) {
        case RINGBUF_TYPE_PADDING:
-               rb_inc_page(cpu_buffer, &cpu_buffer->head_page);
-               rb_reset_read_page(cpu_buffer);
-               goto again;
+               RB_WARN_ON(cpu_buffer, 1);
+               rb_advance_reader(cpu_buffer);
+               return NULL;
 
        case RINGBUF_TYPE_TIME_EXTEND:
                /* Internal data, OK to advance */
-               rb_advance_head(cpu_buffer);
+               rb_advance_reader(cpu_buffer);
                goto again;
 
        case RINGBUF_TYPE_TIME_STAMP:
                /* FIXME: not implemented */
-               rb_advance_head(cpu_buffer);
+               rb_advance_reader(cpu_buffer);
                goto again;
 
        case RINGBUF_TYPE_DATA:
@@ -1385,21 +1916,15 @@ ring_buffer_peek(struct ring_buffer *buffer, int cpu, u64 *ts)
 
        return NULL;
 }
+EXPORT_SYMBOL_GPL(ring_buffer_peek);
 
-/**
- * ring_buffer_iter_peek - peek at the next event to be read
- * @iter: The ring buffer iterator
- * @ts: The timestamp counter of this event.
- *
- * This will return the event that will be read next, but does
- * not increment the iterator.
- */
-struct ring_buffer_event *
-ring_buffer_iter_peek(struct ring_buffer_iter *iter, u64 *ts)
+static struct ring_buffer_event *
+rb_iter_peek(struct ring_buffer_iter *iter, u64 *ts)
 {
        struct ring_buffer *buffer;
        struct ring_buffer_per_cpu *cpu_buffer;
        struct ring_buffer_event *event;
+       int nr_loops = 0;
 
        if (ring_buffer_iter_empty(iter))
                return NULL;
@@ -1408,6 +1933,17 @@ ring_buffer_iter_peek(struct ring_buffer_iter *iter, u64 *ts)
        buffer = cpu_buffer->buffer;
 
  again:
+       /*
+        * We repeat when a timestamp is encountered. It is possible
+        * to get multiple timestamps from an interrupt entering just
+        * as one timestamp is about to be written. The max times
+        * that this can happen is the number of nested interrupts we
+        * can have. Nesting 10 deep of interrupts is clearly
+        * an anomaly.
+        */
+       if (RB_WARN_ON(cpu_buffer, ++nr_loops > 10))
+               return NULL;
+
        if (rb_per_cpu_empty(cpu_buffer))
                return NULL;
 
@@ -1415,8 +1951,7 @@ ring_buffer_iter_peek(struct ring_buffer_iter *iter, u64 *ts)
 
        switch (event->type) {
        case RINGBUF_TYPE_PADDING:
-               rb_inc_page(cpu_buffer, &iter->head_page);
-               rb_reset_iter_read_page(iter);
+               rb_inc_iter(iter);
                goto again;
 
        case RINGBUF_TYPE_TIME_EXTEND:
@@ -1442,6 +1977,52 @@ ring_buffer_iter_peek(struct ring_buffer_iter *iter, u64 *ts)
 
        return NULL;
 }
+EXPORT_SYMBOL_GPL(ring_buffer_iter_peek);
+
+/**
+ * ring_buffer_peek - peek at the next event to be read
+ * @buffer: The ring buffer to read
+ * @cpu: The cpu to peak at
+ * @ts: The timestamp counter of this event.
+ *
+ * This will return the event that will be read next, but does
+ * not consume the data.
+ */
+struct ring_buffer_event *
+ring_buffer_peek(struct ring_buffer *buffer, int cpu, u64 *ts)
+{
+       struct ring_buffer_per_cpu *cpu_buffer = buffer->buffers[cpu];
+       struct ring_buffer_event *event;
+       unsigned long flags;
+
+       spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
+       event = rb_buffer_peek(buffer, cpu, ts);
+       spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
+
+       return event;
+}
+
+/**
+ * ring_buffer_iter_peek - peek at the next event to be read
+ * @iter: The ring buffer iterator
+ * @ts: The timestamp counter of this event.
+ *
+ * This will return the event that will be read next, but does
+ * not increment the iterator.
+ */
+struct ring_buffer_event *
+ring_buffer_iter_peek(struct ring_buffer_iter *iter, u64 *ts)
+{
+       struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
+       struct ring_buffer_event *event;
+       unsigned long flags;
+
+       spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
+       event = rb_iter_peek(iter, ts);
+       spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
+
+       return event;
+}
 
 /**
  * ring_buffer_consume - return an event and consume it
@@ -1454,21 +2035,27 @@ ring_buffer_iter_peek(struct ring_buffer_iter *iter, u64 *ts)
 struct ring_buffer_event *
 ring_buffer_consume(struct ring_buffer *buffer, int cpu, u64 *ts)
 {
-       struct ring_buffer_per_cpu *cpu_buffer;
+       struct ring_buffer_per_cpu *cpu_buffer = buffer->buffers[cpu];
        struct ring_buffer_event *event;
+       unsigned long flags;
 
-       if (!cpu_isset(cpu, buffer->cpumask))
+       if (!cpumask_test_cpu(cpu, buffer->cpumask))
                return NULL;
 
-       event = ring_buffer_peek(buffer, cpu, ts);
+       spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
+
+       event = rb_buffer_peek(buffer, cpu, ts);
        if (!event)
-               return NULL;
+               goto out;
 
-       cpu_buffer = buffer->buffers[cpu];
-       rb_advance_head(cpu_buffer);
+       rb_advance_reader(cpu_buffer);
+
+ out:
+       spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
 
        return event;
 }
+EXPORT_SYMBOL_GPL(ring_buffer_consume);
 
 /**
  * ring_buffer_read_start - start a non consuming read of the buffer
@@ -1487,8 +2074,9 @@ ring_buffer_read_start(struct ring_buffer *buffer, int cpu)
 {
        struct ring_buffer_per_cpu *cpu_buffer;
        struct ring_buffer_iter *iter;
+       unsigned long flags;
 
-       if (!cpu_isset(cpu, buffer->cpumask))
+       if (!cpumask_test_cpu(cpu, buffer->cpumask))
                return NULL;
 
        iter = kmalloc(sizeof(*iter), GFP_KERNEL);
@@ -1502,14 +2090,15 @@ ring_buffer_read_start(struct ring_buffer *buffer, int cpu)
        atomic_inc(&cpu_buffer->record_disabled);
        synchronize_sched();
 
-       spin_lock(&cpu_buffer->lock);
-       iter->head = cpu_buffer->head;
-       iter->head_page = cpu_buffer->head_page;
-       rb_reset_iter_read_page(iter);
-       spin_unlock(&cpu_buffer->lock);
+       spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
+       __raw_spin_lock(&cpu_buffer->lock);
+       rb_iter_reset(iter);
+       __raw_spin_unlock(&cpu_buffer->lock);
+       spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
 
        return iter;
 }
+EXPORT_SYMBOL_GPL(ring_buffer_read_start);
 
 /**
  * ring_buffer_finish - finish reading the iterator of the buffer
@@ -1526,6 +2115,7 @@ ring_buffer_read_finish(struct ring_buffer_iter *iter)
        atomic_dec(&cpu_buffer->record_disabled);
        kfree(iter);
 }
+EXPORT_SYMBOL_GPL(ring_buffer_read_finish);
 
 /**
  * ring_buffer_read - read the next item in the ring buffer by the iterator
@@ -1538,15 +2128,21 @@ struct ring_buffer_event *
 ring_buffer_read(struct ring_buffer_iter *iter, u64 *ts)
 {
        struct ring_buffer_event *event;
+       struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
+       unsigned long flags;
 
-       event = ring_buffer_iter_peek(iter, ts);
+       spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
+       event = rb_iter_peek(iter, ts);
        if (!event)
-               return NULL;
+               goto out;
 
        rb_advance_iter(iter);
+ out:
+       spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
 
        return event;
 }
+EXPORT_SYMBOL_GPL(ring_buffer_read);
 
 /**
  * ring_buffer_size - return the size of the ring buffer (in bytes)
@@ -1556,16 +2152,26 @@ unsigned long ring_buffer_size(struct ring_buffer *buffer)
 {
        return BUF_PAGE_SIZE * buffer->pages;
 }
+EXPORT_SYMBOL_GPL(ring_buffer_size);
 
 static void
 rb_reset_cpu(struct ring_buffer_per_cpu *cpu_buffer)
 {
        cpu_buffer->head_page
                = list_entry(cpu_buffer->pages.next, struct buffer_page, list);
-       cpu_buffer->tail_page
-               = list_entry(cpu_buffer->pages.next, struct buffer_page, list);
+       local_set(&cpu_buffer->head_page->write, 0);
+       local_set(&cpu_buffer->head_page->page->commit, 0);
+
+       cpu_buffer->head_page->read = 0;
+
+       cpu_buffer->tail_page = cpu_buffer->head_page;
+       cpu_buffer->commit_page = cpu_buffer->head_page;
+
+       INIT_LIST_HEAD(&cpu_buffer->reader_page->list);
+       local_set(&cpu_buffer->reader_page->write, 0);
+       local_set(&cpu_buffer->reader_page->page->commit, 0);
+       cpu_buffer->reader_page->read = 0;
 
-       cpu_buffer->head = cpu_buffer->tail = 0;
        cpu_buffer->overrun = 0;
        cpu_buffer->entries = 0;
 }
@@ -1580,17 +2186,20 @@ void ring_buffer_reset_cpu(struct ring_buffer *buffer, int cpu)
        struct ring_buffer_per_cpu *cpu_buffer = buffer->buffers[cpu];
        unsigned long flags;
 
-       if (!cpu_isset(cpu, buffer->cpumask))
+       if (!cpumask_test_cpu(cpu, buffer->cpumask))
                return;
 
-       local_irq_save(flags);
-       spin_lock(&cpu_buffer->lock);
+       spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
+
+       __raw_spin_lock(&cpu_buffer->lock);
 
        rb_reset_cpu(cpu_buffer);
 
-       spin_unlock(&cpu_buffer->lock);
-       local_irq_restore(flags);
+       __raw_spin_unlock(&cpu_buffer->lock);
+
+       spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
 }
+EXPORT_SYMBOL_GPL(ring_buffer_reset_cpu);
 
 /**
  * ring_buffer_reset - reset a ring buffer
@@ -1598,16 +2207,12 @@ void ring_buffer_reset_cpu(struct ring_buffer *buffer, int cpu)
  */
 void ring_buffer_reset(struct ring_buffer *buffer)
 {
-       unsigned long flags;
        int cpu;
 
-       ring_buffer_lock(buffer, &flags);
-
        for_each_buffer_cpu(buffer, cpu)
-               rb_reset_cpu(buffer->buffers[cpu]);
-
-       ring_buffer_unlock(buffer, flags);
+               ring_buffer_reset_cpu(buffer, cpu);
 }
+EXPORT_SYMBOL_GPL(ring_buffer_reset);
 
 /**
  * rind_buffer_empty - is the ring buffer empty?
@@ -1626,6 +2231,7 @@ int ring_buffer_empty(struct ring_buffer *buffer)
        }
        return 1;
 }
+EXPORT_SYMBOL_GPL(ring_buffer_empty);
 
 /**
  * ring_buffer_empty_cpu - is a cpu buffer of a ring buffer empty?
@@ -1636,12 +2242,13 @@ int ring_buffer_empty_cpu(struct ring_buffer *buffer, int cpu)
 {
        struct ring_buffer_per_cpu *cpu_buffer;
 
-       if (!cpu_isset(cpu, buffer->cpumask))
+       if (!cpumask_test_cpu(cpu, buffer->cpumask))
                return 1;
 
        cpu_buffer = buffer->buffers[cpu];
        return rb_per_cpu_empty(cpu_buffer);
 }
+EXPORT_SYMBOL_GPL(ring_buffer_empty_cpu);
 
 /**
  * ring_buffer_swap_cpu - swap a CPU buffer between two ring buffers
@@ -1659,13 +2266,12 @@ int ring_buffer_swap_cpu(struct ring_buffer *buffer_a,
        struct ring_buffer_per_cpu *cpu_buffer_a;
        struct ring_buffer_per_cpu *cpu_buffer_b;
 
-       if (!cpu_isset(cpu, buffer_a->cpumask) ||
-           !cpu_isset(cpu, buffer_b->cpumask))
+       if (!cpumask_test_cpu(cpu, buffer_a->cpumask) ||
+           !cpumask_test_cpu(cpu, buffer_b->cpumask))
                return -EINVAL;
 
        /* At least make sure the two buffers are somewhat the same */
-       if (buffer_a->size != buffer_b->size ||
-           buffer_a->pages != buffer_b->pages)
+       if (buffer_a->pages != buffer_b->pages)
                return -EINVAL;
 
        cpu_buffer_a = buffer_a->buffers[cpu];
@@ -1691,4 +2297,235 @@ int ring_buffer_swap_cpu(struct ring_buffer *buffer_a,
 
        return 0;
 }
+EXPORT_SYMBOL_GPL(ring_buffer_swap_cpu);
+
+static void rb_remove_entries(struct ring_buffer_per_cpu *cpu_buffer,
+                             struct buffer_data_page *bpage)
+{
+       struct ring_buffer_event *event;
+       unsigned long head;
+
+       __raw_spin_lock(&cpu_buffer->lock);
+       for (head = 0; head < local_read(&bpage->commit);
+            head += rb_event_length(event)) {
+
+               event = __rb_data_page_index(bpage, head);
+               if (RB_WARN_ON(cpu_buffer, rb_null_event(event)))
+                       return;
+               /* Only count data entries */
+               if (event->type != RINGBUF_TYPE_DATA)
+                       continue;
+               cpu_buffer->entries--;
+       }
+       __raw_spin_unlock(&cpu_buffer->lock);
+}
+
+/**
+ * ring_buffer_alloc_read_page - allocate a page to read from buffer
+ * @buffer: the buffer to allocate for.
+ *
+ * This function is used in conjunction with ring_buffer_read_page.
+ * When reading a full page from the ring buffer, these functions
+ * can be used to speed up the process. The calling function should
+ * allocate a few pages first with this function. Then when it
+ * needs to get pages from the ring buffer, it passes the result
+ * of this function into ring_buffer_read_page, which will swap
+ * the page that was allocated, with the read page of the buffer.
+ *
+ * Returns:
+ *  The page allocated, or NULL on error.
+ */
+void *ring_buffer_alloc_read_page(struct ring_buffer *buffer)
+{
+       unsigned long addr;
+       struct buffer_data_page *bpage;
+
+       addr = __get_free_page(GFP_KERNEL);
+       if (!addr)
+               return NULL;
+
+       bpage = (void *)addr;
+
+       return bpage;
+}
+
+/**
+ * ring_buffer_free_read_page - free an allocated read page
+ * @buffer: the buffer the page was allocate for
+ * @data: the page to free
+ *
+ * Free a page allocated from ring_buffer_alloc_read_page.
+ */
+void ring_buffer_free_read_page(struct ring_buffer *buffer, void *data)
+{
+       free_page((unsigned long)data);
+}
+
+/**
+ * ring_buffer_read_page - extract a page from the ring buffer
+ * @buffer: buffer to extract from
+ * @data_page: the page to use allocated from ring_buffer_alloc_read_page
+ * @cpu: the cpu of the buffer to extract
+ * @full: should the extraction only happen when the page is full.
+ *
+ * This function will pull out a page from the ring buffer and consume it.
+ * @data_page must be the address of the variable that was returned
+ * from ring_buffer_alloc_read_page. This is because the page might be used
+ * to swap with a page in the ring buffer.
+ *
+ * for example:
+ *     rpage = ring_buffer_alloc_page(buffer);
+ *     if (!rpage)
+ *             return error;
+ *     ret = ring_buffer_read_page(buffer, &rpage, cpu, 0);
+ *     if (ret)
+ *             process_page(rpage);
+ *
+ * When @full is set, the function will not return true unless
+ * the writer is off the reader page.
+ *
+ * Note: it is up to the calling functions to handle sleeps and wakeups.
+ *  The ring buffer can be used anywhere in the kernel and can not
+ *  blindly call wake_up. The layer that uses the ring buffer must be
+ *  responsible for that.
+ *
+ * Returns:
+ *  1 if data has been transferred
+ *  0 if no data has been transferred.
+ */
+int ring_buffer_read_page(struct ring_buffer *buffer,
+                           void **data_page, int cpu, int full)
+{
+       struct ring_buffer_per_cpu *cpu_buffer = buffer->buffers[cpu];
+       struct ring_buffer_event *event;
+       struct buffer_data_page *bpage;
+       unsigned long flags;
+       int ret = 0;
+
+       if (!data_page)
+               return 0;
+
+       bpage = *data_page;
+       if (!bpage)
+               return 0;
+
+       spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
+
+       /*
+        * rb_buffer_peek will get the next ring buffer if
+        * the current reader page is empty.
+        */
+       event = rb_buffer_peek(buffer, cpu, NULL);
+       if (!event)
+               goto out;
+
+       /* check for data */
+       if (!local_read(&cpu_buffer->reader_page->page->commit))
+               goto out;
+       /*
+        * If the writer is already off of the read page, then simply
+        * switch the read page with the given page. Otherwise
+        * we need to copy the data from the reader to the writer.
+        */
+       if (cpu_buffer->reader_page == cpu_buffer->commit_page) {
+               unsigned int read = cpu_buffer->reader_page->read;
+
+               if (full)
+                       goto out;
+               /* The writer is still on the reader page, we must copy */
+               bpage = cpu_buffer->reader_page->page;
+               memcpy(bpage->data,
+                      cpu_buffer->reader_page->page->data + read,
+                      local_read(&bpage->commit) - read);
+
+               /* consume what was read */
+               cpu_buffer->reader_page += read;
+
+       } else {
+               /* swap the pages */
+               rb_init_page(bpage);
+               bpage = cpu_buffer->reader_page->page;
+               cpu_buffer->reader_page->page = *data_page;
+               cpu_buffer->reader_page->read = 0;
+               *data_page = bpage;
+       }
+       ret = 1;
+
+       /* update the entry counter */
+       rb_remove_entries(cpu_buffer, bpage);
+ out:
+       spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
+
+       return ret;
+}
+
+static ssize_t
+rb_simple_read(struct file *filp, char __user *ubuf,
+              size_t cnt, loff_t *ppos)
+{
+       long *p = filp->private_data;
+       char buf[64];
+       int r;
+
+       if (test_bit(RB_BUFFERS_DISABLED_BIT, p))
+               r = sprintf(buf, "permanently disabled\n");
+       else
+               r = sprintf(buf, "%d\n", test_bit(RB_BUFFERS_ON_BIT, p));
+
+       return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
+}
+
+static ssize_t
+rb_simple_write(struct file *filp, const char __user *ubuf,
+               size_t cnt, loff_t *ppos)
+{
+       long *p = filp->private_data;
+       char buf[64];
+       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;
+
+       if (val)
+               set_bit(RB_BUFFERS_ON_BIT, p);
+       else
+               clear_bit(RB_BUFFERS_ON_BIT, p);
+
+       (*ppos)++;
+
+       return cnt;
+}
+
+static struct file_operations rb_simple_fops = {
+       .open           = tracing_open_generic,
+       .read           = rb_simple_read,
+       .write          = rb_simple_write,
+};
+
+
+static __init int rb_init_debugfs(void)
+{
+       struct dentry *d_tracer;
+       struct dentry *entry;
+
+       d_tracer = tracing_init_dentry();
+
+       entry = debugfs_create_file("tracing_on", 0644, d_tracer,
+                                   &ring_buffer_flags, &rb_simple_fops);
+       if (!entry)
+               pr_warning("Could not create debugfs 'tracing_on' entry\n");
+
+       return 0;
+}
 
+fs_initcall(rb_init_debugfs);