4 * Copyright (C) 2008 Steven Rostedt <srostedt@redhat.com>
6 #include <linux/ring_buffer.h>
7 #include <linux/spinlock.h>
8 #include <linux/debugfs.h>
9 #include <linux/uaccess.h>
10 #include <linux/module.h>
11 #include <linux/percpu.h>
12 #include <linux/mutex.h>
13 #include <linux/sched.h> /* used for sched_clock() (for now) */
14 #include <linux/init.h>
15 #include <linux/hash.h>
16 #include <linux/list.h>
22 * A fast way to enable or disable all ring buffers is to
23 * call tracing_on or tracing_off. Turning off the ring buffers
24 * prevents all ring buffers from being recorded to.
25 * Turning this switch on, makes it OK to write to the
26 * ring buffer, if the ring buffer is enabled itself.
28 * There's three layers that must be on in order to write
31 * 1) This global flag must be set.
32 * 2) The ring buffer must be enabled for recording.
33 * 3) The per cpu buffer must be enabled for recording.
35 * In case of an anomaly, this global flag has a bit set that
36 * will permantly disable all ring buffers.
40 * Global flag to disable all recording to ring buffers
41 * This has two bits: ON, DISABLED
45 * 0 0 : ring buffers are off
46 * 1 0 : ring buffers are on
47 * X 1 : ring buffers are permanently disabled
51 RB_BUFFERS_ON_BIT = 0,
52 RB_BUFFERS_DISABLED_BIT = 1,
56 RB_BUFFERS_ON = 1 << RB_BUFFERS_ON_BIT,
57 RB_BUFFERS_DISABLED = 1 << RB_BUFFERS_DISABLED_BIT,
60 static long ring_buffer_flags __read_mostly = RB_BUFFERS_ON;
63 * tracing_on - enable all tracing buffers
65 * This function enables all tracing buffers that may have been
66 * disabled with tracing_off.
70 set_bit(RB_BUFFERS_ON_BIT, &ring_buffer_flags);
72 EXPORT_SYMBOL_GPL(tracing_on);
75 * tracing_off - turn off all tracing buffers
77 * This function stops all tracing buffers from recording data.
78 * It does not disable any overhead the tracers themselves may
79 * be causing. This function simply causes all recording to
80 * the ring buffers to fail.
82 void tracing_off(void)
84 clear_bit(RB_BUFFERS_ON_BIT, &ring_buffer_flags);
86 EXPORT_SYMBOL_GPL(tracing_off);
89 * tracing_off_permanent - permanently disable ring buffers
91 * This function, once called, will disable all ring buffers
94 void tracing_off_permanent(void)
96 set_bit(RB_BUFFERS_DISABLED_BIT, &ring_buffer_flags);
101 /* Up this if you want to test the TIME_EXTENTS and normalization */
102 #define DEBUG_SHIFT 0
105 u64 ring_buffer_time_stamp(int cpu)
109 preempt_disable_notrace();
110 /* shift to debug/test normalization and TIME_EXTENTS */
111 time = sched_clock() << DEBUG_SHIFT;
112 preempt_enable_no_resched_notrace();
116 EXPORT_SYMBOL_GPL(ring_buffer_time_stamp);
118 void ring_buffer_normalize_time_stamp(int cpu, u64 *ts)
120 /* Just stupid testing the normalize function and deltas */
123 EXPORT_SYMBOL_GPL(ring_buffer_normalize_time_stamp);
125 #define RB_EVNT_HDR_SIZE (sizeof(struct ring_buffer_event))
126 #define RB_ALIGNMENT_SHIFT 2
127 #define RB_ALIGNMENT (1 << RB_ALIGNMENT_SHIFT)
128 #define RB_MAX_SMALL_DATA 28
131 RB_LEN_TIME_EXTEND = 8,
132 RB_LEN_TIME_STAMP = 16,
135 /* inline for ring buffer fast paths */
136 static inline unsigned
137 rb_event_length(struct ring_buffer_event *event)
141 switch (event->type) {
142 case RINGBUF_TYPE_PADDING:
146 case RINGBUF_TYPE_TIME_EXTEND:
147 return RB_LEN_TIME_EXTEND;
149 case RINGBUF_TYPE_TIME_STAMP:
150 return RB_LEN_TIME_STAMP;
152 case RINGBUF_TYPE_DATA:
154 length = event->len << RB_ALIGNMENT_SHIFT;
156 length = event->array[0];
157 return length + RB_EVNT_HDR_SIZE;
166 * ring_buffer_event_length - return the length of the event
167 * @event: the event to get the length of
169 unsigned ring_buffer_event_length(struct ring_buffer_event *event)
171 return rb_event_length(event);
173 EXPORT_SYMBOL_GPL(ring_buffer_event_length);
175 /* inline for ring buffer fast paths */
177 rb_event_data(struct ring_buffer_event *event)
179 BUG_ON(event->type != RINGBUF_TYPE_DATA);
180 /* If length is in len field, then array[0] has the data */
182 return (void *)&event->array[0];
183 /* Otherwise length is in array[0] and array[1] has the data */
184 return (void *)&event->array[1];
188 * ring_buffer_event_data - return the data of the event
189 * @event: the event to get the data from
191 void *ring_buffer_event_data(struct ring_buffer_event *event)
193 return rb_event_data(event);
195 EXPORT_SYMBOL_GPL(ring_buffer_event_data);
197 #define for_each_buffer_cpu(buffer, cpu) \
198 for_each_cpu_mask(cpu, buffer->cpumask)
201 #define TS_MASK ((1ULL << TS_SHIFT) - 1)
202 #define TS_DELTA_TEST (~TS_MASK)
204 struct buffer_data_page {
205 u64 time_stamp; /* page time stamp */
206 local_t commit; /* write commited index */
207 unsigned char data[]; /* data of buffer page */
211 local_t write; /* index for next write */
212 unsigned read; /* index for next read */
213 struct list_head list; /* list of free pages */
214 struct buffer_data_page *page; /* Actual data page */
217 static void rb_init_page(struct buffer_data_page *bpage)
219 local_set(&bpage->commit, 0);
223 * Also stolen from mm/slob.c. Thanks to Mathieu Desnoyers for pointing
226 static inline void free_buffer_page(struct buffer_page *bpage)
229 free_page((unsigned long)bpage->page);
234 * We need to fit the time_stamp delta into 27 bits.
236 static inline int test_time_stamp(u64 delta)
238 if (delta & TS_DELTA_TEST)
243 #define BUF_PAGE_SIZE (PAGE_SIZE - sizeof(struct buffer_data_page))
246 * head_page == tail_page && head == tail then buffer is empty.
248 struct ring_buffer_per_cpu {
250 struct ring_buffer *buffer;
251 spinlock_t reader_lock; /* serialize readers */
253 struct lock_class_key lock_key;
254 struct list_head pages;
255 struct buffer_page *head_page; /* read from head */
256 struct buffer_page *tail_page; /* write to tail */
257 struct buffer_page *commit_page; /* commited pages */
258 struct buffer_page *reader_page;
259 unsigned long overrun;
260 unsigned long entries;
263 atomic_t record_disabled;
271 atomic_t record_disabled;
275 struct ring_buffer_per_cpu **buffers;
278 struct ring_buffer_iter {
279 struct ring_buffer_per_cpu *cpu_buffer;
281 struct buffer_page *head_page;
285 /* buffer may be either ring_buffer or ring_buffer_per_cpu */
286 #define RB_WARN_ON(buffer, cond) \
288 int _____ret = unlikely(cond); \
290 atomic_inc(&buffer->record_disabled); \
297 * check_pages - integrity check of buffer pages
298 * @cpu_buffer: CPU buffer with pages to test
300 * As a safty measure we check to make sure the data pages have not
303 static int rb_check_pages(struct ring_buffer_per_cpu *cpu_buffer)
305 struct list_head *head = &cpu_buffer->pages;
306 struct buffer_page *bpage, *tmp;
308 if (RB_WARN_ON(cpu_buffer, head->next->prev != head))
310 if (RB_WARN_ON(cpu_buffer, head->prev->next != head))
313 list_for_each_entry_safe(bpage, tmp, head, list) {
314 if (RB_WARN_ON(cpu_buffer,
315 bpage->list.next->prev != &bpage->list))
317 if (RB_WARN_ON(cpu_buffer,
318 bpage->list.prev->next != &bpage->list))
325 static int rb_allocate_pages(struct ring_buffer_per_cpu *cpu_buffer,
328 struct list_head *head = &cpu_buffer->pages;
329 struct buffer_page *bpage, *tmp;
334 for (i = 0; i < nr_pages; i++) {
335 bpage = kzalloc_node(ALIGN(sizeof(*bpage), cache_line_size()),
336 GFP_KERNEL, cpu_to_node(cpu_buffer->cpu));
339 list_add(&bpage->list, &pages);
341 addr = __get_free_page(GFP_KERNEL);
344 bpage->page = (void *)addr;
345 rb_init_page(bpage->page);
348 list_splice(&pages, head);
350 rb_check_pages(cpu_buffer);
355 list_for_each_entry_safe(bpage, tmp, &pages, list) {
356 list_del_init(&bpage->list);
357 free_buffer_page(bpage);
362 static struct ring_buffer_per_cpu *
363 rb_allocate_cpu_buffer(struct ring_buffer *buffer, int cpu)
365 struct ring_buffer_per_cpu *cpu_buffer;
366 struct buffer_page *bpage;
370 cpu_buffer = kzalloc_node(ALIGN(sizeof(*cpu_buffer), cache_line_size()),
371 GFP_KERNEL, cpu_to_node(cpu));
375 cpu_buffer->cpu = cpu;
376 cpu_buffer->buffer = buffer;
377 spin_lock_init(&cpu_buffer->reader_lock);
378 cpu_buffer->lock = (raw_spinlock_t)__RAW_SPIN_LOCK_UNLOCKED;
379 INIT_LIST_HEAD(&cpu_buffer->pages);
381 bpage = kzalloc_node(ALIGN(sizeof(*bpage), cache_line_size()),
382 GFP_KERNEL, cpu_to_node(cpu));
384 goto fail_free_buffer;
386 cpu_buffer->reader_page = bpage;
387 addr = __get_free_page(GFP_KERNEL);
389 goto fail_free_reader;
390 bpage->page = (void *)addr;
391 rb_init_page(bpage->page);
393 INIT_LIST_HEAD(&cpu_buffer->reader_page->list);
395 ret = rb_allocate_pages(cpu_buffer, buffer->pages);
397 goto fail_free_reader;
399 cpu_buffer->head_page
400 = list_entry(cpu_buffer->pages.next, struct buffer_page, list);
401 cpu_buffer->tail_page = cpu_buffer->commit_page = cpu_buffer->head_page;
406 free_buffer_page(cpu_buffer->reader_page);
413 static void rb_free_cpu_buffer(struct ring_buffer_per_cpu *cpu_buffer)
415 struct list_head *head = &cpu_buffer->pages;
416 struct buffer_page *bpage, *tmp;
418 list_del_init(&cpu_buffer->reader_page->list);
419 free_buffer_page(cpu_buffer->reader_page);
421 list_for_each_entry_safe(bpage, tmp, head, list) {
422 list_del_init(&bpage->list);
423 free_buffer_page(bpage);
429 * Causes compile errors if the struct buffer_page gets bigger
430 * than the struct page.
432 extern int ring_buffer_page_too_big(void);
435 * ring_buffer_alloc - allocate a new ring_buffer
436 * @size: the size in bytes per cpu that is needed.
437 * @flags: attributes to set for the ring buffer.
439 * Currently the only flag that is available is the RB_FL_OVERWRITE
440 * flag. This flag means that the buffer will overwrite old data
441 * when the buffer wraps. If this flag is not set, the buffer will
442 * drop data when the tail hits the head.
444 struct ring_buffer *ring_buffer_alloc(unsigned long size, unsigned flags)
446 struct ring_buffer *buffer;
450 /* Paranoid! Optimizes out when all is well */
451 if (sizeof(struct buffer_page) > sizeof(struct page))
452 ring_buffer_page_too_big();
455 /* keep it in its own cache line */
456 buffer = kzalloc(ALIGN(sizeof(*buffer), cache_line_size()),
461 buffer->pages = DIV_ROUND_UP(size, BUF_PAGE_SIZE);
462 buffer->flags = flags;
464 /* need at least two pages */
465 if (buffer->pages == 1)
468 buffer->cpumask = cpu_possible_map;
469 buffer->cpus = nr_cpu_ids;
471 bsize = sizeof(void *) * nr_cpu_ids;
472 buffer->buffers = kzalloc(ALIGN(bsize, cache_line_size()),
474 if (!buffer->buffers)
475 goto fail_free_buffer;
477 for_each_buffer_cpu(buffer, cpu) {
478 buffer->buffers[cpu] =
479 rb_allocate_cpu_buffer(buffer, cpu);
480 if (!buffer->buffers[cpu])
481 goto fail_free_buffers;
484 mutex_init(&buffer->mutex);
489 for_each_buffer_cpu(buffer, cpu) {
490 if (buffer->buffers[cpu])
491 rb_free_cpu_buffer(buffer->buffers[cpu]);
493 kfree(buffer->buffers);
499 EXPORT_SYMBOL_GPL(ring_buffer_alloc);
502 * ring_buffer_free - free a ring buffer.
503 * @buffer: the buffer to free.
506 ring_buffer_free(struct ring_buffer *buffer)
510 for_each_buffer_cpu(buffer, cpu)
511 rb_free_cpu_buffer(buffer->buffers[cpu]);
515 EXPORT_SYMBOL_GPL(ring_buffer_free);
517 static void rb_reset_cpu(struct ring_buffer_per_cpu *cpu_buffer);
520 rb_remove_pages(struct ring_buffer_per_cpu *cpu_buffer, unsigned nr_pages)
522 struct buffer_page *bpage;
526 atomic_inc(&cpu_buffer->record_disabled);
529 for (i = 0; i < nr_pages; i++) {
530 if (RB_WARN_ON(cpu_buffer, list_empty(&cpu_buffer->pages)))
532 p = cpu_buffer->pages.next;
533 bpage = list_entry(p, struct buffer_page, list);
534 list_del_init(&bpage->list);
535 free_buffer_page(bpage);
537 if (RB_WARN_ON(cpu_buffer, list_empty(&cpu_buffer->pages)))
540 rb_reset_cpu(cpu_buffer);
542 rb_check_pages(cpu_buffer);
544 atomic_dec(&cpu_buffer->record_disabled);
549 rb_insert_pages(struct ring_buffer_per_cpu *cpu_buffer,
550 struct list_head *pages, unsigned nr_pages)
552 struct buffer_page *bpage;
556 atomic_inc(&cpu_buffer->record_disabled);
559 for (i = 0; i < nr_pages; i++) {
560 if (RB_WARN_ON(cpu_buffer, list_empty(pages)))
563 bpage = list_entry(p, struct buffer_page, list);
564 list_del_init(&bpage->list);
565 list_add_tail(&bpage->list, &cpu_buffer->pages);
567 rb_reset_cpu(cpu_buffer);
569 rb_check_pages(cpu_buffer);
571 atomic_dec(&cpu_buffer->record_disabled);
575 * ring_buffer_resize - resize the ring buffer
576 * @buffer: the buffer to resize.
577 * @size: the new size.
579 * The tracer is responsible for making sure that the buffer is
580 * not being used while changing the size.
581 * Note: We may be able to change the above requirement by using
582 * RCU synchronizations.
584 * Minimum size is 2 * BUF_PAGE_SIZE.
586 * Returns -1 on failure.
588 int ring_buffer_resize(struct ring_buffer *buffer, unsigned long size)
590 struct ring_buffer_per_cpu *cpu_buffer;
591 unsigned nr_pages, rm_pages, new_pages;
592 struct buffer_page *bpage, *tmp;
593 unsigned long buffer_size;
599 * Always succeed at resizing a non-existent buffer:
604 size = DIV_ROUND_UP(size, BUF_PAGE_SIZE);
605 size *= BUF_PAGE_SIZE;
606 buffer_size = buffer->pages * BUF_PAGE_SIZE;
608 /* we need a minimum of two pages */
609 if (size < BUF_PAGE_SIZE * 2)
610 size = BUF_PAGE_SIZE * 2;
612 if (size == buffer_size)
615 mutex_lock(&buffer->mutex);
617 nr_pages = DIV_ROUND_UP(size, BUF_PAGE_SIZE);
619 if (size < buffer_size) {
621 /* easy case, just free pages */
622 if (RB_WARN_ON(buffer, nr_pages >= buffer->pages)) {
623 mutex_unlock(&buffer->mutex);
627 rm_pages = buffer->pages - nr_pages;
629 for_each_buffer_cpu(buffer, cpu) {
630 cpu_buffer = buffer->buffers[cpu];
631 rb_remove_pages(cpu_buffer, rm_pages);
637 * This is a bit more difficult. We only want to add pages
638 * when we can allocate enough for all CPUs. We do this
639 * by allocating all the pages and storing them on a local
640 * link list. If we succeed in our allocation, then we
641 * add these pages to the cpu_buffers. Otherwise we just free
642 * them all and return -ENOMEM;
644 if (RB_WARN_ON(buffer, nr_pages <= buffer->pages)) {
645 mutex_unlock(&buffer->mutex);
649 new_pages = nr_pages - buffer->pages;
651 for_each_buffer_cpu(buffer, cpu) {
652 for (i = 0; i < new_pages; i++) {
653 bpage = kzalloc_node(ALIGN(sizeof(*bpage),
655 GFP_KERNEL, cpu_to_node(cpu));
658 list_add(&bpage->list, &pages);
659 addr = __get_free_page(GFP_KERNEL);
662 bpage->page = (void *)addr;
663 rb_init_page(bpage->page);
667 for_each_buffer_cpu(buffer, cpu) {
668 cpu_buffer = buffer->buffers[cpu];
669 rb_insert_pages(cpu_buffer, &pages, new_pages);
672 if (RB_WARN_ON(buffer, !list_empty(&pages))) {
673 mutex_unlock(&buffer->mutex);
678 buffer->pages = nr_pages;
679 mutex_unlock(&buffer->mutex);
684 list_for_each_entry_safe(bpage, tmp, &pages, list) {
685 list_del_init(&bpage->list);
686 free_buffer_page(bpage);
688 mutex_unlock(&buffer->mutex);
691 EXPORT_SYMBOL_GPL(ring_buffer_resize);
693 static inline int rb_null_event(struct ring_buffer_event *event)
695 return event->type == RINGBUF_TYPE_PADDING;
699 __rb_data_page_index(struct buffer_data_page *bpage, unsigned index)
701 return bpage->data + index;
704 static inline void *__rb_page_index(struct buffer_page *bpage, unsigned index)
706 return bpage->page->data + index;
709 static inline struct ring_buffer_event *
710 rb_reader_event(struct ring_buffer_per_cpu *cpu_buffer)
712 return __rb_page_index(cpu_buffer->reader_page,
713 cpu_buffer->reader_page->read);
716 static inline struct ring_buffer_event *
717 rb_head_event(struct ring_buffer_per_cpu *cpu_buffer)
719 return __rb_page_index(cpu_buffer->head_page,
720 cpu_buffer->head_page->read);
723 static inline struct ring_buffer_event *
724 rb_iter_head_event(struct ring_buffer_iter *iter)
726 return __rb_page_index(iter->head_page, iter->head);
729 static inline unsigned rb_page_write(struct buffer_page *bpage)
731 return local_read(&bpage->write);
734 static inline unsigned rb_page_commit(struct buffer_page *bpage)
736 return local_read(&bpage->page->commit);
739 /* Size is determined by what has been commited */
740 static inline unsigned rb_page_size(struct buffer_page *bpage)
742 return rb_page_commit(bpage);
745 static inline unsigned
746 rb_commit_index(struct ring_buffer_per_cpu *cpu_buffer)
748 return rb_page_commit(cpu_buffer->commit_page);
751 static inline unsigned rb_head_size(struct ring_buffer_per_cpu *cpu_buffer)
753 return rb_page_commit(cpu_buffer->head_page);
757 * When the tail hits the head and the buffer is in overwrite mode,
758 * the head jumps to the next page and all content on the previous
759 * page is discarded. But before doing so, we update the overrun
760 * variable of the buffer.
762 static void rb_update_overflow(struct ring_buffer_per_cpu *cpu_buffer)
764 struct ring_buffer_event *event;
767 for (head = 0; head < rb_head_size(cpu_buffer);
768 head += rb_event_length(event)) {
770 event = __rb_page_index(cpu_buffer->head_page, head);
771 if (RB_WARN_ON(cpu_buffer, rb_null_event(event)))
773 /* Only count data entries */
774 if (event->type != RINGBUF_TYPE_DATA)
776 cpu_buffer->overrun++;
777 cpu_buffer->entries--;
781 static inline void rb_inc_page(struct ring_buffer_per_cpu *cpu_buffer,
782 struct buffer_page **bpage)
784 struct list_head *p = (*bpage)->list.next;
786 if (p == &cpu_buffer->pages)
789 *bpage = list_entry(p, struct buffer_page, list);
792 static inline unsigned
793 rb_event_index(struct ring_buffer_event *event)
795 unsigned long addr = (unsigned long)event;
797 return (addr & ~PAGE_MASK) - (PAGE_SIZE - BUF_PAGE_SIZE);
801 rb_is_commit(struct ring_buffer_per_cpu *cpu_buffer,
802 struct ring_buffer_event *event)
804 unsigned long addr = (unsigned long)event;
807 index = rb_event_index(event);
810 return cpu_buffer->commit_page->page == (void *)addr &&
811 rb_commit_index(cpu_buffer) == index;
815 rb_set_commit_event(struct ring_buffer_per_cpu *cpu_buffer,
816 struct ring_buffer_event *event)
818 unsigned long addr = (unsigned long)event;
821 index = rb_event_index(event);
824 while (cpu_buffer->commit_page->page != (void *)addr) {
825 if (RB_WARN_ON(cpu_buffer,
826 cpu_buffer->commit_page == cpu_buffer->tail_page))
828 cpu_buffer->commit_page->page->commit =
829 cpu_buffer->commit_page->write;
830 rb_inc_page(cpu_buffer, &cpu_buffer->commit_page);
831 cpu_buffer->write_stamp =
832 cpu_buffer->commit_page->page->time_stamp;
835 /* Now set the commit to the event's index */
836 local_set(&cpu_buffer->commit_page->page->commit, index);
840 rb_set_commit_to_write(struct ring_buffer_per_cpu *cpu_buffer)
843 * We only race with interrupts and NMIs on this CPU.
844 * If we own the commit event, then we can commit
845 * all others that interrupted us, since the interruptions
846 * are in stack format (they finish before they come
847 * back to us). This allows us to do a simple loop to
848 * assign the commit to the tail.
851 while (cpu_buffer->commit_page != cpu_buffer->tail_page) {
852 cpu_buffer->commit_page->page->commit =
853 cpu_buffer->commit_page->write;
854 rb_inc_page(cpu_buffer, &cpu_buffer->commit_page);
855 cpu_buffer->write_stamp =
856 cpu_buffer->commit_page->page->time_stamp;
857 /* add barrier to keep gcc from optimizing too much */
860 while (rb_commit_index(cpu_buffer) !=
861 rb_page_write(cpu_buffer->commit_page)) {
862 cpu_buffer->commit_page->page->commit =
863 cpu_buffer->commit_page->write;
867 /* again, keep gcc from optimizing */
871 * If an interrupt came in just after the first while loop
872 * and pushed the tail page forward, we will be left with
873 * a dangling commit that will never go forward.
875 if (unlikely(cpu_buffer->commit_page != cpu_buffer->tail_page))
879 static void rb_reset_reader_page(struct ring_buffer_per_cpu *cpu_buffer)
881 cpu_buffer->read_stamp = cpu_buffer->reader_page->page->time_stamp;
882 cpu_buffer->reader_page->read = 0;
885 static inline void rb_inc_iter(struct ring_buffer_iter *iter)
887 struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
890 * The iterator could be on the reader page (it starts there).
891 * But the head could have moved, since the reader was
892 * found. Check for this case and assign the iterator
893 * to the head page instead of next.
895 if (iter->head_page == cpu_buffer->reader_page)
896 iter->head_page = cpu_buffer->head_page;
898 rb_inc_page(cpu_buffer, &iter->head_page);
900 iter->read_stamp = iter->head_page->page->time_stamp;
905 * ring_buffer_update_event - update event type and data
906 * @event: the even to update
907 * @type: the type of event
908 * @length: the size of the event field in the ring buffer
910 * Update the type and data fields of the event. The length
911 * is the actual size that is written to the ring buffer,
912 * and with this, we can determine what to place into the
916 rb_update_event(struct ring_buffer_event *event,
917 unsigned type, unsigned length)
923 case RINGBUF_TYPE_PADDING:
926 case RINGBUF_TYPE_TIME_EXTEND:
928 (RB_LEN_TIME_EXTEND + (RB_ALIGNMENT-1))
929 >> RB_ALIGNMENT_SHIFT;
932 case RINGBUF_TYPE_TIME_STAMP:
934 (RB_LEN_TIME_STAMP + (RB_ALIGNMENT-1))
935 >> RB_ALIGNMENT_SHIFT;
938 case RINGBUF_TYPE_DATA:
939 length -= RB_EVNT_HDR_SIZE;
940 if (length > RB_MAX_SMALL_DATA) {
942 event->array[0] = length;
945 (length + (RB_ALIGNMENT-1))
946 >> RB_ALIGNMENT_SHIFT;
953 static inline unsigned rb_calculate_event_length(unsigned length)
955 struct ring_buffer_event event; /* Used only for sizeof array */
957 /* zero length can cause confusions */
961 if (length > RB_MAX_SMALL_DATA)
962 length += sizeof(event.array[0]);
964 length += RB_EVNT_HDR_SIZE;
965 length = ALIGN(length, RB_ALIGNMENT);
970 static struct ring_buffer_event *
971 __rb_reserve_next(struct ring_buffer_per_cpu *cpu_buffer,
972 unsigned type, unsigned long length, u64 *ts)
974 struct buffer_page *tail_page, *head_page, *reader_page, *commit_page;
975 unsigned long tail, write;
976 struct ring_buffer *buffer = cpu_buffer->buffer;
977 struct ring_buffer_event *event;
980 commit_page = cpu_buffer->commit_page;
981 /* we just need to protect against interrupts */
983 tail_page = cpu_buffer->tail_page;
984 write = local_add_return(length, &tail_page->write);
985 tail = write - length;
987 /* See if we shot pass the end of this buffer page */
988 if (write > BUF_PAGE_SIZE) {
989 struct buffer_page *next_page = tail_page;
991 local_irq_save(flags);
992 __raw_spin_lock(&cpu_buffer->lock);
994 rb_inc_page(cpu_buffer, &next_page);
996 head_page = cpu_buffer->head_page;
997 reader_page = cpu_buffer->reader_page;
999 /* we grabbed the lock before incrementing */
1000 if (RB_WARN_ON(cpu_buffer, next_page == reader_page))
1004 * If for some reason, we had an interrupt storm that made
1005 * it all the way around the buffer, bail, and warn
1008 if (unlikely(next_page == commit_page)) {
1013 if (next_page == head_page) {
1014 if (!(buffer->flags & RB_FL_OVERWRITE)) {
1016 if (tail <= BUF_PAGE_SIZE)
1017 local_set(&tail_page->write, tail);
1021 /* tail_page has not moved yet? */
1022 if (tail_page == cpu_buffer->tail_page) {
1023 /* count overflows */
1024 rb_update_overflow(cpu_buffer);
1026 rb_inc_page(cpu_buffer, &head_page);
1027 cpu_buffer->head_page = head_page;
1028 cpu_buffer->head_page->read = 0;
1033 * If the tail page is still the same as what we think
1034 * it is, then it is up to us to update the tail
1037 if (tail_page == cpu_buffer->tail_page) {
1038 local_set(&next_page->write, 0);
1039 local_set(&next_page->page->commit, 0);
1040 cpu_buffer->tail_page = next_page;
1042 /* reread the time stamp */
1043 *ts = ring_buffer_time_stamp(cpu_buffer->cpu);
1044 cpu_buffer->tail_page->page->time_stamp = *ts;
1048 * The actual tail page has moved forward.
1050 if (tail < BUF_PAGE_SIZE) {
1051 /* Mark the rest of the page with padding */
1052 event = __rb_page_index(tail_page, tail);
1053 event->type = RINGBUF_TYPE_PADDING;
1056 if (tail <= BUF_PAGE_SIZE)
1057 /* Set the write back to the previous setting */
1058 local_set(&tail_page->write, tail);
1061 * If this was a commit entry that failed,
1062 * increment that too
1064 if (tail_page == cpu_buffer->commit_page &&
1065 tail == rb_commit_index(cpu_buffer)) {
1066 rb_set_commit_to_write(cpu_buffer);
1069 __raw_spin_unlock(&cpu_buffer->lock);
1070 local_irq_restore(flags);
1072 /* fail and let the caller try again */
1073 return ERR_PTR(-EAGAIN);
1076 /* We reserved something on the buffer */
1078 if (RB_WARN_ON(cpu_buffer, write > BUF_PAGE_SIZE))
1081 event = __rb_page_index(tail_page, tail);
1082 rb_update_event(event, type, length);
1085 * If this is a commit and the tail is zero, then update
1086 * this page's time stamp.
1088 if (!tail && rb_is_commit(cpu_buffer, event))
1089 cpu_buffer->commit_page->page->time_stamp = *ts;
1094 __raw_spin_unlock(&cpu_buffer->lock);
1095 local_irq_restore(flags);
1100 rb_add_time_stamp(struct ring_buffer_per_cpu *cpu_buffer,
1101 u64 *ts, u64 *delta)
1103 struct ring_buffer_event *event;
1107 if (unlikely(*delta > (1ULL << 59) && !once++)) {
1108 printk(KERN_WARNING "Delta way too big! %llu"
1109 " ts=%llu write stamp = %llu\n",
1110 (unsigned long long)*delta,
1111 (unsigned long long)*ts,
1112 (unsigned long long)cpu_buffer->write_stamp);
1117 * The delta is too big, we to add a
1120 event = __rb_reserve_next(cpu_buffer,
1121 RINGBUF_TYPE_TIME_EXTEND,
1127 if (PTR_ERR(event) == -EAGAIN)
1130 /* Only a commited time event can update the write stamp */
1131 if (rb_is_commit(cpu_buffer, event)) {
1133 * If this is the first on the page, then we need to
1134 * update the page itself, and just put in a zero.
1136 if (rb_event_index(event)) {
1137 event->time_delta = *delta & TS_MASK;
1138 event->array[0] = *delta >> TS_SHIFT;
1140 cpu_buffer->commit_page->page->time_stamp = *ts;
1141 event->time_delta = 0;
1142 event->array[0] = 0;
1144 cpu_buffer->write_stamp = *ts;
1145 /* let the caller know this was the commit */
1148 /* Darn, this is just wasted space */
1149 event->time_delta = 0;
1150 event->array[0] = 0;
1159 static struct ring_buffer_event *
1160 rb_reserve_next_event(struct ring_buffer_per_cpu *cpu_buffer,
1161 unsigned type, unsigned long length)
1163 struct ring_buffer_event *event;
1170 * We allow for interrupts to reenter here and do a trace.
1171 * If one does, it will cause this original code to loop
1172 * back here. Even with heavy interrupts happening, this
1173 * should only happen a few times in a row. If this happens
1174 * 1000 times in a row, there must be either an interrupt
1175 * storm or we have something buggy.
1178 if (RB_WARN_ON(cpu_buffer, ++nr_loops > 1000))
1181 ts = ring_buffer_time_stamp(cpu_buffer->cpu);
1184 * Only the first commit can update the timestamp.
1185 * Yes there is a race here. If an interrupt comes in
1186 * just after the conditional and it traces too, then it
1187 * will also check the deltas. More than one timestamp may
1188 * also be made. But only the entry that did the actual
1189 * commit will be something other than zero.
1191 if (cpu_buffer->tail_page == cpu_buffer->commit_page &&
1192 rb_page_write(cpu_buffer->tail_page) ==
1193 rb_commit_index(cpu_buffer)) {
1195 delta = ts - cpu_buffer->write_stamp;
1197 /* make sure this delta is calculated here */
1200 /* Did the write stamp get updated already? */
1201 if (unlikely(ts < cpu_buffer->write_stamp))
1204 if (test_time_stamp(delta)) {
1206 commit = rb_add_time_stamp(cpu_buffer, &ts, &delta);
1208 if (commit == -EBUSY)
1211 if (commit == -EAGAIN)
1214 RB_WARN_ON(cpu_buffer, commit < 0);
1217 /* Non commits have zero deltas */
1220 event = __rb_reserve_next(cpu_buffer, type, length, &ts);
1221 if (PTR_ERR(event) == -EAGAIN)
1225 if (unlikely(commit))
1227 * Ouch! We needed a timestamp and it was commited. But
1228 * we didn't get our event reserved.
1230 rb_set_commit_to_write(cpu_buffer);
1235 * If the timestamp was commited, make the commit our entry
1236 * now so that we will update it when needed.
1239 rb_set_commit_event(cpu_buffer, event);
1240 else if (!rb_is_commit(cpu_buffer, event))
1243 event->time_delta = delta;
1248 static DEFINE_PER_CPU(int, rb_need_resched);
1251 * ring_buffer_lock_reserve - reserve a part of the buffer
1252 * @buffer: the ring buffer to reserve from
1253 * @length: the length of the data to reserve (excluding event header)
1254 * @flags: a pointer to save the interrupt flags
1256 * Returns a reseverd event on the ring buffer to copy directly to.
1257 * The user of this interface will need to get the body to write into
1258 * and can use the ring_buffer_event_data() interface.
1260 * The length is the length of the data needed, not the event length
1261 * which also includes the event header.
1263 * Must be paired with ring_buffer_unlock_commit, unless NULL is returned.
1264 * If NULL is returned, then nothing has been allocated or locked.
1266 struct ring_buffer_event *
1267 ring_buffer_lock_reserve(struct ring_buffer *buffer,
1268 unsigned long length,
1269 unsigned long *flags)
1271 struct ring_buffer_per_cpu *cpu_buffer;
1272 struct ring_buffer_event *event;
1275 if (ring_buffer_flags != RB_BUFFERS_ON)
1278 if (atomic_read(&buffer->record_disabled))
1281 /* If we are tracing schedule, we don't want to recurse */
1282 resched = ftrace_preempt_disable();
1284 cpu = raw_smp_processor_id();
1286 if (!cpu_isset(cpu, buffer->cpumask))
1289 cpu_buffer = buffer->buffers[cpu];
1291 if (atomic_read(&cpu_buffer->record_disabled))
1294 length = rb_calculate_event_length(length);
1295 if (length > BUF_PAGE_SIZE)
1298 event = rb_reserve_next_event(cpu_buffer, RINGBUF_TYPE_DATA, length);
1303 * Need to store resched state on this cpu.
1304 * Only the first needs to.
1307 if (preempt_count() == 1)
1308 per_cpu(rb_need_resched, cpu) = resched;
1313 ftrace_preempt_enable(resched);
1316 EXPORT_SYMBOL_GPL(ring_buffer_lock_reserve);
1318 static void rb_commit(struct ring_buffer_per_cpu *cpu_buffer,
1319 struct ring_buffer_event *event)
1321 cpu_buffer->entries++;
1323 /* Only process further if we own the commit */
1324 if (!rb_is_commit(cpu_buffer, event))
1327 cpu_buffer->write_stamp += event->time_delta;
1329 rb_set_commit_to_write(cpu_buffer);
1333 * ring_buffer_unlock_commit - commit a reserved
1334 * @buffer: The buffer to commit to
1335 * @event: The event pointer to commit.
1336 * @flags: the interrupt flags received from ring_buffer_lock_reserve.
1338 * This commits the data to the ring buffer, and releases any locks held.
1340 * Must be paired with ring_buffer_lock_reserve.
1342 int ring_buffer_unlock_commit(struct ring_buffer *buffer,
1343 struct ring_buffer_event *event,
1344 unsigned long flags)
1346 struct ring_buffer_per_cpu *cpu_buffer;
1347 int cpu = raw_smp_processor_id();
1349 cpu_buffer = buffer->buffers[cpu];
1351 rb_commit(cpu_buffer, event);
1354 * Only the last preempt count needs to restore preemption.
1356 if (preempt_count() == 1)
1357 ftrace_preempt_enable(per_cpu(rb_need_resched, cpu));
1359 preempt_enable_no_resched_notrace();
1363 EXPORT_SYMBOL_GPL(ring_buffer_unlock_commit);
1366 * ring_buffer_write - write data to the buffer without reserving
1367 * @buffer: The ring buffer to write to.
1368 * @length: The length of the data being written (excluding the event header)
1369 * @data: The data to write to the buffer.
1371 * This is like ring_buffer_lock_reserve and ring_buffer_unlock_commit as
1372 * one function. If you already have the data to write to the buffer, it
1373 * may be easier to simply call this function.
1375 * Note, like ring_buffer_lock_reserve, the length is the length of the data
1376 * and not the length of the event which would hold the header.
1378 int ring_buffer_write(struct ring_buffer *buffer,
1379 unsigned long length,
1382 struct ring_buffer_per_cpu *cpu_buffer;
1383 struct ring_buffer_event *event;
1384 unsigned long event_length;
1389 if (ring_buffer_flags != RB_BUFFERS_ON)
1392 if (atomic_read(&buffer->record_disabled))
1395 resched = ftrace_preempt_disable();
1397 cpu = raw_smp_processor_id();
1399 if (!cpu_isset(cpu, buffer->cpumask))
1402 cpu_buffer = buffer->buffers[cpu];
1404 if (atomic_read(&cpu_buffer->record_disabled))
1407 event_length = rb_calculate_event_length(length);
1408 event = rb_reserve_next_event(cpu_buffer,
1409 RINGBUF_TYPE_DATA, event_length);
1413 body = rb_event_data(event);
1415 memcpy(body, data, length);
1417 rb_commit(cpu_buffer, event);
1421 ftrace_preempt_enable(resched);
1425 EXPORT_SYMBOL_GPL(ring_buffer_write);
1427 static inline int rb_per_cpu_empty(struct ring_buffer_per_cpu *cpu_buffer)
1429 struct buffer_page *reader = cpu_buffer->reader_page;
1430 struct buffer_page *head = cpu_buffer->head_page;
1431 struct buffer_page *commit = cpu_buffer->commit_page;
1433 return reader->read == rb_page_commit(reader) &&
1434 (commit == reader ||
1436 head->read == rb_page_commit(commit)));
1440 * ring_buffer_record_disable - stop all writes into the buffer
1441 * @buffer: The ring buffer to stop writes to.
1443 * This prevents all writes to the buffer. Any attempt to write
1444 * to the buffer after this will fail and return NULL.
1446 * The caller should call synchronize_sched() after this.
1448 void ring_buffer_record_disable(struct ring_buffer *buffer)
1450 atomic_inc(&buffer->record_disabled);
1452 EXPORT_SYMBOL_GPL(ring_buffer_record_disable);
1455 * ring_buffer_record_enable - enable writes to the buffer
1456 * @buffer: The ring buffer to enable writes
1458 * Note, multiple disables will need the same number of enables
1459 * to truely enable the writing (much like preempt_disable).
1461 void ring_buffer_record_enable(struct ring_buffer *buffer)
1463 atomic_dec(&buffer->record_disabled);
1465 EXPORT_SYMBOL_GPL(ring_buffer_record_enable);
1468 * ring_buffer_record_disable_cpu - stop all writes into the cpu_buffer
1469 * @buffer: The ring buffer to stop writes to.
1470 * @cpu: The CPU buffer to stop
1472 * This prevents all writes to the buffer. Any attempt to write
1473 * to the buffer after this will fail and return NULL.
1475 * The caller should call synchronize_sched() after this.
1477 void ring_buffer_record_disable_cpu(struct ring_buffer *buffer, int cpu)
1479 struct ring_buffer_per_cpu *cpu_buffer;
1481 if (!cpu_isset(cpu, buffer->cpumask))
1484 cpu_buffer = buffer->buffers[cpu];
1485 atomic_inc(&cpu_buffer->record_disabled);
1487 EXPORT_SYMBOL_GPL(ring_buffer_record_disable_cpu);
1490 * ring_buffer_record_enable_cpu - enable writes to the buffer
1491 * @buffer: The ring buffer to enable writes
1492 * @cpu: The CPU to enable.
1494 * Note, multiple disables will need the same number of enables
1495 * to truely enable the writing (much like preempt_disable).
1497 void ring_buffer_record_enable_cpu(struct ring_buffer *buffer, int cpu)
1499 struct ring_buffer_per_cpu *cpu_buffer;
1501 if (!cpu_isset(cpu, buffer->cpumask))
1504 cpu_buffer = buffer->buffers[cpu];
1505 atomic_dec(&cpu_buffer->record_disabled);
1507 EXPORT_SYMBOL_GPL(ring_buffer_record_enable_cpu);
1510 * ring_buffer_entries_cpu - get the number of entries in a cpu buffer
1511 * @buffer: The ring buffer
1512 * @cpu: The per CPU buffer to get the entries from.
1514 unsigned long ring_buffer_entries_cpu(struct ring_buffer *buffer, int cpu)
1516 struct ring_buffer_per_cpu *cpu_buffer;
1518 if (!cpu_isset(cpu, buffer->cpumask))
1521 cpu_buffer = buffer->buffers[cpu];
1522 return cpu_buffer->entries;
1524 EXPORT_SYMBOL_GPL(ring_buffer_entries_cpu);
1527 * ring_buffer_overrun_cpu - get the number of overruns in a cpu_buffer
1528 * @buffer: The ring buffer
1529 * @cpu: The per CPU buffer to get the number of overruns from
1531 unsigned long ring_buffer_overrun_cpu(struct ring_buffer *buffer, int cpu)
1533 struct ring_buffer_per_cpu *cpu_buffer;
1535 if (!cpu_isset(cpu, buffer->cpumask))
1538 cpu_buffer = buffer->buffers[cpu];
1539 return cpu_buffer->overrun;
1541 EXPORT_SYMBOL_GPL(ring_buffer_overrun_cpu);
1544 * ring_buffer_entries - get the number of entries in a buffer
1545 * @buffer: The ring buffer
1547 * Returns the total number of entries in the ring buffer
1550 unsigned long ring_buffer_entries(struct ring_buffer *buffer)
1552 struct ring_buffer_per_cpu *cpu_buffer;
1553 unsigned long entries = 0;
1556 /* if you care about this being correct, lock the buffer */
1557 for_each_buffer_cpu(buffer, cpu) {
1558 cpu_buffer = buffer->buffers[cpu];
1559 entries += cpu_buffer->entries;
1564 EXPORT_SYMBOL_GPL(ring_buffer_entries);
1567 * ring_buffer_overrun_cpu - get the number of overruns in buffer
1568 * @buffer: The ring buffer
1570 * Returns the total number of overruns in the ring buffer
1573 unsigned long ring_buffer_overruns(struct ring_buffer *buffer)
1575 struct ring_buffer_per_cpu *cpu_buffer;
1576 unsigned long overruns = 0;
1579 /* if you care about this being correct, lock the buffer */
1580 for_each_buffer_cpu(buffer, cpu) {
1581 cpu_buffer = buffer->buffers[cpu];
1582 overruns += cpu_buffer->overrun;
1587 EXPORT_SYMBOL_GPL(ring_buffer_overruns);
1589 static void rb_iter_reset(struct ring_buffer_iter *iter)
1591 struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
1593 /* Iterator usage is expected to have record disabled */
1594 if (list_empty(&cpu_buffer->reader_page->list)) {
1595 iter->head_page = cpu_buffer->head_page;
1596 iter->head = cpu_buffer->head_page->read;
1598 iter->head_page = cpu_buffer->reader_page;
1599 iter->head = cpu_buffer->reader_page->read;
1602 iter->read_stamp = cpu_buffer->read_stamp;
1604 iter->read_stamp = iter->head_page->page->time_stamp;
1608 * ring_buffer_iter_reset - reset an iterator
1609 * @iter: The iterator to reset
1611 * Resets the iterator, so that it will start from the beginning
1614 void ring_buffer_iter_reset(struct ring_buffer_iter *iter)
1616 struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
1617 unsigned long flags;
1619 spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
1620 rb_iter_reset(iter);
1621 spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
1623 EXPORT_SYMBOL_GPL(ring_buffer_iter_reset);
1626 * ring_buffer_iter_empty - check if an iterator has no more to read
1627 * @iter: The iterator to check
1629 int ring_buffer_iter_empty(struct ring_buffer_iter *iter)
1631 struct ring_buffer_per_cpu *cpu_buffer;
1633 cpu_buffer = iter->cpu_buffer;
1635 return iter->head_page == cpu_buffer->commit_page &&
1636 iter->head == rb_commit_index(cpu_buffer);
1638 EXPORT_SYMBOL_GPL(ring_buffer_iter_empty);
1641 rb_update_read_stamp(struct ring_buffer_per_cpu *cpu_buffer,
1642 struct ring_buffer_event *event)
1646 switch (event->type) {
1647 case RINGBUF_TYPE_PADDING:
1650 case RINGBUF_TYPE_TIME_EXTEND:
1651 delta = event->array[0];
1653 delta += event->time_delta;
1654 cpu_buffer->read_stamp += delta;
1657 case RINGBUF_TYPE_TIME_STAMP:
1658 /* FIXME: not implemented */
1661 case RINGBUF_TYPE_DATA:
1662 cpu_buffer->read_stamp += event->time_delta;
1672 rb_update_iter_read_stamp(struct ring_buffer_iter *iter,
1673 struct ring_buffer_event *event)
1677 switch (event->type) {
1678 case RINGBUF_TYPE_PADDING:
1681 case RINGBUF_TYPE_TIME_EXTEND:
1682 delta = event->array[0];
1684 delta += event->time_delta;
1685 iter->read_stamp += delta;
1688 case RINGBUF_TYPE_TIME_STAMP:
1689 /* FIXME: not implemented */
1692 case RINGBUF_TYPE_DATA:
1693 iter->read_stamp += event->time_delta;
1702 static struct buffer_page *
1703 rb_get_reader_page(struct ring_buffer_per_cpu *cpu_buffer)
1705 struct buffer_page *reader = NULL;
1706 unsigned long flags;
1709 local_irq_save(flags);
1710 __raw_spin_lock(&cpu_buffer->lock);
1714 * This should normally only loop twice. But because the
1715 * start of the reader inserts an empty page, it causes
1716 * a case where we will loop three times. There should be no
1717 * reason to loop four times (that I know of).
1719 if (RB_WARN_ON(cpu_buffer, ++nr_loops > 3)) {
1724 reader = cpu_buffer->reader_page;
1726 /* If there's more to read, return this page */
1727 if (cpu_buffer->reader_page->read < rb_page_size(reader))
1730 /* Never should we have an index greater than the size */
1731 if (RB_WARN_ON(cpu_buffer,
1732 cpu_buffer->reader_page->read > rb_page_size(reader)))
1735 /* check if we caught up to the tail */
1737 if (cpu_buffer->commit_page == cpu_buffer->reader_page)
1741 * Splice the empty reader page into the list around the head.
1742 * Reset the reader page to size zero.
1745 reader = cpu_buffer->head_page;
1746 cpu_buffer->reader_page->list.next = reader->list.next;
1747 cpu_buffer->reader_page->list.prev = reader->list.prev;
1749 local_set(&cpu_buffer->reader_page->write, 0);
1750 local_set(&cpu_buffer->reader_page->page->commit, 0);
1752 /* Make the reader page now replace the head */
1753 reader->list.prev->next = &cpu_buffer->reader_page->list;
1754 reader->list.next->prev = &cpu_buffer->reader_page->list;
1757 * If the tail is on the reader, then we must set the head
1758 * to the inserted page, otherwise we set it one before.
1760 cpu_buffer->head_page = cpu_buffer->reader_page;
1762 if (cpu_buffer->commit_page != reader)
1763 rb_inc_page(cpu_buffer, &cpu_buffer->head_page);
1765 /* Finally update the reader page to the new head */
1766 cpu_buffer->reader_page = reader;
1767 rb_reset_reader_page(cpu_buffer);
1772 __raw_spin_unlock(&cpu_buffer->lock);
1773 local_irq_restore(flags);
1778 static void rb_advance_reader(struct ring_buffer_per_cpu *cpu_buffer)
1780 struct ring_buffer_event *event;
1781 struct buffer_page *reader;
1784 reader = rb_get_reader_page(cpu_buffer);
1786 /* This function should not be called when buffer is empty */
1787 if (RB_WARN_ON(cpu_buffer, !reader))
1790 event = rb_reader_event(cpu_buffer);
1792 if (event->type == RINGBUF_TYPE_DATA)
1793 cpu_buffer->entries--;
1795 rb_update_read_stamp(cpu_buffer, event);
1797 length = rb_event_length(event);
1798 cpu_buffer->reader_page->read += length;
1801 static void rb_advance_iter(struct ring_buffer_iter *iter)
1803 struct ring_buffer *buffer;
1804 struct ring_buffer_per_cpu *cpu_buffer;
1805 struct ring_buffer_event *event;
1808 cpu_buffer = iter->cpu_buffer;
1809 buffer = cpu_buffer->buffer;
1812 * Check if we are at the end of the buffer.
1814 if (iter->head >= rb_page_size(iter->head_page)) {
1815 if (RB_WARN_ON(buffer,
1816 iter->head_page == cpu_buffer->commit_page))
1822 event = rb_iter_head_event(iter);
1824 length = rb_event_length(event);
1827 * This should not be called to advance the header if we are
1828 * at the tail of the buffer.
1830 if (RB_WARN_ON(cpu_buffer,
1831 (iter->head_page == cpu_buffer->commit_page) &&
1832 (iter->head + length > rb_commit_index(cpu_buffer))))
1835 rb_update_iter_read_stamp(iter, event);
1837 iter->head += length;
1839 /* check for end of page padding */
1840 if ((iter->head >= rb_page_size(iter->head_page)) &&
1841 (iter->head_page != cpu_buffer->commit_page))
1842 rb_advance_iter(iter);
1845 static struct ring_buffer_event *
1846 rb_buffer_peek(struct ring_buffer *buffer, int cpu, u64 *ts)
1848 struct ring_buffer_per_cpu *cpu_buffer;
1849 struct ring_buffer_event *event;
1850 struct buffer_page *reader;
1853 if (!cpu_isset(cpu, buffer->cpumask))
1856 cpu_buffer = buffer->buffers[cpu];
1860 * We repeat when a timestamp is encountered. It is possible
1861 * to get multiple timestamps from an interrupt entering just
1862 * as one timestamp is about to be written. The max times
1863 * that this can happen is the number of nested interrupts we
1864 * can have. Nesting 10 deep of interrupts is clearly
1867 if (RB_WARN_ON(cpu_buffer, ++nr_loops > 10))
1870 reader = rb_get_reader_page(cpu_buffer);
1874 event = rb_reader_event(cpu_buffer);
1876 switch (event->type) {
1877 case RINGBUF_TYPE_PADDING:
1878 RB_WARN_ON(cpu_buffer, 1);
1879 rb_advance_reader(cpu_buffer);
1882 case RINGBUF_TYPE_TIME_EXTEND:
1883 /* Internal data, OK to advance */
1884 rb_advance_reader(cpu_buffer);
1887 case RINGBUF_TYPE_TIME_STAMP:
1888 /* FIXME: not implemented */
1889 rb_advance_reader(cpu_buffer);
1892 case RINGBUF_TYPE_DATA:
1894 *ts = cpu_buffer->read_stamp + event->time_delta;
1895 ring_buffer_normalize_time_stamp(cpu_buffer->cpu, ts);
1905 EXPORT_SYMBOL_GPL(ring_buffer_peek);
1907 static struct ring_buffer_event *
1908 rb_iter_peek(struct ring_buffer_iter *iter, u64 *ts)
1910 struct ring_buffer *buffer;
1911 struct ring_buffer_per_cpu *cpu_buffer;
1912 struct ring_buffer_event *event;
1915 if (ring_buffer_iter_empty(iter))
1918 cpu_buffer = iter->cpu_buffer;
1919 buffer = cpu_buffer->buffer;
1923 * We repeat when a timestamp is encountered. It is possible
1924 * to get multiple timestamps from an interrupt entering just
1925 * as one timestamp is about to be written. The max times
1926 * that this can happen is the number of nested interrupts we
1927 * can have. Nesting 10 deep of interrupts is clearly
1930 if (RB_WARN_ON(cpu_buffer, ++nr_loops > 10))
1933 if (rb_per_cpu_empty(cpu_buffer))
1936 event = rb_iter_head_event(iter);
1938 switch (event->type) {
1939 case RINGBUF_TYPE_PADDING:
1943 case RINGBUF_TYPE_TIME_EXTEND:
1944 /* Internal data, OK to advance */
1945 rb_advance_iter(iter);
1948 case RINGBUF_TYPE_TIME_STAMP:
1949 /* FIXME: not implemented */
1950 rb_advance_iter(iter);
1953 case RINGBUF_TYPE_DATA:
1955 *ts = iter->read_stamp + event->time_delta;
1956 ring_buffer_normalize_time_stamp(cpu_buffer->cpu, ts);
1966 EXPORT_SYMBOL_GPL(ring_buffer_iter_peek);
1969 * ring_buffer_peek - peek at the next event to be read
1970 * @buffer: The ring buffer to read
1971 * @cpu: The cpu to peak at
1972 * @ts: The timestamp counter of this event.
1974 * This will return the event that will be read next, but does
1975 * not consume the data.
1977 struct ring_buffer_event *
1978 ring_buffer_peek(struct ring_buffer *buffer, int cpu, u64 *ts)
1980 struct ring_buffer_per_cpu *cpu_buffer = buffer->buffers[cpu];
1981 struct ring_buffer_event *event;
1982 unsigned long flags;
1984 spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
1985 event = rb_buffer_peek(buffer, cpu, ts);
1986 spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
1992 * ring_buffer_iter_peek - peek at the next event to be read
1993 * @iter: The ring buffer iterator
1994 * @ts: The timestamp counter of this event.
1996 * This will return the event that will be read next, but does
1997 * not increment the iterator.
1999 struct ring_buffer_event *
2000 ring_buffer_iter_peek(struct ring_buffer_iter *iter, u64 *ts)
2002 struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
2003 struct ring_buffer_event *event;
2004 unsigned long flags;
2006 spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
2007 event = rb_iter_peek(iter, ts);
2008 spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
2014 * ring_buffer_consume - return an event and consume it
2015 * @buffer: The ring buffer to get the next event from
2017 * Returns the next event in the ring buffer, and that event is consumed.
2018 * Meaning, that sequential reads will keep returning a different event,
2019 * and eventually empty the ring buffer if the producer is slower.
2021 struct ring_buffer_event *
2022 ring_buffer_consume(struct ring_buffer *buffer, int cpu, u64 *ts)
2024 struct ring_buffer_per_cpu *cpu_buffer = buffer->buffers[cpu];
2025 struct ring_buffer_event *event;
2026 unsigned long flags;
2028 if (!cpu_isset(cpu, buffer->cpumask))
2031 spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
2033 event = rb_buffer_peek(buffer, cpu, ts);
2037 rb_advance_reader(cpu_buffer);
2040 spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
2044 EXPORT_SYMBOL_GPL(ring_buffer_consume);
2047 * ring_buffer_read_start - start a non consuming read of the buffer
2048 * @buffer: The ring buffer to read from
2049 * @cpu: The cpu buffer to iterate over
2051 * This starts up an iteration through the buffer. It also disables
2052 * the recording to the buffer until the reading is finished.
2053 * This prevents the reading from being corrupted. This is not
2054 * a consuming read, so a producer is not expected.
2056 * Must be paired with ring_buffer_finish.
2058 struct ring_buffer_iter *
2059 ring_buffer_read_start(struct ring_buffer *buffer, int cpu)
2061 struct ring_buffer_per_cpu *cpu_buffer;
2062 struct ring_buffer_iter *iter;
2063 unsigned long flags;
2065 if (!cpu_isset(cpu, buffer->cpumask))
2068 iter = kmalloc(sizeof(*iter), GFP_KERNEL);
2072 cpu_buffer = buffer->buffers[cpu];
2074 iter->cpu_buffer = cpu_buffer;
2076 atomic_inc(&cpu_buffer->record_disabled);
2077 synchronize_sched();
2079 spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
2080 __raw_spin_lock(&cpu_buffer->lock);
2081 rb_iter_reset(iter);
2082 __raw_spin_unlock(&cpu_buffer->lock);
2083 spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
2087 EXPORT_SYMBOL_GPL(ring_buffer_read_start);
2090 * ring_buffer_finish - finish reading the iterator of the buffer
2091 * @iter: The iterator retrieved by ring_buffer_start
2093 * This re-enables the recording to the buffer, and frees the
2097 ring_buffer_read_finish(struct ring_buffer_iter *iter)
2099 struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
2101 atomic_dec(&cpu_buffer->record_disabled);
2104 EXPORT_SYMBOL_GPL(ring_buffer_read_finish);
2107 * ring_buffer_read - read the next item in the ring buffer by the iterator
2108 * @iter: The ring buffer iterator
2109 * @ts: The time stamp of the event read.
2111 * This reads the next event in the ring buffer and increments the iterator.
2113 struct ring_buffer_event *
2114 ring_buffer_read(struct ring_buffer_iter *iter, u64 *ts)
2116 struct ring_buffer_event *event;
2117 struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
2118 unsigned long flags;
2120 spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
2121 event = rb_iter_peek(iter, ts);
2125 rb_advance_iter(iter);
2127 spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
2131 EXPORT_SYMBOL_GPL(ring_buffer_read);
2134 * ring_buffer_size - return the size of the ring buffer (in bytes)
2135 * @buffer: The ring buffer.
2137 unsigned long ring_buffer_size(struct ring_buffer *buffer)
2139 return BUF_PAGE_SIZE * buffer->pages;
2141 EXPORT_SYMBOL_GPL(ring_buffer_size);
2144 rb_reset_cpu(struct ring_buffer_per_cpu *cpu_buffer)
2146 cpu_buffer->head_page
2147 = list_entry(cpu_buffer->pages.next, struct buffer_page, list);
2148 local_set(&cpu_buffer->head_page->write, 0);
2149 local_set(&cpu_buffer->head_page->page->commit, 0);
2151 cpu_buffer->head_page->read = 0;
2153 cpu_buffer->tail_page = cpu_buffer->head_page;
2154 cpu_buffer->commit_page = cpu_buffer->head_page;
2156 INIT_LIST_HEAD(&cpu_buffer->reader_page->list);
2157 local_set(&cpu_buffer->reader_page->write, 0);
2158 local_set(&cpu_buffer->reader_page->page->commit, 0);
2159 cpu_buffer->reader_page->read = 0;
2161 cpu_buffer->overrun = 0;
2162 cpu_buffer->entries = 0;
2166 * ring_buffer_reset_cpu - reset a ring buffer per CPU buffer
2167 * @buffer: The ring buffer to reset a per cpu buffer of
2168 * @cpu: The CPU buffer to be reset
2170 void ring_buffer_reset_cpu(struct ring_buffer *buffer, int cpu)
2172 struct ring_buffer_per_cpu *cpu_buffer = buffer->buffers[cpu];
2173 unsigned long flags;
2175 if (!cpu_isset(cpu, buffer->cpumask))
2178 spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
2180 __raw_spin_lock(&cpu_buffer->lock);
2182 rb_reset_cpu(cpu_buffer);
2184 __raw_spin_unlock(&cpu_buffer->lock);
2186 spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
2188 EXPORT_SYMBOL_GPL(ring_buffer_reset_cpu);
2191 * ring_buffer_reset - reset a ring buffer
2192 * @buffer: The ring buffer to reset all cpu buffers
2194 void ring_buffer_reset(struct ring_buffer *buffer)
2198 for_each_buffer_cpu(buffer, cpu)
2199 ring_buffer_reset_cpu(buffer, cpu);
2201 EXPORT_SYMBOL_GPL(ring_buffer_reset);
2204 * rind_buffer_empty - is the ring buffer empty?
2205 * @buffer: The ring buffer to test
2207 int ring_buffer_empty(struct ring_buffer *buffer)
2209 struct ring_buffer_per_cpu *cpu_buffer;
2212 /* yes this is racy, but if you don't like the race, lock the buffer */
2213 for_each_buffer_cpu(buffer, cpu) {
2214 cpu_buffer = buffer->buffers[cpu];
2215 if (!rb_per_cpu_empty(cpu_buffer))
2220 EXPORT_SYMBOL_GPL(ring_buffer_empty);
2223 * ring_buffer_empty_cpu - is a cpu buffer of a ring buffer empty?
2224 * @buffer: The ring buffer
2225 * @cpu: The CPU buffer to test
2227 int ring_buffer_empty_cpu(struct ring_buffer *buffer, int cpu)
2229 struct ring_buffer_per_cpu *cpu_buffer;
2231 if (!cpu_isset(cpu, buffer->cpumask))
2234 cpu_buffer = buffer->buffers[cpu];
2235 return rb_per_cpu_empty(cpu_buffer);
2237 EXPORT_SYMBOL_GPL(ring_buffer_empty_cpu);
2240 * ring_buffer_swap_cpu - swap a CPU buffer between two ring buffers
2241 * @buffer_a: One buffer to swap with
2242 * @buffer_b: The other buffer to swap with
2244 * This function is useful for tracers that want to take a "snapshot"
2245 * of a CPU buffer and has another back up buffer lying around.
2246 * it is expected that the tracer handles the cpu buffer not being
2247 * used at the moment.
2249 int ring_buffer_swap_cpu(struct ring_buffer *buffer_a,
2250 struct ring_buffer *buffer_b, int cpu)
2252 struct ring_buffer_per_cpu *cpu_buffer_a;
2253 struct ring_buffer_per_cpu *cpu_buffer_b;
2255 if (!cpu_isset(cpu, buffer_a->cpumask) ||
2256 !cpu_isset(cpu, buffer_b->cpumask))
2259 /* At least make sure the two buffers are somewhat the same */
2260 if (buffer_a->pages != buffer_b->pages)
2263 cpu_buffer_a = buffer_a->buffers[cpu];
2264 cpu_buffer_b = buffer_b->buffers[cpu];
2267 * We can't do a synchronize_sched here because this
2268 * function can be called in atomic context.
2269 * Normally this will be called from the same CPU as cpu.
2270 * If not it's up to the caller to protect this.
2272 atomic_inc(&cpu_buffer_a->record_disabled);
2273 atomic_inc(&cpu_buffer_b->record_disabled);
2275 buffer_a->buffers[cpu] = cpu_buffer_b;
2276 buffer_b->buffers[cpu] = cpu_buffer_a;
2278 cpu_buffer_b->buffer = buffer_a;
2279 cpu_buffer_a->buffer = buffer_b;
2281 atomic_dec(&cpu_buffer_a->record_disabled);
2282 atomic_dec(&cpu_buffer_b->record_disabled);
2286 EXPORT_SYMBOL_GPL(ring_buffer_swap_cpu);
2288 static void rb_remove_entries(struct ring_buffer_per_cpu *cpu_buffer,
2289 struct buffer_data_page *bpage)
2291 struct ring_buffer_event *event;
2294 __raw_spin_lock(&cpu_buffer->lock);
2295 for (head = 0; head < local_read(&bpage->commit);
2296 head += rb_event_length(event)) {
2298 event = __rb_data_page_index(bpage, head);
2299 if (RB_WARN_ON(cpu_buffer, rb_null_event(event)))
2301 /* Only count data entries */
2302 if (event->type != RINGBUF_TYPE_DATA)
2304 cpu_buffer->entries--;
2306 __raw_spin_unlock(&cpu_buffer->lock);
2310 * ring_buffer_alloc_read_page - allocate a page to read from buffer
2311 * @buffer: the buffer to allocate for.
2313 * This function is used in conjunction with ring_buffer_read_page.
2314 * When reading a full page from the ring buffer, these functions
2315 * can be used to speed up the process. The calling function should
2316 * allocate a few pages first with this function. Then when it
2317 * needs to get pages from the ring buffer, it passes the result
2318 * of this function into ring_buffer_read_page, which will swap
2319 * the page that was allocated, with the read page of the buffer.
2322 * The page allocated, or NULL on error.
2324 void *ring_buffer_alloc_read_page(struct ring_buffer *buffer)
2327 struct buffer_data_page *bpage;
2329 addr = __get_free_page(GFP_KERNEL);
2333 bpage = (void *)addr;
2339 * ring_buffer_free_read_page - free an allocated read page
2340 * @buffer: the buffer the page was allocate for
2341 * @data: the page to free
2343 * Free a page allocated from ring_buffer_alloc_read_page.
2345 void ring_buffer_free_read_page(struct ring_buffer *buffer, void *data)
2347 free_page((unsigned long)data);
2351 * ring_buffer_read_page - extract a page from the ring buffer
2352 * @buffer: buffer to extract from
2353 * @data_page: the page to use allocated from ring_buffer_alloc_read_page
2354 * @cpu: the cpu of the buffer to extract
2355 * @full: should the extraction only happen when the page is full.
2357 * This function will pull out a page from the ring buffer and consume it.
2358 * @data_page must be the address of the variable that was returned
2359 * from ring_buffer_alloc_read_page. This is because the page might be used
2360 * to swap with a page in the ring buffer.
2363 * rpage = ring_buffer_alloc_page(buffer);
2366 * ret = ring_buffer_read_page(buffer, &rpage, cpu, 0);
2368 * process_page(rpage);
2370 * When @full is set, the function will not return true unless
2371 * the writer is off the reader page.
2373 * Note: it is up to the calling functions to handle sleeps and wakeups.
2374 * The ring buffer can be used anywhere in the kernel and can not
2375 * blindly call wake_up. The layer that uses the ring buffer must be
2376 * responsible for that.
2379 * 1 if data has been transferred
2380 * 0 if no data has been transferred.
2382 int ring_buffer_read_page(struct ring_buffer *buffer,
2383 void **data_page, int cpu, int full)
2385 struct ring_buffer_per_cpu *cpu_buffer = buffer->buffers[cpu];
2386 struct ring_buffer_event *event;
2387 struct buffer_data_page *bpage;
2388 unsigned long flags;
2398 spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
2401 * rb_buffer_peek will get the next ring buffer if
2402 * the current reader page is empty.
2404 event = rb_buffer_peek(buffer, cpu, NULL);
2408 /* check for data */
2409 if (!local_read(&cpu_buffer->reader_page->page->commit))
2412 * If the writer is already off of the read page, then simply
2413 * switch the read page with the given page. Otherwise
2414 * we need to copy the data from the reader to the writer.
2416 if (cpu_buffer->reader_page == cpu_buffer->commit_page) {
2417 unsigned int read = cpu_buffer->reader_page->read;
2421 /* The writer is still on the reader page, we must copy */
2422 bpage = cpu_buffer->reader_page->page;
2424 cpu_buffer->reader_page->page->data + read,
2425 local_read(&bpage->commit) - read);
2427 /* consume what was read */
2428 cpu_buffer->reader_page += read;
2431 /* swap the pages */
2432 rb_init_page(bpage);
2433 bpage = cpu_buffer->reader_page->page;
2434 cpu_buffer->reader_page->page = *data_page;
2435 cpu_buffer->reader_page->read = 0;
2440 /* update the entry counter */
2441 rb_remove_entries(cpu_buffer, bpage);
2443 spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
2449 rb_simple_read(struct file *filp, char __user *ubuf,
2450 size_t cnt, loff_t *ppos)
2452 long *p = filp->private_data;
2456 if (test_bit(RB_BUFFERS_DISABLED_BIT, p))
2457 r = sprintf(buf, "permanently disabled\n");
2459 r = sprintf(buf, "%d\n", test_bit(RB_BUFFERS_ON_BIT, p));
2461 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2465 rb_simple_write(struct file *filp, const char __user *ubuf,
2466 size_t cnt, loff_t *ppos)
2468 long *p = filp->private_data;
2473 if (cnt >= sizeof(buf))
2476 if (copy_from_user(&buf, ubuf, cnt))
2481 ret = strict_strtoul(buf, 10, &val);
2486 set_bit(RB_BUFFERS_ON_BIT, p);
2488 clear_bit(RB_BUFFERS_ON_BIT, p);
2495 static struct file_operations rb_simple_fops = {
2496 .open = tracing_open_generic,
2497 .read = rb_simple_read,
2498 .write = rb_simple_write,
2502 static __init int rb_init_debugfs(void)
2504 struct dentry *d_tracer;
2505 struct dentry *entry;
2507 d_tracer = tracing_init_dentry();
2509 entry = debugfs_create_file("tracing_on", 0644, d_tracer,
2510 &ring_buffer_flags, &rb_simple_fops);
2512 pr_warning("Could not create debugfs 'tracing_on' entry\n");
2517 fs_initcall(rb_init_debugfs);