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 u64 ring_buffer_time_stamp(struct ring_buffer *buffer, int cpu)
461 preempt_disable_notrace();
462 /* shift to debug/test normalization and TIME_EXTENTS */
463 time = buffer->clock() << DEBUG_SHIFT;
464 preempt_enable_no_resched_notrace();
468 EXPORT_SYMBOL_GPL(ring_buffer_time_stamp);
470 void ring_buffer_normalize_time_stamp(struct ring_buffer *buffer,
473 /* Just stupid testing the normalize function and deltas */
476 EXPORT_SYMBOL_GPL(ring_buffer_normalize_time_stamp);
479 * check_pages - integrity check of buffer pages
480 * @cpu_buffer: CPU buffer with pages to test
482 * As a safety measure we check to make sure the data pages have not
485 static int rb_check_pages(struct ring_buffer_per_cpu *cpu_buffer)
487 struct list_head *head = &cpu_buffer->pages;
488 struct buffer_page *bpage, *tmp;
490 if (RB_WARN_ON(cpu_buffer, head->next->prev != head))
492 if (RB_WARN_ON(cpu_buffer, head->prev->next != head))
495 list_for_each_entry_safe(bpage, tmp, head, list) {
496 if (RB_WARN_ON(cpu_buffer,
497 bpage->list.next->prev != &bpage->list))
499 if (RB_WARN_ON(cpu_buffer,
500 bpage->list.prev->next != &bpage->list))
507 static int rb_allocate_pages(struct ring_buffer_per_cpu *cpu_buffer,
510 struct list_head *head = &cpu_buffer->pages;
511 struct buffer_page *bpage, *tmp;
516 for (i = 0; i < nr_pages; i++) {
517 bpage = kzalloc_node(ALIGN(sizeof(*bpage), cache_line_size()),
518 GFP_KERNEL, cpu_to_node(cpu_buffer->cpu));
521 list_add(&bpage->list, &pages);
523 addr = __get_free_page(GFP_KERNEL);
526 bpage->page = (void *)addr;
527 rb_init_page(bpage->page);
530 list_splice(&pages, head);
532 rb_check_pages(cpu_buffer);
537 list_for_each_entry_safe(bpage, tmp, &pages, list) {
538 list_del_init(&bpage->list);
539 free_buffer_page(bpage);
544 static struct ring_buffer_per_cpu *
545 rb_allocate_cpu_buffer(struct ring_buffer *buffer, int cpu)
547 struct ring_buffer_per_cpu *cpu_buffer;
548 struct buffer_page *bpage;
552 cpu_buffer = kzalloc_node(ALIGN(sizeof(*cpu_buffer), cache_line_size()),
553 GFP_KERNEL, cpu_to_node(cpu));
557 cpu_buffer->cpu = cpu;
558 cpu_buffer->buffer = buffer;
559 spin_lock_init(&cpu_buffer->reader_lock);
560 cpu_buffer->lock = (raw_spinlock_t)__RAW_SPIN_LOCK_UNLOCKED;
561 INIT_LIST_HEAD(&cpu_buffer->pages);
563 bpage = kzalloc_node(ALIGN(sizeof(*bpage), cache_line_size()),
564 GFP_KERNEL, cpu_to_node(cpu));
566 goto fail_free_buffer;
568 cpu_buffer->reader_page = bpage;
569 addr = __get_free_page(GFP_KERNEL);
571 goto fail_free_reader;
572 bpage->page = (void *)addr;
573 rb_init_page(bpage->page);
575 INIT_LIST_HEAD(&cpu_buffer->reader_page->list);
577 ret = rb_allocate_pages(cpu_buffer, buffer->pages);
579 goto fail_free_reader;
581 cpu_buffer->head_page
582 = list_entry(cpu_buffer->pages.next, struct buffer_page, list);
583 cpu_buffer->tail_page = cpu_buffer->commit_page = cpu_buffer->head_page;
588 free_buffer_page(cpu_buffer->reader_page);
595 static void rb_free_cpu_buffer(struct ring_buffer_per_cpu *cpu_buffer)
597 struct list_head *head = &cpu_buffer->pages;
598 struct buffer_page *bpage, *tmp;
600 free_buffer_page(cpu_buffer->reader_page);
602 list_for_each_entry_safe(bpage, tmp, head, list) {
603 list_del_init(&bpage->list);
604 free_buffer_page(bpage);
610 * Causes compile errors if the struct buffer_page gets bigger
611 * than the struct page.
613 extern int ring_buffer_page_too_big(void);
615 #ifdef CONFIG_HOTPLUG_CPU
616 static int rb_cpu_notify(struct notifier_block *self,
617 unsigned long action, void *hcpu);
621 * ring_buffer_alloc - allocate a new ring_buffer
622 * @size: the size in bytes per cpu that is needed.
623 * @flags: attributes to set for the ring buffer.
625 * Currently the only flag that is available is the RB_FL_OVERWRITE
626 * flag. This flag means that the buffer will overwrite old data
627 * when the buffer wraps. If this flag is not set, the buffer will
628 * drop data when the tail hits the head.
630 struct ring_buffer *ring_buffer_alloc(unsigned long size, unsigned flags)
632 struct ring_buffer *buffer;
636 /* Paranoid! Optimizes out when all is well */
637 if (sizeof(struct buffer_page) > sizeof(struct page))
638 ring_buffer_page_too_big();
641 /* keep it in its own cache line */
642 buffer = kzalloc(ALIGN(sizeof(*buffer), cache_line_size()),
647 if (!alloc_cpumask_var(&buffer->cpumask, GFP_KERNEL))
648 goto fail_free_buffer;
650 buffer->pages = DIV_ROUND_UP(size, BUF_PAGE_SIZE);
651 buffer->flags = flags;
652 buffer->clock = trace_clock_local;
654 /* need at least two pages */
655 if (buffer->pages == 1)
659 * In case of non-hotplug cpu, if the ring-buffer is allocated
660 * in early initcall, it will not be notified of secondary cpus.
661 * In that off case, we need to allocate for all possible cpus.
663 #ifdef CONFIG_HOTPLUG_CPU
665 cpumask_copy(buffer->cpumask, cpu_online_mask);
667 cpumask_copy(buffer->cpumask, cpu_possible_mask);
669 buffer->cpus = nr_cpu_ids;
671 bsize = sizeof(void *) * nr_cpu_ids;
672 buffer->buffers = kzalloc(ALIGN(bsize, cache_line_size()),
674 if (!buffer->buffers)
675 goto fail_free_cpumask;
677 for_each_buffer_cpu(buffer, cpu) {
678 buffer->buffers[cpu] =
679 rb_allocate_cpu_buffer(buffer, cpu);
680 if (!buffer->buffers[cpu])
681 goto fail_free_buffers;
684 #ifdef CONFIG_HOTPLUG_CPU
685 buffer->cpu_notify.notifier_call = rb_cpu_notify;
686 buffer->cpu_notify.priority = 0;
687 register_cpu_notifier(&buffer->cpu_notify);
691 mutex_init(&buffer->mutex);
696 for_each_buffer_cpu(buffer, cpu) {
697 if (buffer->buffers[cpu])
698 rb_free_cpu_buffer(buffer->buffers[cpu]);
700 kfree(buffer->buffers);
703 free_cpumask_var(buffer->cpumask);
710 EXPORT_SYMBOL_GPL(ring_buffer_alloc);
713 * ring_buffer_free - free a ring buffer.
714 * @buffer: the buffer to free.
717 ring_buffer_free(struct ring_buffer *buffer)
723 #ifdef CONFIG_HOTPLUG_CPU
724 unregister_cpu_notifier(&buffer->cpu_notify);
727 for_each_buffer_cpu(buffer, cpu)
728 rb_free_cpu_buffer(buffer->buffers[cpu]);
732 free_cpumask_var(buffer->cpumask);
736 EXPORT_SYMBOL_GPL(ring_buffer_free);
738 void ring_buffer_set_clock(struct ring_buffer *buffer,
741 buffer->clock = clock;
744 static void rb_reset_cpu(struct ring_buffer_per_cpu *cpu_buffer);
747 rb_remove_pages(struct ring_buffer_per_cpu *cpu_buffer, unsigned nr_pages)
749 struct buffer_page *bpage;
753 atomic_inc(&cpu_buffer->record_disabled);
756 for (i = 0; i < nr_pages; i++) {
757 if (RB_WARN_ON(cpu_buffer, list_empty(&cpu_buffer->pages)))
759 p = cpu_buffer->pages.next;
760 bpage = list_entry(p, struct buffer_page, list);
761 list_del_init(&bpage->list);
762 free_buffer_page(bpage);
764 if (RB_WARN_ON(cpu_buffer, list_empty(&cpu_buffer->pages)))
767 rb_reset_cpu(cpu_buffer);
769 rb_check_pages(cpu_buffer);
771 atomic_dec(&cpu_buffer->record_disabled);
776 rb_insert_pages(struct ring_buffer_per_cpu *cpu_buffer,
777 struct list_head *pages, unsigned nr_pages)
779 struct buffer_page *bpage;
783 atomic_inc(&cpu_buffer->record_disabled);
786 for (i = 0; i < nr_pages; i++) {
787 if (RB_WARN_ON(cpu_buffer, list_empty(pages)))
790 bpage = list_entry(p, struct buffer_page, list);
791 list_del_init(&bpage->list);
792 list_add_tail(&bpage->list, &cpu_buffer->pages);
794 rb_reset_cpu(cpu_buffer);
796 rb_check_pages(cpu_buffer);
798 atomic_dec(&cpu_buffer->record_disabled);
802 * ring_buffer_resize - resize the ring buffer
803 * @buffer: the buffer to resize.
804 * @size: the new size.
806 * The tracer is responsible for making sure that the buffer is
807 * not being used while changing the size.
808 * Note: We may be able to change the above requirement by using
809 * RCU synchronizations.
811 * Minimum size is 2 * BUF_PAGE_SIZE.
813 * Returns -1 on failure.
815 int ring_buffer_resize(struct ring_buffer *buffer, unsigned long size)
817 struct ring_buffer_per_cpu *cpu_buffer;
818 unsigned nr_pages, rm_pages, new_pages;
819 struct buffer_page *bpage, *tmp;
820 unsigned long buffer_size;
826 * Always succeed at resizing a non-existent buffer:
831 size = DIV_ROUND_UP(size, BUF_PAGE_SIZE);
832 size *= BUF_PAGE_SIZE;
833 buffer_size = buffer->pages * BUF_PAGE_SIZE;
835 /* we need a minimum of two pages */
836 if (size < BUF_PAGE_SIZE * 2)
837 size = BUF_PAGE_SIZE * 2;
839 if (size == buffer_size)
842 mutex_lock(&buffer->mutex);
845 nr_pages = DIV_ROUND_UP(size, BUF_PAGE_SIZE);
847 if (size < buffer_size) {
849 /* easy case, just free pages */
850 if (RB_WARN_ON(buffer, nr_pages >= buffer->pages))
853 rm_pages = buffer->pages - nr_pages;
855 for_each_buffer_cpu(buffer, cpu) {
856 cpu_buffer = buffer->buffers[cpu];
857 rb_remove_pages(cpu_buffer, rm_pages);
863 * This is a bit more difficult. We only want to add pages
864 * when we can allocate enough for all CPUs. We do this
865 * by allocating all the pages and storing them on a local
866 * link list. If we succeed in our allocation, then we
867 * add these pages to the cpu_buffers. Otherwise we just free
868 * them all and return -ENOMEM;
870 if (RB_WARN_ON(buffer, nr_pages <= buffer->pages))
873 new_pages = nr_pages - buffer->pages;
875 for_each_buffer_cpu(buffer, cpu) {
876 for (i = 0; i < new_pages; i++) {
877 bpage = kzalloc_node(ALIGN(sizeof(*bpage),
879 GFP_KERNEL, cpu_to_node(cpu));
882 list_add(&bpage->list, &pages);
883 addr = __get_free_page(GFP_KERNEL);
886 bpage->page = (void *)addr;
887 rb_init_page(bpage->page);
891 for_each_buffer_cpu(buffer, cpu) {
892 cpu_buffer = buffer->buffers[cpu];
893 rb_insert_pages(cpu_buffer, &pages, new_pages);
896 if (RB_WARN_ON(buffer, !list_empty(&pages)))
900 buffer->pages = nr_pages;
902 mutex_unlock(&buffer->mutex);
907 list_for_each_entry_safe(bpage, tmp, &pages, list) {
908 list_del_init(&bpage->list);
909 free_buffer_page(bpage);
912 mutex_unlock(&buffer->mutex);
916 * Something went totally wrong, and we are too paranoid
917 * to even clean up the mess.
921 mutex_unlock(&buffer->mutex);
924 EXPORT_SYMBOL_GPL(ring_buffer_resize);
927 __rb_data_page_index(struct buffer_data_page *bpage, unsigned index)
929 return bpage->data + index;
932 static inline void *__rb_page_index(struct buffer_page *bpage, unsigned index)
934 return bpage->page->data + index;
937 static inline struct ring_buffer_event *
938 rb_reader_event(struct ring_buffer_per_cpu *cpu_buffer)
940 return __rb_page_index(cpu_buffer->reader_page,
941 cpu_buffer->reader_page->read);
944 static inline struct ring_buffer_event *
945 rb_head_event(struct ring_buffer_per_cpu *cpu_buffer)
947 return __rb_page_index(cpu_buffer->head_page,
948 cpu_buffer->head_page->read);
951 static inline struct ring_buffer_event *
952 rb_iter_head_event(struct ring_buffer_iter *iter)
954 return __rb_page_index(iter->head_page, iter->head);
957 static inline unsigned rb_page_write(struct buffer_page *bpage)
959 return local_read(&bpage->write);
962 static inline unsigned rb_page_commit(struct buffer_page *bpage)
964 return local_read(&bpage->page->commit);
967 /* Size is determined by what has been commited */
968 static inline unsigned rb_page_size(struct buffer_page *bpage)
970 return rb_page_commit(bpage);
973 static inline unsigned
974 rb_commit_index(struct ring_buffer_per_cpu *cpu_buffer)
976 return rb_page_commit(cpu_buffer->commit_page);
979 static inline unsigned rb_head_size(struct ring_buffer_per_cpu *cpu_buffer)
981 return rb_page_commit(cpu_buffer->head_page);
984 static inline void rb_inc_page(struct ring_buffer_per_cpu *cpu_buffer,
985 struct buffer_page **bpage)
987 struct list_head *p = (*bpage)->list.next;
989 if (p == &cpu_buffer->pages)
992 *bpage = list_entry(p, struct buffer_page, list);
995 static inline unsigned
996 rb_event_index(struct ring_buffer_event *event)
998 unsigned long addr = (unsigned long)event;
1000 return (addr & ~PAGE_MASK) - (PAGE_SIZE - BUF_PAGE_SIZE);
1004 rb_is_commit(struct ring_buffer_per_cpu *cpu_buffer,
1005 struct ring_buffer_event *event)
1007 unsigned long addr = (unsigned long)event;
1008 unsigned long index;
1010 index = rb_event_index(event);
1013 return cpu_buffer->commit_page->page == (void *)addr &&
1014 rb_commit_index(cpu_buffer) == index;
1018 rb_set_commit_event(struct ring_buffer_per_cpu *cpu_buffer,
1019 struct ring_buffer_event *event)
1021 unsigned long addr = (unsigned long)event;
1022 unsigned long index;
1024 index = rb_event_index(event);
1027 while (cpu_buffer->commit_page->page != (void *)addr) {
1028 if (RB_WARN_ON(cpu_buffer,
1029 cpu_buffer->commit_page == cpu_buffer->tail_page))
1031 cpu_buffer->commit_page->page->commit =
1032 cpu_buffer->commit_page->write;
1033 rb_inc_page(cpu_buffer, &cpu_buffer->commit_page);
1034 cpu_buffer->write_stamp =
1035 cpu_buffer->commit_page->page->time_stamp;
1038 /* Now set the commit to the event's index */
1039 local_set(&cpu_buffer->commit_page->page->commit, index);
1043 rb_set_commit_to_write(struct ring_buffer_per_cpu *cpu_buffer)
1046 * We only race with interrupts and NMIs on this CPU.
1047 * If we own the commit event, then we can commit
1048 * all others that interrupted us, since the interruptions
1049 * are in stack format (they finish before they come
1050 * back to us). This allows us to do a simple loop to
1051 * assign the commit to the tail.
1054 while (cpu_buffer->commit_page != cpu_buffer->tail_page) {
1055 cpu_buffer->commit_page->page->commit =
1056 cpu_buffer->commit_page->write;
1057 rb_inc_page(cpu_buffer, &cpu_buffer->commit_page);
1058 cpu_buffer->write_stamp =
1059 cpu_buffer->commit_page->page->time_stamp;
1060 /* add barrier to keep gcc from optimizing too much */
1063 while (rb_commit_index(cpu_buffer) !=
1064 rb_page_write(cpu_buffer->commit_page)) {
1065 cpu_buffer->commit_page->page->commit =
1066 cpu_buffer->commit_page->write;
1070 /* again, keep gcc from optimizing */
1074 * If an interrupt came in just after the first while loop
1075 * and pushed the tail page forward, we will be left with
1076 * a dangling commit that will never go forward.
1078 if (unlikely(cpu_buffer->commit_page != cpu_buffer->tail_page))
1082 static void rb_reset_reader_page(struct ring_buffer_per_cpu *cpu_buffer)
1084 cpu_buffer->read_stamp = cpu_buffer->reader_page->page->time_stamp;
1085 cpu_buffer->reader_page->read = 0;
1088 static void rb_inc_iter(struct ring_buffer_iter *iter)
1090 struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
1093 * The iterator could be on the reader page (it starts there).
1094 * But the head could have moved, since the reader was
1095 * found. Check for this case and assign the iterator
1096 * to the head page instead of next.
1098 if (iter->head_page == cpu_buffer->reader_page)
1099 iter->head_page = cpu_buffer->head_page;
1101 rb_inc_page(cpu_buffer, &iter->head_page);
1103 iter->read_stamp = iter->head_page->page->time_stamp;
1108 * ring_buffer_update_event - update event type and data
1109 * @event: the even to update
1110 * @type: the type of event
1111 * @length: the size of the event field in the ring buffer
1113 * Update the type and data fields of the event. The length
1114 * is the actual size that is written to the ring buffer,
1115 * and with this, we can determine what to place into the
1119 rb_update_event(struct ring_buffer_event *event,
1120 unsigned type, unsigned length)
1122 event->type_len = type;
1126 case RINGBUF_TYPE_PADDING:
1127 case RINGBUF_TYPE_TIME_EXTEND:
1128 case RINGBUF_TYPE_TIME_STAMP:
1132 length -= RB_EVNT_HDR_SIZE;
1133 if (length > RB_MAX_SMALL_DATA)
1134 event->array[0] = length;
1136 event->type_len = DIV_ROUND_UP(length, RB_ALIGNMENT);
1143 static unsigned rb_calculate_event_length(unsigned length)
1145 struct ring_buffer_event event; /* Used only for sizeof array */
1147 /* zero length can cause confusions */
1151 if (length > RB_MAX_SMALL_DATA)
1152 length += sizeof(event.array[0]);
1154 length += RB_EVNT_HDR_SIZE;
1155 length = ALIGN(length, RB_ALIGNMENT);
1161 static struct ring_buffer_event *
1162 rb_move_tail(struct ring_buffer_per_cpu *cpu_buffer,
1163 unsigned long length, unsigned long tail,
1164 struct buffer_page *commit_page,
1165 struct buffer_page *tail_page, u64 *ts)
1167 struct buffer_page *next_page, *head_page, *reader_page;
1168 struct ring_buffer *buffer = cpu_buffer->buffer;
1169 struct ring_buffer_event *event;
1170 bool lock_taken = false;
1171 unsigned long flags;
1173 next_page = tail_page;
1175 local_irq_save(flags);
1177 * Since the write to the buffer is still not
1178 * fully lockless, we must be careful with NMIs.
1179 * The locks in the writers are taken when a write
1180 * crosses to a new page. The locks protect against
1181 * races with the readers (this will soon be fixed
1182 * with a lockless solution).
1184 * Because we can not protect against NMIs, and we
1185 * want to keep traces reentrant, we need to manage
1186 * what happens when we are in an NMI.
1188 * NMIs can happen after we take the lock.
1189 * If we are in an NMI, only take the lock
1190 * if it is not already taken. Otherwise
1193 if (unlikely(in_nmi())) {
1194 if (!__raw_spin_trylock(&cpu_buffer->lock)) {
1195 cpu_buffer->nmi_dropped++;
1199 __raw_spin_lock(&cpu_buffer->lock);
1203 rb_inc_page(cpu_buffer, &next_page);
1205 head_page = cpu_buffer->head_page;
1206 reader_page = cpu_buffer->reader_page;
1208 /* we grabbed the lock before incrementing */
1209 if (RB_WARN_ON(cpu_buffer, next_page == reader_page))
1213 * If for some reason, we had an interrupt storm that made
1214 * it all the way around the buffer, bail, and warn
1217 if (unlikely(next_page == commit_page)) {
1218 cpu_buffer->commit_overrun++;
1222 if (next_page == head_page) {
1223 if (!(buffer->flags & RB_FL_OVERWRITE))
1226 /* tail_page has not moved yet? */
1227 if (tail_page == cpu_buffer->tail_page) {
1228 /* count overflows */
1229 cpu_buffer->overrun +=
1230 local_read(&head_page->entries);
1232 rb_inc_page(cpu_buffer, &head_page);
1233 cpu_buffer->head_page = head_page;
1234 cpu_buffer->head_page->read = 0;
1239 * If the tail page is still the same as what we think
1240 * it is, then it is up to us to update the tail
1243 if (tail_page == cpu_buffer->tail_page) {
1244 local_set(&next_page->write, 0);
1245 local_set(&next_page->entries, 0);
1246 local_set(&next_page->page->commit, 0);
1247 cpu_buffer->tail_page = next_page;
1249 /* reread the time stamp */
1250 *ts = ring_buffer_time_stamp(buffer, cpu_buffer->cpu);
1251 cpu_buffer->tail_page->page->time_stamp = *ts;
1255 * The actual tail page has moved forward.
1257 if (tail < BUF_PAGE_SIZE) {
1258 /* Mark the rest of the page with padding */
1259 event = __rb_page_index(tail_page, tail);
1260 rb_event_set_padding(event);
1263 /* Set the write back to the previous setting */
1264 local_sub(length, &tail_page->write);
1267 * If this was a commit entry that failed,
1268 * increment that too
1270 if (tail_page == cpu_buffer->commit_page &&
1271 tail == rb_commit_index(cpu_buffer)) {
1272 rb_set_commit_to_write(cpu_buffer);
1275 __raw_spin_unlock(&cpu_buffer->lock);
1276 local_irq_restore(flags);
1278 /* fail and let the caller try again */
1279 return ERR_PTR(-EAGAIN);
1283 local_sub(length, &tail_page->write);
1285 if (likely(lock_taken))
1286 __raw_spin_unlock(&cpu_buffer->lock);
1287 local_irq_restore(flags);
1291 static struct ring_buffer_event *
1292 __rb_reserve_next(struct ring_buffer_per_cpu *cpu_buffer,
1293 unsigned type, unsigned long length, u64 *ts)
1295 struct buffer_page *tail_page, *commit_page;
1296 struct ring_buffer_event *event;
1297 unsigned long tail, write;
1299 commit_page = cpu_buffer->commit_page;
1300 /* we just need to protect against interrupts */
1302 tail_page = cpu_buffer->tail_page;
1303 write = local_add_return(length, &tail_page->write);
1304 tail = write - length;
1306 /* See if we shot pass the end of this buffer page */
1307 if (write > BUF_PAGE_SIZE)
1308 return rb_move_tail(cpu_buffer, length, tail,
1309 commit_page, tail_page, ts);
1311 /* We reserved something on the buffer */
1313 if (RB_WARN_ON(cpu_buffer, write > BUF_PAGE_SIZE))
1316 event = __rb_page_index(tail_page, tail);
1317 rb_update_event(event, type, length);
1319 /* The passed in type is zero for DATA */
1321 local_inc(&tail_page->entries);
1324 * If this is a commit and the tail is zero, then update
1325 * this page's time stamp.
1327 if (!tail && rb_is_commit(cpu_buffer, event))
1328 cpu_buffer->commit_page->page->time_stamp = *ts;
1334 rb_add_time_stamp(struct ring_buffer_per_cpu *cpu_buffer,
1335 u64 *ts, u64 *delta)
1337 struct ring_buffer_event *event;
1341 if (unlikely(*delta > (1ULL << 59) && !once++)) {
1342 printk(KERN_WARNING "Delta way too big! %llu"
1343 " ts=%llu write stamp = %llu\n",
1344 (unsigned long long)*delta,
1345 (unsigned long long)*ts,
1346 (unsigned long long)cpu_buffer->write_stamp);
1351 * The delta is too big, we to add a
1354 event = __rb_reserve_next(cpu_buffer,
1355 RINGBUF_TYPE_TIME_EXTEND,
1361 if (PTR_ERR(event) == -EAGAIN)
1364 /* Only a commited time event can update the write stamp */
1365 if (rb_is_commit(cpu_buffer, event)) {
1367 * If this is the first on the page, then we need to
1368 * update the page itself, and just put in a zero.
1370 if (rb_event_index(event)) {
1371 event->time_delta = *delta & TS_MASK;
1372 event->array[0] = *delta >> TS_SHIFT;
1374 cpu_buffer->commit_page->page->time_stamp = *ts;
1375 event->time_delta = 0;
1376 event->array[0] = 0;
1378 cpu_buffer->write_stamp = *ts;
1379 /* let the caller know this was the commit */
1382 /* Darn, this is just wasted space */
1383 event->time_delta = 0;
1384 event->array[0] = 0;
1393 static struct ring_buffer_event *
1394 rb_reserve_next_event(struct ring_buffer_per_cpu *cpu_buffer,
1395 unsigned long length)
1397 struct ring_buffer_event *event;
1402 length = rb_calculate_event_length(length);
1405 * We allow for interrupts to reenter here and do a trace.
1406 * If one does, it will cause this original code to loop
1407 * back here. Even with heavy interrupts happening, this
1408 * should only happen a few times in a row. If this happens
1409 * 1000 times in a row, there must be either an interrupt
1410 * storm or we have something buggy.
1413 if (RB_WARN_ON(cpu_buffer, ++nr_loops > 1000))
1416 ts = ring_buffer_time_stamp(cpu_buffer->buffer, cpu_buffer->cpu);
1419 * Only the first commit can update the timestamp.
1420 * Yes there is a race here. If an interrupt comes in
1421 * just after the conditional and it traces too, then it
1422 * will also check the deltas. More than one timestamp may
1423 * also be made. But only the entry that did the actual
1424 * commit will be something other than zero.
1426 if (likely(cpu_buffer->tail_page == cpu_buffer->commit_page &&
1427 rb_page_write(cpu_buffer->tail_page) ==
1428 rb_commit_index(cpu_buffer))) {
1430 delta = ts - cpu_buffer->write_stamp;
1432 /* make sure this delta is calculated here */
1435 /* Did the write stamp get updated already? */
1436 if (unlikely(ts < cpu_buffer->write_stamp))
1439 else if (unlikely(test_time_stamp(delta))) {
1441 commit = rb_add_time_stamp(cpu_buffer, &ts, &delta);
1443 if (commit == -EBUSY)
1446 if (commit == -EAGAIN)
1449 RB_WARN_ON(cpu_buffer, commit < 0);
1452 /* Non commits have zero deltas */
1455 event = __rb_reserve_next(cpu_buffer, 0, length, &ts);
1456 if (PTR_ERR(event) == -EAGAIN)
1460 if (unlikely(commit))
1462 * Ouch! We needed a timestamp and it was commited. But
1463 * we didn't get our event reserved.
1465 rb_set_commit_to_write(cpu_buffer);
1470 * If the timestamp was commited, make the commit our entry
1471 * now so that we will update it when needed.
1473 if (unlikely(commit))
1474 rb_set_commit_event(cpu_buffer, event);
1475 else if (!rb_is_commit(cpu_buffer, event))
1478 event->time_delta = delta;
1483 #define TRACE_RECURSIVE_DEPTH 16
1485 static int trace_recursive_lock(void)
1487 current->trace_recursion++;
1489 if (likely(current->trace_recursion < TRACE_RECURSIVE_DEPTH))
1492 /* Disable all tracing before we do anything else */
1493 tracing_off_permanent();
1495 printk_once(KERN_WARNING "Tracing recursion: depth[%ld]:"
1496 "HC[%lu]:SC[%lu]:NMI[%lu]\n",
1497 current->trace_recursion,
1498 hardirq_count() >> HARDIRQ_SHIFT,
1499 softirq_count() >> SOFTIRQ_SHIFT,
1506 static void trace_recursive_unlock(void)
1508 WARN_ON_ONCE(!current->trace_recursion);
1510 current->trace_recursion--;
1513 static DEFINE_PER_CPU(int, rb_need_resched);
1516 * ring_buffer_lock_reserve - reserve a part of the buffer
1517 * @buffer: the ring buffer to reserve from
1518 * @length: the length of the data to reserve (excluding event header)
1520 * Returns a reseverd event on the ring buffer to copy directly to.
1521 * The user of this interface will need to get the body to write into
1522 * and can use the ring_buffer_event_data() interface.
1524 * The length is the length of the data needed, not the event length
1525 * which also includes the event header.
1527 * Must be paired with ring_buffer_unlock_commit, unless NULL is returned.
1528 * If NULL is returned, then nothing has been allocated or locked.
1530 struct ring_buffer_event *
1531 ring_buffer_lock_reserve(struct ring_buffer *buffer, unsigned long length)
1533 struct ring_buffer_per_cpu *cpu_buffer;
1534 struct ring_buffer_event *event;
1537 if (ring_buffer_flags != RB_BUFFERS_ON)
1540 if (atomic_read(&buffer->record_disabled))
1543 /* If we are tracing schedule, we don't want to recurse */
1544 resched = ftrace_preempt_disable();
1546 if (trace_recursive_lock())
1549 cpu = raw_smp_processor_id();
1551 if (!cpumask_test_cpu(cpu, buffer->cpumask))
1554 cpu_buffer = buffer->buffers[cpu];
1556 if (atomic_read(&cpu_buffer->record_disabled))
1559 if (length > BUF_MAX_DATA_SIZE)
1562 event = rb_reserve_next_event(cpu_buffer, length);
1567 * Need to store resched state on this cpu.
1568 * Only the first needs to.
1571 if (preempt_count() == 1)
1572 per_cpu(rb_need_resched, cpu) = resched;
1577 trace_recursive_unlock();
1580 ftrace_preempt_enable(resched);
1583 EXPORT_SYMBOL_GPL(ring_buffer_lock_reserve);
1585 static void rb_commit(struct ring_buffer_per_cpu *cpu_buffer,
1586 struct ring_buffer_event *event)
1588 local_inc(&cpu_buffer->entries);
1590 /* Only process further if we own the commit */
1591 if (!rb_is_commit(cpu_buffer, event))
1594 cpu_buffer->write_stamp += event->time_delta;
1596 rb_set_commit_to_write(cpu_buffer);
1600 * ring_buffer_unlock_commit - commit a reserved
1601 * @buffer: The buffer to commit to
1602 * @event: The event pointer to commit.
1604 * This commits the data to the ring buffer, and releases any locks held.
1606 * Must be paired with ring_buffer_lock_reserve.
1608 int ring_buffer_unlock_commit(struct ring_buffer *buffer,
1609 struct ring_buffer_event *event)
1611 struct ring_buffer_per_cpu *cpu_buffer;
1612 int cpu = raw_smp_processor_id();
1614 cpu_buffer = buffer->buffers[cpu];
1616 rb_commit(cpu_buffer, event);
1618 trace_recursive_unlock();
1621 * Only the last preempt count needs to restore preemption.
1623 if (preempt_count() == 1)
1624 ftrace_preempt_enable(per_cpu(rb_need_resched, cpu));
1626 preempt_enable_no_resched_notrace();
1630 EXPORT_SYMBOL_GPL(ring_buffer_unlock_commit);
1632 static inline void rb_event_discard(struct ring_buffer_event *event)
1634 /* array[0] holds the actual length for the discarded event */
1635 event->array[0] = rb_event_data_length(event) - RB_EVNT_HDR_SIZE;
1636 event->type_len = RINGBUF_TYPE_PADDING;
1637 /* time delta must be non zero */
1638 if (!event->time_delta)
1639 event->time_delta = 1;
1643 * ring_buffer_event_discard - discard any event in the ring buffer
1644 * @event: the event to discard
1646 * Sometimes a event that is in the ring buffer needs to be ignored.
1647 * This function lets the user discard an event in the ring buffer
1648 * and then that event will not be read later.
1650 * Note, it is up to the user to be careful with this, and protect
1651 * against races. If the user discards an event that has been consumed
1652 * it is possible that it could corrupt the ring buffer.
1654 void ring_buffer_event_discard(struct ring_buffer_event *event)
1656 rb_event_discard(event);
1658 EXPORT_SYMBOL_GPL(ring_buffer_event_discard);
1661 * ring_buffer_commit_discard - discard an event that has not been committed
1662 * @buffer: the ring buffer
1663 * @event: non committed event to discard
1665 * This is similar to ring_buffer_event_discard but must only be
1666 * performed on an event that has not been committed yet. The difference
1667 * is that this will also try to free the event from the ring buffer
1668 * if another event has not been added behind it.
1670 * If another event has been added behind it, it will set the event
1671 * up as discarded, and perform the commit.
1673 * If this function is called, do not call ring_buffer_unlock_commit on
1676 void ring_buffer_discard_commit(struct ring_buffer *buffer,
1677 struct ring_buffer_event *event)
1679 struct ring_buffer_per_cpu *cpu_buffer;
1680 unsigned long new_index, old_index;
1681 struct buffer_page *bpage;
1682 unsigned long index;
1686 /* The event is discarded regardless */
1687 rb_event_discard(event);
1690 * This must only be called if the event has not been
1691 * committed yet. Thus we can assume that preemption
1692 * is still disabled.
1694 RB_WARN_ON(buffer, preemptible());
1696 cpu = smp_processor_id();
1697 cpu_buffer = buffer->buffers[cpu];
1699 new_index = rb_event_index(event);
1700 old_index = new_index + rb_event_length(event);
1701 addr = (unsigned long)event;
1704 bpage = cpu_buffer->tail_page;
1706 if (bpage == (void *)addr && rb_page_write(bpage) == old_index) {
1708 * This is on the tail page. It is possible that
1709 * a write could come in and move the tail page
1710 * and write to the next page. That is fine
1711 * because we just shorten what is on this page.
1713 index = local_cmpxchg(&bpage->write, old_index, new_index);
1714 if (index == old_index)
1719 * The commit is still visible by the reader, so we
1720 * must increment entries.
1722 local_inc(&cpu_buffer->entries);
1725 * If a write came in and pushed the tail page
1726 * we still need to update the commit pointer
1727 * if we were the commit.
1729 if (rb_is_commit(cpu_buffer, event))
1730 rb_set_commit_to_write(cpu_buffer);
1732 trace_recursive_unlock();
1735 * Only the last preempt count needs to restore preemption.
1737 if (preempt_count() == 1)
1738 ftrace_preempt_enable(per_cpu(rb_need_resched, cpu));
1740 preempt_enable_no_resched_notrace();
1743 EXPORT_SYMBOL_GPL(ring_buffer_discard_commit);
1746 * ring_buffer_write - write data to the buffer without reserving
1747 * @buffer: The ring buffer to write to.
1748 * @length: The length of the data being written (excluding the event header)
1749 * @data: The data to write to the buffer.
1751 * This is like ring_buffer_lock_reserve and ring_buffer_unlock_commit as
1752 * one function. If you already have the data to write to the buffer, it
1753 * may be easier to simply call this function.
1755 * Note, like ring_buffer_lock_reserve, the length is the length of the data
1756 * and not the length of the event which would hold the header.
1758 int ring_buffer_write(struct ring_buffer *buffer,
1759 unsigned long length,
1762 struct ring_buffer_per_cpu *cpu_buffer;
1763 struct ring_buffer_event *event;
1768 if (ring_buffer_flags != RB_BUFFERS_ON)
1771 if (atomic_read(&buffer->record_disabled))
1774 resched = ftrace_preempt_disable();
1776 cpu = raw_smp_processor_id();
1778 if (!cpumask_test_cpu(cpu, buffer->cpumask))
1781 cpu_buffer = buffer->buffers[cpu];
1783 if (atomic_read(&cpu_buffer->record_disabled))
1786 if (length > BUF_MAX_DATA_SIZE)
1789 event = rb_reserve_next_event(cpu_buffer, length);
1793 body = rb_event_data(event);
1795 memcpy(body, data, length);
1797 rb_commit(cpu_buffer, event);
1801 ftrace_preempt_enable(resched);
1805 EXPORT_SYMBOL_GPL(ring_buffer_write);
1807 static int rb_per_cpu_empty(struct ring_buffer_per_cpu *cpu_buffer)
1809 struct buffer_page *reader = cpu_buffer->reader_page;
1810 struct buffer_page *head = cpu_buffer->head_page;
1811 struct buffer_page *commit = cpu_buffer->commit_page;
1813 return reader->read == rb_page_commit(reader) &&
1814 (commit == reader ||
1816 head->read == rb_page_commit(commit)));
1820 * ring_buffer_record_disable - stop all writes into the buffer
1821 * @buffer: The ring buffer to stop writes to.
1823 * This prevents all writes to the buffer. Any attempt to write
1824 * to the buffer after this will fail and return NULL.
1826 * The caller should call synchronize_sched() after this.
1828 void ring_buffer_record_disable(struct ring_buffer *buffer)
1830 atomic_inc(&buffer->record_disabled);
1832 EXPORT_SYMBOL_GPL(ring_buffer_record_disable);
1835 * ring_buffer_record_enable - enable writes to the buffer
1836 * @buffer: The ring buffer to enable writes
1838 * Note, multiple disables will need the same number of enables
1839 * to truely enable the writing (much like preempt_disable).
1841 void ring_buffer_record_enable(struct ring_buffer *buffer)
1843 atomic_dec(&buffer->record_disabled);
1845 EXPORT_SYMBOL_GPL(ring_buffer_record_enable);
1848 * ring_buffer_record_disable_cpu - stop all writes into the cpu_buffer
1849 * @buffer: The ring buffer to stop writes to.
1850 * @cpu: The CPU buffer to stop
1852 * This prevents all writes to the buffer. Any attempt to write
1853 * to the buffer after this will fail and return NULL.
1855 * The caller should call synchronize_sched() after this.
1857 void ring_buffer_record_disable_cpu(struct ring_buffer *buffer, int cpu)
1859 struct ring_buffer_per_cpu *cpu_buffer;
1861 if (!cpumask_test_cpu(cpu, buffer->cpumask))
1864 cpu_buffer = buffer->buffers[cpu];
1865 atomic_inc(&cpu_buffer->record_disabled);
1867 EXPORT_SYMBOL_GPL(ring_buffer_record_disable_cpu);
1870 * ring_buffer_record_enable_cpu - enable writes to the buffer
1871 * @buffer: The ring buffer to enable writes
1872 * @cpu: The CPU to enable.
1874 * Note, multiple disables will need the same number of enables
1875 * to truely enable the writing (much like preempt_disable).
1877 void ring_buffer_record_enable_cpu(struct ring_buffer *buffer, int cpu)
1879 struct ring_buffer_per_cpu *cpu_buffer;
1881 if (!cpumask_test_cpu(cpu, buffer->cpumask))
1884 cpu_buffer = buffer->buffers[cpu];
1885 atomic_dec(&cpu_buffer->record_disabled);
1887 EXPORT_SYMBOL_GPL(ring_buffer_record_enable_cpu);
1890 * ring_buffer_entries_cpu - get the number of entries in a cpu buffer
1891 * @buffer: The ring buffer
1892 * @cpu: The per CPU buffer to get the entries from.
1894 unsigned long ring_buffer_entries_cpu(struct ring_buffer *buffer, int cpu)
1896 struct ring_buffer_per_cpu *cpu_buffer;
1899 if (!cpumask_test_cpu(cpu, buffer->cpumask))
1902 cpu_buffer = buffer->buffers[cpu];
1903 ret = (local_read(&cpu_buffer->entries) - cpu_buffer->overrun)
1908 EXPORT_SYMBOL_GPL(ring_buffer_entries_cpu);
1911 * ring_buffer_overrun_cpu - get the number of overruns in a cpu_buffer
1912 * @buffer: The ring buffer
1913 * @cpu: The per CPU buffer to get the number of overruns from
1915 unsigned long ring_buffer_overrun_cpu(struct ring_buffer *buffer, int cpu)
1917 struct ring_buffer_per_cpu *cpu_buffer;
1920 if (!cpumask_test_cpu(cpu, buffer->cpumask))
1923 cpu_buffer = buffer->buffers[cpu];
1924 ret = cpu_buffer->overrun;
1928 EXPORT_SYMBOL_GPL(ring_buffer_overrun_cpu);
1931 * ring_buffer_nmi_dropped_cpu - get the number of nmis that were dropped
1932 * @buffer: The ring buffer
1933 * @cpu: The per CPU buffer to get the number of overruns from
1935 unsigned long ring_buffer_nmi_dropped_cpu(struct ring_buffer *buffer, int cpu)
1937 struct ring_buffer_per_cpu *cpu_buffer;
1940 if (!cpumask_test_cpu(cpu, buffer->cpumask))
1943 cpu_buffer = buffer->buffers[cpu];
1944 ret = cpu_buffer->nmi_dropped;
1948 EXPORT_SYMBOL_GPL(ring_buffer_nmi_dropped_cpu);
1951 * ring_buffer_commit_overrun_cpu - get the number of overruns caused by commits
1952 * @buffer: The ring buffer
1953 * @cpu: The per CPU buffer to get the number of overruns from
1956 ring_buffer_commit_overrun_cpu(struct ring_buffer *buffer, int cpu)
1958 struct ring_buffer_per_cpu *cpu_buffer;
1961 if (!cpumask_test_cpu(cpu, buffer->cpumask))
1964 cpu_buffer = buffer->buffers[cpu];
1965 ret = cpu_buffer->commit_overrun;
1969 EXPORT_SYMBOL_GPL(ring_buffer_commit_overrun_cpu);
1972 * ring_buffer_entries - get the number of entries in a buffer
1973 * @buffer: The ring buffer
1975 * Returns the total number of entries in the ring buffer
1978 unsigned long ring_buffer_entries(struct ring_buffer *buffer)
1980 struct ring_buffer_per_cpu *cpu_buffer;
1981 unsigned long entries = 0;
1984 /* if you care about this being correct, lock the buffer */
1985 for_each_buffer_cpu(buffer, cpu) {
1986 cpu_buffer = buffer->buffers[cpu];
1987 entries += (local_read(&cpu_buffer->entries) -
1988 cpu_buffer->overrun) - cpu_buffer->read;
1993 EXPORT_SYMBOL_GPL(ring_buffer_entries);
1996 * ring_buffer_overrun_cpu - get the number of overruns in buffer
1997 * @buffer: The ring buffer
1999 * Returns the total number of overruns in the ring buffer
2002 unsigned long ring_buffer_overruns(struct ring_buffer *buffer)
2004 struct ring_buffer_per_cpu *cpu_buffer;
2005 unsigned long overruns = 0;
2008 /* if you care about this being correct, lock the buffer */
2009 for_each_buffer_cpu(buffer, cpu) {
2010 cpu_buffer = buffer->buffers[cpu];
2011 overruns += cpu_buffer->overrun;
2016 EXPORT_SYMBOL_GPL(ring_buffer_overruns);
2018 static void rb_iter_reset(struct ring_buffer_iter *iter)
2020 struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
2022 /* Iterator usage is expected to have record disabled */
2023 if (list_empty(&cpu_buffer->reader_page->list)) {
2024 iter->head_page = cpu_buffer->head_page;
2025 iter->head = cpu_buffer->head_page->read;
2027 iter->head_page = cpu_buffer->reader_page;
2028 iter->head = cpu_buffer->reader_page->read;
2031 iter->read_stamp = cpu_buffer->read_stamp;
2033 iter->read_stamp = iter->head_page->page->time_stamp;
2037 * ring_buffer_iter_reset - reset an iterator
2038 * @iter: The iterator to reset
2040 * Resets the iterator, so that it will start from the beginning
2043 void ring_buffer_iter_reset(struct ring_buffer_iter *iter)
2045 struct ring_buffer_per_cpu *cpu_buffer;
2046 unsigned long flags;
2051 cpu_buffer = iter->cpu_buffer;
2053 spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
2054 rb_iter_reset(iter);
2055 spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
2057 EXPORT_SYMBOL_GPL(ring_buffer_iter_reset);
2060 * ring_buffer_iter_empty - check if an iterator has no more to read
2061 * @iter: The iterator to check
2063 int ring_buffer_iter_empty(struct ring_buffer_iter *iter)
2065 struct ring_buffer_per_cpu *cpu_buffer;
2067 cpu_buffer = iter->cpu_buffer;
2069 return iter->head_page == cpu_buffer->commit_page &&
2070 iter->head == rb_commit_index(cpu_buffer);
2072 EXPORT_SYMBOL_GPL(ring_buffer_iter_empty);
2075 rb_update_read_stamp(struct ring_buffer_per_cpu *cpu_buffer,
2076 struct ring_buffer_event *event)
2080 switch (event->type_len) {
2081 case RINGBUF_TYPE_PADDING:
2084 case RINGBUF_TYPE_TIME_EXTEND:
2085 delta = event->array[0];
2087 delta += event->time_delta;
2088 cpu_buffer->read_stamp += delta;
2091 case RINGBUF_TYPE_TIME_STAMP:
2092 /* FIXME: not implemented */
2095 case RINGBUF_TYPE_DATA:
2096 cpu_buffer->read_stamp += event->time_delta;
2106 rb_update_iter_read_stamp(struct ring_buffer_iter *iter,
2107 struct ring_buffer_event *event)
2111 switch (event->type_len) {
2112 case RINGBUF_TYPE_PADDING:
2115 case RINGBUF_TYPE_TIME_EXTEND:
2116 delta = event->array[0];
2118 delta += event->time_delta;
2119 iter->read_stamp += delta;
2122 case RINGBUF_TYPE_TIME_STAMP:
2123 /* FIXME: not implemented */
2126 case RINGBUF_TYPE_DATA:
2127 iter->read_stamp += event->time_delta;
2136 static struct buffer_page *
2137 rb_get_reader_page(struct ring_buffer_per_cpu *cpu_buffer)
2139 struct buffer_page *reader = NULL;
2140 unsigned long flags;
2143 local_irq_save(flags);
2144 __raw_spin_lock(&cpu_buffer->lock);
2148 * This should normally only loop twice. But because the
2149 * start of the reader inserts an empty page, it causes
2150 * a case where we will loop three times. There should be no
2151 * reason to loop four times (that I know of).
2153 if (RB_WARN_ON(cpu_buffer, ++nr_loops > 3)) {
2158 reader = cpu_buffer->reader_page;
2160 /* If there's more to read, return this page */
2161 if (cpu_buffer->reader_page->read < rb_page_size(reader))
2164 /* Never should we have an index greater than the size */
2165 if (RB_WARN_ON(cpu_buffer,
2166 cpu_buffer->reader_page->read > rb_page_size(reader)))
2169 /* check if we caught up to the tail */
2171 if (cpu_buffer->commit_page == cpu_buffer->reader_page)
2175 * Splice the empty reader page into the list around the head.
2176 * Reset the reader page to size zero.
2179 reader = cpu_buffer->head_page;
2180 cpu_buffer->reader_page->list.next = reader->list.next;
2181 cpu_buffer->reader_page->list.prev = reader->list.prev;
2183 local_set(&cpu_buffer->reader_page->write, 0);
2184 local_set(&cpu_buffer->reader_page->entries, 0);
2185 local_set(&cpu_buffer->reader_page->page->commit, 0);
2187 /* Make the reader page now replace the head */
2188 reader->list.prev->next = &cpu_buffer->reader_page->list;
2189 reader->list.next->prev = &cpu_buffer->reader_page->list;
2192 * If the tail is on the reader, then we must set the head
2193 * to the inserted page, otherwise we set it one before.
2195 cpu_buffer->head_page = cpu_buffer->reader_page;
2197 if (cpu_buffer->commit_page != reader)
2198 rb_inc_page(cpu_buffer, &cpu_buffer->head_page);
2200 /* Finally update the reader page to the new head */
2201 cpu_buffer->reader_page = reader;
2202 rb_reset_reader_page(cpu_buffer);
2207 __raw_spin_unlock(&cpu_buffer->lock);
2208 local_irq_restore(flags);
2213 static void rb_advance_reader(struct ring_buffer_per_cpu *cpu_buffer)
2215 struct ring_buffer_event *event;
2216 struct buffer_page *reader;
2219 reader = rb_get_reader_page(cpu_buffer);
2221 /* This function should not be called when buffer is empty */
2222 if (RB_WARN_ON(cpu_buffer, !reader))
2225 event = rb_reader_event(cpu_buffer);
2227 if (event->type_len <= RINGBUF_TYPE_DATA_TYPE_LEN_MAX
2228 || rb_discarded_event(event))
2231 rb_update_read_stamp(cpu_buffer, event);
2233 length = rb_event_length(event);
2234 cpu_buffer->reader_page->read += length;
2237 static void rb_advance_iter(struct ring_buffer_iter *iter)
2239 struct ring_buffer *buffer;
2240 struct ring_buffer_per_cpu *cpu_buffer;
2241 struct ring_buffer_event *event;
2244 cpu_buffer = iter->cpu_buffer;
2245 buffer = cpu_buffer->buffer;
2248 * Check if we are at the end of the buffer.
2250 if (iter->head >= rb_page_size(iter->head_page)) {
2251 if (RB_WARN_ON(buffer,
2252 iter->head_page == cpu_buffer->commit_page))
2258 event = rb_iter_head_event(iter);
2260 length = rb_event_length(event);
2263 * This should not be called to advance the header if we are
2264 * at the tail of the buffer.
2266 if (RB_WARN_ON(cpu_buffer,
2267 (iter->head_page == cpu_buffer->commit_page) &&
2268 (iter->head + length > rb_commit_index(cpu_buffer))))
2271 rb_update_iter_read_stamp(iter, event);
2273 iter->head += length;
2275 /* check for end of page padding */
2276 if ((iter->head >= rb_page_size(iter->head_page)) &&
2277 (iter->head_page != cpu_buffer->commit_page))
2278 rb_advance_iter(iter);
2281 static struct ring_buffer_event *
2282 rb_buffer_peek(struct ring_buffer *buffer, int cpu, u64 *ts)
2284 struct ring_buffer_per_cpu *cpu_buffer;
2285 struct ring_buffer_event *event;
2286 struct buffer_page *reader;
2289 cpu_buffer = buffer->buffers[cpu];
2293 * We repeat when a timestamp is encountered. It is possible
2294 * to get multiple timestamps from an interrupt entering just
2295 * as one timestamp is about to be written. The max times
2296 * that this can happen is the number of nested interrupts we
2297 * can have. Nesting 10 deep of interrupts is clearly
2300 if (RB_WARN_ON(cpu_buffer, ++nr_loops > 10))
2303 reader = rb_get_reader_page(cpu_buffer);
2307 event = rb_reader_event(cpu_buffer);
2309 switch (event->type_len) {
2310 case RINGBUF_TYPE_PADDING:
2311 if (rb_null_event(event))
2312 RB_WARN_ON(cpu_buffer, 1);
2314 * Because the writer could be discarding every
2315 * event it creates (which would probably be bad)
2316 * if we were to go back to "again" then we may never
2317 * catch up, and will trigger the warn on, or lock
2318 * the box. Return the padding, and we will release
2319 * the current locks, and try again.
2321 rb_advance_reader(cpu_buffer);
2324 case RINGBUF_TYPE_TIME_EXTEND:
2325 /* Internal data, OK to advance */
2326 rb_advance_reader(cpu_buffer);
2329 case RINGBUF_TYPE_TIME_STAMP:
2330 /* FIXME: not implemented */
2331 rb_advance_reader(cpu_buffer);
2334 case RINGBUF_TYPE_DATA:
2336 *ts = cpu_buffer->read_stamp + event->time_delta;
2337 ring_buffer_normalize_time_stamp(buffer,
2338 cpu_buffer->cpu, ts);
2348 EXPORT_SYMBOL_GPL(ring_buffer_peek);
2350 static struct ring_buffer_event *
2351 rb_iter_peek(struct ring_buffer_iter *iter, u64 *ts)
2353 struct ring_buffer *buffer;
2354 struct ring_buffer_per_cpu *cpu_buffer;
2355 struct ring_buffer_event *event;
2358 if (ring_buffer_iter_empty(iter))
2361 cpu_buffer = iter->cpu_buffer;
2362 buffer = cpu_buffer->buffer;
2366 * We repeat when a timestamp is encountered. It is possible
2367 * to get multiple timestamps from an interrupt entering just
2368 * as one timestamp is about to be written. The max times
2369 * that this can happen is the number of nested interrupts we
2370 * can have. Nesting 10 deep of interrupts is clearly
2373 if (RB_WARN_ON(cpu_buffer, ++nr_loops > 10))
2376 if (rb_per_cpu_empty(cpu_buffer))
2379 event = rb_iter_head_event(iter);
2381 switch (event->type_len) {
2382 case RINGBUF_TYPE_PADDING:
2383 if (rb_null_event(event)) {
2387 rb_advance_iter(iter);
2390 case RINGBUF_TYPE_TIME_EXTEND:
2391 /* Internal data, OK to advance */
2392 rb_advance_iter(iter);
2395 case RINGBUF_TYPE_TIME_STAMP:
2396 /* FIXME: not implemented */
2397 rb_advance_iter(iter);
2400 case RINGBUF_TYPE_DATA:
2402 *ts = iter->read_stamp + event->time_delta;
2403 ring_buffer_normalize_time_stamp(buffer,
2404 cpu_buffer->cpu, ts);
2414 EXPORT_SYMBOL_GPL(ring_buffer_iter_peek);
2417 * ring_buffer_peek - peek at the next event to be read
2418 * @buffer: The ring buffer to read
2419 * @cpu: The cpu to peak at
2420 * @ts: The timestamp counter of this event.
2422 * This will return the event that will be read next, but does
2423 * not consume the data.
2425 struct ring_buffer_event *
2426 ring_buffer_peek(struct ring_buffer *buffer, int cpu, u64 *ts)
2428 struct ring_buffer_per_cpu *cpu_buffer = buffer->buffers[cpu];
2429 struct ring_buffer_event *event;
2430 unsigned long flags;
2432 if (!cpumask_test_cpu(cpu, buffer->cpumask))
2436 spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
2437 event = rb_buffer_peek(buffer, cpu, ts);
2438 spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
2440 if (event && event->type_len == RINGBUF_TYPE_PADDING) {
2449 * ring_buffer_iter_peek - peek at the next event to be read
2450 * @iter: The ring buffer iterator
2451 * @ts: The timestamp counter of this event.
2453 * This will return the event that will be read next, but does
2454 * not increment the iterator.
2456 struct ring_buffer_event *
2457 ring_buffer_iter_peek(struct ring_buffer_iter *iter, u64 *ts)
2459 struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
2460 struct ring_buffer_event *event;
2461 unsigned long flags;
2464 spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
2465 event = rb_iter_peek(iter, ts);
2466 spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
2468 if (event && event->type_len == RINGBUF_TYPE_PADDING) {
2477 * ring_buffer_consume - return an event and consume it
2478 * @buffer: The ring buffer to get the next event from
2480 * Returns the next event in the ring buffer, and that event is consumed.
2481 * Meaning, that sequential reads will keep returning a different event,
2482 * and eventually empty the ring buffer if the producer is slower.
2484 struct ring_buffer_event *
2485 ring_buffer_consume(struct ring_buffer *buffer, int cpu, u64 *ts)
2487 struct ring_buffer_per_cpu *cpu_buffer;
2488 struct ring_buffer_event *event = NULL;
2489 unsigned long flags;
2492 /* might be called in atomic */
2495 if (!cpumask_test_cpu(cpu, buffer->cpumask))
2498 cpu_buffer = buffer->buffers[cpu];
2499 spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
2501 event = rb_buffer_peek(buffer, cpu, ts);
2505 rb_advance_reader(cpu_buffer);
2508 spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
2513 if (event && event->type_len == RINGBUF_TYPE_PADDING) {
2520 EXPORT_SYMBOL_GPL(ring_buffer_consume);
2523 * ring_buffer_read_start - start a non consuming read of the buffer
2524 * @buffer: The ring buffer to read from
2525 * @cpu: The cpu buffer to iterate over
2527 * This starts up an iteration through the buffer. It also disables
2528 * the recording to the buffer until the reading is finished.
2529 * This prevents the reading from being corrupted. This is not
2530 * a consuming read, so a producer is not expected.
2532 * Must be paired with ring_buffer_finish.
2534 struct ring_buffer_iter *
2535 ring_buffer_read_start(struct ring_buffer *buffer, int cpu)
2537 struct ring_buffer_per_cpu *cpu_buffer;
2538 struct ring_buffer_iter *iter;
2539 unsigned long flags;
2541 if (!cpumask_test_cpu(cpu, buffer->cpumask))
2544 iter = kmalloc(sizeof(*iter), GFP_KERNEL);
2548 cpu_buffer = buffer->buffers[cpu];
2550 iter->cpu_buffer = cpu_buffer;
2552 atomic_inc(&cpu_buffer->record_disabled);
2553 synchronize_sched();
2555 spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
2556 __raw_spin_lock(&cpu_buffer->lock);
2557 rb_iter_reset(iter);
2558 __raw_spin_unlock(&cpu_buffer->lock);
2559 spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
2563 EXPORT_SYMBOL_GPL(ring_buffer_read_start);
2566 * ring_buffer_finish - finish reading the iterator of the buffer
2567 * @iter: The iterator retrieved by ring_buffer_start
2569 * This re-enables the recording to the buffer, and frees the
2573 ring_buffer_read_finish(struct ring_buffer_iter *iter)
2575 struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
2577 atomic_dec(&cpu_buffer->record_disabled);
2580 EXPORT_SYMBOL_GPL(ring_buffer_read_finish);
2583 * ring_buffer_read - read the next item in the ring buffer by the iterator
2584 * @iter: The ring buffer iterator
2585 * @ts: The time stamp of the event read.
2587 * This reads the next event in the ring buffer and increments the iterator.
2589 struct ring_buffer_event *
2590 ring_buffer_read(struct ring_buffer_iter *iter, u64 *ts)
2592 struct ring_buffer_event *event;
2593 struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
2594 unsigned long flags;
2597 spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
2598 event = rb_iter_peek(iter, ts);
2602 rb_advance_iter(iter);
2604 spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
2606 if (event && event->type_len == RINGBUF_TYPE_PADDING) {
2613 EXPORT_SYMBOL_GPL(ring_buffer_read);
2616 * ring_buffer_size - return the size of the ring buffer (in bytes)
2617 * @buffer: The ring buffer.
2619 unsigned long ring_buffer_size(struct ring_buffer *buffer)
2621 return BUF_PAGE_SIZE * buffer->pages;
2623 EXPORT_SYMBOL_GPL(ring_buffer_size);
2626 rb_reset_cpu(struct ring_buffer_per_cpu *cpu_buffer)
2628 cpu_buffer->head_page
2629 = list_entry(cpu_buffer->pages.next, struct buffer_page, list);
2630 local_set(&cpu_buffer->head_page->write, 0);
2631 local_set(&cpu_buffer->head_page->entries, 0);
2632 local_set(&cpu_buffer->head_page->page->commit, 0);
2634 cpu_buffer->head_page->read = 0;
2636 cpu_buffer->tail_page = cpu_buffer->head_page;
2637 cpu_buffer->commit_page = cpu_buffer->head_page;
2639 INIT_LIST_HEAD(&cpu_buffer->reader_page->list);
2640 local_set(&cpu_buffer->reader_page->write, 0);
2641 local_set(&cpu_buffer->reader_page->entries, 0);
2642 local_set(&cpu_buffer->reader_page->page->commit, 0);
2643 cpu_buffer->reader_page->read = 0;
2645 cpu_buffer->nmi_dropped = 0;
2646 cpu_buffer->commit_overrun = 0;
2647 cpu_buffer->overrun = 0;
2648 cpu_buffer->read = 0;
2649 local_set(&cpu_buffer->entries, 0);
2651 cpu_buffer->write_stamp = 0;
2652 cpu_buffer->read_stamp = 0;
2656 * ring_buffer_reset_cpu - reset a ring buffer per CPU buffer
2657 * @buffer: The ring buffer to reset a per cpu buffer of
2658 * @cpu: The CPU buffer to be reset
2660 void ring_buffer_reset_cpu(struct ring_buffer *buffer, int cpu)
2662 struct ring_buffer_per_cpu *cpu_buffer = buffer->buffers[cpu];
2663 unsigned long flags;
2665 if (!cpumask_test_cpu(cpu, buffer->cpumask))
2668 atomic_inc(&cpu_buffer->record_disabled);
2670 spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
2672 __raw_spin_lock(&cpu_buffer->lock);
2674 rb_reset_cpu(cpu_buffer);
2676 __raw_spin_unlock(&cpu_buffer->lock);
2678 spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
2680 atomic_dec(&cpu_buffer->record_disabled);
2682 EXPORT_SYMBOL_GPL(ring_buffer_reset_cpu);
2685 * ring_buffer_reset - reset a ring buffer
2686 * @buffer: The ring buffer to reset all cpu buffers
2688 void ring_buffer_reset(struct ring_buffer *buffer)
2692 for_each_buffer_cpu(buffer, cpu)
2693 ring_buffer_reset_cpu(buffer, cpu);
2695 EXPORT_SYMBOL_GPL(ring_buffer_reset);
2698 * rind_buffer_empty - is the ring buffer empty?
2699 * @buffer: The ring buffer to test
2701 int ring_buffer_empty(struct ring_buffer *buffer)
2703 struct ring_buffer_per_cpu *cpu_buffer;
2706 /* yes this is racy, but if you don't like the race, lock the buffer */
2707 for_each_buffer_cpu(buffer, cpu) {
2708 cpu_buffer = buffer->buffers[cpu];
2709 if (!rb_per_cpu_empty(cpu_buffer))
2715 EXPORT_SYMBOL_GPL(ring_buffer_empty);
2718 * ring_buffer_empty_cpu - is a cpu buffer of a ring buffer empty?
2719 * @buffer: The ring buffer
2720 * @cpu: The CPU buffer to test
2722 int ring_buffer_empty_cpu(struct ring_buffer *buffer, int cpu)
2724 struct ring_buffer_per_cpu *cpu_buffer;
2727 if (!cpumask_test_cpu(cpu, buffer->cpumask))
2730 cpu_buffer = buffer->buffers[cpu];
2731 ret = rb_per_cpu_empty(cpu_buffer);
2736 EXPORT_SYMBOL_GPL(ring_buffer_empty_cpu);
2739 * ring_buffer_swap_cpu - swap a CPU buffer between two ring buffers
2740 * @buffer_a: One buffer to swap with
2741 * @buffer_b: The other buffer to swap with
2743 * This function is useful for tracers that want to take a "snapshot"
2744 * of a CPU buffer and has another back up buffer lying around.
2745 * it is expected that the tracer handles the cpu buffer not being
2746 * used at the moment.
2748 int ring_buffer_swap_cpu(struct ring_buffer *buffer_a,
2749 struct ring_buffer *buffer_b, int cpu)
2751 struct ring_buffer_per_cpu *cpu_buffer_a;
2752 struct ring_buffer_per_cpu *cpu_buffer_b;
2755 if (!cpumask_test_cpu(cpu, buffer_a->cpumask) ||
2756 !cpumask_test_cpu(cpu, buffer_b->cpumask))
2759 /* At least make sure the two buffers are somewhat the same */
2760 if (buffer_a->pages != buffer_b->pages)
2765 if (ring_buffer_flags != RB_BUFFERS_ON)
2768 if (atomic_read(&buffer_a->record_disabled))
2771 if (atomic_read(&buffer_b->record_disabled))
2774 cpu_buffer_a = buffer_a->buffers[cpu];
2775 cpu_buffer_b = buffer_b->buffers[cpu];
2777 if (atomic_read(&cpu_buffer_a->record_disabled))
2780 if (atomic_read(&cpu_buffer_b->record_disabled))
2784 * We can't do a synchronize_sched here because this
2785 * function can be called in atomic context.
2786 * Normally this will be called from the same CPU as cpu.
2787 * If not it's up to the caller to protect this.
2789 atomic_inc(&cpu_buffer_a->record_disabled);
2790 atomic_inc(&cpu_buffer_b->record_disabled);
2792 buffer_a->buffers[cpu] = cpu_buffer_b;
2793 buffer_b->buffers[cpu] = cpu_buffer_a;
2795 cpu_buffer_b->buffer = buffer_a;
2796 cpu_buffer_a->buffer = buffer_b;
2798 atomic_dec(&cpu_buffer_a->record_disabled);
2799 atomic_dec(&cpu_buffer_b->record_disabled);
2805 EXPORT_SYMBOL_GPL(ring_buffer_swap_cpu);
2808 * ring_buffer_alloc_read_page - allocate a page to read from buffer
2809 * @buffer: the buffer to allocate for.
2811 * This function is used in conjunction with ring_buffer_read_page.
2812 * When reading a full page from the ring buffer, these functions
2813 * can be used to speed up the process. The calling function should
2814 * allocate a few pages first with this function. Then when it
2815 * needs to get pages from the ring buffer, it passes the result
2816 * of this function into ring_buffer_read_page, which will swap
2817 * the page that was allocated, with the read page of the buffer.
2820 * The page allocated, or NULL on error.
2822 void *ring_buffer_alloc_read_page(struct ring_buffer *buffer)
2824 struct buffer_data_page *bpage;
2827 addr = __get_free_page(GFP_KERNEL);
2831 bpage = (void *)addr;
2833 rb_init_page(bpage);
2837 EXPORT_SYMBOL_GPL(ring_buffer_alloc_read_page);
2840 * ring_buffer_free_read_page - free an allocated read page
2841 * @buffer: the buffer the page was allocate for
2842 * @data: the page to free
2844 * Free a page allocated from ring_buffer_alloc_read_page.
2846 void ring_buffer_free_read_page(struct ring_buffer *buffer, void *data)
2848 free_page((unsigned long)data);
2850 EXPORT_SYMBOL_GPL(ring_buffer_free_read_page);
2853 * ring_buffer_read_page - extract a page from the ring buffer
2854 * @buffer: buffer to extract from
2855 * @data_page: the page to use allocated from ring_buffer_alloc_read_page
2856 * @len: amount to extract
2857 * @cpu: the cpu of the buffer to extract
2858 * @full: should the extraction only happen when the page is full.
2860 * This function will pull out a page from the ring buffer and consume it.
2861 * @data_page must be the address of the variable that was returned
2862 * from ring_buffer_alloc_read_page. This is because the page might be used
2863 * to swap with a page in the ring buffer.
2866 * rpage = ring_buffer_alloc_read_page(buffer);
2869 * ret = ring_buffer_read_page(buffer, &rpage, len, cpu, 0);
2871 * process_page(rpage, ret);
2873 * When @full is set, the function will not return true unless
2874 * the writer is off the reader page.
2876 * Note: it is up to the calling functions to handle sleeps and wakeups.
2877 * The ring buffer can be used anywhere in the kernel and can not
2878 * blindly call wake_up. The layer that uses the ring buffer must be
2879 * responsible for that.
2882 * >=0 if data has been transferred, returns the offset of consumed data.
2883 * <0 if no data has been transferred.
2885 int ring_buffer_read_page(struct ring_buffer *buffer,
2886 void **data_page, size_t len, int cpu, int full)
2888 struct ring_buffer_per_cpu *cpu_buffer = buffer->buffers[cpu];
2889 struct ring_buffer_event *event;
2890 struct buffer_data_page *bpage;
2891 struct buffer_page *reader;
2892 unsigned long flags;
2893 unsigned int commit;
2898 if (!cpumask_test_cpu(cpu, buffer->cpumask))
2902 * If len is not big enough to hold the page header, then
2903 * we can not copy anything.
2905 if (len <= BUF_PAGE_HDR_SIZE)
2908 len -= BUF_PAGE_HDR_SIZE;
2917 spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
2919 reader = rb_get_reader_page(cpu_buffer);
2923 event = rb_reader_event(cpu_buffer);
2925 read = reader->read;
2926 commit = rb_page_commit(reader);
2929 * If this page has been partially read or
2930 * if len is not big enough to read the rest of the page or
2931 * a writer is still on the page, then
2932 * we must copy the data from the page to the buffer.
2933 * Otherwise, we can simply swap the page with the one passed in.
2935 if (read || (len < (commit - read)) ||
2936 cpu_buffer->reader_page == cpu_buffer->commit_page) {
2937 struct buffer_data_page *rpage = cpu_buffer->reader_page->page;
2938 unsigned int rpos = read;
2939 unsigned int pos = 0;
2945 if (len > (commit - read))
2946 len = (commit - read);
2948 size = rb_event_length(event);
2953 /* save the current timestamp, since the user will need it */
2954 save_timestamp = cpu_buffer->read_stamp;
2956 /* Need to copy one event at a time */
2958 memcpy(bpage->data + pos, rpage->data + rpos, size);
2962 rb_advance_reader(cpu_buffer);
2963 rpos = reader->read;
2966 event = rb_reader_event(cpu_buffer);
2967 size = rb_event_length(event);
2968 } while (len > size);
2971 local_set(&bpage->commit, pos);
2972 bpage->time_stamp = save_timestamp;
2974 /* we copied everything to the beginning */
2977 /* update the entry counter */
2978 cpu_buffer->read += local_read(&reader->entries);
2980 /* swap the pages */
2981 rb_init_page(bpage);
2982 bpage = reader->page;
2983 reader->page = *data_page;
2984 local_set(&reader->write, 0);
2985 local_set(&reader->entries, 0);
2992 spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
2997 EXPORT_SYMBOL_GPL(ring_buffer_read_page);
3000 rb_simple_read(struct file *filp, char __user *ubuf,
3001 size_t cnt, loff_t *ppos)
3003 unsigned long *p = filp->private_data;
3007 if (test_bit(RB_BUFFERS_DISABLED_BIT, p))
3008 r = sprintf(buf, "permanently disabled\n");
3010 r = sprintf(buf, "%d\n", test_bit(RB_BUFFERS_ON_BIT, p));
3012 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
3016 rb_simple_write(struct file *filp, const char __user *ubuf,
3017 size_t cnt, loff_t *ppos)
3019 unsigned long *p = filp->private_data;
3024 if (cnt >= sizeof(buf))
3027 if (copy_from_user(&buf, ubuf, cnt))
3032 ret = strict_strtoul(buf, 10, &val);
3037 set_bit(RB_BUFFERS_ON_BIT, p);
3039 clear_bit(RB_BUFFERS_ON_BIT, p);
3046 static const struct file_operations rb_simple_fops = {
3047 .open = tracing_open_generic,
3048 .read = rb_simple_read,
3049 .write = rb_simple_write,
3053 static __init int rb_init_debugfs(void)
3055 struct dentry *d_tracer;
3057 d_tracer = tracing_init_dentry();
3059 trace_create_file("tracing_on", 0644, d_tracer,
3060 &ring_buffer_flags, &rb_simple_fops);
3065 fs_initcall(rb_init_debugfs);
3067 #ifdef CONFIG_HOTPLUG_CPU
3068 static int rb_cpu_notify(struct notifier_block *self,
3069 unsigned long action, void *hcpu)
3071 struct ring_buffer *buffer =
3072 container_of(self, struct ring_buffer, cpu_notify);
3073 long cpu = (long)hcpu;
3076 case CPU_UP_PREPARE:
3077 case CPU_UP_PREPARE_FROZEN:
3078 if (cpu_isset(cpu, *buffer->cpumask))
3081 buffer->buffers[cpu] =
3082 rb_allocate_cpu_buffer(buffer, cpu);
3083 if (!buffer->buffers[cpu]) {
3084 WARN(1, "failed to allocate ring buffer on CPU %ld\n",
3089 cpu_set(cpu, *buffer->cpumask);
3091 case CPU_DOWN_PREPARE:
3092 case CPU_DOWN_PREPARE_FROZEN:
3095 * If we were to free the buffer, then the user would
3096 * lose any trace that was in the buffer.