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);
102 * tracing_is_on - show state of ring buffers enabled
104 int tracing_is_on(void)
106 return ring_buffer_flags == RB_BUFFERS_ON;
108 EXPORT_SYMBOL_GPL(tracing_is_on);
112 /* Up this if you want to test the TIME_EXTENTS and normalization */
113 #define DEBUG_SHIFT 0
116 u64 ring_buffer_time_stamp(int cpu)
120 preempt_disable_notrace();
121 /* shift to debug/test normalization and TIME_EXTENTS */
122 time = sched_clock() << DEBUG_SHIFT;
123 preempt_enable_no_resched_notrace();
127 EXPORT_SYMBOL_GPL(ring_buffer_time_stamp);
129 void ring_buffer_normalize_time_stamp(int cpu, u64 *ts)
131 /* Just stupid testing the normalize function and deltas */
134 EXPORT_SYMBOL_GPL(ring_buffer_normalize_time_stamp);
136 #define RB_EVNT_HDR_SIZE (sizeof(struct ring_buffer_event))
137 #define RB_ALIGNMENT 4U
138 #define RB_MAX_SMALL_DATA 28
141 RB_LEN_TIME_EXTEND = 8,
142 RB_LEN_TIME_STAMP = 16,
145 /* inline for ring buffer fast paths */
147 rb_event_length(struct ring_buffer_event *event)
151 switch (event->type) {
152 case RINGBUF_TYPE_PADDING:
156 case RINGBUF_TYPE_TIME_EXTEND:
157 return RB_LEN_TIME_EXTEND;
159 case RINGBUF_TYPE_TIME_STAMP:
160 return RB_LEN_TIME_STAMP;
162 case RINGBUF_TYPE_DATA:
164 length = event->len * RB_ALIGNMENT;
166 length = event->array[0];
167 return length + RB_EVNT_HDR_SIZE;
176 * ring_buffer_event_length - return the length of the event
177 * @event: the event to get the length of
179 unsigned ring_buffer_event_length(struct ring_buffer_event *event)
181 unsigned length = rb_event_length(event);
182 if (event->type != RINGBUF_TYPE_DATA)
184 length -= RB_EVNT_HDR_SIZE;
185 if (length > RB_MAX_SMALL_DATA + sizeof(event->array[0]))
186 length -= sizeof(event->array[0]);
189 EXPORT_SYMBOL_GPL(ring_buffer_event_length);
191 /* inline for ring buffer fast paths */
193 rb_event_data(struct ring_buffer_event *event)
195 BUG_ON(event->type != RINGBUF_TYPE_DATA);
196 /* If length is in len field, then array[0] has the data */
198 return (void *)&event->array[0];
199 /* Otherwise length is in array[0] and array[1] has the data */
200 return (void *)&event->array[1];
204 * ring_buffer_event_data - return the data of the event
205 * @event: the event to get the data from
207 void *ring_buffer_event_data(struct ring_buffer_event *event)
209 return rb_event_data(event);
211 EXPORT_SYMBOL_GPL(ring_buffer_event_data);
213 #define for_each_buffer_cpu(buffer, cpu) \
214 for_each_cpu(cpu, buffer->cpumask)
217 #define TS_MASK ((1ULL << TS_SHIFT) - 1)
218 #define TS_DELTA_TEST (~TS_MASK)
220 struct buffer_data_page {
221 u64 time_stamp; /* page time stamp */
222 local_t commit; /* write committed index */
223 unsigned char data[]; /* data of buffer page */
227 local_t write; /* index for next write */
228 unsigned read; /* index for next read */
229 struct list_head list; /* list of free pages */
230 struct buffer_data_page *page; /* Actual data page */
233 static void rb_init_page(struct buffer_data_page *bpage)
235 local_set(&bpage->commit, 0);
239 * Also stolen from mm/slob.c. Thanks to Mathieu Desnoyers for pointing
242 static void free_buffer_page(struct buffer_page *bpage)
244 free_page((unsigned long)bpage->page);
249 * We need to fit the time_stamp delta into 27 bits.
251 static inline int test_time_stamp(u64 delta)
253 if (delta & TS_DELTA_TEST)
258 #define BUF_PAGE_SIZE (PAGE_SIZE - offsetof(struct buffer_data_page, data))
261 * head_page == tail_page && head == tail then buffer is empty.
263 struct ring_buffer_per_cpu {
265 struct ring_buffer *buffer;
266 spinlock_t reader_lock; /* serialize readers */
268 struct lock_class_key lock_key;
269 struct list_head pages;
270 struct buffer_page *head_page; /* read from head */
271 struct buffer_page *tail_page; /* write to tail */
272 struct buffer_page *commit_page; /* committed pages */
273 struct buffer_page *reader_page;
274 unsigned long overrun;
275 unsigned long entries;
278 atomic_t record_disabled;
285 atomic_t record_disabled;
286 cpumask_var_t cpumask;
290 struct ring_buffer_per_cpu **buffers;
293 struct ring_buffer_iter {
294 struct ring_buffer_per_cpu *cpu_buffer;
296 struct buffer_page *head_page;
300 /* buffer may be either ring_buffer or ring_buffer_per_cpu */
301 #define RB_WARN_ON(buffer, cond) \
303 int _____ret = unlikely(cond); \
305 atomic_inc(&buffer->record_disabled); \
312 * check_pages - integrity check of buffer pages
313 * @cpu_buffer: CPU buffer with pages to test
315 * As a safety measure we check to make sure the data pages have not
318 static int rb_check_pages(struct ring_buffer_per_cpu *cpu_buffer)
320 struct list_head *head = &cpu_buffer->pages;
321 struct buffer_page *bpage, *tmp;
323 if (RB_WARN_ON(cpu_buffer, head->next->prev != head))
325 if (RB_WARN_ON(cpu_buffer, head->prev->next != head))
328 list_for_each_entry_safe(bpage, tmp, head, list) {
329 if (RB_WARN_ON(cpu_buffer,
330 bpage->list.next->prev != &bpage->list))
332 if (RB_WARN_ON(cpu_buffer,
333 bpage->list.prev->next != &bpage->list))
340 static int rb_allocate_pages(struct ring_buffer_per_cpu *cpu_buffer,
343 struct list_head *head = &cpu_buffer->pages;
344 struct buffer_page *bpage, *tmp;
349 for (i = 0; i < nr_pages; i++) {
350 bpage = kzalloc_node(ALIGN(sizeof(*bpage), cache_line_size()),
351 GFP_KERNEL, cpu_to_node(cpu_buffer->cpu));
354 list_add(&bpage->list, &pages);
356 addr = __get_free_page(GFP_KERNEL);
359 bpage->page = (void *)addr;
360 rb_init_page(bpage->page);
363 list_splice(&pages, head);
365 rb_check_pages(cpu_buffer);
370 list_for_each_entry_safe(bpage, tmp, &pages, list) {
371 list_del_init(&bpage->list);
372 free_buffer_page(bpage);
377 static struct ring_buffer_per_cpu *
378 rb_allocate_cpu_buffer(struct ring_buffer *buffer, int cpu)
380 struct ring_buffer_per_cpu *cpu_buffer;
381 struct buffer_page *bpage;
385 cpu_buffer = kzalloc_node(ALIGN(sizeof(*cpu_buffer), cache_line_size()),
386 GFP_KERNEL, cpu_to_node(cpu));
390 cpu_buffer->cpu = cpu;
391 cpu_buffer->buffer = buffer;
392 spin_lock_init(&cpu_buffer->reader_lock);
393 cpu_buffer->lock = (raw_spinlock_t)__RAW_SPIN_LOCK_UNLOCKED;
394 INIT_LIST_HEAD(&cpu_buffer->pages);
396 bpage = kzalloc_node(ALIGN(sizeof(*bpage), cache_line_size()),
397 GFP_KERNEL, cpu_to_node(cpu));
399 goto fail_free_buffer;
401 cpu_buffer->reader_page = bpage;
402 addr = __get_free_page(GFP_KERNEL);
404 goto fail_free_reader;
405 bpage->page = (void *)addr;
406 rb_init_page(bpage->page);
408 INIT_LIST_HEAD(&cpu_buffer->reader_page->list);
410 ret = rb_allocate_pages(cpu_buffer, buffer->pages);
412 goto fail_free_reader;
414 cpu_buffer->head_page
415 = list_entry(cpu_buffer->pages.next, struct buffer_page, list);
416 cpu_buffer->tail_page = cpu_buffer->commit_page = cpu_buffer->head_page;
421 free_buffer_page(cpu_buffer->reader_page);
428 static void rb_free_cpu_buffer(struct ring_buffer_per_cpu *cpu_buffer)
430 struct list_head *head = &cpu_buffer->pages;
431 struct buffer_page *bpage, *tmp;
433 list_del_init(&cpu_buffer->reader_page->list);
434 free_buffer_page(cpu_buffer->reader_page);
436 list_for_each_entry_safe(bpage, tmp, head, list) {
437 list_del_init(&bpage->list);
438 free_buffer_page(bpage);
444 * Causes compile errors if the struct buffer_page gets bigger
445 * than the struct page.
447 extern int ring_buffer_page_too_big(void);
450 * ring_buffer_alloc - allocate a new ring_buffer
451 * @size: the size in bytes per cpu that is needed.
452 * @flags: attributes to set for the ring buffer.
454 * Currently the only flag that is available is the RB_FL_OVERWRITE
455 * flag. This flag means that the buffer will overwrite old data
456 * when the buffer wraps. If this flag is not set, the buffer will
457 * drop data when the tail hits the head.
459 struct ring_buffer *ring_buffer_alloc(unsigned long size, unsigned flags)
461 struct ring_buffer *buffer;
465 /* Paranoid! Optimizes out when all is well */
466 if (sizeof(struct buffer_page) > sizeof(struct page))
467 ring_buffer_page_too_big();
470 /* keep it in its own cache line */
471 buffer = kzalloc(ALIGN(sizeof(*buffer), cache_line_size()),
476 if (!alloc_cpumask_var(&buffer->cpumask, GFP_KERNEL))
477 goto fail_free_buffer;
479 buffer->pages = DIV_ROUND_UP(size, BUF_PAGE_SIZE);
480 buffer->flags = flags;
482 /* need at least two pages */
483 if (buffer->pages == 1)
486 cpumask_copy(buffer->cpumask, cpu_possible_mask);
487 buffer->cpus = nr_cpu_ids;
489 bsize = sizeof(void *) * nr_cpu_ids;
490 buffer->buffers = kzalloc(ALIGN(bsize, cache_line_size()),
492 if (!buffer->buffers)
493 goto fail_free_cpumask;
495 for_each_buffer_cpu(buffer, cpu) {
496 buffer->buffers[cpu] =
497 rb_allocate_cpu_buffer(buffer, cpu);
498 if (!buffer->buffers[cpu])
499 goto fail_free_buffers;
502 mutex_init(&buffer->mutex);
507 for_each_buffer_cpu(buffer, cpu) {
508 if (buffer->buffers[cpu])
509 rb_free_cpu_buffer(buffer->buffers[cpu]);
511 kfree(buffer->buffers);
514 free_cpumask_var(buffer->cpumask);
520 EXPORT_SYMBOL_GPL(ring_buffer_alloc);
523 * ring_buffer_free - free a ring buffer.
524 * @buffer: the buffer to free.
527 ring_buffer_free(struct ring_buffer *buffer)
531 for_each_buffer_cpu(buffer, cpu)
532 rb_free_cpu_buffer(buffer->buffers[cpu]);
534 free_cpumask_var(buffer->cpumask);
538 EXPORT_SYMBOL_GPL(ring_buffer_free);
540 static void rb_reset_cpu(struct ring_buffer_per_cpu *cpu_buffer);
543 rb_remove_pages(struct ring_buffer_per_cpu *cpu_buffer, unsigned nr_pages)
545 struct buffer_page *bpage;
549 atomic_inc(&cpu_buffer->record_disabled);
552 for (i = 0; i < nr_pages; i++) {
553 if (RB_WARN_ON(cpu_buffer, list_empty(&cpu_buffer->pages)))
555 p = cpu_buffer->pages.next;
556 bpage = list_entry(p, struct buffer_page, list);
557 list_del_init(&bpage->list);
558 free_buffer_page(bpage);
560 if (RB_WARN_ON(cpu_buffer, list_empty(&cpu_buffer->pages)))
563 rb_reset_cpu(cpu_buffer);
565 rb_check_pages(cpu_buffer);
567 atomic_dec(&cpu_buffer->record_disabled);
572 rb_insert_pages(struct ring_buffer_per_cpu *cpu_buffer,
573 struct list_head *pages, unsigned nr_pages)
575 struct buffer_page *bpage;
579 atomic_inc(&cpu_buffer->record_disabled);
582 for (i = 0; i < nr_pages; i++) {
583 if (RB_WARN_ON(cpu_buffer, list_empty(pages)))
586 bpage = list_entry(p, struct buffer_page, list);
587 list_del_init(&bpage->list);
588 list_add_tail(&bpage->list, &cpu_buffer->pages);
590 rb_reset_cpu(cpu_buffer);
592 rb_check_pages(cpu_buffer);
594 atomic_dec(&cpu_buffer->record_disabled);
598 * ring_buffer_resize - resize the ring buffer
599 * @buffer: the buffer to resize.
600 * @size: the new size.
602 * The tracer is responsible for making sure that the buffer is
603 * not being used while changing the size.
604 * Note: We may be able to change the above requirement by using
605 * RCU synchronizations.
607 * Minimum size is 2 * BUF_PAGE_SIZE.
609 * Returns -1 on failure.
611 int ring_buffer_resize(struct ring_buffer *buffer, unsigned long size)
613 struct ring_buffer_per_cpu *cpu_buffer;
614 unsigned nr_pages, rm_pages, new_pages;
615 struct buffer_page *bpage, *tmp;
616 unsigned long buffer_size;
622 * Always succeed at resizing a non-existent buffer:
627 size = DIV_ROUND_UP(size, BUF_PAGE_SIZE);
628 size *= BUF_PAGE_SIZE;
629 buffer_size = buffer->pages * BUF_PAGE_SIZE;
631 /* we need a minimum of two pages */
632 if (size < BUF_PAGE_SIZE * 2)
633 size = BUF_PAGE_SIZE * 2;
635 if (size == buffer_size)
638 mutex_lock(&buffer->mutex);
640 nr_pages = DIV_ROUND_UP(size, BUF_PAGE_SIZE);
642 if (size < buffer_size) {
644 /* easy case, just free pages */
645 if (RB_WARN_ON(buffer, nr_pages >= buffer->pages)) {
646 mutex_unlock(&buffer->mutex);
650 rm_pages = buffer->pages - nr_pages;
652 for_each_buffer_cpu(buffer, cpu) {
653 cpu_buffer = buffer->buffers[cpu];
654 rb_remove_pages(cpu_buffer, rm_pages);
660 * This is a bit more difficult. We only want to add pages
661 * when we can allocate enough for all CPUs. We do this
662 * by allocating all the pages and storing them on a local
663 * link list. If we succeed in our allocation, then we
664 * add these pages to the cpu_buffers. Otherwise we just free
665 * them all and return -ENOMEM;
667 if (RB_WARN_ON(buffer, nr_pages <= buffer->pages)) {
668 mutex_unlock(&buffer->mutex);
672 new_pages = nr_pages - buffer->pages;
674 for_each_buffer_cpu(buffer, cpu) {
675 for (i = 0; i < new_pages; i++) {
676 bpage = kzalloc_node(ALIGN(sizeof(*bpage),
678 GFP_KERNEL, cpu_to_node(cpu));
681 list_add(&bpage->list, &pages);
682 addr = __get_free_page(GFP_KERNEL);
685 bpage->page = (void *)addr;
686 rb_init_page(bpage->page);
690 for_each_buffer_cpu(buffer, cpu) {
691 cpu_buffer = buffer->buffers[cpu];
692 rb_insert_pages(cpu_buffer, &pages, new_pages);
695 if (RB_WARN_ON(buffer, !list_empty(&pages))) {
696 mutex_unlock(&buffer->mutex);
701 buffer->pages = nr_pages;
702 mutex_unlock(&buffer->mutex);
707 list_for_each_entry_safe(bpage, tmp, &pages, list) {
708 list_del_init(&bpage->list);
709 free_buffer_page(bpage);
711 mutex_unlock(&buffer->mutex);
714 EXPORT_SYMBOL_GPL(ring_buffer_resize);
716 static inline int rb_null_event(struct ring_buffer_event *event)
718 return event->type == RINGBUF_TYPE_PADDING;
722 __rb_data_page_index(struct buffer_data_page *bpage, unsigned index)
724 return bpage->data + index;
727 static inline void *__rb_page_index(struct buffer_page *bpage, unsigned index)
729 return bpage->page->data + index;
732 static inline struct ring_buffer_event *
733 rb_reader_event(struct ring_buffer_per_cpu *cpu_buffer)
735 return __rb_page_index(cpu_buffer->reader_page,
736 cpu_buffer->reader_page->read);
739 static inline struct ring_buffer_event *
740 rb_head_event(struct ring_buffer_per_cpu *cpu_buffer)
742 return __rb_page_index(cpu_buffer->head_page,
743 cpu_buffer->head_page->read);
746 static inline struct ring_buffer_event *
747 rb_iter_head_event(struct ring_buffer_iter *iter)
749 return __rb_page_index(iter->head_page, iter->head);
752 static inline unsigned rb_page_write(struct buffer_page *bpage)
754 return local_read(&bpage->write);
757 static inline unsigned rb_page_commit(struct buffer_page *bpage)
759 return local_read(&bpage->page->commit);
762 /* Size is determined by what has been commited */
763 static inline unsigned rb_page_size(struct buffer_page *bpage)
765 return rb_page_commit(bpage);
768 static inline unsigned
769 rb_commit_index(struct ring_buffer_per_cpu *cpu_buffer)
771 return rb_page_commit(cpu_buffer->commit_page);
774 static inline unsigned rb_head_size(struct ring_buffer_per_cpu *cpu_buffer)
776 return rb_page_commit(cpu_buffer->head_page);
780 * When the tail hits the head and the buffer is in overwrite mode,
781 * the head jumps to the next page and all content on the previous
782 * page is discarded. But before doing so, we update the overrun
783 * variable of the buffer.
785 static void rb_update_overflow(struct ring_buffer_per_cpu *cpu_buffer)
787 struct ring_buffer_event *event;
790 for (head = 0; head < rb_head_size(cpu_buffer);
791 head += rb_event_length(event)) {
793 event = __rb_page_index(cpu_buffer->head_page, head);
794 if (RB_WARN_ON(cpu_buffer, rb_null_event(event)))
796 /* Only count data entries */
797 if (event->type != RINGBUF_TYPE_DATA)
799 cpu_buffer->overrun++;
800 cpu_buffer->entries--;
804 static inline void rb_inc_page(struct ring_buffer_per_cpu *cpu_buffer,
805 struct buffer_page **bpage)
807 struct list_head *p = (*bpage)->list.next;
809 if (p == &cpu_buffer->pages)
812 *bpage = list_entry(p, struct buffer_page, list);
815 static inline unsigned
816 rb_event_index(struct ring_buffer_event *event)
818 unsigned long addr = (unsigned long)event;
820 return (addr & ~PAGE_MASK) - (PAGE_SIZE - BUF_PAGE_SIZE);
824 rb_is_commit(struct ring_buffer_per_cpu *cpu_buffer,
825 struct ring_buffer_event *event)
827 unsigned long addr = (unsigned long)event;
830 index = rb_event_index(event);
833 return cpu_buffer->commit_page->page == (void *)addr &&
834 rb_commit_index(cpu_buffer) == index;
838 rb_set_commit_event(struct ring_buffer_per_cpu *cpu_buffer,
839 struct ring_buffer_event *event)
841 unsigned long addr = (unsigned long)event;
844 index = rb_event_index(event);
847 while (cpu_buffer->commit_page->page != (void *)addr) {
848 if (RB_WARN_ON(cpu_buffer,
849 cpu_buffer->commit_page == cpu_buffer->tail_page))
851 cpu_buffer->commit_page->page->commit =
852 cpu_buffer->commit_page->write;
853 rb_inc_page(cpu_buffer, &cpu_buffer->commit_page);
854 cpu_buffer->write_stamp =
855 cpu_buffer->commit_page->page->time_stamp;
858 /* Now set the commit to the event's index */
859 local_set(&cpu_buffer->commit_page->page->commit, index);
863 rb_set_commit_to_write(struct ring_buffer_per_cpu *cpu_buffer)
866 * We only race with interrupts and NMIs on this CPU.
867 * If we own the commit event, then we can commit
868 * all others that interrupted us, since the interruptions
869 * are in stack format (they finish before they come
870 * back to us). This allows us to do a simple loop to
871 * assign the commit to the tail.
874 while (cpu_buffer->commit_page != cpu_buffer->tail_page) {
875 cpu_buffer->commit_page->page->commit =
876 cpu_buffer->commit_page->write;
877 rb_inc_page(cpu_buffer, &cpu_buffer->commit_page);
878 cpu_buffer->write_stamp =
879 cpu_buffer->commit_page->page->time_stamp;
880 /* add barrier to keep gcc from optimizing too much */
883 while (rb_commit_index(cpu_buffer) !=
884 rb_page_write(cpu_buffer->commit_page)) {
885 cpu_buffer->commit_page->page->commit =
886 cpu_buffer->commit_page->write;
890 /* again, keep gcc from optimizing */
894 * If an interrupt came in just after the first while loop
895 * and pushed the tail page forward, we will be left with
896 * a dangling commit that will never go forward.
898 if (unlikely(cpu_buffer->commit_page != cpu_buffer->tail_page))
902 static void rb_reset_reader_page(struct ring_buffer_per_cpu *cpu_buffer)
904 cpu_buffer->read_stamp = cpu_buffer->reader_page->page->time_stamp;
905 cpu_buffer->reader_page->read = 0;
908 static void rb_inc_iter(struct ring_buffer_iter *iter)
910 struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
913 * The iterator could be on the reader page (it starts there).
914 * But the head could have moved, since the reader was
915 * found. Check for this case and assign the iterator
916 * to the head page instead of next.
918 if (iter->head_page == cpu_buffer->reader_page)
919 iter->head_page = cpu_buffer->head_page;
921 rb_inc_page(cpu_buffer, &iter->head_page);
923 iter->read_stamp = iter->head_page->page->time_stamp;
928 * ring_buffer_update_event - update event type and data
929 * @event: the even to update
930 * @type: the type of event
931 * @length: the size of the event field in the ring buffer
933 * Update the type and data fields of the event. The length
934 * is the actual size that is written to the ring buffer,
935 * and with this, we can determine what to place into the
939 rb_update_event(struct ring_buffer_event *event,
940 unsigned type, unsigned length)
946 case RINGBUF_TYPE_PADDING:
949 case RINGBUF_TYPE_TIME_EXTEND:
950 event->len = DIV_ROUND_UP(RB_LEN_TIME_EXTEND, RB_ALIGNMENT);
953 case RINGBUF_TYPE_TIME_STAMP:
954 event->len = DIV_ROUND_UP(RB_LEN_TIME_STAMP, RB_ALIGNMENT);
957 case RINGBUF_TYPE_DATA:
958 length -= RB_EVNT_HDR_SIZE;
959 if (length > RB_MAX_SMALL_DATA) {
961 event->array[0] = length;
963 event->len = DIV_ROUND_UP(length, RB_ALIGNMENT);
970 static unsigned rb_calculate_event_length(unsigned length)
972 struct ring_buffer_event event; /* Used only for sizeof array */
974 /* zero length can cause confusions */
978 if (length > RB_MAX_SMALL_DATA)
979 length += sizeof(event.array[0]);
981 length += RB_EVNT_HDR_SIZE;
982 length = ALIGN(length, RB_ALIGNMENT);
987 static struct ring_buffer_event *
988 __rb_reserve_next(struct ring_buffer_per_cpu *cpu_buffer,
989 unsigned type, unsigned long length, u64 *ts)
991 struct buffer_page *tail_page, *head_page, *reader_page, *commit_page;
992 unsigned long tail, write;
993 struct ring_buffer *buffer = cpu_buffer->buffer;
994 struct ring_buffer_event *event;
996 bool lock_taken = false;
998 commit_page = cpu_buffer->commit_page;
999 /* we just need to protect against interrupts */
1001 tail_page = cpu_buffer->tail_page;
1002 write = local_add_return(length, &tail_page->write);
1003 tail = write - length;
1005 /* See if we shot pass the end of this buffer page */
1006 if (write > BUF_PAGE_SIZE) {
1007 struct buffer_page *next_page = tail_page;
1009 local_irq_save(flags);
1011 * Since the write to the buffer is still not
1012 * fully lockless, we must be careful with NMIs.
1013 * The locks in the writers are taken when a write
1014 * crosses to a new page. The locks protect against
1015 * races with the readers (this will soon be fixed
1016 * with a lockless solution).
1018 * Because we can not protect against NMIs, and we
1019 * want to keep traces reentrant, we need to manage
1020 * what happens when we are in an NMI.
1022 * NMIs can happen after we take the lock.
1023 * If we are in an NMI, only take the lock
1024 * if it is not already taken. Otherwise
1027 if (unlikely(in_nmi())) {
1028 if (!__raw_spin_trylock(&cpu_buffer->lock))
1031 __raw_spin_lock(&cpu_buffer->lock);
1035 rb_inc_page(cpu_buffer, &next_page);
1037 head_page = cpu_buffer->head_page;
1038 reader_page = cpu_buffer->reader_page;
1040 /* we grabbed the lock before incrementing */
1041 if (RB_WARN_ON(cpu_buffer, next_page == reader_page))
1045 * If for some reason, we had an interrupt storm that made
1046 * it all the way around the buffer, bail, and warn
1049 if (unlikely(next_page == commit_page)) {
1054 if (next_page == head_page) {
1055 if (!(buffer->flags & RB_FL_OVERWRITE))
1058 /* tail_page has not moved yet? */
1059 if (tail_page == cpu_buffer->tail_page) {
1060 /* count overflows */
1061 rb_update_overflow(cpu_buffer);
1063 rb_inc_page(cpu_buffer, &head_page);
1064 cpu_buffer->head_page = head_page;
1065 cpu_buffer->head_page->read = 0;
1070 * If the tail page is still the same as what we think
1071 * it is, then it is up to us to update the tail
1074 if (tail_page == cpu_buffer->tail_page) {
1075 local_set(&next_page->write, 0);
1076 local_set(&next_page->page->commit, 0);
1077 cpu_buffer->tail_page = next_page;
1079 /* reread the time stamp */
1080 *ts = ring_buffer_time_stamp(cpu_buffer->cpu);
1081 cpu_buffer->tail_page->page->time_stamp = *ts;
1085 * The actual tail page has moved forward.
1087 if (tail < BUF_PAGE_SIZE) {
1088 /* Mark the rest of the page with padding */
1089 event = __rb_page_index(tail_page, tail);
1090 event->type = RINGBUF_TYPE_PADDING;
1093 if (tail <= BUF_PAGE_SIZE)
1094 /* Set the write back to the previous setting */
1095 local_set(&tail_page->write, tail);
1098 * If this was a commit entry that failed,
1099 * increment that too
1101 if (tail_page == cpu_buffer->commit_page &&
1102 tail == rb_commit_index(cpu_buffer)) {
1103 rb_set_commit_to_write(cpu_buffer);
1106 __raw_spin_unlock(&cpu_buffer->lock);
1107 local_irq_restore(flags);
1109 /* fail and let the caller try again */
1110 return ERR_PTR(-EAGAIN);
1113 /* We reserved something on the buffer */
1115 if (RB_WARN_ON(cpu_buffer, write > BUF_PAGE_SIZE))
1118 event = __rb_page_index(tail_page, tail);
1119 rb_update_event(event, type, length);
1122 * If this is a commit and the tail is zero, then update
1123 * this page's time stamp.
1125 if (!tail && rb_is_commit(cpu_buffer, event))
1126 cpu_buffer->commit_page->page->time_stamp = *ts;
1132 if (tail <= BUF_PAGE_SIZE)
1133 local_set(&tail_page->write, tail);
1135 if (likely(lock_taken))
1136 __raw_spin_unlock(&cpu_buffer->lock);
1137 local_irq_restore(flags);
1142 rb_add_time_stamp(struct ring_buffer_per_cpu *cpu_buffer,
1143 u64 *ts, u64 *delta)
1145 struct ring_buffer_event *event;
1149 if (unlikely(*delta > (1ULL << 59) && !once++)) {
1150 printk(KERN_WARNING "Delta way too big! %llu"
1151 " ts=%llu write stamp = %llu\n",
1152 (unsigned long long)*delta,
1153 (unsigned long long)*ts,
1154 (unsigned long long)cpu_buffer->write_stamp);
1159 * The delta is too big, we to add a
1162 event = __rb_reserve_next(cpu_buffer,
1163 RINGBUF_TYPE_TIME_EXTEND,
1169 if (PTR_ERR(event) == -EAGAIN)
1172 /* Only a commited time event can update the write stamp */
1173 if (rb_is_commit(cpu_buffer, event)) {
1175 * If this is the first on the page, then we need to
1176 * update the page itself, and just put in a zero.
1178 if (rb_event_index(event)) {
1179 event->time_delta = *delta & TS_MASK;
1180 event->array[0] = *delta >> TS_SHIFT;
1182 cpu_buffer->commit_page->page->time_stamp = *ts;
1183 event->time_delta = 0;
1184 event->array[0] = 0;
1186 cpu_buffer->write_stamp = *ts;
1187 /* let the caller know this was the commit */
1190 /* Darn, this is just wasted space */
1191 event->time_delta = 0;
1192 event->array[0] = 0;
1201 static struct ring_buffer_event *
1202 rb_reserve_next_event(struct ring_buffer_per_cpu *cpu_buffer,
1203 unsigned type, unsigned long length)
1205 struct ring_buffer_event *event;
1212 * We allow for interrupts to reenter here and do a trace.
1213 * If one does, it will cause this original code to loop
1214 * back here. Even with heavy interrupts happening, this
1215 * should only happen a few times in a row. If this happens
1216 * 1000 times in a row, there must be either an interrupt
1217 * storm or we have something buggy.
1220 if (RB_WARN_ON(cpu_buffer, ++nr_loops > 1000))
1223 ts = ring_buffer_time_stamp(cpu_buffer->cpu);
1226 * Only the first commit can update the timestamp.
1227 * Yes there is a race here. If an interrupt comes in
1228 * just after the conditional and it traces too, then it
1229 * will also check the deltas. More than one timestamp may
1230 * also be made. But only the entry that did the actual
1231 * commit will be something other than zero.
1233 if (cpu_buffer->tail_page == cpu_buffer->commit_page &&
1234 rb_page_write(cpu_buffer->tail_page) ==
1235 rb_commit_index(cpu_buffer)) {
1237 delta = ts - cpu_buffer->write_stamp;
1239 /* make sure this delta is calculated here */
1242 /* Did the write stamp get updated already? */
1243 if (unlikely(ts < cpu_buffer->write_stamp))
1246 if (test_time_stamp(delta)) {
1248 commit = rb_add_time_stamp(cpu_buffer, &ts, &delta);
1250 if (commit == -EBUSY)
1253 if (commit == -EAGAIN)
1256 RB_WARN_ON(cpu_buffer, commit < 0);
1259 /* Non commits have zero deltas */
1262 event = __rb_reserve_next(cpu_buffer, type, length, &ts);
1263 if (PTR_ERR(event) == -EAGAIN)
1267 if (unlikely(commit))
1269 * Ouch! We needed a timestamp and it was commited. But
1270 * we didn't get our event reserved.
1272 rb_set_commit_to_write(cpu_buffer);
1277 * If the timestamp was commited, make the commit our entry
1278 * now so that we will update it when needed.
1281 rb_set_commit_event(cpu_buffer, event);
1282 else if (!rb_is_commit(cpu_buffer, event))
1285 event->time_delta = delta;
1290 static DEFINE_PER_CPU(int, rb_need_resched);
1293 * ring_buffer_lock_reserve - reserve a part of the buffer
1294 * @buffer: the ring buffer to reserve from
1295 * @length: the length of the data to reserve (excluding event header)
1297 * Returns a reseverd event on the ring buffer to copy directly to.
1298 * The user of this interface will need to get the body to write into
1299 * and can use the ring_buffer_event_data() interface.
1301 * The length is the length of the data needed, not the event length
1302 * which also includes the event header.
1304 * Must be paired with ring_buffer_unlock_commit, unless NULL is returned.
1305 * If NULL is returned, then nothing has been allocated or locked.
1307 struct ring_buffer_event *
1308 ring_buffer_lock_reserve(struct ring_buffer *buffer, unsigned long length)
1310 struct ring_buffer_per_cpu *cpu_buffer;
1311 struct ring_buffer_event *event;
1314 if (ring_buffer_flags != RB_BUFFERS_ON)
1317 if (atomic_read(&buffer->record_disabled))
1320 /* If we are tracing schedule, we don't want to recurse */
1321 resched = ftrace_preempt_disable();
1323 cpu = raw_smp_processor_id();
1325 if (!cpumask_test_cpu(cpu, buffer->cpumask))
1328 cpu_buffer = buffer->buffers[cpu];
1330 if (atomic_read(&cpu_buffer->record_disabled))
1333 length = rb_calculate_event_length(length);
1334 if (length > BUF_PAGE_SIZE)
1337 event = rb_reserve_next_event(cpu_buffer, RINGBUF_TYPE_DATA, length);
1342 * Need to store resched state on this cpu.
1343 * Only the first needs to.
1346 if (preempt_count() == 1)
1347 per_cpu(rb_need_resched, cpu) = resched;
1352 ftrace_preempt_enable(resched);
1355 EXPORT_SYMBOL_GPL(ring_buffer_lock_reserve);
1357 static void rb_commit(struct ring_buffer_per_cpu *cpu_buffer,
1358 struct ring_buffer_event *event)
1360 cpu_buffer->entries++;
1362 /* Only process further if we own the commit */
1363 if (!rb_is_commit(cpu_buffer, event))
1366 cpu_buffer->write_stamp += event->time_delta;
1368 rb_set_commit_to_write(cpu_buffer);
1372 * ring_buffer_unlock_commit - commit a reserved
1373 * @buffer: The buffer to commit to
1374 * @event: The event pointer to commit.
1376 * This commits the data to the ring buffer, and releases any locks held.
1378 * Must be paired with ring_buffer_lock_reserve.
1380 int ring_buffer_unlock_commit(struct ring_buffer *buffer,
1381 struct ring_buffer_event *event)
1383 struct ring_buffer_per_cpu *cpu_buffer;
1384 int cpu = raw_smp_processor_id();
1386 cpu_buffer = buffer->buffers[cpu];
1388 rb_commit(cpu_buffer, event);
1391 * Only the last preempt count needs to restore preemption.
1393 if (preempt_count() == 1)
1394 ftrace_preempt_enable(per_cpu(rb_need_resched, cpu));
1396 preempt_enable_no_resched_notrace();
1400 EXPORT_SYMBOL_GPL(ring_buffer_unlock_commit);
1403 * ring_buffer_write - write data to the buffer without reserving
1404 * @buffer: The ring buffer to write to.
1405 * @length: The length of the data being written (excluding the event header)
1406 * @data: The data to write to the buffer.
1408 * This is like ring_buffer_lock_reserve and ring_buffer_unlock_commit as
1409 * one function. If you already have the data to write to the buffer, it
1410 * may be easier to simply call this function.
1412 * Note, like ring_buffer_lock_reserve, the length is the length of the data
1413 * and not the length of the event which would hold the header.
1415 int ring_buffer_write(struct ring_buffer *buffer,
1416 unsigned long length,
1419 struct ring_buffer_per_cpu *cpu_buffer;
1420 struct ring_buffer_event *event;
1421 unsigned long event_length;
1426 if (ring_buffer_flags != RB_BUFFERS_ON)
1429 if (atomic_read(&buffer->record_disabled))
1432 resched = ftrace_preempt_disable();
1434 cpu = raw_smp_processor_id();
1436 if (!cpumask_test_cpu(cpu, buffer->cpumask))
1439 cpu_buffer = buffer->buffers[cpu];
1441 if (atomic_read(&cpu_buffer->record_disabled))
1444 event_length = rb_calculate_event_length(length);
1445 event = rb_reserve_next_event(cpu_buffer,
1446 RINGBUF_TYPE_DATA, event_length);
1450 body = rb_event_data(event);
1452 memcpy(body, data, length);
1454 rb_commit(cpu_buffer, event);
1458 ftrace_preempt_enable(resched);
1462 EXPORT_SYMBOL_GPL(ring_buffer_write);
1464 static int rb_per_cpu_empty(struct ring_buffer_per_cpu *cpu_buffer)
1466 struct buffer_page *reader = cpu_buffer->reader_page;
1467 struct buffer_page *head = cpu_buffer->head_page;
1468 struct buffer_page *commit = cpu_buffer->commit_page;
1470 return reader->read == rb_page_commit(reader) &&
1471 (commit == reader ||
1473 head->read == rb_page_commit(commit)));
1477 * ring_buffer_record_disable - stop all writes into the buffer
1478 * @buffer: The ring buffer to stop writes to.
1480 * This prevents all writes to the buffer. Any attempt to write
1481 * to the buffer after this will fail and return NULL.
1483 * The caller should call synchronize_sched() after this.
1485 void ring_buffer_record_disable(struct ring_buffer *buffer)
1487 atomic_inc(&buffer->record_disabled);
1489 EXPORT_SYMBOL_GPL(ring_buffer_record_disable);
1492 * ring_buffer_record_enable - enable writes to the buffer
1493 * @buffer: The ring buffer to enable writes
1495 * Note, multiple disables will need the same number of enables
1496 * to truely enable the writing (much like preempt_disable).
1498 void ring_buffer_record_enable(struct ring_buffer *buffer)
1500 atomic_dec(&buffer->record_disabled);
1502 EXPORT_SYMBOL_GPL(ring_buffer_record_enable);
1505 * ring_buffer_record_disable_cpu - stop all writes into the cpu_buffer
1506 * @buffer: The ring buffer to stop writes to.
1507 * @cpu: The CPU buffer to stop
1509 * This prevents all writes to the buffer. Any attempt to write
1510 * to the buffer after this will fail and return NULL.
1512 * The caller should call synchronize_sched() after this.
1514 void ring_buffer_record_disable_cpu(struct ring_buffer *buffer, int cpu)
1516 struct ring_buffer_per_cpu *cpu_buffer;
1518 if (!cpumask_test_cpu(cpu, buffer->cpumask))
1521 cpu_buffer = buffer->buffers[cpu];
1522 atomic_inc(&cpu_buffer->record_disabled);
1524 EXPORT_SYMBOL_GPL(ring_buffer_record_disable_cpu);
1527 * ring_buffer_record_enable_cpu - enable writes to the buffer
1528 * @buffer: The ring buffer to enable writes
1529 * @cpu: The CPU to enable.
1531 * Note, multiple disables will need the same number of enables
1532 * to truely enable the writing (much like preempt_disable).
1534 void ring_buffer_record_enable_cpu(struct ring_buffer *buffer, int cpu)
1536 struct ring_buffer_per_cpu *cpu_buffer;
1538 if (!cpumask_test_cpu(cpu, buffer->cpumask))
1541 cpu_buffer = buffer->buffers[cpu];
1542 atomic_dec(&cpu_buffer->record_disabled);
1544 EXPORT_SYMBOL_GPL(ring_buffer_record_enable_cpu);
1547 * ring_buffer_entries_cpu - get the number of entries in a cpu buffer
1548 * @buffer: The ring buffer
1549 * @cpu: The per CPU buffer to get the entries from.
1551 unsigned long ring_buffer_entries_cpu(struct ring_buffer *buffer, int cpu)
1553 struct ring_buffer_per_cpu *cpu_buffer;
1555 if (!cpumask_test_cpu(cpu, buffer->cpumask))
1558 cpu_buffer = buffer->buffers[cpu];
1559 return cpu_buffer->entries;
1561 EXPORT_SYMBOL_GPL(ring_buffer_entries_cpu);
1564 * ring_buffer_overrun_cpu - get the number of overruns in a cpu_buffer
1565 * @buffer: The ring buffer
1566 * @cpu: The per CPU buffer to get the number of overruns from
1568 unsigned long ring_buffer_overrun_cpu(struct ring_buffer *buffer, int cpu)
1570 struct ring_buffer_per_cpu *cpu_buffer;
1572 if (!cpumask_test_cpu(cpu, buffer->cpumask))
1575 cpu_buffer = buffer->buffers[cpu];
1576 return cpu_buffer->overrun;
1578 EXPORT_SYMBOL_GPL(ring_buffer_overrun_cpu);
1581 * ring_buffer_entries - get the number of entries in a buffer
1582 * @buffer: The ring buffer
1584 * Returns the total number of entries in the ring buffer
1587 unsigned long ring_buffer_entries(struct ring_buffer *buffer)
1589 struct ring_buffer_per_cpu *cpu_buffer;
1590 unsigned long entries = 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 entries += cpu_buffer->entries;
1601 EXPORT_SYMBOL_GPL(ring_buffer_entries);
1604 * ring_buffer_overrun_cpu - get the number of overruns in buffer
1605 * @buffer: The ring buffer
1607 * Returns the total number of overruns in the ring buffer
1610 unsigned long ring_buffer_overruns(struct ring_buffer *buffer)
1612 struct ring_buffer_per_cpu *cpu_buffer;
1613 unsigned long overruns = 0;
1616 /* if you care about this being correct, lock the buffer */
1617 for_each_buffer_cpu(buffer, cpu) {
1618 cpu_buffer = buffer->buffers[cpu];
1619 overruns += cpu_buffer->overrun;
1624 EXPORT_SYMBOL_GPL(ring_buffer_overruns);
1626 static void rb_iter_reset(struct ring_buffer_iter *iter)
1628 struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
1630 /* Iterator usage is expected to have record disabled */
1631 if (list_empty(&cpu_buffer->reader_page->list)) {
1632 iter->head_page = cpu_buffer->head_page;
1633 iter->head = cpu_buffer->head_page->read;
1635 iter->head_page = cpu_buffer->reader_page;
1636 iter->head = cpu_buffer->reader_page->read;
1639 iter->read_stamp = cpu_buffer->read_stamp;
1641 iter->read_stamp = iter->head_page->page->time_stamp;
1645 * ring_buffer_iter_reset - reset an iterator
1646 * @iter: The iterator to reset
1648 * Resets the iterator, so that it will start from the beginning
1651 void ring_buffer_iter_reset(struct ring_buffer_iter *iter)
1653 struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
1654 unsigned long flags;
1656 spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
1657 rb_iter_reset(iter);
1658 spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
1660 EXPORT_SYMBOL_GPL(ring_buffer_iter_reset);
1663 * ring_buffer_iter_empty - check if an iterator has no more to read
1664 * @iter: The iterator to check
1666 int ring_buffer_iter_empty(struct ring_buffer_iter *iter)
1668 struct ring_buffer_per_cpu *cpu_buffer;
1670 cpu_buffer = iter->cpu_buffer;
1672 return iter->head_page == cpu_buffer->commit_page &&
1673 iter->head == rb_commit_index(cpu_buffer);
1675 EXPORT_SYMBOL_GPL(ring_buffer_iter_empty);
1678 rb_update_read_stamp(struct ring_buffer_per_cpu *cpu_buffer,
1679 struct ring_buffer_event *event)
1683 switch (event->type) {
1684 case RINGBUF_TYPE_PADDING:
1687 case RINGBUF_TYPE_TIME_EXTEND:
1688 delta = event->array[0];
1690 delta += event->time_delta;
1691 cpu_buffer->read_stamp += delta;
1694 case RINGBUF_TYPE_TIME_STAMP:
1695 /* FIXME: not implemented */
1698 case RINGBUF_TYPE_DATA:
1699 cpu_buffer->read_stamp += event->time_delta;
1709 rb_update_iter_read_stamp(struct ring_buffer_iter *iter,
1710 struct ring_buffer_event *event)
1714 switch (event->type) {
1715 case RINGBUF_TYPE_PADDING:
1718 case RINGBUF_TYPE_TIME_EXTEND:
1719 delta = event->array[0];
1721 delta += event->time_delta;
1722 iter->read_stamp += delta;
1725 case RINGBUF_TYPE_TIME_STAMP:
1726 /* FIXME: not implemented */
1729 case RINGBUF_TYPE_DATA:
1730 iter->read_stamp += event->time_delta;
1739 static struct buffer_page *
1740 rb_get_reader_page(struct ring_buffer_per_cpu *cpu_buffer)
1742 struct buffer_page *reader = NULL;
1743 unsigned long flags;
1746 local_irq_save(flags);
1747 __raw_spin_lock(&cpu_buffer->lock);
1751 * This should normally only loop twice. But because the
1752 * start of the reader inserts an empty page, it causes
1753 * a case where we will loop three times. There should be no
1754 * reason to loop four times (that I know of).
1756 if (RB_WARN_ON(cpu_buffer, ++nr_loops > 3)) {
1761 reader = cpu_buffer->reader_page;
1763 /* If there's more to read, return this page */
1764 if (cpu_buffer->reader_page->read < rb_page_size(reader))
1767 /* Never should we have an index greater than the size */
1768 if (RB_WARN_ON(cpu_buffer,
1769 cpu_buffer->reader_page->read > rb_page_size(reader)))
1772 /* check if we caught up to the tail */
1774 if (cpu_buffer->commit_page == cpu_buffer->reader_page)
1778 * Splice the empty reader page into the list around the head.
1779 * Reset the reader page to size zero.
1782 reader = cpu_buffer->head_page;
1783 cpu_buffer->reader_page->list.next = reader->list.next;
1784 cpu_buffer->reader_page->list.prev = reader->list.prev;
1786 local_set(&cpu_buffer->reader_page->write, 0);
1787 local_set(&cpu_buffer->reader_page->page->commit, 0);
1789 /* Make the reader page now replace the head */
1790 reader->list.prev->next = &cpu_buffer->reader_page->list;
1791 reader->list.next->prev = &cpu_buffer->reader_page->list;
1794 * If the tail is on the reader, then we must set the head
1795 * to the inserted page, otherwise we set it one before.
1797 cpu_buffer->head_page = cpu_buffer->reader_page;
1799 if (cpu_buffer->commit_page != reader)
1800 rb_inc_page(cpu_buffer, &cpu_buffer->head_page);
1802 /* Finally update the reader page to the new head */
1803 cpu_buffer->reader_page = reader;
1804 rb_reset_reader_page(cpu_buffer);
1809 __raw_spin_unlock(&cpu_buffer->lock);
1810 local_irq_restore(flags);
1815 static void rb_advance_reader(struct ring_buffer_per_cpu *cpu_buffer)
1817 struct ring_buffer_event *event;
1818 struct buffer_page *reader;
1821 reader = rb_get_reader_page(cpu_buffer);
1823 /* This function should not be called when buffer is empty */
1824 if (RB_WARN_ON(cpu_buffer, !reader))
1827 event = rb_reader_event(cpu_buffer);
1829 if (event->type == RINGBUF_TYPE_DATA)
1830 cpu_buffer->entries--;
1832 rb_update_read_stamp(cpu_buffer, event);
1834 length = rb_event_length(event);
1835 cpu_buffer->reader_page->read += length;
1838 static void rb_advance_iter(struct ring_buffer_iter *iter)
1840 struct ring_buffer *buffer;
1841 struct ring_buffer_per_cpu *cpu_buffer;
1842 struct ring_buffer_event *event;
1845 cpu_buffer = iter->cpu_buffer;
1846 buffer = cpu_buffer->buffer;
1849 * Check if we are at the end of the buffer.
1851 if (iter->head >= rb_page_size(iter->head_page)) {
1852 if (RB_WARN_ON(buffer,
1853 iter->head_page == cpu_buffer->commit_page))
1859 event = rb_iter_head_event(iter);
1861 length = rb_event_length(event);
1864 * This should not be called to advance the header if we are
1865 * at the tail of the buffer.
1867 if (RB_WARN_ON(cpu_buffer,
1868 (iter->head_page == cpu_buffer->commit_page) &&
1869 (iter->head + length > rb_commit_index(cpu_buffer))))
1872 rb_update_iter_read_stamp(iter, event);
1874 iter->head += length;
1876 /* check for end of page padding */
1877 if ((iter->head >= rb_page_size(iter->head_page)) &&
1878 (iter->head_page != cpu_buffer->commit_page))
1879 rb_advance_iter(iter);
1882 static struct ring_buffer_event *
1883 rb_buffer_peek(struct ring_buffer *buffer, int cpu, u64 *ts)
1885 struct ring_buffer_per_cpu *cpu_buffer;
1886 struct ring_buffer_event *event;
1887 struct buffer_page *reader;
1890 if (!cpumask_test_cpu(cpu, buffer->cpumask))
1893 cpu_buffer = buffer->buffers[cpu];
1897 * We repeat when a timestamp is encountered. It is possible
1898 * to get multiple timestamps from an interrupt entering just
1899 * as one timestamp is about to be written. The max times
1900 * that this can happen is the number of nested interrupts we
1901 * can have. Nesting 10 deep of interrupts is clearly
1904 if (RB_WARN_ON(cpu_buffer, ++nr_loops > 10))
1907 reader = rb_get_reader_page(cpu_buffer);
1911 event = rb_reader_event(cpu_buffer);
1913 switch (event->type) {
1914 case RINGBUF_TYPE_PADDING:
1915 RB_WARN_ON(cpu_buffer, 1);
1916 rb_advance_reader(cpu_buffer);
1919 case RINGBUF_TYPE_TIME_EXTEND:
1920 /* Internal data, OK to advance */
1921 rb_advance_reader(cpu_buffer);
1924 case RINGBUF_TYPE_TIME_STAMP:
1925 /* FIXME: not implemented */
1926 rb_advance_reader(cpu_buffer);
1929 case RINGBUF_TYPE_DATA:
1931 *ts = cpu_buffer->read_stamp + event->time_delta;
1932 ring_buffer_normalize_time_stamp(cpu_buffer->cpu, ts);
1942 EXPORT_SYMBOL_GPL(ring_buffer_peek);
1944 static struct ring_buffer_event *
1945 rb_iter_peek(struct ring_buffer_iter *iter, u64 *ts)
1947 struct ring_buffer *buffer;
1948 struct ring_buffer_per_cpu *cpu_buffer;
1949 struct ring_buffer_event *event;
1952 if (ring_buffer_iter_empty(iter))
1955 cpu_buffer = iter->cpu_buffer;
1956 buffer = cpu_buffer->buffer;
1960 * We repeat when a timestamp is encountered. It is possible
1961 * to get multiple timestamps from an interrupt entering just
1962 * as one timestamp is about to be written. The max times
1963 * that this can happen is the number of nested interrupts we
1964 * can have. Nesting 10 deep of interrupts is clearly
1967 if (RB_WARN_ON(cpu_buffer, ++nr_loops > 10))
1970 if (rb_per_cpu_empty(cpu_buffer))
1973 event = rb_iter_head_event(iter);
1975 switch (event->type) {
1976 case RINGBUF_TYPE_PADDING:
1980 case RINGBUF_TYPE_TIME_EXTEND:
1981 /* Internal data, OK to advance */
1982 rb_advance_iter(iter);
1985 case RINGBUF_TYPE_TIME_STAMP:
1986 /* FIXME: not implemented */
1987 rb_advance_iter(iter);
1990 case RINGBUF_TYPE_DATA:
1992 *ts = iter->read_stamp + event->time_delta;
1993 ring_buffer_normalize_time_stamp(cpu_buffer->cpu, ts);
2003 EXPORT_SYMBOL_GPL(ring_buffer_iter_peek);
2006 * ring_buffer_peek - peek at the next event to be read
2007 * @buffer: The ring buffer to read
2008 * @cpu: The cpu to peak at
2009 * @ts: The timestamp counter of this event.
2011 * This will return the event that will be read next, but does
2012 * not consume the data.
2014 struct ring_buffer_event *
2015 ring_buffer_peek(struct ring_buffer *buffer, int cpu, u64 *ts)
2017 struct ring_buffer_per_cpu *cpu_buffer = buffer->buffers[cpu];
2018 struct ring_buffer_event *event;
2019 unsigned long flags;
2021 spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
2022 event = rb_buffer_peek(buffer, cpu, ts);
2023 spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
2029 * ring_buffer_iter_peek - peek at the next event to be read
2030 * @iter: The ring buffer iterator
2031 * @ts: The timestamp counter of this event.
2033 * This will return the event that will be read next, but does
2034 * not increment the iterator.
2036 struct ring_buffer_event *
2037 ring_buffer_iter_peek(struct ring_buffer_iter *iter, u64 *ts)
2039 struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
2040 struct ring_buffer_event *event;
2041 unsigned long flags;
2043 spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
2044 event = rb_iter_peek(iter, ts);
2045 spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
2051 * ring_buffer_consume - return an event and consume it
2052 * @buffer: The ring buffer to get the next event from
2054 * Returns the next event in the ring buffer, and that event is consumed.
2055 * Meaning, that sequential reads will keep returning a different event,
2056 * and eventually empty the ring buffer if the producer is slower.
2058 struct ring_buffer_event *
2059 ring_buffer_consume(struct ring_buffer *buffer, int cpu, u64 *ts)
2061 struct ring_buffer_per_cpu *cpu_buffer = buffer->buffers[cpu];
2062 struct ring_buffer_event *event;
2063 unsigned long flags;
2065 if (!cpumask_test_cpu(cpu, buffer->cpumask))
2068 spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
2070 event = rb_buffer_peek(buffer, cpu, ts);
2074 rb_advance_reader(cpu_buffer);
2077 spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
2081 EXPORT_SYMBOL_GPL(ring_buffer_consume);
2084 * ring_buffer_read_start - start a non consuming read of the buffer
2085 * @buffer: The ring buffer to read from
2086 * @cpu: The cpu buffer to iterate over
2088 * This starts up an iteration through the buffer. It also disables
2089 * the recording to the buffer until the reading is finished.
2090 * This prevents the reading from being corrupted. This is not
2091 * a consuming read, so a producer is not expected.
2093 * Must be paired with ring_buffer_finish.
2095 struct ring_buffer_iter *
2096 ring_buffer_read_start(struct ring_buffer *buffer, int cpu)
2098 struct ring_buffer_per_cpu *cpu_buffer;
2099 struct ring_buffer_iter *iter;
2100 unsigned long flags;
2102 if (!cpumask_test_cpu(cpu, buffer->cpumask))
2105 iter = kmalloc(sizeof(*iter), GFP_KERNEL);
2109 cpu_buffer = buffer->buffers[cpu];
2111 iter->cpu_buffer = cpu_buffer;
2113 atomic_inc(&cpu_buffer->record_disabled);
2114 synchronize_sched();
2116 spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
2117 __raw_spin_lock(&cpu_buffer->lock);
2118 rb_iter_reset(iter);
2119 __raw_spin_unlock(&cpu_buffer->lock);
2120 spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
2124 EXPORT_SYMBOL_GPL(ring_buffer_read_start);
2127 * ring_buffer_finish - finish reading the iterator of the buffer
2128 * @iter: The iterator retrieved by ring_buffer_start
2130 * This re-enables the recording to the buffer, and frees the
2134 ring_buffer_read_finish(struct ring_buffer_iter *iter)
2136 struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
2138 atomic_dec(&cpu_buffer->record_disabled);
2141 EXPORT_SYMBOL_GPL(ring_buffer_read_finish);
2144 * ring_buffer_read - read the next item in the ring buffer by the iterator
2145 * @iter: The ring buffer iterator
2146 * @ts: The time stamp of the event read.
2148 * This reads the next event in the ring buffer and increments the iterator.
2150 struct ring_buffer_event *
2151 ring_buffer_read(struct ring_buffer_iter *iter, u64 *ts)
2153 struct ring_buffer_event *event;
2154 struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
2155 unsigned long flags;
2157 spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
2158 event = rb_iter_peek(iter, ts);
2162 rb_advance_iter(iter);
2164 spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
2168 EXPORT_SYMBOL_GPL(ring_buffer_read);
2171 * ring_buffer_size - return the size of the ring buffer (in bytes)
2172 * @buffer: The ring buffer.
2174 unsigned long ring_buffer_size(struct ring_buffer *buffer)
2176 return BUF_PAGE_SIZE * buffer->pages;
2178 EXPORT_SYMBOL_GPL(ring_buffer_size);
2181 rb_reset_cpu(struct ring_buffer_per_cpu *cpu_buffer)
2183 cpu_buffer->head_page
2184 = list_entry(cpu_buffer->pages.next, struct buffer_page, list);
2185 local_set(&cpu_buffer->head_page->write, 0);
2186 local_set(&cpu_buffer->head_page->page->commit, 0);
2188 cpu_buffer->head_page->read = 0;
2190 cpu_buffer->tail_page = cpu_buffer->head_page;
2191 cpu_buffer->commit_page = cpu_buffer->head_page;
2193 INIT_LIST_HEAD(&cpu_buffer->reader_page->list);
2194 local_set(&cpu_buffer->reader_page->write, 0);
2195 local_set(&cpu_buffer->reader_page->page->commit, 0);
2196 cpu_buffer->reader_page->read = 0;
2198 cpu_buffer->overrun = 0;
2199 cpu_buffer->entries = 0;
2201 cpu_buffer->write_stamp = 0;
2202 cpu_buffer->read_stamp = 0;
2206 * ring_buffer_reset_cpu - reset a ring buffer per CPU buffer
2207 * @buffer: The ring buffer to reset a per cpu buffer of
2208 * @cpu: The CPU buffer to be reset
2210 void ring_buffer_reset_cpu(struct ring_buffer *buffer, int cpu)
2212 struct ring_buffer_per_cpu *cpu_buffer = buffer->buffers[cpu];
2213 unsigned long flags;
2215 if (!cpumask_test_cpu(cpu, buffer->cpumask))
2218 spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
2220 __raw_spin_lock(&cpu_buffer->lock);
2222 rb_reset_cpu(cpu_buffer);
2224 __raw_spin_unlock(&cpu_buffer->lock);
2226 spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
2228 EXPORT_SYMBOL_GPL(ring_buffer_reset_cpu);
2231 * ring_buffer_reset - reset a ring buffer
2232 * @buffer: The ring buffer to reset all cpu buffers
2234 void ring_buffer_reset(struct ring_buffer *buffer)
2238 for_each_buffer_cpu(buffer, cpu)
2239 ring_buffer_reset_cpu(buffer, cpu);
2241 EXPORT_SYMBOL_GPL(ring_buffer_reset);
2244 * rind_buffer_empty - is the ring buffer empty?
2245 * @buffer: The ring buffer to test
2247 int ring_buffer_empty(struct ring_buffer *buffer)
2249 struct ring_buffer_per_cpu *cpu_buffer;
2252 /* yes this is racy, but if you don't like the race, lock the buffer */
2253 for_each_buffer_cpu(buffer, cpu) {
2254 cpu_buffer = buffer->buffers[cpu];
2255 if (!rb_per_cpu_empty(cpu_buffer))
2260 EXPORT_SYMBOL_GPL(ring_buffer_empty);
2263 * ring_buffer_empty_cpu - is a cpu buffer of a ring buffer empty?
2264 * @buffer: The ring buffer
2265 * @cpu: The CPU buffer to test
2267 int ring_buffer_empty_cpu(struct ring_buffer *buffer, int cpu)
2269 struct ring_buffer_per_cpu *cpu_buffer;
2271 if (!cpumask_test_cpu(cpu, buffer->cpumask))
2274 cpu_buffer = buffer->buffers[cpu];
2275 return rb_per_cpu_empty(cpu_buffer);
2277 EXPORT_SYMBOL_GPL(ring_buffer_empty_cpu);
2280 * ring_buffer_swap_cpu - swap a CPU buffer between two ring buffers
2281 * @buffer_a: One buffer to swap with
2282 * @buffer_b: The other buffer to swap with
2284 * This function is useful for tracers that want to take a "snapshot"
2285 * of a CPU buffer and has another back up buffer lying around.
2286 * it is expected that the tracer handles the cpu buffer not being
2287 * used at the moment.
2289 int ring_buffer_swap_cpu(struct ring_buffer *buffer_a,
2290 struct ring_buffer *buffer_b, int cpu)
2292 struct ring_buffer_per_cpu *cpu_buffer_a;
2293 struct ring_buffer_per_cpu *cpu_buffer_b;
2295 if (!cpumask_test_cpu(cpu, buffer_a->cpumask) ||
2296 !cpumask_test_cpu(cpu, buffer_b->cpumask))
2299 /* At least make sure the two buffers are somewhat the same */
2300 if (buffer_a->pages != buffer_b->pages)
2303 if (ring_buffer_flags != RB_BUFFERS_ON)
2306 if (atomic_read(&buffer_a->record_disabled))
2309 if (atomic_read(&buffer_b->record_disabled))
2312 cpu_buffer_a = buffer_a->buffers[cpu];
2313 cpu_buffer_b = buffer_b->buffers[cpu];
2315 if (atomic_read(&cpu_buffer_a->record_disabled))
2318 if (atomic_read(&cpu_buffer_b->record_disabled))
2322 * We can't do a synchronize_sched here because this
2323 * function can be called in atomic context.
2324 * Normally this will be called from the same CPU as cpu.
2325 * If not it's up to the caller to protect this.
2327 atomic_inc(&cpu_buffer_a->record_disabled);
2328 atomic_inc(&cpu_buffer_b->record_disabled);
2330 buffer_a->buffers[cpu] = cpu_buffer_b;
2331 buffer_b->buffers[cpu] = cpu_buffer_a;
2333 cpu_buffer_b->buffer = buffer_a;
2334 cpu_buffer_a->buffer = buffer_b;
2336 atomic_dec(&cpu_buffer_a->record_disabled);
2337 atomic_dec(&cpu_buffer_b->record_disabled);
2341 EXPORT_SYMBOL_GPL(ring_buffer_swap_cpu);
2343 static void rb_remove_entries(struct ring_buffer_per_cpu *cpu_buffer,
2344 struct buffer_data_page *bpage,
2345 unsigned int offset)
2347 struct ring_buffer_event *event;
2350 __raw_spin_lock(&cpu_buffer->lock);
2351 for (head = offset; head < local_read(&bpage->commit);
2352 head += rb_event_length(event)) {
2354 event = __rb_data_page_index(bpage, head);
2355 if (RB_WARN_ON(cpu_buffer, rb_null_event(event)))
2357 /* Only count data entries */
2358 if (event->type != RINGBUF_TYPE_DATA)
2360 cpu_buffer->entries--;
2362 __raw_spin_unlock(&cpu_buffer->lock);
2366 * ring_buffer_alloc_read_page - allocate a page to read from buffer
2367 * @buffer: the buffer to allocate for.
2369 * This function is used in conjunction with ring_buffer_read_page.
2370 * When reading a full page from the ring buffer, these functions
2371 * can be used to speed up the process. The calling function should
2372 * allocate a few pages first with this function. Then when it
2373 * needs to get pages from the ring buffer, it passes the result
2374 * of this function into ring_buffer_read_page, which will swap
2375 * the page that was allocated, with the read page of the buffer.
2378 * The page allocated, or NULL on error.
2380 void *ring_buffer_alloc_read_page(struct ring_buffer *buffer)
2383 struct buffer_data_page *bpage;
2385 addr = __get_free_page(GFP_KERNEL);
2389 bpage = (void *)addr;
2395 * ring_buffer_free_read_page - free an allocated read page
2396 * @buffer: the buffer the page was allocate for
2397 * @data: the page to free
2399 * Free a page allocated from ring_buffer_alloc_read_page.
2401 void ring_buffer_free_read_page(struct ring_buffer *buffer, void *data)
2403 free_page((unsigned long)data);
2407 * ring_buffer_read_page - extract a page from the ring buffer
2408 * @buffer: buffer to extract from
2409 * @data_page: the page to use allocated from ring_buffer_alloc_read_page
2410 * @cpu: the cpu of the buffer to extract
2411 * @full: should the extraction only happen when the page is full.
2413 * This function will pull out a page from the ring buffer and consume it.
2414 * @data_page must be the address of the variable that was returned
2415 * from ring_buffer_alloc_read_page. This is because the page might be used
2416 * to swap with a page in the ring buffer.
2419 * rpage = ring_buffer_alloc_read_page(buffer);
2422 * ret = ring_buffer_read_page(buffer, &rpage, cpu, 0);
2424 * process_page(rpage, ret);
2426 * When @full is set, the function will not return true unless
2427 * the writer is off the reader page.
2429 * Note: it is up to the calling functions to handle sleeps and wakeups.
2430 * The ring buffer can be used anywhere in the kernel and can not
2431 * blindly call wake_up. The layer that uses the ring buffer must be
2432 * responsible for that.
2435 * >=0 if data has been transferred, returns the offset of consumed data.
2436 * <0 if no data has been transferred.
2438 int ring_buffer_read_page(struct ring_buffer *buffer,
2439 void **data_page, int cpu, int full)
2441 struct ring_buffer_per_cpu *cpu_buffer = buffer->buffers[cpu];
2442 struct ring_buffer_event *event;
2443 struct buffer_data_page *bpage;
2444 unsigned long flags;
2455 spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
2458 * rb_buffer_peek will get the next ring buffer if
2459 * the current reader page is empty.
2461 event = rb_buffer_peek(buffer, cpu, NULL);
2465 /* check for data */
2466 if (!local_read(&cpu_buffer->reader_page->page->commit))
2469 read = cpu_buffer->reader_page->read;
2471 * If the writer is already off of the read page, then simply
2472 * switch the read page with the given page. Otherwise
2473 * we need to copy the data from the reader to the writer.
2475 if (cpu_buffer->reader_page == cpu_buffer->commit_page) {
2476 unsigned int commit = rb_page_commit(cpu_buffer->reader_page);
2477 struct buffer_data_page *rpage = cpu_buffer->reader_page->page;
2481 /* The writer is still on the reader page, we must copy */
2482 memcpy(bpage->data + read, rpage->data + read, commit - read);
2484 /* consume what was read */
2485 cpu_buffer->reader_page->read = commit;
2488 local_set(&bpage->commit, commit);
2490 bpage->time_stamp = rpage->time_stamp;
2492 /* swap the pages */
2493 rb_init_page(bpage);
2494 bpage = cpu_buffer->reader_page->page;
2495 cpu_buffer->reader_page->page = *data_page;
2496 cpu_buffer->reader_page->read = 0;
2501 /* update the entry counter */
2502 rb_remove_entries(cpu_buffer, bpage, read);
2504 spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
2510 rb_simple_read(struct file *filp, char __user *ubuf,
2511 size_t cnt, loff_t *ppos)
2513 unsigned long *p = filp->private_data;
2517 if (test_bit(RB_BUFFERS_DISABLED_BIT, p))
2518 r = sprintf(buf, "permanently disabled\n");
2520 r = sprintf(buf, "%d\n", test_bit(RB_BUFFERS_ON_BIT, p));
2522 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2526 rb_simple_write(struct file *filp, const char __user *ubuf,
2527 size_t cnt, loff_t *ppos)
2529 unsigned long *p = filp->private_data;
2534 if (cnt >= sizeof(buf))
2537 if (copy_from_user(&buf, ubuf, cnt))
2542 ret = strict_strtoul(buf, 10, &val);
2547 set_bit(RB_BUFFERS_ON_BIT, p);
2549 clear_bit(RB_BUFFERS_ON_BIT, p);
2556 static struct file_operations rb_simple_fops = {
2557 .open = tracing_open_generic,
2558 .read = rb_simple_read,
2559 .write = rb_simple_write,
2563 static __init int rb_init_debugfs(void)
2565 struct dentry *d_tracer;
2566 struct dentry *entry;
2568 d_tracer = tracing_init_dentry();
2570 entry = debugfs_create_file("tracing_on", 0644, d_tracer,
2571 &ring_buffer_flags, &rb_simple_fops);
2573 pr_warning("Could not create debugfs 'tracing_on' entry\n");
2578 fs_initcall(rb_init_debugfs);