4 * Copyright (C) 2008 Steven Rostedt <srostedt@redhat.com>
6 #include <linux/ftrace_event.h>
7 #include <linux/ring_buffer.h>
8 #include <linux/trace_clock.h>
9 #include <linux/trace_seq.h>
10 #include <linux/spinlock.h>
11 #include <linux/debugfs.h>
12 #include <linux/uaccess.h>
13 #include <linux/hardirq.h>
14 #include <linux/kmemcheck.h>
15 #include <linux/module.h>
16 #include <linux/percpu.h>
17 #include <linux/mutex.h>
18 #include <linux/slab.h>
19 #include <linux/init.h>
20 #include <linux/hash.h>
21 #include <linux/list.h>
22 #include <linux/cpu.h>
25 #include <asm/local.h>
27 static void update_pages_handler(struct work_struct *work);
30 * The ring buffer header is special. We must manually up keep it.
32 int ring_buffer_print_entry_header(struct trace_seq *s)
36 ret = trace_seq_printf(s, "# compressed entry header\n");
37 ret = trace_seq_printf(s, "\ttype_len : 5 bits\n");
38 ret = trace_seq_printf(s, "\ttime_delta : 27 bits\n");
39 ret = trace_seq_printf(s, "\tarray : 32 bits\n");
40 ret = trace_seq_printf(s, "\n");
41 ret = trace_seq_printf(s, "\tpadding : type == %d\n",
42 RINGBUF_TYPE_PADDING);
43 ret = trace_seq_printf(s, "\ttime_extend : type == %d\n",
44 RINGBUF_TYPE_TIME_EXTEND);
45 ret = trace_seq_printf(s, "\tdata max type_len == %d\n",
46 RINGBUF_TYPE_DATA_TYPE_LEN_MAX);
52 * The ring buffer is made up of a list of pages. A separate list of pages is
53 * allocated for each CPU. A writer may only write to a buffer that is
54 * associated with the CPU it is currently executing on. A reader may read
55 * from any per cpu buffer.
57 * The reader is special. For each per cpu buffer, the reader has its own
58 * reader page. When a reader has read the entire reader page, this reader
59 * page is swapped with another page in the ring buffer.
61 * Now, as long as the writer is off the reader page, the reader can do what
62 * ever it wants with that page. The writer will never write to that page
63 * again (as long as it is out of the ring buffer).
65 * Here's some silly ASCII art.
68 * |reader| RING BUFFER
70 * +------+ +---+ +---+ +---+
79 * |reader| RING BUFFER
80 * |page |------------------v
81 * +------+ +---+ +---+ +---+
90 * |reader| RING BUFFER
91 * |page |------------------v
92 * +------+ +---+ +---+ +---+
97 * +------------------------------+
101 * |buffer| RING BUFFER
102 * |page |------------------v
103 * +------+ +---+ +---+ +---+
105 * | New +---+ +---+ +---+
108 * +------------------------------+
111 * After we make this swap, the reader can hand this page off to the splice
112 * code and be done with it. It can even allocate a new page if it needs to
113 * and swap that into the ring buffer.
115 * We will be using cmpxchg soon to make all this lockless.
120 * A fast way to enable or disable all ring buffers is to
121 * call tracing_on or tracing_off. Turning off the ring buffers
122 * prevents all ring buffers from being recorded to.
123 * Turning this switch on, makes it OK to write to the
124 * ring buffer, if the ring buffer is enabled itself.
126 * There's three layers that must be on in order to write
127 * to the ring buffer.
129 * 1) This global flag must be set.
130 * 2) The ring buffer must be enabled for recording.
131 * 3) The per cpu buffer must be enabled for recording.
133 * In case of an anomaly, this global flag has a bit set that
134 * will permantly disable all ring buffers.
138 * Global flag to disable all recording to ring buffers
139 * This has two bits: ON, DISABLED
143 * 0 0 : ring buffers are off
144 * 1 0 : ring buffers are on
145 * X 1 : ring buffers are permanently disabled
149 RB_BUFFERS_ON_BIT = 0,
150 RB_BUFFERS_DISABLED_BIT = 1,
154 RB_BUFFERS_ON = 1 << RB_BUFFERS_ON_BIT,
155 RB_BUFFERS_DISABLED = 1 << RB_BUFFERS_DISABLED_BIT,
158 static unsigned long ring_buffer_flags __read_mostly = RB_BUFFERS_ON;
160 /* Used for individual buffers (after the counter) */
161 #define RB_BUFFER_OFF (1 << 20)
163 #define BUF_PAGE_HDR_SIZE offsetof(struct buffer_data_page, data)
166 * tracing_off_permanent - permanently disable ring buffers
168 * This function, once called, will disable all ring buffers
171 void tracing_off_permanent(void)
173 set_bit(RB_BUFFERS_DISABLED_BIT, &ring_buffer_flags);
176 #define RB_EVNT_HDR_SIZE (offsetof(struct ring_buffer_event, array))
177 #define RB_ALIGNMENT 4U
178 #define RB_MAX_SMALL_DATA (RB_ALIGNMENT * RINGBUF_TYPE_DATA_TYPE_LEN_MAX)
179 #define RB_EVNT_MIN_SIZE 8U /* two 32bit words */
181 #ifndef CONFIG_HAVE_64BIT_ALIGNED_ACCESS
182 # define RB_FORCE_8BYTE_ALIGNMENT 0
183 # define RB_ARCH_ALIGNMENT RB_ALIGNMENT
185 # define RB_FORCE_8BYTE_ALIGNMENT 1
186 # define RB_ARCH_ALIGNMENT 8U
189 #define RB_ALIGN_DATA __aligned(RB_ARCH_ALIGNMENT)
191 /* define RINGBUF_TYPE_DATA for 'case RINGBUF_TYPE_DATA:' */
192 #define RINGBUF_TYPE_DATA 0 ... RINGBUF_TYPE_DATA_TYPE_LEN_MAX
195 RB_LEN_TIME_EXTEND = 8,
196 RB_LEN_TIME_STAMP = 16,
199 #define skip_time_extend(event) \
200 ((struct ring_buffer_event *)((char *)event + RB_LEN_TIME_EXTEND))
202 static inline int rb_null_event(struct ring_buffer_event *event)
204 return event->type_len == RINGBUF_TYPE_PADDING && !event->time_delta;
207 static void rb_event_set_padding(struct ring_buffer_event *event)
209 /* padding has a NULL time_delta */
210 event->type_len = RINGBUF_TYPE_PADDING;
211 event->time_delta = 0;
215 rb_event_data_length(struct ring_buffer_event *event)
220 length = event->type_len * RB_ALIGNMENT;
222 length = event->array[0];
223 return length + RB_EVNT_HDR_SIZE;
227 * Return the length of the given event. Will return
228 * the length of the time extend if the event is a
231 static inline unsigned
232 rb_event_length(struct ring_buffer_event *event)
234 switch (event->type_len) {
235 case RINGBUF_TYPE_PADDING:
236 if (rb_null_event(event))
239 return event->array[0] + RB_EVNT_HDR_SIZE;
241 case RINGBUF_TYPE_TIME_EXTEND:
242 return RB_LEN_TIME_EXTEND;
244 case RINGBUF_TYPE_TIME_STAMP:
245 return RB_LEN_TIME_STAMP;
247 case RINGBUF_TYPE_DATA:
248 return rb_event_data_length(event);
257 * Return total length of time extend and data,
258 * or just the event length for all other events.
260 static inline unsigned
261 rb_event_ts_length(struct ring_buffer_event *event)
265 if (event->type_len == RINGBUF_TYPE_TIME_EXTEND) {
266 /* time extends include the data event after it */
267 len = RB_LEN_TIME_EXTEND;
268 event = skip_time_extend(event);
270 return len + rb_event_length(event);
274 * ring_buffer_event_length - return the length of the event
275 * @event: the event to get the length of
277 * Returns the size of the data load of a data event.
278 * If the event is something other than a data event, it
279 * returns the size of the event itself. With the exception
280 * of a TIME EXTEND, where it still returns the size of the
281 * data load of the data event after it.
283 unsigned ring_buffer_event_length(struct ring_buffer_event *event)
287 if (event->type_len == RINGBUF_TYPE_TIME_EXTEND)
288 event = skip_time_extend(event);
290 length = rb_event_length(event);
291 if (event->type_len > RINGBUF_TYPE_DATA_TYPE_LEN_MAX)
293 length -= RB_EVNT_HDR_SIZE;
294 if (length > RB_MAX_SMALL_DATA + sizeof(event->array[0]))
295 length -= sizeof(event->array[0]);
298 EXPORT_SYMBOL_GPL(ring_buffer_event_length);
300 /* inline for ring buffer fast paths */
302 rb_event_data(struct ring_buffer_event *event)
304 if (event->type_len == RINGBUF_TYPE_TIME_EXTEND)
305 event = skip_time_extend(event);
306 BUG_ON(event->type_len > RINGBUF_TYPE_DATA_TYPE_LEN_MAX);
307 /* If length is in len field, then array[0] has the data */
309 return (void *)&event->array[0];
310 /* Otherwise length is in array[0] and array[1] has the data */
311 return (void *)&event->array[1];
315 * ring_buffer_event_data - return the data of the event
316 * @event: the event to get the data from
318 void *ring_buffer_event_data(struct ring_buffer_event *event)
320 return rb_event_data(event);
322 EXPORT_SYMBOL_GPL(ring_buffer_event_data);
324 #define for_each_buffer_cpu(buffer, cpu) \
325 for_each_cpu(cpu, buffer->cpumask)
328 #define TS_MASK ((1ULL << TS_SHIFT) - 1)
329 #define TS_DELTA_TEST (~TS_MASK)
331 /* Flag when events were overwritten */
332 #define RB_MISSED_EVENTS (1 << 31)
333 /* Missed count stored at end */
334 #define RB_MISSED_STORED (1 << 30)
336 struct buffer_data_page {
337 u64 time_stamp; /* page time stamp */
338 local_t commit; /* write committed index */
339 unsigned char data[] RB_ALIGN_DATA; /* data of buffer page */
343 * Note, the buffer_page list must be first. The buffer pages
344 * are allocated in cache lines, which means that each buffer
345 * page will be at the beginning of a cache line, and thus
346 * the least significant bits will be zero. We use this to
347 * add flags in the list struct pointers, to make the ring buffer
351 struct list_head list; /* list of buffer pages */
352 local_t write; /* index for next write */
353 unsigned read; /* index for next read */
354 local_t entries; /* entries on this page */
355 unsigned long real_end; /* real end of data */
356 struct buffer_data_page *page; /* Actual data page */
360 * The buffer page counters, write and entries, must be reset
361 * atomically when crossing page boundaries. To synchronize this
362 * update, two counters are inserted into the number. One is
363 * the actual counter for the write position or count on the page.
365 * The other is a counter of updaters. Before an update happens
366 * the update partition of the counter is incremented. This will
367 * allow the updater to update the counter atomically.
369 * The counter is 20 bits, and the state data is 12.
371 #define RB_WRITE_MASK 0xfffff
372 #define RB_WRITE_INTCNT (1 << 20)
374 static void rb_init_page(struct buffer_data_page *bpage)
376 local_set(&bpage->commit, 0);
380 * ring_buffer_page_len - the size of data on the page.
381 * @page: The page to read
383 * Returns the amount of data on the page, including buffer page header.
385 size_t ring_buffer_page_len(void *page)
387 return local_read(&((struct buffer_data_page *)page)->commit)
392 * Also stolen from mm/slob.c. Thanks to Mathieu Desnoyers for pointing
395 static void free_buffer_page(struct buffer_page *bpage)
397 free_page((unsigned long)bpage->page);
402 * We need to fit the time_stamp delta into 27 bits.
404 static inline int test_time_stamp(u64 delta)
406 if (delta & TS_DELTA_TEST)
411 #define BUF_PAGE_SIZE (PAGE_SIZE - BUF_PAGE_HDR_SIZE)
413 /* Max payload is BUF_PAGE_SIZE - header (8bytes) */
414 #define BUF_MAX_DATA_SIZE (BUF_PAGE_SIZE - (sizeof(u32) * 2))
416 int ring_buffer_print_page_header(struct trace_seq *s)
418 struct buffer_data_page field;
421 ret = trace_seq_printf(s, "\tfield: u64 timestamp;\t"
422 "offset:0;\tsize:%u;\tsigned:%u;\n",
423 (unsigned int)sizeof(field.time_stamp),
424 (unsigned int)is_signed_type(u64));
426 ret = trace_seq_printf(s, "\tfield: local_t commit;\t"
427 "offset:%u;\tsize:%u;\tsigned:%u;\n",
428 (unsigned int)offsetof(typeof(field), commit),
429 (unsigned int)sizeof(field.commit),
430 (unsigned int)is_signed_type(long));
432 ret = trace_seq_printf(s, "\tfield: int overwrite;\t"
433 "offset:%u;\tsize:%u;\tsigned:%u;\n",
434 (unsigned int)offsetof(typeof(field), commit),
436 (unsigned int)is_signed_type(long));
438 ret = trace_seq_printf(s, "\tfield: char data;\t"
439 "offset:%u;\tsize:%u;\tsigned:%u;\n",
440 (unsigned int)offsetof(typeof(field), data),
441 (unsigned int)BUF_PAGE_SIZE,
442 (unsigned int)is_signed_type(char));
448 * head_page == tail_page && head == tail then buffer is empty.
450 struct ring_buffer_per_cpu {
452 atomic_t record_disabled;
453 struct ring_buffer *buffer;
454 raw_spinlock_t reader_lock; /* serialize readers */
455 arch_spinlock_t lock;
456 struct lock_class_key lock_key;
457 unsigned int nr_pages;
458 struct list_head *pages;
459 struct buffer_page *head_page; /* read from head */
460 struct buffer_page *tail_page; /* write to tail */
461 struct buffer_page *commit_page; /* committed pages */
462 struct buffer_page *reader_page;
463 unsigned long lost_events;
464 unsigned long last_overrun;
465 local_t entries_bytes;
468 local_t commit_overrun;
469 local_t dropped_events;
473 unsigned long read_bytes;
476 /* ring buffer pages to update, > 0 to add, < 0 to remove */
477 int nr_pages_to_update;
478 struct list_head new_pages; /* new pages to add */
479 struct work_struct update_pages_work;
480 struct completion update_done;
486 atomic_t record_disabled;
487 atomic_t resize_disabled;
488 cpumask_var_t cpumask;
490 struct lock_class_key *reader_lock_key;
494 struct ring_buffer_per_cpu **buffers;
496 #ifdef CONFIG_HOTPLUG_CPU
497 struct notifier_block cpu_notify;
502 struct ring_buffer_iter {
503 struct ring_buffer_per_cpu *cpu_buffer;
505 struct buffer_page *head_page;
506 struct buffer_page *cache_reader_page;
507 unsigned long cache_read;
511 /* buffer may be either ring_buffer or ring_buffer_per_cpu */
512 #define RB_WARN_ON(b, cond) \
514 int _____ret = unlikely(cond); \
516 if (__same_type(*(b), struct ring_buffer_per_cpu)) { \
517 struct ring_buffer_per_cpu *__b = \
519 atomic_inc(&__b->buffer->record_disabled); \
521 atomic_inc(&b->record_disabled); \
527 /* Up this if you want to test the TIME_EXTENTS and normalization */
528 #define DEBUG_SHIFT 0
530 static inline u64 rb_time_stamp(struct ring_buffer *buffer)
532 /* shift to debug/test normalization and TIME_EXTENTS */
533 return buffer->clock() << DEBUG_SHIFT;
536 u64 ring_buffer_time_stamp(struct ring_buffer *buffer, int cpu)
540 preempt_disable_notrace();
541 time = rb_time_stamp(buffer);
542 preempt_enable_no_resched_notrace();
546 EXPORT_SYMBOL_GPL(ring_buffer_time_stamp);
548 void ring_buffer_normalize_time_stamp(struct ring_buffer *buffer,
551 /* Just stupid testing the normalize function and deltas */
554 EXPORT_SYMBOL_GPL(ring_buffer_normalize_time_stamp);
557 * Making the ring buffer lockless makes things tricky.
558 * Although writes only happen on the CPU that they are on,
559 * and they only need to worry about interrupts. Reads can
562 * The reader page is always off the ring buffer, but when the
563 * reader finishes with a page, it needs to swap its page with
564 * a new one from the buffer. The reader needs to take from
565 * the head (writes go to the tail). But if a writer is in overwrite
566 * mode and wraps, it must push the head page forward.
568 * Here lies the problem.
570 * The reader must be careful to replace only the head page, and
571 * not another one. As described at the top of the file in the
572 * ASCII art, the reader sets its old page to point to the next
573 * page after head. It then sets the page after head to point to
574 * the old reader page. But if the writer moves the head page
575 * during this operation, the reader could end up with the tail.
577 * We use cmpxchg to help prevent this race. We also do something
578 * special with the page before head. We set the LSB to 1.
580 * When the writer must push the page forward, it will clear the
581 * bit that points to the head page, move the head, and then set
582 * the bit that points to the new head page.
584 * We also don't want an interrupt coming in and moving the head
585 * page on another writer. Thus we use the second LSB to catch
588 * head->list->prev->next bit 1 bit 0
591 * Points to head page 0 1
594 * Note we can not trust the prev pointer of the head page, because:
596 * +----+ +-----+ +-----+
597 * | |------>| T |---X--->| N |
599 * +----+ +-----+ +-----+
602 * +----------| R |----------+ |
606 * Key: ---X--> HEAD flag set in pointer
611 * (see __rb_reserve_next() to see where this happens)
613 * What the above shows is that the reader just swapped out
614 * the reader page with a page in the buffer, but before it
615 * could make the new header point back to the new page added
616 * it was preempted by a writer. The writer moved forward onto
617 * the new page added by the reader and is about to move forward
620 * You can see, it is legitimate for the previous pointer of
621 * the head (or any page) not to point back to itself. But only
625 #define RB_PAGE_NORMAL 0UL
626 #define RB_PAGE_HEAD 1UL
627 #define RB_PAGE_UPDATE 2UL
630 #define RB_FLAG_MASK 3UL
632 /* PAGE_MOVED is not part of the mask */
633 #define RB_PAGE_MOVED 4UL
636 * rb_list_head - remove any bit
638 static struct list_head *rb_list_head(struct list_head *list)
640 unsigned long val = (unsigned long)list;
642 return (struct list_head *)(val & ~RB_FLAG_MASK);
646 * rb_is_head_page - test if the given page is the head page
648 * Because the reader may move the head_page pointer, we can
649 * not trust what the head page is (it may be pointing to
650 * the reader page). But if the next page is a header page,
651 * its flags will be non zero.
654 rb_is_head_page(struct ring_buffer_per_cpu *cpu_buffer,
655 struct buffer_page *page, struct list_head *list)
659 val = (unsigned long)list->next;
661 if ((val & ~RB_FLAG_MASK) != (unsigned long)&page->list)
662 return RB_PAGE_MOVED;
664 return val & RB_FLAG_MASK;
670 * The unique thing about the reader page, is that, if the
671 * writer is ever on it, the previous pointer never points
672 * back to the reader page.
674 static int rb_is_reader_page(struct buffer_page *page)
676 struct list_head *list = page->list.prev;
678 return rb_list_head(list->next) != &page->list;
682 * rb_set_list_to_head - set a list_head to be pointing to head.
684 static void rb_set_list_to_head(struct ring_buffer_per_cpu *cpu_buffer,
685 struct list_head *list)
689 ptr = (unsigned long *)&list->next;
690 *ptr |= RB_PAGE_HEAD;
691 *ptr &= ~RB_PAGE_UPDATE;
695 * rb_head_page_activate - sets up head page
697 static void rb_head_page_activate(struct ring_buffer_per_cpu *cpu_buffer)
699 struct buffer_page *head;
701 head = cpu_buffer->head_page;
706 * Set the previous list pointer to have the HEAD flag.
708 rb_set_list_to_head(cpu_buffer, head->list.prev);
711 static void rb_list_head_clear(struct list_head *list)
713 unsigned long *ptr = (unsigned long *)&list->next;
715 *ptr &= ~RB_FLAG_MASK;
719 * rb_head_page_dactivate - clears head page ptr (for free list)
722 rb_head_page_deactivate(struct ring_buffer_per_cpu *cpu_buffer)
724 struct list_head *hd;
726 /* Go through the whole list and clear any pointers found. */
727 rb_list_head_clear(cpu_buffer->pages);
729 list_for_each(hd, cpu_buffer->pages)
730 rb_list_head_clear(hd);
733 static int rb_head_page_set(struct ring_buffer_per_cpu *cpu_buffer,
734 struct buffer_page *head,
735 struct buffer_page *prev,
736 int old_flag, int new_flag)
738 struct list_head *list;
739 unsigned long val = (unsigned long)&head->list;
744 val &= ~RB_FLAG_MASK;
746 ret = cmpxchg((unsigned long *)&list->next,
747 val | old_flag, val | new_flag);
749 /* check if the reader took the page */
750 if ((ret & ~RB_FLAG_MASK) != val)
751 return RB_PAGE_MOVED;
753 return ret & RB_FLAG_MASK;
756 static int rb_head_page_set_update(struct ring_buffer_per_cpu *cpu_buffer,
757 struct buffer_page *head,
758 struct buffer_page *prev,
761 return rb_head_page_set(cpu_buffer, head, prev,
762 old_flag, RB_PAGE_UPDATE);
765 static int rb_head_page_set_head(struct ring_buffer_per_cpu *cpu_buffer,
766 struct buffer_page *head,
767 struct buffer_page *prev,
770 return rb_head_page_set(cpu_buffer, head, prev,
771 old_flag, RB_PAGE_HEAD);
774 static int rb_head_page_set_normal(struct ring_buffer_per_cpu *cpu_buffer,
775 struct buffer_page *head,
776 struct buffer_page *prev,
779 return rb_head_page_set(cpu_buffer, head, prev,
780 old_flag, RB_PAGE_NORMAL);
783 static inline void rb_inc_page(struct ring_buffer_per_cpu *cpu_buffer,
784 struct buffer_page **bpage)
786 struct list_head *p = rb_list_head((*bpage)->list.next);
788 *bpage = list_entry(p, struct buffer_page, list);
791 static struct buffer_page *
792 rb_set_head_page(struct ring_buffer_per_cpu *cpu_buffer)
794 struct buffer_page *head;
795 struct buffer_page *page;
796 struct list_head *list;
799 if (RB_WARN_ON(cpu_buffer, !cpu_buffer->head_page))
803 list = cpu_buffer->pages;
804 if (RB_WARN_ON(cpu_buffer, rb_list_head(list->prev->next) != list))
807 page = head = cpu_buffer->head_page;
809 * It is possible that the writer moves the header behind
810 * where we started, and we miss in one loop.
811 * A second loop should grab the header, but we'll do
812 * three loops just because I'm paranoid.
814 for (i = 0; i < 3; i++) {
816 if (rb_is_head_page(cpu_buffer, page, page->list.prev)) {
817 cpu_buffer->head_page = page;
820 rb_inc_page(cpu_buffer, &page);
821 } while (page != head);
824 RB_WARN_ON(cpu_buffer, 1);
829 static int rb_head_page_replace(struct buffer_page *old,
830 struct buffer_page *new)
832 unsigned long *ptr = (unsigned long *)&old->list.prev->next;
836 val = *ptr & ~RB_FLAG_MASK;
839 ret = cmpxchg(ptr, val, (unsigned long)&new->list);
845 * rb_tail_page_update - move the tail page forward
847 * Returns 1 if moved tail page, 0 if someone else did.
849 static int rb_tail_page_update(struct ring_buffer_per_cpu *cpu_buffer,
850 struct buffer_page *tail_page,
851 struct buffer_page *next_page)
853 struct buffer_page *old_tail;
854 unsigned long old_entries;
855 unsigned long old_write;
859 * The tail page now needs to be moved forward.
861 * We need to reset the tail page, but without messing
862 * with possible erasing of data brought in by interrupts
863 * that have moved the tail page and are currently on it.
865 * We add a counter to the write field to denote this.
867 old_write = local_add_return(RB_WRITE_INTCNT, &next_page->write);
868 old_entries = local_add_return(RB_WRITE_INTCNT, &next_page->entries);
871 * Just make sure we have seen our old_write and synchronize
872 * with any interrupts that come in.
877 * If the tail page is still the same as what we think
878 * it is, then it is up to us to update the tail
881 if (tail_page == cpu_buffer->tail_page) {
882 /* Zero the write counter */
883 unsigned long val = old_write & ~RB_WRITE_MASK;
884 unsigned long eval = old_entries & ~RB_WRITE_MASK;
887 * This will only succeed if an interrupt did
888 * not come in and change it. In which case, we
889 * do not want to modify it.
891 * We add (void) to let the compiler know that we do not care
892 * about the return value of these functions. We use the
893 * cmpxchg to only update if an interrupt did not already
894 * do it for us. If the cmpxchg fails, we don't care.
896 (void)local_cmpxchg(&next_page->write, old_write, val);
897 (void)local_cmpxchg(&next_page->entries, old_entries, eval);
900 * No need to worry about races with clearing out the commit.
901 * it only can increment when a commit takes place. But that
902 * only happens in the outer most nested commit.
904 local_set(&next_page->page->commit, 0);
906 old_tail = cmpxchg(&cpu_buffer->tail_page,
907 tail_page, next_page);
909 if (old_tail == tail_page)
916 static int rb_check_bpage(struct ring_buffer_per_cpu *cpu_buffer,
917 struct buffer_page *bpage)
919 unsigned long val = (unsigned long)bpage;
921 if (RB_WARN_ON(cpu_buffer, val & RB_FLAG_MASK))
928 * rb_check_list - make sure a pointer to a list has the last bits zero
930 static int rb_check_list(struct ring_buffer_per_cpu *cpu_buffer,
931 struct list_head *list)
933 if (RB_WARN_ON(cpu_buffer, rb_list_head(list->prev) != list->prev))
935 if (RB_WARN_ON(cpu_buffer, rb_list_head(list->next) != list->next))
941 * check_pages - integrity check of buffer pages
942 * @cpu_buffer: CPU buffer with pages to test
944 * As a safety measure we check to make sure the data pages have not
947 static int rb_check_pages(struct ring_buffer_per_cpu *cpu_buffer)
949 struct list_head *head = cpu_buffer->pages;
950 struct buffer_page *bpage, *tmp;
952 /* Reset the head page if it exists */
953 if (cpu_buffer->head_page)
954 rb_set_head_page(cpu_buffer);
956 rb_head_page_deactivate(cpu_buffer);
958 if (RB_WARN_ON(cpu_buffer, head->next->prev != head))
960 if (RB_WARN_ON(cpu_buffer, head->prev->next != head))
963 if (rb_check_list(cpu_buffer, head))
966 list_for_each_entry_safe(bpage, tmp, head, list) {
967 if (RB_WARN_ON(cpu_buffer,
968 bpage->list.next->prev != &bpage->list))
970 if (RB_WARN_ON(cpu_buffer,
971 bpage->list.prev->next != &bpage->list))
973 if (rb_check_list(cpu_buffer, &bpage->list))
977 rb_head_page_activate(cpu_buffer);
982 static int __rb_allocate_pages(int nr_pages, struct list_head *pages, int cpu)
985 struct buffer_page *bpage, *tmp;
987 for (i = 0; i < nr_pages; i++) {
990 * __GFP_NORETRY flag makes sure that the allocation fails
991 * gracefully without invoking oom-killer and the system is
994 bpage = kzalloc_node(ALIGN(sizeof(*bpage), cache_line_size()),
995 GFP_KERNEL | __GFP_NORETRY,
1000 list_add(&bpage->list, pages);
1002 page = alloc_pages_node(cpu_to_node(cpu),
1003 GFP_KERNEL | __GFP_NORETRY, 0);
1006 bpage->page = page_address(page);
1007 rb_init_page(bpage->page);
1013 list_for_each_entry_safe(bpage, tmp, pages, list) {
1014 list_del_init(&bpage->list);
1015 free_buffer_page(bpage);
1021 static int rb_allocate_pages(struct ring_buffer_per_cpu *cpu_buffer,
1028 if (__rb_allocate_pages(nr_pages, &pages, cpu_buffer->cpu))
1032 * The ring buffer page list is a circular list that does not
1033 * start and end with a list head. All page list items point to
1036 cpu_buffer->pages = pages.next;
1039 cpu_buffer->nr_pages = nr_pages;
1041 rb_check_pages(cpu_buffer);
1046 static struct ring_buffer_per_cpu *
1047 rb_allocate_cpu_buffer(struct ring_buffer *buffer, int nr_pages, int cpu)
1049 struct ring_buffer_per_cpu *cpu_buffer;
1050 struct buffer_page *bpage;
1054 cpu_buffer = kzalloc_node(ALIGN(sizeof(*cpu_buffer), cache_line_size()),
1055 GFP_KERNEL, cpu_to_node(cpu));
1059 cpu_buffer->cpu = cpu;
1060 cpu_buffer->buffer = buffer;
1061 raw_spin_lock_init(&cpu_buffer->reader_lock);
1062 lockdep_set_class(&cpu_buffer->reader_lock, buffer->reader_lock_key);
1063 cpu_buffer->lock = (arch_spinlock_t)__ARCH_SPIN_LOCK_UNLOCKED;
1064 INIT_WORK(&cpu_buffer->update_pages_work, update_pages_handler);
1065 init_completion(&cpu_buffer->update_done);
1067 bpage = kzalloc_node(ALIGN(sizeof(*bpage), cache_line_size()),
1068 GFP_KERNEL, cpu_to_node(cpu));
1070 goto fail_free_buffer;
1072 rb_check_bpage(cpu_buffer, bpage);
1074 cpu_buffer->reader_page = bpage;
1075 page = alloc_pages_node(cpu_to_node(cpu), GFP_KERNEL, 0);
1077 goto fail_free_reader;
1078 bpage->page = page_address(page);
1079 rb_init_page(bpage->page);
1081 INIT_LIST_HEAD(&cpu_buffer->reader_page->list);
1082 INIT_LIST_HEAD(&cpu_buffer->new_pages);
1084 ret = rb_allocate_pages(cpu_buffer, nr_pages);
1086 goto fail_free_reader;
1088 cpu_buffer->head_page
1089 = list_entry(cpu_buffer->pages, struct buffer_page, list);
1090 cpu_buffer->tail_page = cpu_buffer->commit_page = cpu_buffer->head_page;
1092 rb_head_page_activate(cpu_buffer);
1097 free_buffer_page(cpu_buffer->reader_page);
1104 static void rb_free_cpu_buffer(struct ring_buffer_per_cpu *cpu_buffer)
1106 struct list_head *head = cpu_buffer->pages;
1107 struct buffer_page *bpage, *tmp;
1109 free_buffer_page(cpu_buffer->reader_page);
1111 rb_head_page_deactivate(cpu_buffer);
1114 list_for_each_entry_safe(bpage, tmp, head, list) {
1115 list_del_init(&bpage->list);
1116 free_buffer_page(bpage);
1118 bpage = list_entry(head, struct buffer_page, list);
1119 free_buffer_page(bpage);
1125 #ifdef CONFIG_HOTPLUG_CPU
1126 static int rb_cpu_notify(struct notifier_block *self,
1127 unsigned long action, void *hcpu);
1131 * ring_buffer_alloc - allocate a new ring_buffer
1132 * @size: the size in bytes per cpu that is needed.
1133 * @flags: attributes to set for the ring buffer.
1135 * Currently the only flag that is available is the RB_FL_OVERWRITE
1136 * flag. This flag means that the buffer will overwrite old data
1137 * when the buffer wraps. If this flag is not set, the buffer will
1138 * drop data when the tail hits the head.
1140 struct ring_buffer *__ring_buffer_alloc(unsigned long size, unsigned flags,
1141 struct lock_class_key *key)
1143 struct ring_buffer *buffer;
1147 /* keep it in its own cache line */
1148 buffer = kzalloc(ALIGN(sizeof(*buffer), cache_line_size()),
1153 if (!alloc_cpumask_var(&buffer->cpumask, GFP_KERNEL))
1154 goto fail_free_buffer;
1156 nr_pages = DIV_ROUND_UP(size, BUF_PAGE_SIZE);
1157 buffer->flags = flags;
1158 buffer->clock = trace_clock_local;
1159 buffer->reader_lock_key = key;
1161 /* need at least two pages */
1166 * In case of non-hotplug cpu, if the ring-buffer is allocated
1167 * in early initcall, it will not be notified of secondary cpus.
1168 * In that off case, we need to allocate for all possible cpus.
1170 #ifdef CONFIG_HOTPLUG_CPU
1172 cpumask_copy(buffer->cpumask, cpu_online_mask);
1174 cpumask_copy(buffer->cpumask, cpu_possible_mask);
1176 buffer->cpus = nr_cpu_ids;
1178 bsize = sizeof(void *) * nr_cpu_ids;
1179 buffer->buffers = kzalloc(ALIGN(bsize, cache_line_size()),
1181 if (!buffer->buffers)
1182 goto fail_free_cpumask;
1184 for_each_buffer_cpu(buffer, cpu) {
1185 buffer->buffers[cpu] =
1186 rb_allocate_cpu_buffer(buffer, nr_pages, cpu);
1187 if (!buffer->buffers[cpu])
1188 goto fail_free_buffers;
1191 #ifdef CONFIG_HOTPLUG_CPU
1192 buffer->cpu_notify.notifier_call = rb_cpu_notify;
1193 buffer->cpu_notify.priority = 0;
1194 register_cpu_notifier(&buffer->cpu_notify);
1198 mutex_init(&buffer->mutex);
1203 for_each_buffer_cpu(buffer, cpu) {
1204 if (buffer->buffers[cpu])
1205 rb_free_cpu_buffer(buffer->buffers[cpu]);
1207 kfree(buffer->buffers);
1210 free_cpumask_var(buffer->cpumask);
1217 EXPORT_SYMBOL_GPL(__ring_buffer_alloc);
1220 * ring_buffer_free - free a ring buffer.
1221 * @buffer: the buffer to free.
1224 ring_buffer_free(struct ring_buffer *buffer)
1230 #ifdef CONFIG_HOTPLUG_CPU
1231 unregister_cpu_notifier(&buffer->cpu_notify);
1234 for_each_buffer_cpu(buffer, cpu)
1235 rb_free_cpu_buffer(buffer->buffers[cpu]);
1239 kfree(buffer->buffers);
1240 free_cpumask_var(buffer->cpumask);
1244 EXPORT_SYMBOL_GPL(ring_buffer_free);
1246 void ring_buffer_set_clock(struct ring_buffer *buffer,
1249 buffer->clock = clock;
1252 static void rb_reset_cpu(struct ring_buffer_per_cpu *cpu_buffer);
1254 static inline unsigned long rb_page_entries(struct buffer_page *bpage)
1256 return local_read(&bpage->entries) & RB_WRITE_MASK;
1259 static inline unsigned long rb_page_write(struct buffer_page *bpage)
1261 return local_read(&bpage->write) & RB_WRITE_MASK;
1265 rb_remove_pages(struct ring_buffer_per_cpu *cpu_buffer, unsigned int nr_pages)
1267 struct list_head *tail_page, *to_remove, *next_page;
1268 struct buffer_page *to_remove_page, *tmp_iter_page;
1269 struct buffer_page *last_page, *first_page;
1270 unsigned int nr_removed;
1271 unsigned long head_bit;
1276 raw_spin_lock_irq(&cpu_buffer->reader_lock);
1277 atomic_inc(&cpu_buffer->record_disabled);
1279 * We don't race with the readers since we have acquired the reader
1280 * lock. We also don't race with writers after disabling recording.
1281 * This makes it easy to figure out the first and the last page to be
1282 * removed from the list. We unlink all the pages in between including
1283 * the first and last pages. This is done in a busy loop so that we
1284 * lose the least number of traces.
1285 * The pages are freed after we restart recording and unlock readers.
1287 tail_page = &cpu_buffer->tail_page->list;
1290 * tail page might be on reader page, we remove the next page
1291 * from the ring buffer
1293 if (cpu_buffer->tail_page == cpu_buffer->reader_page)
1294 tail_page = rb_list_head(tail_page->next);
1295 to_remove = tail_page;
1297 /* start of pages to remove */
1298 first_page = list_entry(rb_list_head(to_remove->next),
1299 struct buffer_page, list);
1301 for (nr_removed = 0; nr_removed < nr_pages; nr_removed++) {
1302 to_remove = rb_list_head(to_remove)->next;
1303 head_bit |= (unsigned long)to_remove & RB_PAGE_HEAD;
1306 next_page = rb_list_head(to_remove)->next;
1309 * Now we remove all pages between tail_page and next_page.
1310 * Make sure that we have head_bit value preserved for the
1313 tail_page->next = (struct list_head *)((unsigned long)next_page |
1315 next_page = rb_list_head(next_page);
1316 next_page->prev = tail_page;
1318 /* make sure pages points to a valid page in the ring buffer */
1319 cpu_buffer->pages = next_page;
1321 /* update head page */
1323 cpu_buffer->head_page = list_entry(next_page,
1324 struct buffer_page, list);
1327 * change read pointer to make sure any read iterators reset
1330 cpu_buffer->read = 0;
1332 /* pages are removed, resume tracing and then free the pages */
1333 atomic_dec(&cpu_buffer->record_disabled);
1334 raw_spin_unlock_irq(&cpu_buffer->reader_lock);
1336 RB_WARN_ON(cpu_buffer, list_empty(cpu_buffer->pages));
1338 /* last buffer page to remove */
1339 last_page = list_entry(rb_list_head(to_remove), struct buffer_page,
1341 tmp_iter_page = first_page;
1344 to_remove_page = tmp_iter_page;
1345 rb_inc_page(cpu_buffer, &tmp_iter_page);
1347 /* update the counters */
1348 page_entries = rb_page_entries(to_remove_page);
1351 * If something was added to this page, it was full
1352 * since it is not the tail page. So we deduct the
1353 * bytes consumed in ring buffer from here.
1354 * Increment overrun to account for the lost events.
1356 local_add(page_entries, &cpu_buffer->overrun);
1357 local_sub(BUF_PAGE_SIZE, &cpu_buffer->entries_bytes);
1361 * We have already removed references to this list item, just
1362 * free up the buffer_page and its page
1364 free_buffer_page(to_remove_page);
1367 } while (to_remove_page != last_page);
1369 RB_WARN_ON(cpu_buffer, nr_removed);
1371 return nr_removed == 0;
1375 rb_insert_pages(struct ring_buffer_per_cpu *cpu_buffer)
1377 struct list_head *pages = &cpu_buffer->new_pages;
1378 int retries, success;
1380 raw_spin_lock_irq(&cpu_buffer->reader_lock);
1382 * We are holding the reader lock, so the reader page won't be swapped
1383 * in the ring buffer. Now we are racing with the writer trying to
1384 * move head page and the tail page.
1385 * We are going to adapt the reader page update process where:
1386 * 1. We first splice the start and end of list of new pages between
1387 * the head page and its previous page.
1388 * 2. We cmpxchg the prev_page->next to point from head page to the
1389 * start of new pages list.
1390 * 3. Finally, we update the head->prev to the end of new list.
1392 * We will try this process 10 times, to make sure that we don't keep
1398 struct list_head *head_page, *prev_page, *r;
1399 struct list_head *last_page, *first_page;
1400 struct list_head *head_page_with_bit;
1402 head_page = &rb_set_head_page(cpu_buffer)->list;
1405 prev_page = head_page->prev;
1407 first_page = pages->next;
1408 last_page = pages->prev;
1410 head_page_with_bit = (struct list_head *)
1411 ((unsigned long)head_page | RB_PAGE_HEAD);
1413 last_page->next = head_page_with_bit;
1414 first_page->prev = prev_page;
1416 r = cmpxchg(&prev_page->next, head_page_with_bit, first_page);
1418 if (r == head_page_with_bit) {
1420 * yay, we replaced the page pointer to our new list,
1421 * now, we just have to update to head page's prev
1422 * pointer to point to end of list
1424 head_page->prev = last_page;
1431 INIT_LIST_HEAD(pages);
1433 * If we weren't successful in adding in new pages, warn and stop
1436 RB_WARN_ON(cpu_buffer, !success);
1437 raw_spin_unlock_irq(&cpu_buffer->reader_lock);
1439 /* free pages if they weren't inserted */
1441 struct buffer_page *bpage, *tmp;
1442 list_for_each_entry_safe(bpage, tmp, &cpu_buffer->new_pages,
1444 list_del_init(&bpage->list);
1445 free_buffer_page(bpage);
1451 static void rb_update_pages(struct ring_buffer_per_cpu *cpu_buffer)
1455 if (cpu_buffer->nr_pages_to_update > 0)
1456 success = rb_insert_pages(cpu_buffer);
1458 success = rb_remove_pages(cpu_buffer,
1459 -cpu_buffer->nr_pages_to_update);
1462 cpu_buffer->nr_pages += cpu_buffer->nr_pages_to_update;
1465 static void update_pages_handler(struct work_struct *work)
1467 struct ring_buffer_per_cpu *cpu_buffer = container_of(work,
1468 struct ring_buffer_per_cpu, update_pages_work);
1469 rb_update_pages(cpu_buffer);
1470 complete(&cpu_buffer->update_done);
1474 * ring_buffer_resize - resize the ring buffer
1475 * @buffer: the buffer to resize.
1476 * @size: the new size.
1478 * Minimum size is 2 * BUF_PAGE_SIZE.
1480 * Returns 0 on success and < 0 on failure.
1482 int ring_buffer_resize(struct ring_buffer *buffer, unsigned long size,
1485 struct ring_buffer_per_cpu *cpu_buffer;
1490 * Always succeed at resizing a non-existent buffer:
1495 /* Make sure the requested buffer exists */
1496 if (cpu_id != RING_BUFFER_ALL_CPUS &&
1497 !cpumask_test_cpu(cpu_id, buffer->cpumask))
1500 size = DIV_ROUND_UP(size, BUF_PAGE_SIZE);
1501 size *= BUF_PAGE_SIZE;
1503 /* we need a minimum of two pages */
1504 if (size < BUF_PAGE_SIZE * 2)
1505 size = BUF_PAGE_SIZE * 2;
1507 nr_pages = DIV_ROUND_UP(size, BUF_PAGE_SIZE);
1510 * Don't succeed if resizing is disabled, as a reader might be
1511 * manipulating the ring buffer and is expecting a sane state while
1514 if (atomic_read(&buffer->resize_disabled))
1517 /* prevent another thread from changing buffer sizes */
1518 mutex_lock(&buffer->mutex);
1520 if (cpu_id == RING_BUFFER_ALL_CPUS) {
1521 /* calculate the pages to update */
1522 for_each_buffer_cpu(buffer, cpu) {
1523 cpu_buffer = buffer->buffers[cpu];
1525 cpu_buffer->nr_pages_to_update = nr_pages -
1526 cpu_buffer->nr_pages;
1528 * nothing more to do for removing pages or no update
1530 if (cpu_buffer->nr_pages_to_update <= 0)
1533 * to add pages, make sure all new pages can be
1534 * allocated without receiving ENOMEM
1536 INIT_LIST_HEAD(&cpu_buffer->new_pages);
1537 if (__rb_allocate_pages(cpu_buffer->nr_pages_to_update,
1538 &cpu_buffer->new_pages, cpu)) {
1539 /* not enough memory for new pages */
1547 * Fire off all the required work handlers
1548 * We can't schedule on offline CPUs, but it's not necessary
1549 * since we can change their buffer sizes without any race.
1551 for_each_buffer_cpu(buffer, cpu) {
1552 cpu_buffer = buffer->buffers[cpu];
1553 if (!cpu_buffer->nr_pages_to_update)
1556 if (cpu_online(cpu))
1557 schedule_work_on(cpu,
1558 &cpu_buffer->update_pages_work);
1560 rb_update_pages(cpu_buffer);
1563 /* wait for all the updates to complete */
1564 for_each_buffer_cpu(buffer, cpu) {
1565 cpu_buffer = buffer->buffers[cpu];
1566 if (!cpu_buffer->nr_pages_to_update)
1569 if (cpu_online(cpu))
1570 wait_for_completion(&cpu_buffer->update_done);
1571 cpu_buffer->nr_pages_to_update = 0;
1576 /* Make sure this CPU has been intitialized */
1577 if (!cpumask_test_cpu(cpu_id, buffer->cpumask))
1580 cpu_buffer = buffer->buffers[cpu_id];
1582 if (nr_pages == cpu_buffer->nr_pages)
1585 cpu_buffer->nr_pages_to_update = nr_pages -
1586 cpu_buffer->nr_pages;
1588 INIT_LIST_HEAD(&cpu_buffer->new_pages);
1589 if (cpu_buffer->nr_pages_to_update > 0 &&
1590 __rb_allocate_pages(cpu_buffer->nr_pages_to_update,
1591 &cpu_buffer->new_pages, cpu_id)) {
1598 if (cpu_online(cpu_id)) {
1599 schedule_work_on(cpu_id,
1600 &cpu_buffer->update_pages_work);
1601 wait_for_completion(&cpu_buffer->update_done);
1603 rb_update_pages(cpu_buffer);
1605 cpu_buffer->nr_pages_to_update = 0;
1611 * The ring buffer resize can happen with the ring buffer
1612 * enabled, so that the update disturbs the tracing as little
1613 * as possible. But if the buffer is disabled, we do not need
1614 * to worry about that, and we can take the time to verify
1615 * that the buffer is not corrupt.
1617 if (atomic_read(&buffer->record_disabled)) {
1618 atomic_inc(&buffer->record_disabled);
1620 * Even though the buffer was disabled, we must make sure
1621 * that it is truly disabled before calling rb_check_pages.
1622 * There could have been a race between checking
1623 * record_disable and incrementing it.
1625 synchronize_sched();
1626 for_each_buffer_cpu(buffer, cpu) {
1627 cpu_buffer = buffer->buffers[cpu];
1628 rb_check_pages(cpu_buffer);
1630 atomic_dec(&buffer->record_disabled);
1633 mutex_unlock(&buffer->mutex);
1637 for_each_buffer_cpu(buffer, cpu) {
1638 struct buffer_page *bpage, *tmp;
1640 cpu_buffer = buffer->buffers[cpu];
1641 cpu_buffer->nr_pages_to_update = 0;
1643 if (list_empty(&cpu_buffer->new_pages))
1646 list_for_each_entry_safe(bpage, tmp, &cpu_buffer->new_pages,
1648 list_del_init(&bpage->list);
1649 free_buffer_page(bpage);
1652 mutex_unlock(&buffer->mutex);
1655 EXPORT_SYMBOL_GPL(ring_buffer_resize);
1657 void ring_buffer_change_overwrite(struct ring_buffer *buffer, int val)
1659 mutex_lock(&buffer->mutex);
1661 buffer->flags |= RB_FL_OVERWRITE;
1663 buffer->flags &= ~RB_FL_OVERWRITE;
1664 mutex_unlock(&buffer->mutex);
1666 EXPORT_SYMBOL_GPL(ring_buffer_change_overwrite);
1668 static inline void *
1669 __rb_data_page_index(struct buffer_data_page *bpage, unsigned index)
1671 return bpage->data + index;
1674 static inline void *__rb_page_index(struct buffer_page *bpage, unsigned index)
1676 return bpage->page->data + index;
1679 static inline struct ring_buffer_event *
1680 rb_reader_event(struct ring_buffer_per_cpu *cpu_buffer)
1682 return __rb_page_index(cpu_buffer->reader_page,
1683 cpu_buffer->reader_page->read);
1686 static inline struct ring_buffer_event *
1687 rb_iter_head_event(struct ring_buffer_iter *iter)
1689 return __rb_page_index(iter->head_page, iter->head);
1692 static inline unsigned rb_page_commit(struct buffer_page *bpage)
1694 return local_read(&bpage->page->commit);
1697 /* Size is determined by what has been committed */
1698 static inline unsigned rb_page_size(struct buffer_page *bpage)
1700 return rb_page_commit(bpage);
1703 static inline unsigned
1704 rb_commit_index(struct ring_buffer_per_cpu *cpu_buffer)
1706 return rb_page_commit(cpu_buffer->commit_page);
1709 static inline unsigned
1710 rb_event_index(struct ring_buffer_event *event)
1712 unsigned long addr = (unsigned long)event;
1714 return (addr & ~PAGE_MASK) - BUF_PAGE_HDR_SIZE;
1718 rb_event_is_commit(struct ring_buffer_per_cpu *cpu_buffer,
1719 struct ring_buffer_event *event)
1721 unsigned long addr = (unsigned long)event;
1722 unsigned long index;
1724 index = rb_event_index(event);
1727 return cpu_buffer->commit_page->page == (void *)addr &&
1728 rb_commit_index(cpu_buffer) == index;
1732 rb_set_commit_to_write(struct ring_buffer_per_cpu *cpu_buffer)
1734 unsigned long max_count;
1737 * We only race with interrupts and NMIs on this CPU.
1738 * If we own the commit event, then we can commit
1739 * all others that interrupted us, since the interruptions
1740 * are in stack format (they finish before they come
1741 * back to us). This allows us to do a simple loop to
1742 * assign the commit to the tail.
1745 max_count = cpu_buffer->nr_pages * 100;
1747 while (cpu_buffer->commit_page != cpu_buffer->tail_page) {
1748 if (RB_WARN_ON(cpu_buffer, !(--max_count)))
1750 if (RB_WARN_ON(cpu_buffer,
1751 rb_is_reader_page(cpu_buffer->tail_page)))
1753 local_set(&cpu_buffer->commit_page->page->commit,
1754 rb_page_write(cpu_buffer->commit_page));
1755 rb_inc_page(cpu_buffer, &cpu_buffer->commit_page);
1756 cpu_buffer->write_stamp =
1757 cpu_buffer->commit_page->page->time_stamp;
1758 /* add barrier to keep gcc from optimizing too much */
1761 while (rb_commit_index(cpu_buffer) !=
1762 rb_page_write(cpu_buffer->commit_page)) {
1764 local_set(&cpu_buffer->commit_page->page->commit,
1765 rb_page_write(cpu_buffer->commit_page));
1766 RB_WARN_ON(cpu_buffer,
1767 local_read(&cpu_buffer->commit_page->page->commit) &
1772 /* again, keep gcc from optimizing */
1776 * If an interrupt came in just after the first while loop
1777 * and pushed the tail page forward, we will be left with
1778 * a dangling commit that will never go forward.
1780 if (unlikely(cpu_buffer->commit_page != cpu_buffer->tail_page))
1784 static void rb_reset_reader_page(struct ring_buffer_per_cpu *cpu_buffer)
1786 cpu_buffer->read_stamp = cpu_buffer->reader_page->page->time_stamp;
1787 cpu_buffer->reader_page->read = 0;
1790 static void rb_inc_iter(struct ring_buffer_iter *iter)
1792 struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
1795 * The iterator could be on the reader page (it starts there).
1796 * But the head could have moved, since the reader was
1797 * found. Check for this case and assign the iterator
1798 * to the head page instead of next.
1800 if (iter->head_page == cpu_buffer->reader_page)
1801 iter->head_page = rb_set_head_page(cpu_buffer);
1803 rb_inc_page(cpu_buffer, &iter->head_page);
1805 iter->read_stamp = iter->head_page->page->time_stamp;
1809 /* Slow path, do not inline */
1810 static noinline struct ring_buffer_event *
1811 rb_add_time_stamp(struct ring_buffer_event *event, u64 delta)
1813 event->type_len = RINGBUF_TYPE_TIME_EXTEND;
1815 /* Not the first event on the page? */
1816 if (rb_event_index(event)) {
1817 event->time_delta = delta & TS_MASK;
1818 event->array[0] = delta >> TS_SHIFT;
1820 /* nope, just zero it */
1821 event->time_delta = 0;
1822 event->array[0] = 0;
1825 return skip_time_extend(event);
1829 * rb_update_event - update event type and data
1830 * @event: the even to update
1831 * @type: the type of event
1832 * @length: the size of the event field in the ring buffer
1834 * Update the type and data fields of the event. The length
1835 * is the actual size that is written to the ring buffer,
1836 * and with this, we can determine what to place into the
1840 rb_update_event(struct ring_buffer_per_cpu *cpu_buffer,
1841 struct ring_buffer_event *event, unsigned length,
1842 int add_timestamp, u64 delta)
1844 /* Only a commit updates the timestamp */
1845 if (unlikely(!rb_event_is_commit(cpu_buffer, event)))
1849 * If we need to add a timestamp, then we
1850 * add it to the start of the resevered space.
1852 if (unlikely(add_timestamp)) {
1853 event = rb_add_time_stamp(event, delta);
1854 length -= RB_LEN_TIME_EXTEND;
1858 event->time_delta = delta;
1859 length -= RB_EVNT_HDR_SIZE;
1860 if (length > RB_MAX_SMALL_DATA || RB_FORCE_8BYTE_ALIGNMENT) {
1861 event->type_len = 0;
1862 event->array[0] = length;
1864 event->type_len = DIV_ROUND_UP(length, RB_ALIGNMENT);
1868 * rb_handle_head_page - writer hit the head page
1870 * Returns: +1 to retry page
1875 rb_handle_head_page(struct ring_buffer_per_cpu *cpu_buffer,
1876 struct buffer_page *tail_page,
1877 struct buffer_page *next_page)
1879 struct buffer_page *new_head;
1884 entries = rb_page_entries(next_page);
1887 * The hard part is here. We need to move the head
1888 * forward, and protect against both readers on
1889 * other CPUs and writers coming in via interrupts.
1891 type = rb_head_page_set_update(cpu_buffer, next_page, tail_page,
1895 * type can be one of four:
1896 * NORMAL - an interrupt already moved it for us
1897 * HEAD - we are the first to get here.
1898 * UPDATE - we are the interrupt interrupting
1900 * MOVED - a reader on another CPU moved the next
1901 * pointer to its reader page. Give up
1908 * We changed the head to UPDATE, thus
1909 * it is our responsibility to update
1912 local_add(entries, &cpu_buffer->overrun);
1913 local_sub(BUF_PAGE_SIZE, &cpu_buffer->entries_bytes);
1916 * The entries will be zeroed out when we move the
1920 /* still more to do */
1923 case RB_PAGE_UPDATE:
1925 * This is an interrupt that interrupt the
1926 * previous update. Still more to do.
1929 case RB_PAGE_NORMAL:
1931 * An interrupt came in before the update
1932 * and processed this for us.
1933 * Nothing left to do.
1938 * The reader is on another CPU and just did
1939 * a swap with our next_page.
1944 RB_WARN_ON(cpu_buffer, 1); /* WTF??? */
1949 * Now that we are here, the old head pointer is
1950 * set to UPDATE. This will keep the reader from
1951 * swapping the head page with the reader page.
1952 * The reader (on another CPU) will spin till
1955 * We just need to protect against interrupts
1956 * doing the job. We will set the next pointer
1957 * to HEAD. After that, we set the old pointer
1958 * to NORMAL, but only if it was HEAD before.
1959 * otherwise we are an interrupt, and only
1960 * want the outer most commit to reset it.
1962 new_head = next_page;
1963 rb_inc_page(cpu_buffer, &new_head);
1965 ret = rb_head_page_set_head(cpu_buffer, new_head, next_page,
1969 * Valid returns are:
1970 * HEAD - an interrupt came in and already set it.
1971 * NORMAL - One of two things:
1972 * 1) We really set it.
1973 * 2) A bunch of interrupts came in and moved
1974 * the page forward again.
1978 case RB_PAGE_NORMAL:
1982 RB_WARN_ON(cpu_buffer, 1);
1987 * It is possible that an interrupt came in,
1988 * set the head up, then more interrupts came in
1989 * and moved it again. When we get back here,
1990 * the page would have been set to NORMAL but we
1991 * just set it back to HEAD.
1993 * How do you detect this? Well, if that happened
1994 * the tail page would have moved.
1996 if (ret == RB_PAGE_NORMAL) {
1998 * If the tail had moved passed next, then we need
1999 * to reset the pointer.
2001 if (cpu_buffer->tail_page != tail_page &&
2002 cpu_buffer->tail_page != next_page)
2003 rb_head_page_set_normal(cpu_buffer, new_head,
2009 * If this was the outer most commit (the one that
2010 * changed the original pointer from HEAD to UPDATE),
2011 * then it is up to us to reset it to NORMAL.
2013 if (type == RB_PAGE_HEAD) {
2014 ret = rb_head_page_set_normal(cpu_buffer, next_page,
2017 if (RB_WARN_ON(cpu_buffer,
2018 ret != RB_PAGE_UPDATE))
2025 static unsigned rb_calculate_event_length(unsigned length)
2027 struct ring_buffer_event event; /* Used only for sizeof array */
2029 /* zero length can cause confusions */
2033 if (length > RB_MAX_SMALL_DATA || RB_FORCE_8BYTE_ALIGNMENT)
2034 length += sizeof(event.array[0]);
2036 length += RB_EVNT_HDR_SIZE;
2037 length = ALIGN(length, RB_ARCH_ALIGNMENT);
2043 rb_reset_tail(struct ring_buffer_per_cpu *cpu_buffer,
2044 struct buffer_page *tail_page,
2045 unsigned long tail, unsigned long length)
2047 struct ring_buffer_event *event;
2050 * Only the event that crossed the page boundary
2051 * must fill the old tail_page with padding.
2053 if (tail >= BUF_PAGE_SIZE) {
2055 * If the page was filled, then we still need
2056 * to update the real_end. Reset it to zero
2057 * and the reader will ignore it.
2059 if (tail == BUF_PAGE_SIZE)
2060 tail_page->real_end = 0;
2062 local_sub(length, &tail_page->write);
2066 event = __rb_page_index(tail_page, tail);
2067 kmemcheck_annotate_bitfield(event, bitfield);
2069 /* account for padding bytes */
2070 local_add(BUF_PAGE_SIZE - tail, &cpu_buffer->entries_bytes);
2073 * Save the original length to the meta data.
2074 * This will be used by the reader to add lost event
2077 tail_page->real_end = tail;
2080 * If this event is bigger than the minimum size, then
2081 * we need to be careful that we don't subtract the
2082 * write counter enough to allow another writer to slip
2084 * We put in a discarded commit instead, to make sure
2085 * that this space is not used again.
2087 * If we are less than the minimum size, we don't need to
2090 if (tail > (BUF_PAGE_SIZE - RB_EVNT_MIN_SIZE)) {
2091 /* No room for any events */
2093 /* Mark the rest of the page with padding */
2094 rb_event_set_padding(event);
2096 /* Set the write back to the previous setting */
2097 local_sub(length, &tail_page->write);
2101 /* Put in a discarded event */
2102 event->array[0] = (BUF_PAGE_SIZE - tail) - RB_EVNT_HDR_SIZE;
2103 event->type_len = RINGBUF_TYPE_PADDING;
2104 /* time delta must be non zero */
2105 event->time_delta = 1;
2107 /* Set write to end of buffer */
2108 length = (tail + length) - BUF_PAGE_SIZE;
2109 local_sub(length, &tail_page->write);
2113 * This is the slow path, force gcc not to inline it.
2115 static noinline struct ring_buffer_event *
2116 rb_move_tail(struct ring_buffer_per_cpu *cpu_buffer,
2117 unsigned long length, unsigned long tail,
2118 struct buffer_page *tail_page, u64 ts)
2120 struct buffer_page *commit_page = cpu_buffer->commit_page;
2121 struct ring_buffer *buffer = cpu_buffer->buffer;
2122 struct buffer_page *next_page;
2125 next_page = tail_page;
2127 rb_inc_page(cpu_buffer, &next_page);
2130 * If for some reason, we had an interrupt storm that made
2131 * it all the way around the buffer, bail, and warn
2134 if (unlikely(next_page == commit_page)) {
2135 local_inc(&cpu_buffer->commit_overrun);
2140 * This is where the fun begins!
2142 * We are fighting against races between a reader that
2143 * could be on another CPU trying to swap its reader
2144 * page with the buffer head.
2146 * We are also fighting against interrupts coming in and
2147 * moving the head or tail on us as well.
2149 * If the next page is the head page then we have filled
2150 * the buffer, unless the commit page is still on the
2153 if (rb_is_head_page(cpu_buffer, next_page, &tail_page->list)) {
2156 * If the commit is not on the reader page, then
2157 * move the header page.
2159 if (!rb_is_reader_page(cpu_buffer->commit_page)) {
2161 * If we are not in overwrite mode,
2162 * this is easy, just stop here.
2164 if (!(buffer->flags & RB_FL_OVERWRITE)) {
2165 local_inc(&cpu_buffer->dropped_events);
2169 ret = rb_handle_head_page(cpu_buffer,
2178 * We need to be careful here too. The
2179 * commit page could still be on the reader
2180 * page. We could have a small buffer, and
2181 * have filled up the buffer with events
2182 * from interrupts and such, and wrapped.
2184 * Note, if the tail page is also the on the
2185 * reader_page, we let it move out.
2187 if (unlikely((cpu_buffer->commit_page !=
2188 cpu_buffer->tail_page) &&
2189 (cpu_buffer->commit_page ==
2190 cpu_buffer->reader_page))) {
2191 local_inc(&cpu_buffer->commit_overrun);
2197 ret = rb_tail_page_update(cpu_buffer, tail_page, next_page);
2200 * Nested commits always have zero deltas, so
2201 * just reread the time stamp
2203 ts = rb_time_stamp(buffer);
2204 next_page->page->time_stamp = ts;
2209 rb_reset_tail(cpu_buffer, tail_page, tail, length);
2211 /* fail and let the caller try again */
2212 return ERR_PTR(-EAGAIN);
2216 rb_reset_tail(cpu_buffer, tail_page, tail, length);
2221 static struct ring_buffer_event *
2222 __rb_reserve_next(struct ring_buffer_per_cpu *cpu_buffer,
2223 unsigned long length, u64 ts,
2224 u64 delta, int add_timestamp)
2226 struct buffer_page *tail_page;
2227 struct ring_buffer_event *event;
2228 unsigned long tail, write;
2231 * If the time delta since the last event is too big to
2232 * hold in the time field of the event, then we append a
2233 * TIME EXTEND event ahead of the data event.
2235 if (unlikely(add_timestamp))
2236 length += RB_LEN_TIME_EXTEND;
2238 tail_page = cpu_buffer->tail_page;
2239 write = local_add_return(length, &tail_page->write);
2241 /* set write to only the index of the write */
2242 write &= RB_WRITE_MASK;
2243 tail = write - length;
2245 /* See if we shot pass the end of this buffer page */
2246 if (unlikely(write > BUF_PAGE_SIZE))
2247 return rb_move_tail(cpu_buffer, length, tail,
2250 /* We reserved something on the buffer */
2252 event = __rb_page_index(tail_page, tail);
2253 kmemcheck_annotate_bitfield(event, bitfield);
2254 rb_update_event(cpu_buffer, event, length, add_timestamp, delta);
2256 local_inc(&tail_page->entries);
2259 * If this is the first commit on the page, then update
2263 tail_page->page->time_stamp = ts;
2265 /* account for these added bytes */
2266 local_add(length, &cpu_buffer->entries_bytes);
2272 rb_try_to_discard(struct ring_buffer_per_cpu *cpu_buffer,
2273 struct ring_buffer_event *event)
2275 unsigned long new_index, old_index;
2276 struct buffer_page *bpage;
2277 unsigned long index;
2280 new_index = rb_event_index(event);
2281 old_index = new_index + rb_event_ts_length(event);
2282 addr = (unsigned long)event;
2285 bpage = cpu_buffer->tail_page;
2287 if (bpage->page == (void *)addr && rb_page_write(bpage) == old_index) {
2288 unsigned long write_mask =
2289 local_read(&bpage->write) & ~RB_WRITE_MASK;
2290 unsigned long event_length = rb_event_length(event);
2292 * This is on the tail page. It is possible that
2293 * a write could come in and move the tail page
2294 * and write to the next page. That is fine
2295 * because we just shorten what is on this page.
2297 old_index += write_mask;
2298 new_index += write_mask;
2299 index = local_cmpxchg(&bpage->write, old_index, new_index);
2300 if (index == old_index) {
2301 /* update counters */
2302 local_sub(event_length, &cpu_buffer->entries_bytes);
2307 /* could not discard */
2311 static void rb_start_commit(struct ring_buffer_per_cpu *cpu_buffer)
2313 local_inc(&cpu_buffer->committing);
2314 local_inc(&cpu_buffer->commits);
2317 static inline void rb_end_commit(struct ring_buffer_per_cpu *cpu_buffer)
2319 unsigned long commits;
2321 if (RB_WARN_ON(cpu_buffer,
2322 !local_read(&cpu_buffer->committing)))
2326 commits = local_read(&cpu_buffer->commits);
2327 /* synchronize with interrupts */
2329 if (local_read(&cpu_buffer->committing) == 1)
2330 rb_set_commit_to_write(cpu_buffer);
2332 local_dec(&cpu_buffer->committing);
2334 /* synchronize with interrupts */
2338 * Need to account for interrupts coming in between the
2339 * updating of the commit page and the clearing of the
2340 * committing counter.
2342 if (unlikely(local_read(&cpu_buffer->commits) != commits) &&
2343 !local_read(&cpu_buffer->committing)) {
2344 local_inc(&cpu_buffer->committing);
2349 static struct ring_buffer_event *
2350 rb_reserve_next_event(struct ring_buffer *buffer,
2351 struct ring_buffer_per_cpu *cpu_buffer,
2352 unsigned long length)
2354 struct ring_buffer_event *event;
2360 rb_start_commit(cpu_buffer);
2362 #ifdef CONFIG_RING_BUFFER_ALLOW_SWAP
2364 * Due to the ability to swap a cpu buffer from a buffer
2365 * it is possible it was swapped before we committed.
2366 * (committing stops a swap). We check for it here and
2367 * if it happened, we have to fail the write.
2370 if (unlikely(ACCESS_ONCE(cpu_buffer->buffer) != buffer)) {
2371 local_dec(&cpu_buffer->committing);
2372 local_dec(&cpu_buffer->commits);
2377 length = rb_calculate_event_length(length);
2383 * We allow for interrupts to reenter here and do a trace.
2384 * If one does, it will cause this original code to loop
2385 * back here. Even with heavy interrupts happening, this
2386 * should only happen a few times in a row. If this happens
2387 * 1000 times in a row, there must be either an interrupt
2388 * storm or we have something buggy.
2391 if (RB_WARN_ON(cpu_buffer, ++nr_loops > 1000))
2394 ts = rb_time_stamp(cpu_buffer->buffer);
2395 diff = ts - cpu_buffer->write_stamp;
2397 /* make sure this diff is calculated here */
2400 /* Did the write stamp get updated already? */
2401 if (likely(ts >= cpu_buffer->write_stamp)) {
2403 if (unlikely(test_time_stamp(delta))) {
2404 int local_clock_stable = 1;
2405 #ifdef CONFIG_HAVE_UNSTABLE_SCHED_CLOCK
2406 local_clock_stable = sched_clock_stable;
2408 WARN_ONCE(delta > (1ULL << 59),
2409 KERN_WARNING "Delta way too big! %llu ts=%llu write stamp = %llu\n%s",
2410 (unsigned long long)delta,
2411 (unsigned long long)ts,
2412 (unsigned long long)cpu_buffer->write_stamp,
2413 local_clock_stable ? "" :
2414 "If you just came from a suspend/resume,\n"
2415 "please switch to the trace global clock:\n"
2416 " echo global > /sys/kernel/debug/tracing/trace_clock\n");
2421 event = __rb_reserve_next(cpu_buffer, length, ts,
2422 delta, add_timestamp);
2423 if (unlikely(PTR_ERR(event) == -EAGAIN))
2432 rb_end_commit(cpu_buffer);
2436 #ifdef CONFIG_TRACING
2439 * The lock and unlock are done within a preempt disable section.
2440 * The current_context per_cpu variable can only be modified
2441 * by the current task between lock and unlock. But it can
2442 * be modified more than once via an interrupt. To pass this
2443 * information from the lock to the unlock without having to
2444 * access the 'in_interrupt()' functions again (which do show
2445 * a bit of overhead in something as critical as function tracing,
2446 * we use a bitmask trick.
2448 * bit 0 = NMI context
2449 * bit 1 = IRQ context
2450 * bit 2 = SoftIRQ context
2451 * bit 3 = normal context.
2453 * This works because this is the order of contexts that can
2454 * preempt other contexts. A SoftIRQ never preempts an IRQ
2457 * When the context is determined, the corresponding bit is
2458 * checked and set (if it was set, then a recursion of that context
2461 * On unlock, we need to clear this bit. To do so, just subtract
2462 * 1 from the current_context and AND it to itself.
2466 * 101 & 100 = 100 (clearing bit zero)
2469 * 1010 & 1001 = 1000 (clearing bit 1)
2471 * The least significant bit can be cleared this way, and it
2472 * just so happens that it is the same bit corresponding to
2473 * the current context.
2475 static DEFINE_PER_CPU(unsigned int, current_context);
2477 static __always_inline int trace_recursive_lock(void)
2479 unsigned int val = this_cpu_read(current_context);
2482 if (in_interrupt()) {
2492 if (unlikely(val & (1 << bit)))
2496 this_cpu_write(current_context, val);
2501 static __always_inline void trace_recursive_unlock(void)
2503 unsigned int val = this_cpu_read(current_context);
2506 val &= this_cpu_read(current_context);
2507 this_cpu_write(current_context, val);
2512 #define trace_recursive_lock() (0)
2513 #define trace_recursive_unlock() do { } while (0)
2518 * ring_buffer_lock_reserve - reserve a part of the buffer
2519 * @buffer: the ring buffer to reserve from
2520 * @length: the length of the data to reserve (excluding event header)
2522 * Returns a reseverd event on the ring buffer to copy directly to.
2523 * The user of this interface will need to get the body to write into
2524 * and can use the ring_buffer_event_data() interface.
2526 * The length is the length of the data needed, not the event length
2527 * which also includes the event header.
2529 * Must be paired with ring_buffer_unlock_commit, unless NULL is returned.
2530 * If NULL is returned, then nothing has been allocated or locked.
2532 struct ring_buffer_event *
2533 ring_buffer_lock_reserve(struct ring_buffer *buffer, unsigned long length)
2535 struct ring_buffer_per_cpu *cpu_buffer;
2536 struct ring_buffer_event *event;
2539 if (ring_buffer_flags != RB_BUFFERS_ON)
2542 /* If we are tracing schedule, we don't want to recurse */
2543 preempt_disable_notrace();
2545 if (atomic_read(&buffer->record_disabled))
2548 if (trace_recursive_lock())
2551 cpu = raw_smp_processor_id();
2553 if (!cpumask_test_cpu(cpu, buffer->cpumask))
2556 cpu_buffer = buffer->buffers[cpu];
2558 if (atomic_read(&cpu_buffer->record_disabled))
2561 if (length > BUF_MAX_DATA_SIZE)
2564 event = rb_reserve_next_event(buffer, cpu_buffer, length);
2571 trace_recursive_unlock();
2574 preempt_enable_notrace();
2577 EXPORT_SYMBOL_GPL(ring_buffer_lock_reserve);
2580 rb_update_write_stamp(struct ring_buffer_per_cpu *cpu_buffer,
2581 struct ring_buffer_event *event)
2586 * The event first in the commit queue updates the
2589 if (rb_event_is_commit(cpu_buffer, event)) {
2591 * A commit event that is first on a page
2592 * updates the write timestamp with the page stamp
2594 if (!rb_event_index(event))
2595 cpu_buffer->write_stamp =
2596 cpu_buffer->commit_page->page->time_stamp;
2597 else if (event->type_len == RINGBUF_TYPE_TIME_EXTEND) {
2598 delta = event->array[0];
2600 delta += event->time_delta;
2601 cpu_buffer->write_stamp += delta;
2603 cpu_buffer->write_stamp += event->time_delta;
2607 static void rb_commit(struct ring_buffer_per_cpu *cpu_buffer,
2608 struct ring_buffer_event *event)
2610 local_inc(&cpu_buffer->entries);
2611 rb_update_write_stamp(cpu_buffer, event);
2612 rb_end_commit(cpu_buffer);
2616 * ring_buffer_unlock_commit - commit a reserved
2617 * @buffer: The buffer to commit to
2618 * @event: The event pointer to commit.
2620 * This commits the data to the ring buffer, and releases any locks held.
2622 * Must be paired with ring_buffer_lock_reserve.
2624 int ring_buffer_unlock_commit(struct ring_buffer *buffer,
2625 struct ring_buffer_event *event)
2627 struct ring_buffer_per_cpu *cpu_buffer;
2628 int cpu = raw_smp_processor_id();
2630 cpu_buffer = buffer->buffers[cpu];
2632 rb_commit(cpu_buffer, event);
2634 trace_recursive_unlock();
2636 preempt_enable_notrace();
2640 EXPORT_SYMBOL_GPL(ring_buffer_unlock_commit);
2642 static inline void rb_event_discard(struct ring_buffer_event *event)
2644 if (event->type_len == RINGBUF_TYPE_TIME_EXTEND)
2645 event = skip_time_extend(event);
2647 /* array[0] holds the actual length for the discarded event */
2648 event->array[0] = rb_event_data_length(event) - RB_EVNT_HDR_SIZE;
2649 event->type_len = RINGBUF_TYPE_PADDING;
2650 /* time delta must be non zero */
2651 if (!event->time_delta)
2652 event->time_delta = 1;
2656 * Decrement the entries to the page that an event is on.
2657 * The event does not even need to exist, only the pointer
2658 * to the page it is on. This may only be called before the commit
2662 rb_decrement_entry(struct ring_buffer_per_cpu *cpu_buffer,
2663 struct ring_buffer_event *event)
2665 unsigned long addr = (unsigned long)event;
2666 struct buffer_page *bpage = cpu_buffer->commit_page;
2667 struct buffer_page *start;
2671 /* Do the likely case first */
2672 if (likely(bpage->page == (void *)addr)) {
2673 local_dec(&bpage->entries);
2678 * Because the commit page may be on the reader page we
2679 * start with the next page and check the end loop there.
2681 rb_inc_page(cpu_buffer, &bpage);
2684 if (bpage->page == (void *)addr) {
2685 local_dec(&bpage->entries);
2688 rb_inc_page(cpu_buffer, &bpage);
2689 } while (bpage != start);
2691 /* commit not part of this buffer?? */
2692 RB_WARN_ON(cpu_buffer, 1);
2696 * ring_buffer_commit_discard - discard an event that has not been committed
2697 * @buffer: the ring buffer
2698 * @event: non committed event to discard
2700 * Sometimes an event that is in the ring buffer needs to be ignored.
2701 * This function lets the user discard an event in the ring buffer
2702 * and then that event will not be read later.
2704 * This function only works if it is called before the the item has been
2705 * committed. It will try to free the event from the ring buffer
2706 * if another event has not been added behind it.
2708 * If another event has been added behind it, it will set the event
2709 * up as discarded, and perform the commit.
2711 * If this function is called, do not call ring_buffer_unlock_commit on
2714 void ring_buffer_discard_commit(struct ring_buffer *buffer,
2715 struct ring_buffer_event *event)
2717 struct ring_buffer_per_cpu *cpu_buffer;
2720 /* The event is discarded regardless */
2721 rb_event_discard(event);
2723 cpu = smp_processor_id();
2724 cpu_buffer = buffer->buffers[cpu];
2727 * This must only be called if the event has not been
2728 * committed yet. Thus we can assume that preemption
2729 * is still disabled.
2731 RB_WARN_ON(buffer, !local_read(&cpu_buffer->committing));
2733 rb_decrement_entry(cpu_buffer, event);
2734 if (rb_try_to_discard(cpu_buffer, event))
2738 * The commit is still visible by the reader, so we
2739 * must still update the timestamp.
2741 rb_update_write_stamp(cpu_buffer, event);
2743 rb_end_commit(cpu_buffer);
2745 trace_recursive_unlock();
2747 preempt_enable_notrace();
2750 EXPORT_SYMBOL_GPL(ring_buffer_discard_commit);
2753 * ring_buffer_write - write data to the buffer without reserving
2754 * @buffer: The ring buffer to write to.
2755 * @length: The length of the data being written (excluding the event header)
2756 * @data: The data to write to the buffer.
2758 * This is like ring_buffer_lock_reserve and ring_buffer_unlock_commit as
2759 * one function. If you already have the data to write to the buffer, it
2760 * may be easier to simply call this function.
2762 * Note, like ring_buffer_lock_reserve, the length is the length of the data
2763 * and not the length of the event which would hold the header.
2765 int ring_buffer_write(struct ring_buffer *buffer,
2766 unsigned long length,
2769 struct ring_buffer_per_cpu *cpu_buffer;
2770 struct ring_buffer_event *event;
2775 if (ring_buffer_flags != RB_BUFFERS_ON)
2778 preempt_disable_notrace();
2780 if (atomic_read(&buffer->record_disabled))
2783 cpu = raw_smp_processor_id();
2785 if (!cpumask_test_cpu(cpu, buffer->cpumask))
2788 cpu_buffer = buffer->buffers[cpu];
2790 if (atomic_read(&cpu_buffer->record_disabled))
2793 if (length > BUF_MAX_DATA_SIZE)
2796 event = rb_reserve_next_event(buffer, cpu_buffer, length);
2800 body = rb_event_data(event);
2802 memcpy(body, data, length);
2804 rb_commit(cpu_buffer, event);
2808 preempt_enable_notrace();
2812 EXPORT_SYMBOL_GPL(ring_buffer_write);
2814 static int rb_per_cpu_empty(struct ring_buffer_per_cpu *cpu_buffer)
2816 struct buffer_page *reader = cpu_buffer->reader_page;
2817 struct buffer_page *head = rb_set_head_page(cpu_buffer);
2818 struct buffer_page *commit = cpu_buffer->commit_page;
2820 /* In case of error, head will be NULL */
2821 if (unlikely(!head))
2824 return reader->read == rb_page_commit(reader) &&
2825 (commit == reader ||
2827 head->read == rb_page_commit(commit)));
2831 * ring_buffer_record_disable - stop all writes into the buffer
2832 * @buffer: The ring buffer to stop writes to.
2834 * This prevents all writes to the buffer. Any attempt to write
2835 * to the buffer after this will fail and return NULL.
2837 * The caller should call synchronize_sched() after this.
2839 void ring_buffer_record_disable(struct ring_buffer *buffer)
2841 atomic_inc(&buffer->record_disabled);
2843 EXPORT_SYMBOL_GPL(ring_buffer_record_disable);
2846 * ring_buffer_record_enable - enable writes to the buffer
2847 * @buffer: The ring buffer to enable writes
2849 * Note, multiple disables will need the same number of enables
2850 * to truly enable the writing (much like preempt_disable).
2852 void ring_buffer_record_enable(struct ring_buffer *buffer)
2854 atomic_dec(&buffer->record_disabled);
2856 EXPORT_SYMBOL_GPL(ring_buffer_record_enable);
2859 * ring_buffer_record_off - stop all writes into the buffer
2860 * @buffer: The ring buffer to stop writes to.
2862 * This prevents all writes to the buffer. Any attempt to write
2863 * to the buffer after this will fail and return NULL.
2865 * This is different than ring_buffer_record_disable() as
2866 * it works like an on/off switch, where as the disable() version
2867 * must be paired with a enable().
2869 void ring_buffer_record_off(struct ring_buffer *buffer)
2872 unsigned int new_rd;
2875 rd = atomic_read(&buffer->record_disabled);
2876 new_rd = rd | RB_BUFFER_OFF;
2877 } while (atomic_cmpxchg(&buffer->record_disabled, rd, new_rd) != rd);
2879 EXPORT_SYMBOL_GPL(ring_buffer_record_off);
2882 * ring_buffer_record_on - restart writes into the buffer
2883 * @buffer: The ring buffer to start writes to.
2885 * This enables all writes to the buffer that was disabled by
2886 * ring_buffer_record_off().
2888 * This is different than ring_buffer_record_enable() as
2889 * it works like an on/off switch, where as the enable() version
2890 * must be paired with a disable().
2892 void ring_buffer_record_on(struct ring_buffer *buffer)
2895 unsigned int new_rd;
2898 rd = atomic_read(&buffer->record_disabled);
2899 new_rd = rd & ~RB_BUFFER_OFF;
2900 } while (atomic_cmpxchg(&buffer->record_disabled, rd, new_rd) != rd);
2902 EXPORT_SYMBOL_GPL(ring_buffer_record_on);
2905 * ring_buffer_record_is_on - return true if the ring buffer can write
2906 * @buffer: The ring buffer to see if write is enabled
2908 * Returns true if the ring buffer is in a state that it accepts writes.
2910 int ring_buffer_record_is_on(struct ring_buffer *buffer)
2912 return !atomic_read(&buffer->record_disabled);
2916 * ring_buffer_record_disable_cpu - stop all writes into the cpu_buffer
2917 * @buffer: The ring buffer to stop writes to.
2918 * @cpu: The CPU buffer to stop
2920 * This prevents all writes to the buffer. Any attempt to write
2921 * to the buffer after this will fail and return NULL.
2923 * The caller should call synchronize_sched() after this.
2925 void ring_buffer_record_disable_cpu(struct ring_buffer *buffer, int cpu)
2927 struct ring_buffer_per_cpu *cpu_buffer;
2929 if (!cpumask_test_cpu(cpu, buffer->cpumask))
2932 cpu_buffer = buffer->buffers[cpu];
2933 atomic_inc(&cpu_buffer->record_disabled);
2935 EXPORT_SYMBOL_GPL(ring_buffer_record_disable_cpu);
2938 * ring_buffer_record_enable_cpu - enable writes to the buffer
2939 * @buffer: The ring buffer to enable writes
2940 * @cpu: The CPU to enable.
2942 * Note, multiple disables will need the same number of enables
2943 * to truly enable the writing (much like preempt_disable).
2945 void ring_buffer_record_enable_cpu(struct ring_buffer *buffer, int cpu)
2947 struct ring_buffer_per_cpu *cpu_buffer;
2949 if (!cpumask_test_cpu(cpu, buffer->cpumask))
2952 cpu_buffer = buffer->buffers[cpu];
2953 atomic_dec(&cpu_buffer->record_disabled);
2955 EXPORT_SYMBOL_GPL(ring_buffer_record_enable_cpu);
2958 * The total entries in the ring buffer is the running counter
2959 * of entries entered into the ring buffer, minus the sum of
2960 * the entries read from the ring buffer and the number of
2961 * entries that were overwritten.
2963 static inline unsigned long
2964 rb_num_of_entries(struct ring_buffer_per_cpu *cpu_buffer)
2966 return local_read(&cpu_buffer->entries) -
2967 (local_read(&cpu_buffer->overrun) + cpu_buffer->read);
2971 * ring_buffer_oldest_event_ts - get the oldest event timestamp from the buffer
2972 * @buffer: The ring buffer
2973 * @cpu: The per CPU buffer to read from.
2975 u64 ring_buffer_oldest_event_ts(struct ring_buffer *buffer, int cpu)
2977 unsigned long flags;
2978 struct ring_buffer_per_cpu *cpu_buffer;
2979 struct buffer_page *bpage;
2982 if (!cpumask_test_cpu(cpu, buffer->cpumask))
2985 cpu_buffer = buffer->buffers[cpu];
2986 raw_spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
2988 * if the tail is on reader_page, oldest time stamp is on the reader
2991 if (cpu_buffer->tail_page == cpu_buffer->reader_page)
2992 bpage = cpu_buffer->reader_page;
2994 bpage = rb_set_head_page(cpu_buffer);
2996 ret = bpage->page->time_stamp;
2997 raw_spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
3001 EXPORT_SYMBOL_GPL(ring_buffer_oldest_event_ts);
3004 * ring_buffer_bytes_cpu - get the number of bytes consumed in a cpu buffer
3005 * @buffer: The ring buffer
3006 * @cpu: The per CPU buffer to read from.
3008 unsigned long ring_buffer_bytes_cpu(struct ring_buffer *buffer, int cpu)
3010 struct ring_buffer_per_cpu *cpu_buffer;
3013 if (!cpumask_test_cpu(cpu, buffer->cpumask))
3016 cpu_buffer = buffer->buffers[cpu];
3017 ret = local_read(&cpu_buffer->entries_bytes) - cpu_buffer->read_bytes;
3021 EXPORT_SYMBOL_GPL(ring_buffer_bytes_cpu);
3024 * ring_buffer_entries_cpu - get the number of entries in a cpu buffer
3025 * @buffer: The ring buffer
3026 * @cpu: The per CPU buffer to get the entries from.
3028 unsigned long ring_buffer_entries_cpu(struct ring_buffer *buffer, int cpu)
3030 struct ring_buffer_per_cpu *cpu_buffer;
3032 if (!cpumask_test_cpu(cpu, buffer->cpumask))
3035 cpu_buffer = buffer->buffers[cpu];
3037 return rb_num_of_entries(cpu_buffer);
3039 EXPORT_SYMBOL_GPL(ring_buffer_entries_cpu);
3042 * ring_buffer_overrun_cpu - get the number of overruns caused by the ring
3043 * buffer wrapping around (only if RB_FL_OVERWRITE is on).
3044 * @buffer: The ring buffer
3045 * @cpu: The per CPU buffer to get the number of overruns from
3047 unsigned long ring_buffer_overrun_cpu(struct ring_buffer *buffer, int cpu)
3049 struct ring_buffer_per_cpu *cpu_buffer;
3052 if (!cpumask_test_cpu(cpu, buffer->cpumask))
3055 cpu_buffer = buffer->buffers[cpu];
3056 ret = local_read(&cpu_buffer->overrun);
3060 EXPORT_SYMBOL_GPL(ring_buffer_overrun_cpu);
3063 * ring_buffer_commit_overrun_cpu - get the number of overruns caused by
3064 * commits failing due to the buffer wrapping around while there are uncommitted
3065 * events, such as during an interrupt storm.
3066 * @buffer: The ring buffer
3067 * @cpu: The per CPU buffer to get the number of overruns from
3070 ring_buffer_commit_overrun_cpu(struct ring_buffer *buffer, int cpu)
3072 struct ring_buffer_per_cpu *cpu_buffer;
3075 if (!cpumask_test_cpu(cpu, buffer->cpumask))
3078 cpu_buffer = buffer->buffers[cpu];
3079 ret = local_read(&cpu_buffer->commit_overrun);
3083 EXPORT_SYMBOL_GPL(ring_buffer_commit_overrun_cpu);
3086 * ring_buffer_dropped_events_cpu - get the number of dropped events caused by
3087 * the ring buffer filling up (only if RB_FL_OVERWRITE is off).
3088 * @buffer: The ring buffer
3089 * @cpu: The per CPU buffer to get the number of overruns from
3092 ring_buffer_dropped_events_cpu(struct ring_buffer *buffer, int cpu)
3094 struct ring_buffer_per_cpu *cpu_buffer;
3097 if (!cpumask_test_cpu(cpu, buffer->cpumask))
3100 cpu_buffer = buffer->buffers[cpu];
3101 ret = local_read(&cpu_buffer->dropped_events);
3105 EXPORT_SYMBOL_GPL(ring_buffer_dropped_events_cpu);
3108 * ring_buffer_read_events_cpu - get the number of events successfully read
3109 * @buffer: The ring buffer
3110 * @cpu: The per CPU buffer to get the number of events read
3113 ring_buffer_read_events_cpu(struct ring_buffer *buffer, int cpu)
3115 struct ring_buffer_per_cpu *cpu_buffer;
3117 if (!cpumask_test_cpu(cpu, buffer->cpumask))
3120 cpu_buffer = buffer->buffers[cpu];
3121 return cpu_buffer->read;
3123 EXPORT_SYMBOL_GPL(ring_buffer_read_events_cpu);
3126 * ring_buffer_entries - get the number of entries in a buffer
3127 * @buffer: The ring buffer
3129 * Returns the total number of entries in the ring buffer
3132 unsigned long ring_buffer_entries(struct ring_buffer *buffer)
3134 struct ring_buffer_per_cpu *cpu_buffer;
3135 unsigned long entries = 0;
3138 /* if you care about this being correct, lock the buffer */
3139 for_each_buffer_cpu(buffer, cpu) {
3140 cpu_buffer = buffer->buffers[cpu];
3141 entries += rb_num_of_entries(cpu_buffer);
3146 EXPORT_SYMBOL_GPL(ring_buffer_entries);
3149 * ring_buffer_overruns - get the number of overruns in buffer
3150 * @buffer: The ring buffer
3152 * Returns the total number of overruns in the ring buffer
3155 unsigned long ring_buffer_overruns(struct ring_buffer *buffer)
3157 struct ring_buffer_per_cpu *cpu_buffer;
3158 unsigned long overruns = 0;
3161 /* if you care about this being correct, lock the buffer */
3162 for_each_buffer_cpu(buffer, cpu) {
3163 cpu_buffer = buffer->buffers[cpu];
3164 overruns += local_read(&cpu_buffer->overrun);
3169 EXPORT_SYMBOL_GPL(ring_buffer_overruns);
3171 static void rb_iter_reset(struct ring_buffer_iter *iter)
3173 struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
3175 /* Iterator usage is expected to have record disabled */
3176 if (list_empty(&cpu_buffer->reader_page->list)) {
3177 iter->head_page = rb_set_head_page(cpu_buffer);
3178 if (unlikely(!iter->head_page))
3180 iter->head = iter->head_page->read;
3182 iter->head_page = cpu_buffer->reader_page;
3183 iter->head = cpu_buffer->reader_page->read;
3186 iter->read_stamp = cpu_buffer->read_stamp;
3188 iter->read_stamp = iter->head_page->page->time_stamp;
3189 iter->cache_reader_page = cpu_buffer->reader_page;
3190 iter->cache_read = cpu_buffer->read;
3194 * ring_buffer_iter_reset - reset an iterator
3195 * @iter: The iterator to reset
3197 * Resets the iterator, so that it will start from the beginning
3200 void ring_buffer_iter_reset(struct ring_buffer_iter *iter)
3202 struct ring_buffer_per_cpu *cpu_buffer;
3203 unsigned long flags;
3208 cpu_buffer = iter->cpu_buffer;
3210 raw_spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
3211 rb_iter_reset(iter);
3212 raw_spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
3214 EXPORT_SYMBOL_GPL(ring_buffer_iter_reset);
3217 * ring_buffer_iter_empty - check if an iterator has no more to read
3218 * @iter: The iterator to check
3220 int ring_buffer_iter_empty(struct ring_buffer_iter *iter)
3222 struct ring_buffer_per_cpu *cpu_buffer;
3224 cpu_buffer = iter->cpu_buffer;
3226 return iter->head_page == cpu_buffer->commit_page &&
3227 iter->head == rb_commit_index(cpu_buffer);
3229 EXPORT_SYMBOL_GPL(ring_buffer_iter_empty);
3232 rb_update_read_stamp(struct ring_buffer_per_cpu *cpu_buffer,
3233 struct ring_buffer_event *event)
3237 switch (event->type_len) {
3238 case RINGBUF_TYPE_PADDING:
3241 case RINGBUF_TYPE_TIME_EXTEND:
3242 delta = event->array[0];
3244 delta += event->time_delta;
3245 cpu_buffer->read_stamp += delta;
3248 case RINGBUF_TYPE_TIME_STAMP:
3249 /* FIXME: not implemented */
3252 case RINGBUF_TYPE_DATA:
3253 cpu_buffer->read_stamp += event->time_delta;
3263 rb_update_iter_read_stamp(struct ring_buffer_iter *iter,
3264 struct ring_buffer_event *event)
3268 switch (event->type_len) {
3269 case RINGBUF_TYPE_PADDING:
3272 case RINGBUF_TYPE_TIME_EXTEND:
3273 delta = event->array[0];
3275 delta += event->time_delta;
3276 iter->read_stamp += delta;
3279 case RINGBUF_TYPE_TIME_STAMP:
3280 /* FIXME: not implemented */
3283 case RINGBUF_TYPE_DATA:
3284 iter->read_stamp += event->time_delta;
3293 static struct buffer_page *
3294 rb_get_reader_page(struct ring_buffer_per_cpu *cpu_buffer)
3296 struct buffer_page *reader = NULL;
3297 unsigned long overwrite;
3298 unsigned long flags;
3302 local_irq_save(flags);
3303 arch_spin_lock(&cpu_buffer->lock);
3307 * This should normally only loop twice. But because the
3308 * start of the reader inserts an empty page, it causes
3309 * a case where we will loop three times. There should be no
3310 * reason to loop four times (that I know of).
3312 if (RB_WARN_ON(cpu_buffer, ++nr_loops > 3)) {
3317 reader = cpu_buffer->reader_page;
3319 /* If there's more to read, return this page */
3320 if (cpu_buffer->reader_page->read < rb_page_size(reader))
3323 /* Never should we have an index greater than the size */
3324 if (RB_WARN_ON(cpu_buffer,
3325 cpu_buffer->reader_page->read > rb_page_size(reader)))
3328 /* check if we caught up to the tail */
3330 if (cpu_buffer->commit_page == cpu_buffer->reader_page)
3333 /* Don't bother swapping if the ring buffer is empty */
3334 if (rb_num_of_entries(cpu_buffer) == 0)
3338 * Reset the reader page to size zero.
3340 local_set(&cpu_buffer->reader_page->write, 0);
3341 local_set(&cpu_buffer->reader_page->entries, 0);
3342 local_set(&cpu_buffer->reader_page->page->commit, 0);
3343 cpu_buffer->reader_page->real_end = 0;
3347 * Splice the empty reader page into the list around the head.
3349 reader = rb_set_head_page(cpu_buffer);
3352 cpu_buffer->reader_page->list.next = rb_list_head(reader->list.next);
3353 cpu_buffer->reader_page->list.prev = reader->list.prev;
3356 * cpu_buffer->pages just needs to point to the buffer, it
3357 * has no specific buffer page to point to. Lets move it out
3358 * of our way so we don't accidentally swap it.
3360 cpu_buffer->pages = reader->list.prev;
3362 /* The reader page will be pointing to the new head */
3363 rb_set_list_to_head(cpu_buffer, &cpu_buffer->reader_page->list);
3366 * We want to make sure we read the overruns after we set up our
3367 * pointers to the next object. The writer side does a
3368 * cmpxchg to cross pages which acts as the mb on the writer
3369 * side. Note, the reader will constantly fail the swap
3370 * while the writer is updating the pointers, so this
3371 * guarantees that the overwrite recorded here is the one we
3372 * want to compare with the last_overrun.
3375 overwrite = local_read(&(cpu_buffer->overrun));
3378 * Here's the tricky part.
3380 * We need to move the pointer past the header page.
3381 * But we can only do that if a writer is not currently
3382 * moving it. The page before the header page has the
3383 * flag bit '1' set if it is pointing to the page we want.
3384 * but if the writer is in the process of moving it
3385 * than it will be '2' or already moved '0'.
3388 ret = rb_head_page_replace(reader, cpu_buffer->reader_page);
3391 * If we did not convert it, then we must try again.
3397 * Yeah! We succeeded in replacing the page.
3399 * Now make the new head point back to the reader page.
3401 rb_list_head(reader->list.next)->prev = &cpu_buffer->reader_page->list;
3402 rb_inc_page(cpu_buffer, &cpu_buffer->head_page);
3404 /* Finally update the reader page to the new head */
3405 cpu_buffer->reader_page = reader;
3406 rb_reset_reader_page(cpu_buffer);
3408 if (overwrite != cpu_buffer->last_overrun) {
3409 cpu_buffer->lost_events = overwrite - cpu_buffer->last_overrun;
3410 cpu_buffer->last_overrun = overwrite;
3416 arch_spin_unlock(&cpu_buffer->lock);
3417 local_irq_restore(flags);
3422 static void rb_advance_reader(struct ring_buffer_per_cpu *cpu_buffer)
3424 struct ring_buffer_event *event;
3425 struct buffer_page *reader;
3428 reader = rb_get_reader_page(cpu_buffer);
3430 /* This function should not be called when buffer is empty */
3431 if (RB_WARN_ON(cpu_buffer, !reader))
3434 event = rb_reader_event(cpu_buffer);
3436 if (event->type_len <= RINGBUF_TYPE_DATA_TYPE_LEN_MAX)
3439 rb_update_read_stamp(cpu_buffer, event);
3441 length = rb_event_length(event);
3442 cpu_buffer->reader_page->read += length;
3445 static void rb_advance_iter(struct ring_buffer_iter *iter)
3447 struct ring_buffer_per_cpu *cpu_buffer;
3448 struct ring_buffer_event *event;
3451 cpu_buffer = iter->cpu_buffer;
3454 * Check if we are at the end of the buffer.
3456 if (iter->head >= rb_page_size(iter->head_page)) {
3457 /* discarded commits can make the page empty */
3458 if (iter->head_page == cpu_buffer->commit_page)
3464 event = rb_iter_head_event(iter);
3466 length = rb_event_length(event);
3469 * This should not be called to advance the header if we are
3470 * at the tail of the buffer.
3472 if (RB_WARN_ON(cpu_buffer,
3473 (iter->head_page == cpu_buffer->commit_page) &&
3474 (iter->head + length > rb_commit_index(cpu_buffer))))
3477 rb_update_iter_read_stamp(iter, event);
3479 iter->head += length;
3481 /* check for end of page padding */
3482 if ((iter->head >= rb_page_size(iter->head_page)) &&
3483 (iter->head_page != cpu_buffer->commit_page))
3487 static int rb_lost_events(struct ring_buffer_per_cpu *cpu_buffer)
3489 return cpu_buffer->lost_events;
3492 static struct ring_buffer_event *
3493 rb_buffer_peek(struct ring_buffer_per_cpu *cpu_buffer, u64 *ts,
3494 unsigned long *lost_events)
3496 struct ring_buffer_event *event;
3497 struct buffer_page *reader;
3502 * We repeat when a time extend is encountered.
3503 * Since the time extend is always attached to a data event,
3504 * we should never loop more than once.
3505 * (We never hit the following condition more than twice).
3507 if (RB_WARN_ON(cpu_buffer, ++nr_loops > 2))
3510 reader = rb_get_reader_page(cpu_buffer);
3514 event = rb_reader_event(cpu_buffer);
3516 switch (event->type_len) {
3517 case RINGBUF_TYPE_PADDING:
3518 if (rb_null_event(event))
3519 RB_WARN_ON(cpu_buffer, 1);
3521 * Because the writer could be discarding every
3522 * event it creates (which would probably be bad)
3523 * if we were to go back to "again" then we may never
3524 * catch up, and will trigger the warn on, or lock
3525 * the box. Return the padding, and we will release
3526 * the current locks, and try again.
3530 case RINGBUF_TYPE_TIME_EXTEND:
3531 /* Internal data, OK to advance */
3532 rb_advance_reader(cpu_buffer);
3535 case RINGBUF_TYPE_TIME_STAMP:
3536 /* FIXME: not implemented */
3537 rb_advance_reader(cpu_buffer);
3540 case RINGBUF_TYPE_DATA:
3542 *ts = cpu_buffer->read_stamp + event->time_delta;
3543 ring_buffer_normalize_time_stamp(cpu_buffer->buffer,
3544 cpu_buffer->cpu, ts);
3547 *lost_events = rb_lost_events(cpu_buffer);
3556 EXPORT_SYMBOL_GPL(ring_buffer_peek);
3558 static struct ring_buffer_event *
3559 rb_iter_peek(struct ring_buffer_iter *iter, u64 *ts)
3561 struct ring_buffer *buffer;
3562 struct ring_buffer_per_cpu *cpu_buffer;
3563 struct ring_buffer_event *event;
3566 cpu_buffer = iter->cpu_buffer;
3567 buffer = cpu_buffer->buffer;
3570 * Check if someone performed a consuming read to
3571 * the buffer. A consuming read invalidates the iterator
3572 * and we need to reset the iterator in this case.
3574 if (unlikely(iter->cache_read != cpu_buffer->read ||
3575 iter->cache_reader_page != cpu_buffer->reader_page))
3576 rb_iter_reset(iter);
3579 if (ring_buffer_iter_empty(iter))
3583 * We repeat when a time extend is encountered.
3584 * Since the time extend is always attached to a data event,
3585 * we should never loop more than once.
3586 * (We never hit the following condition more than twice).
3588 if (RB_WARN_ON(cpu_buffer, ++nr_loops > 2))
3591 if (rb_per_cpu_empty(cpu_buffer))
3594 if (iter->head >= local_read(&iter->head_page->page->commit)) {
3599 event = rb_iter_head_event(iter);
3601 switch (event->type_len) {
3602 case RINGBUF_TYPE_PADDING:
3603 if (rb_null_event(event)) {
3607 rb_advance_iter(iter);
3610 case RINGBUF_TYPE_TIME_EXTEND:
3611 /* Internal data, OK to advance */
3612 rb_advance_iter(iter);
3615 case RINGBUF_TYPE_TIME_STAMP:
3616 /* FIXME: not implemented */
3617 rb_advance_iter(iter);
3620 case RINGBUF_TYPE_DATA:
3622 *ts = iter->read_stamp + event->time_delta;
3623 ring_buffer_normalize_time_stamp(buffer,
3624 cpu_buffer->cpu, ts);
3634 EXPORT_SYMBOL_GPL(ring_buffer_iter_peek);
3636 static inline int rb_ok_to_lock(void)
3639 * If an NMI die dumps out the content of the ring buffer
3640 * do not grab locks. We also permanently disable the ring
3641 * buffer too. A one time deal is all you get from reading
3642 * the ring buffer from an NMI.
3644 if (likely(!in_nmi()))
3647 tracing_off_permanent();
3652 * ring_buffer_peek - peek at the next event to be read
3653 * @buffer: The ring buffer to read
3654 * @cpu: The cpu to peak at
3655 * @ts: The timestamp counter of this event.
3656 * @lost_events: a variable to store if events were lost (may be NULL)
3658 * This will return the event that will be read next, but does
3659 * not consume the data.
3661 struct ring_buffer_event *
3662 ring_buffer_peek(struct ring_buffer *buffer, int cpu, u64 *ts,
3663 unsigned long *lost_events)
3665 struct ring_buffer_per_cpu *cpu_buffer = buffer->buffers[cpu];
3666 struct ring_buffer_event *event;
3667 unsigned long flags;
3670 if (!cpumask_test_cpu(cpu, buffer->cpumask))
3673 dolock = rb_ok_to_lock();
3675 local_irq_save(flags);
3677 raw_spin_lock(&cpu_buffer->reader_lock);
3678 event = rb_buffer_peek(cpu_buffer, ts, lost_events);
3679 if (event && event->type_len == RINGBUF_TYPE_PADDING)
3680 rb_advance_reader(cpu_buffer);
3682 raw_spin_unlock(&cpu_buffer->reader_lock);
3683 local_irq_restore(flags);
3685 if (event && event->type_len == RINGBUF_TYPE_PADDING)
3692 * ring_buffer_iter_peek - peek at the next event to be read
3693 * @iter: The ring buffer iterator
3694 * @ts: The timestamp counter of this event.
3696 * This will return the event that will be read next, but does
3697 * not increment the iterator.
3699 struct ring_buffer_event *
3700 ring_buffer_iter_peek(struct ring_buffer_iter *iter, u64 *ts)
3702 struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
3703 struct ring_buffer_event *event;
3704 unsigned long flags;
3707 raw_spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
3708 event = rb_iter_peek(iter, ts);
3709 raw_spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
3711 if (event && event->type_len == RINGBUF_TYPE_PADDING)
3718 * ring_buffer_consume - return an event and consume it
3719 * @buffer: The ring buffer to get the next event from
3720 * @cpu: the cpu to read the buffer from
3721 * @ts: a variable to store the timestamp (may be NULL)
3722 * @lost_events: a variable to store if events were lost (may be NULL)
3724 * Returns the next event in the ring buffer, and that event is consumed.
3725 * Meaning, that sequential reads will keep returning a different event,
3726 * and eventually empty the ring buffer if the producer is slower.
3728 struct ring_buffer_event *
3729 ring_buffer_consume(struct ring_buffer *buffer, int cpu, u64 *ts,
3730 unsigned long *lost_events)
3732 struct ring_buffer_per_cpu *cpu_buffer;
3733 struct ring_buffer_event *event = NULL;
3734 unsigned long flags;
3737 dolock = rb_ok_to_lock();
3740 /* might be called in atomic */
3743 if (!cpumask_test_cpu(cpu, buffer->cpumask))
3746 cpu_buffer = buffer->buffers[cpu];
3747 local_irq_save(flags);
3749 raw_spin_lock(&cpu_buffer->reader_lock);
3751 event = rb_buffer_peek(cpu_buffer, ts, lost_events);
3753 cpu_buffer->lost_events = 0;
3754 rb_advance_reader(cpu_buffer);
3758 raw_spin_unlock(&cpu_buffer->reader_lock);
3759 local_irq_restore(flags);
3764 if (event && event->type_len == RINGBUF_TYPE_PADDING)
3769 EXPORT_SYMBOL_GPL(ring_buffer_consume);
3772 * ring_buffer_read_prepare - Prepare for a non consuming read of the buffer
3773 * @buffer: The ring buffer to read from
3774 * @cpu: The cpu buffer to iterate over
3776 * This performs the initial preparations necessary to iterate
3777 * through the buffer. Memory is allocated, buffer recording
3778 * is disabled, and the iterator pointer is returned to the caller.
3780 * Disabling buffer recordng prevents the reading from being
3781 * corrupted. This is not a consuming read, so a producer is not
3784 * After a sequence of ring_buffer_read_prepare calls, the user is
3785 * expected to make at least one call to ring_buffer_prepare_sync.
3786 * Afterwards, ring_buffer_read_start is invoked to get things going
3789 * This overall must be paired with ring_buffer_finish.
3791 struct ring_buffer_iter *
3792 ring_buffer_read_prepare(struct ring_buffer *buffer, int cpu)
3794 struct ring_buffer_per_cpu *cpu_buffer;
3795 struct ring_buffer_iter *iter;
3797 if (!cpumask_test_cpu(cpu, buffer->cpumask))
3800 iter = kmalloc(sizeof(*iter), GFP_KERNEL);
3804 cpu_buffer = buffer->buffers[cpu];
3806 iter->cpu_buffer = cpu_buffer;
3808 atomic_inc(&buffer->resize_disabled);
3809 atomic_inc(&cpu_buffer->record_disabled);
3813 EXPORT_SYMBOL_GPL(ring_buffer_read_prepare);
3816 * ring_buffer_read_prepare_sync - Synchronize a set of prepare calls
3818 * All previously invoked ring_buffer_read_prepare calls to prepare
3819 * iterators will be synchronized. Afterwards, read_buffer_read_start
3820 * calls on those iterators are allowed.
3823 ring_buffer_read_prepare_sync(void)
3825 synchronize_sched();
3827 EXPORT_SYMBOL_GPL(ring_buffer_read_prepare_sync);
3830 * ring_buffer_read_start - start a non consuming read of the buffer
3831 * @iter: The iterator returned by ring_buffer_read_prepare
3833 * This finalizes the startup of an iteration through the buffer.
3834 * The iterator comes from a call to ring_buffer_read_prepare and
3835 * an intervening ring_buffer_read_prepare_sync must have been
3838 * Must be paired with ring_buffer_finish.
3841 ring_buffer_read_start(struct ring_buffer_iter *iter)
3843 struct ring_buffer_per_cpu *cpu_buffer;
3844 unsigned long flags;
3849 cpu_buffer = iter->cpu_buffer;
3851 raw_spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
3852 arch_spin_lock(&cpu_buffer->lock);
3853 rb_iter_reset(iter);
3854 arch_spin_unlock(&cpu_buffer->lock);
3855 raw_spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
3857 EXPORT_SYMBOL_GPL(ring_buffer_read_start);
3860 * ring_buffer_finish - finish reading the iterator of the buffer
3861 * @iter: The iterator retrieved by ring_buffer_start
3863 * This re-enables the recording to the buffer, and frees the
3867 ring_buffer_read_finish(struct ring_buffer_iter *iter)
3869 struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
3870 unsigned long flags;
3873 * Ring buffer is disabled from recording, here's a good place
3874 * to check the integrity of the ring buffer.
3875 * Must prevent readers from trying to read, as the check
3876 * clears the HEAD page and readers require it.
3878 raw_spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
3879 rb_check_pages(cpu_buffer);
3880 raw_spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
3882 atomic_dec(&cpu_buffer->record_disabled);
3883 atomic_dec(&cpu_buffer->buffer->resize_disabled);
3886 EXPORT_SYMBOL_GPL(ring_buffer_read_finish);
3889 * ring_buffer_read - read the next item in the ring buffer by the iterator
3890 * @iter: The ring buffer iterator
3891 * @ts: The time stamp of the event read.
3893 * This reads the next event in the ring buffer and increments the iterator.
3895 struct ring_buffer_event *
3896 ring_buffer_read(struct ring_buffer_iter *iter, u64 *ts)
3898 struct ring_buffer_event *event;
3899 struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
3900 unsigned long flags;
3902 raw_spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
3904 event = rb_iter_peek(iter, ts);
3908 if (event->type_len == RINGBUF_TYPE_PADDING)
3911 rb_advance_iter(iter);
3913 raw_spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
3917 EXPORT_SYMBOL_GPL(ring_buffer_read);
3920 * ring_buffer_size - return the size of the ring buffer (in bytes)
3921 * @buffer: The ring buffer.
3923 unsigned long ring_buffer_size(struct ring_buffer *buffer, int cpu)
3926 * Earlier, this method returned
3927 * BUF_PAGE_SIZE * buffer->nr_pages
3928 * Since the nr_pages field is now removed, we have converted this to
3929 * return the per cpu buffer value.
3931 if (!cpumask_test_cpu(cpu, buffer->cpumask))
3934 return BUF_PAGE_SIZE * buffer->buffers[cpu]->nr_pages;
3936 EXPORT_SYMBOL_GPL(ring_buffer_size);
3939 rb_reset_cpu(struct ring_buffer_per_cpu *cpu_buffer)
3941 rb_head_page_deactivate(cpu_buffer);
3943 cpu_buffer->head_page
3944 = list_entry(cpu_buffer->pages, struct buffer_page, list);
3945 local_set(&cpu_buffer->head_page->write, 0);
3946 local_set(&cpu_buffer->head_page->entries, 0);
3947 local_set(&cpu_buffer->head_page->page->commit, 0);
3949 cpu_buffer->head_page->read = 0;
3951 cpu_buffer->tail_page = cpu_buffer->head_page;
3952 cpu_buffer->commit_page = cpu_buffer->head_page;
3954 INIT_LIST_HEAD(&cpu_buffer->reader_page->list);
3955 INIT_LIST_HEAD(&cpu_buffer->new_pages);
3956 local_set(&cpu_buffer->reader_page->write, 0);
3957 local_set(&cpu_buffer->reader_page->entries, 0);
3958 local_set(&cpu_buffer->reader_page->page->commit, 0);
3959 cpu_buffer->reader_page->read = 0;
3961 local_set(&cpu_buffer->entries_bytes, 0);
3962 local_set(&cpu_buffer->overrun, 0);
3963 local_set(&cpu_buffer->commit_overrun, 0);
3964 local_set(&cpu_buffer->dropped_events, 0);
3965 local_set(&cpu_buffer->entries, 0);
3966 local_set(&cpu_buffer->committing, 0);
3967 local_set(&cpu_buffer->commits, 0);
3968 cpu_buffer->read = 0;
3969 cpu_buffer->read_bytes = 0;
3971 cpu_buffer->write_stamp = 0;
3972 cpu_buffer->read_stamp = 0;
3974 cpu_buffer->lost_events = 0;
3975 cpu_buffer->last_overrun = 0;
3977 rb_head_page_activate(cpu_buffer);
3981 * ring_buffer_reset_cpu - reset a ring buffer per CPU buffer
3982 * @buffer: The ring buffer to reset a per cpu buffer of
3983 * @cpu: The CPU buffer to be reset
3985 void ring_buffer_reset_cpu(struct ring_buffer *buffer, int cpu)
3987 struct ring_buffer_per_cpu *cpu_buffer = buffer->buffers[cpu];
3988 unsigned long flags;
3990 if (!cpumask_test_cpu(cpu, buffer->cpumask))
3993 atomic_inc(&buffer->resize_disabled);
3994 atomic_inc(&cpu_buffer->record_disabled);
3996 /* Make sure all commits have finished */
3997 synchronize_sched();
3999 raw_spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
4001 if (RB_WARN_ON(cpu_buffer, local_read(&cpu_buffer->committing)))
4004 arch_spin_lock(&cpu_buffer->lock);
4006 rb_reset_cpu(cpu_buffer);
4008 arch_spin_unlock(&cpu_buffer->lock);
4011 raw_spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
4013 atomic_dec(&cpu_buffer->record_disabled);
4014 atomic_dec(&buffer->resize_disabled);
4016 EXPORT_SYMBOL_GPL(ring_buffer_reset_cpu);
4019 * ring_buffer_reset - reset a ring buffer
4020 * @buffer: The ring buffer to reset all cpu buffers
4022 void ring_buffer_reset(struct ring_buffer *buffer)
4026 for_each_buffer_cpu(buffer, cpu)
4027 ring_buffer_reset_cpu(buffer, cpu);
4029 EXPORT_SYMBOL_GPL(ring_buffer_reset);
4032 * rind_buffer_empty - is the ring buffer empty?
4033 * @buffer: The ring buffer to test
4035 int ring_buffer_empty(struct ring_buffer *buffer)
4037 struct ring_buffer_per_cpu *cpu_buffer;
4038 unsigned long flags;
4043 dolock = rb_ok_to_lock();
4045 /* yes this is racy, but if you don't like the race, lock the buffer */
4046 for_each_buffer_cpu(buffer, cpu) {
4047 cpu_buffer = buffer->buffers[cpu];
4048 local_irq_save(flags);
4050 raw_spin_lock(&cpu_buffer->reader_lock);
4051 ret = rb_per_cpu_empty(cpu_buffer);
4053 raw_spin_unlock(&cpu_buffer->reader_lock);
4054 local_irq_restore(flags);
4062 EXPORT_SYMBOL_GPL(ring_buffer_empty);
4065 * ring_buffer_empty_cpu - is a cpu buffer of a ring buffer empty?
4066 * @buffer: The ring buffer
4067 * @cpu: The CPU buffer to test
4069 int ring_buffer_empty_cpu(struct ring_buffer *buffer, int cpu)
4071 struct ring_buffer_per_cpu *cpu_buffer;
4072 unsigned long flags;
4076 if (!cpumask_test_cpu(cpu, buffer->cpumask))
4079 dolock = rb_ok_to_lock();
4081 cpu_buffer = buffer->buffers[cpu];
4082 local_irq_save(flags);
4084 raw_spin_lock(&cpu_buffer->reader_lock);
4085 ret = rb_per_cpu_empty(cpu_buffer);
4087 raw_spin_unlock(&cpu_buffer->reader_lock);
4088 local_irq_restore(flags);
4092 EXPORT_SYMBOL_GPL(ring_buffer_empty_cpu);
4094 #ifdef CONFIG_RING_BUFFER_ALLOW_SWAP
4096 * ring_buffer_swap_cpu - swap a CPU buffer between two ring buffers
4097 * @buffer_a: One buffer to swap with
4098 * @buffer_b: The other buffer to swap with
4100 * This function is useful for tracers that want to take a "snapshot"
4101 * of a CPU buffer and has another back up buffer lying around.
4102 * it is expected that the tracer handles the cpu buffer not being
4103 * used at the moment.
4105 int ring_buffer_swap_cpu(struct ring_buffer *buffer_a,
4106 struct ring_buffer *buffer_b, int cpu)
4108 struct ring_buffer_per_cpu *cpu_buffer_a;
4109 struct ring_buffer_per_cpu *cpu_buffer_b;
4112 if (!cpumask_test_cpu(cpu, buffer_a->cpumask) ||
4113 !cpumask_test_cpu(cpu, buffer_b->cpumask))
4116 cpu_buffer_a = buffer_a->buffers[cpu];
4117 cpu_buffer_b = buffer_b->buffers[cpu];
4119 /* At least make sure the two buffers are somewhat the same */
4120 if (cpu_buffer_a->nr_pages != cpu_buffer_b->nr_pages)
4125 if (ring_buffer_flags != RB_BUFFERS_ON)
4128 if (atomic_read(&buffer_a->record_disabled))
4131 if (atomic_read(&buffer_b->record_disabled))
4134 if (atomic_read(&cpu_buffer_a->record_disabled))
4137 if (atomic_read(&cpu_buffer_b->record_disabled))
4141 * We can't do a synchronize_sched here because this
4142 * function can be called in atomic context.
4143 * Normally this will be called from the same CPU as cpu.
4144 * If not it's up to the caller to protect this.
4146 atomic_inc(&cpu_buffer_a->record_disabled);
4147 atomic_inc(&cpu_buffer_b->record_disabled);
4150 if (local_read(&cpu_buffer_a->committing))
4152 if (local_read(&cpu_buffer_b->committing))
4155 buffer_a->buffers[cpu] = cpu_buffer_b;
4156 buffer_b->buffers[cpu] = cpu_buffer_a;
4158 cpu_buffer_b->buffer = buffer_a;
4159 cpu_buffer_a->buffer = buffer_b;
4164 atomic_dec(&cpu_buffer_a->record_disabled);
4165 atomic_dec(&cpu_buffer_b->record_disabled);
4169 EXPORT_SYMBOL_GPL(ring_buffer_swap_cpu);
4170 #endif /* CONFIG_RING_BUFFER_ALLOW_SWAP */
4173 * ring_buffer_alloc_read_page - allocate a page to read from buffer
4174 * @buffer: the buffer to allocate for.
4176 * This function is used in conjunction with ring_buffer_read_page.
4177 * When reading a full page from the ring buffer, these functions
4178 * can be used to speed up the process. The calling function should
4179 * allocate a few pages first with this function. Then when it
4180 * needs to get pages from the ring buffer, it passes the result
4181 * of this function into ring_buffer_read_page, which will swap
4182 * the page that was allocated, with the read page of the buffer.
4185 * The page allocated, or NULL on error.
4187 void *ring_buffer_alloc_read_page(struct ring_buffer *buffer, int cpu)
4189 struct buffer_data_page *bpage;
4192 page = alloc_pages_node(cpu_to_node(cpu),
4193 GFP_KERNEL | __GFP_NORETRY, 0);
4197 bpage = page_address(page);
4199 rb_init_page(bpage);
4203 EXPORT_SYMBOL_GPL(ring_buffer_alloc_read_page);
4206 * ring_buffer_free_read_page - free an allocated read page
4207 * @buffer: the buffer the page was allocate for
4208 * @data: the page to free
4210 * Free a page allocated from ring_buffer_alloc_read_page.
4212 void ring_buffer_free_read_page(struct ring_buffer *buffer, void *data)
4214 free_page((unsigned long)data);
4216 EXPORT_SYMBOL_GPL(ring_buffer_free_read_page);
4219 * ring_buffer_read_page - extract a page from the ring buffer
4220 * @buffer: buffer to extract from
4221 * @data_page: the page to use allocated from ring_buffer_alloc_read_page
4222 * @len: amount to extract
4223 * @cpu: the cpu of the buffer to extract
4224 * @full: should the extraction only happen when the page is full.
4226 * This function will pull out a page from the ring buffer and consume it.
4227 * @data_page must be the address of the variable that was returned
4228 * from ring_buffer_alloc_read_page. This is because the page might be used
4229 * to swap with a page in the ring buffer.
4232 * rpage = ring_buffer_alloc_read_page(buffer);
4235 * ret = ring_buffer_read_page(buffer, &rpage, len, cpu, 0);
4237 * process_page(rpage, ret);
4239 * When @full is set, the function will not return true unless
4240 * the writer is off the reader page.
4242 * Note: it is up to the calling functions to handle sleeps and wakeups.
4243 * The ring buffer can be used anywhere in the kernel and can not
4244 * blindly call wake_up. The layer that uses the ring buffer must be
4245 * responsible for that.
4248 * >=0 if data has been transferred, returns the offset of consumed data.
4249 * <0 if no data has been transferred.
4251 int ring_buffer_read_page(struct ring_buffer *buffer,
4252 void **data_page, size_t len, int cpu, int full)
4254 struct ring_buffer_per_cpu *cpu_buffer = buffer->buffers[cpu];
4255 struct ring_buffer_event *event;
4256 struct buffer_data_page *bpage;
4257 struct buffer_page *reader;
4258 unsigned long missed_events;
4259 unsigned long flags;
4260 unsigned int commit;
4265 if (!cpumask_test_cpu(cpu, buffer->cpumask))
4269 * If len is not big enough to hold the page header, then
4270 * we can not copy anything.
4272 if (len <= BUF_PAGE_HDR_SIZE)
4275 len -= BUF_PAGE_HDR_SIZE;
4284 raw_spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
4286 reader = rb_get_reader_page(cpu_buffer);
4290 event = rb_reader_event(cpu_buffer);
4292 read = reader->read;
4293 commit = rb_page_commit(reader);
4295 /* Check if any events were dropped */
4296 missed_events = cpu_buffer->lost_events;
4299 * If this page has been partially read or
4300 * if len is not big enough to read the rest of the page or
4301 * a writer is still on the page, then
4302 * we must copy the data from the page to the buffer.
4303 * Otherwise, we can simply swap the page with the one passed in.
4305 if (read || (len < (commit - read)) ||
4306 cpu_buffer->reader_page == cpu_buffer->commit_page) {
4307 struct buffer_data_page *rpage = cpu_buffer->reader_page->page;
4308 unsigned int rpos = read;
4309 unsigned int pos = 0;
4315 if (len > (commit - read))
4316 len = (commit - read);
4318 /* Always keep the time extend and data together */
4319 size = rb_event_ts_length(event);
4324 /* save the current timestamp, since the user will need it */
4325 save_timestamp = cpu_buffer->read_stamp;
4327 /* Need to copy one event at a time */
4329 /* We need the size of one event, because
4330 * rb_advance_reader only advances by one event,
4331 * whereas rb_event_ts_length may include the size of
4332 * one or two events.
4333 * We have already ensured there's enough space if this
4334 * is a time extend. */
4335 size = rb_event_length(event);
4336 memcpy(bpage->data + pos, rpage->data + rpos, size);
4340 rb_advance_reader(cpu_buffer);
4341 rpos = reader->read;
4347 event = rb_reader_event(cpu_buffer);
4348 /* Always keep the time extend and data together */
4349 size = rb_event_ts_length(event);
4350 } while (len >= size);
4353 local_set(&bpage->commit, pos);
4354 bpage->time_stamp = save_timestamp;
4356 /* we copied everything to the beginning */
4359 /* update the entry counter */
4360 cpu_buffer->read += rb_page_entries(reader);
4361 cpu_buffer->read_bytes += BUF_PAGE_SIZE;
4363 /* swap the pages */
4364 rb_init_page(bpage);
4365 bpage = reader->page;
4366 reader->page = *data_page;
4367 local_set(&reader->write, 0);
4368 local_set(&reader->entries, 0);
4373 * Use the real_end for the data size,
4374 * This gives us a chance to store the lost events
4377 if (reader->real_end)
4378 local_set(&bpage->commit, reader->real_end);
4382 cpu_buffer->lost_events = 0;
4384 commit = local_read(&bpage->commit);
4386 * Set a flag in the commit field if we lost events
4388 if (missed_events) {
4389 /* If there is room at the end of the page to save the
4390 * missed events, then record it there.
4392 if (BUF_PAGE_SIZE - commit >= sizeof(missed_events)) {
4393 memcpy(&bpage->data[commit], &missed_events,
4394 sizeof(missed_events));
4395 local_add(RB_MISSED_STORED, &bpage->commit);
4396 commit += sizeof(missed_events);
4398 local_add(RB_MISSED_EVENTS, &bpage->commit);
4402 * This page may be off to user land. Zero it out here.
4404 if (commit < BUF_PAGE_SIZE)
4405 memset(&bpage->data[commit], 0, BUF_PAGE_SIZE - commit);
4408 raw_spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
4413 EXPORT_SYMBOL_GPL(ring_buffer_read_page);
4415 #ifdef CONFIG_HOTPLUG_CPU
4416 static int rb_cpu_notify(struct notifier_block *self,
4417 unsigned long action, void *hcpu)
4419 struct ring_buffer *buffer =
4420 container_of(self, struct ring_buffer, cpu_notify);
4421 long cpu = (long)hcpu;
4422 int cpu_i, nr_pages_same;
4423 unsigned int nr_pages;
4426 case CPU_UP_PREPARE:
4427 case CPU_UP_PREPARE_FROZEN:
4428 if (cpumask_test_cpu(cpu, buffer->cpumask))
4433 /* check if all cpu sizes are same */
4434 for_each_buffer_cpu(buffer, cpu_i) {
4435 /* fill in the size from first enabled cpu */
4437 nr_pages = buffer->buffers[cpu_i]->nr_pages;
4438 if (nr_pages != buffer->buffers[cpu_i]->nr_pages) {
4443 /* allocate minimum pages, user can later expand it */
4446 buffer->buffers[cpu] =
4447 rb_allocate_cpu_buffer(buffer, nr_pages, cpu);
4448 if (!buffer->buffers[cpu]) {
4449 WARN(1, "failed to allocate ring buffer on CPU %ld\n",
4454 cpumask_set_cpu(cpu, buffer->cpumask);
4456 case CPU_DOWN_PREPARE:
4457 case CPU_DOWN_PREPARE_FROZEN:
4460 * If we were to free the buffer, then the user would
4461 * lose any trace that was in the buffer.