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