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)
290 static u64 rb_event_time_stamp(struct ring_buffer_event *event)
294 ts = event->array[0];
296 ts += event->time_delta;
301 /* Flag when events were overwritten */
302 #define RB_MISSED_EVENTS (1 << 31)
303 /* Missed count stored at end */
304 #define RB_MISSED_STORED (1 << 30)
306 struct buffer_data_page {
307 u64 time_stamp; /* page time stamp */
308 local_t commit; /* write committed index */
309 unsigned char data[] RB_ALIGN_DATA; /* data of buffer page */
313 * Note, the buffer_page list must be first. The buffer pages
314 * are allocated in cache lines, which means that each buffer
315 * page will be at the beginning of a cache line, and thus
316 * the least significant bits will be zero. We use this to
317 * add flags in the list struct pointers, to make the ring buffer
321 struct list_head list; /* list of buffer pages */
322 local_t write; /* index for next write */
323 unsigned read; /* index for next read */
324 local_t entries; /* entries on this page */
325 unsigned long real_end; /* real end of data */
326 struct buffer_data_page *page; /* Actual data page */
330 * The buffer page counters, write and entries, must be reset
331 * atomically when crossing page boundaries. To synchronize this
332 * update, two counters are inserted into the number. One is
333 * the actual counter for the write position or count on the page.
335 * The other is a counter of updaters. Before an update happens
336 * the update partition of the counter is incremented. This will
337 * allow the updater to update the counter atomically.
339 * The counter is 20 bits, and the state data is 12.
341 #define RB_WRITE_MASK 0xfffff
342 #define RB_WRITE_INTCNT (1 << 20)
344 static void rb_init_page(struct buffer_data_page *bpage)
346 local_set(&bpage->commit, 0);
349 static __always_inline unsigned int rb_page_commit(struct buffer_page *bpage)
351 return local_read(&bpage->page->commit);
354 static void free_buffer_page(struct buffer_page *bpage)
356 free_page((unsigned long)bpage->page);
361 * We need to fit the time_stamp delta into 27 bits.
363 static inline int test_time_stamp(u64 delta)
365 if (delta & TS_DELTA_TEST)
370 #define BUF_PAGE_SIZE (PAGE_SIZE - BUF_PAGE_HDR_SIZE)
372 /* Max payload is BUF_PAGE_SIZE - header (8bytes) */
373 #define BUF_MAX_DATA_SIZE (BUF_PAGE_SIZE - (sizeof(u32) * 2))
375 int ring_buffer_print_page_header(struct trace_seq *s)
377 struct buffer_data_page field;
379 trace_seq_printf(s, "\tfield: u64 timestamp;\t"
380 "offset:0;\tsize:%u;\tsigned:%u;\n",
381 (unsigned int)sizeof(field.time_stamp),
382 (unsigned int)is_signed_type(u64));
384 trace_seq_printf(s, "\tfield: local_t commit;\t"
385 "offset:%u;\tsize:%u;\tsigned:%u;\n",
386 (unsigned int)offsetof(typeof(field), commit),
387 (unsigned int)sizeof(field.commit),
388 (unsigned int)is_signed_type(long));
390 trace_seq_printf(s, "\tfield: int overwrite;\t"
391 "offset:%u;\tsize:%u;\tsigned:%u;\n",
392 (unsigned int)offsetof(typeof(field), commit),
394 (unsigned int)is_signed_type(long));
396 trace_seq_printf(s, "\tfield: char data;\t"
397 "offset:%u;\tsize:%u;\tsigned:%u;\n",
398 (unsigned int)offsetof(typeof(field), data),
399 (unsigned int)BUF_PAGE_SIZE,
400 (unsigned int)is_signed_type(char));
402 return !trace_seq_has_overflowed(s);
406 struct irq_work work;
407 wait_queue_head_t waiters;
408 wait_queue_head_t full_waiters;
410 bool waiters_pending;
411 bool full_waiters_pending;
416 * Structure to hold event state and handle nested events.
418 struct rb_event_info {
423 unsigned long length;
424 struct buffer_page *tail_page;
429 * Used for the add_timestamp
431 * EXTEND - wants a time extend
432 * ABSOLUTE - the buffer requests all events to have absolute time stamps
433 * FORCE - force a full time stamp.
436 RB_ADD_STAMP_NONE = 0,
437 RB_ADD_STAMP_EXTEND = BIT(1),
438 RB_ADD_STAMP_ABSOLUTE = BIT(2),
439 RB_ADD_STAMP_FORCE = BIT(3)
442 * Used for which event context the event is in.
449 * See trace_recursive_lock() comment below for more details.
460 #if BITS_PER_LONG == 32
464 /* To test on 64 bit machines */
469 struct rb_time_struct {
475 #include <asm/local64.h>
476 struct rb_time_struct {
480 typedef struct rb_time_struct rb_time_t;
485 * head_page == tail_page && head == tail then buffer is empty.
487 struct ring_buffer_per_cpu {
489 atomic_t record_disabled;
490 atomic_t resize_disabled;
491 struct trace_buffer *buffer;
492 raw_spinlock_t reader_lock; /* serialize readers */
493 arch_spinlock_t lock;
494 struct lock_class_key lock_key;
495 struct buffer_data_page *free_page;
496 unsigned long nr_pages;
497 unsigned int current_context;
498 struct list_head *pages;
499 struct buffer_page *head_page; /* read from head */
500 struct buffer_page *tail_page; /* write to tail */
501 struct buffer_page *commit_page; /* committed pages */
502 struct buffer_page *reader_page;
503 unsigned long lost_events;
504 unsigned long last_overrun;
506 local_t entries_bytes;
509 local_t commit_overrun;
510 local_t dropped_events;
513 local_t pages_touched;
516 long last_pages_touch;
517 size_t shortest_full;
519 unsigned long read_bytes;
520 rb_time_t write_stamp;
521 rb_time_t before_stamp;
522 u64 event_stamp[MAX_NEST];
524 /* pages removed since last reset */
525 unsigned long pages_removed;
526 /* ring buffer pages to update, > 0 to add, < 0 to remove */
527 long nr_pages_to_update;
528 struct list_head new_pages; /* new pages to add */
529 struct work_struct update_pages_work;
530 struct completion update_done;
532 struct rb_irq_work irq_work;
535 struct trace_buffer {
538 atomic_t record_disabled;
540 cpumask_var_t cpumask;
542 struct lock_class_key *reader_lock_key;
546 struct ring_buffer_per_cpu **buffers;
548 struct hlist_node node;
551 struct rb_irq_work irq_work;
555 struct ring_buffer_iter {
556 struct ring_buffer_per_cpu *cpu_buffer;
558 unsigned long next_event;
559 struct buffer_page *head_page;
560 struct buffer_page *cache_reader_page;
561 unsigned long cache_read;
562 unsigned long cache_pages_removed;
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 * Enable this to make sure that the event passed to
754 * ring_buffer_event_time_stamp() is not committed and also
755 * is on the buffer that it passed in.
757 //#define RB_VERIFY_EVENT
758 #ifdef RB_VERIFY_EVENT
759 static struct list_head *rb_list_head(struct list_head *list);
760 static void verify_event(struct ring_buffer_per_cpu *cpu_buffer,
763 struct buffer_page *page = cpu_buffer->commit_page;
764 struct buffer_page *tail_page = READ_ONCE(cpu_buffer->tail_page);
765 struct list_head *next;
767 unsigned long addr = (unsigned long)event;
771 /* Make sure the event exists and is not committed yet */
773 if (page == tail_page || WARN_ON_ONCE(stop++ > 100))
775 commit = local_read(&page->page->commit);
776 write = local_read(&page->write);
777 if (addr >= (unsigned long)&page->page->data[commit] &&
778 addr < (unsigned long)&page->page->data[write])
781 next = rb_list_head(page->list.next);
782 page = list_entry(next, struct buffer_page, list);
787 static inline void verify_event(struct ring_buffer_per_cpu *cpu_buffer,
794 static inline u64 rb_time_stamp(struct trace_buffer *buffer);
797 * ring_buffer_event_time_stamp - return the event's current time stamp
798 * @buffer: The buffer that the event is on
799 * @event: the event to get the time stamp of
801 * Note, this must be called after @event is reserved, and before it is
802 * committed to the ring buffer. And must be called from the same
803 * context where the event was reserved (normal, softirq, irq, etc).
805 * Returns the time stamp associated with the current event.
806 * If the event has an extended time stamp, then that is used as
807 * the time stamp to return.
808 * In the highly unlikely case that the event was nested more than
809 * the max nesting, then the write_stamp of the buffer is returned,
810 * otherwise current time is returned, but that really neither of
811 * the last two cases should ever happen.
813 u64 ring_buffer_event_time_stamp(struct trace_buffer *buffer,
814 struct ring_buffer_event *event)
816 struct ring_buffer_per_cpu *cpu_buffer = buffer->buffers[smp_processor_id()];
820 /* If the event includes an absolute time, then just use that */
821 if (event->type_len == RINGBUF_TYPE_TIME_STAMP)
822 return rb_event_time_stamp(event);
824 nest = local_read(&cpu_buffer->committing);
825 verify_event(cpu_buffer, event);
826 if (WARN_ON_ONCE(!nest))
829 /* Read the current saved nesting level time stamp */
830 if (likely(--nest < MAX_NEST))
831 return cpu_buffer->event_stamp[nest];
833 /* Shouldn't happen, warn if it does */
834 WARN_ONCE(1, "nest (%d) greater than max", nest);
837 /* Can only fail on 32 bit */
838 if (!rb_time_read(&cpu_buffer->write_stamp, &ts))
839 /* Screw it, just read the current time */
840 ts = rb_time_stamp(cpu_buffer->buffer);
846 * ring_buffer_nr_pages - get the number of buffer pages in the ring buffer
847 * @buffer: The ring_buffer to get the number of pages from
848 * @cpu: The cpu of the ring_buffer to get the number of pages from
850 * Returns the number of pages used by a per_cpu buffer of the ring buffer.
852 size_t ring_buffer_nr_pages(struct trace_buffer *buffer, int cpu)
854 return buffer->buffers[cpu]->nr_pages;
858 * ring_buffer_nr_pages_dirty - get the number of used pages in the ring buffer
859 * @buffer: The ring_buffer to get the number of pages from
860 * @cpu: The cpu of the ring_buffer to get the number of pages from
862 * Returns the number of pages that have content in the ring buffer.
864 size_t ring_buffer_nr_dirty_pages(struct trace_buffer *buffer, int cpu)
870 read = local_read(&buffer->buffers[cpu]->pages_read);
871 lost = local_read(&buffer->buffers[cpu]->pages_lost);
872 cnt = local_read(&buffer->buffers[cpu]->pages_touched);
874 if (WARN_ON_ONCE(cnt < lost))
879 /* The reader can read an empty page, but not more than that */
881 WARN_ON_ONCE(read > cnt + 1);
888 static __always_inline bool full_hit(struct trace_buffer *buffer, int cpu, int full)
890 struct ring_buffer_per_cpu *cpu_buffer = buffer->buffers[cpu];
894 nr_pages = cpu_buffer->nr_pages;
895 if (!nr_pages || !full)
898 dirty = ring_buffer_nr_dirty_pages(buffer, cpu);
900 return (dirty * 100) > (full * nr_pages);
904 * rb_wake_up_waiters - wake up tasks waiting for ring buffer input
906 * Schedules a delayed work to wake up any task that is blocked on the
907 * ring buffer waiters queue.
909 static void rb_wake_up_waiters(struct irq_work *work)
911 struct rb_irq_work *rbwork = container_of(work, struct rb_irq_work, work);
913 wake_up_all(&rbwork->waiters);
914 if (rbwork->full_waiters_pending || rbwork->wakeup_full) {
915 rbwork->wakeup_full = false;
916 rbwork->full_waiters_pending = false;
917 wake_up_all(&rbwork->full_waiters);
922 * ring_buffer_wake_waiters - wake up any waiters on this ring buffer
923 * @buffer: The ring buffer to wake waiters on
925 * In the case of a file that represents a ring buffer is closing,
926 * it is prudent to wake up any waiters that are on this.
928 void ring_buffer_wake_waiters(struct trace_buffer *buffer, int cpu)
930 struct ring_buffer_per_cpu *cpu_buffer;
931 struct rb_irq_work *rbwork;
936 if (cpu == RING_BUFFER_ALL_CPUS) {
938 /* Wake up individual ones too. One level recursion */
939 for_each_buffer_cpu(buffer, cpu)
940 ring_buffer_wake_waiters(buffer, cpu);
942 rbwork = &buffer->irq_work;
944 if (WARN_ON_ONCE(!buffer->buffers))
946 if (WARN_ON_ONCE(cpu >= nr_cpu_ids))
949 cpu_buffer = buffer->buffers[cpu];
950 /* The CPU buffer may not have been initialized yet */
953 rbwork = &cpu_buffer->irq_work;
956 rbwork->wait_index++;
957 /* make sure the waiters see the new index */
960 rb_wake_up_waiters(&rbwork->work);
964 * ring_buffer_wait - wait for input to the ring buffer
965 * @buffer: buffer to wait on
966 * @cpu: the cpu buffer to wait on
967 * @full: wait until the percentage of pages are available, if @cpu != RING_BUFFER_ALL_CPUS
969 * If @cpu == RING_BUFFER_ALL_CPUS then the task will wake up as soon
970 * as data is added to any of the @buffer's cpu buffers. Otherwise
971 * it will wait for data to be added to a specific cpu buffer.
973 int ring_buffer_wait(struct trace_buffer *buffer, int cpu, int full)
975 struct ring_buffer_per_cpu *cpu_buffer;
977 struct rb_irq_work *work;
982 * Depending on what the caller is waiting for, either any
983 * data in any cpu buffer, or a specific buffer, put the
984 * caller on the appropriate wait queue.
986 if (cpu == RING_BUFFER_ALL_CPUS) {
987 work = &buffer->irq_work;
988 /* Full only makes sense on per cpu reads */
991 if (!cpumask_test_cpu(cpu, buffer->cpumask))
993 cpu_buffer = buffer->buffers[cpu];
994 work = &cpu_buffer->irq_work;
997 wait_index = READ_ONCE(work->wait_index);
1001 prepare_to_wait(&work->full_waiters, &wait, TASK_INTERRUPTIBLE);
1003 prepare_to_wait(&work->waiters, &wait, TASK_INTERRUPTIBLE);
1006 * The events can happen in critical sections where
1007 * checking a work queue can cause deadlocks.
1008 * After adding a task to the queue, this flag is set
1009 * only to notify events to try to wake up the queue
1012 * We don't clear it even if the buffer is no longer
1013 * empty. The flag only causes the next event to run
1014 * irq_work to do the work queue wake up. The worse
1015 * that can happen if we race with !trace_empty() is that
1016 * an event will cause an irq_work to try to wake up
1019 * There's no reason to protect this flag either, as
1020 * the work queue and irq_work logic will do the necessary
1021 * synchronization for the wake ups. The only thing
1022 * that is necessary is that the wake up happens after
1023 * a task has been queued. It's OK for spurious wake ups.
1026 work->full_waiters_pending = true;
1028 work->waiters_pending = true;
1030 if (signal_pending(current)) {
1035 if (cpu == RING_BUFFER_ALL_CPUS && !ring_buffer_empty(buffer))
1038 if (cpu != RING_BUFFER_ALL_CPUS &&
1039 !ring_buffer_empty_cpu(buffer, cpu)) {
1040 unsigned long flags;
1047 raw_spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
1048 pagebusy = cpu_buffer->reader_page == cpu_buffer->commit_page;
1049 done = !pagebusy && full_hit(buffer, cpu, full);
1051 if (!cpu_buffer->shortest_full ||
1052 cpu_buffer->shortest_full > full)
1053 cpu_buffer->shortest_full = full;
1054 raw_spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
1061 /* Make sure to see the new wait index */
1063 if (wait_index != work->wait_index)
1068 finish_wait(&work->full_waiters, &wait);
1070 finish_wait(&work->waiters, &wait);
1076 * ring_buffer_poll_wait - poll on buffer input
1077 * @buffer: buffer to wait on
1078 * @cpu: the cpu buffer to wait on
1079 * @filp: the file descriptor
1080 * @poll_table: The poll descriptor
1081 * @full: wait until the percentage of pages are available, if @cpu != RING_BUFFER_ALL_CPUS
1083 * If @cpu == RING_BUFFER_ALL_CPUS then the task will wake up as soon
1084 * as data is added to any of the @buffer's cpu buffers. Otherwise
1085 * it will wait for data to be added to a specific cpu buffer.
1087 * Returns EPOLLIN | EPOLLRDNORM if data exists in the buffers,
1090 __poll_t ring_buffer_poll_wait(struct trace_buffer *buffer, int cpu,
1091 struct file *filp, poll_table *poll_table, int full)
1093 struct ring_buffer_per_cpu *cpu_buffer;
1094 struct rb_irq_work *work;
1096 if (cpu == RING_BUFFER_ALL_CPUS) {
1097 work = &buffer->irq_work;
1100 if (!cpumask_test_cpu(cpu, buffer->cpumask))
1103 cpu_buffer = buffer->buffers[cpu];
1104 work = &cpu_buffer->irq_work;
1108 poll_wait(filp, &work->full_waiters, poll_table);
1109 work->full_waiters_pending = true;
1110 if (!cpu_buffer->shortest_full ||
1111 cpu_buffer->shortest_full > full)
1112 cpu_buffer->shortest_full = full;
1114 poll_wait(filp, &work->waiters, poll_table);
1115 work->waiters_pending = true;
1119 * There's a tight race between setting the waiters_pending and
1120 * checking if the ring buffer is empty. Once the waiters_pending bit
1121 * is set, the next event will wake the task up, but we can get stuck
1122 * if there's only a single event in.
1124 * FIXME: Ideally, we need a memory barrier on the writer side as well,
1125 * but adding a memory barrier to all events will cause too much of a
1126 * performance hit in the fast path. We only need a memory barrier when
1127 * the buffer goes from empty to having content. But as this race is
1128 * extremely small, and it's not a problem if another event comes in, we
1129 * will fix it later.
1134 return full_hit(buffer, cpu, full) ? EPOLLIN | EPOLLRDNORM : 0;
1136 if ((cpu == RING_BUFFER_ALL_CPUS && !ring_buffer_empty(buffer)) ||
1137 (cpu != RING_BUFFER_ALL_CPUS && !ring_buffer_empty_cpu(buffer, cpu)))
1138 return EPOLLIN | EPOLLRDNORM;
1142 /* buffer may be either ring_buffer or ring_buffer_per_cpu */
1143 #define RB_WARN_ON(b, cond) \
1145 int _____ret = unlikely(cond); \
1147 if (__same_type(*(b), struct ring_buffer_per_cpu)) { \
1148 struct ring_buffer_per_cpu *__b = \
1150 atomic_inc(&__b->buffer->record_disabled); \
1152 atomic_inc(&b->record_disabled); \
1158 /* Up this if you want to test the TIME_EXTENTS and normalization */
1159 #define DEBUG_SHIFT 0
1161 static inline u64 rb_time_stamp(struct trace_buffer *buffer)
1165 /* Skip retpolines :-( */
1166 if (IS_ENABLED(CONFIG_RETPOLINE) && likely(buffer->clock == trace_clock_local))
1167 ts = trace_clock_local();
1169 ts = buffer->clock();
1171 /* shift to debug/test normalization and TIME_EXTENTS */
1172 return ts << DEBUG_SHIFT;
1175 u64 ring_buffer_time_stamp(struct trace_buffer *buffer)
1179 preempt_disable_notrace();
1180 time = rb_time_stamp(buffer);
1181 preempt_enable_notrace();
1185 EXPORT_SYMBOL_GPL(ring_buffer_time_stamp);
1187 void ring_buffer_normalize_time_stamp(struct trace_buffer *buffer,
1190 /* Just stupid testing the normalize function and deltas */
1191 *ts >>= DEBUG_SHIFT;
1193 EXPORT_SYMBOL_GPL(ring_buffer_normalize_time_stamp);
1196 * Making the ring buffer lockless makes things tricky.
1197 * Although writes only happen on the CPU that they are on,
1198 * and they only need to worry about interrupts. Reads can
1199 * happen on any CPU.
1201 * The reader page is always off the ring buffer, but when the
1202 * reader finishes with a page, it needs to swap its page with
1203 * a new one from the buffer. The reader needs to take from
1204 * the head (writes go to the tail). But if a writer is in overwrite
1205 * mode and wraps, it must push the head page forward.
1207 * Here lies the problem.
1209 * The reader must be careful to replace only the head page, and
1210 * not another one. As described at the top of the file in the
1211 * ASCII art, the reader sets its old page to point to the next
1212 * page after head. It then sets the page after head to point to
1213 * the old reader page. But if the writer moves the head page
1214 * during this operation, the reader could end up with the tail.
1216 * We use cmpxchg to help prevent this race. We also do something
1217 * special with the page before head. We set the LSB to 1.
1219 * When the writer must push the page forward, it will clear the
1220 * bit that points to the head page, move the head, and then set
1221 * the bit that points to the new head page.
1223 * We also don't want an interrupt coming in and moving the head
1224 * page on another writer. Thus we use the second LSB to catch
1227 * head->list->prev->next bit 1 bit 0
1230 * Points to head page 0 1
1233 * Note we can not trust the prev pointer of the head page, because:
1235 * +----+ +-----+ +-----+
1236 * | |------>| T |---X--->| N |
1238 * +----+ +-----+ +-----+
1241 * +----------| R |----------+ |
1245 * Key: ---X--> HEAD flag set in pointer
1250 * (see __rb_reserve_next() to see where this happens)
1252 * What the above shows is that the reader just swapped out
1253 * the reader page with a page in the buffer, but before it
1254 * could make the new header point back to the new page added
1255 * it was preempted by a writer. The writer moved forward onto
1256 * the new page added by the reader and is about to move forward
1259 * You can see, it is legitimate for the previous pointer of
1260 * the head (or any page) not to point back to itself. But only
1264 #define RB_PAGE_NORMAL 0UL
1265 #define RB_PAGE_HEAD 1UL
1266 #define RB_PAGE_UPDATE 2UL
1269 #define RB_FLAG_MASK 3UL
1271 /* PAGE_MOVED is not part of the mask */
1272 #define RB_PAGE_MOVED 4UL
1275 * rb_list_head - remove any bit
1277 static struct list_head *rb_list_head(struct list_head *list)
1279 unsigned long val = (unsigned long)list;
1281 return (struct list_head *)(val & ~RB_FLAG_MASK);
1285 * rb_is_head_page - test if the given page is the head page
1287 * Because the reader may move the head_page pointer, we can
1288 * not trust what the head page is (it may be pointing to
1289 * the reader page). But if the next page is a header page,
1290 * its flags will be non zero.
1293 rb_is_head_page(struct buffer_page *page, struct list_head *list)
1297 val = (unsigned long)list->next;
1299 if ((val & ~RB_FLAG_MASK) != (unsigned long)&page->list)
1300 return RB_PAGE_MOVED;
1302 return val & RB_FLAG_MASK;
1308 * The unique thing about the reader page, is that, if the
1309 * writer is ever on it, the previous pointer never points
1310 * back to the reader page.
1312 static bool rb_is_reader_page(struct buffer_page *page)
1314 struct list_head *list = page->list.prev;
1316 return rb_list_head(list->next) != &page->list;
1320 * rb_set_list_to_head - set a list_head to be pointing to head.
1322 static void rb_set_list_to_head(struct list_head *list)
1326 ptr = (unsigned long *)&list->next;
1327 *ptr |= RB_PAGE_HEAD;
1328 *ptr &= ~RB_PAGE_UPDATE;
1332 * rb_head_page_activate - sets up head page
1334 static void rb_head_page_activate(struct ring_buffer_per_cpu *cpu_buffer)
1336 struct buffer_page *head;
1338 head = cpu_buffer->head_page;
1343 * Set the previous list pointer to have the HEAD flag.
1345 rb_set_list_to_head(head->list.prev);
1348 static void rb_list_head_clear(struct list_head *list)
1350 unsigned long *ptr = (unsigned long *)&list->next;
1352 *ptr &= ~RB_FLAG_MASK;
1356 * rb_head_page_deactivate - clears head page ptr (for free list)
1359 rb_head_page_deactivate(struct ring_buffer_per_cpu *cpu_buffer)
1361 struct list_head *hd;
1363 /* Go through the whole list and clear any pointers found. */
1364 rb_list_head_clear(cpu_buffer->pages);
1366 list_for_each(hd, cpu_buffer->pages)
1367 rb_list_head_clear(hd);
1370 static int rb_head_page_set(struct ring_buffer_per_cpu *cpu_buffer,
1371 struct buffer_page *head,
1372 struct buffer_page *prev,
1373 int old_flag, int new_flag)
1375 struct list_head *list;
1376 unsigned long val = (unsigned long)&head->list;
1381 val &= ~RB_FLAG_MASK;
1383 ret = cmpxchg((unsigned long *)&list->next,
1384 val | old_flag, val | new_flag);
1386 /* check if the reader took the page */
1387 if ((ret & ~RB_FLAG_MASK) != val)
1388 return RB_PAGE_MOVED;
1390 return ret & RB_FLAG_MASK;
1393 static int rb_head_page_set_update(struct ring_buffer_per_cpu *cpu_buffer,
1394 struct buffer_page *head,
1395 struct buffer_page *prev,
1398 return rb_head_page_set(cpu_buffer, head, prev,
1399 old_flag, RB_PAGE_UPDATE);
1402 static int rb_head_page_set_head(struct ring_buffer_per_cpu *cpu_buffer,
1403 struct buffer_page *head,
1404 struct buffer_page *prev,
1407 return rb_head_page_set(cpu_buffer, head, prev,
1408 old_flag, RB_PAGE_HEAD);
1411 static int rb_head_page_set_normal(struct ring_buffer_per_cpu *cpu_buffer,
1412 struct buffer_page *head,
1413 struct buffer_page *prev,
1416 return rb_head_page_set(cpu_buffer, head, prev,
1417 old_flag, RB_PAGE_NORMAL);
1420 static inline void rb_inc_page(struct buffer_page **bpage)
1422 struct list_head *p = rb_list_head((*bpage)->list.next);
1424 *bpage = list_entry(p, struct buffer_page, list);
1427 static struct buffer_page *
1428 rb_set_head_page(struct ring_buffer_per_cpu *cpu_buffer)
1430 struct buffer_page *head;
1431 struct buffer_page *page;
1432 struct list_head *list;
1435 if (RB_WARN_ON(cpu_buffer, !cpu_buffer->head_page))
1439 list = cpu_buffer->pages;
1440 if (RB_WARN_ON(cpu_buffer, rb_list_head(list->prev->next) != list))
1443 page = head = cpu_buffer->head_page;
1445 * It is possible that the writer moves the header behind
1446 * where we started, and we miss in one loop.
1447 * A second loop should grab the header, but we'll do
1448 * three loops just because I'm paranoid.
1450 for (i = 0; i < 3; i++) {
1452 if (rb_is_head_page(page, page->list.prev)) {
1453 cpu_buffer->head_page = page;
1457 } while (page != head);
1460 RB_WARN_ON(cpu_buffer, 1);
1465 static int rb_head_page_replace(struct buffer_page *old,
1466 struct buffer_page *new)
1468 unsigned long *ptr = (unsigned long *)&old->list.prev->next;
1472 val = *ptr & ~RB_FLAG_MASK;
1473 val |= RB_PAGE_HEAD;
1475 ret = cmpxchg(ptr, val, (unsigned long)&new->list);
1481 * rb_tail_page_update - move the tail page forward
1483 static void rb_tail_page_update(struct ring_buffer_per_cpu *cpu_buffer,
1484 struct buffer_page *tail_page,
1485 struct buffer_page *next_page)
1487 unsigned long old_entries;
1488 unsigned long old_write;
1491 * The tail page now needs to be moved forward.
1493 * We need to reset the tail page, but without messing
1494 * with possible erasing of data brought in by interrupts
1495 * that have moved the tail page and are currently on it.
1497 * We add a counter to the write field to denote this.
1499 old_write = local_add_return(RB_WRITE_INTCNT, &next_page->write);
1500 old_entries = local_add_return(RB_WRITE_INTCNT, &next_page->entries);
1502 local_inc(&cpu_buffer->pages_touched);
1504 * Just make sure we have seen our old_write and synchronize
1505 * with any interrupts that come in.
1510 * If the tail page is still the same as what we think
1511 * it is, then it is up to us to update the tail
1514 if (tail_page == READ_ONCE(cpu_buffer->tail_page)) {
1515 /* Zero the write counter */
1516 unsigned long val = old_write & ~RB_WRITE_MASK;
1517 unsigned long eval = old_entries & ~RB_WRITE_MASK;
1520 * This will only succeed if an interrupt did
1521 * not come in and change it. In which case, we
1522 * do not want to modify it.
1524 * We add (void) to let the compiler know that we do not care
1525 * about the return value of these functions. We use the
1526 * cmpxchg to only update if an interrupt did not already
1527 * do it for us. If the cmpxchg fails, we don't care.
1529 (void)local_cmpxchg(&next_page->write, old_write, val);
1530 (void)local_cmpxchg(&next_page->entries, old_entries, eval);
1533 * No need to worry about races with clearing out the commit.
1534 * it only can increment when a commit takes place. But that
1535 * only happens in the outer most nested commit.
1537 local_set(&next_page->page->commit, 0);
1539 /* Again, either we update tail_page or an interrupt does */
1540 (void)cmpxchg(&cpu_buffer->tail_page, tail_page, next_page);
1544 static int rb_check_bpage(struct ring_buffer_per_cpu *cpu_buffer,
1545 struct buffer_page *bpage)
1547 unsigned long val = (unsigned long)bpage;
1549 if (RB_WARN_ON(cpu_buffer, val & RB_FLAG_MASK))
1556 * rb_check_pages - integrity check of buffer pages
1557 * @cpu_buffer: CPU buffer with pages to test
1559 * As a safety measure we check to make sure the data pages have not
1562 static int rb_check_pages(struct ring_buffer_per_cpu *cpu_buffer)
1564 struct list_head *head = rb_list_head(cpu_buffer->pages);
1565 struct list_head *tmp;
1567 if (RB_WARN_ON(cpu_buffer,
1568 rb_list_head(rb_list_head(head->next)->prev) != head))
1571 if (RB_WARN_ON(cpu_buffer,
1572 rb_list_head(rb_list_head(head->prev)->next) != head))
1575 for (tmp = rb_list_head(head->next); tmp != head; tmp = rb_list_head(tmp->next)) {
1576 if (RB_WARN_ON(cpu_buffer,
1577 rb_list_head(rb_list_head(tmp->next)->prev) != tmp))
1580 if (RB_WARN_ON(cpu_buffer,
1581 rb_list_head(rb_list_head(tmp->prev)->next) != tmp))
1588 static int __rb_allocate_pages(struct ring_buffer_per_cpu *cpu_buffer,
1589 long nr_pages, struct list_head *pages)
1591 struct buffer_page *bpage, *tmp;
1592 bool user_thread = current->mm != NULL;
1597 * Check if the available memory is there first.
1598 * Note, si_mem_available() only gives us a rough estimate of available
1599 * memory. It may not be accurate. But we don't care, we just want
1600 * to prevent doing any allocation when it is obvious that it is
1601 * not going to succeed.
1603 i = si_mem_available();
1608 * __GFP_RETRY_MAYFAIL flag makes sure that the allocation fails
1609 * gracefully without invoking oom-killer and the system is not
1612 mflags = GFP_KERNEL | __GFP_RETRY_MAYFAIL;
1615 * If a user thread allocates too much, and si_mem_available()
1616 * reports there's enough memory, even though there is not.
1617 * Make sure the OOM killer kills this thread. This can happen
1618 * even with RETRY_MAYFAIL because another task may be doing
1619 * an allocation after this task has taken all memory.
1620 * This is the task the OOM killer needs to take out during this
1621 * loop, even if it was triggered by an allocation somewhere else.
1624 set_current_oom_origin();
1625 for (i = 0; i < nr_pages; i++) {
1628 bpage = kzalloc_node(ALIGN(sizeof(*bpage), cache_line_size()),
1629 mflags, cpu_to_node(cpu_buffer->cpu));
1633 rb_check_bpage(cpu_buffer, bpage);
1635 list_add(&bpage->list, pages);
1637 page = alloc_pages_node(cpu_to_node(cpu_buffer->cpu), mflags, 0);
1640 bpage->page = page_address(page);
1641 rb_init_page(bpage->page);
1643 if (user_thread && fatal_signal_pending(current))
1647 clear_current_oom_origin();
1652 list_for_each_entry_safe(bpage, tmp, pages, list) {
1653 list_del_init(&bpage->list);
1654 free_buffer_page(bpage);
1657 clear_current_oom_origin();
1662 static int rb_allocate_pages(struct ring_buffer_per_cpu *cpu_buffer,
1663 unsigned long nr_pages)
1669 if (__rb_allocate_pages(cpu_buffer, nr_pages, &pages))
1673 * The ring buffer page list is a circular list that does not
1674 * start and end with a list head. All page list items point to
1677 cpu_buffer->pages = pages.next;
1680 cpu_buffer->nr_pages = nr_pages;
1682 rb_check_pages(cpu_buffer);
1687 static struct ring_buffer_per_cpu *
1688 rb_allocate_cpu_buffer(struct trace_buffer *buffer, long nr_pages, int cpu)
1690 struct ring_buffer_per_cpu *cpu_buffer;
1691 struct buffer_page *bpage;
1695 cpu_buffer = kzalloc_node(ALIGN(sizeof(*cpu_buffer), cache_line_size()),
1696 GFP_KERNEL, cpu_to_node(cpu));
1700 cpu_buffer->cpu = cpu;
1701 cpu_buffer->buffer = buffer;
1702 raw_spin_lock_init(&cpu_buffer->reader_lock);
1703 lockdep_set_class(&cpu_buffer->reader_lock, buffer->reader_lock_key);
1704 cpu_buffer->lock = (arch_spinlock_t)__ARCH_SPIN_LOCK_UNLOCKED;
1705 INIT_WORK(&cpu_buffer->update_pages_work, update_pages_handler);
1706 init_completion(&cpu_buffer->update_done);
1707 init_irq_work(&cpu_buffer->irq_work.work, rb_wake_up_waiters);
1708 init_waitqueue_head(&cpu_buffer->irq_work.waiters);
1709 init_waitqueue_head(&cpu_buffer->irq_work.full_waiters);
1711 bpage = kzalloc_node(ALIGN(sizeof(*bpage), cache_line_size()),
1712 GFP_KERNEL, cpu_to_node(cpu));
1714 goto fail_free_buffer;
1716 rb_check_bpage(cpu_buffer, bpage);
1718 cpu_buffer->reader_page = bpage;
1719 page = alloc_pages_node(cpu_to_node(cpu), GFP_KERNEL, 0);
1721 goto fail_free_reader;
1722 bpage->page = page_address(page);
1723 rb_init_page(bpage->page);
1725 INIT_LIST_HEAD(&cpu_buffer->reader_page->list);
1726 INIT_LIST_HEAD(&cpu_buffer->new_pages);
1728 ret = rb_allocate_pages(cpu_buffer, nr_pages);
1730 goto fail_free_reader;
1732 cpu_buffer->head_page
1733 = list_entry(cpu_buffer->pages, struct buffer_page, list);
1734 cpu_buffer->tail_page = cpu_buffer->commit_page = cpu_buffer->head_page;
1736 rb_head_page_activate(cpu_buffer);
1741 free_buffer_page(cpu_buffer->reader_page);
1748 static void rb_free_cpu_buffer(struct ring_buffer_per_cpu *cpu_buffer)
1750 struct list_head *head = cpu_buffer->pages;
1751 struct buffer_page *bpage, *tmp;
1753 irq_work_sync(&cpu_buffer->irq_work.work);
1755 free_buffer_page(cpu_buffer->reader_page);
1758 rb_head_page_deactivate(cpu_buffer);
1760 list_for_each_entry_safe(bpage, tmp, head, list) {
1761 list_del_init(&bpage->list);
1762 free_buffer_page(bpage);
1764 bpage = list_entry(head, struct buffer_page, list);
1765 free_buffer_page(bpage);
1772 * __ring_buffer_alloc - allocate a new ring_buffer
1773 * @size: the size in bytes per cpu that is needed.
1774 * @flags: attributes to set for the ring buffer.
1775 * @key: ring buffer reader_lock_key.
1777 * Currently the only flag that is available is the RB_FL_OVERWRITE
1778 * flag. This flag means that the buffer will overwrite old data
1779 * when the buffer wraps. If this flag is not set, the buffer will
1780 * drop data when the tail hits the head.
1782 struct trace_buffer *__ring_buffer_alloc(unsigned long size, unsigned flags,
1783 struct lock_class_key *key)
1785 struct trace_buffer *buffer;
1791 /* keep it in its own cache line */
1792 buffer = kzalloc(ALIGN(sizeof(*buffer), cache_line_size()),
1797 if (!zalloc_cpumask_var(&buffer->cpumask, GFP_KERNEL))
1798 goto fail_free_buffer;
1800 nr_pages = DIV_ROUND_UP(size, BUF_PAGE_SIZE);
1801 buffer->flags = flags;
1802 buffer->clock = trace_clock_local;
1803 buffer->reader_lock_key = key;
1805 init_irq_work(&buffer->irq_work.work, rb_wake_up_waiters);
1806 init_waitqueue_head(&buffer->irq_work.waiters);
1808 /* need at least two pages */
1812 buffer->cpus = nr_cpu_ids;
1814 bsize = sizeof(void *) * nr_cpu_ids;
1815 buffer->buffers = kzalloc(ALIGN(bsize, cache_line_size()),
1817 if (!buffer->buffers)
1818 goto fail_free_cpumask;
1820 cpu = raw_smp_processor_id();
1821 cpumask_set_cpu(cpu, buffer->cpumask);
1822 buffer->buffers[cpu] = rb_allocate_cpu_buffer(buffer, nr_pages, cpu);
1823 if (!buffer->buffers[cpu])
1824 goto fail_free_buffers;
1826 ret = cpuhp_state_add_instance(CPUHP_TRACE_RB_PREPARE, &buffer->node);
1828 goto fail_free_buffers;
1830 mutex_init(&buffer->mutex);
1835 for_each_buffer_cpu(buffer, cpu) {
1836 if (buffer->buffers[cpu])
1837 rb_free_cpu_buffer(buffer->buffers[cpu]);
1839 kfree(buffer->buffers);
1842 free_cpumask_var(buffer->cpumask);
1848 EXPORT_SYMBOL_GPL(__ring_buffer_alloc);
1851 * ring_buffer_free - free a ring buffer.
1852 * @buffer: the buffer to free.
1855 ring_buffer_free(struct trace_buffer *buffer)
1859 cpuhp_state_remove_instance(CPUHP_TRACE_RB_PREPARE, &buffer->node);
1861 irq_work_sync(&buffer->irq_work.work);
1863 for_each_buffer_cpu(buffer, cpu)
1864 rb_free_cpu_buffer(buffer->buffers[cpu]);
1866 kfree(buffer->buffers);
1867 free_cpumask_var(buffer->cpumask);
1871 EXPORT_SYMBOL_GPL(ring_buffer_free);
1873 void ring_buffer_set_clock(struct trace_buffer *buffer,
1876 buffer->clock = clock;
1879 void ring_buffer_set_time_stamp_abs(struct trace_buffer *buffer, bool abs)
1881 buffer->time_stamp_abs = abs;
1884 bool ring_buffer_time_stamp_abs(struct trace_buffer *buffer)
1886 return buffer->time_stamp_abs;
1889 static void rb_reset_cpu(struct ring_buffer_per_cpu *cpu_buffer);
1891 static inline unsigned long rb_page_entries(struct buffer_page *bpage)
1893 return local_read(&bpage->entries) & RB_WRITE_MASK;
1896 static inline unsigned long rb_page_write(struct buffer_page *bpage)
1898 return local_read(&bpage->write) & RB_WRITE_MASK;
1902 rb_remove_pages(struct ring_buffer_per_cpu *cpu_buffer, unsigned long nr_pages)
1904 struct list_head *tail_page, *to_remove, *next_page;
1905 struct buffer_page *to_remove_page, *tmp_iter_page;
1906 struct buffer_page *last_page, *first_page;
1907 unsigned long nr_removed;
1908 unsigned long head_bit;
1913 raw_spin_lock_irq(&cpu_buffer->reader_lock);
1914 atomic_inc(&cpu_buffer->record_disabled);
1916 * We don't race with the readers since we have acquired the reader
1917 * lock. We also don't race with writers after disabling recording.
1918 * This makes it easy to figure out the first and the last page to be
1919 * removed from the list. We unlink all the pages in between including
1920 * the first and last pages. This is done in a busy loop so that we
1921 * lose the least number of traces.
1922 * The pages are freed after we restart recording and unlock readers.
1924 tail_page = &cpu_buffer->tail_page->list;
1927 * tail page might be on reader page, we remove the next page
1928 * from the ring buffer
1930 if (cpu_buffer->tail_page == cpu_buffer->reader_page)
1931 tail_page = rb_list_head(tail_page->next);
1932 to_remove = tail_page;
1934 /* start of pages to remove */
1935 first_page = list_entry(rb_list_head(to_remove->next),
1936 struct buffer_page, list);
1938 for (nr_removed = 0; nr_removed < nr_pages; nr_removed++) {
1939 to_remove = rb_list_head(to_remove)->next;
1940 head_bit |= (unsigned long)to_remove & RB_PAGE_HEAD;
1942 /* Read iterators need to reset themselves when some pages removed */
1943 cpu_buffer->pages_removed += nr_removed;
1945 next_page = rb_list_head(to_remove)->next;
1948 * Now we remove all pages between tail_page and next_page.
1949 * Make sure that we have head_bit value preserved for the
1952 tail_page->next = (struct list_head *)((unsigned long)next_page |
1954 next_page = rb_list_head(next_page);
1955 next_page->prev = tail_page;
1957 /* make sure pages points to a valid page in the ring buffer */
1958 cpu_buffer->pages = next_page;
1960 /* update head page */
1962 cpu_buffer->head_page = list_entry(next_page,
1963 struct buffer_page, list);
1965 /* pages are removed, resume tracing and then free the pages */
1966 atomic_dec(&cpu_buffer->record_disabled);
1967 raw_spin_unlock_irq(&cpu_buffer->reader_lock);
1969 RB_WARN_ON(cpu_buffer, list_empty(cpu_buffer->pages));
1971 /* last buffer page to remove */
1972 last_page = list_entry(rb_list_head(to_remove), struct buffer_page,
1974 tmp_iter_page = first_page;
1979 to_remove_page = tmp_iter_page;
1980 rb_inc_page(&tmp_iter_page);
1982 /* update the counters */
1983 page_entries = rb_page_entries(to_remove_page);
1986 * If something was added to this page, it was full
1987 * since it is not the tail page. So we deduct the
1988 * bytes consumed in ring buffer from here.
1989 * Increment overrun to account for the lost events.
1991 local_add(page_entries, &cpu_buffer->overrun);
1992 local_sub(rb_page_commit(to_remove_page), &cpu_buffer->entries_bytes);
1993 local_inc(&cpu_buffer->pages_lost);
1997 * We have already removed references to this list item, just
1998 * free up the buffer_page and its page
2000 free_buffer_page(to_remove_page);
2003 } while (to_remove_page != last_page);
2005 RB_WARN_ON(cpu_buffer, nr_removed);
2007 return nr_removed == 0;
2011 rb_insert_pages(struct ring_buffer_per_cpu *cpu_buffer)
2013 struct list_head *pages = &cpu_buffer->new_pages;
2014 int retries, success;
2016 raw_spin_lock_irq(&cpu_buffer->reader_lock);
2018 * We are holding the reader lock, so the reader page won't be swapped
2019 * in the ring buffer. Now we are racing with the writer trying to
2020 * move head page and the tail page.
2021 * We are going to adapt the reader page update process where:
2022 * 1. We first splice the start and end of list of new pages between
2023 * the head page and its previous page.
2024 * 2. We cmpxchg the prev_page->next to point from head page to the
2025 * start of new pages list.
2026 * 3. Finally, we update the head->prev to the end of new list.
2028 * We will try this process 10 times, to make sure that we don't keep
2034 struct list_head *head_page, *prev_page, *r;
2035 struct list_head *last_page, *first_page;
2036 struct list_head *head_page_with_bit;
2038 head_page = &rb_set_head_page(cpu_buffer)->list;
2041 prev_page = head_page->prev;
2043 first_page = pages->next;
2044 last_page = pages->prev;
2046 head_page_with_bit = (struct list_head *)
2047 ((unsigned long)head_page | RB_PAGE_HEAD);
2049 last_page->next = head_page_with_bit;
2050 first_page->prev = prev_page;
2052 r = cmpxchg(&prev_page->next, head_page_with_bit, first_page);
2054 if (r == head_page_with_bit) {
2056 * yay, we replaced the page pointer to our new list,
2057 * now, we just have to update to head page's prev
2058 * pointer to point to end of list
2060 head_page->prev = last_page;
2067 INIT_LIST_HEAD(pages);
2069 * If we weren't successful in adding in new pages, warn and stop
2072 RB_WARN_ON(cpu_buffer, !success);
2073 raw_spin_unlock_irq(&cpu_buffer->reader_lock);
2075 /* free pages if they weren't inserted */
2077 struct buffer_page *bpage, *tmp;
2078 list_for_each_entry_safe(bpage, tmp, &cpu_buffer->new_pages,
2080 list_del_init(&bpage->list);
2081 free_buffer_page(bpage);
2087 static void rb_update_pages(struct ring_buffer_per_cpu *cpu_buffer)
2091 if (cpu_buffer->nr_pages_to_update > 0)
2092 success = rb_insert_pages(cpu_buffer);
2094 success = rb_remove_pages(cpu_buffer,
2095 -cpu_buffer->nr_pages_to_update);
2098 cpu_buffer->nr_pages += cpu_buffer->nr_pages_to_update;
2101 static void update_pages_handler(struct work_struct *work)
2103 struct ring_buffer_per_cpu *cpu_buffer = container_of(work,
2104 struct ring_buffer_per_cpu, update_pages_work);
2105 rb_update_pages(cpu_buffer);
2106 complete(&cpu_buffer->update_done);
2110 * ring_buffer_resize - resize the ring buffer
2111 * @buffer: the buffer to resize.
2112 * @size: the new size.
2113 * @cpu_id: the cpu buffer to resize
2115 * Minimum size is 2 * BUF_PAGE_SIZE.
2117 * Returns 0 on success and < 0 on failure.
2119 int ring_buffer_resize(struct trace_buffer *buffer, unsigned long size,
2122 struct ring_buffer_per_cpu *cpu_buffer;
2123 unsigned long nr_pages;
2127 * Always succeed at resizing a non-existent buffer:
2132 /* Make sure the requested buffer exists */
2133 if (cpu_id != RING_BUFFER_ALL_CPUS &&
2134 !cpumask_test_cpu(cpu_id, buffer->cpumask))
2137 nr_pages = DIV_ROUND_UP(size, BUF_PAGE_SIZE);
2139 /* we need a minimum of two pages */
2143 /* prevent another thread from changing buffer sizes */
2144 mutex_lock(&buffer->mutex);
2145 atomic_inc(&buffer->resizing);
2147 if (cpu_id == RING_BUFFER_ALL_CPUS) {
2149 * Don't succeed if resizing is disabled, as a reader might be
2150 * manipulating the ring buffer and is expecting a sane state while
2153 for_each_buffer_cpu(buffer, cpu) {
2154 cpu_buffer = buffer->buffers[cpu];
2155 if (atomic_read(&cpu_buffer->resize_disabled)) {
2157 goto out_err_unlock;
2161 /* calculate the pages to update */
2162 for_each_buffer_cpu(buffer, cpu) {
2163 cpu_buffer = buffer->buffers[cpu];
2165 cpu_buffer->nr_pages_to_update = nr_pages -
2166 cpu_buffer->nr_pages;
2168 * nothing more to do for removing pages or no update
2170 if (cpu_buffer->nr_pages_to_update <= 0)
2173 * to add pages, make sure all new pages can be
2174 * allocated without receiving ENOMEM
2176 INIT_LIST_HEAD(&cpu_buffer->new_pages);
2177 if (__rb_allocate_pages(cpu_buffer, cpu_buffer->nr_pages_to_update,
2178 &cpu_buffer->new_pages)) {
2179 /* not enough memory for new pages */
2189 * Fire off all the required work handlers
2190 * We can't schedule on offline CPUs, but it's not necessary
2191 * since we can change their buffer sizes without any race.
2193 for_each_buffer_cpu(buffer, cpu) {
2194 cpu_buffer = buffer->buffers[cpu];
2195 if (!cpu_buffer->nr_pages_to_update)
2198 /* Can't run something on an offline CPU. */
2199 if (!cpu_online(cpu)) {
2200 rb_update_pages(cpu_buffer);
2201 cpu_buffer->nr_pages_to_update = 0;
2203 schedule_work_on(cpu,
2204 &cpu_buffer->update_pages_work);
2208 /* wait for all the updates to complete */
2209 for_each_buffer_cpu(buffer, cpu) {
2210 cpu_buffer = buffer->buffers[cpu];
2211 if (!cpu_buffer->nr_pages_to_update)
2214 if (cpu_online(cpu))
2215 wait_for_completion(&cpu_buffer->update_done);
2216 cpu_buffer->nr_pages_to_update = 0;
2221 cpu_buffer = buffer->buffers[cpu_id];
2223 if (nr_pages == cpu_buffer->nr_pages)
2227 * Don't succeed if resizing is disabled, as a reader might be
2228 * manipulating the ring buffer and is expecting a sane state while
2231 if (atomic_read(&cpu_buffer->resize_disabled)) {
2233 goto out_err_unlock;
2236 cpu_buffer->nr_pages_to_update = nr_pages -
2237 cpu_buffer->nr_pages;
2239 INIT_LIST_HEAD(&cpu_buffer->new_pages);
2240 if (cpu_buffer->nr_pages_to_update > 0 &&
2241 __rb_allocate_pages(cpu_buffer, cpu_buffer->nr_pages_to_update,
2242 &cpu_buffer->new_pages)) {
2249 /* Can't run something on an offline CPU. */
2250 if (!cpu_online(cpu_id))
2251 rb_update_pages(cpu_buffer);
2253 schedule_work_on(cpu_id,
2254 &cpu_buffer->update_pages_work);
2255 wait_for_completion(&cpu_buffer->update_done);
2258 cpu_buffer->nr_pages_to_update = 0;
2264 * The ring buffer resize can happen with the ring buffer
2265 * enabled, so that the update disturbs the tracing as little
2266 * as possible. But if the buffer is disabled, we do not need
2267 * to worry about that, and we can take the time to verify
2268 * that the buffer is not corrupt.
2270 if (atomic_read(&buffer->record_disabled)) {
2271 atomic_inc(&buffer->record_disabled);
2273 * Even though the buffer was disabled, we must make sure
2274 * that it is truly disabled before calling rb_check_pages.
2275 * There could have been a race between checking
2276 * record_disable and incrementing it.
2279 for_each_buffer_cpu(buffer, cpu) {
2280 cpu_buffer = buffer->buffers[cpu];
2281 rb_check_pages(cpu_buffer);
2283 atomic_dec(&buffer->record_disabled);
2286 atomic_dec(&buffer->resizing);
2287 mutex_unlock(&buffer->mutex);
2291 for_each_buffer_cpu(buffer, cpu) {
2292 struct buffer_page *bpage, *tmp;
2294 cpu_buffer = buffer->buffers[cpu];
2295 cpu_buffer->nr_pages_to_update = 0;
2297 if (list_empty(&cpu_buffer->new_pages))
2300 list_for_each_entry_safe(bpage, tmp, &cpu_buffer->new_pages,
2302 list_del_init(&bpage->list);
2303 free_buffer_page(bpage);
2307 atomic_dec(&buffer->resizing);
2308 mutex_unlock(&buffer->mutex);
2311 EXPORT_SYMBOL_GPL(ring_buffer_resize);
2313 void ring_buffer_change_overwrite(struct trace_buffer *buffer, int val)
2315 mutex_lock(&buffer->mutex);
2317 buffer->flags |= RB_FL_OVERWRITE;
2319 buffer->flags &= ~RB_FL_OVERWRITE;
2320 mutex_unlock(&buffer->mutex);
2322 EXPORT_SYMBOL_GPL(ring_buffer_change_overwrite);
2324 static __always_inline void *__rb_page_index(struct buffer_page *bpage, unsigned index)
2326 return bpage->page->data + index;
2329 static __always_inline struct ring_buffer_event *
2330 rb_reader_event(struct ring_buffer_per_cpu *cpu_buffer)
2332 return __rb_page_index(cpu_buffer->reader_page,
2333 cpu_buffer->reader_page->read);
2336 static struct ring_buffer_event *
2337 rb_iter_head_event(struct ring_buffer_iter *iter)
2339 struct ring_buffer_event *event;
2340 struct buffer_page *iter_head_page = iter->head_page;
2341 unsigned long commit;
2344 if (iter->head != iter->next_event)
2348 * When the writer goes across pages, it issues a cmpxchg which
2349 * is a mb(), which will synchronize with the rmb here.
2350 * (see rb_tail_page_update() and __rb_reserve_next())
2352 commit = rb_page_commit(iter_head_page);
2355 /* An event needs to be at least 8 bytes in size */
2356 if (iter->head > commit - 8)
2359 event = __rb_page_index(iter_head_page, iter->head);
2360 length = rb_event_length(event);
2363 * READ_ONCE() doesn't work on functions and we don't want the
2364 * compiler doing any crazy optimizations with length.
2368 if ((iter->head + length) > commit || length > BUF_MAX_DATA_SIZE)
2369 /* Writer corrupted the read? */
2372 memcpy(iter->event, event, length);
2374 * If the page stamp is still the same after this rmb() then the
2375 * event was safely copied without the writer entering the page.
2379 /* Make sure the page didn't change since we read this */
2380 if (iter->page_stamp != iter_head_page->page->time_stamp ||
2381 commit > rb_page_commit(iter_head_page))
2384 iter->next_event = iter->head + length;
2387 /* Reset to the beginning */
2388 iter->page_stamp = iter->read_stamp = iter->head_page->page->time_stamp;
2390 iter->next_event = 0;
2391 iter->missed_events = 1;
2395 /* Size is determined by what has been committed */
2396 static __always_inline unsigned rb_page_size(struct buffer_page *bpage)
2398 return rb_page_commit(bpage);
2401 static __always_inline unsigned
2402 rb_commit_index(struct ring_buffer_per_cpu *cpu_buffer)
2404 return rb_page_commit(cpu_buffer->commit_page);
2407 static __always_inline unsigned
2408 rb_event_index(struct ring_buffer_event *event)
2410 unsigned long addr = (unsigned long)event;
2412 return (addr & ~PAGE_MASK) - BUF_PAGE_HDR_SIZE;
2415 static void rb_inc_iter(struct ring_buffer_iter *iter)
2417 struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
2420 * The iterator could be on the reader page (it starts there).
2421 * But the head could have moved, since the reader was
2422 * found. Check for this case and assign the iterator
2423 * to the head page instead of next.
2425 if (iter->head_page == cpu_buffer->reader_page)
2426 iter->head_page = rb_set_head_page(cpu_buffer);
2428 rb_inc_page(&iter->head_page);
2430 iter->page_stamp = iter->read_stamp = iter->head_page->page->time_stamp;
2432 iter->next_event = 0;
2436 * rb_handle_head_page - writer hit the head page
2438 * Returns: +1 to retry page
2443 rb_handle_head_page(struct ring_buffer_per_cpu *cpu_buffer,
2444 struct buffer_page *tail_page,
2445 struct buffer_page *next_page)
2447 struct buffer_page *new_head;
2452 entries = rb_page_entries(next_page);
2455 * The hard part is here. We need to move the head
2456 * forward, and protect against both readers on
2457 * other CPUs and writers coming in via interrupts.
2459 type = rb_head_page_set_update(cpu_buffer, next_page, tail_page,
2463 * type can be one of four:
2464 * NORMAL - an interrupt already moved it for us
2465 * HEAD - we are the first to get here.
2466 * UPDATE - we are the interrupt interrupting
2468 * MOVED - a reader on another CPU moved the next
2469 * pointer to its reader page. Give up
2476 * We changed the head to UPDATE, thus
2477 * it is our responsibility to update
2480 local_add(entries, &cpu_buffer->overrun);
2481 local_sub(rb_page_commit(next_page), &cpu_buffer->entries_bytes);
2482 local_inc(&cpu_buffer->pages_lost);
2485 * The entries will be zeroed out when we move the
2489 /* still more to do */
2492 case RB_PAGE_UPDATE:
2494 * This is an interrupt that interrupt the
2495 * previous update. Still more to do.
2498 case RB_PAGE_NORMAL:
2500 * An interrupt came in before the update
2501 * and processed this for us.
2502 * Nothing left to do.
2507 * The reader is on another CPU and just did
2508 * a swap with our next_page.
2513 RB_WARN_ON(cpu_buffer, 1); /* WTF??? */
2518 * Now that we are here, the old head pointer is
2519 * set to UPDATE. This will keep the reader from
2520 * swapping the head page with the reader page.
2521 * The reader (on another CPU) will spin till
2524 * We just need to protect against interrupts
2525 * doing the job. We will set the next pointer
2526 * to HEAD. After that, we set the old pointer
2527 * to NORMAL, but only if it was HEAD before.
2528 * otherwise we are an interrupt, and only
2529 * want the outer most commit to reset it.
2531 new_head = next_page;
2532 rb_inc_page(&new_head);
2534 ret = rb_head_page_set_head(cpu_buffer, new_head, next_page,
2538 * Valid returns are:
2539 * HEAD - an interrupt came in and already set it.
2540 * NORMAL - One of two things:
2541 * 1) We really set it.
2542 * 2) A bunch of interrupts came in and moved
2543 * the page forward again.
2547 case RB_PAGE_NORMAL:
2551 RB_WARN_ON(cpu_buffer, 1);
2556 * It is possible that an interrupt came in,
2557 * set the head up, then more interrupts came in
2558 * and moved it again. When we get back here,
2559 * the page would have been set to NORMAL but we
2560 * just set it back to HEAD.
2562 * How do you detect this? Well, if that happened
2563 * the tail page would have moved.
2565 if (ret == RB_PAGE_NORMAL) {
2566 struct buffer_page *buffer_tail_page;
2568 buffer_tail_page = READ_ONCE(cpu_buffer->tail_page);
2570 * If the tail had moved passed next, then we need
2571 * to reset the pointer.
2573 if (buffer_tail_page != tail_page &&
2574 buffer_tail_page != next_page)
2575 rb_head_page_set_normal(cpu_buffer, new_head,
2581 * If this was the outer most commit (the one that
2582 * changed the original pointer from HEAD to UPDATE),
2583 * then it is up to us to reset it to NORMAL.
2585 if (type == RB_PAGE_HEAD) {
2586 ret = rb_head_page_set_normal(cpu_buffer, next_page,
2589 if (RB_WARN_ON(cpu_buffer,
2590 ret != RB_PAGE_UPDATE))
2598 rb_reset_tail(struct ring_buffer_per_cpu *cpu_buffer,
2599 unsigned long tail, struct rb_event_info *info)
2601 struct buffer_page *tail_page = info->tail_page;
2602 struct ring_buffer_event *event;
2603 unsigned long length = info->length;
2606 * Only the event that crossed the page boundary
2607 * must fill the old tail_page with padding.
2609 if (tail >= BUF_PAGE_SIZE) {
2611 * If the page was filled, then we still need
2612 * to update the real_end. Reset it to zero
2613 * and the reader will ignore it.
2615 if (tail == BUF_PAGE_SIZE)
2616 tail_page->real_end = 0;
2618 local_sub(length, &tail_page->write);
2622 event = __rb_page_index(tail_page, tail);
2625 * Save the original length to the meta data.
2626 * This will be used by the reader to add lost event
2629 tail_page->real_end = tail;
2632 * If this event is bigger than the minimum size, then
2633 * we need to be careful that we don't subtract the
2634 * write counter enough to allow another writer to slip
2636 * We put in a discarded commit instead, to make sure
2637 * that this space is not used again, and this space will
2638 * not be accounted into 'entries_bytes'.
2640 * If we are less than the minimum size, we don't need to
2643 if (tail > (BUF_PAGE_SIZE - RB_EVNT_MIN_SIZE)) {
2644 /* No room for any events */
2646 /* Mark the rest of the page with padding */
2647 rb_event_set_padding(event);
2649 /* Make sure the padding is visible before the write update */
2652 /* Set the write back to the previous setting */
2653 local_sub(length, &tail_page->write);
2657 /* Put in a discarded event */
2658 event->array[0] = (BUF_PAGE_SIZE - tail) - RB_EVNT_HDR_SIZE;
2659 event->type_len = RINGBUF_TYPE_PADDING;
2660 /* time delta must be non zero */
2661 event->time_delta = 1;
2663 /* account for padding bytes */
2664 local_add(BUF_PAGE_SIZE - tail, &cpu_buffer->entries_bytes);
2666 /* Make sure the padding is visible before the tail_page->write update */
2669 /* Set write to end of buffer */
2670 length = (tail + length) - BUF_PAGE_SIZE;
2671 local_sub(length, &tail_page->write);
2674 static inline void rb_end_commit(struct ring_buffer_per_cpu *cpu_buffer);
2677 * This is the slow path, force gcc not to inline it.
2679 static noinline struct ring_buffer_event *
2680 rb_move_tail(struct ring_buffer_per_cpu *cpu_buffer,
2681 unsigned long tail, struct rb_event_info *info)
2683 struct buffer_page *tail_page = info->tail_page;
2684 struct buffer_page *commit_page = cpu_buffer->commit_page;
2685 struct trace_buffer *buffer = cpu_buffer->buffer;
2686 struct buffer_page *next_page;
2689 next_page = tail_page;
2691 rb_inc_page(&next_page);
2694 * If for some reason, we had an interrupt storm that made
2695 * it all the way around the buffer, bail, and warn
2698 if (unlikely(next_page == commit_page)) {
2699 local_inc(&cpu_buffer->commit_overrun);
2704 * This is where the fun begins!
2706 * We are fighting against races between a reader that
2707 * could be on another CPU trying to swap its reader
2708 * page with the buffer head.
2710 * We are also fighting against interrupts coming in and
2711 * moving the head or tail on us as well.
2713 * If the next page is the head page then we have filled
2714 * the buffer, unless the commit page is still on the
2717 if (rb_is_head_page(next_page, &tail_page->list)) {
2720 * If the commit is not on the reader page, then
2721 * move the header page.
2723 if (!rb_is_reader_page(cpu_buffer->commit_page)) {
2725 * If we are not in overwrite mode,
2726 * this is easy, just stop here.
2728 if (!(buffer->flags & RB_FL_OVERWRITE)) {
2729 local_inc(&cpu_buffer->dropped_events);
2733 ret = rb_handle_head_page(cpu_buffer,
2742 * We need to be careful here too. The
2743 * commit page could still be on the reader
2744 * page. We could have a small buffer, and
2745 * have filled up the buffer with events
2746 * from interrupts and such, and wrapped.
2748 * Note, if the tail page is also on the
2749 * reader_page, we let it move out.
2751 if (unlikely((cpu_buffer->commit_page !=
2752 cpu_buffer->tail_page) &&
2753 (cpu_buffer->commit_page ==
2754 cpu_buffer->reader_page))) {
2755 local_inc(&cpu_buffer->commit_overrun);
2761 rb_tail_page_update(cpu_buffer, tail_page, next_page);
2765 rb_reset_tail(cpu_buffer, tail, info);
2767 /* Commit what we have for now. */
2768 rb_end_commit(cpu_buffer);
2769 /* rb_end_commit() decs committing */
2770 local_inc(&cpu_buffer->committing);
2772 /* fail and let the caller try again */
2773 return ERR_PTR(-EAGAIN);
2777 rb_reset_tail(cpu_buffer, tail, info);
2783 static struct ring_buffer_event *
2784 rb_add_time_stamp(struct ring_buffer_event *event, u64 delta, bool abs)
2787 event->type_len = RINGBUF_TYPE_TIME_STAMP;
2789 event->type_len = RINGBUF_TYPE_TIME_EXTEND;
2791 /* Not the first event on the page, or not delta? */
2792 if (abs || rb_event_index(event)) {
2793 event->time_delta = delta & TS_MASK;
2794 event->array[0] = delta >> TS_SHIFT;
2796 /* nope, just zero it */
2797 event->time_delta = 0;
2798 event->array[0] = 0;
2801 return skip_time_extend(event);
2804 #ifndef CONFIG_HAVE_UNSTABLE_SCHED_CLOCK
2805 static inline bool sched_clock_stable(void)
2812 rb_check_timestamp(struct ring_buffer_per_cpu *cpu_buffer,
2813 struct rb_event_info *info)
2817 WARN_ONCE(1, "Delta way too big! %llu ts=%llu before=%llu after=%llu write stamp=%llu\n%s",
2818 (unsigned long long)info->delta,
2819 (unsigned long long)info->ts,
2820 (unsigned long long)info->before,
2821 (unsigned long long)info->after,
2822 (unsigned long long)(rb_time_read(&cpu_buffer->write_stamp, &write_stamp) ? write_stamp : 0),
2823 sched_clock_stable() ? "" :
2824 "If you just came from a suspend/resume,\n"
2825 "please switch to the trace global clock:\n"
2826 " echo global > /sys/kernel/debug/tracing/trace_clock\n"
2827 "or add trace_clock=global to the kernel command line\n");
2830 static void rb_add_timestamp(struct ring_buffer_per_cpu *cpu_buffer,
2831 struct ring_buffer_event **event,
2832 struct rb_event_info *info,
2834 unsigned int *length)
2836 bool abs = info->add_timestamp &
2837 (RB_ADD_STAMP_FORCE | RB_ADD_STAMP_ABSOLUTE);
2839 if (unlikely(info->delta > (1ULL << 59))) {
2840 /* did the clock go backwards */
2841 if (info->before == info->after && info->before > info->ts) {
2842 /* not interrupted */
2846 * This is possible with a recalibrating of the TSC.
2847 * Do not produce a call stack, but just report it.
2851 pr_warn("Ring buffer clock went backwards: %llu -> %llu\n",
2852 info->before, info->ts);
2855 rb_check_timestamp(cpu_buffer, info);
2859 *event = rb_add_time_stamp(*event, info->delta, abs);
2860 *length -= RB_LEN_TIME_EXTEND;
2865 * rb_update_event - update event type and data
2866 * @cpu_buffer: The per cpu buffer of the @event
2867 * @event: the event to update
2868 * @info: The info to update the @event with (contains length and delta)
2870 * Update the type and data fields of the @event. The length
2871 * is the actual size that is written to the ring buffer,
2872 * and with this, we can determine what to place into the
2876 rb_update_event(struct ring_buffer_per_cpu *cpu_buffer,
2877 struct ring_buffer_event *event,
2878 struct rb_event_info *info)
2880 unsigned length = info->length;
2881 u64 delta = info->delta;
2882 unsigned int nest = local_read(&cpu_buffer->committing) - 1;
2884 if (!WARN_ON_ONCE(nest >= MAX_NEST))
2885 cpu_buffer->event_stamp[nest] = info->ts;
2888 * If we need to add a timestamp, then we
2889 * add it to the start of the reserved space.
2891 if (unlikely(info->add_timestamp))
2892 rb_add_timestamp(cpu_buffer, &event, info, &delta, &length);
2894 event->time_delta = delta;
2895 length -= RB_EVNT_HDR_SIZE;
2896 if (length > RB_MAX_SMALL_DATA || RB_FORCE_8BYTE_ALIGNMENT) {
2897 event->type_len = 0;
2898 event->array[0] = length;
2900 event->type_len = DIV_ROUND_UP(length, RB_ALIGNMENT);
2903 static unsigned rb_calculate_event_length(unsigned length)
2905 struct ring_buffer_event event; /* Used only for sizeof array */
2907 /* zero length can cause confusions */
2911 if (length > RB_MAX_SMALL_DATA || RB_FORCE_8BYTE_ALIGNMENT)
2912 length += sizeof(event.array[0]);
2914 length += RB_EVNT_HDR_SIZE;
2915 length = ALIGN(length, RB_ARCH_ALIGNMENT);
2918 * In case the time delta is larger than the 27 bits for it
2919 * in the header, we need to add a timestamp. If another
2920 * event comes in when trying to discard this one to increase
2921 * the length, then the timestamp will be added in the allocated
2922 * space of this event. If length is bigger than the size needed
2923 * for the TIME_EXTEND, then padding has to be used. The events
2924 * length must be either RB_LEN_TIME_EXTEND, or greater than or equal
2925 * to RB_LEN_TIME_EXTEND + 8, as 8 is the minimum size for padding.
2926 * As length is a multiple of 4, we only need to worry if it
2927 * is 12 (RB_LEN_TIME_EXTEND + 4).
2929 if (length == RB_LEN_TIME_EXTEND + RB_ALIGNMENT)
2930 length += RB_ALIGNMENT;
2935 static u64 rb_time_delta(struct ring_buffer_event *event)
2937 switch (event->type_len) {
2938 case RINGBUF_TYPE_PADDING:
2941 case RINGBUF_TYPE_TIME_EXTEND:
2942 return rb_event_time_stamp(event);
2944 case RINGBUF_TYPE_TIME_STAMP:
2947 case RINGBUF_TYPE_DATA:
2948 return event->time_delta;
2955 rb_try_to_discard(struct ring_buffer_per_cpu *cpu_buffer,
2956 struct ring_buffer_event *event)
2958 unsigned long new_index, old_index;
2959 struct buffer_page *bpage;
2960 unsigned long index;
2965 new_index = rb_event_index(event);
2966 old_index = new_index + rb_event_ts_length(event);
2967 addr = (unsigned long)event;
2970 bpage = READ_ONCE(cpu_buffer->tail_page);
2972 delta = rb_time_delta(event);
2974 if (!rb_time_read(&cpu_buffer->write_stamp, &write_stamp))
2977 /* Make sure the write stamp is read before testing the location */
2980 if (bpage->page == (void *)addr && rb_page_write(bpage) == old_index) {
2981 unsigned long write_mask =
2982 local_read(&bpage->write) & ~RB_WRITE_MASK;
2983 unsigned long event_length = rb_event_length(event);
2985 /* Something came in, can't discard */
2986 if (!rb_time_cmpxchg(&cpu_buffer->write_stamp,
2987 write_stamp, write_stamp - delta))
2991 * It's possible that the event time delta is zero
2992 * (has the same time stamp as the previous event)
2993 * in which case write_stamp and before_stamp could
2994 * be the same. In such a case, force before_stamp
2995 * to be different than write_stamp. It doesn't
2996 * matter what it is, as long as its different.
2999 rb_time_set(&cpu_buffer->before_stamp, 0);
3002 * If an event were to come in now, it would see that the
3003 * write_stamp and the before_stamp are different, and assume
3004 * that this event just added itself before updating
3005 * the write stamp. The interrupting event will fix the
3006 * write stamp for us, and use the before stamp as its delta.
3010 * This is on the tail page. It is possible that
3011 * a write could come in and move the tail page
3012 * and write to the next page. That is fine
3013 * because we just shorten what is on this page.
3015 old_index += write_mask;
3016 new_index += write_mask;
3017 index = local_cmpxchg(&bpage->write, old_index, new_index);
3018 if (index == old_index) {
3019 /* update counters */
3020 local_sub(event_length, &cpu_buffer->entries_bytes);
3025 /* could not discard */
3029 static void rb_start_commit(struct ring_buffer_per_cpu *cpu_buffer)
3031 local_inc(&cpu_buffer->committing);
3032 local_inc(&cpu_buffer->commits);
3035 static __always_inline void
3036 rb_set_commit_to_write(struct ring_buffer_per_cpu *cpu_buffer)
3038 unsigned long max_count;
3041 * We only race with interrupts and NMIs on this CPU.
3042 * If we own the commit event, then we can commit
3043 * all others that interrupted us, since the interruptions
3044 * are in stack format (they finish before they come
3045 * back to us). This allows us to do a simple loop to
3046 * assign the commit to the tail.
3049 max_count = cpu_buffer->nr_pages * 100;
3051 while (cpu_buffer->commit_page != READ_ONCE(cpu_buffer->tail_page)) {
3052 if (RB_WARN_ON(cpu_buffer, !(--max_count)))
3054 if (RB_WARN_ON(cpu_buffer,
3055 rb_is_reader_page(cpu_buffer->tail_page)))
3058 * No need for a memory barrier here, as the update
3059 * of the tail_page did it for this page.
3061 local_set(&cpu_buffer->commit_page->page->commit,
3062 rb_page_write(cpu_buffer->commit_page));
3063 rb_inc_page(&cpu_buffer->commit_page);
3064 /* add barrier to keep gcc from optimizing too much */
3067 while (rb_commit_index(cpu_buffer) !=
3068 rb_page_write(cpu_buffer->commit_page)) {
3070 /* Make sure the readers see the content of what is committed. */
3072 local_set(&cpu_buffer->commit_page->page->commit,
3073 rb_page_write(cpu_buffer->commit_page));
3074 RB_WARN_ON(cpu_buffer,
3075 local_read(&cpu_buffer->commit_page->page->commit) &
3080 /* again, keep gcc from optimizing */
3084 * If an interrupt came in just after the first while loop
3085 * and pushed the tail page forward, we will be left with
3086 * a dangling commit that will never go forward.
3088 if (unlikely(cpu_buffer->commit_page != READ_ONCE(cpu_buffer->tail_page)))
3092 static __always_inline void rb_end_commit(struct ring_buffer_per_cpu *cpu_buffer)
3094 unsigned long commits;
3096 if (RB_WARN_ON(cpu_buffer,
3097 !local_read(&cpu_buffer->committing)))
3101 commits = local_read(&cpu_buffer->commits);
3102 /* synchronize with interrupts */
3104 if (local_read(&cpu_buffer->committing) == 1)
3105 rb_set_commit_to_write(cpu_buffer);
3107 local_dec(&cpu_buffer->committing);
3109 /* synchronize with interrupts */
3113 * Need to account for interrupts coming in between the
3114 * updating of the commit page and the clearing of the
3115 * committing counter.
3117 if (unlikely(local_read(&cpu_buffer->commits) != commits) &&
3118 !local_read(&cpu_buffer->committing)) {
3119 local_inc(&cpu_buffer->committing);
3124 static inline void rb_event_discard(struct ring_buffer_event *event)
3126 if (extended_time(event))
3127 event = skip_time_extend(event);
3129 /* array[0] holds the actual length for the discarded event */
3130 event->array[0] = rb_event_data_length(event) - RB_EVNT_HDR_SIZE;
3131 event->type_len = RINGBUF_TYPE_PADDING;
3132 /* time delta must be non zero */
3133 if (!event->time_delta)
3134 event->time_delta = 1;
3137 static void rb_commit(struct ring_buffer_per_cpu *cpu_buffer,
3138 struct ring_buffer_event *event)
3140 local_inc(&cpu_buffer->entries);
3141 rb_end_commit(cpu_buffer);
3144 static __always_inline void
3145 rb_wakeups(struct trace_buffer *buffer, struct ring_buffer_per_cpu *cpu_buffer)
3147 if (buffer->irq_work.waiters_pending) {
3148 buffer->irq_work.waiters_pending = false;
3149 /* irq_work_queue() supplies it's own memory barriers */
3150 irq_work_queue(&buffer->irq_work.work);
3153 if (cpu_buffer->irq_work.waiters_pending) {
3154 cpu_buffer->irq_work.waiters_pending = false;
3155 /* irq_work_queue() supplies it's own memory barriers */
3156 irq_work_queue(&cpu_buffer->irq_work.work);
3159 if (cpu_buffer->last_pages_touch == local_read(&cpu_buffer->pages_touched))
3162 if (cpu_buffer->reader_page == cpu_buffer->commit_page)
3165 if (!cpu_buffer->irq_work.full_waiters_pending)
3168 cpu_buffer->last_pages_touch = local_read(&cpu_buffer->pages_touched);
3170 if (!full_hit(buffer, cpu_buffer->cpu, cpu_buffer->shortest_full))
3173 cpu_buffer->irq_work.wakeup_full = true;
3174 cpu_buffer->irq_work.full_waiters_pending = false;
3175 /* irq_work_queue() supplies it's own memory barriers */
3176 irq_work_queue(&cpu_buffer->irq_work.work);
3179 #ifdef CONFIG_RING_BUFFER_RECORD_RECURSION
3180 # define do_ring_buffer_record_recursion() \
3181 do_ftrace_record_recursion(_THIS_IP_, _RET_IP_)
3183 # define do_ring_buffer_record_recursion() do { } while (0)
3187 * The lock and unlock are done within a preempt disable section.
3188 * The current_context per_cpu variable can only be modified
3189 * by the current task between lock and unlock. But it can
3190 * be modified more than once via an interrupt. To pass this
3191 * information from the lock to the unlock without having to
3192 * access the 'in_interrupt()' functions again (which do show
3193 * a bit of overhead in something as critical as function tracing,
3194 * we use a bitmask trick.
3196 * bit 1 = NMI context
3197 * bit 2 = IRQ context
3198 * bit 3 = SoftIRQ context
3199 * bit 4 = normal context.
3201 * This works because this is the order of contexts that can
3202 * preempt other contexts. A SoftIRQ never preempts an IRQ
3205 * When the context is determined, the corresponding bit is
3206 * checked and set (if it was set, then a recursion of that context
3209 * On unlock, we need to clear this bit. To do so, just subtract
3210 * 1 from the current_context and AND it to itself.
3214 * 101 & 100 = 100 (clearing bit zero)
3217 * 1010 & 1001 = 1000 (clearing bit 1)
3219 * The least significant bit can be cleared this way, and it
3220 * just so happens that it is the same bit corresponding to
3221 * the current context.
3223 * Now the TRANSITION bit breaks the above slightly. The TRANSITION bit
3224 * is set when a recursion is detected at the current context, and if
3225 * the TRANSITION bit is already set, it will fail the recursion.
3226 * This is needed because there's a lag between the changing of
3227 * interrupt context and updating the preempt count. In this case,
3228 * a false positive will be found. To handle this, one extra recursion
3229 * is allowed, and this is done by the TRANSITION bit. If the TRANSITION
3230 * bit is already set, then it is considered a recursion and the function
3231 * ends. Otherwise, the TRANSITION bit is set, and that bit is returned.
3233 * On the trace_recursive_unlock(), the TRANSITION bit will be the first
3234 * to be cleared. Even if it wasn't the context that set it. That is,
3235 * if an interrupt comes in while NORMAL bit is set and the ring buffer
3236 * is called before preempt_count() is updated, since the check will
3237 * be on the NORMAL bit, the TRANSITION bit will then be set. If an
3238 * NMI then comes in, it will set the NMI bit, but when the NMI code
3239 * does the trace_recursive_unlock() it will clear the TRANSITION bit
3240 * and leave the NMI bit set. But this is fine, because the interrupt
3241 * code that set the TRANSITION bit will then clear the NMI bit when it
3242 * calls trace_recursive_unlock(). If another NMI comes in, it will
3243 * set the TRANSITION bit and continue.
3245 * Note: The TRANSITION bit only handles a single transition between context.
3248 static __always_inline int
3249 trace_recursive_lock(struct ring_buffer_per_cpu *cpu_buffer)
3251 unsigned int val = cpu_buffer->current_context;
3252 unsigned long pc = preempt_count();
3255 if (!(pc & (NMI_MASK | HARDIRQ_MASK | SOFTIRQ_OFFSET)))
3256 bit = RB_CTX_NORMAL;
3258 bit = pc & NMI_MASK ? RB_CTX_NMI :
3259 pc & HARDIRQ_MASK ? RB_CTX_IRQ : RB_CTX_SOFTIRQ;
3261 if (unlikely(val & (1 << (bit + cpu_buffer->nest)))) {
3263 * It is possible that this was called by transitioning
3264 * between interrupt context, and preempt_count() has not
3265 * been updated yet. In this case, use the TRANSITION bit.
3267 bit = RB_CTX_TRANSITION;
3268 if (val & (1 << (bit + cpu_buffer->nest))) {
3269 do_ring_buffer_record_recursion();
3274 val |= (1 << (bit + cpu_buffer->nest));
3275 cpu_buffer->current_context = val;
3280 static __always_inline void
3281 trace_recursive_unlock(struct ring_buffer_per_cpu *cpu_buffer)
3283 cpu_buffer->current_context &=
3284 cpu_buffer->current_context - (1 << cpu_buffer->nest);
3287 /* The recursive locking above uses 5 bits */
3288 #define NESTED_BITS 5
3291 * ring_buffer_nest_start - Allow to trace while nested
3292 * @buffer: The ring buffer to modify
3294 * The ring buffer has a safety mechanism to prevent recursion.
3295 * But there may be a case where a trace needs to be done while
3296 * tracing something else. In this case, calling this function
3297 * will allow this function to nest within a currently active
3298 * ring_buffer_lock_reserve().
3300 * Call this function before calling another ring_buffer_lock_reserve() and
3301 * call ring_buffer_nest_end() after the nested ring_buffer_unlock_commit().
3303 void ring_buffer_nest_start(struct trace_buffer *buffer)
3305 struct ring_buffer_per_cpu *cpu_buffer;
3308 /* Enabled by ring_buffer_nest_end() */
3309 preempt_disable_notrace();
3310 cpu = raw_smp_processor_id();
3311 cpu_buffer = buffer->buffers[cpu];
3312 /* This is the shift value for the above recursive locking */
3313 cpu_buffer->nest += NESTED_BITS;
3317 * ring_buffer_nest_end - Allow to trace while nested
3318 * @buffer: The ring buffer to modify
3320 * Must be called after ring_buffer_nest_start() and after the
3321 * ring_buffer_unlock_commit().
3323 void ring_buffer_nest_end(struct trace_buffer *buffer)
3325 struct ring_buffer_per_cpu *cpu_buffer;
3328 /* disabled by ring_buffer_nest_start() */
3329 cpu = raw_smp_processor_id();
3330 cpu_buffer = buffer->buffers[cpu];
3331 /* This is the shift value for the above recursive locking */
3332 cpu_buffer->nest -= NESTED_BITS;
3333 preempt_enable_notrace();
3337 * ring_buffer_unlock_commit - commit a reserved
3338 * @buffer: The buffer to commit to
3339 * @event: The event pointer to commit.
3341 * This commits the data to the ring buffer, and releases any locks held.
3343 * Must be paired with ring_buffer_lock_reserve.
3345 int ring_buffer_unlock_commit(struct trace_buffer *buffer,
3346 struct ring_buffer_event *event)
3348 struct ring_buffer_per_cpu *cpu_buffer;
3349 int cpu = raw_smp_processor_id();
3351 cpu_buffer = buffer->buffers[cpu];
3353 rb_commit(cpu_buffer, event);
3355 rb_wakeups(buffer, cpu_buffer);
3357 trace_recursive_unlock(cpu_buffer);
3359 preempt_enable_notrace();
3363 EXPORT_SYMBOL_GPL(ring_buffer_unlock_commit);
3365 /* Special value to validate all deltas on a page. */
3366 #define CHECK_FULL_PAGE 1L
3368 #ifdef CONFIG_RING_BUFFER_VALIDATE_TIME_DELTAS
3369 static void dump_buffer_page(struct buffer_data_page *bpage,
3370 struct rb_event_info *info,
3373 struct ring_buffer_event *event;
3377 ts = bpage->time_stamp;
3378 pr_warn(" [%lld] PAGE TIME STAMP\n", ts);
3380 for (e = 0; e < tail; e += rb_event_length(event)) {
3382 event = (struct ring_buffer_event *)(bpage->data + e);
3384 switch (event->type_len) {
3386 case RINGBUF_TYPE_TIME_EXTEND:
3387 delta = rb_event_time_stamp(event);
3389 pr_warn(" [%lld] delta:%lld TIME EXTEND\n", ts, delta);
3392 case RINGBUF_TYPE_TIME_STAMP:
3393 delta = rb_event_time_stamp(event);
3395 pr_warn(" [%lld] absolute:%lld TIME STAMP\n", ts, delta);
3398 case RINGBUF_TYPE_PADDING:
3399 ts += event->time_delta;
3400 pr_warn(" [%lld] delta:%d PADDING\n", ts, event->time_delta);
3403 case RINGBUF_TYPE_DATA:
3404 ts += event->time_delta;
3405 pr_warn(" [%lld] delta:%d\n", ts, event->time_delta);
3414 static DEFINE_PER_CPU(atomic_t, checking);
3415 static atomic_t ts_dump;
3418 * Check if the current event time stamp matches the deltas on
3421 static void check_buffer(struct ring_buffer_per_cpu *cpu_buffer,
3422 struct rb_event_info *info,
3425 struct ring_buffer_event *event;
3426 struct buffer_data_page *bpage;
3431 bpage = info->tail_page->page;
3433 if (tail == CHECK_FULL_PAGE) {
3435 tail = local_read(&bpage->commit);
3436 } else if (info->add_timestamp &
3437 (RB_ADD_STAMP_FORCE | RB_ADD_STAMP_ABSOLUTE)) {
3438 /* Ignore events with absolute time stamps */
3443 * Do not check the first event (skip possible extends too).
3444 * Also do not check if previous events have not been committed.
3446 if (tail <= 8 || tail > local_read(&bpage->commit))
3450 * If this interrupted another event,
3452 if (atomic_inc_return(this_cpu_ptr(&checking)) != 1)
3455 ts = bpage->time_stamp;
3457 for (e = 0; e < tail; e += rb_event_length(event)) {
3459 event = (struct ring_buffer_event *)(bpage->data + e);
3461 switch (event->type_len) {
3463 case RINGBUF_TYPE_TIME_EXTEND:
3464 delta = rb_event_time_stamp(event);
3468 case RINGBUF_TYPE_TIME_STAMP:
3469 delta = rb_event_time_stamp(event);
3473 case RINGBUF_TYPE_PADDING:
3474 if (event->time_delta == 1)
3477 case RINGBUF_TYPE_DATA:
3478 ts += event->time_delta;
3482 RB_WARN_ON(cpu_buffer, 1);
3485 if ((full && ts > info->ts) ||
3486 (!full && ts + info->delta != info->ts)) {
3487 /* If another report is happening, ignore this one */
3488 if (atomic_inc_return(&ts_dump) != 1) {
3489 atomic_dec(&ts_dump);
3492 atomic_inc(&cpu_buffer->record_disabled);
3493 /* There's some cases in boot up that this can happen */
3494 WARN_ON_ONCE(system_state != SYSTEM_BOOTING);
3495 pr_warn("[CPU: %d]TIME DOES NOT MATCH expected:%lld actual:%lld delta:%lld before:%lld after:%lld%s\n",
3497 ts + info->delta, info->ts, info->delta,
3498 info->before, info->after,
3499 full ? " (full)" : "");
3500 dump_buffer_page(bpage, info, tail);
3501 atomic_dec(&ts_dump);
3502 /* Do not re-enable checking */
3506 atomic_dec(this_cpu_ptr(&checking));
3509 static inline void check_buffer(struct ring_buffer_per_cpu *cpu_buffer,
3510 struct rb_event_info *info,
3514 #endif /* CONFIG_RING_BUFFER_VALIDATE_TIME_DELTAS */
3516 static struct ring_buffer_event *
3517 __rb_reserve_next(struct ring_buffer_per_cpu *cpu_buffer,
3518 struct rb_event_info *info)
3520 struct ring_buffer_event *event;
3521 struct buffer_page *tail_page;
3522 unsigned long tail, write, w;
3526 /* Don't let the compiler play games with cpu_buffer->tail_page */
3527 tail_page = info->tail_page = READ_ONCE(cpu_buffer->tail_page);
3529 /*A*/ w = local_read(&tail_page->write) & RB_WRITE_MASK;
3531 b_ok = rb_time_read(&cpu_buffer->before_stamp, &info->before);
3532 a_ok = rb_time_read(&cpu_buffer->write_stamp, &info->after);
3534 info->ts = rb_time_stamp(cpu_buffer->buffer);
3536 if ((info->add_timestamp & RB_ADD_STAMP_ABSOLUTE)) {
3537 info->delta = info->ts;
3540 * If interrupting an event time update, we may need an
3541 * absolute timestamp.
3542 * Don't bother if this is the start of a new page (w == 0).
3544 if (unlikely(!a_ok || !b_ok || (info->before != info->after && w))) {
3545 info->add_timestamp |= RB_ADD_STAMP_FORCE | RB_ADD_STAMP_EXTEND;
3546 info->length += RB_LEN_TIME_EXTEND;
3548 info->delta = info->ts - info->after;
3549 if (unlikely(test_time_stamp(info->delta))) {
3550 info->add_timestamp |= RB_ADD_STAMP_EXTEND;
3551 info->length += RB_LEN_TIME_EXTEND;
3556 /*B*/ rb_time_set(&cpu_buffer->before_stamp, info->ts);
3558 /*C*/ write = local_add_return(info->length, &tail_page->write);
3560 /* set write to only the index of the write */
3561 write &= RB_WRITE_MASK;
3563 tail = write - info->length;
3565 /* See if we shot pass the end of this buffer page */
3566 if (unlikely(write > BUF_PAGE_SIZE)) {
3567 /* before and after may now different, fix it up*/
3568 b_ok = rb_time_read(&cpu_buffer->before_stamp, &info->before);
3569 a_ok = rb_time_read(&cpu_buffer->write_stamp, &info->after);
3570 if (a_ok && b_ok && info->before != info->after)
3571 (void)rb_time_cmpxchg(&cpu_buffer->before_stamp,
3572 info->before, info->after);
3574 check_buffer(cpu_buffer, info, CHECK_FULL_PAGE);
3575 return rb_move_tail(cpu_buffer, tail, info);
3578 if (likely(tail == w)) {
3582 /* Nothing interrupted us between A and C */
3583 /*D*/ rb_time_set(&cpu_buffer->write_stamp, info->ts);
3585 /*E*/ s_ok = rb_time_read(&cpu_buffer->before_stamp, &save_before);
3586 RB_WARN_ON(cpu_buffer, !s_ok);
3587 if (likely(!(info->add_timestamp &
3588 (RB_ADD_STAMP_FORCE | RB_ADD_STAMP_ABSOLUTE))))
3589 /* This did not interrupt any time update */
3590 info->delta = info->ts - info->after;
3592 /* Just use full timestamp for interrupting event */
3593 info->delta = info->ts;
3595 check_buffer(cpu_buffer, info, tail);
3596 if (unlikely(info->ts != save_before)) {
3597 /* SLOW PATH - Interrupted between C and E */
3599 a_ok = rb_time_read(&cpu_buffer->write_stamp, &info->after);
3600 RB_WARN_ON(cpu_buffer, !a_ok);
3602 /* Write stamp must only go forward */
3603 if (save_before > info->after) {
3605 * We do not care about the result, only that
3606 * it gets updated atomically.
3608 (void)rb_time_cmpxchg(&cpu_buffer->write_stamp,
3609 info->after, save_before);
3614 /* SLOW PATH - Interrupted between A and C */
3615 a_ok = rb_time_read(&cpu_buffer->write_stamp, &info->after);
3616 /* Was interrupted before here, write_stamp must be valid */
3617 RB_WARN_ON(cpu_buffer, !a_ok);
3618 ts = rb_time_stamp(cpu_buffer->buffer);
3620 /*E*/ if (write == (local_read(&tail_page->write) & RB_WRITE_MASK) &&
3622 rb_time_cmpxchg(&cpu_buffer->write_stamp,
3624 /* Nothing came after this event between C and E */
3625 info->delta = ts - info->after;
3628 * Interrupted between C and E:
3629 * Lost the previous events time stamp. Just set the
3630 * delta to zero, and this will be the same time as
3631 * the event this event interrupted. And the events that
3632 * came after this will still be correct (as they would
3633 * have built their delta on the previous event.
3638 info->add_timestamp &= ~RB_ADD_STAMP_FORCE;
3642 * If this is the first commit on the page, then it has the same
3643 * timestamp as the page itself.
3645 if (unlikely(!tail && !(info->add_timestamp &
3646 (RB_ADD_STAMP_FORCE | RB_ADD_STAMP_ABSOLUTE))))
3649 /* We reserved something on the buffer */
3651 event = __rb_page_index(tail_page, tail);
3652 rb_update_event(cpu_buffer, event, info);
3654 local_inc(&tail_page->entries);
3657 * If this is the first commit on the page, then update
3660 if (unlikely(!tail))
3661 tail_page->page->time_stamp = info->ts;
3663 /* account for these added bytes */
3664 local_add(info->length, &cpu_buffer->entries_bytes);
3669 static __always_inline struct ring_buffer_event *
3670 rb_reserve_next_event(struct trace_buffer *buffer,
3671 struct ring_buffer_per_cpu *cpu_buffer,
3672 unsigned long length)
3674 struct ring_buffer_event *event;
3675 struct rb_event_info info;
3679 rb_start_commit(cpu_buffer);
3680 /* The commit page can not change after this */
3682 #ifdef CONFIG_RING_BUFFER_ALLOW_SWAP
3684 * Due to the ability to swap a cpu buffer from a buffer
3685 * it is possible it was swapped before we committed.
3686 * (committing stops a swap). We check for it here and
3687 * if it happened, we have to fail the write.
3690 if (unlikely(READ_ONCE(cpu_buffer->buffer) != buffer)) {
3691 local_dec(&cpu_buffer->committing);
3692 local_dec(&cpu_buffer->commits);
3697 info.length = rb_calculate_event_length(length);
3699 if (ring_buffer_time_stamp_abs(cpu_buffer->buffer)) {
3700 add_ts_default = RB_ADD_STAMP_ABSOLUTE;
3701 info.length += RB_LEN_TIME_EXTEND;
3703 add_ts_default = RB_ADD_STAMP_NONE;
3707 info.add_timestamp = add_ts_default;
3711 * We allow for interrupts to reenter here and do a trace.
3712 * If one does, it will cause this original code to loop
3713 * back here. Even with heavy interrupts happening, this
3714 * should only happen a few times in a row. If this happens
3715 * 1000 times in a row, there must be either an interrupt
3716 * storm or we have something buggy.
3719 if (RB_WARN_ON(cpu_buffer, ++nr_loops > 1000))
3722 event = __rb_reserve_next(cpu_buffer, &info);
3724 if (unlikely(PTR_ERR(event) == -EAGAIN)) {
3725 if (info.add_timestamp & (RB_ADD_STAMP_FORCE | RB_ADD_STAMP_EXTEND))
3726 info.length -= RB_LEN_TIME_EXTEND;
3733 rb_end_commit(cpu_buffer);
3738 * ring_buffer_lock_reserve - reserve a part of the buffer
3739 * @buffer: the ring buffer to reserve from
3740 * @length: the length of the data to reserve (excluding event header)
3742 * Returns a reserved event on the ring buffer to copy directly to.
3743 * The user of this interface will need to get the body to write into
3744 * and can use the ring_buffer_event_data() interface.
3746 * The length is the length of the data needed, not the event length
3747 * which also includes the event header.
3749 * Must be paired with ring_buffer_unlock_commit, unless NULL is returned.
3750 * If NULL is returned, then nothing has been allocated or locked.
3752 struct ring_buffer_event *
3753 ring_buffer_lock_reserve(struct trace_buffer *buffer, unsigned long length)
3755 struct ring_buffer_per_cpu *cpu_buffer;
3756 struct ring_buffer_event *event;
3759 /* If we are tracing schedule, we don't want to recurse */
3760 preempt_disable_notrace();
3762 if (unlikely(atomic_read(&buffer->record_disabled)))
3765 cpu = raw_smp_processor_id();
3767 if (unlikely(!cpumask_test_cpu(cpu, buffer->cpumask)))
3770 cpu_buffer = buffer->buffers[cpu];
3772 if (unlikely(atomic_read(&cpu_buffer->record_disabled)))
3775 if (unlikely(length > BUF_MAX_DATA_SIZE))
3778 if (unlikely(trace_recursive_lock(cpu_buffer)))
3781 event = rb_reserve_next_event(buffer, cpu_buffer, length);
3788 trace_recursive_unlock(cpu_buffer);
3790 preempt_enable_notrace();
3793 EXPORT_SYMBOL_GPL(ring_buffer_lock_reserve);
3796 * Decrement the entries to the page that an event is on.
3797 * The event does not even need to exist, only the pointer
3798 * to the page it is on. This may only be called before the commit
3802 rb_decrement_entry(struct ring_buffer_per_cpu *cpu_buffer,
3803 struct ring_buffer_event *event)
3805 unsigned long addr = (unsigned long)event;
3806 struct buffer_page *bpage = cpu_buffer->commit_page;
3807 struct buffer_page *start;
3811 /* Do the likely case first */
3812 if (likely(bpage->page == (void *)addr)) {
3813 local_dec(&bpage->entries);
3818 * Because the commit page may be on the reader page we
3819 * start with the next page and check the end loop there.
3821 rb_inc_page(&bpage);
3824 if (bpage->page == (void *)addr) {
3825 local_dec(&bpage->entries);
3828 rb_inc_page(&bpage);
3829 } while (bpage != start);
3831 /* commit not part of this buffer?? */
3832 RB_WARN_ON(cpu_buffer, 1);
3836 * ring_buffer_discard_commit - discard an event that has not been committed
3837 * @buffer: the ring buffer
3838 * @event: non committed event to discard
3840 * Sometimes an event that is in the ring buffer needs to be ignored.
3841 * This function lets the user discard an event in the ring buffer
3842 * and then that event will not be read later.
3844 * This function only works if it is called before the item has been
3845 * committed. It will try to free the event from the ring buffer
3846 * if another event has not been added behind it.
3848 * If another event has been added behind it, it will set the event
3849 * up as discarded, and perform the commit.
3851 * If this function is called, do not call ring_buffer_unlock_commit on
3854 void ring_buffer_discard_commit(struct trace_buffer *buffer,
3855 struct ring_buffer_event *event)
3857 struct ring_buffer_per_cpu *cpu_buffer;
3860 /* The event is discarded regardless */
3861 rb_event_discard(event);
3863 cpu = smp_processor_id();
3864 cpu_buffer = buffer->buffers[cpu];
3867 * This must only be called if the event has not been
3868 * committed yet. Thus we can assume that preemption
3869 * is still disabled.
3871 RB_WARN_ON(buffer, !local_read(&cpu_buffer->committing));
3873 rb_decrement_entry(cpu_buffer, event);
3874 if (rb_try_to_discard(cpu_buffer, event))
3878 rb_end_commit(cpu_buffer);
3880 trace_recursive_unlock(cpu_buffer);
3882 preempt_enable_notrace();
3885 EXPORT_SYMBOL_GPL(ring_buffer_discard_commit);
3888 * ring_buffer_write - write data to the buffer without reserving
3889 * @buffer: The ring buffer to write to.
3890 * @length: The length of the data being written (excluding the event header)
3891 * @data: The data to write to the buffer.
3893 * This is like ring_buffer_lock_reserve and ring_buffer_unlock_commit as
3894 * one function. If you already have the data to write to the buffer, it
3895 * may be easier to simply call this function.
3897 * Note, like ring_buffer_lock_reserve, the length is the length of the data
3898 * and not the length of the event which would hold the header.
3900 int ring_buffer_write(struct trace_buffer *buffer,
3901 unsigned long length,
3904 struct ring_buffer_per_cpu *cpu_buffer;
3905 struct ring_buffer_event *event;
3910 preempt_disable_notrace();
3912 if (atomic_read(&buffer->record_disabled))
3915 cpu = raw_smp_processor_id();
3917 if (!cpumask_test_cpu(cpu, buffer->cpumask))
3920 cpu_buffer = buffer->buffers[cpu];
3922 if (atomic_read(&cpu_buffer->record_disabled))
3925 if (length > BUF_MAX_DATA_SIZE)
3928 if (unlikely(trace_recursive_lock(cpu_buffer)))
3931 event = rb_reserve_next_event(buffer, cpu_buffer, length);
3935 body = rb_event_data(event);
3937 memcpy(body, data, length);
3939 rb_commit(cpu_buffer, event);
3941 rb_wakeups(buffer, cpu_buffer);
3946 trace_recursive_unlock(cpu_buffer);
3949 preempt_enable_notrace();
3953 EXPORT_SYMBOL_GPL(ring_buffer_write);
3955 static bool rb_per_cpu_empty(struct ring_buffer_per_cpu *cpu_buffer)
3957 struct buffer_page *reader = cpu_buffer->reader_page;
3958 struct buffer_page *head = rb_set_head_page(cpu_buffer);
3959 struct buffer_page *commit = cpu_buffer->commit_page;
3961 /* In case of error, head will be NULL */
3962 if (unlikely(!head))
3965 /* Reader should exhaust content in reader page */
3966 if (reader->read != rb_page_commit(reader))
3970 * If writers are committing on the reader page, knowing all
3971 * committed content has been read, the ring buffer is empty.
3973 if (commit == reader)
3977 * If writers are committing on a page other than reader page
3978 * and head page, there should always be content to read.
3984 * Writers are committing on the head page, we just need
3985 * to care about there're committed data, and the reader will
3986 * swap reader page with head page when it is to read data.
3988 return rb_page_commit(commit) == 0;
3992 * ring_buffer_record_disable - stop all writes into the buffer
3993 * @buffer: The ring buffer to stop writes to.
3995 * This prevents all writes to the buffer. Any attempt to write
3996 * to the buffer after this will fail and return NULL.
3998 * The caller should call synchronize_rcu() after this.
4000 void ring_buffer_record_disable(struct trace_buffer *buffer)
4002 atomic_inc(&buffer->record_disabled);
4004 EXPORT_SYMBOL_GPL(ring_buffer_record_disable);
4007 * ring_buffer_record_enable - enable writes to the buffer
4008 * @buffer: The ring buffer to enable writes
4010 * Note, multiple disables will need the same number of enables
4011 * to truly enable the writing (much like preempt_disable).
4013 void ring_buffer_record_enable(struct trace_buffer *buffer)
4015 atomic_dec(&buffer->record_disabled);
4017 EXPORT_SYMBOL_GPL(ring_buffer_record_enable);
4020 * ring_buffer_record_off - stop all writes into the buffer
4021 * @buffer: The ring buffer to stop writes to.
4023 * This prevents all writes to the buffer. Any attempt to write
4024 * to the buffer after this will fail and return NULL.
4026 * This is different than ring_buffer_record_disable() as
4027 * it works like an on/off switch, where as the disable() version
4028 * must be paired with a enable().
4030 void ring_buffer_record_off(struct trace_buffer *buffer)
4033 unsigned int new_rd;
4036 rd = atomic_read(&buffer->record_disabled);
4037 new_rd = rd | RB_BUFFER_OFF;
4038 } while (atomic_cmpxchg(&buffer->record_disabled, rd, new_rd) != rd);
4040 EXPORT_SYMBOL_GPL(ring_buffer_record_off);
4043 * ring_buffer_record_on - restart writes into the buffer
4044 * @buffer: The ring buffer to start writes to.
4046 * This enables all writes to the buffer that was disabled by
4047 * ring_buffer_record_off().
4049 * This is different than ring_buffer_record_enable() as
4050 * it works like an on/off switch, where as the enable() version
4051 * must be paired with a disable().
4053 void ring_buffer_record_on(struct trace_buffer *buffer)
4056 unsigned int new_rd;
4059 rd = atomic_read(&buffer->record_disabled);
4060 new_rd = rd & ~RB_BUFFER_OFF;
4061 } while (atomic_cmpxchg(&buffer->record_disabled, rd, new_rd) != rd);
4063 EXPORT_SYMBOL_GPL(ring_buffer_record_on);
4066 * ring_buffer_record_is_on - return true if the ring buffer can write
4067 * @buffer: The ring buffer to see if write is enabled
4069 * Returns true if the ring buffer is in a state that it accepts writes.
4071 bool ring_buffer_record_is_on(struct trace_buffer *buffer)
4073 return !atomic_read(&buffer->record_disabled);
4077 * ring_buffer_record_is_set_on - return true if the ring buffer is set writable
4078 * @buffer: The ring buffer to see if write is set enabled
4080 * Returns true if the ring buffer is set writable by ring_buffer_record_on().
4081 * Note that this does NOT mean it is in a writable state.
4083 * It may return true when the ring buffer has been disabled by
4084 * ring_buffer_record_disable(), as that is a temporary disabling of
4087 bool ring_buffer_record_is_set_on(struct trace_buffer *buffer)
4089 return !(atomic_read(&buffer->record_disabled) & RB_BUFFER_OFF);
4093 * ring_buffer_record_disable_cpu - stop all writes into the cpu_buffer
4094 * @buffer: The ring buffer to stop writes to.
4095 * @cpu: The CPU buffer to stop
4097 * This prevents all writes to the buffer. Any attempt to write
4098 * to the buffer after this will fail and return NULL.
4100 * The caller should call synchronize_rcu() after this.
4102 void ring_buffer_record_disable_cpu(struct trace_buffer *buffer, int cpu)
4104 struct ring_buffer_per_cpu *cpu_buffer;
4106 if (!cpumask_test_cpu(cpu, buffer->cpumask))
4109 cpu_buffer = buffer->buffers[cpu];
4110 atomic_inc(&cpu_buffer->record_disabled);
4112 EXPORT_SYMBOL_GPL(ring_buffer_record_disable_cpu);
4115 * ring_buffer_record_enable_cpu - enable writes to the buffer
4116 * @buffer: The ring buffer to enable writes
4117 * @cpu: The CPU to enable.
4119 * Note, multiple disables will need the same number of enables
4120 * to truly enable the writing (much like preempt_disable).
4122 void ring_buffer_record_enable_cpu(struct trace_buffer *buffer, int cpu)
4124 struct ring_buffer_per_cpu *cpu_buffer;
4126 if (!cpumask_test_cpu(cpu, buffer->cpumask))
4129 cpu_buffer = buffer->buffers[cpu];
4130 atomic_dec(&cpu_buffer->record_disabled);
4132 EXPORT_SYMBOL_GPL(ring_buffer_record_enable_cpu);
4135 * The total entries in the ring buffer is the running counter
4136 * of entries entered into the ring buffer, minus the sum of
4137 * the entries read from the ring buffer and the number of
4138 * entries that were overwritten.
4140 static inline unsigned long
4141 rb_num_of_entries(struct ring_buffer_per_cpu *cpu_buffer)
4143 return local_read(&cpu_buffer->entries) -
4144 (local_read(&cpu_buffer->overrun) + cpu_buffer->read);
4148 * ring_buffer_oldest_event_ts - get the oldest event timestamp from the buffer
4149 * @buffer: The ring buffer
4150 * @cpu: The per CPU buffer to read from.
4152 u64 ring_buffer_oldest_event_ts(struct trace_buffer *buffer, int cpu)
4154 unsigned long flags;
4155 struct ring_buffer_per_cpu *cpu_buffer;
4156 struct buffer_page *bpage;
4159 if (!cpumask_test_cpu(cpu, buffer->cpumask))
4162 cpu_buffer = buffer->buffers[cpu];
4163 raw_spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
4165 * if the tail is on reader_page, oldest time stamp is on the reader
4168 if (cpu_buffer->tail_page == cpu_buffer->reader_page)
4169 bpage = cpu_buffer->reader_page;
4171 bpage = rb_set_head_page(cpu_buffer);
4173 ret = bpage->page->time_stamp;
4174 raw_spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
4178 EXPORT_SYMBOL_GPL(ring_buffer_oldest_event_ts);
4181 * ring_buffer_bytes_cpu - get the number of bytes unconsumed in a cpu buffer
4182 * @buffer: The ring buffer
4183 * @cpu: The per CPU buffer to read from.
4185 unsigned long ring_buffer_bytes_cpu(struct trace_buffer *buffer, int cpu)
4187 struct ring_buffer_per_cpu *cpu_buffer;
4190 if (!cpumask_test_cpu(cpu, buffer->cpumask))
4193 cpu_buffer = buffer->buffers[cpu];
4194 ret = local_read(&cpu_buffer->entries_bytes) - cpu_buffer->read_bytes;
4198 EXPORT_SYMBOL_GPL(ring_buffer_bytes_cpu);
4201 * ring_buffer_entries_cpu - get the number of entries in a cpu buffer
4202 * @buffer: The ring buffer
4203 * @cpu: The per CPU buffer to get the entries from.
4205 unsigned long ring_buffer_entries_cpu(struct trace_buffer *buffer, int cpu)
4207 struct ring_buffer_per_cpu *cpu_buffer;
4209 if (!cpumask_test_cpu(cpu, buffer->cpumask))
4212 cpu_buffer = buffer->buffers[cpu];
4214 return rb_num_of_entries(cpu_buffer);
4216 EXPORT_SYMBOL_GPL(ring_buffer_entries_cpu);
4219 * ring_buffer_overrun_cpu - get the number of overruns caused by the ring
4220 * buffer wrapping around (only if RB_FL_OVERWRITE is on).
4221 * @buffer: The ring buffer
4222 * @cpu: The per CPU buffer to get the number of overruns from
4224 unsigned long ring_buffer_overrun_cpu(struct trace_buffer *buffer, int cpu)
4226 struct ring_buffer_per_cpu *cpu_buffer;
4229 if (!cpumask_test_cpu(cpu, buffer->cpumask))
4232 cpu_buffer = buffer->buffers[cpu];
4233 ret = local_read(&cpu_buffer->overrun);
4237 EXPORT_SYMBOL_GPL(ring_buffer_overrun_cpu);
4240 * ring_buffer_commit_overrun_cpu - get the number of overruns caused by
4241 * commits failing due to the buffer wrapping around while there are uncommitted
4242 * events, such as during an interrupt storm.
4243 * @buffer: The ring buffer
4244 * @cpu: The per CPU buffer to get the number of overruns from
4247 ring_buffer_commit_overrun_cpu(struct trace_buffer *buffer, int cpu)
4249 struct ring_buffer_per_cpu *cpu_buffer;
4252 if (!cpumask_test_cpu(cpu, buffer->cpumask))
4255 cpu_buffer = buffer->buffers[cpu];
4256 ret = local_read(&cpu_buffer->commit_overrun);
4260 EXPORT_SYMBOL_GPL(ring_buffer_commit_overrun_cpu);
4263 * ring_buffer_dropped_events_cpu - get the number of dropped events caused by
4264 * the ring buffer filling up (only if RB_FL_OVERWRITE is off).
4265 * @buffer: The ring buffer
4266 * @cpu: The per CPU buffer to get the number of overruns from
4269 ring_buffer_dropped_events_cpu(struct trace_buffer *buffer, int cpu)
4271 struct ring_buffer_per_cpu *cpu_buffer;
4274 if (!cpumask_test_cpu(cpu, buffer->cpumask))
4277 cpu_buffer = buffer->buffers[cpu];
4278 ret = local_read(&cpu_buffer->dropped_events);
4282 EXPORT_SYMBOL_GPL(ring_buffer_dropped_events_cpu);
4285 * ring_buffer_read_events_cpu - get the number of events successfully read
4286 * @buffer: The ring buffer
4287 * @cpu: The per CPU buffer to get the number of events read
4290 ring_buffer_read_events_cpu(struct trace_buffer *buffer, int cpu)
4292 struct ring_buffer_per_cpu *cpu_buffer;
4294 if (!cpumask_test_cpu(cpu, buffer->cpumask))
4297 cpu_buffer = buffer->buffers[cpu];
4298 return cpu_buffer->read;
4300 EXPORT_SYMBOL_GPL(ring_buffer_read_events_cpu);
4303 * ring_buffer_entries - get the number of entries in a buffer
4304 * @buffer: The ring buffer
4306 * Returns the total number of entries in the ring buffer
4309 unsigned long ring_buffer_entries(struct trace_buffer *buffer)
4311 struct ring_buffer_per_cpu *cpu_buffer;
4312 unsigned long entries = 0;
4315 /* if you care about this being correct, lock the buffer */
4316 for_each_buffer_cpu(buffer, cpu) {
4317 cpu_buffer = buffer->buffers[cpu];
4318 entries += rb_num_of_entries(cpu_buffer);
4323 EXPORT_SYMBOL_GPL(ring_buffer_entries);
4326 * ring_buffer_overruns - get the number of overruns in buffer
4327 * @buffer: The ring buffer
4329 * Returns the total number of overruns in the ring buffer
4332 unsigned long ring_buffer_overruns(struct trace_buffer *buffer)
4334 struct ring_buffer_per_cpu *cpu_buffer;
4335 unsigned long overruns = 0;
4338 /* if you care about this being correct, lock the buffer */
4339 for_each_buffer_cpu(buffer, cpu) {
4340 cpu_buffer = buffer->buffers[cpu];
4341 overruns += local_read(&cpu_buffer->overrun);
4346 EXPORT_SYMBOL_GPL(ring_buffer_overruns);
4348 static void rb_iter_reset(struct ring_buffer_iter *iter)
4350 struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
4352 /* Iterator usage is expected to have record disabled */
4353 iter->head_page = cpu_buffer->reader_page;
4354 iter->head = cpu_buffer->reader_page->read;
4355 iter->next_event = iter->head;
4357 iter->cache_reader_page = iter->head_page;
4358 iter->cache_read = cpu_buffer->read;
4359 iter->cache_pages_removed = cpu_buffer->pages_removed;
4362 iter->read_stamp = cpu_buffer->read_stamp;
4363 iter->page_stamp = cpu_buffer->reader_page->page->time_stamp;
4365 iter->read_stamp = iter->head_page->page->time_stamp;
4366 iter->page_stamp = iter->read_stamp;
4371 * ring_buffer_iter_reset - reset an iterator
4372 * @iter: The iterator to reset
4374 * Resets the iterator, so that it will start from the beginning
4377 void ring_buffer_iter_reset(struct ring_buffer_iter *iter)
4379 struct ring_buffer_per_cpu *cpu_buffer;
4380 unsigned long flags;
4385 cpu_buffer = iter->cpu_buffer;
4387 raw_spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
4388 rb_iter_reset(iter);
4389 raw_spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
4391 EXPORT_SYMBOL_GPL(ring_buffer_iter_reset);
4394 * ring_buffer_iter_empty - check if an iterator has no more to read
4395 * @iter: The iterator to check
4397 int ring_buffer_iter_empty(struct ring_buffer_iter *iter)
4399 struct ring_buffer_per_cpu *cpu_buffer;
4400 struct buffer_page *reader;
4401 struct buffer_page *head_page;
4402 struct buffer_page *commit_page;
4403 struct buffer_page *curr_commit_page;
4408 cpu_buffer = iter->cpu_buffer;
4409 reader = cpu_buffer->reader_page;
4410 head_page = cpu_buffer->head_page;
4411 commit_page = cpu_buffer->commit_page;
4412 commit_ts = commit_page->page->time_stamp;
4415 * When the writer goes across pages, it issues a cmpxchg which
4416 * is a mb(), which will synchronize with the rmb here.
4417 * (see rb_tail_page_update())
4420 commit = rb_page_commit(commit_page);
4421 /* We want to make sure that the commit page doesn't change */
4424 /* Make sure commit page didn't change */
4425 curr_commit_page = READ_ONCE(cpu_buffer->commit_page);
4426 curr_commit_ts = READ_ONCE(curr_commit_page->page->time_stamp);
4428 /* If the commit page changed, then there's more data */
4429 if (curr_commit_page != commit_page ||
4430 curr_commit_ts != commit_ts)
4433 /* Still racy, as it may return a false positive, but that's OK */
4434 return ((iter->head_page == commit_page && iter->head >= commit) ||
4435 (iter->head_page == reader && commit_page == head_page &&
4436 head_page->read == commit &&
4437 iter->head == rb_page_commit(cpu_buffer->reader_page)));
4439 EXPORT_SYMBOL_GPL(ring_buffer_iter_empty);
4442 rb_update_read_stamp(struct ring_buffer_per_cpu *cpu_buffer,
4443 struct ring_buffer_event *event)
4447 switch (event->type_len) {
4448 case RINGBUF_TYPE_PADDING:
4451 case RINGBUF_TYPE_TIME_EXTEND:
4452 delta = rb_event_time_stamp(event);
4453 cpu_buffer->read_stamp += delta;
4456 case RINGBUF_TYPE_TIME_STAMP:
4457 delta = rb_event_time_stamp(event);
4458 cpu_buffer->read_stamp = delta;
4461 case RINGBUF_TYPE_DATA:
4462 cpu_buffer->read_stamp += event->time_delta;
4466 RB_WARN_ON(cpu_buffer, 1);
4472 rb_update_iter_read_stamp(struct ring_buffer_iter *iter,
4473 struct ring_buffer_event *event)
4477 switch (event->type_len) {
4478 case RINGBUF_TYPE_PADDING:
4481 case RINGBUF_TYPE_TIME_EXTEND:
4482 delta = rb_event_time_stamp(event);
4483 iter->read_stamp += delta;
4486 case RINGBUF_TYPE_TIME_STAMP:
4487 delta = rb_event_time_stamp(event);
4488 iter->read_stamp = delta;
4491 case RINGBUF_TYPE_DATA:
4492 iter->read_stamp += event->time_delta;
4496 RB_WARN_ON(iter->cpu_buffer, 1);
4501 static struct buffer_page *
4502 rb_get_reader_page(struct ring_buffer_per_cpu *cpu_buffer)
4504 struct buffer_page *reader = NULL;
4505 unsigned long overwrite;
4506 unsigned long flags;
4510 local_irq_save(flags);
4511 arch_spin_lock(&cpu_buffer->lock);
4515 * This should normally only loop twice. But because the
4516 * start of the reader inserts an empty page, it causes
4517 * a case where we will loop three times. There should be no
4518 * reason to loop four times (that I know of).
4520 if (RB_WARN_ON(cpu_buffer, ++nr_loops > 3)) {
4525 reader = cpu_buffer->reader_page;
4527 /* If there's more to read, return this page */
4528 if (cpu_buffer->reader_page->read < rb_page_size(reader))
4531 /* Never should we have an index greater than the size */
4532 if (RB_WARN_ON(cpu_buffer,
4533 cpu_buffer->reader_page->read > rb_page_size(reader)))
4536 /* check if we caught up to the tail */
4538 if (cpu_buffer->commit_page == cpu_buffer->reader_page)
4541 /* Don't bother swapping if the ring buffer is empty */
4542 if (rb_num_of_entries(cpu_buffer) == 0)
4546 * Reset the reader page to size zero.
4548 local_set(&cpu_buffer->reader_page->write, 0);
4549 local_set(&cpu_buffer->reader_page->entries, 0);
4550 local_set(&cpu_buffer->reader_page->page->commit, 0);
4551 cpu_buffer->reader_page->real_end = 0;
4555 * Splice the empty reader page into the list around the head.
4557 reader = rb_set_head_page(cpu_buffer);
4560 cpu_buffer->reader_page->list.next = rb_list_head(reader->list.next);
4561 cpu_buffer->reader_page->list.prev = reader->list.prev;
4564 * cpu_buffer->pages just needs to point to the buffer, it
4565 * has no specific buffer page to point to. Lets move it out
4566 * of our way so we don't accidentally swap it.
4568 cpu_buffer->pages = reader->list.prev;
4570 /* The reader page will be pointing to the new head */
4571 rb_set_list_to_head(&cpu_buffer->reader_page->list);
4574 * We want to make sure we read the overruns after we set up our
4575 * pointers to the next object. The writer side does a
4576 * cmpxchg to cross pages which acts as the mb on the writer
4577 * side. Note, the reader will constantly fail the swap
4578 * while the writer is updating the pointers, so this
4579 * guarantees that the overwrite recorded here is the one we
4580 * want to compare with the last_overrun.
4583 overwrite = local_read(&(cpu_buffer->overrun));
4586 * Here's the tricky part.
4588 * We need to move the pointer past the header page.
4589 * But we can only do that if a writer is not currently
4590 * moving it. The page before the header page has the
4591 * flag bit '1' set if it is pointing to the page we want.
4592 * but if the writer is in the process of moving it
4593 * than it will be '2' or already moved '0'.
4596 ret = rb_head_page_replace(reader, cpu_buffer->reader_page);
4599 * If we did not convert it, then we must try again.
4605 * Yay! We succeeded in replacing the page.
4607 * Now make the new head point back to the reader page.
4609 rb_list_head(reader->list.next)->prev = &cpu_buffer->reader_page->list;
4610 rb_inc_page(&cpu_buffer->head_page);
4612 local_inc(&cpu_buffer->pages_read);
4614 /* Finally update the reader page to the new head */
4615 cpu_buffer->reader_page = reader;
4616 cpu_buffer->reader_page->read = 0;
4618 if (overwrite != cpu_buffer->last_overrun) {
4619 cpu_buffer->lost_events = overwrite - cpu_buffer->last_overrun;
4620 cpu_buffer->last_overrun = overwrite;
4626 /* Update the read_stamp on the first event */
4627 if (reader && reader->read == 0)
4628 cpu_buffer->read_stamp = reader->page->time_stamp;
4630 arch_spin_unlock(&cpu_buffer->lock);
4631 local_irq_restore(flags);
4634 * The writer has preempt disable, wait for it. But not forever
4635 * Although, 1 second is pretty much "forever"
4637 #define USECS_WAIT 1000000
4638 for (nr_loops = 0; nr_loops < USECS_WAIT; nr_loops++) {
4639 /* If the write is past the end of page, a writer is still updating it */
4640 if (likely(!reader || rb_page_write(reader) <= BUF_PAGE_SIZE))
4645 /* Get the latest version of the reader write value */
4649 /* The writer is not moving forward? Something is wrong */
4650 if (RB_WARN_ON(cpu_buffer, nr_loops == USECS_WAIT))
4654 * Make sure we see any padding after the write update
4655 * (see rb_reset_tail()).
4657 * In addition, a writer may be writing on the reader page
4658 * if the page has not been fully filled, so the read barrier
4659 * is also needed to make sure we see the content of what is
4660 * committed by the writer (see rb_set_commit_to_write()).
4668 static void rb_advance_reader(struct ring_buffer_per_cpu *cpu_buffer)
4670 struct ring_buffer_event *event;
4671 struct buffer_page *reader;
4674 reader = rb_get_reader_page(cpu_buffer);
4676 /* This function should not be called when buffer is empty */
4677 if (RB_WARN_ON(cpu_buffer, !reader))
4680 event = rb_reader_event(cpu_buffer);
4682 if (event->type_len <= RINGBUF_TYPE_DATA_TYPE_LEN_MAX)
4685 rb_update_read_stamp(cpu_buffer, event);
4687 length = rb_event_length(event);
4688 cpu_buffer->reader_page->read += length;
4689 cpu_buffer->read_bytes += length;
4692 static void rb_advance_iter(struct ring_buffer_iter *iter)
4694 struct ring_buffer_per_cpu *cpu_buffer;
4696 cpu_buffer = iter->cpu_buffer;
4698 /* If head == next_event then we need to jump to the next event */
4699 if (iter->head == iter->next_event) {
4700 /* If the event gets overwritten again, there's nothing to do */
4701 if (rb_iter_head_event(iter) == NULL)
4705 iter->head = iter->next_event;
4708 * Check if we are at the end of the buffer.
4710 if (iter->next_event >= rb_page_size(iter->head_page)) {
4711 /* discarded commits can make the page empty */
4712 if (iter->head_page == cpu_buffer->commit_page)
4718 rb_update_iter_read_stamp(iter, iter->event);
4721 static int rb_lost_events(struct ring_buffer_per_cpu *cpu_buffer)
4723 return cpu_buffer->lost_events;
4726 static struct ring_buffer_event *
4727 rb_buffer_peek(struct ring_buffer_per_cpu *cpu_buffer, u64 *ts,
4728 unsigned long *lost_events)
4730 struct ring_buffer_event *event;
4731 struct buffer_page *reader;
4738 * We repeat when a time extend is encountered.
4739 * Since the time extend is always attached to a data event,
4740 * we should never loop more than once.
4741 * (We never hit the following condition more than twice).
4743 if (RB_WARN_ON(cpu_buffer, ++nr_loops > 2))
4746 reader = rb_get_reader_page(cpu_buffer);
4750 event = rb_reader_event(cpu_buffer);
4752 switch (event->type_len) {
4753 case RINGBUF_TYPE_PADDING:
4754 if (rb_null_event(event))
4755 RB_WARN_ON(cpu_buffer, 1);
4757 * Because the writer could be discarding every
4758 * event it creates (which would probably be bad)
4759 * if we were to go back to "again" then we may never
4760 * catch up, and will trigger the warn on, or lock
4761 * the box. Return the padding, and we will release
4762 * the current locks, and try again.
4766 case RINGBUF_TYPE_TIME_EXTEND:
4767 /* Internal data, OK to advance */
4768 rb_advance_reader(cpu_buffer);
4771 case RINGBUF_TYPE_TIME_STAMP:
4773 *ts = rb_event_time_stamp(event);
4774 ring_buffer_normalize_time_stamp(cpu_buffer->buffer,
4775 cpu_buffer->cpu, ts);
4777 /* Internal data, OK to advance */
4778 rb_advance_reader(cpu_buffer);
4781 case RINGBUF_TYPE_DATA:
4783 *ts = cpu_buffer->read_stamp + event->time_delta;
4784 ring_buffer_normalize_time_stamp(cpu_buffer->buffer,
4785 cpu_buffer->cpu, ts);
4788 *lost_events = rb_lost_events(cpu_buffer);
4792 RB_WARN_ON(cpu_buffer, 1);
4797 EXPORT_SYMBOL_GPL(ring_buffer_peek);
4799 static struct ring_buffer_event *
4800 rb_iter_peek(struct ring_buffer_iter *iter, u64 *ts)
4802 struct trace_buffer *buffer;
4803 struct ring_buffer_per_cpu *cpu_buffer;
4804 struct ring_buffer_event *event;
4810 cpu_buffer = iter->cpu_buffer;
4811 buffer = cpu_buffer->buffer;
4814 * Check if someone performed a consuming read to the buffer
4815 * or removed some pages from the buffer. In these cases,
4816 * iterator was invalidated and we need to reset it.
4818 if (unlikely(iter->cache_read != cpu_buffer->read ||
4819 iter->cache_reader_page != cpu_buffer->reader_page ||
4820 iter->cache_pages_removed != cpu_buffer->pages_removed))
4821 rb_iter_reset(iter);
4824 if (ring_buffer_iter_empty(iter))
4828 * As the writer can mess with what the iterator is trying
4829 * to read, just give up if we fail to get an event after
4830 * three tries. The iterator is not as reliable when reading
4831 * the ring buffer with an active write as the consumer is.
4832 * Do not warn if the three failures is reached.
4837 if (rb_per_cpu_empty(cpu_buffer))
4840 if (iter->head >= rb_page_size(iter->head_page)) {
4845 event = rb_iter_head_event(iter);
4849 switch (event->type_len) {
4850 case RINGBUF_TYPE_PADDING:
4851 if (rb_null_event(event)) {
4855 rb_advance_iter(iter);
4858 case RINGBUF_TYPE_TIME_EXTEND:
4859 /* Internal data, OK to advance */
4860 rb_advance_iter(iter);
4863 case RINGBUF_TYPE_TIME_STAMP:
4865 *ts = rb_event_time_stamp(event);
4866 ring_buffer_normalize_time_stamp(cpu_buffer->buffer,
4867 cpu_buffer->cpu, ts);
4869 /* Internal data, OK to advance */
4870 rb_advance_iter(iter);
4873 case RINGBUF_TYPE_DATA:
4875 *ts = iter->read_stamp + event->time_delta;
4876 ring_buffer_normalize_time_stamp(buffer,
4877 cpu_buffer->cpu, ts);
4882 RB_WARN_ON(cpu_buffer, 1);
4887 EXPORT_SYMBOL_GPL(ring_buffer_iter_peek);
4889 static inline bool rb_reader_lock(struct ring_buffer_per_cpu *cpu_buffer)
4891 if (likely(!in_nmi())) {
4892 raw_spin_lock(&cpu_buffer->reader_lock);
4897 * If an NMI die dumps out the content of the ring buffer
4898 * trylock must be used to prevent a deadlock if the NMI
4899 * preempted a task that holds the ring buffer locks. If
4900 * we get the lock then all is fine, if not, then continue
4901 * to do the read, but this can corrupt the ring buffer,
4902 * so it must be permanently disabled from future writes.
4903 * Reading from NMI is a oneshot deal.
4905 if (raw_spin_trylock(&cpu_buffer->reader_lock))
4908 /* Continue without locking, but disable the ring buffer */
4909 atomic_inc(&cpu_buffer->record_disabled);
4914 rb_reader_unlock(struct ring_buffer_per_cpu *cpu_buffer, bool locked)
4917 raw_spin_unlock(&cpu_buffer->reader_lock);
4922 * ring_buffer_peek - peek at the next event to be read
4923 * @buffer: The ring buffer to read
4924 * @cpu: The cpu to peak at
4925 * @ts: The timestamp counter of this event.
4926 * @lost_events: a variable to store if events were lost (may be NULL)
4928 * This will return the event that will be read next, but does
4929 * not consume the data.
4931 struct ring_buffer_event *
4932 ring_buffer_peek(struct trace_buffer *buffer, int cpu, u64 *ts,
4933 unsigned long *lost_events)
4935 struct ring_buffer_per_cpu *cpu_buffer = buffer->buffers[cpu];
4936 struct ring_buffer_event *event;
4937 unsigned long flags;
4940 if (!cpumask_test_cpu(cpu, buffer->cpumask))
4944 local_irq_save(flags);
4945 dolock = rb_reader_lock(cpu_buffer);
4946 event = rb_buffer_peek(cpu_buffer, ts, lost_events);
4947 if (event && event->type_len == RINGBUF_TYPE_PADDING)
4948 rb_advance_reader(cpu_buffer);
4949 rb_reader_unlock(cpu_buffer, dolock);
4950 local_irq_restore(flags);
4952 if (event && event->type_len == RINGBUF_TYPE_PADDING)
4958 /** ring_buffer_iter_dropped - report if there are dropped events
4959 * @iter: The ring buffer iterator
4961 * Returns true if there was dropped events since the last peek.
4963 bool ring_buffer_iter_dropped(struct ring_buffer_iter *iter)
4965 bool ret = iter->missed_events != 0;
4967 iter->missed_events = 0;
4970 EXPORT_SYMBOL_GPL(ring_buffer_iter_dropped);
4973 * ring_buffer_iter_peek - peek at the next event to be read
4974 * @iter: The ring buffer iterator
4975 * @ts: The timestamp counter of this event.
4977 * This will return the event that will be read next, but does
4978 * not increment the iterator.
4980 struct ring_buffer_event *
4981 ring_buffer_iter_peek(struct ring_buffer_iter *iter, u64 *ts)
4983 struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
4984 struct ring_buffer_event *event;
4985 unsigned long flags;
4988 raw_spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
4989 event = rb_iter_peek(iter, ts);
4990 raw_spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
4992 if (event && event->type_len == RINGBUF_TYPE_PADDING)
4999 * ring_buffer_consume - return an event and consume it
5000 * @buffer: The ring buffer to get the next event from
5001 * @cpu: the cpu to read the buffer from
5002 * @ts: a variable to store the timestamp (may be NULL)
5003 * @lost_events: a variable to store if events were lost (may be NULL)
5005 * Returns the next event in the ring buffer, and that event is consumed.
5006 * Meaning, that sequential reads will keep returning a different event,
5007 * and eventually empty the ring buffer if the producer is slower.
5009 struct ring_buffer_event *
5010 ring_buffer_consume(struct trace_buffer *buffer, int cpu, u64 *ts,
5011 unsigned long *lost_events)
5013 struct ring_buffer_per_cpu *cpu_buffer;
5014 struct ring_buffer_event *event = NULL;
5015 unsigned long flags;
5019 /* might be called in atomic */
5022 if (!cpumask_test_cpu(cpu, buffer->cpumask))
5025 cpu_buffer = buffer->buffers[cpu];
5026 local_irq_save(flags);
5027 dolock = rb_reader_lock(cpu_buffer);
5029 event = rb_buffer_peek(cpu_buffer, ts, lost_events);
5031 cpu_buffer->lost_events = 0;
5032 rb_advance_reader(cpu_buffer);
5035 rb_reader_unlock(cpu_buffer, dolock);
5036 local_irq_restore(flags);
5041 if (event && event->type_len == RINGBUF_TYPE_PADDING)
5046 EXPORT_SYMBOL_GPL(ring_buffer_consume);
5049 * ring_buffer_read_prepare - Prepare for a non consuming read of the buffer
5050 * @buffer: The ring buffer to read from
5051 * @cpu: The cpu buffer to iterate over
5052 * @flags: gfp flags to use for memory allocation
5054 * This performs the initial preparations necessary to iterate
5055 * through the buffer. Memory is allocated, buffer recording
5056 * is disabled, and the iterator pointer is returned to the caller.
5058 * Disabling buffer recording prevents the reading from being
5059 * corrupted. This is not a consuming read, so a producer is not
5062 * After a sequence of ring_buffer_read_prepare calls, the user is
5063 * expected to make at least one call to ring_buffer_read_prepare_sync.
5064 * Afterwards, ring_buffer_read_start is invoked to get things going
5067 * This overall must be paired with ring_buffer_read_finish.
5069 struct ring_buffer_iter *
5070 ring_buffer_read_prepare(struct trace_buffer *buffer, int cpu, gfp_t flags)
5072 struct ring_buffer_per_cpu *cpu_buffer;
5073 struct ring_buffer_iter *iter;
5075 if (!cpumask_test_cpu(cpu, buffer->cpumask))
5078 iter = kzalloc(sizeof(*iter), flags);
5082 iter->event = kmalloc(BUF_MAX_DATA_SIZE, flags);
5088 cpu_buffer = buffer->buffers[cpu];
5090 iter->cpu_buffer = cpu_buffer;
5092 atomic_inc(&cpu_buffer->resize_disabled);
5096 EXPORT_SYMBOL_GPL(ring_buffer_read_prepare);
5099 * ring_buffer_read_prepare_sync - Synchronize a set of prepare calls
5101 * All previously invoked ring_buffer_read_prepare calls to prepare
5102 * iterators will be synchronized. Afterwards, read_buffer_read_start
5103 * calls on those iterators are allowed.
5106 ring_buffer_read_prepare_sync(void)
5110 EXPORT_SYMBOL_GPL(ring_buffer_read_prepare_sync);
5113 * ring_buffer_read_start - start a non consuming read of the buffer
5114 * @iter: The iterator returned by ring_buffer_read_prepare
5116 * This finalizes the startup of an iteration through the buffer.
5117 * The iterator comes from a call to ring_buffer_read_prepare and
5118 * an intervening ring_buffer_read_prepare_sync must have been
5121 * Must be paired with ring_buffer_read_finish.
5124 ring_buffer_read_start(struct ring_buffer_iter *iter)
5126 struct ring_buffer_per_cpu *cpu_buffer;
5127 unsigned long flags;
5132 cpu_buffer = iter->cpu_buffer;
5134 raw_spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
5135 arch_spin_lock(&cpu_buffer->lock);
5136 rb_iter_reset(iter);
5137 arch_spin_unlock(&cpu_buffer->lock);
5138 raw_spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
5140 EXPORT_SYMBOL_GPL(ring_buffer_read_start);
5143 * ring_buffer_read_finish - finish reading the iterator of the buffer
5144 * @iter: The iterator retrieved by ring_buffer_start
5146 * This re-enables the recording to the buffer, and frees the
5150 ring_buffer_read_finish(struct ring_buffer_iter *iter)
5152 struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
5153 unsigned long flags;
5156 * Ring buffer is disabled from recording, here's a good place
5157 * to check the integrity of the ring buffer.
5158 * Must prevent readers from trying to read, as the check
5159 * clears the HEAD page and readers require it.
5161 raw_spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
5162 rb_check_pages(cpu_buffer);
5163 raw_spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
5165 atomic_dec(&cpu_buffer->resize_disabled);
5169 EXPORT_SYMBOL_GPL(ring_buffer_read_finish);
5172 * ring_buffer_iter_advance - advance the iterator to the next location
5173 * @iter: The ring buffer iterator
5175 * Move the location of the iterator such that the next read will
5176 * be the next location of the iterator.
5178 void ring_buffer_iter_advance(struct ring_buffer_iter *iter)
5180 struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
5181 unsigned long flags;
5183 raw_spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
5185 rb_advance_iter(iter);
5187 raw_spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
5189 EXPORT_SYMBOL_GPL(ring_buffer_iter_advance);
5192 * ring_buffer_size - return the size of the ring buffer (in bytes)
5193 * @buffer: The ring buffer.
5194 * @cpu: The CPU to get ring buffer size from.
5196 unsigned long ring_buffer_size(struct trace_buffer *buffer, int cpu)
5199 * Earlier, this method returned
5200 * BUF_PAGE_SIZE * buffer->nr_pages
5201 * Since the nr_pages field is now removed, we have converted this to
5202 * return the per cpu buffer value.
5204 if (!cpumask_test_cpu(cpu, buffer->cpumask))
5207 return BUF_PAGE_SIZE * buffer->buffers[cpu]->nr_pages;
5209 EXPORT_SYMBOL_GPL(ring_buffer_size);
5211 static void rb_clear_buffer_page(struct buffer_page *page)
5213 local_set(&page->write, 0);
5214 local_set(&page->entries, 0);
5215 rb_init_page(page->page);
5220 rb_reset_cpu(struct ring_buffer_per_cpu *cpu_buffer)
5222 struct buffer_page *page;
5224 rb_head_page_deactivate(cpu_buffer);
5226 cpu_buffer->head_page
5227 = list_entry(cpu_buffer->pages, struct buffer_page, list);
5228 rb_clear_buffer_page(cpu_buffer->head_page);
5229 list_for_each_entry(page, cpu_buffer->pages, list) {
5230 rb_clear_buffer_page(page);
5233 cpu_buffer->tail_page = cpu_buffer->head_page;
5234 cpu_buffer->commit_page = cpu_buffer->head_page;
5236 INIT_LIST_HEAD(&cpu_buffer->reader_page->list);
5237 INIT_LIST_HEAD(&cpu_buffer->new_pages);
5238 rb_clear_buffer_page(cpu_buffer->reader_page);
5240 local_set(&cpu_buffer->entries_bytes, 0);
5241 local_set(&cpu_buffer->overrun, 0);
5242 local_set(&cpu_buffer->commit_overrun, 0);
5243 local_set(&cpu_buffer->dropped_events, 0);
5244 local_set(&cpu_buffer->entries, 0);
5245 local_set(&cpu_buffer->committing, 0);
5246 local_set(&cpu_buffer->commits, 0);
5247 local_set(&cpu_buffer->pages_touched, 0);
5248 local_set(&cpu_buffer->pages_lost, 0);
5249 local_set(&cpu_buffer->pages_read, 0);
5250 cpu_buffer->last_pages_touch = 0;
5251 cpu_buffer->shortest_full = 0;
5252 cpu_buffer->read = 0;
5253 cpu_buffer->read_bytes = 0;
5255 rb_time_set(&cpu_buffer->write_stamp, 0);
5256 rb_time_set(&cpu_buffer->before_stamp, 0);
5258 memset(cpu_buffer->event_stamp, 0, sizeof(cpu_buffer->event_stamp));
5260 cpu_buffer->lost_events = 0;
5261 cpu_buffer->last_overrun = 0;
5263 rb_head_page_activate(cpu_buffer);
5264 cpu_buffer->pages_removed = 0;
5267 /* Must have disabled the cpu buffer then done a synchronize_rcu */
5268 static void reset_disabled_cpu_buffer(struct ring_buffer_per_cpu *cpu_buffer)
5270 unsigned long flags;
5272 raw_spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
5274 if (RB_WARN_ON(cpu_buffer, local_read(&cpu_buffer->committing)))
5277 arch_spin_lock(&cpu_buffer->lock);
5279 rb_reset_cpu(cpu_buffer);
5281 arch_spin_unlock(&cpu_buffer->lock);
5284 raw_spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
5288 * ring_buffer_reset_cpu - reset a ring buffer per CPU buffer
5289 * @buffer: The ring buffer to reset a per cpu buffer of
5290 * @cpu: The CPU buffer to be reset
5292 void ring_buffer_reset_cpu(struct trace_buffer *buffer, int cpu)
5294 struct ring_buffer_per_cpu *cpu_buffer = buffer->buffers[cpu];
5296 if (!cpumask_test_cpu(cpu, buffer->cpumask))
5299 /* prevent another thread from changing buffer sizes */
5300 mutex_lock(&buffer->mutex);
5302 atomic_inc(&cpu_buffer->resize_disabled);
5303 atomic_inc(&cpu_buffer->record_disabled);
5305 /* Make sure all commits have finished */
5308 reset_disabled_cpu_buffer(cpu_buffer);
5310 atomic_dec(&cpu_buffer->record_disabled);
5311 atomic_dec(&cpu_buffer->resize_disabled);
5313 mutex_unlock(&buffer->mutex);
5315 EXPORT_SYMBOL_GPL(ring_buffer_reset_cpu);
5317 /* Flag to ensure proper resetting of atomic variables */
5318 #define RESET_BIT (1 << 30)
5321 * ring_buffer_reset_cpu - reset a ring buffer per CPU buffer
5322 * @buffer: The ring buffer to reset a per cpu buffer of
5323 * @cpu: The CPU buffer to be reset
5325 void ring_buffer_reset_online_cpus(struct trace_buffer *buffer)
5327 struct ring_buffer_per_cpu *cpu_buffer;
5330 /* prevent another thread from changing buffer sizes */
5331 mutex_lock(&buffer->mutex);
5333 for_each_online_buffer_cpu(buffer, cpu) {
5334 cpu_buffer = buffer->buffers[cpu];
5336 atomic_add(RESET_BIT, &cpu_buffer->resize_disabled);
5337 atomic_inc(&cpu_buffer->record_disabled);
5340 /* Make sure all commits have finished */
5343 for_each_buffer_cpu(buffer, cpu) {
5344 cpu_buffer = buffer->buffers[cpu];
5347 * If a CPU came online during the synchronize_rcu(), then
5350 if (!(atomic_read(&cpu_buffer->resize_disabled) & RESET_BIT))
5353 reset_disabled_cpu_buffer(cpu_buffer);
5355 atomic_dec(&cpu_buffer->record_disabled);
5356 atomic_sub(RESET_BIT, &cpu_buffer->resize_disabled);
5359 mutex_unlock(&buffer->mutex);
5363 * ring_buffer_reset - reset a ring buffer
5364 * @buffer: The ring buffer to reset all cpu buffers
5366 void ring_buffer_reset(struct trace_buffer *buffer)
5368 struct ring_buffer_per_cpu *cpu_buffer;
5371 /* prevent another thread from changing buffer sizes */
5372 mutex_lock(&buffer->mutex);
5374 for_each_buffer_cpu(buffer, cpu) {
5375 cpu_buffer = buffer->buffers[cpu];
5377 atomic_inc(&cpu_buffer->resize_disabled);
5378 atomic_inc(&cpu_buffer->record_disabled);
5381 /* Make sure all commits have finished */
5384 for_each_buffer_cpu(buffer, cpu) {
5385 cpu_buffer = buffer->buffers[cpu];
5387 reset_disabled_cpu_buffer(cpu_buffer);
5389 atomic_dec(&cpu_buffer->record_disabled);
5390 atomic_dec(&cpu_buffer->resize_disabled);
5393 mutex_unlock(&buffer->mutex);
5395 EXPORT_SYMBOL_GPL(ring_buffer_reset);
5398 * rind_buffer_empty - is the ring buffer empty?
5399 * @buffer: The ring buffer to test
5401 bool ring_buffer_empty(struct trace_buffer *buffer)
5403 struct ring_buffer_per_cpu *cpu_buffer;
5404 unsigned long flags;
5409 /* yes this is racy, but if you don't like the race, lock the buffer */
5410 for_each_buffer_cpu(buffer, cpu) {
5411 cpu_buffer = buffer->buffers[cpu];
5412 local_irq_save(flags);
5413 dolock = rb_reader_lock(cpu_buffer);
5414 ret = rb_per_cpu_empty(cpu_buffer);
5415 rb_reader_unlock(cpu_buffer, dolock);
5416 local_irq_restore(flags);
5424 EXPORT_SYMBOL_GPL(ring_buffer_empty);
5427 * ring_buffer_empty_cpu - is a cpu buffer of a ring buffer empty?
5428 * @buffer: The ring buffer
5429 * @cpu: The CPU buffer to test
5431 bool ring_buffer_empty_cpu(struct trace_buffer *buffer, int cpu)
5433 struct ring_buffer_per_cpu *cpu_buffer;
5434 unsigned long flags;
5438 if (!cpumask_test_cpu(cpu, buffer->cpumask))
5441 cpu_buffer = buffer->buffers[cpu];
5442 local_irq_save(flags);
5443 dolock = rb_reader_lock(cpu_buffer);
5444 ret = rb_per_cpu_empty(cpu_buffer);
5445 rb_reader_unlock(cpu_buffer, dolock);
5446 local_irq_restore(flags);
5450 EXPORT_SYMBOL_GPL(ring_buffer_empty_cpu);
5452 #ifdef CONFIG_RING_BUFFER_ALLOW_SWAP
5454 * ring_buffer_swap_cpu - swap a CPU buffer between two ring buffers
5455 * @buffer_a: One buffer to swap with
5456 * @buffer_b: The other buffer to swap with
5457 * @cpu: the CPU of the buffers to swap
5459 * This function is useful for tracers that want to take a "snapshot"
5460 * of a CPU buffer and has another back up buffer lying around.
5461 * it is expected that the tracer handles the cpu buffer not being
5462 * used at the moment.
5464 int ring_buffer_swap_cpu(struct trace_buffer *buffer_a,
5465 struct trace_buffer *buffer_b, int cpu)
5467 struct ring_buffer_per_cpu *cpu_buffer_a;
5468 struct ring_buffer_per_cpu *cpu_buffer_b;
5471 if (!cpumask_test_cpu(cpu, buffer_a->cpumask) ||
5472 !cpumask_test_cpu(cpu, buffer_b->cpumask))
5475 cpu_buffer_a = buffer_a->buffers[cpu];
5476 cpu_buffer_b = buffer_b->buffers[cpu];
5478 /* At least make sure the two buffers are somewhat the same */
5479 if (cpu_buffer_a->nr_pages != cpu_buffer_b->nr_pages)
5484 if (atomic_read(&buffer_a->record_disabled))
5487 if (atomic_read(&buffer_b->record_disabled))
5490 if (atomic_read(&cpu_buffer_a->record_disabled))
5493 if (atomic_read(&cpu_buffer_b->record_disabled))
5497 * We can't do a synchronize_rcu here because this
5498 * function can be called in atomic context.
5499 * Normally this will be called from the same CPU as cpu.
5500 * If not it's up to the caller to protect this.
5502 atomic_inc(&cpu_buffer_a->record_disabled);
5503 atomic_inc(&cpu_buffer_b->record_disabled);
5506 if (local_read(&cpu_buffer_a->committing))
5508 if (local_read(&cpu_buffer_b->committing))
5512 * When resize is in progress, we cannot swap it because
5513 * it will mess the state of the cpu buffer.
5515 if (atomic_read(&buffer_a->resizing))
5517 if (atomic_read(&buffer_b->resizing))
5520 buffer_a->buffers[cpu] = cpu_buffer_b;
5521 buffer_b->buffers[cpu] = cpu_buffer_a;
5523 cpu_buffer_b->buffer = buffer_a;
5524 cpu_buffer_a->buffer = buffer_b;
5529 atomic_dec(&cpu_buffer_a->record_disabled);
5530 atomic_dec(&cpu_buffer_b->record_disabled);
5534 EXPORT_SYMBOL_GPL(ring_buffer_swap_cpu);
5535 #endif /* CONFIG_RING_BUFFER_ALLOW_SWAP */
5538 * ring_buffer_alloc_read_page - allocate a page to read from buffer
5539 * @buffer: the buffer to allocate for.
5540 * @cpu: the cpu buffer to allocate.
5542 * This function is used in conjunction with ring_buffer_read_page.
5543 * When reading a full page from the ring buffer, these functions
5544 * can be used to speed up the process. The calling function should
5545 * allocate a few pages first with this function. Then when it
5546 * needs to get pages from the ring buffer, it passes the result
5547 * of this function into ring_buffer_read_page, which will swap
5548 * the page that was allocated, with the read page of the buffer.
5551 * The page allocated, or ERR_PTR
5553 void *ring_buffer_alloc_read_page(struct trace_buffer *buffer, int cpu)
5555 struct ring_buffer_per_cpu *cpu_buffer;
5556 struct buffer_data_page *bpage = NULL;
5557 unsigned long flags;
5560 if (!cpumask_test_cpu(cpu, buffer->cpumask))
5561 return ERR_PTR(-ENODEV);
5563 cpu_buffer = buffer->buffers[cpu];
5564 local_irq_save(flags);
5565 arch_spin_lock(&cpu_buffer->lock);
5567 if (cpu_buffer->free_page) {
5568 bpage = cpu_buffer->free_page;
5569 cpu_buffer->free_page = NULL;
5572 arch_spin_unlock(&cpu_buffer->lock);
5573 local_irq_restore(flags);
5578 page = alloc_pages_node(cpu_to_node(cpu),
5579 GFP_KERNEL | __GFP_NORETRY, 0);
5581 return ERR_PTR(-ENOMEM);
5583 bpage = page_address(page);
5586 rb_init_page(bpage);
5590 EXPORT_SYMBOL_GPL(ring_buffer_alloc_read_page);
5593 * ring_buffer_free_read_page - free an allocated read page
5594 * @buffer: the buffer the page was allocate for
5595 * @cpu: the cpu buffer the page came from
5596 * @data: the page to free
5598 * Free a page allocated from ring_buffer_alloc_read_page.
5600 void ring_buffer_free_read_page(struct trace_buffer *buffer, int cpu, void *data)
5602 struct ring_buffer_per_cpu *cpu_buffer;
5603 struct buffer_data_page *bpage = data;
5604 struct page *page = virt_to_page(bpage);
5605 unsigned long flags;
5607 if (!buffer || !buffer->buffers || !buffer->buffers[cpu])
5610 cpu_buffer = buffer->buffers[cpu];
5612 /* If the page is still in use someplace else, we can't reuse it */
5613 if (page_ref_count(page) > 1)
5616 local_irq_save(flags);
5617 arch_spin_lock(&cpu_buffer->lock);
5619 if (!cpu_buffer->free_page) {
5620 cpu_buffer->free_page = bpage;
5624 arch_spin_unlock(&cpu_buffer->lock);
5625 local_irq_restore(flags);
5628 free_page((unsigned long)bpage);
5630 EXPORT_SYMBOL_GPL(ring_buffer_free_read_page);
5633 * ring_buffer_read_page - extract a page from the ring buffer
5634 * @buffer: buffer to extract from
5635 * @data_page: the page to use allocated from ring_buffer_alloc_read_page
5636 * @len: amount to extract
5637 * @cpu: the cpu of the buffer to extract
5638 * @full: should the extraction only happen when the page is full.
5640 * This function will pull out a page from the ring buffer and consume it.
5641 * @data_page must be the address of the variable that was returned
5642 * from ring_buffer_alloc_read_page. This is because the page might be used
5643 * to swap with a page in the ring buffer.
5646 * rpage = ring_buffer_alloc_read_page(buffer, cpu);
5647 * if (IS_ERR(rpage))
5648 * return PTR_ERR(rpage);
5649 * ret = ring_buffer_read_page(buffer, &rpage, len, cpu, 0);
5651 * process_page(rpage, ret);
5653 * When @full is set, the function will not return true unless
5654 * the writer is off the reader page.
5656 * Note: it is up to the calling functions to handle sleeps and wakeups.
5657 * The ring buffer can be used anywhere in the kernel and can not
5658 * blindly call wake_up. The layer that uses the ring buffer must be
5659 * responsible for that.
5662 * >=0 if data has been transferred, returns the offset of consumed data.
5663 * <0 if no data has been transferred.
5665 int ring_buffer_read_page(struct trace_buffer *buffer,
5666 void **data_page, size_t len, int cpu, int full)
5668 struct ring_buffer_per_cpu *cpu_buffer = buffer->buffers[cpu];
5669 struct ring_buffer_event *event;
5670 struct buffer_data_page *bpage;
5671 struct buffer_page *reader;
5672 unsigned long missed_events;
5673 unsigned long flags;
5674 unsigned int commit;
5679 if (!cpumask_test_cpu(cpu, buffer->cpumask))
5683 * If len is not big enough to hold the page header, then
5684 * we can not copy anything.
5686 if (len <= BUF_PAGE_HDR_SIZE)
5689 len -= BUF_PAGE_HDR_SIZE;
5698 raw_spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
5700 reader = rb_get_reader_page(cpu_buffer);
5704 event = rb_reader_event(cpu_buffer);
5706 read = reader->read;
5707 commit = rb_page_commit(reader);
5709 /* Check if any events were dropped */
5710 missed_events = cpu_buffer->lost_events;
5713 * If this page has been partially read or
5714 * if len is not big enough to read the rest of the page or
5715 * a writer is still on the page, then
5716 * we must copy the data from the page to the buffer.
5717 * Otherwise, we can simply swap the page with the one passed in.
5719 if (read || (len < (commit - read)) ||
5720 cpu_buffer->reader_page == cpu_buffer->commit_page) {
5721 struct buffer_data_page *rpage = cpu_buffer->reader_page->page;
5722 unsigned int rpos = read;
5723 unsigned int pos = 0;
5727 * If a full page is expected, this can still be returned
5728 * if there's been a previous partial read and the
5729 * rest of the page can be read and the commit page is off
5733 (!read || (len < (commit - read)) ||
5734 cpu_buffer->reader_page == cpu_buffer->commit_page))
5737 if (len > (commit - read))
5738 len = (commit - read);
5740 /* Always keep the time extend and data together */
5741 size = rb_event_ts_length(event);
5746 /* save the current timestamp, since the user will need it */
5747 save_timestamp = cpu_buffer->read_stamp;
5749 /* Need to copy one event at a time */
5751 /* We need the size of one event, because
5752 * rb_advance_reader only advances by one event,
5753 * whereas rb_event_ts_length may include the size of
5754 * one or two events.
5755 * We have already ensured there's enough space if this
5756 * is a time extend. */
5757 size = rb_event_length(event);
5758 memcpy(bpage->data + pos, rpage->data + rpos, size);
5762 rb_advance_reader(cpu_buffer);
5763 rpos = reader->read;
5769 event = rb_reader_event(cpu_buffer);
5770 /* Always keep the time extend and data together */
5771 size = rb_event_ts_length(event);
5772 } while (len >= size);
5775 local_set(&bpage->commit, pos);
5776 bpage->time_stamp = save_timestamp;
5778 /* we copied everything to the beginning */
5781 /* update the entry counter */
5782 cpu_buffer->read += rb_page_entries(reader);
5783 cpu_buffer->read_bytes += rb_page_commit(reader);
5785 /* swap the pages */
5786 rb_init_page(bpage);
5787 bpage = reader->page;
5788 reader->page = *data_page;
5789 local_set(&reader->write, 0);
5790 local_set(&reader->entries, 0);
5795 * Use the real_end for the data size,
5796 * This gives us a chance to store the lost events
5799 if (reader->real_end)
5800 local_set(&bpage->commit, reader->real_end);
5804 cpu_buffer->lost_events = 0;
5806 commit = local_read(&bpage->commit);
5808 * Set a flag in the commit field if we lost events
5810 if (missed_events) {
5811 /* If there is room at the end of the page to save the
5812 * missed events, then record it there.
5814 if (BUF_PAGE_SIZE - commit >= sizeof(missed_events)) {
5815 memcpy(&bpage->data[commit], &missed_events,
5816 sizeof(missed_events));
5817 local_add(RB_MISSED_STORED, &bpage->commit);
5818 commit += sizeof(missed_events);
5820 local_add(RB_MISSED_EVENTS, &bpage->commit);
5824 * This page may be off to user land. Zero it out here.
5826 if (commit < BUF_PAGE_SIZE)
5827 memset(&bpage->data[commit], 0, BUF_PAGE_SIZE - commit);
5830 raw_spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
5835 EXPORT_SYMBOL_GPL(ring_buffer_read_page);
5838 * We only allocate new buffers, never free them if the CPU goes down.
5839 * If we were to free the buffer, then the user would lose any trace that was in
5842 int trace_rb_cpu_prepare(unsigned int cpu, struct hlist_node *node)
5844 struct trace_buffer *buffer;
5847 unsigned long nr_pages;
5849 buffer = container_of(node, struct trace_buffer, node);
5850 if (cpumask_test_cpu(cpu, buffer->cpumask))
5855 /* check if all cpu sizes are same */
5856 for_each_buffer_cpu(buffer, cpu_i) {
5857 /* fill in the size from first enabled cpu */
5859 nr_pages = buffer->buffers[cpu_i]->nr_pages;
5860 if (nr_pages != buffer->buffers[cpu_i]->nr_pages) {
5865 /* allocate minimum pages, user can later expand it */
5868 buffer->buffers[cpu] =
5869 rb_allocate_cpu_buffer(buffer, nr_pages, cpu);
5870 if (!buffer->buffers[cpu]) {
5871 WARN(1, "failed to allocate ring buffer on CPU %u\n",
5876 cpumask_set_cpu(cpu, buffer->cpumask);
5880 #ifdef CONFIG_RING_BUFFER_STARTUP_TEST
5882 * This is a basic integrity check of the ring buffer.
5883 * Late in the boot cycle this test will run when configured in.
5884 * It will kick off a thread per CPU that will go into a loop
5885 * writing to the per cpu ring buffer various sizes of data.
5886 * Some of the data will be large items, some small.
5888 * Another thread is created that goes into a spin, sending out
5889 * IPIs to the other CPUs to also write into the ring buffer.
5890 * this is to test the nesting ability of the buffer.
5892 * Basic stats are recorded and reported. If something in the
5893 * ring buffer should happen that's not expected, a big warning
5894 * is displayed and all ring buffers are disabled.
5896 static struct task_struct *rb_threads[NR_CPUS] __initdata;
5898 struct rb_test_data {
5899 struct trace_buffer *buffer;
5900 unsigned long events;
5901 unsigned long bytes_written;
5902 unsigned long bytes_alloc;
5903 unsigned long bytes_dropped;
5904 unsigned long events_nested;
5905 unsigned long bytes_written_nested;
5906 unsigned long bytes_alloc_nested;
5907 unsigned long bytes_dropped_nested;
5908 int min_size_nested;
5909 int max_size_nested;
5916 static struct rb_test_data rb_data[NR_CPUS] __initdata;
5919 #define RB_TEST_BUFFER_SIZE 1048576
5921 static char rb_string[] __initdata =
5922 "abcdefghijklmnopqrstuvwxyz1234567890!@#$%^&*()?+\\"
5923 "?+|:';\",.<>/?abcdefghijklmnopqrstuvwxyz1234567890"
5924 "!@#$%^&*()?+\\?+|:';\",.<>/?abcdefghijklmnopqrstuv";
5926 static bool rb_test_started __initdata;
5933 static __init int rb_write_something(struct rb_test_data *data, bool nested)
5935 struct ring_buffer_event *event;
5936 struct rb_item *item;
5943 /* Have nested writes different that what is written */
5944 cnt = data->cnt + (nested ? 27 : 0);
5946 /* Multiply cnt by ~e, to make some unique increment */
5947 size = (cnt * 68 / 25) % (sizeof(rb_string) - 1);
5949 len = size + sizeof(struct rb_item);
5951 started = rb_test_started;
5952 /* read rb_test_started before checking buffer enabled */
5955 event = ring_buffer_lock_reserve(data->buffer, len);
5957 /* Ignore dropped events before test starts. */
5960 data->bytes_dropped += len;
5962 data->bytes_dropped_nested += len;
5967 event_len = ring_buffer_event_length(event);
5969 if (RB_WARN_ON(data->buffer, event_len < len))
5972 item = ring_buffer_event_data(event);
5974 memcpy(item->str, rb_string, size);
5977 data->bytes_alloc_nested += event_len;
5978 data->bytes_written_nested += len;
5979 data->events_nested++;
5980 if (!data->min_size_nested || len < data->min_size_nested)
5981 data->min_size_nested = len;
5982 if (len > data->max_size_nested)
5983 data->max_size_nested = len;
5985 data->bytes_alloc += event_len;
5986 data->bytes_written += len;
5988 if (!data->min_size || len < data->min_size)
5989 data->max_size = len;
5990 if (len > data->max_size)
5991 data->max_size = len;
5995 ring_buffer_unlock_commit(data->buffer, event);
6000 static __init int rb_test(void *arg)
6002 struct rb_test_data *data = arg;
6004 while (!kthread_should_stop()) {
6005 rb_write_something(data, false);
6008 set_current_state(TASK_INTERRUPTIBLE);
6009 /* Now sleep between a min of 100-300us and a max of 1ms */
6010 usleep_range(((data->cnt % 3) + 1) * 100, 1000);
6016 static __init void rb_ipi(void *ignore)
6018 struct rb_test_data *data;
6019 int cpu = smp_processor_id();
6021 data = &rb_data[cpu];
6022 rb_write_something(data, true);
6025 static __init int rb_hammer_test(void *arg)
6027 while (!kthread_should_stop()) {
6029 /* Send an IPI to all cpus to write data! */
6030 smp_call_function(rb_ipi, NULL, 1);
6031 /* No sleep, but for non preempt, let others run */
6038 static __init int test_ringbuffer(void)
6040 struct task_struct *rb_hammer;
6041 struct trace_buffer *buffer;
6045 if (security_locked_down(LOCKDOWN_TRACEFS)) {
6046 pr_warn("Lockdown is enabled, skipping ring buffer tests\n");
6050 pr_info("Running ring buffer tests...\n");
6052 buffer = ring_buffer_alloc(RB_TEST_BUFFER_SIZE, RB_FL_OVERWRITE);
6053 if (WARN_ON(!buffer))
6056 /* Disable buffer so that threads can't write to it yet */
6057 ring_buffer_record_off(buffer);
6059 for_each_online_cpu(cpu) {
6060 rb_data[cpu].buffer = buffer;
6061 rb_data[cpu].cpu = cpu;
6062 rb_data[cpu].cnt = cpu;
6063 rb_threads[cpu] = kthread_create(rb_test, &rb_data[cpu],
6064 "rbtester/%d", cpu);
6065 if (WARN_ON(IS_ERR(rb_threads[cpu]))) {
6066 pr_cont("FAILED\n");
6067 ret = PTR_ERR(rb_threads[cpu]);
6071 kthread_bind(rb_threads[cpu], cpu);
6072 wake_up_process(rb_threads[cpu]);
6075 /* Now create the rb hammer! */
6076 rb_hammer = kthread_run(rb_hammer_test, NULL, "rbhammer");
6077 if (WARN_ON(IS_ERR(rb_hammer))) {
6078 pr_cont("FAILED\n");
6079 ret = PTR_ERR(rb_hammer);
6083 ring_buffer_record_on(buffer);
6085 * Show buffer is enabled before setting rb_test_started.
6086 * Yes there's a small race window where events could be
6087 * dropped and the thread wont catch it. But when a ring
6088 * buffer gets enabled, there will always be some kind of
6089 * delay before other CPUs see it. Thus, we don't care about
6090 * those dropped events. We care about events dropped after
6091 * the threads see that the buffer is active.
6094 rb_test_started = true;
6096 set_current_state(TASK_INTERRUPTIBLE);
6097 /* Just run for 10 seconds */;
6098 schedule_timeout(10 * HZ);
6100 kthread_stop(rb_hammer);
6103 for_each_online_cpu(cpu) {
6104 if (!rb_threads[cpu])
6106 kthread_stop(rb_threads[cpu]);
6109 ring_buffer_free(buffer);
6114 pr_info("finished\n");
6115 for_each_online_cpu(cpu) {
6116 struct ring_buffer_event *event;
6117 struct rb_test_data *data = &rb_data[cpu];
6118 struct rb_item *item;
6119 unsigned long total_events;
6120 unsigned long total_dropped;
6121 unsigned long total_written;
6122 unsigned long total_alloc;
6123 unsigned long total_read = 0;
6124 unsigned long total_size = 0;
6125 unsigned long total_len = 0;
6126 unsigned long total_lost = 0;
6129 int small_event_size;
6133 total_events = data->events + data->events_nested;
6134 total_written = data->bytes_written + data->bytes_written_nested;
6135 total_alloc = data->bytes_alloc + data->bytes_alloc_nested;
6136 total_dropped = data->bytes_dropped + data->bytes_dropped_nested;
6138 big_event_size = data->max_size + data->max_size_nested;
6139 small_event_size = data->min_size + data->min_size_nested;
6141 pr_info("CPU %d:\n", cpu);
6142 pr_info(" events: %ld\n", total_events);
6143 pr_info(" dropped bytes: %ld\n", total_dropped);
6144 pr_info(" alloced bytes: %ld\n", total_alloc);
6145 pr_info(" written bytes: %ld\n", total_written);
6146 pr_info(" biggest event: %d\n", big_event_size);
6147 pr_info(" smallest event: %d\n", small_event_size);
6149 if (RB_WARN_ON(buffer, total_dropped))
6154 while ((event = ring_buffer_consume(buffer, cpu, NULL, &lost))) {
6156 item = ring_buffer_event_data(event);
6157 total_len += ring_buffer_event_length(event);
6158 total_size += item->size + sizeof(struct rb_item);
6159 if (memcmp(&item->str[0], rb_string, item->size) != 0) {
6160 pr_info("FAILED!\n");
6161 pr_info("buffer had: %.*s\n", item->size, item->str);
6162 pr_info("expected: %.*s\n", item->size, rb_string);
6163 RB_WARN_ON(buffer, 1);
6174 pr_info(" read events: %ld\n", total_read);
6175 pr_info(" lost events: %ld\n", total_lost);
6176 pr_info(" total events: %ld\n", total_lost + total_read);
6177 pr_info(" recorded len bytes: %ld\n", total_len);
6178 pr_info(" recorded size bytes: %ld\n", total_size);
6180 pr_info(" With dropped events, record len and size may not match\n"
6181 " alloced and written from above\n");
6183 if (RB_WARN_ON(buffer, total_len != total_alloc ||
6184 total_size != total_written))
6187 if (RB_WARN_ON(buffer, total_lost + total_read != total_events))
6193 pr_info("Ring buffer PASSED!\n");
6195 ring_buffer_free(buffer);
6199 late_initcall(test_ringbuffer);
6200 #endif /* CONFIG_RING_BUFFER_STARTUP_TEST */