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 /* Set the write back to the previous setting */
1294 local_sub(length, &tail_page->write);
1297 * If this was a commit entry that failed,
1298 * increment that too
1300 if (tail_page == cpu_buffer->commit_page &&
1301 tail == rb_commit_index(cpu_buffer)) {
1302 rb_set_commit_to_write(cpu_buffer);
1305 __raw_spin_unlock(&cpu_buffer->lock);
1306 local_irq_restore(flags);
1308 /* fail and let the caller try again */
1309 return ERR_PTR(-EAGAIN);
1313 local_sub(length, &tail_page->write);
1315 if (likely(lock_taken))
1316 __raw_spin_unlock(&cpu_buffer->lock);
1317 local_irq_restore(flags);
1322 rb_add_time_stamp(struct ring_buffer_per_cpu *cpu_buffer,
1323 u64 *ts, u64 *delta)
1325 struct ring_buffer_event *event;
1329 if (unlikely(*delta > (1ULL << 59) && !once++)) {
1330 printk(KERN_WARNING "Delta way too big! %llu"
1331 " ts=%llu write stamp = %llu\n",
1332 (unsigned long long)*delta,
1333 (unsigned long long)*ts,
1334 (unsigned long long)cpu_buffer->write_stamp);
1339 * The delta is too big, we to add a
1342 event = __rb_reserve_next(cpu_buffer,
1343 RINGBUF_TYPE_TIME_EXTEND,
1349 if (PTR_ERR(event) == -EAGAIN)
1352 /* Only a commited time event can update the write stamp */
1353 if (rb_is_commit(cpu_buffer, event)) {
1355 * If this is the first on the page, then we need to
1356 * update the page itself, and just put in a zero.
1358 if (rb_event_index(event)) {
1359 event->time_delta = *delta & TS_MASK;
1360 event->array[0] = *delta >> TS_SHIFT;
1362 cpu_buffer->commit_page->page->time_stamp = *ts;
1363 event->time_delta = 0;
1364 event->array[0] = 0;
1366 cpu_buffer->write_stamp = *ts;
1367 /* let the caller know this was the commit */
1370 /* Darn, this is just wasted space */
1371 event->time_delta = 0;
1372 event->array[0] = 0;
1381 static struct ring_buffer_event *
1382 rb_reserve_next_event(struct ring_buffer_per_cpu *cpu_buffer,
1383 unsigned type, unsigned long length)
1385 struct ring_buffer_event *event;
1392 * We allow for interrupts to reenter here and do a trace.
1393 * If one does, it will cause this original code to loop
1394 * back here. Even with heavy interrupts happening, this
1395 * should only happen a few times in a row. If this happens
1396 * 1000 times in a row, there must be either an interrupt
1397 * storm or we have something buggy.
1400 if (RB_WARN_ON(cpu_buffer, ++nr_loops > 1000))
1403 ts = ring_buffer_time_stamp(cpu_buffer->buffer, cpu_buffer->cpu);
1406 * Only the first commit can update the timestamp.
1407 * Yes there is a race here. If an interrupt comes in
1408 * just after the conditional and it traces too, then it
1409 * will also check the deltas. More than one timestamp may
1410 * also be made. But only the entry that did the actual
1411 * commit will be something other than zero.
1413 if (cpu_buffer->tail_page == cpu_buffer->commit_page &&
1414 rb_page_write(cpu_buffer->tail_page) ==
1415 rb_commit_index(cpu_buffer)) {
1417 delta = ts - cpu_buffer->write_stamp;
1419 /* make sure this delta is calculated here */
1422 /* Did the write stamp get updated already? */
1423 if (unlikely(ts < cpu_buffer->write_stamp))
1426 if (test_time_stamp(delta)) {
1428 commit = rb_add_time_stamp(cpu_buffer, &ts, &delta);
1430 if (commit == -EBUSY)
1433 if (commit == -EAGAIN)
1436 RB_WARN_ON(cpu_buffer, commit < 0);
1439 /* Non commits have zero deltas */
1442 event = __rb_reserve_next(cpu_buffer, type, length, &ts);
1443 if (PTR_ERR(event) == -EAGAIN)
1447 if (unlikely(commit))
1449 * Ouch! We needed a timestamp and it was commited. But
1450 * we didn't get our event reserved.
1452 rb_set_commit_to_write(cpu_buffer);
1457 * If the timestamp was commited, make the commit our entry
1458 * now so that we will update it when needed.
1461 rb_set_commit_event(cpu_buffer, event);
1462 else if (!rb_is_commit(cpu_buffer, event))
1465 event->time_delta = delta;
1470 #define TRACE_RECURSIVE_DEPTH 16
1472 static int trace_recursive_lock(void)
1474 current->trace_recursion++;
1476 if (likely(current->trace_recursion < TRACE_RECURSIVE_DEPTH))
1479 /* Disable all tracing before we do anything else */
1480 tracing_off_permanent();
1482 printk_once(KERN_WARNING "Tracing recursion: depth[%ld]:"
1483 "HC[%lu]:SC[%lu]:NMI[%lu]\n",
1484 current->trace_recursion,
1485 hardirq_count() >> HARDIRQ_SHIFT,
1486 softirq_count() >> SOFTIRQ_SHIFT,
1493 static void trace_recursive_unlock(void)
1495 WARN_ON_ONCE(!current->trace_recursion);
1497 current->trace_recursion--;
1500 static DEFINE_PER_CPU(int, rb_need_resched);
1503 * ring_buffer_lock_reserve - reserve a part of the buffer
1504 * @buffer: the ring buffer to reserve from
1505 * @length: the length of the data to reserve (excluding event header)
1507 * Returns a reseverd event on the ring buffer to copy directly to.
1508 * The user of this interface will need to get the body to write into
1509 * and can use the ring_buffer_event_data() interface.
1511 * The length is the length of the data needed, not the event length
1512 * which also includes the event header.
1514 * Must be paired with ring_buffer_unlock_commit, unless NULL is returned.
1515 * If NULL is returned, then nothing has been allocated or locked.
1517 struct ring_buffer_event *
1518 ring_buffer_lock_reserve(struct ring_buffer *buffer, unsigned long length)
1520 struct ring_buffer_per_cpu *cpu_buffer;
1521 struct ring_buffer_event *event;
1524 if (ring_buffer_flags != RB_BUFFERS_ON)
1527 if (atomic_read(&buffer->record_disabled))
1530 /* If we are tracing schedule, we don't want to recurse */
1531 resched = ftrace_preempt_disable();
1533 if (trace_recursive_lock())
1536 cpu = raw_smp_processor_id();
1538 if (!cpumask_test_cpu(cpu, buffer->cpumask))
1541 cpu_buffer = buffer->buffers[cpu];
1543 if (atomic_read(&cpu_buffer->record_disabled))
1546 length = rb_calculate_event_length(length);
1547 if (length > BUF_PAGE_SIZE)
1550 event = rb_reserve_next_event(cpu_buffer, 0, length);
1555 * Need to store resched state on this cpu.
1556 * Only the first needs to.
1559 if (preempt_count() == 1)
1560 per_cpu(rb_need_resched, cpu) = resched;
1565 trace_recursive_unlock();
1568 ftrace_preempt_enable(resched);
1571 EXPORT_SYMBOL_GPL(ring_buffer_lock_reserve);
1573 static void rb_commit(struct ring_buffer_per_cpu *cpu_buffer,
1574 struct ring_buffer_event *event)
1576 local_inc(&cpu_buffer->entries);
1578 /* Only process further if we own the commit */
1579 if (!rb_is_commit(cpu_buffer, event))
1582 cpu_buffer->write_stamp += event->time_delta;
1584 rb_set_commit_to_write(cpu_buffer);
1588 * ring_buffer_unlock_commit - commit a reserved
1589 * @buffer: The buffer to commit to
1590 * @event: The event pointer to commit.
1592 * This commits the data to the ring buffer, and releases any locks held.
1594 * Must be paired with ring_buffer_lock_reserve.
1596 int ring_buffer_unlock_commit(struct ring_buffer *buffer,
1597 struct ring_buffer_event *event)
1599 struct ring_buffer_per_cpu *cpu_buffer;
1600 int cpu = raw_smp_processor_id();
1602 cpu_buffer = buffer->buffers[cpu];
1604 rb_commit(cpu_buffer, event);
1606 trace_recursive_unlock();
1609 * Only the last preempt count needs to restore preemption.
1611 if (preempt_count() == 1)
1612 ftrace_preempt_enable(per_cpu(rb_need_resched, cpu));
1614 preempt_enable_no_resched_notrace();
1618 EXPORT_SYMBOL_GPL(ring_buffer_unlock_commit);
1620 static inline void rb_event_discard(struct ring_buffer_event *event)
1622 /* array[0] holds the actual length for the discarded event */
1623 event->array[0] = rb_event_data_length(event) - RB_EVNT_HDR_SIZE;
1624 event->type_len = RINGBUF_TYPE_PADDING;
1625 /* time delta must be non zero */
1626 if (!event->time_delta)
1627 event->time_delta = 1;
1631 * ring_buffer_event_discard - discard any event in the ring buffer
1632 * @event: the event to discard
1634 * Sometimes a event that is in the ring buffer needs to be ignored.
1635 * This function lets the user discard an event in the ring buffer
1636 * and then that event will not be read later.
1638 * Note, it is up to the user to be careful with this, and protect
1639 * against races. If the user discards an event that has been consumed
1640 * it is possible that it could corrupt the ring buffer.
1642 void ring_buffer_event_discard(struct ring_buffer_event *event)
1644 rb_event_discard(event);
1646 EXPORT_SYMBOL_GPL(ring_buffer_event_discard);
1649 * ring_buffer_commit_discard - discard an event that has not been committed
1650 * @buffer: the ring buffer
1651 * @event: non committed event to discard
1653 * This is similar to ring_buffer_event_discard but must only be
1654 * performed on an event that has not been committed yet. The difference
1655 * is that this will also try to free the event from the ring buffer
1656 * if another event has not been added behind it.
1658 * If another event has been added behind it, it will set the event
1659 * up as discarded, and perform the commit.
1661 * If this function is called, do not call ring_buffer_unlock_commit on
1664 void ring_buffer_discard_commit(struct ring_buffer *buffer,
1665 struct ring_buffer_event *event)
1667 struct ring_buffer_per_cpu *cpu_buffer;
1668 unsigned long new_index, old_index;
1669 struct buffer_page *bpage;
1670 unsigned long index;
1674 /* The event is discarded regardless */
1675 rb_event_discard(event);
1678 * This must only be called if the event has not been
1679 * committed yet. Thus we can assume that preemption
1680 * is still disabled.
1682 RB_WARN_ON(buffer, !preempt_count());
1684 cpu = smp_processor_id();
1685 cpu_buffer = buffer->buffers[cpu];
1687 new_index = rb_event_index(event);
1688 old_index = new_index + rb_event_length(event);
1689 addr = (unsigned long)event;
1692 bpage = cpu_buffer->tail_page;
1694 if (bpage == (void *)addr && rb_page_write(bpage) == old_index) {
1696 * This is on the tail page. It is possible that
1697 * a write could come in and move the tail page
1698 * and write to the next page. That is fine
1699 * because we just shorten what is on this page.
1701 index = local_cmpxchg(&bpage->write, old_index, new_index);
1702 if (index == old_index)
1707 * The commit is still visible by the reader, so we
1708 * must increment entries.
1710 local_inc(&cpu_buffer->entries);
1713 * If a write came in and pushed the tail page
1714 * we still need to update the commit pointer
1715 * if we were the commit.
1717 if (rb_is_commit(cpu_buffer, event))
1718 rb_set_commit_to_write(cpu_buffer);
1720 trace_recursive_unlock();
1723 * Only the last preempt count needs to restore preemption.
1725 if (preempt_count() == 1)
1726 ftrace_preempt_enable(per_cpu(rb_need_resched, cpu));
1728 preempt_enable_no_resched_notrace();
1731 EXPORT_SYMBOL_GPL(ring_buffer_discard_commit);
1734 * ring_buffer_write - write data to the buffer without reserving
1735 * @buffer: The ring buffer to write to.
1736 * @length: The length of the data being written (excluding the event header)
1737 * @data: The data to write to the buffer.
1739 * This is like ring_buffer_lock_reserve and ring_buffer_unlock_commit as
1740 * one function. If you already have the data to write to the buffer, it
1741 * may be easier to simply call this function.
1743 * Note, like ring_buffer_lock_reserve, the length is the length of the data
1744 * and not the length of the event which would hold the header.
1746 int ring_buffer_write(struct ring_buffer *buffer,
1747 unsigned long length,
1750 struct ring_buffer_per_cpu *cpu_buffer;
1751 struct ring_buffer_event *event;
1752 unsigned long event_length;
1757 if (ring_buffer_flags != RB_BUFFERS_ON)
1760 if (atomic_read(&buffer->record_disabled))
1763 resched = ftrace_preempt_disable();
1765 cpu = raw_smp_processor_id();
1767 if (!cpumask_test_cpu(cpu, buffer->cpumask))
1770 cpu_buffer = buffer->buffers[cpu];
1772 if (atomic_read(&cpu_buffer->record_disabled))
1775 event_length = rb_calculate_event_length(length);
1776 event = rb_reserve_next_event(cpu_buffer, 0, event_length);
1780 body = rb_event_data(event);
1782 memcpy(body, data, length);
1784 rb_commit(cpu_buffer, event);
1788 ftrace_preempt_enable(resched);
1792 EXPORT_SYMBOL_GPL(ring_buffer_write);
1794 static int rb_per_cpu_empty(struct ring_buffer_per_cpu *cpu_buffer)
1796 struct buffer_page *reader = cpu_buffer->reader_page;
1797 struct buffer_page *head = cpu_buffer->head_page;
1798 struct buffer_page *commit = cpu_buffer->commit_page;
1800 return reader->read == rb_page_commit(reader) &&
1801 (commit == reader ||
1803 head->read == rb_page_commit(commit)));
1807 * ring_buffer_record_disable - stop all writes into the buffer
1808 * @buffer: The ring buffer to stop writes to.
1810 * This prevents all writes to the buffer. Any attempt to write
1811 * to the buffer after this will fail and return NULL.
1813 * The caller should call synchronize_sched() after this.
1815 void ring_buffer_record_disable(struct ring_buffer *buffer)
1817 atomic_inc(&buffer->record_disabled);
1819 EXPORT_SYMBOL_GPL(ring_buffer_record_disable);
1822 * ring_buffer_record_enable - enable writes to the buffer
1823 * @buffer: The ring buffer to enable writes
1825 * Note, multiple disables will need the same number of enables
1826 * to truely enable the writing (much like preempt_disable).
1828 void ring_buffer_record_enable(struct ring_buffer *buffer)
1830 atomic_dec(&buffer->record_disabled);
1832 EXPORT_SYMBOL_GPL(ring_buffer_record_enable);
1835 * ring_buffer_record_disable_cpu - stop all writes into the cpu_buffer
1836 * @buffer: The ring buffer to stop writes to.
1837 * @cpu: The CPU buffer to stop
1839 * This prevents all writes to the buffer. Any attempt to write
1840 * to the buffer after this will fail and return NULL.
1842 * The caller should call synchronize_sched() after this.
1844 void ring_buffer_record_disable_cpu(struct ring_buffer *buffer, int cpu)
1846 struct ring_buffer_per_cpu *cpu_buffer;
1848 if (!cpumask_test_cpu(cpu, buffer->cpumask))
1851 cpu_buffer = buffer->buffers[cpu];
1852 atomic_inc(&cpu_buffer->record_disabled);
1854 EXPORT_SYMBOL_GPL(ring_buffer_record_disable_cpu);
1857 * ring_buffer_record_enable_cpu - enable writes to the buffer
1858 * @buffer: The ring buffer to enable writes
1859 * @cpu: The CPU to enable.
1861 * Note, multiple disables will need the same number of enables
1862 * to truely enable the writing (much like preempt_disable).
1864 void ring_buffer_record_enable_cpu(struct ring_buffer *buffer, int cpu)
1866 struct ring_buffer_per_cpu *cpu_buffer;
1868 if (!cpumask_test_cpu(cpu, buffer->cpumask))
1871 cpu_buffer = buffer->buffers[cpu];
1872 atomic_dec(&cpu_buffer->record_disabled);
1874 EXPORT_SYMBOL_GPL(ring_buffer_record_enable_cpu);
1877 * ring_buffer_entries_cpu - get the number of entries in a cpu buffer
1878 * @buffer: The ring buffer
1879 * @cpu: The per CPU buffer to get the entries from.
1881 unsigned long ring_buffer_entries_cpu(struct ring_buffer *buffer, int cpu)
1883 struct ring_buffer_per_cpu *cpu_buffer;
1886 if (!cpumask_test_cpu(cpu, buffer->cpumask))
1889 cpu_buffer = buffer->buffers[cpu];
1890 ret = (local_read(&cpu_buffer->entries) - cpu_buffer->overrun)
1895 EXPORT_SYMBOL_GPL(ring_buffer_entries_cpu);
1898 * ring_buffer_overrun_cpu - get the number of overruns in a cpu_buffer
1899 * @buffer: The ring buffer
1900 * @cpu: The per CPU buffer to get the number of overruns from
1902 unsigned long ring_buffer_overrun_cpu(struct ring_buffer *buffer, int cpu)
1904 struct ring_buffer_per_cpu *cpu_buffer;
1907 if (!cpumask_test_cpu(cpu, buffer->cpumask))
1910 cpu_buffer = buffer->buffers[cpu];
1911 ret = cpu_buffer->overrun;
1915 EXPORT_SYMBOL_GPL(ring_buffer_overrun_cpu);
1918 * ring_buffer_nmi_dropped_cpu - get the number of nmis that were dropped
1919 * @buffer: The ring buffer
1920 * @cpu: The per CPU buffer to get the number of overruns from
1922 unsigned long ring_buffer_nmi_dropped_cpu(struct ring_buffer *buffer, int cpu)
1924 struct ring_buffer_per_cpu *cpu_buffer;
1927 if (!cpumask_test_cpu(cpu, buffer->cpumask))
1930 cpu_buffer = buffer->buffers[cpu];
1931 ret = cpu_buffer->nmi_dropped;
1935 EXPORT_SYMBOL_GPL(ring_buffer_nmi_dropped_cpu);
1938 * ring_buffer_commit_overrun_cpu - get the number of overruns caused by commits
1939 * @buffer: The ring buffer
1940 * @cpu: The per CPU buffer to get the number of overruns from
1943 ring_buffer_commit_overrun_cpu(struct ring_buffer *buffer, int cpu)
1945 struct ring_buffer_per_cpu *cpu_buffer;
1948 if (!cpumask_test_cpu(cpu, buffer->cpumask))
1951 cpu_buffer = buffer->buffers[cpu];
1952 ret = cpu_buffer->commit_overrun;
1956 EXPORT_SYMBOL_GPL(ring_buffer_commit_overrun_cpu);
1959 * ring_buffer_entries - get the number of entries in a buffer
1960 * @buffer: The ring buffer
1962 * Returns the total number of entries in the ring buffer
1965 unsigned long ring_buffer_entries(struct ring_buffer *buffer)
1967 struct ring_buffer_per_cpu *cpu_buffer;
1968 unsigned long entries = 0;
1971 /* if you care about this being correct, lock the buffer */
1972 for_each_buffer_cpu(buffer, cpu) {
1973 cpu_buffer = buffer->buffers[cpu];
1974 entries += (local_read(&cpu_buffer->entries) -
1975 cpu_buffer->overrun) - cpu_buffer->read;
1980 EXPORT_SYMBOL_GPL(ring_buffer_entries);
1983 * ring_buffer_overrun_cpu - get the number of overruns in buffer
1984 * @buffer: The ring buffer
1986 * Returns the total number of overruns in the ring buffer
1989 unsigned long ring_buffer_overruns(struct ring_buffer *buffer)
1991 struct ring_buffer_per_cpu *cpu_buffer;
1992 unsigned long overruns = 0;
1995 /* if you care about this being correct, lock the buffer */
1996 for_each_buffer_cpu(buffer, cpu) {
1997 cpu_buffer = buffer->buffers[cpu];
1998 overruns += cpu_buffer->overrun;
2003 EXPORT_SYMBOL_GPL(ring_buffer_overruns);
2005 static void rb_iter_reset(struct ring_buffer_iter *iter)
2007 struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
2009 /* Iterator usage is expected to have record disabled */
2010 if (list_empty(&cpu_buffer->reader_page->list)) {
2011 iter->head_page = cpu_buffer->head_page;
2012 iter->head = cpu_buffer->head_page->read;
2014 iter->head_page = cpu_buffer->reader_page;
2015 iter->head = cpu_buffer->reader_page->read;
2018 iter->read_stamp = cpu_buffer->read_stamp;
2020 iter->read_stamp = iter->head_page->page->time_stamp;
2024 * ring_buffer_iter_reset - reset an iterator
2025 * @iter: The iterator to reset
2027 * Resets the iterator, so that it will start from the beginning
2030 void ring_buffer_iter_reset(struct ring_buffer_iter *iter)
2032 struct ring_buffer_per_cpu *cpu_buffer;
2033 unsigned long flags;
2038 cpu_buffer = iter->cpu_buffer;
2040 spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
2041 rb_iter_reset(iter);
2042 spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
2044 EXPORT_SYMBOL_GPL(ring_buffer_iter_reset);
2047 * ring_buffer_iter_empty - check if an iterator has no more to read
2048 * @iter: The iterator to check
2050 int ring_buffer_iter_empty(struct ring_buffer_iter *iter)
2052 struct ring_buffer_per_cpu *cpu_buffer;
2054 cpu_buffer = iter->cpu_buffer;
2056 return iter->head_page == cpu_buffer->commit_page &&
2057 iter->head == rb_commit_index(cpu_buffer);
2059 EXPORT_SYMBOL_GPL(ring_buffer_iter_empty);
2062 rb_update_read_stamp(struct ring_buffer_per_cpu *cpu_buffer,
2063 struct ring_buffer_event *event)
2067 switch (event->type_len) {
2068 case RINGBUF_TYPE_PADDING:
2071 case RINGBUF_TYPE_TIME_EXTEND:
2072 delta = event->array[0];
2074 delta += event->time_delta;
2075 cpu_buffer->read_stamp += delta;
2078 case RINGBUF_TYPE_TIME_STAMP:
2079 /* FIXME: not implemented */
2082 case RINGBUF_TYPE_DATA:
2083 cpu_buffer->read_stamp += event->time_delta;
2093 rb_update_iter_read_stamp(struct ring_buffer_iter *iter,
2094 struct ring_buffer_event *event)
2098 switch (event->type_len) {
2099 case RINGBUF_TYPE_PADDING:
2102 case RINGBUF_TYPE_TIME_EXTEND:
2103 delta = event->array[0];
2105 delta += event->time_delta;
2106 iter->read_stamp += delta;
2109 case RINGBUF_TYPE_TIME_STAMP:
2110 /* FIXME: not implemented */
2113 case RINGBUF_TYPE_DATA:
2114 iter->read_stamp += event->time_delta;
2123 static struct buffer_page *
2124 rb_get_reader_page(struct ring_buffer_per_cpu *cpu_buffer)
2126 struct buffer_page *reader = NULL;
2127 unsigned long flags;
2130 local_irq_save(flags);
2131 __raw_spin_lock(&cpu_buffer->lock);
2135 * This should normally only loop twice. But because the
2136 * start of the reader inserts an empty page, it causes
2137 * a case where we will loop three times. There should be no
2138 * reason to loop four times (that I know of).
2140 if (RB_WARN_ON(cpu_buffer, ++nr_loops > 3)) {
2145 reader = cpu_buffer->reader_page;
2147 /* If there's more to read, return this page */
2148 if (cpu_buffer->reader_page->read < rb_page_size(reader))
2151 /* Never should we have an index greater than the size */
2152 if (RB_WARN_ON(cpu_buffer,
2153 cpu_buffer->reader_page->read > rb_page_size(reader)))
2156 /* check if we caught up to the tail */
2158 if (cpu_buffer->commit_page == cpu_buffer->reader_page)
2162 * Splice the empty reader page into the list around the head.
2163 * Reset the reader page to size zero.
2166 reader = cpu_buffer->head_page;
2167 cpu_buffer->reader_page->list.next = reader->list.next;
2168 cpu_buffer->reader_page->list.prev = reader->list.prev;
2170 local_set(&cpu_buffer->reader_page->write, 0);
2171 local_set(&cpu_buffer->reader_page->entries, 0);
2172 local_set(&cpu_buffer->reader_page->page->commit, 0);
2174 /* Make the reader page now replace the head */
2175 reader->list.prev->next = &cpu_buffer->reader_page->list;
2176 reader->list.next->prev = &cpu_buffer->reader_page->list;
2179 * If the tail is on the reader, then we must set the head
2180 * to the inserted page, otherwise we set it one before.
2182 cpu_buffer->head_page = cpu_buffer->reader_page;
2184 if (cpu_buffer->commit_page != reader)
2185 rb_inc_page(cpu_buffer, &cpu_buffer->head_page);
2187 /* Finally update the reader page to the new head */
2188 cpu_buffer->reader_page = reader;
2189 rb_reset_reader_page(cpu_buffer);
2194 __raw_spin_unlock(&cpu_buffer->lock);
2195 local_irq_restore(flags);
2200 static void rb_advance_reader(struct ring_buffer_per_cpu *cpu_buffer)
2202 struct ring_buffer_event *event;
2203 struct buffer_page *reader;
2206 reader = rb_get_reader_page(cpu_buffer);
2208 /* This function should not be called when buffer is empty */
2209 if (RB_WARN_ON(cpu_buffer, !reader))
2212 event = rb_reader_event(cpu_buffer);
2214 if (event->type_len <= RINGBUF_TYPE_DATA_TYPE_LEN_MAX
2215 || rb_discarded_event(event))
2218 rb_update_read_stamp(cpu_buffer, event);
2220 length = rb_event_length(event);
2221 cpu_buffer->reader_page->read += length;
2224 static void rb_advance_iter(struct ring_buffer_iter *iter)
2226 struct ring_buffer *buffer;
2227 struct ring_buffer_per_cpu *cpu_buffer;
2228 struct ring_buffer_event *event;
2231 cpu_buffer = iter->cpu_buffer;
2232 buffer = cpu_buffer->buffer;
2235 * Check if we are at the end of the buffer.
2237 if (iter->head >= rb_page_size(iter->head_page)) {
2238 if (RB_WARN_ON(buffer,
2239 iter->head_page == cpu_buffer->commit_page))
2245 event = rb_iter_head_event(iter);
2247 length = rb_event_length(event);
2250 * This should not be called to advance the header if we are
2251 * at the tail of the buffer.
2253 if (RB_WARN_ON(cpu_buffer,
2254 (iter->head_page == cpu_buffer->commit_page) &&
2255 (iter->head + length > rb_commit_index(cpu_buffer))))
2258 rb_update_iter_read_stamp(iter, event);
2260 iter->head += length;
2262 /* check for end of page padding */
2263 if ((iter->head >= rb_page_size(iter->head_page)) &&
2264 (iter->head_page != cpu_buffer->commit_page))
2265 rb_advance_iter(iter);
2268 static struct ring_buffer_event *
2269 rb_buffer_peek(struct ring_buffer *buffer, int cpu, u64 *ts)
2271 struct ring_buffer_per_cpu *cpu_buffer;
2272 struct ring_buffer_event *event;
2273 struct buffer_page *reader;
2276 cpu_buffer = buffer->buffers[cpu];
2280 * We repeat when a timestamp is encountered. It is possible
2281 * to get multiple timestamps from an interrupt entering just
2282 * as one timestamp is about to be written. The max times
2283 * that this can happen is the number of nested interrupts we
2284 * can have. Nesting 10 deep of interrupts is clearly
2287 if (RB_WARN_ON(cpu_buffer, ++nr_loops > 10))
2290 reader = rb_get_reader_page(cpu_buffer);
2294 event = rb_reader_event(cpu_buffer);
2296 switch (event->type_len) {
2297 case RINGBUF_TYPE_PADDING:
2298 if (rb_null_event(event))
2299 RB_WARN_ON(cpu_buffer, 1);
2301 * Because the writer could be discarding every
2302 * event it creates (which would probably be bad)
2303 * if we were to go back to "again" then we may never
2304 * catch up, and will trigger the warn on, or lock
2305 * the box. Return the padding, and we will release
2306 * the current locks, and try again.
2308 rb_advance_reader(cpu_buffer);
2311 case RINGBUF_TYPE_TIME_EXTEND:
2312 /* Internal data, OK to advance */
2313 rb_advance_reader(cpu_buffer);
2316 case RINGBUF_TYPE_TIME_STAMP:
2317 /* FIXME: not implemented */
2318 rb_advance_reader(cpu_buffer);
2321 case RINGBUF_TYPE_DATA:
2323 *ts = cpu_buffer->read_stamp + event->time_delta;
2324 ring_buffer_normalize_time_stamp(buffer,
2325 cpu_buffer->cpu, ts);
2335 EXPORT_SYMBOL_GPL(ring_buffer_peek);
2337 static struct ring_buffer_event *
2338 rb_iter_peek(struct ring_buffer_iter *iter, u64 *ts)
2340 struct ring_buffer *buffer;
2341 struct ring_buffer_per_cpu *cpu_buffer;
2342 struct ring_buffer_event *event;
2345 if (ring_buffer_iter_empty(iter))
2348 cpu_buffer = iter->cpu_buffer;
2349 buffer = cpu_buffer->buffer;
2353 * We repeat when a timestamp is encountered. It is possible
2354 * to get multiple timestamps from an interrupt entering just
2355 * as one timestamp is about to be written. The max times
2356 * that this can happen is the number of nested interrupts we
2357 * can have. Nesting 10 deep of interrupts is clearly
2360 if (RB_WARN_ON(cpu_buffer, ++nr_loops > 10))
2363 if (rb_per_cpu_empty(cpu_buffer))
2366 event = rb_iter_head_event(iter);
2368 switch (event->type_len) {
2369 case RINGBUF_TYPE_PADDING:
2370 if (rb_null_event(event)) {
2374 rb_advance_iter(iter);
2377 case RINGBUF_TYPE_TIME_EXTEND:
2378 /* Internal data, OK to advance */
2379 rb_advance_iter(iter);
2382 case RINGBUF_TYPE_TIME_STAMP:
2383 /* FIXME: not implemented */
2384 rb_advance_iter(iter);
2387 case RINGBUF_TYPE_DATA:
2389 *ts = iter->read_stamp + event->time_delta;
2390 ring_buffer_normalize_time_stamp(buffer,
2391 cpu_buffer->cpu, ts);
2401 EXPORT_SYMBOL_GPL(ring_buffer_iter_peek);
2404 * ring_buffer_peek - peek at the next event to be read
2405 * @buffer: The ring buffer to read
2406 * @cpu: The cpu to peak at
2407 * @ts: The timestamp counter of this event.
2409 * This will return the event that will be read next, but does
2410 * not consume the data.
2412 struct ring_buffer_event *
2413 ring_buffer_peek(struct ring_buffer *buffer, int cpu, u64 *ts)
2415 struct ring_buffer_per_cpu *cpu_buffer = buffer->buffers[cpu];
2416 struct ring_buffer_event *event;
2417 unsigned long flags;
2419 if (!cpumask_test_cpu(cpu, buffer->cpumask))
2423 spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
2424 event = rb_buffer_peek(buffer, cpu, ts);
2425 spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
2427 if (event && event->type_len == RINGBUF_TYPE_PADDING) {
2436 * ring_buffer_iter_peek - peek at the next event to be read
2437 * @iter: The ring buffer iterator
2438 * @ts: The timestamp counter of this event.
2440 * This will return the event that will be read next, but does
2441 * not increment the iterator.
2443 struct ring_buffer_event *
2444 ring_buffer_iter_peek(struct ring_buffer_iter *iter, u64 *ts)
2446 struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
2447 struct ring_buffer_event *event;
2448 unsigned long flags;
2451 spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
2452 event = rb_iter_peek(iter, ts);
2453 spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
2455 if (event && event->type_len == RINGBUF_TYPE_PADDING) {
2464 * ring_buffer_consume - return an event and consume it
2465 * @buffer: The ring buffer to get the next event from
2467 * Returns the next event in the ring buffer, and that event is consumed.
2468 * Meaning, that sequential reads will keep returning a different event,
2469 * and eventually empty the ring buffer if the producer is slower.
2471 struct ring_buffer_event *
2472 ring_buffer_consume(struct ring_buffer *buffer, int cpu, u64 *ts)
2474 struct ring_buffer_per_cpu *cpu_buffer;
2475 struct ring_buffer_event *event = NULL;
2476 unsigned long flags;
2479 /* might be called in atomic */
2482 if (!cpumask_test_cpu(cpu, buffer->cpumask))
2485 cpu_buffer = buffer->buffers[cpu];
2486 spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
2488 event = rb_buffer_peek(buffer, cpu, ts);
2492 rb_advance_reader(cpu_buffer);
2495 spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
2500 if (event && event->type_len == RINGBUF_TYPE_PADDING) {
2507 EXPORT_SYMBOL_GPL(ring_buffer_consume);
2510 * ring_buffer_read_start - start a non consuming read of the buffer
2511 * @buffer: The ring buffer to read from
2512 * @cpu: The cpu buffer to iterate over
2514 * This starts up an iteration through the buffer. It also disables
2515 * the recording to the buffer until the reading is finished.
2516 * This prevents the reading from being corrupted. This is not
2517 * a consuming read, so a producer is not expected.
2519 * Must be paired with ring_buffer_finish.
2521 struct ring_buffer_iter *
2522 ring_buffer_read_start(struct ring_buffer *buffer, int cpu)
2524 struct ring_buffer_per_cpu *cpu_buffer;
2525 struct ring_buffer_iter *iter;
2526 unsigned long flags;
2528 if (!cpumask_test_cpu(cpu, buffer->cpumask))
2531 iter = kmalloc(sizeof(*iter), GFP_KERNEL);
2535 cpu_buffer = buffer->buffers[cpu];
2537 iter->cpu_buffer = cpu_buffer;
2539 atomic_inc(&cpu_buffer->record_disabled);
2540 synchronize_sched();
2542 spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
2543 __raw_spin_lock(&cpu_buffer->lock);
2544 rb_iter_reset(iter);
2545 __raw_spin_unlock(&cpu_buffer->lock);
2546 spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
2550 EXPORT_SYMBOL_GPL(ring_buffer_read_start);
2553 * ring_buffer_finish - finish reading the iterator of the buffer
2554 * @iter: The iterator retrieved by ring_buffer_start
2556 * This re-enables the recording to the buffer, and frees the
2560 ring_buffer_read_finish(struct ring_buffer_iter *iter)
2562 struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
2564 atomic_dec(&cpu_buffer->record_disabled);
2567 EXPORT_SYMBOL_GPL(ring_buffer_read_finish);
2570 * ring_buffer_read - read the next item in the ring buffer by the iterator
2571 * @iter: The ring buffer iterator
2572 * @ts: The time stamp of the event read.
2574 * This reads the next event in the ring buffer and increments the iterator.
2576 struct ring_buffer_event *
2577 ring_buffer_read(struct ring_buffer_iter *iter, u64 *ts)
2579 struct ring_buffer_event *event;
2580 struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
2581 unsigned long flags;
2584 spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
2585 event = rb_iter_peek(iter, ts);
2589 rb_advance_iter(iter);
2591 spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
2593 if (event && event->type_len == RINGBUF_TYPE_PADDING) {
2600 EXPORT_SYMBOL_GPL(ring_buffer_read);
2603 * ring_buffer_size - return the size of the ring buffer (in bytes)
2604 * @buffer: The ring buffer.
2606 unsigned long ring_buffer_size(struct ring_buffer *buffer)
2608 return BUF_PAGE_SIZE * buffer->pages;
2610 EXPORT_SYMBOL_GPL(ring_buffer_size);
2613 rb_reset_cpu(struct ring_buffer_per_cpu *cpu_buffer)
2615 cpu_buffer->head_page
2616 = list_entry(cpu_buffer->pages.next, struct buffer_page, list);
2617 local_set(&cpu_buffer->head_page->write, 0);
2618 local_set(&cpu_buffer->head_page->entries, 0);
2619 local_set(&cpu_buffer->head_page->page->commit, 0);
2621 cpu_buffer->head_page->read = 0;
2623 cpu_buffer->tail_page = cpu_buffer->head_page;
2624 cpu_buffer->commit_page = cpu_buffer->head_page;
2626 INIT_LIST_HEAD(&cpu_buffer->reader_page->list);
2627 local_set(&cpu_buffer->reader_page->write, 0);
2628 local_set(&cpu_buffer->reader_page->entries, 0);
2629 local_set(&cpu_buffer->reader_page->page->commit, 0);
2630 cpu_buffer->reader_page->read = 0;
2632 cpu_buffer->nmi_dropped = 0;
2633 cpu_buffer->commit_overrun = 0;
2634 cpu_buffer->overrun = 0;
2635 cpu_buffer->read = 0;
2636 local_set(&cpu_buffer->entries, 0);
2638 cpu_buffer->write_stamp = 0;
2639 cpu_buffer->read_stamp = 0;
2643 * ring_buffer_reset_cpu - reset a ring buffer per CPU buffer
2644 * @buffer: The ring buffer to reset a per cpu buffer of
2645 * @cpu: The CPU buffer to be reset
2647 void ring_buffer_reset_cpu(struct ring_buffer *buffer, int cpu)
2649 struct ring_buffer_per_cpu *cpu_buffer = buffer->buffers[cpu];
2650 unsigned long flags;
2652 if (!cpumask_test_cpu(cpu, buffer->cpumask))
2655 atomic_inc(&cpu_buffer->record_disabled);
2657 spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
2659 __raw_spin_lock(&cpu_buffer->lock);
2661 rb_reset_cpu(cpu_buffer);
2663 __raw_spin_unlock(&cpu_buffer->lock);
2665 spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
2667 atomic_dec(&cpu_buffer->record_disabled);
2669 EXPORT_SYMBOL_GPL(ring_buffer_reset_cpu);
2672 * ring_buffer_reset - reset a ring buffer
2673 * @buffer: The ring buffer to reset all cpu buffers
2675 void ring_buffer_reset(struct ring_buffer *buffer)
2679 for_each_buffer_cpu(buffer, cpu)
2680 ring_buffer_reset_cpu(buffer, cpu);
2682 EXPORT_SYMBOL_GPL(ring_buffer_reset);
2685 * rind_buffer_empty - is the ring buffer empty?
2686 * @buffer: The ring buffer to test
2688 int ring_buffer_empty(struct ring_buffer *buffer)
2690 struct ring_buffer_per_cpu *cpu_buffer;
2693 /* yes this is racy, but if you don't like the race, lock the buffer */
2694 for_each_buffer_cpu(buffer, cpu) {
2695 cpu_buffer = buffer->buffers[cpu];
2696 if (!rb_per_cpu_empty(cpu_buffer))
2702 EXPORT_SYMBOL_GPL(ring_buffer_empty);
2705 * ring_buffer_empty_cpu - is a cpu buffer of a ring buffer empty?
2706 * @buffer: The ring buffer
2707 * @cpu: The CPU buffer to test
2709 int ring_buffer_empty_cpu(struct ring_buffer *buffer, int cpu)
2711 struct ring_buffer_per_cpu *cpu_buffer;
2714 if (!cpumask_test_cpu(cpu, buffer->cpumask))
2717 cpu_buffer = buffer->buffers[cpu];
2718 ret = rb_per_cpu_empty(cpu_buffer);
2723 EXPORT_SYMBOL_GPL(ring_buffer_empty_cpu);
2726 * ring_buffer_swap_cpu - swap a CPU buffer between two ring buffers
2727 * @buffer_a: One buffer to swap with
2728 * @buffer_b: The other buffer to swap with
2730 * This function is useful for tracers that want to take a "snapshot"
2731 * of a CPU buffer and has another back up buffer lying around.
2732 * it is expected that the tracer handles the cpu buffer not being
2733 * used at the moment.
2735 int ring_buffer_swap_cpu(struct ring_buffer *buffer_a,
2736 struct ring_buffer *buffer_b, int cpu)
2738 struct ring_buffer_per_cpu *cpu_buffer_a;
2739 struct ring_buffer_per_cpu *cpu_buffer_b;
2742 if (!cpumask_test_cpu(cpu, buffer_a->cpumask) ||
2743 !cpumask_test_cpu(cpu, buffer_b->cpumask))
2746 /* At least make sure the two buffers are somewhat the same */
2747 if (buffer_a->pages != buffer_b->pages)
2752 if (ring_buffer_flags != RB_BUFFERS_ON)
2755 if (atomic_read(&buffer_a->record_disabled))
2758 if (atomic_read(&buffer_b->record_disabled))
2761 cpu_buffer_a = buffer_a->buffers[cpu];
2762 cpu_buffer_b = buffer_b->buffers[cpu];
2764 if (atomic_read(&cpu_buffer_a->record_disabled))
2767 if (atomic_read(&cpu_buffer_b->record_disabled))
2771 * We can't do a synchronize_sched here because this
2772 * function can be called in atomic context.
2773 * Normally this will be called from the same CPU as cpu.
2774 * If not it's up to the caller to protect this.
2776 atomic_inc(&cpu_buffer_a->record_disabled);
2777 atomic_inc(&cpu_buffer_b->record_disabled);
2779 buffer_a->buffers[cpu] = cpu_buffer_b;
2780 buffer_b->buffers[cpu] = cpu_buffer_a;
2782 cpu_buffer_b->buffer = buffer_a;
2783 cpu_buffer_a->buffer = buffer_b;
2785 atomic_dec(&cpu_buffer_a->record_disabled);
2786 atomic_dec(&cpu_buffer_b->record_disabled);
2792 EXPORT_SYMBOL_GPL(ring_buffer_swap_cpu);
2795 * ring_buffer_alloc_read_page - allocate a page to read from buffer
2796 * @buffer: the buffer to allocate for.
2798 * This function is used in conjunction with ring_buffer_read_page.
2799 * When reading a full page from the ring buffer, these functions
2800 * can be used to speed up the process. The calling function should
2801 * allocate a few pages first with this function. Then when it
2802 * needs to get pages from the ring buffer, it passes the result
2803 * of this function into ring_buffer_read_page, which will swap
2804 * the page that was allocated, with the read page of the buffer.
2807 * The page allocated, or NULL on error.
2809 void *ring_buffer_alloc_read_page(struct ring_buffer *buffer)
2811 struct buffer_data_page *bpage;
2814 addr = __get_free_page(GFP_KERNEL);
2818 bpage = (void *)addr;
2820 rb_init_page(bpage);
2824 EXPORT_SYMBOL_GPL(ring_buffer_alloc_read_page);
2827 * ring_buffer_free_read_page - free an allocated read page
2828 * @buffer: the buffer the page was allocate for
2829 * @data: the page to free
2831 * Free a page allocated from ring_buffer_alloc_read_page.
2833 void ring_buffer_free_read_page(struct ring_buffer *buffer, void *data)
2835 free_page((unsigned long)data);
2837 EXPORT_SYMBOL_GPL(ring_buffer_free_read_page);
2840 * ring_buffer_read_page - extract a page from the ring buffer
2841 * @buffer: buffer to extract from
2842 * @data_page: the page to use allocated from ring_buffer_alloc_read_page
2843 * @len: amount to extract
2844 * @cpu: the cpu of the buffer to extract
2845 * @full: should the extraction only happen when the page is full.
2847 * This function will pull out a page from the ring buffer and consume it.
2848 * @data_page must be the address of the variable that was returned
2849 * from ring_buffer_alloc_read_page. This is because the page might be used
2850 * to swap with a page in the ring buffer.
2853 * rpage = ring_buffer_alloc_read_page(buffer);
2856 * ret = ring_buffer_read_page(buffer, &rpage, len, cpu, 0);
2858 * process_page(rpage, ret);
2860 * When @full is set, the function will not return true unless
2861 * the writer is off the reader page.
2863 * Note: it is up to the calling functions to handle sleeps and wakeups.
2864 * The ring buffer can be used anywhere in the kernel and can not
2865 * blindly call wake_up. The layer that uses the ring buffer must be
2866 * responsible for that.
2869 * >=0 if data has been transferred, returns the offset of consumed data.
2870 * <0 if no data has been transferred.
2872 int ring_buffer_read_page(struct ring_buffer *buffer,
2873 void **data_page, size_t len, int cpu, int full)
2875 struct ring_buffer_per_cpu *cpu_buffer = buffer->buffers[cpu];
2876 struct ring_buffer_event *event;
2877 struct buffer_data_page *bpage;
2878 struct buffer_page *reader;
2879 unsigned long flags;
2880 unsigned int commit;
2885 if (!cpumask_test_cpu(cpu, buffer->cpumask))
2889 * If len is not big enough to hold the page header, then
2890 * we can not copy anything.
2892 if (len <= BUF_PAGE_HDR_SIZE)
2895 len -= BUF_PAGE_HDR_SIZE;
2904 spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
2906 reader = rb_get_reader_page(cpu_buffer);
2910 event = rb_reader_event(cpu_buffer);
2912 read = reader->read;
2913 commit = rb_page_commit(reader);
2916 * If this page has been partially read or
2917 * if len is not big enough to read the rest of the page or
2918 * a writer is still on the page, then
2919 * we must copy the data from the page to the buffer.
2920 * Otherwise, we can simply swap the page with the one passed in.
2922 if (read || (len < (commit - read)) ||
2923 cpu_buffer->reader_page == cpu_buffer->commit_page) {
2924 struct buffer_data_page *rpage = cpu_buffer->reader_page->page;
2925 unsigned int rpos = read;
2926 unsigned int pos = 0;
2932 if (len > (commit - read))
2933 len = (commit - read);
2935 size = rb_event_length(event);
2940 /* save the current timestamp, since the user will need it */
2941 save_timestamp = cpu_buffer->read_stamp;
2943 /* Need to copy one event at a time */
2945 memcpy(bpage->data + pos, rpage->data + rpos, size);
2949 rb_advance_reader(cpu_buffer);
2950 rpos = reader->read;
2953 event = rb_reader_event(cpu_buffer);
2954 size = rb_event_length(event);
2955 } while (len > size);
2958 local_set(&bpage->commit, pos);
2959 bpage->time_stamp = save_timestamp;
2961 /* we copied everything to the beginning */
2964 /* update the entry counter */
2965 cpu_buffer->read += local_read(&reader->entries);
2967 /* swap the pages */
2968 rb_init_page(bpage);
2969 bpage = reader->page;
2970 reader->page = *data_page;
2971 local_set(&reader->write, 0);
2972 local_set(&reader->entries, 0);
2979 spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
2984 EXPORT_SYMBOL_GPL(ring_buffer_read_page);
2987 rb_simple_read(struct file *filp, char __user *ubuf,
2988 size_t cnt, loff_t *ppos)
2990 unsigned long *p = filp->private_data;
2994 if (test_bit(RB_BUFFERS_DISABLED_BIT, p))
2995 r = sprintf(buf, "permanently disabled\n");
2997 r = sprintf(buf, "%d\n", test_bit(RB_BUFFERS_ON_BIT, p));
2999 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
3003 rb_simple_write(struct file *filp, const char __user *ubuf,
3004 size_t cnt, loff_t *ppos)
3006 unsigned long *p = filp->private_data;
3011 if (cnt >= sizeof(buf))
3014 if (copy_from_user(&buf, ubuf, cnt))
3019 ret = strict_strtoul(buf, 10, &val);
3024 set_bit(RB_BUFFERS_ON_BIT, p);
3026 clear_bit(RB_BUFFERS_ON_BIT, p);
3033 static const struct file_operations rb_simple_fops = {
3034 .open = tracing_open_generic,
3035 .read = rb_simple_read,
3036 .write = rb_simple_write,
3040 static __init int rb_init_debugfs(void)
3042 struct dentry *d_tracer;
3044 d_tracer = tracing_init_dentry();
3046 trace_create_file("tracing_on", 0644, d_tracer,
3047 &ring_buffer_flags, &rb_simple_fops);
3052 fs_initcall(rb_init_debugfs);
3054 #ifdef CONFIG_HOTPLUG_CPU
3055 static int rb_cpu_notify(struct notifier_block *self,
3056 unsigned long action, void *hcpu)
3058 struct ring_buffer *buffer =
3059 container_of(self, struct ring_buffer, cpu_notify);
3060 long cpu = (long)hcpu;
3063 case CPU_UP_PREPARE:
3064 case CPU_UP_PREPARE_FROZEN:
3065 if (cpu_isset(cpu, *buffer->cpumask))
3068 buffer->buffers[cpu] =
3069 rb_allocate_cpu_buffer(buffer, cpu);
3070 if (!buffer->buffers[cpu]) {
3071 WARN(1, "failed to allocate ring buffer on CPU %ld\n",
3076 cpu_set(cpu, *buffer->cpumask);
3078 case CPU_DOWN_PREPARE:
3079 case CPU_DOWN_PREPARE_FROZEN:
3082 * If we were to free the buffer, then the user would
3083 * lose any trace that was in the buffer.