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