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/kmemcheck.h>
14 #include <linux/module.h>
15 #include <linux/percpu.h>
16 #include <linux/mutex.h>
17 #include <linux/init.h>
18 #include <linux/hash.h>
19 #include <linux/list.h>
20 #include <linux/cpu.h>
26 * The ring buffer header is special. We must manually up keep it.
28 int ring_buffer_print_entry_header(struct trace_seq *s)
32 ret = trace_seq_printf(s, "# compressed entry header\n");
33 ret = trace_seq_printf(s, "\ttype_len : 5 bits\n");
34 ret = trace_seq_printf(s, "\ttime_delta : 27 bits\n");
35 ret = trace_seq_printf(s, "\tarray : 32 bits\n");
36 ret = trace_seq_printf(s, "\n");
37 ret = trace_seq_printf(s, "\tpadding : type == %d\n",
38 RINGBUF_TYPE_PADDING);
39 ret = trace_seq_printf(s, "\ttime_extend : type == %d\n",
40 RINGBUF_TYPE_TIME_EXTEND);
41 ret = trace_seq_printf(s, "\tdata max type_len == %d\n",
42 RINGBUF_TYPE_DATA_TYPE_LEN_MAX);
48 * The ring buffer is made up of a list of pages. A separate list of pages is
49 * allocated for each CPU. A writer may only write to a buffer that is
50 * associated with the CPU it is currently executing on. A reader may read
51 * from any per cpu buffer.
53 * The reader is special. For each per cpu buffer, the reader has its own
54 * reader page. When a reader has read the entire reader page, this reader
55 * page is swapped with another page in the ring buffer.
57 * Now, as long as the writer is off the reader page, the reader can do what
58 * ever it wants with that page. The writer will never write to that page
59 * again (as long as it is out of the ring buffer).
61 * Here's some silly ASCII art.
64 * |reader| RING BUFFER
66 * +------+ +---+ +---+ +---+
75 * |reader| RING BUFFER
76 * |page |------------------v
77 * +------+ +---+ +---+ +---+
86 * |reader| RING BUFFER
87 * |page |------------------v
88 * +------+ +---+ +---+ +---+
93 * +------------------------------+
97 * |buffer| RING BUFFER
98 * |page |------------------v
99 * +------+ +---+ +---+ +---+
101 * | New +---+ +---+ +---+
104 * +------------------------------+
107 * After we make this swap, the reader can hand this page off to the splice
108 * code and be done with it. It can even allocate a new page if it needs to
109 * and swap that into the ring buffer.
111 * We will be using cmpxchg soon to make all this lockless.
116 * A fast way to enable or disable all ring buffers is to
117 * call tracing_on or tracing_off. Turning off the ring buffers
118 * prevents all ring buffers from being recorded to.
119 * Turning this switch on, makes it OK to write to the
120 * ring buffer, if the ring buffer is enabled itself.
122 * There's three layers that must be on in order to write
123 * to the ring buffer.
125 * 1) This global flag must be set.
126 * 2) The ring buffer must be enabled for recording.
127 * 3) The per cpu buffer must be enabled for recording.
129 * In case of an anomaly, this global flag has a bit set that
130 * will permantly disable all ring buffers.
134 * Global flag to disable all recording to ring buffers
135 * This has two bits: ON, DISABLED
139 * 0 0 : ring buffers are off
140 * 1 0 : ring buffers are on
141 * X 1 : ring buffers are permanently disabled
145 RB_BUFFERS_ON_BIT = 0,
146 RB_BUFFERS_DISABLED_BIT = 1,
150 RB_BUFFERS_ON = 1 << RB_BUFFERS_ON_BIT,
151 RB_BUFFERS_DISABLED = 1 << RB_BUFFERS_DISABLED_BIT,
154 static unsigned long ring_buffer_flags __read_mostly = RB_BUFFERS_ON;
156 #define BUF_PAGE_HDR_SIZE offsetof(struct buffer_data_page, data)
159 * tracing_on - enable all tracing buffers
161 * This function enables all tracing buffers that may have been
162 * disabled with tracing_off.
164 void tracing_on(void)
166 set_bit(RB_BUFFERS_ON_BIT, &ring_buffer_flags);
168 EXPORT_SYMBOL_GPL(tracing_on);
171 * tracing_off - turn off all tracing buffers
173 * This function stops all tracing buffers from recording data.
174 * It does not disable any overhead the tracers themselves may
175 * be causing. This function simply causes all recording to
176 * the ring buffers to fail.
178 void tracing_off(void)
180 clear_bit(RB_BUFFERS_ON_BIT, &ring_buffer_flags);
182 EXPORT_SYMBOL_GPL(tracing_off);
185 * tracing_off_permanent - permanently disable ring buffers
187 * This function, once called, will disable all ring buffers
190 void tracing_off_permanent(void)
192 set_bit(RB_BUFFERS_DISABLED_BIT, &ring_buffer_flags);
196 * tracing_is_on - show state of ring buffers enabled
198 int tracing_is_on(void)
200 return ring_buffer_flags == RB_BUFFERS_ON;
202 EXPORT_SYMBOL_GPL(tracing_is_on);
206 #define RB_EVNT_HDR_SIZE (offsetof(struct ring_buffer_event, array))
207 #define RB_ALIGNMENT 4U
208 #define RB_MAX_SMALL_DATA (RB_ALIGNMENT * RINGBUF_TYPE_DATA_TYPE_LEN_MAX)
209 #define RB_EVNT_MIN_SIZE 8U /* two 32bit words */
211 /* define RINGBUF_TYPE_DATA for 'case RINGBUF_TYPE_DATA:' */
212 #define RINGBUF_TYPE_DATA 0 ... RINGBUF_TYPE_DATA_TYPE_LEN_MAX
215 RB_LEN_TIME_EXTEND = 8,
216 RB_LEN_TIME_STAMP = 16,
219 static inline int rb_null_event(struct ring_buffer_event *event)
221 return event->type_len == RINGBUF_TYPE_PADDING
222 && event->time_delta == 0;
225 static inline int rb_discarded_event(struct ring_buffer_event *event)
227 return event->type_len == RINGBUF_TYPE_PADDING && event->time_delta;
230 static void rb_event_set_padding(struct ring_buffer_event *event)
232 event->type_len = RINGBUF_TYPE_PADDING;
233 event->time_delta = 0;
237 rb_event_data_length(struct ring_buffer_event *event)
242 length = event->type_len * RB_ALIGNMENT;
244 length = event->array[0];
245 return length + RB_EVNT_HDR_SIZE;
248 /* inline for ring buffer fast paths */
250 rb_event_length(struct ring_buffer_event *event)
252 switch (event->type_len) {
253 case RINGBUF_TYPE_PADDING:
254 if (rb_null_event(event))
257 return event->array[0] + RB_EVNT_HDR_SIZE;
259 case RINGBUF_TYPE_TIME_EXTEND:
260 return RB_LEN_TIME_EXTEND;
262 case RINGBUF_TYPE_TIME_STAMP:
263 return RB_LEN_TIME_STAMP;
265 case RINGBUF_TYPE_DATA:
266 return rb_event_data_length(event);
275 * ring_buffer_event_length - return the length of the event
276 * @event: the event to get the length of
278 unsigned ring_buffer_event_length(struct ring_buffer_event *event)
280 unsigned length = rb_event_length(event);
281 if (event->type_len > RINGBUF_TYPE_DATA_TYPE_LEN_MAX)
283 length -= RB_EVNT_HDR_SIZE;
284 if (length > RB_MAX_SMALL_DATA + sizeof(event->array[0]))
285 length -= sizeof(event->array[0]);
288 EXPORT_SYMBOL_GPL(ring_buffer_event_length);
290 /* inline for ring buffer fast paths */
292 rb_event_data(struct ring_buffer_event *event)
294 BUG_ON(event->type_len > RINGBUF_TYPE_DATA_TYPE_LEN_MAX);
295 /* If length is in len field, then array[0] has the data */
297 return (void *)&event->array[0];
298 /* Otherwise length is in array[0] and array[1] has the data */
299 return (void *)&event->array[1];
303 * ring_buffer_event_data - return the data of the event
304 * @event: the event to get the data from
306 void *ring_buffer_event_data(struct ring_buffer_event *event)
308 return rb_event_data(event);
310 EXPORT_SYMBOL_GPL(ring_buffer_event_data);
312 #define for_each_buffer_cpu(buffer, cpu) \
313 for_each_cpu(cpu, buffer->cpumask)
316 #define TS_MASK ((1ULL << TS_SHIFT) - 1)
317 #define TS_DELTA_TEST (~TS_MASK)
319 struct buffer_data_page {
320 u64 time_stamp; /* page time stamp */
321 local_t commit; /* write committed index */
322 unsigned char data[]; /* data of buffer page */
326 struct list_head list; /* list of buffer pages */
327 local_t write; /* index for next write */
328 unsigned read; /* index for next read */
329 local_t entries; /* entries on this page */
330 struct buffer_data_page *page; /* Actual data page */
333 static void rb_init_page(struct buffer_data_page *bpage)
335 local_set(&bpage->commit, 0);
339 * ring_buffer_page_len - the size of data on the page.
340 * @page: The page to read
342 * Returns the amount of data on the page, including buffer page header.
344 size_t ring_buffer_page_len(void *page)
346 return local_read(&((struct buffer_data_page *)page)->commit)
351 * Also stolen from mm/slob.c. Thanks to Mathieu Desnoyers for pointing
354 static void free_buffer_page(struct buffer_page *bpage)
356 free_page((unsigned long)bpage->page);
361 * We need to fit the time_stamp delta into 27 bits.
363 static inline int test_time_stamp(u64 delta)
365 if (delta & TS_DELTA_TEST)
370 #define BUF_PAGE_SIZE (PAGE_SIZE - BUF_PAGE_HDR_SIZE)
372 /* Max payload is BUF_PAGE_SIZE - header (8bytes) */
373 #define BUF_MAX_DATA_SIZE (BUF_PAGE_SIZE - (sizeof(u32) * 2))
375 /* Max number of timestamps that can fit on a page */
376 #define RB_TIMESTAMPS_PER_PAGE (BUF_PAGE_SIZE / RB_LEN_TIME_STAMP)
378 int ring_buffer_print_page_header(struct trace_seq *s)
380 struct buffer_data_page field;
383 ret = trace_seq_printf(s, "\tfield: u64 timestamp;\t"
384 "offset:0;\tsize:%u;\n",
385 (unsigned int)sizeof(field.time_stamp));
387 ret = trace_seq_printf(s, "\tfield: local_t commit;\t"
388 "offset:%u;\tsize:%u;\n",
389 (unsigned int)offsetof(typeof(field), commit),
390 (unsigned int)sizeof(field.commit));
392 ret = trace_seq_printf(s, "\tfield: char data;\t"
393 "offset:%u;\tsize:%u;\n",
394 (unsigned int)offsetof(typeof(field), data),
395 (unsigned int)BUF_PAGE_SIZE);
401 * head_page == tail_page && head == tail then buffer is empty.
403 struct ring_buffer_per_cpu {
405 struct ring_buffer *buffer;
406 spinlock_t reader_lock; /* serialize readers */
408 struct lock_class_key lock_key;
409 struct list_head pages;
410 struct buffer_page *head_page; /* read from head */
411 struct buffer_page *tail_page; /* write to tail */
412 struct buffer_page *commit_page; /* committed pages */
413 struct buffer_page *reader_page;
414 unsigned long nmi_dropped;
415 unsigned long commit_overrun;
416 unsigned long overrun;
423 atomic_t record_disabled;
430 atomic_t record_disabled;
431 cpumask_var_t cpumask;
433 struct lock_class_key *reader_lock_key;
437 struct ring_buffer_per_cpu **buffers;
439 #ifdef CONFIG_HOTPLUG_CPU
440 struct notifier_block cpu_notify;
445 struct ring_buffer_iter {
446 struct ring_buffer_per_cpu *cpu_buffer;
448 struct buffer_page *head_page;
452 /* buffer may be either ring_buffer or ring_buffer_per_cpu */
453 #define RB_WARN_ON(buffer, cond) \
455 int _____ret = unlikely(cond); \
457 atomic_inc(&buffer->record_disabled); \
463 /* Up this if you want to test the TIME_EXTENTS and normalization */
464 #define DEBUG_SHIFT 0
466 static inline u64 rb_time_stamp(struct ring_buffer *buffer, int cpu)
468 /* shift to debug/test normalization and TIME_EXTENTS */
469 return buffer->clock() << DEBUG_SHIFT;
472 u64 ring_buffer_time_stamp(struct ring_buffer *buffer, int cpu)
476 preempt_disable_notrace();
477 time = rb_time_stamp(buffer, cpu);
478 preempt_enable_no_resched_notrace();
482 EXPORT_SYMBOL_GPL(ring_buffer_time_stamp);
484 void ring_buffer_normalize_time_stamp(struct ring_buffer *buffer,
487 /* Just stupid testing the normalize function and deltas */
490 EXPORT_SYMBOL_GPL(ring_buffer_normalize_time_stamp);
493 * check_pages - integrity check of buffer pages
494 * @cpu_buffer: CPU buffer with pages to test
496 * As a safety measure we check to make sure the data pages have not
499 static int rb_check_pages(struct ring_buffer_per_cpu *cpu_buffer)
501 struct list_head *head = &cpu_buffer->pages;
502 struct buffer_page *bpage, *tmp;
504 if (RB_WARN_ON(cpu_buffer, head->next->prev != head))
506 if (RB_WARN_ON(cpu_buffer, head->prev->next != head))
509 list_for_each_entry_safe(bpage, tmp, head, list) {
510 if (RB_WARN_ON(cpu_buffer,
511 bpage->list.next->prev != &bpage->list))
513 if (RB_WARN_ON(cpu_buffer,
514 bpage->list.prev->next != &bpage->list))
521 static int rb_allocate_pages(struct ring_buffer_per_cpu *cpu_buffer,
524 struct list_head *head = &cpu_buffer->pages;
525 struct buffer_page *bpage, *tmp;
530 for (i = 0; i < nr_pages; i++) {
531 bpage = kzalloc_node(ALIGN(sizeof(*bpage), cache_line_size()),
532 GFP_KERNEL, cpu_to_node(cpu_buffer->cpu));
535 list_add(&bpage->list, &pages);
537 addr = __get_free_page(GFP_KERNEL);
540 bpage->page = (void *)addr;
541 rb_init_page(bpage->page);
544 list_splice(&pages, head);
546 rb_check_pages(cpu_buffer);
551 list_for_each_entry_safe(bpage, tmp, &pages, list) {
552 list_del_init(&bpage->list);
553 free_buffer_page(bpage);
558 static struct ring_buffer_per_cpu *
559 rb_allocate_cpu_buffer(struct ring_buffer *buffer, int cpu)
561 struct ring_buffer_per_cpu *cpu_buffer;
562 struct buffer_page *bpage;
566 cpu_buffer = kzalloc_node(ALIGN(sizeof(*cpu_buffer), cache_line_size()),
567 GFP_KERNEL, cpu_to_node(cpu));
571 cpu_buffer->cpu = cpu;
572 cpu_buffer->buffer = buffer;
573 spin_lock_init(&cpu_buffer->reader_lock);
574 lockdep_set_class(&cpu_buffer->reader_lock, buffer->reader_lock_key);
575 cpu_buffer->lock = (raw_spinlock_t)__RAW_SPIN_LOCK_UNLOCKED;
576 INIT_LIST_HEAD(&cpu_buffer->pages);
578 bpage = kzalloc_node(ALIGN(sizeof(*bpage), cache_line_size()),
579 GFP_KERNEL, cpu_to_node(cpu));
581 goto fail_free_buffer;
583 cpu_buffer->reader_page = bpage;
584 addr = __get_free_page(GFP_KERNEL);
586 goto fail_free_reader;
587 bpage->page = (void *)addr;
588 rb_init_page(bpage->page);
590 INIT_LIST_HEAD(&cpu_buffer->reader_page->list);
592 ret = rb_allocate_pages(cpu_buffer, buffer->pages);
594 goto fail_free_reader;
596 cpu_buffer->head_page
597 = list_entry(cpu_buffer->pages.next, struct buffer_page, list);
598 cpu_buffer->tail_page = cpu_buffer->commit_page = cpu_buffer->head_page;
603 free_buffer_page(cpu_buffer->reader_page);
610 static void rb_free_cpu_buffer(struct ring_buffer_per_cpu *cpu_buffer)
612 struct list_head *head = &cpu_buffer->pages;
613 struct buffer_page *bpage, *tmp;
615 free_buffer_page(cpu_buffer->reader_page);
617 list_for_each_entry_safe(bpage, tmp, head, list) {
618 list_del_init(&bpage->list);
619 free_buffer_page(bpage);
624 #ifdef CONFIG_HOTPLUG_CPU
625 static int rb_cpu_notify(struct notifier_block *self,
626 unsigned long action, void *hcpu);
630 * ring_buffer_alloc - allocate a new ring_buffer
631 * @size: the size in bytes per cpu that is needed.
632 * @flags: attributes to set for the ring buffer.
634 * Currently the only flag that is available is the RB_FL_OVERWRITE
635 * flag. This flag means that the buffer will overwrite old data
636 * when the buffer wraps. If this flag is not set, the buffer will
637 * drop data when the tail hits the head.
639 struct ring_buffer *__ring_buffer_alloc(unsigned long size, unsigned flags,
640 struct lock_class_key *key)
642 struct ring_buffer *buffer;
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;
658 buffer->reader_lock_key = key;
660 /* need at least two pages */
661 if (buffer->pages < 2)
665 * In case of non-hotplug cpu, if the ring-buffer is allocated
666 * in early initcall, it will not be notified of secondary cpus.
667 * In that off case, we need to allocate for all possible cpus.
669 #ifdef CONFIG_HOTPLUG_CPU
671 cpumask_copy(buffer->cpumask, cpu_online_mask);
673 cpumask_copy(buffer->cpumask, cpu_possible_mask);
675 buffer->cpus = nr_cpu_ids;
677 bsize = sizeof(void *) * nr_cpu_ids;
678 buffer->buffers = kzalloc(ALIGN(bsize, cache_line_size()),
680 if (!buffer->buffers)
681 goto fail_free_cpumask;
683 for_each_buffer_cpu(buffer, cpu) {
684 buffer->buffers[cpu] =
685 rb_allocate_cpu_buffer(buffer, cpu);
686 if (!buffer->buffers[cpu])
687 goto fail_free_buffers;
690 #ifdef CONFIG_HOTPLUG_CPU
691 buffer->cpu_notify.notifier_call = rb_cpu_notify;
692 buffer->cpu_notify.priority = 0;
693 register_cpu_notifier(&buffer->cpu_notify);
697 mutex_init(&buffer->mutex);
702 for_each_buffer_cpu(buffer, cpu) {
703 if (buffer->buffers[cpu])
704 rb_free_cpu_buffer(buffer->buffers[cpu]);
706 kfree(buffer->buffers);
709 free_cpumask_var(buffer->cpumask);
716 EXPORT_SYMBOL_GPL(__ring_buffer_alloc);
719 * ring_buffer_free - free a ring buffer.
720 * @buffer: the buffer to free.
723 ring_buffer_free(struct ring_buffer *buffer)
729 #ifdef CONFIG_HOTPLUG_CPU
730 unregister_cpu_notifier(&buffer->cpu_notify);
733 for_each_buffer_cpu(buffer, cpu)
734 rb_free_cpu_buffer(buffer->buffers[cpu]);
738 free_cpumask_var(buffer->cpumask);
742 EXPORT_SYMBOL_GPL(ring_buffer_free);
744 void ring_buffer_set_clock(struct ring_buffer *buffer,
747 buffer->clock = clock;
750 static void rb_reset_cpu(struct ring_buffer_per_cpu *cpu_buffer);
753 rb_remove_pages(struct ring_buffer_per_cpu *cpu_buffer, unsigned nr_pages)
755 struct buffer_page *bpage;
759 atomic_inc(&cpu_buffer->record_disabled);
762 for (i = 0; i < nr_pages; i++) {
763 if (RB_WARN_ON(cpu_buffer, list_empty(&cpu_buffer->pages)))
765 p = cpu_buffer->pages.next;
766 bpage = list_entry(p, struct buffer_page, list);
767 list_del_init(&bpage->list);
768 free_buffer_page(bpage);
770 if (RB_WARN_ON(cpu_buffer, list_empty(&cpu_buffer->pages)))
773 rb_reset_cpu(cpu_buffer);
775 rb_check_pages(cpu_buffer);
777 atomic_dec(&cpu_buffer->record_disabled);
782 rb_insert_pages(struct ring_buffer_per_cpu *cpu_buffer,
783 struct list_head *pages, unsigned nr_pages)
785 struct buffer_page *bpage;
789 atomic_inc(&cpu_buffer->record_disabled);
792 for (i = 0; i < nr_pages; i++) {
793 if (RB_WARN_ON(cpu_buffer, list_empty(pages)))
796 bpage = list_entry(p, struct buffer_page, list);
797 list_del_init(&bpage->list);
798 list_add_tail(&bpage->list, &cpu_buffer->pages);
800 rb_reset_cpu(cpu_buffer);
802 rb_check_pages(cpu_buffer);
804 atomic_dec(&cpu_buffer->record_disabled);
808 * ring_buffer_resize - resize the ring buffer
809 * @buffer: the buffer to resize.
810 * @size: the new size.
812 * The tracer is responsible for making sure that the buffer is
813 * not being used while changing the size.
814 * Note: We may be able to change the above requirement by using
815 * RCU synchronizations.
817 * Minimum size is 2 * BUF_PAGE_SIZE.
819 * Returns -1 on failure.
821 int ring_buffer_resize(struct ring_buffer *buffer, unsigned long size)
823 struct ring_buffer_per_cpu *cpu_buffer;
824 unsigned nr_pages, rm_pages, new_pages;
825 struct buffer_page *bpage, *tmp;
826 unsigned long buffer_size;
832 * Always succeed at resizing a non-existent buffer:
837 size = DIV_ROUND_UP(size, BUF_PAGE_SIZE);
838 size *= BUF_PAGE_SIZE;
839 buffer_size = buffer->pages * BUF_PAGE_SIZE;
841 /* we need a minimum of two pages */
842 if (size < BUF_PAGE_SIZE * 2)
843 size = BUF_PAGE_SIZE * 2;
845 if (size == buffer_size)
848 mutex_lock(&buffer->mutex);
851 nr_pages = DIV_ROUND_UP(size, BUF_PAGE_SIZE);
853 if (size < buffer_size) {
855 /* easy case, just free pages */
856 if (RB_WARN_ON(buffer, nr_pages >= buffer->pages))
859 rm_pages = buffer->pages - nr_pages;
861 for_each_buffer_cpu(buffer, cpu) {
862 cpu_buffer = buffer->buffers[cpu];
863 rb_remove_pages(cpu_buffer, rm_pages);
869 * This is a bit more difficult. We only want to add pages
870 * when we can allocate enough for all CPUs. We do this
871 * by allocating all the pages and storing them on a local
872 * link list. If we succeed in our allocation, then we
873 * add these pages to the cpu_buffers. Otherwise we just free
874 * them all and return -ENOMEM;
876 if (RB_WARN_ON(buffer, nr_pages <= buffer->pages))
879 new_pages = nr_pages - buffer->pages;
881 for_each_buffer_cpu(buffer, cpu) {
882 for (i = 0; i < new_pages; i++) {
883 bpage = kzalloc_node(ALIGN(sizeof(*bpage),
885 GFP_KERNEL, cpu_to_node(cpu));
888 list_add(&bpage->list, &pages);
889 addr = __get_free_page(GFP_KERNEL);
892 bpage->page = (void *)addr;
893 rb_init_page(bpage->page);
897 for_each_buffer_cpu(buffer, cpu) {
898 cpu_buffer = buffer->buffers[cpu];
899 rb_insert_pages(cpu_buffer, &pages, new_pages);
902 if (RB_WARN_ON(buffer, !list_empty(&pages)))
906 buffer->pages = nr_pages;
908 mutex_unlock(&buffer->mutex);
913 list_for_each_entry_safe(bpage, tmp, &pages, list) {
914 list_del_init(&bpage->list);
915 free_buffer_page(bpage);
918 mutex_unlock(&buffer->mutex);
922 * Something went totally wrong, and we are too paranoid
923 * to even clean up the mess.
927 mutex_unlock(&buffer->mutex);
930 EXPORT_SYMBOL_GPL(ring_buffer_resize);
933 __rb_data_page_index(struct buffer_data_page *bpage, unsigned index)
935 return bpage->data + index;
938 static inline void *__rb_page_index(struct buffer_page *bpage, unsigned index)
940 return bpage->page->data + index;
943 static inline struct ring_buffer_event *
944 rb_reader_event(struct ring_buffer_per_cpu *cpu_buffer)
946 return __rb_page_index(cpu_buffer->reader_page,
947 cpu_buffer->reader_page->read);
950 static inline struct ring_buffer_event *
951 rb_head_event(struct ring_buffer_per_cpu *cpu_buffer)
953 return __rb_page_index(cpu_buffer->head_page,
954 cpu_buffer->head_page->read);
957 static inline struct ring_buffer_event *
958 rb_iter_head_event(struct ring_buffer_iter *iter)
960 return __rb_page_index(iter->head_page, iter->head);
963 static inline unsigned rb_page_write(struct buffer_page *bpage)
965 return local_read(&bpage->write);
968 static inline unsigned rb_page_commit(struct buffer_page *bpage)
970 return local_read(&bpage->page->commit);
973 /* Size is determined by what has been commited */
974 static inline unsigned rb_page_size(struct buffer_page *bpage)
976 return rb_page_commit(bpage);
979 static inline unsigned
980 rb_commit_index(struct ring_buffer_per_cpu *cpu_buffer)
982 return rb_page_commit(cpu_buffer->commit_page);
985 static inline unsigned rb_head_size(struct ring_buffer_per_cpu *cpu_buffer)
987 return rb_page_commit(cpu_buffer->head_page);
990 static inline void rb_inc_page(struct ring_buffer_per_cpu *cpu_buffer,
991 struct buffer_page **bpage)
993 struct list_head *p = (*bpage)->list.next;
995 if (p == &cpu_buffer->pages)
998 *bpage = list_entry(p, struct buffer_page, list);
1001 static inline unsigned
1002 rb_event_index(struct ring_buffer_event *event)
1004 unsigned long addr = (unsigned long)event;
1006 return (addr & ~PAGE_MASK) - BUF_PAGE_HDR_SIZE;
1010 rb_event_is_commit(struct ring_buffer_per_cpu *cpu_buffer,
1011 struct ring_buffer_event *event)
1013 unsigned long addr = (unsigned long)event;
1014 unsigned long index;
1016 index = rb_event_index(event);
1019 return cpu_buffer->commit_page->page == (void *)addr &&
1020 rb_commit_index(cpu_buffer) == index;
1024 rb_set_commit_to_write(struct ring_buffer_per_cpu *cpu_buffer)
1027 * We only race with interrupts and NMIs on this CPU.
1028 * If we own the commit event, then we can commit
1029 * all others that interrupted us, since the interruptions
1030 * are in stack format (they finish before they come
1031 * back to us). This allows us to do a simple loop to
1032 * assign the commit to the tail.
1035 while (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;
1041 /* add barrier to keep gcc from optimizing too much */
1044 while (rb_commit_index(cpu_buffer) !=
1045 rb_page_write(cpu_buffer->commit_page)) {
1046 cpu_buffer->commit_page->page->commit =
1047 cpu_buffer->commit_page->write;
1051 /* again, keep gcc from optimizing */
1055 * If an interrupt came in just after the first while loop
1056 * and pushed the tail page forward, we will be left with
1057 * a dangling commit that will never go forward.
1059 if (unlikely(cpu_buffer->commit_page != cpu_buffer->tail_page))
1063 static void rb_reset_reader_page(struct ring_buffer_per_cpu *cpu_buffer)
1065 cpu_buffer->read_stamp = cpu_buffer->reader_page->page->time_stamp;
1066 cpu_buffer->reader_page->read = 0;
1069 static void rb_inc_iter(struct ring_buffer_iter *iter)
1071 struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
1074 * The iterator could be on the reader page (it starts there).
1075 * But the head could have moved, since the reader was
1076 * found. Check for this case and assign the iterator
1077 * to the head page instead of next.
1079 if (iter->head_page == cpu_buffer->reader_page)
1080 iter->head_page = cpu_buffer->head_page;
1082 rb_inc_page(cpu_buffer, &iter->head_page);
1084 iter->read_stamp = iter->head_page->page->time_stamp;
1089 * ring_buffer_update_event - update event type and data
1090 * @event: the even to update
1091 * @type: the type of event
1092 * @length: the size of the event field in the ring buffer
1094 * Update the type and data fields of the event. The length
1095 * is the actual size that is written to the ring buffer,
1096 * and with this, we can determine what to place into the
1100 rb_update_event(struct ring_buffer_event *event,
1101 unsigned type, unsigned length)
1103 event->type_len = type;
1107 case RINGBUF_TYPE_PADDING:
1108 case RINGBUF_TYPE_TIME_EXTEND:
1109 case RINGBUF_TYPE_TIME_STAMP:
1113 length -= RB_EVNT_HDR_SIZE;
1114 if (length > RB_MAX_SMALL_DATA)
1115 event->array[0] = length;
1117 event->type_len = DIV_ROUND_UP(length, RB_ALIGNMENT);
1124 static unsigned rb_calculate_event_length(unsigned length)
1126 struct ring_buffer_event event; /* Used only for sizeof array */
1128 /* zero length can cause confusions */
1132 if (length > RB_MAX_SMALL_DATA)
1133 length += sizeof(event.array[0]);
1135 length += RB_EVNT_HDR_SIZE;
1136 length = ALIGN(length, RB_ALIGNMENT);
1142 rb_reset_tail(struct ring_buffer_per_cpu *cpu_buffer,
1143 struct buffer_page *tail_page,
1144 unsigned long tail, unsigned long length)
1146 struct ring_buffer_event *event;
1149 * Only the event that crossed the page boundary
1150 * must fill the old tail_page with padding.
1152 if (tail >= BUF_PAGE_SIZE) {
1153 local_sub(length, &tail_page->write);
1157 event = __rb_page_index(tail_page, tail);
1158 kmemcheck_annotate_bitfield(event, bitfield);
1161 * If this event is bigger than the minimum size, then
1162 * we need to be careful that we don't subtract the
1163 * write counter enough to allow another writer to slip
1165 * We put in a discarded commit instead, to make sure
1166 * that this space is not used again.
1168 * If we are less than the minimum size, we don't need to
1171 if (tail > (BUF_PAGE_SIZE - RB_EVNT_MIN_SIZE)) {
1172 /* No room for any events */
1174 /* Mark the rest of the page with padding */
1175 rb_event_set_padding(event);
1177 /* Set the write back to the previous setting */
1178 local_sub(length, &tail_page->write);
1182 /* Put in a discarded event */
1183 event->array[0] = (BUF_PAGE_SIZE - tail) - RB_EVNT_HDR_SIZE;
1184 event->type_len = RINGBUF_TYPE_PADDING;
1185 /* time delta must be non zero */
1186 event->time_delta = 1;
1187 /* Account for this as an entry */
1188 local_inc(&tail_page->entries);
1189 local_inc(&cpu_buffer->entries);
1191 /* Set write to end of buffer */
1192 length = (tail + length) - BUF_PAGE_SIZE;
1193 local_sub(length, &tail_page->write);
1196 static struct ring_buffer_event *
1197 rb_move_tail(struct ring_buffer_per_cpu *cpu_buffer,
1198 unsigned long length, unsigned long tail,
1199 struct buffer_page *commit_page,
1200 struct buffer_page *tail_page, u64 *ts)
1202 struct buffer_page *next_page, *head_page, *reader_page;
1203 struct ring_buffer *buffer = cpu_buffer->buffer;
1204 bool lock_taken = false;
1205 unsigned long flags;
1207 next_page = tail_page;
1209 local_irq_save(flags);
1211 * Since the write to the buffer is still not
1212 * fully lockless, we must be careful with NMIs.
1213 * The locks in the writers are taken when a write
1214 * crosses to a new page. The locks protect against
1215 * races with the readers (this will soon be fixed
1216 * with a lockless solution).
1218 * Because we can not protect against NMIs, and we
1219 * want to keep traces reentrant, we need to manage
1220 * what happens when we are in an NMI.
1222 * NMIs can happen after we take the lock.
1223 * If we are in an NMI, only take the lock
1224 * if it is not already taken. Otherwise
1227 if (unlikely(in_nmi())) {
1228 if (!__raw_spin_trylock(&cpu_buffer->lock)) {
1229 cpu_buffer->nmi_dropped++;
1233 __raw_spin_lock(&cpu_buffer->lock);
1237 rb_inc_page(cpu_buffer, &next_page);
1239 head_page = cpu_buffer->head_page;
1240 reader_page = cpu_buffer->reader_page;
1242 /* we grabbed the lock before incrementing */
1243 if (RB_WARN_ON(cpu_buffer, next_page == reader_page))
1247 * If for some reason, we had an interrupt storm that made
1248 * it all the way around the buffer, bail, and warn
1251 if (unlikely(next_page == commit_page)) {
1252 cpu_buffer->commit_overrun++;
1256 if (next_page == head_page) {
1257 if (!(buffer->flags & RB_FL_OVERWRITE))
1260 /* tail_page has not moved yet? */
1261 if (tail_page == cpu_buffer->tail_page) {
1262 /* count overflows */
1263 cpu_buffer->overrun +=
1264 local_read(&head_page->entries);
1266 rb_inc_page(cpu_buffer, &head_page);
1267 cpu_buffer->head_page = head_page;
1268 cpu_buffer->head_page->read = 0;
1273 * If the tail page is still the same as what we think
1274 * it is, then it is up to us to update the tail
1277 if (tail_page == cpu_buffer->tail_page) {
1278 local_set(&next_page->write, 0);
1279 local_set(&next_page->entries, 0);
1280 local_set(&next_page->page->commit, 0);
1281 cpu_buffer->tail_page = next_page;
1283 /* reread the time stamp */
1284 *ts = rb_time_stamp(buffer, cpu_buffer->cpu);
1285 cpu_buffer->tail_page->page->time_stamp = *ts;
1288 rb_reset_tail(cpu_buffer, tail_page, tail, length);
1290 __raw_spin_unlock(&cpu_buffer->lock);
1291 local_irq_restore(flags);
1293 /* fail and let the caller try again */
1294 return ERR_PTR(-EAGAIN);
1298 rb_reset_tail(cpu_buffer, tail_page, tail, length);
1300 if (likely(lock_taken))
1301 __raw_spin_unlock(&cpu_buffer->lock);
1302 local_irq_restore(flags);
1306 static struct ring_buffer_event *
1307 __rb_reserve_next(struct ring_buffer_per_cpu *cpu_buffer,
1308 unsigned type, unsigned long length, u64 *ts)
1310 struct buffer_page *tail_page, *commit_page;
1311 struct ring_buffer_event *event;
1312 unsigned long tail, write;
1314 commit_page = cpu_buffer->commit_page;
1315 /* we just need to protect against interrupts */
1317 tail_page = cpu_buffer->tail_page;
1318 write = local_add_return(length, &tail_page->write);
1319 tail = write - length;
1321 /* See if we shot pass the end of this buffer page */
1322 if (write > BUF_PAGE_SIZE)
1323 return rb_move_tail(cpu_buffer, length, tail,
1324 commit_page, tail_page, ts);
1326 /* We reserved something on the buffer */
1328 event = __rb_page_index(tail_page, tail);
1329 kmemcheck_annotate_bitfield(event, bitfield);
1330 rb_update_event(event, type, length);
1332 /* The passed in type is zero for DATA */
1334 local_inc(&tail_page->entries);
1337 * If this is the first commit on the page, then update
1341 tail_page->page->time_stamp = *ts;
1347 rb_try_to_discard(struct ring_buffer_per_cpu *cpu_buffer,
1348 struct ring_buffer_event *event)
1350 unsigned long new_index, old_index;
1351 struct buffer_page *bpage;
1352 unsigned long index;
1355 new_index = rb_event_index(event);
1356 old_index = new_index + rb_event_length(event);
1357 addr = (unsigned long)event;
1360 bpage = cpu_buffer->tail_page;
1362 if (bpage->page == (void *)addr && rb_page_write(bpage) == old_index) {
1364 * This is on the tail page. It is possible that
1365 * a write could come in and move the tail page
1366 * and write to the next page. That is fine
1367 * because we just shorten what is on this page.
1369 index = local_cmpxchg(&bpage->write, old_index, new_index);
1370 if (index == old_index)
1374 /* could not discard */
1379 rb_add_time_stamp(struct ring_buffer_per_cpu *cpu_buffer,
1380 u64 *ts, u64 *delta)
1382 struct ring_buffer_event *event;
1386 if (unlikely(*delta > (1ULL << 59) && !once++)) {
1387 printk(KERN_WARNING "Delta way too big! %llu"
1388 " ts=%llu write stamp = %llu\n",
1389 (unsigned long long)*delta,
1390 (unsigned long long)*ts,
1391 (unsigned long long)cpu_buffer->write_stamp);
1396 * The delta is too big, we to add a
1399 event = __rb_reserve_next(cpu_buffer,
1400 RINGBUF_TYPE_TIME_EXTEND,
1406 if (PTR_ERR(event) == -EAGAIN)
1409 /* Only a commited time event can update the write stamp */
1410 if (rb_event_is_commit(cpu_buffer, event)) {
1412 * If this is the first on the page, then it was
1413 * updated with the page itself. Try to discard it
1414 * and if we can't just make it zero.
1416 if (rb_event_index(event)) {
1417 event->time_delta = *delta & TS_MASK;
1418 event->array[0] = *delta >> TS_SHIFT;
1420 /* try to discard, since we do not need this */
1421 if (!rb_try_to_discard(cpu_buffer, event)) {
1422 /* nope, just zero it */
1423 event->time_delta = 0;
1424 event->array[0] = 0;
1427 cpu_buffer->write_stamp = *ts;
1428 /* let the caller know this was the commit */
1431 /* Try to discard the event */
1432 if (!rb_try_to_discard(cpu_buffer, event)) {
1433 /* Darn, this is just wasted space */
1434 event->time_delta = 0;
1435 event->array[0] = 0;
1445 static void rb_start_commit(struct ring_buffer_per_cpu *cpu_buffer)
1447 local_inc(&cpu_buffer->committing);
1448 local_inc(&cpu_buffer->commits);
1451 static void rb_end_commit(struct ring_buffer_per_cpu *cpu_buffer)
1453 unsigned long commits;
1455 if (RB_WARN_ON(cpu_buffer,
1456 !local_read(&cpu_buffer->committing)))
1460 commits = local_read(&cpu_buffer->commits);
1461 /* synchronize with interrupts */
1463 if (local_read(&cpu_buffer->committing) == 1)
1464 rb_set_commit_to_write(cpu_buffer);
1466 local_dec(&cpu_buffer->committing);
1468 /* synchronize with interrupts */
1472 * Need to account for interrupts coming in between the
1473 * updating of the commit page and the clearing of the
1474 * committing counter.
1476 if (unlikely(local_read(&cpu_buffer->commits) != commits) &&
1477 !local_read(&cpu_buffer->committing)) {
1478 local_inc(&cpu_buffer->committing);
1483 static struct ring_buffer_event *
1484 rb_reserve_next_event(struct ring_buffer_per_cpu *cpu_buffer,
1485 unsigned long length)
1487 struct ring_buffer_event *event;
1492 rb_start_commit(cpu_buffer);
1494 length = rb_calculate_event_length(length);
1497 * We allow for interrupts to reenter here and do a trace.
1498 * If one does, it will cause this original code to loop
1499 * back here. Even with heavy interrupts happening, this
1500 * should only happen a few times in a row. If this happens
1501 * 1000 times in a row, there must be either an interrupt
1502 * storm or we have something buggy.
1505 if (RB_WARN_ON(cpu_buffer, ++nr_loops > 1000))
1508 ts = rb_time_stamp(cpu_buffer->buffer, cpu_buffer->cpu);
1511 * Only the first commit can update the timestamp.
1512 * Yes there is a race here. If an interrupt comes in
1513 * just after the conditional and it traces too, then it
1514 * will also check the deltas. More than one timestamp may
1515 * also be made. But only the entry that did the actual
1516 * commit will be something other than zero.
1518 if (likely(cpu_buffer->tail_page == cpu_buffer->commit_page &&
1519 rb_page_write(cpu_buffer->tail_page) ==
1520 rb_commit_index(cpu_buffer))) {
1523 diff = ts - cpu_buffer->write_stamp;
1525 /* make sure this diff is calculated here */
1528 /* Did the write stamp get updated already? */
1529 if (unlikely(ts < cpu_buffer->write_stamp))
1533 if (unlikely(test_time_stamp(delta))) {
1535 commit = rb_add_time_stamp(cpu_buffer, &ts, &delta);
1536 if (commit == -EBUSY)
1539 if (commit == -EAGAIN)
1542 RB_WARN_ON(cpu_buffer, commit < 0);
1547 event = __rb_reserve_next(cpu_buffer, 0, length, &ts);
1548 if (unlikely(PTR_ERR(event) == -EAGAIN))
1554 if (!rb_event_is_commit(cpu_buffer, event))
1557 event->time_delta = delta;
1562 rb_end_commit(cpu_buffer);
1566 #define TRACE_RECURSIVE_DEPTH 16
1568 static int trace_recursive_lock(void)
1570 current->trace_recursion++;
1572 if (likely(current->trace_recursion < TRACE_RECURSIVE_DEPTH))
1575 /* Disable all tracing before we do anything else */
1576 tracing_off_permanent();
1578 printk_once(KERN_WARNING "Tracing recursion: depth[%ld]:"
1579 "HC[%lu]:SC[%lu]:NMI[%lu]\n",
1580 current->trace_recursion,
1581 hardirq_count() >> HARDIRQ_SHIFT,
1582 softirq_count() >> SOFTIRQ_SHIFT,
1589 static void trace_recursive_unlock(void)
1591 WARN_ON_ONCE(!current->trace_recursion);
1593 current->trace_recursion--;
1596 static DEFINE_PER_CPU(int, rb_need_resched);
1599 * ring_buffer_lock_reserve - reserve a part of the buffer
1600 * @buffer: the ring buffer to reserve from
1601 * @length: the length of the data to reserve (excluding event header)
1603 * Returns a reseverd event on the ring buffer to copy directly to.
1604 * The user of this interface will need to get the body to write into
1605 * and can use the ring_buffer_event_data() interface.
1607 * The length is the length of the data needed, not the event length
1608 * which also includes the event header.
1610 * Must be paired with ring_buffer_unlock_commit, unless NULL is returned.
1611 * If NULL is returned, then nothing has been allocated or locked.
1613 struct ring_buffer_event *
1614 ring_buffer_lock_reserve(struct ring_buffer *buffer, unsigned long length)
1616 struct ring_buffer_per_cpu *cpu_buffer;
1617 struct ring_buffer_event *event;
1620 if (ring_buffer_flags != RB_BUFFERS_ON)
1623 if (atomic_read(&buffer->record_disabled))
1626 /* If we are tracing schedule, we don't want to recurse */
1627 resched = ftrace_preempt_disable();
1629 if (trace_recursive_lock())
1632 cpu = raw_smp_processor_id();
1634 if (!cpumask_test_cpu(cpu, buffer->cpumask))
1637 cpu_buffer = buffer->buffers[cpu];
1639 if (atomic_read(&cpu_buffer->record_disabled))
1642 if (length > BUF_MAX_DATA_SIZE)
1645 event = rb_reserve_next_event(cpu_buffer, length);
1650 * Need to store resched state on this cpu.
1651 * Only the first needs to.
1654 if (preempt_count() == 1)
1655 per_cpu(rb_need_resched, cpu) = resched;
1660 trace_recursive_unlock();
1663 ftrace_preempt_enable(resched);
1666 EXPORT_SYMBOL_GPL(ring_buffer_lock_reserve);
1668 static void rb_commit(struct ring_buffer_per_cpu *cpu_buffer,
1669 struct ring_buffer_event *event)
1671 local_inc(&cpu_buffer->entries);
1674 * The event first in the commit queue updates the
1677 if (rb_event_is_commit(cpu_buffer, event))
1678 cpu_buffer->write_stamp += event->time_delta;
1680 rb_end_commit(cpu_buffer);
1684 * ring_buffer_unlock_commit - commit a reserved
1685 * @buffer: The buffer to commit to
1686 * @event: The event pointer to commit.
1688 * This commits the data to the ring buffer, and releases any locks held.
1690 * Must be paired with ring_buffer_lock_reserve.
1692 int ring_buffer_unlock_commit(struct ring_buffer *buffer,
1693 struct ring_buffer_event *event)
1695 struct ring_buffer_per_cpu *cpu_buffer;
1696 int cpu = raw_smp_processor_id();
1698 cpu_buffer = buffer->buffers[cpu];
1700 rb_commit(cpu_buffer, event);
1702 trace_recursive_unlock();
1705 * Only the last preempt count needs to restore preemption.
1707 if (preempt_count() == 1)
1708 ftrace_preempt_enable(per_cpu(rb_need_resched, cpu));
1710 preempt_enable_no_resched_notrace();
1714 EXPORT_SYMBOL_GPL(ring_buffer_unlock_commit);
1716 static inline void rb_event_discard(struct ring_buffer_event *event)
1718 /* array[0] holds the actual length for the discarded event */
1719 event->array[0] = rb_event_data_length(event) - RB_EVNT_HDR_SIZE;
1720 event->type_len = RINGBUF_TYPE_PADDING;
1721 /* time delta must be non zero */
1722 if (!event->time_delta)
1723 event->time_delta = 1;
1727 * ring_buffer_event_discard - discard any event in the ring buffer
1728 * @event: the event to discard
1730 * Sometimes a event that is in the ring buffer needs to be ignored.
1731 * This function lets the user discard an event in the ring buffer
1732 * and then that event will not be read later.
1734 * Note, it is up to the user to be careful with this, and protect
1735 * against races. If the user discards an event that has been consumed
1736 * it is possible that it could corrupt the ring buffer.
1738 void ring_buffer_event_discard(struct ring_buffer_event *event)
1740 rb_event_discard(event);
1742 EXPORT_SYMBOL_GPL(ring_buffer_event_discard);
1745 * ring_buffer_commit_discard - discard an event that has not been committed
1746 * @buffer: the ring buffer
1747 * @event: non committed event to discard
1749 * This is similar to ring_buffer_event_discard but must only be
1750 * performed on an event that has not been committed yet. The difference
1751 * is that this will also try to free the event from the ring buffer
1752 * if another event has not been added behind it.
1754 * If another event has been added behind it, it will set the event
1755 * up as discarded, and perform the commit.
1757 * If this function is called, do not call ring_buffer_unlock_commit on
1760 void ring_buffer_discard_commit(struct ring_buffer *buffer,
1761 struct ring_buffer_event *event)
1763 struct ring_buffer_per_cpu *cpu_buffer;
1766 /* The event is discarded regardless */
1767 rb_event_discard(event);
1769 cpu = smp_processor_id();
1770 cpu_buffer = buffer->buffers[cpu];
1773 * This must only be called if the event has not been
1774 * committed yet. Thus we can assume that preemption
1775 * is still disabled.
1777 RB_WARN_ON(buffer, !local_read(&cpu_buffer->committing));
1779 if (!rb_try_to_discard(cpu_buffer, event))
1783 * The commit is still visible by the reader, so we
1784 * must increment entries.
1786 local_inc(&cpu_buffer->entries);
1788 rb_end_commit(cpu_buffer);
1790 trace_recursive_unlock();
1793 * Only the last preempt count needs to restore preemption.
1795 if (preempt_count() == 1)
1796 ftrace_preempt_enable(per_cpu(rb_need_resched, cpu));
1798 preempt_enable_no_resched_notrace();
1801 EXPORT_SYMBOL_GPL(ring_buffer_discard_commit);
1804 * ring_buffer_write - write data to the buffer without reserving
1805 * @buffer: The ring buffer to write to.
1806 * @length: The length of the data being written (excluding the event header)
1807 * @data: The data to write to the buffer.
1809 * This is like ring_buffer_lock_reserve and ring_buffer_unlock_commit as
1810 * one function. If you already have the data to write to the buffer, it
1811 * may be easier to simply call this function.
1813 * Note, like ring_buffer_lock_reserve, the length is the length of the data
1814 * and not the length of the event which would hold the header.
1816 int ring_buffer_write(struct ring_buffer *buffer,
1817 unsigned long length,
1820 struct ring_buffer_per_cpu *cpu_buffer;
1821 struct ring_buffer_event *event;
1826 if (ring_buffer_flags != RB_BUFFERS_ON)
1829 if (atomic_read(&buffer->record_disabled))
1832 resched = ftrace_preempt_disable();
1834 cpu = raw_smp_processor_id();
1836 if (!cpumask_test_cpu(cpu, buffer->cpumask))
1839 cpu_buffer = buffer->buffers[cpu];
1841 if (atomic_read(&cpu_buffer->record_disabled))
1844 if (length > BUF_MAX_DATA_SIZE)
1847 event = rb_reserve_next_event(cpu_buffer, length);
1851 body = rb_event_data(event);
1853 memcpy(body, data, length);
1855 rb_commit(cpu_buffer, event);
1859 ftrace_preempt_enable(resched);
1863 EXPORT_SYMBOL_GPL(ring_buffer_write);
1865 static int rb_per_cpu_empty(struct ring_buffer_per_cpu *cpu_buffer)
1867 struct buffer_page *reader = cpu_buffer->reader_page;
1868 struct buffer_page *head = cpu_buffer->head_page;
1869 struct buffer_page *commit = cpu_buffer->commit_page;
1871 return reader->read == rb_page_commit(reader) &&
1872 (commit == reader ||
1874 head->read == rb_page_commit(commit)));
1878 * ring_buffer_record_disable - stop all writes into the buffer
1879 * @buffer: The ring buffer to stop writes to.
1881 * This prevents all writes to the buffer. Any attempt to write
1882 * to the buffer after this will fail and return NULL.
1884 * The caller should call synchronize_sched() after this.
1886 void ring_buffer_record_disable(struct ring_buffer *buffer)
1888 atomic_inc(&buffer->record_disabled);
1890 EXPORT_SYMBOL_GPL(ring_buffer_record_disable);
1893 * ring_buffer_record_enable - enable writes to the buffer
1894 * @buffer: The ring buffer to enable writes
1896 * Note, multiple disables will need the same number of enables
1897 * to truely enable the writing (much like preempt_disable).
1899 void ring_buffer_record_enable(struct ring_buffer *buffer)
1901 atomic_dec(&buffer->record_disabled);
1903 EXPORT_SYMBOL_GPL(ring_buffer_record_enable);
1906 * ring_buffer_record_disable_cpu - stop all writes into the cpu_buffer
1907 * @buffer: The ring buffer to stop writes to.
1908 * @cpu: The CPU buffer to stop
1910 * This prevents all writes to the buffer. Any attempt to write
1911 * to the buffer after this will fail and return NULL.
1913 * The caller should call synchronize_sched() after this.
1915 void ring_buffer_record_disable_cpu(struct ring_buffer *buffer, int cpu)
1917 struct ring_buffer_per_cpu *cpu_buffer;
1919 if (!cpumask_test_cpu(cpu, buffer->cpumask))
1922 cpu_buffer = buffer->buffers[cpu];
1923 atomic_inc(&cpu_buffer->record_disabled);
1925 EXPORT_SYMBOL_GPL(ring_buffer_record_disable_cpu);
1928 * ring_buffer_record_enable_cpu - enable writes to the buffer
1929 * @buffer: The ring buffer to enable writes
1930 * @cpu: The CPU to enable.
1932 * Note, multiple disables will need the same number of enables
1933 * to truely enable the writing (much like preempt_disable).
1935 void ring_buffer_record_enable_cpu(struct ring_buffer *buffer, int cpu)
1937 struct ring_buffer_per_cpu *cpu_buffer;
1939 if (!cpumask_test_cpu(cpu, buffer->cpumask))
1942 cpu_buffer = buffer->buffers[cpu];
1943 atomic_dec(&cpu_buffer->record_disabled);
1945 EXPORT_SYMBOL_GPL(ring_buffer_record_enable_cpu);
1948 * ring_buffer_entries_cpu - get the number of entries in a cpu buffer
1949 * @buffer: The ring buffer
1950 * @cpu: The per CPU buffer to get the entries from.
1952 unsigned long ring_buffer_entries_cpu(struct ring_buffer *buffer, int cpu)
1954 struct ring_buffer_per_cpu *cpu_buffer;
1957 if (!cpumask_test_cpu(cpu, buffer->cpumask))
1960 cpu_buffer = buffer->buffers[cpu];
1961 ret = (local_read(&cpu_buffer->entries) - cpu_buffer->overrun)
1966 EXPORT_SYMBOL_GPL(ring_buffer_entries_cpu);
1969 * ring_buffer_overrun_cpu - get the number of overruns in a cpu_buffer
1970 * @buffer: The ring buffer
1971 * @cpu: The per CPU buffer to get the number of overruns from
1973 unsigned long ring_buffer_overrun_cpu(struct ring_buffer *buffer, int cpu)
1975 struct ring_buffer_per_cpu *cpu_buffer;
1978 if (!cpumask_test_cpu(cpu, buffer->cpumask))
1981 cpu_buffer = buffer->buffers[cpu];
1982 ret = cpu_buffer->overrun;
1986 EXPORT_SYMBOL_GPL(ring_buffer_overrun_cpu);
1989 * ring_buffer_nmi_dropped_cpu - get the number of nmis that were dropped
1990 * @buffer: The ring buffer
1991 * @cpu: The per CPU buffer to get the number of overruns from
1993 unsigned long ring_buffer_nmi_dropped_cpu(struct ring_buffer *buffer, int cpu)
1995 struct ring_buffer_per_cpu *cpu_buffer;
1998 if (!cpumask_test_cpu(cpu, buffer->cpumask))
2001 cpu_buffer = buffer->buffers[cpu];
2002 ret = cpu_buffer->nmi_dropped;
2006 EXPORT_SYMBOL_GPL(ring_buffer_nmi_dropped_cpu);
2009 * ring_buffer_commit_overrun_cpu - get the number of overruns caused by commits
2010 * @buffer: The ring buffer
2011 * @cpu: The per CPU buffer to get the number of overruns from
2014 ring_buffer_commit_overrun_cpu(struct ring_buffer *buffer, int cpu)
2016 struct ring_buffer_per_cpu *cpu_buffer;
2019 if (!cpumask_test_cpu(cpu, buffer->cpumask))
2022 cpu_buffer = buffer->buffers[cpu];
2023 ret = cpu_buffer->commit_overrun;
2027 EXPORT_SYMBOL_GPL(ring_buffer_commit_overrun_cpu);
2030 * ring_buffer_entries - get the number of entries in a buffer
2031 * @buffer: The ring buffer
2033 * Returns the total number of entries in the ring buffer
2036 unsigned long ring_buffer_entries(struct ring_buffer *buffer)
2038 struct ring_buffer_per_cpu *cpu_buffer;
2039 unsigned long entries = 0;
2042 /* if you care about this being correct, lock the buffer */
2043 for_each_buffer_cpu(buffer, cpu) {
2044 cpu_buffer = buffer->buffers[cpu];
2045 entries += (local_read(&cpu_buffer->entries) -
2046 cpu_buffer->overrun) - cpu_buffer->read;
2051 EXPORT_SYMBOL_GPL(ring_buffer_entries);
2054 * ring_buffer_overrun_cpu - get the number of overruns in buffer
2055 * @buffer: The ring buffer
2057 * Returns the total number of overruns in the ring buffer
2060 unsigned long ring_buffer_overruns(struct ring_buffer *buffer)
2062 struct ring_buffer_per_cpu *cpu_buffer;
2063 unsigned long overruns = 0;
2066 /* if you care about this being correct, lock the buffer */
2067 for_each_buffer_cpu(buffer, cpu) {
2068 cpu_buffer = buffer->buffers[cpu];
2069 overruns += cpu_buffer->overrun;
2074 EXPORT_SYMBOL_GPL(ring_buffer_overruns);
2076 static void rb_iter_reset(struct ring_buffer_iter *iter)
2078 struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
2080 /* Iterator usage is expected to have record disabled */
2081 if (list_empty(&cpu_buffer->reader_page->list)) {
2082 iter->head_page = cpu_buffer->head_page;
2083 iter->head = cpu_buffer->head_page->read;
2085 iter->head_page = cpu_buffer->reader_page;
2086 iter->head = cpu_buffer->reader_page->read;
2089 iter->read_stamp = cpu_buffer->read_stamp;
2091 iter->read_stamp = iter->head_page->page->time_stamp;
2095 * ring_buffer_iter_reset - reset an iterator
2096 * @iter: The iterator to reset
2098 * Resets the iterator, so that it will start from the beginning
2101 void ring_buffer_iter_reset(struct ring_buffer_iter *iter)
2103 struct ring_buffer_per_cpu *cpu_buffer;
2104 unsigned long flags;
2109 cpu_buffer = iter->cpu_buffer;
2111 spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
2112 rb_iter_reset(iter);
2113 spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
2115 EXPORT_SYMBOL_GPL(ring_buffer_iter_reset);
2118 * ring_buffer_iter_empty - check if an iterator has no more to read
2119 * @iter: The iterator to check
2121 int ring_buffer_iter_empty(struct ring_buffer_iter *iter)
2123 struct ring_buffer_per_cpu *cpu_buffer;
2125 cpu_buffer = iter->cpu_buffer;
2127 return iter->head_page == cpu_buffer->commit_page &&
2128 iter->head == rb_commit_index(cpu_buffer);
2130 EXPORT_SYMBOL_GPL(ring_buffer_iter_empty);
2133 rb_update_read_stamp(struct ring_buffer_per_cpu *cpu_buffer,
2134 struct ring_buffer_event *event)
2138 switch (event->type_len) {
2139 case RINGBUF_TYPE_PADDING:
2142 case RINGBUF_TYPE_TIME_EXTEND:
2143 delta = event->array[0];
2145 delta += event->time_delta;
2146 cpu_buffer->read_stamp += delta;
2149 case RINGBUF_TYPE_TIME_STAMP:
2150 /* FIXME: not implemented */
2153 case RINGBUF_TYPE_DATA:
2154 cpu_buffer->read_stamp += event->time_delta;
2164 rb_update_iter_read_stamp(struct ring_buffer_iter *iter,
2165 struct ring_buffer_event *event)
2169 switch (event->type_len) {
2170 case RINGBUF_TYPE_PADDING:
2173 case RINGBUF_TYPE_TIME_EXTEND:
2174 delta = event->array[0];
2176 delta += event->time_delta;
2177 iter->read_stamp += delta;
2180 case RINGBUF_TYPE_TIME_STAMP:
2181 /* FIXME: not implemented */
2184 case RINGBUF_TYPE_DATA:
2185 iter->read_stamp += event->time_delta;
2194 static struct buffer_page *
2195 rb_get_reader_page(struct ring_buffer_per_cpu *cpu_buffer)
2197 struct buffer_page *reader = NULL;
2198 unsigned long flags;
2201 local_irq_save(flags);
2202 __raw_spin_lock(&cpu_buffer->lock);
2206 * This should normally only loop twice. But because the
2207 * start of the reader inserts an empty page, it causes
2208 * a case where we will loop three times. There should be no
2209 * reason to loop four times (that I know of).
2211 if (RB_WARN_ON(cpu_buffer, ++nr_loops > 3)) {
2216 reader = cpu_buffer->reader_page;
2218 /* If there's more to read, return this page */
2219 if (cpu_buffer->reader_page->read < rb_page_size(reader))
2222 /* Never should we have an index greater than the size */
2223 if (RB_WARN_ON(cpu_buffer,
2224 cpu_buffer->reader_page->read > rb_page_size(reader)))
2227 /* check if we caught up to the tail */
2229 if (cpu_buffer->commit_page == cpu_buffer->reader_page)
2233 * Splice the empty reader page into the list around the head.
2234 * Reset the reader page to size zero.
2237 reader = cpu_buffer->head_page;
2238 cpu_buffer->reader_page->list.next = reader->list.next;
2239 cpu_buffer->reader_page->list.prev = reader->list.prev;
2241 local_set(&cpu_buffer->reader_page->write, 0);
2242 local_set(&cpu_buffer->reader_page->entries, 0);
2243 local_set(&cpu_buffer->reader_page->page->commit, 0);
2245 /* Make the reader page now replace the head */
2246 reader->list.prev->next = &cpu_buffer->reader_page->list;
2247 reader->list.next->prev = &cpu_buffer->reader_page->list;
2250 * If the tail is on the reader, then we must set the head
2251 * to the inserted page, otherwise we set it one before.
2253 cpu_buffer->head_page = cpu_buffer->reader_page;
2255 if (cpu_buffer->commit_page != reader)
2256 rb_inc_page(cpu_buffer, &cpu_buffer->head_page);
2258 /* Finally update the reader page to the new head */
2259 cpu_buffer->reader_page = reader;
2260 rb_reset_reader_page(cpu_buffer);
2265 __raw_spin_unlock(&cpu_buffer->lock);
2266 local_irq_restore(flags);
2271 static void rb_advance_reader(struct ring_buffer_per_cpu *cpu_buffer)
2273 struct ring_buffer_event *event;
2274 struct buffer_page *reader;
2277 reader = rb_get_reader_page(cpu_buffer);
2279 /* This function should not be called when buffer is empty */
2280 if (RB_WARN_ON(cpu_buffer, !reader))
2283 event = rb_reader_event(cpu_buffer);
2285 if (event->type_len <= RINGBUF_TYPE_DATA_TYPE_LEN_MAX
2286 || rb_discarded_event(event))
2289 rb_update_read_stamp(cpu_buffer, event);
2291 length = rb_event_length(event);
2292 cpu_buffer->reader_page->read += length;
2295 static void rb_advance_iter(struct ring_buffer_iter *iter)
2297 struct ring_buffer *buffer;
2298 struct ring_buffer_per_cpu *cpu_buffer;
2299 struct ring_buffer_event *event;
2302 cpu_buffer = iter->cpu_buffer;
2303 buffer = cpu_buffer->buffer;
2306 * Check if we are at the end of the buffer.
2308 if (iter->head >= rb_page_size(iter->head_page)) {
2309 /* discarded commits can make the page empty */
2310 if (iter->head_page == cpu_buffer->commit_page)
2316 event = rb_iter_head_event(iter);
2318 length = rb_event_length(event);
2321 * This should not be called to advance the header if we are
2322 * at the tail of the buffer.
2324 if (RB_WARN_ON(cpu_buffer,
2325 (iter->head_page == cpu_buffer->commit_page) &&
2326 (iter->head + length > rb_commit_index(cpu_buffer))))
2329 rb_update_iter_read_stamp(iter, event);
2331 iter->head += length;
2333 /* check for end of page padding */
2334 if ((iter->head >= rb_page_size(iter->head_page)) &&
2335 (iter->head_page != cpu_buffer->commit_page))
2336 rb_advance_iter(iter);
2339 static struct ring_buffer_event *
2340 rb_buffer_peek(struct ring_buffer *buffer, int cpu, u64 *ts)
2342 struct ring_buffer_per_cpu *cpu_buffer;
2343 struct ring_buffer_event *event;
2344 struct buffer_page *reader;
2347 cpu_buffer = buffer->buffers[cpu];
2351 * We repeat when a timestamp is encountered. It is possible
2352 * to get multiple timestamps from an interrupt entering just
2353 * as one timestamp is about to be written, or from discarded
2354 * commits. The most that we can have is the number on a single page.
2356 if (RB_WARN_ON(cpu_buffer, ++nr_loops > RB_TIMESTAMPS_PER_PAGE))
2359 reader = rb_get_reader_page(cpu_buffer);
2363 event = rb_reader_event(cpu_buffer);
2365 switch (event->type_len) {
2366 case RINGBUF_TYPE_PADDING:
2367 if (rb_null_event(event))
2368 RB_WARN_ON(cpu_buffer, 1);
2370 * Because the writer could be discarding every
2371 * event it creates (which would probably be bad)
2372 * if we were to go back to "again" then we may never
2373 * catch up, and will trigger the warn on, or lock
2374 * the box. Return the padding, and we will release
2375 * the current locks, and try again.
2377 rb_advance_reader(cpu_buffer);
2380 case RINGBUF_TYPE_TIME_EXTEND:
2381 /* Internal data, OK to advance */
2382 rb_advance_reader(cpu_buffer);
2385 case RINGBUF_TYPE_TIME_STAMP:
2386 /* FIXME: not implemented */
2387 rb_advance_reader(cpu_buffer);
2390 case RINGBUF_TYPE_DATA:
2392 *ts = cpu_buffer->read_stamp + event->time_delta;
2393 ring_buffer_normalize_time_stamp(buffer,
2394 cpu_buffer->cpu, ts);
2404 EXPORT_SYMBOL_GPL(ring_buffer_peek);
2406 static struct ring_buffer_event *
2407 rb_iter_peek(struct ring_buffer_iter *iter, u64 *ts)
2409 struct ring_buffer *buffer;
2410 struct ring_buffer_per_cpu *cpu_buffer;
2411 struct ring_buffer_event *event;
2414 if (ring_buffer_iter_empty(iter))
2417 cpu_buffer = iter->cpu_buffer;
2418 buffer = cpu_buffer->buffer;
2422 * We repeat when a timestamp is encountered.
2423 * We can get multiple timestamps by nested interrupts or also
2424 * if filtering is on (discarding commits). Since discarding
2425 * commits can be frequent we can get a lot of timestamps.
2426 * But we limit them by not adding timestamps if they begin
2427 * at the start of a page.
2429 if (RB_WARN_ON(cpu_buffer, ++nr_loops > RB_TIMESTAMPS_PER_PAGE))
2432 if (rb_per_cpu_empty(cpu_buffer))
2435 event = rb_iter_head_event(iter);
2437 switch (event->type_len) {
2438 case RINGBUF_TYPE_PADDING:
2439 if (rb_null_event(event)) {
2443 rb_advance_iter(iter);
2446 case RINGBUF_TYPE_TIME_EXTEND:
2447 /* Internal data, OK to advance */
2448 rb_advance_iter(iter);
2451 case RINGBUF_TYPE_TIME_STAMP:
2452 /* FIXME: not implemented */
2453 rb_advance_iter(iter);
2456 case RINGBUF_TYPE_DATA:
2458 *ts = iter->read_stamp + event->time_delta;
2459 ring_buffer_normalize_time_stamp(buffer,
2460 cpu_buffer->cpu, ts);
2470 EXPORT_SYMBOL_GPL(ring_buffer_iter_peek);
2472 static inline int rb_ok_to_lock(void)
2475 * If an NMI die dumps out the content of the ring buffer
2476 * do not grab locks. We also permanently disable the ring
2477 * buffer too. A one time deal is all you get from reading
2478 * the ring buffer from an NMI.
2480 if (likely(!in_nmi() && !oops_in_progress))
2483 tracing_off_permanent();
2488 * ring_buffer_peek - peek at the next event to be read
2489 * @buffer: The ring buffer to read
2490 * @cpu: The cpu to peak at
2491 * @ts: The timestamp counter of this event.
2493 * This will return the event that will be read next, but does
2494 * not consume the data.
2496 struct ring_buffer_event *
2497 ring_buffer_peek(struct ring_buffer *buffer, int cpu, u64 *ts)
2499 struct ring_buffer_per_cpu *cpu_buffer = buffer->buffers[cpu];
2500 struct ring_buffer_event *event;
2501 unsigned long flags;
2504 if (!cpumask_test_cpu(cpu, buffer->cpumask))
2507 dolock = rb_ok_to_lock();
2509 local_irq_save(flags);
2511 spin_lock(&cpu_buffer->reader_lock);
2512 event = rb_buffer_peek(buffer, cpu, ts);
2514 spin_unlock(&cpu_buffer->reader_lock);
2515 local_irq_restore(flags);
2517 if (event && event->type_len == RINGBUF_TYPE_PADDING) {
2526 * ring_buffer_iter_peek - peek at the next event to be read
2527 * @iter: The ring buffer iterator
2528 * @ts: The timestamp counter of this event.
2530 * This will return the event that will be read next, but does
2531 * not increment the iterator.
2533 struct ring_buffer_event *
2534 ring_buffer_iter_peek(struct ring_buffer_iter *iter, u64 *ts)
2536 struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
2537 struct ring_buffer_event *event;
2538 unsigned long flags;
2541 spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
2542 event = rb_iter_peek(iter, ts);
2543 spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
2545 if (event && event->type_len == RINGBUF_TYPE_PADDING) {
2554 * ring_buffer_consume - return an event and consume it
2555 * @buffer: The ring buffer to get the next event from
2557 * Returns the next event in the ring buffer, and that event is consumed.
2558 * Meaning, that sequential reads will keep returning a different event,
2559 * and eventually empty the ring buffer if the producer is slower.
2561 struct ring_buffer_event *
2562 ring_buffer_consume(struct ring_buffer *buffer, int cpu, u64 *ts)
2564 struct ring_buffer_per_cpu *cpu_buffer;
2565 struct ring_buffer_event *event = NULL;
2566 unsigned long flags;
2569 dolock = rb_ok_to_lock();
2572 /* might be called in atomic */
2575 if (!cpumask_test_cpu(cpu, buffer->cpumask))
2578 cpu_buffer = buffer->buffers[cpu];
2579 local_irq_save(flags);
2581 spin_lock(&cpu_buffer->reader_lock);
2583 event = rb_buffer_peek(buffer, cpu, ts);
2587 rb_advance_reader(cpu_buffer);
2591 spin_unlock(&cpu_buffer->reader_lock);
2592 local_irq_restore(flags);
2597 if (event && event->type_len == RINGBUF_TYPE_PADDING) {
2604 EXPORT_SYMBOL_GPL(ring_buffer_consume);
2607 * ring_buffer_read_start - start a non consuming read of the buffer
2608 * @buffer: The ring buffer to read from
2609 * @cpu: The cpu buffer to iterate over
2611 * This starts up an iteration through the buffer. It also disables
2612 * the recording to the buffer until the reading is finished.
2613 * This prevents the reading from being corrupted. This is not
2614 * a consuming read, so a producer is not expected.
2616 * Must be paired with ring_buffer_finish.
2618 struct ring_buffer_iter *
2619 ring_buffer_read_start(struct ring_buffer *buffer, int cpu)
2621 struct ring_buffer_per_cpu *cpu_buffer;
2622 struct ring_buffer_iter *iter;
2623 unsigned long flags;
2625 if (!cpumask_test_cpu(cpu, buffer->cpumask))
2628 iter = kmalloc(sizeof(*iter), GFP_KERNEL);
2632 cpu_buffer = buffer->buffers[cpu];
2634 iter->cpu_buffer = cpu_buffer;
2636 atomic_inc(&cpu_buffer->record_disabled);
2637 synchronize_sched();
2639 spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
2640 __raw_spin_lock(&cpu_buffer->lock);
2641 rb_iter_reset(iter);
2642 __raw_spin_unlock(&cpu_buffer->lock);
2643 spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
2647 EXPORT_SYMBOL_GPL(ring_buffer_read_start);
2650 * ring_buffer_finish - finish reading the iterator of the buffer
2651 * @iter: The iterator retrieved by ring_buffer_start
2653 * This re-enables the recording to the buffer, and frees the
2657 ring_buffer_read_finish(struct ring_buffer_iter *iter)
2659 struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
2661 atomic_dec(&cpu_buffer->record_disabled);
2664 EXPORT_SYMBOL_GPL(ring_buffer_read_finish);
2667 * ring_buffer_read - read the next item in the ring buffer by the iterator
2668 * @iter: The ring buffer iterator
2669 * @ts: The time stamp of the event read.
2671 * This reads the next event in the ring buffer and increments the iterator.
2673 struct ring_buffer_event *
2674 ring_buffer_read(struct ring_buffer_iter *iter, u64 *ts)
2676 struct ring_buffer_event *event;
2677 struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
2678 unsigned long flags;
2681 spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
2682 event = rb_iter_peek(iter, ts);
2686 rb_advance_iter(iter);
2688 spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
2690 if (event && event->type_len == RINGBUF_TYPE_PADDING) {
2697 EXPORT_SYMBOL_GPL(ring_buffer_read);
2700 * ring_buffer_size - return the size of the ring buffer (in bytes)
2701 * @buffer: The ring buffer.
2703 unsigned long ring_buffer_size(struct ring_buffer *buffer)
2705 return BUF_PAGE_SIZE * buffer->pages;
2707 EXPORT_SYMBOL_GPL(ring_buffer_size);
2710 rb_reset_cpu(struct ring_buffer_per_cpu *cpu_buffer)
2712 cpu_buffer->head_page
2713 = list_entry(cpu_buffer->pages.next, struct buffer_page, list);
2714 local_set(&cpu_buffer->head_page->write, 0);
2715 local_set(&cpu_buffer->head_page->entries, 0);
2716 local_set(&cpu_buffer->head_page->page->commit, 0);
2718 cpu_buffer->head_page->read = 0;
2720 cpu_buffer->tail_page = cpu_buffer->head_page;
2721 cpu_buffer->commit_page = cpu_buffer->head_page;
2723 INIT_LIST_HEAD(&cpu_buffer->reader_page->list);
2724 local_set(&cpu_buffer->reader_page->write, 0);
2725 local_set(&cpu_buffer->reader_page->entries, 0);
2726 local_set(&cpu_buffer->reader_page->page->commit, 0);
2727 cpu_buffer->reader_page->read = 0;
2729 cpu_buffer->nmi_dropped = 0;
2730 cpu_buffer->commit_overrun = 0;
2731 cpu_buffer->overrun = 0;
2732 cpu_buffer->read = 0;
2733 local_set(&cpu_buffer->entries, 0);
2734 local_set(&cpu_buffer->committing, 0);
2735 local_set(&cpu_buffer->commits, 0);
2737 cpu_buffer->write_stamp = 0;
2738 cpu_buffer->read_stamp = 0;
2742 * ring_buffer_reset_cpu - reset a ring buffer per CPU buffer
2743 * @buffer: The ring buffer to reset a per cpu buffer of
2744 * @cpu: The CPU buffer to be reset
2746 void ring_buffer_reset_cpu(struct ring_buffer *buffer, int cpu)
2748 struct ring_buffer_per_cpu *cpu_buffer = buffer->buffers[cpu];
2749 unsigned long flags;
2751 if (!cpumask_test_cpu(cpu, buffer->cpumask))
2754 atomic_inc(&cpu_buffer->record_disabled);
2756 spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
2758 __raw_spin_lock(&cpu_buffer->lock);
2760 rb_reset_cpu(cpu_buffer);
2762 __raw_spin_unlock(&cpu_buffer->lock);
2764 spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
2766 atomic_dec(&cpu_buffer->record_disabled);
2768 EXPORT_SYMBOL_GPL(ring_buffer_reset_cpu);
2771 * ring_buffer_reset - reset a ring buffer
2772 * @buffer: The ring buffer to reset all cpu buffers
2774 void ring_buffer_reset(struct ring_buffer *buffer)
2778 for_each_buffer_cpu(buffer, cpu)
2779 ring_buffer_reset_cpu(buffer, cpu);
2781 EXPORT_SYMBOL_GPL(ring_buffer_reset);
2784 * rind_buffer_empty - is the ring buffer empty?
2785 * @buffer: The ring buffer to test
2787 int ring_buffer_empty(struct ring_buffer *buffer)
2789 struct ring_buffer_per_cpu *cpu_buffer;
2790 unsigned long flags;
2795 dolock = rb_ok_to_lock();
2797 /* yes this is racy, but if you don't like the race, lock the buffer */
2798 for_each_buffer_cpu(buffer, cpu) {
2799 cpu_buffer = buffer->buffers[cpu];
2800 local_irq_save(flags);
2802 spin_lock(&cpu_buffer->reader_lock);
2803 ret = rb_per_cpu_empty(cpu_buffer);
2805 spin_unlock(&cpu_buffer->reader_lock);
2806 local_irq_restore(flags);
2814 EXPORT_SYMBOL_GPL(ring_buffer_empty);
2817 * ring_buffer_empty_cpu - is a cpu buffer of a ring buffer empty?
2818 * @buffer: The ring buffer
2819 * @cpu: The CPU buffer to test
2821 int ring_buffer_empty_cpu(struct ring_buffer *buffer, int cpu)
2823 struct ring_buffer_per_cpu *cpu_buffer;
2824 unsigned long flags;
2828 if (!cpumask_test_cpu(cpu, buffer->cpumask))
2831 dolock = rb_ok_to_lock();
2833 cpu_buffer = buffer->buffers[cpu];
2834 local_irq_save(flags);
2836 spin_lock(&cpu_buffer->reader_lock);
2837 ret = rb_per_cpu_empty(cpu_buffer);
2839 spin_unlock(&cpu_buffer->reader_lock);
2840 local_irq_restore(flags);
2844 EXPORT_SYMBOL_GPL(ring_buffer_empty_cpu);
2847 * ring_buffer_swap_cpu - swap a CPU buffer between two ring buffers
2848 * @buffer_a: One buffer to swap with
2849 * @buffer_b: The other buffer to swap with
2851 * This function is useful for tracers that want to take a "snapshot"
2852 * of a CPU buffer and has another back up buffer lying around.
2853 * it is expected that the tracer handles the cpu buffer not being
2854 * used at the moment.
2856 int ring_buffer_swap_cpu(struct ring_buffer *buffer_a,
2857 struct ring_buffer *buffer_b, int cpu)
2859 struct ring_buffer_per_cpu *cpu_buffer_a;
2860 struct ring_buffer_per_cpu *cpu_buffer_b;
2863 if (!cpumask_test_cpu(cpu, buffer_a->cpumask) ||
2864 !cpumask_test_cpu(cpu, buffer_b->cpumask))
2867 /* At least make sure the two buffers are somewhat the same */
2868 if (buffer_a->pages != buffer_b->pages)
2873 if (ring_buffer_flags != RB_BUFFERS_ON)
2876 if (atomic_read(&buffer_a->record_disabled))
2879 if (atomic_read(&buffer_b->record_disabled))
2882 cpu_buffer_a = buffer_a->buffers[cpu];
2883 cpu_buffer_b = buffer_b->buffers[cpu];
2885 if (atomic_read(&cpu_buffer_a->record_disabled))
2888 if (atomic_read(&cpu_buffer_b->record_disabled))
2892 * We can't do a synchronize_sched here because this
2893 * function can be called in atomic context.
2894 * Normally this will be called from the same CPU as cpu.
2895 * If not it's up to the caller to protect this.
2897 atomic_inc(&cpu_buffer_a->record_disabled);
2898 atomic_inc(&cpu_buffer_b->record_disabled);
2900 buffer_a->buffers[cpu] = cpu_buffer_b;
2901 buffer_b->buffers[cpu] = cpu_buffer_a;
2903 cpu_buffer_b->buffer = buffer_a;
2904 cpu_buffer_a->buffer = buffer_b;
2906 atomic_dec(&cpu_buffer_a->record_disabled);
2907 atomic_dec(&cpu_buffer_b->record_disabled);
2913 EXPORT_SYMBOL_GPL(ring_buffer_swap_cpu);
2916 * ring_buffer_alloc_read_page - allocate a page to read from buffer
2917 * @buffer: the buffer to allocate for.
2919 * This function is used in conjunction with ring_buffer_read_page.
2920 * When reading a full page from the ring buffer, these functions
2921 * can be used to speed up the process. The calling function should
2922 * allocate a few pages first with this function. Then when it
2923 * needs to get pages from the ring buffer, it passes the result
2924 * of this function into ring_buffer_read_page, which will swap
2925 * the page that was allocated, with the read page of the buffer.
2928 * The page allocated, or NULL on error.
2930 void *ring_buffer_alloc_read_page(struct ring_buffer *buffer)
2932 struct buffer_data_page *bpage;
2935 addr = __get_free_page(GFP_KERNEL);
2939 bpage = (void *)addr;
2941 rb_init_page(bpage);
2945 EXPORT_SYMBOL_GPL(ring_buffer_alloc_read_page);
2948 * ring_buffer_free_read_page - free an allocated read page
2949 * @buffer: the buffer the page was allocate for
2950 * @data: the page to free
2952 * Free a page allocated from ring_buffer_alloc_read_page.
2954 void ring_buffer_free_read_page(struct ring_buffer *buffer, void *data)
2956 free_page((unsigned long)data);
2958 EXPORT_SYMBOL_GPL(ring_buffer_free_read_page);
2961 * ring_buffer_read_page - extract a page from the ring buffer
2962 * @buffer: buffer to extract from
2963 * @data_page: the page to use allocated from ring_buffer_alloc_read_page
2964 * @len: amount to extract
2965 * @cpu: the cpu of the buffer to extract
2966 * @full: should the extraction only happen when the page is full.
2968 * This function will pull out a page from the ring buffer and consume it.
2969 * @data_page must be the address of the variable that was returned
2970 * from ring_buffer_alloc_read_page. This is because the page might be used
2971 * to swap with a page in the ring buffer.
2974 * rpage = ring_buffer_alloc_read_page(buffer);
2977 * ret = ring_buffer_read_page(buffer, &rpage, len, cpu, 0);
2979 * process_page(rpage, ret);
2981 * When @full is set, the function will not return true unless
2982 * the writer is off the reader page.
2984 * Note: it is up to the calling functions to handle sleeps and wakeups.
2985 * The ring buffer can be used anywhere in the kernel and can not
2986 * blindly call wake_up. The layer that uses the ring buffer must be
2987 * responsible for that.
2990 * >=0 if data has been transferred, returns the offset of consumed data.
2991 * <0 if no data has been transferred.
2993 int ring_buffer_read_page(struct ring_buffer *buffer,
2994 void **data_page, size_t len, int cpu, int full)
2996 struct ring_buffer_per_cpu *cpu_buffer = buffer->buffers[cpu];
2997 struct ring_buffer_event *event;
2998 struct buffer_data_page *bpage;
2999 struct buffer_page *reader;
3000 unsigned long flags;
3001 unsigned int commit;
3006 if (!cpumask_test_cpu(cpu, buffer->cpumask))
3010 * If len is not big enough to hold the page header, then
3011 * we can not copy anything.
3013 if (len <= BUF_PAGE_HDR_SIZE)
3016 len -= BUF_PAGE_HDR_SIZE;
3025 spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
3027 reader = rb_get_reader_page(cpu_buffer);
3031 event = rb_reader_event(cpu_buffer);
3033 read = reader->read;
3034 commit = rb_page_commit(reader);
3037 * If this page has been partially read or
3038 * if len is not big enough to read the rest of the page or
3039 * a writer is still on the page, then
3040 * we must copy the data from the page to the buffer.
3041 * Otherwise, we can simply swap the page with the one passed in.
3043 if (read || (len < (commit - read)) ||
3044 cpu_buffer->reader_page == cpu_buffer->commit_page) {
3045 struct buffer_data_page *rpage = cpu_buffer->reader_page->page;
3046 unsigned int rpos = read;
3047 unsigned int pos = 0;
3053 if (len > (commit - read))
3054 len = (commit - read);
3056 size = rb_event_length(event);
3061 /* save the current timestamp, since the user will need it */
3062 save_timestamp = cpu_buffer->read_stamp;
3064 /* Need to copy one event at a time */
3066 memcpy(bpage->data + pos, rpage->data + rpos, size);
3070 rb_advance_reader(cpu_buffer);
3071 rpos = reader->read;
3074 event = rb_reader_event(cpu_buffer);
3075 size = rb_event_length(event);
3076 } while (len > size);
3079 local_set(&bpage->commit, pos);
3080 bpage->time_stamp = save_timestamp;
3082 /* we copied everything to the beginning */
3085 /* update the entry counter */
3086 cpu_buffer->read += local_read(&reader->entries);
3088 /* swap the pages */
3089 rb_init_page(bpage);
3090 bpage = reader->page;
3091 reader->page = *data_page;
3092 local_set(&reader->write, 0);
3093 local_set(&reader->entries, 0);
3100 spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
3105 EXPORT_SYMBOL_GPL(ring_buffer_read_page);
3108 rb_simple_read(struct file *filp, char __user *ubuf,
3109 size_t cnt, loff_t *ppos)
3111 unsigned long *p = filp->private_data;
3115 if (test_bit(RB_BUFFERS_DISABLED_BIT, p))
3116 r = sprintf(buf, "permanently disabled\n");
3118 r = sprintf(buf, "%d\n", test_bit(RB_BUFFERS_ON_BIT, p));
3120 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
3124 rb_simple_write(struct file *filp, const char __user *ubuf,
3125 size_t cnt, loff_t *ppos)
3127 unsigned long *p = filp->private_data;
3132 if (cnt >= sizeof(buf))
3135 if (copy_from_user(&buf, ubuf, cnt))
3140 ret = strict_strtoul(buf, 10, &val);
3145 set_bit(RB_BUFFERS_ON_BIT, p);
3147 clear_bit(RB_BUFFERS_ON_BIT, p);
3154 static const struct file_operations rb_simple_fops = {
3155 .open = tracing_open_generic,
3156 .read = rb_simple_read,
3157 .write = rb_simple_write,
3161 static __init int rb_init_debugfs(void)
3163 struct dentry *d_tracer;
3165 d_tracer = tracing_init_dentry();
3167 trace_create_file("tracing_on", 0644, d_tracer,
3168 &ring_buffer_flags, &rb_simple_fops);
3173 fs_initcall(rb_init_debugfs);
3175 #ifdef CONFIG_HOTPLUG_CPU
3176 static int rb_cpu_notify(struct notifier_block *self,
3177 unsigned long action, void *hcpu)
3179 struct ring_buffer *buffer =
3180 container_of(self, struct ring_buffer, cpu_notify);
3181 long cpu = (long)hcpu;
3184 case CPU_UP_PREPARE:
3185 case CPU_UP_PREPARE_FROZEN:
3186 if (cpumask_test_cpu(cpu, buffer->cpumask))
3189 buffer->buffers[cpu] =
3190 rb_allocate_cpu_buffer(buffer, cpu);
3191 if (!buffer->buffers[cpu]) {
3192 WARN(1, "failed to allocate ring buffer on CPU %ld\n",
3197 cpumask_set_cpu(cpu, buffer->cpumask);
3199 case CPU_DOWN_PREPARE:
3200 case CPU_DOWN_PREPARE_FROZEN:
3203 * If we were to free the buffer, then the user would
3204 * lose any trace that was in the buffer.