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