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 header is special. We must manually up keep it.
27 int ring_buffer_print_entry_header(struct trace_seq *s)
31 ret = trace_seq_printf(s, "\ttype : 2 bits\n");
32 ret = trace_seq_printf(s, "\tlen : 3 bits\n");
33 ret = trace_seq_printf(s, "\ttime_delta : 27 bits\n");
34 ret = trace_seq_printf(s, "\tarray : 32 bits\n");
35 ret = trace_seq_printf(s, "\n");
36 ret = trace_seq_printf(s, "\tpadding : type == %d\n",
37 RINGBUF_TYPE_PADDING);
38 ret = trace_seq_printf(s, "\ttime_extend : type == %d\n",
39 RINGBUF_TYPE_TIME_EXTEND);
40 ret = trace_seq_printf(s, "\tdata : type == %d\n",
47 * The ring buffer is made up of a list of pages. A separate list of pages is
48 * allocated for each CPU. A writer may only write to a buffer that is
49 * associated with the CPU it is currently executing on. A reader may read
50 * from any per cpu buffer.
52 * The reader is special. For each per cpu buffer, the reader has its own
53 * reader page. When a reader has read the entire reader page, this reader
54 * page is swapped with another page in the ring buffer.
56 * Now, as long as the writer is off the reader page, the reader can do what
57 * ever it wants with that page. The writer will never write to that page
58 * again (as long as it is out of the ring buffer).
60 * Here's some silly ASCII art.
63 * |reader| RING BUFFER
65 * +------+ +---+ +---+ +---+
74 * |reader| RING BUFFER
75 * |page |------------------v
76 * +------+ +---+ +---+ +---+
85 * |reader| RING BUFFER
86 * |page |------------------v
87 * +------+ +---+ +---+ +---+
92 * +------------------------------+
96 * |buffer| RING BUFFER
97 * |page |------------------v
98 * +------+ +---+ +---+ +---+
100 * | New +---+ +---+ +---+
103 * +------------------------------+
106 * After we make this swap, the reader can hand this page off to the splice
107 * code and be done with it. It can even allocate a new page if it needs to
108 * and swap that into the ring buffer.
110 * We will be using cmpxchg soon to make all this lockless.
115 * A fast way to enable or disable all ring buffers is to
116 * call tracing_on or tracing_off. Turning off the ring buffers
117 * prevents all ring buffers from being recorded to.
118 * Turning this switch on, makes it OK to write to the
119 * ring buffer, if the ring buffer is enabled itself.
121 * There's three layers that must be on in order to write
122 * to the ring buffer.
124 * 1) This global flag must be set.
125 * 2) The ring buffer must be enabled for recording.
126 * 3) The per cpu buffer must be enabled for recording.
128 * In case of an anomaly, this global flag has a bit set that
129 * will permantly disable all ring buffers.
133 * Global flag to disable all recording to ring buffers
134 * This has two bits: ON, DISABLED
138 * 0 0 : ring buffers are off
139 * 1 0 : ring buffers are on
140 * X 1 : ring buffers are permanently disabled
144 RB_BUFFERS_ON_BIT = 0,
145 RB_BUFFERS_DISABLED_BIT = 1,
149 RB_BUFFERS_ON = 1 << RB_BUFFERS_ON_BIT,
150 RB_BUFFERS_DISABLED = 1 << RB_BUFFERS_DISABLED_BIT,
153 static unsigned long ring_buffer_flags __read_mostly = RB_BUFFERS_ON;
155 #define BUF_PAGE_HDR_SIZE offsetof(struct buffer_data_page, data)
158 * tracing_on - enable all tracing buffers
160 * This function enables all tracing buffers that may have been
161 * disabled with tracing_off.
163 void tracing_on(void)
165 set_bit(RB_BUFFERS_ON_BIT, &ring_buffer_flags);
167 EXPORT_SYMBOL_GPL(tracing_on);
170 * tracing_off - turn off all tracing buffers
172 * This function stops all tracing buffers from recording data.
173 * It does not disable any overhead the tracers themselves may
174 * be causing. This function simply causes all recording to
175 * the ring buffers to fail.
177 void tracing_off(void)
179 clear_bit(RB_BUFFERS_ON_BIT, &ring_buffer_flags);
181 EXPORT_SYMBOL_GPL(tracing_off);
184 * tracing_off_permanent - permanently disable ring buffers
186 * This function, once called, will disable all ring buffers
189 void tracing_off_permanent(void)
191 set_bit(RB_BUFFERS_DISABLED_BIT, &ring_buffer_flags);
195 * tracing_is_on - show state of ring buffers enabled
197 int tracing_is_on(void)
199 return ring_buffer_flags == RB_BUFFERS_ON;
201 EXPORT_SYMBOL_GPL(tracing_is_on);
205 #define RB_EVNT_HDR_SIZE (offsetof(struct ring_buffer_event, array))
206 #define RB_ALIGNMENT 4U
207 #define RB_MAX_SMALL_DATA 28
210 RB_LEN_TIME_EXTEND = 8,
211 RB_LEN_TIME_STAMP = 16,
214 static inline int rb_null_event(struct ring_buffer_event *event)
216 return event->type == RINGBUF_TYPE_PADDING && event->time_delta == 0;
219 static inline int rb_discarded_event(struct ring_buffer_event *event)
221 return event->type == RINGBUF_TYPE_PADDING && event->time_delta;
224 static void rb_event_set_padding(struct ring_buffer_event *event)
226 event->type = RINGBUF_TYPE_PADDING;
227 event->time_delta = 0;
231 rb_event_data_length(struct ring_buffer_event *event)
236 length = event->len * RB_ALIGNMENT;
238 length = event->array[0];
239 return length + RB_EVNT_HDR_SIZE;
242 /* inline for ring buffer fast paths */
244 rb_event_length(struct ring_buffer_event *event)
246 switch (event->type) {
247 case RINGBUF_TYPE_PADDING:
248 if (rb_null_event(event))
251 return rb_event_data_length(event);
253 case RINGBUF_TYPE_TIME_EXTEND:
254 return RB_LEN_TIME_EXTEND;
256 case RINGBUF_TYPE_TIME_STAMP:
257 return RB_LEN_TIME_STAMP;
259 case RINGBUF_TYPE_DATA:
260 return rb_event_data_length(event);
269 * ring_buffer_event_length - return the length of the event
270 * @event: the event to get the length of
272 unsigned ring_buffer_event_length(struct ring_buffer_event *event)
274 unsigned length = rb_event_length(event);
275 if (event->type != RINGBUF_TYPE_DATA)
277 length -= RB_EVNT_HDR_SIZE;
278 if (length > RB_MAX_SMALL_DATA + sizeof(event->array[0]))
279 length -= sizeof(event->array[0]);
282 EXPORT_SYMBOL_GPL(ring_buffer_event_length);
284 /* inline for ring buffer fast paths */
286 rb_event_data(struct ring_buffer_event *event)
288 BUG_ON(event->type != RINGBUF_TYPE_DATA);
289 /* If length is in len field, then array[0] has the data */
291 return (void *)&event->array[0];
292 /* Otherwise length is in array[0] and array[1] has the data */
293 return (void *)&event->array[1];
297 * ring_buffer_event_data - return the data of the event
298 * @event: the event to get the data from
300 void *ring_buffer_event_data(struct ring_buffer_event *event)
302 return rb_event_data(event);
304 EXPORT_SYMBOL_GPL(ring_buffer_event_data);
306 #define for_each_buffer_cpu(buffer, cpu) \
307 for_each_cpu(cpu, buffer->cpumask)
310 #define TS_MASK ((1ULL << TS_SHIFT) - 1)
311 #define TS_DELTA_TEST (~TS_MASK)
313 struct buffer_data_page {
314 u64 time_stamp; /* page time stamp */
315 local_t commit; /* write committed index */
316 unsigned char data[]; /* data of buffer page */
320 local_t write; /* index for next write */
321 unsigned read; /* index for next read */
322 struct list_head list; /* list of free pages */
323 struct buffer_data_page *page; /* Actual data page */
326 static void rb_init_page(struct buffer_data_page *bpage)
328 local_set(&bpage->commit, 0);
332 * ring_buffer_page_len - the size of data on the page.
333 * @page: The page to read
335 * Returns the amount of data on the page, including buffer page header.
337 size_t ring_buffer_page_len(void *page)
339 return local_read(&((struct buffer_data_page *)page)->commit)
344 * Also stolen from mm/slob.c. Thanks to Mathieu Desnoyers for pointing
347 static void free_buffer_page(struct buffer_page *bpage)
349 free_page((unsigned long)bpage->page);
354 * We need to fit the time_stamp delta into 27 bits.
356 static inline int test_time_stamp(u64 delta)
358 if (delta & TS_DELTA_TEST)
363 #define BUF_PAGE_SIZE (PAGE_SIZE - BUF_PAGE_HDR_SIZE)
365 int ring_buffer_print_page_header(struct trace_seq *s)
367 struct buffer_data_page field;
370 ret = trace_seq_printf(s, "\tfield: u64 timestamp;\t"
371 "offset:0;\tsize:%u;\n",
372 (unsigned int)sizeof(field.time_stamp));
374 ret = trace_seq_printf(s, "\tfield: local_t commit;\t"
375 "offset:%u;\tsize:%u;\n",
376 (unsigned int)offsetof(typeof(field), commit),
377 (unsigned int)sizeof(field.commit));
379 ret = trace_seq_printf(s, "\tfield: char data;\t"
380 "offset:%u;\tsize:%u;\n",
381 (unsigned int)offsetof(typeof(field), data),
382 (unsigned int)BUF_PAGE_SIZE);
388 * head_page == tail_page && head == tail then buffer is empty.
390 struct ring_buffer_per_cpu {
392 struct ring_buffer *buffer;
393 spinlock_t reader_lock; /* serialize readers */
395 struct lock_class_key lock_key;
396 struct list_head pages;
397 struct buffer_page *head_page; /* read from head */
398 struct buffer_page *tail_page; /* write to tail */
399 struct buffer_page *commit_page; /* committed pages */
400 struct buffer_page *reader_page;
401 unsigned long overrun;
402 unsigned long entries;
405 atomic_t record_disabled;
412 atomic_t record_disabled;
413 cpumask_var_t cpumask;
417 struct ring_buffer_per_cpu **buffers;
419 #ifdef CONFIG_HOTPLUG_CPU
420 struct notifier_block cpu_notify;
425 struct ring_buffer_iter {
426 struct ring_buffer_per_cpu *cpu_buffer;
428 struct buffer_page *head_page;
432 /* buffer may be either ring_buffer or ring_buffer_per_cpu */
433 #define RB_WARN_ON(buffer, cond) \
435 int _____ret = unlikely(cond); \
437 atomic_inc(&buffer->record_disabled); \
443 /* Up this if you want to test the TIME_EXTENTS and normalization */
444 #define DEBUG_SHIFT 0
446 u64 ring_buffer_time_stamp(struct ring_buffer *buffer, int cpu)
450 preempt_disable_notrace();
451 /* shift to debug/test normalization and TIME_EXTENTS */
452 time = buffer->clock() << DEBUG_SHIFT;
453 preempt_enable_no_resched_notrace();
457 EXPORT_SYMBOL_GPL(ring_buffer_time_stamp);
459 void ring_buffer_normalize_time_stamp(struct ring_buffer *buffer,
462 /* Just stupid testing the normalize function and deltas */
465 EXPORT_SYMBOL_GPL(ring_buffer_normalize_time_stamp);
468 * check_pages - integrity check of buffer pages
469 * @cpu_buffer: CPU buffer with pages to test
471 * As a safety measure we check to make sure the data pages have not
474 static int rb_check_pages(struct ring_buffer_per_cpu *cpu_buffer)
476 struct list_head *head = &cpu_buffer->pages;
477 struct buffer_page *bpage, *tmp;
479 if (RB_WARN_ON(cpu_buffer, head->next->prev != head))
481 if (RB_WARN_ON(cpu_buffer, head->prev->next != head))
484 list_for_each_entry_safe(bpage, tmp, head, list) {
485 if (RB_WARN_ON(cpu_buffer,
486 bpage->list.next->prev != &bpage->list))
488 if (RB_WARN_ON(cpu_buffer,
489 bpage->list.prev->next != &bpage->list))
496 static int rb_allocate_pages(struct ring_buffer_per_cpu *cpu_buffer,
499 struct list_head *head = &cpu_buffer->pages;
500 struct buffer_page *bpage, *tmp;
505 for (i = 0; i < nr_pages; i++) {
506 bpage = kzalloc_node(ALIGN(sizeof(*bpage), cache_line_size()),
507 GFP_KERNEL, cpu_to_node(cpu_buffer->cpu));
510 list_add(&bpage->list, &pages);
512 addr = __get_free_page(GFP_KERNEL);
515 bpage->page = (void *)addr;
516 rb_init_page(bpage->page);
519 list_splice(&pages, head);
521 rb_check_pages(cpu_buffer);
526 list_for_each_entry_safe(bpage, tmp, &pages, list) {
527 list_del_init(&bpage->list);
528 free_buffer_page(bpage);
533 static struct ring_buffer_per_cpu *
534 rb_allocate_cpu_buffer(struct ring_buffer *buffer, int cpu)
536 struct ring_buffer_per_cpu *cpu_buffer;
537 struct buffer_page *bpage;
541 cpu_buffer = kzalloc_node(ALIGN(sizeof(*cpu_buffer), cache_line_size()),
542 GFP_KERNEL, cpu_to_node(cpu));
546 cpu_buffer->cpu = cpu;
547 cpu_buffer->buffer = buffer;
548 spin_lock_init(&cpu_buffer->reader_lock);
549 cpu_buffer->lock = (raw_spinlock_t)__RAW_SPIN_LOCK_UNLOCKED;
550 INIT_LIST_HEAD(&cpu_buffer->pages);
552 bpage = kzalloc_node(ALIGN(sizeof(*bpage), cache_line_size()),
553 GFP_KERNEL, cpu_to_node(cpu));
555 goto fail_free_buffer;
557 cpu_buffer->reader_page = bpage;
558 addr = __get_free_page(GFP_KERNEL);
560 goto fail_free_reader;
561 bpage->page = (void *)addr;
562 rb_init_page(bpage->page);
564 INIT_LIST_HEAD(&cpu_buffer->reader_page->list);
566 ret = rb_allocate_pages(cpu_buffer, buffer->pages);
568 goto fail_free_reader;
570 cpu_buffer->head_page
571 = list_entry(cpu_buffer->pages.next, struct buffer_page, list);
572 cpu_buffer->tail_page = cpu_buffer->commit_page = cpu_buffer->head_page;
577 free_buffer_page(cpu_buffer->reader_page);
584 static void rb_free_cpu_buffer(struct ring_buffer_per_cpu *cpu_buffer)
586 struct list_head *head = &cpu_buffer->pages;
587 struct buffer_page *bpage, *tmp;
589 free_buffer_page(cpu_buffer->reader_page);
591 list_for_each_entry_safe(bpage, tmp, head, list) {
592 list_del_init(&bpage->list);
593 free_buffer_page(bpage);
599 * Causes compile errors if the struct buffer_page gets bigger
600 * than the struct page.
602 extern int ring_buffer_page_too_big(void);
604 #ifdef CONFIG_HOTPLUG_CPU
605 static int rb_cpu_notify(struct notifier_block *self,
606 unsigned long action, void *hcpu);
610 * ring_buffer_alloc - allocate a new ring_buffer
611 * @size: the size in bytes per cpu that is needed.
612 * @flags: attributes to set for the ring buffer.
614 * Currently the only flag that is available is the RB_FL_OVERWRITE
615 * flag. This flag means that the buffer will overwrite old data
616 * when the buffer wraps. If this flag is not set, the buffer will
617 * drop data when the tail hits the head.
619 struct ring_buffer *ring_buffer_alloc(unsigned long size, unsigned flags)
621 struct ring_buffer *buffer;
625 /* Paranoid! Optimizes out when all is well */
626 if (sizeof(struct buffer_page) > sizeof(struct page))
627 ring_buffer_page_too_big();
630 /* keep it in its own cache line */
631 buffer = kzalloc(ALIGN(sizeof(*buffer), cache_line_size()),
636 if (!alloc_cpumask_var(&buffer->cpumask, GFP_KERNEL))
637 goto fail_free_buffer;
639 buffer->pages = DIV_ROUND_UP(size, BUF_PAGE_SIZE);
640 buffer->flags = flags;
641 buffer->clock = trace_clock_local;
643 /* need at least two pages */
644 if (buffer->pages == 1)
648 * In case of non-hotplug cpu, if the ring-buffer is allocated
649 * in early initcall, it will not be notified of secondary cpus.
650 * In that off case, we need to allocate for all possible cpus.
652 #ifdef CONFIG_HOTPLUG_CPU
654 cpumask_copy(buffer->cpumask, cpu_online_mask);
656 cpumask_copy(buffer->cpumask, cpu_possible_mask);
658 buffer->cpus = nr_cpu_ids;
660 bsize = sizeof(void *) * nr_cpu_ids;
661 buffer->buffers = kzalloc(ALIGN(bsize, cache_line_size()),
663 if (!buffer->buffers)
664 goto fail_free_cpumask;
666 for_each_buffer_cpu(buffer, cpu) {
667 buffer->buffers[cpu] =
668 rb_allocate_cpu_buffer(buffer, cpu);
669 if (!buffer->buffers[cpu])
670 goto fail_free_buffers;
673 #ifdef CONFIG_HOTPLUG_CPU
674 buffer->cpu_notify.notifier_call = rb_cpu_notify;
675 buffer->cpu_notify.priority = 0;
676 register_cpu_notifier(&buffer->cpu_notify);
680 mutex_init(&buffer->mutex);
685 for_each_buffer_cpu(buffer, cpu) {
686 if (buffer->buffers[cpu])
687 rb_free_cpu_buffer(buffer->buffers[cpu]);
689 kfree(buffer->buffers);
692 free_cpumask_var(buffer->cpumask);
699 EXPORT_SYMBOL_GPL(ring_buffer_alloc);
702 * ring_buffer_free - free a ring buffer.
703 * @buffer: the buffer to free.
706 ring_buffer_free(struct ring_buffer *buffer)
712 #ifdef CONFIG_HOTPLUG_CPU
713 unregister_cpu_notifier(&buffer->cpu_notify);
716 for_each_buffer_cpu(buffer, cpu)
717 rb_free_cpu_buffer(buffer->buffers[cpu]);
721 free_cpumask_var(buffer->cpumask);
725 EXPORT_SYMBOL_GPL(ring_buffer_free);
727 void ring_buffer_set_clock(struct ring_buffer *buffer,
730 buffer->clock = clock;
733 static void rb_reset_cpu(struct ring_buffer_per_cpu *cpu_buffer);
736 rb_remove_pages(struct ring_buffer_per_cpu *cpu_buffer, unsigned nr_pages)
738 struct buffer_page *bpage;
742 atomic_inc(&cpu_buffer->record_disabled);
745 for (i = 0; i < nr_pages; i++) {
746 if (RB_WARN_ON(cpu_buffer, list_empty(&cpu_buffer->pages)))
748 p = cpu_buffer->pages.next;
749 bpage = list_entry(p, struct buffer_page, list);
750 list_del_init(&bpage->list);
751 free_buffer_page(bpage);
753 if (RB_WARN_ON(cpu_buffer, list_empty(&cpu_buffer->pages)))
756 rb_reset_cpu(cpu_buffer);
758 rb_check_pages(cpu_buffer);
760 atomic_dec(&cpu_buffer->record_disabled);
765 rb_insert_pages(struct ring_buffer_per_cpu *cpu_buffer,
766 struct list_head *pages, unsigned nr_pages)
768 struct buffer_page *bpage;
772 atomic_inc(&cpu_buffer->record_disabled);
775 for (i = 0; i < nr_pages; i++) {
776 if (RB_WARN_ON(cpu_buffer, list_empty(pages)))
779 bpage = list_entry(p, struct buffer_page, list);
780 list_del_init(&bpage->list);
781 list_add_tail(&bpage->list, &cpu_buffer->pages);
783 rb_reset_cpu(cpu_buffer);
785 rb_check_pages(cpu_buffer);
787 atomic_dec(&cpu_buffer->record_disabled);
791 * ring_buffer_resize - resize the ring buffer
792 * @buffer: the buffer to resize.
793 * @size: the new size.
795 * The tracer is responsible for making sure that the buffer is
796 * not being used while changing the size.
797 * Note: We may be able to change the above requirement by using
798 * RCU synchronizations.
800 * Minimum size is 2 * BUF_PAGE_SIZE.
802 * Returns -1 on failure.
804 int ring_buffer_resize(struct ring_buffer *buffer, unsigned long size)
806 struct ring_buffer_per_cpu *cpu_buffer;
807 unsigned nr_pages, rm_pages, new_pages;
808 struct buffer_page *bpage, *tmp;
809 unsigned long buffer_size;
815 * Always succeed at resizing a non-existent buffer:
820 size = DIV_ROUND_UP(size, BUF_PAGE_SIZE);
821 size *= BUF_PAGE_SIZE;
822 buffer_size = buffer->pages * BUF_PAGE_SIZE;
824 /* we need a minimum of two pages */
825 if (size < BUF_PAGE_SIZE * 2)
826 size = BUF_PAGE_SIZE * 2;
828 if (size == buffer_size)
831 mutex_lock(&buffer->mutex);
834 nr_pages = DIV_ROUND_UP(size, BUF_PAGE_SIZE);
836 if (size < buffer_size) {
838 /* easy case, just free pages */
839 if (RB_WARN_ON(buffer, nr_pages >= buffer->pages))
842 rm_pages = buffer->pages - nr_pages;
844 for_each_buffer_cpu(buffer, cpu) {
845 cpu_buffer = buffer->buffers[cpu];
846 rb_remove_pages(cpu_buffer, rm_pages);
852 * This is a bit more difficult. We only want to add pages
853 * when we can allocate enough for all CPUs. We do this
854 * by allocating all the pages and storing them on a local
855 * link list. If we succeed in our allocation, then we
856 * add these pages to the cpu_buffers. Otherwise we just free
857 * them all and return -ENOMEM;
859 if (RB_WARN_ON(buffer, nr_pages <= buffer->pages))
862 new_pages = nr_pages - buffer->pages;
864 for_each_buffer_cpu(buffer, cpu) {
865 for (i = 0; i < new_pages; i++) {
866 bpage = kzalloc_node(ALIGN(sizeof(*bpage),
868 GFP_KERNEL, cpu_to_node(cpu));
871 list_add(&bpage->list, &pages);
872 addr = __get_free_page(GFP_KERNEL);
875 bpage->page = (void *)addr;
876 rb_init_page(bpage->page);
880 for_each_buffer_cpu(buffer, cpu) {
881 cpu_buffer = buffer->buffers[cpu];
882 rb_insert_pages(cpu_buffer, &pages, new_pages);
885 if (RB_WARN_ON(buffer, !list_empty(&pages)))
889 buffer->pages = nr_pages;
891 mutex_unlock(&buffer->mutex);
896 list_for_each_entry_safe(bpage, tmp, &pages, list) {
897 list_del_init(&bpage->list);
898 free_buffer_page(bpage);
901 mutex_unlock(&buffer->mutex);
905 * Something went totally wrong, and we are too paranoid
906 * to even clean up the mess.
910 mutex_unlock(&buffer->mutex);
913 EXPORT_SYMBOL_GPL(ring_buffer_resize);
916 __rb_data_page_index(struct buffer_data_page *bpage, unsigned index)
918 return bpage->data + index;
921 static inline void *__rb_page_index(struct buffer_page *bpage, unsigned index)
923 return bpage->page->data + index;
926 static inline struct ring_buffer_event *
927 rb_reader_event(struct ring_buffer_per_cpu *cpu_buffer)
929 return __rb_page_index(cpu_buffer->reader_page,
930 cpu_buffer->reader_page->read);
933 static inline struct ring_buffer_event *
934 rb_head_event(struct ring_buffer_per_cpu *cpu_buffer)
936 return __rb_page_index(cpu_buffer->head_page,
937 cpu_buffer->head_page->read);
940 static inline struct ring_buffer_event *
941 rb_iter_head_event(struct ring_buffer_iter *iter)
943 return __rb_page_index(iter->head_page, iter->head);
946 static inline unsigned rb_page_write(struct buffer_page *bpage)
948 return local_read(&bpage->write);
951 static inline unsigned rb_page_commit(struct buffer_page *bpage)
953 return local_read(&bpage->page->commit);
956 /* Size is determined by what has been commited */
957 static inline unsigned rb_page_size(struct buffer_page *bpage)
959 return rb_page_commit(bpage);
962 static inline unsigned
963 rb_commit_index(struct ring_buffer_per_cpu *cpu_buffer)
965 return rb_page_commit(cpu_buffer->commit_page);
968 static inline unsigned rb_head_size(struct ring_buffer_per_cpu *cpu_buffer)
970 return rb_page_commit(cpu_buffer->head_page);
974 * When the tail hits the head and the buffer is in overwrite mode,
975 * the head jumps to the next page and all content on the previous
976 * page is discarded. But before doing so, we update the overrun
977 * variable of the buffer.
979 static void rb_update_overflow(struct ring_buffer_per_cpu *cpu_buffer)
981 struct ring_buffer_event *event;
984 for (head = 0; head < rb_head_size(cpu_buffer);
985 head += rb_event_length(event)) {
987 event = __rb_page_index(cpu_buffer->head_page, head);
988 if (RB_WARN_ON(cpu_buffer, rb_null_event(event)))
990 /* Only count data entries */
991 if (event->type != RINGBUF_TYPE_DATA)
993 cpu_buffer->overrun++;
994 cpu_buffer->entries--;
998 static inline void rb_inc_page(struct ring_buffer_per_cpu *cpu_buffer,
999 struct buffer_page **bpage)
1001 struct list_head *p = (*bpage)->list.next;
1003 if (p == &cpu_buffer->pages)
1006 *bpage = list_entry(p, struct buffer_page, list);
1009 static inline unsigned
1010 rb_event_index(struct ring_buffer_event *event)
1012 unsigned long addr = (unsigned long)event;
1014 return (addr & ~PAGE_MASK) - (PAGE_SIZE - BUF_PAGE_SIZE);
1018 rb_is_commit(struct ring_buffer_per_cpu *cpu_buffer,
1019 struct ring_buffer_event *event)
1021 unsigned long addr = (unsigned long)event;
1022 unsigned long index;
1024 index = rb_event_index(event);
1027 return cpu_buffer->commit_page->page == (void *)addr &&
1028 rb_commit_index(cpu_buffer) == index;
1032 rb_set_commit_event(struct ring_buffer_per_cpu *cpu_buffer,
1033 struct ring_buffer_event *event)
1035 unsigned long addr = (unsigned long)event;
1036 unsigned long index;
1038 index = rb_event_index(event);
1041 while (cpu_buffer->commit_page->page != (void *)addr) {
1042 if (RB_WARN_ON(cpu_buffer,
1043 cpu_buffer->commit_page == cpu_buffer->tail_page))
1045 cpu_buffer->commit_page->page->commit =
1046 cpu_buffer->commit_page->write;
1047 rb_inc_page(cpu_buffer, &cpu_buffer->commit_page);
1048 cpu_buffer->write_stamp =
1049 cpu_buffer->commit_page->page->time_stamp;
1052 /* Now set the commit to the event's index */
1053 local_set(&cpu_buffer->commit_page->page->commit, index);
1057 rb_set_commit_to_write(struct ring_buffer_per_cpu *cpu_buffer)
1060 * We only race with interrupts and NMIs on this CPU.
1061 * If we own the commit event, then we can commit
1062 * all others that interrupted us, since the interruptions
1063 * are in stack format (they finish before they come
1064 * back to us). This allows us to do a simple loop to
1065 * assign the commit to the tail.
1068 while (cpu_buffer->commit_page != cpu_buffer->tail_page) {
1069 cpu_buffer->commit_page->page->commit =
1070 cpu_buffer->commit_page->write;
1071 rb_inc_page(cpu_buffer, &cpu_buffer->commit_page);
1072 cpu_buffer->write_stamp =
1073 cpu_buffer->commit_page->page->time_stamp;
1074 /* add barrier to keep gcc from optimizing too much */
1077 while (rb_commit_index(cpu_buffer) !=
1078 rb_page_write(cpu_buffer->commit_page)) {
1079 cpu_buffer->commit_page->page->commit =
1080 cpu_buffer->commit_page->write;
1084 /* again, keep gcc from optimizing */
1088 * If an interrupt came in just after the first while loop
1089 * and pushed the tail page forward, we will be left with
1090 * a dangling commit that will never go forward.
1092 if (unlikely(cpu_buffer->commit_page != cpu_buffer->tail_page))
1096 static void rb_reset_reader_page(struct ring_buffer_per_cpu *cpu_buffer)
1098 cpu_buffer->read_stamp = cpu_buffer->reader_page->page->time_stamp;
1099 cpu_buffer->reader_page->read = 0;
1102 static void rb_inc_iter(struct ring_buffer_iter *iter)
1104 struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
1107 * The iterator could be on the reader page (it starts there).
1108 * But the head could have moved, since the reader was
1109 * found. Check for this case and assign the iterator
1110 * to the head page instead of next.
1112 if (iter->head_page == cpu_buffer->reader_page)
1113 iter->head_page = cpu_buffer->head_page;
1115 rb_inc_page(cpu_buffer, &iter->head_page);
1117 iter->read_stamp = iter->head_page->page->time_stamp;
1122 * ring_buffer_update_event - update event type and data
1123 * @event: the even to update
1124 * @type: the type of event
1125 * @length: the size of the event field in the ring buffer
1127 * Update the type and data fields of the event. The length
1128 * is the actual size that is written to the ring buffer,
1129 * and with this, we can determine what to place into the
1133 rb_update_event(struct ring_buffer_event *event,
1134 unsigned type, unsigned length)
1140 case RINGBUF_TYPE_PADDING:
1143 case RINGBUF_TYPE_TIME_EXTEND:
1144 event->len = DIV_ROUND_UP(RB_LEN_TIME_EXTEND, RB_ALIGNMENT);
1147 case RINGBUF_TYPE_TIME_STAMP:
1148 event->len = DIV_ROUND_UP(RB_LEN_TIME_STAMP, RB_ALIGNMENT);
1151 case RINGBUF_TYPE_DATA:
1152 length -= RB_EVNT_HDR_SIZE;
1153 if (length > RB_MAX_SMALL_DATA) {
1155 event->array[0] = length;
1157 event->len = DIV_ROUND_UP(length, RB_ALIGNMENT);
1164 static unsigned rb_calculate_event_length(unsigned length)
1166 struct ring_buffer_event event; /* Used only for sizeof array */
1168 /* zero length can cause confusions */
1172 if (length > RB_MAX_SMALL_DATA)
1173 length += sizeof(event.array[0]);
1175 length += RB_EVNT_HDR_SIZE;
1176 length = ALIGN(length, RB_ALIGNMENT);
1181 static struct ring_buffer_event *
1182 __rb_reserve_next(struct ring_buffer_per_cpu *cpu_buffer,
1183 unsigned type, unsigned long length, u64 *ts)
1185 struct buffer_page *tail_page, *head_page, *reader_page, *commit_page;
1186 unsigned long tail, write;
1187 struct ring_buffer *buffer = cpu_buffer->buffer;
1188 struct ring_buffer_event *event;
1189 unsigned long flags;
1190 bool lock_taken = false;
1192 commit_page = cpu_buffer->commit_page;
1193 /* we just need to protect against interrupts */
1195 tail_page = cpu_buffer->tail_page;
1196 write = local_add_return(length, &tail_page->write);
1197 tail = write - length;
1199 /* See if we shot pass the end of this buffer page */
1200 if (write > BUF_PAGE_SIZE) {
1201 struct buffer_page *next_page = tail_page;
1203 local_irq_save(flags);
1205 * Since the write to the buffer is still not
1206 * fully lockless, we must be careful with NMIs.
1207 * The locks in the writers are taken when a write
1208 * crosses to a new page. The locks protect against
1209 * races with the readers (this will soon be fixed
1210 * with a lockless solution).
1212 * Because we can not protect against NMIs, and we
1213 * want to keep traces reentrant, we need to manage
1214 * what happens when we are in an NMI.
1216 * NMIs can happen after we take the lock.
1217 * If we are in an NMI, only take the lock
1218 * if it is not already taken. Otherwise
1221 if (unlikely(in_nmi())) {
1222 if (!__raw_spin_trylock(&cpu_buffer->lock))
1225 __raw_spin_lock(&cpu_buffer->lock);
1229 rb_inc_page(cpu_buffer, &next_page);
1231 head_page = cpu_buffer->head_page;
1232 reader_page = cpu_buffer->reader_page;
1234 /* we grabbed the lock before incrementing */
1235 if (RB_WARN_ON(cpu_buffer, next_page == reader_page))
1239 * If for some reason, we had an interrupt storm that made
1240 * it all the way around the buffer, bail, and warn
1243 if (unlikely(next_page == commit_page)) {
1248 if (next_page == head_page) {
1249 if (!(buffer->flags & RB_FL_OVERWRITE))
1252 /* tail_page has not moved yet? */
1253 if (tail_page == cpu_buffer->tail_page) {
1254 /* count overflows */
1255 rb_update_overflow(cpu_buffer);
1257 rb_inc_page(cpu_buffer, &head_page);
1258 cpu_buffer->head_page = head_page;
1259 cpu_buffer->head_page->read = 0;
1264 * If the tail page is still the same as what we think
1265 * it is, then it is up to us to update the tail
1268 if (tail_page == cpu_buffer->tail_page) {
1269 local_set(&next_page->write, 0);
1270 local_set(&next_page->page->commit, 0);
1271 cpu_buffer->tail_page = next_page;
1273 /* reread the time stamp */
1274 *ts = ring_buffer_time_stamp(buffer, cpu_buffer->cpu);
1275 cpu_buffer->tail_page->page->time_stamp = *ts;
1279 * The actual tail page has moved forward.
1281 if (tail < BUF_PAGE_SIZE) {
1282 /* Mark the rest of the page with padding */
1283 event = __rb_page_index(tail_page, tail);
1284 rb_event_set_padding(event);
1287 if (tail <= BUF_PAGE_SIZE)
1288 /* Set the write back to the previous setting */
1289 local_set(&tail_page->write, tail);
1292 * If this was a commit entry that failed,
1293 * increment that too
1295 if (tail_page == cpu_buffer->commit_page &&
1296 tail == rb_commit_index(cpu_buffer)) {
1297 rb_set_commit_to_write(cpu_buffer);
1300 __raw_spin_unlock(&cpu_buffer->lock);
1301 local_irq_restore(flags);
1303 /* fail and let the caller try again */
1304 return ERR_PTR(-EAGAIN);
1307 /* We reserved something on the buffer */
1309 if (RB_WARN_ON(cpu_buffer, write > BUF_PAGE_SIZE))
1312 event = __rb_page_index(tail_page, tail);
1313 rb_update_event(event, type, length);
1316 * If this is a commit and the tail is zero, then update
1317 * this page's time stamp.
1319 if (!tail && rb_is_commit(cpu_buffer, event))
1320 cpu_buffer->commit_page->page->time_stamp = *ts;
1326 if (tail <= BUF_PAGE_SIZE)
1327 local_set(&tail_page->write, tail);
1329 if (likely(lock_taken))
1330 __raw_spin_unlock(&cpu_buffer->lock);
1331 local_irq_restore(flags);
1336 rb_add_time_stamp(struct ring_buffer_per_cpu *cpu_buffer,
1337 u64 *ts, u64 *delta)
1339 struct ring_buffer_event *event;
1343 if (unlikely(*delta > (1ULL << 59) && !once++)) {
1344 printk(KERN_WARNING "Delta way too big! %llu"
1345 " ts=%llu write stamp = %llu\n",
1346 (unsigned long long)*delta,
1347 (unsigned long long)*ts,
1348 (unsigned long long)cpu_buffer->write_stamp);
1353 * The delta is too big, we to add a
1356 event = __rb_reserve_next(cpu_buffer,
1357 RINGBUF_TYPE_TIME_EXTEND,
1363 if (PTR_ERR(event) == -EAGAIN)
1366 /* Only a commited time event can update the write stamp */
1367 if (rb_is_commit(cpu_buffer, event)) {
1369 * If this is the first on the page, then we need to
1370 * update the page itself, and just put in a zero.
1372 if (rb_event_index(event)) {
1373 event->time_delta = *delta & TS_MASK;
1374 event->array[0] = *delta >> TS_SHIFT;
1376 cpu_buffer->commit_page->page->time_stamp = *ts;
1377 event->time_delta = 0;
1378 event->array[0] = 0;
1380 cpu_buffer->write_stamp = *ts;
1381 /* let the caller know this was the commit */
1384 /* Darn, this is just wasted space */
1385 event->time_delta = 0;
1386 event->array[0] = 0;
1395 static struct ring_buffer_event *
1396 rb_reserve_next_event(struct ring_buffer_per_cpu *cpu_buffer,
1397 unsigned type, unsigned long length)
1399 struct ring_buffer_event *event;
1406 * We allow for interrupts to reenter here and do a trace.
1407 * If one does, it will cause this original code to loop
1408 * back here. Even with heavy interrupts happening, this
1409 * should only happen a few times in a row. If this happens
1410 * 1000 times in a row, there must be either an interrupt
1411 * storm or we have something buggy.
1414 if (RB_WARN_ON(cpu_buffer, ++nr_loops > 1000))
1417 ts = ring_buffer_time_stamp(cpu_buffer->buffer, cpu_buffer->cpu);
1420 * Only the first commit can update the timestamp.
1421 * Yes there is a race here. If an interrupt comes in
1422 * just after the conditional and it traces too, then it
1423 * will also check the deltas. More than one timestamp may
1424 * also be made. But only the entry that did the actual
1425 * commit will be something other than zero.
1427 if (cpu_buffer->tail_page == cpu_buffer->commit_page &&
1428 rb_page_write(cpu_buffer->tail_page) ==
1429 rb_commit_index(cpu_buffer)) {
1431 delta = ts - cpu_buffer->write_stamp;
1433 /* make sure this delta is calculated here */
1436 /* Did the write stamp get updated already? */
1437 if (unlikely(ts < cpu_buffer->write_stamp))
1440 if (test_time_stamp(delta)) {
1442 commit = rb_add_time_stamp(cpu_buffer, &ts, &delta);
1444 if (commit == -EBUSY)
1447 if (commit == -EAGAIN)
1450 RB_WARN_ON(cpu_buffer, commit < 0);
1453 /* Non commits have zero deltas */
1456 event = __rb_reserve_next(cpu_buffer, type, length, &ts);
1457 if (PTR_ERR(event) == -EAGAIN)
1461 if (unlikely(commit))
1463 * Ouch! We needed a timestamp and it was commited. But
1464 * we didn't get our event reserved.
1466 rb_set_commit_to_write(cpu_buffer);
1471 * If the timestamp was commited, make the commit our entry
1472 * now so that we will update it when needed.
1475 rb_set_commit_event(cpu_buffer, event);
1476 else if (!rb_is_commit(cpu_buffer, event))
1479 event->time_delta = delta;
1484 static int trace_irq_level(void)
1486 return (hardirq_count() >> HARDIRQ_SHIFT) +
1487 (softirq_count() >> + SOFTIRQ_SHIFT) +
1491 static int trace_recursive_lock(void)
1495 level = trace_irq_level();
1497 if (unlikely(current->trace_recursion & (1 << level))) {
1498 /* Disable all tracing before we do anything else */
1499 tracing_off_permanent();
1501 printk_once(KERN_WARNING "Tracing recursion: "
1502 "HC[%lu]:SC[%lu]:NMI[%lu]\n",
1503 hardirq_count() >> HARDIRQ_SHIFT,
1504 softirq_count() >> SOFTIRQ_SHIFT,
1511 current->trace_recursion |= 1 << level;
1516 static void trace_recursive_unlock(void)
1520 level = trace_irq_level();
1522 WARN_ON_ONCE(!current->trace_recursion & (1 << level));
1524 current->trace_recursion &= ~(1 << level);
1527 static DEFINE_PER_CPU(int, rb_need_resched);
1530 * ring_buffer_lock_reserve - reserve a part of the buffer
1531 * @buffer: the ring buffer to reserve from
1532 * @length: the length of the data to reserve (excluding event header)
1534 * Returns a reseverd event on the ring buffer to copy directly to.
1535 * The user of this interface will need to get the body to write into
1536 * and can use the ring_buffer_event_data() interface.
1538 * The length is the length of the data needed, not the event length
1539 * which also includes the event header.
1541 * Must be paired with ring_buffer_unlock_commit, unless NULL is returned.
1542 * If NULL is returned, then nothing has been allocated or locked.
1544 struct ring_buffer_event *
1545 ring_buffer_lock_reserve(struct ring_buffer *buffer, unsigned long length)
1547 struct ring_buffer_per_cpu *cpu_buffer;
1548 struct ring_buffer_event *event;
1551 if (ring_buffer_flags != RB_BUFFERS_ON)
1554 if (atomic_read(&buffer->record_disabled))
1557 /* If we are tracing schedule, we don't want to recurse */
1558 resched = ftrace_preempt_disable();
1560 if (trace_recursive_lock())
1563 cpu = raw_smp_processor_id();
1565 if (!cpumask_test_cpu(cpu, buffer->cpumask))
1568 cpu_buffer = buffer->buffers[cpu];
1570 if (atomic_read(&cpu_buffer->record_disabled))
1573 length = rb_calculate_event_length(length);
1574 if (length > BUF_PAGE_SIZE)
1577 event = rb_reserve_next_event(cpu_buffer, RINGBUF_TYPE_DATA, length);
1582 * Need to store resched state on this cpu.
1583 * Only the first needs to.
1586 if (preempt_count() == 1)
1587 per_cpu(rb_need_resched, cpu) = resched;
1592 trace_recursive_unlock();
1595 ftrace_preempt_enable(resched);
1598 EXPORT_SYMBOL_GPL(ring_buffer_lock_reserve);
1600 static void rb_commit(struct ring_buffer_per_cpu *cpu_buffer,
1601 struct ring_buffer_event *event)
1603 cpu_buffer->entries++;
1605 /* Only process further if we own the commit */
1606 if (!rb_is_commit(cpu_buffer, event))
1609 cpu_buffer->write_stamp += event->time_delta;
1611 rb_set_commit_to_write(cpu_buffer);
1615 * ring_buffer_unlock_commit - commit a reserved
1616 * @buffer: The buffer to commit to
1617 * @event: The event pointer to commit.
1619 * This commits the data to the ring buffer, and releases any locks held.
1621 * Must be paired with ring_buffer_lock_reserve.
1623 int ring_buffer_unlock_commit(struct ring_buffer *buffer,
1624 struct ring_buffer_event *event)
1626 struct ring_buffer_per_cpu *cpu_buffer;
1627 int cpu = raw_smp_processor_id();
1629 cpu_buffer = buffer->buffers[cpu];
1631 rb_commit(cpu_buffer, event);
1633 trace_recursive_unlock();
1636 * Only the last preempt count needs to restore preemption.
1638 if (preempt_count() == 1)
1639 ftrace_preempt_enable(per_cpu(rb_need_resched, cpu));
1641 preempt_enable_no_resched_notrace();
1645 EXPORT_SYMBOL_GPL(ring_buffer_unlock_commit);
1647 static inline void rb_event_discard(struct ring_buffer_event *event)
1649 event->type = RINGBUF_TYPE_PADDING;
1650 /* time delta must be non zero */
1651 if (!event->time_delta)
1652 event->time_delta = 1;
1656 * ring_buffer_event_discard - discard any event in the ring buffer
1657 * @event: the event to discard
1659 * Sometimes a event that is in the ring buffer needs to be ignored.
1660 * This function lets the user discard an event in the ring buffer
1661 * and then that event will not be read later.
1663 * Note, it is up to the user to be careful with this, and protect
1664 * against races. If the user discards an event that has been consumed
1665 * it is possible that it could corrupt the ring buffer.
1667 void ring_buffer_event_discard(struct ring_buffer_event *event)
1669 rb_event_discard(event);
1670 trace_recursive_unlock();
1672 EXPORT_SYMBOL_GPL(ring_buffer_event_discard);
1675 * ring_buffer_commit_discard - discard an event that has not been committed
1676 * @buffer: the ring buffer
1677 * @event: non committed event to discard
1679 * This is similar to ring_buffer_event_discard but must only be
1680 * performed on an event that has not been committed yet. The difference
1681 * is that this will also try to free the event from the ring buffer
1682 * if another event has not been added behind it.
1684 * If another event has been added behind it, it will set the event
1685 * up as discarded, and perform the commit.
1687 * If this function is called, do not call ring_buffer_unlock_commit on
1690 void ring_buffer_discard_commit(struct ring_buffer *buffer,
1691 struct ring_buffer_event *event)
1693 struct ring_buffer_per_cpu *cpu_buffer;
1694 unsigned long new_index, old_index;
1695 struct buffer_page *bpage;
1696 unsigned long index;
1700 /* The event is discarded regardless */
1701 rb_event_discard(event);
1704 * This must only be called if the event has not been
1705 * committed yet. Thus we can assume that preemption
1706 * is still disabled.
1708 RB_WARN_ON(buffer, !preempt_count());
1710 cpu = smp_processor_id();
1711 cpu_buffer = buffer->buffers[cpu];
1713 new_index = rb_event_index(event);
1714 old_index = new_index + rb_event_length(event);
1715 addr = (unsigned long)event;
1718 bpage = cpu_buffer->tail_page;
1720 if (bpage == (void *)addr && rb_page_write(bpage) == old_index) {
1722 * This is on the tail page. It is possible that
1723 * a write could come in and move the tail page
1724 * and write to the next page. That is fine
1725 * because we just shorten what is on this page.
1727 index = local_cmpxchg(&bpage->write, old_index, new_index);
1728 if (index == old_index)
1733 * The commit is still visible by the reader, so we
1734 * must increment entries.
1736 cpu_buffer->entries++;
1739 * If a write came in and pushed the tail page
1740 * we still need to update the commit pointer
1741 * if we were the commit.
1743 if (rb_is_commit(cpu_buffer, event))
1744 rb_set_commit_to_write(cpu_buffer);
1746 trace_recursive_unlock();
1749 * Only the last preempt count needs to restore preemption.
1751 if (preempt_count() == 1)
1752 ftrace_preempt_enable(per_cpu(rb_need_resched, cpu));
1754 preempt_enable_no_resched_notrace();
1757 EXPORT_SYMBOL_GPL(ring_buffer_discard_commit);
1760 * ring_buffer_write - write data to the buffer without reserving
1761 * @buffer: The ring buffer to write to.
1762 * @length: The length of the data being written (excluding the event header)
1763 * @data: The data to write to the buffer.
1765 * This is like ring_buffer_lock_reserve and ring_buffer_unlock_commit as
1766 * one function. If you already have the data to write to the buffer, it
1767 * may be easier to simply call this function.
1769 * Note, like ring_buffer_lock_reserve, the length is the length of the data
1770 * and not the length of the event which would hold the header.
1772 int ring_buffer_write(struct ring_buffer *buffer,
1773 unsigned long length,
1776 struct ring_buffer_per_cpu *cpu_buffer;
1777 struct ring_buffer_event *event;
1778 unsigned long event_length;
1783 if (ring_buffer_flags != RB_BUFFERS_ON)
1786 if (atomic_read(&buffer->record_disabled))
1789 resched = ftrace_preempt_disable();
1791 cpu = raw_smp_processor_id();
1793 if (!cpumask_test_cpu(cpu, buffer->cpumask))
1796 cpu_buffer = buffer->buffers[cpu];
1798 if (atomic_read(&cpu_buffer->record_disabled))
1801 event_length = rb_calculate_event_length(length);
1802 event = rb_reserve_next_event(cpu_buffer,
1803 RINGBUF_TYPE_DATA, event_length);
1807 body = rb_event_data(event);
1809 memcpy(body, data, length);
1811 rb_commit(cpu_buffer, event);
1815 ftrace_preempt_enable(resched);
1819 EXPORT_SYMBOL_GPL(ring_buffer_write);
1821 static int rb_per_cpu_empty(struct ring_buffer_per_cpu *cpu_buffer)
1823 struct buffer_page *reader = cpu_buffer->reader_page;
1824 struct buffer_page *head = cpu_buffer->head_page;
1825 struct buffer_page *commit = cpu_buffer->commit_page;
1827 return reader->read == rb_page_commit(reader) &&
1828 (commit == reader ||
1830 head->read == rb_page_commit(commit)));
1834 * ring_buffer_record_disable - stop all writes into the buffer
1835 * @buffer: The ring buffer to stop writes to.
1837 * This prevents all writes to the buffer. Any attempt to write
1838 * to the buffer after this will fail and return NULL.
1840 * The caller should call synchronize_sched() after this.
1842 void ring_buffer_record_disable(struct ring_buffer *buffer)
1844 atomic_inc(&buffer->record_disabled);
1846 EXPORT_SYMBOL_GPL(ring_buffer_record_disable);
1849 * ring_buffer_record_enable - enable writes to the buffer
1850 * @buffer: The ring buffer to enable writes
1852 * Note, multiple disables will need the same number of enables
1853 * to truely enable the writing (much like preempt_disable).
1855 void ring_buffer_record_enable(struct ring_buffer *buffer)
1857 atomic_dec(&buffer->record_disabled);
1859 EXPORT_SYMBOL_GPL(ring_buffer_record_enable);
1862 * ring_buffer_record_disable_cpu - stop all writes into the cpu_buffer
1863 * @buffer: The ring buffer to stop writes to.
1864 * @cpu: The CPU buffer to stop
1866 * This prevents all writes to the buffer. Any attempt to write
1867 * to the buffer after this will fail and return NULL.
1869 * The caller should call synchronize_sched() after this.
1871 void ring_buffer_record_disable_cpu(struct ring_buffer *buffer, int cpu)
1873 struct ring_buffer_per_cpu *cpu_buffer;
1875 if (!cpumask_test_cpu(cpu, buffer->cpumask))
1878 cpu_buffer = buffer->buffers[cpu];
1879 atomic_inc(&cpu_buffer->record_disabled);
1881 EXPORT_SYMBOL_GPL(ring_buffer_record_disable_cpu);
1884 * ring_buffer_record_enable_cpu - enable writes to the buffer
1885 * @buffer: The ring buffer to enable writes
1886 * @cpu: The CPU to enable.
1888 * Note, multiple disables will need the same number of enables
1889 * to truely enable the writing (much like preempt_disable).
1891 void ring_buffer_record_enable_cpu(struct ring_buffer *buffer, int cpu)
1893 struct ring_buffer_per_cpu *cpu_buffer;
1895 if (!cpumask_test_cpu(cpu, buffer->cpumask))
1898 cpu_buffer = buffer->buffers[cpu];
1899 atomic_dec(&cpu_buffer->record_disabled);
1901 EXPORT_SYMBOL_GPL(ring_buffer_record_enable_cpu);
1904 * ring_buffer_entries_cpu - get the number of entries in a cpu buffer
1905 * @buffer: The ring buffer
1906 * @cpu: The per CPU buffer to get the entries from.
1908 unsigned long ring_buffer_entries_cpu(struct ring_buffer *buffer, int cpu)
1910 struct ring_buffer_per_cpu *cpu_buffer;
1913 if (!cpumask_test_cpu(cpu, buffer->cpumask))
1916 cpu_buffer = buffer->buffers[cpu];
1917 ret = cpu_buffer->entries;
1921 EXPORT_SYMBOL_GPL(ring_buffer_entries_cpu);
1924 * ring_buffer_overrun_cpu - get the number of overruns in a cpu_buffer
1925 * @buffer: The ring buffer
1926 * @cpu: The per CPU buffer to get the number of overruns from
1928 unsigned long ring_buffer_overrun_cpu(struct ring_buffer *buffer, int cpu)
1930 struct ring_buffer_per_cpu *cpu_buffer;
1933 if (!cpumask_test_cpu(cpu, buffer->cpumask))
1936 cpu_buffer = buffer->buffers[cpu];
1937 ret = cpu_buffer->overrun;
1941 EXPORT_SYMBOL_GPL(ring_buffer_overrun_cpu);
1944 * ring_buffer_entries - get the number of entries in a buffer
1945 * @buffer: The ring buffer
1947 * Returns the total number of entries in the ring buffer
1950 unsigned long ring_buffer_entries(struct ring_buffer *buffer)
1952 struct ring_buffer_per_cpu *cpu_buffer;
1953 unsigned long entries = 0;
1956 /* if you care about this being correct, lock the buffer */
1957 for_each_buffer_cpu(buffer, cpu) {
1958 cpu_buffer = buffer->buffers[cpu];
1959 entries += cpu_buffer->entries;
1964 EXPORT_SYMBOL_GPL(ring_buffer_entries);
1967 * ring_buffer_overrun_cpu - get the number of overruns in buffer
1968 * @buffer: The ring buffer
1970 * Returns the total number of overruns in the ring buffer
1973 unsigned long ring_buffer_overruns(struct ring_buffer *buffer)
1975 struct ring_buffer_per_cpu *cpu_buffer;
1976 unsigned long overruns = 0;
1979 /* if you care about this being correct, lock the buffer */
1980 for_each_buffer_cpu(buffer, cpu) {
1981 cpu_buffer = buffer->buffers[cpu];
1982 overruns += cpu_buffer->overrun;
1987 EXPORT_SYMBOL_GPL(ring_buffer_overruns);
1989 static void rb_iter_reset(struct ring_buffer_iter *iter)
1991 struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
1993 /* Iterator usage is expected to have record disabled */
1994 if (list_empty(&cpu_buffer->reader_page->list)) {
1995 iter->head_page = cpu_buffer->head_page;
1996 iter->head = cpu_buffer->head_page->read;
1998 iter->head_page = cpu_buffer->reader_page;
1999 iter->head = cpu_buffer->reader_page->read;
2002 iter->read_stamp = cpu_buffer->read_stamp;
2004 iter->read_stamp = iter->head_page->page->time_stamp;
2008 * ring_buffer_iter_reset - reset an iterator
2009 * @iter: The iterator to reset
2011 * Resets the iterator, so that it will start from the beginning
2014 void ring_buffer_iter_reset(struct ring_buffer_iter *iter)
2016 struct ring_buffer_per_cpu *cpu_buffer;
2017 unsigned long flags;
2022 cpu_buffer = iter->cpu_buffer;
2024 spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
2025 rb_iter_reset(iter);
2026 spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
2028 EXPORT_SYMBOL_GPL(ring_buffer_iter_reset);
2031 * ring_buffer_iter_empty - check if an iterator has no more to read
2032 * @iter: The iterator to check
2034 int ring_buffer_iter_empty(struct ring_buffer_iter *iter)
2036 struct ring_buffer_per_cpu *cpu_buffer;
2038 cpu_buffer = iter->cpu_buffer;
2040 return iter->head_page == cpu_buffer->commit_page &&
2041 iter->head == rb_commit_index(cpu_buffer);
2043 EXPORT_SYMBOL_GPL(ring_buffer_iter_empty);
2046 rb_update_read_stamp(struct ring_buffer_per_cpu *cpu_buffer,
2047 struct ring_buffer_event *event)
2051 switch (event->type) {
2052 case RINGBUF_TYPE_PADDING:
2055 case RINGBUF_TYPE_TIME_EXTEND:
2056 delta = event->array[0];
2058 delta += event->time_delta;
2059 cpu_buffer->read_stamp += delta;
2062 case RINGBUF_TYPE_TIME_STAMP:
2063 /* FIXME: not implemented */
2066 case RINGBUF_TYPE_DATA:
2067 cpu_buffer->read_stamp += event->time_delta;
2077 rb_update_iter_read_stamp(struct ring_buffer_iter *iter,
2078 struct ring_buffer_event *event)
2082 switch (event->type) {
2083 case RINGBUF_TYPE_PADDING:
2086 case RINGBUF_TYPE_TIME_EXTEND:
2087 delta = event->array[0];
2089 delta += event->time_delta;
2090 iter->read_stamp += delta;
2093 case RINGBUF_TYPE_TIME_STAMP:
2094 /* FIXME: not implemented */
2097 case RINGBUF_TYPE_DATA:
2098 iter->read_stamp += event->time_delta;
2107 static struct buffer_page *
2108 rb_get_reader_page(struct ring_buffer_per_cpu *cpu_buffer)
2110 struct buffer_page *reader = NULL;
2111 unsigned long flags;
2114 local_irq_save(flags);
2115 __raw_spin_lock(&cpu_buffer->lock);
2119 * This should normally only loop twice. But because the
2120 * start of the reader inserts an empty page, it causes
2121 * a case where we will loop three times. There should be no
2122 * reason to loop four times (that I know of).
2124 if (RB_WARN_ON(cpu_buffer, ++nr_loops > 3)) {
2129 reader = cpu_buffer->reader_page;
2131 /* If there's more to read, return this page */
2132 if (cpu_buffer->reader_page->read < rb_page_size(reader))
2135 /* Never should we have an index greater than the size */
2136 if (RB_WARN_ON(cpu_buffer,
2137 cpu_buffer->reader_page->read > rb_page_size(reader)))
2140 /* check if we caught up to the tail */
2142 if (cpu_buffer->commit_page == cpu_buffer->reader_page)
2146 * Splice the empty reader page into the list around the head.
2147 * Reset the reader page to size zero.
2150 reader = cpu_buffer->head_page;
2151 cpu_buffer->reader_page->list.next = reader->list.next;
2152 cpu_buffer->reader_page->list.prev = reader->list.prev;
2154 local_set(&cpu_buffer->reader_page->write, 0);
2155 local_set(&cpu_buffer->reader_page->page->commit, 0);
2157 /* Make the reader page now replace the head */
2158 reader->list.prev->next = &cpu_buffer->reader_page->list;
2159 reader->list.next->prev = &cpu_buffer->reader_page->list;
2162 * If the tail is on the reader, then we must set the head
2163 * to the inserted page, otherwise we set it one before.
2165 cpu_buffer->head_page = cpu_buffer->reader_page;
2167 if (cpu_buffer->commit_page != reader)
2168 rb_inc_page(cpu_buffer, &cpu_buffer->head_page);
2170 /* Finally update the reader page to the new head */
2171 cpu_buffer->reader_page = reader;
2172 rb_reset_reader_page(cpu_buffer);
2177 __raw_spin_unlock(&cpu_buffer->lock);
2178 local_irq_restore(flags);
2183 static void rb_advance_reader(struct ring_buffer_per_cpu *cpu_buffer)
2185 struct ring_buffer_event *event;
2186 struct buffer_page *reader;
2189 reader = rb_get_reader_page(cpu_buffer);
2191 /* This function should not be called when buffer is empty */
2192 if (RB_WARN_ON(cpu_buffer, !reader))
2195 event = rb_reader_event(cpu_buffer);
2197 if (event->type == RINGBUF_TYPE_DATA || rb_discarded_event(event))
2198 cpu_buffer->entries--;
2200 rb_update_read_stamp(cpu_buffer, event);
2202 length = rb_event_length(event);
2203 cpu_buffer->reader_page->read += length;
2206 static void rb_advance_iter(struct ring_buffer_iter *iter)
2208 struct ring_buffer *buffer;
2209 struct ring_buffer_per_cpu *cpu_buffer;
2210 struct ring_buffer_event *event;
2213 cpu_buffer = iter->cpu_buffer;
2214 buffer = cpu_buffer->buffer;
2217 * Check if we are at the end of the buffer.
2219 if (iter->head >= rb_page_size(iter->head_page)) {
2220 if (RB_WARN_ON(buffer,
2221 iter->head_page == cpu_buffer->commit_page))
2227 event = rb_iter_head_event(iter);
2229 length = rb_event_length(event);
2232 * This should not be called to advance the header if we are
2233 * at the tail of the buffer.
2235 if (RB_WARN_ON(cpu_buffer,
2236 (iter->head_page == cpu_buffer->commit_page) &&
2237 (iter->head + length > rb_commit_index(cpu_buffer))))
2240 rb_update_iter_read_stamp(iter, event);
2242 iter->head += length;
2244 /* check for end of page padding */
2245 if ((iter->head >= rb_page_size(iter->head_page)) &&
2246 (iter->head_page != cpu_buffer->commit_page))
2247 rb_advance_iter(iter);
2250 static struct ring_buffer_event *
2251 rb_buffer_peek(struct ring_buffer *buffer, int cpu, u64 *ts)
2253 struct ring_buffer_per_cpu *cpu_buffer;
2254 struct ring_buffer_event *event;
2255 struct buffer_page *reader;
2258 cpu_buffer = buffer->buffers[cpu];
2262 * We repeat when a timestamp is encountered. It is possible
2263 * to get multiple timestamps from an interrupt entering just
2264 * as one timestamp is about to be written. The max times
2265 * that this can happen is the number of nested interrupts we
2266 * can have. Nesting 10 deep of interrupts is clearly
2269 if (RB_WARN_ON(cpu_buffer, ++nr_loops > 10))
2272 reader = rb_get_reader_page(cpu_buffer);
2276 event = rb_reader_event(cpu_buffer);
2278 switch (event->type) {
2279 case RINGBUF_TYPE_PADDING:
2280 if (rb_null_event(event))
2281 RB_WARN_ON(cpu_buffer, 1);
2283 * Because the writer could be discarding every
2284 * event it creates (which would probably be bad)
2285 * if we were to go back to "again" then we may never
2286 * catch up, and will trigger the warn on, or lock
2287 * the box. Return the padding, and we will release
2288 * the current locks, and try again.
2290 rb_advance_reader(cpu_buffer);
2293 case RINGBUF_TYPE_TIME_EXTEND:
2294 /* Internal data, OK to advance */
2295 rb_advance_reader(cpu_buffer);
2298 case RINGBUF_TYPE_TIME_STAMP:
2299 /* FIXME: not implemented */
2300 rb_advance_reader(cpu_buffer);
2303 case RINGBUF_TYPE_DATA:
2305 *ts = cpu_buffer->read_stamp + event->time_delta;
2306 ring_buffer_normalize_time_stamp(buffer,
2307 cpu_buffer->cpu, ts);
2317 EXPORT_SYMBOL_GPL(ring_buffer_peek);
2319 static struct ring_buffer_event *
2320 rb_iter_peek(struct ring_buffer_iter *iter, u64 *ts)
2322 struct ring_buffer *buffer;
2323 struct ring_buffer_per_cpu *cpu_buffer;
2324 struct ring_buffer_event *event;
2327 if (ring_buffer_iter_empty(iter))
2330 cpu_buffer = iter->cpu_buffer;
2331 buffer = cpu_buffer->buffer;
2335 * We repeat when a timestamp is encountered. It is possible
2336 * to get multiple timestamps from an interrupt entering just
2337 * as one timestamp is about to be written. The max times
2338 * that this can happen is the number of nested interrupts we
2339 * can have. Nesting 10 deep of interrupts is clearly
2342 if (RB_WARN_ON(cpu_buffer, ++nr_loops > 10))
2345 if (rb_per_cpu_empty(cpu_buffer))
2348 event = rb_iter_head_event(iter);
2350 switch (event->type) {
2351 case RINGBUF_TYPE_PADDING:
2352 if (rb_null_event(event)) {
2356 rb_advance_iter(iter);
2359 case RINGBUF_TYPE_TIME_EXTEND:
2360 /* Internal data, OK to advance */
2361 rb_advance_iter(iter);
2364 case RINGBUF_TYPE_TIME_STAMP:
2365 /* FIXME: not implemented */
2366 rb_advance_iter(iter);
2369 case RINGBUF_TYPE_DATA:
2371 *ts = iter->read_stamp + event->time_delta;
2372 ring_buffer_normalize_time_stamp(buffer,
2373 cpu_buffer->cpu, ts);
2383 EXPORT_SYMBOL_GPL(ring_buffer_iter_peek);
2386 * ring_buffer_peek - peek at the next event to be read
2387 * @buffer: The ring buffer to read
2388 * @cpu: The cpu to peak at
2389 * @ts: The timestamp counter of this event.
2391 * This will return the event that will be read next, but does
2392 * not consume the data.
2394 struct ring_buffer_event *
2395 ring_buffer_peek(struct ring_buffer *buffer, int cpu, u64 *ts)
2397 struct ring_buffer_per_cpu *cpu_buffer = buffer->buffers[cpu];
2398 struct ring_buffer_event *event;
2399 unsigned long flags;
2401 if (!cpumask_test_cpu(cpu, buffer->cpumask))
2405 spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
2406 event = rb_buffer_peek(buffer, cpu, ts);
2407 spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
2409 if (event && event->type == RINGBUF_TYPE_PADDING) {
2418 * ring_buffer_iter_peek - peek at the next event to be read
2419 * @iter: The ring buffer iterator
2420 * @ts: The timestamp counter of this event.
2422 * This will return the event that will be read next, but does
2423 * not increment the iterator.
2425 struct ring_buffer_event *
2426 ring_buffer_iter_peek(struct ring_buffer_iter *iter, u64 *ts)
2428 struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
2429 struct ring_buffer_event *event;
2430 unsigned long flags;
2433 spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
2434 event = rb_iter_peek(iter, ts);
2435 spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
2437 if (event && event->type == RINGBUF_TYPE_PADDING) {
2446 * ring_buffer_consume - return an event and consume it
2447 * @buffer: The ring buffer to get the next event from
2449 * Returns the next event in the ring buffer, and that event is consumed.
2450 * Meaning, that sequential reads will keep returning a different event,
2451 * and eventually empty the ring buffer if the producer is slower.
2453 struct ring_buffer_event *
2454 ring_buffer_consume(struct ring_buffer *buffer, int cpu, u64 *ts)
2456 struct ring_buffer_per_cpu *cpu_buffer;
2457 struct ring_buffer_event *event = NULL;
2458 unsigned long flags;
2461 /* might be called in atomic */
2464 if (!cpumask_test_cpu(cpu, buffer->cpumask))
2467 cpu_buffer = buffer->buffers[cpu];
2468 spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
2470 event = rb_buffer_peek(buffer, cpu, ts);
2474 rb_advance_reader(cpu_buffer);
2477 spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
2482 if (event && event->type == RINGBUF_TYPE_PADDING) {
2489 EXPORT_SYMBOL_GPL(ring_buffer_consume);
2492 * ring_buffer_read_start - start a non consuming read of the buffer
2493 * @buffer: The ring buffer to read from
2494 * @cpu: The cpu buffer to iterate over
2496 * This starts up an iteration through the buffer. It also disables
2497 * the recording to the buffer until the reading is finished.
2498 * This prevents the reading from being corrupted. This is not
2499 * a consuming read, so a producer is not expected.
2501 * Must be paired with ring_buffer_finish.
2503 struct ring_buffer_iter *
2504 ring_buffer_read_start(struct ring_buffer *buffer, int cpu)
2506 struct ring_buffer_per_cpu *cpu_buffer;
2507 struct ring_buffer_iter *iter;
2508 unsigned long flags;
2510 if (!cpumask_test_cpu(cpu, buffer->cpumask))
2513 iter = kmalloc(sizeof(*iter), GFP_KERNEL);
2517 cpu_buffer = buffer->buffers[cpu];
2519 iter->cpu_buffer = cpu_buffer;
2521 atomic_inc(&cpu_buffer->record_disabled);
2522 synchronize_sched();
2524 spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
2525 __raw_spin_lock(&cpu_buffer->lock);
2526 rb_iter_reset(iter);
2527 __raw_spin_unlock(&cpu_buffer->lock);
2528 spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
2532 EXPORT_SYMBOL_GPL(ring_buffer_read_start);
2535 * ring_buffer_finish - finish reading the iterator of the buffer
2536 * @iter: The iterator retrieved by ring_buffer_start
2538 * This re-enables the recording to the buffer, and frees the
2542 ring_buffer_read_finish(struct ring_buffer_iter *iter)
2544 struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
2546 atomic_dec(&cpu_buffer->record_disabled);
2549 EXPORT_SYMBOL_GPL(ring_buffer_read_finish);
2552 * ring_buffer_read - read the next item in the ring buffer by the iterator
2553 * @iter: The ring buffer iterator
2554 * @ts: The time stamp of the event read.
2556 * This reads the next event in the ring buffer and increments the iterator.
2558 struct ring_buffer_event *
2559 ring_buffer_read(struct ring_buffer_iter *iter, u64 *ts)
2561 struct ring_buffer_event *event;
2562 struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
2563 unsigned long flags;
2566 spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
2567 event = rb_iter_peek(iter, ts);
2571 rb_advance_iter(iter);
2573 spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
2575 if (event && event->type == RINGBUF_TYPE_PADDING) {
2582 EXPORT_SYMBOL_GPL(ring_buffer_read);
2585 * ring_buffer_size - return the size of the ring buffer (in bytes)
2586 * @buffer: The ring buffer.
2588 unsigned long ring_buffer_size(struct ring_buffer *buffer)
2590 return BUF_PAGE_SIZE * buffer->pages;
2592 EXPORT_SYMBOL_GPL(ring_buffer_size);
2595 rb_reset_cpu(struct ring_buffer_per_cpu *cpu_buffer)
2597 cpu_buffer->head_page
2598 = list_entry(cpu_buffer->pages.next, struct buffer_page, list);
2599 local_set(&cpu_buffer->head_page->write, 0);
2600 local_set(&cpu_buffer->head_page->page->commit, 0);
2602 cpu_buffer->head_page->read = 0;
2604 cpu_buffer->tail_page = cpu_buffer->head_page;
2605 cpu_buffer->commit_page = cpu_buffer->head_page;
2607 INIT_LIST_HEAD(&cpu_buffer->reader_page->list);
2608 local_set(&cpu_buffer->reader_page->write, 0);
2609 local_set(&cpu_buffer->reader_page->page->commit, 0);
2610 cpu_buffer->reader_page->read = 0;
2612 cpu_buffer->overrun = 0;
2613 cpu_buffer->entries = 0;
2615 cpu_buffer->write_stamp = 0;
2616 cpu_buffer->read_stamp = 0;
2620 * ring_buffer_reset_cpu - reset a ring buffer per CPU buffer
2621 * @buffer: The ring buffer to reset a per cpu buffer of
2622 * @cpu: The CPU buffer to be reset
2624 void ring_buffer_reset_cpu(struct ring_buffer *buffer, int cpu)
2626 struct ring_buffer_per_cpu *cpu_buffer = buffer->buffers[cpu];
2627 unsigned long flags;
2629 if (!cpumask_test_cpu(cpu, buffer->cpumask))
2632 spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
2634 __raw_spin_lock(&cpu_buffer->lock);
2636 rb_reset_cpu(cpu_buffer);
2638 __raw_spin_unlock(&cpu_buffer->lock);
2640 spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
2642 EXPORT_SYMBOL_GPL(ring_buffer_reset_cpu);
2645 * ring_buffer_reset - reset a ring buffer
2646 * @buffer: The ring buffer to reset all cpu buffers
2648 void ring_buffer_reset(struct ring_buffer *buffer)
2652 for_each_buffer_cpu(buffer, cpu)
2653 ring_buffer_reset_cpu(buffer, cpu);
2655 EXPORT_SYMBOL_GPL(ring_buffer_reset);
2658 * rind_buffer_empty - is the ring buffer empty?
2659 * @buffer: The ring buffer to test
2661 int ring_buffer_empty(struct ring_buffer *buffer)
2663 struct ring_buffer_per_cpu *cpu_buffer;
2666 /* yes this is racy, but if you don't like the race, lock the buffer */
2667 for_each_buffer_cpu(buffer, cpu) {
2668 cpu_buffer = buffer->buffers[cpu];
2669 if (!rb_per_cpu_empty(cpu_buffer))
2675 EXPORT_SYMBOL_GPL(ring_buffer_empty);
2678 * ring_buffer_empty_cpu - is a cpu buffer of a ring buffer empty?
2679 * @buffer: The ring buffer
2680 * @cpu: The CPU buffer to test
2682 int ring_buffer_empty_cpu(struct ring_buffer *buffer, int cpu)
2684 struct ring_buffer_per_cpu *cpu_buffer;
2687 if (!cpumask_test_cpu(cpu, buffer->cpumask))
2690 cpu_buffer = buffer->buffers[cpu];
2691 ret = rb_per_cpu_empty(cpu_buffer);
2696 EXPORT_SYMBOL_GPL(ring_buffer_empty_cpu);
2699 * ring_buffer_swap_cpu - swap a CPU buffer between two ring buffers
2700 * @buffer_a: One buffer to swap with
2701 * @buffer_b: The other buffer to swap with
2703 * This function is useful for tracers that want to take a "snapshot"
2704 * of a CPU buffer and has another back up buffer lying around.
2705 * it is expected that the tracer handles the cpu buffer not being
2706 * used at the moment.
2708 int ring_buffer_swap_cpu(struct ring_buffer *buffer_a,
2709 struct ring_buffer *buffer_b, int cpu)
2711 struct ring_buffer_per_cpu *cpu_buffer_a;
2712 struct ring_buffer_per_cpu *cpu_buffer_b;
2715 if (!cpumask_test_cpu(cpu, buffer_a->cpumask) ||
2716 !cpumask_test_cpu(cpu, buffer_b->cpumask))
2719 /* At least make sure the two buffers are somewhat the same */
2720 if (buffer_a->pages != buffer_b->pages)
2725 if (ring_buffer_flags != RB_BUFFERS_ON)
2728 if (atomic_read(&buffer_a->record_disabled))
2731 if (atomic_read(&buffer_b->record_disabled))
2734 cpu_buffer_a = buffer_a->buffers[cpu];
2735 cpu_buffer_b = buffer_b->buffers[cpu];
2737 if (atomic_read(&cpu_buffer_a->record_disabled))
2740 if (atomic_read(&cpu_buffer_b->record_disabled))
2744 * We can't do a synchronize_sched here because this
2745 * function can be called in atomic context.
2746 * Normally this will be called from the same CPU as cpu.
2747 * If not it's up to the caller to protect this.
2749 atomic_inc(&cpu_buffer_a->record_disabled);
2750 atomic_inc(&cpu_buffer_b->record_disabled);
2752 buffer_a->buffers[cpu] = cpu_buffer_b;
2753 buffer_b->buffers[cpu] = cpu_buffer_a;
2755 cpu_buffer_b->buffer = buffer_a;
2756 cpu_buffer_a->buffer = buffer_b;
2758 atomic_dec(&cpu_buffer_a->record_disabled);
2759 atomic_dec(&cpu_buffer_b->record_disabled);
2765 EXPORT_SYMBOL_GPL(ring_buffer_swap_cpu);
2767 static void rb_remove_entries(struct ring_buffer_per_cpu *cpu_buffer,
2768 struct buffer_data_page *bpage,
2769 unsigned int offset)
2771 struct ring_buffer_event *event;
2774 __raw_spin_lock(&cpu_buffer->lock);
2775 for (head = offset; head < local_read(&bpage->commit);
2776 head += rb_event_length(event)) {
2778 event = __rb_data_page_index(bpage, head);
2779 if (RB_WARN_ON(cpu_buffer, rb_null_event(event)))
2781 /* Only count data entries */
2782 if (event->type != RINGBUF_TYPE_DATA)
2784 cpu_buffer->entries--;
2786 __raw_spin_unlock(&cpu_buffer->lock);
2790 * ring_buffer_alloc_read_page - allocate a page to read from buffer
2791 * @buffer: the buffer to allocate for.
2793 * This function is used in conjunction with ring_buffer_read_page.
2794 * When reading a full page from the ring buffer, these functions
2795 * can be used to speed up the process. The calling function should
2796 * allocate a few pages first with this function. Then when it
2797 * needs to get pages from the ring buffer, it passes the result
2798 * of this function into ring_buffer_read_page, which will swap
2799 * the page that was allocated, with the read page of the buffer.
2802 * The page allocated, or NULL on error.
2804 void *ring_buffer_alloc_read_page(struct ring_buffer *buffer)
2806 struct buffer_data_page *bpage;
2809 addr = __get_free_page(GFP_KERNEL);
2813 bpage = (void *)addr;
2815 rb_init_page(bpage);
2821 * ring_buffer_free_read_page - free an allocated read page
2822 * @buffer: the buffer the page was allocate for
2823 * @data: the page to free
2825 * Free a page allocated from ring_buffer_alloc_read_page.
2827 void ring_buffer_free_read_page(struct ring_buffer *buffer, void *data)
2829 free_page((unsigned long)data);
2833 * ring_buffer_read_page - extract a page from the ring buffer
2834 * @buffer: buffer to extract from
2835 * @data_page: the page to use allocated from ring_buffer_alloc_read_page
2836 * @len: amount to extract
2837 * @cpu: the cpu of the buffer to extract
2838 * @full: should the extraction only happen when the page is full.
2840 * This function will pull out a page from the ring buffer and consume it.
2841 * @data_page must be the address of the variable that was returned
2842 * from ring_buffer_alloc_read_page. This is because the page might be used
2843 * to swap with a page in the ring buffer.
2846 * rpage = ring_buffer_alloc_read_page(buffer);
2849 * ret = ring_buffer_read_page(buffer, &rpage, len, cpu, 0);
2851 * process_page(rpage, ret);
2853 * When @full is set, the function will not return true unless
2854 * the writer is off the reader page.
2856 * Note: it is up to the calling functions to handle sleeps and wakeups.
2857 * The ring buffer can be used anywhere in the kernel and can not
2858 * blindly call wake_up. The layer that uses the ring buffer must be
2859 * responsible for that.
2862 * >=0 if data has been transferred, returns the offset of consumed data.
2863 * <0 if no data has been transferred.
2865 int ring_buffer_read_page(struct ring_buffer *buffer,
2866 void **data_page, size_t len, int cpu, int full)
2868 struct ring_buffer_per_cpu *cpu_buffer = buffer->buffers[cpu];
2869 struct ring_buffer_event *event;
2870 struct buffer_data_page *bpage;
2871 struct buffer_page *reader;
2872 unsigned long flags;
2873 unsigned int commit;
2878 if (!cpumask_test_cpu(cpu, buffer->cpumask))
2882 * If len is not big enough to hold the page header, then
2883 * we can not copy anything.
2885 if (len <= BUF_PAGE_HDR_SIZE)
2888 len -= BUF_PAGE_HDR_SIZE;
2897 spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
2899 reader = rb_get_reader_page(cpu_buffer);
2903 event = rb_reader_event(cpu_buffer);
2905 read = reader->read;
2906 commit = rb_page_commit(reader);
2909 * If this page has been partially read or
2910 * if len is not big enough to read the rest of the page or
2911 * a writer is still on the page, then
2912 * we must copy the data from the page to the buffer.
2913 * Otherwise, we can simply swap the page with the one passed in.
2915 if (read || (len < (commit - read)) ||
2916 cpu_buffer->reader_page == cpu_buffer->commit_page) {
2917 struct buffer_data_page *rpage = cpu_buffer->reader_page->page;
2918 unsigned int rpos = read;
2919 unsigned int pos = 0;
2925 if (len > (commit - read))
2926 len = (commit - read);
2928 size = rb_event_length(event);
2933 /* save the current timestamp, since the user will need it */
2934 save_timestamp = cpu_buffer->read_stamp;
2936 /* Need to copy one event at a time */
2938 memcpy(bpage->data + pos, rpage->data + rpos, size);
2942 rb_advance_reader(cpu_buffer);
2943 rpos = reader->read;
2946 event = rb_reader_event(cpu_buffer);
2947 size = rb_event_length(event);
2948 } while (len > size);
2951 local_set(&bpage->commit, pos);
2952 bpage->time_stamp = save_timestamp;
2954 /* we copied everything to the beginning */
2957 /* swap the pages */
2958 rb_init_page(bpage);
2959 bpage = reader->page;
2960 reader->page = *data_page;
2961 local_set(&reader->write, 0);
2965 /* update the entry counter */
2966 rb_remove_entries(cpu_buffer, bpage, read);
2971 spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
2978 rb_simple_read(struct file *filp, char __user *ubuf,
2979 size_t cnt, loff_t *ppos)
2981 unsigned long *p = filp->private_data;
2985 if (test_bit(RB_BUFFERS_DISABLED_BIT, p))
2986 r = sprintf(buf, "permanently disabled\n");
2988 r = sprintf(buf, "%d\n", test_bit(RB_BUFFERS_ON_BIT, p));
2990 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2994 rb_simple_write(struct file *filp, const char __user *ubuf,
2995 size_t cnt, loff_t *ppos)
2997 unsigned long *p = filp->private_data;
3002 if (cnt >= sizeof(buf))
3005 if (copy_from_user(&buf, ubuf, cnt))
3010 ret = strict_strtoul(buf, 10, &val);
3015 set_bit(RB_BUFFERS_ON_BIT, p);
3017 clear_bit(RB_BUFFERS_ON_BIT, p);
3024 static const struct file_operations rb_simple_fops = {
3025 .open = tracing_open_generic,
3026 .read = rb_simple_read,
3027 .write = rb_simple_write,
3031 static __init int rb_init_debugfs(void)
3033 struct dentry *d_tracer;
3035 d_tracer = tracing_init_dentry();
3037 trace_create_file("tracing_on", 0644, d_tracer,
3038 &ring_buffer_flags, &rb_simple_fops);
3043 fs_initcall(rb_init_debugfs);
3045 #ifdef CONFIG_HOTPLUG_CPU
3046 static int rb_cpu_notify(struct notifier_block *self,
3047 unsigned long action, void *hcpu)
3049 struct ring_buffer *buffer =
3050 container_of(self, struct ring_buffer, cpu_notify);
3051 long cpu = (long)hcpu;
3054 case CPU_UP_PREPARE:
3055 case CPU_UP_PREPARE_FROZEN:
3056 if (cpu_isset(cpu, *buffer->cpumask))
3059 buffer->buffers[cpu] =
3060 rb_allocate_cpu_buffer(buffer, cpu);
3061 if (!buffer->buffers[cpu]) {
3062 WARN(1, "failed to allocate ring buffer on CPU %ld\n",
3067 cpu_set(cpu, *buffer->cpumask);
3069 case CPU_DOWN_PREPARE:
3070 case CPU_DOWN_PREPARE_FROZEN:
3073 * If we were to free the buffer, then the user would
3074 * lose any trace that was in the buffer.