4 * Copyright (C) 2008 Steven Rostedt <srostedt@redhat.com>
6 #include <linux/ftrace_event.h>
7 #include <linux/ring_buffer.h>
8 #include <linux/trace_clock.h>
9 #include <linux/trace_seq.h>
10 #include <linux/spinlock.h>
11 #include <linux/debugfs.h>
12 #include <linux/uaccess.h>
13 #include <linux/hardirq.h>
14 #include <linux/kmemcheck.h>
15 #include <linux/module.h>
16 #include <linux/percpu.h>
17 #include <linux/mutex.h>
18 #include <linux/slab.h>
19 #include <linux/init.h>
20 #include <linux/hash.h>
21 #include <linux/list.h>
22 #include <linux/cpu.h>
25 #include <asm/local.h>
27 static void update_pages_handler(struct work_struct
*work
);
30 * The ring buffer header is special. We must manually up keep it.
32 int ring_buffer_print_entry_header(struct trace_seq
*s
)
36 ret
= trace_seq_printf(s
, "# compressed entry header\n");
37 ret
= trace_seq_printf(s
, "\ttype_len : 5 bits\n");
38 ret
= trace_seq_printf(s
, "\ttime_delta : 27 bits\n");
39 ret
= trace_seq_printf(s
, "\tarray : 32 bits\n");
40 ret
= trace_seq_printf(s
, "\n");
41 ret
= trace_seq_printf(s
, "\tpadding : type == %d\n",
42 RINGBUF_TYPE_PADDING
);
43 ret
= trace_seq_printf(s
, "\ttime_extend : type == %d\n",
44 RINGBUF_TYPE_TIME_EXTEND
);
45 ret
= trace_seq_printf(s
, "\tdata max type_len == %d\n",
46 RINGBUF_TYPE_DATA_TYPE_LEN_MAX
);
52 * The ring buffer is made up of a list of pages. A separate list of pages is
53 * allocated for each CPU. A writer may only write to a buffer that is
54 * associated with the CPU it is currently executing on. A reader may read
55 * from any per cpu buffer.
57 * The reader is special. For each per cpu buffer, the reader has its own
58 * reader page. When a reader has read the entire reader page, this reader
59 * page is swapped with another page in the ring buffer.
61 * Now, as long as the writer is off the reader page, the reader can do what
62 * ever it wants with that page. The writer will never write to that page
63 * again (as long as it is out of the ring buffer).
65 * Here's some silly ASCII art.
68 * |reader| RING BUFFER
70 * +------+ +---+ +---+ +---+
79 * |reader| RING BUFFER
80 * |page |------------------v
81 * +------+ +---+ +---+ +---+
90 * |reader| RING BUFFER
91 * |page |------------------v
92 * +------+ +---+ +---+ +---+
97 * +------------------------------+
101 * |buffer| RING BUFFER
102 * |page |------------------v
103 * +------+ +---+ +---+ +---+
105 * | New +---+ +---+ +---+
108 * +------------------------------+
111 * After we make this swap, the reader can hand this page off to the splice
112 * code and be done with it. It can even allocate a new page if it needs to
113 * and swap that into the ring buffer.
115 * We will be using cmpxchg soon to make all this lockless.
120 * A fast way to enable or disable all ring buffers is to
121 * call tracing_on or tracing_off. Turning off the ring buffers
122 * prevents all ring buffers from being recorded to.
123 * Turning this switch on, makes it OK to write to the
124 * ring buffer, if the ring buffer is enabled itself.
126 * There's three layers that must be on in order to write
127 * to the ring buffer.
129 * 1) This global flag must be set.
130 * 2) The ring buffer must be enabled for recording.
131 * 3) The per cpu buffer must be enabled for recording.
133 * In case of an anomaly, this global flag has a bit set that
134 * will permantly disable all ring buffers.
138 * Global flag to disable all recording to ring buffers
139 * This has two bits: ON, DISABLED
143 * 0 0 : ring buffers are off
144 * 1 0 : ring buffers are on
145 * X 1 : ring buffers are permanently disabled
149 RB_BUFFERS_ON_BIT
= 0,
150 RB_BUFFERS_DISABLED_BIT
= 1,
154 RB_BUFFERS_ON
= 1 << RB_BUFFERS_ON_BIT
,
155 RB_BUFFERS_DISABLED
= 1 << RB_BUFFERS_DISABLED_BIT
,
158 static unsigned long ring_buffer_flags __read_mostly
= RB_BUFFERS_ON
;
160 /* Used for individual buffers (after the counter) */
161 #define RB_BUFFER_OFF (1 << 20)
163 #define BUF_PAGE_HDR_SIZE offsetof(struct buffer_data_page, data)
166 * tracing_off_permanent - permanently disable ring buffers
168 * This function, once called, will disable all ring buffers
171 void tracing_off_permanent(void)
173 set_bit(RB_BUFFERS_DISABLED_BIT
, &ring_buffer_flags
);
176 #define RB_EVNT_HDR_SIZE (offsetof(struct ring_buffer_event, array))
177 #define RB_ALIGNMENT 4U
178 #define RB_MAX_SMALL_DATA (RB_ALIGNMENT * RINGBUF_TYPE_DATA_TYPE_LEN_MAX)
179 #define RB_EVNT_MIN_SIZE 8U /* two 32bit words */
181 #if !defined(CONFIG_64BIT) || defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS)
182 # define RB_FORCE_8BYTE_ALIGNMENT 0
183 # define RB_ARCH_ALIGNMENT RB_ALIGNMENT
185 # define RB_FORCE_8BYTE_ALIGNMENT 1
186 # define RB_ARCH_ALIGNMENT 8U
189 /* define RINGBUF_TYPE_DATA for 'case RINGBUF_TYPE_DATA:' */
190 #define RINGBUF_TYPE_DATA 0 ... RINGBUF_TYPE_DATA_TYPE_LEN_MAX
193 RB_LEN_TIME_EXTEND
= 8,
194 RB_LEN_TIME_STAMP
= 16,
197 #define skip_time_extend(event) \
198 ((struct ring_buffer_event *)((char *)event + RB_LEN_TIME_EXTEND))
200 static inline int rb_null_event(struct ring_buffer_event
*event
)
202 return event
->type_len
== RINGBUF_TYPE_PADDING
&& !event
->time_delta
;
205 static void rb_event_set_padding(struct ring_buffer_event
*event
)
207 /* padding has a NULL time_delta */
208 event
->type_len
= RINGBUF_TYPE_PADDING
;
209 event
->time_delta
= 0;
213 rb_event_data_length(struct ring_buffer_event
*event
)
218 length
= event
->type_len
* RB_ALIGNMENT
;
220 length
= event
->array
[0];
221 return length
+ RB_EVNT_HDR_SIZE
;
225 * Return the length of the given event. Will return
226 * the length of the time extend if the event is a
229 static inline unsigned
230 rb_event_length(struct ring_buffer_event
*event
)
232 switch (event
->type_len
) {
233 case RINGBUF_TYPE_PADDING
:
234 if (rb_null_event(event
))
237 return event
->array
[0] + RB_EVNT_HDR_SIZE
;
239 case RINGBUF_TYPE_TIME_EXTEND
:
240 return RB_LEN_TIME_EXTEND
;
242 case RINGBUF_TYPE_TIME_STAMP
:
243 return RB_LEN_TIME_STAMP
;
245 case RINGBUF_TYPE_DATA
:
246 return rb_event_data_length(event
);
255 * Return total length of time extend and data,
256 * or just the event length for all other events.
258 static inline unsigned
259 rb_event_ts_length(struct ring_buffer_event
*event
)
263 if (event
->type_len
== RINGBUF_TYPE_TIME_EXTEND
) {
264 /* time extends include the data event after it */
265 len
= RB_LEN_TIME_EXTEND
;
266 event
= skip_time_extend(event
);
268 return len
+ rb_event_length(event
);
272 * ring_buffer_event_length - return the length of the event
273 * @event: the event to get the length of
275 * Returns the size of the data load of a data event.
276 * If the event is something other than a data event, it
277 * returns the size of the event itself. With the exception
278 * of a TIME EXTEND, where it still returns the size of the
279 * data load of the data event after it.
281 unsigned ring_buffer_event_length(struct ring_buffer_event
*event
)
285 if (event
->type_len
== RINGBUF_TYPE_TIME_EXTEND
)
286 event
= skip_time_extend(event
);
288 length
= rb_event_length(event
);
289 if (event
->type_len
> RINGBUF_TYPE_DATA_TYPE_LEN_MAX
)
291 length
-= RB_EVNT_HDR_SIZE
;
292 if (length
> RB_MAX_SMALL_DATA
+ sizeof(event
->array
[0]))
293 length
-= sizeof(event
->array
[0]);
296 EXPORT_SYMBOL_GPL(ring_buffer_event_length
);
298 /* inline for ring buffer fast paths */
300 rb_event_data(struct ring_buffer_event
*event
)
302 if (event
->type_len
== RINGBUF_TYPE_TIME_EXTEND
)
303 event
= skip_time_extend(event
);
304 BUG_ON(event
->type_len
> RINGBUF_TYPE_DATA_TYPE_LEN_MAX
);
305 /* If length is in len field, then array[0] has the data */
307 return (void *)&event
->array
[0];
308 /* Otherwise length is in array[0] and array[1] has the data */
309 return (void *)&event
->array
[1];
313 * ring_buffer_event_data - return the data of the event
314 * @event: the event to get the data from
316 void *ring_buffer_event_data(struct ring_buffer_event
*event
)
318 return rb_event_data(event
);
320 EXPORT_SYMBOL_GPL(ring_buffer_event_data
);
322 #define for_each_buffer_cpu(buffer, cpu) \
323 for_each_cpu(cpu, buffer->cpumask)
326 #define TS_MASK ((1ULL << TS_SHIFT) - 1)
327 #define TS_DELTA_TEST (~TS_MASK)
329 /* Flag when events were overwritten */
330 #define RB_MISSED_EVENTS (1 << 31)
331 /* Missed count stored at end */
332 #define RB_MISSED_STORED (1 << 30)
334 struct buffer_data_page
{
335 u64 time_stamp
; /* page time stamp */
336 local_t commit
; /* write committed index */
337 unsigned char data
[]; /* data of buffer page */
341 * Note, the buffer_page list must be first. The buffer pages
342 * are allocated in cache lines, which means that each buffer
343 * page will be at the beginning of a cache line, and thus
344 * the least significant bits will be zero. We use this to
345 * add flags in the list struct pointers, to make the ring buffer
349 struct list_head list
; /* list of buffer pages */
350 local_t write
; /* index for next write */
351 unsigned read
; /* index for next read */
352 local_t entries
; /* entries on this page */
353 unsigned long real_end
; /* real end of data */
354 struct buffer_data_page
*page
; /* Actual data page */
358 * The buffer page counters, write and entries, must be reset
359 * atomically when crossing page boundaries. To synchronize this
360 * update, two counters are inserted into the number. One is
361 * the actual counter for the write position or count on the page.
363 * The other is a counter of updaters. Before an update happens
364 * the update partition of the counter is incremented. This will
365 * allow the updater to update the counter atomically.
367 * The counter is 20 bits, and the state data is 12.
369 #define RB_WRITE_MASK 0xfffff
370 #define RB_WRITE_INTCNT (1 << 20)
372 static void rb_init_page(struct buffer_data_page
*bpage
)
374 local_set(&bpage
->commit
, 0);
378 * ring_buffer_page_len - the size of data on the page.
379 * @page: The page to read
381 * Returns the amount of data on the page, including buffer page header.
383 size_t ring_buffer_page_len(void *page
)
385 return local_read(&((struct buffer_data_page
*)page
)->commit
)
390 * Also stolen from mm/slob.c. Thanks to Mathieu Desnoyers for pointing
393 static void free_buffer_page(struct buffer_page
*bpage
)
395 free_page((unsigned long)bpage
->page
);
400 * We need to fit the time_stamp delta into 27 bits.
402 static inline int test_time_stamp(u64 delta
)
404 if (delta
& TS_DELTA_TEST
)
409 #define BUF_PAGE_SIZE (PAGE_SIZE - BUF_PAGE_HDR_SIZE)
411 /* Max payload is BUF_PAGE_SIZE - header (8bytes) */
412 #define BUF_MAX_DATA_SIZE (BUF_PAGE_SIZE - (sizeof(u32) * 2))
414 int ring_buffer_print_page_header(struct trace_seq
*s
)
416 struct buffer_data_page field
;
419 ret
= trace_seq_printf(s
, "\tfield: u64 timestamp;\t"
420 "offset:0;\tsize:%u;\tsigned:%u;\n",
421 (unsigned int)sizeof(field
.time_stamp
),
422 (unsigned int)is_signed_type(u64
));
424 ret
= trace_seq_printf(s
, "\tfield: local_t commit;\t"
425 "offset:%u;\tsize:%u;\tsigned:%u;\n",
426 (unsigned int)offsetof(typeof(field
), commit
),
427 (unsigned int)sizeof(field
.commit
),
428 (unsigned int)is_signed_type(long));
430 ret
= trace_seq_printf(s
, "\tfield: int overwrite;\t"
431 "offset:%u;\tsize:%u;\tsigned:%u;\n",
432 (unsigned int)offsetof(typeof(field
), commit
),
434 (unsigned int)is_signed_type(long));
436 ret
= trace_seq_printf(s
, "\tfield: char data;\t"
437 "offset:%u;\tsize:%u;\tsigned:%u;\n",
438 (unsigned int)offsetof(typeof(field
), data
),
439 (unsigned int)BUF_PAGE_SIZE
,
440 (unsigned int)is_signed_type(char));
446 * head_page == tail_page && head == tail then buffer is empty.
448 struct ring_buffer_per_cpu
{
450 atomic_t record_disabled
;
451 struct ring_buffer
*buffer
;
452 raw_spinlock_t reader_lock
; /* serialize readers */
453 arch_spinlock_t lock
;
454 struct lock_class_key lock_key
;
455 unsigned int nr_pages
;
456 struct list_head
*pages
;
457 struct buffer_page
*head_page
; /* read from head */
458 struct buffer_page
*tail_page
; /* write to tail */
459 struct buffer_page
*commit_page
; /* committed pages */
460 struct buffer_page
*reader_page
;
461 unsigned long lost_events
;
462 unsigned long last_overrun
;
463 local_t entries_bytes
;
466 local_t commit_overrun
;
467 local_t dropped_events
;
471 unsigned long read_bytes
;
474 /* ring buffer pages to update, > 0 to add, < 0 to remove */
475 int nr_pages_to_update
;
476 struct list_head new_pages
; /* new pages to add */
477 struct work_struct update_pages_work
;
478 struct completion update_done
;
484 atomic_t record_disabled
;
485 atomic_t resize_disabled
;
486 cpumask_var_t cpumask
;
488 struct lock_class_key
*reader_lock_key
;
492 struct ring_buffer_per_cpu
**buffers
;
494 #ifdef CONFIG_HOTPLUG_CPU
495 struct notifier_block cpu_notify
;
500 struct ring_buffer_iter
{
501 struct ring_buffer_per_cpu
*cpu_buffer
;
503 struct buffer_page
*head_page
;
504 struct buffer_page
*cache_reader_page
;
505 unsigned long cache_read
;
509 /* buffer may be either ring_buffer or ring_buffer_per_cpu */
510 #define RB_WARN_ON(b, cond) \
512 int _____ret = unlikely(cond); \
514 if (__same_type(*(b), struct ring_buffer_per_cpu)) { \
515 struct ring_buffer_per_cpu *__b = \
517 atomic_inc(&__b->buffer->record_disabled); \
519 atomic_inc(&b->record_disabled); \
525 /* Up this if you want to test the TIME_EXTENTS and normalization */
526 #define DEBUG_SHIFT 0
528 static inline u64
rb_time_stamp(struct ring_buffer
*buffer
)
530 /* shift to debug/test normalization and TIME_EXTENTS */
531 return buffer
->clock() << DEBUG_SHIFT
;
534 u64
ring_buffer_time_stamp(struct ring_buffer
*buffer
, int cpu
)
538 preempt_disable_notrace();
539 time
= rb_time_stamp(buffer
);
540 preempt_enable_no_resched_notrace();
544 EXPORT_SYMBOL_GPL(ring_buffer_time_stamp
);
546 void ring_buffer_normalize_time_stamp(struct ring_buffer
*buffer
,
549 /* Just stupid testing the normalize function and deltas */
552 EXPORT_SYMBOL_GPL(ring_buffer_normalize_time_stamp
);
555 * Making the ring buffer lockless makes things tricky.
556 * Although writes only happen on the CPU that they are on,
557 * and they only need to worry about interrupts. Reads can
560 * The reader page is always off the ring buffer, but when the
561 * reader finishes with a page, it needs to swap its page with
562 * a new one from the buffer. The reader needs to take from
563 * the head (writes go to the tail). But if a writer is in overwrite
564 * mode and wraps, it must push the head page forward.
566 * Here lies the problem.
568 * The reader must be careful to replace only the head page, and
569 * not another one. As described at the top of the file in the
570 * ASCII art, the reader sets its old page to point to the next
571 * page after head. It then sets the page after head to point to
572 * the old reader page. But if the writer moves the head page
573 * during this operation, the reader could end up with the tail.
575 * We use cmpxchg to help prevent this race. We also do something
576 * special with the page before head. We set the LSB to 1.
578 * When the writer must push the page forward, it will clear the
579 * bit that points to the head page, move the head, and then set
580 * the bit that points to the new head page.
582 * We also don't want an interrupt coming in and moving the head
583 * page on another writer. Thus we use the second LSB to catch
586 * head->list->prev->next bit 1 bit 0
589 * Points to head page 0 1
592 * Note we can not trust the prev pointer of the head page, because:
594 * +----+ +-----+ +-----+
595 * | |------>| T |---X--->| N |
597 * +----+ +-----+ +-----+
600 * +----------| R |----------+ |
604 * Key: ---X--> HEAD flag set in pointer
609 * (see __rb_reserve_next() to see where this happens)
611 * What the above shows is that the reader just swapped out
612 * the reader page with a page in the buffer, but before it
613 * could make the new header point back to the new page added
614 * it was preempted by a writer. The writer moved forward onto
615 * the new page added by the reader and is about to move forward
618 * You can see, it is legitimate for the previous pointer of
619 * the head (or any page) not to point back to itself. But only
623 #define RB_PAGE_NORMAL 0UL
624 #define RB_PAGE_HEAD 1UL
625 #define RB_PAGE_UPDATE 2UL
628 #define RB_FLAG_MASK 3UL
630 /* PAGE_MOVED is not part of the mask */
631 #define RB_PAGE_MOVED 4UL
634 * rb_list_head - remove any bit
636 static struct list_head
*rb_list_head(struct list_head
*list
)
638 unsigned long val
= (unsigned long)list
;
640 return (struct list_head
*)(val
& ~RB_FLAG_MASK
);
644 * rb_is_head_page - test if the given page is the head page
646 * Because the reader may move the head_page pointer, we can
647 * not trust what the head page is (it may be pointing to
648 * the reader page). But if the next page is a header page,
649 * its flags will be non zero.
652 rb_is_head_page(struct ring_buffer_per_cpu
*cpu_buffer
,
653 struct buffer_page
*page
, struct list_head
*list
)
657 val
= (unsigned long)list
->next
;
659 if ((val
& ~RB_FLAG_MASK
) != (unsigned long)&page
->list
)
660 return RB_PAGE_MOVED
;
662 return val
& RB_FLAG_MASK
;
668 * The unique thing about the reader page, is that, if the
669 * writer is ever on it, the previous pointer never points
670 * back to the reader page.
672 static int rb_is_reader_page(struct buffer_page
*page
)
674 struct list_head
*list
= page
->list
.prev
;
676 return rb_list_head(list
->next
) != &page
->list
;
680 * rb_set_list_to_head - set a list_head to be pointing to head.
682 static void rb_set_list_to_head(struct ring_buffer_per_cpu
*cpu_buffer
,
683 struct list_head
*list
)
687 ptr
= (unsigned long *)&list
->next
;
688 *ptr
|= RB_PAGE_HEAD
;
689 *ptr
&= ~RB_PAGE_UPDATE
;
693 * rb_head_page_activate - sets up head page
695 static void rb_head_page_activate(struct ring_buffer_per_cpu
*cpu_buffer
)
697 struct buffer_page
*head
;
699 head
= cpu_buffer
->head_page
;
704 * Set the previous list pointer to have the HEAD flag.
706 rb_set_list_to_head(cpu_buffer
, head
->list
.prev
);
709 static void rb_list_head_clear(struct list_head
*list
)
711 unsigned long *ptr
= (unsigned long *)&list
->next
;
713 *ptr
&= ~RB_FLAG_MASK
;
717 * rb_head_page_dactivate - clears head page ptr (for free list)
720 rb_head_page_deactivate(struct ring_buffer_per_cpu
*cpu_buffer
)
722 struct list_head
*hd
;
724 /* Go through the whole list and clear any pointers found. */
725 rb_list_head_clear(cpu_buffer
->pages
);
727 list_for_each(hd
, cpu_buffer
->pages
)
728 rb_list_head_clear(hd
);
731 static int rb_head_page_set(struct ring_buffer_per_cpu
*cpu_buffer
,
732 struct buffer_page
*head
,
733 struct buffer_page
*prev
,
734 int old_flag
, int new_flag
)
736 struct list_head
*list
;
737 unsigned long val
= (unsigned long)&head
->list
;
742 val
&= ~RB_FLAG_MASK
;
744 ret
= cmpxchg((unsigned long *)&list
->next
,
745 val
| old_flag
, val
| new_flag
);
747 /* check if the reader took the page */
748 if ((ret
& ~RB_FLAG_MASK
) != val
)
749 return RB_PAGE_MOVED
;
751 return ret
& RB_FLAG_MASK
;
754 static int rb_head_page_set_update(struct ring_buffer_per_cpu
*cpu_buffer
,
755 struct buffer_page
*head
,
756 struct buffer_page
*prev
,
759 return rb_head_page_set(cpu_buffer
, head
, prev
,
760 old_flag
, RB_PAGE_UPDATE
);
763 static int rb_head_page_set_head(struct ring_buffer_per_cpu
*cpu_buffer
,
764 struct buffer_page
*head
,
765 struct buffer_page
*prev
,
768 return rb_head_page_set(cpu_buffer
, head
, prev
,
769 old_flag
, RB_PAGE_HEAD
);
772 static int rb_head_page_set_normal(struct ring_buffer_per_cpu
*cpu_buffer
,
773 struct buffer_page
*head
,
774 struct buffer_page
*prev
,
777 return rb_head_page_set(cpu_buffer
, head
, prev
,
778 old_flag
, RB_PAGE_NORMAL
);
781 static inline void rb_inc_page(struct ring_buffer_per_cpu
*cpu_buffer
,
782 struct buffer_page
**bpage
)
784 struct list_head
*p
= rb_list_head((*bpage
)->list
.next
);
786 *bpage
= list_entry(p
, struct buffer_page
, list
);
789 static struct buffer_page
*
790 rb_set_head_page(struct ring_buffer_per_cpu
*cpu_buffer
)
792 struct buffer_page
*head
;
793 struct buffer_page
*page
;
794 struct list_head
*list
;
797 if (RB_WARN_ON(cpu_buffer
, !cpu_buffer
->head_page
))
801 list
= cpu_buffer
->pages
;
802 if (RB_WARN_ON(cpu_buffer
, rb_list_head(list
->prev
->next
) != list
))
805 page
= head
= cpu_buffer
->head_page
;
807 * It is possible that the writer moves the header behind
808 * where we started, and we miss in one loop.
809 * A second loop should grab the header, but we'll do
810 * three loops just because I'm paranoid.
812 for (i
= 0; i
< 3; i
++) {
814 if (rb_is_head_page(cpu_buffer
, page
, page
->list
.prev
)) {
815 cpu_buffer
->head_page
= page
;
818 rb_inc_page(cpu_buffer
, &page
);
819 } while (page
!= head
);
822 RB_WARN_ON(cpu_buffer
, 1);
827 static int rb_head_page_replace(struct buffer_page
*old
,
828 struct buffer_page
*new)
830 unsigned long *ptr
= (unsigned long *)&old
->list
.prev
->next
;
834 val
= *ptr
& ~RB_FLAG_MASK
;
837 ret
= cmpxchg(ptr
, val
, (unsigned long)&new->list
);
843 * rb_tail_page_update - move the tail page forward
845 * Returns 1 if moved tail page, 0 if someone else did.
847 static int rb_tail_page_update(struct ring_buffer_per_cpu
*cpu_buffer
,
848 struct buffer_page
*tail_page
,
849 struct buffer_page
*next_page
)
851 struct buffer_page
*old_tail
;
852 unsigned long old_entries
;
853 unsigned long old_write
;
857 * The tail page now needs to be moved forward.
859 * We need to reset the tail page, but without messing
860 * with possible erasing of data brought in by interrupts
861 * that have moved the tail page and are currently on it.
863 * We add a counter to the write field to denote this.
865 old_write
= local_add_return(RB_WRITE_INTCNT
, &next_page
->write
);
866 old_entries
= local_add_return(RB_WRITE_INTCNT
, &next_page
->entries
);
869 * Just make sure we have seen our old_write and synchronize
870 * with any interrupts that come in.
875 * If the tail page is still the same as what we think
876 * it is, then it is up to us to update the tail
879 if (tail_page
== cpu_buffer
->tail_page
) {
880 /* Zero the write counter */
881 unsigned long val
= old_write
& ~RB_WRITE_MASK
;
882 unsigned long eval
= old_entries
& ~RB_WRITE_MASK
;
885 * This will only succeed if an interrupt did
886 * not come in and change it. In which case, we
887 * do not want to modify it.
889 * We add (void) to let the compiler know that we do not care
890 * about the return value of these functions. We use the
891 * cmpxchg to only update if an interrupt did not already
892 * do it for us. If the cmpxchg fails, we don't care.
894 (void)local_cmpxchg(&next_page
->write
, old_write
, val
);
895 (void)local_cmpxchg(&next_page
->entries
, old_entries
, eval
);
898 * No need to worry about races with clearing out the commit.
899 * it only can increment when a commit takes place. But that
900 * only happens in the outer most nested commit.
902 local_set(&next_page
->page
->commit
, 0);
904 old_tail
= cmpxchg(&cpu_buffer
->tail_page
,
905 tail_page
, next_page
);
907 if (old_tail
== tail_page
)
914 static int rb_check_bpage(struct ring_buffer_per_cpu
*cpu_buffer
,
915 struct buffer_page
*bpage
)
917 unsigned long val
= (unsigned long)bpage
;
919 if (RB_WARN_ON(cpu_buffer
, val
& RB_FLAG_MASK
))
926 * rb_check_list - make sure a pointer to a list has the last bits zero
928 static int rb_check_list(struct ring_buffer_per_cpu
*cpu_buffer
,
929 struct list_head
*list
)
931 if (RB_WARN_ON(cpu_buffer
, rb_list_head(list
->prev
) != list
->prev
))
933 if (RB_WARN_ON(cpu_buffer
, rb_list_head(list
->next
) != list
->next
))
939 * check_pages - integrity check of buffer pages
940 * @cpu_buffer: CPU buffer with pages to test
942 * As a safety measure we check to make sure the data pages have not
945 static int rb_check_pages(struct ring_buffer_per_cpu
*cpu_buffer
)
947 struct list_head
*head
= cpu_buffer
->pages
;
948 struct buffer_page
*bpage
, *tmp
;
950 /* Reset the head page if it exists */
951 if (cpu_buffer
->head_page
)
952 rb_set_head_page(cpu_buffer
);
954 rb_head_page_deactivate(cpu_buffer
);
956 if (RB_WARN_ON(cpu_buffer
, head
->next
->prev
!= head
))
958 if (RB_WARN_ON(cpu_buffer
, head
->prev
->next
!= head
))
961 if (rb_check_list(cpu_buffer
, head
))
964 list_for_each_entry_safe(bpage
, tmp
, head
, list
) {
965 if (RB_WARN_ON(cpu_buffer
,
966 bpage
->list
.next
->prev
!= &bpage
->list
))
968 if (RB_WARN_ON(cpu_buffer
,
969 bpage
->list
.prev
->next
!= &bpage
->list
))
971 if (rb_check_list(cpu_buffer
, &bpage
->list
))
975 rb_head_page_activate(cpu_buffer
);
980 static int __rb_allocate_pages(int nr_pages
, struct list_head
*pages
, int cpu
)
983 struct buffer_page
*bpage
, *tmp
;
985 for (i
= 0; i
< nr_pages
; i
++) {
988 * __GFP_NORETRY flag makes sure that the allocation fails
989 * gracefully without invoking oom-killer and the system is
992 bpage
= kzalloc_node(ALIGN(sizeof(*bpage
), cache_line_size()),
993 GFP_KERNEL
| __GFP_NORETRY
,
998 list_add(&bpage
->list
, pages
);
1000 page
= alloc_pages_node(cpu_to_node(cpu
),
1001 GFP_KERNEL
| __GFP_NORETRY
, 0);
1004 bpage
->page
= page_address(page
);
1005 rb_init_page(bpage
->page
);
1011 list_for_each_entry_safe(bpage
, tmp
, pages
, list
) {
1012 list_del_init(&bpage
->list
);
1013 free_buffer_page(bpage
);
1019 static int rb_allocate_pages(struct ring_buffer_per_cpu
*cpu_buffer
,
1026 if (__rb_allocate_pages(nr_pages
, &pages
, cpu_buffer
->cpu
))
1030 * The ring buffer page list is a circular list that does not
1031 * start and end with a list head. All page list items point to
1034 cpu_buffer
->pages
= pages
.next
;
1037 cpu_buffer
->nr_pages
= nr_pages
;
1039 rb_check_pages(cpu_buffer
);
1044 static struct ring_buffer_per_cpu
*
1045 rb_allocate_cpu_buffer(struct ring_buffer
*buffer
, int nr_pages
, int cpu
)
1047 struct ring_buffer_per_cpu
*cpu_buffer
;
1048 struct buffer_page
*bpage
;
1052 cpu_buffer
= kzalloc_node(ALIGN(sizeof(*cpu_buffer
), cache_line_size()),
1053 GFP_KERNEL
, cpu_to_node(cpu
));
1057 cpu_buffer
->cpu
= cpu
;
1058 cpu_buffer
->buffer
= buffer
;
1059 raw_spin_lock_init(&cpu_buffer
->reader_lock
);
1060 lockdep_set_class(&cpu_buffer
->reader_lock
, buffer
->reader_lock_key
);
1061 cpu_buffer
->lock
= (arch_spinlock_t
)__ARCH_SPIN_LOCK_UNLOCKED
;
1062 INIT_WORK(&cpu_buffer
->update_pages_work
, update_pages_handler
);
1063 init_completion(&cpu_buffer
->update_done
);
1065 bpage
= kzalloc_node(ALIGN(sizeof(*bpage
), cache_line_size()),
1066 GFP_KERNEL
, cpu_to_node(cpu
));
1068 goto fail_free_buffer
;
1070 rb_check_bpage(cpu_buffer
, bpage
);
1072 cpu_buffer
->reader_page
= bpage
;
1073 page
= alloc_pages_node(cpu_to_node(cpu
), GFP_KERNEL
, 0);
1075 goto fail_free_reader
;
1076 bpage
->page
= page_address(page
);
1077 rb_init_page(bpage
->page
);
1079 INIT_LIST_HEAD(&cpu_buffer
->reader_page
->list
);
1080 INIT_LIST_HEAD(&cpu_buffer
->new_pages
);
1082 ret
= rb_allocate_pages(cpu_buffer
, nr_pages
);
1084 goto fail_free_reader
;
1086 cpu_buffer
->head_page
1087 = list_entry(cpu_buffer
->pages
, struct buffer_page
, list
);
1088 cpu_buffer
->tail_page
= cpu_buffer
->commit_page
= cpu_buffer
->head_page
;
1090 rb_head_page_activate(cpu_buffer
);
1095 free_buffer_page(cpu_buffer
->reader_page
);
1102 static void rb_free_cpu_buffer(struct ring_buffer_per_cpu
*cpu_buffer
)
1104 struct list_head
*head
= cpu_buffer
->pages
;
1105 struct buffer_page
*bpage
, *tmp
;
1107 free_buffer_page(cpu_buffer
->reader_page
);
1109 rb_head_page_deactivate(cpu_buffer
);
1112 list_for_each_entry_safe(bpage
, tmp
, head
, list
) {
1113 list_del_init(&bpage
->list
);
1114 free_buffer_page(bpage
);
1116 bpage
= list_entry(head
, struct buffer_page
, list
);
1117 free_buffer_page(bpage
);
1123 #ifdef CONFIG_HOTPLUG_CPU
1124 static int rb_cpu_notify(struct notifier_block
*self
,
1125 unsigned long action
, void *hcpu
);
1129 * ring_buffer_alloc - allocate a new ring_buffer
1130 * @size: the size in bytes per cpu that is needed.
1131 * @flags: attributes to set for the ring buffer.
1133 * Currently the only flag that is available is the RB_FL_OVERWRITE
1134 * flag. This flag means that the buffer will overwrite old data
1135 * when the buffer wraps. If this flag is not set, the buffer will
1136 * drop data when the tail hits the head.
1138 struct ring_buffer
*__ring_buffer_alloc(unsigned long size
, unsigned flags
,
1139 struct lock_class_key
*key
)
1141 struct ring_buffer
*buffer
;
1145 /* keep it in its own cache line */
1146 buffer
= kzalloc(ALIGN(sizeof(*buffer
), cache_line_size()),
1151 if (!alloc_cpumask_var(&buffer
->cpumask
, GFP_KERNEL
))
1152 goto fail_free_buffer
;
1154 nr_pages
= DIV_ROUND_UP(size
, BUF_PAGE_SIZE
);
1155 buffer
->flags
= flags
;
1156 buffer
->clock
= trace_clock_local
;
1157 buffer
->reader_lock_key
= key
;
1159 /* need at least two pages */
1164 * In case of non-hotplug cpu, if the ring-buffer is allocated
1165 * in early initcall, it will not be notified of secondary cpus.
1166 * In that off case, we need to allocate for all possible cpus.
1168 #ifdef CONFIG_HOTPLUG_CPU
1170 cpumask_copy(buffer
->cpumask
, cpu_online_mask
);
1172 cpumask_copy(buffer
->cpumask
, cpu_possible_mask
);
1174 buffer
->cpus
= nr_cpu_ids
;
1176 bsize
= sizeof(void *) * nr_cpu_ids
;
1177 buffer
->buffers
= kzalloc(ALIGN(bsize
, cache_line_size()),
1179 if (!buffer
->buffers
)
1180 goto fail_free_cpumask
;
1182 for_each_buffer_cpu(buffer
, cpu
) {
1183 buffer
->buffers
[cpu
] =
1184 rb_allocate_cpu_buffer(buffer
, nr_pages
, cpu
);
1185 if (!buffer
->buffers
[cpu
])
1186 goto fail_free_buffers
;
1189 #ifdef CONFIG_HOTPLUG_CPU
1190 buffer
->cpu_notify
.notifier_call
= rb_cpu_notify
;
1191 buffer
->cpu_notify
.priority
= 0;
1192 register_cpu_notifier(&buffer
->cpu_notify
);
1196 mutex_init(&buffer
->mutex
);
1201 for_each_buffer_cpu(buffer
, cpu
) {
1202 if (buffer
->buffers
[cpu
])
1203 rb_free_cpu_buffer(buffer
->buffers
[cpu
]);
1205 kfree(buffer
->buffers
);
1208 free_cpumask_var(buffer
->cpumask
);
1215 EXPORT_SYMBOL_GPL(__ring_buffer_alloc
);
1218 * ring_buffer_free - free a ring buffer.
1219 * @buffer: the buffer to free.
1222 ring_buffer_free(struct ring_buffer
*buffer
)
1228 #ifdef CONFIG_HOTPLUG_CPU
1229 unregister_cpu_notifier(&buffer
->cpu_notify
);
1232 for_each_buffer_cpu(buffer
, cpu
)
1233 rb_free_cpu_buffer(buffer
->buffers
[cpu
]);
1237 kfree(buffer
->buffers
);
1238 free_cpumask_var(buffer
->cpumask
);
1242 EXPORT_SYMBOL_GPL(ring_buffer_free
);
1244 void ring_buffer_set_clock(struct ring_buffer
*buffer
,
1247 buffer
->clock
= clock
;
1250 static void rb_reset_cpu(struct ring_buffer_per_cpu
*cpu_buffer
);
1252 static inline unsigned long rb_page_entries(struct buffer_page
*bpage
)
1254 return local_read(&bpage
->entries
) & RB_WRITE_MASK
;
1257 static inline unsigned long rb_page_write(struct buffer_page
*bpage
)
1259 return local_read(&bpage
->write
) & RB_WRITE_MASK
;
1263 rb_remove_pages(struct ring_buffer_per_cpu
*cpu_buffer
, unsigned int nr_pages
)
1265 struct list_head
*tail_page
, *to_remove
, *next_page
;
1266 struct buffer_page
*to_remove_page
, *tmp_iter_page
;
1267 struct buffer_page
*last_page
, *first_page
;
1268 unsigned int nr_removed
;
1269 unsigned long head_bit
;
1274 raw_spin_lock_irq(&cpu_buffer
->reader_lock
);
1275 atomic_inc(&cpu_buffer
->record_disabled
);
1277 * We don't race with the readers since we have acquired the reader
1278 * lock. We also don't race with writers after disabling recording.
1279 * This makes it easy to figure out the first and the last page to be
1280 * removed from the list. We unlink all the pages in between including
1281 * the first and last pages. This is done in a busy loop so that we
1282 * lose the least number of traces.
1283 * The pages are freed after we restart recording and unlock readers.
1285 tail_page
= &cpu_buffer
->tail_page
->list
;
1288 * tail page might be on reader page, we remove the next page
1289 * from the ring buffer
1291 if (cpu_buffer
->tail_page
== cpu_buffer
->reader_page
)
1292 tail_page
= rb_list_head(tail_page
->next
);
1293 to_remove
= tail_page
;
1295 /* start of pages to remove */
1296 first_page
= list_entry(rb_list_head(to_remove
->next
),
1297 struct buffer_page
, list
);
1299 for (nr_removed
= 0; nr_removed
< nr_pages
; nr_removed
++) {
1300 to_remove
= rb_list_head(to_remove
)->next
;
1301 head_bit
|= (unsigned long)to_remove
& RB_PAGE_HEAD
;
1304 next_page
= rb_list_head(to_remove
)->next
;
1307 * Now we remove all pages between tail_page and next_page.
1308 * Make sure that we have head_bit value preserved for the
1311 tail_page
->next
= (struct list_head
*)((unsigned long)next_page
|
1313 next_page
= rb_list_head(next_page
);
1314 next_page
->prev
= tail_page
;
1316 /* make sure pages points to a valid page in the ring buffer */
1317 cpu_buffer
->pages
= next_page
;
1319 /* update head page */
1321 cpu_buffer
->head_page
= list_entry(next_page
,
1322 struct buffer_page
, list
);
1325 * change read pointer to make sure any read iterators reset
1328 cpu_buffer
->read
= 0;
1330 /* pages are removed, resume tracing and then free the pages */
1331 atomic_dec(&cpu_buffer
->record_disabled
);
1332 raw_spin_unlock_irq(&cpu_buffer
->reader_lock
);
1334 RB_WARN_ON(cpu_buffer
, list_empty(cpu_buffer
->pages
));
1336 /* last buffer page to remove */
1337 last_page
= list_entry(rb_list_head(to_remove
), struct buffer_page
,
1339 tmp_iter_page
= first_page
;
1342 to_remove_page
= tmp_iter_page
;
1343 rb_inc_page(cpu_buffer
, &tmp_iter_page
);
1345 /* update the counters */
1346 page_entries
= rb_page_entries(to_remove_page
);
1349 * If something was added to this page, it was full
1350 * since it is not the tail page. So we deduct the
1351 * bytes consumed in ring buffer from here.
1352 * Increment overrun to account for the lost events.
1354 local_add(page_entries
, &cpu_buffer
->overrun
);
1355 local_sub(BUF_PAGE_SIZE
, &cpu_buffer
->entries_bytes
);
1359 * We have already removed references to this list item, just
1360 * free up the buffer_page and its page
1362 free_buffer_page(to_remove_page
);
1365 } while (to_remove_page
!= last_page
);
1367 RB_WARN_ON(cpu_buffer
, nr_removed
);
1369 return nr_removed
== 0;
1373 rb_insert_pages(struct ring_buffer_per_cpu
*cpu_buffer
)
1375 struct list_head
*pages
= &cpu_buffer
->new_pages
;
1376 int retries
, success
;
1378 raw_spin_lock_irq(&cpu_buffer
->reader_lock
);
1380 * We are holding the reader lock, so the reader page won't be swapped
1381 * in the ring buffer. Now we are racing with the writer trying to
1382 * move head page and the tail page.
1383 * We are going to adapt the reader page update process where:
1384 * 1. We first splice the start and end of list of new pages between
1385 * the head page and its previous page.
1386 * 2. We cmpxchg the prev_page->next to point from head page to the
1387 * start of new pages list.
1388 * 3. Finally, we update the head->prev to the end of new list.
1390 * We will try this process 10 times, to make sure that we don't keep
1396 struct list_head
*head_page
, *prev_page
, *r
;
1397 struct list_head
*last_page
, *first_page
;
1398 struct list_head
*head_page_with_bit
;
1400 head_page
= &rb_set_head_page(cpu_buffer
)->list
;
1403 prev_page
= head_page
->prev
;
1405 first_page
= pages
->next
;
1406 last_page
= pages
->prev
;
1408 head_page_with_bit
= (struct list_head
*)
1409 ((unsigned long)head_page
| RB_PAGE_HEAD
);
1411 last_page
->next
= head_page_with_bit
;
1412 first_page
->prev
= prev_page
;
1414 r
= cmpxchg(&prev_page
->next
, head_page_with_bit
, first_page
);
1416 if (r
== head_page_with_bit
) {
1418 * yay, we replaced the page pointer to our new list,
1419 * now, we just have to update to head page's prev
1420 * pointer to point to end of list
1422 head_page
->prev
= last_page
;
1429 INIT_LIST_HEAD(pages
);
1431 * If we weren't successful in adding in new pages, warn and stop
1434 RB_WARN_ON(cpu_buffer
, !success
);
1435 raw_spin_unlock_irq(&cpu_buffer
->reader_lock
);
1437 /* free pages if they weren't inserted */
1439 struct buffer_page
*bpage
, *tmp
;
1440 list_for_each_entry_safe(bpage
, tmp
, &cpu_buffer
->new_pages
,
1442 list_del_init(&bpage
->list
);
1443 free_buffer_page(bpage
);
1449 static void rb_update_pages(struct ring_buffer_per_cpu
*cpu_buffer
)
1453 if (cpu_buffer
->nr_pages_to_update
> 0)
1454 success
= rb_insert_pages(cpu_buffer
);
1456 success
= rb_remove_pages(cpu_buffer
,
1457 -cpu_buffer
->nr_pages_to_update
);
1460 cpu_buffer
->nr_pages
+= cpu_buffer
->nr_pages_to_update
;
1463 static void update_pages_handler(struct work_struct
*work
)
1465 struct ring_buffer_per_cpu
*cpu_buffer
= container_of(work
,
1466 struct ring_buffer_per_cpu
, update_pages_work
);
1467 rb_update_pages(cpu_buffer
);
1468 complete(&cpu_buffer
->update_done
);
1472 * ring_buffer_resize - resize the ring buffer
1473 * @buffer: the buffer to resize.
1474 * @size: the new size.
1476 * Minimum size is 2 * BUF_PAGE_SIZE.
1478 * Returns 0 on success and < 0 on failure.
1480 int ring_buffer_resize(struct ring_buffer
*buffer
, unsigned long size
,
1483 struct ring_buffer_per_cpu
*cpu_buffer
;
1488 * Always succeed at resizing a non-existent buffer:
1493 /* Make sure the requested buffer exists */
1494 if (cpu_id
!= RING_BUFFER_ALL_CPUS
&&
1495 !cpumask_test_cpu(cpu_id
, buffer
->cpumask
))
1498 size
= DIV_ROUND_UP(size
, BUF_PAGE_SIZE
);
1499 size
*= BUF_PAGE_SIZE
;
1501 /* we need a minimum of two pages */
1502 if (size
< BUF_PAGE_SIZE
* 2)
1503 size
= BUF_PAGE_SIZE
* 2;
1505 nr_pages
= DIV_ROUND_UP(size
, BUF_PAGE_SIZE
);
1508 * Don't succeed if resizing is disabled, as a reader might be
1509 * manipulating the ring buffer and is expecting a sane state while
1512 if (atomic_read(&buffer
->resize_disabled
))
1515 /* prevent another thread from changing buffer sizes */
1516 mutex_lock(&buffer
->mutex
);
1518 if (cpu_id
== RING_BUFFER_ALL_CPUS
) {
1519 /* calculate the pages to update */
1520 for_each_buffer_cpu(buffer
, cpu
) {
1521 cpu_buffer
= buffer
->buffers
[cpu
];
1523 cpu_buffer
->nr_pages_to_update
= nr_pages
-
1524 cpu_buffer
->nr_pages
;
1526 * nothing more to do for removing pages or no update
1528 if (cpu_buffer
->nr_pages_to_update
<= 0)
1531 * to add pages, make sure all new pages can be
1532 * allocated without receiving ENOMEM
1534 INIT_LIST_HEAD(&cpu_buffer
->new_pages
);
1535 if (__rb_allocate_pages(cpu_buffer
->nr_pages_to_update
,
1536 &cpu_buffer
->new_pages
, cpu
)) {
1537 /* not enough memory for new pages */
1545 * Fire off all the required work handlers
1546 * We can't schedule on offline CPUs, but it's not necessary
1547 * since we can change their buffer sizes without any race.
1549 for_each_buffer_cpu(buffer
, cpu
) {
1550 cpu_buffer
= buffer
->buffers
[cpu
];
1551 if (!cpu_buffer
->nr_pages_to_update
)
1554 if (cpu_online(cpu
))
1555 schedule_work_on(cpu
,
1556 &cpu_buffer
->update_pages_work
);
1558 rb_update_pages(cpu_buffer
);
1561 /* wait for all the updates to complete */
1562 for_each_buffer_cpu(buffer
, cpu
) {
1563 cpu_buffer
= buffer
->buffers
[cpu
];
1564 if (!cpu_buffer
->nr_pages_to_update
)
1567 if (cpu_online(cpu
))
1568 wait_for_completion(&cpu_buffer
->update_done
);
1569 cpu_buffer
->nr_pages_to_update
= 0;
1574 /* Make sure this CPU has been intitialized */
1575 if (!cpumask_test_cpu(cpu_id
, buffer
->cpumask
))
1578 cpu_buffer
= buffer
->buffers
[cpu_id
];
1580 if (nr_pages
== cpu_buffer
->nr_pages
)
1583 cpu_buffer
->nr_pages_to_update
= nr_pages
-
1584 cpu_buffer
->nr_pages
;
1586 INIT_LIST_HEAD(&cpu_buffer
->new_pages
);
1587 if (cpu_buffer
->nr_pages_to_update
> 0 &&
1588 __rb_allocate_pages(cpu_buffer
->nr_pages_to_update
,
1589 &cpu_buffer
->new_pages
, cpu_id
)) {
1596 if (cpu_online(cpu_id
)) {
1597 schedule_work_on(cpu_id
,
1598 &cpu_buffer
->update_pages_work
);
1599 wait_for_completion(&cpu_buffer
->update_done
);
1601 rb_update_pages(cpu_buffer
);
1603 cpu_buffer
->nr_pages_to_update
= 0;
1609 * The ring buffer resize can happen with the ring buffer
1610 * enabled, so that the update disturbs the tracing as little
1611 * as possible. But if the buffer is disabled, we do not need
1612 * to worry about that, and we can take the time to verify
1613 * that the buffer is not corrupt.
1615 if (atomic_read(&buffer
->record_disabled
)) {
1616 atomic_inc(&buffer
->record_disabled
);
1618 * Even though the buffer was disabled, we must make sure
1619 * that it is truly disabled before calling rb_check_pages.
1620 * There could have been a race between checking
1621 * record_disable and incrementing it.
1623 synchronize_sched();
1624 for_each_buffer_cpu(buffer
, cpu
) {
1625 cpu_buffer
= buffer
->buffers
[cpu
];
1626 rb_check_pages(cpu_buffer
);
1628 atomic_dec(&buffer
->record_disabled
);
1631 mutex_unlock(&buffer
->mutex
);
1635 for_each_buffer_cpu(buffer
, cpu
) {
1636 struct buffer_page
*bpage
, *tmp
;
1638 cpu_buffer
= buffer
->buffers
[cpu
];
1639 cpu_buffer
->nr_pages_to_update
= 0;
1641 if (list_empty(&cpu_buffer
->new_pages
))
1644 list_for_each_entry_safe(bpage
, tmp
, &cpu_buffer
->new_pages
,
1646 list_del_init(&bpage
->list
);
1647 free_buffer_page(bpage
);
1650 mutex_unlock(&buffer
->mutex
);
1653 EXPORT_SYMBOL_GPL(ring_buffer_resize
);
1655 void ring_buffer_change_overwrite(struct ring_buffer
*buffer
, int val
)
1657 mutex_lock(&buffer
->mutex
);
1659 buffer
->flags
|= RB_FL_OVERWRITE
;
1661 buffer
->flags
&= ~RB_FL_OVERWRITE
;
1662 mutex_unlock(&buffer
->mutex
);
1664 EXPORT_SYMBOL_GPL(ring_buffer_change_overwrite
);
1666 static inline void *
1667 __rb_data_page_index(struct buffer_data_page
*bpage
, unsigned index
)
1669 return bpage
->data
+ index
;
1672 static inline void *__rb_page_index(struct buffer_page
*bpage
, unsigned index
)
1674 return bpage
->page
->data
+ index
;
1677 static inline struct ring_buffer_event
*
1678 rb_reader_event(struct ring_buffer_per_cpu
*cpu_buffer
)
1680 return __rb_page_index(cpu_buffer
->reader_page
,
1681 cpu_buffer
->reader_page
->read
);
1684 static inline struct ring_buffer_event
*
1685 rb_iter_head_event(struct ring_buffer_iter
*iter
)
1687 return __rb_page_index(iter
->head_page
, iter
->head
);
1690 static inline unsigned rb_page_commit(struct buffer_page
*bpage
)
1692 return local_read(&bpage
->page
->commit
);
1695 /* Size is determined by what has been committed */
1696 static inline unsigned rb_page_size(struct buffer_page
*bpage
)
1698 return rb_page_commit(bpage
);
1701 static inline unsigned
1702 rb_commit_index(struct ring_buffer_per_cpu
*cpu_buffer
)
1704 return rb_page_commit(cpu_buffer
->commit_page
);
1707 static inline unsigned
1708 rb_event_index(struct ring_buffer_event
*event
)
1710 unsigned long addr
= (unsigned long)event
;
1712 return (addr
& ~PAGE_MASK
) - BUF_PAGE_HDR_SIZE
;
1716 rb_event_is_commit(struct ring_buffer_per_cpu
*cpu_buffer
,
1717 struct ring_buffer_event
*event
)
1719 unsigned long addr
= (unsigned long)event
;
1720 unsigned long index
;
1722 index
= rb_event_index(event
);
1725 return cpu_buffer
->commit_page
->page
== (void *)addr
&&
1726 rb_commit_index(cpu_buffer
) == index
;
1730 rb_set_commit_to_write(struct ring_buffer_per_cpu
*cpu_buffer
)
1732 unsigned long max_count
;
1735 * We only race with interrupts and NMIs on this CPU.
1736 * If we own the commit event, then we can commit
1737 * all others that interrupted us, since the interruptions
1738 * are in stack format (they finish before they come
1739 * back to us). This allows us to do a simple loop to
1740 * assign the commit to the tail.
1743 max_count
= cpu_buffer
->nr_pages
* 100;
1745 while (cpu_buffer
->commit_page
!= cpu_buffer
->tail_page
) {
1746 if (RB_WARN_ON(cpu_buffer
, !(--max_count
)))
1748 if (RB_WARN_ON(cpu_buffer
,
1749 rb_is_reader_page(cpu_buffer
->tail_page
)))
1751 local_set(&cpu_buffer
->commit_page
->page
->commit
,
1752 rb_page_write(cpu_buffer
->commit_page
));
1753 rb_inc_page(cpu_buffer
, &cpu_buffer
->commit_page
);
1754 cpu_buffer
->write_stamp
=
1755 cpu_buffer
->commit_page
->page
->time_stamp
;
1756 /* add barrier to keep gcc from optimizing too much */
1759 while (rb_commit_index(cpu_buffer
) !=
1760 rb_page_write(cpu_buffer
->commit_page
)) {
1762 local_set(&cpu_buffer
->commit_page
->page
->commit
,
1763 rb_page_write(cpu_buffer
->commit_page
));
1764 RB_WARN_ON(cpu_buffer
,
1765 local_read(&cpu_buffer
->commit_page
->page
->commit
) &
1770 /* again, keep gcc from optimizing */
1774 * If an interrupt came in just after the first while loop
1775 * and pushed the tail page forward, we will be left with
1776 * a dangling commit that will never go forward.
1778 if (unlikely(cpu_buffer
->commit_page
!= cpu_buffer
->tail_page
))
1782 static void rb_reset_reader_page(struct ring_buffer_per_cpu
*cpu_buffer
)
1784 cpu_buffer
->read_stamp
= cpu_buffer
->reader_page
->page
->time_stamp
;
1785 cpu_buffer
->reader_page
->read
= 0;
1788 static void rb_inc_iter(struct ring_buffer_iter
*iter
)
1790 struct ring_buffer_per_cpu
*cpu_buffer
= iter
->cpu_buffer
;
1793 * The iterator could be on the reader page (it starts there).
1794 * But the head could have moved, since the reader was
1795 * found. Check for this case and assign the iterator
1796 * to the head page instead of next.
1798 if (iter
->head_page
== cpu_buffer
->reader_page
)
1799 iter
->head_page
= rb_set_head_page(cpu_buffer
);
1801 rb_inc_page(cpu_buffer
, &iter
->head_page
);
1803 iter
->read_stamp
= iter
->head_page
->page
->time_stamp
;
1807 /* Slow path, do not inline */
1808 static noinline
struct ring_buffer_event
*
1809 rb_add_time_stamp(struct ring_buffer_event
*event
, u64 delta
)
1811 event
->type_len
= RINGBUF_TYPE_TIME_EXTEND
;
1813 /* Not the first event on the page? */
1814 if (rb_event_index(event
)) {
1815 event
->time_delta
= delta
& TS_MASK
;
1816 event
->array
[0] = delta
>> TS_SHIFT
;
1818 /* nope, just zero it */
1819 event
->time_delta
= 0;
1820 event
->array
[0] = 0;
1823 return skip_time_extend(event
);
1827 * rb_update_event - update event type and data
1828 * @event: the even to update
1829 * @type: the type of event
1830 * @length: the size of the event field in the ring buffer
1832 * Update the type and data fields of the event. The length
1833 * is the actual size that is written to the ring buffer,
1834 * and with this, we can determine what to place into the
1838 rb_update_event(struct ring_buffer_per_cpu
*cpu_buffer
,
1839 struct ring_buffer_event
*event
, unsigned length
,
1840 int add_timestamp
, u64 delta
)
1842 /* Only a commit updates the timestamp */
1843 if (unlikely(!rb_event_is_commit(cpu_buffer
, event
)))
1847 * If we need to add a timestamp, then we
1848 * add it to the start of the resevered space.
1850 if (unlikely(add_timestamp
)) {
1851 event
= rb_add_time_stamp(event
, delta
);
1852 length
-= RB_LEN_TIME_EXTEND
;
1856 event
->time_delta
= delta
;
1857 length
-= RB_EVNT_HDR_SIZE
;
1858 if (length
> RB_MAX_SMALL_DATA
|| RB_FORCE_8BYTE_ALIGNMENT
) {
1859 event
->type_len
= 0;
1860 event
->array
[0] = length
;
1862 event
->type_len
= DIV_ROUND_UP(length
, RB_ALIGNMENT
);
1866 * rb_handle_head_page - writer hit the head page
1868 * Returns: +1 to retry page
1873 rb_handle_head_page(struct ring_buffer_per_cpu
*cpu_buffer
,
1874 struct buffer_page
*tail_page
,
1875 struct buffer_page
*next_page
)
1877 struct buffer_page
*new_head
;
1882 entries
= rb_page_entries(next_page
);
1885 * The hard part is here. We need to move the head
1886 * forward, and protect against both readers on
1887 * other CPUs and writers coming in via interrupts.
1889 type
= rb_head_page_set_update(cpu_buffer
, next_page
, tail_page
,
1893 * type can be one of four:
1894 * NORMAL - an interrupt already moved it for us
1895 * HEAD - we are the first to get here.
1896 * UPDATE - we are the interrupt interrupting
1898 * MOVED - a reader on another CPU moved the next
1899 * pointer to its reader page. Give up
1906 * We changed the head to UPDATE, thus
1907 * it is our responsibility to update
1910 local_add(entries
, &cpu_buffer
->overrun
);
1911 local_sub(BUF_PAGE_SIZE
, &cpu_buffer
->entries_bytes
);
1914 * The entries will be zeroed out when we move the
1918 /* still more to do */
1921 case RB_PAGE_UPDATE
:
1923 * This is an interrupt that interrupt the
1924 * previous update. Still more to do.
1927 case RB_PAGE_NORMAL
:
1929 * An interrupt came in before the update
1930 * and processed this for us.
1931 * Nothing left to do.
1936 * The reader is on another CPU and just did
1937 * a swap with our next_page.
1942 RB_WARN_ON(cpu_buffer
, 1); /* WTF??? */
1947 * Now that we are here, the old head pointer is
1948 * set to UPDATE. This will keep the reader from
1949 * swapping the head page with the reader page.
1950 * The reader (on another CPU) will spin till
1953 * We just need to protect against interrupts
1954 * doing the job. We will set the next pointer
1955 * to HEAD. After that, we set the old pointer
1956 * to NORMAL, but only if it was HEAD before.
1957 * otherwise we are an interrupt, and only
1958 * want the outer most commit to reset it.
1960 new_head
= next_page
;
1961 rb_inc_page(cpu_buffer
, &new_head
);
1963 ret
= rb_head_page_set_head(cpu_buffer
, new_head
, next_page
,
1967 * Valid returns are:
1968 * HEAD - an interrupt came in and already set it.
1969 * NORMAL - One of two things:
1970 * 1) We really set it.
1971 * 2) A bunch of interrupts came in and moved
1972 * the page forward again.
1976 case RB_PAGE_NORMAL
:
1980 RB_WARN_ON(cpu_buffer
, 1);
1985 * It is possible that an interrupt came in,
1986 * set the head up, then more interrupts came in
1987 * and moved it again. When we get back here,
1988 * the page would have been set to NORMAL but we
1989 * just set it back to HEAD.
1991 * How do you detect this? Well, if that happened
1992 * the tail page would have moved.
1994 if (ret
== RB_PAGE_NORMAL
) {
1996 * If the tail had moved passed next, then we need
1997 * to reset the pointer.
1999 if (cpu_buffer
->tail_page
!= tail_page
&&
2000 cpu_buffer
->tail_page
!= next_page
)
2001 rb_head_page_set_normal(cpu_buffer
, new_head
,
2007 * If this was the outer most commit (the one that
2008 * changed the original pointer from HEAD to UPDATE),
2009 * then it is up to us to reset it to NORMAL.
2011 if (type
== RB_PAGE_HEAD
) {
2012 ret
= rb_head_page_set_normal(cpu_buffer
, next_page
,
2015 if (RB_WARN_ON(cpu_buffer
,
2016 ret
!= RB_PAGE_UPDATE
))
2023 static unsigned rb_calculate_event_length(unsigned length
)
2025 struct ring_buffer_event event
; /* Used only for sizeof array */
2027 /* zero length can cause confusions */
2031 if (length
> RB_MAX_SMALL_DATA
|| RB_FORCE_8BYTE_ALIGNMENT
)
2032 length
+= sizeof(event
.array
[0]);
2034 length
+= RB_EVNT_HDR_SIZE
;
2035 length
= ALIGN(length
, RB_ARCH_ALIGNMENT
);
2041 rb_reset_tail(struct ring_buffer_per_cpu
*cpu_buffer
,
2042 struct buffer_page
*tail_page
,
2043 unsigned long tail
, unsigned long length
)
2045 struct ring_buffer_event
*event
;
2048 * Only the event that crossed the page boundary
2049 * must fill the old tail_page with padding.
2051 if (tail
>= BUF_PAGE_SIZE
) {
2053 * If the page was filled, then we still need
2054 * to update the real_end. Reset it to zero
2055 * and the reader will ignore it.
2057 if (tail
== BUF_PAGE_SIZE
)
2058 tail_page
->real_end
= 0;
2060 local_sub(length
, &tail_page
->write
);
2064 event
= __rb_page_index(tail_page
, tail
);
2065 kmemcheck_annotate_bitfield(event
, bitfield
);
2067 /* account for padding bytes */
2068 local_add(BUF_PAGE_SIZE
- tail
, &cpu_buffer
->entries_bytes
);
2071 * Save the original length to the meta data.
2072 * This will be used by the reader to add lost event
2075 tail_page
->real_end
= tail
;
2078 * If this event is bigger than the minimum size, then
2079 * we need to be careful that we don't subtract the
2080 * write counter enough to allow another writer to slip
2082 * We put in a discarded commit instead, to make sure
2083 * that this space is not used again.
2085 * If we are less than the minimum size, we don't need to
2088 if (tail
> (BUF_PAGE_SIZE
- RB_EVNT_MIN_SIZE
)) {
2089 /* No room for any events */
2091 /* Mark the rest of the page with padding */
2092 rb_event_set_padding(event
);
2094 /* Set the write back to the previous setting */
2095 local_sub(length
, &tail_page
->write
);
2099 /* Put in a discarded event */
2100 event
->array
[0] = (BUF_PAGE_SIZE
- tail
) - RB_EVNT_HDR_SIZE
;
2101 event
->type_len
= RINGBUF_TYPE_PADDING
;
2102 /* time delta must be non zero */
2103 event
->time_delta
= 1;
2105 /* Set write to end of buffer */
2106 length
= (tail
+ length
) - BUF_PAGE_SIZE
;
2107 local_sub(length
, &tail_page
->write
);
2111 * This is the slow path, force gcc not to inline it.
2113 static noinline
struct ring_buffer_event
*
2114 rb_move_tail(struct ring_buffer_per_cpu
*cpu_buffer
,
2115 unsigned long length
, unsigned long tail
,
2116 struct buffer_page
*tail_page
, u64 ts
)
2118 struct buffer_page
*commit_page
= cpu_buffer
->commit_page
;
2119 struct ring_buffer
*buffer
= cpu_buffer
->buffer
;
2120 struct buffer_page
*next_page
;
2123 next_page
= tail_page
;
2125 rb_inc_page(cpu_buffer
, &next_page
);
2128 * If for some reason, we had an interrupt storm that made
2129 * it all the way around the buffer, bail, and warn
2132 if (unlikely(next_page
== commit_page
)) {
2133 local_inc(&cpu_buffer
->commit_overrun
);
2138 * This is where the fun begins!
2140 * We are fighting against races between a reader that
2141 * could be on another CPU trying to swap its reader
2142 * page with the buffer head.
2144 * We are also fighting against interrupts coming in and
2145 * moving the head or tail on us as well.
2147 * If the next page is the head page then we have filled
2148 * the buffer, unless the commit page is still on the
2151 if (rb_is_head_page(cpu_buffer
, next_page
, &tail_page
->list
)) {
2154 * If the commit is not on the reader page, then
2155 * move the header page.
2157 if (!rb_is_reader_page(cpu_buffer
->commit_page
)) {
2159 * If we are not in overwrite mode,
2160 * this is easy, just stop here.
2162 if (!(buffer
->flags
& RB_FL_OVERWRITE
)) {
2163 local_inc(&cpu_buffer
->dropped_events
);
2167 ret
= rb_handle_head_page(cpu_buffer
,
2176 * We need to be careful here too. The
2177 * commit page could still be on the reader
2178 * page. We could have a small buffer, and
2179 * have filled up the buffer with events
2180 * from interrupts and such, and wrapped.
2182 * Note, if the tail page is also the on the
2183 * reader_page, we let it move out.
2185 if (unlikely((cpu_buffer
->commit_page
!=
2186 cpu_buffer
->tail_page
) &&
2187 (cpu_buffer
->commit_page
==
2188 cpu_buffer
->reader_page
))) {
2189 local_inc(&cpu_buffer
->commit_overrun
);
2195 ret
= rb_tail_page_update(cpu_buffer
, tail_page
, next_page
);
2198 * Nested commits always have zero deltas, so
2199 * just reread the time stamp
2201 ts
= rb_time_stamp(buffer
);
2202 next_page
->page
->time_stamp
= ts
;
2207 rb_reset_tail(cpu_buffer
, tail_page
, tail
, length
);
2209 /* fail and let the caller try again */
2210 return ERR_PTR(-EAGAIN
);
2214 rb_reset_tail(cpu_buffer
, tail_page
, tail
, length
);
2219 static struct ring_buffer_event
*
2220 __rb_reserve_next(struct ring_buffer_per_cpu
*cpu_buffer
,
2221 unsigned long length
, u64 ts
,
2222 u64 delta
, int add_timestamp
)
2224 struct buffer_page
*tail_page
;
2225 struct ring_buffer_event
*event
;
2226 unsigned long tail
, write
;
2229 * If the time delta since the last event is too big to
2230 * hold in the time field of the event, then we append a
2231 * TIME EXTEND event ahead of the data event.
2233 if (unlikely(add_timestamp
))
2234 length
+= RB_LEN_TIME_EXTEND
;
2236 tail_page
= cpu_buffer
->tail_page
;
2237 write
= local_add_return(length
, &tail_page
->write
);
2239 /* set write to only the index of the write */
2240 write
&= RB_WRITE_MASK
;
2241 tail
= write
- length
;
2243 /* See if we shot pass the end of this buffer page */
2244 if (unlikely(write
> BUF_PAGE_SIZE
))
2245 return rb_move_tail(cpu_buffer
, length
, tail
,
2248 /* We reserved something on the buffer */
2250 event
= __rb_page_index(tail_page
, tail
);
2251 kmemcheck_annotate_bitfield(event
, bitfield
);
2252 rb_update_event(cpu_buffer
, event
, length
, add_timestamp
, delta
);
2254 local_inc(&tail_page
->entries
);
2257 * If this is the first commit on the page, then update
2261 tail_page
->page
->time_stamp
= ts
;
2263 /* account for these added bytes */
2264 local_add(length
, &cpu_buffer
->entries_bytes
);
2270 rb_try_to_discard(struct ring_buffer_per_cpu
*cpu_buffer
,
2271 struct ring_buffer_event
*event
)
2273 unsigned long new_index
, old_index
;
2274 struct buffer_page
*bpage
;
2275 unsigned long index
;
2278 new_index
= rb_event_index(event
);
2279 old_index
= new_index
+ rb_event_ts_length(event
);
2280 addr
= (unsigned long)event
;
2283 bpage
= cpu_buffer
->tail_page
;
2285 if (bpage
->page
== (void *)addr
&& rb_page_write(bpage
) == old_index
) {
2286 unsigned long write_mask
=
2287 local_read(&bpage
->write
) & ~RB_WRITE_MASK
;
2288 unsigned long event_length
= rb_event_length(event
);
2290 * This is on the tail page. It is possible that
2291 * a write could come in and move the tail page
2292 * and write to the next page. That is fine
2293 * because we just shorten what is on this page.
2295 old_index
+= write_mask
;
2296 new_index
+= write_mask
;
2297 index
= local_cmpxchg(&bpage
->write
, old_index
, new_index
);
2298 if (index
== old_index
) {
2299 /* update counters */
2300 local_sub(event_length
, &cpu_buffer
->entries_bytes
);
2305 /* could not discard */
2309 static void rb_start_commit(struct ring_buffer_per_cpu
*cpu_buffer
)
2311 local_inc(&cpu_buffer
->committing
);
2312 local_inc(&cpu_buffer
->commits
);
2315 static inline void rb_end_commit(struct ring_buffer_per_cpu
*cpu_buffer
)
2317 unsigned long commits
;
2319 if (RB_WARN_ON(cpu_buffer
,
2320 !local_read(&cpu_buffer
->committing
)))
2324 commits
= local_read(&cpu_buffer
->commits
);
2325 /* synchronize with interrupts */
2327 if (local_read(&cpu_buffer
->committing
) == 1)
2328 rb_set_commit_to_write(cpu_buffer
);
2330 local_dec(&cpu_buffer
->committing
);
2332 /* synchronize with interrupts */
2336 * Need to account for interrupts coming in between the
2337 * updating of the commit page and the clearing of the
2338 * committing counter.
2340 if (unlikely(local_read(&cpu_buffer
->commits
) != commits
) &&
2341 !local_read(&cpu_buffer
->committing
)) {
2342 local_inc(&cpu_buffer
->committing
);
2347 static struct ring_buffer_event
*
2348 rb_reserve_next_event(struct ring_buffer
*buffer
,
2349 struct ring_buffer_per_cpu
*cpu_buffer
,
2350 unsigned long length
)
2352 struct ring_buffer_event
*event
;
2358 rb_start_commit(cpu_buffer
);
2360 #ifdef CONFIG_RING_BUFFER_ALLOW_SWAP
2362 * Due to the ability to swap a cpu buffer from a buffer
2363 * it is possible it was swapped before we committed.
2364 * (committing stops a swap). We check for it here and
2365 * if it happened, we have to fail the write.
2368 if (unlikely(ACCESS_ONCE(cpu_buffer
->buffer
) != buffer
)) {
2369 local_dec(&cpu_buffer
->committing
);
2370 local_dec(&cpu_buffer
->commits
);
2375 length
= rb_calculate_event_length(length
);
2381 * We allow for interrupts to reenter here and do a trace.
2382 * If one does, it will cause this original code to loop
2383 * back here. Even with heavy interrupts happening, this
2384 * should only happen a few times in a row. If this happens
2385 * 1000 times in a row, there must be either an interrupt
2386 * storm or we have something buggy.
2389 if (RB_WARN_ON(cpu_buffer
, ++nr_loops
> 1000))
2392 ts
= rb_time_stamp(cpu_buffer
->buffer
);
2393 diff
= ts
- cpu_buffer
->write_stamp
;
2395 /* make sure this diff is calculated here */
2398 /* Did the write stamp get updated already? */
2399 if (likely(ts
>= cpu_buffer
->write_stamp
)) {
2401 if (unlikely(test_time_stamp(delta
))) {
2402 int local_clock_stable
= 1;
2403 #ifdef CONFIG_HAVE_UNSTABLE_SCHED_CLOCK
2404 local_clock_stable
= sched_clock_stable
;
2406 WARN_ONCE(delta
> (1ULL << 59),
2407 KERN_WARNING
"Delta way too big! %llu ts=%llu write stamp = %llu\n%s",
2408 (unsigned long long)delta
,
2409 (unsigned long long)ts
,
2410 (unsigned long long)cpu_buffer
->write_stamp
,
2411 local_clock_stable
? "" :
2412 "If you just came from a suspend/resume,\n"
2413 "please switch to the trace global clock:\n"
2414 " echo global > /sys/kernel/debug/tracing/trace_clock\n");
2419 event
= __rb_reserve_next(cpu_buffer
, length
, ts
,
2420 delta
, add_timestamp
);
2421 if (unlikely(PTR_ERR(event
) == -EAGAIN
))
2430 rb_end_commit(cpu_buffer
);
2434 #ifdef CONFIG_TRACING
2437 * The lock and unlock are done within a preempt disable section.
2438 * The current_context per_cpu variable can only be modified
2439 * by the current task between lock and unlock. But it can
2440 * be modified more than once via an interrupt. To pass this
2441 * information from the lock to the unlock without having to
2442 * access the 'in_interrupt()' functions again (which do show
2443 * a bit of overhead in something as critical as function tracing,
2444 * we use a bitmask trick.
2446 * bit 0 = NMI context
2447 * bit 1 = IRQ context
2448 * bit 2 = SoftIRQ context
2449 * bit 3 = normal context.
2451 * This works because this is the order of contexts that can
2452 * preempt other contexts. A SoftIRQ never preempts an IRQ
2455 * When the context is determined, the corresponding bit is
2456 * checked and set (if it was set, then a recursion of that context
2459 * On unlock, we need to clear this bit. To do so, just subtract
2460 * 1 from the current_context and AND it to itself.
2464 * 101 & 100 = 100 (clearing bit zero)
2467 * 1010 & 1001 = 1000 (clearing bit 1)
2469 * The least significant bit can be cleared this way, and it
2470 * just so happens that it is the same bit corresponding to
2471 * the current context.
2473 static DEFINE_PER_CPU(unsigned int, current_context
);
2475 static __always_inline
int trace_recursive_lock(void)
2477 unsigned int val
= this_cpu_read(current_context
);
2480 if (in_interrupt()) {
2490 if (unlikely(val
& (1 << bit
)))
2494 this_cpu_write(current_context
, val
);
2499 static __always_inline
void trace_recursive_unlock(void)
2501 unsigned int val
= this_cpu_read(current_context
);
2504 val
&= this_cpu_read(current_context
);
2505 this_cpu_write(current_context
, val
);
2510 #define trace_recursive_lock() (0)
2511 #define trace_recursive_unlock() do { } while (0)
2516 * ring_buffer_lock_reserve - reserve a part of the buffer
2517 * @buffer: the ring buffer to reserve from
2518 * @length: the length of the data to reserve (excluding event header)
2520 * Returns a reseverd event on the ring buffer to copy directly to.
2521 * The user of this interface will need to get the body to write into
2522 * and can use the ring_buffer_event_data() interface.
2524 * The length is the length of the data needed, not the event length
2525 * which also includes the event header.
2527 * Must be paired with ring_buffer_unlock_commit, unless NULL is returned.
2528 * If NULL is returned, then nothing has been allocated or locked.
2530 struct ring_buffer_event
*
2531 ring_buffer_lock_reserve(struct ring_buffer
*buffer
, unsigned long length
)
2533 struct ring_buffer_per_cpu
*cpu_buffer
;
2534 struct ring_buffer_event
*event
;
2537 if (ring_buffer_flags
!= RB_BUFFERS_ON
)
2540 /* If we are tracing schedule, we don't want to recurse */
2541 preempt_disable_notrace();
2543 if (atomic_read(&buffer
->record_disabled
))
2546 if (trace_recursive_lock())
2549 cpu
= raw_smp_processor_id();
2551 if (!cpumask_test_cpu(cpu
, buffer
->cpumask
))
2554 cpu_buffer
= buffer
->buffers
[cpu
];
2556 if (atomic_read(&cpu_buffer
->record_disabled
))
2559 if (length
> BUF_MAX_DATA_SIZE
)
2562 event
= rb_reserve_next_event(buffer
, cpu_buffer
, length
);
2569 trace_recursive_unlock();
2572 preempt_enable_notrace();
2575 EXPORT_SYMBOL_GPL(ring_buffer_lock_reserve
);
2578 rb_update_write_stamp(struct ring_buffer_per_cpu
*cpu_buffer
,
2579 struct ring_buffer_event
*event
)
2584 * The event first in the commit queue updates the
2587 if (rb_event_is_commit(cpu_buffer
, event
)) {
2589 * A commit event that is first on a page
2590 * updates the write timestamp with the page stamp
2592 if (!rb_event_index(event
))
2593 cpu_buffer
->write_stamp
=
2594 cpu_buffer
->commit_page
->page
->time_stamp
;
2595 else if (event
->type_len
== RINGBUF_TYPE_TIME_EXTEND
) {
2596 delta
= event
->array
[0];
2598 delta
+= event
->time_delta
;
2599 cpu_buffer
->write_stamp
+= delta
;
2601 cpu_buffer
->write_stamp
+= event
->time_delta
;
2605 static void rb_commit(struct ring_buffer_per_cpu
*cpu_buffer
,
2606 struct ring_buffer_event
*event
)
2608 local_inc(&cpu_buffer
->entries
);
2609 rb_update_write_stamp(cpu_buffer
, event
);
2610 rb_end_commit(cpu_buffer
);
2614 * ring_buffer_unlock_commit - commit a reserved
2615 * @buffer: The buffer to commit to
2616 * @event: The event pointer to commit.
2618 * This commits the data to the ring buffer, and releases any locks held.
2620 * Must be paired with ring_buffer_lock_reserve.
2622 int ring_buffer_unlock_commit(struct ring_buffer
*buffer
,
2623 struct ring_buffer_event
*event
)
2625 struct ring_buffer_per_cpu
*cpu_buffer
;
2626 int cpu
= raw_smp_processor_id();
2628 cpu_buffer
= buffer
->buffers
[cpu
];
2630 rb_commit(cpu_buffer
, event
);
2632 trace_recursive_unlock();
2634 preempt_enable_notrace();
2638 EXPORT_SYMBOL_GPL(ring_buffer_unlock_commit
);
2640 static inline void rb_event_discard(struct ring_buffer_event
*event
)
2642 if (event
->type_len
== RINGBUF_TYPE_TIME_EXTEND
)
2643 event
= skip_time_extend(event
);
2645 /* array[0] holds the actual length for the discarded event */
2646 event
->array
[0] = rb_event_data_length(event
) - RB_EVNT_HDR_SIZE
;
2647 event
->type_len
= RINGBUF_TYPE_PADDING
;
2648 /* time delta must be non zero */
2649 if (!event
->time_delta
)
2650 event
->time_delta
= 1;
2654 * Decrement the entries to the page that an event is on.
2655 * The event does not even need to exist, only the pointer
2656 * to the page it is on. This may only be called before the commit
2660 rb_decrement_entry(struct ring_buffer_per_cpu
*cpu_buffer
,
2661 struct ring_buffer_event
*event
)
2663 unsigned long addr
= (unsigned long)event
;
2664 struct buffer_page
*bpage
= cpu_buffer
->commit_page
;
2665 struct buffer_page
*start
;
2669 /* Do the likely case first */
2670 if (likely(bpage
->page
== (void *)addr
)) {
2671 local_dec(&bpage
->entries
);
2676 * Because the commit page may be on the reader page we
2677 * start with the next page and check the end loop there.
2679 rb_inc_page(cpu_buffer
, &bpage
);
2682 if (bpage
->page
== (void *)addr
) {
2683 local_dec(&bpage
->entries
);
2686 rb_inc_page(cpu_buffer
, &bpage
);
2687 } while (bpage
!= start
);
2689 /* commit not part of this buffer?? */
2690 RB_WARN_ON(cpu_buffer
, 1);
2694 * ring_buffer_commit_discard - discard an event that has not been committed
2695 * @buffer: the ring buffer
2696 * @event: non committed event to discard
2698 * Sometimes an event that is in the ring buffer needs to be ignored.
2699 * This function lets the user discard an event in the ring buffer
2700 * and then that event will not be read later.
2702 * This function only works if it is called before the the item has been
2703 * committed. It will try to free the event from the ring buffer
2704 * if another event has not been added behind it.
2706 * If another event has been added behind it, it will set the event
2707 * up as discarded, and perform the commit.
2709 * If this function is called, do not call ring_buffer_unlock_commit on
2712 void ring_buffer_discard_commit(struct ring_buffer
*buffer
,
2713 struct ring_buffer_event
*event
)
2715 struct ring_buffer_per_cpu
*cpu_buffer
;
2718 /* The event is discarded regardless */
2719 rb_event_discard(event
);
2721 cpu
= smp_processor_id();
2722 cpu_buffer
= buffer
->buffers
[cpu
];
2725 * This must only be called if the event has not been
2726 * committed yet. Thus we can assume that preemption
2727 * is still disabled.
2729 RB_WARN_ON(buffer
, !local_read(&cpu_buffer
->committing
));
2731 rb_decrement_entry(cpu_buffer
, event
);
2732 if (rb_try_to_discard(cpu_buffer
, event
))
2736 * The commit is still visible by the reader, so we
2737 * must still update the timestamp.
2739 rb_update_write_stamp(cpu_buffer
, event
);
2741 rb_end_commit(cpu_buffer
);
2743 trace_recursive_unlock();
2745 preempt_enable_notrace();
2748 EXPORT_SYMBOL_GPL(ring_buffer_discard_commit
);
2751 * ring_buffer_write - write data to the buffer without reserving
2752 * @buffer: The ring buffer to write to.
2753 * @length: The length of the data being written (excluding the event header)
2754 * @data: The data to write to the buffer.
2756 * This is like ring_buffer_lock_reserve and ring_buffer_unlock_commit as
2757 * one function. If you already have the data to write to the buffer, it
2758 * may be easier to simply call this function.
2760 * Note, like ring_buffer_lock_reserve, the length is the length of the data
2761 * and not the length of the event which would hold the header.
2763 int ring_buffer_write(struct ring_buffer
*buffer
,
2764 unsigned long length
,
2767 struct ring_buffer_per_cpu
*cpu_buffer
;
2768 struct ring_buffer_event
*event
;
2773 if (ring_buffer_flags
!= RB_BUFFERS_ON
)
2776 preempt_disable_notrace();
2778 if (atomic_read(&buffer
->record_disabled
))
2781 cpu
= raw_smp_processor_id();
2783 if (!cpumask_test_cpu(cpu
, buffer
->cpumask
))
2786 cpu_buffer
= buffer
->buffers
[cpu
];
2788 if (atomic_read(&cpu_buffer
->record_disabled
))
2791 if (length
> BUF_MAX_DATA_SIZE
)
2794 event
= rb_reserve_next_event(buffer
, cpu_buffer
, length
);
2798 body
= rb_event_data(event
);
2800 memcpy(body
, data
, length
);
2802 rb_commit(cpu_buffer
, event
);
2806 preempt_enable_notrace();
2810 EXPORT_SYMBOL_GPL(ring_buffer_write
);
2812 static int rb_per_cpu_empty(struct ring_buffer_per_cpu
*cpu_buffer
)
2814 struct buffer_page
*reader
= cpu_buffer
->reader_page
;
2815 struct buffer_page
*head
= rb_set_head_page(cpu_buffer
);
2816 struct buffer_page
*commit
= cpu_buffer
->commit_page
;
2818 /* In case of error, head will be NULL */
2819 if (unlikely(!head
))
2822 return reader
->read
== rb_page_commit(reader
) &&
2823 (commit
== reader
||
2825 head
->read
== rb_page_commit(commit
)));
2829 * ring_buffer_record_disable - stop all writes into the buffer
2830 * @buffer: The ring buffer to stop writes to.
2832 * This prevents all writes to the buffer. Any attempt to write
2833 * to the buffer after this will fail and return NULL.
2835 * The caller should call synchronize_sched() after this.
2837 void ring_buffer_record_disable(struct ring_buffer
*buffer
)
2839 atomic_inc(&buffer
->record_disabled
);
2841 EXPORT_SYMBOL_GPL(ring_buffer_record_disable
);
2844 * ring_buffer_record_enable - enable writes to the buffer
2845 * @buffer: The ring buffer to enable writes
2847 * Note, multiple disables will need the same number of enables
2848 * to truly enable the writing (much like preempt_disable).
2850 void ring_buffer_record_enable(struct ring_buffer
*buffer
)
2852 atomic_dec(&buffer
->record_disabled
);
2854 EXPORT_SYMBOL_GPL(ring_buffer_record_enable
);
2857 * ring_buffer_record_off - stop all writes into the buffer
2858 * @buffer: The ring buffer to stop writes to.
2860 * This prevents all writes to the buffer. Any attempt to write
2861 * to the buffer after this will fail and return NULL.
2863 * This is different than ring_buffer_record_disable() as
2864 * it works like an on/off switch, where as the disable() version
2865 * must be paired with a enable().
2867 void ring_buffer_record_off(struct ring_buffer
*buffer
)
2870 unsigned int new_rd
;
2873 rd
= atomic_read(&buffer
->record_disabled
);
2874 new_rd
= rd
| RB_BUFFER_OFF
;
2875 } while (atomic_cmpxchg(&buffer
->record_disabled
, rd
, new_rd
) != rd
);
2877 EXPORT_SYMBOL_GPL(ring_buffer_record_off
);
2880 * ring_buffer_record_on - restart writes into the buffer
2881 * @buffer: The ring buffer to start writes to.
2883 * This enables all writes to the buffer that was disabled by
2884 * ring_buffer_record_off().
2886 * This is different than ring_buffer_record_enable() as
2887 * it works like an on/off switch, where as the enable() version
2888 * must be paired with a disable().
2890 void ring_buffer_record_on(struct ring_buffer
*buffer
)
2893 unsigned int new_rd
;
2896 rd
= atomic_read(&buffer
->record_disabled
);
2897 new_rd
= rd
& ~RB_BUFFER_OFF
;
2898 } while (atomic_cmpxchg(&buffer
->record_disabled
, rd
, new_rd
) != rd
);
2900 EXPORT_SYMBOL_GPL(ring_buffer_record_on
);
2903 * ring_buffer_record_is_on - return true if the ring buffer can write
2904 * @buffer: The ring buffer to see if write is enabled
2906 * Returns true if the ring buffer is in a state that it accepts writes.
2908 int ring_buffer_record_is_on(struct ring_buffer
*buffer
)
2910 return !atomic_read(&buffer
->record_disabled
);
2914 * ring_buffer_record_disable_cpu - stop all writes into the cpu_buffer
2915 * @buffer: The ring buffer to stop writes to.
2916 * @cpu: The CPU buffer to stop
2918 * This prevents all writes to the buffer. Any attempt to write
2919 * to the buffer after this will fail and return NULL.
2921 * The caller should call synchronize_sched() after this.
2923 void ring_buffer_record_disable_cpu(struct ring_buffer
*buffer
, int cpu
)
2925 struct ring_buffer_per_cpu
*cpu_buffer
;
2927 if (!cpumask_test_cpu(cpu
, buffer
->cpumask
))
2930 cpu_buffer
= buffer
->buffers
[cpu
];
2931 atomic_inc(&cpu_buffer
->record_disabled
);
2933 EXPORT_SYMBOL_GPL(ring_buffer_record_disable_cpu
);
2936 * ring_buffer_record_enable_cpu - enable writes to the buffer
2937 * @buffer: The ring buffer to enable writes
2938 * @cpu: The CPU to enable.
2940 * Note, multiple disables will need the same number of enables
2941 * to truly enable the writing (much like preempt_disable).
2943 void ring_buffer_record_enable_cpu(struct ring_buffer
*buffer
, int cpu
)
2945 struct ring_buffer_per_cpu
*cpu_buffer
;
2947 if (!cpumask_test_cpu(cpu
, buffer
->cpumask
))
2950 cpu_buffer
= buffer
->buffers
[cpu
];
2951 atomic_dec(&cpu_buffer
->record_disabled
);
2953 EXPORT_SYMBOL_GPL(ring_buffer_record_enable_cpu
);
2956 * The total entries in the ring buffer is the running counter
2957 * of entries entered into the ring buffer, minus the sum of
2958 * the entries read from the ring buffer and the number of
2959 * entries that were overwritten.
2961 static inline unsigned long
2962 rb_num_of_entries(struct ring_buffer_per_cpu
*cpu_buffer
)
2964 return local_read(&cpu_buffer
->entries
) -
2965 (local_read(&cpu_buffer
->overrun
) + cpu_buffer
->read
);
2969 * ring_buffer_oldest_event_ts - get the oldest event timestamp from the buffer
2970 * @buffer: The ring buffer
2971 * @cpu: The per CPU buffer to read from.
2973 u64
ring_buffer_oldest_event_ts(struct ring_buffer
*buffer
, int cpu
)
2975 unsigned long flags
;
2976 struct ring_buffer_per_cpu
*cpu_buffer
;
2977 struct buffer_page
*bpage
;
2980 if (!cpumask_test_cpu(cpu
, buffer
->cpumask
))
2983 cpu_buffer
= buffer
->buffers
[cpu
];
2984 raw_spin_lock_irqsave(&cpu_buffer
->reader_lock
, flags
);
2986 * if the tail is on reader_page, oldest time stamp is on the reader
2989 if (cpu_buffer
->tail_page
== cpu_buffer
->reader_page
)
2990 bpage
= cpu_buffer
->reader_page
;
2992 bpage
= rb_set_head_page(cpu_buffer
);
2994 ret
= bpage
->page
->time_stamp
;
2995 raw_spin_unlock_irqrestore(&cpu_buffer
->reader_lock
, flags
);
2999 EXPORT_SYMBOL_GPL(ring_buffer_oldest_event_ts
);
3002 * ring_buffer_bytes_cpu - get the number of bytes consumed in a cpu buffer
3003 * @buffer: The ring buffer
3004 * @cpu: The per CPU buffer to read from.
3006 unsigned long ring_buffer_bytes_cpu(struct ring_buffer
*buffer
, int cpu
)
3008 struct ring_buffer_per_cpu
*cpu_buffer
;
3011 if (!cpumask_test_cpu(cpu
, buffer
->cpumask
))
3014 cpu_buffer
= buffer
->buffers
[cpu
];
3015 ret
= local_read(&cpu_buffer
->entries_bytes
) - cpu_buffer
->read_bytes
;
3019 EXPORT_SYMBOL_GPL(ring_buffer_bytes_cpu
);
3022 * ring_buffer_entries_cpu - get the number of entries in a cpu buffer
3023 * @buffer: The ring buffer
3024 * @cpu: The per CPU buffer to get the entries from.
3026 unsigned long ring_buffer_entries_cpu(struct ring_buffer
*buffer
, int cpu
)
3028 struct ring_buffer_per_cpu
*cpu_buffer
;
3030 if (!cpumask_test_cpu(cpu
, buffer
->cpumask
))
3033 cpu_buffer
= buffer
->buffers
[cpu
];
3035 return rb_num_of_entries(cpu_buffer
);
3037 EXPORT_SYMBOL_GPL(ring_buffer_entries_cpu
);
3040 * ring_buffer_overrun_cpu - get the number of overruns caused by the ring
3041 * buffer wrapping around (only if RB_FL_OVERWRITE is on).
3042 * @buffer: The ring buffer
3043 * @cpu: The per CPU buffer to get the number of overruns from
3045 unsigned long ring_buffer_overrun_cpu(struct ring_buffer
*buffer
, int cpu
)
3047 struct ring_buffer_per_cpu
*cpu_buffer
;
3050 if (!cpumask_test_cpu(cpu
, buffer
->cpumask
))
3053 cpu_buffer
= buffer
->buffers
[cpu
];
3054 ret
= local_read(&cpu_buffer
->overrun
);
3058 EXPORT_SYMBOL_GPL(ring_buffer_overrun_cpu
);
3061 * ring_buffer_commit_overrun_cpu - get the number of overruns caused by
3062 * commits failing due to the buffer wrapping around while there are uncommitted
3063 * events, such as during an interrupt storm.
3064 * @buffer: The ring buffer
3065 * @cpu: The per CPU buffer to get the number of overruns from
3068 ring_buffer_commit_overrun_cpu(struct ring_buffer
*buffer
, int cpu
)
3070 struct ring_buffer_per_cpu
*cpu_buffer
;
3073 if (!cpumask_test_cpu(cpu
, buffer
->cpumask
))
3076 cpu_buffer
= buffer
->buffers
[cpu
];
3077 ret
= local_read(&cpu_buffer
->commit_overrun
);
3081 EXPORT_SYMBOL_GPL(ring_buffer_commit_overrun_cpu
);
3084 * ring_buffer_dropped_events_cpu - get the number of dropped events caused by
3085 * the ring buffer filling up (only if RB_FL_OVERWRITE is off).
3086 * @buffer: The ring buffer
3087 * @cpu: The per CPU buffer to get the number of overruns from
3090 ring_buffer_dropped_events_cpu(struct ring_buffer
*buffer
, int cpu
)
3092 struct ring_buffer_per_cpu
*cpu_buffer
;
3095 if (!cpumask_test_cpu(cpu
, buffer
->cpumask
))
3098 cpu_buffer
= buffer
->buffers
[cpu
];
3099 ret
= local_read(&cpu_buffer
->dropped_events
);
3103 EXPORT_SYMBOL_GPL(ring_buffer_dropped_events_cpu
);
3106 * ring_buffer_read_events_cpu - get the number of events successfully read
3107 * @buffer: The ring buffer
3108 * @cpu: The per CPU buffer to get the number of events read
3111 ring_buffer_read_events_cpu(struct ring_buffer
*buffer
, int cpu
)
3113 struct ring_buffer_per_cpu
*cpu_buffer
;
3115 if (!cpumask_test_cpu(cpu
, buffer
->cpumask
))
3118 cpu_buffer
= buffer
->buffers
[cpu
];
3119 return cpu_buffer
->read
;
3121 EXPORT_SYMBOL_GPL(ring_buffer_read_events_cpu
);
3124 * ring_buffer_entries - get the number of entries in a buffer
3125 * @buffer: The ring buffer
3127 * Returns the total number of entries in the ring buffer
3130 unsigned long ring_buffer_entries(struct ring_buffer
*buffer
)
3132 struct ring_buffer_per_cpu
*cpu_buffer
;
3133 unsigned long entries
= 0;
3136 /* if you care about this being correct, lock the buffer */
3137 for_each_buffer_cpu(buffer
, cpu
) {
3138 cpu_buffer
= buffer
->buffers
[cpu
];
3139 entries
+= rb_num_of_entries(cpu_buffer
);
3144 EXPORT_SYMBOL_GPL(ring_buffer_entries
);
3147 * ring_buffer_overruns - get the number of overruns in buffer
3148 * @buffer: The ring buffer
3150 * Returns the total number of overruns in the ring buffer
3153 unsigned long ring_buffer_overruns(struct ring_buffer
*buffer
)
3155 struct ring_buffer_per_cpu
*cpu_buffer
;
3156 unsigned long overruns
= 0;
3159 /* if you care about this being correct, lock the buffer */
3160 for_each_buffer_cpu(buffer
, cpu
) {
3161 cpu_buffer
= buffer
->buffers
[cpu
];
3162 overruns
+= local_read(&cpu_buffer
->overrun
);
3167 EXPORT_SYMBOL_GPL(ring_buffer_overruns
);
3169 static void rb_iter_reset(struct ring_buffer_iter
*iter
)
3171 struct ring_buffer_per_cpu
*cpu_buffer
= iter
->cpu_buffer
;
3173 /* Iterator usage is expected to have record disabled */
3174 if (list_empty(&cpu_buffer
->reader_page
->list
)) {
3175 iter
->head_page
= rb_set_head_page(cpu_buffer
);
3176 if (unlikely(!iter
->head_page
))
3178 iter
->head
= iter
->head_page
->read
;
3180 iter
->head_page
= cpu_buffer
->reader_page
;
3181 iter
->head
= cpu_buffer
->reader_page
->read
;
3184 iter
->read_stamp
= cpu_buffer
->read_stamp
;
3186 iter
->read_stamp
= iter
->head_page
->page
->time_stamp
;
3187 iter
->cache_reader_page
= cpu_buffer
->reader_page
;
3188 iter
->cache_read
= cpu_buffer
->read
;
3192 * ring_buffer_iter_reset - reset an iterator
3193 * @iter: The iterator to reset
3195 * Resets the iterator, so that it will start from the beginning
3198 void ring_buffer_iter_reset(struct ring_buffer_iter
*iter
)
3200 struct ring_buffer_per_cpu
*cpu_buffer
;
3201 unsigned long flags
;
3206 cpu_buffer
= iter
->cpu_buffer
;
3208 raw_spin_lock_irqsave(&cpu_buffer
->reader_lock
, flags
);
3209 rb_iter_reset(iter
);
3210 raw_spin_unlock_irqrestore(&cpu_buffer
->reader_lock
, flags
);
3212 EXPORT_SYMBOL_GPL(ring_buffer_iter_reset
);
3215 * ring_buffer_iter_empty - check if an iterator has no more to read
3216 * @iter: The iterator to check
3218 int ring_buffer_iter_empty(struct ring_buffer_iter
*iter
)
3220 struct ring_buffer_per_cpu
*cpu_buffer
;
3222 cpu_buffer
= iter
->cpu_buffer
;
3224 return iter
->head_page
== cpu_buffer
->commit_page
&&
3225 iter
->head
== rb_commit_index(cpu_buffer
);
3227 EXPORT_SYMBOL_GPL(ring_buffer_iter_empty
);
3230 rb_update_read_stamp(struct ring_buffer_per_cpu
*cpu_buffer
,
3231 struct ring_buffer_event
*event
)
3235 switch (event
->type_len
) {
3236 case RINGBUF_TYPE_PADDING
:
3239 case RINGBUF_TYPE_TIME_EXTEND
:
3240 delta
= event
->array
[0];
3242 delta
+= event
->time_delta
;
3243 cpu_buffer
->read_stamp
+= delta
;
3246 case RINGBUF_TYPE_TIME_STAMP
:
3247 /* FIXME: not implemented */
3250 case RINGBUF_TYPE_DATA
:
3251 cpu_buffer
->read_stamp
+= event
->time_delta
;
3261 rb_update_iter_read_stamp(struct ring_buffer_iter
*iter
,
3262 struct ring_buffer_event
*event
)
3266 switch (event
->type_len
) {
3267 case RINGBUF_TYPE_PADDING
:
3270 case RINGBUF_TYPE_TIME_EXTEND
:
3271 delta
= event
->array
[0];
3273 delta
+= event
->time_delta
;
3274 iter
->read_stamp
+= delta
;
3277 case RINGBUF_TYPE_TIME_STAMP
:
3278 /* FIXME: not implemented */
3281 case RINGBUF_TYPE_DATA
:
3282 iter
->read_stamp
+= event
->time_delta
;
3291 static struct buffer_page
*
3292 rb_get_reader_page(struct ring_buffer_per_cpu
*cpu_buffer
)
3294 struct buffer_page
*reader
= NULL
;
3295 unsigned long overwrite
;
3296 unsigned long flags
;
3300 local_irq_save(flags
);
3301 arch_spin_lock(&cpu_buffer
->lock
);
3305 * This should normally only loop twice. But because the
3306 * start of the reader inserts an empty page, it causes
3307 * a case where we will loop three times. There should be no
3308 * reason to loop four times (that I know of).
3310 if (RB_WARN_ON(cpu_buffer
, ++nr_loops
> 3)) {
3315 reader
= cpu_buffer
->reader_page
;
3317 /* If there's more to read, return this page */
3318 if (cpu_buffer
->reader_page
->read
< rb_page_size(reader
))
3321 /* Never should we have an index greater than the size */
3322 if (RB_WARN_ON(cpu_buffer
,
3323 cpu_buffer
->reader_page
->read
> rb_page_size(reader
)))
3326 /* check if we caught up to the tail */
3328 if (cpu_buffer
->commit_page
== cpu_buffer
->reader_page
)
3331 /* Don't bother swapping if the ring buffer is empty */
3332 if (rb_num_of_entries(cpu_buffer
) == 0)
3336 * Reset the reader page to size zero.
3338 local_set(&cpu_buffer
->reader_page
->write
, 0);
3339 local_set(&cpu_buffer
->reader_page
->entries
, 0);
3340 local_set(&cpu_buffer
->reader_page
->page
->commit
, 0);
3341 cpu_buffer
->reader_page
->real_end
= 0;
3345 * Splice the empty reader page into the list around the head.
3347 reader
= rb_set_head_page(cpu_buffer
);
3350 cpu_buffer
->reader_page
->list
.next
= rb_list_head(reader
->list
.next
);
3351 cpu_buffer
->reader_page
->list
.prev
= reader
->list
.prev
;
3354 * cpu_buffer->pages just needs to point to the buffer, it
3355 * has no specific buffer page to point to. Lets move it out
3356 * of our way so we don't accidentally swap it.
3358 cpu_buffer
->pages
= reader
->list
.prev
;
3360 /* The reader page will be pointing to the new head */
3361 rb_set_list_to_head(cpu_buffer
, &cpu_buffer
->reader_page
->list
);
3364 * We want to make sure we read the overruns after we set up our
3365 * pointers to the next object. The writer side does a
3366 * cmpxchg to cross pages which acts as the mb on the writer
3367 * side. Note, the reader will constantly fail the swap
3368 * while the writer is updating the pointers, so this
3369 * guarantees that the overwrite recorded here is the one we
3370 * want to compare with the last_overrun.
3373 overwrite
= local_read(&(cpu_buffer
->overrun
));
3376 * Here's the tricky part.
3378 * We need to move the pointer past the header page.
3379 * But we can only do that if a writer is not currently
3380 * moving it. The page before the header page has the
3381 * flag bit '1' set if it is pointing to the page we want.
3382 * but if the writer is in the process of moving it
3383 * than it will be '2' or already moved '0'.
3386 ret
= rb_head_page_replace(reader
, cpu_buffer
->reader_page
);
3389 * If we did not convert it, then we must try again.
3395 * Yeah! We succeeded in replacing the page.
3397 * Now make the new head point back to the reader page.
3399 rb_list_head(reader
->list
.next
)->prev
= &cpu_buffer
->reader_page
->list
;
3400 rb_inc_page(cpu_buffer
, &cpu_buffer
->head_page
);
3402 /* Finally update the reader page to the new head */
3403 cpu_buffer
->reader_page
= reader
;
3404 rb_reset_reader_page(cpu_buffer
);
3406 if (overwrite
!= cpu_buffer
->last_overrun
) {
3407 cpu_buffer
->lost_events
= overwrite
- cpu_buffer
->last_overrun
;
3408 cpu_buffer
->last_overrun
= overwrite
;
3414 arch_spin_unlock(&cpu_buffer
->lock
);
3415 local_irq_restore(flags
);
3420 static void rb_advance_reader(struct ring_buffer_per_cpu
*cpu_buffer
)
3422 struct ring_buffer_event
*event
;
3423 struct buffer_page
*reader
;
3426 reader
= rb_get_reader_page(cpu_buffer
);
3428 /* This function should not be called when buffer is empty */
3429 if (RB_WARN_ON(cpu_buffer
, !reader
))
3432 event
= rb_reader_event(cpu_buffer
);
3434 if (event
->type_len
<= RINGBUF_TYPE_DATA_TYPE_LEN_MAX
)
3437 rb_update_read_stamp(cpu_buffer
, event
);
3439 length
= rb_event_length(event
);
3440 cpu_buffer
->reader_page
->read
+= length
;
3443 static void rb_advance_iter(struct ring_buffer_iter
*iter
)
3445 struct ring_buffer_per_cpu
*cpu_buffer
;
3446 struct ring_buffer_event
*event
;
3449 cpu_buffer
= iter
->cpu_buffer
;
3452 * Check if we are at the end of the buffer.
3454 if (iter
->head
>= rb_page_size(iter
->head_page
)) {
3455 /* discarded commits can make the page empty */
3456 if (iter
->head_page
== cpu_buffer
->commit_page
)
3462 event
= rb_iter_head_event(iter
);
3464 length
= rb_event_length(event
);
3467 * This should not be called to advance the header if we are
3468 * at the tail of the buffer.
3470 if (RB_WARN_ON(cpu_buffer
,
3471 (iter
->head_page
== cpu_buffer
->commit_page
) &&
3472 (iter
->head
+ length
> rb_commit_index(cpu_buffer
))))
3475 rb_update_iter_read_stamp(iter
, event
);
3477 iter
->head
+= length
;
3479 /* check for end of page padding */
3480 if ((iter
->head
>= rb_page_size(iter
->head_page
)) &&
3481 (iter
->head_page
!= cpu_buffer
->commit_page
))
3485 static int rb_lost_events(struct ring_buffer_per_cpu
*cpu_buffer
)
3487 return cpu_buffer
->lost_events
;
3490 static struct ring_buffer_event
*
3491 rb_buffer_peek(struct ring_buffer_per_cpu
*cpu_buffer
, u64
*ts
,
3492 unsigned long *lost_events
)
3494 struct ring_buffer_event
*event
;
3495 struct buffer_page
*reader
;
3500 * We repeat when a time extend is encountered.
3501 * Since the time extend is always attached to a data event,
3502 * we should never loop more than once.
3503 * (We never hit the following condition more than twice).
3505 if (RB_WARN_ON(cpu_buffer
, ++nr_loops
> 2))
3508 reader
= rb_get_reader_page(cpu_buffer
);
3512 event
= rb_reader_event(cpu_buffer
);
3514 switch (event
->type_len
) {
3515 case RINGBUF_TYPE_PADDING
:
3516 if (rb_null_event(event
))
3517 RB_WARN_ON(cpu_buffer
, 1);
3519 * Because the writer could be discarding every
3520 * event it creates (which would probably be bad)
3521 * if we were to go back to "again" then we may never
3522 * catch up, and will trigger the warn on, or lock
3523 * the box. Return the padding, and we will release
3524 * the current locks, and try again.
3528 case RINGBUF_TYPE_TIME_EXTEND
:
3529 /* Internal data, OK to advance */
3530 rb_advance_reader(cpu_buffer
);
3533 case RINGBUF_TYPE_TIME_STAMP
:
3534 /* FIXME: not implemented */
3535 rb_advance_reader(cpu_buffer
);
3538 case RINGBUF_TYPE_DATA
:
3540 *ts
= cpu_buffer
->read_stamp
+ event
->time_delta
;
3541 ring_buffer_normalize_time_stamp(cpu_buffer
->buffer
,
3542 cpu_buffer
->cpu
, ts
);
3545 *lost_events
= rb_lost_events(cpu_buffer
);
3554 EXPORT_SYMBOL_GPL(ring_buffer_peek
);
3556 static struct ring_buffer_event
*
3557 rb_iter_peek(struct ring_buffer_iter
*iter
, u64
*ts
)
3559 struct ring_buffer
*buffer
;
3560 struct ring_buffer_per_cpu
*cpu_buffer
;
3561 struct ring_buffer_event
*event
;
3564 cpu_buffer
= iter
->cpu_buffer
;
3565 buffer
= cpu_buffer
->buffer
;
3568 * Check if someone performed a consuming read to
3569 * the buffer. A consuming read invalidates the iterator
3570 * and we need to reset the iterator in this case.
3572 if (unlikely(iter
->cache_read
!= cpu_buffer
->read
||
3573 iter
->cache_reader_page
!= cpu_buffer
->reader_page
))
3574 rb_iter_reset(iter
);
3577 if (ring_buffer_iter_empty(iter
))
3581 * We repeat when a time extend is encountered.
3582 * Since the time extend is always attached to a data event,
3583 * we should never loop more than once.
3584 * (We never hit the following condition more than twice).
3586 if (RB_WARN_ON(cpu_buffer
, ++nr_loops
> 2))
3589 if (rb_per_cpu_empty(cpu_buffer
))
3592 if (iter
->head
>= local_read(&iter
->head_page
->page
->commit
)) {
3597 event
= rb_iter_head_event(iter
);
3599 switch (event
->type_len
) {
3600 case RINGBUF_TYPE_PADDING
:
3601 if (rb_null_event(event
)) {
3605 rb_advance_iter(iter
);
3608 case RINGBUF_TYPE_TIME_EXTEND
:
3609 /* Internal data, OK to advance */
3610 rb_advance_iter(iter
);
3613 case RINGBUF_TYPE_TIME_STAMP
:
3614 /* FIXME: not implemented */
3615 rb_advance_iter(iter
);
3618 case RINGBUF_TYPE_DATA
:
3620 *ts
= iter
->read_stamp
+ event
->time_delta
;
3621 ring_buffer_normalize_time_stamp(buffer
,
3622 cpu_buffer
->cpu
, ts
);
3632 EXPORT_SYMBOL_GPL(ring_buffer_iter_peek
);
3634 static inline int rb_ok_to_lock(void)
3637 * If an NMI die dumps out the content of the ring buffer
3638 * do not grab locks. We also permanently disable the ring
3639 * buffer too. A one time deal is all you get from reading
3640 * the ring buffer from an NMI.
3642 if (likely(!in_nmi()))
3645 tracing_off_permanent();
3650 * ring_buffer_peek - peek at the next event to be read
3651 * @buffer: The ring buffer to read
3652 * @cpu: The cpu to peak at
3653 * @ts: The timestamp counter of this event.
3654 * @lost_events: a variable to store if events were lost (may be NULL)
3656 * This will return the event that will be read next, but does
3657 * not consume the data.
3659 struct ring_buffer_event
*
3660 ring_buffer_peek(struct ring_buffer
*buffer
, int cpu
, u64
*ts
,
3661 unsigned long *lost_events
)
3663 struct ring_buffer_per_cpu
*cpu_buffer
= buffer
->buffers
[cpu
];
3664 struct ring_buffer_event
*event
;
3665 unsigned long flags
;
3668 if (!cpumask_test_cpu(cpu
, buffer
->cpumask
))
3671 dolock
= rb_ok_to_lock();
3673 local_irq_save(flags
);
3675 raw_spin_lock(&cpu_buffer
->reader_lock
);
3676 event
= rb_buffer_peek(cpu_buffer
, ts
, lost_events
);
3677 if (event
&& event
->type_len
== RINGBUF_TYPE_PADDING
)
3678 rb_advance_reader(cpu_buffer
);
3680 raw_spin_unlock(&cpu_buffer
->reader_lock
);
3681 local_irq_restore(flags
);
3683 if (event
&& event
->type_len
== RINGBUF_TYPE_PADDING
)
3690 * ring_buffer_iter_peek - peek at the next event to be read
3691 * @iter: The ring buffer iterator
3692 * @ts: The timestamp counter of this event.
3694 * This will return the event that will be read next, but does
3695 * not increment the iterator.
3697 struct ring_buffer_event
*
3698 ring_buffer_iter_peek(struct ring_buffer_iter
*iter
, u64
*ts
)
3700 struct ring_buffer_per_cpu
*cpu_buffer
= iter
->cpu_buffer
;
3701 struct ring_buffer_event
*event
;
3702 unsigned long flags
;
3705 raw_spin_lock_irqsave(&cpu_buffer
->reader_lock
, flags
);
3706 event
= rb_iter_peek(iter
, ts
);
3707 raw_spin_unlock_irqrestore(&cpu_buffer
->reader_lock
, flags
);
3709 if (event
&& event
->type_len
== RINGBUF_TYPE_PADDING
)
3716 * ring_buffer_consume - return an event and consume it
3717 * @buffer: The ring buffer to get the next event from
3718 * @cpu: the cpu to read the buffer from
3719 * @ts: a variable to store the timestamp (may be NULL)
3720 * @lost_events: a variable to store if events were lost (may be NULL)
3722 * Returns the next event in the ring buffer, and that event is consumed.
3723 * Meaning, that sequential reads will keep returning a different event,
3724 * and eventually empty the ring buffer if the producer is slower.
3726 struct ring_buffer_event
*
3727 ring_buffer_consume(struct ring_buffer
*buffer
, int cpu
, u64
*ts
,
3728 unsigned long *lost_events
)
3730 struct ring_buffer_per_cpu
*cpu_buffer
;
3731 struct ring_buffer_event
*event
= NULL
;
3732 unsigned long flags
;
3735 dolock
= rb_ok_to_lock();
3738 /* might be called in atomic */
3741 if (!cpumask_test_cpu(cpu
, buffer
->cpumask
))
3744 cpu_buffer
= buffer
->buffers
[cpu
];
3745 local_irq_save(flags
);
3747 raw_spin_lock(&cpu_buffer
->reader_lock
);
3749 event
= rb_buffer_peek(cpu_buffer
, ts
, lost_events
);
3751 cpu_buffer
->lost_events
= 0;
3752 rb_advance_reader(cpu_buffer
);
3756 raw_spin_unlock(&cpu_buffer
->reader_lock
);
3757 local_irq_restore(flags
);
3762 if (event
&& event
->type_len
== RINGBUF_TYPE_PADDING
)
3767 EXPORT_SYMBOL_GPL(ring_buffer_consume
);
3770 * ring_buffer_read_prepare - Prepare for a non consuming read of the buffer
3771 * @buffer: The ring buffer to read from
3772 * @cpu: The cpu buffer to iterate over
3774 * This performs the initial preparations necessary to iterate
3775 * through the buffer. Memory is allocated, buffer recording
3776 * is disabled, and the iterator pointer is returned to the caller.
3778 * Disabling buffer recordng prevents the reading from being
3779 * corrupted. This is not a consuming read, so a producer is not
3782 * After a sequence of ring_buffer_read_prepare calls, the user is
3783 * expected to make at least one call to ring_buffer_prepare_sync.
3784 * Afterwards, ring_buffer_read_start is invoked to get things going
3787 * This overall must be paired with ring_buffer_finish.
3789 struct ring_buffer_iter
*
3790 ring_buffer_read_prepare(struct ring_buffer
*buffer
, int cpu
)
3792 struct ring_buffer_per_cpu
*cpu_buffer
;
3793 struct ring_buffer_iter
*iter
;
3795 if (!cpumask_test_cpu(cpu
, buffer
->cpumask
))
3798 iter
= kmalloc(sizeof(*iter
), GFP_KERNEL
);
3802 cpu_buffer
= buffer
->buffers
[cpu
];
3804 iter
->cpu_buffer
= cpu_buffer
;
3806 atomic_inc(&buffer
->resize_disabled
);
3807 atomic_inc(&cpu_buffer
->record_disabled
);
3811 EXPORT_SYMBOL_GPL(ring_buffer_read_prepare
);
3814 * ring_buffer_read_prepare_sync - Synchronize a set of prepare calls
3816 * All previously invoked ring_buffer_read_prepare calls to prepare
3817 * iterators will be synchronized. Afterwards, read_buffer_read_start
3818 * calls on those iterators are allowed.
3821 ring_buffer_read_prepare_sync(void)
3823 synchronize_sched();
3825 EXPORT_SYMBOL_GPL(ring_buffer_read_prepare_sync
);
3828 * ring_buffer_read_start - start a non consuming read of the buffer
3829 * @iter: The iterator returned by ring_buffer_read_prepare
3831 * This finalizes the startup of an iteration through the buffer.
3832 * The iterator comes from a call to ring_buffer_read_prepare and
3833 * an intervening ring_buffer_read_prepare_sync must have been
3836 * Must be paired with ring_buffer_finish.
3839 ring_buffer_read_start(struct ring_buffer_iter
*iter
)
3841 struct ring_buffer_per_cpu
*cpu_buffer
;
3842 unsigned long flags
;
3847 cpu_buffer
= iter
->cpu_buffer
;
3849 raw_spin_lock_irqsave(&cpu_buffer
->reader_lock
, flags
);
3850 arch_spin_lock(&cpu_buffer
->lock
);
3851 rb_iter_reset(iter
);
3852 arch_spin_unlock(&cpu_buffer
->lock
);
3853 raw_spin_unlock_irqrestore(&cpu_buffer
->reader_lock
, flags
);
3855 EXPORT_SYMBOL_GPL(ring_buffer_read_start
);
3858 * ring_buffer_finish - finish reading the iterator of the buffer
3859 * @iter: The iterator retrieved by ring_buffer_start
3861 * This re-enables the recording to the buffer, and frees the
3865 ring_buffer_read_finish(struct ring_buffer_iter
*iter
)
3867 struct ring_buffer_per_cpu
*cpu_buffer
= iter
->cpu_buffer
;
3868 unsigned long flags
;
3871 * Ring buffer is disabled from recording, here's a good place
3872 * to check the integrity of the ring buffer.
3873 * Must prevent readers from trying to read, as the check
3874 * clears the HEAD page and readers require it.
3876 raw_spin_lock_irqsave(&cpu_buffer
->reader_lock
, flags
);
3877 rb_check_pages(cpu_buffer
);
3878 raw_spin_unlock_irqrestore(&cpu_buffer
->reader_lock
, flags
);
3880 atomic_dec(&cpu_buffer
->record_disabled
);
3881 atomic_dec(&cpu_buffer
->buffer
->resize_disabled
);
3884 EXPORT_SYMBOL_GPL(ring_buffer_read_finish
);
3887 * ring_buffer_read - read the next item in the ring buffer by the iterator
3888 * @iter: The ring buffer iterator
3889 * @ts: The time stamp of the event read.
3891 * This reads the next event in the ring buffer and increments the iterator.
3893 struct ring_buffer_event
*
3894 ring_buffer_read(struct ring_buffer_iter
*iter
, u64
*ts
)
3896 struct ring_buffer_event
*event
;
3897 struct ring_buffer_per_cpu
*cpu_buffer
= iter
->cpu_buffer
;
3898 unsigned long flags
;
3900 raw_spin_lock_irqsave(&cpu_buffer
->reader_lock
, flags
);
3902 event
= rb_iter_peek(iter
, ts
);
3906 if (event
->type_len
== RINGBUF_TYPE_PADDING
)
3909 rb_advance_iter(iter
);
3911 raw_spin_unlock_irqrestore(&cpu_buffer
->reader_lock
, flags
);
3915 EXPORT_SYMBOL_GPL(ring_buffer_read
);
3918 * ring_buffer_size - return the size of the ring buffer (in bytes)
3919 * @buffer: The ring buffer.
3921 unsigned long ring_buffer_size(struct ring_buffer
*buffer
, int cpu
)
3924 * Earlier, this method returned
3925 * BUF_PAGE_SIZE * buffer->nr_pages
3926 * Since the nr_pages field is now removed, we have converted this to
3927 * return the per cpu buffer value.
3929 if (!cpumask_test_cpu(cpu
, buffer
->cpumask
))
3932 return BUF_PAGE_SIZE
* buffer
->buffers
[cpu
]->nr_pages
;
3934 EXPORT_SYMBOL_GPL(ring_buffer_size
);
3937 rb_reset_cpu(struct ring_buffer_per_cpu
*cpu_buffer
)
3939 rb_head_page_deactivate(cpu_buffer
);
3941 cpu_buffer
->head_page
3942 = list_entry(cpu_buffer
->pages
, struct buffer_page
, list
);
3943 local_set(&cpu_buffer
->head_page
->write
, 0);
3944 local_set(&cpu_buffer
->head_page
->entries
, 0);
3945 local_set(&cpu_buffer
->head_page
->page
->commit
, 0);
3947 cpu_buffer
->head_page
->read
= 0;
3949 cpu_buffer
->tail_page
= cpu_buffer
->head_page
;
3950 cpu_buffer
->commit_page
= cpu_buffer
->head_page
;
3952 INIT_LIST_HEAD(&cpu_buffer
->reader_page
->list
);
3953 INIT_LIST_HEAD(&cpu_buffer
->new_pages
);
3954 local_set(&cpu_buffer
->reader_page
->write
, 0);
3955 local_set(&cpu_buffer
->reader_page
->entries
, 0);
3956 local_set(&cpu_buffer
->reader_page
->page
->commit
, 0);
3957 cpu_buffer
->reader_page
->read
= 0;
3959 local_set(&cpu_buffer
->entries_bytes
, 0);
3960 local_set(&cpu_buffer
->overrun
, 0);
3961 local_set(&cpu_buffer
->commit_overrun
, 0);
3962 local_set(&cpu_buffer
->dropped_events
, 0);
3963 local_set(&cpu_buffer
->entries
, 0);
3964 local_set(&cpu_buffer
->committing
, 0);
3965 local_set(&cpu_buffer
->commits
, 0);
3966 cpu_buffer
->read
= 0;
3967 cpu_buffer
->read_bytes
= 0;
3969 cpu_buffer
->write_stamp
= 0;
3970 cpu_buffer
->read_stamp
= 0;
3972 cpu_buffer
->lost_events
= 0;
3973 cpu_buffer
->last_overrun
= 0;
3975 rb_head_page_activate(cpu_buffer
);
3979 * ring_buffer_reset_cpu - reset a ring buffer per CPU buffer
3980 * @buffer: The ring buffer to reset a per cpu buffer of
3981 * @cpu: The CPU buffer to be reset
3983 void ring_buffer_reset_cpu(struct ring_buffer
*buffer
, int cpu
)
3985 struct ring_buffer_per_cpu
*cpu_buffer
= buffer
->buffers
[cpu
];
3986 unsigned long flags
;
3988 if (!cpumask_test_cpu(cpu
, buffer
->cpumask
))
3991 atomic_inc(&buffer
->resize_disabled
);
3992 atomic_inc(&cpu_buffer
->record_disabled
);
3994 /* Make sure all commits have finished */
3995 synchronize_sched();
3997 raw_spin_lock_irqsave(&cpu_buffer
->reader_lock
, flags
);
3999 if (RB_WARN_ON(cpu_buffer
, local_read(&cpu_buffer
->committing
)))
4002 arch_spin_lock(&cpu_buffer
->lock
);
4004 rb_reset_cpu(cpu_buffer
);
4006 arch_spin_unlock(&cpu_buffer
->lock
);
4009 raw_spin_unlock_irqrestore(&cpu_buffer
->reader_lock
, flags
);
4011 atomic_dec(&cpu_buffer
->record_disabled
);
4012 atomic_dec(&buffer
->resize_disabled
);
4014 EXPORT_SYMBOL_GPL(ring_buffer_reset_cpu
);
4017 * ring_buffer_reset - reset a ring buffer
4018 * @buffer: The ring buffer to reset all cpu buffers
4020 void ring_buffer_reset(struct ring_buffer
*buffer
)
4024 for_each_buffer_cpu(buffer
, cpu
)
4025 ring_buffer_reset_cpu(buffer
, cpu
);
4027 EXPORT_SYMBOL_GPL(ring_buffer_reset
);
4030 * rind_buffer_empty - is the ring buffer empty?
4031 * @buffer: The ring buffer to test
4033 int ring_buffer_empty(struct ring_buffer
*buffer
)
4035 struct ring_buffer_per_cpu
*cpu_buffer
;
4036 unsigned long flags
;
4041 dolock
= rb_ok_to_lock();
4043 /* yes this is racy, but if you don't like the race, lock the buffer */
4044 for_each_buffer_cpu(buffer
, cpu
) {
4045 cpu_buffer
= buffer
->buffers
[cpu
];
4046 local_irq_save(flags
);
4048 raw_spin_lock(&cpu_buffer
->reader_lock
);
4049 ret
= rb_per_cpu_empty(cpu_buffer
);
4051 raw_spin_unlock(&cpu_buffer
->reader_lock
);
4052 local_irq_restore(flags
);
4060 EXPORT_SYMBOL_GPL(ring_buffer_empty
);
4063 * ring_buffer_empty_cpu - is a cpu buffer of a ring buffer empty?
4064 * @buffer: The ring buffer
4065 * @cpu: The CPU buffer to test
4067 int ring_buffer_empty_cpu(struct ring_buffer
*buffer
, int cpu
)
4069 struct ring_buffer_per_cpu
*cpu_buffer
;
4070 unsigned long flags
;
4074 if (!cpumask_test_cpu(cpu
, buffer
->cpumask
))
4077 dolock
= rb_ok_to_lock();
4079 cpu_buffer
= buffer
->buffers
[cpu
];
4080 local_irq_save(flags
);
4082 raw_spin_lock(&cpu_buffer
->reader_lock
);
4083 ret
= rb_per_cpu_empty(cpu_buffer
);
4085 raw_spin_unlock(&cpu_buffer
->reader_lock
);
4086 local_irq_restore(flags
);
4090 EXPORT_SYMBOL_GPL(ring_buffer_empty_cpu
);
4092 #ifdef CONFIG_RING_BUFFER_ALLOW_SWAP
4094 * ring_buffer_swap_cpu - swap a CPU buffer between two ring buffers
4095 * @buffer_a: One buffer to swap with
4096 * @buffer_b: The other buffer to swap with
4098 * This function is useful for tracers that want to take a "snapshot"
4099 * of a CPU buffer and has another back up buffer lying around.
4100 * it is expected that the tracer handles the cpu buffer not being
4101 * used at the moment.
4103 int ring_buffer_swap_cpu(struct ring_buffer
*buffer_a
,
4104 struct ring_buffer
*buffer_b
, int cpu
)
4106 struct ring_buffer_per_cpu
*cpu_buffer_a
;
4107 struct ring_buffer_per_cpu
*cpu_buffer_b
;
4110 if (!cpumask_test_cpu(cpu
, buffer_a
->cpumask
) ||
4111 !cpumask_test_cpu(cpu
, buffer_b
->cpumask
))
4114 cpu_buffer_a
= buffer_a
->buffers
[cpu
];
4115 cpu_buffer_b
= buffer_b
->buffers
[cpu
];
4117 /* At least make sure the two buffers are somewhat the same */
4118 if (cpu_buffer_a
->nr_pages
!= cpu_buffer_b
->nr_pages
)
4123 if (ring_buffer_flags
!= RB_BUFFERS_ON
)
4126 if (atomic_read(&buffer_a
->record_disabled
))
4129 if (atomic_read(&buffer_b
->record_disabled
))
4132 if (atomic_read(&cpu_buffer_a
->record_disabled
))
4135 if (atomic_read(&cpu_buffer_b
->record_disabled
))
4139 * We can't do a synchronize_sched here because this
4140 * function can be called in atomic context.
4141 * Normally this will be called from the same CPU as cpu.
4142 * If not it's up to the caller to protect this.
4144 atomic_inc(&cpu_buffer_a
->record_disabled
);
4145 atomic_inc(&cpu_buffer_b
->record_disabled
);
4148 if (local_read(&cpu_buffer_a
->committing
))
4150 if (local_read(&cpu_buffer_b
->committing
))
4153 buffer_a
->buffers
[cpu
] = cpu_buffer_b
;
4154 buffer_b
->buffers
[cpu
] = cpu_buffer_a
;
4156 cpu_buffer_b
->buffer
= buffer_a
;
4157 cpu_buffer_a
->buffer
= buffer_b
;
4162 atomic_dec(&cpu_buffer_a
->record_disabled
);
4163 atomic_dec(&cpu_buffer_b
->record_disabled
);
4167 EXPORT_SYMBOL_GPL(ring_buffer_swap_cpu
);
4168 #endif /* CONFIG_RING_BUFFER_ALLOW_SWAP */
4171 * ring_buffer_alloc_read_page - allocate a page to read from buffer
4172 * @buffer: the buffer to allocate for.
4174 * This function is used in conjunction with ring_buffer_read_page.
4175 * When reading a full page from the ring buffer, these functions
4176 * can be used to speed up the process. The calling function should
4177 * allocate a few pages first with this function. Then when it
4178 * needs to get pages from the ring buffer, it passes the result
4179 * of this function into ring_buffer_read_page, which will swap
4180 * the page that was allocated, with the read page of the buffer.
4183 * The page allocated, or NULL on error.
4185 void *ring_buffer_alloc_read_page(struct ring_buffer
*buffer
, int cpu
)
4187 struct buffer_data_page
*bpage
;
4190 page
= alloc_pages_node(cpu_to_node(cpu
),
4191 GFP_KERNEL
| __GFP_NORETRY
, 0);
4195 bpage
= page_address(page
);
4197 rb_init_page(bpage
);
4201 EXPORT_SYMBOL_GPL(ring_buffer_alloc_read_page
);
4204 * ring_buffer_free_read_page - free an allocated read page
4205 * @buffer: the buffer the page was allocate for
4206 * @data: the page to free
4208 * Free a page allocated from ring_buffer_alloc_read_page.
4210 void ring_buffer_free_read_page(struct ring_buffer
*buffer
, void *data
)
4212 free_page((unsigned long)data
);
4214 EXPORT_SYMBOL_GPL(ring_buffer_free_read_page
);
4217 * ring_buffer_read_page - extract a page from the ring buffer
4218 * @buffer: buffer to extract from
4219 * @data_page: the page to use allocated from ring_buffer_alloc_read_page
4220 * @len: amount to extract
4221 * @cpu: the cpu of the buffer to extract
4222 * @full: should the extraction only happen when the page is full.
4224 * This function will pull out a page from the ring buffer and consume it.
4225 * @data_page must be the address of the variable that was returned
4226 * from ring_buffer_alloc_read_page. This is because the page might be used
4227 * to swap with a page in the ring buffer.
4230 * rpage = ring_buffer_alloc_read_page(buffer);
4233 * ret = ring_buffer_read_page(buffer, &rpage, len, cpu, 0);
4235 * process_page(rpage, ret);
4237 * When @full is set, the function will not return true unless
4238 * the writer is off the reader page.
4240 * Note: it is up to the calling functions to handle sleeps and wakeups.
4241 * The ring buffer can be used anywhere in the kernel and can not
4242 * blindly call wake_up. The layer that uses the ring buffer must be
4243 * responsible for that.
4246 * >=0 if data has been transferred, returns the offset of consumed data.
4247 * <0 if no data has been transferred.
4249 int ring_buffer_read_page(struct ring_buffer
*buffer
,
4250 void **data_page
, size_t len
, int cpu
, int full
)
4252 struct ring_buffer_per_cpu
*cpu_buffer
= buffer
->buffers
[cpu
];
4253 struct ring_buffer_event
*event
;
4254 struct buffer_data_page
*bpage
;
4255 struct buffer_page
*reader
;
4256 unsigned long missed_events
;
4257 unsigned long flags
;
4258 unsigned int commit
;
4263 if (!cpumask_test_cpu(cpu
, buffer
->cpumask
))
4267 * If len is not big enough to hold the page header, then
4268 * we can not copy anything.
4270 if (len
<= BUF_PAGE_HDR_SIZE
)
4273 len
-= BUF_PAGE_HDR_SIZE
;
4282 raw_spin_lock_irqsave(&cpu_buffer
->reader_lock
, flags
);
4284 reader
= rb_get_reader_page(cpu_buffer
);
4288 event
= rb_reader_event(cpu_buffer
);
4290 read
= reader
->read
;
4291 commit
= rb_page_commit(reader
);
4293 /* Check if any events were dropped */
4294 missed_events
= cpu_buffer
->lost_events
;
4297 * If this page has been partially read or
4298 * if len is not big enough to read the rest of the page or
4299 * a writer is still on the page, then
4300 * we must copy the data from the page to the buffer.
4301 * Otherwise, we can simply swap the page with the one passed in.
4303 if (read
|| (len
< (commit
- read
)) ||
4304 cpu_buffer
->reader_page
== cpu_buffer
->commit_page
) {
4305 struct buffer_data_page
*rpage
= cpu_buffer
->reader_page
->page
;
4306 unsigned int rpos
= read
;
4307 unsigned int pos
= 0;
4313 if (len
> (commit
- read
))
4314 len
= (commit
- read
);
4316 /* Always keep the time extend and data together */
4317 size
= rb_event_ts_length(event
);
4322 /* save the current timestamp, since the user will need it */
4323 save_timestamp
= cpu_buffer
->read_stamp
;
4325 /* Need to copy one event at a time */
4327 /* We need the size of one event, because
4328 * rb_advance_reader only advances by one event,
4329 * whereas rb_event_ts_length may include the size of
4330 * one or two events.
4331 * We have already ensured there's enough space if this
4332 * is a time extend. */
4333 size
= rb_event_length(event
);
4334 memcpy(bpage
->data
+ pos
, rpage
->data
+ rpos
, size
);
4338 rb_advance_reader(cpu_buffer
);
4339 rpos
= reader
->read
;
4345 event
= rb_reader_event(cpu_buffer
);
4346 /* Always keep the time extend and data together */
4347 size
= rb_event_ts_length(event
);
4348 } while (len
>= size
);
4351 local_set(&bpage
->commit
, pos
);
4352 bpage
->time_stamp
= save_timestamp
;
4354 /* we copied everything to the beginning */
4357 /* update the entry counter */
4358 cpu_buffer
->read
+= rb_page_entries(reader
);
4359 cpu_buffer
->read_bytes
+= BUF_PAGE_SIZE
;
4361 /* swap the pages */
4362 rb_init_page(bpage
);
4363 bpage
= reader
->page
;
4364 reader
->page
= *data_page
;
4365 local_set(&reader
->write
, 0);
4366 local_set(&reader
->entries
, 0);
4371 * Use the real_end for the data size,
4372 * This gives us a chance to store the lost events
4375 if (reader
->real_end
)
4376 local_set(&bpage
->commit
, reader
->real_end
);
4380 cpu_buffer
->lost_events
= 0;
4382 commit
= local_read(&bpage
->commit
);
4384 * Set a flag in the commit field if we lost events
4386 if (missed_events
) {
4387 /* If there is room at the end of the page to save the
4388 * missed events, then record it there.
4390 if (BUF_PAGE_SIZE
- commit
>= sizeof(missed_events
)) {
4391 memcpy(&bpage
->data
[commit
], &missed_events
,
4392 sizeof(missed_events
));
4393 local_add(RB_MISSED_STORED
, &bpage
->commit
);
4394 commit
+= sizeof(missed_events
);
4396 local_add(RB_MISSED_EVENTS
, &bpage
->commit
);
4400 * This page may be off to user land. Zero it out here.
4402 if (commit
< BUF_PAGE_SIZE
)
4403 memset(&bpage
->data
[commit
], 0, BUF_PAGE_SIZE
- commit
);
4406 raw_spin_unlock_irqrestore(&cpu_buffer
->reader_lock
, flags
);
4411 EXPORT_SYMBOL_GPL(ring_buffer_read_page
);
4413 #ifdef CONFIG_HOTPLUG_CPU
4414 static int rb_cpu_notify(struct notifier_block
*self
,
4415 unsigned long action
, void *hcpu
)
4417 struct ring_buffer
*buffer
=
4418 container_of(self
, struct ring_buffer
, cpu_notify
);
4419 long cpu
= (long)hcpu
;
4420 int cpu_i
, nr_pages_same
;
4421 unsigned int nr_pages
;
4424 case CPU_UP_PREPARE
:
4425 case CPU_UP_PREPARE_FROZEN
:
4426 if (cpumask_test_cpu(cpu
, buffer
->cpumask
))
4431 /* check if all cpu sizes are same */
4432 for_each_buffer_cpu(buffer
, cpu_i
) {
4433 /* fill in the size from first enabled cpu */
4435 nr_pages
= buffer
->buffers
[cpu_i
]->nr_pages
;
4436 if (nr_pages
!= buffer
->buffers
[cpu_i
]->nr_pages
) {
4441 /* allocate minimum pages, user can later expand it */
4444 buffer
->buffers
[cpu
] =
4445 rb_allocate_cpu_buffer(buffer
, nr_pages
, cpu
);
4446 if (!buffer
->buffers
[cpu
]) {
4447 WARN(1, "failed to allocate ring buffer on CPU %ld\n",
4452 cpumask_set_cpu(cpu
, buffer
->cpumask
);
4454 case CPU_DOWN_PREPARE
:
4455 case CPU_DOWN_PREPARE_FROZEN
:
4458 * If we were to free the buffer, then the user would
4459 * lose any trace that was in the buffer.