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/irq_work.h>
12 #include <linux/debugfs.h>
13 #include <linux/uaccess.h>
14 #include <linux/hardirq.h>
15 #include <linux/kthread.h> /* for self test */
16 #include <linux/kmemcheck.h>
17 #include <linux/module.h>
18 #include <linux/percpu.h>
19 #include <linux/mutex.h>
20 #include <linux/delay.h>
21 #include <linux/slab.h>
22 #include <linux/init.h>
23 #include <linux/hash.h>
24 #include <linux/list.h>
25 #include <linux/cpu.h>
28 #include <asm/local.h>
30 #ifdef CONFIG_MTK_EXTMEM
31 extern void* extmem_malloc_page_align(size_t bytes
);
32 extern void extmem_free(void* mem
);
35 static void update_pages_handler(struct work_struct
*work
);
38 * The ring buffer header is special. We must manually up keep it.
40 int ring_buffer_print_entry_header(struct trace_seq
*s
)
44 ret
= trace_seq_printf(s
, "# compressed entry header\n");
45 ret
= trace_seq_printf(s
, "\ttype_len : 5 bits\n");
46 ret
= trace_seq_printf(s
, "\ttime_delta : 27 bits\n");
47 ret
= trace_seq_printf(s
, "\tarray : 32 bits\n");
48 ret
= trace_seq_printf(s
, "\n");
49 ret
= trace_seq_printf(s
, "\tpadding : type == %d\n",
50 RINGBUF_TYPE_PADDING
);
51 ret
= trace_seq_printf(s
, "\ttime_extend : type == %d\n",
52 RINGBUF_TYPE_TIME_EXTEND
);
53 ret
= trace_seq_printf(s
, "\tdata max type_len == %d\n",
54 RINGBUF_TYPE_DATA_TYPE_LEN_MAX
);
60 * The ring buffer is made up of a list of pages. A separate list of pages is
61 * allocated for each CPU. A writer may only write to a buffer that is
62 * associated with the CPU it is currently executing on. A reader may read
63 * from any per cpu buffer.
65 * The reader is special. For each per cpu buffer, the reader has its own
66 * reader page. When a reader has read the entire reader page, this reader
67 * page is swapped with another page in the ring buffer.
69 * Now, as long as the writer is off the reader page, the reader can do what
70 * ever it wants with that page. The writer will never write to that page
71 * again (as long as it is out of the ring buffer).
73 * Here's some silly ASCII art.
76 * |reader| RING BUFFER
78 * +------+ +---+ +---+ +---+
87 * |reader| RING BUFFER
88 * |page |------------------v
89 * +------+ +---+ +---+ +---+
98 * |reader| RING BUFFER
99 * |page |------------------v
100 * +------+ +---+ +---+ +---+
102 * | +---+ +---+ +---+
105 * +------------------------------+
109 * |buffer| RING BUFFER
110 * |page |------------------v
111 * +------+ +---+ +---+ +---+
113 * | New +---+ +---+ +---+
116 * +------------------------------+
119 * After we make this swap, the reader can hand this page off to the splice
120 * code and be done with it. It can even allocate a new page if it needs to
121 * and swap that into the ring buffer.
123 * We will be using cmpxchg soon to make all this lockless.
128 * A fast way to enable or disable all ring buffers is to
129 * call tracing_on or tracing_off. Turning off the ring buffers
130 * prevents all ring buffers from being recorded to.
131 * Turning this switch on, makes it OK to write to the
132 * ring buffer, if the ring buffer is enabled itself.
134 * There's three layers that must be on in order to write
135 * to the ring buffer.
137 * 1) This global flag must be set.
138 * 2) The ring buffer must be enabled for recording.
139 * 3) The per cpu buffer must be enabled for recording.
141 * In case of an anomaly, this global flag has a bit set that
142 * will permantly disable all ring buffers.
146 * Global flag to disable all recording to ring buffers
147 * This has two bits: ON, DISABLED
151 * 0 0 : ring buffers are off
152 * 1 0 : ring buffers are on
153 * X 1 : ring buffers are permanently disabled
157 RB_BUFFERS_ON_BIT
= 0,
158 RB_BUFFERS_DISABLED_BIT
= 1,
162 RB_BUFFERS_ON
= 1 << RB_BUFFERS_ON_BIT
,
163 RB_BUFFERS_DISABLED
= 1 << RB_BUFFERS_DISABLED_BIT
,
166 static unsigned long ring_buffer_flags __read_mostly
= RB_BUFFERS_ON
;
168 /* Used for individual buffers (after the counter) */
169 #define RB_BUFFER_OFF (1 << 20)
171 #define BUF_PAGE_HDR_SIZE offsetof(struct buffer_data_page, data)
174 * tracing_off_permanent - permanently disable ring buffers
176 * This function, once called, will disable all ring buffers
179 void tracing_off_permanent(void)
181 set_bit(RB_BUFFERS_DISABLED_BIT
, &ring_buffer_flags
);
184 #define RB_EVNT_HDR_SIZE (offsetof(struct ring_buffer_event, array))
185 #define RB_ALIGNMENT 4U
186 #define RB_MAX_SMALL_DATA (RB_ALIGNMENT * RINGBUF_TYPE_DATA_TYPE_LEN_MAX)
187 #define RB_EVNT_MIN_SIZE 8U /* two 32bit words */
189 #ifndef CONFIG_HAVE_64BIT_ALIGNED_ACCESS
190 # define RB_FORCE_8BYTE_ALIGNMENT 0
191 # define RB_ARCH_ALIGNMENT RB_ALIGNMENT
193 # define RB_FORCE_8BYTE_ALIGNMENT 1
194 # define RB_ARCH_ALIGNMENT 8U
197 #define RB_ALIGN_DATA __aligned(RB_ARCH_ALIGNMENT)
199 /* define RINGBUF_TYPE_DATA for 'case RINGBUF_TYPE_DATA:' */
200 #define RINGBUF_TYPE_DATA 0 ... RINGBUF_TYPE_DATA_TYPE_LEN_MAX
203 RB_LEN_TIME_EXTEND
= 8,
204 RB_LEN_TIME_STAMP
= 16,
207 #define skip_time_extend(event) \
208 ((struct ring_buffer_event *)((char *)event + RB_LEN_TIME_EXTEND))
210 static inline int rb_null_event(struct ring_buffer_event
*event
)
212 return event
->type_len
== RINGBUF_TYPE_PADDING
&& !event
->time_delta
;
215 static void rb_event_set_padding(struct ring_buffer_event
*event
)
217 /* padding has a NULL time_delta */
218 event
->type_len
= RINGBUF_TYPE_PADDING
;
219 event
->time_delta
= 0;
223 rb_event_data_length(struct ring_buffer_event
*event
)
228 length
= event
->type_len
* RB_ALIGNMENT
;
230 length
= event
->array
[0];
231 return length
+ RB_EVNT_HDR_SIZE
;
235 * Return the length of the given event. Will return
236 * the length of the time extend if the event is a
239 static inline unsigned
240 rb_event_length(struct ring_buffer_event
*event
)
242 switch (event
->type_len
) {
243 case RINGBUF_TYPE_PADDING
:
244 if (rb_null_event(event
))
247 return event
->array
[0] + RB_EVNT_HDR_SIZE
;
249 case RINGBUF_TYPE_TIME_EXTEND
:
250 return RB_LEN_TIME_EXTEND
;
252 case RINGBUF_TYPE_TIME_STAMP
:
253 return RB_LEN_TIME_STAMP
;
255 case RINGBUF_TYPE_DATA
:
256 return rb_event_data_length(event
);
265 * Return total length of time extend and data,
266 * or just the event length for all other events.
268 static inline unsigned
269 rb_event_ts_length(struct ring_buffer_event
*event
)
273 if (event
->type_len
== RINGBUF_TYPE_TIME_EXTEND
) {
274 /* time extends include the data event after it */
275 len
= RB_LEN_TIME_EXTEND
;
276 event
= skip_time_extend(event
);
278 return len
+ rb_event_length(event
);
282 * ring_buffer_event_length - return the length of the event
283 * @event: the event to get the length of
285 * Returns the size of the data load of a data event.
286 * If the event is something other than a data event, it
287 * returns the size of the event itself. With the exception
288 * of a TIME EXTEND, where it still returns the size of the
289 * data load of the data event after it.
291 unsigned ring_buffer_event_length(struct ring_buffer_event
*event
)
295 if (event
->type_len
== RINGBUF_TYPE_TIME_EXTEND
)
296 event
= skip_time_extend(event
);
298 length
= rb_event_length(event
);
299 if (event
->type_len
> RINGBUF_TYPE_DATA_TYPE_LEN_MAX
)
301 length
-= RB_EVNT_HDR_SIZE
;
302 if (length
> RB_MAX_SMALL_DATA
+ sizeof(event
->array
[0]))
303 length
-= sizeof(event
->array
[0]);
306 EXPORT_SYMBOL_GPL(ring_buffer_event_length
);
308 /* inline for ring buffer fast paths */
310 rb_event_data(struct ring_buffer_event
*event
)
312 if (event
->type_len
== RINGBUF_TYPE_TIME_EXTEND
)
313 event
= skip_time_extend(event
);
314 BUG_ON(event
->type_len
> RINGBUF_TYPE_DATA_TYPE_LEN_MAX
);
315 /* If length is in len field, then array[0] has the data */
317 return (void *)&event
->array
[0];
318 /* Otherwise length is in array[0] and array[1] has the data */
319 return (void *)&event
->array
[1];
323 * ring_buffer_event_data - return the data of the event
324 * @event: the event to get the data from
326 void *ring_buffer_event_data(struct ring_buffer_event
*event
)
328 return rb_event_data(event
);
330 EXPORT_SYMBOL_GPL(ring_buffer_event_data
);
332 #define for_each_buffer_cpu(buffer, cpu) \
333 for_each_cpu(cpu, buffer->cpumask)
336 #define TS_MASK ((1ULL << TS_SHIFT) - 1)
337 #define TS_DELTA_TEST (~TS_MASK)
339 /* Flag when events were overwritten */
340 #define RB_MISSED_EVENTS (1 << 31)
341 /* Missed count stored at end */
342 #define RB_MISSED_STORED (1 << 30)
344 struct buffer_data_page
{
345 u64 time_stamp
; /* page time stamp */
346 local_t commit
; /* write committed index */
347 unsigned char data
[] RB_ALIGN_DATA
; /* data of buffer page */
351 * Note, the buffer_page list must be first. The buffer pages
352 * are allocated in cache lines, which means that each buffer
353 * page will be at the beginning of a cache line, and thus
354 * the least significant bits will be zero. We use this to
355 * add flags in the list struct pointers, to make the ring buffer
359 struct list_head list
; /* list of buffer pages */
360 local_t write
; /* index for next write */
361 unsigned read
; /* index for next read */
362 local_t entries
; /* entries on this page */
363 unsigned long real_end
; /* real end of data */
364 struct buffer_data_page
*page
; /* Actual data page */
368 * The buffer page counters, write and entries, must be reset
369 * atomically when crossing page boundaries. To synchronize this
370 * update, two counters are inserted into the number. One is
371 * the actual counter for the write position or count on the page.
373 * The other is a counter of updaters. Before an update happens
374 * the update partition of the counter is incremented. This will
375 * allow the updater to update the counter atomically.
377 * The counter is 20 bits, and the state data is 12.
379 #define RB_WRITE_MASK 0xfffff
380 #define RB_WRITE_INTCNT (1 << 20)
382 static void rb_init_page(struct buffer_data_page
*bpage
)
384 local_set(&bpage
->commit
, 0);
388 * ring_buffer_page_len - the size of data on the page.
389 * @page: The page to read
391 * Returns the amount of data on the page, including buffer page header.
393 size_t ring_buffer_page_len(void *page
)
395 return local_read(&((struct buffer_data_page
*)page
)->commit
)
400 * Also stolen from mm/slob.c. Thanks to Mathieu Desnoyers for pointing
403 static void free_buffer_page(struct buffer_page
*bpage
)
405 #ifdef CONFIG_MTK_EXTMEM
406 extmem_free((void*) bpage
->page
);
408 free_page((unsigned long)bpage
->page
);
414 * We need to fit the time_stamp delta into 27 bits.
416 static inline int test_time_stamp(u64 delta
)
418 if (delta
& TS_DELTA_TEST
)
423 #define BUF_PAGE_SIZE (PAGE_SIZE - BUF_PAGE_HDR_SIZE)
425 /* Max payload is BUF_PAGE_SIZE - header (8bytes) */
426 #define BUF_MAX_DATA_SIZE (BUF_PAGE_SIZE - (sizeof(u32) * 2))
428 int ring_buffer_print_page_header(struct trace_seq
*s
)
430 struct buffer_data_page field
;
433 ret
= trace_seq_printf(s
, "\tfield: u64 timestamp;\t"
434 "offset:0;\tsize:%u;\tsigned:%u;\n",
435 (unsigned int)sizeof(field
.time_stamp
),
436 (unsigned int)is_signed_type(u64
));
438 ret
= trace_seq_printf(s
, "\tfield: local_t commit;\t"
439 "offset:%u;\tsize:%u;\tsigned:%u;\n",
440 (unsigned int)offsetof(typeof(field
), commit
),
441 (unsigned int)sizeof(field
.commit
),
442 (unsigned int)is_signed_type(long));
444 ret
= trace_seq_printf(s
, "\tfield: int overwrite;\t"
445 "offset:%u;\tsize:%u;\tsigned:%u;\n",
446 (unsigned int)offsetof(typeof(field
), commit
),
448 (unsigned int)is_signed_type(long));
450 ret
= trace_seq_printf(s
, "\tfield: char data;\t"
451 "offset:%u;\tsize:%u;\tsigned:%u;\n",
452 (unsigned int)offsetof(typeof(field
), data
),
453 (unsigned int)BUF_PAGE_SIZE
,
454 (unsigned int)is_signed_type(char));
460 struct irq_work work
;
461 wait_queue_head_t waiters
;
462 bool waiters_pending
;
466 * head_page == tail_page && head == tail then buffer is empty.
468 struct ring_buffer_per_cpu
{
470 atomic_t record_disabled
;
471 struct ring_buffer
*buffer
;
472 raw_spinlock_t reader_lock
; /* serialize readers */
473 arch_spinlock_t lock
;
474 struct lock_class_key lock_key
;
475 unsigned int nr_pages
;
476 struct list_head
*pages
;
477 struct buffer_page
*head_page
; /* read from head */
478 struct buffer_page
*tail_page
; /* write to tail */
479 struct buffer_page
*commit_page
; /* committed pages */
480 struct buffer_page
*reader_page
;
481 unsigned long lost_events
;
482 unsigned long last_overrun
;
483 local_t entries_bytes
;
486 local_t commit_overrun
;
487 local_t dropped_events
;
491 unsigned long read_bytes
;
494 /* ring buffer pages to update, > 0 to add, < 0 to remove */
495 int nr_pages_to_update
;
496 struct list_head new_pages
; /* new pages to add */
497 struct work_struct update_pages_work
;
498 struct completion update_done
;
500 struct rb_irq_work irq_work
;
506 atomic_t record_disabled
;
507 atomic_t resize_disabled
;
508 cpumask_var_t cpumask
;
510 struct lock_class_key
*reader_lock_key
;
514 struct ring_buffer_per_cpu
**buffers
;
516 #ifdef CONFIG_HOTPLUG_CPU
517 struct notifier_block cpu_notify
;
521 struct rb_irq_work irq_work
;
524 struct ring_buffer_iter
{
525 struct ring_buffer_per_cpu
*cpu_buffer
;
527 struct buffer_page
*head_page
;
528 struct buffer_page
*cache_reader_page
;
529 unsigned long cache_read
;
534 * rb_wake_up_waiters - wake up tasks waiting for ring buffer input
536 * Schedules a delayed work to wake up any task that is blocked on the
537 * ring buffer waiters queue.
539 static void rb_wake_up_waiters(struct irq_work
*work
)
541 struct rb_irq_work
*rbwork
= container_of(work
, struct rb_irq_work
, work
);
543 wake_up_all(&rbwork
->waiters
);
547 * ring_buffer_wait - wait for input to the ring buffer
548 * @buffer: buffer to wait on
549 * @cpu: the cpu buffer to wait on
551 * If @cpu == RING_BUFFER_ALL_CPUS then the task will wake up as soon
552 * as data is added to any of the @buffer's cpu buffers. Otherwise
553 * it will wait for data to be added to a specific cpu buffer.
555 int ring_buffer_wait(struct ring_buffer
*buffer
, int cpu
)
557 struct ring_buffer_per_cpu
*cpu_buffer
;
559 struct rb_irq_work
*work
;
562 * Depending on what the caller is waiting for, either any
563 * data in any cpu buffer, or a specific buffer, put the
564 * caller on the appropriate wait queue.
566 if (cpu
== RING_BUFFER_ALL_CPUS
)
567 work
= &buffer
->irq_work
;
569 if (!cpumask_test_cpu(cpu
, buffer
->cpumask
))
571 cpu_buffer
= buffer
->buffers
[cpu
];
572 work
= &cpu_buffer
->irq_work
;
576 prepare_to_wait(&work
->waiters
, &wait
, TASK_INTERRUPTIBLE
);
579 * The events can happen in critical sections where
580 * checking a work queue can cause deadlocks.
581 * After adding a task to the queue, this flag is set
582 * only to notify events to try to wake up the queue
585 * We don't clear it even if the buffer is no longer
586 * empty. The flag only causes the next event to run
587 * irq_work to do the work queue wake up. The worse
588 * that can happen if we race with !trace_empty() is that
589 * an event will cause an irq_work to try to wake up
592 * There's no reason to protect this flag either, as
593 * the work queue and irq_work logic will do the necessary
594 * synchronization for the wake ups. The only thing
595 * that is necessary is that the wake up happens after
596 * a task has been queued. It's OK for spurious wake ups.
598 work
->waiters_pending
= true;
600 if ((cpu
== RING_BUFFER_ALL_CPUS
&& ring_buffer_empty(buffer
)) ||
601 (cpu
!= RING_BUFFER_ALL_CPUS
&& ring_buffer_empty_cpu(buffer
, cpu
)))
604 finish_wait(&work
->waiters
, &wait
);
609 * ring_buffer_poll_wait - poll on buffer input
610 * @buffer: buffer to wait on
611 * @cpu: the cpu buffer to wait on
612 * @filp: the file descriptor
613 * @poll_table: The poll descriptor
615 * If @cpu == RING_BUFFER_ALL_CPUS then the task will wake up as soon
616 * as data is added to any of the @buffer's cpu buffers. Otherwise
617 * it will wait for data to be added to a specific cpu buffer.
619 * Returns POLLIN | POLLRDNORM if data exists in the buffers,
622 int ring_buffer_poll_wait(struct ring_buffer
*buffer
, int cpu
,
623 struct file
*filp
, poll_table
*poll_table
)
625 struct ring_buffer_per_cpu
*cpu_buffer
;
626 struct rb_irq_work
*work
;
628 if (cpu
== RING_BUFFER_ALL_CPUS
)
629 work
= &buffer
->irq_work
;
631 if (!cpumask_test_cpu(cpu
, buffer
->cpumask
))
634 cpu_buffer
= buffer
->buffers
[cpu
];
635 work
= &cpu_buffer
->irq_work
;
638 work
->waiters_pending
= true;
639 poll_wait(filp
, &work
->waiters
, poll_table
);
641 if ((cpu
== RING_BUFFER_ALL_CPUS
&& !ring_buffer_empty(buffer
)) ||
642 (cpu
!= RING_BUFFER_ALL_CPUS
&& !ring_buffer_empty_cpu(buffer
, cpu
)))
643 return POLLIN
| POLLRDNORM
;
647 /* buffer may be either ring_buffer or ring_buffer_per_cpu */
648 #define RB_WARN_ON(b, cond) \
650 int _____ret = unlikely(cond); \
652 if (__same_type(*(b), struct ring_buffer_per_cpu)) { \
653 struct ring_buffer_per_cpu *__b = \
655 atomic_inc(&__b->buffer->record_disabled); \
657 atomic_inc(&b->record_disabled); \
663 /* Up this if you want to test the TIME_EXTENTS and normalization */
664 #define DEBUG_SHIFT 0
666 static inline u64
rb_time_stamp(struct ring_buffer
*buffer
)
668 /* shift to debug/test normalization and TIME_EXTENTS */
669 return buffer
->clock() << DEBUG_SHIFT
;
672 u64
ring_buffer_time_stamp(struct ring_buffer
*buffer
, int cpu
)
676 preempt_disable_notrace();
677 time
= rb_time_stamp(buffer
);
678 preempt_enable_no_resched_notrace();
682 EXPORT_SYMBOL_GPL(ring_buffer_time_stamp
);
684 void ring_buffer_normalize_time_stamp(struct ring_buffer
*buffer
,
687 /* Just stupid testing the normalize function and deltas */
690 EXPORT_SYMBOL_GPL(ring_buffer_normalize_time_stamp
);
693 * Making the ring buffer lockless makes things tricky.
694 * Although writes only happen on the CPU that they are on,
695 * and they only need to worry about interrupts. Reads can
698 * The reader page is always off the ring buffer, but when the
699 * reader finishes with a page, it needs to swap its page with
700 * a new one from the buffer. The reader needs to take from
701 * the head (writes go to the tail). But if a writer is in overwrite
702 * mode and wraps, it must push the head page forward.
704 * Here lies the problem.
706 * The reader must be careful to replace only the head page, and
707 * not another one. As described at the top of the file in the
708 * ASCII art, the reader sets its old page to point to the next
709 * page after head. It then sets the page after head to point to
710 * the old reader page. But if the writer moves the head page
711 * during this operation, the reader could end up with the tail.
713 * We use cmpxchg to help prevent this race. We also do something
714 * special with the page before head. We set the LSB to 1.
716 * When the writer must push the page forward, it will clear the
717 * bit that points to the head page, move the head, and then set
718 * the bit that points to the new head page.
720 * We also don't want an interrupt coming in and moving the head
721 * page on another writer. Thus we use the second LSB to catch
724 * head->list->prev->next bit 1 bit 0
727 * Points to head page 0 1
730 * Note we can not trust the prev pointer of the head page, because:
732 * +----+ +-----+ +-----+
733 * | |------>| T |---X--->| N |
735 * +----+ +-----+ +-----+
738 * +----------| R |----------+ |
742 * Key: ---X--> HEAD flag set in pointer
747 * (see __rb_reserve_next() to see where this happens)
749 * What the above shows is that the reader just swapped out
750 * the reader page with a page in the buffer, but before it
751 * could make the new header point back to the new page added
752 * it was preempted by a writer. The writer moved forward onto
753 * the new page added by the reader and is about to move forward
756 * You can see, it is legitimate for the previous pointer of
757 * the head (or any page) not to point back to itself. But only
761 #define RB_PAGE_NORMAL 0UL
762 #define RB_PAGE_HEAD 1UL
763 #define RB_PAGE_UPDATE 2UL
766 #define RB_FLAG_MASK 3UL
768 /* PAGE_MOVED is not part of the mask */
769 #define RB_PAGE_MOVED 4UL
772 * rb_list_head - remove any bit
774 static struct list_head
*rb_list_head(struct list_head
*list
)
776 unsigned long val
= (unsigned long)list
;
778 return (struct list_head
*)(val
& ~RB_FLAG_MASK
);
782 * rb_is_head_page - test if the given page is the head page
784 * Because the reader may move the head_page pointer, we can
785 * not trust what the head page is (it may be pointing to
786 * the reader page). But if the next page is a header page,
787 * its flags will be non zero.
790 rb_is_head_page(struct ring_buffer_per_cpu
*cpu_buffer
,
791 struct buffer_page
*page
, struct list_head
*list
)
795 val
= (unsigned long)list
->next
;
797 if ((val
& ~RB_FLAG_MASK
) != (unsigned long)&page
->list
)
798 return RB_PAGE_MOVED
;
800 return val
& RB_FLAG_MASK
;
806 * The unique thing about the reader page, is that, if the
807 * writer is ever on it, the previous pointer never points
808 * back to the reader page.
810 static int rb_is_reader_page(struct buffer_page
*page
)
812 struct list_head
*list
= page
->list
.prev
;
814 return rb_list_head(list
->next
) != &page
->list
;
818 * rb_set_list_to_head - set a list_head to be pointing to head.
820 static void rb_set_list_to_head(struct ring_buffer_per_cpu
*cpu_buffer
,
821 struct list_head
*list
)
825 ptr
= (unsigned long *)&list
->next
;
826 *ptr
|= RB_PAGE_HEAD
;
827 *ptr
&= ~RB_PAGE_UPDATE
;
831 * rb_head_page_activate - sets up head page
833 static void rb_head_page_activate(struct ring_buffer_per_cpu
*cpu_buffer
)
835 struct buffer_page
*head
;
837 head
= cpu_buffer
->head_page
;
842 * Set the previous list pointer to have the HEAD flag.
844 rb_set_list_to_head(cpu_buffer
, head
->list
.prev
);
847 static void rb_list_head_clear(struct list_head
*list
)
849 unsigned long *ptr
= (unsigned long *)&list
->next
;
851 *ptr
&= ~RB_FLAG_MASK
;
855 * rb_head_page_dactivate - clears head page ptr (for free list)
858 rb_head_page_deactivate(struct ring_buffer_per_cpu
*cpu_buffer
)
860 struct list_head
*hd
;
862 /* Go through the whole list and clear any pointers found. */
863 rb_list_head_clear(cpu_buffer
->pages
);
865 list_for_each(hd
, cpu_buffer
->pages
)
866 rb_list_head_clear(hd
);
869 static int rb_head_page_set(struct ring_buffer_per_cpu
*cpu_buffer
,
870 struct buffer_page
*head
,
871 struct buffer_page
*prev
,
872 int old_flag
, int new_flag
)
874 struct list_head
*list
;
875 unsigned long val
= (unsigned long)&head
->list
;
880 val
&= ~RB_FLAG_MASK
;
882 ret
= cmpxchg((unsigned long *)&list
->next
,
883 val
| old_flag
, val
| new_flag
);
885 /* check if the reader took the page */
886 if ((ret
& ~RB_FLAG_MASK
) != val
)
887 return RB_PAGE_MOVED
;
889 return ret
& RB_FLAG_MASK
;
892 static int rb_head_page_set_update(struct ring_buffer_per_cpu
*cpu_buffer
,
893 struct buffer_page
*head
,
894 struct buffer_page
*prev
,
897 return rb_head_page_set(cpu_buffer
, head
, prev
,
898 old_flag
, RB_PAGE_UPDATE
);
901 static int rb_head_page_set_head(struct ring_buffer_per_cpu
*cpu_buffer
,
902 struct buffer_page
*head
,
903 struct buffer_page
*prev
,
906 return rb_head_page_set(cpu_buffer
, head
, prev
,
907 old_flag
, RB_PAGE_HEAD
);
910 static int rb_head_page_set_normal(struct ring_buffer_per_cpu
*cpu_buffer
,
911 struct buffer_page
*head
,
912 struct buffer_page
*prev
,
915 return rb_head_page_set(cpu_buffer
, head
, prev
,
916 old_flag
, RB_PAGE_NORMAL
);
919 static inline void rb_inc_page(struct ring_buffer_per_cpu
*cpu_buffer
,
920 struct buffer_page
**bpage
)
922 struct list_head
*p
= rb_list_head((*bpage
)->list
.next
);
924 *bpage
= list_entry(p
, struct buffer_page
, list
);
927 static struct buffer_page
*
928 rb_set_head_page(struct ring_buffer_per_cpu
*cpu_buffer
)
930 struct buffer_page
*head
;
931 struct buffer_page
*page
;
932 struct list_head
*list
;
935 if (RB_WARN_ON(cpu_buffer
, !cpu_buffer
->head_page
))
939 list
= cpu_buffer
->pages
;
940 if (RB_WARN_ON(cpu_buffer
, rb_list_head(list
->prev
->next
) != list
))
943 page
= head
= cpu_buffer
->head_page
;
945 * It is possible that the writer moves the header behind
946 * where we started, and we miss in one loop.
947 * A second loop should grab the header, but we'll do
948 * three loops just because I'm paranoid.
950 for (i
= 0; i
< 3; i
++) {
952 if (rb_is_head_page(cpu_buffer
, page
, page
->list
.prev
)) {
953 cpu_buffer
->head_page
= page
;
956 rb_inc_page(cpu_buffer
, &page
);
957 } while (page
!= head
);
960 RB_WARN_ON(cpu_buffer
, 1);
965 static int rb_head_page_replace(struct buffer_page
*old
,
966 struct buffer_page
*new)
968 unsigned long *ptr
= (unsigned long *)&old
->list
.prev
->next
;
972 val
= *ptr
& ~RB_FLAG_MASK
;
975 ret
= cmpxchg(ptr
, val
, (unsigned long)&new->list
);
981 * rb_tail_page_update - move the tail page forward
983 * Returns 1 if moved tail page, 0 if someone else did.
985 static int rb_tail_page_update(struct ring_buffer_per_cpu
*cpu_buffer
,
986 struct buffer_page
*tail_page
,
987 struct buffer_page
*next_page
)
989 struct buffer_page
*old_tail
;
990 unsigned long old_entries
;
991 unsigned long old_write
;
995 * The tail page now needs to be moved forward.
997 * We need to reset the tail page, but without messing
998 * with possible erasing of data brought in by interrupts
999 * that have moved the tail page and are currently on it.
1001 * We add a counter to the write field to denote this.
1003 old_write
= local_add_return(RB_WRITE_INTCNT
, &next_page
->write
);
1004 old_entries
= local_add_return(RB_WRITE_INTCNT
, &next_page
->entries
);
1007 * Just make sure we have seen our old_write and synchronize
1008 * with any interrupts that come in.
1013 * If the tail page is still the same as what we think
1014 * it is, then it is up to us to update the tail
1017 if (tail_page
== cpu_buffer
->tail_page
) {
1018 /* Zero the write counter */
1019 unsigned long val
= old_write
& ~RB_WRITE_MASK
;
1020 unsigned long eval
= old_entries
& ~RB_WRITE_MASK
;
1023 * This will only succeed if an interrupt did
1024 * not come in and change it. In which case, we
1025 * do not want to modify it.
1027 * We add (void) to let the compiler know that we do not care
1028 * about the return value of these functions. We use the
1029 * cmpxchg to only update if an interrupt did not already
1030 * do it for us. If the cmpxchg fails, we don't care.
1032 (void)local_cmpxchg(&next_page
->write
, old_write
, val
);
1033 (void)local_cmpxchg(&next_page
->entries
, old_entries
, eval
);
1036 * No need to worry about races with clearing out the commit.
1037 * it only can increment when a commit takes place. But that
1038 * only happens in the outer most nested commit.
1040 local_set(&next_page
->page
->commit
, 0);
1042 old_tail
= cmpxchg(&cpu_buffer
->tail_page
,
1043 tail_page
, next_page
);
1045 if (old_tail
== tail_page
)
1052 static int rb_check_bpage(struct ring_buffer_per_cpu
*cpu_buffer
,
1053 struct buffer_page
*bpage
)
1055 unsigned long val
= (unsigned long)bpage
;
1057 if (RB_WARN_ON(cpu_buffer
, val
& RB_FLAG_MASK
))
1064 * rb_check_list - make sure a pointer to a list has the last bits zero
1066 static int rb_check_list(struct ring_buffer_per_cpu
*cpu_buffer
,
1067 struct list_head
*list
)
1069 if (RB_WARN_ON(cpu_buffer
, rb_list_head(list
->prev
) != list
->prev
))
1071 if (RB_WARN_ON(cpu_buffer
, rb_list_head(list
->next
) != list
->next
))
1077 * check_pages - integrity check of buffer pages
1078 * @cpu_buffer: CPU buffer with pages to test
1080 * As a safety measure we check to make sure the data pages have not
1083 static int rb_check_pages(struct ring_buffer_per_cpu
*cpu_buffer
)
1085 struct list_head
*head
= cpu_buffer
->pages
;
1086 struct buffer_page
*bpage
, *tmp
;
1088 /* Reset the head page if it exists */
1089 if (cpu_buffer
->head_page
)
1090 rb_set_head_page(cpu_buffer
);
1092 rb_head_page_deactivate(cpu_buffer
);
1094 if (RB_WARN_ON(cpu_buffer
, head
->next
->prev
!= head
))
1096 if (RB_WARN_ON(cpu_buffer
, head
->prev
->next
!= head
))
1099 if (rb_check_list(cpu_buffer
, head
))
1102 list_for_each_entry_safe(bpage
, tmp
, head
, list
) {
1103 if (RB_WARN_ON(cpu_buffer
,
1104 bpage
->list
.next
->prev
!= &bpage
->list
))
1106 if (RB_WARN_ON(cpu_buffer
,
1107 bpage
->list
.prev
->next
!= &bpage
->list
))
1109 if (rb_check_list(cpu_buffer
, &bpage
->list
))
1113 rb_head_page_activate(cpu_buffer
);
1118 static int __rb_allocate_pages(int nr_pages
, struct list_head
*pages
, int cpu
)
1121 struct buffer_page
*bpage
, *tmp
;
1123 for (i
= 0; i
< nr_pages
; i
++) {
1124 #if !defined (CONFIG_MTK_EXTMEM)
1128 * __GFP_NORETRY flag makes sure that the allocation fails
1129 * gracefully without invoking oom-killer and the system is
1132 bpage
= kzalloc_node(ALIGN(sizeof(*bpage
), cache_line_size()),
1133 GFP_KERNEL
| __GFP_NORETRY
,
1138 list_add(&bpage
->list
, pages
);
1140 #ifdef CONFIG_MTK_EXTMEM
1141 bpage
->page
= extmem_malloc_page_align(PAGE_SIZE
);
1142 if(bpage
->page
== NULL
) {
1143 pr_err("%s[%s] ext memory alloc failed!!!\n", __FILE__
, __FUNCTION__
);
1147 page
= alloc_pages_node(cpu_to_node(cpu
),
1148 GFP_KERNEL
| __GFP_NORETRY
, 0);
1151 bpage
->page
= page_address(page
);
1153 rb_init_page(bpage
->page
);
1159 list_for_each_entry_safe(bpage
, tmp
, pages
, list
) {
1160 list_del_init(&bpage
->list
);
1161 free_buffer_page(bpage
);
1167 static int rb_allocate_pages(struct ring_buffer_per_cpu
*cpu_buffer
,
1174 if (__rb_allocate_pages(nr_pages
, &pages
, cpu_buffer
->cpu
))
1178 * The ring buffer page list is a circular list that does not
1179 * start and end with a list head. All page list items point to
1182 cpu_buffer
->pages
= pages
.next
;
1185 cpu_buffer
->nr_pages
= nr_pages
;
1187 rb_check_pages(cpu_buffer
);
1192 static struct ring_buffer_per_cpu
*
1193 rb_allocate_cpu_buffer(struct ring_buffer
*buffer
, int nr_pages
, int cpu
)
1195 struct ring_buffer_per_cpu
*cpu_buffer
;
1196 struct buffer_page
*bpage
;
1197 #if !defined (CONFIG_MTK_EXTMEM)
1202 cpu_buffer
= kzalloc_node(ALIGN(sizeof(*cpu_buffer
), cache_line_size()),
1203 GFP_KERNEL
, cpu_to_node(cpu
));
1207 cpu_buffer
->cpu
= cpu
;
1208 cpu_buffer
->buffer
= buffer
;
1209 raw_spin_lock_init(&cpu_buffer
->reader_lock
);
1210 lockdep_set_class(&cpu_buffer
->reader_lock
, buffer
->reader_lock_key
);
1211 cpu_buffer
->lock
= (arch_spinlock_t
)__ARCH_SPIN_LOCK_UNLOCKED
;
1212 INIT_WORK(&cpu_buffer
->update_pages_work
, update_pages_handler
);
1213 init_completion(&cpu_buffer
->update_done
);
1214 init_irq_work(&cpu_buffer
->irq_work
.work
, rb_wake_up_waiters
);
1215 init_waitqueue_head(&cpu_buffer
->irq_work
.waiters
);
1217 bpage
= kzalloc_node(ALIGN(sizeof(*bpage
), cache_line_size()),
1218 GFP_KERNEL
, cpu_to_node(cpu
));
1220 goto fail_free_buffer
;
1222 rb_check_bpage(cpu_buffer
, bpage
);
1224 cpu_buffer
->reader_page
= bpage
;
1226 #ifdef CONFIG_MTK_EXTMEM
1227 bpage
->page
= extmem_malloc_page_align(PAGE_SIZE
);
1228 if(bpage
->page
== NULL
)
1229 goto fail_free_reader
;
1231 page
= alloc_pages_node(cpu_to_node(cpu
), GFP_KERNEL
, 0);
1233 goto fail_free_reader
;
1234 bpage
->page
= page_address(page
);
1236 rb_init_page(bpage
->page
);
1238 INIT_LIST_HEAD(&cpu_buffer
->reader_page
->list
);
1239 INIT_LIST_HEAD(&cpu_buffer
->new_pages
);
1241 ret
= rb_allocate_pages(cpu_buffer
, nr_pages
);
1243 goto fail_free_reader
;
1245 cpu_buffer
->head_page
1246 = list_entry(cpu_buffer
->pages
, struct buffer_page
, list
);
1247 cpu_buffer
->tail_page
= cpu_buffer
->commit_page
= cpu_buffer
->head_page
;
1249 rb_head_page_activate(cpu_buffer
);
1254 free_buffer_page(cpu_buffer
->reader_page
);
1261 static void rb_free_cpu_buffer(struct ring_buffer_per_cpu
*cpu_buffer
)
1263 struct list_head
*head
= cpu_buffer
->pages
;
1264 struct buffer_page
*bpage
, *tmp
;
1266 free_buffer_page(cpu_buffer
->reader_page
);
1268 rb_head_page_deactivate(cpu_buffer
);
1271 list_for_each_entry_safe(bpage
, tmp
, head
, list
) {
1272 list_del_init(&bpage
->list
);
1273 free_buffer_page(bpage
);
1275 bpage
= list_entry(head
, struct buffer_page
, list
);
1276 free_buffer_page(bpage
);
1282 #ifdef CONFIG_HOTPLUG_CPU
1283 static int rb_cpu_notify(struct notifier_block
*self
,
1284 unsigned long action
, void *hcpu
);
1288 * ring_buffer_alloc - allocate a new ring_buffer
1289 * @size: the size in bytes per cpu that is needed.
1290 * @flags: attributes to set for the ring buffer.
1292 * Currently the only flag that is available is the RB_FL_OVERWRITE
1293 * flag. This flag means that the buffer will overwrite old data
1294 * when the buffer wraps. If this flag is not set, the buffer will
1295 * drop data when the tail hits the head.
1297 struct ring_buffer
*__ring_buffer_alloc(unsigned long size
, unsigned flags
,
1298 struct lock_class_key
*key
)
1300 struct ring_buffer
*buffer
;
1304 /* keep it in its own cache line */
1305 buffer
= kzalloc(ALIGN(sizeof(*buffer
), cache_line_size()),
1310 if (!alloc_cpumask_var(&buffer
->cpumask
, GFP_KERNEL
))
1311 goto fail_free_buffer
;
1313 nr_pages
= DIV_ROUND_UP(size
, BUF_PAGE_SIZE
);
1314 buffer
->flags
= flags
;
1315 buffer
->clock
= trace_clock_local
;
1316 buffer
->reader_lock_key
= key
;
1318 init_irq_work(&buffer
->irq_work
.work
, rb_wake_up_waiters
);
1319 init_waitqueue_head(&buffer
->irq_work
.waiters
);
1321 /* need at least two pages */
1326 * In case of non-hotplug cpu, if the ring-buffer is allocated
1327 * in early initcall, it will not be notified of secondary cpus.
1328 * In that off case, we need to allocate for all possible cpus.
1330 #ifdef CONFIG_HOTPLUG_CPU
1332 cpumask_copy(buffer
->cpumask
, cpu_online_mask
);
1334 cpumask_copy(buffer
->cpumask
, cpu_possible_mask
);
1336 buffer
->cpus
= nr_cpu_ids
;
1338 bsize
= sizeof(void *) * nr_cpu_ids
;
1339 buffer
->buffers
= kzalloc(ALIGN(bsize
, cache_line_size()),
1341 if (!buffer
->buffers
)
1342 goto fail_free_cpumask
;
1344 for_each_buffer_cpu(buffer
, cpu
) {
1345 buffer
->buffers
[cpu
] =
1346 rb_allocate_cpu_buffer(buffer
, nr_pages
, cpu
);
1347 if (!buffer
->buffers
[cpu
])
1348 goto fail_free_buffers
;
1351 #ifdef CONFIG_HOTPLUG_CPU
1352 buffer
->cpu_notify
.notifier_call
= rb_cpu_notify
;
1353 buffer
->cpu_notify
.priority
= 0;
1354 register_cpu_notifier(&buffer
->cpu_notify
);
1358 mutex_init(&buffer
->mutex
);
1363 for_each_buffer_cpu(buffer
, cpu
) {
1364 if (buffer
->buffers
[cpu
])
1365 rb_free_cpu_buffer(buffer
->buffers
[cpu
]);
1367 kfree(buffer
->buffers
);
1370 free_cpumask_var(buffer
->cpumask
);
1377 EXPORT_SYMBOL_GPL(__ring_buffer_alloc
);
1380 * ring_buffer_free - free a ring buffer.
1381 * @buffer: the buffer to free.
1384 ring_buffer_free(struct ring_buffer
*buffer
)
1390 #ifdef CONFIG_HOTPLUG_CPU
1391 unregister_cpu_notifier(&buffer
->cpu_notify
);
1394 for_each_buffer_cpu(buffer
, cpu
)
1395 rb_free_cpu_buffer(buffer
->buffers
[cpu
]);
1399 kfree(buffer
->buffers
);
1400 free_cpumask_var(buffer
->cpumask
);
1404 EXPORT_SYMBOL_GPL(ring_buffer_free
);
1406 void ring_buffer_set_clock(struct ring_buffer
*buffer
,
1409 buffer
->clock
= clock
;
1412 static void rb_reset_cpu(struct ring_buffer_per_cpu
*cpu_buffer
);
1414 static inline unsigned long rb_page_entries(struct buffer_page
*bpage
)
1416 return local_read(&bpage
->entries
) & RB_WRITE_MASK
;
1419 static inline unsigned long rb_page_write(struct buffer_page
*bpage
)
1421 return local_read(&bpage
->write
) & RB_WRITE_MASK
;
1425 rb_remove_pages(struct ring_buffer_per_cpu
*cpu_buffer
, unsigned int nr_pages
)
1427 struct list_head
*tail_page
, *to_remove
, *next_page
;
1428 struct buffer_page
*to_remove_page
, *tmp_iter_page
;
1429 struct buffer_page
*last_page
, *first_page
;
1430 unsigned int nr_removed
;
1431 unsigned long head_bit
;
1436 raw_spin_lock_irq(&cpu_buffer
->reader_lock
);
1437 atomic_inc(&cpu_buffer
->record_disabled
);
1439 * We don't race with the readers since we have acquired the reader
1440 * lock. We also don't race with writers after disabling recording.
1441 * This makes it easy to figure out the first and the last page to be
1442 * removed from the list. We unlink all the pages in between including
1443 * the first and last pages. This is done in a busy loop so that we
1444 * lose the least number of traces.
1445 * The pages are freed after we restart recording and unlock readers.
1447 tail_page
= &cpu_buffer
->tail_page
->list
;
1450 * tail page might be on reader page, we remove the next page
1451 * from the ring buffer
1453 if (cpu_buffer
->tail_page
== cpu_buffer
->reader_page
)
1454 tail_page
= rb_list_head(tail_page
->next
);
1455 to_remove
= tail_page
;
1457 /* start of pages to remove */
1458 first_page
= list_entry(rb_list_head(to_remove
->next
),
1459 struct buffer_page
, list
);
1461 for (nr_removed
= 0; nr_removed
< nr_pages
; nr_removed
++) {
1462 to_remove
= rb_list_head(to_remove
)->next
;
1463 head_bit
|= (unsigned long)to_remove
& RB_PAGE_HEAD
;
1466 next_page
= rb_list_head(to_remove
)->next
;
1469 * Now we remove all pages between tail_page and next_page.
1470 * Make sure that we have head_bit value preserved for the
1473 tail_page
->next
= (struct list_head
*)((unsigned long)next_page
|
1475 next_page
= rb_list_head(next_page
);
1476 next_page
->prev
= tail_page
;
1478 /* make sure pages points to a valid page in the ring buffer */
1479 cpu_buffer
->pages
= next_page
;
1481 /* update head page */
1483 cpu_buffer
->head_page
= list_entry(next_page
,
1484 struct buffer_page
, list
);
1487 * change read pointer to make sure any read iterators reset
1490 cpu_buffer
->read
= 0;
1492 /* pages are removed, resume tracing and then free the pages */
1493 atomic_dec(&cpu_buffer
->record_disabled
);
1494 raw_spin_unlock_irq(&cpu_buffer
->reader_lock
);
1496 RB_WARN_ON(cpu_buffer
, list_empty(cpu_buffer
->pages
));
1498 /* last buffer page to remove */
1499 last_page
= list_entry(rb_list_head(to_remove
), struct buffer_page
,
1501 tmp_iter_page
= first_page
;
1504 to_remove_page
= tmp_iter_page
;
1505 rb_inc_page(cpu_buffer
, &tmp_iter_page
);
1507 /* update the counters */
1508 page_entries
= rb_page_entries(to_remove_page
);
1511 * If something was added to this page, it was full
1512 * since it is not the tail page. So we deduct the
1513 * bytes consumed in ring buffer from here.
1514 * Increment overrun to account for the lost events.
1516 local_add(page_entries
, &cpu_buffer
->overrun
);
1517 local_sub(BUF_PAGE_SIZE
, &cpu_buffer
->entries_bytes
);
1521 * We have already removed references to this list item, just
1522 * free up the buffer_page and its page
1524 free_buffer_page(to_remove_page
);
1527 } while (to_remove_page
!= last_page
);
1529 RB_WARN_ON(cpu_buffer
, nr_removed
);
1531 return nr_removed
== 0;
1535 rb_insert_pages(struct ring_buffer_per_cpu
*cpu_buffer
)
1537 struct list_head
*pages
= &cpu_buffer
->new_pages
;
1538 int retries
, success
;
1540 raw_spin_lock_irq(&cpu_buffer
->reader_lock
);
1542 * We are holding the reader lock, so the reader page won't be swapped
1543 * in the ring buffer. Now we are racing with the writer trying to
1544 * move head page and the tail page.
1545 * We are going to adapt the reader page update process where:
1546 * 1. We first splice the start and end of list of new pages between
1547 * the head page and its previous page.
1548 * 2. We cmpxchg the prev_page->next to point from head page to the
1549 * start of new pages list.
1550 * 3. Finally, we update the head->prev to the end of new list.
1552 * We will try this process 10 times, to make sure that we don't keep
1558 struct list_head
*head_page
, *prev_page
, *r
;
1559 struct list_head
*last_page
, *first_page
;
1560 struct list_head
*head_page_with_bit
;
1562 head_page
= &rb_set_head_page(cpu_buffer
)->list
;
1565 prev_page
= head_page
->prev
;
1567 first_page
= pages
->next
;
1568 last_page
= pages
->prev
;
1570 head_page_with_bit
= (struct list_head
*)
1571 ((unsigned long)head_page
| RB_PAGE_HEAD
);
1573 last_page
->next
= head_page_with_bit
;
1574 first_page
->prev
= prev_page
;
1576 r
= cmpxchg(&prev_page
->next
, head_page_with_bit
, first_page
);
1578 if (r
== head_page_with_bit
) {
1580 * yay, we replaced the page pointer to our new list,
1581 * now, we just have to update to head page's prev
1582 * pointer to point to end of list
1584 head_page
->prev
= last_page
;
1591 INIT_LIST_HEAD(pages
);
1593 * If we weren't successful in adding in new pages, warn and stop
1596 RB_WARN_ON(cpu_buffer
, !success
);
1597 raw_spin_unlock_irq(&cpu_buffer
->reader_lock
);
1599 /* free pages if they weren't inserted */
1601 struct buffer_page
*bpage
, *tmp
;
1602 list_for_each_entry_safe(bpage
, tmp
, &cpu_buffer
->new_pages
,
1604 list_del_init(&bpage
->list
);
1605 free_buffer_page(bpage
);
1611 static void rb_update_pages(struct ring_buffer_per_cpu
*cpu_buffer
)
1615 if (cpu_buffer
->nr_pages_to_update
> 0)
1616 success
= rb_insert_pages(cpu_buffer
);
1618 success
= rb_remove_pages(cpu_buffer
,
1619 -cpu_buffer
->nr_pages_to_update
);
1622 cpu_buffer
->nr_pages
+= cpu_buffer
->nr_pages_to_update
;
1625 static void update_pages_handler(struct work_struct
*work
)
1627 struct ring_buffer_per_cpu
*cpu_buffer
= container_of(work
,
1628 struct ring_buffer_per_cpu
, update_pages_work
);
1629 rb_update_pages(cpu_buffer
);
1630 complete(&cpu_buffer
->update_done
);
1634 * ring_buffer_resize - resize the ring buffer
1635 * @buffer: the buffer to resize.
1636 * @size: the new size.
1638 * Minimum size is 2 * BUF_PAGE_SIZE.
1640 * Returns 0 on success and < 0 on failure.
1642 int ring_buffer_resize(struct ring_buffer
*buffer
, unsigned long size
,
1645 struct ring_buffer_per_cpu
*cpu_buffer
;
1650 * Always succeed at resizing a non-existent buffer:
1655 /* Make sure the requested buffer exists */
1656 if (cpu_id
!= RING_BUFFER_ALL_CPUS
&&
1657 !cpumask_test_cpu(cpu_id
, buffer
->cpumask
))
1660 size
= DIV_ROUND_UP(size
, BUF_PAGE_SIZE
);
1661 size
*= BUF_PAGE_SIZE
;
1663 /* we need a minimum of two pages */
1664 if (size
< BUF_PAGE_SIZE
* 2)
1665 size
= BUF_PAGE_SIZE
* 2;
1667 nr_pages
= DIV_ROUND_UP(size
, BUF_PAGE_SIZE
);
1670 * Don't succeed if resizing is disabled, as a reader might be
1671 * manipulating the ring buffer and is expecting a sane state while
1674 if (atomic_read(&buffer
->resize_disabled
))
1677 /* prevent another thread from changing buffer sizes */
1678 mutex_lock(&buffer
->mutex
);
1680 if (cpu_id
== RING_BUFFER_ALL_CPUS
) {
1681 /* calculate the pages to update */
1682 for_each_buffer_cpu(buffer
, cpu
) {
1683 cpu_buffer
= buffer
->buffers
[cpu
];
1685 cpu_buffer
->nr_pages_to_update
= nr_pages
-
1686 cpu_buffer
->nr_pages
;
1688 * nothing more to do for removing pages or no update
1690 if (cpu_buffer
->nr_pages_to_update
<= 0)
1693 * to add pages, make sure all new pages can be
1694 * allocated without receiving ENOMEM
1696 INIT_LIST_HEAD(&cpu_buffer
->new_pages
);
1697 if (__rb_allocate_pages(cpu_buffer
->nr_pages_to_update
,
1698 &cpu_buffer
->new_pages
, cpu
)) {
1699 /* not enough memory for new pages */
1707 * Fire off all the required work handlers
1708 * We can't schedule on offline CPUs, but it's not necessary
1709 * since we can change their buffer sizes without any race.
1711 for_each_buffer_cpu(buffer
, cpu
) {
1712 cpu_buffer
= buffer
->buffers
[cpu
];
1713 if (!cpu_buffer
->nr_pages_to_update
)
1716 /* The update must run on the CPU that is being updated. */
1718 if (cpu
== smp_processor_id() || !cpu_online(cpu
)) {
1719 rb_update_pages(cpu_buffer
);
1720 cpu_buffer
->nr_pages_to_update
= 0;
1723 * Can not disable preemption for schedule_work_on()
1727 schedule_work_on(cpu
,
1728 &cpu_buffer
->update_pages_work
);
1734 /* wait for all the updates to complete */
1735 for_each_buffer_cpu(buffer
, cpu
) {
1736 cpu_buffer
= buffer
->buffers
[cpu
];
1737 if (!cpu_buffer
->nr_pages_to_update
)
1740 if (cpu_online(cpu
))
1741 wait_for_completion(&cpu_buffer
->update_done
);
1742 cpu_buffer
->nr_pages_to_update
= 0;
1747 /* Make sure this CPU has been intitialized */
1748 if (!cpumask_test_cpu(cpu_id
, buffer
->cpumask
))
1751 cpu_buffer
= buffer
->buffers
[cpu_id
];
1753 if (nr_pages
== cpu_buffer
->nr_pages
)
1756 cpu_buffer
->nr_pages_to_update
= nr_pages
-
1757 cpu_buffer
->nr_pages
;
1759 INIT_LIST_HEAD(&cpu_buffer
->new_pages
);
1760 if (cpu_buffer
->nr_pages_to_update
> 0 &&
1761 __rb_allocate_pages(cpu_buffer
->nr_pages_to_update
,
1762 &cpu_buffer
->new_pages
, cpu_id
)) {
1770 /* The update must run on the CPU that is being updated. */
1771 if (cpu_id
== smp_processor_id() || !cpu_online(cpu_id
))
1772 rb_update_pages(cpu_buffer
);
1775 * Can not disable preemption for schedule_work_on()
1779 schedule_work_on(cpu_id
,
1780 &cpu_buffer
->update_pages_work
);
1781 wait_for_completion(&cpu_buffer
->update_done
);
1786 cpu_buffer
->nr_pages_to_update
= 0;
1792 * The ring buffer resize can happen with the ring buffer
1793 * enabled, so that the update disturbs the tracing as little
1794 * as possible. But if the buffer is disabled, we do not need
1795 * to worry about that, and we can take the time to verify
1796 * that the buffer is not corrupt.
1798 if (atomic_read(&buffer
->record_disabled
)) {
1799 atomic_inc(&buffer
->record_disabled
);
1801 * Even though the buffer was disabled, we must make sure
1802 * that it is truly disabled before calling rb_check_pages.
1803 * There could have been a race between checking
1804 * record_disable and incrementing it.
1806 synchronize_sched();
1807 for_each_buffer_cpu(buffer
, cpu
) {
1808 cpu_buffer
= buffer
->buffers
[cpu
];
1809 rb_check_pages(cpu_buffer
);
1811 atomic_dec(&buffer
->record_disabled
);
1814 mutex_unlock(&buffer
->mutex
);
1818 for_each_buffer_cpu(buffer
, cpu
) {
1819 struct buffer_page
*bpage
, *tmp
;
1821 cpu_buffer
= buffer
->buffers
[cpu
];
1822 cpu_buffer
->nr_pages_to_update
= 0;
1824 if (list_empty(&cpu_buffer
->new_pages
))
1827 list_for_each_entry_safe(bpage
, tmp
, &cpu_buffer
->new_pages
,
1829 list_del_init(&bpage
->list
);
1830 free_buffer_page(bpage
);
1833 mutex_unlock(&buffer
->mutex
);
1836 EXPORT_SYMBOL_GPL(ring_buffer_resize
);
1838 void ring_buffer_change_overwrite(struct ring_buffer
*buffer
, int val
)
1840 mutex_lock(&buffer
->mutex
);
1842 buffer
->flags
|= RB_FL_OVERWRITE
;
1844 buffer
->flags
&= ~RB_FL_OVERWRITE
;
1845 mutex_unlock(&buffer
->mutex
);
1847 EXPORT_SYMBOL_GPL(ring_buffer_change_overwrite
);
1849 static inline void *
1850 __rb_data_page_index(struct buffer_data_page
*bpage
, unsigned index
)
1852 return bpage
->data
+ index
;
1855 static inline void *__rb_page_index(struct buffer_page
*bpage
, unsigned index
)
1857 return bpage
->page
->data
+ index
;
1860 static inline struct ring_buffer_event
*
1861 rb_reader_event(struct ring_buffer_per_cpu
*cpu_buffer
)
1863 return __rb_page_index(cpu_buffer
->reader_page
,
1864 cpu_buffer
->reader_page
->read
);
1867 static inline struct ring_buffer_event
*
1868 rb_iter_head_event(struct ring_buffer_iter
*iter
)
1870 return __rb_page_index(iter
->head_page
, iter
->head
);
1873 static inline unsigned rb_page_commit(struct buffer_page
*bpage
)
1875 return local_read(&bpage
->page
->commit
);
1878 /* Size is determined by what has been committed */
1879 static inline unsigned rb_page_size(struct buffer_page
*bpage
)
1881 return rb_page_commit(bpage
);
1884 static inline unsigned
1885 rb_commit_index(struct ring_buffer_per_cpu
*cpu_buffer
)
1887 return rb_page_commit(cpu_buffer
->commit_page
);
1890 static inline unsigned
1891 rb_event_index(struct ring_buffer_event
*event
)
1893 unsigned long addr
= (unsigned long)event
;
1895 return (addr
& ~PAGE_MASK
) - BUF_PAGE_HDR_SIZE
;
1899 rb_event_is_commit(struct ring_buffer_per_cpu
*cpu_buffer
,
1900 struct ring_buffer_event
*event
)
1902 unsigned long addr
= (unsigned long)event
;
1903 unsigned long index
;
1905 index
= rb_event_index(event
);
1908 return cpu_buffer
->commit_page
->page
== (void *)addr
&&
1909 rb_commit_index(cpu_buffer
) == index
;
1913 rb_set_commit_to_write(struct ring_buffer_per_cpu
*cpu_buffer
)
1915 unsigned long max_count
;
1918 * We only race with interrupts and NMIs on this CPU.
1919 * If we own the commit event, then we can commit
1920 * all others that interrupted us, since the interruptions
1921 * are in stack format (they finish before they come
1922 * back to us). This allows us to do a simple loop to
1923 * assign the commit to the tail.
1926 max_count
= cpu_buffer
->nr_pages
* 100;
1928 while (cpu_buffer
->commit_page
!= cpu_buffer
->tail_page
) {
1929 if (RB_WARN_ON(cpu_buffer
, !(--max_count
)))
1931 if (RB_WARN_ON(cpu_buffer
,
1932 rb_is_reader_page(cpu_buffer
->tail_page
)))
1934 local_set(&cpu_buffer
->commit_page
->page
->commit
,
1935 rb_page_write(cpu_buffer
->commit_page
));
1936 rb_inc_page(cpu_buffer
, &cpu_buffer
->commit_page
);
1937 cpu_buffer
->write_stamp
=
1938 cpu_buffer
->commit_page
->page
->time_stamp
;
1939 /* add barrier to keep gcc from optimizing too much */
1942 while (rb_commit_index(cpu_buffer
) !=
1943 rb_page_write(cpu_buffer
->commit_page
)) {
1945 local_set(&cpu_buffer
->commit_page
->page
->commit
,
1946 rb_page_write(cpu_buffer
->commit_page
));
1947 RB_WARN_ON(cpu_buffer
,
1948 local_read(&cpu_buffer
->commit_page
->page
->commit
) &
1953 /* again, keep gcc from optimizing */
1957 * If an interrupt came in just after the first while loop
1958 * and pushed the tail page forward, we will be left with
1959 * a dangling commit that will never go forward.
1961 if (unlikely(cpu_buffer
->commit_page
!= cpu_buffer
->tail_page
))
1965 static void rb_reset_reader_page(struct ring_buffer_per_cpu
*cpu_buffer
)
1967 cpu_buffer
->read_stamp
= cpu_buffer
->reader_page
->page
->time_stamp
;
1968 cpu_buffer
->reader_page
->read
= 0;
1971 static void rb_inc_iter(struct ring_buffer_iter
*iter
)
1973 struct ring_buffer_per_cpu
*cpu_buffer
= iter
->cpu_buffer
;
1976 * The iterator could be on the reader page (it starts there).
1977 * But the head could have moved, since the reader was
1978 * found. Check for this case and assign the iterator
1979 * to the head page instead of next.
1981 if (iter
->head_page
== cpu_buffer
->reader_page
)
1982 iter
->head_page
= rb_set_head_page(cpu_buffer
);
1984 rb_inc_page(cpu_buffer
, &iter
->head_page
);
1986 iter
->read_stamp
= iter
->head_page
->page
->time_stamp
;
1990 /* Slow path, do not inline */
1991 static noinline
struct ring_buffer_event
*
1992 rb_add_time_stamp(struct ring_buffer_event
*event
, u64 delta
)
1994 event
->type_len
= RINGBUF_TYPE_TIME_EXTEND
;
1996 /* Not the first event on the page? */
1997 if (rb_event_index(event
)) {
1998 event
->time_delta
= delta
& TS_MASK
;
1999 event
->array
[0] = delta
>> TS_SHIFT
;
2001 /* nope, just zero it */
2002 event
->time_delta
= 0;
2003 event
->array
[0] = 0;
2006 return skip_time_extend(event
);
2010 * rb_update_event - update event type and data
2011 * @event: the event to update
2012 * @type: the type of event
2013 * @length: the size of the event field in the ring buffer
2015 * Update the type and data fields of the event. The length
2016 * is the actual size that is written to the ring buffer,
2017 * and with this, we can determine what to place into the
2021 rb_update_event(struct ring_buffer_per_cpu
*cpu_buffer
,
2022 struct ring_buffer_event
*event
, unsigned length
,
2023 int add_timestamp
, u64 delta
)
2025 /* Only a commit updates the timestamp */
2026 if (unlikely(!rb_event_is_commit(cpu_buffer
, event
)))
2030 * If we need to add a timestamp, then we
2031 * add it to the start of the resevered space.
2033 if (unlikely(add_timestamp
)) {
2034 event
= rb_add_time_stamp(event
, delta
);
2035 length
-= RB_LEN_TIME_EXTEND
;
2039 event
->time_delta
= delta
;
2040 length
-= RB_EVNT_HDR_SIZE
;
2041 if (length
> RB_MAX_SMALL_DATA
|| RB_FORCE_8BYTE_ALIGNMENT
) {
2042 event
->type_len
= 0;
2043 event
->array
[0] = length
;
2045 event
->type_len
= DIV_ROUND_UP(length
, RB_ALIGNMENT
);
2049 * rb_handle_head_page - writer hit the head page
2051 * Returns: +1 to retry page
2056 rb_handle_head_page(struct ring_buffer_per_cpu
*cpu_buffer
,
2057 struct buffer_page
*tail_page
,
2058 struct buffer_page
*next_page
)
2060 struct buffer_page
*new_head
;
2065 entries
= rb_page_entries(next_page
);
2068 * The hard part is here. We need to move the head
2069 * forward, and protect against both readers on
2070 * other CPUs and writers coming in via interrupts.
2072 type
= rb_head_page_set_update(cpu_buffer
, next_page
, tail_page
,
2076 * type can be one of four:
2077 * NORMAL - an interrupt already moved it for us
2078 * HEAD - we are the first to get here.
2079 * UPDATE - we are the interrupt interrupting
2081 * MOVED - a reader on another CPU moved the next
2082 * pointer to its reader page. Give up
2089 * We changed the head to UPDATE, thus
2090 * it is our responsibility to update
2093 local_add(entries
, &cpu_buffer
->overrun
);
2094 local_sub(BUF_PAGE_SIZE
, &cpu_buffer
->entries_bytes
);
2097 * The entries will be zeroed out when we move the
2101 /* still more to do */
2104 case RB_PAGE_UPDATE
:
2106 * This is an interrupt that interrupt the
2107 * previous update. Still more to do.
2110 case RB_PAGE_NORMAL
:
2112 * An interrupt came in before the update
2113 * and processed this for us.
2114 * Nothing left to do.
2119 * The reader is on another CPU and just did
2120 * a swap with our next_page.
2125 RB_WARN_ON(cpu_buffer
, 1); /* WTF??? */
2130 * Now that we are here, the old head pointer is
2131 * set to UPDATE. This will keep the reader from
2132 * swapping the head page with the reader page.
2133 * The reader (on another CPU) will spin till
2136 * We just need to protect against interrupts
2137 * doing the job. We will set the next pointer
2138 * to HEAD. After that, we set the old pointer
2139 * to NORMAL, but only if it was HEAD before.
2140 * otherwise we are an interrupt, and only
2141 * want the outer most commit to reset it.
2143 new_head
= next_page
;
2144 rb_inc_page(cpu_buffer
, &new_head
);
2146 ret
= rb_head_page_set_head(cpu_buffer
, new_head
, next_page
,
2150 * Valid returns are:
2151 * HEAD - an interrupt came in and already set it.
2152 * NORMAL - One of two things:
2153 * 1) We really set it.
2154 * 2) A bunch of interrupts came in and moved
2155 * the page forward again.
2159 case RB_PAGE_NORMAL
:
2163 RB_WARN_ON(cpu_buffer
, 1);
2168 * It is possible that an interrupt came in,
2169 * set the head up, then more interrupts came in
2170 * and moved it again. When we get back here,
2171 * the page would have been set to NORMAL but we
2172 * just set it back to HEAD.
2174 * How do you detect this? Well, if that happened
2175 * the tail page would have moved.
2177 if (ret
== RB_PAGE_NORMAL
) {
2179 * If the tail had moved passed next, then we need
2180 * to reset the pointer.
2182 if (cpu_buffer
->tail_page
!= tail_page
&&
2183 cpu_buffer
->tail_page
!= next_page
)
2184 rb_head_page_set_normal(cpu_buffer
, new_head
,
2190 * If this was the outer most commit (the one that
2191 * changed the original pointer from HEAD to UPDATE),
2192 * then it is up to us to reset it to NORMAL.
2194 if (type
== RB_PAGE_HEAD
) {
2195 ret
= rb_head_page_set_normal(cpu_buffer
, next_page
,
2198 if (RB_WARN_ON(cpu_buffer
,
2199 ret
!= RB_PAGE_UPDATE
))
2206 static unsigned rb_calculate_event_length(unsigned length
)
2208 struct ring_buffer_event event
; /* Used only for sizeof array */
2210 /* zero length can cause confusions */
2214 if (length
> RB_MAX_SMALL_DATA
|| RB_FORCE_8BYTE_ALIGNMENT
)
2215 length
+= sizeof(event
.array
[0]);
2217 length
+= RB_EVNT_HDR_SIZE
;
2218 length
= ALIGN(length
, RB_ARCH_ALIGNMENT
);
2224 rb_reset_tail(struct ring_buffer_per_cpu
*cpu_buffer
,
2225 struct buffer_page
*tail_page
,
2226 unsigned long tail
, unsigned long length
)
2228 struct ring_buffer_event
*event
;
2231 * Only the event that crossed the page boundary
2232 * must fill the old tail_page with padding.
2234 if (tail
>= BUF_PAGE_SIZE
) {
2236 * If the page was filled, then we still need
2237 * to update the real_end. Reset it to zero
2238 * and the reader will ignore it.
2240 if (tail
== BUF_PAGE_SIZE
)
2241 tail_page
->real_end
= 0;
2243 local_sub(length
, &tail_page
->write
);
2247 event
= __rb_page_index(tail_page
, tail
);
2248 kmemcheck_annotate_bitfield(event
, bitfield
);
2250 /* account for padding bytes */
2251 local_add(BUF_PAGE_SIZE
- tail
, &cpu_buffer
->entries_bytes
);
2254 * Save the original length to the meta data.
2255 * This will be used by the reader to add lost event
2258 tail_page
->real_end
= tail
;
2261 * If this event is bigger than the minimum size, then
2262 * we need to be careful that we don't subtract the
2263 * write counter enough to allow another writer to slip
2265 * We put in a discarded commit instead, to make sure
2266 * that this space is not used again.
2268 * If we are less than the minimum size, we don't need to
2271 if (tail
> (BUF_PAGE_SIZE
- RB_EVNT_MIN_SIZE
)) {
2272 /* No room for any events */
2274 /* Mark the rest of the page with padding */
2275 rb_event_set_padding(event
);
2277 /* Set the write back to the previous setting */
2278 local_sub(length
, &tail_page
->write
);
2282 /* Put in a discarded event */
2283 event
->array
[0] = (BUF_PAGE_SIZE
- tail
) - RB_EVNT_HDR_SIZE
;
2284 event
->type_len
= RINGBUF_TYPE_PADDING
;
2285 /* time delta must be non zero */
2286 event
->time_delta
= 1;
2288 /* Set write to end of buffer */
2289 length
= (tail
+ length
) - BUF_PAGE_SIZE
;
2290 local_sub(length
, &tail_page
->write
);
2294 * This is the slow path, force gcc not to inline it.
2296 static noinline
struct ring_buffer_event
*
2297 rb_move_tail(struct ring_buffer_per_cpu
*cpu_buffer
,
2298 unsigned long length
, unsigned long tail
,
2299 struct buffer_page
*tail_page
, u64 ts
)
2301 struct buffer_page
*commit_page
= cpu_buffer
->commit_page
;
2302 struct ring_buffer
*buffer
= cpu_buffer
->buffer
;
2303 struct buffer_page
*next_page
;
2306 next_page
= tail_page
;
2308 rb_inc_page(cpu_buffer
, &next_page
);
2311 * If for some reason, we had an interrupt storm that made
2312 * it all the way around the buffer, bail, and warn
2315 if (unlikely(next_page
== commit_page
)) {
2316 local_inc(&cpu_buffer
->commit_overrun
);
2321 * This is where the fun begins!
2323 * We are fighting against races between a reader that
2324 * could be on another CPU trying to swap its reader
2325 * page with the buffer head.
2327 * We are also fighting against interrupts coming in and
2328 * moving the head or tail on us as well.
2330 * If the next page is the head page then we have filled
2331 * the buffer, unless the commit page is still on the
2334 if (rb_is_head_page(cpu_buffer
, next_page
, &tail_page
->list
)) {
2337 * If the commit is not on the reader page, then
2338 * move the header page.
2340 if (!rb_is_reader_page(cpu_buffer
->commit_page
)) {
2342 * If we are not in overwrite mode,
2343 * this is easy, just stop here.
2345 if (!(buffer
->flags
& RB_FL_OVERWRITE
)) {
2346 local_inc(&cpu_buffer
->dropped_events
);
2350 ret
= rb_handle_head_page(cpu_buffer
,
2359 * We need to be careful here too. The
2360 * commit page could still be on the reader
2361 * page. We could have a small buffer, and
2362 * have filled up the buffer with events
2363 * from interrupts and such, and wrapped.
2365 * Note, if the tail page is also the on the
2366 * reader_page, we let it move out.
2368 if (unlikely((cpu_buffer
->commit_page
!=
2369 cpu_buffer
->tail_page
) &&
2370 (cpu_buffer
->commit_page
==
2371 cpu_buffer
->reader_page
))) {
2372 local_inc(&cpu_buffer
->commit_overrun
);
2378 ret
= rb_tail_page_update(cpu_buffer
, tail_page
, next_page
);
2381 * Nested commits always have zero deltas, so
2382 * just reread the time stamp
2384 ts
= rb_time_stamp(buffer
);
2385 next_page
->page
->time_stamp
= ts
;
2390 rb_reset_tail(cpu_buffer
, tail_page
, tail
, length
);
2392 /* fail and let the caller try again */
2393 return ERR_PTR(-EAGAIN
);
2397 rb_reset_tail(cpu_buffer
, tail_page
, tail
, length
);
2402 static struct ring_buffer_event
*
2403 __rb_reserve_next(struct ring_buffer_per_cpu
*cpu_buffer
,
2404 unsigned long length
, u64 ts
,
2405 u64 delta
, int add_timestamp
)
2407 struct buffer_page
*tail_page
;
2408 struct ring_buffer_event
*event
;
2409 unsigned long tail
, write
;
2412 * If the time delta since the last event is too big to
2413 * hold in the time field of the event, then we append a
2414 * TIME EXTEND event ahead of the data event.
2416 if (unlikely(add_timestamp
))
2417 length
+= RB_LEN_TIME_EXTEND
;
2419 tail_page
= cpu_buffer
->tail_page
;
2420 write
= local_add_return(length
, &tail_page
->write
);
2422 /* set write to only the index of the write */
2423 write
&= RB_WRITE_MASK
;
2424 tail
= write
- length
;
2427 * If this is the first commit on the page, then it has the same
2428 * timestamp as the page itself.
2433 /* See if we shot pass the end of this buffer page */
2434 if (unlikely(write
> BUF_PAGE_SIZE
))
2435 return rb_move_tail(cpu_buffer
, length
, tail
,
2438 /* We reserved something on the buffer */
2440 event
= __rb_page_index(tail_page
, tail
);
2441 kmemcheck_annotate_bitfield(event
, bitfield
);
2442 rb_update_event(cpu_buffer
, event
, length
, add_timestamp
, delta
);
2444 local_inc(&tail_page
->entries
);
2447 * If this is the first commit on the page, then update
2451 tail_page
->page
->time_stamp
= ts
;
2453 /* account for these added bytes */
2454 local_add(length
, &cpu_buffer
->entries_bytes
);
2460 rb_try_to_discard(struct ring_buffer_per_cpu
*cpu_buffer
,
2461 struct ring_buffer_event
*event
)
2463 unsigned long new_index
, old_index
;
2464 struct buffer_page
*bpage
;
2465 unsigned long index
;
2468 new_index
= rb_event_index(event
);
2469 old_index
= new_index
+ rb_event_ts_length(event
);
2470 addr
= (unsigned long)event
;
2473 bpage
= cpu_buffer
->tail_page
;
2475 if (bpage
->page
== (void *)addr
&& rb_page_write(bpage
) == old_index
) {
2476 unsigned long write_mask
=
2477 local_read(&bpage
->write
) & ~RB_WRITE_MASK
;
2478 unsigned long event_length
= rb_event_length(event
);
2480 * This is on the tail page. It is possible that
2481 * a write could come in and move the tail page
2482 * and write to the next page. That is fine
2483 * because we just shorten what is on this page.
2485 old_index
+= write_mask
;
2486 new_index
+= write_mask
;
2487 index
= local_cmpxchg(&bpage
->write
, old_index
, new_index
);
2488 if (index
== old_index
) {
2489 /* update counters */
2490 local_sub(event_length
, &cpu_buffer
->entries_bytes
);
2495 /* could not discard */
2499 static void rb_start_commit(struct ring_buffer_per_cpu
*cpu_buffer
)
2501 local_inc(&cpu_buffer
->committing
);
2502 local_inc(&cpu_buffer
->commits
);
2505 static inline void rb_end_commit(struct ring_buffer_per_cpu
*cpu_buffer
)
2507 unsigned long commits
;
2509 if (RB_WARN_ON(cpu_buffer
,
2510 !local_read(&cpu_buffer
->committing
)))
2514 commits
= local_read(&cpu_buffer
->commits
);
2515 /* synchronize with interrupts */
2517 if (local_read(&cpu_buffer
->committing
) == 1)
2518 rb_set_commit_to_write(cpu_buffer
);
2520 local_dec(&cpu_buffer
->committing
);
2522 /* synchronize with interrupts */
2526 * Need to account for interrupts coming in between the
2527 * updating of the commit page and the clearing of the
2528 * committing counter.
2530 if (unlikely(local_read(&cpu_buffer
->commits
) != commits
) &&
2531 !local_read(&cpu_buffer
->committing
)) {
2532 local_inc(&cpu_buffer
->committing
);
2537 static struct ring_buffer_event
*
2538 rb_reserve_next_event(struct ring_buffer
*buffer
,
2539 struct ring_buffer_per_cpu
*cpu_buffer
,
2540 unsigned long length
)
2542 struct ring_buffer_event
*event
;
2548 rb_start_commit(cpu_buffer
);
2550 #ifdef CONFIG_RING_BUFFER_ALLOW_SWAP
2552 * Due to the ability to swap a cpu buffer from a buffer
2553 * it is possible it was swapped before we committed.
2554 * (committing stops a swap). We check for it here and
2555 * if it happened, we have to fail the write.
2558 if (unlikely(ACCESS_ONCE(cpu_buffer
->buffer
) != buffer
)) {
2559 local_dec(&cpu_buffer
->committing
);
2560 local_dec(&cpu_buffer
->commits
);
2565 length
= rb_calculate_event_length(length
);
2571 * We allow for interrupts to reenter here and do a trace.
2572 * If one does, it will cause this original code to loop
2573 * back here. Even with heavy interrupts happening, this
2574 * should only happen a few times in a row. If this happens
2575 * 1000 times in a row, there must be either an interrupt
2576 * storm or we have something buggy.
2579 if (RB_WARN_ON(cpu_buffer
, ++nr_loops
> 1000))
2582 ts
= rb_time_stamp(cpu_buffer
->buffer
);
2583 diff
= ts
- cpu_buffer
->write_stamp
;
2585 /* make sure this diff is calculated here */
2588 /* Did the write stamp get updated already? */
2589 if (likely(ts
>= cpu_buffer
->write_stamp
)) {
2591 if (unlikely(test_time_stamp(delta
))) {
2592 int local_clock_stable
= 1;
2593 #ifdef CONFIG_HAVE_UNSTABLE_SCHED_CLOCK
2594 local_clock_stable
= sched_clock_stable
;
2596 WARN_ONCE(delta
> (1ULL << 59),
2597 KERN_WARNING
"Delta way too big! %llu ts=%llu write stamp = %llu\n%s",
2598 (unsigned long long)delta
,
2599 (unsigned long long)ts
,
2600 (unsigned long long)cpu_buffer
->write_stamp
,
2601 local_clock_stable
? "" :
2602 "If you just came from a suspend/resume,\n"
2603 "please switch to the trace global clock:\n"
2604 " echo global > /sys/kernel/debug/tracing/trace_clock\n");
2609 event
= __rb_reserve_next(cpu_buffer
, length
, ts
,
2610 delta
, add_timestamp
);
2611 if (unlikely(PTR_ERR(event
) == -EAGAIN
))
2620 rb_end_commit(cpu_buffer
);
2624 #ifdef CONFIG_TRACING
2627 * The lock and unlock are done within a preempt disable section.
2628 * The current_context per_cpu variable can only be modified
2629 * by the current task between lock and unlock. But it can
2630 * be modified more than once via an interrupt. To pass this
2631 * information from the lock to the unlock without having to
2632 * access the 'in_interrupt()' functions again (which do show
2633 * a bit of overhead in something as critical as function tracing,
2634 * we use a bitmask trick.
2636 * bit 0 = NMI context
2637 * bit 1 = IRQ context
2638 * bit 2 = SoftIRQ context
2639 * bit 3 = normal context.
2641 * This works because this is the order of contexts that can
2642 * preempt other contexts. A SoftIRQ never preempts an IRQ
2645 * When the context is determined, the corresponding bit is
2646 * checked and set (if it was set, then a recursion of that context
2649 * On unlock, we need to clear this bit. To do so, just subtract
2650 * 1 from the current_context and AND it to itself.
2654 * 101 & 100 = 100 (clearing bit zero)
2657 * 1010 & 1001 = 1000 (clearing bit 1)
2659 * The least significant bit can be cleared this way, and it
2660 * just so happens that it is the same bit corresponding to
2661 * the current context.
2663 static DEFINE_PER_CPU(unsigned int, current_context
);
2665 static __always_inline
int trace_recursive_lock(void)
2667 unsigned int val
= this_cpu_read(current_context
);
2670 if (in_interrupt()) {
2680 if (unlikely(val
& (1 << bit
)))
2684 this_cpu_write(current_context
, val
);
2689 static __always_inline
void trace_recursive_unlock(void)
2691 unsigned int val
= this_cpu_read(current_context
);
2694 val
&= this_cpu_read(current_context
);
2695 this_cpu_write(current_context
, val
);
2700 #define trace_recursive_lock() (0)
2701 #define trace_recursive_unlock() do { } while (0)
2706 * ring_buffer_lock_reserve - reserve a part of the buffer
2707 * @buffer: the ring buffer to reserve from
2708 * @length: the length of the data to reserve (excluding event header)
2710 * Returns a reseverd event on the ring buffer to copy directly to.
2711 * The user of this interface will need to get the body to write into
2712 * and can use the ring_buffer_event_data() interface.
2714 * The length is the length of the data needed, not the event length
2715 * which also includes the event header.
2717 * Must be paired with ring_buffer_unlock_commit, unless NULL is returned.
2718 * If NULL is returned, then nothing has been allocated or locked.
2720 struct ring_buffer_event
*
2721 ring_buffer_lock_reserve(struct ring_buffer
*buffer
, unsigned long length
)
2723 struct ring_buffer_per_cpu
*cpu_buffer
;
2724 struct ring_buffer_event
*event
;
2727 if (ring_buffer_flags
!= RB_BUFFERS_ON
)
2730 /* If we are tracing schedule, we don't want to recurse */
2731 preempt_disable_notrace();
2733 if (atomic_read(&buffer
->record_disabled
))
2736 if (trace_recursive_lock())
2739 cpu
= raw_smp_processor_id();
2741 if (!cpumask_test_cpu(cpu
, buffer
->cpumask
))
2744 cpu_buffer
= buffer
->buffers
[cpu
];
2746 if (atomic_read(&cpu_buffer
->record_disabled
))
2749 if (length
> BUF_MAX_DATA_SIZE
)
2752 event
= rb_reserve_next_event(buffer
, cpu_buffer
, length
);
2759 trace_recursive_unlock();
2762 preempt_enable_notrace();
2765 EXPORT_SYMBOL_GPL(ring_buffer_lock_reserve
);
2768 rb_update_write_stamp(struct ring_buffer_per_cpu
*cpu_buffer
,
2769 struct ring_buffer_event
*event
)
2774 * The event first in the commit queue updates the
2777 if (rb_event_is_commit(cpu_buffer
, event
)) {
2779 * A commit event that is first on a page
2780 * updates the write timestamp with the page stamp
2782 if (!rb_event_index(event
))
2783 cpu_buffer
->write_stamp
=
2784 cpu_buffer
->commit_page
->page
->time_stamp
;
2785 else if (event
->type_len
== RINGBUF_TYPE_TIME_EXTEND
) {
2786 delta
= event
->array
[0];
2788 delta
+= event
->time_delta
;
2789 cpu_buffer
->write_stamp
+= delta
;
2791 cpu_buffer
->write_stamp
+= event
->time_delta
;
2795 static void rb_commit(struct ring_buffer_per_cpu
*cpu_buffer
,
2796 struct ring_buffer_event
*event
)
2798 local_inc(&cpu_buffer
->entries
);
2799 rb_update_write_stamp(cpu_buffer
, event
);
2800 rb_end_commit(cpu_buffer
);
2803 static __always_inline
void
2804 rb_wakeups(struct ring_buffer
*buffer
, struct ring_buffer_per_cpu
*cpu_buffer
)
2806 if (buffer
->irq_work
.waiters_pending
) {
2807 buffer
->irq_work
.waiters_pending
= false;
2808 /* irq_work_queue() supplies it's own memory barriers */
2809 irq_work_queue(&buffer
->irq_work
.work
);
2812 if (cpu_buffer
->irq_work
.waiters_pending
) {
2813 cpu_buffer
->irq_work
.waiters_pending
= false;
2814 /* irq_work_queue() supplies it's own memory barriers */
2815 irq_work_queue(&cpu_buffer
->irq_work
.work
);
2820 * ring_buffer_unlock_commit - commit a reserved
2821 * @buffer: The buffer to commit to
2822 * @event: The event pointer to commit.
2824 * This commits the data to the ring buffer, and releases any locks held.
2826 * Must be paired with ring_buffer_lock_reserve.
2828 int ring_buffer_unlock_commit(struct ring_buffer
*buffer
,
2829 struct ring_buffer_event
*event
)
2831 struct ring_buffer_per_cpu
*cpu_buffer
;
2832 int cpu
= raw_smp_processor_id();
2834 cpu_buffer
= buffer
->buffers
[cpu
];
2836 rb_commit(cpu_buffer
, event
);
2838 rb_wakeups(buffer
, cpu_buffer
);
2840 trace_recursive_unlock();
2842 preempt_enable_notrace();
2846 EXPORT_SYMBOL_GPL(ring_buffer_unlock_commit
);
2848 static inline void rb_event_discard(struct ring_buffer_event
*event
)
2850 if (event
->type_len
== RINGBUF_TYPE_TIME_EXTEND
)
2851 event
= skip_time_extend(event
);
2853 /* array[0] holds the actual length for the discarded event */
2854 event
->array
[0] = rb_event_data_length(event
) - RB_EVNT_HDR_SIZE
;
2855 event
->type_len
= RINGBUF_TYPE_PADDING
;
2856 /* time delta must be non zero */
2857 if (!event
->time_delta
)
2858 event
->time_delta
= 1;
2862 * Decrement the entries to the page that an event is on.
2863 * The event does not even need to exist, only the pointer
2864 * to the page it is on. This may only be called before the commit
2868 rb_decrement_entry(struct ring_buffer_per_cpu
*cpu_buffer
,
2869 struct ring_buffer_event
*event
)
2871 unsigned long addr
= (unsigned long)event
;
2872 struct buffer_page
*bpage
= cpu_buffer
->commit_page
;
2873 struct buffer_page
*start
;
2877 /* Do the likely case first */
2878 if (likely(bpage
->page
== (void *)addr
)) {
2879 local_dec(&bpage
->entries
);
2884 * Because the commit page may be on the reader page we
2885 * start with the next page and check the end loop there.
2887 rb_inc_page(cpu_buffer
, &bpage
);
2890 if (bpage
->page
== (void *)addr
) {
2891 local_dec(&bpage
->entries
);
2894 rb_inc_page(cpu_buffer
, &bpage
);
2895 } while (bpage
!= start
);
2897 /* commit not part of this buffer?? */
2898 RB_WARN_ON(cpu_buffer
, 1);
2902 * ring_buffer_commit_discard - discard an event that has not been committed
2903 * @buffer: the ring buffer
2904 * @event: non committed event to discard
2906 * Sometimes an event that is in the ring buffer needs to be ignored.
2907 * This function lets the user discard an event in the ring buffer
2908 * and then that event will not be read later.
2910 * This function only works if it is called before the the item has been
2911 * committed. It will try to free the event from the ring buffer
2912 * if another event has not been added behind it.
2914 * If another event has been added behind it, it will set the event
2915 * up as discarded, and perform the commit.
2917 * If this function is called, do not call ring_buffer_unlock_commit on
2920 void ring_buffer_discard_commit(struct ring_buffer
*buffer
,
2921 struct ring_buffer_event
*event
)
2923 struct ring_buffer_per_cpu
*cpu_buffer
;
2926 /* The event is discarded regardless */
2927 rb_event_discard(event
);
2929 cpu
= smp_processor_id();
2930 cpu_buffer
= buffer
->buffers
[cpu
];
2933 * This must only be called if the event has not been
2934 * committed yet. Thus we can assume that preemption
2935 * is still disabled.
2937 RB_WARN_ON(buffer
, !local_read(&cpu_buffer
->committing
));
2939 rb_decrement_entry(cpu_buffer
, event
);
2940 if (rb_try_to_discard(cpu_buffer
, event
))
2944 * The commit is still visible by the reader, so we
2945 * must still update the timestamp.
2947 rb_update_write_stamp(cpu_buffer
, event
);
2949 rb_end_commit(cpu_buffer
);
2951 trace_recursive_unlock();
2953 preempt_enable_notrace();
2956 EXPORT_SYMBOL_GPL(ring_buffer_discard_commit
);
2959 * ring_buffer_write - write data to the buffer without reserving
2960 * @buffer: The ring buffer to write to.
2961 * @length: The length of the data being written (excluding the event header)
2962 * @data: The data to write to the buffer.
2964 * This is like ring_buffer_lock_reserve and ring_buffer_unlock_commit as
2965 * one function. If you already have the data to write to the buffer, it
2966 * may be easier to simply call this function.
2968 * Note, like ring_buffer_lock_reserve, the length is the length of the data
2969 * and not the length of the event which would hold the header.
2971 int ring_buffer_write(struct ring_buffer
*buffer
,
2972 unsigned long length
,
2975 struct ring_buffer_per_cpu
*cpu_buffer
;
2976 struct ring_buffer_event
*event
;
2981 if (ring_buffer_flags
!= RB_BUFFERS_ON
)
2984 preempt_disable_notrace();
2986 if (atomic_read(&buffer
->record_disabled
))
2989 cpu
= raw_smp_processor_id();
2991 if (!cpumask_test_cpu(cpu
, buffer
->cpumask
))
2994 cpu_buffer
= buffer
->buffers
[cpu
];
2996 if (atomic_read(&cpu_buffer
->record_disabled
))
2999 if (length
> BUF_MAX_DATA_SIZE
)
3002 event
= rb_reserve_next_event(buffer
, cpu_buffer
, length
);
3006 body
= rb_event_data(event
);
3008 memcpy(body
, data
, length
);
3010 rb_commit(cpu_buffer
, event
);
3012 rb_wakeups(buffer
, cpu_buffer
);
3016 preempt_enable_notrace();
3020 EXPORT_SYMBOL_GPL(ring_buffer_write
);
3022 static int rb_per_cpu_empty(struct ring_buffer_per_cpu
*cpu_buffer
)
3024 struct buffer_page
*reader
= cpu_buffer
->reader_page
;
3025 struct buffer_page
*head
= rb_set_head_page(cpu_buffer
);
3026 struct buffer_page
*commit
= cpu_buffer
->commit_page
;
3028 /* In case of error, head will be NULL */
3029 if (unlikely(!head
))
3032 return reader
->read
== rb_page_commit(reader
) &&
3033 (commit
== reader
||
3035 head
->read
== rb_page_commit(commit
)));
3039 * ring_buffer_record_disable - stop all writes into the buffer
3040 * @buffer: The ring buffer to stop writes to.
3042 * This prevents all writes to the buffer. Any attempt to write
3043 * to the buffer after this will fail and return NULL.
3045 * The caller should call synchronize_sched() after this.
3047 void ring_buffer_record_disable(struct ring_buffer
*buffer
)
3049 atomic_inc(&buffer
->record_disabled
);
3051 EXPORT_SYMBOL_GPL(ring_buffer_record_disable
);
3054 * ring_buffer_record_enable - enable writes to the buffer
3055 * @buffer: The ring buffer to enable writes
3057 * Note, multiple disables will need the same number of enables
3058 * to truly enable the writing (much like preempt_disable).
3060 void ring_buffer_record_enable(struct ring_buffer
*buffer
)
3062 atomic_dec(&buffer
->record_disabled
);
3064 EXPORT_SYMBOL_GPL(ring_buffer_record_enable
);
3067 * ring_buffer_record_off - stop all writes into the buffer
3068 * @buffer: The ring buffer to stop writes to.
3070 * This prevents all writes to the buffer. Any attempt to write
3071 * to the buffer after this will fail and return NULL.
3073 * This is different than ring_buffer_record_disable() as
3074 * it works like an on/off switch, where as the disable() version
3075 * must be paired with a enable().
3077 void ring_buffer_record_off(struct ring_buffer
*buffer
)
3080 unsigned int new_rd
;
3083 rd
= atomic_read(&buffer
->record_disabled
);
3084 new_rd
= rd
| RB_BUFFER_OFF
;
3085 } while (atomic_cmpxchg(&buffer
->record_disabled
, rd
, new_rd
) != rd
);
3087 EXPORT_SYMBOL_GPL(ring_buffer_record_off
);
3090 * ring_buffer_record_on - restart writes into the buffer
3091 * @buffer: The ring buffer to start writes to.
3093 * This enables all writes to the buffer that was disabled by
3094 * ring_buffer_record_off().
3096 * This is different than ring_buffer_record_enable() as
3097 * it works like an on/off switch, where as the enable() version
3098 * must be paired with a disable().
3100 void ring_buffer_record_on(struct ring_buffer
*buffer
)
3103 unsigned int new_rd
;
3106 rd
= atomic_read(&buffer
->record_disabled
);
3107 new_rd
= rd
& ~RB_BUFFER_OFF
;
3108 } while (atomic_cmpxchg(&buffer
->record_disabled
, rd
, new_rd
) != rd
);
3110 EXPORT_SYMBOL_GPL(ring_buffer_record_on
);
3113 * ring_buffer_record_is_on - return true if the ring buffer can write
3114 * @buffer: The ring buffer to see if write is enabled
3116 * Returns true if the ring buffer is in a state that it accepts writes.
3118 int ring_buffer_record_is_on(struct ring_buffer
*buffer
)
3120 return !atomic_read(&buffer
->record_disabled
);
3124 * ring_buffer_record_disable_cpu - stop all writes into the cpu_buffer
3125 * @buffer: The ring buffer to stop writes to.
3126 * @cpu: The CPU buffer to stop
3128 * This prevents all writes to the buffer. Any attempt to write
3129 * to the buffer after this will fail and return NULL.
3131 * The caller should call synchronize_sched() after this.
3133 void ring_buffer_record_disable_cpu(struct ring_buffer
*buffer
, int cpu
)
3135 struct ring_buffer_per_cpu
*cpu_buffer
;
3137 if (!cpumask_test_cpu(cpu
, buffer
->cpumask
))
3140 cpu_buffer
= buffer
->buffers
[cpu
];
3141 atomic_inc(&cpu_buffer
->record_disabled
);
3143 EXPORT_SYMBOL_GPL(ring_buffer_record_disable_cpu
);
3146 * ring_buffer_record_enable_cpu - enable writes to the buffer
3147 * @buffer: The ring buffer to enable writes
3148 * @cpu: The CPU to enable.
3150 * Note, multiple disables will need the same number of enables
3151 * to truly enable the writing (much like preempt_disable).
3153 void ring_buffer_record_enable_cpu(struct ring_buffer
*buffer
, int cpu
)
3155 struct ring_buffer_per_cpu
*cpu_buffer
;
3157 if (!cpumask_test_cpu(cpu
, buffer
->cpumask
))
3160 cpu_buffer
= buffer
->buffers
[cpu
];
3161 atomic_dec(&cpu_buffer
->record_disabled
);
3163 EXPORT_SYMBOL_GPL(ring_buffer_record_enable_cpu
);
3166 * The total entries in the ring buffer is the running counter
3167 * of entries entered into the ring buffer, minus the sum of
3168 * the entries read from the ring buffer and the number of
3169 * entries that were overwritten.
3171 static inline unsigned long
3172 rb_num_of_entries(struct ring_buffer_per_cpu
*cpu_buffer
)
3174 return local_read(&cpu_buffer
->entries
) -
3175 (local_read(&cpu_buffer
->overrun
) + cpu_buffer
->read
);
3179 * ring_buffer_oldest_event_ts - get the oldest event timestamp from the buffer
3180 * @buffer: The ring buffer
3181 * @cpu: The per CPU buffer to read from.
3183 u64
ring_buffer_oldest_event_ts(struct ring_buffer
*buffer
, int cpu
)
3185 unsigned long flags
;
3186 struct ring_buffer_per_cpu
*cpu_buffer
;
3187 struct buffer_page
*bpage
;
3190 if (!cpumask_test_cpu(cpu
, buffer
->cpumask
))
3193 cpu_buffer
= buffer
->buffers
[cpu
];
3194 raw_spin_lock_irqsave(&cpu_buffer
->reader_lock
, flags
);
3196 * if the tail is on reader_page, oldest time stamp is on the reader
3199 if (cpu_buffer
->tail_page
== cpu_buffer
->reader_page
)
3200 bpage
= cpu_buffer
->reader_page
;
3202 bpage
= rb_set_head_page(cpu_buffer
);
3204 ret
= bpage
->page
->time_stamp
;
3205 raw_spin_unlock_irqrestore(&cpu_buffer
->reader_lock
, flags
);
3209 EXPORT_SYMBOL_GPL(ring_buffer_oldest_event_ts
);
3212 * ring_buffer_bytes_cpu - get the number of bytes consumed in a cpu buffer
3213 * @buffer: The ring buffer
3214 * @cpu: The per CPU buffer to read from.
3216 unsigned long ring_buffer_bytes_cpu(struct ring_buffer
*buffer
, int cpu
)
3218 struct ring_buffer_per_cpu
*cpu_buffer
;
3221 if (!cpumask_test_cpu(cpu
, buffer
->cpumask
))
3224 cpu_buffer
= buffer
->buffers
[cpu
];
3225 ret
= local_read(&cpu_buffer
->entries_bytes
) - cpu_buffer
->read_bytes
;
3229 EXPORT_SYMBOL_GPL(ring_buffer_bytes_cpu
);
3232 * ring_buffer_entries_cpu - get the number of entries in a cpu buffer
3233 * @buffer: The ring buffer
3234 * @cpu: The per CPU buffer to get the entries from.
3236 unsigned long ring_buffer_entries_cpu(struct ring_buffer
*buffer
, int cpu
)
3238 struct ring_buffer_per_cpu
*cpu_buffer
;
3240 if (!cpumask_test_cpu(cpu
, buffer
->cpumask
))
3243 cpu_buffer
= buffer
->buffers
[cpu
];
3245 return rb_num_of_entries(cpu_buffer
);
3247 EXPORT_SYMBOL_GPL(ring_buffer_entries_cpu
);
3250 * ring_buffer_overrun_cpu - get the number of overruns caused by the ring
3251 * buffer wrapping around (only if RB_FL_OVERWRITE is on).
3252 * @buffer: The ring buffer
3253 * @cpu: The per CPU buffer to get the number of overruns from
3255 unsigned long ring_buffer_overrun_cpu(struct ring_buffer
*buffer
, int cpu
)
3257 struct ring_buffer_per_cpu
*cpu_buffer
;
3260 if (!cpumask_test_cpu(cpu
, buffer
->cpumask
))
3263 cpu_buffer
= buffer
->buffers
[cpu
];
3264 ret
= local_read(&cpu_buffer
->overrun
);
3268 EXPORT_SYMBOL_GPL(ring_buffer_overrun_cpu
);
3271 * ring_buffer_commit_overrun_cpu - get the number of overruns caused by
3272 * commits failing due to the buffer wrapping around while there are uncommitted
3273 * events, such as during an interrupt storm.
3274 * @buffer: The ring buffer
3275 * @cpu: The per CPU buffer to get the number of overruns from
3278 ring_buffer_commit_overrun_cpu(struct ring_buffer
*buffer
, int cpu
)
3280 struct ring_buffer_per_cpu
*cpu_buffer
;
3283 if (!cpumask_test_cpu(cpu
, buffer
->cpumask
))
3286 cpu_buffer
= buffer
->buffers
[cpu
];
3287 ret
= local_read(&cpu_buffer
->commit_overrun
);
3291 EXPORT_SYMBOL_GPL(ring_buffer_commit_overrun_cpu
);
3294 * ring_buffer_dropped_events_cpu - get the number of dropped events caused by
3295 * the ring buffer filling up (only if RB_FL_OVERWRITE is off).
3296 * @buffer: The ring buffer
3297 * @cpu: The per CPU buffer to get the number of overruns from
3300 ring_buffer_dropped_events_cpu(struct ring_buffer
*buffer
, int cpu
)
3302 struct ring_buffer_per_cpu
*cpu_buffer
;
3305 if (!cpumask_test_cpu(cpu
, buffer
->cpumask
))
3308 cpu_buffer
= buffer
->buffers
[cpu
];
3309 ret
= local_read(&cpu_buffer
->dropped_events
);
3313 EXPORT_SYMBOL_GPL(ring_buffer_dropped_events_cpu
);
3316 * ring_buffer_read_events_cpu - get the number of events successfully read
3317 * @buffer: The ring buffer
3318 * @cpu: The per CPU buffer to get the number of events read
3321 ring_buffer_read_events_cpu(struct ring_buffer
*buffer
, int cpu
)
3323 struct ring_buffer_per_cpu
*cpu_buffer
;
3325 if (!cpumask_test_cpu(cpu
, buffer
->cpumask
))
3328 cpu_buffer
= buffer
->buffers
[cpu
];
3329 return cpu_buffer
->read
;
3331 EXPORT_SYMBOL_GPL(ring_buffer_read_events_cpu
);
3334 * ring_buffer_entries - get the number of entries in a buffer
3335 * @buffer: The ring buffer
3337 * Returns the total number of entries in the ring buffer
3340 unsigned long ring_buffer_entries(struct ring_buffer
*buffer
)
3342 struct ring_buffer_per_cpu
*cpu_buffer
;
3343 unsigned long entries
= 0;
3346 /* if you care about this being correct, lock the buffer */
3347 for_each_buffer_cpu(buffer
, cpu
) {
3348 cpu_buffer
= buffer
->buffers
[cpu
];
3349 entries
+= rb_num_of_entries(cpu_buffer
);
3354 EXPORT_SYMBOL_GPL(ring_buffer_entries
);
3357 * ring_buffer_overruns - get the number of overruns in buffer
3358 * @buffer: The ring buffer
3360 * Returns the total number of overruns in the ring buffer
3363 unsigned long ring_buffer_overruns(struct ring_buffer
*buffer
)
3365 struct ring_buffer_per_cpu
*cpu_buffer
;
3366 unsigned long overruns
= 0;
3369 /* if you care about this being correct, lock the buffer */
3370 for_each_buffer_cpu(buffer
, cpu
) {
3371 cpu_buffer
= buffer
->buffers
[cpu
];
3372 overruns
+= local_read(&cpu_buffer
->overrun
);
3377 EXPORT_SYMBOL_GPL(ring_buffer_overruns
);
3379 static void rb_iter_reset(struct ring_buffer_iter
*iter
)
3381 struct ring_buffer_per_cpu
*cpu_buffer
= iter
->cpu_buffer
;
3383 /* Iterator usage is expected to have record disabled */
3384 iter
->head_page
= cpu_buffer
->reader_page
;
3385 iter
->head
= cpu_buffer
->reader_page
->read
;
3387 iter
->cache_reader_page
= iter
->head_page
;
3388 iter
->cache_read
= iter
->head
;
3391 iter
->read_stamp
= cpu_buffer
->read_stamp
;
3393 iter
->read_stamp
= iter
->head_page
->page
->time_stamp
;
3397 * ring_buffer_iter_reset - reset an iterator
3398 * @iter: The iterator to reset
3400 * Resets the iterator, so that it will start from the beginning
3403 void ring_buffer_iter_reset(struct ring_buffer_iter
*iter
)
3405 struct ring_buffer_per_cpu
*cpu_buffer
;
3406 unsigned long flags
;
3411 cpu_buffer
= iter
->cpu_buffer
;
3413 raw_spin_lock_irqsave(&cpu_buffer
->reader_lock
, flags
);
3414 rb_iter_reset(iter
);
3415 raw_spin_unlock_irqrestore(&cpu_buffer
->reader_lock
, flags
);
3417 EXPORT_SYMBOL_GPL(ring_buffer_iter_reset
);
3420 * ring_buffer_iter_empty - check if an iterator has no more to read
3421 * @iter: The iterator to check
3423 int ring_buffer_iter_empty(struct ring_buffer_iter
*iter
)
3425 struct ring_buffer_per_cpu
*cpu_buffer
;
3427 cpu_buffer
= iter
->cpu_buffer
;
3429 return iter
->head_page
== cpu_buffer
->commit_page
&&
3430 iter
->head
== rb_commit_index(cpu_buffer
);
3432 EXPORT_SYMBOL_GPL(ring_buffer_iter_empty
);
3435 rb_update_read_stamp(struct ring_buffer_per_cpu
*cpu_buffer
,
3436 struct ring_buffer_event
*event
)
3440 switch (event
->type_len
) {
3441 case RINGBUF_TYPE_PADDING
:
3444 case RINGBUF_TYPE_TIME_EXTEND
:
3445 delta
= event
->array
[0];
3447 delta
+= event
->time_delta
;
3448 cpu_buffer
->read_stamp
+= delta
;
3451 case RINGBUF_TYPE_TIME_STAMP
:
3452 /* FIXME: not implemented */
3455 case RINGBUF_TYPE_DATA
:
3456 cpu_buffer
->read_stamp
+= event
->time_delta
;
3466 rb_update_iter_read_stamp(struct ring_buffer_iter
*iter
,
3467 struct ring_buffer_event
*event
)
3471 switch (event
->type_len
) {
3472 case RINGBUF_TYPE_PADDING
:
3475 case RINGBUF_TYPE_TIME_EXTEND
:
3476 delta
= event
->array
[0];
3478 delta
+= event
->time_delta
;
3479 iter
->read_stamp
+= delta
;
3482 case RINGBUF_TYPE_TIME_STAMP
:
3483 /* FIXME: not implemented */
3486 case RINGBUF_TYPE_DATA
:
3487 iter
->read_stamp
+= event
->time_delta
;
3496 static struct buffer_page
*
3497 rb_get_reader_page(struct ring_buffer_per_cpu
*cpu_buffer
)
3499 struct buffer_page
*reader
= NULL
;
3500 unsigned long overwrite
;
3501 unsigned long flags
;
3505 local_irq_save(flags
);
3506 arch_spin_lock(&cpu_buffer
->lock
);
3510 * This should normally only loop twice. But because the
3511 * start of the reader inserts an empty page, it causes
3512 * a case where we will loop three times. There should be no
3513 * reason to loop four times (that I know of).
3515 if (RB_WARN_ON(cpu_buffer
, ++nr_loops
> 3)) {
3520 reader
= cpu_buffer
->reader_page
;
3522 /* If there's more to read, return this page */
3523 if (cpu_buffer
->reader_page
->read
< rb_page_size(reader
))
3526 /* Never should we have an index greater than the size */
3527 if (RB_WARN_ON(cpu_buffer
,
3528 cpu_buffer
->reader_page
->read
> rb_page_size(reader
)))
3531 /* check if we caught up to the tail */
3533 if (cpu_buffer
->commit_page
== cpu_buffer
->reader_page
)
3536 /* Don't bother swapping if the ring buffer is empty */
3537 if (rb_num_of_entries(cpu_buffer
) == 0)
3541 * Reset the reader page to size zero.
3543 local_set(&cpu_buffer
->reader_page
->write
, 0);
3544 local_set(&cpu_buffer
->reader_page
->entries
, 0);
3545 local_set(&cpu_buffer
->reader_page
->page
->commit
, 0);
3546 cpu_buffer
->reader_page
->real_end
= 0;
3550 * Splice the empty reader page into the list around the head.
3552 reader
= rb_set_head_page(cpu_buffer
);
3555 cpu_buffer
->reader_page
->list
.next
= rb_list_head(reader
->list
.next
);
3556 cpu_buffer
->reader_page
->list
.prev
= reader
->list
.prev
;
3559 * cpu_buffer->pages just needs to point to the buffer, it
3560 * has no specific buffer page to point to. Lets move it out
3561 * of our way so we don't accidentally swap it.
3563 cpu_buffer
->pages
= reader
->list
.prev
;
3565 /* The reader page will be pointing to the new head */
3566 rb_set_list_to_head(cpu_buffer
, &cpu_buffer
->reader_page
->list
);
3569 * We want to make sure we read the overruns after we set up our
3570 * pointers to the next object. The writer side does a
3571 * cmpxchg to cross pages which acts as the mb on the writer
3572 * side. Note, the reader will constantly fail the swap
3573 * while the writer is updating the pointers, so this
3574 * guarantees that the overwrite recorded here is the one we
3575 * want to compare with the last_overrun.
3578 overwrite
= local_read(&(cpu_buffer
->overrun
));
3581 * Here's the tricky part.
3583 * We need to move the pointer past the header page.
3584 * But we can only do that if a writer is not currently
3585 * moving it. The page before the header page has the
3586 * flag bit '1' set if it is pointing to the page we want.
3587 * but if the writer is in the process of moving it
3588 * than it will be '2' or already moved '0'.
3591 ret
= rb_head_page_replace(reader
, cpu_buffer
->reader_page
);
3594 * If we did not convert it, then we must try again.
3600 * Yeah! We succeeded in replacing the page.
3602 * Now make the new head point back to the reader page.
3604 rb_list_head(reader
->list
.next
)->prev
= &cpu_buffer
->reader_page
->list
;
3605 rb_inc_page(cpu_buffer
, &cpu_buffer
->head_page
);
3607 /* Finally update the reader page to the new head */
3608 cpu_buffer
->reader_page
= reader
;
3609 rb_reset_reader_page(cpu_buffer
);
3611 if (overwrite
!= cpu_buffer
->last_overrun
) {
3612 cpu_buffer
->lost_events
= overwrite
- cpu_buffer
->last_overrun
;
3613 cpu_buffer
->last_overrun
= overwrite
;
3619 arch_spin_unlock(&cpu_buffer
->lock
);
3620 local_irq_restore(flags
);
3625 static void rb_advance_reader(struct ring_buffer_per_cpu
*cpu_buffer
)
3627 struct ring_buffer_event
*event
;
3628 struct buffer_page
*reader
;
3631 reader
= rb_get_reader_page(cpu_buffer
);
3633 /* This function should not be called when buffer is empty */
3634 if (RB_WARN_ON(cpu_buffer
, !reader
))
3637 event
= rb_reader_event(cpu_buffer
);
3639 if (event
->type_len
<= RINGBUF_TYPE_DATA_TYPE_LEN_MAX
)
3642 rb_update_read_stamp(cpu_buffer
, event
);
3644 length
= rb_event_length(event
);
3645 cpu_buffer
->reader_page
->read
+= length
;
3648 static void rb_advance_iter(struct ring_buffer_iter
*iter
)
3650 struct ring_buffer_per_cpu
*cpu_buffer
;
3651 struct ring_buffer_event
*event
;
3654 cpu_buffer
= iter
->cpu_buffer
;
3657 * Check if we are at the end of the buffer.
3659 if (iter
->head
>= rb_page_size(iter
->head_page
)) {
3660 /* discarded commits can make the page empty */
3661 if (iter
->head_page
== cpu_buffer
->commit_page
)
3667 event
= rb_iter_head_event(iter
);
3669 length
= rb_event_length(event
);
3672 * This should not be called to advance the header if we are
3673 * at the tail of the buffer.
3675 if (RB_WARN_ON(cpu_buffer
,
3676 (iter
->head_page
== cpu_buffer
->commit_page
) &&
3677 (iter
->head
+ length
> rb_commit_index(cpu_buffer
))))
3680 rb_update_iter_read_stamp(iter
, event
);
3682 iter
->head
+= length
;
3684 /* check for end of page padding */
3685 if ((iter
->head
>= rb_page_size(iter
->head_page
)) &&
3686 (iter
->head_page
!= cpu_buffer
->commit_page
))
3690 static int rb_lost_events(struct ring_buffer_per_cpu
*cpu_buffer
)
3692 return cpu_buffer
->lost_events
;
3695 static struct ring_buffer_event
*
3696 rb_buffer_peek(struct ring_buffer_per_cpu
*cpu_buffer
, u64
*ts
,
3697 unsigned long *lost_events
)
3699 struct ring_buffer_event
*event
;
3700 struct buffer_page
*reader
;
3705 * We repeat when a time extend is encountered.
3706 * Since the time extend is always attached to a data event,
3707 * we should never loop more than once.
3708 * (We never hit the following condition more than twice).
3710 if (RB_WARN_ON(cpu_buffer
, ++nr_loops
> 2))
3713 reader
= rb_get_reader_page(cpu_buffer
);
3717 event
= rb_reader_event(cpu_buffer
);
3719 switch (event
->type_len
) {
3720 case RINGBUF_TYPE_PADDING
:
3721 if (rb_null_event(event
))
3722 RB_WARN_ON(cpu_buffer
, 1);
3724 * Because the writer could be discarding every
3725 * event it creates (which would probably be bad)
3726 * if we were to go back to "again" then we may never
3727 * catch up, and will trigger the warn on, or lock
3728 * the box. Return the padding, and we will release
3729 * the current locks, and try again.
3733 case RINGBUF_TYPE_TIME_EXTEND
:
3734 /* Internal data, OK to advance */
3735 rb_advance_reader(cpu_buffer
);
3738 case RINGBUF_TYPE_TIME_STAMP
:
3739 /* FIXME: not implemented */
3740 rb_advance_reader(cpu_buffer
);
3743 case RINGBUF_TYPE_DATA
:
3745 *ts
= cpu_buffer
->read_stamp
+ event
->time_delta
;
3746 ring_buffer_normalize_time_stamp(cpu_buffer
->buffer
,
3747 cpu_buffer
->cpu
, ts
);
3750 *lost_events
= rb_lost_events(cpu_buffer
);
3759 EXPORT_SYMBOL_GPL(ring_buffer_peek
);
3761 static struct ring_buffer_event
*
3762 rb_iter_peek(struct ring_buffer_iter
*iter
, u64
*ts
)
3764 struct ring_buffer
*buffer
;
3765 struct ring_buffer_per_cpu
*cpu_buffer
;
3766 struct ring_buffer_event
*event
;
3769 cpu_buffer
= iter
->cpu_buffer
;
3770 buffer
= cpu_buffer
->buffer
;
3773 * Check if someone performed a consuming read to
3774 * the buffer. A consuming read invalidates the iterator
3775 * and we need to reset the iterator in this case.
3777 if (unlikely(iter
->cache_read
!= cpu_buffer
->read
||
3778 iter
->cache_reader_page
!= cpu_buffer
->reader_page
))
3779 rb_iter_reset(iter
);
3782 if (ring_buffer_iter_empty(iter
))
3786 * We repeat when a time extend is encountered or we hit
3787 * the end of the page. Since the time extend is always attached
3788 * to a data event, we should never loop more than three times.
3789 * Once for going to next page, once on time extend, and
3790 * finally once to get the event.
3791 * (We never hit the following condition more than thrice).
3793 if (RB_WARN_ON(cpu_buffer
, ++nr_loops
> 3))
3796 if (rb_per_cpu_empty(cpu_buffer
))
3799 if (iter
->head
>= local_read(&iter
->head_page
->page
->commit
)) {
3804 event
= rb_iter_head_event(iter
);
3806 switch (event
->type_len
) {
3807 case RINGBUF_TYPE_PADDING
:
3808 if (rb_null_event(event
)) {
3812 rb_advance_iter(iter
);
3815 case RINGBUF_TYPE_TIME_EXTEND
:
3816 /* Internal data, OK to advance */
3817 rb_advance_iter(iter
);
3820 case RINGBUF_TYPE_TIME_STAMP
:
3821 /* FIXME: not implemented */
3822 rb_advance_iter(iter
);
3825 case RINGBUF_TYPE_DATA
:
3827 *ts
= iter
->read_stamp
+ event
->time_delta
;
3828 ring_buffer_normalize_time_stamp(buffer
,
3829 cpu_buffer
->cpu
, ts
);
3839 EXPORT_SYMBOL_GPL(ring_buffer_iter_peek
);
3841 static inline int rb_ok_to_lock(void)
3844 * If an NMI die dumps out the content of the ring buffer
3845 * do not grab locks. We also permanently disable the ring
3846 * buffer too. A one time deal is all you get from reading
3847 * the ring buffer from an NMI.
3849 if (likely(!in_nmi()))
3852 tracing_off_permanent();
3857 * ring_buffer_peek - peek at the next event to be read
3858 * @buffer: The ring buffer to read
3859 * @cpu: The cpu to peak at
3860 * @ts: The timestamp counter of this event.
3861 * @lost_events: a variable to store if events were lost (may be NULL)
3863 * This will return the event that will be read next, but does
3864 * not consume the data.
3866 struct ring_buffer_event
*
3867 ring_buffer_peek(struct ring_buffer
*buffer
, int cpu
, u64
*ts
,
3868 unsigned long *lost_events
)
3870 struct ring_buffer_per_cpu
*cpu_buffer
= buffer
->buffers
[cpu
];
3871 struct ring_buffer_event
*event
;
3872 unsigned long flags
;
3875 if (!cpumask_test_cpu(cpu
, buffer
->cpumask
))
3878 dolock
= rb_ok_to_lock();
3880 local_irq_save(flags
);
3882 raw_spin_lock(&cpu_buffer
->reader_lock
);
3883 event
= rb_buffer_peek(cpu_buffer
, ts
, lost_events
);
3884 if (event
&& event
->type_len
== RINGBUF_TYPE_PADDING
)
3885 rb_advance_reader(cpu_buffer
);
3887 raw_spin_unlock(&cpu_buffer
->reader_lock
);
3888 local_irq_restore(flags
);
3890 if (event
&& event
->type_len
== RINGBUF_TYPE_PADDING
)
3897 * ring_buffer_iter_peek - peek at the next event to be read
3898 * @iter: The ring buffer iterator
3899 * @ts: The timestamp counter of this event.
3901 * This will return the event that will be read next, but does
3902 * not increment the iterator.
3904 struct ring_buffer_event
*
3905 ring_buffer_iter_peek(struct ring_buffer_iter
*iter
, u64
*ts
)
3907 struct ring_buffer_per_cpu
*cpu_buffer
= iter
->cpu_buffer
;
3908 struct ring_buffer_event
*event
;
3909 unsigned long flags
;
3912 raw_spin_lock_irqsave(&cpu_buffer
->reader_lock
, flags
);
3913 event
= rb_iter_peek(iter
, ts
);
3914 raw_spin_unlock_irqrestore(&cpu_buffer
->reader_lock
, flags
);
3916 if (event
&& event
->type_len
== RINGBUF_TYPE_PADDING
)
3923 * ring_buffer_consume - return an event and consume it
3924 * @buffer: The ring buffer to get the next event from
3925 * @cpu: the cpu to read the buffer from
3926 * @ts: a variable to store the timestamp (may be NULL)
3927 * @lost_events: a variable to store if events were lost (may be NULL)
3929 * Returns the next event in the ring buffer, and that event is consumed.
3930 * Meaning, that sequential reads will keep returning a different event,
3931 * and eventually empty the ring buffer if the producer is slower.
3933 struct ring_buffer_event
*
3934 ring_buffer_consume(struct ring_buffer
*buffer
, int cpu
, u64
*ts
,
3935 unsigned long *lost_events
)
3937 struct ring_buffer_per_cpu
*cpu_buffer
;
3938 struct ring_buffer_event
*event
= NULL
;
3939 unsigned long flags
;
3942 dolock
= rb_ok_to_lock();
3945 /* might be called in atomic */
3948 if (!cpumask_test_cpu(cpu
, buffer
->cpumask
))
3951 cpu_buffer
= buffer
->buffers
[cpu
];
3952 local_irq_save(flags
);
3954 raw_spin_lock(&cpu_buffer
->reader_lock
);
3956 event
= rb_buffer_peek(cpu_buffer
, ts
, lost_events
);
3958 cpu_buffer
->lost_events
= 0;
3959 rb_advance_reader(cpu_buffer
);
3963 raw_spin_unlock(&cpu_buffer
->reader_lock
);
3964 local_irq_restore(flags
);
3969 if (event
&& event
->type_len
== RINGBUF_TYPE_PADDING
)
3974 EXPORT_SYMBOL_GPL(ring_buffer_consume
);
3977 * ring_buffer_read_prepare - Prepare for a non consuming read of the buffer
3978 * @buffer: The ring buffer to read from
3979 * @cpu: The cpu buffer to iterate over
3981 * This performs the initial preparations necessary to iterate
3982 * through the buffer. Memory is allocated, buffer recording
3983 * is disabled, and the iterator pointer is returned to the caller.
3985 * Disabling buffer recordng prevents the reading from being
3986 * corrupted. This is not a consuming read, so a producer is not
3989 * After a sequence of ring_buffer_read_prepare calls, the user is
3990 * expected to make at least one call to ring_buffer_prepare_sync.
3991 * Afterwards, ring_buffer_read_start is invoked to get things going
3994 * This overall must be paired with ring_buffer_finish.
3996 struct ring_buffer_iter
*
3997 ring_buffer_read_prepare(struct ring_buffer
*buffer
, int cpu
)
3999 struct ring_buffer_per_cpu
*cpu_buffer
;
4000 struct ring_buffer_iter
*iter
;
4002 if (!cpumask_test_cpu(cpu
, buffer
->cpumask
))
4005 iter
= kmalloc(sizeof(*iter
), GFP_KERNEL
);
4009 cpu_buffer
= buffer
->buffers
[cpu
];
4011 iter
->cpu_buffer
= cpu_buffer
;
4013 atomic_inc(&buffer
->resize_disabled
);
4014 atomic_inc(&cpu_buffer
->record_disabled
);
4018 EXPORT_SYMBOL_GPL(ring_buffer_read_prepare
);
4021 * ring_buffer_read_prepare_sync - Synchronize a set of prepare calls
4023 * All previously invoked ring_buffer_read_prepare calls to prepare
4024 * iterators will be synchronized. Afterwards, read_buffer_read_start
4025 * calls on those iterators are allowed.
4028 ring_buffer_read_prepare_sync(void)
4030 synchronize_sched();
4032 EXPORT_SYMBOL_GPL(ring_buffer_read_prepare_sync
);
4035 * ring_buffer_read_start - start a non consuming read of the buffer
4036 * @iter: The iterator returned by ring_buffer_read_prepare
4038 * This finalizes the startup of an iteration through the buffer.
4039 * The iterator comes from a call to ring_buffer_read_prepare and
4040 * an intervening ring_buffer_read_prepare_sync must have been
4043 * Must be paired with ring_buffer_finish.
4046 ring_buffer_read_start(struct ring_buffer_iter
*iter
)
4048 struct ring_buffer_per_cpu
*cpu_buffer
;
4049 unsigned long flags
;
4054 cpu_buffer
= iter
->cpu_buffer
;
4056 raw_spin_lock_irqsave(&cpu_buffer
->reader_lock
, flags
);
4057 arch_spin_lock(&cpu_buffer
->lock
);
4058 rb_iter_reset(iter
);
4059 arch_spin_unlock(&cpu_buffer
->lock
);
4060 raw_spin_unlock_irqrestore(&cpu_buffer
->reader_lock
, flags
);
4062 EXPORT_SYMBOL_GPL(ring_buffer_read_start
);
4065 * ring_buffer_finish - finish reading the iterator of the buffer
4066 * @iter: The iterator retrieved by ring_buffer_start
4068 * This re-enables the recording to the buffer, and frees the
4072 ring_buffer_read_finish(struct ring_buffer_iter
*iter
)
4074 struct ring_buffer_per_cpu
*cpu_buffer
= iter
->cpu_buffer
;
4075 unsigned long flags
;
4078 * Ring buffer is disabled from recording, here's a good place
4079 * to check the integrity of the ring buffer.
4080 * Must prevent readers from trying to read, as the check
4081 * clears the HEAD page and readers require it.
4083 raw_spin_lock_irqsave(&cpu_buffer
->reader_lock
, flags
);
4084 rb_check_pages(cpu_buffer
);
4085 raw_spin_unlock_irqrestore(&cpu_buffer
->reader_lock
, flags
);
4087 atomic_dec(&cpu_buffer
->record_disabled
);
4088 atomic_dec(&cpu_buffer
->buffer
->resize_disabled
);
4091 EXPORT_SYMBOL_GPL(ring_buffer_read_finish
);
4094 * ring_buffer_read - read the next item in the ring buffer by the iterator
4095 * @iter: The ring buffer iterator
4096 * @ts: The time stamp of the event read.
4098 * This reads the next event in the ring buffer and increments the iterator.
4100 struct ring_buffer_event
*
4101 ring_buffer_read(struct ring_buffer_iter
*iter
, u64
*ts
)
4103 struct ring_buffer_event
*event
;
4104 struct ring_buffer_per_cpu
*cpu_buffer
= iter
->cpu_buffer
;
4105 unsigned long flags
;
4107 raw_spin_lock_irqsave(&cpu_buffer
->reader_lock
, flags
);
4109 event
= rb_iter_peek(iter
, ts
);
4113 if (event
->type_len
== RINGBUF_TYPE_PADDING
)
4116 rb_advance_iter(iter
);
4118 raw_spin_unlock_irqrestore(&cpu_buffer
->reader_lock
, flags
);
4122 EXPORT_SYMBOL_GPL(ring_buffer_read
);
4125 * ring_buffer_size - return the size of the ring buffer (in bytes)
4126 * @buffer: The ring buffer.
4128 unsigned long ring_buffer_size(struct ring_buffer
*buffer
, int cpu
)
4131 * Earlier, this method returned
4132 * BUF_PAGE_SIZE * buffer->nr_pages
4133 * Since the nr_pages field is now removed, we have converted this to
4134 * return the per cpu buffer value.
4136 if (!cpumask_test_cpu(cpu
, buffer
->cpumask
))
4139 return BUF_PAGE_SIZE
* buffer
->buffers
[cpu
]->nr_pages
;
4141 EXPORT_SYMBOL_GPL(ring_buffer_size
);
4144 rb_reset_cpu(struct ring_buffer_per_cpu
*cpu_buffer
)
4146 rb_head_page_deactivate(cpu_buffer
);
4148 cpu_buffer
->head_page
4149 = list_entry(cpu_buffer
->pages
, struct buffer_page
, list
);
4150 local_set(&cpu_buffer
->head_page
->write
, 0);
4151 local_set(&cpu_buffer
->head_page
->entries
, 0);
4152 local_set(&cpu_buffer
->head_page
->page
->commit
, 0);
4154 cpu_buffer
->head_page
->read
= 0;
4156 cpu_buffer
->tail_page
= cpu_buffer
->head_page
;
4157 cpu_buffer
->commit_page
= cpu_buffer
->head_page
;
4159 INIT_LIST_HEAD(&cpu_buffer
->reader_page
->list
);
4160 INIT_LIST_HEAD(&cpu_buffer
->new_pages
);
4161 local_set(&cpu_buffer
->reader_page
->write
, 0);
4162 local_set(&cpu_buffer
->reader_page
->entries
, 0);
4163 local_set(&cpu_buffer
->reader_page
->page
->commit
, 0);
4164 cpu_buffer
->reader_page
->read
= 0;
4166 local_set(&cpu_buffer
->entries_bytes
, 0);
4167 local_set(&cpu_buffer
->overrun
, 0);
4168 local_set(&cpu_buffer
->commit_overrun
, 0);
4169 local_set(&cpu_buffer
->dropped_events
, 0);
4170 local_set(&cpu_buffer
->entries
, 0);
4171 local_set(&cpu_buffer
->committing
, 0);
4172 local_set(&cpu_buffer
->commits
, 0);
4173 cpu_buffer
->read
= 0;
4174 cpu_buffer
->read_bytes
= 0;
4176 cpu_buffer
->write_stamp
= 0;
4177 cpu_buffer
->read_stamp
= 0;
4179 cpu_buffer
->lost_events
= 0;
4180 cpu_buffer
->last_overrun
= 0;
4182 rb_head_page_activate(cpu_buffer
);
4186 * ring_buffer_reset_cpu - reset a ring buffer per CPU buffer
4187 * @buffer: The ring buffer to reset a per cpu buffer of
4188 * @cpu: The CPU buffer to be reset
4190 void ring_buffer_reset_cpu(struct ring_buffer
*buffer
, int cpu
)
4192 struct ring_buffer_per_cpu
*cpu_buffer
= buffer
->buffers
[cpu
];
4193 unsigned long flags
;
4195 if (!cpumask_test_cpu(cpu
, buffer
->cpumask
))
4198 atomic_inc(&buffer
->resize_disabled
);
4199 atomic_inc(&cpu_buffer
->record_disabled
);
4201 /* Make sure all commits have finished */
4202 synchronize_sched();
4204 raw_spin_lock_irqsave(&cpu_buffer
->reader_lock
, flags
);
4206 if (RB_WARN_ON(cpu_buffer
, local_read(&cpu_buffer
->committing
)))
4209 arch_spin_lock(&cpu_buffer
->lock
);
4211 rb_reset_cpu(cpu_buffer
);
4213 arch_spin_unlock(&cpu_buffer
->lock
);
4216 raw_spin_unlock_irqrestore(&cpu_buffer
->reader_lock
, flags
);
4218 atomic_dec(&cpu_buffer
->record_disabled
);
4219 atomic_dec(&buffer
->resize_disabled
);
4221 EXPORT_SYMBOL_GPL(ring_buffer_reset_cpu
);
4224 * ring_buffer_reset - reset a ring buffer
4225 * @buffer: The ring buffer to reset all cpu buffers
4227 void ring_buffer_reset(struct ring_buffer
*buffer
)
4231 for_each_buffer_cpu(buffer
, cpu
)
4232 ring_buffer_reset_cpu(buffer
, cpu
);
4234 EXPORT_SYMBOL_GPL(ring_buffer_reset
);
4237 * rind_buffer_empty - is the ring buffer empty?
4238 * @buffer: The ring buffer to test
4240 int ring_buffer_empty(struct ring_buffer
*buffer
)
4242 struct ring_buffer_per_cpu
*cpu_buffer
;
4243 unsigned long flags
;
4248 dolock
= rb_ok_to_lock();
4250 /* yes this is racy, but if you don't like the race, lock the buffer */
4251 for_each_buffer_cpu(buffer
, cpu
) {
4252 cpu_buffer
= buffer
->buffers
[cpu
];
4253 local_irq_save(flags
);
4255 raw_spin_lock(&cpu_buffer
->reader_lock
);
4256 ret
= rb_per_cpu_empty(cpu_buffer
);
4258 raw_spin_unlock(&cpu_buffer
->reader_lock
);
4259 local_irq_restore(flags
);
4267 EXPORT_SYMBOL_GPL(ring_buffer_empty
);
4270 * ring_buffer_empty_cpu - is a cpu buffer of a ring buffer empty?
4271 * @buffer: The ring buffer
4272 * @cpu: The CPU buffer to test
4274 int ring_buffer_empty_cpu(struct ring_buffer
*buffer
, int cpu
)
4276 struct ring_buffer_per_cpu
*cpu_buffer
;
4277 unsigned long flags
;
4281 if (!cpumask_test_cpu(cpu
, buffer
->cpumask
))
4284 dolock
= rb_ok_to_lock();
4286 cpu_buffer
= buffer
->buffers
[cpu
];
4287 local_irq_save(flags
);
4289 raw_spin_lock(&cpu_buffer
->reader_lock
);
4290 ret
= rb_per_cpu_empty(cpu_buffer
);
4292 raw_spin_unlock(&cpu_buffer
->reader_lock
);
4293 local_irq_restore(flags
);
4297 EXPORT_SYMBOL_GPL(ring_buffer_empty_cpu
);
4299 #ifdef CONFIG_RING_BUFFER_ALLOW_SWAP
4301 * ring_buffer_swap_cpu - swap a CPU buffer between two ring buffers
4302 * @buffer_a: One buffer to swap with
4303 * @buffer_b: The other buffer to swap with
4305 * This function is useful for tracers that want to take a "snapshot"
4306 * of a CPU buffer and has another back up buffer lying around.
4307 * it is expected that the tracer handles the cpu buffer not being
4308 * used at the moment.
4310 int ring_buffer_swap_cpu(struct ring_buffer
*buffer_a
,
4311 struct ring_buffer
*buffer_b
, int cpu
)
4313 struct ring_buffer_per_cpu
*cpu_buffer_a
;
4314 struct ring_buffer_per_cpu
*cpu_buffer_b
;
4317 if (!cpumask_test_cpu(cpu
, buffer_a
->cpumask
) ||
4318 !cpumask_test_cpu(cpu
, buffer_b
->cpumask
))
4321 cpu_buffer_a
= buffer_a
->buffers
[cpu
];
4322 cpu_buffer_b
= buffer_b
->buffers
[cpu
];
4324 /* At least make sure the two buffers are somewhat the same */
4325 if (cpu_buffer_a
->nr_pages
!= cpu_buffer_b
->nr_pages
)
4330 if (ring_buffer_flags
!= RB_BUFFERS_ON
)
4333 if (atomic_read(&buffer_a
->record_disabled
))
4336 if (atomic_read(&buffer_b
->record_disabled
))
4339 if (atomic_read(&cpu_buffer_a
->record_disabled
))
4342 if (atomic_read(&cpu_buffer_b
->record_disabled
))
4346 * We can't do a synchronize_sched here because this
4347 * function can be called in atomic context.
4348 * Normally this will be called from the same CPU as cpu.
4349 * If not it's up to the caller to protect this.
4351 atomic_inc(&cpu_buffer_a
->record_disabled
);
4352 atomic_inc(&cpu_buffer_b
->record_disabled
);
4355 if (local_read(&cpu_buffer_a
->committing
))
4357 if (local_read(&cpu_buffer_b
->committing
))
4360 buffer_a
->buffers
[cpu
] = cpu_buffer_b
;
4361 buffer_b
->buffers
[cpu
] = cpu_buffer_a
;
4363 cpu_buffer_b
->buffer
= buffer_a
;
4364 cpu_buffer_a
->buffer
= buffer_b
;
4369 atomic_dec(&cpu_buffer_a
->record_disabled
);
4370 atomic_dec(&cpu_buffer_b
->record_disabled
);
4374 EXPORT_SYMBOL_GPL(ring_buffer_swap_cpu
);
4375 #endif /* CONFIG_RING_BUFFER_ALLOW_SWAP */
4378 * ring_buffer_alloc_read_page - allocate a page to read from buffer
4379 * @buffer: the buffer to allocate for.
4381 * This function is used in conjunction with ring_buffer_read_page.
4382 * When reading a full page from the ring buffer, these functions
4383 * can be used to speed up the process. The calling function should
4384 * allocate a few pages first with this function. Then when it
4385 * needs to get pages from the ring buffer, it passes the result
4386 * of this function into ring_buffer_read_page, which will swap
4387 * the page that was allocated, with the read page of the buffer.
4390 * The page allocated, or NULL on error.
4392 void *ring_buffer_alloc_read_page(struct ring_buffer
*buffer
, int cpu
)
4394 struct buffer_data_page
*bpage
;
4397 page
= alloc_pages_node(cpu_to_node(cpu
),
4398 GFP_KERNEL
| __GFP_NORETRY
, 0);
4402 bpage
= page_address(page
);
4404 rb_init_page(bpage
);
4408 EXPORT_SYMBOL_GPL(ring_buffer_alloc_read_page
);
4411 * ring_buffer_free_read_page - free an allocated read page
4412 * @buffer: the buffer the page was allocate for
4413 * @data: the page to free
4415 * Free a page allocated from ring_buffer_alloc_read_page.
4417 void ring_buffer_free_read_page(struct ring_buffer
*buffer
, void *data
)
4419 free_page((unsigned long)data
);
4421 EXPORT_SYMBOL_GPL(ring_buffer_free_read_page
);
4424 * ring_buffer_read_page - extract a page from the ring buffer
4425 * @buffer: buffer to extract from
4426 * @data_page: the page to use allocated from ring_buffer_alloc_read_page
4427 * @len: amount to extract
4428 * @cpu: the cpu of the buffer to extract
4429 * @full: should the extraction only happen when the page is full.
4431 * This function will pull out a page from the ring buffer and consume it.
4432 * @data_page must be the address of the variable that was returned
4433 * from ring_buffer_alloc_read_page. This is because the page might be used
4434 * to swap with a page in the ring buffer.
4437 * rpage = ring_buffer_alloc_read_page(buffer);
4440 * ret = ring_buffer_read_page(buffer, &rpage, len, cpu, 0);
4442 * process_page(rpage, ret);
4444 * When @full is set, the function will not return true unless
4445 * the writer is off the reader page.
4447 * Note: it is up to the calling functions to handle sleeps and wakeups.
4448 * The ring buffer can be used anywhere in the kernel and can not
4449 * blindly call wake_up. The layer that uses the ring buffer must be
4450 * responsible for that.
4453 * >=0 if data has been transferred, returns the offset of consumed data.
4454 * <0 if no data has been transferred.
4456 int ring_buffer_read_page(struct ring_buffer
*buffer
,
4457 void **data_page
, size_t len
, int cpu
, int full
)
4459 struct ring_buffer_per_cpu
*cpu_buffer
= buffer
->buffers
[cpu
];
4460 struct ring_buffer_event
*event
;
4461 struct buffer_data_page
*bpage
;
4462 struct buffer_page
*reader
;
4463 unsigned long missed_events
;
4464 unsigned long flags
;
4465 unsigned int commit
;
4470 if (!cpumask_test_cpu(cpu
, buffer
->cpumask
))
4474 * If len is not big enough to hold the page header, then
4475 * we can not copy anything.
4477 if (len
<= BUF_PAGE_HDR_SIZE
)
4480 len
-= BUF_PAGE_HDR_SIZE
;
4489 raw_spin_lock_irqsave(&cpu_buffer
->reader_lock
, flags
);
4491 reader
= rb_get_reader_page(cpu_buffer
);
4495 event
= rb_reader_event(cpu_buffer
);
4497 read
= reader
->read
;
4498 commit
= rb_page_commit(reader
);
4500 /* Check if any events were dropped */
4501 missed_events
= cpu_buffer
->lost_events
;
4504 * If this page has been partially read or
4505 * if len is not big enough to read the rest of the page or
4506 * a writer is still on the page, then
4507 * we must copy the data from the page to the buffer.
4508 * Otherwise, we can simply swap the page with the one passed in.
4510 if (read
|| (len
< (commit
- read
)) ||
4511 cpu_buffer
->reader_page
== cpu_buffer
->commit_page
) {
4512 struct buffer_data_page
*rpage
= cpu_buffer
->reader_page
->page
;
4513 unsigned int rpos
= read
;
4514 unsigned int pos
= 0;
4520 if (len
> (commit
- read
))
4521 len
= (commit
- read
);
4523 /* Always keep the time extend and data together */
4524 size
= rb_event_ts_length(event
);
4529 /* save the current timestamp, since the user will need it */
4530 save_timestamp
= cpu_buffer
->read_stamp
;
4532 /* Need to copy one event at a time */
4534 /* We need the size of one event, because
4535 * rb_advance_reader only advances by one event,
4536 * whereas rb_event_ts_length may include the size of
4537 * one or two events.
4538 * We have already ensured there's enough space if this
4539 * is a time extend. */
4540 size
= rb_event_length(event
);
4541 memcpy(bpage
->data
+ pos
, rpage
->data
+ rpos
, size
);
4545 rb_advance_reader(cpu_buffer
);
4546 rpos
= reader
->read
;
4552 event
= rb_reader_event(cpu_buffer
);
4553 /* Always keep the time extend and data together */
4554 size
= rb_event_ts_length(event
);
4555 } while (len
>= size
);
4558 local_set(&bpage
->commit
, pos
);
4559 bpage
->time_stamp
= save_timestamp
;
4561 /* we copied everything to the beginning */
4564 /* update the entry counter */
4565 cpu_buffer
->read
+= rb_page_entries(reader
);
4566 cpu_buffer
->read_bytes
+= BUF_PAGE_SIZE
;
4568 /* swap the pages */
4569 rb_init_page(bpage
);
4570 bpage
= reader
->page
;
4571 reader
->page
= *data_page
;
4572 local_set(&reader
->write
, 0);
4573 local_set(&reader
->entries
, 0);
4578 * Use the real_end for the data size,
4579 * This gives us a chance to store the lost events
4582 if (reader
->real_end
)
4583 local_set(&bpage
->commit
, reader
->real_end
);
4587 cpu_buffer
->lost_events
= 0;
4589 commit
= local_read(&bpage
->commit
);
4591 * Set a flag in the commit field if we lost events
4593 if (missed_events
) {
4594 /* If there is room at the end of the page to save the
4595 * missed events, then record it there.
4597 if (BUF_PAGE_SIZE
- commit
>= sizeof(missed_events
)) {
4598 memcpy(&bpage
->data
[commit
], &missed_events
,
4599 sizeof(missed_events
));
4600 local_add(RB_MISSED_STORED
, &bpage
->commit
);
4601 commit
+= sizeof(missed_events
);
4603 local_add(RB_MISSED_EVENTS
, &bpage
->commit
);
4607 * This page may be off to user land. Zero it out here.
4609 if (commit
< BUF_PAGE_SIZE
)
4610 memset(&bpage
->data
[commit
], 0, BUF_PAGE_SIZE
- commit
);
4613 raw_spin_unlock_irqrestore(&cpu_buffer
->reader_lock
, flags
);
4618 EXPORT_SYMBOL_GPL(ring_buffer_read_page
);
4620 #ifdef CONFIG_HOTPLUG_CPU
4621 static int rb_cpu_notify(struct notifier_block
*self
,
4622 unsigned long action
, void *hcpu
)
4624 struct ring_buffer
*buffer
=
4625 container_of(self
, struct ring_buffer
, cpu_notify
);
4626 long cpu
= (long)hcpu
;
4627 int cpu_i
, nr_pages_same
;
4628 unsigned int nr_pages
;
4631 case CPU_UP_PREPARE
:
4632 case CPU_UP_PREPARE_FROZEN
:
4633 if (cpumask_test_cpu(cpu
, buffer
->cpumask
))
4638 /* check if all cpu sizes are same */
4639 for_each_buffer_cpu(buffer
, cpu_i
) {
4640 /* fill in the size from first enabled cpu */
4642 nr_pages
= buffer
->buffers
[cpu_i
]->nr_pages
;
4643 if (nr_pages
!= buffer
->buffers
[cpu_i
]->nr_pages
) {
4648 /* allocate minimum pages, user can later expand it */
4651 buffer
->buffers
[cpu
] =
4652 rb_allocate_cpu_buffer(buffer
, nr_pages
, cpu
);
4653 if (!buffer
->buffers
[cpu
]) {
4654 WARN(1, "failed to allocate ring buffer on CPU %ld\n",
4659 cpumask_set_cpu(cpu
, buffer
->cpumask
);
4661 case CPU_DOWN_PREPARE
:
4662 case CPU_DOWN_PREPARE_FROZEN
:
4665 * If we were to free the buffer, then the user would
4666 * lose any trace that was in the buffer.
4676 #ifdef CONFIG_RING_BUFFER_STARTUP_TEST
4678 * This is a basic integrity check of the ring buffer.
4679 * Late in the boot cycle this test will run when configured in.
4680 * It will kick off a thread per CPU that will go into a loop
4681 * writing to the per cpu ring buffer various sizes of data.
4682 * Some of the data will be large items, some small.
4684 * Another thread is created that goes into a spin, sending out
4685 * IPIs to the other CPUs to also write into the ring buffer.
4686 * this is to test the nesting ability of the buffer.
4688 * Basic stats are recorded and reported. If something in the
4689 * ring buffer should happen that's not expected, a big warning
4690 * is displayed and all ring buffers are disabled.
4692 static struct task_struct
*rb_threads
[NR_CPUS
] __initdata
;
4694 struct rb_test_data
{
4695 struct ring_buffer
*buffer
;
4696 unsigned long events
;
4697 unsigned long bytes_written
;
4698 unsigned long bytes_alloc
;
4699 unsigned long bytes_dropped
;
4700 unsigned long events_nested
;
4701 unsigned long bytes_written_nested
;
4702 unsigned long bytes_alloc_nested
;
4703 unsigned long bytes_dropped_nested
;
4704 int min_size_nested
;
4705 int max_size_nested
;
4712 static struct rb_test_data rb_data
[NR_CPUS
] __initdata
;
4715 #define RB_TEST_BUFFER_SIZE 1048576
4717 static char rb_string
[] __initdata
=
4718 "abcdefghijklmnopqrstuvwxyz1234567890!@#$%^&*()?+\\"
4719 "?+|:';\",.<>/?abcdefghijklmnopqrstuvwxyz1234567890"
4720 "!@#$%^&*()?+\\?+|:';\",.<>/?abcdefghijklmnopqrstuv";
4722 static bool rb_test_started __initdata
;
4729 static __init
int rb_write_something(struct rb_test_data
*data
, bool nested
)
4731 struct ring_buffer_event
*event
;
4732 struct rb_item
*item
;
4739 /* Have nested writes different that what is written */
4740 cnt
= data
->cnt
+ (nested
? 27 : 0);
4742 /* Multiply cnt by ~e, to make some unique increment */
4743 size
= (data
->cnt
* 68 / 25) % (sizeof(rb_string
) - 1);
4745 len
= size
+ sizeof(struct rb_item
);
4747 started
= rb_test_started
;
4748 /* read rb_test_started before checking buffer enabled */
4751 event
= ring_buffer_lock_reserve(data
->buffer
, len
);
4753 /* Ignore dropped events before test starts. */
4756 data
->bytes_dropped
+= len
;
4758 data
->bytes_dropped_nested
+= len
;
4763 event_len
= ring_buffer_event_length(event
);
4765 if (RB_WARN_ON(data
->buffer
, event_len
< len
))
4768 item
= ring_buffer_event_data(event
);
4770 memcpy(item
->str
, rb_string
, size
);
4773 data
->bytes_alloc_nested
+= event_len
;
4774 data
->bytes_written_nested
+= len
;
4775 data
->events_nested
++;
4776 if (!data
->min_size_nested
|| len
< data
->min_size_nested
)
4777 data
->min_size_nested
= len
;
4778 if (len
> data
->max_size_nested
)
4779 data
->max_size_nested
= len
;
4781 data
->bytes_alloc
+= event_len
;
4782 data
->bytes_written
+= len
;
4784 if (!data
->min_size
|| len
< data
->min_size
)
4785 data
->max_size
= len
;
4786 if (len
> data
->max_size
)
4787 data
->max_size
= len
;
4791 ring_buffer_unlock_commit(data
->buffer
, event
);
4796 static __init
int rb_test(void *arg
)
4798 struct rb_test_data
*data
= arg
;
4800 while (!kthread_should_stop()) {
4801 rb_write_something(data
, false);
4804 set_current_state(TASK_INTERRUPTIBLE
);
4805 /* Now sleep between a min of 100-300us and a max of 1ms */
4806 usleep_range(((data
->cnt
% 3) + 1) * 100, 1000);
4812 static __init
void rb_ipi(void *ignore
)
4814 struct rb_test_data
*data
;
4815 int cpu
= smp_processor_id();
4817 data
= &rb_data
[cpu
];
4818 rb_write_something(data
, true);
4821 static __init
int rb_hammer_test(void *arg
)
4823 while (!kthread_should_stop()) {
4825 /* Send an IPI to all cpus to write data! */
4826 smp_call_function(rb_ipi
, NULL
, 1);
4827 /* No sleep, but for non preempt, let others run */
4834 static __init
int test_ringbuffer(void)
4836 struct task_struct
*rb_hammer
;
4837 struct ring_buffer
*buffer
;
4841 pr_info("Running ring buffer tests...\n");
4843 buffer
= ring_buffer_alloc(RB_TEST_BUFFER_SIZE
, RB_FL_OVERWRITE
);
4844 if (WARN_ON(!buffer
))
4847 /* Disable buffer so that threads can't write to it yet */
4848 ring_buffer_record_off(buffer
);
4850 for_each_online_cpu(cpu
) {
4851 rb_data
[cpu
].buffer
= buffer
;
4852 rb_data
[cpu
].cpu
= cpu
;
4853 rb_data
[cpu
].cnt
= cpu
;
4854 rb_threads
[cpu
] = kthread_create(rb_test
, &rb_data
[cpu
],
4855 "rbtester/%d", cpu
);
4856 if (WARN_ON(!rb_threads
[cpu
])) {
4857 pr_cont("FAILED\n");
4862 kthread_bind(rb_threads
[cpu
], cpu
);
4863 wake_up_process(rb_threads
[cpu
]);
4866 /* Now create the rb hammer! */
4867 rb_hammer
= kthread_run(rb_hammer_test
, NULL
, "rbhammer");
4868 if (WARN_ON(!rb_hammer
)) {
4869 pr_cont("FAILED\n");
4874 ring_buffer_record_on(buffer
);
4876 * Show buffer is enabled before setting rb_test_started.
4877 * Yes there's a small race window where events could be
4878 * dropped and the thread wont catch it. But when a ring
4879 * buffer gets enabled, there will always be some kind of
4880 * delay before other CPUs see it. Thus, we don't care about
4881 * those dropped events. We care about events dropped after
4882 * the threads see that the buffer is active.
4885 rb_test_started
= true;
4887 set_current_state(TASK_INTERRUPTIBLE
);
4888 /* Just run for 10 seconds */;
4889 schedule_timeout(10 * HZ
);
4891 kthread_stop(rb_hammer
);
4894 for_each_online_cpu(cpu
) {
4895 if (!rb_threads
[cpu
])
4897 kthread_stop(rb_threads
[cpu
]);
4900 ring_buffer_free(buffer
);
4905 pr_info("finished\n");
4906 for_each_online_cpu(cpu
) {
4907 struct ring_buffer_event
*event
;
4908 struct rb_test_data
*data
= &rb_data
[cpu
];
4909 struct rb_item
*item
;
4910 unsigned long total_events
;
4911 unsigned long total_dropped
;
4912 unsigned long total_written
;
4913 unsigned long total_alloc
;
4914 unsigned long total_read
= 0;
4915 unsigned long total_size
= 0;
4916 unsigned long total_len
= 0;
4917 unsigned long total_lost
= 0;
4920 int small_event_size
;
4924 total_events
= data
->events
+ data
->events_nested
;
4925 total_written
= data
->bytes_written
+ data
->bytes_written_nested
;
4926 total_alloc
= data
->bytes_alloc
+ data
->bytes_alloc_nested
;
4927 total_dropped
= data
->bytes_dropped
+ data
->bytes_dropped_nested
;
4929 big_event_size
= data
->max_size
+ data
->max_size_nested
;
4930 small_event_size
= data
->min_size
+ data
->min_size_nested
;
4932 pr_info("CPU %d:\n", cpu
);
4933 pr_info(" events: %ld\n", total_events
);
4934 pr_info(" dropped bytes: %ld\n", total_dropped
);
4935 pr_info(" alloced bytes: %ld\n", total_alloc
);
4936 pr_info(" written bytes: %ld\n", total_written
);
4937 pr_info(" biggest event: %d\n", big_event_size
);
4938 pr_info(" smallest event: %d\n", small_event_size
);
4940 if (RB_WARN_ON(buffer
, total_dropped
))
4945 while ((event
= ring_buffer_consume(buffer
, cpu
, NULL
, &lost
))) {
4947 item
= ring_buffer_event_data(event
);
4948 total_len
+= ring_buffer_event_length(event
);
4949 total_size
+= item
->size
+ sizeof(struct rb_item
);
4950 if (memcmp(&item
->str
[0], rb_string
, item
->size
) != 0) {
4951 pr_info("FAILED!\n");
4952 pr_info("buffer had: %.*s\n", item
->size
, item
->str
);
4953 pr_info("expected: %.*s\n", item
->size
, rb_string
);
4954 RB_WARN_ON(buffer
, 1);
4965 pr_info(" read events: %ld\n", total_read
);
4966 pr_info(" lost events: %ld\n", total_lost
);
4967 pr_info(" total events: %ld\n", total_lost
+ total_read
);
4968 pr_info(" recorded len bytes: %ld\n", total_len
);
4969 pr_info(" recorded size bytes: %ld\n", total_size
);
4971 pr_info(" With dropped events, record len and size may not match\n"
4972 " alloced and written from above\n");
4974 if (RB_WARN_ON(buffer
, total_len
!= total_alloc
||
4975 total_size
!= total_written
))
4978 if (RB_WARN_ON(buffer
, total_lost
+ total_read
!= total_events
))
4984 pr_info("Ring buffer PASSED!\n");
4986 ring_buffer_free(buffer
);
4990 late_initcall(test_ringbuffer
);
4991 #endif /* CONFIG_RING_BUFFER_STARTUP_TEST */