4 * Copyright (C) 2008 Steven Rostedt <srostedt@redhat.com>
6 #include <linux/ring_buffer.h>
7 #include <linux/ftrace_irq.h>
8 #include <linux/spinlock.h>
9 #include <linux/debugfs.h>
10 #include <linux/uaccess.h>
11 #include <linux/hardirq.h>
12 #include <linux/module.h>
13 #include <linux/percpu.h>
14 #include <linux/mutex.h>
15 #include <linux/sched.h> /* used for sched_clock() (for now) */
16 #include <linux/init.h>
17 #include <linux/hash.h>
18 #include <linux/list.h>
24 * A fast way to enable or disable all ring buffers is to
25 * call tracing_on or tracing_off. Turning off the ring buffers
26 * prevents all ring buffers from being recorded to.
27 * Turning this switch on, makes it OK to write to the
28 * ring buffer, if the ring buffer is enabled itself.
30 * There's three layers that must be on in order to write
33 * 1) This global flag must be set.
34 * 2) The ring buffer must be enabled for recording.
35 * 3) The per cpu buffer must be enabled for recording.
37 * In case of an anomaly, this global flag has a bit set that
38 * will permantly disable all ring buffers.
42 * Global flag to disable all recording to ring buffers
43 * This has two bits: ON, DISABLED
47 * 0 0 : ring buffers are off
48 * 1 0 : ring buffers are on
49 * X 1 : ring buffers are permanently disabled
53 RB_BUFFERS_ON_BIT = 0,
54 RB_BUFFERS_DISABLED_BIT = 1,
58 RB_BUFFERS_ON = 1 << RB_BUFFERS_ON_BIT,
59 RB_BUFFERS_DISABLED = 1 << RB_BUFFERS_DISABLED_BIT,
62 static unsigned long ring_buffer_flags __read_mostly = RB_BUFFERS_ON;
65 * tracing_on - enable all tracing buffers
67 * This function enables all tracing buffers that may have been
68 * disabled with tracing_off.
72 set_bit(RB_BUFFERS_ON_BIT, &ring_buffer_flags);
74 EXPORT_SYMBOL_GPL(tracing_on);
77 * tracing_off - turn off all tracing buffers
79 * This function stops all tracing buffers from recording data.
80 * It does not disable any overhead the tracers themselves may
81 * be causing. This function simply causes all recording to
82 * the ring buffers to fail.
84 void tracing_off(void)
86 clear_bit(RB_BUFFERS_ON_BIT, &ring_buffer_flags);
88 EXPORT_SYMBOL_GPL(tracing_off);
91 * tracing_off_permanent - permanently disable ring buffers
93 * This function, once called, will disable all ring buffers
96 void tracing_off_permanent(void)
98 set_bit(RB_BUFFERS_DISABLED_BIT, &ring_buffer_flags);
103 /* Up this if you want to test the TIME_EXTENTS and normalization */
104 #define DEBUG_SHIFT 0
107 u64 ring_buffer_time_stamp(int cpu)
111 preempt_disable_notrace();
112 /* shift to debug/test normalization and TIME_EXTENTS */
113 time = sched_clock() << DEBUG_SHIFT;
114 preempt_enable_no_resched_notrace();
118 EXPORT_SYMBOL_GPL(ring_buffer_time_stamp);
120 void ring_buffer_normalize_time_stamp(int cpu, u64 *ts)
122 /* Just stupid testing the normalize function and deltas */
125 EXPORT_SYMBOL_GPL(ring_buffer_normalize_time_stamp);
127 #define RB_EVNT_HDR_SIZE (sizeof(struct ring_buffer_event))
128 #define RB_ALIGNMENT 4U
129 #define RB_MAX_SMALL_DATA 28
132 RB_LEN_TIME_EXTEND = 8,
133 RB_LEN_TIME_STAMP = 16,
136 /* inline for ring buffer fast paths */
138 rb_event_length(struct ring_buffer_event *event)
142 switch (event->type) {
143 case RINGBUF_TYPE_PADDING:
147 case RINGBUF_TYPE_TIME_EXTEND:
148 return RB_LEN_TIME_EXTEND;
150 case RINGBUF_TYPE_TIME_STAMP:
151 return RB_LEN_TIME_STAMP;
153 case RINGBUF_TYPE_DATA:
155 length = event->len * RB_ALIGNMENT;
157 length = event->array[0];
158 return length + RB_EVNT_HDR_SIZE;
167 * ring_buffer_event_length - return the length of the event
168 * @event: the event to get the length of
170 unsigned ring_buffer_event_length(struct ring_buffer_event *event)
172 unsigned length = rb_event_length(event);
173 if (event->type != RINGBUF_TYPE_DATA)
175 length -= RB_EVNT_HDR_SIZE;
176 if (length > RB_MAX_SMALL_DATA + sizeof(event->array[0]))
177 length -= sizeof(event->array[0]);
180 EXPORT_SYMBOL_GPL(ring_buffer_event_length);
182 /* inline for ring buffer fast paths */
184 rb_event_data(struct ring_buffer_event *event)
186 BUG_ON(event->type != RINGBUF_TYPE_DATA);
187 /* If length is in len field, then array[0] has the data */
189 return (void *)&event->array[0];
190 /* Otherwise length is in array[0] and array[1] has the data */
191 return (void *)&event->array[1];
195 * ring_buffer_event_data - return the data of the event
196 * @event: the event to get the data from
198 void *ring_buffer_event_data(struct ring_buffer_event *event)
200 return rb_event_data(event);
202 EXPORT_SYMBOL_GPL(ring_buffer_event_data);
204 #define for_each_buffer_cpu(buffer, cpu) \
205 for_each_cpu(cpu, buffer->cpumask)
208 #define TS_MASK ((1ULL << TS_SHIFT) - 1)
209 #define TS_DELTA_TEST (~TS_MASK)
211 struct buffer_data_page {
212 u64 time_stamp; /* page time stamp */
213 local_t commit; /* write committed index */
214 unsigned char data[]; /* data of buffer page */
218 local_t write; /* index for next write */
219 unsigned read; /* index for next read */
220 struct list_head list; /* list of free pages */
221 struct buffer_data_page *page; /* Actual data page */
224 static void rb_init_page(struct buffer_data_page *bpage)
226 local_set(&bpage->commit, 0);
230 * Also stolen from mm/slob.c. Thanks to Mathieu Desnoyers for pointing
233 static 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; /* committed pages */
264 struct buffer_page *reader_page;
265 unsigned long overrun;
266 unsigned long entries;
269 atomic_t record_disabled;
276 atomic_t record_disabled;
277 cpumask_var_t cpumask;
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 safety 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 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:
941 event->len = DIV_ROUND_UP(RB_LEN_TIME_EXTEND, RB_ALIGNMENT);
944 case RINGBUF_TYPE_TIME_STAMP:
945 event->len = DIV_ROUND_UP(RB_LEN_TIME_STAMP, RB_ALIGNMENT);
948 case RINGBUF_TYPE_DATA:
949 length -= RB_EVNT_HDR_SIZE;
950 if (length > RB_MAX_SMALL_DATA) {
952 event->array[0] = length;
954 event->len = DIV_ROUND_UP(length, RB_ALIGNMENT);
961 static unsigned rb_calculate_event_length(unsigned length)
963 struct ring_buffer_event event; /* Used only for sizeof array */
965 /* zero length can cause confusions */
969 if (length > RB_MAX_SMALL_DATA)
970 length += sizeof(event.array[0]);
972 length += RB_EVNT_HDR_SIZE;
973 length = ALIGN(length, RB_ALIGNMENT);
978 static struct ring_buffer_event *
979 __rb_reserve_next(struct ring_buffer_per_cpu *cpu_buffer,
980 unsigned type, unsigned long length, u64 *ts)
982 struct buffer_page *tail_page, *head_page, *reader_page, *commit_page;
983 unsigned long tail, write;
984 struct ring_buffer *buffer = cpu_buffer->buffer;
985 struct ring_buffer_event *event;
987 bool lock_taken = false;
989 commit_page = cpu_buffer->commit_page;
990 /* we just need to protect against interrupts */
992 tail_page = cpu_buffer->tail_page;
993 write = local_add_return(length, &tail_page->write);
994 tail = write - length;
996 /* See if we shot pass the end of this buffer page */
997 if (write > BUF_PAGE_SIZE) {
998 struct buffer_page *next_page = tail_page;
1000 local_irq_save(flags);
1002 * Since the write to the buffer is still not
1003 * fully lockless, we must be careful with NMIs.
1004 * The locks in the writers are taken when a write
1005 * crosses to a new page. The locks protect against
1006 * races with the readers (this will soon be fixed
1007 * with a lockless solution).
1009 * Because we can not protect against NMIs, and we
1010 * want to keep traces reentrant, we need to manage
1011 * what happens when we are in an NMI.
1013 * NMIs can happen after we take the lock.
1014 * If we are in an NMI, only take the lock
1015 * if it is not already taken. Otherwise
1018 if (unlikely(in_nmi())) {
1019 if (!__raw_spin_trylock(&cpu_buffer->lock))
1022 __raw_spin_lock(&cpu_buffer->lock);
1026 rb_inc_page(cpu_buffer, &next_page);
1028 head_page = cpu_buffer->head_page;
1029 reader_page = cpu_buffer->reader_page;
1031 /* we grabbed the lock before incrementing */
1032 if (RB_WARN_ON(cpu_buffer, next_page == reader_page))
1036 * If for some reason, we had an interrupt storm that made
1037 * it all the way around the buffer, bail, and warn
1040 if (unlikely(next_page == commit_page)) {
1045 if (next_page == head_page) {
1046 if (!(buffer->flags & RB_FL_OVERWRITE))
1049 /* tail_page has not moved yet? */
1050 if (tail_page == cpu_buffer->tail_page) {
1051 /* count overflows */
1052 rb_update_overflow(cpu_buffer);
1054 rb_inc_page(cpu_buffer, &head_page);
1055 cpu_buffer->head_page = head_page;
1056 cpu_buffer->head_page->read = 0;
1061 * If the tail page is still the same as what we think
1062 * it is, then it is up to us to update the tail
1065 if (tail_page == cpu_buffer->tail_page) {
1066 local_set(&next_page->write, 0);
1067 local_set(&next_page->page->commit, 0);
1068 cpu_buffer->tail_page = next_page;
1070 /* reread the time stamp */
1071 *ts = ring_buffer_time_stamp(cpu_buffer->cpu);
1072 cpu_buffer->tail_page->page->time_stamp = *ts;
1076 * The actual tail page has moved forward.
1078 if (tail < BUF_PAGE_SIZE) {
1079 /* Mark the rest of the page with padding */
1080 event = __rb_page_index(tail_page, tail);
1081 event->type = RINGBUF_TYPE_PADDING;
1084 if (tail <= BUF_PAGE_SIZE)
1085 /* Set the write back to the previous setting */
1086 local_set(&tail_page->write, tail);
1089 * If this was a commit entry that failed,
1090 * increment that too
1092 if (tail_page == cpu_buffer->commit_page &&
1093 tail == rb_commit_index(cpu_buffer)) {
1094 rb_set_commit_to_write(cpu_buffer);
1097 __raw_spin_unlock(&cpu_buffer->lock);
1098 local_irq_restore(flags);
1100 /* fail and let the caller try again */
1101 return ERR_PTR(-EAGAIN);
1104 /* We reserved something on the buffer */
1106 if (RB_WARN_ON(cpu_buffer, write > BUF_PAGE_SIZE))
1109 event = __rb_page_index(tail_page, tail);
1110 rb_update_event(event, type, length);
1113 * If this is a commit and the tail is zero, then update
1114 * this page's time stamp.
1116 if (!tail && rb_is_commit(cpu_buffer, event))
1117 cpu_buffer->commit_page->page->time_stamp = *ts;
1123 if (tail <= BUF_PAGE_SIZE)
1124 local_set(&tail_page->write, tail);
1126 if (likely(lock_taken))
1127 __raw_spin_unlock(&cpu_buffer->lock);
1128 local_irq_restore(flags);
1133 rb_add_time_stamp(struct ring_buffer_per_cpu *cpu_buffer,
1134 u64 *ts, u64 *delta)
1136 struct ring_buffer_event *event;
1140 if (unlikely(*delta > (1ULL << 59) && !once++)) {
1141 printk(KERN_WARNING "Delta way too big! %llu"
1142 " ts=%llu write stamp = %llu\n",
1143 (unsigned long long)*delta,
1144 (unsigned long long)*ts,
1145 (unsigned long long)cpu_buffer->write_stamp);
1150 * The delta is too big, we to add a
1153 event = __rb_reserve_next(cpu_buffer,
1154 RINGBUF_TYPE_TIME_EXTEND,
1160 if (PTR_ERR(event) == -EAGAIN)
1163 /* Only a commited time event can update the write stamp */
1164 if (rb_is_commit(cpu_buffer, event)) {
1166 * If this is the first on the page, then we need to
1167 * update the page itself, and just put in a zero.
1169 if (rb_event_index(event)) {
1170 event->time_delta = *delta & TS_MASK;
1171 event->array[0] = *delta >> TS_SHIFT;
1173 cpu_buffer->commit_page->page->time_stamp = *ts;
1174 event->time_delta = 0;
1175 event->array[0] = 0;
1177 cpu_buffer->write_stamp = *ts;
1178 /* let the caller know this was the commit */
1181 /* Darn, this is just wasted space */
1182 event->time_delta = 0;
1183 event->array[0] = 0;
1192 static struct ring_buffer_event *
1193 rb_reserve_next_event(struct ring_buffer_per_cpu *cpu_buffer,
1194 unsigned type, unsigned long length)
1196 struct ring_buffer_event *event;
1203 * We allow for interrupts to reenter here and do a trace.
1204 * If one does, it will cause this original code to loop
1205 * back here. Even with heavy interrupts happening, this
1206 * should only happen a few times in a row. If this happens
1207 * 1000 times in a row, there must be either an interrupt
1208 * storm or we have something buggy.
1211 if (RB_WARN_ON(cpu_buffer, ++nr_loops > 1000))
1214 ts = ring_buffer_time_stamp(cpu_buffer->cpu);
1217 * Only the first commit can update the timestamp.
1218 * Yes there is a race here. If an interrupt comes in
1219 * just after the conditional and it traces too, then it
1220 * will also check the deltas. More than one timestamp may
1221 * also be made. But only the entry that did the actual
1222 * commit will be something other than zero.
1224 if (cpu_buffer->tail_page == cpu_buffer->commit_page &&
1225 rb_page_write(cpu_buffer->tail_page) ==
1226 rb_commit_index(cpu_buffer)) {
1228 delta = ts - cpu_buffer->write_stamp;
1230 /* make sure this delta is calculated here */
1233 /* Did the write stamp get updated already? */
1234 if (unlikely(ts < cpu_buffer->write_stamp))
1237 if (test_time_stamp(delta)) {
1239 commit = rb_add_time_stamp(cpu_buffer, &ts, &delta);
1241 if (commit == -EBUSY)
1244 if (commit == -EAGAIN)
1247 RB_WARN_ON(cpu_buffer, commit < 0);
1250 /* Non commits have zero deltas */
1253 event = __rb_reserve_next(cpu_buffer, type, length, &ts);
1254 if (PTR_ERR(event) == -EAGAIN)
1258 if (unlikely(commit))
1260 * Ouch! We needed a timestamp and it was commited. But
1261 * we didn't get our event reserved.
1263 rb_set_commit_to_write(cpu_buffer);
1268 * If the timestamp was commited, make the commit our entry
1269 * now so that we will update it when needed.
1272 rb_set_commit_event(cpu_buffer, event);
1273 else if (!rb_is_commit(cpu_buffer, event))
1276 event->time_delta = delta;
1281 static DEFINE_PER_CPU(int, rb_need_resched);
1284 * ring_buffer_lock_reserve - reserve a part of the buffer
1285 * @buffer: the ring buffer to reserve from
1286 * @length: the length of the data to reserve (excluding event header)
1288 * Returns a reseverd event on the ring buffer to copy directly to.
1289 * The user of this interface will need to get the body to write into
1290 * and can use the ring_buffer_event_data() interface.
1292 * The length is the length of the data needed, not the event length
1293 * which also includes the event header.
1295 * Must be paired with ring_buffer_unlock_commit, unless NULL is returned.
1296 * If NULL is returned, then nothing has been allocated or locked.
1298 struct ring_buffer_event *
1299 ring_buffer_lock_reserve(struct ring_buffer *buffer, unsigned long length)
1301 struct ring_buffer_per_cpu *cpu_buffer;
1302 struct ring_buffer_event *event;
1305 if (ring_buffer_flags != RB_BUFFERS_ON)
1308 if (atomic_read(&buffer->record_disabled))
1311 /* If we are tracing schedule, we don't want to recurse */
1312 resched = ftrace_preempt_disable();
1314 cpu = raw_smp_processor_id();
1316 if (!cpumask_test_cpu(cpu, buffer->cpumask))
1319 cpu_buffer = buffer->buffers[cpu];
1321 if (atomic_read(&cpu_buffer->record_disabled))
1324 length = rb_calculate_event_length(length);
1325 if (length > BUF_PAGE_SIZE)
1328 event = rb_reserve_next_event(cpu_buffer, RINGBUF_TYPE_DATA, length);
1333 * Need to store resched state on this cpu.
1334 * Only the first needs to.
1337 if (preempt_count() == 1)
1338 per_cpu(rb_need_resched, cpu) = resched;
1343 ftrace_preempt_enable(resched);
1346 EXPORT_SYMBOL_GPL(ring_buffer_lock_reserve);
1348 static void rb_commit(struct ring_buffer_per_cpu *cpu_buffer,
1349 struct ring_buffer_event *event)
1351 cpu_buffer->entries++;
1353 /* Only process further if we own the commit */
1354 if (!rb_is_commit(cpu_buffer, event))
1357 cpu_buffer->write_stamp += event->time_delta;
1359 rb_set_commit_to_write(cpu_buffer);
1363 * ring_buffer_unlock_commit - commit a reserved
1364 * @buffer: The buffer to commit to
1365 * @event: The event pointer to commit.
1367 * This commits the data to the ring buffer, and releases any locks held.
1369 * Must be paired with ring_buffer_lock_reserve.
1371 int ring_buffer_unlock_commit(struct ring_buffer *buffer,
1372 struct ring_buffer_event *event)
1374 struct ring_buffer_per_cpu *cpu_buffer;
1375 int cpu = raw_smp_processor_id();
1377 cpu_buffer = buffer->buffers[cpu];
1379 rb_commit(cpu_buffer, event);
1382 * Only the last preempt count needs to restore preemption.
1384 if (preempt_count() == 1)
1385 ftrace_preempt_enable(per_cpu(rb_need_resched, cpu));
1387 preempt_enable_no_resched_notrace();
1391 EXPORT_SYMBOL_GPL(ring_buffer_unlock_commit);
1394 * ring_buffer_write - write data to the buffer without reserving
1395 * @buffer: The ring buffer to write to.
1396 * @length: The length of the data being written (excluding the event header)
1397 * @data: The data to write to the buffer.
1399 * This is like ring_buffer_lock_reserve and ring_buffer_unlock_commit as
1400 * one function. If you already have the data to write to the buffer, it
1401 * may be easier to simply call this function.
1403 * Note, like ring_buffer_lock_reserve, the length is the length of the data
1404 * and not the length of the event which would hold the header.
1406 int ring_buffer_write(struct ring_buffer *buffer,
1407 unsigned long length,
1410 struct ring_buffer_per_cpu *cpu_buffer;
1411 struct ring_buffer_event *event;
1412 unsigned long event_length;
1417 if (ring_buffer_flags != RB_BUFFERS_ON)
1420 if (atomic_read(&buffer->record_disabled))
1423 resched = ftrace_preempt_disable();
1425 cpu = raw_smp_processor_id();
1427 if (!cpumask_test_cpu(cpu, buffer->cpumask))
1430 cpu_buffer = buffer->buffers[cpu];
1432 if (atomic_read(&cpu_buffer->record_disabled))
1435 event_length = rb_calculate_event_length(length);
1436 event = rb_reserve_next_event(cpu_buffer,
1437 RINGBUF_TYPE_DATA, event_length);
1441 body = rb_event_data(event);
1443 memcpy(body, data, length);
1445 rb_commit(cpu_buffer, event);
1449 ftrace_preempt_enable(resched);
1453 EXPORT_SYMBOL_GPL(ring_buffer_write);
1455 static int rb_per_cpu_empty(struct ring_buffer_per_cpu *cpu_buffer)
1457 struct buffer_page *reader = cpu_buffer->reader_page;
1458 struct buffer_page *head = cpu_buffer->head_page;
1459 struct buffer_page *commit = cpu_buffer->commit_page;
1461 return reader->read == rb_page_commit(reader) &&
1462 (commit == reader ||
1464 head->read == rb_page_commit(commit)));
1468 * ring_buffer_record_disable - stop all writes into the buffer
1469 * @buffer: The ring buffer to stop writes to.
1471 * This prevents all writes to the buffer. Any attempt to write
1472 * to the buffer after this will fail and return NULL.
1474 * The caller should call synchronize_sched() after this.
1476 void ring_buffer_record_disable(struct ring_buffer *buffer)
1478 atomic_inc(&buffer->record_disabled);
1480 EXPORT_SYMBOL_GPL(ring_buffer_record_disable);
1483 * ring_buffer_record_enable - enable writes to the buffer
1484 * @buffer: The ring buffer to enable writes
1486 * Note, multiple disables will need the same number of enables
1487 * to truely enable the writing (much like preempt_disable).
1489 void ring_buffer_record_enable(struct ring_buffer *buffer)
1491 atomic_dec(&buffer->record_disabled);
1493 EXPORT_SYMBOL_GPL(ring_buffer_record_enable);
1496 * ring_buffer_record_disable_cpu - stop all writes into the cpu_buffer
1497 * @buffer: The ring buffer to stop writes to.
1498 * @cpu: The CPU buffer to stop
1500 * This prevents all writes to the buffer. Any attempt to write
1501 * to the buffer after this will fail and return NULL.
1503 * The caller should call synchronize_sched() after this.
1505 void ring_buffer_record_disable_cpu(struct ring_buffer *buffer, int cpu)
1507 struct ring_buffer_per_cpu *cpu_buffer;
1509 if (!cpumask_test_cpu(cpu, buffer->cpumask))
1512 cpu_buffer = buffer->buffers[cpu];
1513 atomic_inc(&cpu_buffer->record_disabled);
1515 EXPORT_SYMBOL_GPL(ring_buffer_record_disable_cpu);
1518 * ring_buffer_record_enable_cpu - enable writes to the buffer
1519 * @buffer: The ring buffer to enable writes
1520 * @cpu: The CPU to enable.
1522 * Note, multiple disables will need the same number of enables
1523 * to truely enable the writing (much like preempt_disable).
1525 void ring_buffer_record_enable_cpu(struct ring_buffer *buffer, int cpu)
1527 struct ring_buffer_per_cpu *cpu_buffer;
1529 if (!cpumask_test_cpu(cpu, buffer->cpumask))
1532 cpu_buffer = buffer->buffers[cpu];
1533 atomic_dec(&cpu_buffer->record_disabled);
1535 EXPORT_SYMBOL_GPL(ring_buffer_record_enable_cpu);
1538 * ring_buffer_entries_cpu - get the number of entries in a cpu buffer
1539 * @buffer: The ring buffer
1540 * @cpu: The per CPU buffer to get the entries from.
1542 unsigned long ring_buffer_entries_cpu(struct ring_buffer *buffer, int cpu)
1544 struct ring_buffer_per_cpu *cpu_buffer;
1546 if (!cpumask_test_cpu(cpu, buffer->cpumask))
1549 cpu_buffer = buffer->buffers[cpu];
1550 return cpu_buffer->entries;
1552 EXPORT_SYMBOL_GPL(ring_buffer_entries_cpu);
1555 * ring_buffer_overrun_cpu - get the number of overruns in a cpu_buffer
1556 * @buffer: The ring buffer
1557 * @cpu: The per CPU buffer to get the number of overruns from
1559 unsigned long ring_buffer_overrun_cpu(struct ring_buffer *buffer, int cpu)
1561 struct ring_buffer_per_cpu *cpu_buffer;
1563 if (!cpumask_test_cpu(cpu, buffer->cpumask))
1566 cpu_buffer = buffer->buffers[cpu];
1567 return cpu_buffer->overrun;
1569 EXPORT_SYMBOL_GPL(ring_buffer_overrun_cpu);
1572 * ring_buffer_entries - get the number of entries in a buffer
1573 * @buffer: The ring buffer
1575 * Returns the total number of entries in the ring buffer
1578 unsigned long ring_buffer_entries(struct ring_buffer *buffer)
1580 struct ring_buffer_per_cpu *cpu_buffer;
1581 unsigned long entries = 0;
1584 /* if you care about this being correct, lock the buffer */
1585 for_each_buffer_cpu(buffer, cpu) {
1586 cpu_buffer = buffer->buffers[cpu];
1587 entries += cpu_buffer->entries;
1592 EXPORT_SYMBOL_GPL(ring_buffer_entries);
1595 * ring_buffer_overrun_cpu - get the number of overruns in buffer
1596 * @buffer: The ring buffer
1598 * Returns the total number of overruns in the ring buffer
1601 unsigned long ring_buffer_overruns(struct ring_buffer *buffer)
1603 struct ring_buffer_per_cpu *cpu_buffer;
1604 unsigned long overruns = 0;
1607 /* if you care about this being correct, lock the buffer */
1608 for_each_buffer_cpu(buffer, cpu) {
1609 cpu_buffer = buffer->buffers[cpu];
1610 overruns += cpu_buffer->overrun;
1615 EXPORT_SYMBOL_GPL(ring_buffer_overruns);
1617 static void rb_iter_reset(struct ring_buffer_iter *iter)
1619 struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
1621 /* Iterator usage is expected to have record disabled */
1622 if (list_empty(&cpu_buffer->reader_page->list)) {
1623 iter->head_page = cpu_buffer->head_page;
1624 iter->head = cpu_buffer->head_page->read;
1626 iter->head_page = cpu_buffer->reader_page;
1627 iter->head = cpu_buffer->reader_page->read;
1630 iter->read_stamp = cpu_buffer->read_stamp;
1632 iter->read_stamp = iter->head_page->page->time_stamp;
1636 * ring_buffer_iter_reset - reset an iterator
1637 * @iter: The iterator to reset
1639 * Resets the iterator, so that it will start from the beginning
1642 void ring_buffer_iter_reset(struct ring_buffer_iter *iter)
1644 struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
1645 unsigned long flags;
1647 spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
1648 rb_iter_reset(iter);
1649 spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
1651 EXPORT_SYMBOL_GPL(ring_buffer_iter_reset);
1654 * ring_buffer_iter_empty - check if an iterator has no more to read
1655 * @iter: The iterator to check
1657 int ring_buffer_iter_empty(struct ring_buffer_iter *iter)
1659 struct ring_buffer_per_cpu *cpu_buffer;
1661 cpu_buffer = iter->cpu_buffer;
1663 return iter->head_page == cpu_buffer->commit_page &&
1664 iter->head == rb_commit_index(cpu_buffer);
1666 EXPORT_SYMBOL_GPL(ring_buffer_iter_empty);
1669 rb_update_read_stamp(struct ring_buffer_per_cpu *cpu_buffer,
1670 struct ring_buffer_event *event)
1674 switch (event->type) {
1675 case RINGBUF_TYPE_PADDING:
1678 case RINGBUF_TYPE_TIME_EXTEND:
1679 delta = event->array[0];
1681 delta += event->time_delta;
1682 cpu_buffer->read_stamp += delta;
1685 case RINGBUF_TYPE_TIME_STAMP:
1686 /* FIXME: not implemented */
1689 case RINGBUF_TYPE_DATA:
1690 cpu_buffer->read_stamp += event->time_delta;
1700 rb_update_iter_read_stamp(struct ring_buffer_iter *iter,
1701 struct ring_buffer_event *event)
1705 switch (event->type) {
1706 case RINGBUF_TYPE_PADDING:
1709 case RINGBUF_TYPE_TIME_EXTEND:
1710 delta = event->array[0];
1712 delta += event->time_delta;
1713 iter->read_stamp += delta;
1716 case RINGBUF_TYPE_TIME_STAMP:
1717 /* FIXME: not implemented */
1720 case RINGBUF_TYPE_DATA:
1721 iter->read_stamp += event->time_delta;
1730 static struct buffer_page *
1731 rb_get_reader_page(struct ring_buffer_per_cpu *cpu_buffer)
1733 struct buffer_page *reader = NULL;
1734 unsigned long flags;
1737 local_irq_save(flags);
1738 __raw_spin_lock(&cpu_buffer->lock);
1742 * This should normally only loop twice. But because the
1743 * start of the reader inserts an empty page, it causes
1744 * a case where we will loop three times. There should be no
1745 * reason to loop four times (that I know of).
1747 if (RB_WARN_ON(cpu_buffer, ++nr_loops > 3)) {
1752 reader = cpu_buffer->reader_page;
1754 /* If there's more to read, return this page */
1755 if (cpu_buffer->reader_page->read < rb_page_size(reader))
1758 /* Never should we have an index greater than the size */
1759 if (RB_WARN_ON(cpu_buffer,
1760 cpu_buffer->reader_page->read > rb_page_size(reader)))
1763 /* check if we caught up to the tail */
1765 if (cpu_buffer->commit_page == cpu_buffer->reader_page)
1769 * Splice the empty reader page into the list around the head.
1770 * Reset the reader page to size zero.
1773 reader = cpu_buffer->head_page;
1774 cpu_buffer->reader_page->list.next = reader->list.next;
1775 cpu_buffer->reader_page->list.prev = reader->list.prev;
1777 local_set(&cpu_buffer->reader_page->write, 0);
1778 local_set(&cpu_buffer->reader_page->page->commit, 0);
1780 /* Make the reader page now replace the head */
1781 reader->list.prev->next = &cpu_buffer->reader_page->list;
1782 reader->list.next->prev = &cpu_buffer->reader_page->list;
1785 * If the tail is on the reader, then we must set the head
1786 * to the inserted page, otherwise we set it one before.
1788 cpu_buffer->head_page = cpu_buffer->reader_page;
1790 if (cpu_buffer->commit_page != reader)
1791 rb_inc_page(cpu_buffer, &cpu_buffer->head_page);
1793 /* Finally update the reader page to the new head */
1794 cpu_buffer->reader_page = reader;
1795 rb_reset_reader_page(cpu_buffer);
1800 __raw_spin_unlock(&cpu_buffer->lock);
1801 local_irq_restore(flags);
1806 static void rb_advance_reader(struct ring_buffer_per_cpu *cpu_buffer)
1808 struct ring_buffer_event *event;
1809 struct buffer_page *reader;
1812 reader = rb_get_reader_page(cpu_buffer);
1814 /* This function should not be called when buffer is empty */
1815 if (RB_WARN_ON(cpu_buffer, !reader))
1818 event = rb_reader_event(cpu_buffer);
1820 if (event->type == RINGBUF_TYPE_DATA)
1821 cpu_buffer->entries--;
1823 rb_update_read_stamp(cpu_buffer, event);
1825 length = rb_event_length(event);
1826 cpu_buffer->reader_page->read += length;
1829 static void rb_advance_iter(struct ring_buffer_iter *iter)
1831 struct ring_buffer *buffer;
1832 struct ring_buffer_per_cpu *cpu_buffer;
1833 struct ring_buffer_event *event;
1836 cpu_buffer = iter->cpu_buffer;
1837 buffer = cpu_buffer->buffer;
1840 * Check if we are at the end of the buffer.
1842 if (iter->head >= rb_page_size(iter->head_page)) {
1843 if (RB_WARN_ON(buffer,
1844 iter->head_page == cpu_buffer->commit_page))
1850 event = rb_iter_head_event(iter);
1852 length = rb_event_length(event);
1855 * This should not be called to advance the header if we are
1856 * at the tail of the buffer.
1858 if (RB_WARN_ON(cpu_buffer,
1859 (iter->head_page == cpu_buffer->commit_page) &&
1860 (iter->head + length > rb_commit_index(cpu_buffer))))
1863 rb_update_iter_read_stamp(iter, event);
1865 iter->head += length;
1867 /* check for end of page padding */
1868 if ((iter->head >= rb_page_size(iter->head_page)) &&
1869 (iter->head_page != cpu_buffer->commit_page))
1870 rb_advance_iter(iter);
1873 static struct ring_buffer_event *
1874 rb_buffer_peek(struct ring_buffer *buffer, int cpu, u64 *ts)
1876 struct ring_buffer_per_cpu *cpu_buffer;
1877 struct ring_buffer_event *event;
1878 struct buffer_page *reader;
1881 if (!cpumask_test_cpu(cpu, buffer->cpumask))
1884 cpu_buffer = buffer->buffers[cpu];
1888 * We repeat when a timestamp is encountered. It is possible
1889 * to get multiple timestamps from an interrupt entering just
1890 * as one timestamp is about to be written. The max times
1891 * that this can happen is the number of nested interrupts we
1892 * can have. Nesting 10 deep of interrupts is clearly
1895 if (RB_WARN_ON(cpu_buffer, ++nr_loops > 10))
1898 reader = rb_get_reader_page(cpu_buffer);
1902 event = rb_reader_event(cpu_buffer);
1904 switch (event->type) {
1905 case RINGBUF_TYPE_PADDING:
1906 RB_WARN_ON(cpu_buffer, 1);
1907 rb_advance_reader(cpu_buffer);
1910 case RINGBUF_TYPE_TIME_EXTEND:
1911 /* Internal data, OK to advance */
1912 rb_advance_reader(cpu_buffer);
1915 case RINGBUF_TYPE_TIME_STAMP:
1916 /* FIXME: not implemented */
1917 rb_advance_reader(cpu_buffer);
1920 case RINGBUF_TYPE_DATA:
1922 *ts = cpu_buffer->read_stamp + event->time_delta;
1923 ring_buffer_normalize_time_stamp(cpu_buffer->cpu, ts);
1933 EXPORT_SYMBOL_GPL(ring_buffer_peek);
1935 static struct ring_buffer_event *
1936 rb_iter_peek(struct ring_buffer_iter *iter, u64 *ts)
1938 struct ring_buffer *buffer;
1939 struct ring_buffer_per_cpu *cpu_buffer;
1940 struct ring_buffer_event *event;
1943 if (ring_buffer_iter_empty(iter))
1946 cpu_buffer = iter->cpu_buffer;
1947 buffer = cpu_buffer->buffer;
1951 * We repeat when a timestamp is encountered. It is possible
1952 * to get multiple timestamps from an interrupt entering just
1953 * as one timestamp is about to be written. The max times
1954 * that this can happen is the number of nested interrupts we
1955 * can have. Nesting 10 deep of interrupts is clearly
1958 if (RB_WARN_ON(cpu_buffer, ++nr_loops > 10))
1961 if (rb_per_cpu_empty(cpu_buffer))
1964 event = rb_iter_head_event(iter);
1966 switch (event->type) {
1967 case RINGBUF_TYPE_PADDING:
1971 case RINGBUF_TYPE_TIME_EXTEND:
1972 /* Internal data, OK to advance */
1973 rb_advance_iter(iter);
1976 case RINGBUF_TYPE_TIME_STAMP:
1977 /* FIXME: not implemented */
1978 rb_advance_iter(iter);
1981 case RINGBUF_TYPE_DATA:
1983 *ts = iter->read_stamp + event->time_delta;
1984 ring_buffer_normalize_time_stamp(cpu_buffer->cpu, ts);
1994 EXPORT_SYMBOL_GPL(ring_buffer_iter_peek);
1997 * ring_buffer_peek - peek at the next event to be read
1998 * @buffer: The ring buffer to read
1999 * @cpu: The cpu to peak at
2000 * @ts: The timestamp counter of this event.
2002 * This will return the event that will be read next, but does
2003 * not consume the data.
2005 struct ring_buffer_event *
2006 ring_buffer_peek(struct ring_buffer *buffer, int cpu, u64 *ts)
2008 struct ring_buffer_per_cpu *cpu_buffer = buffer->buffers[cpu];
2009 struct ring_buffer_event *event;
2010 unsigned long flags;
2012 spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
2013 event = rb_buffer_peek(buffer, cpu, ts);
2014 spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
2020 * ring_buffer_iter_peek - peek at the next event to be read
2021 * @iter: The ring buffer iterator
2022 * @ts: The timestamp counter of this event.
2024 * This will return the event that will be read next, but does
2025 * not increment the iterator.
2027 struct ring_buffer_event *
2028 ring_buffer_iter_peek(struct ring_buffer_iter *iter, u64 *ts)
2030 struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
2031 struct ring_buffer_event *event;
2032 unsigned long flags;
2034 spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
2035 event = rb_iter_peek(iter, ts);
2036 spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
2042 * ring_buffer_consume - return an event and consume it
2043 * @buffer: The ring buffer to get the next event from
2045 * Returns the next event in the ring buffer, and that event is consumed.
2046 * Meaning, that sequential reads will keep returning a different event,
2047 * and eventually empty the ring buffer if the producer is slower.
2049 struct ring_buffer_event *
2050 ring_buffer_consume(struct ring_buffer *buffer, int cpu, u64 *ts)
2052 struct ring_buffer_per_cpu *cpu_buffer = buffer->buffers[cpu];
2053 struct ring_buffer_event *event;
2054 unsigned long flags;
2056 if (!cpumask_test_cpu(cpu, buffer->cpumask))
2059 spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
2061 event = rb_buffer_peek(buffer, cpu, ts);
2065 rb_advance_reader(cpu_buffer);
2068 spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
2072 EXPORT_SYMBOL_GPL(ring_buffer_consume);
2075 * ring_buffer_read_start - start a non consuming read of the buffer
2076 * @buffer: The ring buffer to read from
2077 * @cpu: The cpu buffer to iterate over
2079 * This starts up an iteration through the buffer. It also disables
2080 * the recording to the buffer until the reading is finished.
2081 * This prevents the reading from being corrupted. This is not
2082 * a consuming read, so a producer is not expected.
2084 * Must be paired with ring_buffer_finish.
2086 struct ring_buffer_iter *
2087 ring_buffer_read_start(struct ring_buffer *buffer, int cpu)
2089 struct ring_buffer_per_cpu *cpu_buffer;
2090 struct ring_buffer_iter *iter;
2091 unsigned long flags;
2093 if (!cpumask_test_cpu(cpu, buffer->cpumask))
2096 iter = kmalloc(sizeof(*iter), GFP_KERNEL);
2100 cpu_buffer = buffer->buffers[cpu];
2102 iter->cpu_buffer = cpu_buffer;
2104 atomic_inc(&cpu_buffer->record_disabled);
2105 synchronize_sched();
2107 spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
2108 __raw_spin_lock(&cpu_buffer->lock);
2109 rb_iter_reset(iter);
2110 __raw_spin_unlock(&cpu_buffer->lock);
2111 spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
2115 EXPORT_SYMBOL_GPL(ring_buffer_read_start);
2118 * ring_buffer_finish - finish reading the iterator of the buffer
2119 * @iter: The iterator retrieved by ring_buffer_start
2121 * This re-enables the recording to the buffer, and frees the
2125 ring_buffer_read_finish(struct ring_buffer_iter *iter)
2127 struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
2129 atomic_dec(&cpu_buffer->record_disabled);
2132 EXPORT_SYMBOL_GPL(ring_buffer_read_finish);
2135 * ring_buffer_read - read the next item in the ring buffer by the iterator
2136 * @iter: The ring buffer iterator
2137 * @ts: The time stamp of the event read.
2139 * This reads the next event in the ring buffer and increments the iterator.
2141 struct ring_buffer_event *
2142 ring_buffer_read(struct ring_buffer_iter *iter, u64 *ts)
2144 struct ring_buffer_event *event;
2145 struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
2146 unsigned long flags;
2148 spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
2149 event = rb_iter_peek(iter, ts);
2153 rb_advance_iter(iter);
2155 spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
2159 EXPORT_SYMBOL_GPL(ring_buffer_read);
2162 * ring_buffer_size - return the size of the ring buffer (in bytes)
2163 * @buffer: The ring buffer.
2165 unsigned long ring_buffer_size(struct ring_buffer *buffer)
2167 return BUF_PAGE_SIZE * buffer->pages;
2169 EXPORT_SYMBOL_GPL(ring_buffer_size);
2172 rb_reset_cpu(struct ring_buffer_per_cpu *cpu_buffer)
2174 cpu_buffer->head_page
2175 = list_entry(cpu_buffer->pages.next, struct buffer_page, list);
2176 local_set(&cpu_buffer->head_page->write, 0);
2177 local_set(&cpu_buffer->head_page->page->commit, 0);
2179 cpu_buffer->head_page->read = 0;
2181 cpu_buffer->tail_page = cpu_buffer->head_page;
2182 cpu_buffer->commit_page = cpu_buffer->head_page;
2184 INIT_LIST_HEAD(&cpu_buffer->reader_page->list);
2185 local_set(&cpu_buffer->reader_page->write, 0);
2186 local_set(&cpu_buffer->reader_page->page->commit, 0);
2187 cpu_buffer->reader_page->read = 0;
2189 cpu_buffer->overrun = 0;
2190 cpu_buffer->entries = 0;
2192 cpu_buffer->write_stamp = 0;
2193 cpu_buffer->read_stamp = 0;
2197 * ring_buffer_reset_cpu - reset a ring buffer per CPU buffer
2198 * @buffer: The ring buffer to reset a per cpu buffer of
2199 * @cpu: The CPU buffer to be reset
2201 void ring_buffer_reset_cpu(struct ring_buffer *buffer, int cpu)
2203 struct ring_buffer_per_cpu *cpu_buffer = buffer->buffers[cpu];
2204 unsigned long flags;
2206 if (!cpumask_test_cpu(cpu, buffer->cpumask))
2209 spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
2211 __raw_spin_lock(&cpu_buffer->lock);
2213 rb_reset_cpu(cpu_buffer);
2215 __raw_spin_unlock(&cpu_buffer->lock);
2217 spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
2219 EXPORT_SYMBOL_GPL(ring_buffer_reset_cpu);
2222 * ring_buffer_reset - reset a ring buffer
2223 * @buffer: The ring buffer to reset all cpu buffers
2225 void ring_buffer_reset(struct ring_buffer *buffer)
2229 for_each_buffer_cpu(buffer, cpu)
2230 ring_buffer_reset_cpu(buffer, cpu);
2232 EXPORT_SYMBOL_GPL(ring_buffer_reset);
2235 * rind_buffer_empty - is the ring buffer empty?
2236 * @buffer: The ring buffer to test
2238 int ring_buffer_empty(struct ring_buffer *buffer)
2240 struct ring_buffer_per_cpu *cpu_buffer;
2243 /* yes this is racy, but if you don't like the race, lock the buffer */
2244 for_each_buffer_cpu(buffer, cpu) {
2245 cpu_buffer = buffer->buffers[cpu];
2246 if (!rb_per_cpu_empty(cpu_buffer))
2251 EXPORT_SYMBOL_GPL(ring_buffer_empty);
2254 * ring_buffer_empty_cpu - is a cpu buffer of a ring buffer empty?
2255 * @buffer: The ring buffer
2256 * @cpu: The CPU buffer to test
2258 int ring_buffer_empty_cpu(struct ring_buffer *buffer, int cpu)
2260 struct ring_buffer_per_cpu *cpu_buffer;
2262 if (!cpumask_test_cpu(cpu, buffer->cpumask))
2265 cpu_buffer = buffer->buffers[cpu];
2266 return rb_per_cpu_empty(cpu_buffer);
2268 EXPORT_SYMBOL_GPL(ring_buffer_empty_cpu);
2271 * ring_buffer_swap_cpu - swap a CPU buffer between two ring buffers
2272 * @buffer_a: One buffer to swap with
2273 * @buffer_b: The other buffer to swap with
2275 * This function is useful for tracers that want to take a "snapshot"
2276 * of a CPU buffer and has another back up buffer lying around.
2277 * it is expected that the tracer handles the cpu buffer not being
2278 * used at the moment.
2280 int ring_buffer_swap_cpu(struct ring_buffer *buffer_a,
2281 struct ring_buffer *buffer_b, int cpu)
2283 struct ring_buffer_per_cpu *cpu_buffer_a;
2284 struct ring_buffer_per_cpu *cpu_buffer_b;
2286 if (!cpumask_test_cpu(cpu, buffer_a->cpumask) ||
2287 !cpumask_test_cpu(cpu, buffer_b->cpumask))
2290 /* At least make sure the two buffers are somewhat the same */
2291 if (buffer_a->pages != buffer_b->pages)
2294 if (ring_buffer_flags != RB_BUFFERS_ON)
2297 if (atomic_read(&buffer_a->record_disabled))
2300 if (atomic_read(&buffer_b->record_disabled))
2303 cpu_buffer_a = buffer_a->buffers[cpu];
2304 cpu_buffer_b = buffer_b->buffers[cpu];
2306 if (atomic_read(&cpu_buffer_a->record_disabled))
2309 if (atomic_read(&cpu_buffer_b->record_disabled))
2313 * We can't do a synchronize_sched here because this
2314 * function can be called in atomic context.
2315 * Normally this will be called from the same CPU as cpu.
2316 * If not it's up to the caller to protect this.
2318 atomic_inc(&cpu_buffer_a->record_disabled);
2319 atomic_inc(&cpu_buffer_b->record_disabled);
2321 buffer_a->buffers[cpu] = cpu_buffer_b;
2322 buffer_b->buffers[cpu] = cpu_buffer_a;
2324 cpu_buffer_b->buffer = buffer_a;
2325 cpu_buffer_a->buffer = buffer_b;
2327 atomic_dec(&cpu_buffer_a->record_disabled);
2328 atomic_dec(&cpu_buffer_b->record_disabled);
2332 EXPORT_SYMBOL_GPL(ring_buffer_swap_cpu);
2334 static void rb_remove_entries(struct ring_buffer_per_cpu *cpu_buffer,
2335 struct buffer_data_page *bpage,
2336 unsigned int offset)
2338 struct ring_buffer_event *event;
2341 __raw_spin_lock(&cpu_buffer->lock);
2342 for (head = offset; head < local_read(&bpage->commit);
2343 head += rb_event_length(event)) {
2345 event = __rb_data_page_index(bpage, head);
2346 if (RB_WARN_ON(cpu_buffer, rb_null_event(event)))
2348 /* Only count data entries */
2349 if (event->type != RINGBUF_TYPE_DATA)
2351 cpu_buffer->entries--;
2353 __raw_spin_unlock(&cpu_buffer->lock);
2357 * ring_buffer_alloc_read_page - allocate a page to read from buffer
2358 * @buffer: the buffer to allocate for.
2360 * This function is used in conjunction with ring_buffer_read_page.
2361 * When reading a full page from the ring buffer, these functions
2362 * can be used to speed up the process. The calling function should
2363 * allocate a few pages first with this function. Then when it
2364 * needs to get pages from the ring buffer, it passes the result
2365 * of this function into ring_buffer_read_page, which will swap
2366 * the page that was allocated, with the read page of the buffer.
2369 * The page allocated, or NULL on error.
2371 void *ring_buffer_alloc_read_page(struct ring_buffer *buffer)
2374 struct buffer_data_page *bpage;
2376 addr = __get_free_page(GFP_KERNEL);
2380 bpage = (void *)addr;
2386 * ring_buffer_free_read_page - free an allocated read page
2387 * @buffer: the buffer the page was allocate for
2388 * @data: the page to free
2390 * Free a page allocated from ring_buffer_alloc_read_page.
2392 void ring_buffer_free_read_page(struct ring_buffer *buffer, void *data)
2394 free_page((unsigned long)data);
2398 * ring_buffer_read_page - extract a page from the ring buffer
2399 * @buffer: buffer to extract from
2400 * @data_page: the page to use allocated from ring_buffer_alloc_read_page
2401 * @cpu: the cpu of the buffer to extract
2402 * @full: should the extraction only happen when the page is full.
2404 * This function will pull out a page from the ring buffer and consume it.
2405 * @data_page must be the address of the variable that was returned
2406 * from ring_buffer_alloc_read_page. This is because the page might be used
2407 * to swap with a page in the ring buffer.
2410 * rpage = ring_buffer_alloc_read_page(buffer);
2413 * ret = ring_buffer_read_page(buffer, &rpage, cpu, 0);
2415 * process_page(rpage, ret);
2417 * When @full is set, the function will not return true unless
2418 * the writer is off the reader page.
2420 * Note: it is up to the calling functions to handle sleeps and wakeups.
2421 * The ring buffer can be used anywhere in the kernel and can not
2422 * blindly call wake_up. The layer that uses the ring buffer must be
2423 * responsible for that.
2426 * >=0 if data has been transferred, returns the offset of consumed data.
2427 * <0 if no data has been transferred.
2429 int ring_buffer_read_page(struct ring_buffer *buffer,
2430 void **data_page, int cpu, int full)
2432 struct ring_buffer_per_cpu *cpu_buffer = buffer->buffers[cpu];
2433 struct ring_buffer_event *event;
2434 struct buffer_data_page *bpage;
2435 unsigned long flags;
2446 spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
2449 * rb_buffer_peek will get the next ring buffer if
2450 * the current reader page is empty.
2452 event = rb_buffer_peek(buffer, cpu, NULL);
2456 /* check for data */
2457 if (!local_read(&cpu_buffer->reader_page->page->commit))
2460 read = cpu_buffer->reader_page->read;
2462 * If the writer is already off of the read page, then simply
2463 * switch the read page with the given page. Otherwise
2464 * we need to copy the data from the reader to the writer.
2466 if (cpu_buffer->reader_page == cpu_buffer->commit_page) {
2467 unsigned int commit = rb_page_commit(cpu_buffer->reader_page);
2468 struct buffer_data_page *rpage = cpu_buffer->reader_page->page;
2472 /* The writer is still on the reader page, we must copy */
2473 memcpy(bpage->data + read, rpage->data + read, commit - read);
2475 /* consume what was read */
2476 cpu_buffer->reader_page->read = commit;
2479 local_set(&bpage->commit, commit);
2481 bpage->time_stamp = rpage->time_stamp;
2483 /* swap the pages */
2484 rb_init_page(bpage);
2485 bpage = cpu_buffer->reader_page->page;
2486 cpu_buffer->reader_page->page = *data_page;
2487 cpu_buffer->reader_page->read = 0;
2492 /* update the entry counter */
2493 rb_remove_entries(cpu_buffer, bpage, read);
2495 spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
2501 rb_simple_read(struct file *filp, char __user *ubuf,
2502 size_t cnt, loff_t *ppos)
2504 unsigned long *p = filp->private_data;
2508 if (test_bit(RB_BUFFERS_DISABLED_BIT, p))
2509 r = sprintf(buf, "permanently disabled\n");
2511 r = sprintf(buf, "%d\n", test_bit(RB_BUFFERS_ON_BIT, p));
2513 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2517 rb_simple_write(struct file *filp, const char __user *ubuf,
2518 size_t cnt, loff_t *ppos)
2520 unsigned long *p = filp->private_data;
2525 if (cnt >= sizeof(buf))
2528 if (copy_from_user(&buf, ubuf, cnt))
2533 ret = strict_strtoul(buf, 10, &val);
2538 set_bit(RB_BUFFERS_ON_BIT, p);
2540 clear_bit(RB_BUFFERS_ON_BIT, p);
2547 static struct file_operations rb_simple_fops = {
2548 .open = tracing_open_generic,
2549 .read = rb_simple_read,
2550 .write = rb_simple_write,
2554 static __init int rb_init_debugfs(void)
2556 struct dentry *d_tracer;
2557 struct dentry *entry;
2559 d_tracer = tracing_init_dentry();
2561 entry = debugfs_create_file("tracing_on", 0644, d_tracer,
2562 &ring_buffer_flags, &rb_simple_fops);
2564 pr_warning("Could not create debugfs 'tracing_on' entry\n");
2569 fs_initcall(rb_init_debugfs);