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