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