4 * Copyright (C) 2008 Steven Rostedt <srostedt@redhat.com>
6 #include <linux/ring_buffer.h>
7 #include <linux/trace_clock.h>
8 #include <linux/ftrace_irq.h>
9 #include <linux/spinlock.h>
10 #include <linux/debugfs.h>
11 #include <linux/uaccess.h>
12 #include <linux/hardirq.h>
13 #include <linux/module.h>
14 #include <linux/percpu.h>
15 #include <linux/mutex.h>
16 #include <linux/init.h>
17 #include <linux/hash.h>
18 #include <linux/list.h>
19 #include <linux/cpu.h>
25 * The ring buffer is made up of a list of pages. A separate list of pages is
26 * allocated for each CPU. A writer may only write to a buffer that is
27 * associated with the CPU it is currently executing on. A reader may read
28 * from any per cpu buffer.
30 * The reader is special. For each per cpu buffer, the reader has its own
31 * reader page. When a reader has read the entire reader page, this reader
32 * page is swapped with another page in the ring buffer.
34 * Now, as long as the writer is off the reader page, the reader can do what
35 * ever it wants with that page. The writer will never write to that page
36 * again (as long as it is out of the ring buffer).
38 * Here's some silly ASCII art.
41 * |reader| RING BUFFER
43 * +------+ +---+ +---+ +---+
52 * |reader| RING BUFFER
53 * |page |------------------v
54 * +------+ +---+ +---+ +---+
63 * |reader| RING BUFFER
64 * |page |------------------v
65 * +------+ +---+ +---+ +---+
70 * +------------------------------+
74 * |buffer| RING BUFFER
75 * |page |------------------v
76 * +------+ +---+ +---+ +---+
78 * | New +---+ +---+ +---+
81 * +------------------------------+
84 * After we make this swap, the reader can hand this page off to the splice
85 * code and be done with it. It can even allocate a new page if it needs to
86 * and swap that into the ring buffer.
88 * We will be using cmpxchg soon to make all this lockless.
93 * A fast way to enable or disable all ring buffers is to
94 * call tracing_on or tracing_off. Turning off the ring buffers
95 * prevents all ring buffers from being recorded to.
96 * Turning this switch on, makes it OK to write to the
97 * ring buffer, if the ring buffer is enabled itself.
99 * There's three layers that must be on in order to write
100 * to the ring buffer.
102 * 1) This global flag must be set.
103 * 2) The ring buffer must be enabled for recording.
104 * 3) The per cpu buffer must be enabled for recording.
106 * In case of an anomaly, this global flag has a bit set that
107 * will permantly disable all ring buffers.
111 * Global flag to disable all recording to ring buffers
112 * This has two bits: ON, DISABLED
116 * 0 0 : ring buffers are off
117 * 1 0 : ring buffers are on
118 * X 1 : ring buffers are permanently disabled
122 RB_BUFFERS_ON_BIT
= 0,
123 RB_BUFFERS_DISABLED_BIT
= 1,
127 RB_BUFFERS_ON
= 1 << RB_BUFFERS_ON_BIT
,
128 RB_BUFFERS_DISABLED
= 1 << RB_BUFFERS_DISABLED_BIT
,
131 static unsigned long ring_buffer_flags __read_mostly
= RB_BUFFERS_ON
;
133 #define BUF_PAGE_HDR_SIZE offsetof(struct buffer_data_page, data)
136 * tracing_on - enable all tracing buffers
138 * This function enables all tracing buffers that may have been
139 * disabled with tracing_off.
141 void tracing_on(void)
143 set_bit(RB_BUFFERS_ON_BIT
, &ring_buffer_flags
);
145 EXPORT_SYMBOL_GPL(tracing_on
);
148 * tracing_off - turn off all tracing buffers
150 * This function stops all tracing buffers from recording data.
151 * It does not disable any overhead the tracers themselves may
152 * be causing. This function simply causes all recording to
153 * the ring buffers to fail.
155 void tracing_off(void)
157 clear_bit(RB_BUFFERS_ON_BIT
, &ring_buffer_flags
);
159 EXPORT_SYMBOL_GPL(tracing_off
);
162 * tracing_off_permanent - permanently disable ring buffers
164 * This function, once called, will disable all ring buffers
167 void tracing_off_permanent(void)
169 set_bit(RB_BUFFERS_DISABLED_BIT
, &ring_buffer_flags
);
173 * tracing_is_on - show state of ring buffers enabled
175 int tracing_is_on(void)
177 return ring_buffer_flags
== RB_BUFFERS_ON
;
179 EXPORT_SYMBOL_GPL(tracing_is_on
);
183 #define RB_EVNT_HDR_SIZE (offsetof(struct ring_buffer_event, array))
184 #define RB_ALIGNMENT 4U
185 #define RB_MAX_SMALL_DATA 28
188 RB_LEN_TIME_EXTEND
= 8,
189 RB_LEN_TIME_STAMP
= 16,
192 static inline int rb_null_event(struct ring_buffer_event
*event
)
194 return event
->type
== RINGBUF_TYPE_PADDING
&& event
->time_delta
== 0;
197 static inline int rb_discarded_event(struct ring_buffer_event
*event
)
199 return event
->type
== RINGBUF_TYPE_PADDING
&& event
->time_delta
;
202 static void rb_event_set_padding(struct ring_buffer_event
*event
)
204 event
->type
= RINGBUF_TYPE_PADDING
;
205 event
->time_delta
= 0;
209 * ring_buffer_event_discard - discard an event in the ring buffer
210 * @buffer: the ring buffer
211 * @event: the event to discard
213 * Sometimes a event that is in the ring buffer needs to be ignored.
214 * This function lets the user discard an event in the ring buffer
215 * and then that event will not be read later.
217 * Note, it is up to the user to be careful with this, and protect
218 * against races. If the user discards an event that has been consumed
219 * it is possible that it could corrupt the ring buffer.
221 void ring_buffer_event_discard(struct ring_buffer_event
*event
)
223 event
->type
= RINGBUF_TYPE_PADDING
;
224 /* time delta must be non zero */
225 if (!event
->time_delta
)
226 event
->time_delta
= 1;
230 rb_event_data_length(struct ring_buffer_event
*event
)
235 length
= event
->len
* RB_ALIGNMENT
;
237 length
= event
->array
[0];
238 return length
+ RB_EVNT_HDR_SIZE
;
241 /* inline for ring buffer fast paths */
243 rb_event_length(struct ring_buffer_event
*event
)
245 switch (event
->type
) {
246 case RINGBUF_TYPE_PADDING
:
247 if (rb_null_event(event
))
250 return rb_event_data_length(event
);
252 case RINGBUF_TYPE_TIME_EXTEND
:
253 return RB_LEN_TIME_EXTEND
;
255 case RINGBUF_TYPE_TIME_STAMP
:
256 return RB_LEN_TIME_STAMP
;
258 case RINGBUF_TYPE_DATA
:
259 return rb_event_data_length(event
);
268 * ring_buffer_event_length - return the length of the event
269 * @event: the event to get the length of
271 unsigned ring_buffer_event_length(struct ring_buffer_event
*event
)
273 unsigned length
= rb_event_length(event
);
274 if (event
->type
!= RINGBUF_TYPE_DATA
)
276 length
-= RB_EVNT_HDR_SIZE
;
277 if (length
> RB_MAX_SMALL_DATA
+ sizeof(event
->array
[0]))
278 length
-= sizeof(event
->array
[0]);
281 EXPORT_SYMBOL_GPL(ring_buffer_event_length
);
283 /* inline for ring buffer fast paths */
285 rb_event_data(struct ring_buffer_event
*event
)
287 BUG_ON(event
->type
!= RINGBUF_TYPE_DATA
);
288 /* If length is in len field, then array[0] has the data */
290 return (void *)&event
->array
[0];
291 /* Otherwise length is in array[0] and array[1] has the data */
292 return (void *)&event
->array
[1];
296 * ring_buffer_event_data - return the data of the event
297 * @event: the event to get the data from
299 void *ring_buffer_event_data(struct ring_buffer_event
*event
)
301 return rb_event_data(event
);
303 EXPORT_SYMBOL_GPL(ring_buffer_event_data
);
305 #define for_each_buffer_cpu(buffer, cpu) \
306 for_each_cpu(cpu, buffer->cpumask)
309 #define TS_MASK ((1ULL << TS_SHIFT) - 1)
310 #define TS_DELTA_TEST (~TS_MASK)
312 struct buffer_data_page
{
313 u64 time_stamp
; /* page time stamp */
314 local_t commit
; /* write committed index */
315 unsigned char data
[]; /* data of buffer page */
319 local_t write
; /* index for next write */
320 unsigned read
; /* index for next read */
321 struct list_head list
; /* list of free pages */
322 struct buffer_data_page
*page
; /* Actual data page */
325 static void rb_init_page(struct buffer_data_page
*bpage
)
327 local_set(&bpage
->commit
, 0);
331 * ring_buffer_page_len - the size of data on the page.
332 * @page: The page to read
334 * Returns the amount of data on the page, including buffer page header.
336 size_t ring_buffer_page_len(void *page
)
338 return local_read(&((struct buffer_data_page
*)page
)->commit
)
343 * Also stolen from mm/slob.c. Thanks to Mathieu Desnoyers for pointing
346 static void free_buffer_page(struct buffer_page
*bpage
)
348 free_page((unsigned long)bpage
->page
);
353 * We need to fit the time_stamp delta into 27 bits.
355 static inline int test_time_stamp(u64 delta
)
357 if (delta
& TS_DELTA_TEST
)
362 #define BUF_PAGE_SIZE (PAGE_SIZE - BUF_PAGE_HDR_SIZE)
365 * head_page == tail_page && head == tail then buffer is empty.
367 struct ring_buffer_per_cpu
{
369 struct ring_buffer
*buffer
;
370 spinlock_t reader_lock
; /* serialize readers */
372 struct lock_class_key lock_key
;
373 struct list_head pages
;
374 struct buffer_page
*head_page
; /* read from head */
375 struct buffer_page
*tail_page
; /* write to tail */
376 struct buffer_page
*commit_page
; /* committed pages */
377 struct buffer_page
*reader_page
;
378 unsigned long overrun
;
379 unsigned long entries
;
382 atomic_t record_disabled
;
389 atomic_t record_disabled
;
390 cpumask_var_t cpumask
;
394 struct ring_buffer_per_cpu
**buffers
;
396 #ifdef CONFIG_HOTPLUG_CPU
397 struct notifier_block cpu_notify
;
402 struct ring_buffer_iter
{
403 struct ring_buffer_per_cpu
*cpu_buffer
;
405 struct buffer_page
*head_page
;
409 /* buffer may be either ring_buffer or ring_buffer_per_cpu */
410 #define RB_WARN_ON(buffer, cond) \
412 int _____ret = unlikely(cond); \
414 atomic_inc(&buffer->record_disabled); \
420 /* Up this if you want to test the TIME_EXTENTS and normalization */
421 #define DEBUG_SHIFT 0
423 u64
ring_buffer_time_stamp(struct ring_buffer
*buffer
, int cpu
)
427 preempt_disable_notrace();
428 /* shift to debug/test normalization and TIME_EXTENTS */
429 time
= buffer
->clock() << DEBUG_SHIFT
;
430 preempt_enable_no_resched_notrace();
434 EXPORT_SYMBOL_GPL(ring_buffer_time_stamp
);
436 void ring_buffer_normalize_time_stamp(struct ring_buffer
*buffer
,
439 /* Just stupid testing the normalize function and deltas */
442 EXPORT_SYMBOL_GPL(ring_buffer_normalize_time_stamp
);
445 * check_pages - integrity check of buffer pages
446 * @cpu_buffer: CPU buffer with pages to test
448 * As a safety measure we check to make sure the data pages have not
451 static int rb_check_pages(struct ring_buffer_per_cpu
*cpu_buffer
)
453 struct list_head
*head
= &cpu_buffer
->pages
;
454 struct buffer_page
*bpage
, *tmp
;
456 if (RB_WARN_ON(cpu_buffer
, head
->next
->prev
!= head
))
458 if (RB_WARN_ON(cpu_buffer
, head
->prev
->next
!= head
))
461 list_for_each_entry_safe(bpage
, tmp
, head
, list
) {
462 if (RB_WARN_ON(cpu_buffer
,
463 bpage
->list
.next
->prev
!= &bpage
->list
))
465 if (RB_WARN_ON(cpu_buffer
,
466 bpage
->list
.prev
->next
!= &bpage
->list
))
473 static int rb_allocate_pages(struct ring_buffer_per_cpu
*cpu_buffer
,
476 struct list_head
*head
= &cpu_buffer
->pages
;
477 struct buffer_page
*bpage
, *tmp
;
482 for (i
= 0; i
< nr_pages
; i
++) {
483 bpage
= kzalloc_node(ALIGN(sizeof(*bpage
), cache_line_size()),
484 GFP_KERNEL
, cpu_to_node(cpu_buffer
->cpu
));
487 list_add(&bpage
->list
, &pages
);
489 addr
= __get_free_page(GFP_KERNEL
);
492 bpage
->page
= (void *)addr
;
493 rb_init_page(bpage
->page
);
496 list_splice(&pages
, head
);
498 rb_check_pages(cpu_buffer
);
503 list_for_each_entry_safe(bpage
, tmp
, &pages
, list
) {
504 list_del_init(&bpage
->list
);
505 free_buffer_page(bpage
);
510 static struct ring_buffer_per_cpu
*
511 rb_allocate_cpu_buffer(struct ring_buffer
*buffer
, int cpu
)
513 struct ring_buffer_per_cpu
*cpu_buffer
;
514 struct buffer_page
*bpage
;
518 cpu_buffer
= kzalloc_node(ALIGN(sizeof(*cpu_buffer
), cache_line_size()),
519 GFP_KERNEL
, cpu_to_node(cpu
));
523 cpu_buffer
->cpu
= cpu
;
524 cpu_buffer
->buffer
= buffer
;
525 spin_lock_init(&cpu_buffer
->reader_lock
);
526 cpu_buffer
->lock
= (raw_spinlock_t
)__RAW_SPIN_LOCK_UNLOCKED
;
527 INIT_LIST_HEAD(&cpu_buffer
->pages
);
529 bpage
= kzalloc_node(ALIGN(sizeof(*bpage
), cache_line_size()),
530 GFP_KERNEL
, cpu_to_node(cpu
));
532 goto fail_free_buffer
;
534 cpu_buffer
->reader_page
= bpage
;
535 addr
= __get_free_page(GFP_KERNEL
);
537 goto fail_free_reader
;
538 bpage
->page
= (void *)addr
;
539 rb_init_page(bpage
->page
);
541 INIT_LIST_HEAD(&cpu_buffer
->reader_page
->list
);
543 ret
= rb_allocate_pages(cpu_buffer
, buffer
->pages
);
545 goto fail_free_reader
;
547 cpu_buffer
->head_page
548 = list_entry(cpu_buffer
->pages
.next
, struct buffer_page
, list
);
549 cpu_buffer
->tail_page
= cpu_buffer
->commit_page
= cpu_buffer
->head_page
;
554 free_buffer_page(cpu_buffer
->reader_page
);
561 static void rb_free_cpu_buffer(struct ring_buffer_per_cpu
*cpu_buffer
)
563 struct list_head
*head
= &cpu_buffer
->pages
;
564 struct buffer_page
*bpage
, *tmp
;
566 free_buffer_page(cpu_buffer
->reader_page
);
568 list_for_each_entry_safe(bpage
, tmp
, head
, list
) {
569 list_del_init(&bpage
->list
);
570 free_buffer_page(bpage
);
576 * Causes compile errors if the struct buffer_page gets bigger
577 * than the struct page.
579 extern int ring_buffer_page_too_big(void);
581 #ifdef CONFIG_HOTPLUG_CPU
582 static int rb_cpu_notify(struct notifier_block
*self
,
583 unsigned long action
, void *hcpu
);
587 * ring_buffer_alloc - allocate a new ring_buffer
588 * @size: the size in bytes per cpu that is needed.
589 * @flags: attributes to set for the ring buffer.
591 * Currently the only flag that is available is the RB_FL_OVERWRITE
592 * flag. This flag means that the buffer will overwrite old data
593 * when the buffer wraps. If this flag is not set, the buffer will
594 * drop data when the tail hits the head.
596 struct ring_buffer
*ring_buffer_alloc(unsigned long size
, unsigned flags
)
598 struct ring_buffer
*buffer
;
602 /* Paranoid! Optimizes out when all is well */
603 if (sizeof(struct buffer_page
) > sizeof(struct page
))
604 ring_buffer_page_too_big();
607 /* keep it in its own cache line */
608 buffer
= kzalloc(ALIGN(sizeof(*buffer
), cache_line_size()),
613 if (!alloc_cpumask_var(&buffer
->cpumask
, GFP_KERNEL
))
614 goto fail_free_buffer
;
616 buffer
->pages
= DIV_ROUND_UP(size
, BUF_PAGE_SIZE
);
617 buffer
->flags
= flags
;
618 buffer
->clock
= trace_clock_local
;
620 /* need at least two pages */
621 if (buffer
->pages
== 1)
625 * In case of non-hotplug cpu, if the ring-buffer is allocated
626 * in early initcall, it will not be notified of secondary cpus.
627 * In that off case, we need to allocate for all possible cpus.
629 #ifdef CONFIG_HOTPLUG_CPU
631 cpumask_copy(buffer
->cpumask
, cpu_online_mask
);
633 cpumask_copy(buffer
->cpumask
, cpu_possible_mask
);
635 buffer
->cpus
= nr_cpu_ids
;
637 bsize
= sizeof(void *) * nr_cpu_ids
;
638 buffer
->buffers
= kzalloc(ALIGN(bsize
, cache_line_size()),
640 if (!buffer
->buffers
)
641 goto fail_free_cpumask
;
643 for_each_buffer_cpu(buffer
, cpu
) {
644 buffer
->buffers
[cpu
] =
645 rb_allocate_cpu_buffer(buffer
, cpu
);
646 if (!buffer
->buffers
[cpu
])
647 goto fail_free_buffers
;
650 #ifdef CONFIG_HOTPLUG_CPU
651 buffer
->cpu_notify
.notifier_call
= rb_cpu_notify
;
652 buffer
->cpu_notify
.priority
= 0;
653 register_cpu_notifier(&buffer
->cpu_notify
);
657 mutex_init(&buffer
->mutex
);
662 for_each_buffer_cpu(buffer
, cpu
) {
663 if (buffer
->buffers
[cpu
])
664 rb_free_cpu_buffer(buffer
->buffers
[cpu
]);
666 kfree(buffer
->buffers
);
669 free_cpumask_var(buffer
->cpumask
);
676 EXPORT_SYMBOL_GPL(ring_buffer_alloc
);
679 * ring_buffer_free - free a ring buffer.
680 * @buffer: the buffer to free.
683 ring_buffer_free(struct ring_buffer
*buffer
)
689 #ifdef CONFIG_HOTPLUG_CPU
690 unregister_cpu_notifier(&buffer
->cpu_notify
);
693 for_each_buffer_cpu(buffer
, cpu
)
694 rb_free_cpu_buffer(buffer
->buffers
[cpu
]);
698 free_cpumask_var(buffer
->cpumask
);
702 EXPORT_SYMBOL_GPL(ring_buffer_free
);
704 void ring_buffer_set_clock(struct ring_buffer
*buffer
,
707 buffer
->clock
= clock
;
710 static void rb_reset_cpu(struct ring_buffer_per_cpu
*cpu_buffer
);
713 rb_remove_pages(struct ring_buffer_per_cpu
*cpu_buffer
, unsigned nr_pages
)
715 struct buffer_page
*bpage
;
719 atomic_inc(&cpu_buffer
->record_disabled
);
722 for (i
= 0; i
< nr_pages
; i
++) {
723 if (RB_WARN_ON(cpu_buffer
, list_empty(&cpu_buffer
->pages
)))
725 p
= cpu_buffer
->pages
.next
;
726 bpage
= list_entry(p
, struct buffer_page
, list
);
727 list_del_init(&bpage
->list
);
728 free_buffer_page(bpage
);
730 if (RB_WARN_ON(cpu_buffer
, list_empty(&cpu_buffer
->pages
)))
733 rb_reset_cpu(cpu_buffer
);
735 rb_check_pages(cpu_buffer
);
737 atomic_dec(&cpu_buffer
->record_disabled
);
742 rb_insert_pages(struct ring_buffer_per_cpu
*cpu_buffer
,
743 struct list_head
*pages
, unsigned nr_pages
)
745 struct buffer_page
*bpage
;
749 atomic_inc(&cpu_buffer
->record_disabled
);
752 for (i
= 0; i
< nr_pages
; i
++) {
753 if (RB_WARN_ON(cpu_buffer
, list_empty(pages
)))
756 bpage
= list_entry(p
, struct buffer_page
, list
);
757 list_del_init(&bpage
->list
);
758 list_add_tail(&bpage
->list
, &cpu_buffer
->pages
);
760 rb_reset_cpu(cpu_buffer
);
762 rb_check_pages(cpu_buffer
);
764 atomic_dec(&cpu_buffer
->record_disabled
);
768 * ring_buffer_resize - resize the ring buffer
769 * @buffer: the buffer to resize.
770 * @size: the new size.
772 * The tracer is responsible for making sure that the buffer is
773 * not being used while changing the size.
774 * Note: We may be able to change the above requirement by using
775 * RCU synchronizations.
777 * Minimum size is 2 * BUF_PAGE_SIZE.
779 * Returns -1 on failure.
781 int ring_buffer_resize(struct ring_buffer
*buffer
, unsigned long size
)
783 struct ring_buffer_per_cpu
*cpu_buffer
;
784 unsigned nr_pages
, rm_pages
, new_pages
;
785 struct buffer_page
*bpage
, *tmp
;
786 unsigned long buffer_size
;
792 * Always succeed at resizing a non-existent buffer:
797 size
= DIV_ROUND_UP(size
, BUF_PAGE_SIZE
);
798 size
*= BUF_PAGE_SIZE
;
799 buffer_size
= buffer
->pages
* BUF_PAGE_SIZE
;
801 /* we need a minimum of two pages */
802 if (size
< BUF_PAGE_SIZE
* 2)
803 size
= BUF_PAGE_SIZE
* 2;
805 if (size
== buffer_size
)
808 mutex_lock(&buffer
->mutex
);
811 nr_pages
= DIV_ROUND_UP(size
, BUF_PAGE_SIZE
);
813 if (size
< buffer_size
) {
815 /* easy case, just free pages */
816 if (RB_WARN_ON(buffer
, nr_pages
>= buffer
->pages
))
819 rm_pages
= buffer
->pages
- nr_pages
;
821 for_each_buffer_cpu(buffer
, cpu
) {
822 cpu_buffer
= buffer
->buffers
[cpu
];
823 rb_remove_pages(cpu_buffer
, rm_pages
);
829 * This is a bit more difficult. We only want to add pages
830 * when we can allocate enough for all CPUs. We do this
831 * by allocating all the pages and storing them on a local
832 * link list. If we succeed in our allocation, then we
833 * add these pages to the cpu_buffers. Otherwise we just free
834 * them all and return -ENOMEM;
836 if (RB_WARN_ON(buffer
, nr_pages
<= buffer
->pages
))
839 new_pages
= nr_pages
- buffer
->pages
;
841 for_each_buffer_cpu(buffer
, cpu
) {
842 for (i
= 0; i
< new_pages
; i
++) {
843 bpage
= kzalloc_node(ALIGN(sizeof(*bpage
),
845 GFP_KERNEL
, cpu_to_node(cpu
));
848 list_add(&bpage
->list
, &pages
);
849 addr
= __get_free_page(GFP_KERNEL
);
852 bpage
->page
= (void *)addr
;
853 rb_init_page(bpage
->page
);
857 for_each_buffer_cpu(buffer
, cpu
) {
858 cpu_buffer
= buffer
->buffers
[cpu
];
859 rb_insert_pages(cpu_buffer
, &pages
, new_pages
);
862 if (RB_WARN_ON(buffer
, !list_empty(&pages
)))
866 buffer
->pages
= nr_pages
;
868 mutex_unlock(&buffer
->mutex
);
873 list_for_each_entry_safe(bpage
, tmp
, &pages
, list
) {
874 list_del_init(&bpage
->list
);
875 free_buffer_page(bpage
);
878 mutex_unlock(&buffer
->mutex
);
882 * Something went totally wrong, and we are too paranoid
883 * to even clean up the mess.
887 mutex_unlock(&buffer
->mutex
);
890 EXPORT_SYMBOL_GPL(ring_buffer_resize
);
893 __rb_data_page_index(struct buffer_data_page
*bpage
, unsigned index
)
895 return bpage
->data
+ index
;
898 static inline void *__rb_page_index(struct buffer_page
*bpage
, unsigned index
)
900 return bpage
->page
->data
+ index
;
903 static inline struct ring_buffer_event
*
904 rb_reader_event(struct ring_buffer_per_cpu
*cpu_buffer
)
906 return __rb_page_index(cpu_buffer
->reader_page
,
907 cpu_buffer
->reader_page
->read
);
910 static inline struct ring_buffer_event
*
911 rb_head_event(struct ring_buffer_per_cpu
*cpu_buffer
)
913 return __rb_page_index(cpu_buffer
->head_page
,
914 cpu_buffer
->head_page
->read
);
917 static inline struct ring_buffer_event
*
918 rb_iter_head_event(struct ring_buffer_iter
*iter
)
920 return __rb_page_index(iter
->head_page
, iter
->head
);
923 static inline unsigned rb_page_write(struct buffer_page
*bpage
)
925 return local_read(&bpage
->write
);
928 static inline unsigned rb_page_commit(struct buffer_page
*bpage
)
930 return local_read(&bpage
->page
->commit
);
933 /* Size is determined by what has been commited */
934 static inline unsigned rb_page_size(struct buffer_page
*bpage
)
936 return rb_page_commit(bpage
);
939 static inline unsigned
940 rb_commit_index(struct ring_buffer_per_cpu
*cpu_buffer
)
942 return rb_page_commit(cpu_buffer
->commit_page
);
945 static inline unsigned rb_head_size(struct ring_buffer_per_cpu
*cpu_buffer
)
947 return rb_page_commit(cpu_buffer
->head_page
);
951 * When the tail hits the head and the buffer is in overwrite mode,
952 * the head jumps to the next page and all content on the previous
953 * page is discarded. But before doing so, we update the overrun
954 * variable of the buffer.
956 static void rb_update_overflow(struct ring_buffer_per_cpu
*cpu_buffer
)
958 struct ring_buffer_event
*event
;
961 for (head
= 0; head
< rb_head_size(cpu_buffer
);
962 head
+= rb_event_length(event
)) {
964 event
= __rb_page_index(cpu_buffer
->head_page
, head
);
965 if (RB_WARN_ON(cpu_buffer
, rb_null_event(event
)))
967 /* Only count data entries */
968 if (event
->type
!= RINGBUF_TYPE_DATA
)
970 cpu_buffer
->overrun
++;
971 cpu_buffer
->entries
--;
975 static inline void rb_inc_page(struct ring_buffer_per_cpu
*cpu_buffer
,
976 struct buffer_page
**bpage
)
978 struct list_head
*p
= (*bpage
)->list
.next
;
980 if (p
== &cpu_buffer
->pages
)
983 *bpage
= list_entry(p
, struct buffer_page
, list
);
986 static inline unsigned
987 rb_event_index(struct ring_buffer_event
*event
)
989 unsigned long addr
= (unsigned long)event
;
991 return (addr
& ~PAGE_MASK
) - (PAGE_SIZE
- BUF_PAGE_SIZE
);
995 rb_is_commit(struct ring_buffer_per_cpu
*cpu_buffer
,
996 struct ring_buffer_event
*event
)
998 unsigned long addr
= (unsigned long)event
;
1001 index
= rb_event_index(event
);
1004 return cpu_buffer
->commit_page
->page
== (void *)addr
&&
1005 rb_commit_index(cpu_buffer
) == index
;
1009 rb_set_commit_event(struct ring_buffer_per_cpu
*cpu_buffer
,
1010 struct ring_buffer_event
*event
)
1012 unsigned long addr
= (unsigned long)event
;
1013 unsigned long index
;
1015 index
= rb_event_index(event
);
1018 while (cpu_buffer
->commit_page
->page
!= (void *)addr
) {
1019 if (RB_WARN_ON(cpu_buffer
,
1020 cpu_buffer
->commit_page
== cpu_buffer
->tail_page
))
1022 cpu_buffer
->commit_page
->page
->commit
=
1023 cpu_buffer
->commit_page
->write
;
1024 rb_inc_page(cpu_buffer
, &cpu_buffer
->commit_page
);
1025 cpu_buffer
->write_stamp
=
1026 cpu_buffer
->commit_page
->page
->time_stamp
;
1029 /* Now set the commit to the event's index */
1030 local_set(&cpu_buffer
->commit_page
->page
->commit
, index
);
1034 rb_set_commit_to_write(struct ring_buffer_per_cpu
*cpu_buffer
)
1037 * We only race with interrupts and NMIs on this CPU.
1038 * If we own the commit event, then we can commit
1039 * all others that interrupted us, since the interruptions
1040 * are in stack format (they finish before they come
1041 * back to us). This allows us to do a simple loop to
1042 * assign the commit to the tail.
1045 while (cpu_buffer
->commit_page
!= cpu_buffer
->tail_page
) {
1046 cpu_buffer
->commit_page
->page
->commit
=
1047 cpu_buffer
->commit_page
->write
;
1048 rb_inc_page(cpu_buffer
, &cpu_buffer
->commit_page
);
1049 cpu_buffer
->write_stamp
=
1050 cpu_buffer
->commit_page
->page
->time_stamp
;
1051 /* add barrier to keep gcc from optimizing too much */
1054 while (rb_commit_index(cpu_buffer
) !=
1055 rb_page_write(cpu_buffer
->commit_page
)) {
1056 cpu_buffer
->commit_page
->page
->commit
=
1057 cpu_buffer
->commit_page
->write
;
1061 /* again, keep gcc from optimizing */
1065 * If an interrupt came in just after the first while loop
1066 * and pushed the tail page forward, we will be left with
1067 * a dangling commit that will never go forward.
1069 if (unlikely(cpu_buffer
->commit_page
!= cpu_buffer
->tail_page
))
1073 static void rb_reset_reader_page(struct ring_buffer_per_cpu
*cpu_buffer
)
1075 cpu_buffer
->read_stamp
= cpu_buffer
->reader_page
->page
->time_stamp
;
1076 cpu_buffer
->reader_page
->read
= 0;
1079 static void rb_inc_iter(struct ring_buffer_iter
*iter
)
1081 struct ring_buffer_per_cpu
*cpu_buffer
= iter
->cpu_buffer
;
1084 * The iterator could be on the reader page (it starts there).
1085 * But the head could have moved, since the reader was
1086 * found. Check for this case and assign the iterator
1087 * to the head page instead of next.
1089 if (iter
->head_page
== cpu_buffer
->reader_page
)
1090 iter
->head_page
= cpu_buffer
->head_page
;
1092 rb_inc_page(cpu_buffer
, &iter
->head_page
);
1094 iter
->read_stamp
= iter
->head_page
->page
->time_stamp
;
1099 * ring_buffer_update_event - update event type and data
1100 * @event: the even to update
1101 * @type: the type of event
1102 * @length: the size of the event field in the ring buffer
1104 * Update the type and data fields of the event. The length
1105 * is the actual size that is written to the ring buffer,
1106 * and with this, we can determine what to place into the
1110 rb_update_event(struct ring_buffer_event
*event
,
1111 unsigned type
, unsigned length
)
1117 case RINGBUF_TYPE_PADDING
:
1120 case RINGBUF_TYPE_TIME_EXTEND
:
1121 event
->len
= DIV_ROUND_UP(RB_LEN_TIME_EXTEND
, RB_ALIGNMENT
);
1124 case RINGBUF_TYPE_TIME_STAMP
:
1125 event
->len
= DIV_ROUND_UP(RB_LEN_TIME_STAMP
, RB_ALIGNMENT
);
1128 case RINGBUF_TYPE_DATA
:
1129 length
-= RB_EVNT_HDR_SIZE
;
1130 if (length
> RB_MAX_SMALL_DATA
) {
1132 event
->array
[0] = length
;
1134 event
->len
= DIV_ROUND_UP(length
, RB_ALIGNMENT
);
1141 static unsigned rb_calculate_event_length(unsigned length
)
1143 struct ring_buffer_event event
; /* Used only for sizeof array */
1145 /* zero length can cause confusions */
1149 if (length
> RB_MAX_SMALL_DATA
)
1150 length
+= sizeof(event
.array
[0]);
1152 length
+= RB_EVNT_HDR_SIZE
;
1153 length
= ALIGN(length
, RB_ALIGNMENT
);
1158 static struct ring_buffer_event
*
1159 __rb_reserve_next(struct ring_buffer_per_cpu
*cpu_buffer
,
1160 unsigned type
, unsigned long length
, u64
*ts
)
1162 struct buffer_page
*tail_page
, *head_page
, *reader_page
, *commit_page
;
1163 unsigned long tail
, write
;
1164 struct ring_buffer
*buffer
= cpu_buffer
->buffer
;
1165 struct ring_buffer_event
*event
;
1166 unsigned long flags
;
1167 bool lock_taken
= false;
1169 commit_page
= cpu_buffer
->commit_page
;
1170 /* we just need to protect against interrupts */
1172 tail_page
= cpu_buffer
->tail_page
;
1173 write
= local_add_return(length
, &tail_page
->write
);
1174 tail
= write
- length
;
1176 /* See if we shot pass the end of this buffer page */
1177 if (write
> BUF_PAGE_SIZE
) {
1178 struct buffer_page
*next_page
= tail_page
;
1180 local_irq_save(flags
);
1182 * Since the write to the buffer is still not
1183 * fully lockless, we must be careful with NMIs.
1184 * The locks in the writers are taken when a write
1185 * crosses to a new page. The locks protect against
1186 * races with the readers (this will soon be fixed
1187 * with a lockless solution).
1189 * Because we can not protect against NMIs, and we
1190 * want to keep traces reentrant, we need to manage
1191 * what happens when we are in an NMI.
1193 * NMIs can happen after we take the lock.
1194 * If we are in an NMI, only take the lock
1195 * if it is not already taken. Otherwise
1198 if (unlikely(in_nmi())) {
1199 if (!__raw_spin_trylock(&cpu_buffer
->lock
))
1202 __raw_spin_lock(&cpu_buffer
->lock
);
1206 rb_inc_page(cpu_buffer
, &next_page
);
1208 head_page
= cpu_buffer
->head_page
;
1209 reader_page
= cpu_buffer
->reader_page
;
1211 /* we grabbed the lock before incrementing */
1212 if (RB_WARN_ON(cpu_buffer
, next_page
== reader_page
))
1216 * If for some reason, we had an interrupt storm that made
1217 * it all the way around the buffer, bail, and warn
1220 if (unlikely(next_page
== commit_page
)) {
1225 if (next_page
== head_page
) {
1226 if (!(buffer
->flags
& RB_FL_OVERWRITE
))
1229 /* tail_page has not moved yet? */
1230 if (tail_page
== cpu_buffer
->tail_page
) {
1231 /* count overflows */
1232 rb_update_overflow(cpu_buffer
);
1234 rb_inc_page(cpu_buffer
, &head_page
);
1235 cpu_buffer
->head_page
= head_page
;
1236 cpu_buffer
->head_page
->read
= 0;
1241 * If the tail page is still the same as what we think
1242 * it is, then it is up to us to update the tail
1245 if (tail_page
== cpu_buffer
->tail_page
) {
1246 local_set(&next_page
->write
, 0);
1247 local_set(&next_page
->page
->commit
, 0);
1248 cpu_buffer
->tail_page
= next_page
;
1250 /* reread the time stamp */
1251 *ts
= ring_buffer_time_stamp(buffer
, cpu_buffer
->cpu
);
1252 cpu_buffer
->tail_page
->page
->time_stamp
= *ts
;
1256 * The actual tail page has moved forward.
1258 if (tail
< BUF_PAGE_SIZE
) {
1259 /* Mark the rest of the page with padding */
1260 event
= __rb_page_index(tail_page
, tail
);
1261 rb_event_set_padding(event
);
1264 if (tail
<= BUF_PAGE_SIZE
)
1265 /* Set the write back to the previous setting */
1266 local_set(&tail_page
->write
, tail
);
1269 * If this was a commit entry that failed,
1270 * increment that too
1272 if (tail_page
== cpu_buffer
->commit_page
&&
1273 tail
== rb_commit_index(cpu_buffer
)) {
1274 rb_set_commit_to_write(cpu_buffer
);
1277 __raw_spin_unlock(&cpu_buffer
->lock
);
1278 local_irq_restore(flags
);
1280 /* fail and let the caller try again */
1281 return ERR_PTR(-EAGAIN
);
1284 /* We reserved something on the buffer */
1286 if (RB_WARN_ON(cpu_buffer
, write
> BUF_PAGE_SIZE
))
1289 event
= __rb_page_index(tail_page
, tail
);
1290 rb_update_event(event
, type
, length
);
1293 * If this is a commit and the tail is zero, then update
1294 * this page's time stamp.
1296 if (!tail
&& rb_is_commit(cpu_buffer
, event
))
1297 cpu_buffer
->commit_page
->page
->time_stamp
= *ts
;
1303 if (tail
<= BUF_PAGE_SIZE
)
1304 local_set(&tail_page
->write
, tail
);
1306 if (likely(lock_taken
))
1307 __raw_spin_unlock(&cpu_buffer
->lock
);
1308 local_irq_restore(flags
);
1313 rb_add_time_stamp(struct ring_buffer_per_cpu
*cpu_buffer
,
1314 u64
*ts
, u64
*delta
)
1316 struct ring_buffer_event
*event
;
1320 if (unlikely(*delta
> (1ULL << 59) && !once
++)) {
1321 printk(KERN_WARNING
"Delta way too big! %llu"
1322 " ts=%llu write stamp = %llu\n",
1323 (unsigned long long)*delta
,
1324 (unsigned long long)*ts
,
1325 (unsigned long long)cpu_buffer
->write_stamp
);
1330 * The delta is too big, we to add a
1333 event
= __rb_reserve_next(cpu_buffer
,
1334 RINGBUF_TYPE_TIME_EXTEND
,
1340 if (PTR_ERR(event
) == -EAGAIN
)
1343 /* Only a commited time event can update the write stamp */
1344 if (rb_is_commit(cpu_buffer
, event
)) {
1346 * If this is the first on the page, then we need to
1347 * update the page itself, and just put in a zero.
1349 if (rb_event_index(event
)) {
1350 event
->time_delta
= *delta
& TS_MASK
;
1351 event
->array
[0] = *delta
>> TS_SHIFT
;
1353 cpu_buffer
->commit_page
->page
->time_stamp
= *ts
;
1354 event
->time_delta
= 0;
1355 event
->array
[0] = 0;
1357 cpu_buffer
->write_stamp
= *ts
;
1358 /* let the caller know this was the commit */
1361 /* Darn, this is just wasted space */
1362 event
->time_delta
= 0;
1363 event
->array
[0] = 0;
1372 static struct ring_buffer_event
*
1373 rb_reserve_next_event(struct ring_buffer_per_cpu
*cpu_buffer
,
1374 unsigned type
, unsigned long length
)
1376 struct ring_buffer_event
*event
;
1383 * We allow for interrupts to reenter here and do a trace.
1384 * If one does, it will cause this original code to loop
1385 * back here. Even with heavy interrupts happening, this
1386 * should only happen a few times in a row. If this happens
1387 * 1000 times in a row, there must be either an interrupt
1388 * storm or we have something buggy.
1391 if (RB_WARN_ON(cpu_buffer
, ++nr_loops
> 1000))
1394 ts
= ring_buffer_time_stamp(cpu_buffer
->buffer
, cpu_buffer
->cpu
);
1397 * Only the first commit can update the timestamp.
1398 * Yes there is a race here. If an interrupt comes in
1399 * just after the conditional and it traces too, then it
1400 * will also check the deltas. More than one timestamp may
1401 * also be made. But only the entry that did the actual
1402 * commit will be something other than zero.
1404 if (cpu_buffer
->tail_page
== cpu_buffer
->commit_page
&&
1405 rb_page_write(cpu_buffer
->tail_page
) ==
1406 rb_commit_index(cpu_buffer
)) {
1408 delta
= ts
- cpu_buffer
->write_stamp
;
1410 /* make sure this delta is calculated here */
1413 /* Did the write stamp get updated already? */
1414 if (unlikely(ts
< cpu_buffer
->write_stamp
))
1417 if (test_time_stamp(delta
)) {
1419 commit
= rb_add_time_stamp(cpu_buffer
, &ts
, &delta
);
1421 if (commit
== -EBUSY
)
1424 if (commit
== -EAGAIN
)
1427 RB_WARN_ON(cpu_buffer
, commit
< 0);
1430 /* Non commits have zero deltas */
1433 event
= __rb_reserve_next(cpu_buffer
, type
, length
, &ts
);
1434 if (PTR_ERR(event
) == -EAGAIN
)
1438 if (unlikely(commit
))
1440 * Ouch! We needed a timestamp and it was commited. But
1441 * we didn't get our event reserved.
1443 rb_set_commit_to_write(cpu_buffer
);
1448 * If the timestamp was commited, make the commit our entry
1449 * now so that we will update it when needed.
1452 rb_set_commit_event(cpu_buffer
, event
);
1453 else if (!rb_is_commit(cpu_buffer
, event
))
1456 event
->time_delta
= delta
;
1461 static DEFINE_PER_CPU(int, rb_need_resched
);
1464 * ring_buffer_lock_reserve - reserve a part of the buffer
1465 * @buffer: the ring buffer to reserve from
1466 * @length: the length of the data to reserve (excluding event header)
1468 * Returns a reseverd event on the ring buffer to copy directly to.
1469 * The user of this interface will need to get the body to write into
1470 * and can use the ring_buffer_event_data() interface.
1472 * The length is the length of the data needed, not the event length
1473 * which also includes the event header.
1475 * Must be paired with ring_buffer_unlock_commit, unless NULL is returned.
1476 * If NULL is returned, then nothing has been allocated or locked.
1478 struct ring_buffer_event
*
1479 ring_buffer_lock_reserve(struct ring_buffer
*buffer
, unsigned long length
)
1481 struct ring_buffer_per_cpu
*cpu_buffer
;
1482 struct ring_buffer_event
*event
;
1485 if (ring_buffer_flags
!= RB_BUFFERS_ON
)
1488 if (atomic_read(&buffer
->record_disabled
))
1491 /* If we are tracing schedule, we don't want to recurse */
1492 resched
= ftrace_preempt_disable();
1494 cpu
= raw_smp_processor_id();
1496 if (!cpumask_test_cpu(cpu
, buffer
->cpumask
))
1499 cpu_buffer
= buffer
->buffers
[cpu
];
1501 if (atomic_read(&cpu_buffer
->record_disabled
))
1504 length
= rb_calculate_event_length(length
);
1505 if (length
> BUF_PAGE_SIZE
)
1508 event
= rb_reserve_next_event(cpu_buffer
, RINGBUF_TYPE_DATA
, length
);
1513 * Need to store resched state on this cpu.
1514 * Only the first needs to.
1517 if (preempt_count() == 1)
1518 per_cpu(rb_need_resched
, cpu
) = resched
;
1523 ftrace_preempt_enable(resched
);
1526 EXPORT_SYMBOL_GPL(ring_buffer_lock_reserve
);
1528 static void rb_commit(struct ring_buffer_per_cpu
*cpu_buffer
,
1529 struct ring_buffer_event
*event
)
1531 cpu_buffer
->entries
++;
1533 /* Only process further if we own the commit */
1534 if (!rb_is_commit(cpu_buffer
, event
))
1537 cpu_buffer
->write_stamp
+= event
->time_delta
;
1539 rb_set_commit_to_write(cpu_buffer
);
1543 * ring_buffer_unlock_commit - commit a reserved
1544 * @buffer: The buffer to commit to
1545 * @event: The event pointer to commit.
1547 * This commits the data to the ring buffer, and releases any locks held.
1549 * Must be paired with ring_buffer_lock_reserve.
1551 int ring_buffer_unlock_commit(struct ring_buffer
*buffer
,
1552 struct ring_buffer_event
*event
)
1554 struct ring_buffer_per_cpu
*cpu_buffer
;
1555 int cpu
= raw_smp_processor_id();
1557 cpu_buffer
= buffer
->buffers
[cpu
];
1559 rb_commit(cpu_buffer
, event
);
1562 * Only the last preempt count needs to restore preemption.
1564 if (preempt_count() == 1)
1565 ftrace_preempt_enable(per_cpu(rb_need_resched
, cpu
));
1567 preempt_enable_no_resched_notrace();
1571 EXPORT_SYMBOL_GPL(ring_buffer_unlock_commit
);
1574 * ring_buffer_write - write data to the buffer without reserving
1575 * @buffer: The ring buffer to write to.
1576 * @length: The length of the data being written (excluding the event header)
1577 * @data: The data to write to the buffer.
1579 * This is like ring_buffer_lock_reserve and ring_buffer_unlock_commit as
1580 * one function. If you already have the data to write to the buffer, it
1581 * may be easier to simply call this function.
1583 * Note, like ring_buffer_lock_reserve, the length is the length of the data
1584 * and not the length of the event which would hold the header.
1586 int ring_buffer_write(struct ring_buffer
*buffer
,
1587 unsigned long length
,
1590 struct ring_buffer_per_cpu
*cpu_buffer
;
1591 struct ring_buffer_event
*event
;
1592 unsigned long event_length
;
1597 if (ring_buffer_flags
!= RB_BUFFERS_ON
)
1600 if (atomic_read(&buffer
->record_disabled
))
1603 resched
= ftrace_preempt_disable();
1605 cpu
= raw_smp_processor_id();
1607 if (!cpumask_test_cpu(cpu
, buffer
->cpumask
))
1610 cpu_buffer
= buffer
->buffers
[cpu
];
1612 if (atomic_read(&cpu_buffer
->record_disabled
))
1615 event_length
= rb_calculate_event_length(length
);
1616 event
= rb_reserve_next_event(cpu_buffer
,
1617 RINGBUF_TYPE_DATA
, event_length
);
1621 body
= rb_event_data(event
);
1623 memcpy(body
, data
, length
);
1625 rb_commit(cpu_buffer
, event
);
1629 ftrace_preempt_enable(resched
);
1633 EXPORT_SYMBOL_GPL(ring_buffer_write
);
1635 static int rb_per_cpu_empty(struct ring_buffer_per_cpu
*cpu_buffer
)
1637 struct buffer_page
*reader
= cpu_buffer
->reader_page
;
1638 struct buffer_page
*head
= cpu_buffer
->head_page
;
1639 struct buffer_page
*commit
= cpu_buffer
->commit_page
;
1641 return reader
->read
== rb_page_commit(reader
) &&
1642 (commit
== reader
||
1644 head
->read
== rb_page_commit(commit
)));
1648 * ring_buffer_record_disable - stop all writes into the buffer
1649 * @buffer: The ring buffer to stop writes to.
1651 * This prevents all writes to the buffer. Any attempt to write
1652 * to the buffer after this will fail and return NULL.
1654 * The caller should call synchronize_sched() after this.
1656 void ring_buffer_record_disable(struct ring_buffer
*buffer
)
1658 atomic_inc(&buffer
->record_disabled
);
1660 EXPORT_SYMBOL_GPL(ring_buffer_record_disable
);
1663 * ring_buffer_record_enable - enable writes to the buffer
1664 * @buffer: The ring buffer to enable writes
1666 * Note, multiple disables will need the same number of enables
1667 * to truely enable the writing (much like preempt_disable).
1669 void ring_buffer_record_enable(struct ring_buffer
*buffer
)
1671 atomic_dec(&buffer
->record_disabled
);
1673 EXPORT_SYMBOL_GPL(ring_buffer_record_enable
);
1676 * ring_buffer_record_disable_cpu - stop all writes into the cpu_buffer
1677 * @buffer: The ring buffer to stop writes to.
1678 * @cpu: The CPU buffer to stop
1680 * This prevents all writes to the buffer. Any attempt to write
1681 * to the buffer after this will fail and return NULL.
1683 * The caller should call synchronize_sched() after this.
1685 void ring_buffer_record_disable_cpu(struct ring_buffer
*buffer
, int cpu
)
1687 struct ring_buffer_per_cpu
*cpu_buffer
;
1689 if (!cpumask_test_cpu(cpu
, buffer
->cpumask
))
1692 cpu_buffer
= buffer
->buffers
[cpu
];
1693 atomic_inc(&cpu_buffer
->record_disabled
);
1695 EXPORT_SYMBOL_GPL(ring_buffer_record_disable_cpu
);
1698 * ring_buffer_record_enable_cpu - enable writes to the buffer
1699 * @buffer: The ring buffer to enable writes
1700 * @cpu: The CPU to enable.
1702 * Note, multiple disables will need the same number of enables
1703 * to truely enable the writing (much like preempt_disable).
1705 void ring_buffer_record_enable_cpu(struct ring_buffer
*buffer
, int cpu
)
1707 struct ring_buffer_per_cpu
*cpu_buffer
;
1709 if (!cpumask_test_cpu(cpu
, buffer
->cpumask
))
1712 cpu_buffer
= buffer
->buffers
[cpu
];
1713 atomic_dec(&cpu_buffer
->record_disabled
);
1715 EXPORT_SYMBOL_GPL(ring_buffer_record_enable_cpu
);
1718 * ring_buffer_entries_cpu - get the number of entries in a cpu buffer
1719 * @buffer: The ring buffer
1720 * @cpu: The per CPU buffer to get the entries from.
1722 unsigned long ring_buffer_entries_cpu(struct ring_buffer
*buffer
, int cpu
)
1724 struct ring_buffer_per_cpu
*cpu_buffer
;
1727 if (!cpumask_test_cpu(cpu
, buffer
->cpumask
))
1730 cpu_buffer
= buffer
->buffers
[cpu
];
1731 ret
= cpu_buffer
->entries
;
1735 EXPORT_SYMBOL_GPL(ring_buffer_entries_cpu
);
1738 * ring_buffer_overrun_cpu - get the number of overruns in a cpu_buffer
1739 * @buffer: The ring buffer
1740 * @cpu: The per CPU buffer to get the number of overruns from
1742 unsigned long ring_buffer_overrun_cpu(struct ring_buffer
*buffer
, int cpu
)
1744 struct ring_buffer_per_cpu
*cpu_buffer
;
1747 if (!cpumask_test_cpu(cpu
, buffer
->cpumask
))
1750 cpu_buffer
= buffer
->buffers
[cpu
];
1751 ret
= cpu_buffer
->overrun
;
1755 EXPORT_SYMBOL_GPL(ring_buffer_overrun_cpu
);
1758 * ring_buffer_entries - get the number of entries in a buffer
1759 * @buffer: The ring buffer
1761 * Returns the total number of entries in the ring buffer
1764 unsigned long ring_buffer_entries(struct ring_buffer
*buffer
)
1766 struct ring_buffer_per_cpu
*cpu_buffer
;
1767 unsigned long entries
= 0;
1770 /* if you care about this being correct, lock the buffer */
1771 for_each_buffer_cpu(buffer
, cpu
) {
1772 cpu_buffer
= buffer
->buffers
[cpu
];
1773 entries
+= cpu_buffer
->entries
;
1778 EXPORT_SYMBOL_GPL(ring_buffer_entries
);
1781 * ring_buffer_overrun_cpu - get the number of overruns in buffer
1782 * @buffer: The ring buffer
1784 * Returns the total number of overruns in the ring buffer
1787 unsigned long ring_buffer_overruns(struct ring_buffer
*buffer
)
1789 struct ring_buffer_per_cpu
*cpu_buffer
;
1790 unsigned long overruns
= 0;
1793 /* if you care about this being correct, lock the buffer */
1794 for_each_buffer_cpu(buffer
, cpu
) {
1795 cpu_buffer
= buffer
->buffers
[cpu
];
1796 overruns
+= cpu_buffer
->overrun
;
1801 EXPORT_SYMBOL_GPL(ring_buffer_overruns
);
1803 static void rb_iter_reset(struct ring_buffer_iter
*iter
)
1805 struct ring_buffer_per_cpu
*cpu_buffer
= iter
->cpu_buffer
;
1807 /* Iterator usage is expected to have record disabled */
1808 if (list_empty(&cpu_buffer
->reader_page
->list
)) {
1809 iter
->head_page
= cpu_buffer
->head_page
;
1810 iter
->head
= cpu_buffer
->head_page
->read
;
1812 iter
->head_page
= cpu_buffer
->reader_page
;
1813 iter
->head
= cpu_buffer
->reader_page
->read
;
1816 iter
->read_stamp
= cpu_buffer
->read_stamp
;
1818 iter
->read_stamp
= iter
->head_page
->page
->time_stamp
;
1822 * ring_buffer_iter_reset - reset an iterator
1823 * @iter: The iterator to reset
1825 * Resets the iterator, so that it will start from the beginning
1828 void ring_buffer_iter_reset(struct ring_buffer_iter
*iter
)
1830 struct ring_buffer_per_cpu
*cpu_buffer
;
1831 unsigned long flags
;
1836 cpu_buffer
= iter
->cpu_buffer
;
1838 spin_lock_irqsave(&cpu_buffer
->reader_lock
, flags
);
1839 rb_iter_reset(iter
);
1840 spin_unlock_irqrestore(&cpu_buffer
->reader_lock
, flags
);
1842 EXPORT_SYMBOL_GPL(ring_buffer_iter_reset
);
1845 * ring_buffer_iter_empty - check if an iterator has no more to read
1846 * @iter: The iterator to check
1848 int ring_buffer_iter_empty(struct ring_buffer_iter
*iter
)
1850 struct ring_buffer_per_cpu
*cpu_buffer
;
1852 cpu_buffer
= iter
->cpu_buffer
;
1854 return iter
->head_page
== cpu_buffer
->commit_page
&&
1855 iter
->head
== rb_commit_index(cpu_buffer
);
1857 EXPORT_SYMBOL_GPL(ring_buffer_iter_empty
);
1860 rb_update_read_stamp(struct ring_buffer_per_cpu
*cpu_buffer
,
1861 struct ring_buffer_event
*event
)
1865 switch (event
->type
) {
1866 case RINGBUF_TYPE_PADDING
:
1869 case RINGBUF_TYPE_TIME_EXTEND
:
1870 delta
= event
->array
[0];
1872 delta
+= event
->time_delta
;
1873 cpu_buffer
->read_stamp
+= delta
;
1876 case RINGBUF_TYPE_TIME_STAMP
:
1877 /* FIXME: not implemented */
1880 case RINGBUF_TYPE_DATA
:
1881 cpu_buffer
->read_stamp
+= event
->time_delta
;
1891 rb_update_iter_read_stamp(struct ring_buffer_iter
*iter
,
1892 struct ring_buffer_event
*event
)
1896 switch (event
->type
) {
1897 case RINGBUF_TYPE_PADDING
:
1900 case RINGBUF_TYPE_TIME_EXTEND
:
1901 delta
= event
->array
[0];
1903 delta
+= event
->time_delta
;
1904 iter
->read_stamp
+= delta
;
1907 case RINGBUF_TYPE_TIME_STAMP
:
1908 /* FIXME: not implemented */
1911 case RINGBUF_TYPE_DATA
:
1912 iter
->read_stamp
+= event
->time_delta
;
1921 static struct buffer_page
*
1922 rb_get_reader_page(struct ring_buffer_per_cpu
*cpu_buffer
)
1924 struct buffer_page
*reader
= NULL
;
1925 unsigned long flags
;
1928 local_irq_save(flags
);
1929 __raw_spin_lock(&cpu_buffer
->lock
);
1933 * This should normally only loop twice. But because the
1934 * start of the reader inserts an empty page, it causes
1935 * a case where we will loop three times. There should be no
1936 * reason to loop four times (that I know of).
1938 if (RB_WARN_ON(cpu_buffer
, ++nr_loops
> 3)) {
1943 reader
= cpu_buffer
->reader_page
;
1945 /* If there's more to read, return this page */
1946 if (cpu_buffer
->reader_page
->read
< rb_page_size(reader
))
1949 /* Never should we have an index greater than the size */
1950 if (RB_WARN_ON(cpu_buffer
,
1951 cpu_buffer
->reader_page
->read
> rb_page_size(reader
)))
1954 /* check if we caught up to the tail */
1956 if (cpu_buffer
->commit_page
== cpu_buffer
->reader_page
)
1960 * Splice the empty reader page into the list around the head.
1961 * Reset the reader page to size zero.
1964 reader
= cpu_buffer
->head_page
;
1965 cpu_buffer
->reader_page
->list
.next
= reader
->list
.next
;
1966 cpu_buffer
->reader_page
->list
.prev
= reader
->list
.prev
;
1968 local_set(&cpu_buffer
->reader_page
->write
, 0);
1969 local_set(&cpu_buffer
->reader_page
->page
->commit
, 0);
1971 /* Make the reader page now replace the head */
1972 reader
->list
.prev
->next
= &cpu_buffer
->reader_page
->list
;
1973 reader
->list
.next
->prev
= &cpu_buffer
->reader_page
->list
;
1976 * If the tail is on the reader, then we must set the head
1977 * to the inserted page, otherwise we set it one before.
1979 cpu_buffer
->head_page
= cpu_buffer
->reader_page
;
1981 if (cpu_buffer
->commit_page
!= reader
)
1982 rb_inc_page(cpu_buffer
, &cpu_buffer
->head_page
);
1984 /* Finally update the reader page to the new head */
1985 cpu_buffer
->reader_page
= reader
;
1986 rb_reset_reader_page(cpu_buffer
);
1991 __raw_spin_unlock(&cpu_buffer
->lock
);
1992 local_irq_restore(flags
);
1997 static void rb_advance_reader(struct ring_buffer_per_cpu
*cpu_buffer
)
1999 struct ring_buffer_event
*event
;
2000 struct buffer_page
*reader
;
2003 reader
= rb_get_reader_page(cpu_buffer
);
2005 /* This function should not be called when buffer is empty */
2006 if (RB_WARN_ON(cpu_buffer
, !reader
))
2009 event
= rb_reader_event(cpu_buffer
);
2011 if (event
->type
== RINGBUF_TYPE_DATA
|| rb_discarded_event(event
))
2012 cpu_buffer
->entries
--;
2014 rb_update_read_stamp(cpu_buffer
, event
);
2016 length
= rb_event_length(event
);
2017 cpu_buffer
->reader_page
->read
+= length
;
2020 static void rb_advance_iter(struct ring_buffer_iter
*iter
)
2022 struct ring_buffer
*buffer
;
2023 struct ring_buffer_per_cpu
*cpu_buffer
;
2024 struct ring_buffer_event
*event
;
2027 cpu_buffer
= iter
->cpu_buffer
;
2028 buffer
= cpu_buffer
->buffer
;
2031 * Check if we are at the end of the buffer.
2033 if (iter
->head
>= rb_page_size(iter
->head_page
)) {
2034 if (RB_WARN_ON(buffer
,
2035 iter
->head_page
== cpu_buffer
->commit_page
))
2041 event
= rb_iter_head_event(iter
);
2043 length
= rb_event_length(event
);
2046 * This should not be called to advance the header if we are
2047 * at the tail of the buffer.
2049 if (RB_WARN_ON(cpu_buffer
,
2050 (iter
->head_page
== cpu_buffer
->commit_page
) &&
2051 (iter
->head
+ length
> rb_commit_index(cpu_buffer
))))
2054 rb_update_iter_read_stamp(iter
, event
);
2056 iter
->head
+= length
;
2058 /* check for end of page padding */
2059 if ((iter
->head
>= rb_page_size(iter
->head_page
)) &&
2060 (iter
->head_page
!= cpu_buffer
->commit_page
))
2061 rb_advance_iter(iter
);
2064 static struct ring_buffer_event
*
2065 rb_buffer_peek(struct ring_buffer
*buffer
, int cpu
, u64
*ts
)
2067 struct ring_buffer_per_cpu
*cpu_buffer
;
2068 struct ring_buffer_event
*event
;
2069 struct buffer_page
*reader
;
2072 cpu_buffer
= buffer
->buffers
[cpu
];
2076 * We repeat when a timestamp is encountered. It is possible
2077 * to get multiple timestamps from an interrupt entering just
2078 * as one timestamp is about to be written. The max times
2079 * that this can happen is the number of nested interrupts we
2080 * can have. Nesting 10 deep of interrupts is clearly
2083 if (RB_WARN_ON(cpu_buffer
, ++nr_loops
> 10))
2086 reader
= rb_get_reader_page(cpu_buffer
);
2090 event
= rb_reader_event(cpu_buffer
);
2092 switch (event
->type
) {
2093 case RINGBUF_TYPE_PADDING
:
2094 if (rb_null_event(event
))
2095 RB_WARN_ON(cpu_buffer
, 1);
2097 * Because the writer could be discarding every
2098 * event it creates (which would probably be bad)
2099 * if we were to go back to "again" then we may never
2100 * catch up, and will trigger the warn on, or lock
2101 * the box. Return the padding, and we will release
2102 * the current locks, and try again.
2104 rb_advance_reader(cpu_buffer
);
2107 case RINGBUF_TYPE_TIME_EXTEND
:
2108 /* Internal data, OK to advance */
2109 rb_advance_reader(cpu_buffer
);
2112 case RINGBUF_TYPE_TIME_STAMP
:
2113 /* FIXME: not implemented */
2114 rb_advance_reader(cpu_buffer
);
2117 case RINGBUF_TYPE_DATA
:
2119 *ts
= cpu_buffer
->read_stamp
+ event
->time_delta
;
2120 ring_buffer_normalize_time_stamp(buffer
,
2121 cpu_buffer
->cpu
, ts
);
2131 EXPORT_SYMBOL_GPL(ring_buffer_peek
);
2133 static struct ring_buffer_event
*
2134 rb_iter_peek(struct ring_buffer_iter
*iter
, u64
*ts
)
2136 struct ring_buffer
*buffer
;
2137 struct ring_buffer_per_cpu
*cpu_buffer
;
2138 struct ring_buffer_event
*event
;
2141 if (ring_buffer_iter_empty(iter
))
2144 cpu_buffer
= iter
->cpu_buffer
;
2145 buffer
= cpu_buffer
->buffer
;
2149 * We repeat when a timestamp is encountered. It is possible
2150 * to get multiple timestamps from an interrupt entering just
2151 * as one timestamp is about to be written. The max times
2152 * that this can happen is the number of nested interrupts we
2153 * can have. Nesting 10 deep of interrupts is clearly
2156 if (RB_WARN_ON(cpu_buffer
, ++nr_loops
> 10))
2159 if (rb_per_cpu_empty(cpu_buffer
))
2162 event
= rb_iter_head_event(iter
);
2164 switch (event
->type
) {
2165 case RINGBUF_TYPE_PADDING
:
2166 if (rb_null_event(event
)) {
2170 rb_advance_iter(iter
);
2173 case RINGBUF_TYPE_TIME_EXTEND
:
2174 /* Internal data, OK to advance */
2175 rb_advance_iter(iter
);
2178 case RINGBUF_TYPE_TIME_STAMP
:
2179 /* FIXME: not implemented */
2180 rb_advance_iter(iter
);
2183 case RINGBUF_TYPE_DATA
:
2185 *ts
= iter
->read_stamp
+ event
->time_delta
;
2186 ring_buffer_normalize_time_stamp(buffer
,
2187 cpu_buffer
->cpu
, ts
);
2197 EXPORT_SYMBOL_GPL(ring_buffer_iter_peek
);
2200 * ring_buffer_peek - peek at the next event to be read
2201 * @buffer: The ring buffer to read
2202 * @cpu: The cpu to peak at
2203 * @ts: The timestamp counter of this event.
2205 * This will return the event that will be read next, but does
2206 * not consume the data.
2208 struct ring_buffer_event
*
2209 ring_buffer_peek(struct ring_buffer
*buffer
, int cpu
, u64
*ts
)
2211 struct ring_buffer_per_cpu
*cpu_buffer
= buffer
->buffers
[cpu
];
2212 struct ring_buffer_event
*event
;
2213 unsigned long flags
;
2215 if (!cpumask_test_cpu(cpu
, buffer
->cpumask
))
2219 spin_lock_irqsave(&cpu_buffer
->reader_lock
, flags
);
2220 event
= rb_buffer_peek(buffer
, cpu
, ts
);
2221 spin_unlock_irqrestore(&cpu_buffer
->reader_lock
, flags
);
2223 if (event
&& event
->type
== RINGBUF_TYPE_PADDING
) {
2232 * ring_buffer_iter_peek - peek at the next event to be read
2233 * @iter: The ring buffer iterator
2234 * @ts: The timestamp counter of this event.
2236 * This will return the event that will be read next, but does
2237 * not increment the iterator.
2239 struct ring_buffer_event
*
2240 ring_buffer_iter_peek(struct ring_buffer_iter
*iter
, u64
*ts
)
2242 struct ring_buffer_per_cpu
*cpu_buffer
= iter
->cpu_buffer
;
2243 struct ring_buffer_event
*event
;
2244 unsigned long flags
;
2247 spin_lock_irqsave(&cpu_buffer
->reader_lock
, flags
);
2248 event
= rb_iter_peek(iter
, ts
);
2249 spin_unlock_irqrestore(&cpu_buffer
->reader_lock
, flags
);
2251 if (event
&& event
->type
== RINGBUF_TYPE_PADDING
) {
2260 * ring_buffer_consume - return an event and consume it
2261 * @buffer: The ring buffer to get the next event from
2263 * Returns the next event in the ring buffer, and that event is consumed.
2264 * Meaning, that sequential reads will keep returning a different event,
2265 * and eventually empty the ring buffer if the producer is slower.
2267 struct ring_buffer_event
*
2268 ring_buffer_consume(struct ring_buffer
*buffer
, int cpu
, u64
*ts
)
2270 struct ring_buffer_per_cpu
*cpu_buffer
;
2271 struct ring_buffer_event
*event
= NULL
;
2272 unsigned long flags
;
2275 /* might be called in atomic */
2278 if (!cpumask_test_cpu(cpu
, buffer
->cpumask
))
2281 cpu_buffer
= buffer
->buffers
[cpu
];
2282 spin_lock_irqsave(&cpu_buffer
->reader_lock
, flags
);
2284 event
= rb_buffer_peek(buffer
, cpu
, ts
);
2288 rb_advance_reader(cpu_buffer
);
2291 spin_unlock_irqrestore(&cpu_buffer
->reader_lock
, flags
);
2296 if (event
&& event
->type
== RINGBUF_TYPE_PADDING
) {
2303 EXPORT_SYMBOL_GPL(ring_buffer_consume
);
2306 * ring_buffer_read_start - start a non consuming read of the buffer
2307 * @buffer: The ring buffer to read from
2308 * @cpu: The cpu buffer to iterate over
2310 * This starts up an iteration through the buffer. It also disables
2311 * the recording to the buffer until the reading is finished.
2312 * This prevents the reading from being corrupted. This is not
2313 * a consuming read, so a producer is not expected.
2315 * Must be paired with ring_buffer_finish.
2317 struct ring_buffer_iter
*
2318 ring_buffer_read_start(struct ring_buffer
*buffer
, int cpu
)
2320 struct ring_buffer_per_cpu
*cpu_buffer
;
2321 struct ring_buffer_iter
*iter
;
2322 unsigned long flags
;
2324 if (!cpumask_test_cpu(cpu
, buffer
->cpumask
))
2327 iter
= kmalloc(sizeof(*iter
), GFP_KERNEL
);
2331 cpu_buffer
= buffer
->buffers
[cpu
];
2333 iter
->cpu_buffer
= cpu_buffer
;
2335 atomic_inc(&cpu_buffer
->record_disabled
);
2336 synchronize_sched();
2338 spin_lock_irqsave(&cpu_buffer
->reader_lock
, flags
);
2339 __raw_spin_lock(&cpu_buffer
->lock
);
2340 rb_iter_reset(iter
);
2341 __raw_spin_unlock(&cpu_buffer
->lock
);
2342 spin_unlock_irqrestore(&cpu_buffer
->reader_lock
, flags
);
2346 EXPORT_SYMBOL_GPL(ring_buffer_read_start
);
2349 * ring_buffer_finish - finish reading the iterator of the buffer
2350 * @iter: The iterator retrieved by ring_buffer_start
2352 * This re-enables the recording to the buffer, and frees the
2356 ring_buffer_read_finish(struct ring_buffer_iter
*iter
)
2358 struct ring_buffer_per_cpu
*cpu_buffer
= iter
->cpu_buffer
;
2360 atomic_dec(&cpu_buffer
->record_disabled
);
2363 EXPORT_SYMBOL_GPL(ring_buffer_read_finish
);
2366 * ring_buffer_read - read the next item in the ring buffer by the iterator
2367 * @iter: The ring buffer iterator
2368 * @ts: The time stamp of the event read.
2370 * This reads the next event in the ring buffer and increments the iterator.
2372 struct ring_buffer_event
*
2373 ring_buffer_read(struct ring_buffer_iter
*iter
, u64
*ts
)
2375 struct ring_buffer_event
*event
;
2376 struct ring_buffer_per_cpu
*cpu_buffer
= iter
->cpu_buffer
;
2377 unsigned long flags
;
2380 spin_lock_irqsave(&cpu_buffer
->reader_lock
, flags
);
2381 event
= rb_iter_peek(iter
, ts
);
2385 rb_advance_iter(iter
);
2387 spin_unlock_irqrestore(&cpu_buffer
->reader_lock
, flags
);
2389 if (event
&& event
->type
== RINGBUF_TYPE_PADDING
) {
2396 EXPORT_SYMBOL_GPL(ring_buffer_read
);
2399 * ring_buffer_size - return the size of the ring buffer (in bytes)
2400 * @buffer: The ring buffer.
2402 unsigned long ring_buffer_size(struct ring_buffer
*buffer
)
2404 return BUF_PAGE_SIZE
* buffer
->pages
;
2406 EXPORT_SYMBOL_GPL(ring_buffer_size
);
2409 rb_reset_cpu(struct ring_buffer_per_cpu
*cpu_buffer
)
2411 cpu_buffer
->head_page
2412 = list_entry(cpu_buffer
->pages
.next
, struct buffer_page
, list
);
2413 local_set(&cpu_buffer
->head_page
->write
, 0);
2414 local_set(&cpu_buffer
->head_page
->page
->commit
, 0);
2416 cpu_buffer
->head_page
->read
= 0;
2418 cpu_buffer
->tail_page
= cpu_buffer
->head_page
;
2419 cpu_buffer
->commit_page
= cpu_buffer
->head_page
;
2421 INIT_LIST_HEAD(&cpu_buffer
->reader_page
->list
);
2422 local_set(&cpu_buffer
->reader_page
->write
, 0);
2423 local_set(&cpu_buffer
->reader_page
->page
->commit
, 0);
2424 cpu_buffer
->reader_page
->read
= 0;
2426 cpu_buffer
->overrun
= 0;
2427 cpu_buffer
->entries
= 0;
2429 cpu_buffer
->write_stamp
= 0;
2430 cpu_buffer
->read_stamp
= 0;
2434 * ring_buffer_reset_cpu - reset a ring buffer per CPU buffer
2435 * @buffer: The ring buffer to reset a per cpu buffer of
2436 * @cpu: The CPU buffer to be reset
2438 void ring_buffer_reset_cpu(struct ring_buffer
*buffer
, int cpu
)
2440 struct ring_buffer_per_cpu
*cpu_buffer
= buffer
->buffers
[cpu
];
2441 unsigned long flags
;
2443 if (!cpumask_test_cpu(cpu
, buffer
->cpumask
))
2446 spin_lock_irqsave(&cpu_buffer
->reader_lock
, flags
);
2448 __raw_spin_lock(&cpu_buffer
->lock
);
2450 rb_reset_cpu(cpu_buffer
);
2452 __raw_spin_unlock(&cpu_buffer
->lock
);
2454 spin_unlock_irqrestore(&cpu_buffer
->reader_lock
, flags
);
2456 EXPORT_SYMBOL_GPL(ring_buffer_reset_cpu
);
2459 * ring_buffer_reset - reset a ring buffer
2460 * @buffer: The ring buffer to reset all cpu buffers
2462 void ring_buffer_reset(struct ring_buffer
*buffer
)
2466 for_each_buffer_cpu(buffer
, cpu
)
2467 ring_buffer_reset_cpu(buffer
, cpu
);
2469 EXPORT_SYMBOL_GPL(ring_buffer_reset
);
2472 * rind_buffer_empty - is the ring buffer empty?
2473 * @buffer: The ring buffer to test
2475 int ring_buffer_empty(struct ring_buffer
*buffer
)
2477 struct ring_buffer_per_cpu
*cpu_buffer
;
2480 /* yes this is racy, but if you don't like the race, lock the buffer */
2481 for_each_buffer_cpu(buffer
, cpu
) {
2482 cpu_buffer
= buffer
->buffers
[cpu
];
2483 if (!rb_per_cpu_empty(cpu_buffer
))
2489 EXPORT_SYMBOL_GPL(ring_buffer_empty
);
2492 * ring_buffer_empty_cpu - is a cpu buffer of a ring buffer empty?
2493 * @buffer: The ring buffer
2494 * @cpu: The CPU buffer to test
2496 int ring_buffer_empty_cpu(struct ring_buffer
*buffer
, int cpu
)
2498 struct ring_buffer_per_cpu
*cpu_buffer
;
2501 if (!cpumask_test_cpu(cpu
, buffer
->cpumask
))
2504 cpu_buffer
= buffer
->buffers
[cpu
];
2505 ret
= rb_per_cpu_empty(cpu_buffer
);
2510 EXPORT_SYMBOL_GPL(ring_buffer_empty_cpu
);
2513 * ring_buffer_swap_cpu - swap a CPU buffer between two ring buffers
2514 * @buffer_a: One buffer to swap with
2515 * @buffer_b: The other buffer to swap with
2517 * This function is useful for tracers that want to take a "snapshot"
2518 * of a CPU buffer and has another back up buffer lying around.
2519 * it is expected that the tracer handles the cpu buffer not being
2520 * used at the moment.
2522 int ring_buffer_swap_cpu(struct ring_buffer
*buffer_a
,
2523 struct ring_buffer
*buffer_b
, int cpu
)
2525 struct ring_buffer_per_cpu
*cpu_buffer_a
;
2526 struct ring_buffer_per_cpu
*cpu_buffer_b
;
2529 if (!cpumask_test_cpu(cpu
, buffer_a
->cpumask
) ||
2530 !cpumask_test_cpu(cpu
, buffer_b
->cpumask
))
2533 /* At least make sure the two buffers are somewhat the same */
2534 if (buffer_a
->pages
!= buffer_b
->pages
)
2539 if (ring_buffer_flags
!= RB_BUFFERS_ON
)
2542 if (atomic_read(&buffer_a
->record_disabled
))
2545 if (atomic_read(&buffer_b
->record_disabled
))
2548 cpu_buffer_a
= buffer_a
->buffers
[cpu
];
2549 cpu_buffer_b
= buffer_b
->buffers
[cpu
];
2551 if (atomic_read(&cpu_buffer_a
->record_disabled
))
2554 if (atomic_read(&cpu_buffer_b
->record_disabled
))
2558 * We can't do a synchronize_sched here because this
2559 * function can be called in atomic context.
2560 * Normally this will be called from the same CPU as cpu.
2561 * If not it's up to the caller to protect this.
2563 atomic_inc(&cpu_buffer_a
->record_disabled
);
2564 atomic_inc(&cpu_buffer_b
->record_disabled
);
2566 buffer_a
->buffers
[cpu
] = cpu_buffer_b
;
2567 buffer_b
->buffers
[cpu
] = cpu_buffer_a
;
2569 cpu_buffer_b
->buffer
= buffer_a
;
2570 cpu_buffer_a
->buffer
= buffer_b
;
2572 atomic_dec(&cpu_buffer_a
->record_disabled
);
2573 atomic_dec(&cpu_buffer_b
->record_disabled
);
2579 EXPORT_SYMBOL_GPL(ring_buffer_swap_cpu
);
2581 static void rb_remove_entries(struct ring_buffer_per_cpu
*cpu_buffer
,
2582 struct buffer_data_page
*bpage
,
2583 unsigned int offset
)
2585 struct ring_buffer_event
*event
;
2588 __raw_spin_lock(&cpu_buffer
->lock
);
2589 for (head
= offset
; head
< local_read(&bpage
->commit
);
2590 head
+= rb_event_length(event
)) {
2592 event
= __rb_data_page_index(bpage
, head
);
2593 if (RB_WARN_ON(cpu_buffer
, rb_null_event(event
)))
2595 /* Only count data entries */
2596 if (event
->type
!= RINGBUF_TYPE_DATA
)
2598 cpu_buffer
->entries
--;
2600 __raw_spin_unlock(&cpu_buffer
->lock
);
2604 * ring_buffer_alloc_read_page - allocate a page to read from buffer
2605 * @buffer: the buffer to allocate for.
2607 * This function is used in conjunction with ring_buffer_read_page.
2608 * When reading a full page from the ring buffer, these functions
2609 * can be used to speed up the process. The calling function should
2610 * allocate a few pages first with this function. Then when it
2611 * needs to get pages from the ring buffer, it passes the result
2612 * of this function into ring_buffer_read_page, which will swap
2613 * the page that was allocated, with the read page of the buffer.
2616 * The page allocated, or NULL on error.
2618 void *ring_buffer_alloc_read_page(struct ring_buffer
*buffer
)
2620 struct buffer_data_page
*bpage
;
2623 addr
= __get_free_page(GFP_KERNEL
);
2627 bpage
= (void *)addr
;
2629 rb_init_page(bpage
);
2635 * ring_buffer_free_read_page - free an allocated read page
2636 * @buffer: the buffer the page was allocate for
2637 * @data: the page to free
2639 * Free a page allocated from ring_buffer_alloc_read_page.
2641 void ring_buffer_free_read_page(struct ring_buffer
*buffer
, void *data
)
2643 free_page((unsigned long)data
);
2647 * ring_buffer_read_page - extract a page from the ring buffer
2648 * @buffer: buffer to extract from
2649 * @data_page: the page to use allocated from ring_buffer_alloc_read_page
2650 * @len: amount to extract
2651 * @cpu: the cpu of the buffer to extract
2652 * @full: should the extraction only happen when the page is full.
2654 * This function will pull out a page from the ring buffer and consume it.
2655 * @data_page must be the address of the variable that was returned
2656 * from ring_buffer_alloc_read_page. This is because the page might be used
2657 * to swap with a page in the ring buffer.
2660 * rpage = ring_buffer_alloc_read_page(buffer);
2663 * ret = ring_buffer_read_page(buffer, &rpage, len, cpu, 0);
2665 * process_page(rpage, ret);
2667 * When @full is set, the function will not return true unless
2668 * the writer is off the reader page.
2670 * Note: it is up to the calling functions to handle sleeps and wakeups.
2671 * The ring buffer can be used anywhere in the kernel and can not
2672 * blindly call wake_up. The layer that uses the ring buffer must be
2673 * responsible for that.
2676 * >=0 if data has been transferred, returns the offset of consumed data.
2677 * <0 if no data has been transferred.
2679 int ring_buffer_read_page(struct ring_buffer
*buffer
,
2680 void **data_page
, size_t len
, int cpu
, int full
)
2682 struct ring_buffer_per_cpu
*cpu_buffer
= buffer
->buffers
[cpu
];
2683 struct ring_buffer_event
*event
;
2684 struct buffer_data_page
*bpage
;
2685 struct buffer_page
*reader
;
2686 unsigned long flags
;
2687 unsigned int commit
;
2692 if (!cpumask_test_cpu(cpu
, buffer
->cpumask
))
2696 * If len is not big enough to hold the page header, then
2697 * we can not copy anything.
2699 if (len
<= BUF_PAGE_HDR_SIZE
)
2702 len
-= BUF_PAGE_HDR_SIZE
;
2711 spin_lock_irqsave(&cpu_buffer
->reader_lock
, flags
);
2713 reader
= rb_get_reader_page(cpu_buffer
);
2717 event
= rb_reader_event(cpu_buffer
);
2719 read
= reader
->read
;
2720 commit
= rb_page_commit(reader
);
2723 * If this page has been partially read or
2724 * if len is not big enough to read the rest of the page or
2725 * a writer is still on the page, then
2726 * we must copy the data from the page to the buffer.
2727 * Otherwise, we can simply swap the page with the one passed in.
2729 if (read
|| (len
< (commit
- read
)) ||
2730 cpu_buffer
->reader_page
== cpu_buffer
->commit_page
) {
2731 struct buffer_data_page
*rpage
= cpu_buffer
->reader_page
->page
;
2732 unsigned int rpos
= read
;
2733 unsigned int pos
= 0;
2739 if (len
> (commit
- read
))
2740 len
= (commit
- read
);
2742 size
= rb_event_length(event
);
2747 /* save the current timestamp, since the user will need it */
2748 save_timestamp
= cpu_buffer
->read_stamp
;
2750 /* Need to copy one event at a time */
2752 memcpy(bpage
->data
+ pos
, rpage
->data
+ rpos
, size
);
2756 rb_advance_reader(cpu_buffer
);
2757 rpos
= reader
->read
;
2760 event
= rb_reader_event(cpu_buffer
);
2761 size
= rb_event_length(event
);
2762 } while (len
> size
);
2765 local_set(&bpage
->commit
, pos
);
2766 bpage
->time_stamp
= save_timestamp
;
2768 /* we copied everything to the beginning */
2771 /* swap the pages */
2772 rb_init_page(bpage
);
2773 bpage
= reader
->page
;
2774 reader
->page
= *data_page
;
2775 local_set(&reader
->write
, 0);
2779 /* update the entry counter */
2780 rb_remove_entries(cpu_buffer
, bpage
, read
);
2785 spin_unlock_irqrestore(&cpu_buffer
->reader_lock
, flags
);
2792 rb_simple_read(struct file
*filp
, char __user
*ubuf
,
2793 size_t cnt
, loff_t
*ppos
)
2795 unsigned long *p
= filp
->private_data
;
2799 if (test_bit(RB_BUFFERS_DISABLED_BIT
, p
))
2800 r
= sprintf(buf
, "permanently disabled\n");
2802 r
= sprintf(buf
, "%d\n", test_bit(RB_BUFFERS_ON_BIT
, p
));
2804 return simple_read_from_buffer(ubuf
, cnt
, ppos
, buf
, r
);
2808 rb_simple_write(struct file
*filp
, const char __user
*ubuf
,
2809 size_t cnt
, loff_t
*ppos
)
2811 unsigned long *p
= filp
->private_data
;
2816 if (cnt
>= sizeof(buf
))
2819 if (copy_from_user(&buf
, ubuf
, cnt
))
2824 ret
= strict_strtoul(buf
, 10, &val
);
2829 set_bit(RB_BUFFERS_ON_BIT
, p
);
2831 clear_bit(RB_BUFFERS_ON_BIT
, p
);
2838 static const struct file_operations rb_simple_fops
= {
2839 .open
= tracing_open_generic
,
2840 .read
= rb_simple_read
,
2841 .write
= rb_simple_write
,
2845 static __init
int rb_init_debugfs(void)
2847 struct dentry
*d_tracer
;
2848 struct dentry
*entry
;
2850 d_tracer
= tracing_init_dentry();
2852 entry
= debugfs_create_file("tracing_on", 0644, d_tracer
,
2853 &ring_buffer_flags
, &rb_simple_fops
);
2855 pr_warning("Could not create debugfs 'tracing_on' entry\n");
2860 fs_initcall(rb_init_debugfs
);
2862 #ifdef CONFIG_HOTPLUG_CPU
2863 static int rb_cpu_notify(struct notifier_block
*self
,
2864 unsigned long action
, void *hcpu
)
2866 struct ring_buffer
*buffer
=
2867 container_of(self
, struct ring_buffer
, cpu_notify
);
2868 long cpu
= (long)hcpu
;
2871 case CPU_UP_PREPARE
:
2872 case CPU_UP_PREPARE_FROZEN
:
2873 if (cpu_isset(cpu
, *buffer
->cpumask
))
2876 buffer
->buffers
[cpu
] =
2877 rb_allocate_cpu_buffer(buffer
, cpu
);
2878 if (!buffer
->buffers
[cpu
]) {
2879 WARN(1, "failed to allocate ring buffer on CPU %ld\n",
2884 cpu_set(cpu
, *buffer
->cpumask
);
2886 case CPU_DOWN_PREPARE
:
2887 case CPU_DOWN_PREPARE_FROZEN
:
2890 * If we were to free the buffer, then the user would
2891 * lose any trace that was in the buffer.