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 struct list_head list; /* list of buffer pages */
325 local_t write; /* index for next write */
326 unsigned read; /* index for next read */
327 local_t entries; /* entries on this page */
328 struct buffer_data_page *page; /* Actual data page */
331 static void rb_init_page(struct buffer_data_page *bpage)
333 local_set(&bpage->commit, 0);
337 * ring_buffer_page_len - the size of data on the page.
338 * @page: The page to read
340 * Returns the amount of data on the page, including buffer page header.
342 size_t ring_buffer_page_len(void *page)
344 return local_read(&((struct buffer_data_page *)page)->commit)
349 * Also stolen from mm/slob.c. Thanks to Mathieu Desnoyers for pointing
352 static void free_buffer_page(struct buffer_page *bpage)
354 free_page((unsigned long)bpage->page);
359 * We need to fit the time_stamp delta into 27 bits.
361 static inline int test_time_stamp(u64 delta)
363 if (delta & TS_DELTA_TEST)
368 #define BUF_PAGE_SIZE (PAGE_SIZE - BUF_PAGE_HDR_SIZE)
370 int ring_buffer_print_page_header(struct trace_seq *s)
372 struct buffer_data_page field;
375 ret = trace_seq_printf(s, "\tfield: u64 timestamp;\t"
376 "offset:0;\tsize:%u;\n",
377 (unsigned int)sizeof(field.time_stamp));
379 ret = trace_seq_printf(s, "\tfield: local_t commit;\t"
380 "offset:%u;\tsize:%u;\n",
381 (unsigned int)offsetof(typeof(field), commit),
382 (unsigned int)sizeof(field.commit));
384 ret = trace_seq_printf(s, "\tfield: char data;\t"
385 "offset:%u;\tsize:%u;\n",
386 (unsigned int)offsetof(typeof(field), data),
387 (unsigned int)BUF_PAGE_SIZE);
393 * head_page == tail_page && head == tail then buffer is empty.
395 struct ring_buffer_per_cpu {
397 struct ring_buffer *buffer;
398 spinlock_t reader_lock; /* serialize readers */
400 struct lock_class_key lock_key;
401 struct list_head pages;
402 struct buffer_page *head_page; /* read from head */
403 struct buffer_page *tail_page; /* write to tail */
404 struct buffer_page *commit_page; /* committed pages */
405 struct buffer_page *reader_page;
406 unsigned long nmi_dropped;
407 unsigned long commit_overrun;
408 unsigned long overrun;
413 atomic_t record_disabled;
420 atomic_t record_disabled;
421 cpumask_var_t cpumask;
425 struct ring_buffer_per_cpu **buffers;
427 #ifdef CONFIG_HOTPLUG_CPU
428 struct notifier_block cpu_notify;
433 struct ring_buffer_iter {
434 struct ring_buffer_per_cpu *cpu_buffer;
436 struct buffer_page *head_page;
440 /* buffer may be either ring_buffer or ring_buffer_per_cpu */
441 #define RB_WARN_ON(buffer, cond) \
443 int _____ret = unlikely(cond); \
445 atomic_inc(&buffer->record_disabled); \
451 /* Up this if you want to test the TIME_EXTENTS and normalization */
452 #define DEBUG_SHIFT 0
454 u64 ring_buffer_time_stamp(struct ring_buffer *buffer, int cpu)
458 preempt_disable_notrace();
459 /* shift to debug/test normalization and TIME_EXTENTS */
460 time = buffer->clock() << DEBUG_SHIFT;
461 preempt_enable_no_resched_notrace();
465 EXPORT_SYMBOL_GPL(ring_buffer_time_stamp);
467 void ring_buffer_normalize_time_stamp(struct ring_buffer *buffer,
470 /* Just stupid testing the normalize function and deltas */
473 EXPORT_SYMBOL_GPL(ring_buffer_normalize_time_stamp);
476 * check_pages - integrity check of buffer pages
477 * @cpu_buffer: CPU buffer with pages to test
479 * As a safety measure we check to make sure the data pages have not
482 static int rb_check_pages(struct ring_buffer_per_cpu *cpu_buffer)
484 struct list_head *head = &cpu_buffer->pages;
485 struct buffer_page *bpage, *tmp;
487 if (RB_WARN_ON(cpu_buffer, head->next->prev != head))
489 if (RB_WARN_ON(cpu_buffer, head->prev->next != head))
492 list_for_each_entry_safe(bpage, tmp, head, list) {
493 if (RB_WARN_ON(cpu_buffer,
494 bpage->list.next->prev != &bpage->list))
496 if (RB_WARN_ON(cpu_buffer,
497 bpage->list.prev->next != &bpage->list))
504 static int rb_allocate_pages(struct ring_buffer_per_cpu *cpu_buffer,
507 struct list_head *head = &cpu_buffer->pages;
508 struct buffer_page *bpage, *tmp;
513 for (i = 0; i < nr_pages; i++) {
514 bpage = kzalloc_node(ALIGN(sizeof(*bpage), cache_line_size()),
515 GFP_KERNEL, cpu_to_node(cpu_buffer->cpu));
518 list_add(&bpage->list, &pages);
520 addr = __get_free_page(GFP_KERNEL);
523 bpage->page = (void *)addr;
524 rb_init_page(bpage->page);
527 list_splice(&pages, head);
529 rb_check_pages(cpu_buffer);
534 list_for_each_entry_safe(bpage, tmp, &pages, list) {
535 list_del_init(&bpage->list);
536 free_buffer_page(bpage);
541 static struct ring_buffer_per_cpu *
542 rb_allocate_cpu_buffer(struct ring_buffer *buffer, int cpu)
544 struct ring_buffer_per_cpu *cpu_buffer;
545 struct buffer_page *bpage;
549 cpu_buffer = kzalloc_node(ALIGN(sizeof(*cpu_buffer), cache_line_size()),
550 GFP_KERNEL, cpu_to_node(cpu));
554 cpu_buffer->cpu = cpu;
555 cpu_buffer->buffer = buffer;
556 spin_lock_init(&cpu_buffer->reader_lock);
557 cpu_buffer->lock = (raw_spinlock_t)__RAW_SPIN_LOCK_UNLOCKED;
558 INIT_LIST_HEAD(&cpu_buffer->pages);
560 bpage = kzalloc_node(ALIGN(sizeof(*bpage), cache_line_size()),
561 GFP_KERNEL, cpu_to_node(cpu));
563 goto fail_free_buffer;
565 cpu_buffer->reader_page = bpage;
566 addr = __get_free_page(GFP_KERNEL);
568 goto fail_free_reader;
569 bpage->page = (void *)addr;
570 rb_init_page(bpage->page);
572 INIT_LIST_HEAD(&cpu_buffer->reader_page->list);
574 ret = rb_allocate_pages(cpu_buffer, buffer->pages);
576 goto fail_free_reader;
578 cpu_buffer->head_page
579 = list_entry(cpu_buffer->pages.next, struct buffer_page, list);
580 cpu_buffer->tail_page = cpu_buffer->commit_page = cpu_buffer->head_page;
585 free_buffer_page(cpu_buffer->reader_page);
592 static void rb_free_cpu_buffer(struct ring_buffer_per_cpu *cpu_buffer)
594 struct list_head *head = &cpu_buffer->pages;
595 struct buffer_page *bpage, *tmp;
597 free_buffer_page(cpu_buffer->reader_page);
599 list_for_each_entry_safe(bpage, tmp, head, list) {
600 list_del_init(&bpage->list);
601 free_buffer_page(bpage);
607 * Causes compile errors if the struct buffer_page gets bigger
608 * than the struct page.
610 extern int ring_buffer_page_too_big(void);
612 #ifdef CONFIG_HOTPLUG_CPU
613 static int rb_cpu_notify(struct notifier_block *self,
614 unsigned long action, void *hcpu);
618 * ring_buffer_alloc - allocate a new ring_buffer
619 * @size: the size in bytes per cpu that is needed.
620 * @flags: attributes to set for the ring buffer.
622 * Currently the only flag that is available is the RB_FL_OVERWRITE
623 * flag. This flag means that the buffer will overwrite old data
624 * when the buffer wraps. If this flag is not set, the buffer will
625 * drop data when the tail hits the head.
627 struct ring_buffer *ring_buffer_alloc(unsigned long size, unsigned flags)
629 struct ring_buffer *buffer;
633 /* Paranoid! Optimizes out when all is well */
634 if (sizeof(struct buffer_page) > sizeof(struct page))
635 ring_buffer_page_too_big();
638 /* keep it in its own cache line */
639 buffer = kzalloc(ALIGN(sizeof(*buffer), cache_line_size()),
644 if (!alloc_cpumask_var(&buffer->cpumask, GFP_KERNEL))
645 goto fail_free_buffer;
647 buffer->pages = DIV_ROUND_UP(size, BUF_PAGE_SIZE);
648 buffer->flags = flags;
649 buffer->clock = trace_clock_local;
651 /* need at least two pages */
652 if (buffer->pages == 1)
656 * In case of non-hotplug cpu, if the ring-buffer is allocated
657 * in early initcall, it will not be notified of secondary cpus.
658 * In that off case, we need to allocate for all possible cpus.
660 #ifdef CONFIG_HOTPLUG_CPU
662 cpumask_copy(buffer->cpumask, cpu_online_mask);
664 cpumask_copy(buffer->cpumask, cpu_possible_mask);
666 buffer->cpus = nr_cpu_ids;
668 bsize = sizeof(void *) * nr_cpu_ids;
669 buffer->buffers = kzalloc(ALIGN(bsize, cache_line_size()),
671 if (!buffer->buffers)
672 goto fail_free_cpumask;
674 for_each_buffer_cpu(buffer, cpu) {
675 buffer->buffers[cpu] =
676 rb_allocate_cpu_buffer(buffer, cpu);
677 if (!buffer->buffers[cpu])
678 goto fail_free_buffers;
681 #ifdef CONFIG_HOTPLUG_CPU
682 buffer->cpu_notify.notifier_call = rb_cpu_notify;
683 buffer->cpu_notify.priority = 0;
684 register_cpu_notifier(&buffer->cpu_notify);
688 mutex_init(&buffer->mutex);
693 for_each_buffer_cpu(buffer, cpu) {
694 if (buffer->buffers[cpu])
695 rb_free_cpu_buffer(buffer->buffers[cpu]);
697 kfree(buffer->buffers);
700 free_cpumask_var(buffer->cpumask);
707 EXPORT_SYMBOL_GPL(ring_buffer_alloc);
710 * ring_buffer_free - free a ring buffer.
711 * @buffer: the buffer to free.
714 ring_buffer_free(struct ring_buffer *buffer)
720 #ifdef CONFIG_HOTPLUG_CPU
721 unregister_cpu_notifier(&buffer->cpu_notify);
724 for_each_buffer_cpu(buffer, cpu)
725 rb_free_cpu_buffer(buffer->buffers[cpu]);
729 free_cpumask_var(buffer->cpumask);
733 EXPORT_SYMBOL_GPL(ring_buffer_free);
735 void ring_buffer_set_clock(struct ring_buffer *buffer,
738 buffer->clock = clock;
741 static void rb_reset_cpu(struct ring_buffer_per_cpu *cpu_buffer);
744 rb_remove_pages(struct ring_buffer_per_cpu *cpu_buffer, unsigned nr_pages)
746 struct buffer_page *bpage;
750 atomic_inc(&cpu_buffer->record_disabled);
753 for (i = 0; i < nr_pages; i++) {
754 if (RB_WARN_ON(cpu_buffer, list_empty(&cpu_buffer->pages)))
756 p = cpu_buffer->pages.next;
757 bpage = list_entry(p, struct buffer_page, list);
758 list_del_init(&bpage->list);
759 free_buffer_page(bpage);
761 if (RB_WARN_ON(cpu_buffer, list_empty(&cpu_buffer->pages)))
764 rb_reset_cpu(cpu_buffer);
766 rb_check_pages(cpu_buffer);
768 atomic_dec(&cpu_buffer->record_disabled);
773 rb_insert_pages(struct ring_buffer_per_cpu *cpu_buffer,
774 struct list_head *pages, unsigned nr_pages)
776 struct buffer_page *bpage;
780 atomic_inc(&cpu_buffer->record_disabled);
783 for (i = 0; i < nr_pages; i++) {
784 if (RB_WARN_ON(cpu_buffer, list_empty(pages)))
787 bpage = list_entry(p, struct buffer_page, list);
788 list_del_init(&bpage->list);
789 list_add_tail(&bpage->list, &cpu_buffer->pages);
791 rb_reset_cpu(cpu_buffer);
793 rb_check_pages(cpu_buffer);
795 atomic_dec(&cpu_buffer->record_disabled);
799 * ring_buffer_resize - resize the ring buffer
800 * @buffer: the buffer to resize.
801 * @size: the new size.
803 * The tracer is responsible for making sure that the buffer is
804 * not being used while changing the size.
805 * Note: We may be able to change the above requirement by using
806 * RCU synchronizations.
808 * Minimum size is 2 * BUF_PAGE_SIZE.
810 * Returns -1 on failure.
812 int ring_buffer_resize(struct ring_buffer *buffer, unsigned long size)
814 struct ring_buffer_per_cpu *cpu_buffer;
815 unsigned nr_pages, rm_pages, new_pages;
816 struct buffer_page *bpage, *tmp;
817 unsigned long buffer_size;
823 * Always succeed at resizing a non-existent buffer:
828 size = DIV_ROUND_UP(size, BUF_PAGE_SIZE);
829 size *= BUF_PAGE_SIZE;
830 buffer_size = buffer->pages * BUF_PAGE_SIZE;
832 /* we need a minimum of two pages */
833 if (size < BUF_PAGE_SIZE * 2)
834 size = BUF_PAGE_SIZE * 2;
836 if (size == buffer_size)
839 mutex_lock(&buffer->mutex);
842 nr_pages = DIV_ROUND_UP(size, BUF_PAGE_SIZE);
844 if (size < buffer_size) {
846 /* easy case, just free pages */
847 if (RB_WARN_ON(buffer, nr_pages >= buffer->pages))
850 rm_pages = buffer->pages - nr_pages;
852 for_each_buffer_cpu(buffer, cpu) {
853 cpu_buffer = buffer->buffers[cpu];
854 rb_remove_pages(cpu_buffer, rm_pages);
860 * This is a bit more difficult. We only want to add pages
861 * when we can allocate enough for all CPUs. We do this
862 * by allocating all the pages and storing them on a local
863 * link list. If we succeed in our allocation, then we
864 * add these pages to the cpu_buffers. Otherwise we just free
865 * them all and return -ENOMEM;
867 if (RB_WARN_ON(buffer, nr_pages <= buffer->pages))
870 new_pages = nr_pages - buffer->pages;
872 for_each_buffer_cpu(buffer, cpu) {
873 for (i = 0; i < new_pages; i++) {
874 bpage = kzalloc_node(ALIGN(sizeof(*bpage),
876 GFP_KERNEL, cpu_to_node(cpu));
879 list_add(&bpage->list, &pages);
880 addr = __get_free_page(GFP_KERNEL);
883 bpage->page = (void *)addr;
884 rb_init_page(bpage->page);
888 for_each_buffer_cpu(buffer, cpu) {
889 cpu_buffer = buffer->buffers[cpu];
890 rb_insert_pages(cpu_buffer, &pages, new_pages);
893 if (RB_WARN_ON(buffer, !list_empty(&pages)))
897 buffer->pages = nr_pages;
899 mutex_unlock(&buffer->mutex);
904 list_for_each_entry_safe(bpage, tmp, &pages, list) {
905 list_del_init(&bpage->list);
906 free_buffer_page(bpage);
909 mutex_unlock(&buffer->mutex);
913 * Something went totally wrong, and we are too paranoid
914 * to even clean up the mess.
918 mutex_unlock(&buffer->mutex);
921 EXPORT_SYMBOL_GPL(ring_buffer_resize);
924 __rb_data_page_index(struct buffer_data_page *bpage, unsigned index)
926 return bpage->data + index;
929 static inline void *__rb_page_index(struct buffer_page *bpage, unsigned index)
931 return bpage->page->data + index;
934 static inline struct ring_buffer_event *
935 rb_reader_event(struct ring_buffer_per_cpu *cpu_buffer)
937 return __rb_page_index(cpu_buffer->reader_page,
938 cpu_buffer->reader_page->read);
941 static inline struct ring_buffer_event *
942 rb_head_event(struct ring_buffer_per_cpu *cpu_buffer)
944 return __rb_page_index(cpu_buffer->head_page,
945 cpu_buffer->head_page->read);
948 static inline struct ring_buffer_event *
949 rb_iter_head_event(struct ring_buffer_iter *iter)
951 return __rb_page_index(iter->head_page, iter->head);
954 static inline unsigned rb_page_write(struct buffer_page *bpage)
956 return local_read(&bpage->write);
959 static inline unsigned rb_page_commit(struct buffer_page *bpage)
961 return local_read(&bpage->page->commit);
964 /* Size is determined by what has been commited */
965 static inline unsigned rb_page_size(struct buffer_page *bpage)
967 return rb_page_commit(bpage);
970 static inline unsigned
971 rb_commit_index(struct ring_buffer_per_cpu *cpu_buffer)
973 return rb_page_commit(cpu_buffer->commit_page);
976 static inline unsigned rb_head_size(struct ring_buffer_per_cpu *cpu_buffer)
978 return rb_page_commit(cpu_buffer->head_page);
981 static inline void rb_inc_page(struct ring_buffer_per_cpu *cpu_buffer,
982 struct buffer_page **bpage)
984 struct list_head *p = (*bpage)->list.next;
986 if (p == &cpu_buffer->pages)
989 *bpage = list_entry(p, struct buffer_page, list);
992 static inline unsigned
993 rb_event_index(struct ring_buffer_event *event)
995 unsigned long addr = (unsigned long)event;
997 return (addr & ~PAGE_MASK) - (PAGE_SIZE - BUF_PAGE_SIZE);
1001 rb_is_commit(struct ring_buffer_per_cpu *cpu_buffer,
1002 struct ring_buffer_event *event)
1004 unsigned long addr = (unsigned long)event;
1005 unsigned long index;
1007 index = rb_event_index(event);
1010 return cpu_buffer->commit_page->page == (void *)addr &&
1011 rb_commit_index(cpu_buffer) == index;
1015 rb_set_commit_event(struct ring_buffer_per_cpu *cpu_buffer,
1016 struct ring_buffer_event *event)
1018 unsigned long addr = (unsigned long)event;
1019 unsigned long index;
1021 index = rb_event_index(event);
1024 while (cpu_buffer->commit_page->page != (void *)addr) {
1025 if (RB_WARN_ON(cpu_buffer,
1026 cpu_buffer->commit_page == cpu_buffer->tail_page))
1028 cpu_buffer->commit_page->page->commit =
1029 cpu_buffer->commit_page->write;
1030 rb_inc_page(cpu_buffer, &cpu_buffer->commit_page);
1031 cpu_buffer->write_stamp =
1032 cpu_buffer->commit_page->page->time_stamp;
1035 /* Now set the commit to the event's index */
1036 local_set(&cpu_buffer->commit_page->page->commit, index);
1040 rb_set_commit_to_write(struct ring_buffer_per_cpu *cpu_buffer)
1043 * We only race with interrupts and NMIs on this CPU.
1044 * If we own the commit event, then we can commit
1045 * all others that interrupted us, since the interruptions
1046 * are in stack format (they finish before they come
1047 * back to us). This allows us to do a simple loop to
1048 * assign the commit to the tail.
1051 while (cpu_buffer->commit_page != cpu_buffer->tail_page) {
1052 cpu_buffer->commit_page->page->commit =
1053 cpu_buffer->commit_page->write;
1054 rb_inc_page(cpu_buffer, &cpu_buffer->commit_page);
1055 cpu_buffer->write_stamp =
1056 cpu_buffer->commit_page->page->time_stamp;
1057 /* add barrier to keep gcc from optimizing too much */
1060 while (rb_commit_index(cpu_buffer) !=
1061 rb_page_write(cpu_buffer->commit_page)) {
1062 cpu_buffer->commit_page->page->commit =
1063 cpu_buffer->commit_page->write;
1067 /* again, keep gcc from optimizing */
1071 * If an interrupt came in just after the first while loop
1072 * and pushed the tail page forward, we will be left with
1073 * a dangling commit that will never go forward.
1075 if (unlikely(cpu_buffer->commit_page != cpu_buffer->tail_page))
1079 static void rb_reset_reader_page(struct ring_buffer_per_cpu *cpu_buffer)
1081 cpu_buffer->read_stamp = cpu_buffer->reader_page->page->time_stamp;
1082 cpu_buffer->reader_page->read = 0;
1085 static void rb_inc_iter(struct ring_buffer_iter *iter)
1087 struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
1090 * The iterator could be on the reader page (it starts there).
1091 * But the head could have moved, since the reader was
1092 * found. Check for this case and assign the iterator
1093 * to the head page instead of next.
1095 if (iter->head_page == cpu_buffer->reader_page)
1096 iter->head_page = cpu_buffer->head_page;
1098 rb_inc_page(cpu_buffer, &iter->head_page);
1100 iter->read_stamp = iter->head_page->page->time_stamp;
1105 * ring_buffer_update_event - update event type and data
1106 * @event: the even to update
1107 * @type: the type of event
1108 * @length: the size of the event field in the ring buffer
1110 * Update the type and data fields of the event. The length
1111 * is the actual size that is written to the ring buffer,
1112 * and with this, we can determine what to place into the
1116 rb_update_event(struct ring_buffer_event *event,
1117 unsigned type, unsigned length)
1119 event->type_len = type;
1123 case RINGBUF_TYPE_PADDING:
1124 case RINGBUF_TYPE_TIME_EXTEND:
1125 case RINGBUF_TYPE_TIME_STAMP:
1129 length -= RB_EVNT_HDR_SIZE;
1130 if (length > RB_MAX_SMALL_DATA)
1131 event->array[0] = length;
1133 event->type_len = DIV_ROUND_UP(length, RB_ALIGNMENT);
1140 static unsigned rb_calculate_event_length(unsigned length)
1142 struct ring_buffer_event event; /* Used only for sizeof array */
1144 /* zero length can cause confusions */
1148 if (length > RB_MAX_SMALL_DATA)
1149 length += sizeof(event.array[0]);
1151 length += RB_EVNT_HDR_SIZE;
1152 length = ALIGN(length, RB_ALIGNMENT);
1157 static struct ring_buffer_event *
1158 __rb_reserve_next(struct ring_buffer_per_cpu *cpu_buffer,
1159 unsigned type, unsigned long length, u64 *ts)
1161 struct buffer_page *tail_page, *head_page, *reader_page, *commit_page;
1162 struct buffer_page *next_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)
1180 /* We reserved something on the buffer */
1182 if (RB_WARN_ON(cpu_buffer, write > BUF_PAGE_SIZE))
1185 event = __rb_page_index(tail_page, tail);
1186 rb_update_event(event, type, length);
1188 /* The passed in type is zero for DATA */
1190 local_inc(&tail_page->entries);
1193 * If this is a commit and the tail is zero, then update
1194 * this page's time stamp.
1196 if (!tail && rb_is_commit(cpu_buffer, event))
1197 cpu_buffer->commit_page->page->time_stamp = *ts;
1203 next_page = tail_page;
1205 local_irq_save(flags);
1207 * Since the write to the buffer is still not
1208 * fully lockless, we must be careful with NMIs.
1209 * The locks in the writers are taken when a write
1210 * crosses to a new page. The locks protect against
1211 * races with the readers (this will soon be fixed
1212 * with a lockless solution).
1214 * Because we can not protect against NMIs, and we
1215 * want to keep traces reentrant, we need to manage
1216 * what happens when we are in an NMI.
1218 * NMIs can happen after we take the lock.
1219 * If we are in an NMI, only take the lock
1220 * if it is not already taken. Otherwise
1223 if (unlikely(in_nmi())) {
1224 if (!__raw_spin_trylock(&cpu_buffer->lock)) {
1225 cpu_buffer->nmi_dropped++;
1229 __raw_spin_lock(&cpu_buffer->lock);
1233 rb_inc_page(cpu_buffer, &next_page);
1235 head_page = cpu_buffer->head_page;
1236 reader_page = cpu_buffer->reader_page;
1238 /* we grabbed the lock before incrementing */
1239 if (RB_WARN_ON(cpu_buffer, next_page == reader_page))
1243 * If for some reason, we had an interrupt storm that made
1244 * it all the way around the buffer, bail, and warn
1247 if (unlikely(next_page == commit_page)) {
1248 cpu_buffer->commit_overrun++;
1252 if (next_page == head_page) {
1253 if (!(buffer->flags & RB_FL_OVERWRITE))
1256 /* tail_page has not moved yet? */
1257 if (tail_page == cpu_buffer->tail_page) {
1258 /* count overflows */
1259 cpu_buffer->overrun +=
1260 local_read(&head_page->entries);
1262 rb_inc_page(cpu_buffer, &head_page);
1263 cpu_buffer->head_page = head_page;
1264 cpu_buffer->head_page->read = 0;
1269 * If the tail page is still the same as what we think
1270 * it is, then it is up to us to update the tail
1273 if (tail_page == cpu_buffer->tail_page) {
1274 local_set(&next_page->write, 0);
1275 local_set(&next_page->entries, 0);
1276 local_set(&next_page->page->commit, 0);
1277 cpu_buffer->tail_page = next_page;
1279 /* reread the time stamp */
1280 *ts = ring_buffer_time_stamp(buffer, cpu_buffer->cpu);
1281 cpu_buffer->tail_page->page->time_stamp = *ts;
1285 * The actual tail page has moved forward.
1287 if (tail < BUF_PAGE_SIZE) {
1288 /* Mark the rest of the page with padding */
1289 event = __rb_page_index(tail_page, tail);
1290 rb_event_set_padding(event);
1293 if (tail <= BUF_PAGE_SIZE)
1294 /* Set the write back to the previous setting */
1295 local_set(&tail_page->write, tail);
1298 * If this was a commit entry that failed,
1299 * increment that too
1301 if (tail_page == cpu_buffer->commit_page &&
1302 tail == rb_commit_index(cpu_buffer)) {
1303 rb_set_commit_to_write(cpu_buffer);
1306 __raw_spin_unlock(&cpu_buffer->lock);
1307 local_irq_restore(flags);
1309 /* fail and let the caller try again */
1310 return ERR_PTR(-EAGAIN);
1314 if (tail <= BUF_PAGE_SIZE)
1315 local_set(&tail_page->write, tail);
1317 if (likely(lock_taken))
1318 __raw_spin_unlock(&cpu_buffer->lock);
1319 local_irq_restore(flags);
1324 rb_add_time_stamp(struct ring_buffer_per_cpu *cpu_buffer,
1325 u64 *ts, u64 *delta)
1327 struct ring_buffer_event *event;
1331 if (unlikely(*delta > (1ULL << 59) && !once++)) {
1332 printk(KERN_WARNING "Delta way too big! %llu"
1333 " ts=%llu write stamp = %llu\n",
1334 (unsigned long long)*delta,
1335 (unsigned long long)*ts,
1336 (unsigned long long)cpu_buffer->write_stamp);
1341 * The delta is too big, we to add a
1344 event = __rb_reserve_next(cpu_buffer,
1345 RINGBUF_TYPE_TIME_EXTEND,
1351 if (PTR_ERR(event) == -EAGAIN)
1354 /* Only a commited time event can update the write stamp */
1355 if (rb_is_commit(cpu_buffer, event)) {
1357 * If this is the first on the page, then we need to
1358 * update the page itself, and just put in a zero.
1360 if (rb_event_index(event)) {
1361 event->time_delta = *delta & TS_MASK;
1362 event->array[0] = *delta >> TS_SHIFT;
1364 cpu_buffer->commit_page->page->time_stamp = *ts;
1365 event->time_delta = 0;
1366 event->array[0] = 0;
1368 cpu_buffer->write_stamp = *ts;
1369 /* let the caller know this was the commit */
1372 /* Darn, this is just wasted space */
1373 event->time_delta = 0;
1374 event->array[0] = 0;
1383 static struct ring_buffer_event *
1384 rb_reserve_next_event(struct ring_buffer_per_cpu *cpu_buffer,
1385 unsigned type, unsigned long length)
1387 struct ring_buffer_event *event;
1394 * We allow for interrupts to reenter here and do a trace.
1395 * If one does, it will cause this original code to loop
1396 * back here. Even with heavy interrupts happening, this
1397 * should only happen a few times in a row. If this happens
1398 * 1000 times in a row, there must be either an interrupt
1399 * storm or we have something buggy.
1402 if (RB_WARN_ON(cpu_buffer, ++nr_loops > 1000))
1405 ts = ring_buffer_time_stamp(cpu_buffer->buffer, cpu_buffer->cpu);
1408 * Only the first commit can update the timestamp.
1409 * Yes there is a race here. If an interrupt comes in
1410 * just after the conditional and it traces too, then it
1411 * will also check the deltas. More than one timestamp may
1412 * also be made. But only the entry that did the actual
1413 * commit will be something other than zero.
1415 if (cpu_buffer->tail_page == cpu_buffer->commit_page &&
1416 rb_page_write(cpu_buffer->tail_page) ==
1417 rb_commit_index(cpu_buffer)) {
1419 delta = ts - cpu_buffer->write_stamp;
1421 /* make sure this delta is calculated here */
1424 /* Did the write stamp get updated already? */
1425 if (unlikely(ts < cpu_buffer->write_stamp))
1428 if (test_time_stamp(delta)) {
1430 commit = rb_add_time_stamp(cpu_buffer, &ts, &delta);
1432 if (commit == -EBUSY)
1435 if (commit == -EAGAIN)
1438 RB_WARN_ON(cpu_buffer, commit < 0);
1441 /* Non commits have zero deltas */
1444 event = __rb_reserve_next(cpu_buffer, type, length, &ts);
1445 if (PTR_ERR(event) == -EAGAIN)
1449 if (unlikely(commit))
1451 * Ouch! We needed a timestamp and it was commited. But
1452 * we didn't get our event reserved.
1454 rb_set_commit_to_write(cpu_buffer);
1459 * If the timestamp was commited, make the commit our entry
1460 * now so that we will update it when needed.
1463 rb_set_commit_event(cpu_buffer, event);
1464 else if (!rb_is_commit(cpu_buffer, event))
1467 event->time_delta = delta;
1472 #define TRACE_RECURSIVE_DEPTH 16
1474 static int trace_recursive_lock(void)
1476 current->trace_recursion++;
1478 if (likely(current->trace_recursion < TRACE_RECURSIVE_DEPTH))
1481 /* Disable all tracing before we do anything else */
1482 tracing_off_permanent();
1484 printk_once(KERN_WARNING "Tracing recursion: depth[%ld]:"
1485 "HC[%lu]:SC[%lu]:NMI[%lu]\n",
1486 current->trace_recursion,
1487 hardirq_count() >> HARDIRQ_SHIFT,
1488 softirq_count() >> SOFTIRQ_SHIFT,
1495 static void trace_recursive_unlock(void)
1497 WARN_ON_ONCE(!current->trace_recursion);
1499 current->trace_recursion--;
1502 static DEFINE_PER_CPU(int, rb_need_resched);
1505 * ring_buffer_lock_reserve - reserve a part of the buffer
1506 * @buffer: the ring buffer to reserve from
1507 * @length: the length of the data to reserve (excluding event header)
1509 * Returns a reseverd event on the ring buffer to copy directly to.
1510 * The user of this interface will need to get the body to write into
1511 * and can use the ring_buffer_event_data() interface.
1513 * The length is the length of the data needed, not the event length
1514 * which also includes the event header.
1516 * Must be paired with ring_buffer_unlock_commit, unless NULL is returned.
1517 * If NULL is returned, then nothing has been allocated or locked.
1519 struct ring_buffer_event *
1520 ring_buffer_lock_reserve(struct ring_buffer *buffer, unsigned long length)
1522 struct ring_buffer_per_cpu *cpu_buffer;
1523 struct ring_buffer_event *event;
1526 if (ring_buffer_flags != RB_BUFFERS_ON)
1529 if (atomic_read(&buffer->record_disabled))
1532 /* If we are tracing schedule, we don't want to recurse */
1533 resched = ftrace_preempt_disable();
1535 if (trace_recursive_lock())
1538 cpu = raw_smp_processor_id();
1540 if (!cpumask_test_cpu(cpu, buffer->cpumask))
1543 cpu_buffer = buffer->buffers[cpu];
1545 if (atomic_read(&cpu_buffer->record_disabled))
1548 length = rb_calculate_event_length(length);
1549 if (length > BUF_PAGE_SIZE)
1552 event = rb_reserve_next_event(cpu_buffer, 0, length);
1557 * Need to store resched state on this cpu.
1558 * Only the first needs to.
1561 if (preempt_count() == 1)
1562 per_cpu(rb_need_resched, cpu) = resched;
1567 trace_recursive_unlock();
1570 ftrace_preempt_enable(resched);
1573 EXPORT_SYMBOL_GPL(ring_buffer_lock_reserve);
1575 static void rb_commit(struct ring_buffer_per_cpu *cpu_buffer,
1576 struct ring_buffer_event *event)
1578 local_inc(&cpu_buffer->entries);
1580 /* Only process further if we own the commit */
1581 if (!rb_is_commit(cpu_buffer, event))
1584 cpu_buffer->write_stamp += event->time_delta;
1586 rb_set_commit_to_write(cpu_buffer);
1590 * ring_buffer_unlock_commit - commit a reserved
1591 * @buffer: The buffer to commit to
1592 * @event: The event pointer to commit.
1594 * This commits the data to the ring buffer, and releases any locks held.
1596 * Must be paired with ring_buffer_lock_reserve.
1598 int ring_buffer_unlock_commit(struct ring_buffer *buffer,
1599 struct ring_buffer_event *event)
1601 struct ring_buffer_per_cpu *cpu_buffer;
1602 int cpu = raw_smp_processor_id();
1604 cpu_buffer = buffer->buffers[cpu];
1606 rb_commit(cpu_buffer, event);
1608 trace_recursive_unlock();
1611 * Only the last preempt count needs to restore preemption.
1613 if (preempt_count() == 1)
1614 ftrace_preempt_enable(per_cpu(rb_need_resched, cpu));
1616 preempt_enable_no_resched_notrace();
1620 EXPORT_SYMBOL_GPL(ring_buffer_unlock_commit);
1622 static inline void rb_event_discard(struct ring_buffer_event *event)
1624 /* array[0] holds the actual length for the discarded event */
1625 event->array[0] = rb_event_data_length(event) - RB_EVNT_HDR_SIZE;
1626 event->type_len = RINGBUF_TYPE_PADDING;
1627 /* time delta must be non zero */
1628 if (!event->time_delta)
1629 event->time_delta = 1;
1633 * ring_buffer_event_discard - discard any event in the ring buffer
1634 * @event: the event to discard
1636 * Sometimes a event that is in the ring buffer needs to be ignored.
1637 * This function lets the user discard an event in the ring buffer
1638 * and then that event will not be read later.
1640 * Note, it is up to the user to be careful with this, and protect
1641 * against races. If the user discards an event that has been consumed
1642 * it is possible that it could corrupt the ring buffer.
1644 void ring_buffer_event_discard(struct ring_buffer_event *event)
1646 rb_event_discard(event);
1648 EXPORT_SYMBOL_GPL(ring_buffer_event_discard);
1651 * ring_buffer_commit_discard - discard an event that has not been committed
1652 * @buffer: the ring buffer
1653 * @event: non committed event to discard
1655 * This is similar to ring_buffer_event_discard but must only be
1656 * performed on an event that has not been committed yet. The difference
1657 * is that this will also try to free the event from the ring buffer
1658 * if another event has not been added behind it.
1660 * If another event has been added behind it, it will set the event
1661 * up as discarded, and perform the commit.
1663 * If this function is called, do not call ring_buffer_unlock_commit on
1666 void ring_buffer_discard_commit(struct ring_buffer *buffer,
1667 struct ring_buffer_event *event)
1669 struct ring_buffer_per_cpu *cpu_buffer;
1670 unsigned long new_index, old_index;
1671 struct buffer_page *bpage;
1672 unsigned long index;
1676 /* The event is discarded regardless */
1677 rb_event_discard(event);
1680 * This must only be called if the event has not been
1681 * committed yet. Thus we can assume that preemption
1682 * is still disabled.
1684 RB_WARN_ON(buffer, !preempt_count());
1686 cpu = smp_processor_id();
1687 cpu_buffer = buffer->buffers[cpu];
1689 new_index = rb_event_index(event);
1690 old_index = new_index + rb_event_length(event);
1691 addr = (unsigned long)event;
1694 bpage = cpu_buffer->tail_page;
1696 if (bpage == (void *)addr && rb_page_write(bpage) == old_index) {
1698 * This is on the tail page. It is possible that
1699 * a write could come in and move the tail page
1700 * and write to the next page. That is fine
1701 * because we just shorten what is on this page.
1703 index = local_cmpxchg(&bpage->write, old_index, new_index);
1704 if (index == old_index)
1709 * The commit is still visible by the reader, so we
1710 * must increment entries.
1712 local_inc(&cpu_buffer->entries);
1715 * If a write came in and pushed the tail page
1716 * we still need to update the commit pointer
1717 * if we were the commit.
1719 if (rb_is_commit(cpu_buffer, event))
1720 rb_set_commit_to_write(cpu_buffer);
1722 trace_recursive_unlock();
1725 * Only the last preempt count needs to restore preemption.
1727 if (preempt_count() == 1)
1728 ftrace_preempt_enable(per_cpu(rb_need_resched, cpu));
1730 preempt_enable_no_resched_notrace();
1733 EXPORT_SYMBOL_GPL(ring_buffer_discard_commit);
1736 * ring_buffer_write - write data to the buffer without reserving
1737 * @buffer: The ring buffer to write to.
1738 * @length: The length of the data being written (excluding the event header)
1739 * @data: The data to write to the buffer.
1741 * This is like ring_buffer_lock_reserve and ring_buffer_unlock_commit as
1742 * one function. If you already have the data to write to the buffer, it
1743 * may be easier to simply call this function.
1745 * Note, like ring_buffer_lock_reserve, the length is the length of the data
1746 * and not the length of the event which would hold the header.
1748 int ring_buffer_write(struct ring_buffer *buffer,
1749 unsigned long length,
1752 struct ring_buffer_per_cpu *cpu_buffer;
1753 struct ring_buffer_event *event;
1754 unsigned long event_length;
1759 if (ring_buffer_flags != RB_BUFFERS_ON)
1762 if (atomic_read(&buffer->record_disabled))
1765 resched = ftrace_preempt_disable();
1767 cpu = raw_smp_processor_id();
1769 if (!cpumask_test_cpu(cpu, buffer->cpumask))
1772 cpu_buffer = buffer->buffers[cpu];
1774 if (atomic_read(&cpu_buffer->record_disabled))
1777 event_length = rb_calculate_event_length(length);
1778 event = rb_reserve_next_event(cpu_buffer, 0, event_length);
1782 body = rb_event_data(event);
1784 memcpy(body, data, length);
1786 rb_commit(cpu_buffer, event);
1790 ftrace_preempt_enable(resched);
1794 EXPORT_SYMBOL_GPL(ring_buffer_write);
1796 static int rb_per_cpu_empty(struct ring_buffer_per_cpu *cpu_buffer)
1798 struct buffer_page *reader = cpu_buffer->reader_page;
1799 struct buffer_page *head = cpu_buffer->head_page;
1800 struct buffer_page *commit = cpu_buffer->commit_page;
1802 return reader->read == rb_page_commit(reader) &&
1803 (commit == reader ||
1805 head->read == rb_page_commit(commit)));
1809 * ring_buffer_record_disable - stop all writes into the buffer
1810 * @buffer: The ring buffer to stop writes to.
1812 * This prevents all writes to the buffer. Any attempt to write
1813 * to the buffer after this will fail and return NULL.
1815 * The caller should call synchronize_sched() after this.
1817 void ring_buffer_record_disable(struct ring_buffer *buffer)
1819 atomic_inc(&buffer->record_disabled);
1821 EXPORT_SYMBOL_GPL(ring_buffer_record_disable);
1824 * ring_buffer_record_enable - enable writes to the buffer
1825 * @buffer: The ring buffer to enable writes
1827 * Note, multiple disables will need the same number of enables
1828 * to truely enable the writing (much like preempt_disable).
1830 void ring_buffer_record_enable(struct ring_buffer *buffer)
1832 atomic_dec(&buffer->record_disabled);
1834 EXPORT_SYMBOL_GPL(ring_buffer_record_enable);
1837 * ring_buffer_record_disable_cpu - stop all writes into the cpu_buffer
1838 * @buffer: The ring buffer to stop writes to.
1839 * @cpu: The CPU buffer to stop
1841 * This prevents all writes to the buffer. Any attempt to write
1842 * to the buffer after this will fail and return NULL.
1844 * The caller should call synchronize_sched() after this.
1846 void ring_buffer_record_disable_cpu(struct ring_buffer *buffer, int cpu)
1848 struct ring_buffer_per_cpu *cpu_buffer;
1850 if (!cpumask_test_cpu(cpu, buffer->cpumask))
1853 cpu_buffer = buffer->buffers[cpu];
1854 atomic_inc(&cpu_buffer->record_disabled);
1856 EXPORT_SYMBOL_GPL(ring_buffer_record_disable_cpu);
1859 * ring_buffer_record_enable_cpu - enable writes to the buffer
1860 * @buffer: The ring buffer to enable writes
1861 * @cpu: The CPU to enable.
1863 * Note, multiple disables will need the same number of enables
1864 * to truely enable the writing (much like preempt_disable).
1866 void ring_buffer_record_enable_cpu(struct ring_buffer *buffer, int cpu)
1868 struct ring_buffer_per_cpu *cpu_buffer;
1870 if (!cpumask_test_cpu(cpu, buffer->cpumask))
1873 cpu_buffer = buffer->buffers[cpu];
1874 atomic_dec(&cpu_buffer->record_disabled);
1876 EXPORT_SYMBOL_GPL(ring_buffer_record_enable_cpu);
1879 * ring_buffer_entries_cpu - get the number of entries in a cpu buffer
1880 * @buffer: The ring buffer
1881 * @cpu: The per CPU buffer to get the entries from.
1883 unsigned long ring_buffer_entries_cpu(struct ring_buffer *buffer, int cpu)
1885 struct ring_buffer_per_cpu *cpu_buffer;
1888 if (!cpumask_test_cpu(cpu, buffer->cpumask))
1891 cpu_buffer = buffer->buffers[cpu];
1892 ret = (local_read(&cpu_buffer->entries) - cpu_buffer->overrun)
1897 EXPORT_SYMBOL_GPL(ring_buffer_entries_cpu);
1900 * ring_buffer_overrun_cpu - get the number of overruns in a cpu_buffer
1901 * @buffer: The ring buffer
1902 * @cpu: The per CPU buffer to get the number of overruns from
1904 unsigned long ring_buffer_overrun_cpu(struct ring_buffer *buffer, int cpu)
1906 struct ring_buffer_per_cpu *cpu_buffer;
1909 if (!cpumask_test_cpu(cpu, buffer->cpumask))
1912 cpu_buffer = buffer->buffers[cpu];
1913 ret = cpu_buffer->overrun;
1917 EXPORT_SYMBOL_GPL(ring_buffer_overrun_cpu);
1920 * ring_buffer_nmi_dropped_cpu - get the number of nmis that were dropped
1921 * @buffer: The ring buffer
1922 * @cpu: The per CPU buffer to get the number of overruns from
1924 unsigned long ring_buffer_nmi_dropped_cpu(struct ring_buffer *buffer, int cpu)
1926 struct ring_buffer_per_cpu *cpu_buffer;
1929 if (!cpumask_test_cpu(cpu, buffer->cpumask))
1932 cpu_buffer = buffer->buffers[cpu];
1933 ret = cpu_buffer->nmi_dropped;
1937 EXPORT_SYMBOL_GPL(ring_buffer_nmi_dropped_cpu);
1940 * ring_buffer_commit_overrun_cpu - get the number of overruns caused by commits
1941 * @buffer: The ring buffer
1942 * @cpu: The per CPU buffer to get the number of overruns from
1945 ring_buffer_commit_overrun_cpu(struct ring_buffer *buffer, int cpu)
1947 struct ring_buffer_per_cpu *cpu_buffer;
1950 if (!cpumask_test_cpu(cpu, buffer->cpumask))
1953 cpu_buffer = buffer->buffers[cpu];
1954 ret = cpu_buffer->commit_overrun;
1958 EXPORT_SYMBOL_GPL(ring_buffer_commit_overrun_cpu);
1961 * ring_buffer_entries - get the number of entries in a buffer
1962 * @buffer: The ring buffer
1964 * Returns the total number of entries in the ring buffer
1967 unsigned long ring_buffer_entries(struct ring_buffer *buffer)
1969 struct ring_buffer_per_cpu *cpu_buffer;
1970 unsigned long entries = 0;
1973 /* if you care about this being correct, lock the buffer */
1974 for_each_buffer_cpu(buffer, cpu) {
1975 cpu_buffer = buffer->buffers[cpu];
1976 entries += (local_read(&cpu_buffer->entries) -
1977 cpu_buffer->overrun) - cpu_buffer->read;
1982 EXPORT_SYMBOL_GPL(ring_buffer_entries);
1985 * ring_buffer_overrun_cpu - get the number of overruns in buffer
1986 * @buffer: The ring buffer
1988 * Returns the total number of overruns in the ring buffer
1991 unsigned long ring_buffer_overruns(struct ring_buffer *buffer)
1993 struct ring_buffer_per_cpu *cpu_buffer;
1994 unsigned long overruns = 0;
1997 /* if you care about this being correct, lock the buffer */
1998 for_each_buffer_cpu(buffer, cpu) {
1999 cpu_buffer = buffer->buffers[cpu];
2000 overruns += cpu_buffer->overrun;
2005 EXPORT_SYMBOL_GPL(ring_buffer_overruns);
2007 static void rb_iter_reset(struct ring_buffer_iter *iter)
2009 struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
2011 /* Iterator usage is expected to have record disabled */
2012 if (list_empty(&cpu_buffer->reader_page->list)) {
2013 iter->head_page = cpu_buffer->head_page;
2014 iter->head = cpu_buffer->head_page->read;
2016 iter->head_page = cpu_buffer->reader_page;
2017 iter->head = cpu_buffer->reader_page->read;
2020 iter->read_stamp = cpu_buffer->read_stamp;
2022 iter->read_stamp = iter->head_page->page->time_stamp;
2026 * ring_buffer_iter_reset - reset an iterator
2027 * @iter: The iterator to reset
2029 * Resets the iterator, so that it will start from the beginning
2032 void ring_buffer_iter_reset(struct ring_buffer_iter *iter)
2034 struct ring_buffer_per_cpu *cpu_buffer;
2035 unsigned long flags;
2040 cpu_buffer = iter->cpu_buffer;
2042 spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
2043 rb_iter_reset(iter);
2044 spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
2046 EXPORT_SYMBOL_GPL(ring_buffer_iter_reset);
2049 * ring_buffer_iter_empty - check if an iterator has no more to read
2050 * @iter: The iterator to check
2052 int ring_buffer_iter_empty(struct ring_buffer_iter *iter)
2054 struct ring_buffer_per_cpu *cpu_buffer;
2056 cpu_buffer = iter->cpu_buffer;
2058 return iter->head_page == cpu_buffer->commit_page &&
2059 iter->head == rb_commit_index(cpu_buffer);
2061 EXPORT_SYMBOL_GPL(ring_buffer_iter_empty);
2064 rb_update_read_stamp(struct ring_buffer_per_cpu *cpu_buffer,
2065 struct ring_buffer_event *event)
2069 switch (event->type_len) {
2070 case RINGBUF_TYPE_PADDING:
2073 case RINGBUF_TYPE_TIME_EXTEND:
2074 delta = event->array[0];
2076 delta += event->time_delta;
2077 cpu_buffer->read_stamp += delta;
2080 case RINGBUF_TYPE_TIME_STAMP:
2081 /* FIXME: not implemented */
2084 case RINGBUF_TYPE_DATA:
2085 cpu_buffer->read_stamp += event->time_delta;
2095 rb_update_iter_read_stamp(struct ring_buffer_iter *iter,
2096 struct ring_buffer_event *event)
2100 switch (event->type_len) {
2101 case RINGBUF_TYPE_PADDING:
2104 case RINGBUF_TYPE_TIME_EXTEND:
2105 delta = event->array[0];
2107 delta += event->time_delta;
2108 iter->read_stamp += delta;
2111 case RINGBUF_TYPE_TIME_STAMP:
2112 /* FIXME: not implemented */
2115 case RINGBUF_TYPE_DATA:
2116 iter->read_stamp += event->time_delta;
2125 static struct buffer_page *
2126 rb_get_reader_page(struct ring_buffer_per_cpu *cpu_buffer)
2128 struct buffer_page *reader = NULL;
2129 unsigned long flags;
2132 local_irq_save(flags);
2133 __raw_spin_lock(&cpu_buffer->lock);
2137 * This should normally only loop twice. But because the
2138 * start of the reader inserts an empty page, it causes
2139 * a case where we will loop three times. There should be no
2140 * reason to loop four times (that I know of).
2142 if (RB_WARN_ON(cpu_buffer, ++nr_loops > 3)) {
2147 reader = cpu_buffer->reader_page;
2149 /* If there's more to read, return this page */
2150 if (cpu_buffer->reader_page->read < rb_page_size(reader))
2153 /* Never should we have an index greater than the size */
2154 if (RB_WARN_ON(cpu_buffer,
2155 cpu_buffer->reader_page->read > rb_page_size(reader)))
2158 /* check if we caught up to the tail */
2160 if (cpu_buffer->commit_page == cpu_buffer->reader_page)
2164 * Splice the empty reader page into the list around the head.
2165 * Reset the reader page to size zero.
2168 reader = cpu_buffer->head_page;
2169 cpu_buffer->reader_page->list.next = reader->list.next;
2170 cpu_buffer->reader_page->list.prev = reader->list.prev;
2172 local_set(&cpu_buffer->reader_page->write, 0);
2173 local_set(&cpu_buffer->reader_page->entries, 0);
2174 local_set(&cpu_buffer->reader_page->page->commit, 0);
2176 /* Make the reader page now replace the head */
2177 reader->list.prev->next = &cpu_buffer->reader_page->list;
2178 reader->list.next->prev = &cpu_buffer->reader_page->list;
2181 * If the tail is on the reader, then we must set the head
2182 * to the inserted page, otherwise we set it one before.
2184 cpu_buffer->head_page = cpu_buffer->reader_page;
2186 if (cpu_buffer->commit_page != reader)
2187 rb_inc_page(cpu_buffer, &cpu_buffer->head_page);
2189 /* Finally update the reader page to the new head */
2190 cpu_buffer->reader_page = reader;
2191 rb_reset_reader_page(cpu_buffer);
2196 __raw_spin_unlock(&cpu_buffer->lock);
2197 local_irq_restore(flags);
2202 static void rb_advance_reader(struct ring_buffer_per_cpu *cpu_buffer)
2204 struct ring_buffer_event *event;
2205 struct buffer_page *reader;
2208 reader = rb_get_reader_page(cpu_buffer);
2210 /* This function should not be called when buffer is empty */
2211 if (RB_WARN_ON(cpu_buffer, !reader))
2214 event = rb_reader_event(cpu_buffer);
2216 if (event->type_len <= RINGBUF_TYPE_DATA_TYPE_LEN_MAX
2217 || rb_discarded_event(event))
2220 rb_update_read_stamp(cpu_buffer, event);
2222 length = rb_event_length(event);
2223 cpu_buffer->reader_page->read += length;
2226 static void rb_advance_iter(struct ring_buffer_iter *iter)
2228 struct ring_buffer *buffer;
2229 struct ring_buffer_per_cpu *cpu_buffer;
2230 struct ring_buffer_event *event;
2233 cpu_buffer = iter->cpu_buffer;
2234 buffer = cpu_buffer->buffer;
2237 * Check if we are at the end of the buffer.
2239 if (iter->head >= rb_page_size(iter->head_page)) {
2240 if (RB_WARN_ON(buffer,
2241 iter->head_page == cpu_buffer->commit_page))
2247 event = rb_iter_head_event(iter);
2249 length = rb_event_length(event);
2252 * This should not be called to advance the header if we are
2253 * at the tail of the buffer.
2255 if (RB_WARN_ON(cpu_buffer,
2256 (iter->head_page == cpu_buffer->commit_page) &&
2257 (iter->head + length > rb_commit_index(cpu_buffer))))
2260 rb_update_iter_read_stamp(iter, event);
2262 iter->head += length;
2264 /* check for end of page padding */
2265 if ((iter->head >= rb_page_size(iter->head_page)) &&
2266 (iter->head_page != cpu_buffer->commit_page))
2267 rb_advance_iter(iter);
2270 static struct ring_buffer_event *
2271 rb_buffer_peek(struct ring_buffer *buffer, int cpu, u64 *ts)
2273 struct ring_buffer_per_cpu *cpu_buffer;
2274 struct ring_buffer_event *event;
2275 struct buffer_page *reader;
2278 cpu_buffer = buffer->buffers[cpu];
2282 * We repeat when a timestamp is encountered. It is possible
2283 * to get multiple timestamps from an interrupt entering just
2284 * as one timestamp is about to be written. The max times
2285 * that this can happen is the number of nested interrupts we
2286 * can have. Nesting 10 deep of interrupts is clearly
2289 if (RB_WARN_ON(cpu_buffer, ++nr_loops > 10))
2292 reader = rb_get_reader_page(cpu_buffer);
2296 event = rb_reader_event(cpu_buffer);
2298 switch (event->type_len) {
2299 case RINGBUF_TYPE_PADDING:
2300 if (rb_null_event(event))
2301 RB_WARN_ON(cpu_buffer, 1);
2303 * Because the writer could be discarding every
2304 * event it creates (which would probably be bad)
2305 * if we were to go back to "again" then we may never
2306 * catch up, and will trigger the warn on, or lock
2307 * the box. Return the padding, and we will release
2308 * the current locks, and try again.
2310 rb_advance_reader(cpu_buffer);
2313 case RINGBUF_TYPE_TIME_EXTEND:
2314 /* Internal data, OK to advance */
2315 rb_advance_reader(cpu_buffer);
2318 case RINGBUF_TYPE_TIME_STAMP:
2319 /* FIXME: not implemented */
2320 rb_advance_reader(cpu_buffer);
2323 case RINGBUF_TYPE_DATA:
2325 *ts = cpu_buffer->read_stamp + event->time_delta;
2326 ring_buffer_normalize_time_stamp(buffer,
2327 cpu_buffer->cpu, ts);
2337 EXPORT_SYMBOL_GPL(ring_buffer_peek);
2339 static struct ring_buffer_event *
2340 rb_iter_peek(struct ring_buffer_iter *iter, u64 *ts)
2342 struct ring_buffer *buffer;
2343 struct ring_buffer_per_cpu *cpu_buffer;
2344 struct ring_buffer_event *event;
2347 if (ring_buffer_iter_empty(iter))
2350 cpu_buffer = iter->cpu_buffer;
2351 buffer = cpu_buffer->buffer;
2355 * We repeat when a timestamp is encountered. It is possible
2356 * to get multiple timestamps from an interrupt entering just
2357 * as one timestamp is about to be written. The max times
2358 * that this can happen is the number of nested interrupts we
2359 * can have. Nesting 10 deep of interrupts is clearly
2362 if (RB_WARN_ON(cpu_buffer, ++nr_loops > 10))
2365 if (rb_per_cpu_empty(cpu_buffer))
2368 event = rb_iter_head_event(iter);
2370 switch (event->type_len) {
2371 case RINGBUF_TYPE_PADDING:
2372 if (rb_null_event(event)) {
2376 rb_advance_iter(iter);
2379 case RINGBUF_TYPE_TIME_EXTEND:
2380 /* Internal data, OK to advance */
2381 rb_advance_iter(iter);
2384 case RINGBUF_TYPE_TIME_STAMP:
2385 /* FIXME: not implemented */
2386 rb_advance_iter(iter);
2389 case RINGBUF_TYPE_DATA:
2391 *ts = iter->read_stamp + event->time_delta;
2392 ring_buffer_normalize_time_stamp(buffer,
2393 cpu_buffer->cpu, ts);
2403 EXPORT_SYMBOL_GPL(ring_buffer_iter_peek);
2406 * ring_buffer_peek - peek at the next event to be read
2407 * @buffer: The ring buffer to read
2408 * @cpu: The cpu to peak at
2409 * @ts: The timestamp counter of this event.
2411 * This will return the event that will be read next, but does
2412 * not consume the data.
2414 struct ring_buffer_event *
2415 ring_buffer_peek(struct ring_buffer *buffer, int cpu, u64 *ts)
2417 struct ring_buffer_per_cpu *cpu_buffer = buffer->buffers[cpu];
2418 struct ring_buffer_event *event;
2419 unsigned long flags;
2421 if (!cpumask_test_cpu(cpu, buffer->cpumask))
2425 spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
2426 event = rb_buffer_peek(buffer, cpu, ts);
2427 spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
2429 if (event && event->type_len == RINGBUF_TYPE_PADDING) {
2438 * ring_buffer_iter_peek - peek at the next event to be read
2439 * @iter: The ring buffer iterator
2440 * @ts: The timestamp counter of this event.
2442 * This will return the event that will be read next, but does
2443 * not increment the iterator.
2445 struct ring_buffer_event *
2446 ring_buffer_iter_peek(struct ring_buffer_iter *iter, u64 *ts)
2448 struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
2449 struct ring_buffer_event *event;
2450 unsigned long flags;
2453 spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
2454 event = rb_iter_peek(iter, ts);
2455 spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
2457 if (event && event->type_len == RINGBUF_TYPE_PADDING) {
2466 * ring_buffer_consume - return an event and consume it
2467 * @buffer: The ring buffer to get the next event from
2469 * Returns the next event in the ring buffer, and that event is consumed.
2470 * Meaning, that sequential reads will keep returning a different event,
2471 * and eventually empty the ring buffer if the producer is slower.
2473 struct ring_buffer_event *
2474 ring_buffer_consume(struct ring_buffer *buffer, int cpu, u64 *ts)
2476 struct ring_buffer_per_cpu *cpu_buffer;
2477 struct ring_buffer_event *event = NULL;
2478 unsigned long flags;
2481 /* might be called in atomic */
2484 if (!cpumask_test_cpu(cpu, buffer->cpumask))
2487 cpu_buffer = buffer->buffers[cpu];
2488 spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
2490 event = rb_buffer_peek(buffer, cpu, ts);
2494 rb_advance_reader(cpu_buffer);
2497 spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
2502 if (event && event->type_len == RINGBUF_TYPE_PADDING) {
2509 EXPORT_SYMBOL_GPL(ring_buffer_consume);
2512 * ring_buffer_read_start - start a non consuming read of the buffer
2513 * @buffer: The ring buffer to read from
2514 * @cpu: The cpu buffer to iterate over
2516 * This starts up an iteration through the buffer. It also disables
2517 * the recording to the buffer until the reading is finished.
2518 * This prevents the reading from being corrupted. This is not
2519 * a consuming read, so a producer is not expected.
2521 * Must be paired with ring_buffer_finish.
2523 struct ring_buffer_iter *
2524 ring_buffer_read_start(struct ring_buffer *buffer, int cpu)
2526 struct ring_buffer_per_cpu *cpu_buffer;
2527 struct ring_buffer_iter *iter;
2528 unsigned long flags;
2530 if (!cpumask_test_cpu(cpu, buffer->cpumask))
2533 iter = kmalloc(sizeof(*iter), GFP_KERNEL);
2537 cpu_buffer = buffer->buffers[cpu];
2539 iter->cpu_buffer = cpu_buffer;
2541 atomic_inc(&cpu_buffer->record_disabled);
2542 synchronize_sched();
2544 spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
2545 __raw_spin_lock(&cpu_buffer->lock);
2546 rb_iter_reset(iter);
2547 __raw_spin_unlock(&cpu_buffer->lock);
2548 spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
2552 EXPORT_SYMBOL_GPL(ring_buffer_read_start);
2555 * ring_buffer_finish - finish reading the iterator of the buffer
2556 * @iter: The iterator retrieved by ring_buffer_start
2558 * This re-enables the recording to the buffer, and frees the
2562 ring_buffer_read_finish(struct ring_buffer_iter *iter)
2564 struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
2566 atomic_dec(&cpu_buffer->record_disabled);
2569 EXPORT_SYMBOL_GPL(ring_buffer_read_finish);
2572 * ring_buffer_read - read the next item in the ring buffer by the iterator
2573 * @iter: The ring buffer iterator
2574 * @ts: The time stamp of the event read.
2576 * This reads the next event in the ring buffer and increments the iterator.
2578 struct ring_buffer_event *
2579 ring_buffer_read(struct ring_buffer_iter *iter, u64 *ts)
2581 struct ring_buffer_event *event;
2582 struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
2583 unsigned long flags;
2586 spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
2587 event = rb_iter_peek(iter, ts);
2591 rb_advance_iter(iter);
2593 spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
2595 if (event && event->type_len == RINGBUF_TYPE_PADDING) {
2602 EXPORT_SYMBOL_GPL(ring_buffer_read);
2605 * ring_buffer_size - return the size of the ring buffer (in bytes)
2606 * @buffer: The ring buffer.
2608 unsigned long ring_buffer_size(struct ring_buffer *buffer)
2610 return BUF_PAGE_SIZE * buffer->pages;
2612 EXPORT_SYMBOL_GPL(ring_buffer_size);
2615 rb_reset_cpu(struct ring_buffer_per_cpu *cpu_buffer)
2617 cpu_buffer->head_page
2618 = list_entry(cpu_buffer->pages.next, struct buffer_page, list);
2619 local_set(&cpu_buffer->head_page->write, 0);
2620 local_set(&cpu_buffer->head_page->entries, 0);
2621 local_set(&cpu_buffer->head_page->page->commit, 0);
2623 cpu_buffer->head_page->read = 0;
2625 cpu_buffer->tail_page = cpu_buffer->head_page;
2626 cpu_buffer->commit_page = cpu_buffer->head_page;
2628 INIT_LIST_HEAD(&cpu_buffer->reader_page->list);
2629 local_set(&cpu_buffer->reader_page->write, 0);
2630 local_set(&cpu_buffer->reader_page->entries, 0);
2631 local_set(&cpu_buffer->reader_page->page->commit, 0);
2632 cpu_buffer->reader_page->read = 0;
2634 cpu_buffer->nmi_dropped = 0;
2635 cpu_buffer->commit_overrun = 0;
2636 cpu_buffer->overrun = 0;
2637 cpu_buffer->read = 0;
2638 local_set(&cpu_buffer->entries, 0);
2640 cpu_buffer->write_stamp = 0;
2641 cpu_buffer->read_stamp = 0;
2645 * ring_buffer_reset_cpu - reset a ring buffer per CPU buffer
2646 * @buffer: The ring buffer to reset a per cpu buffer of
2647 * @cpu: The CPU buffer to be reset
2649 void ring_buffer_reset_cpu(struct ring_buffer *buffer, int cpu)
2651 struct ring_buffer_per_cpu *cpu_buffer = buffer->buffers[cpu];
2652 unsigned long flags;
2654 if (!cpumask_test_cpu(cpu, buffer->cpumask))
2657 atomic_inc(&cpu_buffer->record_disabled);
2659 spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
2661 __raw_spin_lock(&cpu_buffer->lock);
2663 rb_reset_cpu(cpu_buffer);
2665 __raw_spin_unlock(&cpu_buffer->lock);
2667 spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
2669 atomic_dec(&cpu_buffer->record_disabled);
2671 EXPORT_SYMBOL_GPL(ring_buffer_reset_cpu);
2674 * ring_buffer_reset - reset a ring buffer
2675 * @buffer: The ring buffer to reset all cpu buffers
2677 void ring_buffer_reset(struct ring_buffer *buffer)
2681 for_each_buffer_cpu(buffer, cpu)
2682 ring_buffer_reset_cpu(buffer, cpu);
2684 EXPORT_SYMBOL_GPL(ring_buffer_reset);
2687 * rind_buffer_empty - is the ring buffer empty?
2688 * @buffer: The ring buffer to test
2690 int ring_buffer_empty(struct ring_buffer *buffer)
2692 struct ring_buffer_per_cpu *cpu_buffer;
2695 /* yes this is racy, but if you don't like the race, lock the buffer */
2696 for_each_buffer_cpu(buffer, cpu) {
2697 cpu_buffer = buffer->buffers[cpu];
2698 if (!rb_per_cpu_empty(cpu_buffer))
2704 EXPORT_SYMBOL_GPL(ring_buffer_empty);
2707 * ring_buffer_empty_cpu - is a cpu buffer of a ring buffer empty?
2708 * @buffer: The ring buffer
2709 * @cpu: The CPU buffer to test
2711 int ring_buffer_empty_cpu(struct ring_buffer *buffer, int cpu)
2713 struct ring_buffer_per_cpu *cpu_buffer;
2716 if (!cpumask_test_cpu(cpu, buffer->cpumask))
2719 cpu_buffer = buffer->buffers[cpu];
2720 ret = rb_per_cpu_empty(cpu_buffer);
2725 EXPORT_SYMBOL_GPL(ring_buffer_empty_cpu);
2728 * ring_buffer_swap_cpu - swap a CPU buffer between two ring buffers
2729 * @buffer_a: One buffer to swap with
2730 * @buffer_b: The other buffer to swap with
2732 * This function is useful for tracers that want to take a "snapshot"
2733 * of a CPU buffer and has another back up buffer lying around.
2734 * it is expected that the tracer handles the cpu buffer not being
2735 * used at the moment.
2737 int ring_buffer_swap_cpu(struct ring_buffer *buffer_a,
2738 struct ring_buffer *buffer_b, int cpu)
2740 struct ring_buffer_per_cpu *cpu_buffer_a;
2741 struct ring_buffer_per_cpu *cpu_buffer_b;
2744 if (!cpumask_test_cpu(cpu, buffer_a->cpumask) ||
2745 !cpumask_test_cpu(cpu, buffer_b->cpumask))
2748 /* At least make sure the two buffers are somewhat the same */
2749 if (buffer_a->pages != buffer_b->pages)
2754 if (ring_buffer_flags != RB_BUFFERS_ON)
2757 if (atomic_read(&buffer_a->record_disabled))
2760 if (atomic_read(&buffer_b->record_disabled))
2763 cpu_buffer_a = buffer_a->buffers[cpu];
2764 cpu_buffer_b = buffer_b->buffers[cpu];
2766 if (atomic_read(&cpu_buffer_a->record_disabled))
2769 if (atomic_read(&cpu_buffer_b->record_disabled))
2773 * We can't do a synchronize_sched here because this
2774 * function can be called in atomic context.
2775 * Normally this will be called from the same CPU as cpu.
2776 * If not it's up to the caller to protect this.
2778 atomic_inc(&cpu_buffer_a->record_disabled);
2779 atomic_inc(&cpu_buffer_b->record_disabled);
2781 buffer_a->buffers[cpu] = cpu_buffer_b;
2782 buffer_b->buffers[cpu] = cpu_buffer_a;
2784 cpu_buffer_b->buffer = buffer_a;
2785 cpu_buffer_a->buffer = buffer_b;
2787 atomic_dec(&cpu_buffer_a->record_disabled);
2788 atomic_dec(&cpu_buffer_b->record_disabled);
2794 EXPORT_SYMBOL_GPL(ring_buffer_swap_cpu);
2797 * ring_buffer_alloc_read_page - allocate a page to read from buffer
2798 * @buffer: the buffer to allocate for.
2800 * This function is used in conjunction with ring_buffer_read_page.
2801 * When reading a full page from the ring buffer, these functions
2802 * can be used to speed up the process. The calling function should
2803 * allocate a few pages first with this function. Then when it
2804 * needs to get pages from the ring buffer, it passes the result
2805 * of this function into ring_buffer_read_page, which will swap
2806 * the page that was allocated, with the read page of the buffer.
2809 * The page allocated, or NULL on error.
2811 void *ring_buffer_alloc_read_page(struct ring_buffer *buffer)
2813 struct buffer_data_page *bpage;
2816 addr = __get_free_page(GFP_KERNEL);
2820 bpage = (void *)addr;
2822 rb_init_page(bpage);
2826 EXPORT_SYMBOL_GPL(ring_buffer_alloc_read_page);
2829 * ring_buffer_free_read_page - free an allocated read page
2830 * @buffer: the buffer the page was allocate for
2831 * @data: the page to free
2833 * Free a page allocated from ring_buffer_alloc_read_page.
2835 void ring_buffer_free_read_page(struct ring_buffer *buffer, void *data)
2837 free_page((unsigned long)data);
2839 EXPORT_SYMBOL_GPL(ring_buffer_free_read_page);
2842 * ring_buffer_read_page - extract a page from the ring buffer
2843 * @buffer: buffer to extract from
2844 * @data_page: the page to use allocated from ring_buffer_alloc_read_page
2845 * @len: amount to extract
2846 * @cpu: the cpu of the buffer to extract
2847 * @full: should the extraction only happen when the page is full.
2849 * This function will pull out a page from the ring buffer and consume it.
2850 * @data_page must be the address of the variable that was returned
2851 * from ring_buffer_alloc_read_page. This is because the page might be used
2852 * to swap with a page in the ring buffer.
2855 * rpage = ring_buffer_alloc_read_page(buffer);
2858 * ret = ring_buffer_read_page(buffer, &rpage, len, cpu, 0);
2860 * process_page(rpage, ret);
2862 * When @full is set, the function will not return true unless
2863 * the writer is off the reader page.
2865 * Note: it is up to the calling functions to handle sleeps and wakeups.
2866 * The ring buffer can be used anywhere in the kernel and can not
2867 * blindly call wake_up. The layer that uses the ring buffer must be
2868 * responsible for that.
2871 * >=0 if data has been transferred, returns the offset of consumed data.
2872 * <0 if no data has been transferred.
2874 int ring_buffer_read_page(struct ring_buffer *buffer,
2875 void **data_page, size_t len, int cpu, int full)
2877 struct ring_buffer_per_cpu *cpu_buffer = buffer->buffers[cpu];
2878 struct ring_buffer_event *event;
2879 struct buffer_data_page *bpage;
2880 struct buffer_page *reader;
2881 unsigned long flags;
2882 unsigned int commit;
2887 if (!cpumask_test_cpu(cpu, buffer->cpumask))
2891 * If len is not big enough to hold the page header, then
2892 * we can not copy anything.
2894 if (len <= BUF_PAGE_HDR_SIZE)
2897 len -= BUF_PAGE_HDR_SIZE;
2906 spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
2908 reader = rb_get_reader_page(cpu_buffer);
2912 event = rb_reader_event(cpu_buffer);
2914 read = reader->read;
2915 commit = rb_page_commit(reader);
2918 * If this page has been partially read or
2919 * if len is not big enough to read the rest of the page or
2920 * a writer is still on the page, then
2921 * we must copy the data from the page to the buffer.
2922 * Otherwise, we can simply swap the page with the one passed in.
2924 if (read || (len < (commit - read)) ||
2925 cpu_buffer->reader_page == cpu_buffer->commit_page) {
2926 struct buffer_data_page *rpage = cpu_buffer->reader_page->page;
2927 unsigned int rpos = read;
2928 unsigned int pos = 0;
2934 if (len > (commit - read))
2935 len = (commit - read);
2937 size = rb_event_length(event);
2942 /* save the current timestamp, since the user will need it */
2943 save_timestamp = cpu_buffer->read_stamp;
2945 /* Need to copy one event at a time */
2947 memcpy(bpage->data + pos, rpage->data + rpos, size);
2951 rb_advance_reader(cpu_buffer);
2952 rpos = reader->read;
2955 event = rb_reader_event(cpu_buffer);
2956 size = rb_event_length(event);
2957 } while (len > size);
2960 local_set(&bpage->commit, pos);
2961 bpage->time_stamp = save_timestamp;
2963 /* we copied everything to the beginning */
2966 /* update the entry counter */
2967 cpu_buffer->read += local_read(&reader->entries);
2969 /* swap the pages */
2970 rb_init_page(bpage);
2971 bpage = reader->page;
2972 reader->page = *data_page;
2973 local_set(&reader->write, 0);
2974 local_set(&reader->entries, 0);
2981 spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
2986 EXPORT_SYMBOL_GPL(ring_buffer_read_page);
2989 rb_simple_read(struct file *filp, char __user *ubuf,
2990 size_t cnt, loff_t *ppos)
2992 unsigned long *p = filp->private_data;
2996 if (test_bit(RB_BUFFERS_DISABLED_BIT, p))
2997 r = sprintf(buf, "permanently disabled\n");
2999 r = sprintf(buf, "%d\n", test_bit(RB_BUFFERS_ON_BIT, p));
3001 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
3005 rb_simple_write(struct file *filp, const char __user *ubuf,
3006 size_t cnt, loff_t *ppos)
3008 unsigned long *p = filp->private_data;
3013 if (cnt >= sizeof(buf))
3016 if (copy_from_user(&buf, ubuf, cnt))
3021 ret = strict_strtoul(buf, 10, &val);
3026 set_bit(RB_BUFFERS_ON_BIT, p);
3028 clear_bit(RB_BUFFERS_ON_BIT, p);
3035 static const struct file_operations rb_simple_fops = {
3036 .open = tracing_open_generic,
3037 .read = rb_simple_read,
3038 .write = rb_simple_write,
3042 static __init int rb_init_debugfs(void)
3044 struct dentry *d_tracer;
3046 d_tracer = tracing_init_dentry();
3048 trace_create_file("tracing_on", 0644, d_tracer,
3049 &ring_buffer_flags, &rb_simple_fops);
3054 fs_initcall(rb_init_debugfs);
3056 #ifdef CONFIG_HOTPLUG_CPU
3057 static int rb_cpu_notify(struct notifier_block *self,
3058 unsigned long action, void *hcpu)
3060 struct ring_buffer *buffer =
3061 container_of(self, struct ring_buffer, cpu_notify);
3062 long cpu = (long)hcpu;
3065 case CPU_UP_PREPARE:
3066 case CPU_UP_PREPARE_FROZEN:
3067 if (cpu_isset(cpu, *buffer->cpumask))
3070 buffer->buffers[cpu] =
3071 rb_allocate_cpu_buffer(buffer, cpu);
3072 if (!buffer->buffers[cpu]) {
3073 WARN(1, "failed to allocate ring buffer on CPU %ld\n",
3078 cpu_set(cpu, *buffer->cpumask);
3080 case CPU_DOWN_PREPARE:
3081 case CPU_DOWN_PREPARE_FROZEN:
3084 * If we were to free the buffer, then the user would
3085 * lose any trace that was in the buffer.