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