blob: 1d601a7c4587eb075b8fe0100705a6d00ffc9172 [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 Rostedt033601a2008-11-21 12:41:55 -050021/*
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;
Steven Rostedta3583242008-11-11 15:01:42 -050061
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{
Steven Rostedt033601a2008-11-21 12:41:55 -050070 set_bit(RB_BUFFERS_ON_BIT, &ring_buffer_flags);
Steven Rostedta3583242008-11-11 15:01:42 -050071}
Robert Richterc4f50182008-12-11 16:49:22 +010072EXPORT_SYMBOL_GPL(tracing_on);
Steven Rostedta3583242008-11-11 15:01:42 -050073
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{
Steven Rostedt033601a2008-11-21 12:41:55 -050084 clear_bit(RB_BUFFERS_ON_BIT, &ring_buffer_flags);
85}
Robert Richterc4f50182008-12-11 16:49:22 +010086EXPORT_SYMBOL_GPL(tracing_off);
Steven Rostedt033601a2008-11-21 12:41:55 -050087
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);
Steven Rostedta3583242008-11-11 15:01:42 -050097}
98
Ingo Molnard06bbd62008-11-12 10:11:37 +010099#include "trace.h"
100
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400101/* 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{
Steven Rostedt47e74f22008-11-12 00:01:27 -0500107 u64 time;
108
109 preempt_disable_notrace();
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400110 /* shift to debug/test normalization and TIME_EXTENTS */
Steven Rostedt47e74f22008-11-12 00:01:27 -0500111 time = sched_clock() << DEBUG_SHIFT;
Frederic Weisbecker2c2d7322008-12-16 22:08:58 +0100112 preempt_enable_no_resched_notrace();
Steven Rostedt47e74f22008-11-12 00:01:27 -0500113
114 return time;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400115}
Robert Richterc4f50182008-12-11 16:49:22 +0100116EXPORT_SYMBOL_GPL(ring_buffer_time_stamp);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400117
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}
Robert Richterc4f50182008-12-11 16:49:22 +0100123EXPORT_SYMBOL_GPL(ring_buffer_normalize_time_stamp);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400124
125#define RB_EVNT_HDR_SIZE (sizeof(struct ring_buffer_event))
126#define RB_ALIGNMENT_SHIFT 2
127#define RB_ALIGNMENT (1 << RB_ALIGNMENT_SHIFT)
128#define RB_MAX_SMALL_DATA 28
129
130enum {
131 RB_LEN_TIME_EXTEND = 8,
132 RB_LEN_TIME_STAMP = 16,
133};
134
135/* inline for ring buffer fast paths */
136static inline unsigned
137rb_event_length(struct ring_buffer_event *event)
138{
139 unsigned length;
140
141 switch (event->type) {
142 case RINGBUF_TYPE_PADDING:
143 /* undefined */
144 return -1;
145
146 case RINGBUF_TYPE_TIME_EXTEND:
147 return RB_LEN_TIME_EXTEND;
148
149 case RINGBUF_TYPE_TIME_STAMP:
150 return RB_LEN_TIME_STAMP;
151
152 case RINGBUF_TYPE_DATA:
153 if (event->len)
154 length = event->len << RB_ALIGNMENT_SHIFT;
155 else
156 length = event->array[0];
157 return length + RB_EVNT_HDR_SIZE;
158 default:
159 BUG();
160 }
161 /* not hit */
162 return 0;
163}
164
165/**
166 * ring_buffer_event_length - return the length of the event
167 * @event: the event to get the length of
168 */
169unsigned ring_buffer_event_length(struct ring_buffer_event *event)
170{
171 return rb_event_length(event);
172}
Robert Richterc4f50182008-12-11 16:49:22 +0100173EXPORT_SYMBOL_GPL(ring_buffer_event_length);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400174
175/* inline for ring buffer fast paths */
176static inline void *
177rb_event_data(struct ring_buffer_event *event)
178{
179 BUG_ON(event->type != RINGBUF_TYPE_DATA);
180 /* If length is in len field, then array[0] has the data */
181 if (event->len)
182 return (void *)&event->array[0];
183 /* Otherwise length is in array[0] and array[1] has the data */
184 return (void *)&event->array[1];
185}
186
187/**
188 * ring_buffer_event_data - return the data of the event
189 * @event: the event to get the data from
190 */
191void *ring_buffer_event_data(struct ring_buffer_event *event)
192{
193 return rb_event_data(event);
194}
Robert Richterc4f50182008-12-11 16:49:22 +0100195EXPORT_SYMBOL_GPL(ring_buffer_event_data);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400196
197#define for_each_buffer_cpu(buffer, cpu) \
198 for_each_cpu_mask(cpu, buffer->cpumask)
199
200#define TS_SHIFT 27
201#define TS_MASK ((1ULL << TS_SHIFT) - 1)
202#define TS_DELTA_TEST (~TS_MASK)
203
Steven Rostedtabc9b562008-12-02 15:34:06 -0500204struct buffer_data_page {
Steven Rostedte4c2ce82008-10-01 11:14:54 -0400205 u64 time_stamp; /* page time stamp */
Steven Rostedtbf41a152008-10-04 02:00:59 -0400206 local_t commit; /* write commited index */
Steven Rostedtabc9b562008-12-02 15:34:06 -0500207 unsigned char data[]; /* data of buffer page */
208};
209
210struct buffer_page {
211 local_t write; /* index for next write */
Steven Rostedt6f807ac2008-10-04 02:00:58 -0400212 unsigned read; /* index for next read */
Steven Rostedte4c2ce82008-10-01 11:14:54 -0400213 struct list_head list; /* list of free pages */
Steven Rostedtabc9b562008-12-02 15:34:06 -0500214 struct buffer_data_page *page; /* Actual data page */
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400215};
216
Steven Rostedt044fa782008-12-02 23:50:03 -0500217static void rb_init_page(struct buffer_data_page *bpage)
Steven Rostedtabc9b562008-12-02 15:34:06 -0500218{
Steven Rostedt044fa782008-12-02 23:50:03 -0500219 local_set(&bpage->commit, 0);
Steven Rostedtabc9b562008-12-02 15:34:06 -0500220}
221
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400222/*
Steven Rostedted568292008-09-29 23:02:40 -0400223 * Also stolen from mm/slob.c. Thanks to Mathieu Desnoyers for pointing
224 * this issue out.
225 */
226static inline void free_buffer_page(struct buffer_page *bpage)
227{
Steven Rostedte4c2ce82008-10-01 11:14:54 -0400228 if (bpage->page)
Steven Rostedt6ae2a072008-10-13 10:22:06 -0400229 free_page((unsigned long)bpage->page);
Steven Rostedte4c2ce82008-10-01 11:14:54 -0400230 kfree(bpage);
Steven Rostedted568292008-09-29 23:02:40 -0400231}
232
233/*
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400234 * We need to fit the time_stamp delta into 27 bits.
235 */
236static inline int test_time_stamp(u64 delta)
237{
238 if (delta & TS_DELTA_TEST)
239 return 1;
240 return 0;
241}
242
Steven Rostedtabc9b562008-12-02 15:34:06 -0500243#define BUF_PAGE_SIZE (PAGE_SIZE - sizeof(struct buffer_data_page))
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400244
245/*
246 * head_page == tail_page && head == tail then buffer is empty.
247 */
248struct ring_buffer_per_cpu {
249 int cpu;
250 struct ring_buffer *buffer;
Steven Rostedtf83c9d02008-11-11 18:47:44 +0100251 spinlock_t reader_lock; /* serialize readers */
Steven Rostedt3e03fb72008-11-06 00:09:43 -0500252 raw_spinlock_t lock;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400253 struct lock_class_key lock_key;
254 struct list_head pages;
Steven Rostedt6f807ac2008-10-04 02:00:58 -0400255 struct buffer_page *head_page; /* read from head */
256 struct buffer_page *tail_page; /* write to tail */
Steven Rostedtbf41a152008-10-04 02:00:59 -0400257 struct buffer_page *commit_page; /* commited pages */
Steven Rostedtd7690412008-10-01 00:29:53 -0400258 struct buffer_page *reader_page;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400259 unsigned long overrun;
260 unsigned long entries;
261 u64 write_stamp;
262 u64 read_stamp;
263 atomic_t record_disabled;
264};
265
266struct ring_buffer {
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400267 unsigned pages;
268 unsigned flags;
269 int cpus;
270 cpumask_t cpumask;
271 atomic_t record_disabled;
272
273 struct mutex mutex;
274
275 struct ring_buffer_per_cpu **buffers;
276};
277
278struct ring_buffer_iter {
279 struct ring_buffer_per_cpu *cpu_buffer;
280 unsigned long head;
281 struct buffer_page *head_page;
282 u64 read_stamp;
283};
284
Steven Rostedtf536aaf2008-11-10 23:07:30 -0500285/* buffer may be either ring_buffer or ring_buffer_per_cpu */
Steven Rostedtbf41a152008-10-04 02:00:59 -0400286#define RB_WARN_ON(buffer, cond) \
Steven Rostedt3e89c7bb2008-11-11 15:28:41 -0500287 ({ \
288 int _____ret = unlikely(cond); \
289 if (_____ret) { \
Steven Rostedtbf41a152008-10-04 02:00:59 -0400290 atomic_inc(&buffer->record_disabled); \
291 WARN_ON(1); \
292 } \
Steven Rostedt3e89c7bb2008-11-11 15:28:41 -0500293 _____ret; \
294 })
Steven Rostedtf536aaf2008-11-10 23:07:30 -0500295
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400296/**
297 * check_pages - integrity check of buffer pages
298 * @cpu_buffer: CPU buffer with pages to test
299 *
300 * As a safty measure we check to make sure the data pages have not
301 * been corrupted.
302 */
303static int rb_check_pages(struct ring_buffer_per_cpu *cpu_buffer)
304{
305 struct list_head *head = &cpu_buffer->pages;
Steven Rostedt044fa782008-12-02 23:50:03 -0500306 struct buffer_page *bpage, *tmp;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400307
Steven Rostedt3e89c7bb2008-11-11 15:28:41 -0500308 if (RB_WARN_ON(cpu_buffer, head->next->prev != head))
309 return -1;
310 if (RB_WARN_ON(cpu_buffer, head->prev->next != head))
311 return -1;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400312
Steven Rostedt044fa782008-12-02 23:50:03 -0500313 list_for_each_entry_safe(bpage, tmp, head, list) {
Steven Rostedt3e89c7bb2008-11-11 15:28:41 -0500314 if (RB_WARN_ON(cpu_buffer,
Steven Rostedt044fa782008-12-02 23:50:03 -0500315 bpage->list.next->prev != &bpage->list))
Steven Rostedt3e89c7bb2008-11-11 15:28:41 -0500316 return -1;
317 if (RB_WARN_ON(cpu_buffer,
Steven Rostedt044fa782008-12-02 23:50:03 -0500318 bpage->list.prev->next != &bpage->list))
Steven Rostedt3e89c7bb2008-11-11 15:28:41 -0500319 return -1;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400320 }
321
322 return 0;
323}
324
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400325static int rb_allocate_pages(struct ring_buffer_per_cpu *cpu_buffer,
326 unsigned nr_pages)
327{
328 struct list_head *head = &cpu_buffer->pages;
Steven Rostedt044fa782008-12-02 23:50:03 -0500329 struct buffer_page *bpage, *tmp;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400330 unsigned long addr;
331 LIST_HEAD(pages);
332 unsigned i;
333
334 for (i = 0; i < nr_pages; i++) {
Steven Rostedt044fa782008-12-02 23:50:03 -0500335 bpage = kzalloc_node(ALIGN(sizeof(*bpage), cache_line_size()),
Steven Rostedtaa1e0e3b2008-10-02 19:18:09 -0400336 GFP_KERNEL, cpu_to_node(cpu_buffer->cpu));
Steven Rostedt044fa782008-12-02 23:50:03 -0500337 if (!bpage)
Steven Rostedte4c2ce82008-10-01 11:14:54 -0400338 goto free_pages;
Steven Rostedt044fa782008-12-02 23:50:03 -0500339 list_add(&bpage->list, &pages);
Steven Rostedte4c2ce82008-10-01 11:14:54 -0400340
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400341 addr = __get_free_page(GFP_KERNEL);
342 if (!addr)
343 goto free_pages;
Steven Rostedt044fa782008-12-02 23:50:03 -0500344 bpage->page = (void *)addr;
345 rb_init_page(bpage->page);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400346 }
347
348 list_splice(&pages, head);
349
350 rb_check_pages(cpu_buffer);
351
352 return 0;
353
354 free_pages:
Steven Rostedt044fa782008-12-02 23:50:03 -0500355 list_for_each_entry_safe(bpage, tmp, &pages, list) {
356 list_del_init(&bpage->list);
357 free_buffer_page(bpage);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400358 }
359 return -ENOMEM;
360}
361
362static struct ring_buffer_per_cpu *
363rb_allocate_cpu_buffer(struct ring_buffer *buffer, int cpu)
364{
365 struct ring_buffer_per_cpu *cpu_buffer;
Steven Rostedt044fa782008-12-02 23:50:03 -0500366 struct buffer_page *bpage;
Steven Rostedtd7690412008-10-01 00:29:53 -0400367 unsigned long addr;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400368 int ret;
369
370 cpu_buffer = kzalloc_node(ALIGN(sizeof(*cpu_buffer), cache_line_size()),
371 GFP_KERNEL, cpu_to_node(cpu));
372 if (!cpu_buffer)
373 return NULL;
374
375 cpu_buffer->cpu = cpu;
376 cpu_buffer->buffer = buffer;
Steven Rostedtf83c9d02008-11-11 18:47:44 +0100377 spin_lock_init(&cpu_buffer->reader_lock);
Steven Rostedt3e03fb72008-11-06 00:09:43 -0500378 cpu_buffer->lock = (raw_spinlock_t)__RAW_SPIN_LOCK_UNLOCKED;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400379 INIT_LIST_HEAD(&cpu_buffer->pages);
380
Steven Rostedt044fa782008-12-02 23:50:03 -0500381 bpage = kzalloc_node(ALIGN(sizeof(*bpage), cache_line_size()),
Steven Rostedte4c2ce82008-10-01 11:14:54 -0400382 GFP_KERNEL, cpu_to_node(cpu));
Steven Rostedt044fa782008-12-02 23:50:03 -0500383 if (!bpage)
Steven Rostedte4c2ce82008-10-01 11:14:54 -0400384 goto fail_free_buffer;
385
Steven Rostedt044fa782008-12-02 23:50:03 -0500386 cpu_buffer->reader_page = bpage;
Steven Rostedtd7690412008-10-01 00:29:53 -0400387 addr = __get_free_page(GFP_KERNEL);
388 if (!addr)
Steven Rostedte4c2ce82008-10-01 11:14:54 -0400389 goto fail_free_reader;
Steven Rostedt044fa782008-12-02 23:50:03 -0500390 bpage->page = (void *)addr;
391 rb_init_page(bpage->page);
Steven Rostedte4c2ce82008-10-01 11:14:54 -0400392
Steven Rostedtd7690412008-10-01 00:29:53 -0400393 INIT_LIST_HEAD(&cpu_buffer->reader_page->list);
Steven Rostedtd7690412008-10-01 00:29:53 -0400394
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400395 ret = rb_allocate_pages(cpu_buffer, buffer->pages);
396 if (ret < 0)
Steven Rostedtd7690412008-10-01 00:29:53 -0400397 goto fail_free_reader;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400398
399 cpu_buffer->head_page
400 = list_entry(cpu_buffer->pages.next, struct buffer_page, list);
Steven Rostedtbf41a152008-10-04 02:00:59 -0400401 cpu_buffer->tail_page = cpu_buffer->commit_page = cpu_buffer->head_page;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400402
403 return cpu_buffer;
404
Steven Rostedtd7690412008-10-01 00:29:53 -0400405 fail_free_reader:
406 free_buffer_page(cpu_buffer->reader_page);
407
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400408 fail_free_buffer:
409 kfree(cpu_buffer);
410 return NULL;
411}
412
413static void rb_free_cpu_buffer(struct ring_buffer_per_cpu *cpu_buffer)
414{
415 struct list_head *head = &cpu_buffer->pages;
Steven Rostedt044fa782008-12-02 23:50:03 -0500416 struct buffer_page *bpage, *tmp;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400417
Steven Rostedtd7690412008-10-01 00:29:53 -0400418 list_del_init(&cpu_buffer->reader_page->list);
419 free_buffer_page(cpu_buffer->reader_page);
420
Steven Rostedt044fa782008-12-02 23:50:03 -0500421 list_for_each_entry_safe(bpage, tmp, head, list) {
422 list_del_init(&bpage->list);
423 free_buffer_page(bpage);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400424 }
425 kfree(cpu_buffer);
426}
427
Steven Rostedta7b13742008-09-29 23:02:39 -0400428/*
429 * Causes compile errors if the struct buffer_page gets bigger
430 * than the struct page.
431 */
432extern int ring_buffer_page_too_big(void);
433
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400434/**
435 * ring_buffer_alloc - allocate a new ring_buffer
Robert Richter68814b52008-11-24 12:24:12 +0100436 * @size: the size in bytes per cpu that is needed.
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400437 * @flags: attributes to set for the ring buffer.
438 *
439 * Currently the only flag that is available is the RB_FL_OVERWRITE
440 * flag. This flag means that the buffer will overwrite old data
441 * when the buffer wraps. If this flag is not set, the buffer will
442 * drop data when the tail hits the head.
443 */
444struct ring_buffer *ring_buffer_alloc(unsigned long size, unsigned flags)
445{
446 struct ring_buffer *buffer;
447 int bsize;
448 int cpu;
449
Steven Rostedta7b13742008-09-29 23:02:39 -0400450 /* Paranoid! Optimizes out when all is well */
451 if (sizeof(struct buffer_page) > sizeof(struct page))
452 ring_buffer_page_too_big();
453
454
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400455 /* keep it in its own cache line */
456 buffer = kzalloc(ALIGN(sizeof(*buffer), cache_line_size()),
457 GFP_KERNEL);
458 if (!buffer)
459 return NULL;
460
461 buffer->pages = DIV_ROUND_UP(size, BUF_PAGE_SIZE);
462 buffer->flags = flags;
463
464 /* need at least two pages */
465 if (buffer->pages == 1)
466 buffer->pages++;
467
468 buffer->cpumask = cpu_possible_map;
469 buffer->cpus = nr_cpu_ids;
470
471 bsize = sizeof(void *) * nr_cpu_ids;
472 buffer->buffers = kzalloc(ALIGN(bsize, cache_line_size()),
473 GFP_KERNEL);
474 if (!buffer->buffers)
475 goto fail_free_buffer;
476
477 for_each_buffer_cpu(buffer, cpu) {
478 buffer->buffers[cpu] =
479 rb_allocate_cpu_buffer(buffer, cpu);
480 if (!buffer->buffers[cpu])
481 goto fail_free_buffers;
482 }
483
484 mutex_init(&buffer->mutex);
485
486 return buffer;
487
488 fail_free_buffers:
489 for_each_buffer_cpu(buffer, cpu) {
490 if (buffer->buffers[cpu])
491 rb_free_cpu_buffer(buffer->buffers[cpu]);
492 }
493 kfree(buffer->buffers);
494
495 fail_free_buffer:
496 kfree(buffer);
497 return NULL;
498}
Robert Richterc4f50182008-12-11 16:49:22 +0100499EXPORT_SYMBOL_GPL(ring_buffer_alloc);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400500
501/**
502 * ring_buffer_free - free a ring buffer.
503 * @buffer: the buffer to free.
504 */
505void
506ring_buffer_free(struct ring_buffer *buffer)
507{
508 int cpu;
509
510 for_each_buffer_cpu(buffer, cpu)
511 rb_free_cpu_buffer(buffer->buffers[cpu]);
512
513 kfree(buffer);
514}
Robert Richterc4f50182008-12-11 16:49:22 +0100515EXPORT_SYMBOL_GPL(ring_buffer_free);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400516
517static void rb_reset_cpu(struct ring_buffer_per_cpu *cpu_buffer);
518
519static void
520rb_remove_pages(struct ring_buffer_per_cpu *cpu_buffer, unsigned nr_pages)
521{
Steven Rostedt044fa782008-12-02 23:50:03 -0500522 struct buffer_page *bpage;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400523 struct list_head *p;
524 unsigned i;
525
526 atomic_inc(&cpu_buffer->record_disabled);
527 synchronize_sched();
528
529 for (i = 0; i < nr_pages; i++) {
Steven Rostedt3e89c7bb2008-11-11 15:28:41 -0500530 if (RB_WARN_ON(cpu_buffer, list_empty(&cpu_buffer->pages)))
531 return;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400532 p = cpu_buffer->pages.next;
Steven Rostedt044fa782008-12-02 23:50:03 -0500533 bpage = list_entry(p, struct buffer_page, list);
534 list_del_init(&bpage->list);
535 free_buffer_page(bpage);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400536 }
Steven Rostedt3e89c7bb2008-11-11 15:28:41 -0500537 if (RB_WARN_ON(cpu_buffer, list_empty(&cpu_buffer->pages)))
538 return;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400539
540 rb_reset_cpu(cpu_buffer);
541
542 rb_check_pages(cpu_buffer);
543
544 atomic_dec(&cpu_buffer->record_disabled);
545
546}
547
548static void
549rb_insert_pages(struct ring_buffer_per_cpu *cpu_buffer,
550 struct list_head *pages, unsigned nr_pages)
551{
Steven Rostedt044fa782008-12-02 23:50:03 -0500552 struct buffer_page *bpage;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400553 struct list_head *p;
554 unsigned i;
555
556 atomic_inc(&cpu_buffer->record_disabled);
557 synchronize_sched();
558
559 for (i = 0; i < nr_pages; i++) {
Steven Rostedt3e89c7bb2008-11-11 15:28:41 -0500560 if (RB_WARN_ON(cpu_buffer, list_empty(pages)))
561 return;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400562 p = pages->next;
Steven Rostedt044fa782008-12-02 23:50:03 -0500563 bpage = list_entry(p, struct buffer_page, list);
564 list_del_init(&bpage->list);
565 list_add_tail(&bpage->list, &cpu_buffer->pages);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400566 }
567 rb_reset_cpu(cpu_buffer);
568
569 rb_check_pages(cpu_buffer);
570
571 atomic_dec(&cpu_buffer->record_disabled);
572}
573
574/**
575 * ring_buffer_resize - resize the ring buffer
576 * @buffer: the buffer to resize.
577 * @size: the new size.
578 *
579 * The tracer is responsible for making sure that the buffer is
580 * not being used while changing the size.
581 * Note: We may be able to change the above requirement by using
582 * RCU synchronizations.
583 *
584 * Minimum size is 2 * BUF_PAGE_SIZE.
585 *
586 * Returns -1 on failure.
587 */
588int ring_buffer_resize(struct ring_buffer *buffer, unsigned long size)
589{
590 struct ring_buffer_per_cpu *cpu_buffer;
591 unsigned nr_pages, rm_pages, new_pages;
Steven Rostedt044fa782008-12-02 23:50:03 -0500592 struct buffer_page *bpage, *tmp;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400593 unsigned long buffer_size;
594 unsigned long addr;
595 LIST_HEAD(pages);
596 int i, cpu;
597
Ingo Molnaree51a1d2008-11-13 14:58:31 +0100598 /*
599 * Always succeed at resizing a non-existent buffer:
600 */
601 if (!buffer)
602 return size;
603
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400604 size = DIV_ROUND_UP(size, BUF_PAGE_SIZE);
605 size *= BUF_PAGE_SIZE;
606 buffer_size = buffer->pages * BUF_PAGE_SIZE;
607
608 /* we need a minimum of two pages */
609 if (size < BUF_PAGE_SIZE * 2)
610 size = BUF_PAGE_SIZE * 2;
611
612 if (size == buffer_size)
613 return size;
614
615 mutex_lock(&buffer->mutex);
616
617 nr_pages = DIV_ROUND_UP(size, BUF_PAGE_SIZE);
618
619 if (size < buffer_size) {
620
621 /* easy case, just free pages */
Steven Rostedt3e89c7bb2008-11-11 15:28:41 -0500622 if (RB_WARN_ON(buffer, nr_pages >= buffer->pages)) {
623 mutex_unlock(&buffer->mutex);
624 return -1;
625 }
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400626
627 rm_pages = buffer->pages - nr_pages;
628
629 for_each_buffer_cpu(buffer, cpu) {
630 cpu_buffer = buffer->buffers[cpu];
631 rb_remove_pages(cpu_buffer, rm_pages);
632 }
633 goto out;
634 }
635
636 /*
637 * This is a bit more difficult. We only want to add pages
638 * when we can allocate enough for all CPUs. We do this
639 * by allocating all the pages and storing them on a local
640 * link list. If we succeed in our allocation, then we
641 * add these pages to the cpu_buffers. Otherwise we just free
642 * them all and return -ENOMEM;
643 */
Steven Rostedt3e89c7bb2008-11-11 15:28:41 -0500644 if (RB_WARN_ON(buffer, nr_pages <= buffer->pages)) {
645 mutex_unlock(&buffer->mutex);
646 return -1;
647 }
Steven Rostedtf536aaf2008-11-10 23:07:30 -0500648
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400649 new_pages = nr_pages - buffer->pages;
650
651 for_each_buffer_cpu(buffer, cpu) {
652 for (i = 0; i < new_pages; i++) {
Steven Rostedt044fa782008-12-02 23:50:03 -0500653 bpage = kzalloc_node(ALIGN(sizeof(*bpage),
Steven Rostedte4c2ce82008-10-01 11:14:54 -0400654 cache_line_size()),
655 GFP_KERNEL, cpu_to_node(cpu));
Steven Rostedt044fa782008-12-02 23:50:03 -0500656 if (!bpage)
Steven Rostedte4c2ce82008-10-01 11:14:54 -0400657 goto free_pages;
Steven Rostedt044fa782008-12-02 23:50:03 -0500658 list_add(&bpage->list, &pages);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400659 addr = __get_free_page(GFP_KERNEL);
660 if (!addr)
661 goto free_pages;
Steven Rostedt044fa782008-12-02 23:50:03 -0500662 bpage->page = (void *)addr;
663 rb_init_page(bpage->page);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400664 }
665 }
666
667 for_each_buffer_cpu(buffer, cpu) {
668 cpu_buffer = buffer->buffers[cpu];
669 rb_insert_pages(cpu_buffer, &pages, new_pages);
670 }
671
Steven Rostedt3e89c7bb2008-11-11 15:28:41 -0500672 if (RB_WARN_ON(buffer, !list_empty(&pages))) {
673 mutex_unlock(&buffer->mutex);
674 return -1;
675 }
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400676
677 out:
678 buffer->pages = nr_pages;
679 mutex_unlock(&buffer->mutex);
680
681 return size;
682
683 free_pages:
Steven Rostedt044fa782008-12-02 23:50:03 -0500684 list_for_each_entry_safe(bpage, tmp, &pages, list) {
685 list_del_init(&bpage->list);
686 free_buffer_page(bpage);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400687 }
Vegard Nossum641d2f62008-11-18 19:22:13 +0100688 mutex_unlock(&buffer->mutex);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400689 return -ENOMEM;
690}
Robert Richterc4f50182008-12-11 16:49:22 +0100691EXPORT_SYMBOL_GPL(ring_buffer_resize);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400692
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400693static inline int rb_null_event(struct ring_buffer_event *event)
694{
695 return event->type == RINGBUF_TYPE_PADDING;
696}
697
Steven Rostedt8789a9e2008-12-02 15:34:07 -0500698static inline void *
Steven Rostedt044fa782008-12-02 23:50:03 -0500699__rb_data_page_index(struct buffer_data_page *bpage, unsigned index)
Steven Rostedt8789a9e2008-12-02 15:34:07 -0500700{
Steven Rostedt044fa782008-12-02 23:50:03 -0500701 return bpage->data + index;
Steven Rostedt8789a9e2008-12-02 15:34:07 -0500702}
703
Steven Rostedt044fa782008-12-02 23:50:03 -0500704static inline void *__rb_page_index(struct buffer_page *bpage, unsigned index)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400705{
Steven Rostedt044fa782008-12-02 23:50:03 -0500706 return bpage->page->data + index;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400707}
708
709static inline struct ring_buffer_event *
Steven Rostedtd7690412008-10-01 00:29:53 -0400710rb_reader_event(struct ring_buffer_per_cpu *cpu_buffer)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400711{
Steven Rostedt6f807ac2008-10-04 02:00:58 -0400712 return __rb_page_index(cpu_buffer->reader_page,
713 cpu_buffer->reader_page->read);
714}
715
716static inline struct ring_buffer_event *
717rb_head_event(struct ring_buffer_per_cpu *cpu_buffer)
718{
719 return __rb_page_index(cpu_buffer->head_page,
720 cpu_buffer->head_page->read);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400721}
722
723static inline struct ring_buffer_event *
724rb_iter_head_event(struct ring_buffer_iter *iter)
725{
Steven Rostedt6f807ac2008-10-04 02:00:58 -0400726 return __rb_page_index(iter->head_page, iter->head);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400727}
728
Steven Rostedtbf41a152008-10-04 02:00:59 -0400729static inline unsigned rb_page_write(struct buffer_page *bpage)
730{
731 return local_read(&bpage->write);
732}
733
734static inline unsigned rb_page_commit(struct buffer_page *bpage)
735{
Steven Rostedtabc9b562008-12-02 15:34:06 -0500736 return local_read(&bpage->page->commit);
Steven Rostedtbf41a152008-10-04 02:00:59 -0400737}
738
739/* Size is determined by what has been commited */
740static inline unsigned rb_page_size(struct buffer_page *bpage)
741{
742 return rb_page_commit(bpage);
743}
744
745static inline unsigned
746rb_commit_index(struct ring_buffer_per_cpu *cpu_buffer)
747{
748 return rb_page_commit(cpu_buffer->commit_page);
749}
750
751static inline unsigned rb_head_size(struct ring_buffer_per_cpu *cpu_buffer)
752{
753 return rb_page_commit(cpu_buffer->head_page);
754}
755
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400756/*
757 * When the tail hits the head and the buffer is in overwrite mode,
758 * the head jumps to the next page and all content on the previous
759 * page is discarded. But before doing so, we update the overrun
760 * variable of the buffer.
761 */
762static void rb_update_overflow(struct ring_buffer_per_cpu *cpu_buffer)
763{
764 struct ring_buffer_event *event;
765 unsigned long head;
766
767 for (head = 0; head < rb_head_size(cpu_buffer);
768 head += rb_event_length(event)) {
769
Steven Rostedt6f807ac2008-10-04 02:00:58 -0400770 event = __rb_page_index(cpu_buffer->head_page, head);
Steven Rostedt3e89c7bb2008-11-11 15:28:41 -0500771 if (RB_WARN_ON(cpu_buffer, rb_null_event(event)))
772 return;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400773 /* Only count data entries */
774 if (event->type != RINGBUF_TYPE_DATA)
775 continue;
776 cpu_buffer->overrun++;
777 cpu_buffer->entries--;
778 }
779}
780
781static inline void rb_inc_page(struct ring_buffer_per_cpu *cpu_buffer,
Steven Rostedt044fa782008-12-02 23:50:03 -0500782 struct buffer_page **bpage)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400783{
Steven Rostedt044fa782008-12-02 23:50:03 -0500784 struct list_head *p = (*bpage)->list.next;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400785
786 if (p == &cpu_buffer->pages)
787 p = p->next;
788
Steven Rostedt044fa782008-12-02 23:50:03 -0500789 *bpage = list_entry(p, struct buffer_page, list);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400790}
791
Steven Rostedtbf41a152008-10-04 02:00:59 -0400792static inline unsigned
793rb_event_index(struct ring_buffer_event *event)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400794{
Steven Rostedtbf41a152008-10-04 02:00:59 -0400795 unsigned long addr = (unsigned long)event;
796
797 return (addr & ~PAGE_MASK) - (PAGE_SIZE - BUF_PAGE_SIZE);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400798}
799
Steven Rostedtbf41a152008-10-04 02:00:59 -0400800static inline int
801rb_is_commit(struct ring_buffer_per_cpu *cpu_buffer,
802 struct ring_buffer_event *event)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400803{
Steven Rostedtbf41a152008-10-04 02:00:59 -0400804 unsigned long addr = (unsigned long)event;
805 unsigned long index;
806
807 index = rb_event_index(event);
808 addr &= PAGE_MASK;
809
810 return cpu_buffer->commit_page->page == (void *)addr &&
811 rb_commit_index(cpu_buffer) == index;
812}
813
814static inline void
815rb_set_commit_event(struct ring_buffer_per_cpu *cpu_buffer,
816 struct ring_buffer_event *event)
817{
818 unsigned long addr = (unsigned long)event;
819 unsigned long index;
820
821 index = rb_event_index(event);
822 addr &= PAGE_MASK;
823
824 while (cpu_buffer->commit_page->page != (void *)addr) {
Steven Rostedt3e89c7bb2008-11-11 15:28:41 -0500825 if (RB_WARN_ON(cpu_buffer,
826 cpu_buffer->commit_page == cpu_buffer->tail_page))
827 return;
Steven Rostedtabc9b562008-12-02 15:34:06 -0500828 cpu_buffer->commit_page->page->commit =
Steven Rostedtbf41a152008-10-04 02:00:59 -0400829 cpu_buffer->commit_page->write;
830 rb_inc_page(cpu_buffer, &cpu_buffer->commit_page);
Steven Rostedtabc9b562008-12-02 15:34:06 -0500831 cpu_buffer->write_stamp =
832 cpu_buffer->commit_page->page->time_stamp;
Steven Rostedtbf41a152008-10-04 02:00:59 -0400833 }
834
835 /* Now set the commit to the event's index */
Steven Rostedtabc9b562008-12-02 15:34:06 -0500836 local_set(&cpu_buffer->commit_page->page->commit, index);
Steven Rostedtbf41a152008-10-04 02:00:59 -0400837}
838
839static inline void
840rb_set_commit_to_write(struct ring_buffer_per_cpu *cpu_buffer)
841{
842 /*
843 * We only race with interrupts and NMIs on this CPU.
844 * If we own the commit event, then we can commit
845 * all others that interrupted us, since the interruptions
846 * are in stack format (they finish before they come
847 * back to us). This allows us to do a simple loop to
848 * assign the commit to the tail.
849 */
Steven Rostedta8ccf1d2008-12-23 11:32:24 -0500850 again:
Steven Rostedtbf41a152008-10-04 02:00:59 -0400851 while (cpu_buffer->commit_page != cpu_buffer->tail_page) {
Steven Rostedtabc9b562008-12-02 15:34:06 -0500852 cpu_buffer->commit_page->page->commit =
Steven Rostedtbf41a152008-10-04 02:00:59 -0400853 cpu_buffer->commit_page->write;
854 rb_inc_page(cpu_buffer, &cpu_buffer->commit_page);
Steven Rostedtabc9b562008-12-02 15:34:06 -0500855 cpu_buffer->write_stamp =
856 cpu_buffer->commit_page->page->time_stamp;
Steven Rostedtbf41a152008-10-04 02:00:59 -0400857 /* add barrier to keep gcc from optimizing too much */
858 barrier();
859 }
860 while (rb_commit_index(cpu_buffer) !=
861 rb_page_write(cpu_buffer->commit_page)) {
Steven Rostedtabc9b562008-12-02 15:34:06 -0500862 cpu_buffer->commit_page->page->commit =
Steven Rostedtbf41a152008-10-04 02:00:59 -0400863 cpu_buffer->commit_page->write;
864 barrier();
865 }
Steven Rostedta8ccf1d2008-12-23 11:32:24 -0500866
867 /* again, keep gcc from optimizing */
868 barrier();
869
870 /*
871 * If an interrupt came in just after the first while loop
872 * and pushed the tail page forward, we will be left with
873 * a dangling commit that will never go forward.
874 */
875 if (unlikely(cpu_buffer->commit_page != cpu_buffer->tail_page))
876 goto again;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400877}
878
Steven Rostedtd7690412008-10-01 00:29:53 -0400879static void rb_reset_reader_page(struct ring_buffer_per_cpu *cpu_buffer)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400880{
Steven Rostedtabc9b562008-12-02 15:34:06 -0500881 cpu_buffer->read_stamp = cpu_buffer->reader_page->page->time_stamp;
Steven Rostedt6f807ac2008-10-04 02:00:58 -0400882 cpu_buffer->reader_page->read = 0;
Steven Rostedtd7690412008-10-01 00:29:53 -0400883}
884
885static inline void rb_inc_iter(struct ring_buffer_iter *iter)
886{
887 struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
888
889 /*
890 * The iterator could be on the reader page (it starts there).
891 * But the head could have moved, since the reader was
892 * found. Check for this case and assign the iterator
893 * to the head page instead of next.
894 */
895 if (iter->head_page == cpu_buffer->reader_page)
896 iter->head_page = cpu_buffer->head_page;
897 else
898 rb_inc_page(cpu_buffer, &iter->head_page);
899
Steven Rostedtabc9b562008-12-02 15:34:06 -0500900 iter->read_stamp = iter->head_page->page->time_stamp;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400901 iter->head = 0;
902}
903
904/**
905 * ring_buffer_update_event - update event type and data
906 * @event: the even to update
907 * @type: the type of event
908 * @length: the size of the event field in the ring buffer
909 *
910 * Update the type and data fields of the event. The length
911 * is the actual size that is written to the ring buffer,
912 * and with this, we can determine what to place into the
913 * data field.
914 */
915static inline void
916rb_update_event(struct ring_buffer_event *event,
917 unsigned type, unsigned length)
918{
919 event->type = type;
920
921 switch (type) {
922
923 case RINGBUF_TYPE_PADDING:
924 break;
925
926 case RINGBUF_TYPE_TIME_EXTEND:
927 event->len =
928 (RB_LEN_TIME_EXTEND + (RB_ALIGNMENT-1))
929 >> RB_ALIGNMENT_SHIFT;
930 break;
931
932 case RINGBUF_TYPE_TIME_STAMP:
933 event->len =
934 (RB_LEN_TIME_STAMP + (RB_ALIGNMENT-1))
935 >> RB_ALIGNMENT_SHIFT;
936 break;
937
938 case RINGBUF_TYPE_DATA:
939 length -= RB_EVNT_HDR_SIZE;
940 if (length > RB_MAX_SMALL_DATA) {
941 event->len = 0;
942 event->array[0] = length;
943 } else
944 event->len =
945 (length + (RB_ALIGNMENT-1))
946 >> RB_ALIGNMENT_SHIFT;
947 break;
948 default:
949 BUG();
950 }
951}
952
953static inline unsigned rb_calculate_event_length(unsigned length)
954{
955 struct ring_buffer_event event; /* Used only for sizeof array */
956
957 /* zero length can cause confusions */
958 if (!length)
959 length = 1;
960
961 if (length > RB_MAX_SMALL_DATA)
962 length += sizeof(event.array[0]);
963
964 length += RB_EVNT_HDR_SIZE;
965 length = ALIGN(length, RB_ALIGNMENT);
966
967 return length;
968}
969
970static struct ring_buffer_event *
971__rb_reserve_next(struct ring_buffer_per_cpu *cpu_buffer,
972 unsigned type, unsigned long length, u64 *ts)
973{
Steven Rostedt98db8df2008-12-23 11:32:25 -0500974 struct buffer_page *tail_page, *head_page, *reader_page, *commit_page;
Steven Rostedtbf41a152008-10-04 02:00:59 -0400975 unsigned long tail, write;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400976 struct ring_buffer *buffer = cpu_buffer->buffer;
977 struct ring_buffer_event *event;
Steven Rostedtbf41a152008-10-04 02:00:59 -0400978 unsigned long flags;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400979
Steven Rostedt98db8df2008-12-23 11:32:25 -0500980 commit_page = cpu_buffer->commit_page;
981 /* we just need to protect against interrupts */
982 barrier();
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400983 tail_page = cpu_buffer->tail_page;
Steven Rostedtbf41a152008-10-04 02:00:59 -0400984 write = local_add_return(length, &tail_page->write);
985 tail = write - length;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400986
Steven Rostedtbf41a152008-10-04 02:00:59 -0400987 /* See if we shot pass the end of this buffer page */
988 if (write > BUF_PAGE_SIZE) {
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400989 struct buffer_page *next_page = tail_page;
990
Steven Rostedt3e03fb72008-11-06 00:09:43 -0500991 local_irq_save(flags);
992 __raw_spin_lock(&cpu_buffer->lock);
Steven Rostedtbf41a152008-10-04 02:00:59 -0400993
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400994 rb_inc_page(cpu_buffer, &next_page);
995
Steven Rostedtd7690412008-10-01 00:29:53 -0400996 head_page = cpu_buffer->head_page;
997 reader_page = cpu_buffer->reader_page;
998
999 /* we grabbed the lock before incrementing */
Steven Rostedt3e89c7bb2008-11-11 15:28:41 -05001000 if (RB_WARN_ON(cpu_buffer, next_page == reader_page))
1001 goto out_unlock;
Steven Rostedtbf41a152008-10-04 02:00:59 -04001002
1003 /*
1004 * If for some reason, we had an interrupt storm that made
1005 * it all the way around the buffer, bail, and warn
1006 * about it.
1007 */
Steven Rostedt98db8df2008-12-23 11:32:25 -05001008 if (unlikely(next_page == commit_page)) {
Steven Rostedtbf41a152008-10-04 02:00:59 -04001009 WARN_ON_ONCE(1);
1010 goto out_unlock;
1011 }
Steven Rostedtd7690412008-10-01 00:29:53 -04001012
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001013 if (next_page == head_page) {
Steven Rostedtd7690412008-10-01 00:29:53 -04001014 if (!(buffer->flags & RB_FL_OVERWRITE)) {
Steven Rostedtbf41a152008-10-04 02:00:59 -04001015 /* reset write */
1016 if (tail <= BUF_PAGE_SIZE)
1017 local_set(&tail_page->write, tail);
1018 goto out_unlock;
Steven Rostedtd7690412008-10-01 00:29:53 -04001019 }
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001020
Steven Rostedtbf41a152008-10-04 02:00:59 -04001021 /* tail_page has not moved yet? */
1022 if (tail_page == cpu_buffer->tail_page) {
1023 /* count overflows */
1024 rb_update_overflow(cpu_buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001025
Steven Rostedtbf41a152008-10-04 02:00:59 -04001026 rb_inc_page(cpu_buffer, &head_page);
1027 cpu_buffer->head_page = head_page;
1028 cpu_buffer->head_page->read = 0;
1029 }
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001030 }
1031
Steven Rostedtbf41a152008-10-04 02:00:59 -04001032 /*
1033 * If the tail page is still the same as what we think
1034 * it is, then it is up to us to update the tail
1035 * pointer.
1036 */
1037 if (tail_page == cpu_buffer->tail_page) {
1038 local_set(&next_page->write, 0);
Steven Rostedtabc9b562008-12-02 15:34:06 -05001039 local_set(&next_page->page->commit, 0);
Steven Rostedtbf41a152008-10-04 02:00:59 -04001040 cpu_buffer->tail_page = next_page;
1041
1042 /* reread the time stamp */
1043 *ts = ring_buffer_time_stamp(cpu_buffer->cpu);
Steven Rostedtabc9b562008-12-02 15:34:06 -05001044 cpu_buffer->tail_page->page->time_stamp = *ts;
Steven Rostedtbf41a152008-10-04 02:00:59 -04001045 }
1046
1047 /*
1048 * The actual tail page has moved forward.
1049 */
1050 if (tail < BUF_PAGE_SIZE) {
1051 /* Mark the rest of the page with padding */
Steven Rostedt6f807ac2008-10-04 02:00:58 -04001052 event = __rb_page_index(tail_page, tail);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001053 event->type = RINGBUF_TYPE_PADDING;
1054 }
1055
Steven Rostedtbf41a152008-10-04 02:00:59 -04001056 if (tail <= BUF_PAGE_SIZE)
1057 /* Set the write back to the previous setting */
1058 local_set(&tail_page->write, tail);
1059
1060 /*
1061 * If this was a commit entry that failed,
1062 * increment that too
1063 */
1064 if (tail_page == cpu_buffer->commit_page &&
1065 tail == rb_commit_index(cpu_buffer)) {
1066 rb_set_commit_to_write(cpu_buffer);
1067 }
1068
Steven Rostedt3e03fb72008-11-06 00:09:43 -05001069 __raw_spin_unlock(&cpu_buffer->lock);
1070 local_irq_restore(flags);
Steven Rostedtbf41a152008-10-04 02:00:59 -04001071
1072 /* fail and let the caller try again */
1073 return ERR_PTR(-EAGAIN);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001074 }
1075
Steven Rostedtbf41a152008-10-04 02:00:59 -04001076 /* We reserved something on the buffer */
1077
Steven Rostedt3e89c7bb2008-11-11 15:28:41 -05001078 if (RB_WARN_ON(cpu_buffer, write > BUF_PAGE_SIZE))
1079 return NULL;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001080
Steven Rostedt6f807ac2008-10-04 02:00:58 -04001081 event = __rb_page_index(tail_page, tail);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001082 rb_update_event(event, type, length);
1083
Steven Rostedtbf41a152008-10-04 02:00:59 -04001084 /*
1085 * If this is a commit and the tail is zero, then update
1086 * this page's time stamp.
1087 */
1088 if (!tail && rb_is_commit(cpu_buffer, event))
Steven Rostedtabc9b562008-12-02 15:34:06 -05001089 cpu_buffer->commit_page->page->time_stamp = *ts;
Steven Rostedtbf41a152008-10-04 02:00:59 -04001090
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001091 return event;
Steven Rostedtbf41a152008-10-04 02:00:59 -04001092
1093 out_unlock:
Steven Rostedt3e03fb72008-11-06 00:09:43 -05001094 __raw_spin_unlock(&cpu_buffer->lock);
1095 local_irq_restore(flags);
Steven Rostedtbf41a152008-10-04 02:00:59 -04001096 return NULL;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001097}
1098
1099static int
1100rb_add_time_stamp(struct ring_buffer_per_cpu *cpu_buffer,
1101 u64 *ts, u64 *delta)
1102{
1103 struct ring_buffer_event *event;
1104 static int once;
Steven Rostedtbf41a152008-10-04 02:00:59 -04001105 int ret;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001106
1107 if (unlikely(*delta > (1ULL << 59) && !once++)) {
1108 printk(KERN_WARNING "Delta way too big! %llu"
1109 " ts=%llu write stamp = %llu\n",
Stephen Rothwelle2862c92008-10-27 17:43:28 +11001110 (unsigned long long)*delta,
1111 (unsigned long long)*ts,
1112 (unsigned long long)cpu_buffer->write_stamp);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001113 WARN_ON(1);
1114 }
1115
1116 /*
1117 * The delta is too big, we to add a
1118 * new timestamp.
1119 */
1120 event = __rb_reserve_next(cpu_buffer,
1121 RINGBUF_TYPE_TIME_EXTEND,
1122 RB_LEN_TIME_EXTEND,
1123 ts);
1124 if (!event)
Steven Rostedtbf41a152008-10-04 02:00:59 -04001125 return -EBUSY;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001126
Steven Rostedtbf41a152008-10-04 02:00:59 -04001127 if (PTR_ERR(event) == -EAGAIN)
1128 return -EAGAIN;
1129
1130 /* Only a commited time event can update the write stamp */
1131 if (rb_is_commit(cpu_buffer, event)) {
1132 /*
1133 * If this is the first on the page, then we need to
1134 * update the page itself, and just put in a zero.
1135 */
1136 if (rb_event_index(event)) {
1137 event->time_delta = *delta & TS_MASK;
1138 event->array[0] = *delta >> TS_SHIFT;
1139 } else {
Steven Rostedtabc9b562008-12-02 15:34:06 -05001140 cpu_buffer->commit_page->page->time_stamp = *ts;
Steven Rostedtbf41a152008-10-04 02:00:59 -04001141 event->time_delta = 0;
1142 event->array[0] = 0;
1143 }
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001144 cpu_buffer->write_stamp = *ts;
Steven Rostedtbf41a152008-10-04 02:00:59 -04001145 /* let the caller know this was the commit */
1146 ret = 1;
1147 } else {
1148 /* Darn, this is just wasted space */
1149 event->time_delta = 0;
1150 event->array[0] = 0;
1151 ret = 0;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001152 }
1153
Steven Rostedtbf41a152008-10-04 02:00:59 -04001154 *delta = 0;
1155
1156 return ret;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001157}
1158
1159static struct ring_buffer_event *
1160rb_reserve_next_event(struct ring_buffer_per_cpu *cpu_buffer,
1161 unsigned type, unsigned long length)
1162{
1163 struct ring_buffer_event *event;
1164 u64 ts, delta;
Steven Rostedtbf41a152008-10-04 02:00:59 -04001165 int commit = 0;
Steven Rostedt818e3dd2008-10-31 09:58:35 -04001166 int nr_loops = 0;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001167
Steven Rostedtbf41a152008-10-04 02:00:59 -04001168 again:
Steven Rostedt818e3dd2008-10-31 09:58:35 -04001169 /*
1170 * We allow for interrupts to reenter here and do a trace.
1171 * If one does, it will cause this original code to loop
1172 * back here. Even with heavy interrupts happening, this
1173 * should only happen a few times in a row. If this happens
1174 * 1000 times in a row, there must be either an interrupt
1175 * storm or we have something buggy.
1176 * Bail!
1177 */
Steven Rostedt3e89c7bb2008-11-11 15:28:41 -05001178 if (RB_WARN_ON(cpu_buffer, ++nr_loops > 1000))
Steven Rostedt818e3dd2008-10-31 09:58:35 -04001179 return NULL;
Steven Rostedt818e3dd2008-10-31 09:58:35 -04001180
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001181 ts = ring_buffer_time_stamp(cpu_buffer->cpu);
1182
Steven Rostedtbf41a152008-10-04 02:00:59 -04001183 /*
1184 * Only the first commit can update the timestamp.
1185 * Yes there is a race here. If an interrupt comes in
1186 * just after the conditional and it traces too, then it
1187 * will also check the deltas. More than one timestamp may
1188 * also be made. But only the entry that did the actual
1189 * commit will be something other than zero.
1190 */
1191 if (cpu_buffer->tail_page == cpu_buffer->commit_page &&
1192 rb_page_write(cpu_buffer->tail_page) ==
1193 rb_commit_index(cpu_buffer)) {
1194
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001195 delta = ts - cpu_buffer->write_stamp;
1196
Steven Rostedtbf41a152008-10-04 02:00:59 -04001197 /* make sure this delta is calculated here */
1198 barrier();
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001199
Steven Rostedtbf41a152008-10-04 02:00:59 -04001200 /* Did the write stamp get updated already? */
1201 if (unlikely(ts < cpu_buffer->write_stamp))
Steven Rostedt4143c5c2008-11-10 21:46:01 -05001202 delta = 0;
Steven Rostedtbf41a152008-10-04 02:00:59 -04001203
1204 if (test_time_stamp(delta)) {
1205
1206 commit = rb_add_time_stamp(cpu_buffer, &ts, &delta);
1207
1208 if (commit == -EBUSY)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001209 return NULL;
Steven Rostedtbf41a152008-10-04 02:00:59 -04001210
1211 if (commit == -EAGAIN)
1212 goto again;
1213
1214 RB_WARN_ON(cpu_buffer, commit < 0);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001215 }
Steven Rostedtbf41a152008-10-04 02:00:59 -04001216 } else
1217 /* Non commits have zero deltas */
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001218 delta = 0;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001219
1220 event = __rb_reserve_next(cpu_buffer, type, length, &ts);
Steven Rostedtbf41a152008-10-04 02:00:59 -04001221 if (PTR_ERR(event) == -EAGAIN)
1222 goto again;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001223
Steven Rostedtbf41a152008-10-04 02:00:59 -04001224 if (!event) {
1225 if (unlikely(commit))
1226 /*
1227 * Ouch! We needed a timestamp and it was commited. But
1228 * we didn't get our event reserved.
1229 */
1230 rb_set_commit_to_write(cpu_buffer);
1231 return NULL;
1232 }
1233
1234 /*
1235 * If the timestamp was commited, make the commit our entry
1236 * now so that we will update it when needed.
1237 */
1238 if (commit)
1239 rb_set_commit_event(cpu_buffer, event);
1240 else if (!rb_is_commit(cpu_buffer, event))
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001241 delta = 0;
1242
1243 event->time_delta = delta;
1244
1245 return event;
1246}
1247
Steven Rostedtbf41a152008-10-04 02:00:59 -04001248static DEFINE_PER_CPU(int, rb_need_resched);
1249
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001250/**
1251 * ring_buffer_lock_reserve - reserve a part of the buffer
1252 * @buffer: the ring buffer to reserve from
1253 * @length: the length of the data to reserve (excluding event header)
1254 * @flags: a pointer to save the interrupt flags
1255 *
1256 * Returns a reseverd event on the ring buffer to copy directly to.
1257 * The user of this interface will need to get the body to write into
1258 * and can use the ring_buffer_event_data() interface.
1259 *
1260 * The length is the length of the data needed, not the event length
1261 * which also includes the event header.
1262 *
1263 * Must be paired with ring_buffer_unlock_commit, unless NULL is returned.
1264 * If NULL is returned, then nothing has been allocated or locked.
1265 */
1266struct ring_buffer_event *
1267ring_buffer_lock_reserve(struct ring_buffer *buffer,
1268 unsigned long length,
1269 unsigned long *flags)
1270{
1271 struct ring_buffer_per_cpu *cpu_buffer;
1272 struct ring_buffer_event *event;
Steven Rostedtbf41a152008-10-04 02:00:59 -04001273 int cpu, resched;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001274
Steven Rostedt033601a2008-11-21 12:41:55 -05001275 if (ring_buffer_flags != RB_BUFFERS_ON)
Steven Rostedta3583242008-11-11 15:01:42 -05001276 return NULL;
1277
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001278 if (atomic_read(&buffer->record_disabled))
1279 return NULL;
1280
Steven Rostedtbf41a152008-10-04 02:00:59 -04001281 /* If we are tracing schedule, we don't want to recurse */
Steven Rostedt182e9f52008-11-03 23:15:56 -05001282 resched = ftrace_preempt_disable();
Steven Rostedtbf41a152008-10-04 02:00:59 -04001283
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001284 cpu = raw_smp_processor_id();
1285
1286 if (!cpu_isset(cpu, buffer->cpumask))
Steven Rostedtd7690412008-10-01 00:29:53 -04001287 goto out;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001288
1289 cpu_buffer = buffer->buffers[cpu];
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001290
1291 if (atomic_read(&cpu_buffer->record_disabled))
Steven Rostedtd7690412008-10-01 00:29:53 -04001292 goto out;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001293
1294 length = rb_calculate_event_length(length);
1295 if (length > BUF_PAGE_SIZE)
Steven Rostedtbf41a152008-10-04 02:00:59 -04001296 goto out;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001297
1298 event = rb_reserve_next_event(cpu_buffer, RINGBUF_TYPE_DATA, length);
1299 if (!event)
Steven Rostedtd7690412008-10-01 00:29:53 -04001300 goto out;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001301
Steven Rostedtbf41a152008-10-04 02:00:59 -04001302 /*
1303 * Need to store resched state on this cpu.
1304 * Only the first needs to.
1305 */
1306
1307 if (preempt_count() == 1)
1308 per_cpu(rb_need_resched, cpu) = resched;
1309
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001310 return event;
1311
Steven Rostedtd7690412008-10-01 00:29:53 -04001312 out:
Steven Rostedt182e9f52008-11-03 23:15:56 -05001313 ftrace_preempt_enable(resched);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001314 return NULL;
1315}
Robert Richterc4f50182008-12-11 16:49:22 +01001316EXPORT_SYMBOL_GPL(ring_buffer_lock_reserve);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001317
1318static void rb_commit(struct ring_buffer_per_cpu *cpu_buffer,
1319 struct ring_buffer_event *event)
1320{
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001321 cpu_buffer->entries++;
Steven Rostedtbf41a152008-10-04 02:00:59 -04001322
1323 /* Only process further if we own the commit */
1324 if (!rb_is_commit(cpu_buffer, event))
1325 return;
1326
1327 cpu_buffer->write_stamp += event->time_delta;
1328
1329 rb_set_commit_to_write(cpu_buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001330}
1331
1332/**
1333 * ring_buffer_unlock_commit - commit a reserved
1334 * @buffer: The buffer to commit to
1335 * @event: The event pointer to commit.
1336 * @flags: the interrupt flags received from ring_buffer_lock_reserve.
1337 *
1338 * This commits the data to the ring buffer, and releases any locks held.
1339 *
1340 * Must be paired with ring_buffer_lock_reserve.
1341 */
1342int ring_buffer_unlock_commit(struct ring_buffer *buffer,
1343 struct ring_buffer_event *event,
1344 unsigned long flags)
1345{
1346 struct ring_buffer_per_cpu *cpu_buffer;
1347 int cpu = raw_smp_processor_id();
1348
1349 cpu_buffer = buffer->buffers[cpu];
1350
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001351 rb_commit(cpu_buffer, event);
1352
Steven Rostedtbf41a152008-10-04 02:00:59 -04001353 /*
1354 * Only the last preempt count needs to restore preemption.
1355 */
Steven Rostedt182e9f52008-11-03 23:15:56 -05001356 if (preempt_count() == 1)
1357 ftrace_preempt_enable(per_cpu(rb_need_resched, cpu));
1358 else
Steven Rostedtbf41a152008-10-04 02:00:59 -04001359 preempt_enable_no_resched_notrace();
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001360
1361 return 0;
1362}
Robert Richterc4f50182008-12-11 16:49:22 +01001363EXPORT_SYMBOL_GPL(ring_buffer_unlock_commit);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001364
1365/**
1366 * ring_buffer_write - write data to the buffer without reserving
1367 * @buffer: The ring buffer to write to.
1368 * @length: The length of the data being written (excluding the event header)
1369 * @data: The data to write to the buffer.
1370 *
1371 * This is like ring_buffer_lock_reserve and ring_buffer_unlock_commit as
1372 * one function. If you already have the data to write to the buffer, it
1373 * may be easier to simply call this function.
1374 *
1375 * Note, like ring_buffer_lock_reserve, the length is the length of the data
1376 * and not the length of the event which would hold the header.
1377 */
1378int ring_buffer_write(struct ring_buffer *buffer,
1379 unsigned long length,
1380 void *data)
1381{
1382 struct ring_buffer_per_cpu *cpu_buffer;
1383 struct ring_buffer_event *event;
Steven Rostedtbf41a152008-10-04 02:00:59 -04001384 unsigned long event_length;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001385 void *body;
1386 int ret = -EBUSY;
Steven Rostedtbf41a152008-10-04 02:00:59 -04001387 int cpu, resched;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001388
Steven Rostedt033601a2008-11-21 12:41:55 -05001389 if (ring_buffer_flags != RB_BUFFERS_ON)
Steven Rostedta3583242008-11-11 15:01:42 -05001390 return -EBUSY;
1391
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001392 if (atomic_read(&buffer->record_disabled))
1393 return -EBUSY;
1394
Steven Rostedt182e9f52008-11-03 23:15:56 -05001395 resched = ftrace_preempt_disable();
Steven Rostedtbf41a152008-10-04 02:00:59 -04001396
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001397 cpu = raw_smp_processor_id();
1398
1399 if (!cpu_isset(cpu, buffer->cpumask))
Steven Rostedtd7690412008-10-01 00:29:53 -04001400 goto out;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001401
1402 cpu_buffer = buffer->buffers[cpu];
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001403
1404 if (atomic_read(&cpu_buffer->record_disabled))
1405 goto out;
1406
1407 event_length = rb_calculate_event_length(length);
1408 event = rb_reserve_next_event(cpu_buffer,
1409 RINGBUF_TYPE_DATA, event_length);
1410 if (!event)
1411 goto out;
1412
1413 body = rb_event_data(event);
1414
1415 memcpy(body, data, length);
1416
1417 rb_commit(cpu_buffer, event);
1418
1419 ret = 0;
1420 out:
Steven Rostedt182e9f52008-11-03 23:15:56 -05001421 ftrace_preempt_enable(resched);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001422
1423 return ret;
1424}
Robert Richterc4f50182008-12-11 16:49:22 +01001425EXPORT_SYMBOL_GPL(ring_buffer_write);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001426
Steven Rostedtbf41a152008-10-04 02:00:59 -04001427static inline int rb_per_cpu_empty(struct ring_buffer_per_cpu *cpu_buffer)
1428{
1429 struct buffer_page *reader = cpu_buffer->reader_page;
1430 struct buffer_page *head = cpu_buffer->head_page;
1431 struct buffer_page *commit = cpu_buffer->commit_page;
1432
1433 return reader->read == rb_page_commit(reader) &&
1434 (commit == reader ||
1435 (commit == head &&
1436 head->read == rb_page_commit(commit)));
1437}
1438
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001439/**
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001440 * ring_buffer_record_disable - stop all writes into the buffer
1441 * @buffer: The ring buffer to stop writes to.
1442 *
1443 * This prevents all writes to the buffer. Any attempt to write
1444 * to the buffer after this will fail and return NULL.
1445 *
1446 * The caller should call synchronize_sched() after this.
1447 */
1448void ring_buffer_record_disable(struct ring_buffer *buffer)
1449{
1450 atomic_inc(&buffer->record_disabled);
1451}
Robert Richterc4f50182008-12-11 16:49:22 +01001452EXPORT_SYMBOL_GPL(ring_buffer_record_disable);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001453
1454/**
1455 * ring_buffer_record_enable - enable writes to the buffer
1456 * @buffer: The ring buffer to enable writes
1457 *
1458 * Note, multiple disables will need the same number of enables
1459 * to truely enable the writing (much like preempt_disable).
1460 */
1461void ring_buffer_record_enable(struct ring_buffer *buffer)
1462{
1463 atomic_dec(&buffer->record_disabled);
1464}
Robert Richterc4f50182008-12-11 16:49:22 +01001465EXPORT_SYMBOL_GPL(ring_buffer_record_enable);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001466
1467/**
1468 * ring_buffer_record_disable_cpu - stop all writes into the cpu_buffer
1469 * @buffer: The ring buffer to stop writes to.
1470 * @cpu: The CPU buffer to stop
1471 *
1472 * This prevents all writes to the buffer. Any attempt to write
1473 * to the buffer after this will fail and return NULL.
1474 *
1475 * The caller should call synchronize_sched() after this.
1476 */
1477void ring_buffer_record_disable_cpu(struct ring_buffer *buffer, int cpu)
1478{
1479 struct ring_buffer_per_cpu *cpu_buffer;
1480
1481 if (!cpu_isset(cpu, buffer->cpumask))
1482 return;
1483
1484 cpu_buffer = buffer->buffers[cpu];
1485 atomic_inc(&cpu_buffer->record_disabled);
1486}
Robert Richterc4f50182008-12-11 16:49:22 +01001487EXPORT_SYMBOL_GPL(ring_buffer_record_disable_cpu);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001488
1489/**
1490 * ring_buffer_record_enable_cpu - enable writes to the buffer
1491 * @buffer: The ring buffer to enable writes
1492 * @cpu: The CPU to enable.
1493 *
1494 * Note, multiple disables will need the same number of enables
1495 * to truely enable the writing (much like preempt_disable).
1496 */
1497void ring_buffer_record_enable_cpu(struct ring_buffer *buffer, int cpu)
1498{
1499 struct ring_buffer_per_cpu *cpu_buffer;
1500
1501 if (!cpu_isset(cpu, buffer->cpumask))
1502 return;
1503
1504 cpu_buffer = buffer->buffers[cpu];
1505 atomic_dec(&cpu_buffer->record_disabled);
1506}
Robert Richterc4f50182008-12-11 16:49:22 +01001507EXPORT_SYMBOL_GPL(ring_buffer_record_enable_cpu);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001508
1509/**
1510 * ring_buffer_entries_cpu - get the number of entries in a cpu buffer
1511 * @buffer: The ring buffer
1512 * @cpu: The per CPU buffer to get the entries from.
1513 */
1514unsigned long ring_buffer_entries_cpu(struct ring_buffer *buffer, int cpu)
1515{
1516 struct ring_buffer_per_cpu *cpu_buffer;
1517
1518 if (!cpu_isset(cpu, buffer->cpumask))
1519 return 0;
1520
1521 cpu_buffer = buffer->buffers[cpu];
1522 return cpu_buffer->entries;
1523}
Robert Richterc4f50182008-12-11 16:49:22 +01001524EXPORT_SYMBOL_GPL(ring_buffer_entries_cpu);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001525
1526/**
1527 * ring_buffer_overrun_cpu - get the number of overruns in a cpu_buffer
1528 * @buffer: The ring buffer
1529 * @cpu: The per CPU buffer to get the number of overruns from
1530 */
1531unsigned long ring_buffer_overrun_cpu(struct ring_buffer *buffer, int cpu)
1532{
1533 struct ring_buffer_per_cpu *cpu_buffer;
1534
1535 if (!cpu_isset(cpu, buffer->cpumask))
1536 return 0;
1537
1538 cpu_buffer = buffer->buffers[cpu];
1539 return cpu_buffer->overrun;
1540}
Robert Richterc4f50182008-12-11 16:49:22 +01001541EXPORT_SYMBOL_GPL(ring_buffer_overrun_cpu);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001542
1543/**
1544 * ring_buffer_entries - get the number of entries in a buffer
1545 * @buffer: The ring buffer
1546 *
1547 * Returns the total number of entries in the ring buffer
1548 * (all CPU entries)
1549 */
1550unsigned long ring_buffer_entries(struct ring_buffer *buffer)
1551{
1552 struct ring_buffer_per_cpu *cpu_buffer;
1553 unsigned long entries = 0;
1554 int cpu;
1555
1556 /* if you care about this being correct, lock the buffer */
1557 for_each_buffer_cpu(buffer, cpu) {
1558 cpu_buffer = buffer->buffers[cpu];
1559 entries += cpu_buffer->entries;
1560 }
1561
1562 return entries;
1563}
Robert Richterc4f50182008-12-11 16:49:22 +01001564EXPORT_SYMBOL_GPL(ring_buffer_entries);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001565
1566/**
1567 * ring_buffer_overrun_cpu - get the number of overruns in buffer
1568 * @buffer: The ring buffer
1569 *
1570 * Returns the total number of overruns in the ring buffer
1571 * (all CPU entries)
1572 */
1573unsigned long ring_buffer_overruns(struct ring_buffer *buffer)
1574{
1575 struct ring_buffer_per_cpu *cpu_buffer;
1576 unsigned long overruns = 0;
1577 int cpu;
1578
1579 /* if you care about this being correct, lock the buffer */
1580 for_each_buffer_cpu(buffer, cpu) {
1581 cpu_buffer = buffer->buffers[cpu];
1582 overruns += cpu_buffer->overrun;
1583 }
1584
1585 return overruns;
1586}
Robert Richterc4f50182008-12-11 16:49:22 +01001587EXPORT_SYMBOL_GPL(ring_buffer_overruns);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001588
Steven Rostedt642edba2008-11-12 00:01:26 -05001589static void rb_iter_reset(struct ring_buffer_iter *iter)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001590{
1591 struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
1592
Steven Rostedtd7690412008-10-01 00:29:53 -04001593 /* Iterator usage is expected to have record disabled */
1594 if (list_empty(&cpu_buffer->reader_page->list)) {
1595 iter->head_page = cpu_buffer->head_page;
Steven Rostedt6f807ac2008-10-04 02:00:58 -04001596 iter->head = cpu_buffer->head_page->read;
Steven Rostedtd7690412008-10-01 00:29:53 -04001597 } else {
1598 iter->head_page = cpu_buffer->reader_page;
Steven Rostedt6f807ac2008-10-04 02:00:58 -04001599 iter->head = cpu_buffer->reader_page->read;
Steven Rostedtd7690412008-10-01 00:29:53 -04001600 }
1601 if (iter->head)
1602 iter->read_stamp = cpu_buffer->read_stamp;
1603 else
Steven Rostedtabc9b562008-12-02 15:34:06 -05001604 iter->read_stamp = iter->head_page->page->time_stamp;
Steven Rostedt642edba2008-11-12 00:01:26 -05001605}
Steven Rostedtf83c9d02008-11-11 18:47:44 +01001606
Steven Rostedt642edba2008-11-12 00:01:26 -05001607/**
1608 * ring_buffer_iter_reset - reset an iterator
1609 * @iter: The iterator to reset
1610 *
1611 * Resets the iterator, so that it will start from the beginning
1612 * again.
1613 */
1614void ring_buffer_iter_reset(struct ring_buffer_iter *iter)
1615{
1616 struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
1617 unsigned long flags;
1618
1619 spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
1620 rb_iter_reset(iter);
Steven Rostedtf83c9d02008-11-11 18:47:44 +01001621 spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001622}
Robert Richterc4f50182008-12-11 16:49:22 +01001623EXPORT_SYMBOL_GPL(ring_buffer_iter_reset);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001624
1625/**
1626 * ring_buffer_iter_empty - check if an iterator has no more to read
1627 * @iter: The iterator to check
1628 */
1629int ring_buffer_iter_empty(struct ring_buffer_iter *iter)
1630{
1631 struct ring_buffer_per_cpu *cpu_buffer;
1632
1633 cpu_buffer = iter->cpu_buffer;
1634
Steven Rostedtbf41a152008-10-04 02:00:59 -04001635 return iter->head_page == cpu_buffer->commit_page &&
1636 iter->head == rb_commit_index(cpu_buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001637}
Robert Richterc4f50182008-12-11 16:49:22 +01001638EXPORT_SYMBOL_GPL(ring_buffer_iter_empty);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001639
1640static void
1641rb_update_read_stamp(struct ring_buffer_per_cpu *cpu_buffer,
1642 struct ring_buffer_event *event)
1643{
1644 u64 delta;
1645
1646 switch (event->type) {
1647 case RINGBUF_TYPE_PADDING:
1648 return;
1649
1650 case RINGBUF_TYPE_TIME_EXTEND:
1651 delta = event->array[0];
1652 delta <<= TS_SHIFT;
1653 delta += event->time_delta;
1654 cpu_buffer->read_stamp += delta;
1655 return;
1656
1657 case RINGBUF_TYPE_TIME_STAMP:
1658 /* FIXME: not implemented */
1659 return;
1660
1661 case RINGBUF_TYPE_DATA:
1662 cpu_buffer->read_stamp += event->time_delta;
1663 return;
1664
1665 default:
1666 BUG();
1667 }
1668 return;
1669}
1670
1671static void
1672rb_update_iter_read_stamp(struct ring_buffer_iter *iter,
1673 struct ring_buffer_event *event)
1674{
1675 u64 delta;
1676
1677 switch (event->type) {
1678 case RINGBUF_TYPE_PADDING:
1679 return;
1680
1681 case RINGBUF_TYPE_TIME_EXTEND:
1682 delta = event->array[0];
1683 delta <<= TS_SHIFT;
1684 delta += event->time_delta;
1685 iter->read_stamp += delta;
1686 return;
1687
1688 case RINGBUF_TYPE_TIME_STAMP:
1689 /* FIXME: not implemented */
1690 return;
1691
1692 case RINGBUF_TYPE_DATA:
1693 iter->read_stamp += event->time_delta;
1694 return;
1695
1696 default:
1697 BUG();
1698 }
1699 return;
1700}
1701
Steven Rostedtd7690412008-10-01 00:29:53 -04001702static struct buffer_page *
1703rb_get_reader_page(struct ring_buffer_per_cpu *cpu_buffer)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001704{
Steven Rostedtd7690412008-10-01 00:29:53 -04001705 struct buffer_page *reader = NULL;
1706 unsigned long flags;
Steven Rostedt818e3dd2008-10-31 09:58:35 -04001707 int nr_loops = 0;
Steven Rostedtd7690412008-10-01 00:29:53 -04001708
Steven Rostedt3e03fb72008-11-06 00:09:43 -05001709 local_irq_save(flags);
1710 __raw_spin_lock(&cpu_buffer->lock);
Steven Rostedtd7690412008-10-01 00:29:53 -04001711
1712 again:
Steven Rostedt818e3dd2008-10-31 09:58:35 -04001713 /*
1714 * This should normally only loop twice. But because the
1715 * start of the reader inserts an empty page, it causes
1716 * a case where we will loop three times. There should be no
1717 * reason to loop four times (that I know of).
1718 */
Steven Rostedt3e89c7bb2008-11-11 15:28:41 -05001719 if (RB_WARN_ON(cpu_buffer, ++nr_loops > 3)) {
Steven Rostedt818e3dd2008-10-31 09:58:35 -04001720 reader = NULL;
1721 goto out;
1722 }
1723
Steven Rostedtd7690412008-10-01 00:29:53 -04001724 reader = cpu_buffer->reader_page;
1725
1726 /* If there's more to read, return this page */
Steven Rostedtbf41a152008-10-04 02:00:59 -04001727 if (cpu_buffer->reader_page->read < rb_page_size(reader))
Steven Rostedtd7690412008-10-01 00:29:53 -04001728 goto out;
1729
1730 /* Never should we have an index greater than the size */
Steven Rostedt3e89c7bb2008-11-11 15:28:41 -05001731 if (RB_WARN_ON(cpu_buffer,
1732 cpu_buffer->reader_page->read > rb_page_size(reader)))
1733 goto out;
Steven Rostedtd7690412008-10-01 00:29:53 -04001734
1735 /* check if we caught up to the tail */
1736 reader = NULL;
Steven Rostedtbf41a152008-10-04 02:00:59 -04001737 if (cpu_buffer->commit_page == cpu_buffer->reader_page)
Steven Rostedtd7690412008-10-01 00:29:53 -04001738 goto out;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001739
1740 /*
Steven Rostedtd7690412008-10-01 00:29:53 -04001741 * Splice the empty reader page into the list around the head.
1742 * Reset the reader page to size zero.
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001743 */
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001744
Steven Rostedtd7690412008-10-01 00:29:53 -04001745 reader = cpu_buffer->head_page;
1746 cpu_buffer->reader_page->list.next = reader->list.next;
1747 cpu_buffer->reader_page->list.prev = reader->list.prev;
Steven Rostedtbf41a152008-10-04 02:00:59 -04001748
1749 local_set(&cpu_buffer->reader_page->write, 0);
Steven Rostedtabc9b562008-12-02 15:34:06 -05001750 local_set(&cpu_buffer->reader_page->page->commit, 0);
Steven Rostedtd7690412008-10-01 00:29:53 -04001751
1752 /* Make the reader page now replace the head */
1753 reader->list.prev->next = &cpu_buffer->reader_page->list;
1754 reader->list.next->prev = &cpu_buffer->reader_page->list;
1755
1756 /*
1757 * If the tail is on the reader, then we must set the head
1758 * to the inserted page, otherwise we set it one before.
1759 */
1760 cpu_buffer->head_page = cpu_buffer->reader_page;
1761
Steven Rostedtbf41a152008-10-04 02:00:59 -04001762 if (cpu_buffer->commit_page != reader)
Steven Rostedtd7690412008-10-01 00:29:53 -04001763 rb_inc_page(cpu_buffer, &cpu_buffer->head_page);
1764
1765 /* Finally update the reader page to the new head */
1766 cpu_buffer->reader_page = reader;
1767 rb_reset_reader_page(cpu_buffer);
1768
1769 goto again;
1770
1771 out:
Steven Rostedt3e03fb72008-11-06 00:09:43 -05001772 __raw_spin_unlock(&cpu_buffer->lock);
1773 local_irq_restore(flags);
Steven Rostedtd7690412008-10-01 00:29:53 -04001774
1775 return reader;
1776}
1777
1778static void rb_advance_reader(struct ring_buffer_per_cpu *cpu_buffer)
1779{
1780 struct ring_buffer_event *event;
1781 struct buffer_page *reader;
1782 unsigned length;
1783
1784 reader = rb_get_reader_page(cpu_buffer);
1785
1786 /* This function should not be called when buffer is empty */
Steven Rostedt3e89c7bb2008-11-11 15:28:41 -05001787 if (RB_WARN_ON(cpu_buffer, !reader))
1788 return;
Steven Rostedtd7690412008-10-01 00:29:53 -04001789
1790 event = rb_reader_event(cpu_buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001791
1792 if (event->type == RINGBUF_TYPE_DATA)
1793 cpu_buffer->entries--;
1794
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001795 rb_update_read_stamp(cpu_buffer, event);
1796
Steven Rostedtd7690412008-10-01 00:29:53 -04001797 length = rb_event_length(event);
Steven Rostedt6f807ac2008-10-04 02:00:58 -04001798 cpu_buffer->reader_page->read += length;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001799}
1800
1801static void rb_advance_iter(struct ring_buffer_iter *iter)
1802{
1803 struct ring_buffer *buffer;
1804 struct ring_buffer_per_cpu *cpu_buffer;
1805 struct ring_buffer_event *event;
1806 unsigned length;
1807
1808 cpu_buffer = iter->cpu_buffer;
1809 buffer = cpu_buffer->buffer;
1810
1811 /*
1812 * Check if we are at the end of the buffer.
1813 */
Steven Rostedtbf41a152008-10-04 02:00:59 -04001814 if (iter->head >= rb_page_size(iter->head_page)) {
Steven Rostedt3e89c7bb2008-11-11 15:28:41 -05001815 if (RB_WARN_ON(buffer,
1816 iter->head_page == cpu_buffer->commit_page))
1817 return;
Steven Rostedtd7690412008-10-01 00:29:53 -04001818 rb_inc_iter(iter);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001819 return;
1820 }
1821
1822 event = rb_iter_head_event(iter);
1823
1824 length = rb_event_length(event);
1825
1826 /*
1827 * This should not be called to advance the header if we are
1828 * at the tail of the buffer.
1829 */
Steven Rostedt3e89c7bb2008-11-11 15:28:41 -05001830 if (RB_WARN_ON(cpu_buffer,
Steven Rostedtf536aaf2008-11-10 23:07:30 -05001831 (iter->head_page == cpu_buffer->commit_page) &&
Steven Rostedt3e89c7bb2008-11-11 15:28:41 -05001832 (iter->head + length > rb_commit_index(cpu_buffer))))
1833 return;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001834
1835 rb_update_iter_read_stamp(iter, event);
1836
1837 iter->head += length;
1838
1839 /* check for end of page padding */
Steven Rostedtbf41a152008-10-04 02:00:59 -04001840 if ((iter->head >= rb_page_size(iter->head_page)) &&
1841 (iter->head_page != cpu_buffer->commit_page))
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001842 rb_advance_iter(iter);
1843}
1844
Steven Rostedtf83c9d02008-11-11 18:47:44 +01001845static struct ring_buffer_event *
1846rb_buffer_peek(struct ring_buffer *buffer, int cpu, u64 *ts)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001847{
1848 struct ring_buffer_per_cpu *cpu_buffer;
1849 struct ring_buffer_event *event;
Steven Rostedtd7690412008-10-01 00:29:53 -04001850 struct buffer_page *reader;
Steven Rostedt818e3dd2008-10-31 09:58:35 -04001851 int nr_loops = 0;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001852
1853 if (!cpu_isset(cpu, buffer->cpumask))
1854 return NULL;
1855
1856 cpu_buffer = buffer->buffers[cpu];
1857
1858 again:
Steven Rostedt818e3dd2008-10-31 09:58:35 -04001859 /*
1860 * We repeat when a timestamp is encountered. It is possible
1861 * to get multiple timestamps from an interrupt entering just
1862 * as one timestamp is about to be written. The max times
1863 * that this can happen is the number of nested interrupts we
1864 * can have. Nesting 10 deep of interrupts is clearly
1865 * an anomaly.
1866 */
Steven Rostedt3e89c7bb2008-11-11 15:28:41 -05001867 if (RB_WARN_ON(cpu_buffer, ++nr_loops > 10))
Steven Rostedt818e3dd2008-10-31 09:58:35 -04001868 return NULL;
Steven Rostedt818e3dd2008-10-31 09:58:35 -04001869
Steven Rostedtd7690412008-10-01 00:29:53 -04001870 reader = rb_get_reader_page(cpu_buffer);
1871 if (!reader)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001872 return NULL;
1873
Steven Rostedtd7690412008-10-01 00:29:53 -04001874 event = rb_reader_event(cpu_buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001875
1876 switch (event->type) {
1877 case RINGBUF_TYPE_PADDING:
Steven Rostedtbf41a152008-10-04 02:00:59 -04001878 RB_WARN_ON(cpu_buffer, 1);
Steven Rostedtd7690412008-10-01 00:29:53 -04001879 rb_advance_reader(cpu_buffer);
1880 return NULL;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001881
1882 case RINGBUF_TYPE_TIME_EXTEND:
1883 /* Internal data, OK to advance */
Steven Rostedtd7690412008-10-01 00:29:53 -04001884 rb_advance_reader(cpu_buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001885 goto again;
1886
1887 case RINGBUF_TYPE_TIME_STAMP:
1888 /* FIXME: not implemented */
Steven Rostedtd7690412008-10-01 00:29:53 -04001889 rb_advance_reader(cpu_buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001890 goto again;
1891
1892 case RINGBUF_TYPE_DATA:
1893 if (ts) {
1894 *ts = cpu_buffer->read_stamp + event->time_delta;
1895 ring_buffer_normalize_time_stamp(cpu_buffer->cpu, ts);
1896 }
1897 return event;
1898
1899 default:
1900 BUG();
1901 }
1902
1903 return NULL;
1904}
Robert Richterc4f50182008-12-11 16:49:22 +01001905EXPORT_SYMBOL_GPL(ring_buffer_peek);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001906
Steven Rostedtf83c9d02008-11-11 18:47:44 +01001907static struct ring_buffer_event *
1908rb_iter_peek(struct ring_buffer_iter *iter, u64 *ts)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001909{
1910 struct ring_buffer *buffer;
1911 struct ring_buffer_per_cpu *cpu_buffer;
1912 struct ring_buffer_event *event;
Steven Rostedt818e3dd2008-10-31 09:58:35 -04001913 int nr_loops = 0;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001914
1915 if (ring_buffer_iter_empty(iter))
1916 return NULL;
1917
1918 cpu_buffer = iter->cpu_buffer;
1919 buffer = cpu_buffer->buffer;
1920
1921 again:
Steven Rostedt818e3dd2008-10-31 09:58:35 -04001922 /*
1923 * We repeat when a timestamp is encountered. It is possible
1924 * to get multiple timestamps from an interrupt entering just
1925 * as one timestamp is about to be written. The max times
1926 * that this can happen is the number of nested interrupts we
1927 * can have. Nesting 10 deep of interrupts is clearly
1928 * an anomaly.
1929 */
Steven Rostedt3e89c7bb2008-11-11 15:28:41 -05001930 if (RB_WARN_ON(cpu_buffer, ++nr_loops > 10))
Steven Rostedt818e3dd2008-10-31 09:58:35 -04001931 return NULL;
Steven Rostedt818e3dd2008-10-31 09:58:35 -04001932
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001933 if (rb_per_cpu_empty(cpu_buffer))
1934 return NULL;
1935
1936 event = rb_iter_head_event(iter);
1937
1938 switch (event->type) {
1939 case RINGBUF_TYPE_PADDING:
Steven Rostedtd7690412008-10-01 00:29:53 -04001940 rb_inc_iter(iter);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001941 goto again;
1942
1943 case RINGBUF_TYPE_TIME_EXTEND:
1944 /* Internal data, OK to advance */
1945 rb_advance_iter(iter);
1946 goto again;
1947
1948 case RINGBUF_TYPE_TIME_STAMP:
1949 /* FIXME: not implemented */
1950 rb_advance_iter(iter);
1951 goto again;
1952
1953 case RINGBUF_TYPE_DATA:
1954 if (ts) {
1955 *ts = iter->read_stamp + event->time_delta;
1956 ring_buffer_normalize_time_stamp(cpu_buffer->cpu, ts);
1957 }
1958 return event;
1959
1960 default:
1961 BUG();
1962 }
1963
1964 return NULL;
1965}
Robert Richterc4f50182008-12-11 16:49:22 +01001966EXPORT_SYMBOL_GPL(ring_buffer_iter_peek);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001967
1968/**
Steven Rostedtf83c9d02008-11-11 18:47:44 +01001969 * ring_buffer_peek - peek at the next event to be read
1970 * @buffer: The ring buffer to read
1971 * @cpu: The cpu to peak at
1972 * @ts: The timestamp counter of this event.
1973 *
1974 * This will return the event that will be read next, but does
1975 * not consume the data.
1976 */
1977struct ring_buffer_event *
1978ring_buffer_peek(struct ring_buffer *buffer, int cpu, u64 *ts)
1979{
1980 struct ring_buffer_per_cpu *cpu_buffer = buffer->buffers[cpu];
1981 struct ring_buffer_event *event;
1982 unsigned long flags;
1983
1984 spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
1985 event = rb_buffer_peek(buffer, cpu, ts);
1986 spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
1987
1988 return event;
1989}
1990
1991/**
1992 * ring_buffer_iter_peek - peek at the next event to be read
1993 * @iter: The ring buffer iterator
1994 * @ts: The timestamp counter of this event.
1995 *
1996 * This will return the event that will be read next, but does
1997 * not increment the iterator.
1998 */
1999struct ring_buffer_event *
2000ring_buffer_iter_peek(struct ring_buffer_iter *iter, u64 *ts)
2001{
2002 struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
2003 struct ring_buffer_event *event;
2004 unsigned long flags;
2005
2006 spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
2007 event = rb_iter_peek(iter, ts);
2008 spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
2009
2010 return event;
2011}
2012
2013/**
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002014 * ring_buffer_consume - return an event and consume it
2015 * @buffer: The ring buffer to get the next event from
2016 *
2017 * Returns the next event in the ring buffer, and that event is consumed.
2018 * Meaning, that sequential reads will keep returning a different event,
2019 * and eventually empty the ring buffer if the producer is slower.
2020 */
2021struct ring_buffer_event *
2022ring_buffer_consume(struct ring_buffer *buffer, int cpu, u64 *ts)
2023{
Steven Rostedtf83c9d02008-11-11 18:47:44 +01002024 struct ring_buffer_per_cpu *cpu_buffer = buffer->buffers[cpu];
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002025 struct ring_buffer_event *event;
Steven Rostedtf83c9d02008-11-11 18:47:44 +01002026 unsigned long flags;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002027
2028 if (!cpu_isset(cpu, buffer->cpumask))
2029 return NULL;
2030
Steven Rostedtf83c9d02008-11-11 18:47:44 +01002031 spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002032
Steven Rostedtf83c9d02008-11-11 18:47:44 +01002033 event = rb_buffer_peek(buffer, cpu, ts);
2034 if (!event)
2035 goto out;
2036
Steven Rostedtd7690412008-10-01 00:29:53 -04002037 rb_advance_reader(cpu_buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002038
Steven Rostedtf83c9d02008-11-11 18:47:44 +01002039 out:
2040 spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
2041
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002042 return event;
2043}
Robert Richterc4f50182008-12-11 16:49:22 +01002044EXPORT_SYMBOL_GPL(ring_buffer_consume);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002045
2046/**
2047 * ring_buffer_read_start - start a non consuming read of the buffer
2048 * @buffer: The ring buffer to read from
2049 * @cpu: The cpu buffer to iterate over
2050 *
2051 * This starts up an iteration through the buffer. It also disables
2052 * the recording to the buffer until the reading is finished.
2053 * This prevents the reading from being corrupted. This is not
2054 * a consuming read, so a producer is not expected.
2055 *
2056 * Must be paired with ring_buffer_finish.
2057 */
2058struct ring_buffer_iter *
2059ring_buffer_read_start(struct ring_buffer *buffer, int cpu)
2060{
2061 struct ring_buffer_per_cpu *cpu_buffer;
2062 struct ring_buffer_iter *iter;
Steven Rostedtd7690412008-10-01 00:29:53 -04002063 unsigned long flags;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002064
2065 if (!cpu_isset(cpu, buffer->cpumask))
2066 return NULL;
2067
2068 iter = kmalloc(sizeof(*iter), GFP_KERNEL);
2069 if (!iter)
2070 return NULL;
2071
2072 cpu_buffer = buffer->buffers[cpu];
2073
2074 iter->cpu_buffer = cpu_buffer;
2075
2076 atomic_inc(&cpu_buffer->record_disabled);
2077 synchronize_sched();
2078
Steven Rostedtf83c9d02008-11-11 18:47:44 +01002079 spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
Steven Rostedt3e03fb72008-11-06 00:09:43 -05002080 __raw_spin_lock(&cpu_buffer->lock);
Steven Rostedt642edba2008-11-12 00:01:26 -05002081 rb_iter_reset(iter);
Steven Rostedt3e03fb72008-11-06 00:09:43 -05002082 __raw_spin_unlock(&cpu_buffer->lock);
Steven Rostedtf83c9d02008-11-11 18:47:44 +01002083 spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002084
2085 return iter;
2086}
Robert Richterc4f50182008-12-11 16:49:22 +01002087EXPORT_SYMBOL_GPL(ring_buffer_read_start);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002088
2089/**
2090 * ring_buffer_finish - finish reading the iterator of the buffer
2091 * @iter: The iterator retrieved by ring_buffer_start
2092 *
2093 * This re-enables the recording to the buffer, and frees the
2094 * iterator.
2095 */
2096void
2097ring_buffer_read_finish(struct ring_buffer_iter *iter)
2098{
2099 struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
2100
2101 atomic_dec(&cpu_buffer->record_disabled);
2102 kfree(iter);
2103}
Robert Richterc4f50182008-12-11 16:49:22 +01002104EXPORT_SYMBOL_GPL(ring_buffer_read_finish);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002105
2106/**
2107 * ring_buffer_read - read the next item in the ring buffer by the iterator
2108 * @iter: The ring buffer iterator
2109 * @ts: The time stamp of the event read.
2110 *
2111 * This reads the next event in the ring buffer and increments the iterator.
2112 */
2113struct ring_buffer_event *
2114ring_buffer_read(struct ring_buffer_iter *iter, u64 *ts)
2115{
2116 struct ring_buffer_event *event;
Steven Rostedtf83c9d02008-11-11 18:47:44 +01002117 struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
2118 unsigned long flags;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002119
Steven Rostedtf83c9d02008-11-11 18:47:44 +01002120 spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
2121 event = rb_iter_peek(iter, ts);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002122 if (!event)
Steven Rostedtf83c9d02008-11-11 18:47:44 +01002123 goto out;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002124
2125 rb_advance_iter(iter);
Steven Rostedtf83c9d02008-11-11 18:47:44 +01002126 out:
2127 spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002128
2129 return event;
2130}
Robert Richterc4f50182008-12-11 16:49:22 +01002131EXPORT_SYMBOL_GPL(ring_buffer_read);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002132
2133/**
2134 * ring_buffer_size - return the size of the ring buffer (in bytes)
2135 * @buffer: The ring buffer.
2136 */
2137unsigned long ring_buffer_size(struct ring_buffer *buffer)
2138{
2139 return BUF_PAGE_SIZE * buffer->pages;
2140}
Robert Richterc4f50182008-12-11 16:49:22 +01002141EXPORT_SYMBOL_GPL(ring_buffer_size);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002142
2143static void
2144rb_reset_cpu(struct ring_buffer_per_cpu *cpu_buffer)
2145{
2146 cpu_buffer->head_page
2147 = list_entry(cpu_buffer->pages.next, struct buffer_page, list);
Steven Rostedtbf41a152008-10-04 02:00:59 -04002148 local_set(&cpu_buffer->head_page->write, 0);
Steven Rostedtabc9b562008-12-02 15:34:06 -05002149 local_set(&cpu_buffer->head_page->page->commit, 0);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002150
Steven Rostedt6f807ac2008-10-04 02:00:58 -04002151 cpu_buffer->head_page->read = 0;
Steven Rostedtbf41a152008-10-04 02:00:59 -04002152
2153 cpu_buffer->tail_page = cpu_buffer->head_page;
2154 cpu_buffer->commit_page = cpu_buffer->head_page;
2155
2156 INIT_LIST_HEAD(&cpu_buffer->reader_page->list);
2157 local_set(&cpu_buffer->reader_page->write, 0);
Steven Rostedtabc9b562008-12-02 15:34:06 -05002158 local_set(&cpu_buffer->reader_page->page->commit, 0);
Steven Rostedt6f807ac2008-10-04 02:00:58 -04002159 cpu_buffer->reader_page->read = 0;
Steven Rostedtd7690412008-10-01 00:29:53 -04002160
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002161 cpu_buffer->overrun = 0;
2162 cpu_buffer->entries = 0;
2163}
2164
2165/**
2166 * ring_buffer_reset_cpu - reset a ring buffer per CPU buffer
2167 * @buffer: The ring buffer to reset a per cpu buffer of
2168 * @cpu: The CPU buffer to be reset
2169 */
2170void ring_buffer_reset_cpu(struct ring_buffer *buffer, int cpu)
2171{
2172 struct ring_buffer_per_cpu *cpu_buffer = buffer->buffers[cpu];
2173 unsigned long flags;
2174
2175 if (!cpu_isset(cpu, buffer->cpumask))
2176 return;
2177
Steven Rostedtf83c9d02008-11-11 18:47:44 +01002178 spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
2179
Steven Rostedt3e03fb72008-11-06 00:09:43 -05002180 __raw_spin_lock(&cpu_buffer->lock);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002181
2182 rb_reset_cpu(cpu_buffer);
2183
Steven Rostedt3e03fb72008-11-06 00:09:43 -05002184 __raw_spin_unlock(&cpu_buffer->lock);
Steven Rostedtf83c9d02008-11-11 18:47:44 +01002185
2186 spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002187}
Robert Richterc4f50182008-12-11 16:49:22 +01002188EXPORT_SYMBOL_GPL(ring_buffer_reset_cpu);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002189
2190/**
2191 * ring_buffer_reset - reset a ring buffer
2192 * @buffer: The ring buffer to reset all cpu buffers
2193 */
2194void ring_buffer_reset(struct ring_buffer *buffer)
2195{
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002196 int cpu;
2197
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002198 for_each_buffer_cpu(buffer, cpu)
Steven Rostedtd7690412008-10-01 00:29:53 -04002199 ring_buffer_reset_cpu(buffer, cpu);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002200}
Robert Richterc4f50182008-12-11 16:49:22 +01002201EXPORT_SYMBOL_GPL(ring_buffer_reset);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002202
2203/**
2204 * rind_buffer_empty - is the ring buffer empty?
2205 * @buffer: The ring buffer to test
2206 */
2207int ring_buffer_empty(struct ring_buffer *buffer)
2208{
2209 struct ring_buffer_per_cpu *cpu_buffer;
2210 int cpu;
2211
2212 /* yes this is racy, but if you don't like the race, lock the buffer */
2213 for_each_buffer_cpu(buffer, cpu) {
2214 cpu_buffer = buffer->buffers[cpu];
2215 if (!rb_per_cpu_empty(cpu_buffer))
2216 return 0;
2217 }
2218 return 1;
2219}
Robert Richterc4f50182008-12-11 16:49:22 +01002220EXPORT_SYMBOL_GPL(ring_buffer_empty);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002221
2222/**
2223 * ring_buffer_empty_cpu - is a cpu buffer of a ring buffer empty?
2224 * @buffer: The ring buffer
2225 * @cpu: The CPU buffer to test
2226 */
2227int ring_buffer_empty_cpu(struct ring_buffer *buffer, int cpu)
2228{
2229 struct ring_buffer_per_cpu *cpu_buffer;
2230
2231 if (!cpu_isset(cpu, buffer->cpumask))
2232 return 1;
2233
2234 cpu_buffer = buffer->buffers[cpu];
2235 return rb_per_cpu_empty(cpu_buffer);
2236}
Robert Richterc4f50182008-12-11 16:49:22 +01002237EXPORT_SYMBOL_GPL(ring_buffer_empty_cpu);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002238
2239/**
2240 * ring_buffer_swap_cpu - swap a CPU buffer between two ring buffers
2241 * @buffer_a: One buffer to swap with
2242 * @buffer_b: The other buffer to swap with
2243 *
2244 * This function is useful for tracers that want to take a "snapshot"
2245 * of a CPU buffer and has another back up buffer lying around.
2246 * it is expected that the tracer handles the cpu buffer not being
2247 * used at the moment.
2248 */
2249int ring_buffer_swap_cpu(struct ring_buffer *buffer_a,
2250 struct ring_buffer *buffer_b, int cpu)
2251{
2252 struct ring_buffer_per_cpu *cpu_buffer_a;
2253 struct ring_buffer_per_cpu *cpu_buffer_b;
2254
2255 if (!cpu_isset(cpu, buffer_a->cpumask) ||
2256 !cpu_isset(cpu, buffer_b->cpumask))
2257 return -EINVAL;
2258
2259 /* At least make sure the two buffers are somewhat the same */
Lai Jiangshan6d102bc2008-12-17 17:48:23 +08002260 if (buffer_a->pages != buffer_b->pages)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002261 return -EINVAL;
2262
2263 cpu_buffer_a = buffer_a->buffers[cpu];
2264 cpu_buffer_b = buffer_b->buffers[cpu];
2265
2266 /*
2267 * We can't do a synchronize_sched here because this
2268 * function can be called in atomic context.
2269 * Normally this will be called from the same CPU as cpu.
2270 * If not it's up to the caller to protect this.
2271 */
2272 atomic_inc(&cpu_buffer_a->record_disabled);
2273 atomic_inc(&cpu_buffer_b->record_disabled);
2274
2275 buffer_a->buffers[cpu] = cpu_buffer_b;
2276 buffer_b->buffers[cpu] = cpu_buffer_a;
2277
2278 cpu_buffer_b->buffer = buffer_a;
2279 cpu_buffer_a->buffer = buffer_b;
2280
2281 atomic_dec(&cpu_buffer_a->record_disabled);
2282 atomic_dec(&cpu_buffer_b->record_disabled);
2283
2284 return 0;
2285}
Robert Richterc4f50182008-12-11 16:49:22 +01002286EXPORT_SYMBOL_GPL(ring_buffer_swap_cpu);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002287
Steven Rostedt8789a9e2008-12-02 15:34:07 -05002288static void rb_remove_entries(struct ring_buffer_per_cpu *cpu_buffer,
Steven Rostedt044fa782008-12-02 23:50:03 -05002289 struct buffer_data_page *bpage)
Steven Rostedt8789a9e2008-12-02 15:34:07 -05002290{
2291 struct ring_buffer_event *event;
2292 unsigned long head;
2293
2294 __raw_spin_lock(&cpu_buffer->lock);
Steven Rostedt044fa782008-12-02 23:50:03 -05002295 for (head = 0; head < local_read(&bpage->commit);
Steven Rostedt8789a9e2008-12-02 15:34:07 -05002296 head += rb_event_length(event)) {
2297
Steven Rostedt044fa782008-12-02 23:50:03 -05002298 event = __rb_data_page_index(bpage, head);
Steven Rostedt8789a9e2008-12-02 15:34:07 -05002299 if (RB_WARN_ON(cpu_buffer, rb_null_event(event)))
2300 return;
2301 /* Only count data entries */
2302 if (event->type != RINGBUF_TYPE_DATA)
2303 continue;
2304 cpu_buffer->entries--;
2305 }
2306 __raw_spin_unlock(&cpu_buffer->lock);
2307}
2308
2309/**
2310 * ring_buffer_alloc_read_page - allocate a page to read from buffer
2311 * @buffer: the buffer to allocate for.
2312 *
2313 * This function is used in conjunction with ring_buffer_read_page.
2314 * When reading a full page from the ring buffer, these functions
2315 * can be used to speed up the process. The calling function should
2316 * allocate a few pages first with this function. Then when it
2317 * needs to get pages from the ring buffer, it passes the result
2318 * of this function into ring_buffer_read_page, which will swap
2319 * the page that was allocated, with the read page of the buffer.
2320 *
2321 * Returns:
2322 * The page allocated, or NULL on error.
2323 */
2324void *ring_buffer_alloc_read_page(struct ring_buffer *buffer)
2325{
2326 unsigned long addr;
Steven Rostedt044fa782008-12-02 23:50:03 -05002327 struct buffer_data_page *bpage;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05002328
2329 addr = __get_free_page(GFP_KERNEL);
2330 if (!addr)
2331 return NULL;
2332
Steven Rostedt044fa782008-12-02 23:50:03 -05002333 bpage = (void *)addr;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05002334
Steven Rostedt044fa782008-12-02 23:50:03 -05002335 return bpage;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05002336}
2337
2338/**
2339 * ring_buffer_free_read_page - free an allocated read page
2340 * @buffer: the buffer the page was allocate for
2341 * @data: the page to free
2342 *
2343 * Free a page allocated from ring_buffer_alloc_read_page.
2344 */
2345void ring_buffer_free_read_page(struct ring_buffer *buffer, void *data)
2346{
2347 free_page((unsigned long)data);
2348}
2349
2350/**
2351 * ring_buffer_read_page - extract a page from the ring buffer
2352 * @buffer: buffer to extract from
2353 * @data_page: the page to use allocated from ring_buffer_alloc_read_page
2354 * @cpu: the cpu of the buffer to extract
2355 * @full: should the extraction only happen when the page is full.
2356 *
2357 * This function will pull out a page from the ring buffer and consume it.
2358 * @data_page must be the address of the variable that was returned
2359 * from ring_buffer_alloc_read_page. This is because the page might be used
2360 * to swap with a page in the ring buffer.
2361 *
2362 * for example:
2363 * rpage = ring_buffer_alloc_page(buffer);
2364 * if (!rpage)
2365 * return error;
2366 * ret = ring_buffer_read_page(buffer, &rpage, cpu, 0);
2367 * if (ret)
2368 * process_page(rpage);
2369 *
2370 * When @full is set, the function will not return true unless
2371 * the writer is off the reader page.
2372 *
2373 * Note: it is up to the calling functions to handle sleeps and wakeups.
2374 * The ring buffer can be used anywhere in the kernel and can not
2375 * blindly call wake_up. The layer that uses the ring buffer must be
2376 * responsible for that.
2377 *
2378 * Returns:
2379 * 1 if data has been transferred
2380 * 0 if no data has been transferred.
2381 */
2382int ring_buffer_read_page(struct ring_buffer *buffer,
2383 void **data_page, int cpu, int full)
2384{
2385 struct ring_buffer_per_cpu *cpu_buffer = buffer->buffers[cpu];
2386 struct ring_buffer_event *event;
Steven Rostedt044fa782008-12-02 23:50:03 -05002387 struct buffer_data_page *bpage;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05002388 unsigned long flags;
2389 int ret = 0;
2390
2391 if (!data_page)
2392 return 0;
2393
Steven Rostedt044fa782008-12-02 23:50:03 -05002394 bpage = *data_page;
2395 if (!bpage)
Steven Rostedt8789a9e2008-12-02 15:34:07 -05002396 return 0;
2397
2398 spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
2399
2400 /*
2401 * rb_buffer_peek will get the next ring buffer if
2402 * the current reader page is empty.
2403 */
2404 event = rb_buffer_peek(buffer, cpu, NULL);
2405 if (!event)
2406 goto out;
2407
2408 /* check for data */
2409 if (!local_read(&cpu_buffer->reader_page->page->commit))
2410 goto out;
2411 /*
2412 * If the writer is already off of the read page, then simply
2413 * switch the read page with the given page. Otherwise
2414 * we need to copy the data from the reader to the writer.
2415 */
2416 if (cpu_buffer->reader_page == cpu_buffer->commit_page) {
2417 unsigned int read = cpu_buffer->reader_page->read;
2418
2419 if (full)
2420 goto out;
2421 /* The writer is still on the reader page, we must copy */
Steven Rostedt044fa782008-12-02 23:50:03 -05002422 bpage = cpu_buffer->reader_page->page;
2423 memcpy(bpage->data,
Steven Rostedt8789a9e2008-12-02 15:34:07 -05002424 cpu_buffer->reader_page->page->data + read,
Steven Rostedt044fa782008-12-02 23:50:03 -05002425 local_read(&bpage->commit) - read);
Steven Rostedt8789a9e2008-12-02 15:34:07 -05002426
2427 /* consume what was read */
2428 cpu_buffer->reader_page += read;
2429
2430 } else {
2431 /* swap the pages */
Steven Rostedt044fa782008-12-02 23:50:03 -05002432 rb_init_page(bpage);
2433 bpage = cpu_buffer->reader_page->page;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05002434 cpu_buffer->reader_page->page = *data_page;
2435 cpu_buffer->reader_page->read = 0;
Steven Rostedt044fa782008-12-02 23:50:03 -05002436 *data_page = bpage;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05002437 }
2438 ret = 1;
2439
2440 /* update the entry counter */
Steven Rostedt044fa782008-12-02 23:50:03 -05002441 rb_remove_entries(cpu_buffer, bpage);
Steven Rostedt8789a9e2008-12-02 15:34:07 -05002442 out:
2443 spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
2444
2445 return ret;
2446}
2447
Steven Rostedta3583242008-11-11 15:01:42 -05002448static ssize_t
2449rb_simple_read(struct file *filp, char __user *ubuf,
2450 size_t cnt, loff_t *ppos)
2451{
Steven Rostedt033601a2008-11-21 12:41:55 -05002452 long *p = filp->private_data;
Steven Rostedta3583242008-11-11 15:01:42 -05002453 char buf[64];
2454 int r;
2455
Steven Rostedt033601a2008-11-21 12:41:55 -05002456 if (test_bit(RB_BUFFERS_DISABLED_BIT, p))
2457 r = sprintf(buf, "permanently disabled\n");
2458 else
2459 r = sprintf(buf, "%d\n", test_bit(RB_BUFFERS_ON_BIT, p));
Steven Rostedta3583242008-11-11 15:01:42 -05002460
2461 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2462}
2463
2464static ssize_t
2465rb_simple_write(struct file *filp, const char __user *ubuf,
2466 size_t cnt, loff_t *ppos)
2467{
Steven Rostedt033601a2008-11-21 12:41:55 -05002468 long *p = filp->private_data;
Steven Rostedta3583242008-11-11 15:01:42 -05002469 char buf[64];
2470 long val;
2471 int ret;
2472
2473 if (cnt >= sizeof(buf))
2474 return -EINVAL;
2475
2476 if (copy_from_user(&buf, ubuf, cnt))
2477 return -EFAULT;
2478
2479 buf[cnt] = 0;
2480
2481 ret = strict_strtoul(buf, 10, &val);
2482 if (ret < 0)
2483 return ret;
2484
Steven Rostedt033601a2008-11-21 12:41:55 -05002485 if (val)
2486 set_bit(RB_BUFFERS_ON_BIT, p);
2487 else
2488 clear_bit(RB_BUFFERS_ON_BIT, p);
Steven Rostedta3583242008-11-11 15:01:42 -05002489
2490 (*ppos)++;
2491
2492 return cnt;
2493}
2494
2495static struct file_operations rb_simple_fops = {
2496 .open = tracing_open_generic,
2497 .read = rb_simple_read,
2498 .write = rb_simple_write,
2499};
2500
2501
2502static __init int rb_init_debugfs(void)
2503{
2504 struct dentry *d_tracer;
2505 struct dentry *entry;
2506
2507 d_tracer = tracing_init_dentry();
2508
2509 entry = debugfs_create_file("tracing_on", 0644, d_tracer,
Steven Rostedt033601a2008-11-21 12:41:55 -05002510 &ring_buffer_flags, &rb_simple_fops);
Steven Rostedta3583242008-11-11 15:01:42 -05002511 if (!entry)
2512 pr_warning("Could not create debugfs 'tracing_on' entry\n");
2513
2514 return 0;
2515}
2516
2517fs_initcall(rb_init_debugfs);