ring-buffer: add ring_buffer_discard_commit
[safe/jmp/linux-2.6] / kernel / trace / ring_buffer.c
1 /*
2  * Generic ring buffer
3  *
4  * Copyright (C) 2008 Steven Rostedt <srostedt@redhat.com>
5  */
6 #include <linux/ring_buffer.h>
7 #include <linux/trace_clock.h>
8 #include <linux/ftrace_irq.h>
9 #include <linux/spinlock.h>
10 #include <linux/debugfs.h>
11 #include <linux/uaccess.h>
12 #include <linux/hardirq.h>
13 #include <linux/module.h>
14 #include <linux/percpu.h>
15 #include <linux/mutex.h>
16 #include <linux/init.h>
17 #include <linux/hash.h>
18 #include <linux/list.h>
19 #include <linux/cpu.h>
20 #include <linux/fs.h>
21
22 #include "trace.h"
23
24 /*
25  * The ring buffer is made up of a list of pages. A separate list of pages is
26  * allocated for each CPU. A writer may only write to a buffer that is
27  * associated with the CPU it is currently executing on.  A reader may read
28  * from any per cpu buffer.
29  *
30  * The reader is special. For each per cpu buffer, the reader has its own
31  * reader page. When a reader has read the entire reader page, this reader
32  * page is swapped with another page in the ring buffer.
33  *
34  * Now, as long as the writer is off the reader page, the reader can do what
35  * ever it wants with that page. The writer will never write to that page
36  * again (as long as it is out of the ring buffer).
37  *
38  * Here's some silly ASCII art.
39  *
40  *   +------+
41  *   |reader|          RING BUFFER
42  *   |page  |
43  *   +------+        +---+   +---+   +---+
44  *                   |   |-->|   |-->|   |
45  *                   +---+   +---+   +---+
46  *                     ^               |
47  *                     |               |
48  *                     +---------------+
49  *
50  *
51  *   +------+
52  *   |reader|          RING BUFFER
53  *   |page  |------------------v
54  *   +------+        +---+   +---+   +---+
55  *                   |   |-->|   |-->|   |
56  *                   +---+   +---+   +---+
57  *                     ^               |
58  *                     |               |
59  *                     +---------------+
60  *
61  *
62  *   +------+
63  *   |reader|          RING BUFFER
64  *   |page  |------------------v
65  *   +------+        +---+   +---+   +---+
66  *      ^            |   |-->|   |-->|   |
67  *      |            +---+   +---+   +---+
68  *      |                              |
69  *      |                              |
70  *      +------------------------------+
71  *
72  *
73  *   +------+
74  *   |buffer|          RING BUFFER
75  *   |page  |------------------v
76  *   +------+        +---+   +---+   +---+
77  *      ^            |   |   |   |-->|   |
78  *      |   New      +---+   +---+   +---+
79  *      |  Reader------^               |
80  *      |   page                       |
81  *      +------------------------------+
82  *
83  *
84  * After we make this swap, the reader can hand this page off to the splice
85  * code and be done with it. It can even allocate a new page if it needs to
86  * and swap that into the ring buffer.
87  *
88  * We will be using cmpxchg soon to make all this lockless.
89  *
90  */
91
92 /*
93  * A fast way to enable or disable all ring buffers is to
94  * call tracing_on or tracing_off. Turning off the ring buffers
95  * prevents all ring buffers from being recorded to.
96  * Turning this switch on, makes it OK to write to the
97  * ring buffer, if the ring buffer is enabled itself.
98  *
99  * There's three layers that must be on in order to write
100  * to the ring buffer.
101  *
102  * 1) This global flag must be set.
103  * 2) The ring buffer must be enabled for recording.
104  * 3) The per cpu buffer must be enabled for recording.
105  *
106  * In case of an anomaly, this global flag has a bit set that
107  * will permantly disable all ring buffers.
108  */
109
110 /*
111  * Global flag to disable all recording to ring buffers
112  *  This has two bits: ON, DISABLED
113  *
114  *  ON   DISABLED
115  * ---- ----------
116  *   0      0        : ring buffers are off
117  *   1      0        : ring buffers are on
118  *   X      1        : ring buffers are permanently disabled
119  */
120
121 enum {
122         RB_BUFFERS_ON_BIT       = 0,
123         RB_BUFFERS_DISABLED_BIT = 1,
124 };
125
126 enum {
127         RB_BUFFERS_ON           = 1 << RB_BUFFERS_ON_BIT,
128         RB_BUFFERS_DISABLED     = 1 << RB_BUFFERS_DISABLED_BIT,
129 };
130
131 static unsigned long ring_buffer_flags __read_mostly = RB_BUFFERS_ON;
132
133 #define BUF_PAGE_HDR_SIZE offsetof(struct buffer_data_page, data)
134
135 /**
136  * tracing_on - enable all tracing buffers
137  *
138  * This function enables all tracing buffers that may have been
139  * disabled with tracing_off.
140  */
141 void tracing_on(void)
142 {
143         set_bit(RB_BUFFERS_ON_BIT, &ring_buffer_flags);
144 }
145 EXPORT_SYMBOL_GPL(tracing_on);
146
147 /**
148  * tracing_off - turn off all tracing buffers
149  *
150  * This function stops all tracing buffers from recording data.
151  * It does not disable any overhead the tracers themselves may
152  * be causing. This function simply causes all recording to
153  * the ring buffers to fail.
154  */
155 void tracing_off(void)
156 {
157         clear_bit(RB_BUFFERS_ON_BIT, &ring_buffer_flags);
158 }
159 EXPORT_SYMBOL_GPL(tracing_off);
160
161 /**
162  * tracing_off_permanent - permanently disable ring buffers
163  *
164  * This function, once called, will disable all ring buffers
165  * permanently.
166  */
167 void tracing_off_permanent(void)
168 {
169         set_bit(RB_BUFFERS_DISABLED_BIT, &ring_buffer_flags);
170 }
171
172 /**
173  * tracing_is_on - show state of ring buffers enabled
174  */
175 int tracing_is_on(void)
176 {
177         return ring_buffer_flags == RB_BUFFERS_ON;
178 }
179 EXPORT_SYMBOL_GPL(tracing_is_on);
180
181 #include "trace.h"
182
183 #define RB_EVNT_HDR_SIZE (offsetof(struct ring_buffer_event, array))
184 #define RB_ALIGNMENT            4U
185 #define RB_MAX_SMALL_DATA       28
186
187 enum {
188         RB_LEN_TIME_EXTEND = 8,
189         RB_LEN_TIME_STAMP = 16,
190 };
191
192 static inline int rb_null_event(struct ring_buffer_event *event)
193 {
194         return event->type == RINGBUF_TYPE_PADDING && event->time_delta == 0;
195 }
196
197 static inline int rb_discarded_event(struct ring_buffer_event *event)
198 {
199         return event->type == RINGBUF_TYPE_PADDING && event->time_delta;
200 }
201
202 static void rb_event_set_padding(struct ring_buffer_event *event)
203 {
204         event->type = RINGBUF_TYPE_PADDING;
205         event->time_delta = 0;
206 }
207
208 static unsigned
209 rb_event_data_length(struct ring_buffer_event *event)
210 {
211         unsigned length;
212
213         if (event->len)
214                 length = event->len * RB_ALIGNMENT;
215         else
216                 length = event->array[0];
217         return length + RB_EVNT_HDR_SIZE;
218 }
219
220 /* inline for ring buffer fast paths */
221 static unsigned
222 rb_event_length(struct ring_buffer_event *event)
223 {
224         switch (event->type) {
225         case RINGBUF_TYPE_PADDING:
226                 if (rb_null_event(event))
227                         /* undefined */
228                         return -1;
229                 return rb_event_data_length(event);
230
231         case RINGBUF_TYPE_TIME_EXTEND:
232                 return RB_LEN_TIME_EXTEND;
233
234         case RINGBUF_TYPE_TIME_STAMP:
235                 return RB_LEN_TIME_STAMP;
236
237         case RINGBUF_TYPE_DATA:
238                 return rb_event_data_length(event);
239         default:
240                 BUG();
241         }
242         /* not hit */
243         return 0;
244 }
245
246 /**
247  * ring_buffer_event_length - return the length of the event
248  * @event: the event to get the length of
249  */
250 unsigned ring_buffer_event_length(struct ring_buffer_event *event)
251 {
252         unsigned length = rb_event_length(event);
253         if (event->type != RINGBUF_TYPE_DATA)
254                 return length;
255         length -= RB_EVNT_HDR_SIZE;
256         if (length > RB_MAX_SMALL_DATA + sizeof(event->array[0]))
257                 length -= sizeof(event->array[0]);
258         return length;
259 }
260 EXPORT_SYMBOL_GPL(ring_buffer_event_length);
261
262 /* inline for ring buffer fast paths */
263 static void *
264 rb_event_data(struct ring_buffer_event *event)
265 {
266         BUG_ON(event->type != RINGBUF_TYPE_DATA);
267         /* If length is in len field, then array[0] has the data */
268         if (event->len)
269                 return (void *)&event->array[0];
270         /* Otherwise length is in array[0] and array[1] has the data */
271         return (void *)&event->array[1];
272 }
273
274 /**
275  * ring_buffer_event_data - return the data of the event
276  * @event: the event to get the data from
277  */
278 void *ring_buffer_event_data(struct ring_buffer_event *event)
279 {
280         return rb_event_data(event);
281 }
282 EXPORT_SYMBOL_GPL(ring_buffer_event_data);
283
284 #define for_each_buffer_cpu(buffer, cpu)                \
285         for_each_cpu(cpu, buffer->cpumask)
286
287 #define TS_SHIFT        27
288 #define TS_MASK         ((1ULL << TS_SHIFT) - 1)
289 #define TS_DELTA_TEST   (~TS_MASK)
290
291 struct buffer_data_page {
292         u64              time_stamp;    /* page time stamp */
293         local_t          commit;        /* write committed index */
294         unsigned char    data[];        /* data of buffer page */
295 };
296
297 struct buffer_page {
298         local_t          write;         /* index for next write */
299         unsigned         read;          /* index for next read */
300         struct list_head list;          /* list of free pages */
301         struct buffer_data_page *page;  /* Actual data page */
302 };
303
304 static void rb_init_page(struct buffer_data_page *bpage)
305 {
306         local_set(&bpage->commit, 0);
307 }
308
309 /**
310  * ring_buffer_page_len - the size of data on the page.
311  * @page: The page to read
312  *
313  * Returns the amount of data on the page, including buffer page header.
314  */
315 size_t ring_buffer_page_len(void *page)
316 {
317         return local_read(&((struct buffer_data_page *)page)->commit)
318                 + BUF_PAGE_HDR_SIZE;
319 }
320
321 /*
322  * Also stolen from mm/slob.c. Thanks to Mathieu Desnoyers for pointing
323  * this issue out.
324  */
325 static void free_buffer_page(struct buffer_page *bpage)
326 {
327         free_page((unsigned long)bpage->page);
328         kfree(bpage);
329 }
330
331 /*
332  * We need to fit the time_stamp delta into 27 bits.
333  */
334 static inline int test_time_stamp(u64 delta)
335 {
336         if (delta & TS_DELTA_TEST)
337                 return 1;
338         return 0;
339 }
340
341 #define BUF_PAGE_SIZE (PAGE_SIZE - BUF_PAGE_HDR_SIZE)
342
343 /*
344  * head_page == tail_page && head == tail then buffer is empty.
345  */
346 struct ring_buffer_per_cpu {
347         int                             cpu;
348         struct ring_buffer              *buffer;
349         spinlock_t                      reader_lock; /* serialize readers */
350         raw_spinlock_t                  lock;
351         struct lock_class_key           lock_key;
352         struct list_head                pages;
353         struct buffer_page              *head_page;     /* read from head */
354         struct buffer_page              *tail_page;     /* write to tail */
355         struct buffer_page              *commit_page;   /* committed pages */
356         struct buffer_page              *reader_page;
357         unsigned long                   overrun;
358         unsigned long                   entries;
359         u64                             write_stamp;
360         u64                             read_stamp;
361         atomic_t                        record_disabled;
362 };
363
364 struct ring_buffer {
365         unsigned                        pages;
366         unsigned                        flags;
367         int                             cpus;
368         atomic_t                        record_disabled;
369         cpumask_var_t                   cpumask;
370
371         struct mutex                    mutex;
372
373         struct ring_buffer_per_cpu      **buffers;
374
375 #ifdef CONFIG_HOTPLUG_CPU
376         struct notifier_block           cpu_notify;
377 #endif
378         u64                             (*clock)(void);
379 };
380
381 struct ring_buffer_iter {
382         struct ring_buffer_per_cpu      *cpu_buffer;
383         unsigned long                   head;
384         struct buffer_page              *head_page;
385         u64                             read_stamp;
386 };
387
388 /* buffer may be either ring_buffer or ring_buffer_per_cpu */
389 #define RB_WARN_ON(buffer, cond)                                \
390         ({                                                      \
391                 int _____ret = unlikely(cond);                  \
392                 if (_____ret) {                                 \
393                         atomic_inc(&buffer->record_disabled);   \
394                         WARN_ON(1);                             \
395                 }                                               \
396                 _____ret;                                       \
397         })
398
399 /* Up this if you want to test the TIME_EXTENTS and normalization */
400 #define DEBUG_SHIFT 0
401
402 u64 ring_buffer_time_stamp(struct ring_buffer *buffer, int cpu)
403 {
404         u64 time;
405
406         preempt_disable_notrace();
407         /* shift to debug/test normalization and TIME_EXTENTS */
408         time = buffer->clock() << DEBUG_SHIFT;
409         preempt_enable_no_resched_notrace();
410
411         return time;
412 }
413 EXPORT_SYMBOL_GPL(ring_buffer_time_stamp);
414
415 void ring_buffer_normalize_time_stamp(struct ring_buffer *buffer,
416                                       int cpu, u64 *ts)
417 {
418         /* Just stupid testing the normalize function and deltas */
419         *ts >>= DEBUG_SHIFT;
420 }
421 EXPORT_SYMBOL_GPL(ring_buffer_normalize_time_stamp);
422
423 /**
424  * check_pages - integrity check of buffer pages
425  * @cpu_buffer: CPU buffer with pages to test
426  *
427  * As a safety measure we check to make sure the data pages have not
428  * been corrupted.
429  */
430 static int rb_check_pages(struct ring_buffer_per_cpu *cpu_buffer)
431 {
432         struct list_head *head = &cpu_buffer->pages;
433         struct buffer_page *bpage, *tmp;
434
435         if (RB_WARN_ON(cpu_buffer, head->next->prev != head))
436                 return -1;
437         if (RB_WARN_ON(cpu_buffer, head->prev->next != head))
438                 return -1;
439
440         list_for_each_entry_safe(bpage, tmp, head, list) {
441                 if (RB_WARN_ON(cpu_buffer,
442                                bpage->list.next->prev != &bpage->list))
443                         return -1;
444                 if (RB_WARN_ON(cpu_buffer,
445                                bpage->list.prev->next != &bpage->list))
446                         return -1;
447         }
448
449         return 0;
450 }
451
452 static int rb_allocate_pages(struct ring_buffer_per_cpu *cpu_buffer,
453                              unsigned nr_pages)
454 {
455         struct list_head *head = &cpu_buffer->pages;
456         struct buffer_page *bpage, *tmp;
457         unsigned long addr;
458         LIST_HEAD(pages);
459         unsigned i;
460
461         for (i = 0; i < nr_pages; i++) {
462                 bpage = kzalloc_node(ALIGN(sizeof(*bpage), cache_line_size()),
463                                     GFP_KERNEL, cpu_to_node(cpu_buffer->cpu));
464                 if (!bpage)
465                         goto free_pages;
466                 list_add(&bpage->list, &pages);
467
468                 addr = __get_free_page(GFP_KERNEL);
469                 if (!addr)
470                         goto free_pages;
471                 bpage->page = (void *)addr;
472                 rb_init_page(bpage->page);
473         }
474
475         list_splice(&pages, head);
476
477         rb_check_pages(cpu_buffer);
478
479         return 0;
480
481  free_pages:
482         list_for_each_entry_safe(bpage, tmp, &pages, list) {
483                 list_del_init(&bpage->list);
484                 free_buffer_page(bpage);
485         }
486         return -ENOMEM;
487 }
488
489 static struct ring_buffer_per_cpu *
490 rb_allocate_cpu_buffer(struct ring_buffer *buffer, int cpu)
491 {
492         struct ring_buffer_per_cpu *cpu_buffer;
493         struct buffer_page *bpage;
494         unsigned long addr;
495         int ret;
496
497         cpu_buffer = kzalloc_node(ALIGN(sizeof(*cpu_buffer), cache_line_size()),
498                                   GFP_KERNEL, cpu_to_node(cpu));
499         if (!cpu_buffer)
500                 return NULL;
501
502         cpu_buffer->cpu = cpu;
503         cpu_buffer->buffer = buffer;
504         spin_lock_init(&cpu_buffer->reader_lock);
505         cpu_buffer->lock = (raw_spinlock_t)__RAW_SPIN_LOCK_UNLOCKED;
506         INIT_LIST_HEAD(&cpu_buffer->pages);
507
508         bpage = kzalloc_node(ALIGN(sizeof(*bpage), cache_line_size()),
509                             GFP_KERNEL, cpu_to_node(cpu));
510         if (!bpage)
511                 goto fail_free_buffer;
512
513         cpu_buffer->reader_page = bpage;
514         addr = __get_free_page(GFP_KERNEL);
515         if (!addr)
516                 goto fail_free_reader;
517         bpage->page = (void *)addr;
518         rb_init_page(bpage->page);
519
520         INIT_LIST_HEAD(&cpu_buffer->reader_page->list);
521
522         ret = rb_allocate_pages(cpu_buffer, buffer->pages);
523         if (ret < 0)
524                 goto fail_free_reader;
525
526         cpu_buffer->head_page
527                 = list_entry(cpu_buffer->pages.next, struct buffer_page, list);
528         cpu_buffer->tail_page = cpu_buffer->commit_page = cpu_buffer->head_page;
529
530         return cpu_buffer;
531
532  fail_free_reader:
533         free_buffer_page(cpu_buffer->reader_page);
534
535  fail_free_buffer:
536         kfree(cpu_buffer);
537         return NULL;
538 }
539
540 static void rb_free_cpu_buffer(struct ring_buffer_per_cpu *cpu_buffer)
541 {
542         struct list_head *head = &cpu_buffer->pages;
543         struct buffer_page *bpage, *tmp;
544
545         free_buffer_page(cpu_buffer->reader_page);
546
547         list_for_each_entry_safe(bpage, tmp, head, list) {
548                 list_del_init(&bpage->list);
549                 free_buffer_page(bpage);
550         }
551         kfree(cpu_buffer);
552 }
553
554 /*
555  * Causes compile errors if the struct buffer_page gets bigger
556  * than the struct page.
557  */
558 extern int ring_buffer_page_too_big(void);
559
560 #ifdef CONFIG_HOTPLUG_CPU
561 static int rb_cpu_notify(struct notifier_block *self,
562                          unsigned long action, void *hcpu);
563 #endif
564
565 /**
566  * ring_buffer_alloc - allocate a new ring_buffer
567  * @size: the size in bytes per cpu that is needed.
568  * @flags: attributes to set for the ring buffer.
569  *
570  * Currently the only flag that is available is the RB_FL_OVERWRITE
571  * flag. This flag means that the buffer will overwrite old data
572  * when the buffer wraps. If this flag is not set, the buffer will
573  * drop data when the tail hits the head.
574  */
575 struct ring_buffer *ring_buffer_alloc(unsigned long size, unsigned flags)
576 {
577         struct ring_buffer *buffer;
578         int bsize;
579         int cpu;
580
581         /* Paranoid! Optimizes out when all is well */
582         if (sizeof(struct buffer_page) > sizeof(struct page))
583                 ring_buffer_page_too_big();
584
585
586         /* keep it in its own cache line */
587         buffer = kzalloc(ALIGN(sizeof(*buffer), cache_line_size()),
588                          GFP_KERNEL);
589         if (!buffer)
590                 return NULL;
591
592         if (!alloc_cpumask_var(&buffer->cpumask, GFP_KERNEL))
593                 goto fail_free_buffer;
594
595         buffer->pages = DIV_ROUND_UP(size, BUF_PAGE_SIZE);
596         buffer->flags = flags;
597         buffer->clock = trace_clock_local;
598
599         /* need at least two pages */
600         if (buffer->pages == 1)
601                 buffer->pages++;
602
603         /*
604          * In case of non-hotplug cpu, if the ring-buffer is allocated
605          * in early initcall, it will not be notified of secondary cpus.
606          * In that off case, we need to allocate for all possible cpus.
607          */
608 #ifdef CONFIG_HOTPLUG_CPU
609         get_online_cpus();
610         cpumask_copy(buffer->cpumask, cpu_online_mask);
611 #else
612         cpumask_copy(buffer->cpumask, cpu_possible_mask);
613 #endif
614         buffer->cpus = nr_cpu_ids;
615
616         bsize = sizeof(void *) * nr_cpu_ids;
617         buffer->buffers = kzalloc(ALIGN(bsize, cache_line_size()),
618                                   GFP_KERNEL);
619         if (!buffer->buffers)
620                 goto fail_free_cpumask;
621
622         for_each_buffer_cpu(buffer, cpu) {
623                 buffer->buffers[cpu] =
624                         rb_allocate_cpu_buffer(buffer, cpu);
625                 if (!buffer->buffers[cpu])
626                         goto fail_free_buffers;
627         }
628
629 #ifdef CONFIG_HOTPLUG_CPU
630         buffer->cpu_notify.notifier_call = rb_cpu_notify;
631         buffer->cpu_notify.priority = 0;
632         register_cpu_notifier(&buffer->cpu_notify);
633 #endif
634
635         put_online_cpus();
636         mutex_init(&buffer->mutex);
637
638         return buffer;
639
640  fail_free_buffers:
641         for_each_buffer_cpu(buffer, cpu) {
642                 if (buffer->buffers[cpu])
643                         rb_free_cpu_buffer(buffer->buffers[cpu]);
644         }
645         kfree(buffer->buffers);
646
647  fail_free_cpumask:
648         free_cpumask_var(buffer->cpumask);
649         put_online_cpus();
650
651  fail_free_buffer:
652         kfree(buffer);
653         return NULL;
654 }
655 EXPORT_SYMBOL_GPL(ring_buffer_alloc);
656
657 /**
658  * ring_buffer_free - free a ring buffer.
659  * @buffer: the buffer to free.
660  */
661 void
662 ring_buffer_free(struct ring_buffer *buffer)
663 {
664         int cpu;
665
666         get_online_cpus();
667
668 #ifdef CONFIG_HOTPLUG_CPU
669         unregister_cpu_notifier(&buffer->cpu_notify);
670 #endif
671
672         for_each_buffer_cpu(buffer, cpu)
673                 rb_free_cpu_buffer(buffer->buffers[cpu]);
674
675         put_online_cpus();
676
677         free_cpumask_var(buffer->cpumask);
678
679         kfree(buffer);
680 }
681 EXPORT_SYMBOL_GPL(ring_buffer_free);
682
683 void ring_buffer_set_clock(struct ring_buffer *buffer,
684                            u64 (*clock)(void))
685 {
686         buffer->clock = clock;
687 }
688
689 static void rb_reset_cpu(struct ring_buffer_per_cpu *cpu_buffer);
690
691 static void
692 rb_remove_pages(struct ring_buffer_per_cpu *cpu_buffer, unsigned nr_pages)
693 {
694         struct buffer_page *bpage;
695         struct list_head *p;
696         unsigned i;
697
698         atomic_inc(&cpu_buffer->record_disabled);
699         synchronize_sched();
700
701         for (i = 0; i < nr_pages; i++) {
702                 if (RB_WARN_ON(cpu_buffer, list_empty(&cpu_buffer->pages)))
703                         return;
704                 p = cpu_buffer->pages.next;
705                 bpage = list_entry(p, struct buffer_page, list);
706                 list_del_init(&bpage->list);
707                 free_buffer_page(bpage);
708         }
709         if (RB_WARN_ON(cpu_buffer, list_empty(&cpu_buffer->pages)))
710                 return;
711
712         rb_reset_cpu(cpu_buffer);
713
714         rb_check_pages(cpu_buffer);
715
716         atomic_dec(&cpu_buffer->record_disabled);
717
718 }
719
720 static void
721 rb_insert_pages(struct ring_buffer_per_cpu *cpu_buffer,
722                 struct list_head *pages, unsigned nr_pages)
723 {
724         struct buffer_page *bpage;
725         struct list_head *p;
726         unsigned i;
727
728         atomic_inc(&cpu_buffer->record_disabled);
729         synchronize_sched();
730
731         for (i = 0; i < nr_pages; i++) {
732                 if (RB_WARN_ON(cpu_buffer, list_empty(pages)))
733                         return;
734                 p = pages->next;
735                 bpage = list_entry(p, struct buffer_page, list);
736                 list_del_init(&bpage->list);
737                 list_add_tail(&bpage->list, &cpu_buffer->pages);
738         }
739         rb_reset_cpu(cpu_buffer);
740
741         rb_check_pages(cpu_buffer);
742
743         atomic_dec(&cpu_buffer->record_disabled);
744 }
745
746 /**
747  * ring_buffer_resize - resize the ring buffer
748  * @buffer: the buffer to resize.
749  * @size: the new size.
750  *
751  * The tracer is responsible for making sure that the buffer is
752  * not being used while changing the size.
753  * Note: We may be able to change the above requirement by using
754  *  RCU synchronizations.
755  *
756  * Minimum size is 2 * BUF_PAGE_SIZE.
757  *
758  * Returns -1 on failure.
759  */
760 int ring_buffer_resize(struct ring_buffer *buffer, unsigned long size)
761 {
762         struct ring_buffer_per_cpu *cpu_buffer;
763         unsigned nr_pages, rm_pages, new_pages;
764         struct buffer_page *bpage, *tmp;
765         unsigned long buffer_size;
766         unsigned long addr;
767         LIST_HEAD(pages);
768         int i, cpu;
769
770         /*
771          * Always succeed at resizing a non-existent buffer:
772          */
773         if (!buffer)
774                 return size;
775
776         size = DIV_ROUND_UP(size, BUF_PAGE_SIZE);
777         size *= BUF_PAGE_SIZE;
778         buffer_size = buffer->pages * BUF_PAGE_SIZE;
779
780         /* we need a minimum of two pages */
781         if (size < BUF_PAGE_SIZE * 2)
782                 size = BUF_PAGE_SIZE * 2;
783
784         if (size == buffer_size)
785                 return size;
786
787         mutex_lock(&buffer->mutex);
788         get_online_cpus();
789
790         nr_pages = DIV_ROUND_UP(size, BUF_PAGE_SIZE);
791
792         if (size < buffer_size) {
793
794                 /* easy case, just free pages */
795                 if (RB_WARN_ON(buffer, nr_pages >= buffer->pages))
796                         goto out_fail;
797
798                 rm_pages = buffer->pages - nr_pages;
799
800                 for_each_buffer_cpu(buffer, cpu) {
801                         cpu_buffer = buffer->buffers[cpu];
802                         rb_remove_pages(cpu_buffer, rm_pages);
803                 }
804                 goto out;
805         }
806
807         /*
808          * This is a bit more difficult. We only want to add pages
809          * when we can allocate enough for all CPUs. We do this
810          * by allocating all the pages and storing them on a local
811          * link list. If we succeed in our allocation, then we
812          * add these pages to the cpu_buffers. Otherwise we just free
813          * them all and return -ENOMEM;
814          */
815         if (RB_WARN_ON(buffer, nr_pages <= buffer->pages))
816                 goto out_fail;
817
818         new_pages = nr_pages - buffer->pages;
819
820         for_each_buffer_cpu(buffer, cpu) {
821                 for (i = 0; i < new_pages; i++) {
822                         bpage = kzalloc_node(ALIGN(sizeof(*bpage),
823                                                   cache_line_size()),
824                                             GFP_KERNEL, cpu_to_node(cpu));
825                         if (!bpage)
826                                 goto free_pages;
827                         list_add(&bpage->list, &pages);
828                         addr = __get_free_page(GFP_KERNEL);
829                         if (!addr)
830                                 goto free_pages;
831                         bpage->page = (void *)addr;
832                         rb_init_page(bpage->page);
833                 }
834         }
835
836         for_each_buffer_cpu(buffer, cpu) {
837                 cpu_buffer = buffer->buffers[cpu];
838                 rb_insert_pages(cpu_buffer, &pages, new_pages);
839         }
840
841         if (RB_WARN_ON(buffer, !list_empty(&pages)))
842                 goto out_fail;
843
844  out:
845         buffer->pages = nr_pages;
846         put_online_cpus();
847         mutex_unlock(&buffer->mutex);
848
849         return size;
850
851  free_pages:
852         list_for_each_entry_safe(bpage, tmp, &pages, list) {
853                 list_del_init(&bpage->list);
854                 free_buffer_page(bpage);
855         }
856         put_online_cpus();
857         mutex_unlock(&buffer->mutex);
858         return -ENOMEM;
859
860         /*
861          * Something went totally wrong, and we are too paranoid
862          * to even clean up the mess.
863          */
864  out_fail:
865         put_online_cpus();
866         mutex_unlock(&buffer->mutex);
867         return -1;
868 }
869 EXPORT_SYMBOL_GPL(ring_buffer_resize);
870
871 static inline void *
872 __rb_data_page_index(struct buffer_data_page *bpage, unsigned index)
873 {
874         return bpage->data + index;
875 }
876
877 static inline void *__rb_page_index(struct buffer_page *bpage, unsigned index)
878 {
879         return bpage->page->data + index;
880 }
881
882 static inline struct ring_buffer_event *
883 rb_reader_event(struct ring_buffer_per_cpu *cpu_buffer)
884 {
885         return __rb_page_index(cpu_buffer->reader_page,
886                                cpu_buffer->reader_page->read);
887 }
888
889 static inline struct ring_buffer_event *
890 rb_head_event(struct ring_buffer_per_cpu *cpu_buffer)
891 {
892         return __rb_page_index(cpu_buffer->head_page,
893                                cpu_buffer->head_page->read);
894 }
895
896 static inline struct ring_buffer_event *
897 rb_iter_head_event(struct ring_buffer_iter *iter)
898 {
899         return __rb_page_index(iter->head_page, iter->head);
900 }
901
902 static inline unsigned rb_page_write(struct buffer_page *bpage)
903 {
904         return local_read(&bpage->write);
905 }
906
907 static inline unsigned rb_page_commit(struct buffer_page *bpage)
908 {
909         return local_read(&bpage->page->commit);
910 }
911
912 /* Size is determined by what has been commited */
913 static inline unsigned rb_page_size(struct buffer_page *bpage)
914 {
915         return rb_page_commit(bpage);
916 }
917
918 static inline unsigned
919 rb_commit_index(struct ring_buffer_per_cpu *cpu_buffer)
920 {
921         return rb_page_commit(cpu_buffer->commit_page);
922 }
923
924 static inline unsigned rb_head_size(struct ring_buffer_per_cpu *cpu_buffer)
925 {
926         return rb_page_commit(cpu_buffer->head_page);
927 }
928
929 /*
930  * When the tail hits the head and the buffer is in overwrite mode,
931  * the head jumps to the next page and all content on the previous
932  * page is discarded. But before doing so, we update the overrun
933  * variable of the buffer.
934  */
935 static void rb_update_overflow(struct ring_buffer_per_cpu *cpu_buffer)
936 {
937         struct ring_buffer_event *event;
938         unsigned long head;
939
940         for (head = 0; head < rb_head_size(cpu_buffer);
941              head += rb_event_length(event)) {
942
943                 event = __rb_page_index(cpu_buffer->head_page, head);
944                 if (RB_WARN_ON(cpu_buffer, rb_null_event(event)))
945                         return;
946                 /* Only count data entries */
947                 if (event->type != RINGBUF_TYPE_DATA)
948                         continue;
949                 cpu_buffer->overrun++;
950                 cpu_buffer->entries--;
951         }
952 }
953
954 static inline void rb_inc_page(struct ring_buffer_per_cpu *cpu_buffer,
955                                struct buffer_page **bpage)
956 {
957         struct list_head *p = (*bpage)->list.next;
958
959         if (p == &cpu_buffer->pages)
960                 p = p->next;
961
962         *bpage = list_entry(p, struct buffer_page, list);
963 }
964
965 static inline unsigned
966 rb_event_index(struct ring_buffer_event *event)
967 {
968         unsigned long addr = (unsigned long)event;
969
970         return (addr & ~PAGE_MASK) - (PAGE_SIZE - BUF_PAGE_SIZE);
971 }
972
973 static int
974 rb_is_commit(struct ring_buffer_per_cpu *cpu_buffer,
975              struct ring_buffer_event *event)
976 {
977         unsigned long addr = (unsigned long)event;
978         unsigned long index;
979
980         index = rb_event_index(event);
981         addr &= PAGE_MASK;
982
983         return cpu_buffer->commit_page->page == (void *)addr &&
984                 rb_commit_index(cpu_buffer) == index;
985 }
986
987 static void
988 rb_set_commit_event(struct ring_buffer_per_cpu *cpu_buffer,
989                     struct ring_buffer_event *event)
990 {
991         unsigned long addr = (unsigned long)event;
992         unsigned long index;
993
994         index = rb_event_index(event);
995         addr &= PAGE_MASK;
996
997         while (cpu_buffer->commit_page->page != (void *)addr) {
998                 if (RB_WARN_ON(cpu_buffer,
999                           cpu_buffer->commit_page == cpu_buffer->tail_page))
1000                         return;
1001                 cpu_buffer->commit_page->page->commit =
1002                         cpu_buffer->commit_page->write;
1003                 rb_inc_page(cpu_buffer, &cpu_buffer->commit_page);
1004                 cpu_buffer->write_stamp =
1005                         cpu_buffer->commit_page->page->time_stamp;
1006         }
1007
1008         /* Now set the commit to the event's index */
1009         local_set(&cpu_buffer->commit_page->page->commit, index);
1010 }
1011
1012 static void
1013 rb_set_commit_to_write(struct ring_buffer_per_cpu *cpu_buffer)
1014 {
1015         /*
1016          * We only race with interrupts and NMIs on this CPU.
1017          * If we own the commit event, then we can commit
1018          * all others that interrupted us, since the interruptions
1019          * are in stack format (they finish before they come
1020          * back to us). This allows us to do a simple loop to
1021          * assign the commit to the tail.
1022          */
1023  again:
1024         while (cpu_buffer->commit_page != cpu_buffer->tail_page) {
1025                 cpu_buffer->commit_page->page->commit =
1026                         cpu_buffer->commit_page->write;
1027                 rb_inc_page(cpu_buffer, &cpu_buffer->commit_page);
1028                 cpu_buffer->write_stamp =
1029                         cpu_buffer->commit_page->page->time_stamp;
1030                 /* add barrier to keep gcc from optimizing too much */
1031                 barrier();
1032         }
1033         while (rb_commit_index(cpu_buffer) !=
1034                rb_page_write(cpu_buffer->commit_page)) {
1035                 cpu_buffer->commit_page->page->commit =
1036                         cpu_buffer->commit_page->write;
1037                 barrier();
1038         }
1039
1040         /* again, keep gcc from optimizing */
1041         barrier();
1042
1043         /*
1044          * If an interrupt came in just after the first while loop
1045          * and pushed the tail page forward, we will be left with
1046          * a dangling commit that will never go forward.
1047          */
1048         if (unlikely(cpu_buffer->commit_page != cpu_buffer->tail_page))
1049                 goto again;
1050 }
1051
1052 static void rb_reset_reader_page(struct ring_buffer_per_cpu *cpu_buffer)
1053 {
1054         cpu_buffer->read_stamp = cpu_buffer->reader_page->page->time_stamp;
1055         cpu_buffer->reader_page->read = 0;
1056 }
1057
1058 static void rb_inc_iter(struct ring_buffer_iter *iter)
1059 {
1060         struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
1061
1062         /*
1063          * The iterator could be on the reader page (it starts there).
1064          * But the head could have moved, since the reader was
1065          * found. Check for this case and assign the iterator
1066          * to the head page instead of next.
1067          */
1068         if (iter->head_page == cpu_buffer->reader_page)
1069                 iter->head_page = cpu_buffer->head_page;
1070         else
1071                 rb_inc_page(cpu_buffer, &iter->head_page);
1072
1073         iter->read_stamp = iter->head_page->page->time_stamp;
1074         iter->head = 0;
1075 }
1076
1077 /**
1078  * ring_buffer_update_event - update event type and data
1079  * @event: the even to update
1080  * @type: the type of event
1081  * @length: the size of the event field in the ring buffer
1082  *
1083  * Update the type and data fields of the event. The length
1084  * is the actual size that is written to the ring buffer,
1085  * and with this, we can determine what to place into the
1086  * data field.
1087  */
1088 static void
1089 rb_update_event(struct ring_buffer_event *event,
1090                          unsigned type, unsigned length)
1091 {
1092         event->type = type;
1093
1094         switch (type) {
1095
1096         case RINGBUF_TYPE_PADDING:
1097                 break;
1098
1099         case RINGBUF_TYPE_TIME_EXTEND:
1100                 event->len = DIV_ROUND_UP(RB_LEN_TIME_EXTEND, RB_ALIGNMENT);
1101                 break;
1102
1103         case RINGBUF_TYPE_TIME_STAMP:
1104                 event->len = DIV_ROUND_UP(RB_LEN_TIME_STAMP, RB_ALIGNMENT);
1105                 break;
1106
1107         case RINGBUF_TYPE_DATA:
1108                 length -= RB_EVNT_HDR_SIZE;
1109                 if (length > RB_MAX_SMALL_DATA) {
1110                         event->len = 0;
1111                         event->array[0] = length;
1112                 } else
1113                         event->len = DIV_ROUND_UP(length, RB_ALIGNMENT);
1114                 break;
1115         default:
1116                 BUG();
1117         }
1118 }
1119
1120 static unsigned rb_calculate_event_length(unsigned length)
1121 {
1122         struct ring_buffer_event event; /* Used only for sizeof array */
1123
1124         /* zero length can cause confusions */
1125         if (!length)
1126                 length = 1;
1127
1128         if (length > RB_MAX_SMALL_DATA)
1129                 length += sizeof(event.array[0]);
1130
1131         length += RB_EVNT_HDR_SIZE;
1132         length = ALIGN(length, RB_ALIGNMENT);
1133
1134         return length;
1135 }
1136
1137 static struct ring_buffer_event *
1138 __rb_reserve_next(struct ring_buffer_per_cpu *cpu_buffer,
1139                   unsigned type, unsigned long length, u64 *ts)
1140 {
1141         struct buffer_page *tail_page, *head_page, *reader_page, *commit_page;
1142         unsigned long tail, write;
1143         struct ring_buffer *buffer = cpu_buffer->buffer;
1144         struct ring_buffer_event *event;
1145         unsigned long flags;
1146         bool lock_taken = false;
1147
1148         commit_page = cpu_buffer->commit_page;
1149         /* we just need to protect against interrupts */
1150         barrier();
1151         tail_page = cpu_buffer->tail_page;
1152         write = local_add_return(length, &tail_page->write);
1153         tail = write - length;
1154
1155         /* See if we shot pass the end of this buffer page */
1156         if (write > BUF_PAGE_SIZE) {
1157                 struct buffer_page *next_page = tail_page;
1158
1159                 local_irq_save(flags);
1160                 /*
1161                  * Since the write to the buffer is still not
1162                  * fully lockless, we must be careful with NMIs.
1163                  * The locks in the writers are taken when a write
1164                  * crosses to a new page. The locks protect against
1165                  * races with the readers (this will soon be fixed
1166                  * with a lockless solution).
1167                  *
1168                  * Because we can not protect against NMIs, and we
1169                  * want to keep traces reentrant, we need to manage
1170                  * what happens when we are in an NMI.
1171                  *
1172                  * NMIs can happen after we take the lock.
1173                  * If we are in an NMI, only take the lock
1174                  * if it is not already taken. Otherwise
1175                  * simply fail.
1176                  */
1177                 if (unlikely(in_nmi())) {
1178                         if (!__raw_spin_trylock(&cpu_buffer->lock))
1179                                 goto out_reset;
1180                 } else
1181                         __raw_spin_lock(&cpu_buffer->lock);
1182
1183                 lock_taken = true;
1184
1185                 rb_inc_page(cpu_buffer, &next_page);
1186
1187                 head_page = cpu_buffer->head_page;
1188                 reader_page = cpu_buffer->reader_page;
1189
1190                 /* we grabbed the lock before incrementing */
1191                 if (RB_WARN_ON(cpu_buffer, next_page == reader_page))
1192                         goto out_reset;
1193
1194                 /*
1195                  * If for some reason, we had an interrupt storm that made
1196                  * it all the way around the buffer, bail, and warn
1197                  * about it.
1198                  */
1199                 if (unlikely(next_page == commit_page)) {
1200                         WARN_ON_ONCE(1);
1201                         goto out_reset;
1202                 }
1203
1204                 if (next_page == head_page) {
1205                         if (!(buffer->flags & RB_FL_OVERWRITE))
1206                                 goto out_reset;
1207
1208                         /* tail_page has not moved yet? */
1209                         if (tail_page == cpu_buffer->tail_page) {
1210                                 /* count overflows */
1211                                 rb_update_overflow(cpu_buffer);
1212
1213                                 rb_inc_page(cpu_buffer, &head_page);
1214                                 cpu_buffer->head_page = head_page;
1215                                 cpu_buffer->head_page->read = 0;
1216                         }
1217                 }
1218
1219                 /*
1220                  * If the tail page is still the same as what we think
1221                  * it is, then it is up to us to update the tail
1222                  * pointer.
1223                  */
1224                 if (tail_page == cpu_buffer->tail_page) {
1225                         local_set(&next_page->write, 0);
1226                         local_set(&next_page->page->commit, 0);
1227                         cpu_buffer->tail_page = next_page;
1228
1229                         /* reread the time stamp */
1230                         *ts = ring_buffer_time_stamp(buffer, cpu_buffer->cpu);
1231                         cpu_buffer->tail_page->page->time_stamp = *ts;
1232                 }
1233
1234                 /*
1235                  * The actual tail page has moved forward.
1236                  */
1237                 if (tail < BUF_PAGE_SIZE) {
1238                         /* Mark the rest of the page with padding */
1239                         event = __rb_page_index(tail_page, tail);
1240                         rb_event_set_padding(event);
1241                 }
1242
1243                 if (tail <= BUF_PAGE_SIZE)
1244                         /* Set the write back to the previous setting */
1245                         local_set(&tail_page->write, tail);
1246
1247                 /*
1248                  * If this was a commit entry that failed,
1249                  * increment that too
1250                  */
1251                 if (tail_page == cpu_buffer->commit_page &&
1252                     tail == rb_commit_index(cpu_buffer)) {
1253                         rb_set_commit_to_write(cpu_buffer);
1254                 }
1255
1256                 __raw_spin_unlock(&cpu_buffer->lock);
1257                 local_irq_restore(flags);
1258
1259                 /* fail and let the caller try again */
1260                 return ERR_PTR(-EAGAIN);
1261         }
1262
1263         /* We reserved something on the buffer */
1264
1265         if (RB_WARN_ON(cpu_buffer, write > BUF_PAGE_SIZE))
1266                 return NULL;
1267
1268         event = __rb_page_index(tail_page, tail);
1269         rb_update_event(event, type, length);
1270
1271         /*
1272          * If this is a commit and the tail is zero, then update
1273          * this page's time stamp.
1274          */
1275         if (!tail && rb_is_commit(cpu_buffer, event))
1276                 cpu_buffer->commit_page->page->time_stamp = *ts;
1277
1278         return event;
1279
1280  out_reset:
1281         /* reset write */
1282         if (tail <= BUF_PAGE_SIZE)
1283                 local_set(&tail_page->write, tail);
1284
1285         if (likely(lock_taken))
1286                 __raw_spin_unlock(&cpu_buffer->lock);
1287         local_irq_restore(flags);
1288         return NULL;
1289 }
1290
1291 static int
1292 rb_add_time_stamp(struct ring_buffer_per_cpu *cpu_buffer,
1293                   u64 *ts, u64 *delta)
1294 {
1295         struct ring_buffer_event *event;
1296         static int once;
1297         int ret;
1298
1299         if (unlikely(*delta > (1ULL << 59) && !once++)) {
1300                 printk(KERN_WARNING "Delta way too big! %llu"
1301                        " ts=%llu write stamp = %llu\n",
1302                        (unsigned long long)*delta,
1303                        (unsigned long long)*ts,
1304                        (unsigned long long)cpu_buffer->write_stamp);
1305                 WARN_ON(1);
1306         }
1307
1308         /*
1309          * The delta is too big, we to add a
1310          * new timestamp.
1311          */
1312         event = __rb_reserve_next(cpu_buffer,
1313                                   RINGBUF_TYPE_TIME_EXTEND,
1314                                   RB_LEN_TIME_EXTEND,
1315                                   ts);
1316         if (!event)
1317                 return -EBUSY;
1318
1319         if (PTR_ERR(event) == -EAGAIN)
1320                 return -EAGAIN;
1321
1322         /* Only a commited time event can update the write stamp */
1323         if (rb_is_commit(cpu_buffer, event)) {
1324                 /*
1325                  * If this is the first on the page, then we need to
1326                  * update the page itself, and just put in a zero.
1327                  */
1328                 if (rb_event_index(event)) {
1329                         event->time_delta = *delta & TS_MASK;
1330                         event->array[0] = *delta >> TS_SHIFT;
1331                 } else {
1332                         cpu_buffer->commit_page->page->time_stamp = *ts;
1333                         event->time_delta = 0;
1334                         event->array[0] = 0;
1335                 }
1336                 cpu_buffer->write_stamp = *ts;
1337                 /* let the caller know this was the commit */
1338                 ret = 1;
1339         } else {
1340                 /* Darn, this is just wasted space */
1341                 event->time_delta = 0;
1342                 event->array[0] = 0;
1343                 ret = 0;
1344         }
1345
1346         *delta = 0;
1347
1348         return ret;
1349 }
1350
1351 static struct ring_buffer_event *
1352 rb_reserve_next_event(struct ring_buffer_per_cpu *cpu_buffer,
1353                       unsigned type, unsigned long length)
1354 {
1355         struct ring_buffer_event *event;
1356         u64 ts, delta;
1357         int commit = 0;
1358         int nr_loops = 0;
1359
1360  again:
1361         /*
1362          * We allow for interrupts to reenter here and do a trace.
1363          * If one does, it will cause this original code to loop
1364          * back here. Even with heavy interrupts happening, this
1365          * should only happen a few times in a row. If this happens
1366          * 1000 times in a row, there must be either an interrupt
1367          * storm or we have something buggy.
1368          * Bail!
1369          */
1370         if (RB_WARN_ON(cpu_buffer, ++nr_loops > 1000))
1371                 return NULL;
1372
1373         ts = ring_buffer_time_stamp(cpu_buffer->buffer, cpu_buffer->cpu);
1374
1375         /*
1376          * Only the first commit can update the timestamp.
1377          * Yes there is a race here. If an interrupt comes in
1378          * just after the conditional and it traces too, then it
1379          * will also check the deltas. More than one timestamp may
1380          * also be made. But only the entry that did the actual
1381          * commit will be something other than zero.
1382          */
1383         if (cpu_buffer->tail_page == cpu_buffer->commit_page &&
1384             rb_page_write(cpu_buffer->tail_page) ==
1385             rb_commit_index(cpu_buffer)) {
1386
1387                 delta = ts - cpu_buffer->write_stamp;
1388
1389                 /* make sure this delta is calculated here */
1390                 barrier();
1391
1392                 /* Did the write stamp get updated already? */
1393                 if (unlikely(ts < cpu_buffer->write_stamp))
1394                         delta = 0;
1395
1396                 if (test_time_stamp(delta)) {
1397
1398                         commit = rb_add_time_stamp(cpu_buffer, &ts, &delta);
1399
1400                         if (commit == -EBUSY)
1401                                 return NULL;
1402
1403                         if (commit == -EAGAIN)
1404                                 goto again;
1405
1406                         RB_WARN_ON(cpu_buffer, commit < 0);
1407                 }
1408         } else
1409                 /* Non commits have zero deltas */
1410                 delta = 0;
1411
1412         event = __rb_reserve_next(cpu_buffer, type, length, &ts);
1413         if (PTR_ERR(event) == -EAGAIN)
1414                 goto again;
1415
1416         if (!event) {
1417                 if (unlikely(commit))
1418                         /*
1419                          * Ouch! We needed a timestamp and it was commited. But
1420                          * we didn't get our event reserved.
1421                          */
1422                         rb_set_commit_to_write(cpu_buffer);
1423                 return NULL;
1424         }
1425
1426         /*
1427          * If the timestamp was commited, make the commit our entry
1428          * now so that we will update it when needed.
1429          */
1430         if (commit)
1431                 rb_set_commit_event(cpu_buffer, event);
1432         else if (!rb_is_commit(cpu_buffer, event))
1433                 delta = 0;
1434
1435         event->time_delta = delta;
1436
1437         return event;
1438 }
1439
1440 static DEFINE_PER_CPU(int, rb_need_resched);
1441
1442 /**
1443  * ring_buffer_lock_reserve - reserve a part of the buffer
1444  * @buffer: the ring buffer to reserve from
1445  * @length: the length of the data to reserve (excluding event header)
1446  *
1447  * Returns a reseverd event on the ring buffer to copy directly to.
1448  * The user of this interface will need to get the body to write into
1449  * and can use the ring_buffer_event_data() interface.
1450  *
1451  * The length is the length of the data needed, not the event length
1452  * which also includes the event header.
1453  *
1454  * Must be paired with ring_buffer_unlock_commit, unless NULL is returned.
1455  * If NULL is returned, then nothing has been allocated or locked.
1456  */
1457 struct ring_buffer_event *
1458 ring_buffer_lock_reserve(struct ring_buffer *buffer, unsigned long length)
1459 {
1460         struct ring_buffer_per_cpu *cpu_buffer;
1461         struct ring_buffer_event *event;
1462         int cpu, resched;
1463
1464         if (ring_buffer_flags != RB_BUFFERS_ON)
1465                 return NULL;
1466
1467         if (atomic_read(&buffer->record_disabled))
1468                 return NULL;
1469
1470         /* If we are tracing schedule, we don't want to recurse */
1471         resched = ftrace_preempt_disable();
1472
1473         cpu = raw_smp_processor_id();
1474
1475         if (!cpumask_test_cpu(cpu, buffer->cpumask))
1476                 goto out;
1477
1478         cpu_buffer = buffer->buffers[cpu];
1479
1480         if (atomic_read(&cpu_buffer->record_disabled))
1481                 goto out;
1482
1483         length = rb_calculate_event_length(length);
1484         if (length > BUF_PAGE_SIZE)
1485                 goto out;
1486
1487         event = rb_reserve_next_event(cpu_buffer, RINGBUF_TYPE_DATA, length);
1488         if (!event)
1489                 goto out;
1490
1491         /*
1492          * Need to store resched state on this cpu.
1493          * Only the first needs to.
1494          */
1495
1496         if (preempt_count() == 1)
1497                 per_cpu(rb_need_resched, cpu) = resched;
1498
1499         return event;
1500
1501  out:
1502         ftrace_preempt_enable(resched);
1503         return NULL;
1504 }
1505 EXPORT_SYMBOL_GPL(ring_buffer_lock_reserve);
1506
1507 static void rb_commit(struct ring_buffer_per_cpu *cpu_buffer,
1508                       struct ring_buffer_event *event)
1509 {
1510         cpu_buffer->entries++;
1511
1512         /* Only process further if we own the commit */
1513         if (!rb_is_commit(cpu_buffer, event))
1514                 return;
1515
1516         cpu_buffer->write_stamp += event->time_delta;
1517
1518         rb_set_commit_to_write(cpu_buffer);
1519 }
1520
1521 /**
1522  * ring_buffer_unlock_commit - commit a reserved
1523  * @buffer: The buffer to commit to
1524  * @event: The event pointer to commit.
1525  *
1526  * This commits the data to the ring buffer, and releases any locks held.
1527  *
1528  * Must be paired with ring_buffer_lock_reserve.
1529  */
1530 int ring_buffer_unlock_commit(struct ring_buffer *buffer,
1531                               struct ring_buffer_event *event)
1532 {
1533         struct ring_buffer_per_cpu *cpu_buffer;
1534         int cpu = raw_smp_processor_id();
1535
1536         cpu_buffer = buffer->buffers[cpu];
1537
1538         rb_commit(cpu_buffer, event);
1539
1540         /*
1541          * Only the last preempt count needs to restore preemption.
1542          */
1543         if (preempt_count() == 1)
1544                 ftrace_preempt_enable(per_cpu(rb_need_resched, cpu));
1545         else
1546                 preempt_enable_no_resched_notrace();
1547
1548         return 0;
1549 }
1550 EXPORT_SYMBOL_GPL(ring_buffer_unlock_commit);
1551
1552 /**
1553  * ring_buffer_event_discard - discard any event in the ring buffer
1554  * @event: the event to discard
1555  *
1556  * Sometimes a event that is in the ring buffer needs to be ignored.
1557  * This function lets the user discard an event in the ring buffer
1558  * and then that event will not be read later.
1559  *
1560  * Note, it is up to the user to be careful with this, and protect
1561  * against races. If the user discards an event that has been consumed
1562  * it is possible that it could corrupt the ring buffer.
1563  */
1564 void ring_buffer_event_discard(struct ring_buffer_event *event)
1565 {
1566         event->type = RINGBUF_TYPE_PADDING;
1567         /* time delta must be non zero */
1568         if (!event->time_delta)
1569                 event->time_delta = 1;
1570 }
1571 EXPORT_SYMBOL_GPL(ring_buffer_event_discard);
1572
1573 /**
1574  * ring_buffer_commit_discard - discard an event that has not been committed
1575  * @buffer: the ring buffer
1576  * @event: non committed event to discard
1577  *
1578  * This is similar to ring_buffer_event_discard but must only be
1579  * performed on an event that has not been committed yet. The difference
1580  * is that this will also try to free the event from the ring buffer
1581  * if another event has not been added behind it.
1582  *
1583  * If another event has been added behind it, it will set the event
1584  * up as discarded, and perform the commit.
1585  *
1586  * If this function is called, do not call ring_buffer_unlock_commit on
1587  * the event.
1588  */
1589 void ring_buffer_discard_commit(struct ring_buffer *buffer,
1590                                 struct ring_buffer_event *event)
1591 {
1592         struct ring_buffer_per_cpu *cpu_buffer;
1593         unsigned long new_index, old_index;
1594         struct buffer_page *bpage;
1595         unsigned long index;
1596         unsigned long addr;
1597         int cpu;
1598
1599         /* The event is discarded regardless */
1600         ring_buffer_event_discard(event);
1601
1602         /*
1603          * This must only be called if the event has not been
1604          * committed yet. Thus we can assume that preemption
1605          * is still disabled.
1606          */
1607         RB_WARN_ON(buffer, !preempt_count());
1608
1609         cpu = smp_processor_id();
1610         cpu_buffer = buffer->buffers[cpu];
1611
1612         new_index = rb_event_index(event);
1613         old_index = new_index + rb_event_length(event);
1614         addr = (unsigned long)event;
1615         addr &= PAGE_MASK;
1616
1617         bpage = cpu_buffer->tail_page;
1618
1619         if (bpage == (void *)addr && rb_page_write(bpage) == old_index) {
1620                 /*
1621                  * This is on the tail page. It is possible that
1622                  * a write could come in and move the tail page
1623                  * and write to the next page. That is fine
1624                  * because we just shorten what is on this page.
1625                  */
1626                 index = local_cmpxchg(&bpage->write, old_index, new_index);
1627                 if (index == old_index)
1628                         goto out;
1629         }
1630
1631         /*
1632          * The commit is still visible by the reader, so we
1633          * must increment entries.
1634          */
1635         cpu_buffer->entries++;
1636  out:
1637         /*
1638          * If a write came in and pushed the tail page
1639          * we still need to update the commit pointer
1640          * if we were the commit.
1641          */
1642         if (rb_is_commit(cpu_buffer, event))
1643                 rb_set_commit_to_write(cpu_buffer);
1644
1645         /*
1646          * Only the last preempt count needs to restore preemption.
1647          */
1648         if (preempt_count() == 1)
1649                 ftrace_preempt_enable(per_cpu(rb_need_resched, cpu));
1650         else
1651                 preempt_enable_no_resched_notrace();
1652
1653 }
1654 EXPORT_SYMBOL_GPL(ring_buffer_discard_commit);
1655
1656 /**
1657  * ring_buffer_write - write data to the buffer without reserving
1658  * @buffer: The ring buffer to write to.
1659  * @length: The length of the data being written (excluding the event header)
1660  * @data: The data to write to the buffer.
1661  *
1662  * This is like ring_buffer_lock_reserve and ring_buffer_unlock_commit as
1663  * one function. If you already have the data to write to the buffer, it
1664  * may be easier to simply call this function.
1665  *
1666  * Note, like ring_buffer_lock_reserve, the length is the length of the data
1667  * and not the length of the event which would hold the header.
1668  */
1669 int ring_buffer_write(struct ring_buffer *buffer,
1670                         unsigned long length,
1671                         void *data)
1672 {
1673         struct ring_buffer_per_cpu *cpu_buffer;
1674         struct ring_buffer_event *event;
1675         unsigned long event_length;
1676         void *body;
1677         int ret = -EBUSY;
1678         int cpu, resched;
1679
1680         if (ring_buffer_flags != RB_BUFFERS_ON)
1681                 return -EBUSY;
1682
1683         if (atomic_read(&buffer->record_disabled))
1684                 return -EBUSY;
1685
1686         resched = ftrace_preempt_disable();
1687
1688         cpu = raw_smp_processor_id();
1689
1690         if (!cpumask_test_cpu(cpu, buffer->cpumask))
1691                 goto out;
1692
1693         cpu_buffer = buffer->buffers[cpu];
1694
1695         if (atomic_read(&cpu_buffer->record_disabled))
1696                 goto out;
1697
1698         event_length = rb_calculate_event_length(length);
1699         event = rb_reserve_next_event(cpu_buffer,
1700                                       RINGBUF_TYPE_DATA, event_length);
1701         if (!event)
1702                 goto out;
1703
1704         body = rb_event_data(event);
1705
1706         memcpy(body, data, length);
1707
1708         rb_commit(cpu_buffer, event);
1709
1710         ret = 0;
1711  out:
1712         ftrace_preempt_enable(resched);
1713
1714         return ret;
1715 }
1716 EXPORT_SYMBOL_GPL(ring_buffer_write);
1717
1718 static int rb_per_cpu_empty(struct ring_buffer_per_cpu *cpu_buffer)
1719 {
1720         struct buffer_page *reader = cpu_buffer->reader_page;
1721         struct buffer_page *head = cpu_buffer->head_page;
1722         struct buffer_page *commit = cpu_buffer->commit_page;
1723
1724         return reader->read == rb_page_commit(reader) &&
1725                 (commit == reader ||
1726                  (commit == head &&
1727                   head->read == rb_page_commit(commit)));
1728 }
1729
1730 /**
1731  * ring_buffer_record_disable - stop all writes into the buffer
1732  * @buffer: The ring buffer to stop writes to.
1733  *
1734  * This prevents all writes to the buffer. Any attempt to write
1735  * to the buffer after this will fail and return NULL.
1736  *
1737  * The caller should call synchronize_sched() after this.
1738  */
1739 void ring_buffer_record_disable(struct ring_buffer *buffer)
1740 {
1741         atomic_inc(&buffer->record_disabled);
1742 }
1743 EXPORT_SYMBOL_GPL(ring_buffer_record_disable);
1744
1745 /**
1746  * ring_buffer_record_enable - enable writes to the buffer
1747  * @buffer: The ring buffer to enable writes
1748  *
1749  * Note, multiple disables will need the same number of enables
1750  * to truely enable the writing (much like preempt_disable).
1751  */
1752 void ring_buffer_record_enable(struct ring_buffer *buffer)
1753 {
1754         atomic_dec(&buffer->record_disabled);
1755 }
1756 EXPORT_SYMBOL_GPL(ring_buffer_record_enable);
1757
1758 /**
1759  * ring_buffer_record_disable_cpu - stop all writes into the cpu_buffer
1760  * @buffer: The ring buffer to stop writes to.
1761  * @cpu: The CPU buffer to stop
1762  *
1763  * This prevents all writes to the buffer. Any attempt to write
1764  * to the buffer after this will fail and return NULL.
1765  *
1766  * The caller should call synchronize_sched() after this.
1767  */
1768 void ring_buffer_record_disable_cpu(struct ring_buffer *buffer, int cpu)
1769 {
1770         struct ring_buffer_per_cpu *cpu_buffer;
1771
1772         if (!cpumask_test_cpu(cpu, buffer->cpumask))
1773                 return;
1774
1775         cpu_buffer = buffer->buffers[cpu];
1776         atomic_inc(&cpu_buffer->record_disabled);
1777 }
1778 EXPORT_SYMBOL_GPL(ring_buffer_record_disable_cpu);
1779
1780 /**
1781  * ring_buffer_record_enable_cpu - enable writes to the buffer
1782  * @buffer: The ring buffer to enable writes
1783  * @cpu: The CPU to enable.
1784  *
1785  * Note, multiple disables will need the same number of enables
1786  * to truely enable the writing (much like preempt_disable).
1787  */
1788 void ring_buffer_record_enable_cpu(struct ring_buffer *buffer, int cpu)
1789 {
1790         struct ring_buffer_per_cpu *cpu_buffer;
1791
1792         if (!cpumask_test_cpu(cpu, buffer->cpumask))
1793                 return;
1794
1795         cpu_buffer = buffer->buffers[cpu];
1796         atomic_dec(&cpu_buffer->record_disabled);
1797 }
1798 EXPORT_SYMBOL_GPL(ring_buffer_record_enable_cpu);
1799
1800 /**
1801  * ring_buffer_entries_cpu - get the number of entries in a cpu buffer
1802  * @buffer: The ring buffer
1803  * @cpu: The per CPU buffer to get the entries from.
1804  */
1805 unsigned long ring_buffer_entries_cpu(struct ring_buffer *buffer, int cpu)
1806 {
1807         struct ring_buffer_per_cpu *cpu_buffer;
1808         unsigned long ret;
1809
1810         if (!cpumask_test_cpu(cpu, buffer->cpumask))
1811                 return 0;
1812
1813         cpu_buffer = buffer->buffers[cpu];
1814         ret = cpu_buffer->entries;
1815
1816         return ret;
1817 }
1818 EXPORT_SYMBOL_GPL(ring_buffer_entries_cpu);
1819
1820 /**
1821  * ring_buffer_overrun_cpu - get the number of overruns in a cpu_buffer
1822  * @buffer: The ring buffer
1823  * @cpu: The per CPU buffer to get the number of overruns from
1824  */
1825 unsigned long ring_buffer_overrun_cpu(struct ring_buffer *buffer, int cpu)
1826 {
1827         struct ring_buffer_per_cpu *cpu_buffer;
1828         unsigned long ret;
1829
1830         if (!cpumask_test_cpu(cpu, buffer->cpumask))
1831                 return 0;
1832
1833         cpu_buffer = buffer->buffers[cpu];
1834         ret = cpu_buffer->overrun;
1835
1836         return ret;
1837 }
1838 EXPORT_SYMBOL_GPL(ring_buffer_overrun_cpu);
1839
1840 /**
1841  * ring_buffer_entries - get the number of entries in a buffer
1842  * @buffer: The ring buffer
1843  *
1844  * Returns the total number of entries in the ring buffer
1845  * (all CPU entries)
1846  */
1847 unsigned long ring_buffer_entries(struct ring_buffer *buffer)
1848 {
1849         struct ring_buffer_per_cpu *cpu_buffer;
1850         unsigned long entries = 0;
1851         int cpu;
1852
1853         /* if you care about this being correct, lock the buffer */
1854         for_each_buffer_cpu(buffer, cpu) {
1855                 cpu_buffer = buffer->buffers[cpu];
1856                 entries += cpu_buffer->entries;
1857         }
1858
1859         return entries;
1860 }
1861 EXPORT_SYMBOL_GPL(ring_buffer_entries);
1862
1863 /**
1864  * ring_buffer_overrun_cpu - get the number of overruns in buffer
1865  * @buffer: The ring buffer
1866  *
1867  * Returns the total number of overruns in the ring buffer
1868  * (all CPU entries)
1869  */
1870 unsigned long ring_buffer_overruns(struct ring_buffer *buffer)
1871 {
1872         struct ring_buffer_per_cpu *cpu_buffer;
1873         unsigned long overruns = 0;
1874         int cpu;
1875
1876         /* if you care about this being correct, lock the buffer */
1877         for_each_buffer_cpu(buffer, cpu) {
1878                 cpu_buffer = buffer->buffers[cpu];
1879                 overruns += cpu_buffer->overrun;
1880         }
1881
1882         return overruns;
1883 }
1884 EXPORT_SYMBOL_GPL(ring_buffer_overruns);
1885
1886 static void rb_iter_reset(struct ring_buffer_iter *iter)
1887 {
1888         struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
1889
1890         /* Iterator usage is expected to have record disabled */
1891         if (list_empty(&cpu_buffer->reader_page->list)) {
1892                 iter->head_page = cpu_buffer->head_page;
1893                 iter->head = cpu_buffer->head_page->read;
1894         } else {
1895                 iter->head_page = cpu_buffer->reader_page;
1896                 iter->head = cpu_buffer->reader_page->read;
1897         }
1898         if (iter->head)
1899                 iter->read_stamp = cpu_buffer->read_stamp;
1900         else
1901                 iter->read_stamp = iter->head_page->page->time_stamp;
1902 }
1903
1904 /**
1905  * ring_buffer_iter_reset - reset an iterator
1906  * @iter: The iterator to reset
1907  *
1908  * Resets the iterator, so that it will start from the beginning
1909  * again.
1910  */
1911 void ring_buffer_iter_reset(struct ring_buffer_iter *iter)
1912 {
1913         struct ring_buffer_per_cpu *cpu_buffer;
1914         unsigned long flags;
1915
1916         if (!iter)
1917                 return;
1918
1919         cpu_buffer = iter->cpu_buffer;
1920
1921         spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
1922         rb_iter_reset(iter);
1923         spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
1924 }
1925 EXPORT_SYMBOL_GPL(ring_buffer_iter_reset);
1926
1927 /**
1928  * ring_buffer_iter_empty - check if an iterator has no more to read
1929  * @iter: The iterator to check
1930  */
1931 int ring_buffer_iter_empty(struct ring_buffer_iter *iter)
1932 {
1933         struct ring_buffer_per_cpu *cpu_buffer;
1934
1935         cpu_buffer = iter->cpu_buffer;
1936
1937         return iter->head_page == cpu_buffer->commit_page &&
1938                 iter->head == rb_commit_index(cpu_buffer);
1939 }
1940 EXPORT_SYMBOL_GPL(ring_buffer_iter_empty);
1941
1942 static void
1943 rb_update_read_stamp(struct ring_buffer_per_cpu *cpu_buffer,
1944                      struct ring_buffer_event *event)
1945 {
1946         u64 delta;
1947
1948         switch (event->type) {
1949         case RINGBUF_TYPE_PADDING:
1950                 return;
1951
1952         case RINGBUF_TYPE_TIME_EXTEND:
1953                 delta = event->array[0];
1954                 delta <<= TS_SHIFT;
1955                 delta += event->time_delta;
1956                 cpu_buffer->read_stamp += delta;
1957                 return;
1958
1959         case RINGBUF_TYPE_TIME_STAMP:
1960                 /* FIXME: not implemented */
1961                 return;
1962
1963         case RINGBUF_TYPE_DATA:
1964                 cpu_buffer->read_stamp += event->time_delta;
1965                 return;
1966
1967         default:
1968                 BUG();
1969         }
1970         return;
1971 }
1972
1973 static void
1974 rb_update_iter_read_stamp(struct ring_buffer_iter *iter,
1975                           struct ring_buffer_event *event)
1976 {
1977         u64 delta;
1978
1979         switch (event->type) {
1980         case RINGBUF_TYPE_PADDING:
1981                 return;
1982
1983         case RINGBUF_TYPE_TIME_EXTEND:
1984                 delta = event->array[0];
1985                 delta <<= TS_SHIFT;
1986                 delta += event->time_delta;
1987                 iter->read_stamp += delta;
1988                 return;
1989
1990         case RINGBUF_TYPE_TIME_STAMP:
1991                 /* FIXME: not implemented */
1992                 return;
1993
1994         case RINGBUF_TYPE_DATA:
1995                 iter->read_stamp += event->time_delta;
1996                 return;
1997
1998         default:
1999                 BUG();
2000         }
2001         return;
2002 }
2003
2004 static struct buffer_page *
2005 rb_get_reader_page(struct ring_buffer_per_cpu *cpu_buffer)
2006 {
2007         struct buffer_page *reader = NULL;
2008         unsigned long flags;
2009         int nr_loops = 0;
2010
2011         local_irq_save(flags);
2012         __raw_spin_lock(&cpu_buffer->lock);
2013
2014  again:
2015         /*
2016          * This should normally only loop twice. But because the
2017          * start of the reader inserts an empty page, it causes
2018          * a case where we will loop three times. There should be no
2019          * reason to loop four times (that I know of).
2020          */
2021         if (RB_WARN_ON(cpu_buffer, ++nr_loops > 3)) {
2022                 reader = NULL;
2023                 goto out;
2024         }
2025
2026         reader = cpu_buffer->reader_page;
2027
2028         /* If there's more to read, return this page */
2029         if (cpu_buffer->reader_page->read < rb_page_size(reader))
2030                 goto out;
2031
2032         /* Never should we have an index greater than the size */
2033         if (RB_WARN_ON(cpu_buffer,
2034                        cpu_buffer->reader_page->read > rb_page_size(reader)))
2035                 goto out;
2036
2037         /* check if we caught up to the tail */
2038         reader = NULL;
2039         if (cpu_buffer->commit_page == cpu_buffer->reader_page)
2040                 goto out;
2041
2042         /*
2043          * Splice the empty reader page into the list around the head.
2044          * Reset the reader page to size zero.
2045          */
2046
2047         reader = cpu_buffer->head_page;
2048         cpu_buffer->reader_page->list.next = reader->list.next;
2049         cpu_buffer->reader_page->list.prev = reader->list.prev;
2050
2051         local_set(&cpu_buffer->reader_page->write, 0);
2052         local_set(&cpu_buffer->reader_page->page->commit, 0);
2053
2054         /* Make the reader page now replace the head */
2055         reader->list.prev->next = &cpu_buffer->reader_page->list;
2056         reader->list.next->prev = &cpu_buffer->reader_page->list;
2057
2058         /*
2059          * If the tail is on the reader, then we must set the head
2060          * to the inserted page, otherwise we set it one before.
2061          */
2062         cpu_buffer->head_page = cpu_buffer->reader_page;
2063
2064         if (cpu_buffer->commit_page != reader)
2065                 rb_inc_page(cpu_buffer, &cpu_buffer->head_page);
2066
2067         /* Finally update the reader page to the new head */
2068         cpu_buffer->reader_page = reader;
2069         rb_reset_reader_page(cpu_buffer);
2070
2071         goto again;
2072
2073  out:
2074         __raw_spin_unlock(&cpu_buffer->lock);
2075         local_irq_restore(flags);
2076
2077         return reader;
2078 }
2079
2080 static void rb_advance_reader(struct ring_buffer_per_cpu *cpu_buffer)
2081 {
2082         struct ring_buffer_event *event;
2083         struct buffer_page *reader;
2084         unsigned length;
2085
2086         reader = rb_get_reader_page(cpu_buffer);
2087
2088         /* This function should not be called when buffer is empty */
2089         if (RB_WARN_ON(cpu_buffer, !reader))
2090                 return;
2091
2092         event = rb_reader_event(cpu_buffer);
2093
2094         if (event->type == RINGBUF_TYPE_DATA || rb_discarded_event(event))
2095                 cpu_buffer->entries--;
2096
2097         rb_update_read_stamp(cpu_buffer, event);
2098
2099         length = rb_event_length(event);
2100         cpu_buffer->reader_page->read += length;
2101 }
2102
2103 static void rb_advance_iter(struct ring_buffer_iter *iter)
2104 {
2105         struct ring_buffer *buffer;
2106         struct ring_buffer_per_cpu *cpu_buffer;
2107         struct ring_buffer_event *event;
2108         unsigned length;
2109
2110         cpu_buffer = iter->cpu_buffer;
2111         buffer = cpu_buffer->buffer;
2112
2113         /*
2114          * Check if we are at the end of the buffer.
2115          */
2116         if (iter->head >= rb_page_size(iter->head_page)) {
2117                 if (RB_WARN_ON(buffer,
2118                                iter->head_page == cpu_buffer->commit_page))
2119                         return;
2120                 rb_inc_iter(iter);
2121                 return;
2122         }
2123
2124         event = rb_iter_head_event(iter);
2125
2126         length = rb_event_length(event);
2127
2128         /*
2129          * This should not be called to advance the header if we are
2130          * at the tail of the buffer.
2131          */
2132         if (RB_WARN_ON(cpu_buffer,
2133                        (iter->head_page == cpu_buffer->commit_page) &&
2134                        (iter->head + length > rb_commit_index(cpu_buffer))))
2135                 return;
2136
2137         rb_update_iter_read_stamp(iter, event);
2138
2139         iter->head += length;
2140
2141         /* check for end of page padding */
2142         if ((iter->head >= rb_page_size(iter->head_page)) &&
2143             (iter->head_page != cpu_buffer->commit_page))
2144                 rb_advance_iter(iter);
2145 }
2146
2147 static struct ring_buffer_event *
2148 rb_buffer_peek(struct ring_buffer *buffer, int cpu, u64 *ts)
2149 {
2150         struct ring_buffer_per_cpu *cpu_buffer;
2151         struct ring_buffer_event *event;
2152         struct buffer_page *reader;
2153         int nr_loops = 0;
2154
2155         cpu_buffer = buffer->buffers[cpu];
2156
2157  again:
2158         /*
2159          * We repeat when a timestamp is encountered. It is possible
2160          * to get multiple timestamps from an interrupt entering just
2161          * as one timestamp is about to be written. The max times
2162          * that this can happen is the number of nested interrupts we
2163          * can have.  Nesting 10 deep of interrupts is clearly
2164          * an anomaly.
2165          */
2166         if (RB_WARN_ON(cpu_buffer, ++nr_loops > 10))
2167                 return NULL;
2168
2169         reader = rb_get_reader_page(cpu_buffer);
2170         if (!reader)
2171                 return NULL;
2172
2173         event = rb_reader_event(cpu_buffer);
2174
2175         switch (event->type) {
2176         case RINGBUF_TYPE_PADDING:
2177                 if (rb_null_event(event))
2178                         RB_WARN_ON(cpu_buffer, 1);
2179                 /*
2180                  * Because the writer could be discarding every
2181                  * event it creates (which would probably be bad)
2182                  * if we were to go back to "again" then we may never
2183                  * catch up, and will trigger the warn on, or lock
2184                  * the box. Return the padding, and we will release
2185                  * the current locks, and try again.
2186                  */
2187                 rb_advance_reader(cpu_buffer);
2188                 return event;
2189
2190         case RINGBUF_TYPE_TIME_EXTEND:
2191                 /* Internal data, OK to advance */
2192                 rb_advance_reader(cpu_buffer);
2193                 goto again;
2194
2195         case RINGBUF_TYPE_TIME_STAMP:
2196                 /* FIXME: not implemented */
2197                 rb_advance_reader(cpu_buffer);
2198                 goto again;
2199
2200         case RINGBUF_TYPE_DATA:
2201                 if (ts) {
2202                         *ts = cpu_buffer->read_stamp + event->time_delta;
2203                         ring_buffer_normalize_time_stamp(buffer,
2204                                                          cpu_buffer->cpu, ts);
2205                 }
2206                 return event;
2207
2208         default:
2209                 BUG();
2210         }
2211
2212         return NULL;
2213 }
2214 EXPORT_SYMBOL_GPL(ring_buffer_peek);
2215
2216 static struct ring_buffer_event *
2217 rb_iter_peek(struct ring_buffer_iter *iter, u64 *ts)
2218 {
2219         struct ring_buffer *buffer;
2220         struct ring_buffer_per_cpu *cpu_buffer;
2221         struct ring_buffer_event *event;
2222         int nr_loops = 0;
2223
2224         if (ring_buffer_iter_empty(iter))
2225                 return NULL;
2226
2227         cpu_buffer = iter->cpu_buffer;
2228         buffer = cpu_buffer->buffer;
2229
2230  again:
2231         /*
2232          * We repeat when a timestamp is encountered. It is possible
2233          * to get multiple timestamps from an interrupt entering just
2234          * as one timestamp is about to be written. The max times
2235          * that this can happen is the number of nested interrupts we
2236          * can have. Nesting 10 deep of interrupts is clearly
2237          * an anomaly.
2238          */
2239         if (RB_WARN_ON(cpu_buffer, ++nr_loops > 10))
2240                 return NULL;
2241
2242         if (rb_per_cpu_empty(cpu_buffer))
2243                 return NULL;
2244
2245         event = rb_iter_head_event(iter);
2246
2247         switch (event->type) {
2248         case RINGBUF_TYPE_PADDING:
2249                 if (rb_null_event(event)) {
2250                         rb_inc_iter(iter);
2251                         goto again;
2252                 }
2253                 rb_advance_iter(iter);
2254                 return event;
2255
2256         case RINGBUF_TYPE_TIME_EXTEND:
2257                 /* Internal data, OK to advance */
2258                 rb_advance_iter(iter);
2259                 goto again;
2260
2261         case RINGBUF_TYPE_TIME_STAMP:
2262                 /* FIXME: not implemented */
2263                 rb_advance_iter(iter);
2264                 goto again;
2265
2266         case RINGBUF_TYPE_DATA:
2267                 if (ts) {
2268                         *ts = iter->read_stamp + event->time_delta;
2269                         ring_buffer_normalize_time_stamp(buffer,
2270                                                          cpu_buffer->cpu, ts);
2271                 }
2272                 return event;
2273
2274         default:
2275                 BUG();
2276         }
2277
2278         return NULL;
2279 }
2280 EXPORT_SYMBOL_GPL(ring_buffer_iter_peek);
2281
2282 /**
2283  * ring_buffer_peek - peek at the next event to be read
2284  * @buffer: The ring buffer to read
2285  * @cpu: The cpu to peak at
2286  * @ts: The timestamp counter of this event.
2287  *
2288  * This will return the event that will be read next, but does
2289  * not consume the data.
2290  */
2291 struct ring_buffer_event *
2292 ring_buffer_peek(struct ring_buffer *buffer, int cpu, u64 *ts)
2293 {
2294         struct ring_buffer_per_cpu *cpu_buffer = buffer->buffers[cpu];
2295         struct ring_buffer_event *event;
2296         unsigned long flags;
2297
2298         if (!cpumask_test_cpu(cpu, buffer->cpumask))
2299                 return NULL;
2300
2301  again:
2302         spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
2303         event = rb_buffer_peek(buffer, cpu, ts);
2304         spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
2305
2306         if (event && event->type == RINGBUF_TYPE_PADDING) {
2307                 cpu_relax();
2308                 goto again;
2309         }
2310
2311         return event;
2312 }
2313
2314 /**
2315  * ring_buffer_iter_peek - peek at the next event to be read
2316  * @iter: The ring buffer iterator
2317  * @ts: The timestamp counter of this event.
2318  *
2319  * This will return the event that will be read next, but does
2320  * not increment the iterator.
2321  */
2322 struct ring_buffer_event *
2323 ring_buffer_iter_peek(struct ring_buffer_iter *iter, u64 *ts)
2324 {
2325         struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
2326         struct ring_buffer_event *event;
2327         unsigned long flags;
2328
2329  again:
2330         spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
2331         event = rb_iter_peek(iter, ts);
2332         spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
2333
2334         if (event && event->type == RINGBUF_TYPE_PADDING) {
2335                 cpu_relax();
2336                 goto again;
2337         }
2338
2339         return event;
2340 }
2341
2342 /**
2343  * ring_buffer_consume - return an event and consume it
2344  * @buffer: The ring buffer to get the next event from
2345  *
2346  * Returns the next event in the ring buffer, and that event is consumed.
2347  * Meaning, that sequential reads will keep returning a different event,
2348  * and eventually empty the ring buffer if the producer is slower.
2349  */
2350 struct ring_buffer_event *
2351 ring_buffer_consume(struct ring_buffer *buffer, int cpu, u64 *ts)
2352 {
2353         struct ring_buffer_per_cpu *cpu_buffer;
2354         struct ring_buffer_event *event = NULL;
2355         unsigned long flags;
2356
2357  again:
2358         /* might be called in atomic */
2359         preempt_disable();
2360
2361         if (!cpumask_test_cpu(cpu, buffer->cpumask))
2362                 goto out;
2363
2364         cpu_buffer = buffer->buffers[cpu];
2365         spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
2366
2367         event = rb_buffer_peek(buffer, cpu, ts);
2368         if (!event)
2369                 goto out_unlock;
2370
2371         rb_advance_reader(cpu_buffer);
2372
2373  out_unlock:
2374         spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
2375
2376  out:
2377         preempt_enable();
2378
2379         if (event && event->type == RINGBUF_TYPE_PADDING) {
2380                 cpu_relax();
2381                 goto again;
2382         }
2383
2384         return event;
2385 }
2386 EXPORT_SYMBOL_GPL(ring_buffer_consume);
2387
2388 /**
2389  * ring_buffer_read_start - start a non consuming read of the buffer
2390  * @buffer: The ring buffer to read from
2391  * @cpu: The cpu buffer to iterate over
2392  *
2393  * This starts up an iteration through the buffer. It also disables
2394  * the recording to the buffer until the reading is finished.
2395  * This prevents the reading from being corrupted. This is not
2396  * a consuming read, so a producer is not expected.
2397  *
2398  * Must be paired with ring_buffer_finish.
2399  */
2400 struct ring_buffer_iter *
2401 ring_buffer_read_start(struct ring_buffer *buffer, int cpu)
2402 {
2403         struct ring_buffer_per_cpu *cpu_buffer;
2404         struct ring_buffer_iter *iter;
2405         unsigned long flags;
2406
2407         if (!cpumask_test_cpu(cpu, buffer->cpumask))
2408                 return NULL;
2409
2410         iter = kmalloc(sizeof(*iter), GFP_KERNEL);
2411         if (!iter)
2412                 return NULL;
2413
2414         cpu_buffer = buffer->buffers[cpu];
2415
2416         iter->cpu_buffer = cpu_buffer;
2417
2418         atomic_inc(&cpu_buffer->record_disabled);
2419         synchronize_sched();
2420
2421         spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
2422         __raw_spin_lock(&cpu_buffer->lock);
2423         rb_iter_reset(iter);
2424         __raw_spin_unlock(&cpu_buffer->lock);
2425         spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
2426
2427         return iter;
2428 }
2429 EXPORT_SYMBOL_GPL(ring_buffer_read_start);
2430
2431 /**
2432  * ring_buffer_finish - finish reading the iterator of the buffer
2433  * @iter: The iterator retrieved by ring_buffer_start
2434  *
2435  * This re-enables the recording to the buffer, and frees the
2436  * iterator.
2437  */
2438 void
2439 ring_buffer_read_finish(struct ring_buffer_iter *iter)
2440 {
2441         struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
2442
2443         atomic_dec(&cpu_buffer->record_disabled);
2444         kfree(iter);
2445 }
2446 EXPORT_SYMBOL_GPL(ring_buffer_read_finish);
2447
2448 /**
2449  * ring_buffer_read - read the next item in the ring buffer by the iterator
2450  * @iter: The ring buffer iterator
2451  * @ts: The time stamp of the event read.
2452  *
2453  * This reads the next event in the ring buffer and increments the iterator.
2454  */
2455 struct ring_buffer_event *
2456 ring_buffer_read(struct ring_buffer_iter *iter, u64 *ts)
2457 {
2458         struct ring_buffer_event *event;
2459         struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
2460         unsigned long flags;
2461
2462  again:
2463         spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
2464         event = rb_iter_peek(iter, ts);
2465         if (!event)
2466                 goto out;
2467
2468         rb_advance_iter(iter);
2469  out:
2470         spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
2471
2472         if (event && event->type == RINGBUF_TYPE_PADDING) {
2473                 cpu_relax();
2474                 goto again;
2475         }
2476
2477         return event;
2478 }
2479 EXPORT_SYMBOL_GPL(ring_buffer_read);
2480
2481 /**
2482  * ring_buffer_size - return the size of the ring buffer (in bytes)
2483  * @buffer: The ring buffer.
2484  */
2485 unsigned long ring_buffer_size(struct ring_buffer *buffer)
2486 {
2487         return BUF_PAGE_SIZE * buffer->pages;
2488 }
2489 EXPORT_SYMBOL_GPL(ring_buffer_size);
2490
2491 static void
2492 rb_reset_cpu(struct ring_buffer_per_cpu *cpu_buffer)
2493 {
2494         cpu_buffer->head_page
2495                 = list_entry(cpu_buffer->pages.next, struct buffer_page, list);
2496         local_set(&cpu_buffer->head_page->write, 0);
2497         local_set(&cpu_buffer->head_page->page->commit, 0);
2498
2499         cpu_buffer->head_page->read = 0;
2500
2501         cpu_buffer->tail_page = cpu_buffer->head_page;
2502         cpu_buffer->commit_page = cpu_buffer->head_page;
2503
2504         INIT_LIST_HEAD(&cpu_buffer->reader_page->list);
2505         local_set(&cpu_buffer->reader_page->write, 0);
2506         local_set(&cpu_buffer->reader_page->page->commit, 0);
2507         cpu_buffer->reader_page->read = 0;
2508
2509         cpu_buffer->overrun = 0;
2510         cpu_buffer->entries = 0;
2511
2512         cpu_buffer->write_stamp = 0;
2513         cpu_buffer->read_stamp = 0;
2514 }
2515
2516 /**
2517  * ring_buffer_reset_cpu - reset a ring buffer per CPU buffer
2518  * @buffer: The ring buffer to reset a per cpu buffer of
2519  * @cpu: The CPU buffer to be reset
2520  */
2521 void ring_buffer_reset_cpu(struct ring_buffer *buffer, int cpu)
2522 {
2523         struct ring_buffer_per_cpu *cpu_buffer = buffer->buffers[cpu];
2524         unsigned long flags;
2525
2526         if (!cpumask_test_cpu(cpu, buffer->cpumask))
2527                 return;
2528
2529         spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
2530
2531         __raw_spin_lock(&cpu_buffer->lock);
2532
2533         rb_reset_cpu(cpu_buffer);
2534
2535         __raw_spin_unlock(&cpu_buffer->lock);
2536
2537         spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
2538 }
2539 EXPORT_SYMBOL_GPL(ring_buffer_reset_cpu);
2540
2541 /**
2542  * ring_buffer_reset - reset a ring buffer
2543  * @buffer: The ring buffer to reset all cpu buffers
2544  */
2545 void ring_buffer_reset(struct ring_buffer *buffer)
2546 {
2547         int cpu;
2548
2549         for_each_buffer_cpu(buffer, cpu)
2550                 ring_buffer_reset_cpu(buffer, cpu);
2551 }
2552 EXPORT_SYMBOL_GPL(ring_buffer_reset);
2553
2554 /**
2555  * rind_buffer_empty - is the ring buffer empty?
2556  * @buffer: The ring buffer to test
2557  */
2558 int ring_buffer_empty(struct ring_buffer *buffer)
2559 {
2560         struct ring_buffer_per_cpu *cpu_buffer;
2561         int cpu;
2562
2563         /* yes this is racy, but if you don't like the race, lock the buffer */
2564         for_each_buffer_cpu(buffer, cpu) {
2565                 cpu_buffer = buffer->buffers[cpu];
2566                 if (!rb_per_cpu_empty(cpu_buffer))
2567                         return 0;
2568         }
2569
2570         return 1;
2571 }
2572 EXPORT_SYMBOL_GPL(ring_buffer_empty);
2573
2574 /**
2575  * ring_buffer_empty_cpu - is a cpu buffer of a ring buffer empty?
2576  * @buffer: The ring buffer
2577  * @cpu: The CPU buffer to test
2578  */
2579 int ring_buffer_empty_cpu(struct ring_buffer *buffer, int cpu)
2580 {
2581         struct ring_buffer_per_cpu *cpu_buffer;
2582         int ret;
2583
2584         if (!cpumask_test_cpu(cpu, buffer->cpumask))
2585                 return 1;
2586
2587         cpu_buffer = buffer->buffers[cpu];
2588         ret = rb_per_cpu_empty(cpu_buffer);
2589
2590
2591         return ret;
2592 }
2593 EXPORT_SYMBOL_GPL(ring_buffer_empty_cpu);
2594
2595 /**
2596  * ring_buffer_swap_cpu - swap a CPU buffer between two ring buffers
2597  * @buffer_a: One buffer to swap with
2598  * @buffer_b: The other buffer to swap with
2599  *
2600  * This function is useful for tracers that want to take a "snapshot"
2601  * of a CPU buffer and has another back up buffer lying around.
2602  * it is expected that the tracer handles the cpu buffer not being
2603  * used at the moment.
2604  */
2605 int ring_buffer_swap_cpu(struct ring_buffer *buffer_a,
2606                          struct ring_buffer *buffer_b, int cpu)
2607 {
2608         struct ring_buffer_per_cpu *cpu_buffer_a;
2609         struct ring_buffer_per_cpu *cpu_buffer_b;
2610         int ret = -EINVAL;
2611
2612         if (!cpumask_test_cpu(cpu, buffer_a->cpumask) ||
2613             !cpumask_test_cpu(cpu, buffer_b->cpumask))
2614                 goto out;
2615
2616         /* At least make sure the two buffers are somewhat the same */
2617         if (buffer_a->pages != buffer_b->pages)
2618                 goto out;
2619
2620         ret = -EAGAIN;
2621
2622         if (ring_buffer_flags != RB_BUFFERS_ON)
2623                 goto out;
2624
2625         if (atomic_read(&buffer_a->record_disabled))
2626                 goto out;
2627
2628         if (atomic_read(&buffer_b->record_disabled))
2629                 goto out;
2630
2631         cpu_buffer_a = buffer_a->buffers[cpu];
2632         cpu_buffer_b = buffer_b->buffers[cpu];
2633
2634         if (atomic_read(&cpu_buffer_a->record_disabled))
2635                 goto out;
2636
2637         if (atomic_read(&cpu_buffer_b->record_disabled))
2638                 goto out;
2639
2640         /*
2641          * We can't do a synchronize_sched here because this
2642          * function can be called in atomic context.
2643          * Normally this will be called from the same CPU as cpu.
2644          * If not it's up to the caller to protect this.
2645          */
2646         atomic_inc(&cpu_buffer_a->record_disabled);
2647         atomic_inc(&cpu_buffer_b->record_disabled);
2648
2649         buffer_a->buffers[cpu] = cpu_buffer_b;
2650         buffer_b->buffers[cpu] = cpu_buffer_a;
2651
2652         cpu_buffer_b->buffer = buffer_a;
2653         cpu_buffer_a->buffer = buffer_b;
2654
2655         atomic_dec(&cpu_buffer_a->record_disabled);
2656         atomic_dec(&cpu_buffer_b->record_disabled);
2657
2658         ret = 0;
2659 out:
2660         return ret;
2661 }
2662 EXPORT_SYMBOL_GPL(ring_buffer_swap_cpu);
2663
2664 static void rb_remove_entries(struct ring_buffer_per_cpu *cpu_buffer,
2665                               struct buffer_data_page *bpage,
2666                               unsigned int offset)
2667 {
2668         struct ring_buffer_event *event;
2669         unsigned long head;
2670
2671         __raw_spin_lock(&cpu_buffer->lock);
2672         for (head = offset; head < local_read(&bpage->commit);
2673              head += rb_event_length(event)) {
2674
2675                 event = __rb_data_page_index(bpage, head);
2676                 if (RB_WARN_ON(cpu_buffer, rb_null_event(event)))
2677                         return;
2678                 /* Only count data entries */
2679                 if (event->type != RINGBUF_TYPE_DATA)
2680                         continue;
2681                 cpu_buffer->entries--;
2682         }
2683         __raw_spin_unlock(&cpu_buffer->lock);
2684 }
2685
2686 /**
2687  * ring_buffer_alloc_read_page - allocate a page to read from buffer
2688  * @buffer: the buffer to allocate for.
2689  *
2690  * This function is used in conjunction with ring_buffer_read_page.
2691  * When reading a full page from the ring buffer, these functions
2692  * can be used to speed up the process. The calling function should
2693  * allocate a few pages first with this function. Then when it
2694  * needs to get pages from the ring buffer, it passes the result
2695  * of this function into ring_buffer_read_page, which will swap
2696  * the page that was allocated, with the read page of the buffer.
2697  *
2698  * Returns:
2699  *  The page allocated, or NULL on error.
2700  */
2701 void *ring_buffer_alloc_read_page(struct ring_buffer *buffer)
2702 {
2703         struct buffer_data_page *bpage;
2704         unsigned long addr;
2705
2706         addr = __get_free_page(GFP_KERNEL);
2707         if (!addr)
2708                 return NULL;
2709
2710         bpage = (void *)addr;
2711
2712         rb_init_page(bpage);
2713
2714         return bpage;
2715 }
2716
2717 /**
2718  * ring_buffer_free_read_page - free an allocated read page
2719  * @buffer: the buffer the page was allocate for
2720  * @data: the page to free
2721  *
2722  * Free a page allocated from ring_buffer_alloc_read_page.
2723  */
2724 void ring_buffer_free_read_page(struct ring_buffer *buffer, void *data)
2725 {
2726         free_page((unsigned long)data);
2727 }
2728
2729 /**
2730  * ring_buffer_read_page - extract a page from the ring buffer
2731  * @buffer: buffer to extract from
2732  * @data_page: the page to use allocated from ring_buffer_alloc_read_page
2733  * @len: amount to extract
2734  * @cpu: the cpu of the buffer to extract
2735  * @full: should the extraction only happen when the page is full.
2736  *
2737  * This function will pull out a page from the ring buffer and consume it.
2738  * @data_page must be the address of the variable that was returned
2739  * from ring_buffer_alloc_read_page. This is because the page might be used
2740  * to swap with a page in the ring buffer.
2741  *
2742  * for example:
2743  *      rpage = ring_buffer_alloc_read_page(buffer);
2744  *      if (!rpage)
2745  *              return error;
2746  *      ret = ring_buffer_read_page(buffer, &rpage, len, cpu, 0);
2747  *      if (ret >= 0)
2748  *              process_page(rpage, ret);
2749  *
2750  * When @full is set, the function will not return true unless
2751  * the writer is off the reader page.
2752  *
2753  * Note: it is up to the calling functions to handle sleeps and wakeups.
2754  *  The ring buffer can be used anywhere in the kernel and can not
2755  *  blindly call wake_up. The layer that uses the ring buffer must be
2756  *  responsible for that.
2757  *
2758  * Returns:
2759  *  >=0 if data has been transferred, returns the offset of consumed data.
2760  *  <0 if no data has been transferred.
2761  */
2762 int ring_buffer_read_page(struct ring_buffer *buffer,
2763                           void **data_page, size_t len, int cpu, int full)
2764 {
2765         struct ring_buffer_per_cpu *cpu_buffer = buffer->buffers[cpu];
2766         struct ring_buffer_event *event;
2767         struct buffer_data_page *bpage;
2768         struct buffer_page *reader;
2769         unsigned long flags;
2770         unsigned int commit;
2771         unsigned int read;
2772         u64 save_timestamp;
2773         int ret = -1;
2774
2775         if (!cpumask_test_cpu(cpu, buffer->cpumask))
2776                 goto out;
2777
2778         /*
2779          * If len is not big enough to hold the page header, then
2780          * we can not copy anything.
2781          */
2782         if (len <= BUF_PAGE_HDR_SIZE)
2783                 goto out;
2784
2785         len -= BUF_PAGE_HDR_SIZE;
2786
2787         if (!data_page)
2788                 goto out;
2789
2790         bpage = *data_page;
2791         if (!bpage)
2792                 goto out;
2793
2794         spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
2795
2796         reader = rb_get_reader_page(cpu_buffer);
2797         if (!reader)
2798                 goto out_unlock;
2799
2800         event = rb_reader_event(cpu_buffer);
2801
2802         read = reader->read;
2803         commit = rb_page_commit(reader);
2804
2805         /*
2806          * If this page has been partially read or
2807          * if len is not big enough to read the rest of the page or
2808          * a writer is still on the page, then
2809          * we must copy the data from the page to the buffer.
2810          * Otherwise, we can simply swap the page with the one passed in.
2811          */
2812         if (read || (len < (commit - read)) ||
2813             cpu_buffer->reader_page == cpu_buffer->commit_page) {
2814                 struct buffer_data_page *rpage = cpu_buffer->reader_page->page;
2815                 unsigned int rpos = read;
2816                 unsigned int pos = 0;
2817                 unsigned int size;
2818
2819                 if (full)
2820                         goto out_unlock;
2821
2822                 if (len > (commit - read))
2823                         len = (commit - read);
2824
2825                 size = rb_event_length(event);
2826
2827                 if (len < size)
2828                         goto out_unlock;
2829
2830                 /* save the current timestamp, since the user will need it */
2831                 save_timestamp = cpu_buffer->read_stamp;
2832
2833                 /* Need to copy one event at a time */
2834                 do {
2835                         memcpy(bpage->data + pos, rpage->data + rpos, size);
2836
2837                         len -= size;
2838
2839                         rb_advance_reader(cpu_buffer);
2840                         rpos = reader->read;
2841                         pos += size;
2842
2843                         event = rb_reader_event(cpu_buffer);
2844                         size = rb_event_length(event);
2845                 } while (len > size);
2846
2847                 /* update bpage */
2848                 local_set(&bpage->commit, pos);
2849                 bpage->time_stamp = save_timestamp;
2850
2851                 /* we copied everything to the beginning */
2852                 read = 0;
2853         } else {
2854                 /* swap the pages */
2855                 rb_init_page(bpage);
2856                 bpage = reader->page;
2857                 reader->page = *data_page;
2858                 local_set(&reader->write, 0);
2859                 reader->read = 0;
2860                 *data_page = bpage;
2861
2862                 /* update the entry counter */
2863                 rb_remove_entries(cpu_buffer, bpage, read);
2864         }
2865         ret = read;
2866
2867  out_unlock:
2868         spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
2869
2870  out:
2871         return ret;
2872 }
2873
2874 static ssize_t
2875 rb_simple_read(struct file *filp, char __user *ubuf,
2876                size_t cnt, loff_t *ppos)
2877 {
2878         unsigned long *p = filp->private_data;
2879         char buf[64];
2880         int r;
2881
2882         if (test_bit(RB_BUFFERS_DISABLED_BIT, p))
2883                 r = sprintf(buf, "permanently disabled\n");
2884         else
2885                 r = sprintf(buf, "%d\n", test_bit(RB_BUFFERS_ON_BIT, p));
2886
2887         return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2888 }
2889
2890 static ssize_t
2891 rb_simple_write(struct file *filp, const char __user *ubuf,
2892                 size_t cnt, loff_t *ppos)
2893 {
2894         unsigned long *p = filp->private_data;
2895         char buf[64];
2896         unsigned long val;
2897         int ret;
2898
2899         if (cnt >= sizeof(buf))
2900                 return -EINVAL;
2901
2902         if (copy_from_user(&buf, ubuf, cnt))
2903                 return -EFAULT;
2904
2905         buf[cnt] = 0;
2906
2907         ret = strict_strtoul(buf, 10, &val);
2908         if (ret < 0)
2909                 return ret;
2910
2911         if (val)
2912                 set_bit(RB_BUFFERS_ON_BIT, p);
2913         else
2914                 clear_bit(RB_BUFFERS_ON_BIT, p);
2915
2916         (*ppos)++;
2917
2918         return cnt;
2919 }
2920
2921 static const struct file_operations rb_simple_fops = {
2922         .open           = tracing_open_generic,
2923         .read           = rb_simple_read,
2924         .write          = rb_simple_write,
2925 };
2926
2927
2928 static __init int rb_init_debugfs(void)
2929 {
2930         struct dentry *d_tracer;
2931
2932         d_tracer = tracing_init_dentry();
2933
2934         trace_create_file("tracing_on", 0644, d_tracer,
2935                             &ring_buffer_flags, &rb_simple_fops);
2936
2937         return 0;
2938 }
2939
2940 fs_initcall(rb_init_debugfs);
2941
2942 #ifdef CONFIG_HOTPLUG_CPU
2943 static int rb_cpu_notify(struct notifier_block *self,
2944                          unsigned long action, void *hcpu)
2945 {
2946         struct ring_buffer *buffer =
2947                 container_of(self, struct ring_buffer, cpu_notify);
2948         long cpu = (long)hcpu;
2949
2950         switch (action) {
2951         case CPU_UP_PREPARE:
2952         case CPU_UP_PREPARE_FROZEN:
2953                 if (cpu_isset(cpu, *buffer->cpumask))
2954                         return NOTIFY_OK;
2955
2956                 buffer->buffers[cpu] =
2957                         rb_allocate_cpu_buffer(buffer, cpu);
2958                 if (!buffer->buffers[cpu]) {
2959                         WARN(1, "failed to allocate ring buffer on CPU %ld\n",
2960                              cpu);
2961                         return NOTIFY_OK;
2962                 }
2963                 smp_wmb();
2964                 cpu_set(cpu, *buffer->cpumask);
2965                 break;
2966         case CPU_DOWN_PREPARE:
2967         case CPU_DOWN_PREPARE_FROZEN:
2968                 /*
2969                  * Do nothing.
2970                  *  If we were to free the buffer, then the user would
2971                  *  lose any trace that was in the buffer.
2972                  */
2973                 break;
2974         default:
2975                 break;
2976         }
2977         return NOTIFY_OK;
2978 }
2979 #endif