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