4 * Copyright (C) 2008 Steven Rostedt <srostedt@redhat.com>
6 #include <linux/ring_buffer.h>
7 #include <linux/trace_clock.h>
8 #include <linux/ftrace_irq.h>
9 #include <linux/spinlock.h>
10 #include <linux/debugfs.h>
11 #include <linux/uaccess.h>
12 #include <linux/hardirq.h>
13 #include <linux/kmemcheck.h>
14 #include <linux/module.h>
15 #include <linux/percpu.h>
16 #include <linux/mutex.h>
17 #include <linux/init.h>
18 #include <linux/hash.h>
19 #include <linux/list.h>
20 #include <linux/cpu.h>
23 #include <asm/local.h>
27 * The ring buffer header is special. We must manually up keep it.
29 int ring_buffer_print_entry_header(struct trace_seq *s)
33 ret = trace_seq_printf(s, "# compressed entry header\n");
34 ret = trace_seq_printf(s, "\ttype_len : 5 bits\n");
35 ret = trace_seq_printf(s, "\ttime_delta : 27 bits\n");
36 ret = trace_seq_printf(s, "\tarray : 32 bits\n");
37 ret = trace_seq_printf(s, "\n");
38 ret = trace_seq_printf(s, "\tpadding : type == %d\n",
39 RINGBUF_TYPE_PADDING);
40 ret = trace_seq_printf(s, "\ttime_extend : type == %d\n",
41 RINGBUF_TYPE_TIME_EXTEND);
42 ret = trace_seq_printf(s, "\tdata max type_len == %d\n",
43 RINGBUF_TYPE_DATA_TYPE_LEN_MAX);
49 * The ring buffer is made up of a list of pages. A separate list of pages is
50 * allocated for each CPU. A writer may only write to a buffer that is
51 * associated with the CPU it is currently executing on. A reader may read
52 * from any per cpu buffer.
54 * The reader is special. For each per cpu buffer, the reader has its own
55 * reader page. When a reader has read the entire reader page, this reader
56 * page is swapped with another page in the ring buffer.
58 * Now, as long as the writer is off the reader page, the reader can do what
59 * ever it wants with that page. The writer will never write to that page
60 * again (as long as it is out of the ring buffer).
62 * Here's some silly ASCII art.
65 * |reader| RING BUFFER
67 * +------+ +---+ +---+ +---+
76 * |reader| RING BUFFER
77 * |page |------------------v
78 * +------+ +---+ +---+ +---+
87 * |reader| RING BUFFER
88 * |page |------------------v
89 * +------+ +---+ +---+ +---+
94 * +------------------------------+
98 * |buffer| RING BUFFER
99 * |page |------------------v
100 * +------+ +---+ +---+ +---+
102 * | New +---+ +---+ +---+
105 * +------------------------------+
108 * After we make this swap, the reader can hand this page off to the splice
109 * code and be done with it. It can even allocate a new page if it needs to
110 * and swap that into the ring buffer.
112 * We will be using cmpxchg soon to make all this lockless.
117 * A fast way to enable or disable all ring buffers is to
118 * call tracing_on or tracing_off. Turning off the ring buffers
119 * prevents all ring buffers from being recorded to.
120 * Turning this switch on, makes it OK to write to the
121 * ring buffer, if the ring buffer is enabled itself.
123 * There's three layers that must be on in order to write
124 * to the ring buffer.
126 * 1) This global flag must be set.
127 * 2) The ring buffer must be enabled for recording.
128 * 3) The per cpu buffer must be enabled for recording.
130 * In case of an anomaly, this global flag has a bit set that
131 * will permantly disable all ring buffers.
135 * Global flag to disable all recording to ring buffers
136 * This has two bits: ON, DISABLED
140 * 0 0 : ring buffers are off
141 * 1 0 : ring buffers are on
142 * X 1 : ring buffers are permanently disabled
146 RB_BUFFERS_ON_BIT = 0,
147 RB_BUFFERS_DISABLED_BIT = 1,
151 RB_BUFFERS_ON = 1 << RB_BUFFERS_ON_BIT,
152 RB_BUFFERS_DISABLED = 1 << RB_BUFFERS_DISABLED_BIT,
155 static unsigned long ring_buffer_flags __read_mostly = RB_BUFFERS_ON;
157 #define BUF_PAGE_HDR_SIZE offsetof(struct buffer_data_page, data)
160 * tracing_on - enable all tracing buffers
162 * This function enables all tracing buffers that may have been
163 * disabled with tracing_off.
165 void tracing_on(void)
167 set_bit(RB_BUFFERS_ON_BIT, &ring_buffer_flags);
169 EXPORT_SYMBOL_GPL(tracing_on);
172 * tracing_off - turn off all tracing buffers
174 * This function stops all tracing buffers from recording data.
175 * It does not disable any overhead the tracers themselves may
176 * be causing. This function simply causes all recording to
177 * the ring buffers to fail.
179 void tracing_off(void)
181 clear_bit(RB_BUFFERS_ON_BIT, &ring_buffer_flags);
183 EXPORT_SYMBOL_GPL(tracing_off);
186 * tracing_off_permanent - permanently disable ring buffers
188 * This function, once called, will disable all ring buffers
191 void tracing_off_permanent(void)
193 set_bit(RB_BUFFERS_DISABLED_BIT, &ring_buffer_flags);
197 * tracing_is_on - show state of ring buffers enabled
199 int tracing_is_on(void)
201 return ring_buffer_flags == RB_BUFFERS_ON;
203 EXPORT_SYMBOL_GPL(tracing_is_on);
205 #define RB_EVNT_HDR_SIZE (offsetof(struct ring_buffer_event, array))
206 #define RB_ALIGNMENT 4U
207 #define RB_MAX_SMALL_DATA (RB_ALIGNMENT * RINGBUF_TYPE_DATA_TYPE_LEN_MAX)
208 #define RB_EVNT_MIN_SIZE 8U /* two 32bit words */
210 #if !defined(CONFIG_64BIT) || defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS)
211 # define RB_FORCE_8BYTE_ALIGNMENT 0
212 # define RB_ARCH_ALIGNMENT RB_ALIGNMENT
214 # define RB_FORCE_8BYTE_ALIGNMENT 1
215 # define RB_ARCH_ALIGNMENT 8U
218 /* define RINGBUF_TYPE_DATA for 'case RINGBUF_TYPE_DATA:' */
219 #define RINGBUF_TYPE_DATA 0 ... RINGBUF_TYPE_DATA_TYPE_LEN_MAX
222 RB_LEN_TIME_EXTEND = 8,
223 RB_LEN_TIME_STAMP = 16,
226 static inline int rb_null_event(struct ring_buffer_event *event)
228 return event->type_len == RINGBUF_TYPE_PADDING && !event->time_delta;
231 static void rb_event_set_padding(struct ring_buffer_event *event)
233 /* padding has a NULL time_delta */
234 event->type_len = RINGBUF_TYPE_PADDING;
235 event->time_delta = 0;
239 rb_event_data_length(struct ring_buffer_event *event)
244 length = event->type_len * RB_ALIGNMENT;
246 length = event->array[0];
247 return length + RB_EVNT_HDR_SIZE;
250 /* inline for ring buffer fast paths */
252 rb_event_length(struct ring_buffer_event *event)
254 switch (event->type_len) {
255 case RINGBUF_TYPE_PADDING:
256 if (rb_null_event(event))
259 return event->array[0] + RB_EVNT_HDR_SIZE;
261 case RINGBUF_TYPE_TIME_EXTEND:
262 return RB_LEN_TIME_EXTEND;
264 case RINGBUF_TYPE_TIME_STAMP:
265 return RB_LEN_TIME_STAMP;
267 case RINGBUF_TYPE_DATA:
268 return rb_event_data_length(event);
277 * ring_buffer_event_length - return the length of the event
278 * @event: the event to get the length of
280 unsigned ring_buffer_event_length(struct ring_buffer_event *event)
282 unsigned length = rb_event_length(event);
283 if (event->type_len > RINGBUF_TYPE_DATA_TYPE_LEN_MAX)
285 length -= RB_EVNT_HDR_SIZE;
286 if (length > RB_MAX_SMALL_DATA + sizeof(event->array[0]))
287 length -= sizeof(event->array[0]);
290 EXPORT_SYMBOL_GPL(ring_buffer_event_length);
292 /* inline for ring buffer fast paths */
294 rb_event_data(struct ring_buffer_event *event)
296 BUG_ON(event->type_len > RINGBUF_TYPE_DATA_TYPE_LEN_MAX);
297 /* If length is in len field, then array[0] has the data */
299 return (void *)&event->array[0];
300 /* Otherwise length is in array[0] and array[1] has the data */
301 return (void *)&event->array[1];
305 * ring_buffer_event_data - return the data of the event
306 * @event: the event to get the data from
308 void *ring_buffer_event_data(struct ring_buffer_event *event)
310 return rb_event_data(event);
312 EXPORT_SYMBOL_GPL(ring_buffer_event_data);
314 #define for_each_buffer_cpu(buffer, cpu) \
315 for_each_cpu(cpu, buffer->cpumask)
318 #define TS_MASK ((1ULL << TS_SHIFT) - 1)
319 #define TS_DELTA_TEST (~TS_MASK)
321 /* Flag when events were overwritten */
322 #define RB_MISSED_EVENTS (1 << 31)
324 struct buffer_data_page {
325 u64 time_stamp; /* page time stamp */
326 local_t commit; /* write committed index */
327 unsigned char data[]; /* data of buffer page */
331 * Note, the buffer_page list must be first. The buffer pages
332 * are allocated in cache lines, which means that each buffer
333 * page will be at the beginning of a cache line, and thus
334 * the least significant bits will be zero. We use this to
335 * add flags in the list struct pointers, to make the ring buffer
339 struct list_head list; /* list of buffer pages */
340 local_t write; /* index for next write */
341 unsigned read; /* index for next read */
342 local_t entries; /* entries on this page */
343 struct buffer_data_page *page; /* Actual data page */
347 * The buffer page counters, write and entries, must be reset
348 * atomically when crossing page boundaries. To synchronize this
349 * update, two counters are inserted into the number. One is
350 * the actual counter for the write position or count on the page.
352 * The other is a counter of updaters. Before an update happens
353 * the update partition of the counter is incremented. This will
354 * allow the updater to update the counter atomically.
356 * The counter is 20 bits, and the state data is 12.
358 #define RB_WRITE_MASK 0xfffff
359 #define RB_WRITE_INTCNT (1 << 20)
361 static void rb_init_page(struct buffer_data_page *bpage)
363 local_set(&bpage->commit, 0);
367 * ring_buffer_page_len - the size of data on the page.
368 * @page: The page to read
370 * Returns the amount of data on the page, including buffer page header.
372 size_t ring_buffer_page_len(void *page)
374 return local_read(&((struct buffer_data_page *)page)->commit)
379 * Also stolen from mm/slob.c. Thanks to Mathieu Desnoyers for pointing
382 static void free_buffer_page(struct buffer_page *bpage)
384 free_page((unsigned long)bpage->page);
389 * We need to fit the time_stamp delta into 27 bits.
391 static inline int test_time_stamp(u64 delta)
393 if (delta & TS_DELTA_TEST)
398 #define BUF_PAGE_SIZE (PAGE_SIZE - BUF_PAGE_HDR_SIZE)
400 /* Max payload is BUF_PAGE_SIZE - header (8bytes) */
401 #define BUF_MAX_DATA_SIZE (BUF_PAGE_SIZE - (sizeof(u32) * 2))
403 /* Max number of timestamps that can fit on a page */
404 #define RB_TIMESTAMPS_PER_PAGE (BUF_PAGE_SIZE / RB_LEN_TIME_STAMP)
406 int ring_buffer_print_page_header(struct trace_seq *s)
408 struct buffer_data_page field;
411 ret = trace_seq_printf(s, "\tfield: u64 timestamp;\t"
412 "offset:0;\tsize:%u;\tsigned:%u;\n",
413 (unsigned int)sizeof(field.time_stamp),
414 (unsigned int)is_signed_type(u64));
416 ret = trace_seq_printf(s, "\tfield: local_t commit;\t"
417 "offset:%u;\tsize:%u;\tsigned:%u;\n",
418 (unsigned int)offsetof(typeof(field), commit),
419 (unsigned int)sizeof(field.commit),
420 (unsigned int)is_signed_type(long));
422 ret = trace_seq_printf(s, "\tfield: int overwrite;\t"
423 "offset:%u;\tsize:%u;\tsigned:%u;\n",
424 (unsigned int)offsetof(typeof(field), commit),
426 (unsigned int)is_signed_type(long));
428 ret = trace_seq_printf(s, "\tfield: char data;\t"
429 "offset:%u;\tsize:%u;\tsigned:%u;\n",
430 (unsigned int)offsetof(typeof(field), data),
431 (unsigned int)BUF_PAGE_SIZE,
432 (unsigned int)is_signed_type(char));
438 * head_page == tail_page && head == tail then buffer is empty.
440 struct ring_buffer_per_cpu {
442 struct ring_buffer *buffer;
443 spinlock_t reader_lock; /* serialize readers */
444 arch_spinlock_t lock;
445 struct lock_class_key lock_key;
446 struct list_head *pages;
447 struct buffer_page *head_page; /* read from head */
448 struct buffer_page *tail_page; /* write to tail */
449 struct buffer_page *commit_page; /* committed pages */
450 struct buffer_page *reader_page;
451 unsigned long lost_events;
452 unsigned long last_overrun;
453 local_t commit_overrun;
461 atomic_t record_disabled;
468 atomic_t record_disabled;
469 cpumask_var_t cpumask;
471 struct lock_class_key *reader_lock_key;
475 struct ring_buffer_per_cpu **buffers;
477 #ifdef CONFIG_HOTPLUG_CPU
478 struct notifier_block cpu_notify;
483 struct ring_buffer_iter {
484 struct ring_buffer_per_cpu *cpu_buffer;
486 struct buffer_page *head_page;
487 struct buffer_page *cache_reader_page;
488 unsigned long cache_read;
492 /* buffer may be either ring_buffer or ring_buffer_per_cpu */
493 #define RB_WARN_ON(b, cond) \
495 int _____ret = unlikely(cond); \
497 if (__same_type(*(b), struct ring_buffer_per_cpu)) { \
498 struct ring_buffer_per_cpu *__b = \
500 atomic_inc(&__b->buffer->record_disabled); \
502 atomic_inc(&b->record_disabled); \
508 /* Up this if you want to test the TIME_EXTENTS and normalization */
509 #define DEBUG_SHIFT 0
511 static inline u64 rb_time_stamp(struct ring_buffer *buffer)
513 /* shift to debug/test normalization and TIME_EXTENTS */
514 return buffer->clock() << DEBUG_SHIFT;
517 u64 ring_buffer_time_stamp(struct ring_buffer *buffer, int cpu)
521 preempt_disable_notrace();
522 time = rb_time_stamp(buffer);
523 preempt_enable_no_resched_notrace();
527 EXPORT_SYMBOL_GPL(ring_buffer_time_stamp);
529 void ring_buffer_normalize_time_stamp(struct ring_buffer *buffer,
532 /* Just stupid testing the normalize function and deltas */
535 EXPORT_SYMBOL_GPL(ring_buffer_normalize_time_stamp);
538 * Making the ring buffer lockless makes things tricky.
539 * Although writes only happen on the CPU that they are on,
540 * and they only need to worry about interrupts. Reads can
543 * The reader page is always off the ring buffer, but when the
544 * reader finishes with a page, it needs to swap its page with
545 * a new one from the buffer. The reader needs to take from
546 * the head (writes go to the tail). But if a writer is in overwrite
547 * mode and wraps, it must push the head page forward.
549 * Here lies the problem.
551 * The reader must be careful to replace only the head page, and
552 * not another one. As described at the top of the file in the
553 * ASCII art, the reader sets its old page to point to the next
554 * page after head. It then sets the page after head to point to
555 * the old reader page. But if the writer moves the head page
556 * during this operation, the reader could end up with the tail.
558 * We use cmpxchg to help prevent this race. We also do something
559 * special with the page before head. We set the LSB to 1.
561 * When the writer must push the page forward, it will clear the
562 * bit that points to the head page, move the head, and then set
563 * the bit that points to the new head page.
565 * We also don't want an interrupt coming in and moving the head
566 * page on another writer. Thus we use the second LSB to catch
569 * head->list->prev->next bit 1 bit 0
572 * Points to head page 0 1
575 * Note we can not trust the prev pointer of the head page, because:
577 * +----+ +-----+ +-----+
578 * | |------>| T |---X--->| N |
580 * +----+ +-----+ +-----+
583 * +----------| R |----------+ |
587 * Key: ---X--> HEAD flag set in pointer
592 * (see __rb_reserve_next() to see where this happens)
594 * What the above shows is that the reader just swapped out
595 * the reader page with a page in the buffer, but before it
596 * could make the new header point back to the new page added
597 * it was preempted by a writer. The writer moved forward onto
598 * the new page added by the reader and is about to move forward
601 * You can see, it is legitimate for the previous pointer of
602 * the head (or any page) not to point back to itself. But only
606 #define RB_PAGE_NORMAL 0UL
607 #define RB_PAGE_HEAD 1UL
608 #define RB_PAGE_UPDATE 2UL
611 #define RB_FLAG_MASK 3UL
613 /* PAGE_MOVED is not part of the mask */
614 #define RB_PAGE_MOVED 4UL
617 * rb_list_head - remove any bit
619 static struct list_head *rb_list_head(struct list_head *list)
621 unsigned long val = (unsigned long)list;
623 return (struct list_head *)(val & ~RB_FLAG_MASK);
627 * rb_is_head_page - test if the given page is the head page
629 * Because the reader may move the head_page pointer, we can
630 * not trust what the head page is (it may be pointing to
631 * the reader page). But if the next page is a header page,
632 * its flags will be non zero.
635 rb_is_head_page(struct ring_buffer_per_cpu *cpu_buffer,
636 struct buffer_page *page, struct list_head *list)
640 val = (unsigned long)list->next;
642 if ((val & ~RB_FLAG_MASK) != (unsigned long)&page->list)
643 return RB_PAGE_MOVED;
645 return val & RB_FLAG_MASK;
651 * The unique thing about the reader page, is that, if the
652 * writer is ever on it, the previous pointer never points
653 * back to the reader page.
655 static int rb_is_reader_page(struct buffer_page *page)
657 struct list_head *list = page->list.prev;
659 return rb_list_head(list->next) != &page->list;
663 * rb_set_list_to_head - set a list_head to be pointing to head.
665 static void rb_set_list_to_head(struct ring_buffer_per_cpu *cpu_buffer,
666 struct list_head *list)
670 ptr = (unsigned long *)&list->next;
671 *ptr |= RB_PAGE_HEAD;
672 *ptr &= ~RB_PAGE_UPDATE;
676 * rb_head_page_activate - sets up head page
678 static void rb_head_page_activate(struct ring_buffer_per_cpu *cpu_buffer)
680 struct buffer_page *head;
682 head = cpu_buffer->head_page;
687 * Set the previous list pointer to have the HEAD flag.
689 rb_set_list_to_head(cpu_buffer, head->list.prev);
692 static void rb_list_head_clear(struct list_head *list)
694 unsigned long *ptr = (unsigned long *)&list->next;
696 *ptr &= ~RB_FLAG_MASK;
700 * rb_head_page_dactivate - clears head page ptr (for free list)
703 rb_head_page_deactivate(struct ring_buffer_per_cpu *cpu_buffer)
705 struct list_head *hd;
707 /* Go through the whole list and clear any pointers found. */
708 rb_list_head_clear(cpu_buffer->pages);
710 list_for_each(hd, cpu_buffer->pages)
711 rb_list_head_clear(hd);
714 static int rb_head_page_set(struct ring_buffer_per_cpu *cpu_buffer,
715 struct buffer_page *head,
716 struct buffer_page *prev,
717 int old_flag, int new_flag)
719 struct list_head *list;
720 unsigned long val = (unsigned long)&head->list;
725 val &= ~RB_FLAG_MASK;
727 ret = cmpxchg((unsigned long *)&list->next,
728 val | old_flag, val | new_flag);
730 /* check if the reader took the page */
731 if ((ret & ~RB_FLAG_MASK) != val)
732 return RB_PAGE_MOVED;
734 return ret & RB_FLAG_MASK;
737 static int rb_head_page_set_update(struct ring_buffer_per_cpu *cpu_buffer,
738 struct buffer_page *head,
739 struct buffer_page *prev,
742 return rb_head_page_set(cpu_buffer, head, prev,
743 old_flag, RB_PAGE_UPDATE);
746 static int rb_head_page_set_head(struct ring_buffer_per_cpu *cpu_buffer,
747 struct buffer_page *head,
748 struct buffer_page *prev,
751 return rb_head_page_set(cpu_buffer, head, prev,
752 old_flag, RB_PAGE_HEAD);
755 static int rb_head_page_set_normal(struct ring_buffer_per_cpu *cpu_buffer,
756 struct buffer_page *head,
757 struct buffer_page *prev,
760 return rb_head_page_set(cpu_buffer, head, prev,
761 old_flag, RB_PAGE_NORMAL);
764 static inline void rb_inc_page(struct ring_buffer_per_cpu *cpu_buffer,
765 struct buffer_page **bpage)
767 struct list_head *p = rb_list_head((*bpage)->list.next);
769 *bpage = list_entry(p, struct buffer_page, list);
772 static struct buffer_page *
773 rb_set_head_page(struct ring_buffer_per_cpu *cpu_buffer)
775 struct buffer_page *head;
776 struct buffer_page *page;
777 struct list_head *list;
780 if (RB_WARN_ON(cpu_buffer, !cpu_buffer->head_page))
784 list = cpu_buffer->pages;
785 if (RB_WARN_ON(cpu_buffer, rb_list_head(list->prev->next) != list))
788 page = head = cpu_buffer->head_page;
790 * It is possible that the writer moves the header behind
791 * where we started, and we miss in one loop.
792 * A second loop should grab the header, but we'll do
793 * three loops just because I'm paranoid.
795 for (i = 0; i < 3; i++) {
797 if (rb_is_head_page(cpu_buffer, page, page->list.prev)) {
798 cpu_buffer->head_page = page;
801 rb_inc_page(cpu_buffer, &page);
802 } while (page != head);
805 RB_WARN_ON(cpu_buffer, 1);
810 static int rb_head_page_replace(struct buffer_page *old,
811 struct buffer_page *new)
813 unsigned long *ptr = (unsigned long *)&old->list.prev->next;
817 val = *ptr & ~RB_FLAG_MASK;
820 ret = cmpxchg(ptr, val, (unsigned long)&new->list);
826 * rb_tail_page_update - move the tail page forward
828 * Returns 1 if moved tail page, 0 if someone else did.
830 static int rb_tail_page_update(struct ring_buffer_per_cpu *cpu_buffer,
831 struct buffer_page *tail_page,
832 struct buffer_page *next_page)
834 struct buffer_page *old_tail;
835 unsigned long old_entries;
836 unsigned long old_write;
840 * The tail page now needs to be moved forward.
842 * We need to reset the tail page, but without messing
843 * with possible erasing of data brought in by interrupts
844 * that have moved the tail page and are currently on it.
846 * We add a counter to the write field to denote this.
848 old_write = local_add_return(RB_WRITE_INTCNT, &next_page->write);
849 old_entries = local_add_return(RB_WRITE_INTCNT, &next_page->entries);
852 * Just make sure we have seen our old_write and synchronize
853 * with any interrupts that come in.
858 * If the tail page is still the same as what we think
859 * it is, then it is up to us to update the tail
862 if (tail_page == cpu_buffer->tail_page) {
863 /* Zero the write counter */
864 unsigned long val = old_write & ~RB_WRITE_MASK;
865 unsigned long eval = old_entries & ~RB_WRITE_MASK;
868 * This will only succeed if an interrupt did
869 * not come in and change it. In which case, we
870 * do not want to modify it.
872 * We add (void) to let the compiler know that we do not care
873 * about the return value of these functions. We use the
874 * cmpxchg to only update if an interrupt did not already
875 * do it for us. If the cmpxchg fails, we don't care.
877 (void)local_cmpxchg(&next_page->write, old_write, val);
878 (void)local_cmpxchg(&next_page->entries, old_entries, eval);
881 * No need to worry about races with clearing out the commit.
882 * it only can increment when a commit takes place. But that
883 * only happens in the outer most nested commit.
885 local_set(&next_page->page->commit, 0);
887 old_tail = cmpxchg(&cpu_buffer->tail_page,
888 tail_page, next_page);
890 if (old_tail == tail_page)
897 static int rb_check_bpage(struct ring_buffer_per_cpu *cpu_buffer,
898 struct buffer_page *bpage)
900 unsigned long val = (unsigned long)bpage;
902 if (RB_WARN_ON(cpu_buffer, val & RB_FLAG_MASK))
909 * rb_check_list - make sure a pointer to a list has the last bits zero
911 static int rb_check_list(struct ring_buffer_per_cpu *cpu_buffer,
912 struct list_head *list)
914 if (RB_WARN_ON(cpu_buffer, rb_list_head(list->prev) != list->prev))
916 if (RB_WARN_ON(cpu_buffer, rb_list_head(list->next) != list->next))
922 * check_pages - integrity check of buffer pages
923 * @cpu_buffer: CPU buffer with pages to test
925 * As a safety measure we check to make sure the data pages have not
928 static int rb_check_pages(struct ring_buffer_per_cpu *cpu_buffer)
930 struct list_head *head = cpu_buffer->pages;
931 struct buffer_page *bpage, *tmp;
933 rb_head_page_deactivate(cpu_buffer);
935 if (RB_WARN_ON(cpu_buffer, head->next->prev != head))
937 if (RB_WARN_ON(cpu_buffer, head->prev->next != head))
940 if (rb_check_list(cpu_buffer, head))
943 list_for_each_entry_safe(bpage, tmp, head, list) {
944 if (RB_WARN_ON(cpu_buffer,
945 bpage->list.next->prev != &bpage->list))
947 if (RB_WARN_ON(cpu_buffer,
948 bpage->list.prev->next != &bpage->list))
950 if (rb_check_list(cpu_buffer, &bpage->list))
954 rb_head_page_activate(cpu_buffer);
959 static int rb_allocate_pages(struct ring_buffer_per_cpu *cpu_buffer,
962 struct buffer_page *bpage, *tmp;
969 for (i = 0; i < nr_pages; i++) {
970 bpage = kzalloc_node(ALIGN(sizeof(*bpage), cache_line_size()),
971 GFP_KERNEL, cpu_to_node(cpu_buffer->cpu));
975 rb_check_bpage(cpu_buffer, bpage);
977 list_add(&bpage->list, &pages);
979 addr = __get_free_page(GFP_KERNEL);
982 bpage->page = (void *)addr;
983 rb_init_page(bpage->page);
987 * The ring buffer page list is a circular list that does not
988 * start and end with a list head. All page list items point to
991 cpu_buffer->pages = pages.next;
994 rb_check_pages(cpu_buffer);
999 list_for_each_entry_safe(bpage, tmp, &pages, list) {
1000 list_del_init(&bpage->list);
1001 free_buffer_page(bpage);
1006 static struct ring_buffer_per_cpu *
1007 rb_allocate_cpu_buffer(struct ring_buffer *buffer, int cpu)
1009 struct ring_buffer_per_cpu *cpu_buffer;
1010 struct buffer_page *bpage;
1014 cpu_buffer = kzalloc_node(ALIGN(sizeof(*cpu_buffer), cache_line_size()),
1015 GFP_KERNEL, cpu_to_node(cpu));
1019 cpu_buffer->cpu = cpu;
1020 cpu_buffer->buffer = buffer;
1021 spin_lock_init(&cpu_buffer->reader_lock);
1022 lockdep_set_class(&cpu_buffer->reader_lock, buffer->reader_lock_key);
1023 cpu_buffer->lock = (arch_spinlock_t)__ARCH_SPIN_LOCK_UNLOCKED;
1025 bpage = kzalloc_node(ALIGN(sizeof(*bpage), cache_line_size()),
1026 GFP_KERNEL, cpu_to_node(cpu));
1028 goto fail_free_buffer;
1030 rb_check_bpage(cpu_buffer, bpage);
1032 cpu_buffer->reader_page = bpage;
1033 addr = __get_free_page(GFP_KERNEL);
1035 goto fail_free_reader;
1036 bpage->page = (void *)addr;
1037 rb_init_page(bpage->page);
1039 INIT_LIST_HEAD(&cpu_buffer->reader_page->list);
1041 ret = rb_allocate_pages(cpu_buffer, buffer->pages);
1043 goto fail_free_reader;
1045 cpu_buffer->head_page
1046 = list_entry(cpu_buffer->pages, struct buffer_page, list);
1047 cpu_buffer->tail_page = cpu_buffer->commit_page = cpu_buffer->head_page;
1049 rb_head_page_activate(cpu_buffer);
1054 free_buffer_page(cpu_buffer->reader_page);
1061 static void rb_free_cpu_buffer(struct ring_buffer_per_cpu *cpu_buffer)
1063 struct list_head *head = cpu_buffer->pages;
1064 struct buffer_page *bpage, *tmp;
1066 free_buffer_page(cpu_buffer->reader_page);
1068 rb_head_page_deactivate(cpu_buffer);
1071 list_for_each_entry_safe(bpage, tmp, head, list) {
1072 list_del_init(&bpage->list);
1073 free_buffer_page(bpage);
1075 bpage = list_entry(head, struct buffer_page, list);
1076 free_buffer_page(bpage);
1082 #ifdef CONFIG_HOTPLUG_CPU
1083 static int rb_cpu_notify(struct notifier_block *self,
1084 unsigned long action, void *hcpu);
1088 * ring_buffer_alloc - allocate a new ring_buffer
1089 * @size: the size in bytes per cpu that is needed.
1090 * @flags: attributes to set for the ring buffer.
1092 * Currently the only flag that is available is the RB_FL_OVERWRITE
1093 * flag. This flag means that the buffer will overwrite old data
1094 * when the buffer wraps. If this flag is not set, the buffer will
1095 * drop data when the tail hits the head.
1097 struct ring_buffer *__ring_buffer_alloc(unsigned long size, unsigned flags,
1098 struct lock_class_key *key)
1100 struct ring_buffer *buffer;
1104 /* keep it in its own cache line */
1105 buffer = kzalloc(ALIGN(sizeof(*buffer), cache_line_size()),
1110 if (!alloc_cpumask_var(&buffer->cpumask, GFP_KERNEL))
1111 goto fail_free_buffer;
1113 buffer->pages = DIV_ROUND_UP(size, BUF_PAGE_SIZE);
1114 buffer->flags = flags;
1115 buffer->clock = trace_clock_local;
1116 buffer->reader_lock_key = key;
1118 /* need at least two pages */
1119 if (buffer->pages < 2)
1123 * In case of non-hotplug cpu, if the ring-buffer is allocated
1124 * in early initcall, it will not be notified of secondary cpus.
1125 * In that off case, we need to allocate for all possible cpus.
1127 #ifdef CONFIG_HOTPLUG_CPU
1129 cpumask_copy(buffer->cpumask, cpu_online_mask);
1131 cpumask_copy(buffer->cpumask, cpu_possible_mask);
1133 buffer->cpus = nr_cpu_ids;
1135 bsize = sizeof(void *) * nr_cpu_ids;
1136 buffer->buffers = kzalloc(ALIGN(bsize, cache_line_size()),
1138 if (!buffer->buffers)
1139 goto fail_free_cpumask;
1141 for_each_buffer_cpu(buffer, cpu) {
1142 buffer->buffers[cpu] =
1143 rb_allocate_cpu_buffer(buffer, cpu);
1144 if (!buffer->buffers[cpu])
1145 goto fail_free_buffers;
1148 #ifdef CONFIG_HOTPLUG_CPU
1149 buffer->cpu_notify.notifier_call = rb_cpu_notify;
1150 buffer->cpu_notify.priority = 0;
1151 register_cpu_notifier(&buffer->cpu_notify);
1155 mutex_init(&buffer->mutex);
1160 for_each_buffer_cpu(buffer, cpu) {
1161 if (buffer->buffers[cpu])
1162 rb_free_cpu_buffer(buffer->buffers[cpu]);
1164 kfree(buffer->buffers);
1167 free_cpumask_var(buffer->cpumask);
1174 EXPORT_SYMBOL_GPL(__ring_buffer_alloc);
1177 * ring_buffer_free - free a ring buffer.
1178 * @buffer: the buffer to free.
1181 ring_buffer_free(struct ring_buffer *buffer)
1187 #ifdef CONFIG_HOTPLUG_CPU
1188 unregister_cpu_notifier(&buffer->cpu_notify);
1191 for_each_buffer_cpu(buffer, cpu)
1192 rb_free_cpu_buffer(buffer->buffers[cpu]);
1196 kfree(buffer->buffers);
1197 free_cpumask_var(buffer->cpumask);
1201 EXPORT_SYMBOL_GPL(ring_buffer_free);
1203 void ring_buffer_set_clock(struct ring_buffer *buffer,
1206 buffer->clock = clock;
1209 static void rb_reset_cpu(struct ring_buffer_per_cpu *cpu_buffer);
1212 rb_remove_pages(struct ring_buffer_per_cpu *cpu_buffer, unsigned nr_pages)
1214 struct buffer_page *bpage;
1215 struct list_head *p;
1218 spin_lock_irq(&cpu_buffer->reader_lock);
1219 rb_head_page_deactivate(cpu_buffer);
1221 for (i = 0; i < nr_pages; i++) {
1222 if (RB_WARN_ON(cpu_buffer, list_empty(cpu_buffer->pages)))
1224 p = cpu_buffer->pages->next;
1225 bpage = list_entry(p, struct buffer_page, list);
1226 list_del_init(&bpage->list);
1227 free_buffer_page(bpage);
1229 if (RB_WARN_ON(cpu_buffer, list_empty(cpu_buffer->pages)))
1232 rb_reset_cpu(cpu_buffer);
1233 rb_check_pages(cpu_buffer);
1235 spin_unlock_irq(&cpu_buffer->reader_lock);
1239 rb_insert_pages(struct ring_buffer_per_cpu *cpu_buffer,
1240 struct list_head *pages, unsigned nr_pages)
1242 struct buffer_page *bpage;
1243 struct list_head *p;
1246 spin_lock_irq(&cpu_buffer->reader_lock);
1247 rb_head_page_deactivate(cpu_buffer);
1249 for (i = 0; i < nr_pages; i++) {
1250 if (RB_WARN_ON(cpu_buffer, list_empty(pages)))
1253 bpage = list_entry(p, struct buffer_page, list);
1254 list_del_init(&bpage->list);
1255 list_add_tail(&bpage->list, cpu_buffer->pages);
1257 rb_reset_cpu(cpu_buffer);
1258 rb_check_pages(cpu_buffer);
1260 spin_unlock_irq(&cpu_buffer->reader_lock);
1264 * ring_buffer_resize - resize the ring buffer
1265 * @buffer: the buffer to resize.
1266 * @size: the new size.
1268 * Minimum size is 2 * BUF_PAGE_SIZE.
1270 * Returns -1 on failure.
1272 int ring_buffer_resize(struct ring_buffer *buffer, unsigned long size)
1274 struct ring_buffer_per_cpu *cpu_buffer;
1275 unsigned nr_pages, rm_pages, new_pages;
1276 struct buffer_page *bpage, *tmp;
1277 unsigned long buffer_size;
1283 * Always succeed at resizing a non-existent buffer:
1288 size = DIV_ROUND_UP(size, BUF_PAGE_SIZE);
1289 size *= BUF_PAGE_SIZE;
1290 buffer_size = buffer->pages * BUF_PAGE_SIZE;
1292 /* we need a minimum of two pages */
1293 if (size < BUF_PAGE_SIZE * 2)
1294 size = BUF_PAGE_SIZE * 2;
1296 if (size == buffer_size)
1299 atomic_inc(&buffer->record_disabled);
1301 /* Make sure all writers are done with this buffer. */
1302 synchronize_sched();
1304 mutex_lock(&buffer->mutex);
1307 nr_pages = DIV_ROUND_UP(size, BUF_PAGE_SIZE);
1309 if (size < buffer_size) {
1311 /* easy case, just free pages */
1312 if (RB_WARN_ON(buffer, nr_pages >= buffer->pages))
1315 rm_pages = buffer->pages - nr_pages;
1317 for_each_buffer_cpu(buffer, cpu) {
1318 cpu_buffer = buffer->buffers[cpu];
1319 rb_remove_pages(cpu_buffer, rm_pages);
1325 * This is a bit more difficult. We only want to add pages
1326 * when we can allocate enough for all CPUs. We do this
1327 * by allocating all the pages and storing them on a local
1328 * link list. If we succeed in our allocation, then we
1329 * add these pages to the cpu_buffers. Otherwise we just free
1330 * them all and return -ENOMEM;
1332 if (RB_WARN_ON(buffer, nr_pages <= buffer->pages))
1335 new_pages = nr_pages - buffer->pages;
1337 for_each_buffer_cpu(buffer, cpu) {
1338 for (i = 0; i < new_pages; i++) {
1339 bpage = kzalloc_node(ALIGN(sizeof(*bpage),
1341 GFP_KERNEL, cpu_to_node(cpu));
1344 list_add(&bpage->list, &pages);
1345 addr = __get_free_page(GFP_KERNEL);
1348 bpage->page = (void *)addr;
1349 rb_init_page(bpage->page);
1353 for_each_buffer_cpu(buffer, cpu) {
1354 cpu_buffer = buffer->buffers[cpu];
1355 rb_insert_pages(cpu_buffer, &pages, new_pages);
1358 if (RB_WARN_ON(buffer, !list_empty(&pages)))
1362 buffer->pages = nr_pages;
1364 mutex_unlock(&buffer->mutex);
1366 atomic_dec(&buffer->record_disabled);
1371 list_for_each_entry_safe(bpage, tmp, &pages, list) {
1372 list_del_init(&bpage->list);
1373 free_buffer_page(bpage);
1376 mutex_unlock(&buffer->mutex);
1377 atomic_dec(&buffer->record_disabled);
1381 * Something went totally wrong, and we are too paranoid
1382 * to even clean up the mess.
1386 mutex_unlock(&buffer->mutex);
1387 atomic_dec(&buffer->record_disabled);
1390 EXPORT_SYMBOL_GPL(ring_buffer_resize);
1392 static inline void *
1393 __rb_data_page_index(struct buffer_data_page *bpage, unsigned index)
1395 return bpage->data + index;
1398 static inline void *__rb_page_index(struct buffer_page *bpage, unsigned index)
1400 return bpage->page->data + index;
1403 static inline struct ring_buffer_event *
1404 rb_reader_event(struct ring_buffer_per_cpu *cpu_buffer)
1406 return __rb_page_index(cpu_buffer->reader_page,
1407 cpu_buffer->reader_page->read);
1410 static inline struct ring_buffer_event *
1411 rb_iter_head_event(struct ring_buffer_iter *iter)
1413 return __rb_page_index(iter->head_page, iter->head);
1416 static inline unsigned long rb_page_write(struct buffer_page *bpage)
1418 return local_read(&bpage->write) & RB_WRITE_MASK;
1421 static inline unsigned rb_page_commit(struct buffer_page *bpage)
1423 return local_read(&bpage->page->commit);
1426 static inline unsigned long rb_page_entries(struct buffer_page *bpage)
1428 return local_read(&bpage->entries) & RB_WRITE_MASK;
1431 /* Size is determined by what has been commited */
1432 static inline unsigned rb_page_size(struct buffer_page *bpage)
1434 return rb_page_commit(bpage);
1437 static inline unsigned
1438 rb_commit_index(struct ring_buffer_per_cpu *cpu_buffer)
1440 return rb_page_commit(cpu_buffer->commit_page);
1443 static inline unsigned
1444 rb_event_index(struct ring_buffer_event *event)
1446 unsigned long addr = (unsigned long)event;
1448 return (addr & ~PAGE_MASK) - BUF_PAGE_HDR_SIZE;
1452 rb_event_is_commit(struct ring_buffer_per_cpu *cpu_buffer,
1453 struct ring_buffer_event *event)
1455 unsigned long addr = (unsigned long)event;
1456 unsigned long index;
1458 index = rb_event_index(event);
1461 return cpu_buffer->commit_page->page == (void *)addr &&
1462 rb_commit_index(cpu_buffer) == index;
1466 rb_set_commit_to_write(struct ring_buffer_per_cpu *cpu_buffer)
1468 unsigned long max_count;
1471 * We only race with interrupts and NMIs on this CPU.
1472 * If we own the commit event, then we can commit
1473 * all others that interrupted us, since the interruptions
1474 * are in stack format (they finish before they come
1475 * back to us). This allows us to do a simple loop to
1476 * assign the commit to the tail.
1479 max_count = cpu_buffer->buffer->pages * 100;
1481 while (cpu_buffer->commit_page != cpu_buffer->tail_page) {
1482 if (RB_WARN_ON(cpu_buffer, !(--max_count)))
1484 if (RB_WARN_ON(cpu_buffer,
1485 rb_is_reader_page(cpu_buffer->tail_page)))
1487 local_set(&cpu_buffer->commit_page->page->commit,
1488 rb_page_write(cpu_buffer->commit_page));
1489 rb_inc_page(cpu_buffer, &cpu_buffer->commit_page);
1490 cpu_buffer->write_stamp =
1491 cpu_buffer->commit_page->page->time_stamp;
1492 /* add barrier to keep gcc from optimizing too much */
1495 while (rb_commit_index(cpu_buffer) !=
1496 rb_page_write(cpu_buffer->commit_page)) {
1498 local_set(&cpu_buffer->commit_page->page->commit,
1499 rb_page_write(cpu_buffer->commit_page));
1500 RB_WARN_ON(cpu_buffer,
1501 local_read(&cpu_buffer->commit_page->page->commit) &
1506 /* again, keep gcc from optimizing */
1510 * If an interrupt came in just after the first while loop
1511 * and pushed the tail page forward, we will be left with
1512 * a dangling commit that will never go forward.
1514 if (unlikely(cpu_buffer->commit_page != cpu_buffer->tail_page))
1518 static void rb_reset_reader_page(struct ring_buffer_per_cpu *cpu_buffer)
1520 cpu_buffer->read_stamp = cpu_buffer->reader_page->page->time_stamp;
1521 cpu_buffer->reader_page->read = 0;
1524 static void rb_inc_iter(struct ring_buffer_iter *iter)
1526 struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
1529 * The iterator could be on the reader page (it starts there).
1530 * But the head could have moved, since the reader was
1531 * found. Check for this case and assign the iterator
1532 * to the head page instead of next.
1534 if (iter->head_page == cpu_buffer->reader_page)
1535 iter->head_page = rb_set_head_page(cpu_buffer);
1537 rb_inc_page(cpu_buffer, &iter->head_page);
1539 iter->read_stamp = iter->head_page->page->time_stamp;
1544 * ring_buffer_update_event - update event type and data
1545 * @event: the even to update
1546 * @type: the type of event
1547 * @length: the size of the event field in the ring buffer
1549 * Update the type and data fields of the event. The length
1550 * is the actual size that is written to the ring buffer,
1551 * and with this, we can determine what to place into the
1555 rb_update_event(struct ring_buffer_event *event,
1556 unsigned type, unsigned length)
1558 event->type_len = type;
1562 case RINGBUF_TYPE_PADDING:
1563 case RINGBUF_TYPE_TIME_EXTEND:
1564 case RINGBUF_TYPE_TIME_STAMP:
1568 length -= RB_EVNT_HDR_SIZE;
1569 if (length > RB_MAX_SMALL_DATA || RB_FORCE_8BYTE_ALIGNMENT)
1570 event->array[0] = length;
1572 event->type_len = DIV_ROUND_UP(length, RB_ALIGNMENT);
1580 * rb_handle_head_page - writer hit the head page
1582 * Returns: +1 to retry page
1587 rb_handle_head_page(struct ring_buffer_per_cpu *cpu_buffer,
1588 struct buffer_page *tail_page,
1589 struct buffer_page *next_page)
1591 struct buffer_page *new_head;
1596 entries = rb_page_entries(next_page);
1599 * The hard part is here. We need to move the head
1600 * forward, and protect against both readers on
1601 * other CPUs and writers coming in via interrupts.
1603 type = rb_head_page_set_update(cpu_buffer, next_page, tail_page,
1607 * type can be one of four:
1608 * NORMAL - an interrupt already moved it for us
1609 * HEAD - we are the first to get here.
1610 * UPDATE - we are the interrupt interrupting
1612 * MOVED - a reader on another CPU moved the next
1613 * pointer to its reader page. Give up
1620 * We changed the head to UPDATE, thus
1621 * it is our responsibility to update
1624 local_add(entries, &cpu_buffer->overrun);
1627 * The entries will be zeroed out when we move the
1631 /* still more to do */
1634 case RB_PAGE_UPDATE:
1636 * This is an interrupt that interrupt the
1637 * previous update. Still more to do.
1640 case RB_PAGE_NORMAL:
1642 * An interrupt came in before the update
1643 * and processed this for us.
1644 * Nothing left to do.
1649 * The reader is on another CPU and just did
1650 * a swap with our next_page.
1655 RB_WARN_ON(cpu_buffer, 1); /* WTF??? */
1660 * Now that we are here, the old head pointer is
1661 * set to UPDATE. This will keep the reader from
1662 * swapping the head page with the reader page.
1663 * The reader (on another CPU) will spin till
1666 * We just need to protect against interrupts
1667 * doing the job. We will set the next pointer
1668 * to HEAD. After that, we set the old pointer
1669 * to NORMAL, but only if it was HEAD before.
1670 * otherwise we are an interrupt, and only
1671 * want the outer most commit to reset it.
1673 new_head = next_page;
1674 rb_inc_page(cpu_buffer, &new_head);
1676 ret = rb_head_page_set_head(cpu_buffer, new_head, next_page,
1680 * Valid returns are:
1681 * HEAD - an interrupt came in and already set it.
1682 * NORMAL - One of two things:
1683 * 1) We really set it.
1684 * 2) A bunch of interrupts came in and moved
1685 * the page forward again.
1689 case RB_PAGE_NORMAL:
1693 RB_WARN_ON(cpu_buffer, 1);
1698 * It is possible that an interrupt came in,
1699 * set the head up, then more interrupts came in
1700 * and moved it again. When we get back here,
1701 * the page would have been set to NORMAL but we
1702 * just set it back to HEAD.
1704 * How do you detect this? Well, if that happened
1705 * the tail page would have moved.
1707 if (ret == RB_PAGE_NORMAL) {
1709 * If the tail had moved passed next, then we need
1710 * to reset the pointer.
1712 if (cpu_buffer->tail_page != tail_page &&
1713 cpu_buffer->tail_page != next_page)
1714 rb_head_page_set_normal(cpu_buffer, new_head,
1720 * If this was the outer most commit (the one that
1721 * changed the original pointer from HEAD to UPDATE),
1722 * then it is up to us to reset it to NORMAL.
1724 if (type == RB_PAGE_HEAD) {
1725 ret = rb_head_page_set_normal(cpu_buffer, next_page,
1728 if (RB_WARN_ON(cpu_buffer,
1729 ret != RB_PAGE_UPDATE))
1736 static unsigned rb_calculate_event_length(unsigned length)
1738 struct ring_buffer_event event; /* Used only for sizeof array */
1740 /* zero length can cause confusions */
1744 if (length > RB_MAX_SMALL_DATA || RB_FORCE_8BYTE_ALIGNMENT)
1745 length += sizeof(event.array[0]);
1747 length += RB_EVNT_HDR_SIZE;
1748 length = ALIGN(length, RB_ARCH_ALIGNMENT);
1754 rb_reset_tail(struct ring_buffer_per_cpu *cpu_buffer,
1755 struct buffer_page *tail_page,
1756 unsigned long tail, unsigned long length)
1758 struct ring_buffer_event *event;
1761 * Only the event that crossed the page boundary
1762 * must fill the old tail_page with padding.
1764 if (tail >= BUF_PAGE_SIZE) {
1765 local_sub(length, &tail_page->write);
1769 event = __rb_page_index(tail_page, tail);
1770 kmemcheck_annotate_bitfield(event, bitfield);
1773 * If this event is bigger than the minimum size, then
1774 * we need to be careful that we don't subtract the
1775 * write counter enough to allow another writer to slip
1777 * We put in a discarded commit instead, to make sure
1778 * that this space is not used again.
1780 * If we are less than the minimum size, we don't need to
1783 if (tail > (BUF_PAGE_SIZE - RB_EVNT_MIN_SIZE)) {
1784 /* No room for any events */
1786 /* Mark the rest of the page with padding */
1787 rb_event_set_padding(event);
1789 /* Set the write back to the previous setting */
1790 local_sub(length, &tail_page->write);
1794 /* Put in a discarded event */
1795 event->array[0] = (BUF_PAGE_SIZE - tail) - RB_EVNT_HDR_SIZE;
1796 event->type_len = RINGBUF_TYPE_PADDING;
1797 /* time delta must be non zero */
1798 event->time_delta = 1;
1800 /* Set write to end of buffer */
1801 length = (tail + length) - BUF_PAGE_SIZE;
1802 local_sub(length, &tail_page->write);
1805 static struct ring_buffer_event *
1806 rb_move_tail(struct ring_buffer_per_cpu *cpu_buffer,
1807 unsigned long length, unsigned long tail,
1808 struct buffer_page *tail_page, u64 *ts)
1810 struct buffer_page *commit_page = cpu_buffer->commit_page;
1811 struct ring_buffer *buffer = cpu_buffer->buffer;
1812 struct buffer_page *next_page;
1815 next_page = tail_page;
1817 rb_inc_page(cpu_buffer, &next_page);
1820 * If for some reason, we had an interrupt storm that made
1821 * it all the way around the buffer, bail, and warn
1824 if (unlikely(next_page == commit_page)) {
1825 local_inc(&cpu_buffer->commit_overrun);
1830 * This is where the fun begins!
1832 * We are fighting against races between a reader that
1833 * could be on another CPU trying to swap its reader
1834 * page with the buffer head.
1836 * We are also fighting against interrupts coming in and
1837 * moving the head or tail on us as well.
1839 * If the next page is the head page then we have filled
1840 * the buffer, unless the commit page is still on the
1843 if (rb_is_head_page(cpu_buffer, next_page, &tail_page->list)) {
1846 * If the commit is not on the reader page, then
1847 * move the header page.
1849 if (!rb_is_reader_page(cpu_buffer->commit_page)) {
1851 * If we are not in overwrite mode,
1852 * this is easy, just stop here.
1854 if (!(buffer->flags & RB_FL_OVERWRITE))
1857 ret = rb_handle_head_page(cpu_buffer,
1866 * We need to be careful here too. The
1867 * commit page could still be on the reader
1868 * page. We could have a small buffer, and
1869 * have filled up the buffer with events
1870 * from interrupts and such, and wrapped.
1872 * Note, if the tail page is also the on the
1873 * reader_page, we let it move out.
1875 if (unlikely((cpu_buffer->commit_page !=
1876 cpu_buffer->tail_page) &&
1877 (cpu_buffer->commit_page ==
1878 cpu_buffer->reader_page))) {
1879 local_inc(&cpu_buffer->commit_overrun);
1885 ret = rb_tail_page_update(cpu_buffer, tail_page, next_page);
1888 * Nested commits always have zero deltas, so
1889 * just reread the time stamp
1891 *ts = rb_time_stamp(buffer);
1892 next_page->page->time_stamp = *ts;
1897 rb_reset_tail(cpu_buffer, tail_page, tail, length);
1899 /* fail and let the caller try again */
1900 return ERR_PTR(-EAGAIN);
1904 rb_reset_tail(cpu_buffer, tail_page, tail, length);
1909 static struct ring_buffer_event *
1910 __rb_reserve_next(struct ring_buffer_per_cpu *cpu_buffer,
1911 unsigned type, unsigned long length, u64 *ts)
1913 struct buffer_page *tail_page;
1914 struct ring_buffer_event *event;
1915 unsigned long tail, write;
1917 tail_page = cpu_buffer->tail_page;
1918 write = local_add_return(length, &tail_page->write);
1920 /* set write to only the index of the write */
1921 write &= RB_WRITE_MASK;
1922 tail = write - length;
1924 /* See if we shot pass the end of this buffer page */
1925 if (write > BUF_PAGE_SIZE)
1926 return rb_move_tail(cpu_buffer, length, tail,
1929 /* We reserved something on the buffer */
1931 event = __rb_page_index(tail_page, tail);
1932 kmemcheck_annotate_bitfield(event, bitfield);
1933 rb_update_event(event, type, length);
1935 /* The passed in type is zero for DATA */
1937 local_inc(&tail_page->entries);
1940 * If this is the first commit on the page, then update
1944 tail_page->page->time_stamp = *ts;
1950 rb_try_to_discard(struct ring_buffer_per_cpu *cpu_buffer,
1951 struct ring_buffer_event *event)
1953 unsigned long new_index, old_index;
1954 struct buffer_page *bpage;
1955 unsigned long index;
1958 new_index = rb_event_index(event);
1959 old_index = new_index + rb_event_length(event);
1960 addr = (unsigned long)event;
1963 bpage = cpu_buffer->tail_page;
1965 if (bpage->page == (void *)addr && rb_page_write(bpage) == old_index) {
1966 unsigned long write_mask =
1967 local_read(&bpage->write) & ~RB_WRITE_MASK;
1969 * This is on the tail page. It is possible that
1970 * a write could come in and move the tail page
1971 * and write to the next page. That is fine
1972 * because we just shorten what is on this page.
1974 old_index += write_mask;
1975 new_index += write_mask;
1976 index = local_cmpxchg(&bpage->write, old_index, new_index);
1977 if (index == old_index)
1981 /* could not discard */
1986 rb_add_time_stamp(struct ring_buffer_per_cpu *cpu_buffer,
1987 u64 *ts, u64 *delta)
1989 struct ring_buffer_event *event;
1993 if (unlikely(*delta > (1ULL << 59) && !once++)) {
1994 printk(KERN_WARNING "Delta way too big! %llu"
1995 " ts=%llu write stamp = %llu\n",
1996 (unsigned long long)*delta,
1997 (unsigned long long)*ts,
1998 (unsigned long long)cpu_buffer->write_stamp);
2003 * The delta is too big, we to add a
2006 event = __rb_reserve_next(cpu_buffer,
2007 RINGBUF_TYPE_TIME_EXTEND,
2013 if (PTR_ERR(event) == -EAGAIN)
2016 /* Only a commited time event can update the write stamp */
2017 if (rb_event_is_commit(cpu_buffer, event)) {
2019 * If this is the first on the page, then it was
2020 * updated with the page itself. Try to discard it
2021 * and if we can't just make it zero.
2023 if (rb_event_index(event)) {
2024 event->time_delta = *delta & TS_MASK;
2025 event->array[0] = *delta >> TS_SHIFT;
2027 /* try to discard, since we do not need this */
2028 if (!rb_try_to_discard(cpu_buffer, event)) {
2029 /* nope, just zero it */
2030 event->time_delta = 0;
2031 event->array[0] = 0;
2034 cpu_buffer->write_stamp = *ts;
2035 /* let the caller know this was the commit */
2038 /* Try to discard the event */
2039 if (!rb_try_to_discard(cpu_buffer, event)) {
2040 /* Darn, this is just wasted space */
2041 event->time_delta = 0;
2042 event->array[0] = 0;
2052 static void rb_start_commit(struct ring_buffer_per_cpu *cpu_buffer)
2054 local_inc(&cpu_buffer->committing);
2055 local_inc(&cpu_buffer->commits);
2058 static void rb_end_commit(struct ring_buffer_per_cpu *cpu_buffer)
2060 unsigned long commits;
2062 if (RB_WARN_ON(cpu_buffer,
2063 !local_read(&cpu_buffer->committing)))
2067 commits = local_read(&cpu_buffer->commits);
2068 /* synchronize with interrupts */
2070 if (local_read(&cpu_buffer->committing) == 1)
2071 rb_set_commit_to_write(cpu_buffer);
2073 local_dec(&cpu_buffer->committing);
2075 /* synchronize with interrupts */
2079 * Need to account for interrupts coming in between the
2080 * updating of the commit page and the clearing of the
2081 * committing counter.
2083 if (unlikely(local_read(&cpu_buffer->commits) != commits) &&
2084 !local_read(&cpu_buffer->committing)) {
2085 local_inc(&cpu_buffer->committing);
2090 static struct ring_buffer_event *
2091 rb_reserve_next_event(struct ring_buffer *buffer,
2092 struct ring_buffer_per_cpu *cpu_buffer,
2093 unsigned long length)
2095 struct ring_buffer_event *event;
2100 rb_start_commit(cpu_buffer);
2102 #ifdef CONFIG_RING_BUFFER_ALLOW_SWAP
2104 * Due to the ability to swap a cpu buffer from a buffer
2105 * it is possible it was swapped before we committed.
2106 * (committing stops a swap). We check for it here and
2107 * if it happened, we have to fail the write.
2110 if (unlikely(ACCESS_ONCE(cpu_buffer->buffer) != buffer)) {
2111 local_dec(&cpu_buffer->committing);
2112 local_dec(&cpu_buffer->commits);
2117 length = rb_calculate_event_length(length);
2120 * We allow for interrupts to reenter here and do a trace.
2121 * If one does, it will cause this original code to loop
2122 * back here. Even with heavy interrupts happening, this
2123 * should only happen a few times in a row. If this happens
2124 * 1000 times in a row, there must be either an interrupt
2125 * storm or we have something buggy.
2128 if (RB_WARN_ON(cpu_buffer, ++nr_loops > 1000))
2131 ts = rb_time_stamp(cpu_buffer->buffer);
2134 * Only the first commit can update the timestamp.
2135 * Yes there is a race here. If an interrupt comes in
2136 * just after the conditional and it traces too, then it
2137 * will also check the deltas. More than one timestamp may
2138 * also be made. But only the entry that did the actual
2139 * commit will be something other than zero.
2141 if (likely(cpu_buffer->tail_page == cpu_buffer->commit_page &&
2142 rb_page_write(cpu_buffer->tail_page) ==
2143 rb_commit_index(cpu_buffer))) {
2146 diff = ts - cpu_buffer->write_stamp;
2148 /* make sure this diff is calculated here */
2151 /* Did the write stamp get updated already? */
2152 if (unlikely(ts < cpu_buffer->write_stamp))
2156 if (unlikely(test_time_stamp(delta))) {
2158 commit = rb_add_time_stamp(cpu_buffer, &ts, &delta);
2159 if (commit == -EBUSY)
2162 if (commit == -EAGAIN)
2165 RB_WARN_ON(cpu_buffer, commit < 0);
2170 event = __rb_reserve_next(cpu_buffer, 0, length, &ts);
2171 if (unlikely(PTR_ERR(event) == -EAGAIN))
2177 if (!rb_event_is_commit(cpu_buffer, event))
2180 event->time_delta = delta;
2185 rb_end_commit(cpu_buffer);
2189 #ifdef CONFIG_TRACING
2191 #define TRACE_RECURSIVE_DEPTH 16
2193 static int trace_recursive_lock(void)
2195 current->trace_recursion++;
2197 if (likely(current->trace_recursion < TRACE_RECURSIVE_DEPTH))
2200 /* Disable all tracing before we do anything else */
2201 tracing_off_permanent();
2203 printk_once(KERN_WARNING "Tracing recursion: depth[%ld]:"
2204 "HC[%lu]:SC[%lu]:NMI[%lu]\n",
2205 current->trace_recursion,
2206 hardirq_count() >> HARDIRQ_SHIFT,
2207 softirq_count() >> SOFTIRQ_SHIFT,
2214 static void trace_recursive_unlock(void)
2216 WARN_ON_ONCE(!current->trace_recursion);
2218 current->trace_recursion--;
2223 #define trace_recursive_lock() (0)
2224 #define trace_recursive_unlock() do { } while (0)
2228 static DEFINE_PER_CPU(int, rb_need_resched);
2231 * ring_buffer_lock_reserve - reserve a part of the buffer
2232 * @buffer: the ring buffer to reserve from
2233 * @length: the length of the data to reserve (excluding event header)
2235 * Returns a reseverd event on the ring buffer to copy directly to.
2236 * The user of this interface will need to get the body to write into
2237 * and can use the ring_buffer_event_data() interface.
2239 * The length is the length of the data needed, not the event length
2240 * which also includes the event header.
2242 * Must be paired with ring_buffer_unlock_commit, unless NULL is returned.
2243 * If NULL is returned, then nothing has been allocated or locked.
2245 struct ring_buffer_event *
2246 ring_buffer_lock_reserve(struct ring_buffer *buffer, unsigned long length)
2248 struct ring_buffer_per_cpu *cpu_buffer;
2249 struct ring_buffer_event *event;
2252 if (ring_buffer_flags != RB_BUFFERS_ON)
2255 /* If we are tracing schedule, we don't want to recurse */
2256 resched = ftrace_preempt_disable();
2258 if (atomic_read(&buffer->record_disabled))
2261 if (trace_recursive_lock())
2264 cpu = raw_smp_processor_id();
2266 if (!cpumask_test_cpu(cpu, buffer->cpumask))
2269 cpu_buffer = buffer->buffers[cpu];
2271 if (atomic_read(&cpu_buffer->record_disabled))
2274 if (length > BUF_MAX_DATA_SIZE)
2277 event = rb_reserve_next_event(buffer, cpu_buffer, length);
2282 * Need to store resched state on this cpu.
2283 * Only the first needs to.
2286 if (preempt_count() == 1)
2287 per_cpu(rb_need_resched, cpu) = resched;
2292 trace_recursive_unlock();
2295 ftrace_preempt_enable(resched);
2298 EXPORT_SYMBOL_GPL(ring_buffer_lock_reserve);
2301 rb_update_write_stamp(struct ring_buffer_per_cpu *cpu_buffer,
2302 struct ring_buffer_event *event)
2305 * The event first in the commit queue updates the
2308 if (rb_event_is_commit(cpu_buffer, event))
2309 cpu_buffer->write_stamp += event->time_delta;
2312 static void rb_commit(struct ring_buffer_per_cpu *cpu_buffer,
2313 struct ring_buffer_event *event)
2315 local_inc(&cpu_buffer->entries);
2316 rb_update_write_stamp(cpu_buffer, event);
2317 rb_end_commit(cpu_buffer);
2321 * ring_buffer_unlock_commit - commit a reserved
2322 * @buffer: The buffer to commit to
2323 * @event: The event pointer to commit.
2325 * This commits the data to the ring buffer, and releases any locks held.
2327 * Must be paired with ring_buffer_lock_reserve.
2329 int ring_buffer_unlock_commit(struct ring_buffer *buffer,
2330 struct ring_buffer_event *event)
2332 struct ring_buffer_per_cpu *cpu_buffer;
2333 int cpu = raw_smp_processor_id();
2335 cpu_buffer = buffer->buffers[cpu];
2337 rb_commit(cpu_buffer, event);
2339 trace_recursive_unlock();
2342 * Only the last preempt count needs to restore preemption.
2344 if (preempt_count() == 1)
2345 ftrace_preempt_enable(per_cpu(rb_need_resched, cpu));
2347 preempt_enable_no_resched_notrace();
2351 EXPORT_SYMBOL_GPL(ring_buffer_unlock_commit);
2353 static inline void rb_event_discard(struct ring_buffer_event *event)
2355 /* array[0] holds the actual length for the discarded event */
2356 event->array[0] = rb_event_data_length(event) - RB_EVNT_HDR_SIZE;
2357 event->type_len = RINGBUF_TYPE_PADDING;
2358 /* time delta must be non zero */
2359 if (!event->time_delta)
2360 event->time_delta = 1;
2364 * Decrement the entries to the page that an event is on.
2365 * The event does not even need to exist, only the pointer
2366 * to the page it is on. This may only be called before the commit
2370 rb_decrement_entry(struct ring_buffer_per_cpu *cpu_buffer,
2371 struct ring_buffer_event *event)
2373 unsigned long addr = (unsigned long)event;
2374 struct buffer_page *bpage = cpu_buffer->commit_page;
2375 struct buffer_page *start;
2379 /* Do the likely case first */
2380 if (likely(bpage->page == (void *)addr)) {
2381 local_dec(&bpage->entries);
2386 * Because the commit page may be on the reader page we
2387 * start with the next page and check the end loop there.
2389 rb_inc_page(cpu_buffer, &bpage);
2392 if (bpage->page == (void *)addr) {
2393 local_dec(&bpage->entries);
2396 rb_inc_page(cpu_buffer, &bpage);
2397 } while (bpage != start);
2399 /* commit not part of this buffer?? */
2400 RB_WARN_ON(cpu_buffer, 1);
2404 * ring_buffer_commit_discard - discard an event that has not been committed
2405 * @buffer: the ring buffer
2406 * @event: non committed event to discard
2408 * Sometimes an event that is in the ring buffer needs to be ignored.
2409 * This function lets the user discard an event in the ring buffer
2410 * and then that event will not be read later.
2412 * This function only works if it is called before the the item has been
2413 * committed. It will try to free the event from the ring buffer
2414 * if another event has not been added behind it.
2416 * If another event has been added behind it, it will set the event
2417 * up as discarded, and perform the commit.
2419 * If this function is called, do not call ring_buffer_unlock_commit on
2422 void ring_buffer_discard_commit(struct ring_buffer *buffer,
2423 struct ring_buffer_event *event)
2425 struct ring_buffer_per_cpu *cpu_buffer;
2428 /* The event is discarded regardless */
2429 rb_event_discard(event);
2431 cpu = smp_processor_id();
2432 cpu_buffer = buffer->buffers[cpu];
2435 * This must only be called if the event has not been
2436 * committed yet. Thus we can assume that preemption
2437 * is still disabled.
2439 RB_WARN_ON(buffer, !local_read(&cpu_buffer->committing));
2441 rb_decrement_entry(cpu_buffer, event);
2442 if (rb_try_to_discard(cpu_buffer, event))
2446 * The commit is still visible by the reader, so we
2447 * must still update the timestamp.
2449 rb_update_write_stamp(cpu_buffer, event);
2451 rb_end_commit(cpu_buffer);
2453 trace_recursive_unlock();
2456 * Only the last preempt count needs to restore preemption.
2458 if (preempt_count() == 1)
2459 ftrace_preempt_enable(per_cpu(rb_need_resched, cpu));
2461 preempt_enable_no_resched_notrace();
2464 EXPORT_SYMBOL_GPL(ring_buffer_discard_commit);
2467 * ring_buffer_write - write data to the buffer without reserving
2468 * @buffer: The ring buffer to write to.
2469 * @length: The length of the data being written (excluding the event header)
2470 * @data: The data to write to the buffer.
2472 * This is like ring_buffer_lock_reserve and ring_buffer_unlock_commit as
2473 * one function. If you already have the data to write to the buffer, it
2474 * may be easier to simply call this function.
2476 * Note, like ring_buffer_lock_reserve, the length is the length of the data
2477 * and not the length of the event which would hold the header.
2479 int ring_buffer_write(struct ring_buffer *buffer,
2480 unsigned long length,
2483 struct ring_buffer_per_cpu *cpu_buffer;
2484 struct ring_buffer_event *event;
2489 if (ring_buffer_flags != RB_BUFFERS_ON)
2492 resched = ftrace_preempt_disable();
2494 if (atomic_read(&buffer->record_disabled))
2497 cpu = raw_smp_processor_id();
2499 if (!cpumask_test_cpu(cpu, buffer->cpumask))
2502 cpu_buffer = buffer->buffers[cpu];
2504 if (atomic_read(&cpu_buffer->record_disabled))
2507 if (length > BUF_MAX_DATA_SIZE)
2510 event = rb_reserve_next_event(buffer, cpu_buffer, length);
2514 body = rb_event_data(event);
2516 memcpy(body, data, length);
2518 rb_commit(cpu_buffer, event);
2522 ftrace_preempt_enable(resched);
2526 EXPORT_SYMBOL_GPL(ring_buffer_write);
2528 static int rb_per_cpu_empty(struct ring_buffer_per_cpu *cpu_buffer)
2530 struct buffer_page *reader = cpu_buffer->reader_page;
2531 struct buffer_page *head = rb_set_head_page(cpu_buffer);
2532 struct buffer_page *commit = cpu_buffer->commit_page;
2534 /* In case of error, head will be NULL */
2535 if (unlikely(!head))
2538 return reader->read == rb_page_commit(reader) &&
2539 (commit == reader ||
2541 head->read == rb_page_commit(commit)));
2545 * ring_buffer_record_disable - stop all writes into the buffer
2546 * @buffer: The ring buffer to stop writes to.
2548 * This prevents all writes to the buffer. Any attempt to write
2549 * to the buffer after this will fail and return NULL.
2551 * The caller should call synchronize_sched() after this.
2553 void ring_buffer_record_disable(struct ring_buffer *buffer)
2555 atomic_inc(&buffer->record_disabled);
2557 EXPORT_SYMBOL_GPL(ring_buffer_record_disable);
2560 * ring_buffer_record_enable - enable writes to the buffer
2561 * @buffer: The ring buffer to enable writes
2563 * Note, multiple disables will need the same number of enables
2564 * to truly enable the writing (much like preempt_disable).
2566 void ring_buffer_record_enable(struct ring_buffer *buffer)
2568 atomic_dec(&buffer->record_disabled);
2570 EXPORT_SYMBOL_GPL(ring_buffer_record_enable);
2573 * ring_buffer_record_disable_cpu - stop all writes into the cpu_buffer
2574 * @buffer: The ring buffer to stop writes to.
2575 * @cpu: The CPU buffer to stop
2577 * This prevents all writes to the buffer. Any attempt to write
2578 * to the buffer after this will fail and return NULL.
2580 * The caller should call synchronize_sched() after this.
2582 void ring_buffer_record_disable_cpu(struct ring_buffer *buffer, int cpu)
2584 struct ring_buffer_per_cpu *cpu_buffer;
2586 if (!cpumask_test_cpu(cpu, buffer->cpumask))
2589 cpu_buffer = buffer->buffers[cpu];
2590 atomic_inc(&cpu_buffer->record_disabled);
2592 EXPORT_SYMBOL_GPL(ring_buffer_record_disable_cpu);
2595 * ring_buffer_record_enable_cpu - enable writes to the buffer
2596 * @buffer: The ring buffer to enable writes
2597 * @cpu: The CPU to enable.
2599 * Note, multiple disables will need the same number of enables
2600 * to truly enable the writing (much like preempt_disable).
2602 void ring_buffer_record_enable_cpu(struct ring_buffer *buffer, int cpu)
2604 struct ring_buffer_per_cpu *cpu_buffer;
2606 if (!cpumask_test_cpu(cpu, buffer->cpumask))
2609 cpu_buffer = buffer->buffers[cpu];
2610 atomic_dec(&cpu_buffer->record_disabled);
2612 EXPORT_SYMBOL_GPL(ring_buffer_record_enable_cpu);
2615 * ring_buffer_entries_cpu - get the number of entries in a cpu buffer
2616 * @buffer: The ring buffer
2617 * @cpu: The per CPU buffer to get the entries from.
2619 unsigned long ring_buffer_entries_cpu(struct ring_buffer *buffer, int cpu)
2621 struct ring_buffer_per_cpu *cpu_buffer;
2624 if (!cpumask_test_cpu(cpu, buffer->cpumask))
2627 cpu_buffer = buffer->buffers[cpu];
2628 ret = (local_read(&cpu_buffer->entries) - local_read(&cpu_buffer->overrun))
2633 EXPORT_SYMBOL_GPL(ring_buffer_entries_cpu);
2636 * ring_buffer_overrun_cpu - get the number of overruns in a cpu_buffer
2637 * @buffer: The ring buffer
2638 * @cpu: The per CPU buffer to get the number of overruns from
2640 unsigned long ring_buffer_overrun_cpu(struct ring_buffer *buffer, int cpu)
2642 struct ring_buffer_per_cpu *cpu_buffer;
2645 if (!cpumask_test_cpu(cpu, buffer->cpumask))
2648 cpu_buffer = buffer->buffers[cpu];
2649 ret = local_read(&cpu_buffer->overrun);
2653 EXPORT_SYMBOL_GPL(ring_buffer_overrun_cpu);
2656 * ring_buffer_commit_overrun_cpu - get the number of overruns caused by commits
2657 * @buffer: The ring buffer
2658 * @cpu: The per CPU buffer to get the number of overruns from
2661 ring_buffer_commit_overrun_cpu(struct ring_buffer *buffer, int cpu)
2663 struct ring_buffer_per_cpu *cpu_buffer;
2666 if (!cpumask_test_cpu(cpu, buffer->cpumask))
2669 cpu_buffer = buffer->buffers[cpu];
2670 ret = local_read(&cpu_buffer->commit_overrun);
2674 EXPORT_SYMBOL_GPL(ring_buffer_commit_overrun_cpu);
2677 * ring_buffer_entries - get the number of entries in a buffer
2678 * @buffer: The ring buffer
2680 * Returns the total number of entries in the ring buffer
2683 unsigned long ring_buffer_entries(struct ring_buffer *buffer)
2685 struct ring_buffer_per_cpu *cpu_buffer;
2686 unsigned long entries = 0;
2689 /* if you care about this being correct, lock the buffer */
2690 for_each_buffer_cpu(buffer, cpu) {
2691 cpu_buffer = buffer->buffers[cpu];
2692 entries += (local_read(&cpu_buffer->entries) -
2693 local_read(&cpu_buffer->overrun)) - cpu_buffer->read;
2698 EXPORT_SYMBOL_GPL(ring_buffer_entries);
2701 * ring_buffer_overruns - get the number of overruns in buffer
2702 * @buffer: The ring buffer
2704 * Returns the total number of overruns in the ring buffer
2707 unsigned long ring_buffer_overruns(struct ring_buffer *buffer)
2709 struct ring_buffer_per_cpu *cpu_buffer;
2710 unsigned long overruns = 0;
2713 /* if you care about this being correct, lock the buffer */
2714 for_each_buffer_cpu(buffer, cpu) {
2715 cpu_buffer = buffer->buffers[cpu];
2716 overruns += local_read(&cpu_buffer->overrun);
2721 EXPORT_SYMBOL_GPL(ring_buffer_overruns);
2723 static void rb_iter_reset(struct ring_buffer_iter *iter)
2725 struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
2727 /* Iterator usage is expected to have record disabled */
2728 if (list_empty(&cpu_buffer->reader_page->list)) {
2729 iter->head_page = rb_set_head_page(cpu_buffer);
2730 if (unlikely(!iter->head_page))
2732 iter->head = iter->head_page->read;
2734 iter->head_page = cpu_buffer->reader_page;
2735 iter->head = cpu_buffer->reader_page->read;
2738 iter->read_stamp = cpu_buffer->read_stamp;
2740 iter->read_stamp = iter->head_page->page->time_stamp;
2741 iter->cache_reader_page = cpu_buffer->reader_page;
2742 iter->cache_read = cpu_buffer->read;
2746 * ring_buffer_iter_reset - reset an iterator
2747 * @iter: The iterator to reset
2749 * Resets the iterator, so that it will start from the beginning
2752 void ring_buffer_iter_reset(struct ring_buffer_iter *iter)
2754 struct ring_buffer_per_cpu *cpu_buffer;
2755 unsigned long flags;
2760 cpu_buffer = iter->cpu_buffer;
2762 spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
2763 rb_iter_reset(iter);
2764 spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
2766 EXPORT_SYMBOL_GPL(ring_buffer_iter_reset);
2769 * ring_buffer_iter_empty - check if an iterator has no more to read
2770 * @iter: The iterator to check
2772 int ring_buffer_iter_empty(struct ring_buffer_iter *iter)
2774 struct ring_buffer_per_cpu *cpu_buffer;
2776 cpu_buffer = iter->cpu_buffer;
2778 return iter->head_page == cpu_buffer->commit_page &&
2779 iter->head == rb_commit_index(cpu_buffer);
2781 EXPORT_SYMBOL_GPL(ring_buffer_iter_empty);
2784 rb_update_read_stamp(struct ring_buffer_per_cpu *cpu_buffer,
2785 struct ring_buffer_event *event)
2789 switch (event->type_len) {
2790 case RINGBUF_TYPE_PADDING:
2793 case RINGBUF_TYPE_TIME_EXTEND:
2794 delta = event->array[0];
2796 delta += event->time_delta;
2797 cpu_buffer->read_stamp += delta;
2800 case RINGBUF_TYPE_TIME_STAMP:
2801 /* FIXME: not implemented */
2804 case RINGBUF_TYPE_DATA:
2805 cpu_buffer->read_stamp += event->time_delta;
2815 rb_update_iter_read_stamp(struct ring_buffer_iter *iter,
2816 struct ring_buffer_event *event)
2820 switch (event->type_len) {
2821 case RINGBUF_TYPE_PADDING:
2824 case RINGBUF_TYPE_TIME_EXTEND:
2825 delta = event->array[0];
2827 delta += event->time_delta;
2828 iter->read_stamp += delta;
2831 case RINGBUF_TYPE_TIME_STAMP:
2832 /* FIXME: not implemented */
2835 case RINGBUF_TYPE_DATA:
2836 iter->read_stamp += event->time_delta;
2845 static struct buffer_page *
2846 rb_get_reader_page(struct ring_buffer_per_cpu *cpu_buffer)
2848 struct buffer_page *reader = NULL;
2849 unsigned long overwrite;
2850 unsigned long flags;
2854 local_irq_save(flags);
2855 arch_spin_lock(&cpu_buffer->lock);
2859 * This should normally only loop twice. But because the
2860 * start of the reader inserts an empty page, it causes
2861 * a case where we will loop three times. There should be no
2862 * reason to loop four times (that I know of).
2864 if (RB_WARN_ON(cpu_buffer, ++nr_loops > 3)) {
2869 reader = cpu_buffer->reader_page;
2871 /* If there's more to read, return this page */
2872 if (cpu_buffer->reader_page->read < rb_page_size(reader))
2875 /* Never should we have an index greater than the size */
2876 if (RB_WARN_ON(cpu_buffer,
2877 cpu_buffer->reader_page->read > rb_page_size(reader)))
2880 /* check if we caught up to the tail */
2882 if (cpu_buffer->commit_page == cpu_buffer->reader_page)
2886 * Reset the reader page to size zero.
2888 local_set(&cpu_buffer->reader_page->write, 0);
2889 local_set(&cpu_buffer->reader_page->entries, 0);
2890 local_set(&cpu_buffer->reader_page->page->commit, 0);
2894 * Splice the empty reader page into the list around the head.
2896 reader = rb_set_head_page(cpu_buffer);
2897 cpu_buffer->reader_page->list.next = rb_list_head(reader->list.next);
2898 cpu_buffer->reader_page->list.prev = reader->list.prev;
2901 * cpu_buffer->pages just needs to point to the buffer, it
2902 * has no specific buffer page to point to. Lets move it out
2903 * of our way so we don't accidently swap it.
2905 cpu_buffer->pages = reader->list.prev;
2907 /* The reader page will be pointing to the new head */
2908 rb_set_list_to_head(cpu_buffer, &cpu_buffer->reader_page->list);
2911 * We want to make sure we read the overruns after we set up our
2912 * pointers to the next object. The writer side does a
2913 * cmpxchg to cross pages which acts as the mb on the writer
2914 * side. Note, the reader will constantly fail the swap
2915 * while the writer is updating the pointers, so this
2916 * guarantees that the overwrite recorded here is the one we
2917 * want to compare with the last_overrun.
2920 overwrite = local_read(&(cpu_buffer->overrun));
2923 * Here's the tricky part.
2925 * We need to move the pointer past the header page.
2926 * But we can only do that if a writer is not currently
2927 * moving it. The page before the header page has the
2928 * flag bit '1' set if it is pointing to the page we want.
2929 * but if the writer is in the process of moving it
2930 * than it will be '2' or already moved '0'.
2933 ret = rb_head_page_replace(reader, cpu_buffer->reader_page);
2936 * If we did not convert it, then we must try again.
2942 * Yeah! We succeeded in replacing the page.
2944 * Now make the new head point back to the reader page.
2946 rb_list_head(reader->list.next)->prev = &cpu_buffer->reader_page->list;
2947 rb_inc_page(cpu_buffer, &cpu_buffer->head_page);
2949 /* Finally update the reader page to the new head */
2950 cpu_buffer->reader_page = reader;
2951 rb_reset_reader_page(cpu_buffer);
2953 if (overwrite != cpu_buffer->last_overrun) {
2954 cpu_buffer->lost_events = overwrite - cpu_buffer->last_overrun;
2955 cpu_buffer->last_overrun = overwrite;
2961 arch_spin_unlock(&cpu_buffer->lock);
2962 local_irq_restore(flags);
2967 static void rb_advance_reader(struct ring_buffer_per_cpu *cpu_buffer)
2969 struct ring_buffer_event *event;
2970 struct buffer_page *reader;
2973 reader = rb_get_reader_page(cpu_buffer);
2975 /* This function should not be called when buffer is empty */
2976 if (RB_WARN_ON(cpu_buffer, !reader))
2979 event = rb_reader_event(cpu_buffer);
2981 if (event->type_len <= RINGBUF_TYPE_DATA_TYPE_LEN_MAX)
2984 rb_update_read_stamp(cpu_buffer, event);
2986 length = rb_event_length(event);
2987 cpu_buffer->reader_page->read += length;
2990 static void rb_advance_iter(struct ring_buffer_iter *iter)
2992 struct ring_buffer *buffer;
2993 struct ring_buffer_per_cpu *cpu_buffer;
2994 struct ring_buffer_event *event;
2997 cpu_buffer = iter->cpu_buffer;
2998 buffer = cpu_buffer->buffer;
3001 * Check if we are at the end of the buffer.
3003 if (iter->head >= rb_page_size(iter->head_page)) {
3004 /* discarded commits can make the page empty */
3005 if (iter->head_page == cpu_buffer->commit_page)
3011 event = rb_iter_head_event(iter);
3013 length = rb_event_length(event);
3016 * This should not be called to advance the header if we are
3017 * at the tail of the buffer.
3019 if (RB_WARN_ON(cpu_buffer,
3020 (iter->head_page == cpu_buffer->commit_page) &&
3021 (iter->head + length > rb_commit_index(cpu_buffer))))
3024 rb_update_iter_read_stamp(iter, event);
3026 iter->head += length;
3028 /* check for end of page padding */
3029 if ((iter->head >= rb_page_size(iter->head_page)) &&
3030 (iter->head_page != cpu_buffer->commit_page))
3031 rb_advance_iter(iter);
3034 static int rb_lost_events(struct ring_buffer_per_cpu *cpu_buffer)
3036 return cpu_buffer->lost_events;
3039 static struct ring_buffer_event *
3040 rb_buffer_peek(struct ring_buffer_per_cpu *cpu_buffer, u64 *ts,
3041 unsigned long *lost_events)
3043 struct ring_buffer_event *event;
3044 struct buffer_page *reader;
3049 * We repeat when a timestamp is encountered. It is possible
3050 * to get multiple timestamps from an interrupt entering just
3051 * as one timestamp is about to be written, or from discarded
3052 * commits. The most that we can have is the number on a single page.
3054 if (RB_WARN_ON(cpu_buffer, ++nr_loops > RB_TIMESTAMPS_PER_PAGE))
3057 reader = rb_get_reader_page(cpu_buffer);
3061 event = rb_reader_event(cpu_buffer);
3063 switch (event->type_len) {
3064 case RINGBUF_TYPE_PADDING:
3065 if (rb_null_event(event))
3066 RB_WARN_ON(cpu_buffer, 1);
3068 * Because the writer could be discarding every
3069 * event it creates (which would probably be bad)
3070 * if we were to go back to "again" then we may never
3071 * catch up, and will trigger the warn on, or lock
3072 * the box. Return the padding, and we will release
3073 * the current locks, and try again.
3077 case RINGBUF_TYPE_TIME_EXTEND:
3078 /* Internal data, OK to advance */
3079 rb_advance_reader(cpu_buffer);
3082 case RINGBUF_TYPE_TIME_STAMP:
3083 /* FIXME: not implemented */
3084 rb_advance_reader(cpu_buffer);
3087 case RINGBUF_TYPE_DATA:
3089 *ts = cpu_buffer->read_stamp + event->time_delta;
3090 ring_buffer_normalize_time_stamp(cpu_buffer->buffer,
3091 cpu_buffer->cpu, ts);
3094 *lost_events = rb_lost_events(cpu_buffer);
3103 EXPORT_SYMBOL_GPL(ring_buffer_peek);
3105 static struct ring_buffer_event *
3106 rb_iter_peek(struct ring_buffer_iter *iter, u64 *ts)
3108 struct ring_buffer *buffer;
3109 struct ring_buffer_per_cpu *cpu_buffer;
3110 struct ring_buffer_event *event;
3113 cpu_buffer = iter->cpu_buffer;
3114 buffer = cpu_buffer->buffer;
3117 * Check if someone performed a consuming read to
3118 * the buffer. A consuming read invalidates the iterator
3119 * and we need to reset the iterator in this case.
3121 if (unlikely(iter->cache_read != cpu_buffer->read ||
3122 iter->cache_reader_page != cpu_buffer->reader_page))
3123 rb_iter_reset(iter);
3126 if (ring_buffer_iter_empty(iter))
3130 * We repeat when a timestamp is encountered.
3131 * We can get multiple timestamps by nested interrupts or also
3132 * if filtering is on (discarding commits). Since discarding
3133 * commits can be frequent we can get a lot of timestamps.
3134 * But we limit them by not adding timestamps if they begin
3135 * at the start of a page.
3137 if (RB_WARN_ON(cpu_buffer, ++nr_loops > RB_TIMESTAMPS_PER_PAGE))
3140 if (rb_per_cpu_empty(cpu_buffer))
3143 if (iter->head >= local_read(&iter->head_page->page->commit)) {
3148 event = rb_iter_head_event(iter);
3150 switch (event->type_len) {
3151 case RINGBUF_TYPE_PADDING:
3152 if (rb_null_event(event)) {
3156 rb_advance_iter(iter);
3159 case RINGBUF_TYPE_TIME_EXTEND:
3160 /* Internal data, OK to advance */
3161 rb_advance_iter(iter);
3164 case RINGBUF_TYPE_TIME_STAMP:
3165 /* FIXME: not implemented */
3166 rb_advance_iter(iter);
3169 case RINGBUF_TYPE_DATA:
3171 *ts = iter->read_stamp + event->time_delta;
3172 ring_buffer_normalize_time_stamp(buffer,
3173 cpu_buffer->cpu, ts);
3183 EXPORT_SYMBOL_GPL(ring_buffer_iter_peek);
3185 static inline int rb_ok_to_lock(void)
3188 * If an NMI die dumps out the content of the ring buffer
3189 * do not grab locks. We also permanently disable the ring
3190 * buffer too. A one time deal is all you get from reading
3191 * the ring buffer from an NMI.
3193 if (likely(!in_nmi()))
3196 tracing_off_permanent();
3201 * ring_buffer_peek - peek at the next event to be read
3202 * @buffer: The ring buffer to read
3203 * @cpu: The cpu to peak at
3204 * @ts: The timestamp counter of this event.
3205 * @lost_events: a variable to store if events were lost (may be NULL)
3207 * This will return the event that will be read next, but does
3208 * not consume the data.
3210 struct ring_buffer_event *
3211 ring_buffer_peek(struct ring_buffer *buffer, int cpu, u64 *ts,
3212 unsigned long *lost_events)
3214 struct ring_buffer_per_cpu *cpu_buffer = buffer->buffers[cpu];
3215 struct ring_buffer_event *event;
3216 unsigned long flags;
3219 if (!cpumask_test_cpu(cpu, buffer->cpumask))
3222 dolock = rb_ok_to_lock();
3224 local_irq_save(flags);
3226 spin_lock(&cpu_buffer->reader_lock);
3227 event = rb_buffer_peek(cpu_buffer, ts, lost_events);
3228 if (event && event->type_len == RINGBUF_TYPE_PADDING)
3229 rb_advance_reader(cpu_buffer);
3231 spin_unlock(&cpu_buffer->reader_lock);
3232 local_irq_restore(flags);
3234 if (event && event->type_len == RINGBUF_TYPE_PADDING)
3241 * ring_buffer_iter_peek - peek at the next event to be read
3242 * @iter: The ring buffer iterator
3243 * @ts: The timestamp counter of this event.
3245 * This will return the event that will be read next, but does
3246 * not increment the iterator.
3248 struct ring_buffer_event *
3249 ring_buffer_iter_peek(struct ring_buffer_iter *iter, u64 *ts)
3251 struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
3252 struct ring_buffer_event *event;
3253 unsigned long flags;
3256 spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
3257 event = rb_iter_peek(iter, ts);
3258 spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
3260 if (event && event->type_len == RINGBUF_TYPE_PADDING)
3267 * ring_buffer_consume - return an event and consume it
3268 * @buffer: The ring buffer to get the next event from
3269 * @cpu: the cpu to read the buffer from
3270 * @ts: a variable to store the timestamp (may be NULL)
3271 * @lost_events: a variable to store if events were lost (may be NULL)
3273 * Returns the next event in the ring buffer, and that event is consumed.
3274 * Meaning, that sequential reads will keep returning a different event,
3275 * and eventually empty the ring buffer if the producer is slower.
3277 struct ring_buffer_event *
3278 ring_buffer_consume(struct ring_buffer *buffer, int cpu, u64 *ts,
3279 unsigned long *lost_events)
3281 struct ring_buffer_per_cpu *cpu_buffer;
3282 struct ring_buffer_event *event = NULL;
3283 unsigned long flags;
3286 dolock = rb_ok_to_lock();
3289 /* might be called in atomic */
3292 if (!cpumask_test_cpu(cpu, buffer->cpumask))
3295 cpu_buffer = buffer->buffers[cpu];
3296 local_irq_save(flags);
3298 spin_lock(&cpu_buffer->reader_lock);
3300 event = rb_buffer_peek(cpu_buffer, ts, lost_events);
3302 cpu_buffer->lost_events = 0;
3303 rb_advance_reader(cpu_buffer);
3307 spin_unlock(&cpu_buffer->reader_lock);
3308 local_irq_restore(flags);
3313 if (event && event->type_len == RINGBUF_TYPE_PADDING)
3318 EXPORT_SYMBOL_GPL(ring_buffer_consume);
3321 * ring_buffer_read_start - start a non consuming read of the buffer
3322 * @buffer: The ring buffer to read from
3323 * @cpu: The cpu buffer to iterate over
3325 * This starts up an iteration through the buffer. It also disables
3326 * the recording to the buffer until the reading is finished.
3327 * This prevents the reading from being corrupted. This is not
3328 * a consuming read, so a producer is not expected.
3330 * Must be paired with ring_buffer_finish.
3332 struct ring_buffer_iter *
3333 ring_buffer_read_start(struct ring_buffer *buffer, int cpu)
3335 struct ring_buffer_per_cpu *cpu_buffer;
3336 struct ring_buffer_iter *iter;
3337 unsigned long flags;
3339 if (!cpumask_test_cpu(cpu, buffer->cpumask))
3342 iter = kmalloc(sizeof(*iter), GFP_KERNEL);
3346 cpu_buffer = buffer->buffers[cpu];
3348 iter->cpu_buffer = cpu_buffer;
3350 atomic_inc(&cpu_buffer->record_disabled);
3351 synchronize_sched();
3353 spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
3354 arch_spin_lock(&cpu_buffer->lock);
3355 rb_iter_reset(iter);
3356 arch_spin_unlock(&cpu_buffer->lock);
3357 spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
3361 EXPORT_SYMBOL_GPL(ring_buffer_read_start);
3364 * ring_buffer_finish - finish reading the iterator of the buffer
3365 * @iter: The iterator retrieved by ring_buffer_start
3367 * This re-enables the recording to the buffer, and frees the
3371 ring_buffer_read_finish(struct ring_buffer_iter *iter)
3373 struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
3375 atomic_dec(&cpu_buffer->record_disabled);
3378 EXPORT_SYMBOL_GPL(ring_buffer_read_finish);
3381 * ring_buffer_read - read the next item in the ring buffer by the iterator
3382 * @iter: The ring buffer iterator
3383 * @ts: The time stamp of the event read.
3385 * This reads the next event in the ring buffer and increments the iterator.
3387 struct ring_buffer_event *
3388 ring_buffer_read(struct ring_buffer_iter *iter, u64 *ts)
3390 struct ring_buffer_event *event;
3391 struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
3392 unsigned long flags;
3394 spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
3396 event = rb_iter_peek(iter, ts);
3400 if (event->type_len == RINGBUF_TYPE_PADDING)
3403 rb_advance_iter(iter);
3405 spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
3409 EXPORT_SYMBOL_GPL(ring_buffer_read);
3412 * ring_buffer_size - return the size of the ring buffer (in bytes)
3413 * @buffer: The ring buffer.
3415 unsigned long ring_buffer_size(struct ring_buffer *buffer)
3417 return BUF_PAGE_SIZE * buffer->pages;
3419 EXPORT_SYMBOL_GPL(ring_buffer_size);
3422 rb_reset_cpu(struct ring_buffer_per_cpu *cpu_buffer)
3424 rb_head_page_deactivate(cpu_buffer);
3426 cpu_buffer->head_page
3427 = list_entry(cpu_buffer->pages, struct buffer_page, list);
3428 local_set(&cpu_buffer->head_page->write, 0);
3429 local_set(&cpu_buffer->head_page->entries, 0);
3430 local_set(&cpu_buffer->head_page->page->commit, 0);
3432 cpu_buffer->head_page->read = 0;
3434 cpu_buffer->tail_page = cpu_buffer->head_page;
3435 cpu_buffer->commit_page = cpu_buffer->head_page;
3437 INIT_LIST_HEAD(&cpu_buffer->reader_page->list);
3438 local_set(&cpu_buffer->reader_page->write, 0);
3439 local_set(&cpu_buffer->reader_page->entries, 0);
3440 local_set(&cpu_buffer->reader_page->page->commit, 0);
3441 cpu_buffer->reader_page->read = 0;
3443 local_set(&cpu_buffer->commit_overrun, 0);
3444 local_set(&cpu_buffer->overrun, 0);
3445 local_set(&cpu_buffer->entries, 0);
3446 local_set(&cpu_buffer->committing, 0);
3447 local_set(&cpu_buffer->commits, 0);
3448 cpu_buffer->read = 0;
3450 cpu_buffer->write_stamp = 0;
3451 cpu_buffer->read_stamp = 0;
3453 cpu_buffer->lost_events = 0;
3454 cpu_buffer->last_overrun = 0;
3456 rb_head_page_activate(cpu_buffer);
3460 * ring_buffer_reset_cpu - reset a ring buffer per CPU buffer
3461 * @buffer: The ring buffer to reset a per cpu buffer of
3462 * @cpu: The CPU buffer to be reset
3464 void ring_buffer_reset_cpu(struct ring_buffer *buffer, int cpu)
3466 struct ring_buffer_per_cpu *cpu_buffer = buffer->buffers[cpu];
3467 unsigned long flags;
3469 if (!cpumask_test_cpu(cpu, buffer->cpumask))
3472 atomic_inc(&cpu_buffer->record_disabled);
3474 spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
3476 if (RB_WARN_ON(cpu_buffer, local_read(&cpu_buffer->committing)))
3479 arch_spin_lock(&cpu_buffer->lock);
3481 rb_reset_cpu(cpu_buffer);
3483 arch_spin_unlock(&cpu_buffer->lock);
3486 spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
3488 atomic_dec(&cpu_buffer->record_disabled);
3490 EXPORT_SYMBOL_GPL(ring_buffer_reset_cpu);
3493 * ring_buffer_reset - reset a ring buffer
3494 * @buffer: The ring buffer to reset all cpu buffers
3496 void ring_buffer_reset(struct ring_buffer *buffer)
3500 for_each_buffer_cpu(buffer, cpu)
3501 ring_buffer_reset_cpu(buffer, cpu);
3503 EXPORT_SYMBOL_GPL(ring_buffer_reset);
3506 * rind_buffer_empty - is the ring buffer empty?
3507 * @buffer: The ring buffer to test
3509 int ring_buffer_empty(struct ring_buffer *buffer)
3511 struct ring_buffer_per_cpu *cpu_buffer;
3512 unsigned long flags;
3517 dolock = rb_ok_to_lock();
3519 /* yes this is racy, but if you don't like the race, lock the buffer */
3520 for_each_buffer_cpu(buffer, cpu) {
3521 cpu_buffer = buffer->buffers[cpu];
3522 local_irq_save(flags);
3524 spin_lock(&cpu_buffer->reader_lock);
3525 ret = rb_per_cpu_empty(cpu_buffer);
3527 spin_unlock(&cpu_buffer->reader_lock);
3528 local_irq_restore(flags);
3536 EXPORT_SYMBOL_GPL(ring_buffer_empty);
3539 * ring_buffer_empty_cpu - is a cpu buffer of a ring buffer empty?
3540 * @buffer: The ring buffer
3541 * @cpu: The CPU buffer to test
3543 int ring_buffer_empty_cpu(struct ring_buffer *buffer, int cpu)
3545 struct ring_buffer_per_cpu *cpu_buffer;
3546 unsigned long flags;
3550 if (!cpumask_test_cpu(cpu, buffer->cpumask))
3553 dolock = rb_ok_to_lock();
3555 cpu_buffer = buffer->buffers[cpu];
3556 local_irq_save(flags);
3558 spin_lock(&cpu_buffer->reader_lock);
3559 ret = rb_per_cpu_empty(cpu_buffer);
3561 spin_unlock(&cpu_buffer->reader_lock);
3562 local_irq_restore(flags);
3566 EXPORT_SYMBOL_GPL(ring_buffer_empty_cpu);
3568 #ifdef CONFIG_RING_BUFFER_ALLOW_SWAP
3570 * ring_buffer_swap_cpu - swap a CPU buffer between two ring buffers
3571 * @buffer_a: One buffer to swap with
3572 * @buffer_b: The other buffer to swap with
3574 * This function is useful for tracers that want to take a "snapshot"
3575 * of a CPU buffer and has another back up buffer lying around.
3576 * it is expected that the tracer handles the cpu buffer not being
3577 * used at the moment.
3579 int ring_buffer_swap_cpu(struct ring_buffer *buffer_a,
3580 struct ring_buffer *buffer_b, int cpu)
3582 struct ring_buffer_per_cpu *cpu_buffer_a;
3583 struct ring_buffer_per_cpu *cpu_buffer_b;
3586 if (!cpumask_test_cpu(cpu, buffer_a->cpumask) ||
3587 !cpumask_test_cpu(cpu, buffer_b->cpumask))
3590 /* At least make sure the two buffers are somewhat the same */
3591 if (buffer_a->pages != buffer_b->pages)
3596 if (ring_buffer_flags != RB_BUFFERS_ON)
3599 if (atomic_read(&buffer_a->record_disabled))
3602 if (atomic_read(&buffer_b->record_disabled))
3605 cpu_buffer_a = buffer_a->buffers[cpu];
3606 cpu_buffer_b = buffer_b->buffers[cpu];
3608 if (atomic_read(&cpu_buffer_a->record_disabled))
3611 if (atomic_read(&cpu_buffer_b->record_disabled))
3615 * We can't do a synchronize_sched here because this
3616 * function can be called in atomic context.
3617 * Normally this will be called from the same CPU as cpu.
3618 * If not it's up to the caller to protect this.
3620 atomic_inc(&cpu_buffer_a->record_disabled);
3621 atomic_inc(&cpu_buffer_b->record_disabled);
3624 if (local_read(&cpu_buffer_a->committing))
3626 if (local_read(&cpu_buffer_b->committing))
3629 buffer_a->buffers[cpu] = cpu_buffer_b;
3630 buffer_b->buffers[cpu] = cpu_buffer_a;
3632 cpu_buffer_b->buffer = buffer_a;
3633 cpu_buffer_a->buffer = buffer_b;
3638 atomic_dec(&cpu_buffer_a->record_disabled);
3639 atomic_dec(&cpu_buffer_b->record_disabled);
3643 EXPORT_SYMBOL_GPL(ring_buffer_swap_cpu);
3644 #endif /* CONFIG_RING_BUFFER_ALLOW_SWAP */
3647 * ring_buffer_alloc_read_page - allocate a page to read from buffer
3648 * @buffer: the buffer to allocate for.
3650 * This function is used in conjunction with ring_buffer_read_page.
3651 * When reading a full page from the ring buffer, these functions
3652 * can be used to speed up the process. The calling function should
3653 * allocate a few pages first with this function. Then when it
3654 * needs to get pages from the ring buffer, it passes the result
3655 * of this function into ring_buffer_read_page, which will swap
3656 * the page that was allocated, with the read page of the buffer.
3659 * The page allocated, or NULL on error.
3661 void *ring_buffer_alloc_read_page(struct ring_buffer *buffer)
3663 struct buffer_data_page *bpage;
3666 addr = __get_free_page(GFP_KERNEL);
3670 bpage = (void *)addr;
3672 rb_init_page(bpage);
3676 EXPORT_SYMBOL_GPL(ring_buffer_alloc_read_page);
3679 * ring_buffer_free_read_page - free an allocated read page
3680 * @buffer: the buffer the page was allocate for
3681 * @data: the page to free
3683 * Free a page allocated from ring_buffer_alloc_read_page.
3685 void ring_buffer_free_read_page(struct ring_buffer *buffer, void *data)
3687 free_page((unsigned long)data);
3689 EXPORT_SYMBOL_GPL(ring_buffer_free_read_page);
3692 * ring_buffer_read_page - extract a page from the ring buffer
3693 * @buffer: buffer to extract from
3694 * @data_page: the page to use allocated from ring_buffer_alloc_read_page
3695 * @len: amount to extract
3696 * @cpu: the cpu of the buffer to extract
3697 * @full: should the extraction only happen when the page is full.
3699 * This function will pull out a page from the ring buffer and consume it.
3700 * @data_page must be the address of the variable that was returned
3701 * from ring_buffer_alloc_read_page. This is because the page might be used
3702 * to swap with a page in the ring buffer.
3705 * rpage = ring_buffer_alloc_read_page(buffer);
3708 * ret = ring_buffer_read_page(buffer, &rpage, len, cpu, 0);
3710 * process_page(rpage, ret);
3712 * When @full is set, the function will not return true unless
3713 * the writer is off the reader page.
3715 * Note: it is up to the calling functions to handle sleeps and wakeups.
3716 * The ring buffer can be used anywhere in the kernel and can not
3717 * blindly call wake_up. The layer that uses the ring buffer must be
3718 * responsible for that.
3721 * >=0 if data has been transferred, returns the offset of consumed data.
3722 * <0 if no data has been transferred.
3724 int ring_buffer_read_page(struct ring_buffer *buffer,
3725 void **data_page, size_t len, int cpu, int full)
3727 struct ring_buffer_per_cpu *cpu_buffer = buffer->buffers[cpu];
3728 struct ring_buffer_event *event;
3729 struct buffer_data_page *bpage;
3730 struct buffer_page *reader;
3731 unsigned long flags;
3732 unsigned int commit;
3735 int missed_events = 0;
3738 if (!cpumask_test_cpu(cpu, buffer->cpumask))
3742 * If len is not big enough to hold the page header, then
3743 * we can not copy anything.
3745 if (len <= BUF_PAGE_HDR_SIZE)
3748 len -= BUF_PAGE_HDR_SIZE;
3757 spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
3759 reader = rb_get_reader_page(cpu_buffer);
3763 event = rb_reader_event(cpu_buffer);
3765 read = reader->read;
3766 commit = rb_page_commit(reader);
3768 /* Check if any events were dropped */
3769 if (cpu_buffer->lost_events)
3773 * If this page has been partially read or
3774 * if len is not big enough to read the rest of the page or
3775 * a writer is still on the page, then
3776 * we must copy the data from the page to the buffer.
3777 * Otherwise, we can simply swap the page with the one passed in.
3779 if (read || (len < (commit - read)) ||
3780 cpu_buffer->reader_page == cpu_buffer->commit_page) {
3781 struct buffer_data_page *rpage = cpu_buffer->reader_page->page;
3782 unsigned int rpos = read;
3783 unsigned int pos = 0;
3789 if (len > (commit - read))
3790 len = (commit - read);
3792 size = rb_event_length(event);
3797 /* save the current timestamp, since the user will need it */
3798 save_timestamp = cpu_buffer->read_stamp;
3800 /* Need to copy one event at a time */
3802 memcpy(bpage->data + pos, rpage->data + rpos, size);
3806 rb_advance_reader(cpu_buffer);
3807 rpos = reader->read;
3810 event = rb_reader_event(cpu_buffer);
3811 size = rb_event_length(event);
3812 } while (len > size);
3815 local_set(&bpage->commit, pos);
3816 bpage->time_stamp = save_timestamp;
3818 /* we copied everything to the beginning */
3821 /* update the entry counter */
3822 cpu_buffer->read += rb_page_entries(reader);
3824 /* swap the pages */
3825 rb_init_page(bpage);
3826 bpage = reader->page;
3827 reader->page = *data_page;
3828 local_set(&reader->write, 0);
3829 local_set(&reader->entries, 0);
3835 cpu_buffer->lost_events = 0;
3837 * Set a flag in the commit field if we lost events
3840 local_add(RB_MISSED_EVENTS, &bpage->commit);
3843 spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
3848 EXPORT_SYMBOL_GPL(ring_buffer_read_page);
3850 #ifdef CONFIG_TRACING
3852 rb_simple_read(struct file *filp, char __user *ubuf,
3853 size_t cnt, loff_t *ppos)
3855 unsigned long *p = filp->private_data;
3859 if (test_bit(RB_BUFFERS_DISABLED_BIT, p))
3860 r = sprintf(buf, "permanently disabled\n");
3862 r = sprintf(buf, "%d\n", test_bit(RB_BUFFERS_ON_BIT, p));
3864 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
3868 rb_simple_write(struct file *filp, const char __user *ubuf,
3869 size_t cnt, loff_t *ppos)
3871 unsigned long *p = filp->private_data;
3876 if (cnt >= sizeof(buf))
3879 if (copy_from_user(&buf, ubuf, cnt))
3884 ret = strict_strtoul(buf, 10, &val);
3889 set_bit(RB_BUFFERS_ON_BIT, p);
3891 clear_bit(RB_BUFFERS_ON_BIT, p);
3898 static const struct file_operations rb_simple_fops = {
3899 .open = tracing_open_generic,
3900 .read = rb_simple_read,
3901 .write = rb_simple_write,
3905 static __init int rb_init_debugfs(void)
3907 struct dentry *d_tracer;
3909 d_tracer = tracing_init_dentry();
3911 trace_create_file("tracing_on", 0644, d_tracer,
3912 &ring_buffer_flags, &rb_simple_fops);
3917 fs_initcall(rb_init_debugfs);
3920 #ifdef CONFIG_HOTPLUG_CPU
3921 static int rb_cpu_notify(struct notifier_block *self,
3922 unsigned long action, void *hcpu)
3924 struct ring_buffer *buffer =
3925 container_of(self, struct ring_buffer, cpu_notify);
3926 long cpu = (long)hcpu;
3929 case CPU_UP_PREPARE:
3930 case CPU_UP_PREPARE_FROZEN:
3931 if (cpumask_test_cpu(cpu, buffer->cpumask))
3934 buffer->buffers[cpu] =
3935 rb_allocate_cpu_buffer(buffer, cpu);
3936 if (!buffer->buffers[cpu]) {
3937 WARN(1, "failed to allocate ring buffer on CPU %ld\n",
3942 cpumask_set_cpu(cpu, buffer->cpumask);
3944 case CPU_DOWN_PREPARE:
3945 case CPU_DOWN_PREPARE_FROZEN:
3948 * If we were to free the buffer, then the user would
3949 * lose any trace that was in the buffer.