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 /* Max payload is BUF_PAGE_SIZE - header (8bytes) */
371 #define BUF_MAX_DATA_SIZE (BUF_PAGE_SIZE - (sizeof(u32) * 2))
373 int ring_buffer_print_page_header(struct trace_seq *s)
375 struct buffer_data_page field;
378 ret = trace_seq_printf(s, "\tfield: u64 timestamp;\t"
379 "offset:0;\tsize:%u;\n",
380 (unsigned int)sizeof(field.time_stamp));
382 ret = trace_seq_printf(s, "\tfield: local_t commit;\t"
383 "offset:%u;\tsize:%u;\n",
384 (unsigned int)offsetof(typeof(field), commit),
385 (unsigned int)sizeof(field.commit));
387 ret = trace_seq_printf(s, "\tfield: char data;\t"
388 "offset:%u;\tsize:%u;\n",
389 (unsigned int)offsetof(typeof(field), data),
390 (unsigned int)BUF_PAGE_SIZE);
396 * head_page == tail_page && head == tail then buffer is empty.
398 struct ring_buffer_per_cpu {
400 struct ring_buffer *buffer;
401 spinlock_t reader_lock; /* serialize readers */
403 struct lock_class_key lock_key;
404 struct list_head pages;
405 struct buffer_page *head_page; /* read from head */
406 struct buffer_page *tail_page; /* write to tail */
407 struct buffer_page *commit_page; /* committed pages */
408 struct buffer_page *reader_page;
409 unsigned long nmi_dropped;
410 unsigned long commit_overrun;
411 unsigned long overrun;
416 atomic_t record_disabled;
423 atomic_t record_disabled;
424 cpumask_var_t cpumask;
428 struct ring_buffer_per_cpu **buffers;
430 #ifdef CONFIG_HOTPLUG_CPU
431 struct notifier_block cpu_notify;
436 struct ring_buffer_iter {
437 struct ring_buffer_per_cpu *cpu_buffer;
439 struct buffer_page *head_page;
443 /* buffer may be either ring_buffer or ring_buffer_per_cpu */
444 #define RB_WARN_ON(buffer, cond) \
446 int _____ret = unlikely(cond); \
448 atomic_inc(&buffer->record_disabled); \
454 /* Up this if you want to test the TIME_EXTENTS and normalization */
455 #define DEBUG_SHIFT 0
457 static inline u64 rb_time_stamp(struct ring_buffer *buffer, int cpu)
459 /* shift to debug/test normalization and TIME_EXTENTS */
460 return buffer->clock() << DEBUG_SHIFT;
463 u64 ring_buffer_time_stamp(struct ring_buffer *buffer, int cpu)
467 preempt_disable_notrace();
468 time = rb_time_stamp(buffer, cpu);
469 preempt_enable_no_resched_notrace();
473 EXPORT_SYMBOL_GPL(ring_buffer_time_stamp);
475 void ring_buffer_normalize_time_stamp(struct ring_buffer *buffer,
478 /* Just stupid testing the normalize function and deltas */
481 EXPORT_SYMBOL_GPL(ring_buffer_normalize_time_stamp);
484 * check_pages - integrity check of buffer pages
485 * @cpu_buffer: CPU buffer with pages to test
487 * As a safety measure we check to make sure the data pages have not
490 static int rb_check_pages(struct ring_buffer_per_cpu *cpu_buffer)
492 struct list_head *head = &cpu_buffer->pages;
493 struct buffer_page *bpage, *tmp;
495 if (RB_WARN_ON(cpu_buffer, head->next->prev != head))
497 if (RB_WARN_ON(cpu_buffer, head->prev->next != head))
500 list_for_each_entry_safe(bpage, tmp, head, list) {
501 if (RB_WARN_ON(cpu_buffer,
502 bpage->list.next->prev != &bpage->list))
504 if (RB_WARN_ON(cpu_buffer,
505 bpage->list.prev->next != &bpage->list))
512 static int rb_allocate_pages(struct ring_buffer_per_cpu *cpu_buffer,
515 struct list_head *head = &cpu_buffer->pages;
516 struct buffer_page *bpage, *tmp;
521 for (i = 0; i < nr_pages; i++) {
522 bpage = kzalloc_node(ALIGN(sizeof(*bpage), cache_line_size()),
523 GFP_KERNEL, cpu_to_node(cpu_buffer->cpu));
526 list_add(&bpage->list, &pages);
528 addr = __get_free_page(GFP_KERNEL);
531 bpage->page = (void *)addr;
532 rb_init_page(bpage->page);
535 list_splice(&pages, head);
537 rb_check_pages(cpu_buffer);
542 list_for_each_entry_safe(bpage, tmp, &pages, list) {
543 list_del_init(&bpage->list);
544 free_buffer_page(bpage);
549 static struct ring_buffer_per_cpu *
550 rb_allocate_cpu_buffer(struct ring_buffer *buffer, int cpu)
552 struct ring_buffer_per_cpu *cpu_buffer;
553 struct buffer_page *bpage;
557 cpu_buffer = kzalloc_node(ALIGN(sizeof(*cpu_buffer), cache_line_size()),
558 GFP_KERNEL, cpu_to_node(cpu));
562 cpu_buffer->cpu = cpu;
563 cpu_buffer->buffer = buffer;
564 spin_lock_init(&cpu_buffer->reader_lock);
565 cpu_buffer->lock = (raw_spinlock_t)__RAW_SPIN_LOCK_UNLOCKED;
566 INIT_LIST_HEAD(&cpu_buffer->pages);
568 bpage = kzalloc_node(ALIGN(sizeof(*bpage), cache_line_size()),
569 GFP_KERNEL, cpu_to_node(cpu));
571 goto fail_free_buffer;
573 cpu_buffer->reader_page = bpage;
574 addr = __get_free_page(GFP_KERNEL);
576 goto fail_free_reader;
577 bpage->page = (void *)addr;
578 rb_init_page(bpage->page);
580 INIT_LIST_HEAD(&cpu_buffer->reader_page->list);
582 ret = rb_allocate_pages(cpu_buffer, buffer->pages);
584 goto fail_free_reader;
586 cpu_buffer->head_page
587 = list_entry(cpu_buffer->pages.next, struct buffer_page, list);
588 cpu_buffer->tail_page = cpu_buffer->commit_page = cpu_buffer->head_page;
593 free_buffer_page(cpu_buffer->reader_page);
600 static void rb_free_cpu_buffer(struct ring_buffer_per_cpu *cpu_buffer)
602 struct list_head *head = &cpu_buffer->pages;
603 struct buffer_page *bpage, *tmp;
605 free_buffer_page(cpu_buffer->reader_page);
607 list_for_each_entry_safe(bpage, tmp, head, list) {
608 list_del_init(&bpage->list);
609 free_buffer_page(bpage);
615 * Causes compile errors if the struct buffer_page gets bigger
616 * than the struct page.
618 extern int ring_buffer_page_too_big(void);
620 #ifdef CONFIG_HOTPLUG_CPU
621 static int rb_cpu_notify(struct notifier_block *self,
622 unsigned long action, void *hcpu);
626 * ring_buffer_alloc - allocate a new ring_buffer
627 * @size: the size in bytes per cpu that is needed.
628 * @flags: attributes to set for the ring buffer.
630 * Currently the only flag that is available is the RB_FL_OVERWRITE
631 * flag. This flag means that the buffer will overwrite old data
632 * when the buffer wraps. If this flag is not set, the buffer will
633 * drop data when the tail hits the head.
635 struct ring_buffer *ring_buffer_alloc(unsigned long size, unsigned flags)
637 struct ring_buffer *buffer;
641 /* Paranoid! Optimizes out when all is well */
642 if (sizeof(struct buffer_page) > sizeof(struct page))
643 ring_buffer_page_too_big();
646 /* keep it in its own cache line */
647 buffer = kzalloc(ALIGN(sizeof(*buffer), cache_line_size()),
652 if (!alloc_cpumask_var(&buffer->cpumask, GFP_KERNEL))
653 goto fail_free_buffer;
655 buffer->pages = DIV_ROUND_UP(size, BUF_PAGE_SIZE);
656 buffer->flags = flags;
657 buffer->clock = trace_clock_local;
659 /* need at least two pages */
660 if (buffer->pages == 1)
664 * In case of non-hotplug cpu, if the ring-buffer is allocated
665 * in early initcall, it will not be notified of secondary cpus.
666 * In that off case, we need to allocate for all possible cpus.
668 #ifdef CONFIG_HOTPLUG_CPU
670 cpumask_copy(buffer->cpumask, cpu_online_mask);
672 cpumask_copy(buffer->cpumask, cpu_possible_mask);
674 buffer->cpus = nr_cpu_ids;
676 bsize = sizeof(void *) * nr_cpu_ids;
677 buffer->buffers = kzalloc(ALIGN(bsize, cache_line_size()),
679 if (!buffer->buffers)
680 goto fail_free_cpumask;
682 for_each_buffer_cpu(buffer, cpu) {
683 buffer->buffers[cpu] =
684 rb_allocate_cpu_buffer(buffer, cpu);
685 if (!buffer->buffers[cpu])
686 goto fail_free_buffers;
689 #ifdef CONFIG_HOTPLUG_CPU
690 buffer->cpu_notify.notifier_call = rb_cpu_notify;
691 buffer->cpu_notify.priority = 0;
692 register_cpu_notifier(&buffer->cpu_notify);
696 mutex_init(&buffer->mutex);
701 for_each_buffer_cpu(buffer, cpu) {
702 if (buffer->buffers[cpu])
703 rb_free_cpu_buffer(buffer->buffers[cpu]);
705 kfree(buffer->buffers);
708 free_cpumask_var(buffer->cpumask);
715 EXPORT_SYMBOL_GPL(ring_buffer_alloc);
718 * ring_buffer_free - free a ring buffer.
719 * @buffer: the buffer to free.
722 ring_buffer_free(struct ring_buffer *buffer)
728 #ifdef CONFIG_HOTPLUG_CPU
729 unregister_cpu_notifier(&buffer->cpu_notify);
732 for_each_buffer_cpu(buffer, cpu)
733 rb_free_cpu_buffer(buffer->buffers[cpu]);
737 free_cpumask_var(buffer->cpumask);
741 EXPORT_SYMBOL_GPL(ring_buffer_free);
743 void ring_buffer_set_clock(struct ring_buffer *buffer,
746 buffer->clock = clock;
749 static void rb_reset_cpu(struct ring_buffer_per_cpu *cpu_buffer);
752 rb_remove_pages(struct ring_buffer_per_cpu *cpu_buffer, unsigned nr_pages)
754 struct buffer_page *bpage;
758 atomic_inc(&cpu_buffer->record_disabled);
761 for (i = 0; i < nr_pages; i++) {
762 if (RB_WARN_ON(cpu_buffer, list_empty(&cpu_buffer->pages)))
764 p = cpu_buffer->pages.next;
765 bpage = list_entry(p, struct buffer_page, list);
766 list_del_init(&bpage->list);
767 free_buffer_page(bpage);
769 if (RB_WARN_ON(cpu_buffer, list_empty(&cpu_buffer->pages)))
772 rb_reset_cpu(cpu_buffer);
774 rb_check_pages(cpu_buffer);
776 atomic_dec(&cpu_buffer->record_disabled);
781 rb_insert_pages(struct ring_buffer_per_cpu *cpu_buffer,
782 struct list_head *pages, unsigned nr_pages)
784 struct buffer_page *bpage;
788 atomic_inc(&cpu_buffer->record_disabled);
791 for (i = 0; i < nr_pages; i++) {
792 if (RB_WARN_ON(cpu_buffer, list_empty(pages)))
795 bpage = list_entry(p, struct buffer_page, list);
796 list_del_init(&bpage->list);
797 list_add_tail(&bpage->list, &cpu_buffer->pages);
799 rb_reset_cpu(cpu_buffer);
801 rb_check_pages(cpu_buffer);
803 atomic_dec(&cpu_buffer->record_disabled);
807 * ring_buffer_resize - resize the ring buffer
808 * @buffer: the buffer to resize.
809 * @size: the new size.
811 * The tracer is responsible for making sure that the buffer is
812 * not being used while changing the size.
813 * Note: We may be able to change the above requirement by using
814 * RCU synchronizations.
816 * Minimum size is 2 * BUF_PAGE_SIZE.
818 * Returns -1 on failure.
820 int ring_buffer_resize(struct ring_buffer *buffer, unsigned long size)
822 struct ring_buffer_per_cpu *cpu_buffer;
823 unsigned nr_pages, rm_pages, new_pages;
824 struct buffer_page *bpage, *tmp;
825 unsigned long buffer_size;
831 * Always succeed at resizing a non-existent buffer:
836 size = DIV_ROUND_UP(size, BUF_PAGE_SIZE);
837 size *= BUF_PAGE_SIZE;
838 buffer_size = buffer->pages * BUF_PAGE_SIZE;
840 /* we need a minimum of two pages */
841 if (size < BUF_PAGE_SIZE * 2)
842 size = BUF_PAGE_SIZE * 2;
844 if (size == buffer_size)
847 mutex_lock(&buffer->mutex);
850 nr_pages = DIV_ROUND_UP(size, BUF_PAGE_SIZE);
852 if (size < buffer_size) {
854 /* easy case, just free pages */
855 if (RB_WARN_ON(buffer, nr_pages >= buffer->pages))
858 rm_pages = buffer->pages - nr_pages;
860 for_each_buffer_cpu(buffer, cpu) {
861 cpu_buffer = buffer->buffers[cpu];
862 rb_remove_pages(cpu_buffer, rm_pages);
868 * This is a bit more difficult. We only want to add pages
869 * when we can allocate enough for all CPUs. We do this
870 * by allocating all the pages and storing them on a local
871 * link list. If we succeed in our allocation, then we
872 * add these pages to the cpu_buffers. Otherwise we just free
873 * them all and return -ENOMEM;
875 if (RB_WARN_ON(buffer, nr_pages <= buffer->pages))
878 new_pages = nr_pages - buffer->pages;
880 for_each_buffer_cpu(buffer, cpu) {
881 for (i = 0; i < new_pages; i++) {
882 bpage = kzalloc_node(ALIGN(sizeof(*bpage),
884 GFP_KERNEL, cpu_to_node(cpu));
887 list_add(&bpage->list, &pages);
888 addr = __get_free_page(GFP_KERNEL);
891 bpage->page = (void *)addr;
892 rb_init_page(bpage->page);
896 for_each_buffer_cpu(buffer, cpu) {
897 cpu_buffer = buffer->buffers[cpu];
898 rb_insert_pages(cpu_buffer, &pages, new_pages);
901 if (RB_WARN_ON(buffer, !list_empty(&pages)))
905 buffer->pages = nr_pages;
907 mutex_unlock(&buffer->mutex);
912 list_for_each_entry_safe(bpage, tmp, &pages, list) {
913 list_del_init(&bpage->list);
914 free_buffer_page(bpage);
917 mutex_unlock(&buffer->mutex);
921 * Something went totally wrong, and we are too paranoid
922 * to even clean up the mess.
926 mutex_unlock(&buffer->mutex);
929 EXPORT_SYMBOL_GPL(ring_buffer_resize);
932 __rb_data_page_index(struct buffer_data_page *bpage, unsigned index)
934 return bpage->data + index;
937 static inline void *__rb_page_index(struct buffer_page *bpage, unsigned index)
939 return bpage->page->data + index;
942 static inline struct ring_buffer_event *
943 rb_reader_event(struct ring_buffer_per_cpu *cpu_buffer)
945 return __rb_page_index(cpu_buffer->reader_page,
946 cpu_buffer->reader_page->read);
949 static inline struct ring_buffer_event *
950 rb_head_event(struct ring_buffer_per_cpu *cpu_buffer)
952 return __rb_page_index(cpu_buffer->head_page,
953 cpu_buffer->head_page->read);
956 static inline struct ring_buffer_event *
957 rb_iter_head_event(struct ring_buffer_iter *iter)
959 return __rb_page_index(iter->head_page, iter->head);
962 static inline unsigned rb_page_write(struct buffer_page *bpage)
964 return local_read(&bpage->write);
967 static inline unsigned rb_page_commit(struct buffer_page *bpage)
969 return local_read(&bpage->page->commit);
972 /* Size is determined by what has been commited */
973 static inline unsigned rb_page_size(struct buffer_page *bpage)
975 return rb_page_commit(bpage);
978 static inline unsigned
979 rb_commit_index(struct ring_buffer_per_cpu *cpu_buffer)
981 return rb_page_commit(cpu_buffer->commit_page);
984 static inline unsigned rb_head_size(struct ring_buffer_per_cpu *cpu_buffer)
986 return rb_page_commit(cpu_buffer->head_page);
989 static inline void rb_inc_page(struct ring_buffer_per_cpu *cpu_buffer,
990 struct buffer_page **bpage)
992 struct list_head *p = (*bpage)->list.next;
994 if (p == &cpu_buffer->pages)
997 *bpage = list_entry(p, struct buffer_page, list);
1000 static inline unsigned
1001 rb_event_index(struct ring_buffer_event *event)
1003 unsigned long addr = (unsigned long)event;
1005 return (addr & ~PAGE_MASK) - (PAGE_SIZE - BUF_PAGE_SIZE);
1009 rb_is_commit(struct ring_buffer_per_cpu *cpu_buffer,
1010 struct ring_buffer_event *event)
1012 unsigned long addr = (unsigned long)event;
1013 unsigned long index;
1015 index = rb_event_index(event);
1018 return cpu_buffer->commit_page->page == (void *)addr &&
1019 rb_commit_index(cpu_buffer) == index;
1023 rb_set_commit_event(struct ring_buffer_per_cpu *cpu_buffer,
1024 struct ring_buffer_event *event)
1026 unsigned long addr = (unsigned long)event;
1027 unsigned long index;
1029 index = rb_event_index(event);
1032 while (cpu_buffer->commit_page->page != (void *)addr) {
1033 if (RB_WARN_ON(cpu_buffer,
1034 cpu_buffer->commit_page == cpu_buffer->tail_page))
1036 cpu_buffer->commit_page->page->commit =
1037 cpu_buffer->commit_page->write;
1038 rb_inc_page(cpu_buffer, &cpu_buffer->commit_page);
1039 cpu_buffer->write_stamp =
1040 cpu_buffer->commit_page->page->time_stamp;
1043 /* Now set the commit to the event's index */
1044 local_set(&cpu_buffer->commit_page->page->commit, index);
1048 rb_set_commit_to_write(struct ring_buffer_per_cpu *cpu_buffer)
1051 * We only race with interrupts and NMIs on this CPU.
1052 * If we own the commit event, then we can commit
1053 * all others that interrupted us, since the interruptions
1054 * are in stack format (they finish before they come
1055 * back to us). This allows us to do a simple loop to
1056 * assign the commit to the tail.
1059 while (cpu_buffer->commit_page != cpu_buffer->tail_page) {
1060 cpu_buffer->commit_page->page->commit =
1061 cpu_buffer->commit_page->write;
1062 rb_inc_page(cpu_buffer, &cpu_buffer->commit_page);
1063 cpu_buffer->write_stamp =
1064 cpu_buffer->commit_page->page->time_stamp;
1065 /* add barrier to keep gcc from optimizing too much */
1068 while (rb_commit_index(cpu_buffer) !=
1069 rb_page_write(cpu_buffer->commit_page)) {
1070 cpu_buffer->commit_page->page->commit =
1071 cpu_buffer->commit_page->write;
1075 /* again, keep gcc from optimizing */
1079 * If an interrupt came in just after the first while loop
1080 * and pushed the tail page forward, we will be left with
1081 * a dangling commit that will never go forward.
1083 if (unlikely(cpu_buffer->commit_page != cpu_buffer->tail_page))
1087 static void rb_reset_reader_page(struct ring_buffer_per_cpu *cpu_buffer)
1089 cpu_buffer->read_stamp = cpu_buffer->reader_page->page->time_stamp;
1090 cpu_buffer->reader_page->read = 0;
1093 static void rb_inc_iter(struct ring_buffer_iter *iter)
1095 struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
1098 * The iterator could be on the reader page (it starts there).
1099 * But the head could have moved, since the reader was
1100 * found. Check for this case and assign the iterator
1101 * to the head page instead of next.
1103 if (iter->head_page == cpu_buffer->reader_page)
1104 iter->head_page = cpu_buffer->head_page;
1106 rb_inc_page(cpu_buffer, &iter->head_page);
1108 iter->read_stamp = iter->head_page->page->time_stamp;
1113 * ring_buffer_update_event - update event type and data
1114 * @event: the even to update
1115 * @type: the type of event
1116 * @length: the size of the event field in the ring buffer
1118 * Update the type and data fields of the event. The length
1119 * is the actual size that is written to the ring buffer,
1120 * and with this, we can determine what to place into the
1124 rb_update_event(struct ring_buffer_event *event,
1125 unsigned type, unsigned length)
1127 event->type_len = type;
1131 case RINGBUF_TYPE_PADDING:
1132 case RINGBUF_TYPE_TIME_EXTEND:
1133 case RINGBUF_TYPE_TIME_STAMP:
1137 length -= RB_EVNT_HDR_SIZE;
1138 if (length > RB_MAX_SMALL_DATA)
1139 event->array[0] = length;
1141 event->type_len = DIV_ROUND_UP(length, RB_ALIGNMENT);
1148 static unsigned rb_calculate_event_length(unsigned length)
1150 struct ring_buffer_event event; /* Used only for sizeof array */
1152 /* zero length can cause confusions */
1156 if (length > RB_MAX_SMALL_DATA)
1157 length += sizeof(event.array[0]);
1159 length += RB_EVNT_HDR_SIZE;
1160 length = ALIGN(length, RB_ALIGNMENT);
1166 static struct ring_buffer_event *
1167 rb_move_tail(struct ring_buffer_per_cpu *cpu_buffer,
1168 unsigned long length, unsigned long tail,
1169 struct buffer_page *commit_page,
1170 struct buffer_page *tail_page, u64 *ts)
1172 struct buffer_page *next_page, *head_page, *reader_page;
1173 struct ring_buffer *buffer = cpu_buffer->buffer;
1174 struct ring_buffer_event *event;
1175 bool lock_taken = false;
1176 unsigned long flags;
1178 next_page = tail_page;
1180 local_irq_save(flags);
1182 * Since the write to the buffer is still not
1183 * fully lockless, we must be careful with NMIs.
1184 * The locks in the writers are taken when a write
1185 * crosses to a new page. The locks protect against
1186 * races with the readers (this will soon be fixed
1187 * with a lockless solution).
1189 * Because we can not protect against NMIs, and we
1190 * want to keep traces reentrant, we need to manage
1191 * what happens when we are in an NMI.
1193 * NMIs can happen after we take the lock.
1194 * If we are in an NMI, only take the lock
1195 * if it is not already taken. Otherwise
1198 if (unlikely(in_nmi())) {
1199 if (!__raw_spin_trylock(&cpu_buffer->lock)) {
1200 cpu_buffer->nmi_dropped++;
1204 __raw_spin_lock(&cpu_buffer->lock);
1208 rb_inc_page(cpu_buffer, &next_page);
1210 head_page = cpu_buffer->head_page;
1211 reader_page = cpu_buffer->reader_page;
1213 /* we grabbed the lock before incrementing */
1214 if (RB_WARN_ON(cpu_buffer, next_page == reader_page))
1218 * If for some reason, we had an interrupt storm that made
1219 * it all the way around the buffer, bail, and warn
1222 if (unlikely(next_page == commit_page)) {
1223 cpu_buffer->commit_overrun++;
1227 if (next_page == head_page) {
1228 if (!(buffer->flags & RB_FL_OVERWRITE))
1231 /* tail_page has not moved yet? */
1232 if (tail_page == cpu_buffer->tail_page) {
1233 /* count overflows */
1234 cpu_buffer->overrun +=
1235 local_read(&head_page->entries);
1237 rb_inc_page(cpu_buffer, &head_page);
1238 cpu_buffer->head_page = head_page;
1239 cpu_buffer->head_page->read = 0;
1244 * If the tail page is still the same as what we think
1245 * it is, then it is up to us to update the tail
1248 if (tail_page == cpu_buffer->tail_page) {
1249 local_set(&next_page->write, 0);
1250 local_set(&next_page->entries, 0);
1251 local_set(&next_page->page->commit, 0);
1252 cpu_buffer->tail_page = next_page;
1254 /* reread the time stamp */
1255 *ts = rb_time_stamp(buffer, cpu_buffer->cpu);
1256 cpu_buffer->tail_page->page->time_stamp = *ts;
1260 * The actual tail page has moved forward.
1262 if (tail < BUF_PAGE_SIZE) {
1263 /* Mark the rest of the page with padding */
1264 event = __rb_page_index(tail_page, tail);
1265 rb_event_set_padding(event);
1268 /* Set the write back to the previous setting */
1269 local_sub(length, &tail_page->write);
1272 * If this was a commit entry that failed,
1273 * increment that too
1275 if (tail_page == cpu_buffer->commit_page &&
1276 tail == rb_commit_index(cpu_buffer)) {
1277 rb_set_commit_to_write(cpu_buffer);
1280 __raw_spin_unlock(&cpu_buffer->lock);
1281 local_irq_restore(flags);
1283 /* fail and let the caller try again */
1284 return ERR_PTR(-EAGAIN);
1288 local_sub(length, &tail_page->write);
1290 if (likely(lock_taken))
1291 __raw_spin_unlock(&cpu_buffer->lock);
1292 local_irq_restore(flags);
1296 static struct ring_buffer_event *
1297 __rb_reserve_next(struct ring_buffer_per_cpu *cpu_buffer,
1298 unsigned type, unsigned long length, u64 *ts)
1300 struct buffer_page *tail_page, *commit_page;
1301 struct ring_buffer_event *event;
1302 unsigned long tail, write;
1304 commit_page = cpu_buffer->commit_page;
1305 /* we just need to protect against interrupts */
1307 tail_page = cpu_buffer->tail_page;
1308 write = local_add_return(length, &tail_page->write);
1309 tail = write - length;
1311 /* See if we shot pass the end of this buffer page */
1312 if (write > BUF_PAGE_SIZE)
1313 return rb_move_tail(cpu_buffer, length, tail,
1314 commit_page, tail_page, ts);
1316 /* We reserved something on the buffer */
1318 if (RB_WARN_ON(cpu_buffer, write > BUF_PAGE_SIZE))
1321 event = __rb_page_index(tail_page, tail);
1322 rb_update_event(event, type, length);
1324 /* The passed in type is zero for DATA */
1326 local_inc(&tail_page->entries);
1329 * If this is a commit and the tail is zero, then update
1330 * this page's time stamp.
1332 if (!tail && rb_is_commit(cpu_buffer, event))
1333 cpu_buffer->commit_page->page->time_stamp = *ts;
1339 rb_add_time_stamp(struct ring_buffer_per_cpu *cpu_buffer,
1340 u64 *ts, u64 *delta)
1342 struct ring_buffer_event *event;
1346 if (unlikely(*delta > (1ULL << 59) && !once++)) {
1347 printk(KERN_WARNING "Delta way too big! %llu"
1348 " ts=%llu write stamp = %llu\n",
1349 (unsigned long long)*delta,
1350 (unsigned long long)*ts,
1351 (unsigned long long)cpu_buffer->write_stamp);
1356 * The delta is too big, we to add a
1359 event = __rb_reserve_next(cpu_buffer,
1360 RINGBUF_TYPE_TIME_EXTEND,
1366 if (PTR_ERR(event) == -EAGAIN)
1369 /* Only a commited time event can update the write stamp */
1370 if (rb_is_commit(cpu_buffer, event)) {
1372 * If this is the first on the page, then we need to
1373 * update the page itself, and just put in a zero.
1375 if (rb_event_index(event)) {
1376 event->time_delta = *delta & TS_MASK;
1377 event->array[0] = *delta >> TS_SHIFT;
1379 cpu_buffer->commit_page->page->time_stamp = *ts;
1380 event->time_delta = 0;
1381 event->array[0] = 0;
1383 cpu_buffer->write_stamp = *ts;
1384 /* let the caller know this was the commit */
1387 /* Darn, this is just wasted space */
1388 event->time_delta = 0;
1389 event->array[0] = 0;
1398 static struct ring_buffer_event *
1399 rb_reserve_next_event(struct ring_buffer_per_cpu *cpu_buffer,
1400 unsigned long length)
1402 struct ring_buffer_event *event;
1407 length = rb_calculate_event_length(length);
1410 * We allow for interrupts to reenter here and do a trace.
1411 * If one does, it will cause this original code to loop
1412 * back here. Even with heavy interrupts happening, this
1413 * should only happen a few times in a row. If this happens
1414 * 1000 times in a row, there must be either an interrupt
1415 * storm or we have something buggy.
1418 if (RB_WARN_ON(cpu_buffer, ++nr_loops > 1000))
1421 ts = rb_time_stamp(cpu_buffer->buffer, cpu_buffer->cpu);
1424 * Only the first commit can update the timestamp.
1425 * Yes there is a race here. If an interrupt comes in
1426 * just after the conditional and it traces too, then it
1427 * will also check the deltas. More than one timestamp may
1428 * also be made. But only the entry that did the actual
1429 * commit will be something other than zero.
1431 if (likely(cpu_buffer->tail_page == cpu_buffer->commit_page &&
1432 rb_page_write(cpu_buffer->tail_page) ==
1433 rb_commit_index(cpu_buffer))) {
1435 delta = ts - cpu_buffer->write_stamp;
1437 /* make sure this delta is calculated here */
1440 /* Did the write stamp get updated already? */
1441 if (unlikely(ts < cpu_buffer->write_stamp))
1444 else if (unlikely(test_time_stamp(delta))) {
1446 commit = rb_add_time_stamp(cpu_buffer, &ts, &delta);
1448 if (commit == -EBUSY)
1451 if (commit == -EAGAIN)
1454 RB_WARN_ON(cpu_buffer, commit < 0);
1457 /* Non commits have zero deltas */
1460 event = __rb_reserve_next(cpu_buffer, 0, length, &ts);
1461 if (PTR_ERR(event) == -EAGAIN)
1465 if (unlikely(commit))
1467 * Ouch! We needed a timestamp and it was commited. But
1468 * we didn't get our event reserved.
1470 rb_set_commit_to_write(cpu_buffer);
1475 * If the timestamp was commited, make the commit our entry
1476 * now so that we will update it when needed.
1478 if (unlikely(commit))
1479 rb_set_commit_event(cpu_buffer, event);
1480 else if (!rb_is_commit(cpu_buffer, event))
1483 event->time_delta = delta;
1488 #define TRACE_RECURSIVE_DEPTH 16
1490 static int trace_recursive_lock(void)
1492 current->trace_recursion++;
1494 if (likely(current->trace_recursion < TRACE_RECURSIVE_DEPTH))
1497 /* Disable all tracing before we do anything else */
1498 tracing_off_permanent();
1500 printk_once(KERN_WARNING "Tracing recursion: depth[%ld]:"
1501 "HC[%lu]:SC[%lu]:NMI[%lu]\n",
1502 current->trace_recursion,
1503 hardirq_count() >> HARDIRQ_SHIFT,
1504 softirq_count() >> SOFTIRQ_SHIFT,
1511 static void trace_recursive_unlock(void)
1513 WARN_ON_ONCE(!current->trace_recursion);
1515 current->trace_recursion--;
1518 static DEFINE_PER_CPU(int, rb_need_resched);
1521 * ring_buffer_lock_reserve - reserve a part of the buffer
1522 * @buffer: the ring buffer to reserve from
1523 * @length: the length of the data to reserve (excluding event header)
1525 * Returns a reseverd event on the ring buffer to copy directly to.
1526 * The user of this interface will need to get the body to write into
1527 * and can use the ring_buffer_event_data() interface.
1529 * The length is the length of the data needed, not the event length
1530 * which also includes the event header.
1532 * Must be paired with ring_buffer_unlock_commit, unless NULL is returned.
1533 * If NULL is returned, then nothing has been allocated or locked.
1535 struct ring_buffer_event *
1536 ring_buffer_lock_reserve(struct ring_buffer *buffer, unsigned long length)
1538 struct ring_buffer_per_cpu *cpu_buffer;
1539 struct ring_buffer_event *event;
1542 if (ring_buffer_flags != RB_BUFFERS_ON)
1545 if (atomic_read(&buffer->record_disabled))
1548 /* If we are tracing schedule, we don't want to recurse */
1549 resched = ftrace_preempt_disable();
1551 if (trace_recursive_lock())
1554 cpu = raw_smp_processor_id();
1556 if (!cpumask_test_cpu(cpu, buffer->cpumask))
1559 cpu_buffer = buffer->buffers[cpu];
1561 if (atomic_read(&cpu_buffer->record_disabled))
1564 if (length > BUF_MAX_DATA_SIZE)
1567 event = rb_reserve_next_event(cpu_buffer, length);
1572 * Need to store resched state on this cpu.
1573 * Only the first needs to.
1576 if (preempt_count() == 1)
1577 per_cpu(rb_need_resched, cpu) = resched;
1582 trace_recursive_unlock();
1585 ftrace_preempt_enable(resched);
1588 EXPORT_SYMBOL_GPL(ring_buffer_lock_reserve);
1590 static void rb_commit(struct ring_buffer_per_cpu *cpu_buffer,
1591 struct ring_buffer_event *event)
1593 local_inc(&cpu_buffer->entries);
1595 /* Only process further if we own the commit */
1596 if (!rb_is_commit(cpu_buffer, event))
1599 cpu_buffer->write_stamp += event->time_delta;
1601 rb_set_commit_to_write(cpu_buffer);
1605 * ring_buffer_unlock_commit - commit a reserved
1606 * @buffer: The buffer to commit to
1607 * @event: The event pointer to commit.
1609 * This commits the data to the ring buffer, and releases any locks held.
1611 * Must be paired with ring_buffer_lock_reserve.
1613 int ring_buffer_unlock_commit(struct ring_buffer *buffer,
1614 struct ring_buffer_event *event)
1616 struct ring_buffer_per_cpu *cpu_buffer;
1617 int cpu = raw_smp_processor_id();
1619 cpu_buffer = buffer->buffers[cpu];
1621 rb_commit(cpu_buffer, event);
1623 trace_recursive_unlock();
1626 * Only the last preempt count needs to restore preemption.
1628 if (preempt_count() == 1)
1629 ftrace_preempt_enable(per_cpu(rb_need_resched, cpu));
1631 preempt_enable_no_resched_notrace();
1635 EXPORT_SYMBOL_GPL(ring_buffer_unlock_commit);
1637 static inline void rb_event_discard(struct ring_buffer_event *event)
1639 /* array[0] holds the actual length for the discarded event */
1640 event->array[0] = rb_event_data_length(event) - RB_EVNT_HDR_SIZE;
1641 event->type_len = RINGBUF_TYPE_PADDING;
1642 /* time delta must be non zero */
1643 if (!event->time_delta)
1644 event->time_delta = 1;
1648 * ring_buffer_event_discard - discard any event in the ring buffer
1649 * @event: the event to discard
1651 * Sometimes a event that is in the ring buffer needs to be ignored.
1652 * This function lets the user discard an event in the ring buffer
1653 * and then that event will not be read later.
1655 * Note, it is up to the user to be careful with this, and protect
1656 * against races. If the user discards an event that has been consumed
1657 * it is possible that it could corrupt the ring buffer.
1659 void ring_buffer_event_discard(struct ring_buffer_event *event)
1661 rb_event_discard(event);
1663 EXPORT_SYMBOL_GPL(ring_buffer_event_discard);
1666 * ring_buffer_commit_discard - discard an event that has not been committed
1667 * @buffer: the ring buffer
1668 * @event: non committed event to discard
1670 * This is similar to ring_buffer_event_discard but must only be
1671 * performed on an event that has not been committed yet. The difference
1672 * is that this will also try to free the event from the ring buffer
1673 * if another event has not been added behind it.
1675 * If another event has been added behind it, it will set the event
1676 * up as discarded, and perform the commit.
1678 * If this function is called, do not call ring_buffer_unlock_commit on
1681 void ring_buffer_discard_commit(struct ring_buffer *buffer,
1682 struct ring_buffer_event *event)
1684 struct ring_buffer_per_cpu *cpu_buffer;
1685 unsigned long new_index, old_index;
1686 struct buffer_page *bpage;
1687 unsigned long index;
1691 /* The event is discarded regardless */
1692 rb_event_discard(event);
1695 * This must only be called if the event has not been
1696 * committed yet. Thus we can assume that preemption
1697 * is still disabled.
1699 RB_WARN_ON(buffer, preemptible());
1701 cpu = smp_processor_id();
1702 cpu_buffer = buffer->buffers[cpu];
1704 new_index = rb_event_index(event);
1705 old_index = new_index + rb_event_length(event);
1706 addr = (unsigned long)event;
1709 bpage = cpu_buffer->tail_page;
1711 if (bpage == (void *)addr && rb_page_write(bpage) == old_index) {
1713 * This is on the tail page. It is possible that
1714 * a write could come in and move the tail page
1715 * and write to the next page. That is fine
1716 * because we just shorten what is on this page.
1718 index = local_cmpxchg(&bpage->write, old_index, new_index);
1719 if (index == old_index)
1724 * The commit is still visible by the reader, so we
1725 * must increment entries.
1727 local_inc(&cpu_buffer->entries);
1730 * If a write came in and pushed the tail page
1731 * we still need to update the commit pointer
1732 * if we were the commit.
1734 if (rb_is_commit(cpu_buffer, event))
1735 rb_set_commit_to_write(cpu_buffer);
1737 trace_recursive_unlock();
1740 * Only the last preempt count needs to restore preemption.
1742 if (preempt_count() == 1)
1743 ftrace_preempt_enable(per_cpu(rb_need_resched, cpu));
1745 preempt_enable_no_resched_notrace();
1748 EXPORT_SYMBOL_GPL(ring_buffer_discard_commit);
1751 * ring_buffer_write - write data to the buffer without reserving
1752 * @buffer: The ring buffer to write to.
1753 * @length: The length of the data being written (excluding the event header)
1754 * @data: The data to write to the buffer.
1756 * This is like ring_buffer_lock_reserve and ring_buffer_unlock_commit as
1757 * one function. If you already have the data to write to the buffer, it
1758 * may be easier to simply call this function.
1760 * Note, like ring_buffer_lock_reserve, the length is the length of the data
1761 * and not the length of the event which would hold the header.
1763 int ring_buffer_write(struct ring_buffer *buffer,
1764 unsigned long length,
1767 struct ring_buffer_per_cpu *cpu_buffer;
1768 struct ring_buffer_event *event;
1773 if (ring_buffer_flags != RB_BUFFERS_ON)
1776 if (atomic_read(&buffer->record_disabled))
1779 resched = ftrace_preempt_disable();
1781 cpu = raw_smp_processor_id();
1783 if (!cpumask_test_cpu(cpu, buffer->cpumask))
1786 cpu_buffer = buffer->buffers[cpu];
1788 if (atomic_read(&cpu_buffer->record_disabled))
1791 if (length > BUF_MAX_DATA_SIZE)
1794 event = rb_reserve_next_event(cpu_buffer, length);
1798 body = rb_event_data(event);
1800 memcpy(body, data, length);
1802 rb_commit(cpu_buffer, event);
1806 ftrace_preempt_enable(resched);
1810 EXPORT_SYMBOL_GPL(ring_buffer_write);
1812 static int rb_per_cpu_empty(struct ring_buffer_per_cpu *cpu_buffer)
1814 struct buffer_page *reader = cpu_buffer->reader_page;
1815 struct buffer_page *head = cpu_buffer->head_page;
1816 struct buffer_page *commit = cpu_buffer->commit_page;
1818 return reader->read == rb_page_commit(reader) &&
1819 (commit == reader ||
1821 head->read == rb_page_commit(commit)));
1825 * ring_buffer_record_disable - stop all writes into the buffer
1826 * @buffer: The ring buffer to stop writes to.
1828 * This prevents all writes to the buffer. Any attempt to write
1829 * to the buffer after this will fail and return NULL.
1831 * The caller should call synchronize_sched() after this.
1833 void ring_buffer_record_disable(struct ring_buffer *buffer)
1835 atomic_inc(&buffer->record_disabled);
1837 EXPORT_SYMBOL_GPL(ring_buffer_record_disable);
1840 * ring_buffer_record_enable - enable writes to the buffer
1841 * @buffer: The ring buffer to enable writes
1843 * Note, multiple disables will need the same number of enables
1844 * to truely enable the writing (much like preempt_disable).
1846 void ring_buffer_record_enable(struct ring_buffer *buffer)
1848 atomic_dec(&buffer->record_disabled);
1850 EXPORT_SYMBOL_GPL(ring_buffer_record_enable);
1853 * ring_buffer_record_disable_cpu - stop all writes into the cpu_buffer
1854 * @buffer: The ring buffer to stop writes to.
1855 * @cpu: The CPU buffer to stop
1857 * This prevents all writes to the buffer. Any attempt to write
1858 * to the buffer after this will fail and return NULL.
1860 * The caller should call synchronize_sched() after this.
1862 void ring_buffer_record_disable_cpu(struct ring_buffer *buffer, int cpu)
1864 struct ring_buffer_per_cpu *cpu_buffer;
1866 if (!cpumask_test_cpu(cpu, buffer->cpumask))
1869 cpu_buffer = buffer->buffers[cpu];
1870 atomic_inc(&cpu_buffer->record_disabled);
1872 EXPORT_SYMBOL_GPL(ring_buffer_record_disable_cpu);
1875 * ring_buffer_record_enable_cpu - enable writes to the buffer
1876 * @buffer: The ring buffer to enable writes
1877 * @cpu: The CPU to enable.
1879 * Note, multiple disables will need the same number of enables
1880 * to truely enable the writing (much like preempt_disable).
1882 void ring_buffer_record_enable_cpu(struct ring_buffer *buffer, int cpu)
1884 struct ring_buffer_per_cpu *cpu_buffer;
1886 if (!cpumask_test_cpu(cpu, buffer->cpumask))
1889 cpu_buffer = buffer->buffers[cpu];
1890 atomic_dec(&cpu_buffer->record_disabled);
1892 EXPORT_SYMBOL_GPL(ring_buffer_record_enable_cpu);
1895 * ring_buffer_entries_cpu - get the number of entries in a cpu buffer
1896 * @buffer: The ring buffer
1897 * @cpu: The per CPU buffer to get the entries from.
1899 unsigned long ring_buffer_entries_cpu(struct ring_buffer *buffer, int cpu)
1901 struct ring_buffer_per_cpu *cpu_buffer;
1904 if (!cpumask_test_cpu(cpu, buffer->cpumask))
1907 cpu_buffer = buffer->buffers[cpu];
1908 ret = (local_read(&cpu_buffer->entries) - cpu_buffer->overrun)
1913 EXPORT_SYMBOL_GPL(ring_buffer_entries_cpu);
1916 * ring_buffer_overrun_cpu - get the number of overruns in a cpu_buffer
1917 * @buffer: The ring buffer
1918 * @cpu: The per CPU buffer to get the number of overruns from
1920 unsigned long ring_buffer_overrun_cpu(struct ring_buffer *buffer, int cpu)
1922 struct ring_buffer_per_cpu *cpu_buffer;
1925 if (!cpumask_test_cpu(cpu, buffer->cpumask))
1928 cpu_buffer = buffer->buffers[cpu];
1929 ret = cpu_buffer->overrun;
1933 EXPORT_SYMBOL_GPL(ring_buffer_overrun_cpu);
1936 * ring_buffer_nmi_dropped_cpu - get the number of nmis that were dropped
1937 * @buffer: The ring buffer
1938 * @cpu: The per CPU buffer to get the number of overruns from
1940 unsigned long ring_buffer_nmi_dropped_cpu(struct ring_buffer *buffer, int cpu)
1942 struct ring_buffer_per_cpu *cpu_buffer;
1945 if (!cpumask_test_cpu(cpu, buffer->cpumask))
1948 cpu_buffer = buffer->buffers[cpu];
1949 ret = cpu_buffer->nmi_dropped;
1953 EXPORT_SYMBOL_GPL(ring_buffer_nmi_dropped_cpu);
1956 * ring_buffer_commit_overrun_cpu - get the number of overruns caused by commits
1957 * @buffer: The ring buffer
1958 * @cpu: The per CPU buffer to get the number of overruns from
1961 ring_buffer_commit_overrun_cpu(struct ring_buffer *buffer, int cpu)
1963 struct ring_buffer_per_cpu *cpu_buffer;
1966 if (!cpumask_test_cpu(cpu, buffer->cpumask))
1969 cpu_buffer = buffer->buffers[cpu];
1970 ret = cpu_buffer->commit_overrun;
1974 EXPORT_SYMBOL_GPL(ring_buffer_commit_overrun_cpu);
1977 * ring_buffer_entries - get the number of entries in a buffer
1978 * @buffer: The ring buffer
1980 * Returns the total number of entries in the ring buffer
1983 unsigned long ring_buffer_entries(struct ring_buffer *buffer)
1985 struct ring_buffer_per_cpu *cpu_buffer;
1986 unsigned long entries = 0;
1989 /* if you care about this being correct, lock the buffer */
1990 for_each_buffer_cpu(buffer, cpu) {
1991 cpu_buffer = buffer->buffers[cpu];
1992 entries += (local_read(&cpu_buffer->entries) -
1993 cpu_buffer->overrun) - cpu_buffer->read;
1998 EXPORT_SYMBOL_GPL(ring_buffer_entries);
2001 * ring_buffer_overrun_cpu - get the number of overruns in buffer
2002 * @buffer: The ring buffer
2004 * Returns the total number of overruns in the ring buffer
2007 unsigned long ring_buffer_overruns(struct ring_buffer *buffer)
2009 struct ring_buffer_per_cpu *cpu_buffer;
2010 unsigned long overruns = 0;
2013 /* if you care about this being correct, lock the buffer */
2014 for_each_buffer_cpu(buffer, cpu) {
2015 cpu_buffer = buffer->buffers[cpu];
2016 overruns += cpu_buffer->overrun;
2021 EXPORT_SYMBOL_GPL(ring_buffer_overruns);
2023 static void rb_iter_reset(struct ring_buffer_iter *iter)
2025 struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
2027 /* Iterator usage is expected to have record disabled */
2028 if (list_empty(&cpu_buffer->reader_page->list)) {
2029 iter->head_page = cpu_buffer->head_page;
2030 iter->head = cpu_buffer->head_page->read;
2032 iter->head_page = cpu_buffer->reader_page;
2033 iter->head = cpu_buffer->reader_page->read;
2036 iter->read_stamp = cpu_buffer->read_stamp;
2038 iter->read_stamp = iter->head_page->page->time_stamp;
2042 * ring_buffer_iter_reset - reset an iterator
2043 * @iter: The iterator to reset
2045 * Resets the iterator, so that it will start from the beginning
2048 void ring_buffer_iter_reset(struct ring_buffer_iter *iter)
2050 struct ring_buffer_per_cpu *cpu_buffer;
2051 unsigned long flags;
2056 cpu_buffer = iter->cpu_buffer;
2058 spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
2059 rb_iter_reset(iter);
2060 spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
2062 EXPORT_SYMBOL_GPL(ring_buffer_iter_reset);
2065 * ring_buffer_iter_empty - check if an iterator has no more to read
2066 * @iter: The iterator to check
2068 int ring_buffer_iter_empty(struct ring_buffer_iter *iter)
2070 struct ring_buffer_per_cpu *cpu_buffer;
2072 cpu_buffer = iter->cpu_buffer;
2074 return iter->head_page == cpu_buffer->commit_page &&
2075 iter->head == rb_commit_index(cpu_buffer);
2077 EXPORT_SYMBOL_GPL(ring_buffer_iter_empty);
2080 rb_update_read_stamp(struct ring_buffer_per_cpu *cpu_buffer,
2081 struct ring_buffer_event *event)
2085 switch (event->type_len) {
2086 case RINGBUF_TYPE_PADDING:
2089 case RINGBUF_TYPE_TIME_EXTEND:
2090 delta = event->array[0];
2092 delta += event->time_delta;
2093 cpu_buffer->read_stamp += delta;
2096 case RINGBUF_TYPE_TIME_STAMP:
2097 /* FIXME: not implemented */
2100 case RINGBUF_TYPE_DATA:
2101 cpu_buffer->read_stamp += event->time_delta;
2111 rb_update_iter_read_stamp(struct ring_buffer_iter *iter,
2112 struct ring_buffer_event *event)
2116 switch (event->type_len) {
2117 case RINGBUF_TYPE_PADDING:
2120 case RINGBUF_TYPE_TIME_EXTEND:
2121 delta = event->array[0];
2123 delta += event->time_delta;
2124 iter->read_stamp += delta;
2127 case RINGBUF_TYPE_TIME_STAMP:
2128 /* FIXME: not implemented */
2131 case RINGBUF_TYPE_DATA:
2132 iter->read_stamp += event->time_delta;
2141 static struct buffer_page *
2142 rb_get_reader_page(struct ring_buffer_per_cpu *cpu_buffer)
2144 struct buffer_page *reader = NULL;
2145 unsigned long flags;
2148 local_irq_save(flags);
2149 __raw_spin_lock(&cpu_buffer->lock);
2153 * This should normally only loop twice. But because the
2154 * start of the reader inserts an empty page, it causes
2155 * a case where we will loop three times. There should be no
2156 * reason to loop four times (that I know of).
2158 if (RB_WARN_ON(cpu_buffer, ++nr_loops > 3)) {
2163 reader = cpu_buffer->reader_page;
2165 /* If there's more to read, return this page */
2166 if (cpu_buffer->reader_page->read < rb_page_size(reader))
2169 /* Never should we have an index greater than the size */
2170 if (RB_WARN_ON(cpu_buffer,
2171 cpu_buffer->reader_page->read > rb_page_size(reader)))
2174 /* check if we caught up to the tail */
2176 if (cpu_buffer->commit_page == cpu_buffer->reader_page)
2180 * Splice the empty reader page into the list around the head.
2181 * Reset the reader page to size zero.
2184 reader = cpu_buffer->head_page;
2185 cpu_buffer->reader_page->list.next = reader->list.next;
2186 cpu_buffer->reader_page->list.prev = reader->list.prev;
2188 local_set(&cpu_buffer->reader_page->write, 0);
2189 local_set(&cpu_buffer->reader_page->entries, 0);
2190 local_set(&cpu_buffer->reader_page->page->commit, 0);
2192 /* Make the reader page now replace the head */
2193 reader->list.prev->next = &cpu_buffer->reader_page->list;
2194 reader->list.next->prev = &cpu_buffer->reader_page->list;
2197 * If the tail is on the reader, then we must set the head
2198 * to the inserted page, otherwise we set it one before.
2200 cpu_buffer->head_page = cpu_buffer->reader_page;
2202 if (cpu_buffer->commit_page != reader)
2203 rb_inc_page(cpu_buffer, &cpu_buffer->head_page);
2205 /* Finally update the reader page to the new head */
2206 cpu_buffer->reader_page = reader;
2207 rb_reset_reader_page(cpu_buffer);
2212 __raw_spin_unlock(&cpu_buffer->lock);
2213 local_irq_restore(flags);
2218 static void rb_advance_reader(struct ring_buffer_per_cpu *cpu_buffer)
2220 struct ring_buffer_event *event;
2221 struct buffer_page *reader;
2224 reader = rb_get_reader_page(cpu_buffer);
2226 /* This function should not be called when buffer is empty */
2227 if (RB_WARN_ON(cpu_buffer, !reader))
2230 event = rb_reader_event(cpu_buffer);
2232 if (event->type_len <= RINGBUF_TYPE_DATA_TYPE_LEN_MAX
2233 || rb_discarded_event(event))
2236 rb_update_read_stamp(cpu_buffer, event);
2238 length = rb_event_length(event);
2239 cpu_buffer->reader_page->read += length;
2242 static void rb_advance_iter(struct ring_buffer_iter *iter)
2244 struct ring_buffer *buffer;
2245 struct ring_buffer_per_cpu *cpu_buffer;
2246 struct ring_buffer_event *event;
2249 cpu_buffer = iter->cpu_buffer;
2250 buffer = cpu_buffer->buffer;
2253 * Check if we are at the end of the buffer.
2255 if (iter->head >= rb_page_size(iter->head_page)) {
2256 if (RB_WARN_ON(buffer,
2257 iter->head_page == cpu_buffer->commit_page))
2263 event = rb_iter_head_event(iter);
2265 length = rb_event_length(event);
2268 * This should not be called to advance the header if we are
2269 * at the tail of the buffer.
2271 if (RB_WARN_ON(cpu_buffer,
2272 (iter->head_page == cpu_buffer->commit_page) &&
2273 (iter->head + length > rb_commit_index(cpu_buffer))))
2276 rb_update_iter_read_stamp(iter, event);
2278 iter->head += length;
2280 /* check for end of page padding */
2281 if ((iter->head >= rb_page_size(iter->head_page)) &&
2282 (iter->head_page != cpu_buffer->commit_page))
2283 rb_advance_iter(iter);
2286 static struct ring_buffer_event *
2287 rb_buffer_peek(struct ring_buffer *buffer, int cpu, u64 *ts)
2289 struct ring_buffer_per_cpu *cpu_buffer;
2290 struct ring_buffer_event *event;
2291 struct buffer_page *reader;
2294 cpu_buffer = buffer->buffers[cpu];
2298 * We repeat when a timestamp is encountered. It is possible
2299 * to get multiple timestamps from an interrupt entering just
2300 * as one timestamp is about to be written. The max times
2301 * that this can happen is the number of nested interrupts we
2302 * can have. Nesting 10 deep of interrupts is clearly
2305 if (RB_WARN_ON(cpu_buffer, ++nr_loops > 10))
2308 reader = rb_get_reader_page(cpu_buffer);
2312 event = rb_reader_event(cpu_buffer);
2314 switch (event->type_len) {
2315 case RINGBUF_TYPE_PADDING:
2316 if (rb_null_event(event))
2317 RB_WARN_ON(cpu_buffer, 1);
2319 * Because the writer could be discarding every
2320 * event it creates (which would probably be bad)
2321 * if we were to go back to "again" then we may never
2322 * catch up, and will trigger the warn on, or lock
2323 * the box. Return the padding, and we will release
2324 * the current locks, and try again.
2326 rb_advance_reader(cpu_buffer);
2329 case RINGBUF_TYPE_TIME_EXTEND:
2330 /* Internal data, OK to advance */
2331 rb_advance_reader(cpu_buffer);
2334 case RINGBUF_TYPE_TIME_STAMP:
2335 /* FIXME: not implemented */
2336 rb_advance_reader(cpu_buffer);
2339 case RINGBUF_TYPE_DATA:
2341 *ts = cpu_buffer->read_stamp + event->time_delta;
2342 ring_buffer_normalize_time_stamp(buffer,
2343 cpu_buffer->cpu, ts);
2353 EXPORT_SYMBOL_GPL(ring_buffer_peek);
2355 static struct ring_buffer_event *
2356 rb_iter_peek(struct ring_buffer_iter *iter, u64 *ts)
2358 struct ring_buffer *buffer;
2359 struct ring_buffer_per_cpu *cpu_buffer;
2360 struct ring_buffer_event *event;
2363 if (ring_buffer_iter_empty(iter))
2366 cpu_buffer = iter->cpu_buffer;
2367 buffer = cpu_buffer->buffer;
2371 * We repeat when a timestamp is encountered. It is possible
2372 * to get multiple timestamps from an interrupt entering just
2373 * as one timestamp is about to be written. The max times
2374 * that this can happen is the number of nested interrupts we
2375 * can have. Nesting 10 deep of interrupts is clearly
2378 if (RB_WARN_ON(cpu_buffer, ++nr_loops > 10))
2381 if (rb_per_cpu_empty(cpu_buffer))
2384 event = rb_iter_head_event(iter);
2386 switch (event->type_len) {
2387 case RINGBUF_TYPE_PADDING:
2388 if (rb_null_event(event)) {
2392 rb_advance_iter(iter);
2395 case RINGBUF_TYPE_TIME_EXTEND:
2396 /* Internal data, OK to advance */
2397 rb_advance_iter(iter);
2400 case RINGBUF_TYPE_TIME_STAMP:
2401 /* FIXME: not implemented */
2402 rb_advance_iter(iter);
2405 case RINGBUF_TYPE_DATA:
2407 *ts = iter->read_stamp + event->time_delta;
2408 ring_buffer_normalize_time_stamp(buffer,
2409 cpu_buffer->cpu, ts);
2419 EXPORT_SYMBOL_GPL(ring_buffer_iter_peek);
2422 * ring_buffer_peek - peek at the next event to be read
2423 * @buffer: The ring buffer to read
2424 * @cpu: The cpu to peak at
2425 * @ts: The timestamp counter of this event.
2427 * This will return the event that will be read next, but does
2428 * not consume the data.
2430 struct ring_buffer_event *
2431 ring_buffer_peek(struct ring_buffer *buffer, int cpu, u64 *ts)
2433 struct ring_buffer_per_cpu *cpu_buffer = buffer->buffers[cpu];
2434 struct ring_buffer_event *event;
2435 unsigned long flags;
2437 if (!cpumask_test_cpu(cpu, buffer->cpumask))
2441 spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
2442 event = rb_buffer_peek(buffer, cpu, ts);
2443 spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
2445 if (event && event->type_len == RINGBUF_TYPE_PADDING) {
2454 * ring_buffer_iter_peek - peek at the next event to be read
2455 * @iter: The ring buffer iterator
2456 * @ts: The timestamp counter of this event.
2458 * This will return the event that will be read next, but does
2459 * not increment the iterator.
2461 struct ring_buffer_event *
2462 ring_buffer_iter_peek(struct ring_buffer_iter *iter, u64 *ts)
2464 struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
2465 struct ring_buffer_event *event;
2466 unsigned long flags;
2469 spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
2470 event = rb_iter_peek(iter, ts);
2471 spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
2473 if (event && event->type_len == RINGBUF_TYPE_PADDING) {
2482 * ring_buffer_consume - return an event and consume it
2483 * @buffer: The ring buffer to get the next event from
2485 * Returns the next event in the ring buffer, and that event is consumed.
2486 * Meaning, that sequential reads will keep returning a different event,
2487 * and eventually empty the ring buffer if the producer is slower.
2489 struct ring_buffer_event *
2490 ring_buffer_consume(struct ring_buffer *buffer, int cpu, u64 *ts)
2492 struct ring_buffer_per_cpu *cpu_buffer;
2493 struct ring_buffer_event *event = NULL;
2494 unsigned long flags;
2497 /* might be called in atomic */
2500 if (!cpumask_test_cpu(cpu, buffer->cpumask))
2503 cpu_buffer = buffer->buffers[cpu];
2504 spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
2506 event = rb_buffer_peek(buffer, cpu, ts);
2510 rb_advance_reader(cpu_buffer);
2513 spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
2518 if (event && event->type_len == RINGBUF_TYPE_PADDING) {
2525 EXPORT_SYMBOL_GPL(ring_buffer_consume);
2528 * ring_buffer_read_start - start a non consuming read of the buffer
2529 * @buffer: The ring buffer to read from
2530 * @cpu: The cpu buffer to iterate over
2532 * This starts up an iteration through the buffer. It also disables
2533 * the recording to the buffer until the reading is finished.
2534 * This prevents the reading from being corrupted. This is not
2535 * a consuming read, so a producer is not expected.
2537 * Must be paired with ring_buffer_finish.
2539 struct ring_buffer_iter *
2540 ring_buffer_read_start(struct ring_buffer *buffer, int cpu)
2542 struct ring_buffer_per_cpu *cpu_buffer;
2543 struct ring_buffer_iter *iter;
2544 unsigned long flags;
2546 if (!cpumask_test_cpu(cpu, buffer->cpumask))
2549 iter = kmalloc(sizeof(*iter), GFP_KERNEL);
2553 cpu_buffer = buffer->buffers[cpu];
2555 iter->cpu_buffer = cpu_buffer;
2557 atomic_inc(&cpu_buffer->record_disabled);
2558 synchronize_sched();
2560 spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
2561 __raw_spin_lock(&cpu_buffer->lock);
2562 rb_iter_reset(iter);
2563 __raw_spin_unlock(&cpu_buffer->lock);
2564 spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
2568 EXPORT_SYMBOL_GPL(ring_buffer_read_start);
2571 * ring_buffer_finish - finish reading the iterator of the buffer
2572 * @iter: The iterator retrieved by ring_buffer_start
2574 * This re-enables the recording to the buffer, and frees the
2578 ring_buffer_read_finish(struct ring_buffer_iter *iter)
2580 struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
2582 atomic_dec(&cpu_buffer->record_disabled);
2585 EXPORT_SYMBOL_GPL(ring_buffer_read_finish);
2588 * ring_buffer_read - read the next item in the ring buffer by the iterator
2589 * @iter: The ring buffer iterator
2590 * @ts: The time stamp of the event read.
2592 * This reads the next event in the ring buffer and increments the iterator.
2594 struct ring_buffer_event *
2595 ring_buffer_read(struct ring_buffer_iter *iter, u64 *ts)
2597 struct ring_buffer_event *event;
2598 struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
2599 unsigned long flags;
2602 spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
2603 event = rb_iter_peek(iter, ts);
2607 rb_advance_iter(iter);
2609 spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
2611 if (event && event->type_len == RINGBUF_TYPE_PADDING) {
2618 EXPORT_SYMBOL_GPL(ring_buffer_read);
2621 * ring_buffer_size - return the size of the ring buffer (in bytes)
2622 * @buffer: The ring buffer.
2624 unsigned long ring_buffer_size(struct ring_buffer *buffer)
2626 return BUF_PAGE_SIZE * buffer->pages;
2628 EXPORT_SYMBOL_GPL(ring_buffer_size);
2631 rb_reset_cpu(struct ring_buffer_per_cpu *cpu_buffer)
2633 cpu_buffer->head_page
2634 = list_entry(cpu_buffer->pages.next, struct buffer_page, list);
2635 local_set(&cpu_buffer->head_page->write, 0);
2636 local_set(&cpu_buffer->head_page->entries, 0);
2637 local_set(&cpu_buffer->head_page->page->commit, 0);
2639 cpu_buffer->head_page->read = 0;
2641 cpu_buffer->tail_page = cpu_buffer->head_page;
2642 cpu_buffer->commit_page = cpu_buffer->head_page;
2644 INIT_LIST_HEAD(&cpu_buffer->reader_page->list);
2645 local_set(&cpu_buffer->reader_page->write, 0);
2646 local_set(&cpu_buffer->reader_page->entries, 0);
2647 local_set(&cpu_buffer->reader_page->page->commit, 0);
2648 cpu_buffer->reader_page->read = 0;
2650 cpu_buffer->nmi_dropped = 0;
2651 cpu_buffer->commit_overrun = 0;
2652 cpu_buffer->overrun = 0;
2653 cpu_buffer->read = 0;
2654 local_set(&cpu_buffer->entries, 0);
2656 cpu_buffer->write_stamp = 0;
2657 cpu_buffer->read_stamp = 0;
2661 * ring_buffer_reset_cpu - reset a ring buffer per CPU buffer
2662 * @buffer: The ring buffer to reset a per cpu buffer of
2663 * @cpu: The CPU buffer to be reset
2665 void ring_buffer_reset_cpu(struct ring_buffer *buffer, int cpu)
2667 struct ring_buffer_per_cpu *cpu_buffer = buffer->buffers[cpu];
2668 unsigned long flags;
2670 if (!cpumask_test_cpu(cpu, buffer->cpumask))
2673 atomic_inc(&cpu_buffer->record_disabled);
2675 spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
2677 __raw_spin_lock(&cpu_buffer->lock);
2679 rb_reset_cpu(cpu_buffer);
2681 __raw_spin_unlock(&cpu_buffer->lock);
2683 spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
2685 atomic_dec(&cpu_buffer->record_disabled);
2687 EXPORT_SYMBOL_GPL(ring_buffer_reset_cpu);
2690 * ring_buffer_reset - reset a ring buffer
2691 * @buffer: The ring buffer to reset all cpu buffers
2693 void ring_buffer_reset(struct ring_buffer *buffer)
2697 for_each_buffer_cpu(buffer, cpu)
2698 ring_buffer_reset_cpu(buffer, cpu);
2700 EXPORT_SYMBOL_GPL(ring_buffer_reset);
2703 * rind_buffer_empty - is the ring buffer empty?
2704 * @buffer: The ring buffer to test
2706 int ring_buffer_empty(struct ring_buffer *buffer)
2708 struct ring_buffer_per_cpu *cpu_buffer;
2711 /* yes this is racy, but if you don't like the race, lock the buffer */
2712 for_each_buffer_cpu(buffer, cpu) {
2713 cpu_buffer = buffer->buffers[cpu];
2714 if (!rb_per_cpu_empty(cpu_buffer))
2720 EXPORT_SYMBOL_GPL(ring_buffer_empty);
2723 * ring_buffer_empty_cpu - is a cpu buffer of a ring buffer empty?
2724 * @buffer: The ring buffer
2725 * @cpu: The CPU buffer to test
2727 int ring_buffer_empty_cpu(struct ring_buffer *buffer, int cpu)
2729 struct ring_buffer_per_cpu *cpu_buffer;
2732 if (!cpumask_test_cpu(cpu, buffer->cpumask))
2735 cpu_buffer = buffer->buffers[cpu];
2736 ret = rb_per_cpu_empty(cpu_buffer);
2741 EXPORT_SYMBOL_GPL(ring_buffer_empty_cpu);
2744 * ring_buffer_swap_cpu - swap a CPU buffer between two ring buffers
2745 * @buffer_a: One buffer to swap with
2746 * @buffer_b: The other buffer to swap with
2748 * This function is useful for tracers that want to take a "snapshot"
2749 * of a CPU buffer and has another back up buffer lying around.
2750 * it is expected that the tracer handles the cpu buffer not being
2751 * used at the moment.
2753 int ring_buffer_swap_cpu(struct ring_buffer *buffer_a,
2754 struct ring_buffer *buffer_b, int cpu)
2756 struct ring_buffer_per_cpu *cpu_buffer_a;
2757 struct ring_buffer_per_cpu *cpu_buffer_b;
2760 if (!cpumask_test_cpu(cpu, buffer_a->cpumask) ||
2761 !cpumask_test_cpu(cpu, buffer_b->cpumask))
2764 /* At least make sure the two buffers are somewhat the same */
2765 if (buffer_a->pages != buffer_b->pages)
2770 if (ring_buffer_flags != RB_BUFFERS_ON)
2773 if (atomic_read(&buffer_a->record_disabled))
2776 if (atomic_read(&buffer_b->record_disabled))
2779 cpu_buffer_a = buffer_a->buffers[cpu];
2780 cpu_buffer_b = buffer_b->buffers[cpu];
2782 if (atomic_read(&cpu_buffer_a->record_disabled))
2785 if (atomic_read(&cpu_buffer_b->record_disabled))
2789 * We can't do a synchronize_sched here because this
2790 * function can be called in atomic context.
2791 * Normally this will be called from the same CPU as cpu.
2792 * If not it's up to the caller to protect this.
2794 atomic_inc(&cpu_buffer_a->record_disabled);
2795 atomic_inc(&cpu_buffer_b->record_disabled);
2797 buffer_a->buffers[cpu] = cpu_buffer_b;
2798 buffer_b->buffers[cpu] = cpu_buffer_a;
2800 cpu_buffer_b->buffer = buffer_a;
2801 cpu_buffer_a->buffer = buffer_b;
2803 atomic_dec(&cpu_buffer_a->record_disabled);
2804 atomic_dec(&cpu_buffer_b->record_disabled);
2810 EXPORT_SYMBOL_GPL(ring_buffer_swap_cpu);
2813 * ring_buffer_alloc_read_page - allocate a page to read from buffer
2814 * @buffer: the buffer to allocate for.
2816 * This function is used in conjunction with ring_buffer_read_page.
2817 * When reading a full page from the ring buffer, these functions
2818 * can be used to speed up the process. The calling function should
2819 * allocate a few pages first with this function. Then when it
2820 * needs to get pages from the ring buffer, it passes the result
2821 * of this function into ring_buffer_read_page, which will swap
2822 * the page that was allocated, with the read page of the buffer.
2825 * The page allocated, or NULL on error.
2827 void *ring_buffer_alloc_read_page(struct ring_buffer *buffer)
2829 struct buffer_data_page *bpage;
2832 addr = __get_free_page(GFP_KERNEL);
2836 bpage = (void *)addr;
2838 rb_init_page(bpage);
2842 EXPORT_SYMBOL_GPL(ring_buffer_alloc_read_page);
2845 * ring_buffer_free_read_page - free an allocated read page
2846 * @buffer: the buffer the page was allocate for
2847 * @data: the page to free
2849 * Free a page allocated from ring_buffer_alloc_read_page.
2851 void ring_buffer_free_read_page(struct ring_buffer *buffer, void *data)
2853 free_page((unsigned long)data);
2855 EXPORT_SYMBOL_GPL(ring_buffer_free_read_page);
2858 * ring_buffer_read_page - extract a page from the ring buffer
2859 * @buffer: buffer to extract from
2860 * @data_page: the page to use allocated from ring_buffer_alloc_read_page
2861 * @len: amount to extract
2862 * @cpu: the cpu of the buffer to extract
2863 * @full: should the extraction only happen when the page is full.
2865 * This function will pull out a page from the ring buffer and consume it.
2866 * @data_page must be the address of the variable that was returned
2867 * from ring_buffer_alloc_read_page. This is because the page might be used
2868 * to swap with a page in the ring buffer.
2871 * rpage = ring_buffer_alloc_read_page(buffer);
2874 * ret = ring_buffer_read_page(buffer, &rpage, len, cpu, 0);
2876 * process_page(rpage, ret);
2878 * When @full is set, the function will not return true unless
2879 * the writer is off the reader page.
2881 * Note: it is up to the calling functions to handle sleeps and wakeups.
2882 * The ring buffer can be used anywhere in the kernel and can not
2883 * blindly call wake_up. The layer that uses the ring buffer must be
2884 * responsible for that.
2887 * >=0 if data has been transferred, returns the offset of consumed data.
2888 * <0 if no data has been transferred.
2890 int ring_buffer_read_page(struct ring_buffer *buffer,
2891 void **data_page, size_t len, int cpu, int full)
2893 struct ring_buffer_per_cpu *cpu_buffer = buffer->buffers[cpu];
2894 struct ring_buffer_event *event;
2895 struct buffer_data_page *bpage;
2896 struct buffer_page *reader;
2897 unsigned long flags;
2898 unsigned int commit;
2903 if (!cpumask_test_cpu(cpu, buffer->cpumask))
2907 * If len is not big enough to hold the page header, then
2908 * we can not copy anything.
2910 if (len <= BUF_PAGE_HDR_SIZE)
2913 len -= BUF_PAGE_HDR_SIZE;
2922 spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
2924 reader = rb_get_reader_page(cpu_buffer);
2928 event = rb_reader_event(cpu_buffer);
2930 read = reader->read;
2931 commit = rb_page_commit(reader);
2934 * If this page has been partially read or
2935 * if len is not big enough to read the rest of the page or
2936 * a writer is still on the page, then
2937 * we must copy the data from the page to the buffer.
2938 * Otherwise, we can simply swap the page with the one passed in.
2940 if (read || (len < (commit - read)) ||
2941 cpu_buffer->reader_page == cpu_buffer->commit_page) {
2942 struct buffer_data_page *rpage = cpu_buffer->reader_page->page;
2943 unsigned int rpos = read;
2944 unsigned int pos = 0;
2950 if (len > (commit - read))
2951 len = (commit - read);
2953 size = rb_event_length(event);
2958 /* save the current timestamp, since the user will need it */
2959 save_timestamp = cpu_buffer->read_stamp;
2961 /* Need to copy one event at a time */
2963 memcpy(bpage->data + pos, rpage->data + rpos, size);
2967 rb_advance_reader(cpu_buffer);
2968 rpos = reader->read;
2971 event = rb_reader_event(cpu_buffer);
2972 size = rb_event_length(event);
2973 } while (len > size);
2976 local_set(&bpage->commit, pos);
2977 bpage->time_stamp = save_timestamp;
2979 /* we copied everything to the beginning */
2982 /* update the entry counter */
2983 cpu_buffer->read += local_read(&reader->entries);
2985 /* swap the pages */
2986 rb_init_page(bpage);
2987 bpage = reader->page;
2988 reader->page = *data_page;
2989 local_set(&reader->write, 0);
2990 local_set(&reader->entries, 0);
2997 spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
3002 EXPORT_SYMBOL_GPL(ring_buffer_read_page);
3005 rb_simple_read(struct file *filp, char __user *ubuf,
3006 size_t cnt, loff_t *ppos)
3008 unsigned long *p = filp->private_data;
3012 if (test_bit(RB_BUFFERS_DISABLED_BIT, p))
3013 r = sprintf(buf, "permanently disabled\n");
3015 r = sprintf(buf, "%d\n", test_bit(RB_BUFFERS_ON_BIT, p));
3017 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
3021 rb_simple_write(struct file *filp, const char __user *ubuf,
3022 size_t cnt, loff_t *ppos)
3024 unsigned long *p = filp->private_data;
3029 if (cnt >= sizeof(buf))
3032 if (copy_from_user(&buf, ubuf, cnt))
3037 ret = strict_strtoul(buf, 10, &val);
3042 set_bit(RB_BUFFERS_ON_BIT, p);
3044 clear_bit(RB_BUFFERS_ON_BIT, p);
3051 static const struct file_operations rb_simple_fops = {
3052 .open = tracing_open_generic,
3053 .read = rb_simple_read,
3054 .write = rb_simple_write,
3058 static __init int rb_init_debugfs(void)
3060 struct dentry *d_tracer;
3062 d_tracer = tracing_init_dentry();
3064 trace_create_file("tracing_on", 0644, d_tracer,
3065 &ring_buffer_flags, &rb_simple_fops);
3070 fs_initcall(rb_init_debugfs);
3072 #ifdef CONFIG_HOTPLUG_CPU
3073 static int rb_cpu_notify(struct notifier_block *self,
3074 unsigned long action, void *hcpu)
3076 struct ring_buffer *buffer =
3077 container_of(self, struct ring_buffer, cpu_notify);
3078 long cpu = (long)hcpu;
3081 case CPU_UP_PREPARE:
3082 case CPU_UP_PREPARE_FROZEN:
3083 if (cpu_isset(cpu, *buffer->cpumask))
3086 buffer->buffers[cpu] =
3087 rb_allocate_cpu_buffer(buffer, cpu);
3088 if (!buffer->buffers[cpu]) {
3089 WARN(1, "failed to allocate ring buffer on CPU %ld\n",
3094 cpu_set(cpu, *buffer->cpumask);
3096 case CPU_DOWN_PREPARE:
3097 case CPU_DOWN_PREPARE_FROZEN:
3100 * If we were to free the buffer, then the user would
3101 * lose any trace that was in the buffer.