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 unsigned length = rb_event_length(event);
172 if (event->type != RINGBUF_TYPE_DATA)
174 length -= RB_EVNT_HDR_SIZE;
175 if (length > RB_MAX_SMALL_DATA + sizeof(event->array[0]))
176 length -= sizeof(event->array[0]);
179 EXPORT_SYMBOL_GPL(ring_buffer_event_length);
181 /* inline for ring buffer fast paths */
183 rb_event_data(struct ring_buffer_event *event)
185 BUG_ON(event->type != RINGBUF_TYPE_DATA);
186 /* If length is in len field, then array[0] has the data */
188 return (void *)&event->array[0];
189 /* Otherwise length is in array[0] and array[1] has the data */
190 return (void *)&event->array[1];
194 * ring_buffer_event_data - return the data of the event
195 * @event: the event to get the data from
197 void *ring_buffer_event_data(struct ring_buffer_event *event)
199 return rb_event_data(event);
201 EXPORT_SYMBOL_GPL(ring_buffer_event_data);
203 #define for_each_buffer_cpu(buffer, cpu) \
204 for_each_cpu(cpu, buffer->cpumask)
207 #define TS_MASK ((1ULL << TS_SHIFT) - 1)
208 #define TS_DELTA_TEST (~TS_MASK)
210 struct buffer_data_page {
211 u64 time_stamp; /* page time stamp */
212 local_t commit; /* write commited index */
213 unsigned char data[]; /* data of buffer page */
217 local_t write; /* index for next write */
218 unsigned read; /* index for next read */
219 struct list_head list; /* list of free pages */
220 struct buffer_data_page *page; /* Actual data page */
223 static void rb_init_page(struct buffer_data_page *bpage)
225 local_set(&bpage->commit, 0);
229 * Also stolen from mm/slob.c. Thanks to Mathieu Desnoyers for pointing
232 static inline void free_buffer_page(struct buffer_page *bpage)
235 free_page((unsigned long)bpage->page);
240 * We need to fit the time_stamp delta into 27 bits.
242 static inline int test_time_stamp(u64 delta)
244 if (delta & TS_DELTA_TEST)
249 #define BUF_PAGE_SIZE (PAGE_SIZE - offsetof(struct buffer_data_page, data))
252 * head_page == tail_page && head == tail then buffer is empty.
254 struct ring_buffer_per_cpu {
256 struct ring_buffer *buffer;
257 spinlock_t reader_lock; /* serialize readers */
259 struct lock_class_key lock_key;
260 struct list_head pages;
261 struct buffer_page *head_page; /* read from head */
262 struct buffer_page *tail_page; /* write to tail */
263 struct buffer_page *commit_page; /* commited pages */
264 struct buffer_page *reader_page;
265 unsigned long overrun;
266 unsigned long entries;
269 atomic_t record_disabled;
276 cpumask_var_t cpumask;
277 atomic_t record_disabled;
281 struct ring_buffer_per_cpu **buffers;
284 struct ring_buffer_iter {
285 struct ring_buffer_per_cpu *cpu_buffer;
287 struct buffer_page *head_page;
291 /* buffer may be either ring_buffer or ring_buffer_per_cpu */
292 #define RB_WARN_ON(buffer, cond) \
294 int _____ret = unlikely(cond); \
296 atomic_inc(&buffer->record_disabled); \
303 * check_pages - integrity check of buffer pages
304 * @cpu_buffer: CPU buffer with pages to test
306 * As a safty measure we check to make sure the data pages have not
309 static int rb_check_pages(struct ring_buffer_per_cpu *cpu_buffer)
311 struct list_head *head = &cpu_buffer->pages;
312 struct buffer_page *bpage, *tmp;
314 if (RB_WARN_ON(cpu_buffer, head->next->prev != head))
316 if (RB_WARN_ON(cpu_buffer, head->prev->next != head))
319 list_for_each_entry_safe(bpage, tmp, head, list) {
320 if (RB_WARN_ON(cpu_buffer,
321 bpage->list.next->prev != &bpage->list))
323 if (RB_WARN_ON(cpu_buffer,
324 bpage->list.prev->next != &bpage->list))
331 static int rb_allocate_pages(struct ring_buffer_per_cpu *cpu_buffer,
334 struct list_head *head = &cpu_buffer->pages;
335 struct buffer_page *bpage, *tmp;
340 for (i = 0; i < nr_pages; i++) {
341 bpage = kzalloc_node(ALIGN(sizeof(*bpage), cache_line_size()),
342 GFP_KERNEL, cpu_to_node(cpu_buffer->cpu));
345 list_add(&bpage->list, &pages);
347 addr = __get_free_page(GFP_KERNEL);
350 bpage->page = (void *)addr;
351 rb_init_page(bpage->page);
354 list_splice(&pages, head);
356 rb_check_pages(cpu_buffer);
361 list_for_each_entry_safe(bpage, tmp, &pages, list) {
362 list_del_init(&bpage->list);
363 free_buffer_page(bpage);
368 static struct ring_buffer_per_cpu *
369 rb_allocate_cpu_buffer(struct ring_buffer *buffer, int cpu)
371 struct ring_buffer_per_cpu *cpu_buffer;
372 struct buffer_page *bpage;
376 cpu_buffer = kzalloc_node(ALIGN(sizeof(*cpu_buffer), cache_line_size()),
377 GFP_KERNEL, cpu_to_node(cpu));
381 cpu_buffer->cpu = cpu;
382 cpu_buffer->buffer = buffer;
383 spin_lock_init(&cpu_buffer->reader_lock);
384 cpu_buffer->lock = (raw_spinlock_t)__RAW_SPIN_LOCK_UNLOCKED;
385 INIT_LIST_HEAD(&cpu_buffer->pages);
387 bpage = kzalloc_node(ALIGN(sizeof(*bpage), cache_line_size()),
388 GFP_KERNEL, cpu_to_node(cpu));
390 goto fail_free_buffer;
392 cpu_buffer->reader_page = bpage;
393 addr = __get_free_page(GFP_KERNEL);
395 goto fail_free_reader;
396 bpage->page = (void *)addr;
397 rb_init_page(bpage->page);
399 INIT_LIST_HEAD(&cpu_buffer->reader_page->list);
401 ret = rb_allocate_pages(cpu_buffer, buffer->pages);
403 goto fail_free_reader;
405 cpu_buffer->head_page
406 = list_entry(cpu_buffer->pages.next, struct buffer_page, list);
407 cpu_buffer->tail_page = cpu_buffer->commit_page = cpu_buffer->head_page;
412 free_buffer_page(cpu_buffer->reader_page);
419 static void rb_free_cpu_buffer(struct ring_buffer_per_cpu *cpu_buffer)
421 struct list_head *head = &cpu_buffer->pages;
422 struct buffer_page *bpage, *tmp;
424 list_del_init(&cpu_buffer->reader_page->list);
425 free_buffer_page(cpu_buffer->reader_page);
427 list_for_each_entry_safe(bpage, tmp, head, list) {
428 list_del_init(&bpage->list);
429 free_buffer_page(bpage);
435 * Causes compile errors if the struct buffer_page gets bigger
436 * than the struct page.
438 extern int ring_buffer_page_too_big(void);
441 * ring_buffer_alloc - allocate a new ring_buffer
442 * @size: the size in bytes per cpu that is needed.
443 * @flags: attributes to set for the ring buffer.
445 * Currently the only flag that is available is the RB_FL_OVERWRITE
446 * flag. This flag means that the buffer will overwrite old data
447 * when the buffer wraps. If this flag is not set, the buffer will
448 * drop data when the tail hits the head.
450 struct ring_buffer *ring_buffer_alloc(unsigned long size, unsigned flags)
452 struct ring_buffer *buffer;
456 /* Paranoid! Optimizes out when all is well */
457 if (sizeof(struct buffer_page) > sizeof(struct page))
458 ring_buffer_page_too_big();
461 /* keep it in its own cache line */
462 buffer = kzalloc(ALIGN(sizeof(*buffer), cache_line_size()),
467 if (!alloc_cpumask_var(&buffer->cpumask, GFP_KERNEL))
468 goto fail_free_buffer;
470 buffer->pages = DIV_ROUND_UP(size, BUF_PAGE_SIZE);
471 buffer->flags = flags;
473 /* need at least two pages */
474 if (buffer->pages == 1)
477 cpumask_copy(buffer->cpumask, cpu_possible_mask);
478 buffer->cpus = nr_cpu_ids;
480 bsize = sizeof(void *) * nr_cpu_ids;
481 buffer->buffers = kzalloc(ALIGN(bsize, cache_line_size()),
483 if (!buffer->buffers)
484 goto fail_free_cpumask;
486 for_each_buffer_cpu(buffer, cpu) {
487 buffer->buffers[cpu] =
488 rb_allocate_cpu_buffer(buffer, cpu);
489 if (!buffer->buffers[cpu])
490 goto fail_free_buffers;
493 mutex_init(&buffer->mutex);
498 for_each_buffer_cpu(buffer, cpu) {
499 if (buffer->buffers[cpu])
500 rb_free_cpu_buffer(buffer->buffers[cpu]);
502 kfree(buffer->buffers);
505 free_cpumask_var(buffer->cpumask);
511 EXPORT_SYMBOL_GPL(ring_buffer_alloc);
514 * ring_buffer_free - free a ring buffer.
515 * @buffer: the buffer to free.
518 ring_buffer_free(struct ring_buffer *buffer)
522 for_each_buffer_cpu(buffer, cpu)
523 rb_free_cpu_buffer(buffer->buffers[cpu]);
525 free_cpumask_var(buffer->cpumask);
529 EXPORT_SYMBOL_GPL(ring_buffer_free);
531 static void rb_reset_cpu(struct ring_buffer_per_cpu *cpu_buffer);
534 rb_remove_pages(struct ring_buffer_per_cpu *cpu_buffer, unsigned nr_pages)
536 struct buffer_page *bpage;
540 atomic_inc(&cpu_buffer->record_disabled);
543 for (i = 0; i < nr_pages; i++) {
544 if (RB_WARN_ON(cpu_buffer, list_empty(&cpu_buffer->pages)))
546 p = cpu_buffer->pages.next;
547 bpage = list_entry(p, struct buffer_page, list);
548 list_del_init(&bpage->list);
549 free_buffer_page(bpage);
551 if (RB_WARN_ON(cpu_buffer, list_empty(&cpu_buffer->pages)))
554 rb_reset_cpu(cpu_buffer);
556 rb_check_pages(cpu_buffer);
558 atomic_dec(&cpu_buffer->record_disabled);
563 rb_insert_pages(struct ring_buffer_per_cpu *cpu_buffer,
564 struct list_head *pages, unsigned nr_pages)
566 struct buffer_page *bpage;
570 atomic_inc(&cpu_buffer->record_disabled);
573 for (i = 0; i < nr_pages; i++) {
574 if (RB_WARN_ON(cpu_buffer, list_empty(pages)))
577 bpage = list_entry(p, struct buffer_page, list);
578 list_del_init(&bpage->list);
579 list_add_tail(&bpage->list, &cpu_buffer->pages);
581 rb_reset_cpu(cpu_buffer);
583 rb_check_pages(cpu_buffer);
585 atomic_dec(&cpu_buffer->record_disabled);
589 * ring_buffer_resize - resize the ring buffer
590 * @buffer: the buffer to resize.
591 * @size: the new size.
593 * The tracer is responsible for making sure that the buffer is
594 * not being used while changing the size.
595 * Note: We may be able to change the above requirement by using
596 * RCU synchronizations.
598 * Minimum size is 2 * BUF_PAGE_SIZE.
600 * Returns -1 on failure.
602 int ring_buffer_resize(struct ring_buffer *buffer, unsigned long size)
604 struct ring_buffer_per_cpu *cpu_buffer;
605 unsigned nr_pages, rm_pages, new_pages;
606 struct buffer_page *bpage, *tmp;
607 unsigned long buffer_size;
613 * Always succeed at resizing a non-existent buffer:
618 size = DIV_ROUND_UP(size, BUF_PAGE_SIZE);
619 size *= BUF_PAGE_SIZE;
620 buffer_size = buffer->pages * BUF_PAGE_SIZE;
622 /* we need a minimum of two pages */
623 if (size < BUF_PAGE_SIZE * 2)
624 size = BUF_PAGE_SIZE * 2;
626 if (size == buffer_size)
629 mutex_lock(&buffer->mutex);
631 nr_pages = DIV_ROUND_UP(size, BUF_PAGE_SIZE);
633 if (size < buffer_size) {
635 /* easy case, just free pages */
636 if (RB_WARN_ON(buffer, nr_pages >= buffer->pages)) {
637 mutex_unlock(&buffer->mutex);
641 rm_pages = buffer->pages - nr_pages;
643 for_each_buffer_cpu(buffer, cpu) {
644 cpu_buffer = buffer->buffers[cpu];
645 rb_remove_pages(cpu_buffer, rm_pages);
651 * This is a bit more difficult. We only want to add pages
652 * when we can allocate enough for all CPUs. We do this
653 * by allocating all the pages and storing them on a local
654 * link list. If we succeed in our allocation, then we
655 * add these pages to the cpu_buffers. Otherwise we just free
656 * them all and return -ENOMEM;
658 if (RB_WARN_ON(buffer, nr_pages <= buffer->pages)) {
659 mutex_unlock(&buffer->mutex);
663 new_pages = nr_pages - buffer->pages;
665 for_each_buffer_cpu(buffer, cpu) {
666 for (i = 0; i < new_pages; i++) {
667 bpage = kzalloc_node(ALIGN(sizeof(*bpage),
669 GFP_KERNEL, cpu_to_node(cpu));
672 list_add(&bpage->list, &pages);
673 addr = __get_free_page(GFP_KERNEL);
676 bpage->page = (void *)addr;
677 rb_init_page(bpage->page);
681 for_each_buffer_cpu(buffer, cpu) {
682 cpu_buffer = buffer->buffers[cpu];
683 rb_insert_pages(cpu_buffer, &pages, new_pages);
686 if (RB_WARN_ON(buffer, !list_empty(&pages))) {
687 mutex_unlock(&buffer->mutex);
692 buffer->pages = nr_pages;
693 mutex_unlock(&buffer->mutex);
698 list_for_each_entry_safe(bpage, tmp, &pages, list) {
699 list_del_init(&bpage->list);
700 free_buffer_page(bpage);
702 mutex_unlock(&buffer->mutex);
705 EXPORT_SYMBOL_GPL(ring_buffer_resize);
707 static inline int rb_null_event(struct ring_buffer_event *event)
709 return event->type == RINGBUF_TYPE_PADDING;
713 __rb_data_page_index(struct buffer_data_page *bpage, unsigned index)
715 return bpage->data + index;
718 static inline void *__rb_page_index(struct buffer_page *bpage, unsigned index)
720 return bpage->page->data + index;
723 static inline struct ring_buffer_event *
724 rb_reader_event(struct ring_buffer_per_cpu *cpu_buffer)
726 return __rb_page_index(cpu_buffer->reader_page,
727 cpu_buffer->reader_page->read);
730 static inline struct ring_buffer_event *
731 rb_head_event(struct ring_buffer_per_cpu *cpu_buffer)
733 return __rb_page_index(cpu_buffer->head_page,
734 cpu_buffer->head_page->read);
737 static inline struct ring_buffer_event *
738 rb_iter_head_event(struct ring_buffer_iter *iter)
740 return __rb_page_index(iter->head_page, iter->head);
743 static inline unsigned rb_page_write(struct buffer_page *bpage)
745 return local_read(&bpage->write);
748 static inline unsigned rb_page_commit(struct buffer_page *bpage)
750 return local_read(&bpage->page->commit);
753 /* Size is determined by what has been commited */
754 static inline unsigned rb_page_size(struct buffer_page *bpage)
756 return rb_page_commit(bpage);
759 static inline unsigned
760 rb_commit_index(struct ring_buffer_per_cpu *cpu_buffer)
762 return rb_page_commit(cpu_buffer->commit_page);
765 static inline unsigned rb_head_size(struct ring_buffer_per_cpu *cpu_buffer)
767 return rb_page_commit(cpu_buffer->head_page);
771 * When the tail hits the head and the buffer is in overwrite mode,
772 * the head jumps to the next page and all content on the previous
773 * page is discarded. But before doing so, we update the overrun
774 * variable of the buffer.
776 static void rb_update_overflow(struct ring_buffer_per_cpu *cpu_buffer)
778 struct ring_buffer_event *event;
781 for (head = 0; head < rb_head_size(cpu_buffer);
782 head += rb_event_length(event)) {
784 event = __rb_page_index(cpu_buffer->head_page, head);
785 if (RB_WARN_ON(cpu_buffer, rb_null_event(event)))
787 /* Only count data entries */
788 if (event->type != RINGBUF_TYPE_DATA)
790 cpu_buffer->overrun++;
791 cpu_buffer->entries--;
795 static inline void rb_inc_page(struct ring_buffer_per_cpu *cpu_buffer,
796 struct buffer_page **bpage)
798 struct list_head *p = (*bpage)->list.next;
800 if (p == &cpu_buffer->pages)
803 *bpage = list_entry(p, struct buffer_page, list);
806 static inline unsigned
807 rb_event_index(struct ring_buffer_event *event)
809 unsigned long addr = (unsigned long)event;
811 return (addr & ~PAGE_MASK) - (PAGE_SIZE - BUF_PAGE_SIZE);
815 rb_is_commit(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 return cpu_buffer->commit_page->page == (void *)addr &&
825 rb_commit_index(cpu_buffer) == index;
829 rb_set_commit_event(struct ring_buffer_per_cpu *cpu_buffer,
830 struct ring_buffer_event *event)
832 unsigned long addr = (unsigned long)event;
835 index = rb_event_index(event);
838 while (cpu_buffer->commit_page->page != (void *)addr) {
839 if (RB_WARN_ON(cpu_buffer,
840 cpu_buffer->commit_page == cpu_buffer->tail_page))
842 cpu_buffer->commit_page->page->commit =
843 cpu_buffer->commit_page->write;
844 rb_inc_page(cpu_buffer, &cpu_buffer->commit_page);
845 cpu_buffer->write_stamp =
846 cpu_buffer->commit_page->page->time_stamp;
849 /* Now set the commit to the event's index */
850 local_set(&cpu_buffer->commit_page->page->commit, index);
854 rb_set_commit_to_write(struct ring_buffer_per_cpu *cpu_buffer)
857 * We only race with interrupts and NMIs on this CPU.
858 * If we own the commit event, then we can commit
859 * all others that interrupted us, since the interruptions
860 * are in stack format (they finish before they come
861 * back to us). This allows us to do a simple loop to
862 * assign the commit to the tail.
865 while (cpu_buffer->commit_page != cpu_buffer->tail_page) {
866 cpu_buffer->commit_page->page->commit =
867 cpu_buffer->commit_page->write;
868 rb_inc_page(cpu_buffer, &cpu_buffer->commit_page);
869 cpu_buffer->write_stamp =
870 cpu_buffer->commit_page->page->time_stamp;
871 /* add barrier to keep gcc from optimizing too much */
874 while (rb_commit_index(cpu_buffer) !=
875 rb_page_write(cpu_buffer->commit_page)) {
876 cpu_buffer->commit_page->page->commit =
877 cpu_buffer->commit_page->write;
881 /* again, keep gcc from optimizing */
885 * If an interrupt came in just after the first while loop
886 * and pushed the tail page forward, we will be left with
887 * a dangling commit that will never go forward.
889 if (unlikely(cpu_buffer->commit_page != cpu_buffer->tail_page))
893 static void rb_reset_reader_page(struct ring_buffer_per_cpu *cpu_buffer)
895 cpu_buffer->read_stamp = cpu_buffer->reader_page->page->time_stamp;
896 cpu_buffer->reader_page->read = 0;
899 static inline void rb_inc_iter(struct ring_buffer_iter *iter)
901 struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
904 * The iterator could be on the reader page (it starts there).
905 * But the head could have moved, since the reader was
906 * found. Check for this case and assign the iterator
907 * to the head page instead of next.
909 if (iter->head_page == cpu_buffer->reader_page)
910 iter->head_page = cpu_buffer->head_page;
912 rb_inc_page(cpu_buffer, &iter->head_page);
914 iter->read_stamp = iter->head_page->page->time_stamp;
919 * ring_buffer_update_event - update event type and data
920 * @event: the even to update
921 * @type: the type of event
922 * @length: the size of the event field in the ring buffer
924 * Update the type and data fields of the event. The length
925 * is the actual size that is written to the ring buffer,
926 * and with this, we can determine what to place into the
930 rb_update_event(struct ring_buffer_event *event,
931 unsigned type, unsigned length)
937 case RINGBUF_TYPE_PADDING:
940 case RINGBUF_TYPE_TIME_EXTEND:
942 (RB_LEN_TIME_EXTEND + (RB_ALIGNMENT-1))
943 >> RB_ALIGNMENT_SHIFT;
946 case RINGBUF_TYPE_TIME_STAMP:
948 (RB_LEN_TIME_STAMP + (RB_ALIGNMENT-1))
949 >> RB_ALIGNMENT_SHIFT;
952 case RINGBUF_TYPE_DATA:
953 length -= RB_EVNT_HDR_SIZE;
954 if (length > RB_MAX_SMALL_DATA) {
956 event->array[0] = length;
959 (length + (RB_ALIGNMENT-1))
960 >> RB_ALIGNMENT_SHIFT;
967 static inline unsigned rb_calculate_event_length(unsigned length)
969 struct ring_buffer_event event; /* Used only for sizeof array */
971 /* zero length can cause confusions */
975 if (length > RB_MAX_SMALL_DATA)
976 length += sizeof(event.array[0]);
978 length += RB_EVNT_HDR_SIZE;
979 length = ALIGN(length, RB_ALIGNMENT);
984 static struct ring_buffer_event *
985 __rb_reserve_next(struct ring_buffer_per_cpu *cpu_buffer,
986 unsigned type, unsigned long length, u64 *ts)
988 struct buffer_page *tail_page, *head_page, *reader_page, *commit_page;
989 unsigned long tail, write;
990 struct ring_buffer *buffer = cpu_buffer->buffer;
991 struct ring_buffer_event *event;
994 commit_page = cpu_buffer->commit_page;
995 /* we just need to protect against interrupts */
997 tail_page = cpu_buffer->tail_page;
998 write = local_add_return(length, &tail_page->write);
999 tail = write - length;
1001 /* See if we shot pass the end of this buffer page */
1002 if (write > BUF_PAGE_SIZE) {
1003 struct buffer_page *next_page = tail_page;
1005 local_irq_save(flags);
1006 __raw_spin_lock(&cpu_buffer->lock);
1008 rb_inc_page(cpu_buffer, &next_page);
1010 head_page = cpu_buffer->head_page;
1011 reader_page = cpu_buffer->reader_page;
1013 /* we grabbed the lock before incrementing */
1014 if (RB_WARN_ON(cpu_buffer, next_page == reader_page))
1018 * If for some reason, we had an interrupt storm that made
1019 * it all the way around the buffer, bail, and warn
1022 if (unlikely(next_page == commit_page)) {
1027 if (next_page == head_page) {
1028 if (!(buffer->flags & RB_FL_OVERWRITE))
1031 /* tail_page has not moved yet? */
1032 if (tail_page == cpu_buffer->tail_page) {
1033 /* count overflows */
1034 rb_update_overflow(cpu_buffer);
1036 rb_inc_page(cpu_buffer, &head_page);
1037 cpu_buffer->head_page = head_page;
1038 cpu_buffer->head_page->read = 0;
1043 * If the tail page is still the same as what we think
1044 * it is, then it is up to us to update the tail
1047 if (tail_page == cpu_buffer->tail_page) {
1048 local_set(&next_page->write, 0);
1049 local_set(&next_page->page->commit, 0);
1050 cpu_buffer->tail_page = next_page;
1052 /* reread the time stamp */
1053 *ts = ring_buffer_time_stamp(cpu_buffer->cpu);
1054 cpu_buffer->tail_page->page->time_stamp = *ts;
1058 * The actual tail page has moved forward.
1060 if (tail < BUF_PAGE_SIZE) {
1061 /* Mark the rest of the page with padding */
1062 event = __rb_page_index(tail_page, tail);
1063 event->type = RINGBUF_TYPE_PADDING;
1066 if (tail <= BUF_PAGE_SIZE)
1067 /* Set the write back to the previous setting */
1068 local_set(&tail_page->write, tail);
1071 * If this was a commit entry that failed,
1072 * increment that too
1074 if (tail_page == cpu_buffer->commit_page &&
1075 tail == rb_commit_index(cpu_buffer)) {
1076 rb_set_commit_to_write(cpu_buffer);
1079 __raw_spin_unlock(&cpu_buffer->lock);
1080 local_irq_restore(flags);
1082 /* fail and let the caller try again */
1083 return ERR_PTR(-EAGAIN);
1086 /* We reserved something on the buffer */
1088 if (RB_WARN_ON(cpu_buffer, write > BUF_PAGE_SIZE))
1091 event = __rb_page_index(tail_page, tail);
1092 rb_update_event(event, type, length);
1095 * If this is a commit and the tail is zero, then update
1096 * this page's time stamp.
1098 if (!tail && rb_is_commit(cpu_buffer, event))
1099 cpu_buffer->commit_page->page->time_stamp = *ts;
1105 if (tail <= BUF_PAGE_SIZE)
1106 local_set(&tail_page->write, tail);
1108 __raw_spin_unlock(&cpu_buffer->lock);
1109 local_irq_restore(flags);
1114 rb_add_time_stamp(struct ring_buffer_per_cpu *cpu_buffer,
1115 u64 *ts, u64 *delta)
1117 struct ring_buffer_event *event;
1121 if (unlikely(*delta > (1ULL << 59) && !once++)) {
1122 printk(KERN_WARNING "Delta way too big! %llu"
1123 " ts=%llu write stamp = %llu\n",
1124 (unsigned long long)*delta,
1125 (unsigned long long)*ts,
1126 (unsigned long long)cpu_buffer->write_stamp);
1131 * The delta is too big, we to add a
1134 event = __rb_reserve_next(cpu_buffer,
1135 RINGBUF_TYPE_TIME_EXTEND,
1141 if (PTR_ERR(event) == -EAGAIN)
1144 /* Only a commited time event can update the write stamp */
1145 if (rb_is_commit(cpu_buffer, event)) {
1147 * If this is the first on the page, then we need to
1148 * update the page itself, and just put in a zero.
1150 if (rb_event_index(event)) {
1151 event->time_delta = *delta & TS_MASK;
1152 event->array[0] = *delta >> TS_SHIFT;
1154 cpu_buffer->commit_page->page->time_stamp = *ts;
1155 event->time_delta = 0;
1156 event->array[0] = 0;
1158 cpu_buffer->write_stamp = *ts;
1159 /* let the caller know this was the commit */
1162 /* Darn, this is just wasted space */
1163 event->time_delta = 0;
1164 event->array[0] = 0;
1173 static struct ring_buffer_event *
1174 rb_reserve_next_event(struct ring_buffer_per_cpu *cpu_buffer,
1175 unsigned type, unsigned long length)
1177 struct ring_buffer_event *event;
1184 * We allow for interrupts to reenter here and do a trace.
1185 * If one does, it will cause this original code to loop
1186 * back here. Even with heavy interrupts happening, this
1187 * should only happen a few times in a row. If this happens
1188 * 1000 times in a row, there must be either an interrupt
1189 * storm or we have something buggy.
1192 if (RB_WARN_ON(cpu_buffer, ++nr_loops > 1000))
1195 ts = ring_buffer_time_stamp(cpu_buffer->cpu);
1198 * Only the first commit can update the timestamp.
1199 * Yes there is a race here. If an interrupt comes in
1200 * just after the conditional and it traces too, then it
1201 * will also check the deltas. More than one timestamp may
1202 * also be made. But only the entry that did the actual
1203 * commit will be something other than zero.
1205 if (cpu_buffer->tail_page == cpu_buffer->commit_page &&
1206 rb_page_write(cpu_buffer->tail_page) ==
1207 rb_commit_index(cpu_buffer)) {
1209 delta = ts - cpu_buffer->write_stamp;
1211 /* make sure this delta is calculated here */
1214 /* Did the write stamp get updated already? */
1215 if (unlikely(ts < cpu_buffer->write_stamp))
1218 if (test_time_stamp(delta)) {
1220 commit = rb_add_time_stamp(cpu_buffer, &ts, &delta);
1222 if (commit == -EBUSY)
1225 if (commit == -EAGAIN)
1228 RB_WARN_ON(cpu_buffer, commit < 0);
1231 /* Non commits have zero deltas */
1234 event = __rb_reserve_next(cpu_buffer, type, length, &ts);
1235 if (PTR_ERR(event) == -EAGAIN)
1239 if (unlikely(commit))
1241 * Ouch! We needed a timestamp and it was commited. But
1242 * we didn't get our event reserved.
1244 rb_set_commit_to_write(cpu_buffer);
1249 * If the timestamp was commited, make the commit our entry
1250 * now so that we will update it when needed.
1253 rb_set_commit_event(cpu_buffer, event);
1254 else if (!rb_is_commit(cpu_buffer, event))
1257 event->time_delta = delta;
1262 static DEFINE_PER_CPU(int, rb_need_resched);
1265 * ring_buffer_lock_reserve - reserve a part of the buffer
1266 * @buffer: the ring buffer to reserve from
1267 * @length: the length of the data to reserve (excluding event header)
1268 * @flags: a pointer to save the interrupt flags
1270 * Returns a reseverd event on the ring buffer to copy directly to.
1271 * The user of this interface will need to get the body to write into
1272 * and can use the ring_buffer_event_data() interface.
1274 * The length is the length of the data needed, not the event length
1275 * which also includes the event header.
1277 * Must be paired with ring_buffer_unlock_commit, unless NULL is returned.
1278 * If NULL is returned, then nothing has been allocated or locked.
1280 struct ring_buffer_event *
1281 ring_buffer_lock_reserve(struct ring_buffer *buffer,
1282 unsigned long length,
1283 unsigned long *flags)
1285 struct ring_buffer_per_cpu *cpu_buffer;
1286 struct ring_buffer_event *event;
1289 if (ring_buffer_flags != RB_BUFFERS_ON)
1292 if (atomic_read(&buffer->record_disabled))
1295 /* If we are tracing schedule, we don't want to recurse */
1296 resched = ftrace_preempt_disable();
1298 cpu = raw_smp_processor_id();
1300 if (!cpumask_test_cpu(cpu, buffer->cpumask))
1303 cpu_buffer = buffer->buffers[cpu];
1305 if (atomic_read(&cpu_buffer->record_disabled))
1308 length = rb_calculate_event_length(length);
1309 if (length > BUF_PAGE_SIZE)
1312 event = rb_reserve_next_event(cpu_buffer, RINGBUF_TYPE_DATA, length);
1317 * Need to store resched state on this cpu.
1318 * Only the first needs to.
1321 if (preempt_count() == 1)
1322 per_cpu(rb_need_resched, cpu) = resched;
1327 ftrace_preempt_enable(resched);
1330 EXPORT_SYMBOL_GPL(ring_buffer_lock_reserve);
1332 static void rb_commit(struct ring_buffer_per_cpu *cpu_buffer,
1333 struct ring_buffer_event *event)
1335 cpu_buffer->entries++;
1337 /* Only process further if we own the commit */
1338 if (!rb_is_commit(cpu_buffer, event))
1341 cpu_buffer->write_stamp += event->time_delta;
1343 rb_set_commit_to_write(cpu_buffer);
1347 * ring_buffer_unlock_commit - commit a reserved
1348 * @buffer: The buffer to commit to
1349 * @event: The event pointer to commit.
1350 * @flags: the interrupt flags received from ring_buffer_lock_reserve.
1352 * This commits the data to the ring buffer, and releases any locks held.
1354 * Must be paired with ring_buffer_lock_reserve.
1356 int ring_buffer_unlock_commit(struct ring_buffer *buffer,
1357 struct ring_buffer_event *event,
1358 unsigned long flags)
1360 struct ring_buffer_per_cpu *cpu_buffer;
1361 int cpu = raw_smp_processor_id();
1363 cpu_buffer = buffer->buffers[cpu];
1365 rb_commit(cpu_buffer, event);
1368 * Only the last preempt count needs to restore preemption.
1370 if (preempt_count() == 1)
1371 ftrace_preempt_enable(per_cpu(rb_need_resched, cpu));
1373 preempt_enable_no_resched_notrace();
1377 EXPORT_SYMBOL_GPL(ring_buffer_unlock_commit);
1380 * ring_buffer_write - write data to the buffer without reserving
1381 * @buffer: The ring buffer to write to.
1382 * @length: The length of the data being written (excluding the event header)
1383 * @data: The data to write to the buffer.
1385 * This is like ring_buffer_lock_reserve and ring_buffer_unlock_commit as
1386 * one function. If you already have the data to write to the buffer, it
1387 * may be easier to simply call this function.
1389 * Note, like ring_buffer_lock_reserve, the length is the length of the data
1390 * and not the length of the event which would hold the header.
1392 int ring_buffer_write(struct ring_buffer *buffer,
1393 unsigned long length,
1396 struct ring_buffer_per_cpu *cpu_buffer;
1397 struct ring_buffer_event *event;
1398 unsigned long event_length;
1403 if (ring_buffer_flags != RB_BUFFERS_ON)
1406 if (atomic_read(&buffer->record_disabled))
1409 resched = ftrace_preempt_disable();
1411 cpu = raw_smp_processor_id();
1413 if (!cpumask_test_cpu(cpu, buffer->cpumask))
1416 cpu_buffer = buffer->buffers[cpu];
1418 if (atomic_read(&cpu_buffer->record_disabled))
1421 event_length = rb_calculate_event_length(length);
1422 event = rb_reserve_next_event(cpu_buffer,
1423 RINGBUF_TYPE_DATA, event_length);
1427 body = rb_event_data(event);
1429 memcpy(body, data, length);
1431 rb_commit(cpu_buffer, event);
1435 ftrace_preempt_enable(resched);
1439 EXPORT_SYMBOL_GPL(ring_buffer_write);
1441 static inline int rb_per_cpu_empty(struct ring_buffer_per_cpu *cpu_buffer)
1443 struct buffer_page *reader = cpu_buffer->reader_page;
1444 struct buffer_page *head = cpu_buffer->head_page;
1445 struct buffer_page *commit = cpu_buffer->commit_page;
1447 return reader->read == rb_page_commit(reader) &&
1448 (commit == reader ||
1450 head->read == rb_page_commit(commit)));
1454 * ring_buffer_record_disable - stop all writes into the buffer
1455 * @buffer: The ring buffer to stop writes to.
1457 * This prevents all writes to the buffer. Any attempt to write
1458 * to the buffer after this will fail and return NULL.
1460 * The caller should call synchronize_sched() after this.
1462 void ring_buffer_record_disable(struct ring_buffer *buffer)
1464 atomic_inc(&buffer->record_disabled);
1466 EXPORT_SYMBOL_GPL(ring_buffer_record_disable);
1469 * ring_buffer_record_enable - enable writes to the buffer
1470 * @buffer: The ring buffer to enable writes
1472 * Note, multiple disables will need the same number of enables
1473 * to truely enable the writing (much like preempt_disable).
1475 void ring_buffer_record_enable(struct ring_buffer *buffer)
1477 atomic_dec(&buffer->record_disabled);
1479 EXPORT_SYMBOL_GPL(ring_buffer_record_enable);
1482 * ring_buffer_record_disable_cpu - stop all writes into the cpu_buffer
1483 * @buffer: The ring buffer to stop writes to.
1484 * @cpu: The CPU buffer to stop
1486 * This prevents all writes to the buffer. Any attempt to write
1487 * to the buffer after this will fail and return NULL.
1489 * The caller should call synchronize_sched() after this.
1491 void ring_buffer_record_disable_cpu(struct ring_buffer *buffer, int cpu)
1493 struct ring_buffer_per_cpu *cpu_buffer;
1495 if (!cpumask_test_cpu(cpu, buffer->cpumask))
1498 cpu_buffer = buffer->buffers[cpu];
1499 atomic_inc(&cpu_buffer->record_disabled);
1501 EXPORT_SYMBOL_GPL(ring_buffer_record_disable_cpu);
1504 * ring_buffer_record_enable_cpu - enable writes to the buffer
1505 * @buffer: The ring buffer to enable writes
1506 * @cpu: The CPU to enable.
1508 * Note, multiple disables will need the same number of enables
1509 * to truely enable the writing (much like preempt_disable).
1511 void ring_buffer_record_enable_cpu(struct ring_buffer *buffer, int cpu)
1513 struct ring_buffer_per_cpu *cpu_buffer;
1515 if (!cpumask_test_cpu(cpu, buffer->cpumask))
1518 cpu_buffer = buffer->buffers[cpu];
1519 atomic_dec(&cpu_buffer->record_disabled);
1521 EXPORT_SYMBOL_GPL(ring_buffer_record_enable_cpu);
1524 * ring_buffer_entries_cpu - get the number of entries in a cpu buffer
1525 * @buffer: The ring buffer
1526 * @cpu: The per CPU buffer to get the entries from.
1528 unsigned long ring_buffer_entries_cpu(struct ring_buffer *buffer, int cpu)
1530 struct ring_buffer_per_cpu *cpu_buffer;
1532 if (!cpumask_test_cpu(cpu, buffer->cpumask))
1535 cpu_buffer = buffer->buffers[cpu];
1536 return cpu_buffer->entries;
1538 EXPORT_SYMBOL_GPL(ring_buffer_entries_cpu);
1541 * ring_buffer_overrun_cpu - get the number of overruns in a cpu_buffer
1542 * @buffer: The ring buffer
1543 * @cpu: The per CPU buffer to get the number of overruns from
1545 unsigned long ring_buffer_overrun_cpu(struct ring_buffer *buffer, int cpu)
1547 struct ring_buffer_per_cpu *cpu_buffer;
1549 if (!cpumask_test_cpu(cpu, buffer->cpumask))
1552 cpu_buffer = buffer->buffers[cpu];
1553 return cpu_buffer->overrun;
1555 EXPORT_SYMBOL_GPL(ring_buffer_overrun_cpu);
1558 * ring_buffer_entries - get the number of entries in a buffer
1559 * @buffer: The ring buffer
1561 * Returns the total number of entries in the ring buffer
1564 unsigned long ring_buffer_entries(struct ring_buffer *buffer)
1566 struct ring_buffer_per_cpu *cpu_buffer;
1567 unsigned long entries = 0;
1570 /* if you care about this being correct, lock the buffer */
1571 for_each_buffer_cpu(buffer, cpu) {
1572 cpu_buffer = buffer->buffers[cpu];
1573 entries += cpu_buffer->entries;
1578 EXPORT_SYMBOL_GPL(ring_buffer_entries);
1581 * ring_buffer_overrun_cpu - get the number of overruns in buffer
1582 * @buffer: The ring buffer
1584 * Returns the total number of overruns in the ring buffer
1587 unsigned long ring_buffer_overruns(struct ring_buffer *buffer)
1589 struct ring_buffer_per_cpu *cpu_buffer;
1590 unsigned long overruns = 0;
1593 /* if you care about this being correct, lock the buffer */
1594 for_each_buffer_cpu(buffer, cpu) {
1595 cpu_buffer = buffer->buffers[cpu];
1596 overruns += cpu_buffer->overrun;
1601 EXPORT_SYMBOL_GPL(ring_buffer_overruns);
1603 static void rb_iter_reset(struct ring_buffer_iter *iter)
1605 struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
1607 /* Iterator usage is expected to have record disabled */
1608 if (list_empty(&cpu_buffer->reader_page->list)) {
1609 iter->head_page = cpu_buffer->head_page;
1610 iter->head = cpu_buffer->head_page->read;
1612 iter->head_page = cpu_buffer->reader_page;
1613 iter->head = cpu_buffer->reader_page->read;
1616 iter->read_stamp = cpu_buffer->read_stamp;
1618 iter->read_stamp = iter->head_page->page->time_stamp;
1622 * ring_buffer_iter_reset - reset an iterator
1623 * @iter: The iterator to reset
1625 * Resets the iterator, so that it will start from the beginning
1628 void ring_buffer_iter_reset(struct ring_buffer_iter *iter)
1630 struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
1631 unsigned long flags;
1633 spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
1634 rb_iter_reset(iter);
1635 spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
1637 EXPORT_SYMBOL_GPL(ring_buffer_iter_reset);
1640 * ring_buffer_iter_empty - check if an iterator has no more to read
1641 * @iter: The iterator to check
1643 int ring_buffer_iter_empty(struct ring_buffer_iter *iter)
1645 struct ring_buffer_per_cpu *cpu_buffer;
1647 cpu_buffer = iter->cpu_buffer;
1649 return iter->head_page == cpu_buffer->commit_page &&
1650 iter->head == rb_commit_index(cpu_buffer);
1652 EXPORT_SYMBOL_GPL(ring_buffer_iter_empty);
1655 rb_update_read_stamp(struct ring_buffer_per_cpu *cpu_buffer,
1656 struct ring_buffer_event *event)
1660 switch (event->type) {
1661 case RINGBUF_TYPE_PADDING:
1664 case RINGBUF_TYPE_TIME_EXTEND:
1665 delta = event->array[0];
1667 delta += event->time_delta;
1668 cpu_buffer->read_stamp += delta;
1671 case RINGBUF_TYPE_TIME_STAMP:
1672 /* FIXME: not implemented */
1675 case RINGBUF_TYPE_DATA:
1676 cpu_buffer->read_stamp += event->time_delta;
1686 rb_update_iter_read_stamp(struct ring_buffer_iter *iter,
1687 struct ring_buffer_event *event)
1691 switch (event->type) {
1692 case RINGBUF_TYPE_PADDING:
1695 case RINGBUF_TYPE_TIME_EXTEND:
1696 delta = event->array[0];
1698 delta += event->time_delta;
1699 iter->read_stamp += delta;
1702 case RINGBUF_TYPE_TIME_STAMP:
1703 /* FIXME: not implemented */
1706 case RINGBUF_TYPE_DATA:
1707 iter->read_stamp += event->time_delta;
1716 static struct buffer_page *
1717 rb_get_reader_page(struct ring_buffer_per_cpu *cpu_buffer)
1719 struct buffer_page *reader = NULL;
1720 unsigned long flags;
1723 local_irq_save(flags);
1724 __raw_spin_lock(&cpu_buffer->lock);
1728 * This should normally only loop twice. But because the
1729 * start of the reader inserts an empty page, it causes
1730 * a case where we will loop three times. There should be no
1731 * reason to loop four times (that I know of).
1733 if (RB_WARN_ON(cpu_buffer, ++nr_loops > 3)) {
1738 reader = cpu_buffer->reader_page;
1740 /* If there's more to read, return this page */
1741 if (cpu_buffer->reader_page->read < rb_page_size(reader))
1744 /* Never should we have an index greater than the size */
1745 if (RB_WARN_ON(cpu_buffer,
1746 cpu_buffer->reader_page->read > rb_page_size(reader)))
1749 /* check if we caught up to the tail */
1751 if (cpu_buffer->commit_page == cpu_buffer->reader_page)
1755 * Splice the empty reader page into the list around the head.
1756 * Reset the reader page to size zero.
1759 reader = cpu_buffer->head_page;
1760 cpu_buffer->reader_page->list.next = reader->list.next;
1761 cpu_buffer->reader_page->list.prev = reader->list.prev;
1763 local_set(&cpu_buffer->reader_page->write, 0);
1764 local_set(&cpu_buffer->reader_page->page->commit, 0);
1766 /* Make the reader page now replace the head */
1767 reader->list.prev->next = &cpu_buffer->reader_page->list;
1768 reader->list.next->prev = &cpu_buffer->reader_page->list;
1771 * If the tail is on the reader, then we must set the head
1772 * to the inserted page, otherwise we set it one before.
1774 cpu_buffer->head_page = cpu_buffer->reader_page;
1776 if (cpu_buffer->commit_page != reader)
1777 rb_inc_page(cpu_buffer, &cpu_buffer->head_page);
1779 /* Finally update the reader page to the new head */
1780 cpu_buffer->reader_page = reader;
1781 rb_reset_reader_page(cpu_buffer);
1786 __raw_spin_unlock(&cpu_buffer->lock);
1787 local_irq_restore(flags);
1792 static void rb_advance_reader(struct ring_buffer_per_cpu *cpu_buffer)
1794 struct ring_buffer_event *event;
1795 struct buffer_page *reader;
1798 reader = rb_get_reader_page(cpu_buffer);
1800 /* This function should not be called when buffer is empty */
1801 if (RB_WARN_ON(cpu_buffer, !reader))
1804 event = rb_reader_event(cpu_buffer);
1806 if (event->type == RINGBUF_TYPE_DATA)
1807 cpu_buffer->entries--;
1809 rb_update_read_stamp(cpu_buffer, event);
1811 length = rb_event_length(event);
1812 cpu_buffer->reader_page->read += length;
1815 static void rb_advance_iter(struct ring_buffer_iter *iter)
1817 struct ring_buffer *buffer;
1818 struct ring_buffer_per_cpu *cpu_buffer;
1819 struct ring_buffer_event *event;
1822 cpu_buffer = iter->cpu_buffer;
1823 buffer = cpu_buffer->buffer;
1826 * Check if we are at the end of the buffer.
1828 if (iter->head >= rb_page_size(iter->head_page)) {
1829 if (RB_WARN_ON(buffer,
1830 iter->head_page == cpu_buffer->commit_page))
1836 event = rb_iter_head_event(iter);
1838 length = rb_event_length(event);
1841 * This should not be called to advance the header if we are
1842 * at the tail of the buffer.
1844 if (RB_WARN_ON(cpu_buffer,
1845 (iter->head_page == cpu_buffer->commit_page) &&
1846 (iter->head + length > rb_commit_index(cpu_buffer))))
1849 rb_update_iter_read_stamp(iter, event);
1851 iter->head += length;
1853 /* check for end of page padding */
1854 if ((iter->head >= rb_page_size(iter->head_page)) &&
1855 (iter->head_page != cpu_buffer->commit_page))
1856 rb_advance_iter(iter);
1859 static struct ring_buffer_event *
1860 rb_buffer_peek(struct ring_buffer *buffer, int cpu, u64 *ts)
1862 struct ring_buffer_per_cpu *cpu_buffer;
1863 struct ring_buffer_event *event;
1864 struct buffer_page *reader;
1867 if (!cpumask_test_cpu(cpu, buffer->cpumask))
1870 cpu_buffer = buffer->buffers[cpu];
1874 * We repeat when a timestamp is encountered. It is possible
1875 * to get multiple timestamps from an interrupt entering just
1876 * as one timestamp is about to be written. The max times
1877 * that this can happen is the number of nested interrupts we
1878 * can have. Nesting 10 deep of interrupts is clearly
1881 if (RB_WARN_ON(cpu_buffer, ++nr_loops > 10))
1884 reader = rb_get_reader_page(cpu_buffer);
1888 event = rb_reader_event(cpu_buffer);
1890 switch (event->type) {
1891 case RINGBUF_TYPE_PADDING:
1892 RB_WARN_ON(cpu_buffer, 1);
1893 rb_advance_reader(cpu_buffer);
1896 case RINGBUF_TYPE_TIME_EXTEND:
1897 /* Internal data, OK to advance */
1898 rb_advance_reader(cpu_buffer);
1901 case RINGBUF_TYPE_TIME_STAMP:
1902 /* FIXME: not implemented */
1903 rb_advance_reader(cpu_buffer);
1906 case RINGBUF_TYPE_DATA:
1908 *ts = cpu_buffer->read_stamp + event->time_delta;
1909 ring_buffer_normalize_time_stamp(cpu_buffer->cpu, ts);
1919 EXPORT_SYMBOL_GPL(ring_buffer_peek);
1921 static struct ring_buffer_event *
1922 rb_iter_peek(struct ring_buffer_iter *iter, u64 *ts)
1924 struct ring_buffer *buffer;
1925 struct ring_buffer_per_cpu *cpu_buffer;
1926 struct ring_buffer_event *event;
1929 if (ring_buffer_iter_empty(iter))
1932 cpu_buffer = iter->cpu_buffer;
1933 buffer = cpu_buffer->buffer;
1937 * We repeat when a timestamp is encountered. It is possible
1938 * to get multiple timestamps from an interrupt entering just
1939 * as one timestamp is about to be written. The max times
1940 * that this can happen is the number of nested interrupts we
1941 * can have. Nesting 10 deep of interrupts is clearly
1944 if (RB_WARN_ON(cpu_buffer, ++nr_loops > 10))
1947 if (rb_per_cpu_empty(cpu_buffer))
1950 event = rb_iter_head_event(iter);
1952 switch (event->type) {
1953 case RINGBUF_TYPE_PADDING:
1957 case RINGBUF_TYPE_TIME_EXTEND:
1958 /* Internal data, OK to advance */
1959 rb_advance_iter(iter);
1962 case RINGBUF_TYPE_TIME_STAMP:
1963 /* FIXME: not implemented */
1964 rb_advance_iter(iter);
1967 case RINGBUF_TYPE_DATA:
1969 *ts = iter->read_stamp + event->time_delta;
1970 ring_buffer_normalize_time_stamp(cpu_buffer->cpu, ts);
1980 EXPORT_SYMBOL_GPL(ring_buffer_iter_peek);
1983 * ring_buffer_peek - peek at the next event to be read
1984 * @buffer: The ring buffer to read
1985 * @cpu: The cpu to peak at
1986 * @ts: The timestamp counter of this event.
1988 * This will return the event that will be read next, but does
1989 * not consume the data.
1991 struct ring_buffer_event *
1992 ring_buffer_peek(struct ring_buffer *buffer, int cpu, u64 *ts)
1994 struct ring_buffer_per_cpu *cpu_buffer = buffer->buffers[cpu];
1995 struct ring_buffer_event *event;
1996 unsigned long flags;
1998 spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
1999 event = rb_buffer_peek(buffer, cpu, ts);
2000 spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
2006 * ring_buffer_iter_peek - peek at the next event to be read
2007 * @iter: The ring buffer iterator
2008 * @ts: The timestamp counter of this event.
2010 * This will return the event that will be read next, but does
2011 * not increment the iterator.
2013 struct ring_buffer_event *
2014 ring_buffer_iter_peek(struct ring_buffer_iter *iter, u64 *ts)
2016 struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
2017 struct ring_buffer_event *event;
2018 unsigned long flags;
2020 spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
2021 event = rb_iter_peek(iter, ts);
2022 spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
2028 * ring_buffer_consume - return an event and consume it
2029 * @buffer: The ring buffer to get the next event from
2031 * Returns the next event in the ring buffer, and that event is consumed.
2032 * Meaning, that sequential reads will keep returning a different event,
2033 * and eventually empty the ring buffer if the producer is slower.
2035 struct ring_buffer_event *
2036 ring_buffer_consume(struct ring_buffer *buffer, int cpu, u64 *ts)
2038 struct ring_buffer_per_cpu *cpu_buffer = buffer->buffers[cpu];
2039 struct ring_buffer_event *event;
2040 unsigned long flags;
2042 if (!cpumask_test_cpu(cpu, buffer->cpumask))
2045 spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
2047 event = rb_buffer_peek(buffer, cpu, ts);
2051 rb_advance_reader(cpu_buffer);
2054 spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
2058 EXPORT_SYMBOL_GPL(ring_buffer_consume);
2061 * ring_buffer_read_start - start a non consuming read of the buffer
2062 * @buffer: The ring buffer to read from
2063 * @cpu: The cpu buffer to iterate over
2065 * This starts up an iteration through the buffer. It also disables
2066 * the recording to the buffer until the reading is finished.
2067 * This prevents the reading from being corrupted. This is not
2068 * a consuming read, so a producer is not expected.
2070 * Must be paired with ring_buffer_finish.
2072 struct ring_buffer_iter *
2073 ring_buffer_read_start(struct ring_buffer *buffer, int cpu)
2075 struct ring_buffer_per_cpu *cpu_buffer;
2076 struct ring_buffer_iter *iter;
2077 unsigned long flags;
2079 if (!cpumask_test_cpu(cpu, buffer->cpumask))
2082 iter = kmalloc(sizeof(*iter), GFP_KERNEL);
2086 cpu_buffer = buffer->buffers[cpu];
2088 iter->cpu_buffer = cpu_buffer;
2090 atomic_inc(&cpu_buffer->record_disabled);
2091 synchronize_sched();
2093 spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
2094 __raw_spin_lock(&cpu_buffer->lock);
2095 rb_iter_reset(iter);
2096 __raw_spin_unlock(&cpu_buffer->lock);
2097 spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
2101 EXPORT_SYMBOL_GPL(ring_buffer_read_start);
2104 * ring_buffer_finish - finish reading the iterator of the buffer
2105 * @iter: The iterator retrieved by ring_buffer_start
2107 * This re-enables the recording to the buffer, and frees the
2111 ring_buffer_read_finish(struct ring_buffer_iter *iter)
2113 struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
2115 atomic_dec(&cpu_buffer->record_disabled);
2118 EXPORT_SYMBOL_GPL(ring_buffer_read_finish);
2121 * ring_buffer_read - read the next item in the ring buffer by the iterator
2122 * @iter: The ring buffer iterator
2123 * @ts: The time stamp of the event read.
2125 * This reads the next event in the ring buffer and increments the iterator.
2127 struct ring_buffer_event *
2128 ring_buffer_read(struct ring_buffer_iter *iter, u64 *ts)
2130 struct ring_buffer_event *event;
2131 struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
2132 unsigned long flags;
2134 spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
2135 event = rb_iter_peek(iter, ts);
2139 rb_advance_iter(iter);
2141 spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
2145 EXPORT_SYMBOL_GPL(ring_buffer_read);
2148 * ring_buffer_size - return the size of the ring buffer (in bytes)
2149 * @buffer: The ring buffer.
2151 unsigned long ring_buffer_size(struct ring_buffer *buffer)
2153 return BUF_PAGE_SIZE * buffer->pages;
2155 EXPORT_SYMBOL_GPL(ring_buffer_size);
2158 rb_reset_cpu(struct ring_buffer_per_cpu *cpu_buffer)
2160 cpu_buffer->head_page
2161 = list_entry(cpu_buffer->pages.next, struct buffer_page, list);
2162 local_set(&cpu_buffer->head_page->write, 0);
2163 local_set(&cpu_buffer->head_page->page->commit, 0);
2165 cpu_buffer->head_page->read = 0;
2167 cpu_buffer->tail_page = cpu_buffer->head_page;
2168 cpu_buffer->commit_page = cpu_buffer->head_page;
2170 INIT_LIST_HEAD(&cpu_buffer->reader_page->list);
2171 local_set(&cpu_buffer->reader_page->write, 0);
2172 local_set(&cpu_buffer->reader_page->page->commit, 0);
2173 cpu_buffer->reader_page->read = 0;
2175 cpu_buffer->overrun = 0;
2176 cpu_buffer->entries = 0;
2178 cpu_buffer->write_stamp = 0;
2179 cpu_buffer->read_stamp = 0;
2183 * ring_buffer_reset_cpu - reset a ring buffer per CPU buffer
2184 * @buffer: The ring buffer to reset a per cpu buffer of
2185 * @cpu: The CPU buffer to be reset
2187 void ring_buffer_reset_cpu(struct ring_buffer *buffer, int cpu)
2189 struct ring_buffer_per_cpu *cpu_buffer = buffer->buffers[cpu];
2190 unsigned long flags;
2192 if (!cpumask_test_cpu(cpu, buffer->cpumask))
2195 spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
2197 __raw_spin_lock(&cpu_buffer->lock);
2199 rb_reset_cpu(cpu_buffer);
2201 __raw_spin_unlock(&cpu_buffer->lock);
2203 spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
2205 EXPORT_SYMBOL_GPL(ring_buffer_reset_cpu);
2208 * ring_buffer_reset - reset a ring buffer
2209 * @buffer: The ring buffer to reset all cpu buffers
2211 void ring_buffer_reset(struct ring_buffer *buffer)
2215 for_each_buffer_cpu(buffer, cpu)
2216 ring_buffer_reset_cpu(buffer, cpu);
2218 EXPORT_SYMBOL_GPL(ring_buffer_reset);
2221 * rind_buffer_empty - is the ring buffer empty?
2222 * @buffer: The ring buffer to test
2224 int ring_buffer_empty(struct ring_buffer *buffer)
2226 struct ring_buffer_per_cpu *cpu_buffer;
2229 /* yes this is racy, but if you don't like the race, lock the buffer */
2230 for_each_buffer_cpu(buffer, cpu) {
2231 cpu_buffer = buffer->buffers[cpu];
2232 if (!rb_per_cpu_empty(cpu_buffer))
2237 EXPORT_SYMBOL_GPL(ring_buffer_empty);
2240 * ring_buffer_empty_cpu - is a cpu buffer of a ring buffer empty?
2241 * @buffer: The ring buffer
2242 * @cpu: The CPU buffer to test
2244 int ring_buffer_empty_cpu(struct ring_buffer *buffer, int cpu)
2246 struct ring_buffer_per_cpu *cpu_buffer;
2248 if (!cpumask_test_cpu(cpu, buffer->cpumask))
2251 cpu_buffer = buffer->buffers[cpu];
2252 return rb_per_cpu_empty(cpu_buffer);
2254 EXPORT_SYMBOL_GPL(ring_buffer_empty_cpu);
2257 * ring_buffer_swap_cpu - swap a CPU buffer between two ring buffers
2258 * @buffer_a: One buffer to swap with
2259 * @buffer_b: The other buffer to swap with
2261 * This function is useful for tracers that want to take a "snapshot"
2262 * of a CPU buffer and has another back up buffer lying around.
2263 * it is expected that the tracer handles the cpu buffer not being
2264 * used at the moment.
2266 int ring_buffer_swap_cpu(struct ring_buffer *buffer_a,
2267 struct ring_buffer *buffer_b, int cpu)
2269 struct ring_buffer_per_cpu *cpu_buffer_a;
2270 struct ring_buffer_per_cpu *cpu_buffer_b;
2272 if (!cpumask_test_cpu(cpu, buffer_a->cpumask) ||
2273 !cpumask_test_cpu(cpu, buffer_b->cpumask))
2276 /* At least make sure the two buffers are somewhat the same */
2277 if (buffer_a->pages != buffer_b->pages)
2280 cpu_buffer_a = buffer_a->buffers[cpu];
2281 cpu_buffer_b = buffer_b->buffers[cpu];
2284 * We can't do a synchronize_sched here because this
2285 * function can be called in atomic context.
2286 * Normally this will be called from the same CPU as cpu.
2287 * If not it's up to the caller to protect this.
2289 atomic_inc(&cpu_buffer_a->record_disabled);
2290 atomic_inc(&cpu_buffer_b->record_disabled);
2292 buffer_a->buffers[cpu] = cpu_buffer_b;
2293 buffer_b->buffers[cpu] = cpu_buffer_a;
2295 cpu_buffer_b->buffer = buffer_a;
2296 cpu_buffer_a->buffer = buffer_b;
2298 atomic_dec(&cpu_buffer_a->record_disabled);
2299 atomic_dec(&cpu_buffer_b->record_disabled);
2303 EXPORT_SYMBOL_GPL(ring_buffer_swap_cpu);
2305 static void rb_remove_entries(struct ring_buffer_per_cpu *cpu_buffer,
2306 struct buffer_data_page *bpage)
2308 struct ring_buffer_event *event;
2311 __raw_spin_lock(&cpu_buffer->lock);
2312 for (head = 0; head < local_read(&bpage->commit);
2313 head += rb_event_length(event)) {
2315 event = __rb_data_page_index(bpage, head);
2316 if (RB_WARN_ON(cpu_buffer, rb_null_event(event)))
2318 /* Only count data entries */
2319 if (event->type != RINGBUF_TYPE_DATA)
2321 cpu_buffer->entries--;
2323 __raw_spin_unlock(&cpu_buffer->lock);
2327 * ring_buffer_alloc_read_page - allocate a page to read from buffer
2328 * @buffer: the buffer to allocate for.
2330 * This function is used in conjunction with ring_buffer_read_page.
2331 * When reading a full page from the ring buffer, these functions
2332 * can be used to speed up the process. The calling function should
2333 * allocate a few pages first with this function. Then when it
2334 * needs to get pages from the ring buffer, it passes the result
2335 * of this function into ring_buffer_read_page, which will swap
2336 * the page that was allocated, with the read page of the buffer.
2339 * The page allocated, or NULL on error.
2341 void *ring_buffer_alloc_read_page(struct ring_buffer *buffer)
2344 struct buffer_data_page *bpage;
2346 addr = __get_free_page(GFP_KERNEL);
2350 bpage = (void *)addr;
2356 * ring_buffer_free_read_page - free an allocated read page
2357 * @buffer: the buffer the page was allocate for
2358 * @data: the page to free
2360 * Free a page allocated from ring_buffer_alloc_read_page.
2362 void ring_buffer_free_read_page(struct ring_buffer *buffer, void *data)
2364 free_page((unsigned long)data);
2368 * ring_buffer_read_page - extract a page from the ring buffer
2369 * @buffer: buffer to extract from
2370 * @data_page: the page to use allocated from ring_buffer_alloc_read_page
2371 * @cpu: the cpu of the buffer to extract
2372 * @full: should the extraction only happen when the page is full.
2374 * This function will pull out a page from the ring buffer and consume it.
2375 * @data_page must be the address of the variable that was returned
2376 * from ring_buffer_alloc_read_page. This is because the page might be used
2377 * to swap with a page in the ring buffer.
2380 * rpage = ring_buffer_alloc_page(buffer);
2383 * ret = ring_buffer_read_page(buffer, &rpage, cpu, 0);
2385 * process_page(rpage);
2387 * When @full is set, the function will not return true unless
2388 * the writer is off the reader page.
2390 * Note: it is up to the calling functions to handle sleeps and wakeups.
2391 * The ring buffer can be used anywhere in the kernel and can not
2392 * blindly call wake_up. The layer that uses the ring buffer must be
2393 * responsible for that.
2396 * 1 if data has been transferred
2397 * 0 if no data has been transferred.
2399 int ring_buffer_read_page(struct ring_buffer *buffer,
2400 void **data_page, int cpu, int full)
2402 struct ring_buffer_per_cpu *cpu_buffer = buffer->buffers[cpu];
2403 struct ring_buffer_event *event;
2404 struct buffer_data_page *bpage;
2405 unsigned long flags;
2415 spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
2418 * rb_buffer_peek will get the next ring buffer if
2419 * the current reader page is empty.
2421 event = rb_buffer_peek(buffer, cpu, NULL);
2425 /* check for data */
2426 if (!local_read(&cpu_buffer->reader_page->page->commit))
2429 * If the writer is already off of the read page, then simply
2430 * switch the read page with the given page. Otherwise
2431 * we need to copy the data from the reader to the writer.
2433 if (cpu_buffer->reader_page == cpu_buffer->commit_page) {
2434 unsigned int read = cpu_buffer->reader_page->read;
2438 /* The writer is still on the reader page, we must copy */
2439 bpage = cpu_buffer->reader_page->page;
2441 cpu_buffer->reader_page->page->data + read,
2442 local_read(&bpage->commit) - read);
2444 /* consume what was read */
2445 cpu_buffer->reader_page += read;
2448 /* swap the pages */
2449 rb_init_page(bpage);
2450 bpage = cpu_buffer->reader_page->page;
2451 cpu_buffer->reader_page->page = *data_page;
2452 cpu_buffer->reader_page->read = 0;
2457 /* update the entry counter */
2458 rb_remove_entries(cpu_buffer, bpage);
2460 spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
2466 rb_simple_read(struct file *filp, char __user *ubuf,
2467 size_t cnt, loff_t *ppos)
2469 long *p = filp->private_data;
2473 if (test_bit(RB_BUFFERS_DISABLED_BIT, p))
2474 r = sprintf(buf, "permanently disabled\n");
2476 r = sprintf(buf, "%d\n", test_bit(RB_BUFFERS_ON_BIT, p));
2478 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2482 rb_simple_write(struct file *filp, const char __user *ubuf,
2483 size_t cnt, loff_t *ppos)
2485 long *p = filp->private_data;
2490 if (cnt >= sizeof(buf))
2493 if (copy_from_user(&buf, ubuf, cnt))
2498 ret = strict_strtoul(buf, 10, &val);
2503 set_bit(RB_BUFFERS_ON_BIT, p);
2505 clear_bit(RB_BUFFERS_ON_BIT, p);
2512 static struct file_operations rb_simple_fops = {
2513 .open = tracing_open_generic,
2514 .read = rb_simple_read,
2515 .write = rb_simple_write,
2519 static __init int rb_init_debugfs(void)
2521 struct dentry *d_tracer;
2522 struct dentry *entry;
2524 d_tracer = tracing_init_dentry();
2526 entry = debugfs_create_file("tracing_on", 0644, d_tracer,
2527 &ring_buffer_flags, &rb_simple_fops);
2529 pr_warning("Could not create debugfs 'tracing_on' entry\n");
2534 fs_initcall(rb_init_debugfs);