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