blob: 7bcfd3e605375a8176360e0bb2b20649d758872c [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>
Ingo Molnar14131f22009-02-26 18:47:11 +01007#include <linux/trace_clock.h>
Steven Rostedt78d904b2009-02-05 18:43:07 -05008#include <linux/ftrace_irq.h>
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04009#include <linux/spinlock.h>
10#include <linux/debugfs.h>
11#include <linux/uaccess.h>
Steven Rostedta81bd802009-02-06 01:45:16 -050012#include <linux/hardirq.h>
Steven Rostedt7a8e76a2008-09-29 23:02:38 -040013#include <linux/module.h>
14#include <linux/percpu.h>
15#include <linux/mutex.h>
Steven Rostedt7a8e76a2008-09-29 23:02:38 -040016#include <linux/init.h>
17#include <linux/hash.h>
18#include <linux/list.h>
Steven Rostedt554f7862009-03-11 22:00:13 -040019#include <linux/cpu.h>
Steven Rostedt7a8e76a2008-09-29 23:02:38 -040020#include <linux/fs.h>
21
Steven Rostedt182e9f52008-11-03 23:15:56 -050022#include "trace.h"
23
Steven Rostedt033601a2008-11-21 12:41:55 -050024/*
Steven Rostedtd1b182a2009-04-15 16:53:47 -040025 * The ring buffer header is special. We must manually up keep it.
26 */
27int ring_buffer_print_entry_header(struct trace_seq *s)
28{
29 int ret;
30
31 ret = trace_seq_printf(s, "\ttype : 2 bits\n");
32 ret = trace_seq_printf(s, "\tlen : 3 bits\n");
33 ret = trace_seq_printf(s, "\ttime_delta : 27 bits\n");
34 ret = trace_seq_printf(s, "\tarray : 32 bits\n");
35 ret = trace_seq_printf(s, "\n");
36 ret = trace_seq_printf(s, "\tpadding : type == %d\n",
37 RINGBUF_TYPE_PADDING);
38 ret = trace_seq_printf(s, "\ttime_extend : type == %d\n",
39 RINGBUF_TYPE_TIME_EXTEND);
40 ret = trace_seq_printf(s, "\tdata : type == %d\n",
41 RINGBUF_TYPE_DATA);
42
43 return ret;
44}
45
46/*
Steven Rostedt5cc98542009-03-12 22:24:17 -040047 * The ring buffer is made up of a list of pages. A separate list of pages is
48 * allocated for each CPU. A writer may only write to a buffer that is
49 * associated with the CPU it is currently executing on. A reader may read
50 * from any per cpu buffer.
51 *
52 * The reader is special. For each per cpu buffer, the reader has its own
53 * reader page. When a reader has read the entire reader page, this reader
54 * page is swapped with another page in the ring buffer.
55 *
56 * Now, as long as the writer is off the reader page, the reader can do what
57 * ever it wants with that page. The writer will never write to that page
58 * again (as long as it is out of the ring buffer).
59 *
60 * Here's some silly ASCII art.
61 *
62 * +------+
63 * |reader| RING BUFFER
64 * |page |
65 * +------+ +---+ +---+ +---+
66 * | |-->| |-->| |
67 * +---+ +---+ +---+
68 * ^ |
69 * | |
70 * +---------------+
71 *
72 *
73 * +------+
74 * |reader| RING BUFFER
75 * |page |------------------v
76 * +------+ +---+ +---+ +---+
77 * | |-->| |-->| |
78 * +---+ +---+ +---+
79 * ^ |
80 * | |
81 * +---------------+
82 *
83 *
84 * +------+
85 * |reader| RING BUFFER
86 * |page |------------------v
87 * +------+ +---+ +---+ +---+
88 * ^ | |-->| |-->| |
89 * | +---+ +---+ +---+
90 * | |
91 * | |
92 * +------------------------------+
93 *
94 *
95 * +------+
96 * |buffer| RING BUFFER
97 * |page |------------------v
98 * +------+ +---+ +---+ +---+
99 * ^ | | | |-->| |
100 * | New +---+ +---+ +---+
101 * | Reader------^ |
102 * | page |
103 * +------------------------------+
104 *
105 *
106 * After we make this swap, the reader can hand this page off to the splice
107 * code and be done with it. It can even allocate a new page if it needs to
108 * and swap that into the ring buffer.
109 *
110 * We will be using cmpxchg soon to make all this lockless.
111 *
112 */
113
114/*
Steven Rostedt033601a2008-11-21 12:41:55 -0500115 * A fast way to enable or disable all ring buffers is to
116 * call tracing_on or tracing_off. Turning off the ring buffers
117 * prevents all ring buffers from being recorded to.
118 * Turning this switch on, makes it OK to write to the
119 * ring buffer, if the ring buffer is enabled itself.
120 *
121 * There's three layers that must be on in order to write
122 * to the ring buffer.
123 *
124 * 1) This global flag must be set.
125 * 2) The ring buffer must be enabled for recording.
126 * 3) The per cpu buffer must be enabled for recording.
127 *
128 * In case of an anomaly, this global flag has a bit set that
129 * will permantly disable all ring buffers.
130 */
131
132/*
133 * Global flag to disable all recording to ring buffers
134 * This has two bits: ON, DISABLED
135 *
136 * ON DISABLED
137 * ---- ----------
138 * 0 0 : ring buffers are off
139 * 1 0 : ring buffers are on
140 * X 1 : ring buffers are permanently disabled
141 */
142
143enum {
144 RB_BUFFERS_ON_BIT = 0,
145 RB_BUFFERS_DISABLED_BIT = 1,
146};
147
148enum {
149 RB_BUFFERS_ON = 1 << RB_BUFFERS_ON_BIT,
150 RB_BUFFERS_DISABLED = 1 << RB_BUFFERS_DISABLED_BIT,
151};
152
Hannes Eder5e398412009-02-10 19:44:34 +0100153static unsigned long ring_buffer_flags __read_mostly = RB_BUFFERS_ON;
Steven Rostedta3583242008-11-11 15:01:42 -0500154
Steven Rostedt474d32b2009-03-03 19:51:40 -0500155#define BUF_PAGE_HDR_SIZE offsetof(struct buffer_data_page, data)
156
Steven Rostedta3583242008-11-11 15:01:42 -0500157/**
158 * tracing_on - enable all tracing buffers
159 *
160 * This function enables all tracing buffers that may have been
161 * disabled with tracing_off.
162 */
163void tracing_on(void)
164{
Steven Rostedt033601a2008-11-21 12:41:55 -0500165 set_bit(RB_BUFFERS_ON_BIT, &ring_buffer_flags);
Steven Rostedta3583242008-11-11 15:01:42 -0500166}
Robert Richterc4f50182008-12-11 16:49:22 +0100167EXPORT_SYMBOL_GPL(tracing_on);
Steven Rostedta3583242008-11-11 15:01:42 -0500168
169/**
170 * tracing_off - turn off all tracing buffers
171 *
172 * This function stops all tracing buffers from recording data.
173 * It does not disable any overhead the tracers themselves may
174 * be causing. This function simply causes all recording to
175 * the ring buffers to fail.
176 */
177void tracing_off(void)
178{
Steven Rostedt033601a2008-11-21 12:41:55 -0500179 clear_bit(RB_BUFFERS_ON_BIT, &ring_buffer_flags);
180}
Robert Richterc4f50182008-12-11 16:49:22 +0100181EXPORT_SYMBOL_GPL(tracing_off);
Steven Rostedt033601a2008-11-21 12:41:55 -0500182
183/**
184 * tracing_off_permanent - permanently disable ring buffers
185 *
186 * This function, once called, will disable all ring buffers
Wenji Huangc3706f02009-02-10 01:03:18 -0500187 * permanently.
Steven Rostedt033601a2008-11-21 12:41:55 -0500188 */
189void tracing_off_permanent(void)
190{
191 set_bit(RB_BUFFERS_DISABLED_BIT, &ring_buffer_flags);
Steven Rostedta3583242008-11-11 15:01:42 -0500192}
193
Steven Rostedt988ae9d2009-02-14 19:17:02 -0500194/**
195 * tracing_is_on - show state of ring buffers enabled
196 */
197int tracing_is_on(void)
198{
199 return ring_buffer_flags == RB_BUFFERS_ON;
200}
201EXPORT_SYMBOL_GPL(tracing_is_on);
202
Ingo Molnard06bbd62008-11-12 10:11:37 +0100203#include "trace.h"
204
Steven Rostedte3d6bf02009-03-03 13:53:07 -0500205#define RB_EVNT_HDR_SIZE (offsetof(struct ring_buffer_event, array))
Andrew Morton67d34722009-01-09 12:27:09 -0800206#define RB_ALIGNMENT 4U
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400207#define RB_MAX_SMALL_DATA 28
208
209enum {
210 RB_LEN_TIME_EXTEND = 8,
211 RB_LEN_TIME_STAMP = 16,
212};
213
Tom Zanussi2d622712009-03-22 03:30:49 -0500214static inline int rb_null_event(struct ring_buffer_event *event)
215{
216 return event->type == RINGBUF_TYPE_PADDING && event->time_delta == 0;
217}
218
219static inline int rb_discarded_event(struct ring_buffer_event *event)
220{
221 return event->type == RINGBUF_TYPE_PADDING && event->time_delta;
222}
223
224static void rb_event_set_padding(struct ring_buffer_event *event)
225{
226 event->type = RINGBUF_TYPE_PADDING;
227 event->time_delta = 0;
228}
229
Tom Zanussi2d622712009-03-22 03:30:49 -0500230static unsigned
231rb_event_data_length(struct ring_buffer_event *event)
232{
233 unsigned length;
234
235 if (event->len)
236 length = event->len * RB_ALIGNMENT;
237 else
238 length = event->array[0];
239 return length + RB_EVNT_HDR_SIZE;
240}
241
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400242/* inline for ring buffer fast paths */
Andrew Morton34a148b2009-01-09 12:27:09 -0800243static unsigned
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400244rb_event_length(struct ring_buffer_event *event)
245{
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400246 switch (event->type) {
247 case RINGBUF_TYPE_PADDING:
Tom Zanussi2d622712009-03-22 03:30:49 -0500248 if (rb_null_event(event))
249 /* undefined */
250 return -1;
251 return rb_event_data_length(event);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400252
253 case RINGBUF_TYPE_TIME_EXTEND:
254 return RB_LEN_TIME_EXTEND;
255
256 case RINGBUF_TYPE_TIME_STAMP:
257 return RB_LEN_TIME_STAMP;
258
259 case RINGBUF_TYPE_DATA:
Tom Zanussi2d622712009-03-22 03:30:49 -0500260 return rb_event_data_length(event);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400261 default:
262 BUG();
263 }
264 /* not hit */
265 return 0;
266}
267
268/**
269 * ring_buffer_event_length - return the length of the event
270 * @event: the event to get the length of
271 */
272unsigned ring_buffer_event_length(struct ring_buffer_event *event)
273{
Robert Richter465634a2009-01-07 15:32:11 +0100274 unsigned length = rb_event_length(event);
275 if (event->type != RINGBUF_TYPE_DATA)
276 return length;
277 length -= RB_EVNT_HDR_SIZE;
278 if (length > RB_MAX_SMALL_DATA + sizeof(event->array[0]))
279 length -= sizeof(event->array[0]);
280 return length;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400281}
Robert Richterc4f50182008-12-11 16:49:22 +0100282EXPORT_SYMBOL_GPL(ring_buffer_event_length);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400283
284/* inline for ring buffer fast paths */
Andrew Morton34a148b2009-01-09 12:27:09 -0800285static void *
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400286rb_event_data(struct ring_buffer_event *event)
287{
288 BUG_ON(event->type != RINGBUF_TYPE_DATA);
289 /* If length is in len field, then array[0] has the data */
290 if (event->len)
291 return (void *)&event->array[0];
292 /* Otherwise length is in array[0] and array[1] has the data */
293 return (void *)&event->array[1];
294}
295
296/**
297 * ring_buffer_event_data - return the data of the event
298 * @event: the event to get the data from
299 */
300void *ring_buffer_event_data(struct ring_buffer_event *event)
301{
302 return rb_event_data(event);
303}
Robert Richterc4f50182008-12-11 16:49:22 +0100304EXPORT_SYMBOL_GPL(ring_buffer_event_data);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400305
306#define for_each_buffer_cpu(buffer, cpu) \
Rusty Russell9e01c1b2009-01-01 10:12:22 +1030307 for_each_cpu(cpu, buffer->cpumask)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400308
309#define TS_SHIFT 27
310#define TS_MASK ((1ULL << TS_SHIFT) - 1)
311#define TS_DELTA_TEST (~TS_MASK)
312
Steven Rostedtabc9b562008-12-02 15:34:06 -0500313struct buffer_data_page {
Steven Rostedte4c2ce82008-10-01 11:14:54 -0400314 u64 time_stamp; /* page time stamp */
Wenji Huangc3706f02009-02-10 01:03:18 -0500315 local_t commit; /* write committed index */
Steven Rostedtabc9b562008-12-02 15:34:06 -0500316 unsigned char data[]; /* data of buffer page */
317};
318
319struct buffer_page {
320 local_t write; /* index for next write */
Steven Rostedt6f807ac2008-10-04 02:00:58 -0400321 unsigned read; /* index for next read */
Steven Rostedte4c2ce82008-10-01 11:14:54 -0400322 struct list_head list; /* list of free pages */
Steven Rostedtabc9b562008-12-02 15:34:06 -0500323 struct buffer_data_page *page; /* Actual data page */
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400324};
325
Steven Rostedt044fa782008-12-02 23:50:03 -0500326static void rb_init_page(struct buffer_data_page *bpage)
Steven Rostedtabc9b562008-12-02 15:34:06 -0500327{
Steven Rostedt044fa782008-12-02 23:50:03 -0500328 local_set(&bpage->commit, 0);
Steven Rostedtabc9b562008-12-02 15:34:06 -0500329}
330
Steven Rostedt474d32b2009-03-03 19:51:40 -0500331/**
332 * ring_buffer_page_len - the size of data on the page.
333 * @page: The page to read
334 *
335 * Returns the amount of data on the page, including buffer page header.
336 */
Steven Rostedtef7a4a12009-03-03 00:27:49 -0500337size_t ring_buffer_page_len(void *page)
338{
Steven Rostedt474d32b2009-03-03 19:51:40 -0500339 return local_read(&((struct buffer_data_page *)page)->commit)
340 + BUF_PAGE_HDR_SIZE;
Steven Rostedtef7a4a12009-03-03 00:27:49 -0500341}
342
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400343/*
Steven Rostedted568292008-09-29 23:02:40 -0400344 * Also stolen from mm/slob.c. Thanks to Mathieu Desnoyers for pointing
345 * this issue out.
346 */
Andrew Morton34a148b2009-01-09 12:27:09 -0800347static void free_buffer_page(struct buffer_page *bpage)
Steven Rostedted568292008-09-29 23:02:40 -0400348{
Andrew Morton34a148b2009-01-09 12:27:09 -0800349 free_page((unsigned long)bpage->page);
Steven Rostedte4c2ce82008-10-01 11:14:54 -0400350 kfree(bpage);
Steven Rostedted568292008-09-29 23:02:40 -0400351}
352
353/*
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400354 * We need to fit the time_stamp delta into 27 bits.
355 */
356static inline int test_time_stamp(u64 delta)
357{
358 if (delta & TS_DELTA_TEST)
359 return 1;
360 return 0;
361}
362
Steven Rostedt474d32b2009-03-03 19:51:40 -0500363#define BUF_PAGE_SIZE (PAGE_SIZE - BUF_PAGE_HDR_SIZE)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400364
Steven Rostedtd1b182a2009-04-15 16:53:47 -0400365int ring_buffer_print_page_header(struct trace_seq *s)
366{
367 struct buffer_data_page field;
368 int ret;
369
370 ret = trace_seq_printf(s, "\tfield: u64 timestamp;\t"
371 "offset:0;\tsize:%u;\n",
372 (unsigned int)sizeof(field.time_stamp));
373
374 ret = trace_seq_printf(s, "\tfield: local_t commit;\t"
375 "offset:%u;\tsize:%u;\n",
376 (unsigned int)offsetof(typeof(field), commit),
377 (unsigned int)sizeof(field.commit));
378
379 ret = trace_seq_printf(s, "\tfield: char data;\t"
380 "offset:%u;\tsize:%u;\n",
381 (unsigned int)offsetof(typeof(field), data),
382 (unsigned int)BUF_PAGE_SIZE);
383
384 return ret;
385}
386
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400387/*
388 * head_page == tail_page && head == tail then buffer is empty.
389 */
390struct ring_buffer_per_cpu {
391 int cpu;
392 struct ring_buffer *buffer;
Steven Rostedtf83c9d02008-11-11 18:47:44 +0100393 spinlock_t reader_lock; /* serialize readers */
Steven Rostedt3e03fb72008-11-06 00:09:43 -0500394 raw_spinlock_t lock;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400395 struct lock_class_key lock_key;
396 struct list_head pages;
Steven Rostedt6f807ac2008-10-04 02:00:58 -0400397 struct buffer_page *head_page; /* read from head */
398 struct buffer_page *tail_page; /* write to tail */
Wenji Huangc3706f02009-02-10 01:03:18 -0500399 struct buffer_page *commit_page; /* committed pages */
Steven Rostedtd7690412008-10-01 00:29:53 -0400400 struct buffer_page *reader_page;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400401 unsigned long overrun;
402 unsigned long entries;
403 u64 write_stamp;
404 u64 read_stamp;
405 atomic_t record_disabled;
406};
407
408struct ring_buffer {
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400409 unsigned pages;
410 unsigned flags;
411 int cpus;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400412 atomic_t record_disabled;
Arnaldo Carvalho de Melo00f62f62009-02-09 17:04:06 -0200413 cpumask_var_t cpumask;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400414
415 struct mutex mutex;
416
417 struct ring_buffer_per_cpu **buffers;
Steven Rostedt554f7862009-03-11 22:00:13 -0400418
Steven Rostedt59222ef2009-03-12 11:46:03 -0400419#ifdef CONFIG_HOTPLUG_CPU
Steven Rostedt554f7862009-03-11 22:00:13 -0400420 struct notifier_block cpu_notify;
421#endif
Steven Rostedt37886f62009-03-17 17:22:06 -0400422 u64 (*clock)(void);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400423};
424
425struct ring_buffer_iter {
426 struct ring_buffer_per_cpu *cpu_buffer;
427 unsigned long head;
428 struct buffer_page *head_page;
429 u64 read_stamp;
430};
431
Steven Rostedtf536aaf2008-11-10 23:07:30 -0500432/* buffer may be either ring_buffer or ring_buffer_per_cpu */
Steven Rostedtbf41a152008-10-04 02:00:59 -0400433#define RB_WARN_ON(buffer, cond) \
Steven Rostedt3e89c7b2008-11-11 15:28:41 -0500434 ({ \
435 int _____ret = unlikely(cond); \
436 if (_____ret) { \
Steven Rostedtbf41a152008-10-04 02:00:59 -0400437 atomic_inc(&buffer->record_disabled); \
438 WARN_ON(1); \
439 } \
Steven Rostedt3e89c7b2008-11-11 15:28:41 -0500440 _____ret; \
441 })
Steven Rostedtf536aaf2008-11-10 23:07:30 -0500442
Steven Rostedt37886f62009-03-17 17:22:06 -0400443/* Up this if you want to test the TIME_EXTENTS and normalization */
444#define DEBUG_SHIFT 0
445
446u64 ring_buffer_time_stamp(struct ring_buffer *buffer, int cpu)
447{
448 u64 time;
449
450 preempt_disable_notrace();
451 /* shift to debug/test normalization and TIME_EXTENTS */
452 time = buffer->clock() << DEBUG_SHIFT;
453 preempt_enable_no_resched_notrace();
454
455 return time;
456}
457EXPORT_SYMBOL_GPL(ring_buffer_time_stamp);
458
459void ring_buffer_normalize_time_stamp(struct ring_buffer *buffer,
460 int cpu, u64 *ts)
461{
462 /* Just stupid testing the normalize function and deltas */
463 *ts >>= DEBUG_SHIFT;
464}
465EXPORT_SYMBOL_GPL(ring_buffer_normalize_time_stamp);
466
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400467/**
468 * check_pages - integrity check of buffer pages
469 * @cpu_buffer: CPU buffer with pages to test
470 *
Wenji Huangc3706f02009-02-10 01:03:18 -0500471 * As a safety measure we check to make sure the data pages have not
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400472 * been corrupted.
473 */
474static int rb_check_pages(struct ring_buffer_per_cpu *cpu_buffer)
475{
476 struct list_head *head = &cpu_buffer->pages;
Steven Rostedt044fa782008-12-02 23:50:03 -0500477 struct buffer_page *bpage, *tmp;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400478
Steven Rostedt3e89c7b2008-11-11 15:28:41 -0500479 if (RB_WARN_ON(cpu_buffer, head->next->prev != head))
480 return -1;
481 if (RB_WARN_ON(cpu_buffer, head->prev->next != head))
482 return -1;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400483
Steven Rostedt044fa782008-12-02 23:50:03 -0500484 list_for_each_entry_safe(bpage, tmp, head, list) {
Steven Rostedt3e89c7b2008-11-11 15:28:41 -0500485 if (RB_WARN_ON(cpu_buffer,
Steven Rostedt044fa782008-12-02 23:50:03 -0500486 bpage->list.next->prev != &bpage->list))
Steven Rostedt3e89c7b2008-11-11 15:28:41 -0500487 return -1;
488 if (RB_WARN_ON(cpu_buffer,
Steven Rostedt044fa782008-12-02 23:50:03 -0500489 bpage->list.prev->next != &bpage->list))
Steven Rostedt3e89c7b2008-11-11 15:28:41 -0500490 return -1;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400491 }
492
493 return 0;
494}
495
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400496static int rb_allocate_pages(struct ring_buffer_per_cpu *cpu_buffer,
497 unsigned nr_pages)
498{
499 struct list_head *head = &cpu_buffer->pages;
Steven Rostedt044fa782008-12-02 23:50:03 -0500500 struct buffer_page *bpage, *tmp;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400501 unsigned long addr;
502 LIST_HEAD(pages);
503 unsigned i;
504
505 for (i = 0; i < nr_pages; i++) {
Steven Rostedt044fa782008-12-02 23:50:03 -0500506 bpage = kzalloc_node(ALIGN(sizeof(*bpage), cache_line_size()),
Steven Rostedtaa1e0e32008-10-02 19:18:09 -0400507 GFP_KERNEL, cpu_to_node(cpu_buffer->cpu));
Steven Rostedt044fa782008-12-02 23:50:03 -0500508 if (!bpage)
Steven Rostedte4c2ce82008-10-01 11:14:54 -0400509 goto free_pages;
Steven Rostedt044fa782008-12-02 23:50:03 -0500510 list_add(&bpage->list, &pages);
Steven Rostedte4c2ce82008-10-01 11:14:54 -0400511
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400512 addr = __get_free_page(GFP_KERNEL);
513 if (!addr)
514 goto free_pages;
Steven Rostedt044fa782008-12-02 23:50:03 -0500515 bpage->page = (void *)addr;
516 rb_init_page(bpage->page);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400517 }
518
519 list_splice(&pages, head);
520
521 rb_check_pages(cpu_buffer);
522
523 return 0;
524
525 free_pages:
Steven Rostedt044fa782008-12-02 23:50:03 -0500526 list_for_each_entry_safe(bpage, tmp, &pages, list) {
527 list_del_init(&bpage->list);
528 free_buffer_page(bpage);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400529 }
530 return -ENOMEM;
531}
532
533static struct ring_buffer_per_cpu *
534rb_allocate_cpu_buffer(struct ring_buffer *buffer, int cpu)
535{
536 struct ring_buffer_per_cpu *cpu_buffer;
Steven Rostedt044fa782008-12-02 23:50:03 -0500537 struct buffer_page *bpage;
Steven Rostedtd7690412008-10-01 00:29:53 -0400538 unsigned long addr;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400539 int ret;
540
541 cpu_buffer = kzalloc_node(ALIGN(sizeof(*cpu_buffer), cache_line_size()),
542 GFP_KERNEL, cpu_to_node(cpu));
543 if (!cpu_buffer)
544 return NULL;
545
546 cpu_buffer->cpu = cpu;
547 cpu_buffer->buffer = buffer;
Steven Rostedtf83c9d02008-11-11 18:47:44 +0100548 spin_lock_init(&cpu_buffer->reader_lock);
Steven Rostedt3e03fb72008-11-06 00:09:43 -0500549 cpu_buffer->lock = (raw_spinlock_t)__RAW_SPIN_LOCK_UNLOCKED;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400550 INIT_LIST_HEAD(&cpu_buffer->pages);
551
Steven Rostedt044fa782008-12-02 23:50:03 -0500552 bpage = kzalloc_node(ALIGN(sizeof(*bpage), cache_line_size()),
Steven Rostedte4c2ce82008-10-01 11:14:54 -0400553 GFP_KERNEL, cpu_to_node(cpu));
Steven Rostedt044fa782008-12-02 23:50:03 -0500554 if (!bpage)
Steven Rostedte4c2ce82008-10-01 11:14:54 -0400555 goto fail_free_buffer;
556
Steven Rostedt044fa782008-12-02 23:50:03 -0500557 cpu_buffer->reader_page = bpage;
Steven Rostedtd7690412008-10-01 00:29:53 -0400558 addr = __get_free_page(GFP_KERNEL);
559 if (!addr)
Steven Rostedte4c2ce82008-10-01 11:14:54 -0400560 goto fail_free_reader;
Steven Rostedt044fa782008-12-02 23:50:03 -0500561 bpage->page = (void *)addr;
562 rb_init_page(bpage->page);
Steven Rostedte4c2ce82008-10-01 11:14:54 -0400563
Steven Rostedtd7690412008-10-01 00:29:53 -0400564 INIT_LIST_HEAD(&cpu_buffer->reader_page->list);
Steven Rostedtd7690412008-10-01 00:29:53 -0400565
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400566 ret = rb_allocate_pages(cpu_buffer, buffer->pages);
567 if (ret < 0)
Steven Rostedtd7690412008-10-01 00:29:53 -0400568 goto fail_free_reader;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400569
570 cpu_buffer->head_page
571 = list_entry(cpu_buffer->pages.next, struct buffer_page, list);
Steven Rostedtbf41a152008-10-04 02:00:59 -0400572 cpu_buffer->tail_page = cpu_buffer->commit_page = cpu_buffer->head_page;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400573
574 return cpu_buffer;
575
Steven Rostedtd7690412008-10-01 00:29:53 -0400576 fail_free_reader:
577 free_buffer_page(cpu_buffer->reader_page);
578
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400579 fail_free_buffer:
580 kfree(cpu_buffer);
581 return NULL;
582}
583
584static void rb_free_cpu_buffer(struct ring_buffer_per_cpu *cpu_buffer)
585{
586 struct list_head *head = &cpu_buffer->pages;
Steven Rostedt044fa782008-12-02 23:50:03 -0500587 struct buffer_page *bpage, *tmp;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400588
Steven Rostedtd7690412008-10-01 00:29:53 -0400589 free_buffer_page(cpu_buffer->reader_page);
590
Steven Rostedt044fa782008-12-02 23:50:03 -0500591 list_for_each_entry_safe(bpage, tmp, head, list) {
592 list_del_init(&bpage->list);
593 free_buffer_page(bpage);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400594 }
595 kfree(cpu_buffer);
596}
597
Steven Rostedta7b13742008-09-29 23:02:39 -0400598/*
599 * Causes compile errors if the struct buffer_page gets bigger
600 * than the struct page.
601 */
602extern int ring_buffer_page_too_big(void);
603
Steven Rostedt59222ef2009-03-12 11:46:03 -0400604#ifdef CONFIG_HOTPLUG_CPU
Frederic Weisbecker09c9e842009-03-21 04:33:36 +0100605static int rb_cpu_notify(struct notifier_block *self,
606 unsigned long action, void *hcpu);
Steven Rostedt554f7862009-03-11 22:00:13 -0400607#endif
608
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400609/**
610 * ring_buffer_alloc - allocate a new ring_buffer
Robert Richter68814b52008-11-24 12:24:12 +0100611 * @size: the size in bytes per cpu that is needed.
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400612 * @flags: attributes to set for the ring buffer.
613 *
614 * Currently the only flag that is available is the RB_FL_OVERWRITE
615 * flag. This flag means that the buffer will overwrite old data
616 * when the buffer wraps. If this flag is not set, the buffer will
617 * drop data when the tail hits the head.
618 */
619struct ring_buffer *ring_buffer_alloc(unsigned long size, unsigned flags)
620{
621 struct ring_buffer *buffer;
622 int bsize;
623 int cpu;
624
Steven Rostedta7b13742008-09-29 23:02:39 -0400625 /* Paranoid! Optimizes out when all is well */
626 if (sizeof(struct buffer_page) > sizeof(struct page))
627 ring_buffer_page_too_big();
628
629
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400630 /* keep it in its own cache line */
631 buffer = kzalloc(ALIGN(sizeof(*buffer), cache_line_size()),
632 GFP_KERNEL);
633 if (!buffer)
634 return NULL;
635
Rusty Russell9e01c1b2009-01-01 10:12:22 +1030636 if (!alloc_cpumask_var(&buffer->cpumask, GFP_KERNEL))
637 goto fail_free_buffer;
638
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400639 buffer->pages = DIV_ROUND_UP(size, BUF_PAGE_SIZE);
640 buffer->flags = flags;
Steven Rostedt37886f62009-03-17 17:22:06 -0400641 buffer->clock = trace_clock_local;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400642
643 /* need at least two pages */
644 if (buffer->pages == 1)
645 buffer->pages++;
646
Frederic Weisbecker3bf832c2009-03-19 14:47:33 +0100647 /*
648 * In case of non-hotplug cpu, if the ring-buffer is allocated
649 * in early initcall, it will not be notified of secondary cpus.
650 * In that off case, we need to allocate for all possible cpus.
651 */
652#ifdef CONFIG_HOTPLUG_CPU
Steven Rostedt554f7862009-03-11 22:00:13 -0400653 get_online_cpus();
654 cpumask_copy(buffer->cpumask, cpu_online_mask);
Frederic Weisbecker3bf832c2009-03-19 14:47:33 +0100655#else
656 cpumask_copy(buffer->cpumask, cpu_possible_mask);
657#endif
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400658 buffer->cpus = nr_cpu_ids;
659
660 bsize = sizeof(void *) * nr_cpu_ids;
661 buffer->buffers = kzalloc(ALIGN(bsize, cache_line_size()),
662 GFP_KERNEL);
663 if (!buffer->buffers)
Rusty Russell9e01c1b2009-01-01 10:12:22 +1030664 goto fail_free_cpumask;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400665
666 for_each_buffer_cpu(buffer, cpu) {
667 buffer->buffers[cpu] =
668 rb_allocate_cpu_buffer(buffer, cpu);
669 if (!buffer->buffers[cpu])
670 goto fail_free_buffers;
671 }
672
Steven Rostedt59222ef2009-03-12 11:46:03 -0400673#ifdef CONFIG_HOTPLUG_CPU
Steven Rostedt554f7862009-03-11 22:00:13 -0400674 buffer->cpu_notify.notifier_call = rb_cpu_notify;
675 buffer->cpu_notify.priority = 0;
676 register_cpu_notifier(&buffer->cpu_notify);
677#endif
678
679 put_online_cpus();
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400680 mutex_init(&buffer->mutex);
681
682 return buffer;
683
684 fail_free_buffers:
685 for_each_buffer_cpu(buffer, cpu) {
686 if (buffer->buffers[cpu])
687 rb_free_cpu_buffer(buffer->buffers[cpu]);
688 }
689 kfree(buffer->buffers);
690
Rusty Russell9e01c1b2009-01-01 10:12:22 +1030691 fail_free_cpumask:
692 free_cpumask_var(buffer->cpumask);
Steven Rostedt554f7862009-03-11 22:00:13 -0400693 put_online_cpus();
Rusty Russell9e01c1b2009-01-01 10:12:22 +1030694
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400695 fail_free_buffer:
696 kfree(buffer);
697 return NULL;
698}
Robert Richterc4f50182008-12-11 16:49:22 +0100699EXPORT_SYMBOL_GPL(ring_buffer_alloc);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400700
701/**
702 * ring_buffer_free - free a ring buffer.
703 * @buffer: the buffer to free.
704 */
705void
706ring_buffer_free(struct ring_buffer *buffer)
707{
708 int cpu;
709
Steven Rostedt554f7862009-03-11 22:00:13 -0400710 get_online_cpus();
711
Steven Rostedt59222ef2009-03-12 11:46:03 -0400712#ifdef CONFIG_HOTPLUG_CPU
Steven Rostedt554f7862009-03-11 22:00:13 -0400713 unregister_cpu_notifier(&buffer->cpu_notify);
714#endif
715
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400716 for_each_buffer_cpu(buffer, cpu)
717 rb_free_cpu_buffer(buffer->buffers[cpu]);
718
Steven Rostedt554f7862009-03-11 22:00:13 -0400719 put_online_cpus();
720
Rusty Russell9e01c1b2009-01-01 10:12:22 +1030721 free_cpumask_var(buffer->cpumask);
722
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400723 kfree(buffer);
724}
Robert Richterc4f50182008-12-11 16:49:22 +0100725EXPORT_SYMBOL_GPL(ring_buffer_free);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400726
Steven Rostedt37886f62009-03-17 17:22:06 -0400727void ring_buffer_set_clock(struct ring_buffer *buffer,
728 u64 (*clock)(void))
729{
730 buffer->clock = clock;
731}
732
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400733static void rb_reset_cpu(struct ring_buffer_per_cpu *cpu_buffer);
734
735static void
736rb_remove_pages(struct ring_buffer_per_cpu *cpu_buffer, unsigned nr_pages)
737{
Steven Rostedt044fa782008-12-02 23:50:03 -0500738 struct buffer_page *bpage;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400739 struct list_head *p;
740 unsigned i;
741
742 atomic_inc(&cpu_buffer->record_disabled);
743 synchronize_sched();
744
745 for (i = 0; i < nr_pages; i++) {
Steven Rostedt3e89c7b2008-11-11 15:28:41 -0500746 if (RB_WARN_ON(cpu_buffer, list_empty(&cpu_buffer->pages)))
747 return;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400748 p = cpu_buffer->pages.next;
Steven Rostedt044fa782008-12-02 23:50:03 -0500749 bpage = list_entry(p, struct buffer_page, list);
750 list_del_init(&bpage->list);
751 free_buffer_page(bpage);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400752 }
Steven Rostedt3e89c7b2008-11-11 15:28:41 -0500753 if (RB_WARN_ON(cpu_buffer, list_empty(&cpu_buffer->pages)))
754 return;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400755
756 rb_reset_cpu(cpu_buffer);
757
758 rb_check_pages(cpu_buffer);
759
760 atomic_dec(&cpu_buffer->record_disabled);
761
762}
763
764static void
765rb_insert_pages(struct ring_buffer_per_cpu *cpu_buffer,
766 struct list_head *pages, unsigned nr_pages)
767{
Steven Rostedt044fa782008-12-02 23:50:03 -0500768 struct buffer_page *bpage;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400769 struct list_head *p;
770 unsigned i;
771
772 atomic_inc(&cpu_buffer->record_disabled);
773 synchronize_sched();
774
775 for (i = 0; i < nr_pages; i++) {
Steven Rostedt3e89c7b2008-11-11 15:28:41 -0500776 if (RB_WARN_ON(cpu_buffer, list_empty(pages)))
777 return;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400778 p = pages->next;
Steven Rostedt044fa782008-12-02 23:50:03 -0500779 bpage = list_entry(p, struct buffer_page, list);
780 list_del_init(&bpage->list);
781 list_add_tail(&bpage->list, &cpu_buffer->pages);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400782 }
783 rb_reset_cpu(cpu_buffer);
784
785 rb_check_pages(cpu_buffer);
786
787 atomic_dec(&cpu_buffer->record_disabled);
788}
789
790/**
791 * ring_buffer_resize - resize the ring buffer
792 * @buffer: the buffer to resize.
793 * @size: the new size.
794 *
795 * The tracer is responsible for making sure that the buffer is
796 * not being used while changing the size.
797 * Note: We may be able to change the above requirement by using
798 * RCU synchronizations.
799 *
800 * Minimum size is 2 * BUF_PAGE_SIZE.
801 *
802 * Returns -1 on failure.
803 */
804int ring_buffer_resize(struct ring_buffer *buffer, unsigned long size)
805{
806 struct ring_buffer_per_cpu *cpu_buffer;
807 unsigned nr_pages, rm_pages, new_pages;
Steven Rostedt044fa782008-12-02 23:50:03 -0500808 struct buffer_page *bpage, *tmp;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400809 unsigned long buffer_size;
810 unsigned long addr;
811 LIST_HEAD(pages);
812 int i, cpu;
813
Ingo Molnaree51a1d2008-11-13 14:58:31 +0100814 /*
815 * Always succeed at resizing a non-existent buffer:
816 */
817 if (!buffer)
818 return size;
819
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400820 size = DIV_ROUND_UP(size, BUF_PAGE_SIZE);
821 size *= BUF_PAGE_SIZE;
822 buffer_size = buffer->pages * BUF_PAGE_SIZE;
823
824 /* we need a minimum of two pages */
825 if (size < BUF_PAGE_SIZE * 2)
826 size = BUF_PAGE_SIZE * 2;
827
828 if (size == buffer_size)
829 return size;
830
831 mutex_lock(&buffer->mutex);
Steven Rostedt554f7862009-03-11 22:00:13 -0400832 get_online_cpus();
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400833
834 nr_pages = DIV_ROUND_UP(size, BUF_PAGE_SIZE);
835
836 if (size < buffer_size) {
837
838 /* easy case, just free pages */
Steven Rostedt554f7862009-03-11 22:00:13 -0400839 if (RB_WARN_ON(buffer, nr_pages >= buffer->pages))
840 goto out_fail;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400841
842 rm_pages = buffer->pages - nr_pages;
843
844 for_each_buffer_cpu(buffer, cpu) {
845 cpu_buffer = buffer->buffers[cpu];
846 rb_remove_pages(cpu_buffer, rm_pages);
847 }
848 goto out;
849 }
850
851 /*
852 * This is a bit more difficult. We only want to add pages
853 * when we can allocate enough for all CPUs. We do this
854 * by allocating all the pages and storing them on a local
855 * link list. If we succeed in our allocation, then we
856 * add these pages to the cpu_buffers. Otherwise we just free
857 * them all and return -ENOMEM;
858 */
Steven Rostedt554f7862009-03-11 22:00:13 -0400859 if (RB_WARN_ON(buffer, nr_pages <= buffer->pages))
860 goto out_fail;
Steven Rostedtf536aaf2008-11-10 23:07:30 -0500861
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400862 new_pages = nr_pages - buffer->pages;
863
864 for_each_buffer_cpu(buffer, cpu) {
865 for (i = 0; i < new_pages; i++) {
Steven Rostedt044fa782008-12-02 23:50:03 -0500866 bpage = kzalloc_node(ALIGN(sizeof(*bpage),
Steven Rostedte4c2ce82008-10-01 11:14:54 -0400867 cache_line_size()),
868 GFP_KERNEL, cpu_to_node(cpu));
Steven Rostedt044fa782008-12-02 23:50:03 -0500869 if (!bpage)
Steven Rostedte4c2ce82008-10-01 11:14:54 -0400870 goto free_pages;
Steven Rostedt044fa782008-12-02 23:50:03 -0500871 list_add(&bpage->list, &pages);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400872 addr = __get_free_page(GFP_KERNEL);
873 if (!addr)
874 goto free_pages;
Steven Rostedt044fa782008-12-02 23:50:03 -0500875 bpage->page = (void *)addr;
876 rb_init_page(bpage->page);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400877 }
878 }
879
880 for_each_buffer_cpu(buffer, cpu) {
881 cpu_buffer = buffer->buffers[cpu];
882 rb_insert_pages(cpu_buffer, &pages, new_pages);
883 }
884
Steven Rostedt554f7862009-03-11 22:00:13 -0400885 if (RB_WARN_ON(buffer, !list_empty(&pages)))
886 goto out_fail;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400887
888 out:
889 buffer->pages = nr_pages;
Steven Rostedt554f7862009-03-11 22:00:13 -0400890 put_online_cpus();
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400891 mutex_unlock(&buffer->mutex);
892
893 return size;
894
895 free_pages:
Steven Rostedt044fa782008-12-02 23:50:03 -0500896 list_for_each_entry_safe(bpage, tmp, &pages, list) {
897 list_del_init(&bpage->list);
898 free_buffer_page(bpage);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400899 }
Steven Rostedt554f7862009-03-11 22:00:13 -0400900 put_online_cpus();
Vegard Nossum641d2f62008-11-18 19:22:13 +0100901 mutex_unlock(&buffer->mutex);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400902 return -ENOMEM;
Steven Rostedt554f7862009-03-11 22:00:13 -0400903
904 /*
905 * Something went totally wrong, and we are too paranoid
906 * to even clean up the mess.
907 */
908 out_fail:
909 put_online_cpus();
910 mutex_unlock(&buffer->mutex);
911 return -1;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400912}
Robert Richterc4f50182008-12-11 16:49:22 +0100913EXPORT_SYMBOL_GPL(ring_buffer_resize);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400914
Steven Rostedt8789a9e2008-12-02 15:34:07 -0500915static inline void *
Steven Rostedt044fa782008-12-02 23:50:03 -0500916__rb_data_page_index(struct buffer_data_page *bpage, unsigned index)
Steven Rostedt8789a9e2008-12-02 15:34:07 -0500917{
Steven Rostedt044fa782008-12-02 23:50:03 -0500918 return bpage->data + index;
Steven Rostedt8789a9e2008-12-02 15:34:07 -0500919}
920
Steven Rostedt044fa782008-12-02 23:50:03 -0500921static inline void *__rb_page_index(struct buffer_page *bpage, unsigned index)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400922{
Steven Rostedt044fa782008-12-02 23:50:03 -0500923 return bpage->page->data + index;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400924}
925
926static inline struct ring_buffer_event *
Steven Rostedtd7690412008-10-01 00:29:53 -0400927rb_reader_event(struct ring_buffer_per_cpu *cpu_buffer)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400928{
Steven Rostedt6f807ac2008-10-04 02:00:58 -0400929 return __rb_page_index(cpu_buffer->reader_page,
930 cpu_buffer->reader_page->read);
931}
932
933static inline struct ring_buffer_event *
934rb_head_event(struct ring_buffer_per_cpu *cpu_buffer)
935{
936 return __rb_page_index(cpu_buffer->head_page,
937 cpu_buffer->head_page->read);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400938}
939
940static inline struct ring_buffer_event *
941rb_iter_head_event(struct ring_buffer_iter *iter)
942{
Steven Rostedt6f807ac2008-10-04 02:00:58 -0400943 return __rb_page_index(iter->head_page, iter->head);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400944}
945
Steven Rostedtbf41a152008-10-04 02:00:59 -0400946static inline unsigned rb_page_write(struct buffer_page *bpage)
947{
948 return local_read(&bpage->write);
949}
950
951static inline unsigned rb_page_commit(struct buffer_page *bpage)
952{
Steven Rostedtabc9b562008-12-02 15:34:06 -0500953 return local_read(&bpage->page->commit);
Steven Rostedtbf41a152008-10-04 02:00:59 -0400954}
955
956/* Size is determined by what has been commited */
957static inline unsigned rb_page_size(struct buffer_page *bpage)
958{
959 return rb_page_commit(bpage);
960}
961
962static inline unsigned
963rb_commit_index(struct ring_buffer_per_cpu *cpu_buffer)
964{
965 return rb_page_commit(cpu_buffer->commit_page);
966}
967
968static inline unsigned rb_head_size(struct ring_buffer_per_cpu *cpu_buffer)
969{
970 return rb_page_commit(cpu_buffer->head_page);
971}
972
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400973/*
974 * When the tail hits the head and the buffer is in overwrite mode,
975 * the head jumps to the next page and all content on the previous
976 * page is discarded. But before doing so, we update the overrun
977 * variable of the buffer.
978 */
979static void rb_update_overflow(struct ring_buffer_per_cpu *cpu_buffer)
980{
981 struct ring_buffer_event *event;
982 unsigned long head;
983
984 for (head = 0; head < rb_head_size(cpu_buffer);
985 head += rb_event_length(event)) {
986
Steven Rostedt6f807ac2008-10-04 02:00:58 -0400987 event = __rb_page_index(cpu_buffer->head_page, head);
Steven Rostedt3e89c7b2008-11-11 15:28:41 -0500988 if (RB_WARN_ON(cpu_buffer, rb_null_event(event)))
989 return;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400990 /* Only count data entries */
991 if (event->type != RINGBUF_TYPE_DATA)
992 continue;
993 cpu_buffer->overrun++;
994 cpu_buffer->entries--;
995 }
996}
997
998static inline void rb_inc_page(struct ring_buffer_per_cpu *cpu_buffer,
Steven Rostedt044fa782008-12-02 23:50:03 -0500999 struct buffer_page **bpage)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001000{
Steven Rostedt044fa782008-12-02 23:50:03 -05001001 struct list_head *p = (*bpage)->list.next;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001002
1003 if (p == &cpu_buffer->pages)
1004 p = p->next;
1005
Steven Rostedt044fa782008-12-02 23:50:03 -05001006 *bpage = list_entry(p, struct buffer_page, list);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001007}
1008
Steven Rostedtbf41a152008-10-04 02:00:59 -04001009static inline unsigned
1010rb_event_index(struct ring_buffer_event *event)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001011{
Steven Rostedtbf41a152008-10-04 02:00:59 -04001012 unsigned long addr = (unsigned long)event;
1013
1014 return (addr & ~PAGE_MASK) - (PAGE_SIZE - BUF_PAGE_SIZE);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001015}
1016
Andrew Morton34a148b2009-01-09 12:27:09 -08001017static int
Steven Rostedtbf41a152008-10-04 02:00:59 -04001018rb_is_commit(struct ring_buffer_per_cpu *cpu_buffer,
1019 struct ring_buffer_event *event)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001020{
Steven Rostedtbf41a152008-10-04 02:00:59 -04001021 unsigned long addr = (unsigned long)event;
1022 unsigned long index;
1023
1024 index = rb_event_index(event);
1025 addr &= PAGE_MASK;
1026
1027 return cpu_buffer->commit_page->page == (void *)addr &&
1028 rb_commit_index(cpu_buffer) == index;
1029}
1030
Andrew Morton34a148b2009-01-09 12:27:09 -08001031static void
Steven Rostedtbf41a152008-10-04 02:00:59 -04001032rb_set_commit_event(struct ring_buffer_per_cpu *cpu_buffer,
1033 struct ring_buffer_event *event)
1034{
1035 unsigned long addr = (unsigned long)event;
1036 unsigned long index;
1037
1038 index = rb_event_index(event);
1039 addr &= PAGE_MASK;
1040
1041 while (cpu_buffer->commit_page->page != (void *)addr) {
Steven Rostedt3e89c7b2008-11-11 15:28:41 -05001042 if (RB_WARN_ON(cpu_buffer,
1043 cpu_buffer->commit_page == cpu_buffer->tail_page))
1044 return;
Steven Rostedtabc9b562008-12-02 15:34:06 -05001045 cpu_buffer->commit_page->page->commit =
Steven Rostedtbf41a152008-10-04 02:00:59 -04001046 cpu_buffer->commit_page->write;
1047 rb_inc_page(cpu_buffer, &cpu_buffer->commit_page);
Steven Rostedtabc9b562008-12-02 15:34:06 -05001048 cpu_buffer->write_stamp =
1049 cpu_buffer->commit_page->page->time_stamp;
Steven Rostedtbf41a152008-10-04 02:00:59 -04001050 }
1051
1052 /* Now set the commit to the event's index */
Steven Rostedtabc9b562008-12-02 15:34:06 -05001053 local_set(&cpu_buffer->commit_page->page->commit, index);
Steven Rostedtbf41a152008-10-04 02:00:59 -04001054}
1055
Andrew Morton34a148b2009-01-09 12:27:09 -08001056static void
Steven Rostedtbf41a152008-10-04 02:00:59 -04001057rb_set_commit_to_write(struct ring_buffer_per_cpu *cpu_buffer)
1058{
1059 /*
1060 * We only race with interrupts and NMIs on this CPU.
1061 * If we own the commit event, then we can commit
1062 * all others that interrupted us, since the interruptions
1063 * are in stack format (they finish before they come
1064 * back to us). This allows us to do a simple loop to
1065 * assign the commit to the tail.
1066 */
Steven Rostedta8ccf1d2008-12-23 11:32:24 -05001067 again:
Steven Rostedtbf41a152008-10-04 02:00:59 -04001068 while (cpu_buffer->commit_page != cpu_buffer->tail_page) {
Steven Rostedtabc9b562008-12-02 15:34:06 -05001069 cpu_buffer->commit_page->page->commit =
Steven Rostedtbf41a152008-10-04 02:00:59 -04001070 cpu_buffer->commit_page->write;
1071 rb_inc_page(cpu_buffer, &cpu_buffer->commit_page);
Steven Rostedtabc9b562008-12-02 15:34:06 -05001072 cpu_buffer->write_stamp =
1073 cpu_buffer->commit_page->page->time_stamp;
Steven Rostedtbf41a152008-10-04 02:00:59 -04001074 /* add barrier to keep gcc from optimizing too much */
1075 barrier();
1076 }
1077 while (rb_commit_index(cpu_buffer) !=
1078 rb_page_write(cpu_buffer->commit_page)) {
Steven Rostedtabc9b562008-12-02 15:34:06 -05001079 cpu_buffer->commit_page->page->commit =
Steven Rostedtbf41a152008-10-04 02:00:59 -04001080 cpu_buffer->commit_page->write;
1081 barrier();
1082 }
Steven Rostedta8ccf1d2008-12-23 11:32:24 -05001083
1084 /* again, keep gcc from optimizing */
1085 barrier();
1086
1087 /*
1088 * If an interrupt came in just after the first while loop
1089 * and pushed the tail page forward, we will be left with
1090 * a dangling commit that will never go forward.
1091 */
1092 if (unlikely(cpu_buffer->commit_page != cpu_buffer->tail_page))
1093 goto again;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001094}
1095
Steven Rostedtd7690412008-10-01 00:29:53 -04001096static void rb_reset_reader_page(struct ring_buffer_per_cpu *cpu_buffer)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001097{
Steven Rostedtabc9b562008-12-02 15:34:06 -05001098 cpu_buffer->read_stamp = cpu_buffer->reader_page->page->time_stamp;
Steven Rostedt6f807ac2008-10-04 02:00:58 -04001099 cpu_buffer->reader_page->read = 0;
Steven Rostedtd7690412008-10-01 00:29:53 -04001100}
1101
Andrew Morton34a148b2009-01-09 12:27:09 -08001102static void rb_inc_iter(struct ring_buffer_iter *iter)
Steven Rostedtd7690412008-10-01 00:29:53 -04001103{
1104 struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
1105
1106 /*
1107 * The iterator could be on the reader page (it starts there).
1108 * But the head could have moved, since the reader was
1109 * found. Check for this case and assign the iterator
1110 * to the head page instead of next.
1111 */
1112 if (iter->head_page == cpu_buffer->reader_page)
1113 iter->head_page = cpu_buffer->head_page;
1114 else
1115 rb_inc_page(cpu_buffer, &iter->head_page);
1116
Steven Rostedtabc9b562008-12-02 15:34:06 -05001117 iter->read_stamp = iter->head_page->page->time_stamp;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001118 iter->head = 0;
1119}
1120
1121/**
1122 * ring_buffer_update_event - update event type and data
1123 * @event: the even to update
1124 * @type: the type of event
1125 * @length: the size of the event field in the ring buffer
1126 *
1127 * Update the type and data fields of the event. The length
1128 * is the actual size that is written to the ring buffer,
1129 * and with this, we can determine what to place into the
1130 * data field.
1131 */
Andrew Morton34a148b2009-01-09 12:27:09 -08001132static void
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001133rb_update_event(struct ring_buffer_event *event,
1134 unsigned type, unsigned length)
1135{
1136 event->type = type;
1137
1138 switch (type) {
1139
1140 case RINGBUF_TYPE_PADDING:
1141 break;
1142
1143 case RINGBUF_TYPE_TIME_EXTEND:
Andrew Morton67d34722009-01-09 12:27:09 -08001144 event->len = DIV_ROUND_UP(RB_LEN_TIME_EXTEND, RB_ALIGNMENT);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001145 break;
1146
1147 case RINGBUF_TYPE_TIME_STAMP:
Andrew Morton67d34722009-01-09 12:27:09 -08001148 event->len = DIV_ROUND_UP(RB_LEN_TIME_STAMP, RB_ALIGNMENT);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001149 break;
1150
1151 case RINGBUF_TYPE_DATA:
1152 length -= RB_EVNT_HDR_SIZE;
1153 if (length > RB_MAX_SMALL_DATA) {
1154 event->len = 0;
1155 event->array[0] = length;
1156 } else
Andrew Morton67d34722009-01-09 12:27:09 -08001157 event->len = DIV_ROUND_UP(length, RB_ALIGNMENT);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001158 break;
1159 default:
1160 BUG();
1161 }
1162}
1163
Andrew Morton34a148b2009-01-09 12:27:09 -08001164static unsigned rb_calculate_event_length(unsigned length)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001165{
1166 struct ring_buffer_event event; /* Used only for sizeof array */
1167
1168 /* zero length can cause confusions */
1169 if (!length)
1170 length = 1;
1171
1172 if (length > RB_MAX_SMALL_DATA)
1173 length += sizeof(event.array[0]);
1174
1175 length += RB_EVNT_HDR_SIZE;
1176 length = ALIGN(length, RB_ALIGNMENT);
1177
1178 return length;
1179}
1180
1181static struct ring_buffer_event *
1182__rb_reserve_next(struct ring_buffer_per_cpu *cpu_buffer,
1183 unsigned type, unsigned long length, u64 *ts)
1184{
Steven Rostedt98db8df2008-12-23 11:32:25 -05001185 struct buffer_page *tail_page, *head_page, *reader_page, *commit_page;
Steven Rostedtbf41a152008-10-04 02:00:59 -04001186 unsigned long tail, write;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001187 struct ring_buffer *buffer = cpu_buffer->buffer;
1188 struct ring_buffer_event *event;
Steven Rostedtbf41a152008-10-04 02:00:59 -04001189 unsigned long flags;
Steven Rostedt78d904b2009-02-05 18:43:07 -05001190 bool lock_taken = false;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001191
Steven Rostedt98db8df2008-12-23 11:32:25 -05001192 commit_page = cpu_buffer->commit_page;
1193 /* we just need to protect against interrupts */
1194 barrier();
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001195 tail_page = cpu_buffer->tail_page;
Steven Rostedtbf41a152008-10-04 02:00:59 -04001196 write = local_add_return(length, &tail_page->write);
1197 tail = write - length;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001198
Steven Rostedtbf41a152008-10-04 02:00:59 -04001199 /* See if we shot pass the end of this buffer page */
1200 if (write > BUF_PAGE_SIZE) {
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001201 struct buffer_page *next_page = tail_page;
1202
Steven Rostedt3e03fb72008-11-06 00:09:43 -05001203 local_irq_save(flags);
Steven Rostedt78d904b2009-02-05 18:43:07 -05001204 /*
Steven Rostedta81bd802009-02-06 01:45:16 -05001205 * Since the write to the buffer is still not
1206 * fully lockless, we must be careful with NMIs.
1207 * The locks in the writers are taken when a write
1208 * crosses to a new page. The locks protect against
1209 * races with the readers (this will soon be fixed
1210 * with a lockless solution).
1211 *
1212 * Because we can not protect against NMIs, and we
1213 * want to keep traces reentrant, we need to manage
1214 * what happens when we are in an NMI.
1215 *
Steven Rostedt78d904b2009-02-05 18:43:07 -05001216 * NMIs can happen after we take the lock.
1217 * If we are in an NMI, only take the lock
1218 * if it is not already taken. Otherwise
1219 * simply fail.
1220 */
Steven Rostedta81bd802009-02-06 01:45:16 -05001221 if (unlikely(in_nmi())) {
Steven Rostedt78d904b2009-02-05 18:43:07 -05001222 if (!__raw_spin_trylock(&cpu_buffer->lock))
Steven Rostedt45141d42009-02-12 13:19:48 -05001223 goto out_reset;
Steven Rostedt78d904b2009-02-05 18:43:07 -05001224 } else
1225 __raw_spin_lock(&cpu_buffer->lock);
1226
1227 lock_taken = true;
Steven Rostedtbf41a152008-10-04 02:00:59 -04001228
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001229 rb_inc_page(cpu_buffer, &next_page);
1230
Steven Rostedtd7690412008-10-01 00:29:53 -04001231 head_page = cpu_buffer->head_page;
1232 reader_page = cpu_buffer->reader_page;
1233
1234 /* we grabbed the lock before incrementing */
Steven Rostedt3e89c7b2008-11-11 15:28:41 -05001235 if (RB_WARN_ON(cpu_buffer, next_page == reader_page))
Steven Rostedt45141d42009-02-12 13:19:48 -05001236 goto out_reset;
Steven Rostedtbf41a152008-10-04 02:00:59 -04001237
1238 /*
1239 * If for some reason, we had an interrupt storm that made
1240 * it all the way around the buffer, bail, and warn
1241 * about it.
1242 */
Steven Rostedt98db8df2008-12-23 11:32:25 -05001243 if (unlikely(next_page == commit_page)) {
Steven Rostedtbf41a152008-10-04 02:00:59 -04001244 WARN_ON_ONCE(1);
Steven Rostedt45141d42009-02-12 13:19:48 -05001245 goto out_reset;
Steven Rostedtbf41a152008-10-04 02:00:59 -04001246 }
Steven Rostedtd7690412008-10-01 00:29:53 -04001247
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001248 if (next_page == head_page) {
Lai Jiangshan6f3b3442009-01-12 11:06:18 +08001249 if (!(buffer->flags & RB_FL_OVERWRITE))
Steven Rostedt45141d42009-02-12 13:19:48 -05001250 goto out_reset;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001251
Steven Rostedtbf41a152008-10-04 02:00:59 -04001252 /* tail_page has not moved yet? */
1253 if (tail_page == cpu_buffer->tail_page) {
1254 /* count overflows */
1255 rb_update_overflow(cpu_buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001256
Steven Rostedtbf41a152008-10-04 02:00:59 -04001257 rb_inc_page(cpu_buffer, &head_page);
1258 cpu_buffer->head_page = head_page;
1259 cpu_buffer->head_page->read = 0;
1260 }
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001261 }
1262
Steven Rostedtbf41a152008-10-04 02:00:59 -04001263 /*
1264 * If the tail page is still the same as what we think
1265 * it is, then it is up to us to update the tail
1266 * pointer.
1267 */
1268 if (tail_page == cpu_buffer->tail_page) {
1269 local_set(&next_page->write, 0);
Steven Rostedtabc9b562008-12-02 15:34:06 -05001270 local_set(&next_page->page->commit, 0);
Steven Rostedtbf41a152008-10-04 02:00:59 -04001271 cpu_buffer->tail_page = next_page;
1272
1273 /* reread the time stamp */
Steven Rostedt37886f62009-03-17 17:22:06 -04001274 *ts = ring_buffer_time_stamp(buffer, cpu_buffer->cpu);
Steven Rostedtabc9b562008-12-02 15:34:06 -05001275 cpu_buffer->tail_page->page->time_stamp = *ts;
Steven Rostedtbf41a152008-10-04 02:00:59 -04001276 }
1277
1278 /*
1279 * The actual tail page has moved forward.
1280 */
1281 if (tail < BUF_PAGE_SIZE) {
1282 /* Mark the rest of the page with padding */
Steven Rostedt6f807ac2008-10-04 02:00:58 -04001283 event = __rb_page_index(tail_page, tail);
Tom Zanussi2d622712009-03-22 03:30:49 -05001284 rb_event_set_padding(event);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001285 }
1286
Steven Rostedtbf41a152008-10-04 02:00:59 -04001287 if (tail <= BUF_PAGE_SIZE)
1288 /* Set the write back to the previous setting */
1289 local_set(&tail_page->write, tail);
1290
1291 /*
1292 * If this was a commit entry that failed,
1293 * increment that too
1294 */
1295 if (tail_page == cpu_buffer->commit_page &&
1296 tail == rb_commit_index(cpu_buffer)) {
1297 rb_set_commit_to_write(cpu_buffer);
1298 }
1299
Steven Rostedt3e03fb72008-11-06 00:09:43 -05001300 __raw_spin_unlock(&cpu_buffer->lock);
1301 local_irq_restore(flags);
Steven Rostedtbf41a152008-10-04 02:00:59 -04001302
1303 /* fail and let the caller try again */
1304 return ERR_PTR(-EAGAIN);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001305 }
1306
Steven Rostedtbf41a152008-10-04 02:00:59 -04001307 /* We reserved something on the buffer */
1308
Steven Rostedt3e89c7b2008-11-11 15:28:41 -05001309 if (RB_WARN_ON(cpu_buffer, write > BUF_PAGE_SIZE))
1310 return NULL;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001311
Steven Rostedt6f807ac2008-10-04 02:00:58 -04001312 event = __rb_page_index(tail_page, tail);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001313 rb_update_event(event, type, length);
1314
Steven Rostedtbf41a152008-10-04 02:00:59 -04001315 /*
1316 * If this is a commit and the tail is zero, then update
1317 * this page's time stamp.
1318 */
1319 if (!tail && rb_is_commit(cpu_buffer, event))
Steven Rostedtabc9b562008-12-02 15:34:06 -05001320 cpu_buffer->commit_page->page->time_stamp = *ts;
Steven Rostedtbf41a152008-10-04 02:00:59 -04001321
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001322 return event;
Steven Rostedtbf41a152008-10-04 02:00:59 -04001323
Steven Rostedt45141d42009-02-12 13:19:48 -05001324 out_reset:
Lai Jiangshan6f3b3442009-01-12 11:06:18 +08001325 /* reset write */
1326 if (tail <= BUF_PAGE_SIZE)
1327 local_set(&tail_page->write, tail);
1328
Steven Rostedt78d904b2009-02-05 18:43:07 -05001329 if (likely(lock_taken))
1330 __raw_spin_unlock(&cpu_buffer->lock);
Steven Rostedt3e03fb72008-11-06 00:09:43 -05001331 local_irq_restore(flags);
Steven Rostedtbf41a152008-10-04 02:00:59 -04001332 return NULL;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001333}
1334
1335static int
1336rb_add_time_stamp(struct ring_buffer_per_cpu *cpu_buffer,
1337 u64 *ts, u64 *delta)
1338{
1339 struct ring_buffer_event *event;
1340 static int once;
Steven Rostedtbf41a152008-10-04 02:00:59 -04001341 int ret;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001342
1343 if (unlikely(*delta > (1ULL << 59) && !once++)) {
1344 printk(KERN_WARNING "Delta way too big! %llu"
1345 " ts=%llu write stamp = %llu\n",
Stephen Rothwelle2862c92008-10-27 17:43:28 +11001346 (unsigned long long)*delta,
1347 (unsigned long long)*ts,
1348 (unsigned long long)cpu_buffer->write_stamp);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001349 WARN_ON(1);
1350 }
1351
1352 /*
1353 * The delta is too big, we to add a
1354 * new timestamp.
1355 */
1356 event = __rb_reserve_next(cpu_buffer,
1357 RINGBUF_TYPE_TIME_EXTEND,
1358 RB_LEN_TIME_EXTEND,
1359 ts);
1360 if (!event)
Steven Rostedtbf41a152008-10-04 02:00:59 -04001361 return -EBUSY;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001362
Steven Rostedtbf41a152008-10-04 02:00:59 -04001363 if (PTR_ERR(event) == -EAGAIN)
1364 return -EAGAIN;
1365
1366 /* Only a commited time event can update the write stamp */
1367 if (rb_is_commit(cpu_buffer, event)) {
1368 /*
1369 * If this is the first on the page, then we need to
1370 * update the page itself, and just put in a zero.
1371 */
1372 if (rb_event_index(event)) {
1373 event->time_delta = *delta & TS_MASK;
1374 event->array[0] = *delta >> TS_SHIFT;
1375 } else {
Steven Rostedtabc9b562008-12-02 15:34:06 -05001376 cpu_buffer->commit_page->page->time_stamp = *ts;
Steven Rostedtbf41a152008-10-04 02:00:59 -04001377 event->time_delta = 0;
1378 event->array[0] = 0;
1379 }
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001380 cpu_buffer->write_stamp = *ts;
Steven Rostedtbf41a152008-10-04 02:00:59 -04001381 /* let the caller know this was the commit */
1382 ret = 1;
1383 } else {
1384 /* Darn, this is just wasted space */
1385 event->time_delta = 0;
1386 event->array[0] = 0;
1387 ret = 0;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001388 }
1389
Steven Rostedtbf41a152008-10-04 02:00:59 -04001390 *delta = 0;
1391
1392 return ret;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001393}
1394
1395static struct ring_buffer_event *
1396rb_reserve_next_event(struct ring_buffer_per_cpu *cpu_buffer,
1397 unsigned type, unsigned long length)
1398{
1399 struct ring_buffer_event *event;
1400 u64 ts, delta;
Steven Rostedtbf41a152008-10-04 02:00:59 -04001401 int commit = 0;
Steven Rostedt818e3dd2008-10-31 09:58:35 -04001402 int nr_loops = 0;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001403
Steven Rostedtbf41a152008-10-04 02:00:59 -04001404 again:
Steven Rostedt818e3dd2008-10-31 09:58:35 -04001405 /*
1406 * We allow for interrupts to reenter here and do a trace.
1407 * If one does, it will cause this original code to loop
1408 * back here. Even with heavy interrupts happening, this
1409 * should only happen a few times in a row. If this happens
1410 * 1000 times in a row, there must be either an interrupt
1411 * storm or we have something buggy.
1412 * Bail!
1413 */
Steven Rostedt3e89c7b2008-11-11 15:28:41 -05001414 if (RB_WARN_ON(cpu_buffer, ++nr_loops > 1000))
Steven Rostedt818e3dd2008-10-31 09:58:35 -04001415 return NULL;
Steven Rostedt818e3dd2008-10-31 09:58:35 -04001416
Steven Rostedt37886f62009-03-17 17:22:06 -04001417 ts = ring_buffer_time_stamp(cpu_buffer->buffer, cpu_buffer->cpu);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001418
Steven Rostedtbf41a152008-10-04 02:00:59 -04001419 /*
1420 * Only the first commit can update the timestamp.
1421 * Yes there is a race here. If an interrupt comes in
1422 * just after the conditional and it traces too, then it
1423 * will also check the deltas. More than one timestamp may
1424 * also be made. But only the entry that did the actual
1425 * commit will be something other than zero.
1426 */
1427 if (cpu_buffer->tail_page == cpu_buffer->commit_page &&
1428 rb_page_write(cpu_buffer->tail_page) ==
1429 rb_commit_index(cpu_buffer)) {
1430
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001431 delta = ts - cpu_buffer->write_stamp;
1432
Steven Rostedtbf41a152008-10-04 02:00:59 -04001433 /* make sure this delta is calculated here */
1434 barrier();
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001435
Steven Rostedtbf41a152008-10-04 02:00:59 -04001436 /* Did the write stamp get updated already? */
1437 if (unlikely(ts < cpu_buffer->write_stamp))
Steven Rostedt4143c5c2008-11-10 21:46:01 -05001438 delta = 0;
Steven Rostedtbf41a152008-10-04 02:00:59 -04001439
1440 if (test_time_stamp(delta)) {
1441
1442 commit = rb_add_time_stamp(cpu_buffer, &ts, &delta);
1443
1444 if (commit == -EBUSY)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001445 return NULL;
Steven Rostedtbf41a152008-10-04 02:00:59 -04001446
1447 if (commit == -EAGAIN)
1448 goto again;
1449
1450 RB_WARN_ON(cpu_buffer, commit < 0);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001451 }
Steven Rostedtbf41a152008-10-04 02:00:59 -04001452 } else
1453 /* Non commits have zero deltas */
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001454 delta = 0;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001455
1456 event = __rb_reserve_next(cpu_buffer, type, length, &ts);
Steven Rostedtbf41a152008-10-04 02:00:59 -04001457 if (PTR_ERR(event) == -EAGAIN)
1458 goto again;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001459
Steven Rostedtbf41a152008-10-04 02:00:59 -04001460 if (!event) {
1461 if (unlikely(commit))
1462 /*
1463 * Ouch! We needed a timestamp and it was commited. But
1464 * we didn't get our event reserved.
1465 */
1466 rb_set_commit_to_write(cpu_buffer);
1467 return NULL;
1468 }
1469
1470 /*
1471 * If the timestamp was commited, make the commit our entry
1472 * now so that we will update it when needed.
1473 */
1474 if (commit)
1475 rb_set_commit_event(cpu_buffer, event);
1476 else if (!rb_is_commit(cpu_buffer, event))
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001477 delta = 0;
1478
1479 event->time_delta = delta;
1480
1481 return event;
1482}
1483
Steven Rostedtaa18efb2009-04-20 16:16:11 -04001484#define TRACE_RECURSIVE_DEPTH 16
Steven Rostedt261842b2009-04-16 21:41:52 -04001485
1486static int trace_recursive_lock(void)
1487{
Steven Rostedtaa18efb2009-04-20 16:16:11 -04001488 current->trace_recursion++;
Steven Rostedt261842b2009-04-16 21:41:52 -04001489
Steven Rostedtaa18efb2009-04-20 16:16:11 -04001490 if (likely(current->trace_recursion < TRACE_RECURSIVE_DEPTH))
1491 return 0;
Steven Rostedt261842b2009-04-16 21:41:52 -04001492
Steven Rostedtaa18efb2009-04-20 16:16:11 -04001493 /* Disable all tracing before we do anything else */
1494 tracing_off_permanent();
Frederic Weisbeckere057a5e2009-04-19 23:38:12 +02001495
Steven Rostedtaa18efb2009-04-20 16:16:11 -04001496 printk_once(KERN_WARNING "Tracing recursion: depth[%d]:"
1497 "HC[%lu]:SC[%lu]:NMI[%lu]\n",
1498 current->trace_recursion,
1499 hardirq_count() >> HARDIRQ_SHIFT,
1500 softirq_count() >> SOFTIRQ_SHIFT,
1501 in_nmi());
Frederic Weisbeckere057a5e2009-04-19 23:38:12 +02001502
Steven Rostedtaa18efb2009-04-20 16:16:11 -04001503 WARN_ON_ONCE(1);
1504 return -1;
Steven Rostedt261842b2009-04-16 21:41:52 -04001505}
1506
1507static void trace_recursive_unlock(void)
1508{
Steven Rostedtaa18efb2009-04-20 16:16:11 -04001509 WARN_ON_ONCE(!current->trace_recursion);
Steven Rostedt261842b2009-04-16 21:41:52 -04001510
Steven Rostedtaa18efb2009-04-20 16:16:11 -04001511 current->trace_recursion--;
Steven Rostedt261842b2009-04-16 21:41:52 -04001512}
1513
Steven Rostedtbf41a152008-10-04 02:00:59 -04001514static DEFINE_PER_CPU(int, rb_need_resched);
1515
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001516/**
1517 * ring_buffer_lock_reserve - reserve a part of the buffer
1518 * @buffer: the ring buffer to reserve from
1519 * @length: the length of the data to reserve (excluding event header)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001520 *
1521 * Returns a reseverd event on the ring buffer to copy directly to.
1522 * The user of this interface will need to get the body to write into
1523 * and can use the ring_buffer_event_data() interface.
1524 *
1525 * The length is the length of the data needed, not the event length
1526 * which also includes the event header.
1527 *
1528 * Must be paired with ring_buffer_unlock_commit, unless NULL is returned.
1529 * If NULL is returned, then nothing has been allocated or locked.
1530 */
1531struct ring_buffer_event *
Arnaldo Carvalho de Melo0a987752009-02-05 16:12:56 -02001532ring_buffer_lock_reserve(struct ring_buffer *buffer, unsigned long length)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001533{
1534 struct ring_buffer_per_cpu *cpu_buffer;
1535 struct ring_buffer_event *event;
Steven Rostedtbf41a152008-10-04 02:00:59 -04001536 int cpu, resched;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001537
Steven Rostedt033601a2008-11-21 12:41:55 -05001538 if (ring_buffer_flags != RB_BUFFERS_ON)
Steven Rostedta3583242008-11-11 15:01:42 -05001539 return NULL;
1540
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001541 if (atomic_read(&buffer->record_disabled))
1542 return NULL;
1543
Steven Rostedtbf41a152008-10-04 02:00:59 -04001544 /* If we are tracing schedule, we don't want to recurse */
Steven Rostedt182e9f52008-11-03 23:15:56 -05001545 resched = ftrace_preempt_disable();
Steven Rostedtbf41a152008-10-04 02:00:59 -04001546
Steven Rostedt261842b2009-04-16 21:41:52 -04001547 if (trace_recursive_lock())
1548 goto out_nocheck;
1549
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001550 cpu = raw_smp_processor_id();
1551
Rusty Russell9e01c1b2009-01-01 10:12:22 +10301552 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedtd7690412008-10-01 00:29:53 -04001553 goto out;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001554
1555 cpu_buffer = buffer->buffers[cpu];
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001556
1557 if (atomic_read(&cpu_buffer->record_disabled))
Steven Rostedtd7690412008-10-01 00:29:53 -04001558 goto out;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001559
1560 length = rb_calculate_event_length(length);
1561 if (length > BUF_PAGE_SIZE)
Steven Rostedtbf41a152008-10-04 02:00:59 -04001562 goto out;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001563
1564 event = rb_reserve_next_event(cpu_buffer, RINGBUF_TYPE_DATA, length);
1565 if (!event)
Steven Rostedtd7690412008-10-01 00:29:53 -04001566 goto out;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001567
Steven Rostedtbf41a152008-10-04 02:00:59 -04001568 /*
1569 * Need to store resched state on this cpu.
1570 * Only the first needs to.
1571 */
1572
1573 if (preempt_count() == 1)
1574 per_cpu(rb_need_resched, cpu) = resched;
1575
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001576 return event;
1577
Steven Rostedtd7690412008-10-01 00:29:53 -04001578 out:
Steven Rostedt261842b2009-04-16 21:41:52 -04001579 trace_recursive_unlock();
1580
1581 out_nocheck:
Steven Rostedt182e9f52008-11-03 23:15:56 -05001582 ftrace_preempt_enable(resched);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001583 return NULL;
1584}
Robert Richterc4f50182008-12-11 16:49:22 +01001585EXPORT_SYMBOL_GPL(ring_buffer_lock_reserve);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001586
1587static void rb_commit(struct ring_buffer_per_cpu *cpu_buffer,
1588 struct ring_buffer_event *event)
1589{
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001590 cpu_buffer->entries++;
Steven Rostedtbf41a152008-10-04 02:00:59 -04001591
1592 /* Only process further if we own the commit */
1593 if (!rb_is_commit(cpu_buffer, event))
1594 return;
1595
1596 cpu_buffer->write_stamp += event->time_delta;
1597
1598 rb_set_commit_to_write(cpu_buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001599}
1600
1601/**
1602 * ring_buffer_unlock_commit - commit a reserved
1603 * @buffer: The buffer to commit to
1604 * @event: The event pointer to commit.
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001605 *
1606 * This commits the data to the ring buffer, and releases any locks held.
1607 *
1608 * Must be paired with ring_buffer_lock_reserve.
1609 */
1610int ring_buffer_unlock_commit(struct ring_buffer *buffer,
Arnaldo Carvalho de Melo0a987752009-02-05 16:12:56 -02001611 struct ring_buffer_event *event)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001612{
1613 struct ring_buffer_per_cpu *cpu_buffer;
1614 int cpu = raw_smp_processor_id();
1615
1616 cpu_buffer = buffer->buffers[cpu];
1617
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001618 rb_commit(cpu_buffer, event);
1619
Steven Rostedt261842b2009-04-16 21:41:52 -04001620 trace_recursive_unlock();
1621
Steven Rostedtbf41a152008-10-04 02:00:59 -04001622 /*
1623 * Only the last preempt count needs to restore preemption.
1624 */
Steven Rostedt182e9f52008-11-03 23:15:56 -05001625 if (preempt_count() == 1)
1626 ftrace_preempt_enable(per_cpu(rb_need_resched, cpu));
1627 else
Steven Rostedtbf41a152008-10-04 02:00:59 -04001628 preempt_enable_no_resched_notrace();
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001629
1630 return 0;
1631}
Robert Richterc4f50182008-12-11 16:49:22 +01001632EXPORT_SYMBOL_GPL(ring_buffer_unlock_commit);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001633
Frederic Weisbeckerf3b9aae2009-04-19 23:39:33 +02001634static inline void rb_event_discard(struct ring_buffer_event *event)
1635{
1636 event->type = RINGBUF_TYPE_PADDING;
1637 /* time delta must be non zero */
1638 if (!event->time_delta)
1639 event->time_delta = 1;
1640}
1641
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001642/**
Steven Rostedtfa1b47d2009-04-02 00:09:41 -04001643 * ring_buffer_event_discard - discard any event in the ring buffer
1644 * @event: the event to discard
1645 *
1646 * Sometimes a event that is in the ring buffer needs to be ignored.
1647 * This function lets the user discard an event in the ring buffer
1648 * and then that event will not be read later.
1649 *
1650 * Note, it is up to the user to be careful with this, and protect
1651 * against races. If the user discards an event that has been consumed
1652 * it is possible that it could corrupt the ring buffer.
1653 */
1654void ring_buffer_event_discard(struct ring_buffer_event *event)
1655{
Frederic Weisbeckerf3b9aae2009-04-19 23:39:33 +02001656 rb_event_discard(event);
Steven Rostedtfa1b47d2009-04-02 00:09:41 -04001657}
1658EXPORT_SYMBOL_GPL(ring_buffer_event_discard);
1659
1660/**
1661 * ring_buffer_commit_discard - discard an event that has not been committed
1662 * @buffer: the ring buffer
1663 * @event: non committed event to discard
1664 *
1665 * This is similar to ring_buffer_event_discard but must only be
1666 * performed on an event that has not been committed yet. The difference
1667 * is that this will also try to free the event from the ring buffer
1668 * if another event has not been added behind it.
1669 *
1670 * If another event has been added behind it, it will set the event
1671 * up as discarded, and perform the commit.
1672 *
1673 * If this function is called, do not call ring_buffer_unlock_commit on
1674 * the event.
1675 */
1676void ring_buffer_discard_commit(struct ring_buffer *buffer,
1677 struct ring_buffer_event *event)
1678{
1679 struct ring_buffer_per_cpu *cpu_buffer;
1680 unsigned long new_index, old_index;
1681 struct buffer_page *bpage;
1682 unsigned long index;
1683 unsigned long addr;
1684 int cpu;
1685
1686 /* The event is discarded regardless */
Frederic Weisbeckerf3b9aae2009-04-19 23:39:33 +02001687 rb_event_discard(event);
Steven Rostedtfa1b47d2009-04-02 00:09:41 -04001688
1689 /*
1690 * This must only be called if the event has not been
1691 * committed yet. Thus we can assume that preemption
1692 * is still disabled.
1693 */
1694 RB_WARN_ON(buffer, !preempt_count());
1695
1696 cpu = smp_processor_id();
1697 cpu_buffer = buffer->buffers[cpu];
1698
1699 new_index = rb_event_index(event);
1700 old_index = new_index + rb_event_length(event);
1701 addr = (unsigned long)event;
1702 addr &= PAGE_MASK;
1703
1704 bpage = cpu_buffer->tail_page;
1705
1706 if (bpage == (void *)addr && rb_page_write(bpage) == old_index) {
1707 /*
1708 * This is on the tail page. It is possible that
1709 * a write could come in and move the tail page
1710 * and write to the next page. That is fine
1711 * because we just shorten what is on this page.
1712 */
1713 index = local_cmpxchg(&bpage->write, old_index, new_index);
1714 if (index == old_index)
1715 goto out;
1716 }
1717
1718 /*
1719 * The commit is still visible by the reader, so we
1720 * must increment entries.
1721 */
1722 cpu_buffer->entries++;
1723 out:
1724 /*
1725 * If a write came in and pushed the tail page
1726 * we still need to update the commit pointer
1727 * if we were the commit.
1728 */
1729 if (rb_is_commit(cpu_buffer, event))
1730 rb_set_commit_to_write(cpu_buffer);
1731
Frederic Weisbeckerf3b9aae2009-04-19 23:39:33 +02001732 trace_recursive_unlock();
1733
Steven Rostedtfa1b47d2009-04-02 00:09:41 -04001734 /*
1735 * Only the last preempt count needs to restore preemption.
1736 */
1737 if (preempt_count() == 1)
1738 ftrace_preempt_enable(per_cpu(rb_need_resched, cpu));
1739 else
1740 preempt_enable_no_resched_notrace();
1741
1742}
1743EXPORT_SYMBOL_GPL(ring_buffer_discard_commit);
1744
1745/**
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001746 * ring_buffer_write - write data to the buffer without reserving
1747 * @buffer: The ring buffer to write to.
1748 * @length: The length of the data being written (excluding the event header)
1749 * @data: The data to write to the buffer.
1750 *
1751 * This is like ring_buffer_lock_reserve and ring_buffer_unlock_commit as
1752 * one function. If you already have the data to write to the buffer, it
1753 * may be easier to simply call this function.
1754 *
1755 * Note, like ring_buffer_lock_reserve, the length is the length of the data
1756 * and not the length of the event which would hold the header.
1757 */
1758int ring_buffer_write(struct ring_buffer *buffer,
1759 unsigned long length,
1760 void *data)
1761{
1762 struct ring_buffer_per_cpu *cpu_buffer;
1763 struct ring_buffer_event *event;
Steven Rostedtbf41a152008-10-04 02:00:59 -04001764 unsigned long event_length;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001765 void *body;
1766 int ret = -EBUSY;
Steven Rostedtbf41a152008-10-04 02:00:59 -04001767 int cpu, resched;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001768
Steven Rostedt033601a2008-11-21 12:41:55 -05001769 if (ring_buffer_flags != RB_BUFFERS_ON)
Steven Rostedta3583242008-11-11 15:01:42 -05001770 return -EBUSY;
1771
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001772 if (atomic_read(&buffer->record_disabled))
1773 return -EBUSY;
1774
Steven Rostedt182e9f52008-11-03 23:15:56 -05001775 resched = ftrace_preempt_disable();
Steven Rostedtbf41a152008-10-04 02:00:59 -04001776
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001777 cpu = raw_smp_processor_id();
1778
Rusty Russell9e01c1b2009-01-01 10:12:22 +10301779 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedtd7690412008-10-01 00:29:53 -04001780 goto out;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001781
1782 cpu_buffer = buffer->buffers[cpu];
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001783
1784 if (atomic_read(&cpu_buffer->record_disabled))
1785 goto out;
1786
1787 event_length = rb_calculate_event_length(length);
1788 event = rb_reserve_next_event(cpu_buffer,
1789 RINGBUF_TYPE_DATA, event_length);
1790 if (!event)
1791 goto out;
1792
1793 body = rb_event_data(event);
1794
1795 memcpy(body, data, length);
1796
1797 rb_commit(cpu_buffer, event);
1798
1799 ret = 0;
1800 out:
Steven Rostedt182e9f52008-11-03 23:15:56 -05001801 ftrace_preempt_enable(resched);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001802
1803 return ret;
1804}
Robert Richterc4f50182008-12-11 16:49:22 +01001805EXPORT_SYMBOL_GPL(ring_buffer_write);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001806
Andrew Morton34a148b2009-01-09 12:27:09 -08001807static int rb_per_cpu_empty(struct ring_buffer_per_cpu *cpu_buffer)
Steven Rostedtbf41a152008-10-04 02:00:59 -04001808{
1809 struct buffer_page *reader = cpu_buffer->reader_page;
1810 struct buffer_page *head = cpu_buffer->head_page;
1811 struct buffer_page *commit = cpu_buffer->commit_page;
1812
1813 return reader->read == rb_page_commit(reader) &&
1814 (commit == reader ||
1815 (commit == head &&
1816 head->read == rb_page_commit(commit)));
1817}
1818
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001819/**
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001820 * ring_buffer_record_disable - stop all writes into the buffer
1821 * @buffer: The ring buffer to stop writes to.
1822 *
1823 * This prevents all writes to the buffer. Any attempt to write
1824 * to the buffer after this will fail and return NULL.
1825 *
1826 * The caller should call synchronize_sched() after this.
1827 */
1828void ring_buffer_record_disable(struct ring_buffer *buffer)
1829{
1830 atomic_inc(&buffer->record_disabled);
1831}
Robert Richterc4f50182008-12-11 16:49:22 +01001832EXPORT_SYMBOL_GPL(ring_buffer_record_disable);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001833
1834/**
1835 * ring_buffer_record_enable - enable writes to the buffer
1836 * @buffer: The ring buffer to enable writes
1837 *
1838 * Note, multiple disables will need the same number of enables
1839 * to truely enable the writing (much like preempt_disable).
1840 */
1841void ring_buffer_record_enable(struct ring_buffer *buffer)
1842{
1843 atomic_dec(&buffer->record_disabled);
1844}
Robert Richterc4f50182008-12-11 16:49:22 +01001845EXPORT_SYMBOL_GPL(ring_buffer_record_enable);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001846
1847/**
1848 * ring_buffer_record_disable_cpu - stop all writes into the cpu_buffer
1849 * @buffer: The ring buffer to stop writes to.
1850 * @cpu: The CPU buffer to stop
1851 *
1852 * This prevents all writes to the buffer. Any attempt to write
1853 * to the buffer after this will fail and return NULL.
1854 *
1855 * The caller should call synchronize_sched() after this.
1856 */
1857void ring_buffer_record_disable_cpu(struct ring_buffer *buffer, int cpu)
1858{
1859 struct ring_buffer_per_cpu *cpu_buffer;
1860
Rusty Russell9e01c1b2009-01-01 10:12:22 +10301861 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedt8aabee52009-03-12 13:13:49 -04001862 return;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001863
1864 cpu_buffer = buffer->buffers[cpu];
1865 atomic_inc(&cpu_buffer->record_disabled);
1866}
Robert Richterc4f50182008-12-11 16:49:22 +01001867EXPORT_SYMBOL_GPL(ring_buffer_record_disable_cpu);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001868
1869/**
1870 * ring_buffer_record_enable_cpu - enable writes to the buffer
1871 * @buffer: The ring buffer to enable writes
1872 * @cpu: The CPU to enable.
1873 *
1874 * Note, multiple disables will need the same number of enables
1875 * to truely enable the writing (much like preempt_disable).
1876 */
1877void ring_buffer_record_enable_cpu(struct ring_buffer *buffer, int cpu)
1878{
1879 struct ring_buffer_per_cpu *cpu_buffer;
1880
Rusty Russell9e01c1b2009-01-01 10:12:22 +10301881 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedt8aabee52009-03-12 13:13:49 -04001882 return;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001883
1884 cpu_buffer = buffer->buffers[cpu];
1885 atomic_dec(&cpu_buffer->record_disabled);
1886}
Robert Richterc4f50182008-12-11 16:49:22 +01001887EXPORT_SYMBOL_GPL(ring_buffer_record_enable_cpu);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001888
1889/**
1890 * ring_buffer_entries_cpu - get the number of entries in a cpu buffer
1891 * @buffer: The ring buffer
1892 * @cpu: The per CPU buffer to get the entries from.
1893 */
1894unsigned long ring_buffer_entries_cpu(struct ring_buffer *buffer, int cpu)
1895{
1896 struct ring_buffer_per_cpu *cpu_buffer;
Steven Rostedt8aabee52009-03-12 13:13:49 -04001897 unsigned long ret;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001898
Rusty Russell9e01c1b2009-01-01 10:12:22 +10301899 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedt8aabee52009-03-12 13:13:49 -04001900 return 0;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001901
1902 cpu_buffer = buffer->buffers[cpu];
Steven Rostedt554f7862009-03-11 22:00:13 -04001903 ret = cpu_buffer->entries;
Steven Rostedt554f7862009-03-11 22:00:13 -04001904
1905 return ret;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001906}
Robert Richterc4f50182008-12-11 16:49:22 +01001907EXPORT_SYMBOL_GPL(ring_buffer_entries_cpu);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001908
1909/**
1910 * ring_buffer_overrun_cpu - get the number of overruns in a cpu_buffer
1911 * @buffer: The ring buffer
1912 * @cpu: The per CPU buffer to get the number of overruns from
1913 */
1914unsigned long ring_buffer_overrun_cpu(struct ring_buffer *buffer, int cpu)
1915{
1916 struct ring_buffer_per_cpu *cpu_buffer;
Steven Rostedt8aabee52009-03-12 13:13:49 -04001917 unsigned long ret;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001918
Rusty Russell9e01c1b2009-01-01 10:12:22 +10301919 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedt8aabee52009-03-12 13:13:49 -04001920 return 0;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001921
1922 cpu_buffer = buffer->buffers[cpu];
Steven Rostedt554f7862009-03-11 22:00:13 -04001923 ret = cpu_buffer->overrun;
Steven Rostedt554f7862009-03-11 22:00:13 -04001924
1925 return ret;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001926}
Robert Richterc4f50182008-12-11 16:49:22 +01001927EXPORT_SYMBOL_GPL(ring_buffer_overrun_cpu);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001928
1929/**
1930 * ring_buffer_entries - get the number of entries in a buffer
1931 * @buffer: The ring buffer
1932 *
1933 * Returns the total number of entries in the ring buffer
1934 * (all CPU entries)
1935 */
1936unsigned long ring_buffer_entries(struct ring_buffer *buffer)
1937{
1938 struct ring_buffer_per_cpu *cpu_buffer;
1939 unsigned long entries = 0;
1940 int cpu;
1941
1942 /* if you care about this being correct, lock the buffer */
1943 for_each_buffer_cpu(buffer, cpu) {
1944 cpu_buffer = buffer->buffers[cpu];
1945 entries += cpu_buffer->entries;
1946 }
1947
1948 return entries;
1949}
Robert Richterc4f50182008-12-11 16:49:22 +01001950EXPORT_SYMBOL_GPL(ring_buffer_entries);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001951
1952/**
1953 * ring_buffer_overrun_cpu - get the number of overruns in buffer
1954 * @buffer: The ring buffer
1955 *
1956 * Returns the total number of overruns in the ring buffer
1957 * (all CPU entries)
1958 */
1959unsigned long ring_buffer_overruns(struct ring_buffer *buffer)
1960{
1961 struct ring_buffer_per_cpu *cpu_buffer;
1962 unsigned long overruns = 0;
1963 int cpu;
1964
1965 /* if you care about this being correct, lock the buffer */
1966 for_each_buffer_cpu(buffer, cpu) {
1967 cpu_buffer = buffer->buffers[cpu];
1968 overruns += cpu_buffer->overrun;
1969 }
1970
1971 return overruns;
1972}
Robert Richterc4f50182008-12-11 16:49:22 +01001973EXPORT_SYMBOL_GPL(ring_buffer_overruns);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001974
Steven Rostedt642edba2008-11-12 00:01:26 -05001975static void rb_iter_reset(struct ring_buffer_iter *iter)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001976{
1977 struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
1978
Steven Rostedtd7690412008-10-01 00:29:53 -04001979 /* Iterator usage is expected to have record disabled */
1980 if (list_empty(&cpu_buffer->reader_page->list)) {
1981 iter->head_page = cpu_buffer->head_page;
Steven Rostedt6f807ac2008-10-04 02:00:58 -04001982 iter->head = cpu_buffer->head_page->read;
Steven Rostedtd7690412008-10-01 00:29:53 -04001983 } else {
1984 iter->head_page = cpu_buffer->reader_page;
Steven Rostedt6f807ac2008-10-04 02:00:58 -04001985 iter->head = cpu_buffer->reader_page->read;
Steven Rostedtd7690412008-10-01 00:29:53 -04001986 }
1987 if (iter->head)
1988 iter->read_stamp = cpu_buffer->read_stamp;
1989 else
Steven Rostedtabc9b562008-12-02 15:34:06 -05001990 iter->read_stamp = iter->head_page->page->time_stamp;
Steven Rostedt642edba2008-11-12 00:01:26 -05001991}
Steven Rostedtf83c9d02008-11-11 18:47:44 +01001992
Steven Rostedt642edba2008-11-12 00:01:26 -05001993/**
1994 * ring_buffer_iter_reset - reset an iterator
1995 * @iter: The iterator to reset
1996 *
1997 * Resets the iterator, so that it will start from the beginning
1998 * again.
1999 */
2000void ring_buffer_iter_reset(struct ring_buffer_iter *iter)
2001{
Steven Rostedt554f7862009-03-11 22:00:13 -04002002 struct ring_buffer_per_cpu *cpu_buffer;
Steven Rostedt642edba2008-11-12 00:01:26 -05002003 unsigned long flags;
2004
Steven Rostedt554f7862009-03-11 22:00:13 -04002005 if (!iter)
2006 return;
2007
2008 cpu_buffer = iter->cpu_buffer;
2009
Steven Rostedt642edba2008-11-12 00:01:26 -05002010 spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
2011 rb_iter_reset(iter);
Steven Rostedtf83c9d02008-11-11 18:47:44 +01002012 spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002013}
Robert Richterc4f50182008-12-11 16:49:22 +01002014EXPORT_SYMBOL_GPL(ring_buffer_iter_reset);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002015
2016/**
2017 * ring_buffer_iter_empty - check if an iterator has no more to read
2018 * @iter: The iterator to check
2019 */
2020int ring_buffer_iter_empty(struct ring_buffer_iter *iter)
2021{
2022 struct ring_buffer_per_cpu *cpu_buffer;
2023
2024 cpu_buffer = iter->cpu_buffer;
2025
Steven Rostedtbf41a152008-10-04 02:00:59 -04002026 return iter->head_page == cpu_buffer->commit_page &&
2027 iter->head == rb_commit_index(cpu_buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002028}
Robert Richterc4f50182008-12-11 16:49:22 +01002029EXPORT_SYMBOL_GPL(ring_buffer_iter_empty);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002030
2031static void
2032rb_update_read_stamp(struct ring_buffer_per_cpu *cpu_buffer,
2033 struct ring_buffer_event *event)
2034{
2035 u64 delta;
2036
2037 switch (event->type) {
2038 case RINGBUF_TYPE_PADDING:
2039 return;
2040
2041 case RINGBUF_TYPE_TIME_EXTEND:
2042 delta = event->array[0];
2043 delta <<= TS_SHIFT;
2044 delta += event->time_delta;
2045 cpu_buffer->read_stamp += delta;
2046 return;
2047
2048 case RINGBUF_TYPE_TIME_STAMP:
2049 /* FIXME: not implemented */
2050 return;
2051
2052 case RINGBUF_TYPE_DATA:
2053 cpu_buffer->read_stamp += event->time_delta;
2054 return;
2055
2056 default:
2057 BUG();
2058 }
2059 return;
2060}
2061
2062static void
2063rb_update_iter_read_stamp(struct ring_buffer_iter *iter,
2064 struct ring_buffer_event *event)
2065{
2066 u64 delta;
2067
2068 switch (event->type) {
2069 case RINGBUF_TYPE_PADDING:
2070 return;
2071
2072 case RINGBUF_TYPE_TIME_EXTEND:
2073 delta = event->array[0];
2074 delta <<= TS_SHIFT;
2075 delta += event->time_delta;
2076 iter->read_stamp += delta;
2077 return;
2078
2079 case RINGBUF_TYPE_TIME_STAMP:
2080 /* FIXME: not implemented */
2081 return;
2082
2083 case RINGBUF_TYPE_DATA:
2084 iter->read_stamp += event->time_delta;
2085 return;
2086
2087 default:
2088 BUG();
2089 }
2090 return;
2091}
2092
Steven Rostedtd7690412008-10-01 00:29:53 -04002093static struct buffer_page *
2094rb_get_reader_page(struct ring_buffer_per_cpu *cpu_buffer)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002095{
Steven Rostedtd7690412008-10-01 00:29:53 -04002096 struct buffer_page *reader = NULL;
2097 unsigned long flags;
Steven Rostedt818e3dd2008-10-31 09:58:35 -04002098 int nr_loops = 0;
Steven Rostedtd7690412008-10-01 00:29:53 -04002099
Steven Rostedt3e03fb72008-11-06 00:09:43 -05002100 local_irq_save(flags);
2101 __raw_spin_lock(&cpu_buffer->lock);
Steven Rostedtd7690412008-10-01 00:29:53 -04002102
2103 again:
Steven Rostedt818e3dd2008-10-31 09:58:35 -04002104 /*
2105 * This should normally only loop twice. But because the
2106 * start of the reader inserts an empty page, it causes
2107 * a case where we will loop three times. There should be no
2108 * reason to loop four times (that I know of).
2109 */
Steven Rostedt3e89c7b2008-11-11 15:28:41 -05002110 if (RB_WARN_ON(cpu_buffer, ++nr_loops > 3)) {
Steven Rostedt818e3dd2008-10-31 09:58:35 -04002111 reader = NULL;
2112 goto out;
2113 }
2114
Steven Rostedtd7690412008-10-01 00:29:53 -04002115 reader = cpu_buffer->reader_page;
2116
2117 /* If there's more to read, return this page */
Steven Rostedtbf41a152008-10-04 02:00:59 -04002118 if (cpu_buffer->reader_page->read < rb_page_size(reader))
Steven Rostedtd7690412008-10-01 00:29:53 -04002119 goto out;
2120
2121 /* Never should we have an index greater than the size */
Steven Rostedt3e89c7b2008-11-11 15:28:41 -05002122 if (RB_WARN_ON(cpu_buffer,
2123 cpu_buffer->reader_page->read > rb_page_size(reader)))
2124 goto out;
Steven Rostedtd7690412008-10-01 00:29:53 -04002125
2126 /* check if we caught up to the tail */
2127 reader = NULL;
Steven Rostedtbf41a152008-10-04 02:00:59 -04002128 if (cpu_buffer->commit_page == cpu_buffer->reader_page)
Steven Rostedtd7690412008-10-01 00:29:53 -04002129 goto out;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002130
2131 /*
Steven Rostedtd7690412008-10-01 00:29:53 -04002132 * Splice the empty reader page into the list around the head.
2133 * Reset the reader page to size zero.
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002134 */
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002135
Steven Rostedtd7690412008-10-01 00:29:53 -04002136 reader = cpu_buffer->head_page;
2137 cpu_buffer->reader_page->list.next = reader->list.next;
2138 cpu_buffer->reader_page->list.prev = reader->list.prev;
Steven Rostedtbf41a152008-10-04 02:00:59 -04002139
2140 local_set(&cpu_buffer->reader_page->write, 0);
Steven Rostedtabc9b562008-12-02 15:34:06 -05002141 local_set(&cpu_buffer->reader_page->page->commit, 0);
Steven Rostedtd7690412008-10-01 00:29:53 -04002142
2143 /* Make the reader page now replace the head */
2144 reader->list.prev->next = &cpu_buffer->reader_page->list;
2145 reader->list.next->prev = &cpu_buffer->reader_page->list;
2146
2147 /*
2148 * If the tail is on the reader, then we must set the head
2149 * to the inserted page, otherwise we set it one before.
2150 */
2151 cpu_buffer->head_page = cpu_buffer->reader_page;
2152
Steven Rostedtbf41a152008-10-04 02:00:59 -04002153 if (cpu_buffer->commit_page != reader)
Steven Rostedtd7690412008-10-01 00:29:53 -04002154 rb_inc_page(cpu_buffer, &cpu_buffer->head_page);
2155
2156 /* Finally update the reader page to the new head */
2157 cpu_buffer->reader_page = reader;
2158 rb_reset_reader_page(cpu_buffer);
2159
2160 goto again;
2161
2162 out:
Steven Rostedt3e03fb72008-11-06 00:09:43 -05002163 __raw_spin_unlock(&cpu_buffer->lock);
2164 local_irq_restore(flags);
Steven Rostedtd7690412008-10-01 00:29:53 -04002165
2166 return reader;
2167}
2168
2169static void rb_advance_reader(struct ring_buffer_per_cpu *cpu_buffer)
2170{
2171 struct ring_buffer_event *event;
2172 struct buffer_page *reader;
2173 unsigned length;
2174
2175 reader = rb_get_reader_page(cpu_buffer);
2176
2177 /* This function should not be called when buffer is empty */
Steven Rostedt3e89c7b2008-11-11 15:28:41 -05002178 if (RB_WARN_ON(cpu_buffer, !reader))
2179 return;
Steven Rostedtd7690412008-10-01 00:29:53 -04002180
2181 event = rb_reader_event(cpu_buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002182
Tom Zanussi2d622712009-03-22 03:30:49 -05002183 if (event->type == RINGBUF_TYPE_DATA || rb_discarded_event(event))
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002184 cpu_buffer->entries--;
2185
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002186 rb_update_read_stamp(cpu_buffer, event);
2187
Steven Rostedtd7690412008-10-01 00:29:53 -04002188 length = rb_event_length(event);
Steven Rostedt6f807ac2008-10-04 02:00:58 -04002189 cpu_buffer->reader_page->read += length;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002190}
2191
2192static void rb_advance_iter(struct ring_buffer_iter *iter)
2193{
2194 struct ring_buffer *buffer;
2195 struct ring_buffer_per_cpu *cpu_buffer;
2196 struct ring_buffer_event *event;
2197 unsigned length;
2198
2199 cpu_buffer = iter->cpu_buffer;
2200 buffer = cpu_buffer->buffer;
2201
2202 /*
2203 * Check if we are at the end of the buffer.
2204 */
Steven Rostedtbf41a152008-10-04 02:00:59 -04002205 if (iter->head >= rb_page_size(iter->head_page)) {
Steven Rostedt3e89c7b2008-11-11 15:28:41 -05002206 if (RB_WARN_ON(buffer,
2207 iter->head_page == cpu_buffer->commit_page))
2208 return;
Steven Rostedtd7690412008-10-01 00:29:53 -04002209 rb_inc_iter(iter);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002210 return;
2211 }
2212
2213 event = rb_iter_head_event(iter);
2214
2215 length = rb_event_length(event);
2216
2217 /*
2218 * This should not be called to advance the header if we are
2219 * at the tail of the buffer.
2220 */
Steven Rostedt3e89c7b2008-11-11 15:28:41 -05002221 if (RB_WARN_ON(cpu_buffer,
Steven Rostedtf536aaf2008-11-10 23:07:30 -05002222 (iter->head_page == cpu_buffer->commit_page) &&
Steven Rostedt3e89c7b2008-11-11 15:28:41 -05002223 (iter->head + length > rb_commit_index(cpu_buffer))))
2224 return;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002225
2226 rb_update_iter_read_stamp(iter, event);
2227
2228 iter->head += length;
2229
2230 /* check for end of page padding */
Steven Rostedtbf41a152008-10-04 02:00:59 -04002231 if ((iter->head >= rb_page_size(iter->head_page)) &&
2232 (iter->head_page != cpu_buffer->commit_page))
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002233 rb_advance_iter(iter);
2234}
2235
Steven Rostedtf83c9d02008-11-11 18:47:44 +01002236static struct ring_buffer_event *
2237rb_buffer_peek(struct ring_buffer *buffer, int cpu, u64 *ts)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002238{
2239 struct ring_buffer_per_cpu *cpu_buffer;
2240 struct ring_buffer_event *event;
Steven Rostedtd7690412008-10-01 00:29:53 -04002241 struct buffer_page *reader;
Steven Rostedt818e3dd2008-10-31 09:58:35 -04002242 int nr_loops = 0;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002243
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002244 cpu_buffer = buffer->buffers[cpu];
2245
2246 again:
Steven Rostedt818e3dd2008-10-31 09:58:35 -04002247 /*
2248 * We repeat when a timestamp is encountered. It is possible
2249 * to get multiple timestamps from an interrupt entering just
2250 * as one timestamp is about to be written. The max times
2251 * that this can happen is the number of nested interrupts we
2252 * can have. Nesting 10 deep of interrupts is clearly
2253 * an anomaly.
2254 */
Steven Rostedt3e89c7b2008-11-11 15:28:41 -05002255 if (RB_WARN_ON(cpu_buffer, ++nr_loops > 10))
Steven Rostedt818e3dd2008-10-31 09:58:35 -04002256 return NULL;
Steven Rostedt818e3dd2008-10-31 09:58:35 -04002257
Steven Rostedtd7690412008-10-01 00:29:53 -04002258 reader = rb_get_reader_page(cpu_buffer);
2259 if (!reader)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002260 return NULL;
2261
Steven Rostedtd7690412008-10-01 00:29:53 -04002262 event = rb_reader_event(cpu_buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002263
2264 switch (event->type) {
2265 case RINGBUF_TYPE_PADDING:
Tom Zanussi2d622712009-03-22 03:30:49 -05002266 if (rb_null_event(event))
2267 RB_WARN_ON(cpu_buffer, 1);
2268 /*
2269 * Because the writer could be discarding every
2270 * event it creates (which would probably be bad)
2271 * if we were to go back to "again" then we may never
2272 * catch up, and will trigger the warn on, or lock
2273 * the box. Return the padding, and we will release
2274 * the current locks, and try again.
2275 */
Steven Rostedtd7690412008-10-01 00:29:53 -04002276 rb_advance_reader(cpu_buffer);
Tom Zanussi2d622712009-03-22 03:30:49 -05002277 return event;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002278
2279 case RINGBUF_TYPE_TIME_EXTEND:
2280 /* Internal data, OK to advance */
Steven Rostedtd7690412008-10-01 00:29:53 -04002281 rb_advance_reader(cpu_buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002282 goto again;
2283
2284 case RINGBUF_TYPE_TIME_STAMP:
2285 /* FIXME: not implemented */
Steven Rostedtd7690412008-10-01 00:29:53 -04002286 rb_advance_reader(cpu_buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002287 goto again;
2288
2289 case RINGBUF_TYPE_DATA:
2290 if (ts) {
2291 *ts = cpu_buffer->read_stamp + event->time_delta;
Steven Rostedt37886f62009-03-17 17:22:06 -04002292 ring_buffer_normalize_time_stamp(buffer,
2293 cpu_buffer->cpu, ts);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002294 }
2295 return event;
2296
2297 default:
2298 BUG();
2299 }
2300
2301 return NULL;
2302}
Robert Richterc4f50182008-12-11 16:49:22 +01002303EXPORT_SYMBOL_GPL(ring_buffer_peek);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002304
Steven Rostedtf83c9d02008-11-11 18:47:44 +01002305static struct ring_buffer_event *
2306rb_iter_peek(struct ring_buffer_iter *iter, u64 *ts)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002307{
2308 struct ring_buffer *buffer;
2309 struct ring_buffer_per_cpu *cpu_buffer;
2310 struct ring_buffer_event *event;
Steven Rostedt818e3dd2008-10-31 09:58:35 -04002311 int nr_loops = 0;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002312
2313 if (ring_buffer_iter_empty(iter))
2314 return NULL;
2315
2316 cpu_buffer = iter->cpu_buffer;
2317 buffer = cpu_buffer->buffer;
2318
2319 again:
Steven Rostedt818e3dd2008-10-31 09:58:35 -04002320 /*
2321 * We repeat when a timestamp is encountered. It is possible
2322 * to get multiple timestamps from an interrupt entering just
2323 * as one timestamp is about to be written. The max times
2324 * that this can happen is the number of nested interrupts we
2325 * can have. Nesting 10 deep of interrupts is clearly
2326 * an anomaly.
2327 */
Steven Rostedt3e89c7b2008-11-11 15:28:41 -05002328 if (RB_WARN_ON(cpu_buffer, ++nr_loops > 10))
Steven Rostedt818e3dd2008-10-31 09:58:35 -04002329 return NULL;
Steven Rostedt818e3dd2008-10-31 09:58:35 -04002330
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002331 if (rb_per_cpu_empty(cpu_buffer))
2332 return NULL;
2333
2334 event = rb_iter_head_event(iter);
2335
2336 switch (event->type) {
2337 case RINGBUF_TYPE_PADDING:
Tom Zanussi2d622712009-03-22 03:30:49 -05002338 if (rb_null_event(event)) {
2339 rb_inc_iter(iter);
2340 goto again;
2341 }
2342 rb_advance_iter(iter);
2343 return event;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002344
2345 case RINGBUF_TYPE_TIME_EXTEND:
2346 /* Internal data, OK to advance */
2347 rb_advance_iter(iter);
2348 goto again;
2349
2350 case RINGBUF_TYPE_TIME_STAMP:
2351 /* FIXME: not implemented */
2352 rb_advance_iter(iter);
2353 goto again;
2354
2355 case RINGBUF_TYPE_DATA:
2356 if (ts) {
2357 *ts = iter->read_stamp + event->time_delta;
Steven Rostedt37886f62009-03-17 17:22:06 -04002358 ring_buffer_normalize_time_stamp(buffer,
2359 cpu_buffer->cpu, ts);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002360 }
2361 return event;
2362
2363 default:
2364 BUG();
2365 }
2366
2367 return NULL;
2368}
Robert Richterc4f50182008-12-11 16:49:22 +01002369EXPORT_SYMBOL_GPL(ring_buffer_iter_peek);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002370
2371/**
Steven Rostedtf83c9d02008-11-11 18:47:44 +01002372 * ring_buffer_peek - peek at the next event to be read
2373 * @buffer: The ring buffer to read
2374 * @cpu: The cpu to peak at
2375 * @ts: The timestamp counter of this event.
2376 *
2377 * This will return the event that will be read next, but does
2378 * not consume the data.
2379 */
2380struct ring_buffer_event *
2381ring_buffer_peek(struct ring_buffer *buffer, int cpu, u64 *ts)
2382{
2383 struct ring_buffer_per_cpu *cpu_buffer = buffer->buffers[cpu];
Steven Rostedt8aabee52009-03-12 13:13:49 -04002384 struct ring_buffer_event *event;
Steven Rostedtf83c9d02008-11-11 18:47:44 +01002385 unsigned long flags;
2386
Steven Rostedt554f7862009-03-11 22:00:13 -04002387 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedt8aabee52009-03-12 13:13:49 -04002388 return NULL;
Steven Rostedt554f7862009-03-11 22:00:13 -04002389
Tom Zanussi2d622712009-03-22 03:30:49 -05002390 again:
Steven Rostedtf83c9d02008-11-11 18:47:44 +01002391 spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
2392 event = rb_buffer_peek(buffer, cpu, ts);
2393 spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
2394
Tom Zanussi2d622712009-03-22 03:30:49 -05002395 if (event && event->type == RINGBUF_TYPE_PADDING) {
2396 cpu_relax();
2397 goto again;
2398 }
2399
Steven Rostedtf83c9d02008-11-11 18:47:44 +01002400 return event;
2401}
2402
2403/**
2404 * ring_buffer_iter_peek - peek at the next event to be read
2405 * @iter: The ring buffer iterator
2406 * @ts: The timestamp counter of this event.
2407 *
2408 * This will return the event that will be read next, but does
2409 * not increment the iterator.
2410 */
2411struct ring_buffer_event *
2412ring_buffer_iter_peek(struct ring_buffer_iter *iter, u64 *ts)
2413{
2414 struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
2415 struct ring_buffer_event *event;
2416 unsigned long flags;
2417
Tom Zanussi2d622712009-03-22 03:30:49 -05002418 again:
Steven Rostedtf83c9d02008-11-11 18:47:44 +01002419 spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
2420 event = rb_iter_peek(iter, ts);
2421 spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
2422
Tom Zanussi2d622712009-03-22 03:30:49 -05002423 if (event && event->type == RINGBUF_TYPE_PADDING) {
2424 cpu_relax();
2425 goto again;
2426 }
2427
Steven Rostedtf83c9d02008-11-11 18:47:44 +01002428 return event;
2429}
2430
2431/**
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002432 * ring_buffer_consume - return an event and consume it
2433 * @buffer: The ring buffer to get the next event from
2434 *
2435 * Returns the next event in the ring buffer, and that event is consumed.
2436 * Meaning, that sequential reads will keep returning a different event,
2437 * and eventually empty the ring buffer if the producer is slower.
2438 */
2439struct ring_buffer_event *
2440ring_buffer_consume(struct ring_buffer *buffer, int cpu, u64 *ts)
2441{
Steven Rostedt554f7862009-03-11 22:00:13 -04002442 struct ring_buffer_per_cpu *cpu_buffer;
2443 struct ring_buffer_event *event = NULL;
Steven Rostedtf83c9d02008-11-11 18:47:44 +01002444 unsigned long flags;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002445
Tom Zanussi2d622712009-03-22 03:30:49 -05002446 again:
Steven Rostedt554f7862009-03-11 22:00:13 -04002447 /* might be called in atomic */
2448 preempt_disable();
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002449
Steven Rostedt554f7862009-03-11 22:00:13 -04002450 if (!cpumask_test_cpu(cpu, buffer->cpumask))
2451 goto out;
2452
2453 cpu_buffer = buffer->buffers[cpu];
Steven Rostedtf83c9d02008-11-11 18:47:44 +01002454 spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002455
Steven Rostedtf83c9d02008-11-11 18:47:44 +01002456 event = rb_buffer_peek(buffer, cpu, ts);
2457 if (!event)
Steven Rostedt554f7862009-03-11 22:00:13 -04002458 goto out_unlock;
Steven Rostedtf83c9d02008-11-11 18:47:44 +01002459
Steven Rostedtd7690412008-10-01 00:29:53 -04002460 rb_advance_reader(cpu_buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002461
Steven Rostedt554f7862009-03-11 22:00:13 -04002462 out_unlock:
Steven Rostedtf83c9d02008-11-11 18:47:44 +01002463 spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
2464
Steven Rostedt554f7862009-03-11 22:00:13 -04002465 out:
2466 preempt_enable();
2467
Tom Zanussi2d622712009-03-22 03:30:49 -05002468 if (event && event->type == RINGBUF_TYPE_PADDING) {
2469 cpu_relax();
2470 goto again;
2471 }
2472
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002473 return event;
2474}
Robert Richterc4f50182008-12-11 16:49:22 +01002475EXPORT_SYMBOL_GPL(ring_buffer_consume);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002476
2477/**
2478 * ring_buffer_read_start - start a non consuming read of the buffer
2479 * @buffer: The ring buffer to read from
2480 * @cpu: The cpu buffer to iterate over
2481 *
2482 * This starts up an iteration through the buffer. It also disables
2483 * the recording to the buffer until the reading is finished.
2484 * This prevents the reading from being corrupted. This is not
2485 * a consuming read, so a producer is not expected.
2486 *
2487 * Must be paired with ring_buffer_finish.
2488 */
2489struct ring_buffer_iter *
2490ring_buffer_read_start(struct ring_buffer *buffer, int cpu)
2491{
2492 struct ring_buffer_per_cpu *cpu_buffer;
Steven Rostedt8aabee52009-03-12 13:13:49 -04002493 struct ring_buffer_iter *iter;
Steven Rostedtd7690412008-10-01 00:29:53 -04002494 unsigned long flags;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002495
Rusty Russell9e01c1b2009-01-01 10:12:22 +10302496 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedt8aabee52009-03-12 13:13:49 -04002497 return NULL;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002498
2499 iter = kmalloc(sizeof(*iter), GFP_KERNEL);
2500 if (!iter)
Steven Rostedt8aabee52009-03-12 13:13:49 -04002501 return NULL;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002502
2503 cpu_buffer = buffer->buffers[cpu];
2504
2505 iter->cpu_buffer = cpu_buffer;
2506
2507 atomic_inc(&cpu_buffer->record_disabled);
2508 synchronize_sched();
2509
Steven Rostedtf83c9d02008-11-11 18:47:44 +01002510 spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
Steven Rostedt3e03fb72008-11-06 00:09:43 -05002511 __raw_spin_lock(&cpu_buffer->lock);
Steven Rostedt642edba2008-11-12 00:01:26 -05002512 rb_iter_reset(iter);
Steven Rostedt3e03fb72008-11-06 00:09:43 -05002513 __raw_spin_unlock(&cpu_buffer->lock);
Steven Rostedtf83c9d02008-11-11 18:47:44 +01002514 spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002515
2516 return iter;
2517}
Robert Richterc4f50182008-12-11 16:49:22 +01002518EXPORT_SYMBOL_GPL(ring_buffer_read_start);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002519
2520/**
2521 * ring_buffer_finish - finish reading the iterator of the buffer
2522 * @iter: The iterator retrieved by ring_buffer_start
2523 *
2524 * This re-enables the recording to the buffer, and frees the
2525 * iterator.
2526 */
2527void
2528ring_buffer_read_finish(struct ring_buffer_iter *iter)
2529{
2530 struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
2531
2532 atomic_dec(&cpu_buffer->record_disabled);
2533 kfree(iter);
2534}
Robert Richterc4f50182008-12-11 16:49:22 +01002535EXPORT_SYMBOL_GPL(ring_buffer_read_finish);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002536
2537/**
2538 * ring_buffer_read - read the next item in the ring buffer by the iterator
2539 * @iter: The ring buffer iterator
2540 * @ts: The time stamp of the event read.
2541 *
2542 * This reads the next event in the ring buffer and increments the iterator.
2543 */
2544struct ring_buffer_event *
2545ring_buffer_read(struct ring_buffer_iter *iter, u64 *ts)
2546{
2547 struct ring_buffer_event *event;
Steven Rostedtf83c9d02008-11-11 18:47:44 +01002548 struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
2549 unsigned long flags;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002550
Tom Zanussi2d622712009-03-22 03:30:49 -05002551 again:
Steven Rostedtf83c9d02008-11-11 18:47:44 +01002552 spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
2553 event = rb_iter_peek(iter, ts);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002554 if (!event)
Steven Rostedtf83c9d02008-11-11 18:47:44 +01002555 goto out;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002556
2557 rb_advance_iter(iter);
Steven Rostedtf83c9d02008-11-11 18:47:44 +01002558 out:
2559 spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002560
Tom Zanussi2d622712009-03-22 03:30:49 -05002561 if (event && event->type == RINGBUF_TYPE_PADDING) {
2562 cpu_relax();
2563 goto again;
2564 }
2565
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002566 return event;
2567}
Robert Richterc4f50182008-12-11 16:49:22 +01002568EXPORT_SYMBOL_GPL(ring_buffer_read);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002569
2570/**
2571 * ring_buffer_size - return the size of the ring buffer (in bytes)
2572 * @buffer: The ring buffer.
2573 */
2574unsigned long ring_buffer_size(struct ring_buffer *buffer)
2575{
2576 return BUF_PAGE_SIZE * buffer->pages;
2577}
Robert Richterc4f50182008-12-11 16:49:22 +01002578EXPORT_SYMBOL_GPL(ring_buffer_size);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002579
2580static void
2581rb_reset_cpu(struct ring_buffer_per_cpu *cpu_buffer)
2582{
2583 cpu_buffer->head_page
2584 = list_entry(cpu_buffer->pages.next, struct buffer_page, list);
Steven Rostedtbf41a152008-10-04 02:00:59 -04002585 local_set(&cpu_buffer->head_page->write, 0);
Steven Rostedtabc9b562008-12-02 15:34:06 -05002586 local_set(&cpu_buffer->head_page->page->commit, 0);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002587
Steven Rostedt6f807ac2008-10-04 02:00:58 -04002588 cpu_buffer->head_page->read = 0;
Steven Rostedtbf41a152008-10-04 02:00:59 -04002589
2590 cpu_buffer->tail_page = cpu_buffer->head_page;
2591 cpu_buffer->commit_page = cpu_buffer->head_page;
2592
2593 INIT_LIST_HEAD(&cpu_buffer->reader_page->list);
2594 local_set(&cpu_buffer->reader_page->write, 0);
Steven Rostedtabc9b562008-12-02 15:34:06 -05002595 local_set(&cpu_buffer->reader_page->page->commit, 0);
Steven Rostedt6f807ac2008-10-04 02:00:58 -04002596 cpu_buffer->reader_page->read = 0;
Steven Rostedtd7690412008-10-01 00:29:53 -04002597
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002598 cpu_buffer->overrun = 0;
2599 cpu_buffer->entries = 0;
Steven Rostedt69507c02009-01-21 18:45:57 -05002600
2601 cpu_buffer->write_stamp = 0;
2602 cpu_buffer->read_stamp = 0;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002603}
2604
2605/**
2606 * ring_buffer_reset_cpu - reset a ring buffer per CPU buffer
2607 * @buffer: The ring buffer to reset a per cpu buffer of
2608 * @cpu: The CPU buffer to be reset
2609 */
2610void ring_buffer_reset_cpu(struct ring_buffer *buffer, int cpu)
2611{
2612 struct ring_buffer_per_cpu *cpu_buffer = buffer->buffers[cpu];
2613 unsigned long flags;
2614
Rusty Russell9e01c1b2009-01-01 10:12:22 +10302615 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedt8aabee52009-03-12 13:13:49 -04002616 return;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002617
Steven Rostedtf83c9d02008-11-11 18:47:44 +01002618 spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
2619
Steven Rostedt3e03fb72008-11-06 00:09:43 -05002620 __raw_spin_lock(&cpu_buffer->lock);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002621
2622 rb_reset_cpu(cpu_buffer);
2623
Steven Rostedt3e03fb72008-11-06 00:09:43 -05002624 __raw_spin_unlock(&cpu_buffer->lock);
Steven Rostedtf83c9d02008-11-11 18:47:44 +01002625
2626 spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002627}
Robert Richterc4f50182008-12-11 16:49:22 +01002628EXPORT_SYMBOL_GPL(ring_buffer_reset_cpu);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002629
2630/**
2631 * ring_buffer_reset - reset a ring buffer
2632 * @buffer: The ring buffer to reset all cpu buffers
2633 */
2634void ring_buffer_reset(struct ring_buffer *buffer)
2635{
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002636 int cpu;
2637
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002638 for_each_buffer_cpu(buffer, cpu)
Steven Rostedtd7690412008-10-01 00:29:53 -04002639 ring_buffer_reset_cpu(buffer, cpu);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002640}
Robert Richterc4f50182008-12-11 16:49:22 +01002641EXPORT_SYMBOL_GPL(ring_buffer_reset);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002642
2643/**
2644 * rind_buffer_empty - is the ring buffer empty?
2645 * @buffer: The ring buffer to test
2646 */
2647int ring_buffer_empty(struct ring_buffer *buffer)
2648{
2649 struct ring_buffer_per_cpu *cpu_buffer;
2650 int cpu;
2651
2652 /* yes this is racy, but if you don't like the race, lock the buffer */
2653 for_each_buffer_cpu(buffer, cpu) {
2654 cpu_buffer = buffer->buffers[cpu];
2655 if (!rb_per_cpu_empty(cpu_buffer))
2656 return 0;
2657 }
Steven Rostedt554f7862009-03-11 22:00:13 -04002658
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002659 return 1;
2660}
Robert Richterc4f50182008-12-11 16:49:22 +01002661EXPORT_SYMBOL_GPL(ring_buffer_empty);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002662
2663/**
2664 * ring_buffer_empty_cpu - is a cpu buffer of a ring buffer empty?
2665 * @buffer: The ring buffer
2666 * @cpu: The CPU buffer to test
2667 */
2668int ring_buffer_empty_cpu(struct ring_buffer *buffer, int cpu)
2669{
2670 struct ring_buffer_per_cpu *cpu_buffer;
Steven Rostedt8aabee52009-03-12 13:13:49 -04002671 int ret;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002672
Rusty Russell9e01c1b2009-01-01 10:12:22 +10302673 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedt8aabee52009-03-12 13:13:49 -04002674 return 1;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002675
2676 cpu_buffer = buffer->buffers[cpu];
Steven Rostedt554f7862009-03-11 22:00:13 -04002677 ret = rb_per_cpu_empty(cpu_buffer);
2678
Steven Rostedt554f7862009-03-11 22:00:13 -04002679
2680 return ret;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002681}
Robert Richterc4f50182008-12-11 16:49:22 +01002682EXPORT_SYMBOL_GPL(ring_buffer_empty_cpu);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002683
2684/**
2685 * ring_buffer_swap_cpu - swap a CPU buffer between two ring buffers
2686 * @buffer_a: One buffer to swap with
2687 * @buffer_b: The other buffer to swap with
2688 *
2689 * This function is useful for tracers that want to take a "snapshot"
2690 * of a CPU buffer and has another back up buffer lying around.
2691 * it is expected that the tracer handles the cpu buffer not being
2692 * used at the moment.
2693 */
2694int ring_buffer_swap_cpu(struct ring_buffer *buffer_a,
2695 struct ring_buffer *buffer_b, int cpu)
2696{
2697 struct ring_buffer_per_cpu *cpu_buffer_a;
2698 struct ring_buffer_per_cpu *cpu_buffer_b;
Steven Rostedt554f7862009-03-11 22:00:13 -04002699 int ret = -EINVAL;
2700
Rusty Russell9e01c1b2009-01-01 10:12:22 +10302701 if (!cpumask_test_cpu(cpu, buffer_a->cpumask) ||
2702 !cpumask_test_cpu(cpu, buffer_b->cpumask))
Steven Rostedt554f7862009-03-11 22:00:13 -04002703 goto out;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002704
2705 /* At least make sure the two buffers are somewhat the same */
Lai Jiangshan6d102bc2008-12-17 17:48:23 +08002706 if (buffer_a->pages != buffer_b->pages)
Steven Rostedt554f7862009-03-11 22:00:13 -04002707 goto out;
2708
2709 ret = -EAGAIN;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002710
Steven Rostedt97b17ef2009-01-21 15:24:56 -05002711 if (ring_buffer_flags != RB_BUFFERS_ON)
Steven Rostedt554f7862009-03-11 22:00:13 -04002712 goto out;
Steven Rostedt97b17ef2009-01-21 15:24:56 -05002713
2714 if (atomic_read(&buffer_a->record_disabled))
Steven Rostedt554f7862009-03-11 22:00:13 -04002715 goto out;
Steven Rostedt97b17ef2009-01-21 15:24:56 -05002716
2717 if (atomic_read(&buffer_b->record_disabled))
Steven Rostedt554f7862009-03-11 22:00:13 -04002718 goto out;
Steven Rostedt97b17ef2009-01-21 15:24:56 -05002719
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002720 cpu_buffer_a = buffer_a->buffers[cpu];
2721 cpu_buffer_b = buffer_b->buffers[cpu];
2722
Steven Rostedt97b17ef2009-01-21 15:24:56 -05002723 if (atomic_read(&cpu_buffer_a->record_disabled))
Steven Rostedt554f7862009-03-11 22:00:13 -04002724 goto out;
Steven Rostedt97b17ef2009-01-21 15:24:56 -05002725
2726 if (atomic_read(&cpu_buffer_b->record_disabled))
Steven Rostedt554f7862009-03-11 22:00:13 -04002727 goto out;
Steven Rostedt97b17ef2009-01-21 15:24:56 -05002728
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002729 /*
2730 * We can't do a synchronize_sched here because this
2731 * function can be called in atomic context.
2732 * Normally this will be called from the same CPU as cpu.
2733 * If not it's up to the caller to protect this.
2734 */
2735 atomic_inc(&cpu_buffer_a->record_disabled);
2736 atomic_inc(&cpu_buffer_b->record_disabled);
2737
2738 buffer_a->buffers[cpu] = cpu_buffer_b;
2739 buffer_b->buffers[cpu] = cpu_buffer_a;
2740
2741 cpu_buffer_b->buffer = buffer_a;
2742 cpu_buffer_a->buffer = buffer_b;
2743
2744 atomic_dec(&cpu_buffer_a->record_disabled);
2745 atomic_dec(&cpu_buffer_b->record_disabled);
2746
Steven Rostedt554f7862009-03-11 22:00:13 -04002747 ret = 0;
2748out:
Steven Rostedt554f7862009-03-11 22:00:13 -04002749 return ret;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002750}
Robert Richterc4f50182008-12-11 16:49:22 +01002751EXPORT_SYMBOL_GPL(ring_buffer_swap_cpu);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002752
Steven Rostedt8789a9e2008-12-02 15:34:07 -05002753static void rb_remove_entries(struct ring_buffer_per_cpu *cpu_buffer,
Lai Jiangshan667d2412009-02-09 14:21:17 +08002754 struct buffer_data_page *bpage,
2755 unsigned int offset)
Steven Rostedt8789a9e2008-12-02 15:34:07 -05002756{
2757 struct ring_buffer_event *event;
2758 unsigned long head;
2759
2760 __raw_spin_lock(&cpu_buffer->lock);
Lai Jiangshan667d2412009-02-09 14:21:17 +08002761 for (head = offset; head < local_read(&bpage->commit);
Steven Rostedt8789a9e2008-12-02 15:34:07 -05002762 head += rb_event_length(event)) {
2763
Steven Rostedt044fa782008-12-02 23:50:03 -05002764 event = __rb_data_page_index(bpage, head);
Steven Rostedt8789a9e2008-12-02 15:34:07 -05002765 if (RB_WARN_ON(cpu_buffer, rb_null_event(event)))
2766 return;
2767 /* Only count data entries */
2768 if (event->type != RINGBUF_TYPE_DATA)
2769 continue;
2770 cpu_buffer->entries--;
2771 }
2772 __raw_spin_unlock(&cpu_buffer->lock);
2773}
2774
2775/**
2776 * ring_buffer_alloc_read_page - allocate a page to read from buffer
2777 * @buffer: the buffer to allocate for.
2778 *
2779 * This function is used in conjunction with ring_buffer_read_page.
2780 * When reading a full page from the ring buffer, these functions
2781 * can be used to speed up the process. The calling function should
2782 * allocate a few pages first with this function. Then when it
2783 * needs to get pages from the ring buffer, it passes the result
2784 * of this function into ring_buffer_read_page, which will swap
2785 * the page that was allocated, with the read page of the buffer.
2786 *
2787 * Returns:
2788 * The page allocated, or NULL on error.
2789 */
2790void *ring_buffer_alloc_read_page(struct ring_buffer *buffer)
2791{
Steven Rostedt044fa782008-12-02 23:50:03 -05002792 struct buffer_data_page *bpage;
Steven Rostedtef7a4a12009-03-03 00:27:49 -05002793 unsigned long addr;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05002794
2795 addr = __get_free_page(GFP_KERNEL);
2796 if (!addr)
2797 return NULL;
2798
Steven Rostedt044fa782008-12-02 23:50:03 -05002799 bpage = (void *)addr;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05002800
Steven Rostedtef7a4a12009-03-03 00:27:49 -05002801 rb_init_page(bpage);
2802
Steven Rostedt044fa782008-12-02 23:50:03 -05002803 return bpage;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05002804}
2805
2806/**
2807 * ring_buffer_free_read_page - free an allocated read page
2808 * @buffer: the buffer the page was allocate for
2809 * @data: the page to free
2810 *
2811 * Free a page allocated from ring_buffer_alloc_read_page.
2812 */
2813void ring_buffer_free_read_page(struct ring_buffer *buffer, void *data)
2814{
2815 free_page((unsigned long)data);
2816}
2817
2818/**
2819 * ring_buffer_read_page - extract a page from the ring buffer
2820 * @buffer: buffer to extract from
2821 * @data_page: the page to use allocated from ring_buffer_alloc_read_page
Steven Rostedtef7a4a12009-03-03 00:27:49 -05002822 * @len: amount to extract
Steven Rostedt8789a9e2008-12-02 15:34:07 -05002823 * @cpu: the cpu of the buffer to extract
2824 * @full: should the extraction only happen when the page is full.
2825 *
2826 * This function will pull out a page from the ring buffer and consume it.
2827 * @data_page must be the address of the variable that was returned
2828 * from ring_buffer_alloc_read_page. This is because the page might be used
2829 * to swap with a page in the ring buffer.
2830 *
2831 * for example:
Lai Jiangshanb85fa012009-02-09 14:21:14 +08002832 * rpage = ring_buffer_alloc_read_page(buffer);
Steven Rostedt8789a9e2008-12-02 15:34:07 -05002833 * if (!rpage)
2834 * return error;
Steven Rostedtef7a4a12009-03-03 00:27:49 -05002835 * ret = ring_buffer_read_page(buffer, &rpage, len, cpu, 0);
Lai Jiangshan667d2412009-02-09 14:21:17 +08002836 * if (ret >= 0)
2837 * process_page(rpage, ret);
Steven Rostedt8789a9e2008-12-02 15:34:07 -05002838 *
2839 * When @full is set, the function will not return true unless
2840 * the writer is off the reader page.
2841 *
2842 * Note: it is up to the calling functions to handle sleeps and wakeups.
2843 * The ring buffer can be used anywhere in the kernel and can not
2844 * blindly call wake_up. The layer that uses the ring buffer must be
2845 * responsible for that.
2846 *
2847 * Returns:
Lai Jiangshan667d2412009-02-09 14:21:17 +08002848 * >=0 if data has been transferred, returns the offset of consumed data.
2849 * <0 if no data has been transferred.
Steven Rostedt8789a9e2008-12-02 15:34:07 -05002850 */
2851int ring_buffer_read_page(struct ring_buffer *buffer,
Steven Rostedtef7a4a12009-03-03 00:27:49 -05002852 void **data_page, size_t len, int cpu, int full)
Steven Rostedt8789a9e2008-12-02 15:34:07 -05002853{
2854 struct ring_buffer_per_cpu *cpu_buffer = buffer->buffers[cpu];
2855 struct ring_buffer_event *event;
Steven Rostedt044fa782008-12-02 23:50:03 -05002856 struct buffer_data_page *bpage;
Steven Rostedtef7a4a12009-03-03 00:27:49 -05002857 struct buffer_page *reader;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05002858 unsigned long flags;
Steven Rostedtef7a4a12009-03-03 00:27:49 -05002859 unsigned int commit;
Lai Jiangshan667d2412009-02-09 14:21:17 +08002860 unsigned int read;
Steven Rostedt4f3640f2009-03-03 23:52:42 -05002861 u64 save_timestamp;
Lai Jiangshan667d2412009-02-09 14:21:17 +08002862 int ret = -1;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05002863
Steven Rostedt554f7862009-03-11 22:00:13 -04002864 if (!cpumask_test_cpu(cpu, buffer->cpumask))
2865 goto out;
2866
Steven Rostedt474d32b2009-03-03 19:51:40 -05002867 /*
2868 * If len is not big enough to hold the page header, then
2869 * we can not copy anything.
2870 */
2871 if (len <= BUF_PAGE_HDR_SIZE)
Steven Rostedt554f7862009-03-11 22:00:13 -04002872 goto out;
Steven Rostedt474d32b2009-03-03 19:51:40 -05002873
2874 len -= BUF_PAGE_HDR_SIZE;
2875
Steven Rostedt8789a9e2008-12-02 15:34:07 -05002876 if (!data_page)
Steven Rostedt554f7862009-03-11 22:00:13 -04002877 goto out;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05002878
Steven Rostedt044fa782008-12-02 23:50:03 -05002879 bpage = *data_page;
2880 if (!bpage)
Steven Rostedt554f7862009-03-11 22:00:13 -04002881 goto out;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05002882
2883 spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
2884
Steven Rostedtef7a4a12009-03-03 00:27:49 -05002885 reader = rb_get_reader_page(cpu_buffer);
2886 if (!reader)
Steven Rostedt554f7862009-03-11 22:00:13 -04002887 goto out_unlock;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05002888
Steven Rostedtef7a4a12009-03-03 00:27:49 -05002889 event = rb_reader_event(cpu_buffer);
Lai Jiangshan667d2412009-02-09 14:21:17 +08002890
Steven Rostedtef7a4a12009-03-03 00:27:49 -05002891 read = reader->read;
2892 commit = rb_page_commit(reader);
2893
Steven Rostedt8789a9e2008-12-02 15:34:07 -05002894 /*
Steven Rostedt474d32b2009-03-03 19:51:40 -05002895 * If this page has been partially read or
2896 * if len is not big enough to read the rest of the page or
2897 * a writer is still on the page, then
2898 * we must copy the data from the page to the buffer.
2899 * Otherwise, we can simply swap the page with the one passed in.
Steven Rostedt8789a9e2008-12-02 15:34:07 -05002900 */
Steven Rostedt474d32b2009-03-03 19:51:40 -05002901 if (read || (len < (commit - read)) ||
Steven Rostedtef7a4a12009-03-03 00:27:49 -05002902 cpu_buffer->reader_page == cpu_buffer->commit_page) {
Lai Jiangshan667d2412009-02-09 14:21:17 +08002903 struct buffer_data_page *rpage = cpu_buffer->reader_page->page;
Steven Rostedt474d32b2009-03-03 19:51:40 -05002904 unsigned int rpos = read;
2905 unsigned int pos = 0;
Steven Rostedtef7a4a12009-03-03 00:27:49 -05002906 unsigned int size;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05002907
2908 if (full)
Steven Rostedt554f7862009-03-11 22:00:13 -04002909 goto out_unlock;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05002910
Steven Rostedtef7a4a12009-03-03 00:27:49 -05002911 if (len > (commit - read))
2912 len = (commit - read);
2913
2914 size = rb_event_length(event);
2915
2916 if (len < size)
Steven Rostedt554f7862009-03-11 22:00:13 -04002917 goto out_unlock;
Steven Rostedtef7a4a12009-03-03 00:27:49 -05002918
Steven Rostedt4f3640f2009-03-03 23:52:42 -05002919 /* save the current timestamp, since the user will need it */
2920 save_timestamp = cpu_buffer->read_stamp;
2921
Steven Rostedtef7a4a12009-03-03 00:27:49 -05002922 /* Need to copy one event at a time */
2923 do {
Steven Rostedt474d32b2009-03-03 19:51:40 -05002924 memcpy(bpage->data + pos, rpage->data + rpos, size);
Steven Rostedtef7a4a12009-03-03 00:27:49 -05002925
2926 len -= size;
2927
2928 rb_advance_reader(cpu_buffer);
Steven Rostedt474d32b2009-03-03 19:51:40 -05002929 rpos = reader->read;
2930 pos += size;
Steven Rostedtef7a4a12009-03-03 00:27:49 -05002931
2932 event = rb_reader_event(cpu_buffer);
2933 size = rb_event_length(event);
2934 } while (len > size);
Lai Jiangshan667d2412009-02-09 14:21:17 +08002935
2936 /* update bpage */
Steven Rostedtef7a4a12009-03-03 00:27:49 -05002937 local_set(&bpage->commit, pos);
Steven Rostedt4f3640f2009-03-03 23:52:42 -05002938 bpage->time_stamp = save_timestamp;
Steven Rostedtef7a4a12009-03-03 00:27:49 -05002939
Steven Rostedt474d32b2009-03-03 19:51:40 -05002940 /* we copied everything to the beginning */
2941 read = 0;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05002942 } else {
2943 /* swap the pages */
Steven Rostedt044fa782008-12-02 23:50:03 -05002944 rb_init_page(bpage);
Steven Rostedtef7a4a12009-03-03 00:27:49 -05002945 bpage = reader->page;
2946 reader->page = *data_page;
2947 local_set(&reader->write, 0);
2948 reader->read = 0;
Steven Rostedt044fa782008-12-02 23:50:03 -05002949 *data_page = bpage;
Steven Rostedtef7a4a12009-03-03 00:27:49 -05002950
2951 /* update the entry counter */
2952 rb_remove_entries(cpu_buffer, bpage, read);
Steven Rostedt8789a9e2008-12-02 15:34:07 -05002953 }
Lai Jiangshan667d2412009-02-09 14:21:17 +08002954 ret = read;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05002955
Steven Rostedt554f7862009-03-11 22:00:13 -04002956 out_unlock:
Steven Rostedt8789a9e2008-12-02 15:34:07 -05002957 spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
2958
Steven Rostedt554f7862009-03-11 22:00:13 -04002959 out:
Steven Rostedt8789a9e2008-12-02 15:34:07 -05002960 return ret;
2961}
2962
Steven Rostedta3583242008-11-11 15:01:42 -05002963static ssize_t
2964rb_simple_read(struct file *filp, char __user *ubuf,
2965 size_t cnt, loff_t *ppos)
2966{
Hannes Eder5e398412009-02-10 19:44:34 +01002967 unsigned long *p = filp->private_data;
Steven Rostedta3583242008-11-11 15:01:42 -05002968 char buf[64];
2969 int r;
2970
Steven Rostedt033601a2008-11-21 12:41:55 -05002971 if (test_bit(RB_BUFFERS_DISABLED_BIT, p))
2972 r = sprintf(buf, "permanently disabled\n");
2973 else
2974 r = sprintf(buf, "%d\n", test_bit(RB_BUFFERS_ON_BIT, p));
Steven Rostedta3583242008-11-11 15:01:42 -05002975
2976 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2977}
2978
2979static ssize_t
2980rb_simple_write(struct file *filp, const char __user *ubuf,
2981 size_t cnt, loff_t *ppos)
2982{
Hannes Eder5e398412009-02-10 19:44:34 +01002983 unsigned long *p = filp->private_data;
Steven Rostedta3583242008-11-11 15:01:42 -05002984 char buf[64];
Hannes Eder5e398412009-02-10 19:44:34 +01002985 unsigned long val;
Steven Rostedta3583242008-11-11 15:01:42 -05002986 int ret;
2987
2988 if (cnt >= sizeof(buf))
2989 return -EINVAL;
2990
2991 if (copy_from_user(&buf, ubuf, cnt))
2992 return -EFAULT;
2993
2994 buf[cnt] = 0;
2995
2996 ret = strict_strtoul(buf, 10, &val);
2997 if (ret < 0)
2998 return ret;
2999
Steven Rostedt033601a2008-11-21 12:41:55 -05003000 if (val)
3001 set_bit(RB_BUFFERS_ON_BIT, p);
3002 else
3003 clear_bit(RB_BUFFERS_ON_BIT, p);
Steven Rostedta3583242008-11-11 15:01:42 -05003004
3005 (*ppos)++;
3006
3007 return cnt;
3008}
3009
Steven Rostedt5e2336a2009-03-05 21:44:55 -05003010static const struct file_operations rb_simple_fops = {
Steven Rostedta3583242008-11-11 15:01:42 -05003011 .open = tracing_open_generic,
3012 .read = rb_simple_read,
3013 .write = rb_simple_write,
3014};
3015
3016
3017static __init int rb_init_debugfs(void)
3018{
3019 struct dentry *d_tracer;
Steven Rostedta3583242008-11-11 15:01:42 -05003020
3021 d_tracer = tracing_init_dentry();
3022
Frederic Weisbecker5452af62009-03-27 00:25:38 +01003023 trace_create_file("tracing_on", 0644, d_tracer,
3024 &ring_buffer_flags, &rb_simple_fops);
Steven Rostedta3583242008-11-11 15:01:42 -05003025
3026 return 0;
3027}
3028
3029fs_initcall(rb_init_debugfs);
Steven Rostedt554f7862009-03-11 22:00:13 -04003030
Steven Rostedt59222ef2009-03-12 11:46:03 -04003031#ifdef CONFIG_HOTPLUG_CPU
Frederic Weisbecker09c9e842009-03-21 04:33:36 +01003032static int rb_cpu_notify(struct notifier_block *self,
3033 unsigned long action, void *hcpu)
Steven Rostedt554f7862009-03-11 22:00:13 -04003034{
3035 struct ring_buffer *buffer =
3036 container_of(self, struct ring_buffer, cpu_notify);
3037 long cpu = (long)hcpu;
3038
3039 switch (action) {
3040 case CPU_UP_PREPARE:
3041 case CPU_UP_PREPARE_FROZEN:
3042 if (cpu_isset(cpu, *buffer->cpumask))
3043 return NOTIFY_OK;
3044
3045 buffer->buffers[cpu] =
3046 rb_allocate_cpu_buffer(buffer, cpu);
3047 if (!buffer->buffers[cpu]) {
3048 WARN(1, "failed to allocate ring buffer on CPU %ld\n",
3049 cpu);
3050 return NOTIFY_OK;
3051 }
3052 smp_wmb();
3053 cpu_set(cpu, *buffer->cpumask);
3054 break;
3055 case CPU_DOWN_PREPARE:
3056 case CPU_DOWN_PREPARE_FROZEN:
3057 /*
3058 * Do nothing.
3059 * If we were to free the buffer, then the user would
3060 * lose any trace that was in the buffer.
3061 */
3062 break;
3063 default:
3064 break;
3065 }
3066 return NOTIFY_OK;
3067}
3068#endif