tracing: add EXPORT_SYMBOL_GPL for trace commits
[linux-2.6-block.git] / kernel / trace / ring_buffer.c
CommitLineData
7a8e76a3
SR
1/*
2 * Generic ring buffer
3 *
4 * Copyright (C) 2008 Steven Rostedt <srostedt@redhat.com>
5 */
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>
7a8e76a3
SR
13#include <linux/module.h>
14#include <linux/percpu.h>
15#include <linux/mutex.h>
7a8e76a3
SR
16#include <linux/init.h>
17#include <linux/hash.h>
18#include <linux/list.h>
554f786e 19#include <linux/cpu.h>
7a8e76a3
SR
20#include <linux/fs.h>
21
182e9f5f
SR
22#include "trace.h"
23
d1b182a8
SR
24/*
25 * The ring buffer header is special. We must manually up keep it.
26 */
27int ring_buffer_print_entry_header(struct trace_seq *s)
28{
29 int ret;
30
31 ret = trace_seq_printf(s, "\ttype : 2 bits\n");
32 ret = trace_seq_printf(s, "\tlen : 3 bits\n");
33 ret = trace_seq_printf(s, "\ttime_delta : 27 bits\n");
34 ret = trace_seq_printf(s, "\tarray : 32 bits\n");
35 ret = trace_seq_printf(s, "\n");
36 ret = trace_seq_printf(s, "\tpadding : type == %d\n",
37 RINGBUF_TYPE_PADDING);
38 ret = trace_seq_printf(s, "\ttime_extend : type == %d\n",
39 RINGBUF_TYPE_TIME_EXTEND);
40 ret = trace_seq_printf(s, "\tdata : type == %d\n",
41 RINGBUF_TYPE_DATA);
42
43 return ret;
44}
45
5cc98548
SR
46/*
47 * The ring buffer is made up of a list of pages. A separate list of pages is
48 * allocated for each CPU. A writer may only write to a buffer that is
49 * associated with the CPU it is currently executing on. A reader may read
50 * from any per cpu buffer.
51 *
52 * The reader is special. For each per cpu buffer, the reader has its own
53 * reader page. When a reader has read the entire reader page, this reader
54 * page is swapped with another page in the ring buffer.
55 *
56 * Now, as long as the writer is off the reader page, the reader can do what
57 * ever it wants with that page. The writer will never write to that page
58 * again (as long as it is out of the ring buffer).
59 *
60 * Here's some silly ASCII art.
61 *
62 * +------+
63 * |reader| RING BUFFER
64 * |page |
65 * +------+ +---+ +---+ +---+
66 * | |-->| |-->| |
67 * +---+ +---+ +---+
68 * ^ |
69 * | |
70 * +---------------+
71 *
72 *
73 * +------+
74 * |reader| RING BUFFER
75 * |page |------------------v
76 * +------+ +---+ +---+ +---+
77 * | |-->| |-->| |
78 * +---+ +---+ +---+
79 * ^ |
80 * | |
81 * +---------------+
82 *
83 *
84 * +------+
85 * |reader| RING BUFFER
86 * |page |------------------v
87 * +------+ +---+ +---+ +---+
88 * ^ | |-->| |-->| |
89 * | +---+ +---+ +---+
90 * | |
91 * | |
92 * +------------------------------+
93 *
94 *
95 * +------+
96 * |buffer| RING BUFFER
97 * |page |------------------v
98 * +------+ +---+ +---+ +---+
99 * ^ | | | |-->| |
100 * | New +---+ +---+ +---+
101 * | Reader------^ |
102 * | page |
103 * +------------------------------+
104 *
105 *
106 * After we make this swap, the reader can hand this page off to the splice
107 * code and be done with it. It can even allocate a new page if it needs to
108 * and swap that into the ring buffer.
109 *
110 * We will be using cmpxchg soon to make all this lockless.
111 *
112 */
113
033601a3
SR
114/*
115 * A fast way to enable or disable all ring buffers is to
116 * call tracing_on or tracing_off. Turning off the ring buffers
117 * prevents all ring buffers from being recorded to.
118 * Turning this switch on, makes it OK to write to the
119 * ring buffer, if the ring buffer is enabled itself.
120 *
121 * There's three layers that must be on in order to write
122 * to the ring buffer.
123 *
124 * 1) This global flag must be set.
125 * 2) The ring buffer must be enabled for recording.
126 * 3) The per cpu buffer must be enabled for recording.
127 *
128 * In case of an anomaly, this global flag has a bit set that
129 * will permantly disable all ring buffers.
130 */
131
132/*
133 * Global flag to disable all recording to ring buffers
134 * This has two bits: ON, DISABLED
135 *
136 * ON DISABLED
137 * ---- ----------
138 * 0 0 : ring buffers are off
139 * 1 0 : ring buffers are on
140 * X 1 : ring buffers are permanently disabled
141 */
142
143enum {
144 RB_BUFFERS_ON_BIT = 0,
145 RB_BUFFERS_DISABLED_BIT = 1,
146};
147
148enum {
149 RB_BUFFERS_ON = 1 << RB_BUFFERS_ON_BIT,
150 RB_BUFFERS_DISABLED = 1 << RB_BUFFERS_DISABLED_BIT,
151};
152
5e39841c 153static unsigned long ring_buffer_flags __read_mostly = RB_BUFFERS_ON;
a3583244 154
474d32b6
SR
155#define BUF_PAGE_HDR_SIZE offsetof(struct buffer_data_page, data)
156
a3583244
SR
157/**
158 * tracing_on - enable all tracing buffers
159 *
160 * This function enables all tracing buffers that may have been
161 * disabled with tracing_off.
162 */
163void tracing_on(void)
164{
033601a3 165 set_bit(RB_BUFFERS_ON_BIT, &ring_buffer_flags);
a3583244 166}
c4f50183 167EXPORT_SYMBOL_GPL(tracing_on);
a3583244
SR
168
169/**
170 * tracing_off - turn off all tracing buffers
171 *
172 * This function stops all tracing buffers from recording data.
173 * It does not disable any overhead the tracers themselves may
174 * be causing. This function simply causes all recording to
175 * the ring buffers to fail.
176 */
177void tracing_off(void)
178{
033601a3
SR
179 clear_bit(RB_BUFFERS_ON_BIT, &ring_buffer_flags);
180}
c4f50183 181EXPORT_SYMBOL_GPL(tracing_off);
033601a3
SR
182
183/**
184 * tracing_off_permanent - permanently disable ring buffers
185 *
186 * This function, once called, will disable all ring buffers
c3706f00 187 * permanently.
033601a3
SR
188 */
189void tracing_off_permanent(void)
190{
191 set_bit(RB_BUFFERS_DISABLED_BIT, &ring_buffer_flags);
a3583244
SR
192}
193
988ae9d6
SR
194/**
195 * tracing_is_on - show state of ring buffers enabled
196 */
197int tracing_is_on(void)
198{
199 return ring_buffer_flags == RB_BUFFERS_ON;
200}
201EXPORT_SYMBOL_GPL(tracing_is_on);
202
d06bbd66
IM
203#include "trace.h"
204
e3d6bf0a 205#define RB_EVNT_HDR_SIZE (offsetof(struct ring_buffer_event, array))
67d34724 206#define RB_ALIGNMENT 4U
7a8e76a3
SR
207#define RB_MAX_SMALL_DATA 28
208
209enum {
210 RB_LEN_TIME_EXTEND = 8,
211 RB_LEN_TIME_STAMP = 16,
212};
213
2d622719
TZ
214static inline int rb_null_event(struct ring_buffer_event *event)
215{
216 return event->type == RINGBUF_TYPE_PADDING && event->time_delta == 0;
217}
218
219static inline int rb_discarded_event(struct ring_buffer_event *event)
220{
221 return event->type == RINGBUF_TYPE_PADDING && event->time_delta;
222}
223
224static void rb_event_set_padding(struct ring_buffer_event *event)
225{
226 event->type = RINGBUF_TYPE_PADDING;
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
2d622719
TZ
235 if (event->len)
236 length = event->len * RB_ALIGNMENT;
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{
7a8e76a3
SR
246 switch (event->type) {
247 case RINGBUF_TYPE_PADDING:
2d622719
TZ
248 if (rb_null_event(event))
249 /* undefined */
250 return -1;
251 return rb_event_data_length(event);
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
RR
274 unsigned length = rb_event_length(event);
275 if (event->type != RINGBUF_TYPE_DATA)
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{
288 BUG_ON(event->type != RINGBUF_TYPE_DATA);
289 /* If length is in len field, then array[0] has the data */
290 if (event->len)
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
319struct buffer_page {
320 local_t write; /* index for next write */
6f807acd 321 unsigned read; /* index for next read */
e4c2ce82 322 struct list_head list; /* list of free pages */
abc9b56d 323 struct buffer_data_page *page; /* Actual data page */
7a8e76a3
SR
324};
325
044fa782 326static void rb_init_page(struct buffer_data_page *bpage)
abc9b56d 327{
044fa782 328 local_set(&bpage->commit, 0);
abc9b56d
SR
329}
330
474d32b6
SR
331/**
332 * ring_buffer_page_len - the size of data on the page.
333 * @page: The page to read
334 *
335 * Returns the amount of data on the page, including buffer page header.
336 */
ef7a4a16
SR
337size_t ring_buffer_page_len(void *page)
338{
474d32b6
SR
339 return local_read(&((struct buffer_data_page *)page)->commit)
340 + BUF_PAGE_HDR_SIZE;
ef7a4a16
SR
341}
342
ed56829c
SR
343/*
344 * Also stolen from mm/slob.c. Thanks to Mathieu Desnoyers for pointing
345 * this issue out.
346 */
34a148bf 347static void free_buffer_page(struct buffer_page *bpage)
ed56829c 348{
34a148bf 349 free_page((unsigned long)bpage->page);
e4c2ce82 350 kfree(bpage);
ed56829c
SR
351}
352
7a8e76a3
SR
353/*
354 * We need to fit the time_stamp delta into 27 bits.
355 */
356static inline int test_time_stamp(u64 delta)
357{
358 if (delta & TS_DELTA_TEST)
359 return 1;
360 return 0;
361}
362
474d32b6 363#define BUF_PAGE_SIZE (PAGE_SIZE - BUF_PAGE_HDR_SIZE)
7a8e76a3 364
d1b182a8
SR
365int ring_buffer_print_page_header(struct trace_seq *s)
366{
367 struct buffer_data_page field;
368 int ret;
369
370 ret = trace_seq_printf(s, "\tfield: u64 timestamp;\t"
371 "offset:0;\tsize:%u;\n",
372 (unsigned int)sizeof(field.time_stamp));
373
374 ret = trace_seq_printf(s, "\tfield: local_t commit;\t"
375 "offset:%u;\tsize:%u;\n",
376 (unsigned int)offsetof(typeof(field), commit),
377 (unsigned int)sizeof(field.commit));
378
379 ret = trace_seq_printf(s, "\tfield: char data;\t"
380 "offset:%u;\tsize:%u;\n",
381 (unsigned int)offsetof(typeof(field), data),
382 (unsigned int)BUF_PAGE_SIZE);
383
384 return ret;
385}
386
7a8e76a3
SR
387/*
388 * head_page == tail_page && head == tail then buffer is empty.
389 */
390struct ring_buffer_per_cpu {
391 int cpu;
392 struct ring_buffer *buffer;
f83c9d0f 393 spinlock_t reader_lock; /* serialize readers */
3e03fb7f 394 raw_spinlock_t lock;
7a8e76a3
SR
395 struct lock_class_key lock_key;
396 struct list_head pages;
6f807acd
SR
397 struct buffer_page *head_page; /* read from head */
398 struct buffer_page *tail_page; /* write to tail */
c3706f00 399 struct buffer_page *commit_page; /* committed pages */
d769041f 400 struct buffer_page *reader_page;
7a8e76a3
SR
401 unsigned long overrun;
402 unsigned long entries;
403 u64 write_stamp;
404 u64 read_stamp;
405 atomic_t record_disabled;
406};
407
408struct ring_buffer {
7a8e76a3
SR
409 unsigned pages;
410 unsigned flags;
411 int cpus;
7a8e76a3 412 atomic_t record_disabled;
00f62f61 413 cpumask_var_t cpumask;
7a8e76a3
SR
414
415 struct mutex mutex;
416
417 struct ring_buffer_per_cpu **buffers;
554f786e 418
59222efe 419#ifdef CONFIG_HOTPLUG_CPU
554f786e
SR
420 struct notifier_block cpu_notify;
421#endif
37886f6a 422 u64 (*clock)(void);
7a8e76a3
SR
423};
424
425struct ring_buffer_iter {
426 struct ring_buffer_per_cpu *cpu_buffer;
427 unsigned long head;
428 struct buffer_page *head_page;
429 u64 read_stamp;
430};
431
f536aafc 432/* buffer may be either ring_buffer or ring_buffer_per_cpu */
bf41a158 433#define RB_WARN_ON(buffer, cond) \
3e89c7bb
SR
434 ({ \
435 int _____ret = unlikely(cond); \
436 if (_____ret) { \
bf41a158
SR
437 atomic_inc(&buffer->record_disabled); \
438 WARN_ON(1); \
439 } \
3e89c7bb
SR
440 _____ret; \
441 })
f536aafc 442
37886f6a
SR
443/* Up this if you want to test the TIME_EXTENTS and normalization */
444#define DEBUG_SHIFT 0
445
446u64 ring_buffer_time_stamp(struct ring_buffer *buffer, int cpu)
447{
448 u64 time;
449
450 preempt_disable_notrace();
451 /* shift to debug/test normalization and TIME_EXTENTS */
452 time = buffer->clock() << DEBUG_SHIFT;
453 preempt_enable_no_resched_notrace();
454
455 return time;
456}
457EXPORT_SYMBOL_GPL(ring_buffer_time_stamp);
458
459void ring_buffer_normalize_time_stamp(struct ring_buffer *buffer,
460 int cpu, u64 *ts)
461{
462 /* Just stupid testing the normalize function and deltas */
463 *ts >>= DEBUG_SHIFT;
464}
465EXPORT_SYMBOL_GPL(ring_buffer_normalize_time_stamp);
466
7a8e76a3
SR
467/**
468 * check_pages - integrity check of buffer pages
469 * @cpu_buffer: CPU buffer with pages to test
470 *
c3706f00 471 * As a safety measure we check to make sure the data pages have not
7a8e76a3
SR
472 * been corrupted.
473 */
474static int rb_check_pages(struct ring_buffer_per_cpu *cpu_buffer)
475{
476 struct list_head *head = &cpu_buffer->pages;
044fa782 477 struct buffer_page *bpage, *tmp;
7a8e76a3 478
3e89c7bb
SR
479 if (RB_WARN_ON(cpu_buffer, head->next->prev != head))
480 return -1;
481 if (RB_WARN_ON(cpu_buffer, head->prev->next != head))
482 return -1;
7a8e76a3 483
044fa782 484 list_for_each_entry_safe(bpage, tmp, head, list) {
3e89c7bb 485 if (RB_WARN_ON(cpu_buffer,
044fa782 486 bpage->list.next->prev != &bpage->list))
3e89c7bb
SR
487 return -1;
488 if (RB_WARN_ON(cpu_buffer,
044fa782 489 bpage->list.prev->next != &bpage->list))
3e89c7bb 490 return -1;
7a8e76a3
SR
491 }
492
493 return 0;
494}
495
7a8e76a3
SR
496static int rb_allocate_pages(struct ring_buffer_per_cpu *cpu_buffer,
497 unsigned nr_pages)
498{
499 struct list_head *head = &cpu_buffer->pages;
044fa782 500 struct buffer_page *bpage, *tmp;
7a8e76a3
SR
501 unsigned long addr;
502 LIST_HEAD(pages);
503 unsigned i;
504
505 for (i = 0; i < nr_pages; i++) {
044fa782 506 bpage = kzalloc_node(ALIGN(sizeof(*bpage), cache_line_size()),
aa1e0e3b 507 GFP_KERNEL, cpu_to_node(cpu_buffer->cpu));
044fa782 508 if (!bpage)
e4c2ce82 509 goto free_pages;
044fa782 510 list_add(&bpage->list, &pages);
e4c2ce82 511
7a8e76a3
SR
512 addr = __get_free_page(GFP_KERNEL);
513 if (!addr)
514 goto free_pages;
044fa782
SR
515 bpage->page = (void *)addr;
516 rb_init_page(bpage->page);
7a8e76a3
SR
517 }
518
519 list_splice(&pages, head);
520
521 rb_check_pages(cpu_buffer);
522
523 return 0;
524
525 free_pages:
044fa782
SR
526 list_for_each_entry_safe(bpage, tmp, &pages, list) {
527 list_del_init(&bpage->list);
528 free_buffer_page(bpage);
7a8e76a3
SR
529 }
530 return -ENOMEM;
531}
532
533static struct ring_buffer_per_cpu *
534rb_allocate_cpu_buffer(struct ring_buffer *buffer, int cpu)
535{
536 struct ring_buffer_per_cpu *cpu_buffer;
044fa782 537 struct buffer_page *bpage;
d769041f 538 unsigned long addr;
7a8e76a3
SR
539 int ret;
540
541 cpu_buffer = kzalloc_node(ALIGN(sizeof(*cpu_buffer), cache_line_size()),
542 GFP_KERNEL, cpu_to_node(cpu));
543 if (!cpu_buffer)
544 return NULL;
545
546 cpu_buffer->cpu = cpu;
547 cpu_buffer->buffer = buffer;
f83c9d0f 548 spin_lock_init(&cpu_buffer->reader_lock);
3e03fb7f 549 cpu_buffer->lock = (raw_spinlock_t)__RAW_SPIN_LOCK_UNLOCKED;
7a8e76a3
SR
550 INIT_LIST_HEAD(&cpu_buffer->pages);
551
044fa782 552 bpage = kzalloc_node(ALIGN(sizeof(*bpage), cache_line_size()),
e4c2ce82 553 GFP_KERNEL, cpu_to_node(cpu));
044fa782 554 if (!bpage)
e4c2ce82
SR
555 goto fail_free_buffer;
556
044fa782 557 cpu_buffer->reader_page = bpage;
d769041f
SR
558 addr = __get_free_page(GFP_KERNEL);
559 if (!addr)
e4c2ce82 560 goto fail_free_reader;
044fa782
SR
561 bpage->page = (void *)addr;
562 rb_init_page(bpage->page);
e4c2ce82 563
d769041f 564 INIT_LIST_HEAD(&cpu_buffer->reader_page->list);
d769041f 565
7a8e76a3
SR
566 ret = rb_allocate_pages(cpu_buffer, buffer->pages);
567 if (ret < 0)
d769041f 568 goto fail_free_reader;
7a8e76a3
SR
569
570 cpu_buffer->head_page
571 = list_entry(cpu_buffer->pages.next, struct buffer_page, list);
bf41a158 572 cpu_buffer->tail_page = cpu_buffer->commit_page = cpu_buffer->head_page;
7a8e76a3
SR
573
574 return cpu_buffer;
575
d769041f
SR
576 fail_free_reader:
577 free_buffer_page(cpu_buffer->reader_page);
578
7a8e76a3
SR
579 fail_free_buffer:
580 kfree(cpu_buffer);
581 return NULL;
582}
583
584static void rb_free_cpu_buffer(struct ring_buffer_per_cpu *cpu_buffer)
585{
586 struct list_head *head = &cpu_buffer->pages;
044fa782 587 struct buffer_page *bpage, *tmp;
7a8e76a3 588
d769041f
SR
589 free_buffer_page(cpu_buffer->reader_page);
590
044fa782
SR
591 list_for_each_entry_safe(bpage, tmp, head, list) {
592 list_del_init(&bpage->list);
593 free_buffer_page(bpage);
7a8e76a3
SR
594 }
595 kfree(cpu_buffer);
596}
597
a7b13743
SR
598/*
599 * Causes compile errors if the struct buffer_page gets bigger
600 * than the struct page.
601 */
602extern int ring_buffer_page_too_big(void);
603
59222efe 604#ifdef CONFIG_HOTPLUG_CPU
09c9e84d
FW
605static int rb_cpu_notify(struct notifier_block *self,
606 unsigned long action, void *hcpu);
554f786e
SR
607#endif
608
7a8e76a3
SR
609/**
610 * ring_buffer_alloc - allocate a new ring_buffer
68814b58 611 * @size: the size in bytes per cpu that is needed.
7a8e76a3
SR
612 * @flags: attributes to set for the ring buffer.
613 *
614 * Currently the only flag that is available is the RB_FL_OVERWRITE
615 * flag. This flag means that the buffer will overwrite old data
616 * when the buffer wraps. If this flag is not set, the buffer will
617 * drop data when the tail hits the head.
618 */
619struct ring_buffer *ring_buffer_alloc(unsigned long size, unsigned flags)
620{
621 struct ring_buffer *buffer;
622 int bsize;
623 int cpu;
624
a7b13743
SR
625 /* Paranoid! Optimizes out when all is well */
626 if (sizeof(struct buffer_page) > sizeof(struct page))
627 ring_buffer_page_too_big();
628
629
7a8e76a3
SR
630 /* keep it in its own cache line */
631 buffer = kzalloc(ALIGN(sizeof(*buffer), cache_line_size()),
632 GFP_KERNEL);
633 if (!buffer)
634 return NULL;
635
9e01c1b7
RR
636 if (!alloc_cpumask_var(&buffer->cpumask, GFP_KERNEL))
637 goto fail_free_buffer;
638
7a8e76a3
SR
639 buffer->pages = DIV_ROUND_UP(size, BUF_PAGE_SIZE);
640 buffer->flags = flags;
37886f6a 641 buffer->clock = trace_clock_local;
7a8e76a3
SR
642
643 /* need at least two pages */
644 if (buffer->pages == 1)
645 buffer->pages++;
646
3bf832ce
FW
647 /*
648 * In case of non-hotplug cpu, if the ring-buffer is allocated
649 * in early initcall, it will not be notified of secondary cpus.
650 * In that off case, we need to allocate for all possible cpus.
651 */
652#ifdef CONFIG_HOTPLUG_CPU
554f786e
SR
653 get_online_cpus();
654 cpumask_copy(buffer->cpumask, cpu_online_mask);
3bf832ce
FW
655#else
656 cpumask_copy(buffer->cpumask, cpu_possible_mask);
657#endif
7a8e76a3
SR
658 buffer->cpus = nr_cpu_ids;
659
660 bsize = sizeof(void *) * nr_cpu_ids;
661 buffer->buffers = kzalloc(ALIGN(bsize, cache_line_size()),
662 GFP_KERNEL);
663 if (!buffer->buffers)
9e01c1b7 664 goto fail_free_cpumask;
7a8e76a3
SR
665
666 for_each_buffer_cpu(buffer, cpu) {
667 buffer->buffers[cpu] =
668 rb_allocate_cpu_buffer(buffer, cpu);
669 if (!buffer->buffers[cpu])
670 goto fail_free_buffers;
671 }
672
59222efe 673#ifdef CONFIG_HOTPLUG_CPU
554f786e
SR
674 buffer->cpu_notify.notifier_call = rb_cpu_notify;
675 buffer->cpu_notify.priority = 0;
676 register_cpu_notifier(&buffer->cpu_notify);
677#endif
678
679 put_online_cpus();
7a8e76a3
SR
680 mutex_init(&buffer->mutex);
681
682 return buffer;
683
684 fail_free_buffers:
685 for_each_buffer_cpu(buffer, cpu) {
686 if (buffer->buffers[cpu])
687 rb_free_cpu_buffer(buffer->buffers[cpu]);
688 }
689 kfree(buffer->buffers);
690
9e01c1b7
RR
691 fail_free_cpumask:
692 free_cpumask_var(buffer->cpumask);
554f786e 693 put_online_cpus();
9e01c1b7 694
7a8e76a3
SR
695 fail_free_buffer:
696 kfree(buffer);
697 return NULL;
698}
c4f50183 699EXPORT_SYMBOL_GPL(ring_buffer_alloc);
7a8e76a3
SR
700
701/**
702 * ring_buffer_free - free a ring buffer.
703 * @buffer: the buffer to free.
704 */
705void
706ring_buffer_free(struct ring_buffer *buffer)
707{
708 int cpu;
709
554f786e
SR
710 get_online_cpus();
711
59222efe 712#ifdef CONFIG_HOTPLUG_CPU
554f786e
SR
713 unregister_cpu_notifier(&buffer->cpu_notify);
714#endif
715
7a8e76a3
SR
716 for_each_buffer_cpu(buffer, cpu)
717 rb_free_cpu_buffer(buffer->buffers[cpu]);
718
554f786e
SR
719 put_online_cpus();
720
9e01c1b7
RR
721 free_cpumask_var(buffer->cpumask);
722
7a8e76a3
SR
723 kfree(buffer);
724}
c4f50183 725EXPORT_SYMBOL_GPL(ring_buffer_free);
7a8e76a3 726
37886f6a
SR
727void ring_buffer_set_clock(struct ring_buffer *buffer,
728 u64 (*clock)(void))
729{
730 buffer->clock = clock;
731}
732
7a8e76a3
SR
733static void rb_reset_cpu(struct ring_buffer_per_cpu *cpu_buffer);
734
735static void
736rb_remove_pages(struct ring_buffer_per_cpu *cpu_buffer, unsigned nr_pages)
737{
044fa782 738 struct buffer_page *bpage;
7a8e76a3
SR
739 struct list_head *p;
740 unsigned i;
741
742 atomic_inc(&cpu_buffer->record_disabled);
743 synchronize_sched();
744
745 for (i = 0; i < nr_pages; i++) {
3e89c7bb
SR
746 if (RB_WARN_ON(cpu_buffer, list_empty(&cpu_buffer->pages)))
747 return;
7a8e76a3 748 p = cpu_buffer->pages.next;
044fa782
SR
749 bpage = list_entry(p, struct buffer_page, list);
750 list_del_init(&bpage->list);
751 free_buffer_page(bpage);
7a8e76a3 752 }
3e89c7bb
SR
753 if (RB_WARN_ON(cpu_buffer, list_empty(&cpu_buffer->pages)))
754 return;
7a8e76a3
SR
755
756 rb_reset_cpu(cpu_buffer);
757
758 rb_check_pages(cpu_buffer);
759
760 atomic_dec(&cpu_buffer->record_disabled);
761
762}
763
764static void
765rb_insert_pages(struct ring_buffer_per_cpu *cpu_buffer,
766 struct list_head *pages, unsigned nr_pages)
767{
044fa782 768 struct buffer_page *bpage;
7a8e76a3
SR
769 struct list_head *p;
770 unsigned i;
771
772 atomic_inc(&cpu_buffer->record_disabled);
773 synchronize_sched();
774
775 for (i = 0; i < nr_pages; i++) {
3e89c7bb
SR
776 if (RB_WARN_ON(cpu_buffer, list_empty(pages)))
777 return;
7a8e76a3 778 p = pages->next;
044fa782
SR
779 bpage = list_entry(p, struct buffer_page, list);
780 list_del_init(&bpage->list);
781 list_add_tail(&bpage->list, &cpu_buffer->pages);
7a8e76a3
SR
782 }
783 rb_reset_cpu(cpu_buffer);
784
785 rb_check_pages(cpu_buffer);
786
787 atomic_dec(&cpu_buffer->record_disabled);
788}
789
790/**
791 * ring_buffer_resize - resize the ring buffer
792 * @buffer: the buffer to resize.
793 * @size: the new size.
794 *
795 * The tracer is responsible for making sure that the buffer is
796 * not being used while changing the size.
797 * Note: We may be able to change the above requirement by using
798 * RCU synchronizations.
799 *
800 * Minimum size is 2 * BUF_PAGE_SIZE.
801 *
802 * Returns -1 on failure.
803 */
804int ring_buffer_resize(struct ring_buffer *buffer, unsigned long size)
805{
806 struct ring_buffer_per_cpu *cpu_buffer;
807 unsigned nr_pages, rm_pages, new_pages;
044fa782 808 struct buffer_page *bpage, *tmp;
7a8e76a3
SR
809 unsigned long buffer_size;
810 unsigned long addr;
811 LIST_HEAD(pages);
812 int i, cpu;
813
ee51a1de
IM
814 /*
815 * Always succeed at resizing a non-existent buffer:
816 */
817 if (!buffer)
818 return size;
819
7a8e76a3
SR
820 size = DIV_ROUND_UP(size, BUF_PAGE_SIZE);
821 size *= BUF_PAGE_SIZE;
822 buffer_size = buffer->pages * BUF_PAGE_SIZE;
823
824 /* we need a minimum of two pages */
825 if (size < BUF_PAGE_SIZE * 2)
826 size = BUF_PAGE_SIZE * 2;
827
828 if (size == buffer_size)
829 return size;
830
831 mutex_lock(&buffer->mutex);
554f786e 832 get_online_cpus();
7a8e76a3
SR
833
834 nr_pages = DIV_ROUND_UP(size, BUF_PAGE_SIZE);
835
836 if (size < buffer_size) {
837
838 /* easy case, just free pages */
554f786e
SR
839 if (RB_WARN_ON(buffer, nr_pages >= buffer->pages))
840 goto out_fail;
7a8e76a3
SR
841
842 rm_pages = buffer->pages - nr_pages;
843
844 for_each_buffer_cpu(buffer, cpu) {
845 cpu_buffer = buffer->buffers[cpu];
846 rb_remove_pages(cpu_buffer, rm_pages);
847 }
848 goto out;
849 }
850
851 /*
852 * This is a bit more difficult. We only want to add pages
853 * when we can allocate enough for all CPUs. We do this
854 * by allocating all the pages and storing them on a local
855 * link list. If we succeed in our allocation, then we
856 * add these pages to the cpu_buffers. Otherwise we just free
857 * them all and return -ENOMEM;
858 */
554f786e
SR
859 if (RB_WARN_ON(buffer, nr_pages <= buffer->pages))
860 goto out_fail;
f536aafc 861
7a8e76a3
SR
862 new_pages = nr_pages - buffer->pages;
863
864 for_each_buffer_cpu(buffer, cpu) {
865 for (i = 0; i < new_pages; i++) {
044fa782 866 bpage = kzalloc_node(ALIGN(sizeof(*bpage),
e4c2ce82
SR
867 cache_line_size()),
868 GFP_KERNEL, cpu_to_node(cpu));
044fa782 869 if (!bpage)
e4c2ce82 870 goto free_pages;
044fa782 871 list_add(&bpage->list, &pages);
7a8e76a3
SR
872 addr = __get_free_page(GFP_KERNEL);
873 if (!addr)
874 goto free_pages;
044fa782
SR
875 bpage->page = (void *)addr;
876 rb_init_page(bpage->page);
7a8e76a3
SR
877 }
878 }
879
880 for_each_buffer_cpu(buffer, cpu) {
881 cpu_buffer = buffer->buffers[cpu];
882 rb_insert_pages(cpu_buffer, &pages, new_pages);
883 }
884
554f786e
SR
885 if (RB_WARN_ON(buffer, !list_empty(&pages)))
886 goto out_fail;
7a8e76a3
SR
887
888 out:
889 buffer->pages = nr_pages;
554f786e 890 put_online_cpus();
7a8e76a3
SR
891 mutex_unlock(&buffer->mutex);
892
893 return size;
894
895 free_pages:
044fa782
SR
896 list_for_each_entry_safe(bpage, tmp, &pages, list) {
897 list_del_init(&bpage->list);
898 free_buffer_page(bpage);
7a8e76a3 899 }
554f786e 900 put_online_cpus();
641d2f63 901 mutex_unlock(&buffer->mutex);
7a8e76a3 902 return -ENOMEM;
554f786e
SR
903
904 /*
905 * Something went totally wrong, and we are too paranoid
906 * to even clean up the mess.
907 */
908 out_fail:
909 put_online_cpus();
910 mutex_unlock(&buffer->mutex);
911 return -1;
7a8e76a3 912}
c4f50183 913EXPORT_SYMBOL_GPL(ring_buffer_resize);
7a8e76a3 914
8789a9e7 915static inline void *
044fa782 916__rb_data_page_index(struct buffer_data_page *bpage, unsigned index)
8789a9e7 917{
044fa782 918 return bpage->data + index;
8789a9e7
SR
919}
920
044fa782 921static inline void *__rb_page_index(struct buffer_page *bpage, unsigned index)
7a8e76a3 922{
044fa782 923 return bpage->page->data + index;
7a8e76a3
SR
924}
925
926static inline struct ring_buffer_event *
d769041f 927rb_reader_event(struct ring_buffer_per_cpu *cpu_buffer)
7a8e76a3 928{
6f807acd
SR
929 return __rb_page_index(cpu_buffer->reader_page,
930 cpu_buffer->reader_page->read);
931}
932
933static inline struct ring_buffer_event *
934rb_head_event(struct ring_buffer_per_cpu *cpu_buffer)
935{
936 return __rb_page_index(cpu_buffer->head_page,
937 cpu_buffer->head_page->read);
7a8e76a3
SR
938}
939
940static inline struct ring_buffer_event *
941rb_iter_head_event(struct ring_buffer_iter *iter)
942{
6f807acd 943 return __rb_page_index(iter->head_page, iter->head);
7a8e76a3
SR
944}
945
bf41a158
SR
946static inline unsigned rb_page_write(struct buffer_page *bpage)
947{
948 return local_read(&bpage->write);
949}
950
951static inline unsigned rb_page_commit(struct buffer_page *bpage)
952{
abc9b56d 953 return local_read(&bpage->page->commit);
bf41a158
SR
954}
955
956/* Size is determined by what has been commited */
957static inline unsigned rb_page_size(struct buffer_page *bpage)
958{
959 return rb_page_commit(bpage);
960}
961
962static inline unsigned
963rb_commit_index(struct ring_buffer_per_cpu *cpu_buffer)
964{
965 return rb_page_commit(cpu_buffer->commit_page);
966}
967
968static inline unsigned rb_head_size(struct ring_buffer_per_cpu *cpu_buffer)
969{
970 return rb_page_commit(cpu_buffer->head_page);
971}
972
7a8e76a3
SR
973/*
974 * When the tail hits the head and the buffer is in overwrite mode,
975 * the head jumps to the next page and all content on the previous
976 * page is discarded. But before doing so, we update the overrun
977 * variable of the buffer.
978 */
979static void rb_update_overflow(struct ring_buffer_per_cpu *cpu_buffer)
980{
981 struct ring_buffer_event *event;
982 unsigned long head;
983
984 for (head = 0; head < rb_head_size(cpu_buffer);
985 head += rb_event_length(event)) {
986
6f807acd 987 event = __rb_page_index(cpu_buffer->head_page, head);
3e89c7bb
SR
988 if (RB_WARN_ON(cpu_buffer, rb_null_event(event)))
989 return;
7a8e76a3
SR
990 /* Only count data entries */
991 if (event->type != RINGBUF_TYPE_DATA)
992 continue;
993 cpu_buffer->overrun++;
994 cpu_buffer->entries--;
995 }
996}
997
998static inline void rb_inc_page(struct ring_buffer_per_cpu *cpu_buffer,
044fa782 999 struct buffer_page **bpage)
7a8e76a3 1000{
044fa782 1001 struct list_head *p = (*bpage)->list.next;
7a8e76a3
SR
1002
1003 if (p == &cpu_buffer->pages)
1004 p = p->next;
1005
044fa782 1006 *bpage = list_entry(p, struct buffer_page, list);
7a8e76a3
SR
1007}
1008
bf41a158
SR
1009static inline unsigned
1010rb_event_index(struct ring_buffer_event *event)
1011{
1012 unsigned long addr = (unsigned long)event;
1013
1014 return (addr & ~PAGE_MASK) - (PAGE_SIZE - BUF_PAGE_SIZE);
1015}
1016
34a148bf 1017static int
bf41a158
SR
1018rb_is_commit(struct ring_buffer_per_cpu *cpu_buffer,
1019 struct ring_buffer_event *event)
1020{
1021 unsigned long addr = (unsigned long)event;
1022 unsigned long index;
1023
1024 index = rb_event_index(event);
1025 addr &= PAGE_MASK;
1026
1027 return cpu_buffer->commit_page->page == (void *)addr &&
1028 rb_commit_index(cpu_buffer) == index;
1029}
1030
34a148bf 1031static void
bf41a158
SR
1032rb_set_commit_event(struct ring_buffer_per_cpu *cpu_buffer,
1033 struct ring_buffer_event *event)
7a8e76a3 1034{
bf41a158
SR
1035 unsigned long addr = (unsigned long)event;
1036 unsigned long index;
1037
1038 index = rb_event_index(event);
1039 addr &= PAGE_MASK;
1040
1041 while (cpu_buffer->commit_page->page != (void *)addr) {
3e89c7bb
SR
1042 if (RB_WARN_ON(cpu_buffer,
1043 cpu_buffer->commit_page == cpu_buffer->tail_page))
1044 return;
abc9b56d 1045 cpu_buffer->commit_page->page->commit =
bf41a158
SR
1046 cpu_buffer->commit_page->write;
1047 rb_inc_page(cpu_buffer, &cpu_buffer->commit_page);
abc9b56d
SR
1048 cpu_buffer->write_stamp =
1049 cpu_buffer->commit_page->page->time_stamp;
bf41a158
SR
1050 }
1051
1052 /* Now set the commit to the event's index */
abc9b56d 1053 local_set(&cpu_buffer->commit_page->page->commit, index);
7a8e76a3
SR
1054}
1055
34a148bf 1056static void
bf41a158 1057rb_set_commit_to_write(struct ring_buffer_per_cpu *cpu_buffer)
7a8e76a3 1058{
bf41a158
SR
1059 /*
1060 * We only race with interrupts and NMIs on this CPU.
1061 * If we own the commit event, then we can commit
1062 * all others that interrupted us, since the interruptions
1063 * are in stack format (they finish before they come
1064 * back to us). This allows us to do a simple loop to
1065 * assign the commit to the tail.
1066 */
a8ccf1d6 1067 again:
bf41a158 1068 while (cpu_buffer->commit_page != cpu_buffer->tail_page) {
abc9b56d 1069 cpu_buffer->commit_page->page->commit =
bf41a158
SR
1070 cpu_buffer->commit_page->write;
1071 rb_inc_page(cpu_buffer, &cpu_buffer->commit_page);
abc9b56d
SR
1072 cpu_buffer->write_stamp =
1073 cpu_buffer->commit_page->page->time_stamp;
bf41a158
SR
1074 /* add barrier to keep gcc from optimizing too much */
1075 barrier();
1076 }
1077 while (rb_commit_index(cpu_buffer) !=
1078 rb_page_write(cpu_buffer->commit_page)) {
abc9b56d 1079 cpu_buffer->commit_page->page->commit =
bf41a158
SR
1080 cpu_buffer->commit_page->write;
1081 barrier();
1082 }
a8ccf1d6
SR
1083
1084 /* again, keep gcc from optimizing */
1085 barrier();
1086
1087 /*
1088 * If an interrupt came in just after the first while loop
1089 * and pushed the tail page forward, we will be left with
1090 * a dangling commit that will never go forward.
1091 */
1092 if (unlikely(cpu_buffer->commit_page != cpu_buffer->tail_page))
1093 goto again;
7a8e76a3
SR
1094}
1095
d769041f 1096static void rb_reset_reader_page(struct ring_buffer_per_cpu *cpu_buffer)
7a8e76a3 1097{
abc9b56d 1098 cpu_buffer->read_stamp = cpu_buffer->reader_page->page->time_stamp;
6f807acd 1099 cpu_buffer->reader_page->read = 0;
d769041f
SR
1100}
1101
34a148bf 1102static void rb_inc_iter(struct ring_buffer_iter *iter)
d769041f
SR
1103{
1104 struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
1105
1106 /*
1107 * The iterator could be on the reader page (it starts there).
1108 * But the head could have moved, since the reader was
1109 * found. Check for this case and assign the iterator
1110 * to the head page instead of next.
1111 */
1112 if (iter->head_page == cpu_buffer->reader_page)
1113 iter->head_page = cpu_buffer->head_page;
1114 else
1115 rb_inc_page(cpu_buffer, &iter->head_page);
1116
abc9b56d 1117 iter->read_stamp = iter->head_page->page->time_stamp;
7a8e76a3
SR
1118 iter->head = 0;
1119}
1120
1121/**
1122 * ring_buffer_update_event - update event type and data
1123 * @event: the even to update
1124 * @type: the type of event
1125 * @length: the size of the event field in the ring buffer
1126 *
1127 * Update the type and data fields of the event. The length
1128 * is the actual size that is written to the ring buffer,
1129 * and with this, we can determine what to place into the
1130 * data field.
1131 */
34a148bf 1132static void
7a8e76a3
SR
1133rb_update_event(struct ring_buffer_event *event,
1134 unsigned type, unsigned length)
1135{
1136 event->type = type;
1137
1138 switch (type) {
1139
1140 case RINGBUF_TYPE_PADDING:
1141 break;
1142
1143 case RINGBUF_TYPE_TIME_EXTEND:
67d34724 1144 event->len = DIV_ROUND_UP(RB_LEN_TIME_EXTEND, RB_ALIGNMENT);
7a8e76a3
SR
1145 break;
1146
1147 case RINGBUF_TYPE_TIME_STAMP:
67d34724 1148 event->len = DIV_ROUND_UP(RB_LEN_TIME_STAMP, RB_ALIGNMENT);
7a8e76a3
SR
1149 break;
1150
1151 case RINGBUF_TYPE_DATA:
1152 length -= RB_EVNT_HDR_SIZE;
1153 if (length > RB_MAX_SMALL_DATA) {
1154 event->len = 0;
1155 event->array[0] = length;
1156 } else
67d34724 1157 event->len = DIV_ROUND_UP(length, RB_ALIGNMENT);
7a8e76a3
SR
1158 break;
1159 default:
1160 BUG();
1161 }
1162}
1163
34a148bf 1164static unsigned rb_calculate_event_length(unsigned length)
7a8e76a3
SR
1165{
1166 struct ring_buffer_event event; /* Used only for sizeof array */
1167
1168 /* zero length can cause confusions */
1169 if (!length)
1170 length = 1;
1171
1172 if (length > RB_MAX_SMALL_DATA)
1173 length += sizeof(event.array[0]);
1174
1175 length += RB_EVNT_HDR_SIZE;
1176 length = ALIGN(length, RB_ALIGNMENT);
1177
1178 return length;
1179}
1180
1181static struct ring_buffer_event *
1182__rb_reserve_next(struct ring_buffer_per_cpu *cpu_buffer,
1183 unsigned type, unsigned long length, u64 *ts)
1184{
98db8df7 1185 struct buffer_page *tail_page, *head_page, *reader_page, *commit_page;
bf41a158 1186 unsigned long tail, write;
7a8e76a3
SR
1187 struct ring_buffer *buffer = cpu_buffer->buffer;
1188 struct ring_buffer_event *event;
bf41a158 1189 unsigned long flags;
78d904b4 1190 bool lock_taken = false;
7a8e76a3 1191
98db8df7
SR
1192 commit_page = cpu_buffer->commit_page;
1193 /* we just need to protect against interrupts */
1194 barrier();
7a8e76a3 1195 tail_page = cpu_buffer->tail_page;
bf41a158
SR
1196 write = local_add_return(length, &tail_page->write);
1197 tail = write - length;
7a8e76a3 1198
bf41a158
SR
1199 /* See if we shot pass the end of this buffer page */
1200 if (write > BUF_PAGE_SIZE) {
7a8e76a3
SR
1201 struct buffer_page *next_page = tail_page;
1202
3e03fb7f 1203 local_irq_save(flags);
78d904b4 1204 /*
a81bd80a
SR
1205 * Since the write to the buffer is still not
1206 * fully lockless, we must be careful with NMIs.
1207 * The locks in the writers are taken when a write
1208 * crosses to a new page. The locks protect against
1209 * races with the readers (this will soon be fixed
1210 * with a lockless solution).
1211 *
1212 * Because we can not protect against NMIs, and we
1213 * want to keep traces reentrant, we need to manage
1214 * what happens when we are in an NMI.
1215 *
78d904b4
SR
1216 * NMIs can happen after we take the lock.
1217 * If we are in an NMI, only take the lock
1218 * if it is not already taken. Otherwise
1219 * simply fail.
1220 */
a81bd80a 1221 if (unlikely(in_nmi())) {
78d904b4 1222 if (!__raw_spin_trylock(&cpu_buffer->lock))
45141d46 1223 goto out_reset;
78d904b4
SR
1224 } else
1225 __raw_spin_lock(&cpu_buffer->lock);
1226
1227 lock_taken = true;
bf41a158 1228
7a8e76a3
SR
1229 rb_inc_page(cpu_buffer, &next_page);
1230
d769041f
SR
1231 head_page = cpu_buffer->head_page;
1232 reader_page = cpu_buffer->reader_page;
1233
1234 /* we grabbed the lock before incrementing */
3e89c7bb 1235 if (RB_WARN_ON(cpu_buffer, next_page == reader_page))
45141d46 1236 goto out_reset;
bf41a158
SR
1237
1238 /*
1239 * If for some reason, we had an interrupt storm that made
1240 * it all the way around the buffer, bail, and warn
1241 * about it.
1242 */
98db8df7 1243 if (unlikely(next_page == commit_page)) {
bf41a158 1244 WARN_ON_ONCE(1);
45141d46 1245 goto out_reset;
bf41a158 1246 }
d769041f 1247
7a8e76a3 1248 if (next_page == head_page) {
6f3b3440 1249 if (!(buffer->flags & RB_FL_OVERWRITE))
45141d46 1250 goto out_reset;
7a8e76a3 1251
bf41a158
SR
1252 /* tail_page has not moved yet? */
1253 if (tail_page == cpu_buffer->tail_page) {
1254 /* count overflows */
1255 rb_update_overflow(cpu_buffer);
1256
1257 rb_inc_page(cpu_buffer, &head_page);
1258 cpu_buffer->head_page = head_page;
1259 cpu_buffer->head_page->read = 0;
1260 }
1261 }
7a8e76a3 1262
bf41a158
SR
1263 /*
1264 * If the tail page is still the same as what we think
1265 * it is, then it is up to us to update the tail
1266 * pointer.
1267 */
1268 if (tail_page == cpu_buffer->tail_page) {
1269 local_set(&next_page->write, 0);
abc9b56d 1270 local_set(&next_page->page->commit, 0);
bf41a158
SR
1271 cpu_buffer->tail_page = next_page;
1272
1273 /* reread the time stamp */
37886f6a 1274 *ts = ring_buffer_time_stamp(buffer, cpu_buffer->cpu);
abc9b56d 1275 cpu_buffer->tail_page->page->time_stamp = *ts;
7a8e76a3
SR
1276 }
1277
bf41a158
SR
1278 /*
1279 * The actual tail page has moved forward.
1280 */
1281 if (tail < BUF_PAGE_SIZE) {
1282 /* Mark the rest of the page with padding */
6f807acd 1283 event = __rb_page_index(tail_page, tail);
2d622719 1284 rb_event_set_padding(event);
7a8e76a3
SR
1285 }
1286
bf41a158
SR
1287 if (tail <= BUF_PAGE_SIZE)
1288 /* Set the write back to the previous setting */
1289 local_set(&tail_page->write, tail);
1290
1291 /*
1292 * If this was a commit entry that failed,
1293 * increment that too
1294 */
1295 if (tail_page == cpu_buffer->commit_page &&
1296 tail == rb_commit_index(cpu_buffer)) {
1297 rb_set_commit_to_write(cpu_buffer);
1298 }
1299
3e03fb7f
SR
1300 __raw_spin_unlock(&cpu_buffer->lock);
1301 local_irq_restore(flags);
bf41a158
SR
1302
1303 /* fail and let the caller try again */
1304 return ERR_PTR(-EAGAIN);
7a8e76a3
SR
1305 }
1306
bf41a158
SR
1307 /* We reserved something on the buffer */
1308
3e89c7bb
SR
1309 if (RB_WARN_ON(cpu_buffer, write > BUF_PAGE_SIZE))
1310 return NULL;
7a8e76a3 1311
6f807acd 1312 event = __rb_page_index(tail_page, tail);
7a8e76a3
SR
1313 rb_update_event(event, type, length);
1314
bf41a158
SR
1315 /*
1316 * If this is a commit and the tail is zero, then update
1317 * this page's time stamp.
1318 */
1319 if (!tail && rb_is_commit(cpu_buffer, event))
abc9b56d 1320 cpu_buffer->commit_page->page->time_stamp = *ts;
bf41a158 1321
7a8e76a3 1322 return event;
bf41a158 1323
45141d46 1324 out_reset:
6f3b3440
LJ
1325 /* reset write */
1326 if (tail <= BUF_PAGE_SIZE)
1327 local_set(&tail_page->write, tail);
1328
78d904b4
SR
1329 if (likely(lock_taken))
1330 __raw_spin_unlock(&cpu_buffer->lock);
3e03fb7f 1331 local_irq_restore(flags);
bf41a158 1332 return NULL;
7a8e76a3
SR
1333}
1334
1335static int
1336rb_add_time_stamp(struct ring_buffer_per_cpu *cpu_buffer,
1337 u64 *ts, u64 *delta)
1338{
1339 struct ring_buffer_event *event;
1340 static int once;
bf41a158 1341 int ret;
7a8e76a3
SR
1342
1343 if (unlikely(*delta > (1ULL << 59) && !once++)) {
1344 printk(KERN_WARNING "Delta way too big! %llu"
1345 " ts=%llu write stamp = %llu\n",
e2862c94
SR
1346 (unsigned long long)*delta,
1347 (unsigned long long)*ts,
1348 (unsigned long long)cpu_buffer->write_stamp);
7a8e76a3
SR
1349 WARN_ON(1);
1350 }
1351
1352 /*
1353 * The delta is too big, we to add a
1354 * new timestamp.
1355 */
1356 event = __rb_reserve_next(cpu_buffer,
1357 RINGBUF_TYPE_TIME_EXTEND,
1358 RB_LEN_TIME_EXTEND,
1359 ts);
1360 if (!event)
bf41a158 1361 return -EBUSY;
7a8e76a3 1362
bf41a158
SR
1363 if (PTR_ERR(event) == -EAGAIN)
1364 return -EAGAIN;
1365
1366 /* Only a commited time event can update the write stamp */
1367 if (rb_is_commit(cpu_buffer, event)) {
1368 /*
1369 * If this is the first on the page, then we need to
1370 * update the page itself, and just put in a zero.
1371 */
1372 if (rb_event_index(event)) {
1373 event->time_delta = *delta & TS_MASK;
1374 event->array[0] = *delta >> TS_SHIFT;
1375 } else {
abc9b56d 1376 cpu_buffer->commit_page->page->time_stamp = *ts;
bf41a158
SR
1377 event->time_delta = 0;
1378 event->array[0] = 0;
1379 }
7a8e76a3 1380 cpu_buffer->write_stamp = *ts;
bf41a158
SR
1381 /* let the caller know this was the commit */
1382 ret = 1;
1383 } else {
1384 /* Darn, this is just wasted space */
1385 event->time_delta = 0;
1386 event->array[0] = 0;
1387 ret = 0;
7a8e76a3
SR
1388 }
1389
bf41a158
SR
1390 *delta = 0;
1391
1392 return ret;
7a8e76a3
SR
1393}
1394
1395static struct ring_buffer_event *
1396rb_reserve_next_event(struct ring_buffer_per_cpu *cpu_buffer,
1397 unsigned type, unsigned long length)
1398{
1399 struct ring_buffer_event *event;
1400 u64 ts, delta;
bf41a158 1401 int commit = 0;
818e3dd3 1402 int nr_loops = 0;
7a8e76a3 1403
bf41a158 1404 again:
818e3dd3
SR
1405 /*
1406 * We allow for interrupts to reenter here and do a trace.
1407 * If one does, it will cause this original code to loop
1408 * back here. Even with heavy interrupts happening, this
1409 * should only happen a few times in a row. If this happens
1410 * 1000 times in a row, there must be either an interrupt
1411 * storm or we have something buggy.
1412 * Bail!
1413 */
3e89c7bb 1414 if (RB_WARN_ON(cpu_buffer, ++nr_loops > 1000))
818e3dd3 1415 return NULL;
818e3dd3 1416
37886f6a 1417 ts = ring_buffer_time_stamp(cpu_buffer->buffer, cpu_buffer->cpu);
7a8e76a3 1418
bf41a158
SR
1419 /*
1420 * Only the first commit can update the timestamp.
1421 * Yes there is a race here. If an interrupt comes in
1422 * just after the conditional and it traces too, then it
1423 * will also check the deltas. More than one timestamp may
1424 * also be made. But only the entry that did the actual
1425 * commit will be something other than zero.
1426 */
1427 if (cpu_buffer->tail_page == cpu_buffer->commit_page &&
1428 rb_page_write(cpu_buffer->tail_page) ==
1429 rb_commit_index(cpu_buffer)) {
1430
7a8e76a3
SR
1431 delta = ts - cpu_buffer->write_stamp;
1432
bf41a158
SR
1433 /* make sure this delta is calculated here */
1434 barrier();
1435
1436 /* Did the write stamp get updated already? */
1437 if (unlikely(ts < cpu_buffer->write_stamp))
4143c5cb 1438 delta = 0;
bf41a158 1439
7a8e76a3 1440 if (test_time_stamp(delta)) {
7a8e76a3 1441
bf41a158
SR
1442 commit = rb_add_time_stamp(cpu_buffer, &ts, &delta);
1443
1444 if (commit == -EBUSY)
7a8e76a3 1445 return NULL;
bf41a158
SR
1446
1447 if (commit == -EAGAIN)
1448 goto again;
1449
1450 RB_WARN_ON(cpu_buffer, commit < 0);
7a8e76a3 1451 }
bf41a158
SR
1452 } else
1453 /* Non commits have zero deltas */
7a8e76a3 1454 delta = 0;
7a8e76a3
SR
1455
1456 event = __rb_reserve_next(cpu_buffer, type, length, &ts);
bf41a158
SR
1457 if (PTR_ERR(event) == -EAGAIN)
1458 goto again;
1459
1460 if (!event) {
1461 if (unlikely(commit))
1462 /*
1463 * Ouch! We needed a timestamp and it was commited. But
1464 * we didn't get our event reserved.
1465 */
1466 rb_set_commit_to_write(cpu_buffer);
7a8e76a3 1467 return NULL;
bf41a158 1468 }
7a8e76a3 1469
bf41a158
SR
1470 /*
1471 * If the timestamp was commited, make the commit our entry
1472 * now so that we will update it when needed.
1473 */
1474 if (commit)
1475 rb_set_commit_event(cpu_buffer, event);
1476 else if (!rb_is_commit(cpu_buffer, event))
7a8e76a3
SR
1477 delta = 0;
1478
1479 event->time_delta = delta;
1480
1481 return event;
1482}
1483
bf41a158
SR
1484static DEFINE_PER_CPU(int, rb_need_resched);
1485
7a8e76a3
SR
1486/**
1487 * ring_buffer_lock_reserve - reserve a part of the buffer
1488 * @buffer: the ring buffer to reserve from
1489 * @length: the length of the data to reserve (excluding event header)
7a8e76a3
SR
1490 *
1491 * Returns a reseverd event on the ring buffer to copy directly to.
1492 * The user of this interface will need to get the body to write into
1493 * and can use the ring_buffer_event_data() interface.
1494 *
1495 * The length is the length of the data needed, not the event length
1496 * which also includes the event header.
1497 *
1498 * Must be paired with ring_buffer_unlock_commit, unless NULL is returned.
1499 * If NULL is returned, then nothing has been allocated or locked.
1500 */
1501struct ring_buffer_event *
0a987751 1502ring_buffer_lock_reserve(struct ring_buffer *buffer, unsigned long length)
7a8e76a3
SR
1503{
1504 struct ring_buffer_per_cpu *cpu_buffer;
1505 struct ring_buffer_event *event;
bf41a158 1506 int cpu, resched;
7a8e76a3 1507
033601a3 1508 if (ring_buffer_flags != RB_BUFFERS_ON)
a3583244
SR
1509 return NULL;
1510
7a8e76a3
SR
1511 if (atomic_read(&buffer->record_disabled))
1512 return NULL;
1513
bf41a158 1514 /* If we are tracing schedule, we don't want to recurse */
182e9f5f 1515 resched = ftrace_preempt_disable();
bf41a158 1516
7a8e76a3
SR
1517 cpu = raw_smp_processor_id();
1518
9e01c1b7 1519 if (!cpumask_test_cpu(cpu, buffer->cpumask))
d769041f 1520 goto out;
7a8e76a3
SR
1521
1522 cpu_buffer = buffer->buffers[cpu];
7a8e76a3
SR
1523
1524 if (atomic_read(&cpu_buffer->record_disabled))
d769041f 1525 goto out;
7a8e76a3
SR
1526
1527 length = rb_calculate_event_length(length);
1528 if (length > BUF_PAGE_SIZE)
bf41a158 1529 goto out;
7a8e76a3
SR
1530
1531 event = rb_reserve_next_event(cpu_buffer, RINGBUF_TYPE_DATA, length);
1532 if (!event)
d769041f 1533 goto out;
7a8e76a3 1534
bf41a158
SR
1535 /*
1536 * Need to store resched state on this cpu.
1537 * Only the first needs to.
1538 */
1539
1540 if (preempt_count() == 1)
1541 per_cpu(rb_need_resched, cpu) = resched;
1542
7a8e76a3
SR
1543 return event;
1544
d769041f 1545 out:
182e9f5f 1546 ftrace_preempt_enable(resched);
7a8e76a3
SR
1547 return NULL;
1548}
c4f50183 1549EXPORT_SYMBOL_GPL(ring_buffer_lock_reserve);
7a8e76a3
SR
1550
1551static void rb_commit(struct ring_buffer_per_cpu *cpu_buffer,
1552 struct ring_buffer_event *event)
1553{
7a8e76a3 1554 cpu_buffer->entries++;
bf41a158
SR
1555
1556 /* Only process further if we own the commit */
1557 if (!rb_is_commit(cpu_buffer, event))
1558 return;
1559
1560 cpu_buffer->write_stamp += event->time_delta;
1561
1562 rb_set_commit_to_write(cpu_buffer);
7a8e76a3
SR
1563}
1564
1565/**
1566 * ring_buffer_unlock_commit - commit a reserved
1567 * @buffer: The buffer to commit to
1568 * @event: The event pointer to commit.
7a8e76a3
SR
1569 *
1570 * This commits the data to the ring buffer, and releases any locks held.
1571 *
1572 * Must be paired with ring_buffer_lock_reserve.
1573 */
1574int ring_buffer_unlock_commit(struct ring_buffer *buffer,
0a987751 1575 struct ring_buffer_event *event)
7a8e76a3
SR
1576{
1577 struct ring_buffer_per_cpu *cpu_buffer;
1578 int cpu = raw_smp_processor_id();
1579
1580 cpu_buffer = buffer->buffers[cpu];
1581
7a8e76a3
SR
1582 rb_commit(cpu_buffer, event);
1583
bf41a158
SR
1584 /*
1585 * Only the last preempt count needs to restore preemption.
1586 */
182e9f5f
SR
1587 if (preempt_count() == 1)
1588 ftrace_preempt_enable(per_cpu(rb_need_resched, cpu));
1589 else
bf41a158 1590 preempt_enable_no_resched_notrace();
7a8e76a3
SR
1591
1592 return 0;
1593}
c4f50183 1594EXPORT_SYMBOL_GPL(ring_buffer_unlock_commit);
7a8e76a3 1595
fa1b47dd
SR
1596/**
1597 * ring_buffer_event_discard - discard any event in the ring buffer
1598 * @event: the event to discard
1599 *
1600 * Sometimes a event that is in the ring buffer needs to be ignored.
1601 * This function lets the user discard an event in the ring buffer
1602 * and then that event will not be read later.
1603 *
1604 * Note, it is up to the user to be careful with this, and protect
1605 * against races. If the user discards an event that has been consumed
1606 * it is possible that it could corrupt the ring buffer.
1607 */
1608void ring_buffer_event_discard(struct ring_buffer_event *event)
1609{
1610 event->type = RINGBUF_TYPE_PADDING;
1611 /* time delta must be non zero */
1612 if (!event->time_delta)
1613 event->time_delta = 1;
1614}
1615EXPORT_SYMBOL_GPL(ring_buffer_event_discard);
1616
1617/**
1618 * ring_buffer_commit_discard - discard an event that has not been committed
1619 * @buffer: the ring buffer
1620 * @event: non committed event to discard
1621 *
1622 * This is similar to ring_buffer_event_discard but must only be
1623 * performed on an event that has not been committed yet. The difference
1624 * is that this will also try to free the event from the ring buffer
1625 * if another event has not been added behind it.
1626 *
1627 * If another event has been added behind it, it will set the event
1628 * up as discarded, and perform the commit.
1629 *
1630 * If this function is called, do not call ring_buffer_unlock_commit on
1631 * the event.
1632 */
1633void ring_buffer_discard_commit(struct ring_buffer *buffer,
1634 struct ring_buffer_event *event)
1635{
1636 struct ring_buffer_per_cpu *cpu_buffer;
1637 unsigned long new_index, old_index;
1638 struct buffer_page *bpage;
1639 unsigned long index;
1640 unsigned long addr;
1641 int cpu;
1642
1643 /* The event is discarded regardless */
1644 ring_buffer_event_discard(event);
1645
1646 /*
1647 * This must only be called if the event has not been
1648 * committed yet. Thus we can assume that preemption
1649 * is still disabled.
1650 */
1651 RB_WARN_ON(buffer, !preempt_count());
1652
1653 cpu = smp_processor_id();
1654 cpu_buffer = buffer->buffers[cpu];
1655
1656 new_index = rb_event_index(event);
1657 old_index = new_index + rb_event_length(event);
1658 addr = (unsigned long)event;
1659 addr &= PAGE_MASK;
1660
1661 bpage = cpu_buffer->tail_page;
1662
1663 if (bpage == (void *)addr && rb_page_write(bpage) == old_index) {
1664 /*
1665 * This is on the tail page. It is possible that
1666 * a write could come in and move the tail page
1667 * and write to the next page. That is fine
1668 * because we just shorten what is on this page.
1669 */
1670 index = local_cmpxchg(&bpage->write, old_index, new_index);
1671 if (index == old_index)
1672 goto out;
1673 }
1674
1675 /*
1676 * The commit is still visible by the reader, so we
1677 * must increment entries.
1678 */
1679 cpu_buffer->entries++;
1680 out:
1681 /*
1682 * If a write came in and pushed the tail page
1683 * we still need to update the commit pointer
1684 * if we were the commit.
1685 */
1686 if (rb_is_commit(cpu_buffer, event))
1687 rb_set_commit_to_write(cpu_buffer);
1688
1689 /*
1690 * Only the last preempt count needs to restore preemption.
1691 */
1692 if (preempt_count() == 1)
1693 ftrace_preempt_enable(per_cpu(rb_need_resched, cpu));
1694 else
1695 preempt_enable_no_resched_notrace();
1696
1697}
1698EXPORT_SYMBOL_GPL(ring_buffer_discard_commit);
1699
7a8e76a3
SR
1700/**
1701 * ring_buffer_write - write data to the buffer without reserving
1702 * @buffer: The ring buffer to write to.
1703 * @length: The length of the data being written (excluding the event header)
1704 * @data: The data to write to the buffer.
1705 *
1706 * This is like ring_buffer_lock_reserve and ring_buffer_unlock_commit as
1707 * one function. If you already have the data to write to the buffer, it
1708 * may be easier to simply call this function.
1709 *
1710 * Note, like ring_buffer_lock_reserve, the length is the length of the data
1711 * and not the length of the event which would hold the header.
1712 */
1713int ring_buffer_write(struct ring_buffer *buffer,
1714 unsigned long length,
1715 void *data)
1716{
1717 struct ring_buffer_per_cpu *cpu_buffer;
1718 struct ring_buffer_event *event;
bf41a158 1719 unsigned long event_length;
7a8e76a3
SR
1720 void *body;
1721 int ret = -EBUSY;
bf41a158 1722 int cpu, resched;
7a8e76a3 1723
033601a3 1724 if (ring_buffer_flags != RB_BUFFERS_ON)
a3583244
SR
1725 return -EBUSY;
1726
7a8e76a3
SR
1727 if (atomic_read(&buffer->record_disabled))
1728 return -EBUSY;
1729
182e9f5f 1730 resched = ftrace_preempt_disable();
bf41a158 1731
7a8e76a3
SR
1732 cpu = raw_smp_processor_id();
1733
9e01c1b7 1734 if (!cpumask_test_cpu(cpu, buffer->cpumask))
d769041f 1735 goto out;
7a8e76a3
SR
1736
1737 cpu_buffer = buffer->buffers[cpu];
7a8e76a3
SR
1738
1739 if (atomic_read(&cpu_buffer->record_disabled))
1740 goto out;
1741
1742 event_length = rb_calculate_event_length(length);
1743 event = rb_reserve_next_event(cpu_buffer,
1744 RINGBUF_TYPE_DATA, event_length);
1745 if (!event)
1746 goto out;
1747
1748 body = rb_event_data(event);
1749
1750 memcpy(body, data, length);
1751
1752 rb_commit(cpu_buffer, event);
1753
1754 ret = 0;
1755 out:
182e9f5f 1756 ftrace_preempt_enable(resched);
7a8e76a3
SR
1757
1758 return ret;
1759}
c4f50183 1760EXPORT_SYMBOL_GPL(ring_buffer_write);
7a8e76a3 1761
34a148bf 1762static int rb_per_cpu_empty(struct ring_buffer_per_cpu *cpu_buffer)
bf41a158
SR
1763{
1764 struct buffer_page *reader = cpu_buffer->reader_page;
1765 struct buffer_page *head = cpu_buffer->head_page;
1766 struct buffer_page *commit = cpu_buffer->commit_page;
1767
1768 return reader->read == rb_page_commit(reader) &&
1769 (commit == reader ||
1770 (commit == head &&
1771 head->read == rb_page_commit(commit)));
1772}
1773
7a8e76a3
SR
1774/**
1775 * ring_buffer_record_disable - stop all writes into the buffer
1776 * @buffer: The ring buffer to stop writes to.
1777 *
1778 * This prevents all writes to the buffer. Any attempt to write
1779 * to the buffer after this will fail and return NULL.
1780 *
1781 * The caller should call synchronize_sched() after this.
1782 */
1783void ring_buffer_record_disable(struct ring_buffer *buffer)
1784{
1785 atomic_inc(&buffer->record_disabled);
1786}
c4f50183 1787EXPORT_SYMBOL_GPL(ring_buffer_record_disable);
7a8e76a3
SR
1788
1789/**
1790 * ring_buffer_record_enable - enable writes to the buffer
1791 * @buffer: The ring buffer to enable writes
1792 *
1793 * Note, multiple disables will need the same number of enables
1794 * to truely enable the writing (much like preempt_disable).
1795 */
1796void ring_buffer_record_enable(struct ring_buffer *buffer)
1797{
1798 atomic_dec(&buffer->record_disabled);
1799}
c4f50183 1800EXPORT_SYMBOL_GPL(ring_buffer_record_enable);
7a8e76a3
SR
1801
1802/**
1803 * ring_buffer_record_disable_cpu - stop all writes into the cpu_buffer
1804 * @buffer: The ring buffer to stop writes to.
1805 * @cpu: The CPU buffer to stop
1806 *
1807 * This prevents all writes to the buffer. Any attempt to write
1808 * to the buffer after this will fail and return NULL.
1809 *
1810 * The caller should call synchronize_sched() after this.
1811 */
1812void ring_buffer_record_disable_cpu(struct ring_buffer *buffer, int cpu)
1813{
1814 struct ring_buffer_per_cpu *cpu_buffer;
1815
9e01c1b7 1816 if (!cpumask_test_cpu(cpu, buffer->cpumask))
8aabee57 1817 return;
7a8e76a3
SR
1818
1819 cpu_buffer = buffer->buffers[cpu];
1820 atomic_inc(&cpu_buffer->record_disabled);
1821}
c4f50183 1822EXPORT_SYMBOL_GPL(ring_buffer_record_disable_cpu);
7a8e76a3
SR
1823
1824/**
1825 * ring_buffer_record_enable_cpu - enable writes to the buffer
1826 * @buffer: The ring buffer to enable writes
1827 * @cpu: The CPU to enable.
1828 *
1829 * Note, multiple disables will need the same number of enables
1830 * to truely enable the writing (much like preempt_disable).
1831 */
1832void ring_buffer_record_enable_cpu(struct ring_buffer *buffer, int cpu)
1833{
1834 struct ring_buffer_per_cpu *cpu_buffer;
1835
9e01c1b7 1836 if (!cpumask_test_cpu(cpu, buffer->cpumask))
8aabee57 1837 return;
7a8e76a3
SR
1838
1839 cpu_buffer = buffer->buffers[cpu];
1840 atomic_dec(&cpu_buffer->record_disabled);
1841}
c4f50183 1842EXPORT_SYMBOL_GPL(ring_buffer_record_enable_cpu);
7a8e76a3
SR
1843
1844/**
1845 * ring_buffer_entries_cpu - get the number of entries in a cpu buffer
1846 * @buffer: The ring buffer
1847 * @cpu: The per CPU buffer to get the entries from.
1848 */
1849unsigned long ring_buffer_entries_cpu(struct ring_buffer *buffer, int cpu)
1850{
1851 struct ring_buffer_per_cpu *cpu_buffer;
8aabee57 1852 unsigned long ret;
7a8e76a3 1853
9e01c1b7 1854 if (!cpumask_test_cpu(cpu, buffer->cpumask))
8aabee57 1855 return 0;
7a8e76a3
SR
1856
1857 cpu_buffer = buffer->buffers[cpu];
554f786e 1858 ret = cpu_buffer->entries;
554f786e
SR
1859
1860 return ret;
7a8e76a3 1861}
c4f50183 1862EXPORT_SYMBOL_GPL(ring_buffer_entries_cpu);
7a8e76a3
SR
1863
1864/**
1865 * ring_buffer_overrun_cpu - get the number of overruns in a cpu_buffer
1866 * @buffer: The ring buffer
1867 * @cpu: The per CPU buffer to get the number of overruns from
1868 */
1869unsigned long ring_buffer_overrun_cpu(struct ring_buffer *buffer, int cpu)
1870{
1871 struct ring_buffer_per_cpu *cpu_buffer;
8aabee57 1872 unsigned long ret;
7a8e76a3 1873
9e01c1b7 1874 if (!cpumask_test_cpu(cpu, buffer->cpumask))
8aabee57 1875 return 0;
7a8e76a3
SR
1876
1877 cpu_buffer = buffer->buffers[cpu];
554f786e 1878 ret = cpu_buffer->overrun;
554f786e
SR
1879
1880 return ret;
7a8e76a3 1881}
c4f50183 1882EXPORT_SYMBOL_GPL(ring_buffer_overrun_cpu);
7a8e76a3
SR
1883
1884/**
1885 * ring_buffer_entries - get the number of entries in a buffer
1886 * @buffer: The ring buffer
1887 *
1888 * Returns the total number of entries in the ring buffer
1889 * (all CPU entries)
1890 */
1891unsigned long ring_buffer_entries(struct ring_buffer *buffer)
1892{
1893 struct ring_buffer_per_cpu *cpu_buffer;
1894 unsigned long entries = 0;
1895 int cpu;
1896
1897 /* if you care about this being correct, lock the buffer */
1898 for_each_buffer_cpu(buffer, cpu) {
1899 cpu_buffer = buffer->buffers[cpu];
1900 entries += cpu_buffer->entries;
1901 }
1902
1903 return entries;
1904}
c4f50183 1905EXPORT_SYMBOL_GPL(ring_buffer_entries);
7a8e76a3
SR
1906
1907/**
1908 * ring_buffer_overrun_cpu - get the number of overruns in buffer
1909 * @buffer: The ring buffer
1910 *
1911 * Returns the total number of overruns in the ring buffer
1912 * (all CPU entries)
1913 */
1914unsigned long ring_buffer_overruns(struct ring_buffer *buffer)
1915{
1916 struct ring_buffer_per_cpu *cpu_buffer;
1917 unsigned long overruns = 0;
1918 int cpu;
1919
1920 /* if you care about this being correct, lock the buffer */
1921 for_each_buffer_cpu(buffer, cpu) {
1922 cpu_buffer = buffer->buffers[cpu];
1923 overruns += cpu_buffer->overrun;
1924 }
1925
1926 return overruns;
1927}
c4f50183 1928EXPORT_SYMBOL_GPL(ring_buffer_overruns);
7a8e76a3 1929
642edba5 1930static void rb_iter_reset(struct ring_buffer_iter *iter)
7a8e76a3
SR
1931{
1932 struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
1933
d769041f
SR
1934 /* Iterator usage is expected to have record disabled */
1935 if (list_empty(&cpu_buffer->reader_page->list)) {
1936 iter->head_page = cpu_buffer->head_page;
6f807acd 1937 iter->head = cpu_buffer->head_page->read;
d769041f
SR
1938 } else {
1939 iter->head_page = cpu_buffer->reader_page;
6f807acd 1940 iter->head = cpu_buffer->reader_page->read;
d769041f
SR
1941 }
1942 if (iter->head)
1943 iter->read_stamp = cpu_buffer->read_stamp;
1944 else
abc9b56d 1945 iter->read_stamp = iter->head_page->page->time_stamp;
642edba5 1946}
f83c9d0f 1947
642edba5
SR
1948/**
1949 * ring_buffer_iter_reset - reset an iterator
1950 * @iter: The iterator to reset
1951 *
1952 * Resets the iterator, so that it will start from the beginning
1953 * again.
1954 */
1955void ring_buffer_iter_reset(struct ring_buffer_iter *iter)
1956{
554f786e 1957 struct ring_buffer_per_cpu *cpu_buffer;
642edba5
SR
1958 unsigned long flags;
1959
554f786e
SR
1960 if (!iter)
1961 return;
1962
1963 cpu_buffer = iter->cpu_buffer;
1964
642edba5
SR
1965 spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
1966 rb_iter_reset(iter);
f83c9d0f 1967 spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
7a8e76a3 1968}
c4f50183 1969EXPORT_SYMBOL_GPL(ring_buffer_iter_reset);
7a8e76a3
SR
1970
1971/**
1972 * ring_buffer_iter_empty - check if an iterator has no more to read
1973 * @iter: The iterator to check
1974 */
1975int ring_buffer_iter_empty(struct ring_buffer_iter *iter)
1976{
1977 struct ring_buffer_per_cpu *cpu_buffer;
1978
1979 cpu_buffer = iter->cpu_buffer;
1980
bf41a158
SR
1981 return iter->head_page == cpu_buffer->commit_page &&
1982 iter->head == rb_commit_index(cpu_buffer);
7a8e76a3 1983}
c4f50183 1984EXPORT_SYMBOL_GPL(ring_buffer_iter_empty);
7a8e76a3
SR
1985
1986static void
1987rb_update_read_stamp(struct ring_buffer_per_cpu *cpu_buffer,
1988 struct ring_buffer_event *event)
1989{
1990 u64 delta;
1991
1992 switch (event->type) {
1993 case RINGBUF_TYPE_PADDING:
1994 return;
1995
1996 case RINGBUF_TYPE_TIME_EXTEND:
1997 delta = event->array[0];
1998 delta <<= TS_SHIFT;
1999 delta += event->time_delta;
2000 cpu_buffer->read_stamp += delta;
2001 return;
2002
2003 case RINGBUF_TYPE_TIME_STAMP:
2004 /* FIXME: not implemented */
2005 return;
2006
2007 case RINGBUF_TYPE_DATA:
2008 cpu_buffer->read_stamp += event->time_delta;
2009 return;
2010
2011 default:
2012 BUG();
2013 }
2014 return;
2015}
2016
2017static void
2018rb_update_iter_read_stamp(struct ring_buffer_iter *iter,
2019 struct ring_buffer_event *event)
2020{
2021 u64 delta;
2022
2023 switch (event->type) {
2024 case RINGBUF_TYPE_PADDING:
2025 return;
2026
2027 case RINGBUF_TYPE_TIME_EXTEND:
2028 delta = event->array[0];
2029 delta <<= TS_SHIFT;
2030 delta += event->time_delta;
2031 iter->read_stamp += delta;
2032 return;
2033
2034 case RINGBUF_TYPE_TIME_STAMP:
2035 /* FIXME: not implemented */
2036 return;
2037
2038 case RINGBUF_TYPE_DATA:
2039 iter->read_stamp += event->time_delta;
2040 return;
2041
2042 default:
2043 BUG();
2044 }
2045 return;
2046}
2047
d769041f
SR
2048static struct buffer_page *
2049rb_get_reader_page(struct ring_buffer_per_cpu *cpu_buffer)
7a8e76a3 2050{
d769041f
SR
2051 struct buffer_page *reader = NULL;
2052 unsigned long flags;
818e3dd3 2053 int nr_loops = 0;
d769041f 2054
3e03fb7f
SR
2055 local_irq_save(flags);
2056 __raw_spin_lock(&cpu_buffer->lock);
d769041f
SR
2057
2058 again:
818e3dd3
SR
2059 /*
2060 * This should normally only loop twice. But because the
2061 * start of the reader inserts an empty page, it causes
2062 * a case where we will loop three times. There should be no
2063 * reason to loop four times (that I know of).
2064 */
3e89c7bb 2065 if (RB_WARN_ON(cpu_buffer, ++nr_loops > 3)) {
818e3dd3
SR
2066 reader = NULL;
2067 goto out;
2068 }
2069
d769041f
SR
2070 reader = cpu_buffer->reader_page;
2071
2072 /* If there's more to read, return this page */
bf41a158 2073 if (cpu_buffer->reader_page->read < rb_page_size(reader))
d769041f
SR
2074 goto out;
2075
2076 /* Never should we have an index greater than the size */
3e89c7bb
SR
2077 if (RB_WARN_ON(cpu_buffer,
2078 cpu_buffer->reader_page->read > rb_page_size(reader)))
2079 goto out;
d769041f
SR
2080
2081 /* check if we caught up to the tail */
2082 reader = NULL;
bf41a158 2083 if (cpu_buffer->commit_page == cpu_buffer->reader_page)
d769041f 2084 goto out;
7a8e76a3
SR
2085
2086 /*
d769041f
SR
2087 * Splice the empty reader page into the list around the head.
2088 * Reset the reader page to size zero.
7a8e76a3 2089 */
7a8e76a3 2090
d769041f
SR
2091 reader = cpu_buffer->head_page;
2092 cpu_buffer->reader_page->list.next = reader->list.next;
2093 cpu_buffer->reader_page->list.prev = reader->list.prev;
bf41a158
SR
2094
2095 local_set(&cpu_buffer->reader_page->write, 0);
abc9b56d 2096 local_set(&cpu_buffer->reader_page->page->commit, 0);
7a8e76a3 2097
d769041f
SR
2098 /* Make the reader page now replace the head */
2099 reader->list.prev->next = &cpu_buffer->reader_page->list;
2100 reader->list.next->prev = &cpu_buffer->reader_page->list;
7a8e76a3
SR
2101
2102 /*
d769041f
SR
2103 * If the tail is on the reader, then we must set the head
2104 * to the inserted page, otherwise we set it one before.
7a8e76a3 2105 */
d769041f 2106 cpu_buffer->head_page = cpu_buffer->reader_page;
7a8e76a3 2107
bf41a158 2108 if (cpu_buffer->commit_page != reader)
d769041f
SR
2109 rb_inc_page(cpu_buffer, &cpu_buffer->head_page);
2110
2111 /* Finally update the reader page to the new head */
2112 cpu_buffer->reader_page = reader;
2113 rb_reset_reader_page(cpu_buffer);
2114
2115 goto again;
2116
2117 out:
3e03fb7f
SR
2118 __raw_spin_unlock(&cpu_buffer->lock);
2119 local_irq_restore(flags);
d769041f
SR
2120
2121 return reader;
2122}
2123
2124static void rb_advance_reader(struct ring_buffer_per_cpu *cpu_buffer)
2125{
2126 struct ring_buffer_event *event;
2127 struct buffer_page *reader;
2128 unsigned length;
2129
2130 reader = rb_get_reader_page(cpu_buffer);
7a8e76a3 2131
d769041f 2132 /* This function should not be called when buffer is empty */
3e89c7bb
SR
2133 if (RB_WARN_ON(cpu_buffer, !reader))
2134 return;
7a8e76a3 2135
d769041f
SR
2136 event = rb_reader_event(cpu_buffer);
2137
2d622719 2138 if (event->type == RINGBUF_TYPE_DATA || rb_discarded_event(event))
d769041f
SR
2139 cpu_buffer->entries--;
2140
2141 rb_update_read_stamp(cpu_buffer, event);
2142
2143 length = rb_event_length(event);
6f807acd 2144 cpu_buffer->reader_page->read += length;
7a8e76a3
SR
2145}
2146
2147static void rb_advance_iter(struct ring_buffer_iter *iter)
2148{
2149 struct ring_buffer *buffer;
2150 struct ring_buffer_per_cpu *cpu_buffer;
2151 struct ring_buffer_event *event;
2152 unsigned length;
2153
2154 cpu_buffer = iter->cpu_buffer;
2155 buffer = cpu_buffer->buffer;
2156
2157 /*
2158 * Check if we are at the end of the buffer.
2159 */
bf41a158 2160 if (iter->head >= rb_page_size(iter->head_page)) {
3e89c7bb
SR
2161 if (RB_WARN_ON(buffer,
2162 iter->head_page == cpu_buffer->commit_page))
2163 return;
d769041f 2164 rb_inc_iter(iter);
7a8e76a3
SR
2165 return;
2166 }
2167
2168 event = rb_iter_head_event(iter);
2169
2170 length = rb_event_length(event);
2171
2172 /*
2173 * This should not be called to advance the header if we are
2174 * at the tail of the buffer.
2175 */
3e89c7bb 2176 if (RB_WARN_ON(cpu_buffer,
f536aafc 2177 (iter->head_page == cpu_buffer->commit_page) &&
3e89c7bb
SR
2178 (iter->head + length > rb_commit_index(cpu_buffer))))
2179 return;
7a8e76a3
SR
2180
2181 rb_update_iter_read_stamp(iter, event);
2182
2183 iter->head += length;
2184
2185 /* check for end of page padding */
bf41a158
SR
2186 if ((iter->head >= rb_page_size(iter->head_page)) &&
2187 (iter->head_page != cpu_buffer->commit_page))
7a8e76a3
SR
2188 rb_advance_iter(iter);
2189}
2190
f83c9d0f
SR
2191static struct ring_buffer_event *
2192rb_buffer_peek(struct ring_buffer *buffer, int cpu, u64 *ts)
7a8e76a3
SR
2193{
2194 struct ring_buffer_per_cpu *cpu_buffer;
2195 struct ring_buffer_event *event;
d769041f 2196 struct buffer_page *reader;
818e3dd3 2197 int nr_loops = 0;
7a8e76a3 2198
7a8e76a3
SR
2199 cpu_buffer = buffer->buffers[cpu];
2200
2201 again:
818e3dd3
SR
2202 /*
2203 * We repeat when a timestamp is encountered. It is possible
2204 * to get multiple timestamps from an interrupt entering just
2205 * as one timestamp is about to be written. The max times
2206 * that this can happen is the number of nested interrupts we
2207 * can have. Nesting 10 deep of interrupts is clearly
2208 * an anomaly.
2209 */
3e89c7bb 2210 if (RB_WARN_ON(cpu_buffer, ++nr_loops > 10))
818e3dd3 2211 return NULL;
818e3dd3 2212
d769041f
SR
2213 reader = rb_get_reader_page(cpu_buffer);
2214 if (!reader)
7a8e76a3
SR
2215 return NULL;
2216
d769041f 2217 event = rb_reader_event(cpu_buffer);
7a8e76a3
SR
2218
2219 switch (event->type) {
2220 case RINGBUF_TYPE_PADDING:
2d622719
TZ
2221 if (rb_null_event(event))
2222 RB_WARN_ON(cpu_buffer, 1);
2223 /*
2224 * Because the writer could be discarding every
2225 * event it creates (which would probably be bad)
2226 * if we were to go back to "again" then we may never
2227 * catch up, and will trigger the warn on, or lock
2228 * the box. Return the padding, and we will release
2229 * the current locks, and try again.
2230 */
d769041f 2231 rb_advance_reader(cpu_buffer);
2d622719 2232 return event;
7a8e76a3
SR
2233
2234 case RINGBUF_TYPE_TIME_EXTEND:
2235 /* Internal data, OK to advance */
d769041f 2236 rb_advance_reader(cpu_buffer);
7a8e76a3
SR
2237 goto again;
2238
2239 case RINGBUF_TYPE_TIME_STAMP:
2240 /* FIXME: not implemented */
d769041f 2241 rb_advance_reader(cpu_buffer);
7a8e76a3
SR
2242 goto again;
2243
2244 case RINGBUF_TYPE_DATA:
2245 if (ts) {
2246 *ts = cpu_buffer->read_stamp + event->time_delta;
37886f6a
SR
2247 ring_buffer_normalize_time_stamp(buffer,
2248 cpu_buffer->cpu, ts);
7a8e76a3
SR
2249 }
2250 return event;
2251
2252 default:
2253 BUG();
2254 }
2255
2256 return NULL;
2257}
c4f50183 2258EXPORT_SYMBOL_GPL(ring_buffer_peek);
7a8e76a3 2259
f83c9d0f
SR
2260static struct ring_buffer_event *
2261rb_iter_peek(struct ring_buffer_iter *iter, u64 *ts)
7a8e76a3
SR
2262{
2263 struct ring_buffer *buffer;
2264 struct ring_buffer_per_cpu *cpu_buffer;
2265 struct ring_buffer_event *event;
818e3dd3 2266 int nr_loops = 0;
7a8e76a3
SR
2267
2268 if (ring_buffer_iter_empty(iter))
2269 return NULL;
2270
2271 cpu_buffer = iter->cpu_buffer;
2272 buffer = cpu_buffer->buffer;
2273
2274 again:
818e3dd3
SR
2275 /*
2276 * We repeat when a timestamp is encountered. It is possible
2277 * to get multiple timestamps from an interrupt entering just
2278 * as one timestamp is about to be written. The max times
2279 * that this can happen is the number of nested interrupts we
2280 * can have. Nesting 10 deep of interrupts is clearly
2281 * an anomaly.
2282 */
3e89c7bb 2283 if (RB_WARN_ON(cpu_buffer, ++nr_loops > 10))
818e3dd3 2284 return NULL;
818e3dd3 2285
7a8e76a3
SR
2286 if (rb_per_cpu_empty(cpu_buffer))
2287 return NULL;
2288
2289 event = rb_iter_head_event(iter);
2290
2291 switch (event->type) {
2292 case RINGBUF_TYPE_PADDING:
2d622719
TZ
2293 if (rb_null_event(event)) {
2294 rb_inc_iter(iter);
2295 goto again;
2296 }
2297 rb_advance_iter(iter);
2298 return event;
7a8e76a3
SR
2299
2300 case RINGBUF_TYPE_TIME_EXTEND:
2301 /* Internal data, OK to advance */
2302 rb_advance_iter(iter);
2303 goto again;
2304
2305 case RINGBUF_TYPE_TIME_STAMP:
2306 /* FIXME: not implemented */
2307 rb_advance_iter(iter);
2308 goto again;
2309
2310 case RINGBUF_TYPE_DATA:
2311 if (ts) {
2312 *ts = iter->read_stamp + event->time_delta;
37886f6a
SR
2313 ring_buffer_normalize_time_stamp(buffer,
2314 cpu_buffer->cpu, ts);
7a8e76a3
SR
2315 }
2316 return event;
2317
2318 default:
2319 BUG();
2320 }
2321
2322 return NULL;
2323}
c4f50183 2324EXPORT_SYMBOL_GPL(ring_buffer_iter_peek);
7a8e76a3 2325
f83c9d0f
SR
2326/**
2327 * ring_buffer_peek - peek at the next event to be read
2328 * @buffer: The ring buffer to read
2329 * @cpu: The cpu to peak at
2330 * @ts: The timestamp counter of this event.
2331 *
2332 * This will return the event that will be read next, but does
2333 * not consume the data.
2334 */
2335struct ring_buffer_event *
2336ring_buffer_peek(struct ring_buffer *buffer, int cpu, u64 *ts)
2337{
2338 struct ring_buffer_per_cpu *cpu_buffer = buffer->buffers[cpu];
8aabee57 2339 struct ring_buffer_event *event;
f83c9d0f
SR
2340 unsigned long flags;
2341
554f786e 2342 if (!cpumask_test_cpu(cpu, buffer->cpumask))
8aabee57 2343 return NULL;
554f786e 2344
2d622719 2345 again:
f83c9d0f
SR
2346 spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
2347 event = rb_buffer_peek(buffer, cpu, ts);
2348 spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
2349
2d622719
TZ
2350 if (event && event->type == RINGBUF_TYPE_PADDING) {
2351 cpu_relax();
2352 goto again;
2353 }
2354
f83c9d0f
SR
2355 return event;
2356}
2357
2358/**
2359 * ring_buffer_iter_peek - peek at the next event to be read
2360 * @iter: The ring buffer iterator
2361 * @ts: The timestamp counter of this event.
2362 *
2363 * This will return the event that will be read next, but does
2364 * not increment the iterator.
2365 */
2366struct ring_buffer_event *
2367ring_buffer_iter_peek(struct ring_buffer_iter *iter, u64 *ts)
2368{
2369 struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
2370 struct ring_buffer_event *event;
2371 unsigned long flags;
2372
2d622719 2373 again:
f83c9d0f
SR
2374 spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
2375 event = rb_iter_peek(iter, ts);
2376 spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
2377
2d622719
TZ
2378 if (event && event->type == RINGBUF_TYPE_PADDING) {
2379 cpu_relax();
2380 goto again;
2381 }
2382
f83c9d0f
SR
2383 return event;
2384}
2385
7a8e76a3
SR
2386/**
2387 * ring_buffer_consume - return an event and consume it
2388 * @buffer: The ring buffer to get the next event from
2389 *
2390 * Returns the next event in the ring buffer, and that event is consumed.
2391 * Meaning, that sequential reads will keep returning a different event,
2392 * and eventually empty the ring buffer if the producer is slower.
2393 */
2394struct ring_buffer_event *
2395ring_buffer_consume(struct ring_buffer *buffer, int cpu, u64 *ts)
2396{
554f786e
SR
2397 struct ring_buffer_per_cpu *cpu_buffer;
2398 struct ring_buffer_event *event = NULL;
f83c9d0f 2399 unsigned long flags;
7a8e76a3 2400
2d622719 2401 again:
554f786e
SR
2402 /* might be called in atomic */
2403 preempt_disable();
2404
9e01c1b7 2405 if (!cpumask_test_cpu(cpu, buffer->cpumask))
554f786e 2406 goto out;
7a8e76a3 2407
554f786e 2408 cpu_buffer = buffer->buffers[cpu];
f83c9d0f
SR
2409 spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
2410
2411 event = rb_buffer_peek(buffer, cpu, ts);
7a8e76a3 2412 if (!event)
554f786e 2413 goto out_unlock;
7a8e76a3 2414
d769041f 2415 rb_advance_reader(cpu_buffer);
7a8e76a3 2416
554f786e 2417 out_unlock:
f83c9d0f
SR
2418 spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
2419
554f786e
SR
2420 out:
2421 preempt_enable();
2422
2d622719
TZ
2423 if (event && event->type == RINGBUF_TYPE_PADDING) {
2424 cpu_relax();
2425 goto again;
2426 }
2427
7a8e76a3
SR
2428 return event;
2429}
c4f50183 2430EXPORT_SYMBOL_GPL(ring_buffer_consume);
7a8e76a3
SR
2431
2432/**
2433 * ring_buffer_read_start - start a non consuming read of the buffer
2434 * @buffer: The ring buffer to read from
2435 * @cpu: The cpu buffer to iterate over
2436 *
2437 * This starts up an iteration through the buffer. It also disables
2438 * the recording to the buffer until the reading is finished.
2439 * This prevents the reading from being corrupted. This is not
2440 * a consuming read, so a producer is not expected.
2441 *
2442 * Must be paired with ring_buffer_finish.
2443 */
2444struct ring_buffer_iter *
2445ring_buffer_read_start(struct ring_buffer *buffer, int cpu)
2446{
2447 struct ring_buffer_per_cpu *cpu_buffer;
8aabee57 2448 struct ring_buffer_iter *iter;
d769041f 2449 unsigned long flags;
7a8e76a3 2450
9e01c1b7 2451 if (!cpumask_test_cpu(cpu, buffer->cpumask))
8aabee57 2452 return NULL;
7a8e76a3
SR
2453
2454 iter = kmalloc(sizeof(*iter), GFP_KERNEL);
2455 if (!iter)
8aabee57 2456 return NULL;
7a8e76a3
SR
2457
2458 cpu_buffer = buffer->buffers[cpu];
2459
2460 iter->cpu_buffer = cpu_buffer;
2461
2462 atomic_inc(&cpu_buffer->record_disabled);
2463 synchronize_sched();
2464
f83c9d0f 2465 spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
3e03fb7f 2466 __raw_spin_lock(&cpu_buffer->lock);
642edba5 2467 rb_iter_reset(iter);
3e03fb7f 2468 __raw_spin_unlock(&cpu_buffer->lock);
f83c9d0f 2469 spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
7a8e76a3
SR
2470
2471 return iter;
2472}
c4f50183 2473EXPORT_SYMBOL_GPL(ring_buffer_read_start);
7a8e76a3
SR
2474
2475/**
2476 * ring_buffer_finish - finish reading the iterator of the buffer
2477 * @iter: The iterator retrieved by ring_buffer_start
2478 *
2479 * This re-enables the recording to the buffer, and frees the
2480 * iterator.
2481 */
2482void
2483ring_buffer_read_finish(struct ring_buffer_iter *iter)
2484{
2485 struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
2486
2487 atomic_dec(&cpu_buffer->record_disabled);
2488 kfree(iter);
2489}
c4f50183 2490EXPORT_SYMBOL_GPL(ring_buffer_read_finish);
7a8e76a3
SR
2491
2492/**
2493 * ring_buffer_read - read the next item in the ring buffer by the iterator
2494 * @iter: The ring buffer iterator
2495 * @ts: The time stamp of the event read.
2496 *
2497 * This reads the next event in the ring buffer and increments the iterator.
2498 */
2499struct ring_buffer_event *
2500ring_buffer_read(struct ring_buffer_iter *iter, u64 *ts)
2501{
2502 struct ring_buffer_event *event;
f83c9d0f
SR
2503 struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
2504 unsigned long flags;
7a8e76a3 2505
2d622719 2506 again:
f83c9d0f
SR
2507 spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
2508 event = rb_iter_peek(iter, ts);
7a8e76a3 2509 if (!event)
f83c9d0f 2510 goto out;
7a8e76a3
SR
2511
2512 rb_advance_iter(iter);
f83c9d0f
SR
2513 out:
2514 spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
7a8e76a3 2515
2d622719
TZ
2516 if (event && event->type == RINGBUF_TYPE_PADDING) {
2517 cpu_relax();
2518 goto again;
2519 }
2520
7a8e76a3
SR
2521 return event;
2522}
c4f50183 2523EXPORT_SYMBOL_GPL(ring_buffer_read);
7a8e76a3
SR
2524
2525/**
2526 * ring_buffer_size - return the size of the ring buffer (in bytes)
2527 * @buffer: The ring buffer.
2528 */
2529unsigned long ring_buffer_size(struct ring_buffer *buffer)
2530{
2531 return BUF_PAGE_SIZE * buffer->pages;
2532}
c4f50183 2533EXPORT_SYMBOL_GPL(ring_buffer_size);
7a8e76a3
SR
2534
2535static void
2536rb_reset_cpu(struct ring_buffer_per_cpu *cpu_buffer)
2537{
2538 cpu_buffer->head_page
2539 = list_entry(cpu_buffer->pages.next, struct buffer_page, list);
bf41a158 2540 local_set(&cpu_buffer->head_page->write, 0);
abc9b56d 2541 local_set(&cpu_buffer->head_page->page->commit, 0);
d769041f 2542
6f807acd 2543 cpu_buffer->head_page->read = 0;
bf41a158
SR
2544
2545 cpu_buffer->tail_page = cpu_buffer->head_page;
2546 cpu_buffer->commit_page = cpu_buffer->head_page;
2547
2548 INIT_LIST_HEAD(&cpu_buffer->reader_page->list);
2549 local_set(&cpu_buffer->reader_page->write, 0);
abc9b56d 2550 local_set(&cpu_buffer->reader_page->page->commit, 0);
6f807acd 2551 cpu_buffer->reader_page->read = 0;
7a8e76a3 2552
7a8e76a3
SR
2553 cpu_buffer->overrun = 0;
2554 cpu_buffer->entries = 0;
69507c06
SR
2555
2556 cpu_buffer->write_stamp = 0;
2557 cpu_buffer->read_stamp = 0;
7a8e76a3
SR
2558}
2559
2560/**
2561 * ring_buffer_reset_cpu - reset a ring buffer per CPU buffer
2562 * @buffer: The ring buffer to reset a per cpu buffer of
2563 * @cpu: The CPU buffer to be reset
2564 */
2565void ring_buffer_reset_cpu(struct ring_buffer *buffer, int cpu)
2566{
2567 struct ring_buffer_per_cpu *cpu_buffer = buffer->buffers[cpu];
2568 unsigned long flags;
2569
9e01c1b7 2570 if (!cpumask_test_cpu(cpu, buffer->cpumask))
8aabee57 2571 return;
7a8e76a3 2572
f83c9d0f
SR
2573 spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
2574
3e03fb7f 2575 __raw_spin_lock(&cpu_buffer->lock);
7a8e76a3
SR
2576
2577 rb_reset_cpu(cpu_buffer);
2578
3e03fb7f 2579 __raw_spin_unlock(&cpu_buffer->lock);
f83c9d0f
SR
2580
2581 spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
7a8e76a3 2582}
c4f50183 2583EXPORT_SYMBOL_GPL(ring_buffer_reset_cpu);
7a8e76a3
SR
2584
2585/**
2586 * ring_buffer_reset - reset a ring buffer
2587 * @buffer: The ring buffer to reset all cpu buffers
2588 */
2589void ring_buffer_reset(struct ring_buffer *buffer)
2590{
7a8e76a3
SR
2591 int cpu;
2592
7a8e76a3 2593 for_each_buffer_cpu(buffer, cpu)
d769041f 2594 ring_buffer_reset_cpu(buffer, cpu);
7a8e76a3 2595}
c4f50183 2596EXPORT_SYMBOL_GPL(ring_buffer_reset);
7a8e76a3
SR
2597
2598/**
2599 * rind_buffer_empty - is the ring buffer empty?
2600 * @buffer: The ring buffer to test
2601 */
2602int ring_buffer_empty(struct ring_buffer *buffer)
2603{
2604 struct ring_buffer_per_cpu *cpu_buffer;
2605 int cpu;
2606
2607 /* yes this is racy, but if you don't like the race, lock the buffer */
2608 for_each_buffer_cpu(buffer, cpu) {
2609 cpu_buffer = buffer->buffers[cpu];
2610 if (!rb_per_cpu_empty(cpu_buffer))
2611 return 0;
2612 }
554f786e 2613
7a8e76a3
SR
2614 return 1;
2615}
c4f50183 2616EXPORT_SYMBOL_GPL(ring_buffer_empty);
7a8e76a3
SR
2617
2618/**
2619 * ring_buffer_empty_cpu - is a cpu buffer of a ring buffer empty?
2620 * @buffer: The ring buffer
2621 * @cpu: The CPU buffer to test
2622 */
2623int ring_buffer_empty_cpu(struct ring_buffer *buffer, int cpu)
2624{
2625 struct ring_buffer_per_cpu *cpu_buffer;
8aabee57 2626 int ret;
7a8e76a3 2627
9e01c1b7 2628 if (!cpumask_test_cpu(cpu, buffer->cpumask))
8aabee57 2629 return 1;
7a8e76a3
SR
2630
2631 cpu_buffer = buffer->buffers[cpu];
554f786e
SR
2632 ret = rb_per_cpu_empty(cpu_buffer);
2633
554f786e
SR
2634
2635 return ret;
7a8e76a3 2636}
c4f50183 2637EXPORT_SYMBOL_GPL(ring_buffer_empty_cpu);
7a8e76a3
SR
2638
2639/**
2640 * ring_buffer_swap_cpu - swap a CPU buffer between two ring buffers
2641 * @buffer_a: One buffer to swap with
2642 * @buffer_b: The other buffer to swap with
2643 *
2644 * This function is useful for tracers that want to take a "snapshot"
2645 * of a CPU buffer and has another back up buffer lying around.
2646 * it is expected that the tracer handles the cpu buffer not being
2647 * used at the moment.
2648 */
2649int ring_buffer_swap_cpu(struct ring_buffer *buffer_a,
2650 struct ring_buffer *buffer_b, int cpu)
2651{
2652 struct ring_buffer_per_cpu *cpu_buffer_a;
2653 struct ring_buffer_per_cpu *cpu_buffer_b;
554f786e
SR
2654 int ret = -EINVAL;
2655
9e01c1b7
RR
2656 if (!cpumask_test_cpu(cpu, buffer_a->cpumask) ||
2657 !cpumask_test_cpu(cpu, buffer_b->cpumask))
554f786e 2658 goto out;
7a8e76a3
SR
2659
2660 /* At least make sure the two buffers are somewhat the same */
6d102bc6 2661 if (buffer_a->pages != buffer_b->pages)
554f786e
SR
2662 goto out;
2663
2664 ret = -EAGAIN;
7a8e76a3 2665
97b17efe 2666 if (ring_buffer_flags != RB_BUFFERS_ON)
554f786e 2667 goto out;
97b17efe
SR
2668
2669 if (atomic_read(&buffer_a->record_disabled))
554f786e 2670 goto out;
97b17efe
SR
2671
2672 if (atomic_read(&buffer_b->record_disabled))
554f786e 2673 goto out;
97b17efe 2674
7a8e76a3
SR
2675 cpu_buffer_a = buffer_a->buffers[cpu];
2676 cpu_buffer_b = buffer_b->buffers[cpu];
2677
97b17efe 2678 if (atomic_read(&cpu_buffer_a->record_disabled))
554f786e 2679 goto out;
97b17efe
SR
2680
2681 if (atomic_read(&cpu_buffer_b->record_disabled))
554f786e 2682 goto out;
97b17efe 2683
7a8e76a3
SR
2684 /*
2685 * We can't do a synchronize_sched here because this
2686 * function can be called in atomic context.
2687 * Normally this will be called from the same CPU as cpu.
2688 * If not it's up to the caller to protect this.
2689 */
2690 atomic_inc(&cpu_buffer_a->record_disabled);
2691 atomic_inc(&cpu_buffer_b->record_disabled);
2692
2693 buffer_a->buffers[cpu] = cpu_buffer_b;
2694 buffer_b->buffers[cpu] = cpu_buffer_a;
2695
2696 cpu_buffer_b->buffer = buffer_a;
2697 cpu_buffer_a->buffer = buffer_b;
2698
2699 atomic_dec(&cpu_buffer_a->record_disabled);
2700 atomic_dec(&cpu_buffer_b->record_disabled);
2701
554f786e
SR
2702 ret = 0;
2703out:
554f786e 2704 return ret;
7a8e76a3 2705}
c4f50183 2706EXPORT_SYMBOL_GPL(ring_buffer_swap_cpu);
7a8e76a3 2707
8789a9e7 2708static void rb_remove_entries(struct ring_buffer_per_cpu *cpu_buffer,
667d2412
LJ
2709 struct buffer_data_page *bpage,
2710 unsigned int offset)
8789a9e7
SR
2711{
2712 struct ring_buffer_event *event;
2713 unsigned long head;
2714
2715 __raw_spin_lock(&cpu_buffer->lock);
667d2412 2716 for (head = offset; head < local_read(&bpage->commit);
8789a9e7
SR
2717 head += rb_event_length(event)) {
2718
044fa782 2719 event = __rb_data_page_index(bpage, head);
8789a9e7
SR
2720 if (RB_WARN_ON(cpu_buffer, rb_null_event(event)))
2721 return;
2722 /* Only count data entries */
2723 if (event->type != RINGBUF_TYPE_DATA)
2724 continue;
2725 cpu_buffer->entries--;
2726 }
2727 __raw_spin_unlock(&cpu_buffer->lock);
2728}
2729
2730/**
2731 * ring_buffer_alloc_read_page - allocate a page to read from buffer
2732 * @buffer: the buffer to allocate for.
2733 *
2734 * This function is used in conjunction with ring_buffer_read_page.
2735 * When reading a full page from the ring buffer, these functions
2736 * can be used to speed up the process. The calling function should
2737 * allocate a few pages first with this function. Then when it
2738 * needs to get pages from the ring buffer, it passes the result
2739 * of this function into ring_buffer_read_page, which will swap
2740 * the page that was allocated, with the read page of the buffer.
2741 *
2742 * Returns:
2743 * The page allocated, or NULL on error.
2744 */
2745void *ring_buffer_alloc_read_page(struct ring_buffer *buffer)
2746{
044fa782 2747 struct buffer_data_page *bpage;
ef7a4a16 2748 unsigned long addr;
8789a9e7
SR
2749
2750 addr = __get_free_page(GFP_KERNEL);
2751 if (!addr)
2752 return NULL;
2753
044fa782 2754 bpage = (void *)addr;
8789a9e7 2755
ef7a4a16
SR
2756 rb_init_page(bpage);
2757
044fa782 2758 return bpage;
8789a9e7
SR
2759}
2760
2761/**
2762 * ring_buffer_free_read_page - free an allocated read page
2763 * @buffer: the buffer the page was allocate for
2764 * @data: the page to free
2765 *
2766 * Free a page allocated from ring_buffer_alloc_read_page.
2767 */
2768void ring_buffer_free_read_page(struct ring_buffer *buffer, void *data)
2769{
2770 free_page((unsigned long)data);
2771}
2772
2773/**
2774 * ring_buffer_read_page - extract a page from the ring buffer
2775 * @buffer: buffer to extract from
2776 * @data_page: the page to use allocated from ring_buffer_alloc_read_page
ef7a4a16 2777 * @len: amount to extract
8789a9e7
SR
2778 * @cpu: the cpu of the buffer to extract
2779 * @full: should the extraction only happen when the page is full.
2780 *
2781 * This function will pull out a page from the ring buffer and consume it.
2782 * @data_page must be the address of the variable that was returned
2783 * from ring_buffer_alloc_read_page. This is because the page might be used
2784 * to swap with a page in the ring buffer.
2785 *
2786 * for example:
b85fa01e 2787 * rpage = ring_buffer_alloc_read_page(buffer);
8789a9e7
SR
2788 * if (!rpage)
2789 * return error;
ef7a4a16 2790 * ret = ring_buffer_read_page(buffer, &rpage, len, cpu, 0);
667d2412
LJ
2791 * if (ret >= 0)
2792 * process_page(rpage, ret);
8789a9e7
SR
2793 *
2794 * When @full is set, the function will not return true unless
2795 * the writer is off the reader page.
2796 *
2797 * Note: it is up to the calling functions to handle sleeps and wakeups.
2798 * The ring buffer can be used anywhere in the kernel and can not
2799 * blindly call wake_up. The layer that uses the ring buffer must be
2800 * responsible for that.
2801 *
2802 * Returns:
667d2412
LJ
2803 * >=0 if data has been transferred, returns the offset of consumed data.
2804 * <0 if no data has been transferred.
8789a9e7
SR
2805 */
2806int ring_buffer_read_page(struct ring_buffer *buffer,
ef7a4a16 2807 void **data_page, size_t len, int cpu, int full)
8789a9e7
SR
2808{
2809 struct ring_buffer_per_cpu *cpu_buffer = buffer->buffers[cpu];
2810 struct ring_buffer_event *event;
044fa782 2811 struct buffer_data_page *bpage;
ef7a4a16 2812 struct buffer_page *reader;
8789a9e7 2813 unsigned long flags;
ef7a4a16 2814 unsigned int commit;
667d2412 2815 unsigned int read;
4f3640f8 2816 u64 save_timestamp;
667d2412 2817 int ret = -1;
8789a9e7 2818
554f786e
SR
2819 if (!cpumask_test_cpu(cpu, buffer->cpumask))
2820 goto out;
2821
474d32b6
SR
2822 /*
2823 * If len is not big enough to hold the page header, then
2824 * we can not copy anything.
2825 */
2826 if (len <= BUF_PAGE_HDR_SIZE)
554f786e 2827 goto out;
474d32b6
SR
2828
2829 len -= BUF_PAGE_HDR_SIZE;
2830
8789a9e7 2831 if (!data_page)
554f786e 2832 goto out;
8789a9e7 2833
044fa782
SR
2834 bpage = *data_page;
2835 if (!bpage)
554f786e 2836 goto out;
8789a9e7
SR
2837
2838 spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
2839
ef7a4a16
SR
2840 reader = rb_get_reader_page(cpu_buffer);
2841 if (!reader)
554f786e 2842 goto out_unlock;
8789a9e7 2843
ef7a4a16
SR
2844 event = rb_reader_event(cpu_buffer);
2845
2846 read = reader->read;
2847 commit = rb_page_commit(reader);
667d2412 2848
8789a9e7 2849 /*
474d32b6
SR
2850 * If this page has been partially read or
2851 * if len is not big enough to read the rest of the page or
2852 * a writer is still on the page, then
2853 * we must copy the data from the page to the buffer.
2854 * Otherwise, we can simply swap the page with the one passed in.
8789a9e7 2855 */
474d32b6 2856 if (read || (len < (commit - read)) ||
ef7a4a16 2857 cpu_buffer->reader_page == cpu_buffer->commit_page) {
667d2412 2858 struct buffer_data_page *rpage = cpu_buffer->reader_page->page;
474d32b6
SR
2859 unsigned int rpos = read;
2860 unsigned int pos = 0;
ef7a4a16 2861 unsigned int size;
8789a9e7
SR
2862
2863 if (full)
554f786e 2864 goto out_unlock;
8789a9e7 2865
ef7a4a16
SR
2866 if (len > (commit - read))
2867 len = (commit - read);
2868
2869 size = rb_event_length(event);
2870
2871 if (len < size)
554f786e 2872 goto out_unlock;
ef7a4a16 2873
4f3640f8
SR
2874 /* save the current timestamp, since the user will need it */
2875 save_timestamp = cpu_buffer->read_stamp;
2876
ef7a4a16
SR
2877 /* Need to copy one event at a time */
2878 do {
474d32b6 2879 memcpy(bpage->data + pos, rpage->data + rpos, size);
ef7a4a16
SR
2880
2881 len -= size;
2882
2883 rb_advance_reader(cpu_buffer);
474d32b6
SR
2884 rpos = reader->read;
2885 pos += size;
ef7a4a16
SR
2886
2887 event = rb_reader_event(cpu_buffer);
2888 size = rb_event_length(event);
2889 } while (len > size);
667d2412
LJ
2890
2891 /* update bpage */
ef7a4a16 2892 local_set(&bpage->commit, pos);
4f3640f8 2893 bpage->time_stamp = save_timestamp;
ef7a4a16 2894
474d32b6
SR
2895 /* we copied everything to the beginning */
2896 read = 0;
8789a9e7
SR
2897 } else {
2898 /* swap the pages */
044fa782 2899 rb_init_page(bpage);
ef7a4a16
SR
2900 bpage = reader->page;
2901 reader->page = *data_page;
2902 local_set(&reader->write, 0);
2903 reader->read = 0;
044fa782 2904 *data_page = bpage;
ef7a4a16
SR
2905
2906 /* update the entry counter */
2907 rb_remove_entries(cpu_buffer, bpage, read);
8789a9e7 2908 }
667d2412 2909 ret = read;
8789a9e7 2910
554f786e 2911 out_unlock:
8789a9e7
SR
2912 spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
2913
554f786e 2914 out:
8789a9e7
SR
2915 return ret;
2916}
2917
a3583244
SR
2918static ssize_t
2919rb_simple_read(struct file *filp, char __user *ubuf,
2920 size_t cnt, loff_t *ppos)
2921{
5e39841c 2922 unsigned long *p = filp->private_data;
a3583244
SR
2923 char buf[64];
2924 int r;
2925
033601a3
SR
2926 if (test_bit(RB_BUFFERS_DISABLED_BIT, p))
2927 r = sprintf(buf, "permanently disabled\n");
2928 else
2929 r = sprintf(buf, "%d\n", test_bit(RB_BUFFERS_ON_BIT, p));
a3583244
SR
2930
2931 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2932}
2933
2934static ssize_t
2935rb_simple_write(struct file *filp, const char __user *ubuf,
2936 size_t cnt, loff_t *ppos)
2937{
5e39841c 2938 unsigned long *p = filp->private_data;
a3583244 2939 char buf[64];
5e39841c 2940 unsigned long val;
a3583244
SR
2941 int ret;
2942
2943 if (cnt >= sizeof(buf))
2944 return -EINVAL;
2945
2946 if (copy_from_user(&buf, ubuf, cnt))
2947 return -EFAULT;
2948
2949 buf[cnt] = 0;
2950
2951 ret = strict_strtoul(buf, 10, &val);
2952 if (ret < 0)
2953 return ret;
2954
033601a3
SR
2955 if (val)
2956 set_bit(RB_BUFFERS_ON_BIT, p);
2957 else
2958 clear_bit(RB_BUFFERS_ON_BIT, p);
a3583244
SR
2959
2960 (*ppos)++;
2961
2962 return cnt;
2963}
2964
5e2336a0 2965static const struct file_operations rb_simple_fops = {
a3583244
SR
2966 .open = tracing_open_generic,
2967 .read = rb_simple_read,
2968 .write = rb_simple_write,
2969};
2970
2971
2972static __init int rb_init_debugfs(void)
2973{
2974 struct dentry *d_tracer;
a3583244
SR
2975
2976 d_tracer = tracing_init_dentry();
2977
5452af66
FW
2978 trace_create_file("tracing_on", 0644, d_tracer,
2979 &ring_buffer_flags, &rb_simple_fops);
a3583244
SR
2980
2981 return 0;
2982}
2983
2984fs_initcall(rb_init_debugfs);
554f786e 2985
59222efe 2986#ifdef CONFIG_HOTPLUG_CPU
09c9e84d
FW
2987static int rb_cpu_notify(struct notifier_block *self,
2988 unsigned long action, void *hcpu)
554f786e
SR
2989{
2990 struct ring_buffer *buffer =
2991 container_of(self, struct ring_buffer, cpu_notify);
2992 long cpu = (long)hcpu;
2993
2994 switch (action) {
2995 case CPU_UP_PREPARE:
2996 case CPU_UP_PREPARE_FROZEN:
2997 if (cpu_isset(cpu, *buffer->cpumask))
2998 return NOTIFY_OK;
2999
3000 buffer->buffers[cpu] =
3001 rb_allocate_cpu_buffer(buffer, cpu);
3002 if (!buffer->buffers[cpu]) {
3003 WARN(1, "failed to allocate ring buffer on CPU %ld\n",
3004 cpu);
3005 return NOTIFY_OK;
3006 }
3007 smp_wmb();
3008 cpu_set(cpu, *buffer->cpumask);
3009 break;
3010 case CPU_DOWN_PREPARE:
3011 case CPU_DOWN_PREPARE_FROZEN:
3012 /*
3013 * Do nothing.
3014 * If we were to free the buffer, then the user would
3015 * lose any trace that was in the buffer.
3016 */
3017 break;
3018 default:
3019 break;
3020 }
3021 return NOTIFY_OK;
3022}
3023#endif