Merge tag 'v3.10.55' into update
[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 event 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