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, "# compressed entry header\n");
32 ret = trace_seq_printf(s, "\ttype_len : 5 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 max type_len == %d\n",
41 RINGBUF_TYPE_DATA_TYPE_LEN_MAX);
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 (RB_ALIGNMENT * RINGBUF_TYPE_DATA_TYPE_LEN_MAX)
209 /* define RINGBUF_TYPE_DATA for 'case RINGBUF_TYPE_DATA:' */
210 #define RINGBUF_TYPE_DATA 0 ... RINGBUF_TYPE_DATA_TYPE_LEN_MAX
213 RB_LEN_TIME_EXTEND = 8,
214 RB_LEN_TIME_STAMP = 16,
217 static inline int rb_null_event(struct ring_buffer_event *event)
219 return event->type_len == RINGBUF_TYPE_PADDING
220 && event->time_delta == 0;
223 static inline int rb_discarded_event(struct ring_buffer_event *event)
225 return event->type_len == RINGBUF_TYPE_PADDING && event->time_delta;
228 static void rb_event_set_padding(struct ring_buffer_event *event)
230 event->type_len = RINGBUF_TYPE_PADDING;
231 event->time_delta = 0;
235 rb_event_data_length(struct ring_buffer_event *event)
240 length = event->type_len * RB_ALIGNMENT;
242 length = event->array[0];
243 return length + RB_EVNT_HDR_SIZE;
246 /* inline for ring buffer fast paths */
248 rb_event_length(struct ring_buffer_event *event)
250 switch (event->type_len) {
251 case RINGBUF_TYPE_PADDING:
252 if (rb_null_event(event))
255 return event->array[0] + RB_EVNT_HDR_SIZE;
257 case RINGBUF_TYPE_TIME_EXTEND:
258 return RB_LEN_TIME_EXTEND;
260 case RINGBUF_TYPE_TIME_STAMP:
261 return RB_LEN_TIME_STAMP;
263 case RINGBUF_TYPE_DATA:
264 return rb_event_data_length(event);
273 * ring_buffer_event_length - return the length of the event
274 * @event: the event to get the length of
276 unsigned ring_buffer_event_length(struct ring_buffer_event *event)
278 unsigned length = rb_event_length(event);
279 if (event->type_len > RINGBUF_TYPE_DATA_TYPE_LEN_MAX)
281 length -= RB_EVNT_HDR_SIZE;
282 if (length > RB_MAX_SMALL_DATA + sizeof(event->array[0]))
283 length -= sizeof(event->array[0]);
286 EXPORT_SYMBOL_GPL(ring_buffer_event_length);
288 /* inline for ring buffer fast paths */
290 rb_event_data(struct ring_buffer_event *event)
292 BUG_ON(event->type_len > RINGBUF_TYPE_DATA_TYPE_LEN_MAX);
293 /* If length is in len field, then array[0] has the data */
295 return (void *)&event->array[0];
296 /* Otherwise length is in array[0] and array[1] has the data */
297 return (void *)&event->array[1];
301 * ring_buffer_event_data - return the data of the event
302 * @event: the event to get the data from
304 void *ring_buffer_event_data(struct ring_buffer_event *event)
306 return rb_event_data(event);
308 EXPORT_SYMBOL_GPL(ring_buffer_event_data);
310 #define for_each_buffer_cpu(buffer, cpu) \
311 for_each_cpu(cpu, buffer->cpumask)
314 #define TS_MASK ((1ULL << TS_SHIFT) - 1)
315 #define TS_DELTA_TEST (~TS_MASK)
317 struct buffer_data_page {
318 u64 time_stamp; /* page time stamp */
319 local_t commit; /* write committed index */
320 unsigned char data[]; /* data of buffer page */
324 local_t write; /* index for next write */
325 unsigned read; /* index for next read */
326 struct list_head list; /* list of free pages */
327 struct buffer_data_page *page; /* Actual data page */
330 static void rb_init_page(struct buffer_data_page *bpage)
332 local_set(&bpage->commit, 0);
336 * ring_buffer_page_len - the size of data on the page.
337 * @page: The page to read
339 * Returns the amount of data on the page, including buffer page header.
341 size_t ring_buffer_page_len(void *page)
343 return local_read(&((struct buffer_data_page *)page)->commit)
348 * Also stolen from mm/slob.c. Thanks to Mathieu Desnoyers for pointing
351 static void free_buffer_page(struct buffer_page *bpage)
353 free_page((unsigned long)bpage->page);
358 * We need to fit the time_stamp delta into 27 bits.
360 static inline int test_time_stamp(u64 delta)
362 if (delta & TS_DELTA_TEST)
367 #define BUF_PAGE_SIZE (PAGE_SIZE - BUF_PAGE_HDR_SIZE)
369 int ring_buffer_print_page_header(struct trace_seq *s)
371 struct buffer_data_page field;
374 ret = trace_seq_printf(s, "\tfield: u64 timestamp;\t"
375 "offset:0;\tsize:%u;\n",
376 (unsigned int)sizeof(field.time_stamp));
378 ret = trace_seq_printf(s, "\tfield: local_t commit;\t"
379 "offset:%u;\tsize:%u;\n",
380 (unsigned int)offsetof(typeof(field), commit),
381 (unsigned int)sizeof(field.commit));
383 ret = trace_seq_printf(s, "\tfield: char data;\t"
384 "offset:%u;\tsize:%u;\n",
385 (unsigned int)offsetof(typeof(field), data),
386 (unsigned int)BUF_PAGE_SIZE);
392 * head_page == tail_page && head == tail then buffer is empty.
394 struct ring_buffer_per_cpu {
396 struct ring_buffer *buffer;
397 spinlock_t reader_lock; /* serialize readers */
399 struct lock_class_key lock_key;
400 struct list_head pages;
401 struct buffer_page *head_page; /* read from head */
402 struct buffer_page *tail_page; /* write to tail */
403 struct buffer_page *commit_page; /* committed pages */
404 struct buffer_page *reader_page;
405 unsigned long nmi_dropped;
406 unsigned long commit_overrun;
407 unsigned long overrun;
408 unsigned long entries;
411 atomic_t record_disabled;
418 atomic_t record_disabled;
419 cpumask_var_t cpumask;
423 struct ring_buffer_per_cpu **buffers;
425 #ifdef CONFIG_HOTPLUG_CPU
426 struct notifier_block cpu_notify;
431 struct ring_buffer_iter {
432 struct ring_buffer_per_cpu *cpu_buffer;
434 struct buffer_page *head_page;
438 /* buffer may be either ring_buffer or ring_buffer_per_cpu */
439 #define RB_WARN_ON(buffer, cond) \
441 int _____ret = unlikely(cond); \
443 atomic_inc(&buffer->record_disabled); \
449 /* Up this if you want to test the TIME_EXTENTS and normalization */
450 #define DEBUG_SHIFT 0
452 u64 ring_buffer_time_stamp(struct ring_buffer *buffer, int cpu)
456 preempt_disable_notrace();
457 /* shift to debug/test normalization and TIME_EXTENTS */
458 time = buffer->clock() << DEBUG_SHIFT;
459 preempt_enable_no_resched_notrace();
463 EXPORT_SYMBOL_GPL(ring_buffer_time_stamp);
465 void ring_buffer_normalize_time_stamp(struct ring_buffer *buffer,
468 /* Just stupid testing the normalize function and deltas */
471 EXPORT_SYMBOL_GPL(ring_buffer_normalize_time_stamp);
474 * check_pages - integrity check of buffer pages
475 * @cpu_buffer: CPU buffer with pages to test
477 * As a safety measure we check to make sure the data pages have not
480 static int rb_check_pages(struct ring_buffer_per_cpu *cpu_buffer)
482 struct list_head *head = &cpu_buffer->pages;
483 struct buffer_page *bpage, *tmp;
485 if (RB_WARN_ON(cpu_buffer, head->next->prev != head))
487 if (RB_WARN_ON(cpu_buffer, head->prev->next != head))
490 list_for_each_entry_safe(bpage, tmp, head, list) {
491 if (RB_WARN_ON(cpu_buffer,
492 bpage->list.next->prev != &bpage->list))
494 if (RB_WARN_ON(cpu_buffer,
495 bpage->list.prev->next != &bpage->list))
502 static int rb_allocate_pages(struct ring_buffer_per_cpu *cpu_buffer,
505 struct list_head *head = &cpu_buffer->pages;
506 struct buffer_page *bpage, *tmp;
511 for (i = 0; i < nr_pages; i++) {
512 bpage = kzalloc_node(ALIGN(sizeof(*bpage), cache_line_size()),
513 GFP_KERNEL, cpu_to_node(cpu_buffer->cpu));
516 list_add(&bpage->list, &pages);
518 addr = __get_free_page(GFP_KERNEL);
521 bpage->page = (void *)addr;
522 rb_init_page(bpage->page);
525 list_splice(&pages, head);
527 rb_check_pages(cpu_buffer);
532 list_for_each_entry_safe(bpage, tmp, &pages, list) {
533 list_del_init(&bpage->list);
534 free_buffer_page(bpage);
539 static struct ring_buffer_per_cpu *
540 rb_allocate_cpu_buffer(struct ring_buffer *buffer, int cpu)
542 struct ring_buffer_per_cpu *cpu_buffer;
543 struct buffer_page *bpage;
547 cpu_buffer = kzalloc_node(ALIGN(sizeof(*cpu_buffer), cache_line_size()),
548 GFP_KERNEL, cpu_to_node(cpu));
552 cpu_buffer->cpu = cpu;
553 cpu_buffer->buffer = buffer;
554 spin_lock_init(&cpu_buffer->reader_lock);
555 cpu_buffer->lock = (raw_spinlock_t)__RAW_SPIN_LOCK_UNLOCKED;
556 INIT_LIST_HEAD(&cpu_buffer->pages);
558 bpage = kzalloc_node(ALIGN(sizeof(*bpage), cache_line_size()),
559 GFP_KERNEL, cpu_to_node(cpu));
561 goto fail_free_buffer;
563 cpu_buffer->reader_page = bpage;
564 addr = __get_free_page(GFP_KERNEL);
566 goto fail_free_reader;
567 bpage->page = (void *)addr;
568 rb_init_page(bpage->page);
570 INIT_LIST_HEAD(&cpu_buffer->reader_page->list);
572 ret = rb_allocate_pages(cpu_buffer, buffer->pages);
574 goto fail_free_reader;
576 cpu_buffer->head_page
577 = list_entry(cpu_buffer->pages.next, struct buffer_page, list);
578 cpu_buffer->tail_page = cpu_buffer->commit_page = cpu_buffer->head_page;
583 free_buffer_page(cpu_buffer->reader_page);
590 static void rb_free_cpu_buffer(struct ring_buffer_per_cpu *cpu_buffer)
592 struct list_head *head = &cpu_buffer->pages;
593 struct buffer_page *bpage, *tmp;
595 free_buffer_page(cpu_buffer->reader_page);
597 list_for_each_entry_safe(bpage, tmp, head, list) {
598 list_del_init(&bpage->list);
599 free_buffer_page(bpage);
605 * Causes compile errors if the struct buffer_page gets bigger
606 * than the struct page.
608 extern int ring_buffer_page_too_big(void);
610 #ifdef CONFIG_HOTPLUG_CPU
611 static int rb_cpu_notify(struct notifier_block *self,
612 unsigned long action, void *hcpu);
616 * ring_buffer_alloc - allocate a new ring_buffer
617 * @size: the size in bytes per cpu that is needed.
618 * @flags: attributes to set for the ring buffer.
620 * Currently the only flag that is available is the RB_FL_OVERWRITE
621 * flag. This flag means that the buffer will overwrite old data
622 * when the buffer wraps. If this flag is not set, the buffer will
623 * drop data when the tail hits the head.
625 struct ring_buffer *ring_buffer_alloc(unsigned long size, unsigned flags)
627 struct ring_buffer *buffer;
631 /* Paranoid! Optimizes out when all is well */
632 if (sizeof(struct buffer_page) > sizeof(struct page))
633 ring_buffer_page_too_big();
636 /* keep it in its own cache line */
637 buffer = kzalloc(ALIGN(sizeof(*buffer), cache_line_size()),
642 if (!alloc_cpumask_var(&buffer->cpumask, GFP_KERNEL))
643 goto fail_free_buffer;
645 buffer->pages = DIV_ROUND_UP(size, BUF_PAGE_SIZE);
646 buffer->flags = flags;
647 buffer->clock = trace_clock_local;
649 /* need at least two pages */
650 if (buffer->pages == 1)
654 * In case of non-hotplug cpu, if the ring-buffer is allocated
655 * in early initcall, it will not be notified of secondary cpus.
656 * In that off case, we need to allocate for all possible cpus.
658 #ifdef CONFIG_HOTPLUG_CPU
660 cpumask_copy(buffer->cpumask, cpu_online_mask);
662 cpumask_copy(buffer->cpumask, cpu_possible_mask);
664 buffer->cpus = nr_cpu_ids;
666 bsize = sizeof(void *) * nr_cpu_ids;
667 buffer->buffers = kzalloc(ALIGN(bsize, cache_line_size()),
669 if (!buffer->buffers)
670 goto fail_free_cpumask;
672 for_each_buffer_cpu(buffer, cpu) {
673 buffer->buffers[cpu] =
674 rb_allocate_cpu_buffer(buffer, cpu);
675 if (!buffer->buffers[cpu])
676 goto fail_free_buffers;
679 #ifdef CONFIG_HOTPLUG_CPU
680 buffer->cpu_notify.notifier_call = rb_cpu_notify;
681 buffer->cpu_notify.priority = 0;
682 register_cpu_notifier(&buffer->cpu_notify);
686 mutex_init(&buffer->mutex);
691 for_each_buffer_cpu(buffer, cpu) {
692 if (buffer->buffers[cpu])
693 rb_free_cpu_buffer(buffer->buffers[cpu]);
695 kfree(buffer->buffers);
698 free_cpumask_var(buffer->cpumask);
705 EXPORT_SYMBOL_GPL(ring_buffer_alloc);
708 * ring_buffer_free - free a ring buffer.
709 * @buffer: the buffer to free.
712 ring_buffer_free(struct ring_buffer *buffer)
718 #ifdef CONFIG_HOTPLUG_CPU
719 unregister_cpu_notifier(&buffer->cpu_notify);
722 for_each_buffer_cpu(buffer, cpu)
723 rb_free_cpu_buffer(buffer->buffers[cpu]);
727 free_cpumask_var(buffer->cpumask);
731 EXPORT_SYMBOL_GPL(ring_buffer_free);
733 void ring_buffer_set_clock(struct ring_buffer *buffer,
736 buffer->clock = clock;
739 static void rb_reset_cpu(struct ring_buffer_per_cpu *cpu_buffer);
742 rb_remove_pages(struct ring_buffer_per_cpu *cpu_buffer, unsigned nr_pages)
744 struct buffer_page *bpage;
748 atomic_inc(&cpu_buffer->record_disabled);
751 for (i = 0; i < nr_pages; i++) {
752 if (RB_WARN_ON(cpu_buffer, list_empty(&cpu_buffer->pages)))
754 p = cpu_buffer->pages.next;
755 bpage = list_entry(p, struct buffer_page, list);
756 list_del_init(&bpage->list);
757 free_buffer_page(bpage);
759 if (RB_WARN_ON(cpu_buffer, list_empty(&cpu_buffer->pages)))
762 rb_reset_cpu(cpu_buffer);
764 rb_check_pages(cpu_buffer);
766 atomic_dec(&cpu_buffer->record_disabled);
771 rb_insert_pages(struct ring_buffer_per_cpu *cpu_buffer,
772 struct list_head *pages, unsigned nr_pages)
774 struct buffer_page *bpage;
778 atomic_inc(&cpu_buffer->record_disabled);
781 for (i = 0; i < nr_pages; i++) {
782 if (RB_WARN_ON(cpu_buffer, list_empty(pages)))
785 bpage = list_entry(p, struct buffer_page, list);
786 list_del_init(&bpage->list);
787 list_add_tail(&bpage->list, &cpu_buffer->pages);
789 rb_reset_cpu(cpu_buffer);
791 rb_check_pages(cpu_buffer);
793 atomic_dec(&cpu_buffer->record_disabled);
797 * ring_buffer_resize - resize the ring buffer
798 * @buffer: the buffer to resize.
799 * @size: the new size.
801 * The tracer is responsible for making sure that the buffer is
802 * not being used while changing the size.
803 * Note: We may be able to change the above requirement by using
804 * RCU synchronizations.
806 * Minimum size is 2 * BUF_PAGE_SIZE.
808 * Returns -1 on failure.
810 int ring_buffer_resize(struct ring_buffer *buffer, unsigned long size)
812 struct ring_buffer_per_cpu *cpu_buffer;
813 unsigned nr_pages, rm_pages, new_pages;
814 struct buffer_page *bpage, *tmp;
815 unsigned long buffer_size;
821 * Always succeed at resizing a non-existent buffer:
826 size = DIV_ROUND_UP(size, BUF_PAGE_SIZE);
827 size *= BUF_PAGE_SIZE;
828 buffer_size = buffer->pages * BUF_PAGE_SIZE;
830 /* we need a minimum of two pages */
831 if (size < BUF_PAGE_SIZE * 2)
832 size = BUF_PAGE_SIZE * 2;
834 if (size == buffer_size)
837 mutex_lock(&buffer->mutex);
840 nr_pages = DIV_ROUND_UP(size, BUF_PAGE_SIZE);
842 if (size < buffer_size) {
844 /* easy case, just free pages */
845 if (RB_WARN_ON(buffer, nr_pages >= buffer->pages))
848 rm_pages = buffer->pages - nr_pages;
850 for_each_buffer_cpu(buffer, cpu) {
851 cpu_buffer = buffer->buffers[cpu];
852 rb_remove_pages(cpu_buffer, rm_pages);
858 * This is a bit more difficult. We only want to add pages
859 * when we can allocate enough for all CPUs. We do this
860 * by allocating all the pages and storing them on a local
861 * link list. If we succeed in our allocation, then we
862 * add these pages to the cpu_buffers. Otherwise we just free
863 * them all and return -ENOMEM;
865 if (RB_WARN_ON(buffer, nr_pages <= buffer->pages))
868 new_pages = nr_pages - buffer->pages;
870 for_each_buffer_cpu(buffer, cpu) {
871 for (i = 0; i < new_pages; i++) {
872 bpage = kzalloc_node(ALIGN(sizeof(*bpage),
874 GFP_KERNEL, cpu_to_node(cpu));
877 list_add(&bpage->list, &pages);
878 addr = __get_free_page(GFP_KERNEL);
881 bpage->page = (void *)addr;
882 rb_init_page(bpage->page);
886 for_each_buffer_cpu(buffer, cpu) {
887 cpu_buffer = buffer->buffers[cpu];
888 rb_insert_pages(cpu_buffer, &pages, new_pages);
891 if (RB_WARN_ON(buffer, !list_empty(&pages)))
895 buffer->pages = nr_pages;
897 mutex_unlock(&buffer->mutex);
902 list_for_each_entry_safe(bpage, tmp, &pages, list) {
903 list_del_init(&bpage->list);
904 free_buffer_page(bpage);
907 mutex_unlock(&buffer->mutex);
911 * Something went totally wrong, and we are too paranoid
912 * to even clean up the mess.
916 mutex_unlock(&buffer->mutex);
919 EXPORT_SYMBOL_GPL(ring_buffer_resize);
922 __rb_data_page_index(struct buffer_data_page *bpage, unsigned index)
924 return bpage->data + index;
927 static inline void *__rb_page_index(struct buffer_page *bpage, unsigned index)
929 return bpage->page->data + index;
932 static inline struct ring_buffer_event *
933 rb_reader_event(struct ring_buffer_per_cpu *cpu_buffer)
935 return __rb_page_index(cpu_buffer->reader_page,
936 cpu_buffer->reader_page->read);
939 static inline struct ring_buffer_event *
940 rb_head_event(struct ring_buffer_per_cpu *cpu_buffer)
942 return __rb_page_index(cpu_buffer->head_page,
943 cpu_buffer->head_page->read);
946 static inline struct ring_buffer_event *
947 rb_iter_head_event(struct ring_buffer_iter *iter)
949 return __rb_page_index(iter->head_page, iter->head);
952 static inline unsigned rb_page_write(struct buffer_page *bpage)
954 return local_read(&bpage->write);
957 static inline unsigned rb_page_commit(struct buffer_page *bpage)
959 return local_read(&bpage->page->commit);
962 /* Size is determined by what has been commited */
963 static inline unsigned rb_page_size(struct buffer_page *bpage)
965 return rb_page_commit(bpage);
968 static inline unsigned
969 rb_commit_index(struct ring_buffer_per_cpu *cpu_buffer)
971 return rb_page_commit(cpu_buffer->commit_page);
974 static inline unsigned rb_head_size(struct ring_buffer_per_cpu *cpu_buffer)
976 return rb_page_commit(cpu_buffer->head_page);
980 * When the tail hits the head and the buffer is in overwrite mode,
981 * the head jumps to the next page and all content on the previous
982 * page is discarded. But before doing so, we update the overrun
983 * variable of the buffer.
985 static void rb_update_overflow(struct ring_buffer_per_cpu *cpu_buffer)
987 struct ring_buffer_event *event;
990 for (head = 0; head < rb_head_size(cpu_buffer);
991 head += rb_event_length(event)) {
993 event = __rb_page_index(cpu_buffer->head_page, head);
994 if (RB_WARN_ON(cpu_buffer, rb_null_event(event)))
996 /* Only count data entries */
997 if (event->type_len > RINGBUF_TYPE_DATA_TYPE_LEN_MAX)
999 cpu_buffer->overrun++;
1000 cpu_buffer->entries--;
1004 static inline void rb_inc_page(struct ring_buffer_per_cpu *cpu_buffer,
1005 struct buffer_page **bpage)
1007 struct list_head *p = (*bpage)->list.next;
1009 if (p == &cpu_buffer->pages)
1012 *bpage = list_entry(p, struct buffer_page, list);
1015 static inline unsigned
1016 rb_event_index(struct ring_buffer_event *event)
1018 unsigned long addr = (unsigned long)event;
1020 return (addr & ~PAGE_MASK) - (PAGE_SIZE - BUF_PAGE_SIZE);
1024 rb_is_commit(struct ring_buffer_per_cpu *cpu_buffer,
1025 struct ring_buffer_event *event)
1027 unsigned long addr = (unsigned long)event;
1028 unsigned long index;
1030 index = rb_event_index(event);
1033 return cpu_buffer->commit_page->page == (void *)addr &&
1034 rb_commit_index(cpu_buffer) == index;
1038 rb_set_commit_event(struct ring_buffer_per_cpu *cpu_buffer,
1039 struct ring_buffer_event *event)
1041 unsigned long addr = (unsigned long)event;
1042 unsigned long index;
1044 index = rb_event_index(event);
1047 while (cpu_buffer->commit_page->page != (void *)addr) {
1048 if (RB_WARN_ON(cpu_buffer,
1049 cpu_buffer->commit_page == cpu_buffer->tail_page))
1051 cpu_buffer->commit_page->page->commit =
1052 cpu_buffer->commit_page->write;
1053 rb_inc_page(cpu_buffer, &cpu_buffer->commit_page);
1054 cpu_buffer->write_stamp =
1055 cpu_buffer->commit_page->page->time_stamp;
1058 /* Now set the commit to the event's index */
1059 local_set(&cpu_buffer->commit_page->page->commit, index);
1063 rb_set_commit_to_write(struct ring_buffer_per_cpu *cpu_buffer)
1066 * We only race with interrupts and NMIs on this CPU.
1067 * If we own the commit event, then we can commit
1068 * all others that interrupted us, since the interruptions
1069 * are in stack format (they finish before they come
1070 * back to us). This allows us to do a simple loop to
1071 * assign the commit to the tail.
1074 while (cpu_buffer->commit_page != cpu_buffer->tail_page) {
1075 cpu_buffer->commit_page->page->commit =
1076 cpu_buffer->commit_page->write;
1077 rb_inc_page(cpu_buffer, &cpu_buffer->commit_page);
1078 cpu_buffer->write_stamp =
1079 cpu_buffer->commit_page->page->time_stamp;
1080 /* add barrier to keep gcc from optimizing too much */
1083 while (rb_commit_index(cpu_buffer) !=
1084 rb_page_write(cpu_buffer->commit_page)) {
1085 cpu_buffer->commit_page->page->commit =
1086 cpu_buffer->commit_page->write;
1090 /* again, keep gcc from optimizing */
1094 * If an interrupt came in just after the first while loop
1095 * and pushed the tail page forward, we will be left with
1096 * a dangling commit that will never go forward.
1098 if (unlikely(cpu_buffer->commit_page != cpu_buffer->tail_page))
1102 static void rb_reset_reader_page(struct ring_buffer_per_cpu *cpu_buffer)
1104 cpu_buffer->read_stamp = cpu_buffer->reader_page->page->time_stamp;
1105 cpu_buffer->reader_page->read = 0;
1108 static void rb_inc_iter(struct ring_buffer_iter *iter)
1110 struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
1113 * The iterator could be on the reader page (it starts there).
1114 * But the head could have moved, since the reader was
1115 * found. Check for this case and assign the iterator
1116 * to the head page instead of next.
1118 if (iter->head_page == cpu_buffer->reader_page)
1119 iter->head_page = cpu_buffer->head_page;
1121 rb_inc_page(cpu_buffer, &iter->head_page);
1123 iter->read_stamp = iter->head_page->page->time_stamp;
1128 * ring_buffer_update_event - update event type and data
1129 * @event: the even to update
1130 * @type: the type of event
1131 * @length: the size of the event field in the ring buffer
1133 * Update the type and data fields of the event. The length
1134 * is the actual size that is written to the ring buffer,
1135 * and with this, we can determine what to place into the
1139 rb_update_event(struct ring_buffer_event *event,
1140 unsigned type, unsigned length)
1142 event->type_len = type;
1146 case RINGBUF_TYPE_PADDING:
1147 case RINGBUF_TYPE_TIME_EXTEND:
1148 case RINGBUF_TYPE_TIME_STAMP:
1152 length -= RB_EVNT_HDR_SIZE;
1153 if (length > RB_MAX_SMALL_DATA)
1154 event->array[0] = length;
1156 event->type_len = DIV_ROUND_UP(length, RB_ALIGNMENT);
1163 static unsigned rb_calculate_event_length(unsigned length)
1165 struct ring_buffer_event event; /* Used only for sizeof array */
1167 /* zero length can cause confusions */
1171 if (length > RB_MAX_SMALL_DATA)
1172 length += sizeof(event.array[0]);
1174 length += RB_EVNT_HDR_SIZE;
1175 length = ALIGN(length, RB_ALIGNMENT);
1180 static struct ring_buffer_event *
1181 __rb_reserve_next(struct ring_buffer_per_cpu *cpu_buffer,
1182 unsigned type, unsigned long length, u64 *ts)
1184 struct buffer_page *tail_page, *head_page, *reader_page, *commit_page;
1185 unsigned long tail, write;
1186 struct ring_buffer *buffer = cpu_buffer->buffer;
1187 struct ring_buffer_event *event;
1188 unsigned long flags;
1189 bool lock_taken = false;
1191 commit_page = cpu_buffer->commit_page;
1192 /* we just need to protect against interrupts */
1194 tail_page = cpu_buffer->tail_page;
1195 write = local_add_return(length, &tail_page->write);
1196 tail = write - length;
1198 /* See if we shot pass the end of this buffer page */
1199 if (write > BUF_PAGE_SIZE) {
1200 struct buffer_page *next_page = tail_page;
1202 local_irq_save(flags);
1204 * Since the write to the buffer is still not
1205 * fully lockless, we must be careful with NMIs.
1206 * The locks in the writers are taken when a write
1207 * crosses to a new page. The locks protect against
1208 * races with the readers (this will soon be fixed
1209 * with a lockless solution).
1211 * Because we can not protect against NMIs, and we
1212 * want to keep traces reentrant, we need to manage
1213 * what happens when we are in an NMI.
1215 * NMIs can happen after we take the lock.
1216 * If we are in an NMI, only take the lock
1217 * if it is not already taken. Otherwise
1220 if (unlikely(in_nmi())) {
1221 if (!__raw_spin_trylock(&cpu_buffer->lock)) {
1222 cpu_buffer->nmi_dropped++;
1226 __raw_spin_lock(&cpu_buffer->lock);
1230 rb_inc_page(cpu_buffer, &next_page);
1232 head_page = cpu_buffer->head_page;
1233 reader_page = cpu_buffer->reader_page;
1235 /* we grabbed the lock before incrementing */
1236 if (RB_WARN_ON(cpu_buffer, next_page == reader_page))
1240 * If for some reason, we had an interrupt storm that made
1241 * it all the way around the buffer, bail, and warn
1244 if (unlikely(next_page == commit_page)) {
1245 cpu_buffer->commit_overrun++;
1249 if (next_page == head_page) {
1250 if (!(buffer->flags & RB_FL_OVERWRITE))
1253 /* tail_page has not moved yet? */
1254 if (tail_page == cpu_buffer->tail_page) {
1255 /* count overflows */
1256 rb_update_overflow(cpu_buffer);
1258 rb_inc_page(cpu_buffer, &head_page);
1259 cpu_buffer->head_page = head_page;
1260 cpu_buffer->head_page->read = 0;
1265 * If the tail page is still the same as what we think
1266 * it is, then it is up to us to update the tail
1269 if (tail_page == cpu_buffer->tail_page) {
1270 local_set(&next_page->write, 0);
1271 local_set(&next_page->page->commit, 0);
1272 cpu_buffer->tail_page = next_page;
1274 /* reread the time stamp */
1275 *ts = ring_buffer_time_stamp(buffer, cpu_buffer->cpu);
1276 cpu_buffer->tail_page->page->time_stamp = *ts;
1280 * The actual tail page has moved forward.
1282 if (tail < BUF_PAGE_SIZE) {
1283 /* Mark the rest of the page with padding */
1284 event = __rb_page_index(tail_page, tail);
1285 rb_event_set_padding(event);
1288 if (tail <= BUF_PAGE_SIZE)
1289 /* Set the write back to the previous setting */
1290 local_set(&tail_page->write, tail);
1293 * If this was a commit entry that failed,
1294 * increment that too
1296 if (tail_page == cpu_buffer->commit_page &&
1297 tail == rb_commit_index(cpu_buffer)) {
1298 rb_set_commit_to_write(cpu_buffer);
1301 __raw_spin_unlock(&cpu_buffer->lock);
1302 local_irq_restore(flags);
1304 /* fail and let the caller try again */
1305 return ERR_PTR(-EAGAIN);
1308 /* We reserved something on the buffer */
1310 if (RB_WARN_ON(cpu_buffer, write > BUF_PAGE_SIZE))
1313 event = __rb_page_index(tail_page, tail);
1314 rb_update_event(event, type, length);
1317 * If this is a commit and the tail is zero, then update
1318 * this page's time stamp.
1320 if (!tail && rb_is_commit(cpu_buffer, event))
1321 cpu_buffer->commit_page->page->time_stamp = *ts;
1327 if (tail <= BUF_PAGE_SIZE)
1328 local_set(&tail_page->write, tail);
1330 if (likely(lock_taken))
1331 __raw_spin_unlock(&cpu_buffer->lock);
1332 local_irq_restore(flags);
1337 rb_add_time_stamp(struct ring_buffer_per_cpu *cpu_buffer,
1338 u64 *ts, u64 *delta)
1340 struct ring_buffer_event *event;
1344 if (unlikely(*delta > (1ULL << 59) && !once++)) {
1345 printk(KERN_WARNING "Delta way too big! %llu"
1346 " ts=%llu write stamp = %llu\n",
1347 (unsigned long long)*delta,
1348 (unsigned long long)*ts,
1349 (unsigned long long)cpu_buffer->write_stamp);
1354 * The delta is too big, we to add a
1357 event = __rb_reserve_next(cpu_buffer,
1358 RINGBUF_TYPE_TIME_EXTEND,
1364 if (PTR_ERR(event) == -EAGAIN)
1367 /* Only a commited time event can update the write stamp */
1368 if (rb_is_commit(cpu_buffer, event)) {
1370 * If this is the first on the page, then we need to
1371 * update the page itself, and just put in a zero.
1373 if (rb_event_index(event)) {
1374 event->time_delta = *delta & TS_MASK;
1375 event->array[0] = *delta >> TS_SHIFT;
1377 cpu_buffer->commit_page->page->time_stamp = *ts;
1378 event->time_delta = 0;
1379 event->array[0] = 0;
1381 cpu_buffer->write_stamp = *ts;
1382 /* let the caller know this was the commit */
1385 /* Darn, this is just wasted space */
1386 event->time_delta = 0;
1387 event->array[0] = 0;
1396 static struct ring_buffer_event *
1397 rb_reserve_next_event(struct ring_buffer_per_cpu *cpu_buffer,
1398 unsigned type, unsigned long length)
1400 struct ring_buffer_event *event;
1407 * We allow for interrupts to reenter here and do a trace.
1408 * If one does, it will cause this original code to loop
1409 * back here. Even with heavy interrupts happening, this
1410 * should only happen a few times in a row. If this happens
1411 * 1000 times in a row, there must be either an interrupt
1412 * storm or we have something buggy.
1415 if (RB_WARN_ON(cpu_buffer, ++nr_loops > 1000))
1418 ts = ring_buffer_time_stamp(cpu_buffer->buffer, cpu_buffer->cpu);
1421 * Only the first commit can update the timestamp.
1422 * Yes there is a race here. If an interrupt comes in
1423 * just after the conditional and it traces too, then it
1424 * will also check the deltas. More than one timestamp may
1425 * also be made. But only the entry that did the actual
1426 * commit will be something other than zero.
1428 if (cpu_buffer->tail_page == cpu_buffer->commit_page &&
1429 rb_page_write(cpu_buffer->tail_page) ==
1430 rb_commit_index(cpu_buffer)) {
1432 delta = ts - cpu_buffer->write_stamp;
1434 /* make sure this delta is calculated here */
1437 /* Did the write stamp get updated already? */
1438 if (unlikely(ts < cpu_buffer->write_stamp))
1441 if (test_time_stamp(delta)) {
1443 commit = rb_add_time_stamp(cpu_buffer, &ts, &delta);
1445 if (commit == -EBUSY)
1448 if (commit == -EAGAIN)
1451 RB_WARN_ON(cpu_buffer, commit < 0);
1454 /* Non commits have zero deltas */
1457 event = __rb_reserve_next(cpu_buffer, type, length, &ts);
1458 if (PTR_ERR(event) == -EAGAIN)
1462 if (unlikely(commit))
1464 * Ouch! We needed a timestamp and it was commited. But
1465 * we didn't get our event reserved.
1467 rb_set_commit_to_write(cpu_buffer);
1472 * If the timestamp was commited, make the commit our entry
1473 * now so that we will update it when needed.
1476 rb_set_commit_event(cpu_buffer, event);
1477 else if (!rb_is_commit(cpu_buffer, event))
1480 event->time_delta = delta;
1485 #define TRACE_RECURSIVE_DEPTH 16
1487 static int trace_recursive_lock(void)
1489 current->trace_recursion++;
1491 if (likely(current->trace_recursion < TRACE_RECURSIVE_DEPTH))
1494 /* Disable all tracing before we do anything else */
1495 tracing_off_permanent();
1497 printk_once(KERN_WARNING "Tracing recursion: depth[%ld]:"
1498 "HC[%lu]:SC[%lu]:NMI[%lu]\n",
1499 current->trace_recursion,
1500 hardirq_count() >> HARDIRQ_SHIFT,
1501 softirq_count() >> SOFTIRQ_SHIFT,
1508 static void trace_recursive_unlock(void)
1510 WARN_ON_ONCE(!current->trace_recursion);
1512 current->trace_recursion--;
1515 static DEFINE_PER_CPU(int, rb_need_resched);
1518 * ring_buffer_lock_reserve - reserve a part of the buffer
1519 * @buffer: the ring buffer to reserve from
1520 * @length: the length of the data to reserve (excluding event header)
1522 * Returns a reseverd event on the ring buffer to copy directly to.
1523 * The user of this interface will need to get the body to write into
1524 * and can use the ring_buffer_event_data() interface.
1526 * The length is the length of the data needed, not the event length
1527 * which also includes the event header.
1529 * Must be paired with ring_buffer_unlock_commit, unless NULL is returned.
1530 * If NULL is returned, then nothing has been allocated or locked.
1532 struct ring_buffer_event *
1533 ring_buffer_lock_reserve(struct ring_buffer *buffer, unsigned long length)
1535 struct ring_buffer_per_cpu *cpu_buffer;
1536 struct ring_buffer_event *event;
1539 if (ring_buffer_flags != RB_BUFFERS_ON)
1542 if (atomic_read(&buffer->record_disabled))
1545 /* If we are tracing schedule, we don't want to recurse */
1546 resched = ftrace_preempt_disable();
1548 if (trace_recursive_lock())
1551 cpu = raw_smp_processor_id();
1553 if (!cpumask_test_cpu(cpu, buffer->cpumask))
1556 cpu_buffer = buffer->buffers[cpu];
1558 if (atomic_read(&cpu_buffer->record_disabled))
1561 length = rb_calculate_event_length(length);
1562 if (length > BUF_PAGE_SIZE)
1565 event = rb_reserve_next_event(cpu_buffer, 0, length);
1570 * Need to store resched state on this cpu.
1571 * Only the first needs to.
1574 if (preempt_count() == 1)
1575 per_cpu(rb_need_resched, cpu) = resched;
1580 trace_recursive_unlock();
1583 ftrace_preempt_enable(resched);
1586 EXPORT_SYMBOL_GPL(ring_buffer_lock_reserve);
1588 static void rb_commit(struct ring_buffer_per_cpu *cpu_buffer,
1589 struct ring_buffer_event *event)
1591 cpu_buffer->entries++;
1593 /* Only process further if we own the commit */
1594 if (!rb_is_commit(cpu_buffer, event))
1597 cpu_buffer->write_stamp += event->time_delta;
1599 rb_set_commit_to_write(cpu_buffer);
1603 * ring_buffer_unlock_commit - commit a reserved
1604 * @buffer: The buffer to commit to
1605 * @event: The event pointer to commit.
1607 * This commits the data to the ring buffer, and releases any locks held.
1609 * Must be paired with ring_buffer_lock_reserve.
1611 int ring_buffer_unlock_commit(struct ring_buffer *buffer,
1612 struct ring_buffer_event *event)
1614 struct ring_buffer_per_cpu *cpu_buffer;
1615 int cpu = raw_smp_processor_id();
1617 cpu_buffer = buffer->buffers[cpu];
1619 rb_commit(cpu_buffer, event);
1621 trace_recursive_unlock();
1624 * Only the last preempt count needs to restore preemption.
1626 if (preempt_count() == 1)
1627 ftrace_preempt_enable(per_cpu(rb_need_resched, cpu));
1629 preempt_enable_no_resched_notrace();
1633 EXPORT_SYMBOL_GPL(ring_buffer_unlock_commit);
1635 static inline void rb_event_discard(struct ring_buffer_event *event)
1637 /* array[0] holds the actual length for the discarded event */
1638 event->array[0] = rb_event_data_length(event) - RB_EVNT_HDR_SIZE;
1639 event->type_len = RINGBUF_TYPE_PADDING;
1640 /* time delta must be non zero */
1641 if (!event->time_delta)
1642 event->time_delta = 1;
1646 * ring_buffer_event_discard - discard any event in the ring buffer
1647 * @event: the event to discard
1649 * Sometimes a event that is in the ring buffer needs to be ignored.
1650 * This function lets the user discard an event in the ring buffer
1651 * and then that event will not be read later.
1653 * Note, it is up to the user to be careful with this, and protect
1654 * against races. If the user discards an event that has been consumed
1655 * it is possible that it could corrupt the ring buffer.
1657 void ring_buffer_event_discard(struct ring_buffer_event *event)
1659 rb_event_discard(event);
1661 EXPORT_SYMBOL_GPL(ring_buffer_event_discard);
1664 * ring_buffer_commit_discard - discard an event that has not been committed
1665 * @buffer: the ring buffer
1666 * @event: non committed event to discard
1668 * This is similar to ring_buffer_event_discard but must only be
1669 * performed on an event that has not been committed yet. The difference
1670 * is that this will also try to free the event from the ring buffer
1671 * if another event has not been added behind it.
1673 * If another event has been added behind it, it will set the event
1674 * up as discarded, and perform the commit.
1676 * If this function is called, do not call ring_buffer_unlock_commit on
1679 void ring_buffer_discard_commit(struct ring_buffer *buffer,
1680 struct ring_buffer_event *event)
1682 struct ring_buffer_per_cpu *cpu_buffer;
1683 unsigned long new_index, old_index;
1684 struct buffer_page *bpage;
1685 unsigned long index;
1689 /* The event is discarded regardless */
1690 rb_event_discard(event);
1693 * This must only be called if the event has not been
1694 * committed yet. Thus we can assume that preemption
1695 * is still disabled.
1697 RB_WARN_ON(buffer, !preempt_count());
1699 cpu = smp_processor_id();
1700 cpu_buffer = buffer->buffers[cpu];
1702 new_index = rb_event_index(event);
1703 old_index = new_index + rb_event_length(event);
1704 addr = (unsigned long)event;
1707 bpage = cpu_buffer->tail_page;
1709 if (bpage == (void *)addr && rb_page_write(bpage) == old_index) {
1711 * This is on the tail page. It is possible that
1712 * a write could come in and move the tail page
1713 * and write to the next page. That is fine
1714 * because we just shorten what is on this page.
1716 index = local_cmpxchg(&bpage->write, old_index, new_index);
1717 if (index == old_index)
1722 * The commit is still visible by the reader, so we
1723 * must increment entries.
1725 cpu_buffer->entries++;
1728 * If a write came in and pushed the tail page
1729 * we still need to update the commit pointer
1730 * if we were the commit.
1732 if (rb_is_commit(cpu_buffer, event))
1733 rb_set_commit_to_write(cpu_buffer);
1735 trace_recursive_unlock();
1738 * Only the last preempt count needs to restore preemption.
1740 if (preempt_count() == 1)
1741 ftrace_preempt_enable(per_cpu(rb_need_resched, cpu));
1743 preempt_enable_no_resched_notrace();
1746 EXPORT_SYMBOL_GPL(ring_buffer_discard_commit);
1749 * ring_buffer_write - write data to the buffer without reserving
1750 * @buffer: The ring buffer to write to.
1751 * @length: The length of the data being written (excluding the event header)
1752 * @data: The data to write to the buffer.
1754 * This is like ring_buffer_lock_reserve and ring_buffer_unlock_commit as
1755 * one function. If you already have the data to write to the buffer, it
1756 * may be easier to simply call this function.
1758 * Note, like ring_buffer_lock_reserve, the length is the length of the data
1759 * and not the length of the event which would hold the header.
1761 int ring_buffer_write(struct ring_buffer *buffer,
1762 unsigned long length,
1765 struct ring_buffer_per_cpu *cpu_buffer;
1766 struct ring_buffer_event *event;
1767 unsigned long event_length;
1772 if (ring_buffer_flags != RB_BUFFERS_ON)
1775 if (atomic_read(&buffer->record_disabled))
1778 resched = ftrace_preempt_disable();
1780 cpu = raw_smp_processor_id();
1782 if (!cpumask_test_cpu(cpu, buffer->cpumask))
1785 cpu_buffer = buffer->buffers[cpu];
1787 if (atomic_read(&cpu_buffer->record_disabled))
1790 event_length = rb_calculate_event_length(length);
1791 event = rb_reserve_next_event(cpu_buffer, 0, event_length);
1795 body = rb_event_data(event);
1797 memcpy(body, data, length);
1799 rb_commit(cpu_buffer, event);
1803 ftrace_preempt_enable(resched);
1807 EXPORT_SYMBOL_GPL(ring_buffer_write);
1809 static int rb_per_cpu_empty(struct ring_buffer_per_cpu *cpu_buffer)
1811 struct buffer_page *reader = cpu_buffer->reader_page;
1812 struct buffer_page *head = cpu_buffer->head_page;
1813 struct buffer_page *commit = cpu_buffer->commit_page;
1815 return reader->read == rb_page_commit(reader) &&
1816 (commit == reader ||
1818 head->read == rb_page_commit(commit)));
1822 * ring_buffer_record_disable - stop all writes into the buffer
1823 * @buffer: The ring buffer to stop writes to.
1825 * This prevents all writes to the buffer. Any attempt to write
1826 * to the buffer after this will fail and return NULL.
1828 * The caller should call synchronize_sched() after this.
1830 void ring_buffer_record_disable(struct ring_buffer *buffer)
1832 atomic_inc(&buffer->record_disabled);
1834 EXPORT_SYMBOL_GPL(ring_buffer_record_disable);
1837 * ring_buffer_record_enable - enable writes to the buffer
1838 * @buffer: The ring buffer to enable writes
1840 * Note, multiple disables will need the same number of enables
1841 * to truely enable the writing (much like preempt_disable).
1843 void ring_buffer_record_enable(struct ring_buffer *buffer)
1845 atomic_dec(&buffer->record_disabled);
1847 EXPORT_SYMBOL_GPL(ring_buffer_record_enable);
1850 * ring_buffer_record_disable_cpu - stop all writes into the cpu_buffer
1851 * @buffer: The ring buffer to stop writes to.
1852 * @cpu: The CPU buffer to stop
1854 * This prevents all writes to the buffer. Any attempt to write
1855 * to the buffer after this will fail and return NULL.
1857 * The caller should call synchronize_sched() after this.
1859 void ring_buffer_record_disable_cpu(struct ring_buffer *buffer, int cpu)
1861 struct ring_buffer_per_cpu *cpu_buffer;
1863 if (!cpumask_test_cpu(cpu, buffer->cpumask))
1866 cpu_buffer = buffer->buffers[cpu];
1867 atomic_inc(&cpu_buffer->record_disabled);
1869 EXPORT_SYMBOL_GPL(ring_buffer_record_disable_cpu);
1872 * ring_buffer_record_enable_cpu - enable writes to the buffer
1873 * @buffer: The ring buffer to enable writes
1874 * @cpu: The CPU to enable.
1876 * Note, multiple disables will need the same number of enables
1877 * to truely enable the writing (much like preempt_disable).
1879 void ring_buffer_record_enable_cpu(struct ring_buffer *buffer, int cpu)
1881 struct ring_buffer_per_cpu *cpu_buffer;
1883 if (!cpumask_test_cpu(cpu, buffer->cpumask))
1886 cpu_buffer = buffer->buffers[cpu];
1887 atomic_dec(&cpu_buffer->record_disabled);
1889 EXPORT_SYMBOL_GPL(ring_buffer_record_enable_cpu);
1892 * ring_buffer_entries_cpu - get the number of entries in a cpu buffer
1893 * @buffer: The ring buffer
1894 * @cpu: The per CPU buffer to get the entries from.
1896 unsigned long ring_buffer_entries_cpu(struct ring_buffer *buffer, int cpu)
1898 struct ring_buffer_per_cpu *cpu_buffer;
1901 if (!cpumask_test_cpu(cpu, buffer->cpumask))
1904 cpu_buffer = buffer->buffers[cpu];
1905 ret = cpu_buffer->entries;
1909 EXPORT_SYMBOL_GPL(ring_buffer_entries_cpu);
1912 * ring_buffer_overrun_cpu - get the number of overruns in a cpu_buffer
1913 * @buffer: The ring buffer
1914 * @cpu: The per CPU buffer to get the number of overruns from
1916 unsigned long ring_buffer_overrun_cpu(struct ring_buffer *buffer, int cpu)
1918 struct ring_buffer_per_cpu *cpu_buffer;
1921 if (!cpumask_test_cpu(cpu, buffer->cpumask))
1924 cpu_buffer = buffer->buffers[cpu];
1925 ret = cpu_buffer->overrun;
1929 EXPORT_SYMBOL_GPL(ring_buffer_overrun_cpu);
1932 * ring_buffer_nmi_dropped_cpu - get the number of nmis that were dropped
1933 * @buffer: The ring buffer
1934 * @cpu: The per CPU buffer to get the number of overruns from
1936 unsigned long ring_buffer_nmi_dropped_cpu(struct ring_buffer *buffer, int cpu)
1938 struct ring_buffer_per_cpu *cpu_buffer;
1941 if (!cpumask_test_cpu(cpu, buffer->cpumask))
1944 cpu_buffer = buffer->buffers[cpu];
1945 ret = cpu_buffer->nmi_dropped;
1949 EXPORT_SYMBOL_GPL(ring_buffer_nmi_dropped_cpu);
1952 * ring_buffer_commit_overrun_cpu - get the number of overruns caused by commits
1953 * @buffer: The ring buffer
1954 * @cpu: The per CPU buffer to get the number of overruns from
1957 ring_buffer_commit_overrun_cpu(struct ring_buffer *buffer, int cpu)
1959 struct ring_buffer_per_cpu *cpu_buffer;
1962 if (!cpumask_test_cpu(cpu, buffer->cpumask))
1965 cpu_buffer = buffer->buffers[cpu];
1966 ret = cpu_buffer->commit_overrun;
1970 EXPORT_SYMBOL_GPL(ring_buffer_commit_overrun_cpu);
1973 * ring_buffer_entries - get the number of entries in a buffer
1974 * @buffer: The ring buffer
1976 * Returns the total number of entries in the ring buffer
1979 unsigned long ring_buffer_entries(struct ring_buffer *buffer)
1981 struct ring_buffer_per_cpu *cpu_buffer;
1982 unsigned long entries = 0;
1985 /* if you care about this being correct, lock the buffer */
1986 for_each_buffer_cpu(buffer, cpu) {
1987 cpu_buffer = buffer->buffers[cpu];
1988 entries += cpu_buffer->entries;
1993 EXPORT_SYMBOL_GPL(ring_buffer_entries);
1996 * ring_buffer_overrun_cpu - get the number of overruns in buffer
1997 * @buffer: The ring buffer
1999 * Returns the total number of overruns in the ring buffer
2002 unsigned long ring_buffer_overruns(struct ring_buffer *buffer)
2004 struct ring_buffer_per_cpu *cpu_buffer;
2005 unsigned long overruns = 0;
2008 /* if you care about this being correct, lock the buffer */
2009 for_each_buffer_cpu(buffer, cpu) {
2010 cpu_buffer = buffer->buffers[cpu];
2011 overruns += cpu_buffer->overrun;
2016 EXPORT_SYMBOL_GPL(ring_buffer_overruns);
2018 static void rb_iter_reset(struct ring_buffer_iter *iter)
2020 struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
2022 /* Iterator usage is expected to have record disabled */
2023 if (list_empty(&cpu_buffer->reader_page->list)) {
2024 iter->head_page = cpu_buffer->head_page;
2025 iter->head = cpu_buffer->head_page->read;
2027 iter->head_page = cpu_buffer->reader_page;
2028 iter->head = cpu_buffer->reader_page->read;
2031 iter->read_stamp = cpu_buffer->read_stamp;
2033 iter->read_stamp = iter->head_page->page->time_stamp;
2037 * ring_buffer_iter_reset - reset an iterator
2038 * @iter: The iterator to reset
2040 * Resets the iterator, so that it will start from the beginning
2043 void ring_buffer_iter_reset(struct ring_buffer_iter *iter)
2045 struct ring_buffer_per_cpu *cpu_buffer;
2046 unsigned long flags;
2051 cpu_buffer = iter->cpu_buffer;
2053 spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
2054 rb_iter_reset(iter);
2055 spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
2057 EXPORT_SYMBOL_GPL(ring_buffer_iter_reset);
2060 * ring_buffer_iter_empty - check if an iterator has no more to read
2061 * @iter: The iterator to check
2063 int ring_buffer_iter_empty(struct ring_buffer_iter *iter)
2065 struct ring_buffer_per_cpu *cpu_buffer;
2067 cpu_buffer = iter->cpu_buffer;
2069 return iter->head_page == cpu_buffer->commit_page &&
2070 iter->head == rb_commit_index(cpu_buffer);
2072 EXPORT_SYMBOL_GPL(ring_buffer_iter_empty);
2075 rb_update_read_stamp(struct ring_buffer_per_cpu *cpu_buffer,
2076 struct ring_buffer_event *event)
2080 switch (event->type_len) {
2081 case RINGBUF_TYPE_PADDING:
2084 case RINGBUF_TYPE_TIME_EXTEND:
2085 delta = event->array[0];
2087 delta += event->time_delta;
2088 cpu_buffer->read_stamp += delta;
2091 case RINGBUF_TYPE_TIME_STAMP:
2092 /* FIXME: not implemented */
2095 case RINGBUF_TYPE_DATA:
2096 cpu_buffer->read_stamp += event->time_delta;
2106 rb_update_iter_read_stamp(struct ring_buffer_iter *iter,
2107 struct ring_buffer_event *event)
2111 switch (event->type_len) {
2112 case RINGBUF_TYPE_PADDING:
2115 case RINGBUF_TYPE_TIME_EXTEND:
2116 delta = event->array[0];
2118 delta += event->time_delta;
2119 iter->read_stamp += delta;
2122 case RINGBUF_TYPE_TIME_STAMP:
2123 /* FIXME: not implemented */
2126 case RINGBUF_TYPE_DATA:
2127 iter->read_stamp += event->time_delta;
2136 static struct buffer_page *
2137 rb_get_reader_page(struct ring_buffer_per_cpu *cpu_buffer)
2139 struct buffer_page *reader = NULL;
2140 unsigned long flags;
2143 local_irq_save(flags);
2144 __raw_spin_lock(&cpu_buffer->lock);
2148 * This should normally only loop twice. But because the
2149 * start of the reader inserts an empty page, it causes
2150 * a case where we will loop three times. There should be no
2151 * reason to loop four times (that I know of).
2153 if (RB_WARN_ON(cpu_buffer, ++nr_loops > 3)) {
2158 reader = cpu_buffer->reader_page;
2160 /* If there's more to read, return this page */
2161 if (cpu_buffer->reader_page->read < rb_page_size(reader))
2164 /* Never should we have an index greater than the size */
2165 if (RB_WARN_ON(cpu_buffer,
2166 cpu_buffer->reader_page->read > rb_page_size(reader)))
2169 /* check if we caught up to the tail */
2171 if (cpu_buffer->commit_page == cpu_buffer->reader_page)
2175 * Splice the empty reader page into the list around the head.
2176 * Reset the reader page to size zero.
2179 reader = cpu_buffer->head_page;
2180 cpu_buffer->reader_page->list.next = reader->list.next;
2181 cpu_buffer->reader_page->list.prev = reader->list.prev;
2183 local_set(&cpu_buffer->reader_page->write, 0);
2184 local_set(&cpu_buffer->reader_page->page->commit, 0);
2186 /* Make the reader page now replace the head */
2187 reader->list.prev->next = &cpu_buffer->reader_page->list;
2188 reader->list.next->prev = &cpu_buffer->reader_page->list;
2191 * If the tail is on the reader, then we must set the head
2192 * to the inserted page, otherwise we set it one before.
2194 cpu_buffer->head_page = cpu_buffer->reader_page;
2196 if (cpu_buffer->commit_page != reader)
2197 rb_inc_page(cpu_buffer, &cpu_buffer->head_page);
2199 /* Finally update the reader page to the new head */
2200 cpu_buffer->reader_page = reader;
2201 rb_reset_reader_page(cpu_buffer);
2206 __raw_spin_unlock(&cpu_buffer->lock);
2207 local_irq_restore(flags);
2212 static void rb_advance_reader(struct ring_buffer_per_cpu *cpu_buffer)
2214 struct ring_buffer_event *event;
2215 struct buffer_page *reader;
2218 reader = rb_get_reader_page(cpu_buffer);
2220 /* This function should not be called when buffer is empty */
2221 if (RB_WARN_ON(cpu_buffer, !reader))
2224 event = rb_reader_event(cpu_buffer);
2226 if (event->type_len <= RINGBUF_TYPE_DATA_TYPE_LEN_MAX
2227 || rb_discarded_event(event))
2228 cpu_buffer->entries--;
2230 rb_update_read_stamp(cpu_buffer, event);
2232 length = rb_event_length(event);
2233 cpu_buffer->reader_page->read += length;
2236 static void rb_advance_iter(struct ring_buffer_iter *iter)
2238 struct ring_buffer *buffer;
2239 struct ring_buffer_per_cpu *cpu_buffer;
2240 struct ring_buffer_event *event;
2243 cpu_buffer = iter->cpu_buffer;
2244 buffer = cpu_buffer->buffer;
2247 * Check if we are at the end of the buffer.
2249 if (iter->head >= rb_page_size(iter->head_page)) {
2250 if (RB_WARN_ON(buffer,
2251 iter->head_page == cpu_buffer->commit_page))
2257 event = rb_iter_head_event(iter);
2259 length = rb_event_length(event);
2262 * This should not be called to advance the header if we are
2263 * at the tail of the buffer.
2265 if (RB_WARN_ON(cpu_buffer,
2266 (iter->head_page == cpu_buffer->commit_page) &&
2267 (iter->head + length > rb_commit_index(cpu_buffer))))
2270 rb_update_iter_read_stamp(iter, event);
2272 iter->head += length;
2274 /* check for end of page padding */
2275 if ((iter->head >= rb_page_size(iter->head_page)) &&
2276 (iter->head_page != cpu_buffer->commit_page))
2277 rb_advance_iter(iter);
2280 static struct ring_buffer_event *
2281 rb_buffer_peek(struct ring_buffer *buffer, int cpu, u64 *ts)
2283 struct ring_buffer_per_cpu *cpu_buffer;
2284 struct ring_buffer_event *event;
2285 struct buffer_page *reader;
2288 cpu_buffer = buffer->buffers[cpu];
2292 * We repeat when a timestamp is encountered. It is possible
2293 * to get multiple timestamps from an interrupt entering just
2294 * as one timestamp is about to be written. The max times
2295 * that this can happen is the number of nested interrupts we
2296 * can have. Nesting 10 deep of interrupts is clearly
2299 if (RB_WARN_ON(cpu_buffer, ++nr_loops > 10))
2302 reader = rb_get_reader_page(cpu_buffer);
2306 event = rb_reader_event(cpu_buffer);
2308 switch (event->type_len) {
2309 case RINGBUF_TYPE_PADDING:
2310 if (rb_null_event(event))
2311 RB_WARN_ON(cpu_buffer, 1);
2313 * Because the writer could be discarding every
2314 * event it creates (which would probably be bad)
2315 * if we were to go back to "again" then we may never
2316 * catch up, and will trigger the warn on, or lock
2317 * the box. Return the padding, and we will release
2318 * the current locks, and try again.
2320 rb_advance_reader(cpu_buffer);
2323 case RINGBUF_TYPE_TIME_EXTEND:
2324 /* Internal data, OK to advance */
2325 rb_advance_reader(cpu_buffer);
2328 case RINGBUF_TYPE_TIME_STAMP:
2329 /* FIXME: not implemented */
2330 rb_advance_reader(cpu_buffer);
2333 case RINGBUF_TYPE_DATA:
2335 *ts = cpu_buffer->read_stamp + event->time_delta;
2336 ring_buffer_normalize_time_stamp(buffer,
2337 cpu_buffer->cpu, ts);
2347 EXPORT_SYMBOL_GPL(ring_buffer_peek);
2349 static struct ring_buffer_event *
2350 rb_iter_peek(struct ring_buffer_iter *iter, u64 *ts)
2352 struct ring_buffer *buffer;
2353 struct ring_buffer_per_cpu *cpu_buffer;
2354 struct ring_buffer_event *event;
2357 if (ring_buffer_iter_empty(iter))
2360 cpu_buffer = iter->cpu_buffer;
2361 buffer = cpu_buffer->buffer;
2365 * We repeat when a timestamp is encountered. It is possible
2366 * to get multiple timestamps from an interrupt entering just
2367 * as one timestamp is about to be written. The max times
2368 * that this can happen is the number of nested interrupts we
2369 * can have. Nesting 10 deep of interrupts is clearly
2372 if (RB_WARN_ON(cpu_buffer, ++nr_loops > 10))
2375 if (rb_per_cpu_empty(cpu_buffer))
2378 event = rb_iter_head_event(iter);
2380 switch (event->type_len) {
2381 case RINGBUF_TYPE_PADDING:
2382 if (rb_null_event(event)) {
2386 rb_advance_iter(iter);
2389 case RINGBUF_TYPE_TIME_EXTEND:
2390 /* Internal data, OK to advance */
2391 rb_advance_iter(iter);
2394 case RINGBUF_TYPE_TIME_STAMP:
2395 /* FIXME: not implemented */
2396 rb_advance_iter(iter);
2399 case RINGBUF_TYPE_DATA:
2401 *ts = iter->read_stamp + event->time_delta;
2402 ring_buffer_normalize_time_stamp(buffer,
2403 cpu_buffer->cpu, ts);
2413 EXPORT_SYMBOL_GPL(ring_buffer_iter_peek);
2416 * ring_buffer_peek - peek at the next event to be read
2417 * @buffer: The ring buffer to read
2418 * @cpu: The cpu to peak at
2419 * @ts: The timestamp counter of this event.
2421 * This will return the event that will be read next, but does
2422 * not consume the data.
2424 struct ring_buffer_event *
2425 ring_buffer_peek(struct ring_buffer *buffer, int cpu, u64 *ts)
2427 struct ring_buffer_per_cpu *cpu_buffer = buffer->buffers[cpu];
2428 struct ring_buffer_event *event;
2429 unsigned long flags;
2431 if (!cpumask_test_cpu(cpu, buffer->cpumask))
2435 spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
2436 event = rb_buffer_peek(buffer, cpu, ts);
2437 spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
2439 if (event && event->type_len == RINGBUF_TYPE_PADDING) {
2448 * ring_buffer_iter_peek - peek at the next event to be read
2449 * @iter: The ring buffer iterator
2450 * @ts: The timestamp counter of this event.
2452 * This will return the event that will be read next, but does
2453 * not increment the iterator.
2455 struct ring_buffer_event *
2456 ring_buffer_iter_peek(struct ring_buffer_iter *iter, u64 *ts)
2458 struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
2459 struct ring_buffer_event *event;
2460 unsigned long flags;
2463 spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
2464 event = rb_iter_peek(iter, ts);
2465 spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
2467 if (event && event->type_len == RINGBUF_TYPE_PADDING) {
2476 * ring_buffer_consume - return an event and consume it
2477 * @buffer: The ring buffer to get the next event from
2479 * Returns the next event in the ring buffer, and that event is consumed.
2480 * Meaning, that sequential reads will keep returning a different event,
2481 * and eventually empty the ring buffer if the producer is slower.
2483 struct ring_buffer_event *
2484 ring_buffer_consume(struct ring_buffer *buffer, int cpu, u64 *ts)
2486 struct ring_buffer_per_cpu *cpu_buffer;
2487 struct ring_buffer_event *event = NULL;
2488 unsigned long flags;
2491 /* might be called in atomic */
2494 if (!cpumask_test_cpu(cpu, buffer->cpumask))
2497 cpu_buffer = buffer->buffers[cpu];
2498 spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
2500 event = rb_buffer_peek(buffer, cpu, ts);
2504 rb_advance_reader(cpu_buffer);
2507 spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
2512 if (event && event->type_len == RINGBUF_TYPE_PADDING) {
2519 EXPORT_SYMBOL_GPL(ring_buffer_consume);
2522 * ring_buffer_read_start - start a non consuming read of the buffer
2523 * @buffer: The ring buffer to read from
2524 * @cpu: The cpu buffer to iterate over
2526 * This starts up an iteration through the buffer. It also disables
2527 * the recording to the buffer until the reading is finished.
2528 * This prevents the reading from being corrupted. This is not
2529 * a consuming read, so a producer is not expected.
2531 * Must be paired with ring_buffer_finish.
2533 struct ring_buffer_iter *
2534 ring_buffer_read_start(struct ring_buffer *buffer, int cpu)
2536 struct ring_buffer_per_cpu *cpu_buffer;
2537 struct ring_buffer_iter *iter;
2538 unsigned long flags;
2540 if (!cpumask_test_cpu(cpu, buffer->cpumask))
2543 iter = kmalloc(sizeof(*iter), GFP_KERNEL);
2547 cpu_buffer = buffer->buffers[cpu];
2549 iter->cpu_buffer = cpu_buffer;
2551 atomic_inc(&cpu_buffer->record_disabled);
2552 synchronize_sched();
2554 spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
2555 __raw_spin_lock(&cpu_buffer->lock);
2556 rb_iter_reset(iter);
2557 __raw_spin_unlock(&cpu_buffer->lock);
2558 spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
2562 EXPORT_SYMBOL_GPL(ring_buffer_read_start);
2565 * ring_buffer_finish - finish reading the iterator of the buffer
2566 * @iter: The iterator retrieved by ring_buffer_start
2568 * This re-enables the recording to the buffer, and frees the
2572 ring_buffer_read_finish(struct ring_buffer_iter *iter)
2574 struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
2576 atomic_dec(&cpu_buffer->record_disabled);
2579 EXPORT_SYMBOL_GPL(ring_buffer_read_finish);
2582 * ring_buffer_read - read the next item in the ring buffer by the iterator
2583 * @iter: The ring buffer iterator
2584 * @ts: The time stamp of the event read.
2586 * This reads the next event in the ring buffer and increments the iterator.
2588 struct ring_buffer_event *
2589 ring_buffer_read(struct ring_buffer_iter *iter, u64 *ts)
2591 struct ring_buffer_event *event;
2592 struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
2593 unsigned long flags;
2596 spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
2597 event = rb_iter_peek(iter, ts);
2601 rb_advance_iter(iter);
2603 spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
2605 if (event && event->type_len == RINGBUF_TYPE_PADDING) {
2612 EXPORT_SYMBOL_GPL(ring_buffer_read);
2615 * ring_buffer_size - return the size of the ring buffer (in bytes)
2616 * @buffer: The ring buffer.
2618 unsigned long ring_buffer_size(struct ring_buffer *buffer)
2620 return BUF_PAGE_SIZE * buffer->pages;
2622 EXPORT_SYMBOL_GPL(ring_buffer_size);
2625 rb_reset_cpu(struct ring_buffer_per_cpu *cpu_buffer)
2627 cpu_buffer->head_page
2628 = list_entry(cpu_buffer->pages.next, struct buffer_page, list);
2629 local_set(&cpu_buffer->head_page->write, 0);
2630 local_set(&cpu_buffer->head_page->page->commit, 0);
2632 cpu_buffer->head_page->read = 0;
2634 cpu_buffer->tail_page = cpu_buffer->head_page;
2635 cpu_buffer->commit_page = cpu_buffer->head_page;
2637 INIT_LIST_HEAD(&cpu_buffer->reader_page->list);
2638 local_set(&cpu_buffer->reader_page->write, 0);
2639 local_set(&cpu_buffer->reader_page->page->commit, 0);
2640 cpu_buffer->reader_page->read = 0;
2642 cpu_buffer->nmi_dropped = 0;
2643 cpu_buffer->commit_overrun = 0;
2644 cpu_buffer->overrun = 0;
2645 cpu_buffer->entries = 0;
2647 cpu_buffer->write_stamp = 0;
2648 cpu_buffer->read_stamp = 0;
2652 * ring_buffer_reset_cpu - reset a ring buffer per CPU buffer
2653 * @buffer: The ring buffer to reset a per cpu buffer of
2654 * @cpu: The CPU buffer to be reset
2656 void ring_buffer_reset_cpu(struct ring_buffer *buffer, int cpu)
2658 struct ring_buffer_per_cpu *cpu_buffer = buffer->buffers[cpu];
2659 unsigned long flags;
2661 if (!cpumask_test_cpu(cpu, buffer->cpumask))
2664 spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
2666 __raw_spin_lock(&cpu_buffer->lock);
2668 rb_reset_cpu(cpu_buffer);
2670 __raw_spin_unlock(&cpu_buffer->lock);
2672 spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
2674 EXPORT_SYMBOL_GPL(ring_buffer_reset_cpu);
2677 * ring_buffer_reset - reset a ring buffer
2678 * @buffer: The ring buffer to reset all cpu buffers
2680 void ring_buffer_reset(struct ring_buffer *buffer)
2684 for_each_buffer_cpu(buffer, cpu)
2685 ring_buffer_reset_cpu(buffer, cpu);
2687 EXPORT_SYMBOL_GPL(ring_buffer_reset);
2690 * rind_buffer_empty - is the ring buffer empty?
2691 * @buffer: The ring buffer to test
2693 int ring_buffer_empty(struct ring_buffer *buffer)
2695 struct ring_buffer_per_cpu *cpu_buffer;
2698 /* yes this is racy, but if you don't like the race, lock the buffer */
2699 for_each_buffer_cpu(buffer, cpu) {
2700 cpu_buffer = buffer->buffers[cpu];
2701 if (!rb_per_cpu_empty(cpu_buffer))
2707 EXPORT_SYMBOL_GPL(ring_buffer_empty);
2710 * ring_buffer_empty_cpu - is a cpu buffer of a ring buffer empty?
2711 * @buffer: The ring buffer
2712 * @cpu: The CPU buffer to test
2714 int ring_buffer_empty_cpu(struct ring_buffer *buffer, int cpu)
2716 struct ring_buffer_per_cpu *cpu_buffer;
2719 if (!cpumask_test_cpu(cpu, buffer->cpumask))
2722 cpu_buffer = buffer->buffers[cpu];
2723 ret = rb_per_cpu_empty(cpu_buffer);
2728 EXPORT_SYMBOL_GPL(ring_buffer_empty_cpu);
2731 * ring_buffer_swap_cpu - swap a CPU buffer between two ring buffers
2732 * @buffer_a: One buffer to swap with
2733 * @buffer_b: The other buffer to swap with
2735 * This function is useful for tracers that want to take a "snapshot"
2736 * of a CPU buffer and has another back up buffer lying around.
2737 * it is expected that the tracer handles the cpu buffer not being
2738 * used at the moment.
2740 int ring_buffer_swap_cpu(struct ring_buffer *buffer_a,
2741 struct ring_buffer *buffer_b, int cpu)
2743 struct ring_buffer_per_cpu *cpu_buffer_a;
2744 struct ring_buffer_per_cpu *cpu_buffer_b;
2747 if (!cpumask_test_cpu(cpu, buffer_a->cpumask) ||
2748 !cpumask_test_cpu(cpu, buffer_b->cpumask))
2751 /* At least make sure the two buffers are somewhat the same */
2752 if (buffer_a->pages != buffer_b->pages)
2757 if (ring_buffer_flags != RB_BUFFERS_ON)
2760 if (atomic_read(&buffer_a->record_disabled))
2763 if (atomic_read(&buffer_b->record_disabled))
2766 cpu_buffer_a = buffer_a->buffers[cpu];
2767 cpu_buffer_b = buffer_b->buffers[cpu];
2769 if (atomic_read(&cpu_buffer_a->record_disabled))
2772 if (atomic_read(&cpu_buffer_b->record_disabled))
2776 * We can't do a synchronize_sched here because this
2777 * function can be called in atomic context.
2778 * Normally this will be called from the same CPU as cpu.
2779 * If not it's up to the caller to protect this.
2781 atomic_inc(&cpu_buffer_a->record_disabled);
2782 atomic_inc(&cpu_buffer_b->record_disabled);
2784 buffer_a->buffers[cpu] = cpu_buffer_b;
2785 buffer_b->buffers[cpu] = cpu_buffer_a;
2787 cpu_buffer_b->buffer = buffer_a;
2788 cpu_buffer_a->buffer = buffer_b;
2790 atomic_dec(&cpu_buffer_a->record_disabled);
2791 atomic_dec(&cpu_buffer_b->record_disabled);
2797 EXPORT_SYMBOL_GPL(ring_buffer_swap_cpu);
2799 static void rb_remove_entries(struct ring_buffer_per_cpu *cpu_buffer,
2800 struct buffer_data_page *bpage,
2801 unsigned int offset)
2803 struct ring_buffer_event *event;
2806 __raw_spin_lock(&cpu_buffer->lock);
2807 for (head = offset; head < local_read(&bpage->commit);
2808 head += rb_event_length(event)) {
2810 event = __rb_data_page_index(bpage, head);
2811 if (RB_WARN_ON(cpu_buffer, rb_null_event(event)))
2813 /* Only count data entries */
2814 if (event->type_len > RINGBUF_TYPE_DATA_TYPE_LEN_MAX)
2816 cpu_buffer->entries--;
2818 __raw_spin_unlock(&cpu_buffer->lock);
2822 * ring_buffer_alloc_read_page - allocate a page to read from buffer
2823 * @buffer: the buffer to allocate for.
2825 * This function is used in conjunction with ring_buffer_read_page.
2826 * When reading a full page from the ring buffer, these functions
2827 * can be used to speed up the process. The calling function should
2828 * allocate a few pages first with this function. Then when it
2829 * needs to get pages from the ring buffer, it passes the result
2830 * of this function into ring_buffer_read_page, which will swap
2831 * the page that was allocated, with the read page of the buffer.
2834 * The page allocated, or NULL on error.
2836 void *ring_buffer_alloc_read_page(struct ring_buffer *buffer)
2838 struct buffer_data_page *bpage;
2841 addr = __get_free_page(GFP_KERNEL);
2845 bpage = (void *)addr;
2847 rb_init_page(bpage);
2851 EXPORT_SYMBOL_GPL(ring_buffer_alloc_read_page);
2854 * ring_buffer_free_read_page - free an allocated read page
2855 * @buffer: the buffer the page was allocate for
2856 * @data: the page to free
2858 * Free a page allocated from ring_buffer_alloc_read_page.
2860 void ring_buffer_free_read_page(struct ring_buffer *buffer, void *data)
2862 free_page((unsigned long)data);
2864 EXPORT_SYMBOL_GPL(ring_buffer_free_read_page);
2867 * ring_buffer_read_page - extract a page from the ring buffer
2868 * @buffer: buffer to extract from
2869 * @data_page: the page to use allocated from ring_buffer_alloc_read_page
2870 * @len: amount to extract
2871 * @cpu: the cpu of the buffer to extract
2872 * @full: should the extraction only happen when the page is full.
2874 * This function will pull out a page from the ring buffer and consume it.
2875 * @data_page must be the address of the variable that was returned
2876 * from ring_buffer_alloc_read_page. This is because the page might be used
2877 * to swap with a page in the ring buffer.
2880 * rpage = ring_buffer_alloc_read_page(buffer);
2883 * ret = ring_buffer_read_page(buffer, &rpage, len, cpu, 0);
2885 * process_page(rpage, ret);
2887 * When @full is set, the function will not return true unless
2888 * the writer is off the reader page.
2890 * Note: it is up to the calling functions to handle sleeps and wakeups.
2891 * The ring buffer can be used anywhere in the kernel and can not
2892 * blindly call wake_up. The layer that uses the ring buffer must be
2893 * responsible for that.
2896 * >=0 if data has been transferred, returns the offset of consumed data.
2897 * <0 if no data has been transferred.
2899 int ring_buffer_read_page(struct ring_buffer *buffer,
2900 void **data_page, size_t len, int cpu, int full)
2902 struct ring_buffer_per_cpu *cpu_buffer = buffer->buffers[cpu];
2903 struct ring_buffer_event *event;
2904 struct buffer_data_page *bpage;
2905 struct buffer_page *reader;
2906 unsigned long flags;
2907 unsigned int commit;
2912 if (!cpumask_test_cpu(cpu, buffer->cpumask))
2916 * If len is not big enough to hold the page header, then
2917 * we can not copy anything.
2919 if (len <= BUF_PAGE_HDR_SIZE)
2922 len -= BUF_PAGE_HDR_SIZE;
2931 spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
2933 reader = rb_get_reader_page(cpu_buffer);
2937 event = rb_reader_event(cpu_buffer);
2939 read = reader->read;
2940 commit = rb_page_commit(reader);
2943 * If this page has been partially read or
2944 * if len is not big enough to read the rest of the page or
2945 * a writer is still on the page, then
2946 * we must copy the data from the page to the buffer.
2947 * Otherwise, we can simply swap the page with the one passed in.
2949 if (read || (len < (commit - read)) ||
2950 cpu_buffer->reader_page == cpu_buffer->commit_page) {
2951 struct buffer_data_page *rpage = cpu_buffer->reader_page->page;
2952 unsigned int rpos = read;
2953 unsigned int pos = 0;
2959 if (len > (commit - read))
2960 len = (commit - read);
2962 size = rb_event_length(event);
2967 /* save the current timestamp, since the user will need it */
2968 save_timestamp = cpu_buffer->read_stamp;
2970 /* Need to copy one event at a time */
2972 memcpy(bpage->data + pos, rpage->data + rpos, size);
2976 rb_advance_reader(cpu_buffer);
2977 rpos = reader->read;
2980 event = rb_reader_event(cpu_buffer);
2981 size = rb_event_length(event);
2982 } while (len > size);
2985 local_set(&bpage->commit, pos);
2986 bpage->time_stamp = save_timestamp;
2988 /* we copied everything to the beginning */
2991 /* swap the pages */
2992 rb_init_page(bpage);
2993 bpage = reader->page;
2994 reader->page = *data_page;
2995 local_set(&reader->write, 0);
2999 /* update the entry counter */
3000 rb_remove_entries(cpu_buffer, bpage, read);
3005 spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
3010 EXPORT_SYMBOL_GPL(ring_buffer_read_page);
3013 rb_simple_read(struct file *filp, char __user *ubuf,
3014 size_t cnt, loff_t *ppos)
3016 unsigned long *p = filp->private_data;
3020 if (test_bit(RB_BUFFERS_DISABLED_BIT, p))
3021 r = sprintf(buf, "permanently disabled\n");
3023 r = sprintf(buf, "%d\n", test_bit(RB_BUFFERS_ON_BIT, p));
3025 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
3029 rb_simple_write(struct file *filp, const char __user *ubuf,
3030 size_t cnt, loff_t *ppos)
3032 unsigned long *p = filp->private_data;
3037 if (cnt >= sizeof(buf))
3040 if (copy_from_user(&buf, ubuf, cnt))
3045 ret = strict_strtoul(buf, 10, &val);
3050 set_bit(RB_BUFFERS_ON_BIT, p);
3052 clear_bit(RB_BUFFERS_ON_BIT, p);
3059 static const struct file_operations rb_simple_fops = {
3060 .open = tracing_open_generic,
3061 .read = rb_simple_read,
3062 .write = rb_simple_write,
3066 static __init int rb_init_debugfs(void)
3068 struct dentry *d_tracer;
3070 d_tracer = tracing_init_dentry();
3072 trace_create_file("tracing_on", 0644, d_tracer,
3073 &ring_buffer_flags, &rb_simple_fops);
3078 fs_initcall(rb_init_debugfs);
3080 #ifdef CONFIG_HOTPLUG_CPU
3081 static int rb_cpu_notify(struct notifier_block *self,
3082 unsigned long action, void *hcpu)
3084 struct ring_buffer *buffer =
3085 container_of(self, struct ring_buffer, cpu_notify);
3086 long cpu = (long)hcpu;
3089 case CPU_UP_PREPARE:
3090 case CPU_UP_PREPARE_FROZEN:
3091 if (cpu_isset(cpu, *buffer->cpumask))
3094 buffer->buffers[cpu] =
3095 rb_allocate_cpu_buffer(buffer, cpu);
3096 if (!buffer->buffers[cpu]) {
3097 WARN(1, "failed to allocate ring buffer on CPU %ld\n",
3102 cpu_set(cpu, *buffer->cpumask);
3104 case CPU_DOWN_PREPARE:
3105 case CPU_DOWN_PREPARE_FROZEN:
3108 * If we were to free the buffer, then the user would
3109 * lose any trace that was in the buffer.