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