1 // SPDX-License-Identifier: GPL-2.0
5 * Copyright (C) 2008 Steven Rostedt <srostedt@redhat.com>
7 #include <linux/trace_events.h>
8 #include <linux/ring_buffer.h>
9 #include <linux/trace_clock.h>
10 #include <linux/sched/clock.h>
11 #include <linux/trace_seq.h>
12 #include <linux/spinlock.h>
13 #include <linux/irq_work.h>
14 #include <linux/security.h>
15 #include <linux/uaccess.h>
16 #include <linux/hardirq.h>
17 #include <linux/kthread.h> /* for self test */
18 #include <linux/module.h>
19 #include <linux/percpu.h>
20 #include <linux/mutex.h>
21 #include <linux/delay.h>
22 #include <linux/slab.h>
23 #include <linux/init.h>
24 #include <linux/hash.h>
25 #include <linux/list.h>
26 #include <linux/cpu.h>
27 #include <linux/oom.h>
29 #include <asm/local.h>
31 static void update_pages_handler(struct work_struct *work);
34 * The ring buffer header is special. We must manually up keep it.
36 int ring_buffer_print_entry_header(struct trace_seq *s)
38 trace_seq_puts(s, "# compressed entry header\n");
39 trace_seq_puts(s, "\ttype_len : 5 bits\n");
40 trace_seq_puts(s, "\ttime_delta : 27 bits\n");
41 trace_seq_puts(s, "\tarray : 32 bits\n");
42 trace_seq_putc(s, '\n');
43 trace_seq_printf(s, "\tpadding : type == %d\n",
44 RINGBUF_TYPE_PADDING);
45 trace_seq_printf(s, "\ttime_extend : type == %d\n",
46 RINGBUF_TYPE_TIME_EXTEND);
47 trace_seq_printf(s, "\ttime_stamp : type == %d\n",
48 RINGBUF_TYPE_TIME_STAMP);
49 trace_seq_printf(s, "\tdata max type_len == %d\n",
50 RINGBUF_TYPE_DATA_TYPE_LEN_MAX);
52 return !trace_seq_has_overflowed(s);
56 * The ring buffer is made up of a list of pages. A separate list of pages is
57 * allocated for each CPU. A writer may only write to a buffer that is
58 * associated with the CPU it is currently executing on. A reader may read
59 * from any per cpu buffer.
61 * The reader is special. For each per cpu buffer, the reader has its own
62 * reader page. When a reader has read the entire reader page, this reader
63 * page is swapped with another page in the ring buffer.
65 * Now, as long as the writer is off the reader page, the reader can do what
66 * ever it wants with that page. The writer will never write to that page
67 * again (as long as it is out of the ring buffer).
69 * Here's some silly ASCII art.
72 * |reader| RING BUFFER
74 * +------+ +---+ +---+ +---+
83 * |reader| RING BUFFER
84 * |page |------------------v
85 * +------+ +---+ +---+ +---+
94 * |reader| RING BUFFER
95 * |page |------------------v
96 * +------+ +---+ +---+ +---+
101 * +------------------------------+
105 * |buffer| RING BUFFER
106 * |page |------------------v
107 * +------+ +---+ +---+ +---+
109 * | New +---+ +---+ +---+
112 * +------------------------------+
115 * After we make this swap, the reader can hand this page off to the splice
116 * code and be done with it. It can even allocate a new page if it needs to
117 * and swap that into the ring buffer.
119 * We will be using cmpxchg soon to make all this lockless.
123 /* Used for individual buffers (after the counter) */
124 #define RB_BUFFER_OFF (1 << 20)
126 #define BUF_PAGE_HDR_SIZE offsetof(struct buffer_data_page, data)
128 #define RB_EVNT_HDR_SIZE (offsetof(struct ring_buffer_event, array))
129 #define RB_ALIGNMENT 4U
130 #define RB_MAX_SMALL_DATA (RB_ALIGNMENT * RINGBUF_TYPE_DATA_TYPE_LEN_MAX)
131 #define RB_EVNT_MIN_SIZE 8U /* two 32bit words */
132 #define RB_ALIGN_DATA __aligned(RB_ALIGNMENT)
134 /* define RINGBUF_TYPE_DATA for 'case RINGBUF_TYPE_DATA:' */
135 #define RINGBUF_TYPE_DATA 0 ... RINGBUF_TYPE_DATA_TYPE_LEN_MAX
138 RB_LEN_TIME_EXTEND = 8,
139 RB_LEN_TIME_STAMP = 8,
142 #define skip_time_extend(event) \
143 ((struct ring_buffer_event *)((char *)event + RB_LEN_TIME_EXTEND))
145 #define extended_time(event) \
146 (event->type_len >= RINGBUF_TYPE_TIME_EXTEND)
148 static inline int rb_null_event(struct ring_buffer_event *event)
150 return event->type_len == RINGBUF_TYPE_PADDING && !event->time_delta;
153 static void rb_event_set_padding(struct ring_buffer_event *event)
155 /* padding has a NULL time_delta */
156 event->type_len = RINGBUF_TYPE_PADDING;
157 event->time_delta = 0;
161 rb_event_data_length(struct ring_buffer_event *event)
166 length = event->type_len * RB_ALIGNMENT;
168 length = event->array[0];
169 return length + RB_EVNT_HDR_SIZE;
173 * Return the length of the given event. Will return
174 * the length of the time extend if the event is a
177 static inline unsigned
178 rb_event_length(struct ring_buffer_event *event)
180 switch (event->type_len) {
181 case RINGBUF_TYPE_PADDING:
182 if (rb_null_event(event))
185 return event->array[0] + RB_EVNT_HDR_SIZE;
187 case RINGBUF_TYPE_TIME_EXTEND:
188 return RB_LEN_TIME_EXTEND;
190 case RINGBUF_TYPE_TIME_STAMP:
191 return RB_LEN_TIME_STAMP;
193 case RINGBUF_TYPE_DATA:
194 return rb_event_data_length(event);
203 * Return total length of time extend and data,
204 * or just the event length for all other events.
206 static inline unsigned
207 rb_event_ts_length(struct ring_buffer_event *event)
211 if (extended_time(event)) {
212 /* time extends include the data event after it */
213 len = RB_LEN_TIME_EXTEND;
214 event = skip_time_extend(event);
216 return len + rb_event_length(event);
220 * ring_buffer_event_length - return the length of the event
221 * @event: the event to get the length of
223 * Returns the size of the data load of a data event.
224 * If the event is something other than a data event, it
225 * returns the size of the event itself. With the exception
226 * of a TIME EXTEND, where it still returns the size of the
227 * data load of the data event after it.
229 unsigned ring_buffer_event_length(struct ring_buffer_event *event)
233 if (extended_time(event))
234 event = skip_time_extend(event);
236 length = rb_event_length(event);
237 if (event->type_len > RINGBUF_TYPE_DATA_TYPE_LEN_MAX)
239 length -= RB_EVNT_HDR_SIZE;
240 if (length > RB_MAX_SMALL_DATA + sizeof(event->array[0]))
241 length -= sizeof(event->array[0]);
244 EXPORT_SYMBOL_GPL(ring_buffer_event_length);
246 /* inline for ring buffer fast paths */
247 static __always_inline void *
248 rb_event_data(struct ring_buffer_event *event)
250 if (extended_time(event))
251 event = skip_time_extend(event);
252 WARN_ON_ONCE(event->type_len > RINGBUF_TYPE_DATA_TYPE_LEN_MAX);
253 /* If length is in len field, then array[0] has the data */
255 return (void *)&event->array[0];
256 /* Otherwise length is in array[0] and array[1] has the data */
257 return (void *)&event->array[1];
261 * ring_buffer_event_data - return the data of the event
262 * @event: the event to get the data from
264 void *ring_buffer_event_data(struct ring_buffer_event *event)
266 return rb_event_data(event);
268 EXPORT_SYMBOL_GPL(ring_buffer_event_data);
270 #define for_each_buffer_cpu(buffer, cpu) \
271 for_each_cpu(cpu, buffer->cpumask)
273 #define for_each_online_buffer_cpu(buffer, cpu) \
274 for_each_cpu_and(cpu, buffer->cpumask, cpu_online_mask)
277 #define TS_MASK ((1ULL << TS_SHIFT) - 1)
278 #define TS_DELTA_TEST (~TS_MASK)
281 * ring_buffer_event_time_stamp - return the event's extended timestamp
282 * @event: the event to get the timestamp of
284 * Returns the extended timestamp associated with a data event.
285 * An extended time_stamp is a 64-bit timestamp represented
286 * internally in a special way that makes the best use of space
287 * contained within a ring buffer event. This function decodes
288 * it and maps it to a straight u64 value.
290 u64 ring_buffer_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);
350 * Also stolen from mm/slob.c. Thanks to Mathieu Desnoyers for pointing
353 static void free_buffer_page(struct buffer_page *bpage)
355 free_page((unsigned long)bpage->page);
360 * We need to fit the time_stamp delta into 27 bits.
362 static inline int test_time_stamp(u64 delta)
364 if (delta & TS_DELTA_TEST)
369 #define BUF_PAGE_SIZE (PAGE_SIZE - BUF_PAGE_HDR_SIZE)
371 /* Max payload is BUF_PAGE_SIZE - header (8bytes) */
372 #define BUF_MAX_DATA_SIZE (BUF_PAGE_SIZE - (sizeof(u32) * 2))
374 int ring_buffer_print_page_header(struct trace_seq *s)
376 struct buffer_data_page field;
378 trace_seq_printf(s, "\tfield: u64 timestamp;\t"
379 "offset:0;\tsize:%u;\tsigned:%u;\n",
380 (unsigned int)sizeof(field.time_stamp),
381 (unsigned int)is_signed_type(u64));
383 trace_seq_printf(s, "\tfield: local_t commit;\t"
384 "offset:%u;\tsize:%u;\tsigned:%u;\n",
385 (unsigned int)offsetof(typeof(field), commit),
386 (unsigned int)sizeof(field.commit),
387 (unsigned int)is_signed_type(long));
389 trace_seq_printf(s, "\tfield: int overwrite;\t"
390 "offset:%u;\tsize:%u;\tsigned:%u;\n",
391 (unsigned int)offsetof(typeof(field), commit),
393 (unsigned int)is_signed_type(long));
395 trace_seq_printf(s, "\tfield: char data;\t"
396 "offset:%u;\tsize:%u;\tsigned:%u;\n",
397 (unsigned int)offsetof(typeof(field), data),
398 (unsigned int)BUF_PAGE_SIZE,
399 (unsigned int)is_signed_type(char));
401 return !trace_seq_has_overflowed(s);
405 struct irq_work work;
406 wait_queue_head_t waiters;
407 wait_queue_head_t full_waiters;
408 bool waiters_pending;
409 bool full_waiters_pending;
414 * Structure to hold event state and handle nested events.
416 struct rb_event_info {
421 unsigned long length;
422 struct buffer_page *tail_page;
427 * Used for the add_timestamp
429 * EXTEND - wants a time extend
430 * ABSOLUTE - the buffer requests all events to have absolute time stamps
431 * FORCE - force a full time stamp.
434 RB_ADD_STAMP_NONE = 0,
435 RB_ADD_STAMP_EXTEND = BIT(1),
436 RB_ADD_STAMP_ABSOLUTE = BIT(2),
437 RB_ADD_STAMP_FORCE = BIT(3)
440 * Used for which event context the event is in.
447 * See trace_recursive_lock() comment below for more details.
458 #if BITS_PER_LONG == 32
462 /* To test on 64 bit machines */
467 struct rb_time_struct {
473 #include <asm/local64.h>
474 struct rb_time_struct {
478 typedef struct rb_time_struct rb_time_t;
481 * head_page == tail_page && head == tail then buffer is empty.
483 struct ring_buffer_per_cpu {
485 atomic_t record_disabled;
486 atomic_t resize_disabled;
487 struct trace_buffer *buffer;
488 raw_spinlock_t reader_lock; /* serialize readers */
489 arch_spinlock_t lock;
490 struct lock_class_key lock_key;
491 struct buffer_data_page *free_page;
492 unsigned long nr_pages;
493 unsigned int current_context;
494 struct list_head *pages;
495 struct buffer_page *head_page; /* read from head */
496 struct buffer_page *tail_page; /* write to tail */
497 struct buffer_page *commit_page; /* committed pages */
498 struct buffer_page *reader_page;
499 unsigned long lost_events;
500 unsigned long last_overrun;
502 local_t entries_bytes;
505 local_t commit_overrun;
506 local_t dropped_events;
509 local_t pages_touched;
511 long last_pages_touch;
512 size_t shortest_full;
514 unsigned long read_bytes;
515 rb_time_t write_stamp;
516 rb_time_t before_stamp;
518 /* ring buffer pages to update, > 0 to add, < 0 to remove */
519 long nr_pages_to_update;
520 struct list_head new_pages; /* new pages to add */
521 struct work_struct update_pages_work;
522 struct completion update_done;
524 struct rb_irq_work irq_work;
527 struct trace_buffer {
530 atomic_t record_disabled;
531 cpumask_var_t cpumask;
533 struct lock_class_key *reader_lock_key;
537 struct ring_buffer_per_cpu **buffers;
539 struct hlist_node node;
542 struct rb_irq_work irq_work;
546 struct ring_buffer_iter {
547 struct ring_buffer_per_cpu *cpu_buffer;
549 unsigned long next_event;
550 struct buffer_page *head_page;
551 struct buffer_page *cache_reader_page;
552 unsigned long cache_read;
555 struct ring_buffer_event *event;
562 * On 32 bit machines, local64_t is very expensive. As the ring
563 * buffer doesn't need all the features of a true 64 bit atomic,
564 * on 32 bit, it uses these functions (64 still uses local64_t).
566 * For the ring buffer, 64 bit required operations for the time is
569 * - Only need 59 bits (uses 60 to make it even).
570 * - Reads may fail if it interrupted a modification of the time stamp.
571 * It will succeed if it did not interrupt another write even if
572 * the read itself is interrupted by a write.
573 * It returns whether it was successful or not.
575 * - Writes always succeed and will overwrite other writes and writes
576 * that were done by events interrupting the current write.
578 * - A write followed by a read of the same time stamp will always succeed,
579 * but may not contain the same value.
581 * - A cmpxchg will fail if it interrupted another write or cmpxchg.
582 * Other than that, it acts like a normal cmpxchg.
584 * The 60 bit time stamp is broken up by 30 bits in a top and bottom half
585 * (bottom being the least significant 30 bits of the 60 bit time stamp).
587 * The two most significant bits of each half holds a 2 bit counter (0-3).
588 * Each update will increment this counter by one.
589 * When reading the top and bottom, if the two counter bits match then the
590 * top and bottom together make a valid 60 bit number.
592 #define RB_TIME_SHIFT 30
593 #define RB_TIME_VAL_MASK ((1 << RB_TIME_SHIFT) - 1)
595 static inline int rb_time_cnt(unsigned long val)
597 return (val >> RB_TIME_SHIFT) & 3;
600 static inline u64 rb_time_val(unsigned long top, unsigned long bottom)
604 val = top & RB_TIME_VAL_MASK;
605 val <<= RB_TIME_SHIFT;
606 val |= bottom & RB_TIME_VAL_MASK;
611 static inline bool __rb_time_read(rb_time_t *t, u64 *ret, unsigned long *cnt)
613 unsigned long top, bottom;
617 * If the read is interrupted by a write, then the cnt will
618 * be different. Loop until both top and bottom have been read
619 * without interruption.
622 c = local_read(&t->cnt);
623 top = local_read(&t->top);
624 bottom = local_read(&t->bottom);
625 } while (c != local_read(&t->cnt));
627 *cnt = rb_time_cnt(top);
629 /* If top and bottom counts don't match, this interrupted a write */
630 if (*cnt != rb_time_cnt(bottom))
633 *ret = rb_time_val(top, bottom);
637 static bool rb_time_read(rb_time_t *t, u64 *ret)
641 return __rb_time_read(t, ret, &cnt);
644 static inline unsigned long rb_time_val_cnt(unsigned long val, unsigned long cnt)
646 return (val & RB_TIME_VAL_MASK) | ((cnt & 3) << RB_TIME_SHIFT);
649 static inline void rb_time_split(u64 val, unsigned long *top, unsigned long *bottom)
651 *top = (unsigned long)((val >> RB_TIME_SHIFT) & RB_TIME_VAL_MASK);
652 *bottom = (unsigned long)(val & RB_TIME_VAL_MASK);
655 static inline void rb_time_val_set(local_t *t, unsigned long val, unsigned long cnt)
657 val = rb_time_val_cnt(val, cnt);
661 static void rb_time_set(rb_time_t *t, u64 val)
663 unsigned long cnt, top, bottom;
665 rb_time_split(val, &top, &bottom);
667 /* Writes always succeed with a valid number even if it gets interrupted. */
669 cnt = local_inc_return(&t->cnt);
670 rb_time_val_set(&t->top, top, cnt);
671 rb_time_val_set(&t->bottom, bottom, cnt);
672 } while (cnt != local_read(&t->cnt));
676 rb_time_read_cmpxchg(local_t *l, unsigned long expect, unsigned long set)
680 ret = local_cmpxchg(l, expect, set);
681 return ret == expect;
684 static int rb_time_cmpxchg(rb_time_t *t, u64 expect, u64 set)
686 unsigned long cnt, top, bottom;
687 unsigned long cnt2, top2, bottom2;
690 /* The cmpxchg always fails if it interrupted an update */
691 if (!__rb_time_read(t, &val, &cnt2))
697 cnt = local_read(&t->cnt);
698 if ((cnt & 3) != cnt2)
703 rb_time_split(val, &top, &bottom);
704 top = rb_time_val_cnt(top, cnt);
705 bottom = rb_time_val_cnt(bottom, cnt);
707 rb_time_split(set, &top2, &bottom2);
708 top2 = rb_time_val_cnt(top2, cnt2);
709 bottom2 = rb_time_val_cnt(bottom2, cnt2);
711 if (!rb_time_read_cmpxchg(&t->cnt, cnt, cnt2))
713 if (!rb_time_read_cmpxchg(&t->top, top, top2))
715 if (!rb_time_read_cmpxchg(&t->bottom, bottom, bottom2))
722 /* local64_t always succeeds */
724 static inline bool rb_time_read(rb_time_t *t, u64 *ret)
726 *ret = local64_read(&t->time);
729 static void rb_time_set(rb_time_t *t, u64 val)
731 local64_set(&t->time, val);
734 static bool rb_time_cmpxchg(rb_time_t *t, u64 expect, u64 set)
737 val = local64_cmpxchg(&t->time, expect, set);
738 return val == expect;
743 * ring_buffer_nr_pages - get the number of buffer pages in the ring buffer
744 * @buffer: The ring_buffer to get the number of pages from
745 * @cpu: The cpu of the ring_buffer to get the number of pages from
747 * Returns the number of pages used by a per_cpu buffer of the ring buffer.
749 size_t ring_buffer_nr_pages(struct trace_buffer *buffer, int cpu)
751 return buffer->buffers[cpu]->nr_pages;
755 * ring_buffer_nr_pages_dirty - get the number of used pages in the ring buffer
756 * @buffer: The ring_buffer to get the number of pages from
757 * @cpu: The cpu of the ring_buffer to get the number of pages from
759 * Returns the number of pages that have content in the ring buffer.
761 size_t ring_buffer_nr_dirty_pages(struct trace_buffer *buffer, int cpu)
766 read = local_read(&buffer->buffers[cpu]->pages_read);
767 cnt = local_read(&buffer->buffers[cpu]->pages_touched);
768 /* The reader can read an empty page, but not more than that */
770 WARN_ON_ONCE(read > cnt + 1);
778 * rb_wake_up_waiters - wake up tasks waiting for ring buffer input
780 * Schedules a delayed work to wake up any task that is blocked on the
781 * ring buffer waiters queue.
783 static void rb_wake_up_waiters(struct irq_work *work)
785 struct rb_irq_work *rbwork = container_of(work, struct rb_irq_work, work);
787 wake_up_all(&rbwork->waiters);
788 if (rbwork->wakeup_full) {
789 rbwork->wakeup_full = false;
790 wake_up_all(&rbwork->full_waiters);
795 * ring_buffer_wait - wait for input to the ring buffer
796 * @buffer: buffer to wait on
797 * @cpu: the cpu buffer to wait on
798 * @full: wait until the percentage of pages are available, if @cpu != RING_BUFFER_ALL_CPUS
800 * If @cpu == RING_BUFFER_ALL_CPUS then the task will wake up as soon
801 * as data is added to any of the @buffer's cpu buffers. Otherwise
802 * it will wait for data to be added to a specific cpu buffer.
804 int ring_buffer_wait(struct trace_buffer *buffer, int cpu, int full)
806 struct ring_buffer_per_cpu *cpu_buffer;
808 struct rb_irq_work *work;
812 * Depending on what the caller is waiting for, either any
813 * data in any cpu buffer, or a specific buffer, put the
814 * caller on the appropriate wait queue.
816 if (cpu == RING_BUFFER_ALL_CPUS) {
817 work = &buffer->irq_work;
818 /* Full only makes sense on per cpu reads */
821 if (!cpumask_test_cpu(cpu, buffer->cpumask))
823 cpu_buffer = buffer->buffers[cpu];
824 work = &cpu_buffer->irq_work;
830 prepare_to_wait(&work->full_waiters, &wait, TASK_INTERRUPTIBLE);
832 prepare_to_wait(&work->waiters, &wait, TASK_INTERRUPTIBLE);
835 * The events can happen in critical sections where
836 * checking a work queue can cause deadlocks.
837 * After adding a task to the queue, this flag is set
838 * only to notify events to try to wake up the queue
841 * We don't clear it even if the buffer is no longer
842 * empty. The flag only causes the next event to run
843 * irq_work to do the work queue wake up. The worse
844 * that can happen if we race with !trace_empty() is that
845 * an event will cause an irq_work to try to wake up
848 * There's no reason to protect this flag either, as
849 * the work queue and irq_work logic will do the necessary
850 * synchronization for the wake ups. The only thing
851 * that is necessary is that the wake up happens after
852 * a task has been queued. It's OK for spurious wake ups.
855 work->full_waiters_pending = true;
857 work->waiters_pending = true;
859 if (signal_pending(current)) {
864 if (cpu == RING_BUFFER_ALL_CPUS && !ring_buffer_empty(buffer))
867 if (cpu != RING_BUFFER_ALL_CPUS &&
868 !ring_buffer_empty_cpu(buffer, cpu)) {
877 raw_spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
878 pagebusy = cpu_buffer->reader_page == cpu_buffer->commit_page;
879 nr_pages = cpu_buffer->nr_pages;
880 dirty = ring_buffer_nr_dirty_pages(buffer, cpu);
881 if (!cpu_buffer->shortest_full ||
882 cpu_buffer->shortest_full < full)
883 cpu_buffer->shortest_full = full;
884 raw_spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
886 (!nr_pages || (dirty * 100) > full * nr_pages))
894 finish_wait(&work->full_waiters, &wait);
896 finish_wait(&work->waiters, &wait);
902 * ring_buffer_poll_wait - poll on buffer input
903 * @buffer: buffer to wait on
904 * @cpu: the cpu buffer to wait on
905 * @filp: the file descriptor
906 * @poll_table: The poll descriptor
908 * If @cpu == RING_BUFFER_ALL_CPUS then the task will wake up as soon
909 * as data is added to any of the @buffer's cpu buffers. Otherwise
910 * it will wait for data to be added to a specific cpu buffer.
912 * Returns EPOLLIN | EPOLLRDNORM if data exists in the buffers,
915 __poll_t ring_buffer_poll_wait(struct trace_buffer *buffer, int cpu,
916 struct file *filp, poll_table *poll_table)
918 struct ring_buffer_per_cpu *cpu_buffer;
919 struct rb_irq_work *work;
921 if (cpu == RING_BUFFER_ALL_CPUS)
922 work = &buffer->irq_work;
924 if (!cpumask_test_cpu(cpu, buffer->cpumask))
927 cpu_buffer = buffer->buffers[cpu];
928 work = &cpu_buffer->irq_work;
931 poll_wait(filp, &work->waiters, poll_table);
932 work->waiters_pending = true;
934 * There's a tight race between setting the waiters_pending and
935 * checking if the ring buffer is empty. Once the waiters_pending bit
936 * is set, the next event will wake the task up, but we can get stuck
937 * if there's only a single event in.
939 * FIXME: Ideally, we need a memory barrier on the writer side as well,
940 * but adding a memory barrier to all events will cause too much of a
941 * performance hit in the fast path. We only need a memory barrier when
942 * the buffer goes from empty to having content. But as this race is
943 * extremely small, and it's not a problem if another event comes in, we
948 if ((cpu == RING_BUFFER_ALL_CPUS && !ring_buffer_empty(buffer)) ||
949 (cpu != RING_BUFFER_ALL_CPUS && !ring_buffer_empty_cpu(buffer, cpu)))
950 return EPOLLIN | EPOLLRDNORM;
954 /* buffer may be either ring_buffer or ring_buffer_per_cpu */
955 #define RB_WARN_ON(b, cond) \
957 int _____ret = unlikely(cond); \
959 if (__same_type(*(b), struct ring_buffer_per_cpu)) { \
960 struct ring_buffer_per_cpu *__b = \
962 atomic_inc(&__b->buffer->record_disabled); \
964 atomic_inc(&b->record_disabled); \
970 /* Up this if you want to test the TIME_EXTENTS and normalization */
971 #define DEBUG_SHIFT 0
973 static inline u64 rb_time_stamp(struct trace_buffer *buffer)
977 /* Skip retpolines :-( */
978 if (IS_ENABLED(CONFIG_RETPOLINE) && likely(buffer->clock == trace_clock_local))
979 ts = trace_clock_local();
981 ts = buffer->clock();
983 /* shift to debug/test normalization and TIME_EXTENTS */
984 return ts << DEBUG_SHIFT;
987 u64 ring_buffer_time_stamp(struct trace_buffer *buffer, int cpu)
991 preempt_disable_notrace();
992 time = rb_time_stamp(buffer);
993 preempt_enable_notrace();
997 EXPORT_SYMBOL_GPL(ring_buffer_time_stamp);
999 void ring_buffer_normalize_time_stamp(struct trace_buffer *buffer,
1002 /* Just stupid testing the normalize function and deltas */
1003 *ts >>= DEBUG_SHIFT;
1005 EXPORT_SYMBOL_GPL(ring_buffer_normalize_time_stamp);
1008 * Making the ring buffer lockless makes things tricky.
1009 * Although writes only happen on the CPU that they are on,
1010 * and they only need to worry about interrupts. Reads can
1011 * happen on any CPU.
1013 * The reader page is always off the ring buffer, but when the
1014 * reader finishes with a page, it needs to swap its page with
1015 * a new one from the buffer. The reader needs to take from
1016 * the head (writes go to the tail). But if a writer is in overwrite
1017 * mode and wraps, it must push the head page forward.
1019 * Here lies the problem.
1021 * The reader must be careful to replace only the head page, and
1022 * not another one. As described at the top of the file in the
1023 * ASCII art, the reader sets its old page to point to the next
1024 * page after head. It then sets the page after head to point to
1025 * the old reader page. But if the writer moves the head page
1026 * during this operation, the reader could end up with the tail.
1028 * We use cmpxchg to help prevent this race. We also do something
1029 * special with the page before head. We set the LSB to 1.
1031 * When the writer must push the page forward, it will clear the
1032 * bit that points to the head page, move the head, and then set
1033 * the bit that points to the new head page.
1035 * We also don't want an interrupt coming in and moving the head
1036 * page on another writer. Thus we use the second LSB to catch
1039 * head->list->prev->next bit 1 bit 0
1042 * Points to head page 0 1
1045 * Note we can not trust the prev pointer of the head page, because:
1047 * +----+ +-----+ +-----+
1048 * | |------>| T |---X--->| N |
1050 * +----+ +-----+ +-----+
1053 * +----------| R |----------+ |
1057 * Key: ---X--> HEAD flag set in pointer
1062 * (see __rb_reserve_next() to see where this happens)
1064 * What the above shows is that the reader just swapped out
1065 * the reader page with a page in the buffer, but before it
1066 * could make the new header point back to the new page added
1067 * it was preempted by a writer. The writer moved forward onto
1068 * the new page added by the reader and is about to move forward
1071 * You can see, it is legitimate for the previous pointer of
1072 * the head (or any page) not to point back to itself. But only
1076 #define RB_PAGE_NORMAL 0UL
1077 #define RB_PAGE_HEAD 1UL
1078 #define RB_PAGE_UPDATE 2UL
1081 #define RB_FLAG_MASK 3UL
1083 /* PAGE_MOVED is not part of the mask */
1084 #define RB_PAGE_MOVED 4UL
1087 * rb_list_head - remove any bit
1089 static struct list_head *rb_list_head(struct list_head *list)
1091 unsigned long val = (unsigned long)list;
1093 return (struct list_head *)(val & ~RB_FLAG_MASK);
1097 * rb_is_head_page - test if the given page is the head page
1099 * Because the reader may move the head_page pointer, we can
1100 * not trust what the head page is (it may be pointing to
1101 * the reader page). But if the next page is a header page,
1102 * its flags will be non zero.
1105 rb_is_head_page(struct ring_buffer_per_cpu *cpu_buffer,
1106 struct buffer_page *page, struct list_head *list)
1110 val = (unsigned long)list->next;
1112 if ((val & ~RB_FLAG_MASK) != (unsigned long)&page->list)
1113 return RB_PAGE_MOVED;
1115 return val & RB_FLAG_MASK;
1121 * The unique thing about the reader page, is that, if the
1122 * writer is ever on it, the previous pointer never points
1123 * back to the reader page.
1125 static bool rb_is_reader_page(struct buffer_page *page)
1127 struct list_head *list = page->list.prev;
1129 return rb_list_head(list->next) != &page->list;
1133 * rb_set_list_to_head - set a list_head to be pointing to head.
1135 static void rb_set_list_to_head(struct ring_buffer_per_cpu *cpu_buffer,
1136 struct list_head *list)
1140 ptr = (unsigned long *)&list->next;
1141 *ptr |= RB_PAGE_HEAD;
1142 *ptr &= ~RB_PAGE_UPDATE;
1146 * rb_head_page_activate - sets up head page
1148 static void rb_head_page_activate(struct ring_buffer_per_cpu *cpu_buffer)
1150 struct buffer_page *head;
1152 head = cpu_buffer->head_page;
1157 * Set the previous list pointer to have the HEAD flag.
1159 rb_set_list_to_head(cpu_buffer, head->list.prev);
1162 static void rb_list_head_clear(struct list_head *list)
1164 unsigned long *ptr = (unsigned long *)&list->next;
1166 *ptr &= ~RB_FLAG_MASK;
1170 * rb_head_page_deactivate - clears head page ptr (for free list)
1173 rb_head_page_deactivate(struct ring_buffer_per_cpu *cpu_buffer)
1175 struct list_head *hd;
1177 /* Go through the whole list and clear any pointers found. */
1178 rb_list_head_clear(cpu_buffer->pages);
1180 list_for_each(hd, cpu_buffer->pages)
1181 rb_list_head_clear(hd);
1184 static int rb_head_page_set(struct ring_buffer_per_cpu *cpu_buffer,
1185 struct buffer_page *head,
1186 struct buffer_page *prev,
1187 int old_flag, int new_flag)
1189 struct list_head *list;
1190 unsigned long val = (unsigned long)&head->list;
1195 val &= ~RB_FLAG_MASK;
1197 ret = cmpxchg((unsigned long *)&list->next,
1198 val | old_flag, val | new_flag);
1200 /* check if the reader took the page */
1201 if ((ret & ~RB_FLAG_MASK) != val)
1202 return RB_PAGE_MOVED;
1204 return ret & RB_FLAG_MASK;
1207 static int rb_head_page_set_update(struct ring_buffer_per_cpu *cpu_buffer,
1208 struct buffer_page *head,
1209 struct buffer_page *prev,
1212 return rb_head_page_set(cpu_buffer, head, prev,
1213 old_flag, RB_PAGE_UPDATE);
1216 static int rb_head_page_set_head(struct ring_buffer_per_cpu *cpu_buffer,
1217 struct buffer_page *head,
1218 struct buffer_page *prev,
1221 return rb_head_page_set(cpu_buffer, head, prev,
1222 old_flag, RB_PAGE_HEAD);
1225 static int rb_head_page_set_normal(struct ring_buffer_per_cpu *cpu_buffer,
1226 struct buffer_page *head,
1227 struct buffer_page *prev,
1230 return rb_head_page_set(cpu_buffer, head, prev,
1231 old_flag, RB_PAGE_NORMAL);
1234 static inline void rb_inc_page(struct ring_buffer_per_cpu *cpu_buffer,
1235 struct buffer_page **bpage)
1237 struct list_head *p = rb_list_head((*bpage)->list.next);
1239 *bpage = list_entry(p, struct buffer_page, list);
1242 static struct buffer_page *
1243 rb_set_head_page(struct ring_buffer_per_cpu *cpu_buffer)
1245 struct buffer_page *head;
1246 struct buffer_page *page;
1247 struct list_head *list;
1250 if (RB_WARN_ON(cpu_buffer, !cpu_buffer->head_page))
1254 list = cpu_buffer->pages;
1255 if (RB_WARN_ON(cpu_buffer, rb_list_head(list->prev->next) != list))
1258 page = head = cpu_buffer->head_page;
1260 * It is possible that the writer moves the header behind
1261 * where we started, and we miss in one loop.
1262 * A second loop should grab the header, but we'll do
1263 * three loops just because I'm paranoid.
1265 for (i = 0; i < 3; i++) {
1267 if (rb_is_head_page(cpu_buffer, page, page->list.prev)) {
1268 cpu_buffer->head_page = page;
1271 rb_inc_page(cpu_buffer, &page);
1272 } while (page != head);
1275 RB_WARN_ON(cpu_buffer, 1);
1280 static int rb_head_page_replace(struct buffer_page *old,
1281 struct buffer_page *new)
1283 unsigned long *ptr = (unsigned long *)&old->list.prev->next;
1287 val = *ptr & ~RB_FLAG_MASK;
1288 val |= RB_PAGE_HEAD;
1290 ret = cmpxchg(ptr, val, (unsigned long)&new->list);
1296 * rb_tail_page_update - move the tail page forward
1298 static void rb_tail_page_update(struct ring_buffer_per_cpu *cpu_buffer,
1299 struct buffer_page *tail_page,
1300 struct buffer_page *next_page)
1302 unsigned long old_entries;
1303 unsigned long old_write;
1306 * The tail page now needs to be moved forward.
1308 * We need to reset the tail page, but without messing
1309 * with possible erasing of data brought in by interrupts
1310 * that have moved the tail page and are currently on it.
1312 * We add a counter to the write field to denote this.
1314 old_write = local_add_return(RB_WRITE_INTCNT, &next_page->write);
1315 old_entries = local_add_return(RB_WRITE_INTCNT, &next_page->entries);
1317 local_inc(&cpu_buffer->pages_touched);
1319 * Just make sure we have seen our old_write and synchronize
1320 * with any interrupts that come in.
1325 * If the tail page is still the same as what we think
1326 * it is, then it is up to us to update the tail
1329 if (tail_page == READ_ONCE(cpu_buffer->tail_page)) {
1330 /* Zero the write counter */
1331 unsigned long val = old_write & ~RB_WRITE_MASK;
1332 unsigned long eval = old_entries & ~RB_WRITE_MASK;
1335 * This will only succeed if an interrupt did
1336 * not come in and change it. In which case, we
1337 * do not want to modify it.
1339 * We add (void) to let the compiler know that we do not care
1340 * about the return value of these functions. We use the
1341 * cmpxchg to only update if an interrupt did not already
1342 * do it for us. If the cmpxchg fails, we don't care.
1344 (void)local_cmpxchg(&next_page->write, old_write, val);
1345 (void)local_cmpxchg(&next_page->entries, old_entries, eval);
1348 * No need to worry about races with clearing out the commit.
1349 * it only can increment when a commit takes place. But that
1350 * only happens in the outer most nested commit.
1352 local_set(&next_page->page->commit, 0);
1354 /* Again, either we update tail_page or an interrupt does */
1355 (void)cmpxchg(&cpu_buffer->tail_page, tail_page, next_page);
1359 static int rb_check_bpage(struct ring_buffer_per_cpu *cpu_buffer,
1360 struct buffer_page *bpage)
1362 unsigned long val = (unsigned long)bpage;
1364 if (RB_WARN_ON(cpu_buffer, val & RB_FLAG_MASK))
1371 * rb_check_list - make sure a pointer to a list has the last bits zero
1373 static int rb_check_list(struct ring_buffer_per_cpu *cpu_buffer,
1374 struct list_head *list)
1376 if (RB_WARN_ON(cpu_buffer, rb_list_head(list->prev) != list->prev))
1378 if (RB_WARN_ON(cpu_buffer, rb_list_head(list->next) != list->next))
1384 * rb_check_pages - integrity check of buffer pages
1385 * @cpu_buffer: CPU buffer with pages to test
1387 * As a safety measure we check to make sure the data pages have not
1390 static int rb_check_pages(struct ring_buffer_per_cpu *cpu_buffer)
1392 struct list_head *head = cpu_buffer->pages;
1393 struct buffer_page *bpage, *tmp;
1395 /* Reset the head page if it exists */
1396 if (cpu_buffer->head_page)
1397 rb_set_head_page(cpu_buffer);
1399 rb_head_page_deactivate(cpu_buffer);
1401 if (RB_WARN_ON(cpu_buffer, head->next->prev != head))
1403 if (RB_WARN_ON(cpu_buffer, head->prev->next != head))
1406 if (rb_check_list(cpu_buffer, head))
1409 list_for_each_entry_safe(bpage, tmp, head, list) {
1410 if (RB_WARN_ON(cpu_buffer,
1411 bpage->list.next->prev != &bpage->list))
1413 if (RB_WARN_ON(cpu_buffer,
1414 bpage->list.prev->next != &bpage->list))
1416 if (rb_check_list(cpu_buffer, &bpage->list))
1420 rb_head_page_activate(cpu_buffer);
1425 static int __rb_allocate_pages(long nr_pages, struct list_head *pages, int cpu)
1427 struct buffer_page *bpage, *tmp;
1428 bool user_thread = current->mm != NULL;
1433 * Check if the available memory is there first.
1434 * Note, si_mem_available() only gives us a rough estimate of available
1435 * memory. It may not be accurate. But we don't care, we just want
1436 * to prevent doing any allocation when it is obvious that it is
1437 * not going to succeed.
1439 i = si_mem_available();
1444 * __GFP_RETRY_MAYFAIL flag makes sure that the allocation fails
1445 * gracefully without invoking oom-killer and the system is not
1448 mflags = GFP_KERNEL | __GFP_RETRY_MAYFAIL;
1451 * If a user thread allocates too much, and si_mem_available()
1452 * reports there's enough memory, even though there is not.
1453 * Make sure the OOM killer kills this thread. This can happen
1454 * even with RETRY_MAYFAIL because another task may be doing
1455 * an allocation after this task has taken all memory.
1456 * This is the task the OOM killer needs to take out during this
1457 * loop, even if it was triggered by an allocation somewhere else.
1460 set_current_oom_origin();
1461 for (i = 0; i < nr_pages; i++) {
1464 bpage = kzalloc_node(ALIGN(sizeof(*bpage), cache_line_size()),
1465 mflags, cpu_to_node(cpu));
1469 list_add(&bpage->list, pages);
1471 page = alloc_pages_node(cpu_to_node(cpu), mflags, 0);
1474 bpage->page = page_address(page);
1475 rb_init_page(bpage->page);
1477 if (user_thread && fatal_signal_pending(current))
1481 clear_current_oom_origin();
1486 list_for_each_entry_safe(bpage, tmp, pages, list) {
1487 list_del_init(&bpage->list);
1488 free_buffer_page(bpage);
1491 clear_current_oom_origin();
1496 static int rb_allocate_pages(struct ring_buffer_per_cpu *cpu_buffer,
1497 unsigned long nr_pages)
1503 if (__rb_allocate_pages(nr_pages, &pages, cpu_buffer->cpu))
1507 * The ring buffer page list is a circular list that does not
1508 * start and end with a list head. All page list items point to
1511 cpu_buffer->pages = pages.next;
1514 cpu_buffer->nr_pages = nr_pages;
1516 rb_check_pages(cpu_buffer);
1521 static struct ring_buffer_per_cpu *
1522 rb_allocate_cpu_buffer(struct trace_buffer *buffer, long nr_pages, int cpu)
1524 struct ring_buffer_per_cpu *cpu_buffer;
1525 struct buffer_page *bpage;
1529 cpu_buffer = kzalloc_node(ALIGN(sizeof(*cpu_buffer), cache_line_size()),
1530 GFP_KERNEL, cpu_to_node(cpu));
1534 cpu_buffer->cpu = cpu;
1535 cpu_buffer->buffer = buffer;
1536 raw_spin_lock_init(&cpu_buffer->reader_lock);
1537 lockdep_set_class(&cpu_buffer->reader_lock, buffer->reader_lock_key);
1538 cpu_buffer->lock = (arch_spinlock_t)__ARCH_SPIN_LOCK_UNLOCKED;
1539 INIT_WORK(&cpu_buffer->update_pages_work, update_pages_handler);
1540 init_completion(&cpu_buffer->update_done);
1541 init_irq_work(&cpu_buffer->irq_work.work, rb_wake_up_waiters);
1542 init_waitqueue_head(&cpu_buffer->irq_work.waiters);
1543 init_waitqueue_head(&cpu_buffer->irq_work.full_waiters);
1545 bpage = kzalloc_node(ALIGN(sizeof(*bpage), cache_line_size()),
1546 GFP_KERNEL, cpu_to_node(cpu));
1548 goto fail_free_buffer;
1550 rb_check_bpage(cpu_buffer, bpage);
1552 cpu_buffer->reader_page = bpage;
1553 page = alloc_pages_node(cpu_to_node(cpu), GFP_KERNEL, 0);
1555 goto fail_free_reader;
1556 bpage->page = page_address(page);
1557 rb_init_page(bpage->page);
1559 INIT_LIST_HEAD(&cpu_buffer->reader_page->list);
1560 INIT_LIST_HEAD(&cpu_buffer->new_pages);
1562 ret = rb_allocate_pages(cpu_buffer, nr_pages);
1564 goto fail_free_reader;
1566 cpu_buffer->head_page
1567 = list_entry(cpu_buffer->pages, struct buffer_page, list);
1568 cpu_buffer->tail_page = cpu_buffer->commit_page = cpu_buffer->head_page;
1570 rb_head_page_activate(cpu_buffer);
1575 free_buffer_page(cpu_buffer->reader_page);
1582 static void rb_free_cpu_buffer(struct ring_buffer_per_cpu *cpu_buffer)
1584 struct list_head *head = cpu_buffer->pages;
1585 struct buffer_page *bpage, *tmp;
1587 free_buffer_page(cpu_buffer->reader_page);
1589 rb_head_page_deactivate(cpu_buffer);
1592 list_for_each_entry_safe(bpage, tmp, head, list) {
1593 list_del_init(&bpage->list);
1594 free_buffer_page(bpage);
1596 bpage = list_entry(head, struct buffer_page, list);
1597 free_buffer_page(bpage);
1604 * __ring_buffer_alloc - allocate a new ring_buffer
1605 * @size: the size in bytes per cpu that is needed.
1606 * @flags: attributes to set for the ring buffer.
1607 * @key: ring buffer reader_lock_key.
1609 * Currently the only flag that is available is the RB_FL_OVERWRITE
1610 * flag. This flag means that the buffer will overwrite old data
1611 * when the buffer wraps. If this flag is not set, the buffer will
1612 * drop data when the tail hits the head.
1614 struct trace_buffer *__ring_buffer_alloc(unsigned long size, unsigned flags,
1615 struct lock_class_key *key)
1617 struct trace_buffer *buffer;
1623 /* keep it in its own cache line */
1624 buffer = kzalloc(ALIGN(sizeof(*buffer), cache_line_size()),
1629 if (!zalloc_cpumask_var(&buffer->cpumask, GFP_KERNEL))
1630 goto fail_free_buffer;
1632 nr_pages = DIV_ROUND_UP(size, BUF_PAGE_SIZE);
1633 buffer->flags = flags;
1634 buffer->clock = trace_clock_local;
1635 buffer->reader_lock_key = key;
1637 init_irq_work(&buffer->irq_work.work, rb_wake_up_waiters);
1638 init_waitqueue_head(&buffer->irq_work.waiters);
1640 /* need at least two pages */
1644 buffer->cpus = nr_cpu_ids;
1646 bsize = sizeof(void *) * nr_cpu_ids;
1647 buffer->buffers = kzalloc(ALIGN(bsize, cache_line_size()),
1649 if (!buffer->buffers)
1650 goto fail_free_cpumask;
1652 cpu = raw_smp_processor_id();
1653 cpumask_set_cpu(cpu, buffer->cpumask);
1654 buffer->buffers[cpu] = rb_allocate_cpu_buffer(buffer, nr_pages, cpu);
1655 if (!buffer->buffers[cpu])
1656 goto fail_free_buffers;
1658 ret = cpuhp_state_add_instance(CPUHP_TRACE_RB_PREPARE, &buffer->node);
1660 goto fail_free_buffers;
1662 mutex_init(&buffer->mutex);
1667 for_each_buffer_cpu(buffer, cpu) {
1668 if (buffer->buffers[cpu])
1669 rb_free_cpu_buffer(buffer->buffers[cpu]);
1671 kfree(buffer->buffers);
1674 free_cpumask_var(buffer->cpumask);
1680 EXPORT_SYMBOL_GPL(__ring_buffer_alloc);
1683 * ring_buffer_free - free a ring buffer.
1684 * @buffer: the buffer to free.
1687 ring_buffer_free(struct trace_buffer *buffer)
1691 cpuhp_state_remove_instance(CPUHP_TRACE_RB_PREPARE, &buffer->node);
1693 for_each_buffer_cpu(buffer, cpu)
1694 rb_free_cpu_buffer(buffer->buffers[cpu]);
1696 kfree(buffer->buffers);
1697 free_cpumask_var(buffer->cpumask);
1701 EXPORT_SYMBOL_GPL(ring_buffer_free);
1703 void ring_buffer_set_clock(struct trace_buffer *buffer,
1706 buffer->clock = clock;
1709 void ring_buffer_set_time_stamp_abs(struct trace_buffer *buffer, bool abs)
1711 buffer->time_stamp_abs = abs;
1714 bool ring_buffer_time_stamp_abs(struct trace_buffer *buffer)
1716 return buffer->time_stamp_abs;
1719 static void rb_reset_cpu(struct ring_buffer_per_cpu *cpu_buffer);
1721 static inline unsigned long rb_page_entries(struct buffer_page *bpage)
1723 return local_read(&bpage->entries) & RB_WRITE_MASK;
1726 static inline unsigned long rb_page_write(struct buffer_page *bpage)
1728 return local_read(&bpage->write) & RB_WRITE_MASK;
1732 rb_remove_pages(struct ring_buffer_per_cpu *cpu_buffer, unsigned long nr_pages)
1734 struct list_head *tail_page, *to_remove, *next_page;
1735 struct buffer_page *to_remove_page, *tmp_iter_page;
1736 struct buffer_page *last_page, *first_page;
1737 unsigned long nr_removed;
1738 unsigned long head_bit;
1743 raw_spin_lock_irq(&cpu_buffer->reader_lock);
1744 atomic_inc(&cpu_buffer->record_disabled);
1746 * We don't race with the readers since we have acquired the reader
1747 * lock. We also don't race with writers after disabling recording.
1748 * This makes it easy to figure out the first and the last page to be
1749 * removed from the list. We unlink all the pages in between including
1750 * the first and last pages. This is done in a busy loop so that we
1751 * lose the least number of traces.
1752 * The pages are freed after we restart recording and unlock readers.
1754 tail_page = &cpu_buffer->tail_page->list;
1757 * tail page might be on reader page, we remove the next page
1758 * from the ring buffer
1760 if (cpu_buffer->tail_page == cpu_buffer->reader_page)
1761 tail_page = rb_list_head(tail_page->next);
1762 to_remove = tail_page;
1764 /* start of pages to remove */
1765 first_page = list_entry(rb_list_head(to_remove->next),
1766 struct buffer_page, list);
1768 for (nr_removed = 0; nr_removed < nr_pages; nr_removed++) {
1769 to_remove = rb_list_head(to_remove)->next;
1770 head_bit |= (unsigned long)to_remove & RB_PAGE_HEAD;
1773 next_page = rb_list_head(to_remove)->next;
1776 * Now we remove all pages between tail_page and next_page.
1777 * Make sure that we have head_bit value preserved for the
1780 tail_page->next = (struct list_head *)((unsigned long)next_page |
1782 next_page = rb_list_head(next_page);
1783 next_page->prev = tail_page;
1785 /* make sure pages points to a valid page in the ring buffer */
1786 cpu_buffer->pages = next_page;
1788 /* update head page */
1790 cpu_buffer->head_page = list_entry(next_page,
1791 struct buffer_page, list);
1794 * change read pointer to make sure any read iterators reset
1797 cpu_buffer->read = 0;
1799 /* pages are removed, resume tracing and then free the pages */
1800 atomic_dec(&cpu_buffer->record_disabled);
1801 raw_spin_unlock_irq(&cpu_buffer->reader_lock);
1803 RB_WARN_ON(cpu_buffer, list_empty(cpu_buffer->pages));
1805 /* last buffer page to remove */
1806 last_page = list_entry(rb_list_head(to_remove), struct buffer_page,
1808 tmp_iter_page = first_page;
1813 to_remove_page = tmp_iter_page;
1814 rb_inc_page(cpu_buffer, &tmp_iter_page);
1816 /* update the counters */
1817 page_entries = rb_page_entries(to_remove_page);
1820 * If something was added to this page, it was full
1821 * since it is not the tail page. So we deduct the
1822 * bytes consumed in ring buffer from here.
1823 * Increment overrun to account for the lost events.
1825 local_add(page_entries, &cpu_buffer->overrun);
1826 local_sub(BUF_PAGE_SIZE, &cpu_buffer->entries_bytes);
1830 * We have already removed references to this list item, just
1831 * free up the buffer_page and its page
1833 free_buffer_page(to_remove_page);
1836 } while (to_remove_page != last_page);
1838 RB_WARN_ON(cpu_buffer, nr_removed);
1840 return nr_removed == 0;
1844 rb_insert_pages(struct ring_buffer_per_cpu *cpu_buffer)
1846 struct list_head *pages = &cpu_buffer->new_pages;
1847 int retries, success;
1849 raw_spin_lock_irq(&cpu_buffer->reader_lock);
1851 * We are holding the reader lock, so the reader page won't be swapped
1852 * in the ring buffer. Now we are racing with the writer trying to
1853 * move head page and the tail page.
1854 * We are going to adapt the reader page update process where:
1855 * 1. We first splice the start and end of list of new pages between
1856 * the head page and its previous page.
1857 * 2. We cmpxchg the prev_page->next to point from head page to the
1858 * start of new pages list.
1859 * 3. Finally, we update the head->prev to the end of new list.
1861 * We will try this process 10 times, to make sure that we don't keep
1867 struct list_head *head_page, *prev_page, *r;
1868 struct list_head *last_page, *first_page;
1869 struct list_head *head_page_with_bit;
1871 head_page = &rb_set_head_page(cpu_buffer)->list;
1874 prev_page = head_page->prev;
1876 first_page = pages->next;
1877 last_page = pages->prev;
1879 head_page_with_bit = (struct list_head *)
1880 ((unsigned long)head_page | RB_PAGE_HEAD);
1882 last_page->next = head_page_with_bit;
1883 first_page->prev = prev_page;
1885 r = cmpxchg(&prev_page->next, head_page_with_bit, first_page);
1887 if (r == head_page_with_bit) {
1889 * yay, we replaced the page pointer to our new list,
1890 * now, we just have to update to head page's prev
1891 * pointer to point to end of list
1893 head_page->prev = last_page;
1900 INIT_LIST_HEAD(pages);
1902 * If we weren't successful in adding in new pages, warn and stop
1905 RB_WARN_ON(cpu_buffer, !success);
1906 raw_spin_unlock_irq(&cpu_buffer->reader_lock);
1908 /* free pages if they weren't inserted */
1910 struct buffer_page *bpage, *tmp;
1911 list_for_each_entry_safe(bpage, tmp, &cpu_buffer->new_pages,
1913 list_del_init(&bpage->list);
1914 free_buffer_page(bpage);
1920 static void rb_update_pages(struct ring_buffer_per_cpu *cpu_buffer)
1924 if (cpu_buffer->nr_pages_to_update > 0)
1925 success = rb_insert_pages(cpu_buffer);
1927 success = rb_remove_pages(cpu_buffer,
1928 -cpu_buffer->nr_pages_to_update);
1931 cpu_buffer->nr_pages += cpu_buffer->nr_pages_to_update;
1934 static void update_pages_handler(struct work_struct *work)
1936 struct ring_buffer_per_cpu *cpu_buffer = container_of(work,
1937 struct ring_buffer_per_cpu, update_pages_work);
1938 rb_update_pages(cpu_buffer);
1939 complete(&cpu_buffer->update_done);
1943 * ring_buffer_resize - resize the ring buffer
1944 * @buffer: the buffer to resize.
1945 * @size: the new size.
1946 * @cpu_id: the cpu buffer to resize
1948 * Minimum size is 2 * BUF_PAGE_SIZE.
1950 * Returns 0 on success and < 0 on failure.
1952 int ring_buffer_resize(struct trace_buffer *buffer, unsigned long size,
1955 struct ring_buffer_per_cpu *cpu_buffer;
1956 unsigned long nr_pages;
1960 * Always succeed at resizing a non-existent buffer:
1965 /* Make sure the requested buffer exists */
1966 if (cpu_id != RING_BUFFER_ALL_CPUS &&
1967 !cpumask_test_cpu(cpu_id, buffer->cpumask))
1970 nr_pages = DIV_ROUND_UP(size, BUF_PAGE_SIZE);
1972 /* we need a minimum of two pages */
1976 size = nr_pages * BUF_PAGE_SIZE;
1978 /* prevent another thread from changing buffer sizes */
1979 mutex_lock(&buffer->mutex);
1982 if (cpu_id == RING_BUFFER_ALL_CPUS) {
1984 * Don't succeed if resizing is disabled, as a reader might be
1985 * manipulating the ring buffer and is expecting a sane state while
1988 for_each_buffer_cpu(buffer, cpu) {
1989 cpu_buffer = buffer->buffers[cpu];
1990 if (atomic_read(&cpu_buffer->resize_disabled)) {
1992 goto out_err_unlock;
1996 /* calculate the pages to update */
1997 for_each_buffer_cpu(buffer, cpu) {
1998 cpu_buffer = buffer->buffers[cpu];
2000 cpu_buffer->nr_pages_to_update = nr_pages -
2001 cpu_buffer->nr_pages;
2003 * nothing more to do for removing pages or no update
2005 if (cpu_buffer->nr_pages_to_update <= 0)
2008 * to add pages, make sure all new pages can be
2009 * allocated without receiving ENOMEM
2011 INIT_LIST_HEAD(&cpu_buffer->new_pages);
2012 if (__rb_allocate_pages(cpu_buffer->nr_pages_to_update,
2013 &cpu_buffer->new_pages, cpu)) {
2014 /* not enough memory for new pages */
2022 * Fire off all the required work handlers
2023 * We can't schedule on offline CPUs, but it's not necessary
2024 * since we can change their buffer sizes without any race.
2026 for_each_buffer_cpu(buffer, cpu) {
2027 cpu_buffer = buffer->buffers[cpu];
2028 if (!cpu_buffer->nr_pages_to_update)
2031 /* Can't run something on an offline CPU. */
2032 if (!cpu_online(cpu)) {
2033 rb_update_pages(cpu_buffer);
2034 cpu_buffer->nr_pages_to_update = 0;
2036 schedule_work_on(cpu,
2037 &cpu_buffer->update_pages_work);
2041 /* wait for all the updates to complete */
2042 for_each_buffer_cpu(buffer, cpu) {
2043 cpu_buffer = buffer->buffers[cpu];
2044 if (!cpu_buffer->nr_pages_to_update)
2047 if (cpu_online(cpu))
2048 wait_for_completion(&cpu_buffer->update_done);
2049 cpu_buffer->nr_pages_to_update = 0;
2054 /* Make sure this CPU has been initialized */
2055 if (!cpumask_test_cpu(cpu_id, buffer->cpumask))
2058 cpu_buffer = buffer->buffers[cpu_id];
2060 if (nr_pages == cpu_buffer->nr_pages)
2064 * Don't succeed if resizing is disabled, as a reader might be
2065 * manipulating the ring buffer and is expecting a sane state while
2068 if (atomic_read(&cpu_buffer->resize_disabled)) {
2070 goto out_err_unlock;
2073 cpu_buffer->nr_pages_to_update = nr_pages -
2074 cpu_buffer->nr_pages;
2076 INIT_LIST_HEAD(&cpu_buffer->new_pages);
2077 if (cpu_buffer->nr_pages_to_update > 0 &&
2078 __rb_allocate_pages(cpu_buffer->nr_pages_to_update,
2079 &cpu_buffer->new_pages, cpu_id)) {
2086 /* Can't run something on an offline CPU. */
2087 if (!cpu_online(cpu_id))
2088 rb_update_pages(cpu_buffer);
2090 schedule_work_on(cpu_id,
2091 &cpu_buffer->update_pages_work);
2092 wait_for_completion(&cpu_buffer->update_done);
2095 cpu_buffer->nr_pages_to_update = 0;
2101 * The ring buffer resize can happen with the ring buffer
2102 * enabled, so that the update disturbs the tracing as little
2103 * as possible. But if the buffer is disabled, we do not need
2104 * to worry about that, and we can take the time to verify
2105 * that the buffer is not corrupt.
2107 if (atomic_read(&buffer->record_disabled)) {
2108 atomic_inc(&buffer->record_disabled);
2110 * Even though the buffer was disabled, we must make sure
2111 * that it is truly disabled before calling rb_check_pages.
2112 * There could have been a race between checking
2113 * record_disable and incrementing it.
2116 for_each_buffer_cpu(buffer, cpu) {
2117 cpu_buffer = buffer->buffers[cpu];
2118 rb_check_pages(cpu_buffer);
2120 atomic_dec(&buffer->record_disabled);
2123 mutex_unlock(&buffer->mutex);
2127 for_each_buffer_cpu(buffer, cpu) {
2128 struct buffer_page *bpage, *tmp;
2130 cpu_buffer = buffer->buffers[cpu];
2131 cpu_buffer->nr_pages_to_update = 0;
2133 if (list_empty(&cpu_buffer->new_pages))
2136 list_for_each_entry_safe(bpage, tmp, &cpu_buffer->new_pages,
2138 list_del_init(&bpage->list);
2139 free_buffer_page(bpage);
2143 mutex_unlock(&buffer->mutex);
2146 EXPORT_SYMBOL_GPL(ring_buffer_resize);
2148 void ring_buffer_change_overwrite(struct trace_buffer *buffer, int val)
2150 mutex_lock(&buffer->mutex);
2152 buffer->flags |= RB_FL_OVERWRITE;
2154 buffer->flags &= ~RB_FL_OVERWRITE;
2155 mutex_unlock(&buffer->mutex);
2157 EXPORT_SYMBOL_GPL(ring_buffer_change_overwrite);
2159 static __always_inline void *__rb_page_index(struct buffer_page *bpage, unsigned index)
2161 return bpage->page->data + index;
2164 static __always_inline struct ring_buffer_event *
2165 rb_reader_event(struct ring_buffer_per_cpu *cpu_buffer)
2167 return __rb_page_index(cpu_buffer->reader_page,
2168 cpu_buffer->reader_page->read);
2171 static __always_inline unsigned rb_page_commit(struct buffer_page *bpage)
2173 return local_read(&bpage->page->commit);
2176 static struct ring_buffer_event *
2177 rb_iter_head_event(struct ring_buffer_iter *iter)
2179 struct ring_buffer_event *event;
2180 struct buffer_page *iter_head_page = iter->head_page;
2181 unsigned long commit;
2184 if (iter->head != iter->next_event)
2188 * When the writer goes across pages, it issues a cmpxchg which
2189 * is a mb(), which will synchronize with the rmb here.
2190 * (see rb_tail_page_update() and __rb_reserve_next())
2192 commit = rb_page_commit(iter_head_page);
2194 event = __rb_page_index(iter_head_page, iter->head);
2195 length = rb_event_length(event);
2198 * READ_ONCE() doesn't work on functions and we don't want the
2199 * compiler doing any crazy optimizations with length.
2203 if ((iter->head + length) > commit || length > BUF_MAX_DATA_SIZE)
2204 /* Writer corrupted the read? */
2207 memcpy(iter->event, event, length);
2209 * If the page stamp is still the same after this rmb() then the
2210 * event was safely copied without the writer entering the page.
2214 /* Make sure the page didn't change since we read this */
2215 if (iter->page_stamp != iter_head_page->page->time_stamp ||
2216 commit > rb_page_commit(iter_head_page))
2219 iter->next_event = iter->head + length;
2222 /* Reset to the beginning */
2223 iter->page_stamp = iter->read_stamp = iter->head_page->page->time_stamp;
2225 iter->next_event = 0;
2226 iter->missed_events = 1;
2230 /* Size is determined by what has been committed */
2231 static __always_inline unsigned rb_page_size(struct buffer_page *bpage)
2233 return rb_page_commit(bpage);
2236 static __always_inline unsigned
2237 rb_commit_index(struct ring_buffer_per_cpu *cpu_buffer)
2239 return rb_page_commit(cpu_buffer->commit_page);
2242 static __always_inline unsigned
2243 rb_event_index(struct ring_buffer_event *event)
2245 unsigned long addr = (unsigned long)event;
2247 return (addr & ~PAGE_MASK) - BUF_PAGE_HDR_SIZE;
2250 static void rb_inc_iter(struct ring_buffer_iter *iter)
2252 struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
2255 * The iterator could be on the reader page (it starts there).
2256 * But the head could have moved, since the reader was
2257 * found. Check for this case and assign the iterator
2258 * to the head page instead of next.
2260 if (iter->head_page == cpu_buffer->reader_page)
2261 iter->head_page = rb_set_head_page(cpu_buffer);
2263 rb_inc_page(cpu_buffer, &iter->head_page);
2265 iter->page_stamp = iter->read_stamp = iter->head_page->page->time_stamp;
2267 iter->next_event = 0;
2271 * rb_handle_head_page - writer hit the head page
2273 * Returns: +1 to retry page
2278 rb_handle_head_page(struct ring_buffer_per_cpu *cpu_buffer,
2279 struct buffer_page *tail_page,
2280 struct buffer_page *next_page)
2282 struct buffer_page *new_head;
2287 entries = rb_page_entries(next_page);
2290 * The hard part is here. We need to move the head
2291 * forward, and protect against both readers on
2292 * other CPUs and writers coming in via interrupts.
2294 type = rb_head_page_set_update(cpu_buffer, next_page, tail_page,
2298 * type can be one of four:
2299 * NORMAL - an interrupt already moved it for us
2300 * HEAD - we are the first to get here.
2301 * UPDATE - we are the interrupt interrupting
2303 * MOVED - a reader on another CPU moved the next
2304 * pointer to its reader page. Give up
2311 * We changed the head to UPDATE, thus
2312 * it is our responsibility to update
2315 local_add(entries, &cpu_buffer->overrun);
2316 local_sub(BUF_PAGE_SIZE, &cpu_buffer->entries_bytes);
2319 * The entries will be zeroed out when we move the
2323 /* still more to do */
2326 case RB_PAGE_UPDATE:
2328 * This is an interrupt that interrupt the
2329 * previous update. Still more to do.
2332 case RB_PAGE_NORMAL:
2334 * An interrupt came in before the update
2335 * and processed this for us.
2336 * Nothing left to do.
2341 * The reader is on another CPU and just did
2342 * a swap with our next_page.
2347 RB_WARN_ON(cpu_buffer, 1); /* WTF??? */
2352 * Now that we are here, the old head pointer is
2353 * set to UPDATE. This will keep the reader from
2354 * swapping the head page with the reader page.
2355 * The reader (on another CPU) will spin till
2358 * We just need to protect against interrupts
2359 * doing the job. We will set the next pointer
2360 * to HEAD. After that, we set the old pointer
2361 * to NORMAL, but only if it was HEAD before.
2362 * otherwise we are an interrupt, and only
2363 * want the outer most commit to reset it.
2365 new_head = next_page;
2366 rb_inc_page(cpu_buffer, &new_head);
2368 ret = rb_head_page_set_head(cpu_buffer, new_head, next_page,
2372 * Valid returns are:
2373 * HEAD - an interrupt came in and already set it.
2374 * NORMAL - One of two things:
2375 * 1) We really set it.
2376 * 2) A bunch of interrupts came in and moved
2377 * the page forward again.
2381 case RB_PAGE_NORMAL:
2385 RB_WARN_ON(cpu_buffer, 1);
2390 * It is possible that an interrupt came in,
2391 * set the head up, then more interrupts came in
2392 * and moved it again. When we get back here,
2393 * the page would have been set to NORMAL but we
2394 * just set it back to HEAD.
2396 * How do you detect this? Well, if that happened
2397 * the tail page would have moved.
2399 if (ret == RB_PAGE_NORMAL) {
2400 struct buffer_page *buffer_tail_page;
2402 buffer_tail_page = READ_ONCE(cpu_buffer->tail_page);
2404 * If the tail had moved passed next, then we need
2405 * to reset the pointer.
2407 if (buffer_tail_page != tail_page &&
2408 buffer_tail_page != next_page)
2409 rb_head_page_set_normal(cpu_buffer, new_head,
2415 * If this was the outer most commit (the one that
2416 * changed the original pointer from HEAD to UPDATE),
2417 * then it is up to us to reset it to NORMAL.
2419 if (type == RB_PAGE_HEAD) {
2420 ret = rb_head_page_set_normal(cpu_buffer, next_page,
2423 if (RB_WARN_ON(cpu_buffer,
2424 ret != RB_PAGE_UPDATE))
2432 rb_reset_tail(struct ring_buffer_per_cpu *cpu_buffer,
2433 unsigned long tail, struct rb_event_info *info)
2435 struct buffer_page *tail_page = info->tail_page;
2436 struct ring_buffer_event *event;
2437 unsigned long length = info->length;
2440 * Only the event that crossed the page boundary
2441 * must fill the old tail_page with padding.
2443 if (tail >= BUF_PAGE_SIZE) {
2445 * If the page was filled, then we still need
2446 * to update the real_end. Reset it to zero
2447 * and the reader will ignore it.
2449 if (tail == BUF_PAGE_SIZE)
2450 tail_page->real_end = 0;
2452 local_sub(length, &tail_page->write);
2456 event = __rb_page_index(tail_page, tail);
2458 /* account for padding bytes */
2459 local_add(BUF_PAGE_SIZE - tail, &cpu_buffer->entries_bytes);
2462 * Save the original length to the meta data.
2463 * This will be used by the reader to add lost event
2466 tail_page->real_end = tail;
2469 * If this event is bigger than the minimum size, then
2470 * we need to be careful that we don't subtract the
2471 * write counter enough to allow another writer to slip
2473 * We put in a discarded commit instead, to make sure
2474 * that this space is not used again.
2476 * If we are less than the minimum size, we don't need to
2479 if (tail > (BUF_PAGE_SIZE - RB_EVNT_MIN_SIZE)) {
2480 /* No room for any events */
2482 /* Mark the rest of the page with padding */
2483 rb_event_set_padding(event);
2485 /* Set the write back to the previous setting */
2486 local_sub(length, &tail_page->write);
2490 /* Put in a discarded event */
2491 event->array[0] = (BUF_PAGE_SIZE - tail) - RB_EVNT_HDR_SIZE;
2492 event->type_len = RINGBUF_TYPE_PADDING;
2493 /* time delta must be non zero */
2494 event->time_delta = 1;
2496 /* Set write to end of buffer */
2497 length = (tail + length) - BUF_PAGE_SIZE;
2498 local_sub(length, &tail_page->write);
2501 static inline void rb_end_commit(struct ring_buffer_per_cpu *cpu_buffer);
2504 * This is the slow path, force gcc not to inline it.
2506 static noinline struct ring_buffer_event *
2507 rb_move_tail(struct ring_buffer_per_cpu *cpu_buffer,
2508 unsigned long tail, struct rb_event_info *info)
2510 struct buffer_page *tail_page = info->tail_page;
2511 struct buffer_page *commit_page = cpu_buffer->commit_page;
2512 struct trace_buffer *buffer = cpu_buffer->buffer;
2513 struct buffer_page *next_page;
2516 next_page = tail_page;
2518 rb_inc_page(cpu_buffer, &next_page);
2521 * If for some reason, we had an interrupt storm that made
2522 * it all the way around the buffer, bail, and warn
2525 if (unlikely(next_page == commit_page)) {
2526 local_inc(&cpu_buffer->commit_overrun);
2531 * This is where the fun begins!
2533 * We are fighting against races between a reader that
2534 * could be on another CPU trying to swap its reader
2535 * page with the buffer head.
2537 * We are also fighting against interrupts coming in and
2538 * moving the head or tail on us as well.
2540 * If the next page is the head page then we have filled
2541 * the buffer, unless the commit page is still on the
2544 if (rb_is_head_page(cpu_buffer, next_page, &tail_page->list)) {
2547 * If the commit is not on the reader page, then
2548 * move the header page.
2550 if (!rb_is_reader_page(cpu_buffer->commit_page)) {
2552 * If we are not in overwrite mode,
2553 * this is easy, just stop here.
2555 if (!(buffer->flags & RB_FL_OVERWRITE)) {
2556 local_inc(&cpu_buffer->dropped_events);
2560 ret = rb_handle_head_page(cpu_buffer,
2569 * We need to be careful here too. The
2570 * commit page could still be on the reader
2571 * page. We could have a small buffer, and
2572 * have filled up the buffer with events
2573 * from interrupts and such, and wrapped.
2575 * Note, if the tail page is also the on the
2576 * reader_page, we let it move out.
2578 if (unlikely((cpu_buffer->commit_page !=
2579 cpu_buffer->tail_page) &&
2580 (cpu_buffer->commit_page ==
2581 cpu_buffer->reader_page))) {
2582 local_inc(&cpu_buffer->commit_overrun);
2588 rb_tail_page_update(cpu_buffer, tail_page, next_page);
2592 rb_reset_tail(cpu_buffer, tail, info);
2594 /* Commit what we have for now. */
2595 rb_end_commit(cpu_buffer);
2596 /* rb_end_commit() decs committing */
2597 local_inc(&cpu_buffer->committing);
2599 /* fail and let the caller try again */
2600 return ERR_PTR(-EAGAIN);
2604 rb_reset_tail(cpu_buffer, tail, info);
2610 static struct ring_buffer_event *
2611 rb_add_time_stamp(struct ring_buffer_event *event, u64 delta, bool abs)
2614 event->type_len = RINGBUF_TYPE_TIME_STAMP;
2616 event->type_len = RINGBUF_TYPE_TIME_EXTEND;
2618 /* Not the first event on the page, or not delta? */
2619 if (abs || rb_event_index(event)) {
2620 event->time_delta = delta & TS_MASK;
2621 event->array[0] = delta >> TS_SHIFT;
2623 /* nope, just zero it */
2624 event->time_delta = 0;
2625 event->array[0] = 0;
2628 return skip_time_extend(event);
2631 static inline bool rb_event_is_commit(struct ring_buffer_per_cpu *cpu_buffer,
2632 struct ring_buffer_event *event);
2634 #ifndef CONFIG_HAVE_UNSTABLE_SCHED_CLOCK
2635 static inline bool sched_clock_stable(void)
2642 rb_check_timestamp(struct ring_buffer_per_cpu *cpu_buffer,
2643 struct rb_event_info *info)
2647 WARN_ONCE(1, "Delta way too big! %llu ts=%llu before=%llu after=%llu write stamp=%llu\n%s",
2648 (unsigned long long)info->delta,
2649 (unsigned long long)info->ts,
2650 (unsigned long long)info->before,
2651 (unsigned long long)info->after,
2652 (unsigned long long)(rb_time_read(&cpu_buffer->write_stamp, &write_stamp) ? write_stamp : 0),
2653 sched_clock_stable() ? "" :
2654 "If you just came from a suspend/resume,\n"
2655 "please switch to the trace global clock:\n"
2656 " echo global > /sys/kernel/debug/tracing/trace_clock\n"
2657 "or add trace_clock=global to the kernel command line\n");
2660 static void rb_add_timestamp(struct ring_buffer_per_cpu *cpu_buffer,
2661 struct ring_buffer_event **event,
2662 struct rb_event_info *info,
2664 unsigned int *length)
2666 bool abs = info->add_timestamp &
2667 (RB_ADD_STAMP_FORCE | RB_ADD_STAMP_ABSOLUTE);
2669 if (unlikely(info->delta > (1ULL << 59))) {
2670 /* did the clock go backwards */
2671 if (info->before == info->after && info->before > info->ts) {
2672 /* not interrupted */
2676 * This is possible with a recalibrating of the TSC.
2677 * Do not produce a call stack, but just report it.
2681 pr_warn("Ring buffer clock went backwards: %llu -> %llu\n",
2682 info->before, info->ts);
2685 rb_check_timestamp(cpu_buffer, info);
2689 *event = rb_add_time_stamp(*event, info->delta, abs);
2690 *length -= RB_LEN_TIME_EXTEND;
2695 * rb_update_event - update event type and data
2696 * @cpu_buffer: The per cpu buffer of the @event
2697 * @event: the event to update
2698 * @info: The info to update the @event with (contains length and delta)
2700 * Update the type and data fields of the @event. The length
2701 * is the actual size that is written to the ring buffer,
2702 * and with this, we can determine what to place into the
2706 rb_update_event(struct ring_buffer_per_cpu *cpu_buffer,
2707 struct ring_buffer_event *event,
2708 struct rb_event_info *info)
2710 unsigned length = info->length;
2711 u64 delta = info->delta;
2714 * If we need to add a timestamp, then we
2715 * add it to the start of the reserved space.
2717 if (unlikely(info->add_timestamp))
2718 rb_add_timestamp(cpu_buffer, &event, info, &delta, &length);
2720 event->time_delta = delta;
2721 length -= RB_EVNT_HDR_SIZE;
2722 if (length > RB_MAX_SMALL_DATA) {
2723 event->type_len = 0;
2724 event->array[0] = length;
2726 event->type_len = DIV_ROUND_UP(length, RB_ALIGNMENT);
2729 static unsigned rb_calculate_event_length(unsigned length)
2731 struct ring_buffer_event event; /* Used only for sizeof array */
2733 /* zero length can cause confusions */
2737 if (length > RB_MAX_SMALL_DATA)
2738 length += sizeof(event.array[0]);
2740 length += RB_EVNT_HDR_SIZE;
2741 length = ALIGN(length, RB_ALIGNMENT);
2744 * In case the time delta is larger than the 27 bits for it
2745 * in the header, we need to add a timestamp. If another
2746 * event comes in when trying to discard this one to increase
2747 * the length, then the timestamp will be added in the allocated
2748 * space of this event. If length is bigger than the size needed
2749 * for the TIME_EXTEND, then padding has to be used. The events
2750 * length must be either RB_LEN_TIME_EXTEND, or greater than or equal
2751 * to RB_LEN_TIME_EXTEND + 8, as 8 is the minimum size for padding.
2752 * As length is a multiple of 4, we only need to worry if it
2753 * is 12 (RB_LEN_TIME_EXTEND + 4).
2755 if (length == RB_LEN_TIME_EXTEND + RB_ALIGNMENT)
2756 length += RB_ALIGNMENT;
2761 static __always_inline bool
2762 rb_event_is_commit(struct ring_buffer_per_cpu *cpu_buffer,
2763 struct ring_buffer_event *event)
2765 unsigned long addr = (unsigned long)event;
2766 unsigned long index;
2768 index = rb_event_index(event);
2771 return cpu_buffer->commit_page->page == (void *)addr &&
2772 rb_commit_index(cpu_buffer) == index;
2775 static u64 rb_time_delta(struct ring_buffer_event *event)
2777 switch (event->type_len) {
2778 case RINGBUF_TYPE_PADDING:
2781 case RINGBUF_TYPE_TIME_EXTEND:
2782 return ring_buffer_event_time_stamp(event);
2784 case RINGBUF_TYPE_TIME_STAMP:
2787 case RINGBUF_TYPE_DATA:
2788 return event->time_delta;
2795 rb_try_to_discard(struct ring_buffer_per_cpu *cpu_buffer,
2796 struct ring_buffer_event *event)
2798 unsigned long new_index, old_index;
2799 struct buffer_page *bpage;
2800 unsigned long index;
2805 new_index = rb_event_index(event);
2806 old_index = new_index + rb_event_ts_length(event);
2807 addr = (unsigned long)event;
2810 bpage = READ_ONCE(cpu_buffer->tail_page);
2812 delta = rb_time_delta(event);
2814 if (!rb_time_read(&cpu_buffer->write_stamp, &write_stamp))
2817 /* Make sure the write stamp is read before testing the location */
2820 if (bpage->page == (void *)addr && rb_page_write(bpage) == old_index) {
2821 unsigned long write_mask =
2822 local_read(&bpage->write) & ~RB_WRITE_MASK;
2823 unsigned long event_length = rb_event_length(event);
2825 /* Something came in, can't discard */
2826 if (!rb_time_cmpxchg(&cpu_buffer->write_stamp,
2827 write_stamp, write_stamp - delta))
2831 * If an event were to come in now, it would see that the
2832 * write_stamp and the before_stamp are different, and assume
2833 * that this event just added itself before updating
2834 * the write stamp. The interrupting event will fix the
2835 * write stamp for us, and use the before stamp as its delta.
2839 * This is on the tail page. It is possible that
2840 * a write could come in and move the tail page
2841 * and write to the next page. That is fine
2842 * because we just shorten what is on this page.
2844 old_index += write_mask;
2845 new_index += write_mask;
2846 index = local_cmpxchg(&bpage->write, old_index, new_index);
2847 if (index == old_index) {
2848 /* update counters */
2849 local_sub(event_length, &cpu_buffer->entries_bytes);
2854 /* could not discard */
2858 static void rb_start_commit(struct ring_buffer_per_cpu *cpu_buffer)
2860 local_inc(&cpu_buffer->committing);
2861 local_inc(&cpu_buffer->commits);
2864 static __always_inline void
2865 rb_set_commit_to_write(struct ring_buffer_per_cpu *cpu_buffer)
2867 unsigned long max_count;
2870 * We only race with interrupts and NMIs on this CPU.
2871 * If we own the commit event, then we can commit
2872 * all others that interrupted us, since the interruptions
2873 * are in stack format (they finish before they come
2874 * back to us). This allows us to do a simple loop to
2875 * assign the commit to the tail.
2878 max_count = cpu_buffer->nr_pages * 100;
2880 while (cpu_buffer->commit_page != READ_ONCE(cpu_buffer->tail_page)) {
2881 if (RB_WARN_ON(cpu_buffer, !(--max_count)))
2883 if (RB_WARN_ON(cpu_buffer,
2884 rb_is_reader_page(cpu_buffer->tail_page)))
2886 local_set(&cpu_buffer->commit_page->page->commit,
2887 rb_page_write(cpu_buffer->commit_page));
2888 rb_inc_page(cpu_buffer, &cpu_buffer->commit_page);
2889 /* add barrier to keep gcc from optimizing too much */
2892 while (rb_commit_index(cpu_buffer) !=
2893 rb_page_write(cpu_buffer->commit_page)) {
2895 local_set(&cpu_buffer->commit_page->page->commit,
2896 rb_page_write(cpu_buffer->commit_page));
2897 RB_WARN_ON(cpu_buffer,
2898 local_read(&cpu_buffer->commit_page->page->commit) &
2903 /* again, keep gcc from optimizing */
2907 * If an interrupt came in just after the first while loop
2908 * and pushed the tail page forward, we will be left with
2909 * a dangling commit that will never go forward.
2911 if (unlikely(cpu_buffer->commit_page != READ_ONCE(cpu_buffer->tail_page)))
2915 static __always_inline void rb_end_commit(struct ring_buffer_per_cpu *cpu_buffer)
2917 unsigned long commits;
2919 if (RB_WARN_ON(cpu_buffer,
2920 !local_read(&cpu_buffer->committing)))
2924 commits = local_read(&cpu_buffer->commits);
2925 /* synchronize with interrupts */
2927 if (local_read(&cpu_buffer->committing) == 1)
2928 rb_set_commit_to_write(cpu_buffer);
2930 local_dec(&cpu_buffer->committing);
2932 /* synchronize with interrupts */
2936 * Need to account for interrupts coming in between the
2937 * updating of the commit page and the clearing of the
2938 * committing counter.
2940 if (unlikely(local_read(&cpu_buffer->commits) != commits) &&
2941 !local_read(&cpu_buffer->committing)) {
2942 local_inc(&cpu_buffer->committing);
2947 static inline void rb_event_discard(struct ring_buffer_event *event)
2949 if (extended_time(event))
2950 event = skip_time_extend(event);
2952 /* array[0] holds the actual length for the discarded event */
2953 event->array[0] = rb_event_data_length(event) - RB_EVNT_HDR_SIZE;
2954 event->type_len = RINGBUF_TYPE_PADDING;
2955 /* time delta must be non zero */
2956 if (!event->time_delta)
2957 event->time_delta = 1;
2960 static void rb_commit(struct ring_buffer_per_cpu *cpu_buffer,
2961 struct ring_buffer_event *event)
2963 local_inc(&cpu_buffer->entries);
2964 rb_end_commit(cpu_buffer);
2967 static __always_inline void
2968 rb_wakeups(struct trace_buffer *buffer, struct ring_buffer_per_cpu *cpu_buffer)
2974 if (buffer->irq_work.waiters_pending) {
2975 buffer->irq_work.waiters_pending = false;
2976 /* irq_work_queue() supplies it's own memory barriers */
2977 irq_work_queue(&buffer->irq_work.work);
2980 if (cpu_buffer->irq_work.waiters_pending) {
2981 cpu_buffer->irq_work.waiters_pending = false;
2982 /* irq_work_queue() supplies it's own memory barriers */
2983 irq_work_queue(&cpu_buffer->irq_work.work);
2986 if (cpu_buffer->last_pages_touch == local_read(&cpu_buffer->pages_touched))
2989 if (cpu_buffer->reader_page == cpu_buffer->commit_page)
2992 if (!cpu_buffer->irq_work.full_waiters_pending)
2995 cpu_buffer->last_pages_touch = local_read(&cpu_buffer->pages_touched);
2997 full = cpu_buffer->shortest_full;
2998 nr_pages = cpu_buffer->nr_pages;
2999 dirty = ring_buffer_nr_dirty_pages(buffer, cpu_buffer->cpu);
3000 if (full && nr_pages && (dirty * 100) <= full * nr_pages)
3003 cpu_buffer->irq_work.wakeup_full = true;
3004 cpu_buffer->irq_work.full_waiters_pending = false;
3005 /* irq_work_queue() supplies it's own memory barriers */
3006 irq_work_queue(&cpu_buffer->irq_work.work);
3010 * The lock and unlock are done within a preempt disable section.
3011 * The current_context per_cpu variable can only be modified
3012 * by the current task between lock and unlock. But it can
3013 * be modified more than once via an interrupt. To pass this
3014 * information from the lock to the unlock without having to
3015 * access the 'in_interrupt()' functions again (which do show
3016 * a bit of overhead in something as critical as function tracing,
3017 * we use a bitmask trick.
3019 * bit 1 = NMI context
3020 * bit 2 = IRQ context
3021 * bit 3 = SoftIRQ context
3022 * bit 4 = normal context.
3024 * This works because this is the order of contexts that can
3025 * preempt other contexts. A SoftIRQ never preempts an IRQ
3028 * When the context is determined, the corresponding bit is
3029 * checked and set (if it was set, then a recursion of that context
3032 * On unlock, we need to clear this bit. To do so, just subtract
3033 * 1 from the current_context and AND it to itself.
3037 * 101 & 100 = 100 (clearing bit zero)
3040 * 1010 & 1001 = 1000 (clearing bit 1)
3042 * The least significant bit can be cleared this way, and it
3043 * just so happens that it is the same bit corresponding to
3044 * the current context.
3046 * Now the TRANSITION bit breaks the above slightly. The TRANSITION bit
3047 * is set when a recursion is detected at the current context, and if
3048 * the TRANSITION bit is already set, it will fail the recursion.
3049 * This is needed because there's a lag between the changing of
3050 * interrupt context and updating the preempt count. In this case,
3051 * a false positive will be found. To handle this, one extra recursion
3052 * is allowed, and this is done by the TRANSITION bit. If the TRANSITION
3053 * bit is already set, then it is considered a recursion and the function
3054 * ends. Otherwise, the TRANSITION bit is set, and that bit is returned.
3056 * On the trace_recursive_unlock(), the TRANSITION bit will be the first
3057 * to be cleared. Even if it wasn't the context that set it. That is,
3058 * if an interrupt comes in while NORMAL bit is set and the ring buffer
3059 * is called before preempt_count() is updated, since the check will
3060 * be on the NORMAL bit, the TRANSITION bit will then be set. If an
3061 * NMI then comes in, it will set the NMI bit, but when the NMI code
3062 * does the trace_recursive_unlock() it will clear the TRANSTION bit
3063 * and leave the NMI bit set. But this is fine, because the interrupt
3064 * code that set the TRANSITION bit will then clear the NMI bit when it
3065 * calls trace_recursive_unlock(). If another NMI comes in, it will
3066 * set the TRANSITION bit and continue.
3068 * Note: The TRANSITION bit only handles a single transition between context.
3071 static __always_inline int
3072 trace_recursive_lock(struct ring_buffer_per_cpu *cpu_buffer)
3074 unsigned int val = cpu_buffer->current_context;
3075 unsigned long pc = preempt_count();
3078 if (!(pc & (NMI_MASK | HARDIRQ_MASK | SOFTIRQ_OFFSET)))
3079 bit = RB_CTX_NORMAL;
3081 bit = pc & NMI_MASK ? RB_CTX_NMI :
3082 pc & HARDIRQ_MASK ? RB_CTX_IRQ : RB_CTX_SOFTIRQ;
3084 if (unlikely(val & (1 << (bit + cpu_buffer->nest)))) {
3086 * It is possible that this was called by transitioning
3087 * between interrupt context, and preempt_count() has not
3088 * been updated yet. In this case, use the TRANSITION bit.
3090 bit = RB_CTX_TRANSITION;
3091 if (val & (1 << (bit + cpu_buffer->nest)))
3095 val |= (1 << (bit + cpu_buffer->nest));
3096 cpu_buffer->current_context = val;
3101 static __always_inline void
3102 trace_recursive_unlock(struct ring_buffer_per_cpu *cpu_buffer)
3104 cpu_buffer->current_context &=
3105 cpu_buffer->current_context - (1 << cpu_buffer->nest);
3108 /* The recursive locking above uses 5 bits */
3109 #define NESTED_BITS 5
3112 * ring_buffer_nest_start - Allow to trace while nested
3113 * @buffer: The ring buffer to modify
3115 * The ring buffer has a safety mechanism to prevent recursion.
3116 * But there may be a case where a trace needs to be done while
3117 * tracing something else. In this case, calling this function
3118 * will allow this function to nest within a currently active
3119 * ring_buffer_lock_reserve().
3121 * Call this function before calling another ring_buffer_lock_reserve() and
3122 * call ring_buffer_nest_end() after the nested ring_buffer_unlock_commit().
3124 void ring_buffer_nest_start(struct trace_buffer *buffer)
3126 struct ring_buffer_per_cpu *cpu_buffer;
3129 /* Enabled by ring_buffer_nest_end() */
3130 preempt_disable_notrace();
3131 cpu = raw_smp_processor_id();
3132 cpu_buffer = buffer->buffers[cpu];
3133 /* This is the shift value for the above recursive locking */
3134 cpu_buffer->nest += NESTED_BITS;
3138 * ring_buffer_nest_end - Allow to trace while nested
3139 * @buffer: The ring buffer to modify
3141 * Must be called after ring_buffer_nest_start() and after the
3142 * ring_buffer_unlock_commit().
3144 void ring_buffer_nest_end(struct trace_buffer *buffer)
3146 struct ring_buffer_per_cpu *cpu_buffer;
3149 /* disabled by ring_buffer_nest_start() */
3150 cpu = raw_smp_processor_id();
3151 cpu_buffer = buffer->buffers[cpu];
3152 /* This is the shift value for the above recursive locking */
3153 cpu_buffer->nest -= NESTED_BITS;
3154 preempt_enable_notrace();
3158 * ring_buffer_unlock_commit - commit a reserved
3159 * @buffer: The buffer to commit to
3160 * @event: The event pointer to commit.
3162 * This commits the data to the ring buffer, and releases any locks held.
3164 * Must be paired with ring_buffer_lock_reserve.
3166 int ring_buffer_unlock_commit(struct trace_buffer *buffer,
3167 struct ring_buffer_event *event)
3169 struct ring_buffer_per_cpu *cpu_buffer;
3170 int cpu = raw_smp_processor_id();
3172 cpu_buffer = buffer->buffers[cpu];
3174 rb_commit(cpu_buffer, event);
3176 rb_wakeups(buffer, cpu_buffer);
3178 trace_recursive_unlock(cpu_buffer);
3180 preempt_enable_notrace();
3184 EXPORT_SYMBOL_GPL(ring_buffer_unlock_commit);
3186 static struct ring_buffer_event *
3187 __rb_reserve_next(struct ring_buffer_per_cpu *cpu_buffer,
3188 struct rb_event_info *info)
3190 struct ring_buffer_event *event;
3191 struct buffer_page *tail_page;
3192 unsigned long tail, write, w;
3196 /* Don't let the compiler play games with cpu_buffer->tail_page */
3197 tail_page = info->tail_page = READ_ONCE(cpu_buffer->tail_page);
3199 /*A*/ w = local_read(&tail_page->write) & RB_WRITE_MASK;
3201 b_ok = rb_time_read(&cpu_buffer->before_stamp, &info->before);
3202 a_ok = rb_time_read(&cpu_buffer->write_stamp, &info->after);
3204 info->ts = rb_time_stamp(cpu_buffer->buffer);
3206 if ((info->add_timestamp & RB_ADD_STAMP_ABSOLUTE)) {
3207 info->delta = info->ts;
3210 * If interrupting an event time update, we may need an
3211 * absolute timestamp.
3212 * Don't bother if this is the start of a new page (w == 0).
3214 if (unlikely(!a_ok || !b_ok || (info->before != info->after && w))) {
3215 info->add_timestamp |= RB_ADD_STAMP_FORCE | RB_ADD_STAMP_EXTEND;
3216 info->length += RB_LEN_TIME_EXTEND;
3218 info->delta = info->ts - info->after;
3219 if (unlikely(test_time_stamp(info->delta))) {
3220 info->add_timestamp |= RB_ADD_STAMP_EXTEND;
3221 info->length += RB_LEN_TIME_EXTEND;
3226 /*B*/ rb_time_set(&cpu_buffer->before_stamp, info->ts);
3228 /*C*/ write = local_add_return(info->length, &tail_page->write);
3230 /* set write to only the index of the write */
3231 write &= RB_WRITE_MASK;
3233 tail = write - info->length;
3235 /* See if we shot pass the end of this buffer page */
3236 if (unlikely(write > BUF_PAGE_SIZE)) {
3238 /* before and after may now different, fix it up*/
3239 b_ok = rb_time_read(&cpu_buffer->before_stamp, &info->before);
3240 a_ok = rb_time_read(&cpu_buffer->write_stamp, &info->after);
3241 if (a_ok && b_ok && info->before != info->after)
3242 (void)rb_time_cmpxchg(&cpu_buffer->before_stamp,
3243 info->before, info->after);
3245 return rb_move_tail(cpu_buffer, tail, info);
3248 if (likely(tail == w)) {
3252 /* Nothing interrupted us between A and C */
3253 /*D*/ rb_time_set(&cpu_buffer->write_stamp, info->ts);
3255 /*E*/ s_ok = rb_time_read(&cpu_buffer->before_stamp, &save_before);
3256 RB_WARN_ON(cpu_buffer, !s_ok);
3257 if (likely(!(info->add_timestamp &
3258 (RB_ADD_STAMP_FORCE | RB_ADD_STAMP_ABSOLUTE))))
3259 /* This did not interrupt any time update */
3260 info->delta = info->ts - info->after;
3262 /* Just use full timestamp for inerrupting event */
3263 info->delta = info->ts;
3265 if (unlikely(info->ts != save_before)) {
3266 /* SLOW PATH - Interrupted between C and E */
3268 a_ok = rb_time_read(&cpu_buffer->write_stamp, &info->after);
3269 RB_WARN_ON(cpu_buffer, !a_ok);
3271 /* Write stamp must only go forward */
3272 if (save_before > info->after) {
3274 * We do not care about the result, only that
3275 * it gets updated atomically.
3277 (void)rb_time_cmpxchg(&cpu_buffer->write_stamp,
3278 info->after, save_before);
3283 /* SLOW PATH - Interrupted between A and C */
3284 a_ok = rb_time_read(&cpu_buffer->write_stamp, &info->after);
3285 /* Was interrupted before here, write_stamp must be valid */
3286 RB_WARN_ON(cpu_buffer, !a_ok);
3287 ts = rb_time_stamp(cpu_buffer->buffer);
3289 /*E*/ if (write == (local_read(&tail_page->write) & RB_WRITE_MASK) &&
3291 /* Nothing came after this event between C and E */
3292 info->delta = ts - info->after;
3293 (void)rb_time_cmpxchg(&cpu_buffer->write_stamp,
3294 info->after, info->ts);
3298 * Interrupted beween C and E:
3299 * Lost the previous events time stamp. Just set the
3300 * delta to zero, and this will be the same time as
3301 * the event this event interrupted. And the events that
3302 * came after this will still be correct (as they would
3303 * have built their delta on the previous event.
3307 info->add_timestamp &= ~RB_ADD_STAMP_FORCE;
3311 * If this is the first commit on the page, then it has the same
3312 * timestamp as the page itself.
3314 if (unlikely(!tail && !(info->add_timestamp &
3315 (RB_ADD_STAMP_FORCE | RB_ADD_STAMP_ABSOLUTE))))
3318 /* We reserved something on the buffer */
3320 event = __rb_page_index(tail_page, tail);
3321 rb_update_event(cpu_buffer, event, info);
3323 local_inc(&tail_page->entries);
3326 * If this is the first commit on the page, then update
3329 if (unlikely(!tail))
3330 tail_page->page->time_stamp = info->ts;
3332 /* account for these added bytes */
3333 local_add(info->length, &cpu_buffer->entries_bytes);
3338 static __always_inline struct ring_buffer_event *
3339 rb_reserve_next_event(struct trace_buffer *buffer,
3340 struct ring_buffer_per_cpu *cpu_buffer,
3341 unsigned long length)
3343 struct ring_buffer_event *event;
3344 struct rb_event_info info;
3348 rb_start_commit(cpu_buffer);
3349 /* The commit page can not change after this */
3351 #ifdef CONFIG_RING_BUFFER_ALLOW_SWAP
3353 * Due to the ability to swap a cpu buffer from a buffer
3354 * it is possible it was swapped before we committed.
3355 * (committing stops a swap). We check for it here and
3356 * if it happened, we have to fail the write.
3359 if (unlikely(READ_ONCE(cpu_buffer->buffer) != buffer)) {
3360 local_dec(&cpu_buffer->committing);
3361 local_dec(&cpu_buffer->commits);
3366 info.length = rb_calculate_event_length(length);
3368 if (ring_buffer_time_stamp_abs(cpu_buffer->buffer)) {
3369 add_ts_default = RB_ADD_STAMP_ABSOLUTE;
3370 info.length += RB_LEN_TIME_EXTEND;
3372 add_ts_default = RB_ADD_STAMP_NONE;
3376 info.add_timestamp = add_ts_default;
3380 * We allow for interrupts to reenter here and do a trace.
3381 * If one does, it will cause this original code to loop
3382 * back here. Even with heavy interrupts happening, this
3383 * should only happen a few times in a row. If this happens
3384 * 1000 times in a row, there must be either an interrupt
3385 * storm or we have something buggy.
3388 if (RB_WARN_ON(cpu_buffer, ++nr_loops > 1000))
3391 event = __rb_reserve_next(cpu_buffer, &info);
3393 if (unlikely(PTR_ERR(event) == -EAGAIN)) {
3394 if (info.add_timestamp & (RB_ADD_STAMP_FORCE | RB_ADD_STAMP_EXTEND))
3395 info.length -= RB_LEN_TIME_EXTEND;
3402 rb_end_commit(cpu_buffer);
3407 * ring_buffer_lock_reserve - reserve a part of the buffer
3408 * @buffer: the ring buffer to reserve from
3409 * @length: the length of the data to reserve (excluding event header)
3411 * Returns a reserved event on the ring buffer to copy directly to.
3412 * The user of this interface will need to get the body to write into
3413 * and can use the ring_buffer_event_data() interface.
3415 * The length is the length of the data needed, not the event length
3416 * which also includes the event header.
3418 * Must be paired with ring_buffer_unlock_commit, unless NULL is returned.
3419 * If NULL is returned, then nothing has been allocated or locked.
3421 struct ring_buffer_event *
3422 ring_buffer_lock_reserve(struct trace_buffer *buffer, unsigned long length)
3424 struct ring_buffer_per_cpu *cpu_buffer;
3425 struct ring_buffer_event *event;
3428 /* If we are tracing schedule, we don't want to recurse */
3429 preempt_disable_notrace();
3431 if (unlikely(atomic_read(&buffer->record_disabled)))
3434 cpu = raw_smp_processor_id();
3436 if (unlikely(!cpumask_test_cpu(cpu, buffer->cpumask)))
3439 cpu_buffer = buffer->buffers[cpu];
3441 if (unlikely(atomic_read(&cpu_buffer->record_disabled)))
3444 if (unlikely(length > BUF_MAX_DATA_SIZE))
3447 if (unlikely(trace_recursive_lock(cpu_buffer)))
3450 event = rb_reserve_next_event(buffer, cpu_buffer, length);
3457 trace_recursive_unlock(cpu_buffer);
3459 preempt_enable_notrace();
3462 EXPORT_SYMBOL_GPL(ring_buffer_lock_reserve);
3465 * Decrement the entries to the page that an event is on.
3466 * The event does not even need to exist, only the pointer
3467 * to the page it is on. This may only be called before the commit
3471 rb_decrement_entry(struct ring_buffer_per_cpu *cpu_buffer,
3472 struct ring_buffer_event *event)
3474 unsigned long addr = (unsigned long)event;
3475 struct buffer_page *bpage = cpu_buffer->commit_page;
3476 struct buffer_page *start;
3480 /* Do the likely case first */
3481 if (likely(bpage->page == (void *)addr)) {
3482 local_dec(&bpage->entries);
3487 * Because the commit page may be on the reader page we
3488 * start with the next page and check the end loop there.
3490 rb_inc_page(cpu_buffer, &bpage);
3493 if (bpage->page == (void *)addr) {
3494 local_dec(&bpage->entries);
3497 rb_inc_page(cpu_buffer, &bpage);
3498 } while (bpage != start);
3500 /* commit not part of this buffer?? */
3501 RB_WARN_ON(cpu_buffer, 1);
3505 * ring_buffer_commit_discard - discard an event that has not been committed
3506 * @buffer: the ring buffer
3507 * @event: non committed event to discard
3509 * Sometimes an event that is in the ring buffer needs to be ignored.
3510 * This function lets the user discard an event in the ring buffer
3511 * and then that event will not be read later.
3513 * This function only works if it is called before the item has been
3514 * committed. It will try to free the event from the ring buffer
3515 * if another event has not been added behind it.
3517 * If another event has been added behind it, it will set the event
3518 * up as discarded, and perform the commit.
3520 * If this function is called, do not call ring_buffer_unlock_commit on
3523 void ring_buffer_discard_commit(struct trace_buffer *buffer,
3524 struct ring_buffer_event *event)
3526 struct ring_buffer_per_cpu *cpu_buffer;
3529 /* The event is discarded regardless */
3530 rb_event_discard(event);
3532 cpu = smp_processor_id();
3533 cpu_buffer = buffer->buffers[cpu];
3536 * This must only be called if the event has not been
3537 * committed yet. Thus we can assume that preemption
3538 * is still disabled.
3540 RB_WARN_ON(buffer, !local_read(&cpu_buffer->committing));
3542 rb_decrement_entry(cpu_buffer, event);
3543 if (rb_try_to_discard(cpu_buffer, event))
3547 rb_end_commit(cpu_buffer);
3549 trace_recursive_unlock(cpu_buffer);
3551 preempt_enable_notrace();
3554 EXPORT_SYMBOL_GPL(ring_buffer_discard_commit);
3557 * ring_buffer_write - write data to the buffer without reserving
3558 * @buffer: The ring buffer to write to.
3559 * @length: The length of the data being written (excluding the event header)
3560 * @data: The data to write to the buffer.
3562 * This is like ring_buffer_lock_reserve and ring_buffer_unlock_commit as
3563 * one function. If you already have the data to write to the buffer, it
3564 * may be easier to simply call this function.
3566 * Note, like ring_buffer_lock_reserve, the length is the length of the data
3567 * and not the length of the event which would hold the header.
3569 int ring_buffer_write(struct trace_buffer *buffer,
3570 unsigned long length,
3573 struct ring_buffer_per_cpu *cpu_buffer;
3574 struct ring_buffer_event *event;
3579 preempt_disable_notrace();
3581 if (atomic_read(&buffer->record_disabled))
3584 cpu = raw_smp_processor_id();
3586 if (!cpumask_test_cpu(cpu, buffer->cpumask))
3589 cpu_buffer = buffer->buffers[cpu];
3591 if (atomic_read(&cpu_buffer->record_disabled))
3594 if (length > BUF_MAX_DATA_SIZE)
3597 if (unlikely(trace_recursive_lock(cpu_buffer)))
3600 event = rb_reserve_next_event(buffer, cpu_buffer, length);
3604 body = rb_event_data(event);
3606 memcpy(body, data, length);
3608 rb_commit(cpu_buffer, event);
3610 rb_wakeups(buffer, cpu_buffer);
3615 trace_recursive_unlock(cpu_buffer);
3618 preempt_enable_notrace();
3622 EXPORT_SYMBOL_GPL(ring_buffer_write);
3624 static bool rb_per_cpu_empty(struct ring_buffer_per_cpu *cpu_buffer)
3626 struct buffer_page *reader = cpu_buffer->reader_page;
3627 struct buffer_page *head = rb_set_head_page(cpu_buffer);
3628 struct buffer_page *commit = cpu_buffer->commit_page;
3630 /* In case of error, head will be NULL */
3631 if (unlikely(!head))
3634 return reader->read == rb_page_commit(reader) &&
3635 (commit == reader ||
3637 head->read == rb_page_commit(commit)));
3641 * ring_buffer_record_disable - stop all writes into the buffer
3642 * @buffer: The ring buffer to stop writes to.
3644 * This prevents all writes to the buffer. Any attempt to write
3645 * to the buffer after this will fail and return NULL.
3647 * The caller should call synchronize_rcu() after this.
3649 void ring_buffer_record_disable(struct trace_buffer *buffer)
3651 atomic_inc(&buffer->record_disabled);
3653 EXPORT_SYMBOL_GPL(ring_buffer_record_disable);
3656 * ring_buffer_record_enable - enable writes to the buffer
3657 * @buffer: The ring buffer to enable writes
3659 * Note, multiple disables will need the same number of enables
3660 * to truly enable the writing (much like preempt_disable).
3662 void ring_buffer_record_enable(struct trace_buffer *buffer)
3664 atomic_dec(&buffer->record_disabled);
3666 EXPORT_SYMBOL_GPL(ring_buffer_record_enable);
3669 * ring_buffer_record_off - stop all writes into the buffer
3670 * @buffer: The ring buffer to stop writes to.
3672 * This prevents all writes to the buffer. Any attempt to write
3673 * to the buffer after this will fail and return NULL.
3675 * This is different than ring_buffer_record_disable() as
3676 * it works like an on/off switch, where as the disable() version
3677 * must be paired with a enable().
3679 void ring_buffer_record_off(struct trace_buffer *buffer)
3682 unsigned int new_rd;
3685 rd = atomic_read(&buffer->record_disabled);
3686 new_rd = rd | RB_BUFFER_OFF;
3687 } while (atomic_cmpxchg(&buffer->record_disabled, rd, new_rd) != rd);
3689 EXPORT_SYMBOL_GPL(ring_buffer_record_off);
3692 * ring_buffer_record_on - restart writes into the buffer
3693 * @buffer: The ring buffer to start writes to.
3695 * This enables all writes to the buffer that was disabled by
3696 * ring_buffer_record_off().
3698 * This is different than ring_buffer_record_enable() as
3699 * it works like an on/off switch, where as the enable() version
3700 * must be paired with a disable().
3702 void ring_buffer_record_on(struct trace_buffer *buffer)
3705 unsigned int new_rd;
3708 rd = atomic_read(&buffer->record_disabled);
3709 new_rd = rd & ~RB_BUFFER_OFF;
3710 } while (atomic_cmpxchg(&buffer->record_disabled, rd, new_rd) != rd);
3712 EXPORT_SYMBOL_GPL(ring_buffer_record_on);
3715 * ring_buffer_record_is_on - return true if the ring buffer can write
3716 * @buffer: The ring buffer to see if write is enabled
3718 * Returns true if the ring buffer is in a state that it accepts writes.
3720 bool ring_buffer_record_is_on(struct trace_buffer *buffer)
3722 return !atomic_read(&buffer->record_disabled);
3726 * ring_buffer_record_is_set_on - return true if the ring buffer is set writable
3727 * @buffer: The ring buffer to see if write is set enabled
3729 * Returns true if the ring buffer is set writable by ring_buffer_record_on().
3730 * Note that this does NOT mean it is in a writable state.
3732 * It may return true when the ring buffer has been disabled by
3733 * ring_buffer_record_disable(), as that is a temporary disabling of
3736 bool ring_buffer_record_is_set_on(struct trace_buffer *buffer)
3738 return !(atomic_read(&buffer->record_disabled) & RB_BUFFER_OFF);
3742 * ring_buffer_record_disable_cpu - stop all writes into the cpu_buffer
3743 * @buffer: The ring buffer to stop writes to.
3744 * @cpu: The CPU buffer to stop
3746 * This prevents all writes to the buffer. Any attempt to write
3747 * to the buffer after this will fail and return NULL.
3749 * The caller should call synchronize_rcu() after this.
3751 void ring_buffer_record_disable_cpu(struct trace_buffer *buffer, int cpu)
3753 struct ring_buffer_per_cpu *cpu_buffer;
3755 if (!cpumask_test_cpu(cpu, buffer->cpumask))
3758 cpu_buffer = buffer->buffers[cpu];
3759 atomic_inc(&cpu_buffer->record_disabled);
3761 EXPORT_SYMBOL_GPL(ring_buffer_record_disable_cpu);
3764 * ring_buffer_record_enable_cpu - enable writes to the buffer
3765 * @buffer: The ring buffer to enable writes
3766 * @cpu: The CPU to enable.
3768 * Note, multiple disables will need the same number of enables
3769 * to truly enable the writing (much like preempt_disable).
3771 void ring_buffer_record_enable_cpu(struct trace_buffer *buffer, int cpu)
3773 struct ring_buffer_per_cpu *cpu_buffer;
3775 if (!cpumask_test_cpu(cpu, buffer->cpumask))
3778 cpu_buffer = buffer->buffers[cpu];
3779 atomic_dec(&cpu_buffer->record_disabled);
3781 EXPORT_SYMBOL_GPL(ring_buffer_record_enable_cpu);
3784 * The total entries in the ring buffer is the running counter
3785 * of entries entered into the ring buffer, minus the sum of
3786 * the entries read from the ring buffer and the number of
3787 * entries that were overwritten.
3789 static inline unsigned long
3790 rb_num_of_entries(struct ring_buffer_per_cpu *cpu_buffer)
3792 return local_read(&cpu_buffer->entries) -
3793 (local_read(&cpu_buffer->overrun) + cpu_buffer->read);
3797 * ring_buffer_oldest_event_ts - get the oldest event timestamp from the buffer
3798 * @buffer: The ring buffer
3799 * @cpu: The per CPU buffer to read from.
3801 u64 ring_buffer_oldest_event_ts(struct trace_buffer *buffer, int cpu)
3803 unsigned long flags;
3804 struct ring_buffer_per_cpu *cpu_buffer;
3805 struct buffer_page *bpage;
3808 if (!cpumask_test_cpu(cpu, buffer->cpumask))
3811 cpu_buffer = buffer->buffers[cpu];
3812 raw_spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
3814 * if the tail is on reader_page, oldest time stamp is on the reader
3817 if (cpu_buffer->tail_page == cpu_buffer->reader_page)
3818 bpage = cpu_buffer->reader_page;
3820 bpage = rb_set_head_page(cpu_buffer);
3822 ret = bpage->page->time_stamp;
3823 raw_spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
3827 EXPORT_SYMBOL_GPL(ring_buffer_oldest_event_ts);
3830 * ring_buffer_bytes_cpu - get the number of bytes consumed in a cpu buffer
3831 * @buffer: The ring buffer
3832 * @cpu: The per CPU buffer to read from.
3834 unsigned long ring_buffer_bytes_cpu(struct trace_buffer *buffer, int cpu)
3836 struct ring_buffer_per_cpu *cpu_buffer;
3839 if (!cpumask_test_cpu(cpu, buffer->cpumask))
3842 cpu_buffer = buffer->buffers[cpu];
3843 ret = local_read(&cpu_buffer->entries_bytes) - cpu_buffer->read_bytes;
3847 EXPORT_SYMBOL_GPL(ring_buffer_bytes_cpu);
3850 * ring_buffer_entries_cpu - get the number of entries in a cpu buffer
3851 * @buffer: The ring buffer
3852 * @cpu: The per CPU buffer to get the entries from.
3854 unsigned long ring_buffer_entries_cpu(struct trace_buffer *buffer, int cpu)
3856 struct ring_buffer_per_cpu *cpu_buffer;
3858 if (!cpumask_test_cpu(cpu, buffer->cpumask))
3861 cpu_buffer = buffer->buffers[cpu];
3863 return rb_num_of_entries(cpu_buffer);
3865 EXPORT_SYMBOL_GPL(ring_buffer_entries_cpu);
3868 * ring_buffer_overrun_cpu - get the number of overruns caused by the ring
3869 * buffer wrapping around (only if RB_FL_OVERWRITE is on).
3870 * @buffer: The ring buffer
3871 * @cpu: The per CPU buffer to get the number of overruns from
3873 unsigned long ring_buffer_overrun_cpu(struct trace_buffer *buffer, int cpu)
3875 struct ring_buffer_per_cpu *cpu_buffer;
3878 if (!cpumask_test_cpu(cpu, buffer->cpumask))
3881 cpu_buffer = buffer->buffers[cpu];
3882 ret = local_read(&cpu_buffer->overrun);
3886 EXPORT_SYMBOL_GPL(ring_buffer_overrun_cpu);
3889 * ring_buffer_commit_overrun_cpu - get the number of overruns caused by
3890 * commits failing due to the buffer wrapping around while there are uncommitted
3891 * events, such as during an interrupt storm.
3892 * @buffer: The ring buffer
3893 * @cpu: The per CPU buffer to get the number of overruns from
3896 ring_buffer_commit_overrun_cpu(struct trace_buffer *buffer, int cpu)
3898 struct ring_buffer_per_cpu *cpu_buffer;
3901 if (!cpumask_test_cpu(cpu, buffer->cpumask))
3904 cpu_buffer = buffer->buffers[cpu];
3905 ret = local_read(&cpu_buffer->commit_overrun);
3909 EXPORT_SYMBOL_GPL(ring_buffer_commit_overrun_cpu);
3912 * ring_buffer_dropped_events_cpu - get the number of dropped events caused by
3913 * the ring buffer filling up (only if RB_FL_OVERWRITE is off).
3914 * @buffer: The ring buffer
3915 * @cpu: The per CPU buffer to get the number of overruns from
3918 ring_buffer_dropped_events_cpu(struct trace_buffer *buffer, int cpu)
3920 struct ring_buffer_per_cpu *cpu_buffer;
3923 if (!cpumask_test_cpu(cpu, buffer->cpumask))
3926 cpu_buffer = buffer->buffers[cpu];
3927 ret = local_read(&cpu_buffer->dropped_events);
3931 EXPORT_SYMBOL_GPL(ring_buffer_dropped_events_cpu);
3934 * ring_buffer_read_events_cpu - get the number of events successfully read
3935 * @buffer: The ring buffer
3936 * @cpu: The per CPU buffer to get the number of events read
3939 ring_buffer_read_events_cpu(struct trace_buffer *buffer, int cpu)
3941 struct ring_buffer_per_cpu *cpu_buffer;
3943 if (!cpumask_test_cpu(cpu, buffer->cpumask))
3946 cpu_buffer = buffer->buffers[cpu];
3947 return cpu_buffer->read;
3949 EXPORT_SYMBOL_GPL(ring_buffer_read_events_cpu);
3952 * ring_buffer_entries - get the number of entries in a buffer
3953 * @buffer: The ring buffer
3955 * Returns the total number of entries in the ring buffer
3958 unsigned long ring_buffer_entries(struct trace_buffer *buffer)
3960 struct ring_buffer_per_cpu *cpu_buffer;
3961 unsigned long entries = 0;
3964 /* if you care about this being correct, lock the buffer */
3965 for_each_buffer_cpu(buffer, cpu) {
3966 cpu_buffer = buffer->buffers[cpu];
3967 entries += rb_num_of_entries(cpu_buffer);
3972 EXPORT_SYMBOL_GPL(ring_buffer_entries);
3975 * ring_buffer_overruns - get the number of overruns in buffer
3976 * @buffer: The ring buffer
3978 * Returns the total number of overruns in the ring buffer
3981 unsigned long ring_buffer_overruns(struct trace_buffer *buffer)
3983 struct ring_buffer_per_cpu *cpu_buffer;
3984 unsigned long overruns = 0;
3987 /* if you care about this being correct, lock the buffer */
3988 for_each_buffer_cpu(buffer, cpu) {
3989 cpu_buffer = buffer->buffers[cpu];
3990 overruns += local_read(&cpu_buffer->overrun);
3995 EXPORT_SYMBOL_GPL(ring_buffer_overruns);
3997 static void rb_iter_reset(struct ring_buffer_iter *iter)
3999 struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
4001 /* Iterator usage is expected to have record disabled */
4002 iter->head_page = cpu_buffer->reader_page;
4003 iter->head = cpu_buffer->reader_page->read;
4004 iter->next_event = iter->head;
4006 iter->cache_reader_page = iter->head_page;
4007 iter->cache_read = cpu_buffer->read;
4010 iter->read_stamp = cpu_buffer->read_stamp;
4011 iter->page_stamp = cpu_buffer->reader_page->page->time_stamp;
4013 iter->read_stamp = iter->head_page->page->time_stamp;
4014 iter->page_stamp = iter->read_stamp;
4019 * ring_buffer_iter_reset - reset an iterator
4020 * @iter: The iterator to reset
4022 * Resets the iterator, so that it will start from the beginning
4025 void ring_buffer_iter_reset(struct ring_buffer_iter *iter)
4027 struct ring_buffer_per_cpu *cpu_buffer;
4028 unsigned long flags;
4033 cpu_buffer = iter->cpu_buffer;
4035 raw_spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
4036 rb_iter_reset(iter);
4037 raw_spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
4039 EXPORT_SYMBOL_GPL(ring_buffer_iter_reset);
4042 * ring_buffer_iter_empty - check if an iterator has no more to read
4043 * @iter: The iterator to check
4045 int ring_buffer_iter_empty(struct ring_buffer_iter *iter)
4047 struct ring_buffer_per_cpu *cpu_buffer;
4048 struct buffer_page *reader;
4049 struct buffer_page *head_page;
4050 struct buffer_page *commit_page;
4051 struct buffer_page *curr_commit_page;
4056 cpu_buffer = iter->cpu_buffer;
4057 reader = cpu_buffer->reader_page;
4058 head_page = cpu_buffer->head_page;
4059 commit_page = cpu_buffer->commit_page;
4060 commit_ts = commit_page->page->time_stamp;
4063 * When the writer goes across pages, it issues a cmpxchg which
4064 * is a mb(), which will synchronize with the rmb here.
4065 * (see rb_tail_page_update())
4068 commit = rb_page_commit(commit_page);
4069 /* We want to make sure that the commit page doesn't change */
4072 /* Make sure commit page didn't change */
4073 curr_commit_page = READ_ONCE(cpu_buffer->commit_page);
4074 curr_commit_ts = READ_ONCE(curr_commit_page->page->time_stamp);
4076 /* If the commit page changed, then there's more data */
4077 if (curr_commit_page != commit_page ||
4078 curr_commit_ts != commit_ts)
4081 /* Still racy, as it may return a false positive, but that's OK */
4082 return ((iter->head_page == commit_page && iter->head >= commit) ||
4083 (iter->head_page == reader && commit_page == head_page &&
4084 head_page->read == commit &&
4085 iter->head == rb_page_commit(cpu_buffer->reader_page)));
4087 EXPORT_SYMBOL_GPL(ring_buffer_iter_empty);
4090 rb_update_read_stamp(struct ring_buffer_per_cpu *cpu_buffer,
4091 struct ring_buffer_event *event)
4095 switch (event->type_len) {
4096 case RINGBUF_TYPE_PADDING:
4099 case RINGBUF_TYPE_TIME_EXTEND:
4100 delta = ring_buffer_event_time_stamp(event);
4101 cpu_buffer->read_stamp += delta;
4104 case RINGBUF_TYPE_TIME_STAMP:
4105 delta = ring_buffer_event_time_stamp(event);
4106 cpu_buffer->read_stamp = delta;
4109 case RINGBUF_TYPE_DATA:
4110 cpu_buffer->read_stamp += event->time_delta;
4114 RB_WARN_ON(cpu_buffer, 1);
4120 rb_update_iter_read_stamp(struct ring_buffer_iter *iter,
4121 struct ring_buffer_event *event)
4125 switch (event->type_len) {
4126 case RINGBUF_TYPE_PADDING:
4129 case RINGBUF_TYPE_TIME_EXTEND:
4130 delta = ring_buffer_event_time_stamp(event);
4131 iter->read_stamp += delta;
4134 case RINGBUF_TYPE_TIME_STAMP:
4135 delta = ring_buffer_event_time_stamp(event);
4136 iter->read_stamp = delta;
4139 case RINGBUF_TYPE_DATA:
4140 iter->read_stamp += event->time_delta;
4144 RB_WARN_ON(iter->cpu_buffer, 1);
4149 static struct buffer_page *
4150 rb_get_reader_page(struct ring_buffer_per_cpu *cpu_buffer)
4152 struct buffer_page *reader = NULL;
4153 unsigned long overwrite;
4154 unsigned long flags;
4158 local_irq_save(flags);
4159 arch_spin_lock(&cpu_buffer->lock);
4163 * This should normally only loop twice. But because the
4164 * start of the reader inserts an empty page, it causes
4165 * a case where we will loop three times. There should be no
4166 * reason to loop four times (that I know of).
4168 if (RB_WARN_ON(cpu_buffer, ++nr_loops > 3)) {
4173 reader = cpu_buffer->reader_page;
4175 /* If there's more to read, return this page */
4176 if (cpu_buffer->reader_page->read < rb_page_size(reader))
4179 /* Never should we have an index greater than the size */
4180 if (RB_WARN_ON(cpu_buffer,
4181 cpu_buffer->reader_page->read > rb_page_size(reader)))
4184 /* check if we caught up to the tail */
4186 if (cpu_buffer->commit_page == cpu_buffer->reader_page)
4189 /* Don't bother swapping if the ring buffer is empty */
4190 if (rb_num_of_entries(cpu_buffer) == 0)
4194 * Reset the reader page to size zero.
4196 local_set(&cpu_buffer->reader_page->write, 0);
4197 local_set(&cpu_buffer->reader_page->entries, 0);
4198 local_set(&cpu_buffer->reader_page->page->commit, 0);
4199 cpu_buffer->reader_page->real_end = 0;
4203 * Splice the empty reader page into the list around the head.
4205 reader = rb_set_head_page(cpu_buffer);
4208 cpu_buffer->reader_page->list.next = rb_list_head(reader->list.next);
4209 cpu_buffer->reader_page->list.prev = reader->list.prev;
4212 * cpu_buffer->pages just needs to point to the buffer, it
4213 * has no specific buffer page to point to. Lets move it out
4214 * of our way so we don't accidentally swap it.
4216 cpu_buffer->pages = reader->list.prev;
4218 /* The reader page will be pointing to the new head */
4219 rb_set_list_to_head(cpu_buffer, &cpu_buffer->reader_page->list);
4222 * We want to make sure we read the overruns after we set up our
4223 * pointers to the next object. The writer side does a
4224 * cmpxchg to cross pages which acts as the mb on the writer
4225 * side. Note, the reader will constantly fail the swap
4226 * while the writer is updating the pointers, so this
4227 * guarantees that the overwrite recorded here is the one we
4228 * want to compare with the last_overrun.
4231 overwrite = local_read(&(cpu_buffer->overrun));
4234 * Here's the tricky part.
4236 * We need to move the pointer past the header page.
4237 * But we can only do that if a writer is not currently
4238 * moving it. The page before the header page has the
4239 * flag bit '1' set if it is pointing to the page we want.
4240 * but if the writer is in the process of moving it
4241 * than it will be '2' or already moved '0'.
4244 ret = rb_head_page_replace(reader, cpu_buffer->reader_page);
4247 * If we did not convert it, then we must try again.
4253 * Yay! We succeeded in replacing the page.
4255 * Now make the new head point back to the reader page.
4257 rb_list_head(reader->list.next)->prev = &cpu_buffer->reader_page->list;
4258 rb_inc_page(cpu_buffer, &cpu_buffer->head_page);
4260 local_inc(&cpu_buffer->pages_read);
4262 /* Finally update the reader page to the new head */
4263 cpu_buffer->reader_page = reader;
4264 cpu_buffer->reader_page->read = 0;
4266 if (overwrite != cpu_buffer->last_overrun) {
4267 cpu_buffer->lost_events = overwrite - cpu_buffer->last_overrun;
4268 cpu_buffer->last_overrun = overwrite;
4274 /* Update the read_stamp on the first event */
4275 if (reader && reader->read == 0)
4276 cpu_buffer->read_stamp = reader->page->time_stamp;
4278 arch_spin_unlock(&cpu_buffer->lock);
4279 local_irq_restore(flags);
4284 static void rb_advance_reader(struct ring_buffer_per_cpu *cpu_buffer)
4286 struct ring_buffer_event *event;
4287 struct buffer_page *reader;
4290 reader = rb_get_reader_page(cpu_buffer);
4292 /* This function should not be called when buffer is empty */
4293 if (RB_WARN_ON(cpu_buffer, !reader))
4296 event = rb_reader_event(cpu_buffer);
4298 if (event->type_len <= RINGBUF_TYPE_DATA_TYPE_LEN_MAX)
4301 rb_update_read_stamp(cpu_buffer, event);
4303 length = rb_event_length(event);
4304 cpu_buffer->reader_page->read += length;
4307 static void rb_advance_iter(struct ring_buffer_iter *iter)
4309 struct ring_buffer_per_cpu *cpu_buffer;
4311 cpu_buffer = iter->cpu_buffer;
4313 /* If head == next_event then we need to jump to the next event */
4314 if (iter->head == iter->next_event) {
4315 /* If the event gets overwritten again, there's nothing to do */
4316 if (rb_iter_head_event(iter) == NULL)
4320 iter->head = iter->next_event;
4323 * Check if we are at the end of the buffer.
4325 if (iter->next_event >= rb_page_size(iter->head_page)) {
4326 /* discarded commits can make the page empty */
4327 if (iter->head_page == cpu_buffer->commit_page)
4333 rb_update_iter_read_stamp(iter, iter->event);
4336 static int rb_lost_events(struct ring_buffer_per_cpu *cpu_buffer)
4338 return cpu_buffer->lost_events;
4341 static struct ring_buffer_event *
4342 rb_buffer_peek(struct ring_buffer_per_cpu *cpu_buffer, u64 *ts,
4343 unsigned long *lost_events)
4345 struct ring_buffer_event *event;
4346 struct buffer_page *reader;
4353 * We repeat when a time extend is encountered.
4354 * Since the time extend is always attached to a data event,
4355 * we should never loop more than once.
4356 * (We never hit the following condition more than twice).
4358 if (RB_WARN_ON(cpu_buffer, ++nr_loops > 2))
4361 reader = rb_get_reader_page(cpu_buffer);
4365 event = rb_reader_event(cpu_buffer);
4367 switch (event->type_len) {
4368 case RINGBUF_TYPE_PADDING:
4369 if (rb_null_event(event))
4370 RB_WARN_ON(cpu_buffer, 1);
4372 * Because the writer could be discarding every
4373 * event it creates (which would probably be bad)
4374 * if we were to go back to "again" then we may never
4375 * catch up, and will trigger the warn on, or lock
4376 * the box. Return the padding, and we will release
4377 * the current locks, and try again.
4381 case RINGBUF_TYPE_TIME_EXTEND:
4382 /* Internal data, OK to advance */
4383 rb_advance_reader(cpu_buffer);
4386 case RINGBUF_TYPE_TIME_STAMP:
4388 *ts = ring_buffer_event_time_stamp(event);
4389 ring_buffer_normalize_time_stamp(cpu_buffer->buffer,
4390 cpu_buffer->cpu, ts);
4392 /* Internal data, OK to advance */
4393 rb_advance_reader(cpu_buffer);
4396 case RINGBUF_TYPE_DATA:
4398 *ts = cpu_buffer->read_stamp + event->time_delta;
4399 ring_buffer_normalize_time_stamp(cpu_buffer->buffer,
4400 cpu_buffer->cpu, ts);
4403 *lost_events = rb_lost_events(cpu_buffer);
4407 RB_WARN_ON(cpu_buffer, 1);
4412 EXPORT_SYMBOL_GPL(ring_buffer_peek);
4414 static struct ring_buffer_event *
4415 rb_iter_peek(struct ring_buffer_iter *iter, u64 *ts)
4417 struct trace_buffer *buffer;
4418 struct ring_buffer_per_cpu *cpu_buffer;
4419 struct ring_buffer_event *event;
4425 cpu_buffer = iter->cpu_buffer;
4426 buffer = cpu_buffer->buffer;
4429 * Check if someone performed a consuming read to
4430 * the buffer. A consuming read invalidates the iterator
4431 * and we need to reset the iterator in this case.
4433 if (unlikely(iter->cache_read != cpu_buffer->read ||
4434 iter->cache_reader_page != cpu_buffer->reader_page))
4435 rb_iter_reset(iter);
4438 if (ring_buffer_iter_empty(iter))
4442 * As the writer can mess with what the iterator is trying
4443 * to read, just give up if we fail to get an event after
4444 * three tries. The iterator is not as reliable when reading
4445 * the ring buffer with an active write as the consumer is.
4446 * Do not warn if the three failures is reached.
4451 if (rb_per_cpu_empty(cpu_buffer))
4454 if (iter->head >= rb_page_size(iter->head_page)) {
4459 event = rb_iter_head_event(iter);
4463 switch (event->type_len) {
4464 case RINGBUF_TYPE_PADDING:
4465 if (rb_null_event(event)) {
4469 rb_advance_iter(iter);
4472 case RINGBUF_TYPE_TIME_EXTEND:
4473 /* Internal data, OK to advance */
4474 rb_advance_iter(iter);
4477 case RINGBUF_TYPE_TIME_STAMP:
4479 *ts = ring_buffer_event_time_stamp(event);
4480 ring_buffer_normalize_time_stamp(cpu_buffer->buffer,
4481 cpu_buffer->cpu, ts);
4483 /* Internal data, OK to advance */
4484 rb_advance_iter(iter);
4487 case RINGBUF_TYPE_DATA:
4489 *ts = iter->read_stamp + event->time_delta;
4490 ring_buffer_normalize_time_stamp(buffer,
4491 cpu_buffer->cpu, ts);
4496 RB_WARN_ON(cpu_buffer, 1);
4501 EXPORT_SYMBOL_GPL(ring_buffer_iter_peek);
4503 static inline bool rb_reader_lock(struct ring_buffer_per_cpu *cpu_buffer)
4505 if (likely(!in_nmi())) {
4506 raw_spin_lock(&cpu_buffer->reader_lock);
4511 * If an NMI die dumps out the content of the ring buffer
4512 * trylock must be used to prevent a deadlock if the NMI
4513 * preempted a task that holds the ring buffer locks. If
4514 * we get the lock then all is fine, if not, then continue
4515 * to do the read, but this can corrupt the ring buffer,
4516 * so it must be permanently disabled from future writes.
4517 * Reading from NMI is a oneshot deal.
4519 if (raw_spin_trylock(&cpu_buffer->reader_lock))
4522 /* Continue without locking, but disable the ring buffer */
4523 atomic_inc(&cpu_buffer->record_disabled);
4528 rb_reader_unlock(struct ring_buffer_per_cpu *cpu_buffer, bool locked)
4531 raw_spin_unlock(&cpu_buffer->reader_lock);
4536 * ring_buffer_peek - peek at the next event to be read
4537 * @buffer: The ring buffer to read
4538 * @cpu: The cpu to peak at
4539 * @ts: The timestamp counter of this event.
4540 * @lost_events: a variable to store if events were lost (may be NULL)
4542 * This will return the event that will be read next, but does
4543 * not consume the data.
4545 struct ring_buffer_event *
4546 ring_buffer_peek(struct trace_buffer *buffer, int cpu, u64 *ts,
4547 unsigned long *lost_events)
4549 struct ring_buffer_per_cpu *cpu_buffer = buffer->buffers[cpu];
4550 struct ring_buffer_event *event;
4551 unsigned long flags;
4554 if (!cpumask_test_cpu(cpu, buffer->cpumask))
4558 local_irq_save(flags);
4559 dolock = rb_reader_lock(cpu_buffer);
4560 event = rb_buffer_peek(cpu_buffer, ts, lost_events);
4561 if (event && event->type_len == RINGBUF_TYPE_PADDING)
4562 rb_advance_reader(cpu_buffer);
4563 rb_reader_unlock(cpu_buffer, dolock);
4564 local_irq_restore(flags);
4566 if (event && event->type_len == RINGBUF_TYPE_PADDING)
4572 /** ring_buffer_iter_dropped - report if there are dropped events
4573 * @iter: The ring buffer iterator
4575 * Returns true if there was dropped events since the last peek.
4577 bool ring_buffer_iter_dropped(struct ring_buffer_iter *iter)
4579 bool ret = iter->missed_events != 0;
4581 iter->missed_events = 0;
4584 EXPORT_SYMBOL_GPL(ring_buffer_iter_dropped);
4587 * ring_buffer_iter_peek - peek at the next event to be read
4588 * @iter: The ring buffer iterator
4589 * @ts: The timestamp counter of this event.
4591 * This will return the event that will be read next, but does
4592 * not increment the iterator.
4594 struct ring_buffer_event *
4595 ring_buffer_iter_peek(struct ring_buffer_iter *iter, u64 *ts)
4597 struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
4598 struct ring_buffer_event *event;
4599 unsigned long flags;
4602 raw_spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
4603 event = rb_iter_peek(iter, ts);
4604 raw_spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
4606 if (event && event->type_len == RINGBUF_TYPE_PADDING)
4613 * ring_buffer_consume - return an event and consume it
4614 * @buffer: The ring buffer to get the next event from
4615 * @cpu: the cpu to read the buffer from
4616 * @ts: a variable to store the timestamp (may be NULL)
4617 * @lost_events: a variable to store if events were lost (may be NULL)
4619 * Returns the next event in the ring buffer, and that event is consumed.
4620 * Meaning, that sequential reads will keep returning a different event,
4621 * and eventually empty the ring buffer if the producer is slower.
4623 struct ring_buffer_event *
4624 ring_buffer_consume(struct trace_buffer *buffer, int cpu, u64 *ts,
4625 unsigned long *lost_events)
4627 struct ring_buffer_per_cpu *cpu_buffer;
4628 struct ring_buffer_event *event = NULL;
4629 unsigned long flags;
4633 /* might be called in atomic */
4636 if (!cpumask_test_cpu(cpu, buffer->cpumask))
4639 cpu_buffer = buffer->buffers[cpu];
4640 local_irq_save(flags);
4641 dolock = rb_reader_lock(cpu_buffer);
4643 event = rb_buffer_peek(cpu_buffer, ts, lost_events);
4645 cpu_buffer->lost_events = 0;
4646 rb_advance_reader(cpu_buffer);
4649 rb_reader_unlock(cpu_buffer, dolock);
4650 local_irq_restore(flags);
4655 if (event && event->type_len == RINGBUF_TYPE_PADDING)
4660 EXPORT_SYMBOL_GPL(ring_buffer_consume);
4663 * ring_buffer_read_prepare - Prepare for a non consuming read of the buffer
4664 * @buffer: The ring buffer to read from
4665 * @cpu: The cpu buffer to iterate over
4666 * @flags: gfp flags to use for memory allocation
4668 * This performs the initial preparations necessary to iterate
4669 * through the buffer. Memory is allocated, buffer recording
4670 * is disabled, and the iterator pointer is returned to the caller.
4672 * Disabling buffer recording prevents the reading from being
4673 * corrupted. This is not a consuming read, so a producer is not
4676 * After a sequence of ring_buffer_read_prepare calls, the user is
4677 * expected to make at least one call to ring_buffer_read_prepare_sync.
4678 * Afterwards, ring_buffer_read_start is invoked to get things going
4681 * This overall must be paired with ring_buffer_read_finish.
4683 struct ring_buffer_iter *
4684 ring_buffer_read_prepare(struct trace_buffer *buffer, int cpu, gfp_t flags)
4686 struct ring_buffer_per_cpu *cpu_buffer;
4687 struct ring_buffer_iter *iter;
4689 if (!cpumask_test_cpu(cpu, buffer->cpumask))
4692 iter = kzalloc(sizeof(*iter), flags);
4696 iter->event = kmalloc(BUF_MAX_DATA_SIZE, flags);
4702 cpu_buffer = buffer->buffers[cpu];
4704 iter->cpu_buffer = cpu_buffer;
4706 atomic_inc(&cpu_buffer->resize_disabled);
4710 EXPORT_SYMBOL_GPL(ring_buffer_read_prepare);
4713 * ring_buffer_read_prepare_sync - Synchronize a set of prepare calls
4715 * All previously invoked ring_buffer_read_prepare calls to prepare
4716 * iterators will be synchronized. Afterwards, read_buffer_read_start
4717 * calls on those iterators are allowed.
4720 ring_buffer_read_prepare_sync(void)
4724 EXPORT_SYMBOL_GPL(ring_buffer_read_prepare_sync);
4727 * ring_buffer_read_start - start a non consuming read of the buffer
4728 * @iter: The iterator returned by ring_buffer_read_prepare
4730 * This finalizes the startup of an iteration through the buffer.
4731 * The iterator comes from a call to ring_buffer_read_prepare and
4732 * an intervening ring_buffer_read_prepare_sync must have been
4735 * Must be paired with ring_buffer_read_finish.
4738 ring_buffer_read_start(struct ring_buffer_iter *iter)
4740 struct ring_buffer_per_cpu *cpu_buffer;
4741 unsigned long flags;
4746 cpu_buffer = iter->cpu_buffer;
4748 raw_spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
4749 arch_spin_lock(&cpu_buffer->lock);
4750 rb_iter_reset(iter);
4751 arch_spin_unlock(&cpu_buffer->lock);
4752 raw_spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
4754 EXPORT_SYMBOL_GPL(ring_buffer_read_start);
4757 * ring_buffer_read_finish - finish reading the iterator of the buffer
4758 * @iter: The iterator retrieved by ring_buffer_start
4760 * This re-enables the recording to the buffer, and frees the
4764 ring_buffer_read_finish(struct ring_buffer_iter *iter)
4766 struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
4767 unsigned long flags;
4770 * Ring buffer is disabled from recording, here's a good place
4771 * to check the integrity of the ring buffer.
4772 * Must prevent readers from trying to read, as the check
4773 * clears the HEAD page and readers require it.
4775 raw_spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
4776 rb_check_pages(cpu_buffer);
4777 raw_spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
4779 atomic_dec(&cpu_buffer->resize_disabled);
4783 EXPORT_SYMBOL_GPL(ring_buffer_read_finish);
4786 * ring_buffer_iter_advance - advance the iterator to the next location
4787 * @iter: The ring buffer iterator
4789 * Move the location of the iterator such that the next read will
4790 * be the next location of the iterator.
4792 void ring_buffer_iter_advance(struct ring_buffer_iter *iter)
4794 struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
4795 unsigned long flags;
4797 raw_spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
4799 rb_advance_iter(iter);
4801 raw_spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
4803 EXPORT_SYMBOL_GPL(ring_buffer_iter_advance);
4806 * ring_buffer_size - return the size of the ring buffer (in bytes)
4807 * @buffer: The ring buffer.
4808 * @cpu: The CPU to get ring buffer size from.
4810 unsigned long ring_buffer_size(struct trace_buffer *buffer, int cpu)
4813 * Earlier, this method returned
4814 * BUF_PAGE_SIZE * buffer->nr_pages
4815 * Since the nr_pages field is now removed, we have converted this to
4816 * return the per cpu buffer value.
4818 if (!cpumask_test_cpu(cpu, buffer->cpumask))
4821 return BUF_PAGE_SIZE * buffer->buffers[cpu]->nr_pages;
4823 EXPORT_SYMBOL_GPL(ring_buffer_size);
4826 rb_reset_cpu(struct ring_buffer_per_cpu *cpu_buffer)
4828 rb_head_page_deactivate(cpu_buffer);
4830 cpu_buffer->head_page
4831 = list_entry(cpu_buffer->pages, struct buffer_page, list);
4832 local_set(&cpu_buffer->head_page->write, 0);
4833 local_set(&cpu_buffer->head_page->entries, 0);
4834 local_set(&cpu_buffer->head_page->page->commit, 0);
4836 cpu_buffer->head_page->read = 0;
4838 cpu_buffer->tail_page = cpu_buffer->head_page;
4839 cpu_buffer->commit_page = cpu_buffer->head_page;
4841 INIT_LIST_HEAD(&cpu_buffer->reader_page->list);
4842 INIT_LIST_HEAD(&cpu_buffer->new_pages);
4843 local_set(&cpu_buffer->reader_page->write, 0);
4844 local_set(&cpu_buffer->reader_page->entries, 0);
4845 local_set(&cpu_buffer->reader_page->page->commit, 0);
4846 cpu_buffer->reader_page->read = 0;
4848 local_set(&cpu_buffer->entries_bytes, 0);
4849 local_set(&cpu_buffer->overrun, 0);
4850 local_set(&cpu_buffer->commit_overrun, 0);
4851 local_set(&cpu_buffer->dropped_events, 0);
4852 local_set(&cpu_buffer->entries, 0);
4853 local_set(&cpu_buffer->committing, 0);
4854 local_set(&cpu_buffer->commits, 0);
4855 local_set(&cpu_buffer->pages_touched, 0);
4856 local_set(&cpu_buffer->pages_read, 0);
4857 cpu_buffer->last_pages_touch = 0;
4858 cpu_buffer->shortest_full = 0;
4859 cpu_buffer->read = 0;
4860 cpu_buffer->read_bytes = 0;
4862 rb_time_set(&cpu_buffer->write_stamp, 0);
4863 rb_time_set(&cpu_buffer->before_stamp, 0);
4865 cpu_buffer->lost_events = 0;
4866 cpu_buffer->last_overrun = 0;
4868 rb_head_page_activate(cpu_buffer);
4871 /* Must have disabled the cpu buffer then done a synchronize_rcu */
4872 static void reset_disabled_cpu_buffer(struct ring_buffer_per_cpu *cpu_buffer)
4874 unsigned long flags;
4876 raw_spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
4878 if (RB_WARN_ON(cpu_buffer, local_read(&cpu_buffer->committing)))
4881 arch_spin_lock(&cpu_buffer->lock);
4883 rb_reset_cpu(cpu_buffer);
4885 arch_spin_unlock(&cpu_buffer->lock);
4888 raw_spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
4892 * ring_buffer_reset_cpu - reset a ring buffer per CPU buffer
4893 * @buffer: The ring buffer to reset a per cpu buffer of
4894 * @cpu: The CPU buffer to be reset
4896 void ring_buffer_reset_cpu(struct trace_buffer *buffer, int cpu)
4898 struct ring_buffer_per_cpu *cpu_buffer = buffer->buffers[cpu];
4900 if (!cpumask_test_cpu(cpu, buffer->cpumask))
4903 /* prevent another thread from changing buffer sizes */
4904 mutex_lock(&buffer->mutex);
4906 atomic_inc(&cpu_buffer->resize_disabled);
4907 atomic_inc(&cpu_buffer->record_disabled);
4909 /* Make sure all commits have finished */
4912 reset_disabled_cpu_buffer(cpu_buffer);
4914 atomic_dec(&cpu_buffer->record_disabled);
4915 atomic_dec(&cpu_buffer->resize_disabled);
4917 mutex_unlock(&buffer->mutex);
4919 EXPORT_SYMBOL_GPL(ring_buffer_reset_cpu);
4922 * ring_buffer_reset_cpu - reset a ring buffer per CPU buffer
4923 * @buffer: The ring buffer to reset a per cpu buffer of
4924 * @cpu: The CPU buffer to be reset
4926 void ring_buffer_reset_online_cpus(struct trace_buffer *buffer)
4928 struct ring_buffer_per_cpu *cpu_buffer;
4931 /* prevent another thread from changing buffer sizes */
4932 mutex_lock(&buffer->mutex);
4934 for_each_online_buffer_cpu(buffer, cpu) {
4935 cpu_buffer = buffer->buffers[cpu];
4937 atomic_inc(&cpu_buffer->resize_disabled);
4938 atomic_inc(&cpu_buffer->record_disabled);
4941 /* Make sure all commits have finished */
4944 for_each_online_buffer_cpu(buffer, cpu) {
4945 cpu_buffer = buffer->buffers[cpu];
4947 reset_disabled_cpu_buffer(cpu_buffer);
4949 atomic_dec(&cpu_buffer->record_disabled);
4950 atomic_dec(&cpu_buffer->resize_disabled);
4953 mutex_unlock(&buffer->mutex);
4957 * ring_buffer_reset - reset a ring buffer
4958 * @buffer: The ring buffer to reset all cpu buffers
4960 void ring_buffer_reset(struct trace_buffer *buffer)
4962 struct ring_buffer_per_cpu *cpu_buffer;
4965 for_each_buffer_cpu(buffer, cpu) {
4966 cpu_buffer = buffer->buffers[cpu];
4968 atomic_inc(&cpu_buffer->resize_disabled);
4969 atomic_inc(&cpu_buffer->record_disabled);
4972 /* Make sure all commits have finished */
4975 for_each_buffer_cpu(buffer, cpu) {
4976 cpu_buffer = buffer->buffers[cpu];
4978 reset_disabled_cpu_buffer(cpu_buffer);
4980 atomic_dec(&cpu_buffer->record_disabled);
4981 atomic_dec(&cpu_buffer->resize_disabled);
4984 EXPORT_SYMBOL_GPL(ring_buffer_reset);
4987 * rind_buffer_empty - is the ring buffer empty?
4988 * @buffer: The ring buffer to test
4990 bool ring_buffer_empty(struct trace_buffer *buffer)
4992 struct ring_buffer_per_cpu *cpu_buffer;
4993 unsigned long flags;
4998 /* yes this is racy, but if you don't like the race, lock the buffer */
4999 for_each_buffer_cpu(buffer, cpu) {
5000 cpu_buffer = buffer->buffers[cpu];
5001 local_irq_save(flags);
5002 dolock = rb_reader_lock(cpu_buffer);
5003 ret = rb_per_cpu_empty(cpu_buffer);
5004 rb_reader_unlock(cpu_buffer, dolock);
5005 local_irq_restore(flags);
5013 EXPORT_SYMBOL_GPL(ring_buffer_empty);
5016 * ring_buffer_empty_cpu - is a cpu buffer of a ring buffer empty?
5017 * @buffer: The ring buffer
5018 * @cpu: The CPU buffer to test
5020 bool ring_buffer_empty_cpu(struct trace_buffer *buffer, int cpu)
5022 struct ring_buffer_per_cpu *cpu_buffer;
5023 unsigned long flags;
5027 if (!cpumask_test_cpu(cpu, buffer->cpumask))
5030 cpu_buffer = buffer->buffers[cpu];
5031 local_irq_save(flags);
5032 dolock = rb_reader_lock(cpu_buffer);
5033 ret = rb_per_cpu_empty(cpu_buffer);
5034 rb_reader_unlock(cpu_buffer, dolock);
5035 local_irq_restore(flags);
5039 EXPORT_SYMBOL_GPL(ring_buffer_empty_cpu);
5041 #ifdef CONFIG_RING_BUFFER_ALLOW_SWAP
5043 * ring_buffer_swap_cpu - swap a CPU buffer between two ring buffers
5044 * @buffer_a: One buffer to swap with
5045 * @buffer_b: The other buffer to swap with
5046 * @cpu: the CPU of the buffers to swap
5048 * This function is useful for tracers that want to take a "snapshot"
5049 * of a CPU buffer and has another back up buffer lying around.
5050 * it is expected that the tracer handles the cpu buffer not being
5051 * used at the moment.
5053 int ring_buffer_swap_cpu(struct trace_buffer *buffer_a,
5054 struct trace_buffer *buffer_b, int cpu)
5056 struct ring_buffer_per_cpu *cpu_buffer_a;
5057 struct ring_buffer_per_cpu *cpu_buffer_b;
5060 if (!cpumask_test_cpu(cpu, buffer_a->cpumask) ||
5061 !cpumask_test_cpu(cpu, buffer_b->cpumask))
5064 cpu_buffer_a = buffer_a->buffers[cpu];
5065 cpu_buffer_b = buffer_b->buffers[cpu];
5067 /* At least make sure the two buffers are somewhat the same */
5068 if (cpu_buffer_a->nr_pages != cpu_buffer_b->nr_pages)
5073 if (atomic_read(&buffer_a->record_disabled))
5076 if (atomic_read(&buffer_b->record_disabled))
5079 if (atomic_read(&cpu_buffer_a->record_disabled))
5082 if (atomic_read(&cpu_buffer_b->record_disabled))
5086 * We can't do a synchronize_rcu here because this
5087 * function can be called in atomic context.
5088 * Normally this will be called from the same CPU as cpu.
5089 * If not it's up to the caller to protect this.
5091 atomic_inc(&cpu_buffer_a->record_disabled);
5092 atomic_inc(&cpu_buffer_b->record_disabled);
5095 if (local_read(&cpu_buffer_a->committing))
5097 if (local_read(&cpu_buffer_b->committing))
5100 buffer_a->buffers[cpu] = cpu_buffer_b;
5101 buffer_b->buffers[cpu] = cpu_buffer_a;
5103 cpu_buffer_b->buffer = buffer_a;
5104 cpu_buffer_a->buffer = buffer_b;
5109 atomic_dec(&cpu_buffer_a->record_disabled);
5110 atomic_dec(&cpu_buffer_b->record_disabled);
5114 EXPORT_SYMBOL_GPL(ring_buffer_swap_cpu);
5115 #endif /* CONFIG_RING_BUFFER_ALLOW_SWAP */
5118 * ring_buffer_alloc_read_page - allocate a page to read from buffer
5119 * @buffer: the buffer to allocate for.
5120 * @cpu: the cpu buffer to allocate.
5122 * This function is used in conjunction with ring_buffer_read_page.
5123 * When reading a full page from the ring buffer, these functions
5124 * can be used to speed up the process. The calling function should
5125 * allocate a few pages first with this function. Then when it
5126 * needs to get pages from the ring buffer, it passes the result
5127 * of this function into ring_buffer_read_page, which will swap
5128 * the page that was allocated, with the read page of the buffer.
5131 * The page allocated, or ERR_PTR
5133 void *ring_buffer_alloc_read_page(struct trace_buffer *buffer, int cpu)
5135 struct ring_buffer_per_cpu *cpu_buffer;
5136 struct buffer_data_page *bpage = NULL;
5137 unsigned long flags;
5140 if (!cpumask_test_cpu(cpu, buffer->cpumask))
5141 return ERR_PTR(-ENODEV);
5143 cpu_buffer = buffer->buffers[cpu];
5144 local_irq_save(flags);
5145 arch_spin_lock(&cpu_buffer->lock);
5147 if (cpu_buffer->free_page) {
5148 bpage = cpu_buffer->free_page;
5149 cpu_buffer->free_page = NULL;
5152 arch_spin_unlock(&cpu_buffer->lock);
5153 local_irq_restore(flags);
5158 page = alloc_pages_node(cpu_to_node(cpu),
5159 GFP_KERNEL | __GFP_NORETRY, 0);
5161 return ERR_PTR(-ENOMEM);
5163 bpage = page_address(page);
5166 rb_init_page(bpage);
5170 EXPORT_SYMBOL_GPL(ring_buffer_alloc_read_page);
5173 * ring_buffer_free_read_page - free an allocated read page
5174 * @buffer: the buffer the page was allocate for
5175 * @cpu: the cpu buffer the page came from
5176 * @data: the page to free
5178 * Free a page allocated from ring_buffer_alloc_read_page.
5180 void ring_buffer_free_read_page(struct trace_buffer *buffer, int cpu, void *data)
5182 struct ring_buffer_per_cpu *cpu_buffer = buffer->buffers[cpu];
5183 struct buffer_data_page *bpage = data;
5184 struct page *page = virt_to_page(bpage);
5185 unsigned long flags;
5187 /* If the page is still in use someplace else, we can't reuse it */
5188 if (page_ref_count(page) > 1)
5191 local_irq_save(flags);
5192 arch_spin_lock(&cpu_buffer->lock);
5194 if (!cpu_buffer->free_page) {
5195 cpu_buffer->free_page = bpage;
5199 arch_spin_unlock(&cpu_buffer->lock);
5200 local_irq_restore(flags);
5203 free_page((unsigned long)bpage);
5205 EXPORT_SYMBOL_GPL(ring_buffer_free_read_page);
5208 * ring_buffer_read_page - extract a page from the ring buffer
5209 * @buffer: buffer to extract from
5210 * @data_page: the page to use allocated from ring_buffer_alloc_read_page
5211 * @len: amount to extract
5212 * @cpu: the cpu of the buffer to extract
5213 * @full: should the extraction only happen when the page is full.
5215 * This function will pull out a page from the ring buffer and consume it.
5216 * @data_page must be the address of the variable that was returned
5217 * from ring_buffer_alloc_read_page. This is because the page might be used
5218 * to swap with a page in the ring buffer.
5221 * rpage = ring_buffer_alloc_read_page(buffer, cpu);
5222 * if (IS_ERR(rpage))
5223 * return PTR_ERR(rpage);
5224 * ret = ring_buffer_read_page(buffer, &rpage, len, cpu, 0);
5226 * process_page(rpage, ret);
5228 * When @full is set, the function will not return true unless
5229 * the writer is off the reader page.
5231 * Note: it is up to the calling functions to handle sleeps and wakeups.
5232 * The ring buffer can be used anywhere in the kernel and can not
5233 * blindly call wake_up. The layer that uses the ring buffer must be
5234 * responsible for that.
5237 * >=0 if data has been transferred, returns the offset of consumed data.
5238 * <0 if no data has been transferred.
5240 int ring_buffer_read_page(struct trace_buffer *buffer,
5241 void **data_page, size_t len, int cpu, int full)
5243 struct ring_buffer_per_cpu *cpu_buffer = buffer->buffers[cpu];
5244 struct ring_buffer_event *event;
5245 struct buffer_data_page *bpage;
5246 struct buffer_page *reader;
5247 unsigned long missed_events;
5248 unsigned long flags;
5249 unsigned int commit;
5254 if (!cpumask_test_cpu(cpu, buffer->cpumask))
5258 * If len is not big enough to hold the page header, then
5259 * we can not copy anything.
5261 if (len <= BUF_PAGE_HDR_SIZE)
5264 len -= BUF_PAGE_HDR_SIZE;
5273 raw_spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
5275 reader = rb_get_reader_page(cpu_buffer);
5279 event = rb_reader_event(cpu_buffer);
5281 read = reader->read;
5282 commit = rb_page_commit(reader);
5284 /* Check if any events were dropped */
5285 missed_events = cpu_buffer->lost_events;
5288 * If this page has been partially read or
5289 * if len is not big enough to read the rest of the page or
5290 * a writer is still on the page, then
5291 * we must copy the data from the page to the buffer.
5292 * Otherwise, we can simply swap the page with the one passed in.
5294 if (read || (len < (commit - read)) ||
5295 cpu_buffer->reader_page == cpu_buffer->commit_page) {
5296 struct buffer_data_page *rpage = cpu_buffer->reader_page->page;
5297 unsigned int rpos = read;
5298 unsigned int pos = 0;
5304 if (len > (commit - read))
5305 len = (commit - read);
5307 /* Always keep the time extend and data together */
5308 size = rb_event_ts_length(event);
5313 /* save the current timestamp, since the user will need it */
5314 save_timestamp = cpu_buffer->read_stamp;
5316 /* Need to copy one event at a time */
5318 /* We need the size of one event, because
5319 * rb_advance_reader only advances by one event,
5320 * whereas rb_event_ts_length may include the size of
5321 * one or two events.
5322 * We have already ensured there's enough space if this
5323 * is a time extend. */
5324 size = rb_event_length(event);
5325 memcpy(bpage->data + pos, rpage->data + rpos, size);
5329 rb_advance_reader(cpu_buffer);
5330 rpos = reader->read;
5336 event = rb_reader_event(cpu_buffer);
5337 /* Always keep the time extend and data together */
5338 size = rb_event_ts_length(event);
5339 } while (len >= size);
5342 local_set(&bpage->commit, pos);
5343 bpage->time_stamp = save_timestamp;
5345 /* we copied everything to the beginning */
5348 /* update the entry counter */
5349 cpu_buffer->read += rb_page_entries(reader);
5350 cpu_buffer->read_bytes += BUF_PAGE_SIZE;
5352 /* swap the pages */
5353 rb_init_page(bpage);
5354 bpage = reader->page;
5355 reader->page = *data_page;
5356 local_set(&reader->write, 0);
5357 local_set(&reader->entries, 0);
5362 * Use the real_end for the data size,
5363 * This gives us a chance to store the lost events
5366 if (reader->real_end)
5367 local_set(&bpage->commit, reader->real_end);
5371 cpu_buffer->lost_events = 0;
5373 commit = local_read(&bpage->commit);
5375 * Set a flag in the commit field if we lost events
5377 if (missed_events) {
5378 /* If there is room at the end of the page to save the
5379 * missed events, then record it there.
5381 if (BUF_PAGE_SIZE - commit >= sizeof(missed_events)) {
5382 memcpy(&bpage->data[commit], &missed_events,
5383 sizeof(missed_events));
5384 local_add(RB_MISSED_STORED, &bpage->commit);
5385 commit += sizeof(missed_events);
5387 local_add(RB_MISSED_EVENTS, &bpage->commit);
5391 * This page may be off to user land. Zero it out here.
5393 if (commit < BUF_PAGE_SIZE)
5394 memset(&bpage->data[commit], 0, BUF_PAGE_SIZE - commit);
5397 raw_spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
5402 EXPORT_SYMBOL_GPL(ring_buffer_read_page);
5405 * We only allocate new buffers, never free them if the CPU goes down.
5406 * If we were to free the buffer, then the user would lose any trace that was in
5409 int trace_rb_cpu_prepare(unsigned int cpu, struct hlist_node *node)
5411 struct trace_buffer *buffer;
5414 unsigned long nr_pages;
5416 buffer = container_of(node, struct trace_buffer, node);
5417 if (cpumask_test_cpu(cpu, buffer->cpumask))
5422 /* check if all cpu sizes are same */
5423 for_each_buffer_cpu(buffer, cpu_i) {
5424 /* fill in the size from first enabled cpu */
5426 nr_pages = buffer->buffers[cpu_i]->nr_pages;
5427 if (nr_pages != buffer->buffers[cpu_i]->nr_pages) {
5432 /* allocate minimum pages, user can later expand it */
5435 buffer->buffers[cpu] =
5436 rb_allocate_cpu_buffer(buffer, nr_pages, cpu);
5437 if (!buffer->buffers[cpu]) {
5438 WARN(1, "failed to allocate ring buffer on CPU %u\n",
5443 cpumask_set_cpu(cpu, buffer->cpumask);
5447 #ifdef CONFIG_RING_BUFFER_STARTUP_TEST
5449 * This is a basic integrity check of the ring buffer.
5450 * Late in the boot cycle this test will run when configured in.
5451 * It will kick off a thread per CPU that will go into a loop
5452 * writing to the per cpu ring buffer various sizes of data.
5453 * Some of the data will be large items, some small.
5455 * Another thread is created that goes into a spin, sending out
5456 * IPIs to the other CPUs to also write into the ring buffer.
5457 * this is to test the nesting ability of the buffer.
5459 * Basic stats are recorded and reported. If something in the
5460 * ring buffer should happen that's not expected, a big warning
5461 * is displayed and all ring buffers are disabled.
5463 static struct task_struct *rb_threads[NR_CPUS] __initdata;
5465 struct rb_test_data {
5466 struct trace_buffer *buffer;
5467 unsigned long events;
5468 unsigned long bytes_written;
5469 unsigned long bytes_alloc;
5470 unsigned long bytes_dropped;
5471 unsigned long events_nested;
5472 unsigned long bytes_written_nested;
5473 unsigned long bytes_alloc_nested;
5474 unsigned long bytes_dropped_nested;
5475 int min_size_nested;
5476 int max_size_nested;
5483 static struct rb_test_data rb_data[NR_CPUS] __initdata;
5486 #define RB_TEST_BUFFER_SIZE 1048576
5488 static char rb_string[] __initdata =
5489 "abcdefghijklmnopqrstuvwxyz1234567890!@#$%^&*()?+\\"
5490 "?+|:';\",.<>/?abcdefghijklmnopqrstuvwxyz1234567890"
5491 "!@#$%^&*()?+\\?+|:';\",.<>/?abcdefghijklmnopqrstuv";
5493 static bool rb_test_started __initdata;
5500 static __init int rb_write_something(struct rb_test_data *data, bool nested)
5502 struct ring_buffer_event *event;
5503 struct rb_item *item;
5510 /* Have nested writes different that what is written */
5511 cnt = data->cnt + (nested ? 27 : 0);
5513 /* Multiply cnt by ~e, to make some unique increment */
5514 size = (cnt * 68 / 25) % (sizeof(rb_string) - 1);
5516 len = size + sizeof(struct rb_item);
5518 started = rb_test_started;
5519 /* read rb_test_started before checking buffer enabled */
5522 event = ring_buffer_lock_reserve(data->buffer, len);
5524 /* Ignore dropped events before test starts. */
5527 data->bytes_dropped += len;
5529 data->bytes_dropped_nested += len;
5534 event_len = ring_buffer_event_length(event);
5536 if (RB_WARN_ON(data->buffer, event_len < len))
5539 item = ring_buffer_event_data(event);
5541 memcpy(item->str, rb_string, size);
5544 data->bytes_alloc_nested += event_len;
5545 data->bytes_written_nested += len;
5546 data->events_nested++;
5547 if (!data->min_size_nested || len < data->min_size_nested)
5548 data->min_size_nested = len;
5549 if (len > data->max_size_nested)
5550 data->max_size_nested = len;
5552 data->bytes_alloc += event_len;
5553 data->bytes_written += len;
5555 if (!data->min_size || len < data->min_size)
5556 data->max_size = len;
5557 if (len > data->max_size)
5558 data->max_size = len;
5562 ring_buffer_unlock_commit(data->buffer, event);
5567 static __init int rb_test(void *arg)
5569 struct rb_test_data *data = arg;
5571 while (!kthread_should_stop()) {
5572 rb_write_something(data, false);
5575 set_current_state(TASK_INTERRUPTIBLE);
5576 /* Now sleep between a min of 100-300us and a max of 1ms */
5577 usleep_range(((data->cnt % 3) + 1) * 100, 1000);
5583 static __init void rb_ipi(void *ignore)
5585 struct rb_test_data *data;
5586 int cpu = smp_processor_id();
5588 data = &rb_data[cpu];
5589 rb_write_something(data, true);
5592 static __init int rb_hammer_test(void *arg)
5594 while (!kthread_should_stop()) {
5596 /* Send an IPI to all cpus to write data! */
5597 smp_call_function(rb_ipi, NULL, 1);
5598 /* No sleep, but for non preempt, let others run */
5605 static __init int test_ringbuffer(void)
5607 struct task_struct *rb_hammer;
5608 struct trace_buffer *buffer;
5612 if (security_locked_down(LOCKDOWN_TRACEFS)) {
5613 pr_warn("Lockdown is enabled, skipping ring buffer tests\n");
5617 pr_info("Running ring buffer tests...\n");
5619 buffer = ring_buffer_alloc(RB_TEST_BUFFER_SIZE, RB_FL_OVERWRITE);
5620 if (WARN_ON(!buffer))
5623 /* Disable buffer so that threads can't write to it yet */
5624 ring_buffer_record_off(buffer);
5626 for_each_online_cpu(cpu) {
5627 rb_data[cpu].buffer = buffer;
5628 rb_data[cpu].cpu = cpu;
5629 rb_data[cpu].cnt = cpu;
5630 rb_threads[cpu] = kthread_create(rb_test, &rb_data[cpu],
5631 "rbtester/%d", cpu);
5632 if (WARN_ON(IS_ERR(rb_threads[cpu]))) {
5633 pr_cont("FAILED\n");
5634 ret = PTR_ERR(rb_threads[cpu]);
5638 kthread_bind(rb_threads[cpu], cpu);
5639 wake_up_process(rb_threads[cpu]);
5642 /* Now create the rb hammer! */
5643 rb_hammer = kthread_run(rb_hammer_test, NULL, "rbhammer");
5644 if (WARN_ON(IS_ERR(rb_hammer))) {
5645 pr_cont("FAILED\n");
5646 ret = PTR_ERR(rb_hammer);
5650 ring_buffer_record_on(buffer);
5652 * Show buffer is enabled before setting rb_test_started.
5653 * Yes there's a small race window where events could be
5654 * dropped and the thread wont catch it. But when a ring
5655 * buffer gets enabled, there will always be some kind of
5656 * delay before other CPUs see it. Thus, we don't care about
5657 * those dropped events. We care about events dropped after
5658 * the threads see that the buffer is active.
5661 rb_test_started = true;
5663 set_current_state(TASK_INTERRUPTIBLE);
5664 /* Just run for 10 seconds */;
5665 schedule_timeout(10 * HZ);
5667 kthread_stop(rb_hammer);
5670 for_each_online_cpu(cpu) {
5671 if (!rb_threads[cpu])
5673 kthread_stop(rb_threads[cpu]);
5676 ring_buffer_free(buffer);
5681 pr_info("finished\n");
5682 for_each_online_cpu(cpu) {
5683 struct ring_buffer_event *event;
5684 struct rb_test_data *data = &rb_data[cpu];
5685 struct rb_item *item;
5686 unsigned long total_events;
5687 unsigned long total_dropped;
5688 unsigned long total_written;
5689 unsigned long total_alloc;
5690 unsigned long total_read = 0;
5691 unsigned long total_size = 0;
5692 unsigned long total_len = 0;
5693 unsigned long total_lost = 0;
5696 int small_event_size;
5700 total_events = data->events + data->events_nested;
5701 total_written = data->bytes_written + data->bytes_written_nested;
5702 total_alloc = data->bytes_alloc + data->bytes_alloc_nested;
5703 total_dropped = data->bytes_dropped + data->bytes_dropped_nested;
5705 big_event_size = data->max_size + data->max_size_nested;
5706 small_event_size = data->min_size + data->min_size_nested;
5708 pr_info("CPU %d:\n", cpu);
5709 pr_info(" events: %ld\n", total_events);
5710 pr_info(" dropped bytes: %ld\n", total_dropped);
5711 pr_info(" alloced bytes: %ld\n", total_alloc);
5712 pr_info(" written bytes: %ld\n", total_written);
5713 pr_info(" biggest event: %d\n", big_event_size);
5714 pr_info(" smallest event: %d\n", small_event_size);
5716 if (RB_WARN_ON(buffer, total_dropped))
5721 while ((event = ring_buffer_consume(buffer, cpu, NULL, &lost))) {
5723 item = ring_buffer_event_data(event);
5724 total_len += ring_buffer_event_length(event);
5725 total_size += item->size + sizeof(struct rb_item);
5726 if (memcmp(&item->str[0], rb_string, item->size) != 0) {
5727 pr_info("FAILED!\n");
5728 pr_info("buffer had: %.*s\n", item->size, item->str);
5729 pr_info("expected: %.*s\n", item->size, rb_string);
5730 RB_WARN_ON(buffer, 1);
5741 pr_info(" read events: %ld\n", total_read);
5742 pr_info(" lost events: %ld\n", total_lost);
5743 pr_info(" total events: %ld\n", total_lost + total_read);
5744 pr_info(" recorded len bytes: %ld\n", total_len);
5745 pr_info(" recorded size bytes: %ld\n", total_size);
5747 pr_info(" With dropped events, record len and size may not match\n"
5748 " alloced and written from above\n");
5750 if (RB_WARN_ON(buffer, total_len != total_alloc ||
5751 total_size != total_written))
5754 if (RB_WARN_ON(buffer, total_lost + total_read != total_events))
5760 pr_info("Ring buffer PASSED!\n");
5762 ring_buffer_free(buffer);
5766 late_initcall(test_ringbuffer);
5767 #endif /* CONFIG_RING_BUFFER_STARTUP_TEST */