4 * Copyright (C) 2008 Steven Rostedt <srostedt@redhat.com>
6 #include <linux/ring_buffer.h>
7 #include <linux/trace_clock.h>
8 #include <linux/ftrace_irq.h>
9 #include <linux/spinlock.h>
10 #include <linux/debugfs.h>
11 #include <linux/uaccess.h>
12 #include <linux/hardirq.h>
13 #include <linux/module.h>
14 #include <linux/percpu.h>
15 #include <linux/mutex.h>
16 #include <linux/init.h>
17 #include <linux/hash.h>
18 #include <linux/list.h>
19 #include <linux/cpu.h>
25 * The ring buffer is made up of a list of pages. A separate list of pages is
26 * allocated for each CPU. A writer may only write to a buffer that is
27 * associated with the CPU it is currently executing on. A reader may read
28 * from any per cpu buffer.
30 * The reader is special. For each per cpu buffer, the reader has its own
31 * reader page. When a reader has read the entire reader page, this reader
32 * page is swapped with another page in the ring buffer.
34 * Now, as long as the writer is off the reader page, the reader can do what
35 * ever it wants with that page. The writer will never write to that page
36 * again (as long as it is out of the ring buffer).
38 * Here's some silly ASCII art.
41 * |reader| RING BUFFER
43 * +------+ +---+ +---+ +---+
52 * |reader| RING BUFFER
53 * |page |------------------v
54 * +------+ +---+ +---+ +---+
63 * |reader| RING BUFFER
64 * |page |------------------v
65 * +------+ +---+ +---+ +---+
70 * +------------------------------+
74 * |buffer| RING BUFFER
75 * |page |------------------v
76 * +------+ +---+ +---+ +---+
78 * | New +---+ +---+ +---+
81 * +------------------------------+
84 * After we make this swap, the reader can hand this page off to the splice
85 * code and be done with it. It can even allocate a new page if it needs to
86 * and swap that into the ring buffer.
88 * We will be using cmpxchg soon to make all this lockless.
93 * A fast way to enable or disable all ring buffers is to
94 * call tracing_on or tracing_off. Turning off the ring buffers
95 * prevents all ring buffers from being recorded to.
96 * Turning this switch on, makes it OK to write to the
97 * ring buffer, if the ring buffer is enabled itself.
99 * There's three layers that must be on in order to write
100 * to the ring buffer.
102 * 1) This global flag must be set.
103 * 2) The ring buffer must be enabled for recording.
104 * 3) The per cpu buffer must be enabled for recording.
106 * In case of an anomaly, this global flag has a bit set that
107 * will permantly disable all ring buffers.
111 * Global flag to disable all recording to ring buffers
112 * This has two bits: ON, DISABLED
116 * 0 0 : ring buffers are off
117 * 1 0 : ring buffers are on
118 * X 1 : ring buffers are permanently disabled
122 RB_BUFFERS_ON_BIT = 0,
123 RB_BUFFERS_DISABLED_BIT = 1,
127 RB_BUFFERS_ON = 1 << RB_BUFFERS_ON_BIT,
128 RB_BUFFERS_DISABLED = 1 << RB_BUFFERS_DISABLED_BIT,
131 static unsigned long ring_buffer_flags __read_mostly = RB_BUFFERS_ON;
133 #define BUF_PAGE_HDR_SIZE offsetof(struct buffer_data_page, data)
136 * tracing_on - enable all tracing buffers
138 * This function enables all tracing buffers that may have been
139 * disabled with tracing_off.
141 void tracing_on(void)
143 set_bit(RB_BUFFERS_ON_BIT, &ring_buffer_flags);
145 EXPORT_SYMBOL_GPL(tracing_on);
148 * tracing_off - turn off all tracing buffers
150 * This function stops all tracing buffers from recording data.
151 * It does not disable any overhead the tracers themselves may
152 * be causing. This function simply causes all recording to
153 * the ring buffers to fail.
155 void tracing_off(void)
157 clear_bit(RB_BUFFERS_ON_BIT, &ring_buffer_flags);
159 EXPORT_SYMBOL_GPL(tracing_off);
162 * tracing_off_permanent - permanently disable ring buffers
164 * This function, once called, will disable all ring buffers
167 void tracing_off_permanent(void)
169 set_bit(RB_BUFFERS_DISABLED_BIT, &ring_buffer_flags);
173 * tracing_is_on - show state of ring buffers enabled
175 int tracing_is_on(void)
177 return ring_buffer_flags == RB_BUFFERS_ON;
179 EXPORT_SYMBOL_GPL(tracing_is_on);
183 #define RB_EVNT_HDR_SIZE (offsetof(struct ring_buffer_event, array))
184 #define RB_ALIGNMENT 4U
185 #define RB_MAX_SMALL_DATA 28
188 RB_LEN_TIME_EXTEND = 8,
189 RB_LEN_TIME_STAMP = 16,
192 static inline int rb_null_event(struct ring_buffer_event *event)
194 return event->type == RINGBUF_TYPE_PADDING && event->time_delta == 0;
197 static inline int rb_discarded_event(struct ring_buffer_event *event)
199 return event->type == RINGBUF_TYPE_PADDING && event->time_delta;
202 static void rb_event_set_padding(struct ring_buffer_event *event)
204 event->type = RINGBUF_TYPE_PADDING;
205 event->time_delta = 0;
209 rb_event_data_length(struct ring_buffer_event *event)
214 length = event->len * RB_ALIGNMENT;
216 length = event->array[0];
217 return length + RB_EVNT_HDR_SIZE;
220 /* inline for ring buffer fast paths */
222 rb_event_length(struct ring_buffer_event *event)
224 switch (event->type) {
225 case RINGBUF_TYPE_PADDING:
226 if (rb_null_event(event))
229 return rb_event_data_length(event);
231 case RINGBUF_TYPE_TIME_EXTEND:
232 return RB_LEN_TIME_EXTEND;
234 case RINGBUF_TYPE_TIME_STAMP:
235 return RB_LEN_TIME_STAMP;
237 case RINGBUF_TYPE_DATA:
238 return rb_event_data_length(event);
247 * ring_buffer_event_length - return the length of the event
248 * @event: the event to get the length of
250 unsigned ring_buffer_event_length(struct ring_buffer_event *event)
252 unsigned length = rb_event_length(event);
253 if (event->type != RINGBUF_TYPE_DATA)
255 length -= RB_EVNT_HDR_SIZE;
256 if (length > RB_MAX_SMALL_DATA + sizeof(event->array[0]))
257 length -= sizeof(event->array[0]);
260 EXPORT_SYMBOL_GPL(ring_buffer_event_length);
262 /* inline for ring buffer fast paths */
264 rb_event_data(struct ring_buffer_event *event)
266 BUG_ON(event->type != RINGBUF_TYPE_DATA);
267 /* If length is in len field, then array[0] has the data */
269 return (void *)&event->array[0];
270 /* Otherwise length is in array[0] and array[1] has the data */
271 return (void *)&event->array[1];
275 * ring_buffer_event_data - return the data of the event
276 * @event: the event to get the data from
278 void *ring_buffer_event_data(struct ring_buffer_event *event)
280 return rb_event_data(event);
282 EXPORT_SYMBOL_GPL(ring_buffer_event_data);
284 #define for_each_buffer_cpu(buffer, cpu) \
285 for_each_cpu(cpu, buffer->cpumask)
288 #define TS_MASK ((1ULL << TS_SHIFT) - 1)
289 #define TS_DELTA_TEST (~TS_MASK)
291 struct buffer_data_page {
292 u64 time_stamp; /* page time stamp */
293 local_t commit; /* write committed index */
294 unsigned char data[]; /* data of buffer page */
298 local_t write; /* index for next write */
299 unsigned read; /* index for next read */
300 struct list_head list; /* list of free pages */
301 struct buffer_data_page *page; /* Actual data page */
304 static void rb_init_page(struct buffer_data_page *bpage)
306 local_set(&bpage->commit, 0);
310 * ring_buffer_page_len - the size of data on the page.
311 * @page: The page to read
313 * Returns the amount of data on the page, including buffer page header.
315 size_t ring_buffer_page_len(void *page)
317 return local_read(&((struct buffer_data_page *)page)->commit)
322 * Also stolen from mm/slob.c. Thanks to Mathieu Desnoyers for pointing
325 static void free_buffer_page(struct buffer_page *bpage)
327 free_page((unsigned long)bpage->page);
332 * We need to fit the time_stamp delta into 27 bits.
334 static inline int test_time_stamp(u64 delta)
336 if (delta & TS_DELTA_TEST)
341 #define BUF_PAGE_SIZE (PAGE_SIZE - BUF_PAGE_HDR_SIZE)
344 * head_page == tail_page && head == tail then buffer is empty.
346 struct ring_buffer_per_cpu {
348 struct ring_buffer *buffer;
349 spinlock_t reader_lock; /* serialize readers */
351 struct lock_class_key lock_key;
352 struct list_head pages;
353 struct buffer_page *head_page; /* read from head */
354 struct buffer_page *tail_page; /* write to tail */
355 struct buffer_page *commit_page; /* committed pages */
356 struct buffer_page *reader_page;
357 unsigned long overrun;
358 unsigned long entries;
361 atomic_t record_disabled;
368 atomic_t record_disabled;
369 cpumask_var_t cpumask;
373 struct ring_buffer_per_cpu **buffers;
375 #ifdef CONFIG_HOTPLUG_CPU
376 struct notifier_block cpu_notify;
381 struct ring_buffer_iter {
382 struct ring_buffer_per_cpu *cpu_buffer;
384 struct buffer_page *head_page;
388 /* buffer may be either ring_buffer or ring_buffer_per_cpu */
389 #define RB_WARN_ON(buffer, cond) \
391 int _____ret = unlikely(cond); \
393 atomic_inc(&buffer->record_disabled); \
399 /* Up this if you want to test the TIME_EXTENTS and normalization */
400 #define DEBUG_SHIFT 0
402 u64 ring_buffer_time_stamp(struct ring_buffer *buffer, int cpu)
406 preempt_disable_notrace();
407 /* shift to debug/test normalization and TIME_EXTENTS */
408 time = buffer->clock() << DEBUG_SHIFT;
409 preempt_enable_no_resched_notrace();
413 EXPORT_SYMBOL_GPL(ring_buffer_time_stamp);
415 void ring_buffer_normalize_time_stamp(struct ring_buffer *buffer,
418 /* Just stupid testing the normalize function and deltas */
421 EXPORT_SYMBOL_GPL(ring_buffer_normalize_time_stamp);
424 * check_pages - integrity check of buffer pages
425 * @cpu_buffer: CPU buffer with pages to test
427 * As a safety measure we check to make sure the data pages have not
430 static int rb_check_pages(struct ring_buffer_per_cpu *cpu_buffer)
432 struct list_head *head = &cpu_buffer->pages;
433 struct buffer_page *bpage, *tmp;
435 if (RB_WARN_ON(cpu_buffer, head->next->prev != head))
437 if (RB_WARN_ON(cpu_buffer, head->prev->next != head))
440 list_for_each_entry_safe(bpage, tmp, head, list) {
441 if (RB_WARN_ON(cpu_buffer,
442 bpage->list.next->prev != &bpage->list))
444 if (RB_WARN_ON(cpu_buffer,
445 bpage->list.prev->next != &bpage->list))
452 static int rb_allocate_pages(struct ring_buffer_per_cpu *cpu_buffer,
455 struct list_head *head = &cpu_buffer->pages;
456 struct buffer_page *bpage, *tmp;
461 for (i = 0; i < nr_pages; i++) {
462 bpage = kzalloc_node(ALIGN(sizeof(*bpage), cache_line_size()),
463 GFP_KERNEL, cpu_to_node(cpu_buffer->cpu));
466 list_add(&bpage->list, &pages);
468 addr = __get_free_page(GFP_KERNEL);
471 bpage->page = (void *)addr;
472 rb_init_page(bpage->page);
475 list_splice(&pages, head);
477 rb_check_pages(cpu_buffer);
482 list_for_each_entry_safe(bpage, tmp, &pages, list) {
483 list_del_init(&bpage->list);
484 free_buffer_page(bpage);
489 static struct ring_buffer_per_cpu *
490 rb_allocate_cpu_buffer(struct ring_buffer *buffer, int cpu)
492 struct ring_buffer_per_cpu *cpu_buffer;
493 struct buffer_page *bpage;
497 cpu_buffer = kzalloc_node(ALIGN(sizeof(*cpu_buffer), cache_line_size()),
498 GFP_KERNEL, cpu_to_node(cpu));
502 cpu_buffer->cpu = cpu;
503 cpu_buffer->buffer = buffer;
504 spin_lock_init(&cpu_buffer->reader_lock);
505 cpu_buffer->lock = (raw_spinlock_t)__RAW_SPIN_LOCK_UNLOCKED;
506 INIT_LIST_HEAD(&cpu_buffer->pages);
508 bpage = kzalloc_node(ALIGN(sizeof(*bpage), cache_line_size()),
509 GFP_KERNEL, cpu_to_node(cpu));
511 goto fail_free_buffer;
513 cpu_buffer->reader_page = bpage;
514 addr = __get_free_page(GFP_KERNEL);
516 goto fail_free_reader;
517 bpage->page = (void *)addr;
518 rb_init_page(bpage->page);
520 INIT_LIST_HEAD(&cpu_buffer->reader_page->list);
522 ret = rb_allocate_pages(cpu_buffer, buffer->pages);
524 goto fail_free_reader;
526 cpu_buffer->head_page
527 = list_entry(cpu_buffer->pages.next, struct buffer_page, list);
528 cpu_buffer->tail_page = cpu_buffer->commit_page = cpu_buffer->head_page;
533 free_buffer_page(cpu_buffer->reader_page);
540 static void rb_free_cpu_buffer(struct ring_buffer_per_cpu *cpu_buffer)
542 struct list_head *head = &cpu_buffer->pages;
543 struct buffer_page *bpage, *tmp;
545 free_buffer_page(cpu_buffer->reader_page);
547 list_for_each_entry_safe(bpage, tmp, head, list) {
548 list_del_init(&bpage->list);
549 free_buffer_page(bpage);
555 * Causes compile errors if the struct buffer_page gets bigger
556 * than the struct page.
558 extern int ring_buffer_page_too_big(void);
560 #ifdef CONFIG_HOTPLUG_CPU
561 static int rb_cpu_notify(struct notifier_block *self,
562 unsigned long action, void *hcpu);
566 * ring_buffer_alloc - allocate a new ring_buffer
567 * @size: the size in bytes per cpu that is needed.
568 * @flags: attributes to set for the ring buffer.
570 * Currently the only flag that is available is the RB_FL_OVERWRITE
571 * flag. This flag means that the buffer will overwrite old data
572 * when the buffer wraps. If this flag is not set, the buffer will
573 * drop data when the tail hits the head.
575 struct ring_buffer *ring_buffer_alloc(unsigned long size, unsigned flags)
577 struct ring_buffer *buffer;
581 /* Paranoid! Optimizes out when all is well */
582 if (sizeof(struct buffer_page) > sizeof(struct page))
583 ring_buffer_page_too_big();
586 /* keep it in its own cache line */
587 buffer = kzalloc(ALIGN(sizeof(*buffer), cache_line_size()),
592 if (!alloc_cpumask_var(&buffer->cpumask, GFP_KERNEL))
593 goto fail_free_buffer;
595 buffer->pages = DIV_ROUND_UP(size, BUF_PAGE_SIZE);
596 buffer->flags = flags;
597 buffer->clock = trace_clock_local;
599 /* need at least two pages */
600 if (buffer->pages == 1)
604 * In case of non-hotplug cpu, if the ring-buffer is allocated
605 * in early initcall, it will not be notified of secondary cpus.
606 * In that off case, we need to allocate for all possible cpus.
608 #ifdef CONFIG_HOTPLUG_CPU
610 cpumask_copy(buffer->cpumask, cpu_online_mask);
612 cpumask_copy(buffer->cpumask, cpu_possible_mask);
614 buffer->cpus = nr_cpu_ids;
616 bsize = sizeof(void *) * nr_cpu_ids;
617 buffer->buffers = kzalloc(ALIGN(bsize, cache_line_size()),
619 if (!buffer->buffers)
620 goto fail_free_cpumask;
622 for_each_buffer_cpu(buffer, cpu) {
623 buffer->buffers[cpu] =
624 rb_allocate_cpu_buffer(buffer, cpu);
625 if (!buffer->buffers[cpu])
626 goto fail_free_buffers;
629 #ifdef CONFIG_HOTPLUG_CPU
630 buffer->cpu_notify.notifier_call = rb_cpu_notify;
631 buffer->cpu_notify.priority = 0;
632 register_cpu_notifier(&buffer->cpu_notify);
636 mutex_init(&buffer->mutex);
641 for_each_buffer_cpu(buffer, cpu) {
642 if (buffer->buffers[cpu])
643 rb_free_cpu_buffer(buffer->buffers[cpu]);
645 kfree(buffer->buffers);
648 free_cpumask_var(buffer->cpumask);
655 EXPORT_SYMBOL_GPL(ring_buffer_alloc);
658 * ring_buffer_free - free a ring buffer.
659 * @buffer: the buffer to free.
662 ring_buffer_free(struct ring_buffer *buffer)
668 #ifdef CONFIG_HOTPLUG_CPU
669 unregister_cpu_notifier(&buffer->cpu_notify);
672 for_each_buffer_cpu(buffer, cpu)
673 rb_free_cpu_buffer(buffer->buffers[cpu]);
677 free_cpumask_var(buffer->cpumask);
681 EXPORT_SYMBOL_GPL(ring_buffer_free);
683 void ring_buffer_set_clock(struct ring_buffer *buffer,
686 buffer->clock = clock;
689 static void rb_reset_cpu(struct ring_buffer_per_cpu *cpu_buffer);
692 rb_remove_pages(struct ring_buffer_per_cpu *cpu_buffer, unsigned nr_pages)
694 struct buffer_page *bpage;
698 atomic_inc(&cpu_buffer->record_disabled);
701 for (i = 0; i < nr_pages; i++) {
702 if (RB_WARN_ON(cpu_buffer, list_empty(&cpu_buffer->pages)))
704 p = cpu_buffer->pages.next;
705 bpage = list_entry(p, struct buffer_page, list);
706 list_del_init(&bpage->list);
707 free_buffer_page(bpage);
709 if (RB_WARN_ON(cpu_buffer, list_empty(&cpu_buffer->pages)))
712 rb_reset_cpu(cpu_buffer);
714 rb_check_pages(cpu_buffer);
716 atomic_dec(&cpu_buffer->record_disabled);
721 rb_insert_pages(struct ring_buffer_per_cpu *cpu_buffer,
722 struct list_head *pages, unsigned nr_pages)
724 struct buffer_page *bpage;
728 atomic_inc(&cpu_buffer->record_disabled);
731 for (i = 0; i < nr_pages; i++) {
732 if (RB_WARN_ON(cpu_buffer, list_empty(pages)))
735 bpage = list_entry(p, struct buffer_page, list);
736 list_del_init(&bpage->list);
737 list_add_tail(&bpage->list, &cpu_buffer->pages);
739 rb_reset_cpu(cpu_buffer);
741 rb_check_pages(cpu_buffer);
743 atomic_dec(&cpu_buffer->record_disabled);
747 * ring_buffer_resize - resize the ring buffer
748 * @buffer: the buffer to resize.
749 * @size: the new size.
751 * The tracer is responsible for making sure that the buffer is
752 * not being used while changing the size.
753 * Note: We may be able to change the above requirement by using
754 * RCU synchronizations.
756 * Minimum size is 2 * BUF_PAGE_SIZE.
758 * Returns -1 on failure.
760 int ring_buffer_resize(struct ring_buffer *buffer, unsigned long size)
762 struct ring_buffer_per_cpu *cpu_buffer;
763 unsigned nr_pages, rm_pages, new_pages;
764 struct buffer_page *bpage, *tmp;
765 unsigned long buffer_size;
771 * Always succeed at resizing a non-existent buffer:
776 size = DIV_ROUND_UP(size, BUF_PAGE_SIZE);
777 size *= BUF_PAGE_SIZE;
778 buffer_size = buffer->pages * BUF_PAGE_SIZE;
780 /* we need a minimum of two pages */
781 if (size < BUF_PAGE_SIZE * 2)
782 size = BUF_PAGE_SIZE * 2;
784 if (size == buffer_size)
787 mutex_lock(&buffer->mutex);
790 nr_pages = DIV_ROUND_UP(size, BUF_PAGE_SIZE);
792 if (size < buffer_size) {
794 /* easy case, just free pages */
795 if (RB_WARN_ON(buffer, nr_pages >= buffer->pages))
798 rm_pages = buffer->pages - nr_pages;
800 for_each_buffer_cpu(buffer, cpu) {
801 cpu_buffer = buffer->buffers[cpu];
802 rb_remove_pages(cpu_buffer, rm_pages);
808 * This is a bit more difficult. We only want to add pages
809 * when we can allocate enough for all CPUs. We do this
810 * by allocating all the pages and storing them on a local
811 * link list. If we succeed in our allocation, then we
812 * add these pages to the cpu_buffers. Otherwise we just free
813 * them all and return -ENOMEM;
815 if (RB_WARN_ON(buffer, nr_pages <= buffer->pages))
818 new_pages = nr_pages - buffer->pages;
820 for_each_buffer_cpu(buffer, cpu) {
821 for (i = 0; i < new_pages; i++) {
822 bpage = kzalloc_node(ALIGN(sizeof(*bpage),
824 GFP_KERNEL, cpu_to_node(cpu));
827 list_add(&bpage->list, &pages);
828 addr = __get_free_page(GFP_KERNEL);
831 bpage->page = (void *)addr;
832 rb_init_page(bpage->page);
836 for_each_buffer_cpu(buffer, cpu) {
837 cpu_buffer = buffer->buffers[cpu];
838 rb_insert_pages(cpu_buffer, &pages, new_pages);
841 if (RB_WARN_ON(buffer, !list_empty(&pages)))
845 buffer->pages = nr_pages;
847 mutex_unlock(&buffer->mutex);
852 list_for_each_entry_safe(bpage, tmp, &pages, list) {
853 list_del_init(&bpage->list);
854 free_buffer_page(bpage);
857 mutex_unlock(&buffer->mutex);
861 * Something went totally wrong, and we are too paranoid
862 * to even clean up the mess.
866 mutex_unlock(&buffer->mutex);
869 EXPORT_SYMBOL_GPL(ring_buffer_resize);
872 __rb_data_page_index(struct buffer_data_page *bpage, unsigned index)
874 return bpage->data + index;
877 static inline void *__rb_page_index(struct buffer_page *bpage, unsigned index)
879 return bpage->page->data + index;
882 static inline struct ring_buffer_event *
883 rb_reader_event(struct ring_buffer_per_cpu *cpu_buffer)
885 return __rb_page_index(cpu_buffer->reader_page,
886 cpu_buffer->reader_page->read);
889 static inline struct ring_buffer_event *
890 rb_head_event(struct ring_buffer_per_cpu *cpu_buffer)
892 return __rb_page_index(cpu_buffer->head_page,
893 cpu_buffer->head_page->read);
896 static inline struct ring_buffer_event *
897 rb_iter_head_event(struct ring_buffer_iter *iter)
899 return __rb_page_index(iter->head_page, iter->head);
902 static inline unsigned rb_page_write(struct buffer_page *bpage)
904 return local_read(&bpage->write);
907 static inline unsigned rb_page_commit(struct buffer_page *bpage)
909 return local_read(&bpage->page->commit);
912 /* Size is determined by what has been commited */
913 static inline unsigned rb_page_size(struct buffer_page *bpage)
915 return rb_page_commit(bpage);
918 static inline unsigned
919 rb_commit_index(struct ring_buffer_per_cpu *cpu_buffer)
921 return rb_page_commit(cpu_buffer->commit_page);
924 static inline unsigned rb_head_size(struct ring_buffer_per_cpu *cpu_buffer)
926 return rb_page_commit(cpu_buffer->head_page);
930 * When the tail hits the head and the buffer is in overwrite mode,
931 * the head jumps to the next page and all content on the previous
932 * page is discarded. But before doing so, we update the overrun
933 * variable of the buffer.
935 static void rb_update_overflow(struct ring_buffer_per_cpu *cpu_buffer)
937 struct ring_buffer_event *event;
940 for (head = 0; head < rb_head_size(cpu_buffer);
941 head += rb_event_length(event)) {
943 event = __rb_page_index(cpu_buffer->head_page, head);
944 if (RB_WARN_ON(cpu_buffer, rb_null_event(event)))
946 /* Only count data entries */
947 if (event->type != RINGBUF_TYPE_DATA)
949 cpu_buffer->overrun++;
950 cpu_buffer->entries--;
954 static inline void rb_inc_page(struct ring_buffer_per_cpu *cpu_buffer,
955 struct buffer_page **bpage)
957 struct list_head *p = (*bpage)->list.next;
959 if (p == &cpu_buffer->pages)
962 *bpage = list_entry(p, struct buffer_page, list);
965 static inline unsigned
966 rb_event_index(struct ring_buffer_event *event)
968 unsigned long addr = (unsigned long)event;
970 return (addr & ~PAGE_MASK) - (PAGE_SIZE - BUF_PAGE_SIZE);
974 rb_is_commit(struct ring_buffer_per_cpu *cpu_buffer,
975 struct ring_buffer_event *event)
977 unsigned long addr = (unsigned long)event;
980 index = rb_event_index(event);
983 return cpu_buffer->commit_page->page == (void *)addr &&
984 rb_commit_index(cpu_buffer) == index;
988 rb_set_commit_event(struct ring_buffer_per_cpu *cpu_buffer,
989 struct ring_buffer_event *event)
991 unsigned long addr = (unsigned long)event;
994 index = rb_event_index(event);
997 while (cpu_buffer->commit_page->page != (void *)addr) {
998 if (RB_WARN_ON(cpu_buffer,
999 cpu_buffer->commit_page == cpu_buffer->tail_page))
1001 cpu_buffer->commit_page->page->commit =
1002 cpu_buffer->commit_page->write;
1003 rb_inc_page(cpu_buffer, &cpu_buffer->commit_page);
1004 cpu_buffer->write_stamp =
1005 cpu_buffer->commit_page->page->time_stamp;
1008 /* Now set the commit to the event's index */
1009 local_set(&cpu_buffer->commit_page->page->commit, index);
1013 rb_set_commit_to_write(struct ring_buffer_per_cpu *cpu_buffer)
1016 * We only race with interrupts and NMIs on this CPU.
1017 * If we own the commit event, then we can commit
1018 * all others that interrupted us, since the interruptions
1019 * are in stack format (they finish before they come
1020 * back to us). This allows us to do a simple loop to
1021 * assign the commit to the tail.
1024 while (cpu_buffer->commit_page != cpu_buffer->tail_page) {
1025 cpu_buffer->commit_page->page->commit =
1026 cpu_buffer->commit_page->write;
1027 rb_inc_page(cpu_buffer, &cpu_buffer->commit_page);
1028 cpu_buffer->write_stamp =
1029 cpu_buffer->commit_page->page->time_stamp;
1030 /* add barrier to keep gcc from optimizing too much */
1033 while (rb_commit_index(cpu_buffer) !=
1034 rb_page_write(cpu_buffer->commit_page)) {
1035 cpu_buffer->commit_page->page->commit =
1036 cpu_buffer->commit_page->write;
1040 /* again, keep gcc from optimizing */
1044 * If an interrupt came in just after the first while loop
1045 * and pushed the tail page forward, we will be left with
1046 * a dangling commit that will never go forward.
1048 if (unlikely(cpu_buffer->commit_page != cpu_buffer->tail_page))
1052 static void rb_reset_reader_page(struct ring_buffer_per_cpu *cpu_buffer)
1054 cpu_buffer->read_stamp = cpu_buffer->reader_page->page->time_stamp;
1055 cpu_buffer->reader_page->read = 0;
1058 static void rb_inc_iter(struct ring_buffer_iter *iter)
1060 struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
1063 * The iterator could be on the reader page (it starts there).
1064 * But the head could have moved, since the reader was
1065 * found. Check for this case and assign the iterator
1066 * to the head page instead of next.
1068 if (iter->head_page == cpu_buffer->reader_page)
1069 iter->head_page = cpu_buffer->head_page;
1071 rb_inc_page(cpu_buffer, &iter->head_page);
1073 iter->read_stamp = iter->head_page->page->time_stamp;
1078 * ring_buffer_update_event - update event type and data
1079 * @event: the even to update
1080 * @type: the type of event
1081 * @length: the size of the event field in the ring buffer
1083 * Update the type and data fields of the event. The length
1084 * is the actual size that is written to the ring buffer,
1085 * and with this, we can determine what to place into the
1089 rb_update_event(struct ring_buffer_event *event,
1090 unsigned type, unsigned length)
1096 case RINGBUF_TYPE_PADDING:
1099 case RINGBUF_TYPE_TIME_EXTEND:
1100 event->len = DIV_ROUND_UP(RB_LEN_TIME_EXTEND, RB_ALIGNMENT);
1103 case RINGBUF_TYPE_TIME_STAMP:
1104 event->len = DIV_ROUND_UP(RB_LEN_TIME_STAMP, RB_ALIGNMENT);
1107 case RINGBUF_TYPE_DATA:
1108 length -= RB_EVNT_HDR_SIZE;
1109 if (length > RB_MAX_SMALL_DATA) {
1111 event->array[0] = length;
1113 event->len = DIV_ROUND_UP(length, RB_ALIGNMENT);
1120 static unsigned rb_calculate_event_length(unsigned length)
1122 struct ring_buffer_event event; /* Used only for sizeof array */
1124 /* zero length can cause confusions */
1128 if (length > RB_MAX_SMALL_DATA)
1129 length += sizeof(event.array[0]);
1131 length += RB_EVNT_HDR_SIZE;
1132 length = ALIGN(length, RB_ALIGNMENT);
1137 static struct ring_buffer_event *
1138 __rb_reserve_next(struct ring_buffer_per_cpu *cpu_buffer,
1139 unsigned type, unsigned long length, u64 *ts)
1141 struct buffer_page *tail_page, *head_page, *reader_page, *commit_page;
1142 unsigned long tail, write;
1143 struct ring_buffer *buffer = cpu_buffer->buffer;
1144 struct ring_buffer_event *event;
1145 unsigned long flags;
1146 bool lock_taken = false;
1148 commit_page = cpu_buffer->commit_page;
1149 /* we just need to protect against interrupts */
1151 tail_page = cpu_buffer->tail_page;
1152 write = local_add_return(length, &tail_page->write);
1153 tail = write - length;
1155 /* See if we shot pass the end of this buffer page */
1156 if (write > BUF_PAGE_SIZE) {
1157 struct buffer_page *next_page = tail_page;
1159 local_irq_save(flags);
1161 * Since the write to the buffer is still not
1162 * fully lockless, we must be careful with NMIs.
1163 * The locks in the writers are taken when a write
1164 * crosses to a new page. The locks protect against
1165 * races with the readers (this will soon be fixed
1166 * with a lockless solution).
1168 * Because we can not protect against NMIs, and we
1169 * want to keep traces reentrant, we need to manage
1170 * what happens when we are in an NMI.
1172 * NMIs can happen after we take the lock.
1173 * If we are in an NMI, only take the lock
1174 * if it is not already taken. Otherwise
1177 if (unlikely(in_nmi())) {
1178 if (!__raw_spin_trylock(&cpu_buffer->lock))
1181 __raw_spin_lock(&cpu_buffer->lock);
1185 rb_inc_page(cpu_buffer, &next_page);
1187 head_page = cpu_buffer->head_page;
1188 reader_page = cpu_buffer->reader_page;
1190 /* we grabbed the lock before incrementing */
1191 if (RB_WARN_ON(cpu_buffer, next_page == reader_page))
1195 * If for some reason, we had an interrupt storm that made
1196 * it all the way around the buffer, bail, and warn
1199 if (unlikely(next_page == commit_page)) {
1204 if (next_page == head_page) {
1205 if (!(buffer->flags & RB_FL_OVERWRITE))
1208 /* tail_page has not moved yet? */
1209 if (tail_page == cpu_buffer->tail_page) {
1210 /* count overflows */
1211 rb_update_overflow(cpu_buffer);
1213 rb_inc_page(cpu_buffer, &head_page);
1214 cpu_buffer->head_page = head_page;
1215 cpu_buffer->head_page->read = 0;
1220 * If the tail page is still the same as what we think
1221 * it is, then it is up to us to update the tail
1224 if (tail_page == cpu_buffer->tail_page) {
1225 local_set(&next_page->write, 0);
1226 local_set(&next_page->page->commit, 0);
1227 cpu_buffer->tail_page = next_page;
1229 /* reread the time stamp */
1230 *ts = ring_buffer_time_stamp(buffer, cpu_buffer->cpu);
1231 cpu_buffer->tail_page->page->time_stamp = *ts;
1235 * The actual tail page has moved forward.
1237 if (tail < BUF_PAGE_SIZE) {
1238 /* Mark the rest of the page with padding */
1239 event = __rb_page_index(tail_page, tail);
1240 rb_event_set_padding(event);
1243 if (tail <= BUF_PAGE_SIZE)
1244 /* Set the write back to the previous setting */
1245 local_set(&tail_page->write, tail);
1248 * If this was a commit entry that failed,
1249 * increment that too
1251 if (tail_page == cpu_buffer->commit_page &&
1252 tail == rb_commit_index(cpu_buffer)) {
1253 rb_set_commit_to_write(cpu_buffer);
1256 __raw_spin_unlock(&cpu_buffer->lock);
1257 local_irq_restore(flags);
1259 /* fail and let the caller try again */
1260 return ERR_PTR(-EAGAIN);
1263 /* We reserved something on the buffer */
1265 if (RB_WARN_ON(cpu_buffer, write > BUF_PAGE_SIZE))
1268 event = __rb_page_index(tail_page, tail);
1269 rb_update_event(event, type, length);
1272 * If this is a commit and the tail is zero, then update
1273 * this page's time stamp.
1275 if (!tail && rb_is_commit(cpu_buffer, event))
1276 cpu_buffer->commit_page->page->time_stamp = *ts;
1282 if (tail <= BUF_PAGE_SIZE)
1283 local_set(&tail_page->write, tail);
1285 if (likely(lock_taken))
1286 __raw_spin_unlock(&cpu_buffer->lock);
1287 local_irq_restore(flags);
1292 rb_add_time_stamp(struct ring_buffer_per_cpu *cpu_buffer,
1293 u64 *ts, u64 *delta)
1295 struct ring_buffer_event *event;
1299 if (unlikely(*delta > (1ULL << 59) && !once++)) {
1300 printk(KERN_WARNING "Delta way too big! %llu"
1301 " ts=%llu write stamp = %llu\n",
1302 (unsigned long long)*delta,
1303 (unsigned long long)*ts,
1304 (unsigned long long)cpu_buffer->write_stamp);
1309 * The delta is too big, we to add a
1312 event = __rb_reserve_next(cpu_buffer,
1313 RINGBUF_TYPE_TIME_EXTEND,
1319 if (PTR_ERR(event) == -EAGAIN)
1322 /* Only a commited time event can update the write stamp */
1323 if (rb_is_commit(cpu_buffer, event)) {
1325 * If this is the first on the page, then we need to
1326 * update the page itself, and just put in a zero.
1328 if (rb_event_index(event)) {
1329 event->time_delta = *delta & TS_MASK;
1330 event->array[0] = *delta >> TS_SHIFT;
1332 cpu_buffer->commit_page->page->time_stamp = *ts;
1333 event->time_delta = 0;
1334 event->array[0] = 0;
1336 cpu_buffer->write_stamp = *ts;
1337 /* let the caller know this was the commit */
1340 /* Darn, this is just wasted space */
1341 event->time_delta = 0;
1342 event->array[0] = 0;
1351 static struct ring_buffer_event *
1352 rb_reserve_next_event(struct ring_buffer_per_cpu *cpu_buffer,
1353 unsigned type, unsigned long length)
1355 struct ring_buffer_event *event;
1362 * We allow for interrupts to reenter here and do a trace.
1363 * If one does, it will cause this original code to loop
1364 * back here. Even with heavy interrupts happening, this
1365 * should only happen a few times in a row. If this happens
1366 * 1000 times in a row, there must be either an interrupt
1367 * storm or we have something buggy.
1370 if (RB_WARN_ON(cpu_buffer, ++nr_loops > 1000))
1373 ts = ring_buffer_time_stamp(cpu_buffer->buffer, cpu_buffer->cpu);
1376 * Only the first commit can update the timestamp.
1377 * Yes there is a race here. If an interrupt comes in
1378 * just after the conditional and it traces too, then it
1379 * will also check the deltas. More than one timestamp may
1380 * also be made. But only the entry that did the actual
1381 * commit will be something other than zero.
1383 if (cpu_buffer->tail_page == cpu_buffer->commit_page &&
1384 rb_page_write(cpu_buffer->tail_page) ==
1385 rb_commit_index(cpu_buffer)) {
1387 delta = ts - cpu_buffer->write_stamp;
1389 /* make sure this delta is calculated here */
1392 /* Did the write stamp get updated already? */
1393 if (unlikely(ts < cpu_buffer->write_stamp))
1396 if (test_time_stamp(delta)) {
1398 commit = rb_add_time_stamp(cpu_buffer, &ts, &delta);
1400 if (commit == -EBUSY)
1403 if (commit == -EAGAIN)
1406 RB_WARN_ON(cpu_buffer, commit < 0);
1409 /* Non commits have zero deltas */
1412 event = __rb_reserve_next(cpu_buffer, type, length, &ts);
1413 if (PTR_ERR(event) == -EAGAIN)
1417 if (unlikely(commit))
1419 * Ouch! We needed a timestamp and it was commited. But
1420 * we didn't get our event reserved.
1422 rb_set_commit_to_write(cpu_buffer);
1427 * If the timestamp was commited, make the commit our entry
1428 * now so that we will update it when needed.
1431 rb_set_commit_event(cpu_buffer, event);
1432 else if (!rb_is_commit(cpu_buffer, event))
1435 event->time_delta = delta;
1440 static DEFINE_PER_CPU(int, rb_need_resched);
1443 * ring_buffer_lock_reserve - reserve a part of the buffer
1444 * @buffer: the ring buffer to reserve from
1445 * @length: the length of the data to reserve (excluding event header)
1447 * Returns a reseverd event on the ring buffer to copy directly to.
1448 * The user of this interface will need to get the body to write into
1449 * and can use the ring_buffer_event_data() interface.
1451 * The length is the length of the data needed, not the event length
1452 * which also includes the event header.
1454 * Must be paired with ring_buffer_unlock_commit, unless NULL is returned.
1455 * If NULL is returned, then nothing has been allocated or locked.
1457 struct ring_buffer_event *
1458 ring_buffer_lock_reserve(struct ring_buffer *buffer, unsigned long length)
1460 struct ring_buffer_per_cpu *cpu_buffer;
1461 struct ring_buffer_event *event;
1464 if (ring_buffer_flags != RB_BUFFERS_ON)
1467 if (atomic_read(&buffer->record_disabled))
1470 /* If we are tracing schedule, we don't want to recurse */
1471 resched = ftrace_preempt_disable();
1473 cpu = raw_smp_processor_id();
1475 if (!cpumask_test_cpu(cpu, buffer->cpumask))
1478 cpu_buffer = buffer->buffers[cpu];
1480 if (atomic_read(&cpu_buffer->record_disabled))
1483 length = rb_calculate_event_length(length);
1484 if (length > BUF_PAGE_SIZE)
1487 event = rb_reserve_next_event(cpu_buffer, RINGBUF_TYPE_DATA, length);
1492 * Need to store resched state on this cpu.
1493 * Only the first needs to.
1496 if (preempt_count() == 1)
1497 per_cpu(rb_need_resched, cpu) = resched;
1502 ftrace_preempt_enable(resched);
1505 EXPORT_SYMBOL_GPL(ring_buffer_lock_reserve);
1507 static void rb_commit(struct ring_buffer_per_cpu *cpu_buffer,
1508 struct ring_buffer_event *event)
1510 cpu_buffer->entries++;
1512 /* Only process further if we own the commit */
1513 if (!rb_is_commit(cpu_buffer, event))
1516 cpu_buffer->write_stamp += event->time_delta;
1518 rb_set_commit_to_write(cpu_buffer);
1522 * ring_buffer_unlock_commit - commit a reserved
1523 * @buffer: The buffer to commit to
1524 * @event: The event pointer to commit.
1526 * This commits the data to the ring buffer, and releases any locks held.
1528 * Must be paired with ring_buffer_lock_reserve.
1530 int ring_buffer_unlock_commit(struct ring_buffer *buffer,
1531 struct ring_buffer_event *event)
1533 struct ring_buffer_per_cpu *cpu_buffer;
1534 int cpu = raw_smp_processor_id();
1536 cpu_buffer = buffer->buffers[cpu];
1538 rb_commit(cpu_buffer, event);
1541 * Only the last preempt count needs to restore preemption.
1543 if (preempt_count() == 1)
1544 ftrace_preempt_enable(per_cpu(rb_need_resched, cpu));
1546 preempt_enable_no_resched_notrace();
1550 EXPORT_SYMBOL_GPL(ring_buffer_unlock_commit);
1553 * ring_buffer_event_discard - discard any event in the ring buffer
1554 * @event: the event to discard
1556 * Sometimes a event that is in the ring buffer needs to be ignored.
1557 * This function lets the user discard an event in the ring buffer
1558 * and then that event will not be read later.
1560 * Note, it is up to the user to be careful with this, and protect
1561 * against races. If the user discards an event that has been consumed
1562 * it is possible that it could corrupt the ring buffer.
1564 void ring_buffer_event_discard(struct ring_buffer_event *event)
1566 event->type = RINGBUF_TYPE_PADDING;
1567 /* time delta must be non zero */
1568 if (!event->time_delta)
1569 event->time_delta = 1;
1571 EXPORT_SYMBOL_GPL(ring_buffer_event_discard);
1574 * ring_buffer_commit_discard - discard an event that has not been committed
1575 * @buffer: the ring buffer
1576 * @event: non committed event to discard
1578 * This is similar to ring_buffer_event_discard but must only be
1579 * performed on an event that has not been committed yet. The difference
1580 * is that this will also try to free the event from the ring buffer
1581 * if another event has not been added behind it.
1583 * If another event has been added behind it, it will set the event
1584 * up as discarded, and perform the commit.
1586 * If this function is called, do not call ring_buffer_unlock_commit on
1589 void ring_buffer_discard_commit(struct ring_buffer *buffer,
1590 struct ring_buffer_event *event)
1592 struct ring_buffer_per_cpu *cpu_buffer;
1593 unsigned long new_index, old_index;
1594 struct buffer_page *bpage;
1595 unsigned long index;
1599 /* The event is discarded regardless */
1600 ring_buffer_event_discard(event);
1603 * This must only be called if the event has not been
1604 * committed yet. Thus we can assume that preemption
1605 * is still disabled.
1607 RB_WARN_ON(buffer, !preempt_count());
1609 cpu = smp_processor_id();
1610 cpu_buffer = buffer->buffers[cpu];
1612 new_index = rb_event_index(event);
1613 old_index = new_index + rb_event_length(event);
1614 addr = (unsigned long)event;
1617 bpage = cpu_buffer->tail_page;
1619 if (bpage == (void *)addr && rb_page_write(bpage) == old_index) {
1621 * This is on the tail page. It is possible that
1622 * a write could come in and move the tail page
1623 * and write to the next page. That is fine
1624 * because we just shorten what is on this page.
1626 index = local_cmpxchg(&bpage->write, old_index, new_index);
1627 if (index == old_index)
1632 * The commit is still visible by the reader, so we
1633 * must increment entries.
1635 cpu_buffer->entries++;
1638 * If a write came in and pushed the tail page
1639 * we still need to update the commit pointer
1640 * if we were the commit.
1642 if (rb_is_commit(cpu_buffer, event))
1643 rb_set_commit_to_write(cpu_buffer);
1646 * Only the last preempt count needs to restore preemption.
1648 if (preempt_count() == 1)
1649 ftrace_preempt_enable(per_cpu(rb_need_resched, cpu));
1651 preempt_enable_no_resched_notrace();
1654 EXPORT_SYMBOL_GPL(ring_buffer_discard_commit);
1657 * ring_buffer_write - write data to the buffer without reserving
1658 * @buffer: The ring buffer to write to.
1659 * @length: The length of the data being written (excluding the event header)
1660 * @data: The data to write to the buffer.
1662 * This is like ring_buffer_lock_reserve and ring_buffer_unlock_commit as
1663 * one function. If you already have the data to write to the buffer, it
1664 * may be easier to simply call this function.
1666 * Note, like ring_buffer_lock_reserve, the length is the length of the data
1667 * and not the length of the event which would hold the header.
1669 int ring_buffer_write(struct ring_buffer *buffer,
1670 unsigned long length,
1673 struct ring_buffer_per_cpu *cpu_buffer;
1674 struct ring_buffer_event *event;
1675 unsigned long event_length;
1680 if (ring_buffer_flags != RB_BUFFERS_ON)
1683 if (atomic_read(&buffer->record_disabled))
1686 resched = ftrace_preempt_disable();
1688 cpu = raw_smp_processor_id();
1690 if (!cpumask_test_cpu(cpu, buffer->cpumask))
1693 cpu_buffer = buffer->buffers[cpu];
1695 if (atomic_read(&cpu_buffer->record_disabled))
1698 event_length = rb_calculate_event_length(length);
1699 event = rb_reserve_next_event(cpu_buffer,
1700 RINGBUF_TYPE_DATA, event_length);
1704 body = rb_event_data(event);
1706 memcpy(body, data, length);
1708 rb_commit(cpu_buffer, event);
1712 ftrace_preempt_enable(resched);
1716 EXPORT_SYMBOL_GPL(ring_buffer_write);
1718 static int rb_per_cpu_empty(struct ring_buffer_per_cpu *cpu_buffer)
1720 struct buffer_page *reader = cpu_buffer->reader_page;
1721 struct buffer_page *head = cpu_buffer->head_page;
1722 struct buffer_page *commit = cpu_buffer->commit_page;
1724 return reader->read == rb_page_commit(reader) &&
1725 (commit == reader ||
1727 head->read == rb_page_commit(commit)));
1731 * ring_buffer_record_disable - stop all writes into the buffer
1732 * @buffer: The ring buffer to stop writes to.
1734 * This prevents all writes to the buffer. Any attempt to write
1735 * to the buffer after this will fail and return NULL.
1737 * The caller should call synchronize_sched() after this.
1739 void ring_buffer_record_disable(struct ring_buffer *buffer)
1741 atomic_inc(&buffer->record_disabled);
1743 EXPORT_SYMBOL_GPL(ring_buffer_record_disable);
1746 * ring_buffer_record_enable - enable writes to the buffer
1747 * @buffer: The ring buffer to enable writes
1749 * Note, multiple disables will need the same number of enables
1750 * to truely enable the writing (much like preempt_disable).
1752 void ring_buffer_record_enable(struct ring_buffer *buffer)
1754 atomic_dec(&buffer->record_disabled);
1756 EXPORT_SYMBOL_GPL(ring_buffer_record_enable);
1759 * ring_buffer_record_disable_cpu - stop all writes into the cpu_buffer
1760 * @buffer: The ring buffer to stop writes to.
1761 * @cpu: The CPU buffer to stop
1763 * This prevents all writes to the buffer. Any attempt to write
1764 * to the buffer after this will fail and return NULL.
1766 * The caller should call synchronize_sched() after this.
1768 void ring_buffer_record_disable_cpu(struct ring_buffer *buffer, int cpu)
1770 struct ring_buffer_per_cpu *cpu_buffer;
1772 if (!cpumask_test_cpu(cpu, buffer->cpumask))
1775 cpu_buffer = buffer->buffers[cpu];
1776 atomic_inc(&cpu_buffer->record_disabled);
1778 EXPORT_SYMBOL_GPL(ring_buffer_record_disable_cpu);
1781 * ring_buffer_record_enable_cpu - enable writes to the buffer
1782 * @buffer: The ring buffer to enable writes
1783 * @cpu: The CPU to enable.
1785 * Note, multiple disables will need the same number of enables
1786 * to truely enable the writing (much like preempt_disable).
1788 void ring_buffer_record_enable_cpu(struct ring_buffer *buffer, int cpu)
1790 struct ring_buffer_per_cpu *cpu_buffer;
1792 if (!cpumask_test_cpu(cpu, buffer->cpumask))
1795 cpu_buffer = buffer->buffers[cpu];
1796 atomic_dec(&cpu_buffer->record_disabled);
1798 EXPORT_SYMBOL_GPL(ring_buffer_record_enable_cpu);
1801 * ring_buffer_entries_cpu - get the number of entries in a cpu buffer
1802 * @buffer: The ring buffer
1803 * @cpu: The per CPU buffer to get the entries from.
1805 unsigned long ring_buffer_entries_cpu(struct ring_buffer *buffer, int cpu)
1807 struct ring_buffer_per_cpu *cpu_buffer;
1810 if (!cpumask_test_cpu(cpu, buffer->cpumask))
1813 cpu_buffer = buffer->buffers[cpu];
1814 ret = cpu_buffer->entries;
1818 EXPORT_SYMBOL_GPL(ring_buffer_entries_cpu);
1821 * ring_buffer_overrun_cpu - get the number of overruns in a cpu_buffer
1822 * @buffer: The ring buffer
1823 * @cpu: The per CPU buffer to get the number of overruns from
1825 unsigned long ring_buffer_overrun_cpu(struct ring_buffer *buffer, int cpu)
1827 struct ring_buffer_per_cpu *cpu_buffer;
1830 if (!cpumask_test_cpu(cpu, buffer->cpumask))
1833 cpu_buffer = buffer->buffers[cpu];
1834 ret = cpu_buffer->overrun;
1838 EXPORT_SYMBOL_GPL(ring_buffer_overrun_cpu);
1841 * ring_buffer_entries - get the number of entries in a buffer
1842 * @buffer: The ring buffer
1844 * Returns the total number of entries in the ring buffer
1847 unsigned long ring_buffer_entries(struct ring_buffer *buffer)
1849 struct ring_buffer_per_cpu *cpu_buffer;
1850 unsigned long entries = 0;
1853 /* if you care about this being correct, lock the buffer */
1854 for_each_buffer_cpu(buffer, cpu) {
1855 cpu_buffer = buffer->buffers[cpu];
1856 entries += cpu_buffer->entries;
1861 EXPORT_SYMBOL_GPL(ring_buffer_entries);
1864 * ring_buffer_overrun_cpu - get the number of overruns in buffer
1865 * @buffer: The ring buffer
1867 * Returns the total number of overruns in the ring buffer
1870 unsigned long ring_buffer_overruns(struct ring_buffer *buffer)
1872 struct ring_buffer_per_cpu *cpu_buffer;
1873 unsigned long overruns = 0;
1876 /* if you care about this being correct, lock the buffer */
1877 for_each_buffer_cpu(buffer, cpu) {
1878 cpu_buffer = buffer->buffers[cpu];
1879 overruns += cpu_buffer->overrun;
1884 EXPORT_SYMBOL_GPL(ring_buffer_overruns);
1886 static void rb_iter_reset(struct ring_buffer_iter *iter)
1888 struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
1890 /* Iterator usage is expected to have record disabled */
1891 if (list_empty(&cpu_buffer->reader_page->list)) {
1892 iter->head_page = cpu_buffer->head_page;
1893 iter->head = cpu_buffer->head_page->read;
1895 iter->head_page = cpu_buffer->reader_page;
1896 iter->head = cpu_buffer->reader_page->read;
1899 iter->read_stamp = cpu_buffer->read_stamp;
1901 iter->read_stamp = iter->head_page->page->time_stamp;
1905 * ring_buffer_iter_reset - reset an iterator
1906 * @iter: The iterator to reset
1908 * Resets the iterator, so that it will start from the beginning
1911 void ring_buffer_iter_reset(struct ring_buffer_iter *iter)
1913 struct ring_buffer_per_cpu *cpu_buffer;
1914 unsigned long flags;
1919 cpu_buffer = iter->cpu_buffer;
1921 spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
1922 rb_iter_reset(iter);
1923 spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
1925 EXPORT_SYMBOL_GPL(ring_buffer_iter_reset);
1928 * ring_buffer_iter_empty - check if an iterator has no more to read
1929 * @iter: The iterator to check
1931 int ring_buffer_iter_empty(struct ring_buffer_iter *iter)
1933 struct ring_buffer_per_cpu *cpu_buffer;
1935 cpu_buffer = iter->cpu_buffer;
1937 return iter->head_page == cpu_buffer->commit_page &&
1938 iter->head == rb_commit_index(cpu_buffer);
1940 EXPORT_SYMBOL_GPL(ring_buffer_iter_empty);
1943 rb_update_read_stamp(struct ring_buffer_per_cpu *cpu_buffer,
1944 struct ring_buffer_event *event)
1948 switch (event->type) {
1949 case RINGBUF_TYPE_PADDING:
1952 case RINGBUF_TYPE_TIME_EXTEND:
1953 delta = event->array[0];
1955 delta += event->time_delta;
1956 cpu_buffer->read_stamp += delta;
1959 case RINGBUF_TYPE_TIME_STAMP:
1960 /* FIXME: not implemented */
1963 case RINGBUF_TYPE_DATA:
1964 cpu_buffer->read_stamp += event->time_delta;
1974 rb_update_iter_read_stamp(struct ring_buffer_iter *iter,
1975 struct ring_buffer_event *event)
1979 switch (event->type) {
1980 case RINGBUF_TYPE_PADDING:
1983 case RINGBUF_TYPE_TIME_EXTEND:
1984 delta = event->array[0];
1986 delta += event->time_delta;
1987 iter->read_stamp += delta;
1990 case RINGBUF_TYPE_TIME_STAMP:
1991 /* FIXME: not implemented */
1994 case RINGBUF_TYPE_DATA:
1995 iter->read_stamp += event->time_delta;
2004 static struct buffer_page *
2005 rb_get_reader_page(struct ring_buffer_per_cpu *cpu_buffer)
2007 struct buffer_page *reader = NULL;
2008 unsigned long flags;
2011 local_irq_save(flags);
2012 __raw_spin_lock(&cpu_buffer->lock);
2016 * This should normally only loop twice. But because the
2017 * start of the reader inserts an empty page, it causes
2018 * a case where we will loop three times. There should be no
2019 * reason to loop four times (that I know of).
2021 if (RB_WARN_ON(cpu_buffer, ++nr_loops > 3)) {
2026 reader = cpu_buffer->reader_page;
2028 /* If there's more to read, return this page */
2029 if (cpu_buffer->reader_page->read < rb_page_size(reader))
2032 /* Never should we have an index greater than the size */
2033 if (RB_WARN_ON(cpu_buffer,
2034 cpu_buffer->reader_page->read > rb_page_size(reader)))
2037 /* check if we caught up to the tail */
2039 if (cpu_buffer->commit_page == cpu_buffer->reader_page)
2043 * Splice the empty reader page into the list around the head.
2044 * Reset the reader page to size zero.
2047 reader = cpu_buffer->head_page;
2048 cpu_buffer->reader_page->list.next = reader->list.next;
2049 cpu_buffer->reader_page->list.prev = reader->list.prev;
2051 local_set(&cpu_buffer->reader_page->write, 0);
2052 local_set(&cpu_buffer->reader_page->page->commit, 0);
2054 /* Make the reader page now replace the head */
2055 reader->list.prev->next = &cpu_buffer->reader_page->list;
2056 reader->list.next->prev = &cpu_buffer->reader_page->list;
2059 * If the tail is on the reader, then we must set the head
2060 * to the inserted page, otherwise we set it one before.
2062 cpu_buffer->head_page = cpu_buffer->reader_page;
2064 if (cpu_buffer->commit_page != reader)
2065 rb_inc_page(cpu_buffer, &cpu_buffer->head_page);
2067 /* Finally update the reader page to the new head */
2068 cpu_buffer->reader_page = reader;
2069 rb_reset_reader_page(cpu_buffer);
2074 __raw_spin_unlock(&cpu_buffer->lock);
2075 local_irq_restore(flags);
2080 static void rb_advance_reader(struct ring_buffer_per_cpu *cpu_buffer)
2082 struct ring_buffer_event *event;
2083 struct buffer_page *reader;
2086 reader = rb_get_reader_page(cpu_buffer);
2088 /* This function should not be called when buffer is empty */
2089 if (RB_WARN_ON(cpu_buffer, !reader))
2092 event = rb_reader_event(cpu_buffer);
2094 if (event->type == RINGBUF_TYPE_DATA || rb_discarded_event(event))
2095 cpu_buffer->entries--;
2097 rb_update_read_stamp(cpu_buffer, event);
2099 length = rb_event_length(event);
2100 cpu_buffer->reader_page->read += length;
2103 static void rb_advance_iter(struct ring_buffer_iter *iter)
2105 struct ring_buffer *buffer;
2106 struct ring_buffer_per_cpu *cpu_buffer;
2107 struct ring_buffer_event *event;
2110 cpu_buffer = iter->cpu_buffer;
2111 buffer = cpu_buffer->buffer;
2114 * Check if we are at the end of the buffer.
2116 if (iter->head >= rb_page_size(iter->head_page)) {
2117 if (RB_WARN_ON(buffer,
2118 iter->head_page == cpu_buffer->commit_page))
2124 event = rb_iter_head_event(iter);
2126 length = rb_event_length(event);
2129 * This should not be called to advance the header if we are
2130 * at the tail of the buffer.
2132 if (RB_WARN_ON(cpu_buffer,
2133 (iter->head_page == cpu_buffer->commit_page) &&
2134 (iter->head + length > rb_commit_index(cpu_buffer))))
2137 rb_update_iter_read_stamp(iter, event);
2139 iter->head += length;
2141 /* check for end of page padding */
2142 if ((iter->head >= rb_page_size(iter->head_page)) &&
2143 (iter->head_page != cpu_buffer->commit_page))
2144 rb_advance_iter(iter);
2147 static struct ring_buffer_event *
2148 rb_buffer_peek(struct ring_buffer *buffer, int cpu, u64 *ts)
2150 struct ring_buffer_per_cpu *cpu_buffer;
2151 struct ring_buffer_event *event;
2152 struct buffer_page *reader;
2155 cpu_buffer = buffer->buffers[cpu];
2159 * We repeat when a timestamp is encountered. It is possible
2160 * to get multiple timestamps from an interrupt entering just
2161 * as one timestamp is about to be written. The max times
2162 * that this can happen is the number of nested interrupts we
2163 * can have. Nesting 10 deep of interrupts is clearly
2166 if (RB_WARN_ON(cpu_buffer, ++nr_loops > 10))
2169 reader = rb_get_reader_page(cpu_buffer);
2173 event = rb_reader_event(cpu_buffer);
2175 switch (event->type) {
2176 case RINGBUF_TYPE_PADDING:
2177 if (rb_null_event(event))
2178 RB_WARN_ON(cpu_buffer, 1);
2180 * Because the writer could be discarding every
2181 * event it creates (which would probably be bad)
2182 * if we were to go back to "again" then we may never
2183 * catch up, and will trigger the warn on, or lock
2184 * the box. Return the padding, and we will release
2185 * the current locks, and try again.
2187 rb_advance_reader(cpu_buffer);
2190 case RINGBUF_TYPE_TIME_EXTEND:
2191 /* Internal data, OK to advance */
2192 rb_advance_reader(cpu_buffer);
2195 case RINGBUF_TYPE_TIME_STAMP:
2196 /* FIXME: not implemented */
2197 rb_advance_reader(cpu_buffer);
2200 case RINGBUF_TYPE_DATA:
2202 *ts = cpu_buffer->read_stamp + event->time_delta;
2203 ring_buffer_normalize_time_stamp(buffer,
2204 cpu_buffer->cpu, ts);
2214 EXPORT_SYMBOL_GPL(ring_buffer_peek);
2216 static struct ring_buffer_event *
2217 rb_iter_peek(struct ring_buffer_iter *iter, u64 *ts)
2219 struct ring_buffer *buffer;
2220 struct ring_buffer_per_cpu *cpu_buffer;
2221 struct ring_buffer_event *event;
2224 if (ring_buffer_iter_empty(iter))
2227 cpu_buffer = iter->cpu_buffer;
2228 buffer = cpu_buffer->buffer;
2232 * We repeat when a timestamp is encountered. It is possible
2233 * to get multiple timestamps from an interrupt entering just
2234 * as one timestamp is about to be written. The max times
2235 * that this can happen is the number of nested interrupts we
2236 * can have. Nesting 10 deep of interrupts is clearly
2239 if (RB_WARN_ON(cpu_buffer, ++nr_loops > 10))
2242 if (rb_per_cpu_empty(cpu_buffer))
2245 event = rb_iter_head_event(iter);
2247 switch (event->type) {
2248 case RINGBUF_TYPE_PADDING:
2249 if (rb_null_event(event)) {
2253 rb_advance_iter(iter);
2256 case RINGBUF_TYPE_TIME_EXTEND:
2257 /* Internal data, OK to advance */
2258 rb_advance_iter(iter);
2261 case RINGBUF_TYPE_TIME_STAMP:
2262 /* FIXME: not implemented */
2263 rb_advance_iter(iter);
2266 case RINGBUF_TYPE_DATA:
2268 *ts = iter->read_stamp + event->time_delta;
2269 ring_buffer_normalize_time_stamp(buffer,
2270 cpu_buffer->cpu, ts);
2280 EXPORT_SYMBOL_GPL(ring_buffer_iter_peek);
2283 * ring_buffer_peek - peek at the next event to be read
2284 * @buffer: The ring buffer to read
2285 * @cpu: The cpu to peak at
2286 * @ts: The timestamp counter of this event.
2288 * This will return the event that will be read next, but does
2289 * not consume the data.
2291 struct ring_buffer_event *
2292 ring_buffer_peek(struct ring_buffer *buffer, int cpu, u64 *ts)
2294 struct ring_buffer_per_cpu *cpu_buffer = buffer->buffers[cpu];
2295 struct ring_buffer_event *event;
2296 unsigned long flags;
2298 if (!cpumask_test_cpu(cpu, buffer->cpumask))
2302 spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
2303 event = rb_buffer_peek(buffer, cpu, ts);
2304 spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
2306 if (event && event->type == RINGBUF_TYPE_PADDING) {
2315 * ring_buffer_iter_peek - peek at the next event to be read
2316 * @iter: The ring buffer iterator
2317 * @ts: The timestamp counter of this event.
2319 * This will return the event that will be read next, but does
2320 * not increment the iterator.
2322 struct ring_buffer_event *
2323 ring_buffer_iter_peek(struct ring_buffer_iter *iter, u64 *ts)
2325 struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
2326 struct ring_buffer_event *event;
2327 unsigned long flags;
2330 spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
2331 event = rb_iter_peek(iter, ts);
2332 spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
2334 if (event && event->type == RINGBUF_TYPE_PADDING) {
2343 * ring_buffer_consume - return an event and consume it
2344 * @buffer: The ring buffer to get the next event from
2346 * Returns the next event in the ring buffer, and that event is consumed.
2347 * Meaning, that sequential reads will keep returning a different event,
2348 * and eventually empty the ring buffer if the producer is slower.
2350 struct ring_buffer_event *
2351 ring_buffer_consume(struct ring_buffer *buffer, int cpu, u64 *ts)
2353 struct ring_buffer_per_cpu *cpu_buffer;
2354 struct ring_buffer_event *event = NULL;
2355 unsigned long flags;
2358 /* might be called in atomic */
2361 if (!cpumask_test_cpu(cpu, buffer->cpumask))
2364 cpu_buffer = buffer->buffers[cpu];
2365 spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
2367 event = rb_buffer_peek(buffer, cpu, ts);
2371 rb_advance_reader(cpu_buffer);
2374 spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
2379 if (event && event->type == RINGBUF_TYPE_PADDING) {
2386 EXPORT_SYMBOL_GPL(ring_buffer_consume);
2389 * ring_buffer_read_start - start a non consuming read of the buffer
2390 * @buffer: The ring buffer to read from
2391 * @cpu: The cpu buffer to iterate over
2393 * This starts up an iteration through the buffer. It also disables
2394 * the recording to the buffer until the reading is finished.
2395 * This prevents the reading from being corrupted. This is not
2396 * a consuming read, so a producer is not expected.
2398 * Must be paired with ring_buffer_finish.
2400 struct ring_buffer_iter *
2401 ring_buffer_read_start(struct ring_buffer *buffer, int cpu)
2403 struct ring_buffer_per_cpu *cpu_buffer;
2404 struct ring_buffer_iter *iter;
2405 unsigned long flags;
2407 if (!cpumask_test_cpu(cpu, buffer->cpumask))
2410 iter = kmalloc(sizeof(*iter), GFP_KERNEL);
2414 cpu_buffer = buffer->buffers[cpu];
2416 iter->cpu_buffer = cpu_buffer;
2418 atomic_inc(&cpu_buffer->record_disabled);
2419 synchronize_sched();
2421 spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
2422 __raw_spin_lock(&cpu_buffer->lock);
2423 rb_iter_reset(iter);
2424 __raw_spin_unlock(&cpu_buffer->lock);
2425 spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
2429 EXPORT_SYMBOL_GPL(ring_buffer_read_start);
2432 * ring_buffer_finish - finish reading the iterator of the buffer
2433 * @iter: The iterator retrieved by ring_buffer_start
2435 * This re-enables the recording to the buffer, and frees the
2439 ring_buffer_read_finish(struct ring_buffer_iter *iter)
2441 struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
2443 atomic_dec(&cpu_buffer->record_disabled);
2446 EXPORT_SYMBOL_GPL(ring_buffer_read_finish);
2449 * ring_buffer_read - read the next item in the ring buffer by the iterator
2450 * @iter: The ring buffer iterator
2451 * @ts: The time stamp of the event read.
2453 * This reads the next event in the ring buffer and increments the iterator.
2455 struct ring_buffer_event *
2456 ring_buffer_read(struct ring_buffer_iter *iter, u64 *ts)
2458 struct ring_buffer_event *event;
2459 struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
2460 unsigned long flags;
2463 spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
2464 event = rb_iter_peek(iter, ts);
2468 rb_advance_iter(iter);
2470 spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
2472 if (event && event->type == RINGBUF_TYPE_PADDING) {
2479 EXPORT_SYMBOL_GPL(ring_buffer_read);
2482 * ring_buffer_size - return the size of the ring buffer (in bytes)
2483 * @buffer: The ring buffer.
2485 unsigned long ring_buffer_size(struct ring_buffer *buffer)
2487 return BUF_PAGE_SIZE * buffer->pages;
2489 EXPORT_SYMBOL_GPL(ring_buffer_size);
2492 rb_reset_cpu(struct ring_buffer_per_cpu *cpu_buffer)
2494 cpu_buffer->head_page
2495 = list_entry(cpu_buffer->pages.next, struct buffer_page, list);
2496 local_set(&cpu_buffer->head_page->write, 0);
2497 local_set(&cpu_buffer->head_page->page->commit, 0);
2499 cpu_buffer->head_page->read = 0;
2501 cpu_buffer->tail_page = cpu_buffer->head_page;
2502 cpu_buffer->commit_page = cpu_buffer->head_page;
2504 INIT_LIST_HEAD(&cpu_buffer->reader_page->list);
2505 local_set(&cpu_buffer->reader_page->write, 0);
2506 local_set(&cpu_buffer->reader_page->page->commit, 0);
2507 cpu_buffer->reader_page->read = 0;
2509 cpu_buffer->overrun = 0;
2510 cpu_buffer->entries = 0;
2512 cpu_buffer->write_stamp = 0;
2513 cpu_buffer->read_stamp = 0;
2517 * ring_buffer_reset_cpu - reset a ring buffer per CPU buffer
2518 * @buffer: The ring buffer to reset a per cpu buffer of
2519 * @cpu: The CPU buffer to be reset
2521 void ring_buffer_reset_cpu(struct ring_buffer *buffer, int cpu)
2523 struct ring_buffer_per_cpu *cpu_buffer = buffer->buffers[cpu];
2524 unsigned long flags;
2526 if (!cpumask_test_cpu(cpu, buffer->cpumask))
2529 spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
2531 __raw_spin_lock(&cpu_buffer->lock);
2533 rb_reset_cpu(cpu_buffer);
2535 __raw_spin_unlock(&cpu_buffer->lock);
2537 spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
2539 EXPORT_SYMBOL_GPL(ring_buffer_reset_cpu);
2542 * ring_buffer_reset - reset a ring buffer
2543 * @buffer: The ring buffer to reset all cpu buffers
2545 void ring_buffer_reset(struct ring_buffer *buffer)
2549 for_each_buffer_cpu(buffer, cpu)
2550 ring_buffer_reset_cpu(buffer, cpu);
2552 EXPORT_SYMBOL_GPL(ring_buffer_reset);
2555 * rind_buffer_empty - is the ring buffer empty?
2556 * @buffer: The ring buffer to test
2558 int ring_buffer_empty(struct ring_buffer *buffer)
2560 struct ring_buffer_per_cpu *cpu_buffer;
2563 /* yes this is racy, but if you don't like the race, lock the buffer */
2564 for_each_buffer_cpu(buffer, cpu) {
2565 cpu_buffer = buffer->buffers[cpu];
2566 if (!rb_per_cpu_empty(cpu_buffer))
2572 EXPORT_SYMBOL_GPL(ring_buffer_empty);
2575 * ring_buffer_empty_cpu - is a cpu buffer of a ring buffer empty?
2576 * @buffer: The ring buffer
2577 * @cpu: The CPU buffer to test
2579 int ring_buffer_empty_cpu(struct ring_buffer *buffer, int cpu)
2581 struct ring_buffer_per_cpu *cpu_buffer;
2584 if (!cpumask_test_cpu(cpu, buffer->cpumask))
2587 cpu_buffer = buffer->buffers[cpu];
2588 ret = rb_per_cpu_empty(cpu_buffer);
2593 EXPORT_SYMBOL_GPL(ring_buffer_empty_cpu);
2596 * ring_buffer_swap_cpu - swap a CPU buffer between two ring buffers
2597 * @buffer_a: One buffer to swap with
2598 * @buffer_b: The other buffer to swap with
2600 * This function is useful for tracers that want to take a "snapshot"
2601 * of a CPU buffer and has another back up buffer lying around.
2602 * it is expected that the tracer handles the cpu buffer not being
2603 * used at the moment.
2605 int ring_buffer_swap_cpu(struct ring_buffer *buffer_a,
2606 struct ring_buffer *buffer_b, int cpu)
2608 struct ring_buffer_per_cpu *cpu_buffer_a;
2609 struct ring_buffer_per_cpu *cpu_buffer_b;
2612 if (!cpumask_test_cpu(cpu, buffer_a->cpumask) ||
2613 !cpumask_test_cpu(cpu, buffer_b->cpumask))
2616 /* At least make sure the two buffers are somewhat the same */
2617 if (buffer_a->pages != buffer_b->pages)
2622 if (ring_buffer_flags != RB_BUFFERS_ON)
2625 if (atomic_read(&buffer_a->record_disabled))
2628 if (atomic_read(&buffer_b->record_disabled))
2631 cpu_buffer_a = buffer_a->buffers[cpu];
2632 cpu_buffer_b = buffer_b->buffers[cpu];
2634 if (atomic_read(&cpu_buffer_a->record_disabled))
2637 if (atomic_read(&cpu_buffer_b->record_disabled))
2641 * We can't do a synchronize_sched here because this
2642 * function can be called in atomic context.
2643 * Normally this will be called from the same CPU as cpu.
2644 * If not it's up to the caller to protect this.
2646 atomic_inc(&cpu_buffer_a->record_disabled);
2647 atomic_inc(&cpu_buffer_b->record_disabled);
2649 buffer_a->buffers[cpu] = cpu_buffer_b;
2650 buffer_b->buffers[cpu] = cpu_buffer_a;
2652 cpu_buffer_b->buffer = buffer_a;
2653 cpu_buffer_a->buffer = buffer_b;
2655 atomic_dec(&cpu_buffer_a->record_disabled);
2656 atomic_dec(&cpu_buffer_b->record_disabled);
2662 EXPORT_SYMBOL_GPL(ring_buffer_swap_cpu);
2664 static void rb_remove_entries(struct ring_buffer_per_cpu *cpu_buffer,
2665 struct buffer_data_page *bpage,
2666 unsigned int offset)
2668 struct ring_buffer_event *event;
2671 __raw_spin_lock(&cpu_buffer->lock);
2672 for (head = offset; head < local_read(&bpage->commit);
2673 head += rb_event_length(event)) {
2675 event = __rb_data_page_index(bpage, head);
2676 if (RB_WARN_ON(cpu_buffer, rb_null_event(event)))
2678 /* Only count data entries */
2679 if (event->type != RINGBUF_TYPE_DATA)
2681 cpu_buffer->entries--;
2683 __raw_spin_unlock(&cpu_buffer->lock);
2687 * ring_buffer_alloc_read_page - allocate a page to read from buffer
2688 * @buffer: the buffer to allocate for.
2690 * This function is used in conjunction with ring_buffer_read_page.
2691 * When reading a full page from the ring buffer, these functions
2692 * can be used to speed up the process. The calling function should
2693 * allocate a few pages first with this function. Then when it
2694 * needs to get pages from the ring buffer, it passes the result
2695 * of this function into ring_buffer_read_page, which will swap
2696 * the page that was allocated, with the read page of the buffer.
2699 * The page allocated, or NULL on error.
2701 void *ring_buffer_alloc_read_page(struct ring_buffer *buffer)
2703 struct buffer_data_page *bpage;
2706 addr = __get_free_page(GFP_KERNEL);
2710 bpage = (void *)addr;
2712 rb_init_page(bpage);
2718 * ring_buffer_free_read_page - free an allocated read page
2719 * @buffer: the buffer the page was allocate for
2720 * @data: the page to free
2722 * Free a page allocated from ring_buffer_alloc_read_page.
2724 void ring_buffer_free_read_page(struct ring_buffer *buffer, void *data)
2726 free_page((unsigned long)data);
2730 * ring_buffer_read_page - extract a page from the ring buffer
2731 * @buffer: buffer to extract from
2732 * @data_page: the page to use allocated from ring_buffer_alloc_read_page
2733 * @len: amount to extract
2734 * @cpu: the cpu of the buffer to extract
2735 * @full: should the extraction only happen when the page is full.
2737 * This function will pull out a page from the ring buffer and consume it.
2738 * @data_page must be the address of the variable that was returned
2739 * from ring_buffer_alloc_read_page. This is because the page might be used
2740 * to swap with a page in the ring buffer.
2743 * rpage = ring_buffer_alloc_read_page(buffer);
2746 * ret = ring_buffer_read_page(buffer, &rpage, len, cpu, 0);
2748 * process_page(rpage, ret);
2750 * When @full is set, the function will not return true unless
2751 * the writer is off the reader page.
2753 * Note: it is up to the calling functions to handle sleeps and wakeups.
2754 * The ring buffer can be used anywhere in the kernel and can not
2755 * blindly call wake_up. The layer that uses the ring buffer must be
2756 * responsible for that.
2759 * >=0 if data has been transferred, returns the offset of consumed data.
2760 * <0 if no data has been transferred.
2762 int ring_buffer_read_page(struct ring_buffer *buffer,
2763 void **data_page, size_t len, int cpu, int full)
2765 struct ring_buffer_per_cpu *cpu_buffer = buffer->buffers[cpu];
2766 struct ring_buffer_event *event;
2767 struct buffer_data_page *bpage;
2768 struct buffer_page *reader;
2769 unsigned long flags;
2770 unsigned int commit;
2775 if (!cpumask_test_cpu(cpu, buffer->cpumask))
2779 * If len is not big enough to hold the page header, then
2780 * we can not copy anything.
2782 if (len <= BUF_PAGE_HDR_SIZE)
2785 len -= BUF_PAGE_HDR_SIZE;
2794 spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
2796 reader = rb_get_reader_page(cpu_buffer);
2800 event = rb_reader_event(cpu_buffer);
2802 read = reader->read;
2803 commit = rb_page_commit(reader);
2806 * If this page has been partially read or
2807 * if len is not big enough to read the rest of the page or
2808 * a writer is still on the page, then
2809 * we must copy the data from the page to the buffer.
2810 * Otherwise, we can simply swap the page with the one passed in.
2812 if (read || (len < (commit - read)) ||
2813 cpu_buffer->reader_page == cpu_buffer->commit_page) {
2814 struct buffer_data_page *rpage = cpu_buffer->reader_page->page;
2815 unsigned int rpos = read;
2816 unsigned int pos = 0;
2822 if (len > (commit - read))
2823 len = (commit - read);
2825 size = rb_event_length(event);
2830 /* save the current timestamp, since the user will need it */
2831 save_timestamp = cpu_buffer->read_stamp;
2833 /* Need to copy one event at a time */
2835 memcpy(bpage->data + pos, rpage->data + rpos, size);
2839 rb_advance_reader(cpu_buffer);
2840 rpos = reader->read;
2843 event = rb_reader_event(cpu_buffer);
2844 size = rb_event_length(event);
2845 } while (len > size);
2848 local_set(&bpage->commit, pos);
2849 bpage->time_stamp = save_timestamp;
2851 /* we copied everything to the beginning */
2854 /* swap the pages */
2855 rb_init_page(bpage);
2856 bpage = reader->page;
2857 reader->page = *data_page;
2858 local_set(&reader->write, 0);
2862 /* update the entry counter */
2863 rb_remove_entries(cpu_buffer, bpage, read);
2868 spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
2875 rb_simple_read(struct file *filp, char __user *ubuf,
2876 size_t cnt, loff_t *ppos)
2878 unsigned long *p = filp->private_data;
2882 if (test_bit(RB_BUFFERS_DISABLED_BIT, p))
2883 r = sprintf(buf, "permanently disabled\n");
2885 r = sprintf(buf, "%d\n", test_bit(RB_BUFFERS_ON_BIT, p));
2887 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2891 rb_simple_write(struct file *filp, const char __user *ubuf,
2892 size_t cnt, loff_t *ppos)
2894 unsigned long *p = filp->private_data;
2899 if (cnt >= sizeof(buf))
2902 if (copy_from_user(&buf, ubuf, cnt))
2907 ret = strict_strtoul(buf, 10, &val);
2912 set_bit(RB_BUFFERS_ON_BIT, p);
2914 clear_bit(RB_BUFFERS_ON_BIT, p);
2921 static const struct file_operations rb_simple_fops = {
2922 .open = tracing_open_generic,
2923 .read = rb_simple_read,
2924 .write = rb_simple_write,
2928 static __init int rb_init_debugfs(void)
2930 struct dentry *d_tracer;
2932 d_tracer = tracing_init_dentry();
2934 trace_create_file("tracing_on", 0644, d_tracer,
2935 &ring_buffer_flags, &rb_simple_fops);
2940 fs_initcall(rb_init_debugfs);
2942 #ifdef CONFIG_HOTPLUG_CPU
2943 static int rb_cpu_notify(struct notifier_block *self,
2944 unsigned long action, void *hcpu)
2946 struct ring_buffer *buffer =
2947 container_of(self, struct ring_buffer, cpu_notify);
2948 long cpu = (long)hcpu;
2951 case CPU_UP_PREPARE:
2952 case CPU_UP_PREPARE_FROZEN:
2953 if (cpu_isset(cpu, *buffer->cpumask))
2956 buffer->buffers[cpu] =
2957 rb_allocate_cpu_buffer(buffer, cpu);
2958 if (!buffer->buffers[cpu]) {
2959 WARN(1, "failed to allocate ring buffer on CPU %ld\n",
2964 cpu_set(cpu, *buffer->cpumask);
2966 case CPU_DOWN_PREPARE:
2967 case CPU_DOWN_PREPARE_FROZEN:
2970 * If we were to free the buffer, then the user would
2971 * lose any trace that was in the buffer.