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