blob: f5007d0d932d28aa32e10df15a9c9d879112ddb4 [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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090017#include <linux/slab.h>
Steven Rostedt7a8e76a2008-09-29 23:02:38 -040018#include <linux/init.h>
19#include <linux/hash.h>
20#include <linux/list.h>
Steven Rostedt554f7862009-03-11 22:00:13 -040021#include <linux/cpu.h>
Steven Rostedt7a8e76a2008-09-29 23:02:38 -040022#include <linux/fs.h>
23
Christoph Lameter79615762010-01-05 15:34:50 +090024#include <asm/local.h>
Steven Rostedt182e9f52008-11-03 23:15:56 -050025#include "trace.h"
26
Steven Rostedt033601a2008-11-21 12:41:55 -050027/*
Steven Rostedtd1b182a2009-04-15 16:53:47 -040028 * The ring buffer header is special. We must manually up keep it.
29 */
30int ring_buffer_print_entry_header(struct trace_seq *s)
31{
32 int ret;
33
Lai Jiangshan334d4162009-04-24 11:27:05 +080034 ret = trace_seq_printf(s, "# compressed entry header\n");
35 ret = trace_seq_printf(s, "\ttype_len : 5 bits\n");
Steven Rostedtd1b182a2009-04-15 16:53:47 -040036 ret = trace_seq_printf(s, "\ttime_delta : 27 bits\n");
37 ret = trace_seq_printf(s, "\tarray : 32 bits\n");
38 ret = trace_seq_printf(s, "\n");
39 ret = trace_seq_printf(s, "\tpadding : type == %d\n",
40 RINGBUF_TYPE_PADDING);
41 ret = trace_seq_printf(s, "\ttime_extend : type == %d\n",
42 RINGBUF_TYPE_TIME_EXTEND);
Lai Jiangshan334d4162009-04-24 11:27:05 +080043 ret = trace_seq_printf(s, "\tdata max type_len == %d\n",
44 RINGBUF_TYPE_DATA_TYPE_LEN_MAX);
Steven Rostedtd1b182a2009-04-15 16:53:47 -040045
46 return ret;
47}
48
49/*
Steven Rostedt5cc98542009-03-12 22:24:17 -040050 * The ring buffer is made up of a list of pages. A separate list of pages is
51 * allocated for each CPU. A writer may only write to a buffer that is
52 * associated with the CPU it is currently executing on. A reader may read
53 * from any per cpu buffer.
54 *
55 * The reader is special. For each per cpu buffer, the reader has its own
56 * reader page. When a reader has read the entire reader page, this reader
57 * page is swapped with another page in the ring buffer.
58 *
59 * Now, as long as the writer is off the reader page, the reader can do what
60 * ever it wants with that page. The writer will never write to that page
61 * again (as long as it is out of the ring buffer).
62 *
63 * Here's some silly ASCII art.
64 *
65 * +------+
66 * |reader| RING BUFFER
67 * |page |
68 * +------+ +---+ +---+ +---+
69 * | |-->| |-->| |
70 * +---+ +---+ +---+
71 * ^ |
72 * | |
73 * +---------------+
74 *
75 *
76 * +------+
77 * |reader| RING BUFFER
78 * |page |------------------v
79 * +------+ +---+ +---+ +---+
80 * | |-->| |-->| |
81 * +---+ +---+ +---+
82 * ^ |
83 * | |
84 * +---------------+
85 *
86 *
87 * +------+
88 * |reader| RING BUFFER
89 * |page |------------------v
90 * +------+ +---+ +---+ +---+
91 * ^ | |-->| |-->| |
92 * | +---+ +---+ +---+
93 * | |
94 * | |
95 * +------------------------------+
96 *
97 *
98 * +------+
99 * |buffer| RING BUFFER
100 * |page |------------------v
101 * +------+ +---+ +---+ +---+
102 * ^ | | | |-->| |
103 * | New +---+ +---+ +---+
104 * | Reader------^ |
105 * | page |
106 * +------------------------------+
107 *
108 *
109 * After we make this swap, the reader can hand this page off to the splice
110 * code and be done with it. It can even allocate a new page if it needs to
111 * and swap that into the ring buffer.
112 *
113 * We will be using cmpxchg soon to make all this lockless.
114 *
115 */
116
117/*
Steven Rostedt033601a2008-11-21 12:41:55 -0500118 * A fast way to enable or disable all ring buffers is to
119 * call tracing_on or tracing_off. Turning off the ring buffers
120 * prevents all ring buffers from being recorded to.
121 * Turning this switch on, makes it OK to write to the
122 * ring buffer, if the ring buffer is enabled itself.
123 *
124 * There's three layers that must be on in order to write
125 * to the ring buffer.
126 *
127 * 1) This global flag must be set.
128 * 2) The ring buffer must be enabled for recording.
129 * 3) The per cpu buffer must be enabled for recording.
130 *
131 * In case of an anomaly, this global flag has a bit set that
132 * will permantly disable all ring buffers.
133 */
134
135/*
136 * Global flag to disable all recording to ring buffers
137 * This has two bits: ON, DISABLED
138 *
139 * ON DISABLED
140 * ---- ----------
141 * 0 0 : ring buffers are off
142 * 1 0 : ring buffers are on
143 * X 1 : ring buffers are permanently disabled
144 */
145
146enum {
147 RB_BUFFERS_ON_BIT = 0,
148 RB_BUFFERS_DISABLED_BIT = 1,
149};
150
151enum {
152 RB_BUFFERS_ON = 1 << RB_BUFFERS_ON_BIT,
153 RB_BUFFERS_DISABLED = 1 << RB_BUFFERS_DISABLED_BIT,
154};
155
Hannes Eder5e398412009-02-10 19:44:34 +0100156static unsigned long ring_buffer_flags __read_mostly = RB_BUFFERS_ON;
Steven Rostedta3583242008-11-11 15:01:42 -0500157
Steven Rostedt474d32b2009-03-03 19:51:40 -0500158#define BUF_PAGE_HDR_SIZE offsetof(struct buffer_data_page, data)
159
Steven Rostedta3583242008-11-11 15:01:42 -0500160/**
161 * tracing_on - enable all tracing buffers
162 *
163 * This function enables all tracing buffers that may have been
164 * disabled with tracing_off.
165 */
166void tracing_on(void)
167{
Steven Rostedt033601a2008-11-21 12:41:55 -0500168 set_bit(RB_BUFFERS_ON_BIT, &ring_buffer_flags);
Steven Rostedta3583242008-11-11 15:01:42 -0500169}
Robert Richterc4f50182008-12-11 16:49:22 +0100170EXPORT_SYMBOL_GPL(tracing_on);
Steven Rostedta3583242008-11-11 15:01:42 -0500171
172/**
173 * tracing_off - turn off all tracing buffers
174 *
175 * This function stops all tracing buffers from recording data.
176 * It does not disable any overhead the tracers themselves may
177 * be causing. This function simply causes all recording to
178 * the ring buffers to fail.
179 */
180void tracing_off(void)
181{
Steven Rostedt033601a2008-11-21 12:41:55 -0500182 clear_bit(RB_BUFFERS_ON_BIT, &ring_buffer_flags);
183}
Robert Richterc4f50182008-12-11 16:49:22 +0100184EXPORT_SYMBOL_GPL(tracing_off);
Steven Rostedt033601a2008-11-21 12:41:55 -0500185
186/**
187 * tracing_off_permanent - permanently disable ring buffers
188 *
189 * This function, once called, will disable all ring buffers
Wenji Huangc3706f02009-02-10 01:03:18 -0500190 * permanently.
Steven Rostedt033601a2008-11-21 12:41:55 -0500191 */
192void tracing_off_permanent(void)
193{
194 set_bit(RB_BUFFERS_DISABLED_BIT, &ring_buffer_flags);
Steven Rostedta3583242008-11-11 15:01:42 -0500195}
196
Steven Rostedt988ae9d2009-02-14 19:17:02 -0500197/**
198 * tracing_is_on - show state of ring buffers enabled
199 */
200int tracing_is_on(void)
201{
202 return ring_buffer_flags == RB_BUFFERS_ON;
203}
204EXPORT_SYMBOL_GPL(tracing_is_on);
205
Steven Rostedte3d6bf02009-03-03 13:53:07 -0500206#define RB_EVNT_HDR_SIZE (offsetof(struct ring_buffer_event, array))
Andrew Morton67d34722009-01-09 12:27:09 -0800207#define RB_ALIGNMENT 4U
Lai Jiangshan334d4162009-04-24 11:27:05 +0800208#define RB_MAX_SMALL_DATA (RB_ALIGNMENT * RINGBUF_TYPE_DATA_TYPE_LEN_MAX)
Steven Rostedtc7b09302009-06-11 11:12:00 -0400209#define RB_EVNT_MIN_SIZE 8U /* two 32bit words */
Lai Jiangshan334d4162009-04-24 11:27:05 +0800210
Steven Rostedt22710482010-03-18 17:54:19 -0400211#if !defined(CONFIG_64BIT) || defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS)
212# define RB_FORCE_8BYTE_ALIGNMENT 0
213# define RB_ARCH_ALIGNMENT RB_ALIGNMENT
214#else
215# define RB_FORCE_8BYTE_ALIGNMENT 1
216# define RB_ARCH_ALIGNMENT 8U
217#endif
218
Lai Jiangshan334d4162009-04-24 11:27:05 +0800219/* define RINGBUF_TYPE_DATA for 'case RINGBUF_TYPE_DATA:' */
220#define RINGBUF_TYPE_DATA 0 ... RINGBUF_TYPE_DATA_TYPE_LEN_MAX
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400221
222enum {
223 RB_LEN_TIME_EXTEND = 8,
224 RB_LEN_TIME_STAMP = 16,
225};
226
Steven Rostedt69d1b832010-10-07 18:18:05 -0400227#define skip_time_extend(event) \
228 ((struct ring_buffer_event *)((char *)event + RB_LEN_TIME_EXTEND))
229
Tom Zanussi2d622712009-03-22 03:30:49 -0500230static inline int rb_null_event(struct ring_buffer_event *event)
231{
Steven Rostedta1863c22009-09-03 10:23:58 -0400232 return event->type_len == RINGBUF_TYPE_PADDING && !event->time_delta;
Tom Zanussi2d622712009-03-22 03:30:49 -0500233}
234
235static void rb_event_set_padding(struct ring_buffer_event *event)
236{
Steven Rostedta1863c22009-09-03 10:23:58 -0400237 /* padding has a NULL time_delta */
Lai Jiangshan334d4162009-04-24 11:27:05 +0800238 event->type_len = RINGBUF_TYPE_PADDING;
Tom Zanussi2d622712009-03-22 03:30:49 -0500239 event->time_delta = 0;
240}
241
Tom Zanussi2d622712009-03-22 03:30:49 -0500242static unsigned
243rb_event_data_length(struct ring_buffer_event *event)
244{
245 unsigned length;
246
Lai Jiangshan334d4162009-04-24 11:27:05 +0800247 if (event->type_len)
248 length = event->type_len * RB_ALIGNMENT;
Tom Zanussi2d622712009-03-22 03:30:49 -0500249 else
250 length = event->array[0];
251 return length + RB_EVNT_HDR_SIZE;
252}
253
Steven Rostedt69d1b832010-10-07 18:18:05 -0400254/*
255 * Return the length of the given event. Will return
256 * the length of the time extend if the event is a
257 * time extend.
258 */
259static inline unsigned
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400260rb_event_length(struct ring_buffer_event *event)
261{
Lai Jiangshan334d4162009-04-24 11:27:05 +0800262 switch (event->type_len) {
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400263 case RINGBUF_TYPE_PADDING:
Tom Zanussi2d622712009-03-22 03:30:49 -0500264 if (rb_null_event(event))
265 /* undefined */
266 return -1;
Lai Jiangshan334d4162009-04-24 11:27:05 +0800267 return event->array[0] + RB_EVNT_HDR_SIZE;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400268
269 case RINGBUF_TYPE_TIME_EXTEND:
270 return RB_LEN_TIME_EXTEND;
271
272 case RINGBUF_TYPE_TIME_STAMP:
273 return RB_LEN_TIME_STAMP;
274
275 case RINGBUF_TYPE_DATA:
Tom Zanussi2d622712009-03-22 03:30:49 -0500276 return rb_event_data_length(event);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400277 default:
278 BUG();
279 }
280 /* not hit */
281 return 0;
282}
283
Steven Rostedt69d1b832010-10-07 18:18:05 -0400284/*
285 * Return total length of time extend and data,
286 * or just the event length for all other events.
287 */
288static inline unsigned
289rb_event_ts_length(struct ring_buffer_event *event)
290{
291 unsigned len = 0;
292
293 if (event->type_len == RINGBUF_TYPE_TIME_EXTEND) {
294 /* time extends include the data event after it */
295 len = RB_LEN_TIME_EXTEND;
296 event = skip_time_extend(event);
297 }
298 return len + rb_event_length(event);
299}
300
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400301/**
302 * ring_buffer_event_length - return the length of the event
303 * @event: the event to get the length of
Steven Rostedt69d1b832010-10-07 18:18:05 -0400304 *
305 * Returns the size of the data load of a data event.
306 * If the event is something other than a data event, it
307 * returns the size of the event itself. With the exception
308 * of a TIME EXTEND, where it still returns the size of the
309 * data load of the data event after it.
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400310 */
311unsigned ring_buffer_event_length(struct ring_buffer_event *event)
312{
Steven Rostedt69d1b832010-10-07 18:18:05 -0400313 unsigned length;
314
315 if (event->type_len == RINGBUF_TYPE_TIME_EXTEND)
316 event = skip_time_extend(event);
317
318 length = rb_event_length(event);
Lai Jiangshan334d4162009-04-24 11:27:05 +0800319 if (event->type_len > RINGBUF_TYPE_DATA_TYPE_LEN_MAX)
Robert Richter465634a2009-01-07 15:32:11 +0100320 return length;
321 length -= RB_EVNT_HDR_SIZE;
322 if (length > RB_MAX_SMALL_DATA + sizeof(event->array[0]))
323 length -= sizeof(event->array[0]);
324 return length;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400325}
Robert Richterc4f50182008-12-11 16:49:22 +0100326EXPORT_SYMBOL_GPL(ring_buffer_event_length);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400327
328/* inline for ring buffer fast paths */
Andrew Morton34a148b2009-01-09 12:27:09 -0800329static void *
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400330rb_event_data(struct ring_buffer_event *event)
331{
Steven Rostedt69d1b832010-10-07 18:18:05 -0400332 if (event->type_len == RINGBUF_TYPE_TIME_EXTEND)
333 event = skip_time_extend(event);
Lai Jiangshan334d4162009-04-24 11:27:05 +0800334 BUG_ON(event->type_len > RINGBUF_TYPE_DATA_TYPE_LEN_MAX);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400335 /* If length is in len field, then array[0] has the data */
Lai Jiangshan334d4162009-04-24 11:27:05 +0800336 if (event->type_len)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400337 return (void *)&event->array[0];
338 /* Otherwise length is in array[0] and array[1] has the data */
339 return (void *)&event->array[1];
340}
341
342/**
343 * ring_buffer_event_data - return the data of the event
344 * @event: the event to get the data from
345 */
346void *ring_buffer_event_data(struct ring_buffer_event *event)
347{
348 return rb_event_data(event);
349}
Robert Richterc4f50182008-12-11 16:49:22 +0100350EXPORT_SYMBOL_GPL(ring_buffer_event_data);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400351
352#define for_each_buffer_cpu(buffer, cpu) \
Rusty Russell9e01c1b2009-01-01 10:12:22 +1030353 for_each_cpu(cpu, buffer->cpumask)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400354
355#define TS_SHIFT 27
356#define TS_MASK ((1ULL << TS_SHIFT) - 1)
357#define TS_DELTA_TEST (~TS_MASK)
358
Steven Rostedt66a8cb92010-03-31 13:21:56 -0400359/* Flag when events were overwritten */
360#define RB_MISSED_EVENTS (1 << 31)
Steven Rostedtff0ff842010-03-31 22:11:42 -0400361/* Missed count stored at end */
362#define RB_MISSED_STORED (1 << 30)
Steven Rostedt66a8cb92010-03-31 13:21:56 -0400363
Steven Rostedtabc9b562008-12-02 15:34:06 -0500364struct buffer_data_page {
Steven Rostedte4c2ce82008-10-01 11:14:54 -0400365 u64 time_stamp; /* page time stamp */
Wenji Huangc3706f02009-02-10 01:03:18 -0500366 local_t commit; /* write committed index */
Steven Rostedtabc9b562008-12-02 15:34:06 -0500367 unsigned char data[]; /* data of buffer page */
368};
369
Steven Rostedt77ae3652009-03-27 11:00:29 -0400370/*
371 * Note, the buffer_page list must be first. The buffer pages
372 * are allocated in cache lines, which means that each buffer
373 * page will be at the beginning of a cache line, and thus
374 * the least significant bits will be zero. We use this to
375 * add flags in the list struct pointers, to make the ring buffer
376 * lockless.
377 */
Steven Rostedtabc9b562008-12-02 15:34:06 -0500378struct buffer_page {
Steven Rostedt778c55d2009-05-01 18:44:45 -0400379 struct list_head list; /* list of buffer pages */
Steven Rostedtabc9b562008-12-02 15:34:06 -0500380 local_t write; /* index for next write */
Steven Rostedt6f807ac2008-10-04 02:00:58 -0400381 unsigned read; /* index for next read */
Steven Rostedt778c55d2009-05-01 18:44:45 -0400382 local_t entries; /* entries on this page */
Steven Rostedtff0ff842010-03-31 22:11:42 -0400383 unsigned long real_end; /* real end of data */
Steven Rostedtabc9b562008-12-02 15:34:06 -0500384 struct buffer_data_page *page; /* Actual data page */
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400385};
386
Steven Rostedt77ae3652009-03-27 11:00:29 -0400387/*
388 * The buffer page counters, write and entries, must be reset
389 * atomically when crossing page boundaries. To synchronize this
390 * update, two counters are inserted into the number. One is
391 * the actual counter for the write position or count on the page.
392 *
393 * The other is a counter of updaters. Before an update happens
394 * the update partition of the counter is incremented. This will
395 * allow the updater to update the counter atomically.
396 *
397 * The counter is 20 bits, and the state data is 12.
398 */
399#define RB_WRITE_MASK 0xfffff
400#define RB_WRITE_INTCNT (1 << 20)
401
Steven Rostedt044fa782008-12-02 23:50:03 -0500402static void rb_init_page(struct buffer_data_page *bpage)
Steven Rostedtabc9b562008-12-02 15:34:06 -0500403{
Steven Rostedt044fa782008-12-02 23:50:03 -0500404 local_set(&bpage->commit, 0);
Steven Rostedtabc9b562008-12-02 15:34:06 -0500405}
406
Steven Rostedt474d32b2009-03-03 19:51:40 -0500407/**
408 * ring_buffer_page_len - the size of data on the page.
409 * @page: The page to read
410 *
411 * Returns the amount of data on the page, including buffer page header.
412 */
Steven Rostedtef7a4a12009-03-03 00:27:49 -0500413size_t ring_buffer_page_len(void *page)
414{
Steven Rostedt474d32b2009-03-03 19:51:40 -0500415 return local_read(&((struct buffer_data_page *)page)->commit)
416 + BUF_PAGE_HDR_SIZE;
Steven Rostedtef7a4a12009-03-03 00:27:49 -0500417}
418
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400419/*
Steven Rostedted568292008-09-29 23:02:40 -0400420 * Also stolen from mm/slob.c. Thanks to Mathieu Desnoyers for pointing
421 * this issue out.
422 */
Andrew Morton34a148b2009-01-09 12:27:09 -0800423static void free_buffer_page(struct buffer_page *bpage)
Steven Rostedted568292008-09-29 23:02:40 -0400424{
Andrew Morton34a148b2009-01-09 12:27:09 -0800425 free_page((unsigned long)bpage->page);
Steven Rostedte4c2ce82008-10-01 11:14:54 -0400426 kfree(bpage);
Steven Rostedted568292008-09-29 23:02:40 -0400427}
428
429/*
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400430 * We need to fit the time_stamp delta into 27 bits.
431 */
432static inline int test_time_stamp(u64 delta)
433{
434 if (delta & TS_DELTA_TEST)
435 return 1;
436 return 0;
437}
438
Steven Rostedt474d32b2009-03-03 19:51:40 -0500439#define BUF_PAGE_SIZE (PAGE_SIZE - BUF_PAGE_HDR_SIZE)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400440
Steven Rostedtbe957c42009-05-11 14:42:53 -0400441/* Max payload is BUF_PAGE_SIZE - header (8bytes) */
442#define BUF_MAX_DATA_SIZE (BUF_PAGE_SIZE - (sizeof(u32) * 2))
443
Steven Rostedtea05b572009-06-03 09:30:10 -0400444/* Max number of timestamps that can fit on a page */
Steven Rostedtd0134322010-10-12 12:06:43 -0400445#define RB_TIMESTAMPS_PER_PAGE (BUF_PAGE_SIZE / RB_LEN_TIME_EXTEND)
Steven Rostedtea05b572009-06-03 09:30:10 -0400446
Steven Rostedtd1b182a2009-04-15 16:53:47 -0400447int ring_buffer_print_page_header(struct trace_seq *s)
448{
449 struct buffer_data_page field;
450 int ret;
451
452 ret = trace_seq_printf(s, "\tfield: u64 timestamp;\t"
Tom Zanussi26a50742009-10-06 01:09:50 -0500453 "offset:0;\tsize:%u;\tsigned:%u;\n",
454 (unsigned int)sizeof(field.time_stamp),
455 (unsigned int)is_signed_type(u64));
Steven Rostedtd1b182a2009-04-15 16:53:47 -0400456
457 ret = trace_seq_printf(s, "\tfield: local_t commit;\t"
Tom Zanussi26a50742009-10-06 01:09:50 -0500458 "offset:%u;\tsize:%u;\tsigned:%u;\n",
Steven Rostedtd1b182a2009-04-15 16:53:47 -0400459 (unsigned int)offsetof(typeof(field), commit),
Tom Zanussi26a50742009-10-06 01:09:50 -0500460 (unsigned int)sizeof(field.commit),
461 (unsigned int)is_signed_type(long));
Steven Rostedtd1b182a2009-04-15 16:53:47 -0400462
Steven Rostedt66a8cb92010-03-31 13:21:56 -0400463 ret = trace_seq_printf(s, "\tfield: int overwrite;\t"
464 "offset:%u;\tsize:%u;\tsigned:%u;\n",
465 (unsigned int)offsetof(typeof(field), commit),
466 1,
467 (unsigned int)is_signed_type(long));
468
Steven Rostedtd1b182a2009-04-15 16:53:47 -0400469 ret = trace_seq_printf(s, "\tfield: char data;\t"
Tom Zanussi26a50742009-10-06 01:09:50 -0500470 "offset:%u;\tsize:%u;\tsigned:%u;\n",
Steven Rostedtd1b182a2009-04-15 16:53:47 -0400471 (unsigned int)offsetof(typeof(field), data),
Tom Zanussi26a50742009-10-06 01:09:50 -0500472 (unsigned int)BUF_PAGE_SIZE,
473 (unsigned int)is_signed_type(char));
Steven Rostedtd1b182a2009-04-15 16:53:47 -0400474
475 return ret;
476}
477
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400478/*
479 * head_page == tail_page && head == tail then buffer is empty.
480 */
481struct ring_buffer_per_cpu {
482 int cpu;
Richard Kennedy985023d2010-03-25 11:27:36 +0000483 atomic_t record_disabled;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400484 struct ring_buffer *buffer;
Steven Rostedt77ae3652009-03-27 11:00:29 -0400485 spinlock_t reader_lock; /* serialize readers */
Thomas Gleixner445c8952009-12-02 19:49:50 +0100486 arch_spinlock_t lock;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400487 struct lock_class_key lock_key;
Steven Rostedt3adc54f2009-03-30 15:32:01 -0400488 struct list_head *pages;
Steven Rostedt6f807ac2008-10-04 02:00:58 -0400489 struct buffer_page *head_page; /* read from head */
490 struct buffer_page *tail_page; /* write to tail */
Wenji Huangc3706f02009-02-10 01:03:18 -0500491 struct buffer_page *commit_page; /* committed pages */
Steven Rostedtd7690412008-10-01 00:29:53 -0400492 struct buffer_page *reader_page;
Steven Rostedt66a8cb92010-03-31 13:21:56 -0400493 unsigned long lost_events;
494 unsigned long last_overrun;
Steven Rostedt77ae3652009-03-27 11:00:29 -0400495 local_t commit_overrun;
496 local_t overrun;
Steven Rostedte4906ef2009-04-30 20:49:44 -0400497 local_t entries;
Steven Rostedtfa743952009-06-16 12:37:57 -0400498 local_t committing;
499 local_t commits;
Steven Rostedt77ae3652009-03-27 11:00:29 -0400500 unsigned long read;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400501 u64 write_stamp;
502 u64 read_stamp;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400503};
504
505struct ring_buffer {
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400506 unsigned pages;
507 unsigned flags;
508 int cpus;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400509 atomic_t record_disabled;
Arnaldo Carvalho de Melo00f62f62009-02-09 17:04:06 -0200510 cpumask_var_t cpumask;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400511
Peter Zijlstra1f8a6a12009-06-08 18:18:39 +0200512 struct lock_class_key *reader_lock_key;
513
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400514 struct mutex mutex;
515
516 struct ring_buffer_per_cpu **buffers;
Steven Rostedt554f7862009-03-11 22:00:13 -0400517
Steven Rostedt59222ef2009-03-12 11:46:03 -0400518#ifdef CONFIG_HOTPLUG_CPU
Steven Rostedt554f7862009-03-11 22:00:13 -0400519 struct notifier_block cpu_notify;
520#endif
Steven Rostedt37886f62009-03-17 17:22:06 -0400521 u64 (*clock)(void);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400522};
523
524struct ring_buffer_iter {
525 struct ring_buffer_per_cpu *cpu_buffer;
526 unsigned long head;
527 struct buffer_page *head_page;
Steven Rostedt492a74f2010-01-25 15:17:47 -0500528 struct buffer_page *cache_reader_page;
529 unsigned long cache_read;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400530 u64 read_stamp;
531};
532
Steven Rostedtf536aaf2008-11-10 23:07:30 -0500533/* buffer may be either ring_buffer or ring_buffer_per_cpu */
Steven Rostedt077c5402009-09-03 19:53:46 -0400534#define RB_WARN_ON(b, cond) \
535 ({ \
536 int _____ret = unlikely(cond); \
537 if (_____ret) { \
538 if (__same_type(*(b), struct ring_buffer_per_cpu)) { \
539 struct ring_buffer_per_cpu *__b = \
540 (void *)b; \
541 atomic_inc(&__b->buffer->record_disabled); \
542 } else \
543 atomic_inc(&b->record_disabled); \
544 WARN_ON(1); \
545 } \
546 _____ret; \
Steven Rostedt3e89c7b2008-11-11 15:28:41 -0500547 })
Steven Rostedtf536aaf2008-11-10 23:07:30 -0500548
Steven Rostedt37886f62009-03-17 17:22:06 -0400549/* Up this if you want to test the TIME_EXTENTS and normalization */
550#define DEBUG_SHIFT 0
551
Jiri Olsa6d3f1e12009-10-23 19:36:19 -0400552static inline u64 rb_time_stamp(struct ring_buffer *buffer)
Steven Rostedt88eb0122009-05-11 16:28:23 -0400553{
554 /* shift to debug/test normalization and TIME_EXTENTS */
555 return buffer->clock() << DEBUG_SHIFT;
556}
557
Steven Rostedt37886f62009-03-17 17:22:06 -0400558u64 ring_buffer_time_stamp(struct ring_buffer *buffer, int cpu)
559{
560 u64 time;
561
562 preempt_disable_notrace();
Jiri Olsa6d3f1e12009-10-23 19:36:19 -0400563 time = rb_time_stamp(buffer);
Steven Rostedt37886f62009-03-17 17:22:06 -0400564 preempt_enable_no_resched_notrace();
565
566 return time;
567}
568EXPORT_SYMBOL_GPL(ring_buffer_time_stamp);
569
570void ring_buffer_normalize_time_stamp(struct ring_buffer *buffer,
571 int cpu, u64 *ts)
572{
573 /* Just stupid testing the normalize function and deltas */
574 *ts >>= DEBUG_SHIFT;
575}
576EXPORT_SYMBOL_GPL(ring_buffer_normalize_time_stamp);
577
Steven Rostedt77ae3652009-03-27 11:00:29 -0400578/*
579 * Making the ring buffer lockless makes things tricky.
580 * Although writes only happen on the CPU that they are on,
581 * and they only need to worry about interrupts. Reads can
582 * happen on any CPU.
583 *
584 * The reader page is always off the ring buffer, but when the
585 * reader finishes with a page, it needs to swap its page with
586 * a new one from the buffer. The reader needs to take from
587 * the head (writes go to the tail). But if a writer is in overwrite
588 * mode and wraps, it must push the head page forward.
589 *
590 * Here lies the problem.
591 *
592 * The reader must be careful to replace only the head page, and
593 * not another one. As described at the top of the file in the
594 * ASCII art, the reader sets its old page to point to the next
595 * page after head. It then sets the page after head to point to
596 * the old reader page. But if the writer moves the head page
597 * during this operation, the reader could end up with the tail.
598 *
599 * We use cmpxchg to help prevent this race. We also do something
600 * special with the page before head. We set the LSB to 1.
601 *
602 * When the writer must push the page forward, it will clear the
603 * bit that points to the head page, move the head, and then set
604 * the bit that points to the new head page.
605 *
606 * We also don't want an interrupt coming in and moving the head
607 * page on another writer. Thus we use the second LSB to catch
608 * that too. Thus:
609 *
610 * head->list->prev->next bit 1 bit 0
611 * ------- -------
612 * Normal page 0 0
613 * Points to head page 0 1
614 * New head page 1 0
615 *
616 * Note we can not trust the prev pointer of the head page, because:
617 *
618 * +----+ +-----+ +-----+
619 * | |------>| T |---X--->| N |
620 * | |<------| | | |
621 * +----+ +-----+ +-----+
622 * ^ ^ |
623 * | +-----+ | |
624 * +----------| R |----------+ |
625 * | |<-----------+
626 * +-----+
627 *
628 * Key: ---X--> HEAD flag set in pointer
629 * T Tail page
630 * R Reader page
631 * N Next page
632 *
633 * (see __rb_reserve_next() to see where this happens)
634 *
635 * What the above shows is that the reader just swapped out
636 * the reader page with a page in the buffer, but before it
637 * could make the new header point back to the new page added
638 * it was preempted by a writer. The writer moved forward onto
639 * the new page added by the reader and is about to move forward
640 * again.
641 *
642 * You can see, it is legitimate for the previous pointer of
643 * the head (or any page) not to point back to itself. But only
644 * temporarially.
645 */
646
647#define RB_PAGE_NORMAL 0UL
648#define RB_PAGE_HEAD 1UL
649#define RB_PAGE_UPDATE 2UL
650
651
652#define RB_FLAG_MASK 3UL
653
654/* PAGE_MOVED is not part of the mask */
655#define RB_PAGE_MOVED 4UL
656
657/*
658 * rb_list_head - remove any bit
659 */
660static struct list_head *rb_list_head(struct list_head *list)
661{
662 unsigned long val = (unsigned long)list;
663
664 return (struct list_head *)(val & ~RB_FLAG_MASK);
665}
666
667/*
Jiri Olsa6d3f1e12009-10-23 19:36:19 -0400668 * rb_is_head_page - test if the given page is the head page
Steven Rostedt77ae3652009-03-27 11:00:29 -0400669 *
670 * Because the reader may move the head_page pointer, we can
671 * not trust what the head page is (it may be pointing to
672 * the reader page). But if the next page is a header page,
673 * its flags will be non zero.
674 */
675static int inline
676rb_is_head_page(struct ring_buffer_per_cpu *cpu_buffer,
677 struct buffer_page *page, struct list_head *list)
678{
679 unsigned long val;
680
681 val = (unsigned long)list->next;
682
683 if ((val & ~RB_FLAG_MASK) != (unsigned long)&page->list)
684 return RB_PAGE_MOVED;
685
686 return val & RB_FLAG_MASK;
687}
688
689/*
690 * rb_is_reader_page
691 *
692 * The unique thing about the reader page, is that, if the
693 * writer is ever on it, the previous pointer never points
694 * back to the reader page.
695 */
696static int rb_is_reader_page(struct buffer_page *page)
697{
698 struct list_head *list = page->list.prev;
699
700 return rb_list_head(list->next) != &page->list;
701}
702
703/*
704 * rb_set_list_to_head - set a list_head to be pointing to head.
705 */
706static void rb_set_list_to_head(struct ring_buffer_per_cpu *cpu_buffer,
707 struct list_head *list)
708{
709 unsigned long *ptr;
710
711 ptr = (unsigned long *)&list->next;
712 *ptr |= RB_PAGE_HEAD;
713 *ptr &= ~RB_PAGE_UPDATE;
714}
715
716/*
717 * rb_head_page_activate - sets up head page
718 */
719static void rb_head_page_activate(struct ring_buffer_per_cpu *cpu_buffer)
720{
721 struct buffer_page *head;
722
723 head = cpu_buffer->head_page;
724 if (!head)
725 return;
726
727 /*
728 * Set the previous list pointer to have the HEAD flag.
729 */
730 rb_set_list_to_head(cpu_buffer, head->list.prev);
731}
732
733static void rb_list_head_clear(struct list_head *list)
734{
735 unsigned long *ptr = (unsigned long *)&list->next;
736
737 *ptr &= ~RB_FLAG_MASK;
738}
739
740/*
741 * rb_head_page_dactivate - clears head page ptr (for free list)
742 */
743static void
744rb_head_page_deactivate(struct ring_buffer_per_cpu *cpu_buffer)
745{
746 struct list_head *hd;
747
748 /* Go through the whole list and clear any pointers found. */
749 rb_list_head_clear(cpu_buffer->pages);
750
751 list_for_each(hd, cpu_buffer->pages)
752 rb_list_head_clear(hd);
753}
754
755static int rb_head_page_set(struct ring_buffer_per_cpu *cpu_buffer,
756 struct buffer_page *head,
757 struct buffer_page *prev,
758 int old_flag, int new_flag)
759{
760 struct list_head *list;
761 unsigned long val = (unsigned long)&head->list;
762 unsigned long ret;
763
764 list = &prev->list;
765
766 val &= ~RB_FLAG_MASK;
767
Steven Rostedt08a40812009-09-14 09:31:35 -0400768 ret = cmpxchg((unsigned long *)&list->next,
769 val | old_flag, val | new_flag);
Steven Rostedt77ae3652009-03-27 11:00:29 -0400770
771 /* check if the reader took the page */
772 if ((ret & ~RB_FLAG_MASK) != val)
773 return RB_PAGE_MOVED;
774
775 return ret & RB_FLAG_MASK;
776}
777
778static int rb_head_page_set_update(struct ring_buffer_per_cpu *cpu_buffer,
779 struct buffer_page *head,
780 struct buffer_page *prev,
781 int old_flag)
782{
783 return rb_head_page_set(cpu_buffer, head, prev,
784 old_flag, RB_PAGE_UPDATE);
785}
786
787static int rb_head_page_set_head(struct ring_buffer_per_cpu *cpu_buffer,
788 struct buffer_page *head,
789 struct buffer_page *prev,
790 int old_flag)
791{
792 return rb_head_page_set(cpu_buffer, head, prev,
793 old_flag, RB_PAGE_HEAD);
794}
795
796static int rb_head_page_set_normal(struct ring_buffer_per_cpu *cpu_buffer,
797 struct buffer_page *head,
798 struct buffer_page *prev,
799 int old_flag)
800{
801 return rb_head_page_set(cpu_buffer, head, prev,
802 old_flag, RB_PAGE_NORMAL);
803}
804
805static inline void rb_inc_page(struct ring_buffer_per_cpu *cpu_buffer,
806 struct buffer_page **bpage)
807{
808 struct list_head *p = rb_list_head((*bpage)->list.next);
809
810 *bpage = list_entry(p, struct buffer_page, list);
811}
812
813static struct buffer_page *
814rb_set_head_page(struct ring_buffer_per_cpu *cpu_buffer)
815{
816 struct buffer_page *head;
817 struct buffer_page *page;
818 struct list_head *list;
819 int i;
820
821 if (RB_WARN_ON(cpu_buffer, !cpu_buffer->head_page))
822 return NULL;
823
824 /* sanity check */
825 list = cpu_buffer->pages;
826 if (RB_WARN_ON(cpu_buffer, rb_list_head(list->prev->next) != list))
827 return NULL;
828
829 page = head = cpu_buffer->head_page;
830 /*
831 * It is possible that the writer moves the header behind
832 * where we started, and we miss in one loop.
833 * A second loop should grab the header, but we'll do
834 * three loops just because I'm paranoid.
835 */
836 for (i = 0; i < 3; i++) {
837 do {
838 if (rb_is_head_page(cpu_buffer, page, page->list.prev)) {
839 cpu_buffer->head_page = page;
840 return page;
841 }
842 rb_inc_page(cpu_buffer, &page);
843 } while (page != head);
844 }
845
846 RB_WARN_ON(cpu_buffer, 1);
847
848 return NULL;
849}
850
851static int rb_head_page_replace(struct buffer_page *old,
852 struct buffer_page *new)
853{
854 unsigned long *ptr = (unsigned long *)&old->list.prev->next;
855 unsigned long val;
856 unsigned long ret;
857
858 val = *ptr & ~RB_FLAG_MASK;
859 val |= RB_PAGE_HEAD;
860
Steven Rostedt08a40812009-09-14 09:31:35 -0400861 ret = cmpxchg(ptr, val, (unsigned long)&new->list);
Steven Rostedt77ae3652009-03-27 11:00:29 -0400862
863 return ret == val;
864}
865
866/*
867 * rb_tail_page_update - move the tail page forward
868 *
869 * Returns 1 if moved tail page, 0 if someone else did.
870 */
871static int rb_tail_page_update(struct ring_buffer_per_cpu *cpu_buffer,
872 struct buffer_page *tail_page,
873 struct buffer_page *next_page)
874{
875 struct buffer_page *old_tail;
876 unsigned long old_entries;
877 unsigned long old_write;
878 int ret = 0;
879
880 /*
881 * The tail page now needs to be moved forward.
882 *
883 * We need to reset the tail page, but without messing
884 * with possible erasing of data brought in by interrupts
885 * that have moved the tail page and are currently on it.
886 *
887 * We add a counter to the write field to denote this.
888 */
889 old_write = local_add_return(RB_WRITE_INTCNT, &next_page->write);
890 old_entries = local_add_return(RB_WRITE_INTCNT, &next_page->entries);
891
892 /*
893 * Just make sure we have seen our old_write and synchronize
894 * with any interrupts that come in.
895 */
896 barrier();
897
898 /*
899 * If the tail page is still the same as what we think
900 * it is, then it is up to us to update the tail
901 * pointer.
902 */
903 if (tail_page == cpu_buffer->tail_page) {
904 /* Zero the write counter */
905 unsigned long val = old_write & ~RB_WRITE_MASK;
906 unsigned long eval = old_entries & ~RB_WRITE_MASK;
907
908 /*
909 * This will only succeed if an interrupt did
910 * not come in and change it. In which case, we
911 * do not want to modify it.
Lai Jiangshanda706d82009-07-15 16:27:30 +0800912 *
913 * We add (void) to let the compiler know that we do not care
914 * about the return value of these functions. We use the
915 * cmpxchg to only update if an interrupt did not already
916 * do it for us. If the cmpxchg fails, we don't care.
Steven Rostedt77ae3652009-03-27 11:00:29 -0400917 */
Lai Jiangshanda706d82009-07-15 16:27:30 +0800918 (void)local_cmpxchg(&next_page->write, old_write, val);
919 (void)local_cmpxchg(&next_page->entries, old_entries, eval);
Steven Rostedt77ae3652009-03-27 11:00:29 -0400920
921 /*
922 * No need to worry about races with clearing out the commit.
923 * it only can increment when a commit takes place. But that
924 * only happens in the outer most nested commit.
925 */
926 local_set(&next_page->page->commit, 0);
927
928 old_tail = cmpxchg(&cpu_buffer->tail_page,
929 tail_page, next_page);
930
931 if (old_tail == tail_page)
932 ret = 1;
933 }
934
935 return ret;
936}
937
938static int rb_check_bpage(struct ring_buffer_per_cpu *cpu_buffer,
939 struct buffer_page *bpage)
940{
941 unsigned long val = (unsigned long)bpage;
942
943 if (RB_WARN_ON(cpu_buffer, val & RB_FLAG_MASK))
944 return 1;
945
946 return 0;
947}
948
949/**
950 * rb_check_list - make sure a pointer to a list has the last bits zero
951 */
952static int rb_check_list(struct ring_buffer_per_cpu *cpu_buffer,
953 struct list_head *list)
954{
955 if (RB_WARN_ON(cpu_buffer, rb_list_head(list->prev) != list->prev))
956 return 1;
957 if (RB_WARN_ON(cpu_buffer, rb_list_head(list->next) != list->next))
958 return 1;
959 return 0;
960}
961
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400962/**
963 * check_pages - integrity check of buffer pages
964 * @cpu_buffer: CPU buffer with pages to test
965 *
Wenji Huangc3706f02009-02-10 01:03:18 -0500966 * As a safety measure we check to make sure the data pages have not
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400967 * been corrupted.
968 */
969static int rb_check_pages(struct ring_buffer_per_cpu *cpu_buffer)
970{
Steven Rostedt3adc54f2009-03-30 15:32:01 -0400971 struct list_head *head = cpu_buffer->pages;
Steven Rostedt044fa782008-12-02 23:50:03 -0500972 struct buffer_page *bpage, *tmp;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400973
Steven Rostedt77ae3652009-03-27 11:00:29 -0400974 rb_head_page_deactivate(cpu_buffer);
975
Steven Rostedt3e89c7b2008-11-11 15:28:41 -0500976 if (RB_WARN_ON(cpu_buffer, head->next->prev != head))
977 return -1;
978 if (RB_WARN_ON(cpu_buffer, head->prev->next != head))
979 return -1;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400980
Steven Rostedt77ae3652009-03-27 11:00:29 -0400981 if (rb_check_list(cpu_buffer, head))
982 return -1;
983
Steven Rostedt044fa782008-12-02 23:50:03 -0500984 list_for_each_entry_safe(bpage, tmp, head, list) {
Steven Rostedt3e89c7b2008-11-11 15:28:41 -0500985 if (RB_WARN_ON(cpu_buffer,
Steven Rostedt044fa782008-12-02 23:50:03 -0500986 bpage->list.next->prev != &bpage->list))
Steven Rostedt3e89c7b2008-11-11 15:28:41 -0500987 return -1;
988 if (RB_WARN_ON(cpu_buffer,
Steven Rostedt044fa782008-12-02 23:50:03 -0500989 bpage->list.prev->next != &bpage->list))
Steven Rostedt3e89c7b2008-11-11 15:28:41 -0500990 return -1;
Steven Rostedt77ae3652009-03-27 11:00:29 -0400991 if (rb_check_list(cpu_buffer, &bpage->list))
992 return -1;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400993 }
994
Steven Rostedt77ae3652009-03-27 11:00:29 -0400995 rb_head_page_activate(cpu_buffer);
996
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400997 return 0;
998}
999
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001000static int rb_allocate_pages(struct ring_buffer_per_cpu *cpu_buffer,
1001 unsigned nr_pages)
1002{
Steven Rostedt044fa782008-12-02 23:50:03 -05001003 struct buffer_page *bpage, *tmp;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001004 unsigned long addr;
1005 LIST_HEAD(pages);
1006 unsigned i;
1007
Steven Rostedt3adc54f2009-03-30 15:32:01 -04001008 WARN_ON(!nr_pages);
1009
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001010 for (i = 0; i < nr_pages; i++) {
Steven Rostedt044fa782008-12-02 23:50:03 -05001011 bpage = kzalloc_node(ALIGN(sizeof(*bpage), cache_line_size()),
Steven Rostedtaa1e0e32008-10-02 19:18:09 -04001012 GFP_KERNEL, cpu_to_node(cpu_buffer->cpu));
Steven Rostedt044fa782008-12-02 23:50:03 -05001013 if (!bpage)
Steven Rostedte4c2ce82008-10-01 11:14:54 -04001014 goto free_pages;
Steven Rostedt77ae3652009-03-27 11:00:29 -04001015
1016 rb_check_bpage(cpu_buffer, bpage);
1017
Steven Rostedt044fa782008-12-02 23:50:03 -05001018 list_add(&bpage->list, &pages);
Steven Rostedte4c2ce82008-10-01 11:14:54 -04001019
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001020 addr = __get_free_page(GFP_KERNEL);
1021 if (!addr)
1022 goto free_pages;
Steven Rostedt044fa782008-12-02 23:50:03 -05001023 bpage->page = (void *)addr;
1024 rb_init_page(bpage->page);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001025 }
1026
Steven Rostedt3adc54f2009-03-30 15:32:01 -04001027 /*
1028 * The ring buffer page list is a circular list that does not
1029 * start and end with a list head. All page list items point to
1030 * other pages.
1031 */
1032 cpu_buffer->pages = pages.next;
1033 list_del(&pages);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001034
1035 rb_check_pages(cpu_buffer);
1036
1037 return 0;
1038
1039 free_pages:
Steven Rostedt044fa782008-12-02 23:50:03 -05001040 list_for_each_entry_safe(bpage, tmp, &pages, list) {
1041 list_del_init(&bpage->list);
1042 free_buffer_page(bpage);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001043 }
1044 return -ENOMEM;
1045}
1046
1047static struct ring_buffer_per_cpu *
1048rb_allocate_cpu_buffer(struct ring_buffer *buffer, int cpu)
1049{
1050 struct ring_buffer_per_cpu *cpu_buffer;
Steven Rostedt044fa782008-12-02 23:50:03 -05001051 struct buffer_page *bpage;
Steven Rostedtd7690412008-10-01 00:29:53 -04001052 unsigned long addr;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001053 int ret;
1054
1055 cpu_buffer = kzalloc_node(ALIGN(sizeof(*cpu_buffer), cache_line_size()),
1056 GFP_KERNEL, cpu_to_node(cpu));
1057 if (!cpu_buffer)
1058 return NULL;
1059
1060 cpu_buffer->cpu = cpu;
1061 cpu_buffer->buffer = buffer;
Steven Rostedtf83c9d02008-11-11 18:47:44 +01001062 spin_lock_init(&cpu_buffer->reader_lock);
Peter Zijlstra1f8a6a12009-06-08 18:18:39 +02001063 lockdep_set_class(&cpu_buffer->reader_lock, buffer->reader_lock_key);
Thomas Gleixneredc35bd2009-12-03 12:38:57 +01001064 cpu_buffer->lock = (arch_spinlock_t)__ARCH_SPIN_LOCK_UNLOCKED;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001065
Steven Rostedt044fa782008-12-02 23:50:03 -05001066 bpage = kzalloc_node(ALIGN(sizeof(*bpage), cache_line_size()),
Steven Rostedte4c2ce82008-10-01 11:14:54 -04001067 GFP_KERNEL, cpu_to_node(cpu));
Steven Rostedt044fa782008-12-02 23:50:03 -05001068 if (!bpage)
Steven Rostedte4c2ce82008-10-01 11:14:54 -04001069 goto fail_free_buffer;
1070
Steven Rostedt77ae3652009-03-27 11:00:29 -04001071 rb_check_bpage(cpu_buffer, bpage);
1072
Steven Rostedt044fa782008-12-02 23:50:03 -05001073 cpu_buffer->reader_page = bpage;
Steven Rostedtd7690412008-10-01 00:29:53 -04001074 addr = __get_free_page(GFP_KERNEL);
1075 if (!addr)
Steven Rostedte4c2ce82008-10-01 11:14:54 -04001076 goto fail_free_reader;
Steven Rostedt044fa782008-12-02 23:50:03 -05001077 bpage->page = (void *)addr;
1078 rb_init_page(bpage->page);
Steven Rostedte4c2ce82008-10-01 11:14:54 -04001079
Steven Rostedtd7690412008-10-01 00:29:53 -04001080 INIT_LIST_HEAD(&cpu_buffer->reader_page->list);
Steven Rostedtd7690412008-10-01 00:29:53 -04001081
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001082 ret = rb_allocate_pages(cpu_buffer, buffer->pages);
1083 if (ret < 0)
Steven Rostedtd7690412008-10-01 00:29:53 -04001084 goto fail_free_reader;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001085
1086 cpu_buffer->head_page
Steven Rostedt3adc54f2009-03-30 15:32:01 -04001087 = list_entry(cpu_buffer->pages, struct buffer_page, list);
Steven Rostedtbf41a152008-10-04 02:00:59 -04001088 cpu_buffer->tail_page = cpu_buffer->commit_page = cpu_buffer->head_page;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001089
Steven Rostedt77ae3652009-03-27 11:00:29 -04001090 rb_head_page_activate(cpu_buffer);
1091
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001092 return cpu_buffer;
1093
Steven Rostedtd7690412008-10-01 00:29:53 -04001094 fail_free_reader:
1095 free_buffer_page(cpu_buffer->reader_page);
1096
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001097 fail_free_buffer:
1098 kfree(cpu_buffer);
1099 return NULL;
1100}
1101
1102static void rb_free_cpu_buffer(struct ring_buffer_per_cpu *cpu_buffer)
1103{
Steven Rostedt3adc54f2009-03-30 15:32:01 -04001104 struct list_head *head = cpu_buffer->pages;
Steven Rostedt044fa782008-12-02 23:50:03 -05001105 struct buffer_page *bpage, *tmp;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001106
Steven Rostedtd7690412008-10-01 00:29:53 -04001107 free_buffer_page(cpu_buffer->reader_page);
1108
Steven Rostedt77ae3652009-03-27 11:00:29 -04001109 rb_head_page_deactivate(cpu_buffer);
1110
Steven Rostedt3adc54f2009-03-30 15:32:01 -04001111 if (head) {
1112 list_for_each_entry_safe(bpage, tmp, head, list) {
1113 list_del_init(&bpage->list);
1114 free_buffer_page(bpage);
1115 }
1116 bpage = list_entry(head, struct buffer_page, list);
Steven Rostedt044fa782008-12-02 23:50:03 -05001117 free_buffer_page(bpage);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001118 }
Steven Rostedt3adc54f2009-03-30 15:32:01 -04001119
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001120 kfree(cpu_buffer);
1121}
1122
Steven Rostedt59222ef2009-03-12 11:46:03 -04001123#ifdef CONFIG_HOTPLUG_CPU
Frederic Weisbecker09c9e842009-03-21 04:33:36 +01001124static int rb_cpu_notify(struct notifier_block *self,
1125 unsigned long action, void *hcpu);
Steven Rostedt554f7862009-03-11 22:00:13 -04001126#endif
1127
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001128/**
1129 * ring_buffer_alloc - allocate a new ring_buffer
Robert Richter68814b52008-11-24 12:24:12 +01001130 * @size: the size in bytes per cpu that is needed.
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001131 * @flags: attributes to set for the ring buffer.
1132 *
1133 * Currently the only flag that is available is the RB_FL_OVERWRITE
1134 * flag. This flag means that the buffer will overwrite old data
1135 * when the buffer wraps. If this flag is not set, the buffer will
1136 * drop data when the tail hits the head.
1137 */
Peter Zijlstra1f8a6a12009-06-08 18:18:39 +02001138struct ring_buffer *__ring_buffer_alloc(unsigned long size, unsigned flags,
1139 struct lock_class_key *key)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001140{
1141 struct ring_buffer *buffer;
1142 int bsize;
1143 int cpu;
1144
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001145 /* keep it in its own cache line */
1146 buffer = kzalloc(ALIGN(sizeof(*buffer), cache_line_size()),
1147 GFP_KERNEL);
1148 if (!buffer)
1149 return NULL;
1150
Rusty Russell9e01c1b2009-01-01 10:12:22 +10301151 if (!alloc_cpumask_var(&buffer->cpumask, GFP_KERNEL))
1152 goto fail_free_buffer;
1153
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001154 buffer->pages = DIV_ROUND_UP(size, BUF_PAGE_SIZE);
1155 buffer->flags = flags;
Steven Rostedt37886f62009-03-17 17:22:06 -04001156 buffer->clock = trace_clock_local;
Peter Zijlstra1f8a6a12009-06-08 18:18:39 +02001157 buffer->reader_lock_key = key;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001158
1159 /* need at least two pages */
Steven Rostedt5f78abe2009-06-17 14:11:10 -04001160 if (buffer->pages < 2)
1161 buffer->pages = 2;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001162
Frederic Weisbecker3bf832c2009-03-19 14:47:33 +01001163 /*
1164 * In case of non-hotplug cpu, if the ring-buffer is allocated
1165 * in early initcall, it will not be notified of secondary cpus.
1166 * In that off case, we need to allocate for all possible cpus.
1167 */
1168#ifdef CONFIG_HOTPLUG_CPU
Steven Rostedt554f7862009-03-11 22:00:13 -04001169 get_online_cpus();
1170 cpumask_copy(buffer->cpumask, cpu_online_mask);
Frederic Weisbecker3bf832c2009-03-19 14:47:33 +01001171#else
1172 cpumask_copy(buffer->cpumask, cpu_possible_mask);
1173#endif
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001174 buffer->cpus = nr_cpu_ids;
1175
1176 bsize = sizeof(void *) * nr_cpu_ids;
1177 buffer->buffers = kzalloc(ALIGN(bsize, cache_line_size()),
1178 GFP_KERNEL);
1179 if (!buffer->buffers)
Rusty Russell9e01c1b2009-01-01 10:12:22 +10301180 goto fail_free_cpumask;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001181
1182 for_each_buffer_cpu(buffer, cpu) {
1183 buffer->buffers[cpu] =
1184 rb_allocate_cpu_buffer(buffer, cpu);
1185 if (!buffer->buffers[cpu])
1186 goto fail_free_buffers;
1187 }
1188
Steven Rostedt59222ef2009-03-12 11:46:03 -04001189#ifdef CONFIG_HOTPLUG_CPU
Steven Rostedt554f7862009-03-11 22:00:13 -04001190 buffer->cpu_notify.notifier_call = rb_cpu_notify;
1191 buffer->cpu_notify.priority = 0;
1192 register_cpu_notifier(&buffer->cpu_notify);
1193#endif
1194
1195 put_online_cpus();
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001196 mutex_init(&buffer->mutex);
1197
1198 return buffer;
1199
1200 fail_free_buffers:
1201 for_each_buffer_cpu(buffer, cpu) {
1202 if (buffer->buffers[cpu])
1203 rb_free_cpu_buffer(buffer->buffers[cpu]);
1204 }
1205 kfree(buffer->buffers);
1206
Rusty Russell9e01c1b2009-01-01 10:12:22 +10301207 fail_free_cpumask:
1208 free_cpumask_var(buffer->cpumask);
Steven Rostedt554f7862009-03-11 22:00:13 -04001209 put_online_cpus();
Rusty Russell9e01c1b2009-01-01 10:12:22 +10301210
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001211 fail_free_buffer:
1212 kfree(buffer);
1213 return NULL;
1214}
Peter Zijlstra1f8a6a12009-06-08 18:18:39 +02001215EXPORT_SYMBOL_GPL(__ring_buffer_alloc);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001216
1217/**
1218 * ring_buffer_free - free a ring buffer.
1219 * @buffer: the buffer to free.
1220 */
1221void
1222ring_buffer_free(struct ring_buffer *buffer)
1223{
1224 int cpu;
1225
Steven Rostedt554f7862009-03-11 22:00:13 -04001226 get_online_cpus();
1227
Steven Rostedt59222ef2009-03-12 11:46:03 -04001228#ifdef CONFIG_HOTPLUG_CPU
Steven Rostedt554f7862009-03-11 22:00:13 -04001229 unregister_cpu_notifier(&buffer->cpu_notify);
1230#endif
1231
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001232 for_each_buffer_cpu(buffer, cpu)
1233 rb_free_cpu_buffer(buffer->buffers[cpu]);
1234
Steven Rostedt554f7862009-03-11 22:00:13 -04001235 put_online_cpus();
1236
Eric Dumazetbd3f0222009-08-07 12:49:29 +02001237 kfree(buffer->buffers);
Rusty Russell9e01c1b2009-01-01 10:12:22 +10301238 free_cpumask_var(buffer->cpumask);
1239
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001240 kfree(buffer);
1241}
Robert Richterc4f50182008-12-11 16:49:22 +01001242EXPORT_SYMBOL_GPL(ring_buffer_free);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001243
Steven Rostedt37886f62009-03-17 17:22:06 -04001244void ring_buffer_set_clock(struct ring_buffer *buffer,
1245 u64 (*clock)(void))
1246{
1247 buffer->clock = clock;
1248}
1249
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001250static void rb_reset_cpu(struct ring_buffer_per_cpu *cpu_buffer);
1251
1252static void
1253rb_remove_pages(struct ring_buffer_per_cpu *cpu_buffer, unsigned nr_pages)
1254{
Steven Rostedt044fa782008-12-02 23:50:03 -05001255 struct buffer_page *bpage;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001256 struct list_head *p;
1257 unsigned i;
1258
Lai Jiangshanf7112942009-11-03 19:42:45 +08001259 spin_lock_irq(&cpu_buffer->reader_lock);
Steven Rostedt77ae3652009-03-27 11:00:29 -04001260 rb_head_page_deactivate(cpu_buffer);
1261
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001262 for (i = 0; i < nr_pages; i++) {
Steven Rostedt3adc54f2009-03-30 15:32:01 -04001263 if (RB_WARN_ON(cpu_buffer, list_empty(cpu_buffer->pages)))
Julia Lawall292f60c2010-03-29 17:37:02 +02001264 goto out;
Steven Rostedt3adc54f2009-03-30 15:32:01 -04001265 p = cpu_buffer->pages->next;
Steven Rostedt044fa782008-12-02 23:50:03 -05001266 bpage = list_entry(p, struct buffer_page, list);
1267 list_del_init(&bpage->list);
1268 free_buffer_page(bpage);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001269 }
Steven Rostedt3adc54f2009-03-30 15:32:01 -04001270 if (RB_WARN_ON(cpu_buffer, list_empty(cpu_buffer->pages)))
Julia Lawall292f60c2010-03-29 17:37:02 +02001271 goto out;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001272
1273 rb_reset_cpu(cpu_buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001274 rb_check_pages(cpu_buffer);
1275
Julia Lawall292f60c2010-03-29 17:37:02 +02001276out:
Steven Rostedtdd7f5942009-12-10 23:20:52 -05001277 spin_unlock_irq(&cpu_buffer->reader_lock);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001278}
1279
1280static void
1281rb_insert_pages(struct ring_buffer_per_cpu *cpu_buffer,
1282 struct list_head *pages, unsigned nr_pages)
1283{
Steven Rostedt044fa782008-12-02 23:50:03 -05001284 struct buffer_page *bpage;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001285 struct list_head *p;
1286 unsigned i;
1287
Steven Rostedt77ae3652009-03-27 11:00:29 -04001288 spin_lock_irq(&cpu_buffer->reader_lock);
1289 rb_head_page_deactivate(cpu_buffer);
1290
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001291 for (i = 0; i < nr_pages; i++) {
Steven Rostedt3e89c7b2008-11-11 15:28:41 -05001292 if (RB_WARN_ON(cpu_buffer, list_empty(pages)))
Julia Lawall292f60c2010-03-29 17:37:02 +02001293 goto out;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001294 p = pages->next;
Steven Rostedt044fa782008-12-02 23:50:03 -05001295 bpage = list_entry(p, struct buffer_page, list);
1296 list_del_init(&bpage->list);
Steven Rostedt3adc54f2009-03-30 15:32:01 -04001297 list_add_tail(&bpage->list, cpu_buffer->pages);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001298 }
1299 rb_reset_cpu(cpu_buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001300 rb_check_pages(cpu_buffer);
1301
Julia Lawall292f60c2010-03-29 17:37:02 +02001302out:
Steven Rostedtdd7f5942009-12-10 23:20:52 -05001303 spin_unlock_irq(&cpu_buffer->reader_lock);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001304}
1305
1306/**
1307 * ring_buffer_resize - resize the ring buffer
1308 * @buffer: the buffer to resize.
1309 * @size: the new size.
1310 *
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001311 * Minimum size is 2 * BUF_PAGE_SIZE.
1312 *
1313 * Returns -1 on failure.
1314 */
1315int ring_buffer_resize(struct ring_buffer *buffer, unsigned long size)
1316{
1317 struct ring_buffer_per_cpu *cpu_buffer;
1318 unsigned nr_pages, rm_pages, new_pages;
Steven Rostedt044fa782008-12-02 23:50:03 -05001319 struct buffer_page *bpage, *tmp;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001320 unsigned long buffer_size;
1321 unsigned long addr;
1322 LIST_HEAD(pages);
1323 int i, cpu;
1324
Ingo Molnaree51a1d2008-11-13 14:58:31 +01001325 /*
1326 * Always succeed at resizing a non-existent buffer:
1327 */
1328 if (!buffer)
1329 return size;
1330
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001331 size = DIV_ROUND_UP(size, BUF_PAGE_SIZE);
1332 size *= BUF_PAGE_SIZE;
1333 buffer_size = buffer->pages * BUF_PAGE_SIZE;
1334
1335 /* we need a minimum of two pages */
1336 if (size < BUF_PAGE_SIZE * 2)
1337 size = BUF_PAGE_SIZE * 2;
1338
1339 if (size == buffer_size)
1340 return size;
1341
Steven Rostedt18421012009-12-10 22:54:27 -05001342 atomic_inc(&buffer->record_disabled);
1343
1344 /* Make sure all writers are done with this buffer. */
1345 synchronize_sched();
1346
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001347 mutex_lock(&buffer->mutex);
Steven Rostedt554f7862009-03-11 22:00:13 -04001348 get_online_cpus();
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001349
1350 nr_pages = DIV_ROUND_UP(size, BUF_PAGE_SIZE);
1351
1352 if (size < buffer_size) {
1353
1354 /* easy case, just free pages */
Steven Rostedt554f7862009-03-11 22:00:13 -04001355 if (RB_WARN_ON(buffer, nr_pages >= buffer->pages))
1356 goto out_fail;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001357
1358 rm_pages = buffer->pages - nr_pages;
1359
1360 for_each_buffer_cpu(buffer, cpu) {
1361 cpu_buffer = buffer->buffers[cpu];
1362 rb_remove_pages(cpu_buffer, rm_pages);
1363 }
1364 goto out;
1365 }
1366
1367 /*
1368 * This is a bit more difficult. We only want to add pages
1369 * when we can allocate enough for all CPUs. We do this
1370 * by allocating all the pages and storing them on a local
1371 * link list. If we succeed in our allocation, then we
1372 * add these pages to the cpu_buffers. Otherwise we just free
1373 * them all and return -ENOMEM;
1374 */
Steven Rostedt554f7862009-03-11 22:00:13 -04001375 if (RB_WARN_ON(buffer, nr_pages <= buffer->pages))
1376 goto out_fail;
Steven Rostedtf536aaf2008-11-10 23:07:30 -05001377
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001378 new_pages = nr_pages - buffer->pages;
1379
1380 for_each_buffer_cpu(buffer, cpu) {
1381 for (i = 0; i < new_pages; i++) {
Steven Rostedt044fa782008-12-02 23:50:03 -05001382 bpage = kzalloc_node(ALIGN(sizeof(*bpage),
Steven Rostedte4c2ce82008-10-01 11:14:54 -04001383 cache_line_size()),
1384 GFP_KERNEL, cpu_to_node(cpu));
Steven Rostedt044fa782008-12-02 23:50:03 -05001385 if (!bpage)
Steven Rostedte4c2ce82008-10-01 11:14:54 -04001386 goto free_pages;
Steven Rostedt044fa782008-12-02 23:50:03 -05001387 list_add(&bpage->list, &pages);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001388 addr = __get_free_page(GFP_KERNEL);
1389 if (!addr)
1390 goto free_pages;
Steven Rostedt044fa782008-12-02 23:50:03 -05001391 bpage->page = (void *)addr;
1392 rb_init_page(bpage->page);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001393 }
1394 }
1395
1396 for_each_buffer_cpu(buffer, cpu) {
1397 cpu_buffer = buffer->buffers[cpu];
1398 rb_insert_pages(cpu_buffer, &pages, new_pages);
1399 }
1400
Steven Rostedt554f7862009-03-11 22:00:13 -04001401 if (RB_WARN_ON(buffer, !list_empty(&pages)))
1402 goto out_fail;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001403
1404 out:
1405 buffer->pages = nr_pages;
Steven Rostedt554f7862009-03-11 22:00:13 -04001406 put_online_cpus();
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001407 mutex_unlock(&buffer->mutex);
1408
Steven Rostedt18421012009-12-10 22:54:27 -05001409 atomic_dec(&buffer->record_disabled);
1410
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001411 return size;
1412
1413 free_pages:
Steven Rostedt044fa782008-12-02 23:50:03 -05001414 list_for_each_entry_safe(bpage, tmp, &pages, list) {
1415 list_del_init(&bpage->list);
1416 free_buffer_page(bpage);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001417 }
Steven Rostedt554f7862009-03-11 22:00:13 -04001418 put_online_cpus();
Vegard Nossum641d2f62008-11-18 19:22:13 +01001419 mutex_unlock(&buffer->mutex);
Steven Rostedt18421012009-12-10 22:54:27 -05001420 atomic_dec(&buffer->record_disabled);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001421 return -ENOMEM;
Steven Rostedt554f7862009-03-11 22:00:13 -04001422
1423 /*
1424 * Something went totally wrong, and we are too paranoid
1425 * to even clean up the mess.
1426 */
1427 out_fail:
1428 put_online_cpus();
1429 mutex_unlock(&buffer->mutex);
Steven Rostedt18421012009-12-10 22:54:27 -05001430 atomic_dec(&buffer->record_disabled);
Steven Rostedt554f7862009-03-11 22:00:13 -04001431 return -1;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001432}
Robert Richterc4f50182008-12-11 16:49:22 +01001433EXPORT_SYMBOL_GPL(ring_buffer_resize);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001434
Steven Rostedt8789a9e2008-12-02 15:34:07 -05001435static inline void *
Steven Rostedt044fa782008-12-02 23:50:03 -05001436__rb_data_page_index(struct buffer_data_page *bpage, unsigned index)
Steven Rostedt8789a9e2008-12-02 15:34:07 -05001437{
Steven Rostedt044fa782008-12-02 23:50:03 -05001438 return bpage->data + index;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05001439}
1440
Steven Rostedt044fa782008-12-02 23:50:03 -05001441static inline void *__rb_page_index(struct buffer_page *bpage, unsigned index)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001442{
Steven Rostedt044fa782008-12-02 23:50:03 -05001443 return bpage->page->data + index;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001444}
1445
1446static inline struct ring_buffer_event *
Steven Rostedtd7690412008-10-01 00:29:53 -04001447rb_reader_event(struct ring_buffer_per_cpu *cpu_buffer)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001448{
Steven Rostedt6f807ac2008-10-04 02:00:58 -04001449 return __rb_page_index(cpu_buffer->reader_page,
1450 cpu_buffer->reader_page->read);
1451}
1452
1453static inline struct ring_buffer_event *
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001454rb_iter_head_event(struct ring_buffer_iter *iter)
1455{
Steven Rostedt6f807ac2008-10-04 02:00:58 -04001456 return __rb_page_index(iter->head_page, iter->head);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001457}
1458
Steven Rostedt77ae3652009-03-27 11:00:29 -04001459static inline unsigned long rb_page_write(struct buffer_page *bpage)
Steven Rostedtbf41a152008-10-04 02:00:59 -04001460{
Steven Rostedt77ae3652009-03-27 11:00:29 -04001461 return local_read(&bpage->write) & RB_WRITE_MASK;
Steven Rostedtbf41a152008-10-04 02:00:59 -04001462}
1463
1464static inline unsigned rb_page_commit(struct buffer_page *bpage)
1465{
Steven Rostedtabc9b562008-12-02 15:34:06 -05001466 return local_read(&bpage->page->commit);
Steven Rostedtbf41a152008-10-04 02:00:59 -04001467}
1468
Steven Rostedt77ae3652009-03-27 11:00:29 -04001469static inline unsigned long rb_page_entries(struct buffer_page *bpage)
1470{
1471 return local_read(&bpage->entries) & RB_WRITE_MASK;
1472}
1473
Steven Rostedtbf41a152008-10-04 02:00:59 -04001474/* Size is determined by what has been commited */
1475static inline unsigned rb_page_size(struct buffer_page *bpage)
1476{
1477 return rb_page_commit(bpage);
1478}
1479
1480static inline unsigned
1481rb_commit_index(struct ring_buffer_per_cpu *cpu_buffer)
1482{
1483 return rb_page_commit(cpu_buffer->commit_page);
1484}
1485
Steven Rostedtbf41a152008-10-04 02:00:59 -04001486static inline unsigned
1487rb_event_index(struct ring_buffer_event *event)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001488{
Steven Rostedtbf41a152008-10-04 02:00:59 -04001489 unsigned long addr = (unsigned long)event;
1490
Steven Rostedt22f470f2009-06-11 09:29:58 -04001491 return (addr & ~PAGE_MASK) - BUF_PAGE_HDR_SIZE;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001492}
1493
Steven Rostedt0f0c85f2009-05-11 16:08:00 -04001494static inline int
Steven Rostedtfa743952009-06-16 12:37:57 -04001495rb_event_is_commit(struct ring_buffer_per_cpu *cpu_buffer,
1496 struct ring_buffer_event *event)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001497{
Steven Rostedtbf41a152008-10-04 02:00:59 -04001498 unsigned long addr = (unsigned long)event;
1499 unsigned long index;
1500
1501 index = rb_event_index(event);
1502 addr &= PAGE_MASK;
1503
1504 return cpu_buffer->commit_page->page == (void *)addr &&
1505 rb_commit_index(cpu_buffer) == index;
1506}
1507
Andrew Morton34a148b2009-01-09 12:27:09 -08001508static void
Steven Rostedtbf41a152008-10-04 02:00:59 -04001509rb_set_commit_to_write(struct ring_buffer_per_cpu *cpu_buffer)
1510{
Steven Rostedt77ae3652009-03-27 11:00:29 -04001511 unsigned long max_count;
1512
Steven Rostedtbf41a152008-10-04 02:00:59 -04001513 /*
1514 * We only race with interrupts and NMIs on this CPU.
1515 * If we own the commit event, then we can commit
1516 * all others that interrupted us, since the interruptions
1517 * are in stack format (they finish before they come
1518 * back to us). This allows us to do a simple loop to
1519 * assign the commit to the tail.
1520 */
Steven Rostedta8ccf1d2008-12-23 11:32:24 -05001521 again:
Steven Rostedt77ae3652009-03-27 11:00:29 -04001522 max_count = cpu_buffer->buffer->pages * 100;
1523
Steven Rostedtbf41a152008-10-04 02:00:59 -04001524 while (cpu_buffer->commit_page != cpu_buffer->tail_page) {
Steven Rostedt77ae3652009-03-27 11:00:29 -04001525 if (RB_WARN_ON(cpu_buffer, !(--max_count)))
1526 return;
1527 if (RB_WARN_ON(cpu_buffer,
1528 rb_is_reader_page(cpu_buffer->tail_page)))
1529 return;
1530 local_set(&cpu_buffer->commit_page->page->commit,
1531 rb_page_write(cpu_buffer->commit_page));
Steven Rostedtbf41a152008-10-04 02:00:59 -04001532 rb_inc_page(cpu_buffer, &cpu_buffer->commit_page);
Steven Rostedtabc9b562008-12-02 15:34:06 -05001533 cpu_buffer->write_stamp =
1534 cpu_buffer->commit_page->page->time_stamp;
Steven Rostedtbf41a152008-10-04 02:00:59 -04001535 /* add barrier to keep gcc from optimizing too much */
1536 barrier();
1537 }
1538 while (rb_commit_index(cpu_buffer) !=
1539 rb_page_write(cpu_buffer->commit_page)) {
Steven Rostedt77ae3652009-03-27 11:00:29 -04001540
1541 local_set(&cpu_buffer->commit_page->page->commit,
1542 rb_page_write(cpu_buffer->commit_page));
1543 RB_WARN_ON(cpu_buffer,
1544 local_read(&cpu_buffer->commit_page->page->commit) &
1545 ~RB_WRITE_MASK);
Steven Rostedtbf41a152008-10-04 02:00:59 -04001546 barrier();
1547 }
Steven Rostedta8ccf1d2008-12-23 11:32:24 -05001548
1549 /* again, keep gcc from optimizing */
1550 barrier();
1551
1552 /*
1553 * If an interrupt came in just after the first while loop
1554 * and pushed the tail page forward, we will be left with
1555 * a dangling commit that will never go forward.
1556 */
1557 if (unlikely(cpu_buffer->commit_page != cpu_buffer->tail_page))
1558 goto again;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001559}
1560
Steven Rostedtd7690412008-10-01 00:29:53 -04001561static void rb_reset_reader_page(struct ring_buffer_per_cpu *cpu_buffer)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001562{
Steven Rostedtabc9b562008-12-02 15:34:06 -05001563 cpu_buffer->read_stamp = cpu_buffer->reader_page->page->time_stamp;
Steven Rostedt6f807ac2008-10-04 02:00:58 -04001564 cpu_buffer->reader_page->read = 0;
Steven Rostedtd7690412008-10-01 00:29:53 -04001565}
1566
Andrew Morton34a148b2009-01-09 12:27:09 -08001567static void rb_inc_iter(struct ring_buffer_iter *iter)
Steven Rostedtd7690412008-10-01 00:29:53 -04001568{
1569 struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
1570
1571 /*
1572 * The iterator could be on the reader page (it starts there).
1573 * But the head could have moved, since the reader was
1574 * found. Check for this case and assign the iterator
1575 * to the head page instead of next.
1576 */
1577 if (iter->head_page == cpu_buffer->reader_page)
Steven Rostedt77ae3652009-03-27 11:00:29 -04001578 iter->head_page = rb_set_head_page(cpu_buffer);
Steven Rostedtd7690412008-10-01 00:29:53 -04001579 else
1580 rb_inc_page(cpu_buffer, &iter->head_page);
1581
Steven Rostedtabc9b562008-12-02 15:34:06 -05001582 iter->read_stamp = iter->head_page->page->time_stamp;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001583 iter->head = 0;
1584}
1585
Steven Rostedt69d1b832010-10-07 18:18:05 -04001586/* Slow path, do not inline */
1587static noinline struct ring_buffer_event *
1588rb_add_time_stamp(struct ring_buffer_event *event, u64 delta)
1589{
1590 event->type_len = RINGBUF_TYPE_TIME_EXTEND;
1591
1592 /* Not the first event on the page? */
1593 if (rb_event_index(event)) {
1594 event->time_delta = delta & TS_MASK;
1595 event->array[0] = delta >> TS_SHIFT;
1596 } else {
1597 /* nope, just zero it */
1598 event->time_delta = 0;
1599 event->array[0] = 0;
1600 }
1601
1602 return skip_time_extend(event);
1603}
1604
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001605/**
1606 * ring_buffer_update_event - update event type and data
1607 * @event: the even to update
1608 * @type: the type of event
1609 * @length: the size of the event field in the ring buffer
1610 *
1611 * Update the type and data fields of the event. The length
1612 * is the actual size that is written to the ring buffer,
1613 * and with this, we can determine what to place into the
1614 * data field.
1615 */
Andrew Morton34a148b2009-01-09 12:27:09 -08001616static void
Steven Rostedt69d1b832010-10-07 18:18:05 -04001617rb_update_event(struct ring_buffer_per_cpu *cpu_buffer,
1618 struct ring_buffer_event *event, unsigned length,
1619 int add_timestamp, u64 delta)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001620{
Steven Rostedt69d1b832010-10-07 18:18:05 -04001621 /* Only a commit updates the timestamp */
1622 if (unlikely(!rb_event_is_commit(cpu_buffer, event)))
1623 delta = 0;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001624
Steven Rostedt69d1b832010-10-07 18:18:05 -04001625 /*
1626 * If we need to add a timestamp, then we
1627 * add it to the start of the resevered space.
1628 */
1629 if (unlikely(add_timestamp)) {
1630 event = rb_add_time_stamp(event, delta);
1631 length -= RB_LEN_TIME_EXTEND;
1632 delta = 0;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001633 }
Steven Rostedt69d1b832010-10-07 18:18:05 -04001634
1635 event->time_delta = delta;
1636 length -= RB_EVNT_HDR_SIZE;
1637 if (length > RB_MAX_SMALL_DATA || RB_FORCE_8BYTE_ALIGNMENT) {
1638 event->type_len = 0;
1639 event->array[0] = length;
1640 } else
1641 event->type_len = DIV_ROUND_UP(length, RB_ALIGNMENT);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001642}
1643
Steven Rostedt77ae3652009-03-27 11:00:29 -04001644/*
1645 * rb_handle_head_page - writer hit the head page
1646 *
1647 * Returns: +1 to retry page
1648 * 0 to continue
1649 * -1 on error
1650 */
1651static int
1652rb_handle_head_page(struct ring_buffer_per_cpu *cpu_buffer,
1653 struct buffer_page *tail_page,
1654 struct buffer_page *next_page)
1655{
1656 struct buffer_page *new_head;
1657 int entries;
1658 int type;
1659 int ret;
1660
1661 entries = rb_page_entries(next_page);
1662
1663 /*
1664 * The hard part is here. We need to move the head
1665 * forward, and protect against both readers on
1666 * other CPUs and writers coming in via interrupts.
1667 */
1668 type = rb_head_page_set_update(cpu_buffer, next_page, tail_page,
1669 RB_PAGE_HEAD);
1670
1671 /*
1672 * type can be one of four:
1673 * NORMAL - an interrupt already moved it for us
1674 * HEAD - we are the first to get here.
1675 * UPDATE - we are the interrupt interrupting
1676 * a current move.
1677 * MOVED - a reader on another CPU moved the next
1678 * pointer to its reader page. Give up
1679 * and try again.
1680 */
1681
1682 switch (type) {
1683 case RB_PAGE_HEAD:
1684 /*
1685 * We changed the head to UPDATE, thus
1686 * it is our responsibility to update
1687 * the counters.
1688 */
1689 local_add(entries, &cpu_buffer->overrun);
1690
1691 /*
1692 * The entries will be zeroed out when we move the
1693 * tail page.
1694 */
1695
1696 /* still more to do */
1697 break;
1698
1699 case RB_PAGE_UPDATE:
1700 /*
1701 * This is an interrupt that interrupt the
1702 * previous update. Still more to do.
1703 */
1704 break;
1705 case RB_PAGE_NORMAL:
1706 /*
1707 * An interrupt came in before the update
1708 * and processed this for us.
1709 * Nothing left to do.
1710 */
1711 return 1;
1712 case RB_PAGE_MOVED:
1713 /*
1714 * The reader is on another CPU and just did
1715 * a swap with our next_page.
1716 * Try again.
1717 */
1718 return 1;
1719 default:
1720 RB_WARN_ON(cpu_buffer, 1); /* WTF??? */
1721 return -1;
1722 }
1723
1724 /*
1725 * Now that we are here, the old head pointer is
1726 * set to UPDATE. This will keep the reader from
1727 * swapping the head page with the reader page.
1728 * The reader (on another CPU) will spin till
1729 * we are finished.
1730 *
1731 * We just need to protect against interrupts
1732 * doing the job. We will set the next pointer
1733 * to HEAD. After that, we set the old pointer
1734 * to NORMAL, but only if it was HEAD before.
1735 * otherwise we are an interrupt, and only
1736 * want the outer most commit to reset it.
1737 */
1738 new_head = next_page;
1739 rb_inc_page(cpu_buffer, &new_head);
1740
1741 ret = rb_head_page_set_head(cpu_buffer, new_head, next_page,
1742 RB_PAGE_NORMAL);
1743
1744 /*
1745 * Valid returns are:
1746 * HEAD - an interrupt came in and already set it.
1747 * NORMAL - One of two things:
1748 * 1) We really set it.
1749 * 2) A bunch of interrupts came in and moved
1750 * the page forward again.
1751 */
1752 switch (ret) {
1753 case RB_PAGE_HEAD:
1754 case RB_PAGE_NORMAL:
1755 /* OK */
1756 break;
1757 default:
1758 RB_WARN_ON(cpu_buffer, 1);
1759 return -1;
1760 }
1761
1762 /*
1763 * It is possible that an interrupt came in,
1764 * set the head up, then more interrupts came in
1765 * and moved it again. When we get back here,
1766 * the page would have been set to NORMAL but we
1767 * just set it back to HEAD.
1768 *
1769 * How do you detect this? Well, if that happened
1770 * the tail page would have moved.
1771 */
1772 if (ret == RB_PAGE_NORMAL) {
1773 /*
1774 * If the tail had moved passed next, then we need
1775 * to reset the pointer.
1776 */
1777 if (cpu_buffer->tail_page != tail_page &&
1778 cpu_buffer->tail_page != next_page)
1779 rb_head_page_set_normal(cpu_buffer, new_head,
1780 next_page,
1781 RB_PAGE_HEAD);
1782 }
1783
1784 /*
1785 * If this was the outer most commit (the one that
1786 * changed the original pointer from HEAD to UPDATE),
1787 * then it is up to us to reset it to NORMAL.
1788 */
1789 if (type == RB_PAGE_HEAD) {
1790 ret = rb_head_page_set_normal(cpu_buffer, next_page,
1791 tail_page,
1792 RB_PAGE_UPDATE);
1793 if (RB_WARN_ON(cpu_buffer,
1794 ret != RB_PAGE_UPDATE))
1795 return -1;
1796 }
1797
1798 return 0;
1799}
1800
Andrew Morton34a148b2009-01-09 12:27:09 -08001801static unsigned rb_calculate_event_length(unsigned length)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001802{
1803 struct ring_buffer_event event; /* Used only for sizeof array */
1804
1805 /* zero length can cause confusions */
1806 if (!length)
1807 length = 1;
1808
Steven Rostedt22710482010-03-18 17:54:19 -04001809 if (length > RB_MAX_SMALL_DATA || RB_FORCE_8BYTE_ALIGNMENT)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001810 length += sizeof(event.array[0]);
1811
1812 length += RB_EVNT_HDR_SIZE;
Steven Rostedt22710482010-03-18 17:54:19 -04001813 length = ALIGN(length, RB_ARCH_ALIGNMENT);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001814
1815 return length;
1816}
1817
Steven Rostedtc7b09302009-06-11 11:12:00 -04001818static inline void
1819rb_reset_tail(struct ring_buffer_per_cpu *cpu_buffer,
1820 struct buffer_page *tail_page,
1821 unsigned long tail, unsigned long length)
1822{
1823 struct ring_buffer_event *event;
1824
1825 /*
1826 * Only the event that crossed the page boundary
1827 * must fill the old tail_page with padding.
1828 */
1829 if (tail >= BUF_PAGE_SIZE) {
Steven Rostedtb3230c82010-05-21 11:55:21 -04001830 /*
1831 * If the page was filled, then we still need
1832 * to update the real_end. Reset it to zero
1833 * and the reader will ignore it.
1834 */
1835 if (tail == BUF_PAGE_SIZE)
1836 tail_page->real_end = 0;
1837
Steven Rostedtc7b09302009-06-11 11:12:00 -04001838 local_sub(length, &tail_page->write);
1839 return;
1840 }
1841
1842 event = __rb_page_index(tail_page, tail);
Linus Torvaldsb0b70652009-06-20 10:56:46 -07001843 kmemcheck_annotate_bitfield(event, bitfield);
Steven Rostedtc7b09302009-06-11 11:12:00 -04001844
1845 /*
Steven Rostedtff0ff842010-03-31 22:11:42 -04001846 * Save the original length to the meta data.
1847 * This will be used by the reader to add lost event
1848 * counter.
1849 */
1850 tail_page->real_end = tail;
1851
1852 /*
Steven Rostedtc7b09302009-06-11 11:12:00 -04001853 * If this event is bigger than the minimum size, then
1854 * we need to be careful that we don't subtract the
1855 * write counter enough to allow another writer to slip
1856 * in on this page.
1857 * We put in a discarded commit instead, to make sure
1858 * that this space is not used again.
1859 *
1860 * If we are less than the minimum size, we don't need to
1861 * worry about it.
1862 */
1863 if (tail > (BUF_PAGE_SIZE - RB_EVNT_MIN_SIZE)) {
1864 /* No room for any events */
1865
1866 /* Mark the rest of the page with padding */
1867 rb_event_set_padding(event);
1868
1869 /* Set the write back to the previous setting */
1870 local_sub(length, &tail_page->write);
1871 return;
1872 }
1873
1874 /* Put in a discarded event */
1875 event->array[0] = (BUF_PAGE_SIZE - tail) - RB_EVNT_HDR_SIZE;
1876 event->type_len = RINGBUF_TYPE_PADDING;
1877 /* time delta must be non zero */
1878 event->time_delta = 1;
Steven Rostedtc7b09302009-06-11 11:12:00 -04001879
1880 /* Set write to end of buffer */
1881 length = (tail + length) - BUF_PAGE_SIZE;
1882 local_sub(length, &tail_page->write);
1883}
Steven Rostedt6634ff22009-05-06 15:30:07 -04001884
Steven Rostedt747e94a2010-10-08 13:51:48 -04001885/*
1886 * This is the slow path, force gcc not to inline it.
1887 */
1888static noinline struct ring_buffer_event *
Steven Rostedt6634ff22009-05-06 15:30:07 -04001889rb_move_tail(struct ring_buffer_per_cpu *cpu_buffer,
1890 unsigned long length, unsigned long tail,
Steven Rostedte8bc43e2010-10-20 10:58:02 -04001891 struct buffer_page *tail_page, u64 ts)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001892{
Steven Rostedt5a50e332009-11-17 08:43:01 -05001893 struct buffer_page *commit_page = cpu_buffer->commit_page;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001894 struct ring_buffer *buffer = cpu_buffer->buffer;
Steven Rostedt77ae3652009-03-27 11:00:29 -04001895 struct buffer_page *next_page;
1896 int ret;
Steven Rostedtaa20ae82009-05-05 21:16:11 -04001897
1898 next_page = tail_page;
1899
Steven Rostedtaa20ae82009-05-05 21:16:11 -04001900 rb_inc_page(cpu_buffer, &next_page);
1901
Steven Rostedtaa20ae82009-05-05 21:16:11 -04001902 /*
1903 * If for some reason, we had an interrupt storm that made
1904 * it all the way around the buffer, bail, and warn
1905 * about it.
1906 */
1907 if (unlikely(next_page == commit_page)) {
Steven Rostedt77ae3652009-03-27 11:00:29 -04001908 local_inc(&cpu_buffer->commit_overrun);
Steven Rostedtaa20ae82009-05-05 21:16:11 -04001909 goto out_reset;
1910 }
1911
Steven Rostedt77ae3652009-03-27 11:00:29 -04001912 /*
1913 * This is where the fun begins!
1914 *
1915 * We are fighting against races between a reader that
1916 * could be on another CPU trying to swap its reader
1917 * page with the buffer head.
1918 *
1919 * We are also fighting against interrupts coming in and
1920 * moving the head or tail on us as well.
1921 *
1922 * If the next page is the head page then we have filled
1923 * the buffer, unless the commit page is still on the
1924 * reader page.
1925 */
1926 if (rb_is_head_page(cpu_buffer, next_page, &tail_page->list)) {
Steven Rostedtaa20ae82009-05-05 21:16:11 -04001927
Steven Rostedt77ae3652009-03-27 11:00:29 -04001928 /*
1929 * If the commit is not on the reader page, then
1930 * move the header page.
1931 */
1932 if (!rb_is_reader_page(cpu_buffer->commit_page)) {
1933 /*
1934 * If we are not in overwrite mode,
1935 * this is easy, just stop here.
1936 */
1937 if (!(buffer->flags & RB_FL_OVERWRITE))
1938 goto out_reset;
Steven Rostedtaa20ae82009-05-05 21:16:11 -04001939
Steven Rostedt77ae3652009-03-27 11:00:29 -04001940 ret = rb_handle_head_page(cpu_buffer,
1941 tail_page,
1942 next_page);
1943 if (ret < 0)
1944 goto out_reset;
1945 if (ret)
1946 goto out_again;
1947 } else {
1948 /*
1949 * We need to be careful here too. The
1950 * commit page could still be on the reader
1951 * page. We could have a small buffer, and
1952 * have filled up the buffer with events
1953 * from interrupts and such, and wrapped.
1954 *
1955 * Note, if the tail page is also the on the
1956 * reader_page, we let it move out.
1957 */
1958 if (unlikely((cpu_buffer->commit_page !=
1959 cpu_buffer->tail_page) &&
1960 (cpu_buffer->commit_page ==
1961 cpu_buffer->reader_page))) {
1962 local_inc(&cpu_buffer->commit_overrun);
1963 goto out_reset;
1964 }
Steven Rostedtaa20ae82009-05-05 21:16:11 -04001965 }
1966 }
1967
Steven Rostedt77ae3652009-03-27 11:00:29 -04001968 ret = rb_tail_page_update(cpu_buffer, tail_page, next_page);
1969 if (ret) {
1970 /*
1971 * Nested commits always have zero deltas, so
1972 * just reread the time stamp
1973 */
Steven Rostedte8bc43e2010-10-20 10:58:02 -04001974 ts = rb_time_stamp(buffer);
1975 next_page->page->time_stamp = ts;
Steven Rostedtaa20ae82009-05-05 21:16:11 -04001976 }
1977
Steven Rostedt77ae3652009-03-27 11:00:29 -04001978 out_again:
Steven Rostedtaa20ae82009-05-05 21:16:11 -04001979
Steven Rostedt77ae3652009-03-27 11:00:29 -04001980 rb_reset_tail(cpu_buffer, tail_page, tail, length);
Steven Rostedtaa20ae82009-05-05 21:16:11 -04001981
1982 /* fail and let the caller try again */
1983 return ERR_PTR(-EAGAIN);
1984
Steven Rostedt45141d42009-02-12 13:19:48 -05001985 out_reset:
Lai Jiangshan6f3b3442009-01-12 11:06:18 +08001986 /* reset write */
Steven Rostedtc7b09302009-06-11 11:12:00 -04001987 rb_reset_tail(cpu_buffer, tail_page, tail, length);
Lai Jiangshan6f3b3442009-01-12 11:06:18 +08001988
Steven Rostedtbf41a152008-10-04 02:00:59 -04001989 return NULL;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001990}
1991
Steven Rostedt6634ff22009-05-06 15:30:07 -04001992static struct ring_buffer_event *
1993__rb_reserve_next(struct ring_buffer_per_cpu *cpu_buffer,
Steven Rostedt69d1b832010-10-07 18:18:05 -04001994 unsigned long length, u64 ts,
1995 u64 delta, int add_timestamp)
Steven Rostedt6634ff22009-05-06 15:30:07 -04001996{
Steven Rostedt5a50e332009-11-17 08:43:01 -05001997 struct buffer_page *tail_page;
Steven Rostedt6634ff22009-05-06 15:30:07 -04001998 struct ring_buffer_event *event;
1999 unsigned long tail, write;
2000
Steven Rostedt69d1b832010-10-07 18:18:05 -04002001 /*
2002 * If the time delta since the last event is too big to
2003 * hold in the time field of the event, then we append a
2004 * TIME EXTEND event ahead of the data event.
2005 */
2006 if (unlikely(add_timestamp))
2007 length += RB_LEN_TIME_EXTEND;
2008
Steven Rostedt6634ff22009-05-06 15:30:07 -04002009 tail_page = cpu_buffer->tail_page;
2010 write = local_add_return(length, &tail_page->write);
Steven Rostedt77ae3652009-03-27 11:00:29 -04002011
2012 /* set write to only the index of the write */
2013 write &= RB_WRITE_MASK;
Steven Rostedt6634ff22009-05-06 15:30:07 -04002014 tail = write - length;
2015
2016 /* See if we shot pass the end of this buffer page */
Steven Rostedt747e94a2010-10-08 13:51:48 -04002017 if (unlikely(write > BUF_PAGE_SIZE))
Steven Rostedt6634ff22009-05-06 15:30:07 -04002018 return rb_move_tail(cpu_buffer, length, tail,
Steven Rostedt5a50e332009-11-17 08:43:01 -05002019 tail_page, ts);
Steven Rostedt6634ff22009-05-06 15:30:07 -04002020
2021 /* We reserved something on the buffer */
2022
Steven Rostedt6634ff22009-05-06 15:30:07 -04002023 event = __rb_page_index(tail_page, tail);
Vegard Nossum1744a212009-02-28 08:29:44 +01002024 kmemcheck_annotate_bitfield(event, bitfield);
Steven Rostedt69d1b832010-10-07 18:18:05 -04002025 rb_update_event(cpu_buffer, event, length, add_timestamp, delta);
Steven Rostedt6634ff22009-05-06 15:30:07 -04002026
Steven Rostedt69d1b832010-10-07 18:18:05 -04002027 local_inc(&tail_page->entries);
Steven Rostedt6634ff22009-05-06 15:30:07 -04002028
2029 /*
Steven Rostedtfa743952009-06-16 12:37:57 -04002030 * If this is the first commit on the page, then update
2031 * its timestamp.
Steven Rostedt6634ff22009-05-06 15:30:07 -04002032 */
Steven Rostedtfa743952009-06-16 12:37:57 -04002033 if (!tail)
Steven Rostedte8bc43e2010-10-20 10:58:02 -04002034 tail_page->page->time_stamp = ts;
Steven Rostedt6634ff22009-05-06 15:30:07 -04002035
2036 return event;
2037}
2038
Steven Rostedtedd813bf2009-06-02 23:00:53 -04002039static inline int
2040rb_try_to_discard(struct ring_buffer_per_cpu *cpu_buffer,
2041 struct ring_buffer_event *event)
2042{
2043 unsigned long new_index, old_index;
2044 struct buffer_page *bpage;
2045 unsigned long index;
2046 unsigned long addr;
2047
2048 new_index = rb_event_index(event);
Steven Rostedt69d1b832010-10-07 18:18:05 -04002049 old_index = new_index + rb_event_ts_length(event);
Steven Rostedtedd813bf2009-06-02 23:00:53 -04002050 addr = (unsigned long)event;
2051 addr &= PAGE_MASK;
2052
2053 bpage = cpu_buffer->tail_page;
2054
2055 if (bpage->page == (void *)addr && rb_page_write(bpage) == old_index) {
Steven Rostedt77ae3652009-03-27 11:00:29 -04002056 unsigned long write_mask =
2057 local_read(&bpage->write) & ~RB_WRITE_MASK;
Steven Rostedtedd813bf2009-06-02 23:00:53 -04002058 /*
2059 * This is on the tail page. It is possible that
2060 * a write could come in and move the tail page
2061 * and write to the next page. That is fine
2062 * because we just shorten what is on this page.
2063 */
Steven Rostedt77ae3652009-03-27 11:00:29 -04002064 old_index += write_mask;
2065 new_index += write_mask;
Steven Rostedtedd813bf2009-06-02 23:00:53 -04002066 index = local_cmpxchg(&bpage->write, old_index, new_index);
2067 if (index == old_index)
2068 return 1;
2069 }
2070
2071 /* could not discard */
2072 return 0;
2073}
2074
Steven Rostedtfa743952009-06-16 12:37:57 -04002075static void rb_start_commit(struct ring_buffer_per_cpu *cpu_buffer)
2076{
2077 local_inc(&cpu_buffer->committing);
2078 local_inc(&cpu_buffer->commits);
2079}
2080
Steven Rostedtd9abde22010-10-19 13:17:08 -04002081static inline void rb_end_commit(struct ring_buffer_per_cpu *cpu_buffer)
Steven Rostedtfa743952009-06-16 12:37:57 -04002082{
2083 unsigned long commits;
2084
2085 if (RB_WARN_ON(cpu_buffer,
2086 !local_read(&cpu_buffer->committing)))
2087 return;
2088
2089 again:
2090 commits = local_read(&cpu_buffer->commits);
2091 /* synchronize with interrupts */
2092 barrier();
2093 if (local_read(&cpu_buffer->committing) == 1)
2094 rb_set_commit_to_write(cpu_buffer);
2095
2096 local_dec(&cpu_buffer->committing);
2097
2098 /* synchronize with interrupts */
2099 barrier();
2100
2101 /*
2102 * Need to account for interrupts coming in between the
2103 * updating of the commit page and the clearing of the
2104 * committing counter.
2105 */
2106 if (unlikely(local_read(&cpu_buffer->commits) != commits) &&
2107 !local_read(&cpu_buffer->committing)) {
2108 local_inc(&cpu_buffer->committing);
2109 goto again;
2110 }
2111}
2112
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002113static struct ring_buffer_event *
Steven Rostedt62f0b3e2009-09-04 14:11:34 -04002114rb_reserve_next_event(struct ring_buffer *buffer,
2115 struct ring_buffer_per_cpu *cpu_buffer,
Steven Rostedt1cd8d732009-05-11 14:08:09 -04002116 unsigned long length)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002117{
2118 struct ring_buffer_event *event;
Steven Rostedt69d1b832010-10-07 18:18:05 -04002119 u64 ts, delta;
Steven Rostedt818e3dd2008-10-31 09:58:35 -04002120 int nr_loops = 0;
Steven Rostedt69d1b832010-10-07 18:18:05 -04002121 int add_timestamp;
Steven Rostedt140ff892010-10-08 10:50:30 -04002122 u64 diff;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002123
Steven Rostedtfa743952009-06-16 12:37:57 -04002124 rb_start_commit(cpu_buffer);
2125
Steven Rostedt85bac322009-09-04 14:24:40 -04002126#ifdef CONFIG_RING_BUFFER_ALLOW_SWAP
Steven Rostedt62f0b3e2009-09-04 14:11:34 -04002127 /*
2128 * Due to the ability to swap a cpu buffer from a buffer
2129 * it is possible it was swapped before we committed.
2130 * (committing stops a swap). We check for it here and
2131 * if it happened, we have to fail the write.
2132 */
2133 barrier();
2134 if (unlikely(ACCESS_ONCE(cpu_buffer->buffer) != buffer)) {
2135 local_dec(&cpu_buffer->committing);
2136 local_dec(&cpu_buffer->commits);
2137 return NULL;
2138 }
Steven Rostedt85bac322009-09-04 14:24:40 -04002139#endif
Steven Rostedt62f0b3e2009-09-04 14:11:34 -04002140
Steven Rostedtbe957c42009-05-11 14:42:53 -04002141 length = rb_calculate_event_length(length);
Steven Rostedtbf41a152008-10-04 02:00:59 -04002142 again:
Steven Rostedt69d1b832010-10-07 18:18:05 -04002143 add_timestamp = 0;
2144 delta = 0;
2145
Steven Rostedt818e3dd2008-10-31 09:58:35 -04002146 /*
2147 * We allow for interrupts to reenter here and do a trace.
2148 * If one does, it will cause this original code to loop
2149 * back here. Even with heavy interrupts happening, this
2150 * should only happen a few times in a row. If this happens
2151 * 1000 times in a row, there must be either an interrupt
2152 * storm or we have something buggy.
2153 * Bail!
2154 */
Steven Rostedt3e89c7b2008-11-11 15:28:41 -05002155 if (RB_WARN_ON(cpu_buffer, ++nr_loops > 1000))
Steven Rostedtfa743952009-06-16 12:37:57 -04002156 goto out_fail;
Steven Rostedt818e3dd2008-10-31 09:58:35 -04002157
Jiri Olsa6d3f1e12009-10-23 19:36:19 -04002158 ts = rb_time_stamp(cpu_buffer->buffer);
Steven Rostedt140ff892010-10-08 10:50:30 -04002159 diff = ts - cpu_buffer->write_stamp;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002160
Steven Rostedt140ff892010-10-08 10:50:30 -04002161 /* make sure this diff is calculated here */
2162 barrier();
Steven Rostedtbf41a152008-10-04 02:00:59 -04002163
Steven Rostedt140ff892010-10-08 10:50:30 -04002164 /* Did the write stamp get updated already? */
2165 if (likely(ts >= cpu_buffer->write_stamp)) {
Steven Rostedt168b6b12009-05-11 22:11:05 -04002166 delta = diff;
2167 if (unlikely(test_time_stamp(delta))) {
Steven Rostedt69d1b832010-10-07 18:18:05 -04002168 WARN_ONCE(delta > (1ULL << 59),
2169 KERN_WARNING "Delta way too big! %llu ts=%llu write stamp = %llu\n",
2170 (unsigned long long)delta,
2171 (unsigned long long)ts,
2172 (unsigned long long)cpu_buffer->write_stamp);
2173 add_timestamp = 1;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002174 }
Steven Rostedt168b6b12009-05-11 22:11:05 -04002175 }
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002176
Steven Rostedt69d1b832010-10-07 18:18:05 -04002177 event = __rb_reserve_next(cpu_buffer, length, ts,
2178 delta, add_timestamp);
Steven Rostedt168b6b12009-05-11 22:11:05 -04002179 if (unlikely(PTR_ERR(event) == -EAGAIN))
Steven Rostedtbf41a152008-10-04 02:00:59 -04002180 goto again;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002181
Steven Rostedtfa743952009-06-16 12:37:57 -04002182 if (!event)
2183 goto out_fail;
Steven Rostedtbf41a152008-10-04 02:00:59 -04002184
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002185 return event;
Steven Rostedtfa743952009-06-16 12:37:57 -04002186
2187 out_fail:
2188 rb_end_commit(cpu_buffer);
2189 return NULL;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002190}
2191
Paul Mundt1155de42009-06-25 14:30:12 +09002192#ifdef CONFIG_TRACING
2193
Steven Rostedtaa18efb2009-04-20 16:16:11 -04002194#define TRACE_RECURSIVE_DEPTH 16
Steven Rostedt261842b2009-04-16 21:41:52 -04002195
Steven Rostedtd9abde22010-10-19 13:17:08 -04002196/* Keep this code out of the fast path cache */
2197static noinline void trace_recursive_fail(void)
Steven Rostedt261842b2009-04-16 21:41:52 -04002198{
Steven Rostedtaa18efb2009-04-20 16:16:11 -04002199 /* Disable all tracing before we do anything else */
2200 tracing_off_permanent();
Frederic Weisbeckere057a5e2009-04-19 23:38:12 +02002201
Steven Rostedt7d7d2b82009-04-27 12:37:49 -04002202 printk_once(KERN_WARNING "Tracing recursion: depth[%ld]:"
Steven Rostedtaa18efb2009-04-20 16:16:11 -04002203 "HC[%lu]:SC[%lu]:NMI[%lu]\n",
2204 current->trace_recursion,
2205 hardirq_count() >> HARDIRQ_SHIFT,
2206 softirq_count() >> SOFTIRQ_SHIFT,
2207 in_nmi());
Frederic Weisbeckere057a5e2009-04-19 23:38:12 +02002208
Steven Rostedtaa18efb2009-04-20 16:16:11 -04002209 WARN_ON_ONCE(1);
Steven Rostedtd9abde22010-10-19 13:17:08 -04002210}
2211
2212static inline int trace_recursive_lock(void)
2213{
2214 current->trace_recursion++;
2215
2216 if (likely(current->trace_recursion < TRACE_RECURSIVE_DEPTH))
2217 return 0;
2218
2219 trace_recursive_fail();
2220
Steven Rostedtaa18efb2009-04-20 16:16:11 -04002221 return -1;
Steven Rostedt261842b2009-04-16 21:41:52 -04002222}
2223
Steven Rostedtd9abde22010-10-19 13:17:08 -04002224static inline void trace_recursive_unlock(void)
Steven Rostedt261842b2009-04-16 21:41:52 -04002225{
Steven Rostedtaa18efb2009-04-20 16:16:11 -04002226 WARN_ON_ONCE(!current->trace_recursion);
Steven Rostedt261842b2009-04-16 21:41:52 -04002227
Steven Rostedtaa18efb2009-04-20 16:16:11 -04002228 current->trace_recursion--;
Steven Rostedt261842b2009-04-16 21:41:52 -04002229}
2230
Paul Mundt1155de42009-06-25 14:30:12 +09002231#else
2232
2233#define trace_recursive_lock() (0)
2234#define trace_recursive_unlock() do { } while (0)
2235
2236#endif
2237
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002238/**
2239 * ring_buffer_lock_reserve - reserve a part of the buffer
2240 * @buffer: the ring buffer to reserve from
2241 * @length: the length of the data to reserve (excluding event header)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002242 *
2243 * Returns a reseverd event on the ring buffer to copy directly to.
2244 * The user of this interface will need to get the body to write into
2245 * and can use the ring_buffer_event_data() interface.
2246 *
2247 * The length is the length of the data needed, not the event length
2248 * which also includes the event header.
2249 *
2250 * Must be paired with ring_buffer_unlock_commit, unless NULL is returned.
2251 * If NULL is returned, then nothing has been allocated or locked.
2252 */
2253struct ring_buffer_event *
Arnaldo Carvalho de Melo0a987752009-02-05 16:12:56 -02002254ring_buffer_lock_reserve(struct ring_buffer *buffer, unsigned long length)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002255{
2256 struct ring_buffer_per_cpu *cpu_buffer;
2257 struct ring_buffer_event *event;
Steven Rostedt5168ae52010-06-03 09:36:50 -04002258 int cpu;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002259
Steven Rostedt033601a2008-11-21 12:41:55 -05002260 if (ring_buffer_flags != RB_BUFFERS_ON)
Steven Rostedta3583242008-11-11 15:01:42 -05002261 return NULL;
2262
Steven Rostedtbf41a152008-10-04 02:00:59 -04002263 /* If we are tracing schedule, we don't want to recurse */
Steven Rostedt5168ae52010-06-03 09:36:50 -04002264 preempt_disable_notrace();
Steven Rostedtbf41a152008-10-04 02:00:59 -04002265
Lai Jiangshan52fbe9c2010-03-08 14:50:43 +08002266 if (atomic_read(&buffer->record_disabled))
2267 goto out_nocheck;
2268
Steven Rostedt261842b2009-04-16 21:41:52 -04002269 if (trace_recursive_lock())
2270 goto out_nocheck;
2271
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002272 cpu = raw_smp_processor_id();
2273
Rusty Russell9e01c1b2009-01-01 10:12:22 +10302274 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedtd7690412008-10-01 00:29:53 -04002275 goto out;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002276
2277 cpu_buffer = buffer->buffers[cpu];
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002278
2279 if (atomic_read(&cpu_buffer->record_disabled))
Steven Rostedtd7690412008-10-01 00:29:53 -04002280 goto out;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002281
Steven Rostedtbe957c42009-05-11 14:42:53 -04002282 if (length > BUF_MAX_DATA_SIZE)
Steven Rostedtbf41a152008-10-04 02:00:59 -04002283 goto out;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002284
Steven Rostedt62f0b3e2009-09-04 14:11:34 -04002285 event = rb_reserve_next_event(buffer, cpu_buffer, length);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002286 if (!event)
Steven Rostedtd7690412008-10-01 00:29:53 -04002287 goto out;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002288
2289 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 Rostedt5168ae52010-06-03 09:36:50 -04002295 preempt_enable_notrace();
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 Rostedt69d1b832010-10-07 18:18:05 -04002304 u64 delta;
2305
Steven Rostedtfa743952009-06-16 12:37:57 -04002306 /*
2307 * The event first in the commit queue updates the
2308 * time stamp.
2309 */
Steven Rostedt69d1b832010-10-07 18:18:05 -04002310 if (rb_event_is_commit(cpu_buffer, event)) {
2311 /*
2312 * A commit event that is first on a page
2313 * updates the write timestamp with the page stamp
2314 */
2315 if (!rb_event_index(event))
2316 cpu_buffer->write_stamp =
2317 cpu_buffer->commit_page->page->time_stamp;
2318 else if (event->type_len == RINGBUF_TYPE_TIME_EXTEND) {
2319 delta = event->array[0];
2320 delta <<= TS_SHIFT;
2321 delta += event->time_delta;
2322 cpu_buffer->write_stamp += delta;
2323 } else
2324 cpu_buffer->write_stamp += event->time_delta;
2325 }
Steven Rostedta1863c22009-09-03 10:23:58 -04002326}
Steven Rostedtbf41a152008-10-04 02:00:59 -04002327
Steven Rostedta1863c22009-09-03 10:23:58 -04002328static void rb_commit(struct ring_buffer_per_cpu *cpu_buffer,
2329 struct ring_buffer_event *event)
2330{
2331 local_inc(&cpu_buffer->entries);
2332 rb_update_write_stamp(cpu_buffer, event);
Steven Rostedtfa743952009-06-16 12:37:57 -04002333 rb_end_commit(cpu_buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002334}
2335
2336/**
2337 * ring_buffer_unlock_commit - commit a reserved
2338 * @buffer: The buffer to commit to
2339 * @event: The event pointer to commit.
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002340 *
2341 * This commits the data to the ring buffer, and releases any locks held.
2342 *
2343 * Must be paired with ring_buffer_lock_reserve.
2344 */
2345int ring_buffer_unlock_commit(struct ring_buffer *buffer,
Arnaldo Carvalho de Melo0a987752009-02-05 16:12:56 -02002346 struct ring_buffer_event *event)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002347{
2348 struct ring_buffer_per_cpu *cpu_buffer;
2349 int cpu = raw_smp_processor_id();
2350
2351 cpu_buffer = buffer->buffers[cpu];
2352
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002353 rb_commit(cpu_buffer, event);
2354
Steven Rostedt261842b2009-04-16 21:41:52 -04002355 trace_recursive_unlock();
2356
Steven Rostedt5168ae52010-06-03 09:36:50 -04002357 preempt_enable_notrace();
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002358
2359 return 0;
2360}
Robert Richterc4f50182008-12-11 16:49:22 +01002361EXPORT_SYMBOL_GPL(ring_buffer_unlock_commit);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002362
Frederic Weisbeckerf3b9aae2009-04-19 23:39:33 +02002363static inline void rb_event_discard(struct ring_buffer_event *event)
2364{
Steven Rostedt69d1b832010-10-07 18:18:05 -04002365 if (event->type_len == RINGBUF_TYPE_TIME_EXTEND)
2366 event = skip_time_extend(event);
2367
Lai Jiangshan334d4162009-04-24 11:27:05 +08002368 /* array[0] holds the actual length for the discarded event */
2369 event->array[0] = rb_event_data_length(event) - RB_EVNT_HDR_SIZE;
2370 event->type_len = RINGBUF_TYPE_PADDING;
Frederic Weisbeckerf3b9aae2009-04-19 23:39:33 +02002371 /* time delta must be non zero */
2372 if (!event->time_delta)
2373 event->time_delta = 1;
2374}
2375
Steven Rostedta1863c22009-09-03 10:23:58 -04002376/*
2377 * Decrement the entries to the page that an event is on.
2378 * The event does not even need to exist, only the pointer
2379 * to the page it is on. This may only be called before the commit
2380 * takes place.
2381 */
2382static inline void
2383rb_decrement_entry(struct ring_buffer_per_cpu *cpu_buffer,
2384 struct ring_buffer_event *event)
2385{
2386 unsigned long addr = (unsigned long)event;
2387 struct buffer_page *bpage = cpu_buffer->commit_page;
2388 struct buffer_page *start;
2389
2390 addr &= PAGE_MASK;
2391
2392 /* Do the likely case first */
2393 if (likely(bpage->page == (void *)addr)) {
2394 local_dec(&bpage->entries);
2395 return;
2396 }
2397
2398 /*
2399 * Because the commit page may be on the reader page we
2400 * start with the next page and check the end loop there.
2401 */
2402 rb_inc_page(cpu_buffer, &bpage);
2403 start = bpage;
2404 do {
2405 if (bpage->page == (void *)addr) {
2406 local_dec(&bpage->entries);
2407 return;
2408 }
2409 rb_inc_page(cpu_buffer, &bpage);
2410 } while (bpage != start);
2411
2412 /* commit not part of this buffer?? */
2413 RB_WARN_ON(cpu_buffer, 1);
2414}
2415
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002416/**
Steven Rostedtfa1b47d2009-04-02 00:09:41 -04002417 * ring_buffer_commit_discard - discard an event that has not been committed
2418 * @buffer: the ring buffer
2419 * @event: non committed event to discard
2420 *
Steven Rostedtdc892f72009-09-03 15:33:41 -04002421 * Sometimes an event that is in the ring buffer needs to be ignored.
2422 * This function lets the user discard an event in the ring buffer
2423 * and then that event will not be read later.
2424 *
2425 * This function only works if it is called before the the item has been
2426 * committed. It will try to free the event from the ring buffer
Steven Rostedtfa1b47d2009-04-02 00:09:41 -04002427 * if another event has not been added behind it.
2428 *
2429 * If another event has been added behind it, it will set the event
2430 * up as discarded, and perform the commit.
2431 *
2432 * If this function is called, do not call ring_buffer_unlock_commit on
2433 * the event.
2434 */
2435void ring_buffer_discard_commit(struct ring_buffer *buffer,
2436 struct ring_buffer_event *event)
2437{
2438 struct ring_buffer_per_cpu *cpu_buffer;
Steven Rostedtfa1b47d2009-04-02 00:09:41 -04002439 int cpu;
2440
2441 /* The event is discarded regardless */
Frederic Weisbeckerf3b9aae2009-04-19 23:39:33 +02002442 rb_event_discard(event);
Steven Rostedtfa1b47d2009-04-02 00:09:41 -04002443
Steven Rostedtfa743952009-06-16 12:37:57 -04002444 cpu = smp_processor_id();
2445 cpu_buffer = buffer->buffers[cpu];
2446
Steven Rostedtfa1b47d2009-04-02 00:09:41 -04002447 /*
2448 * This must only be called if the event has not been
2449 * committed yet. Thus we can assume that preemption
2450 * is still disabled.
2451 */
Steven Rostedtfa743952009-06-16 12:37:57 -04002452 RB_WARN_ON(buffer, !local_read(&cpu_buffer->committing));
Steven Rostedtfa1b47d2009-04-02 00:09:41 -04002453
Steven Rostedta1863c22009-09-03 10:23:58 -04002454 rb_decrement_entry(cpu_buffer, event);
Steven Rostedt0f2541d2009-08-05 12:02:48 -04002455 if (rb_try_to_discard(cpu_buffer, event))
Steven Rostedtedd813bf2009-06-02 23:00:53 -04002456 goto out;
Steven Rostedtfa1b47d2009-04-02 00:09:41 -04002457
2458 /*
2459 * The commit is still visible by the reader, so we
Steven Rostedta1863c22009-09-03 10:23:58 -04002460 * must still update the timestamp.
Steven Rostedtfa1b47d2009-04-02 00:09:41 -04002461 */
Steven Rostedta1863c22009-09-03 10:23:58 -04002462 rb_update_write_stamp(cpu_buffer, event);
Steven Rostedtfa1b47d2009-04-02 00:09:41 -04002463 out:
Steven Rostedtfa743952009-06-16 12:37:57 -04002464 rb_end_commit(cpu_buffer);
Steven Rostedtfa1b47d2009-04-02 00:09:41 -04002465
Frederic Weisbeckerf3b9aae2009-04-19 23:39:33 +02002466 trace_recursive_unlock();
2467
Steven Rostedt5168ae52010-06-03 09:36:50 -04002468 preempt_enable_notrace();
Steven Rostedtfa1b47d2009-04-02 00:09:41 -04002469
2470}
2471EXPORT_SYMBOL_GPL(ring_buffer_discard_commit);
2472
2473/**
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002474 * ring_buffer_write - write data to the buffer without reserving
2475 * @buffer: The ring buffer to write to.
2476 * @length: The length of the data being written (excluding the event header)
2477 * @data: The data to write to the buffer.
2478 *
2479 * This is like ring_buffer_lock_reserve and ring_buffer_unlock_commit as
2480 * one function. If you already have the data to write to the buffer, it
2481 * may be easier to simply call this function.
2482 *
2483 * Note, like ring_buffer_lock_reserve, the length is the length of the data
2484 * and not the length of the event which would hold the header.
2485 */
2486int ring_buffer_write(struct ring_buffer *buffer,
2487 unsigned long length,
2488 void *data)
2489{
2490 struct ring_buffer_per_cpu *cpu_buffer;
2491 struct ring_buffer_event *event;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002492 void *body;
2493 int ret = -EBUSY;
Steven Rostedt5168ae52010-06-03 09:36:50 -04002494 int cpu;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002495
Steven Rostedt033601a2008-11-21 12:41:55 -05002496 if (ring_buffer_flags != RB_BUFFERS_ON)
Steven Rostedta3583242008-11-11 15:01:42 -05002497 return -EBUSY;
2498
Steven Rostedt5168ae52010-06-03 09:36:50 -04002499 preempt_disable_notrace();
Steven Rostedtbf41a152008-10-04 02:00:59 -04002500
Lai Jiangshan52fbe9c2010-03-08 14:50:43 +08002501 if (atomic_read(&buffer->record_disabled))
2502 goto out;
2503
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002504 cpu = raw_smp_processor_id();
2505
Rusty Russell9e01c1b2009-01-01 10:12:22 +10302506 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedtd7690412008-10-01 00:29:53 -04002507 goto out;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002508
2509 cpu_buffer = buffer->buffers[cpu];
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002510
2511 if (atomic_read(&cpu_buffer->record_disabled))
2512 goto out;
2513
Steven Rostedtbe957c42009-05-11 14:42:53 -04002514 if (length > BUF_MAX_DATA_SIZE)
2515 goto out;
2516
Steven Rostedt62f0b3e2009-09-04 14:11:34 -04002517 event = rb_reserve_next_event(buffer, cpu_buffer, length);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002518 if (!event)
2519 goto out;
2520
2521 body = rb_event_data(event);
2522
2523 memcpy(body, data, length);
2524
2525 rb_commit(cpu_buffer, event);
2526
2527 ret = 0;
2528 out:
Steven Rostedt5168ae52010-06-03 09:36:50 -04002529 preempt_enable_notrace();
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002530
2531 return ret;
2532}
Robert Richterc4f50182008-12-11 16:49:22 +01002533EXPORT_SYMBOL_GPL(ring_buffer_write);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002534
Andrew Morton34a148b2009-01-09 12:27:09 -08002535static int rb_per_cpu_empty(struct ring_buffer_per_cpu *cpu_buffer)
Steven Rostedtbf41a152008-10-04 02:00:59 -04002536{
2537 struct buffer_page *reader = cpu_buffer->reader_page;
Steven Rostedt77ae3652009-03-27 11:00:29 -04002538 struct buffer_page *head = rb_set_head_page(cpu_buffer);
Steven Rostedtbf41a152008-10-04 02:00:59 -04002539 struct buffer_page *commit = cpu_buffer->commit_page;
2540
Steven Rostedt77ae3652009-03-27 11:00:29 -04002541 /* In case of error, head will be NULL */
2542 if (unlikely(!head))
2543 return 1;
2544
Steven Rostedtbf41a152008-10-04 02:00:59 -04002545 return reader->read == rb_page_commit(reader) &&
2546 (commit == reader ||
2547 (commit == head &&
2548 head->read == rb_page_commit(commit)));
2549}
2550
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002551/**
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002552 * ring_buffer_record_disable - stop all writes into the buffer
2553 * @buffer: The ring buffer to stop writes to.
2554 *
2555 * This prevents all writes to the buffer. Any attempt to write
2556 * to the buffer after this will fail and return NULL.
2557 *
2558 * The caller should call synchronize_sched() after this.
2559 */
2560void ring_buffer_record_disable(struct ring_buffer *buffer)
2561{
2562 atomic_inc(&buffer->record_disabled);
2563}
Robert Richterc4f50182008-12-11 16:49:22 +01002564EXPORT_SYMBOL_GPL(ring_buffer_record_disable);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002565
2566/**
2567 * ring_buffer_record_enable - enable writes to the buffer
2568 * @buffer: The ring buffer to enable writes
2569 *
2570 * Note, multiple disables will need the same number of enables
Adam Buchbinderc41b20e2009-12-11 16:35:39 -05002571 * to truly enable the writing (much like preempt_disable).
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002572 */
2573void ring_buffer_record_enable(struct ring_buffer *buffer)
2574{
2575 atomic_dec(&buffer->record_disabled);
2576}
Robert Richterc4f50182008-12-11 16:49:22 +01002577EXPORT_SYMBOL_GPL(ring_buffer_record_enable);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002578
2579/**
2580 * ring_buffer_record_disable_cpu - stop all writes into the cpu_buffer
2581 * @buffer: The ring buffer to stop writes to.
2582 * @cpu: The CPU buffer to stop
2583 *
2584 * This prevents all writes to the buffer. Any attempt to write
2585 * to the buffer after this will fail and return NULL.
2586 *
2587 * The caller should call synchronize_sched() after this.
2588 */
2589void ring_buffer_record_disable_cpu(struct ring_buffer *buffer, int cpu)
2590{
2591 struct ring_buffer_per_cpu *cpu_buffer;
2592
Rusty Russell9e01c1b2009-01-01 10:12:22 +10302593 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedt8aabee52009-03-12 13:13:49 -04002594 return;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002595
2596 cpu_buffer = buffer->buffers[cpu];
2597 atomic_inc(&cpu_buffer->record_disabled);
2598}
Robert Richterc4f50182008-12-11 16:49:22 +01002599EXPORT_SYMBOL_GPL(ring_buffer_record_disable_cpu);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002600
2601/**
2602 * ring_buffer_record_enable_cpu - enable writes to the buffer
2603 * @buffer: The ring buffer to enable writes
2604 * @cpu: The CPU to enable.
2605 *
2606 * Note, multiple disables will need the same number of enables
Adam Buchbinderc41b20e2009-12-11 16:35:39 -05002607 * to truly enable the writing (much like preempt_disable).
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002608 */
2609void ring_buffer_record_enable_cpu(struct ring_buffer *buffer, int cpu)
2610{
2611 struct ring_buffer_per_cpu *cpu_buffer;
2612
Rusty Russell9e01c1b2009-01-01 10:12:22 +10302613 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedt8aabee52009-03-12 13:13:49 -04002614 return;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002615
2616 cpu_buffer = buffer->buffers[cpu];
2617 atomic_dec(&cpu_buffer->record_disabled);
2618}
Robert Richterc4f50182008-12-11 16:49:22 +01002619EXPORT_SYMBOL_GPL(ring_buffer_record_enable_cpu);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002620
2621/**
2622 * ring_buffer_entries_cpu - get the number of entries in a cpu buffer
2623 * @buffer: The ring buffer
2624 * @cpu: The per CPU buffer to get the entries from.
2625 */
2626unsigned long ring_buffer_entries_cpu(struct ring_buffer *buffer, int cpu)
2627{
2628 struct ring_buffer_per_cpu *cpu_buffer;
Steven Rostedt8aabee52009-03-12 13:13:49 -04002629 unsigned long ret;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002630
Rusty Russell9e01c1b2009-01-01 10:12:22 +10302631 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedt8aabee52009-03-12 13:13:49 -04002632 return 0;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002633
2634 cpu_buffer = buffer->buffers[cpu];
Steven Rostedt77ae3652009-03-27 11:00:29 -04002635 ret = (local_read(&cpu_buffer->entries) - local_read(&cpu_buffer->overrun))
Steven Rostedte4906ef2009-04-30 20:49:44 -04002636 - cpu_buffer->read;
Steven Rostedt554f7862009-03-11 22:00:13 -04002637
2638 return ret;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002639}
Robert Richterc4f50182008-12-11 16:49:22 +01002640EXPORT_SYMBOL_GPL(ring_buffer_entries_cpu);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002641
2642/**
2643 * ring_buffer_overrun_cpu - get the number of overruns in a cpu_buffer
2644 * @buffer: The ring buffer
2645 * @cpu: The per CPU buffer to get the number of overruns from
2646 */
2647unsigned long ring_buffer_overrun_cpu(struct ring_buffer *buffer, int cpu)
2648{
2649 struct ring_buffer_per_cpu *cpu_buffer;
Steven Rostedt8aabee52009-03-12 13:13:49 -04002650 unsigned long ret;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002651
Rusty Russell9e01c1b2009-01-01 10:12:22 +10302652 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedt8aabee52009-03-12 13:13:49 -04002653 return 0;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002654
2655 cpu_buffer = buffer->buffers[cpu];
Steven Rostedt77ae3652009-03-27 11:00:29 -04002656 ret = local_read(&cpu_buffer->overrun);
Steven Rostedt554f7862009-03-11 22:00:13 -04002657
2658 return ret;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002659}
Robert Richterc4f50182008-12-11 16:49:22 +01002660EXPORT_SYMBOL_GPL(ring_buffer_overrun_cpu);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002661
2662/**
Steven Rostedtf0d2c682009-04-29 13:43:37 -04002663 * ring_buffer_commit_overrun_cpu - get the number of overruns caused by commits
2664 * @buffer: The ring buffer
2665 * @cpu: The per CPU buffer to get the number of overruns from
2666 */
2667unsigned long
2668ring_buffer_commit_overrun_cpu(struct ring_buffer *buffer, int cpu)
2669{
2670 struct ring_buffer_per_cpu *cpu_buffer;
2671 unsigned long ret;
2672
2673 if (!cpumask_test_cpu(cpu, buffer->cpumask))
2674 return 0;
2675
2676 cpu_buffer = buffer->buffers[cpu];
Steven Rostedt77ae3652009-03-27 11:00:29 -04002677 ret = local_read(&cpu_buffer->commit_overrun);
Steven Rostedtf0d2c682009-04-29 13:43:37 -04002678
2679 return ret;
2680}
2681EXPORT_SYMBOL_GPL(ring_buffer_commit_overrun_cpu);
2682
2683/**
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002684 * ring_buffer_entries - get the number of entries in a buffer
2685 * @buffer: The ring buffer
2686 *
2687 * Returns the total number of entries in the ring buffer
2688 * (all CPU entries)
2689 */
2690unsigned long ring_buffer_entries(struct ring_buffer *buffer)
2691{
2692 struct ring_buffer_per_cpu *cpu_buffer;
2693 unsigned long entries = 0;
2694 int cpu;
2695
2696 /* if you care about this being correct, lock the buffer */
2697 for_each_buffer_cpu(buffer, cpu) {
2698 cpu_buffer = buffer->buffers[cpu];
Steven Rostedte4906ef2009-04-30 20:49:44 -04002699 entries += (local_read(&cpu_buffer->entries) -
Steven Rostedt77ae3652009-03-27 11:00:29 -04002700 local_read(&cpu_buffer->overrun)) - cpu_buffer->read;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002701 }
2702
2703 return entries;
2704}
Robert Richterc4f50182008-12-11 16:49:22 +01002705EXPORT_SYMBOL_GPL(ring_buffer_entries);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002706
2707/**
Jiri Olsa67b394f2009-10-23 19:36:18 -04002708 * ring_buffer_overruns - get the number of overruns in buffer
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002709 * @buffer: The ring buffer
2710 *
2711 * Returns the total number of overruns in the ring buffer
2712 * (all CPU entries)
2713 */
2714unsigned long ring_buffer_overruns(struct ring_buffer *buffer)
2715{
2716 struct ring_buffer_per_cpu *cpu_buffer;
2717 unsigned long overruns = 0;
2718 int cpu;
2719
2720 /* if you care about this being correct, lock the buffer */
2721 for_each_buffer_cpu(buffer, cpu) {
2722 cpu_buffer = buffer->buffers[cpu];
Steven Rostedt77ae3652009-03-27 11:00:29 -04002723 overruns += local_read(&cpu_buffer->overrun);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002724 }
2725
2726 return overruns;
2727}
Robert Richterc4f50182008-12-11 16:49:22 +01002728EXPORT_SYMBOL_GPL(ring_buffer_overruns);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002729
Steven Rostedt642edba2008-11-12 00:01:26 -05002730static void rb_iter_reset(struct ring_buffer_iter *iter)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002731{
2732 struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
2733
Steven Rostedtd7690412008-10-01 00:29:53 -04002734 /* Iterator usage is expected to have record disabled */
2735 if (list_empty(&cpu_buffer->reader_page->list)) {
Steven Rostedt77ae3652009-03-27 11:00:29 -04002736 iter->head_page = rb_set_head_page(cpu_buffer);
2737 if (unlikely(!iter->head_page))
2738 return;
2739 iter->head = iter->head_page->read;
Steven Rostedtd7690412008-10-01 00:29:53 -04002740 } else {
2741 iter->head_page = cpu_buffer->reader_page;
Steven Rostedt6f807ac2008-10-04 02:00:58 -04002742 iter->head = cpu_buffer->reader_page->read;
Steven Rostedtd7690412008-10-01 00:29:53 -04002743 }
2744 if (iter->head)
2745 iter->read_stamp = cpu_buffer->read_stamp;
2746 else
Steven Rostedtabc9b562008-12-02 15:34:06 -05002747 iter->read_stamp = iter->head_page->page->time_stamp;
Steven Rostedt492a74f2010-01-25 15:17:47 -05002748 iter->cache_reader_page = cpu_buffer->reader_page;
2749 iter->cache_read = cpu_buffer->read;
Steven Rostedt642edba2008-11-12 00:01:26 -05002750}
Steven Rostedtf83c9d02008-11-11 18:47:44 +01002751
Steven Rostedt642edba2008-11-12 00:01:26 -05002752/**
2753 * ring_buffer_iter_reset - reset an iterator
2754 * @iter: The iterator to reset
2755 *
2756 * Resets the iterator, so that it will start from the beginning
2757 * again.
2758 */
2759void ring_buffer_iter_reset(struct ring_buffer_iter *iter)
2760{
Steven Rostedt554f7862009-03-11 22:00:13 -04002761 struct ring_buffer_per_cpu *cpu_buffer;
Steven Rostedt642edba2008-11-12 00:01:26 -05002762 unsigned long flags;
2763
Steven Rostedt554f7862009-03-11 22:00:13 -04002764 if (!iter)
2765 return;
2766
2767 cpu_buffer = iter->cpu_buffer;
2768
Steven Rostedt642edba2008-11-12 00:01:26 -05002769 spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
2770 rb_iter_reset(iter);
Steven Rostedtf83c9d02008-11-11 18:47:44 +01002771 spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002772}
Robert Richterc4f50182008-12-11 16:49:22 +01002773EXPORT_SYMBOL_GPL(ring_buffer_iter_reset);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002774
2775/**
2776 * ring_buffer_iter_empty - check if an iterator has no more to read
2777 * @iter: The iterator to check
2778 */
2779int ring_buffer_iter_empty(struct ring_buffer_iter *iter)
2780{
2781 struct ring_buffer_per_cpu *cpu_buffer;
2782
2783 cpu_buffer = iter->cpu_buffer;
2784
Steven Rostedtbf41a152008-10-04 02:00:59 -04002785 return iter->head_page == cpu_buffer->commit_page &&
2786 iter->head == rb_commit_index(cpu_buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002787}
Robert Richterc4f50182008-12-11 16:49:22 +01002788EXPORT_SYMBOL_GPL(ring_buffer_iter_empty);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002789
2790static void
2791rb_update_read_stamp(struct ring_buffer_per_cpu *cpu_buffer,
2792 struct ring_buffer_event *event)
2793{
2794 u64 delta;
2795
Lai Jiangshan334d4162009-04-24 11:27:05 +08002796 switch (event->type_len) {
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002797 case RINGBUF_TYPE_PADDING:
2798 return;
2799
2800 case RINGBUF_TYPE_TIME_EXTEND:
2801 delta = event->array[0];
2802 delta <<= TS_SHIFT;
2803 delta += event->time_delta;
2804 cpu_buffer->read_stamp += delta;
2805 return;
2806
2807 case RINGBUF_TYPE_TIME_STAMP:
2808 /* FIXME: not implemented */
2809 return;
2810
2811 case RINGBUF_TYPE_DATA:
2812 cpu_buffer->read_stamp += event->time_delta;
2813 return;
2814
2815 default:
2816 BUG();
2817 }
2818 return;
2819}
2820
2821static void
2822rb_update_iter_read_stamp(struct ring_buffer_iter *iter,
2823 struct ring_buffer_event *event)
2824{
2825 u64 delta;
2826
Lai Jiangshan334d4162009-04-24 11:27:05 +08002827 switch (event->type_len) {
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002828 case RINGBUF_TYPE_PADDING:
2829 return;
2830
2831 case RINGBUF_TYPE_TIME_EXTEND:
2832 delta = event->array[0];
2833 delta <<= TS_SHIFT;
2834 delta += event->time_delta;
2835 iter->read_stamp += delta;
2836 return;
2837
2838 case RINGBUF_TYPE_TIME_STAMP:
2839 /* FIXME: not implemented */
2840 return;
2841
2842 case RINGBUF_TYPE_DATA:
2843 iter->read_stamp += event->time_delta;
2844 return;
2845
2846 default:
2847 BUG();
2848 }
2849 return;
2850}
2851
Steven Rostedtd7690412008-10-01 00:29:53 -04002852static struct buffer_page *
2853rb_get_reader_page(struct ring_buffer_per_cpu *cpu_buffer)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002854{
Steven Rostedtd7690412008-10-01 00:29:53 -04002855 struct buffer_page *reader = NULL;
Steven Rostedt66a8cb92010-03-31 13:21:56 -04002856 unsigned long overwrite;
Steven Rostedtd7690412008-10-01 00:29:53 -04002857 unsigned long flags;
Steven Rostedt818e3dd2008-10-31 09:58:35 -04002858 int nr_loops = 0;
Steven Rostedt77ae3652009-03-27 11:00:29 -04002859 int ret;
Steven Rostedtd7690412008-10-01 00:29:53 -04002860
Steven Rostedt3e03fb72008-11-06 00:09:43 -05002861 local_irq_save(flags);
Thomas Gleixner0199c4e2009-12-02 20:01:25 +01002862 arch_spin_lock(&cpu_buffer->lock);
Steven Rostedtd7690412008-10-01 00:29:53 -04002863
2864 again:
Steven Rostedt818e3dd2008-10-31 09:58:35 -04002865 /*
2866 * This should normally only loop twice. But because the
2867 * start of the reader inserts an empty page, it causes
2868 * a case where we will loop three times. There should be no
2869 * reason to loop four times (that I know of).
2870 */
Steven Rostedt3e89c7b2008-11-11 15:28:41 -05002871 if (RB_WARN_ON(cpu_buffer, ++nr_loops > 3)) {
Steven Rostedt818e3dd2008-10-31 09:58:35 -04002872 reader = NULL;
2873 goto out;
2874 }
2875
Steven Rostedtd7690412008-10-01 00:29:53 -04002876 reader = cpu_buffer->reader_page;
2877
2878 /* If there's more to read, return this page */
Steven Rostedtbf41a152008-10-04 02:00:59 -04002879 if (cpu_buffer->reader_page->read < rb_page_size(reader))
Steven Rostedtd7690412008-10-01 00:29:53 -04002880 goto out;
2881
2882 /* Never should we have an index greater than the size */
Steven Rostedt3e89c7b2008-11-11 15:28:41 -05002883 if (RB_WARN_ON(cpu_buffer,
2884 cpu_buffer->reader_page->read > rb_page_size(reader)))
2885 goto out;
Steven Rostedtd7690412008-10-01 00:29:53 -04002886
2887 /* check if we caught up to the tail */
2888 reader = NULL;
Steven Rostedtbf41a152008-10-04 02:00:59 -04002889 if (cpu_buffer->commit_page == cpu_buffer->reader_page)
Steven Rostedtd7690412008-10-01 00:29:53 -04002890 goto out;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002891
2892 /*
Steven Rostedtd7690412008-10-01 00:29:53 -04002893 * Reset the reader page to size zero.
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002894 */
Steven Rostedt77ae3652009-03-27 11:00:29 -04002895 local_set(&cpu_buffer->reader_page->write, 0);
2896 local_set(&cpu_buffer->reader_page->entries, 0);
2897 local_set(&cpu_buffer->reader_page->page->commit, 0);
Steven Rostedtff0ff842010-03-31 22:11:42 -04002898 cpu_buffer->reader_page->real_end = 0;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002899
Steven Rostedt77ae3652009-03-27 11:00:29 -04002900 spin:
2901 /*
2902 * Splice the empty reader page into the list around the head.
2903 */
2904 reader = rb_set_head_page(cpu_buffer);
Steven Rostedt0e1ff5d2010-01-06 20:40:44 -05002905 cpu_buffer->reader_page->list.next = rb_list_head(reader->list.next);
Steven Rostedtd7690412008-10-01 00:29:53 -04002906 cpu_buffer->reader_page->list.prev = reader->list.prev;
Steven Rostedtbf41a152008-10-04 02:00:59 -04002907
Steven Rostedt3adc54f2009-03-30 15:32:01 -04002908 /*
2909 * cpu_buffer->pages just needs to point to the buffer, it
2910 * has no specific buffer page to point to. Lets move it out
2911 * of our way so we don't accidently swap it.
2912 */
2913 cpu_buffer->pages = reader->list.prev;
2914
Steven Rostedt77ae3652009-03-27 11:00:29 -04002915 /* The reader page will be pointing to the new head */
2916 rb_set_list_to_head(cpu_buffer, &cpu_buffer->reader_page->list);
Steven Rostedtd7690412008-10-01 00:29:53 -04002917
2918 /*
Steven Rostedt66a8cb92010-03-31 13:21:56 -04002919 * We want to make sure we read the overruns after we set up our
2920 * pointers to the next object. The writer side does a
2921 * cmpxchg to cross pages which acts as the mb on the writer
2922 * side. Note, the reader will constantly fail the swap
2923 * while the writer is updating the pointers, so this
2924 * guarantees that the overwrite recorded here is the one we
2925 * want to compare with the last_overrun.
2926 */
2927 smp_mb();
2928 overwrite = local_read(&(cpu_buffer->overrun));
2929
2930 /*
Steven Rostedt77ae3652009-03-27 11:00:29 -04002931 * Here's the tricky part.
2932 *
2933 * We need to move the pointer past the header page.
2934 * But we can only do that if a writer is not currently
2935 * moving it. The page before the header page has the
2936 * flag bit '1' set if it is pointing to the page we want.
2937 * but if the writer is in the process of moving it
2938 * than it will be '2' or already moved '0'.
Steven Rostedtd7690412008-10-01 00:29:53 -04002939 */
Steven Rostedtd7690412008-10-01 00:29:53 -04002940
Steven Rostedt77ae3652009-03-27 11:00:29 -04002941 ret = rb_head_page_replace(reader, cpu_buffer->reader_page);
2942
2943 /*
2944 * If we did not convert it, then we must try again.
2945 */
2946 if (!ret)
2947 goto spin;
2948
2949 /*
2950 * Yeah! We succeeded in replacing the page.
2951 *
2952 * Now make the new head point back to the reader page.
2953 */
David Sharp5ded3dc2010-01-06 17:12:07 -08002954 rb_list_head(reader->list.next)->prev = &cpu_buffer->reader_page->list;
Steven Rostedt77ae3652009-03-27 11:00:29 -04002955 rb_inc_page(cpu_buffer, &cpu_buffer->head_page);
Steven Rostedtd7690412008-10-01 00:29:53 -04002956
2957 /* Finally update the reader page to the new head */
2958 cpu_buffer->reader_page = reader;
2959 rb_reset_reader_page(cpu_buffer);
2960
Steven Rostedt66a8cb92010-03-31 13:21:56 -04002961 if (overwrite != cpu_buffer->last_overrun) {
2962 cpu_buffer->lost_events = overwrite - cpu_buffer->last_overrun;
2963 cpu_buffer->last_overrun = overwrite;
2964 }
2965
Steven Rostedtd7690412008-10-01 00:29:53 -04002966 goto again;
2967
2968 out:
Thomas Gleixner0199c4e2009-12-02 20:01:25 +01002969 arch_spin_unlock(&cpu_buffer->lock);
Steven Rostedt3e03fb72008-11-06 00:09:43 -05002970 local_irq_restore(flags);
Steven Rostedtd7690412008-10-01 00:29:53 -04002971
2972 return reader;
2973}
2974
2975static void rb_advance_reader(struct ring_buffer_per_cpu *cpu_buffer)
2976{
2977 struct ring_buffer_event *event;
2978 struct buffer_page *reader;
2979 unsigned length;
2980
2981 reader = rb_get_reader_page(cpu_buffer);
2982
2983 /* This function should not be called when buffer is empty */
Steven Rostedt3e89c7b2008-11-11 15:28:41 -05002984 if (RB_WARN_ON(cpu_buffer, !reader))
2985 return;
Steven Rostedtd7690412008-10-01 00:29:53 -04002986
2987 event = rb_reader_event(cpu_buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002988
Steven Rostedta1863c22009-09-03 10:23:58 -04002989 if (event->type_len <= RINGBUF_TYPE_DATA_TYPE_LEN_MAX)
Steven Rostedte4906ef2009-04-30 20:49:44 -04002990 cpu_buffer->read++;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002991
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002992 rb_update_read_stamp(cpu_buffer, event);
2993
Steven Rostedtd7690412008-10-01 00:29:53 -04002994 length = rb_event_length(event);
Steven Rostedt6f807ac2008-10-04 02:00:58 -04002995 cpu_buffer->reader_page->read += length;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002996}
2997
2998static void rb_advance_iter(struct ring_buffer_iter *iter)
2999{
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003000 struct ring_buffer_per_cpu *cpu_buffer;
3001 struct ring_buffer_event *event;
3002 unsigned length;
3003
3004 cpu_buffer = iter->cpu_buffer;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003005
3006 /*
3007 * Check if we are at the end of the buffer.
3008 */
Steven Rostedtbf41a152008-10-04 02:00:59 -04003009 if (iter->head >= rb_page_size(iter->head_page)) {
Steven Rostedtea05b572009-06-03 09:30:10 -04003010 /* discarded commits can make the page empty */
3011 if (iter->head_page == cpu_buffer->commit_page)
Steven Rostedt3e89c7b2008-11-11 15:28:41 -05003012 return;
Steven Rostedtd7690412008-10-01 00:29:53 -04003013 rb_inc_iter(iter);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003014 return;
3015 }
3016
3017 event = rb_iter_head_event(iter);
3018
3019 length = rb_event_length(event);
3020
3021 /*
3022 * This should not be called to advance the header if we are
3023 * at the tail of the buffer.
3024 */
Steven Rostedt3e89c7b2008-11-11 15:28:41 -05003025 if (RB_WARN_ON(cpu_buffer,
Steven Rostedtf536aaf2008-11-10 23:07:30 -05003026 (iter->head_page == cpu_buffer->commit_page) &&
Steven Rostedt3e89c7b2008-11-11 15:28:41 -05003027 (iter->head + length > rb_commit_index(cpu_buffer))))
3028 return;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003029
3030 rb_update_iter_read_stamp(iter, event);
3031
3032 iter->head += length;
3033
3034 /* check for end of page padding */
Steven Rostedtbf41a152008-10-04 02:00:59 -04003035 if ((iter->head >= rb_page_size(iter->head_page)) &&
3036 (iter->head_page != cpu_buffer->commit_page))
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003037 rb_advance_iter(iter);
3038}
3039
Steven Rostedt66a8cb92010-03-31 13:21:56 -04003040static int rb_lost_events(struct ring_buffer_per_cpu *cpu_buffer)
3041{
3042 return cpu_buffer->lost_events;
3043}
3044
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003045static struct ring_buffer_event *
Steven Rostedt66a8cb92010-03-31 13:21:56 -04003046rb_buffer_peek(struct ring_buffer_per_cpu *cpu_buffer, u64 *ts,
3047 unsigned long *lost_events)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003048{
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003049 struct ring_buffer_event *event;
Steven Rostedtd7690412008-10-01 00:29:53 -04003050 struct buffer_page *reader;
Steven Rostedt818e3dd2008-10-31 09:58:35 -04003051 int nr_loops = 0;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003052
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003053 again:
Steven Rostedt818e3dd2008-10-31 09:58:35 -04003054 /*
Steven Rostedt69d1b832010-10-07 18:18:05 -04003055 * We repeat when a time extend is encountered.
3056 * Since the time extend is always attached to a data event,
3057 * we should never loop more than once.
3058 * (We never hit the following condition more than twice).
Steven Rostedt818e3dd2008-10-31 09:58:35 -04003059 */
Steven Rostedt69d1b832010-10-07 18:18:05 -04003060 if (RB_WARN_ON(cpu_buffer, ++nr_loops > 2))
Steven Rostedt818e3dd2008-10-31 09:58:35 -04003061 return NULL;
Steven Rostedt818e3dd2008-10-31 09:58:35 -04003062
Steven Rostedtd7690412008-10-01 00:29:53 -04003063 reader = rb_get_reader_page(cpu_buffer);
3064 if (!reader)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003065 return NULL;
3066
Steven Rostedtd7690412008-10-01 00:29:53 -04003067 event = rb_reader_event(cpu_buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003068
Lai Jiangshan334d4162009-04-24 11:27:05 +08003069 switch (event->type_len) {
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003070 case RINGBUF_TYPE_PADDING:
Tom Zanussi2d622712009-03-22 03:30:49 -05003071 if (rb_null_event(event))
3072 RB_WARN_ON(cpu_buffer, 1);
3073 /*
3074 * Because the writer could be discarding every
3075 * event it creates (which would probably be bad)
3076 * if we were to go back to "again" then we may never
3077 * catch up, and will trigger the warn on, or lock
3078 * the box. Return the padding, and we will release
3079 * the current locks, and try again.
3080 */
Tom Zanussi2d622712009-03-22 03:30:49 -05003081 return event;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003082
3083 case RINGBUF_TYPE_TIME_EXTEND:
3084 /* Internal data, OK to advance */
Steven Rostedtd7690412008-10-01 00:29:53 -04003085 rb_advance_reader(cpu_buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003086 goto again;
3087
3088 case RINGBUF_TYPE_TIME_STAMP:
3089 /* FIXME: not implemented */
Steven Rostedtd7690412008-10-01 00:29:53 -04003090 rb_advance_reader(cpu_buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003091 goto again;
3092
3093 case RINGBUF_TYPE_DATA:
3094 if (ts) {
3095 *ts = cpu_buffer->read_stamp + event->time_delta;
Robert Richterd8eeb2d2009-07-31 14:58:04 +02003096 ring_buffer_normalize_time_stamp(cpu_buffer->buffer,
Steven Rostedt37886f62009-03-17 17:22:06 -04003097 cpu_buffer->cpu, ts);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003098 }
Steven Rostedt66a8cb92010-03-31 13:21:56 -04003099 if (lost_events)
3100 *lost_events = rb_lost_events(cpu_buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003101 return event;
3102
3103 default:
3104 BUG();
3105 }
3106
3107 return NULL;
3108}
Robert Richterc4f50182008-12-11 16:49:22 +01003109EXPORT_SYMBOL_GPL(ring_buffer_peek);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003110
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003111static struct ring_buffer_event *
3112rb_iter_peek(struct ring_buffer_iter *iter, u64 *ts)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003113{
3114 struct ring_buffer *buffer;
3115 struct ring_buffer_per_cpu *cpu_buffer;
3116 struct ring_buffer_event *event;
Steven Rostedt818e3dd2008-10-31 09:58:35 -04003117 int nr_loops = 0;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003118
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003119 cpu_buffer = iter->cpu_buffer;
3120 buffer = cpu_buffer->buffer;
3121
Steven Rostedt492a74f2010-01-25 15:17:47 -05003122 /*
3123 * Check if someone performed a consuming read to
3124 * the buffer. A consuming read invalidates the iterator
3125 * and we need to reset the iterator in this case.
3126 */
3127 if (unlikely(iter->cache_read != cpu_buffer->read ||
3128 iter->cache_reader_page != cpu_buffer->reader_page))
3129 rb_iter_reset(iter);
3130
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003131 again:
Steven Rostedt3c05d742010-01-26 16:14:08 -05003132 if (ring_buffer_iter_empty(iter))
3133 return NULL;
3134
Steven Rostedt818e3dd2008-10-31 09:58:35 -04003135 /*
Steven Rostedt69d1b832010-10-07 18:18:05 -04003136 * We repeat when a time extend is encountered.
3137 * Since the time extend is always attached to a data event,
3138 * we should never loop more than once.
3139 * (We never hit the following condition more than twice).
Steven Rostedt818e3dd2008-10-31 09:58:35 -04003140 */
Steven Rostedt69d1b832010-10-07 18:18:05 -04003141 if (RB_WARN_ON(cpu_buffer, ++nr_loops > 2))
Steven Rostedt818e3dd2008-10-31 09:58:35 -04003142 return NULL;
Steven Rostedt818e3dd2008-10-31 09:58:35 -04003143
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003144 if (rb_per_cpu_empty(cpu_buffer))
3145 return NULL;
3146
Steven Rostedt3c05d742010-01-26 16:14:08 -05003147 if (iter->head >= local_read(&iter->head_page->page->commit)) {
3148 rb_inc_iter(iter);
3149 goto again;
3150 }
3151
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003152 event = rb_iter_head_event(iter);
3153
Lai Jiangshan334d4162009-04-24 11:27:05 +08003154 switch (event->type_len) {
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003155 case RINGBUF_TYPE_PADDING:
Tom Zanussi2d622712009-03-22 03:30:49 -05003156 if (rb_null_event(event)) {
3157 rb_inc_iter(iter);
3158 goto again;
3159 }
3160 rb_advance_iter(iter);
3161 return event;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003162
3163 case RINGBUF_TYPE_TIME_EXTEND:
3164 /* Internal data, OK to advance */
3165 rb_advance_iter(iter);
3166 goto again;
3167
3168 case RINGBUF_TYPE_TIME_STAMP:
3169 /* FIXME: not implemented */
3170 rb_advance_iter(iter);
3171 goto again;
3172
3173 case RINGBUF_TYPE_DATA:
3174 if (ts) {
3175 *ts = iter->read_stamp + event->time_delta;
Steven Rostedt37886f62009-03-17 17:22:06 -04003176 ring_buffer_normalize_time_stamp(buffer,
3177 cpu_buffer->cpu, ts);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003178 }
3179 return event;
3180
3181 default:
3182 BUG();
3183 }
3184
3185 return NULL;
3186}
Robert Richterc4f50182008-12-11 16:49:22 +01003187EXPORT_SYMBOL_GPL(ring_buffer_iter_peek);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003188
Steven Rostedt8d707e82009-06-16 21:22:48 -04003189static inline int rb_ok_to_lock(void)
3190{
3191 /*
3192 * If an NMI die dumps out the content of the ring buffer
3193 * do not grab locks. We also permanently disable the ring
3194 * buffer too. A one time deal is all you get from reading
3195 * the ring buffer from an NMI.
3196 */
Steven Rostedt464e85e2009-08-05 15:26:37 -04003197 if (likely(!in_nmi()))
Steven Rostedt8d707e82009-06-16 21:22:48 -04003198 return 1;
3199
3200 tracing_off_permanent();
3201 return 0;
3202}
3203
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003204/**
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003205 * ring_buffer_peek - peek at the next event to be read
3206 * @buffer: The ring buffer to read
3207 * @cpu: The cpu to peak at
3208 * @ts: The timestamp counter of this event.
Steven Rostedt66a8cb92010-03-31 13:21:56 -04003209 * @lost_events: a variable to store if events were lost (may be NULL)
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003210 *
3211 * This will return the event that will be read next, but does
3212 * not consume the data.
3213 */
3214struct ring_buffer_event *
Steven Rostedt66a8cb92010-03-31 13:21:56 -04003215ring_buffer_peek(struct ring_buffer *buffer, int cpu, u64 *ts,
3216 unsigned long *lost_events)
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003217{
3218 struct ring_buffer_per_cpu *cpu_buffer = buffer->buffers[cpu];
Steven Rostedt8aabee52009-03-12 13:13:49 -04003219 struct ring_buffer_event *event;
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003220 unsigned long flags;
Steven Rostedt8d707e82009-06-16 21:22:48 -04003221 int dolock;
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003222
Steven Rostedt554f7862009-03-11 22:00:13 -04003223 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedt8aabee52009-03-12 13:13:49 -04003224 return NULL;
Steven Rostedt554f7862009-03-11 22:00:13 -04003225
Steven Rostedt8d707e82009-06-16 21:22:48 -04003226 dolock = rb_ok_to_lock();
Tom Zanussi2d622712009-03-22 03:30:49 -05003227 again:
Steven Rostedt8d707e82009-06-16 21:22:48 -04003228 local_irq_save(flags);
3229 if (dolock)
3230 spin_lock(&cpu_buffer->reader_lock);
Steven Rostedt66a8cb92010-03-31 13:21:56 -04003231 event = rb_buffer_peek(cpu_buffer, ts, lost_events);
Robert Richter469535a2009-07-30 19:19:18 +02003232 if (event && event->type_len == RINGBUF_TYPE_PADDING)
3233 rb_advance_reader(cpu_buffer);
Steven Rostedt8d707e82009-06-16 21:22:48 -04003234 if (dolock)
3235 spin_unlock(&cpu_buffer->reader_lock);
3236 local_irq_restore(flags);
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003237
Steven Rostedt1b959e12009-09-03 10:12:13 -04003238 if (event && event->type_len == RINGBUF_TYPE_PADDING)
Tom Zanussi2d622712009-03-22 03:30:49 -05003239 goto again;
Tom Zanussi2d622712009-03-22 03:30:49 -05003240
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003241 return event;
3242}
3243
3244/**
3245 * ring_buffer_iter_peek - peek at the next event to be read
3246 * @iter: The ring buffer iterator
3247 * @ts: The timestamp counter of this event.
3248 *
3249 * This will return the event that will be read next, but does
3250 * not increment the iterator.
3251 */
3252struct ring_buffer_event *
3253ring_buffer_iter_peek(struct ring_buffer_iter *iter, u64 *ts)
3254{
3255 struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
3256 struct ring_buffer_event *event;
3257 unsigned long flags;
3258
Tom Zanussi2d622712009-03-22 03:30:49 -05003259 again:
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003260 spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
3261 event = rb_iter_peek(iter, ts);
3262 spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
3263
Steven Rostedt1b959e12009-09-03 10:12:13 -04003264 if (event && event->type_len == RINGBUF_TYPE_PADDING)
Tom Zanussi2d622712009-03-22 03:30:49 -05003265 goto again;
Tom Zanussi2d622712009-03-22 03:30:49 -05003266
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003267 return event;
3268}
3269
3270/**
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003271 * ring_buffer_consume - return an event and consume it
3272 * @buffer: The ring buffer to get the next event from
Steven Rostedt66a8cb92010-03-31 13:21:56 -04003273 * @cpu: the cpu to read the buffer from
3274 * @ts: a variable to store the timestamp (may be NULL)
3275 * @lost_events: a variable to store if events were lost (may be NULL)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003276 *
3277 * Returns the next event in the ring buffer, and that event is consumed.
3278 * Meaning, that sequential reads will keep returning a different event,
3279 * and eventually empty the ring buffer if the producer is slower.
3280 */
3281struct ring_buffer_event *
Steven Rostedt66a8cb92010-03-31 13:21:56 -04003282ring_buffer_consume(struct ring_buffer *buffer, int cpu, u64 *ts,
3283 unsigned long *lost_events)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003284{
Steven Rostedt554f7862009-03-11 22:00:13 -04003285 struct ring_buffer_per_cpu *cpu_buffer;
3286 struct ring_buffer_event *event = NULL;
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003287 unsigned long flags;
Steven Rostedt8d707e82009-06-16 21:22:48 -04003288 int dolock;
3289
3290 dolock = rb_ok_to_lock();
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003291
Tom Zanussi2d622712009-03-22 03:30:49 -05003292 again:
Steven Rostedt554f7862009-03-11 22:00:13 -04003293 /* might be called in atomic */
3294 preempt_disable();
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003295
Steven Rostedt554f7862009-03-11 22:00:13 -04003296 if (!cpumask_test_cpu(cpu, buffer->cpumask))
3297 goto out;
3298
3299 cpu_buffer = buffer->buffers[cpu];
Steven Rostedt8d707e82009-06-16 21:22:48 -04003300 local_irq_save(flags);
3301 if (dolock)
3302 spin_lock(&cpu_buffer->reader_lock);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003303
Steven Rostedt66a8cb92010-03-31 13:21:56 -04003304 event = rb_buffer_peek(cpu_buffer, ts, lost_events);
3305 if (event) {
3306 cpu_buffer->lost_events = 0;
Robert Richter469535a2009-07-30 19:19:18 +02003307 rb_advance_reader(cpu_buffer);
Steven Rostedt66a8cb92010-03-31 13:21:56 -04003308 }
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003309
Steven Rostedt8d707e82009-06-16 21:22:48 -04003310 if (dolock)
3311 spin_unlock(&cpu_buffer->reader_lock);
3312 local_irq_restore(flags);
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003313
Steven Rostedt554f7862009-03-11 22:00:13 -04003314 out:
3315 preempt_enable();
3316
Steven Rostedt1b959e12009-09-03 10:12:13 -04003317 if (event && event->type_len == RINGBUF_TYPE_PADDING)
Tom Zanussi2d622712009-03-22 03:30:49 -05003318 goto again;
Tom Zanussi2d622712009-03-22 03:30:49 -05003319
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003320 return event;
3321}
Robert Richterc4f50182008-12-11 16:49:22 +01003322EXPORT_SYMBOL_GPL(ring_buffer_consume);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003323
3324/**
David Miller72c9ddf2010-04-20 15:47:11 -07003325 * ring_buffer_read_prepare - Prepare for a non consuming read of the buffer
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003326 * @buffer: The ring buffer to read from
3327 * @cpu: The cpu buffer to iterate over
3328 *
David Miller72c9ddf2010-04-20 15:47:11 -07003329 * This performs the initial preparations necessary to iterate
3330 * through the buffer. Memory is allocated, buffer recording
3331 * is disabled, and the iterator pointer is returned to the caller.
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003332 *
David Miller72c9ddf2010-04-20 15:47:11 -07003333 * Disabling buffer recordng prevents the reading from being
3334 * corrupted. This is not a consuming read, so a producer is not
3335 * expected.
3336 *
3337 * After a sequence of ring_buffer_read_prepare calls, the user is
3338 * expected to make at least one call to ring_buffer_prepare_sync.
3339 * Afterwards, ring_buffer_read_start is invoked to get things going
3340 * for real.
3341 *
3342 * This overall must be paired with ring_buffer_finish.
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003343 */
3344struct ring_buffer_iter *
David Miller72c9ddf2010-04-20 15:47:11 -07003345ring_buffer_read_prepare(struct ring_buffer *buffer, int cpu)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003346{
3347 struct ring_buffer_per_cpu *cpu_buffer;
Steven Rostedt8aabee52009-03-12 13:13:49 -04003348 struct ring_buffer_iter *iter;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003349
Rusty Russell9e01c1b2009-01-01 10:12:22 +10303350 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedt8aabee52009-03-12 13:13:49 -04003351 return NULL;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003352
3353 iter = kmalloc(sizeof(*iter), GFP_KERNEL);
3354 if (!iter)
Steven Rostedt8aabee52009-03-12 13:13:49 -04003355 return NULL;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003356
3357 cpu_buffer = buffer->buffers[cpu];
3358
3359 iter->cpu_buffer = cpu_buffer;
3360
3361 atomic_inc(&cpu_buffer->record_disabled);
David Miller72c9ddf2010-04-20 15:47:11 -07003362
3363 return iter;
3364}
3365EXPORT_SYMBOL_GPL(ring_buffer_read_prepare);
3366
3367/**
3368 * ring_buffer_read_prepare_sync - Synchronize a set of prepare calls
3369 *
3370 * All previously invoked ring_buffer_read_prepare calls to prepare
3371 * iterators will be synchronized. Afterwards, read_buffer_read_start
3372 * calls on those iterators are allowed.
3373 */
3374void
3375ring_buffer_read_prepare_sync(void)
3376{
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003377 synchronize_sched();
David Miller72c9ddf2010-04-20 15:47:11 -07003378}
3379EXPORT_SYMBOL_GPL(ring_buffer_read_prepare_sync);
3380
3381/**
3382 * ring_buffer_read_start - start a non consuming read of the buffer
3383 * @iter: The iterator returned by ring_buffer_read_prepare
3384 *
3385 * This finalizes the startup of an iteration through the buffer.
3386 * The iterator comes from a call to ring_buffer_read_prepare and
3387 * an intervening ring_buffer_read_prepare_sync must have been
3388 * performed.
3389 *
3390 * Must be paired with ring_buffer_finish.
3391 */
3392void
3393ring_buffer_read_start(struct ring_buffer_iter *iter)
3394{
3395 struct ring_buffer_per_cpu *cpu_buffer;
3396 unsigned long flags;
3397
3398 if (!iter)
3399 return;
3400
3401 cpu_buffer = iter->cpu_buffer;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003402
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003403 spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
Thomas Gleixner0199c4e2009-12-02 20:01:25 +01003404 arch_spin_lock(&cpu_buffer->lock);
Steven Rostedt642edba2008-11-12 00:01:26 -05003405 rb_iter_reset(iter);
Thomas Gleixner0199c4e2009-12-02 20:01:25 +01003406 arch_spin_unlock(&cpu_buffer->lock);
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003407 spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003408}
Robert Richterc4f50182008-12-11 16:49:22 +01003409EXPORT_SYMBOL_GPL(ring_buffer_read_start);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003410
3411/**
3412 * ring_buffer_finish - finish reading the iterator of the buffer
3413 * @iter: The iterator retrieved by ring_buffer_start
3414 *
3415 * This re-enables the recording to the buffer, and frees the
3416 * iterator.
3417 */
3418void
3419ring_buffer_read_finish(struct ring_buffer_iter *iter)
3420{
3421 struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
3422
3423 atomic_dec(&cpu_buffer->record_disabled);
3424 kfree(iter);
3425}
Robert Richterc4f50182008-12-11 16:49:22 +01003426EXPORT_SYMBOL_GPL(ring_buffer_read_finish);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003427
3428/**
3429 * ring_buffer_read - read the next item in the ring buffer by the iterator
3430 * @iter: The ring buffer iterator
3431 * @ts: The time stamp of the event read.
3432 *
3433 * This reads the next event in the ring buffer and increments the iterator.
3434 */
3435struct ring_buffer_event *
3436ring_buffer_read(struct ring_buffer_iter *iter, u64 *ts)
3437{
3438 struct ring_buffer_event *event;
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003439 struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
3440 unsigned long flags;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003441
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003442 spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
Steven Rostedt7e9391c2009-09-03 10:02:09 -04003443 again:
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003444 event = rb_iter_peek(iter, ts);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003445 if (!event)
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003446 goto out;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003447
Steven Rostedt7e9391c2009-09-03 10:02:09 -04003448 if (event->type_len == RINGBUF_TYPE_PADDING)
3449 goto again;
3450
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003451 rb_advance_iter(iter);
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003452 out:
3453 spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003454
3455 return event;
3456}
Robert Richterc4f50182008-12-11 16:49:22 +01003457EXPORT_SYMBOL_GPL(ring_buffer_read);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003458
3459/**
3460 * ring_buffer_size - return the size of the ring buffer (in bytes)
3461 * @buffer: The ring buffer.
3462 */
3463unsigned long ring_buffer_size(struct ring_buffer *buffer)
3464{
3465 return BUF_PAGE_SIZE * buffer->pages;
3466}
Robert Richterc4f50182008-12-11 16:49:22 +01003467EXPORT_SYMBOL_GPL(ring_buffer_size);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003468
3469static void
3470rb_reset_cpu(struct ring_buffer_per_cpu *cpu_buffer)
3471{
Steven Rostedt77ae3652009-03-27 11:00:29 -04003472 rb_head_page_deactivate(cpu_buffer);
3473
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003474 cpu_buffer->head_page
Steven Rostedt3adc54f2009-03-30 15:32:01 -04003475 = list_entry(cpu_buffer->pages, struct buffer_page, list);
Steven Rostedtbf41a152008-10-04 02:00:59 -04003476 local_set(&cpu_buffer->head_page->write, 0);
Steven Rostedt778c55d2009-05-01 18:44:45 -04003477 local_set(&cpu_buffer->head_page->entries, 0);
Steven Rostedtabc9b562008-12-02 15:34:06 -05003478 local_set(&cpu_buffer->head_page->page->commit, 0);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003479
Steven Rostedt6f807ac2008-10-04 02:00:58 -04003480 cpu_buffer->head_page->read = 0;
Steven Rostedtbf41a152008-10-04 02:00:59 -04003481
3482 cpu_buffer->tail_page = cpu_buffer->head_page;
3483 cpu_buffer->commit_page = cpu_buffer->head_page;
3484
3485 INIT_LIST_HEAD(&cpu_buffer->reader_page->list);
3486 local_set(&cpu_buffer->reader_page->write, 0);
Steven Rostedt778c55d2009-05-01 18:44:45 -04003487 local_set(&cpu_buffer->reader_page->entries, 0);
Steven Rostedtabc9b562008-12-02 15:34:06 -05003488 local_set(&cpu_buffer->reader_page->page->commit, 0);
Steven Rostedt6f807ac2008-10-04 02:00:58 -04003489 cpu_buffer->reader_page->read = 0;
Steven Rostedtd7690412008-10-01 00:29:53 -04003490
Steven Rostedt77ae3652009-03-27 11:00:29 -04003491 local_set(&cpu_buffer->commit_overrun, 0);
3492 local_set(&cpu_buffer->overrun, 0);
Steven Rostedte4906ef2009-04-30 20:49:44 -04003493 local_set(&cpu_buffer->entries, 0);
Steven Rostedtfa743952009-06-16 12:37:57 -04003494 local_set(&cpu_buffer->committing, 0);
3495 local_set(&cpu_buffer->commits, 0);
Steven Rostedt77ae3652009-03-27 11:00:29 -04003496 cpu_buffer->read = 0;
Steven Rostedt69507c02009-01-21 18:45:57 -05003497
3498 cpu_buffer->write_stamp = 0;
3499 cpu_buffer->read_stamp = 0;
Steven Rostedt77ae3652009-03-27 11:00:29 -04003500
Steven Rostedt66a8cb92010-03-31 13:21:56 -04003501 cpu_buffer->lost_events = 0;
3502 cpu_buffer->last_overrun = 0;
3503
Steven Rostedt77ae3652009-03-27 11:00:29 -04003504 rb_head_page_activate(cpu_buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003505}
3506
3507/**
3508 * ring_buffer_reset_cpu - reset a ring buffer per CPU buffer
3509 * @buffer: The ring buffer to reset a per cpu buffer of
3510 * @cpu: The CPU buffer to be reset
3511 */
3512void ring_buffer_reset_cpu(struct ring_buffer *buffer, int cpu)
3513{
3514 struct ring_buffer_per_cpu *cpu_buffer = buffer->buffers[cpu];
3515 unsigned long flags;
3516
Rusty Russell9e01c1b2009-01-01 10:12:22 +10303517 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedt8aabee52009-03-12 13:13:49 -04003518 return;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003519
Steven Rostedt41ede232009-05-01 20:26:54 -04003520 atomic_inc(&cpu_buffer->record_disabled);
3521
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003522 spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
3523
Steven Rostedt41b6a952009-09-02 09:59:48 -04003524 if (RB_WARN_ON(cpu_buffer, local_read(&cpu_buffer->committing)))
3525 goto out;
3526
Thomas Gleixner0199c4e2009-12-02 20:01:25 +01003527 arch_spin_lock(&cpu_buffer->lock);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003528
3529 rb_reset_cpu(cpu_buffer);
3530
Thomas Gleixner0199c4e2009-12-02 20:01:25 +01003531 arch_spin_unlock(&cpu_buffer->lock);
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003532
Steven Rostedt41b6a952009-09-02 09:59:48 -04003533 out:
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003534 spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
Steven Rostedt41ede232009-05-01 20:26:54 -04003535
3536 atomic_dec(&cpu_buffer->record_disabled);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003537}
Robert Richterc4f50182008-12-11 16:49:22 +01003538EXPORT_SYMBOL_GPL(ring_buffer_reset_cpu);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003539
3540/**
3541 * ring_buffer_reset - reset a ring buffer
3542 * @buffer: The ring buffer to reset all cpu buffers
3543 */
3544void ring_buffer_reset(struct ring_buffer *buffer)
3545{
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003546 int cpu;
3547
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003548 for_each_buffer_cpu(buffer, cpu)
Steven Rostedtd7690412008-10-01 00:29:53 -04003549 ring_buffer_reset_cpu(buffer, cpu);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003550}
Robert Richterc4f50182008-12-11 16:49:22 +01003551EXPORT_SYMBOL_GPL(ring_buffer_reset);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003552
3553/**
3554 * rind_buffer_empty - is the ring buffer empty?
3555 * @buffer: The ring buffer to test
3556 */
3557int ring_buffer_empty(struct ring_buffer *buffer)
3558{
3559 struct ring_buffer_per_cpu *cpu_buffer;
Steven Rostedtd4788202009-06-17 00:39:43 -04003560 unsigned long flags;
Steven Rostedt8d707e82009-06-16 21:22:48 -04003561 int dolock;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003562 int cpu;
Steven Rostedtd4788202009-06-17 00:39:43 -04003563 int ret;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003564
Steven Rostedt8d707e82009-06-16 21:22:48 -04003565 dolock = rb_ok_to_lock();
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003566
3567 /* yes this is racy, but if you don't like the race, lock the buffer */
3568 for_each_buffer_cpu(buffer, cpu) {
3569 cpu_buffer = buffer->buffers[cpu];
Steven Rostedt8d707e82009-06-16 21:22:48 -04003570 local_irq_save(flags);
3571 if (dolock)
3572 spin_lock(&cpu_buffer->reader_lock);
Steven Rostedtd4788202009-06-17 00:39:43 -04003573 ret = rb_per_cpu_empty(cpu_buffer);
Steven Rostedt8d707e82009-06-16 21:22:48 -04003574 if (dolock)
3575 spin_unlock(&cpu_buffer->reader_lock);
3576 local_irq_restore(flags);
3577
Steven Rostedtd4788202009-06-17 00:39:43 -04003578 if (!ret)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003579 return 0;
3580 }
Steven Rostedt554f7862009-03-11 22:00:13 -04003581
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003582 return 1;
3583}
Robert Richterc4f50182008-12-11 16:49:22 +01003584EXPORT_SYMBOL_GPL(ring_buffer_empty);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003585
3586/**
3587 * ring_buffer_empty_cpu - is a cpu buffer of a ring buffer empty?
3588 * @buffer: The ring buffer
3589 * @cpu: The CPU buffer to test
3590 */
3591int ring_buffer_empty_cpu(struct ring_buffer *buffer, int cpu)
3592{
3593 struct ring_buffer_per_cpu *cpu_buffer;
Steven Rostedtd4788202009-06-17 00:39:43 -04003594 unsigned long flags;
Steven Rostedt8d707e82009-06-16 21:22:48 -04003595 int dolock;
Steven Rostedt8aabee52009-03-12 13:13:49 -04003596 int ret;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003597
Rusty Russell9e01c1b2009-01-01 10:12:22 +10303598 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedt8aabee52009-03-12 13:13:49 -04003599 return 1;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003600
Steven Rostedt8d707e82009-06-16 21:22:48 -04003601 dolock = rb_ok_to_lock();
Steven Rostedt554f7862009-03-11 22:00:13 -04003602
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003603 cpu_buffer = buffer->buffers[cpu];
Steven Rostedt8d707e82009-06-16 21:22:48 -04003604 local_irq_save(flags);
3605 if (dolock)
3606 spin_lock(&cpu_buffer->reader_lock);
Steven Rostedt554f7862009-03-11 22:00:13 -04003607 ret = rb_per_cpu_empty(cpu_buffer);
Steven Rostedt8d707e82009-06-16 21:22:48 -04003608 if (dolock)
3609 spin_unlock(&cpu_buffer->reader_lock);
3610 local_irq_restore(flags);
Steven Rostedt554f7862009-03-11 22:00:13 -04003611
3612 return ret;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003613}
Robert Richterc4f50182008-12-11 16:49:22 +01003614EXPORT_SYMBOL_GPL(ring_buffer_empty_cpu);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003615
Steven Rostedt85bac322009-09-04 14:24:40 -04003616#ifdef CONFIG_RING_BUFFER_ALLOW_SWAP
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003617/**
3618 * ring_buffer_swap_cpu - swap a CPU buffer between two ring buffers
3619 * @buffer_a: One buffer to swap with
3620 * @buffer_b: The other buffer to swap with
3621 *
3622 * This function is useful for tracers that want to take a "snapshot"
3623 * of a CPU buffer and has another back up buffer lying around.
3624 * it is expected that the tracer handles the cpu buffer not being
3625 * used at the moment.
3626 */
3627int ring_buffer_swap_cpu(struct ring_buffer *buffer_a,
3628 struct ring_buffer *buffer_b, int cpu)
3629{
3630 struct ring_buffer_per_cpu *cpu_buffer_a;
3631 struct ring_buffer_per_cpu *cpu_buffer_b;
Steven Rostedt554f7862009-03-11 22:00:13 -04003632 int ret = -EINVAL;
3633
Rusty Russell9e01c1b2009-01-01 10:12:22 +10303634 if (!cpumask_test_cpu(cpu, buffer_a->cpumask) ||
3635 !cpumask_test_cpu(cpu, buffer_b->cpumask))
Steven Rostedt554f7862009-03-11 22:00:13 -04003636 goto out;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003637
3638 /* At least make sure the two buffers are somewhat the same */
Lai Jiangshan6d102bc2008-12-17 17:48:23 +08003639 if (buffer_a->pages != buffer_b->pages)
Steven Rostedt554f7862009-03-11 22:00:13 -04003640 goto out;
3641
3642 ret = -EAGAIN;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003643
Steven Rostedt97b17ef2009-01-21 15:24:56 -05003644 if (ring_buffer_flags != RB_BUFFERS_ON)
Steven Rostedt554f7862009-03-11 22:00:13 -04003645 goto out;
Steven Rostedt97b17ef2009-01-21 15:24:56 -05003646
3647 if (atomic_read(&buffer_a->record_disabled))
Steven Rostedt554f7862009-03-11 22:00:13 -04003648 goto out;
Steven Rostedt97b17ef2009-01-21 15:24:56 -05003649
3650 if (atomic_read(&buffer_b->record_disabled))
Steven Rostedt554f7862009-03-11 22:00:13 -04003651 goto out;
Steven Rostedt97b17ef2009-01-21 15:24:56 -05003652
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003653 cpu_buffer_a = buffer_a->buffers[cpu];
3654 cpu_buffer_b = buffer_b->buffers[cpu];
3655
Steven Rostedt97b17ef2009-01-21 15:24:56 -05003656 if (atomic_read(&cpu_buffer_a->record_disabled))
Steven Rostedt554f7862009-03-11 22:00:13 -04003657 goto out;
Steven Rostedt97b17ef2009-01-21 15:24:56 -05003658
3659 if (atomic_read(&cpu_buffer_b->record_disabled))
Steven Rostedt554f7862009-03-11 22:00:13 -04003660 goto out;
Steven Rostedt97b17ef2009-01-21 15:24:56 -05003661
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003662 /*
3663 * We can't do a synchronize_sched here because this
3664 * function can be called in atomic context.
3665 * Normally this will be called from the same CPU as cpu.
3666 * If not it's up to the caller to protect this.
3667 */
3668 atomic_inc(&cpu_buffer_a->record_disabled);
3669 atomic_inc(&cpu_buffer_b->record_disabled);
3670
Steven Rostedt98277992009-09-02 10:56:15 -04003671 ret = -EBUSY;
3672 if (local_read(&cpu_buffer_a->committing))
3673 goto out_dec;
3674 if (local_read(&cpu_buffer_b->committing))
3675 goto out_dec;
3676
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003677 buffer_a->buffers[cpu] = cpu_buffer_b;
3678 buffer_b->buffers[cpu] = cpu_buffer_a;
3679
3680 cpu_buffer_b->buffer = buffer_a;
3681 cpu_buffer_a->buffer = buffer_b;
3682
Steven Rostedt98277992009-09-02 10:56:15 -04003683 ret = 0;
3684
3685out_dec:
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003686 atomic_dec(&cpu_buffer_a->record_disabled);
3687 atomic_dec(&cpu_buffer_b->record_disabled);
Steven Rostedt554f7862009-03-11 22:00:13 -04003688out:
Steven Rostedt554f7862009-03-11 22:00:13 -04003689 return ret;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003690}
Robert Richterc4f50182008-12-11 16:49:22 +01003691EXPORT_SYMBOL_GPL(ring_buffer_swap_cpu);
Steven Rostedt85bac322009-09-04 14:24:40 -04003692#endif /* CONFIG_RING_BUFFER_ALLOW_SWAP */
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003693
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003694/**
3695 * ring_buffer_alloc_read_page - allocate a page to read from buffer
3696 * @buffer: the buffer to allocate for.
3697 *
3698 * This function is used in conjunction with ring_buffer_read_page.
3699 * When reading a full page from the ring buffer, these functions
3700 * can be used to speed up the process. The calling function should
3701 * allocate a few pages first with this function. Then when it
3702 * needs to get pages from the ring buffer, it passes the result
3703 * of this function into ring_buffer_read_page, which will swap
3704 * the page that was allocated, with the read page of the buffer.
3705 *
3706 * Returns:
3707 * The page allocated, or NULL on error.
3708 */
3709void *ring_buffer_alloc_read_page(struct ring_buffer *buffer)
3710{
Steven Rostedt044fa782008-12-02 23:50:03 -05003711 struct buffer_data_page *bpage;
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003712 unsigned long addr;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003713
3714 addr = __get_free_page(GFP_KERNEL);
3715 if (!addr)
3716 return NULL;
3717
Steven Rostedt044fa782008-12-02 23:50:03 -05003718 bpage = (void *)addr;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003719
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003720 rb_init_page(bpage);
3721
Steven Rostedt044fa782008-12-02 23:50:03 -05003722 return bpage;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003723}
Steven Rostedtd6ce96d2009-05-05 01:15:24 -04003724EXPORT_SYMBOL_GPL(ring_buffer_alloc_read_page);
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003725
3726/**
3727 * ring_buffer_free_read_page - free an allocated read page
3728 * @buffer: the buffer the page was allocate for
3729 * @data: the page to free
3730 *
3731 * Free a page allocated from ring_buffer_alloc_read_page.
3732 */
3733void ring_buffer_free_read_page(struct ring_buffer *buffer, void *data)
3734{
3735 free_page((unsigned long)data);
3736}
Steven Rostedtd6ce96d2009-05-05 01:15:24 -04003737EXPORT_SYMBOL_GPL(ring_buffer_free_read_page);
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003738
3739/**
3740 * ring_buffer_read_page - extract a page from the ring buffer
3741 * @buffer: buffer to extract from
3742 * @data_page: the page to use allocated from ring_buffer_alloc_read_page
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003743 * @len: amount to extract
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003744 * @cpu: the cpu of the buffer to extract
3745 * @full: should the extraction only happen when the page is full.
3746 *
3747 * This function will pull out a page from the ring buffer and consume it.
3748 * @data_page must be the address of the variable that was returned
3749 * from ring_buffer_alloc_read_page. This is because the page might be used
3750 * to swap with a page in the ring buffer.
3751 *
3752 * for example:
Lai Jiangshanb85fa012009-02-09 14:21:14 +08003753 * rpage = ring_buffer_alloc_read_page(buffer);
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003754 * if (!rpage)
3755 * return error;
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003756 * ret = ring_buffer_read_page(buffer, &rpage, len, cpu, 0);
Lai Jiangshan667d2412009-02-09 14:21:17 +08003757 * if (ret >= 0)
3758 * process_page(rpage, ret);
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003759 *
3760 * When @full is set, the function will not return true unless
3761 * the writer is off the reader page.
3762 *
3763 * Note: it is up to the calling functions to handle sleeps and wakeups.
3764 * The ring buffer can be used anywhere in the kernel and can not
3765 * blindly call wake_up. The layer that uses the ring buffer must be
3766 * responsible for that.
3767 *
3768 * Returns:
Lai Jiangshan667d2412009-02-09 14:21:17 +08003769 * >=0 if data has been transferred, returns the offset of consumed data.
3770 * <0 if no data has been transferred.
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003771 */
3772int ring_buffer_read_page(struct ring_buffer *buffer,
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003773 void **data_page, size_t len, int cpu, int full)
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003774{
3775 struct ring_buffer_per_cpu *cpu_buffer = buffer->buffers[cpu];
3776 struct ring_buffer_event *event;
Steven Rostedt044fa782008-12-02 23:50:03 -05003777 struct buffer_data_page *bpage;
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003778 struct buffer_page *reader;
Steven Rostedtff0ff842010-03-31 22:11:42 -04003779 unsigned long missed_events;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003780 unsigned long flags;
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003781 unsigned int commit;
Lai Jiangshan667d2412009-02-09 14:21:17 +08003782 unsigned int read;
Steven Rostedt4f3640f2009-03-03 23:52:42 -05003783 u64 save_timestamp;
Lai Jiangshan667d2412009-02-09 14:21:17 +08003784 int ret = -1;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003785
Steven Rostedt554f7862009-03-11 22:00:13 -04003786 if (!cpumask_test_cpu(cpu, buffer->cpumask))
3787 goto out;
3788
Steven Rostedt474d32b2009-03-03 19:51:40 -05003789 /*
3790 * If len is not big enough to hold the page header, then
3791 * we can not copy anything.
3792 */
3793 if (len <= BUF_PAGE_HDR_SIZE)
Steven Rostedt554f7862009-03-11 22:00:13 -04003794 goto out;
Steven Rostedt474d32b2009-03-03 19:51:40 -05003795
3796 len -= BUF_PAGE_HDR_SIZE;
3797
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003798 if (!data_page)
Steven Rostedt554f7862009-03-11 22:00:13 -04003799 goto out;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003800
Steven Rostedt044fa782008-12-02 23:50:03 -05003801 bpage = *data_page;
3802 if (!bpage)
Steven Rostedt554f7862009-03-11 22:00:13 -04003803 goto out;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003804
3805 spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
3806
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003807 reader = rb_get_reader_page(cpu_buffer);
3808 if (!reader)
Steven Rostedt554f7862009-03-11 22:00:13 -04003809 goto out_unlock;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003810
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003811 event = rb_reader_event(cpu_buffer);
Lai Jiangshan667d2412009-02-09 14:21:17 +08003812
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003813 read = reader->read;
3814 commit = rb_page_commit(reader);
3815
Steven Rostedt66a8cb92010-03-31 13:21:56 -04003816 /* Check if any events were dropped */
Steven Rostedtff0ff842010-03-31 22:11:42 -04003817 missed_events = cpu_buffer->lost_events;
Steven Rostedt66a8cb92010-03-31 13:21:56 -04003818
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003819 /*
Steven Rostedt474d32b2009-03-03 19:51:40 -05003820 * If this page has been partially read or
3821 * if len is not big enough to read the rest of the page or
3822 * a writer is still on the page, then
3823 * we must copy the data from the page to the buffer.
3824 * Otherwise, we can simply swap the page with the one passed in.
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003825 */
Steven Rostedt474d32b2009-03-03 19:51:40 -05003826 if (read || (len < (commit - read)) ||
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003827 cpu_buffer->reader_page == cpu_buffer->commit_page) {
Lai Jiangshan667d2412009-02-09 14:21:17 +08003828 struct buffer_data_page *rpage = cpu_buffer->reader_page->page;
Steven Rostedt474d32b2009-03-03 19:51:40 -05003829 unsigned int rpos = read;
3830 unsigned int pos = 0;
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003831 unsigned int size;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003832
3833 if (full)
Steven Rostedt554f7862009-03-11 22:00:13 -04003834 goto out_unlock;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003835
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003836 if (len > (commit - read))
3837 len = (commit - read);
3838
Steven Rostedt69d1b832010-10-07 18:18:05 -04003839 /* Always keep the time extend and data together */
3840 size = rb_event_ts_length(event);
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003841
3842 if (len < size)
Steven Rostedt554f7862009-03-11 22:00:13 -04003843 goto out_unlock;
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003844
Steven Rostedt4f3640f2009-03-03 23:52:42 -05003845 /* save the current timestamp, since the user will need it */
3846 save_timestamp = cpu_buffer->read_stamp;
3847
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003848 /* Need to copy one event at a time */
3849 do {
Steven Rostedt474d32b2009-03-03 19:51:40 -05003850 memcpy(bpage->data + pos, rpage->data + rpos, size);
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003851
3852 len -= size;
3853
3854 rb_advance_reader(cpu_buffer);
Steven Rostedt474d32b2009-03-03 19:51:40 -05003855 rpos = reader->read;
3856 pos += size;
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003857
Huang Ying18fab912010-07-28 14:14:01 +08003858 if (rpos >= commit)
3859 break;
3860
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003861 event = rb_reader_event(cpu_buffer);
Steven Rostedt69d1b832010-10-07 18:18:05 -04003862 /* Always keep the time extend and data together */
3863 size = rb_event_ts_length(event);
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003864 } while (len > size);
Lai Jiangshan667d2412009-02-09 14:21:17 +08003865
3866 /* update bpage */
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003867 local_set(&bpage->commit, pos);
Steven Rostedt4f3640f2009-03-03 23:52:42 -05003868 bpage->time_stamp = save_timestamp;
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003869
Steven Rostedt474d32b2009-03-03 19:51:40 -05003870 /* we copied everything to the beginning */
3871 read = 0;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003872 } else {
Steven Rostedtafbab762009-05-01 19:40:05 -04003873 /* update the entry counter */
Steven Rostedt77ae3652009-03-27 11:00:29 -04003874 cpu_buffer->read += rb_page_entries(reader);
Steven Rostedtafbab762009-05-01 19:40:05 -04003875
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003876 /* swap the pages */
Steven Rostedt044fa782008-12-02 23:50:03 -05003877 rb_init_page(bpage);
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003878 bpage = reader->page;
3879 reader->page = *data_page;
3880 local_set(&reader->write, 0);
Steven Rostedt778c55d2009-05-01 18:44:45 -04003881 local_set(&reader->entries, 0);
Steven Rostedtef7a4a12009-03-03 00:27:49 -05003882 reader->read = 0;
Steven Rostedt044fa782008-12-02 23:50:03 -05003883 *data_page = bpage;
Steven Rostedtff0ff842010-03-31 22:11:42 -04003884
3885 /*
3886 * Use the real_end for the data size,
3887 * This gives us a chance to store the lost events
3888 * on the page.
3889 */
3890 if (reader->real_end)
3891 local_set(&bpage->commit, reader->real_end);
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003892 }
Lai Jiangshan667d2412009-02-09 14:21:17 +08003893 ret = read;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003894
Steven Rostedt66a8cb92010-03-31 13:21:56 -04003895 cpu_buffer->lost_events = 0;
Steven Rostedt2711ca22010-05-21 13:32:26 -04003896
3897 commit = local_read(&bpage->commit);
Steven Rostedt66a8cb92010-03-31 13:21:56 -04003898 /*
3899 * Set a flag in the commit field if we lost events
3900 */
Steven Rostedtff0ff842010-03-31 22:11:42 -04003901 if (missed_events) {
Steven Rostedtff0ff842010-03-31 22:11:42 -04003902 /* If there is room at the end of the page to save the
3903 * missed events, then record it there.
3904 */
3905 if (BUF_PAGE_SIZE - commit >= sizeof(missed_events)) {
3906 memcpy(&bpage->data[commit], &missed_events,
3907 sizeof(missed_events));
3908 local_add(RB_MISSED_STORED, &bpage->commit);
Steven Rostedt2711ca22010-05-21 13:32:26 -04003909 commit += sizeof(missed_events);
Steven Rostedtff0ff842010-03-31 22:11:42 -04003910 }
Steven Rostedt66a8cb92010-03-31 13:21:56 -04003911 local_add(RB_MISSED_EVENTS, &bpage->commit);
Steven Rostedtff0ff842010-03-31 22:11:42 -04003912 }
Steven Rostedt66a8cb92010-03-31 13:21:56 -04003913
Steven Rostedt2711ca22010-05-21 13:32:26 -04003914 /*
3915 * This page may be off to user land. Zero it out here.
3916 */
3917 if (commit < BUF_PAGE_SIZE)
3918 memset(&bpage->data[commit], 0, BUF_PAGE_SIZE - commit);
3919
Steven Rostedt554f7862009-03-11 22:00:13 -04003920 out_unlock:
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003921 spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
3922
Steven Rostedt554f7862009-03-11 22:00:13 -04003923 out:
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003924 return ret;
3925}
Steven Rostedtd6ce96d2009-05-05 01:15:24 -04003926EXPORT_SYMBOL_GPL(ring_buffer_read_page);
Steven Rostedt8789a9e2008-12-02 15:34:07 -05003927
Paul Mundt1155de42009-06-25 14:30:12 +09003928#ifdef CONFIG_TRACING
Steven Rostedta3583242008-11-11 15:01:42 -05003929static ssize_t
3930rb_simple_read(struct file *filp, char __user *ubuf,
3931 size_t cnt, loff_t *ppos)
3932{
Hannes Eder5e398412009-02-10 19:44:34 +01003933 unsigned long *p = filp->private_data;
Steven Rostedta3583242008-11-11 15:01:42 -05003934 char buf[64];
3935 int r;
3936
Steven Rostedt033601a2008-11-21 12:41:55 -05003937 if (test_bit(RB_BUFFERS_DISABLED_BIT, p))
3938 r = sprintf(buf, "permanently disabled\n");
3939 else
3940 r = sprintf(buf, "%d\n", test_bit(RB_BUFFERS_ON_BIT, p));
Steven Rostedta3583242008-11-11 15:01:42 -05003941
3942 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
3943}
3944
3945static ssize_t
3946rb_simple_write(struct file *filp, const char __user *ubuf,
3947 size_t cnt, loff_t *ppos)
3948{
Hannes Eder5e398412009-02-10 19:44:34 +01003949 unsigned long *p = filp->private_data;
Steven Rostedta3583242008-11-11 15:01:42 -05003950 char buf[64];
Hannes Eder5e398412009-02-10 19:44:34 +01003951 unsigned long val;
Steven Rostedta3583242008-11-11 15:01:42 -05003952 int ret;
3953
3954 if (cnt >= sizeof(buf))
3955 return -EINVAL;
3956
3957 if (copy_from_user(&buf, ubuf, cnt))
3958 return -EFAULT;
3959
3960 buf[cnt] = 0;
3961
3962 ret = strict_strtoul(buf, 10, &val);
3963 if (ret < 0)
3964 return ret;
3965
Steven Rostedt033601a2008-11-21 12:41:55 -05003966 if (val)
3967 set_bit(RB_BUFFERS_ON_BIT, p);
3968 else
3969 clear_bit(RB_BUFFERS_ON_BIT, p);
Steven Rostedta3583242008-11-11 15:01:42 -05003970
3971 (*ppos)++;
3972
3973 return cnt;
3974}
3975
Steven Rostedt5e2336a2009-03-05 21:44:55 -05003976static const struct file_operations rb_simple_fops = {
Steven Rostedta3583242008-11-11 15:01:42 -05003977 .open = tracing_open_generic,
3978 .read = rb_simple_read,
3979 .write = rb_simple_write,
3980};
3981
3982
3983static __init int rb_init_debugfs(void)
3984{
3985 struct dentry *d_tracer;
Steven Rostedta3583242008-11-11 15:01:42 -05003986
3987 d_tracer = tracing_init_dentry();
3988
Frederic Weisbecker5452af62009-03-27 00:25:38 +01003989 trace_create_file("tracing_on", 0644, d_tracer,
3990 &ring_buffer_flags, &rb_simple_fops);
Steven Rostedta3583242008-11-11 15:01:42 -05003991
3992 return 0;
3993}
3994
3995fs_initcall(rb_init_debugfs);
Paul Mundt1155de42009-06-25 14:30:12 +09003996#endif
Steven Rostedt554f7862009-03-11 22:00:13 -04003997
Steven Rostedt59222ef2009-03-12 11:46:03 -04003998#ifdef CONFIG_HOTPLUG_CPU
Frederic Weisbecker09c9e842009-03-21 04:33:36 +01003999static int rb_cpu_notify(struct notifier_block *self,
4000 unsigned long action, void *hcpu)
Steven Rostedt554f7862009-03-11 22:00:13 -04004001{
4002 struct ring_buffer *buffer =
4003 container_of(self, struct ring_buffer, cpu_notify);
4004 long cpu = (long)hcpu;
4005
4006 switch (action) {
4007 case CPU_UP_PREPARE:
4008 case CPU_UP_PREPARE_FROZEN:
Rusty Russell3f237a72009-06-12 21:15:30 +09304009 if (cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedt554f7862009-03-11 22:00:13 -04004010 return NOTIFY_OK;
4011
4012 buffer->buffers[cpu] =
4013 rb_allocate_cpu_buffer(buffer, cpu);
4014 if (!buffer->buffers[cpu]) {
4015 WARN(1, "failed to allocate ring buffer on CPU %ld\n",
4016 cpu);
4017 return NOTIFY_OK;
4018 }
4019 smp_wmb();
Rusty Russell3f237a72009-06-12 21:15:30 +09304020 cpumask_set_cpu(cpu, buffer->cpumask);
Steven Rostedt554f7862009-03-11 22:00:13 -04004021 break;
4022 case CPU_DOWN_PREPARE:
4023 case CPU_DOWN_PREPARE_FROZEN:
4024 /*
4025 * Do nothing.
4026 * If we were to free the buffer, then the user would
4027 * lose any trace that was in the buffer.
4028 */
4029 break;
4030 default:
4031 break;
4032 }
4033 return NOTIFY_OK;
4034}
4035#endif