blob: 8295650444c5e939a647b83c7a8a8b5b1d946c47 [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>
Vegard Nossum1744a212009-02-28 08:29:44 +010013#include <linux/kmemcheck.h>
Steven Rostedt7a8e76a2008-09-29 23:02:38 -040014#include <linux/module.h>
15#include <linux/percpu.h>
16#include <linux/mutex.h>
Steven Rostedt7a8e76a2008-09-29 23:02:38 -040017#include <linux/init.h>
18#include <linux/hash.h>
19#include <linux/list.h>
Steven Rostedt554f7862009-03-11 22:00:13 -040020#include <linux/cpu.h>
Steven Rostedt7a8e76a2008-09-29 23:02:38 -040021#include <linux/fs.h>
22
Christoph Lameter79615762010-01-05 15:34:50 +090023#include <asm/local.h>
Steven Rostedt182e9f52008-11-03 23:15:56 -050024#include "trace.h"
25
Steven Rostedt033601a2008-11-21 12:41:55 -050026/*
Steven Rostedtd1b182a2009-04-15 16:53:47 -040027 * The ring buffer header is special. We must manually up keep it.
28 */
29int ring_buffer_print_entry_header(struct trace_seq *s)
30{
31 int ret;
32
Lai Jiangshan334d4162009-04-24 11:27:05 +080033 ret = trace_seq_printf(s, "# compressed entry header\n");
34 ret = trace_seq_printf(s, "\ttype_len : 5 bits\n");
Steven Rostedtd1b182a2009-04-15 16:53:47 -040035 ret = trace_seq_printf(s, "\ttime_delta : 27 bits\n");
36 ret = trace_seq_printf(s, "\tarray : 32 bits\n");
37 ret = trace_seq_printf(s, "\n");
38 ret = trace_seq_printf(s, "\tpadding : type == %d\n",
39 RINGBUF_TYPE_PADDING);
40 ret = trace_seq_printf(s, "\ttime_extend : type == %d\n",
41 RINGBUF_TYPE_TIME_EXTEND);
Lai Jiangshan334d4162009-04-24 11:27:05 +080042 ret = trace_seq_printf(s, "\tdata max type_len == %d\n",
43 RINGBUF_TYPE_DATA_TYPE_LEN_MAX);
Steven Rostedtd1b182a2009-04-15 16:53:47 -040044
45 return ret;
46}
47
48/*
Steven Rostedt5cc98542009-03-12 22:24:17 -040049 * The ring buffer is made up of a list of pages. A separate list of pages is
50 * allocated for each CPU. A writer may only write to a buffer that is
51 * associated with the CPU it is currently executing on. A reader may read
52 * from any per cpu buffer.
53 *
54 * The reader is special. For each per cpu buffer, the reader has its own
55 * reader page. When a reader has read the entire reader page, this reader
56 * page is swapped with another page in the ring buffer.
57 *
58 * Now, as long as the writer is off the reader page, the reader can do what
59 * ever it wants with that page. The writer will never write to that page
60 * again (as long as it is out of the ring buffer).
61 *
62 * Here's some silly ASCII art.
63 *
64 * +------+
65 * |reader| RING BUFFER
66 * |page |
67 * +------+ +---+ +---+ +---+
68 * | |-->| |-->| |
69 * +---+ +---+ +---+
70 * ^ |
71 * | |
72 * +---------------+
73 *
74 *
75 * +------+
76 * |reader| RING BUFFER
77 * |page |------------------v
78 * +------+ +---+ +---+ +---+
79 * | |-->| |-->| |
80 * +---+ +---+ +---+
81 * ^ |
82 * | |
83 * +---------------+
84 *
85 *
86 * +------+
87 * |reader| RING BUFFER
88 * |page |------------------v
89 * +------+ +---+ +---+ +---+
90 * ^ | |-->| |-->| |
91 * | +---+ +---+ +---+
92 * | |
93 * | |
94 * +------------------------------+
95 *
96 *
97 * +------+
98 * |buffer| RING BUFFER
99 * |page |------------------v
100 * +------+ +---+ +---+ +---+
101 * ^ | | | |-->| |
102 * | New +---+ +---+ +---+
103 * | Reader------^ |
104 * | page |
105 * +------------------------------+
106 *
107 *
108 * After we make this swap, the reader can hand this page off to the splice
109 * code and be done with it. It can even allocate a new page if it needs to
110 * and swap that into the ring buffer.
111 *
112 * We will be using cmpxchg soon to make all this lockless.
113 *
114 */
115
116/*
Steven Rostedt033601a2008-11-21 12:41:55 -0500117 * A fast way to enable or disable all ring buffers is to
118 * call tracing_on or tracing_off. Turning off the ring buffers
119 * prevents all ring buffers from being recorded to.
120 * Turning this switch on, makes it OK to write to the
121 * ring buffer, if the ring buffer is enabled itself.
122 *
123 * There's three layers that must be on in order to write
124 * to the ring buffer.
125 *
126 * 1) This global flag must be set.
127 * 2) The ring buffer must be enabled for recording.
128 * 3) The per cpu buffer must be enabled for recording.
129 *
130 * In case of an anomaly, this global flag has a bit set that
131 * will permantly disable all ring buffers.
132 */
133
134/*
135 * Global flag to disable all recording to ring buffers
136 * This has two bits: ON, DISABLED
137 *
138 * ON DISABLED
139 * ---- ----------
140 * 0 0 : ring buffers are off
141 * 1 0 : ring buffers are on
142 * X 1 : ring buffers are permanently disabled
143 */
144
145enum {
146 RB_BUFFERS_ON_BIT = 0,
147 RB_BUFFERS_DISABLED_BIT = 1,
148};
149
150enum {
151 RB_BUFFERS_ON = 1 << RB_BUFFERS_ON_BIT,
152 RB_BUFFERS_DISABLED = 1 << RB_BUFFERS_DISABLED_BIT,
153};
154
Hannes Eder5e398412009-02-10 19:44:34 +0100155static unsigned long ring_buffer_flags __read_mostly = RB_BUFFERS_ON;
Steven Rostedta3583242008-11-11 15:01:42 -0500156
Steven Rostedt474d32b2009-03-03 19:51:40 -0500157#define BUF_PAGE_HDR_SIZE offsetof(struct buffer_data_page, data)
158
Steven Rostedta3583242008-11-11 15:01:42 -0500159/**
160 * tracing_on - enable all tracing buffers
161 *
162 * This function enables all tracing buffers that may have been
163 * disabled with tracing_off.
164 */
165void tracing_on(void)
166{
Steven Rostedt033601a2008-11-21 12:41:55 -0500167 set_bit(RB_BUFFERS_ON_BIT, &ring_buffer_flags);
Steven Rostedta3583242008-11-11 15:01:42 -0500168}
Robert Richterc4f50182008-12-11 16:49:22 +0100169EXPORT_SYMBOL_GPL(tracing_on);
Steven Rostedta3583242008-11-11 15:01:42 -0500170
171/**
172 * tracing_off - turn off all tracing buffers
173 *
174 * This function stops all tracing buffers from recording data.
175 * It does not disable any overhead the tracers themselves may
176 * be causing. This function simply causes all recording to
177 * the ring buffers to fail.
178 */
179void tracing_off(void)
180{
Steven Rostedt033601a2008-11-21 12:41:55 -0500181 clear_bit(RB_BUFFERS_ON_BIT, &ring_buffer_flags);
182}
Robert Richterc4f50182008-12-11 16:49:22 +0100183EXPORT_SYMBOL_GPL(tracing_off);
Steven Rostedt033601a2008-11-21 12:41:55 -0500184
185/**
186 * tracing_off_permanent - permanently disable ring buffers
187 *
188 * This function, once called, will disable all ring buffers
Wenji Huangc3706f02009-02-10 01:03:18 -0500189 * permanently.
Steven Rostedt033601a2008-11-21 12:41:55 -0500190 */
191void tracing_off_permanent(void)
192{
193 set_bit(RB_BUFFERS_DISABLED_BIT, &ring_buffer_flags);
Steven Rostedta3583242008-11-11 15:01:42 -0500194}
195
Steven Rostedt988ae9d2009-02-14 19:17:02 -0500196/**
197 * tracing_is_on - show state of ring buffers enabled
198 */
199int tracing_is_on(void)
200{
201 return ring_buffer_flags == RB_BUFFERS_ON;
202}
203EXPORT_SYMBOL_GPL(tracing_is_on);
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
Lai Jiangshan334d4162009-04-24 11:27:05 +0800207#define RB_MAX_SMALL_DATA (RB_ALIGNMENT * RINGBUF_TYPE_DATA_TYPE_LEN_MAX)
Steven Rostedtc7b09302009-06-11 11:12:00 -0400208#define RB_EVNT_MIN_SIZE 8U /* two 32bit words */
Lai Jiangshan334d4162009-04-24 11:27:05 +0800209
Steven Rostedt22710482010-03-18 17:54:19 -0400210#if !defined(CONFIG_64BIT) || defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS)
211# define RB_FORCE_8BYTE_ALIGNMENT 0
212# define RB_ARCH_ALIGNMENT RB_ALIGNMENT
213#else
214# define RB_FORCE_8BYTE_ALIGNMENT 1
215# define RB_ARCH_ALIGNMENT 8U
216#endif
217
Lai Jiangshan334d4162009-04-24 11:27:05 +0800218/* define RINGBUF_TYPE_DATA for 'case RINGBUF_TYPE_DATA:' */
219#define RINGBUF_TYPE_DATA 0 ... RINGBUF_TYPE_DATA_TYPE_LEN_MAX
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400220
221enum {
222 RB_LEN_TIME_EXTEND = 8,
223 RB_LEN_TIME_STAMP = 16,
224};
225
Tom Zanussi2d622712009-03-22 03:30:49 -0500226static inline int rb_null_event(struct ring_buffer_event *event)
227{
Steven Rostedta1863c22009-09-03 10:23:58 -0400228 return event->type_len == RINGBUF_TYPE_PADDING && !event->time_delta;
Tom Zanussi2d622712009-03-22 03:30:49 -0500229}
230
231static void rb_event_set_padding(struct ring_buffer_event *event)
232{
Steven Rostedta1863c22009-09-03 10:23:58 -0400233 /* padding has a NULL time_delta */
Lai Jiangshan334d4162009-04-24 11:27:05 +0800234 event->type_len = RINGBUF_TYPE_PADDING;
Tom Zanussi2d622712009-03-22 03:30:49 -0500235 event->time_delta = 0;
236}
237
Tom Zanussi2d622712009-03-22 03:30:49 -0500238static unsigned
239rb_event_data_length(struct ring_buffer_event *event)
240{
241 unsigned length;
242
Lai Jiangshan334d4162009-04-24 11:27:05 +0800243 if (event->type_len)
244 length = event->type_len * RB_ALIGNMENT;
Tom Zanussi2d622712009-03-22 03:30:49 -0500245 else
246 length = event->array[0];
247 return length + RB_EVNT_HDR_SIZE;
248}
249
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400250/* inline for ring buffer fast paths */
Andrew Morton34a148b2009-01-09 12:27:09 -0800251static unsigned
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400252rb_event_length(struct ring_buffer_event *event)
253{
Lai Jiangshan334d4162009-04-24 11:27:05 +0800254 switch (event->type_len) {
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400255 case RINGBUF_TYPE_PADDING:
Tom Zanussi2d622712009-03-22 03:30:49 -0500256 if (rb_null_event(event))
257 /* undefined */
258 return -1;
Lai Jiangshan334d4162009-04-24 11:27:05 +0800259 return event->array[0] + RB_EVNT_HDR_SIZE;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400260
261 case RINGBUF_TYPE_TIME_EXTEND:
262 return RB_LEN_TIME_EXTEND;
263
264 case RINGBUF_TYPE_TIME_STAMP:
265 return RB_LEN_TIME_STAMP;
266
267 case RINGBUF_TYPE_DATA:
Tom Zanussi2d622712009-03-22 03:30:49 -0500268 return rb_event_data_length(event);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400269 default:
270 BUG();
271 }
272 /* not hit */
273 return 0;
274}
275
276/**
277 * ring_buffer_event_length - return the length of the event
278 * @event: the event to get the length of
279 */
280unsigned ring_buffer_event_length(struct ring_buffer_event *event)
281{
Robert Richter465634a2009-01-07 15:32:11 +0100282 unsigned length = rb_event_length(event);
Lai Jiangshan334d4162009-04-24 11:27:05 +0800283 if (event->type_len > RINGBUF_TYPE_DATA_TYPE_LEN_MAX)
Robert Richter465634a2009-01-07 15:32:11 +0100284 return length;
285 length -= RB_EVNT_HDR_SIZE;
286 if (length > RB_MAX_SMALL_DATA + sizeof(event->array[0]))
287 length -= sizeof(event->array[0]);
288 return length;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400289}
Robert Richterc4f50182008-12-11 16:49:22 +0100290EXPORT_SYMBOL_GPL(ring_buffer_event_length);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400291
292/* inline for ring buffer fast paths */
Andrew Morton34a148b2009-01-09 12:27:09 -0800293static void *
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400294rb_event_data(struct ring_buffer_event *event)
295{
Lai Jiangshan334d4162009-04-24 11:27:05 +0800296 BUG_ON(event->type_len > RINGBUF_TYPE_DATA_TYPE_LEN_MAX);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400297 /* If length is in len field, then array[0] has the data */
Lai Jiangshan334d4162009-04-24 11:27:05 +0800298 if (event->type_len)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400299 return (void *)&event->array[0];
300 /* Otherwise length is in array[0] and array[1] has the data */
301 return (void *)&event->array[1];
302}
303
304/**
305 * ring_buffer_event_data - return the data of the event
306 * @event: the event to get the data from
307 */
308void *ring_buffer_event_data(struct ring_buffer_event *event)
309{
310 return rb_event_data(event);
311}
Robert Richterc4f50182008-12-11 16:49:22 +0100312EXPORT_SYMBOL_GPL(ring_buffer_event_data);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400313
314#define for_each_buffer_cpu(buffer, cpu) \
Rusty Russell9e01c1b2009-01-01 10:12:22 +1030315 for_each_cpu(cpu, buffer->cpumask)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400316
317#define TS_SHIFT 27
318#define TS_MASK ((1ULL << TS_SHIFT) - 1)
319#define TS_DELTA_TEST (~TS_MASK)
320
Steven Rostedt66a8cb92010-03-31 13:21:56 -0400321/* Flag when events were overwritten */
322#define RB_MISSED_EVENTS (1 << 31)
323
Steven Rostedtabc9b562008-12-02 15:34:06 -0500324struct buffer_data_page {
Steven Rostedte4c2ce82008-10-01 11:14:54 -0400325 u64 time_stamp; /* page time stamp */
Wenji Huangc3706f02009-02-10 01:03:18 -0500326 local_t commit; /* write committed index */
Steven Rostedtabc9b562008-12-02 15:34:06 -0500327 unsigned char data[]; /* data of buffer page */
328};
329
Steven Rostedt77ae3652009-03-27 11:00:29 -0400330/*
331 * Note, the buffer_page list must be first. The buffer pages
332 * are allocated in cache lines, which means that each buffer
333 * page will be at the beginning of a cache line, and thus
334 * the least significant bits will be zero. We use this to
335 * add flags in the list struct pointers, to make the ring buffer
336 * lockless.
337 */
Steven Rostedtabc9b562008-12-02 15:34:06 -0500338struct buffer_page {
Steven Rostedt778c55d2009-05-01 18:44:45 -0400339 struct list_head list; /* list of buffer pages */
Steven Rostedtabc9b562008-12-02 15:34:06 -0500340 local_t write; /* index for next write */
Steven Rostedt6f807ac2008-10-04 02:00:58 -0400341 unsigned read; /* index for next read */
Steven Rostedt778c55d2009-05-01 18:44:45 -0400342 local_t entries; /* entries on this page */
Steven Rostedtabc9b562008-12-02 15:34:06 -0500343 struct buffer_data_page *page; /* Actual data page */
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400344};
345
Steven Rostedt77ae3652009-03-27 11:00:29 -0400346/*
347 * The buffer page counters, write and entries, must be reset
348 * atomically when crossing page boundaries. To synchronize this
349 * update, two counters are inserted into the number. One is
350 * the actual counter for the write position or count on the page.
351 *
352 * The other is a counter of updaters. Before an update happens
353 * the update partition of the counter is incremented. This will
354 * allow the updater to update the counter atomically.
355 *
356 * The counter is 20 bits, and the state data is 12.
357 */
358#define RB_WRITE_MASK 0xfffff
359#define RB_WRITE_INTCNT (1 << 20)
360
Steven Rostedt044fa782008-12-02 23:50:03 -0500361static void rb_init_page(struct buffer_data_page *bpage)
Steven Rostedtabc9b562008-12-02 15:34:06 -0500362{
Steven Rostedt044fa782008-12-02 23:50:03 -0500363 local_set(&bpage->commit, 0);
Steven Rostedtabc9b562008-12-02 15:34:06 -0500364}
365
Steven Rostedt474d32b2009-03-03 19:51:40 -0500366/**
367 * ring_buffer_page_len - the size of data on the page.
368 * @page: The page to read
369 *
370 * Returns the amount of data on the page, including buffer page header.
371 */
Steven Rostedtef7a4a12009-03-03 00:27:49 -0500372size_t ring_buffer_page_len(void *page)
373{
Steven Rostedt474d32b2009-03-03 19:51:40 -0500374 return local_read(&((struct buffer_data_page *)page)->commit)
375 + BUF_PAGE_HDR_SIZE;
Steven Rostedtef7a4a12009-03-03 00:27:49 -0500376}
377
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400378/*
Steven Rostedted568292008-09-29 23:02:40 -0400379 * Also stolen from mm/slob.c. Thanks to Mathieu Desnoyers for pointing
380 * this issue out.
381 */
Andrew Morton34a148b2009-01-09 12:27:09 -0800382static void free_buffer_page(struct buffer_page *bpage)
Steven Rostedted568292008-09-29 23:02:40 -0400383{
Andrew Morton34a148b2009-01-09 12:27:09 -0800384 free_page((unsigned long)bpage->page);
Steven Rostedte4c2ce82008-10-01 11:14:54 -0400385 kfree(bpage);
Steven Rostedted568292008-09-29 23:02:40 -0400386}
387
388/*
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400389 * We need to fit the time_stamp delta into 27 bits.
390 */
391static inline int test_time_stamp(u64 delta)
392{
393 if (delta & TS_DELTA_TEST)
394 return 1;
395 return 0;
396}
397
Steven Rostedt474d32b2009-03-03 19:51:40 -0500398#define BUF_PAGE_SIZE (PAGE_SIZE - BUF_PAGE_HDR_SIZE)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400399
Steven Rostedtbe957c42009-05-11 14:42:53 -0400400/* Max payload is BUF_PAGE_SIZE - header (8bytes) */
401#define BUF_MAX_DATA_SIZE (BUF_PAGE_SIZE - (sizeof(u32) * 2))
402
Steven Rostedtea05b572009-06-03 09:30:10 -0400403/* Max number of timestamps that can fit on a page */
404#define RB_TIMESTAMPS_PER_PAGE (BUF_PAGE_SIZE / RB_LEN_TIME_STAMP)
405
Steven Rostedtd1b182a2009-04-15 16:53:47 -0400406int ring_buffer_print_page_header(struct trace_seq *s)
407{
408 struct buffer_data_page field;
409 int ret;
410
411 ret = trace_seq_printf(s, "\tfield: u64 timestamp;\t"
Tom Zanussi26a50742009-10-06 01:09:50 -0500412 "offset:0;\tsize:%u;\tsigned:%u;\n",
413 (unsigned int)sizeof(field.time_stamp),
414 (unsigned int)is_signed_type(u64));
Steven Rostedtd1b182a2009-04-15 16:53:47 -0400415
416 ret = trace_seq_printf(s, "\tfield: local_t commit;\t"
Tom Zanussi26a50742009-10-06 01:09:50 -0500417 "offset:%u;\tsize:%u;\tsigned:%u;\n",
Steven Rostedtd1b182a2009-04-15 16:53:47 -0400418 (unsigned int)offsetof(typeof(field), commit),
Tom Zanussi26a50742009-10-06 01:09:50 -0500419 (unsigned int)sizeof(field.commit),
420 (unsigned int)is_signed_type(long));
Steven Rostedtd1b182a2009-04-15 16:53:47 -0400421
Steven Rostedt66a8cb92010-03-31 13:21:56 -0400422 ret = trace_seq_printf(s, "\tfield: int overwrite;\t"
423 "offset:%u;\tsize:%u;\tsigned:%u;\n",
424 (unsigned int)offsetof(typeof(field), commit),
425 1,
426 (unsigned int)is_signed_type(long));
427
Steven Rostedtd1b182a2009-04-15 16:53:47 -0400428 ret = trace_seq_printf(s, "\tfield: char data;\t"
Tom Zanussi26a50742009-10-06 01:09:50 -0500429 "offset:%u;\tsize:%u;\tsigned:%u;\n",
Steven Rostedtd1b182a2009-04-15 16:53:47 -0400430 (unsigned int)offsetof(typeof(field), data),
Tom Zanussi26a50742009-10-06 01:09:50 -0500431 (unsigned int)BUF_PAGE_SIZE,
432 (unsigned int)is_signed_type(char));
Steven Rostedtd1b182a2009-04-15 16:53:47 -0400433
434 return ret;
435}
436
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400437/*
438 * head_page == tail_page && head == tail then buffer is empty.
439 */
440struct ring_buffer_per_cpu {
441 int cpu;
442 struct ring_buffer *buffer;
Steven Rostedt77ae3652009-03-27 11:00:29 -0400443 spinlock_t reader_lock; /* serialize readers */
Thomas Gleixner445c8952009-12-02 19:49:50 +0100444 arch_spinlock_t lock;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400445 struct lock_class_key lock_key;
Steven Rostedt3adc54f2009-03-30 15:32:01 -0400446 struct list_head *pages;
Steven Rostedt6f807ac2008-10-04 02:00:58 -0400447 struct buffer_page *head_page; /* read from head */
448 struct buffer_page *tail_page; /* write to tail */
Wenji Huangc3706f02009-02-10 01:03:18 -0500449 struct buffer_page *commit_page; /* committed pages */
Steven Rostedtd7690412008-10-01 00:29:53 -0400450 struct buffer_page *reader_page;
Steven Rostedt66a8cb92010-03-31 13:21:56 -0400451 unsigned long lost_events;
452 unsigned long last_overrun;
Steven Rostedt77ae3652009-03-27 11:00:29 -0400453 local_t commit_overrun;
454 local_t overrun;
Steven Rostedte4906ef2009-04-30 20:49:44 -0400455 local_t entries;
Steven Rostedtfa743952009-06-16 12:37:57 -0400456 local_t committing;
457 local_t commits;
Steven Rostedt77ae3652009-03-27 11:00:29 -0400458 unsigned long read;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400459 u64 write_stamp;
460 u64 read_stamp;
461 atomic_t record_disabled;
462};
463
464struct ring_buffer {
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400465 unsigned pages;
466 unsigned flags;
467 int cpus;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400468 atomic_t record_disabled;
Arnaldo Carvalho de Melo00f62f62009-02-09 17:04:06 -0200469 cpumask_var_t cpumask;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400470
Peter Zijlstra1f8a6a12009-06-08 18:18:39 +0200471 struct lock_class_key *reader_lock_key;
472
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400473 struct mutex mutex;
474
475 struct ring_buffer_per_cpu **buffers;
Steven Rostedt554f7862009-03-11 22:00:13 -0400476
Steven Rostedt59222ef2009-03-12 11:46:03 -0400477#ifdef CONFIG_HOTPLUG_CPU
Steven Rostedt554f7862009-03-11 22:00:13 -0400478 struct notifier_block cpu_notify;
479#endif
Steven Rostedt37886f62009-03-17 17:22:06 -0400480 u64 (*clock)(void);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400481};
482
483struct ring_buffer_iter {
484 struct ring_buffer_per_cpu *cpu_buffer;
485 unsigned long head;
486 struct buffer_page *head_page;
Steven Rostedt492a74f2010-01-25 15:17:47 -0500487 struct buffer_page *cache_reader_page;
488 unsigned long cache_read;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400489 u64 read_stamp;
490};
491
Steven Rostedtf536aaf2008-11-10 23:07:30 -0500492/* buffer may be either ring_buffer or ring_buffer_per_cpu */
Steven Rostedt077c5402009-09-03 19:53:46 -0400493#define RB_WARN_ON(b, cond) \
494 ({ \
495 int _____ret = unlikely(cond); \
496 if (_____ret) { \
497 if (__same_type(*(b), struct ring_buffer_per_cpu)) { \
498 struct ring_buffer_per_cpu *__b = \
499 (void *)b; \
500 atomic_inc(&__b->buffer->record_disabled); \
501 } else \
502 atomic_inc(&b->record_disabled); \
503 WARN_ON(1); \
504 } \
505 _____ret; \
Steven Rostedt3e89c7b2008-11-11 15:28:41 -0500506 })
Steven Rostedtf536aaf2008-11-10 23:07:30 -0500507
Steven Rostedt37886f62009-03-17 17:22:06 -0400508/* Up this if you want to test the TIME_EXTENTS and normalization */
509#define DEBUG_SHIFT 0
510
Jiri Olsa6d3f1e12009-10-23 19:36:19 -0400511static inline u64 rb_time_stamp(struct ring_buffer *buffer)
Steven Rostedt88eb0122009-05-11 16:28:23 -0400512{
513 /* shift to debug/test normalization and TIME_EXTENTS */
514 return buffer->clock() << DEBUG_SHIFT;
515}
516
Steven Rostedt37886f62009-03-17 17:22:06 -0400517u64 ring_buffer_time_stamp(struct ring_buffer *buffer, int cpu)
518{
519 u64 time;
520
521 preempt_disable_notrace();
Jiri Olsa6d3f1e12009-10-23 19:36:19 -0400522 time = rb_time_stamp(buffer);
Steven Rostedt37886f62009-03-17 17:22:06 -0400523 preempt_enable_no_resched_notrace();
524
525 return time;
526}
527EXPORT_SYMBOL_GPL(ring_buffer_time_stamp);
528
529void ring_buffer_normalize_time_stamp(struct ring_buffer *buffer,
530 int cpu, u64 *ts)
531{
532 /* Just stupid testing the normalize function and deltas */
533 *ts >>= DEBUG_SHIFT;
534}
535EXPORT_SYMBOL_GPL(ring_buffer_normalize_time_stamp);
536
Steven Rostedt77ae3652009-03-27 11:00:29 -0400537/*
538 * Making the ring buffer lockless makes things tricky.
539 * Although writes only happen on the CPU that they are on,
540 * and they only need to worry about interrupts. Reads can
541 * happen on any CPU.
542 *
543 * The reader page is always off the ring buffer, but when the
544 * reader finishes with a page, it needs to swap its page with
545 * a new one from the buffer. The reader needs to take from
546 * the head (writes go to the tail). But if a writer is in overwrite
547 * mode and wraps, it must push the head page forward.
548 *
549 * Here lies the problem.
550 *
551 * The reader must be careful to replace only the head page, and
552 * not another one. As described at the top of the file in the
553 * ASCII art, the reader sets its old page to point to the next
554 * page after head. It then sets the page after head to point to
555 * the old reader page. But if the writer moves the head page
556 * during this operation, the reader could end up with the tail.
557 *
558 * We use cmpxchg to help prevent this race. We also do something
559 * special with the page before head. We set the LSB to 1.
560 *
561 * When the writer must push the page forward, it will clear the
562 * bit that points to the head page, move the head, and then set
563 * the bit that points to the new head page.
564 *
565 * We also don't want an interrupt coming in and moving the head
566 * page on another writer. Thus we use the second LSB to catch
567 * that too. Thus:
568 *
569 * head->list->prev->next bit 1 bit 0
570 * ------- -------
571 * Normal page 0 0
572 * Points to head page 0 1
573 * New head page 1 0
574 *
575 * Note we can not trust the prev pointer of the head page, because:
576 *
577 * +----+ +-----+ +-----+
578 * | |------>| T |---X--->| N |
579 * | |<------| | | |
580 * +----+ +-----+ +-----+
581 * ^ ^ |
582 * | +-----+ | |
583 * +----------| R |----------+ |
584 * | |<-----------+
585 * +-----+
586 *
587 * Key: ---X--> HEAD flag set in pointer
588 * T Tail page
589 * R Reader page
590 * N Next page
591 *
592 * (see __rb_reserve_next() to see where this happens)
593 *
594 * What the above shows is that the reader just swapped out
595 * the reader page with a page in the buffer, but before it
596 * could make the new header point back to the new page added
597 * it was preempted by a writer. The writer moved forward onto
598 * the new page added by the reader and is about to move forward
599 * again.
600 *
601 * You can see, it is legitimate for the previous pointer of
602 * the head (or any page) not to point back to itself. But only
603 * temporarially.
604 */
605
606#define RB_PAGE_NORMAL 0UL
607#define RB_PAGE_HEAD 1UL
608#define RB_PAGE_UPDATE 2UL
609
610
611#define RB_FLAG_MASK 3UL
612
613/* PAGE_MOVED is not part of the mask */
614#define RB_PAGE_MOVED 4UL
615
616/*
617 * rb_list_head - remove any bit
618 */
619static struct list_head *rb_list_head(struct list_head *list)
620{
621 unsigned long val = (unsigned long)list;
622
623 return (struct list_head *)(val & ~RB_FLAG_MASK);
624}
625
626/*
Jiri Olsa6d3f1e12009-10-23 19:36:19 -0400627 * rb_is_head_page - test if the given page is the head page
Steven Rostedt77ae3652009-03-27 11:00:29 -0400628 *
629 * Because the reader may move the head_page pointer, we can
630 * not trust what the head page is (it may be pointing to
631 * the reader page). But if the next page is a header page,
632 * its flags will be non zero.
633 */
634static int inline
635rb_is_head_page(struct ring_buffer_per_cpu *cpu_buffer,
636 struct buffer_page *page, struct list_head *list)
637{
638 unsigned long val;
639
640 val = (unsigned long)list->next;
641
642 if ((val & ~RB_FLAG_MASK) != (unsigned long)&page->list)
643 return RB_PAGE_MOVED;
644
645 return val & RB_FLAG_MASK;
646}
647
648/*
649 * rb_is_reader_page
650 *
651 * The unique thing about the reader page, is that, if the
652 * writer is ever on it, the previous pointer never points
653 * back to the reader page.
654 */
655static int rb_is_reader_page(struct buffer_page *page)
656{
657 struct list_head *list = page->list.prev;
658
659 return rb_list_head(list->next) != &page->list;
660}
661
662/*
663 * rb_set_list_to_head - set a list_head to be pointing to head.
664 */
665static void rb_set_list_to_head(struct ring_buffer_per_cpu *cpu_buffer,
666 struct list_head *list)
667{
668 unsigned long *ptr;
669
670 ptr = (unsigned long *)&list->next;
671 *ptr |= RB_PAGE_HEAD;
672 *ptr &= ~RB_PAGE_UPDATE;
673}
674
675/*
676 * rb_head_page_activate - sets up head page
677 */
678static void rb_head_page_activate(struct ring_buffer_per_cpu *cpu_buffer)
679{
680 struct buffer_page *head;
681
682 head = cpu_buffer->head_page;
683 if (!head)
684 return;
685
686 /*
687 * Set the previous list pointer to have the HEAD flag.
688 */
689 rb_set_list_to_head(cpu_buffer, head->list.prev);
690}
691
692static void rb_list_head_clear(struct list_head *list)
693{
694 unsigned long *ptr = (unsigned long *)&list->next;
695
696 *ptr &= ~RB_FLAG_MASK;
697}
698
699/*
700 * rb_head_page_dactivate - clears head page ptr (for free list)
701 */
702static void
703rb_head_page_deactivate(struct ring_buffer_per_cpu *cpu_buffer)
704{
705 struct list_head *hd;
706
707 /* Go through the whole list and clear any pointers found. */
708 rb_list_head_clear(cpu_buffer->pages);
709
710 list_for_each(hd, cpu_buffer->pages)
711 rb_list_head_clear(hd);
712}
713
714static int rb_head_page_set(struct ring_buffer_per_cpu *cpu_buffer,
715 struct buffer_page *head,
716 struct buffer_page *prev,
717 int old_flag, int new_flag)
718{
719 struct list_head *list;
720 unsigned long val = (unsigned long)&head->list;
721 unsigned long ret;
722
723 list = &prev->list;
724
725 val &= ~RB_FLAG_MASK;
726
Steven Rostedt08a40812009-09-14 09:31:35 -0400727 ret = cmpxchg((unsigned long *)&list->next,
728 val | old_flag, val | new_flag);
Steven Rostedt77ae3652009-03-27 11:00:29 -0400729
730 /* check if the reader took the page */
731 if ((ret & ~RB_FLAG_MASK) != val)
732 return RB_PAGE_MOVED;
733
734 return ret & RB_FLAG_MASK;
735}
736
737static int rb_head_page_set_update(struct ring_buffer_per_cpu *cpu_buffer,
738 struct buffer_page *head,
739 struct buffer_page *prev,
740 int old_flag)
741{
742 return rb_head_page_set(cpu_buffer, head, prev,
743 old_flag, RB_PAGE_UPDATE);
744}
745
746static int rb_head_page_set_head(struct ring_buffer_per_cpu *cpu_buffer,
747 struct buffer_page *head,
748 struct buffer_page *prev,
749 int old_flag)
750{
751 return rb_head_page_set(cpu_buffer, head, prev,
752 old_flag, RB_PAGE_HEAD);
753}
754
755static int rb_head_page_set_normal(struct ring_buffer_per_cpu *cpu_buffer,
756 struct buffer_page *head,
757 struct buffer_page *prev,
758 int old_flag)
759{
760 return rb_head_page_set(cpu_buffer, head, prev,
761 old_flag, RB_PAGE_NORMAL);
762}
763
764static inline void rb_inc_page(struct ring_buffer_per_cpu *cpu_buffer,
765 struct buffer_page **bpage)
766{
767 struct list_head *p = rb_list_head((*bpage)->list.next);
768
769 *bpage = list_entry(p, struct buffer_page, list);
770}
771
772static struct buffer_page *
773rb_set_head_page(struct ring_buffer_per_cpu *cpu_buffer)
774{
775 struct buffer_page *head;
776 struct buffer_page *page;
777 struct list_head *list;
778 int i;
779
780 if (RB_WARN_ON(cpu_buffer, !cpu_buffer->head_page))
781 return NULL;
782
783 /* sanity check */
784 list = cpu_buffer->pages;
785 if (RB_WARN_ON(cpu_buffer, rb_list_head(list->prev->next) != list))
786 return NULL;
787
788 page = head = cpu_buffer->head_page;
789 /*
790 * It is possible that the writer moves the header behind
791 * where we started, and we miss in one loop.
792 * A second loop should grab the header, but we'll do
793 * three loops just because I'm paranoid.
794 */
795 for (i = 0; i < 3; i++) {
796 do {
797 if (rb_is_head_page(cpu_buffer, page, page->list.prev)) {
798 cpu_buffer->head_page = page;
799 return page;
800 }
801 rb_inc_page(cpu_buffer, &page);
802 } while (page != head);
803 }
804
805 RB_WARN_ON(cpu_buffer, 1);
806
807 return NULL;
808}
809
810static int rb_head_page_replace(struct buffer_page *old,
811 struct buffer_page *new)
812{
813 unsigned long *ptr = (unsigned long *)&old->list.prev->next;
814 unsigned long val;
815 unsigned long ret;
816
817 val = *ptr & ~RB_FLAG_MASK;
818 val |= RB_PAGE_HEAD;
819
Steven Rostedt08a40812009-09-14 09:31:35 -0400820 ret = cmpxchg(ptr, val, (unsigned long)&new->list);
Steven Rostedt77ae3652009-03-27 11:00:29 -0400821
822 return ret == val;
823}
824
825/*
826 * rb_tail_page_update - move the tail page forward
827 *
828 * Returns 1 if moved tail page, 0 if someone else did.
829 */
830static int rb_tail_page_update(struct ring_buffer_per_cpu *cpu_buffer,
831 struct buffer_page *tail_page,
832 struct buffer_page *next_page)
833{
834 struct buffer_page *old_tail;
835 unsigned long old_entries;
836 unsigned long old_write;
837 int ret = 0;
838
839 /*
840 * The tail page now needs to be moved forward.
841 *
842 * We need to reset the tail page, but without messing
843 * with possible erasing of data brought in by interrupts
844 * that have moved the tail page and are currently on it.
845 *
846 * We add a counter to the write field to denote this.
847 */
848 old_write = local_add_return(RB_WRITE_INTCNT, &next_page->write);
849 old_entries = local_add_return(RB_WRITE_INTCNT, &next_page->entries);
850
851 /*
852 * Just make sure we have seen our old_write and synchronize
853 * with any interrupts that come in.
854 */
855 barrier();
856
857 /*
858 * If the tail page is still the same as what we think
859 * it is, then it is up to us to update the tail
860 * pointer.
861 */
862 if (tail_page == cpu_buffer->tail_page) {
863 /* Zero the write counter */
864 unsigned long val = old_write & ~RB_WRITE_MASK;
865 unsigned long eval = old_entries & ~RB_WRITE_MASK;
866
867 /*
868 * This will only succeed if an interrupt did
869 * not come in and change it. In which case, we
870 * do not want to modify it.
Lai Jiangshanda706d82009-07-15 16:27:30 +0800871 *
872 * We add (void) to let the compiler know that we do not care
873 * about the return value of these functions. We use the
874 * cmpxchg to only update if an interrupt did not already
875 * do it for us. If the cmpxchg fails, we don't care.
Steven Rostedt77ae3652009-03-27 11:00:29 -0400876 */
Lai Jiangshanda706d82009-07-15 16:27:30 +0800877 (void)local_cmpxchg(&next_page->write, old_write, val);
878 (void)local_cmpxchg(&next_page->entries, old_entries, eval);
Steven Rostedt77ae3652009-03-27 11:00:29 -0400879
880 /*
881 * No need to worry about races with clearing out the commit.
882 * it only can increment when a commit takes place. But that
883 * only happens in the outer most nested commit.
884 */
885 local_set(&next_page->page->commit, 0);
886
887 old_tail = cmpxchg(&cpu_buffer->tail_page,
888 tail_page, next_page);
889
890 if (old_tail == tail_page)
891 ret = 1;
892 }
893
894 return ret;
895}
896
897static int rb_check_bpage(struct ring_buffer_per_cpu *cpu_buffer,
898 struct buffer_page *bpage)
899{
900 unsigned long val = (unsigned long)bpage;
901
902 if (RB_WARN_ON(cpu_buffer, val & RB_FLAG_MASK))
903 return 1;
904
905 return 0;
906}
907
908/**
909 * rb_check_list - make sure a pointer to a list has the last bits zero
910 */
911static int rb_check_list(struct ring_buffer_per_cpu *cpu_buffer,
912 struct list_head *list)
913{
914 if (RB_WARN_ON(cpu_buffer, rb_list_head(list->prev) != list->prev))
915 return 1;
916 if (RB_WARN_ON(cpu_buffer, rb_list_head(list->next) != list->next))
917 return 1;
918 return 0;
919}
920
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400921/**
922 * check_pages - integrity check of buffer pages
923 * @cpu_buffer: CPU buffer with pages to test
924 *
Wenji Huangc3706f02009-02-10 01:03:18 -0500925 * As a safety measure we check to make sure the data pages have not
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400926 * been corrupted.
927 */
928static int rb_check_pages(struct ring_buffer_per_cpu *cpu_buffer)
929{
Steven Rostedt3adc54f2009-03-30 15:32:01 -0400930 struct list_head *head = cpu_buffer->pages;
Steven Rostedt044fa782008-12-02 23:50:03 -0500931 struct buffer_page *bpage, *tmp;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400932
Steven Rostedt77ae3652009-03-27 11:00:29 -0400933 rb_head_page_deactivate(cpu_buffer);
934
Steven Rostedt3e89c7b2008-11-11 15:28:41 -0500935 if (RB_WARN_ON(cpu_buffer, head->next->prev != head))
936 return -1;
937 if (RB_WARN_ON(cpu_buffer, head->prev->next != head))
938 return -1;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400939
Steven Rostedt77ae3652009-03-27 11:00:29 -0400940 if (rb_check_list(cpu_buffer, head))
941 return -1;
942
Steven Rostedt044fa782008-12-02 23:50:03 -0500943 list_for_each_entry_safe(bpage, tmp, head, list) {
Steven Rostedt3e89c7b2008-11-11 15:28:41 -0500944 if (RB_WARN_ON(cpu_buffer,
Steven Rostedt044fa782008-12-02 23:50:03 -0500945 bpage->list.next->prev != &bpage->list))
Steven Rostedt3e89c7b2008-11-11 15:28:41 -0500946 return -1;
947 if (RB_WARN_ON(cpu_buffer,
Steven Rostedt044fa782008-12-02 23:50:03 -0500948 bpage->list.prev->next != &bpage->list))
Steven Rostedt3e89c7b2008-11-11 15:28:41 -0500949 return -1;
Steven Rostedt77ae3652009-03-27 11:00:29 -0400950 if (rb_check_list(cpu_buffer, &bpage->list))
951 return -1;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400952 }
953
Steven Rostedt77ae3652009-03-27 11:00:29 -0400954 rb_head_page_activate(cpu_buffer);
955
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400956 return 0;
957}
958
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400959static int rb_allocate_pages(struct ring_buffer_per_cpu *cpu_buffer,
960 unsigned nr_pages)
961{
Steven Rostedt044fa782008-12-02 23:50:03 -0500962 struct buffer_page *bpage, *tmp;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400963 unsigned long addr;
964 LIST_HEAD(pages);
965 unsigned i;
966
Steven Rostedt3adc54f2009-03-30 15:32:01 -0400967 WARN_ON(!nr_pages);
968
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400969 for (i = 0; i < nr_pages; i++) {
Steven Rostedt044fa782008-12-02 23:50:03 -0500970 bpage = kzalloc_node(ALIGN(sizeof(*bpage), cache_line_size()),
Steven Rostedtaa1e0e32008-10-02 19:18:09 -0400971 GFP_KERNEL, cpu_to_node(cpu_buffer->cpu));
Steven Rostedt044fa782008-12-02 23:50:03 -0500972 if (!bpage)
Steven Rostedte4c2ce82008-10-01 11:14:54 -0400973 goto free_pages;
Steven Rostedt77ae3652009-03-27 11:00:29 -0400974
975 rb_check_bpage(cpu_buffer, bpage);
976
Steven Rostedt044fa782008-12-02 23:50:03 -0500977 list_add(&bpage->list, &pages);
Steven Rostedte4c2ce82008-10-01 11:14:54 -0400978
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400979 addr = __get_free_page(GFP_KERNEL);
980 if (!addr)
981 goto free_pages;
Steven Rostedt044fa782008-12-02 23:50:03 -0500982 bpage->page = (void *)addr;
983 rb_init_page(bpage->page);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400984 }
985
Steven Rostedt3adc54f2009-03-30 15:32:01 -0400986 /*
987 * The ring buffer page list is a circular list that does not
988 * start and end with a list head. All page list items point to
989 * other pages.
990 */
991 cpu_buffer->pages = pages.next;
992 list_del(&pages);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400993
994 rb_check_pages(cpu_buffer);
995
996 return 0;
997
998 free_pages:
Steven Rostedt044fa782008-12-02 23:50:03 -0500999 list_for_each_entry_safe(bpage, tmp, &pages, list) {
1000 list_del_init(&bpage->list);
1001 free_buffer_page(bpage);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001002 }
1003 return -ENOMEM;
1004}
1005
1006static struct ring_buffer_per_cpu *
1007rb_allocate_cpu_buffer(struct ring_buffer *buffer, int cpu)
1008{
1009 struct ring_buffer_per_cpu *cpu_buffer;
Steven Rostedt044fa782008-12-02 23:50:03 -05001010 struct buffer_page *bpage;
Steven Rostedtd7690412008-10-01 00:29:53 -04001011 unsigned long addr;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001012 int ret;
1013
1014 cpu_buffer = kzalloc_node(ALIGN(sizeof(*cpu_buffer), cache_line_size()),
1015 GFP_KERNEL, cpu_to_node(cpu));
1016 if (!cpu_buffer)
1017 return NULL;
1018
1019 cpu_buffer->cpu = cpu;
1020 cpu_buffer->buffer = buffer;
Steven Rostedtf83c9d02008-11-11 18:47:44 +01001021 spin_lock_init(&cpu_buffer->reader_lock);
Peter Zijlstra1f8a6a12009-06-08 18:18:39 +02001022 lockdep_set_class(&cpu_buffer->reader_lock, buffer->reader_lock_key);
Thomas Gleixneredc35bd2009-12-03 12:38:57 +01001023 cpu_buffer->lock = (arch_spinlock_t)__ARCH_SPIN_LOCK_UNLOCKED;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001024
Steven Rostedt044fa782008-12-02 23:50:03 -05001025 bpage = kzalloc_node(ALIGN(sizeof(*bpage), cache_line_size()),
Steven Rostedte4c2ce82008-10-01 11:14:54 -04001026 GFP_KERNEL, cpu_to_node(cpu));
Steven Rostedt044fa782008-12-02 23:50:03 -05001027 if (!bpage)
Steven Rostedte4c2ce82008-10-01 11:14:54 -04001028 goto fail_free_buffer;
1029
Steven Rostedt77ae3652009-03-27 11:00:29 -04001030 rb_check_bpage(cpu_buffer, bpage);
1031
Steven Rostedt044fa782008-12-02 23:50:03 -05001032 cpu_buffer->reader_page = bpage;
Steven Rostedtd7690412008-10-01 00:29:53 -04001033 addr = __get_free_page(GFP_KERNEL);
1034 if (!addr)
Steven Rostedte4c2ce82008-10-01 11:14:54 -04001035 goto fail_free_reader;
Steven Rostedt044fa782008-12-02 23:50:03 -05001036 bpage->page = (void *)addr;
1037 rb_init_page(bpage->page);
Steven Rostedte4c2ce82008-10-01 11:14:54 -04001038
Steven Rostedtd7690412008-10-01 00:29:53 -04001039 INIT_LIST_HEAD(&cpu_buffer->reader_page->list);
Steven Rostedtd7690412008-10-01 00:29:53 -04001040
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001041 ret = rb_allocate_pages(cpu_buffer, buffer->pages);
1042 if (ret < 0)
Steven Rostedtd7690412008-10-01 00:29:53 -04001043 goto fail_free_reader;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001044
1045 cpu_buffer->head_page
Steven Rostedt3adc54f2009-03-30 15:32:01 -04001046 = list_entry(cpu_buffer->pages, struct buffer_page, list);
Steven Rostedtbf41a152008-10-04 02:00:59 -04001047 cpu_buffer->tail_page = cpu_buffer->commit_page = cpu_buffer->head_page;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001048
Steven Rostedt77ae3652009-03-27 11:00:29 -04001049 rb_head_page_activate(cpu_buffer);
1050
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001051 return cpu_buffer;
1052
Steven Rostedtd7690412008-10-01 00:29:53 -04001053 fail_free_reader:
1054 free_buffer_page(cpu_buffer->reader_page);
1055
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001056 fail_free_buffer:
1057 kfree(cpu_buffer);
1058 return NULL;
1059}
1060
1061static void rb_free_cpu_buffer(struct ring_buffer_per_cpu *cpu_buffer)
1062{
Steven Rostedt3adc54f2009-03-30 15:32:01 -04001063 struct list_head *head = cpu_buffer->pages;
Steven Rostedt044fa782008-12-02 23:50:03 -05001064 struct buffer_page *bpage, *tmp;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001065
Steven Rostedtd7690412008-10-01 00:29:53 -04001066 free_buffer_page(cpu_buffer->reader_page);
1067
Steven Rostedt77ae3652009-03-27 11:00:29 -04001068 rb_head_page_deactivate(cpu_buffer);
1069
Steven Rostedt3adc54f2009-03-30 15:32:01 -04001070 if (head) {
1071 list_for_each_entry_safe(bpage, tmp, head, list) {
1072 list_del_init(&bpage->list);
1073 free_buffer_page(bpage);
1074 }
1075 bpage = list_entry(head, struct buffer_page, list);
Steven Rostedt044fa782008-12-02 23:50:03 -05001076 free_buffer_page(bpage);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001077 }
Steven Rostedt3adc54f2009-03-30 15:32:01 -04001078
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001079 kfree(cpu_buffer);
1080}
1081
Steven Rostedt59222ef2009-03-12 11:46:03 -04001082#ifdef CONFIG_HOTPLUG_CPU
Frederic Weisbecker09c9e842009-03-21 04:33:36 +01001083static int rb_cpu_notify(struct notifier_block *self,
1084 unsigned long action, void *hcpu);
Steven Rostedt554f7862009-03-11 22:00:13 -04001085#endif
1086
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001087/**
1088 * ring_buffer_alloc - allocate a new ring_buffer
Robert Richter68814b52008-11-24 12:24:12 +01001089 * @size: the size in bytes per cpu that is needed.
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001090 * @flags: attributes to set for the ring buffer.
1091 *
1092 * Currently the only flag that is available is the RB_FL_OVERWRITE
1093 * flag. This flag means that the buffer will overwrite old data
1094 * when the buffer wraps. If this flag is not set, the buffer will
1095 * drop data when the tail hits the head.
1096 */
Peter Zijlstra1f8a6a12009-06-08 18:18:39 +02001097struct ring_buffer *__ring_buffer_alloc(unsigned long size, unsigned flags,
1098 struct lock_class_key *key)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001099{
1100 struct ring_buffer *buffer;
1101 int bsize;
1102 int cpu;
1103
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001104 /* keep it in its own cache line */
1105 buffer = kzalloc(ALIGN(sizeof(*buffer), cache_line_size()),
1106 GFP_KERNEL);
1107 if (!buffer)
1108 return NULL;
1109
Rusty Russell9e01c1b2009-01-01 10:12:22 +10301110 if (!alloc_cpumask_var(&buffer->cpumask, GFP_KERNEL))
1111 goto fail_free_buffer;
1112
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001113 buffer->pages = DIV_ROUND_UP(size, BUF_PAGE_SIZE);
1114 buffer->flags = flags;
Steven Rostedt37886f62009-03-17 17:22:06 -04001115 buffer->clock = trace_clock_local;
Peter Zijlstra1f8a6a12009-06-08 18:18:39 +02001116 buffer->reader_lock_key = key;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001117
1118 /* need at least two pages */
Steven Rostedt5f78abe2009-06-17 14:11:10 -04001119 if (buffer->pages < 2)
1120 buffer->pages = 2;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001121
Frederic Weisbecker3bf832c2009-03-19 14:47:33 +01001122 /*
1123 * In case of non-hotplug cpu, if the ring-buffer is allocated
1124 * in early initcall, it will not be notified of secondary cpus.
1125 * In that off case, we need to allocate for all possible cpus.
1126 */
1127#ifdef CONFIG_HOTPLUG_CPU
Steven Rostedt554f7862009-03-11 22:00:13 -04001128 get_online_cpus();
1129 cpumask_copy(buffer->cpumask, cpu_online_mask);
Frederic Weisbecker3bf832c2009-03-19 14:47:33 +01001130#else
1131 cpumask_copy(buffer->cpumask, cpu_possible_mask);
1132#endif
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001133 buffer->cpus = nr_cpu_ids;
1134
1135 bsize = sizeof(void *) * nr_cpu_ids;
1136 buffer->buffers = kzalloc(ALIGN(bsize, cache_line_size()),
1137 GFP_KERNEL);
1138 if (!buffer->buffers)
Rusty Russell9e01c1b2009-01-01 10:12:22 +10301139 goto fail_free_cpumask;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001140
1141 for_each_buffer_cpu(buffer, cpu) {
1142 buffer->buffers[cpu] =
1143 rb_allocate_cpu_buffer(buffer, cpu);
1144 if (!buffer->buffers[cpu])
1145 goto fail_free_buffers;
1146 }
1147
Steven Rostedt59222ef2009-03-12 11:46:03 -04001148#ifdef CONFIG_HOTPLUG_CPU
Steven Rostedt554f7862009-03-11 22:00:13 -04001149 buffer->cpu_notify.notifier_call = rb_cpu_notify;
1150 buffer->cpu_notify.priority = 0;
1151 register_cpu_notifier(&buffer->cpu_notify);
1152#endif
1153
1154 put_online_cpus();
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001155 mutex_init(&buffer->mutex);
1156
1157 return buffer;
1158
1159 fail_free_buffers:
1160 for_each_buffer_cpu(buffer, cpu) {
1161 if (buffer->buffers[cpu])
1162 rb_free_cpu_buffer(buffer->buffers[cpu]);
1163 }
1164 kfree(buffer->buffers);
1165
Rusty Russell9e01c1b2009-01-01 10:12:22 +10301166 fail_free_cpumask:
1167 free_cpumask_var(buffer->cpumask);
Steven Rostedt554f7862009-03-11 22:00:13 -04001168 put_online_cpus();
Rusty Russell9e01c1b2009-01-01 10:12:22 +10301169
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001170 fail_free_buffer:
1171 kfree(buffer);
1172 return NULL;
1173}
Peter Zijlstra1f8a6a12009-06-08 18:18:39 +02001174EXPORT_SYMBOL_GPL(__ring_buffer_alloc);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001175
1176/**
1177 * ring_buffer_free - free a ring buffer.
1178 * @buffer: the buffer to free.
1179 */
1180void
1181ring_buffer_free(struct ring_buffer *buffer)
1182{
1183 int cpu;
1184
Steven Rostedt554f7862009-03-11 22:00:13 -04001185 get_online_cpus();
1186
Steven Rostedt59222ef2009-03-12 11:46:03 -04001187#ifdef CONFIG_HOTPLUG_CPU
Steven Rostedt554f7862009-03-11 22:00:13 -04001188 unregister_cpu_notifier(&buffer->cpu_notify);
1189#endif
1190
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001191 for_each_buffer_cpu(buffer, cpu)
1192 rb_free_cpu_buffer(buffer->buffers[cpu]);
1193
Steven Rostedt554f7862009-03-11 22:00:13 -04001194 put_online_cpus();
1195
Eric Dumazetbd3f0222009-08-07 12:49:29 +02001196 kfree(buffer->buffers);
Rusty Russell9e01c1b2009-01-01 10:12:22 +10301197 free_cpumask_var(buffer->cpumask);
1198
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001199 kfree(buffer);
1200}
Robert Richterc4f50182008-12-11 16:49:22 +01001201EXPORT_SYMBOL_GPL(ring_buffer_free);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001202
Steven Rostedt37886f62009-03-17 17:22:06 -04001203void ring_buffer_set_clock(struct ring_buffer *buffer,
1204 u64 (*clock)(void))
1205{
1206 buffer->clock = clock;
1207}
1208
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001209static void rb_reset_cpu(struct ring_buffer_per_cpu *cpu_buffer);
1210
1211static void
1212rb_remove_pages(struct ring_buffer_per_cpu *cpu_buffer, unsigned nr_pages)
1213{
Steven Rostedt044fa782008-12-02 23:50:03 -05001214 struct buffer_page *bpage;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001215 struct list_head *p;
1216 unsigned i;
1217
Lai Jiangshanf7112942009-11-03 19:42:45 +08001218 spin_lock_irq(&cpu_buffer->reader_lock);
Steven Rostedt77ae3652009-03-27 11:00:29 -04001219 rb_head_page_deactivate(cpu_buffer);
1220
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001221 for (i = 0; i < nr_pages; i++) {
Steven Rostedt3adc54f2009-03-30 15:32:01 -04001222 if (RB_WARN_ON(cpu_buffer, list_empty(cpu_buffer->pages)))
Steven Rostedt3e89c7b2008-11-11 15:28:41 -05001223 return;
Steven Rostedt3adc54f2009-03-30 15:32:01 -04001224 p = cpu_buffer->pages->next;
Steven Rostedt044fa782008-12-02 23:50:03 -05001225 bpage = list_entry(p, struct buffer_page, list);
1226 list_del_init(&bpage->list);
1227 free_buffer_page(bpage);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001228 }
Steven Rostedt3adc54f2009-03-30 15:32:01 -04001229 if (RB_WARN_ON(cpu_buffer, list_empty(cpu_buffer->pages)))
Steven Rostedt3e89c7b2008-11-11 15:28:41 -05001230 return;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001231
1232 rb_reset_cpu(cpu_buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001233 rb_check_pages(cpu_buffer);
1234
Steven Rostedtdd7f5942009-12-10 23:20:52 -05001235 spin_unlock_irq(&cpu_buffer->reader_lock);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001236}
1237
1238static void
1239rb_insert_pages(struct ring_buffer_per_cpu *cpu_buffer,
1240 struct list_head *pages, unsigned nr_pages)
1241{
Steven Rostedt044fa782008-12-02 23:50:03 -05001242 struct buffer_page *bpage;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001243 struct list_head *p;
1244 unsigned i;
1245
Steven Rostedt77ae3652009-03-27 11:00:29 -04001246 spin_lock_irq(&cpu_buffer->reader_lock);
1247 rb_head_page_deactivate(cpu_buffer);
1248
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001249 for (i = 0; i < nr_pages; i++) {
Steven Rostedt3e89c7b2008-11-11 15:28:41 -05001250 if (RB_WARN_ON(cpu_buffer, list_empty(pages)))
1251 return;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001252 p = pages->next;
Steven Rostedt044fa782008-12-02 23:50:03 -05001253 bpage = list_entry(p, struct buffer_page, list);
1254 list_del_init(&bpage->list);
Steven Rostedt3adc54f2009-03-30 15:32:01 -04001255 list_add_tail(&bpage->list, cpu_buffer->pages);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001256 }
1257 rb_reset_cpu(cpu_buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001258 rb_check_pages(cpu_buffer);
1259
Steven Rostedtdd7f5942009-12-10 23:20:52 -05001260 spin_unlock_irq(&cpu_buffer->reader_lock);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001261}
1262
1263/**
1264 * ring_buffer_resize - resize the ring buffer
1265 * @buffer: the buffer to resize.
1266 * @size: the new size.
1267 *
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001268 * Minimum size is 2 * BUF_PAGE_SIZE.
1269 *
1270 * Returns -1 on failure.
1271 */
1272int ring_buffer_resize(struct ring_buffer *buffer, unsigned long size)
1273{
1274 struct ring_buffer_per_cpu *cpu_buffer;
1275 unsigned nr_pages, rm_pages, new_pages;
Steven Rostedt044fa782008-12-02 23:50:03 -05001276 struct buffer_page *bpage, *tmp;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001277 unsigned long buffer_size;
1278 unsigned long addr;
1279 LIST_HEAD(pages);
1280 int i, cpu;
1281
Ingo Molnaree51a1d2008-11-13 14:58:31 +01001282 /*
1283 * Always succeed at resizing a non-existent buffer:
1284 */
1285 if (!buffer)
1286 return size;
1287
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001288 size = DIV_ROUND_UP(size, BUF_PAGE_SIZE);
1289 size *= BUF_PAGE_SIZE;
1290 buffer_size = buffer->pages * BUF_PAGE_SIZE;
1291
1292 /* we need a minimum of two pages */
1293 if (size < BUF_PAGE_SIZE * 2)
1294 size = BUF_PAGE_SIZE * 2;
1295
1296 if (size == buffer_size)
1297 return size;
1298
Steven Rostedt18421012009-12-10 22:54:27 -05001299 atomic_inc(&buffer->record_disabled);
1300
1301 /* Make sure all writers are done with this buffer. */
1302 synchronize_sched();
1303
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001304 mutex_lock(&buffer->mutex);
Steven Rostedt554f7862009-03-11 22:00:13 -04001305 get_online_cpus();
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001306
1307 nr_pages = DIV_ROUND_UP(size, BUF_PAGE_SIZE);
1308
1309 if (size < buffer_size) {
1310
1311 /* easy case, just free pages */
Steven Rostedt554f7862009-03-11 22:00:13 -04001312 if (RB_WARN_ON(buffer, nr_pages >= buffer->pages))
1313 goto out_fail;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001314
1315 rm_pages = buffer->pages - nr_pages;
1316
1317 for_each_buffer_cpu(buffer, cpu) {
1318 cpu_buffer = buffer->buffers[cpu];
1319 rb_remove_pages(cpu_buffer, rm_pages);
1320 }
1321 goto out;
1322 }
1323
1324 /*
1325 * This is a bit more difficult. We only want to add pages
1326 * when we can allocate enough for all CPUs. We do this
1327 * by allocating all the pages and storing them on a local
1328 * link list. If we succeed in our allocation, then we
1329 * add these pages to the cpu_buffers. Otherwise we just free
1330 * them all and return -ENOMEM;
1331 */
Steven Rostedt554f7862009-03-11 22:00:13 -04001332 if (RB_WARN_ON(buffer, nr_pages <= buffer->pages))
1333 goto out_fail;
Steven Rostedtf536aaf2008-11-10 23:07:30 -05001334
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001335 new_pages = nr_pages - buffer->pages;
1336
1337 for_each_buffer_cpu(buffer, cpu) {
1338 for (i = 0; i < new_pages; i++) {
Steven Rostedt044fa782008-12-02 23:50:03 -05001339 bpage = kzalloc_node(ALIGN(sizeof(*bpage),
Steven Rostedte4c2ce82008-10-01 11:14:54 -04001340 cache_line_size()),
1341 GFP_KERNEL, cpu_to_node(cpu));
Steven Rostedt044fa782008-12-02 23:50:03 -05001342 if (!bpage)
Steven Rostedte4c2ce82008-10-01 11:14:54 -04001343 goto free_pages;
Steven Rostedt044fa782008-12-02 23:50:03 -05001344 list_add(&bpage->list, &pages);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001345 addr = __get_free_page(GFP_KERNEL);
1346 if (!addr)
1347 goto free_pages;
Steven Rostedt044fa782008-12-02 23:50:03 -05001348 bpage->page = (void *)addr;
1349 rb_init_page(bpage->page);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001350 }
1351 }
1352
1353 for_each_buffer_cpu(buffer, cpu) {
1354 cpu_buffer = buffer->buffers[cpu];
1355 rb_insert_pages(cpu_buffer, &pages, new_pages);
1356 }
1357
Steven Rostedt554f7862009-03-11 22:00:13 -04001358 if (RB_WARN_ON(buffer, !list_empty(&pages)))
1359 goto out_fail;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001360
1361 out:
1362 buffer->pages = nr_pages;
Steven Rostedt554f7862009-03-11 22:00:13 -04001363 put_online_cpus();
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001364 mutex_unlock(&buffer->mutex);
1365
Steven Rostedt18421012009-12-10 22:54:27 -05001366 atomic_dec(&buffer->record_disabled);
1367
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001368 return size;
1369
1370 free_pages:
Steven Rostedt044fa782008-12-02 23:50:03 -05001371 list_for_each_entry_safe(bpage, tmp, &pages, list) {
1372 list_del_init(&bpage->list);
1373 free_buffer_page(bpage);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001374 }
Steven Rostedt554f7862009-03-11 22:00:13 -04001375 put_online_cpus();
Vegard Nossum641d2f62008-11-18 19:22:13 +01001376 mutex_unlock(&buffer->mutex);
Steven Rostedt18421012009-12-10 22:54:27 -05001377 atomic_dec(&buffer->record_disabled);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001378 return -ENOMEM;
Steven Rostedt554f7862009-03-11 22:00:13 -04001379
1380 /*
1381 * Something went totally wrong, and we are too paranoid
1382 * to even clean up the mess.
1383 */
1384 out_fail:
1385 put_online_cpus();
1386 mutex_unlock(&buffer->mutex);
Steven Rostedt18421012009-12-10 22:54:27 -05001387 atomic_dec(&buffer->record_disabled);
Steven Rostedt554f7862009-03-11 22:00:13 -04001388 return -1;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001389}
Robert Richterc4f50182008-12-11 16:49:22 +01001390EXPORT_SYMBOL_GPL(ring_buffer_resize);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001391
Steven Rostedt8789a9e2008-12-02 15:34:07 -05001392static inline void *
Steven Rostedt044fa782008-12-02 23:50:03 -05001393__rb_data_page_index(struct buffer_data_page *bpage, unsigned index)
Steven Rostedt8789a9e2008-12-02 15:34:07 -05001394{
Steven Rostedt044fa782008-12-02 23:50:03 -05001395 return bpage->data + index;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05001396}
1397
Steven Rostedt044fa782008-12-02 23:50:03 -05001398static inline void *__rb_page_index(struct buffer_page *bpage, unsigned index)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001399{
Steven Rostedt044fa782008-12-02 23:50:03 -05001400 return bpage->page->data + index;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001401}
1402
1403static inline struct ring_buffer_event *
Steven Rostedtd7690412008-10-01 00:29:53 -04001404rb_reader_event(struct ring_buffer_per_cpu *cpu_buffer)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001405{
Steven Rostedt6f807ac2008-10-04 02:00:58 -04001406 return __rb_page_index(cpu_buffer->reader_page,
1407 cpu_buffer->reader_page->read);
1408}
1409
1410static inline struct ring_buffer_event *
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001411rb_iter_head_event(struct ring_buffer_iter *iter)
1412{
Steven Rostedt6f807ac2008-10-04 02:00:58 -04001413 return __rb_page_index(iter->head_page, iter->head);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001414}
1415
Steven Rostedt77ae3652009-03-27 11:00:29 -04001416static inline unsigned long rb_page_write(struct buffer_page *bpage)
Steven Rostedtbf41a152008-10-04 02:00:59 -04001417{
Steven Rostedt77ae3652009-03-27 11:00:29 -04001418 return local_read(&bpage->write) & RB_WRITE_MASK;
Steven Rostedtbf41a152008-10-04 02:00:59 -04001419}
1420
1421static inline unsigned rb_page_commit(struct buffer_page *bpage)
1422{
Steven Rostedtabc9b562008-12-02 15:34:06 -05001423 return local_read(&bpage->page->commit);
Steven Rostedtbf41a152008-10-04 02:00:59 -04001424}
1425
Steven Rostedt77ae3652009-03-27 11:00:29 -04001426static inline unsigned long rb_page_entries(struct buffer_page *bpage)
1427{
1428 return local_read(&bpage->entries) & RB_WRITE_MASK;
1429}
1430
Steven Rostedtbf41a152008-10-04 02:00:59 -04001431/* Size is determined by what has been commited */
1432static inline unsigned rb_page_size(struct buffer_page *bpage)
1433{
1434 return rb_page_commit(bpage);
1435}
1436
1437static inline unsigned
1438rb_commit_index(struct ring_buffer_per_cpu *cpu_buffer)
1439{
1440 return rb_page_commit(cpu_buffer->commit_page);
1441}
1442
Steven Rostedtbf41a152008-10-04 02:00:59 -04001443static inline unsigned
1444rb_event_index(struct ring_buffer_event *event)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001445{
Steven Rostedtbf41a152008-10-04 02:00:59 -04001446 unsigned long addr = (unsigned long)event;
1447
Steven Rostedt22f470f2009-06-11 09:29:58 -04001448 return (addr & ~PAGE_MASK) - BUF_PAGE_HDR_SIZE;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001449}
1450
Steven Rostedt0f0c85f2009-05-11 16:08:00 -04001451static inline int
Steven Rostedtfa743952009-06-16 12:37:57 -04001452rb_event_is_commit(struct ring_buffer_per_cpu *cpu_buffer,
1453 struct ring_buffer_event *event)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001454{
Steven Rostedtbf41a152008-10-04 02:00:59 -04001455 unsigned long addr = (unsigned long)event;
1456 unsigned long index;
1457
1458 index = rb_event_index(event);
1459 addr &= PAGE_MASK;
1460
1461 return cpu_buffer->commit_page->page == (void *)addr &&
1462 rb_commit_index(cpu_buffer) == index;
1463}
1464
Andrew Morton34a148b2009-01-09 12:27:09 -08001465static void
Steven Rostedtbf41a152008-10-04 02:00:59 -04001466rb_set_commit_to_write(struct ring_buffer_per_cpu *cpu_buffer)
1467{
Steven Rostedt77ae3652009-03-27 11:00:29 -04001468 unsigned long max_count;
1469
Steven Rostedtbf41a152008-10-04 02:00:59 -04001470 /*
1471 * We only race with interrupts and NMIs on this CPU.
1472 * If we own the commit event, then we can commit
1473 * all others that interrupted us, since the interruptions
1474 * are in stack format (they finish before they come
1475 * back to us). This allows us to do a simple loop to
1476 * assign the commit to the tail.
1477 */
Steven Rostedta8ccf1d2008-12-23 11:32:24 -05001478 again:
Steven Rostedt77ae3652009-03-27 11:00:29 -04001479 max_count = cpu_buffer->buffer->pages * 100;
1480
Steven Rostedtbf41a152008-10-04 02:00:59 -04001481 while (cpu_buffer->commit_page != cpu_buffer->tail_page) {
Steven Rostedt77ae3652009-03-27 11:00:29 -04001482 if (RB_WARN_ON(cpu_buffer, !(--max_count)))
1483 return;
1484 if (RB_WARN_ON(cpu_buffer,
1485 rb_is_reader_page(cpu_buffer->tail_page)))
1486 return;
1487 local_set(&cpu_buffer->commit_page->page->commit,
1488 rb_page_write(cpu_buffer->commit_page));
Steven Rostedtbf41a152008-10-04 02:00:59 -04001489 rb_inc_page(cpu_buffer, &cpu_buffer->commit_page);
Steven Rostedtabc9b562008-12-02 15:34:06 -05001490 cpu_buffer->write_stamp =
1491 cpu_buffer->commit_page->page->time_stamp;
Steven Rostedtbf41a152008-10-04 02:00:59 -04001492 /* add barrier to keep gcc from optimizing too much */
1493 barrier();
1494 }
1495 while (rb_commit_index(cpu_buffer) !=
1496 rb_page_write(cpu_buffer->commit_page)) {
Steven Rostedt77ae3652009-03-27 11:00:29 -04001497
1498 local_set(&cpu_buffer->commit_page->page->commit,
1499 rb_page_write(cpu_buffer->commit_page));
1500 RB_WARN_ON(cpu_buffer,
1501 local_read(&cpu_buffer->commit_page->page->commit) &
1502 ~RB_WRITE_MASK);
Steven Rostedtbf41a152008-10-04 02:00:59 -04001503 barrier();
1504 }
Steven Rostedta8ccf1d2008-12-23 11:32:24 -05001505
1506 /* again, keep gcc from optimizing */
1507 barrier();
1508
1509 /*
1510 * If an interrupt came in just after the first while loop
1511 * and pushed the tail page forward, we will be left with
1512 * a dangling commit that will never go forward.
1513 */
1514 if (unlikely(cpu_buffer->commit_page != cpu_buffer->tail_page))
1515 goto again;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001516}
1517
Steven Rostedtd7690412008-10-01 00:29:53 -04001518static void rb_reset_reader_page(struct ring_buffer_per_cpu *cpu_buffer)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001519{
Steven Rostedtabc9b562008-12-02 15:34:06 -05001520 cpu_buffer->read_stamp = cpu_buffer->reader_page->page->time_stamp;
Steven Rostedt6f807ac2008-10-04 02:00:58 -04001521 cpu_buffer->reader_page->read = 0;
Steven Rostedtd7690412008-10-01 00:29:53 -04001522}
1523
Andrew Morton34a148b2009-01-09 12:27:09 -08001524static void rb_inc_iter(struct ring_buffer_iter *iter)
Steven Rostedtd7690412008-10-01 00:29:53 -04001525{
1526 struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
1527
1528 /*
1529 * The iterator could be on the reader page (it starts there).
1530 * But the head could have moved, since the reader was
1531 * found. Check for this case and assign the iterator
1532 * to the head page instead of next.
1533 */
1534 if (iter->head_page == cpu_buffer->reader_page)
Steven Rostedt77ae3652009-03-27 11:00:29 -04001535 iter->head_page = rb_set_head_page(cpu_buffer);
Steven Rostedtd7690412008-10-01 00:29:53 -04001536 else
1537 rb_inc_page(cpu_buffer, &iter->head_page);
1538
Steven Rostedtabc9b562008-12-02 15:34:06 -05001539 iter->read_stamp = iter->head_page->page->time_stamp;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001540 iter->head = 0;
1541}
1542
1543/**
1544 * ring_buffer_update_event - update event type and data
1545 * @event: the even to update
1546 * @type: the type of event
1547 * @length: the size of the event field in the ring buffer
1548 *
1549 * Update the type and data fields of the event. The length
1550 * is the actual size that is written to the ring buffer,
1551 * and with this, we can determine what to place into the
1552 * data field.
1553 */
Andrew Morton34a148b2009-01-09 12:27:09 -08001554static void
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001555rb_update_event(struct ring_buffer_event *event,
1556 unsigned type, unsigned length)
1557{
Lai Jiangshan334d4162009-04-24 11:27:05 +08001558 event->type_len = type;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001559
1560 switch (type) {
1561
1562 case RINGBUF_TYPE_PADDING:
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001563 case RINGBUF_TYPE_TIME_EXTEND:
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001564 case RINGBUF_TYPE_TIME_STAMP:
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001565 break;
1566
Lai Jiangshan334d4162009-04-24 11:27:05 +08001567 case 0:
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001568 length -= RB_EVNT_HDR_SIZE;
Steven Rostedt22710482010-03-18 17:54:19 -04001569 if (length > RB_MAX_SMALL_DATA || RB_FORCE_8BYTE_ALIGNMENT)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001570 event->array[0] = length;
Lai Jiangshan334d4162009-04-24 11:27:05 +08001571 else
1572 event->type_len = DIV_ROUND_UP(length, RB_ALIGNMENT);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001573 break;
1574 default:
1575 BUG();
1576 }
1577}
1578
Steven Rostedt77ae3652009-03-27 11:00:29 -04001579/*
1580 * rb_handle_head_page - writer hit the head page
1581 *
1582 * Returns: +1 to retry page
1583 * 0 to continue
1584 * -1 on error
1585 */
1586static int
1587rb_handle_head_page(struct ring_buffer_per_cpu *cpu_buffer,
1588 struct buffer_page *tail_page,
1589 struct buffer_page *next_page)
1590{
1591 struct buffer_page *new_head;
1592 int entries;
1593 int type;
1594 int ret;
1595
1596 entries = rb_page_entries(next_page);
1597
1598 /*
1599 * The hard part is here. We need to move the head
1600 * forward, and protect against both readers on
1601 * other CPUs and writers coming in via interrupts.
1602 */
1603 type = rb_head_page_set_update(cpu_buffer, next_page, tail_page,
1604 RB_PAGE_HEAD);
1605
1606 /*
1607 * type can be one of four:
1608 * NORMAL - an interrupt already moved it for us
1609 * HEAD - we are the first to get here.
1610 * UPDATE - we are the interrupt interrupting
1611 * a current move.
1612 * MOVED - a reader on another CPU moved the next
1613 * pointer to its reader page. Give up
1614 * and try again.
1615 */
1616
1617 switch (type) {
1618 case RB_PAGE_HEAD:
1619 /*
1620 * We changed the head to UPDATE, thus
1621 * it is our responsibility to update
1622 * the counters.
1623 */
1624 local_add(entries, &cpu_buffer->overrun);
1625
1626 /*
1627 * The entries will be zeroed out when we move the
1628 * tail page.
1629 */
1630
1631 /* still more to do */
1632 break;
1633
1634 case RB_PAGE_UPDATE:
1635 /*
1636 * This is an interrupt that interrupt the
1637 * previous update. Still more to do.
1638 */
1639 break;
1640 case RB_PAGE_NORMAL:
1641 /*
1642 * An interrupt came in before the update
1643 * and processed this for us.
1644 * Nothing left to do.
1645 */
1646 return 1;
1647 case RB_PAGE_MOVED:
1648 /*
1649 * The reader is on another CPU and just did
1650 * a swap with our next_page.
1651 * Try again.
1652 */
1653 return 1;
1654 default:
1655 RB_WARN_ON(cpu_buffer, 1); /* WTF??? */
1656 return -1;
1657 }
1658
1659 /*
1660 * Now that we are here, the old head pointer is
1661 * set to UPDATE. This will keep the reader from
1662 * swapping the head page with the reader page.
1663 * The reader (on another CPU) will spin till
1664 * we are finished.
1665 *
1666 * We just need to protect against interrupts
1667 * doing the job. We will set the next pointer
1668 * to HEAD. After that, we set the old pointer
1669 * to NORMAL, but only if it was HEAD before.
1670 * otherwise we are an interrupt, and only
1671 * want the outer most commit to reset it.
1672 */
1673 new_head = next_page;
1674 rb_inc_page(cpu_buffer, &new_head);
1675
1676 ret = rb_head_page_set_head(cpu_buffer, new_head, next_page,
1677 RB_PAGE_NORMAL);
1678
1679 /*
1680 * Valid returns are:
1681 * HEAD - an interrupt came in and already set it.
1682 * NORMAL - One of two things:
1683 * 1) We really set it.
1684 * 2) A bunch of interrupts came in and moved
1685 * the page forward again.
1686 */
1687 switch (ret) {
1688 case RB_PAGE_HEAD:
1689 case RB_PAGE_NORMAL:
1690 /* OK */
1691 break;
1692 default:
1693 RB_WARN_ON(cpu_buffer, 1);
1694 return -1;
1695 }
1696
1697 /*
1698 * It is possible that an interrupt came in,
1699 * set the head up, then more interrupts came in
1700 * and moved it again. When we get back here,
1701 * the page would have been set to NORMAL but we
1702 * just set it back to HEAD.
1703 *
1704 * How do you detect this? Well, if that happened
1705 * the tail page would have moved.
1706 */
1707 if (ret == RB_PAGE_NORMAL) {
1708 /*
1709 * If the tail had moved passed next, then we need
1710 * to reset the pointer.
1711 */
1712 if (cpu_buffer->tail_page != tail_page &&
1713 cpu_buffer->tail_page != next_page)
1714 rb_head_page_set_normal(cpu_buffer, new_head,
1715 next_page,
1716 RB_PAGE_HEAD);
1717 }
1718
1719 /*
1720 * If this was the outer most commit (the one that
1721 * changed the original pointer from HEAD to UPDATE),
1722 * then it is up to us to reset it to NORMAL.
1723 */
1724 if (type == RB_PAGE_HEAD) {
1725 ret = rb_head_page_set_normal(cpu_buffer, next_page,
1726 tail_page,
1727 RB_PAGE_UPDATE);
1728 if (RB_WARN_ON(cpu_buffer,
1729 ret != RB_PAGE_UPDATE))
1730 return -1;
1731 }
1732
1733 return 0;
1734}
1735
Andrew Morton34a148b2009-01-09 12:27:09 -08001736static unsigned rb_calculate_event_length(unsigned length)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001737{
1738 struct ring_buffer_event event; /* Used only for sizeof array */
1739
1740 /* zero length can cause confusions */
1741 if (!length)
1742 length = 1;
1743
Steven Rostedt22710482010-03-18 17:54:19 -04001744 if (length > RB_MAX_SMALL_DATA || RB_FORCE_8BYTE_ALIGNMENT)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001745 length += sizeof(event.array[0]);
1746
1747 length += RB_EVNT_HDR_SIZE;
Steven Rostedt22710482010-03-18 17:54:19 -04001748 length = ALIGN(length, RB_ARCH_ALIGNMENT);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001749
1750 return length;
1751}
1752
Steven Rostedtc7b09302009-06-11 11:12:00 -04001753static inline void
1754rb_reset_tail(struct ring_buffer_per_cpu *cpu_buffer,
1755 struct buffer_page *tail_page,
1756 unsigned long tail, unsigned long length)
1757{
1758 struct ring_buffer_event *event;
1759
1760 /*
1761 * Only the event that crossed the page boundary
1762 * must fill the old tail_page with padding.
1763 */
1764 if (tail >= BUF_PAGE_SIZE) {
1765 local_sub(length, &tail_page->write);
1766 return;
1767 }
1768
1769 event = __rb_page_index(tail_page, tail);
Linus Torvaldsb0b70652009-06-20 10:56:46 -07001770 kmemcheck_annotate_bitfield(event, bitfield);
Steven Rostedtc7b09302009-06-11 11:12:00 -04001771
1772 /*
1773 * If this event is bigger than the minimum size, then
1774 * we need to be careful that we don't subtract the
1775 * write counter enough to allow another writer to slip
1776 * in on this page.
1777 * We put in a discarded commit instead, to make sure
1778 * that this space is not used again.
1779 *
1780 * If we are less than the minimum size, we don't need to
1781 * worry about it.
1782 */
1783 if (tail > (BUF_PAGE_SIZE - RB_EVNT_MIN_SIZE)) {
1784 /* No room for any events */
1785
1786 /* Mark the rest of the page with padding */
1787 rb_event_set_padding(event);
1788
1789 /* Set the write back to the previous setting */
1790 local_sub(length, &tail_page->write);
1791 return;
1792 }
1793
1794 /* Put in a discarded event */
1795 event->array[0] = (BUF_PAGE_SIZE - tail) - RB_EVNT_HDR_SIZE;
1796 event->type_len = RINGBUF_TYPE_PADDING;
1797 /* time delta must be non zero */
1798 event->time_delta = 1;
Steven Rostedtc7b09302009-06-11 11:12:00 -04001799
1800 /* Set write to end of buffer */
1801 length = (tail + length) - BUF_PAGE_SIZE;
1802 local_sub(length, &tail_page->write);
1803}
Steven Rostedt6634ff22009-05-06 15:30:07 -04001804
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001805static struct ring_buffer_event *
Steven Rostedt6634ff22009-05-06 15:30:07 -04001806rb_move_tail(struct ring_buffer_per_cpu *cpu_buffer,
1807 unsigned long length, unsigned long tail,
Steven Rostedt6634ff22009-05-06 15:30:07 -04001808 struct buffer_page *tail_page, u64 *ts)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001809{
Steven Rostedt5a50e332009-11-17 08:43:01 -05001810 struct buffer_page *commit_page = cpu_buffer->commit_page;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001811 struct ring_buffer *buffer = cpu_buffer->buffer;
Steven Rostedt77ae3652009-03-27 11:00:29 -04001812 struct buffer_page *next_page;
1813 int ret;
Steven Rostedtaa20ae82009-05-05 21:16:11 -04001814
1815 next_page = tail_page;
1816
Steven Rostedtaa20ae82009-05-05 21:16:11 -04001817 rb_inc_page(cpu_buffer, &next_page);
1818
Steven Rostedtaa20ae82009-05-05 21:16:11 -04001819 /*
1820 * If for some reason, we had an interrupt storm that made
1821 * it all the way around the buffer, bail, and warn
1822 * about it.
1823 */
1824 if (unlikely(next_page == commit_page)) {
Steven Rostedt77ae3652009-03-27 11:00:29 -04001825 local_inc(&cpu_buffer->commit_overrun);
Steven Rostedtaa20ae82009-05-05 21:16:11 -04001826 goto out_reset;
1827 }
1828
Steven Rostedt77ae3652009-03-27 11:00:29 -04001829 /*
1830 * This is where the fun begins!
1831 *
1832 * We are fighting against races between a reader that
1833 * could be on another CPU trying to swap its reader
1834 * page with the buffer head.
1835 *
1836 * We are also fighting against interrupts coming in and
1837 * moving the head or tail on us as well.
1838 *
1839 * If the next page is the head page then we have filled
1840 * the buffer, unless the commit page is still on the
1841 * reader page.
1842 */
1843 if (rb_is_head_page(cpu_buffer, next_page, &tail_page->list)) {
Steven Rostedtaa20ae82009-05-05 21:16:11 -04001844
Steven Rostedt77ae3652009-03-27 11:00:29 -04001845 /*
1846 * If the commit is not on the reader page, then
1847 * move the header page.
1848 */
1849 if (!rb_is_reader_page(cpu_buffer->commit_page)) {
1850 /*
1851 * If we are not in overwrite mode,
1852 * this is easy, just stop here.
1853 */
1854 if (!(buffer->flags & RB_FL_OVERWRITE))
1855 goto out_reset;
Steven Rostedtaa20ae82009-05-05 21:16:11 -04001856
Steven Rostedt77ae3652009-03-27 11:00:29 -04001857 ret = rb_handle_head_page(cpu_buffer,
1858 tail_page,
1859 next_page);
1860 if (ret < 0)
1861 goto out_reset;
1862 if (ret)
1863 goto out_again;
1864 } else {
1865 /*
1866 * We need to be careful here too. The
1867 * commit page could still be on the reader
1868 * page. We could have a small buffer, and
1869 * have filled up the buffer with events
1870 * from interrupts and such, and wrapped.
1871 *
1872 * Note, if the tail page is also the on the
1873 * reader_page, we let it move out.
1874 */
1875 if (unlikely((cpu_buffer->commit_page !=
1876 cpu_buffer->tail_page) &&
1877 (cpu_buffer->commit_page ==
1878 cpu_buffer->reader_page))) {
1879 local_inc(&cpu_buffer->commit_overrun);
1880 goto out_reset;
1881 }
Steven Rostedtaa20ae82009-05-05 21:16:11 -04001882 }
1883 }
1884
Steven Rostedt77ae3652009-03-27 11:00:29 -04001885 ret = rb_tail_page_update(cpu_buffer, tail_page, next_page);
1886 if (ret) {
1887 /*
1888 * Nested commits always have zero deltas, so
1889 * just reread the time stamp
1890 */
Jiri Olsa6d3f1e12009-10-23 19:36:19 -04001891 *ts = rb_time_stamp(buffer);
Steven Rostedt77ae3652009-03-27 11:00:29 -04001892 next_page->page->time_stamp = *ts;
Steven Rostedtaa20ae82009-05-05 21:16:11 -04001893 }
1894
Steven Rostedt77ae3652009-03-27 11:00:29 -04001895 out_again:
Steven Rostedtaa20ae82009-05-05 21:16:11 -04001896
Steven Rostedt77ae3652009-03-27 11:00:29 -04001897 rb_reset_tail(cpu_buffer, tail_page, tail, length);
Steven Rostedtaa20ae82009-05-05 21:16:11 -04001898
1899 /* fail and let the caller try again */
1900 return ERR_PTR(-EAGAIN);
1901
Steven Rostedt45141d42009-02-12 13:19:48 -05001902 out_reset:
Lai Jiangshan6f3b3442009-01-12 11:06:18 +08001903 /* reset write */
Steven Rostedtc7b09302009-06-11 11:12:00 -04001904 rb_reset_tail(cpu_buffer, tail_page, tail, length);
Lai Jiangshan6f3b3442009-01-12 11:06:18 +08001905
Steven Rostedtbf41a152008-10-04 02:00:59 -04001906 return NULL;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001907}
1908
Steven Rostedt6634ff22009-05-06 15:30:07 -04001909static struct ring_buffer_event *
1910__rb_reserve_next(struct ring_buffer_per_cpu *cpu_buffer,
1911 unsigned type, unsigned long length, u64 *ts)
1912{
Steven Rostedt5a50e332009-11-17 08:43:01 -05001913 struct buffer_page *tail_page;
Steven Rostedt6634ff22009-05-06 15:30:07 -04001914 struct ring_buffer_event *event;
1915 unsigned long tail, write;
1916
Steven Rostedt6634ff22009-05-06 15:30:07 -04001917 tail_page = cpu_buffer->tail_page;
1918 write = local_add_return(length, &tail_page->write);
Steven Rostedt77ae3652009-03-27 11:00:29 -04001919
1920 /* set write to only the index of the write */
1921 write &= RB_WRITE_MASK;
Steven Rostedt6634ff22009-05-06 15:30:07 -04001922 tail = write - length;
1923
1924 /* See if we shot pass the end of this buffer page */
1925 if (write > BUF_PAGE_SIZE)
1926 return rb_move_tail(cpu_buffer, length, tail,
Steven Rostedt5a50e332009-11-17 08:43:01 -05001927 tail_page, ts);
Steven Rostedt6634ff22009-05-06 15:30:07 -04001928
1929 /* We reserved something on the buffer */
1930
Steven Rostedt6634ff22009-05-06 15:30:07 -04001931 event = __rb_page_index(tail_page, tail);
Vegard Nossum1744a212009-02-28 08:29:44 +01001932 kmemcheck_annotate_bitfield(event, bitfield);
Steven Rostedt6634ff22009-05-06 15:30:07 -04001933 rb_update_event(event, type, length);
1934
1935 /* The passed in type is zero for DATA */
1936 if (likely(!type))
1937 local_inc(&tail_page->entries);
1938
1939 /*
Steven Rostedtfa743952009-06-16 12:37:57 -04001940 * If this is the first commit on the page, then update
1941 * its timestamp.
Steven Rostedt6634ff22009-05-06 15:30:07 -04001942 */
Steven Rostedtfa743952009-06-16 12:37:57 -04001943 if (!tail)
1944 tail_page->page->time_stamp = *ts;
Steven Rostedt6634ff22009-05-06 15:30:07 -04001945
1946 return event;
1947}
1948
Steven Rostedtedd813bf2009-06-02 23:00:53 -04001949static inline int
1950rb_try_to_discard(struct ring_buffer_per_cpu *cpu_buffer,
1951 struct ring_buffer_event *event)
1952{
1953 unsigned long new_index, old_index;
1954 struct buffer_page *bpage;
1955 unsigned long index;
1956 unsigned long addr;
1957
1958 new_index = rb_event_index(event);
1959 old_index = new_index + rb_event_length(event);
1960 addr = (unsigned long)event;
1961 addr &= PAGE_MASK;
1962
1963 bpage = cpu_buffer->tail_page;
1964
1965 if (bpage->page == (void *)addr && rb_page_write(bpage) == old_index) {
Steven Rostedt77ae3652009-03-27 11:00:29 -04001966 unsigned long write_mask =
1967 local_read(&bpage->write) & ~RB_WRITE_MASK;
Steven Rostedtedd813bf2009-06-02 23:00:53 -04001968 /*
1969 * This is on the tail page. It is possible that
1970 * a write could come in and move the tail page
1971 * and write to the next page. That is fine
1972 * because we just shorten what is on this page.
1973 */
Steven Rostedt77ae3652009-03-27 11:00:29 -04001974 old_index += write_mask;
1975 new_index += write_mask;
Steven Rostedtedd813bf2009-06-02 23:00:53 -04001976 index = local_cmpxchg(&bpage->write, old_index, new_index);
1977 if (index == old_index)
1978 return 1;
1979 }
1980
1981 /* could not discard */
1982 return 0;
1983}
1984
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001985static int
1986rb_add_time_stamp(struct ring_buffer_per_cpu *cpu_buffer,
1987 u64 *ts, u64 *delta)
1988{
1989 struct ring_buffer_event *event;
1990 static int once;
Steven Rostedtbf41a152008-10-04 02:00:59 -04001991 int ret;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001992
1993 if (unlikely(*delta > (1ULL << 59) && !once++)) {
1994 printk(KERN_WARNING "Delta way too big! %llu"
1995 " ts=%llu write stamp = %llu\n",
Stephen Rothwelle2862c92008-10-27 17:43:28 +11001996 (unsigned long long)*delta,
1997 (unsigned long long)*ts,
1998 (unsigned long long)cpu_buffer->write_stamp);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001999 WARN_ON(1);
2000 }
2001
2002 /*
2003 * The delta is too big, we to add a
2004 * new timestamp.
2005 */
2006 event = __rb_reserve_next(cpu_buffer,
2007 RINGBUF_TYPE_TIME_EXTEND,
2008 RB_LEN_TIME_EXTEND,
2009 ts);
2010 if (!event)
Steven Rostedtbf41a152008-10-04 02:00:59 -04002011 return -EBUSY;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002012
Steven Rostedtbf41a152008-10-04 02:00:59 -04002013 if (PTR_ERR(event) == -EAGAIN)
2014 return -EAGAIN;
2015
2016 /* Only a commited time event can update the write stamp */
Steven Rostedtfa743952009-06-16 12:37:57 -04002017 if (rb_event_is_commit(cpu_buffer, event)) {
Steven Rostedtbf41a152008-10-04 02:00:59 -04002018 /*
Steven Rostedtfa743952009-06-16 12:37:57 -04002019 * If this is the first on the page, then it was
2020 * updated with the page itself. Try to discard it
2021 * and if we can't just make it zero.
Steven Rostedtbf41a152008-10-04 02:00:59 -04002022 */
2023 if (rb_event_index(event)) {
2024 event->time_delta = *delta & TS_MASK;
2025 event->array[0] = *delta >> TS_SHIFT;
2026 } else {
Steven Rostedtea05b572009-06-03 09:30:10 -04002027 /* try to discard, since we do not need this */
2028 if (!rb_try_to_discard(cpu_buffer, event)) {
2029 /* nope, just zero it */
2030 event->time_delta = 0;
2031 event->array[0] = 0;
2032 }
Steven Rostedtbf41a152008-10-04 02:00:59 -04002033 }
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002034 cpu_buffer->write_stamp = *ts;
Steven Rostedtbf41a152008-10-04 02:00:59 -04002035 /* let the caller know this was the commit */
2036 ret = 1;
2037 } else {
Steven Rostedtedd813bf2009-06-02 23:00:53 -04002038 /* Try to discard the event */
2039 if (!rb_try_to_discard(cpu_buffer, event)) {
2040 /* Darn, this is just wasted space */
2041 event->time_delta = 0;
2042 event->array[0] = 0;
Steven Rostedtedd813bf2009-06-02 23:00:53 -04002043 }
Steven Rostedtf57a8a12009-06-05 14:11:30 -04002044 ret = 0;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002045 }
2046
Steven Rostedtbf41a152008-10-04 02:00:59 -04002047 *delta = 0;
2048
2049 return ret;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002050}
2051
Steven Rostedtfa743952009-06-16 12:37:57 -04002052static void rb_start_commit(struct ring_buffer_per_cpu *cpu_buffer)
2053{
2054 local_inc(&cpu_buffer->committing);
2055 local_inc(&cpu_buffer->commits);
2056}
2057
2058static void rb_end_commit(struct ring_buffer_per_cpu *cpu_buffer)
2059{
2060 unsigned long commits;
2061
2062 if (RB_WARN_ON(cpu_buffer,
2063 !local_read(&cpu_buffer->committing)))
2064 return;
2065
2066 again:
2067 commits = local_read(&cpu_buffer->commits);
2068 /* synchronize with interrupts */
2069 barrier();
2070 if (local_read(&cpu_buffer->committing) == 1)
2071 rb_set_commit_to_write(cpu_buffer);
2072
2073 local_dec(&cpu_buffer->committing);
2074
2075 /* synchronize with interrupts */
2076 barrier();
2077
2078 /*
2079 * Need to account for interrupts coming in between the
2080 * updating of the commit page and the clearing of the
2081 * committing counter.
2082 */
2083 if (unlikely(local_read(&cpu_buffer->commits) != commits) &&
2084 !local_read(&cpu_buffer->committing)) {
2085 local_inc(&cpu_buffer->committing);
2086 goto again;
2087 }
2088}
2089
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002090static struct ring_buffer_event *
Steven Rostedt62f0b3e2009-09-04 14:11:34 -04002091rb_reserve_next_event(struct ring_buffer *buffer,
2092 struct ring_buffer_per_cpu *cpu_buffer,
Steven Rostedt1cd8d732009-05-11 14:08:09 -04002093 unsigned long length)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002094{
2095 struct ring_buffer_event *event;
Steven Rostedt168b6b12009-05-11 22:11:05 -04002096 u64 ts, delta = 0;
Steven Rostedtbf41a152008-10-04 02:00:59 -04002097 int commit = 0;
Steven Rostedt818e3dd2008-10-31 09:58:35 -04002098 int nr_loops = 0;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002099
Steven Rostedtfa743952009-06-16 12:37:57 -04002100 rb_start_commit(cpu_buffer);
2101
Steven Rostedt85bac322009-09-04 14:24:40 -04002102#ifdef CONFIG_RING_BUFFER_ALLOW_SWAP
Steven Rostedt62f0b3e2009-09-04 14:11:34 -04002103 /*
2104 * Due to the ability to swap a cpu buffer from a buffer
2105 * it is possible it was swapped before we committed.
2106 * (committing stops a swap). We check for it here and
2107 * if it happened, we have to fail the write.
2108 */
2109 barrier();
2110 if (unlikely(ACCESS_ONCE(cpu_buffer->buffer) != buffer)) {
2111 local_dec(&cpu_buffer->committing);
2112 local_dec(&cpu_buffer->commits);
2113 return NULL;
2114 }
Steven Rostedt85bac322009-09-04 14:24:40 -04002115#endif
Steven Rostedt62f0b3e2009-09-04 14:11:34 -04002116
Steven Rostedtbe957c42009-05-11 14:42:53 -04002117 length = rb_calculate_event_length(length);
Steven Rostedtbf41a152008-10-04 02:00:59 -04002118 again:
Steven Rostedt818e3dd2008-10-31 09:58:35 -04002119 /*
2120 * We allow for interrupts to reenter here and do a trace.
2121 * If one does, it will cause this original code to loop
2122 * back here. Even with heavy interrupts happening, this
2123 * should only happen a few times in a row. If this happens
2124 * 1000 times in a row, there must be either an interrupt
2125 * storm or we have something buggy.
2126 * Bail!
2127 */
Steven Rostedt3e89c7b2008-11-11 15:28:41 -05002128 if (RB_WARN_ON(cpu_buffer, ++nr_loops > 1000))
Steven Rostedtfa743952009-06-16 12:37:57 -04002129 goto out_fail;
Steven Rostedt818e3dd2008-10-31 09:58:35 -04002130
Jiri Olsa6d3f1e12009-10-23 19:36:19 -04002131 ts = rb_time_stamp(cpu_buffer->buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002132
Steven Rostedtbf41a152008-10-04 02:00:59 -04002133 /*
2134 * Only the first commit can update the timestamp.
2135 * Yes there is a race here. If an interrupt comes in
2136 * just after the conditional and it traces too, then it
2137 * will also check the deltas. More than one timestamp may
2138 * also be made. But only the entry that did the actual
2139 * commit will be something other than zero.
2140 */
Steven Rostedt0f0c85f2009-05-11 16:08:00 -04002141 if (likely(cpu_buffer->tail_page == cpu_buffer->commit_page &&
2142 rb_page_write(cpu_buffer->tail_page) ==
2143 rb_commit_index(cpu_buffer))) {
Steven Rostedt168b6b12009-05-11 22:11:05 -04002144 u64 diff;
Steven Rostedtbf41a152008-10-04 02:00:59 -04002145
Steven Rostedt168b6b12009-05-11 22:11:05 -04002146 diff = ts - cpu_buffer->write_stamp;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002147
Steven Rostedt168b6b12009-05-11 22:11:05 -04002148 /* make sure this diff is calculated here */
Steven Rostedtbf41a152008-10-04 02:00:59 -04002149 barrier();
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002150
Steven Rostedtbf41a152008-10-04 02:00:59 -04002151 /* Did the write stamp get updated already? */
2152 if (unlikely(ts < cpu_buffer->write_stamp))
Steven Rostedt168b6b12009-05-11 22:11:05 -04002153 goto get_event;
Steven Rostedtbf41a152008-10-04 02:00:59 -04002154
Steven Rostedt168b6b12009-05-11 22:11:05 -04002155 delta = diff;
2156 if (unlikely(test_time_stamp(delta))) {
Steven Rostedtbf41a152008-10-04 02:00:59 -04002157
2158 commit = rb_add_time_stamp(cpu_buffer, &ts, &delta);
Steven Rostedtbf41a152008-10-04 02:00:59 -04002159 if (commit == -EBUSY)
Steven Rostedtfa743952009-06-16 12:37:57 -04002160 goto out_fail;
Steven Rostedtbf41a152008-10-04 02:00:59 -04002161
2162 if (commit == -EAGAIN)
2163 goto again;
2164
2165 RB_WARN_ON(cpu_buffer, commit < 0);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002166 }
Steven Rostedt168b6b12009-05-11 22:11:05 -04002167 }
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002168
Steven Rostedt168b6b12009-05-11 22:11:05 -04002169 get_event:
Steven Rostedt1cd8d732009-05-11 14:08:09 -04002170 event = __rb_reserve_next(cpu_buffer, 0, length, &ts);
Steven Rostedt168b6b12009-05-11 22:11:05 -04002171 if (unlikely(PTR_ERR(event) == -EAGAIN))
Steven Rostedtbf41a152008-10-04 02:00:59 -04002172 goto again;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002173
Steven Rostedtfa743952009-06-16 12:37:57 -04002174 if (!event)
2175 goto out_fail;
Steven Rostedtbf41a152008-10-04 02:00:59 -04002176
Steven Rostedtfa743952009-06-16 12:37:57 -04002177 if (!rb_event_is_commit(cpu_buffer, event))
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002178 delta = 0;
2179
2180 event->time_delta = delta;
2181
2182 return event;
Steven Rostedtfa743952009-06-16 12:37:57 -04002183
2184 out_fail:
2185 rb_end_commit(cpu_buffer);
2186 return NULL;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002187}
2188
Paul Mundt1155de42009-06-25 14:30:12 +09002189#ifdef CONFIG_TRACING
2190
Steven Rostedtaa18efb2009-04-20 16:16:11 -04002191#define TRACE_RECURSIVE_DEPTH 16
Steven Rostedt261842b2009-04-16 21:41:52 -04002192
2193static int trace_recursive_lock(void)
2194{
Steven Rostedtaa18efb2009-04-20 16:16:11 -04002195 current->trace_recursion++;
Steven Rostedt261842b2009-04-16 21:41:52 -04002196
Steven Rostedtaa18efb2009-04-20 16:16:11 -04002197 if (likely(current->trace_recursion < TRACE_RECURSIVE_DEPTH))
2198 return 0;
Steven Rostedt261842b2009-04-16 21:41:52 -04002199
Steven Rostedtaa18efb2009-04-20 16:16:11 -04002200 /* Disable all tracing before we do anything else */
2201 tracing_off_permanent();
Frederic Weisbeckere057a5e2009-04-19 23:38:12 +02002202
Steven Rostedt7d7d2b82009-04-27 12:37:49 -04002203 printk_once(KERN_WARNING "Tracing recursion: depth[%ld]:"
Steven Rostedtaa18efb2009-04-20 16:16:11 -04002204 "HC[%lu]:SC[%lu]:NMI[%lu]\n",
2205 current->trace_recursion,
2206 hardirq_count() >> HARDIRQ_SHIFT,
2207 softirq_count() >> SOFTIRQ_SHIFT,
2208 in_nmi());
Frederic Weisbeckere057a5e2009-04-19 23:38:12 +02002209
Steven Rostedtaa18efb2009-04-20 16:16:11 -04002210 WARN_ON_ONCE(1);
2211 return -1;
Steven Rostedt261842b2009-04-16 21:41:52 -04002212}
2213
2214static void trace_recursive_unlock(void)
2215{
Steven Rostedtaa18efb2009-04-20 16:16:11 -04002216 WARN_ON_ONCE(!current->trace_recursion);
Steven Rostedt261842b2009-04-16 21:41:52 -04002217
Steven Rostedtaa18efb2009-04-20 16:16:11 -04002218 current->trace_recursion--;
Steven Rostedt261842b2009-04-16 21:41:52 -04002219}
2220
Paul Mundt1155de42009-06-25 14:30:12 +09002221#else
2222
2223#define trace_recursive_lock() (0)
2224#define trace_recursive_unlock() do { } while (0)
2225
2226#endif
2227
Steven Rostedtbf41a152008-10-04 02:00:59 -04002228static DEFINE_PER_CPU(int, rb_need_resched);
2229
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002230/**
2231 * ring_buffer_lock_reserve - reserve a part of the buffer
2232 * @buffer: the ring buffer to reserve from
2233 * @length: the length of the data to reserve (excluding event header)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002234 *
2235 * Returns a reseverd event on the ring buffer to copy directly to.
2236 * The user of this interface will need to get the body to write into
2237 * and can use the ring_buffer_event_data() interface.
2238 *
2239 * The length is the length of the data needed, not the event length
2240 * which also includes the event header.
2241 *
2242 * Must be paired with ring_buffer_unlock_commit, unless NULL is returned.
2243 * If NULL is returned, then nothing has been allocated or locked.
2244 */
2245struct ring_buffer_event *
Arnaldo Carvalho de Melo0a987752009-02-05 16:12:56 -02002246ring_buffer_lock_reserve(struct ring_buffer *buffer, unsigned long length)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002247{
2248 struct ring_buffer_per_cpu *cpu_buffer;
2249 struct ring_buffer_event *event;
Steven Rostedtbf41a152008-10-04 02:00:59 -04002250 int cpu, resched;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002251
Steven Rostedt033601a2008-11-21 12:41:55 -05002252 if (ring_buffer_flags != RB_BUFFERS_ON)
Steven Rostedta3583242008-11-11 15:01:42 -05002253 return NULL;
2254
Steven Rostedtbf41a152008-10-04 02:00:59 -04002255 /* If we are tracing schedule, we don't want to recurse */
Steven Rostedt182e9f52008-11-03 23:15:56 -05002256 resched = ftrace_preempt_disable();
Steven Rostedtbf41a152008-10-04 02:00:59 -04002257
Lai Jiangshan52fbe9c2010-03-08 14:50:43 +08002258 if (atomic_read(&buffer->record_disabled))
2259 goto out_nocheck;
2260
Steven Rostedt261842b2009-04-16 21:41:52 -04002261 if (trace_recursive_lock())
2262 goto out_nocheck;
2263
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002264 cpu = raw_smp_processor_id();
2265
Rusty Russell9e01c1b2009-01-01 10:12:22 +10302266 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedtd7690412008-10-01 00:29:53 -04002267 goto out;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002268
2269 cpu_buffer = buffer->buffers[cpu];
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002270
2271 if (atomic_read(&cpu_buffer->record_disabled))
Steven Rostedtd7690412008-10-01 00:29:53 -04002272 goto out;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002273
Steven Rostedtbe957c42009-05-11 14:42:53 -04002274 if (length > BUF_MAX_DATA_SIZE)
Steven Rostedtbf41a152008-10-04 02:00:59 -04002275 goto out;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002276
Steven Rostedt62f0b3e2009-09-04 14:11:34 -04002277 event = rb_reserve_next_event(buffer, cpu_buffer, length);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002278 if (!event)
Steven Rostedtd7690412008-10-01 00:29:53 -04002279 goto out;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002280
Steven Rostedtbf41a152008-10-04 02:00:59 -04002281 /*
2282 * Need to store resched state on this cpu.
2283 * Only the first needs to.
2284 */
2285
2286 if (preempt_count() == 1)
2287 per_cpu(rb_need_resched, cpu) = resched;
2288
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002289 return event;
2290
Steven Rostedtd7690412008-10-01 00:29:53 -04002291 out:
Steven Rostedt261842b2009-04-16 21:41:52 -04002292 trace_recursive_unlock();
2293
2294 out_nocheck:
Steven Rostedt182e9f52008-11-03 23:15:56 -05002295 ftrace_preempt_enable(resched);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002296 return NULL;
2297}
Robert Richterc4f50182008-12-11 16:49:22 +01002298EXPORT_SYMBOL_GPL(ring_buffer_lock_reserve);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002299
Steven Rostedta1863c22009-09-03 10:23:58 -04002300static void
2301rb_update_write_stamp(struct ring_buffer_per_cpu *cpu_buffer,
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002302 struct ring_buffer_event *event)
2303{
Steven Rostedtfa743952009-06-16 12:37:57 -04002304 /*
2305 * The event first in the commit queue updates the
2306 * time stamp.
2307 */
2308 if (rb_event_is_commit(cpu_buffer, event))
2309 cpu_buffer->write_stamp += event->time_delta;
Steven Rostedta1863c22009-09-03 10:23:58 -04002310}
Steven Rostedtbf41a152008-10-04 02:00:59 -04002311
Steven Rostedta1863c22009-09-03 10:23:58 -04002312static void rb_commit(struct ring_buffer_per_cpu *cpu_buffer,
2313 struct ring_buffer_event *event)
2314{
2315 local_inc(&cpu_buffer->entries);
2316 rb_update_write_stamp(cpu_buffer, event);
Steven Rostedtfa743952009-06-16 12:37:57 -04002317 rb_end_commit(cpu_buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002318}
2319
2320/**
2321 * ring_buffer_unlock_commit - commit a reserved
2322 * @buffer: The buffer to commit to
2323 * @event: The event pointer to commit.
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002324 *
2325 * This commits the data to the ring buffer, and releases any locks held.
2326 *
2327 * Must be paired with ring_buffer_lock_reserve.
2328 */
2329int ring_buffer_unlock_commit(struct ring_buffer *buffer,
Arnaldo Carvalho de Melo0a987752009-02-05 16:12:56 -02002330 struct ring_buffer_event *event)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002331{
2332 struct ring_buffer_per_cpu *cpu_buffer;
2333 int cpu = raw_smp_processor_id();
2334
2335 cpu_buffer = buffer->buffers[cpu];
2336
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002337 rb_commit(cpu_buffer, event);
2338
Steven Rostedt261842b2009-04-16 21:41:52 -04002339 trace_recursive_unlock();
2340
Steven Rostedtbf41a152008-10-04 02:00:59 -04002341 /*
2342 * Only the last preempt count needs to restore preemption.
2343 */
Steven Rostedt182e9f52008-11-03 23:15:56 -05002344 if (preempt_count() == 1)
2345 ftrace_preempt_enable(per_cpu(rb_need_resched, cpu));
2346 else
Steven Rostedtbf41a152008-10-04 02:00:59 -04002347 preempt_enable_no_resched_notrace();
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002348
2349 return 0;
2350}
Robert Richterc4f50182008-12-11 16:49:22 +01002351EXPORT_SYMBOL_GPL(ring_buffer_unlock_commit);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002352
Frederic Weisbeckerf3b9aae2009-04-19 23:39:33 +02002353static inline void rb_event_discard(struct ring_buffer_event *event)
2354{
Lai Jiangshan334d4162009-04-24 11:27:05 +08002355 /* array[0] holds the actual length for the discarded event */
2356 event->array[0] = rb_event_data_length(event) - RB_EVNT_HDR_SIZE;
2357 event->type_len = RINGBUF_TYPE_PADDING;
Frederic Weisbeckerf3b9aae2009-04-19 23:39:33 +02002358 /* time delta must be non zero */
2359 if (!event->time_delta)
2360 event->time_delta = 1;
2361}
2362
Steven Rostedta1863c22009-09-03 10:23:58 -04002363/*
2364 * Decrement the entries to the page that an event is on.
2365 * The event does not even need to exist, only the pointer
2366 * to the page it is on. This may only be called before the commit
2367 * takes place.
2368 */
2369static inline void
2370rb_decrement_entry(struct ring_buffer_per_cpu *cpu_buffer,
2371 struct ring_buffer_event *event)
2372{
2373 unsigned long addr = (unsigned long)event;
2374 struct buffer_page *bpage = cpu_buffer->commit_page;
2375 struct buffer_page *start;
2376
2377 addr &= PAGE_MASK;
2378
2379 /* Do the likely case first */
2380 if (likely(bpage->page == (void *)addr)) {
2381 local_dec(&bpage->entries);
2382 return;
2383 }
2384
2385 /*
2386 * Because the commit page may be on the reader page we
2387 * start with the next page and check the end loop there.
2388 */
2389 rb_inc_page(cpu_buffer, &bpage);
2390 start = bpage;
2391 do {
2392 if (bpage->page == (void *)addr) {
2393 local_dec(&bpage->entries);
2394 return;
2395 }
2396 rb_inc_page(cpu_buffer, &bpage);
2397 } while (bpage != start);
2398
2399 /* commit not part of this buffer?? */
2400 RB_WARN_ON(cpu_buffer, 1);
2401}
2402
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002403/**
Steven Rostedtfa1b47d2009-04-02 00:09:41 -04002404 * ring_buffer_commit_discard - discard an event that has not been committed
2405 * @buffer: the ring buffer
2406 * @event: non committed event to discard
2407 *
Steven Rostedtdc892f72009-09-03 15:33:41 -04002408 * Sometimes an event that is in the ring buffer needs to be ignored.
2409 * This function lets the user discard an event in the ring buffer
2410 * and then that event will not be read later.
2411 *
2412 * This function only works if it is called before the the item has been
2413 * committed. It will try to free the event from the ring buffer
Steven Rostedtfa1b47d2009-04-02 00:09:41 -04002414 * if another event has not been added behind it.
2415 *
2416 * If another event has been added behind it, it will set the event
2417 * up as discarded, and perform the commit.
2418 *
2419 * If this function is called, do not call ring_buffer_unlock_commit on
2420 * the event.
2421 */
2422void ring_buffer_discard_commit(struct ring_buffer *buffer,
2423 struct ring_buffer_event *event)
2424{
2425 struct ring_buffer_per_cpu *cpu_buffer;
Steven Rostedtfa1b47d2009-04-02 00:09:41 -04002426 int cpu;
2427
2428 /* The event is discarded regardless */
Frederic Weisbeckerf3b9aae2009-04-19 23:39:33 +02002429 rb_event_discard(event);
Steven Rostedtfa1b47d2009-04-02 00:09:41 -04002430
Steven Rostedtfa743952009-06-16 12:37:57 -04002431 cpu = smp_processor_id();
2432 cpu_buffer = buffer->buffers[cpu];
2433
Steven Rostedtfa1b47d2009-04-02 00:09:41 -04002434 /*
2435 * This must only be called if the event has not been
2436 * committed yet. Thus we can assume that preemption
2437 * is still disabled.
2438 */
Steven Rostedtfa743952009-06-16 12:37:57 -04002439 RB_WARN_ON(buffer, !local_read(&cpu_buffer->committing));
Steven Rostedtfa1b47d2009-04-02 00:09:41 -04002440
Steven Rostedta1863c22009-09-03 10:23:58 -04002441 rb_decrement_entry(cpu_buffer, event);
Steven Rostedt0f2541d2009-08-05 12:02:48 -04002442 if (rb_try_to_discard(cpu_buffer, event))
Steven Rostedtedd813bf2009-06-02 23:00:53 -04002443 goto out;
Steven Rostedtfa1b47d2009-04-02 00:09:41 -04002444
2445 /*
2446 * The commit is still visible by the reader, so we
Steven Rostedta1863c22009-09-03 10:23:58 -04002447 * must still update the timestamp.
Steven Rostedtfa1b47d2009-04-02 00:09:41 -04002448 */
Steven Rostedta1863c22009-09-03 10:23:58 -04002449 rb_update_write_stamp(cpu_buffer, event);
Steven Rostedtfa1b47d2009-04-02 00:09:41 -04002450 out:
Steven Rostedtfa743952009-06-16 12:37:57 -04002451 rb_end_commit(cpu_buffer);
Steven Rostedtfa1b47d2009-04-02 00:09:41 -04002452
Frederic Weisbeckerf3b9aae2009-04-19 23:39:33 +02002453 trace_recursive_unlock();
2454
Steven Rostedtfa1b47d2009-04-02 00:09:41 -04002455 /*
2456 * Only the last preempt count needs to restore preemption.
2457 */
2458 if (preempt_count() == 1)
2459 ftrace_preempt_enable(per_cpu(rb_need_resched, cpu));
2460 else
2461 preempt_enable_no_resched_notrace();
2462
2463}
2464EXPORT_SYMBOL_GPL(ring_buffer_discard_commit);
2465
2466/**
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002467 * ring_buffer_write - write data to the buffer without reserving
2468 * @buffer: The ring buffer to write to.
2469 * @length: The length of the data being written (excluding the event header)
2470 * @data: The data to write to the buffer.
2471 *
2472 * This is like ring_buffer_lock_reserve and ring_buffer_unlock_commit as
2473 * one function. If you already have the data to write to the buffer, it
2474 * may be easier to simply call this function.
2475 *
2476 * Note, like ring_buffer_lock_reserve, the length is the length of the data
2477 * and not the length of the event which would hold the header.
2478 */
2479int ring_buffer_write(struct ring_buffer *buffer,
2480 unsigned long length,
2481 void *data)
2482{
2483 struct ring_buffer_per_cpu *cpu_buffer;
2484 struct ring_buffer_event *event;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002485 void *body;
2486 int ret = -EBUSY;
Steven Rostedtbf41a152008-10-04 02:00:59 -04002487 int cpu, resched;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002488
Steven Rostedt033601a2008-11-21 12:41:55 -05002489 if (ring_buffer_flags != RB_BUFFERS_ON)
Steven Rostedta3583242008-11-11 15:01:42 -05002490 return -EBUSY;
2491
Steven Rostedt182e9f52008-11-03 23:15:56 -05002492 resched = ftrace_preempt_disable();
Steven Rostedtbf41a152008-10-04 02:00:59 -04002493
Lai Jiangshan52fbe9c2010-03-08 14:50:43 +08002494 if (atomic_read(&buffer->record_disabled))
2495 goto out;
2496
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002497 cpu = raw_smp_processor_id();
2498
Rusty Russell9e01c1b2009-01-01 10:12:22 +10302499 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedtd7690412008-10-01 00:29:53 -04002500 goto out;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002501
2502 cpu_buffer = buffer->buffers[cpu];
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002503
2504 if (atomic_read(&cpu_buffer->record_disabled))
2505 goto out;
2506
Steven Rostedtbe957c42009-05-11 14:42:53 -04002507 if (length > BUF_MAX_DATA_SIZE)
2508 goto out;
2509
Steven Rostedt62f0b3e2009-09-04 14:11:34 -04002510 event = rb_reserve_next_event(buffer, cpu_buffer, length);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002511 if (!event)
2512 goto out;
2513
2514 body = rb_event_data(event);
2515
2516 memcpy(body, data, length);
2517
2518 rb_commit(cpu_buffer, event);
2519
2520 ret = 0;
2521 out:
Steven Rostedt182e9f52008-11-03 23:15:56 -05002522 ftrace_preempt_enable(resched);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002523
2524 return ret;
2525}
Robert Richterc4f50182008-12-11 16:49:22 +01002526EXPORT_SYMBOL_GPL(ring_buffer_write);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002527
Andrew Morton34a148b2009-01-09 12:27:09 -08002528static int rb_per_cpu_empty(struct ring_buffer_per_cpu *cpu_buffer)
Steven Rostedtbf41a152008-10-04 02:00:59 -04002529{
2530 struct buffer_page *reader = cpu_buffer->reader_page;
Steven Rostedt77ae3652009-03-27 11:00:29 -04002531 struct buffer_page *head = rb_set_head_page(cpu_buffer);
Steven Rostedtbf41a152008-10-04 02:00:59 -04002532 struct buffer_page *commit = cpu_buffer->commit_page;
2533
Steven Rostedt77ae3652009-03-27 11:00:29 -04002534 /* In case of error, head will be NULL */
2535 if (unlikely(!head))
2536 return 1;
2537
Steven Rostedtbf41a152008-10-04 02:00:59 -04002538 return reader->read == rb_page_commit(reader) &&
2539 (commit == reader ||
2540 (commit == head &&
2541 head->read == rb_page_commit(commit)));
2542}
2543
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002544/**
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002545 * ring_buffer_record_disable - stop all writes into the buffer
2546 * @buffer: The ring buffer to stop writes to.
2547 *
2548 * This prevents all writes to the buffer. Any attempt to write
2549 * to the buffer after this will fail and return NULL.
2550 *
2551 * The caller should call synchronize_sched() after this.
2552 */
2553void ring_buffer_record_disable(struct ring_buffer *buffer)
2554{
2555 atomic_inc(&buffer->record_disabled);
2556}
Robert Richterc4f50182008-12-11 16:49:22 +01002557EXPORT_SYMBOL_GPL(ring_buffer_record_disable);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002558
2559/**
2560 * ring_buffer_record_enable - enable writes to the buffer
2561 * @buffer: The ring buffer to enable writes
2562 *
2563 * Note, multiple disables will need the same number of enables
Adam Buchbinderc41b20e2009-12-11 16:35:39 -05002564 * to truly enable the writing (much like preempt_disable).
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002565 */
2566void ring_buffer_record_enable(struct ring_buffer *buffer)
2567{
2568 atomic_dec(&buffer->record_disabled);
2569}
Robert Richterc4f50182008-12-11 16:49:22 +01002570EXPORT_SYMBOL_GPL(ring_buffer_record_enable);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002571
2572/**
2573 * ring_buffer_record_disable_cpu - stop all writes into the cpu_buffer
2574 * @buffer: The ring buffer to stop writes to.
2575 * @cpu: The CPU buffer to stop
2576 *
2577 * This prevents all writes to the buffer. Any attempt to write
2578 * to the buffer after this will fail and return NULL.
2579 *
2580 * The caller should call synchronize_sched() after this.
2581 */
2582void ring_buffer_record_disable_cpu(struct ring_buffer *buffer, int cpu)
2583{
2584 struct ring_buffer_per_cpu *cpu_buffer;
2585
Rusty Russell9e01c1b2009-01-01 10:12:22 +10302586 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedt8aabee52009-03-12 13:13:49 -04002587 return;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002588
2589 cpu_buffer = buffer->buffers[cpu];
2590 atomic_inc(&cpu_buffer->record_disabled);
2591}
Robert Richterc4f50182008-12-11 16:49:22 +01002592EXPORT_SYMBOL_GPL(ring_buffer_record_disable_cpu);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002593
2594/**
2595 * ring_buffer_record_enable_cpu - enable writes to the buffer
2596 * @buffer: The ring buffer to enable writes
2597 * @cpu: The CPU to enable.
2598 *
2599 * Note, multiple disables will need the same number of enables
Adam Buchbinderc41b20e2009-12-11 16:35:39 -05002600 * to truly enable the writing (much like preempt_disable).
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002601 */
2602void ring_buffer_record_enable_cpu(struct ring_buffer *buffer, int cpu)
2603{
2604 struct ring_buffer_per_cpu *cpu_buffer;
2605
Rusty Russell9e01c1b2009-01-01 10:12:22 +10302606 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedt8aabee52009-03-12 13:13:49 -04002607 return;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002608
2609 cpu_buffer = buffer->buffers[cpu];
2610 atomic_dec(&cpu_buffer->record_disabled);
2611}
Robert Richterc4f50182008-12-11 16:49:22 +01002612EXPORT_SYMBOL_GPL(ring_buffer_record_enable_cpu);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002613
2614/**
2615 * ring_buffer_entries_cpu - get the number of entries in a cpu buffer
2616 * @buffer: The ring buffer
2617 * @cpu: The per CPU buffer to get the entries from.
2618 */
2619unsigned long ring_buffer_entries_cpu(struct ring_buffer *buffer, int cpu)
2620{
2621 struct ring_buffer_per_cpu *cpu_buffer;
Steven Rostedt8aabee52009-03-12 13:13:49 -04002622 unsigned long ret;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002623
Rusty Russell9e01c1b2009-01-01 10:12:22 +10302624 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedt8aabee52009-03-12 13:13:49 -04002625 return 0;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002626
2627 cpu_buffer = buffer->buffers[cpu];
Steven Rostedt77ae3652009-03-27 11:00:29 -04002628 ret = (local_read(&cpu_buffer->entries) - local_read(&cpu_buffer->overrun))
Steven Rostedte4906ef2009-04-30 20:49:44 -04002629 - cpu_buffer->read;
Steven Rostedt554f7862009-03-11 22:00:13 -04002630
2631 return ret;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002632}
Robert Richterc4f50182008-12-11 16:49:22 +01002633EXPORT_SYMBOL_GPL(ring_buffer_entries_cpu);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002634
2635/**
2636 * ring_buffer_overrun_cpu - get the number of overruns in a cpu_buffer
2637 * @buffer: The ring buffer
2638 * @cpu: The per CPU buffer to get the number of overruns from
2639 */
2640unsigned long ring_buffer_overrun_cpu(struct ring_buffer *buffer, int cpu)
2641{
2642 struct ring_buffer_per_cpu *cpu_buffer;
Steven Rostedt8aabee52009-03-12 13:13:49 -04002643 unsigned long ret;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002644
Rusty Russell9e01c1b2009-01-01 10:12:22 +10302645 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedt8aabee52009-03-12 13:13:49 -04002646 return 0;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002647
2648 cpu_buffer = buffer->buffers[cpu];
Steven Rostedt77ae3652009-03-27 11:00:29 -04002649 ret = local_read(&cpu_buffer->overrun);
Steven Rostedt554f7862009-03-11 22:00:13 -04002650
2651 return ret;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002652}
Robert Richterc4f50182008-12-11 16:49:22 +01002653EXPORT_SYMBOL_GPL(ring_buffer_overrun_cpu);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002654
2655/**
Steven Rostedtf0d2c682009-04-29 13:43:37 -04002656 * ring_buffer_commit_overrun_cpu - get the number of overruns caused by commits
2657 * @buffer: The ring buffer
2658 * @cpu: The per CPU buffer to get the number of overruns from
2659 */
2660unsigned long
2661ring_buffer_commit_overrun_cpu(struct ring_buffer *buffer, int cpu)
2662{
2663 struct ring_buffer_per_cpu *cpu_buffer;
2664 unsigned long ret;
2665
2666 if (!cpumask_test_cpu(cpu, buffer->cpumask))
2667 return 0;
2668
2669 cpu_buffer = buffer->buffers[cpu];
Steven Rostedt77ae3652009-03-27 11:00:29 -04002670 ret = local_read(&cpu_buffer->commit_overrun);
Steven Rostedtf0d2c682009-04-29 13:43:37 -04002671
2672 return ret;
2673}
2674EXPORT_SYMBOL_GPL(ring_buffer_commit_overrun_cpu);
2675
2676/**
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002677 * ring_buffer_entries - get the number of entries in a buffer
2678 * @buffer: The ring buffer
2679 *
2680 * Returns the total number of entries in the ring buffer
2681 * (all CPU entries)
2682 */
2683unsigned long ring_buffer_entries(struct ring_buffer *buffer)
2684{
2685 struct ring_buffer_per_cpu *cpu_buffer;
2686 unsigned long entries = 0;
2687 int cpu;
2688
2689 /* if you care about this being correct, lock the buffer */
2690 for_each_buffer_cpu(buffer, cpu) {
2691 cpu_buffer = buffer->buffers[cpu];
Steven Rostedte4906ef2009-04-30 20:49:44 -04002692 entries += (local_read(&cpu_buffer->entries) -
Steven Rostedt77ae3652009-03-27 11:00:29 -04002693 local_read(&cpu_buffer->overrun)) - cpu_buffer->read;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002694 }
2695
2696 return entries;
2697}
Robert Richterc4f50182008-12-11 16:49:22 +01002698EXPORT_SYMBOL_GPL(ring_buffer_entries);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002699
2700/**
Jiri Olsa67b394f2009-10-23 19:36:18 -04002701 * ring_buffer_overruns - get the number of overruns in buffer
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002702 * @buffer: The ring buffer
2703 *
2704 * Returns the total number of overruns in the ring buffer
2705 * (all CPU entries)
2706 */
2707unsigned long ring_buffer_overruns(struct ring_buffer *buffer)
2708{
2709 struct ring_buffer_per_cpu *cpu_buffer;
2710 unsigned long overruns = 0;
2711 int cpu;
2712
2713 /* if you care about this being correct, lock the buffer */
2714 for_each_buffer_cpu(buffer, cpu) {
2715 cpu_buffer = buffer->buffers[cpu];
Steven Rostedt77ae3652009-03-27 11:00:29 -04002716 overruns += local_read(&cpu_buffer->overrun);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002717 }
2718
2719 return overruns;
2720}
Robert Richterc4f50182008-12-11 16:49:22 +01002721EXPORT_SYMBOL_GPL(ring_buffer_overruns);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002722
Steven Rostedt642edba2008-11-12 00:01:26 -05002723static void rb_iter_reset(struct ring_buffer_iter *iter)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002724{
2725 struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
2726
Steven Rostedtd7690412008-10-01 00:29:53 -04002727 /* Iterator usage is expected to have record disabled */
2728 if (list_empty(&cpu_buffer->reader_page->list)) {
Steven Rostedt77ae3652009-03-27 11:00:29 -04002729 iter->head_page = rb_set_head_page(cpu_buffer);
2730 if (unlikely(!iter->head_page))
2731 return;
2732 iter->head = iter->head_page->read;
Steven Rostedtd7690412008-10-01 00:29:53 -04002733 } else {
2734 iter->head_page = cpu_buffer->reader_page;
Steven Rostedt6f807ac2008-10-04 02:00:58 -04002735 iter->head = cpu_buffer->reader_page->read;
Steven Rostedtd7690412008-10-01 00:29:53 -04002736 }
2737 if (iter->head)
2738 iter->read_stamp = cpu_buffer->read_stamp;
2739 else
Steven Rostedtabc9b562008-12-02 15:34:06 -05002740 iter->read_stamp = iter->head_page->page->time_stamp;
Steven Rostedt492a74f2010-01-25 15:17:47 -05002741 iter->cache_reader_page = cpu_buffer->reader_page;
2742 iter->cache_read = cpu_buffer->read;
Steven Rostedt642edba2008-11-12 00:01:26 -05002743}
Steven Rostedtf83c9d02008-11-11 18:47:44 +01002744
Steven Rostedt642edba2008-11-12 00:01:26 -05002745/**
2746 * ring_buffer_iter_reset - reset an iterator
2747 * @iter: The iterator to reset
2748 *
2749 * Resets the iterator, so that it will start from the beginning
2750 * again.
2751 */
2752void ring_buffer_iter_reset(struct ring_buffer_iter *iter)
2753{
Steven Rostedt554f7862009-03-11 22:00:13 -04002754 struct ring_buffer_per_cpu *cpu_buffer;
Steven Rostedt642edba2008-11-12 00:01:26 -05002755 unsigned long flags;
2756
Steven Rostedt554f7862009-03-11 22:00:13 -04002757 if (!iter)
2758 return;
2759
2760 cpu_buffer = iter->cpu_buffer;
2761
Steven Rostedt642edba2008-11-12 00:01:26 -05002762 spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
2763 rb_iter_reset(iter);
Steven Rostedtf83c9d02008-11-11 18:47:44 +01002764 spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002765}
Robert Richterc4f50182008-12-11 16:49:22 +01002766EXPORT_SYMBOL_GPL(ring_buffer_iter_reset);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002767
2768/**
2769 * ring_buffer_iter_empty - check if an iterator has no more to read
2770 * @iter: The iterator to check
2771 */
2772int ring_buffer_iter_empty(struct ring_buffer_iter *iter)
2773{
2774 struct ring_buffer_per_cpu *cpu_buffer;
2775
2776 cpu_buffer = iter->cpu_buffer;
2777
Steven Rostedtbf41a152008-10-04 02:00:59 -04002778 return iter->head_page == cpu_buffer->commit_page &&
2779 iter->head == rb_commit_index(cpu_buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002780}
Robert Richterc4f50182008-12-11 16:49:22 +01002781EXPORT_SYMBOL_GPL(ring_buffer_iter_empty);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002782
2783static void
2784rb_update_read_stamp(struct ring_buffer_per_cpu *cpu_buffer,
2785 struct ring_buffer_event *event)
2786{
2787 u64 delta;
2788
Lai Jiangshan334d4162009-04-24 11:27:05 +08002789 switch (event->type_len) {
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002790 case RINGBUF_TYPE_PADDING:
2791 return;
2792
2793 case RINGBUF_TYPE_TIME_EXTEND:
2794 delta = event->array[0];
2795 delta <<= TS_SHIFT;
2796 delta += event->time_delta;
2797 cpu_buffer->read_stamp += delta;
2798 return;
2799
2800 case RINGBUF_TYPE_TIME_STAMP:
2801 /* FIXME: not implemented */
2802 return;
2803
2804 case RINGBUF_TYPE_DATA:
2805 cpu_buffer->read_stamp += event->time_delta;
2806 return;
2807
2808 default:
2809 BUG();
2810 }
2811 return;
2812}
2813
2814static void
2815rb_update_iter_read_stamp(struct ring_buffer_iter *iter,
2816 struct ring_buffer_event *event)
2817{
2818 u64 delta;
2819
Lai Jiangshan334d4162009-04-24 11:27:05 +08002820 switch (event->type_len) {
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002821 case RINGBUF_TYPE_PADDING:
2822 return;
2823
2824 case RINGBUF_TYPE_TIME_EXTEND:
2825 delta = event->array[0];
2826 delta <<= TS_SHIFT;
2827 delta += event->time_delta;
2828 iter->read_stamp += delta;
2829 return;
2830
2831 case RINGBUF_TYPE_TIME_STAMP:
2832 /* FIXME: not implemented */
2833 return;
2834
2835 case RINGBUF_TYPE_DATA:
2836 iter->read_stamp += event->time_delta;
2837 return;
2838
2839 default:
2840 BUG();
2841 }
2842 return;
2843}
2844
Steven Rostedtd7690412008-10-01 00:29:53 -04002845static struct buffer_page *
2846rb_get_reader_page(struct ring_buffer_per_cpu *cpu_buffer)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002847{
Steven Rostedtd7690412008-10-01 00:29:53 -04002848 struct buffer_page *reader = NULL;
Steven Rostedt66a8cb92010-03-31 13:21:56 -04002849 unsigned long overwrite;
Steven Rostedtd7690412008-10-01 00:29:53 -04002850 unsigned long flags;
Steven Rostedt818e3dd2008-10-31 09:58:35 -04002851 int nr_loops = 0;
Steven Rostedt77ae3652009-03-27 11:00:29 -04002852 int ret;
Steven Rostedtd7690412008-10-01 00:29:53 -04002853
Steven Rostedt3e03fb72008-11-06 00:09:43 -05002854 local_irq_save(flags);
Thomas Gleixner0199c4e2009-12-02 20:01:25 +01002855 arch_spin_lock(&cpu_buffer->lock);
Steven Rostedtd7690412008-10-01 00:29:53 -04002856
2857 again:
Steven Rostedt818e3dd2008-10-31 09:58:35 -04002858 /*
2859 * This should normally only loop twice. But because the
2860 * start of the reader inserts an empty page, it causes
2861 * a case where we will loop three times. There should be no
2862 * reason to loop four times (that I know of).
2863 */
Steven Rostedt3e89c7b2008-11-11 15:28:41 -05002864 if (RB_WARN_ON(cpu_buffer, ++nr_loops > 3)) {
Steven Rostedt818e3dd2008-10-31 09:58:35 -04002865 reader = NULL;
2866 goto out;
2867 }
2868
Steven Rostedtd7690412008-10-01 00:29:53 -04002869 reader = cpu_buffer->reader_page;
2870
2871 /* If there's more to read, return this page */
Steven Rostedtbf41a152008-10-04 02:00:59 -04002872 if (cpu_buffer->reader_page->read < rb_page_size(reader))
Steven Rostedtd7690412008-10-01 00:29:53 -04002873 goto out;
2874
2875 /* Never should we have an index greater than the size */
Steven Rostedt3e89c7b2008-11-11 15:28:41 -05002876 if (RB_WARN_ON(cpu_buffer,
2877 cpu_buffer->reader_page->read > rb_page_size(reader)))
2878 goto out;
Steven Rostedtd7690412008-10-01 00:29:53 -04002879
2880 /* check if we caught up to the tail */
2881 reader = NULL;
Steven Rostedtbf41a152008-10-04 02:00:59 -04002882 if (cpu_buffer->commit_page == cpu_buffer->reader_page)
Steven Rostedtd7690412008-10-01 00:29:53 -04002883 goto out;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002884
2885 /*
Steven Rostedtd7690412008-10-01 00:29:53 -04002886 * Reset the reader page to size zero.
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002887 */
Steven Rostedt77ae3652009-03-27 11:00:29 -04002888 local_set(&cpu_buffer->reader_page->write, 0);
2889 local_set(&cpu_buffer->reader_page->entries, 0);
2890 local_set(&cpu_buffer->reader_page->page->commit, 0);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002891
Steven Rostedt77ae3652009-03-27 11:00:29 -04002892 spin:
2893 /*
2894 * Splice the empty reader page into the list around the head.
2895 */
2896 reader = rb_set_head_page(cpu_buffer);
Steven Rostedt0e1ff5d2010-01-06 20:40:44 -05002897 cpu_buffer->reader_page->list.next = rb_list_head(reader->list.next);
Steven Rostedtd7690412008-10-01 00:29:53 -04002898 cpu_buffer->reader_page->list.prev = reader->list.prev;
Steven Rostedtbf41a152008-10-04 02:00:59 -04002899
Steven Rostedt3adc54f2009-03-30 15:32:01 -04002900 /*
2901 * cpu_buffer->pages just needs to point to the buffer, it
2902 * has no specific buffer page to point to. Lets move it out
2903 * of our way so we don't accidently swap it.
2904 */
2905 cpu_buffer->pages = reader->list.prev;
2906
Steven Rostedt77ae3652009-03-27 11:00:29 -04002907 /* The reader page will be pointing to the new head */
2908 rb_set_list_to_head(cpu_buffer, &cpu_buffer->reader_page->list);
Steven Rostedtd7690412008-10-01 00:29:53 -04002909
2910 /*
Steven Rostedt66a8cb92010-03-31 13:21:56 -04002911 * We want to make sure we read the overruns after we set up our
2912 * pointers to the next object. The writer side does a
2913 * cmpxchg to cross pages which acts as the mb on the writer
2914 * side. Note, the reader will constantly fail the swap
2915 * while the writer is updating the pointers, so this
2916 * guarantees that the overwrite recorded here is the one we
2917 * want to compare with the last_overrun.
2918 */
2919 smp_mb();
2920 overwrite = local_read(&(cpu_buffer->overrun));
2921
2922 /*
Steven Rostedt77ae3652009-03-27 11:00:29 -04002923 * Here's the tricky part.
2924 *
2925 * We need to move the pointer past the header page.
2926 * But we can only do that if a writer is not currently
2927 * moving it. The page before the header page has the
2928 * flag bit '1' set if it is pointing to the page we want.
2929 * but if the writer is in the process of moving it
2930 * than it will be '2' or already moved '0'.
Steven Rostedtd7690412008-10-01 00:29:53 -04002931 */
Steven Rostedtd7690412008-10-01 00:29:53 -04002932
Steven Rostedt77ae3652009-03-27 11:00:29 -04002933 ret = rb_head_page_replace(reader, cpu_buffer->reader_page);
2934
2935 /*
2936 * If we did not convert it, then we must try again.
2937 */
2938 if (!ret)
2939 goto spin;
2940
2941 /*
2942 * Yeah! We succeeded in replacing the page.
2943 *
2944 * Now make the new head point back to the reader page.
2945 */
David Sharp5ded3dc2010-01-06 17:12:07 -08002946 rb_list_head(reader->list.next)->prev = &cpu_buffer->reader_page->list;
Steven Rostedt77ae3652009-03-27 11:00:29 -04002947 rb_inc_page(cpu_buffer, &cpu_buffer->head_page);
Steven Rostedtd7690412008-10-01 00:29:53 -04002948
2949 /* Finally update the reader page to the new head */
2950 cpu_buffer->reader_page = reader;
2951 rb_reset_reader_page(cpu_buffer);
2952
Steven Rostedt66a8cb92010-03-31 13:21:56 -04002953 if (overwrite != cpu_buffer->last_overrun) {
2954 cpu_buffer->lost_events = overwrite - cpu_buffer->last_overrun;
2955 cpu_buffer->last_overrun = overwrite;
2956 }
2957
Steven Rostedtd7690412008-10-01 00:29:53 -04002958 goto again;
2959
2960 out:
Thomas Gleixner0199c4e2009-12-02 20:01:25 +01002961 arch_spin_unlock(&cpu_buffer->lock);
Steven Rostedt3e03fb72008-11-06 00:09:43 -05002962 local_irq_restore(flags);
Steven Rostedtd7690412008-10-01 00:29:53 -04002963
2964 return reader;
2965}
2966
2967static void rb_advance_reader(struct ring_buffer_per_cpu *cpu_buffer)
2968{
2969 struct ring_buffer_event *event;
2970 struct buffer_page *reader;
2971 unsigned length;
2972
2973 reader = rb_get_reader_page(cpu_buffer);
2974
2975 /* This function should not be called when buffer is empty */
Steven Rostedt3e89c7b2008-11-11 15:28:41 -05002976 if (RB_WARN_ON(cpu_buffer, !reader))
2977 return;
Steven Rostedtd7690412008-10-01 00:29:53 -04002978
2979 event = rb_reader_event(cpu_buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002980
Steven Rostedta1863c22009-09-03 10:23:58 -04002981 if (event->type_len <= RINGBUF_TYPE_DATA_TYPE_LEN_MAX)
Steven Rostedte4906ef2009-04-30 20:49:44 -04002982 cpu_buffer->read++;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002983
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002984 rb_update_read_stamp(cpu_buffer, event);
2985
Steven Rostedtd7690412008-10-01 00:29:53 -04002986 length = rb_event_length(event);
Steven Rostedt6f807ac2008-10-04 02:00:58 -04002987 cpu_buffer->reader_page->read += length;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002988}
2989
2990static void rb_advance_iter(struct ring_buffer_iter *iter)
2991{
2992 struct ring_buffer *buffer;
2993 struct ring_buffer_per_cpu *cpu_buffer;
2994 struct ring_buffer_event *event;
2995 unsigned length;
2996
2997 cpu_buffer = iter->cpu_buffer;
2998 buffer = cpu_buffer->buffer;
2999
3000 /*
3001 * Check if we are at the end of the buffer.
3002 */
Steven Rostedtbf41a152008-10-04 02:00:59 -04003003 if (iter->head >= rb_page_size(iter->head_page)) {
Steven Rostedtea05b572009-06-03 09:30:10 -04003004 /* discarded commits can make the page empty */
3005 if (iter->head_page == cpu_buffer->commit_page)
Steven Rostedt3e89c7b2008-11-11 15:28:41 -05003006 return;
Steven Rostedtd7690412008-10-01 00:29:53 -04003007 rb_inc_iter(iter);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003008 return;
3009 }
3010
3011 event = rb_iter_head_event(iter);
3012
3013 length = rb_event_length(event);
3014
3015 /*
3016 * This should not be called to advance the header if we are
3017 * at the tail of the buffer.
3018 */
Steven Rostedt3e89c7b2008-11-11 15:28:41 -05003019 if (RB_WARN_ON(cpu_buffer,
Steven Rostedtf536aaf2008-11-10 23:07:30 -05003020 (iter->head_page == cpu_buffer->commit_page) &&
Steven Rostedt3e89c7b2008-11-11 15:28:41 -05003021 (iter->head + length > rb_commit_index(cpu_buffer))))
3022 return;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003023
3024 rb_update_iter_read_stamp(iter, event);
3025
3026 iter->head += length;
3027
3028 /* check for end of page padding */
Steven Rostedtbf41a152008-10-04 02:00:59 -04003029 if ((iter->head >= rb_page_size(iter->head_page)) &&
3030 (iter->head_page != cpu_buffer->commit_page))
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003031 rb_advance_iter(iter);
3032}
3033
Steven Rostedt66a8cb92010-03-31 13:21:56 -04003034static int rb_lost_events(struct ring_buffer_per_cpu *cpu_buffer)
3035{
3036 return cpu_buffer->lost_events;
3037}
3038
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003039static struct ring_buffer_event *
Steven Rostedt66a8cb92010-03-31 13:21:56 -04003040rb_buffer_peek(struct ring_buffer_per_cpu *cpu_buffer, u64 *ts,
3041 unsigned long *lost_events)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003042{
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003043 struct ring_buffer_event *event;
Steven Rostedtd7690412008-10-01 00:29:53 -04003044 struct buffer_page *reader;
Steven Rostedt818e3dd2008-10-31 09:58:35 -04003045 int nr_loops = 0;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003046
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003047 again:
Steven Rostedt818e3dd2008-10-31 09:58:35 -04003048 /*
3049 * We repeat when a timestamp is encountered. It is possible
3050 * to get multiple timestamps from an interrupt entering just
Steven Rostedtea05b572009-06-03 09:30:10 -04003051 * as one timestamp is about to be written, or from discarded
3052 * commits. The most that we can have is the number on a single page.
Steven Rostedt818e3dd2008-10-31 09:58:35 -04003053 */
Steven Rostedtea05b572009-06-03 09:30:10 -04003054 if (RB_WARN_ON(cpu_buffer, ++nr_loops > RB_TIMESTAMPS_PER_PAGE))
Steven Rostedt818e3dd2008-10-31 09:58:35 -04003055 return NULL;
Steven Rostedt818e3dd2008-10-31 09:58:35 -04003056
Steven Rostedtd7690412008-10-01 00:29:53 -04003057 reader = rb_get_reader_page(cpu_buffer);
3058 if (!reader)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003059 return NULL;
3060
Steven Rostedtd7690412008-10-01 00:29:53 -04003061 event = rb_reader_event(cpu_buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003062
Lai Jiangshan334d4162009-04-24 11:27:05 +08003063 switch (event->type_len) {
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003064 case RINGBUF_TYPE_PADDING:
Tom Zanussi2d622712009-03-22 03:30:49 -05003065 if (rb_null_event(event))
3066 RB_WARN_ON(cpu_buffer, 1);
3067 /*
3068 * Because the writer could be discarding every
3069 * event it creates (which would probably be bad)
3070 * if we were to go back to "again" then we may never
3071 * catch up, and will trigger the warn on, or lock
3072 * the box. Return the padding, and we will release
3073 * the current locks, and try again.
3074 */
Tom Zanussi2d622712009-03-22 03:30:49 -05003075 return event;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003076
3077 case RINGBUF_TYPE_TIME_EXTEND:
3078 /* Internal data, OK to advance */
Steven Rostedtd7690412008-10-01 00:29:53 -04003079 rb_advance_reader(cpu_buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003080 goto again;
3081
3082 case RINGBUF_TYPE_TIME_STAMP:
3083 /* FIXME: not implemented */
Steven Rostedtd7690412008-10-01 00:29:53 -04003084 rb_advance_reader(cpu_buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003085 goto again;
3086
3087 case RINGBUF_TYPE_DATA:
3088 if (ts) {
3089 *ts = cpu_buffer->read_stamp + event->time_delta;
Robert Richterd8eeb2d2009-07-31 14:58:04 +02003090 ring_buffer_normalize_time_stamp(cpu_buffer->buffer,
Steven Rostedt37886f62009-03-17 17:22:06 -04003091 cpu_buffer->cpu, ts);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003092 }
Steven Rostedt66a8cb92010-03-31 13:21:56 -04003093 if (lost_events)
3094 *lost_events = rb_lost_events(cpu_buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003095 return event;
3096
3097 default:
3098 BUG();
3099 }
3100
3101 return NULL;
3102}
Robert Richterc4f50182008-12-11 16:49:22 +01003103EXPORT_SYMBOL_GPL(ring_buffer_peek);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003104
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003105static struct ring_buffer_event *
3106rb_iter_peek(struct ring_buffer_iter *iter, u64 *ts)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003107{
3108 struct ring_buffer *buffer;
3109 struct ring_buffer_per_cpu *cpu_buffer;
3110 struct ring_buffer_event *event;
Steven Rostedt818e3dd2008-10-31 09:58:35 -04003111 int nr_loops = 0;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003112
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003113 cpu_buffer = iter->cpu_buffer;
3114 buffer = cpu_buffer->buffer;
3115
Steven Rostedt492a74f2010-01-25 15:17:47 -05003116 /*
3117 * Check if someone performed a consuming read to
3118 * the buffer. A consuming read invalidates the iterator
3119 * and we need to reset the iterator in this case.
3120 */
3121 if (unlikely(iter->cache_read != cpu_buffer->read ||
3122 iter->cache_reader_page != cpu_buffer->reader_page))
3123 rb_iter_reset(iter);
3124
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003125 again:
Steven Rostedt3c05d742010-01-26 16:14:08 -05003126 if (ring_buffer_iter_empty(iter))
3127 return NULL;
3128
Steven Rostedt818e3dd2008-10-31 09:58:35 -04003129 /*
Steven Rostedtea05b572009-06-03 09:30:10 -04003130 * We repeat when a timestamp is encountered.
3131 * We can get multiple timestamps by nested interrupts or also
3132 * if filtering is on (discarding commits). Since discarding
3133 * commits can be frequent we can get a lot of timestamps.
3134 * But we limit them by not adding timestamps if they begin
3135 * at the start of a page.
Steven Rostedt818e3dd2008-10-31 09:58:35 -04003136 */
Steven Rostedtea05b572009-06-03 09:30:10 -04003137 if (RB_WARN_ON(cpu_buffer, ++nr_loops > RB_TIMESTAMPS_PER_PAGE))
Steven Rostedt818e3dd2008-10-31 09:58:35 -04003138 return NULL;
Steven Rostedt818e3dd2008-10-31 09:58:35 -04003139
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003140 if (rb_per_cpu_empty(cpu_buffer))
3141 return NULL;
3142
Steven Rostedt3c05d742010-01-26 16:14:08 -05003143 if (iter->head >= local_read(&iter->head_page->page->commit)) {
3144 rb_inc_iter(iter);
3145 goto again;
3146 }
3147
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003148 event = rb_iter_head_event(iter);
3149
Lai Jiangshan334d4162009-04-24 11:27:05 +08003150 switch (event->type_len) {
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003151 case RINGBUF_TYPE_PADDING:
Tom Zanussi2d622712009-03-22 03:30:49 -05003152 if (rb_null_event(event)) {
3153 rb_inc_iter(iter);
3154 goto again;
3155 }
3156 rb_advance_iter(iter);
3157 return event;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003158
3159 case RINGBUF_TYPE_TIME_EXTEND:
3160 /* Internal data, OK to advance */
3161 rb_advance_iter(iter);
3162 goto again;
3163
3164 case RINGBUF_TYPE_TIME_STAMP:
3165 /* FIXME: not implemented */
3166 rb_advance_iter(iter);
3167 goto again;
3168
3169 case RINGBUF_TYPE_DATA:
3170 if (ts) {
3171 *ts = iter->read_stamp + event->time_delta;
Steven Rostedt37886f62009-03-17 17:22:06 -04003172 ring_buffer_normalize_time_stamp(buffer,
3173 cpu_buffer->cpu, ts);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003174 }
3175 return event;
3176
3177 default:
3178 BUG();
3179 }
3180
3181 return NULL;
3182}
Robert Richterc4f50182008-12-11 16:49:22 +01003183EXPORT_SYMBOL_GPL(ring_buffer_iter_peek);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003184
Steven Rostedt8d707e82009-06-16 21:22:48 -04003185static inline int rb_ok_to_lock(void)
3186{
3187 /*
3188 * If an NMI die dumps out the content of the ring buffer
3189 * do not grab locks. We also permanently disable the ring
3190 * buffer too. A one time deal is all you get from reading
3191 * the ring buffer from an NMI.
3192 */
Steven Rostedt464e85e2009-08-05 15:26:37 -04003193 if (likely(!in_nmi()))
Steven Rostedt8d707e82009-06-16 21:22:48 -04003194 return 1;
3195
3196 tracing_off_permanent();
3197 return 0;
3198}
3199
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003200/**
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003201 * ring_buffer_peek - peek at the next event to be read
3202 * @buffer: The ring buffer to read
3203 * @cpu: The cpu to peak at
3204 * @ts: The timestamp counter of this event.
Steven Rostedt66a8cb92010-03-31 13:21:56 -04003205 * @lost_events: a variable to store if events were lost (may be NULL)
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003206 *
3207 * This will return the event that will be read next, but does
3208 * not consume the data.
3209 */
3210struct ring_buffer_event *
Steven Rostedt66a8cb92010-03-31 13:21:56 -04003211ring_buffer_peek(struct ring_buffer *buffer, int cpu, u64 *ts,
3212 unsigned long *lost_events)
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003213{
3214 struct ring_buffer_per_cpu *cpu_buffer = buffer->buffers[cpu];
Steven Rostedt8aabee52009-03-12 13:13:49 -04003215 struct ring_buffer_event *event;
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003216 unsigned long flags;
Steven Rostedt8d707e82009-06-16 21:22:48 -04003217 int dolock;
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003218
Steven Rostedt554f7862009-03-11 22:00:13 -04003219 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedt8aabee52009-03-12 13:13:49 -04003220 return NULL;
Steven Rostedt554f7862009-03-11 22:00:13 -04003221
Steven Rostedt8d707e82009-06-16 21:22:48 -04003222 dolock = rb_ok_to_lock();
Tom Zanussi2d622712009-03-22 03:30:49 -05003223 again:
Steven Rostedt8d707e82009-06-16 21:22:48 -04003224 local_irq_save(flags);
3225 if (dolock)
3226 spin_lock(&cpu_buffer->reader_lock);
Steven Rostedt66a8cb92010-03-31 13:21:56 -04003227 event = rb_buffer_peek(cpu_buffer, ts, lost_events);
Robert Richter469535a2009-07-30 19:19:18 +02003228 if (event && event->type_len == RINGBUF_TYPE_PADDING)
3229 rb_advance_reader(cpu_buffer);
Steven Rostedt8d707e82009-06-16 21:22:48 -04003230 if (dolock)
3231 spin_unlock(&cpu_buffer->reader_lock);
3232 local_irq_restore(flags);
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003233
Steven Rostedt1b959e12009-09-03 10:12:13 -04003234 if (event && event->type_len == RINGBUF_TYPE_PADDING)
Tom Zanussi2d622712009-03-22 03:30:49 -05003235 goto again;
Tom Zanussi2d622712009-03-22 03:30:49 -05003236
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003237 return event;
3238}
3239
3240/**
3241 * ring_buffer_iter_peek - peek at the next event to be read
3242 * @iter: The ring buffer iterator
3243 * @ts: The timestamp counter of this event.
3244 *
3245 * This will return the event that will be read next, but does
3246 * not increment the iterator.
3247 */
3248struct ring_buffer_event *
3249ring_buffer_iter_peek(struct ring_buffer_iter *iter, u64 *ts)
3250{
3251 struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
3252 struct ring_buffer_event *event;
3253 unsigned long flags;
3254
Tom Zanussi2d622712009-03-22 03:30:49 -05003255 again:
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003256 spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
3257 event = rb_iter_peek(iter, ts);
3258 spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
3259
Steven Rostedt1b959e12009-09-03 10:12:13 -04003260 if (event && event->type_len == RINGBUF_TYPE_PADDING)
Tom Zanussi2d622712009-03-22 03:30:49 -05003261 goto again;
Tom Zanussi2d622712009-03-22 03:30:49 -05003262
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003263 return event;
3264}
3265
3266/**
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003267 * ring_buffer_consume - return an event and consume it
3268 * @buffer: The ring buffer to get the next event from
Steven Rostedt66a8cb92010-03-31 13:21:56 -04003269 * @cpu: the cpu to read the buffer from
3270 * @ts: a variable to store the timestamp (may be NULL)
3271 * @lost_events: a variable to store if events were lost (may be NULL)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003272 *
3273 * Returns the next event in the ring buffer, and that event is consumed.
3274 * Meaning, that sequential reads will keep returning a different event,
3275 * and eventually empty the ring buffer if the producer is slower.
3276 */
3277struct ring_buffer_event *
Steven Rostedt66a8cb92010-03-31 13:21:56 -04003278ring_buffer_consume(struct ring_buffer *buffer, int cpu, u64 *ts,
3279 unsigned long *lost_events)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003280{
Steven Rostedt554f7862009-03-11 22:00:13 -04003281 struct ring_buffer_per_cpu *cpu_buffer;
3282 struct ring_buffer_event *event = NULL;
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003283 unsigned long flags;
Steven Rostedt8d707e82009-06-16 21:22:48 -04003284 int dolock;
3285
3286 dolock = rb_ok_to_lock();
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003287
Tom Zanussi2d622712009-03-22 03:30:49 -05003288 again:
Steven Rostedt554f7862009-03-11 22:00:13 -04003289 /* might be called in atomic */
3290 preempt_disable();
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003291
Steven Rostedt554f7862009-03-11 22:00:13 -04003292 if (!cpumask_test_cpu(cpu, buffer->cpumask))
3293 goto out;
3294
3295 cpu_buffer = buffer->buffers[cpu];
Steven Rostedt8d707e82009-06-16 21:22:48 -04003296 local_irq_save(flags);
3297 if (dolock)
3298 spin_lock(&cpu_buffer->reader_lock);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003299
Steven Rostedt66a8cb92010-03-31 13:21:56 -04003300 event = rb_buffer_peek(cpu_buffer, ts, lost_events);
3301 if (event) {
3302 cpu_buffer->lost_events = 0;
Robert Richter469535a2009-07-30 19:19:18 +02003303 rb_advance_reader(cpu_buffer);
Steven Rostedt66a8cb92010-03-31 13:21:56 -04003304 }
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003305
Steven Rostedt8d707e82009-06-16 21:22:48 -04003306 if (dolock)
3307 spin_unlock(&cpu_buffer->reader_lock);
3308 local_irq_restore(flags);
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003309
Steven Rostedt554f7862009-03-11 22:00:13 -04003310 out:
3311 preempt_enable();
3312
Steven Rostedt1b959e12009-09-03 10:12:13 -04003313 if (event && event->type_len == RINGBUF_TYPE_PADDING)
Tom Zanussi2d622712009-03-22 03:30:49 -05003314 goto again;
Tom Zanussi2d622712009-03-22 03:30:49 -05003315
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003316 return event;
3317}
Robert Richterc4f50182008-12-11 16:49:22 +01003318EXPORT_SYMBOL_GPL(ring_buffer_consume);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003319
3320/**
3321 * ring_buffer_read_start - start a non consuming read of the buffer
3322 * @buffer: The ring buffer to read from
3323 * @cpu: The cpu buffer to iterate over
3324 *
3325 * This starts up an iteration through the buffer. It also disables
3326 * the recording to the buffer until the reading is finished.
3327 * This prevents the reading from being corrupted. This is not
3328 * a consuming read, so a producer is not expected.
3329 *
3330 * Must be paired with ring_buffer_finish.
3331 */
3332struct ring_buffer_iter *
3333ring_buffer_read_start(struct ring_buffer *buffer, int cpu)
3334{
3335 struct ring_buffer_per_cpu *cpu_buffer;
Steven Rostedt8aabee52009-03-12 13:13:49 -04003336 struct ring_buffer_iter *iter;
Steven Rostedtd7690412008-10-01 00:29:53 -04003337 unsigned long flags;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003338
Rusty Russell9e01c1b2009-01-01 10:12:22 +10303339 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedt8aabee52009-03-12 13:13:49 -04003340 return NULL;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003341
3342 iter = kmalloc(sizeof(*iter), GFP_KERNEL);
3343 if (!iter)
Steven Rostedt8aabee52009-03-12 13:13:49 -04003344 return NULL;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003345
3346 cpu_buffer = buffer->buffers[cpu];
3347
3348 iter->cpu_buffer = cpu_buffer;
3349
3350 atomic_inc(&cpu_buffer->record_disabled);
3351 synchronize_sched();
3352
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003353 spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
Thomas Gleixner0199c4e2009-12-02 20:01:25 +01003354 arch_spin_lock(&cpu_buffer->lock);
Steven Rostedt642edba2008-11-12 00:01:26 -05003355 rb_iter_reset(iter);
Thomas Gleixner0199c4e2009-12-02 20:01:25 +01003356 arch_spin_unlock(&cpu_buffer->lock);
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003357 spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003358
3359 return iter;
3360}
Robert Richterc4f50182008-12-11 16:49:22 +01003361EXPORT_SYMBOL_GPL(ring_buffer_read_start);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003362
3363/**
3364 * ring_buffer_finish - finish reading the iterator of the buffer
3365 * @iter: The iterator retrieved by ring_buffer_start
3366 *
3367 * This re-enables the recording to the buffer, and frees the
3368 * iterator.
3369 */
3370void
3371ring_buffer_read_finish(struct ring_buffer_iter *iter)
3372{
3373 struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
3374
3375 atomic_dec(&cpu_buffer->record_disabled);
3376 kfree(iter);
3377}
Robert Richterc4f50182008-12-11 16:49:22 +01003378EXPORT_SYMBOL_GPL(ring_buffer_read_finish);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003379
3380/**
3381 * ring_buffer_read - read the next item in the ring buffer by the iterator
3382 * @iter: The ring buffer iterator
3383 * @ts: The time stamp of the event read.
3384 *
3385 * This reads the next event in the ring buffer and increments the iterator.
3386 */
3387struct ring_buffer_event *
3388ring_buffer_read(struct ring_buffer_iter *iter, u64 *ts)
3389{
3390 struct ring_buffer_event *event;
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003391 struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
3392 unsigned long flags;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003393
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003394 spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
Steven Rostedt7e9391c2009-09-03 10:02:09 -04003395 again:
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003396 event = rb_iter_peek(iter, ts);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003397 if (!event)
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003398 goto out;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003399
Steven Rostedt7e9391c2009-09-03 10:02:09 -04003400 if (event->type_len == RINGBUF_TYPE_PADDING)
3401 goto again;
3402
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003403 rb_advance_iter(iter);
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003404 out:
3405 spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003406
3407 return event;
3408}
Robert Richterc4f50182008-12-11 16:49:22 +01003409EXPORT_SYMBOL_GPL(ring_buffer_read);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003410
3411/**
3412 * ring_buffer_size - return the size of the ring buffer (in bytes)
3413 * @buffer: The ring buffer.
3414 */
3415unsigned long ring_buffer_size(struct ring_buffer *buffer)
3416{
3417 return BUF_PAGE_SIZE * buffer->pages;
3418}
Robert Richterc4f50182008-12-11 16:49:22 +01003419EXPORT_SYMBOL_GPL(ring_buffer_size);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003420
3421static void
3422rb_reset_cpu(struct ring_buffer_per_cpu *cpu_buffer)
3423{
Steven Rostedt77ae3652009-03-27 11:00:29 -04003424 rb_head_page_deactivate(cpu_buffer);
3425
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003426 cpu_buffer->head_page
Steven Rostedt3adc54f2009-03-30 15:32:01 -04003427 = list_entry(cpu_buffer->pages, struct buffer_page, list);
Steven Rostedtbf41a152008-10-04 02:00:59 -04003428 local_set(&cpu_buffer->head_page->write, 0);
Steven Rostedt778c55d2009-05-01 18:44:45 -04003429 local_set(&cpu_buffer->head_page->entries, 0);
Steven Rostedtabc9b562008-12-02 15:34:06 -05003430 local_set(&cpu_buffer->head_page->page->commit, 0);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003431
Steven Rostedt6f807ac2008-10-04 02:00:58 -04003432 cpu_buffer->head_page->read = 0;
Steven Rostedtbf41a152008-10-04 02:00:59 -04003433
3434 cpu_buffer->tail_page = cpu_buffer->head_page;
3435 cpu_buffer->commit_page = cpu_buffer->head_page;
3436
3437 INIT_LIST_HEAD(&cpu_buffer->reader_page->list);
3438 local_set(&cpu_buffer->reader_page->write, 0);
Steven Rostedt778c55d2009-05-01 18:44:45 -04003439 local_set(&cpu_buffer->reader_page->entries, 0);
Steven Rostedtabc9b562008-12-02 15:34:06 -05003440 local_set(&cpu_buffer->reader_page->page->commit, 0);
Steven Rostedt6f807ac2008-10-04 02:00:58 -04003441 cpu_buffer->reader_page->read = 0;
Steven Rostedtd7690412008-10-01 00:29:53 -04003442
Steven Rostedt77ae3652009-03-27 11:00:29 -04003443 local_set(&cpu_buffer->commit_overrun, 0);
3444 local_set(&cpu_buffer->overrun, 0);
Steven Rostedte4906ef2009-04-30 20:49:44 -04003445 local_set(&cpu_buffer->entries, 0);
Steven Rostedtfa743952009-06-16 12:37:57 -04003446 local_set(&cpu_buffer->committing, 0);
3447 local_set(&cpu_buffer->commits, 0);
Steven Rostedt77ae3652009-03-27 11:00:29 -04003448 cpu_buffer->read = 0;
Steven Rostedt69507c02009-01-21 18:45:57 -05003449
3450 cpu_buffer->write_stamp = 0;
3451 cpu_buffer->read_stamp = 0;
Steven Rostedt77ae3652009-03-27 11:00:29 -04003452
Steven Rostedt66a8cb92010-03-31 13:21:56 -04003453 cpu_buffer->lost_events = 0;
3454 cpu_buffer->last_overrun = 0;
3455
Steven Rostedt77ae3652009-03-27 11:00:29 -04003456 rb_head_page_activate(cpu_buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003457}
3458
3459/**
3460 * ring_buffer_reset_cpu - reset a ring buffer per CPU buffer
3461 * @buffer: The ring buffer to reset a per cpu buffer of
3462 * @cpu: The CPU buffer to be reset
3463 */
3464void ring_buffer_reset_cpu(struct ring_buffer *buffer, int cpu)
3465{
3466 struct ring_buffer_per_cpu *cpu_buffer = buffer->buffers[cpu];
3467 unsigned long flags;
3468
Rusty Russell9e01c1b2009-01-01 10:12:22 +10303469 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedt8aabee52009-03-12 13:13:49 -04003470 return;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003471
Steven Rostedt41ede232009-05-01 20:26:54 -04003472 atomic_inc(&cpu_buffer->record_disabled);
3473
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003474 spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
3475
Steven Rostedt41b6a952009-09-02 09:59:48 -04003476 if (RB_WARN_ON(cpu_buffer, local_read(&cpu_buffer->committing)))
3477 goto out;
3478
Thomas Gleixner0199c4e2009-12-02 20:01:25 +01003479 arch_spin_lock(&cpu_buffer->lock);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003480
3481 rb_reset_cpu(cpu_buffer);
3482
Thomas Gleixner0199c4e2009-12-02 20:01:25 +01003483 arch_spin_unlock(&cpu_buffer->lock);
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003484
Steven Rostedt41b6a952009-09-02 09:59:48 -04003485 out:
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003486 spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
Steven Rostedt41ede232009-05-01 20:26:54 -04003487
3488 atomic_dec(&cpu_buffer->record_disabled);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003489}
Robert Richterc4f50182008-12-11 16:49:22 +01003490EXPORT_SYMBOL_GPL(ring_buffer_reset_cpu);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003491
3492/**
3493 * ring_buffer_reset - reset a ring buffer
3494 * @buffer: The ring buffer to reset all cpu buffers
3495 */
3496void ring_buffer_reset(struct ring_buffer *buffer)
3497{
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003498 int cpu;
3499
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003500 for_each_buffer_cpu(buffer, cpu)
Steven Rostedtd7690412008-10-01 00:29:53 -04003501 ring_buffer_reset_cpu(buffer, cpu);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003502}
Robert Richterc4f50182008-12-11 16:49:22 +01003503EXPORT_SYMBOL_GPL(ring_buffer_reset);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003504
3505/**
3506 * rind_buffer_empty - is the ring buffer empty?
3507 * @buffer: The ring buffer to test
3508 */
3509int ring_buffer_empty(struct ring_buffer *buffer)
3510{
3511 struct ring_buffer_per_cpu *cpu_buffer;
Steven Rostedtd4788202009-06-17 00:39:43 -04003512 unsigned long flags;
Steven Rostedt8d707e82009-06-16 21:22:48 -04003513 int dolock;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003514 int cpu;
Steven Rostedtd4788202009-06-17 00:39:43 -04003515 int ret;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003516
Steven Rostedt8d707e82009-06-16 21:22:48 -04003517 dolock = rb_ok_to_lock();
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003518
3519 /* yes this is racy, but if you don't like the race, lock the buffer */
3520 for_each_buffer_cpu(buffer, cpu) {
3521 cpu_buffer = buffer->buffers[cpu];
Steven Rostedt8d707e82009-06-16 21:22:48 -04003522 local_irq_save(flags);
3523 if (dolock)
3524 spin_lock(&cpu_buffer->reader_lock);
Steven Rostedtd4788202009-06-17 00:39:43 -04003525 ret = rb_per_cpu_empty(cpu_buffer);
Steven Rostedt8d707e82009-06-16 21:22:48 -04003526 if (dolock)
3527 spin_unlock(&cpu_buffer->reader_lock);
3528 local_irq_restore(flags);
3529
Steven Rostedtd4788202009-06-17 00:39:43 -04003530 if (!ret)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003531 return 0;
3532 }
Steven Rostedt554f7862009-03-11 22:00:13 -04003533
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003534 return 1;
3535}
Robert Richterc4f50182008-12-11 16:49:22 +01003536EXPORT_SYMBOL_GPL(ring_buffer_empty);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003537
3538/**
3539 * ring_buffer_empty_cpu - is a cpu buffer of a ring buffer empty?
3540 * @buffer: The ring buffer
3541 * @cpu: The CPU buffer to test
3542 */
3543int ring_buffer_empty_cpu(struct ring_buffer *buffer, int cpu)
3544{
3545 struct ring_buffer_per_cpu *cpu_buffer;
Steven Rostedtd4788202009-06-17 00:39:43 -04003546 unsigned long flags;
Steven Rostedt8d707e82009-06-16 21:22:48 -04003547 int dolock;
Steven Rostedt8aabee52009-03-12 13:13:49 -04003548 int ret;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003549
Rusty Russell9e01c1b2009-01-01 10:12:22 +10303550 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedt8aabee52009-03-12 13:13:49 -04003551 return 1;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003552
Steven Rostedt8d707e82009-06-16 21:22:48 -04003553 dolock = rb_ok_to_lock();
Steven Rostedt554f7862009-03-11 22:00:13 -04003554
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003555 cpu_buffer = buffer->buffers[cpu];
Steven Rostedt8d707e82009-06-16 21:22:48 -04003556 local_irq_save(flags);
3557 if (dolock)
3558 spin_lock(&cpu_buffer->reader_lock);
Steven Rostedt554f7862009-03-11 22:00:13 -04003559 ret = rb_per_cpu_empty(cpu_buffer);
Steven Rostedt8d707e82009-06-16 21:22:48 -04003560 if (dolock)
3561 spin_unlock(&cpu_buffer->reader_lock);
3562 local_irq_restore(flags);
Steven Rostedt554f7862009-03-11 22:00:13 -04003563
3564 return ret;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003565}
Robert Richterc4f50182008-12-11 16:49:22 +01003566EXPORT_SYMBOL_GPL(ring_buffer_empty_cpu);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003567
Steven Rostedt85bac322009-09-04 14:24:40 -04003568#ifdef CONFIG_RING_BUFFER_ALLOW_SWAP
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003569/**
3570 * ring_buffer_swap_cpu - swap a CPU buffer between two ring buffers
3571 * @buffer_a: One buffer to swap with
3572 * @buffer_b: The other buffer to swap with
3573 *
3574 * This function is useful for tracers that want to take a "snapshot"
3575 * of a CPU buffer and has another back up buffer lying around.
3576 * it is expected that the tracer handles the cpu buffer not being
3577 * used at the moment.
3578 */
3579int ring_buffer_swap_cpu(struct ring_buffer *buffer_a,
3580 struct ring_buffer *buffer_b, int cpu)
3581{
3582 struct ring_buffer_per_cpu *cpu_buffer_a;
3583 struct ring_buffer_per_cpu *cpu_buffer_b;
Steven Rostedt554f7862009-03-11 22:00:13 -04003584 int ret = -EINVAL;
3585
Rusty Russell9e01c1b2009-01-01 10:12:22 +10303586 if (!cpumask_test_cpu(cpu, buffer_a->cpumask) ||
3587 !cpumask_test_cpu(cpu, buffer_b->cpumask))
Steven Rostedt554f7862009-03-11 22:00:13 -04003588 goto out;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003589
3590 /* At least make sure the two buffers are somewhat the same */
Lai Jiangshan6d102bc2008-12-17 17:48:23 +08003591 if (buffer_a->pages != buffer_b->pages)
Steven Rostedt554f7862009-03-11 22:00:13 -04003592 goto out;
3593
3594 ret = -EAGAIN;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003595
Steven Rostedt97b17ef2009-01-21 15:24:56 -05003596 if (ring_buffer_flags != RB_BUFFERS_ON)
Steven Rostedt554f7862009-03-11 22:00:13 -04003597 goto out;
Steven Rostedt97b17ef2009-01-21 15:24:56 -05003598
3599 if (atomic_read(&buffer_a->record_disabled))
Steven Rostedt554f7862009-03-11 22:00:13 -04003600 goto out;
Steven Rostedt97b17ef2009-01-21 15:24:56 -05003601
3602 if (atomic_read(&buffer_b->record_disabled))
Steven Rostedt554f7862009-03-11 22:00:13 -04003603 goto out;
Steven Rostedt97b17ef2009-01-21 15:24:56 -05003604
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003605 cpu_buffer_a = buffer_a->buffers[cpu];
3606 cpu_buffer_b = buffer_b->buffers[cpu];
3607
Steven Rostedt97b17ef2009-01-21 15:24:56 -05003608 if (atomic_read(&cpu_buffer_a->record_disabled))
Steven Rostedt554f7862009-03-11 22:00:13 -04003609 goto out;
Steven Rostedt97b17ef2009-01-21 15:24:56 -05003610
3611 if (atomic_read(&cpu_buffer_b->record_disabled))
Steven Rostedt554f7862009-03-11 22:00:13 -04003612 goto out;
Steven Rostedt97b17ef2009-01-21 15:24:56 -05003613
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003614 /*
3615 * We can't do a synchronize_sched here because this
3616 * function can be called in atomic context.
3617 * Normally this will be called from the same CPU as cpu.
3618 * If not it's up to the caller to protect this.
3619 */
3620 atomic_inc(&cpu_buffer_a->record_disabled);
3621 atomic_inc(&cpu_buffer_b->record_disabled);
3622
Steven Rostedt98277992009-09-02 10:56:15 -04003623 ret = -EBUSY;
3624 if (local_read(&cpu_buffer_a->committing))
3625 goto out_dec;
3626 if (local_read(&cpu_buffer_b->committing))
3627 goto out_dec;
3628
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003629 buffer_a->buffers[cpu] = cpu_buffer_b;
3630 buffer_b->buffers[cpu] = cpu_buffer_a;
3631
3632 cpu_buffer_b->buffer = buffer_a;
3633 cpu_buffer_a->buffer = buffer_b;
3634
Steven Rostedt98277992009-09-02 10:56:15 -04003635 ret = 0;
3636
3637out_dec:
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003638 atomic_dec(&cpu_buffer_a->record_disabled);
3639 atomic_dec(&cpu_buffer_b->record_disabled);
Steven Rostedt554f7862009-03-11 22:00:13 -04003640out:
Steven Rostedt554f7862009-03-11 22:00:13 -04003641 return ret;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003642}
Robert Richterc4f50182008-12-11 16:49:22 +01003643EXPORT_SYMBOL_GPL(ring_buffer_swap_cpu);
Steven Rostedt85bac322009-09-04 14:24:40 -04003644#endif /* CONFIG_RING_BUFFER_ALLOW_SWAP */
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003645
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003646/**
3647 * ring_buffer_alloc_read_page - allocate a page to read from buffer
3648 * @buffer: the buffer to allocate for.
3649 *
3650 * This function is used in conjunction with ring_buffer_read_page.
3651 * When reading a full page from the ring buffer, these functions
3652 * can be used to speed up the process. The calling function should
3653 * allocate a few pages first with this function. Then when it
3654 * needs to get pages from the ring buffer, it passes the result
3655 * of this function into ring_buffer_read_page, which will swap
3656 * the page that was allocated, with the read page of the buffer.
3657 *
3658 * Returns:
3659 * The page allocated, or NULL on error.
3660 */
3661void *ring_buffer_alloc_read_page(struct ring_buffer *buffer)
3662{
Steven Rostedt044fa782008-12-02 23:50:03 -05003663 struct buffer_data_page *bpage;
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003664 unsigned long addr;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003665
3666 addr = __get_free_page(GFP_KERNEL);
3667 if (!addr)
3668 return NULL;
3669
Steven Rostedt044fa782008-12-02 23:50:03 -05003670 bpage = (void *)addr;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003671
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003672 rb_init_page(bpage);
3673
Steven Rostedt044fa782008-12-02 23:50:03 -05003674 return bpage;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003675}
Steven Rostedtd6ce96d2009-05-05 01:15:24 -04003676EXPORT_SYMBOL_GPL(ring_buffer_alloc_read_page);
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003677
3678/**
3679 * ring_buffer_free_read_page - free an allocated read page
3680 * @buffer: the buffer the page was allocate for
3681 * @data: the page to free
3682 *
3683 * Free a page allocated from ring_buffer_alloc_read_page.
3684 */
3685void ring_buffer_free_read_page(struct ring_buffer *buffer, void *data)
3686{
3687 free_page((unsigned long)data);
3688}
Steven Rostedtd6ce96d2009-05-05 01:15:24 -04003689EXPORT_SYMBOL_GPL(ring_buffer_free_read_page);
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003690
3691/**
3692 * ring_buffer_read_page - extract a page from the ring buffer
3693 * @buffer: buffer to extract from
3694 * @data_page: the page to use allocated from ring_buffer_alloc_read_page
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003695 * @len: amount to extract
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003696 * @cpu: the cpu of the buffer to extract
3697 * @full: should the extraction only happen when the page is full.
3698 *
3699 * This function will pull out a page from the ring buffer and consume it.
3700 * @data_page must be the address of the variable that was returned
3701 * from ring_buffer_alloc_read_page. This is because the page might be used
3702 * to swap with a page in the ring buffer.
3703 *
3704 * for example:
Lai Jiangshanb85fa012009-02-09 14:21:14 +08003705 * rpage = ring_buffer_alloc_read_page(buffer);
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003706 * if (!rpage)
3707 * return error;
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003708 * ret = ring_buffer_read_page(buffer, &rpage, len, cpu, 0);
Lai Jiangshan667d2412009-02-09 14:21:17 +08003709 * if (ret >= 0)
3710 * process_page(rpage, ret);
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003711 *
3712 * When @full is set, the function will not return true unless
3713 * the writer is off the reader page.
3714 *
3715 * Note: it is up to the calling functions to handle sleeps and wakeups.
3716 * The ring buffer can be used anywhere in the kernel and can not
3717 * blindly call wake_up. The layer that uses the ring buffer must be
3718 * responsible for that.
3719 *
3720 * Returns:
Lai Jiangshan667d2412009-02-09 14:21:17 +08003721 * >=0 if data has been transferred, returns the offset of consumed data.
3722 * <0 if no data has been transferred.
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003723 */
3724int ring_buffer_read_page(struct ring_buffer *buffer,
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003725 void **data_page, size_t len, int cpu, int full)
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003726{
3727 struct ring_buffer_per_cpu *cpu_buffer = buffer->buffers[cpu];
3728 struct ring_buffer_event *event;
Steven Rostedt044fa782008-12-02 23:50:03 -05003729 struct buffer_data_page *bpage;
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003730 struct buffer_page *reader;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003731 unsigned long flags;
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003732 unsigned int commit;
Lai Jiangshan667d2412009-02-09 14:21:17 +08003733 unsigned int read;
Steven Rostedt4f3640f2009-03-03 23:52:42 -05003734 u64 save_timestamp;
Steven Rostedt66a8cb92010-03-31 13:21:56 -04003735 int missed_events = 0;
Lai Jiangshan667d2412009-02-09 14:21:17 +08003736 int ret = -1;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003737
Steven Rostedt554f7862009-03-11 22:00:13 -04003738 if (!cpumask_test_cpu(cpu, buffer->cpumask))
3739 goto out;
3740
Steven Rostedt474d32b2009-03-03 19:51:40 -05003741 /*
3742 * If len is not big enough to hold the page header, then
3743 * we can not copy anything.
3744 */
3745 if (len <= BUF_PAGE_HDR_SIZE)
Steven Rostedt554f7862009-03-11 22:00:13 -04003746 goto out;
Steven Rostedt474d32b2009-03-03 19:51:40 -05003747
3748 len -= BUF_PAGE_HDR_SIZE;
3749
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003750 if (!data_page)
Steven Rostedt554f7862009-03-11 22:00:13 -04003751 goto out;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003752
Steven Rostedt044fa782008-12-02 23:50:03 -05003753 bpage = *data_page;
3754 if (!bpage)
Steven Rostedt554f7862009-03-11 22:00:13 -04003755 goto out;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003756
3757 spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
3758
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003759 reader = rb_get_reader_page(cpu_buffer);
3760 if (!reader)
Steven Rostedt554f7862009-03-11 22:00:13 -04003761 goto out_unlock;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003762
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003763 event = rb_reader_event(cpu_buffer);
Lai Jiangshan667d2412009-02-09 14:21:17 +08003764
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003765 read = reader->read;
3766 commit = rb_page_commit(reader);
3767
Steven Rostedt66a8cb92010-03-31 13:21:56 -04003768 /* Check if any events were dropped */
3769 if (cpu_buffer->lost_events)
3770 missed_events = 1;
3771
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003772 /*
Steven Rostedt474d32b2009-03-03 19:51:40 -05003773 * If this page has been partially read or
3774 * if len is not big enough to read the rest of the page or
3775 * a writer is still on the page, then
3776 * we must copy the data from the page to the buffer.
3777 * Otherwise, we can simply swap the page with the one passed in.
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003778 */
Steven Rostedt474d32b2009-03-03 19:51:40 -05003779 if (read || (len < (commit - read)) ||
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003780 cpu_buffer->reader_page == cpu_buffer->commit_page) {
Lai Jiangshan667d2412009-02-09 14:21:17 +08003781 struct buffer_data_page *rpage = cpu_buffer->reader_page->page;
Steven Rostedt474d32b2009-03-03 19:51:40 -05003782 unsigned int rpos = read;
3783 unsigned int pos = 0;
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003784 unsigned int size;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003785
3786 if (full)
Steven Rostedt554f7862009-03-11 22:00:13 -04003787 goto out_unlock;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003788
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003789 if (len > (commit - read))
3790 len = (commit - read);
3791
3792 size = rb_event_length(event);
3793
3794 if (len < size)
Steven Rostedt554f7862009-03-11 22:00:13 -04003795 goto out_unlock;
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003796
Steven Rostedt4f3640f2009-03-03 23:52:42 -05003797 /* save the current timestamp, since the user will need it */
3798 save_timestamp = cpu_buffer->read_stamp;
3799
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003800 /* Need to copy one event at a time */
3801 do {
Steven Rostedt474d32b2009-03-03 19:51:40 -05003802 memcpy(bpage->data + pos, rpage->data + rpos, size);
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003803
3804 len -= size;
3805
3806 rb_advance_reader(cpu_buffer);
Steven Rostedt474d32b2009-03-03 19:51:40 -05003807 rpos = reader->read;
3808 pos += size;
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003809
3810 event = rb_reader_event(cpu_buffer);
3811 size = rb_event_length(event);
3812 } while (len > size);
Lai Jiangshan667d2412009-02-09 14:21:17 +08003813
3814 /* update bpage */
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003815 local_set(&bpage->commit, pos);
Steven Rostedt4f3640f2009-03-03 23:52:42 -05003816 bpage->time_stamp = save_timestamp;
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003817
Steven Rostedt474d32b2009-03-03 19:51:40 -05003818 /* we copied everything to the beginning */
3819 read = 0;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003820 } else {
Steven Rostedtafbab762009-05-01 19:40:05 -04003821 /* update the entry counter */
Steven Rostedt77ae3652009-03-27 11:00:29 -04003822 cpu_buffer->read += rb_page_entries(reader);
Steven Rostedtafbab762009-05-01 19:40:05 -04003823
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003824 /* swap the pages */
Steven Rostedt044fa782008-12-02 23:50:03 -05003825 rb_init_page(bpage);
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003826 bpage = reader->page;
3827 reader->page = *data_page;
3828 local_set(&reader->write, 0);
Steven Rostedt778c55d2009-05-01 18:44:45 -04003829 local_set(&reader->entries, 0);
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003830 reader->read = 0;
Steven Rostedt044fa782008-12-02 23:50:03 -05003831 *data_page = bpage;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003832 }
Lai Jiangshan667d2412009-02-09 14:21:17 +08003833 ret = read;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003834
Steven Rostedt66a8cb92010-03-31 13:21:56 -04003835 cpu_buffer->lost_events = 0;
3836 /*
3837 * Set a flag in the commit field if we lost events
3838 */
3839 if (missed_events)
3840 local_add(RB_MISSED_EVENTS, &bpage->commit);
3841
Steven Rostedt554f7862009-03-11 22:00:13 -04003842 out_unlock:
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003843 spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
3844
Steven Rostedt554f7862009-03-11 22:00:13 -04003845 out:
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003846 return ret;
3847}
Steven Rostedtd6ce96d2009-05-05 01:15:24 -04003848EXPORT_SYMBOL_GPL(ring_buffer_read_page);
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003849
Paul Mundt1155de42009-06-25 14:30:12 +09003850#ifdef CONFIG_TRACING
Steven Rostedta3583242008-11-11 15:01:42 -05003851static ssize_t
3852rb_simple_read(struct file *filp, char __user *ubuf,
3853 size_t cnt, loff_t *ppos)
3854{
Hannes Eder5e398412009-02-10 19:44:34 +01003855 unsigned long *p = filp->private_data;
Steven Rostedta3583242008-11-11 15:01:42 -05003856 char buf[64];
3857 int r;
3858
Steven Rostedt033601a2008-11-21 12:41:55 -05003859 if (test_bit(RB_BUFFERS_DISABLED_BIT, p))
3860 r = sprintf(buf, "permanently disabled\n");
3861 else
3862 r = sprintf(buf, "%d\n", test_bit(RB_BUFFERS_ON_BIT, p));
Steven Rostedta3583242008-11-11 15:01:42 -05003863
3864 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
3865}
3866
3867static ssize_t
3868rb_simple_write(struct file *filp, const char __user *ubuf,
3869 size_t cnt, loff_t *ppos)
3870{
Hannes Eder5e398412009-02-10 19:44:34 +01003871 unsigned long *p = filp->private_data;
Steven Rostedta3583242008-11-11 15:01:42 -05003872 char buf[64];
Hannes Eder5e398412009-02-10 19:44:34 +01003873 unsigned long val;
Steven Rostedta3583242008-11-11 15:01:42 -05003874 int ret;
3875
3876 if (cnt >= sizeof(buf))
3877 return -EINVAL;
3878
3879 if (copy_from_user(&buf, ubuf, cnt))
3880 return -EFAULT;
3881
3882 buf[cnt] = 0;
3883
3884 ret = strict_strtoul(buf, 10, &val);
3885 if (ret < 0)
3886 return ret;
3887
Steven Rostedt033601a2008-11-21 12:41:55 -05003888 if (val)
3889 set_bit(RB_BUFFERS_ON_BIT, p);
3890 else
3891 clear_bit(RB_BUFFERS_ON_BIT, p);
Steven Rostedta3583242008-11-11 15:01:42 -05003892
3893 (*ppos)++;
3894
3895 return cnt;
3896}
3897
Steven Rostedt5e2336a2009-03-05 21:44:55 -05003898static const struct file_operations rb_simple_fops = {
Steven Rostedta3583242008-11-11 15:01:42 -05003899 .open = tracing_open_generic,
3900 .read = rb_simple_read,
3901 .write = rb_simple_write,
3902};
3903
3904
3905static __init int rb_init_debugfs(void)
3906{
3907 struct dentry *d_tracer;
Steven Rostedta3583242008-11-11 15:01:42 -05003908
3909 d_tracer = tracing_init_dentry();
3910
Frederic Weisbecker5452af62009-03-27 00:25:38 +01003911 trace_create_file("tracing_on", 0644, d_tracer,
3912 &ring_buffer_flags, &rb_simple_fops);
Steven Rostedta3583242008-11-11 15:01:42 -05003913
3914 return 0;
3915}
3916
3917fs_initcall(rb_init_debugfs);
Paul Mundt1155de42009-06-25 14:30:12 +09003918#endif
Steven Rostedt554f7862009-03-11 22:00:13 -04003919
Steven Rostedt59222ef2009-03-12 11:46:03 -04003920#ifdef CONFIG_HOTPLUG_CPU
Frederic Weisbecker09c9e842009-03-21 04:33:36 +01003921static int rb_cpu_notify(struct notifier_block *self,
3922 unsigned long action, void *hcpu)
Steven Rostedt554f7862009-03-11 22:00:13 -04003923{
3924 struct ring_buffer *buffer =
3925 container_of(self, struct ring_buffer, cpu_notify);
3926 long cpu = (long)hcpu;
3927
3928 switch (action) {
3929 case CPU_UP_PREPARE:
3930 case CPU_UP_PREPARE_FROZEN:
Rusty Russell3f237a72009-06-12 21:15:30 +09303931 if (cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedt554f7862009-03-11 22:00:13 -04003932 return NOTIFY_OK;
3933
3934 buffer->buffers[cpu] =
3935 rb_allocate_cpu_buffer(buffer, cpu);
3936 if (!buffer->buffers[cpu]) {
3937 WARN(1, "failed to allocate ring buffer on CPU %ld\n",
3938 cpu);
3939 return NOTIFY_OK;
3940 }
3941 smp_wmb();
Rusty Russell3f237a72009-06-12 21:15:30 +09303942 cpumask_set_cpu(cpu, buffer->cpumask);
Steven Rostedt554f7862009-03-11 22:00:13 -04003943 break;
3944 case CPU_DOWN_PREPARE:
3945 case CPU_DOWN_PREPARE_FROZEN:
3946 /*
3947 * Do nothing.
3948 * If we were to free the buffer, then the user would
3949 * lose any trace that was in the buffer.
3950 */
3951 break;
3952 default:
3953 break;
3954 }
3955 return NOTIFY_OK;
3956}
3957#endif