1 /*
2 * Generic ring buffer
3 *
4 * Copyright (C) 2008 Steven Rostedt <srostedt@redhat.com>
5 */
6 #include <linux/trace_events.h>
7 #include <linux/ring_buffer.h>
8 #include <linux/trace_clock.h>
9 #include <linux/trace_seq.h>
10 #include <linux/spinlock.h>
11 #include <linux/irq_work.h>
12 #include <linux/uaccess.h>
13 #include <linux/hardirq.h>
14 #include <linux/kthread.h> /* for self test */
15 #include <linux/kmemcheck.h>
16 #include <linux/module.h>
17 #include <linux/percpu.h>
18 #include <linux/mutex.h>
19 #include <linux/delay.h>
20 #include <linux/slab.h>
21 #include <linux/init.h>
22 #include <linux/hash.h>
23 #include <linux/list.h>
24 #include <linux/cpu.h>
25
26 #include <asm/local.h>
27
28 static void update_pages_handler(struct work_struct *work);
29
30 /*
31 * The ring buffer header is special. We must manually up keep it.
32 */
ring_buffer_print_entry_header(struct trace_seq * s)33 int ring_buffer_print_entry_header(struct trace_seq *s)
34 {
35 trace_seq_puts(s, "# compressed entry header\n");
36 trace_seq_puts(s, "\ttype_len : 5 bits\n");
37 trace_seq_puts(s, "\ttime_delta : 27 bits\n");
38 trace_seq_puts(s, "\tarray : 32 bits\n");
39 trace_seq_putc(s, '\n');
40 trace_seq_printf(s, "\tpadding : type == %d\n",
41 RINGBUF_TYPE_PADDING);
42 trace_seq_printf(s, "\ttime_extend : type == %d\n",
43 RINGBUF_TYPE_TIME_EXTEND);
44 trace_seq_printf(s, "\tdata max type_len == %d\n",
45 RINGBUF_TYPE_DATA_TYPE_LEN_MAX);
46
47 return !trace_seq_has_overflowed(s);
48 }
49
50 /*
51 * The ring buffer is made up of a list of pages. A separate list of pages is
52 * allocated for each CPU. A writer may only write to a buffer that is
53 * associated with the CPU it is currently executing on. A reader may read
54 * from any per cpu buffer.
55 *
56 * The reader is special. For each per cpu buffer, the reader has its own
57 * reader page. When a reader has read the entire reader page, this reader
58 * page is swapped with another page in the ring buffer.
59 *
60 * Now, as long as the writer is off the reader page, the reader can do what
61 * ever it wants with that page. The writer will never write to that page
62 * again (as long as it is out of the ring buffer).
63 *
64 * Here's some silly ASCII art.
65 *
66 * +------+
67 * |reader| RING BUFFER
68 * |page |
69 * +------+ +---+ +---+ +---+
70 * | |-->| |-->| |
71 * +---+ +---+ +---+
72 * ^ |
73 * | |
74 * +---------------+
75 *
76 *
77 * +------+
78 * |reader| RING BUFFER
79 * |page |------------------v
80 * +------+ +---+ +---+ +---+
81 * | |-->| |-->| |
82 * +---+ +---+ +---+
83 * ^ |
84 * | |
85 * +---------------+
86 *
87 *
88 * +------+
89 * |reader| RING BUFFER
90 * |page |------------------v
91 * +------+ +---+ +---+ +---+
92 * ^ | |-->| |-->| |
93 * | +---+ +---+ +---+
94 * | |
95 * | |
96 * +------------------------------+
97 *
98 *
99 * +------+
100 * |buffer| RING BUFFER
101 * |page |------------------v
102 * +------+ +---+ +---+ +---+
103 * ^ | | | |-->| |
104 * | New +---+ +---+ +---+
105 * | Reader------^ |
106 * | page |
107 * +------------------------------+
108 *
109 *
110 * After we make this swap, the reader can hand this page off to the splice
111 * code and be done with it. It can even allocate a new page if it needs to
112 * and swap that into the ring buffer.
113 *
114 * We will be using cmpxchg soon to make all this lockless.
115 *
116 */
117
118 /* Used for individual buffers (after the counter) */
119 #define RB_BUFFER_OFF (1 << 20)
120
121 #define BUF_PAGE_HDR_SIZE offsetof(struct buffer_data_page, data)
122
123 #define RB_EVNT_HDR_SIZE (offsetof(struct ring_buffer_event, array))
124 #define RB_ALIGNMENT 4U
125 #define RB_MAX_SMALL_DATA (RB_ALIGNMENT * RINGBUF_TYPE_DATA_TYPE_LEN_MAX)
126 #define RB_EVNT_MIN_SIZE 8U /* two 32bit words */
127
128 #ifndef CONFIG_HAVE_64BIT_ALIGNED_ACCESS
129 # define RB_FORCE_8BYTE_ALIGNMENT 0
130 # define RB_ARCH_ALIGNMENT RB_ALIGNMENT
131 #else
132 # define RB_FORCE_8BYTE_ALIGNMENT 1
133 # define RB_ARCH_ALIGNMENT 8U
134 #endif
135
136 #define RB_ALIGN_DATA __aligned(RB_ARCH_ALIGNMENT)
137
138 /* define RINGBUF_TYPE_DATA for 'case RINGBUF_TYPE_DATA:' */
139 #define RINGBUF_TYPE_DATA 0 ... RINGBUF_TYPE_DATA_TYPE_LEN_MAX
140
141 enum {
142 RB_LEN_TIME_EXTEND = 8,
143 RB_LEN_TIME_STAMP = 16,
144 };
145
146 #define skip_time_extend(event) \
147 ((struct ring_buffer_event *)((char *)event + RB_LEN_TIME_EXTEND))
148
rb_null_event(struct ring_buffer_event * event)149 static inline int rb_null_event(struct ring_buffer_event *event)
150 {
151 return event->type_len == RINGBUF_TYPE_PADDING && !event->time_delta;
152 }
153
rb_event_set_padding(struct ring_buffer_event * event)154 static void rb_event_set_padding(struct ring_buffer_event *event)
155 {
156 /* padding has a NULL time_delta */
157 event->type_len = RINGBUF_TYPE_PADDING;
158 event->time_delta = 0;
159 }
160
161 static unsigned
rb_event_data_length(struct ring_buffer_event * event)162 rb_event_data_length(struct ring_buffer_event *event)
163 {
164 unsigned length;
165
166 if (event->type_len)
167 length = event->type_len * RB_ALIGNMENT;
168 else
169 length = event->array[0];
170 return length + RB_EVNT_HDR_SIZE;
171 }
172
173 /*
174 * Return the length of the given event. Will return
175 * the length of the time extend if the event is a
176 * time extend.
177 */
178 static inline unsigned
rb_event_length(struct ring_buffer_event * event)179 rb_event_length(struct ring_buffer_event *event)
180 {
181 switch (event->type_len) {
182 case RINGBUF_TYPE_PADDING:
183 if (rb_null_event(event))
184 /* undefined */
185 return -1;
186 return event->array[0] + RB_EVNT_HDR_SIZE;
187
188 case RINGBUF_TYPE_TIME_EXTEND:
189 return RB_LEN_TIME_EXTEND;
190
191 case RINGBUF_TYPE_TIME_STAMP:
192 return RB_LEN_TIME_STAMP;
193
194 case RINGBUF_TYPE_DATA:
195 return rb_event_data_length(event);
196 default:
197 BUG();
198 }
199 /* not hit */
200 return 0;
201 }
202
203 /*
204 * Return total length of time extend and data,
205 * or just the event length for all other events.
206 */
207 static inline unsigned
rb_event_ts_length(struct ring_buffer_event * event)208 rb_event_ts_length(struct ring_buffer_event *event)
209 {
210 unsigned len = 0;
211
212 if (event->type_len == RINGBUF_TYPE_TIME_EXTEND) {
213 /* time extends include the data event after it */
214 len = RB_LEN_TIME_EXTEND;
215 event = skip_time_extend(event);
216 }
217 return len + rb_event_length(event);
218 }
219
220 /**
221 * ring_buffer_event_length - return the length of the event
222 * @event: the event to get the length of
223 *
224 * Returns the size of the data load of a data event.
225 * If the event is something other than a data event, it
226 * returns the size of the event itself. With the exception
227 * of a TIME EXTEND, where it still returns the size of the
228 * data load of the data event after it.
229 */
ring_buffer_event_length(struct ring_buffer_event * event)230 unsigned ring_buffer_event_length(struct ring_buffer_event *event)
231 {
232 unsigned length;
233
234 if (event->type_len == RINGBUF_TYPE_TIME_EXTEND)
235 event = skip_time_extend(event);
236
237 length = rb_event_length(event);
238 if (event->type_len > RINGBUF_TYPE_DATA_TYPE_LEN_MAX)
239 return length;
240 length -= RB_EVNT_HDR_SIZE;
241 if (length > RB_MAX_SMALL_DATA + sizeof(event->array[0]))
242 length -= sizeof(event->array[0]);
243 return length;
244 }
245 EXPORT_SYMBOL_GPL(ring_buffer_event_length);
246
247 /* inline for ring buffer fast paths */
248 static void *
rb_event_data(struct ring_buffer_event * event)249 rb_event_data(struct ring_buffer_event *event)
250 {
251 if (event->type_len == RINGBUF_TYPE_TIME_EXTEND)
252 event = skip_time_extend(event);
253 BUG_ON(event->type_len > RINGBUF_TYPE_DATA_TYPE_LEN_MAX);
254 /* If length is in len field, then array[0] has the data */
255 if (event->type_len)
256 return (void *)&event->array[0];
257 /* Otherwise length is in array[0] and array[1] has the data */
258 return (void *)&event->array[1];
259 }
260
261 /**
262 * ring_buffer_event_data - return the data of the event
263 * @event: the event to get the data from
264 */
ring_buffer_event_data(struct ring_buffer_event * event)265 void *ring_buffer_event_data(struct ring_buffer_event *event)
266 {
267 return rb_event_data(event);
268 }
269 EXPORT_SYMBOL_GPL(ring_buffer_event_data);
270
271 #define for_each_buffer_cpu(buffer, cpu) \
272 for_each_cpu(cpu, buffer->cpumask)
273
274 #define TS_SHIFT 27
275 #define TS_MASK ((1ULL << TS_SHIFT) - 1)
276 #define TS_DELTA_TEST (~TS_MASK)
277
278 /* Flag when events were overwritten */
279 #define RB_MISSED_EVENTS (1 << 31)
280 /* Missed count stored at end */
281 #define RB_MISSED_STORED (1 << 30)
282
283 #define RB_MISSED_FLAGS (RB_MISSED_EVENTS|RB_MISSED_STORED)
284
285 struct buffer_data_page {
286 u64 time_stamp; /* page time stamp */
287 local_t commit; /* write committed index */
288 unsigned char data[] RB_ALIGN_DATA; /* data of buffer page */
289 };
290
291 /*
292 * Note, the buffer_page list must be first. The buffer pages
293 * are allocated in cache lines, which means that each buffer
294 * page will be at the beginning of a cache line, and thus
295 * the least significant bits will be zero. We use this to
296 * add flags in the list struct pointers, to make the ring buffer
297 * lockless.
298 */
299 struct buffer_page {
300 struct list_head list; /* list of buffer pages */
301 local_t write; /* index for next write */
302 unsigned read; /* index for next read */
303 local_t entries; /* entries on this page */
304 unsigned long real_end; /* real end of data */
305 struct buffer_data_page *page; /* Actual data page */
306 };
307
308 /*
309 * The buffer page counters, write and entries, must be reset
310 * atomically when crossing page boundaries. To synchronize this
311 * update, two counters are inserted into the number. One is
312 * the actual counter for the write position or count on the page.
313 *
314 * The other is a counter of updaters. Before an update happens
315 * the update partition of the counter is incremented. This will
316 * allow the updater to update the counter atomically.
317 *
318 * The counter is 20 bits, and the state data is 12.
319 */
320 #define RB_WRITE_MASK 0xfffff
321 #define RB_WRITE_INTCNT (1 << 20)
322
rb_init_page(struct buffer_data_page * bpage)323 static void rb_init_page(struct buffer_data_page *bpage)
324 {
325 local_set(&bpage->commit, 0);
326 }
327
328 /**
329 * ring_buffer_page_len - the size of data on the page.
330 * @page: The page to read
331 *
332 * Returns the amount of data on the page, including buffer page header.
333 */
ring_buffer_page_len(void * page)334 size_t ring_buffer_page_len(void *page)
335 {
336 struct buffer_data_page *bpage = page;
337
338 return (local_read(&bpage->commit) & ~RB_MISSED_FLAGS)
339 + BUF_PAGE_HDR_SIZE;
340 }
341
342 /*
343 * Also stolen from mm/slob.c. Thanks to Mathieu Desnoyers for pointing
344 * this issue out.
345 */
free_buffer_page(struct buffer_page * bpage)346 static void free_buffer_page(struct buffer_page *bpage)
347 {
348 free_page((unsigned long)bpage->page);
349 kfree(bpage);
350 }
351
352 /*
353 * We need to fit the time_stamp delta into 27 bits.
354 */
test_time_stamp(u64 delta)355 static inline int test_time_stamp(u64 delta)
356 {
357 if (delta & TS_DELTA_TEST)
358 return 1;
359 return 0;
360 }
361
362 #define BUF_PAGE_SIZE (PAGE_SIZE - BUF_PAGE_HDR_SIZE)
363
364 /* Max payload is BUF_PAGE_SIZE - header (8bytes) */
365 #define BUF_MAX_DATA_SIZE (BUF_PAGE_SIZE - (sizeof(u32) * 2))
366
ring_buffer_print_page_header(struct trace_seq * s)367 int ring_buffer_print_page_header(struct trace_seq *s)
368 {
369 struct buffer_data_page field;
370
371 trace_seq_printf(s, "\tfield: u64 timestamp;\t"
372 "offset:0;\tsize:%u;\tsigned:%u;\n",
373 (unsigned int)sizeof(field.time_stamp),
374 (unsigned int)is_signed_type(u64));
375
376 trace_seq_printf(s, "\tfield: local_t commit;\t"
377 "offset:%u;\tsize:%u;\tsigned:%u;\n",
378 (unsigned int)offsetof(typeof(field), commit),
379 (unsigned int)sizeof(field.commit),
380 (unsigned int)is_signed_type(long));
381
382 trace_seq_printf(s, "\tfield: int overwrite;\t"
383 "offset:%u;\tsize:%u;\tsigned:%u;\n",
384 (unsigned int)offsetof(typeof(field), commit),
385 1,
386 (unsigned int)is_signed_type(long));
387
388 trace_seq_printf(s, "\tfield: char data;\t"
389 "offset:%u;\tsize:%u;\tsigned:%u;\n",
390 (unsigned int)offsetof(typeof(field), data),
391 (unsigned int)BUF_PAGE_SIZE,
392 (unsigned int)is_signed_type(char));
393
394 return !trace_seq_has_overflowed(s);
395 }
396
397 struct rb_irq_work {
398 struct irq_work work;
399 wait_queue_head_t waiters;
400 wait_queue_head_t full_waiters;
401 bool waiters_pending;
402 bool full_waiters_pending;
403 bool wakeup_full;
404 };
405
406 /*
407 * Structure to hold event state and handle nested events.
408 */
409 struct rb_event_info {
410 u64 ts;
411 u64 delta;
412 unsigned long length;
413 struct buffer_page *tail_page;
414 int add_timestamp;
415 };
416
417 /*
418 * Used for which event context the event is in.
419 * NMI = 0
420 * IRQ = 1
421 * SOFTIRQ = 2
422 * NORMAL = 3
423 *
424 * See trace_recursive_lock() comment below for more details.
425 */
426 enum {
427 RB_CTX_NMI,
428 RB_CTX_IRQ,
429 RB_CTX_SOFTIRQ,
430 RB_CTX_NORMAL,
431 RB_CTX_MAX
432 };
433
434 /*
435 * head_page == tail_page && head == tail then buffer is empty.
436 */
437 struct ring_buffer_per_cpu {
438 int cpu;
439 atomic_t record_disabled;
440 struct ring_buffer *buffer;
441 raw_spinlock_t reader_lock; /* serialize readers */
442 arch_spinlock_t lock;
443 struct lock_class_key lock_key;
444 unsigned long nr_pages;
445 unsigned int current_context;
446 struct list_head *pages;
447 struct buffer_page *head_page; /* read from head */
448 struct buffer_page *tail_page; /* write to tail */
449 struct buffer_page *commit_page; /* committed pages */
450 struct buffer_page *reader_page;
451 unsigned long lost_events;
452 unsigned long last_overrun;
453 local_t entries_bytes;
454 local_t entries;
455 local_t overrun;
456 local_t commit_overrun;
457 local_t dropped_events;
458 local_t committing;
459 local_t commits;
460 unsigned long read;
461 unsigned long read_bytes;
462 u64 write_stamp;
463 u64 read_stamp;
464 /* ring buffer pages to update, > 0 to add, < 0 to remove */
465 long nr_pages_to_update;
466 struct list_head new_pages; /* new pages to add */
467 struct work_struct update_pages_work;
468 struct completion update_done;
469
470 struct rb_irq_work irq_work;
471 };
472
473 struct ring_buffer {
474 unsigned flags;
475 int cpus;
476 atomic_t record_disabled;
477 atomic_t resize_disabled;
478 cpumask_var_t cpumask;
479
480 struct lock_class_key *reader_lock_key;
481
482 struct mutex mutex;
483
484 struct ring_buffer_per_cpu **buffers;
485
486 #ifdef CONFIG_HOTPLUG_CPU
487 struct notifier_block cpu_notify;
488 #endif
489 u64 (*clock)(void);
490
491 struct rb_irq_work irq_work;
492 };
493
494 struct ring_buffer_iter {
495 struct ring_buffer_per_cpu *cpu_buffer;
496 unsigned long head;
497 struct buffer_page *head_page;
498 struct buffer_page *cache_reader_page;
499 unsigned long cache_read;
500 u64 read_stamp;
501 };
502
503 /*
504 * rb_wake_up_waiters - wake up tasks waiting for ring buffer input
505 *
506 * Schedules a delayed work to wake up any task that is blocked on the
507 * ring buffer waiters queue.
508 */
rb_wake_up_waiters(struct irq_work * work)509 static void rb_wake_up_waiters(struct irq_work *work)
510 {
511 struct rb_irq_work *rbwork = container_of(work, struct rb_irq_work, work);
512
513 wake_up_all(&rbwork->waiters);
514 if (rbwork->wakeup_full) {
515 rbwork->wakeup_full = false;
516 wake_up_all(&rbwork->full_waiters);
517 }
518 }
519
520 /**
521 * ring_buffer_wait - wait for input to the ring buffer
522 * @buffer: buffer to wait on
523 * @cpu: the cpu buffer to wait on
524 * @full: wait until a full page is available, if @cpu != RING_BUFFER_ALL_CPUS
525 *
526 * If @cpu == RING_BUFFER_ALL_CPUS then the task will wake up as soon
527 * as data is added to any of the @buffer's cpu buffers. Otherwise
528 * it will wait for data to be added to a specific cpu buffer.
529 */
ring_buffer_wait(struct ring_buffer * buffer,int cpu,bool full)530 int ring_buffer_wait(struct ring_buffer *buffer, int cpu, bool full)
531 {
532 struct ring_buffer_per_cpu *uninitialized_var(cpu_buffer);
533 DEFINE_WAIT(wait);
534 struct rb_irq_work *work;
535 int ret = 0;
536
537 /*
538 * Depending on what the caller is waiting for, either any
539 * data in any cpu buffer, or a specific buffer, put the
540 * caller on the appropriate wait queue.
541 */
542 if (cpu == RING_BUFFER_ALL_CPUS) {
543 work = &buffer->irq_work;
544 /* Full only makes sense on per cpu reads */
545 full = false;
546 } else {
547 if (!cpumask_test_cpu(cpu, buffer->cpumask))
548 return -ENODEV;
549 cpu_buffer = buffer->buffers[cpu];
550 work = &cpu_buffer->irq_work;
551 }
552
553
554 while (true) {
555 if (full)
556 prepare_to_wait(&work->full_waiters, &wait, TASK_INTERRUPTIBLE);
557 else
558 prepare_to_wait(&work->waiters, &wait, TASK_INTERRUPTIBLE);
559
560 /*
561 * The events can happen in critical sections where
562 * checking a work queue can cause deadlocks.
563 * After adding a task to the queue, this flag is set
564 * only to notify events to try to wake up the queue
565 * using irq_work.
566 *
567 * We don't clear it even if the buffer is no longer
568 * empty. The flag only causes the next event to run
569 * irq_work to do the work queue wake up. The worse
570 * that can happen if we race with !trace_empty() is that
571 * an event will cause an irq_work to try to wake up
572 * an empty queue.
573 *
574 * There's no reason to protect this flag either, as
575 * the work queue and irq_work logic will do the necessary
576 * synchronization for the wake ups. The only thing
577 * that is necessary is that the wake up happens after
578 * a task has been queued. It's OK for spurious wake ups.
579 */
580 if (full)
581 work->full_waiters_pending = true;
582 else
583 work->waiters_pending = true;
584
585 if (signal_pending(current)) {
586 ret = -EINTR;
587 break;
588 }
589
590 if (cpu == RING_BUFFER_ALL_CPUS && !ring_buffer_empty(buffer))
591 break;
592
593 if (cpu != RING_BUFFER_ALL_CPUS &&
594 !ring_buffer_empty_cpu(buffer, cpu)) {
595 unsigned long flags;
596 bool pagebusy;
597
598 if (!full)
599 break;
600
601 raw_spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
602 pagebusy = cpu_buffer->reader_page == cpu_buffer->commit_page;
603 raw_spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
604
605 if (!pagebusy)
606 break;
607 }
608
609 schedule();
610 }
611
612 if (full)
613 finish_wait(&work->full_waiters, &wait);
614 else
615 finish_wait(&work->waiters, &wait);
616
617 return ret;
618 }
619
620 /**
621 * ring_buffer_poll_wait - poll on buffer input
622 * @buffer: buffer to wait on
623 * @cpu: the cpu buffer to wait on
624 * @filp: the file descriptor
625 * @poll_table: The poll descriptor
626 *
627 * If @cpu == RING_BUFFER_ALL_CPUS then the task will wake up as soon
628 * as data is added to any of the @buffer's cpu buffers. Otherwise
629 * it will wait for data to be added to a specific cpu buffer.
630 *
631 * Returns POLLIN | POLLRDNORM if data exists in the buffers,
632 * zero otherwise.
633 */
ring_buffer_poll_wait(struct ring_buffer * buffer,int cpu,struct file * filp,poll_table * poll_table)634 int ring_buffer_poll_wait(struct ring_buffer *buffer, int cpu,
635 struct file *filp, poll_table *poll_table)
636 {
637 struct ring_buffer_per_cpu *cpu_buffer;
638 struct rb_irq_work *work;
639
640 if (cpu == RING_BUFFER_ALL_CPUS)
641 work = &buffer->irq_work;
642 else {
643 if (!cpumask_test_cpu(cpu, buffer->cpumask))
644 return -EINVAL;
645
646 cpu_buffer = buffer->buffers[cpu];
647 work = &cpu_buffer->irq_work;
648 }
649
650 poll_wait(filp, &work->waiters, poll_table);
651 work->waiters_pending = true;
652 /*
653 * There's a tight race between setting the waiters_pending and
654 * checking if the ring buffer is empty. Once the waiters_pending bit
655 * is set, the next event will wake the task up, but we can get stuck
656 * if there's only a single event in.
657 *
658 * FIXME: Ideally, we need a memory barrier on the writer side as well,
659 * but adding a memory barrier to all events will cause too much of a
660 * performance hit in the fast path. We only need a memory barrier when
661 * the buffer goes from empty to having content. But as this race is
662 * extremely small, and it's not a problem if another event comes in, we
663 * will fix it later.
664 */
665 smp_mb();
666
667 if ((cpu == RING_BUFFER_ALL_CPUS && !ring_buffer_empty(buffer)) ||
668 (cpu != RING_BUFFER_ALL_CPUS && !ring_buffer_empty_cpu(buffer, cpu)))
669 return POLLIN | POLLRDNORM;
670 return 0;
671 }
672
673 /* buffer may be either ring_buffer or ring_buffer_per_cpu */
674 #define RB_WARN_ON(b, cond) \
675 ({ \
676 int _____ret = unlikely(cond); \
677 if (_____ret) { \
678 if (__same_type(*(b), struct ring_buffer_per_cpu)) { \
679 struct ring_buffer_per_cpu *__b = \
680 (void *)b; \
681 atomic_inc(&__b->buffer->record_disabled); \
682 } else \
683 atomic_inc(&b->record_disabled); \
684 WARN_ON(1); \
685 } \
686 _____ret; \
687 })
688
689 /* Up this if you want to test the TIME_EXTENTS and normalization */
690 #define DEBUG_SHIFT 0
691
rb_time_stamp(struct ring_buffer * buffer)692 static inline u64 rb_time_stamp(struct ring_buffer *buffer)
693 {
694 /* shift to debug/test normalization and TIME_EXTENTS */
695 return buffer->clock() << DEBUG_SHIFT;
696 }
697
ring_buffer_time_stamp(struct ring_buffer * buffer,int cpu)698 u64 ring_buffer_time_stamp(struct ring_buffer *buffer, int cpu)
699 {
700 u64 time;
701
702 preempt_disable_notrace();
703 time = rb_time_stamp(buffer);
704 preempt_enable_no_resched_notrace();
705
706 return time;
707 }
708 EXPORT_SYMBOL_GPL(ring_buffer_time_stamp);
709
ring_buffer_normalize_time_stamp(struct ring_buffer * buffer,int cpu,u64 * ts)710 void ring_buffer_normalize_time_stamp(struct ring_buffer *buffer,
711 int cpu, u64 *ts)
712 {
713 /* Just stupid testing the normalize function and deltas */
714 *ts >>= DEBUG_SHIFT;
715 }
716 EXPORT_SYMBOL_GPL(ring_buffer_normalize_time_stamp);
717
718 /*
719 * Making the ring buffer lockless makes things tricky.
720 * Although writes only happen on the CPU that they are on,
721 * and they only need to worry about interrupts. Reads can
722 * happen on any CPU.
723 *
724 * The reader page is always off the ring buffer, but when the
725 * reader finishes with a page, it needs to swap its page with
726 * a new one from the buffer. The reader needs to take from
727 * the head (writes go to the tail). But if a writer is in overwrite
728 * mode and wraps, it must push the head page forward.
729 *
730 * Here lies the problem.
731 *
732 * The reader must be careful to replace only the head page, and
733 * not another one. As described at the top of the file in the
734 * ASCII art, the reader sets its old page to point to the next
735 * page after head. It then sets the page after head to point to
736 * the old reader page. But if the writer moves the head page
737 * during this operation, the reader could end up with the tail.
738 *
739 * We use cmpxchg to help prevent this race. We also do something
740 * special with the page before head. We set the LSB to 1.
741 *
742 * When the writer must push the page forward, it will clear the
743 * bit that points to the head page, move the head, and then set
744 * the bit that points to the new head page.
745 *
746 * We also don't want an interrupt coming in and moving the head
747 * page on another writer. Thus we use the second LSB to catch
748 * that too. Thus:
749 *
750 * head->list->prev->next bit 1 bit 0
751 * ------- -------
752 * Normal page 0 0
753 * Points to head page 0 1
754 * New head page 1 0
755 *
756 * Note we can not trust the prev pointer of the head page, because:
757 *
758 * +----+ +-----+ +-----+
759 * | |------>| T |---X--->| N |
760 * | |<------| | | |
761 * +----+ +-----+ +-----+
762 * ^ ^ |
763 * | +-----+ | |
764 * +----------| R |----------+ |
765 * | |<-----------+
766 * +-----+
767 *
768 * Key: ---X--> HEAD flag set in pointer
769 * T Tail page
770 * R Reader page
771 * N Next page
772 *
773 * (see __rb_reserve_next() to see where this happens)
774 *
775 * What the above shows is that the reader just swapped out
776 * the reader page with a page in the buffer, but before it
777 * could make the new header point back to the new page added
778 * it was preempted by a writer. The writer moved forward onto
779 * the new page added by the reader and is about to move forward
780 * again.
781 *
782 * You can see, it is legitimate for the previous pointer of
783 * the head (or any page) not to point back to itself. But only
784 * temporarially.
785 */
786
787 #define RB_PAGE_NORMAL 0UL
788 #define RB_PAGE_HEAD 1UL
789 #define RB_PAGE_UPDATE 2UL
790
791
792 #define RB_FLAG_MASK 3UL
793
794 /* PAGE_MOVED is not part of the mask */
795 #define RB_PAGE_MOVED 4UL
796
797 /*
798 * rb_list_head - remove any bit
799 */
rb_list_head(struct list_head * list)800 static struct list_head *rb_list_head(struct list_head *list)
801 {
802 unsigned long val = (unsigned long)list;
803
804 return (struct list_head *)(val & ~RB_FLAG_MASK);
805 }
806
807 /*
808 * rb_is_head_page - test if the given page is the head page
809 *
810 * Because the reader may move the head_page pointer, we can
811 * not trust what the head page is (it may be pointing to
812 * the reader page). But if the next page is a header page,
813 * its flags will be non zero.
814 */
815 static inline int
rb_is_head_page(struct ring_buffer_per_cpu * cpu_buffer,struct buffer_page * page,struct list_head * list)816 rb_is_head_page(struct ring_buffer_per_cpu *cpu_buffer,
817 struct buffer_page *page, struct list_head *list)
818 {
819 unsigned long val;
820
821 val = (unsigned long)list->next;
822
823 if ((val & ~RB_FLAG_MASK) != (unsigned long)&page->list)
824 return RB_PAGE_MOVED;
825
826 return val & RB_FLAG_MASK;
827 }
828
829 /*
830 * rb_is_reader_page
831 *
832 * The unique thing about the reader page, is that, if the
833 * writer is ever on it, the previous pointer never points
834 * back to the reader page.
835 */
rb_is_reader_page(struct buffer_page * page)836 static bool rb_is_reader_page(struct buffer_page *page)
837 {
838 struct list_head *list = page->list.prev;
839
840 return rb_list_head(list->next) != &page->list;
841 }
842
843 /*
844 * rb_set_list_to_head - set a list_head to be pointing to head.
845 */
rb_set_list_to_head(struct ring_buffer_per_cpu * cpu_buffer,struct list_head * list)846 static void rb_set_list_to_head(struct ring_buffer_per_cpu *cpu_buffer,
847 struct list_head *list)
848 {
849 unsigned long *ptr;
850
851 ptr = (unsigned long *)&list->next;
852 *ptr |= RB_PAGE_HEAD;
853 *ptr &= ~RB_PAGE_UPDATE;
854 }
855
856 /*
857 * rb_head_page_activate - sets up head page
858 */
rb_head_page_activate(struct ring_buffer_per_cpu * cpu_buffer)859 static void rb_head_page_activate(struct ring_buffer_per_cpu *cpu_buffer)
860 {
861 struct buffer_page *head;
862
863 head = cpu_buffer->head_page;
864 if (!head)
865 return;
866
867 /*
868 * Set the previous list pointer to have the HEAD flag.
869 */
870 rb_set_list_to_head(cpu_buffer, head->list.prev);
871 }
872
rb_list_head_clear(struct list_head * list)873 static void rb_list_head_clear(struct list_head *list)
874 {
875 unsigned long *ptr = (unsigned long *)&list->next;
876
877 *ptr &= ~RB_FLAG_MASK;
878 }
879
880 /*
881 * rb_head_page_dactivate - clears head page ptr (for free list)
882 */
883 static void
rb_head_page_deactivate(struct ring_buffer_per_cpu * cpu_buffer)884 rb_head_page_deactivate(struct ring_buffer_per_cpu *cpu_buffer)
885 {
886 struct list_head *hd;
887
888 /* Go through the whole list and clear any pointers found. */
889 rb_list_head_clear(cpu_buffer->pages);
890
891 list_for_each(hd, cpu_buffer->pages)
892 rb_list_head_clear(hd);
893 }
894
rb_head_page_set(struct ring_buffer_per_cpu * cpu_buffer,struct buffer_page * head,struct buffer_page * prev,int old_flag,int new_flag)895 static int rb_head_page_set(struct ring_buffer_per_cpu *cpu_buffer,
896 struct buffer_page *head,
897 struct buffer_page *prev,
898 int old_flag, int new_flag)
899 {
900 struct list_head *list;
901 unsigned long val = (unsigned long)&head->list;
902 unsigned long ret;
903
904 list = &prev->list;
905
906 val &= ~RB_FLAG_MASK;
907
908 ret = cmpxchg((unsigned long *)&list->next,
909 val | old_flag, val | new_flag);
910
911 /* check if the reader took the page */
912 if ((ret & ~RB_FLAG_MASK) != val)
913 return RB_PAGE_MOVED;
914
915 return ret & RB_FLAG_MASK;
916 }
917
rb_head_page_set_update(struct ring_buffer_per_cpu * cpu_buffer,struct buffer_page * head,struct buffer_page * prev,int old_flag)918 static int rb_head_page_set_update(struct ring_buffer_per_cpu *cpu_buffer,
919 struct buffer_page *head,
920 struct buffer_page *prev,
921 int old_flag)
922 {
923 return rb_head_page_set(cpu_buffer, head, prev,
924 old_flag, RB_PAGE_UPDATE);
925 }
926
rb_head_page_set_head(struct ring_buffer_per_cpu * cpu_buffer,struct buffer_page * head,struct buffer_page * prev,int old_flag)927 static int rb_head_page_set_head(struct ring_buffer_per_cpu *cpu_buffer,
928 struct buffer_page *head,
929 struct buffer_page *prev,
930 int old_flag)
931 {
932 return rb_head_page_set(cpu_buffer, head, prev,
933 old_flag, RB_PAGE_HEAD);
934 }
935
rb_head_page_set_normal(struct ring_buffer_per_cpu * cpu_buffer,struct buffer_page * head,struct buffer_page * prev,int old_flag)936 static int rb_head_page_set_normal(struct ring_buffer_per_cpu *cpu_buffer,
937 struct buffer_page *head,
938 struct buffer_page *prev,
939 int old_flag)
940 {
941 return rb_head_page_set(cpu_buffer, head, prev,
942 old_flag, RB_PAGE_NORMAL);
943 }
944
rb_inc_page(struct ring_buffer_per_cpu * cpu_buffer,struct buffer_page ** bpage)945 static inline void rb_inc_page(struct ring_buffer_per_cpu *cpu_buffer,
946 struct buffer_page **bpage)
947 {
948 struct list_head *p = rb_list_head((*bpage)->list.next);
949
950 *bpage = list_entry(p, struct buffer_page, list);
951 }
952
953 static struct buffer_page *
rb_set_head_page(struct ring_buffer_per_cpu * cpu_buffer)954 rb_set_head_page(struct ring_buffer_per_cpu *cpu_buffer)
955 {
956 struct buffer_page *head;
957 struct buffer_page *page;
958 struct list_head *list;
959 int i;
960
961 if (RB_WARN_ON(cpu_buffer, !cpu_buffer->head_page))
962 return NULL;
963
964 /* sanity check */
965 list = cpu_buffer->pages;
966 if (RB_WARN_ON(cpu_buffer, rb_list_head(list->prev->next) != list))
967 return NULL;
968
969 page = head = cpu_buffer->head_page;
970 /*
971 * It is possible that the writer moves the header behind
972 * where we started, and we miss in one loop.
973 * A second loop should grab the header, but we'll do
974 * three loops just because I'm paranoid.
975 */
976 for (i = 0; i < 3; i++) {
977 do {
978 if (rb_is_head_page(cpu_buffer, page, page->list.prev)) {
979 cpu_buffer->head_page = page;
980 return page;
981 }
982 rb_inc_page(cpu_buffer, &page);
983 } while (page != head);
984 }
985
986 RB_WARN_ON(cpu_buffer, 1);
987
988 return NULL;
989 }
990
rb_head_page_replace(struct buffer_page * old,struct buffer_page * new)991 static int rb_head_page_replace(struct buffer_page *old,
992 struct buffer_page *new)
993 {
994 unsigned long *ptr = (unsigned long *)&old->list.prev->next;
995 unsigned long val;
996 unsigned long ret;
997
998 val = *ptr & ~RB_FLAG_MASK;
999 val |= RB_PAGE_HEAD;
1000
1001 ret = cmpxchg(ptr, val, (unsigned long)&new->list);
1002
1003 return ret == val;
1004 }
1005
1006 /*
1007 * rb_tail_page_update - move the tail page forward
1008 */
rb_tail_page_update(struct ring_buffer_per_cpu * cpu_buffer,struct buffer_page * tail_page,struct buffer_page * next_page)1009 static void rb_tail_page_update(struct ring_buffer_per_cpu *cpu_buffer,
1010 struct buffer_page *tail_page,
1011 struct buffer_page *next_page)
1012 {
1013 unsigned long old_entries;
1014 unsigned long old_write;
1015
1016 /*
1017 * The tail page now needs to be moved forward.
1018 *
1019 * We need to reset the tail page, but without messing
1020 * with possible erasing of data brought in by interrupts
1021 * that have moved the tail page and are currently on it.
1022 *
1023 * We add a counter to the write field to denote this.
1024 */
1025 old_write = local_add_return(RB_WRITE_INTCNT, &next_page->write);
1026 old_entries = local_add_return(RB_WRITE_INTCNT, &next_page->entries);
1027
1028 /*
1029 * Just make sure we have seen our old_write and synchronize
1030 * with any interrupts that come in.
1031 */
1032 barrier();
1033
1034 /*
1035 * If the tail page is still the same as what we think
1036 * it is, then it is up to us to update the tail
1037 * pointer.
1038 */
1039 if (tail_page == READ_ONCE(cpu_buffer->tail_page)) {
1040 /* Zero the write counter */
1041 unsigned long val = old_write & ~RB_WRITE_MASK;
1042 unsigned long eval = old_entries & ~RB_WRITE_MASK;
1043
1044 /*
1045 * This will only succeed if an interrupt did
1046 * not come in and change it. In which case, we
1047 * do not want to modify it.
1048 *
1049 * We add (void) to let the compiler know that we do not care
1050 * about the return value of these functions. We use the
1051 * cmpxchg to only update if an interrupt did not already
1052 * do it for us. If the cmpxchg fails, we don't care.
1053 */
1054 (void)local_cmpxchg(&next_page->write, old_write, val);
1055 (void)local_cmpxchg(&next_page->entries, old_entries, eval);
1056
1057 /*
1058 * No need to worry about races with clearing out the commit.
1059 * it only can increment when a commit takes place. But that
1060 * only happens in the outer most nested commit.
1061 */
1062 local_set(&next_page->page->commit, 0);
1063
1064 /* Again, either we update tail_page or an interrupt does */
1065 (void)cmpxchg(&cpu_buffer->tail_page, tail_page, next_page);
1066 }
1067 }
1068
rb_check_bpage(struct ring_buffer_per_cpu * cpu_buffer,struct buffer_page * bpage)1069 static int rb_check_bpage(struct ring_buffer_per_cpu *cpu_buffer,
1070 struct buffer_page *bpage)
1071 {
1072 unsigned long val = (unsigned long)bpage;
1073
1074 if (RB_WARN_ON(cpu_buffer, val & RB_FLAG_MASK))
1075 return 1;
1076
1077 return 0;
1078 }
1079
1080 /**
1081 * rb_check_list - make sure a pointer to a list has the last bits zero
1082 */
rb_check_list(struct ring_buffer_per_cpu * cpu_buffer,struct list_head * list)1083 static int rb_check_list(struct ring_buffer_per_cpu *cpu_buffer,
1084 struct list_head *list)
1085 {
1086 if (RB_WARN_ON(cpu_buffer, rb_list_head(list->prev) != list->prev))
1087 return 1;
1088 if (RB_WARN_ON(cpu_buffer, rb_list_head(list->next) != list->next))
1089 return 1;
1090 return 0;
1091 }
1092
1093 /**
1094 * rb_check_pages - integrity check of buffer pages
1095 * @cpu_buffer: CPU buffer with pages to test
1096 *
1097 * As a safety measure we check to make sure the data pages have not
1098 * been corrupted.
1099 */
rb_check_pages(struct ring_buffer_per_cpu * cpu_buffer)1100 static int rb_check_pages(struct ring_buffer_per_cpu *cpu_buffer)
1101 {
1102 struct list_head *head = cpu_buffer->pages;
1103 struct buffer_page *bpage, *tmp;
1104
1105 /* Reset the head page if it exists */
1106 if (cpu_buffer->head_page)
1107 rb_set_head_page(cpu_buffer);
1108
1109 rb_head_page_deactivate(cpu_buffer);
1110
1111 if (RB_WARN_ON(cpu_buffer, head->next->prev != head))
1112 return -1;
1113 if (RB_WARN_ON(cpu_buffer, head->prev->next != head))
1114 return -1;
1115
1116 if (rb_check_list(cpu_buffer, head))
1117 return -1;
1118
1119 list_for_each_entry_safe(bpage, tmp, head, list) {
1120 if (RB_WARN_ON(cpu_buffer,
1121 bpage->list.next->prev != &bpage->list))
1122 return -1;
1123 if (RB_WARN_ON(cpu_buffer,
1124 bpage->list.prev->next != &bpage->list))
1125 return -1;
1126 if (rb_check_list(cpu_buffer, &bpage->list))
1127 return -1;
1128 }
1129
1130 rb_head_page_activate(cpu_buffer);
1131
1132 return 0;
1133 }
1134
__rb_allocate_pages(long nr_pages,struct list_head * pages,int cpu)1135 static int __rb_allocate_pages(long nr_pages, struct list_head *pages, int cpu)
1136 {
1137 struct buffer_page *bpage, *tmp;
1138 long i;
1139
1140 for (i = 0; i < nr_pages; i++) {
1141 struct page *page;
1142 /*
1143 * __GFP_NORETRY flag makes sure that the allocation fails
1144 * gracefully without invoking oom-killer and the system is
1145 * not destabilized.
1146 */
1147 bpage = kzalloc_node(ALIGN(sizeof(*bpage), cache_line_size()),
1148 GFP_KERNEL | __GFP_NORETRY,
1149 cpu_to_node(cpu));
1150 if (!bpage)
1151 goto free_pages;
1152
1153 list_add(&bpage->list, pages);
1154
1155 page = alloc_pages_node(cpu_to_node(cpu),
1156 GFP_KERNEL | __GFP_NORETRY, 0);
1157 if (!page)
1158 goto free_pages;
1159 bpage->page = page_address(page);
1160 rb_init_page(bpage->page);
1161 }
1162
1163 return 0;
1164
1165 free_pages:
1166 list_for_each_entry_safe(bpage, tmp, pages, list) {
1167 list_del_init(&bpage->list);
1168 free_buffer_page(bpage);
1169 }
1170
1171 return -ENOMEM;
1172 }
1173
rb_allocate_pages(struct ring_buffer_per_cpu * cpu_buffer,unsigned long nr_pages)1174 static int rb_allocate_pages(struct ring_buffer_per_cpu *cpu_buffer,
1175 unsigned long nr_pages)
1176 {
1177 LIST_HEAD(pages);
1178
1179 WARN_ON(!nr_pages);
1180
1181 if (__rb_allocate_pages(nr_pages, &pages, cpu_buffer->cpu))
1182 return -ENOMEM;
1183
1184 /*
1185 * The ring buffer page list is a circular list that does not
1186 * start and end with a list head. All page list items point to
1187 * other pages.
1188 */
1189 cpu_buffer->pages = pages.next;
1190 list_del(&pages);
1191
1192 cpu_buffer->nr_pages = nr_pages;
1193
1194 rb_check_pages(cpu_buffer);
1195
1196 return 0;
1197 }
1198
1199 static struct ring_buffer_per_cpu *
rb_allocate_cpu_buffer(struct ring_buffer * buffer,long nr_pages,int cpu)1200 rb_allocate_cpu_buffer(struct ring_buffer *buffer, long nr_pages, int cpu)
1201 {
1202 struct ring_buffer_per_cpu *cpu_buffer;
1203 struct buffer_page *bpage;
1204 struct page *page;
1205 int ret;
1206
1207 cpu_buffer = kzalloc_node(ALIGN(sizeof(*cpu_buffer), cache_line_size()),
1208 GFP_KERNEL, cpu_to_node(cpu));
1209 if (!cpu_buffer)
1210 return NULL;
1211
1212 cpu_buffer->cpu = cpu;
1213 cpu_buffer->buffer = buffer;
1214 raw_spin_lock_init(&cpu_buffer->reader_lock);
1215 lockdep_set_class(&cpu_buffer->reader_lock, buffer->reader_lock_key);
1216 cpu_buffer->lock = (arch_spinlock_t)__ARCH_SPIN_LOCK_UNLOCKED;
1217 INIT_WORK(&cpu_buffer->update_pages_work, update_pages_handler);
1218 init_completion(&cpu_buffer->update_done);
1219 init_irq_work(&cpu_buffer->irq_work.work, rb_wake_up_waiters);
1220 init_waitqueue_head(&cpu_buffer->irq_work.waiters);
1221 init_waitqueue_head(&cpu_buffer->irq_work.full_waiters);
1222
1223 bpage = kzalloc_node(ALIGN(sizeof(*bpage), cache_line_size()),
1224 GFP_KERNEL, cpu_to_node(cpu));
1225 if (!bpage)
1226 goto fail_free_buffer;
1227
1228 rb_check_bpage(cpu_buffer, bpage);
1229
1230 cpu_buffer->reader_page = bpage;
1231 page = alloc_pages_node(cpu_to_node(cpu), GFP_KERNEL, 0);
1232 if (!page)
1233 goto fail_free_reader;
1234 bpage->page = page_address(page);
1235 rb_init_page(bpage->page);
1236
1237 INIT_LIST_HEAD(&cpu_buffer->reader_page->list);
1238 INIT_LIST_HEAD(&cpu_buffer->new_pages);
1239
1240 ret = rb_allocate_pages(cpu_buffer, nr_pages);
1241 if (ret < 0)
1242 goto fail_free_reader;
1243
1244 cpu_buffer->head_page
1245 = list_entry(cpu_buffer->pages, struct buffer_page, list);
1246 cpu_buffer->tail_page = cpu_buffer->commit_page = cpu_buffer->head_page;
1247
1248 rb_head_page_activate(cpu_buffer);
1249
1250 return cpu_buffer;
1251
1252 fail_free_reader:
1253 free_buffer_page(cpu_buffer->reader_page);
1254
1255 fail_free_buffer:
1256 kfree(cpu_buffer);
1257 return NULL;
1258 }
1259
rb_free_cpu_buffer(struct ring_buffer_per_cpu * cpu_buffer)1260 static void rb_free_cpu_buffer(struct ring_buffer_per_cpu *cpu_buffer)
1261 {
1262 struct list_head *head = cpu_buffer->pages;
1263 struct buffer_page *bpage, *tmp;
1264
1265 free_buffer_page(cpu_buffer->reader_page);
1266
1267 rb_head_page_deactivate(cpu_buffer);
1268
1269 if (head) {
1270 list_for_each_entry_safe(bpage, tmp, head, list) {
1271 list_del_init(&bpage->list);
1272 free_buffer_page(bpage);
1273 }
1274 bpage = list_entry(head, struct buffer_page, list);
1275 free_buffer_page(bpage);
1276 }
1277
1278 kfree(cpu_buffer);
1279 }
1280
1281 #ifdef CONFIG_HOTPLUG_CPU
1282 static int rb_cpu_notify(struct notifier_block *self,
1283 unsigned long action, void *hcpu);
1284 #endif
1285
1286 /**
1287 * __ring_buffer_alloc - allocate a new ring_buffer
1288 * @size: the size in bytes per cpu that is needed.
1289 * @flags: attributes to set for the ring buffer.
1290 *
1291 * Currently the only flag that is available is the RB_FL_OVERWRITE
1292 * flag. This flag means that the buffer will overwrite old data
1293 * when the buffer wraps. If this flag is not set, the buffer will
1294 * drop data when the tail hits the head.
1295 */
__ring_buffer_alloc(unsigned long size,unsigned flags,struct lock_class_key * key)1296 struct ring_buffer *__ring_buffer_alloc(unsigned long size, unsigned flags,
1297 struct lock_class_key *key)
1298 {
1299 struct ring_buffer *buffer;
1300 long nr_pages;
1301 int bsize;
1302 int cpu;
1303
1304 /* keep it in its own cache line */
1305 buffer = kzalloc(ALIGN(sizeof(*buffer), cache_line_size()),
1306 GFP_KERNEL);
1307 if (!buffer)
1308 return NULL;
1309
1310 if (!alloc_cpumask_var(&buffer->cpumask, GFP_KERNEL))
1311 goto fail_free_buffer;
1312
1313 nr_pages = DIV_ROUND_UP(size, BUF_PAGE_SIZE);
1314 buffer->flags = flags;
1315 buffer->clock = trace_clock_local;
1316 buffer->reader_lock_key = key;
1317
1318 init_irq_work(&buffer->irq_work.work, rb_wake_up_waiters);
1319 init_waitqueue_head(&buffer->irq_work.waiters);
1320
1321 /* need at least two pages */
1322 if (nr_pages < 2)
1323 nr_pages = 2;
1324
1325 /*
1326 * In case of non-hotplug cpu, if the ring-buffer is allocated
1327 * in early initcall, it will not be notified of secondary cpus.
1328 * In that off case, we need to allocate for all possible cpus.
1329 */
1330 #ifdef CONFIG_HOTPLUG_CPU
1331 cpu_notifier_register_begin();
1332 cpumask_copy(buffer->cpumask, cpu_online_mask);
1333 #else
1334 cpumask_copy(buffer->cpumask, cpu_possible_mask);
1335 #endif
1336 buffer->cpus = nr_cpu_ids;
1337
1338 bsize = sizeof(void *) * nr_cpu_ids;
1339 buffer->buffers = kzalloc(ALIGN(bsize, cache_line_size()),
1340 GFP_KERNEL);
1341 if (!buffer->buffers)
1342 goto fail_free_cpumask;
1343
1344 for_each_buffer_cpu(buffer, cpu) {
1345 buffer->buffers[cpu] =
1346 rb_allocate_cpu_buffer(buffer, nr_pages, cpu);
1347 if (!buffer->buffers[cpu])
1348 goto fail_free_buffers;
1349 }
1350
1351 #ifdef CONFIG_HOTPLUG_CPU
1352 buffer->cpu_notify.notifier_call = rb_cpu_notify;
1353 buffer->cpu_notify.priority = 0;
1354 __register_cpu_notifier(&buffer->cpu_notify);
1355 cpu_notifier_register_done();
1356 #endif
1357
1358 mutex_init(&buffer->mutex);
1359
1360 return buffer;
1361
1362 fail_free_buffers:
1363 for_each_buffer_cpu(buffer, cpu) {
1364 if (buffer->buffers[cpu])
1365 rb_free_cpu_buffer(buffer->buffers[cpu]);
1366 }
1367 kfree(buffer->buffers);
1368
1369 fail_free_cpumask:
1370 free_cpumask_var(buffer->cpumask);
1371 #ifdef CONFIG_HOTPLUG_CPU
1372 cpu_notifier_register_done();
1373 #endif
1374
1375 fail_free_buffer:
1376 kfree(buffer);
1377 return NULL;
1378 }
1379 EXPORT_SYMBOL_GPL(__ring_buffer_alloc);
1380
1381 /**
1382 * ring_buffer_free - free a ring buffer.
1383 * @buffer: the buffer to free.
1384 */
1385 void
ring_buffer_free(struct ring_buffer * buffer)1386 ring_buffer_free(struct ring_buffer *buffer)
1387 {
1388 int cpu;
1389
1390 #ifdef CONFIG_HOTPLUG_CPU
1391 cpu_notifier_register_begin();
1392 __unregister_cpu_notifier(&buffer->cpu_notify);
1393 #endif
1394
1395 for_each_buffer_cpu(buffer, cpu)
1396 rb_free_cpu_buffer(buffer->buffers[cpu]);
1397
1398 #ifdef CONFIG_HOTPLUG_CPU
1399 cpu_notifier_register_done();
1400 #endif
1401
1402 kfree(buffer->buffers);
1403 free_cpumask_var(buffer->cpumask);
1404
1405 kfree(buffer);
1406 }
1407 EXPORT_SYMBOL_GPL(ring_buffer_free);
1408
ring_buffer_set_clock(struct ring_buffer * buffer,u64 (* clock)(void))1409 void ring_buffer_set_clock(struct ring_buffer *buffer,
1410 u64 (*clock)(void))
1411 {
1412 buffer->clock = clock;
1413 }
1414
1415 static void rb_reset_cpu(struct ring_buffer_per_cpu *cpu_buffer);
1416
rb_page_entries(struct buffer_page * bpage)1417 static inline unsigned long rb_page_entries(struct buffer_page *bpage)
1418 {
1419 return local_read(&bpage->entries) & RB_WRITE_MASK;
1420 }
1421
rb_page_write(struct buffer_page * bpage)1422 static inline unsigned long rb_page_write(struct buffer_page *bpage)
1423 {
1424 return local_read(&bpage->write) & RB_WRITE_MASK;
1425 }
1426
1427 static int
rb_remove_pages(struct ring_buffer_per_cpu * cpu_buffer,unsigned long nr_pages)1428 rb_remove_pages(struct ring_buffer_per_cpu *cpu_buffer, unsigned long nr_pages)
1429 {
1430 struct list_head *tail_page, *to_remove, *next_page;
1431 struct buffer_page *to_remove_page, *tmp_iter_page;
1432 struct buffer_page *last_page, *first_page;
1433 unsigned long nr_removed;
1434 unsigned long head_bit;
1435 int page_entries;
1436
1437 head_bit = 0;
1438
1439 raw_spin_lock_irq(&cpu_buffer->reader_lock);
1440 atomic_inc(&cpu_buffer->record_disabled);
1441 /*
1442 * We don't race with the readers since we have acquired the reader
1443 * lock. We also don't race with writers after disabling recording.
1444 * This makes it easy to figure out the first and the last page to be
1445 * removed from the list. We unlink all the pages in between including
1446 * the first and last pages. This is done in a busy loop so that we
1447 * lose the least number of traces.
1448 * The pages are freed after we restart recording and unlock readers.
1449 */
1450 tail_page = &cpu_buffer->tail_page->list;
1451
1452 /*
1453 * tail page might be on reader page, we remove the next page
1454 * from the ring buffer
1455 */
1456 if (cpu_buffer->tail_page == cpu_buffer->reader_page)
1457 tail_page = rb_list_head(tail_page->next);
1458 to_remove = tail_page;
1459
1460 /* start of pages to remove */
1461 first_page = list_entry(rb_list_head(to_remove->next),
1462 struct buffer_page, list);
1463
1464 for (nr_removed = 0; nr_removed < nr_pages; nr_removed++) {
1465 to_remove = rb_list_head(to_remove)->next;
1466 head_bit |= (unsigned long)to_remove & RB_PAGE_HEAD;
1467 }
1468
1469 next_page = rb_list_head(to_remove)->next;
1470
1471 /*
1472 * Now we remove all pages between tail_page and next_page.
1473 * Make sure that we have head_bit value preserved for the
1474 * next page
1475 */
1476 tail_page->next = (struct list_head *)((unsigned long)next_page |
1477 head_bit);
1478 next_page = rb_list_head(next_page);
1479 next_page->prev = tail_page;
1480
1481 /* make sure pages points to a valid page in the ring buffer */
1482 cpu_buffer->pages = next_page;
1483
1484 /* update head page */
1485 if (head_bit)
1486 cpu_buffer->head_page = list_entry(next_page,
1487 struct buffer_page, list);
1488
1489 /*
1490 * change read pointer to make sure any read iterators reset
1491 * themselves
1492 */
1493 cpu_buffer->read = 0;
1494
1495 /* pages are removed, resume tracing and then free the pages */
1496 atomic_dec(&cpu_buffer->record_disabled);
1497 raw_spin_unlock_irq(&cpu_buffer->reader_lock);
1498
1499 RB_WARN_ON(cpu_buffer, list_empty(cpu_buffer->pages));
1500
1501 /* last buffer page to remove */
1502 last_page = list_entry(rb_list_head(to_remove), struct buffer_page,
1503 list);
1504 tmp_iter_page = first_page;
1505
1506 do {
1507 to_remove_page = tmp_iter_page;
1508 rb_inc_page(cpu_buffer, &tmp_iter_page);
1509
1510 /* update the counters */
1511 page_entries = rb_page_entries(to_remove_page);
1512 if (page_entries) {
1513 /*
1514 * If something was added to this page, it was full
1515 * since it is not the tail page. So we deduct the
1516 * bytes consumed in ring buffer from here.
1517 * Increment overrun to account for the lost events.
1518 */
1519 local_add(page_entries, &cpu_buffer->overrun);
1520 local_sub(BUF_PAGE_SIZE, &cpu_buffer->entries_bytes);
1521 }
1522
1523 /*
1524 * We have already removed references to this list item, just
1525 * free up the buffer_page and its page
1526 */
1527 free_buffer_page(to_remove_page);
1528 nr_removed--;
1529
1530 } while (to_remove_page != last_page);
1531
1532 RB_WARN_ON(cpu_buffer, nr_removed);
1533
1534 return nr_removed == 0;
1535 }
1536
1537 static int
rb_insert_pages(struct ring_buffer_per_cpu * cpu_buffer)1538 rb_insert_pages(struct ring_buffer_per_cpu *cpu_buffer)
1539 {
1540 struct list_head *pages = &cpu_buffer->new_pages;
1541 int retries, success;
1542
1543 raw_spin_lock_irq(&cpu_buffer->reader_lock);
1544 /*
1545 * We are holding the reader lock, so the reader page won't be swapped
1546 * in the ring buffer. Now we are racing with the writer trying to
1547 * move head page and the tail page.
1548 * We are going to adapt the reader page update process where:
1549 * 1. We first splice the start and end of list of new pages between
1550 * the head page and its previous page.
1551 * 2. We cmpxchg the prev_page->next to point from head page to the
1552 * start of new pages list.
1553 * 3. Finally, we update the head->prev to the end of new list.
1554 *
1555 * We will try this process 10 times, to make sure that we don't keep
1556 * spinning.
1557 */
1558 retries = 10;
1559 success = 0;
1560 while (retries--) {
1561 struct list_head *head_page, *prev_page, *r;
1562 struct list_head *last_page, *first_page;
1563 struct list_head *head_page_with_bit;
1564
1565 head_page = &rb_set_head_page(cpu_buffer)->list;
1566 if (!head_page)
1567 break;
1568 prev_page = head_page->prev;
1569
1570 first_page = pages->next;
1571 last_page = pages->prev;
1572
1573 head_page_with_bit = (struct list_head *)
1574 ((unsigned long)head_page | RB_PAGE_HEAD);
1575
1576 last_page->next = head_page_with_bit;
1577 first_page->prev = prev_page;
1578
1579 r = cmpxchg(&prev_page->next, head_page_with_bit, first_page);
1580
1581 if (r == head_page_with_bit) {
1582 /*
1583 * yay, we replaced the page pointer to our new list,
1584 * now, we just have to update to head page's prev
1585 * pointer to point to end of list
1586 */
1587 head_page->prev = last_page;
1588 success = 1;
1589 break;
1590 }
1591 }
1592
1593 if (success)
1594 INIT_LIST_HEAD(pages);
1595 /*
1596 * If we weren't successful in adding in new pages, warn and stop
1597 * tracing
1598 */
1599 RB_WARN_ON(cpu_buffer, !success);
1600 raw_spin_unlock_irq(&cpu_buffer->reader_lock);
1601
1602 /* free pages if they weren't inserted */
1603 if (!success) {
1604 struct buffer_page *bpage, *tmp;
1605 list_for_each_entry_safe(bpage, tmp, &cpu_buffer->new_pages,
1606 list) {
1607 list_del_init(&bpage->list);
1608 free_buffer_page(bpage);
1609 }
1610 }
1611 return success;
1612 }
1613
rb_update_pages(struct ring_buffer_per_cpu * cpu_buffer)1614 static void rb_update_pages(struct ring_buffer_per_cpu *cpu_buffer)
1615 {
1616 int success;
1617
1618 if (cpu_buffer->nr_pages_to_update > 0)
1619 success = rb_insert_pages(cpu_buffer);
1620 else
1621 success = rb_remove_pages(cpu_buffer,
1622 -cpu_buffer->nr_pages_to_update);
1623
1624 if (success)
1625 cpu_buffer->nr_pages += cpu_buffer->nr_pages_to_update;
1626 }
1627
update_pages_handler(struct work_struct * work)1628 static void update_pages_handler(struct work_struct *work)
1629 {
1630 struct ring_buffer_per_cpu *cpu_buffer = container_of(work,
1631 struct ring_buffer_per_cpu, update_pages_work);
1632 rb_update_pages(cpu_buffer);
1633 complete(&cpu_buffer->update_done);
1634 }
1635
1636 /**
1637 * ring_buffer_resize - resize the ring buffer
1638 * @buffer: the buffer to resize.
1639 * @size: the new size.
1640 * @cpu_id: the cpu buffer to resize
1641 *
1642 * Minimum size is 2 * BUF_PAGE_SIZE.
1643 *
1644 * Returns 0 on success and < 0 on failure.
1645 */
ring_buffer_resize(struct ring_buffer * buffer,unsigned long size,int cpu_id)1646 int ring_buffer_resize(struct ring_buffer *buffer, unsigned long size,
1647 int cpu_id)
1648 {
1649 struct ring_buffer_per_cpu *cpu_buffer;
1650 unsigned long nr_pages;
1651 int cpu, err = 0;
1652
1653 /*
1654 * Always succeed at resizing a non-existent buffer:
1655 */
1656 if (!buffer)
1657 return size;
1658
1659 /* Make sure the requested buffer exists */
1660 if (cpu_id != RING_BUFFER_ALL_CPUS &&
1661 !cpumask_test_cpu(cpu_id, buffer->cpumask))
1662 return size;
1663
1664 nr_pages = DIV_ROUND_UP(size, BUF_PAGE_SIZE);
1665
1666 /* we need a minimum of two pages */
1667 if (nr_pages < 2)
1668 nr_pages = 2;
1669
1670 size = nr_pages * BUF_PAGE_SIZE;
1671
1672 /*
1673 * Don't succeed if resizing is disabled, as a reader might be
1674 * manipulating the ring buffer and is expecting a sane state while
1675 * this is true.
1676 */
1677 if (atomic_read(&buffer->resize_disabled))
1678 return -EBUSY;
1679
1680 /* prevent another thread from changing buffer sizes */
1681 mutex_lock(&buffer->mutex);
1682
1683 if (cpu_id == RING_BUFFER_ALL_CPUS) {
1684 /* calculate the pages to update */
1685 for_each_buffer_cpu(buffer, cpu) {
1686 cpu_buffer = buffer->buffers[cpu];
1687
1688 cpu_buffer->nr_pages_to_update = nr_pages -
1689 cpu_buffer->nr_pages;
1690 /*
1691 * nothing more to do for removing pages or no update
1692 */
1693 if (cpu_buffer->nr_pages_to_update <= 0)
1694 continue;
1695 /*
1696 * to add pages, make sure all new pages can be
1697 * allocated without receiving ENOMEM
1698 */
1699 INIT_LIST_HEAD(&cpu_buffer->new_pages);
1700 if (__rb_allocate_pages(cpu_buffer->nr_pages_to_update,
1701 &cpu_buffer->new_pages, cpu)) {
1702 /* not enough memory for new pages */
1703 err = -ENOMEM;
1704 goto out_err;
1705 }
1706 }
1707
1708 get_online_cpus();
1709 /*
1710 * Fire off all the required work handlers
1711 * We can't schedule on offline CPUs, but it's not necessary
1712 * since we can change their buffer sizes without any race.
1713 */
1714 for_each_buffer_cpu(buffer, cpu) {
1715 cpu_buffer = buffer->buffers[cpu];
1716 if (!cpu_buffer->nr_pages_to_update)
1717 continue;
1718
1719 /* Can't run something on an offline CPU. */
1720 if (!cpu_online(cpu)) {
1721 rb_update_pages(cpu_buffer);
1722 cpu_buffer->nr_pages_to_update = 0;
1723 } else {
1724 schedule_work_on(cpu,
1725 &cpu_buffer->update_pages_work);
1726 }
1727 }
1728
1729 /* wait for all the updates to complete */
1730 for_each_buffer_cpu(buffer, cpu) {
1731 cpu_buffer = buffer->buffers[cpu];
1732 if (!cpu_buffer->nr_pages_to_update)
1733 continue;
1734
1735 if (cpu_online(cpu))
1736 wait_for_completion(&cpu_buffer->update_done);
1737 cpu_buffer->nr_pages_to_update = 0;
1738 }
1739
1740 put_online_cpus();
1741 } else {
1742 /* Make sure this CPU has been intitialized */
1743 if (!cpumask_test_cpu(cpu_id, buffer->cpumask))
1744 goto out;
1745
1746 cpu_buffer = buffer->buffers[cpu_id];
1747
1748 if (nr_pages == cpu_buffer->nr_pages)
1749 goto out;
1750
1751 cpu_buffer->nr_pages_to_update = nr_pages -
1752 cpu_buffer->nr_pages;
1753
1754 INIT_LIST_HEAD(&cpu_buffer->new_pages);
1755 if (cpu_buffer->nr_pages_to_update > 0 &&
1756 __rb_allocate_pages(cpu_buffer->nr_pages_to_update,
1757 &cpu_buffer->new_pages, cpu_id)) {
1758 err = -ENOMEM;
1759 goto out_err;
1760 }
1761
1762 get_online_cpus();
1763
1764 /* Can't run something on an offline CPU. */
1765 if (!cpu_online(cpu_id))
1766 rb_update_pages(cpu_buffer);
1767 else {
1768 schedule_work_on(cpu_id,
1769 &cpu_buffer->update_pages_work);
1770 wait_for_completion(&cpu_buffer->update_done);
1771 }
1772
1773 cpu_buffer->nr_pages_to_update = 0;
1774 put_online_cpus();
1775 }
1776
1777 out:
1778 /*
1779 * The ring buffer resize can happen with the ring buffer
1780 * enabled, so that the update disturbs the tracing as little
1781 * as possible. But if the buffer is disabled, we do not need
1782 * to worry about that, and we can take the time to verify
1783 * that the buffer is not corrupt.
1784 */
1785 if (atomic_read(&buffer->record_disabled)) {
1786 atomic_inc(&buffer->record_disabled);
1787 /*
1788 * Even though the buffer was disabled, we must make sure
1789 * that it is truly disabled before calling rb_check_pages.
1790 * There could have been a race between checking
1791 * record_disable and incrementing it.
1792 */
1793 synchronize_sched();
1794 for_each_buffer_cpu(buffer, cpu) {
1795 cpu_buffer = buffer->buffers[cpu];
1796 rb_check_pages(cpu_buffer);
1797 }
1798 atomic_dec(&buffer->record_disabled);
1799 }
1800
1801 mutex_unlock(&buffer->mutex);
1802 return size;
1803
1804 out_err:
1805 for_each_buffer_cpu(buffer, cpu) {
1806 struct buffer_page *bpage, *tmp;
1807
1808 cpu_buffer = buffer->buffers[cpu];
1809 cpu_buffer->nr_pages_to_update = 0;
1810
1811 if (list_empty(&cpu_buffer->new_pages))
1812 continue;
1813
1814 list_for_each_entry_safe(bpage, tmp, &cpu_buffer->new_pages,
1815 list) {
1816 list_del_init(&bpage->list);
1817 free_buffer_page(bpage);
1818 }
1819 }
1820 mutex_unlock(&buffer->mutex);
1821 return err;
1822 }
1823 EXPORT_SYMBOL_GPL(ring_buffer_resize);
1824
ring_buffer_change_overwrite(struct ring_buffer * buffer,int val)1825 void ring_buffer_change_overwrite(struct ring_buffer *buffer, int val)
1826 {
1827 mutex_lock(&buffer->mutex);
1828 if (val)
1829 buffer->flags |= RB_FL_OVERWRITE;
1830 else
1831 buffer->flags &= ~RB_FL_OVERWRITE;
1832 mutex_unlock(&buffer->mutex);
1833 }
1834 EXPORT_SYMBOL_GPL(ring_buffer_change_overwrite);
1835
1836 static inline void *
__rb_data_page_index(struct buffer_data_page * bpage,unsigned index)1837 __rb_data_page_index(struct buffer_data_page *bpage, unsigned index)
1838 {
1839 return bpage->data + index;
1840 }
1841
__rb_page_index(struct buffer_page * bpage,unsigned index)1842 static inline void *__rb_page_index(struct buffer_page *bpage, unsigned index)
1843 {
1844 return bpage->page->data + index;
1845 }
1846
1847 static inline struct ring_buffer_event *
rb_reader_event(struct ring_buffer_per_cpu * cpu_buffer)1848 rb_reader_event(struct ring_buffer_per_cpu *cpu_buffer)
1849 {
1850 return __rb_page_index(cpu_buffer->reader_page,
1851 cpu_buffer->reader_page->read);
1852 }
1853
1854 static inline struct ring_buffer_event *
rb_iter_head_event(struct ring_buffer_iter * iter)1855 rb_iter_head_event(struct ring_buffer_iter *iter)
1856 {
1857 return __rb_page_index(iter->head_page, iter->head);
1858 }
1859
rb_page_commit(struct buffer_page * bpage)1860 static inline unsigned rb_page_commit(struct buffer_page *bpage)
1861 {
1862 return local_read(&bpage->page->commit);
1863 }
1864
1865 /* Size is determined by what has been committed */
rb_page_size(struct buffer_page * bpage)1866 static inline unsigned rb_page_size(struct buffer_page *bpage)
1867 {
1868 return rb_page_commit(bpage);
1869 }
1870
1871 static inline unsigned
rb_commit_index(struct ring_buffer_per_cpu * cpu_buffer)1872 rb_commit_index(struct ring_buffer_per_cpu *cpu_buffer)
1873 {
1874 return rb_page_commit(cpu_buffer->commit_page);
1875 }
1876
1877 static inline unsigned
rb_event_index(struct ring_buffer_event * event)1878 rb_event_index(struct ring_buffer_event *event)
1879 {
1880 unsigned long addr = (unsigned long)event;
1881
1882 return (addr & ~PAGE_MASK) - BUF_PAGE_HDR_SIZE;
1883 }
1884
rb_inc_iter(struct ring_buffer_iter * iter)1885 static void rb_inc_iter(struct ring_buffer_iter *iter)
1886 {
1887 struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
1888
1889 /*
1890 * The iterator could be on the reader page (it starts there).
1891 * But the head could have moved, since the reader was
1892 * found. Check for this case and assign the iterator
1893 * to the head page instead of next.
1894 */
1895 if (iter->head_page == cpu_buffer->reader_page)
1896 iter->head_page = rb_set_head_page(cpu_buffer);
1897 else
1898 rb_inc_page(cpu_buffer, &iter->head_page);
1899
1900 iter->read_stamp = iter->head_page->page->time_stamp;
1901 iter->head = 0;
1902 }
1903
1904 /*
1905 * rb_handle_head_page - writer hit the head page
1906 *
1907 * Returns: +1 to retry page
1908 * 0 to continue
1909 * -1 on error
1910 */
1911 static int
rb_handle_head_page(struct ring_buffer_per_cpu * cpu_buffer,struct buffer_page * tail_page,struct buffer_page * next_page)1912 rb_handle_head_page(struct ring_buffer_per_cpu *cpu_buffer,
1913 struct buffer_page *tail_page,
1914 struct buffer_page *next_page)
1915 {
1916 struct buffer_page *new_head;
1917 int entries;
1918 int type;
1919 int ret;
1920
1921 entries = rb_page_entries(next_page);
1922
1923 /*
1924 * The hard part is here. We need to move the head
1925 * forward, and protect against both readers on
1926 * other CPUs and writers coming in via interrupts.
1927 */
1928 type = rb_head_page_set_update(cpu_buffer, next_page, tail_page,
1929 RB_PAGE_HEAD);
1930
1931 /*
1932 * type can be one of four:
1933 * NORMAL - an interrupt already moved it for us
1934 * HEAD - we are the first to get here.
1935 * UPDATE - we are the interrupt interrupting
1936 * a current move.
1937 * MOVED - a reader on another CPU moved the next
1938 * pointer to its reader page. Give up
1939 * and try again.
1940 */
1941
1942 switch (type) {
1943 case RB_PAGE_HEAD:
1944 /*
1945 * We changed the head to UPDATE, thus
1946 * it is our responsibility to update
1947 * the counters.
1948 */
1949 local_add(entries, &cpu_buffer->overrun);
1950 local_sub(BUF_PAGE_SIZE, &cpu_buffer->entries_bytes);
1951
1952 /*
1953 * The entries will be zeroed out when we move the
1954 * tail page.
1955 */
1956
1957 /* still more to do */
1958 break;
1959
1960 case RB_PAGE_UPDATE:
1961 /*
1962 * This is an interrupt that interrupt the
1963 * previous update. Still more to do.
1964 */
1965 break;
1966 case RB_PAGE_NORMAL:
1967 /*
1968 * An interrupt came in before the update
1969 * and processed this for us.
1970 * Nothing left to do.
1971 */
1972 return 1;
1973 case RB_PAGE_MOVED:
1974 /*
1975 * The reader is on another CPU and just did
1976 * a swap with our next_page.
1977 * Try again.
1978 */
1979 return 1;
1980 default:
1981 RB_WARN_ON(cpu_buffer, 1); /* WTF??? */
1982 return -1;
1983 }
1984
1985 /*
1986 * Now that we are here, the old head pointer is
1987 * set to UPDATE. This will keep the reader from
1988 * swapping the head page with the reader page.
1989 * The reader (on another CPU) will spin till
1990 * we are finished.
1991 *
1992 * We just need to protect against interrupts
1993 * doing the job. We will set the next pointer
1994 * to HEAD. After that, we set the old pointer
1995 * to NORMAL, but only if it was HEAD before.
1996 * otherwise we are an interrupt, and only
1997 * want the outer most commit to reset it.
1998 */
1999 new_head = next_page;
2000 rb_inc_page(cpu_buffer, &new_head);
2001
2002 ret = rb_head_page_set_head(cpu_buffer, new_head, next_page,
2003 RB_PAGE_NORMAL);
2004
2005 /*
2006 * Valid returns are:
2007 * HEAD - an interrupt came in and already set it.
2008 * NORMAL - One of two things:
2009 * 1) We really set it.
2010 * 2) A bunch of interrupts came in and moved
2011 * the page forward again.
2012 */
2013 switch (ret) {
2014 case RB_PAGE_HEAD:
2015 case RB_PAGE_NORMAL:
2016 /* OK */
2017 break;
2018 default:
2019 RB_WARN_ON(cpu_buffer, 1);
2020 return -1;
2021 }
2022
2023 /*
2024 * It is possible that an interrupt came in,
2025 * set the head up, then more interrupts came in
2026 * and moved it again. When we get back here,
2027 * the page would have been set to NORMAL but we
2028 * just set it back to HEAD.
2029 *
2030 * How do you detect this? Well, if that happened
2031 * the tail page would have moved.
2032 */
2033 if (ret == RB_PAGE_NORMAL) {
2034 struct buffer_page *buffer_tail_page;
2035
2036 buffer_tail_page = READ_ONCE(cpu_buffer->tail_page);
2037 /*
2038 * If the tail had moved passed next, then we need
2039 * to reset the pointer.
2040 */
2041 if (buffer_tail_page != tail_page &&
2042 buffer_tail_page != next_page)
2043 rb_head_page_set_normal(cpu_buffer, new_head,
2044 next_page,
2045 RB_PAGE_HEAD);
2046 }
2047
2048 /*
2049 * If this was the outer most commit (the one that
2050 * changed the original pointer from HEAD to UPDATE),
2051 * then it is up to us to reset it to NORMAL.
2052 */
2053 if (type == RB_PAGE_HEAD) {
2054 ret = rb_head_page_set_normal(cpu_buffer, next_page,
2055 tail_page,
2056 RB_PAGE_UPDATE);
2057 if (RB_WARN_ON(cpu_buffer,
2058 ret != RB_PAGE_UPDATE))
2059 return -1;
2060 }
2061
2062 return 0;
2063 }
2064
2065 static inline void
rb_reset_tail(struct ring_buffer_per_cpu * cpu_buffer,unsigned long tail,struct rb_event_info * info)2066 rb_reset_tail(struct ring_buffer_per_cpu *cpu_buffer,
2067 unsigned long tail, struct rb_event_info *info)
2068 {
2069 struct buffer_page *tail_page = info->tail_page;
2070 struct ring_buffer_event *event;
2071 unsigned long length = info->length;
2072
2073 /*
2074 * Only the event that crossed the page boundary
2075 * must fill the old tail_page with padding.
2076 */
2077 if (tail >= BUF_PAGE_SIZE) {
2078 /*
2079 * If the page was filled, then we still need
2080 * to update the real_end. Reset it to zero
2081 * and the reader will ignore it.
2082 */
2083 if (tail == BUF_PAGE_SIZE)
2084 tail_page->real_end = 0;
2085
2086 local_sub(length, &tail_page->write);
2087 return;
2088 }
2089
2090 event = __rb_page_index(tail_page, tail);
2091 kmemcheck_annotate_bitfield(event, bitfield);
2092
2093 /* account for padding bytes */
2094 local_add(BUF_PAGE_SIZE - tail, &cpu_buffer->entries_bytes);
2095
2096 /*
2097 * Save the original length to the meta data.
2098 * This will be used by the reader to add lost event
2099 * counter.
2100 */
2101 tail_page->real_end = tail;
2102
2103 /*
2104 * If this event is bigger than the minimum size, then
2105 * we need to be careful that we don't subtract the
2106 * write counter enough to allow another writer to slip
2107 * in on this page.
2108 * We put in a discarded commit instead, to make sure
2109 * that this space is not used again.
2110 *
2111 * If we are less than the minimum size, we don't need to
2112 * worry about it.
2113 */
2114 if (tail > (BUF_PAGE_SIZE - RB_EVNT_MIN_SIZE)) {
2115 /* No room for any events */
2116
2117 /* Mark the rest of the page with padding */
2118 rb_event_set_padding(event);
2119
2120 /* Set the write back to the previous setting */
2121 local_sub(length, &tail_page->write);
2122 return;
2123 }
2124
2125 /* Put in a discarded event */
2126 event->array[0] = (BUF_PAGE_SIZE - tail) - RB_EVNT_HDR_SIZE;
2127 event->type_len = RINGBUF_TYPE_PADDING;
2128 /* time delta must be non zero */
2129 event->time_delta = 1;
2130
2131 /* Set write to end of buffer */
2132 length = (tail + length) - BUF_PAGE_SIZE;
2133 local_sub(length, &tail_page->write);
2134 }
2135
2136 static inline void rb_end_commit(struct ring_buffer_per_cpu *cpu_buffer);
2137
2138 /*
2139 * This is the slow path, force gcc not to inline it.
2140 */
2141 static noinline struct ring_buffer_event *
rb_move_tail(struct ring_buffer_per_cpu * cpu_buffer,unsigned long tail,struct rb_event_info * info)2142 rb_move_tail(struct ring_buffer_per_cpu *cpu_buffer,
2143 unsigned long tail, struct rb_event_info *info)
2144 {
2145 struct buffer_page *tail_page = info->tail_page;
2146 struct buffer_page *commit_page = cpu_buffer->commit_page;
2147 struct ring_buffer *buffer = cpu_buffer->buffer;
2148 struct buffer_page *next_page;
2149 int ret;
2150
2151 next_page = tail_page;
2152
2153 rb_inc_page(cpu_buffer, &next_page);
2154
2155 /*
2156 * If for some reason, we had an interrupt storm that made
2157 * it all the way around the buffer, bail, and warn
2158 * about it.
2159 */
2160 if (unlikely(next_page == commit_page)) {
2161 local_inc(&cpu_buffer->commit_overrun);
2162 goto out_reset;
2163 }
2164
2165 /*
2166 * This is where the fun begins!
2167 *
2168 * We are fighting against races between a reader that
2169 * could be on another CPU trying to swap its reader
2170 * page with the buffer head.
2171 *
2172 * We are also fighting against interrupts coming in and
2173 * moving the head or tail on us as well.
2174 *
2175 * If the next page is the head page then we have filled
2176 * the buffer, unless the commit page is still on the
2177 * reader page.
2178 */
2179 if (rb_is_head_page(cpu_buffer, next_page, &tail_page->list)) {
2180
2181 /*
2182 * If the commit is not on the reader page, then
2183 * move the header page.
2184 */
2185 if (!rb_is_reader_page(cpu_buffer->commit_page)) {
2186 /*
2187 * If we are not in overwrite mode,
2188 * this is easy, just stop here.
2189 */
2190 if (!(buffer->flags & RB_FL_OVERWRITE)) {
2191 local_inc(&cpu_buffer->dropped_events);
2192 goto out_reset;
2193 }
2194
2195 ret = rb_handle_head_page(cpu_buffer,
2196 tail_page,
2197 next_page);
2198 if (ret < 0)
2199 goto out_reset;
2200 if (ret)
2201 goto out_again;
2202 } else {
2203 /*
2204 * We need to be careful here too. The
2205 * commit page could still be on the reader
2206 * page. We could have a small buffer, and
2207 * have filled up the buffer with events
2208 * from interrupts and such, and wrapped.
2209 *
2210 * Note, if the tail page is also the on the
2211 * reader_page, we let it move out.
2212 */
2213 if (unlikely((cpu_buffer->commit_page !=
2214 cpu_buffer->tail_page) &&
2215 (cpu_buffer->commit_page ==
2216 cpu_buffer->reader_page))) {
2217 local_inc(&cpu_buffer->commit_overrun);
2218 goto out_reset;
2219 }
2220 }
2221 }
2222
2223 rb_tail_page_update(cpu_buffer, tail_page, next_page);
2224
2225 out_again:
2226
2227 rb_reset_tail(cpu_buffer, tail, info);
2228
2229 /* Commit what we have for now. */
2230 rb_end_commit(cpu_buffer);
2231 /* rb_end_commit() decs committing */
2232 local_inc(&cpu_buffer->committing);
2233
2234 /* fail and let the caller try again */
2235 return ERR_PTR(-EAGAIN);
2236
2237 out_reset:
2238 /* reset write */
2239 rb_reset_tail(cpu_buffer, tail, info);
2240
2241 return NULL;
2242 }
2243
2244 /* Slow path, do not inline */
2245 static noinline struct ring_buffer_event *
rb_add_time_stamp(struct ring_buffer_event * event,u64 delta)2246 rb_add_time_stamp(struct ring_buffer_event *event, u64 delta)
2247 {
2248 event->type_len = RINGBUF_TYPE_TIME_EXTEND;
2249
2250 /* Not the first event on the page? */
2251 if (rb_event_index(event)) {
2252 event->time_delta = delta & TS_MASK;
2253 event->array[0] = delta >> TS_SHIFT;
2254 } else {
2255 /* nope, just zero it */
2256 event->time_delta = 0;
2257 event->array[0] = 0;
2258 }
2259
2260 return skip_time_extend(event);
2261 }
2262
2263 static inline bool rb_event_is_commit(struct ring_buffer_per_cpu *cpu_buffer,
2264 struct ring_buffer_event *event);
2265
2266 /**
2267 * rb_update_event - update event type and data
2268 * @event: the event to update
2269 * @type: the type of event
2270 * @length: the size of the event field in the ring buffer
2271 *
2272 * Update the type and data fields of the event. The length
2273 * is the actual size that is written to the ring buffer,
2274 * and with this, we can determine what to place into the
2275 * data field.
2276 */
2277 static void
rb_update_event(struct ring_buffer_per_cpu * cpu_buffer,struct ring_buffer_event * event,struct rb_event_info * info)2278 rb_update_event(struct ring_buffer_per_cpu *cpu_buffer,
2279 struct ring_buffer_event *event,
2280 struct rb_event_info *info)
2281 {
2282 unsigned length = info->length;
2283 u64 delta = info->delta;
2284
2285 /* Only a commit updates the timestamp */
2286 if (unlikely(!rb_event_is_commit(cpu_buffer, event)))
2287 delta = 0;
2288
2289 /*
2290 * If we need to add a timestamp, then we
2291 * add it to the start of the resevered space.
2292 */
2293 if (unlikely(info->add_timestamp)) {
2294 event = rb_add_time_stamp(event, delta);
2295 length -= RB_LEN_TIME_EXTEND;
2296 delta = 0;
2297 }
2298
2299 event->time_delta = delta;
2300 length -= RB_EVNT_HDR_SIZE;
2301 if (length > RB_MAX_SMALL_DATA || RB_FORCE_8BYTE_ALIGNMENT) {
2302 event->type_len = 0;
2303 event->array[0] = length;
2304 } else
2305 event->type_len = DIV_ROUND_UP(length, RB_ALIGNMENT);
2306 }
2307
rb_calculate_event_length(unsigned length)2308 static unsigned rb_calculate_event_length(unsigned length)
2309 {
2310 struct ring_buffer_event event; /* Used only for sizeof array */
2311
2312 /* zero length can cause confusions */
2313 if (!length)
2314 length++;
2315
2316 if (length > RB_MAX_SMALL_DATA || RB_FORCE_8BYTE_ALIGNMENT)
2317 length += sizeof(event.array[0]);
2318
2319 length += RB_EVNT_HDR_SIZE;
2320 length = ALIGN(length, RB_ARCH_ALIGNMENT);
2321
2322 /*
2323 * In case the time delta is larger than the 27 bits for it
2324 * in the header, we need to add a timestamp. If another
2325 * event comes in when trying to discard this one to increase
2326 * the length, then the timestamp will be added in the allocated
2327 * space of this event. If length is bigger than the size needed
2328 * for the TIME_EXTEND, then padding has to be used. The events
2329 * length must be either RB_LEN_TIME_EXTEND, or greater than or equal
2330 * to RB_LEN_TIME_EXTEND + 8, as 8 is the minimum size for padding.
2331 * As length is a multiple of 4, we only need to worry if it
2332 * is 12 (RB_LEN_TIME_EXTEND + 4).
2333 */
2334 if (length == RB_LEN_TIME_EXTEND + RB_ALIGNMENT)
2335 length += RB_ALIGNMENT;
2336
2337 return length;
2338 }
2339
2340 #ifndef CONFIG_HAVE_UNSTABLE_SCHED_CLOCK
sched_clock_stable(void)2341 static inline bool sched_clock_stable(void)
2342 {
2343 return true;
2344 }
2345 #endif
2346
2347 static inline int
rb_try_to_discard(struct ring_buffer_per_cpu * cpu_buffer,struct ring_buffer_event * event)2348 rb_try_to_discard(struct ring_buffer_per_cpu *cpu_buffer,
2349 struct ring_buffer_event *event)
2350 {
2351 unsigned long new_index, old_index;
2352 struct buffer_page *bpage;
2353 unsigned long index;
2354 unsigned long addr;
2355
2356 new_index = rb_event_index(event);
2357 old_index = new_index + rb_event_ts_length(event);
2358 addr = (unsigned long)event;
2359 addr &= PAGE_MASK;
2360
2361 bpage = READ_ONCE(cpu_buffer->tail_page);
2362
2363 if (bpage->page == (void *)addr && rb_page_write(bpage) == old_index) {
2364 unsigned long write_mask =
2365 local_read(&bpage->write) & ~RB_WRITE_MASK;
2366 unsigned long event_length = rb_event_length(event);
2367 /*
2368 * This is on the tail page. It is possible that
2369 * a write could come in and move the tail page
2370 * and write to the next page. That is fine
2371 * because we just shorten what is on this page.
2372 */
2373 old_index += write_mask;
2374 new_index += write_mask;
2375 index = local_cmpxchg(&bpage->write, old_index, new_index);
2376 if (index == old_index) {
2377 /* update counters */
2378 local_sub(event_length, &cpu_buffer->entries_bytes);
2379 return 1;
2380 }
2381 }
2382
2383 /* could not discard */
2384 return 0;
2385 }
2386
rb_start_commit(struct ring_buffer_per_cpu * cpu_buffer)2387 static void rb_start_commit(struct ring_buffer_per_cpu *cpu_buffer)
2388 {
2389 local_inc(&cpu_buffer->committing);
2390 local_inc(&cpu_buffer->commits);
2391 }
2392
2393 static void
rb_set_commit_to_write(struct ring_buffer_per_cpu * cpu_buffer)2394 rb_set_commit_to_write(struct ring_buffer_per_cpu *cpu_buffer)
2395 {
2396 unsigned long max_count;
2397
2398 /*
2399 * We only race with interrupts and NMIs on this CPU.
2400 * If we own the commit event, then we can commit
2401 * all others that interrupted us, since the interruptions
2402 * are in stack format (they finish before they come
2403 * back to us). This allows us to do a simple loop to
2404 * assign the commit to the tail.
2405 */
2406 again:
2407 max_count = cpu_buffer->nr_pages * 100;
2408
2409 while (cpu_buffer->commit_page != READ_ONCE(cpu_buffer->tail_page)) {
2410 if (RB_WARN_ON(cpu_buffer, !(--max_count)))
2411 return;
2412 if (RB_WARN_ON(cpu_buffer,
2413 rb_is_reader_page(cpu_buffer->tail_page)))
2414 return;
2415 local_set(&cpu_buffer->commit_page->page->commit,
2416 rb_page_write(cpu_buffer->commit_page));
2417 rb_inc_page(cpu_buffer, &cpu_buffer->commit_page);
2418 /* Only update the write stamp if the page has an event */
2419 if (rb_page_write(cpu_buffer->commit_page))
2420 cpu_buffer->write_stamp =
2421 cpu_buffer->commit_page->page->time_stamp;
2422 /* add barrier to keep gcc from optimizing too much */
2423 barrier();
2424 }
2425 while (rb_commit_index(cpu_buffer) !=
2426 rb_page_write(cpu_buffer->commit_page)) {
2427
2428 local_set(&cpu_buffer->commit_page->page->commit,
2429 rb_page_write(cpu_buffer->commit_page));
2430 RB_WARN_ON(cpu_buffer,
2431 local_read(&cpu_buffer->commit_page->page->commit) &
2432 ~RB_WRITE_MASK);
2433 barrier();
2434 }
2435
2436 /* again, keep gcc from optimizing */
2437 barrier();
2438
2439 /*
2440 * If an interrupt came in just after the first while loop
2441 * and pushed the tail page forward, we will be left with
2442 * a dangling commit that will never go forward.
2443 */
2444 if (unlikely(cpu_buffer->commit_page != READ_ONCE(cpu_buffer->tail_page)))
2445 goto again;
2446 }
2447
rb_end_commit(struct ring_buffer_per_cpu * cpu_buffer)2448 static inline void rb_end_commit(struct ring_buffer_per_cpu *cpu_buffer)
2449 {
2450 unsigned long commits;
2451
2452 if (RB_WARN_ON(cpu_buffer,
2453 !local_read(&cpu_buffer->committing)))
2454 return;
2455
2456 again:
2457 commits = local_read(&cpu_buffer->commits);
2458 /* synchronize with interrupts */
2459 barrier();
2460 if (local_read(&cpu_buffer->committing) == 1)
2461 rb_set_commit_to_write(cpu_buffer);
2462
2463 local_dec(&cpu_buffer->committing);
2464
2465 /* synchronize with interrupts */
2466 barrier();
2467
2468 /*
2469 * Need to account for interrupts coming in between the
2470 * updating of the commit page and the clearing of the
2471 * committing counter.
2472 */
2473 if (unlikely(local_read(&cpu_buffer->commits) != commits) &&
2474 !local_read(&cpu_buffer->committing)) {
2475 local_inc(&cpu_buffer->committing);
2476 goto again;
2477 }
2478 }
2479
rb_event_discard(struct ring_buffer_event * event)2480 static inline void rb_event_discard(struct ring_buffer_event *event)
2481 {
2482 if (event->type_len == RINGBUF_TYPE_TIME_EXTEND)
2483 event = skip_time_extend(event);
2484
2485 /* array[0] holds the actual length for the discarded event */
2486 event->array[0] = rb_event_data_length(event) - RB_EVNT_HDR_SIZE;
2487 event->type_len = RINGBUF_TYPE_PADDING;
2488 /* time delta must be non zero */
2489 if (!event->time_delta)
2490 event->time_delta = 1;
2491 }
2492
2493 static inline bool
rb_event_is_commit(struct ring_buffer_per_cpu * cpu_buffer,struct ring_buffer_event * event)2494 rb_event_is_commit(struct ring_buffer_per_cpu *cpu_buffer,
2495 struct ring_buffer_event *event)
2496 {
2497 unsigned long addr = (unsigned long)event;
2498 unsigned long index;
2499
2500 index = rb_event_index(event);
2501 addr &= PAGE_MASK;
2502
2503 return cpu_buffer->commit_page->page == (void *)addr &&
2504 rb_commit_index(cpu_buffer) == index;
2505 }
2506
2507 static void
rb_update_write_stamp(struct ring_buffer_per_cpu * cpu_buffer,struct ring_buffer_event * event)2508 rb_update_write_stamp(struct ring_buffer_per_cpu *cpu_buffer,
2509 struct ring_buffer_event *event)
2510 {
2511 u64 delta;
2512
2513 /*
2514 * The event first in the commit queue updates the
2515 * time stamp.
2516 */
2517 if (rb_event_is_commit(cpu_buffer, event)) {
2518 /*
2519 * A commit event that is first on a page
2520 * updates the write timestamp with the page stamp
2521 */
2522 if (!rb_event_index(event))
2523 cpu_buffer->write_stamp =
2524 cpu_buffer->commit_page->page->time_stamp;
2525 else if (event->type_len == RINGBUF_TYPE_TIME_EXTEND) {
2526 delta = event->array[0];
2527 delta <<= TS_SHIFT;
2528 delta += event->time_delta;
2529 cpu_buffer->write_stamp += delta;
2530 } else
2531 cpu_buffer->write_stamp += event->time_delta;
2532 }
2533 }
2534
rb_commit(struct ring_buffer_per_cpu * cpu_buffer,struct ring_buffer_event * event)2535 static void rb_commit(struct ring_buffer_per_cpu *cpu_buffer,
2536 struct ring_buffer_event *event)
2537 {
2538 local_inc(&cpu_buffer->entries);
2539 rb_update_write_stamp(cpu_buffer, event);
2540 rb_end_commit(cpu_buffer);
2541 }
2542
2543 static __always_inline void
rb_wakeups(struct ring_buffer * buffer,struct ring_buffer_per_cpu * cpu_buffer)2544 rb_wakeups(struct ring_buffer *buffer, struct ring_buffer_per_cpu *cpu_buffer)
2545 {
2546 bool pagebusy;
2547
2548 if (buffer->irq_work.waiters_pending) {
2549 buffer->irq_work.waiters_pending = false;
2550 /* irq_work_queue() supplies it's own memory barriers */
2551 irq_work_queue(&buffer->irq_work.work);
2552 }
2553
2554 if (cpu_buffer->irq_work.waiters_pending) {
2555 cpu_buffer->irq_work.waiters_pending = false;
2556 /* irq_work_queue() supplies it's own memory barriers */
2557 irq_work_queue(&cpu_buffer->irq_work.work);
2558 }
2559
2560 pagebusy = cpu_buffer->reader_page == cpu_buffer->commit_page;
2561
2562 if (!pagebusy && cpu_buffer->irq_work.full_waiters_pending) {
2563 cpu_buffer->irq_work.wakeup_full = true;
2564 cpu_buffer->irq_work.full_waiters_pending = false;
2565 /* irq_work_queue() supplies it's own memory barriers */
2566 irq_work_queue(&cpu_buffer->irq_work.work);
2567 }
2568 }
2569
2570 /*
2571 * The lock and unlock are done within a preempt disable section.
2572 * The current_context per_cpu variable can only be modified
2573 * by the current task between lock and unlock. But it can
2574 * be modified more than once via an interrupt. To pass this
2575 * information from the lock to the unlock without having to
2576 * access the 'in_interrupt()' functions again (which do show
2577 * a bit of overhead in something as critical as function tracing,
2578 * we use a bitmask trick.
2579 *
2580 * bit 0 = NMI context
2581 * bit 1 = IRQ context
2582 * bit 2 = SoftIRQ context
2583 * bit 3 = normal context.
2584 *
2585 * This works because this is the order of contexts that can
2586 * preempt other contexts. A SoftIRQ never preempts an IRQ
2587 * context.
2588 *
2589 * When the context is determined, the corresponding bit is
2590 * checked and set (if it was set, then a recursion of that context
2591 * happened).
2592 *
2593 * On unlock, we need to clear this bit. To do so, just subtract
2594 * 1 from the current_context and AND it to itself.
2595 *
2596 * (binary)
2597 * 101 - 1 = 100
2598 * 101 & 100 = 100 (clearing bit zero)
2599 *
2600 * 1010 - 1 = 1001
2601 * 1010 & 1001 = 1000 (clearing bit 1)
2602 *
2603 * The least significant bit can be cleared this way, and it
2604 * just so happens that it is the same bit corresponding to
2605 * the current context.
2606 */
2607
2608 static __always_inline int
trace_recursive_lock(struct ring_buffer_per_cpu * cpu_buffer)2609 trace_recursive_lock(struct ring_buffer_per_cpu *cpu_buffer)
2610 {
2611 unsigned int val = cpu_buffer->current_context;
2612 int bit;
2613
2614 if (in_interrupt()) {
2615 if (in_nmi())
2616 bit = RB_CTX_NMI;
2617 else if (in_irq())
2618 bit = RB_CTX_IRQ;
2619 else
2620 bit = RB_CTX_SOFTIRQ;
2621 } else
2622 bit = RB_CTX_NORMAL;
2623
2624 if (unlikely(val & (1 << bit)))
2625 return 1;
2626
2627 val |= (1 << bit);
2628 cpu_buffer->current_context = val;
2629
2630 return 0;
2631 }
2632
2633 static __always_inline void
trace_recursive_unlock(struct ring_buffer_per_cpu * cpu_buffer)2634 trace_recursive_unlock(struct ring_buffer_per_cpu *cpu_buffer)
2635 {
2636 cpu_buffer->current_context &= cpu_buffer->current_context - 1;
2637 }
2638
2639 /**
2640 * ring_buffer_unlock_commit - commit a reserved
2641 * @buffer: The buffer to commit to
2642 * @event: The event pointer to commit.
2643 *
2644 * This commits the data to the ring buffer, and releases any locks held.
2645 *
2646 * Must be paired with ring_buffer_lock_reserve.
2647 */
ring_buffer_unlock_commit(struct ring_buffer * buffer,struct ring_buffer_event * event)2648 int ring_buffer_unlock_commit(struct ring_buffer *buffer,
2649 struct ring_buffer_event *event)
2650 {
2651 struct ring_buffer_per_cpu *cpu_buffer;
2652 int cpu = raw_smp_processor_id();
2653
2654 cpu_buffer = buffer->buffers[cpu];
2655
2656 rb_commit(cpu_buffer, event);
2657
2658 rb_wakeups(buffer, cpu_buffer);
2659
2660 trace_recursive_unlock(cpu_buffer);
2661
2662 preempt_enable_notrace();
2663
2664 return 0;
2665 }
2666 EXPORT_SYMBOL_GPL(ring_buffer_unlock_commit);
2667
2668 static noinline void
rb_handle_timestamp(struct ring_buffer_per_cpu * cpu_buffer,struct rb_event_info * info)2669 rb_handle_timestamp(struct ring_buffer_per_cpu *cpu_buffer,
2670 struct rb_event_info *info)
2671 {
2672 WARN_ONCE(info->delta > (1ULL << 59),
2673 KERN_WARNING "Delta way too big! %llu ts=%llu write stamp = %llu\n%s",
2674 (unsigned long long)info->delta,
2675 (unsigned long long)info->ts,
2676 (unsigned long long)cpu_buffer->write_stamp,
2677 sched_clock_stable() ? "" :
2678 "If you just came from a suspend/resume,\n"
2679 "please switch to the trace global clock:\n"
2680 " echo global > /sys/kernel/debug/tracing/trace_clock\n");
2681 info->add_timestamp = 1;
2682 }
2683
2684 static struct ring_buffer_event *
__rb_reserve_next(struct ring_buffer_per_cpu * cpu_buffer,struct rb_event_info * info)2685 __rb_reserve_next(struct ring_buffer_per_cpu *cpu_buffer,
2686 struct rb_event_info *info)
2687 {
2688 struct ring_buffer_event *event;
2689 struct buffer_page *tail_page;
2690 unsigned long tail, write;
2691
2692 /*
2693 * If the time delta since the last event is too big to
2694 * hold in the time field of the event, then we append a
2695 * TIME EXTEND event ahead of the data event.
2696 */
2697 if (unlikely(info->add_timestamp))
2698 info->length += RB_LEN_TIME_EXTEND;
2699
2700 /* Don't let the compiler play games with cpu_buffer->tail_page */
2701 tail_page = info->tail_page = READ_ONCE(cpu_buffer->tail_page);
2702 write = local_add_return(info->length, &tail_page->write);
2703
2704 /* set write to only the index of the write */
2705 write &= RB_WRITE_MASK;
2706 tail = write - info->length;
2707
2708 /*
2709 * If this is the first commit on the page, then it has the same
2710 * timestamp as the page itself.
2711 */
2712 if (!tail)
2713 info->delta = 0;
2714
2715 /* See if we shot pass the end of this buffer page */
2716 if (unlikely(write > BUF_PAGE_SIZE))
2717 return rb_move_tail(cpu_buffer, tail, info);
2718
2719 /* We reserved something on the buffer */
2720
2721 event = __rb_page_index(tail_page, tail);
2722 kmemcheck_annotate_bitfield(event, bitfield);
2723 rb_update_event(cpu_buffer, event, info);
2724
2725 local_inc(&tail_page->entries);
2726
2727 /*
2728 * If this is the first commit on the page, then update
2729 * its timestamp.
2730 */
2731 if (!tail)
2732 tail_page->page->time_stamp = info->ts;
2733
2734 /* account for these added bytes */
2735 local_add(info->length, &cpu_buffer->entries_bytes);
2736
2737 return event;
2738 }
2739
2740 static struct ring_buffer_event *
rb_reserve_next_event(struct ring_buffer * buffer,struct ring_buffer_per_cpu * cpu_buffer,unsigned long length)2741 rb_reserve_next_event(struct ring_buffer *buffer,
2742 struct ring_buffer_per_cpu *cpu_buffer,
2743 unsigned long length)
2744 {
2745 struct ring_buffer_event *event;
2746 struct rb_event_info info;
2747 int nr_loops = 0;
2748 u64 diff;
2749
2750 rb_start_commit(cpu_buffer);
2751
2752 #ifdef CONFIG_RING_BUFFER_ALLOW_SWAP
2753 /*
2754 * Due to the ability to swap a cpu buffer from a buffer
2755 * it is possible it was swapped before we committed.
2756 * (committing stops a swap). We check for it here and
2757 * if it happened, we have to fail the write.
2758 */
2759 barrier();
2760 if (unlikely(ACCESS_ONCE(cpu_buffer->buffer) != buffer)) {
2761 local_dec(&cpu_buffer->committing);
2762 local_dec(&cpu_buffer->commits);
2763 return NULL;
2764 }
2765 #endif
2766
2767 info.length = rb_calculate_event_length(length);
2768 again:
2769 info.add_timestamp = 0;
2770 info.delta = 0;
2771
2772 /*
2773 * We allow for interrupts to reenter here and do a trace.
2774 * If one does, it will cause this original code to loop
2775 * back here. Even with heavy interrupts happening, this
2776 * should only happen a few times in a row. If this happens
2777 * 1000 times in a row, there must be either an interrupt
2778 * storm or we have something buggy.
2779 * Bail!
2780 */
2781 if (RB_WARN_ON(cpu_buffer, ++nr_loops > 1000))
2782 goto out_fail;
2783
2784 info.ts = rb_time_stamp(cpu_buffer->buffer);
2785 diff = info.ts - cpu_buffer->write_stamp;
2786
2787 /* make sure this diff is calculated here */
2788 barrier();
2789
2790 /* Did the write stamp get updated already? */
2791 if (likely(info.ts >= cpu_buffer->write_stamp)) {
2792 info.delta = diff;
2793 if (unlikely(test_time_stamp(info.delta)))
2794 rb_handle_timestamp(cpu_buffer, &info);
2795 }
2796
2797 event = __rb_reserve_next(cpu_buffer, &info);
2798
2799 if (unlikely(PTR_ERR(event) == -EAGAIN)) {
2800 if (info.add_timestamp)
2801 info.length -= RB_LEN_TIME_EXTEND;
2802 goto again;
2803 }
2804
2805 if (!event)
2806 goto out_fail;
2807
2808 return event;
2809
2810 out_fail:
2811 rb_end_commit(cpu_buffer);
2812 return NULL;
2813 }
2814
2815 /**
2816 * ring_buffer_lock_reserve - reserve a part of the buffer
2817 * @buffer: the ring buffer to reserve from
2818 * @length: the length of the data to reserve (excluding event header)
2819 *
2820 * Returns a reseverd event on the ring buffer to copy directly to.
2821 * The user of this interface will need to get the body to write into
2822 * and can use the ring_buffer_event_data() interface.
2823 *
2824 * The length is the length of the data needed, not the event length
2825 * which also includes the event header.
2826 *
2827 * Must be paired with ring_buffer_unlock_commit, unless NULL is returned.
2828 * If NULL is returned, then nothing has been allocated or locked.
2829 */
2830 struct ring_buffer_event *
ring_buffer_lock_reserve(struct ring_buffer * buffer,unsigned long length)2831 ring_buffer_lock_reserve(struct ring_buffer *buffer, unsigned long length)
2832 {
2833 struct ring_buffer_per_cpu *cpu_buffer;
2834 struct ring_buffer_event *event;
2835 int cpu;
2836
2837 /* If we are tracing schedule, we don't want to recurse */
2838 preempt_disable_notrace();
2839
2840 if (unlikely(atomic_read(&buffer->record_disabled)))
2841 goto out;
2842
2843 cpu = raw_smp_processor_id();
2844
2845 if (unlikely(!cpumask_test_cpu(cpu, buffer->cpumask)))
2846 goto out;
2847
2848 cpu_buffer = buffer->buffers[cpu];
2849
2850 if (unlikely(atomic_read(&cpu_buffer->record_disabled)))
2851 goto out;
2852
2853 if (unlikely(length > BUF_MAX_DATA_SIZE))
2854 goto out;
2855
2856 if (unlikely(trace_recursive_lock(cpu_buffer)))
2857 goto out;
2858
2859 event = rb_reserve_next_event(buffer, cpu_buffer, length);
2860 if (!event)
2861 goto out_unlock;
2862
2863 return event;
2864
2865 out_unlock:
2866 trace_recursive_unlock(cpu_buffer);
2867 out:
2868 preempt_enable_notrace();
2869 return NULL;
2870 }
2871 EXPORT_SYMBOL_GPL(ring_buffer_lock_reserve);
2872
2873 /*
2874 * Decrement the entries to the page that an event is on.
2875 * The event does not even need to exist, only the pointer
2876 * to the page it is on. This may only be called before the commit
2877 * takes place.
2878 */
2879 static inline void
rb_decrement_entry(struct ring_buffer_per_cpu * cpu_buffer,struct ring_buffer_event * event)2880 rb_decrement_entry(struct ring_buffer_per_cpu *cpu_buffer,
2881 struct ring_buffer_event *event)
2882 {
2883 unsigned long addr = (unsigned long)event;
2884 struct buffer_page *bpage = cpu_buffer->commit_page;
2885 struct buffer_page *start;
2886
2887 addr &= PAGE_MASK;
2888
2889 /* Do the likely case first */
2890 if (likely(bpage->page == (void *)addr)) {
2891 local_dec(&bpage->entries);
2892 return;
2893 }
2894
2895 /*
2896 * Because the commit page may be on the reader page we
2897 * start with the next page and check the end loop there.
2898 */
2899 rb_inc_page(cpu_buffer, &bpage);
2900 start = bpage;
2901 do {
2902 if (bpage->page == (void *)addr) {
2903 local_dec(&bpage->entries);
2904 return;
2905 }
2906 rb_inc_page(cpu_buffer, &bpage);
2907 } while (bpage != start);
2908
2909 /* commit not part of this buffer?? */
2910 RB_WARN_ON(cpu_buffer, 1);
2911 }
2912
2913 /**
2914 * ring_buffer_commit_discard - discard an event that has not been committed
2915 * @buffer: the ring buffer
2916 * @event: non committed event to discard
2917 *
2918 * Sometimes an event that is in the ring buffer needs to be ignored.
2919 * This function lets the user discard an event in the ring buffer
2920 * and then that event will not be read later.
2921 *
2922 * This function only works if it is called before the the item has been
2923 * committed. It will try to free the event from the ring buffer
2924 * if another event has not been added behind it.
2925 *
2926 * If another event has been added behind it, it will set the event
2927 * up as discarded, and perform the commit.
2928 *
2929 * If this function is called, do not call ring_buffer_unlock_commit on
2930 * the event.
2931 */
ring_buffer_discard_commit(struct ring_buffer * buffer,struct ring_buffer_event * event)2932 void ring_buffer_discard_commit(struct ring_buffer *buffer,
2933 struct ring_buffer_event *event)
2934 {
2935 struct ring_buffer_per_cpu *cpu_buffer;
2936 int cpu;
2937
2938 /* The event is discarded regardless */
2939 rb_event_discard(event);
2940
2941 cpu = smp_processor_id();
2942 cpu_buffer = buffer->buffers[cpu];
2943
2944 /*
2945 * This must only be called if the event has not been
2946 * committed yet. Thus we can assume that preemption
2947 * is still disabled.
2948 */
2949 RB_WARN_ON(buffer, !local_read(&cpu_buffer->committing));
2950
2951 rb_decrement_entry(cpu_buffer, event);
2952 if (rb_try_to_discard(cpu_buffer, event))
2953 goto out;
2954
2955 /*
2956 * The commit is still visible by the reader, so we
2957 * must still update the timestamp.
2958 */
2959 rb_update_write_stamp(cpu_buffer, event);
2960 out:
2961 rb_end_commit(cpu_buffer);
2962
2963 trace_recursive_unlock(cpu_buffer);
2964
2965 preempt_enable_notrace();
2966
2967 }
2968 EXPORT_SYMBOL_GPL(ring_buffer_discard_commit);
2969
2970 /**
2971 * ring_buffer_write - write data to the buffer without reserving
2972 * @buffer: The ring buffer to write to.
2973 * @length: The length of the data being written (excluding the event header)
2974 * @data: The data to write to the buffer.
2975 *
2976 * This is like ring_buffer_lock_reserve and ring_buffer_unlock_commit as
2977 * one function. If you already have the data to write to the buffer, it
2978 * may be easier to simply call this function.
2979 *
2980 * Note, like ring_buffer_lock_reserve, the length is the length of the data
2981 * and not the length of the event which would hold the header.
2982 */
ring_buffer_write(struct ring_buffer * buffer,unsigned long length,void * data)2983 int ring_buffer_write(struct ring_buffer *buffer,
2984 unsigned long length,
2985 void *data)
2986 {
2987 struct ring_buffer_per_cpu *cpu_buffer;
2988 struct ring_buffer_event *event;
2989 void *body;
2990 int ret = -EBUSY;
2991 int cpu;
2992
2993 preempt_disable_notrace();
2994
2995 if (atomic_read(&buffer->record_disabled))
2996 goto out;
2997
2998 cpu = raw_smp_processor_id();
2999
3000 if (!cpumask_test_cpu(cpu, buffer->cpumask))
3001 goto out;
3002
3003 cpu_buffer = buffer->buffers[cpu];
3004
3005 if (atomic_read(&cpu_buffer->record_disabled))
3006 goto out;
3007
3008 if (length > BUF_MAX_DATA_SIZE)
3009 goto out;
3010
3011 if (unlikely(trace_recursive_lock(cpu_buffer)))
3012 goto out;
3013
3014 event = rb_reserve_next_event(buffer, cpu_buffer, length);
3015 if (!event)
3016 goto out_unlock;
3017
3018 body = rb_event_data(event);
3019
3020 memcpy(body, data, length);
3021
3022 rb_commit(cpu_buffer, event);
3023
3024 rb_wakeups(buffer, cpu_buffer);
3025
3026 ret = 0;
3027
3028 out_unlock:
3029 trace_recursive_unlock(cpu_buffer);
3030
3031 out:
3032 preempt_enable_notrace();
3033
3034 return ret;
3035 }
3036 EXPORT_SYMBOL_GPL(ring_buffer_write);
3037
rb_per_cpu_empty(struct ring_buffer_per_cpu * cpu_buffer)3038 static bool rb_per_cpu_empty(struct ring_buffer_per_cpu *cpu_buffer)
3039 {
3040 struct buffer_page *reader = cpu_buffer->reader_page;
3041 struct buffer_page *head = rb_set_head_page(cpu_buffer);
3042 struct buffer_page *commit = cpu_buffer->commit_page;
3043
3044 /* In case of error, head will be NULL */
3045 if (unlikely(!head))
3046 return true;
3047
3048 return reader->read == rb_page_commit(reader) &&
3049 (commit == reader ||
3050 (commit == head &&
3051 head->read == rb_page_commit(commit)));
3052 }
3053
3054 /**
3055 * ring_buffer_record_disable - stop all writes into the buffer
3056 * @buffer: The ring buffer to stop writes to.
3057 *
3058 * This prevents all writes to the buffer. Any attempt to write
3059 * to the buffer after this will fail and return NULL.
3060 *
3061 * The caller should call synchronize_sched() after this.
3062 */
ring_buffer_record_disable(struct ring_buffer * buffer)3063 void ring_buffer_record_disable(struct ring_buffer *buffer)
3064 {
3065 atomic_inc(&buffer->record_disabled);
3066 }
3067 EXPORT_SYMBOL_GPL(ring_buffer_record_disable);
3068
3069 /**
3070 * ring_buffer_record_enable - enable writes to the buffer
3071 * @buffer: The ring buffer to enable writes
3072 *
3073 * Note, multiple disables will need the same number of enables
3074 * to truly enable the writing (much like preempt_disable).
3075 */
ring_buffer_record_enable(struct ring_buffer * buffer)3076 void ring_buffer_record_enable(struct ring_buffer *buffer)
3077 {
3078 atomic_dec(&buffer->record_disabled);
3079 }
3080 EXPORT_SYMBOL_GPL(ring_buffer_record_enable);
3081
3082 /**
3083 * ring_buffer_record_off - stop all writes into the buffer
3084 * @buffer: The ring buffer to stop writes to.
3085 *
3086 * This prevents all writes to the buffer. Any attempt to write
3087 * to the buffer after this will fail and return NULL.
3088 *
3089 * This is different than ring_buffer_record_disable() as
3090 * it works like an on/off switch, where as the disable() version
3091 * must be paired with a enable().
3092 */
ring_buffer_record_off(struct ring_buffer * buffer)3093 void ring_buffer_record_off(struct ring_buffer *buffer)
3094 {
3095 unsigned int rd;
3096 unsigned int new_rd;
3097
3098 do {
3099 rd = atomic_read(&buffer->record_disabled);
3100 new_rd = rd | RB_BUFFER_OFF;
3101 } while (atomic_cmpxchg(&buffer->record_disabled, rd, new_rd) != rd);
3102 }
3103 EXPORT_SYMBOL_GPL(ring_buffer_record_off);
3104
3105 /**
3106 * ring_buffer_record_on - restart writes into the buffer
3107 * @buffer: The ring buffer to start writes to.
3108 *
3109 * This enables all writes to the buffer that was disabled by
3110 * ring_buffer_record_off().
3111 *
3112 * This is different than ring_buffer_record_enable() as
3113 * it works like an on/off switch, where as the enable() version
3114 * must be paired with a disable().
3115 */
ring_buffer_record_on(struct ring_buffer * buffer)3116 void ring_buffer_record_on(struct ring_buffer *buffer)
3117 {
3118 unsigned int rd;
3119 unsigned int new_rd;
3120
3121 do {
3122 rd = atomic_read(&buffer->record_disabled);
3123 new_rd = rd & ~RB_BUFFER_OFF;
3124 } while (atomic_cmpxchg(&buffer->record_disabled, rd, new_rd) != rd);
3125 }
3126 EXPORT_SYMBOL_GPL(ring_buffer_record_on);
3127
3128 /**
3129 * ring_buffer_record_is_on - return true if the ring buffer can write
3130 * @buffer: The ring buffer to see if write is enabled
3131 *
3132 * Returns true if the ring buffer is in a state that it accepts writes.
3133 */
ring_buffer_record_is_on(struct ring_buffer * buffer)3134 int ring_buffer_record_is_on(struct ring_buffer *buffer)
3135 {
3136 return !atomic_read(&buffer->record_disabled);
3137 }
3138
3139 /**
3140 * ring_buffer_record_disable_cpu - stop all writes into the cpu_buffer
3141 * @buffer: The ring buffer to stop writes to.
3142 * @cpu: The CPU buffer to stop
3143 *
3144 * This prevents all writes to the buffer. Any attempt to write
3145 * to the buffer after this will fail and return NULL.
3146 *
3147 * The caller should call synchronize_sched() after this.
3148 */
ring_buffer_record_disable_cpu(struct ring_buffer * buffer,int cpu)3149 void ring_buffer_record_disable_cpu(struct ring_buffer *buffer, int cpu)
3150 {
3151 struct ring_buffer_per_cpu *cpu_buffer;
3152
3153 if (!cpumask_test_cpu(cpu, buffer->cpumask))
3154 return;
3155
3156 cpu_buffer = buffer->buffers[cpu];
3157 atomic_inc(&cpu_buffer->record_disabled);
3158 }
3159 EXPORT_SYMBOL_GPL(ring_buffer_record_disable_cpu);
3160
3161 /**
3162 * ring_buffer_record_enable_cpu - enable writes to the buffer
3163 * @buffer: The ring buffer to enable writes
3164 * @cpu: The CPU to enable.
3165 *
3166 * Note, multiple disables will need the same number of enables
3167 * to truly enable the writing (much like preempt_disable).
3168 */
ring_buffer_record_enable_cpu(struct ring_buffer * buffer,int cpu)3169 void ring_buffer_record_enable_cpu(struct ring_buffer *buffer, int cpu)
3170 {
3171 struct ring_buffer_per_cpu *cpu_buffer;
3172
3173 if (!cpumask_test_cpu(cpu, buffer->cpumask))
3174 return;
3175
3176 cpu_buffer = buffer->buffers[cpu];
3177 atomic_dec(&cpu_buffer->record_disabled);
3178 }
3179 EXPORT_SYMBOL_GPL(ring_buffer_record_enable_cpu);
3180
3181 /*
3182 * The total entries in the ring buffer is the running counter
3183 * of entries entered into the ring buffer, minus the sum of
3184 * the entries read from the ring buffer and the number of
3185 * entries that were overwritten.
3186 */
3187 static inline unsigned long
rb_num_of_entries(struct ring_buffer_per_cpu * cpu_buffer)3188 rb_num_of_entries(struct ring_buffer_per_cpu *cpu_buffer)
3189 {
3190 return local_read(&cpu_buffer->entries) -
3191 (local_read(&cpu_buffer->overrun) + cpu_buffer->read);
3192 }
3193
3194 /**
3195 * ring_buffer_oldest_event_ts - get the oldest event timestamp from the buffer
3196 * @buffer: The ring buffer
3197 * @cpu: The per CPU buffer to read from.
3198 */
ring_buffer_oldest_event_ts(struct ring_buffer * buffer,int cpu)3199 u64 ring_buffer_oldest_event_ts(struct ring_buffer *buffer, int cpu)
3200 {
3201 unsigned long flags;
3202 struct ring_buffer_per_cpu *cpu_buffer;
3203 struct buffer_page *bpage;
3204 u64 ret = 0;
3205
3206 if (!cpumask_test_cpu(cpu, buffer->cpumask))
3207 return 0;
3208
3209 cpu_buffer = buffer->buffers[cpu];
3210 raw_spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
3211 /*
3212 * if the tail is on reader_page, oldest time stamp is on the reader
3213 * page
3214 */
3215 if (cpu_buffer->tail_page == cpu_buffer->reader_page)
3216 bpage = cpu_buffer->reader_page;
3217 else
3218 bpage = rb_set_head_page(cpu_buffer);
3219 if (bpage)
3220 ret = bpage->page->time_stamp;
3221 raw_spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
3222
3223 return ret;
3224 }
3225 EXPORT_SYMBOL_GPL(ring_buffer_oldest_event_ts);
3226
3227 /**
3228 * ring_buffer_bytes_cpu - get the number of bytes consumed in a cpu buffer
3229 * @buffer: The ring buffer
3230 * @cpu: The per CPU buffer to read from.
3231 */
ring_buffer_bytes_cpu(struct ring_buffer * buffer,int cpu)3232 unsigned long ring_buffer_bytes_cpu(struct ring_buffer *buffer, int cpu)
3233 {
3234 struct ring_buffer_per_cpu *cpu_buffer;
3235 unsigned long ret;
3236
3237 if (!cpumask_test_cpu(cpu, buffer->cpumask))
3238 return 0;
3239
3240 cpu_buffer = buffer->buffers[cpu];
3241 ret = local_read(&cpu_buffer->entries_bytes) - cpu_buffer->read_bytes;
3242
3243 return ret;
3244 }
3245 EXPORT_SYMBOL_GPL(ring_buffer_bytes_cpu);
3246
3247 /**
3248 * ring_buffer_entries_cpu - get the number of entries in a cpu buffer
3249 * @buffer: The ring buffer
3250 * @cpu: The per CPU buffer to get the entries from.
3251 */
ring_buffer_entries_cpu(struct ring_buffer * buffer,int cpu)3252 unsigned long ring_buffer_entries_cpu(struct ring_buffer *buffer, int cpu)
3253 {
3254 struct ring_buffer_per_cpu *cpu_buffer;
3255
3256 if (!cpumask_test_cpu(cpu, buffer->cpumask))
3257 return 0;
3258
3259 cpu_buffer = buffer->buffers[cpu];
3260
3261 return rb_num_of_entries(cpu_buffer);
3262 }
3263 EXPORT_SYMBOL_GPL(ring_buffer_entries_cpu);
3264
3265 /**
3266 * ring_buffer_overrun_cpu - get the number of overruns caused by the ring
3267 * buffer wrapping around (only if RB_FL_OVERWRITE is on).
3268 * @buffer: The ring buffer
3269 * @cpu: The per CPU buffer to get the number of overruns from
3270 */
ring_buffer_overrun_cpu(struct ring_buffer * buffer,int cpu)3271 unsigned long ring_buffer_overrun_cpu(struct ring_buffer *buffer, int cpu)
3272 {
3273 struct ring_buffer_per_cpu *cpu_buffer;
3274 unsigned long ret;
3275
3276 if (!cpumask_test_cpu(cpu, buffer->cpumask))
3277 return 0;
3278
3279 cpu_buffer = buffer->buffers[cpu];
3280 ret = local_read(&cpu_buffer->overrun);
3281
3282 return ret;
3283 }
3284 EXPORT_SYMBOL_GPL(ring_buffer_overrun_cpu);
3285
3286 /**
3287 * ring_buffer_commit_overrun_cpu - get the number of overruns caused by
3288 * commits failing due to the buffer wrapping around while there are uncommitted
3289 * events, such as during an interrupt storm.
3290 * @buffer: The ring buffer
3291 * @cpu: The per CPU buffer to get the number of overruns from
3292 */
3293 unsigned long
ring_buffer_commit_overrun_cpu(struct ring_buffer * buffer,int cpu)3294 ring_buffer_commit_overrun_cpu(struct ring_buffer *buffer, int cpu)
3295 {
3296 struct ring_buffer_per_cpu *cpu_buffer;
3297 unsigned long ret;
3298
3299 if (!cpumask_test_cpu(cpu, buffer->cpumask))
3300 return 0;
3301
3302 cpu_buffer = buffer->buffers[cpu];
3303 ret = local_read(&cpu_buffer->commit_overrun);
3304
3305 return ret;
3306 }
3307 EXPORT_SYMBOL_GPL(ring_buffer_commit_overrun_cpu);
3308
3309 /**
3310 * ring_buffer_dropped_events_cpu - get the number of dropped events caused by
3311 * the ring buffer filling up (only if RB_FL_OVERWRITE is off).
3312 * @buffer: The ring buffer
3313 * @cpu: The per CPU buffer to get the number of overruns from
3314 */
3315 unsigned long
ring_buffer_dropped_events_cpu(struct ring_buffer * buffer,int cpu)3316 ring_buffer_dropped_events_cpu(struct ring_buffer *buffer, int cpu)
3317 {
3318 struct ring_buffer_per_cpu *cpu_buffer;
3319 unsigned long ret;
3320
3321 if (!cpumask_test_cpu(cpu, buffer->cpumask))
3322 return 0;
3323
3324 cpu_buffer = buffer->buffers[cpu];
3325 ret = local_read(&cpu_buffer->dropped_events);
3326
3327 return ret;
3328 }
3329 EXPORT_SYMBOL_GPL(ring_buffer_dropped_events_cpu);
3330
3331 /**
3332 * ring_buffer_read_events_cpu - get the number of events successfully read
3333 * @buffer: The ring buffer
3334 * @cpu: The per CPU buffer to get the number of events read
3335 */
3336 unsigned long
ring_buffer_read_events_cpu(struct ring_buffer * buffer,int cpu)3337 ring_buffer_read_events_cpu(struct ring_buffer *buffer, int cpu)
3338 {
3339 struct ring_buffer_per_cpu *cpu_buffer;
3340
3341 if (!cpumask_test_cpu(cpu, buffer->cpumask))
3342 return 0;
3343
3344 cpu_buffer = buffer->buffers[cpu];
3345 return cpu_buffer->read;
3346 }
3347 EXPORT_SYMBOL_GPL(ring_buffer_read_events_cpu);
3348
3349 /**
3350 * ring_buffer_entries - get the number of entries in a buffer
3351 * @buffer: The ring buffer
3352 *
3353 * Returns the total number of entries in the ring buffer
3354 * (all CPU entries)
3355 */
ring_buffer_entries(struct ring_buffer * buffer)3356 unsigned long ring_buffer_entries(struct ring_buffer *buffer)
3357 {
3358 struct ring_buffer_per_cpu *cpu_buffer;
3359 unsigned long entries = 0;
3360 int cpu;
3361
3362 /* if you care about this being correct, lock the buffer */
3363 for_each_buffer_cpu(buffer, cpu) {
3364 cpu_buffer = buffer->buffers[cpu];
3365 entries += rb_num_of_entries(cpu_buffer);
3366 }
3367
3368 return entries;
3369 }
3370 EXPORT_SYMBOL_GPL(ring_buffer_entries);
3371
3372 /**
3373 * ring_buffer_overruns - get the number of overruns in buffer
3374 * @buffer: The ring buffer
3375 *
3376 * Returns the total number of overruns in the ring buffer
3377 * (all CPU entries)
3378 */
ring_buffer_overruns(struct ring_buffer * buffer)3379 unsigned long ring_buffer_overruns(struct ring_buffer *buffer)
3380 {
3381 struct ring_buffer_per_cpu *cpu_buffer;
3382 unsigned long overruns = 0;
3383 int cpu;
3384
3385 /* if you care about this being correct, lock the buffer */
3386 for_each_buffer_cpu(buffer, cpu) {
3387 cpu_buffer = buffer->buffers[cpu];
3388 overruns += local_read(&cpu_buffer->overrun);
3389 }
3390
3391 return overruns;
3392 }
3393 EXPORT_SYMBOL_GPL(ring_buffer_overruns);
3394
rb_iter_reset(struct ring_buffer_iter * iter)3395 static void rb_iter_reset(struct ring_buffer_iter *iter)
3396 {
3397 struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
3398
3399 /* Iterator usage is expected to have record disabled */
3400 iter->head_page = cpu_buffer->reader_page;
3401 iter->head = cpu_buffer->reader_page->read;
3402
3403 iter->cache_reader_page = iter->head_page;
3404 iter->cache_read = cpu_buffer->read;
3405
3406 if (iter->head)
3407 iter->read_stamp = cpu_buffer->read_stamp;
3408 else
3409 iter->read_stamp = iter->head_page->page->time_stamp;
3410 }
3411
3412 /**
3413 * ring_buffer_iter_reset - reset an iterator
3414 * @iter: The iterator to reset
3415 *
3416 * Resets the iterator, so that it will start from the beginning
3417 * again.
3418 */
ring_buffer_iter_reset(struct ring_buffer_iter * iter)3419 void ring_buffer_iter_reset(struct ring_buffer_iter *iter)
3420 {
3421 struct ring_buffer_per_cpu *cpu_buffer;
3422 unsigned long flags;
3423
3424 if (!iter)
3425 return;
3426
3427 cpu_buffer = iter->cpu_buffer;
3428
3429 raw_spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
3430 rb_iter_reset(iter);
3431 raw_spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
3432 }
3433 EXPORT_SYMBOL_GPL(ring_buffer_iter_reset);
3434
3435 /**
3436 * ring_buffer_iter_empty - check if an iterator has no more to read
3437 * @iter: The iterator to check
3438 */
ring_buffer_iter_empty(struct ring_buffer_iter * iter)3439 int ring_buffer_iter_empty(struct ring_buffer_iter *iter)
3440 {
3441 struct ring_buffer_per_cpu *cpu_buffer;
3442 struct buffer_page *reader;
3443 struct buffer_page *head_page;
3444 struct buffer_page *commit_page;
3445 unsigned commit;
3446
3447 cpu_buffer = iter->cpu_buffer;
3448
3449 /* Remember, trace recording is off when iterator is in use */
3450 reader = cpu_buffer->reader_page;
3451 head_page = cpu_buffer->head_page;
3452 commit_page = cpu_buffer->commit_page;
3453 commit = rb_page_commit(commit_page);
3454
3455 return ((iter->head_page == commit_page && iter->head == commit) ||
3456 (iter->head_page == reader && commit_page == head_page &&
3457 head_page->read == commit &&
3458 iter->head == rb_page_commit(cpu_buffer->reader_page)));
3459 }
3460 EXPORT_SYMBOL_GPL(ring_buffer_iter_empty);
3461
3462 static void
rb_update_read_stamp(struct ring_buffer_per_cpu * cpu_buffer,struct ring_buffer_event * event)3463 rb_update_read_stamp(struct ring_buffer_per_cpu *cpu_buffer,
3464 struct ring_buffer_event *event)
3465 {
3466 u64 delta;
3467
3468 switch (event->type_len) {
3469 case RINGBUF_TYPE_PADDING:
3470 return;
3471
3472 case RINGBUF_TYPE_TIME_EXTEND:
3473 delta = event->array[0];
3474 delta <<= TS_SHIFT;
3475 delta += event->time_delta;
3476 cpu_buffer->read_stamp += delta;
3477 return;
3478
3479 case RINGBUF_TYPE_TIME_STAMP:
3480 /* FIXME: not implemented */
3481 return;
3482
3483 case RINGBUF_TYPE_DATA:
3484 cpu_buffer->read_stamp += event->time_delta;
3485 return;
3486
3487 default:
3488 BUG();
3489 }
3490 return;
3491 }
3492
3493 static void
rb_update_iter_read_stamp(struct ring_buffer_iter * iter,struct ring_buffer_event * event)3494 rb_update_iter_read_stamp(struct ring_buffer_iter *iter,
3495 struct ring_buffer_event *event)
3496 {
3497 u64 delta;
3498
3499 switch (event->type_len) {
3500 case RINGBUF_TYPE_PADDING:
3501 return;
3502
3503 case RINGBUF_TYPE_TIME_EXTEND:
3504 delta = event->array[0];
3505 delta <<= TS_SHIFT;
3506 delta += event->time_delta;
3507 iter->read_stamp += delta;
3508 return;
3509
3510 case RINGBUF_TYPE_TIME_STAMP:
3511 /* FIXME: not implemented */
3512 return;
3513
3514 case RINGBUF_TYPE_DATA:
3515 iter->read_stamp += event->time_delta;
3516 return;
3517
3518 default:
3519 BUG();
3520 }
3521 return;
3522 }
3523
3524 static struct buffer_page *
rb_get_reader_page(struct ring_buffer_per_cpu * cpu_buffer)3525 rb_get_reader_page(struct ring_buffer_per_cpu *cpu_buffer)
3526 {
3527 struct buffer_page *reader = NULL;
3528 unsigned long overwrite;
3529 unsigned long flags;
3530 int nr_loops = 0;
3531 int ret;
3532
3533 local_irq_save(flags);
3534 arch_spin_lock(&cpu_buffer->lock);
3535
3536 again:
3537 /*
3538 * This should normally only loop twice. But because the
3539 * start of the reader inserts an empty page, it causes
3540 * a case where we will loop three times. There should be no
3541 * reason to loop four times (that I know of).
3542 */
3543 if (RB_WARN_ON(cpu_buffer, ++nr_loops > 3)) {
3544 reader = NULL;
3545 goto out;
3546 }
3547
3548 reader = cpu_buffer->reader_page;
3549
3550 /* If there's more to read, return this page */
3551 if (cpu_buffer->reader_page->read < rb_page_size(reader))
3552 goto out;
3553
3554 /* Never should we have an index greater than the size */
3555 if (RB_WARN_ON(cpu_buffer,
3556 cpu_buffer->reader_page->read > rb_page_size(reader)))
3557 goto out;
3558
3559 /* check if we caught up to the tail */
3560 reader = NULL;
3561 if (cpu_buffer->commit_page == cpu_buffer->reader_page)
3562 goto out;
3563
3564 /* Don't bother swapping if the ring buffer is empty */
3565 if (rb_num_of_entries(cpu_buffer) == 0)
3566 goto out;
3567
3568 /*
3569 * Reset the reader page to size zero.
3570 */
3571 local_set(&cpu_buffer->reader_page->write, 0);
3572 local_set(&cpu_buffer->reader_page->entries, 0);
3573 local_set(&cpu_buffer->reader_page->page->commit, 0);
3574 cpu_buffer->reader_page->real_end = 0;
3575
3576 spin:
3577 /*
3578 * Splice the empty reader page into the list around the head.
3579 */
3580 reader = rb_set_head_page(cpu_buffer);
3581 if (!reader)
3582 goto out;
3583 cpu_buffer->reader_page->list.next = rb_list_head(reader->list.next);
3584 cpu_buffer->reader_page->list.prev = reader->list.prev;
3585
3586 /*
3587 * cpu_buffer->pages just needs to point to the buffer, it
3588 * has no specific buffer page to point to. Lets move it out
3589 * of our way so we don't accidentally swap it.
3590 */
3591 cpu_buffer->pages = reader->list.prev;
3592
3593 /* The reader page will be pointing to the new head */
3594 rb_set_list_to_head(cpu_buffer, &cpu_buffer->reader_page->list);
3595
3596 /*
3597 * We want to make sure we read the overruns after we set up our
3598 * pointers to the next object. The writer side does a
3599 * cmpxchg to cross pages which acts as the mb on the writer
3600 * side. Note, the reader will constantly fail the swap
3601 * while the writer is updating the pointers, so this
3602 * guarantees that the overwrite recorded here is the one we
3603 * want to compare with the last_overrun.
3604 */
3605 smp_mb();
3606 overwrite = local_read(&(cpu_buffer->overrun));
3607
3608 /*
3609 * Here's the tricky part.
3610 *
3611 * We need to move the pointer past the header page.
3612 * But we can only do that if a writer is not currently
3613 * moving it. The page before the header page has the
3614 * flag bit '1' set if it is pointing to the page we want.
3615 * but if the writer is in the process of moving it
3616 * than it will be '2' or already moved '0'.
3617 */
3618
3619 ret = rb_head_page_replace(reader, cpu_buffer->reader_page);
3620
3621 /*
3622 * If we did not convert it, then we must try again.
3623 */
3624 if (!ret)
3625 goto spin;
3626
3627 /*
3628 * Yeah! We succeeded in replacing the page.
3629 *
3630 * Now make the new head point back to the reader page.
3631 */
3632 rb_list_head(reader->list.next)->prev = &cpu_buffer->reader_page->list;
3633 rb_inc_page(cpu_buffer, &cpu_buffer->head_page);
3634
3635 /* Finally update the reader page to the new head */
3636 cpu_buffer->reader_page = reader;
3637 cpu_buffer->reader_page->read = 0;
3638
3639 if (overwrite != cpu_buffer->last_overrun) {
3640 cpu_buffer->lost_events = overwrite - cpu_buffer->last_overrun;
3641 cpu_buffer->last_overrun = overwrite;
3642 }
3643
3644 goto again;
3645
3646 out:
3647 /* Update the read_stamp on the first event */
3648 if (reader && reader->read == 0)
3649 cpu_buffer->read_stamp = reader->page->time_stamp;
3650
3651 arch_spin_unlock(&cpu_buffer->lock);
3652 local_irq_restore(flags);
3653
3654 return reader;
3655 }
3656
rb_advance_reader(struct ring_buffer_per_cpu * cpu_buffer)3657 static void rb_advance_reader(struct ring_buffer_per_cpu *cpu_buffer)
3658 {
3659 struct ring_buffer_event *event;
3660 struct buffer_page *reader;
3661 unsigned length;
3662
3663 reader = rb_get_reader_page(cpu_buffer);
3664
3665 /* This function should not be called when buffer is empty */
3666 if (RB_WARN_ON(cpu_buffer, !reader))
3667 return;
3668
3669 event = rb_reader_event(cpu_buffer);
3670
3671 if (event->type_len <= RINGBUF_TYPE_DATA_TYPE_LEN_MAX)
3672 cpu_buffer->read++;
3673
3674 rb_update_read_stamp(cpu_buffer, event);
3675
3676 length = rb_event_length(event);
3677 cpu_buffer->reader_page->read += length;
3678 }
3679
rb_advance_iter(struct ring_buffer_iter * iter)3680 static void rb_advance_iter(struct ring_buffer_iter *iter)
3681 {
3682 struct ring_buffer_per_cpu *cpu_buffer;
3683 struct ring_buffer_event *event;
3684 unsigned length;
3685
3686 cpu_buffer = iter->cpu_buffer;
3687
3688 /*
3689 * Check if we are at the end of the buffer.
3690 */
3691 if (iter->head >= rb_page_size(iter->head_page)) {
3692 /* discarded commits can make the page empty */
3693 if (iter->head_page == cpu_buffer->commit_page)
3694 return;
3695 rb_inc_iter(iter);
3696 return;
3697 }
3698
3699 event = rb_iter_head_event(iter);
3700
3701 length = rb_event_length(event);
3702
3703 /*
3704 * This should not be called to advance the header if we are
3705 * at the tail of the buffer.
3706 */
3707 if (RB_WARN_ON(cpu_buffer,
3708 (iter->head_page == cpu_buffer->commit_page) &&
3709 (iter->head + length > rb_commit_index(cpu_buffer))))
3710 return;
3711
3712 rb_update_iter_read_stamp(iter, event);
3713
3714 iter->head += length;
3715
3716 /* check for end of page padding */
3717 if ((iter->head >= rb_page_size(iter->head_page)) &&
3718 (iter->head_page != cpu_buffer->commit_page))
3719 rb_inc_iter(iter);
3720 }
3721
rb_lost_events(struct ring_buffer_per_cpu * cpu_buffer)3722 static int rb_lost_events(struct ring_buffer_per_cpu *cpu_buffer)
3723 {
3724 return cpu_buffer->lost_events;
3725 }
3726
3727 static struct ring_buffer_event *
rb_buffer_peek(struct ring_buffer_per_cpu * cpu_buffer,u64 * ts,unsigned long * lost_events)3728 rb_buffer_peek(struct ring_buffer_per_cpu *cpu_buffer, u64 *ts,
3729 unsigned long *lost_events)
3730 {
3731 struct ring_buffer_event *event;
3732 struct buffer_page *reader;
3733 int nr_loops = 0;
3734
3735 again:
3736 /*
3737 * We repeat when a time extend is encountered.
3738 * Since the time extend is always attached to a data event,
3739 * we should never loop more than once.
3740 * (We never hit the following condition more than twice).
3741 */
3742 if (RB_WARN_ON(cpu_buffer, ++nr_loops > 2))
3743 return NULL;
3744
3745 reader = rb_get_reader_page(cpu_buffer);
3746 if (!reader)
3747 return NULL;
3748
3749 event = rb_reader_event(cpu_buffer);
3750
3751 switch (event->type_len) {
3752 case RINGBUF_TYPE_PADDING:
3753 if (rb_null_event(event))
3754 RB_WARN_ON(cpu_buffer, 1);
3755 /*
3756 * Because the writer could be discarding every
3757 * event it creates (which would probably be bad)
3758 * if we were to go back to "again" then we may never
3759 * catch up, and will trigger the warn on, or lock
3760 * the box. Return the padding, and we will release
3761 * the current locks, and try again.
3762 */
3763 return event;
3764
3765 case RINGBUF_TYPE_TIME_EXTEND:
3766 /* Internal data, OK to advance */
3767 rb_advance_reader(cpu_buffer);
3768 goto again;
3769
3770 case RINGBUF_TYPE_TIME_STAMP:
3771 /* FIXME: not implemented */
3772 rb_advance_reader(cpu_buffer);
3773 goto again;
3774
3775 case RINGBUF_TYPE_DATA:
3776 if (ts) {
3777 *ts = cpu_buffer->read_stamp + event->time_delta;
3778 ring_buffer_normalize_time_stamp(cpu_buffer->buffer,
3779 cpu_buffer->cpu, ts);
3780 }
3781 if (lost_events)
3782 *lost_events = rb_lost_events(cpu_buffer);
3783 return event;
3784
3785 default:
3786 BUG();
3787 }
3788
3789 return NULL;
3790 }
3791 EXPORT_SYMBOL_GPL(ring_buffer_peek);
3792
3793 static struct ring_buffer_event *
rb_iter_peek(struct ring_buffer_iter * iter,u64 * ts)3794 rb_iter_peek(struct ring_buffer_iter *iter, u64 *ts)
3795 {
3796 struct ring_buffer *buffer;
3797 struct ring_buffer_per_cpu *cpu_buffer;
3798 struct ring_buffer_event *event;
3799 int nr_loops = 0;
3800
3801 cpu_buffer = iter->cpu_buffer;
3802 buffer = cpu_buffer->buffer;
3803
3804 /*
3805 * Check if someone performed a consuming read to
3806 * the buffer. A consuming read invalidates the iterator
3807 * and we need to reset the iterator in this case.
3808 */
3809 if (unlikely(iter->cache_read != cpu_buffer->read ||
3810 iter->cache_reader_page != cpu_buffer->reader_page))
3811 rb_iter_reset(iter);
3812
3813 again:
3814 if (ring_buffer_iter_empty(iter))
3815 return NULL;
3816
3817 /*
3818 * We repeat when a time extend is encountered or we hit
3819 * the end of the page. Since the time extend is always attached
3820 * to a data event, we should never loop more than three times.
3821 * Once for going to next page, once on time extend, and
3822 * finally once to get the event.
3823 * (We never hit the following condition more than thrice).
3824 */
3825 if (RB_WARN_ON(cpu_buffer, ++nr_loops > 3))
3826 return NULL;
3827
3828 if (rb_per_cpu_empty(cpu_buffer))
3829 return NULL;
3830
3831 if (iter->head >= rb_page_size(iter->head_page)) {
3832 rb_inc_iter(iter);
3833 goto again;
3834 }
3835
3836 event = rb_iter_head_event(iter);
3837
3838 switch (event->type_len) {
3839 case RINGBUF_TYPE_PADDING:
3840 if (rb_null_event(event)) {
3841 rb_inc_iter(iter);
3842 goto again;
3843 }
3844 rb_advance_iter(iter);
3845 return event;
3846
3847 case RINGBUF_TYPE_TIME_EXTEND:
3848 /* Internal data, OK to advance */
3849 rb_advance_iter(iter);
3850 goto again;
3851
3852 case RINGBUF_TYPE_TIME_STAMP:
3853 /* FIXME: not implemented */
3854 rb_advance_iter(iter);
3855 goto again;
3856
3857 case RINGBUF_TYPE_DATA:
3858 if (ts) {
3859 *ts = iter->read_stamp + event->time_delta;
3860 ring_buffer_normalize_time_stamp(buffer,
3861 cpu_buffer->cpu, ts);
3862 }
3863 return event;
3864
3865 default:
3866 BUG();
3867 }
3868
3869 return NULL;
3870 }
3871 EXPORT_SYMBOL_GPL(ring_buffer_iter_peek);
3872
rb_reader_lock(struct ring_buffer_per_cpu * cpu_buffer)3873 static inline bool rb_reader_lock(struct ring_buffer_per_cpu *cpu_buffer)
3874 {
3875 if (likely(!in_nmi())) {
3876 raw_spin_lock(&cpu_buffer->reader_lock);
3877 return true;
3878 }
3879
3880 /*
3881 * If an NMI die dumps out the content of the ring buffer
3882 * trylock must be used to prevent a deadlock if the NMI
3883 * preempted a task that holds the ring buffer locks. If
3884 * we get the lock then all is fine, if not, then continue
3885 * to do the read, but this can corrupt the ring buffer,
3886 * so it must be permanently disabled from future writes.
3887 * Reading from NMI is a oneshot deal.
3888 */
3889 if (raw_spin_trylock(&cpu_buffer->reader_lock))
3890 return true;
3891
3892 /* Continue without locking, but disable the ring buffer */
3893 atomic_inc(&cpu_buffer->record_disabled);
3894 return false;
3895 }
3896
3897 static inline void
rb_reader_unlock(struct ring_buffer_per_cpu * cpu_buffer,bool locked)3898 rb_reader_unlock(struct ring_buffer_per_cpu *cpu_buffer, bool locked)
3899 {
3900 if (likely(locked))
3901 raw_spin_unlock(&cpu_buffer->reader_lock);
3902 return;
3903 }
3904
3905 /**
3906 * ring_buffer_peek - peek at the next event to be read
3907 * @buffer: The ring buffer to read
3908 * @cpu: The cpu to peak at
3909 * @ts: The timestamp counter of this event.
3910 * @lost_events: a variable to store if events were lost (may be NULL)
3911 *
3912 * This will return the event that will be read next, but does
3913 * not consume the data.
3914 */
3915 struct ring_buffer_event *
ring_buffer_peek(struct ring_buffer * buffer,int cpu,u64 * ts,unsigned long * lost_events)3916 ring_buffer_peek(struct ring_buffer *buffer, int cpu, u64 *ts,
3917 unsigned long *lost_events)
3918 {
3919 struct ring_buffer_per_cpu *cpu_buffer = buffer->buffers[cpu];
3920 struct ring_buffer_event *event;
3921 unsigned long flags;
3922 bool dolock;
3923
3924 if (!cpumask_test_cpu(cpu, buffer->cpumask))
3925 return NULL;
3926
3927 again:
3928 local_irq_save(flags);
3929 dolock = rb_reader_lock(cpu_buffer);
3930 event = rb_buffer_peek(cpu_buffer, ts, lost_events);
3931 if (event && event->type_len == RINGBUF_TYPE_PADDING)
3932 rb_advance_reader(cpu_buffer);
3933 rb_reader_unlock(cpu_buffer, dolock);
3934 local_irq_restore(flags);
3935
3936 if (event && event->type_len == RINGBUF_TYPE_PADDING)
3937 goto again;
3938
3939 return event;
3940 }
3941
3942 /**
3943 * ring_buffer_iter_peek - peek at the next event to be read
3944 * @iter: The ring buffer iterator
3945 * @ts: The timestamp counter of this event.
3946 *
3947 * This will return the event that will be read next, but does
3948 * not increment the iterator.
3949 */
3950 struct ring_buffer_event *
ring_buffer_iter_peek(struct ring_buffer_iter * iter,u64 * ts)3951 ring_buffer_iter_peek(struct ring_buffer_iter *iter, u64 *ts)
3952 {
3953 struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
3954 struct ring_buffer_event *event;
3955 unsigned long flags;
3956
3957 again:
3958 raw_spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
3959 event = rb_iter_peek(iter, ts);
3960 raw_spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
3961
3962 if (event && event->type_len == RINGBUF_TYPE_PADDING)
3963 goto again;
3964
3965 return event;
3966 }
3967
3968 /**
3969 * ring_buffer_consume - return an event and consume it
3970 * @buffer: The ring buffer to get the next event from
3971 * @cpu: the cpu to read the buffer from
3972 * @ts: a variable to store the timestamp (may be NULL)
3973 * @lost_events: a variable to store if events were lost (may be NULL)
3974 *
3975 * Returns the next event in the ring buffer, and that event is consumed.
3976 * Meaning, that sequential reads will keep returning a different event,
3977 * and eventually empty the ring buffer if the producer is slower.
3978 */
3979 struct ring_buffer_event *
ring_buffer_consume(struct ring_buffer * buffer,int cpu,u64 * ts,unsigned long * lost_events)3980 ring_buffer_consume(struct ring_buffer *buffer, int cpu, u64 *ts,
3981 unsigned long *lost_events)
3982 {
3983 struct ring_buffer_per_cpu *cpu_buffer;
3984 struct ring_buffer_event *event = NULL;
3985 unsigned long flags;
3986 bool dolock;
3987
3988 again:
3989 /* might be called in atomic */
3990 preempt_disable();
3991
3992 if (!cpumask_test_cpu(cpu, buffer->cpumask))
3993 goto out;
3994
3995 cpu_buffer = buffer->buffers[cpu];
3996 local_irq_save(flags);
3997 dolock = rb_reader_lock(cpu_buffer);
3998
3999 event = rb_buffer_peek(cpu_buffer, ts, lost_events);
4000 if (event) {
4001 cpu_buffer->lost_events = 0;
4002 rb_advance_reader(cpu_buffer);
4003 }
4004
4005 rb_reader_unlock(cpu_buffer, dolock);
4006 local_irq_restore(flags);
4007
4008 out:
4009 preempt_enable();
4010
4011 if (event && event->type_len == RINGBUF_TYPE_PADDING)
4012 goto again;
4013
4014 return event;
4015 }
4016 EXPORT_SYMBOL_GPL(ring_buffer_consume);
4017
4018 /**
4019 * ring_buffer_read_prepare - Prepare for a non consuming read of the buffer
4020 * @buffer: The ring buffer to read from
4021 * @cpu: The cpu buffer to iterate over
4022 *
4023 * This performs the initial preparations necessary to iterate
4024 * through the buffer. Memory is allocated, buffer recording
4025 * is disabled, and the iterator pointer is returned to the caller.
4026 *
4027 * Disabling buffer recordng prevents the reading from being
4028 * corrupted. This is not a consuming read, so a producer is not
4029 * expected.
4030 *
4031 * After a sequence of ring_buffer_read_prepare calls, the user is
4032 * expected to make at least one call to ring_buffer_read_prepare_sync.
4033 * Afterwards, ring_buffer_read_start is invoked to get things going
4034 * for real.
4035 *
4036 * This overall must be paired with ring_buffer_read_finish.
4037 */
4038 struct ring_buffer_iter *
ring_buffer_read_prepare(struct ring_buffer * buffer,int cpu)4039 ring_buffer_read_prepare(struct ring_buffer *buffer, int cpu)
4040 {
4041 struct ring_buffer_per_cpu *cpu_buffer;
4042 struct ring_buffer_iter *iter;
4043
4044 if (!cpumask_test_cpu(cpu, buffer->cpumask))
4045 return NULL;
4046
4047 iter = kmalloc(sizeof(*iter), GFP_KERNEL);
4048 if (!iter)
4049 return NULL;
4050
4051 cpu_buffer = buffer->buffers[cpu];
4052
4053 iter->cpu_buffer = cpu_buffer;
4054
4055 atomic_inc(&buffer->resize_disabled);
4056 atomic_inc(&cpu_buffer->record_disabled);
4057
4058 return iter;
4059 }
4060 EXPORT_SYMBOL_GPL(ring_buffer_read_prepare);
4061
4062 /**
4063 * ring_buffer_read_prepare_sync - Synchronize a set of prepare calls
4064 *
4065 * All previously invoked ring_buffer_read_prepare calls to prepare
4066 * iterators will be synchronized. Afterwards, read_buffer_read_start
4067 * calls on those iterators are allowed.
4068 */
4069 void
ring_buffer_read_prepare_sync(void)4070 ring_buffer_read_prepare_sync(void)
4071 {
4072 synchronize_sched();
4073 }
4074 EXPORT_SYMBOL_GPL(ring_buffer_read_prepare_sync);
4075
4076 /**
4077 * ring_buffer_read_start - start a non consuming read of the buffer
4078 * @iter: The iterator returned by ring_buffer_read_prepare
4079 *
4080 * This finalizes the startup of an iteration through the buffer.
4081 * The iterator comes from a call to ring_buffer_read_prepare and
4082 * an intervening ring_buffer_read_prepare_sync must have been
4083 * performed.
4084 *
4085 * Must be paired with ring_buffer_read_finish.
4086 */
4087 void
ring_buffer_read_start(struct ring_buffer_iter * iter)4088 ring_buffer_read_start(struct ring_buffer_iter *iter)
4089 {
4090 struct ring_buffer_per_cpu *cpu_buffer;
4091 unsigned long flags;
4092
4093 if (!iter)
4094 return;
4095
4096 cpu_buffer = iter->cpu_buffer;
4097
4098 raw_spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
4099 arch_spin_lock(&cpu_buffer->lock);
4100 rb_iter_reset(iter);
4101 arch_spin_unlock(&cpu_buffer->lock);
4102 raw_spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
4103 }
4104 EXPORT_SYMBOL_GPL(ring_buffer_read_start);
4105
4106 /**
4107 * ring_buffer_read_finish - finish reading the iterator of the buffer
4108 * @iter: The iterator retrieved by ring_buffer_start
4109 *
4110 * This re-enables the recording to the buffer, and frees the
4111 * iterator.
4112 */
4113 void
ring_buffer_read_finish(struct ring_buffer_iter * iter)4114 ring_buffer_read_finish(struct ring_buffer_iter *iter)
4115 {
4116 struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
4117 unsigned long flags;
4118
4119 /*
4120 * Ring buffer is disabled from recording, here's a good place
4121 * to check the integrity of the ring buffer.
4122 * Must prevent readers from trying to read, as the check
4123 * clears the HEAD page and readers require it.
4124 */
4125 raw_spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
4126 rb_check_pages(cpu_buffer);
4127 raw_spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
4128
4129 atomic_dec(&cpu_buffer->record_disabled);
4130 atomic_dec(&cpu_buffer->buffer->resize_disabled);
4131 kfree(iter);
4132 }
4133 EXPORT_SYMBOL_GPL(ring_buffer_read_finish);
4134
4135 /**
4136 * ring_buffer_read - read the next item in the ring buffer by the iterator
4137 * @iter: The ring buffer iterator
4138 * @ts: The time stamp of the event read.
4139 *
4140 * This reads the next event in the ring buffer and increments the iterator.
4141 */
4142 struct ring_buffer_event *
ring_buffer_read(struct ring_buffer_iter * iter,u64 * ts)4143 ring_buffer_read(struct ring_buffer_iter *iter, u64 *ts)
4144 {
4145 struct ring_buffer_event *event;
4146 struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
4147 unsigned long flags;
4148
4149 raw_spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
4150 again:
4151 event = rb_iter_peek(iter, ts);
4152 if (!event)
4153 goto out;
4154
4155 if (event->type_len == RINGBUF_TYPE_PADDING)
4156 goto again;
4157
4158 rb_advance_iter(iter);
4159 out:
4160 raw_spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
4161
4162 return event;
4163 }
4164 EXPORT_SYMBOL_GPL(ring_buffer_read);
4165
4166 /**
4167 * ring_buffer_size - return the size of the ring buffer (in bytes)
4168 * @buffer: The ring buffer.
4169 */
ring_buffer_size(struct ring_buffer * buffer,int cpu)4170 unsigned long ring_buffer_size(struct ring_buffer *buffer, int cpu)
4171 {
4172 /*
4173 * Earlier, this method returned
4174 * BUF_PAGE_SIZE * buffer->nr_pages
4175 * Since the nr_pages field is now removed, we have converted this to
4176 * return the per cpu buffer value.
4177 */
4178 if (!cpumask_test_cpu(cpu, buffer->cpumask))
4179 return 0;
4180
4181 return BUF_PAGE_SIZE * buffer->buffers[cpu]->nr_pages;
4182 }
4183 EXPORT_SYMBOL_GPL(ring_buffer_size);
4184
4185 static void
rb_reset_cpu(struct ring_buffer_per_cpu * cpu_buffer)4186 rb_reset_cpu(struct ring_buffer_per_cpu *cpu_buffer)
4187 {
4188 rb_head_page_deactivate(cpu_buffer);
4189
4190 cpu_buffer->head_page
4191 = list_entry(cpu_buffer->pages, struct buffer_page, list);
4192 local_set(&cpu_buffer->head_page->write, 0);
4193 local_set(&cpu_buffer->head_page->entries, 0);
4194 local_set(&cpu_buffer->head_page->page->commit, 0);
4195
4196 cpu_buffer->head_page->read = 0;
4197
4198 cpu_buffer->tail_page = cpu_buffer->head_page;
4199 cpu_buffer->commit_page = cpu_buffer->head_page;
4200
4201 INIT_LIST_HEAD(&cpu_buffer->reader_page->list);
4202 INIT_LIST_HEAD(&cpu_buffer->new_pages);
4203 local_set(&cpu_buffer->reader_page->write, 0);
4204 local_set(&cpu_buffer->reader_page->entries, 0);
4205 local_set(&cpu_buffer->reader_page->page->commit, 0);
4206 cpu_buffer->reader_page->read = 0;
4207
4208 local_set(&cpu_buffer->entries_bytes, 0);
4209 local_set(&cpu_buffer->overrun, 0);
4210 local_set(&cpu_buffer->commit_overrun, 0);
4211 local_set(&cpu_buffer->dropped_events, 0);
4212 local_set(&cpu_buffer->entries, 0);
4213 local_set(&cpu_buffer->committing, 0);
4214 local_set(&cpu_buffer->commits, 0);
4215 cpu_buffer->read = 0;
4216 cpu_buffer->read_bytes = 0;
4217
4218 cpu_buffer->write_stamp = 0;
4219 cpu_buffer->read_stamp = 0;
4220
4221 cpu_buffer->lost_events = 0;
4222 cpu_buffer->last_overrun = 0;
4223
4224 rb_head_page_activate(cpu_buffer);
4225 }
4226
4227 /**
4228 * ring_buffer_reset_cpu - reset a ring buffer per CPU buffer
4229 * @buffer: The ring buffer to reset a per cpu buffer of
4230 * @cpu: The CPU buffer to be reset
4231 */
ring_buffer_reset_cpu(struct ring_buffer * buffer,int cpu)4232 void ring_buffer_reset_cpu(struct ring_buffer *buffer, int cpu)
4233 {
4234 struct ring_buffer_per_cpu *cpu_buffer = buffer->buffers[cpu];
4235 unsigned long flags;
4236
4237 if (!cpumask_test_cpu(cpu, buffer->cpumask))
4238 return;
4239
4240 atomic_inc(&buffer->resize_disabled);
4241 atomic_inc(&cpu_buffer->record_disabled);
4242
4243 /* Make sure all commits have finished */
4244 synchronize_sched();
4245
4246 raw_spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
4247
4248 if (RB_WARN_ON(cpu_buffer, local_read(&cpu_buffer->committing)))
4249 goto out;
4250
4251 arch_spin_lock(&cpu_buffer->lock);
4252
4253 rb_reset_cpu(cpu_buffer);
4254
4255 arch_spin_unlock(&cpu_buffer->lock);
4256
4257 out:
4258 raw_spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
4259
4260 atomic_dec(&cpu_buffer->record_disabled);
4261 atomic_dec(&buffer->resize_disabled);
4262 }
4263 EXPORT_SYMBOL_GPL(ring_buffer_reset_cpu);
4264
4265 /**
4266 * ring_buffer_reset - reset a ring buffer
4267 * @buffer: The ring buffer to reset all cpu buffers
4268 */
ring_buffer_reset(struct ring_buffer * buffer)4269 void ring_buffer_reset(struct ring_buffer *buffer)
4270 {
4271 int cpu;
4272
4273 for_each_buffer_cpu(buffer, cpu)
4274 ring_buffer_reset_cpu(buffer, cpu);
4275 }
4276 EXPORT_SYMBOL_GPL(ring_buffer_reset);
4277
4278 /**
4279 * rind_buffer_empty - is the ring buffer empty?
4280 * @buffer: The ring buffer to test
4281 */
ring_buffer_empty(struct ring_buffer * buffer)4282 bool ring_buffer_empty(struct ring_buffer *buffer)
4283 {
4284 struct ring_buffer_per_cpu *cpu_buffer;
4285 unsigned long flags;
4286 bool dolock;
4287 int cpu;
4288 int ret;
4289
4290 /* yes this is racy, but if you don't like the race, lock the buffer */
4291 for_each_buffer_cpu(buffer, cpu) {
4292 cpu_buffer = buffer->buffers[cpu];
4293 local_irq_save(flags);
4294 dolock = rb_reader_lock(cpu_buffer);
4295 ret = rb_per_cpu_empty(cpu_buffer);
4296 rb_reader_unlock(cpu_buffer, dolock);
4297 local_irq_restore(flags);
4298
4299 if (!ret)
4300 return false;
4301 }
4302
4303 return true;
4304 }
4305 EXPORT_SYMBOL_GPL(ring_buffer_empty);
4306
4307 /**
4308 * ring_buffer_empty_cpu - is a cpu buffer of a ring buffer empty?
4309 * @buffer: The ring buffer
4310 * @cpu: The CPU buffer to test
4311 */
ring_buffer_empty_cpu(struct ring_buffer * buffer,int cpu)4312 bool ring_buffer_empty_cpu(struct ring_buffer *buffer, int cpu)
4313 {
4314 struct ring_buffer_per_cpu *cpu_buffer;
4315 unsigned long flags;
4316 bool dolock;
4317 int ret;
4318
4319 if (!cpumask_test_cpu(cpu, buffer->cpumask))
4320 return true;
4321
4322 cpu_buffer = buffer->buffers[cpu];
4323 local_irq_save(flags);
4324 dolock = rb_reader_lock(cpu_buffer);
4325 ret = rb_per_cpu_empty(cpu_buffer);
4326 rb_reader_unlock(cpu_buffer, dolock);
4327 local_irq_restore(flags);
4328
4329 return ret;
4330 }
4331 EXPORT_SYMBOL_GPL(ring_buffer_empty_cpu);
4332
4333 #ifdef CONFIG_RING_BUFFER_ALLOW_SWAP
4334 /**
4335 * ring_buffer_swap_cpu - swap a CPU buffer between two ring buffers
4336 * @buffer_a: One buffer to swap with
4337 * @buffer_b: The other buffer to swap with
4338 *
4339 * This function is useful for tracers that want to take a "snapshot"
4340 * of a CPU buffer and has another back up buffer lying around.
4341 * it is expected that the tracer handles the cpu buffer not being
4342 * used at the moment.
4343 */
ring_buffer_swap_cpu(struct ring_buffer * buffer_a,struct ring_buffer * buffer_b,int cpu)4344 int ring_buffer_swap_cpu(struct ring_buffer *buffer_a,
4345 struct ring_buffer *buffer_b, int cpu)
4346 {
4347 struct ring_buffer_per_cpu *cpu_buffer_a;
4348 struct ring_buffer_per_cpu *cpu_buffer_b;
4349 int ret = -EINVAL;
4350
4351 if (!cpumask_test_cpu(cpu, buffer_a->cpumask) ||
4352 !cpumask_test_cpu(cpu, buffer_b->cpumask))
4353 goto out;
4354
4355 cpu_buffer_a = buffer_a->buffers[cpu];
4356 cpu_buffer_b = buffer_b->buffers[cpu];
4357
4358 /* At least make sure the two buffers are somewhat the same */
4359 if (cpu_buffer_a->nr_pages != cpu_buffer_b->nr_pages)
4360 goto out;
4361
4362 ret = -EAGAIN;
4363
4364 if (atomic_read(&buffer_a->record_disabled))
4365 goto out;
4366
4367 if (atomic_read(&buffer_b->record_disabled))
4368 goto out;
4369
4370 if (atomic_read(&cpu_buffer_a->record_disabled))
4371 goto out;
4372
4373 if (atomic_read(&cpu_buffer_b->record_disabled))
4374 goto out;
4375
4376 /*
4377 * We can't do a synchronize_sched here because this
4378 * function can be called in atomic context.
4379 * Normally this will be called from the same CPU as cpu.
4380 * If not it's up to the caller to protect this.
4381 */
4382 atomic_inc(&cpu_buffer_a->record_disabled);
4383 atomic_inc(&cpu_buffer_b->record_disabled);
4384
4385 ret = -EBUSY;
4386 if (local_read(&cpu_buffer_a->committing))
4387 goto out_dec;
4388 if (local_read(&cpu_buffer_b->committing))
4389 goto out_dec;
4390
4391 buffer_a->buffers[cpu] = cpu_buffer_b;
4392 buffer_b->buffers[cpu] = cpu_buffer_a;
4393
4394 cpu_buffer_b->buffer = buffer_a;
4395 cpu_buffer_a->buffer = buffer_b;
4396
4397 ret = 0;
4398
4399 out_dec:
4400 atomic_dec(&cpu_buffer_a->record_disabled);
4401 atomic_dec(&cpu_buffer_b->record_disabled);
4402 out:
4403 return ret;
4404 }
4405 EXPORT_SYMBOL_GPL(ring_buffer_swap_cpu);
4406 #endif /* CONFIG_RING_BUFFER_ALLOW_SWAP */
4407
4408 /**
4409 * ring_buffer_alloc_read_page - allocate a page to read from buffer
4410 * @buffer: the buffer to allocate for.
4411 * @cpu: the cpu buffer to allocate.
4412 *
4413 * This function is used in conjunction with ring_buffer_read_page.
4414 * When reading a full page from the ring buffer, these functions
4415 * can be used to speed up the process. The calling function should
4416 * allocate a few pages first with this function. Then when it
4417 * needs to get pages from the ring buffer, it passes the result
4418 * of this function into ring_buffer_read_page, which will swap
4419 * the page that was allocated, with the read page of the buffer.
4420 *
4421 * Returns:
4422 * The page allocated, or NULL on error.
4423 */
ring_buffer_alloc_read_page(struct ring_buffer * buffer,int cpu)4424 void *ring_buffer_alloc_read_page(struct ring_buffer *buffer, int cpu)
4425 {
4426 struct buffer_data_page *bpage;
4427 struct page *page;
4428
4429 page = alloc_pages_node(cpu_to_node(cpu),
4430 GFP_KERNEL | __GFP_NORETRY, 0);
4431 if (!page)
4432 return NULL;
4433
4434 bpage = page_address(page);
4435
4436 rb_init_page(bpage);
4437
4438 return bpage;
4439 }
4440 EXPORT_SYMBOL_GPL(ring_buffer_alloc_read_page);
4441
4442 /**
4443 * ring_buffer_free_read_page - free an allocated read page
4444 * @buffer: the buffer the page was allocate for
4445 * @data: the page to free
4446 *
4447 * Free a page allocated from ring_buffer_alloc_read_page.
4448 */
ring_buffer_free_read_page(struct ring_buffer * buffer,void * data)4449 void ring_buffer_free_read_page(struct ring_buffer *buffer, void *data)
4450 {
4451 free_page((unsigned long)data);
4452 }
4453 EXPORT_SYMBOL_GPL(ring_buffer_free_read_page);
4454
4455 /**
4456 * ring_buffer_read_page - extract a page from the ring buffer
4457 * @buffer: buffer to extract from
4458 * @data_page: the page to use allocated from ring_buffer_alloc_read_page
4459 * @len: amount to extract
4460 * @cpu: the cpu of the buffer to extract
4461 * @full: should the extraction only happen when the page is full.
4462 *
4463 * This function will pull out a page from the ring buffer and consume it.
4464 * @data_page must be the address of the variable that was returned
4465 * from ring_buffer_alloc_read_page. This is because the page might be used
4466 * to swap with a page in the ring buffer.
4467 *
4468 * for example:
4469 * rpage = ring_buffer_alloc_read_page(buffer, cpu);
4470 * if (!rpage)
4471 * return error;
4472 * ret = ring_buffer_read_page(buffer, &rpage, len, cpu, 0);
4473 * if (ret >= 0)
4474 * process_page(rpage, ret);
4475 *
4476 * When @full is set, the function will not return true unless
4477 * the writer is off the reader page.
4478 *
4479 * Note: it is up to the calling functions to handle sleeps and wakeups.
4480 * The ring buffer can be used anywhere in the kernel and can not
4481 * blindly call wake_up. The layer that uses the ring buffer must be
4482 * responsible for that.
4483 *
4484 * Returns:
4485 * >=0 if data has been transferred, returns the offset of consumed data.
4486 * <0 if no data has been transferred.
4487 */
ring_buffer_read_page(struct ring_buffer * buffer,void ** data_page,size_t len,int cpu,int full)4488 int ring_buffer_read_page(struct ring_buffer *buffer,
4489 void **data_page, size_t len, int cpu, int full)
4490 {
4491 struct ring_buffer_per_cpu *cpu_buffer = buffer->buffers[cpu];
4492 struct ring_buffer_event *event;
4493 struct buffer_data_page *bpage;
4494 struct buffer_page *reader;
4495 unsigned long missed_events;
4496 unsigned long flags;
4497 unsigned int commit;
4498 unsigned int read;
4499 u64 save_timestamp;
4500 int ret = -1;
4501
4502 if (!cpumask_test_cpu(cpu, buffer->cpumask))
4503 goto out;
4504
4505 /*
4506 * If len is not big enough to hold the page header, then
4507 * we can not copy anything.
4508 */
4509 if (len <= BUF_PAGE_HDR_SIZE)
4510 goto out;
4511
4512 len -= BUF_PAGE_HDR_SIZE;
4513
4514 if (!data_page)
4515 goto out;
4516
4517 bpage = *data_page;
4518 if (!bpage)
4519 goto out;
4520
4521 raw_spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
4522
4523 reader = rb_get_reader_page(cpu_buffer);
4524 if (!reader)
4525 goto out_unlock;
4526
4527 event = rb_reader_event(cpu_buffer);
4528
4529 read = reader->read;
4530 commit = rb_page_commit(reader);
4531
4532 /* Check if any events were dropped */
4533 missed_events = cpu_buffer->lost_events;
4534
4535 /*
4536 * If this page has been partially read or
4537 * if len is not big enough to read the rest of the page or
4538 * a writer is still on the page, then
4539 * we must copy the data from the page to the buffer.
4540 * Otherwise, we can simply swap the page with the one passed in.
4541 */
4542 if (read || (len < (commit - read)) ||
4543 cpu_buffer->reader_page == cpu_buffer->commit_page) {
4544 struct buffer_data_page *rpage = cpu_buffer->reader_page->page;
4545 unsigned int rpos = read;
4546 unsigned int pos = 0;
4547 unsigned int size;
4548
4549 if (full)
4550 goto out_unlock;
4551
4552 if (len > (commit - read))
4553 len = (commit - read);
4554
4555 /* Always keep the time extend and data together */
4556 size = rb_event_ts_length(event);
4557
4558 if (len < size)
4559 goto out_unlock;
4560
4561 /* save the current timestamp, since the user will need it */
4562 save_timestamp = cpu_buffer->read_stamp;
4563
4564 /* Need to copy one event at a time */
4565 do {
4566 /* We need the size of one event, because
4567 * rb_advance_reader only advances by one event,
4568 * whereas rb_event_ts_length may include the size of
4569 * one or two events.
4570 * We have already ensured there's enough space if this
4571 * is a time extend. */
4572 size = rb_event_length(event);
4573 memcpy(bpage->data + pos, rpage->data + rpos, size);
4574
4575 len -= size;
4576
4577 rb_advance_reader(cpu_buffer);
4578 rpos = reader->read;
4579 pos += size;
4580
4581 if (rpos >= commit)
4582 break;
4583
4584 event = rb_reader_event(cpu_buffer);
4585 /* Always keep the time extend and data together */
4586 size = rb_event_ts_length(event);
4587 } while (len >= size);
4588
4589 /* update bpage */
4590 local_set(&bpage->commit, pos);
4591 bpage->time_stamp = save_timestamp;
4592
4593 /* we copied everything to the beginning */
4594 read = 0;
4595 } else {
4596 /* update the entry counter */
4597 cpu_buffer->read += rb_page_entries(reader);
4598 cpu_buffer->read_bytes += BUF_PAGE_SIZE;
4599
4600 /* swap the pages */
4601 rb_init_page(bpage);
4602 bpage = reader->page;
4603 reader->page = *data_page;
4604 local_set(&reader->write, 0);
4605 local_set(&reader->entries, 0);
4606 reader->read = 0;
4607 *data_page = bpage;
4608
4609 /*
4610 * Use the real_end for the data size,
4611 * This gives us a chance to store the lost events
4612 * on the page.
4613 */
4614 if (reader->real_end)
4615 local_set(&bpage->commit, reader->real_end);
4616 }
4617 ret = read;
4618
4619 cpu_buffer->lost_events = 0;
4620
4621 commit = local_read(&bpage->commit);
4622 /*
4623 * Set a flag in the commit field if we lost events
4624 */
4625 if (missed_events) {
4626 /* If there is room at the end of the page to save the
4627 * missed events, then record it there.
4628 */
4629 if (BUF_PAGE_SIZE - commit >= sizeof(missed_events)) {
4630 memcpy(&bpage->data[commit], &missed_events,
4631 sizeof(missed_events));
4632 local_add(RB_MISSED_STORED, &bpage->commit);
4633 commit += sizeof(missed_events);
4634 }
4635 local_add(RB_MISSED_EVENTS, &bpage->commit);
4636 }
4637
4638 /*
4639 * This page may be off to user land. Zero it out here.
4640 */
4641 if (commit < BUF_PAGE_SIZE)
4642 memset(&bpage->data[commit], 0, BUF_PAGE_SIZE - commit);
4643
4644 out_unlock:
4645 raw_spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
4646
4647 out:
4648 return ret;
4649 }
4650 EXPORT_SYMBOL_GPL(ring_buffer_read_page);
4651
4652 #ifdef CONFIG_HOTPLUG_CPU
rb_cpu_notify(struct notifier_block * self,unsigned long action,void * hcpu)4653 static int rb_cpu_notify(struct notifier_block *self,
4654 unsigned long action, void *hcpu)
4655 {
4656 struct ring_buffer *buffer =
4657 container_of(self, struct ring_buffer, cpu_notify);
4658 long cpu = (long)hcpu;
4659 long nr_pages_same;
4660 int cpu_i;
4661 unsigned long nr_pages;
4662
4663 switch (action) {
4664 case CPU_UP_PREPARE:
4665 case CPU_UP_PREPARE_FROZEN:
4666 if (cpumask_test_cpu(cpu, buffer->cpumask))
4667 return NOTIFY_OK;
4668
4669 nr_pages = 0;
4670 nr_pages_same = 1;
4671 /* check if all cpu sizes are same */
4672 for_each_buffer_cpu(buffer, cpu_i) {
4673 /* fill in the size from first enabled cpu */
4674 if (nr_pages == 0)
4675 nr_pages = buffer->buffers[cpu_i]->nr_pages;
4676 if (nr_pages != buffer->buffers[cpu_i]->nr_pages) {
4677 nr_pages_same = 0;
4678 break;
4679 }
4680 }
4681 /* allocate minimum pages, user can later expand it */
4682 if (!nr_pages_same)
4683 nr_pages = 2;
4684 buffer->buffers[cpu] =
4685 rb_allocate_cpu_buffer(buffer, nr_pages, cpu);
4686 if (!buffer->buffers[cpu]) {
4687 WARN(1, "failed to allocate ring buffer on CPU %ld\n",
4688 cpu);
4689 return NOTIFY_OK;
4690 }
4691 smp_wmb();
4692 cpumask_set_cpu(cpu, buffer->cpumask);
4693 break;
4694 case CPU_DOWN_PREPARE:
4695 case CPU_DOWN_PREPARE_FROZEN:
4696 /*
4697 * Do nothing.
4698 * If we were to free the buffer, then the user would
4699 * lose any trace that was in the buffer.
4700 */
4701 break;
4702 default:
4703 break;
4704 }
4705 return NOTIFY_OK;
4706 }
4707 #endif
4708
4709 #ifdef CONFIG_RING_BUFFER_STARTUP_TEST
4710 /*
4711 * This is a basic integrity check of the ring buffer.
4712 * Late in the boot cycle this test will run when configured in.
4713 * It will kick off a thread per CPU that will go into a loop
4714 * writing to the per cpu ring buffer various sizes of data.
4715 * Some of the data will be large items, some small.
4716 *
4717 * Another thread is created that goes into a spin, sending out
4718 * IPIs to the other CPUs to also write into the ring buffer.
4719 * this is to test the nesting ability of the buffer.
4720 *
4721 * Basic stats are recorded and reported. If something in the
4722 * ring buffer should happen that's not expected, a big warning
4723 * is displayed and all ring buffers are disabled.
4724 */
4725 static struct task_struct *rb_threads[NR_CPUS] __initdata;
4726
4727 struct rb_test_data {
4728 struct ring_buffer *buffer;
4729 unsigned long events;
4730 unsigned long bytes_written;
4731 unsigned long bytes_alloc;
4732 unsigned long bytes_dropped;
4733 unsigned long events_nested;
4734 unsigned long bytes_written_nested;
4735 unsigned long bytes_alloc_nested;
4736 unsigned long bytes_dropped_nested;
4737 int min_size_nested;
4738 int max_size_nested;
4739 int max_size;
4740 int min_size;
4741 int cpu;
4742 int cnt;
4743 };
4744
4745 static struct rb_test_data rb_data[NR_CPUS] __initdata;
4746
4747 /* 1 meg per cpu */
4748 #define RB_TEST_BUFFER_SIZE 1048576
4749
4750 static char rb_string[] __initdata =
4751 "abcdefghijklmnopqrstuvwxyz1234567890!@#$%^&*()?+\\"
4752 "?+|:';\",.<>/?abcdefghijklmnopqrstuvwxyz1234567890"
4753 "!@#$%^&*()?+\\?+|:';\",.<>/?abcdefghijklmnopqrstuv";
4754
4755 static bool rb_test_started __initdata;
4756
4757 struct rb_item {
4758 int size;
4759 char str[];
4760 };
4761
rb_write_something(struct rb_test_data * data,bool nested)4762 static __init int rb_write_something(struct rb_test_data *data, bool nested)
4763 {
4764 struct ring_buffer_event *event;
4765 struct rb_item *item;
4766 bool started;
4767 int event_len;
4768 int size;
4769 int len;
4770 int cnt;
4771
4772 /* Have nested writes different that what is written */
4773 cnt = data->cnt + (nested ? 27 : 0);
4774
4775 /* Multiply cnt by ~e, to make some unique increment */
4776 size = (data->cnt * 68 / 25) % (sizeof(rb_string) - 1);
4777
4778 len = size + sizeof(struct rb_item);
4779
4780 started = rb_test_started;
4781 /* read rb_test_started before checking buffer enabled */
4782 smp_rmb();
4783
4784 event = ring_buffer_lock_reserve(data->buffer, len);
4785 if (!event) {
4786 /* Ignore dropped events before test starts. */
4787 if (started) {
4788 if (nested)
4789 data->bytes_dropped += len;
4790 else
4791 data->bytes_dropped_nested += len;
4792 }
4793 return len;
4794 }
4795
4796 event_len = ring_buffer_event_length(event);
4797
4798 if (RB_WARN_ON(data->buffer, event_len < len))
4799 goto out;
4800
4801 item = ring_buffer_event_data(event);
4802 item->size = size;
4803 memcpy(item->str, rb_string, size);
4804
4805 if (nested) {
4806 data->bytes_alloc_nested += event_len;
4807 data->bytes_written_nested += len;
4808 data->events_nested++;
4809 if (!data->min_size_nested || len < data->min_size_nested)
4810 data->min_size_nested = len;
4811 if (len > data->max_size_nested)
4812 data->max_size_nested = len;
4813 } else {
4814 data->bytes_alloc += event_len;
4815 data->bytes_written += len;
4816 data->events++;
4817 if (!data->min_size || len < data->min_size)
4818 data->max_size = len;
4819 if (len > data->max_size)
4820 data->max_size = len;
4821 }
4822
4823 out:
4824 ring_buffer_unlock_commit(data->buffer, event);
4825
4826 return 0;
4827 }
4828
rb_test(void * arg)4829 static __init int rb_test(void *arg)
4830 {
4831 struct rb_test_data *data = arg;
4832
4833 while (!kthread_should_stop()) {
4834 rb_write_something(data, false);
4835 data->cnt++;
4836
4837 set_current_state(TASK_INTERRUPTIBLE);
4838 /* Now sleep between a min of 100-300us and a max of 1ms */
4839 usleep_range(((data->cnt % 3) + 1) * 100, 1000);
4840 }
4841
4842 return 0;
4843 }
4844
rb_ipi(void * ignore)4845 static __init void rb_ipi(void *ignore)
4846 {
4847 struct rb_test_data *data;
4848 int cpu = smp_processor_id();
4849
4850 data = &rb_data[cpu];
4851 rb_write_something(data, true);
4852 }
4853
rb_hammer_test(void * arg)4854 static __init int rb_hammer_test(void *arg)
4855 {
4856 while (!kthread_should_stop()) {
4857
4858 /* Send an IPI to all cpus to write data! */
4859 smp_call_function(rb_ipi, NULL, 1);
4860 /* No sleep, but for non preempt, let others run */
4861 schedule();
4862 }
4863
4864 return 0;
4865 }
4866
test_ringbuffer(void)4867 static __init int test_ringbuffer(void)
4868 {
4869 struct task_struct *rb_hammer;
4870 struct ring_buffer *buffer;
4871 int cpu;
4872 int ret = 0;
4873
4874 pr_info("Running ring buffer tests...\n");
4875
4876 buffer = ring_buffer_alloc(RB_TEST_BUFFER_SIZE, RB_FL_OVERWRITE);
4877 if (WARN_ON(!buffer))
4878 return 0;
4879
4880 /* Disable buffer so that threads can't write to it yet */
4881 ring_buffer_record_off(buffer);
4882
4883 for_each_online_cpu(cpu) {
4884 rb_data[cpu].buffer = buffer;
4885 rb_data[cpu].cpu = cpu;
4886 rb_data[cpu].cnt = cpu;
4887 rb_threads[cpu] = kthread_create(rb_test, &rb_data[cpu],
4888 "rbtester/%d", cpu);
4889 if (WARN_ON(IS_ERR(rb_threads[cpu]))) {
4890 pr_cont("FAILED\n");
4891 ret = PTR_ERR(rb_threads[cpu]);
4892 goto out_free;
4893 }
4894
4895 kthread_bind(rb_threads[cpu], cpu);
4896 wake_up_process(rb_threads[cpu]);
4897 }
4898
4899 /* Now create the rb hammer! */
4900 rb_hammer = kthread_run(rb_hammer_test, NULL, "rbhammer");
4901 if (WARN_ON(IS_ERR(rb_hammer))) {
4902 pr_cont("FAILED\n");
4903 ret = PTR_ERR(rb_hammer);
4904 goto out_free;
4905 }
4906
4907 ring_buffer_record_on(buffer);
4908 /*
4909 * Show buffer is enabled before setting rb_test_started.
4910 * Yes there's a small race window where events could be
4911 * dropped and the thread wont catch it. But when a ring
4912 * buffer gets enabled, there will always be some kind of
4913 * delay before other CPUs see it. Thus, we don't care about
4914 * those dropped events. We care about events dropped after
4915 * the threads see that the buffer is active.
4916 */
4917 smp_wmb();
4918 rb_test_started = true;
4919
4920 set_current_state(TASK_INTERRUPTIBLE);
4921 /* Just run for 10 seconds */;
4922 schedule_timeout(10 * HZ);
4923
4924 kthread_stop(rb_hammer);
4925
4926 out_free:
4927 for_each_online_cpu(cpu) {
4928 if (!rb_threads[cpu])
4929 break;
4930 kthread_stop(rb_threads[cpu]);
4931 }
4932 if (ret) {
4933 ring_buffer_free(buffer);
4934 return ret;
4935 }
4936
4937 /* Report! */
4938 pr_info("finished\n");
4939 for_each_online_cpu(cpu) {
4940 struct ring_buffer_event *event;
4941 struct rb_test_data *data = &rb_data[cpu];
4942 struct rb_item *item;
4943 unsigned long total_events;
4944 unsigned long total_dropped;
4945 unsigned long total_written;
4946 unsigned long total_alloc;
4947 unsigned long total_read = 0;
4948 unsigned long total_size = 0;
4949 unsigned long total_len = 0;
4950 unsigned long total_lost = 0;
4951 unsigned long lost;
4952 int big_event_size;
4953 int small_event_size;
4954
4955 ret = -1;
4956
4957 total_events = data->events + data->events_nested;
4958 total_written = data->bytes_written + data->bytes_written_nested;
4959 total_alloc = data->bytes_alloc + data->bytes_alloc_nested;
4960 total_dropped = data->bytes_dropped + data->bytes_dropped_nested;
4961
4962 big_event_size = data->max_size + data->max_size_nested;
4963 small_event_size = data->min_size + data->min_size_nested;
4964
4965 pr_info("CPU %d:\n", cpu);
4966 pr_info(" events: %ld\n", total_events);
4967 pr_info(" dropped bytes: %ld\n", total_dropped);
4968 pr_info(" alloced bytes: %ld\n", total_alloc);
4969 pr_info(" written bytes: %ld\n", total_written);
4970 pr_info(" biggest event: %d\n", big_event_size);
4971 pr_info(" smallest event: %d\n", small_event_size);
4972
4973 if (RB_WARN_ON(buffer, total_dropped))
4974 break;
4975
4976 ret = 0;
4977
4978 while ((event = ring_buffer_consume(buffer, cpu, NULL, &lost))) {
4979 total_lost += lost;
4980 item = ring_buffer_event_data(event);
4981 total_len += ring_buffer_event_length(event);
4982 total_size += item->size + sizeof(struct rb_item);
4983 if (memcmp(&item->str[0], rb_string, item->size) != 0) {
4984 pr_info("FAILED!\n");
4985 pr_info("buffer had: %.*s\n", item->size, item->str);
4986 pr_info("expected: %.*s\n", item->size, rb_string);
4987 RB_WARN_ON(buffer, 1);
4988 ret = -1;
4989 break;
4990 }
4991 total_read++;
4992 }
4993 if (ret)
4994 break;
4995
4996 ret = -1;
4997
4998 pr_info(" read events: %ld\n", total_read);
4999 pr_info(" lost events: %ld\n", total_lost);
5000 pr_info(" total events: %ld\n", total_lost + total_read);
5001 pr_info(" recorded len bytes: %ld\n", total_len);
5002 pr_info(" recorded size bytes: %ld\n", total_size);
5003 if (total_lost)
5004 pr_info(" With dropped events, record len and size may not match\n"
5005 " alloced and written from above\n");
5006 if (!total_lost) {
5007 if (RB_WARN_ON(buffer, total_len != total_alloc ||
5008 total_size != total_written))
5009 break;
5010 }
5011 if (RB_WARN_ON(buffer, total_lost + total_read != total_events))
5012 break;
5013
5014 ret = 0;
5015 }
5016 if (!ret)
5017 pr_info("Ring buffer PASSED!\n");
5018
5019 ring_buffer_free(buffer);
5020 return 0;
5021 }
5022
5023 late_initcall(test_ringbuffer);
5024 #endif /* CONFIG_RING_BUFFER_STARTUP_TEST */
5025