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