1 // SPDX-License-Identifier: GPL-2.0
5 * Copyright (C) 2008 Steven Rostedt <srostedt@redhat.com>
7 #include <linux/trace_events.h>
8 #include <linux/ring_buffer.h>
9 #include <linux/trace_clock.h>
10 #include <linux/sched/clock.h>
11 #include <linux/trace_seq.h>
12 #include <linux/spinlock.h>
13 #include <linux/irq_work.h>
14 #include <linux/security.h>
15 #include <linux/uaccess.h>
16 #include <linux/hardirq.h>
17 #include <linux/kthread.h> /* for self test */
18 #include <linux/module.h>
19 #include <linux/percpu.h>
20 #include <linux/mutex.h>
21 #include <linux/delay.h>
22 #include <linux/slab.h>
23 #include <linux/init.h>
24 #include <linux/hash.h>
25 #include <linux/list.h>
26 #include <linux/cpu.h>
27 #include <linux/oom.h>
29 #include <asm/local.h>
31 static void update_pages_handler(struct work_struct
*work
);
34 * The ring buffer header is special. We must manually up keep it.
36 int ring_buffer_print_entry_header(struct trace_seq
*s
)
38 trace_seq_puts(s
, "# compressed entry header\n");
39 trace_seq_puts(s
, "\ttype_len : 5 bits\n");
40 trace_seq_puts(s
, "\ttime_delta : 27 bits\n");
41 trace_seq_puts(s
, "\tarray : 32 bits\n");
42 trace_seq_putc(s
, '\n');
43 trace_seq_printf(s
, "\tpadding : type == %d\n",
44 RINGBUF_TYPE_PADDING
);
45 trace_seq_printf(s
, "\ttime_extend : type == %d\n",
46 RINGBUF_TYPE_TIME_EXTEND
);
47 trace_seq_printf(s
, "\ttime_stamp : type == %d\n",
48 RINGBUF_TYPE_TIME_STAMP
);
49 trace_seq_printf(s
, "\tdata max type_len == %d\n",
50 RINGBUF_TYPE_DATA_TYPE_LEN_MAX
);
52 return !trace_seq_has_overflowed(s
);
56 * The ring buffer is made up of a list of pages. A separate list of pages is
57 * allocated for each CPU. A writer may only write to a buffer that is
58 * associated with the CPU it is currently executing on. A reader may read
59 * from any per cpu buffer.
61 * The reader is special. For each per cpu buffer, the reader has its own
62 * reader page. When a reader has read the entire reader page, this reader
63 * page is swapped with another page in the ring buffer.
65 * Now, as long as the writer is off the reader page, the reader can do what
66 * ever it wants with that page. The writer will never write to that page
67 * again (as long as it is out of the ring buffer).
69 * Here's some silly ASCII art.
72 * |reader| RING BUFFER
74 * +------+ +---+ +---+ +---+
83 * |reader| RING BUFFER
84 * |page |------------------v
85 * +------+ +---+ +---+ +---+
94 * |reader| RING BUFFER
95 * |page |------------------v
96 * +------+ +---+ +---+ +---+
101 * +------------------------------+
105 * |buffer| RING BUFFER
106 * |page |------------------v
107 * +------+ +---+ +---+ +---+
109 * | New +---+ +---+ +---+
112 * +------------------------------+
115 * After we make this swap, the reader can hand this page off to the splice
116 * code and be done with it. It can even allocate a new page if it needs to
117 * and swap that into the ring buffer.
119 * We will be using cmpxchg soon to make all this lockless.
123 /* Used for individual buffers (after the counter) */
124 #define RB_BUFFER_OFF (1 << 20)
126 #define BUF_PAGE_HDR_SIZE offsetof(struct buffer_data_page, data)
128 #define RB_EVNT_HDR_SIZE (offsetof(struct ring_buffer_event, array))
129 #define RB_ALIGNMENT 4U
130 #define RB_MAX_SMALL_DATA (RB_ALIGNMENT * RINGBUF_TYPE_DATA_TYPE_LEN_MAX)
131 #define RB_EVNT_MIN_SIZE 8U /* two 32bit words */
132 #define RB_ALIGN_DATA __aligned(RB_ALIGNMENT)
134 /* define RINGBUF_TYPE_DATA for 'case RINGBUF_TYPE_DATA:' */
135 #define RINGBUF_TYPE_DATA 0 ... RINGBUF_TYPE_DATA_TYPE_LEN_MAX
138 RB_LEN_TIME_EXTEND
= 8,
139 RB_LEN_TIME_STAMP
= 8,
142 #define skip_time_extend(event) \
143 ((struct ring_buffer_event *)((char *)event + RB_LEN_TIME_EXTEND))
145 #define extended_time(event) \
146 (event->type_len >= RINGBUF_TYPE_TIME_EXTEND)
148 static inline int rb_null_event(struct ring_buffer_event
*event
)
150 return event
->type_len
== RINGBUF_TYPE_PADDING
&& !event
->time_delta
;
153 static void rb_event_set_padding(struct ring_buffer_event
*event
)
155 /* padding has a NULL time_delta */
156 event
->type_len
= RINGBUF_TYPE_PADDING
;
157 event
->time_delta
= 0;
161 rb_event_data_length(struct ring_buffer_event
*event
)
166 length
= event
->type_len
* RB_ALIGNMENT
;
168 length
= event
->array
[0];
169 return length
+ RB_EVNT_HDR_SIZE
;
173 * Return the length of the given event. Will return
174 * the length of the time extend if the event is a
177 static inline unsigned
178 rb_event_length(struct ring_buffer_event
*event
)
180 switch (event
->type_len
) {
181 case RINGBUF_TYPE_PADDING
:
182 if (rb_null_event(event
))
185 return event
->array
[0] + RB_EVNT_HDR_SIZE
;
187 case RINGBUF_TYPE_TIME_EXTEND
:
188 return RB_LEN_TIME_EXTEND
;
190 case RINGBUF_TYPE_TIME_STAMP
:
191 return RB_LEN_TIME_STAMP
;
193 case RINGBUF_TYPE_DATA
:
194 return rb_event_data_length(event
);
203 * Return total length of time extend and data,
204 * or just the event length for all other events.
206 static inline unsigned
207 rb_event_ts_length(struct ring_buffer_event
*event
)
211 if (extended_time(event
)) {
212 /* time extends include the data event after it */
213 len
= RB_LEN_TIME_EXTEND
;
214 event
= skip_time_extend(event
);
216 return len
+ rb_event_length(event
);
220 * ring_buffer_event_length - return the length of the event
221 * @event: the event to get the length of
223 * Returns the size of the data load of a data event.
224 * If the event is something other than a data event, it
225 * returns the size of the event itself. With the exception
226 * of a TIME EXTEND, where it still returns the size of the
227 * data load of the data event after it.
229 unsigned ring_buffer_event_length(struct ring_buffer_event
*event
)
233 if (extended_time(event
))
234 event
= skip_time_extend(event
);
236 length
= rb_event_length(event
);
237 if (event
->type_len
> RINGBUF_TYPE_DATA_TYPE_LEN_MAX
)
239 length
-= RB_EVNT_HDR_SIZE
;
240 if (length
> RB_MAX_SMALL_DATA
+ sizeof(event
->array
[0]))
241 length
-= sizeof(event
->array
[0]);
244 EXPORT_SYMBOL_GPL(ring_buffer_event_length
);
246 /* inline for ring buffer fast paths */
247 static __always_inline
void *
248 rb_event_data(struct ring_buffer_event
*event
)
250 if (extended_time(event
))
251 event
= skip_time_extend(event
);
252 WARN_ON_ONCE(event
->type_len
> RINGBUF_TYPE_DATA_TYPE_LEN_MAX
);
253 /* If length is in len field, then array[0] has the data */
255 return (void *)&event
->array
[0];
256 /* Otherwise length is in array[0] and array[1] has the data */
257 return (void *)&event
->array
[1];
261 * ring_buffer_event_data - return the data of the event
262 * @event: the event to get the data from
264 void *ring_buffer_event_data(struct ring_buffer_event
*event
)
266 return rb_event_data(event
);
268 EXPORT_SYMBOL_GPL(ring_buffer_event_data
);
270 #define for_each_buffer_cpu(buffer, cpu) \
271 for_each_cpu(cpu, buffer->cpumask)
274 #define TS_MASK ((1ULL << TS_SHIFT) - 1)
275 #define TS_DELTA_TEST (~TS_MASK)
278 * ring_buffer_event_time_stamp - return the event's extended timestamp
279 * @event: the event to get the timestamp of
281 * Returns the extended timestamp associated with a data event.
282 * An extended time_stamp is a 64-bit timestamp represented
283 * internally in a special way that makes the best use of space
284 * contained within a ring buffer event. This function decodes
285 * it and maps it to a straight u64 value.
287 u64
ring_buffer_event_time_stamp(struct ring_buffer_event
*event
)
291 ts
= event
->array
[0];
293 ts
+= event
->time_delta
;
298 /* Flag when events were overwritten */
299 #define RB_MISSED_EVENTS (1 << 31)
300 /* Missed count stored at end */
301 #define RB_MISSED_STORED (1 << 30)
303 struct buffer_data_page
{
304 u64 time_stamp
; /* page time stamp */
305 local_t commit
; /* write committed index */
306 unsigned char data
[] RB_ALIGN_DATA
; /* data of buffer page */
310 * Note, the buffer_page list must be first. The buffer pages
311 * are allocated in cache lines, which means that each buffer
312 * page will be at the beginning of a cache line, and thus
313 * the least significant bits will be zero. We use this to
314 * add flags in the list struct pointers, to make the ring buffer
318 struct list_head list
; /* list of buffer pages */
319 local_t write
; /* index for next write */
320 unsigned read
; /* index for next read */
321 local_t entries
; /* entries on this page */
322 unsigned long real_end
; /* real end of data */
323 struct buffer_data_page
*page
; /* Actual data page */
327 * The buffer page counters, write and entries, must be reset
328 * atomically when crossing page boundaries. To synchronize this
329 * update, two counters are inserted into the number. One is
330 * the actual counter for the write position or count on the page.
332 * The other is a counter of updaters. Before an update happens
333 * the update partition of the counter is incremented. This will
334 * allow the updater to update the counter atomically.
336 * The counter is 20 bits, and the state data is 12.
338 #define RB_WRITE_MASK 0xfffff
339 #define RB_WRITE_INTCNT (1 << 20)
341 static void rb_init_page(struct buffer_data_page
*bpage
)
343 local_set(&bpage
->commit
, 0);
347 * Also stolen from mm/slob.c. Thanks to Mathieu Desnoyers for pointing
350 static void free_buffer_page(struct buffer_page
*bpage
)
352 free_page((unsigned long)bpage
->page
);
357 * We need to fit the time_stamp delta into 27 bits.
359 static inline int test_time_stamp(u64 delta
)
361 if (delta
& TS_DELTA_TEST
)
366 #define BUF_PAGE_SIZE (PAGE_SIZE - BUF_PAGE_HDR_SIZE)
368 /* Max payload is BUF_PAGE_SIZE - header (8bytes) */
369 #define BUF_MAX_DATA_SIZE (BUF_PAGE_SIZE - (sizeof(u32) * 2))
371 int ring_buffer_print_page_header(struct trace_seq
*s
)
373 struct buffer_data_page field
;
375 trace_seq_printf(s
, "\tfield: u64 timestamp;\t"
376 "offset:0;\tsize:%u;\tsigned:%u;\n",
377 (unsigned int)sizeof(field
.time_stamp
),
378 (unsigned int)is_signed_type(u64
));
380 trace_seq_printf(s
, "\tfield: local_t commit;\t"
381 "offset:%u;\tsize:%u;\tsigned:%u;\n",
382 (unsigned int)offsetof(typeof(field
), commit
),
383 (unsigned int)sizeof(field
.commit
),
384 (unsigned int)is_signed_type(long));
386 trace_seq_printf(s
, "\tfield: int overwrite;\t"
387 "offset:%u;\tsize:%u;\tsigned:%u;\n",
388 (unsigned int)offsetof(typeof(field
), commit
),
390 (unsigned int)is_signed_type(long));
392 trace_seq_printf(s
, "\tfield: char data;\t"
393 "offset:%u;\tsize:%u;\tsigned:%u;\n",
394 (unsigned int)offsetof(typeof(field
), data
),
395 (unsigned int)BUF_PAGE_SIZE
,
396 (unsigned int)is_signed_type(char));
398 return !trace_seq_has_overflowed(s
);
402 struct irq_work work
;
403 wait_queue_head_t waiters
;
404 wait_queue_head_t full_waiters
;
405 bool waiters_pending
;
406 bool full_waiters_pending
;
411 * Structure to hold event state and handle nested events.
413 struct rb_event_info
{
416 unsigned long length
;
417 struct buffer_page
*tail_page
;
422 * Used for which event context the event is in.
428 * See trace_recursive_lock() comment below for more details.
439 * head_page == tail_page && head == tail then buffer is empty.
441 struct ring_buffer_per_cpu
{
443 atomic_t record_disabled
;
444 atomic_t resize_disabled
;
445 struct trace_buffer
*buffer
;
446 raw_spinlock_t reader_lock
; /* serialize readers */
447 arch_spinlock_t lock
;
448 struct lock_class_key lock_key
;
449 struct buffer_data_page
*free_page
;
450 unsigned long nr_pages
;
451 unsigned int current_context
;
452 struct list_head
*pages
;
453 struct buffer_page
*head_page
; /* read from head */
454 struct buffer_page
*tail_page
; /* write to tail */
455 struct buffer_page
*commit_page
; /* committed pages */
456 struct buffer_page
*reader_page
;
457 unsigned long lost_events
;
458 unsigned long last_overrun
;
460 local_t entries_bytes
;
463 local_t commit_overrun
;
464 local_t dropped_events
;
467 local_t pages_touched
;
469 long last_pages_touch
;
470 size_t shortest_full
;
472 unsigned long read_bytes
;
475 /* ring buffer pages to update, > 0 to add, < 0 to remove */
476 long nr_pages_to_update
;
477 struct list_head new_pages
; /* new pages to add */
478 struct work_struct update_pages_work
;
479 struct completion update_done
;
481 struct rb_irq_work irq_work
;
484 struct trace_buffer
{
487 atomic_t record_disabled
;
488 cpumask_var_t cpumask
;
490 struct lock_class_key
*reader_lock_key
;
494 struct ring_buffer_per_cpu
**buffers
;
496 struct hlist_node node
;
499 struct rb_irq_work irq_work
;
503 struct ring_buffer_iter
{
504 struct ring_buffer_per_cpu
*cpu_buffer
;
506 unsigned long next_event
;
507 struct buffer_page
*head_page
;
508 struct buffer_page
*cache_reader_page
;
509 unsigned long cache_read
;
512 struct ring_buffer_event
*event
;
517 * ring_buffer_nr_pages - get the number of buffer pages in the ring buffer
518 * @buffer: The ring_buffer to get the number of pages from
519 * @cpu: The cpu of the ring_buffer to get the number of pages from
521 * Returns the number of pages used by a per_cpu buffer of the ring buffer.
523 size_t ring_buffer_nr_pages(struct trace_buffer
*buffer
, int cpu
)
525 return buffer
->buffers
[cpu
]->nr_pages
;
529 * ring_buffer_nr_pages_dirty - get the number of used pages in the ring buffer
530 * @buffer: The ring_buffer to get the number of pages from
531 * @cpu: The cpu of the ring_buffer to get the number of pages from
533 * Returns the number of pages that have content in the ring buffer.
535 size_t ring_buffer_nr_dirty_pages(struct trace_buffer
*buffer
, int cpu
)
540 read
= local_read(&buffer
->buffers
[cpu
]->pages_read
);
541 cnt
= local_read(&buffer
->buffers
[cpu
]->pages_touched
);
542 /* The reader can read an empty page, but not more than that */
544 WARN_ON_ONCE(read
> cnt
+ 1);
552 * rb_wake_up_waiters - wake up tasks waiting for ring buffer input
554 * Schedules a delayed work to wake up any task that is blocked on the
555 * ring buffer waiters queue.
557 static void rb_wake_up_waiters(struct irq_work
*work
)
559 struct rb_irq_work
*rbwork
= container_of(work
, struct rb_irq_work
, work
);
561 wake_up_all(&rbwork
->waiters
);
562 if (rbwork
->wakeup_full
) {
563 rbwork
->wakeup_full
= false;
564 wake_up_all(&rbwork
->full_waiters
);
569 * ring_buffer_wait - wait for input to the ring buffer
570 * @buffer: buffer to wait on
571 * @cpu: the cpu buffer to wait on
572 * @full: wait until a full page is available, if @cpu != RING_BUFFER_ALL_CPUS
574 * If @cpu == RING_BUFFER_ALL_CPUS then the task will wake up as soon
575 * as data is added to any of the @buffer's cpu buffers. Otherwise
576 * it will wait for data to be added to a specific cpu buffer.
578 int ring_buffer_wait(struct trace_buffer
*buffer
, int cpu
, int full
)
580 struct ring_buffer_per_cpu
*uninitialized_var(cpu_buffer
);
582 struct rb_irq_work
*work
;
586 * Depending on what the caller is waiting for, either any
587 * data in any cpu buffer, or a specific buffer, put the
588 * caller on the appropriate wait queue.
590 if (cpu
== RING_BUFFER_ALL_CPUS
) {
591 work
= &buffer
->irq_work
;
592 /* Full only makes sense on per cpu reads */
595 if (!cpumask_test_cpu(cpu
, buffer
->cpumask
))
597 cpu_buffer
= buffer
->buffers
[cpu
];
598 work
= &cpu_buffer
->irq_work
;
604 prepare_to_wait(&work
->full_waiters
, &wait
, TASK_INTERRUPTIBLE
);
606 prepare_to_wait(&work
->waiters
, &wait
, TASK_INTERRUPTIBLE
);
609 * The events can happen in critical sections where
610 * checking a work queue can cause deadlocks.
611 * After adding a task to the queue, this flag is set
612 * only to notify events to try to wake up the queue
615 * We don't clear it even if the buffer is no longer
616 * empty. The flag only causes the next event to run
617 * irq_work to do the work queue wake up. The worse
618 * that can happen if we race with !trace_empty() is that
619 * an event will cause an irq_work to try to wake up
622 * There's no reason to protect this flag either, as
623 * the work queue and irq_work logic will do the necessary
624 * synchronization for the wake ups. The only thing
625 * that is necessary is that the wake up happens after
626 * a task has been queued. It's OK for spurious wake ups.
629 work
->full_waiters_pending
= true;
631 work
->waiters_pending
= true;
633 if (signal_pending(current
)) {
638 if (cpu
== RING_BUFFER_ALL_CPUS
&& !ring_buffer_empty(buffer
))
641 if (cpu
!= RING_BUFFER_ALL_CPUS
&&
642 !ring_buffer_empty_cpu(buffer
, cpu
)) {
651 raw_spin_lock_irqsave(&cpu_buffer
->reader_lock
, flags
);
652 pagebusy
= cpu_buffer
->reader_page
== cpu_buffer
->commit_page
;
653 nr_pages
= cpu_buffer
->nr_pages
;
654 dirty
= ring_buffer_nr_dirty_pages(buffer
, cpu
);
655 if (!cpu_buffer
->shortest_full
||
656 cpu_buffer
->shortest_full
< full
)
657 cpu_buffer
->shortest_full
= full
;
658 raw_spin_unlock_irqrestore(&cpu_buffer
->reader_lock
, flags
);
660 (!nr_pages
|| (dirty
* 100) > full
* nr_pages
))
668 finish_wait(&work
->full_waiters
, &wait
);
670 finish_wait(&work
->waiters
, &wait
);
676 * ring_buffer_poll_wait - poll on buffer input
677 * @buffer: buffer to wait on
678 * @cpu: the cpu buffer to wait on
679 * @filp: the file descriptor
680 * @poll_table: The poll descriptor
682 * If @cpu == RING_BUFFER_ALL_CPUS then the task will wake up as soon
683 * as data is added to any of the @buffer's cpu buffers. Otherwise
684 * it will wait for data to be added to a specific cpu buffer.
686 * Returns EPOLLIN | EPOLLRDNORM if data exists in the buffers,
689 __poll_t
ring_buffer_poll_wait(struct trace_buffer
*buffer
, int cpu
,
690 struct file
*filp
, poll_table
*poll_table
)
692 struct ring_buffer_per_cpu
*cpu_buffer
;
693 struct rb_irq_work
*work
;
695 if (cpu
== RING_BUFFER_ALL_CPUS
)
696 work
= &buffer
->irq_work
;
698 if (!cpumask_test_cpu(cpu
, buffer
->cpumask
))
701 cpu_buffer
= buffer
->buffers
[cpu
];
702 work
= &cpu_buffer
->irq_work
;
705 poll_wait(filp
, &work
->waiters
, poll_table
);
706 work
->waiters_pending
= true;
708 * There's a tight race between setting the waiters_pending and
709 * checking if the ring buffer is empty. Once the waiters_pending bit
710 * is set, the next event will wake the task up, but we can get stuck
711 * if there's only a single event in.
713 * FIXME: Ideally, we need a memory barrier on the writer side as well,
714 * but adding a memory barrier to all events will cause too much of a
715 * performance hit in the fast path. We only need a memory barrier when
716 * the buffer goes from empty to having content. But as this race is
717 * extremely small, and it's not a problem if another event comes in, we
722 if ((cpu
== RING_BUFFER_ALL_CPUS
&& !ring_buffer_empty(buffer
)) ||
723 (cpu
!= RING_BUFFER_ALL_CPUS
&& !ring_buffer_empty_cpu(buffer
, cpu
)))
724 return EPOLLIN
| EPOLLRDNORM
;
728 /* buffer may be either ring_buffer or ring_buffer_per_cpu */
729 #define RB_WARN_ON(b, cond) \
731 int _____ret = unlikely(cond); \
733 if (__same_type(*(b), struct ring_buffer_per_cpu)) { \
734 struct ring_buffer_per_cpu *__b = \
736 atomic_inc(&__b->buffer->record_disabled); \
738 atomic_inc(&b->record_disabled); \
744 /* Up this if you want to test the TIME_EXTENTS and normalization */
745 #define DEBUG_SHIFT 0
747 static inline u64
rb_time_stamp(struct trace_buffer
*buffer
)
749 /* shift to debug/test normalization and TIME_EXTENTS */
750 return buffer
->clock() << DEBUG_SHIFT
;
753 u64
ring_buffer_time_stamp(struct trace_buffer
*buffer
, int cpu
)
757 preempt_disable_notrace();
758 time
= rb_time_stamp(buffer
);
759 preempt_enable_notrace();
763 EXPORT_SYMBOL_GPL(ring_buffer_time_stamp
);
765 void ring_buffer_normalize_time_stamp(struct trace_buffer
*buffer
,
768 /* Just stupid testing the normalize function and deltas */
771 EXPORT_SYMBOL_GPL(ring_buffer_normalize_time_stamp
);
774 * Making the ring buffer lockless makes things tricky.
775 * Although writes only happen on the CPU that they are on,
776 * and they only need to worry about interrupts. Reads can
779 * The reader page is always off the ring buffer, but when the
780 * reader finishes with a page, it needs to swap its page with
781 * a new one from the buffer. The reader needs to take from
782 * the head (writes go to the tail). But if a writer is in overwrite
783 * mode and wraps, it must push the head page forward.
785 * Here lies the problem.
787 * The reader must be careful to replace only the head page, and
788 * not another one. As described at the top of the file in the
789 * ASCII art, the reader sets its old page to point to the next
790 * page after head. It then sets the page after head to point to
791 * the old reader page. But if the writer moves the head page
792 * during this operation, the reader could end up with the tail.
794 * We use cmpxchg to help prevent this race. We also do something
795 * special with the page before head. We set the LSB to 1.
797 * When the writer must push the page forward, it will clear the
798 * bit that points to the head page, move the head, and then set
799 * the bit that points to the new head page.
801 * We also don't want an interrupt coming in and moving the head
802 * page on another writer. Thus we use the second LSB to catch
805 * head->list->prev->next bit 1 bit 0
808 * Points to head page 0 1
811 * Note we can not trust the prev pointer of the head page, because:
813 * +----+ +-----+ +-----+
814 * | |------>| T |---X--->| N |
816 * +----+ +-----+ +-----+
819 * +----------| R |----------+ |
823 * Key: ---X--> HEAD flag set in pointer
828 * (see __rb_reserve_next() to see where this happens)
830 * What the above shows is that the reader just swapped out
831 * the reader page with a page in the buffer, but before it
832 * could make the new header point back to the new page added
833 * it was preempted by a writer. The writer moved forward onto
834 * the new page added by the reader and is about to move forward
837 * You can see, it is legitimate for the previous pointer of
838 * the head (or any page) not to point back to itself. But only
842 #define RB_PAGE_NORMAL 0UL
843 #define RB_PAGE_HEAD 1UL
844 #define RB_PAGE_UPDATE 2UL
847 #define RB_FLAG_MASK 3UL
849 /* PAGE_MOVED is not part of the mask */
850 #define RB_PAGE_MOVED 4UL
853 * rb_list_head - remove any bit
855 static struct list_head
*rb_list_head(struct list_head
*list
)
857 unsigned long val
= (unsigned long)list
;
859 return (struct list_head
*)(val
& ~RB_FLAG_MASK
);
863 * rb_is_head_page - test if the given page is the head page
865 * Because the reader may move the head_page pointer, we can
866 * not trust what the head page is (it may be pointing to
867 * the reader page). But if the next page is a header page,
868 * its flags will be non zero.
871 rb_is_head_page(struct ring_buffer_per_cpu
*cpu_buffer
,
872 struct buffer_page
*page
, struct list_head
*list
)
876 val
= (unsigned long)list
->next
;
878 if ((val
& ~RB_FLAG_MASK
) != (unsigned long)&page
->list
)
879 return RB_PAGE_MOVED
;
881 return val
& RB_FLAG_MASK
;
887 * The unique thing about the reader page, is that, if the
888 * writer is ever on it, the previous pointer never points
889 * back to the reader page.
891 static bool rb_is_reader_page(struct buffer_page
*page
)
893 struct list_head
*list
= page
->list
.prev
;
895 return rb_list_head(list
->next
) != &page
->list
;
899 * rb_set_list_to_head - set a list_head to be pointing to head.
901 static void rb_set_list_to_head(struct ring_buffer_per_cpu
*cpu_buffer
,
902 struct list_head
*list
)
906 ptr
= (unsigned long *)&list
->next
;
907 *ptr
|= RB_PAGE_HEAD
;
908 *ptr
&= ~RB_PAGE_UPDATE
;
912 * rb_head_page_activate - sets up head page
914 static void rb_head_page_activate(struct ring_buffer_per_cpu
*cpu_buffer
)
916 struct buffer_page
*head
;
918 head
= cpu_buffer
->head_page
;
923 * Set the previous list pointer to have the HEAD flag.
925 rb_set_list_to_head(cpu_buffer
, head
->list
.prev
);
928 static void rb_list_head_clear(struct list_head
*list
)
930 unsigned long *ptr
= (unsigned long *)&list
->next
;
932 *ptr
&= ~RB_FLAG_MASK
;
936 * rb_head_page_deactivate - clears head page ptr (for free list)
939 rb_head_page_deactivate(struct ring_buffer_per_cpu
*cpu_buffer
)
941 struct list_head
*hd
;
943 /* Go through the whole list and clear any pointers found. */
944 rb_list_head_clear(cpu_buffer
->pages
);
946 list_for_each(hd
, cpu_buffer
->pages
)
947 rb_list_head_clear(hd
);
950 static int rb_head_page_set(struct ring_buffer_per_cpu
*cpu_buffer
,
951 struct buffer_page
*head
,
952 struct buffer_page
*prev
,
953 int old_flag
, int new_flag
)
955 struct list_head
*list
;
956 unsigned long val
= (unsigned long)&head
->list
;
961 val
&= ~RB_FLAG_MASK
;
963 ret
= cmpxchg((unsigned long *)&list
->next
,
964 val
| old_flag
, val
| new_flag
);
966 /* check if the reader took the page */
967 if ((ret
& ~RB_FLAG_MASK
) != val
)
968 return RB_PAGE_MOVED
;
970 return ret
& RB_FLAG_MASK
;
973 static int rb_head_page_set_update(struct ring_buffer_per_cpu
*cpu_buffer
,
974 struct buffer_page
*head
,
975 struct buffer_page
*prev
,
978 return rb_head_page_set(cpu_buffer
, head
, prev
,
979 old_flag
, RB_PAGE_UPDATE
);
982 static int rb_head_page_set_head(struct ring_buffer_per_cpu
*cpu_buffer
,
983 struct buffer_page
*head
,
984 struct buffer_page
*prev
,
987 return rb_head_page_set(cpu_buffer
, head
, prev
,
988 old_flag
, RB_PAGE_HEAD
);
991 static int rb_head_page_set_normal(struct ring_buffer_per_cpu
*cpu_buffer
,
992 struct buffer_page
*head
,
993 struct buffer_page
*prev
,
996 return rb_head_page_set(cpu_buffer
, head
, prev
,
997 old_flag
, RB_PAGE_NORMAL
);
1000 static inline void rb_inc_page(struct ring_buffer_per_cpu
*cpu_buffer
,
1001 struct buffer_page
**bpage
)
1003 struct list_head
*p
= rb_list_head((*bpage
)->list
.next
);
1005 *bpage
= list_entry(p
, struct buffer_page
, list
);
1008 static struct buffer_page
*
1009 rb_set_head_page(struct ring_buffer_per_cpu
*cpu_buffer
)
1011 struct buffer_page
*head
;
1012 struct buffer_page
*page
;
1013 struct list_head
*list
;
1016 if (RB_WARN_ON(cpu_buffer
, !cpu_buffer
->head_page
))
1020 list
= cpu_buffer
->pages
;
1021 if (RB_WARN_ON(cpu_buffer
, rb_list_head(list
->prev
->next
) != list
))
1024 page
= head
= cpu_buffer
->head_page
;
1026 * It is possible that the writer moves the header behind
1027 * where we started, and we miss in one loop.
1028 * A second loop should grab the header, but we'll do
1029 * three loops just because I'm paranoid.
1031 for (i
= 0; i
< 3; i
++) {
1033 if (rb_is_head_page(cpu_buffer
, page
, page
->list
.prev
)) {
1034 cpu_buffer
->head_page
= page
;
1037 rb_inc_page(cpu_buffer
, &page
);
1038 } while (page
!= head
);
1041 RB_WARN_ON(cpu_buffer
, 1);
1046 static int rb_head_page_replace(struct buffer_page
*old
,
1047 struct buffer_page
*new)
1049 unsigned long *ptr
= (unsigned long *)&old
->list
.prev
->next
;
1053 val
= *ptr
& ~RB_FLAG_MASK
;
1054 val
|= RB_PAGE_HEAD
;
1056 ret
= cmpxchg(ptr
, val
, (unsigned long)&new->list
);
1062 * rb_tail_page_update - move the tail page forward
1064 static void rb_tail_page_update(struct ring_buffer_per_cpu
*cpu_buffer
,
1065 struct buffer_page
*tail_page
,
1066 struct buffer_page
*next_page
)
1068 unsigned long old_entries
;
1069 unsigned long old_write
;
1072 * The tail page now needs to be moved forward.
1074 * We need to reset the tail page, but without messing
1075 * with possible erasing of data brought in by interrupts
1076 * that have moved the tail page and are currently on it.
1078 * We add a counter to the write field to denote this.
1080 old_write
= local_add_return(RB_WRITE_INTCNT
, &next_page
->write
);
1081 old_entries
= local_add_return(RB_WRITE_INTCNT
, &next_page
->entries
);
1083 local_inc(&cpu_buffer
->pages_touched
);
1085 * Just make sure we have seen our old_write and synchronize
1086 * with any interrupts that come in.
1091 * If the tail page is still the same as what we think
1092 * it is, then it is up to us to update the tail
1095 if (tail_page
== READ_ONCE(cpu_buffer
->tail_page
)) {
1096 /* Zero the write counter */
1097 unsigned long val
= old_write
& ~RB_WRITE_MASK
;
1098 unsigned long eval
= old_entries
& ~RB_WRITE_MASK
;
1101 * This will only succeed if an interrupt did
1102 * not come in and change it. In which case, we
1103 * do not want to modify it.
1105 * We add (void) to let the compiler know that we do not care
1106 * about the return value of these functions. We use the
1107 * cmpxchg to only update if an interrupt did not already
1108 * do it for us. If the cmpxchg fails, we don't care.
1110 (void)local_cmpxchg(&next_page
->write
, old_write
, val
);
1111 (void)local_cmpxchg(&next_page
->entries
, old_entries
, eval
);
1114 * No need to worry about races with clearing out the commit.
1115 * it only can increment when a commit takes place. But that
1116 * only happens in the outer most nested commit.
1118 local_set(&next_page
->page
->commit
, 0);
1120 /* Again, either we update tail_page or an interrupt does */
1121 (void)cmpxchg(&cpu_buffer
->tail_page
, tail_page
, next_page
);
1125 static int rb_check_bpage(struct ring_buffer_per_cpu
*cpu_buffer
,
1126 struct buffer_page
*bpage
)
1128 unsigned long val
= (unsigned long)bpage
;
1130 if (RB_WARN_ON(cpu_buffer
, val
& RB_FLAG_MASK
))
1137 * rb_check_list - make sure a pointer to a list has the last bits zero
1139 static int rb_check_list(struct ring_buffer_per_cpu
*cpu_buffer
,
1140 struct list_head
*list
)
1142 if (RB_WARN_ON(cpu_buffer
, rb_list_head(list
->prev
) != list
->prev
))
1144 if (RB_WARN_ON(cpu_buffer
, rb_list_head(list
->next
) != list
->next
))
1150 * rb_check_pages - integrity check of buffer pages
1151 * @cpu_buffer: CPU buffer with pages to test
1153 * As a safety measure we check to make sure the data pages have not
1156 static int rb_check_pages(struct ring_buffer_per_cpu
*cpu_buffer
)
1158 struct list_head
*head
= cpu_buffer
->pages
;
1159 struct buffer_page
*bpage
, *tmp
;
1161 /* Reset the head page if it exists */
1162 if (cpu_buffer
->head_page
)
1163 rb_set_head_page(cpu_buffer
);
1165 rb_head_page_deactivate(cpu_buffer
);
1167 if (RB_WARN_ON(cpu_buffer
, head
->next
->prev
!= head
))
1169 if (RB_WARN_ON(cpu_buffer
, head
->prev
->next
!= head
))
1172 if (rb_check_list(cpu_buffer
, head
))
1175 list_for_each_entry_safe(bpage
, tmp
, head
, list
) {
1176 if (RB_WARN_ON(cpu_buffer
,
1177 bpage
->list
.next
->prev
!= &bpage
->list
))
1179 if (RB_WARN_ON(cpu_buffer
,
1180 bpage
->list
.prev
->next
!= &bpage
->list
))
1182 if (rb_check_list(cpu_buffer
, &bpage
->list
))
1186 rb_head_page_activate(cpu_buffer
);
1191 static int __rb_allocate_pages(long nr_pages
, struct list_head
*pages
, int cpu
)
1193 struct buffer_page
*bpage
, *tmp
;
1194 bool user_thread
= current
->mm
!= NULL
;
1199 * Check if the available memory is there first.
1200 * Note, si_mem_available() only gives us a rough estimate of available
1201 * memory. It may not be accurate. But we don't care, we just want
1202 * to prevent doing any allocation when it is obvious that it is
1203 * not going to succeed.
1205 i
= si_mem_available();
1210 * __GFP_RETRY_MAYFAIL flag makes sure that the allocation fails
1211 * gracefully without invoking oom-killer and the system is not
1214 mflags
= GFP_KERNEL
| __GFP_RETRY_MAYFAIL
;
1217 * If a user thread allocates too much, and si_mem_available()
1218 * reports there's enough memory, even though there is not.
1219 * Make sure the OOM killer kills this thread. This can happen
1220 * even with RETRY_MAYFAIL because another task may be doing
1221 * an allocation after this task has taken all memory.
1222 * This is the task the OOM killer needs to take out during this
1223 * loop, even if it was triggered by an allocation somewhere else.
1226 set_current_oom_origin();
1227 for (i
= 0; i
< nr_pages
; i
++) {
1230 bpage
= kzalloc_node(ALIGN(sizeof(*bpage
), cache_line_size()),
1231 mflags
, cpu_to_node(cpu
));
1235 list_add(&bpage
->list
, pages
);
1237 page
= alloc_pages_node(cpu_to_node(cpu
), mflags
, 0);
1240 bpage
->page
= page_address(page
);
1241 rb_init_page(bpage
->page
);
1243 if (user_thread
&& fatal_signal_pending(current
))
1247 clear_current_oom_origin();
1252 list_for_each_entry_safe(bpage
, tmp
, pages
, list
) {
1253 list_del_init(&bpage
->list
);
1254 free_buffer_page(bpage
);
1257 clear_current_oom_origin();
1262 static int rb_allocate_pages(struct ring_buffer_per_cpu
*cpu_buffer
,
1263 unsigned long nr_pages
)
1269 if (__rb_allocate_pages(nr_pages
, &pages
, cpu_buffer
->cpu
))
1273 * The ring buffer page list is a circular list that does not
1274 * start and end with a list head. All page list items point to
1277 cpu_buffer
->pages
= pages
.next
;
1280 cpu_buffer
->nr_pages
= nr_pages
;
1282 rb_check_pages(cpu_buffer
);
1287 static struct ring_buffer_per_cpu
*
1288 rb_allocate_cpu_buffer(struct trace_buffer
*buffer
, long nr_pages
, int cpu
)
1290 struct ring_buffer_per_cpu
*cpu_buffer
;
1291 struct buffer_page
*bpage
;
1295 cpu_buffer
= kzalloc_node(ALIGN(sizeof(*cpu_buffer
), cache_line_size()),
1296 GFP_KERNEL
, cpu_to_node(cpu
));
1300 cpu_buffer
->cpu
= cpu
;
1301 cpu_buffer
->buffer
= buffer
;
1302 raw_spin_lock_init(&cpu_buffer
->reader_lock
);
1303 lockdep_set_class(&cpu_buffer
->reader_lock
, buffer
->reader_lock_key
);
1304 cpu_buffer
->lock
= (arch_spinlock_t
)__ARCH_SPIN_LOCK_UNLOCKED
;
1305 INIT_WORK(&cpu_buffer
->update_pages_work
, update_pages_handler
);
1306 init_completion(&cpu_buffer
->update_done
);
1307 init_irq_work(&cpu_buffer
->irq_work
.work
, rb_wake_up_waiters
);
1308 init_waitqueue_head(&cpu_buffer
->irq_work
.waiters
);
1309 init_waitqueue_head(&cpu_buffer
->irq_work
.full_waiters
);
1311 bpage
= kzalloc_node(ALIGN(sizeof(*bpage
), cache_line_size()),
1312 GFP_KERNEL
, cpu_to_node(cpu
));
1314 goto fail_free_buffer
;
1316 rb_check_bpage(cpu_buffer
, bpage
);
1318 cpu_buffer
->reader_page
= bpage
;
1319 page
= alloc_pages_node(cpu_to_node(cpu
), GFP_KERNEL
, 0);
1321 goto fail_free_reader
;
1322 bpage
->page
= page_address(page
);
1323 rb_init_page(bpage
->page
);
1325 INIT_LIST_HEAD(&cpu_buffer
->reader_page
->list
);
1326 INIT_LIST_HEAD(&cpu_buffer
->new_pages
);
1328 ret
= rb_allocate_pages(cpu_buffer
, nr_pages
);
1330 goto fail_free_reader
;
1332 cpu_buffer
->head_page
1333 = list_entry(cpu_buffer
->pages
, struct buffer_page
, list
);
1334 cpu_buffer
->tail_page
= cpu_buffer
->commit_page
= cpu_buffer
->head_page
;
1336 rb_head_page_activate(cpu_buffer
);
1341 free_buffer_page(cpu_buffer
->reader_page
);
1348 static void rb_free_cpu_buffer(struct ring_buffer_per_cpu
*cpu_buffer
)
1350 struct list_head
*head
= cpu_buffer
->pages
;
1351 struct buffer_page
*bpage
, *tmp
;
1353 free_buffer_page(cpu_buffer
->reader_page
);
1355 rb_head_page_deactivate(cpu_buffer
);
1358 list_for_each_entry_safe(bpage
, tmp
, head
, list
) {
1359 list_del_init(&bpage
->list
);
1360 free_buffer_page(bpage
);
1362 bpage
= list_entry(head
, struct buffer_page
, list
);
1363 free_buffer_page(bpage
);
1370 * __ring_buffer_alloc - allocate a new ring_buffer
1371 * @size: the size in bytes per cpu that is needed.
1372 * @flags: attributes to set for the ring buffer.
1373 * @key: ring buffer reader_lock_key.
1375 * Currently the only flag that is available is the RB_FL_OVERWRITE
1376 * flag. This flag means that the buffer will overwrite old data
1377 * when the buffer wraps. If this flag is not set, the buffer will
1378 * drop data when the tail hits the head.
1380 struct trace_buffer
*__ring_buffer_alloc(unsigned long size
, unsigned flags
,
1381 struct lock_class_key
*key
)
1383 struct trace_buffer
*buffer
;
1389 /* keep it in its own cache line */
1390 buffer
= kzalloc(ALIGN(sizeof(*buffer
), cache_line_size()),
1395 if (!zalloc_cpumask_var(&buffer
->cpumask
, GFP_KERNEL
))
1396 goto fail_free_buffer
;
1398 nr_pages
= DIV_ROUND_UP(size
, BUF_PAGE_SIZE
);
1399 buffer
->flags
= flags
;
1400 buffer
->clock
= trace_clock_local
;
1401 buffer
->reader_lock_key
= key
;
1403 init_irq_work(&buffer
->irq_work
.work
, rb_wake_up_waiters
);
1404 init_waitqueue_head(&buffer
->irq_work
.waiters
);
1406 /* need at least two pages */
1410 buffer
->cpus
= nr_cpu_ids
;
1412 bsize
= sizeof(void *) * nr_cpu_ids
;
1413 buffer
->buffers
= kzalloc(ALIGN(bsize
, cache_line_size()),
1415 if (!buffer
->buffers
)
1416 goto fail_free_cpumask
;
1418 cpu
= raw_smp_processor_id();
1419 cpumask_set_cpu(cpu
, buffer
->cpumask
);
1420 buffer
->buffers
[cpu
] = rb_allocate_cpu_buffer(buffer
, nr_pages
, cpu
);
1421 if (!buffer
->buffers
[cpu
])
1422 goto fail_free_buffers
;
1424 ret
= cpuhp_state_add_instance(CPUHP_TRACE_RB_PREPARE
, &buffer
->node
);
1426 goto fail_free_buffers
;
1428 mutex_init(&buffer
->mutex
);
1433 for_each_buffer_cpu(buffer
, cpu
) {
1434 if (buffer
->buffers
[cpu
])
1435 rb_free_cpu_buffer(buffer
->buffers
[cpu
]);
1437 kfree(buffer
->buffers
);
1440 free_cpumask_var(buffer
->cpumask
);
1446 EXPORT_SYMBOL_GPL(__ring_buffer_alloc
);
1449 * ring_buffer_free - free a ring buffer.
1450 * @buffer: the buffer to free.
1453 ring_buffer_free(struct trace_buffer
*buffer
)
1457 cpuhp_state_remove_instance(CPUHP_TRACE_RB_PREPARE
, &buffer
->node
);
1459 for_each_buffer_cpu(buffer
, cpu
)
1460 rb_free_cpu_buffer(buffer
->buffers
[cpu
]);
1462 kfree(buffer
->buffers
);
1463 free_cpumask_var(buffer
->cpumask
);
1467 EXPORT_SYMBOL_GPL(ring_buffer_free
);
1469 void ring_buffer_set_clock(struct trace_buffer
*buffer
,
1472 buffer
->clock
= clock
;
1475 void ring_buffer_set_time_stamp_abs(struct trace_buffer
*buffer
, bool abs
)
1477 buffer
->time_stamp_abs
= abs
;
1480 bool ring_buffer_time_stamp_abs(struct trace_buffer
*buffer
)
1482 return buffer
->time_stamp_abs
;
1485 static void rb_reset_cpu(struct ring_buffer_per_cpu
*cpu_buffer
);
1487 static inline unsigned long rb_page_entries(struct buffer_page
*bpage
)
1489 return local_read(&bpage
->entries
) & RB_WRITE_MASK
;
1492 static inline unsigned long rb_page_write(struct buffer_page
*bpage
)
1494 return local_read(&bpage
->write
) & RB_WRITE_MASK
;
1498 rb_remove_pages(struct ring_buffer_per_cpu
*cpu_buffer
, unsigned long nr_pages
)
1500 struct list_head
*tail_page
, *to_remove
, *next_page
;
1501 struct buffer_page
*to_remove_page
, *tmp_iter_page
;
1502 struct buffer_page
*last_page
, *first_page
;
1503 unsigned long nr_removed
;
1504 unsigned long head_bit
;
1509 raw_spin_lock_irq(&cpu_buffer
->reader_lock
);
1510 atomic_inc(&cpu_buffer
->record_disabled
);
1512 * We don't race with the readers since we have acquired the reader
1513 * lock. We also don't race with writers after disabling recording.
1514 * This makes it easy to figure out the first and the last page to be
1515 * removed from the list. We unlink all the pages in between including
1516 * the first and last pages. This is done in a busy loop so that we
1517 * lose the least number of traces.
1518 * The pages are freed after we restart recording and unlock readers.
1520 tail_page
= &cpu_buffer
->tail_page
->list
;
1523 * tail page might be on reader page, we remove the next page
1524 * from the ring buffer
1526 if (cpu_buffer
->tail_page
== cpu_buffer
->reader_page
)
1527 tail_page
= rb_list_head(tail_page
->next
);
1528 to_remove
= tail_page
;
1530 /* start of pages to remove */
1531 first_page
= list_entry(rb_list_head(to_remove
->next
),
1532 struct buffer_page
, list
);
1534 for (nr_removed
= 0; nr_removed
< nr_pages
; nr_removed
++) {
1535 to_remove
= rb_list_head(to_remove
)->next
;
1536 head_bit
|= (unsigned long)to_remove
& RB_PAGE_HEAD
;
1539 next_page
= rb_list_head(to_remove
)->next
;
1542 * Now we remove all pages between tail_page and next_page.
1543 * Make sure that we have head_bit value preserved for the
1546 tail_page
->next
= (struct list_head
*)((unsigned long)next_page
|
1548 next_page
= rb_list_head(next_page
);
1549 next_page
->prev
= tail_page
;
1551 /* make sure pages points to a valid page in the ring buffer */
1552 cpu_buffer
->pages
= next_page
;
1554 /* update head page */
1556 cpu_buffer
->head_page
= list_entry(next_page
,
1557 struct buffer_page
, list
);
1560 * change read pointer to make sure any read iterators reset
1563 cpu_buffer
->read
= 0;
1565 /* pages are removed, resume tracing and then free the pages */
1566 atomic_dec(&cpu_buffer
->record_disabled
);
1567 raw_spin_unlock_irq(&cpu_buffer
->reader_lock
);
1569 RB_WARN_ON(cpu_buffer
, list_empty(cpu_buffer
->pages
));
1571 /* last buffer page to remove */
1572 last_page
= list_entry(rb_list_head(to_remove
), struct buffer_page
,
1574 tmp_iter_page
= first_page
;
1579 to_remove_page
= tmp_iter_page
;
1580 rb_inc_page(cpu_buffer
, &tmp_iter_page
);
1582 /* update the counters */
1583 page_entries
= rb_page_entries(to_remove_page
);
1586 * If something was added to this page, it was full
1587 * since it is not the tail page. So we deduct the
1588 * bytes consumed in ring buffer from here.
1589 * Increment overrun to account for the lost events.
1591 local_add(page_entries
, &cpu_buffer
->overrun
);
1592 local_sub(BUF_PAGE_SIZE
, &cpu_buffer
->entries_bytes
);
1596 * We have already removed references to this list item, just
1597 * free up the buffer_page and its page
1599 free_buffer_page(to_remove_page
);
1602 } while (to_remove_page
!= last_page
);
1604 RB_WARN_ON(cpu_buffer
, nr_removed
);
1606 return nr_removed
== 0;
1610 rb_insert_pages(struct ring_buffer_per_cpu
*cpu_buffer
)
1612 struct list_head
*pages
= &cpu_buffer
->new_pages
;
1613 int retries
, success
;
1615 raw_spin_lock_irq(&cpu_buffer
->reader_lock
);
1617 * We are holding the reader lock, so the reader page won't be swapped
1618 * in the ring buffer. Now we are racing with the writer trying to
1619 * move head page and the tail page.
1620 * We are going to adapt the reader page update process where:
1621 * 1. We first splice the start and end of list of new pages between
1622 * the head page and its previous page.
1623 * 2. We cmpxchg the prev_page->next to point from head page to the
1624 * start of new pages list.
1625 * 3. Finally, we update the head->prev to the end of new list.
1627 * We will try this process 10 times, to make sure that we don't keep
1633 struct list_head
*head_page
, *prev_page
, *r
;
1634 struct list_head
*last_page
, *first_page
;
1635 struct list_head
*head_page_with_bit
;
1637 head_page
= &rb_set_head_page(cpu_buffer
)->list
;
1640 prev_page
= head_page
->prev
;
1642 first_page
= pages
->next
;
1643 last_page
= pages
->prev
;
1645 head_page_with_bit
= (struct list_head
*)
1646 ((unsigned long)head_page
| RB_PAGE_HEAD
);
1648 last_page
->next
= head_page_with_bit
;
1649 first_page
->prev
= prev_page
;
1651 r
= cmpxchg(&prev_page
->next
, head_page_with_bit
, first_page
);
1653 if (r
== head_page_with_bit
) {
1655 * yay, we replaced the page pointer to our new list,
1656 * now, we just have to update to head page's prev
1657 * pointer to point to end of list
1659 head_page
->prev
= last_page
;
1666 INIT_LIST_HEAD(pages
);
1668 * If we weren't successful in adding in new pages, warn and stop
1671 RB_WARN_ON(cpu_buffer
, !success
);
1672 raw_spin_unlock_irq(&cpu_buffer
->reader_lock
);
1674 /* free pages if they weren't inserted */
1676 struct buffer_page
*bpage
, *tmp
;
1677 list_for_each_entry_safe(bpage
, tmp
, &cpu_buffer
->new_pages
,
1679 list_del_init(&bpage
->list
);
1680 free_buffer_page(bpage
);
1686 static void rb_update_pages(struct ring_buffer_per_cpu
*cpu_buffer
)
1690 if (cpu_buffer
->nr_pages_to_update
> 0)
1691 success
= rb_insert_pages(cpu_buffer
);
1693 success
= rb_remove_pages(cpu_buffer
,
1694 -cpu_buffer
->nr_pages_to_update
);
1697 cpu_buffer
->nr_pages
+= cpu_buffer
->nr_pages_to_update
;
1700 static void update_pages_handler(struct work_struct
*work
)
1702 struct ring_buffer_per_cpu
*cpu_buffer
= container_of(work
,
1703 struct ring_buffer_per_cpu
, update_pages_work
);
1704 rb_update_pages(cpu_buffer
);
1705 complete(&cpu_buffer
->update_done
);
1709 * ring_buffer_resize - resize the ring buffer
1710 * @buffer: the buffer to resize.
1711 * @size: the new size.
1712 * @cpu_id: the cpu buffer to resize
1714 * Minimum size is 2 * BUF_PAGE_SIZE.
1716 * Returns 0 on success and < 0 on failure.
1718 int ring_buffer_resize(struct trace_buffer
*buffer
, unsigned long size
,
1721 struct ring_buffer_per_cpu
*cpu_buffer
;
1722 unsigned long nr_pages
;
1726 * Always succeed at resizing a non-existent buffer:
1731 /* Make sure the requested buffer exists */
1732 if (cpu_id
!= RING_BUFFER_ALL_CPUS
&&
1733 !cpumask_test_cpu(cpu_id
, buffer
->cpumask
))
1736 nr_pages
= DIV_ROUND_UP(size
, BUF_PAGE_SIZE
);
1738 /* we need a minimum of two pages */
1742 size
= nr_pages
* BUF_PAGE_SIZE
;
1744 /* prevent another thread from changing buffer sizes */
1745 mutex_lock(&buffer
->mutex
);
1748 if (cpu_id
== RING_BUFFER_ALL_CPUS
) {
1750 * Don't succeed if resizing is disabled, as a reader might be
1751 * manipulating the ring buffer and is expecting a sane state while
1754 for_each_buffer_cpu(buffer
, cpu
) {
1755 cpu_buffer
= buffer
->buffers
[cpu
];
1756 if (atomic_read(&cpu_buffer
->resize_disabled
)) {
1758 goto out_err_unlock
;
1762 /* calculate the pages to update */
1763 for_each_buffer_cpu(buffer
, cpu
) {
1764 cpu_buffer
= buffer
->buffers
[cpu
];
1766 cpu_buffer
->nr_pages_to_update
= nr_pages
-
1767 cpu_buffer
->nr_pages
;
1769 * nothing more to do for removing pages or no update
1771 if (cpu_buffer
->nr_pages_to_update
<= 0)
1774 * to add pages, make sure all new pages can be
1775 * allocated without receiving ENOMEM
1777 INIT_LIST_HEAD(&cpu_buffer
->new_pages
);
1778 if (__rb_allocate_pages(cpu_buffer
->nr_pages_to_update
,
1779 &cpu_buffer
->new_pages
, cpu
)) {
1780 /* not enough memory for new pages */
1788 * Fire off all the required work handlers
1789 * We can't schedule on offline CPUs, but it's not necessary
1790 * since we can change their buffer sizes without any race.
1792 for_each_buffer_cpu(buffer
, cpu
) {
1793 cpu_buffer
= buffer
->buffers
[cpu
];
1794 if (!cpu_buffer
->nr_pages_to_update
)
1797 /* Can't run something on an offline CPU. */
1798 if (!cpu_online(cpu
)) {
1799 rb_update_pages(cpu_buffer
);
1800 cpu_buffer
->nr_pages_to_update
= 0;
1802 schedule_work_on(cpu
,
1803 &cpu_buffer
->update_pages_work
);
1807 /* wait for all the updates to complete */
1808 for_each_buffer_cpu(buffer
, cpu
) {
1809 cpu_buffer
= buffer
->buffers
[cpu
];
1810 if (!cpu_buffer
->nr_pages_to_update
)
1813 if (cpu_online(cpu
))
1814 wait_for_completion(&cpu_buffer
->update_done
);
1815 cpu_buffer
->nr_pages_to_update
= 0;
1820 /* Make sure this CPU has been initialized */
1821 if (!cpumask_test_cpu(cpu_id
, buffer
->cpumask
))
1824 cpu_buffer
= buffer
->buffers
[cpu_id
];
1826 if (nr_pages
== cpu_buffer
->nr_pages
)
1830 * Don't succeed if resizing is disabled, as a reader might be
1831 * manipulating the ring buffer and is expecting a sane state while
1834 if (atomic_read(&cpu_buffer
->resize_disabled
)) {
1836 goto out_err_unlock
;
1839 cpu_buffer
->nr_pages_to_update
= nr_pages
-
1840 cpu_buffer
->nr_pages
;
1842 INIT_LIST_HEAD(&cpu_buffer
->new_pages
);
1843 if (cpu_buffer
->nr_pages_to_update
> 0 &&
1844 __rb_allocate_pages(cpu_buffer
->nr_pages_to_update
,
1845 &cpu_buffer
->new_pages
, cpu_id
)) {
1852 /* Can't run something on an offline CPU. */
1853 if (!cpu_online(cpu_id
))
1854 rb_update_pages(cpu_buffer
);
1856 schedule_work_on(cpu_id
,
1857 &cpu_buffer
->update_pages_work
);
1858 wait_for_completion(&cpu_buffer
->update_done
);
1861 cpu_buffer
->nr_pages_to_update
= 0;
1867 * The ring buffer resize can happen with the ring buffer
1868 * enabled, so that the update disturbs the tracing as little
1869 * as possible. But if the buffer is disabled, we do not need
1870 * to worry about that, and we can take the time to verify
1871 * that the buffer is not corrupt.
1873 if (atomic_read(&buffer
->record_disabled
)) {
1874 atomic_inc(&buffer
->record_disabled
);
1876 * Even though the buffer was disabled, we must make sure
1877 * that it is truly disabled before calling rb_check_pages.
1878 * There could have been a race between checking
1879 * record_disable and incrementing it.
1882 for_each_buffer_cpu(buffer
, cpu
) {
1883 cpu_buffer
= buffer
->buffers
[cpu
];
1884 rb_check_pages(cpu_buffer
);
1886 atomic_dec(&buffer
->record_disabled
);
1889 mutex_unlock(&buffer
->mutex
);
1893 for_each_buffer_cpu(buffer
, cpu
) {
1894 struct buffer_page
*bpage
, *tmp
;
1896 cpu_buffer
= buffer
->buffers
[cpu
];
1897 cpu_buffer
->nr_pages_to_update
= 0;
1899 if (list_empty(&cpu_buffer
->new_pages
))
1902 list_for_each_entry_safe(bpage
, tmp
, &cpu_buffer
->new_pages
,
1904 list_del_init(&bpage
->list
);
1905 free_buffer_page(bpage
);
1909 mutex_unlock(&buffer
->mutex
);
1912 EXPORT_SYMBOL_GPL(ring_buffer_resize
);
1914 void ring_buffer_change_overwrite(struct trace_buffer
*buffer
, int val
)
1916 mutex_lock(&buffer
->mutex
);
1918 buffer
->flags
|= RB_FL_OVERWRITE
;
1920 buffer
->flags
&= ~RB_FL_OVERWRITE
;
1921 mutex_unlock(&buffer
->mutex
);
1923 EXPORT_SYMBOL_GPL(ring_buffer_change_overwrite
);
1925 static __always_inline
void *__rb_page_index(struct buffer_page
*bpage
, unsigned index
)
1927 return bpage
->page
->data
+ index
;
1930 static __always_inline
struct ring_buffer_event
*
1931 rb_reader_event(struct ring_buffer_per_cpu
*cpu_buffer
)
1933 return __rb_page_index(cpu_buffer
->reader_page
,
1934 cpu_buffer
->reader_page
->read
);
1937 static __always_inline
unsigned rb_page_commit(struct buffer_page
*bpage
)
1939 return local_read(&bpage
->page
->commit
);
1942 static struct ring_buffer_event
*
1943 rb_iter_head_event(struct ring_buffer_iter
*iter
)
1945 struct ring_buffer_event
*event
;
1946 struct buffer_page
*iter_head_page
= iter
->head_page
;
1947 unsigned long commit
;
1950 if (iter
->head
!= iter
->next_event
)
1954 * When the writer goes across pages, it issues a cmpxchg which
1955 * is a mb(), which will synchronize with the rmb here.
1956 * (see rb_tail_page_update() and __rb_reserve_next())
1958 commit
= rb_page_commit(iter_head_page
);
1960 event
= __rb_page_index(iter_head_page
, iter
->head
);
1961 length
= rb_event_length(event
);
1964 * READ_ONCE() doesn't work on functions and we don't want the
1965 * compiler doing any crazy optimizations with length.
1969 if ((iter
->head
+ length
) > commit
|| length
> BUF_MAX_DATA_SIZE
)
1970 /* Writer corrupted the read? */
1973 memcpy(iter
->event
, event
, length
);
1975 * If the page stamp is still the same after this rmb() then the
1976 * event was safely copied without the writer entering the page.
1980 /* Make sure the page didn't change since we read this */
1981 if (iter
->page_stamp
!= iter_head_page
->page
->time_stamp
||
1982 commit
> rb_page_commit(iter_head_page
))
1985 iter
->next_event
= iter
->head
+ length
;
1988 /* Reset to the beginning */
1989 iter
->page_stamp
= iter
->read_stamp
= iter
->head_page
->page
->time_stamp
;
1991 iter
->next_event
= 0;
1992 iter
->missed_events
= 1;
1996 /* Size is determined by what has been committed */
1997 static __always_inline
unsigned rb_page_size(struct buffer_page
*bpage
)
1999 return rb_page_commit(bpage
);
2002 static __always_inline
unsigned
2003 rb_commit_index(struct ring_buffer_per_cpu
*cpu_buffer
)
2005 return rb_page_commit(cpu_buffer
->commit_page
);
2008 static __always_inline
unsigned
2009 rb_event_index(struct ring_buffer_event
*event
)
2011 unsigned long addr
= (unsigned long)event
;
2013 return (addr
& ~PAGE_MASK
) - BUF_PAGE_HDR_SIZE
;
2016 static void rb_inc_iter(struct ring_buffer_iter
*iter
)
2018 struct ring_buffer_per_cpu
*cpu_buffer
= iter
->cpu_buffer
;
2021 * The iterator could be on the reader page (it starts there).
2022 * But the head could have moved, since the reader was
2023 * found. Check for this case and assign the iterator
2024 * to the head page instead of next.
2026 if (iter
->head_page
== cpu_buffer
->reader_page
)
2027 iter
->head_page
= rb_set_head_page(cpu_buffer
);
2029 rb_inc_page(cpu_buffer
, &iter
->head_page
);
2031 iter
->page_stamp
= iter
->read_stamp
= iter
->head_page
->page
->time_stamp
;
2033 iter
->next_event
= 0;
2037 * rb_handle_head_page - writer hit the head page
2039 * Returns: +1 to retry page
2044 rb_handle_head_page(struct ring_buffer_per_cpu
*cpu_buffer
,
2045 struct buffer_page
*tail_page
,
2046 struct buffer_page
*next_page
)
2048 struct buffer_page
*new_head
;
2053 entries
= rb_page_entries(next_page
);
2056 * The hard part is here. We need to move the head
2057 * forward, and protect against both readers on
2058 * other CPUs and writers coming in via interrupts.
2060 type
= rb_head_page_set_update(cpu_buffer
, next_page
, tail_page
,
2064 * type can be one of four:
2065 * NORMAL - an interrupt already moved it for us
2066 * HEAD - we are the first to get here.
2067 * UPDATE - we are the interrupt interrupting
2069 * MOVED - a reader on another CPU moved the next
2070 * pointer to its reader page. Give up
2077 * We changed the head to UPDATE, thus
2078 * it is our responsibility to update
2081 local_add(entries
, &cpu_buffer
->overrun
);
2082 local_sub(BUF_PAGE_SIZE
, &cpu_buffer
->entries_bytes
);
2085 * The entries will be zeroed out when we move the
2089 /* still more to do */
2092 case RB_PAGE_UPDATE
:
2094 * This is an interrupt that interrupt the
2095 * previous update. Still more to do.
2098 case RB_PAGE_NORMAL
:
2100 * An interrupt came in before the update
2101 * and processed this for us.
2102 * Nothing left to do.
2107 * The reader is on another CPU and just did
2108 * a swap with our next_page.
2113 RB_WARN_ON(cpu_buffer
, 1); /* WTF??? */
2118 * Now that we are here, the old head pointer is
2119 * set to UPDATE. This will keep the reader from
2120 * swapping the head page with the reader page.
2121 * The reader (on another CPU) will spin till
2124 * We just need to protect against interrupts
2125 * doing the job. We will set the next pointer
2126 * to HEAD. After that, we set the old pointer
2127 * to NORMAL, but only if it was HEAD before.
2128 * otherwise we are an interrupt, and only
2129 * want the outer most commit to reset it.
2131 new_head
= next_page
;
2132 rb_inc_page(cpu_buffer
, &new_head
);
2134 ret
= rb_head_page_set_head(cpu_buffer
, new_head
, next_page
,
2138 * Valid returns are:
2139 * HEAD - an interrupt came in and already set it.
2140 * NORMAL - One of two things:
2141 * 1) We really set it.
2142 * 2) A bunch of interrupts came in and moved
2143 * the page forward again.
2147 case RB_PAGE_NORMAL
:
2151 RB_WARN_ON(cpu_buffer
, 1);
2156 * It is possible that an interrupt came in,
2157 * set the head up, then more interrupts came in
2158 * and moved it again. When we get back here,
2159 * the page would have been set to NORMAL but we
2160 * just set it back to HEAD.
2162 * How do you detect this? Well, if that happened
2163 * the tail page would have moved.
2165 if (ret
== RB_PAGE_NORMAL
) {
2166 struct buffer_page
*buffer_tail_page
;
2168 buffer_tail_page
= READ_ONCE(cpu_buffer
->tail_page
);
2170 * If the tail had moved passed next, then we need
2171 * to reset the pointer.
2173 if (buffer_tail_page
!= tail_page
&&
2174 buffer_tail_page
!= next_page
)
2175 rb_head_page_set_normal(cpu_buffer
, new_head
,
2181 * If this was the outer most commit (the one that
2182 * changed the original pointer from HEAD to UPDATE),
2183 * then it is up to us to reset it to NORMAL.
2185 if (type
== RB_PAGE_HEAD
) {
2186 ret
= rb_head_page_set_normal(cpu_buffer
, next_page
,
2189 if (RB_WARN_ON(cpu_buffer
,
2190 ret
!= RB_PAGE_UPDATE
))
2198 rb_reset_tail(struct ring_buffer_per_cpu
*cpu_buffer
,
2199 unsigned long tail
, struct rb_event_info
*info
)
2201 struct buffer_page
*tail_page
= info
->tail_page
;
2202 struct ring_buffer_event
*event
;
2203 unsigned long length
= info
->length
;
2206 * Only the event that crossed the page boundary
2207 * must fill the old tail_page with padding.
2209 if (tail
>= BUF_PAGE_SIZE
) {
2211 * If the page was filled, then we still need
2212 * to update the real_end. Reset it to zero
2213 * and the reader will ignore it.
2215 if (tail
== BUF_PAGE_SIZE
)
2216 tail_page
->real_end
= 0;
2218 local_sub(length
, &tail_page
->write
);
2222 event
= __rb_page_index(tail_page
, tail
);
2224 /* account for padding bytes */
2225 local_add(BUF_PAGE_SIZE
- tail
, &cpu_buffer
->entries_bytes
);
2228 * Save the original length to the meta data.
2229 * This will be used by the reader to add lost event
2232 tail_page
->real_end
= tail
;
2235 * If this event is bigger than the minimum size, then
2236 * we need to be careful that we don't subtract the
2237 * write counter enough to allow another writer to slip
2239 * We put in a discarded commit instead, to make sure
2240 * that this space is not used again.
2242 * If we are less than the minimum size, we don't need to
2245 if (tail
> (BUF_PAGE_SIZE
- RB_EVNT_MIN_SIZE
)) {
2246 /* No room for any events */
2248 /* Mark the rest of the page with padding */
2249 rb_event_set_padding(event
);
2251 /* Set the write back to the previous setting */
2252 local_sub(length
, &tail_page
->write
);
2256 /* Put in a discarded event */
2257 event
->array
[0] = (BUF_PAGE_SIZE
- tail
) - RB_EVNT_HDR_SIZE
;
2258 event
->type_len
= RINGBUF_TYPE_PADDING
;
2259 /* time delta must be non zero */
2260 event
->time_delta
= 1;
2262 /* Set write to end of buffer */
2263 length
= (tail
+ length
) - BUF_PAGE_SIZE
;
2264 local_sub(length
, &tail_page
->write
);
2267 static inline void rb_end_commit(struct ring_buffer_per_cpu
*cpu_buffer
);
2270 * This is the slow path, force gcc not to inline it.
2272 static noinline
struct ring_buffer_event
*
2273 rb_move_tail(struct ring_buffer_per_cpu
*cpu_buffer
,
2274 unsigned long tail
, struct rb_event_info
*info
)
2276 struct buffer_page
*tail_page
= info
->tail_page
;
2277 struct buffer_page
*commit_page
= cpu_buffer
->commit_page
;
2278 struct trace_buffer
*buffer
= cpu_buffer
->buffer
;
2279 struct buffer_page
*next_page
;
2282 next_page
= tail_page
;
2284 rb_inc_page(cpu_buffer
, &next_page
);
2287 * If for some reason, we had an interrupt storm that made
2288 * it all the way around the buffer, bail, and warn
2291 if (unlikely(next_page
== commit_page
)) {
2292 local_inc(&cpu_buffer
->commit_overrun
);
2297 * This is where the fun begins!
2299 * We are fighting against races between a reader that
2300 * could be on another CPU trying to swap its reader
2301 * page with the buffer head.
2303 * We are also fighting against interrupts coming in and
2304 * moving the head or tail on us as well.
2306 * If the next page is the head page then we have filled
2307 * the buffer, unless the commit page is still on the
2310 if (rb_is_head_page(cpu_buffer
, next_page
, &tail_page
->list
)) {
2313 * If the commit is not on the reader page, then
2314 * move the header page.
2316 if (!rb_is_reader_page(cpu_buffer
->commit_page
)) {
2318 * If we are not in overwrite mode,
2319 * this is easy, just stop here.
2321 if (!(buffer
->flags
& RB_FL_OVERWRITE
)) {
2322 local_inc(&cpu_buffer
->dropped_events
);
2326 ret
= rb_handle_head_page(cpu_buffer
,
2335 * We need to be careful here too. The
2336 * commit page could still be on the reader
2337 * page. We could have a small buffer, and
2338 * have filled up the buffer with events
2339 * from interrupts and such, and wrapped.
2341 * Note, if the tail page is also the on the
2342 * reader_page, we let it move out.
2344 if (unlikely((cpu_buffer
->commit_page
!=
2345 cpu_buffer
->tail_page
) &&
2346 (cpu_buffer
->commit_page
==
2347 cpu_buffer
->reader_page
))) {
2348 local_inc(&cpu_buffer
->commit_overrun
);
2354 rb_tail_page_update(cpu_buffer
, tail_page
, next_page
);
2358 rb_reset_tail(cpu_buffer
, tail
, info
);
2360 /* Commit what we have for now. */
2361 rb_end_commit(cpu_buffer
);
2362 /* rb_end_commit() decs committing */
2363 local_inc(&cpu_buffer
->committing
);
2365 /* fail and let the caller try again */
2366 return ERR_PTR(-EAGAIN
);
2370 rb_reset_tail(cpu_buffer
, tail
, info
);
2375 /* Slow path, do not inline */
2376 static noinline
struct ring_buffer_event
*
2377 rb_add_time_stamp(struct ring_buffer_event
*event
, u64 delta
, bool abs
)
2380 event
->type_len
= RINGBUF_TYPE_TIME_STAMP
;
2382 event
->type_len
= RINGBUF_TYPE_TIME_EXTEND
;
2384 /* Not the first event on the page, or not delta? */
2385 if (abs
|| rb_event_index(event
)) {
2386 event
->time_delta
= delta
& TS_MASK
;
2387 event
->array
[0] = delta
>> TS_SHIFT
;
2389 /* nope, just zero it */
2390 event
->time_delta
= 0;
2391 event
->array
[0] = 0;
2394 return skip_time_extend(event
);
2397 static inline bool rb_event_is_commit(struct ring_buffer_per_cpu
*cpu_buffer
,
2398 struct ring_buffer_event
*event
);
2401 * rb_update_event - update event type and data
2402 * @cpu_buffer: The per cpu buffer of the @event
2403 * @event: the event to update
2404 * @info: The info to update the @event with (contains length and delta)
2406 * Update the type and data fields of the @event. The length
2407 * is the actual size that is written to the ring buffer,
2408 * and with this, we can determine what to place into the
2412 rb_update_event(struct ring_buffer_per_cpu
*cpu_buffer
,
2413 struct ring_buffer_event
*event
,
2414 struct rb_event_info
*info
)
2416 unsigned length
= info
->length
;
2417 u64 delta
= info
->delta
;
2419 /* Only a commit updates the timestamp */
2420 if (unlikely(!rb_event_is_commit(cpu_buffer
, event
)))
2424 * If we need to add a timestamp, then we
2425 * add it to the start of the reserved space.
2427 if (unlikely(info
->add_timestamp
)) {
2428 bool abs
= ring_buffer_time_stamp_abs(cpu_buffer
->buffer
);
2430 event
= rb_add_time_stamp(event
, info
->delta
, abs
);
2431 length
-= RB_LEN_TIME_EXTEND
;
2435 event
->time_delta
= delta
;
2436 length
-= RB_EVNT_HDR_SIZE
;
2437 if (length
> RB_MAX_SMALL_DATA
) {
2438 event
->type_len
= 0;
2439 event
->array
[0] = length
;
2441 event
->type_len
= DIV_ROUND_UP(length
, RB_ALIGNMENT
);
2444 static unsigned rb_calculate_event_length(unsigned length
)
2446 struct ring_buffer_event event
; /* Used only for sizeof array */
2448 /* zero length can cause confusions */
2452 if (length
> RB_MAX_SMALL_DATA
)
2453 length
+= sizeof(event
.array
[0]);
2455 length
+= RB_EVNT_HDR_SIZE
;
2456 length
= ALIGN(length
, RB_ALIGNMENT
);
2459 * In case the time delta is larger than the 27 bits for it
2460 * in the header, we need to add a timestamp. If another
2461 * event comes in when trying to discard this one to increase
2462 * the length, then the timestamp will be added in the allocated
2463 * space of this event. If length is bigger than the size needed
2464 * for the TIME_EXTEND, then padding has to be used. The events
2465 * length must be either RB_LEN_TIME_EXTEND, or greater than or equal
2466 * to RB_LEN_TIME_EXTEND + 8, as 8 is the minimum size for padding.
2467 * As length is a multiple of 4, we only need to worry if it
2468 * is 12 (RB_LEN_TIME_EXTEND + 4).
2470 if (length
== RB_LEN_TIME_EXTEND
+ RB_ALIGNMENT
)
2471 length
+= RB_ALIGNMENT
;
2476 #ifndef CONFIG_HAVE_UNSTABLE_SCHED_CLOCK
2477 static inline bool sched_clock_stable(void)
2484 rb_try_to_discard(struct ring_buffer_per_cpu
*cpu_buffer
,
2485 struct ring_buffer_event
*event
)
2487 unsigned long new_index
, old_index
;
2488 struct buffer_page
*bpage
;
2489 unsigned long index
;
2492 new_index
= rb_event_index(event
);
2493 old_index
= new_index
+ rb_event_ts_length(event
);
2494 addr
= (unsigned long)event
;
2497 bpage
= READ_ONCE(cpu_buffer
->tail_page
);
2499 if (bpage
->page
== (void *)addr
&& rb_page_write(bpage
) == old_index
) {
2500 unsigned long write_mask
=
2501 local_read(&bpage
->write
) & ~RB_WRITE_MASK
;
2502 unsigned long event_length
= rb_event_length(event
);
2504 * This is on the tail page. It is possible that
2505 * a write could come in and move the tail page
2506 * and write to the next page. That is fine
2507 * because we just shorten what is on this page.
2509 old_index
+= write_mask
;
2510 new_index
+= write_mask
;
2511 index
= local_cmpxchg(&bpage
->write
, old_index
, new_index
);
2512 if (index
== old_index
) {
2513 /* update counters */
2514 local_sub(event_length
, &cpu_buffer
->entries_bytes
);
2519 /* could not discard */
2523 static void rb_start_commit(struct ring_buffer_per_cpu
*cpu_buffer
)
2525 local_inc(&cpu_buffer
->committing
);
2526 local_inc(&cpu_buffer
->commits
);
2529 static __always_inline
void
2530 rb_set_commit_to_write(struct ring_buffer_per_cpu
*cpu_buffer
)
2532 unsigned long max_count
;
2535 * We only race with interrupts and NMIs on this CPU.
2536 * If we own the commit event, then we can commit
2537 * all others that interrupted us, since the interruptions
2538 * are in stack format (they finish before they come
2539 * back to us). This allows us to do a simple loop to
2540 * assign the commit to the tail.
2543 max_count
= cpu_buffer
->nr_pages
* 100;
2545 while (cpu_buffer
->commit_page
!= READ_ONCE(cpu_buffer
->tail_page
)) {
2546 if (RB_WARN_ON(cpu_buffer
, !(--max_count
)))
2548 if (RB_WARN_ON(cpu_buffer
,
2549 rb_is_reader_page(cpu_buffer
->tail_page
)))
2551 local_set(&cpu_buffer
->commit_page
->page
->commit
,
2552 rb_page_write(cpu_buffer
->commit_page
));
2553 rb_inc_page(cpu_buffer
, &cpu_buffer
->commit_page
);
2554 /* Only update the write stamp if the page has an event */
2555 if (rb_page_write(cpu_buffer
->commit_page
))
2556 cpu_buffer
->write_stamp
=
2557 cpu_buffer
->commit_page
->page
->time_stamp
;
2558 /* add barrier to keep gcc from optimizing too much */
2561 while (rb_commit_index(cpu_buffer
) !=
2562 rb_page_write(cpu_buffer
->commit_page
)) {
2564 local_set(&cpu_buffer
->commit_page
->page
->commit
,
2565 rb_page_write(cpu_buffer
->commit_page
));
2566 RB_WARN_ON(cpu_buffer
,
2567 local_read(&cpu_buffer
->commit_page
->page
->commit
) &
2572 /* again, keep gcc from optimizing */
2576 * If an interrupt came in just after the first while loop
2577 * and pushed the tail page forward, we will be left with
2578 * a dangling commit that will never go forward.
2580 if (unlikely(cpu_buffer
->commit_page
!= READ_ONCE(cpu_buffer
->tail_page
)))
2584 static __always_inline
void rb_end_commit(struct ring_buffer_per_cpu
*cpu_buffer
)
2586 unsigned long commits
;
2588 if (RB_WARN_ON(cpu_buffer
,
2589 !local_read(&cpu_buffer
->committing
)))
2593 commits
= local_read(&cpu_buffer
->commits
);
2594 /* synchronize with interrupts */
2596 if (local_read(&cpu_buffer
->committing
) == 1)
2597 rb_set_commit_to_write(cpu_buffer
);
2599 local_dec(&cpu_buffer
->committing
);
2601 /* synchronize with interrupts */
2605 * Need to account for interrupts coming in between the
2606 * updating of the commit page and the clearing of the
2607 * committing counter.
2609 if (unlikely(local_read(&cpu_buffer
->commits
) != commits
) &&
2610 !local_read(&cpu_buffer
->committing
)) {
2611 local_inc(&cpu_buffer
->committing
);
2616 static inline void rb_event_discard(struct ring_buffer_event
*event
)
2618 if (extended_time(event
))
2619 event
= skip_time_extend(event
);
2621 /* array[0] holds the actual length for the discarded event */
2622 event
->array
[0] = rb_event_data_length(event
) - RB_EVNT_HDR_SIZE
;
2623 event
->type_len
= RINGBUF_TYPE_PADDING
;
2624 /* time delta must be non zero */
2625 if (!event
->time_delta
)
2626 event
->time_delta
= 1;
2629 static __always_inline
bool
2630 rb_event_is_commit(struct ring_buffer_per_cpu
*cpu_buffer
,
2631 struct ring_buffer_event
*event
)
2633 unsigned long addr
= (unsigned long)event
;
2634 unsigned long index
;
2636 index
= rb_event_index(event
);
2639 return cpu_buffer
->commit_page
->page
== (void *)addr
&&
2640 rb_commit_index(cpu_buffer
) == index
;
2643 static __always_inline
void
2644 rb_update_write_stamp(struct ring_buffer_per_cpu
*cpu_buffer
,
2645 struct ring_buffer_event
*event
)
2650 * The event first in the commit queue updates the
2653 if (rb_event_is_commit(cpu_buffer
, event
)) {
2655 * A commit event that is first on a page
2656 * updates the write timestamp with the page stamp
2658 if (!rb_event_index(event
))
2659 cpu_buffer
->write_stamp
=
2660 cpu_buffer
->commit_page
->page
->time_stamp
;
2661 else if (event
->type_len
== RINGBUF_TYPE_TIME_EXTEND
) {
2662 delta
= ring_buffer_event_time_stamp(event
);
2663 cpu_buffer
->write_stamp
+= delta
;
2664 } else if (event
->type_len
== RINGBUF_TYPE_TIME_STAMP
) {
2665 delta
= ring_buffer_event_time_stamp(event
);
2666 cpu_buffer
->write_stamp
= delta
;
2668 cpu_buffer
->write_stamp
+= event
->time_delta
;
2672 static void rb_commit(struct ring_buffer_per_cpu
*cpu_buffer
,
2673 struct ring_buffer_event
*event
)
2675 local_inc(&cpu_buffer
->entries
);
2676 rb_update_write_stamp(cpu_buffer
, event
);
2677 rb_end_commit(cpu_buffer
);
2680 static __always_inline
void
2681 rb_wakeups(struct trace_buffer
*buffer
, struct ring_buffer_per_cpu
*cpu_buffer
)
2687 if (buffer
->irq_work
.waiters_pending
) {
2688 buffer
->irq_work
.waiters_pending
= false;
2689 /* irq_work_queue() supplies it's own memory barriers */
2690 irq_work_queue(&buffer
->irq_work
.work
);
2693 if (cpu_buffer
->irq_work
.waiters_pending
) {
2694 cpu_buffer
->irq_work
.waiters_pending
= false;
2695 /* irq_work_queue() supplies it's own memory barriers */
2696 irq_work_queue(&cpu_buffer
->irq_work
.work
);
2699 if (cpu_buffer
->last_pages_touch
== local_read(&cpu_buffer
->pages_touched
))
2702 if (cpu_buffer
->reader_page
== cpu_buffer
->commit_page
)
2705 if (!cpu_buffer
->irq_work
.full_waiters_pending
)
2708 cpu_buffer
->last_pages_touch
= local_read(&cpu_buffer
->pages_touched
);
2710 full
= cpu_buffer
->shortest_full
;
2711 nr_pages
= cpu_buffer
->nr_pages
;
2712 dirty
= ring_buffer_nr_dirty_pages(buffer
, cpu_buffer
->cpu
);
2713 if (full
&& nr_pages
&& (dirty
* 100) <= full
* nr_pages
)
2716 cpu_buffer
->irq_work
.wakeup_full
= true;
2717 cpu_buffer
->irq_work
.full_waiters_pending
= false;
2718 /* irq_work_queue() supplies it's own memory barriers */
2719 irq_work_queue(&cpu_buffer
->irq_work
.work
);
2723 * The lock and unlock are done within a preempt disable section.
2724 * The current_context per_cpu variable can only be modified
2725 * by the current task between lock and unlock. But it can
2726 * be modified more than once via an interrupt. To pass this
2727 * information from the lock to the unlock without having to
2728 * access the 'in_interrupt()' functions again (which do show
2729 * a bit of overhead in something as critical as function tracing,
2730 * we use a bitmask trick.
2732 * bit 0 = NMI context
2733 * bit 1 = IRQ context
2734 * bit 2 = SoftIRQ context
2735 * bit 3 = normal context.
2737 * This works because this is the order of contexts that can
2738 * preempt other contexts. A SoftIRQ never preempts an IRQ
2741 * When the context is determined, the corresponding bit is
2742 * checked and set (if it was set, then a recursion of that context
2745 * On unlock, we need to clear this bit. To do so, just subtract
2746 * 1 from the current_context and AND it to itself.
2750 * 101 & 100 = 100 (clearing bit zero)
2753 * 1010 & 1001 = 1000 (clearing bit 1)
2755 * The least significant bit can be cleared this way, and it
2756 * just so happens that it is the same bit corresponding to
2757 * the current context.
2760 static __always_inline
int
2761 trace_recursive_lock(struct ring_buffer_per_cpu
*cpu_buffer
)
2763 unsigned int val
= cpu_buffer
->current_context
;
2764 unsigned long pc
= preempt_count();
2767 if (!(pc
& (NMI_MASK
| HARDIRQ_MASK
| SOFTIRQ_OFFSET
)))
2768 bit
= RB_CTX_NORMAL
;
2770 bit
= pc
& NMI_MASK
? RB_CTX_NMI
:
2771 pc
& HARDIRQ_MASK
? RB_CTX_IRQ
: RB_CTX_SOFTIRQ
;
2773 if (unlikely(val
& (1 << (bit
+ cpu_buffer
->nest
))))
2776 val
|= (1 << (bit
+ cpu_buffer
->nest
));
2777 cpu_buffer
->current_context
= val
;
2782 static __always_inline
void
2783 trace_recursive_unlock(struct ring_buffer_per_cpu
*cpu_buffer
)
2785 cpu_buffer
->current_context
&=
2786 cpu_buffer
->current_context
- (1 << cpu_buffer
->nest
);
2789 /* The recursive locking above uses 4 bits */
2790 #define NESTED_BITS 4
2793 * ring_buffer_nest_start - Allow to trace while nested
2794 * @buffer: The ring buffer to modify
2796 * The ring buffer has a safety mechanism to prevent recursion.
2797 * But there may be a case where a trace needs to be done while
2798 * tracing something else. In this case, calling this function
2799 * will allow this function to nest within a currently active
2800 * ring_buffer_lock_reserve().
2802 * Call this function before calling another ring_buffer_lock_reserve() and
2803 * call ring_buffer_nest_end() after the nested ring_buffer_unlock_commit().
2805 void ring_buffer_nest_start(struct trace_buffer
*buffer
)
2807 struct ring_buffer_per_cpu
*cpu_buffer
;
2810 /* Enabled by ring_buffer_nest_end() */
2811 preempt_disable_notrace();
2812 cpu
= raw_smp_processor_id();
2813 cpu_buffer
= buffer
->buffers
[cpu
];
2814 /* This is the shift value for the above recursive locking */
2815 cpu_buffer
->nest
+= NESTED_BITS
;
2819 * ring_buffer_nest_end - Allow to trace while nested
2820 * @buffer: The ring buffer to modify
2822 * Must be called after ring_buffer_nest_start() and after the
2823 * ring_buffer_unlock_commit().
2825 void ring_buffer_nest_end(struct trace_buffer
*buffer
)
2827 struct ring_buffer_per_cpu
*cpu_buffer
;
2830 /* disabled by ring_buffer_nest_start() */
2831 cpu
= raw_smp_processor_id();
2832 cpu_buffer
= buffer
->buffers
[cpu
];
2833 /* This is the shift value for the above recursive locking */
2834 cpu_buffer
->nest
-= NESTED_BITS
;
2835 preempt_enable_notrace();
2839 * ring_buffer_unlock_commit - commit a reserved
2840 * @buffer: The buffer to commit to
2841 * @event: The event pointer to commit.
2843 * This commits the data to the ring buffer, and releases any locks held.
2845 * Must be paired with ring_buffer_lock_reserve.
2847 int ring_buffer_unlock_commit(struct trace_buffer
*buffer
,
2848 struct ring_buffer_event
*event
)
2850 struct ring_buffer_per_cpu
*cpu_buffer
;
2851 int cpu
= raw_smp_processor_id();
2853 cpu_buffer
= buffer
->buffers
[cpu
];
2855 rb_commit(cpu_buffer
, event
);
2857 rb_wakeups(buffer
, cpu_buffer
);
2859 trace_recursive_unlock(cpu_buffer
);
2861 preempt_enable_notrace();
2865 EXPORT_SYMBOL_GPL(ring_buffer_unlock_commit
);
2867 static noinline
void
2868 rb_handle_timestamp(struct ring_buffer_per_cpu
*cpu_buffer
,
2869 struct rb_event_info
*info
)
2871 WARN_ONCE(info
->delta
> (1ULL << 59),
2872 KERN_WARNING
"Delta way too big! %llu ts=%llu write stamp = %llu\n%s",
2873 (unsigned long long)info
->delta
,
2874 (unsigned long long)info
->ts
,
2875 (unsigned long long)cpu_buffer
->write_stamp
,
2876 sched_clock_stable() ? "" :
2877 "If you just came from a suspend/resume,\n"
2878 "please switch to the trace global clock:\n"
2879 " echo global > /sys/kernel/debug/tracing/trace_clock\n"
2880 "or add trace_clock=global to the kernel command line\n");
2881 info
->add_timestamp
= 1;
2884 static struct ring_buffer_event
*
2885 __rb_reserve_next(struct ring_buffer_per_cpu
*cpu_buffer
,
2886 struct rb_event_info
*info
)
2888 struct ring_buffer_event
*event
;
2889 struct buffer_page
*tail_page
;
2890 unsigned long tail
, write
;
2893 * If the time delta since the last event is too big to
2894 * hold in the time field of the event, then we append a
2895 * TIME EXTEND event ahead of the data event.
2897 if (unlikely(info
->add_timestamp
))
2898 info
->length
+= RB_LEN_TIME_EXTEND
;
2900 /* Don't let the compiler play games with cpu_buffer->tail_page */
2901 tail_page
= info
->tail_page
= READ_ONCE(cpu_buffer
->tail_page
);
2902 write
= local_add_return(info
->length
, &tail_page
->write
);
2904 /* set write to only the index of the write */
2905 write
&= RB_WRITE_MASK
;
2906 tail
= write
- info
->length
;
2909 * If this is the first commit on the page, then it has the same
2910 * timestamp as the page itself.
2912 if (!tail
&& !ring_buffer_time_stamp_abs(cpu_buffer
->buffer
))
2915 /* See if we shot pass the end of this buffer page */
2916 if (unlikely(write
> BUF_PAGE_SIZE
))
2917 return rb_move_tail(cpu_buffer
, tail
, info
);
2919 /* We reserved something on the buffer */
2921 event
= __rb_page_index(tail_page
, tail
);
2922 rb_update_event(cpu_buffer
, event
, info
);
2924 local_inc(&tail_page
->entries
);
2927 * If this is the first commit on the page, then update
2931 tail_page
->page
->time_stamp
= info
->ts
;
2933 /* account for these added bytes */
2934 local_add(info
->length
, &cpu_buffer
->entries_bytes
);
2939 static __always_inline
struct ring_buffer_event
*
2940 rb_reserve_next_event(struct trace_buffer
*buffer
,
2941 struct ring_buffer_per_cpu
*cpu_buffer
,
2942 unsigned long length
)
2944 struct ring_buffer_event
*event
;
2945 struct rb_event_info info
;
2949 rb_start_commit(cpu_buffer
);
2951 #ifdef CONFIG_RING_BUFFER_ALLOW_SWAP
2953 * Due to the ability to swap a cpu buffer from a buffer
2954 * it is possible it was swapped before we committed.
2955 * (committing stops a swap). We check for it here and
2956 * if it happened, we have to fail the write.
2959 if (unlikely(READ_ONCE(cpu_buffer
->buffer
) != buffer
)) {
2960 local_dec(&cpu_buffer
->committing
);
2961 local_dec(&cpu_buffer
->commits
);
2966 info
.length
= rb_calculate_event_length(length
);
2968 info
.add_timestamp
= 0;
2972 * We allow for interrupts to reenter here and do a trace.
2973 * If one does, it will cause this original code to loop
2974 * back here. Even with heavy interrupts happening, this
2975 * should only happen a few times in a row. If this happens
2976 * 1000 times in a row, there must be either an interrupt
2977 * storm or we have something buggy.
2980 if (RB_WARN_ON(cpu_buffer
, ++nr_loops
> 1000))
2983 info
.ts
= rb_time_stamp(cpu_buffer
->buffer
);
2984 diff
= info
.ts
- cpu_buffer
->write_stamp
;
2986 /* make sure this diff is calculated here */
2989 if (ring_buffer_time_stamp_abs(buffer
)) {
2990 info
.delta
= info
.ts
;
2991 rb_handle_timestamp(cpu_buffer
, &info
);
2992 } else /* Did the write stamp get updated already? */
2993 if (likely(info
.ts
>= cpu_buffer
->write_stamp
)) {
2995 if (unlikely(test_time_stamp(info
.delta
)))
2996 rb_handle_timestamp(cpu_buffer
, &info
);
2999 event
= __rb_reserve_next(cpu_buffer
, &info
);
3001 if (unlikely(PTR_ERR(event
) == -EAGAIN
)) {
3002 if (info
.add_timestamp
)
3003 info
.length
-= RB_LEN_TIME_EXTEND
;
3013 rb_end_commit(cpu_buffer
);
3018 * ring_buffer_lock_reserve - reserve a part of the buffer
3019 * @buffer: the ring buffer to reserve from
3020 * @length: the length of the data to reserve (excluding event header)
3022 * Returns a reserved event on the ring buffer to copy directly to.
3023 * The user of this interface will need to get the body to write into
3024 * and can use the ring_buffer_event_data() interface.
3026 * The length is the length of the data needed, not the event length
3027 * which also includes the event header.
3029 * Must be paired with ring_buffer_unlock_commit, unless NULL is returned.
3030 * If NULL is returned, then nothing has been allocated or locked.
3032 struct ring_buffer_event
*
3033 ring_buffer_lock_reserve(struct trace_buffer
*buffer
, unsigned long length
)
3035 struct ring_buffer_per_cpu
*cpu_buffer
;
3036 struct ring_buffer_event
*event
;
3039 /* If we are tracing schedule, we don't want to recurse */
3040 preempt_disable_notrace();
3042 if (unlikely(atomic_read(&buffer
->record_disabled
)))
3045 cpu
= raw_smp_processor_id();
3047 if (unlikely(!cpumask_test_cpu(cpu
, buffer
->cpumask
)))
3050 cpu_buffer
= buffer
->buffers
[cpu
];
3052 if (unlikely(atomic_read(&cpu_buffer
->record_disabled
)))
3055 if (unlikely(length
> BUF_MAX_DATA_SIZE
))
3058 if (unlikely(trace_recursive_lock(cpu_buffer
)))
3061 event
= rb_reserve_next_event(buffer
, cpu_buffer
, length
);
3068 trace_recursive_unlock(cpu_buffer
);
3070 preempt_enable_notrace();
3073 EXPORT_SYMBOL_GPL(ring_buffer_lock_reserve
);
3076 * Decrement the entries to the page that an event is on.
3077 * The event does not even need to exist, only the pointer
3078 * to the page it is on. This may only be called before the commit
3082 rb_decrement_entry(struct ring_buffer_per_cpu
*cpu_buffer
,
3083 struct ring_buffer_event
*event
)
3085 unsigned long addr
= (unsigned long)event
;
3086 struct buffer_page
*bpage
= cpu_buffer
->commit_page
;
3087 struct buffer_page
*start
;
3091 /* Do the likely case first */
3092 if (likely(bpage
->page
== (void *)addr
)) {
3093 local_dec(&bpage
->entries
);
3098 * Because the commit page may be on the reader page we
3099 * start with the next page and check the end loop there.
3101 rb_inc_page(cpu_buffer
, &bpage
);
3104 if (bpage
->page
== (void *)addr
) {
3105 local_dec(&bpage
->entries
);
3108 rb_inc_page(cpu_buffer
, &bpage
);
3109 } while (bpage
!= start
);
3111 /* commit not part of this buffer?? */
3112 RB_WARN_ON(cpu_buffer
, 1);
3116 * ring_buffer_commit_discard - discard an event that has not been committed
3117 * @buffer: the ring buffer
3118 * @event: non committed event to discard
3120 * Sometimes an event that is in the ring buffer needs to be ignored.
3121 * This function lets the user discard an event in the ring buffer
3122 * and then that event will not be read later.
3124 * This function only works if it is called before the item has been
3125 * committed. It will try to free the event from the ring buffer
3126 * if another event has not been added behind it.
3128 * If another event has been added behind it, it will set the event
3129 * up as discarded, and perform the commit.
3131 * If this function is called, do not call ring_buffer_unlock_commit on
3134 void ring_buffer_discard_commit(struct trace_buffer
*buffer
,
3135 struct ring_buffer_event
*event
)
3137 struct ring_buffer_per_cpu
*cpu_buffer
;
3140 /* The event is discarded regardless */
3141 rb_event_discard(event
);
3143 cpu
= smp_processor_id();
3144 cpu_buffer
= buffer
->buffers
[cpu
];
3147 * This must only be called if the event has not been
3148 * committed yet. Thus we can assume that preemption
3149 * is still disabled.
3151 RB_WARN_ON(buffer
, !local_read(&cpu_buffer
->committing
));
3153 rb_decrement_entry(cpu_buffer
, event
);
3154 if (rb_try_to_discard(cpu_buffer
, event
))
3158 * The commit is still visible by the reader, so we
3159 * must still update the timestamp.
3161 rb_update_write_stamp(cpu_buffer
, event
);
3163 rb_end_commit(cpu_buffer
);
3165 trace_recursive_unlock(cpu_buffer
);
3167 preempt_enable_notrace();
3170 EXPORT_SYMBOL_GPL(ring_buffer_discard_commit
);
3173 * ring_buffer_write - write data to the buffer without reserving
3174 * @buffer: The ring buffer to write to.
3175 * @length: The length of the data being written (excluding the event header)
3176 * @data: The data to write to the buffer.
3178 * This is like ring_buffer_lock_reserve and ring_buffer_unlock_commit as
3179 * one function. If you already have the data to write to the buffer, it
3180 * may be easier to simply call this function.
3182 * Note, like ring_buffer_lock_reserve, the length is the length of the data
3183 * and not the length of the event which would hold the header.
3185 int ring_buffer_write(struct trace_buffer
*buffer
,
3186 unsigned long length
,
3189 struct ring_buffer_per_cpu
*cpu_buffer
;
3190 struct ring_buffer_event
*event
;
3195 preempt_disable_notrace();
3197 if (atomic_read(&buffer
->record_disabled
))
3200 cpu
= raw_smp_processor_id();
3202 if (!cpumask_test_cpu(cpu
, buffer
->cpumask
))
3205 cpu_buffer
= buffer
->buffers
[cpu
];
3207 if (atomic_read(&cpu_buffer
->record_disabled
))
3210 if (length
> BUF_MAX_DATA_SIZE
)
3213 if (unlikely(trace_recursive_lock(cpu_buffer
)))
3216 event
= rb_reserve_next_event(buffer
, cpu_buffer
, length
);
3220 body
= rb_event_data(event
);
3222 memcpy(body
, data
, length
);
3224 rb_commit(cpu_buffer
, event
);
3226 rb_wakeups(buffer
, cpu_buffer
);
3231 trace_recursive_unlock(cpu_buffer
);
3234 preempt_enable_notrace();
3238 EXPORT_SYMBOL_GPL(ring_buffer_write
);
3240 static bool rb_per_cpu_empty(struct ring_buffer_per_cpu
*cpu_buffer
)
3242 struct buffer_page
*reader
= cpu_buffer
->reader_page
;
3243 struct buffer_page
*head
= rb_set_head_page(cpu_buffer
);
3244 struct buffer_page
*commit
= cpu_buffer
->commit_page
;
3246 /* In case of error, head will be NULL */
3247 if (unlikely(!head
))
3250 return reader
->read
== rb_page_commit(reader
) &&
3251 (commit
== reader
||
3253 head
->read
== rb_page_commit(commit
)));
3257 * ring_buffer_record_disable - stop all writes into the buffer
3258 * @buffer: The ring buffer to stop writes to.
3260 * This prevents all writes to the buffer. Any attempt to write
3261 * to the buffer after this will fail and return NULL.
3263 * The caller should call synchronize_rcu() after this.
3265 void ring_buffer_record_disable(struct trace_buffer
*buffer
)
3267 atomic_inc(&buffer
->record_disabled
);
3269 EXPORT_SYMBOL_GPL(ring_buffer_record_disable
);
3272 * ring_buffer_record_enable - enable writes to the buffer
3273 * @buffer: The ring buffer to enable writes
3275 * Note, multiple disables will need the same number of enables
3276 * to truly enable the writing (much like preempt_disable).
3278 void ring_buffer_record_enable(struct trace_buffer
*buffer
)
3280 atomic_dec(&buffer
->record_disabled
);
3282 EXPORT_SYMBOL_GPL(ring_buffer_record_enable
);
3285 * ring_buffer_record_off - stop all writes into the buffer
3286 * @buffer: The ring buffer to stop writes to.
3288 * This prevents all writes to the buffer. Any attempt to write
3289 * to the buffer after this will fail and return NULL.
3291 * This is different than ring_buffer_record_disable() as
3292 * it works like an on/off switch, where as the disable() version
3293 * must be paired with a enable().
3295 void ring_buffer_record_off(struct trace_buffer
*buffer
)
3298 unsigned int new_rd
;
3301 rd
= atomic_read(&buffer
->record_disabled
);
3302 new_rd
= rd
| RB_BUFFER_OFF
;
3303 } while (atomic_cmpxchg(&buffer
->record_disabled
, rd
, new_rd
) != rd
);
3305 EXPORT_SYMBOL_GPL(ring_buffer_record_off
);
3308 * ring_buffer_record_on - restart writes into the buffer
3309 * @buffer: The ring buffer to start writes to.
3311 * This enables all writes to the buffer that was disabled by
3312 * ring_buffer_record_off().
3314 * This is different than ring_buffer_record_enable() as
3315 * it works like an on/off switch, where as the enable() version
3316 * must be paired with a disable().
3318 void ring_buffer_record_on(struct trace_buffer
*buffer
)
3321 unsigned int new_rd
;
3324 rd
= atomic_read(&buffer
->record_disabled
);
3325 new_rd
= rd
& ~RB_BUFFER_OFF
;
3326 } while (atomic_cmpxchg(&buffer
->record_disabled
, rd
, new_rd
) != rd
);
3328 EXPORT_SYMBOL_GPL(ring_buffer_record_on
);
3331 * ring_buffer_record_is_on - return true if the ring buffer can write
3332 * @buffer: The ring buffer to see if write is enabled
3334 * Returns true if the ring buffer is in a state that it accepts writes.
3336 bool ring_buffer_record_is_on(struct trace_buffer
*buffer
)
3338 return !atomic_read(&buffer
->record_disabled
);
3342 * ring_buffer_record_is_set_on - return true if the ring buffer is set writable
3343 * @buffer: The ring buffer to see if write is set enabled
3345 * Returns true if the ring buffer is set writable by ring_buffer_record_on().
3346 * Note that this does NOT mean it is in a writable state.
3348 * It may return true when the ring buffer has been disabled by
3349 * ring_buffer_record_disable(), as that is a temporary disabling of
3352 bool ring_buffer_record_is_set_on(struct trace_buffer
*buffer
)
3354 return !(atomic_read(&buffer
->record_disabled
) & RB_BUFFER_OFF
);
3358 * ring_buffer_record_disable_cpu - stop all writes into the cpu_buffer
3359 * @buffer: The ring buffer to stop writes to.
3360 * @cpu: The CPU buffer to stop
3362 * This prevents all writes to the buffer. Any attempt to write
3363 * to the buffer after this will fail and return NULL.
3365 * The caller should call synchronize_rcu() after this.
3367 void ring_buffer_record_disable_cpu(struct trace_buffer
*buffer
, int cpu
)
3369 struct ring_buffer_per_cpu
*cpu_buffer
;
3371 if (!cpumask_test_cpu(cpu
, buffer
->cpumask
))
3374 cpu_buffer
= buffer
->buffers
[cpu
];
3375 atomic_inc(&cpu_buffer
->record_disabled
);
3377 EXPORT_SYMBOL_GPL(ring_buffer_record_disable_cpu
);
3380 * ring_buffer_record_enable_cpu - enable writes to the buffer
3381 * @buffer: The ring buffer to enable writes
3382 * @cpu: The CPU to enable.
3384 * Note, multiple disables will need the same number of enables
3385 * to truly enable the writing (much like preempt_disable).
3387 void ring_buffer_record_enable_cpu(struct trace_buffer
*buffer
, int cpu
)
3389 struct ring_buffer_per_cpu
*cpu_buffer
;
3391 if (!cpumask_test_cpu(cpu
, buffer
->cpumask
))
3394 cpu_buffer
= buffer
->buffers
[cpu
];
3395 atomic_dec(&cpu_buffer
->record_disabled
);
3397 EXPORT_SYMBOL_GPL(ring_buffer_record_enable_cpu
);
3400 * The total entries in the ring buffer is the running counter
3401 * of entries entered into the ring buffer, minus the sum of
3402 * the entries read from the ring buffer and the number of
3403 * entries that were overwritten.
3405 static inline unsigned long
3406 rb_num_of_entries(struct ring_buffer_per_cpu
*cpu_buffer
)
3408 return local_read(&cpu_buffer
->entries
) -
3409 (local_read(&cpu_buffer
->overrun
) + cpu_buffer
->read
);
3413 * ring_buffer_oldest_event_ts - get the oldest event timestamp from the buffer
3414 * @buffer: The ring buffer
3415 * @cpu: The per CPU buffer to read from.
3417 u64
ring_buffer_oldest_event_ts(struct trace_buffer
*buffer
, int cpu
)
3419 unsigned long flags
;
3420 struct ring_buffer_per_cpu
*cpu_buffer
;
3421 struct buffer_page
*bpage
;
3424 if (!cpumask_test_cpu(cpu
, buffer
->cpumask
))
3427 cpu_buffer
= buffer
->buffers
[cpu
];
3428 raw_spin_lock_irqsave(&cpu_buffer
->reader_lock
, flags
);
3430 * if the tail is on reader_page, oldest time stamp is on the reader
3433 if (cpu_buffer
->tail_page
== cpu_buffer
->reader_page
)
3434 bpage
= cpu_buffer
->reader_page
;
3436 bpage
= rb_set_head_page(cpu_buffer
);
3438 ret
= bpage
->page
->time_stamp
;
3439 raw_spin_unlock_irqrestore(&cpu_buffer
->reader_lock
, flags
);
3443 EXPORT_SYMBOL_GPL(ring_buffer_oldest_event_ts
);
3446 * ring_buffer_bytes_cpu - get the number of bytes consumed in a cpu buffer
3447 * @buffer: The ring buffer
3448 * @cpu: The per CPU buffer to read from.
3450 unsigned long ring_buffer_bytes_cpu(struct trace_buffer
*buffer
, int cpu
)
3452 struct ring_buffer_per_cpu
*cpu_buffer
;
3455 if (!cpumask_test_cpu(cpu
, buffer
->cpumask
))
3458 cpu_buffer
= buffer
->buffers
[cpu
];
3459 ret
= local_read(&cpu_buffer
->entries_bytes
) - cpu_buffer
->read_bytes
;
3463 EXPORT_SYMBOL_GPL(ring_buffer_bytes_cpu
);
3466 * ring_buffer_entries_cpu - get the number of entries in a cpu buffer
3467 * @buffer: The ring buffer
3468 * @cpu: The per CPU buffer to get the entries from.
3470 unsigned long ring_buffer_entries_cpu(struct trace_buffer
*buffer
, int cpu
)
3472 struct ring_buffer_per_cpu
*cpu_buffer
;
3474 if (!cpumask_test_cpu(cpu
, buffer
->cpumask
))
3477 cpu_buffer
= buffer
->buffers
[cpu
];
3479 return rb_num_of_entries(cpu_buffer
);
3481 EXPORT_SYMBOL_GPL(ring_buffer_entries_cpu
);
3484 * ring_buffer_overrun_cpu - get the number of overruns caused by the ring
3485 * buffer wrapping around (only if RB_FL_OVERWRITE is on).
3486 * @buffer: The ring buffer
3487 * @cpu: The per CPU buffer to get the number of overruns from
3489 unsigned long ring_buffer_overrun_cpu(struct trace_buffer
*buffer
, int cpu
)
3491 struct ring_buffer_per_cpu
*cpu_buffer
;
3494 if (!cpumask_test_cpu(cpu
, buffer
->cpumask
))
3497 cpu_buffer
= buffer
->buffers
[cpu
];
3498 ret
= local_read(&cpu_buffer
->overrun
);
3502 EXPORT_SYMBOL_GPL(ring_buffer_overrun_cpu
);
3505 * ring_buffer_commit_overrun_cpu - get the number of overruns caused by
3506 * commits failing due to the buffer wrapping around while there are uncommitted
3507 * events, such as during an interrupt storm.
3508 * @buffer: The ring buffer
3509 * @cpu: The per CPU buffer to get the number of overruns from
3512 ring_buffer_commit_overrun_cpu(struct trace_buffer
*buffer
, int cpu
)
3514 struct ring_buffer_per_cpu
*cpu_buffer
;
3517 if (!cpumask_test_cpu(cpu
, buffer
->cpumask
))
3520 cpu_buffer
= buffer
->buffers
[cpu
];
3521 ret
= local_read(&cpu_buffer
->commit_overrun
);
3525 EXPORT_SYMBOL_GPL(ring_buffer_commit_overrun_cpu
);
3528 * ring_buffer_dropped_events_cpu - get the number of dropped events caused by
3529 * the ring buffer filling up (only if RB_FL_OVERWRITE is off).
3530 * @buffer: The ring buffer
3531 * @cpu: The per CPU buffer to get the number of overruns from
3534 ring_buffer_dropped_events_cpu(struct trace_buffer
*buffer
, int cpu
)
3536 struct ring_buffer_per_cpu
*cpu_buffer
;
3539 if (!cpumask_test_cpu(cpu
, buffer
->cpumask
))
3542 cpu_buffer
= buffer
->buffers
[cpu
];
3543 ret
= local_read(&cpu_buffer
->dropped_events
);
3547 EXPORT_SYMBOL_GPL(ring_buffer_dropped_events_cpu
);
3550 * ring_buffer_read_events_cpu - get the number of events successfully read
3551 * @buffer: The ring buffer
3552 * @cpu: The per CPU buffer to get the number of events read
3555 ring_buffer_read_events_cpu(struct trace_buffer
*buffer
, int cpu
)
3557 struct ring_buffer_per_cpu
*cpu_buffer
;
3559 if (!cpumask_test_cpu(cpu
, buffer
->cpumask
))
3562 cpu_buffer
= buffer
->buffers
[cpu
];
3563 return cpu_buffer
->read
;
3565 EXPORT_SYMBOL_GPL(ring_buffer_read_events_cpu
);
3568 * ring_buffer_entries - get the number of entries in a buffer
3569 * @buffer: The ring buffer
3571 * Returns the total number of entries in the ring buffer
3574 unsigned long ring_buffer_entries(struct trace_buffer
*buffer
)
3576 struct ring_buffer_per_cpu
*cpu_buffer
;
3577 unsigned long entries
= 0;
3580 /* if you care about this being correct, lock the buffer */
3581 for_each_buffer_cpu(buffer
, cpu
) {
3582 cpu_buffer
= buffer
->buffers
[cpu
];
3583 entries
+= rb_num_of_entries(cpu_buffer
);
3588 EXPORT_SYMBOL_GPL(ring_buffer_entries
);
3591 * ring_buffer_overruns - get the number of overruns in buffer
3592 * @buffer: The ring buffer
3594 * Returns the total number of overruns in the ring buffer
3597 unsigned long ring_buffer_overruns(struct trace_buffer
*buffer
)
3599 struct ring_buffer_per_cpu
*cpu_buffer
;
3600 unsigned long overruns
= 0;
3603 /* if you care about this being correct, lock the buffer */
3604 for_each_buffer_cpu(buffer
, cpu
) {
3605 cpu_buffer
= buffer
->buffers
[cpu
];
3606 overruns
+= local_read(&cpu_buffer
->overrun
);
3611 EXPORT_SYMBOL_GPL(ring_buffer_overruns
);
3613 static void rb_iter_reset(struct ring_buffer_iter
*iter
)
3615 struct ring_buffer_per_cpu
*cpu_buffer
= iter
->cpu_buffer
;
3617 /* Iterator usage is expected to have record disabled */
3618 iter
->head_page
= cpu_buffer
->reader_page
;
3619 iter
->head
= cpu_buffer
->reader_page
->read
;
3620 iter
->next_event
= iter
->head
;
3622 iter
->cache_reader_page
= iter
->head_page
;
3623 iter
->cache_read
= cpu_buffer
->read
;
3626 iter
->read_stamp
= cpu_buffer
->read_stamp
;
3627 iter
->page_stamp
= cpu_buffer
->reader_page
->page
->time_stamp
;
3629 iter
->read_stamp
= iter
->head_page
->page
->time_stamp
;
3630 iter
->page_stamp
= iter
->read_stamp
;
3635 * ring_buffer_iter_reset - reset an iterator
3636 * @iter: The iterator to reset
3638 * Resets the iterator, so that it will start from the beginning
3641 void ring_buffer_iter_reset(struct ring_buffer_iter
*iter
)
3643 struct ring_buffer_per_cpu
*cpu_buffer
;
3644 unsigned long flags
;
3649 cpu_buffer
= iter
->cpu_buffer
;
3651 raw_spin_lock_irqsave(&cpu_buffer
->reader_lock
, flags
);
3652 rb_iter_reset(iter
);
3653 raw_spin_unlock_irqrestore(&cpu_buffer
->reader_lock
, flags
);
3655 EXPORT_SYMBOL_GPL(ring_buffer_iter_reset
);
3658 * ring_buffer_iter_empty - check if an iterator has no more to read
3659 * @iter: The iterator to check
3661 int ring_buffer_iter_empty(struct ring_buffer_iter
*iter
)
3663 struct ring_buffer_per_cpu
*cpu_buffer
;
3664 struct buffer_page
*reader
;
3665 struct buffer_page
*head_page
;
3666 struct buffer_page
*commit_page
;
3667 struct buffer_page
*curr_commit_page
;
3672 cpu_buffer
= iter
->cpu_buffer
;
3673 reader
= cpu_buffer
->reader_page
;
3674 head_page
= cpu_buffer
->head_page
;
3675 commit_page
= cpu_buffer
->commit_page
;
3676 commit_ts
= commit_page
->page
->time_stamp
;
3679 * When the writer goes across pages, it issues a cmpxchg which
3680 * is a mb(), which will synchronize with the rmb here.
3681 * (see rb_tail_page_update())
3684 commit
= rb_page_commit(commit_page
);
3685 /* We want to make sure that the commit page doesn't change */
3688 /* Make sure commit page didn't change */
3689 curr_commit_page
= READ_ONCE(cpu_buffer
->commit_page
);
3690 curr_commit_ts
= READ_ONCE(curr_commit_page
->page
->time_stamp
);
3692 /* If the commit page changed, then there's more data */
3693 if (curr_commit_page
!= commit_page
||
3694 curr_commit_ts
!= commit_ts
)
3697 /* Still racy, as it may return a false positive, but that's OK */
3698 return ((iter
->head_page
== commit_page
&& iter
->head
>= commit
) ||
3699 (iter
->head_page
== reader
&& commit_page
== head_page
&&
3700 head_page
->read
== commit
&&
3701 iter
->head
== rb_page_commit(cpu_buffer
->reader_page
)));
3703 EXPORT_SYMBOL_GPL(ring_buffer_iter_empty
);
3706 rb_update_read_stamp(struct ring_buffer_per_cpu
*cpu_buffer
,
3707 struct ring_buffer_event
*event
)
3711 switch (event
->type_len
) {
3712 case RINGBUF_TYPE_PADDING
:
3715 case RINGBUF_TYPE_TIME_EXTEND
:
3716 delta
= ring_buffer_event_time_stamp(event
);
3717 cpu_buffer
->read_stamp
+= delta
;
3720 case RINGBUF_TYPE_TIME_STAMP
:
3721 delta
= ring_buffer_event_time_stamp(event
);
3722 cpu_buffer
->read_stamp
= delta
;
3725 case RINGBUF_TYPE_DATA
:
3726 cpu_buffer
->read_stamp
+= event
->time_delta
;
3730 RB_WARN_ON(cpu_buffer
, 1);
3736 rb_update_iter_read_stamp(struct ring_buffer_iter
*iter
,
3737 struct ring_buffer_event
*event
)
3741 switch (event
->type_len
) {
3742 case RINGBUF_TYPE_PADDING
:
3745 case RINGBUF_TYPE_TIME_EXTEND
:
3746 delta
= ring_buffer_event_time_stamp(event
);
3747 iter
->read_stamp
+= delta
;
3750 case RINGBUF_TYPE_TIME_STAMP
:
3751 delta
= ring_buffer_event_time_stamp(event
);
3752 iter
->read_stamp
= delta
;
3755 case RINGBUF_TYPE_DATA
:
3756 iter
->read_stamp
+= event
->time_delta
;
3760 RB_WARN_ON(iter
->cpu_buffer
, 1);
3765 static struct buffer_page
*
3766 rb_get_reader_page(struct ring_buffer_per_cpu
*cpu_buffer
)
3768 struct buffer_page
*reader
= NULL
;
3769 unsigned long overwrite
;
3770 unsigned long flags
;
3774 local_irq_save(flags
);
3775 arch_spin_lock(&cpu_buffer
->lock
);
3779 * This should normally only loop twice. But because the
3780 * start of the reader inserts an empty page, it causes
3781 * a case where we will loop three times. There should be no
3782 * reason to loop four times (that I know of).
3784 if (RB_WARN_ON(cpu_buffer
, ++nr_loops
> 3)) {
3789 reader
= cpu_buffer
->reader_page
;
3791 /* If there's more to read, return this page */
3792 if (cpu_buffer
->reader_page
->read
< rb_page_size(reader
))
3795 /* Never should we have an index greater than the size */
3796 if (RB_WARN_ON(cpu_buffer
,
3797 cpu_buffer
->reader_page
->read
> rb_page_size(reader
)))
3800 /* check if we caught up to the tail */
3802 if (cpu_buffer
->commit_page
== cpu_buffer
->reader_page
)
3805 /* Don't bother swapping if the ring buffer is empty */
3806 if (rb_num_of_entries(cpu_buffer
) == 0)
3810 * Reset the reader page to size zero.
3812 local_set(&cpu_buffer
->reader_page
->write
, 0);
3813 local_set(&cpu_buffer
->reader_page
->entries
, 0);
3814 local_set(&cpu_buffer
->reader_page
->page
->commit
, 0);
3815 cpu_buffer
->reader_page
->real_end
= 0;
3819 * Splice the empty reader page into the list around the head.
3821 reader
= rb_set_head_page(cpu_buffer
);
3824 cpu_buffer
->reader_page
->list
.next
= rb_list_head(reader
->list
.next
);
3825 cpu_buffer
->reader_page
->list
.prev
= reader
->list
.prev
;
3828 * cpu_buffer->pages just needs to point to the buffer, it
3829 * has no specific buffer page to point to. Lets move it out
3830 * of our way so we don't accidentally swap it.
3832 cpu_buffer
->pages
= reader
->list
.prev
;
3834 /* The reader page will be pointing to the new head */
3835 rb_set_list_to_head(cpu_buffer
, &cpu_buffer
->reader_page
->list
);
3838 * We want to make sure we read the overruns after we set up our
3839 * pointers to the next object. The writer side does a
3840 * cmpxchg to cross pages which acts as the mb on the writer
3841 * side. Note, the reader will constantly fail the swap
3842 * while the writer is updating the pointers, so this
3843 * guarantees that the overwrite recorded here is the one we
3844 * want to compare with the last_overrun.
3847 overwrite
= local_read(&(cpu_buffer
->overrun
));
3850 * Here's the tricky part.
3852 * We need to move the pointer past the header page.
3853 * But we can only do that if a writer is not currently
3854 * moving it. The page before the header page has the
3855 * flag bit '1' set if it is pointing to the page we want.
3856 * but if the writer is in the process of moving it
3857 * than it will be '2' or already moved '0'.
3860 ret
= rb_head_page_replace(reader
, cpu_buffer
->reader_page
);
3863 * If we did not convert it, then we must try again.
3869 * Yay! We succeeded in replacing the page.
3871 * Now make the new head point back to the reader page.
3873 rb_list_head(reader
->list
.next
)->prev
= &cpu_buffer
->reader_page
->list
;
3874 rb_inc_page(cpu_buffer
, &cpu_buffer
->head_page
);
3876 local_inc(&cpu_buffer
->pages_read
);
3878 /* Finally update the reader page to the new head */
3879 cpu_buffer
->reader_page
= reader
;
3880 cpu_buffer
->reader_page
->read
= 0;
3882 if (overwrite
!= cpu_buffer
->last_overrun
) {
3883 cpu_buffer
->lost_events
= overwrite
- cpu_buffer
->last_overrun
;
3884 cpu_buffer
->last_overrun
= overwrite
;
3890 /* Update the read_stamp on the first event */
3891 if (reader
&& reader
->read
== 0)
3892 cpu_buffer
->read_stamp
= reader
->page
->time_stamp
;
3894 arch_spin_unlock(&cpu_buffer
->lock
);
3895 local_irq_restore(flags
);
3900 static void rb_advance_reader(struct ring_buffer_per_cpu
*cpu_buffer
)
3902 struct ring_buffer_event
*event
;
3903 struct buffer_page
*reader
;
3906 reader
= rb_get_reader_page(cpu_buffer
);
3908 /* This function should not be called when buffer is empty */
3909 if (RB_WARN_ON(cpu_buffer
, !reader
))
3912 event
= rb_reader_event(cpu_buffer
);
3914 if (event
->type_len
<= RINGBUF_TYPE_DATA_TYPE_LEN_MAX
)
3917 rb_update_read_stamp(cpu_buffer
, event
);
3919 length
= rb_event_length(event
);
3920 cpu_buffer
->reader_page
->read
+= length
;
3923 static void rb_advance_iter(struct ring_buffer_iter
*iter
)
3925 struct ring_buffer_per_cpu
*cpu_buffer
;
3927 cpu_buffer
= iter
->cpu_buffer
;
3929 /* If head == next_event then we need to jump to the next event */
3930 if (iter
->head
== iter
->next_event
) {
3931 /* If the event gets overwritten again, there's nothing to do */
3932 if (rb_iter_head_event(iter
) == NULL
)
3936 iter
->head
= iter
->next_event
;
3939 * Check if we are at the end of the buffer.
3941 if (iter
->next_event
>= rb_page_size(iter
->head_page
)) {
3942 /* discarded commits can make the page empty */
3943 if (iter
->head_page
== cpu_buffer
->commit_page
)
3949 rb_update_iter_read_stamp(iter
, iter
->event
);
3952 static int rb_lost_events(struct ring_buffer_per_cpu
*cpu_buffer
)
3954 return cpu_buffer
->lost_events
;
3957 static struct ring_buffer_event
*
3958 rb_buffer_peek(struct ring_buffer_per_cpu
*cpu_buffer
, u64
*ts
,
3959 unsigned long *lost_events
)
3961 struct ring_buffer_event
*event
;
3962 struct buffer_page
*reader
;
3969 * We repeat when a time extend is encountered.
3970 * Since the time extend is always attached to a data event,
3971 * we should never loop more than once.
3972 * (We never hit the following condition more than twice).
3974 if (RB_WARN_ON(cpu_buffer
, ++nr_loops
> 2))
3977 reader
= rb_get_reader_page(cpu_buffer
);
3981 event
= rb_reader_event(cpu_buffer
);
3983 switch (event
->type_len
) {
3984 case RINGBUF_TYPE_PADDING
:
3985 if (rb_null_event(event
))
3986 RB_WARN_ON(cpu_buffer
, 1);
3988 * Because the writer could be discarding every
3989 * event it creates (which would probably be bad)
3990 * if we were to go back to "again" then we may never
3991 * catch up, and will trigger the warn on, or lock
3992 * the box. Return the padding, and we will release
3993 * the current locks, and try again.
3997 case RINGBUF_TYPE_TIME_EXTEND
:
3998 /* Internal data, OK to advance */
3999 rb_advance_reader(cpu_buffer
);
4002 case RINGBUF_TYPE_TIME_STAMP
:
4004 *ts
= ring_buffer_event_time_stamp(event
);
4005 ring_buffer_normalize_time_stamp(cpu_buffer
->buffer
,
4006 cpu_buffer
->cpu
, ts
);
4008 /* Internal data, OK to advance */
4009 rb_advance_reader(cpu_buffer
);
4012 case RINGBUF_TYPE_DATA
:
4014 *ts
= cpu_buffer
->read_stamp
+ event
->time_delta
;
4015 ring_buffer_normalize_time_stamp(cpu_buffer
->buffer
,
4016 cpu_buffer
->cpu
, ts
);
4019 *lost_events
= rb_lost_events(cpu_buffer
);
4023 RB_WARN_ON(cpu_buffer
, 1);
4028 EXPORT_SYMBOL_GPL(ring_buffer_peek
);
4030 static struct ring_buffer_event
*
4031 rb_iter_peek(struct ring_buffer_iter
*iter
, u64
*ts
)
4033 struct trace_buffer
*buffer
;
4034 struct ring_buffer_per_cpu
*cpu_buffer
;
4035 struct ring_buffer_event
*event
;
4041 cpu_buffer
= iter
->cpu_buffer
;
4042 buffer
= cpu_buffer
->buffer
;
4045 * Check if someone performed a consuming read to
4046 * the buffer. A consuming read invalidates the iterator
4047 * and we need to reset the iterator in this case.
4049 if (unlikely(iter
->cache_read
!= cpu_buffer
->read
||
4050 iter
->cache_reader_page
!= cpu_buffer
->reader_page
))
4051 rb_iter_reset(iter
);
4054 if (ring_buffer_iter_empty(iter
))
4058 * As the writer can mess with what the iterator is trying
4059 * to read, just give up if we fail to get an event after
4060 * three tries. The iterator is not as reliable when reading
4061 * the ring buffer with an active write as the consumer is.
4062 * Do not warn if the three failures is reached.
4067 if (rb_per_cpu_empty(cpu_buffer
))
4070 if (iter
->head
>= rb_page_size(iter
->head_page
)) {
4075 event
= rb_iter_head_event(iter
);
4079 switch (event
->type_len
) {
4080 case RINGBUF_TYPE_PADDING
:
4081 if (rb_null_event(event
)) {
4085 rb_advance_iter(iter
);
4088 case RINGBUF_TYPE_TIME_EXTEND
:
4089 /* Internal data, OK to advance */
4090 rb_advance_iter(iter
);
4093 case RINGBUF_TYPE_TIME_STAMP
:
4095 *ts
= ring_buffer_event_time_stamp(event
);
4096 ring_buffer_normalize_time_stamp(cpu_buffer
->buffer
,
4097 cpu_buffer
->cpu
, ts
);
4099 /* Internal data, OK to advance */
4100 rb_advance_iter(iter
);
4103 case RINGBUF_TYPE_DATA
:
4105 *ts
= iter
->read_stamp
+ event
->time_delta
;
4106 ring_buffer_normalize_time_stamp(buffer
,
4107 cpu_buffer
->cpu
, ts
);
4112 RB_WARN_ON(cpu_buffer
, 1);
4117 EXPORT_SYMBOL_GPL(ring_buffer_iter_peek
);
4119 static inline bool rb_reader_lock(struct ring_buffer_per_cpu
*cpu_buffer
)
4121 if (likely(!in_nmi())) {
4122 raw_spin_lock(&cpu_buffer
->reader_lock
);
4127 * If an NMI die dumps out the content of the ring buffer
4128 * trylock must be used to prevent a deadlock if the NMI
4129 * preempted a task that holds the ring buffer locks. If
4130 * we get the lock then all is fine, if not, then continue
4131 * to do the read, but this can corrupt the ring buffer,
4132 * so it must be permanently disabled from future writes.
4133 * Reading from NMI is a oneshot deal.
4135 if (raw_spin_trylock(&cpu_buffer
->reader_lock
))
4138 /* Continue without locking, but disable the ring buffer */
4139 atomic_inc(&cpu_buffer
->record_disabled
);
4144 rb_reader_unlock(struct ring_buffer_per_cpu
*cpu_buffer
, bool locked
)
4147 raw_spin_unlock(&cpu_buffer
->reader_lock
);
4152 * ring_buffer_peek - peek at the next event to be read
4153 * @buffer: The ring buffer to read
4154 * @cpu: The cpu to peak at
4155 * @ts: The timestamp counter of this event.
4156 * @lost_events: a variable to store if events were lost (may be NULL)
4158 * This will return the event that will be read next, but does
4159 * not consume the data.
4161 struct ring_buffer_event
*
4162 ring_buffer_peek(struct trace_buffer
*buffer
, int cpu
, u64
*ts
,
4163 unsigned long *lost_events
)
4165 struct ring_buffer_per_cpu
*cpu_buffer
= buffer
->buffers
[cpu
];
4166 struct ring_buffer_event
*event
;
4167 unsigned long flags
;
4170 if (!cpumask_test_cpu(cpu
, buffer
->cpumask
))
4174 local_irq_save(flags
);
4175 dolock
= rb_reader_lock(cpu_buffer
);
4176 event
= rb_buffer_peek(cpu_buffer
, ts
, lost_events
);
4177 if (event
&& event
->type_len
== RINGBUF_TYPE_PADDING
)
4178 rb_advance_reader(cpu_buffer
);
4179 rb_reader_unlock(cpu_buffer
, dolock
);
4180 local_irq_restore(flags
);
4182 if (event
&& event
->type_len
== RINGBUF_TYPE_PADDING
)
4188 /** ring_buffer_iter_dropped - report if there are dropped events
4189 * @iter: The ring buffer iterator
4191 * Returns true if there was dropped events since the last peek.
4193 bool ring_buffer_iter_dropped(struct ring_buffer_iter
*iter
)
4195 bool ret
= iter
->missed_events
!= 0;
4197 iter
->missed_events
= 0;
4200 EXPORT_SYMBOL_GPL(ring_buffer_iter_dropped
);
4203 * ring_buffer_iter_peek - peek at the next event to be read
4204 * @iter: The ring buffer iterator
4205 * @ts: The timestamp counter of this event.
4207 * This will return the event that will be read next, but does
4208 * not increment the iterator.
4210 struct ring_buffer_event
*
4211 ring_buffer_iter_peek(struct ring_buffer_iter
*iter
, u64
*ts
)
4213 struct ring_buffer_per_cpu
*cpu_buffer
= iter
->cpu_buffer
;
4214 struct ring_buffer_event
*event
;
4215 unsigned long flags
;
4218 raw_spin_lock_irqsave(&cpu_buffer
->reader_lock
, flags
);
4219 event
= rb_iter_peek(iter
, ts
);
4220 raw_spin_unlock_irqrestore(&cpu_buffer
->reader_lock
, flags
);
4222 if (event
&& event
->type_len
== RINGBUF_TYPE_PADDING
)
4229 * ring_buffer_consume - return an event and consume it
4230 * @buffer: The ring buffer to get the next event from
4231 * @cpu: the cpu to read the buffer from
4232 * @ts: a variable to store the timestamp (may be NULL)
4233 * @lost_events: a variable to store if events were lost (may be NULL)
4235 * Returns the next event in the ring buffer, and that event is consumed.
4236 * Meaning, that sequential reads will keep returning a different event,
4237 * and eventually empty the ring buffer if the producer is slower.
4239 struct ring_buffer_event
*
4240 ring_buffer_consume(struct trace_buffer
*buffer
, int cpu
, u64
*ts
,
4241 unsigned long *lost_events
)
4243 struct ring_buffer_per_cpu
*cpu_buffer
;
4244 struct ring_buffer_event
*event
= NULL
;
4245 unsigned long flags
;
4249 /* might be called in atomic */
4252 if (!cpumask_test_cpu(cpu
, buffer
->cpumask
))
4255 cpu_buffer
= buffer
->buffers
[cpu
];
4256 local_irq_save(flags
);
4257 dolock
= rb_reader_lock(cpu_buffer
);
4259 event
= rb_buffer_peek(cpu_buffer
, ts
, lost_events
);
4261 cpu_buffer
->lost_events
= 0;
4262 rb_advance_reader(cpu_buffer
);
4265 rb_reader_unlock(cpu_buffer
, dolock
);
4266 local_irq_restore(flags
);
4271 if (event
&& event
->type_len
== RINGBUF_TYPE_PADDING
)
4276 EXPORT_SYMBOL_GPL(ring_buffer_consume
);
4279 * ring_buffer_read_prepare - Prepare for a non consuming read of the buffer
4280 * @buffer: The ring buffer to read from
4281 * @cpu: The cpu buffer to iterate over
4282 * @flags: gfp flags to use for memory allocation
4284 * This performs the initial preparations necessary to iterate
4285 * through the buffer. Memory is allocated, buffer recording
4286 * is disabled, and the iterator pointer is returned to the caller.
4288 * Disabling buffer recording prevents the reading from being
4289 * corrupted. This is not a consuming read, so a producer is not
4292 * After a sequence of ring_buffer_read_prepare calls, the user is
4293 * expected to make at least one call to ring_buffer_read_prepare_sync.
4294 * Afterwards, ring_buffer_read_start is invoked to get things going
4297 * This overall must be paired with ring_buffer_read_finish.
4299 struct ring_buffer_iter
*
4300 ring_buffer_read_prepare(struct trace_buffer
*buffer
, int cpu
, gfp_t flags
)
4302 struct ring_buffer_per_cpu
*cpu_buffer
;
4303 struct ring_buffer_iter
*iter
;
4305 if (!cpumask_test_cpu(cpu
, buffer
->cpumask
))
4308 iter
= kzalloc(sizeof(*iter
), flags
);
4312 iter
->event
= kmalloc(BUF_MAX_DATA_SIZE
, flags
);
4318 cpu_buffer
= buffer
->buffers
[cpu
];
4320 iter
->cpu_buffer
= cpu_buffer
;
4322 atomic_inc(&cpu_buffer
->resize_disabled
);
4326 EXPORT_SYMBOL_GPL(ring_buffer_read_prepare
);
4329 * ring_buffer_read_prepare_sync - Synchronize a set of prepare calls
4331 * All previously invoked ring_buffer_read_prepare calls to prepare
4332 * iterators will be synchronized. Afterwards, read_buffer_read_start
4333 * calls on those iterators are allowed.
4336 ring_buffer_read_prepare_sync(void)
4340 EXPORT_SYMBOL_GPL(ring_buffer_read_prepare_sync
);
4343 * ring_buffer_read_start - start a non consuming read of the buffer
4344 * @iter: The iterator returned by ring_buffer_read_prepare
4346 * This finalizes the startup of an iteration through the buffer.
4347 * The iterator comes from a call to ring_buffer_read_prepare and
4348 * an intervening ring_buffer_read_prepare_sync must have been
4351 * Must be paired with ring_buffer_read_finish.
4354 ring_buffer_read_start(struct ring_buffer_iter
*iter
)
4356 struct ring_buffer_per_cpu
*cpu_buffer
;
4357 unsigned long flags
;
4362 cpu_buffer
= iter
->cpu_buffer
;
4364 raw_spin_lock_irqsave(&cpu_buffer
->reader_lock
, flags
);
4365 arch_spin_lock(&cpu_buffer
->lock
);
4366 rb_iter_reset(iter
);
4367 arch_spin_unlock(&cpu_buffer
->lock
);
4368 raw_spin_unlock_irqrestore(&cpu_buffer
->reader_lock
, flags
);
4370 EXPORT_SYMBOL_GPL(ring_buffer_read_start
);
4373 * ring_buffer_read_finish - finish reading the iterator of the buffer
4374 * @iter: The iterator retrieved by ring_buffer_start
4376 * This re-enables the recording to the buffer, and frees the
4380 ring_buffer_read_finish(struct ring_buffer_iter
*iter
)
4382 struct ring_buffer_per_cpu
*cpu_buffer
= iter
->cpu_buffer
;
4383 unsigned long flags
;
4386 * Ring buffer is disabled from recording, here's a good place
4387 * to check the integrity of the ring buffer.
4388 * Must prevent readers from trying to read, as the check
4389 * clears the HEAD page and readers require it.
4391 raw_spin_lock_irqsave(&cpu_buffer
->reader_lock
, flags
);
4392 rb_check_pages(cpu_buffer
);
4393 raw_spin_unlock_irqrestore(&cpu_buffer
->reader_lock
, flags
);
4395 atomic_dec(&cpu_buffer
->resize_disabled
);
4399 EXPORT_SYMBOL_GPL(ring_buffer_read_finish
);
4402 * ring_buffer_iter_advance - advance the iterator to the next location
4403 * @iter: The ring buffer iterator
4405 * Move the location of the iterator such that the next read will
4406 * be the next location of the iterator.
4408 void ring_buffer_iter_advance(struct ring_buffer_iter
*iter
)
4410 struct ring_buffer_per_cpu
*cpu_buffer
= iter
->cpu_buffer
;
4411 unsigned long flags
;
4413 raw_spin_lock_irqsave(&cpu_buffer
->reader_lock
, flags
);
4415 rb_advance_iter(iter
);
4417 raw_spin_unlock_irqrestore(&cpu_buffer
->reader_lock
, flags
);
4419 EXPORT_SYMBOL_GPL(ring_buffer_iter_advance
);
4422 * ring_buffer_size - return the size of the ring buffer (in bytes)
4423 * @buffer: The ring buffer.
4424 * @cpu: The CPU to get ring buffer size from.
4426 unsigned long ring_buffer_size(struct trace_buffer
*buffer
, int cpu
)
4429 * Earlier, this method returned
4430 * BUF_PAGE_SIZE * buffer->nr_pages
4431 * Since the nr_pages field is now removed, we have converted this to
4432 * return the per cpu buffer value.
4434 if (!cpumask_test_cpu(cpu
, buffer
->cpumask
))
4437 return BUF_PAGE_SIZE
* buffer
->buffers
[cpu
]->nr_pages
;
4439 EXPORT_SYMBOL_GPL(ring_buffer_size
);
4442 rb_reset_cpu(struct ring_buffer_per_cpu
*cpu_buffer
)
4444 rb_head_page_deactivate(cpu_buffer
);
4446 cpu_buffer
->head_page
4447 = list_entry(cpu_buffer
->pages
, struct buffer_page
, list
);
4448 local_set(&cpu_buffer
->head_page
->write
, 0);
4449 local_set(&cpu_buffer
->head_page
->entries
, 0);
4450 local_set(&cpu_buffer
->head_page
->page
->commit
, 0);
4452 cpu_buffer
->head_page
->read
= 0;
4454 cpu_buffer
->tail_page
= cpu_buffer
->head_page
;
4455 cpu_buffer
->commit_page
= cpu_buffer
->head_page
;
4457 INIT_LIST_HEAD(&cpu_buffer
->reader_page
->list
);
4458 INIT_LIST_HEAD(&cpu_buffer
->new_pages
);
4459 local_set(&cpu_buffer
->reader_page
->write
, 0);
4460 local_set(&cpu_buffer
->reader_page
->entries
, 0);
4461 local_set(&cpu_buffer
->reader_page
->page
->commit
, 0);
4462 cpu_buffer
->reader_page
->read
= 0;
4464 local_set(&cpu_buffer
->entries_bytes
, 0);
4465 local_set(&cpu_buffer
->overrun
, 0);
4466 local_set(&cpu_buffer
->commit_overrun
, 0);
4467 local_set(&cpu_buffer
->dropped_events
, 0);
4468 local_set(&cpu_buffer
->entries
, 0);
4469 local_set(&cpu_buffer
->committing
, 0);
4470 local_set(&cpu_buffer
->commits
, 0);
4471 local_set(&cpu_buffer
->pages_touched
, 0);
4472 local_set(&cpu_buffer
->pages_read
, 0);
4473 cpu_buffer
->last_pages_touch
= 0;
4474 cpu_buffer
->shortest_full
= 0;
4475 cpu_buffer
->read
= 0;
4476 cpu_buffer
->read_bytes
= 0;
4478 cpu_buffer
->write_stamp
= 0;
4479 cpu_buffer
->read_stamp
= 0;
4481 cpu_buffer
->lost_events
= 0;
4482 cpu_buffer
->last_overrun
= 0;
4484 rb_head_page_activate(cpu_buffer
);
4488 * ring_buffer_reset_cpu - reset a ring buffer per CPU buffer
4489 * @buffer: The ring buffer to reset a per cpu buffer of
4490 * @cpu: The CPU buffer to be reset
4492 void ring_buffer_reset_cpu(struct trace_buffer
*buffer
, int cpu
)
4494 struct ring_buffer_per_cpu
*cpu_buffer
= buffer
->buffers
[cpu
];
4495 unsigned long flags
;
4497 if (!cpumask_test_cpu(cpu
, buffer
->cpumask
))
4500 atomic_inc(&cpu_buffer
->resize_disabled
);
4501 atomic_inc(&cpu_buffer
->record_disabled
);
4503 /* Make sure all commits have finished */
4506 raw_spin_lock_irqsave(&cpu_buffer
->reader_lock
, flags
);
4508 if (RB_WARN_ON(cpu_buffer
, local_read(&cpu_buffer
->committing
)))
4511 arch_spin_lock(&cpu_buffer
->lock
);
4513 rb_reset_cpu(cpu_buffer
);
4515 arch_spin_unlock(&cpu_buffer
->lock
);
4518 raw_spin_unlock_irqrestore(&cpu_buffer
->reader_lock
, flags
);
4520 atomic_dec(&cpu_buffer
->record_disabled
);
4521 atomic_dec(&cpu_buffer
->resize_disabled
);
4523 EXPORT_SYMBOL_GPL(ring_buffer_reset_cpu
);
4526 * ring_buffer_reset - reset a ring buffer
4527 * @buffer: The ring buffer to reset all cpu buffers
4529 void ring_buffer_reset(struct trace_buffer
*buffer
)
4533 for_each_buffer_cpu(buffer
, cpu
)
4534 ring_buffer_reset_cpu(buffer
, cpu
);
4536 EXPORT_SYMBOL_GPL(ring_buffer_reset
);
4539 * rind_buffer_empty - is the ring buffer empty?
4540 * @buffer: The ring buffer to test
4542 bool ring_buffer_empty(struct trace_buffer
*buffer
)
4544 struct ring_buffer_per_cpu
*cpu_buffer
;
4545 unsigned long flags
;
4550 /* yes this is racy, but if you don't like the race, lock the buffer */
4551 for_each_buffer_cpu(buffer
, cpu
) {
4552 cpu_buffer
= buffer
->buffers
[cpu
];
4553 local_irq_save(flags
);
4554 dolock
= rb_reader_lock(cpu_buffer
);
4555 ret
= rb_per_cpu_empty(cpu_buffer
);
4556 rb_reader_unlock(cpu_buffer
, dolock
);
4557 local_irq_restore(flags
);
4565 EXPORT_SYMBOL_GPL(ring_buffer_empty
);
4568 * ring_buffer_empty_cpu - is a cpu buffer of a ring buffer empty?
4569 * @buffer: The ring buffer
4570 * @cpu: The CPU buffer to test
4572 bool ring_buffer_empty_cpu(struct trace_buffer
*buffer
, int cpu
)
4574 struct ring_buffer_per_cpu
*cpu_buffer
;
4575 unsigned long flags
;
4579 if (!cpumask_test_cpu(cpu
, buffer
->cpumask
))
4582 cpu_buffer
= buffer
->buffers
[cpu
];
4583 local_irq_save(flags
);
4584 dolock
= rb_reader_lock(cpu_buffer
);
4585 ret
= rb_per_cpu_empty(cpu_buffer
);
4586 rb_reader_unlock(cpu_buffer
, dolock
);
4587 local_irq_restore(flags
);
4591 EXPORT_SYMBOL_GPL(ring_buffer_empty_cpu
);
4593 #ifdef CONFIG_RING_BUFFER_ALLOW_SWAP
4595 * ring_buffer_swap_cpu - swap a CPU buffer between two ring buffers
4596 * @buffer_a: One buffer to swap with
4597 * @buffer_b: The other buffer to swap with
4598 * @cpu: the CPU of the buffers to swap
4600 * This function is useful for tracers that want to take a "snapshot"
4601 * of a CPU buffer and has another back up buffer lying around.
4602 * it is expected that the tracer handles the cpu buffer not being
4603 * used at the moment.
4605 int ring_buffer_swap_cpu(struct trace_buffer
*buffer_a
,
4606 struct trace_buffer
*buffer_b
, int cpu
)
4608 struct ring_buffer_per_cpu
*cpu_buffer_a
;
4609 struct ring_buffer_per_cpu
*cpu_buffer_b
;
4612 if (!cpumask_test_cpu(cpu
, buffer_a
->cpumask
) ||
4613 !cpumask_test_cpu(cpu
, buffer_b
->cpumask
))
4616 cpu_buffer_a
= buffer_a
->buffers
[cpu
];
4617 cpu_buffer_b
= buffer_b
->buffers
[cpu
];
4619 /* At least make sure the two buffers are somewhat the same */
4620 if (cpu_buffer_a
->nr_pages
!= cpu_buffer_b
->nr_pages
)
4625 if (atomic_read(&buffer_a
->record_disabled
))
4628 if (atomic_read(&buffer_b
->record_disabled
))
4631 if (atomic_read(&cpu_buffer_a
->record_disabled
))
4634 if (atomic_read(&cpu_buffer_b
->record_disabled
))
4638 * We can't do a synchronize_rcu here because this
4639 * function can be called in atomic context.
4640 * Normally this will be called from the same CPU as cpu.
4641 * If not it's up to the caller to protect this.
4643 atomic_inc(&cpu_buffer_a
->record_disabled
);
4644 atomic_inc(&cpu_buffer_b
->record_disabled
);
4647 if (local_read(&cpu_buffer_a
->committing
))
4649 if (local_read(&cpu_buffer_b
->committing
))
4652 buffer_a
->buffers
[cpu
] = cpu_buffer_b
;
4653 buffer_b
->buffers
[cpu
] = cpu_buffer_a
;
4655 cpu_buffer_b
->buffer
= buffer_a
;
4656 cpu_buffer_a
->buffer
= buffer_b
;
4661 atomic_dec(&cpu_buffer_a
->record_disabled
);
4662 atomic_dec(&cpu_buffer_b
->record_disabled
);
4666 EXPORT_SYMBOL_GPL(ring_buffer_swap_cpu
);
4667 #endif /* CONFIG_RING_BUFFER_ALLOW_SWAP */
4670 * ring_buffer_alloc_read_page - allocate a page to read from buffer
4671 * @buffer: the buffer to allocate for.
4672 * @cpu: the cpu buffer to allocate.
4674 * This function is used in conjunction with ring_buffer_read_page.
4675 * When reading a full page from the ring buffer, these functions
4676 * can be used to speed up the process. The calling function should
4677 * allocate a few pages first with this function. Then when it
4678 * needs to get pages from the ring buffer, it passes the result
4679 * of this function into ring_buffer_read_page, which will swap
4680 * the page that was allocated, with the read page of the buffer.
4683 * The page allocated, or ERR_PTR
4685 void *ring_buffer_alloc_read_page(struct trace_buffer
*buffer
, int cpu
)
4687 struct ring_buffer_per_cpu
*cpu_buffer
;
4688 struct buffer_data_page
*bpage
= NULL
;
4689 unsigned long flags
;
4692 if (!cpumask_test_cpu(cpu
, buffer
->cpumask
))
4693 return ERR_PTR(-ENODEV
);
4695 cpu_buffer
= buffer
->buffers
[cpu
];
4696 local_irq_save(flags
);
4697 arch_spin_lock(&cpu_buffer
->lock
);
4699 if (cpu_buffer
->free_page
) {
4700 bpage
= cpu_buffer
->free_page
;
4701 cpu_buffer
->free_page
= NULL
;
4704 arch_spin_unlock(&cpu_buffer
->lock
);
4705 local_irq_restore(flags
);
4710 page
= alloc_pages_node(cpu_to_node(cpu
),
4711 GFP_KERNEL
| __GFP_NORETRY
, 0);
4713 return ERR_PTR(-ENOMEM
);
4715 bpage
= page_address(page
);
4718 rb_init_page(bpage
);
4722 EXPORT_SYMBOL_GPL(ring_buffer_alloc_read_page
);
4725 * ring_buffer_free_read_page - free an allocated read page
4726 * @buffer: the buffer the page was allocate for
4727 * @cpu: the cpu buffer the page came from
4728 * @data: the page to free
4730 * Free a page allocated from ring_buffer_alloc_read_page.
4732 void ring_buffer_free_read_page(struct trace_buffer
*buffer
, int cpu
, void *data
)
4734 struct ring_buffer_per_cpu
*cpu_buffer
= buffer
->buffers
[cpu
];
4735 struct buffer_data_page
*bpage
= data
;
4736 struct page
*page
= virt_to_page(bpage
);
4737 unsigned long flags
;
4739 /* If the page is still in use someplace else, we can't reuse it */
4740 if (page_ref_count(page
) > 1)
4743 local_irq_save(flags
);
4744 arch_spin_lock(&cpu_buffer
->lock
);
4746 if (!cpu_buffer
->free_page
) {
4747 cpu_buffer
->free_page
= bpage
;
4751 arch_spin_unlock(&cpu_buffer
->lock
);
4752 local_irq_restore(flags
);
4755 free_page((unsigned long)bpage
);
4757 EXPORT_SYMBOL_GPL(ring_buffer_free_read_page
);
4760 * ring_buffer_read_page - extract a page from the ring buffer
4761 * @buffer: buffer to extract from
4762 * @data_page: the page to use allocated from ring_buffer_alloc_read_page
4763 * @len: amount to extract
4764 * @cpu: the cpu of the buffer to extract
4765 * @full: should the extraction only happen when the page is full.
4767 * This function will pull out a page from the ring buffer and consume it.
4768 * @data_page must be the address of the variable that was returned
4769 * from ring_buffer_alloc_read_page. This is because the page might be used
4770 * to swap with a page in the ring buffer.
4773 * rpage = ring_buffer_alloc_read_page(buffer, cpu);
4774 * if (IS_ERR(rpage))
4775 * return PTR_ERR(rpage);
4776 * ret = ring_buffer_read_page(buffer, &rpage, len, cpu, 0);
4778 * process_page(rpage, ret);
4780 * When @full is set, the function will not return true unless
4781 * the writer is off the reader page.
4783 * Note: it is up to the calling functions to handle sleeps and wakeups.
4784 * The ring buffer can be used anywhere in the kernel and can not
4785 * blindly call wake_up. The layer that uses the ring buffer must be
4786 * responsible for that.
4789 * >=0 if data has been transferred, returns the offset of consumed data.
4790 * <0 if no data has been transferred.
4792 int ring_buffer_read_page(struct trace_buffer
*buffer
,
4793 void **data_page
, size_t len
, int cpu
, int full
)
4795 struct ring_buffer_per_cpu
*cpu_buffer
= buffer
->buffers
[cpu
];
4796 struct ring_buffer_event
*event
;
4797 struct buffer_data_page
*bpage
;
4798 struct buffer_page
*reader
;
4799 unsigned long missed_events
;
4800 unsigned long flags
;
4801 unsigned int commit
;
4806 if (!cpumask_test_cpu(cpu
, buffer
->cpumask
))
4810 * If len is not big enough to hold the page header, then
4811 * we can not copy anything.
4813 if (len
<= BUF_PAGE_HDR_SIZE
)
4816 len
-= BUF_PAGE_HDR_SIZE
;
4825 raw_spin_lock_irqsave(&cpu_buffer
->reader_lock
, flags
);
4827 reader
= rb_get_reader_page(cpu_buffer
);
4831 event
= rb_reader_event(cpu_buffer
);
4833 read
= reader
->read
;
4834 commit
= rb_page_commit(reader
);
4836 /* Check if any events were dropped */
4837 missed_events
= cpu_buffer
->lost_events
;
4840 * If this page has been partially read or
4841 * if len is not big enough to read the rest of the page or
4842 * a writer is still on the page, then
4843 * we must copy the data from the page to the buffer.
4844 * Otherwise, we can simply swap the page with the one passed in.
4846 if (read
|| (len
< (commit
- read
)) ||
4847 cpu_buffer
->reader_page
== cpu_buffer
->commit_page
) {
4848 struct buffer_data_page
*rpage
= cpu_buffer
->reader_page
->page
;
4849 unsigned int rpos
= read
;
4850 unsigned int pos
= 0;
4856 if (len
> (commit
- read
))
4857 len
= (commit
- read
);
4859 /* Always keep the time extend and data together */
4860 size
= rb_event_ts_length(event
);
4865 /* save the current timestamp, since the user will need it */
4866 save_timestamp
= cpu_buffer
->read_stamp
;
4868 /* Need to copy one event at a time */
4870 /* We need the size of one event, because
4871 * rb_advance_reader only advances by one event,
4872 * whereas rb_event_ts_length may include the size of
4873 * one or two events.
4874 * We have already ensured there's enough space if this
4875 * is a time extend. */
4876 size
= rb_event_length(event
);
4877 memcpy(bpage
->data
+ pos
, rpage
->data
+ rpos
, size
);
4881 rb_advance_reader(cpu_buffer
);
4882 rpos
= reader
->read
;
4888 event
= rb_reader_event(cpu_buffer
);
4889 /* Always keep the time extend and data together */
4890 size
= rb_event_ts_length(event
);
4891 } while (len
>= size
);
4894 local_set(&bpage
->commit
, pos
);
4895 bpage
->time_stamp
= save_timestamp
;
4897 /* we copied everything to the beginning */
4900 /* update the entry counter */
4901 cpu_buffer
->read
+= rb_page_entries(reader
);
4902 cpu_buffer
->read_bytes
+= BUF_PAGE_SIZE
;
4904 /* swap the pages */
4905 rb_init_page(bpage
);
4906 bpage
= reader
->page
;
4907 reader
->page
= *data_page
;
4908 local_set(&reader
->write
, 0);
4909 local_set(&reader
->entries
, 0);
4914 * Use the real_end for the data size,
4915 * This gives us a chance to store the lost events
4918 if (reader
->real_end
)
4919 local_set(&bpage
->commit
, reader
->real_end
);
4923 cpu_buffer
->lost_events
= 0;
4925 commit
= local_read(&bpage
->commit
);
4927 * Set a flag in the commit field if we lost events
4929 if (missed_events
) {
4930 /* If there is room at the end of the page to save the
4931 * missed events, then record it there.
4933 if (BUF_PAGE_SIZE
- commit
>= sizeof(missed_events
)) {
4934 memcpy(&bpage
->data
[commit
], &missed_events
,
4935 sizeof(missed_events
));
4936 local_add(RB_MISSED_STORED
, &bpage
->commit
);
4937 commit
+= sizeof(missed_events
);
4939 local_add(RB_MISSED_EVENTS
, &bpage
->commit
);
4943 * This page may be off to user land. Zero it out here.
4945 if (commit
< BUF_PAGE_SIZE
)
4946 memset(&bpage
->data
[commit
], 0, BUF_PAGE_SIZE
- commit
);
4949 raw_spin_unlock_irqrestore(&cpu_buffer
->reader_lock
, flags
);
4954 EXPORT_SYMBOL_GPL(ring_buffer_read_page
);
4957 * We only allocate new buffers, never free them if the CPU goes down.
4958 * If we were to free the buffer, then the user would lose any trace that was in
4961 int trace_rb_cpu_prepare(unsigned int cpu
, struct hlist_node
*node
)
4963 struct trace_buffer
*buffer
;
4966 unsigned long nr_pages
;
4968 buffer
= container_of(node
, struct trace_buffer
, node
);
4969 if (cpumask_test_cpu(cpu
, buffer
->cpumask
))
4974 /* check if all cpu sizes are same */
4975 for_each_buffer_cpu(buffer
, cpu_i
) {
4976 /* fill in the size from first enabled cpu */
4978 nr_pages
= buffer
->buffers
[cpu_i
]->nr_pages
;
4979 if (nr_pages
!= buffer
->buffers
[cpu_i
]->nr_pages
) {
4984 /* allocate minimum pages, user can later expand it */
4987 buffer
->buffers
[cpu
] =
4988 rb_allocate_cpu_buffer(buffer
, nr_pages
, cpu
);
4989 if (!buffer
->buffers
[cpu
]) {
4990 WARN(1, "failed to allocate ring buffer on CPU %u\n",
4995 cpumask_set_cpu(cpu
, buffer
->cpumask
);
4999 #ifdef CONFIG_RING_BUFFER_STARTUP_TEST
5001 * This is a basic integrity check of the ring buffer.
5002 * Late in the boot cycle this test will run when configured in.
5003 * It will kick off a thread per CPU that will go into a loop
5004 * writing to the per cpu ring buffer various sizes of data.
5005 * Some of the data will be large items, some small.
5007 * Another thread is created that goes into a spin, sending out
5008 * IPIs to the other CPUs to also write into the ring buffer.
5009 * this is to test the nesting ability of the buffer.
5011 * Basic stats are recorded and reported. If something in the
5012 * ring buffer should happen that's not expected, a big warning
5013 * is displayed and all ring buffers are disabled.
5015 static struct task_struct
*rb_threads
[NR_CPUS
] __initdata
;
5017 struct rb_test_data
{
5018 struct trace_buffer
*buffer
;
5019 unsigned long events
;
5020 unsigned long bytes_written
;
5021 unsigned long bytes_alloc
;
5022 unsigned long bytes_dropped
;
5023 unsigned long events_nested
;
5024 unsigned long bytes_written_nested
;
5025 unsigned long bytes_alloc_nested
;
5026 unsigned long bytes_dropped_nested
;
5027 int min_size_nested
;
5028 int max_size_nested
;
5035 static struct rb_test_data rb_data
[NR_CPUS
] __initdata
;
5038 #define RB_TEST_BUFFER_SIZE 1048576
5040 static char rb_string
[] __initdata
=
5041 "abcdefghijklmnopqrstuvwxyz1234567890!@#$%^&*()?+\\"
5042 "?+|:';\",.<>/?abcdefghijklmnopqrstuvwxyz1234567890"
5043 "!@#$%^&*()?+\\?+|:';\",.<>/?abcdefghijklmnopqrstuv";
5045 static bool rb_test_started __initdata
;
5052 static __init
int rb_write_something(struct rb_test_data
*data
, bool nested
)
5054 struct ring_buffer_event
*event
;
5055 struct rb_item
*item
;
5062 /* Have nested writes different that what is written */
5063 cnt
= data
->cnt
+ (nested
? 27 : 0);
5065 /* Multiply cnt by ~e, to make some unique increment */
5066 size
= (cnt
* 68 / 25) % (sizeof(rb_string
) - 1);
5068 len
= size
+ sizeof(struct rb_item
);
5070 started
= rb_test_started
;
5071 /* read rb_test_started before checking buffer enabled */
5074 event
= ring_buffer_lock_reserve(data
->buffer
, len
);
5076 /* Ignore dropped events before test starts. */
5079 data
->bytes_dropped
+= len
;
5081 data
->bytes_dropped_nested
+= len
;
5086 event_len
= ring_buffer_event_length(event
);
5088 if (RB_WARN_ON(data
->buffer
, event_len
< len
))
5091 item
= ring_buffer_event_data(event
);
5093 memcpy(item
->str
, rb_string
, size
);
5096 data
->bytes_alloc_nested
+= event_len
;
5097 data
->bytes_written_nested
+= len
;
5098 data
->events_nested
++;
5099 if (!data
->min_size_nested
|| len
< data
->min_size_nested
)
5100 data
->min_size_nested
= len
;
5101 if (len
> data
->max_size_nested
)
5102 data
->max_size_nested
= len
;
5104 data
->bytes_alloc
+= event_len
;
5105 data
->bytes_written
+= len
;
5107 if (!data
->min_size
|| len
< data
->min_size
)
5108 data
->max_size
= len
;
5109 if (len
> data
->max_size
)
5110 data
->max_size
= len
;
5114 ring_buffer_unlock_commit(data
->buffer
, event
);
5119 static __init
int rb_test(void *arg
)
5121 struct rb_test_data
*data
= arg
;
5123 while (!kthread_should_stop()) {
5124 rb_write_something(data
, false);
5127 set_current_state(TASK_INTERRUPTIBLE
);
5128 /* Now sleep between a min of 100-300us and a max of 1ms */
5129 usleep_range(((data
->cnt
% 3) + 1) * 100, 1000);
5135 static __init
void rb_ipi(void *ignore
)
5137 struct rb_test_data
*data
;
5138 int cpu
= smp_processor_id();
5140 data
= &rb_data
[cpu
];
5141 rb_write_something(data
, true);
5144 static __init
int rb_hammer_test(void *arg
)
5146 while (!kthread_should_stop()) {
5148 /* Send an IPI to all cpus to write data! */
5149 smp_call_function(rb_ipi
, NULL
, 1);
5150 /* No sleep, but for non preempt, let others run */
5157 static __init
int test_ringbuffer(void)
5159 struct task_struct
*rb_hammer
;
5160 struct trace_buffer
*buffer
;
5164 if (security_locked_down(LOCKDOWN_TRACEFS
)) {
5165 pr_warn("Lockdown is enabled, skipping ring buffer tests\n");
5169 pr_info("Running ring buffer tests...\n");
5171 buffer
= ring_buffer_alloc(RB_TEST_BUFFER_SIZE
, RB_FL_OVERWRITE
);
5172 if (WARN_ON(!buffer
))
5175 /* Disable buffer so that threads can't write to it yet */
5176 ring_buffer_record_off(buffer
);
5178 for_each_online_cpu(cpu
) {
5179 rb_data
[cpu
].buffer
= buffer
;
5180 rb_data
[cpu
].cpu
= cpu
;
5181 rb_data
[cpu
].cnt
= cpu
;
5182 rb_threads
[cpu
] = kthread_create(rb_test
, &rb_data
[cpu
],
5183 "rbtester/%d", cpu
);
5184 if (WARN_ON(IS_ERR(rb_threads
[cpu
]))) {
5185 pr_cont("FAILED\n");
5186 ret
= PTR_ERR(rb_threads
[cpu
]);
5190 kthread_bind(rb_threads
[cpu
], cpu
);
5191 wake_up_process(rb_threads
[cpu
]);
5194 /* Now create the rb hammer! */
5195 rb_hammer
= kthread_run(rb_hammer_test
, NULL
, "rbhammer");
5196 if (WARN_ON(IS_ERR(rb_hammer
))) {
5197 pr_cont("FAILED\n");
5198 ret
= PTR_ERR(rb_hammer
);
5202 ring_buffer_record_on(buffer
);
5204 * Show buffer is enabled before setting rb_test_started.
5205 * Yes there's a small race window where events could be
5206 * dropped and the thread wont catch it. But when a ring
5207 * buffer gets enabled, there will always be some kind of
5208 * delay before other CPUs see it. Thus, we don't care about
5209 * those dropped events. We care about events dropped after
5210 * the threads see that the buffer is active.
5213 rb_test_started
= true;
5215 set_current_state(TASK_INTERRUPTIBLE
);
5216 /* Just run for 10 seconds */;
5217 schedule_timeout(10 * HZ
);
5219 kthread_stop(rb_hammer
);
5222 for_each_online_cpu(cpu
) {
5223 if (!rb_threads
[cpu
])
5225 kthread_stop(rb_threads
[cpu
]);
5228 ring_buffer_free(buffer
);
5233 pr_info("finished\n");
5234 for_each_online_cpu(cpu
) {
5235 struct ring_buffer_event
*event
;
5236 struct rb_test_data
*data
= &rb_data
[cpu
];
5237 struct rb_item
*item
;
5238 unsigned long total_events
;
5239 unsigned long total_dropped
;
5240 unsigned long total_written
;
5241 unsigned long total_alloc
;
5242 unsigned long total_read
= 0;
5243 unsigned long total_size
= 0;
5244 unsigned long total_len
= 0;
5245 unsigned long total_lost
= 0;
5248 int small_event_size
;
5252 total_events
= data
->events
+ data
->events_nested
;
5253 total_written
= data
->bytes_written
+ data
->bytes_written_nested
;
5254 total_alloc
= data
->bytes_alloc
+ data
->bytes_alloc_nested
;
5255 total_dropped
= data
->bytes_dropped
+ data
->bytes_dropped_nested
;
5257 big_event_size
= data
->max_size
+ data
->max_size_nested
;
5258 small_event_size
= data
->min_size
+ data
->min_size_nested
;
5260 pr_info("CPU %d:\n", cpu
);
5261 pr_info(" events: %ld\n", total_events
);
5262 pr_info(" dropped bytes: %ld\n", total_dropped
);
5263 pr_info(" alloced bytes: %ld\n", total_alloc
);
5264 pr_info(" written bytes: %ld\n", total_written
);
5265 pr_info(" biggest event: %d\n", big_event_size
);
5266 pr_info(" smallest event: %d\n", small_event_size
);
5268 if (RB_WARN_ON(buffer
, total_dropped
))
5273 while ((event
= ring_buffer_consume(buffer
, cpu
, NULL
, &lost
))) {
5275 item
= ring_buffer_event_data(event
);
5276 total_len
+= ring_buffer_event_length(event
);
5277 total_size
+= item
->size
+ sizeof(struct rb_item
);
5278 if (memcmp(&item
->str
[0], rb_string
, item
->size
) != 0) {
5279 pr_info("FAILED!\n");
5280 pr_info("buffer had: %.*s\n", item
->size
, item
->str
);
5281 pr_info("expected: %.*s\n", item
->size
, rb_string
);
5282 RB_WARN_ON(buffer
, 1);
5293 pr_info(" read events: %ld\n", total_read
);
5294 pr_info(" lost events: %ld\n", total_lost
);
5295 pr_info(" total events: %ld\n", total_lost
+ total_read
);
5296 pr_info(" recorded len bytes: %ld\n", total_len
);
5297 pr_info(" recorded size bytes: %ld\n", total_size
);
5299 pr_info(" With dropped events, record len and size may not match\n"
5300 " alloced and written from above\n");
5302 if (RB_WARN_ON(buffer
, total_len
!= total_alloc
||
5303 total_size
!= total_written
))
5306 if (RB_WARN_ON(buffer
, total_lost
+ total_read
!= total_events
))
5312 pr_info("Ring buffer PASSED!\n");
5314 ring_buffer_free(buffer
);
5318 late_initcall(test_ringbuffer
);
5319 #endif /* CONFIG_RING_BUFFER_STARTUP_TEST */