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/slab.h>
18 #include <linux/init.h>
19 #include <linux/hash.h>
20 #include <linux/list.h>
21 #include <linux/cpu.h>
24 #include <asm/local.h>
28 * The ring buffer header is special. We must manually up keep it.
30 int ring_buffer_print_entry_header(struct trace_seq *s)
34 ret = trace_seq_printf(s, "# compressed entry header\n");
35 ret = trace_seq_printf(s, "\ttype_len : 5 bits\n");
36 ret = trace_seq_printf(s, "\ttime_delta : 27 bits\n");
37 ret = trace_seq_printf(s, "\tarray : 32 bits\n");
38 ret = trace_seq_printf(s, "\n");
39 ret = trace_seq_printf(s, "\tpadding : type == %d\n",
40 RINGBUF_TYPE_PADDING);
41 ret = trace_seq_printf(s, "\ttime_extend : type == %d\n",
42 RINGBUF_TYPE_TIME_EXTEND);
43 ret = trace_seq_printf(s, "\tdata max type_len == %d\n",
44 RINGBUF_TYPE_DATA_TYPE_LEN_MAX);
50 * The ring buffer is made up of a list of pages. A separate list of pages is
51 * allocated for each CPU. A writer may only write to a buffer that is
52 * associated with the CPU it is currently executing on. A reader may read
53 * from any per cpu buffer.
55 * The reader is special. For each per cpu buffer, the reader has its own
56 * reader page. When a reader has read the entire reader page, this reader
57 * page is swapped with another page in the ring buffer.
59 * Now, as long as the writer is off the reader page, the reader can do what
60 * ever it wants with that page. The writer will never write to that page
61 * again (as long as it is out of the ring buffer).
63 * Here's some silly ASCII art.
66 * |reader| RING BUFFER
68 * +------+ +---+ +---+ +---+
77 * |reader| RING BUFFER
78 * |page |------------------v
79 * +------+ +---+ +---+ +---+
88 * |reader| RING BUFFER
89 * |page |------------------v
90 * +------+ +---+ +---+ +---+
95 * +------------------------------+
99 * |buffer| RING BUFFER
100 * |page |------------------v
101 * +------+ +---+ +---+ +---+
103 * | New +---+ +---+ +---+
106 * +------------------------------+
109 * After we make this swap, the reader can hand this page off to the splice
110 * code and be done with it. It can even allocate a new page if it needs to
111 * and swap that into the ring buffer.
113 * We will be using cmpxchg soon to make all this lockless.
118 * A fast way to enable or disable all ring buffers is to
119 * call tracing_on or tracing_off. Turning off the ring buffers
120 * prevents all ring buffers from being recorded to.
121 * Turning this switch on, makes it OK to write to the
122 * ring buffer, if the ring buffer is enabled itself.
124 * There's three layers that must be on in order to write
125 * to the ring buffer.
127 * 1) This global flag must be set.
128 * 2) The ring buffer must be enabled for recording.
129 * 3) The per cpu buffer must be enabled for recording.
131 * In case of an anomaly, this global flag has a bit set that
132 * will permantly disable all ring buffers.
136 * Global flag to disable all recording to ring buffers
137 * This has two bits: ON, DISABLED
141 * 0 0 : ring buffers are off
142 * 1 0 : ring buffers are on
143 * X 1 : ring buffers are permanently disabled
147 RB_BUFFERS_ON_BIT = 0,
148 RB_BUFFERS_DISABLED_BIT = 1,
152 RB_BUFFERS_ON = 1 << RB_BUFFERS_ON_BIT,
153 RB_BUFFERS_DISABLED = 1 << RB_BUFFERS_DISABLED_BIT,
156 static unsigned long ring_buffer_flags __read_mostly = RB_BUFFERS_ON;
158 #define BUF_PAGE_HDR_SIZE offsetof(struct buffer_data_page, data)
161 * tracing_on - enable all tracing buffers
163 * This function enables all tracing buffers that may have been
164 * disabled with tracing_off.
166 void tracing_on(void)
168 set_bit(RB_BUFFERS_ON_BIT, &ring_buffer_flags);
170 EXPORT_SYMBOL_GPL(tracing_on);
173 * tracing_off - turn off all tracing buffers
175 * This function stops all tracing buffers from recording data.
176 * It does not disable any overhead the tracers themselves may
177 * be causing. This function simply causes all recording to
178 * the ring buffers to fail.
180 void tracing_off(void)
182 clear_bit(RB_BUFFERS_ON_BIT, &ring_buffer_flags);
184 EXPORT_SYMBOL_GPL(tracing_off);
187 * tracing_off_permanent - permanently disable ring buffers
189 * This function, once called, will disable all ring buffers
192 void tracing_off_permanent(void)
194 set_bit(RB_BUFFERS_DISABLED_BIT, &ring_buffer_flags);
198 * tracing_is_on - show state of ring buffers enabled
200 int tracing_is_on(void)
202 return ring_buffer_flags == RB_BUFFERS_ON;
204 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 #if !defined(CONFIG_64BIT) || defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS)
212 # define RB_FORCE_8BYTE_ALIGNMENT 0
213 # define RB_ARCH_ALIGNMENT RB_ALIGNMENT
215 # define RB_FORCE_8BYTE_ALIGNMENT 1
216 # define RB_ARCH_ALIGNMENT 8U
219 /* define RINGBUF_TYPE_DATA for 'case RINGBUF_TYPE_DATA:' */
220 #define RINGBUF_TYPE_DATA 0 ... RINGBUF_TYPE_DATA_TYPE_LEN_MAX
223 RB_LEN_TIME_EXTEND = 8,
224 RB_LEN_TIME_STAMP = 16,
227 static inline int rb_null_event(struct ring_buffer_event *event)
229 return event->type_len == RINGBUF_TYPE_PADDING && !event->time_delta;
232 static void rb_event_set_padding(struct ring_buffer_event *event)
234 /* padding has a NULL time_delta */
235 event->type_len = RINGBUF_TYPE_PADDING;
236 event->time_delta = 0;
240 rb_event_data_length(struct ring_buffer_event *event)
245 length = event->type_len * RB_ALIGNMENT;
247 length = event->array[0];
248 return length + RB_EVNT_HDR_SIZE;
251 /* inline for ring buffer fast paths */
253 rb_event_length(struct ring_buffer_event *event)
255 switch (event->type_len) {
256 case RINGBUF_TYPE_PADDING:
257 if (rb_null_event(event))
260 return event->array[0] + RB_EVNT_HDR_SIZE;
262 case RINGBUF_TYPE_TIME_EXTEND:
263 return RB_LEN_TIME_EXTEND;
265 case RINGBUF_TYPE_TIME_STAMP:
266 return RB_LEN_TIME_STAMP;
268 case RINGBUF_TYPE_DATA:
269 return rb_event_data_length(event);
278 * ring_buffer_event_length - return the length of the event
279 * @event: the event to get the length of
281 unsigned ring_buffer_event_length(struct ring_buffer_event *event)
283 unsigned length = rb_event_length(event);
284 if (event->type_len > RINGBUF_TYPE_DATA_TYPE_LEN_MAX)
286 length -= RB_EVNT_HDR_SIZE;
287 if (length > RB_MAX_SMALL_DATA + sizeof(event->array[0]))
288 length -= sizeof(event->array[0]);
291 EXPORT_SYMBOL_GPL(ring_buffer_event_length);
293 /* inline for ring buffer fast paths */
295 rb_event_data(struct ring_buffer_event *event)
297 BUG_ON(event->type_len > RINGBUF_TYPE_DATA_TYPE_LEN_MAX);
298 /* If length is in len field, then array[0] has the data */
300 return (void *)&event->array[0];
301 /* Otherwise length is in array[0] and array[1] has the data */
302 return (void *)&event->array[1];
306 * ring_buffer_event_data - return the data of the event
307 * @event: the event to get the data from
309 void *ring_buffer_event_data(struct ring_buffer_event *event)
311 return rb_event_data(event);
313 EXPORT_SYMBOL_GPL(ring_buffer_event_data);
315 #define for_each_buffer_cpu(buffer, cpu) \
316 for_each_cpu(cpu, buffer->cpumask)
319 #define TS_MASK ((1ULL << TS_SHIFT) - 1)
320 #define TS_DELTA_TEST (~TS_MASK)
322 /* Flag when events were overwritten */
323 #define RB_MISSED_EVENTS (1 << 31)
324 /* Missed count stored at end */
325 #define RB_MISSED_STORED (1 << 30)
327 struct buffer_data_page {
328 u64 time_stamp; /* page time stamp */
329 local_t commit; /* write committed index */
330 unsigned char data[]; /* data of buffer page */
334 * Note, the buffer_page list must be first. The buffer pages
335 * are allocated in cache lines, which means that each buffer
336 * page will be at the beginning of a cache line, and thus
337 * the least significant bits will be zero. We use this to
338 * add flags in the list struct pointers, to make the ring buffer
342 struct list_head list; /* list of buffer pages */
343 local_t write; /* index for next write */
344 unsigned read; /* index for next read */
345 local_t entries; /* entries on this page */
346 unsigned long real_end; /* real end of data */
347 struct buffer_data_page *page; /* Actual data page */
351 * The buffer page counters, write and entries, must be reset
352 * atomically when crossing page boundaries. To synchronize this
353 * update, two counters are inserted into the number. One is
354 * the actual counter for the write position or count on the page.
356 * The other is a counter of updaters. Before an update happens
357 * the update partition of the counter is incremented. This will
358 * allow the updater to update the counter atomically.
360 * The counter is 20 bits, and the state data is 12.
362 #define RB_WRITE_MASK 0xfffff
363 #define RB_WRITE_INTCNT (1 << 20)
365 static void rb_init_page(struct buffer_data_page *bpage)
367 local_set(&bpage->commit, 0);
371 * ring_buffer_page_len - the size of data on the page.
372 * @page: The page to read
374 * Returns the amount of data on the page, including buffer page header.
376 size_t ring_buffer_page_len(void *page)
378 return local_read(&((struct buffer_data_page *)page)->commit)
383 * Also stolen from mm/slob.c. Thanks to Mathieu Desnoyers for pointing
386 static void free_buffer_page(struct buffer_page *bpage)
388 free_page((unsigned long)bpage->page);
393 * We need to fit the time_stamp delta into 27 bits.
395 static inline int test_time_stamp(u64 delta)
397 if (delta & TS_DELTA_TEST)
402 #define BUF_PAGE_SIZE (PAGE_SIZE - BUF_PAGE_HDR_SIZE)
404 /* Max payload is BUF_PAGE_SIZE - header (8bytes) */
405 #define BUF_MAX_DATA_SIZE (BUF_PAGE_SIZE - (sizeof(u32) * 2))
407 /* Max number of timestamps that can fit on a page */
408 #define RB_TIMESTAMPS_PER_PAGE (BUF_PAGE_SIZE / RB_LEN_TIME_STAMP)
410 int ring_buffer_print_page_header(struct trace_seq *s)
412 struct buffer_data_page field;
415 ret = trace_seq_printf(s, "\tfield: u64 timestamp;\t"
416 "offset:0;\tsize:%u;\tsigned:%u;\n",
417 (unsigned int)sizeof(field.time_stamp),
418 (unsigned int)is_signed_type(u64));
420 ret = trace_seq_printf(s, "\tfield: local_t commit;\t"
421 "offset:%u;\tsize:%u;\tsigned:%u;\n",
422 (unsigned int)offsetof(typeof(field), commit),
423 (unsigned int)sizeof(field.commit),
424 (unsigned int)is_signed_type(long));
426 ret = trace_seq_printf(s, "\tfield: int overwrite;\t"
427 "offset:%u;\tsize:%u;\tsigned:%u;\n",
428 (unsigned int)offsetof(typeof(field), commit),
430 (unsigned int)is_signed_type(long));
432 ret = trace_seq_printf(s, "\tfield: char data;\t"
433 "offset:%u;\tsize:%u;\tsigned:%u;\n",
434 (unsigned int)offsetof(typeof(field), data),
435 (unsigned int)BUF_PAGE_SIZE,
436 (unsigned int)is_signed_type(char));
442 * head_page == tail_page && head == tail then buffer is empty.
444 struct ring_buffer_per_cpu {
446 struct ring_buffer *buffer;
447 spinlock_t reader_lock; /* serialize readers */
448 arch_spinlock_t lock;
449 struct lock_class_key lock_key;
450 struct list_head *pages;
451 struct buffer_page *head_page; /* read from head */
452 struct buffer_page *tail_page; /* write to tail */
453 struct buffer_page *commit_page; /* committed pages */
454 struct buffer_page *reader_page;
455 unsigned long lost_events;
456 unsigned long last_overrun;
457 local_t commit_overrun;
465 atomic_t record_disabled;
472 atomic_t record_disabled;
473 cpumask_var_t cpumask;
475 struct lock_class_key *reader_lock_key;
479 struct ring_buffer_per_cpu **buffers;
481 #ifdef CONFIG_HOTPLUG_CPU
482 struct notifier_block cpu_notify;
487 struct ring_buffer_iter {
488 struct ring_buffer_per_cpu *cpu_buffer;
490 struct buffer_page *head_page;
491 struct buffer_page *cache_reader_page;
492 unsigned long cache_read;
496 /* buffer may be either ring_buffer or ring_buffer_per_cpu */
497 #define RB_WARN_ON(b, cond) \
499 int _____ret = unlikely(cond); \
501 if (__same_type(*(b), struct ring_buffer_per_cpu)) { \
502 struct ring_buffer_per_cpu *__b = \
504 atomic_inc(&__b->buffer->record_disabled); \
506 atomic_inc(&b->record_disabled); \
512 /* Up this if you want to test the TIME_EXTENTS and normalization */
513 #define DEBUG_SHIFT 0
515 static inline u64 rb_time_stamp(struct ring_buffer *buffer)
517 /* shift to debug/test normalization and TIME_EXTENTS */
518 return buffer->clock() << DEBUG_SHIFT;
521 u64 ring_buffer_time_stamp(struct ring_buffer *buffer, int cpu)
525 preempt_disable_notrace();
526 time = rb_time_stamp(buffer);
527 preempt_enable_no_resched_notrace();
531 EXPORT_SYMBOL_GPL(ring_buffer_time_stamp);
533 void ring_buffer_normalize_time_stamp(struct ring_buffer *buffer,
536 /* Just stupid testing the normalize function and deltas */
539 EXPORT_SYMBOL_GPL(ring_buffer_normalize_time_stamp);
542 * Making the ring buffer lockless makes things tricky.
543 * Although writes only happen on the CPU that they are on,
544 * and they only need to worry about interrupts. Reads can
547 * The reader page is always off the ring buffer, but when the
548 * reader finishes with a page, it needs to swap its page with
549 * a new one from the buffer. The reader needs to take from
550 * the head (writes go to the tail). But if a writer is in overwrite
551 * mode and wraps, it must push the head page forward.
553 * Here lies the problem.
555 * The reader must be careful to replace only the head page, and
556 * not another one. As described at the top of the file in the
557 * ASCII art, the reader sets its old page to point to the next
558 * page after head. It then sets the page after head to point to
559 * the old reader page. But if the writer moves the head page
560 * during this operation, the reader could end up with the tail.
562 * We use cmpxchg to help prevent this race. We also do something
563 * special with the page before head. We set the LSB to 1.
565 * When the writer must push the page forward, it will clear the
566 * bit that points to the head page, move the head, and then set
567 * the bit that points to the new head page.
569 * We also don't want an interrupt coming in and moving the head
570 * page on another writer. Thus we use the second LSB to catch
573 * head->list->prev->next bit 1 bit 0
576 * Points to head page 0 1
579 * Note we can not trust the prev pointer of the head page, because:
581 * +----+ +-----+ +-----+
582 * | |------>| T |---X--->| N |
584 * +----+ +-----+ +-----+
587 * +----------| R |----------+ |
591 * Key: ---X--> HEAD flag set in pointer
596 * (see __rb_reserve_next() to see where this happens)
598 * What the above shows is that the reader just swapped out
599 * the reader page with a page in the buffer, but before it
600 * could make the new header point back to the new page added
601 * it was preempted by a writer. The writer moved forward onto
602 * the new page added by the reader and is about to move forward
605 * You can see, it is legitimate for the previous pointer of
606 * the head (or any page) not to point back to itself. But only
610 #define RB_PAGE_NORMAL 0UL
611 #define RB_PAGE_HEAD 1UL
612 #define RB_PAGE_UPDATE 2UL
615 #define RB_FLAG_MASK 3UL
617 /* PAGE_MOVED is not part of the mask */
618 #define RB_PAGE_MOVED 4UL
621 * rb_list_head - remove any bit
623 static struct list_head *rb_list_head(struct list_head *list)
625 unsigned long val = (unsigned long)list;
627 return (struct list_head *)(val & ~RB_FLAG_MASK);
631 * rb_is_head_page - test if the given page is the head page
633 * Because the reader may move the head_page pointer, we can
634 * not trust what the head page is (it may be pointing to
635 * the reader page). But if the next page is a header page,
636 * its flags will be non zero.
639 rb_is_head_page(struct ring_buffer_per_cpu *cpu_buffer,
640 struct buffer_page *page, struct list_head *list)
644 val = (unsigned long)list->next;
646 if ((val & ~RB_FLAG_MASK) != (unsigned long)&page->list)
647 return RB_PAGE_MOVED;
649 return val & RB_FLAG_MASK;
655 * The unique thing about the reader page, is that, if the
656 * writer is ever on it, the previous pointer never points
657 * back to the reader page.
659 static int rb_is_reader_page(struct buffer_page *page)
661 struct list_head *list = page->list.prev;
663 return rb_list_head(list->next) != &page->list;
667 * rb_set_list_to_head - set a list_head to be pointing to head.
669 static void rb_set_list_to_head(struct ring_buffer_per_cpu *cpu_buffer,
670 struct list_head *list)
674 ptr = (unsigned long *)&list->next;
675 *ptr |= RB_PAGE_HEAD;
676 *ptr &= ~RB_PAGE_UPDATE;
680 * rb_head_page_activate - sets up head page
682 static void rb_head_page_activate(struct ring_buffer_per_cpu *cpu_buffer)
684 struct buffer_page *head;
686 head = cpu_buffer->head_page;
691 * Set the previous list pointer to have the HEAD flag.
693 rb_set_list_to_head(cpu_buffer, head->list.prev);
696 static void rb_list_head_clear(struct list_head *list)
698 unsigned long *ptr = (unsigned long *)&list->next;
700 *ptr &= ~RB_FLAG_MASK;
704 * rb_head_page_dactivate - clears head page ptr (for free list)
707 rb_head_page_deactivate(struct ring_buffer_per_cpu *cpu_buffer)
709 struct list_head *hd;
711 /* Go through the whole list and clear any pointers found. */
712 rb_list_head_clear(cpu_buffer->pages);
714 list_for_each(hd, cpu_buffer->pages)
715 rb_list_head_clear(hd);
718 static int rb_head_page_set(struct ring_buffer_per_cpu *cpu_buffer,
719 struct buffer_page *head,
720 struct buffer_page *prev,
721 int old_flag, int new_flag)
723 struct list_head *list;
724 unsigned long val = (unsigned long)&head->list;
729 val &= ~RB_FLAG_MASK;
731 ret = cmpxchg((unsigned long *)&list->next,
732 val | old_flag, val | new_flag);
734 /* check if the reader took the page */
735 if ((ret & ~RB_FLAG_MASK) != val)
736 return RB_PAGE_MOVED;
738 return ret & RB_FLAG_MASK;
741 static int rb_head_page_set_update(struct ring_buffer_per_cpu *cpu_buffer,
742 struct buffer_page *head,
743 struct buffer_page *prev,
746 return rb_head_page_set(cpu_buffer, head, prev,
747 old_flag, RB_PAGE_UPDATE);
750 static int rb_head_page_set_head(struct ring_buffer_per_cpu *cpu_buffer,
751 struct buffer_page *head,
752 struct buffer_page *prev,
755 return rb_head_page_set(cpu_buffer, head, prev,
756 old_flag, RB_PAGE_HEAD);
759 static int rb_head_page_set_normal(struct ring_buffer_per_cpu *cpu_buffer,
760 struct buffer_page *head,
761 struct buffer_page *prev,
764 return rb_head_page_set(cpu_buffer, head, prev,
765 old_flag, RB_PAGE_NORMAL);
768 static inline void rb_inc_page(struct ring_buffer_per_cpu *cpu_buffer,
769 struct buffer_page **bpage)
771 struct list_head *p = rb_list_head((*bpage)->list.next);
773 *bpage = list_entry(p, struct buffer_page, list);
776 static struct buffer_page *
777 rb_set_head_page(struct ring_buffer_per_cpu *cpu_buffer)
779 struct buffer_page *head;
780 struct buffer_page *page;
781 struct list_head *list;
784 if (RB_WARN_ON(cpu_buffer, !cpu_buffer->head_page))
788 list = cpu_buffer->pages;
789 if (RB_WARN_ON(cpu_buffer, rb_list_head(list->prev->next) != list))
792 page = head = cpu_buffer->head_page;
794 * It is possible that the writer moves the header behind
795 * where we started, and we miss in one loop.
796 * A second loop should grab the header, but we'll do
797 * three loops just because I'm paranoid.
799 for (i = 0; i < 3; i++) {
801 if (rb_is_head_page(cpu_buffer, page, page->list.prev)) {
802 cpu_buffer->head_page = page;
805 rb_inc_page(cpu_buffer, &page);
806 } while (page != head);
809 RB_WARN_ON(cpu_buffer, 1);
814 static int rb_head_page_replace(struct buffer_page *old,
815 struct buffer_page *new)
817 unsigned long *ptr = (unsigned long *)&old->list.prev->next;
821 val = *ptr & ~RB_FLAG_MASK;
824 ret = cmpxchg(ptr, val, (unsigned long)&new->list);
830 * rb_tail_page_update - move the tail page forward
832 * Returns 1 if moved tail page, 0 if someone else did.
834 static int rb_tail_page_update(struct ring_buffer_per_cpu *cpu_buffer,
835 struct buffer_page *tail_page,
836 struct buffer_page *next_page)
838 struct buffer_page *old_tail;
839 unsigned long old_entries;
840 unsigned long old_write;
844 * The tail page now needs to be moved forward.
846 * We need to reset the tail page, but without messing
847 * with possible erasing of data brought in by interrupts
848 * that have moved the tail page and are currently on it.
850 * We add a counter to the write field to denote this.
852 old_write = local_add_return(RB_WRITE_INTCNT, &next_page->write);
853 old_entries = local_add_return(RB_WRITE_INTCNT, &next_page->entries);
856 * Just make sure we have seen our old_write and synchronize
857 * with any interrupts that come in.
862 * If the tail page is still the same as what we think
863 * it is, then it is up to us to update the tail
866 if (tail_page == cpu_buffer->tail_page) {
867 /* Zero the write counter */
868 unsigned long val = old_write & ~RB_WRITE_MASK;
869 unsigned long eval = old_entries & ~RB_WRITE_MASK;
872 * This will only succeed if an interrupt did
873 * not come in and change it. In which case, we
874 * do not want to modify it.
876 * We add (void) to let the compiler know that we do not care
877 * about the return value of these functions. We use the
878 * cmpxchg to only update if an interrupt did not already
879 * do it for us. If the cmpxchg fails, we don't care.
881 (void)local_cmpxchg(&next_page->write, old_write, val);
882 (void)local_cmpxchg(&next_page->entries, old_entries, eval);
885 * No need to worry about races with clearing out the commit.
886 * it only can increment when a commit takes place. But that
887 * only happens in the outer most nested commit.
889 local_set(&next_page->page->commit, 0);
891 old_tail = cmpxchg(&cpu_buffer->tail_page,
892 tail_page, next_page);
894 if (old_tail == tail_page)
901 static int rb_check_bpage(struct ring_buffer_per_cpu *cpu_buffer,
902 struct buffer_page *bpage)
904 unsigned long val = (unsigned long)bpage;
906 if (RB_WARN_ON(cpu_buffer, val & RB_FLAG_MASK))
913 * rb_check_list - make sure a pointer to a list has the last bits zero
915 static int rb_check_list(struct ring_buffer_per_cpu *cpu_buffer,
916 struct list_head *list)
918 if (RB_WARN_ON(cpu_buffer, rb_list_head(list->prev) != list->prev))
920 if (RB_WARN_ON(cpu_buffer, rb_list_head(list->next) != list->next))
926 * check_pages - integrity check of buffer pages
927 * @cpu_buffer: CPU buffer with pages to test
929 * As a safety measure we check to make sure the data pages have not
932 static int rb_check_pages(struct ring_buffer_per_cpu *cpu_buffer)
934 struct list_head *head = cpu_buffer->pages;
935 struct buffer_page *bpage, *tmp;
937 rb_head_page_deactivate(cpu_buffer);
939 if (RB_WARN_ON(cpu_buffer, head->next->prev != head))
941 if (RB_WARN_ON(cpu_buffer, head->prev->next != head))
944 if (rb_check_list(cpu_buffer, head))
947 list_for_each_entry_safe(bpage, tmp, head, list) {
948 if (RB_WARN_ON(cpu_buffer,
949 bpage->list.next->prev != &bpage->list))
951 if (RB_WARN_ON(cpu_buffer,
952 bpage->list.prev->next != &bpage->list))
954 if (rb_check_list(cpu_buffer, &bpage->list))
958 rb_head_page_activate(cpu_buffer);
963 static int rb_allocate_pages(struct ring_buffer_per_cpu *cpu_buffer,
966 struct buffer_page *bpage, *tmp;
973 for (i = 0; i < nr_pages; i++) {
974 bpage = kzalloc_node(ALIGN(sizeof(*bpage), cache_line_size()),
975 GFP_KERNEL, cpu_to_node(cpu_buffer->cpu));
979 rb_check_bpage(cpu_buffer, bpage);
981 list_add(&bpage->list, &pages);
983 addr = __get_free_page(GFP_KERNEL);
986 bpage->page = (void *)addr;
987 rb_init_page(bpage->page);
991 * The ring buffer page list is a circular list that does not
992 * start and end with a list head. All page list items point to
995 cpu_buffer->pages = pages.next;
998 rb_check_pages(cpu_buffer);
1003 list_for_each_entry_safe(bpage, tmp, &pages, list) {
1004 list_del_init(&bpage->list);
1005 free_buffer_page(bpage);
1010 static struct ring_buffer_per_cpu *
1011 rb_allocate_cpu_buffer(struct ring_buffer *buffer, int cpu)
1013 struct ring_buffer_per_cpu *cpu_buffer;
1014 struct buffer_page *bpage;
1018 cpu_buffer = kzalloc_node(ALIGN(sizeof(*cpu_buffer), cache_line_size()),
1019 GFP_KERNEL, cpu_to_node(cpu));
1023 cpu_buffer->cpu = cpu;
1024 cpu_buffer->buffer = buffer;
1025 spin_lock_init(&cpu_buffer->reader_lock);
1026 lockdep_set_class(&cpu_buffer->reader_lock, buffer->reader_lock_key);
1027 cpu_buffer->lock = (arch_spinlock_t)__ARCH_SPIN_LOCK_UNLOCKED;
1029 bpage = kzalloc_node(ALIGN(sizeof(*bpage), cache_line_size()),
1030 GFP_KERNEL, cpu_to_node(cpu));
1032 goto fail_free_buffer;
1034 rb_check_bpage(cpu_buffer, bpage);
1036 cpu_buffer->reader_page = bpage;
1037 addr = __get_free_page(GFP_KERNEL);
1039 goto fail_free_reader;
1040 bpage->page = (void *)addr;
1041 rb_init_page(bpage->page);
1043 INIT_LIST_HEAD(&cpu_buffer->reader_page->list);
1045 ret = rb_allocate_pages(cpu_buffer, buffer->pages);
1047 goto fail_free_reader;
1049 cpu_buffer->head_page
1050 = list_entry(cpu_buffer->pages, struct buffer_page, list);
1051 cpu_buffer->tail_page = cpu_buffer->commit_page = cpu_buffer->head_page;
1053 rb_head_page_activate(cpu_buffer);
1058 free_buffer_page(cpu_buffer->reader_page);
1065 static void rb_free_cpu_buffer(struct ring_buffer_per_cpu *cpu_buffer)
1067 struct list_head *head = cpu_buffer->pages;
1068 struct buffer_page *bpage, *tmp;
1070 free_buffer_page(cpu_buffer->reader_page);
1072 rb_head_page_deactivate(cpu_buffer);
1075 list_for_each_entry_safe(bpage, tmp, head, list) {
1076 list_del_init(&bpage->list);
1077 free_buffer_page(bpage);
1079 bpage = list_entry(head, struct buffer_page, list);
1080 free_buffer_page(bpage);
1086 #ifdef CONFIG_HOTPLUG_CPU
1087 static int rb_cpu_notify(struct notifier_block *self,
1088 unsigned long action, void *hcpu);
1092 * ring_buffer_alloc - allocate a new ring_buffer
1093 * @size: the size in bytes per cpu that is needed.
1094 * @flags: attributes to set for the ring buffer.
1096 * Currently the only flag that is available is the RB_FL_OVERWRITE
1097 * flag. This flag means that the buffer will overwrite old data
1098 * when the buffer wraps. If this flag is not set, the buffer will
1099 * drop data when the tail hits the head.
1101 struct ring_buffer *__ring_buffer_alloc(unsigned long size, unsigned flags,
1102 struct lock_class_key *key)
1104 struct ring_buffer *buffer;
1108 /* keep it in its own cache line */
1109 buffer = kzalloc(ALIGN(sizeof(*buffer), cache_line_size()),
1114 if (!alloc_cpumask_var(&buffer->cpumask, GFP_KERNEL))
1115 goto fail_free_buffer;
1117 buffer->pages = DIV_ROUND_UP(size, BUF_PAGE_SIZE);
1118 buffer->flags = flags;
1119 buffer->clock = trace_clock_local;
1120 buffer->reader_lock_key = key;
1122 /* need at least two pages */
1123 if (buffer->pages < 2)
1127 * In case of non-hotplug cpu, if the ring-buffer is allocated
1128 * in early initcall, it will not be notified of secondary cpus.
1129 * In that off case, we need to allocate for all possible cpus.
1131 #ifdef CONFIG_HOTPLUG_CPU
1133 cpumask_copy(buffer->cpumask, cpu_online_mask);
1135 cpumask_copy(buffer->cpumask, cpu_possible_mask);
1137 buffer->cpus = nr_cpu_ids;
1139 bsize = sizeof(void *) * nr_cpu_ids;
1140 buffer->buffers = kzalloc(ALIGN(bsize, cache_line_size()),
1142 if (!buffer->buffers)
1143 goto fail_free_cpumask;
1145 for_each_buffer_cpu(buffer, cpu) {
1146 buffer->buffers[cpu] =
1147 rb_allocate_cpu_buffer(buffer, cpu);
1148 if (!buffer->buffers[cpu])
1149 goto fail_free_buffers;
1152 #ifdef CONFIG_HOTPLUG_CPU
1153 buffer->cpu_notify.notifier_call = rb_cpu_notify;
1154 buffer->cpu_notify.priority = 0;
1155 register_cpu_notifier(&buffer->cpu_notify);
1159 mutex_init(&buffer->mutex);
1164 for_each_buffer_cpu(buffer, cpu) {
1165 if (buffer->buffers[cpu])
1166 rb_free_cpu_buffer(buffer->buffers[cpu]);
1168 kfree(buffer->buffers);
1171 free_cpumask_var(buffer->cpumask);
1178 EXPORT_SYMBOL_GPL(__ring_buffer_alloc);
1181 * ring_buffer_free - free a ring buffer.
1182 * @buffer: the buffer to free.
1185 ring_buffer_free(struct ring_buffer *buffer)
1191 #ifdef CONFIG_HOTPLUG_CPU
1192 unregister_cpu_notifier(&buffer->cpu_notify);
1195 for_each_buffer_cpu(buffer, cpu)
1196 rb_free_cpu_buffer(buffer->buffers[cpu]);
1200 kfree(buffer->buffers);
1201 free_cpumask_var(buffer->cpumask);
1205 EXPORT_SYMBOL_GPL(ring_buffer_free);
1207 void ring_buffer_set_clock(struct ring_buffer *buffer,
1210 buffer->clock = clock;
1213 static void rb_reset_cpu(struct ring_buffer_per_cpu *cpu_buffer);
1216 rb_remove_pages(struct ring_buffer_per_cpu *cpu_buffer, unsigned nr_pages)
1218 struct buffer_page *bpage;
1219 struct list_head *p;
1222 spin_lock_irq(&cpu_buffer->reader_lock);
1223 rb_head_page_deactivate(cpu_buffer);
1225 for (i = 0; i < nr_pages; i++) {
1226 if (RB_WARN_ON(cpu_buffer, list_empty(cpu_buffer->pages)))
1228 p = cpu_buffer->pages->next;
1229 bpage = list_entry(p, struct buffer_page, list);
1230 list_del_init(&bpage->list);
1231 free_buffer_page(bpage);
1233 if (RB_WARN_ON(cpu_buffer, list_empty(cpu_buffer->pages)))
1236 rb_reset_cpu(cpu_buffer);
1237 rb_check_pages(cpu_buffer);
1240 spin_unlock_irq(&cpu_buffer->reader_lock);
1244 rb_insert_pages(struct ring_buffer_per_cpu *cpu_buffer,
1245 struct list_head *pages, unsigned nr_pages)
1247 struct buffer_page *bpage;
1248 struct list_head *p;
1251 spin_lock_irq(&cpu_buffer->reader_lock);
1252 rb_head_page_deactivate(cpu_buffer);
1254 for (i = 0; i < nr_pages; i++) {
1255 if (RB_WARN_ON(cpu_buffer, list_empty(pages)))
1258 bpage = list_entry(p, struct buffer_page, list);
1259 list_del_init(&bpage->list);
1260 list_add_tail(&bpage->list, cpu_buffer->pages);
1262 rb_reset_cpu(cpu_buffer);
1263 rb_check_pages(cpu_buffer);
1266 spin_unlock_irq(&cpu_buffer->reader_lock);
1270 * ring_buffer_resize - resize the ring buffer
1271 * @buffer: the buffer to resize.
1272 * @size: the new size.
1274 * Minimum size is 2 * BUF_PAGE_SIZE.
1276 * Returns -1 on failure.
1278 int ring_buffer_resize(struct ring_buffer *buffer, unsigned long size)
1280 struct ring_buffer_per_cpu *cpu_buffer;
1281 unsigned nr_pages, rm_pages, new_pages;
1282 struct buffer_page *bpage, *tmp;
1283 unsigned long buffer_size;
1289 * Always succeed at resizing a non-existent buffer:
1294 size = DIV_ROUND_UP(size, BUF_PAGE_SIZE);
1295 size *= BUF_PAGE_SIZE;
1296 buffer_size = buffer->pages * BUF_PAGE_SIZE;
1298 /* we need a minimum of two pages */
1299 if (size < BUF_PAGE_SIZE * 2)
1300 size = BUF_PAGE_SIZE * 2;
1302 if (size == buffer_size)
1305 atomic_inc(&buffer->record_disabled);
1307 /* Make sure all writers are done with this buffer. */
1308 synchronize_sched();
1310 mutex_lock(&buffer->mutex);
1313 nr_pages = DIV_ROUND_UP(size, BUF_PAGE_SIZE);
1315 if (size < buffer_size) {
1317 /* easy case, just free pages */
1318 if (RB_WARN_ON(buffer, nr_pages >= buffer->pages))
1321 rm_pages = buffer->pages - nr_pages;
1323 for_each_buffer_cpu(buffer, cpu) {
1324 cpu_buffer = buffer->buffers[cpu];
1325 rb_remove_pages(cpu_buffer, rm_pages);
1331 * This is a bit more difficult. We only want to add pages
1332 * when we can allocate enough for all CPUs. We do this
1333 * by allocating all the pages and storing them on a local
1334 * link list. If we succeed in our allocation, then we
1335 * add these pages to the cpu_buffers. Otherwise we just free
1336 * them all and return -ENOMEM;
1338 if (RB_WARN_ON(buffer, nr_pages <= buffer->pages))
1341 new_pages = nr_pages - buffer->pages;
1343 for_each_buffer_cpu(buffer, cpu) {
1344 for (i = 0; i < new_pages; i++) {
1345 bpage = kzalloc_node(ALIGN(sizeof(*bpage),
1347 GFP_KERNEL, cpu_to_node(cpu));
1350 list_add(&bpage->list, &pages);
1351 addr = __get_free_page(GFP_KERNEL);
1354 bpage->page = (void *)addr;
1355 rb_init_page(bpage->page);
1359 for_each_buffer_cpu(buffer, cpu) {
1360 cpu_buffer = buffer->buffers[cpu];
1361 rb_insert_pages(cpu_buffer, &pages, new_pages);
1364 if (RB_WARN_ON(buffer, !list_empty(&pages)))
1368 buffer->pages = nr_pages;
1370 mutex_unlock(&buffer->mutex);
1372 atomic_dec(&buffer->record_disabled);
1377 list_for_each_entry_safe(bpage, tmp, &pages, list) {
1378 list_del_init(&bpage->list);
1379 free_buffer_page(bpage);
1382 mutex_unlock(&buffer->mutex);
1383 atomic_dec(&buffer->record_disabled);
1387 * Something went totally wrong, and we are too paranoid
1388 * to even clean up the mess.
1392 mutex_unlock(&buffer->mutex);
1393 atomic_dec(&buffer->record_disabled);
1396 EXPORT_SYMBOL_GPL(ring_buffer_resize);
1398 static inline void *
1399 __rb_data_page_index(struct buffer_data_page *bpage, unsigned index)
1401 return bpage->data + index;
1404 static inline void *__rb_page_index(struct buffer_page *bpage, unsigned index)
1406 return bpage->page->data + index;
1409 static inline struct ring_buffer_event *
1410 rb_reader_event(struct ring_buffer_per_cpu *cpu_buffer)
1412 return __rb_page_index(cpu_buffer->reader_page,
1413 cpu_buffer->reader_page->read);
1416 static inline struct ring_buffer_event *
1417 rb_iter_head_event(struct ring_buffer_iter *iter)
1419 return __rb_page_index(iter->head_page, iter->head);
1422 static inline unsigned long rb_page_write(struct buffer_page *bpage)
1424 return local_read(&bpage->write) & RB_WRITE_MASK;
1427 static inline unsigned rb_page_commit(struct buffer_page *bpage)
1429 return local_read(&bpage->page->commit);
1432 static inline unsigned long rb_page_entries(struct buffer_page *bpage)
1434 return local_read(&bpage->entries) & RB_WRITE_MASK;
1437 /* Size is determined by what has been commited */
1438 static inline unsigned rb_page_size(struct buffer_page *bpage)
1440 return rb_page_commit(bpage);
1443 static inline unsigned
1444 rb_commit_index(struct ring_buffer_per_cpu *cpu_buffer)
1446 return rb_page_commit(cpu_buffer->commit_page);
1449 static inline unsigned
1450 rb_event_index(struct ring_buffer_event *event)
1452 unsigned long addr = (unsigned long)event;
1454 return (addr & ~PAGE_MASK) - BUF_PAGE_HDR_SIZE;
1458 rb_event_is_commit(struct ring_buffer_per_cpu *cpu_buffer,
1459 struct ring_buffer_event *event)
1461 unsigned long addr = (unsigned long)event;
1462 unsigned long index;
1464 index = rb_event_index(event);
1467 return cpu_buffer->commit_page->page == (void *)addr &&
1468 rb_commit_index(cpu_buffer) == index;
1472 rb_set_commit_to_write(struct ring_buffer_per_cpu *cpu_buffer)
1474 unsigned long max_count;
1477 * We only race with interrupts and NMIs on this CPU.
1478 * If we own the commit event, then we can commit
1479 * all others that interrupted us, since the interruptions
1480 * are in stack format (they finish before they come
1481 * back to us). This allows us to do a simple loop to
1482 * assign the commit to the tail.
1485 max_count = cpu_buffer->buffer->pages * 100;
1487 while (cpu_buffer->commit_page != cpu_buffer->tail_page) {
1488 if (RB_WARN_ON(cpu_buffer, !(--max_count)))
1490 if (RB_WARN_ON(cpu_buffer,
1491 rb_is_reader_page(cpu_buffer->tail_page)))
1493 local_set(&cpu_buffer->commit_page->page->commit,
1494 rb_page_write(cpu_buffer->commit_page));
1495 rb_inc_page(cpu_buffer, &cpu_buffer->commit_page);
1496 cpu_buffer->write_stamp =
1497 cpu_buffer->commit_page->page->time_stamp;
1498 /* add barrier to keep gcc from optimizing too much */
1501 while (rb_commit_index(cpu_buffer) !=
1502 rb_page_write(cpu_buffer->commit_page)) {
1504 local_set(&cpu_buffer->commit_page->page->commit,
1505 rb_page_write(cpu_buffer->commit_page));
1506 RB_WARN_ON(cpu_buffer,
1507 local_read(&cpu_buffer->commit_page->page->commit) &
1512 /* again, keep gcc from optimizing */
1516 * If an interrupt came in just after the first while loop
1517 * and pushed the tail page forward, we will be left with
1518 * a dangling commit that will never go forward.
1520 if (unlikely(cpu_buffer->commit_page != cpu_buffer->tail_page))
1524 static void rb_reset_reader_page(struct ring_buffer_per_cpu *cpu_buffer)
1526 cpu_buffer->read_stamp = cpu_buffer->reader_page->page->time_stamp;
1527 cpu_buffer->reader_page->read = 0;
1530 static void rb_inc_iter(struct ring_buffer_iter *iter)
1532 struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
1535 * The iterator could be on the reader page (it starts there).
1536 * But the head could have moved, since the reader was
1537 * found. Check for this case and assign the iterator
1538 * to the head page instead of next.
1540 if (iter->head_page == cpu_buffer->reader_page)
1541 iter->head_page = rb_set_head_page(cpu_buffer);
1543 rb_inc_page(cpu_buffer, &iter->head_page);
1545 iter->read_stamp = iter->head_page->page->time_stamp;
1550 * ring_buffer_update_event - update event type and data
1551 * @event: the even to update
1552 * @type: the type of event
1553 * @length: the size of the event field in the ring buffer
1555 * Update the type and data fields of the event. The length
1556 * is the actual size that is written to the ring buffer,
1557 * and with this, we can determine what to place into the
1561 rb_update_event(struct ring_buffer_event *event,
1562 unsigned type, unsigned length)
1564 event->type_len = type;
1568 case RINGBUF_TYPE_PADDING:
1569 case RINGBUF_TYPE_TIME_EXTEND:
1570 case RINGBUF_TYPE_TIME_STAMP:
1574 length -= RB_EVNT_HDR_SIZE;
1575 if (length > RB_MAX_SMALL_DATA || RB_FORCE_8BYTE_ALIGNMENT)
1576 event->array[0] = length;
1578 event->type_len = DIV_ROUND_UP(length, RB_ALIGNMENT);
1586 * rb_handle_head_page - writer hit the head page
1588 * Returns: +1 to retry page
1593 rb_handle_head_page(struct ring_buffer_per_cpu *cpu_buffer,
1594 struct buffer_page *tail_page,
1595 struct buffer_page *next_page)
1597 struct buffer_page *new_head;
1602 entries = rb_page_entries(next_page);
1605 * The hard part is here. We need to move the head
1606 * forward, and protect against both readers on
1607 * other CPUs and writers coming in via interrupts.
1609 type = rb_head_page_set_update(cpu_buffer, next_page, tail_page,
1613 * type can be one of four:
1614 * NORMAL - an interrupt already moved it for us
1615 * HEAD - we are the first to get here.
1616 * UPDATE - we are the interrupt interrupting
1618 * MOVED - a reader on another CPU moved the next
1619 * pointer to its reader page. Give up
1626 * We changed the head to UPDATE, thus
1627 * it is our responsibility to update
1630 local_add(entries, &cpu_buffer->overrun);
1633 * The entries will be zeroed out when we move the
1637 /* still more to do */
1640 case RB_PAGE_UPDATE:
1642 * This is an interrupt that interrupt the
1643 * previous update. Still more to do.
1646 case RB_PAGE_NORMAL:
1648 * An interrupt came in before the update
1649 * and processed this for us.
1650 * Nothing left to do.
1655 * The reader is on another CPU and just did
1656 * a swap with our next_page.
1661 RB_WARN_ON(cpu_buffer, 1); /* WTF??? */
1666 * Now that we are here, the old head pointer is
1667 * set to UPDATE. This will keep the reader from
1668 * swapping the head page with the reader page.
1669 * The reader (on another CPU) will spin till
1672 * We just need to protect against interrupts
1673 * doing the job. We will set the next pointer
1674 * to HEAD. After that, we set the old pointer
1675 * to NORMAL, but only if it was HEAD before.
1676 * otherwise we are an interrupt, and only
1677 * want the outer most commit to reset it.
1679 new_head = next_page;
1680 rb_inc_page(cpu_buffer, &new_head);
1682 ret = rb_head_page_set_head(cpu_buffer, new_head, next_page,
1686 * Valid returns are:
1687 * HEAD - an interrupt came in and already set it.
1688 * NORMAL - One of two things:
1689 * 1) We really set it.
1690 * 2) A bunch of interrupts came in and moved
1691 * the page forward again.
1695 case RB_PAGE_NORMAL:
1699 RB_WARN_ON(cpu_buffer, 1);
1704 * It is possible that an interrupt came in,
1705 * set the head up, then more interrupts came in
1706 * and moved it again. When we get back here,
1707 * the page would have been set to NORMAL but we
1708 * just set it back to HEAD.
1710 * How do you detect this? Well, if that happened
1711 * the tail page would have moved.
1713 if (ret == RB_PAGE_NORMAL) {
1715 * If the tail had moved passed next, then we need
1716 * to reset the pointer.
1718 if (cpu_buffer->tail_page != tail_page &&
1719 cpu_buffer->tail_page != next_page)
1720 rb_head_page_set_normal(cpu_buffer, new_head,
1726 * If this was the outer most commit (the one that
1727 * changed the original pointer from HEAD to UPDATE),
1728 * then it is up to us to reset it to NORMAL.
1730 if (type == RB_PAGE_HEAD) {
1731 ret = rb_head_page_set_normal(cpu_buffer, next_page,
1734 if (RB_WARN_ON(cpu_buffer,
1735 ret != RB_PAGE_UPDATE))
1742 static unsigned rb_calculate_event_length(unsigned length)
1744 struct ring_buffer_event event; /* Used only for sizeof array */
1746 /* zero length can cause confusions */
1750 if (length > RB_MAX_SMALL_DATA || RB_FORCE_8BYTE_ALIGNMENT)
1751 length += sizeof(event.array[0]);
1753 length += RB_EVNT_HDR_SIZE;
1754 length = ALIGN(length, RB_ARCH_ALIGNMENT);
1760 rb_reset_tail(struct ring_buffer_per_cpu *cpu_buffer,
1761 struct buffer_page *tail_page,
1762 unsigned long tail, unsigned long length)
1764 struct ring_buffer_event *event;
1767 * Only the event that crossed the page boundary
1768 * must fill the old tail_page with padding.
1770 if (tail >= BUF_PAGE_SIZE) {
1771 local_sub(length, &tail_page->write);
1775 event = __rb_page_index(tail_page, tail);
1776 kmemcheck_annotate_bitfield(event, bitfield);
1779 * Save the original length to the meta data.
1780 * This will be used by the reader to add lost event
1783 tail_page->real_end = tail;
1786 * If this event is bigger than the minimum size, then
1787 * we need to be careful that we don't subtract the
1788 * write counter enough to allow another writer to slip
1790 * We put in a discarded commit instead, to make sure
1791 * that this space is not used again.
1793 * If we are less than the minimum size, we don't need to
1796 if (tail > (BUF_PAGE_SIZE - RB_EVNT_MIN_SIZE)) {
1797 /* No room for any events */
1799 /* Mark the rest of the page with padding */
1800 rb_event_set_padding(event);
1802 /* Set the write back to the previous setting */
1803 local_sub(length, &tail_page->write);
1807 /* Put in a discarded event */
1808 event->array[0] = (BUF_PAGE_SIZE - tail) - RB_EVNT_HDR_SIZE;
1809 event->type_len = RINGBUF_TYPE_PADDING;
1810 /* time delta must be non zero */
1811 event->time_delta = 1;
1813 /* Set write to end of buffer */
1814 length = (tail + length) - BUF_PAGE_SIZE;
1815 local_sub(length, &tail_page->write);
1818 static struct ring_buffer_event *
1819 rb_move_tail(struct ring_buffer_per_cpu *cpu_buffer,
1820 unsigned long length, unsigned long tail,
1821 struct buffer_page *tail_page, u64 *ts)
1823 struct buffer_page *commit_page = cpu_buffer->commit_page;
1824 struct ring_buffer *buffer = cpu_buffer->buffer;
1825 struct buffer_page *next_page;
1828 next_page = tail_page;
1830 rb_inc_page(cpu_buffer, &next_page);
1833 * If for some reason, we had an interrupt storm that made
1834 * it all the way around the buffer, bail, and warn
1837 if (unlikely(next_page == commit_page)) {
1838 local_inc(&cpu_buffer->commit_overrun);
1843 * This is where the fun begins!
1845 * We are fighting against races between a reader that
1846 * could be on another CPU trying to swap its reader
1847 * page with the buffer head.
1849 * We are also fighting against interrupts coming in and
1850 * moving the head or tail on us as well.
1852 * If the next page is the head page then we have filled
1853 * the buffer, unless the commit page is still on the
1856 if (rb_is_head_page(cpu_buffer, next_page, &tail_page->list)) {
1859 * If the commit is not on the reader page, then
1860 * move the header page.
1862 if (!rb_is_reader_page(cpu_buffer->commit_page)) {
1864 * If we are not in overwrite mode,
1865 * this is easy, just stop here.
1867 if (!(buffer->flags & RB_FL_OVERWRITE))
1870 ret = rb_handle_head_page(cpu_buffer,
1879 * We need to be careful here too. The
1880 * commit page could still be on the reader
1881 * page. We could have a small buffer, and
1882 * have filled up the buffer with events
1883 * from interrupts and such, and wrapped.
1885 * Note, if the tail page is also the on the
1886 * reader_page, we let it move out.
1888 if (unlikely((cpu_buffer->commit_page !=
1889 cpu_buffer->tail_page) &&
1890 (cpu_buffer->commit_page ==
1891 cpu_buffer->reader_page))) {
1892 local_inc(&cpu_buffer->commit_overrun);
1898 ret = rb_tail_page_update(cpu_buffer, tail_page, next_page);
1901 * Nested commits always have zero deltas, so
1902 * just reread the time stamp
1904 *ts = rb_time_stamp(buffer);
1905 next_page->page->time_stamp = *ts;
1910 rb_reset_tail(cpu_buffer, tail_page, tail, length);
1912 /* fail and let the caller try again */
1913 return ERR_PTR(-EAGAIN);
1917 rb_reset_tail(cpu_buffer, tail_page, tail, length);
1922 static struct ring_buffer_event *
1923 __rb_reserve_next(struct ring_buffer_per_cpu *cpu_buffer,
1924 unsigned type, unsigned long length, u64 *ts)
1926 struct buffer_page *tail_page;
1927 struct ring_buffer_event *event;
1928 unsigned long tail, write;
1930 tail_page = cpu_buffer->tail_page;
1931 write = local_add_return(length, &tail_page->write);
1933 /* set write to only the index of the write */
1934 write &= RB_WRITE_MASK;
1935 tail = write - length;
1937 /* See if we shot pass the end of this buffer page */
1938 if (write > BUF_PAGE_SIZE)
1939 return rb_move_tail(cpu_buffer, length, tail,
1942 /* We reserved something on the buffer */
1944 event = __rb_page_index(tail_page, tail);
1945 kmemcheck_annotate_bitfield(event, bitfield);
1946 rb_update_event(event, type, length);
1948 /* The passed in type is zero for DATA */
1950 local_inc(&tail_page->entries);
1953 * If this is the first commit on the page, then update
1957 tail_page->page->time_stamp = *ts;
1963 rb_try_to_discard(struct ring_buffer_per_cpu *cpu_buffer,
1964 struct ring_buffer_event *event)
1966 unsigned long new_index, old_index;
1967 struct buffer_page *bpage;
1968 unsigned long index;
1971 new_index = rb_event_index(event);
1972 old_index = new_index + rb_event_length(event);
1973 addr = (unsigned long)event;
1976 bpage = cpu_buffer->tail_page;
1978 if (bpage->page == (void *)addr && rb_page_write(bpage) == old_index) {
1979 unsigned long write_mask =
1980 local_read(&bpage->write) & ~RB_WRITE_MASK;
1982 * This is on the tail page. It is possible that
1983 * a write could come in and move the tail page
1984 * and write to the next page. That is fine
1985 * because we just shorten what is on this page.
1987 old_index += write_mask;
1988 new_index += write_mask;
1989 index = local_cmpxchg(&bpage->write, old_index, new_index);
1990 if (index == old_index)
1994 /* could not discard */
1999 rb_add_time_stamp(struct ring_buffer_per_cpu *cpu_buffer,
2000 u64 *ts, u64 *delta)
2002 struct ring_buffer_event *event;
2005 WARN_ONCE(*delta > (1ULL << 59),
2006 KERN_WARNING "Delta way too big! %llu ts=%llu write stamp = %llu\n",
2007 (unsigned long long)*delta,
2008 (unsigned long long)*ts,
2009 (unsigned long long)cpu_buffer->write_stamp);
2012 * The delta is too big, we to add a
2015 event = __rb_reserve_next(cpu_buffer,
2016 RINGBUF_TYPE_TIME_EXTEND,
2022 if (PTR_ERR(event) == -EAGAIN)
2025 /* Only a commited time event can update the write stamp */
2026 if (rb_event_is_commit(cpu_buffer, event)) {
2028 * If this is the first on the page, then it was
2029 * updated with the page itself. Try to discard it
2030 * and if we can't just make it zero.
2032 if (rb_event_index(event)) {
2033 event->time_delta = *delta & TS_MASK;
2034 event->array[0] = *delta >> TS_SHIFT;
2036 /* try to discard, since we do not need this */
2037 if (!rb_try_to_discard(cpu_buffer, event)) {
2038 /* nope, just zero it */
2039 event->time_delta = 0;
2040 event->array[0] = 0;
2043 cpu_buffer->write_stamp = *ts;
2044 /* let the caller know this was the commit */
2047 /* Try to discard the event */
2048 if (!rb_try_to_discard(cpu_buffer, event)) {
2049 /* Darn, this is just wasted space */
2050 event->time_delta = 0;
2051 event->array[0] = 0;
2061 static void rb_start_commit(struct ring_buffer_per_cpu *cpu_buffer)
2063 local_inc(&cpu_buffer->committing);
2064 local_inc(&cpu_buffer->commits);
2067 static void rb_end_commit(struct ring_buffer_per_cpu *cpu_buffer)
2069 unsigned long commits;
2071 if (RB_WARN_ON(cpu_buffer,
2072 !local_read(&cpu_buffer->committing)))
2076 commits = local_read(&cpu_buffer->commits);
2077 /* synchronize with interrupts */
2079 if (local_read(&cpu_buffer->committing) == 1)
2080 rb_set_commit_to_write(cpu_buffer);
2082 local_dec(&cpu_buffer->committing);
2084 /* synchronize with interrupts */
2088 * Need to account for interrupts coming in between the
2089 * updating of the commit page and the clearing of the
2090 * committing counter.
2092 if (unlikely(local_read(&cpu_buffer->commits) != commits) &&
2093 !local_read(&cpu_buffer->committing)) {
2094 local_inc(&cpu_buffer->committing);
2099 static struct ring_buffer_event *
2100 rb_reserve_next_event(struct ring_buffer *buffer,
2101 struct ring_buffer_per_cpu *cpu_buffer,
2102 unsigned long length)
2104 struct ring_buffer_event *event;
2109 rb_start_commit(cpu_buffer);
2111 #ifdef CONFIG_RING_BUFFER_ALLOW_SWAP
2113 * Due to the ability to swap a cpu buffer from a buffer
2114 * it is possible it was swapped before we committed.
2115 * (committing stops a swap). We check for it here and
2116 * if it happened, we have to fail the write.
2119 if (unlikely(ACCESS_ONCE(cpu_buffer->buffer) != buffer)) {
2120 local_dec(&cpu_buffer->committing);
2121 local_dec(&cpu_buffer->commits);
2126 length = rb_calculate_event_length(length);
2129 * We allow for interrupts to reenter here and do a trace.
2130 * If one does, it will cause this original code to loop
2131 * back here. Even with heavy interrupts happening, this
2132 * should only happen a few times in a row. If this happens
2133 * 1000 times in a row, there must be either an interrupt
2134 * storm or we have something buggy.
2137 if (RB_WARN_ON(cpu_buffer, ++nr_loops > 1000))
2140 ts = rb_time_stamp(cpu_buffer->buffer);
2143 * Only the first commit can update the timestamp.
2144 * Yes there is a race here. If an interrupt comes in
2145 * just after the conditional and it traces too, then it
2146 * will also check the deltas. More than one timestamp may
2147 * also be made. But only the entry that did the actual
2148 * commit will be something other than zero.
2150 if (likely(cpu_buffer->tail_page == cpu_buffer->commit_page &&
2151 rb_page_write(cpu_buffer->tail_page) ==
2152 rb_commit_index(cpu_buffer))) {
2155 diff = ts - cpu_buffer->write_stamp;
2157 /* make sure this diff is calculated here */
2160 /* Did the write stamp get updated already? */
2161 if (unlikely(ts < cpu_buffer->write_stamp))
2165 if (unlikely(test_time_stamp(delta))) {
2167 commit = rb_add_time_stamp(cpu_buffer, &ts, &delta);
2168 if (commit == -EBUSY)
2171 if (commit == -EAGAIN)
2174 RB_WARN_ON(cpu_buffer, commit < 0);
2179 event = __rb_reserve_next(cpu_buffer, 0, length, &ts);
2180 if (unlikely(PTR_ERR(event) == -EAGAIN))
2186 if (!rb_event_is_commit(cpu_buffer, event))
2189 event->time_delta = delta;
2194 rb_end_commit(cpu_buffer);
2198 #ifdef CONFIG_TRACING
2200 #define TRACE_RECURSIVE_DEPTH 16
2202 static int trace_recursive_lock(void)
2204 current->trace_recursion++;
2206 if (likely(current->trace_recursion < TRACE_RECURSIVE_DEPTH))
2209 /* Disable all tracing before we do anything else */
2210 tracing_off_permanent();
2212 printk_once(KERN_WARNING "Tracing recursion: depth[%ld]:"
2213 "HC[%lu]:SC[%lu]:NMI[%lu]\n",
2214 current->trace_recursion,
2215 hardirq_count() >> HARDIRQ_SHIFT,
2216 softirq_count() >> SOFTIRQ_SHIFT,
2223 static void trace_recursive_unlock(void)
2225 WARN_ON_ONCE(!current->trace_recursion);
2227 current->trace_recursion--;
2232 #define trace_recursive_lock() (0)
2233 #define trace_recursive_unlock() do { } while (0)
2237 static DEFINE_PER_CPU(int, rb_need_resched);
2240 * ring_buffer_lock_reserve - reserve a part of the buffer
2241 * @buffer: the ring buffer to reserve from
2242 * @length: the length of the data to reserve (excluding event header)
2244 * Returns a reseverd event on the ring buffer to copy directly to.
2245 * The user of this interface will need to get the body to write into
2246 * and can use the ring_buffer_event_data() interface.
2248 * The length is the length of the data needed, not the event length
2249 * which also includes the event header.
2251 * Must be paired with ring_buffer_unlock_commit, unless NULL is returned.
2252 * If NULL is returned, then nothing has been allocated or locked.
2254 struct ring_buffer_event *
2255 ring_buffer_lock_reserve(struct ring_buffer *buffer, unsigned long length)
2257 struct ring_buffer_per_cpu *cpu_buffer;
2258 struct ring_buffer_event *event;
2261 if (ring_buffer_flags != RB_BUFFERS_ON)
2264 /* If we are tracing schedule, we don't want to recurse */
2265 resched = ftrace_preempt_disable();
2267 if (atomic_read(&buffer->record_disabled))
2270 if (trace_recursive_lock())
2273 cpu = raw_smp_processor_id();
2275 if (!cpumask_test_cpu(cpu, buffer->cpumask))
2278 cpu_buffer = buffer->buffers[cpu];
2280 if (atomic_read(&cpu_buffer->record_disabled))
2283 if (length > BUF_MAX_DATA_SIZE)
2286 event = rb_reserve_next_event(buffer, cpu_buffer, length);
2291 * Need to store resched state on this cpu.
2292 * Only the first needs to.
2295 if (preempt_count() == 1)
2296 per_cpu(rb_need_resched, cpu) = resched;
2301 trace_recursive_unlock();
2304 ftrace_preempt_enable(resched);
2307 EXPORT_SYMBOL_GPL(ring_buffer_lock_reserve);
2310 rb_update_write_stamp(struct ring_buffer_per_cpu *cpu_buffer,
2311 struct ring_buffer_event *event)
2314 * The event first in the commit queue updates the
2317 if (rb_event_is_commit(cpu_buffer, event))
2318 cpu_buffer->write_stamp += event->time_delta;
2321 static void rb_commit(struct ring_buffer_per_cpu *cpu_buffer,
2322 struct ring_buffer_event *event)
2324 local_inc(&cpu_buffer->entries);
2325 rb_update_write_stamp(cpu_buffer, event);
2326 rb_end_commit(cpu_buffer);
2330 * ring_buffer_unlock_commit - commit a reserved
2331 * @buffer: The buffer to commit to
2332 * @event: The event pointer to commit.
2334 * This commits the data to the ring buffer, and releases any locks held.
2336 * Must be paired with ring_buffer_lock_reserve.
2338 int ring_buffer_unlock_commit(struct ring_buffer *buffer,
2339 struct ring_buffer_event *event)
2341 struct ring_buffer_per_cpu *cpu_buffer;
2342 int cpu = raw_smp_processor_id();
2344 cpu_buffer = buffer->buffers[cpu];
2346 rb_commit(cpu_buffer, event);
2348 trace_recursive_unlock();
2351 * Only the last preempt count needs to restore preemption.
2353 if (preempt_count() == 1)
2354 ftrace_preempt_enable(per_cpu(rb_need_resched, cpu));
2356 preempt_enable_no_resched_notrace();
2360 EXPORT_SYMBOL_GPL(ring_buffer_unlock_commit);
2362 static inline void rb_event_discard(struct ring_buffer_event *event)
2364 /* array[0] holds the actual length for the discarded event */
2365 event->array[0] = rb_event_data_length(event) - RB_EVNT_HDR_SIZE;
2366 event->type_len = RINGBUF_TYPE_PADDING;
2367 /* time delta must be non zero */
2368 if (!event->time_delta)
2369 event->time_delta = 1;
2373 * Decrement the entries to the page that an event is on.
2374 * The event does not even need to exist, only the pointer
2375 * to the page it is on. This may only be called before the commit
2379 rb_decrement_entry(struct ring_buffer_per_cpu *cpu_buffer,
2380 struct ring_buffer_event *event)
2382 unsigned long addr = (unsigned long)event;
2383 struct buffer_page *bpage = cpu_buffer->commit_page;
2384 struct buffer_page *start;
2388 /* Do the likely case first */
2389 if (likely(bpage->page == (void *)addr)) {
2390 local_dec(&bpage->entries);
2395 * Because the commit page may be on the reader page we
2396 * start with the next page and check the end loop there.
2398 rb_inc_page(cpu_buffer, &bpage);
2401 if (bpage->page == (void *)addr) {
2402 local_dec(&bpage->entries);
2405 rb_inc_page(cpu_buffer, &bpage);
2406 } while (bpage != start);
2408 /* commit not part of this buffer?? */
2409 RB_WARN_ON(cpu_buffer, 1);
2413 * ring_buffer_commit_discard - discard an event that has not been committed
2414 * @buffer: the ring buffer
2415 * @event: non committed event to discard
2417 * Sometimes an event that is in the ring buffer needs to be ignored.
2418 * This function lets the user discard an event in the ring buffer
2419 * and then that event will not be read later.
2421 * This function only works if it is called before the the item has been
2422 * committed. It will try to free the event from the ring buffer
2423 * if another event has not been added behind it.
2425 * If another event has been added behind it, it will set the event
2426 * up as discarded, and perform the commit.
2428 * If this function is called, do not call ring_buffer_unlock_commit on
2431 void ring_buffer_discard_commit(struct ring_buffer *buffer,
2432 struct ring_buffer_event *event)
2434 struct ring_buffer_per_cpu *cpu_buffer;
2437 /* The event is discarded regardless */
2438 rb_event_discard(event);
2440 cpu = smp_processor_id();
2441 cpu_buffer = buffer->buffers[cpu];
2444 * This must only be called if the event has not been
2445 * committed yet. Thus we can assume that preemption
2446 * is still disabled.
2448 RB_WARN_ON(buffer, !local_read(&cpu_buffer->committing));
2450 rb_decrement_entry(cpu_buffer, event);
2451 if (rb_try_to_discard(cpu_buffer, event))
2455 * The commit is still visible by the reader, so we
2456 * must still update the timestamp.
2458 rb_update_write_stamp(cpu_buffer, event);
2460 rb_end_commit(cpu_buffer);
2462 trace_recursive_unlock();
2465 * Only the last preempt count needs to restore preemption.
2467 if (preempt_count() == 1)
2468 ftrace_preempt_enable(per_cpu(rb_need_resched, cpu));
2470 preempt_enable_no_resched_notrace();
2473 EXPORT_SYMBOL_GPL(ring_buffer_discard_commit);
2476 * ring_buffer_write - write data to the buffer without reserving
2477 * @buffer: The ring buffer to write to.
2478 * @length: The length of the data being written (excluding the event header)
2479 * @data: The data to write to the buffer.
2481 * This is like ring_buffer_lock_reserve and ring_buffer_unlock_commit as
2482 * one function. If you already have the data to write to the buffer, it
2483 * may be easier to simply call this function.
2485 * Note, like ring_buffer_lock_reserve, the length is the length of the data
2486 * and not the length of the event which would hold the header.
2488 int ring_buffer_write(struct ring_buffer *buffer,
2489 unsigned long length,
2492 struct ring_buffer_per_cpu *cpu_buffer;
2493 struct ring_buffer_event *event;
2498 if (ring_buffer_flags != RB_BUFFERS_ON)
2501 resched = ftrace_preempt_disable();
2503 if (atomic_read(&buffer->record_disabled))
2506 cpu = raw_smp_processor_id();
2508 if (!cpumask_test_cpu(cpu, buffer->cpumask))
2511 cpu_buffer = buffer->buffers[cpu];
2513 if (atomic_read(&cpu_buffer->record_disabled))
2516 if (length > BUF_MAX_DATA_SIZE)
2519 event = rb_reserve_next_event(buffer, cpu_buffer, length);
2523 body = rb_event_data(event);
2525 memcpy(body, data, length);
2527 rb_commit(cpu_buffer, event);
2531 ftrace_preempt_enable(resched);
2535 EXPORT_SYMBOL_GPL(ring_buffer_write);
2537 static int rb_per_cpu_empty(struct ring_buffer_per_cpu *cpu_buffer)
2539 struct buffer_page *reader = cpu_buffer->reader_page;
2540 struct buffer_page *head = rb_set_head_page(cpu_buffer);
2541 struct buffer_page *commit = cpu_buffer->commit_page;
2543 /* In case of error, head will be NULL */
2544 if (unlikely(!head))
2547 return reader->read == rb_page_commit(reader) &&
2548 (commit == reader ||
2550 head->read == rb_page_commit(commit)));
2554 * ring_buffer_record_disable - stop all writes into the buffer
2555 * @buffer: The ring buffer to stop writes to.
2557 * This prevents all writes to the buffer. Any attempt to write
2558 * to the buffer after this will fail and return NULL.
2560 * The caller should call synchronize_sched() after this.
2562 void ring_buffer_record_disable(struct ring_buffer *buffer)
2564 atomic_inc(&buffer->record_disabled);
2566 EXPORT_SYMBOL_GPL(ring_buffer_record_disable);
2569 * ring_buffer_record_enable - enable writes to the buffer
2570 * @buffer: The ring buffer to enable writes
2572 * Note, multiple disables will need the same number of enables
2573 * to truly enable the writing (much like preempt_disable).
2575 void ring_buffer_record_enable(struct ring_buffer *buffer)
2577 atomic_dec(&buffer->record_disabled);
2579 EXPORT_SYMBOL_GPL(ring_buffer_record_enable);
2582 * ring_buffer_record_disable_cpu - stop all writes into the cpu_buffer
2583 * @buffer: The ring buffer to stop writes to.
2584 * @cpu: The CPU buffer to stop
2586 * This prevents all writes to the buffer. Any attempt to write
2587 * to the buffer after this will fail and return NULL.
2589 * The caller should call synchronize_sched() after this.
2591 void ring_buffer_record_disable_cpu(struct ring_buffer *buffer, int cpu)
2593 struct ring_buffer_per_cpu *cpu_buffer;
2595 if (!cpumask_test_cpu(cpu, buffer->cpumask))
2598 cpu_buffer = buffer->buffers[cpu];
2599 atomic_inc(&cpu_buffer->record_disabled);
2601 EXPORT_SYMBOL_GPL(ring_buffer_record_disable_cpu);
2604 * ring_buffer_record_enable_cpu - enable writes to the buffer
2605 * @buffer: The ring buffer to enable writes
2606 * @cpu: The CPU to enable.
2608 * Note, multiple disables will need the same number of enables
2609 * to truly enable the writing (much like preempt_disable).
2611 void ring_buffer_record_enable_cpu(struct ring_buffer *buffer, int cpu)
2613 struct ring_buffer_per_cpu *cpu_buffer;
2615 if (!cpumask_test_cpu(cpu, buffer->cpumask))
2618 cpu_buffer = buffer->buffers[cpu];
2619 atomic_dec(&cpu_buffer->record_disabled);
2621 EXPORT_SYMBOL_GPL(ring_buffer_record_enable_cpu);
2624 * ring_buffer_entries_cpu - get the number of entries in a cpu buffer
2625 * @buffer: The ring buffer
2626 * @cpu: The per CPU buffer to get the entries from.
2628 unsigned long ring_buffer_entries_cpu(struct ring_buffer *buffer, int cpu)
2630 struct ring_buffer_per_cpu *cpu_buffer;
2633 if (!cpumask_test_cpu(cpu, buffer->cpumask))
2636 cpu_buffer = buffer->buffers[cpu];
2637 ret = (local_read(&cpu_buffer->entries) - local_read(&cpu_buffer->overrun))
2642 EXPORT_SYMBOL_GPL(ring_buffer_entries_cpu);
2645 * ring_buffer_overrun_cpu - get the number of overruns in a cpu_buffer
2646 * @buffer: The ring buffer
2647 * @cpu: The per CPU buffer to get the number of overruns from
2649 unsigned long ring_buffer_overrun_cpu(struct ring_buffer *buffer, int cpu)
2651 struct ring_buffer_per_cpu *cpu_buffer;
2654 if (!cpumask_test_cpu(cpu, buffer->cpumask))
2657 cpu_buffer = buffer->buffers[cpu];
2658 ret = local_read(&cpu_buffer->overrun);
2662 EXPORT_SYMBOL_GPL(ring_buffer_overrun_cpu);
2665 * ring_buffer_commit_overrun_cpu - get the number of overruns caused by commits
2666 * @buffer: The ring buffer
2667 * @cpu: The per CPU buffer to get the number of overruns from
2670 ring_buffer_commit_overrun_cpu(struct ring_buffer *buffer, int cpu)
2672 struct ring_buffer_per_cpu *cpu_buffer;
2675 if (!cpumask_test_cpu(cpu, buffer->cpumask))
2678 cpu_buffer = buffer->buffers[cpu];
2679 ret = local_read(&cpu_buffer->commit_overrun);
2683 EXPORT_SYMBOL_GPL(ring_buffer_commit_overrun_cpu);
2686 * ring_buffer_entries - get the number of entries in a buffer
2687 * @buffer: The ring buffer
2689 * Returns the total number of entries in the ring buffer
2692 unsigned long ring_buffer_entries(struct ring_buffer *buffer)
2694 struct ring_buffer_per_cpu *cpu_buffer;
2695 unsigned long entries = 0;
2698 /* if you care about this being correct, lock the buffer */
2699 for_each_buffer_cpu(buffer, cpu) {
2700 cpu_buffer = buffer->buffers[cpu];
2701 entries += (local_read(&cpu_buffer->entries) -
2702 local_read(&cpu_buffer->overrun)) - cpu_buffer->read;
2707 EXPORT_SYMBOL_GPL(ring_buffer_entries);
2710 * ring_buffer_overruns - get the number of overruns in buffer
2711 * @buffer: The ring buffer
2713 * Returns the total number of overruns in the ring buffer
2716 unsigned long ring_buffer_overruns(struct ring_buffer *buffer)
2718 struct ring_buffer_per_cpu *cpu_buffer;
2719 unsigned long overruns = 0;
2722 /* if you care about this being correct, lock the buffer */
2723 for_each_buffer_cpu(buffer, cpu) {
2724 cpu_buffer = buffer->buffers[cpu];
2725 overruns += local_read(&cpu_buffer->overrun);
2730 EXPORT_SYMBOL_GPL(ring_buffer_overruns);
2732 static void rb_iter_reset(struct ring_buffer_iter *iter)
2734 struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
2736 /* Iterator usage is expected to have record disabled */
2737 if (list_empty(&cpu_buffer->reader_page->list)) {
2738 iter->head_page = rb_set_head_page(cpu_buffer);
2739 if (unlikely(!iter->head_page))
2741 iter->head = iter->head_page->read;
2743 iter->head_page = cpu_buffer->reader_page;
2744 iter->head = cpu_buffer->reader_page->read;
2747 iter->read_stamp = cpu_buffer->read_stamp;
2749 iter->read_stamp = iter->head_page->page->time_stamp;
2750 iter->cache_reader_page = cpu_buffer->reader_page;
2751 iter->cache_read = cpu_buffer->read;
2755 * ring_buffer_iter_reset - reset an iterator
2756 * @iter: The iterator to reset
2758 * Resets the iterator, so that it will start from the beginning
2761 void ring_buffer_iter_reset(struct ring_buffer_iter *iter)
2763 struct ring_buffer_per_cpu *cpu_buffer;
2764 unsigned long flags;
2769 cpu_buffer = iter->cpu_buffer;
2771 spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
2772 rb_iter_reset(iter);
2773 spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
2775 EXPORT_SYMBOL_GPL(ring_buffer_iter_reset);
2778 * ring_buffer_iter_empty - check if an iterator has no more to read
2779 * @iter: The iterator to check
2781 int ring_buffer_iter_empty(struct ring_buffer_iter *iter)
2783 struct ring_buffer_per_cpu *cpu_buffer;
2785 cpu_buffer = iter->cpu_buffer;
2787 return iter->head_page == cpu_buffer->commit_page &&
2788 iter->head == rb_commit_index(cpu_buffer);
2790 EXPORT_SYMBOL_GPL(ring_buffer_iter_empty);
2793 rb_update_read_stamp(struct ring_buffer_per_cpu *cpu_buffer,
2794 struct ring_buffer_event *event)
2798 switch (event->type_len) {
2799 case RINGBUF_TYPE_PADDING:
2802 case RINGBUF_TYPE_TIME_EXTEND:
2803 delta = event->array[0];
2805 delta += event->time_delta;
2806 cpu_buffer->read_stamp += delta;
2809 case RINGBUF_TYPE_TIME_STAMP:
2810 /* FIXME: not implemented */
2813 case RINGBUF_TYPE_DATA:
2814 cpu_buffer->read_stamp += event->time_delta;
2824 rb_update_iter_read_stamp(struct ring_buffer_iter *iter,
2825 struct ring_buffer_event *event)
2829 switch (event->type_len) {
2830 case RINGBUF_TYPE_PADDING:
2833 case RINGBUF_TYPE_TIME_EXTEND:
2834 delta = event->array[0];
2836 delta += event->time_delta;
2837 iter->read_stamp += delta;
2840 case RINGBUF_TYPE_TIME_STAMP:
2841 /* FIXME: not implemented */
2844 case RINGBUF_TYPE_DATA:
2845 iter->read_stamp += event->time_delta;
2854 static struct buffer_page *
2855 rb_get_reader_page(struct ring_buffer_per_cpu *cpu_buffer)
2857 struct buffer_page *reader = NULL;
2858 unsigned long overwrite;
2859 unsigned long flags;
2863 local_irq_save(flags);
2864 arch_spin_lock(&cpu_buffer->lock);
2868 * This should normally only loop twice. But because the
2869 * start of the reader inserts an empty page, it causes
2870 * a case where we will loop three times. There should be no
2871 * reason to loop four times (that I know of).
2873 if (RB_WARN_ON(cpu_buffer, ++nr_loops > 3)) {
2878 reader = cpu_buffer->reader_page;
2880 /* If there's more to read, return this page */
2881 if (cpu_buffer->reader_page->read < rb_page_size(reader))
2884 /* Never should we have an index greater than the size */
2885 if (RB_WARN_ON(cpu_buffer,
2886 cpu_buffer->reader_page->read > rb_page_size(reader)))
2889 /* check if we caught up to the tail */
2891 if (cpu_buffer->commit_page == cpu_buffer->reader_page)
2895 * Reset the reader page to size zero.
2897 local_set(&cpu_buffer->reader_page->write, 0);
2898 local_set(&cpu_buffer->reader_page->entries, 0);
2899 local_set(&cpu_buffer->reader_page->page->commit, 0);
2900 cpu_buffer->reader_page->real_end = 0;
2904 * Splice the empty reader page into the list around the head.
2906 reader = rb_set_head_page(cpu_buffer);
2907 cpu_buffer->reader_page->list.next = rb_list_head(reader->list.next);
2908 cpu_buffer->reader_page->list.prev = reader->list.prev;
2911 * cpu_buffer->pages just needs to point to the buffer, it
2912 * has no specific buffer page to point to. Lets move it out
2913 * of our way so we don't accidently swap it.
2915 cpu_buffer->pages = reader->list.prev;
2917 /* The reader page will be pointing to the new head */
2918 rb_set_list_to_head(cpu_buffer, &cpu_buffer->reader_page->list);
2921 * We want to make sure we read the overruns after we set up our
2922 * pointers to the next object. The writer side does a
2923 * cmpxchg to cross pages which acts as the mb on the writer
2924 * side. Note, the reader will constantly fail the swap
2925 * while the writer is updating the pointers, so this
2926 * guarantees that the overwrite recorded here is the one we
2927 * want to compare with the last_overrun.
2930 overwrite = local_read(&(cpu_buffer->overrun));
2933 * Here's the tricky part.
2935 * We need to move the pointer past the header page.
2936 * But we can only do that if a writer is not currently
2937 * moving it. The page before the header page has the
2938 * flag bit '1' set if it is pointing to the page we want.
2939 * but if the writer is in the process of moving it
2940 * than it will be '2' or already moved '0'.
2943 ret = rb_head_page_replace(reader, cpu_buffer->reader_page);
2946 * If we did not convert it, then we must try again.
2952 * Yeah! We succeeded in replacing the page.
2954 * Now make the new head point back to the reader page.
2956 rb_list_head(reader->list.next)->prev = &cpu_buffer->reader_page->list;
2957 rb_inc_page(cpu_buffer, &cpu_buffer->head_page);
2959 /* Finally update the reader page to the new head */
2960 cpu_buffer->reader_page = reader;
2961 rb_reset_reader_page(cpu_buffer);
2963 if (overwrite != cpu_buffer->last_overrun) {
2964 cpu_buffer->lost_events = overwrite - cpu_buffer->last_overrun;
2965 cpu_buffer->last_overrun = overwrite;
2971 arch_spin_unlock(&cpu_buffer->lock);
2972 local_irq_restore(flags);
2977 static void rb_advance_reader(struct ring_buffer_per_cpu *cpu_buffer)
2979 struct ring_buffer_event *event;
2980 struct buffer_page *reader;
2983 reader = rb_get_reader_page(cpu_buffer);
2985 /* This function should not be called when buffer is empty */
2986 if (RB_WARN_ON(cpu_buffer, !reader))
2989 event = rb_reader_event(cpu_buffer);
2991 if (event->type_len <= RINGBUF_TYPE_DATA_TYPE_LEN_MAX)
2994 rb_update_read_stamp(cpu_buffer, event);
2996 length = rb_event_length(event);
2997 cpu_buffer->reader_page->read += length;
3000 static void rb_advance_iter(struct ring_buffer_iter *iter)
3002 struct ring_buffer *buffer;
3003 struct ring_buffer_per_cpu *cpu_buffer;
3004 struct ring_buffer_event *event;
3007 cpu_buffer = iter->cpu_buffer;
3008 buffer = cpu_buffer->buffer;
3011 * Check if we are at the end of the buffer.
3013 if (iter->head >= rb_page_size(iter->head_page)) {
3014 /* discarded commits can make the page empty */
3015 if (iter->head_page == cpu_buffer->commit_page)
3021 event = rb_iter_head_event(iter);
3023 length = rb_event_length(event);
3026 * This should not be called to advance the header if we are
3027 * at the tail of the buffer.
3029 if (RB_WARN_ON(cpu_buffer,
3030 (iter->head_page == cpu_buffer->commit_page) &&
3031 (iter->head + length > rb_commit_index(cpu_buffer))))
3034 rb_update_iter_read_stamp(iter, event);
3036 iter->head += length;
3038 /* check for end of page padding */
3039 if ((iter->head >= rb_page_size(iter->head_page)) &&
3040 (iter->head_page != cpu_buffer->commit_page))
3041 rb_advance_iter(iter);
3044 static int rb_lost_events(struct ring_buffer_per_cpu *cpu_buffer)
3046 return cpu_buffer->lost_events;
3049 static struct ring_buffer_event *
3050 rb_buffer_peek(struct ring_buffer_per_cpu *cpu_buffer, u64 *ts,
3051 unsigned long *lost_events)
3053 struct ring_buffer_event *event;
3054 struct buffer_page *reader;
3059 * We repeat when a timestamp is encountered. It is possible
3060 * to get multiple timestamps from an interrupt entering just
3061 * as one timestamp is about to be written, or from discarded
3062 * commits. The most that we can have is the number on a single page.
3064 if (RB_WARN_ON(cpu_buffer, ++nr_loops > RB_TIMESTAMPS_PER_PAGE))
3067 reader = rb_get_reader_page(cpu_buffer);
3071 event = rb_reader_event(cpu_buffer);
3073 switch (event->type_len) {
3074 case RINGBUF_TYPE_PADDING:
3075 if (rb_null_event(event))
3076 RB_WARN_ON(cpu_buffer, 1);
3078 * Because the writer could be discarding every
3079 * event it creates (which would probably be bad)
3080 * if we were to go back to "again" then we may never
3081 * catch up, and will trigger the warn on, or lock
3082 * the box. Return the padding, and we will release
3083 * the current locks, and try again.
3087 case RINGBUF_TYPE_TIME_EXTEND:
3088 /* Internal data, OK to advance */
3089 rb_advance_reader(cpu_buffer);
3092 case RINGBUF_TYPE_TIME_STAMP:
3093 /* FIXME: not implemented */
3094 rb_advance_reader(cpu_buffer);
3097 case RINGBUF_TYPE_DATA:
3099 *ts = cpu_buffer->read_stamp + event->time_delta;
3100 ring_buffer_normalize_time_stamp(cpu_buffer->buffer,
3101 cpu_buffer->cpu, ts);
3104 *lost_events = rb_lost_events(cpu_buffer);
3113 EXPORT_SYMBOL_GPL(ring_buffer_peek);
3115 static struct ring_buffer_event *
3116 rb_iter_peek(struct ring_buffer_iter *iter, u64 *ts)
3118 struct ring_buffer *buffer;
3119 struct ring_buffer_per_cpu *cpu_buffer;
3120 struct ring_buffer_event *event;
3123 cpu_buffer = iter->cpu_buffer;
3124 buffer = cpu_buffer->buffer;
3127 * Check if someone performed a consuming read to
3128 * the buffer. A consuming read invalidates the iterator
3129 * and we need to reset the iterator in this case.
3131 if (unlikely(iter->cache_read != cpu_buffer->read ||
3132 iter->cache_reader_page != cpu_buffer->reader_page))
3133 rb_iter_reset(iter);
3136 if (ring_buffer_iter_empty(iter))
3140 * We repeat when a timestamp is encountered.
3141 * We can get multiple timestamps by nested interrupts or also
3142 * if filtering is on (discarding commits). Since discarding
3143 * commits can be frequent we can get a lot of timestamps.
3144 * But we limit them by not adding timestamps if they begin
3145 * at the start of a page.
3147 if (RB_WARN_ON(cpu_buffer, ++nr_loops > RB_TIMESTAMPS_PER_PAGE))
3150 if (rb_per_cpu_empty(cpu_buffer))
3153 if (iter->head >= local_read(&iter->head_page->page->commit)) {
3158 event = rb_iter_head_event(iter);
3160 switch (event->type_len) {
3161 case RINGBUF_TYPE_PADDING:
3162 if (rb_null_event(event)) {
3166 rb_advance_iter(iter);
3169 case RINGBUF_TYPE_TIME_EXTEND:
3170 /* Internal data, OK to advance */
3171 rb_advance_iter(iter);
3174 case RINGBUF_TYPE_TIME_STAMP:
3175 /* FIXME: not implemented */
3176 rb_advance_iter(iter);
3179 case RINGBUF_TYPE_DATA:
3181 *ts = iter->read_stamp + event->time_delta;
3182 ring_buffer_normalize_time_stamp(buffer,
3183 cpu_buffer->cpu, ts);
3193 EXPORT_SYMBOL_GPL(ring_buffer_iter_peek);
3195 static inline int rb_ok_to_lock(void)
3198 * If an NMI die dumps out the content of the ring buffer
3199 * do not grab locks. We also permanently disable the ring
3200 * buffer too. A one time deal is all you get from reading
3201 * the ring buffer from an NMI.
3203 if (likely(!in_nmi()))
3206 tracing_off_permanent();
3211 * ring_buffer_peek - peek at the next event to be read
3212 * @buffer: The ring buffer to read
3213 * @cpu: The cpu to peak at
3214 * @ts: The timestamp counter of this event.
3215 * @lost_events: a variable to store if events were lost (may be NULL)
3217 * This will return the event that will be read next, but does
3218 * not consume the data.
3220 struct ring_buffer_event *
3221 ring_buffer_peek(struct ring_buffer *buffer, int cpu, u64 *ts,
3222 unsigned long *lost_events)
3224 struct ring_buffer_per_cpu *cpu_buffer = buffer->buffers[cpu];
3225 struct ring_buffer_event *event;
3226 unsigned long flags;
3229 if (!cpumask_test_cpu(cpu, buffer->cpumask))
3232 dolock = rb_ok_to_lock();
3234 local_irq_save(flags);
3236 spin_lock(&cpu_buffer->reader_lock);
3237 event = rb_buffer_peek(cpu_buffer, ts, lost_events);
3238 if (event && event->type_len == RINGBUF_TYPE_PADDING)
3239 rb_advance_reader(cpu_buffer);
3241 spin_unlock(&cpu_buffer->reader_lock);
3242 local_irq_restore(flags);
3244 if (event && event->type_len == RINGBUF_TYPE_PADDING)
3251 * ring_buffer_iter_peek - peek at the next event to be read
3252 * @iter: The ring buffer iterator
3253 * @ts: The timestamp counter of this event.
3255 * This will return the event that will be read next, but does
3256 * not increment the iterator.
3258 struct ring_buffer_event *
3259 ring_buffer_iter_peek(struct ring_buffer_iter *iter, u64 *ts)
3261 struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
3262 struct ring_buffer_event *event;
3263 unsigned long flags;
3266 spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
3267 event = rb_iter_peek(iter, ts);
3268 spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
3270 if (event && event->type_len == RINGBUF_TYPE_PADDING)
3277 * ring_buffer_consume - return an event and consume it
3278 * @buffer: The ring buffer to get the next event from
3279 * @cpu: the cpu to read the buffer from
3280 * @ts: a variable to store the timestamp (may be NULL)
3281 * @lost_events: a variable to store if events were lost (may be NULL)
3283 * Returns the next event in the ring buffer, and that event is consumed.
3284 * Meaning, that sequential reads will keep returning a different event,
3285 * and eventually empty the ring buffer if the producer is slower.
3287 struct ring_buffer_event *
3288 ring_buffer_consume(struct ring_buffer *buffer, int cpu, u64 *ts,
3289 unsigned long *lost_events)
3291 struct ring_buffer_per_cpu *cpu_buffer;
3292 struct ring_buffer_event *event = NULL;
3293 unsigned long flags;
3296 dolock = rb_ok_to_lock();
3299 /* might be called in atomic */
3302 if (!cpumask_test_cpu(cpu, buffer->cpumask))
3305 cpu_buffer = buffer->buffers[cpu];
3306 local_irq_save(flags);
3308 spin_lock(&cpu_buffer->reader_lock);
3310 event = rb_buffer_peek(cpu_buffer, ts, lost_events);
3312 cpu_buffer->lost_events = 0;
3313 rb_advance_reader(cpu_buffer);
3317 spin_unlock(&cpu_buffer->reader_lock);
3318 local_irq_restore(flags);
3323 if (event && event->type_len == RINGBUF_TYPE_PADDING)
3328 EXPORT_SYMBOL_GPL(ring_buffer_consume);
3331 * ring_buffer_read_prepare - Prepare for a non consuming read of the buffer
3332 * @buffer: The ring buffer to read from
3333 * @cpu: The cpu buffer to iterate over
3335 * This performs the initial preparations necessary to iterate
3336 * through the buffer. Memory is allocated, buffer recording
3337 * is disabled, and the iterator pointer is returned to the caller.
3339 * Disabling buffer recordng prevents the reading from being
3340 * corrupted. This is not a consuming read, so a producer is not
3343 * After a sequence of ring_buffer_read_prepare calls, the user is
3344 * expected to make at least one call to ring_buffer_prepare_sync.
3345 * Afterwards, ring_buffer_read_start is invoked to get things going
3348 * This overall must be paired with ring_buffer_finish.
3350 struct ring_buffer_iter *
3351 ring_buffer_read_prepare(struct ring_buffer *buffer, int cpu)
3353 struct ring_buffer_per_cpu *cpu_buffer;
3354 struct ring_buffer_iter *iter;
3356 if (!cpumask_test_cpu(cpu, buffer->cpumask))
3359 iter = kmalloc(sizeof(*iter), GFP_KERNEL);
3363 cpu_buffer = buffer->buffers[cpu];
3365 iter->cpu_buffer = cpu_buffer;
3367 atomic_inc(&cpu_buffer->record_disabled);
3371 EXPORT_SYMBOL_GPL(ring_buffer_read_prepare);
3374 * ring_buffer_read_prepare_sync - Synchronize a set of prepare calls
3376 * All previously invoked ring_buffer_read_prepare calls to prepare
3377 * iterators will be synchronized. Afterwards, read_buffer_read_start
3378 * calls on those iterators are allowed.
3381 ring_buffer_read_prepare_sync(void)
3383 synchronize_sched();
3385 EXPORT_SYMBOL_GPL(ring_buffer_read_prepare_sync);
3388 * ring_buffer_read_start - start a non consuming read of the buffer
3389 * @iter: The iterator returned by ring_buffer_read_prepare
3391 * This finalizes the startup of an iteration through the buffer.
3392 * The iterator comes from a call to ring_buffer_read_prepare and
3393 * an intervening ring_buffer_read_prepare_sync must have been
3396 * Must be paired with ring_buffer_finish.
3399 ring_buffer_read_start(struct ring_buffer_iter *iter)
3401 struct ring_buffer_per_cpu *cpu_buffer;
3402 unsigned long flags;
3407 cpu_buffer = iter->cpu_buffer;
3409 spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
3410 arch_spin_lock(&cpu_buffer->lock);
3411 rb_iter_reset(iter);
3412 arch_spin_unlock(&cpu_buffer->lock);
3413 spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
3415 EXPORT_SYMBOL_GPL(ring_buffer_read_start);
3418 * ring_buffer_finish - finish reading the iterator of the buffer
3419 * @iter: The iterator retrieved by ring_buffer_start
3421 * This re-enables the recording to the buffer, and frees the
3425 ring_buffer_read_finish(struct ring_buffer_iter *iter)
3427 struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
3429 atomic_dec(&cpu_buffer->record_disabled);
3432 EXPORT_SYMBOL_GPL(ring_buffer_read_finish);
3435 * ring_buffer_read - read the next item in the ring buffer by the iterator
3436 * @iter: The ring buffer iterator
3437 * @ts: The time stamp of the event read.
3439 * This reads the next event in the ring buffer and increments the iterator.
3441 struct ring_buffer_event *
3442 ring_buffer_read(struct ring_buffer_iter *iter, u64 *ts)
3444 struct ring_buffer_event *event;
3445 struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
3446 unsigned long flags;
3448 spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
3450 event = rb_iter_peek(iter, ts);
3454 if (event->type_len == RINGBUF_TYPE_PADDING)
3457 rb_advance_iter(iter);
3459 spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
3463 EXPORT_SYMBOL_GPL(ring_buffer_read);
3466 * ring_buffer_size - return the size of the ring buffer (in bytes)
3467 * @buffer: The ring buffer.
3469 unsigned long ring_buffer_size(struct ring_buffer *buffer)
3471 return BUF_PAGE_SIZE * buffer->pages;
3473 EXPORT_SYMBOL_GPL(ring_buffer_size);
3476 rb_reset_cpu(struct ring_buffer_per_cpu *cpu_buffer)
3478 rb_head_page_deactivate(cpu_buffer);
3480 cpu_buffer->head_page
3481 = list_entry(cpu_buffer->pages, struct buffer_page, list);
3482 local_set(&cpu_buffer->head_page->write, 0);
3483 local_set(&cpu_buffer->head_page->entries, 0);
3484 local_set(&cpu_buffer->head_page->page->commit, 0);
3486 cpu_buffer->head_page->read = 0;
3488 cpu_buffer->tail_page = cpu_buffer->head_page;
3489 cpu_buffer->commit_page = cpu_buffer->head_page;
3491 INIT_LIST_HEAD(&cpu_buffer->reader_page->list);
3492 local_set(&cpu_buffer->reader_page->write, 0);
3493 local_set(&cpu_buffer->reader_page->entries, 0);
3494 local_set(&cpu_buffer->reader_page->page->commit, 0);
3495 cpu_buffer->reader_page->read = 0;
3497 local_set(&cpu_buffer->commit_overrun, 0);
3498 local_set(&cpu_buffer->overrun, 0);
3499 local_set(&cpu_buffer->entries, 0);
3500 local_set(&cpu_buffer->committing, 0);
3501 local_set(&cpu_buffer->commits, 0);
3502 cpu_buffer->read = 0;
3504 cpu_buffer->write_stamp = 0;
3505 cpu_buffer->read_stamp = 0;
3507 cpu_buffer->lost_events = 0;
3508 cpu_buffer->last_overrun = 0;
3510 rb_head_page_activate(cpu_buffer);
3514 * ring_buffer_reset_cpu - reset a ring buffer per CPU buffer
3515 * @buffer: The ring buffer to reset a per cpu buffer of
3516 * @cpu: The CPU buffer to be reset
3518 void ring_buffer_reset_cpu(struct ring_buffer *buffer, int cpu)
3520 struct ring_buffer_per_cpu *cpu_buffer = buffer->buffers[cpu];
3521 unsigned long flags;
3523 if (!cpumask_test_cpu(cpu, buffer->cpumask))
3526 atomic_inc(&cpu_buffer->record_disabled);
3528 spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
3530 if (RB_WARN_ON(cpu_buffer, local_read(&cpu_buffer->committing)))
3533 arch_spin_lock(&cpu_buffer->lock);
3535 rb_reset_cpu(cpu_buffer);
3537 arch_spin_unlock(&cpu_buffer->lock);
3540 spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
3542 atomic_dec(&cpu_buffer->record_disabled);
3544 EXPORT_SYMBOL_GPL(ring_buffer_reset_cpu);
3547 * ring_buffer_reset - reset a ring buffer
3548 * @buffer: The ring buffer to reset all cpu buffers
3550 void ring_buffer_reset(struct ring_buffer *buffer)
3554 for_each_buffer_cpu(buffer, cpu)
3555 ring_buffer_reset_cpu(buffer, cpu);
3557 EXPORT_SYMBOL_GPL(ring_buffer_reset);
3560 * rind_buffer_empty - is the ring buffer empty?
3561 * @buffer: The ring buffer to test
3563 int ring_buffer_empty(struct ring_buffer *buffer)
3565 struct ring_buffer_per_cpu *cpu_buffer;
3566 unsigned long flags;
3571 dolock = rb_ok_to_lock();
3573 /* yes this is racy, but if you don't like the race, lock the buffer */
3574 for_each_buffer_cpu(buffer, cpu) {
3575 cpu_buffer = buffer->buffers[cpu];
3576 local_irq_save(flags);
3578 spin_lock(&cpu_buffer->reader_lock);
3579 ret = rb_per_cpu_empty(cpu_buffer);
3581 spin_unlock(&cpu_buffer->reader_lock);
3582 local_irq_restore(flags);
3590 EXPORT_SYMBOL_GPL(ring_buffer_empty);
3593 * ring_buffer_empty_cpu - is a cpu buffer of a ring buffer empty?
3594 * @buffer: The ring buffer
3595 * @cpu: The CPU buffer to test
3597 int ring_buffer_empty_cpu(struct ring_buffer *buffer, int cpu)
3599 struct ring_buffer_per_cpu *cpu_buffer;
3600 unsigned long flags;
3604 if (!cpumask_test_cpu(cpu, buffer->cpumask))
3607 dolock = rb_ok_to_lock();
3609 cpu_buffer = buffer->buffers[cpu];
3610 local_irq_save(flags);
3612 spin_lock(&cpu_buffer->reader_lock);
3613 ret = rb_per_cpu_empty(cpu_buffer);
3615 spin_unlock(&cpu_buffer->reader_lock);
3616 local_irq_restore(flags);
3620 EXPORT_SYMBOL_GPL(ring_buffer_empty_cpu);
3622 #ifdef CONFIG_RING_BUFFER_ALLOW_SWAP
3624 * ring_buffer_swap_cpu - swap a CPU buffer between two ring buffers
3625 * @buffer_a: One buffer to swap with
3626 * @buffer_b: The other buffer to swap with
3628 * This function is useful for tracers that want to take a "snapshot"
3629 * of a CPU buffer and has another back up buffer lying around.
3630 * it is expected that the tracer handles the cpu buffer not being
3631 * used at the moment.
3633 int ring_buffer_swap_cpu(struct ring_buffer *buffer_a,
3634 struct ring_buffer *buffer_b, int cpu)
3636 struct ring_buffer_per_cpu *cpu_buffer_a;
3637 struct ring_buffer_per_cpu *cpu_buffer_b;
3640 if (!cpumask_test_cpu(cpu, buffer_a->cpumask) ||
3641 !cpumask_test_cpu(cpu, buffer_b->cpumask))
3644 /* At least make sure the two buffers are somewhat the same */
3645 if (buffer_a->pages != buffer_b->pages)
3650 if (ring_buffer_flags != RB_BUFFERS_ON)
3653 if (atomic_read(&buffer_a->record_disabled))
3656 if (atomic_read(&buffer_b->record_disabled))
3659 cpu_buffer_a = buffer_a->buffers[cpu];
3660 cpu_buffer_b = buffer_b->buffers[cpu];
3662 if (atomic_read(&cpu_buffer_a->record_disabled))
3665 if (atomic_read(&cpu_buffer_b->record_disabled))
3669 * We can't do a synchronize_sched here because this
3670 * function can be called in atomic context.
3671 * Normally this will be called from the same CPU as cpu.
3672 * If not it's up to the caller to protect this.
3674 atomic_inc(&cpu_buffer_a->record_disabled);
3675 atomic_inc(&cpu_buffer_b->record_disabled);
3678 if (local_read(&cpu_buffer_a->committing))
3680 if (local_read(&cpu_buffer_b->committing))
3683 buffer_a->buffers[cpu] = cpu_buffer_b;
3684 buffer_b->buffers[cpu] = cpu_buffer_a;
3686 cpu_buffer_b->buffer = buffer_a;
3687 cpu_buffer_a->buffer = buffer_b;
3692 atomic_dec(&cpu_buffer_a->record_disabled);
3693 atomic_dec(&cpu_buffer_b->record_disabled);
3697 EXPORT_SYMBOL_GPL(ring_buffer_swap_cpu);
3698 #endif /* CONFIG_RING_BUFFER_ALLOW_SWAP */
3701 * ring_buffer_alloc_read_page - allocate a page to read from buffer
3702 * @buffer: the buffer to allocate for.
3704 * This function is used in conjunction with ring_buffer_read_page.
3705 * When reading a full page from the ring buffer, these functions
3706 * can be used to speed up the process. The calling function should
3707 * allocate a few pages first with this function. Then when it
3708 * needs to get pages from the ring buffer, it passes the result
3709 * of this function into ring_buffer_read_page, which will swap
3710 * the page that was allocated, with the read page of the buffer.
3713 * The page allocated, or NULL on error.
3715 void *ring_buffer_alloc_read_page(struct ring_buffer *buffer)
3717 struct buffer_data_page *bpage;
3720 addr = __get_free_page(GFP_KERNEL);
3724 bpage = (void *)addr;
3726 rb_init_page(bpage);
3730 EXPORT_SYMBOL_GPL(ring_buffer_alloc_read_page);
3733 * ring_buffer_free_read_page - free an allocated read page
3734 * @buffer: the buffer the page was allocate for
3735 * @data: the page to free
3737 * Free a page allocated from ring_buffer_alloc_read_page.
3739 void ring_buffer_free_read_page(struct ring_buffer *buffer, void *data)
3741 free_page((unsigned long)data);
3743 EXPORT_SYMBOL_GPL(ring_buffer_free_read_page);
3746 * ring_buffer_read_page - extract a page from the ring buffer
3747 * @buffer: buffer to extract from
3748 * @data_page: the page to use allocated from ring_buffer_alloc_read_page
3749 * @len: amount to extract
3750 * @cpu: the cpu of the buffer to extract
3751 * @full: should the extraction only happen when the page is full.
3753 * This function will pull out a page from the ring buffer and consume it.
3754 * @data_page must be the address of the variable that was returned
3755 * from ring_buffer_alloc_read_page. This is because the page might be used
3756 * to swap with a page in the ring buffer.
3759 * rpage = ring_buffer_alloc_read_page(buffer);
3762 * ret = ring_buffer_read_page(buffer, &rpage, len, cpu, 0);
3764 * process_page(rpage, ret);
3766 * When @full is set, the function will not return true unless
3767 * the writer is off the reader page.
3769 * Note: it is up to the calling functions to handle sleeps and wakeups.
3770 * The ring buffer can be used anywhere in the kernel and can not
3771 * blindly call wake_up. The layer that uses the ring buffer must be
3772 * responsible for that.
3775 * >=0 if data has been transferred, returns the offset of consumed data.
3776 * <0 if no data has been transferred.
3778 int ring_buffer_read_page(struct ring_buffer *buffer,
3779 void **data_page, size_t len, int cpu, int full)
3781 struct ring_buffer_per_cpu *cpu_buffer = buffer->buffers[cpu];
3782 struct ring_buffer_event *event;
3783 struct buffer_data_page *bpage;
3784 struct buffer_page *reader;
3785 unsigned long missed_events;
3786 unsigned long flags;
3787 unsigned int commit;
3792 if (!cpumask_test_cpu(cpu, buffer->cpumask))
3796 * If len is not big enough to hold the page header, then
3797 * we can not copy anything.
3799 if (len <= BUF_PAGE_HDR_SIZE)
3802 len -= BUF_PAGE_HDR_SIZE;
3811 spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
3813 reader = rb_get_reader_page(cpu_buffer);
3817 event = rb_reader_event(cpu_buffer);
3819 read = reader->read;
3820 commit = rb_page_commit(reader);
3822 /* Check if any events were dropped */
3823 missed_events = cpu_buffer->lost_events;
3826 * If this page has been partially read or
3827 * if len is not big enough to read the rest of the page or
3828 * a writer is still on the page, then
3829 * we must copy the data from the page to the buffer.
3830 * Otherwise, we can simply swap the page with the one passed in.
3832 if (read || (len < (commit - read)) ||
3833 cpu_buffer->reader_page == cpu_buffer->commit_page) {
3834 struct buffer_data_page *rpage = cpu_buffer->reader_page->page;
3835 unsigned int rpos = read;
3836 unsigned int pos = 0;
3842 if (len > (commit - read))
3843 len = (commit - read);
3845 size = rb_event_length(event);
3850 /* save the current timestamp, since the user will need it */
3851 save_timestamp = cpu_buffer->read_stamp;
3853 /* Need to copy one event at a time */
3855 memcpy(bpage->data + pos, rpage->data + rpos, size);
3859 rb_advance_reader(cpu_buffer);
3860 rpos = reader->read;
3863 event = rb_reader_event(cpu_buffer);
3864 size = rb_event_length(event);
3865 } while (len > size);
3868 local_set(&bpage->commit, pos);
3869 bpage->time_stamp = save_timestamp;
3871 /* we copied everything to the beginning */
3874 /* update the entry counter */
3875 cpu_buffer->read += rb_page_entries(reader);
3877 /* swap the pages */
3878 rb_init_page(bpage);
3879 bpage = reader->page;
3880 reader->page = *data_page;
3881 local_set(&reader->write, 0);
3882 local_set(&reader->entries, 0);
3887 * Use the real_end for the data size,
3888 * This gives us a chance to store the lost events
3891 if (reader->real_end)
3892 local_set(&bpage->commit, reader->real_end);
3896 cpu_buffer->lost_events = 0;
3898 * Set a flag in the commit field if we lost events
3900 if (missed_events) {
3901 commit = local_read(&bpage->commit);
3903 /* If there is room at the end of the page to save the
3904 * missed events, then record it there.
3906 if (BUF_PAGE_SIZE - commit >= sizeof(missed_events)) {
3907 memcpy(&bpage->data[commit], &missed_events,
3908 sizeof(missed_events));
3909 local_add(RB_MISSED_STORED, &bpage->commit);
3911 local_add(RB_MISSED_EVENTS, &bpage->commit);
3915 spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
3920 EXPORT_SYMBOL_GPL(ring_buffer_read_page);
3922 #ifdef CONFIG_TRACING
3924 rb_simple_read(struct file *filp, char __user *ubuf,
3925 size_t cnt, loff_t *ppos)
3927 unsigned long *p = filp->private_data;
3931 if (test_bit(RB_BUFFERS_DISABLED_BIT, p))
3932 r = sprintf(buf, "permanently disabled\n");
3934 r = sprintf(buf, "%d\n", test_bit(RB_BUFFERS_ON_BIT, p));
3936 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
3940 rb_simple_write(struct file *filp, const char __user *ubuf,
3941 size_t cnt, loff_t *ppos)
3943 unsigned long *p = filp->private_data;
3948 if (cnt >= sizeof(buf))
3951 if (copy_from_user(&buf, ubuf, cnt))
3956 ret = strict_strtoul(buf, 10, &val);
3961 set_bit(RB_BUFFERS_ON_BIT, p);
3963 clear_bit(RB_BUFFERS_ON_BIT, p);
3970 static const struct file_operations rb_simple_fops = {
3971 .open = tracing_open_generic,
3972 .read = rb_simple_read,
3973 .write = rb_simple_write,
3977 static __init int rb_init_debugfs(void)
3979 struct dentry *d_tracer;
3981 d_tracer = tracing_init_dentry();
3983 trace_create_file("tracing_on", 0644, d_tracer,
3984 &ring_buffer_flags, &rb_simple_fops);
3989 fs_initcall(rb_init_debugfs);
3992 #ifdef CONFIG_HOTPLUG_CPU
3993 static int rb_cpu_notify(struct notifier_block *self,
3994 unsigned long action, void *hcpu)
3996 struct ring_buffer *buffer =
3997 container_of(self, struct ring_buffer, cpu_notify);
3998 long cpu = (long)hcpu;
4001 case CPU_UP_PREPARE:
4002 case CPU_UP_PREPARE_FROZEN:
4003 if (cpumask_test_cpu(cpu, buffer->cpumask))
4006 buffer->buffers[cpu] =
4007 rb_allocate_cpu_buffer(buffer, cpu);
4008 if (!buffer->buffers[cpu]) {
4009 WARN(1, "failed to allocate ring buffer on CPU %ld\n",
4014 cpumask_set_cpu(cpu, buffer->cpumask);
4016 case CPU_DOWN_PREPARE:
4017 case CPU_DOWN_PREPARE_FROZEN:
4020 * If we were to free the buffer, then the user would
4021 * lose any trace that was in the buffer.