1 // SPDX-License-Identifier: GPL-2.0
5 * Copyright (C) 2008 Steven Rostedt <srostedt@redhat.com>
7 #include <linux/trace_recursion.h>
8 #include <linux/trace_events.h>
9 #include <linux/ring_buffer.h>
10 #include <linux/trace_clock.h>
11 #include <linux/sched/clock.h>
12 #include <linux/trace_seq.h>
13 #include <linux/spinlock.h>
14 #include <linux/irq_work.h>
15 #include <linux/security.h>
16 #include <linux/uaccess.h>
17 #include <linux/hardirq.h>
18 #include <linux/kthread.h> /* for self test */
19 #include <linux/module.h>
20 #include <linux/percpu.h>
21 #include <linux/mutex.h>
22 #include <linux/delay.h>
23 #include <linux/slab.h>
24 #include <linux/init.h>
25 #include <linux/hash.h>
26 #include <linux/list.h>
27 #include <linux/cpu.h>
28 #include <linux/oom.h>
30 #include <asm/local.h>
32 static void update_pages_handler(struct work_struct *work);
35 * The ring buffer header is special. We must manually up keep it.
37 int ring_buffer_print_entry_header(struct trace_seq *s)
39 trace_seq_puts(s, "# compressed entry header\n");
40 trace_seq_puts(s, "\ttype_len : 5 bits\n");
41 trace_seq_puts(s, "\ttime_delta : 27 bits\n");
42 trace_seq_puts(s, "\tarray : 32 bits\n");
43 trace_seq_putc(s, '\n');
44 trace_seq_printf(s, "\tpadding : type == %d\n",
45 RINGBUF_TYPE_PADDING);
46 trace_seq_printf(s, "\ttime_extend : type == %d\n",
47 RINGBUF_TYPE_TIME_EXTEND);
48 trace_seq_printf(s, "\ttime_stamp : type == %d\n",
49 RINGBUF_TYPE_TIME_STAMP);
50 trace_seq_printf(s, "\tdata max type_len == %d\n",
51 RINGBUF_TYPE_DATA_TYPE_LEN_MAX);
53 return !trace_seq_has_overflowed(s);
57 * The ring buffer is made up of a list of pages. A separate list of pages is
58 * allocated for each CPU. A writer may only write to a buffer that is
59 * associated with the CPU it is currently executing on. A reader may read
60 * from any per cpu buffer.
62 * The reader is special. For each per cpu buffer, the reader has its own
63 * reader page. When a reader has read the entire reader page, this reader
64 * page is swapped with another page in the ring buffer.
66 * Now, as long as the writer is off the reader page, the reader can do what
67 * ever it wants with that page. The writer will never write to that page
68 * again (as long as it is out of the ring buffer).
70 * Here's some silly ASCII art.
73 * |reader| RING BUFFER
75 * +------+ +---+ +---+ +---+
84 * |reader| RING BUFFER
85 * |page |------------------v
86 * +------+ +---+ +---+ +---+
95 * |reader| RING BUFFER
96 * |page |------------------v
97 * +------+ +---+ +---+ +---+
102 * +------------------------------+
106 * |buffer| RING BUFFER
107 * |page |------------------v
108 * +------+ +---+ +---+ +---+
110 * | New +---+ +---+ +---+
113 * +------------------------------+
116 * After we make this swap, the reader can hand this page off to the splice
117 * code and be done with it. It can even allocate a new page if it needs to
118 * and swap that into the ring buffer.
120 * We will be using cmpxchg soon to make all this lockless.
124 /* Used for individual buffers (after the counter) */
125 #define RB_BUFFER_OFF (1 << 20)
127 #define BUF_PAGE_HDR_SIZE offsetof(struct buffer_data_page, data)
129 #define RB_EVNT_HDR_SIZE (offsetof(struct ring_buffer_event, array))
130 #define RB_ALIGNMENT 4U
131 #define RB_MAX_SMALL_DATA (RB_ALIGNMENT * RINGBUF_TYPE_DATA_TYPE_LEN_MAX)
132 #define RB_EVNT_MIN_SIZE 8U /* two 32bit words */
134 #ifndef CONFIG_HAVE_64BIT_ALIGNED_ACCESS
135 # define RB_FORCE_8BYTE_ALIGNMENT 0
136 # define RB_ARCH_ALIGNMENT RB_ALIGNMENT
138 # define RB_FORCE_8BYTE_ALIGNMENT 1
139 # define RB_ARCH_ALIGNMENT 8U
142 #define RB_ALIGN_DATA __aligned(RB_ARCH_ALIGNMENT)
144 /* define RINGBUF_TYPE_DATA for 'case RINGBUF_TYPE_DATA:' */
145 #define RINGBUF_TYPE_DATA 0 ... RINGBUF_TYPE_DATA_TYPE_LEN_MAX
148 RB_LEN_TIME_EXTEND = 8,
149 RB_LEN_TIME_STAMP = 8,
152 #define skip_time_extend(event) \
153 ((struct ring_buffer_event *)((char *)event + RB_LEN_TIME_EXTEND))
155 #define extended_time(event) \
156 (event->type_len >= RINGBUF_TYPE_TIME_EXTEND)
158 static inline int rb_null_event(struct ring_buffer_event *event)
160 return event->type_len == RINGBUF_TYPE_PADDING && !event->time_delta;
163 static void rb_event_set_padding(struct ring_buffer_event *event)
165 /* padding has a NULL time_delta */
166 event->type_len = RINGBUF_TYPE_PADDING;
167 event->time_delta = 0;
171 rb_event_data_length(struct ring_buffer_event *event)
176 length = event->type_len * RB_ALIGNMENT;
178 length = event->array[0];
179 return length + RB_EVNT_HDR_SIZE;
183 * Return the length of the given event. Will return
184 * the length of the time extend if the event is a
187 static inline unsigned
188 rb_event_length(struct ring_buffer_event *event)
190 switch (event->type_len) {
191 case RINGBUF_TYPE_PADDING:
192 if (rb_null_event(event))
195 return event->array[0] + RB_EVNT_HDR_SIZE;
197 case RINGBUF_TYPE_TIME_EXTEND:
198 return RB_LEN_TIME_EXTEND;
200 case RINGBUF_TYPE_TIME_STAMP:
201 return RB_LEN_TIME_STAMP;
203 case RINGBUF_TYPE_DATA:
204 return rb_event_data_length(event);
213 * Return total length of time extend and data,
214 * or just the event length for all other events.
216 static inline unsigned
217 rb_event_ts_length(struct ring_buffer_event *event)
221 if (extended_time(event)) {
222 /* time extends include the data event after it */
223 len = RB_LEN_TIME_EXTEND;
224 event = skip_time_extend(event);
226 return len + rb_event_length(event);
230 * ring_buffer_event_length - return the length of the event
231 * @event: the event to get the length of
233 * Returns the size of the data load of a data event.
234 * If the event is something other than a data event, it
235 * returns the size of the event itself. With the exception
236 * of a TIME EXTEND, where it still returns the size of the
237 * data load of the data event after it.
239 unsigned ring_buffer_event_length(struct ring_buffer_event *event)
243 if (extended_time(event))
244 event = skip_time_extend(event);
246 length = rb_event_length(event);
247 if (event->type_len > RINGBUF_TYPE_DATA_TYPE_LEN_MAX)
249 length -= RB_EVNT_HDR_SIZE;
250 if (length > RB_MAX_SMALL_DATA + sizeof(event->array[0]))
251 length -= sizeof(event->array[0]);
254 EXPORT_SYMBOL_GPL(ring_buffer_event_length);
256 /* inline for ring buffer fast paths */
257 static __always_inline void *
258 rb_event_data(struct ring_buffer_event *event)
260 if (extended_time(event))
261 event = skip_time_extend(event);
262 WARN_ON_ONCE(event->type_len > RINGBUF_TYPE_DATA_TYPE_LEN_MAX);
263 /* If length is in len field, then array[0] has the data */
265 return (void *)&event->array[0];
266 /* Otherwise length is in array[0] and array[1] has the data */
267 return (void *)&event->array[1];
271 * ring_buffer_event_data - return the data of the event
272 * @event: the event to get the data from
274 void *ring_buffer_event_data(struct ring_buffer_event *event)
276 return rb_event_data(event);
278 EXPORT_SYMBOL_GPL(ring_buffer_event_data);
280 #define for_each_buffer_cpu(buffer, cpu) \
281 for_each_cpu(cpu, buffer->cpumask)
283 #define for_each_online_buffer_cpu(buffer, cpu) \
284 for_each_cpu_and(cpu, buffer->cpumask, cpu_online_mask)
287 #define TS_MASK ((1ULL << TS_SHIFT) - 1)
288 #define TS_DELTA_TEST (~TS_MASK)
291 * ring_buffer_event_time_stamp - return the event's extended timestamp
292 * @event: the event to get the timestamp of
294 * Returns the extended timestamp associated with a data event.
295 * An extended time_stamp is a 64-bit timestamp represented
296 * internally in a special way that makes the best use of space
297 * contained within a ring buffer event. This function decodes
298 * it and maps it to a straight u64 value.
300 u64 ring_buffer_event_time_stamp(struct ring_buffer_event *event)
304 ts = event->array[0];
306 ts += event->time_delta;
311 /* Flag when events were overwritten */
312 #define RB_MISSED_EVENTS (1 << 31)
313 /* Missed count stored at end */
314 #define RB_MISSED_STORED (1 << 30)
316 struct buffer_data_page {
317 u64 time_stamp; /* page time stamp */
318 local_t commit; /* write committed index */
319 unsigned char data[] RB_ALIGN_DATA; /* data of buffer page */
323 * Note, the buffer_page list must be first. The buffer pages
324 * are allocated in cache lines, which means that each buffer
325 * page will be at the beginning of a cache line, and thus
326 * the least significant bits will be zero. We use this to
327 * add flags in the list struct pointers, to make the ring buffer
331 struct list_head list; /* list of buffer pages */
332 local_t write; /* index for next write */
333 unsigned read; /* index for next read */
334 local_t entries; /* entries on this page */
335 unsigned long real_end; /* real end of data */
336 struct buffer_data_page *page; /* Actual data page */
340 * The buffer page counters, write and entries, must be reset
341 * atomically when crossing page boundaries. To synchronize this
342 * update, two counters are inserted into the number. One is
343 * the actual counter for the write position or count on the page.
345 * The other is a counter of updaters. Before an update happens
346 * the update partition of the counter is incremented. This will
347 * allow the updater to update the counter atomically.
349 * The counter is 20 bits, and the state data is 12.
351 #define RB_WRITE_MASK 0xfffff
352 #define RB_WRITE_INTCNT (1 << 20)
354 static void rb_init_page(struct buffer_data_page *bpage)
356 local_set(&bpage->commit, 0);
360 * Also stolen from mm/slob.c. Thanks to Mathieu Desnoyers for pointing
363 static void free_buffer_page(struct buffer_page *bpage)
365 free_page((unsigned long)bpage->page);
370 * We need to fit the time_stamp delta into 27 bits.
372 static inline int test_time_stamp(u64 delta)
374 if (delta & TS_DELTA_TEST)
379 #define BUF_PAGE_SIZE (PAGE_SIZE - BUF_PAGE_HDR_SIZE)
381 /* Max payload is BUF_PAGE_SIZE - header (8bytes) */
382 #define BUF_MAX_DATA_SIZE (BUF_PAGE_SIZE - (sizeof(u32) * 2))
384 int ring_buffer_print_page_header(struct trace_seq *s)
386 struct buffer_data_page field;
388 trace_seq_printf(s, "\tfield: u64 timestamp;\t"
389 "offset:0;\tsize:%u;\tsigned:%u;\n",
390 (unsigned int)sizeof(field.time_stamp),
391 (unsigned int)is_signed_type(u64));
393 trace_seq_printf(s, "\tfield: local_t commit;\t"
394 "offset:%u;\tsize:%u;\tsigned:%u;\n",
395 (unsigned int)offsetof(typeof(field), commit),
396 (unsigned int)sizeof(field.commit),
397 (unsigned int)is_signed_type(long));
399 trace_seq_printf(s, "\tfield: int overwrite;\t"
400 "offset:%u;\tsize:%u;\tsigned:%u;\n",
401 (unsigned int)offsetof(typeof(field), commit),
403 (unsigned int)is_signed_type(long));
405 trace_seq_printf(s, "\tfield: char data;\t"
406 "offset:%u;\tsize:%u;\tsigned:%u;\n",
407 (unsigned int)offsetof(typeof(field), data),
408 (unsigned int)BUF_PAGE_SIZE,
409 (unsigned int)is_signed_type(char));
411 return !trace_seq_has_overflowed(s);
415 struct irq_work work;
416 wait_queue_head_t waiters;
417 wait_queue_head_t full_waiters;
418 bool waiters_pending;
419 bool full_waiters_pending;
424 * Structure to hold event state and handle nested events.
426 struct rb_event_info {
431 unsigned long length;
432 struct buffer_page *tail_page;
437 * Used for the add_timestamp
439 * EXTEND - wants a time extend
440 * ABSOLUTE - the buffer requests all events to have absolute time stamps
441 * FORCE - force a full time stamp.
444 RB_ADD_STAMP_NONE = 0,
445 RB_ADD_STAMP_EXTEND = BIT(1),
446 RB_ADD_STAMP_ABSOLUTE = BIT(2),
447 RB_ADD_STAMP_FORCE = BIT(3)
450 * Used for which event context the event is in.
457 * See trace_recursive_lock() comment below for more details.
468 #if BITS_PER_LONG == 32
472 /* To test on 64 bit machines */
477 struct rb_time_struct {
483 #include <asm/local64.h>
484 struct rb_time_struct {
488 typedef struct rb_time_struct rb_time_t;
491 * head_page == tail_page && head == tail then buffer is empty.
493 struct ring_buffer_per_cpu {
495 atomic_t record_disabled;
496 atomic_t resize_disabled;
497 struct trace_buffer *buffer;
498 raw_spinlock_t reader_lock; /* serialize readers */
499 arch_spinlock_t lock;
500 struct lock_class_key lock_key;
501 struct buffer_data_page *free_page;
502 unsigned long nr_pages;
503 unsigned int current_context;
504 struct list_head *pages;
505 struct buffer_page *head_page; /* read from head */
506 struct buffer_page *tail_page; /* write to tail */
507 struct buffer_page *commit_page; /* committed pages */
508 struct buffer_page *reader_page;
509 unsigned long lost_events;
510 unsigned long last_overrun;
512 local_t entries_bytes;
515 local_t commit_overrun;
516 local_t dropped_events;
519 local_t pages_touched;
521 long last_pages_touch;
522 size_t shortest_full;
524 unsigned long read_bytes;
525 rb_time_t write_stamp;
526 rb_time_t before_stamp;
528 /* ring buffer pages to update, > 0 to add, < 0 to remove */
529 long nr_pages_to_update;
530 struct list_head new_pages; /* new pages to add */
531 struct work_struct update_pages_work;
532 struct completion update_done;
534 struct rb_irq_work irq_work;
537 struct trace_buffer {
540 atomic_t record_disabled;
541 cpumask_var_t cpumask;
543 struct lock_class_key *reader_lock_key;
547 struct ring_buffer_per_cpu **buffers;
549 struct hlist_node node;
552 struct rb_irq_work irq_work;
556 struct ring_buffer_iter {
557 struct ring_buffer_per_cpu *cpu_buffer;
559 unsigned long next_event;
560 struct buffer_page *head_page;
561 struct buffer_page *cache_reader_page;
562 unsigned long cache_read;
565 struct ring_buffer_event *event;
572 * On 32 bit machines, local64_t is very expensive. As the ring
573 * buffer doesn't need all the features of a true 64 bit atomic,
574 * on 32 bit, it uses these functions (64 still uses local64_t).
576 * For the ring buffer, 64 bit required operations for the time is
579 * - Only need 59 bits (uses 60 to make it even).
580 * - Reads may fail if it interrupted a modification of the time stamp.
581 * It will succeed if it did not interrupt another write even if
582 * the read itself is interrupted by a write.
583 * It returns whether it was successful or not.
585 * - Writes always succeed and will overwrite other writes and writes
586 * that were done by events interrupting the current write.
588 * - A write followed by a read of the same time stamp will always succeed,
589 * but may not contain the same value.
591 * - A cmpxchg will fail if it interrupted another write or cmpxchg.
592 * Other than that, it acts like a normal cmpxchg.
594 * The 60 bit time stamp is broken up by 30 bits in a top and bottom half
595 * (bottom being the least significant 30 bits of the 60 bit time stamp).
597 * The two most significant bits of each half holds a 2 bit counter (0-3).
598 * Each update will increment this counter by one.
599 * When reading the top and bottom, if the two counter bits match then the
600 * top and bottom together make a valid 60 bit number.
602 #define RB_TIME_SHIFT 30
603 #define RB_TIME_VAL_MASK ((1 << RB_TIME_SHIFT) - 1)
605 static inline int rb_time_cnt(unsigned long val)
607 return (val >> RB_TIME_SHIFT) & 3;
610 static inline u64 rb_time_val(unsigned long top, unsigned long bottom)
614 val = top & RB_TIME_VAL_MASK;
615 val <<= RB_TIME_SHIFT;
616 val |= bottom & RB_TIME_VAL_MASK;
621 static inline bool __rb_time_read(rb_time_t *t, u64 *ret, unsigned long *cnt)
623 unsigned long top, bottom;
627 * If the read is interrupted by a write, then the cnt will
628 * be different. Loop until both top and bottom have been read
629 * without interruption.
632 c = local_read(&t->cnt);
633 top = local_read(&t->top);
634 bottom = local_read(&t->bottom);
635 } while (c != local_read(&t->cnt));
637 *cnt = rb_time_cnt(top);
639 /* If top and bottom counts don't match, this interrupted a write */
640 if (*cnt != rb_time_cnt(bottom))
643 *ret = rb_time_val(top, bottom);
647 static bool rb_time_read(rb_time_t *t, u64 *ret)
651 return __rb_time_read(t, ret, &cnt);
654 static inline unsigned long rb_time_val_cnt(unsigned long val, unsigned long cnt)
656 return (val & RB_TIME_VAL_MASK) | ((cnt & 3) << RB_TIME_SHIFT);
659 static inline void rb_time_split(u64 val, unsigned long *top, unsigned long *bottom)
661 *top = (unsigned long)((val >> RB_TIME_SHIFT) & RB_TIME_VAL_MASK);
662 *bottom = (unsigned long)(val & RB_TIME_VAL_MASK);
665 static inline void rb_time_val_set(local_t *t, unsigned long val, unsigned long cnt)
667 val = rb_time_val_cnt(val, cnt);
671 static void rb_time_set(rb_time_t *t, u64 val)
673 unsigned long cnt, top, bottom;
675 rb_time_split(val, &top, &bottom);
677 /* Writes always succeed with a valid number even if it gets interrupted. */
679 cnt = local_inc_return(&t->cnt);
680 rb_time_val_set(&t->top, top, cnt);
681 rb_time_val_set(&t->bottom, bottom, cnt);
682 } while (cnt != local_read(&t->cnt));
686 rb_time_read_cmpxchg(local_t *l, unsigned long expect, unsigned long set)
690 ret = local_cmpxchg(l, expect, set);
691 return ret == expect;
694 static int rb_time_cmpxchg(rb_time_t *t, u64 expect, u64 set)
696 unsigned long cnt, top, bottom;
697 unsigned long cnt2, top2, bottom2;
700 /* The cmpxchg always fails if it interrupted an update */
701 if (!__rb_time_read(t, &val, &cnt2))
707 cnt = local_read(&t->cnt);
708 if ((cnt & 3) != cnt2)
713 rb_time_split(val, &top, &bottom);
714 top = rb_time_val_cnt(top, cnt);
715 bottom = rb_time_val_cnt(bottom, cnt);
717 rb_time_split(set, &top2, &bottom2);
718 top2 = rb_time_val_cnt(top2, cnt2);
719 bottom2 = rb_time_val_cnt(bottom2, cnt2);
721 if (!rb_time_read_cmpxchg(&t->cnt, cnt, cnt2))
723 if (!rb_time_read_cmpxchg(&t->top, top, top2))
725 if (!rb_time_read_cmpxchg(&t->bottom, bottom, bottom2))
732 /* local64_t always succeeds */
734 static inline bool rb_time_read(rb_time_t *t, u64 *ret)
736 *ret = local64_read(&t->time);
739 static void rb_time_set(rb_time_t *t, u64 val)
741 local64_set(&t->time, val);
744 static bool rb_time_cmpxchg(rb_time_t *t, u64 expect, u64 set)
747 val = local64_cmpxchg(&t->time, expect, set);
748 return val == expect;
753 * ring_buffer_nr_pages - get the number of buffer pages in the ring buffer
754 * @buffer: The ring_buffer to get the number of pages from
755 * @cpu: The cpu of the ring_buffer to get the number of pages from
757 * Returns the number of pages used by a per_cpu buffer of the ring buffer.
759 size_t ring_buffer_nr_pages(struct trace_buffer *buffer, int cpu)
761 return buffer->buffers[cpu]->nr_pages;
765 * ring_buffer_nr_pages_dirty - get the number of used pages in the ring buffer
766 * @buffer: The ring_buffer to get the number of pages from
767 * @cpu: The cpu of the ring_buffer to get the number of pages from
769 * Returns the number of pages that have content in the ring buffer.
771 size_t ring_buffer_nr_dirty_pages(struct trace_buffer *buffer, int cpu)
776 read = local_read(&buffer->buffers[cpu]->pages_read);
777 cnt = local_read(&buffer->buffers[cpu]->pages_touched);
778 /* The reader can read an empty page, but not more than that */
780 WARN_ON_ONCE(read > cnt + 1);
788 * rb_wake_up_waiters - wake up tasks waiting for ring buffer input
790 * Schedules a delayed work to wake up any task that is blocked on the
791 * ring buffer waiters queue.
793 static void rb_wake_up_waiters(struct irq_work *work)
795 struct rb_irq_work *rbwork = container_of(work, struct rb_irq_work, work);
797 wake_up_all(&rbwork->waiters);
798 if (rbwork->wakeup_full) {
799 rbwork->wakeup_full = false;
800 wake_up_all(&rbwork->full_waiters);
805 * ring_buffer_wait - wait for input to the ring buffer
806 * @buffer: buffer to wait on
807 * @cpu: the cpu buffer to wait on
808 * @full: wait until the percentage of pages are available, if @cpu != RING_BUFFER_ALL_CPUS
810 * If @cpu == RING_BUFFER_ALL_CPUS then the task will wake up as soon
811 * as data is added to any of the @buffer's cpu buffers. Otherwise
812 * it will wait for data to be added to a specific cpu buffer.
814 int ring_buffer_wait(struct trace_buffer *buffer, int cpu, int full)
816 struct ring_buffer_per_cpu *cpu_buffer;
818 struct rb_irq_work *work;
822 * Depending on what the caller is waiting for, either any
823 * data in any cpu buffer, or a specific buffer, put the
824 * caller on the appropriate wait queue.
826 if (cpu == RING_BUFFER_ALL_CPUS) {
827 work = &buffer->irq_work;
828 /* Full only makes sense on per cpu reads */
831 if (!cpumask_test_cpu(cpu, buffer->cpumask))
833 cpu_buffer = buffer->buffers[cpu];
834 work = &cpu_buffer->irq_work;
840 prepare_to_wait(&work->full_waiters, &wait, TASK_INTERRUPTIBLE);
842 prepare_to_wait(&work->waiters, &wait, TASK_INTERRUPTIBLE);
845 * The events can happen in critical sections where
846 * checking a work queue can cause deadlocks.
847 * After adding a task to the queue, this flag is set
848 * only to notify events to try to wake up the queue
851 * We don't clear it even if the buffer is no longer
852 * empty. The flag only causes the next event to run
853 * irq_work to do the work queue wake up. The worse
854 * that can happen if we race with !trace_empty() is that
855 * an event will cause an irq_work to try to wake up
858 * There's no reason to protect this flag either, as
859 * the work queue and irq_work logic will do the necessary
860 * synchronization for the wake ups. The only thing
861 * that is necessary is that the wake up happens after
862 * a task has been queued. It's OK for spurious wake ups.
865 work->full_waiters_pending = true;
867 work->waiters_pending = true;
869 if (signal_pending(current)) {
874 if (cpu == RING_BUFFER_ALL_CPUS && !ring_buffer_empty(buffer))
877 if (cpu != RING_BUFFER_ALL_CPUS &&
878 !ring_buffer_empty_cpu(buffer, cpu)) {
887 raw_spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
888 pagebusy = cpu_buffer->reader_page == cpu_buffer->commit_page;
889 nr_pages = cpu_buffer->nr_pages;
890 dirty = ring_buffer_nr_dirty_pages(buffer, cpu);
891 if (!cpu_buffer->shortest_full ||
892 cpu_buffer->shortest_full < full)
893 cpu_buffer->shortest_full = full;
894 raw_spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
896 (!nr_pages || (dirty * 100) > full * nr_pages))
904 finish_wait(&work->full_waiters, &wait);
906 finish_wait(&work->waiters, &wait);
912 * ring_buffer_poll_wait - poll on buffer input
913 * @buffer: buffer to wait on
914 * @cpu: the cpu buffer to wait on
915 * @filp: the file descriptor
916 * @poll_table: The poll descriptor
918 * If @cpu == RING_BUFFER_ALL_CPUS then the task will wake up as soon
919 * as data is added to any of the @buffer's cpu buffers. Otherwise
920 * it will wait for data to be added to a specific cpu buffer.
922 * Returns EPOLLIN | EPOLLRDNORM if data exists in the buffers,
925 __poll_t ring_buffer_poll_wait(struct trace_buffer *buffer, int cpu,
926 struct file *filp, poll_table *poll_table)
928 struct ring_buffer_per_cpu *cpu_buffer;
929 struct rb_irq_work *work;
931 if (cpu == RING_BUFFER_ALL_CPUS)
932 work = &buffer->irq_work;
934 if (!cpumask_test_cpu(cpu, buffer->cpumask))
937 cpu_buffer = buffer->buffers[cpu];
938 work = &cpu_buffer->irq_work;
941 poll_wait(filp, &work->waiters, poll_table);
942 work->waiters_pending = true;
944 * There's a tight race between setting the waiters_pending and
945 * checking if the ring buffer is empty. Once the waiters_pending bit
946 * is set, the next event will wake the task up, but we can get stuck
947 * if there's only a single event in.
949 * FIXME: Ideally, we need a memory barrier on the writer side as well,
950 * but adding a memory barrier to all events will cause too much of a
951 * performance hit in the fast path. We only need a memory barrier when
952 * the buffer goes from empty to having content. But as this race is
953 * extremely small, and it's not a problem if another event comes in, we
958 if ((cpu == RING_BUFFER_ALL_CPUS && !ring_buffer_empty(buffer)) ||
959 (cpu != RING_BUFFER_ALL_CPUS && !ring_buffer_empty_cpu(buffer, cpu)))
960 return EPOLLIN | EPOLLRDNORM;
964 /* buffer may be either ring_buffer or ring_buffer_per_cpu */
965 #define RB_WARN_ON(b, cond) \
967 int _____ret = unlikely(cond); \
969 if (__same_type(*(b), struct ring_buffer_per_cpu)) { \
970 struct ring_buffer_per_cpu *__b = \
972 atomic_inc(&__b->buffer->record_disabled); \
974 atomic_inc(&b->record_disabled); \
980 /* Up this if you want to test the TIME_EXTENTS and normalization */
981 #define DEBUG_SHIFT 0
983 static inline u64 rb_time_stamp(struct trace_buffer *buffer)
987 /* Skip retpolines :-( */
988 if (IS_ENABLED(CONFIG_RETPOLINE) && likely(buffer->clock == trace_clock_local))
989 ts = trace_clock_local();
991 ts = buffer->clock();
993 /* shift to debug/test normalization and TIME_EXTENTS */
994 return ts << DEBUG_SHIFT;
997 u64 ring_buffer_time_stamp(struct trace_buffer *buffer, int cpu)
1001 preempt_disable_notrace();
1002 time = rb_time_stamp(buffer);
1003 preempt_enable_notrace();
1007 EXPORT_SYMBOL_GPL(ring_buffer_time_stamp);
1009 void ring_buffer_normalize_time_stamp(struct trace_buffer *buffer,
1012 /* Just stupid testing the normalize function and deltas */
1013 *ts >>= DEBUG_SHIFT;
1015 EXPORT_SYMBOL_GPL(ring_buffer_normalize_time_stamp);
1018 * Making the ring buffer lockless makes things tricky.
1019 * Although writes only happen on the CPU that they are on,
1020 * and they only need to worry about interrupts. Reads can
1021 * happen on any CPU.
1023 * The reader page is always off the ring buffer, but when the
1024 * reader finishes with a page, it needs to swap its page with
1025 * a new one from the buffer. The reader needs to take from
1026 * the head (writes go to the tail). But if a writer is in overwrite
1027 * mode and wraps, it must push the head page forward.
1029 * Here lies the problem.
1031 * The reader must be careful to replace only the head page, and
1032 * not another one. As described at the top of the file in the
1033 * ASCII art, the reader sets its old page to point to the next
1034 * page after head. It then sets the page after head to point to
1035 * the old reader page. But if the writer moves the head page
1036 * during this operation, the reader could end up with the tail.
1038 * We use cmpxchg to help prevent this race. We also do something
1039 * special with the page before head. We set the LSB to 1.
1041 * When the writer must push the page forward, it will clear the
1042 * bit that points to the head page, move the head, and then set
1043 * the bit that points to the new head page.
1045 * We also don't want an interrupt coming in and moving the head
1046 * page on another writer. Thus we use the second LSB to catch
1049 * head->list->prev->next bit 1 bit 0
1052 * Points to head page 0 1
1055 * Note we can not trust the prev pointer of the head page, because:
1057 * +----+ +-----+ +-----+
1058 * | |------>| T |---X--->| N |
1060 * +----+ +-----+ +-----+
1063 * +----------| R |----------+ |
1067 * Key: ---X--> HEAD flag set in pointer
1072 * (see __rb_reserve_next() to see where this happens)
1074 * What the above shows is that the reader just swapped out
1075 * the reader page with a page in the buffer, but before it
1076 * could make the new header point back to the new page added
1077 * it was preempted by a writer. The writer moved forward onto
1078 * the new page added by the reader and is about to move forward
1081 * You can see, it is legitimate for the previous pointer of
1082 * the head (or any page) not to point back to itself. But only
1086 #define RB_PAGE_NORMAL 0UL
1087 #define RB_PAGE_HEAD 1UL
1088 #define RB_PAGE_UPDATE 2UL
1091 #define RB_FLAG_MASK 3UL
1093 /* PAGE_MOVED is not part of the mask */
1094 #define RB_PAGE_MOVED 4UL
1097 * rb_list_head - remove any bit
1099 static struct list_head *rb_list_head(struct list_head *list)
1101 unsigned long val = (unsigned long)list;
1103 return (struct list_head *)(val & ~RB_FLAG_MASK);
1107 * rb_is_head_page - test if the given page is the head page
1109 * Because the reader may move the head_page pointer, we can
1110 * not trust what the head page is (it may be pointing to
1111 * the reader page). But if the next page is a header page,
1112 * its flags will be non zero.
1115 rb_is_head_page(struct buffer_page *page, struct list_head *list)
1119 val = (unsigned long)list->next;
1121 if ((val & ~RB_FLAG_MASK) != (unsigned long)&page->list)
1122 return RB_PAGE_MOVED;
1124 return val & RB_FLAG_MASK;
1130 * The unique thing about the reader page, is that, if the
1131 * writer is ever on it, the previous pointer never points
1132 * back to the reader page.
1134 static bool rb_is_reader_page(struct buffer_page *page)
1136 struct list_head *list = page->list.prev;
1138 return rb_list_head(list->next) != &page->list;
1142 * rb_set_list_to_head - set a list_head to be pointing to head.
1144 static void rb_set_list_to_head(struct list_head *list)
1148 ptr = (unsigned long *)&list->next;
1149 *ptr |= RB_PAGE_HEAD;
1150 *ptr &= ~RB_PAGE_UPDATE;
1154 * rb_head_page_activate - sets up head page
1156 static void rb_head_page_activate(struct ring_buffer_per_cpu *cpu_buffer)
1158 struct buffer_page *head;
1160 head = cpu_buffer->head_page;
1165 * Set the previous list pointer to have the HEAD flag.
1167 rb_set_list_to_head(head->list.prev);
1170 static void rb_list_head_clear(struct list_head *list)
1172 unsigned long *ptr = (unsigned long *)&list->next;
1174 *ptr &= ~RB_FLAG_MASK;
1178 * rb_head_page_deactivate - clears head page ptr (for free list)
1181 rb_head_page_deactivate(struct ring_buffer_per_cpu *cpu_buffer)
1183 struct list_head *hd;
1185 /* Go through the whole list and clear any pointers found. */
1186 rb_list_head_clear(cpu_buffer->pages);
1188 list_for_each(hd, cpu_buffer->pages)
1189 rb_list_head_clear(hd);
1192 static int rb_head_page_set(struct ring_buffer_per_cpu *cpu_buffer,
1193 struct buffer_page *head,
1194 struct buffer_page *prev,
1195 int old_flag, int new_flag)
1197 struct list_head *list;
1198 unsigned long val = (unsigned long)&head->list;
1203 val &= ~RB_FLAG_MASK;
1205 ret = cmpxchg((unsigned long *)&list->next,
1206 val | old_flag, val | new_flag);
1208 /* check if the reader took the page */
1209 if ((ret & ~RB_FLAG_MASK) != val)
1210 return RB_PAGE_MOVED;
1212 return ret & RB_FLAG_MASK;
1215 static int rb_head_page_set_update(struct ring_buffer_per_cpu *cpu_buffer,
1216 struct buffer_page *head,
1217 struct buffer_page *prev,
1220 return rb_head_page_set(cpu_buffer, head, prev,
1221 old_flag, RB_PAGE_UPDATE);
1224 static int rb_head_page_set_head(struct ring_buffer_per_cpu *cpu_buffer,
1225 struct buffer_page *head,
1226 struct buffer_page *prev,
1229 return rb_head_page_set(cpu_buffer, head, prev,
1230 old_flag, RB_PAGE_HEAD);
1233 static int rb_head_page_set_normal(struct ring_buffer_per_cpu *cpu_buffer,
1234 struct buffer_page *head,
1235 struct buffer_page *prev,
1238 return rb_head_page_set(cpu_buffer, head, prev,
1239 old_flag, RB_PAGE_NORMAL);
1242 static inline void rb_inc_page(struct buffer_page **bpage)
1244 struct list_head *p = rb_list_head((*bpage)->list.next);
1246 *bpage = list_entry(p, struct buffer_page, list);
1249 static struct buffer_page *
1250 rb_set_head_page(struct ring_buffer_per_cpu *cpu_buffer)
1252 struct buffer_page *head;
1253 struct buffer_page *page;
1254 struct list_head *list;
1257 if (RB_WARN_ON(cpu_buffer, !cpu_buffer->head_page))
1261 list = cpu_buffer->pages;
1262 if (RB_WARN_ON(cpu_buffer, rb_list_head(list->prev->next) != list))
1265 page = head = cpu_buffer->head_page;
1267 * It is possible that the writer moves the header behind
1268 * where we started, and we miss in one loop.
1269 * A second loop should grab the header, but we'll do
1270 * three loops just because I'm paranoid.
1272 for (i = 0; i < 3; i++) {
1274 if (rb_is_head_page(page, page->list.prev)) {
1275 cpu_buffer->head_page = page;
1279 } while (page != head);
1282 RB_WARN_ON(cpu_buffer, 1);
1287 static int rb_head_page_replace(struct buffer_page *old,
1288 struct buffer_page *new)
1290 unsigned long *ptr = (unsigned long *)&old->list.prev->next;
1294 val = *ptr & ~RB_FLAG_MASK;
1295 val |= RB_PAGE_HEAD;
1297 ret = cmpxchg(ptr, val, (unsigned long)&new->list);
1303 * rb_tail_page_update - move the tail page forward
1305 static void rb_tail_page_update(struct ring_buffer_per_cpu *cpu_buffer,
1306 struct buffer_page *tail_page,
1307 struct buffer_page *next_page)
1309 unsigned long old_entries;
1310 unsigned long old_write;
1313 * The tail page now needs to be moved forward.
1315 * We need to reset the tail page, but without messing
1316 * with possible erasing of data brought in by interrupts
1317 * that have moved the tail page and are currently on it.
1319 * We add a counter to the write field to denote this.
1321 old_write = local_add_return(RB_WRITE_INTCNT, &next_page->write);
1322 old_entries = local_add_return(RB_WRITE_INTCNT, &next_page->entries);
1324 local_inc(&cpu_buffer->pages_touched);
1326 * Just make sure we have seen our old_write and synchronize
1327 * with any interrupts that come in.
1332 * If the tail page is still the same as what we think
1333 * it is, then it is up to us to update the tail
1336 if (tail_page == READ_ONCE(cpu_buffer->tail_page)) {
1337 /* Zero the write counter */
1338 unsigned long val = old_write & ~RB_WRITE_MASK;
1339 unsigned long eval = old_entries & ~RB_WRITE_MASK;
1342 * This will only succeed if an interrupt did
1343 * not come in and change it. In which case, we
1344 * do not want to modify it.
1346 * We add (void) to let the compiler know that we do not care
1347 * about the return value of these functions. We use the
1348 * cmpxchg to only update if an interrupt did not already
1349 * do it for us. If the cmpxchg fails, we don't care.
1351 (void)local_cmpxchg(&next_page->write, old_write, val);
1352 (void)local_cmpxchg(&next_page->entries, old_entries, eval);
1355 * No need to worry about races with clearing out the commit.
1356 * it only can increment when a commit takes place. But that
1357 * only happens in the outer most nested commit.
1359 local_set(&next_page->page->commit, 0);
1361 /* Again, either we update tail_page or an interrupt does */
1362 (void)cmpxchg(&cpu_buffer->tail_page, tail_page, next_page);
1366 static int rb_check_bpage(struct ring_buffer_per_cpu *cpu_buffer,
1367 struct buffer_page *bpage)
1369 unsigned long val = (unsigned long)bpage;
1371 if (RB_WARN_ON(cpu_buffer, val & RB_FLAG_MASK))
1378 * rb_check_list - make sure a pointer to a list has the last bits zero
1380 static int rb_check_list(struct ring_buffer_per_cpu *cpu_buffer,
1381 struct list_head *list)
1383 if (RB_WARN_ON(cpu_buffer, rb_list_head(list->prev) != list->prev))
1385 if (RB_WARN_ON(cpu_buffer, rb_list_head(list->next) != list->next))
1391 * rb_check_pages - integrity check of buffer pages
1392 * @cpu_buffer: CPU buffer with pages to test
1394 * As a safety measure we check to make sure the data pages have not
1397 static int rb_check_pages(struct ring_buffer_per_cpu *cpu_buffer)
1399 struct list_head *head = cpu_buffer->pages;
1400 struct buffer_page *bpage, *tmp;
1402 /* Reset the head page if it exists */
1403 if (cpu_buffer->head_page)
1404 rb_set_head_page(cpu_buffer);
1406 rb_head_page_deactivate(cpu_buffer);
1408 if (RB_WARN_ON(cpu_buffer, head->next->prev != head))
1410 if (RB_WARN_ON(cpu_buffer, head->prev->next != head))
1413 if (rb_check_list(cpu_buffer, head))
1416 list_for_each_entry_safe(bpage, tmp, head, list) {
1417 if (RB_WARN_ON(cpu_buffer,
1418 bpage->list.next->prev != &bpage->list))
1420 if (RB_WARN_ON(cpu_buffer,
1421 bpage->list.prev->next != &bpage->list))
1423 if (rb_check_list(cpu_buffer, &bpage->list))
1427 rb_head_page_activate(cpu_buffer);
1432 static int __rb_allocate_pages(struct ring_buffer_per_cpu *cpu_buffer,
1433 long nr_pages, struct list_head *pages)
1435 struct buffer_page *bpage, *tmp;
1436 bool user_thread = current->mm != NULL;
1441 * Check if the available memory is there first.
1442 * Note, si_mem_available() only gives us a rough estimate of available
1443 * memory. It may not be accurate. But we don't care, we just want
1444 * to prevent doing any allocation when it is obvious that it is
1445 * not going to succeed.
1447 i = si_mem_available();
1452 * __GFP_RETRY_MAYFAIL flag makes sure that the allocation fails
1453 * gracefully without invoking oom-killer and the system is not
1456 mflags = GFP_KERNEL | __GFP_RETRY_MAYFAIL;
1459 * If a user thread allocates too much, and si_mem_available()
1460 * reports there's enough memory, even though there is not.
1461 * Make sure the OOM killer kills this thread. This can happen
1462 * even with RETRY_MAYFAIL because another task may be doing
1463 * an allocation after this task has taken all memory.
1464 * This is the task the OOM killer needs to take out during this
1465 * loop, even if it was triggered by an allocation somewhere else.
1468 set_current_oom_origin();
1469 for (i = 0; i < nr_pages; i++) {
1472 bpage = kzalloc_node(ALIGN(sizeof(*bpage), cache_line_size()),
1473 mflags, cpu_to_node(cpu_buffer->cpu));
1477 rb_check_bpage(cpu_buffer, bpage);
1479 list_add(&bpage->list, pages);
1481 page = alloc_pages_node(cpu_to_node(cpu_buffer->cpu), mflags, 0);
1484 bpage->page = page_address(page);
1485 rb_init_page(bpage->page);
1487 if (user_thread && fatal_signal_pending(current))
1491 clear_current_oom_origin();
1496 list_for_each_entry_safe(bpage, tmp, pages, list) {
1497 list_del_init(&bpage->list);
1498 free_buffer_page(bpage);
1501 clear_current_oom_origin();
1506 static int rb_allocate_pages(struct ring_buffer_per_cpu *cpu_buffer,
1507 unsigned long nr_pages)
1513 if (__rb_allocate_pages(cpu_buffer, nr_pages, &pages))
1517 * The ring buffer page list is a circular list that does not
1518 * start and end with a list head. All page list items point to
1521 cpu_buffer->pages = pages.next;
1524 cpu_buffer->nr_pages = nr_pages;
1526 rb_check_pages(cpu_buffer);
1531 static struct ring_buffer_per_cpu *
1532 rb_allocate_cpu_buffer(struct trace_buffer *buffer, long nr_pages, int cpu)
1534 struct ring_buffer_per_cpu *cpu_buffer;
1535 struct buffer_page *bpage;
1539 cpu_buffer = kzalloc_node(ALIGN(sizeof(*cpu_buffer), cache_line_size()),
1540 GFP_KERNEL, cpu_to_node(cpu));
1544 cpu_buffer->cpu = cpu;
1545 cpu_buffer->buffer = buffer;
1546 raw_spin_lock_init(&cpu_buffer->reader_lock);
1547 lockdep_set_class(&cpu_buffer->reader_lock, buffer->reader_lock_key);
1548 cpu_buffer->lock = (arch_spinlock_t)__ARCH_SPIN_LOCK_UNLOCKED;
1549 INIT_WORK(&cpu_buffer->update_pages_work, update_pages_handler);
1550 init_completion(&cpu_buffer->update_done);
1551 init_irq_work(&cpu_buffer->irq_work.work, rb_wake_up_waiters);
1552 init_waitqueue_head(&cpu_buffer->irq_work.waiters);
1553 init_waitqueue_head(&cpu_buffer->irq_work.full_waiters);
1555 bpage = kzalloc_node(ALIGN(sizeof(*bpage), cache_line_size()),
1556 GFP_KERNEL, cpu_to_node(cpu));
1558 goto fail_free_buffer;
1560 rb_check_bpage(cpu_buffer, bpage);
1562 cpu_buffer->reader_page = bpage;
1563 page = alloc_pages_node(cpu_to_node(cpu), GFP_KERNEL, 0);
1565 goto fail_free_reader;
1566 bpage->page = page_address(page);
1567 rb_init_page(bpage->page);
1569 INIT_LIST_HEAD(&cpu_buffer->reader_page->list);
1570 INIT_LIST_HEAD(&cpu_buffer->new_pages);
1572 ret = rb_allocate_pages(cpu_buffer, nr_pages);
1574 goto fail_free_reader;
1576 cpu_buffer->head_page
1577 = list_entry(cpu_buffer->pages, struct buffer_page, list);
1578 cpu_buffer->tail_page = cpu_buffer->commit_page = cpu_buffer->head_page;
1580 rb_head_page_activate(cpu_buffer);
1585 free_buffer_page(cpu_buffer->reader_page);
1592 static void rb_free_cpu_buffer(struct ring_buffer_per_cpu *cpu_buffer)
1594 struct list_head *head = cpu_buffer->pages;
1595 struct buffer_page *bpage, *tmp;
1597 free_buffer_page(cpu_buffer->reader_page);
1599 rb_head_page_deactivate(cpu_buffer);
1602 list_for_each_entry_safe(bpage, tmp, head, list) {
1603 list_del_init(&bpage->list);
1604 free_buffer_page(bpage);
1606 bpage = list_entry(head, struct buffer_page, list);
1607 free_buffer_page(bpage);
1614 * __ring_buffer_alloc - allocate a new ring_buffer
1615 * @size: the size in bytes per cpu that is needed.
1616 * @flags: attributes to set for the ring buffer.
1617 * @key: ring buffer reader_lock_key.
1619 * Currently the only flag that is available is the RB_FL_OVERWRITE
1620 * flag. This flag means that the buffer will overwrite old data
1621 * when the buffer wraps. If this flag is not set, the buffer will
1622 * drop data when the tail hits the head.
1624 struct trace_buffer *__ring_buffer_alloc(unsigned long size, unsigned flags,
1625 struct lock_class_key *key)
1627 struct trace_buffer *buffer;
1633 /* keep it in its own cache line */
1634 buffer = kzalloc(ALIGN(sizeof(*buffer), cache_line_size()),
1639 if (!zalloc_cpumask_var(&buffer->cpumask, GFP_KERNEL))
1640 goto fail_free_buffer;
1642 nr_pages = DIV_ROUND_UP(size, BUF_PAGE_SIZE);
1643 buffer->flags = flags;
1644 buffer->clock = trace_clock_local;
1645 buffer->reader_lock_key = key;
1647 init_irq_work(&buffer->irq_work.work, rb_wake_up_waiters);
1648 init_waitqueue_head(&buffer->irq_work.waiters);
1650 /* need at least two pages */
1654 buffer->cpus = nr_cpu_ids;
1656 bsize = sizeof(void *) * nr_cpu_ids;
1657 buffer->buffers = kzalloc(ALIGN(bsize, cache_line_size()),
1659 if (!buffer->buffers)
1660 goto fail_free_cpumask;
1662 cpu = raw_smp_processor_id();
1663 cpumask_set_cpu(cpu, buffer->cpumask);
1664 buffer->buffers[cpu] = rb_allocate_cpu_buffer(buffer, nr_pages, cpu);
1665 if (!buffer->buffers[cpu])
1666 goto fail_free_buffers;
1668 ret = cpuhp_state_add_instance(CPUHP_TRACE_RB_PREPARE, &buffer->node);
1670 goto fail_free_buffers;
1672 mutex_init(&buffer->mutex);
1677 for_each_buffer_cpu(buffer, cpu) {
1678 if (buffer->buffers[cpu])
1679 rb_free_cpu_buffer(buffer->buffers[cpu]);
1681 kfree(buffer->buffers);
1684 free_cpumask_var(buffer->cpumask);
1690 EXPORT_SYMBOL_GPL(__ring_buffer_alloc);
1693 * ring_buffer_free - free a ring buffer.
1694 * @buffer: the buffer to free.
1697 ring_buffer_free(struct trace_buffer *buffer)
1701 cpuhp_state_remove_instance(CPUHP_TRACE_RB_PREPARE, &buffer->node);
1703 for_each_buffer_cpu(buffer, cpu)
1704 rb_free_cpu_buffer(buffer->buffers[cpu]);
1706 kfree(buffer->buffers);
1707 free_cpumask_var(buffer->cpumask);
1711 EXPORT_SYMBOL_GPL(ring_buffer_free);
1713 void ring_buffer_set_clock(struct trace_buffer *buffer,
1716 buffer->clock = clock;
1719 void ring_buffer_set_time_stamp_abs(struct trace_buffer *buffer, bool abs)
1721 buffer->time_stamp_abs = abs;
1724 bool ring_buffer_time_stamp_abs(struct trace_buffer *buffer)
1726 return buffer->time_stamp_abs;
1729 static void rb_reset_cpu(struct ring_buffer_per_cpu *cpu_buffer);
1731 static inline unsigned long rb_page_entries(struct buffer_page *bpage)
1733 return local_read(&bpage->entries) & RB_WRITE_MASK;
1736 static inline unsigned long rb_page_write(struct buffer_page *bpage)
1738 return local_read(&bpage->write) & RB_WRITE_MASK;
1742 rb_remove_pages(struct ring_buffer_per_cpu *cpu_buffer, unsigned long nr_pages)
1744 struct list_head *tail_page, *to_remove, *next_page;
1745 struct buffer_page *to_remove_page, *tmp_iter_page;
1746 struct buffer_page *last_page, *first_page;
1747 unsigned long nr_removed;
1748 unsigned long head_bit;
1753 raw_spin_lock_irq(&cpu_buffer->reader_lock);
1754 atomic_inc(&cpu_buffer->record_disabled);
1756 * We don't race with the readers since we have acquired the reader
1757 * lock. We also don't race with writers after disabling recording.
1758 * This makes it easy to figure out the first and the last page to be
1759 * removed from the list. We unlink all the pages in between including
1760 * the first and last pages. This is done in a busy loop so that we
1761 * lose the least number of traces.
1762 * The pages are freed after we restart recording and unlock readers.
1764 tail_page = &cpu_buffer->tail_page->list;
1767 * tail page might be on reader page, we remove the next page
1768 * from the ring buffer
1770 if (cpu_buffer->tail_page == cpu_buffer->reader_page)
1771 tail_page = rb_list_head(tail_page->next);
1772 to_remove = tail_page;
1774 /* start of pages to remove */
1775 first_page = list_entry(rb_list_head(to_remove->next),
1776 struct buffer_page, list);
1778 for (nr_removed = 0; nr_removed < nr_pages; nr_removed++) {
1779 to_remove = rb_list_head(to_remove)->next;
1780 head_bit |= (unsigned long)to_remove & RB_PAGE_HEAD;
1783 next_page = rb_list_head(to_remove)->next;
1786 * Now we remove all pages between tail_page and next_page.
1787 * Make sure that we have head_bit value preserved for the
1790 tail_page->next = (struct list_head *)((unsigned long)next_page |
1792 next_page = rb_list_head(next_page);
1793 next_page->prev = tail_page;
1795 /* make sure pages points to a valid page in the ring buffer */
1796 cpu_buffer->pages = next_page;
1798 /* update head page */
1800 cpu_buffer->head_page = list_entry(next_page,
1801 struct buffer_page, list);
1804 * change read pointer to make sure any read iterators reset
1807 cpu_buffer->read = 0;
1809 /* pages are removed, resume tracing and then free the pages */
1810 atomic_dec(&cpu_buffer->record_disabled);
1811 raw_spin_unlock_irq(&cpu_buffer->reader_lock);
1813 RB_WARN_ON(cpu_buffer, list_empty(cpu_buffer->pages));
1815 /* last buffer page to remove */
1816 last_page = list_entry(rb_list_head(to_remove), struct buffer_page,
1818 tmp_iter_page = first_page;
1823 to_remove_page = tmp_iter_page;
1824 rb_inc_page(&tmp_iter_page);
1826 /* update the counters */
1827 page_entries = rb_page_entries(to_remove_page);
1830 * If something was added to this page, it was full
1831 * since it is not the tail page. So we deduct the
1832 * bytes consumed in ring buffer from here.
1833 * Increment overrun to account for the lost events.
1835 local_add(page_entries, &cpu_buffer->overrun);
1836 local_sub(BUF_PAGE_SIZE, &cpu_buffer->entries_bytes);
1840 * We have already removed references to this list item, just
1841 * free up the buffer_page and its page
1843 free_buffer_page(to_remove_page);
1846 } while (to_remove_page != last_page);
1848 RB_WARN_ON(cpu_buffer, nr_removed);
1850 return nr_removed == 0;
1854 rb_insert_pages(struct ring_buffer_per_cpu *cpu_buffer)
1856 struct list_head *pages = &cpu_buffer->new_pages;
1857 int retries, success;
1859 raw_spin_lock_irq(&cpu_buffer->reader_lock);
1861 * We are holding the reader lock, so the reader page won't be swapped
1862 * in the ring buffer. Now we are racing with the writer trying to
1863 * move head page and the tail page.
1864 * We are going to adapt the reader page update process where:
1865 * 1. We first splice the start and end of list of new pages between
1866 * the head page and its previous page.
1867 * 2. We cmpxchg the prev_page->next to point from head page to the
1868 * start of new pages list.
1869 * 3. Finally, we update the head->prev to the end of new list.
1871 * We will try this process 10 times, to make sure that we don't keep
1877 struct list_head *head_page, *prev_page, *r;
1878 struct list_head *last_page, *first_page;
1879 struct list_head *head_page_with_bit;
1881 head_page = &rb_set_head_page(cpu_buffer)->list;
1884 prev_page = head_page->prev;
1886 first_page = pages->next;
1887 last_page = pages->prev;
1889 head_page_with_bit = (struct list_head *)
1890 ((unsigned long)head_page | RB_PAGE_HEAD);
1892 last_page->next = head_page_with_bit;
1893 first_page->prev = prev_page;
1895 r = cmpxchg(&prev_page->next, head_page_with_bit, first_page);
1897 if (r == head_page_with_bit) {
1899 * yay, we replaced the page pointer to our new list,
1900 * now, we just have to update to head page's prev
1901 * pointer to point to end of list
1903 head_page->prev = last_page;
1910 INIT_LIST_HEAD(pages);
1912 * If we weren't successful in adding in new pages, warn and stop
1915 RB_WARN_ON(cpu_buffer, !success);
1916 raw_spin_unlock_irq(&cpu_buffer->reader_lock);
1918 /* free pages if they weren't inserted */
1920 struct buffer_page *bpage, *tmp;
1921 list_for_each_entry_safe(bpage, tmp, &cpu_buffer->new_pages,
1923 list_del_init(&bpage->list);
1924 free_buffer_page(bpage);
1930 static void rb_update_pages(struct ring_buffer_per_cpu *cpu_buffer)
1934 if (cpu_buffer->nr_pages_to_update > 0)
1935 success = rb_insert_pages(cpu_buffer);
1937 success = rb_remove_pages(cpu_buffer,
1938 -cpu_buffer->nr_pages_to_update);
1941 cpu_buffer->nr_pages += cpu_buffer->nr_pages_to_update;
1944 static void update_pages_handler(struct work_struct *work)
1946 struct ring_buffer_per_cpu *cpu_buffer = container_of(work,
1947 struct ring_buffer_per_cpu, update_pages_work);
1948 rb_update_pages(cpu_buffer);
1949 complete(&cpu_buffer->update_done);
1953 * ring_buffer_resize - resize the ring buffer
1954 * @buffer: the buffer to resize.
1955 * @size: the new size.
1956 * @cpu_id: the cpu buffer to resize
1958 * Minimum size is 2 * BUF_PAGE_SIZE.
1960 * Returns 0 on success and < 0 on failure.
1962 int ring_buffer_resize(struct trace_buffer *buffer, unsigned long size,
1965 struct ring_buffer_per_cpu *cpu_buffer;
1966 unsigned long nr_pages;
1970 * Always succeed at resizing a non-existent buffer:
1975 /* Make sure the requested buffer exists */
1976 if (cpu_id != RING_BUFFER_ALL_CPUS &&
1977 !cpumask_test_cpu(cpu_id, buffer->cpumask))
1980 nr_pages = DIV_ROUND_UP(size, BUF_PAGE_SIZE);
1982 /* we need a minimum of two pages */
1986 /* prevent another thread from changing buffer sizes */
1987 mutex_lock(&buffer->mutex);
1990 if (cpu_id == RING_BUFFER_ALL_CPUS) {
1992 * Don't succeed if resizing is disabled, as a reader might be
1993 * manipulating the ring buffer and is expecting a sane state while
1996 for_each_buffer_cpu(buffer, cpu) {
1997 cpu_buffer = buffer->buffers[cpu];
1998 if (atomic_read(&cpu_buffer->resize_disabled)) {
2000 goto out_err_unlock;
2004 /* calculate the pages to update */
2005 for_each_buffer_cpu(buffer, cpu) {
2006 cpu_buffer = buffer->buffers[cpu];
2008 cpu_buffer->nr_pages_to_update = nr_pages -
2009 cpu_buffer->nr_pages;
2011 * nothing more to do for removing pages or no update
2013 if (cpu_buffer->nr_pages_to_update <= 0)
2016 * to add pages, make sure all new pages can be
2017 * allocated without receiving ENOMEM
2019 INIT_LIST_HEAD(&cpu_buffer->new_pages);
2020 if (__rb_allocate_pages(cpu_buffer, cpu_buffer->nr_pages_to_update,
2021 &cpu_buffer->new_pages)) {
2022 /* not enough memory for new pages */
2030 * Fire off all the required work handlers
2031 * We can't schedule on offline CPUs, but it's not necessary
2032 * since we can change their buffer sizes without any race.
2034 for_each_buffer_cpu(buffer, cpu) {
2035 cpu_buffer = buffer->buffers[cpu];
2036 if (!cpu_buffer->nr_pages_to_update)
2039 /* Can't run something on an offline CPU. */
2040 if (!cpu_online(cpu)) {
2041 rb_update_pages(cpu_buffer);
2042 cpu_buffer->nr_pages_to_update = 0;
2044 schedule_work_on(cpu,
2045 &cpu_buffer->update_pages_work);
2049 /* wait for all the updates to complete */
2050 for_each_buffer_cpu(buffer, cpu) {
2051 cpu_buffer = buffer->buffers[cpu];
2052 if (!cpu_buffer->nr_pages_to_update)
2055 if (cpu_online(cpu))
2056 wait_for_completion(&cpu_buffer->update_done);
2057 cpu_buffer->nr_pages_to_update = 0;
2062 cpu_buffer = buffer->buffers[cpu_id];
2064 if (nr_pages == cpu_buffer->nr_pages)
2068 * Don't succeed if resizing is disabled, as a reader might be
2069 * manipulating the ring buffer and is expecting a sane state while
2072 if (atomic_read(&cpu_buffer->resize_disabled)) {
2074 goto out_err_unlock;
2077 cpu_buffer->nr_pages_to_update = nr_pages -
2078 cpu_buffer->nr_pages;
2080 INIT_LIST_HEAD(&cpu_buffer->new_pages);
2081 if (cpu_buffer->nr_pages_to_update > 0 &&
2082 __rb_allocate_pages(cpu_buffer, cpu_buffer->nr_pages_to_update,
2083 &cpu_buffer->new_pages)) {
2090 /* Can't run something on an offline CPU. */
2091 if (!cpu_online(cpu_id))
2092 rb_update_pages(cpu_buffer);
2094 schedule_work_on(cpu_id,
2095 &cpu_buffer->update_pages_work);
2096 wait_for_completion(&cpu_buffer->update_done);
2099 cpu_buffer->nr_pages_to_update = 0;
2105 * The ring buffer resize can happen with the ring buffer
2106 * enabled, so that the update disturbs the tracing as little
2107 * as possible. But if the buffer is disabled, we do not need
2108 * to worry about that, and we can take the time to verify
2109 * that the buffer is not corrupt.
2111 if (atomic_read(&buffer->record_disabled)) {
2112 atomic_inc(&buffer->record_disabled);
2114 * Even though the buffer was disabled, we must make sure
2115 * that it is truly disabled before calling rb_check_pages.
2116 * There could have been a race between checking
2117 * record_disable and incrementing it.
2120 for_each_buffer_cpu(buffer, cpu) {
2121 cpu_buffer = buffer->buffers[cpu];
2122 rb_check_pages(cpu_buffer);
2124 atomic_dec(&buffer->record_disabled);
2127 mutex_unlock(&buffer->mutex);
2131 for_each_buffer_cpu(buffer, cpu) {
2132 struct buffer_page *bpage, *tmp;
2134 cpu_buffer = buffer->buffers[cpu];
2135 cpu_buffer->nr_pages_to_update = 0;
2137 if (list_empty(&cpu_buffer->new_pages))
2140 list_for_each_entry_safe(bpage, tmp, &cpu_buffer->new_pages,
2142 list_del_init(&bpage->list);
2143 free_buffer_page(bpage);
2147 mutex_unlock(&buffer->mutex);
2150 EXPORT_SYMBOL_GPL(ring_buffer_resize);
2152 void ring_buffer_change_overwrite(struct trace_buffer *buffer, int val)
2154 mutex_lock(&buffer->mutex);
2156 buffer->flags |= RB_FL_OVERWRITE;
2158 buffer->flags &= ~RB_FL_OVERWRITE;
2159 mutex_unlock(&buffer->mutex);
2161 EXPORT_SYMBOL_GPL(ring_buffer_change_overwrite);
2163 static __always_inline void *__rb_page_index(struct buffer_page *bpage, unsigned index)
2165 return bpage->page->data + index;
2168 static __always_inline struct ring_buffer_event *
2169 rb_reader_event(struct ring_buffer_per_cpu *cpu_buffer)
2171 return __rb_page_index(cpu_buffer->reader_page,
2172 cpu_buffer->reader_page->read);
2175 static __always_inline unsigned rb_page_commit(struct buffer_page *bpage)
2177 return local_read(&bpage->page->commit);
2180 static struct ring_buffer_event *
2181 rb_iter_head_event(struct ring_buffer_iter *iter)
2183 struct ring_buffer_event *event;
2184 struct buffer_page *iter_head_page = iter->head_page;
2185 unsigned long commit;
2188 if (iter->head != iter->next_event)
2192 * When the writer goes across pages, it issues a cmpxchg which
2193 * is a mb(), which will synchronize with the rmb here.
2194 * (see rb_tail_page_update() and __rb_reserve_next())
2196 commit = rb_page_commit(iter_head_page);
2198 event = __rb_page_index(iter_head_page, iter->head);
2199 length = rb_event_length(event);
2202 * READ_ONCE() doesn't work on functions and we don't want the
2203 * compiler doing any crazy optimizations with length.
2207 if ((iter->head + length) > commit || length > BUF_MAX_DATA_SIZE)
2208 /* Writer corrupted the read? */
2211 memcpy(iter->event, event, length);
2213 * If the page stamp is still the same after this rmb() then the
2214 * event was safely copied without the writer entering the page.
2218 /* Make sure the page didn't change since we read this */
2219 if (iter->page_stamp != iter_head_page->page->time_stamp ||
2220 commit > rb_page_commit(iter_head_page))
2223 iter->next_event = iter->head + length;
2226 /* Reset to the beginning */
2227 iter->page_stamp = iter->read_stamp = iter->head_page->page->time_stamp;
2229 iter->next_event = 0;
2230 iter->missed_events = 1;
2234 /* Size is determined by what has been committed */
2235 static __always_inline unsigned rb_page_size(struct buffer_page *bpage)
2237 return rb_page_commit(bpage);
2240 static __always_inline unsigned
2241 rb_commit_index(struct ring_buffer_per_cpu *cpu_buffer)
2243 return rb_page_commit(cpu_buffer->commit_page);
2246 static __always_inline unsigned
2247 rb_event_index(struct ring_buffer_event *event)
2249 unsigned long addr = (unsigned long)event;
2251 return (addr & ~PAGE_MASK) - BUF_PAGE_HDR_SIZE;
2254 static void rb_inc_iter(struct ring_buffer_iter *iter)
2256 struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
2259 * The iterator could be on the reader page (it starts there).
2260 * But the head could have moved, since the reader was
2261 * found. Check for this case and assign the iterator
2262 * to the head page instead of next.
2264 if (iter->head_page == cpu_buffer->reader_page)
2265 iter->head_page = rb_set_head_page(cpu_buffer);
2267 rb_inc_page(&iter->head_page);
2269 iter->page_stamp = iter->read_stamp = iter->head_page->page->time_stamp;
2271 iter->next_event = 0;
2275 * rb_handle_head_page - writer hit the head page
2277 * Returns: +1 to retry page
2282 rb_handle_head_page(struct ring_buffer_per_cpu *cpu_buffer,
2283 struct buffer_page *tail_page,
2284 struct buffer_page *next_page)
2286 struct buffer_page *new_head;
2291 entries = rb_page_entries(next_page);
2294 * The hard part is here. We need to move the head
2295 * forward, and protect against both readers on
2296 * other CPUs and writers coming in via interrupts.
2298 type = rb_head_page_set_update(cpu_buffer, next_page, tail_page,
2302 * type can be one of four:
2303 * NORMAL - an interrupt already moved it for us
2304 * HEAD - we are the first to get here.
2305 * UPDATE - we are the interrupt interrupting
2307 * MOVED - a reader on another CPU moved the next
2308 * pointer to its reader page. Give up
2315 * We changed the head to UPDATE, thus
2316 * it is our responsibility to update
2319 local_add(entries, &cpu_buffer->overrun);
2320 local_sub(BUF_PAGE_SIZE, &cpu_buffer->entries_bytes);
2323 * The entries will be zeroed out when we move the
2327 /* still more to do */
2330 case RB_PAGE_UPDATE:
2332 * This is an interrupt that interrupt the
2333 * previous update. Still more to do.
2336 case RB_PAGE_NORMAL:
2338 * An interrupt came in before the update
2339 * and processed this for us.
2340 * Nothing left to do.
2345 * The reader is on another CPU and just did
2346 * a swap with our next_page.
2351 RB_WARN_ON(cpu_buffer, 1); /* WTF??? */
2356 * Now that we are here, the old head pointer is
2357 * set to UPDATE. This will keep the reader from
2358 * swapping the head page with the reader page.
2359 * The reader (on another CPU) will spin till
2362 * We just need to protect against interrupts
2363 * doing the job. We will set the next pointer
2364 * to HEAD. After that, we set the old pointer
2365 * to NORMAL, but only if it was HEAD before.
2366 * otherwise we are an interrupt, and only
2367 * want the outer most commit to reset it.
2369 new_head = next_page;
2370 rb_inc_page(&new_head);
2372 ret = rb_head_page_set_head(cpu_buffer, new_head, next_page,
2376 * Valid returns are:
2377 * HEAD - an interrupt came in and already set it.
2378 * NORMAL - One of two things:
2379 * 1) We really set it.
2380 * 2) A bunch of interrupts came in and moved
2381 * the page forward again.
2385 case RB_PAGE_NORMAL:
2389 RB_WARN_ON(cpu_buffer, 1);
2394 * It is possible that an interrupt came in,
2395 * set the head up, then more interrupts came in
2396 * and moved it again. When we get back here,
2397 * the page would have been set to NORMAL but we
2398 * just set it back to HEAD.
2400 * How do you detect this? Well, if that happened
2401 * the tail page would have moved.
2403 if (ret == RB_PAGE_NORMAL) {
2404 struct buffer_page *buffer_tail_page;
2406 buffer_tail_page = READ_ONCE(cpu_buffer->tail_page);
2408 * If the tail had moved passed next, then we need
2409 * to reset the pointer.
2411 if (buffer_tail_page != tail_page &&
2412 buffer_tail_page != next_page)
2413 rb_head_page_set_normal(cpu_buffer, new_head,
2419 * If this was the outer most commit (the one that
2420 * changed the original pointer from HEAD to UPDATE),
2421 * then it is up to us to reset it to NORMAL.
2423 if (type == RB_PAGE_HEAD) {
2424 ret = rb_head_page_set_normal(cpu_buffer, next_page,
2427 if (RB_WARN_ON(cpu_buffer,
2428 ret != RB_PAGE_UPDATE))
2436 rb_reset_tail(struct ring_buffer_per_cpu *cpu_buffer,
2437 unsigned long tail, struct rb_event_info *info)
2439 struct buffer_page *tail_page = info->tail_page;
2440 struct ring_buffer_event *event;
2441 unsigned long length = info->length;
2444 * Only the event that crossed the page boundary
2445 * must fill the old tail_page with padding.
2447 if (tail >= BUF_PAGE_SIZE) {
2449 * If the page was filled, then we still need
2450 * to update the real_end. Reset it to zero
2451 * and the reader will ignore it.
2453 if (tail == BUF_PAGE_SIZE)
2454 tail_page->real_end = 0;
2456 local_sub(length, &tail_page->write);
2460 event = __rb_page_index(tail_page, tail);
2462 /* account for padding bytes */
2463 local_add(BUF_PAGE_SIZE - tail, &cpu_buffer->entries_bytes);
2466 * Save the original length to the meta data.
2467 * This will be used by the reader to add lost event
2470 tail_page->real_end = tail;
2473 * If this event is bigger than the minimum size, then
2474 * we need to be careful that we don't subtract the
2475 * write counter enough to allow another writer to slip
2477 * We put in a discarded commit instead, to make sure
2478 * that this space is not used again.
2480 * If we are less than the minimum size, we don't need to
2483 if (tail > (BUF_PAGE_SIZE - RB_EVNT_MIN_SIZE)) {
2484 /* No room for any events */
2486 /* Mark the rest of the page with padding */
2487 rb_event_set_padding(event);
2489 /* Set the write back to the previous setting */
2490 local_sub(length, &tail_page->write);
2494 /* Put in a discarded event */
2495 event->array[0] = (BUF_PAGE_SIZE - tail) - RB_EVNT_HDR_SIZE;
2496 event->type_len = RINGBUF_TYPE_PADDING;
2497 /* time delta must be non zero */
2498 event->time_delta = 1;
2500 /* Set write to end of buffer */
2501 length = (tail + length) - BUF_PAGE_SIZE;
2502 local_sub(length, &tail_page->write);
2505 static inline void rb_end_commit(struct ring_buffer_per_cpu *cpu_buffer);
2508 * This is the slow path, force gcc not to inline it.
2510 static noinline struct ring_buffer_event *
2511 rb_move_tail(struct ring_buffer_per_cpu *cpu_buffer,
2512 unsigned long tail, struct rb_event_info *info)
2514 struct buffer_page *tail_page = info->tail_page;
2515 struct buffer_page *commit_page = cpu_buffer->commit_page;
2516 struct trace_buffer *buffer = cpu_buffer->buffer;
2517 struct buffer_page *next_page;
2520 next_page = tail_page;
2522 rb_inc_page(&next_page);
2525 * If for some reason, we had an interrupt storm that made
2526 * it all the way around the buffer, bail, and warn
2529 if (unlikely(next_page == commit_page)) {
2530 local_inc(&cpu_buffer->commit_overrun);
2535 * This is where the fun begins!
2537 * We are fighting against races between a reader that
2538 * could be on another CPU trying to swap its reader
2539 * page with the buffer head.
2541 * We are also fighting against interrupts coming in and
2542 * moving the head or tail on us as well.
2544 * If the next page is the head page then we have filled
2545 * the buffer, unless the commit page is still on the
2548 if (rb_is_head_page(next_page, &tail_page->list)) {
2551 * If the commit is not on the reader page, then
2552 * move the header page.
2554 if (!rb_is_reader_page(cpu_buffer->commit_page)) {
2556 * If we are not in overwrite mode,
2557 * this is easy, just stop here.
2559 if (!(buffer->flags & RB_FL_OVERWRITE)) {
2560 local_inc(&cpu_buffer->dropped_events);
2564 ret = rb_handle_head_page(cpu_buffer,
2573 * We need to be careful here too. The
2574 * commit page could still be on the reader
2575 * page. We could have a small buffer, and
2576 * have filled up the buffer with events
2577 * from interrupts and such, and wrapped.
2579 * Note, if the tail page is also on the
2580 * reader_page, we let it move out.
2582 if (unlikely((cpu_buffer->commit_page !=
2583 cpu_buffer->tail_page) &&
2584 (cpu_buffer->commit_page ==
2585 cpu_buffer->reader_page))) {
2586 local_inc(&cpu_buffer->commit_overrun);
2592 rb_tail_page_update(cpu_buffer, tail_page, next_page);
2596 rb_reset_tail(cpu_buffer, tail, info);
2598 /* Commit what we have for now. */
2599 rb_end_commit(cpu_buffer);
2600 /* rb_end_commit() decs committing */
2601 local_inc(&cpu_buffer->committing);
2603 /* fail and let the caller try again */
2604 return ERR_PTR(-EAGAIN);
2608 rb_reset_tail(cpu_buffer, tail, info);
2614 static struct ring_buffer_event *
2615 rb_add_time_stamp(struct ring_buffer_event *event, u64 delta, bool abs)
2618 event->type_len = RINGBUF_TYPE_TIME_STAMP;
2620 event->type_len = RINGBUF_TYPE_TIME_EXTEND;
2622 /* Not the first event on the page, or not delta? */
2623 if (abs || rb_event_index(event)) {
2624 event->time_delta = delta & TS_MASK;
2625 event->array[0] = delta >> TS_SHIFT;
2627 /* nope, just zero it */
2628 event->time_delta = 0;
2629 event->array[0] = 0;
2632 return skip_time_extend(event);
2635 #ifndef CONFIG_HAVE_UNSTABLE_SCHED_CLOCK
2636 static inline bool sched_clock_stable(void)
2643 rb_check_timestamp(struct ring_buffer_per_cpu *cpu_buffer,
2644 struct rb_event_info *info)
2648 WARN_ONCE(1, "Delta way too big! %llu ts=%llu before=%llu after=%llu write stamp=%llu\n%s",
2649 (unsigned long long)info->delta,
2650 (unsigned long long)info->ts,
2651 (unsigned long long)info->before,
2652 (unsigned long long)info->after,
2653 (unsigned long long)(rb_time_read(&cpu_buffer->write_stamp, &write_stamp) ? write_stamp : 0),
2654 sched_clock_stable() ? "" :
2655 "If you just came from a suspend/resume,\n"
2656 "please switch to the trace global clock:\n"
2657 " echo global > /sys/kernel/debug/tracing/trace_clock\n"
2658 "or add trace_clock=global to the kernel command line\n");
2661 static void rb_add_timestamp(struct ring_buffer_per_cpu *cpu_buffer,
2662 struct ring_buffer_event **event,
2663 struct rb_event_info *info,
2665 unsigned int *length)
2667 bool abs = info->add_timestamp &
2668 (RB_ADD_STAMP_FORCE | RB_ADD_STAMP_ABSOLUTE);
2670 if (unlikely(info->delta > (1ULL << 59))) {
2671 /* did the clock go backwards */
2672 if (info->before == info->after && info->before > info->ts) {
2673 /* not interrupted */
2677 * This is possible with a recalibrating of the TSC.
2678 * Do not produce a call stack, but just report it.
2682 pr_warn("Ring buffer clock went backwards: %llu -> %llu\n",
2683 info->before, info->ts);
2686 rb_check_timestamp(cpu_buffer, info);
2690 *event = rb_add_time_stamp(*event, info->delta, abs);
2691 *length -= RB_LEN_TIME_EXTEND;
2696 * rb_update_event - update event type and data
2697 * @cpu_buffer: The per cpu buffer of the @event
2698 * @event: the event to update
2699 * @info: The info to update the @event with (contains length and delta)
2701 * Update the type and data fields of the @event. The length
2702 * is the actual size that is written to the ring buffer,
2703 * and with this, we can determine what to place into the
2707 rb_update_event(struct ring_buffer_per_cpu *cpu_buffer,
2708 struct ring_buffer_event *event,
2709 struct rb_event_info *info)
2711 unsigned length = info->length;
2712 u64 delta = info->delta;
2715 * If we need to add a timestamp, then we
2716 * add it to the start of the reserved space.
2718 if (unlikely(info->add_timestamp))
2719 rb_add_timestamp(cpu_buffer, &event, info, &delta, &length);
2721 event->time_delta = delta;
2722 length -= RB_EVNT_HDR_SIZE;
2723 if (length > RB_MAX_SMALL_DATA || RB_FORCE_8BYTE_ALIGNMENT) {
2724 event->type_len = 0;
2725 event->array[0] = length;
2727 event->type_len = DIV_ROUND_UP(length, RB_ALIGNMENT);
2730 static unsigned rb_calculate_event_length(unsigned length)
2732 struct ring_buffer_event event; /* Used only for sizeof array */
2734 /* zero length can cause confusions */
2738 if (length > RB_MAX_SMALL_DATA || RB_FORCE_8BYTE_ALIGNMENT)
2739 length += sizeof(event.array[0]);
2741 length += RB_EVNT_HDR_SIZE;
2742 length = ALIGN(length, RB_ARCH_ALIGNMENT);
2745 * In case the time delta is larger than the 27 bits for it
2746 * in the header, we need to add a timestamp. If another
2747 * event comes in when trying to discard this one to increase
2748 * the length, then the timestamp will be added in the allocated
2749 * space of this event. If length is bigger than the size needed
2750 * for the TIME_EXTEND, then padding has to be used. The events
2751 * length must be either RB_LEN_TIME_EXTEND, or greater than or equal
2752 * to RB_LEN_TIME_EXTEND + 8, as 8 is the minimum size for padding.
2753 * As length is a multiple of 4, we only need to worry if it
2754 * is 12 (RB_LEN_TIME_EXTEND + 4).
2756 if (length == RB_LEN_TIME_EXTEND + RB_ALIGNMENT)
2757 length += RB_ALIGNMENT;
2762 static u64 rb_time_delta(struct ring_buffer_event *event)
2764 switch (event->type_len) {
2765 case RINGBUF_TYPE_PADDING:
2768 case RINGBUF_TYPE_TIME_EXTEND:
2769 return ring_buffer_event_time_stamp(event);
2771 case RINGBUF_TYPE_TIME_STAMP:
2774 case RINGBUF_TYPE_DATA:
2775 return event->time_delta;
2782 rb_try_to_discard(struct ring_buffer_per_cpu *cpu_buffer,
2783 struct ring_buffer_event *event)
2785 unsigned long new_index, old_index;
2786 struct buffer_page *bpage;
2787 unsigned long index;
2792 new_index = rb_event_index(event);
2793 old_index = new_index + rb_event_ts_length(event);
2794 addr = (unsigned long)event;
2797 bpage = READ_ONCE(cpu_buffer->tail_page);
2799 delta = rb_time_delta(event);
2801 if (!rb_time_read(&cpu_buffer->write_stamp, &write_stamp))
2804 /* Make sure the write stamp is read before testing the location */
2807 if (bpage->page == (void *)addr && rb_page_write(bpage) == old_index) {
2808 unsigned long write_mask =
2809 local_read(&bpage->write) & ~RB_WRITE_MASK;
2810 unsigned long event_length = rb_event_length(event);
2812 /* Something came in, can't discard */
2813 if (!rb_time_cmpxchg(&cpu_buffer->write_stamp,
2814 write_stamp, write_stamp - delta))
2818 * If an event were to come in now, it would see that the
2819 * write_stamp and the before_stamp are different, and assume
2820 * that this event just added itself before updating
2821 * the write stamp. The interrupting event will fix the
2822 * write stamp for us, and use the before stamp as its delta.
2826 * This is on the tail page. It is possible that
2827 * a write could come in and move the tail page
2828 * and write to the next page. That is fine
2829 * because we just shorten what is on this page.
2831 old_index += write_mask;
2832 new_index += write_mask;
2833 index = local_cmpxchg(&bpage->write, old_index, new_index);
2834 if (index == old_index) {
2835 /* update counters */
2836 local_sub(event_length, &cpu_buffer->entries_bytes);
2841 /* could not discard */
2845 static void rb_start_commit(struct ring_buffer_per_cpu *cpu_buffer)
2847 local_inc(&cpu_buffer->committing);
2848 local_inc(&cpu_buffer->commits);
2851 static __always_inline void
2852 rb_set_commit_to_write(struct ring_buffer_per_cpu *cpu_buffer)
2854 unsigned long max_count;
2857 * We only race with interrupts and NMIs on this CPU.
2858 * If we own the commit event, then we can commit
2859 * all others that interrupted us, since the interruptions
2860 * are in stack format (they finish before they come
2861 * back to us). This allows us to do a simple loop to
2862 * assign the commit to the tail.
2865 max_count = cpu_buffer->nr_pages * 100;
2867 while (cpu_buffer->commit_page != READ_ONCE(cpu_buffer->tail_page)) {
2868 if (RB_WARN_ON(cpu_buffer, !(--max_count)))
2870 if (RB_WARN_ON(cpu_buffer,
2871 rb_is_reader_page(cpu_buffer->tail_page)))
2873 local_set(&cpu_buffer->commit_page->page->commit,
2874 rb_page_write(cpu_buffer->commit_page));
2875 rb_inc_page(&cpu_buffer->commit_page);
2876 /* add barrier to keep gcc from optimizing too much */
2879 while (rb_commit_index(cpu_buffer) !=
2880 rb_page_write(cpu_buffer->commit_page)) {
2882 local_set(&cpu_buffer->commit_page->page->commit,
2883 rb_page_write(cpu_buffer->commit_page));
2884 RB_WARN_ON(cpu_buffer,
2885 local_read(&cpu_buffer->commit_page->page->commit) &
2890 /* again, keep gcc from optimizing */
2894 * If an interrupt came in just after the first while loop
2895 * and pushed the tail page forward, we will be left with
2896 * a dangling commit that will never go forward.
2898 if (unlikely(cpu_buffer->commit_page != READ_ONCE(cpu_buffer->tail_page)))
2902 static __always_inline void rb_end_commit(struct ring_buffer_per_cpu *cpu_buffer)
2904 unsigned long commits;
2906 if (RB_WARN_ON(cpu_buffer,
2907 !local_read(&cpu_buffer->committing)))
2911 commits = local_read(&cpu_buffer->commits);
2912 /* synchronize with interrupts */
2914 if (local_read(&cpu_buffer->committing) == 1)
2915 rb_set_commit_to_write(cpu_buffer);
2917 local_dec(&cpu_buffer->committing);
2919 /* synchronize with interrupts */
2923 * Need to account for interrupts coming in between the
2924 * updating of the commit page and the clearing of the
2925 * committing counter.
2927 if (unlikely(local_read(&cpu_buffer->commits) != commits) &&
2928 !local_read(&cpu_buffer->committing)) {
2929 local_inc(&cpu_buffer->committing);
2934 static inline void rb_event_discard(struct ring_buffer_event *event)
2936 if (extended_time(event))
2937 event = skip_time_extend(event);
2939 /* array[0] holds the actual length for the discarded event */
2940 event->array[0] = rb_event_data_length(event) - RB_EVNT_HDR_SIZE;
2941 event->type_len = RINGBUF_TYPE_PADDING;
2942 /* time delta must be non zero */
2943 if (!event->time_delta)
2944 event->time_delta = 1;
2947 static void rb_commit(struct ring_buffer_per_cpu *cpu_buffer,
2948 struct ring_buffer_event *event)
2950 local_inc(&cpu_buffer->entries);
2951 rb_end_commit(cpu_buffer);
2954 static __always_inline void
2955 rb_wakeups(struct trace_buffer *buffer, struct ring_buffer_per_cpu *cpu_buffer)
2961 if (buffer->irq_work.waiters_pending) {
2962 buffer->irq_work.waiters_pending = false;
2963 /* irq_work_queue() supplies it's own memory barriers */
2964 irq_work_queue(&buffer->irq_work.work);
2967 if (cpu_buffer->irq_work.waiters_pending) {
2968 cpu_buffer->irq_work.waiters_pending = false;
2969 /* irq_work_queue() supplies it's own memory barriers */
2970 irq_work_queue(&cpu_buffer->irq_work.work);
2973 if (cpu_buffer->last_pages_touch == local_read(&cpu_buffer->pages_touched))
2976 if (cpu_buffer->reader_page == cpu_buffer->commit_page)
2979 if (!cpu_buffer->irq_work.full_waiters_pending)
2982 cpu_buffer->last_pages_touch = local_read(&cpu_buffer->pages_touched);
2984 full = cpu_buffer->shortest_full;
2985 nr_pages = cpu_buffer->nr_pages;
2986 dirty = ring_buffer_nr_dirty_pages(buffer, cpu_buffer->cpu);
2987 if (full && nr_pages && (dirty * 100) <= full * nr_pages)
2990 cpu_buffer->irq_work.wakeup_full = true;
2991 cpu_buffer->irq_work.full_waiters_pending = false;
2992 /* irq_work_queue() supplies it's own memory barriers */
2993 irq_work_queue(&cpu_buffer->irq_work.work);
2996 #ifdef CONFIG_RING_BUFFER_RECORD_RECURSION
2997 # define do_ring_buffer_record_recursion() \
2998 do_ftrace_record_recursion(_THIS_IP_, _RET_IP_)
3000 # define do_ring_buffer_record_recursion() do { } while (0)
3004 * The lock and unlock are done within a preempt disable section.
3005 * The current_context per_cpu variable can only be modified
3006 * by the current task between lock and unlock. But it can
3007 * be modified more than once via an interrupt. To pass this
3008 * information from the lock to the unlock without having to
3009 * access the 'in_interrupt()' functions again (which do show
3010 * a bit of overhead in something as critical as function tracing,
3011 * we use a bitmask trick.
3013 * bit 1 = NMI context
3014 * bit 2 = IRQ context
3015 * bit 3 = SoftIRQ context
3016 * bit 4 = normal context.
3018 * This works because this is the order of contexts that can
3019 * preempt other contexts. A SoftIRQ never preempts an IRQ
3022 * When the context is determined, the corresponding bit is
3023 * checked and set (if it was set, then a recursion of that context
3026 * On unlock, we need to clear this bit. To do so, just subtract
3027 * 1 from the current_context and AND it to itself.
3031 * 101 & 100 = 100 (clearing bit zero)
3034 * 1010 & 1001 = 1000 (clearing bit 1)
3036 * The least significant bit can be cleared this way, and it
3037 * just so happens that it is the same bit corresponding to
3038 * the current context.
3040 * Now the TRANSITION bit breaks the above slightly. The TRANSITION bit
3041 * is set when a recursion is detected at the current context, and if
3042 * the TRANSITION bit is already set, it will fail the recursion.
3043 * This is needed because there's a lag between the changing of
3044 * interrupt context and updating the preempt count. In this case,
3045 * a false positive will be found. To handle this, one extra recursion
3046 * is allowed, and this is done by the TRANSITION bit. If the TRANSITION
3047 * bit is already set, then it is considered a recursion and the function
3048 * ends. Otherwise, the TRANSITION bit is set, and that bit is returned.
3050 * On the trace_recursive_unlock(), the TRANSITION bit will be the first
3051 * to be cleared. Even if it wasn't the context that set it. That is,
3052 * if an interrupt comes in while NORMAL bit is set and the ring buffer
3053 * is called before preempt_count() is updated, since the check will
3054 * be on the NORMAL bit, the TRANSITION bit will then be set. If an
3055 * NMI then comes in, it will set the NMI bit, but when the NMI code
3056 * does the trace_recursive_unlock() it will clear the TRANSTION bit
3057 * and leave the NMI bit set. But this is fine, because the interrupt
3058 * code that set the TRANSITION bit will then clear the NMI bit when it
3059 * calls trace_recursive_unlock(). If another NMI comes in, it will
3060 * set the TRANSITION bit and continue.
3062 * Note: The TRANSITION bit only handles a single transition between context.
3065 static __always_inline int
3066 trace_recursive_lock(struct ring_buffer_per_cpu *cpu_buffer)
3068 unsigned int val = cpu_buffer->current_context;
3069 unsigned long pc = preempt_count();
3072 if (!(pc & (NMI_MASK | HARDIRQ_MASK | SOFTIRQ_OFFSET)))
3073 bit = RB_CTX_NORMAL;
3075 bit = pc & NMI_MASK ? RB_CTX_NMI :
3076 pc & HARDIRQ_MASK ? RB_CTX_IRQ : RB_CTX_SOFTIRQ;
3078 if (unlikely(val & (1 << (bit + cpu_buffer->nest)))) {
3080 * It is possible that this was called by transitioning
3081 * between interrupt context, and preempt_count() has not
3082 * been updated yet. In this case, use the TRANSITION bit.
3084 bit = RB_CTX_TRANSITION;
3085 if (val & (1 << (bit + cpu_buffer->nest))) {
3086 do_ring_buffer_record_recursion();
3091 val |= (1 << (bit + cpu_buffer->nest));
3092 cpu_buffer->current_context = val;
3097 static __always_inline void
3098 trace_recursive_unlock(struct ring_buffer_per_cpu *cpu_buffer)
3100 cpu_buffer->current_context &=
3101 cpu_buffer->current_context - (1 << cpu_buffer->nest);
3104 /* The recursive locking above uses 5 bits */
3105 #define NESTED_BITS 5
3108 * ring_buffer_nest_start - Allow to trace while nested
3109 * @buffer: The ring buffer to modify
3111 * The ring buffer has a safety mechanism to prevent recursion.
3112 * But there may be a case where a trace needs to be done while
3113 * tracing something else. In this case, calling this function
3114 * will allow this function to nest within a currently active
3115 * ring_buffer_lock_reserve().
3117 * Call this function before calling another ring_buffer_lock_reserve() and
3118 * call ring_buffer_nest_end() after the nested ring_buffer_unlock_commit().
3120 void ring_buffer_nest_start(struct trace_buffer *buffer)
3122 struct ring_buffer_per_cpu *cpu_buffer;
3125 /* Enabled by ring_buffer_nest_end() */
3126 preempt_disable_notrace();
3127 cpu = raw_smp_processor_id();
3128 cpu_buffer = buffer->buffers[cpu];
3129 /* This is the shift value for the above recursive locking */
3130 cpu_buffer->nest += NESTED_BITS;
3134 * ring_buffer_nest_end - Allow to trace while nested
3135 * @buffer: The ring buffer to modify
3137 * Must be called after ring_buffer_nest_start() and after the
3138 * ring_buffer_unlock_commit().
3140 void ring_buffer_nest_end(struct trace_buffer *buffer)
3142 struct ring_buffer_per_cpu *cpu_buffer;
3145 /* disabled by ring_buffer_nest_start() */
3146 cpu = raw_smp_processor_id();
3147 cpu_buffer = buffer->buffers[cpu];
3148 /* This is the shift value for the above recursive locking */
3149 cpu_buffer->nest -= NESTED_BITS;
3150 preempt_enable_notrace();
3154 * ring_buffer_unlock_commit - commit a reserved
3155 * @buffer: The buffer to commit to
3156 * @event: The event pointer to commit.
3158 * This commits the data to the ring buffer, and releases any locks held.
3160 * Must be paired with ring_buffer_lock_reserve.
3162 int ring_buffer_unlock_commit(struct trace_buffer *buffer,
3163 struct ring_buffer_event *event)
3165 struct ring_buffer_per_cpu *cpu_buffer;
3166 int cpu = raw_smp_processor_id();
3168 cpu_buffer = buffer->buffers[cpu];
3170 rb_commit(cpu_buffer, event);
3172 rb_wakeups(buffer, cpu_buffer);
3174 trace_recursive_unlock(cpu_buffer);
3176 preempt_enable_notrace();
3180 EXPORT_SYMBOL_GPL(ring_buffer_unlock_commit);
3182 /* Special value to validate all deltas on a page. */
3183 #define CHECK_FULL_PAGE 1L
3185 #ifdef CONFIG_RING_BUFFER_VALIDATE_TIME_DELTAS
3186 static void dump_buffer_page(struct buffer_data_page *bpage,
3187 struct rb_event_info *info,
3190 struct ring_buffer_event *event;
3194 ts = bpage->time_stamp;
3195 pr_warn(" [%lld] PAGE TIME STAMP\n", ts);
3197 for (e = 0; e < tail; e += rb_event_length(event)) {
3199 event = (struct ring_buffer_event *)(bpage->data + e);
3201 switch (event->type_len) {
3203 case RINGBUF_TYPE_TIME_EXTEND:
3204 delta = ring_buffer_event_time_stamp(event);
3206 pr_warn(" [%lld] delta:%lld TIME EXTEND\n", ts, delta);
3209 case RINGBUF_TYPE_TIME_STAMP:
3210 delta = ring_buffer_event_time_stamp(event);
3212 pr_warn(" [%lld] absolute:%lld TIME STAMP\n", ts, delta);
3215 case RINGBUF_TYPE_PADDING:
3216 ts += event->time_delta;
3217 pr_warn(" [%lld] delta:%d PADDING\n", ts, event->time_delta);
3220 case RINGBUF_TYPE_DATA:
3221 ts += event->time_delta;
3222 pr_warn(" [%lld] delta:%d\n", ts, event->time_delta);
3231 static DEFINE_PER_CPU(atomic_t, checking);
3232 static atomic_t ts_dump;
3235 * Check if the current event time stamp matches the deltas on
3238 static void check_buffer(struct ring_buffer_per_cpu *cpu_buffer,
3239 struct rb_event_info *info,
3242 struct ring_buffer_event *event;
3243 struct buffer_data_page *bpage;
3248 bpage = info->tail_page->page;
3250 if (tail == CHECK_FULL_PAGE) {
3252 tail = local_read(&bpage->commit);
3253 } else if (info->add_timestamp &
3254 (RB_ADD_STAMP_FORCE | RB_ADD_STAMP_ABSOLUTE)) {
3255 /* Ignore events with absolute time stamps */
3260 * Do not check the first event (skip possible extends too).
3261 * Also do not check if previous events have not been committed.
3263 if (tail <= 8 || tail > local_read(&bpage->commit))
3267 * If this interrupted another event,
3269 if (atomic_inc_return(this_cpu_ptr(&checking)) != 1)
3272 ts = bpage->time_stamp;
3274 for (e = 0; e < tail; e += rb_event_length(event)) {
3276 event = (struct ring_buffer_event *)(bpage->data + e);
3278 switch (event->type_len) {
3280 case RINGBUF_TYPE_TIME_EXTEND:
3281 delta = ring_buffer_event_time_stamp(event);
3285 case RINGBUF_TYPE_TIME_STAMP:
3286 delta = ring_buffer_event_time_stamp(event);
3290 case RINGBUF_TYPE_PADDING:
3291 if (event->time_delta == 1)
3294 case RINGBUF_TYPE_DATA:
3295 ts += event->time_delta;
3299 RB_WARN_ON(cpu_buffer, 1);
3302 if ((full && ts > info->ts) ||
3303 (!full && ts + info->delta != info->ts)) {
3304 /* If another report is happening, ignore this one */
3305 if (atomic_inc_return(&ts_dump) != 1) {
3306 atomic_dec(&ts_dump);
3309 atomic_inc(&cpu_buffer->record_disabled);
3310 pr_warn("[CPU: %d]TIME DOES NOT MATCH expected:%lld actual:%lld delta:%lld after:%lld\n",
3312 ts + info->delta, info->ts, info->delta, info->after);
3313 dump_buffer_page(bpage, info, tail);
3314 atomic_dec(&ts_dump);
3315 /* Do not re-enable checking */
3319 atomic_dec(this_cpu_ptr(&checking));
3322 static inline void check_buffer(struct ring_buffer_per_cpu *cpu_buffer,
3323 struct rb_event_info *info,
3327 #endif /* CONFIG_RING_BUFFER_VALIDATE_TIME_DELTAS */
3329 static struct ring_buffer_event *
3330 __rb_reserve_next(struct ring_buffer_per_cpu *cpu_buffer,
3331 struct rb_event_info *info)
3333 struct ring_buffer_event *event;
3334 struct buffer_page *tail_page;
3335 unsigned long tail, write, w;
3339 /* Don't let the compiler play games with cpu_buffer->tail_page */
3340 tail_page = info->tail_page = READ_ONCE(cpu_buffer->tail_page);
3342 /*A*/ w = local_read(&tail_page->write) & RB_WRITE_MASK;
3344 b_ok = rb_time_read(&cpu_buffer->before_stamp, &info->before);
3345 a_ok = rb_time_read(&cpu_buffer->write_stamp, &info->after);
3347 info->ts = rb_time_stamp(cpu_buffer->buffer);
3349 if ((info->add_timestamp & RB_ADD_STAMP_ABSOLUTE)) {
3350 info->delta = info->ts;
3353 * If interrupting an event time update, we may need an
3354 * absolute timestamp.
3355 * Don't bother if this is the start of a new page (w == 0).
3357 if (unlikely(!a_ok || !b_ok || (info->before != info->after && w))) {
3358 info->add_timestamp |= RB_ADD_STAMP_FORCE | RB_ADD_STAMP_EXTEND;
3359 info->length += RB_LEN_TIME_EXTEND;
3361 info->delta = info->ts - info->after;
3362 if (unlikely(test_time_stamp(info->delta))) {
3363 info->add_timestamp |= RB_ADD_STAMP_EXTEND;
3364 info->length += RB_LEN_TIME_EXTEND;
3369 /*B*/ rb_time_set(&cpu_buffer->before_stamp, info->ts);
3371 /*C*/ write = local_add_return(info->length, &tail_page->write);
3373 /* set write to only the index of the write */
3374 write &= RB_WRITE_MASK;
3376 tail = write - info->length;
3378 /* See if we shot pass the end of this buffer page */
3379 if (unlikely(write > BUF_PAGE_SIZE)) {
3380 /* before and after may now different, fix it up*/
3381 b_ok = rb_time_read(&cpu_buffer->before_stamp, &info->before);
3382 a_ok = rb_time_read(&cpu_buffer->write_stamp, &info->after);
3383 if (a_ok && b_ok && info->before != info->after)
3384 (void)rb_time_cmpxchg(&cpu_buffer->before_stamp,
3385 info->before, info->after);
3387 check_buffer(cpu_buffer, info, CHECK_FULL_PAGE);
3388 return rb_move_tail(cpu_buffer, tail, info);
3391 if (likely(tail == w)) {
3395 /* Nothing interrupted us between A and C */
3396 /*D*/ rb_time_set(&cpu_buffer->write_stamp, info->ts);
3398 /*E*/ s_ok = rb_time_read(&cpu_buffer->before_stamp, &save_before);
3399 RB_WARN_ON(cpu_buffer, !s_ok);
3400 if (likely(!(info->add_timestamp &
3401 (RB_ADD_STAMP_FORCE | RB_ADD_STAMP_ABSOLUTE))))
3402 /* This did not interrupt any time update */
3403 info->delta = info->ts - info->after;
3405 /* Just use full timestamp for interrupting event */
3406 info->delta = info->ts;
3408 check_buffer(cpu_buffer, info, tail);
3409 if (unlikely(info->ts != save_before)) {
3410 /* SLOW PATH - Interrupted between C and E */
3412 a_ok = rb_time_read(&cpu_buffer->write_stamp, &info->after);
3413 RB_WARN_ON(cpu_buffer, !a_ok);
3415 /* Write stamp must only go forward */
3416 if (save_before > info->after) {
3418 * We do not care about the result, only that
3419 * it gets updated atomically.
3421 (void)rb_time_cmpxchg(&cpu_buffer->write_stamp,
3422 info->after, save_before);
3427 /* SLOW PATH - Interrupted between A and C */
3428 a_ok = rb_time_read(&cpu_buffer->write_stamp, &info->after);
3429 /* Was interrupted before here, write_stamp must be valid */
3430 RB_WARN_ON(cpu_buffer, !a_ok);
3431 ts = rb_time_stamp(cpu_buffer->buffer);
3433 /*E*/ if (write == (local_read(&tail_page->write) & RB_WRITE_MASK) &&
3435 rb_time_cmpxchg(&cpu_buffer->write_stamp,
3437 /* Nothing came after this event between C and E */
3438 info->delta = ts - info->after;
3442 * Interrupted between C and E:
3443 * Lost the previous events time stamp. Just set the
3444 * delta to zero, and this will be the same time as
3445 * the event this event interrupted. And the events that
3446 * came after this will still be correct (as they would
3447 * have built their delta on the previous event.
3451 info->add_timestamp &= ~RB_ADD_STAMP_FORCE;
3455 * If this is the first commit on the page, then it has the same
3456 * timestamp as the page itself.
3458 if (unlikely(!tail && !(info->add_timestamp &
3459 (RB_ADD_STAMP_FORCE | RB_ADD_STAMP_ABSOLUTE))))
3462 /* We reserved something on the buffer */
3464 event = __rb_page_index(tail_page, tail);
3465 rb_update_event(cpu_buffer, event, info);
3467 local_inc(&tail_page->entries);
3470 * If this is the first commit on the page, then update
3473 if (unlikely(!tail))
3474 tail_page->page->time_stamp = info->ts;
3476 /* account for these added bytes */
3477 local_add(info->length, &cpu_buffer->entries_bytes);
3482 static __always_inline struct ring_buffer_event *
3483 rb_reserve_next_event(struct trace_buffer *buffer,
3484 struct ring_buffer_per_cpu *cpu_buffer,
3485 unsigned long length)
3487 struct ring_buffer_event *event;
3488 struct rb_event_info info;
3492 rb_start_commit(cpu_buffer);
3493 /* The commit page can not change after this */
3495 #ifdef CONFIG_RING_BUFFER_ALLOW_SWAP
3497 * Due to the ability to swap a cpu buffer from a buffer
3498 * it is possible it was swapped before we committed.
3499 * (committing stops a swap). We check for it here and
3500 * if it happened, we have to fail the write.
3503 if (unlikely(READ_ONCE(cpu_buffer->buffer) != buffer)) {
3504 local_dec(&cpu_buffer->committing);
3505 local_dec(&cpu_buffer->commits);
3510 info.length = rb_calculate_event_length(length);
3512 if (ring_buffer_time_stamp_abs(cpu_buffer->buffer)) {
3513 add_ts_default = RB_ADD_STAMP_ABSOLUTE;
3514 info.length += RB_LEN_TIME_EXTEND;
3516 add_ts_default = RB_ADD_STAMP_NONE;
3520 info.add_timestamp = add_ts_default;
3524 * We allow for interrupts to reenter here and do a trace.
3525 * If one does, it will cause this original code to loop
3526 * back here. Even with heavy interrupts happening, this
3527 * should only happen a few times in a row. If this happens
3528 * 1000 times in a row, there must be either an interrupt
3529 * storm or we have something buggy.
3532 if (RB_WARN_ON(cpu_buffer, ++nr_loops > 1000))
3535 event = __rb_reserve_next(cpu_buffer, &info);
3537 if (unlikely(PTR_ERR(event) == -EAGAIN)) {
3538 if (info.add_timestamp & (RB_ADD_STAMP_FORCE | RB_ADD_STAMP_EXTEND))
3539 info.length -= RB_LEN_TIME_EXTEND;
3546 rb_end_commit(cpu_buffer);
3551 * ring_buffer_lock_reserve - reserve a part of the buffer
3552 * @buffer: the ring buffer to reserve from
3553 * @length: the length of the data to reserve (excluding event header)
3555 * Returns a reserved event on the ring buffer to copy directly to.
3556 * The user of this interface will need to get the body to write into
3557 * and can use the ring_buffer_event_data() interface.
3559 * The length is the length of the data needed, not the event length
3560 * which also includes the event header.
3562 * Must be paired with ring_buffer_unlock_commit, unless NULL is returned.
3563 * If NULL is returned, then nothing has been allocated or locked.
3565 struct ring_buffer_event *
3566 ring_buffer_lock_reserve(struct trace_buffer *buffer, unsigned long length)
3568 struct ring_buffer_per_cpu *cpu_buffer;
3569 struct ring_buffer_event *event;
3572 /* If we are tracing schedule, we don't want to recurse */
3573 preempt_disable_notrace();
3575 if (unlikely(atomic_read(&buffer->record_disabled)))
3578 cpu = raw_smp_processor_id();
3580 if (unlikely(!cpumask_test_cpu(cpu, buffer->cpumask)))
3583 cpu_buffer = buffer->buffers[cpu];
3585 if (unlikely(atomic_read(&cpu_buffer->record_disabled)))
3588 if (unlikely(length > BUF_MAX_DATA_SIZE))
3591 if (unlikely(trace_recursive_lock(cpu_buffer)))
3594 event = rb_reserve_next_event(buffer, cpu_buffer, length);
3601 trace_recursive_unlock(cpu_buffer);
3603 preempt_enable_notrace();
3606 EXPORT_SYMBOL_GPL(ring_buffer_lock_reserve);
3609 * Decrement the entries to the page that an event is on.
3610 * The event does not even need to exist, only the pointer
3611 * to the page it is on. This may only be called before the commit
3615 rb_decrement_entry(struct ring_buffer_per_cpu *cpu_buffer,
3616 struct ring_buffer_event *event)
3618 unsigned long addr = (unsigned long)event;
3619 struct buffer_page *bpage = cpu_buffer->commit_page;
3620 struct buffer_page *start;
3624 /* Do the likely case first */
3625 if (likely(bpage->page == (void *)addr)) {
3626 local_dec(&bpage->entries);
3631 * Because the commit page may be on the reader page we
3632 * start with the next page and check the end loop there.
3634 rb_inc_page(&bpage);
3637 if (bpage->page == (void *)addr) {
3638 local_dec(&bpage->entries);
3641 rb_inc_page(&bpage);
3642 } while (bpage != start);
3644 /* commit not part of this buffer?? */
3645 RB_WARN_ON(cpu_buffer, 1);
3649 * ring_buffer_discard_commit - discard an event that has not been committed
3650 * @buffer: the ring buffer
3651 * @event: non committed event to discard
3653 * Sometimes an event that is in the ring buffer needs to be ignored.
3654 * This function lets the user discard an event in the ring buffer
3655 * and then that event will not be read later.
3657 * This function only works if it is called before the item has been
3658 * committed. It will try to free the event from the ring buffer
3659 * if another event has not been added behind it.
3661 * If another event has been added behind it, it will set the event
3662 * up as discarded, and perform the commit.
3664 * If this function is called, do not call ring_buffer_unlock_commit on
3667 void ring_buffer_discard_commit(struct trace_buffer *buffer,
3668 struct ring_buffer_event *event)
3670 struct ring_buffer_per_cpu *cpu_buffer;
3673 /* The event is discarded regardless */
3674 rb_event_discard(event);
3676 cpu = smp_processor_id();
3677 cpu_buffer = buffer->buffers[cpu];
3680 * This must only be called if the event has not been
3681 * committed yet. Thus we can assume that preemption
3682 * is still disabled.
3684 RB_WARN_ON(buffer, !local_read(&cpu_buffer->committing));
3686 rb_decrement_entry(cpu_buffer, event);
3687 if (rb_try_to_discard(cpu_buffer, event))
3691 rb_end_commit(cpu_buffer);
3693 trace_recursive_unlock(cpu_buffer);
3695 preempt_enable_notrace();
3698 EXPORT_SYMBOL_GPL(ring_buffer_discard_commit);
3701 * ring_buffer_write - write data to the buffer without reserving
3702 * @buffer: The ring buffer to write to.
3703 * @length: The length of the data being written (excluding the event header)
3704 * @data: The data to write to the buffer.
3706 * This is like ring_buffer_lock_reserve and ring_buffer_unlock_commit as
3707 * one function. If you already have the data to write to the buffer, it
3708 * may be easier to simply call this function.
3710 * Note, like ring_buffer_lock_reserve, the length is the length of the data
3711 * and not the length of the event which would hold the header.
3713 int ring_buffer_write(struct trace_buffer *buffer,
3714 unsigned long length,
3717 struct ring_buffer_per_cpu *cpu_buffer;
3718 struct ring_buffer_event *event;
3723 preempt_disable_notrace();
3725 if (atomic_read(&buffer->record_disabled))
3728 cpu = raw_smp_processor_id();
3730 if (!cpumask_test_cpu(cpu, buffer->cpumask))
3733 cpu_buffer = buffer->buffers[cpu];
3735 if (atomic_read(&cpu_buffer->record_disabled))
3738 if (length > BUF_MAX_DATA_SIZE)
3741 if (unlikely(trace_recursive_lock(cpu_buffer)))
3744 event = rb_reserve_next_event(buffer, cpu_buffer, length);
3748 body = rb_event_data(event);
3750 memcpy(body, data, length);
3752 rb_commit(cpu_buffer, event);
3754 rb_wakeups(buffer, cpu_buffer);
3759 trace_recursive_unlock(cpu_buffer);
3762 preempt_enable_notrace();
3766 EXPORT_SYMBOL_GPL(ring_buffer_write);
3768 static bool rb_per_cpu_empty(struct ring_buffer_per_cpu *cpu_buffer)
3770 struct buffer_page *reader = cpu_buffer->reader_page;
3771 struct buffer_page *head = rb_set_head_page(cpu_buffer);
3772 struct buffer_page *commit = cpu_buffer->commit_page;
3774 /* In case of error, head will be NULL */
3775 if (unlikely(!head))
3778 return reader->read == rb_page_commit(reader) &&
3779 (commit == reader ||
3781 head->read == rb_page_commit(commit)));
3785 * ring_buffer_record_disable - stop all writes into the buffer
3786 * @buffer: The ring buffer to stop writes to.
3788 * This prevents all writes to the buffer. Any attempt to write
3789 * to the buffer after this will fail and return NULL.
3791 * The caller should call synchronize_rcu() after this.
3793 void ring_buffer_record_disable(struct trace_buffer *buffer)
3795 atomic_inc(&buffer->record_disabled);
3797 EXPORT_SYMBOL_GPL(ring_buffer_record_disable);
3800 * ring_buffer_record_enable - enable writes to the buffer
3801 * @buffer: The ring buffer to enable writes
3803 * Note, multiple disables will need the same number of enables
3804 * to truly enable the writing (much like preempt_disable).
3806 void ring_buffer_record_enable(struct trace_buffer *buffer)
3808 atomic_dec(&buffer->record_disabled);
3810 EXPORT_SYMBOL_GPL(ring_buffer_record_enable);
3813 * ring_buffer_record_off - stop all writes into the buffer
3814 * @buffer: The ring buffer to stop writes to.
3816 * This prevents all writes to the buffer. Any attempt to write
3817 * to the buffer after this will fail and return NULL.
3819 * This is different than ring_buffer_record_disable() as
3820 * it works like an on/off switch, where as the disable() version
3821 * must be paired with a enable().
3823 void ring_buffer_record_off(struct trace_buffer *buffer)
3826 unsigned int new_rd;
3829 rd = atomic_read(&buffer->record_disabled);
3830 new_rd = rd | RB_BUFFER_OFF;
3831 } while (atomic_cmpxchg(&buffer->record_disabled, rd, new_rd) != rd);
3833 EXPORT_SYMBOL_GPL(ring_buffer_record_off);
3836 * ring_buffer_record_on - restart writes into the buffer
3837 * @buffer: The ring buffer to start writes to.
3839 * This enables all writes to the buffer that was disabled by
3840 * ring_buffer_record_off().
3842 * This is different than ring_buffer_record_enable() as
3843 * it works like an on/off switch, where as the enable() version
3844 * must be paired with a disable().
3846 void ring_buffer_record_on(struct trace_buffer *buffer)
3849 unsigned int new_rd;
3852 rd = atomic_read(&buffer->record_disabled);
3853 new_rd = rd & ~RB_BUFFER_OFF;
3854 } while (atomic_cmpxchg(&buffer->record_disabled, rd, new_rd) != rd);
3856 EXPORT_SYMBOL_GPL(ring_buffer_record_on);
3859 * ring_buffer_record_is_on - return true if the ring buffer can write
3860 * @buffer: The ring buffer to see if write is enabled
3862 * Returns true if the ring buffer is in a state that it accepts writes.
3864 bool ring_buffer_record_is_on(struct trace_buffer *buffer)
3866 return !atomic_read(&buffer->record_disabled);
3870 * ring_buffer_record_is_set_on - return true if the ring buffer is set writable
3871 * @buffer: The ring buffer to see if write is set enabled
3873 * Returns true if the ring buffer is set writable by ring_buffer_record_on().
3874 * Note that this does NOT mean it is in a writable state.
3876 * It may return true when the ring buffer has been disabled by
3877 * ring_buffer_record_disable(), as that is a temporary disabling of
3880 bool ring_buffer_record_is_set_on(struct trace_buffer *buffer)
3882 return !(atomic_read(&buffer->record_disabled) & RB_BUFFER_OFF);
3886 * ring_buffer_record_disable_cpu - stop all writes into the cpu_buffer
3887 * @buffer: The ring buffer to stop writes to.
3888 * @cpu: The CPU buffer to stop
3890 * This prevents all writes to the buffer. Any attempt to write
3891 * to the buffer after this will fail and return NULL.
3893 * The caller should call synchronize_rcu() after this.
3895 void ring_buffer_record_disable_cpu(struct trace_buffer *buffer, int cpu)
3897 struct ring_buffer_per_cpu *cpu_buffer;
3899 if (!cpumask_test_cpu(cpu, buffer->cpumask))
3902 cpu_buffer = buffer->buffers[cpu];
3903 atomic_inc(&cpu_buffer->record_disabled);
3905 EXPORT_SYMBOL_GPL(ring_buffer_record_disable_cpu);
3908 * ring_buffer_record_enable_cpu - enable writes to the buffer
3909 * @buffer: The ring buffer to enable writes
3910 * @cpu: The CPU to enable.
3912 * Note, multiple disables will need the same number of enables
3913 * to truly enable the writing (much like preempt_disable).
3915 void ring_buffer_record_enable_cpu(struct trace_buffer *buffer, int cpu)
3917 struct ring_buffer_per_cpu *cpu_buffer;
3919 if (!cpumask_test_cpu(cpu, buffer->cpumask))
3922 cpu_buffer = buffer->buffers[cpu];
3923 atomic_dec(&cpu_buffer->record_disabled);
3925 EXPORT_SYMBOL_GPL(ring_buffer_record_enable_cpu);
3928 * The total entries in the ring buffer is the running counter
3929 * of entries entered into the ring buffer, minus the sum of
3930 * the entries read from the ring buffer and the number of
3931 * entries that were overwritten.
3933 static inline unsigned long
3934 rb_num_of_entries(struct ring_buffer_per_cpu *cpu_buffer)
3936 return local_read(&cpu_buffer->entries) -
3937 (local_read(&cpu_buffer->overrun) + cpu_buffer->read);
3941 * ring_buffer_oldest_event_ts - get the oldest event timestamp from the buffer
3942 * @buffer: The ring buffer
3943 * @cpu: The per CPU buffer to read from.
3945 u64 ring_buffer_oldest_event_ts(struct trace_buffer *buffer, int cpu)
3947 unsigned long flags;
3948 struct ring_buffer_per_cpu *cpu_buffer;
3949 struct buffer_page *bpage;
3952 if (!cpumask_test_cpu(cpu, buffer->cpumask))
3955 cpu_buffer = buffer->buffers[cpu];
3956 raw_spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
3958 * if the tail is on reader_page, oldest time stamp is on the reader
3961 if (cpu_buffer->tail_page == cpu_buffer->reader_page)
3962 bpage = cpu_buffer->reader_page;
3964 bpage = rb_set_head_page(cpu_buffer);
3966 ret = bpage->page->time_stamp;
3967 raw_spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
3971 EXPORT_SYMBOL_GPL(ring_buffer_oldest_event_ts);
3974 * ring_buffer_bytes_cpu - get the number of bytes consumed in a cpu buffer
3975 * @buffer: The ring buffer
3976 * @cpu: The per CPU buffer to read from.
3978 unsigned long ring_buffer_bytes_cpu(struct trace_buffer *buffer, int cpu)
3980 struct ring_buffer_per_cpu *cpu_buffer;
3983 if (!cpumask_test_cpu(cpu, buffer->cpumask))
3986 cpu_buffer = buffer->buffers[cpu];
3987 ret = local_read(&cpu_buffer->entries_bytes) - cpu_buffer->read_bytes;
3991 EXPORT_SYMBOL_GPL(ring_buffer_bytes_cpu);
3994 * ring_buffer_entries_cpu - get the number of entries in a cpu buffer
3995 * @buffer: The ring buffer
3996 * @cpu: The per CPU buffer to get the entries from.
3998 unsigned long ring_buffer_entries_cpu(struct trace_buffer *buffer, int cpu)
4000 struct ring_buffer_per_cpu *cpu_buffer;
4002 if (!cpumask_test_cpu(cpu, buffer->cpumask))
4005 cpu_buffer = buffer->buffers[cpu];
4007 return rb_num_of_entries(cpu_buffer);
4009 EXPORT_SYMBOL_GPL(ring_buffer_entries_cpu);
4012 * ring_buffer_overrun_cpu - get the number of overruns caused by the ring
4013 * buffer wrapping around (only if RB_FL_OVERWRITE is on).
4014 * @buffer: The ring buffer
4015 * @cpu: The per CPU buffer to get the number of overruns from
4017 unsigned long ring_buffer_overrun_cpu(struct trace_buffer *buffer, int cpu)
4019 struct ring_buffer_per_cpu *cpu_buffer;
4022 if (!cpumask_test_cpu(cpu, buffer->cpumask))
4025 cpu_buffer = buffer->buffers[cpu];
4026 ret = local_read(&cpu_buffer->overrun);
4030 EXPORT_SYMBOL_GPL(ring_buffer_overrun_cpu);
4033 * ring_buffer_commit_overrun_cpu - get the number of overruns caused by
4034 * commits failing due to the buffer wrapping around while there are uncommitted
4035 * events, such as during an interrupt storm.
4036 * @buffer: The ring buffer
4037 * @cpu: The per CPU buffer to get the number of overruns from
4040 ring_buffer_commit_overrun_cpu(struct trace_buffer *buffer, int cpu)
4042 struct ring_buffer_per_cpu *cpu_buffer;
4045 if (!cpumask_test_cpu(cpu, buffer->cpumask))
4048 cpu_buffer = buffer->buffers[cpu];
4049 ret = local_read(&cpu_buffer->commit_overrun);
4053 EXPORT_SYMBOL_GPL(ring_buffer_commit_overrun_cpu);
4056 * ring_buffer_dropped_events_cpu - get the number of dropped events caused by
4057 * the ring buffer filling up (only if RB_FL_OVERWRITE is off).
4058 * @buffer: The ring buffer
4059 * @cpu: The per CPU buffer to get the number of overruns from
4062 ring_buffer_dropped_events_cpu(struct trace_buffer *buffer, int cpu)
4064 struct ring_buffer_per_cpu *cpu_buffer;
4067 if (!cpumask_test_cpu(cpu, buffer->cpumask))
4070 cpu_buffer = buffer->buffers[cpu];
4071 ret = local_read(&cpu_buffer->dropped_events);
4075 EXPORT_SYMBOL_GPL(ring_buffer_dropped_events_cpu);
4078 * ring_buffer_read_events_cpu - get the number of events successfully read
4079 * @buffer: The ring buffer
4080 * @cpu: The per CPU buffer to get the number of events read
4083 ring_buffer_read_events_cpu(struct trace_buffer *buffer, int cpu)
4085 struct ring_buffer_per_cpu *cpu_buffer;
4087 if (!cpumask_test_cpu(cpu, buffer->cpumask))
4090 cpu_buffer = buffer->buffers[cpu];
4091 return cpu_buffer->read;
4093 EXPORT_SYMBOL_GPL(ring_buffer_read_events_cpu);
4096 * ring_buffer_entries - get the number of entries in a buffer
4097 * @buffer: The ring buffer
4099 * Returns the total number of entries in the ring buffer
4102 unsigned long ring_buffer_entries(struct trace_buffer *buffer)
4104 struct ring_buffer_per_cpu *cpu_buffer;
4105 unsigned long entries = 0;
4108 /* if you care about this being correct, lock the buffer */
4109 for_each_buffer_cpu(buffer, cpu) {
4110 cpu_buffer = buffer->buffers[cpu];
4111 entries += rb_num_of_entries(cpu_buffer);
4116 EXPORT_SYMBOL_GPL(ring_buffer_entries);
4119 * ring_buffer_overruns - get the number of overruns in buffer
4120 * @buffer: The ring buffer
4122 * Returns the total number of overruns in the ring buffer
4125 unsigned long ring_buffer_overruns(struct trace_buffer *buffer)
4127 struct ring_buffer_per_cpu *cpu_buffer;
4128 unsigned long overruns = 0;
4131 /* if you care about this being correct, lock the buffer */
4132 for_each_buffer_cpu(buffer, cpu) {
4133 cpu_buffer = buffer->buffers[cpu];
4134 overruns += local_read(&cpu_buffer->overrun);
4139 EXPORT_SYMBOL_GPL(ring_buffer_overruns);
4141 static void rb_iter_reset(struct ring_buffer_iter *iter)
4143 struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
4145 /* Iterator usage is expected to have record disabled */
4146 iter->head_page = cpu_buffer->reader_page;
4147 iter->head = cpu_buffer->reader_page->read;
4148 iter->next_event = iter->head;
4150 iter->cache_reader_page = iter->head_page;
4151 iter->cache_read = cpu_buffer->read;
4154 iter->read_stamp = cpu_buffer->read_stamp;
4155 iter->page_stamp = cpu_buffer->reader_page->page->time_stamp;
4157 iter->read_stamp = iter->head_page->page->time_stamp;
4158 iter->page_stamp = iter->read_stamp;
4163 * ring_buffer_iter_reset - reset an iterator
4164 * @iter: The iterator to reset
4166 * Resets the iterator, so that it will start from the beginning
4169 void ring_buffer_iter_reset(struct ring_buffer_iter *iter)
4171 struct ring_buffer_per_cpu *cpu_buffer;
4172 unsigned long flags;
4177 cpu_buffer = iter->cpu_buffer;
4179 raw_spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
4180 rb_iter_reset(iter);
4181 raw_spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
4183 EXPORT_SYMBOL_GPL(ring_buffer_iter_reset);
4186 * ring_buffer_iter_empty - check if an iterator has no more to read
4187 * @iter: The iterator to check
4189 int ring_buffer_iter_empty(struct ring_buffer_iter *iter)
4191 struct ring_buffer_per_cpu *cpu_buffer;
4192 struct buffer_page *reader;
4193 struct buffer_page *head_page;
4194 struct buffer_page *commit_page;
4195 struct buffer_page *curr_commit_page;
4200 cpu_buffer = iter->cpu_buffer;
4201 reader = cpu_buffer->reader_page;
4202 head_page = cpu_buffer->head_page;
4203 commit_page = cpu_buffer->commit_page;
4204 commit_ts = commit_page->page->time_stamp;
4207 * When the writer goes across pages, it issues a cmpxchg which
4208 * is a mb(), which will synchronize with the rmb here.
4209 * (see rb_tail_page_update())
4212 commit = rb_page_commit(commit_page);
4213 /* We want to make sure that the commit page doesn't change */
4216 /* Make sure commit page didn't change */
4217 curr_commit_page = READ_ONCE(cpu_buffer->commit_page);
4218 curr_commit_ts = READ_ONCE(curr_commit_page->page->time_stamp);
4220 /* If the commit page changed, then there's more data */
4221 if (curr_commit_page != commit_page ||
4222 curr_commit_ts != commit_ts)
4225 /* Still racy, as it may return a false positive, but that's OK */
4226 return ((iter->head_page == commit_page && iter->head >= commit) ||
4227 (iter->head_page == reader && commit_page == head_page &&
4228 head_page->read == commit &&
4229 iter->head == rb_page_commit(cpu_buffer->reader_page)));
4231 EXPORT_SYMBOL_GPL(ring_buffer_iter_empty);
4234 rb_update_read_stamp(struct ring_buffer_per_cpu *cpu_buffer,
4235 struct ring_buffer_event *event)
4239 switch (event->type_len) {
4240 case RINGBUF_TYPE_PADDING:
4243 case RINGBUF_TYPE_TIME_EXTEND:
4244 delta = ring_buffer_event_time_stamp(event);
4245 cpu_buffer->read_stamp += delta;
4248 case RINGBUF_TYPE_TIME_STAMP:
4249 delta = ring_buffer_event_time_stamp(event);
4250 cpu_buffer->read_stamp = delta;
4253 case RINGBUF_TYPE_DATA:
4254 cpu_buffer->read_stamp += event->time_delta;
4258 RB_WARN_ON(cpu_buffer, 1);
4264 rb_update_iter_read_stamp(struct ring_buffer_iter *iter,
4265 struct ring_buffer_event *event)
4269 switch (event->type_len) {
4270 case RINGBUF_TYPE_PADDING:
4273 case RINGBUF_TYPE_TIME_EXTEND:
4274 delta = ring_buffer_event_time_stamp(event);
4275 iter->read_stamp += delta;
4278 case RINGBUF_TYPE_TIME_STAMP:
4279 delta = ring_buffer_event_time_stamp(event);
4280 iter->read_stamp = delta;
4283 case RINGBUF_TYPE_DATA:
4284 iter->read_stamp += event->time_delta;
4288 RB_WARN_ON(iter->cpu_buffer, 1);
4293 static struct buffer_page *
4294 rb_get_reader_page(struct ring_buffer_per_cpu *cpu_buffer)
4296 struct buffer_page *reader = NULL;
4297 unsigned long overwrite;
4298 unsigned long flags;
4302 local_irq_save(flags);
4303 arch_spin_lock(&cpu_buffer->lock);
4307 * This should normally only loop twice. But because the
4308 * start of the reader inserts an empty page, it causes
4309 * a case where we will loop three times. There should be no
4310 * reason to loop four times (that I know of).
4312 if (RB_WARN_ON(cpu_buffer, ++nr_loops > 3)) {
4317 reader = cpu_buffer->reader_page;
4319 /* If there's more to read, return this page */
4320 if (cpu_buffer->reader_page->read < rb_page_size(reader))
4323 /* Never should we have an index greater than the size */
4324 if (RB_WARN_ON(cpu_buffer,
4325 cpu_buffer->reader_page->read > rb_page_size(reader)))
4328 /* check if we caught up to the tail */
4330 if (cpu_buffer->commit_page == cpu_buffer->reader_page)
4333 /* Don't bother swapping if the ring buffer is empty */
4334 if (rb_num_of_entries(cpu_buffer) == 0)
4338 * Reset the reader page to size zero.
4340 local_set(&cpu_buffer->reader_page->write, 0);
4341 local_set(&cpu_buffer->reader_page->entries, 0);
4342 local_set(&cpu_buffer->reader_page->page->commit, 0);
4343 cpu_buffer->reader_page->real_end = 0;
4347 * Splice the empty reader page into the list around the head.
4349 reader = rb_set_head_page(cpu_buffer);
4352 cpu_buffer->reader_page->list.next = rb_list_head(reader->list.next);
4353 cpu_buffer->reader_page->list.prev = reader->list.prev;
4356 * cpu_buffer->pages just needs to point to the buffer, it
4357 * has no specific buffer page to point to. Lets move it out
4358 * of our way so we don't accidentally swap it.
4360 cpu_buffer->pages = reader->list.prev;
4362 /* The reader page will be pointing to the new head */
4363 rb_set_list_to_head(&cpu_buffer->reader_page->list);
4366 * We want to make sure we read the overruns after we set up our
4367 * pointers to the next object. The writer side does a
4368 * cmpxchg to cross pages which acts as the mb on the writer
4369 * side. Note, the reader will constantly fail the swap
4370 * while the writer is updating the pointers, so this
4371 * guarantees that the overwrite recorded here is the one we
4372 * want to compare with the last_overrun.
4375 overwrite = local_read(&(cpu_buffer->overrun));
4378 * Here's the tricky part.
4380 * We need to move the pointer past the header page.
4381 * But we can only do that if a writer is not currently
4382 * moving it. The page before the header page has the
4383 * flag bit '1' set if it is pointing to the page we want.
4384 * but if the writer is in the process of moving it
4385 * than it will be '2' or already moved '0'.
4388 ret = rb_head_page_replace(reader, cpu_buffer->reader_page);
4391 * If we did not convert it, then we must try again.
4397 * Yay! We succeeded in replacing the page.
4399 * Now make the new head point back to the reader page.
4401 rb_list_head(reader->list.next)->prev = &cpu_buffer->reader_page->list;
4402 rb_inc_page(&cpu_buffer->head_page);
4404 local_inc(&cpu_buffer->pages_read);
4406 /* Finally update the reader page to the new head */
4407 cpu_buffer->reader_page = reader;
4408 cpu_buffer->reader_page->read = 0;
4410 if (overwrite != cpu_buffer->last_overrun) {
4411 cpu_buffer->lost_events = overwrite - cpu_buffer->last_overrun;
4412 cpu_buffer->last_overrun = overwrite;
4418 /* Update the read_stamp on the first event */
4419 if (reader && reader->read == 0)
4420 cpu_buffer->read_stamp = reader->page->time_stamp;
4422 arch_spin_unlock(&cpu_buffer->lock);
4423 local_irq_restore(flags);
4428 static void rb_advance_reader(struct ring_buffer_per_cpu *cpu_buffer)
4430 struct ring_buffer_event *event;
4431 struct buffer_page *reader;
4434 reader = rb_get_reader_page(cpu_buffer);
4436 /* This function should not be called when buffer is empty */
4437 if (RB_WARN_ON(cpu_buffer, !reader))
4440 event = rb_reader_event(cpu_buffer);
4442 if (event->type_len <= RINGBUF_TYPE_DATA_TYPE_LEN_MAX)
4445 rb_update_read_stamp(cpu_buffer, event);
4447 length = rb_event_length(event);
4448 cpu_buffer->reader_page->read += length;
4451 static void rb_advance_iter(struct ring_buffer_iter *iter)
4453 struct ring_buffer_per_cpu *cpu_buffer;
4455 cpu_buffer = iter->cpu_buffer;
4457 /* If head == next_event then we need to jump to the next event */
4458 if (iter->head == iter->next_event) {
4459 /* If the event gets overwritten again, there's nothing to do */
4460 if (rb_iter_head_event(iter) == NULL)
4464 iter->head = iter->next_event;
4467 * Check if we are at the end of the buffer.
4469 if (iter->next_event >= rb_page_size(iter->head_page)) {
4470 /* discarded commits can make the page empty */
4471 if (iter->head_page == cpu_buffer->commit_page)
4477 rb_update_iter_read_stamp(iter, iter->event);
4480 static int rb_lost_events(struct ring_buffer_per_cpu *cpu_buffer)
4482 return cpu_buffer->lost_events;
4485 static struct ring_buffer_event *
4486 rb_buffer_peek(struct ring_buffer_per_cpu *cpu_buffer, u64 *ts,
4487 unsigned long *lost_events)
4489 struct ring_buffer_event *event;
4490 struct buffer_page *reader;
4497 * We repeat when a time extend is encountered.
4498 * Since the time extend is always attached to a data event,
4499 * we should never loop more than once.
4500 * (We never hit the following condition more than twice).
4502 if (RB_WARN_ON(cpu_buffer, ++nr_loops > 2))
4505 reader = rb_get_reader_page(cpu_buffer);
4509 event = rb_reader_event(cpu_buffer);
4511 switch (event->type_len) {
4512 case RINGBUF_TYPE_PADDING:
4513 if (rb_null_event(event))
4514 RB_WARN_ON(cpu_buffer, 1);
4516 * Because the writer could be discarding every
4517 * event it creates (which would probably be bad)
4518 * if we were to go back to "again" then we may never
4519 * catch up, and will trigger the warn on, or lock
4520 * the box. Return the padding, and we will release
4521 * the current locks, and try again.
4525 case RINGBUF_TYPE_TIME_EXTEND:
4526 /* Internal data, OK to advance */
4527 rb_advance_reader(cpu_buffer);
4530 case RINGBUF_TYPE_TIME_STAMP:
4532 *ts = ring_buffer_event_time_stamp(event);
4533 ring_buffer_normalize_time_stamp(cpu_buffer->buffer,
4534 cpu_buffer->cpu, ts);
4536 /* Internal data, OK to advance */
4537 rb_advance_reader(cpu_buffer);
4540 case RINGBUF_TYPE_DATA:
4542 *ts = cpu_buffer->read_stamp + event->time_delta;
4543 ring_buffer_normalize_time_stamp(cpu_buffer->buffer,
4544 cpu_buffer->cpu, ts);
4547 *lost_events = rb_lost_events(cpu_buffer);
4551 RB_WARN_ON(cpu_buffer, 1);
4556 EXPORT_SYMBOL_GPL(ring_buffer_peek);
4558 static struct ring_buffer_event *
4559 rb_iter_peek(struct ring_buffer_iter *iter, u64 *ts)
4561 struct trace_buffer *buffer;
4562 struct ring_buffer_per_cpu *cpu_buffer;
4563 struct ring_buffer_event *event;
4569 cpu_buffer = iter->cpu_buffer;
4570 buffer = cpu_buffer->buffer;
4573 * Check if someone performed a consuming read to
4574 * the buffer. A consuming read invalidates the iterator
4575 * and we need to reset the iterator in this case.
4577 if (unlikely(iter->cache_read != cpu_buffer->read ||
4578 iter->cache_reader_page != cpu_buffer->reader_page))
4579 rb_iter_reset(iter);
4582 if (ring_buffer_iter_empty(iter))
4586 * As the writer can mess with what the iterator is trying
4587 * to read, just give up if we fail to get an event after
4588 * three tries. The iterator is not as reliable when reading
4589 * the ring buffer with an active write as the consumer is.
4590 * Do not warn if the three failures is reached.
4595 if (rb_per_cpu_empty(cpu_buffer))
4598 if (iter->head >= rb_page_size(iter->head_page)) {
4603 event = rb_iter_head_event(iter);
4607 switch (event->type_len) {
4608 case RINGBUF_TYPE_PADDING:
4609 if (rb_null_event(event)) {
4613 rb_advance_iter(iter);
4616 case RINGBUF_TYPE_TIME_EXTEND:
4617 /* Internal data, OK to advance */
4618 rb_advance_iter(iter);
4621 case RINGBUF_TYPE_TIME_STAMP:
4623 *ts = ring_buffer_event_time_stamp(event);
4624 ring_buffer_normalize_time_stamp(cpu_buffer->buffer,
4625 cpu_buffer->cpu, ts);
4627 /* Internal data, OK to advance */
4628 rb_advance_iter(iter);
4631 case RINGBUF_TYPE_DATA:
4633 *ts = iter->read_stamp + event->time_delta;
4634 ring_buffer_normalize_time_stamp(buffer,
4635 cpu_buffer->cpu, ts);
4640 RB_WARN_ON(cpu_buffer, 1);
4645 EXPORT_SYMBOL_GPL(ring_buffer_iter_peek);
4647 static inline bool rb_reader_lock(struct ring_buffer_per_cpu *cpu_buffer)
4649 if (likely(!in_nmi())) {
4650 raw_spin_lock(&cpu_buffer->reader_lock);
4655 * If an NMI die dumps out the content of the ring buffer
4656 * trylock must be used to prevent a deadlock if the NMI
4657 * preempted a task that holds the ring buffer locks. If
4658 * we get the lock then all is fine, if not, then continue
4659 * to do the read, but this can corrupt the ring buffer,
4660 * so it must be permanently disabled from future writes.
4661 * Reading from NMI is a oneshot deal.
4663 if (raw_spin_trylock(&cpu_buffer->reader_lock))
4666 /* Continue without locking, but disable the ring buffer */
4667 atomic_inc(&cpu_buffer->record_disabled);
4672 rb_reader_unlock(struct ring_buffer_per_cpu *cpu_buffer, bool locked)
4675 raw_spin_unlock(&cpu_buffer->reader_lock);
4680 * ring_buffer_peek - peek at the next event to be read
4681 * @buffer: The ring buffer to read
4682 * @cpu: The cpu to peak at
4683 * @ts: The timestamp counter of this event.
4684 * @lost_events: a variable to store if events were lost (may be NULL)
4686 * This will return the event that will be read next, but does
4687 * not consume the data.
4689 struct ring_buffer_event *
4690 ring_buffer_peek(struct trace_buffer *buffer, int cpu, u64 *ts,
4691 unsigned long *lost_events)
4693 struct ring_buffer_per_cpu *cpu_buffer = buffer->buffers[cpu];
4694 struct ring_buffer_event *event;
4695 unsigned long flags;
4698 if (!cpumask_test_cpu(cpu, buffer->cpumask))
4702 local_irq_save(flags);
4703 dolock = rb_reader_lock(cpu_buffer);
4704 event = rb_buffer_peek(cpu_buffer, ts, lost_events);
4705 if (event && event->type_len == RINGBUF_TYPE_PADDING)
4706 rb_advance_reader(cpu_buffer);
4707 rb_reader_unlock(cpu_buffer, dolock);
4708 local_irq_restore(flags);
4710 if (event && event->type_len == RINGBUF_TYPE_PADDING)
4716 /** ring_buffer_iter_dropped - report if there are dropped events
4717 * @iter: The ring buffer iterator
4719 * Returns true if there was dropped events since the last peek.
4721 bool ring_buffer_iter_dropped(struct ring_buffer_iter *iter)
4723 bool ret = iter->missed_events != 0;
4725 iter->missed_events = 0;
4728 EXPORT_SYMBOL_GPL(ring_buffer_iter_dropped);
4731 * ring_buffer_iter_peek - peek at the next event to be read
4732 * @iter: The ring buffer iterator
4733 * @ts: The timestamp counter of this event.
4735 * This will return the event that will be read next, but does
4736 * not increment the iterator.
4738 struct ring_buffer_event *
4739 ring_buffer_iter_peek(struct ring_buffer_iter *iter, u64 *ts)
4741 struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
4742 struct ring_buffer_event *event;
4743 unsigned long flags;
4746 raw_spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
4747 event = rb_iter_peek(iter, ts);
4748 raw_spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
4750 if (event && event->type_len == RINGBUF_TYPE_PADDING)
4757 * ring_buffer_consume - return an event and consume it
4758 * @buffer: The ring buffer to get the next event from
4759 * @cpu: the cpu to read the buffer from
4760 * @ts: a variable to store the timestamp (may be NULL)
4761 * @lost_events: a variable to store if events were lost (may be NULL)
4763 * Returns the next event in the ring buffer, and that event is consumed.
4764 * Meaning, that sequential reads will keep returning a different event,
4765 * and eventually empty the ring buffer if the producer is slower.
4767 struct ring_buffer_event *
4768 ring_buffer_consume(struct trace_buffer *buffer, int cpu, u64 *ts,
4769 unsigned long *lost_events)
4771 struct ring_buffer_per_cpu *cpu_buffer;
4772 struct ring_buffer_event *event = NULL;
4773 unsigned long flags;
4777 /* might be called in atomic */
4780 if (!cpumask_test_cpu(cpu, buffer->cpumask))
4783 cpu_buffer = buffer->buffers[cpu];
4784 local_irq_save(flags);
4785 dolock = rb_reader_lock(cpu_buffer);
4787 event = rb_buffer_peek(cpu_buffer, ts, lost_events);
4789 cpu_buffer->lost_events = 0;
4790 rb_advance_reader(cpu_buffer);
4793 rb_reader_unlock(cpu_buffer, dolock);
4794 local_irq_restore(flags);
4799 if (event && event->type_len == RINGBUF_TYPE_PADDING)
4804 EXPORT_SYMBOL_GPL(ring_buffer_consume);
4807 * ring_buffer_read_prepare - Prepare for a non consuming read of the buffer
4808 * @buffer: The ring buffer to read from
4809 * @cpu: The cpu buffer to iterate over
4810 * @flags: gfp flags to use for memory allocation
4812 * This performs the initial preparations necessary to iterate
4813 * through the buffer. Memory is allocated, buffer recording
4814 * is disabled, and the iterator pointer is returned to the caller.
4816 * Disabling buffer recording prevents the reading from being
4817 * corrupted. This is not a consuming read, so a producer is not
4820 * After a sequence of ring_buffer_read_prepare calls, the user is
4821 * expected to make at least one call to ring_buffer_read_prepare_sync.
4822 * Afterwards, ring_buffer_read_start is invoked to get things going
4825 * This overall must be paired with ring_buffer_read_finish.
4827 struct ring_buffer_iter *
4828 ring_buffer_read_prepare(struct trace_buffer *buffer, int cpu, gfp_t flags)
4830 struct ring_buffer_per_cpu *cpu_buffer;
4831 struct ring_buffer_iter *iter;
4833 if (!cpumask_test_cpu(cpu, buffer->cpumask))
4836 iter = kzalloc(sizeof(*iter), flags);
4840 iter->event = kmalloc(BUF_MAX_DATA_SIZE, flags);
4846 cpu_buffer = buffer->buffers[cpu];
4848 iter->cpu_buffer = cpu_buffer;
4850 atomic_inc(&cpu_buffer->resize_disabled);
4854 EXPORT_SYMBOL_GPL(ring_buffer_read_prepare);
4857 * ring_buffer_read_prepare_sync - Synchronize a set of prepare calls
4859 * All previously invoked ring_buffer_read_prepare calls to prepare
4860 * iterators will be synchronized. Afterwards, read_buffer_read_start
4861 * calls on those iterators are allowed.
4864 ring_buffer_read_prepare_sync(void)
4868 EXPORT_SYMBOL_GPL(ring_buffer_read_prepare_sync);
4871 * ring_buffer_read_start - start a non consuming read of the buffer
4872 * @iter: The iterator returned by ring_buffer_read_prepare
4874 * This finalizes the startup of an iteration through the buffer.
4875 * The iterator comes from a call to ring_buffer_read_prepare and
4876 * an intervening ring_buffer_read_prepare_sync must have been
4879 * Must be paired with ring_buffer_read_finish.
4882 ring_buffer_read_start(struct ring_buffer_iter *iter)
4884 struct ring_buffer_per_cpu *cpu_buffer;
4885 unsigned long flags;
4890 cpu_buffer = iter->cpu_buffer;
4892 raw_spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
4893 arch_spin_lock(&cpu_buffer->lock);
4894 rb_iter_reset(iter);
4895 arch_spin_unlock(&cpu_buffer->lock);
4896 raw_spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
4898 EXPORT_SYMBOL_GPL(ring_buffer_read_start);
4901 * ring_buffer_read_finish - finish reading the iterator of the buffer
4902 * @iter: The iterator retrieved by ring_buffer_start
4904 * This re-enables the recording to the buffer, and frees the
4908 ring_buffer_read_finish(struct ring_buffer_iter *iter)
4910 struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
4911 unsigned long flags;
4914 * Ring buffer is disabled from recording, here's a good place
4915 * to check the integrity of the ring buffer.
4916 * Must prevent readers from trying to read, as the check
4917 * clears the HEAD page and readers require it.
4919 raw_spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
4920 rb_check_pages(cpu_buffer);
4921 raw_spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
4923 atomic_dec(&cpu_buffer->resize_disabled);
4927 EXPORT_SYMBOL_GPL(ring_buffer_read_finish);
4930 * ring_buffer_iter_advance - advance the iterator to the next location
4931 * @iter: The ring buffer iterator
4933 * Move the location of the iterator such that the next read will
4934 * be the next location of the iterator.
4936 void ring_buffer_iter_advance(struct ring_buffer_iter *iter)
4938 struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
4939 unsigned long flags;
4941 raw_spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
4943 rb_advance_iter(iter);
4945 raw_spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
4947 EXPORT_SYMBOL_GPL(ring_buffer_iter_advance);
4950 * ring_buffer_size - return the size of the ring buffer (in bytes)
4951 * @buffer: The ring buffer.
4952 * @cpu: The CPU to get ring buffer size from.
4954 unsigned long ring_buffer_size(struct trace_buffer *buffer, int cpu)
4957 * Earlier, this method returned
4958 * BUF_PAGE_SIZE * buffer->nr_pages
4959 * Since the nr_pages field is now removed, we have converted this to
4960 * return the per cpu buffer value.
4962 if (!cpumask_test_cpu(cpu, buffer->cpumask))
4965 return BUF_PAGE_SIZE * buffer->buffers[cpu]->nr_pages;
4967 EXPORT_SYMBOL_GPL(ring_buffer_size);
4970 rb_reset_cpu(struct ring_buffer_per_cpu *cpu_buffer)
4972 rb_head_page_deactivate(cpu_buffer);
4974 cpu_buffer->head_page
4975 = list_entry(cpu_buffer->pages, struct buffer_page, list);
4976 local_set(&cpu_buffer->head_page->write, 0);
4977 local_set(&cpu_buffer->head_page->entries, 0);
4978 local_set(&cpu_buffer->head_page->page->commit, 0);
4980 cpu_buffer->head_page->read = 0;
4982 cpu_buffer->tail_page = cpu_buffer->head_page;
4983 cpu_buffer->commit_page = cpu_buffer->head_page;
4985 INIT_LIST_HEAD(&cpu_buffer->reader_page->list);
4986 INIT_LIST_HEAD(&cpu_buffer->new_pages);
4987 local_set(&cpu_buffer->reader_page->write, 0);
4988 local_set(&cpu_buffer->reader_page->entries, 0);
4989 local_set(&cpu_buffer->reader_page->page->commit, 0);
4990 cpu_buffer->reader_page->read = 0;
4992 local_set(&cpu_buffer->entries_bytes, 0);
4993 local_set(&cpu_buffer->overrun, 0);
4994 local_set(&cpu_buffer->commit_overrun, 0);
4995 local_set(&cpu_buffer->dropped_events, 0);
4996 local_set(&cpu_buffer->entries, 0);
4997 local_set(&cpu_buffer->committing, 0);
4998 local_set(&cpu_buffer->commits, 0);
4999 local_set(&cpu_buffer->pages_touched, 0);
5000 local_set(&cpu_buffer->pages_read, 0);
5001 cpu_buffer->last_pages_touch = 0;
5002 cpu_buffer->shortest_full = 0;
5003 cpu_buffer->read = 0;
5004 cpu_buffer->read_bytes = 0;
5006 rb_time_set(&cpu_buffer->write_stamp, 0);
5007 rb_time_set(&cpu_buffer->before_stamp, 0);
5009 cpu_buffer->lost_events = 0;
5010 cpu_buffer->last_overrun = 0;
5012 rb_head_page_activate(cpu_buffer);
5015 /* Must have disabled the cpu buffer then done a synchronize_rcu */
5016 static void reset_disabled_cpu_buffer(struct ring_buffer_per_cpu *cpu_buffer)
5018 unsigned long flags;
5020 raw_spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
5022 if (RB_WARN_ON(cpu_buffer, local_read(&cpu_buffer->committing)))
5025 arch_spin_lock(&cpu_buffer->lock);
5027 rb_reset_cpu(cpu_buffer);
5029 arch_spin_unlock(&cpu_buffer->lock);
5032 raw_spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
5036 * ring_buffer_reset_cpu - reset a ring buffer per CPU buffer
5037 * @buffer: The ring buffer to reset a per cpu buffer of
5038 * @cpu: The CPU buffer to be reset
5040 void ring_buffer_reset_cpu(struct trace_buffer *buffer, int cpu)
5042 struct ring_buffer_per_cpu *cpu_buffer = buffer->buffers[cpu];
5044 if (!cpumask_test_cpu(cpu, buffer->cpumask))
5047 /* prevent another thread from changing buffer sizes */
5048 mutex_lock(&buffer->mutex);
5050 atomic_inc(&cpu_buffer->resize_disabled);
5051 atomic_inc(&cpu_buffer->record_disabled);
5053 /* Make sure all commits have finished */
5056 reset_disabled_cpu_buffer(cpu_buffer);
5058 atomic_dec(&cpu_buffer->record_disabled);
5059 atomic_dec(&cpu_buffer->resize_disabled);
5061 mutex_unlock(&buffer->mutex);
5063 EXPORT_SYMBOL_GPL(ring_buffer_reset_cpu);
5066 * ring_buffer_reset_cpu - reset a ring buffer per CPU buffer
5067 * @buffer: The ring buffer to reset a per cpu buffer of
5068 * @cpu: The CPU buffer to be reset
5070 void ring_buffer_reset_online_cpus(struct trace_buffer *buffer)
5072 struct ring_buffer_per_cpu *cpu_buffer;
5075 /* prevent another thread from changing buffer sizes */
5076 mutex_lock(&buffer->mutex);
5078 for_each_online_buffer_cpu(buffer, cpu) {
5079 cpu_buffer = buffer->buffers[cpu];
5081 atomic_inc(&cpu_buffer->resize_disabled);
5082 atomic_inc(&cpu_buffer->record_disabled);
5085 /* Make sure all commits have finished */
5088 for_each_online_buffer_cpu(buffer, cpu) {
5089 cpu_buffer = buffer->buffers[cpu];
5091 reset_disabled_cpu_buffer(cpu_buffer);
5093 atomic_dec(&cpu_buffer->record_disabled);
5094 atomic_dec(&cpu_buffer->resize_disabled);
5097 mutex_unlock(&buffer->mutex);
5101 * ring_buffer_reset - reset a ring buffer
5102 * @buffer: The ring buffer to reset all cpu buffers
5104 void ring_buffer_reset(struct trace_buffer *buffer)
5106 struct ring_buffer_per_cpu *cpu_buffer;
5109 for_each_buffer_cpu(buffer, cpu) {
5110 cpu_buffer = buffer->buffers[cpu];
5112 atomic_inc(&cpu_buffer->resize_disabled);
5113 atomic_inc(&cpu_buffer->record_disabled);
5116 /* Make sure all commits have finished */
5119 for_each_buffer_cpu(buffer, cpu) {
5120 cpu_buffer = buffer->buffers[cpu];
5122 reset_disabled_cpu_buffer(cpu_buffer);
5124 atomic_dec(&cpu_buffer->record_disabled);
5125 atomic_dec(&cpu_buffer->resize_disabled);
5128 EXPORT_SYMBOL_GPL(ring_buffer_reset);
5131 * rind_buffer_empty - is the ring buffer empty?
5132 * @buffer: The ring buffer to test
5134 bool ring_buffer_empty(struct trace_buffer *buffer)
5136 struct ring_buffer_per_cpu *cpu_buffer;
5137 unsigned long flags;
5142 /* yes this is racy, but if you don't like the race, lock the buffer */
5143 for_each_buffer_cpu(buffer, cpu) {
5144 cpu_buffer = buffer->buffers[cpu];
5145 local_irq_save(flags);
5146 dolock = rb_reader_lock(cpu_buffer);
5147 ret = rb_per_cpu_empty(cpu_buffer);
5148 rb_reader_unlock(cpu_buffer, dolock);
5149 local_irq_restore(flags);
5157 EXPORT_SYMBOL_GPL(ring_buffer_empty);
5160 * ring_buffer_empty_cpu - is a cpu buffer of a ring buffer empty?
5161 * @buffer: The ring buffer
5162 * @cpu: The CPU buffer to test
5164 bool ring_buffer_empty_cpu(struct trace_buffer *buffer, int cpu)
5166 struct ring_buffer_per_cpu *cpu_buffer;
5167 unsigned long flags;
5171 if (!cpumask_test_cpu(cpu, buffer->cpumask))
5174 cpu_buffer = buffer->buffers[cpu];
5175 local_irq_save(flags);
5176 dolock = rb_reader_lock(cpu_buffer);
5177 ret = rb_per_cpu_empty(cpu_buffer);
5178 rb_reader_unlock(cpu_buffer, dolock);
5179 local_irq_restore(flags);
5183 EXPORT_SYMBOL_GPL(ring_buffer_empty_cpu);
5185 #ifdef CONFIG_RING_BUFFER_ALLOW_SWAP
5187 * ring_buffer_swap_cpu - swap a CPU buffer between two ring buffers
5188 * @buffer_a: One buffer to swap with
5189 * @buffer_b: The other buffer to swap with
5190 * @cpu: the CPU of the buffers to swap
5192 * This function is useful for tracers that want to take a "snapshot"
5193 * of a CPU buffer and has another back up buffer lying around.
5194 * it is expected that the tracer handles the cpu buffer not being
5195 * used at the moment.
5197 int ring_buffer_swap_cpu(struct trace_buffer *buffer_a,
5198 struct trace_buffer *buffer_b, int cpu)
5200 struct ring_buffer_per_cpu *cpu_buffer_a;
5201 struct ring_buffer_per_cpu *cpu_buffer_b;
5204 if (!cpumask_test_cpu(cpu, buffer_a->cpumask) ||
5205 !cpumask_test_cpu(cpu, buffer_b->cpumask))
5208 cpu_buffer_a = buffer_a->buffers[cpu];
5209 cpu_buffer_b = buffer_b->buffers[cpu];
5211 /* At least make sure the two buffers are somewhat the same */
5212 if (cpu_buffer_a->nr_pages != cpu_buffer_b->nr_pages)
5217 if (atomic_read(&buffer_a->record_disabled))
5220 if (atomic_read(&buffer_b->record_disabled))
5223 if (atomic_read(&cpu_buffer_a->record_disabled))
5226 if (atomic_read(&cpu_buffer_b->record_disabled))
5230 * We can't do a synchronize_rcu here because this
5231 * function can be called in atomic context.
5232 * Normally this will be called from the same CPU as cpu.
5233 * If not it's up to the caller to protect this.
5235 atomic_inc(&cpu_buffer_a->record_disabled);
5236 atomic_inc(&cpu_buffer_b->record_disabled);
5239 if (local_read(&cpu_buffer_a->committing))
5241 if (local_read(&cpu_buffer_b->committing))
5244 buffer_a->buffers[cpu] = cpu_buffer_b;
5245 buffer_b->buffers[cpu] = cpu_buffer_a;
5247 cpu_buffer_b->buffer = buffer_a;
5248 cpu_buffer_a->buffer = buffer_b;
5253 atomic_dec(&cpu_buffer_a->record_disabled);
5254 atomic_dec(&cpu_buffer_b->record_disabled);
5258 EXPORT_SYMBOL_GPL(ring_buffer_swap_cpu);
5259 #endif /* CONFIG_RING_BUFFER_ALLOW_SWAP */
5262 * ring_buffer_alloc_read_page - allocate a page to read from buffer
5263 * @buffer: the buffer to allocate for.
5264 * @cpu: the cpu buffer to allocate.
5266 * This function is used in conjunction with ring_buffer_read_page.
5267 * When reading a full page from the ring buffer, these functions
5268 * can be used to speed up the process. The calling function should
5269 * allocate a few pages first with this function. Then when it
5270 * needs to get pages from the ring buffer, it passes the result
5271 * of this function into ring_buffer_read_page, which will swap
5272 * the page that was allocated, with the read page of the buffer.
5275 * The page allocated, or ERR_PTR
5277 void *ring_buffer_alloc_read_page(struct trace_buffer *buffer, int cpu)
5279 struct ring_buffer_per_cpu *cpu_buffer;
5280 struct buffer_data_page *bpage = NULL;
5281 unsigned long flags;
5284 if (!cpumask_test_cpu(cpu, buffer->cpumask))
5285 return ERR_PTR(-ENODEV);
5287 cpu_buffer = buffer->buffers[cpu];
5288 local_irq_save(flags);
5289 arch_spin_lock(&cpu_buffer->lock);
5291 if (cpu_buffer->free_page) {
5292 bpage = cpu_buffer->free_page;
5293 cpu_buffer->free_page = NULL;
5296 arch_spin_unlock(&cpu_buffer->lock);
5297 local_irq_restore(flags);
5302 page = alloc_pages_node(cpu_to_node(cpu),
5303 GFP_KERNEL | __GFP_NORETRY, 0);
5305 return ERR_PTR(-ENOMEM);
5307 bpage = page_address(page);
5310 rb_init_page(bpage);
5314 EXPORT_SYMBOL_GPL(ring_buffer_alloc_read_page);
5317 * ring_buffer_free_read_page - free an allocated read page
5318 * @buffer: the buffer the page was allocate for
5319 * @cpu: the cpu buffer the page came from
5320 * @data: the page to free
5322 * Free a page allocated from ring_buffer_alloc_read_page.
5324 void ring_buffer_free_read_page(struct trace_buffer *buffer, int cpu, void *data)
5326 struct ring_buffer_per_cpu *cpu_buffer = buffer->buffers[cpu];
5327 struct buffer_data_page *bpage = data;
5328 struct page *page = virt_to_page(bpage);
5329 unsigned long flags;
5331 /* If the page is still in use someplace else, we can't reuse it */
5332 if (page_ref_count(page) > 1)
5335 local_irq_save(flags);
5336 arch_spin_lock(&cpu_buffer->lock);
5338 if (!cpu_buffer->free_page) {
5339 cpu_buffer->free_page = bpage;
5343 arch_spin_unlock(&cpu_buffer->lock);
5344 local_irq_restore(flags);
5347 free_page((unsigned long)bpage);
5349 EXPORT_SYMBOL_GPL(ring_buffer_free_read_page);
5352 * ring_buffer_read_page - extract a page from the ring buffer
5353 * @buffer: buffer to extract from
5354 * @data_page: the page to use allocated from ring_buffer_alloc_read_page
5355 * @len: amount to extract
5356 * @cpu: the cpu of the buffer to extract
5357 * @full: should the extraction only happen when the page is full.
5359 * This function will pull out a page from the ring buffer and consume it.
5360 * @data_page must be the address of the variable that was returned
5361 * from ring_buffer_alloc_read_page. This is because the page might be used
5362 * to swap with a page in the ring buffer.
5365 * rpage = ring_buffer_alloc_read_page(buffer, cpu);
5366 * if (IS_ERR(rpage))
5367 * return PTR_ERR(rpage);
5368 * ret = ring_buffer_read_page(buffer, &rpage, len, cpu, 0);
5370 * process_page(rpage, ret);
5372 * When @full is set, the function will not return true unless
5373 * the writer is off the reader page.
5375 * Note: it is up to the calling functions to handle sleeps and wakeups.
5376 * The ring buffer can be used anywhere in the kernel and can not
5377 * blindly call wake_up. The layer that uses the ring buffer must be
5378 * responsible for that.
5381 * >=0 if data has been transferred, returns the offset of consumed data.
5382 * <0 if no data has been transferred.
5384 int ring_buffer_read_page(struct trace_buffer *buffer,
5385 void **data_page, size_t len, int cpu, int full)
5387 struct ring_buffer_per_cpu *cpu_buffer = buffer->buffers[cpu];
5388 struct ring_buffer_event *event;
5389 struct buffer_data_page *bpage;
5390 struct buffer_page *reader;
5391 unsigned long missed_events;
5392 unsigned long flags;
5393 unsigned int commit;
5398 if (!cpumask_test_cpu(cpu, buffer->cpumask))
5402 * If len is not big enough to hold the page header, then
5403 * we can not copy anything.
5405 if (len <= BUF_PAGE_HDR_SIZE)
5408 len -= BUF_PAGE_HDR_SIZE;
5417 raw_spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
5419 reader = rb_get_reader_page(cpu_buffer);
5423 event = rb_reader_event(cpu_buffer);
5425 read = reader->read;
5426 commit = rb_page_commit(reader);
5428 /* Check if any events were dropped */
5429 missed_events = cpu_buffer->lost_events;
5432 * If this page has been partially read or
5433 * if len is not big enough to read the rest of the page or
5434 * a writer is still on the page, then
5435 * we must copy the data from the page to the buffer.
5436 * Otherwise, we can simply swap the page with the one passed in.
5438 if (read || (len < (commit - read)) ||
5439 cpu_buffer->reader_page == cpu_buffer->commit_page) {
5440 struct buffer_data_page *rpage = cpu_buffer->reader_page->page;
5441 unsigned int rpos = read;
5442 unsigned int pos = 0;
5448 if (len > (commit - read))
5449 len = (commit - read);
5451 /* Always keep the time extend and data together */
5452 size = rb_event_ts_length(event);
5457 /* save the current timestamp, since the user will need it */
5458 save_timestamp = cpu_buffer->read_stamp;
5460 /* Need to copy one event at a time */
5462 /* We need the size of one event, because
5463 * rb_advance_reader only advances by one event,
5464 * whereas rb_event_ts_length may include the size of
5465 * one or two events.
5466 * We have already ensured there's enough space if this
5467 * is a time extend. */
5468 size = rb_event_length(event);
5469 memcpy(bpage->data + pos, rpage->data + rpos, size);
5473 rb_advance_reader(cpu_buffer);
5474 rpos = reader->read;
5480 event = rb_reader_event(cpu_buffer);
5481 /* Always keep the time extend and data together */
5482 size = rb_event_ts_length(event);
5483 } while (len >= size);
5486 local_set(&bpage->commit, pos);
5487 bpage->time_stamp = save_timestamp;
5489 /* we copied everything to the beginning */
5492 /* update the entry counter */
5493 cpu_buffer->read += rb_page_entries(reader);
5494 cpu_buffer->read_bytes += BUF_PAGE_SIZE;
5496 /* swap the pages */
5497 rb_init_page(bpage);
5498 bpage = reader->page;
5499 reader->page = *data_page;
5500 local_set(&reader->write, 0);
5501 local_set(&reader->entries, 0);
5506 * Use the real_end for the data size,
5507 * This gives us a chance to store the lost events
5510 if (reader->real_end)
5511 local_set(&bpage->commit, reader->real_end);
5515 cpu_buffer->lost_events = 0;
5517 commit = local_read(&bpage->commit);
5519 * Set a flag in the commit field if we lost events
5521 if (missed_events) {
5522 /* If there is room at the end of the page to save the
5523 * missed events, then record it there.
5525 if (BUF_PAGE_SIZE - commit >= sizeof(missed_events)) {
5526 memcpy(&bpage->data[commit], &missed_events,
5527 sizeof(missed_events));
5528 local_add(RB_MISSED_STORED, &bpage->commit);
5529 commit += sizeof(missed_events);
5531 local_add(RB_MISSED_EVENTS, &bpage->commit);
5535 * This page may be off to user land. Zero it out here.
5537 if (commit < BUF_PAGE_SIZE)
5538 memset(&bpage->data[commit], 0, BUF_PAGE_SIZE - commit);
5541 raw_spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
5546 EXPORT_SYMBOL_GPL(ring_buffer_read_page);
5549 * We only allocate new buffers, never free them if the CPU goes down.
5550 * If we were to free the buffer, then the user would lose any trace that was in
5553 int trace_rb_cpu_prepare(unsigned int cpu, struct hlist_node *node)
5555 struct trace_buffer *buffer;
5558 unsigned long nr_pages;
5560 buffer = container_of(node, struct trace_buffer, node);
5561 if (cpumask_test_cpu(cpu, buffer->cpumask))
5566 /* check if all cpu sizes are same */
5567 for_each_buffer_cpu(buffer, cpu_i) {
5568 /* fill in the size from first enabled cpu */
5570 nr_pages = buffer->buffers[cpu_i]->nr_pages;
5571 if (nr_pages != buffer->buffers[cpu_i]->nr_pages) {
5576 /* allocate minimum pages, user can later expand it */
5579 buffer->buffers[cpu] =
5580 rb_allocate_cpu_buffer(buffer, nr_pages, cpu);
5581 if (!buffer->buffers[cpu]) {
5582 WARN(1, "failed to allocate ring buffer on CPU %u\n",
5587 cpumask_set_cpu(cpu, buffer->cpumask);
5591 #ifdef CONFIG_RING_BUFFER_STARTUP_TEST
5593 * This is a basic integrity check of the ring buffer.
5594 * Late in the boot cycle this test will run when configured in.
5595 * It will kick off a thread per CPU that will go into a loop
5596 * writing to the per cpu ring buffer various sizes of data.
5597 * Some of the data will be large items, some small.
5599 * Another thread is created that goes into a spin, sending out
5600 * IPIs to the other CPUs to also write into the ring buffer.
5601 * this is to test the nesting ability of the buffer.
5603 * Basic stats are recorded and reported. If something in the
5604 * ring buffer should happen that's not expected, a big warning
5605 * is displayed and all ring buffers are disabled.
5607 static struct task_struct *rb_threads[NR_CPUS] __initdata;
5609 struct rb_test_data {
5610 struct trace_buffer *buffer;
5611 unsigned long events;
5612 unsigned long bytes_written;
5613 unsigned long bytes_alloc;
5614 unsigned long bytes_dropped;
5615 unsigned long events_nested;
5616 unsigned long bytes_written_nested;
5617 unsigned long bytes_alloc_nested;
5618 unsigned long bytes_dropped_nested;
5619 int min_size_nested;
5620 int max_size_nested;
5627 static struct rb_test_data rb_data[NR_CPUS] __initdata;
5630 #define RB_TEST_BUFFER_SIZE 1048576
5632 static char rb_string[] __initdata =
5633 "abcdefghijklmnopqrstuvwxyz1234567890!@#$%^&*()?+\\"
5634 "?+|:';\",.<>/?abcdefghijklmnopqrstuvwxyz1234567890"
5635 "!@#$%^&*()?+\\?+|:';\",.<>/?abcdefghijklmnopqrstuv";
5637 static bool rb_test_started __initdata;
5644 static __init int rb_write_something(struct rb_test_data *data, bool nested)
5646 struct ring_buffer_event *event;
5647 struct rb_item *item;
5654 /* Have nested writes different that what is written */
5655 cnt = data->cnt + (nested ? 27 : 0);
5657 /* Multiply cnt by ~e, to make some unique increment */
5658 size = (cnt * 68 / 25) % (sizeof(rb_string) - 1);
5660 len = size + sizeof(struct rb_item);
5662 started = rb_test_started;
5663 /* read rb_test_started before checking buffer enabled */
5666 event = ring_buffer_lock_reserve(data->buffer, len);
5668 /* Ignore dropped events before test starts. */
5671 data->bytes_dropped += len;
5673 data->bytes_dropped_nested += len;
5678 event_len = ring_buffer_event_length(event);
5680 if (RB_WARN_ON(data->buffer, event_len < len))
5683 item = ring_buffer_event_data(event);
5685 memcpy(item->str, rb_string, size);
5688 data->bytes_alloc_nested += event_len;
5689 data->bytes_written_nested += len;
5690 data->events_nested++;
5691 if (!data->min_size_nested || len < data->min_size_nested)
5692 data->min_size_nested = len;
5693 if (len > data->max_size_nested)
5694 data->max_size_nested = len;
5696 data->bytes_alloc += event_len;
5697 data->bytes_written += len;
5699 if (!data->min_size || len < data->min_size)
5700 data->max_size = len;
5701 if (len > data->max_size)
5702 data->max_size = len;
5706 ring_buffer_unlock_commit(data->buffer, event);
5711 static __init int rb_test(void *arg)
5713 struct rb_test_data *data = arg;
5715 while (!kthread_should_stop()) {
5716 rb_write_something(data, false);
5719 set_current_state(TASK_INTERRUPTIBLE);
5720 /* Now sleep between a min of 100-300us and a max of 1ms */
5721 usleep_range(((data->cnt % 3) + 1) * 100, 1000);
5727 static __init void rb_ipi(void *ignore)
5729 struct rb_test_data *data;
5730 int cpu = smp_processor_id();
5732 data = &rb_data[cpu];
5733 rb_write_something(data, true);
5736 static __init int rb_hammer_test(void *arg)
5738 while (!kthread_should_stop()) {
5740 /* Send an IPI to all cpus to write data! */
5741 smp_call_function(rb_ipi, NULL, 1);
5742 /* No sleep, but for non preempt, let others run */
5749 static __init int test_ringbuffer(void)
5751 struct task_struct *rb_hammer;
5752 struct trace_buffer *buffer;
5756 if (security_locked_down(LOCKDOWN_TRACEFS)) {
5757 pr_warn("Lockdown is enabled, skipping ring buffer tests\n");
5761 pr_info("Running ring buffer tests...\n");
5763 buffer = ring_buffer_alloc(RB_TEST_BUFFER_SIZE, RB_FL_OVERWRITE);
5764 if (WARN_ON(!buffer))
5767 /* Disable buffer so that threads can't write to it yet */
5768 ring_buffer_record_off(buffer);
5770 for_each_online_cpu(cpu) {
5771 rb_data[cpu].buffer = buffer;
5772 rb_data[cpu].cpu = cpu;
5773 rb_data[cpu].cnt = cpu;
5774 rb_threads[cpu] = kthread_create(rb_test, &rb_data[cpu],
5775 "rbtester/%d", cpu);
5776 if (WARN_ON(IS_ERR(rb_threads[cpu]))) {
5777 pr_cont("FAILED\n");
5778 ret = PTR_ERR(rb_threads[cpu]);
5782 kthread_bind(rb_threads[cpu], cpu);
5783 wake_up_process(rb_threads[cpu]);
5786 /* Now create the rb hammer! */
5787 rb_hammer = kthread_run(rb_hammer_test, NULL, "rbhammer");
5788 if (WARN_ON(IS_ERR(rb_hammer))) {
5789 pr_cont("FAILED\n");
5790 ret = PTR_ERR(rb_hammer);
5794 ring_buffer_record_on(buffer);
5796 * Show buffer is enabled before setting rb_test_started.
5797 * Yes there's a small race window where events could be
5798 * dropped and the thread wont catch it. But when a ring
5799 * buffer gets enabled, there will always be some kind of
5800 * delay before other CPUs see it. Thus, we don't care about
5801 * those dropped events. We care about events dropped after
5802 * the threads see that the buffer is active.
5805 rb_test_started = true;
5807 set_current_state(TASK_INTERRUPTIBLE);
5808 /* Just run for 10 seconds */;
5809 schedule_timeout(10 * HZ);
5811 kthread_stop(rb_hammer);
5814 for_each_online_cpu(cpu) {
5815 if (!rb_threads[cpu])
5817 kthread_stop(rb_threads[cpu]);
5820 ring_buffer_free(buffer);
5825 pr_info("finished\n");
5826 for_each_online_cpu(cpu) {
5827 struct ring_buffer_event *event;
5828 struct rb_test_data *data = &rb_data[cpu];
5829 struct rb_item *item;
5830 unsigned long total_events;
5831 unsigned long total_dropped;
5832 unsigned long total_written;
5833 unsigned long total_alloc;
5834 unsigned long total_read = 0;
5835 unsigned long total_size = 0;
5836 unsigned long total_len = 0;
5837 unsigned long total_lost = 0;
5840 int small_event_size;
5844 total_events = data->events + data->events_nested;
5845 total_written = data->bytes_written + data->bytes_written_nested;
5846 total_alloc = data->bytes_alloc + data->bytes_alloc_nested;
5847 total_dropped = data->bytes_dropped + data->bytes_dropped_nested;
5849 big_event_size = data->max_size + data->max_size_nested;
5850 small_event_size = data->min_size + data->min_size_nested;
5852 pr_info("CPU %d:\n", cpu);
5853 pr_info(" events: %ld\n", total_events);
5854 pr_info(" dropped bytes: %ld\n", total_dropped);
5855 pr_info(" alloced bytes: %ld\n", total_alloc);
5856 pr_info(" written bytes: %ld\n", total_written);
5857 pr_info(" biggest event: %d\n", big_event_size);
5858 pr_info(" smallest event: %d\n", small_event_size);
5860 if (RB_WARN_ON(buffer, total_dropped))
5865 while ((event = ring_buffer_consume(buffer, cpu, NULL, &lost))) {
5867 item = ring_buffer_event_data(event);
5868 total_len += ring_buffer_event_length(event);
5869 total_size += item->size + sizeof(struct rb_item);
5870 if (memcmp(&item->str[0], rb_string, item->size) != 0) {
5871 pr_info("FAILED!\n");
5872 pr_info("buffer had: %.*s\n", item->size, item->str);
5873 pr_info("expected: %.*s\n", item->size, rb_string);
5874 RB_WARN_ON(buffer, 1);
5885 pr_info(" read events: %ld\n", total_read);
5886 pr_info(" lost events: %ld\n", total_lost);
5887 pr_info(" total events: %ld\n", total_lost + total_read);
5888 pr_info(" recorded len bytes: %ld\n", total_len);
5889 pr_info(" recorded size bytes: %ld\n", total_size);
5891 pr_info(" With dropped events, record len and size may not match\n"
5892 " alloced and written from above\n");
5894 if (RB_WARN_ON(buffer, total_len != total_alloc ||
5895 total_size != total_written))
5898 if (RB_WARN_ON(buffer, total_lost + total_read != total_events))
5904 pr_info("Ring buffer PASSED!\n");
5906 ring_buffer_free(buffer);
5910 late_initcall(test_ringbuffer);
5911 #endif /* CONFIG_RING_BUFFER_STARTUP_TEST */