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