]> git.proxmox.com Git - mirror_ubuntu-zesty-kernel.git/blame - kernel/trace/ring_buffer.c
ring-buffer: use generic version of in_nmi
[mirror_ubuntu-zesty-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>
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)
1287 * @flags: a pointer to save the interrupt flags
1288 *
1289 * Returns a reseverd event on the ring buffer to copy directly to.
1290 * The user of this interface will need to get the body to write into
1291 * and can use the ring_buffer_event_data() interface.
1292 *
1293 * The length is the length of the data needed, not the event length
1294 * which also includes the event header.
1295 *
1296 * Must be paired with ring_buffer_unlock_commit, unless NULL is returned.
1297 * If NULL is returned, then nothing has been allocated or locked.
1298 */
1299struct ring_buffer_event *
1300ring_buffer_lock_reserve(struct ring_buffer *buffer,
1301 unsigned long length,
1302 unsigned long *flags)
1303{
1304 struct ring_buffer_per_cpu *cpu_buffer;
1305 struct ring_buffer_event *event;
bf41a158 1306 int cpu, resched;
7a8e76a3 1307
033601a3 1308 if (ring_buffer_flags != RB_BUFFERS_ON)
a3583244
SR
1309 return NULL;
1310
7a8e76a3
SR
1311 if (atomic_read(&buffer->record_disabled))
1312 return NULL;
1313
bf41a158 1314 /* If we are tracing schedule, we don't want to recurse */
182e9f5f 1315 resched = ftrace_preempt_disable();
bf41a158 1316
7a8e76a3
SR
1317 cpu = raw_smp_processor_id();
1318
9e01c1b7 1319 if (!cpumask_test_cpu(cpu, buffer->cpumask))
d769041f 1320 goto out;
7a8e76a3
SR
1321
1322 cpu_buffer = buffer->buffers[cpu];
7a8e76a3
SR
1323
1324 if (atomic_read(&cpu_buffer->record_disabled))
d769041f 1325 goto out;
7a8e76a3
SR
1326
1327 length = rb_calculate_event_length(length);
1328 if (length > BUF_PAGE_SIZE)
bf41a158 1329 goto out;
7a8e76a3
SR
1330
1331 event = rb_reserve_next_event(cpu_buffer, RINGBUF_TYPE_DATA, length);
1332 if (!event)
d769041f 1333 goto out;
7a8e76a3 1334
bf41a158
SR
1335 /*
1336 * Need to store resched state on this cpu.
1337 * Only the first needs to.
1338 */
1339
1340 if (preempt_count() == 1)
1341 per_cpu(rb_need_resched, cpu) = resched;
1342
7a8e76a3
SR
1343 return event;
1344
d769041f 1345 out:
182e9f5f 1346 ftrace_preempt_enable(resched);
7a8e76a3
SR
1347 return NULL;
1348}
c4f50183 1349EXPORT_SYMBOL_GPL(ring_buffer_lock_reserve);
7a8e76a3
SR
1350
1351static void rb_commit(struct ring_buffer_per_cpu *cpu_buffer,
1352 struct ring_buffer_event *event)
1353{
7a8e76a3 1354 cpu_buffer->entries++;
bf41a158
SR
1355
1356 /* Only process further if we own the commit */
1357 if (!rb_is_commit(cpu_buffer, event))
1358 return;
1359
1360 cpu_buffer->write_stamp += event->time_delta;
1361
1362 rb_set_commit_to_write(cpu_buffer);
7a8e76a3
SR
1363}
1364
1365/**
1366 * ring_buffer_unlock_commit - commit a reserved
1367 * @buffer: The buffer to commit to
1368 * @event: The event pointer to commit.
1369 * @flags: the interrupt flags received from ring_buffer_lock_reserve.
1370 *
1371 * This commits the data to the ring buffer, and releases any locks held.
1372 *
1373 * Must be paired with ring_buffer_lock_reserve.
1374 */
1375int ring_buffer_unlock_commit(struct ring_buffer *buffer,
1376 struct ring_buffer_event *event,
1377 unsigned long flags)
1378{
1379 struct ring_buffer_per_cpu *cpu_buffer;
1380 int cpu = raw_smp_processor_id();
1381
1382 cpu_buffer = buffer->buffers[cpu];
1383
7a8e76a3
SR
1384 rb_commit(cpu_buffer, event);
1385
bf41a158
SR
1386 /*
1387 * Only the last preempt count needs to restore preemption.
1388 */
182e9f5f
SR
1389 if (preempt_count() == 1)
1390 ftrace_preempt_enable(per_cpu(rb_need_resched, cpu));
1391 else
bf41a158 1392 preempt_enable_no_resched_notrace();
7a8e76a3
SR
1393
1394 return 0;
1395}
c4f50183 1396EXPORT_SYMBOL_GPL(ring_buffer_unlock_commit);
7a8e76a3
SR
1397
1398/**
1399 * ring_buffer_write - write data to the buffer without reserving
1400 * @buffer: The ring buffer to write to.
1401 * @length: The length of the data being written (excluding the event header)
1402 * @data: The data to write to the buffer.
1403 *
1404 * This is like ring_buffer_lock_reserve and ring_buffer_unlock_commit as
1405 * one function. If you already have the data to write to the buffer, it
1406 * may be easier to simply call this function.
1407 *
1408 * Note, like ring_buffer_lock_reserve, the length is the length of the data
1409 * and not the length of the event which would hold the header.
1410 */
1411int ring_buffer_write(struct ring_buffer *buffer,
1412 unsigned long length,
1413 void *data)
1414{
1415 struct ring_buffer_per_cpu *cpu_buffer;
1416 struct ring_buffer_event *event;
bf41a158 1417 unsigned long event_length;
7a8e76a3
SR
1418 void *body;
1419 int ret = -EBUSY;
bf41a158 1420 int cpu, resched;
7a8e76a3 1421
033601a3 1422 if (ring_buffer_flags != RB_BUFFERS_ON)
a3583244
SR
1423 return -EBUSY;
1424
7a8e76a3
SR
1425 if (atomic_read(&buffer->record_disabled))
1426 return -EBUSY;
1427
182e9f5f 1428 resched = ftrace_preempt_disable();
bf41a158 1429
7a8e76a3
SR
1430 cpu = raw_smp_processor_id();
1431
9e01c1b7 1432 if (!cpumask_test_cpu(cpu, buffer->cpumask))
d769041f 1433 goto out;
7a8e76a3
SR
1434
1435 cpu_buffer = buffer->buffers[cpu];
7a8e76a3
SR
1436
1437 if (atomic_read(&cpu_buffer->record_disabled))
1438 goto out;
1439
1440 event_length = rb_calculate_event_length(length);
1441 event = rb_reserve_next_event(cpu_buffer,
1442 RINGBUF_TYPE_DATA, event_length);
1443 if (!event)
1444 goto out;
1445
1446 body = rb_event_data(event);
1447
1448 memcpy(body, data, length);
1449
1450 rb_commit(cpu_buffer, event);
1451
1452 ret = 0;
1453 out:
182e9f5f 1454 ftrace_preempt_enable(resched);
7a8e76a3
SR
1455
1456 return ret;
1457}
c4f50183 1458EXPORT_SYMBOL_GPL(ring_buffer_write);
7a8e76a3 1459
34a148bf 1460static int rb_per_cpu_empty(struct ring_buffer_per_cpu *cpu_buffer)
bf41a158
SR
1461{
1462 struct buffer_page *reader = cpu_buffer->reader_page;
1463 struct buffer_page *head = cpu_buffer->head_page;
1464 struct buffer_page *commit = cpu_buffer->commit_page;
1465
1466 return reader->read == rb_page_commit(reader) &&
1467 (commit == reader ||
1468 (commit == head &&
1469 head->read == rb_page_commit(commit)));
1470}
1471
7a8e76a3
SR
1472/**
1473 * ring_buffer_record_disable - stop all writes into the buffer
1474 * @buffer: The ring buffer to stop writes to.
1475 *
1476 * This prevents all writes to the buffer. Any attempt to write
1477 * to the buffer after this will fail and return NULL.
1478 *
1479 * The caller should call synchronize_sched() after this.
1480 */
1481void ring_buffer_record_disable(struct ring_buffer *buffer)
1482{
1483 atomic_inc(&buffer->record_disabled);
1484}
c4f50183 1485EXPORT_SYMBOL_GPL(ring_buffer_record_disable);
7a8e76a3
SR
1486
1487/**
1488 * ring_buffer_record_enable - enable writes to the buffer
1489 * @buffer: The ring buffer to enable writes
1490 *
1491 * Note, multiple disables will need the same number of enables
1492 * to truely enable the writing (much like preempt_disable).
1493 */
1494void ring_buffer_record_enable(struct ring_buffer *buffer)
1495{
1496 atomic_dec(&buffer->record_disabled);
1497}
c4f50183 1498EXPORT_SYMBOL_GPL(ring_buffer_record_enable);
7a8e76a3
SR
1499
1500/**
1501 * ring_buffer_record_disable_cpu - stop all writes into the cpu_buffer
1502 * @buffer: The ring buffer to stop writes to.
1503 * @cpu: The CPU buffer to stop
1504 *
1505 * This prevents all writes to the buffer. Any attempt to write
1506 * to the buffer after this will fail and return NULL.
1507 *
1508 * The caller should call synchronize_sched() after this.
1509 */
1510void ring_buffer_record_disable_cpu(struct ring_buffer *buffer, int cpu)
1511{
1512 struct ring_buffer_per_cpu *cpu_buffer;
1513
9e01c1b7 1514 if (!cpumask_test_cpu(cpu, buffer->cpumask))
7a8e76a3
SR
1515 return;
1516
1517 cpu_buffer = buffer->buffers[cpu];
1518 atomic_inc(&cpu_buffer->record_disabled);
1519}
c4f50183 1520EXPORT_SYMBOL_GPL(ring_buffer_record_disable_cpu);
7a8e76a3
SR
1521
1522/**
1523 * ring_buffer_record_enable_cpu - enable writes to the buffer
1524 * @buffer: The ring buffer to enable writes
1525 * @cpu: The CPU to enable.
1526 *
1527 * Note, multiple disables will need the same number of enables
1528 * to truely enable the writing (much like preempt_disable).
1529 */
1530void ring_buffer_record_enable_cpu(struct ring_buffer *buffer, int cpu)
1531{
1532 struct ring_buffer_per_cpu *cpu_buffer;
1533
9e01c1b7 1534 if (!cpumask_test_cpu(cpu, buffer->cpumask))
7a8e76a3
SR
1535 return;
1536
1537 cpu_buffer = buffer->buffers[cpu];
1538 atomic_dec(&cpu_buffer->record_disabled);
1539}
c4f50183 1540EXPORT_SYMBOL_GPL(ring_buffer_record_enable_cpu);
7a8e76a3
SR
1541
1542/**
1543 * ring_buffer_entries_cpu - get the number of entries in a cpu buffer
1544 * @buffer: The ring buffer
1545 * @cpu: The per CPU buffer to get the entries from.
1546 */
1547unsigned long ring_buffer_entries_cpu(struct ring_buffer *buffer, int cpu)
1548{
1549 struct ring_buffer_per_cpu *cpu_buffer;
1550
9e01c1b7 1551 if (!cpumask_test_cpu(cpu, buffer->cpumask))
7a8e76a3
SR
1552 return 0;
1553
1554 cpu_buffer = buffer->buffers[cpu];
1555 return cpu_buffer->entries;
1556}
c4f50183 1557EXPORT_SYMBOL_GPL(ring_buffer_entries_cpu);
7a8e76a3
SR
1558
1559/**
1560 * ring_buffer_overrun_cpu - get the number of overruns in a cpu_buffer
1561 * @buffer: The ring buffer
1562 * @cpu: The per CPU buffer to get the number of overruns from
1563 */
1564unsigned long ring_buffer_overrun_cpu(struct ring_buffer *buffer, int cpu)
1565{
1566 struct ring_buffer_per_cpu *cpu_buffer;
1567
9e01c1b7 1568 if (!cpumask_test_cpu(cpu, buffer->cpumask))
7a8e76a3
SR
1569 return 0;
1570
1571 cpu_buffer = buffer->buffers[cpu];
1572 return cpu_buffer->overrun;
1573}
c4f50183 1574EXPORT_SYMBOL_GPL(ring_buffer_overrun_cpu);
7a8e76a3
SR
1575
1576/**
1577 * ring_buffer_entries - get the number of entries in a buffer
1578 * @buffer: The ring buffer
1579 *
1580 * Returns the total number of entries in the ring buffer
1581 * (all CPU entries)
1582 */
1583unsigned long ring_buffer_entries(struct ring_buffer *buffer)
1584{
1585 struct ring_buffer_per_cpu *cpu_buffer;
1586 unsigned long entries = 0;
1587 int cpu;
1588
1589 /* if you care about this being correct, lock the buffer */
1590 for_each_buffer_cpu(buffer, cpu) {
1591 cpu_buffer = buffer->buffers[cpu];
1592 entries += cpu_buffer->entries;
1593 }
1594
1595 return entries;
1596}
c4f50183 1597EXPORT_SYMBOL_GPL(ring_buffer_entries);
7a8e76a3
SR
1598
1599/**
1600 * ring_buffer_overrun_cpu - get the number of overruns in buffer
1601 * @buffer: The ring buffer
1602 *
1603 * Returns the total number of overruns in the ring buffer
1604 * (all CPU entries)
1605 */
1606unsigned long ring_buffer_overruns(struct ring_buffer *buffer)
1607{
1608 struct ring_buffer_per_cpu *cpu_buffer;
1609 unsigned long overruns = 0;
1610 int cpu;
1611
1612 /* if you care about this being correct, lock the buffer */
1613 for_each_buffer_cpu(buffer, cpu) {
1614 cpu_buffer = buffer->buffers[cpu];
1615 overruns += cpu_buffer->overrun;
1616 }
1617
1618 return overruns;
1619}
c4f50183 1620EXPORT_SYMBOL_GPL(ring_buffer_overruns);
7a8e76a3 1621
642edba5 1622static void rb_iter_reset(struct ring_buffer_iter *iter)
7a8e76a3
SR
1623{
1624 struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
1625
d769041f
SR
1626 /* Iterator usage is expected to have record disabled */
1627 if (list_empty(&cpu_buffer->reader_page->list)) {
1628 iter->head_page = cpu_buffer->head_page;
6f807acd 1629 iter->head = cpu_buffer->head_page->read;
d769041f
SR
1630 } else {
1631 iter->head_page = cpu_buffer->reader_page;
6f807acd 1632 iter->head = cpu_buffer->reader_page->read;
d769041f
SR
1633 }
1634 if (iter->head)
1635 iter->read_stamp = cpu_buffer->read_stamp;
1636 else
abc9b56d 1637 iter->read_stamp = iter->head_page->page->time_stamp;
642edba5 1638}
f83c9d0f 1639
642edba5
SR
1640/**
1641 * ring_buffer_iter_reset - reset an iterator
1642 * @iter: The iterator to reset
1643 *
1644 * Resets the iterator, so that it will start from the beginning
1645 * again.
1646 */
1647void ring_buffer_iter_reset(struct ring_buffer_iter *iter)
1648{
1649 struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
1650 unsigned long flags;
1651
1652 spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
1653 rb_iter_reset(iter);
f83c9d0f 1654 spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
7a8e76a3 1655}
c4f50183 1656EXPORT_SYMBOL_GPL(ring_buffer_iter_reset);
7a8e76a3
SR
1657
1658/**
1659 * ring_buffer_iter_empty - check if an iterator has no more to read
1660 * @iter: The iterator to check
1661 */
1662int ring_buffer_iter_empty(struct ring_buffer_iter *iter)
1663{
1664 struct ring_buffer_per_cpu *cpu_buffer;
1665
1666 cpu_buffer = iter->cpu_buffer;
1667
bf41a158
SR
1668 return iter->head_page == cpu_buffer->commit_page &&
1669 iter->head == rb_commit_index(cpu_buffer);
7a8e76a3 1670}
c4f50183 1671EXPORT_SYMBOL_GPL(ring_buffer_iter_empty);
7a8e76a3
SR
1672
1673static void
1674rb_update_read_stamp(struct ring_buffer_per_cpu *cpu_buffer,
1675 struct ring_buffer_event *event)
1676{
1677 u64 delta;
1678
1679 switch (event->type) {
1680 case RINGBUF_TYPE_PADDING:
1681 return;
1682
1683 case RINGBUF_TYPE_TIME_EXTEND:
1684 delta = event->array[0];
1685 delta <<= TS_SHIFT;
1686 delta += event->time_delta;
1687 cpu_buffer->read_stamp += delta;
1688 return;
1689
1690 case RINGBUF_TYPE_TIME_STAMP:
1691 /* FIXME: not implemented */
1692 return;
1693
1694 case RINGBUF_TYPE_DATA:
1695 cpu_buffer->read_stamp += event->time_delta;
1696 return;
1697
1698 default:
1699 BUG();
1700 }
1701 return;
1702}
1703
1704static void
1705rb_update_iter_read_stamp(struct ring_buffer_iter *iter,
1706 struct ring_buffer_event *event)
1707{
1708 u64 delta;
1709
1710 switch (event->type) {
1711 case RINGBUF_TYPE_PADDING:
1712 return;
1713
1714 case RINGBUF_TYPE_TIME_EXTEND:
1715 delta = event->array[0];
1716 delta <<= TS_SHIFT;
1717 delta += event->time_delta;
1718 iter->read_stamp += delta;
1719 return;
1720
1721 case RINGBUF_TYPE_TIME_STAMP:
1722 /* FIXME: not implemented */
1723 return;
1724
1725 case RINGBUF_TYPE_DATA:
1726 iter->read_stamp += event->time_delta;
1727 return;
1728
1729 default:
1730 BUG();
1731 }
1732 return;
1733}
1734
d769041f
SR
1735static struct buffer_page *
1736rb_get_reader_page(struct ring_buffer_per_cpu *cpu_buffer)
7a8e76a3 1737{
d769041f
SR
1738 struct buffer_page *reader = NULL;
1739 unsigned long flags;
818e3dd3 1740 int nr_loops = 0;
d769041f 1741
3e03fb7f
SR
1742 local_irq_save(flags);
1743 __raw_spin_lock(&cpu_buffer->lock);
d769041f
SR
1744
1745 again:
818e3dd3
SR
1746 /*
1747 * This should normally only loop twice. But because the
1748 * start of the reader inserts an empty page, it causes
1749 * a case where we will loop three times. There should be no
1750 * reason to loop four times (that I know of).
1751 */
3e89c7bb 1752 if (RB_WARN_ON(cpu_buffer, ++nr_loops > 3)) {
818e3dd3
SR
1753 reader = NULL;
1754 goto out;
1755 }
1756
d769041f
SR
1757 reader = cpu_buffer->reader_page;
1758
1759 /* If there's more to read, return this page */
bf41a158 1760 if (cpu_buffer->reader_page->read < rb_page_size(reader))
d769041f
SR
1761 goto out;
1762
1763 /* Never should we have an index greater than the size */
3e89c7bb
SR
1764 if (RB_WARN_ON(cpu_buffer,
1765 cpu_buffer->reader_page->read > rb_page_size(reader)))
1766 goto out;
d769041f
SR
1767
1768 /* check if we caught up to the tail */
1769 reader = NULL;
bf41a158 1770 if (cpu_buffer->commit_page == cpu_buffer->reader_page)
d769041f 1771 goto out;
7a8e76a3
SR
1772
1773 /*
d769041f
SR
1774 * Splice the empty reader page into the list around the head.
1775 * Reset the reader page to size zero.
7a8e76a3 1776 */
7a8e76a3 1777
d769041f
SR
1778 reader = cpu_buffer->head_page;
1779 cpu_buffer->reader_page->list.next = reader->list.next;
1780 cpu_buffer->reader_page->list.prev = reader->list.prev;
bf41a158
SR
1781
1782 local_set(&cpu_buffer->reader_page->write, 0);
abc9b56d 1783 local_set(&cpu_buffer->reader_page->page->commit, 0);
7a8e76a3 1784
d769041f
SR
1785 /* Make the reader page now replace the head */
1786 reader->list.prev->next = &cpu_buffer->reader_page->list;
1787 reader->list.next->prev = &cpu_buffer->reader_page->list;
7a8e76a3
SR
1788
1789 /*
d769041f
SR
1790 * If the tail is on the reader, then we must set the head
1791 * to the inserted page, otherwise we set it one before.
7a8e76a3 1792 */
d769041f 1793 cpu_buffer->head_page = cpu_buffer->reader_page;
7a8e76a3 1794
bf41a158 1795 if (cpu_buffer->commit_page != reader)
d769041f
SR
1796 rb_inc_page(cpu_buffer, &cpu_buffer->head_page);
1797
1798 /* Finally update the reader page to the new head */
1799 cpu_buffer->reader_page = reader;
1800 rb_reset_reader_page(cpu_buffer);
1801
1802 goto again;
1803
1804 out:
3e03fb7f
SR
1805 __raw_spin_unlock(&cpu_buffer->lock);
1806 local_irq_restore(flags);
d769041f
SR
1807
1808 return reader;
1809}
1810
1811static void rb_advance_reader(struct ring_buffer_per_cpu *cpu_buffer)
1812{
1813 struct ring_buffer_event *event;
1814 struct buffer_page *reader;
1815 unsigned length;
1816
1817 reader = rb_get_reader_page(cpu_buffer);
7a8e76a3 1818
d769041f 1819 /* This function should not be called when buffer is empty */
3e89c7bb
SR
1820 if (RB_WARN_ON(cpu_buffer, !reader))
1821 return;
7a8e76a3 1822
d769041f
SR
1823 event = rb_reader_event(cpu_buffer);
1824
1825 if (event->type == RINGBUF_TYPE_DATA)
1826 cpu_buffer->entries--;
1827
1828 rb_update_read_stamp(cpu_buffer, event);
1829
1830 length = rb_event_length(event);
6f807acd 1831 cpu_buffer->reader_page->read += length;
7a8e76a3
SR
1832}
1833
1834static void rb_advance_iter(struct ring_buffer_iter *iter)
1835{
1836 struct ring_buffer *buffer;
1837 struct ring_buffer_per_cpu *cpu_buffer;
1838 struct ring_buffer_event *event;
1839 unsigned length;
1840
1841 cpu_buffer = iter->cpu_buffer;
1842 buffer = cpu_buffer->buffer;
1843
1844 /*
1845 * Check if we are at the end of the buffer.
1846 */
bf41a158 1847 if (iter->head >= rb_page_size(iter->head_page)) {
3e89c7bb
SR
1848 if (RB_WARN_ON(buffer,
1849 iter->head_page == cpu_buffer->commit_page))
1850 return;
d769041f 1851 rb_inc_iter(iter);
7a8e76a3
SR
1852 return;
1853 }
1854
1855 event = rb_iter_head_event(iter);
1856
1857 length = rb_event_length(event);
1858
1859 /*
1860 * This should not be called to advance the header if we are
1861 * at the tail of the buffer.
1862 */
3e89c7bb 1863 if (RB_WARN_ON(cpu_buffer,
f536aafc 1864 (iter->head_page == cpu_buffer->commit_page) &&
3e89c7bb
SR
1865 (iter->head + length > rb_commit_index(cpu_buffer))))
1866 return;
7a8e76a3
SR
1867
1868 rb_update_iter_read_stamp(iter, event);
1869
1870 iter->head += length;
1871
1872 /* check for end of page padding */
bf41a158
SR
1873 if ((iter->head >= rb_page_size(iter->head_page)) &&
1874 (iter->head_page != cpu_buffer->commit_page))
7a8e76a3
SR
1875 rb_advance_iter(iter);
1876}
1877
f83c9d0f
SR
1878static struct ring_buffer_event *
1879rb_buffer_peek(struct ring_buffer *buffer, int cpu, u64 *ts)
7a8e76a3
SR
1880{
1881 struct ring_buffer_per_cpu *cpu_buffer;
1882 struct ring_buffer_event *event;
d769041f 1883 struct buffer_page *reader;
818e3dd3 1884 int nr_loops = 0;
7a8e76a3 1885
9e01c1b7 1886 if (!cpumask_test_cpu(cpu, buffer->cpumask))
7a8e76a3
SR
1887 return NULL;
1888
1889 cpu_buffer = buffer->buffers[cpu];
1890
1891 again:
818e3dd3
SR
1892 /*
1893 * We repeat when a timestamp is encountered. It is possible
1894 * to get multiple timestamps from an interrupt entering just
1895 * as one timestamp is about to be written. The max times
1896 * that this can happen is the number of nested interrupts we
1897 * can have. Nesting 10 deep of interrupts is clearly
1898 * an anomaly.
1899 */
3e89c7bb 1900 if (RB_WARN_ON(cpu_buffer, ++nr_loops > 10))
818e3dd3 1901 return NULL;
818e3dd3 1902
d769041f
SR
1903 reader = rb_get_reader_page(cpu_buffer);
1904 if (!reader)
7a8e76a3
SR
1905 return NULL;
1906
d769041f 1907 event = rb_reader_event(cpu_buffer);
7a8e76a3
SR
1908
1909 switch (event->type) {
1910 case RINGBUF_TYPE_PADDING:
bf41a158 1911 RB_WARN_ON(cpu_buffer, 1);
d769041f
SR
1912 rb_advance_reader(cpu_buffer);
1913 return NULL;
7a8e76a3
SR
1914
1915 case RINGBUF_TYPE_TIME_EXTEND:
1916 /* Internal data, OK to advance */
d769041f 1917 rb_advance_reader(cpu_buffer);
7a8e76a3
SR
1918 goto again;
1919
1920 case RINGBUF_TYPE_TIME_STAMP:
1921 /* FIXME: not implemented */
d769041f 1922 rb_advance_reader(cpu_buffer);
7a8e76a3
SR
1923 goto again;
1924
1925 case RINGBUF_TYPE_DATA:
1926 if (ts) {
1927 *ts = cpu_buffer->read_stamp + event->time_delta;
1928 ring_buffer_normalize_time_stamp(cpu_buffer->cpu, ts);
1929 }
1930 return event;
1931
1932 default:
1933 BUG();
1934 }
1935
1936 return NULL;
1937}
c4f50183 1938EXPORT_SYMBOL_GPL(ring_buffer_peek);
7a8e76a3 1939
f83c9d0f
SR
1940static struct ring_buffer_event *
1941rb_iter_peek(struct ring_buffer_iter *iter, u64 *ts)
7a8e76a3
SR
1942{
1943 struct ring_buffer *buffer;
1944 struct ring_buffer_per_cpu *cpu_buffer;
1945 struct ring_buffer_event *event;
818e3dd3 1946 int nr_loops = 0;
7a8e76a3
SR
1947
1948 if (ring_buffer_iter_empty(iter))
1949 return NULL;
1950
1951 cpu_buffer = iter->cpu_buffer;
1952 buffer = cpu_buffer->buffer;
1953
1954 again:
818e3dd3
SR
1955 /*
1956 * We repeat when a timestamp is encountered. It is possible
1957 * to get multiple timestamps from an interrupt entering just
1958 * as one timestamp is about to be written. The max times
1959 * that this can happen is the number of nested interrupts we
1960 * can have. Nesting 10 deep of interrupts is clearly
1961 * an anomaly.
1962 */
3e89c7bb 1963 if (RB_WARN_ON(cpu_buffer, ++nr_loops > 10))
818e3dd3 1964 return NULL;
818e3dd3 1965
7a8e76a3
SR
1966 if (rb_per_cpu_empty(cpu_buffer))
1967 return NULL;
1968
1969 event = rb_iter_head_event(iter);
1970
1971 switch (event->type) {
1972 case RINGBUF_TYPE_PADDING:
d769041f 1973 rb_inc_iter(iter);
7a8e76a3
SR
1974 goto again;
1975
1976 case RINGBUF_TYPE_TIME_EXTEND:
1977 /* Internal data, OK to advance */
1978 rb_advance_iter(iter);
1979 goto again;
1980
1981 case RINGBUF_TYPE_TIME_STAMP:
1982 /* FIXME: not implemented */
1983 rb_advance_iter(iter);
1984 goto again;
1985
1986 case RINGBUF_TYPE_DATA:
1987 if (ts) {
1988 *ts = iter->read_stamp + event->time_delta;
1989 ring_buffer_normalize_time_stamp(cpu_buffer->cpu, ts);
1990 }
1991 return event;
1992
1993 default:
1994 BUG();
1995 }
1996
1997 return NULL;
1998}
c4f50183 1999EXPORT_SYMBOL_GPL(ring_buffer_iter_peek);
7a8e76a3 2000
f83c9d0f
SR
2001/**
2002 * ring_buffer_peek - peek at the next event to be read
2003 * @buffer: The ring buffer to read
2004 * @cpu: The cpu to peak at
2005 * @ts: The timestamp counter of this event.
2006 *
2007 * This will return the event that will be read next, but does
2008 * not consume the data.
2009 */
2010struct ring_buffer_event *
2011ring_buffer_peek(struct ring_buffer *buffer, int cpu, u64 *ts)
2012{
2013 struct ring_buffer_per_cpu *cpu_buffer = buffer->buffers[cpu];
2014 struct ring_buffer_event *event;
2015 unsigned long flags;
2016
2017 spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
2018 event = rb_buffer_peek(buffer, cpu, ts);
2019 spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
2020
2021 return event;
2022}
2023
2024/**
2025 * ring_buffer_iter_peek - peek at the next event to be read
2026 * @iter: The ring buffer iterator
2027 * @ts: The timestamp counter of this event.
2028 *
2029 * This will return the event that will be read next, but does
2030 * not increment the iterator.
2031 */
2032struct ring_buffer_event *
2033ring_buffer_iter_peek(struct ring_buffer_iter *iter, u64 *ts)
2034{
2035 struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
2036 struct ring_buffer_event *event;
2037 unsigned long flags;
2038
2039 spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
2040 event = rb_iter_peek(iter, ts);
2041 spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
2042
2043 return event;
2044}
2045
7a8e76a3
SR
2046/**
2047 * ring_buffer_consume - return an event and consume it
2048 * @buffer: The ring buffer to get the next event from
2049 *
2050 * Returns the next event in the ring buffer, and that event is consumed.
2051 * Meaning, that sequential reads will keep returning a different event,
2052 * and eventually empty the ring buffer if the producer is slower.
2053 */
2054struct ring_buffer_event *
2055ring_buffer_consume(struct ring_buffer *buffer, int cpu, u64 *ts)
2056{
f83c9d0f 2057 struct ring_buffer_per_cpu *cpu_buffer = buffer->buffers[cpu];
7a8e76a3 2058 struct ring_buffer_event *event;
f83c9d0f 2059 unsigned long flags;
7a8e76a3 2060
9e01c1b7 2061 if (!cpumask_test_cpu(cpu, buffer->cpumask))
7a8e76a3
SR
2062 return NULL;
2063
f83c9d0f
SR
2064 spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
2065
2066 event = rb_buffer_peek(buffer, cpu, ts);
7a8e76a3 2067 if (!event)
f83c9d0f 2068 goto out;
7a8e76a3 2069
d769041f 2070 rb_advance_reader(cpu_buffer);
7a8e76a3 2071
f83c9d0f
SR
2072 out:
2073 spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
2074
7a8e76a3
SR
2075 return event;
2076}
c4f50183 2077EXPORT_SYMBOL_GPL(ring_buffer_consume);
7a8e76a3
SR
2078
2079/**
2080 * ring_buffer_read_start - start a non consuming read of the buffer
2081 * @buffer: The ring buffer to read from
2082 * @cpu: The cpu buffer to iterate over
2083 *
2084 * This starts up an iteration through the buffer. It also disables
2085 * the recording to the buffer until the reading is finished.
2086 * This prevents the reading from being corrupted. This is not
2087 * a consuming read, so a producer is not expected.
2088 *
2089 * Must be paired with ring_buffer_finish.
2090 */
2091struct ring_buffer_iter *
2092ring_buffer_read_start(struct ring_buffer *buffer, int cpu)
2093{
2094 struct ring_buffer_per_cpu *cpu_buffer;
2095 struct ring_buffer_iter *iter;
d769041f 2096 unsigned long flags;
7a8e76a3 2097
9e01c1b7 2098 if (!cpumask_test_cpu(cpu, buffer->cpumask))
7a8e76a3
SR
2099 return NULL;
2100
2101 iter = kmalloc(sizeof(*iter), GFP_KERNEL);
2102 if (!iter)
2103 return NULL;
2104
2105 cpu_buffer = buffer->buffers[cpu];
2106
2107 iter->cpu_buffer = cpu_buffer;
2108
2109 atomic_inc(&cpu_buffer->record_disabled);
2110 synchronize_sched();
2111
f83c9d0f 2112 spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
3e03fb7f 2113 __raw_spin_lock(&cpu_buffer->lock);
642edba5 2114 rb_iter_reset(iter);
3e03fb7f 2115 __raw_spin_unlock(&cpu_buffer->lock);
f83c9d0f 2116 spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
7a8e76a3
SR
2117
2118 return iter;
2119}
c4f50183 2120EXPORT_SYMBOL_GPL(ring_buffer_read_start);
7a8e76a3
SR
2121
2122/**
2123 * ring_buffer_finish - finish reading the iterator of the buffer
2124 * @iter: The iterator retrieved by ring_buffer_start
2125 *
2126 * This re-enables the recording to the buffer, and frees the
2127 * iterator.
2128 */
2129void
2130ring_buffer_read_finish(struct ring_buffer_iter *iter)
2131{
2132 struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
2133
2134 atomic_dec(&cpu_buffer->record_disabled);
2135 kfree(iter);
2136}
c4f50183 2137EXPORT_SYMBOL_GPL(ring_buffer_read_finish);
7a8e76a3
SR
2138
2139/**
2140 * ring_buffer_read - read the next item in the ring buffer by the iterator
2141 * @iter: The ring buffer iterator
2142 * @ts: The time stamp of the event read.
2143 *
2144 * This reads the next event in the ring buffer and increments the iterator.
2145 */
2146struct ring_buffer_event *
2147ring_buffer_read(struct ring_buffer_iter *iter, u64 *ts)
2148{
2149 struct ring_buffer_event *event;
f83c9d0f
SR
2150 struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
2151 unsigned long flags;
7a8e76a3 2152
f83c9d0f
SR
2153 spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
2154 event = rb_iter_peek(iter, ts);
7a8e76a3 2155 if (!event)
f83c9d0f 2156 goto out;
7a8e76a3
SR
2157
2158 rb_advance_iter(iter);
f83c9d0f
SR
2159 out:
2160 spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
7a8e76a3
SR
2161
2162 return event;
2163}
c4f50183 2164EXPORT_SYMBOL_GPL(ring_buffer_read);
7a8e76a3
SR
2165
2166/**
2167 * ring_buffer_size - return the size of the ring buffer (in bytes)
2168 * @buffer: The ring buffer.
2169 */
2170unsigned long ring_buffer_size(struct ring_buffer *buffer)
2171{
2172 return BUF_PAGE_SIZE * buffer->pages;
2173}
c4f50183 2174EXPORT_SYMBOL_GPL(ring_buffer_size);
7a8e76a3
SR
2175
2176static void
2177rb_reset_cpu(struct ring_buffer_per_cpu *cpu_buffer)
2178{
2179 cpu_buffer->head_page
2180 = list_entry(cpu_buffer->pages.next, struct buffer_page, list);
bf41a158 2181 local_set(&cpu_buffer->head_page->write, 0);
abc9b56d 2182 local_set(&cpu_buffer->head_page->page->commit, 0);
d769041f 2183
6f807acd 2184 cpu_buffer->head_page->read = 0;
bf41a158
SR
2185
2186 cpu_buffer->tail_page = cpu_buffer->head_page;
2187 cpu_buffer->commit_page = cpu_buffer->head_page;
2188
2189 INIT_LIST_HEAD(&cpu_buffer->reader_page->list);
2190 local_set(&cpu_buffer->reader_page->write, 0);
abc9b56d 2191 local_set(&cpu_buffer->reader_page->page->commit, 0);
6f807acd 2192 cpu_buffer->reader_page->read = 0;
7a8e76a3 2193
7a8e76a3
SR
2194 cpu_buffer->overrun = 0;
2195 cpu_buffer->entries = 0;
69507c06
SR
2196
2197 cpu_buffer->write_stamp = 0;
2198 cpu_buffer->read_stamp = 0;
7a8e76a3
SR
2199}
2200
2201/**
2202 * ring_buffer_reset_cpu - reset a ring buffer per CPU buffer
2203 * @buffer: The ring buffer to reset a per cpu buffer of
2204 * @cpu: The CPU buffer to be reset
2205 */
2206void ring_buffer_reset_cpu(struct ring_buffer *buffer, int cpu)
2207{
2208 struct ring_buffer_per_cpu *cpu_buffer = buffer->buffers[cpu];
2209 unsigned long flags;
2210
9e01c1b7 2211 if (!cpumask_test_cpu(cpu, buffer->cpumask))
7a8e76a3
SR
2212 return;
2213
f83c9d0f
SR
2214 spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
2215
3e03fb7f 2216 __raw_spin_lock(&cpu_buffer->lock);
7a8e76a3
SR
2217
2218 rb_reset_cpu(cpu_buffer);
2219
3e03fb7f 2220 __raw_spin_unlock(&cpu_buffer->lock);
f83c9d0f
SR
2221
2222 spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
7a8e76a3 2223}
c4f50183 2224EXPORT_SYMBOL_GPL(ring_buffer_reset_cpu);
7a8e76a3
SR
2225
2226/**
2227 * ring_buffer_reset - reset a ring buffer
2228 * @buffer: The ring buffer to reset all cpu buffers
2229 */
2230void ring_buffer_reset(struct ring_buffer *buffer)
2231{
7a8e76a3
SR
2232 int cpu;
2233
7a8e76a3 2234 for_each_buffer_cpu(buffer, cpu)
d769041f 2235 ring_buffer_reset_cpu(buffer, cpu);
7a8e76a3 2236}
c4f50183 2237EXPORT_SYMBOL_GPL(ring_buffer_reset);
7a8e76a3
SR
2238
2239/**
2240 * rind_buffer_empty - is the ring buffer empty?
2241 * @buffer: The ring buffer to test
2242 */
2243int ring_buffer_empty(struct ring_buffer *buffer)
2244{
2245 struct ring_buffer_per_cpu *cpu_buffer;
2246 int cpu;
2247
2248 /* yes this is racy, but if you don't like the race, lock the buffer */
2249 for_each_buffer_cpu(buffer, cpu) {
2250 cpu_buffer = buffer->buffers[cpu];
2251 if (!rb_per_cpu_empty(cpu_buffer))
2252 return 0;
2253 }
2254 return 1;
2255}
c4f50183 2256EXPORT_SYMBOL_GPL(ring_buffer_empty);
7a8e76a3
SR
2257
2258/**
2259 * ring_buffer_empty_cpu - is a cpu buffer of a ring buffer empty?
2260 * @buffer: The ring buffer
2261 * @cpu: The CPU buffer to test
2262 */
2263int ring_buffer_empty_cpu(struct ring_buffer *buffer, int cpu)
2264{
2265 struct ring_buffer_per_cpu *cpu_buffer;
2266
9e01c1b7 2267 if (!cpumask_test_cpu(cpu, buffer->cpumask))
7a8e76a3
SR
2268 return 1;
2269
2270 cpu_buffer = buffer->buffers[cpu];
2271 return rb_per_cpu_empty(cpu_buffer);
2272}
c4f50183 2273EXPORT_SYMBOL_GPL(ring_buffer_empty_cpu);
7a8e76a3
SR
2274
2275/**
2276 * ring_buffer_swap_cpu - swap a CPU buffer between two ring buffers
2277 * @buffer_a: One buffer to swap with
2278 * @buffer_b: The other buffer to swap with
2279 *
2280 * This function is useful for tracers that want to take a "snapshot"
2281 * of a CPU buffer and has another back up buffer lying around.
2282 * it is expected that the tracer handles the cpu buffer not being
2283 * used at the moment.
2284 */
2285int ring_buffer_swap_cpu(struct ring_buffer *buffer_a,
2286 struct ring_buffer *buffer_b, int cpu)
2287{
2288 struct ring_buffer_per_cpu *cpu_buffer_a;
2289 struct ring_buffer_per_cpu *cpu_buffer_b;
2290
9e01c1b7
RR
2291 if (!cpumask_test_cpu(cpu, buffer_a->cpumask) ||
2292 !cpumask_test_cpu(cpu, buffer_b->cpumask))
7a8e76a3
SR
2293 return -EINVAL;
2294
2295 /* At least make sure the two buffers are somewhat the same */
6d102bc6 2296 if (buffer_a->pages != buffer_b->pages)
7a8e76a3
SR
2297 return -EINVAL;
2298
97b17efe
SR
2299 if (ring_buffer_flags != RB_BUFFERS_ON)
2300 return -EAGAIN;
2301
2302 if (atomic_read(&buffer_a->record_disabled))
2303 return -EAGAIN;
2304
2305 if (atomic_read(&buffer_b->record_disabled))
2306 return -EAGAIN;
2307
7a8e76a3
SR
2308 cpu_buffer_a = buffer_a->buffers[cpu];
2309 cpu_buffer_b = buffer_b->buffers[cpu];
2310
97b17efe
SR
2311 if (atomic_read(&cpu_buffer_a->record_disabled))
2312 return -EAGAIN;
2313
2314 if (atomic_read(&cpu_buffer_b->record_disabled))
2315 return -EAGAIN;
2316
7a8e76a3
SR
2317 /*
2318 * We can't do a synchronize_sched here because this
2319 * function can be called in atomic context.
2320 * Normally this will be called from the same CPU as cpu.
2321 * If not it's up to the caller to protect this.
2322 */
2323 atomic_inc(&cpu_buffer_a->record_disabled);
2324 atomic_inc(&cpu_buffer_b->record_disabled);
2325
2326 buffer_a->buffers[cpu] = cpu_buffer_b;
2327 buffer_b->buffers[cpu] = cpu_buffer_a;
2328
2329 cpu_buffer_b->buffer = buffer_a;
2330 cpu_buffer_a->buffer = buffer_b;
2331
2332 atomic_dec(&cpu_buffer_a->record_disabled);
2333 atomic_dec(&cpu_buffer_b->record_disabled);
2334
2335 return 0;
2336}
c4f50183 2337EXPORT_SYMBOL_GPL(ring_buffer_swap_cpu);
7a8e76a3 2338
8789a9e7 2339static void rb_remove_entries(struct ring_buffer_per_cpu *cpu_buffer,
044fa782 2340 struct buffer_data_page *bpage)
8789a9e7
SR
2341{
2342 struct ring_buffer_event *event;
2343 unsigned long head;
2344
2345 __raw_spin_lock(&cpu_buffer->lock);
044fa782 2346 for (head = 0; head < local_read(&bpage->commit);
8789a9e7
SR
2347 head += rb_event_length(event)) {
2348
044fa782 2349 event = __rb_data_page_index(bpage, head);
8789a9e7
SR
2350 if (RB_WARN_ON(cpu_buffer, rb_null_event(event)))
2351 return;
2352 /* Only count data entries */
2353 if (event->type != RINGBUF_TYPE_DATA)
2354 continue;
2355 cpu_buffer->entries--;
2356 }
2357 __raw_spin_unlock(&cpu_buffer->lock);
2358}
2359
2360/**
2361 * ring_buffer_alloc_read_page - allocate a page to read from buffer
2362 * @buffer: the buffer to allocate for.
2363 *
2364 * This function is used in conjunction with ring_buffer_read_page.
2365 * When reading a full page from the ring buffer, these functions
2366 * can be used to speed up the process. The calling function should
2367 * allocate a few pages first with this function. Then when it
2368 * needs to get pages from the ring buffer, it passes the result
2369 * of this function into ring_buffer_read_page, which will swap
2370 * the page that was allocated, with the read page of the buffer.
2371 *
2372 * Returns:
2373 * The page allocated, or NULL on error.
2374 */
2375void *ring_buffer_alloc_read_page(struct ring_buffer *buffer)
2376{
2377 unsigned long addr;
044fa782 2378 struct buffer_data_page *bpage;
8789a9e7
SR
2379
2380 addr = __get_free_page(GFP_KERNEL);
2381 if (!addr)
2382 return NULL;
2383
044fa782 2384 bpage = (void *)addr;
8789a9e7 2385
044fa782 2386 return bpage;
8789a9e7
SR
2387}
2388
2389/**
2390 * ring_buffer_free_read_page - free an allocated read page
2391 * @buffer: the buffer the page was allocate for
2392 * @data: the page to free
2393 *
2394 * Free a page allocated from ring_buffer_alloc_read_page.
2395 */
2396void ring_buffer_free_read_page(struct ring_buffer *buffer, void *data)
2397{
2398 free_page((unsigned long)data);
2399}
2400
2401/**
2402 * ring_buffer_read_page - extract a page from the ring buffer
2403 * @buffer: buffer to extract from
2404 * @data_page: the page to use allocated from ring_buffer_alloc_read_page
2405 * @cpu: the cpu of the buffer to extract
2406 * @full: should the extraction only happen when the page is full.
2407 *
2408 * This function will pull out a page from the ring buffer and consume it.
2409 * @data_page must be the address of the variable that was returned
2410 * from ring_buffer_alloc_read_page. This is because the page might be used
2411 * to swap with a page in the ring buffer.
2412 *
2413 * for example:
2414 * rpage = ring_buffer_alloc_page(buffer);
2415 * if (!rpage)
2416 * return error;
2417 * ret = ring_buffer_read_page(buffer, &rpage, cpu, 0);
2418 * if (ret)
2419 * process_page(rpage);
2420 *
2421 * When @full is set, the function will not return true unless
2422 * the writer is off the reader page.
2423 *
2424 * Note: it is up to the calling functions to handle sleeps and wakeups.
2425 * The ring buffer can be used anywhere in the kernel and can not
2426 * blindly call wake_up. The layer that uses the ring buffer must be
2427 * responsible for that.
2428 *
2429 * Returns:
2430 * 1 if data has been transferred
2431 * 0 if no data has been transferred.
2432 */
2433int ring_buffer_read_page(struct ring_buffer *buffer,
2434 void **data_page, int cpu, int full)
2435{
2436 struct ring_buffer_per_cpu *cpu_buffer = buffer->buffers[cpu];
2437 struct ring_buffer_event *event;
044fa782 2438 struct buffer_data_page *bpage;
8789a9e7
SR
2439 unsigned long flags;
2440 int ret = 0;
2441
2442 if (!data_page)
2443 return 0;
2444
044fa782
SR
2445 bpage = *data_page;
2446 if (!bpage)
8789a9e7
SR
2447 return 0;
2448
2449 spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
2450
2451 /*
2452 * rb_buffer_peek will get the next ring buffer if
2453 * the current reader page is empty.
2454 */
2455 event = rb_buffer_peek(buffer, cpu, NULL);
2456 if (!event)
2457 goto out;
2458
2459 /* check for data */
2460 if (!local_read(&cpu_buffer->reader_page->page->commit))
2461 goto out;
2462 /*
2463 * If the writer is already off of the read page, then simply
2464 * switch the read page with the given page. Otherwise
2465 * we need to copy the data from the reader to the writer.
2466 */
2467 if (cpu_buffer->reader_page == cpu_buffer->commit_page) {
2468 unsigned int read = cpu_buffer->reader_page->read;
2469
2470 if (full)
2471 goto out;
2472 /* The writer is still on the reader page, we must copy */
044fa782
SR
2473 bpage = cpu_buffer->reader_page->page;
2474 memcpy(bpage->data,
8789a9e7 2475 cpu_buffer->reader_page->page->data + read,
044fa782 2476 local_read(&bpage->commit) - read);
8789a9e7
SR
2477
2478 /* consume what was read */
2479 cpu_buffer->reader_page += read;
2480
2481 } else {
2482 /* swap the pages */
044fa782
SR
2483 rb_init_page(bpage);
2484 bpage = cpu_buffer->reader_page->page;
8789a9e7
SR
2485 cpu_buffer->reader_page->page = *data_page;
2486 cpu_buffer->reader_page->read = 0;
044fa782 2487 *data_page = bpage;
8789a9e7
SR
2488 }
2489 ret = 1;
2490
2491 /* update the entry counter */
044fa782 2492 rb_remove_entries(cpu_buffer, bpage);
8789a9e7
SR
2493 out:
2494 spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
2495
2496 return ret;
2497}
2498
a3583244
SR
2499static ssize_t
2500rb_simple_read(struct file *filp, char __user *ubuf,
2501 size_t cnt, loff_t *ppos)
2502{
033601a3 2503 long *p = filp->private_data;
a3583244
SR
2504 char buf[64];
2505 int r;
2506
033601a3
SR
2507 if (test_bit(RB_BUFFERS_DISABLED_BIT, p))
2508 r = sprintf(buf, "permanently disabled\n");
2509 else
2510 r = sprintf(buf, "%d\n", test_bit(RB_BUFFERS_ON_BIT, p));
a3583244
SR
2511
2512 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2513}
2514
2515static ssize_t
2516rb_simple_write(struct file *filp, const char __user *ubuf,
2517 size_t cnt, loff_t *ppos)
2518{
033601a3 2519 long *p = filp->private_data;
a3583244
SR
2520 char buf[64];
2521 long val;
2522 int ret;
2523
2524 if (cnt >= sizeof(buf))
2525 return -EINVAL;
2526
2527 if (copy_from_user(&buf, ubuf, cnt))
2528 return -EFAULT;
2529
2530 buf[cnt] = 0;
2531
2532 ret = strict_strtoul(buf, 10, &val);
2533 if (ret < 0)
2534 return ret;
2535
033601a3
SR
2536 if (val)
2537 set_bit(RB_BUFFERS_ON_BIT, p);
2538 else
2539 clear_bit(RB_BUFFERS_ON_BIT, p);
a3583244
SR
2540
2541 (*ppos)++;
2542
2543 return cnt;
2544}
2545
2546static struct file_operations rb_simple_fops = {
2547 .open = tracing_open_generic,
2548 .read = rb_simple_read,
2549 .write = rb_simple_write,
2550};
2551
2552
2553static __init int rb_init_debugfs(void)
2554{
2555 struct dentry *d_tracer;
2556 struct dentry *entry;
2557
2558 d_tracer = tracing_init_dentry();
2559
2560 entry = debugfs_create_file("tracing_on", 0644, d_tracer,
033601a3 2561 &ring_buffer_flags, &rb_simple_fops);
a3583244
SR
2562 if (!entry)
2563 pr_warning("Could not create debugfs 'tracing_on' entry\n");
2564
2565 return 0;
2566}
2567
2568fs_initcall(rb_init_debugfs);