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