blob: 86dc353f89b99cfe66c01c9348052cab0e06f91d [file] [log] [blame]
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001/*
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
Steven Rostedt182e9f52008-11-03 23:15:56 -050019#include "trace.h"
20
Steven Rostedta3583242008-11-11 15:01:42 -050021/* Global flag to disable all recording to ring buffers */
22static int ring_buffers_off __read_mostly;
23
24/**
25 * tracing_on - enable all tracing buffers
26 *
27 * This function enables all tracing buffers that may have been
28 * disabled with tracing_off.
29 */
30void tracing_on(void)
31{
32 ring_buffers_off = 0;
33}
34
35/**
36 * tracing_off - turn off all tracing buffers
37 *
38 * This function stops all tracing buffers from recording data.
39 * It does not disable any overhead the tracers themselves may
40 * be causing. This function simply causes all recording to
41 * the ring buffers to fail.
42 */
43void tracing_off(void)
44{
45 ring_buffers_off = 1;
46}
47
Ingo Molnard06bbd62008-11-12 10:11:37 +010048#include "trace.h"
49
Steven Rostedt7a8e76a2008-09-29 23:02:38 -040050/* Up this if you want to test the TIME_EXTENTS and normalization */
51#define DEBUG_SHIFT 0
52
53/* FIXME!!! */
54u64 ring_buffer_time_stamp(int cpu)
55{
56 /* shift to debug/test normalization and TIME_EXTENTS */
57 return sched_clock() << DEBUG_SHIFT;
58}
59
60void ring_buffer_normalize_time_stamp(int cpu, u64 *ts)
61{
62 /* Just stupid testing the normalize function and deltas */
63 *ts >>= DEBUG_SHIFT;
64}
65
66#define RB_EVNT_HDR_SIZE (sizeof(struct ring_buffer_event))
67#define RB_ALIGNMENT_SHIFT 2
68#define RB_ALIGNMENT (1 << RB_ALIGNMENT_SHIFT)
69#define RB_MAX_SMALL_DATA 28
70
71enum {
72 RB_LEN_TIME_EXTEND = 8,
73 RB_LEN_TIME_STAMP = 16,
74};
75
76/* inline for ring buffer fast paths */
77static inline unsigned
78rb_event_length(struct ring_buffer_event *event)
79{
80 unsigned length;
81
82 switch (event->type) {
83 case RINGBUF_TYPE_PADDING:
84 /* undefined */
85 return -1;
86
87 case RINGBUF_TYPE_TIME_EXTEND:
88 return RB_LEN_TIME_EXTEND;
89
90 case RINGBUF_TYPE_TIME_STAMP:
91 return RB_LEN_TIME_STAMP;
92
93 case RINGBUF_TYPE_DATA:
94 if (event->len)
95 length = event->len << RB_ALIGNMENT_SHIFT;
96 else
97 length = event->array[0];
98 return length + RB_EVNT_HDR_SIZE;
99 default:
100 BUG();
101 }
102 /* not hit */
103 return 0;
104}
105
106/**
107 * ring_buffer_event_length - return the length of the event
108 * @event: the event to get the length of
109 */
110unsigned ring_buffer_event_length(struct ring_buffer_event *event)
111{
112 return rb_event_length(event);
113}
114
115/* inline for ring buffer fast paths */
116static inline void *
117rb_event_data(struct ring_buffer_event *event)
118{
119 BUG_ON(event->type != RINGBUF_TYPE_DATA);
120 /* If length is in len field, then array[0] has the data */
121 if (event->len)
122 return (void *)&event->array[0];
123 /* Otherwise length is in array[0] and array[1] has the data */
124 return (void *)&event->array[1];
125}
126
127/**
128 * ring_buffer_event_data - return the data of the event
129 * @event: the event to get the data from
130 */
131void *ring_buffer_event_data(struct ring_buffer_event *event)
132{
133 return rb_event_data(event);
134}
135
136#define for_each_buffer_cpu(buffer, cpu) \
137 for_each_cpu_mask(cpu, buffer->cpumask)
138
139#define TS_SHIFT 27
140#define TS_MASK ((1ULL << TS_SHIFT) - 1)
141#define TS_DELTA_TEST (~TS_MASK)
142
143/*
144 * This hack stolen from mm/slob.c.
145 * We can store per page timing information in the page frame of the page.
146 * Thanks to Peter Zijlstra for suggesting this idea.
147 */
148struct buffer_page {
Steven Rostedte4c2ce82008-10-01 11:14:54 -0400149 u64 time_stamp; /* page time stamp */
Steven Rostedtbf41a152008-10-04 02:00:59 -0400150 local_t write; /* index for next write */
151 local_t commit; /* write commited index */
Steven Rostedt6f807ac2008-10-04 02:00:58 -0400152 unsigned read; /* index for next read */
Steven Rostedte4c2ce82008-10-01 11:14:54 -0400153 struct list_head list; /* list of free pages */
154 void *page; /* Actual data page */
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400155};
156
157/*
Steven Rostedted568292008-09-29 23:02:40 -0400158 * Also stolen from mm/slob.c. Thanks to Mathieu Desnoyers for pointing
159 * this issue out.
160 */
161static inline void free_buffer_page(struct buffer_page *bpage)
162{
Steven Rostedte4c2ce82008-10-01 11:14:54 -0400163 if (bpage->page)
Steven Rostedt6ae2a072008-10-13 10:22:06 -0400164 free_page((unsigned long)bpage->page);
Steven Rostedte4c2ce82008-10-01 11:14:54 -0400165 kfree(bpage);
Steven Rostedted568292008-09-29 23:02:40 -0400166}
167
168/*
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400169 * We need to fit the time_stamp delta into 27 bits.
170 */
171static inline int test_time_stamp(u64 delta)
172{
173 if (delta & TS_DELTA_TEST)
174 return 1;
175 return 0;
176}
177
178#define BUF_PAGE_SIZE PAGE_SIZE
179
180/*
181 * head_page == tail_page && head == tail then buffer is empty.
182 */
183struct ring_buffer_per_cpu {
184 int cpu;
185 struct ring_buffer *buffer;
Steven Rostedtf83c9d02008-11-11 18:47:44 +0100186 spinlock_t reader_lock; /* serialize readers */
Steven Rostedt3e03fb72008-11-06 00:09:43 -0500187 raw_spinlock_t lock;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400188 struct lock_class_key lock_key;
189 struct list_head pages;
Steven Rostedt6f807ac2008-10-04 02:00:58 -0400190 struct buffer_page *head_page; /* read from head */
191 struct buffer_page *tail_page; /* write to tail */
Steven Rostedtbf41a152008-10-04 02:00:59 -0400192 struct buffer_page *commit_page; /* commited pages */
Steven Rostedtd7690412008-10-01 00:29:53 -0400193 struct buffer_page *reader_page;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400194 unsigned long overrun;
195 unsigned long entries;
196 u64 write_stamp;
197 u64 read_stamp;
198 atomic_t record_disabled;
199};
200
201struct ring_buffer {
202 unsigned long size;
203 unsigned pages;
204 unsigned flags;
205 int cpus;
206 cpumask_t cpumask;
207 atomic_t record_disabled;
208
209 struct mutex mutex;
210
211 struct ring_buffer_per_cpu **buffers;
212};
213
214struct ring_buffer_iter {
215 struct ring_buffer_per_cpu *cpu_buffer;
216 unsigned long head;
217 struct buffer_page *head_page;
218 u64 read_stamp;
219};
220
Steven Rostedtf536aaf2008-11-10 23:07:30 -0500221/* buffer may be either ring_buffer or ring_buffer_per_cpu */
Steven Rostedtbf41a152008-10-04 02:00:59 -0400222#define RB_WARN_ON(buffer, cond) \
Steven Rostedt3e89c7bb2008-11-11 15:28:41 -0500223 ({ \
224 int _____ret = unlikely(cond); \
225 if (_____ret) { \
Steven Rostedtbf41a152008-10-04 02:00:59 -0400226 atomic_inc(&buffer->record_disabled); \
227 WARN_ON(1); \
228 } \
Steven Rostedt3e89c7bb2008-11-11 15:28:41 -0500229 _____ret; \
230 })
Steven Rostedtf536aaf2008-11-10 23:07:30 -0500231
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400232/**
233 * check_pages - integrity check of buffer pages
234 * @cpu_buffer: CPU buffer with pages to test
235 *
236 * As a safty measure we check to make sure the data pages have not
237 * been corrupted.
238 */
239static int rb_check_pages(struct ring_buffer_per_cpu *cpu_buffer)
240{
241 struct list_head *head = &cpu_buffer->pages;
242 struct buffer_page *page, *tmp;
243
Steven Rostedt3e89c7bb2008-11-11 15:28:41 -0500244 if (RB_WARN_ON(cpu_buffer, head->next->prev != head))
245 return -1;
246 if (RB_WARN_ON(cpu_buffer, head->prev->next != head))
247 return -1;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400248
249 list_for_each_entry_safe(page, tmp, head, list) {
Steven Rostedt3e89c7bb2008-11-11 15:28:41 -0500250 if (RB_WARN_ON(cpu_buffer,
251 page->list.next->prev != &page->list))
252 return -1;
253 if (RB_WARN_ON(cpu_buffer,
254 page->list.prev->next != &page->list))
255 return -1;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400256 }
257
258 return 0;
259}
260
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400261static int rb_allocate_pages(struct ring_buffer_per_cpu *cpu_buffer,
262 unsigned nr_pages)
263{
264 struct list_head *head = &cpu_buffer->pages;
265 struct buffer_page *page, *tmp;
266 unsigned long addr;
267 LIST_HEAD(pages);
268 unsigned i;
269
270 for (i = 0; i < nr_pages; i++) {
Steven Rostedte4c2ce82008-10-01 11:14:54 -0400271 page = kzalloc_node(ALIGN(sizeof(*page), cache_line_size()),
Steven Rostedtaa1e0e3b2008-10-02 19:18:09 -0400272 GFP_KERNEL, cpu_to_node(cpu_buffer->cpu));
Steven Rostedte4c2ce82008-10-01 11:14:54 -0400273 if (!page)
274 goto free_pages;
275 list_add(&page->list, &pages);
276
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400277 addr = __get_free_page(GFP_KERNEL);
278 if (!addr)
279 goto free_pages;
Steven Rostedte4c2ce82008-10-01 11:14:54 -0400280 page->page = (void *)addr;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400281 }
282
283 list_splice(&pages, head);
284
285 rb_check_pages(cpu_buffer);
286
287 return 0;
288
289 free_pages:
290 list_for_each_entry_safe(page, tmp, &pages, list) {
291 list_del_init(&page->list);
Steven Rostedted568292008-09-29 23:02:40 -0400292 free_buffer_page(page);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400293 }
294 return -ENOMEM;
295}
296
297static struct ring_buffer_per_cpu *
298rb_allocate_cpu_buffer(struct ring_buffer *buffer, int cpu)
299{
300 struct ring_buffer_per_cpu *cpu_buffer;
Steven Rostedte4c2ce82008-10-01 11:14:54 -0400301 struct buffer_page *page;
Steven Rostedtd7690412008-10-01 00:29:53 -0400302 unsigned long addr;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400303 int ret;
304
305 cpu_buffer = kzalloc_node(ALIGN(sizeof(*cpu_buffer), cache_line_size()),
306 GFP_KERNEL, cpu_to_node(cpu));
307 if (!cpu_buffer)
308 return NULL;
309
310 cpu_buffer->cpu = cpu;
311 cpu_buffer->buffer = buffer;
Steven Rostedtf83c9d02008-11-11 18:47:44 +0100312 spin_lock_init(&cpu_buffer->reader_lock);
Steven Rostedt3e03fb72008-11-06 00:09:43 -0500313 cpu_buffer->lock = (raw_spinlock_t)__RAW_SPIN_LOCK_UNLOCKED;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400314 INIT_LIST_HEAD(&cpu_buffer->pages);
315
Steven Rostedte4c2ce82008-10-01 11:14:54 -0400316 page = kzalloc_node(ALIGN(sizeof(*page), cache_line_size()),
317 GFP_KERNEL, cpu_to_node(cpu));
318 if (!page)
319 goto fail_free_buffer;
320
321 cpu_buffer->reader_page = page;
Steven Rostedtd7690412008-10-01 00:29:53 -0400322 addr = __get_free_page(GFP_KERNEL);
323 if (!addr)
Steven Rostedte4c2ce82008-10-01 11:14:54 -0400324 goto fail_free_reader;
325 page->page = (void *)addr;
326
Steven Rostedtd7690412008-10-01 00:29:53 -0400327 INIT_LIST_HEAD(&cpu_buffer->reader_page->list);
Steven Rostedtd7690412008-10-01 00:29:53 -0400328
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400329 ret = rb_allocate_pages(cpu_buffer, buffer->pages);
330 if (ret < 0)
Steven Rostedtd7690412008-10-01 00:29:53 -0400331 goto fail_free_reader;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400332
333 cpu_buffer->head_page
334 = list_entry(cpu_buffer->pages.next, struct buffer_page, list);
Steven Rostedtbf41a152008-10-04 02:00:59 -0400335 cpu_buffer->tail_page = cpu_buffer->commit_page = cpu_buffer->head_page;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400336
337 return cpu_buffer;
338
Steven Rostedtd7690412008-10-01 00:29:53 -0400339 fail_free_reader:
340 free_buffer_page(cpu_buffer->reader_page);
341
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400342 fail_free_buffer:
343 kfree(cpu_buffer);
344 return NULL;
345}
346
347static void rb_free_cpu_buffer(struct ring_buffer_per_cpu *cpu_buffer)
348{
349 struct list_head *head = &cpu_buffer->pages;
350 struct buffer_page *page, *tmp;
351
Steven Rostedtd7690412008-10-01 00:29:53 -0400352 list_del_init(&cpu_buffer->reader_page->list);
353 free_buffer_page(cpu_buffer->reader_page);
354
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400355 list_for_each_entry_safe(page, tmp, head, list) {
356 list_del_init(&page->list);
Steven Rostedted568292008-09-29 23:02:40 -0400357 free_buffer_page(page);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400358 }
359 kfree(cpu_buffer);
360}
361
Steven Rostedta7b13742008-09-29 23:02:39 -0400362/*
363 * Causes compile errors if the struct buffer_page gets bigger
364 * than the struct page.
365 */
366extern int ring_buffer_page_too_big(void);
367
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400368/**
369 * ring_buffer_alloc - allocate a new ring_buffer
370 * @size: the size in bytes that is needed.
371 * @flags: attributes to set for the ring buffer.
372 *
373 * Currently the only flag that is available is the RB_FL_OVERWRITE
374 * flag. This flag means that the buffer will overwrite old data
375 * when the buffer wraps. If this flag is not set, the buffer will
376 * drop data when the tail hits the head.
377 */
378struct ring_buffer *ring_buffer_alloc(unsigned long size, unsigned flags)
379{
380 struct ring_buffer *buffer;
381 int bsize;
382 int cpu;
383
Steven Rostedta7b13742008-09-29 23:02:39 -0400384 /* Paranoid! Optimizes out when all is well */
385 if (sizeof(struct buffer_page) > sizeof(struct page))
386 ring_buffer_page_too_big();
387
388
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400389 /* keep it in its own cache line */
390 buffer = kzalloc(ALIGN(sizeof(*buffer), cache_line_size()),
391 GFP_KERNEL);
392 if (!buffer)
393 return NULL;
394
395 buffer->pages = DIV_ROUND_UP(size, BUF_PAGE_SIZE);
396 buffer->flags = flags;
397
398 /* need at least two pages */
399 if (buffer->pages == 1)
400 buffer->pages++;
401
402 buffer->cpumask = cpu_possible_map;
403 buffer->cpus = nr_cpu_ids;
404
405 bsize = sizeof(void *) * nr_cpu_ids;
406 buffer->buffers = kzalloc(ALIGN(bsize, cache_line_size()),
407 GFP_KERNEL);
408 if (!buffer->buffers)
409 goto fail_free_buffer;
410
411 for_each_buffer_cpu(buffer, cpu) {
412 buffer->buffers[cpu] =
413 rb_allocate_cpu_buffer(buffer, cpu);
414 if (!buffer->buffers[cpu])
415 goto fail_free_buffers;
416 }
417
418 mutex_init(&buffer->mutex);
419
420 return buffer;
421
422 fail_free_buffers:
423 for_each_buffer_cpu(buffer, cpu) {
424 if (buffer->buffers[cpu])
425 rb_free_cpu_buffer(buffer->buffers[cpu]);
426 }
427 kfree(buffer->buffers);
428
429 fail_free_buffer:
430 kfree(buffer);
431 return NULL;
432}
433
434/**
435 * ring_buffer_free - free a ring buffer.
436 * @buffer: the buffer to free.
437 */
438void
439ring_buffer_free(struct ring_buffer *buffer)
440{
441 int cpu;
442
443 for_each_buffer_cpu(buffer, cpu)
444 rb_free_cpu_buffer(buffer->buffers[cpu]);
445
446 kfree(buffer);
447}
448
449static void rb_reset_cpu(struct ring_buffer_per_cpu *cpu_buffer);
450
451static void
452rb_remove_pages(struct ring_buffer_per_cpu *cpu_buffer, unsigned nr_pages)
453{
454 struct buffer_page *page;
455 struct list_head *p;
456 unsigned i;
457
458 atomic_inc(&cpu_buffer->record_disabled);
459 synchronize_sched();
460
461 for (i = 0; i < nr_pages; i++) {
Steven Rostedt3e89c7bb2008-11-11 15:28:41 -0500462 if (RB_WARN_ON(cpu_buffer, list_empty(&cpu_buffer->pages)))
463 return;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400464 p = cpu_buffer->pages.next;
465 page = list_entry(p, struct buffer_page, list);
466 list_del_init(&page->list);
Steven Rostedted568292008-09-29 23:02:40 -0400467 free_buffer_page(page);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400468 }
Steven Rostedt3e89c7bb2008-11-11 15:28:41 -0500469 if (RB_WARN_ON(cpu_buffer, list_empty(&cpu_buffer->pages)))
470 return;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400471
472 rb_reset_cpu(cpu_buffer);
473
474 rb_check_pages(cpu_buffer);
475
476 atomic_dec(&cpu_buffer->record_disabled);
477
478}
479
480static void
481rb_insert_pages(struct ring_buffer_per_cpu *cpu_buffer,
482 struct list_head *pages, unsigned nr_pages)
483{
484 struct buffer_page *page;
485 struct list_head *p;
486 unsigned i;
487
488 atomic_inc(&cpu_buffer->record_disabled);
489 synchronize_sched();
490
491 for (i = 0; i < nr_pages; i++) {
Steven Rostedt3e89c7bb2008-11-11 15:28:41 -0500492 if (RB_WARN_ON(cpu_buffer, list_empty(pages)))
493 return;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400494 p = pages->next;
495 page = list_entry(p, struct buffer_page, list);
496 list_del_init(&page->list);
497 list_add_tail(&page->list, &cpu_buffer->pages);
498 }
499 rb_reset_cpu(cpu_buffer);
500
501 rb_check_pages(cpu_buffer);
502
503 atomic_dec(&cpu_buffer->record_disabled);
504}
505
506/**
507 * ring_buffer_resize - resize the ring buffer
508 * @buffer: the buffer to resize.
509 * @size: the new size.
510 *
511 * The tracer is responsible for making sure that the buffer is
512 * not being used while changing the size.
513 * Note: We may be able to change the above requirement by using
514 * RCU synchronizations.
515 *
516 * Minimum size is 2 * BUF_PAGE_SIZE.
517 *
518 * Returns -1 on failure.
519 */
520int ring_buffer_resize(struct ring_buffer *buffer, unsigned long size)
521{
522 struct ring_buffer_per_cpu *cpu_buffer;
523 unsigned nr_pages, rm_pages, new_pages;
524 struct buffer_page *page, *tmp;
525 unsigned long buffer_size;
526 unsigned long addr;
527 LIST_HEAD(pages);
528 int i, cpu;
529
530 size = DIV_ROUND_UP(size, BUF_PAGE_SIZE);
531 size *= BUF_PAGE_SIZE;
532 buffer_size = buffer->pages * BUF_PAGE_SIZE;
533
534 /* we need a minimum of two pages */
535 if (size < BUF_PAGE_SIZE * 2)
536 size = BUF_PAGE_SIZE * 2;
537
538 if (size == buffer_size)
539 return size;
540
541 mutex_lock(&buffer->mutex);
542
543 nr_pages = DIV_ROUND_UP(size, BUF_PAGE_SIZE);
544
545 if (size < buffer_size) {
546
547 /* easy case, just free pages */
Steven Rostedt3e89c7bb2008-11-11 15:28:41 -0500548 if (RB_WARN_ON(buffer, nr_pages >= buffer->pages)) {
549 mutex_unlock(&buffer->mutex);
550 return -1;
551 }
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400552
553 rm_pages = buffer->pages - nr_pages;
554
555 for_each_buffer_cpu(buffer, cpu) {
556 cpu_buffer = buffer->buffers[cpu];
557 rb_remove_pages(cpu_buffer, rm_pages);
558 }
559 goto out;
560 }
561
562 /*
563 * This is a bit more difficult. We only want to add pages
564 * when we can allocate enough for all CPUs. We do this
565 * by allocating all the pages and storing them on a local
566 * link list. If we succeed in our allocation, then we
567 * add these pages to the cpu_buffers. Otherwise we just free
568 * them all and return -ENOMEM;
569 */
Steven Rostedt3e89c7bb2008-11-11 15:28:41 -0500570 if (RB_WARN_ON(buffer, nr_pages <= buffer->pages)) {
571 mutex_unlock(&buffer->mutex);
572 return -1;
573 }
Steven Rostedtf536aaf2008-11-10 23:07:30 -0500574
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400575 new_pages = nr_pages - buffer->pages;
576
577 for_each_buffer_cpu(buffer, cpu) {
578 for (i = 0; i < new_pages; i++) {
Steven Rostedte4c2ce82008-10-01 11:14:54 -0400579 page = kzalloc_node(ALIGN(sizeof(*page),
580 cache_line_size()),
581 GFP_KERNEL, cpu_to_node(cpu));
582 if (!page)
583 goto free_pages;
584 list_add(&page->list, &pages);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400585 addr = __get_free_page(GFP_KERNEL);
586 if (!addr)
587 goto free_pages;
Steven Rostedte4c2ce82008-10-01 11:14:54 -0400588 page->page = (void *)addr;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400589 }
590 }
591
592 for_each_buffer_cpu(buffer, cpu) {
593 cpu_buffer = buffer->buffers[cpu];
594 rb_insert_pages(cpu_buffer, &pages, new_pages);
595 }
596
Steven Rostedt3e89c7bb2008-11-11 15:28:41 -0500597 if (RB_WARN_ON(buffer, !list_empty(&pages))) {
598 mutex_unlock(&buffer->mutex);
599 return -1;
600 }
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400601
602 out:
603 buffer->pages = nr_pages;
604 mutex_unlock(&buffer->mutex);
605
606 return size;
607
608 free_pages:
609 list_for_each_entry_safe(page, tmp, &pages, list) {
610 list_del_init(&page->list);
Steven Rostedted568292008-09-29 23:02:40 -0400611 free_buffer_page(page);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400612 }
613 return -ENOMEM;
614}
615
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400616static inline int rb_null_event(struct ring_buffer_event *event)
617{
618 return event->type == RINGBUF_TYPE_PADDING;
619}
620
Steven Rostedt6f807ac2008-10-04 02:00:58 -0400621static inline void *__rb_page_index(struct buffer_page *page, unsigned index)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400622{
Steven Rostedte4c2ce82008-10-01 11:14:54 -0400623 return page->page + index;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400624}
625
626static inline struct ring_buffer_event *
Steven Rostedtd7690412008-10-01 00:29:53 -0400627rb_reader_event(struct ring_buffer_per_cpu *cpu_buffer)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400628{
Steven Rostedt6f807ac2008-10-04 02:00:58 -0400629 return __rb_page_index(cpu_buffer->reader_page,
630 cpu_buffer->reader_page->read);
631}
632
633static inline struct ring_buffer_event *
634rb_head_event(struct ring_buffer_per_cpu *cpu_buffer)
635{
636 return __rb_page_index(cpu_buffer->head_page,
637 cpu_buffer->head_page->read);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400638}
639
640static inline struct ring_buffer_event *
641rb_iter_head_event(struct ring_buffer_iter *iter)
642{
Steven Rostedt6f807ac2008-10-04 02:00:58 -0400643 return __rb_page_index(iter->head_page, iter->head);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400644}
645
Steven Rostedtbf41a152008-10-04 02:00:59 -0400646static inline unsigned rb_page_write(struct buffer_page *bpage)
647{
648 return local_read(&bpage->write);
649}
650
651static inline unsigned rb_page_commit(struct buffer_page *bpage)
652{
653 return local_read(&bpage->commit);
654}
655
656/* Size is determined by what has been commited */
657static inline unsigned rb_page_size(struct buffer_page *bpage)
658{
659 return rb_page_commit(bpage);
660}
661
662static inline unsigned
663rb_commit_index(struct ring_buffer_per_cpu *cpu_buffer)
664{
665 return rb_page_commit(cpu_buffer->commit_page);
666}
667
668static inline unsigned rb_head_size(struct ring_buffer_per_cpu *cpu_buffer)
669{
670 return rb_page_commit(cpu_buffer->head_page);
671}
672
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400673/*
674 * When the tail hits the head and the buffer is in overwrite mode,
675 * the head jumps to the next page and all content on the previous
676 * page is discarded. But before doing so, we update the overrun
677 * variable of the buffer.
678 */
679static void rb_update_overflow(struct ring_buffer_per_cpu *cpu_buffer)
680{
681 struct ring_buffer_event *event;
682 unsigned long head;
683
684 for (head = 0; head < rb_head_size(cpu_buffer);
685 head += rb_event_length(event)) {
686
Steven Rostedt6f807ac2008-10-04 02:00:58 -0400687 event = __rb_page_index(cpu_buffer->head_page, head);
Steven Rostedt3e89c7bb2008-11-11 15:28:41 -0500688 if (RB_WARN_ON(cpu_buffer, rb_null_event(event)))
689 return;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400690 /* Only count data entries */
691 if (event->type != RINGBUF_TYPE_DATA)
692 continue;
693 cpu_buffer->overrun++;
694 cpu_buffer->entries--;
695 }
696}
697
698static inline void rb_inc_page(struct ring_buffer_per_cpu *cpu_buffer,
699 struct buffer_page **page)
700{
701 struct list_head *p = (*page)->list.next;
702
703 if (p == &cpu_buffer->pages)
704 p = p->next;
705
706 *page = list_entry(p, struct buffer_page, list);
707}
708
Steven Rostedtbf41a152008-10-04 02:00:59 -0400709static inline unsigned
710rb_event_index(struct ring_buffer_event *event)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400711{
Steven Rostedtbf41a152008-10-04 02:00:59 -0400712 unsigned long addr = (unsigned long)event;
713
714 return (addr & ~PAGE_MASK) - (PAGE_SIZE - BUF_PAGE_SIZE);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400715}
716
Steven Rostedtbf41a152008-10-04 02:00:59 -0400717static inline int
718rb_is_commit(struct ring_buffer_per_cpu *cpu_buffer,
719 struct ring_buffer_event *event)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400720{
Steven Rostedtbf41a152008-10-04 02:00:59 -0400721 unsigned long addr = (unsigned long)event;
722 unsigned long index;
723
724 index = rb_event_index(event);
725 addr &= PAGE_MASK;
726
727 return cpu_buffer->commit_page->page == (void *)addr &&
728 rb_commit_index(cpu_buffer) == index;
729}
730
731static inline void
732rb_set_commit_event(struct ring_buffer_per_cpu *cpu_buffer,
733 struct ring_buffer_event *event)
734{
735 unsigned long addr = (unsigned long)event;
736 unsigned long index;
737
738 index = rb_event_index(event);
739 addr &= PAGE_MASK;
740
741 while (cpu_buffer->commit_page->page != (void *)addr) {
Steven Rostedt3e89c7bb2008-11-11 15:28:41 -0500742 if (RB_WARN_ON(cpu_buffer,
743 cpu_buffer->commit_page == cpu_buffer->tail_page))
744 return;
Steven Rostedtbf41a152008-10-04 02:00:59 -0400745 cpu_buffer->commit_page->commit =
746 cpu_buffer->commit_page->write;
747 rb_inc_page(cpu_buffer, &cpu_buffer->commit_page);
748 cpu_buffer->write_stamp = cpu_buffer->commit_page->time_stamp;
749 }
750
751 /* Now set the commit to the event's index */
752 local_set(&cpu_buffer->commit_page->commit, index);
753}
754
755static inline void
756rb_set_commit_to_write(struct ring_buffer_per_cpu *cpu_buffer)
757{
758 /*
759 * We only race with interrupts and NMIs on this CPU.
760 * If we own the commit event, then we can commit
761 * all others that interrupted us, since the interruptions
762 * are in stack format (they finish before they come
763 * back to us). This allows us to do a simple loop to
764 * assign the commit to the tail.
765 */
766 while (cpu_buffer->commit_page != cpu_buffer->tail_page) {
767 cpu_buffer->commit_page->commit =
768 cpu_buffer->commit_page->write;
769 rb_inc_page(cpu_buffer, &cpu_buffer->commit_page);
770 cpu_buffer->write_stamp = cpu_buffer->commit_page->time_stamp;
771 /* add barrier to keep gcc from optimizing too much */
772 barrier();
773 }
774 while (rb_commit_index(cpu_buffer) !=
775 rb_page_write(cpu_buffer->commit_page)) {
776 cpu_buffer->commit_page->commit =
777 cpu_buffer->commit_page->write;
778 barrier();
779 }
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400780}
781
Steven Rostedtd7690412008-10-01 00:29:53 -0400782static void rb_reset_reader_page(struct ring_buffer_per_cpu *cpu_buffer)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400783{
Steven Rostedtd7690412008-10-01 00:29:53 -0400784 cpu_buffer->read_stamp = cpu_buffer->reader_page->time_stamp;
Steven Rostedt6f807ac2008-10-04 02:00:58 -0400785 cpu_buffer->reader_page->read = 0;
Steven Rostedtd7690412008-10-01 00:29:53 -0400786}
787
788static inline void rb_inc_iter(struct ring_buffer_iter *iter)
789{
790 struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
791
792 /*
793 * The iterator could be on the reader page (it starts there).
794 * But the head could have moved, since the reader was
795 * found. Check for this case and assign the iterator
796 * to the head page instead of next.
797 */
798 if (iter->head_page == cpu_buffer->reader_page)
799 iter->head_page = cpu_buffer->head_page;
800 else
801 rb_inc_page(cpu_buffer, &iter->head_page);
802
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400803 iter->read_stamp = iter->head_page->time_stamp;
804 iter->head = 0;
805}
806
807/**
808 * ring_buffer_update_event - update event type and data
809 * @event: the even to update
810 * @type: the type of event
811 * @length: the size of the event field in the ring buffer
812 *
813 * Update the type and data fields of the event. The length
814 * is the actual size that is written to the ring buffer,
815 * and with this, we can determine what to place into the
816 * data field.
817 */
818static inline void
819rb_update_event(struct ring_buffer_event *event,
820 unsigned type, unsigned length)
821{
822 event->type = type;
823
824 switch (type) {
825
826 case RINGBUF_TYPE_PADDING:
827 break;
828
829 case RINGBUF_TYPE_TIME_EXTEND:
830 event->len =
831 (RB_LEN_TIME_EXTEND + (RB_ALIGNMENT-1))
832 >> RB_ALIGNMENT_SHIFT;
833 break;
834
835 case RINGBUF_TYPE_TIME_STAMP:
836 event->len =
837 (RB_LEN_TIME_STAMP + (RB_ALIGNMENT-1))
838 >> RB_ALIGNMENT_SHIFT;
839 break;
840
841 case RINGBUF_TYPE_DATA:
842 length -= RB_EVNT_HDR_SIZE;
843 if (length > RB_MAX_SMALL_DATA) {
844 event->len = 0;
845 event->array[0] = length;
846 } else
847 event->len =
848 (length + (RB_ALIGNMENT-1))
849 >> RB_ALIGNMENT_SHIFT;
850 break;
851 default:
852 BUG();
853 }
854}
855
856static inline unsigned rb_calculate_event_length(unsigned length)
857{
858 struct ring_buffer_event event; /* Used only for sizeof array */
859
860 /* zero length can cause confusions */
861 if (!length)
862 length = 1;
863
864 if (length > RB_MAX_SMALL_DATA)
865 length += sizeof(event.array[0]);
866
867 length += RB_EVNT_HDR_SIZE;
868 length = ALIGN(length, RB_ALIGNMENT);
869
870 return length;
871}
872
873static struct ring_buffer_event *
874__rb_reserve_next(struct ring_buffer_per_cpu *cpu_buffer,
875 unsigned type, unsigned long length, u64 *ts)
876{
Steven Rostedtd7690412008-10-01 00:29:53 -0400877 struct buffer_page *tail_page, *head_page, *reader_page;
Steven Rostedtbf41a152008-10-04 02:00:59 -0400878 unsigned long tail, write;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400879 struct ring_buffer *buffer = cpu_buffer->buffer;
880 struct ring_buffer_event *event;
Steven Rostedtbf41a152008-10-04 02:00:59 -0400881 unsigned long flags;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400882
883 tail_page = cpu_buffer->tail_page;
Steven Rostedtbf41a152008-10-04 02:00:59 -0400884 write = local_add_return(length, &tail_page->write);
885 tail = write - length;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400886
Steven Rostedtbf41a152008-10-04 02:00:59 -0400887 /* See if we shot pass the end of this buffer page */
888 if (write > BUF_PAGE_SIZE) {
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400889 struct buffer_page *next_page = tail_page;
890
Steven Rostedt3e03fb72008-11-06 00:09:43 -0500891 local_irq_save(flags);
892 __raw_spin_lock(&cpu_buffer->lock);
Steven Rostedtbf41a152008-10-04 02:00:59 -0400893
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400894 rb_inc_page(cpu_buffer, &next_page);
895
Steven Rostedtd7690412008-10-01 00:29:53 -0400896 head_page = cpu_buffer->head_page;
897 reader_page = cpu_buffer->reader_page;
898
899 /* we grabbed the lock before incrementing */
Steven Rostedt3e89c7bb2008-11-11 15:28:41 -0500900 if (RB_WARN_ON(cpu_buffer, next_page == reader_page))
901 goto out_unlock;
Steven Rostedtbf41a152008-10-04 02:00:59 -0400902
903 /*
904 * If for some reason, we had an interrupt storm that made
905 * it all the way around the buffer, bail, and warn
906 * about it.
907 */
908 if (unlikely(next_page == cpu_buffer->commit_page)) {
909 WARN_ON_ONCE(1);
910 goto out_unlock;
911 }
Steven Rostedtd7690412008-10-01 00:29:53 -0400912
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400913 if (next_page == head_page) {
Steven Rostedtd7690412008-10-01 00:29:53 -0400914 if (!(buffer->flags & RB_FL_OVERWRITE)) {
Steven Rostedtbf41a152008-10-04 02:00:59 -0400915 /* reset write */
916 if (tail <= BUF_PAGE_SIZE)
917 local_set(&tail_page->write, tail);
918 goto out_unlock;
Steven Rostedtd7690412008-10-01 00:29:53 -0400919 }
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400920
Steven Rostedtbf41a152008-10-04 02:00:59 -0400921 /* tail_page has not moved yet? */
922 if (tail_page == cpu_buffer->tail_page) {
923 /* count overflows */
924 rb_update_overflow(cpu_buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400925
Steven Rostedtbf41a152008-10-04 02:00:59 -0400926 rb_inc_page(cpu_buffer, &head_page);
927 cpu_buffer->head_page = head_page;
928 cpu_buffer->head_page->read = 0;
929 }
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400930 }
931
Steven Rostedtbf41a152008-10-04 02:00:59 -0400932 /*
933 * If the tail page is still the same as what we think
934 * it is, then it is up to us to update the tail
935 * pointer.
936 */
937 if (tail_page == cpu_buffer->tail_page) {
938 local_set(&next_page->write, 0);
939 local_set(&next_page->commit, 0);
940 cpu_buffer->tail_page = next_page;
941
942 /* reread the time stamp */
943 *ts = ring_buffer_time_stamp(cpu_buffer->cpu);
944 cpu_buffer->tail_page->time_stamp = *ts;
945 }
946
947 /*
948 * The actual tail page has moved forward.
949 */
950 if (tail < BUF_PAGE_SIZE) {
951 /* Mark the rest of the page with padding */
Steven Rostedt6f807ac2008-10-04 02:00:58 -0400952 event = __rb_page_index(tail_page, tail);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400953 event->type = RINGBUF_TYPE_PADDING;
954 }
955
Steven Rostedtbf41a152008-10-04 02:00:59 -0400956 if (tail <= BUF_PAGE_SIZE)
957 /* Set the write back to the previous setting */
958 local_set(&tail_page->write, tail);
959
960 /*
961 * If this was a commit entry that failed,
962 * increment that too
963 */
964 if (tail_page == cpu_buffer->commit_page &&
965 tail == rb_commit_index(cpu_buffer)) {
966 rb_set_commit_to_write(cpu_buffer);
967 }
968
Steven Rostedt3e03fb72008-11-06 00:09:43 -0500969 __raw_spin_unlock(&cpu_buffer->lock);
970 local_irq_restore(flags);
Steven Rostedtbf41a152008-10-04 02:00:59 -0400971
972 /* fail and let the caller try again */
973 return ERR_PTR(-EAGAIN);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400974 }
975
Steven Rostedtbf41a152008-10-04 02:00:59 -0400976 /* We reserved something on the buffer */
977
Steven Rostedt3e89c7bb2008-11-11 15:28:41 -0500978 if (RB_WARN_ON(cpu_buffer, write > BUF_PAGE_SIZE))
979 return NULL;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400980
Steven Rostedt6f807ac2008-10-04 02:00:58 -0400981 event = __rb_page_index(tail_page, tail);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400982 rb_update_event(event, type, length);
983
Steven Rostedtbf41a152008-10-04 02:00:59 -0400984 /*
985 * If this is a commit and the tail is zero, then update
986 * this page's time stamp.
987 */
988 if (!tail && rb_is_commit(cpu_buffer, event))
989 cpu_buffer->commit_page->time_stamp = *ts;
990
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400991 return event;
Steven Rostedtbf41a152008-10-04 02:00:59 -0400992
993 out_unlock:
Steven Rostedt3e03fb72008-11-06 00:09:43 -0500994 __raw_spin_unlock(&cpu_buffer->lock);
995 local_irq_restore(flags);
Steven Rostedtbf41a152008-10-04 02:00:59 -0400996 return NULL;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400997}
998
999static int
1000rb_add_time_stamp(struct ring_buffer_per_cpu *cpu_buffer,
1001 u64 *ts, u64 *delta)
1002{
1003 struct ring_buffer_event *event;
1004 static int once;
Steven Rostedtbf41a152008-10-04 02:00:59 -04001005 int ret;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001006
1007 if (unlikely(*delta > (1ULL << 59) && !once++)) {
1008 printk(KERN_WARNING "Delta way too big! %llu"
1009 " ts=%llu write stamp = %llu\n",
Stephen Rothwelle2862c92008-10-27 17:43:28 +11001010 (unsigned long long)*delta,
1011 (unsigned long long)*ts,
1012 (unsigned long long)cpu_buffer->write_stamp);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001013 WARN_ON(1);
1014 }
1015
1016 /*
1017 * The delta is too big, we to add a
1018 * new timestamp.
1019 */
1020 event = __rb_reserve_next(cpu_buffer,
1021 RINGBUF_TYPE_TIME_EXTEND,
1022 RB_LEN_TIME_EXTEND,
1023 ts);
1024 if (!event)
Steven Rostedtbf41a152008-10-04 02:00:59 -04001025 return -EBUSY;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001026
Steven Rostedtbf41a152008-10-04 02:00:59 -04001027 if (PTR_ERR(event) == -EAGAIN)
1028 return -EAGAIN;
1029
1030 /* Only a commited time event can update the write stamp */
1031 if (rb_is_commit(cpu_buffer, event)) {
1032 /*
1033 * If this is the first on the page, then we need to
1034 * update the page itself, and just put in a zero.
1035 */
1036 if (rb_event_index(event)) {
1037 event->time_delta = *delta & TS_MASK;
1038 event->array[0] = *delta >> TS_SHIFT;
1039 } else {
1040 cpu_buffer->commit_page->time_stamp = *ts;
1041 event->time_delta = 0;
1042 event->array[0] = 0;
1043 }
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001044 cpu_buffer->write_stamp = *ts;
Steven Rostedtbf41a152008-10-04 02:00:59 -04001045 /* let the caller know this was the commit */
1046 ret = 1;
1047 } else {
1048 /* Darn, this is just wasted space */
1049 event->time_delta = 0;
1050 event->array[0] = 0;
1051 ret = 0;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001052 }
1053
Steven Rostedtbf41a152008-10-04 02:00:59 -04001054 *delta = 0;
1055
1056 return ret;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001057}
1058
1059static struct ring_buffer_event *
1060rb_reserve_next_event(struct ring_buffer_per_cpu *cpu_buffer,
1061 unsigned type, unsigned long length)
1062{
1063 struct ring_buffer_event *event;
1064 u64 ts, delta;
Steven Rostedtbf41a152008-10-04 02:00:59 -04001065 int commit = 0;
Steven Rostedt818e3dd2008-10-31 09:58:35 -04001066 int nr_loops = 0;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001067
Steven Rostedtbf41a152008-10-04 02:00:59 -04001068 again:
Steven Rostedt818e3dd2008-10-31 09:58:35 -04001069 /*
1070 * We allow for interrupts to reenter here and do a trace.
1071 * If one does, it will cause this original code to loop
1072 * back here. Even with heavy interrupts happening, this
1073 * should only happen a few times in a row. If this happens
1074 * 1000 times in a row, there must be either an interrupt
1075 * storm or we have something buggy.
1076 * Bail!
1077 */
Steven Rostedt3e89c7bb2008-11-11 15:28:41 -05001078 if (RB_WARN_ON(cpu_buffer, ++nr_loops > 1000))
Steven Rostedt818e3dd2008-10-31 09:58:35 -04001079 return NULL;
Steven Rostedt818e3dd2008-10-31 09:58:35 -04001080
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001081 ts = ring_buffer_time_stamp(cpu_buffer->cpu);
1082
Steven Rostedtbf41a152008-10-04 02:00:59 -04001083 /*
1084 * Only the first commit can update the timestamp.
1085 * Yes there is a race here. If an interrupt comes in
1086 * just after the conditional and it traces too, then it
1087 * will also check the deltas. More than one timestamp may
1088 * also be made. But only the entry that did the actual
1089 * commit will be something other than zero.
1090 */
1091 if (cpu_buffer->tail_page == cpu_buffer->commit_page &&
1092 rb_page_write(cpu_buffer->tail_page) ==
1093 rb_commit_index(cpu_buffer)) {
1094
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001095 delta = ts - cpu_buffer->write_stamp;
1096
Steven Rostedtbf41a152008-10-04 02:00:59 -04001097 /* make sure this delta is calculated here */
1098 barrier();
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001099
Steven Rostedtbf41a152008-10-04 02:00:59 -04001100 /* Did the write stamp get updated already? */
1101 if (unlikely(ts < cpu_buffer->write_stamp))
Steven Rostedt4143c5c2008-11-10 21:46:01 -05001102 delta = 0;
Steven Rostedtbf41a152008-10-04 02:00:59 -04001103
1104 if (test_time_stamp(delta)) {
1105
1106 commit = rb_add_time_stamp(cpu_buffer, &ts, &delta);
1107
1108 if (commit == -EBUSY)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001109 return NULL;
Steven Rostedtbf41a152008-10-04 02:00:59 -04001110
1111 if (commit == -EAGAIN)
1112 goto again;
1113
1114 RB_WARN_ON(cpu_buffer, commit < 0);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001115 }
Steven Rostedtbf41a152008-10-04 02:00:59 -04001116 } else
1117 /* Non commits have zero deltas */
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001118 delta = 0;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001119
1120 event = __rb_reserve_next(cpu_buffer, type, length, &ts);
Steven Rostedtbf41a152008-10-04 02:00:59 -04001121 if (PTR_ERR(event) == -EAGAIN)
1122 goto again;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001123
Steven Rostedtbf41a152008-10-04 02:00:59 -04001124 if (!event) {
1125 if (unlikely(commit))
1126 /*
1127 * Ouch! We needed a timestamp and it was commited. But
1128 * we didn't get our event reserved.
1129 */
1130 rb_set_commit_to_write(cpu_buffer);
1131 return NULL;
1132 }
1133
1134 /*
1135 * If the timestamp was commited, make the commit our entry
1136 * now so that we will update it when needed.
1137 */
1138 if (commit)
1139 rb_set_commit_event(cpu_buffer, event);
1140 else if (!rb_is_commit(cpu_buffer, event))
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001141 delta = 0;
1142
1143 event->time_delta = delta;
1144
1145 return event;
1146}
1147
Steven Rostedtbf41a152008-10-04 02:00:59 -04001148static DEFINE_PER_CPU(int, rb_need_resched);
1149
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001150/**
1151 * ring_buffer_lock_reserve - reserve a part of the buffer
1152 * @buffer: the ring buffer to reserve from
1153 * @length: the length of the data to reserve (excluding event header)
1154 * @flags: a pointer to save the interrupt flags
1155 *
1156 * Returns a reseverd event on the ring buffer to copy directly to.
1157 * The user of this interface will need to get the body to write into
1158 * and can use the ring_buffer_event_data() interface.
1159 *
1160 * The length is the length of the data needed, not the event length
1161 * which also includes the event header.
1162 *
1163 * Must be paired with ring_buffer_unlock_commit, unless NULL is returned.
1164 * If NULL is returned, then nothing has been allocated or locked.
1165 */
1166struct ring_buffer_event *
1167ring_buffer_lock_reserve(struct ring_buffer *buffer,
1168 unsigned long length,
1169 unsigned long *flags)
1170{
1171 struct ring_buffer_per_cpu *cpu_buffer;
1172 struct ring_buffer_event *event;
Steven Rostedtbf41a152008-10-04 02:00:59 -04001173 int cpu, resched;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001174
Steven Rostedta3583242008-11-11 15:01:42 -05001175 if (ring_buffers_off)
1176 return NULL;
1177
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001178 if (atomic_read(&buffer->record_disabled))
1179 return NULL;
1180
Steven Rostedtbf41a152008-10-04 02:00:59 -04001181 /* If we are tracing schedule, we don't want to recurse */
Steven Rostedt182e9f52008-11-03 23:15:56 -05001182 resched = ftrace_preempt_disable();
Steven Rostedtbf41a152008-10-04 02:00:59 -04001183
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001184 cpu = raw_smp_processor_id();
1185
1186 if (!cpu_isset(cpu, buffer->cpumask))
Steven Rostedtd7690412008-10-01 00:29:53 -04001187 goto out;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001188
1189 cpu_buffer = buffer->buffers[cpu];
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001190
1191 if (atomic_read(&cpu_buffer->record_disabled))
Steven Rostedtd7690412008-10-01 00:29:53 -04001192 goto out;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001193
1194 length = rb_calculate_event_length(length);
1195 if (length > BUF_PAGE_SIZE)
Steven Rostedtbf41a152008-10-04 02:00:59 -04001196 goto out;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001197
1198 event = rb_reserve_next_event(cpu_buffer, RINGBUF_TYPE_DATA, length);
1199 if (!event)
Steven Rostedtd7690412008-10-01 00:29:53 -04001200 goto out;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001201
Steven Rostedtbf41a152008-10-04 02:00:59 -04001202 /*
1203 * Need to store resched state on this cpu.
1204 * Only the first needs to.
1205 */
1206
1207 if (preempt_count() == 1)
1208 per_cpu(rb_need_resched, cpu) = resched;
1209
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001210 return event;
1211
Steven Rostedtd7690412008-10-01 00:29:53 -04001212 out:
Steven Rostedt182e9f52008-11-03 23:15:56 -05001213 ftrace_preempt_enable(resched);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001214 return NULL;
1215}
1216
1217static void rb_commit(struct ring_buffer_per_cpu *cpu_buffer,
1218 struct ring_buffer_event *event)
1219{
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001220 cpu_buffer->entries++;
Steven Rostedtbf41a152008-10-04 02:00:59 -04001221
1222 /* Only process further if we own the commit */
1223 if (!rb_is_commit(cpu_buffer, event))
1224 return;
1225
1226 cpu_buffer->write_stamp += event->time_delta;
1227
1228 rb_set_commit_to_write(cpu_buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001229}
1230
1231/**
1232 * ring_buffer_unlock_commit - commit a reserved
1233 * @buffer: The buffer to commit to
1234 * @event: The event pointer to commit.
1235 * @flags: the interrupt flags received from ring_buffer_lock_reserve.
1236 *
1237 * This commits the data to the ring buffer, and releases any locks held.
1238 *
1239 * Must be paired with ring_buffer_lock_reserve.
1240 */
1241int ring_buffer_unlock_commit(struct ring_buffer *buffer,
1242 struct ring_buffer_event *event,
1243 unsigned long flags)
1244{
1245 struct ring_buffer_per_cpu *cpu_buffer;
1246 int cpu = raw_smp_processor_id();
1247
1248 cpu_buffer = buffer->buffers[cpu];
1249
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001250 rb_commit(cpu_buffer, event);
1251
Steven Rostedtbf41a152008-10-04 02:00:59 -04001252 /*
1253 * Only the last preempt count needs to restore preemption.
1254 */
Steven Rostedt182e9f52008-11-03 23:15:56 -05001255 if (preempt_count() == 1)
1256 ftrace_preempt_enable(per_cpu(rb_need_resched, cpu));
1257 else
Steven Rostedtbf41a152008-10-04 02:00:59 -04001258 preempt_enable_no_resched_notrace();
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001259
1260 return 0;
1261}
1262
1263/**
1264 * ring_buffer_write - write data to the buffer without reserving
1265 * @buffer: The ring buffer to write to.
1266 * @length: The length of the data being written (excluding the event header)
1267 * @data: The data to write to the buffer.
1268 *
1269 * This is like ring_buffer_lock_reserve and ring_buffer_unlock_commit as
1270 * one function. If you already have the data to write to the buffer, it
1271 * may be easier to simply call this function.
1272 *
1273 * Note, like ring_buffer_lock_reserve, the length is the length of the data
1274 * and not the length of the event which would hold the header.
1275 */
1276int ring_buffer_write(struct ring_buffer *buffer,
1277 unsigned long length,
1278 void *data)
1279{
1280 struct ring_buffer_per_cpu *cpu_buffer;
1281 struct ring_buffer_event *event;
Steven Rostedtbf41a152008-10-04 02:00:59 -04001282 unsigned long event_length;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001283 void *body;
1284 int ret = -EBUSY;
Steven Rostedtbf41a152008-10-04 02:00:59 -04001285 int cpu, resched;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001286
Steven Rostedta3583242008-11-11 15:01:42 -05001287 if (ring_buffers_off)
1288 return -EBUSY;
1289
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001290 if (atomic_read(&buffer->record_disabled))
1291 return -EBUSY;
1292
Steven Rostedt182e9f52008-11-03 23:15:56 -05001293 resched = ftrace_preempt_disable();
Steven Rostedtbf41a152008-10-04 02:00:59 -04001294
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001295 cpu = raw_smp_processor_id();
1296
1297 if (!cpu_isset(cpu, buffer->cpumask))
Steven Rostedtd7690412008-10-01 00:29:53 -04001298 goto out;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001299
1300 cpu_buffer = buffer->buffers[cpu];
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001301
1302 if (atomic_read(&cpu_buffer->record_disabled))
1303 goto out;
1304
1305 event_length = rb_calculate_event_length(length);
1306 event = rb_reserve_next_event(cpu_buffer,
1307 RINGBUF_TYPE_DATA, event_length);
1308 if (!event)
1309 goto out;
1310
1311 body = rb_event_data(event);
1312
1313 memcpy(body, data, length);
1314
1315 rb_commit(cpu_buffer, event);
1316
1317 ret = 0;
1318 out:
Steven Rostedt182e9f52008-11-03 23:15:56 -05001319 ftrace_preempt_enable(resched);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001320
1321 return ret;
1322}
1323
Steven Rostedtbf41a152008-10-04 02:00:59 -04001324static inline int rb_per_cpu_empty(struct ring_buffer_per_cpu *cpu_buffer)
1325{
1326 struct buffer_page *reader = cpu_buffer->reader_page;
1327 struct buffer_page *head = cpu_buffer->head_page;
1328 struct buffer_page *commit = cpu_buffer->commit_page;
1329
1330 return reader->read == rb_page_commit(reader) &&
1331 (commit == reader ||
1332 (commit == head &&
1333 head->read == rb_page_commit(commit)));
1334}
1335
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001336/**
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001337 * ring_buffer_record_disable - stop all writes into the buffer
1338 * @buffer: The ring buffer to stop writes to.
1339 *
1340 * This prevents all writes to the buffer. Any attempt to write
1341 * to the buffer after this will fail and return NULL.
1342 *
1343 * The caller should call synchronize_sched() after this.
1344 */
1345void ring_buffer_record_disable(struct ring_buffer *buffer)
1346{
1347 atomic_inc(&buffer->record_disabled);
1348}
1349
1350/**
1351 * ring_buffer_record_enable - enable writes to the buffer
1352 * @buffer: The ring buffer to enable writes
1353 *
1354 * Note, multiple disables will need the same number of enables
1355 * to truely enable the writing (much like preempt_disable).
1356 */
1357void ring_buffer_record_enable(struct ring_buffer *buffer)
1358{
1359 atomic_dec(&buffer->record_disabled);
1360}
1361
1362/**
1363 * ring_buffer_record_disable_cpu - stop all writes into the cpu_buffer
1364 * @buffer: The ring buffer to stop writes to.
1365 * @cpu: The CPU buffer to stop
1366 *
1367 * This prevents all writes to the buffer. Any attempt to write
1368 * to the buffer after this will fail and return NULL.
1369 *
1370 * The caller should call synchronize_sched() after this.
1371 */
1372void ring_buffer_record_disable_cpu(struct ring_buffer *buffer, int cpu)
1373{
1374 struct ring_buffer_per_cpu *cpu_buffer;
1375
1376 if (!cpu_isset(cpu, buffer->cpumask))
1377 return;
1378
1379 cpu_buffer = buffer->buffers[cpu];
1380 atomic_inc(&cpu_buffer->record_disabled);
1381}
1382
1383/**
1384 * ring_buffer_record_enable_cpu - enable writes to the buffer
1385 * @buffer: The ring buffer to enable writes
1386 * @cpu: The CPU to enable.
1387 *
1388 * Note, multiple disables will need the same number of enables
1389 * to truely enable the writing (much like preempt_disable).
1390 */
1391void ring_buffer_record_enable_cpu(struct ring_buffer *buffer, int cpu)
1392{
1393 struct ring_buffer_per_cpu *cpu_buffer;
1394
1395 if (!cpu_isset(cpu, buffer->cpumask))
1396 return;
1397
1398 cpu_buffer = buffer->buffers[cpu];
1399 atomic_dec(&cpu_buffer->record_disabled);
1400}
1401
1402/**
1403 * ring_buffer_entries_cpu - get the number of entries in a cpu buffer
1404 * @buffer: The ring buffer
1405 * @cpu: The per CPU buffer to get the entries from.
1406 */
1407unsigned long ring_buffer_entries_cpu(struct ring_buffer *buffer, int cpu)
1408{
1409 struct ring_buffer_per_cpu *cpu_buffer;
1410
1411 if (!cpu_isset(cpu, buffer->cpumask))
1412 return 0;
1413
1414 cpu_buffer = buffer->buffers[cpu];
1415 return cpu_buffer->entries;
1416}
1417
1418/**
1419 * ring_buffer_overrun_cpu - get the number of overruns in a cpu_buffer
1420 * @buffer: The ring buffer
1421 * @cpu: The per CPU buffer to get the number of overruns from
1422 */
1423unsigned long ring_buffer_overrun_cpu(struct ring_buffer *buffer, int cpu)
1424{
1425 struct ring_buffer_per_cpu *cpu_buffer;
1426
1427 if (!cpu_isset(cpu, buffer->cpumask))
1428 return 0;
1429
1430 cpu_buffer = buffer->buffers[cpu];
1431 return cpu_buffer->overrun;
1432}
1433
1434/**
1435 * ring_buffer_entries - get the number of entries in a buffer
1436 * @buffer: The ring buffer
1437 *
1438 * Returns the total number of entries in the ring buffer
1439 * (all CPU entries)
1440 */
1441unsigned long ring_buffer_entries(struct ring_buffer *buffer)
1442{
1443 struct ring_buffer_per_cpu *cpu_buffer;
1444 unsigned long entries = 0;
1445 int cpu;
1446
1447 /* if you care about this being correct, lock the buffer */
1448 for_each_buffer_cpu(buffer, cpu) {
1449 cpu_buffer = buffer->buffers[cpu];
1450 entries += cpu_buffer->entries;
1451 }
1452
1453 return entries;
1454}
1455
1456/**
1457 * ring_buffer_overrun_cpu - get the number of overruns in buffer
1458 * @buffer: The ring buffer
1459 *
1460 * Returns the total number of overruns in the ring buffer
1461 * (all CPU entries)
1462 */
1463unsigned long ring_buffer_overruns(struct ring_buffer *buffer)
1464{
1465 struct ring_buffer_per_cpu *cpu_buffer;
1466 unsigned long overruns = 0;
1467 int cpu;
1468
1469 /* if you care about this being correct, lock the buffer */
1470 for_each_buffer_cpu(buffer, cpu) {
1471 cpu_buffer = buffer->buffers[cpu];
1472 overruns += cpu_buffer->overrun;
1473 }
1474
1475 return overruns;
1476}
1477
Steven Rostedt642edba2008-11-12 00:01:26 -05001478static void rb_iter_reset(struct ring_buffer_iter *iter)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001479{
1480 struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
1481
Steven Rostedtd7690412008-10-01 00:29:53 -04001482 /* Iterator usage is expected to have record disabled */
1483 if (list_empty(&cpu_buffer->reader_page->list)) {
1484 iter->head_page = cpu_buffer->head_page;
Steven Rostedt6f807ac2008-10-04 02:00:58 -04001485 iter->head = cpu_buffer->head_page->read;
Steven Rostedtd7690412008-10-01 00:29:53 -04001486 } else {
1487 iter->head_page = cpu_buffer->reader_page;
Steven Rostedt6f807ac2008-10-04 02:00:58 -04001488 iter->head = cpu_buffer->reader_page->read;
Steven Rostedtd7690412008-10-01 00:29:53 -04001489 }
1490 if (iter->head)
1491 iter->read_stamp = cpu_buffer->read_stamp;
1492 else
1493 iter->read_stamp = iter->head_page->time_stamp;
Steven Rostedt642edba2008-11-12 00:01:26 -05001494}
Steven Rostedtf83c9d02008-11-11 18:47:44 +01001495
Steven Rostedt642edba2008-11-12 00:01:26 -05001496/**
1497 * ring_buffer_iter_reset - reset an iterator
1498 * @iter: The iterator to reset
1499 *
1500 * Resets the iterator, so that it will start from the beginning
1501 * again.
1502 */
1503void ring_buffer_iter_reset(struct ring_buffer_iter *iter)
1504{
1505 struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
1506 unsigned long flags;
1507
1508 spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
1509 rb_iter_reset(iter);
Steven Rostedtf83c9d02008-11-11 18:47:44 +01001510 spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001511}
1512
1513/**
1514 * ring_buffer_iter_empty - check if an iterator has no more to read
1515 * @iter: The iterator to check
1516 */
1517int ring_buffer_iter_empty(struct ring_buffer_iter *iter)
1518{
1519 struct ring_buffer_per_cpu *cpu_buffer;
1520
1521 cpu_buffer = iter->cpu_buffer;
1522
Steven Rostedtbf41a152008-10-04 02:00:59 -04001523 return iter->head_page == cpu_buffer->commit_page &&
1524 iter->head == rb_commit_index(cpu_buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001525}
1526
1527static void
1528rb_update_read_stamp(struct ring_buffer_per_cpu *cpu_buffer,
1529 struct ring_buffer_event *event)
1530{
1531 u64 delta;
1532
1533 switch (event->type) {
1534 case RINGBUF_TYPE_PADDING:
1535 return;
1536
1537 case RINGBUF_TYPE_TIME_EXTEND:
1538 delta = event->array[0];
1539 delta <<= TS_SHIFT;
1540 delta += event->time_delta;
1541 cpu_buffer->read_stamp += delta;
1542 return;
1543
1544 case RINGBUF_TYPE_TIME_STAMP:
1545 /* FIXME: not implemented */
1546 return;
1547
1548 case RINGBUF_TYPE_DATA:
1549 cpu_buffer->read_stamp += event->time_delta;
1550 return;
1551
1552 default:
1553 BUG();
1554 }
1555 return;
1556}
1557
1558static void
1559rb_update_iter_read_stamp(struct ring_buffer_iter *iter,
1560 struct ring_buffer_event *event)
1561{
1562 u64 delta;
1563
1564 switch (event->type) {
1565 case RINGBUF_TYPE_PADDING:
1566 return;
1567
1568 case RINGBUF_TYPE_TIME_EXTEND:
1569 delta = event->array[0];
1570 delta <<= TS_SHIFT;
1571 delta += event->time_delta;
1572 iter->read_stamp += delta;
1573 return;
1574
1575 case RINGBUF_TYPE_TIME_STAMP:
1576 /* FIXME: not implemented */
1577 return;
1578
1579 case RINGBUF_TYPE_DATA:
1580 iter->read_stamp += event->time_delta;
1581 return;
1582
1583 default:
1584 BUG();
1585 }
1586 return;
1587}
1588
Steven Rostedtd7690412008-10-01 00:29:53 -04001589static struct buffer_page *
1590rb_get_reader_page(struct ring_buffer_per_cpu *cpu_buffer)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001591{
Steven Rostedtd7690412008-10-01 00:29:53 -04001592 struct buffer_page *reader = NULL;
1593 unsigned long flags;
Steven Rostedt818e3dd2008-10-31 09:58:35 -04001594 int nr_loops = 0;
Steven Rostedtd7690412008-10-01 00:29:53 -04001595
Steven Rostedt3e03fb72008-11-06 00:09:43 -05001596 local_irq_save(flags);
1597 __raw_spin_lock(&cpu_buffer->lock);
Steven Rostedtd7690412008-10-01 00:29:53 -04001598
1599 again:
Steven Rostedt818e3dd2008-10-31 09:58:35 -04001600 /*
1601 * This should normally only loop twice. But because the
1602 * start of the reader inserts an empty page, it causes
1603 * a case where we will loop three times. There should be no
1604 * reason to loop four times (that I know of).
1605 */
Steven Rostedt3e89c7bb2008-11-11 15:28:41 -05001606 if (RB_WARN_ON(cpu_buffer, ++nr_loops > 3)) {
Steven Rostedt818e3dd2008-10-31 09:58:35 -04001607 reader = NULL;
1608 goto out;
1609 }
1610
Steven Rostedtd7690412008-10-01 00:29:53 -04001611 reader = cpu_buffer->reader_page;
1612
1613 /* If there's more to read, return this page */
Steven Rostedtbf41a152008-10-04 02:00:59 -04001614 if (cpu_buffer->reader_page->read < rb_page_size(reader))
Steven Rostedtd7690412008-10-01 00:29:53 -04001615 goto out;
1616
1617 /* Never should we have an index greater than the size */
Steven Rostedt3e89c7bb2008-11-11 15:28:41 -05001618 if (RB_WARN_ON(cpu_buffer,
1619 cpu_buffer->reader_page->read > rb_page_size(reader)))
1620 goto out;
Steven Rostedtd7690412008-10-01 00:29:53 -04001621
1622 /* check if we caught up to the tail */
1623 reader = NULL;
Steven Rostedtbf41a152008-10-04 02:00:59 -04001624 if (cpu_buffer->commit_page == cpu_buffer->reader_page)
Steven Rostedtd7690412008-10-01 00:29:53 -04001625 goto out;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001626
1627 /*
Steven Rostedtd7690412008-10-01 00:29:53 -04001628 * Splice the empty reader page into the list around the head.
1629 * Reset the reader page to size zero.
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001630 */
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001631
Steven Rostedtd7690412008-10-01 00:29:53 -04001632 reader = cpu_buffer->head_page;
1633 cpu_buffer->reader_page->list.next = reader->list.next;
1634 cpu_buffer->reader_page->list.prev = reader->list.prev;
Steven Rostedtbf41a152008-10-04 02:00:59 -04001635
1636 local_set(&cpu_buffer->reader_page->write, 0);
1637 local_set(&cpu_buffer->reader_page->commit, 0);
Steven Rostedtd7690412008-10-01 00:29:53 -04001638
1639 /* Make the reader page now replace the head */
1640 reader->list.prev->next = &cpu_buffer->reader_page->list;
1641 reader->list.next->prev = &cpu_buffer->reader_page->list;
1642
1643 /*
1644 * If the tail is on the reader, then we must set the head
1645 * to the inserted page, otherwise we set it one before.
1646 */
1647 cpu_buffer->head_page = cpu_buffer->reader_page;
1648
Steven Rostedtbf41a152008-10-04 02:00:59 -04001649 if (cpu_buffer->commit_page != reader)
Steven Rostedtd7690412008-10-01 00:29:53 -04001650 rb_inc_page(cpu_buffer, &cpu_buffer->head_page);
1651
1652 /* Finally update the reader page to the new head */
1653 cpu_buffer->reader_page = reader;
1654 rb_reset_reader_page(cpu_buffer);
1655
1656 goto again;
1657
1658 out:
Steven Rostedt3e03fb72008-11-06 00:09:43 -05001659 __raw_spin_unlock(&cpu_buffer->lock);
1660 local_irq_restore(flags);
Steven Rostedtd7690412008-10-01 00:29:53 -04001661
1662 return reader;
1663}
1664
1665static void rb_advance_reader(struct ring_buffer_per_cpu *cpu_buffer)
1666{
1667 struct ring_buffer_event *event;
1668 struct buffer_page *reader;
1669 unsigned length;
1670
1671 reader = rb_get_reader_page(cpu_buffer);
1672
1673 /* This function should not be called when buffer is empty */
Steven Rostedt3e89c7bb2008-11-11 15:28:41 -05001674 if (RB_WARN_ON(cpu_buffer, !reader))
1675 return;
Steven Rostedtd7690412008-10-01 00:29:53 -04001676
1677 event = rb_reader_event(cpu_buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001678
1679 if (event->type == RINGBUF_TYPE_DATA)
1680 cpu_buffer->entries--;
1681
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001682 rb_update_read_stamp(cpu_buffer, event);
1683
Steven Rostedtd7690412008-10-01 00:29:53 -04001684 length = rb_event_length(event);
Steven Rostedt6f807ac2008-10-04 02:00:58 -04001685 cpu_buffer->reader_page->read += length;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001686}
1687
1688static void rb_advance_iter(struct ring_buffer_iter *iter)
1689{
1690 struct ring_buffer *buffer;
1691 struct ring_buffer_per_cpu *cpu_buffer;
1692 struct ring_buffer_event *event;
1693 unsigned length;
1694
1695 cpu_buffer = iter->cpu_buffer;
1696 buffer = cpu_buffer->buffer;
1697
1698 /*
1699 * Check if we are at the end of the buffer.
1700 */
Steven Rostedtbf41a152008-10-04 02:00:59 -04001701 if (iter->head >= rb_page_size(iter->head_page)) {
Steven Rostedt3e89c7bb2008-11-11 15:28:41 -05001702 if (RB_WARN_ON(buffer,
1703 iter->head_page == cpu_buffer->commit_page))
1704 return;
Steven Rostedtd7690412008-10-01 00:29:53 -04001705 rb_inc_iter(iter);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001706 return;
1707 }
1708
1709 event = rb_iter_head_event(iter);
1710
1711 length = rb_event_length(event);
1712
1713 /*
1714 * This should not be called to advance the header if we are
1715 * at the tail of the buffer.
1716 */
Steven Rostedt3e89c7bb2008-11-11 15:28:41 -05001717 if (RB_WARN_ON(cpu_buffer,
Steven Rostedtf536aaf2008-11-10 23:07:30 -05001718 (iter->head_page == cpu_buffer->commit_page) &&
Steven Rostedt3e89c7bb2008-11-11 15:28:41 -05001719 (iter->head + length > rb_commit_index(cpu_buffer))))
1720 return;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001721
1722 rb_update_iter_read_stamp(iter, event);
1723
1724 iter->head += length;
1725
1726 /* check for end of page padding */
Steven Rostedtbf41a152008-10-04 02:00:59 -04001727 if ((iter->head >= rb_page_size(iter->head_page)) &&
1728 (iter->head_page != cpu_buffer->commit_page))
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001729 rb_advance_iter(iter);
1730}
1731
Steven Rostedtf83c9d02008-11-11 18:47:44 +01001732static struct ring_buffer_event *
1733rb_buffer_peek(struct ring_buffer *buffer, int cpu, u64 *ts)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001734{
1735 struct ring_buffer_per_cpu *cpu_buffer;
1736 struct ring_buffer_event *event;
Steven Rostedtd7690412008-10-01 00:29:53 -04001737 struct buffer_page *reader;
Steven Rostedt818e3dd2008-10-31 09:58:35 -04001738 int nr_loops = 0;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001739
1740 if (!cpu_isset(cpu, buffer->cpumask))
1741 return NULL;
1742
1743 cpu_buffer = buffer->buffers[cpu];
1744
1745 again:
Steven Rostedt818e3dd2008-10-31 09:58:35 -04001746 /*
1747 * We repeat when a timestamp is encountered. It is possible
1748 * to get multiple timestamps from an interrupt entering just
1749 * as one timestamp is about to be written. The max times
1750 * that this can happen is the number of nested interrupts we
1751 * can have. Nesting 10 deep of interrupts is clearly
1752 * an anomaly.
1753 */
Steven Rostedt3e89c7bb2008-11-11 15:28:41 -05001754 if (RB_WARN_ON(cpu_buffer, ++nr_loops > 10))
Steven Rostedt818e3dd2008-10-31 09:58:35 -04001755 return NULL;
Steven Rostedt818e3dd2008-10-31 09:58:35 -04001756
Steven Rostedtd7690412008-10-01 00:29:53 -04001757 reader = rb_get_reader_page(cpu_buffer);
1758 if (!reader)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001759 return NULL;
1760
Steven Rostedtd7690412008-10-01 00:29:53 -04001761 event = rb_reader_event(cpu_buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001762
1763 switch (event->type) {
1764 case RINGBUF_TYPE_PADDING:
Steven Rostedtbf41a152008-10-04 02:00:59 -04001765 RB_WARN_ON(cpu_buffer, 1);
Steven Rostedtd7690412008-10-01 00:29:53 -04001766 rb_advance_reader(cpu_buffer);
1767 return NULL;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001768
1769 case RINGBUF_TYPE_TIME_EXTEND:
1770 /* Internal data, OK to advance */
Steven Rostedtd7690412008-10-01 00:29:53 -04001771 rb_advance_reader(cpu_buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001772 goto again;
1773
1774 case RINGBUF_TYPE_TIME_STAMP:
1775 /* FIXME: not implemented */
Steven Rostedtd7690412008-10-01 00:29:53 -04001776 rb_advance_reader(cpu_buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001777 goto again;
1778
1779 case RINGBUF_TYPE_DATA:
1780 if (ts) {
1781 *ts = cpu_buffer->read_stamp + event->time_delta;
1782 ring_buffer_normalize_time_stamp(cpu_buffer->cpu, ts);
1783 }
1784 return event;
1785
1786 default:
1787 BUG();
1788 }
1789
1790 return NULL;
1791}
1792
Steven Rostedtf83c9d02008-11-11 18:47:44 +01001793static struct ring_buffer_event *
1794rb_iter_peek(struct ring_buffer_iter *iter, u64 *ts)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001795{
1796 struct ring_buffer *buffer;
1797 struct ring_buffer_per_cpu *cpu_buffer;
1798 struct ring_buffer_event *event;
Steven Rostedt818e3dd2008-10-31 09:58:35 -04001799 int nr_loops = 0;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001800
1801 if (ring_buffer_iter_empty(iter))
1802 return NULL;
1803
1804 cpu_buffer = iter->cpu_buffer;
1805 buffer = cpu_buffer->buffer;
1806
1807 again:
Steven Rostedt818e3dd2008-10-31 09:58:35 -04001808 /*
1809 * We repeat when a timestamp is encountered. It is possible
1810 * to get multiple timestamps from an interrupt entering just
1811 * as one timestamp is about to be written. The max times
1812 * that this can happen is the number of nested interrupts we
1813 * can have. Nesting 10 deep of interrupts is clearly
1814 * an anomaly.
1815 */
Steven Rostedt3e89c7bb2008-11-11 15:28:41 -05001816 if (RB_WARN_ON(cpu_buffer, ++nr_loops > 10))
Steven Rostedt818e3dd2008-10-31 09:58:35 -04001817 return NULL;
Steven Rostedt818e3dd2008-10-31 09:58:35 -04001818
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001819 if (rb_per_cpu_empty(cpu_buffer))
1820 return NULL;
1821
1822 event = rb_iter_head_event(iter);
1823
1824 switch (event->type) {
1825 case RINGBUF_TYPE_PADDING:
Steven Rostedtd7690412008-10-01 00:29:53 -04001826 rb_inc_iter(iter);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001827 goto again;
1828
1829 case RINGBUF_TYPE_TIME_EXTEND:
1830 /* Internal data, OK to advance */
1831 rb_advance_iter(iter);
1832 goto again;
1833
1834 case RINGBUF_TYPE_TIME_STAMP:
1835 /* FIXME: not implemented */
1836 rb_advance_iter(iter);
1837 goto again;
1838
1839 case RINGBUF_TYPE_DATA:
1840 if (ts) {
1841 *ts = iter->read_stamp + event->time_delta;
1842 ring_buffer_normalize_time_stamp(cpu_buffer->cpu, ts);
1843 }
1844 return event;
1845
1846 default:
1847 BUG();
1848 }
1849
1850 return NULL;
1851}
1852
1853/**
Steven Rostedtf83c9d02008-11-11 18:47:44 +01001854 * ring_buffer_peek - peek at the next event to be read
1855 * @buffer: The ring buffer to read
1856 * @cpu: The cpu to peak at
1857 * @ts: The timestamp counter of this event.
1858 *
1859 * This will return the event that will be read next, but does
1860 * not consume the data.
1861 */
1862struct ring_buffer_event *
1863ring_buffer_peek(struct ring_buffer *buffer, int cpu, u64 *ts)
1864{
1865 struct ring_buffer_per_cpu *cpu_buffer = buffer->buffers[cpu];
1866 struct ring_buffer_event *event;
1867 unsigned long flags;
1868
1869 spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
1870 event = rb_buffer_peek(buffer, cpu, ts);
1871 spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
1872
1873 return event;
1874}
1875
1876/**
1877 * ring_buffer_iter_peek - peek at the next event to be read
1878 * @iter: The ring buffer iterator
1879 * @ts: The timestamp counter of this event.
1880 *
1881 * This will return the event that will be read next, but does
1882 * not increment the iterator.
1883 */
1884struct ring_buffer_event *
1885ring_buffer_iter_peek(struct ring_buffer_iter *iter, u64 *ts)
1886{
1887 struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
1888 struct ring_buffer_event *event;
1889 unsigned long flags;
1890
1891 spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
1892 event = rb_iter_peek(iter, ts);
1893 spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
1894
1895 return event;
1896}
1897
1898/**
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001899 * ring_buffer_consume - return an event and consume it
1900 * @buffer: The ring buffer to get the next event from
1901 *
1902 * Returns the next event in the ring buffer, and that event is consumed.
1903 * Meaning, that sequential reads will keep returning a different event,
1904 * and eventually empty the ring buffer if the producer is slower.
1905 */
1906struct ring_buffer_event *
1907ring_buffer_consume(struct ring_buffer *buffer, int cpu, u64 *ts)
1908{
Steven Rostedtf83c9d02008-11-11 18:47:44 +01001909 struct ring_buffer_per_cpu *cpu_buffer = buffer->buffers[cpu];
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001910 struct ring_buffer_event *event;
Steven Rostedtf83c9d02008-11-11 18:47:44 +01001911 unsigned long flags;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001912
1913 if (!cpu_isset(cpu, buffer->cpumask))
1914 return NULL;
1915
Steven Rostedtf83c9d02008-11-11 18:47:44 +01001916 spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001917
Steven Rostedtf83c9d02008-11-11 18:47:44 +01001918 event = rb_buffer_peek(buffer, cpu, ts);
1919 if (!event)
1920 goto out;
1921
Steven Rostedtd7690412008-10-01 00:29:53 -04001922 rb_advance_reader(cpu_buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001923
Steven Rostedtf83c9d02008-11-11 18:47:44 +01001924 out:
1925 spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
1926
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001927 return event;
1928}
1929
1930/**
1931 * ring_buffer_read_start - start a non consuming read of the buffer
1932 * @buffer: The ring buffer to read from
1933 * @cpu: The cpu buffer to iterate over
1934 *
1935 * This starts up an iteration through the buffer. It also disables
1936 * the recording to the buffer until the reading is finished.
1937 * This prevents the reading from being corrupted. This is not
1938 * a consuming read, so a producer is not expected.
1939 *
1940 * Must be paired with ring_buffer_finish.
1941 */
1942struct ring_buffer_iter *
1943ring_buffer_read_start(struct ring_buffer *buffer, int cpu)
1944{
1945 struct ring_buffer_per_cpu *cpu_buffer;
1946 struct ring_buffer_iter *iter;
Steven Rostedtd7690412008-10-01 00:29:53 -04001947 unsigned long flags;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001948
1949 if (!cpu_isset(cpu, buffer->cpumask))
1950 return NULL;
1951
1952 iter = kmalloc(sizeof(*iter), GFP_KERNEL);
1953 if (!iter)
1954 return NULL;
1955
1956 cpu_buffer = buffer->buffers[cpu];
1957
1958 iter->cpu_buffer = cpu_buffer;
1959
1960 atomic_inc(&cpu_buffer->record_disabled);
1961 synchronize_sched();
1962
Steven Rostedtf83c9d02008-11-11 18:47:44 +01001963 spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
Steven Rostedt3e03fb72008-11-06 00:09:43 -05001964 __raw_spin_lock(&cpu_buffer->lock);
Steven Rostedt642edba2008-11-12 00:01:26 -05001965 rb_iter_reset(iter);
Steven Rostedt3e03fb72008-11-06 00:09:43 -05001966 __raw_spin_unlock(&cpu_buffer->lock);
Steven Rostedtf83c9d02008-11-11 18:47:44 +01001967 spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001968
1969 return iter;
1970}
1971
1972/**
1973 * ring_buffer_finish - finish reading the iterator of the buffer
1974 * @iter: The iterator retrieved by ring_buffer_start
1975 *
1976 * This re-enables the recording to the buffer, and frees the
1977 * iterator.
1978 */
1979void
1980ring_buffer_read_finish(struct ring_buffer_iter *iter)
1981{
1982 struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
1983
1984 atomic_dec(&cpu_buffer->record_disabled);
1985 kfree(iter);
1986}
1987
1988/**
1989 * ring_buffer_read - read the next item in the ring buffer by the iterator
1990 * @iter: The ring buffer iterator
1991 * @ts: The time stamp of the event read.
1992 *
1993 * This reads the next event in the ring buffer and increments the iterator.
1994 */
1995struct ring_buffer_event *
1996ring_buffer_read(struct ring_buffer_iter *iter, u64 *ts)
1997{
1998 struct ring_buffer_event *event;
Steven Rostedtf83c9d02008-11-11 18:47:44 +01001999 struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
2000 unsigned long flags;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002001
Steven Rostedtf83c9d02008-11-11 18:47:44 +01002002 spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
2003 event = rb_iter_peek(iter, ts);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002004 if (!event)
Steven Rostedtf83c9d02008-11-11 18:47:44 +01002005 goto out;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002006
2007 rb_advance_iter(iter);
Steven Rostedtf83c9d02008-11-11 18:47:44 +01002008 out:
2009 spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002010
2011 return event;
2012}
2013
2014/**
2015 * ring_buffer_size - return the size of the ring buffer (in bytes)
2016 * @buffer: The ring buffer.
2017 */
2018unsigned long ring_buffer_size(struct ring_buffer *buffer)
2019{
2020 return BUF_PAGE_SIZE * buffer->pages;
2021}
2022
2023static void
2024rb_reset_cpu(struct ring_buffer_per_cpu *cpu_buffer)
2025{
2026 cpu_buffer->head_page
2027 = list_entry(cpu_buffer->pages.next, struct buffer_page, list);
Steven Rostedtbf41a152008-10-04 02:00:59 -04002028 local_set(&cpu_buffer->head_page->write, 0);
2029 local_set(&cpu_buffer->head_page->commit, 0);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002030
Steven Rostedt6f807ac2008-10-04 02:00:58 -04002031 cpu_buffer->head_page->read = 0;
Steven Rostedtbf41a152008-10-04 02:00:59 -04002032
2033 cpu_buffer->tail_page = cpu_buffer->head_page;
2034 cpu_buffer->commit_page = cpu_buffer->head_page;
2035
2036 INIT_LIST_HEAD(&cpu_buffer->reader_page->list);
2037 local_set(&cpu_buffer->reader_page->write, 0);
2038 local_set(&cpu_buffer->reader_page->commit, 0);
Steven Rostedt6f807ac2008-10-04 02:00:58 -04002039 cpu_buffer->reader_page->read = 0;
Steven Rostedtd7690412008-10-01 00:29:53 -04002040
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002041 cpu_buffer->overrun = 0;
2042 cpu_buffer->entries = 0;
2043}
2044
2045/**
2046 * ring_buffer_reset_cpu - reset a ring buffer per CPU buffer
2047 * @buffer: The ring buffer to reset a per cpu buffer of
2048 * @cpu: The CPU buffer to be reset
2049 */
2050void ring_buffer_reset_cpu(struct ring_buffer *buffer, int cpu)
2051{
2052 struct ring_buffer_per_cpu *cpu_buffer = buffer->buffers[cpu];
2053 unsigned long flags;
2054
2055 if (!cpu_isset(cpu, buffer->cpumask))
2056 return;
2057
Steven Rostedtf83c9d02008-11-11 18:47:44 +01002058 spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
2059
Steven Rostedt3e03fb72008-11-06 00:09:43 -05002060 __raw_spin_lock(&cpu_buffer->lock);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002061
2062 rb_reset_cpu(cpu_buffer);
2063
Steven Rostedt3e03fb72008-11-06 00:09:43 -05002064 __raw_spin_unlock(&cpu_buffer->lock);
Steven Rostedtf83c9d02008-11-11 18:47:44 +01002065
2066 spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002067}
2068
2069/**
2070 * ring_buffer_reset - reset a ring buffer
2071 * @buffer: The ring buffer to reset all cpu buffers
2072 */
2073void ring_buffer_reset(struct ring_buffer *buffer)
2074{
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002075 int cpu;
2076
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002077 for_each_buffer_cpu(buffer, cpu)
Steven Rostedtd7690412008-10-01 00:29:53 -04002078 ring_buffer_reset_cpu(buffer, cpu);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002079}
2080
2081/**
2082 * rind_buffer_empty - is the ring buffer empty?
2083 * @buffer: The ring buffer to test
2084 */
2085int ring_buffer_empty(struct ring_buffer *buffer)
2086{
2087 struct ring_buffer_per_cpu *cpu_buffer;
2088 int cpu;
2089
2090 /* yes this is racy, but if you don't like the race, lock the buffer */
2091 for_each_buffer_cpu(buffer, cpu) {
2092 cpu_buffer = buffer->buffers[cpu];
2093 if (!rb_per_cpu_empty(cpu_buffer))
2094 return 0;
2095 }
2096 return 1;
2097}
2098
2099/**
2100 * ring_buffer_empty_cpu - is a cpu buffer of a ring buffer empty?
2101 * @buffer: The ring buffer
2102 * @cpu: The CPU buffer to test
2103 */
2104int ring_buffer_empty_cpu(struct ring_buffer *buffer, int cpu)
2105{
2106 struct ring_buffer_per_cpu *cpu_buffer;
2107
2108 if (!cpu_isset(cpu, buffer->cpumask))
2109 return 1;
2110
2111 cpu_buffer = buffer->buffers[cpu];
2112 return rb_per_cpu_empty(cpu_buffer);
2113}
2114
2115/**
2116 * ring_buffer_swap_cpu - swap a CPU buffer between two ring buffers
2117 * @buffer_a: One buffer to swap with
2118 * @buffer_b: The other buffer to swap with
2119 *
2120 * This function is useful for tracers that want to take a "snapshot"
2121 * of a CPU buffer and has another back up buffer lying around.
2122 * it is expected that the tracer handles the cpu buffer not being
2123 * used at the moment.
2124 */
2125int ring_buffer_swap_cpu(struct ring_buffer *buffer_a,
2126 struct ring_buffer *buffer_b, int cpu)
2127{
2128 struct ring_buffer_per_cpu *cpu_buffer_a;
2129 struct ring_buffer_per_cpu *cpu_buffer_b;
2130
2131 if (!cpu_isset(cpu, buffer_a->cpumask) ||
2132 !cpu_isset(cpu, buffer_b->cpumask))
2133 return -EINVAL;
2134
2135 /* At least make sure the two buffers are somewhat the same */
2136 if (buffer_a->size != buffer_b->size ||
2137 buffer_a->pages != buffer_b->pages)
2138 return -EINVAL;
2139
2140 cpu_buffer_a = buffer_a->buffers[cpu];
2141 cpu_buffer_b = buffer_b->buffers[cpu];
2142
2143 /*
2144 * We can't do a synchronize_sched here because this
2145 * function can be called in atomic context.
2146 * Normally this will be called from the same CPU as cpu.
2147 * If not it's up to the caller to protect this.
2148 */
2149 atomic_inc(&cpu_buffer_a->record_disabled);
2150 atomic_inc(&cpu_buffer_b->record_disabled);
2151
2152 buffer_a->buffers[cpu] = cpu_buffer_b;
2153 buffer_b->buffers[cpu] = cpu_buffer_a;
2154
2155 cpu_buffer_b->buffer = buffer_a;
2156 cpu_buffer_a->buffer = buffer_b;
2157
2158 atomic_dec(&cpu_buffer_a->record_disabled);
2159 atomic_dec(&cpu_buffer_b->record_disabled);
2160
2161 return 0;
2162}
2163
Steven Rostedta3583242008-11-11 15:01:42 -05002164static ssize_t
2165rb_simple_read(struct file *filp, char __user *ubuf,
2166 size_t cnt, loff_t *ppos)
2167{
2168 int *p = filp->private_data;
2169 char buf[64];
2170 int r;
2171
2172 /* !ring_buffers_off == tracing_on */
2173 r = sprintf(buf, "%d\n", !*p);
2174
2175 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2176}
2177
2178static ssize_t
2179rb_simple_write(struct file *filp, const char __user *ubuf,
2180 size_t cnt, loff_t *ppos)
2181{
2182 int *p = filp->private_data;
2183 char buf[64];
2184 long val;
2185 int ret;
2186
2187 if (cnt >= sizeof(buf))
2188 return -EINVAL;
2189
2190 if (copy_from_user(&buf, ubuf, cnt))
2191 return -EFAULT;
2192
2193 buf[cnt] = 0;
2194
2195 ret = strict_strtoul(buf, 10, &val);
2196 if (ret < 0)
2197 return ret;
2198
2199 /* !ring_buffers_off == tracing_on */
2200 *p = !val;
2201
2202 (*ppos)++;
2203
2204 return cnt;
2205}
2206
2207static struct file_operations rb_simple_fops = {
2208 .open = tracing_open_generic,
2209 .read = rb_simple_read,
2210 .write = rb_simple_write,
2211};
2212
2213
2214static __init int rb_init_debugfs(void)
2215{
2216 struct dentry *d_tracer;
2217 struct dentry *entry;
2218
2219 d_tracer = tracing_init_dentry();
2220
2221 entry = debugfs_create_file("tracing_on", 0644, d_tracer,
2222 &ring_buffers_off, &rb_simple_fops);
2223 if (!entry)
2224 pr_warning("Could not create debugfs 'tracing_on' entry\n");
2225
2226 return 0;
2227}
2228
2229fs_initcall(rb_init_debugfs);