blob: 6989df2ba1947bf58879ecf2d5cc4261c9fdfc2c [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 */
Steven Rostedt0b074362013-01-22 16:58:30 -05006#include <linux/ftrace_event.h>
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04007#include <linux/ring_buffer.h>
Ingo Molnar14131f22009-02-26 18:47:11 +01008#include <linux/trace_clock.h>
Steven Rostedt0b074362013-01-22 16:58:30 -05009#include <linux/trace_seq.h>
Steven Rostedt7a8e76a2008-09-29 23:02:38 -040010#include <linux/spinlock.h>
11#include <linux/debugfs.h>
12#include <linux/uaccess.h>
Steven Rostedta81bd802009-02-06 01:45:16 -050013#include <linux/hardirq.h>
Vegard Nossum1744a212009-02-28 08:29:44 +010014#include <linux/kmemcheck.h>
Steven Rostedt7a8e76a2008-09-29 23:02:38 -040015#include <linux/module.h>
16#include <linux/percpu.h>
17#include <linux/mutex.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090018#include <linux/slab.h>
Steven Rostedt7a8e76a2008-09-29 23:02:38 -040019#include <linux/init.h>
20#include <linux/hash.h>
21#include <linux/list.h>
Steven Rostedt554f7862009-03-11 22:00:13 -040022#include <linux/cpu.h>
Steven Rostedt7a8e76a2008-09-29 23:02:38 -040023#include <linux/fs.h>
24
Christoph Lameter79615762010-01-05 15:34:50 +090025#include <asm/local.h>
Steven Rostedt182e9f52008-11-03 23:15:56 -050026
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -070027static void update_pages_handler(struct work_struct *work);
28
Steven Rostedt033601a2008-11-21 12:41:55 -050029/*
Steven Rostedtd1b182a2009-04-15 16:53:47 -040030 * The ring buffer header is special. We must manually up keep it.
31 */
32int ring_buffer_print_entry_header(struct trace_seq *s)
33{
34 int ret;
35
Lai Jiangshan334d4162009-04-24 11:27:05 +080036 ret = trace_seq_printf(s, "# compressed entry header\n");
37 ret = trace_seq_printf(s, "\ttype_len : 5 bits\n");
Steven Rostedtd1b182a2009-04-15 16:53:47 -040038 ret = trace_seq_printf(s, "\ttime_delta : 27 bits\n");
39 ret = trace_seq_printf(s, "\tarray : 32 bits\n");
40 ret = trace_seq_printf(s, "\n");
41 ret = trace_seq_printf(s, "\tpadding : type == %d\n",
42 RINGBUF_TYPE_PADDING);
43 ret = trace_seq_printf(s, "\ttime_extend : type == %d\n",
44 RINGBUF_TYPE_TIME_EXTEND);
Lai Jiangshan334d4162009-04-24 11:27:05 +080045 ret = trace_seq_printf(s, "\tdata max type_len == %d\n",
46 RINGBUF_TYPE_DATA_TYPE_LEN_MAX);
Steven Rostedtd1b182a2009-04-15 16:53:47 -040047
48 return ret;
49}
50
51/*
Steven Rostedt5cc98542009-03-12 22:24:17 -040052 * The ring buffer is made up of a list of pages. A separate list of pages is
53 * allocated for each CPU. A writer may only write to a buffer that is
54 * associated with the CPU it is currently executing on. A reader may read
55 * from any per cpu buffer.
56 *
57 * The reader is special. For each per cpu buffer, the reader has its own
58 * reader page. When a reader has read the entire reader page, this reader
59 * page is swapped with another page in the ring buffer.
60 *
61 * Now, as long as the writer is off the reader page, the reader can do what
62 * ever it wants with that page. The writer will never write to that page
63 * again (as long as it is out of the ring buffer).
64 *
65 * Here's some silly ASCII art.
66 *
67 * +------+
68 * |reader| RING BUFFER
69 * |page |
70 * +------+ +---+ +---+ +---+
71 * | |-->| |-->| |
72 * +---+ +---+ +---+
73 * ^ |
74 * | |
75 * +---------------+
76 *
77 *
78 * +------+
79 * |reader| RING BUFFER
80 * |page |------------------v
81 * +------+ +---+ +---+ +---+
82 * | |-->| |-->| |
83 * +---+ +---+ +---+
84 * ^ |
85 * | |
86 * +---------------+
87 *
88 *
89 * +------+
90 * |reader| RING BUFFER
91 * |page |------------------v
92 * +------+ +---+ +---+ +---+
93 * ^ | |-->| |-->| |
94 * | +---+ +---+ +---+
95 * | |
96 * | |
97 * +------------------------------+
98 *
99 *
100 * +------+
101 * |buffer| RING BUFFER
102 * |page |------------------v
103 * +------+ +---+ +---+ +---+
104 * ^ | | | |-->| |
105 * | New +---+ +---+ +---+
106 * | Reader------^ |
107 * | page |
108 * +------------------------------+
109 *
110 *
111 * After we make this swap, the reader can hand this page off to the splice
112 * code and be done with it. It can even allocate a new page if it needs to
113 * and swap that into the ring buffer.
114 *
115 * We will be using cmpxchg soon to make all this lockless.
116 *
117 */
118
119/*
Steven Rostedt033601a2008-11-21 12:41:55 -0500120 * A fast way to enable or disable all ring buffers is to
121 * call tracing_on or tracing_off. Turning off the ring buffers
122 * prevents all ring buffers from being recorded to.
123 * Turning this switch on, makes it OK to write to the
124 * ring buffer, if the ring buffer is enabled itself.
125 *
126 * There's three layers that must be on in order to write
127 * to the ring buffer.
128 *
129 * 1) This global flag must be set.
130 * 2) The ring buffer must be enabled for recording.
131 * 3) The per cpu buffer must be enabled for recording.
132 *
133 * In case of an anomaly, this global flag has a bit set that
134 * will permantly disable all ring buffers.
135 */
136
137/*
138 * Global flag to disable all recording to ring buffers
139 * This has two bits: ON, DISABLED
140 *
141 * ON DISABLED
142 * ---- ----------
143 * 0 0 : ring buffers are off
144 * 1 0 : ring buffers are on
145 * X 1 : ring buffers are permanently disabled
146 */
147
148enum {
149 RB_BUFFERS_ON_BIT = 0,
150 RB_BUFFERS_DISABLED_BIT = 1,
151};
152
153enum {
154 RB_BUFFERS_ON = 1 << RB_BUFFERS_ON_BIT,
155 RB_BUFFERS_DISABLED = 1 << RB_BUFFERS_DISABLED_BIT,
156};
157
Hannes Eder5e398412009-02-10 19:44:34 +0100158static unsigned long ring_buffer_flags __read_mostly = RB_BUFFERS_ON;
Steven Rostedta3583242008-11-11 15:01:42 -0500159
Steven Rostedt499e5472012-02-22 15:50:28 -0500160/* Used for individual buffers (after the counter) */
161#define RB_BUFFER_OFF (1 << 20)
162
Steven Rostedt474d32b2009-03-03 19:51:40 -0500163#define BUF_PAGE_HDR_SIZE offsetof(struct buffer_data_page, data)
164
Steven Rostedta3583242008-11-11 15:01:42 -0500165/**
Steven Rostedt033601a2008-11-21 12:41:55 -0500166 * tracing_off_permanent - permanently disable ring buffers
167 *
168 * This function, once called, will disable all ring buffers
Wenji Huangc3706f02009-02-10 01:03:18 -0500169 * permanently.
Steven Rostedt033601a2008-11-21 12:41:55 -0500170 */
171void tracing_off_permanent(void)
172{
173 set_bit(RB_BUFFERS_DISABLED_BIT, &ring_buffer_flags);
Steven Rostedta3583242008-11-11 15:01:42 -0500174}
175
Steven Rostedte3d6bf02009-03-03 13:53:07 -0500176#define RB_EVNT_HDR_SIZE (offsetof(struct ring_buffer_event, array))
Andrew Morton67d34722009-01-09 12:27:09 -0800177#define RB_ALIGNMENT 4U
Lai Jiangshan334d4162009-04-24 11:27:05 +0800178#define RB_MAX_SMALL_DATA (RB_ALIGNMENT * RINGBUF_TYPE_DATA_TYPE_LEN_MAX)
Steven Rostedtc7b09302009-06-11 11:12:00 -0400179#define RB_EVNT_MIN_SIZE 8U /* two 32bit words */
Lai Jiangshan334d4162009-04-24 11:27:05 +0800180
James Hogan649508f2012-05-30 12:11:19 +0100181#ifndef CONFIG_HAVE_64BIT_ALIGNED_ACCESS
Steven Rostedt22710482010-03-18 17:54:19 -0400182# define RB_FORCE_8BYTE_ALIGNMENT 0
183# define RB_ARCH_ALIGNMENT RB_ALIGNMENT
184#else
185# define RB_FORCE_8BYTE_ALIGNMENT 1
186# define RB_ARCH_ALIGNMENT 8U
187#endif
188
James Hogan649508f2012-05-30 12:11:19 +0100189#define RB_ALIGN_DATA __aligned(RB_ARCH_ALIGNMENT)
190
Lai Jiangshan334d4162009-04-24 11:27:05 +0800191/* define RINGBUF_TYPE_DATA for 'case RINGBUF_TYPE_DATA:' */
192#define RINGBUF_TYPE_DATA 0 ... RINGBUF_TYPE_DATA_TYPE_LEN_MAX
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400193
194enum {
195 RB_LEN_TIME_EXTEND = 8,
196 RB_LEN_TIME_STAMP = 16,
197};
198
Steven Rostedt69d1b832010-10-07 18:18:05 -0400199#define skip_time_extend(event) \
200 ((struct ring_buffer_event *)((char *)event + RB_LEN_TIME_EXTEND))
201
Tom Zanussi2d622712009-03-22 03:30:49 -0500202static inline int rb_null_event(struct ring_buffer_event *event)
203{
Steven Rostedta1863c22009-09-03 10:23:58 -0400204 return event->type_len == RINGBUF_TYPE_PADDING && !event->time_delta;
Tom Zanussi2d622712009-03-22 03:30:49 -0500205}
206
207static void rb_event_set_padding(struct ring_buffer_event *event)
208{
Steven Rostedta1863c22009-09-03 10:23:58 -0400209 /* padding has a NULL time_delta */
Lai Jiangshan334d4162009-04-24 11:27:05 +0800210 event->type_len = RINGBUF_TYPE_PADDING;
Tom Zanussi2d622712009-03-22 03:30:49 -0500211 event->time_delta = 0;
212}
213
Tom Zanussi2d622712009-03-22 03:30:49 -0500214static unsigned
215rb_event_data_length(struct ring_buffer_event *event)
216{
217 unsigned length;
218
Lai Jiangshan334d4162009-04-24 11:27:05 +0800219 if (event->type_len)
220 length = event->type_len * RB_ALIGNMENT;
Tom Zanussi2d622712009-03-22 03:30:49 -0500221 else
222 length = event->array[0];
223 return length + RB_EVNT_HDR_SIZE;
224}
225
Steven Rostedt69d1b832010-10-07 18:18:05 -0400226/*
227 * Return the length of the given event. Will return
228 * the length of the time extend if the event is a
229 * time extend.
230 */
231static inline unsigned
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400232rb_event_length(struct ring_buffer_event *event)
233{
Lai Jiangshan334d4162009-04-24 11:27:05 +0800234 switch (event->type_len) {
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400235 case RINGBUF_TYPE_PADDING:
Tom Zanussi2d622712009-03-22 03:30:49 -0500236 if (rb_null_event(event))
237 /* undefined */
238 return -1;
Lai Jiangshan334d4162009-04-24 11:27:05 +0800239 return event->array[0] + RB_EVNT_HDR_SIZE;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400240
241 case RINGBUF_TYPE_TIME_EXTEND:
242 return RB_LEN_TIME_EXTEND;
243
244 case RINGBUF_TYPE_TIME_STAMP:
245 return RB_LEN_TIME_STAMP;
246
247 case RINGBUF_TYPE_DATA:
Tom Zanussi2d622712009-03-22 03:30:49 -0500248 return rb_event_data_length(event);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400249 default:
250 BUG();
251 }
252 /* not hit */
253 return 0;
254}
255
Steven Rostedt69d1b832010-10-07 18:18:05 -0400256/*
257 * Return total length of time extend and data,
258 * or just the event length for all other events.
259 */
260static inline unsigned
261rb_event_ts_length(struct ring_buffer_event *event)
262{
263 unsigned len = 0;
264
265 if (event->type_len == RINGBUF_TYPE_TIME_EXTEND) {
266 /* time extends include the data event after it */
267 len = RB_LEN_TIME_EXTEND;
268 event = skip_time_extend(event);
269 }
270 return len + rb_event_length(event);
271}
272
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400273/**
274 * ring_buffer_event_length - return the length of the event
275 * @event: the event to get the length of
Steven Rostedt69d1b832010-10-07 18:18:05 -0400276 *
277 * Returns the size of the data load of a data event.
278 * If the event is something other than a data event, it
279 * returns the size of the event itself. With the exception
280 * of a TIME EXTEND, where it still returns the size of the
281 * data load of the data event after it.
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400282 */
283unsigned ring_buffer_event_length(struct ring_buffer_event *event)
284{
Steven Rostedt69d1b832010-10-07 18:18:05 -0400285 unsigned length;
286
287 if (event->type_len == RINGBUF_TYPE_TIME_EXTEND)
288 event = skip_time_extend(event);
289
290 length = rb_event_length(event);
Lai Jiangshan334d4162009-04-24 11:27:05 +0800291 if (event->type_len > RINGBUF_TYPE_DATA_TYPE_LEN_MAX)
Robert Richter465634a2009-01-07 15:32:11 +0100292 return length;
293 length -= RB_EVNT_HDR_SIZE;
294 if (length > RB_MAX_SMALL_DATA + sizeof(event->array[0]))
295 length -= sizeof(event->array[0]);
296 return length;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400297}
Robert Richterc4f50182008-12-11 16:49:22 +0100298EXPORT_SYMBOL_GPL(ring_buffer_event_length);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400299
300/* inline for ring buffer fast paths */
Andrew Morton34a148b2009-01-09 12:27:09 -0800301static void *
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400302rb_event_data(struct ring_buffer_event *event)
303{
Steven Rostedt69d1b832010-10-07 18:18:05 -0400304 if (event->type_len == RINGBUF_TYPE_TIME_EXTEND)
305 event = skip_time_extend(event);
Lai Jiangshan334d4162009-04-24 11:27:05 +0800306 BUG_ON(event->type_len > RINGBUF_TYPE_DATA_TYPE_LEN_MAX);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400307 /* If length is in len field, then array[0] has the data */
Lai Jiangshan334d4162009-04-24 11:27:05 +0800308 if (event->type_len)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400309 return (void *)&event->array[0];
310 /* Otherwise length is in array[0] and array[1] has the data */
311 return (void *)&event->array[1];
312}
313
314/**
315 * ring_buffer_event_data - return the data of the event
316 * @event: the event to get the data from
317 */
318void *ring_buffer_event_data(struct ring_buffer_event *event)
319{
320 return rb_event_data(event);
321}
Robert Richterc4f50182008-12-11 16:49:22 +0100322EXPORT_SYMBOL_GPL(ring_buffer_event_data);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400323
324#define for_each_buffer_cpu(buffer, cpu) \
Rusty Russell9e01c1b2009-01-01 10:12:22 +1030325 for_each_cpu(cpu, buffer->cpumask)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400326
327#define TS_SHIFT 27
328#define TS_MASK ((1ULL << TS_SHIFT) - 1)
329#define TS_DELTA_TEST (~TS_MASK)
330
Steven Rostedt66a8cb92010-03-31 13:21:56 -0400331/* Flag when events were overwritten */
332#define RB_MISSED_EVENTS (1 << 31)
Steven Rostedtff0ff842010-03-31 22:11:42 -0400333/* Missed count stored at end */
334#define RB_MISSED_STORED (1 << 30)
Steven Rostedt66a8cb92010-03-31 13:21:56 -0400335
Steven Rostedtabc9b562008-12-02 15:34:06 -0500336struct buffer_data_page {
Steven Rostedte4c2ce82008-10-01 11:14:54 -0400337 u64 time_stamp; /* page time stamp */
Wenji Huangc3706f02009-02-10 01:03:18 -0500338 local_t commit; /* write committed index */
James Hogan649508f2012-05-30 12:11:19 +0100339 unsigned char data[] RB_ALIGN_DATA; /* data of buffer page */
Steven Rostedtabc9b562008-12-02 15:34:06 -0500340};
341
Steven Rostedt77ae3652009-03-27 11:00:29 -0400342/*
343 * Note, the buffer_page list must be first. The buffer pages
344 * are allocated in cache lines, which means that each buffer
345 * page will be at the beginning of a cache line, and thus
346 * the least significant bits will be zero. We use this to
347 * add flags in the list struct pointers, to make the ring buffer
348 * lockless.
349 */
Steven Rostedtabc9b562008-12-02 15:34:06 -0500350struct buffer_page {
Steven Rostedt778c55d2009-05-01 18:44:45 -0400351 struct list_head list; /* list of buffer pages */
Steven Rostedtabc9b562008-12-02 15:34:06 -0500352 local_t write; /* index for next write */
Steven Rostedt6f807ac2008-10-04 02:00:58 -0400353 unsigned read; /* index for next read */
Steven Rostedt778c55d2009-05-01 18:44:45 -0400354 local_t entries; /* entries on this page */
Steven Rostedtff0ff842010-03-31 22:11:42 -0400355 unsigned long real_end; /* real end of data */
Steven Rostedtabc9b562008-12-02 15:34:06 -0500356 struct buffer_data_page *page; /* Actual data page */
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400357};
358
Steven Rostedt77ae3652009-03-27 11:00:29 -0400359/*
360 * The buffer page counters, write and entries, must be reset
361 * atomically when crossing page boundaries. To synchronize this
362 * update, two counters are inserted into the number. One is
363 * the actual counter for the write position or count on the page.
364 *
365 * The other is a counter of updaters. Before an update happens
366 * the update partition of the counter is incremented. This will
367 * allow the updater to update the counter atomically.
368 *
369 * The counter is 20 bits, and the state data is 12.
370 */
371#define RB_WRITE_MASK 0xfffff
372#define RB_WRITE_INTCNT (1 << 20)
373
Steven Rostedt044fa782008-12-02 23:50:03 -0500374static void rb_init_page(struct buffer_data_page *bpage)
Steven Rostedtabc9b562008-12-02 15:34:06 -0500375{
Steven Rostedt044fa782008-12-02 23:50:03 -0500376 local_set(&bpage->commit, 0);
Steven Rostedtabc9b562008-12-02 15:34:06 -0500377}
378
Steven Rostedt474d32b2009-03-03 19:51:40 -0500379/**
380 * ring_buffer_page_len - the size of data on the page.
381 * @page: The page to read
382 *
383 * Returns the amount of data on the page, including buffer page header.
384 */
Steven Rostedtef7a4a12009-03-03 00:27:49 -0500385size_t ring_buffer_page_len(void *page)
386{
Steven Rostedt474d32b2009-03-03 19:51:40 -0500387 return local_read(&((struct buffer_data_page *)page)->commit)
388 + BUF_PAGE_HDR_SIZE;
Steven Rostedtef7a4a12009-03-03 00:27:49 -0500389}
390
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400391/*
Steven Rostedted568292008-09-29 23:02:40 -0400392 * Also stolen from mm/slob.c. Thanks to Mathieu Desnoyers for pointing
393 * this issue out.
394 */
Andrew Morton34a148b2009-01-09 12:27:09 -0800395static void free_buffer_page(struct buffer_page *bpage)
Steven Rostedted568292008-09-29 23:02:40 -0400396{
Andrew Morton34a148b2009-01-09 12:27:09 -0800397 free_page((unsigned long)bpage->page);
Steven Rostedte4c2ce82008-10-01 11:14:54 -0400398 kfree(bpage);
Steven Rostedted568292008-09-29 23:02:40 -0400399}
400
401/*
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400402 * We need to fit the time_stamp delta into 27 bits.
403 */
404static inline int test_time_stamp(u64 delta)
405{
406 if (delta & TS_DELTA_TEST)
407 return 1;
408 return 0;
409}
410
Steven Rostedt474d32b2009-03-03 19:51:40 -0500411#define BUF_PAGE_SIZE (PAGE_SIZE - BUF_PAGE_HDR_SIZE)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400412
Steven Rostedtbe957c42009-05-11 14:42:53 -0400413/* Max payload is BUF_PAGE_SIZE - header (8bytes) */
414#define BUF_MAX_DATA_SIZE (BUF_PAGE_SIZE - (sizeof(u32) * 2))
415
Steven Rostedtd1b182a2009-04-15 16:53:47 -0400416int ring_buffer_print_page_header(struct trace_seq *s)
417{
418 struct buffer_data_page field;
419 int ret;
420
421 ret = trace_seq_printf(s, "\tfield: u64 timestamp;\t"
Tom Zanussi26a50742009-10-06 01:09:50 -0500422 "offset:0;\tsize:%u;\tsigned:%u;\n",
423 (unsigned int)sizeof(field.time_stamp),
424 (unsigned int)is_signed_type(u64));
Steven Rostedtd1b182a2009-04-15 16:53:47 -0400425
426 ret = trace_seq_printf(s, "\tfield: local_t commit;\t"
Tom Zanussi26a50742009-10-06 01:09:50 -0500427 "offset:%u;\tsize:%u;\tsigned:%u;\n",
Steven Rostedtd1b182a2009-04-15 16:53:47 -0400428 (unsigned int)offsetof(typeof(field), commit),
Tom Zanussi26a50742009-10-06 01:09:50 -0500429 (unsigned int)sizeof(field.commit),
430 (unsigned int)is_signed_type(long));
Steven Rostedtd1b182a2009-04-15 16:53:47 -0400431
Steven Rostedt66a8cb92010-03-31 13:21:56 -0400432 ret = trace_seq_printf(s, "\tfield: int overwrite;\t"
433 "offset:%u;\tsize:%u;\tsigned:%u;\n",
434 (unsigned int)offsetof(typeof(field), commit),
435 1,
436 (unsigned int)is_signed_type(long));
437
Steven Rostedtd1b182a2009-04-15 16:53:47 -0400438 ret = trace_seq_printf(s, "\tfield: char data;\t"
Tom Zanussi26a50742009-10-06 01:09:50 -0500439 "offset:%u;\tsize:%u;\tsigned:%u;\n",
Steven Rostedtd1b182a2009-04-15 16:53:47 -0400440 (unsigned int)offsetof(typeof(field), data),
Tom Zanussi26a50742009-10-06 01:09:50 -0500441 (unsigned int)BUF_PAGE_SIZE,
442 (unsigned int)is_signed_type(char));
Steven Rostedtd1b182a2009-04-15 16:53:47 -0400443
444 return ret;
445}
446
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400447/*
448 * head_page == tail_page && head == tail then buffer is empty.
449 */
450struct ring_buffer_per_cpu {
451 int cpu;
Richard Kennedy985023d2010-03-25 11:27:36 +0000452 atomic_t record_disabled;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400453 struct ring_buffer *buffer;
Thomas Gleixner5389f6f2009-07-25 17:13:33 +0200454 raw_spinlock_t reader_lock; /* serialize readers */
Thomas Gleixner445c8952009-12-02 19:49:50 +0100455 arch_spinlock_t lock;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400456 struct lock_class_key lock_key;
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -0800457 unsigned int nr_pages;
Steven Rostedt3adc54f2009-03-30 15:32:01 -0400458 struct list_head *pages;
Steven Rostedt6f807ac2008-10-04 02:00:58 -0400459 struct buffer_page *head_page; /* read from head */
460 struct buffer_page *tail_page; /* write to tail */
Wenji Huangc3706f02009-02-10 01:03:18 -0500461 struct buffer_page *commit_page; /* committed pages */
Steven Rostedtd7690412008-10-01 00:29:53 -0400462 struct buffer_page *reader_page;
Steven Rostedt66a8cb92010-03-31 13:21:56 -0400463 unsigned long lost_events;
464 unsigned long last_overrun;
Vaibhav Nagarnaikc64e1482011-08-16 14:46:16 -0700465 local_t entries_bytes;
Steven Rostedte4906ef2009-04-30 20:49:44 -0400466 local_t entries;
Slava Pestov884bfe82011-07-15 14:23:58 -0700467 local_t overrun;
468 local_t commit_overrun;
469 local_t dropped_events;
Steven Rostedtfa743952009-06-16 12:37:57 -0400470 local_t committing;
471 local_t commits;
Steven Rostedt77ae3652009-03-27 11:00:29 -0400472 unsigned long read;
Vaibhav Nagarnaikc64e1482011-08-16 14:46:16 -0700473 unsigned long read_bytes;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400474 u64 write_stamp;
475 u64 read_stamp;
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -0800476 /* ring buffer pages to update, > 0 to add, < 0 to remove */
477 int nr_pages_to_update;
478 struct list_head new_pages; /* new pages to add */
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -0700479 struct work_struct update_pages_work;
Vaibhav Nagarnaik05fdd702012-05-18 13:29:51 -0700480 struct completion update_done;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400481};
482
483struct ring_buffer {
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400484 unsigned flags;
485 int cpus;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400486 atomic_t record_disabled;
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -0700487 atomic_t resize_disabled;
Arnaldo Carvalho de Melo00f62f62009-02-09 17:04:06 -0200488 cpumask_var_t cpumask;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400489
Peter Zijlstra1f8a6a12009-06-08 18:18:39 +0200490 struct lock_class_key *reader_lock_key;
491
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400492 struct mutex mutex;
493
494 struct ring_buffer_per_cpu **buffers;
Steven Rostedt554f7862009-03-11 22:00:13 -0400495
Steven Rostedt59222ef2009-03-12 11:46:03 -0400496#ifdef CONFIG_HOTPLUG_CPU
Steven Rostedt554f7862009-03-11 22:00:13 -0400497 struct notifier_block cpu_notify;
498#endif
Steven Rostedt37886f62009-03-17 17:22:06 -0400499 u64 (*clock)(void);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400500};
501
502struct ring_buffer_iter {
503 struct ring_buffer_per_cpu *cpu_buffer;
504 unsigned long head;
505 struct buffer_page *head_page;
Steven Rostedt492a74f2010-01-25 15:17:47 -0500506 struct buffer_page *cache_reader_page;
507 unsigned long cache_read;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400508 u64 read_stamp;
509};
510
Steven Rostedtf536aaf2008-11-10 23:07:30 -0500511/* buffer may be either ring_buffer or ring_buffer_per_cpu */
Steven Rostedt077c5402009-09-03 19:53:46 -0400512#define RB_WARN_ON(b, cond) \
513 ({ \
514 int _____ret = unlikely(cond); \
515 if (_____ret) { \
516 if (__same_type(*(b), struct ring_buffer_per_cpu)) { \
517 struct ring_buffer_per_cpu *__b = \
518 (void *)b; \
519 atomic_inc(&__b->buffer->record_disabled); \
520 } else \
521 atomic_inc(&b->record_disabled); \
522 WARN_ON(1); \
523 } \
524 _____ret; \
Steven Rostedt3e89c7b2008-11-11 15:28:41 -0500525 })
Steven Rostedtf536aaf2008-11-10 23:07:30 -0500526
Steven Rostedt37886f62009-03-17 17:22:06 -0400527/* Up this if you want to test the TIME_EXTENTS and normalization */
528#define DEBUG_SHIFT 0
529
Jiri Olsa6d3f1e12009-10-23 19:36:19 -0400530static inline u64 rb_time_stamp(struct ring_buffer *buffer)
Steven Rostedt88eb0122009-05-11 16:28:23 -0400531{
532 /* shift to debug/test normalization and TIME_EXTENTS */
533 return buffer->clock() << DEBUG_SHIFT;
534}
535
Steven Rostedt37886f62009-03-17 17:22:06 -0400536u64 ring_buffer_time_stamp(struct ring_buffer *buffer, int cpu)
537{
538 u64 time;
539
540 preempt_disable_notrace();
Jiri Olsa6d3f1e12009-10-23 19:36:19 -0400541 time = rb_time_stamp(buffer);
Steven Rostedt37886f62009-03-17 17:22:06 -0400542 preempt_enable_no_resched_notrace();
543
544 return time;
545}
546EXPORT_SYMBOL_GPL(ring_buffer_time_stamp);
547
548void ring_buffer_normalize_time_stamp(struct ring_buffer *buffer,
549 int cpu, u64 *ts)
550{
551 /* Just stupid testing the normalize function and deltas */
552 *ts >>= DEBUG_SHIFT;
553}
554EXPORT_SYMBOL_GPL(ring_buffer_normalize_time_stamp);
555
Steven Rostedt77ae3652009-03-27 11:00:29 -0400556/*
557 * Making the ring buffer lockless makes things tricky.
558 * Although writes only happen on the CPU that they are on,
559 * and they only need to worry about interrupts. Reads can
560 * happen on any CPU.
561 *
562 * The reader page is always off the ring buffer, but when the
563 * reader finishes with a page, it needs to swap its page with
564 * a new one from the buffer. The reader needs to take from
565 * the head (writes go to the tail). But if a writer is in overwrite
566 * mode and wraps, it must push the head page forward.
567 *
568 * Here lies the problem.
569 *
570 * The reader must be careful to replace only the head page, and
571 * not another one. As described at the top of the file in the
572 * ASCII art, the reader sets its old page to point to the next
573 * page after head. It then sets the page after head to point to
574 * the old reader page. But if the writer moves the head page
575 * during this operation, the reader could end up with the tail.
576 *
577 * We use cmpxchg to help prevent this race. We also do something
578 * special with the page before head. We set the LSB to 1.
579 *
580 * When the writer must push the page forward, it will clear the
581 * bit that points to the head page, move the head, and then set
582 * the bit that points to the new head page.
583 *
584 * We also don't want an interrupt coming in and moving the head
585 * page on another writer. Thus we use the second LSB to catch
586 * that too. Thus:
587 *
588 * head->list->prev->next bit 1 bit 0
589 * ------- -------
590 * Normal page 0 0
591 * Points to head page 0 1
592 * New head page 1 0
593 *
594 * Note we can not trust the prev pointer of the head page, because:
595 *
596 * +----+ +-----+ +-----+
597 * | |------>| T |---X--->| N |
598 * | |<------| | | |
599 * +----+ +-----+ +-----+
600 * ^ ^ |
601 * | +-----+ | |
602 * +----------| R |----------+ |
603 * | |<-----------+
604 * +-----+
605 *
606 * Key: ---X--> HEAD flag set in pointer
607 * T Tail page
608 * R Reader page
609 * N Next page
610 *
611 * (see __rb_reserve_next() to see where this happens)
612 *
613 * What the above shows is that the reader just swapped out
614 * the reader page with a page in the buffer, but before it
615 * could make the new header point back to the new page added
616 * it was preempted by a writer. The writer moved forward onto
617 * the new page added by the reader and is about to move forward
618 * again.
619 *
620 * You can see, it is legitimate for the previous pointer of
621 * the head (or any page) not to point back to itself. But only
622 * temporarially.
623 */
624
625#define RB_PAGE_NORMAL 0UL
626#define RB_PAGE_HEAD 1UL
627#define RB_PAGE_UPDATE 2UL
628
629
630#define RB_FLAG_MASK 3UL
631
632/* PAGE_MOVED is not part of the mask */
633#define RB_PAGE_MOVED 4UL
634
635/*
636 * rb_list_head - remove any bit
637 */
638static struct list_head *rb_list_head(struct list_head *list)
639{
640 unsigned long val = (unsigned long)list;
641
642 return (struct list_head *)(val & ~RB_FLAG_MASK);
643}
644
645/*
Jiri Olsa6d3f1e12009-10-23 19:36:19 -0400646 * rb_is_head_page - test if the given page is the head page
Steven Rostedt77ae3652009-03-27 11:00:29 -0400647 *
648 * Because the reader may move the head_page pointer, we can
649 * not trust what the head page is (it may be pointing to
650 * the reader page). But if the next page is a header page,
651 * its flags will be non zero.
652 */
Jesper Juhl42b16b32011-01-17 00:09:38 +0100653static inline int
Steven Rostedt77ae3652009-03-27 11:00:29 -0400654rb_is_head_page(struct ring_buffer_per_cpu *cpu_buffer,
655 struct buffer_page *page, struct list_head *list)
656{
657 unsigned long val;
658
659 val = (unsigned long)list->next;
660
661 if ((val & ~RB_FLAG_MASK) != (unsigned long)&page->list)
662 return RB_PAGE_MOVED;
663
664 return val & RB_FLAG_MASK;
665}
666
667/*
668 * rb_is_reader_page
669 *
670 * The unique thing about the reader page, is that, if the
671 * writer is ever on it, the previous pointer never points
672 * back to the reader page.
673 */
674static int rb_is_reader_page(struct buffer_page *page)
675{
676 struct list_head *list = page->list.prev;
677
678 return rb_list_head(list->next) != &page->list;
679}
680
681/*
682 * rb_set_list_to_head - set a list_head to be pointing to head.
683 */
684static void rb_set_list_to_head(struct ring_buffer_per_cpu *cpu_buffer,
685 struct list_head *list)
686{
687 unsigned long *ptr;
688
689 ptr = (unsigned long *)&list->next;
690 *ptr |= RB_PAGE_HEAD;
691 *ptr &= ~RB_PAGE_UPDATE;
692}
693
694/*
695 * rb_head_page_activate - sets up head page
696 */
697static void rb_head_page_activate(struct ring_buffer_per_cpu *cpu_buffer)
698{
699 struct buffer_page *head;
700
701 head = cpu_buffer->head_page;
702 if (!head)
703 return;
704
705 /*
706 * Set the previous list pointer to have the HEAD flag.
707 */
708 rb_set_list_to_head(cpu_buffer, head->list.prev);
709}
710
711static void rb_list_head_clear(struct list_head *list)
712{
713 unsigned long *ptr = (unsigned long *)&list->next;
714
715 *ptr &= ~RB_FLAG_MASK;
716}
717
718/*
719 * rb_head_page_dactivate - clears head page ptr (for free list)
720 */
721static void
722rb_head_page_deactivate(struct ring_buffer_per_cpu *cpu_buffer)
723{
724 struct list_head *hd;
725
726 /* Go through the whole list and clear any pointers found. */
727 rb_list_head_clear(cpu_buffer->pages);
728
729 list_for_each(hd, cpu_buffer->pages)
730 rb_list_head_clear(hd);
731}
732
733static int rb_head_page_set(struct ring_buffer_per_cpu *cpu_buffer,
734 struct buffer_page *head,
735 struct buffer_page *prev,
736 int old_flag, int new_flag)
737{
738 struct list_head *list;
739 unsigned long val = (unsigned long)&head->list;
740 unsigned long ret;
741
742 list = &prev->list;
743
744 val &= ~RB_FLAG_MASK;
745
Steven Rostedt08a40812009-09-14 09:31:35 -0400746 ret = cmpxchg((unsigned long *)&list->next,
747 val | old_flag, val | new_flag);
Steven Rostedt77ae3652009-03-27 11:00:29 -0400748
749 /* check if the reader took the page */
750 if ((ret & ~RB_FLAG_MASK) != val)
751 return RB_PAGE_MOVED;
752
753 return ret & RB_FLAG_MASK;
754}
755
756static int rb_head_page_set_update(struct ring_buffer_per_cpu *cpu_buffer,
757 struct buffer_page *head,
758 struct buffer_page *prev,
759 int old_flag)
760{
761 return rb_head_page_set(cpu_buffer, head, prev,
762 old_flag, RB_PAGE_UPDATE);
763}
764
765static int rb_head_page_set_head(struct ring_buffer_per_cpu *cpu_buffer,
766 struct buffer_page *head,
767 struct buffer_page *prev,
768 int old_flag)
769{
770 return rb_head_page_set(cpu_buffer, head, prev,
771 old_flag, RB_PAGE_HEAD);
772}
773
774static int rb_head_page_set_normal(struct ring_buffer_per_cpu *cpu_buffer,
775 struct buffer_page *head,
776 struct buffer_page *prev,
777 int old_flag)
778{
779 return rb_head_page_set(cpu_buffer, head, prev,
780 old_flag, RB_PAGE_NORMAL);
781}
782
783static inline void rb_inc_page(struct ring_buffer_per_cpu *cpu_buffer,
784 struct buffer_page **bpage)
785{
786 struct list_head *p = rb_list_head((*bpage)->list.next);
787
788 *bpage = list_entry(p, struct buffer_page, list);
789}
790
791static struct buffer_page *
792rb_set_head_page(struct ring_buffer_per_cpu *cpu_buffer)
793{
794 struct buffer_page *head;
795 struct buffer_page *page;
796 struct list_head *list;
797 int i;
798
799 if (RB_WARN_ON(cpu_buffer, !cpu_buffer->head_page))
800 return NULL;
801
802 /* sanity check */
803 list = cpu_buffer->pages;
804 if (RB_WARN_ON(cpu_buffer, rb_list_head(list->prev->next) != list))
805 return NULL;
806
807 page = head = cpu_buffer->head_page;
808 /*
809 * It is possible that the writer moves the header behind
810 * where we started, and we miss in one loop.
811 * A second loop should grab the header, but we'll do
812 * three loops just because I'm paranoid.
813 */
814 for (i = 0; i < 3; i++) {
815 do {
816 if (rb_is_head_page(cpu_buffer, page, page->list.prev)) {
817 cpu_buffer->head_page = page;
818 return page;
819 }
820 rb_inc_page(cpu_buffer, &page);
821 } while (page != head);
822 }
823
824 RB_WARN_ON(cpu_buffer, 1);
825
826 return NULL;
827}
828
829static int rb_head_page_replace(struct buffer_page *old,
830 struct buffer_page *new)
831{
832 unsigned long *ptr = (unsigned long *)&old->list.prev->next;
833 unsigned long val;
834 unsigned long ret;
835
836 val = *ptr & ~RB_FLAG_MASK;
837 val |= RB_PAGE_HEAD;
838
Steven Rostedt08a40812009-09-14 09:31:35 -0400839 ret = cmpxchg(ptr, val, (unsigned long)&new->list);
Steven Rostedt77ae3652009-03-27 11:00:29 -0400840
841 return ret == val;
842}
843
844/*
845 * rb_tail_page_update - move the tail page forward
846 *
847 * Returns 1 if moved tail page, 0 if someone else did.
848 */
849static int rb_tail_page_update(struct ring_buffer_per_cpu *cpu_buffer,
850 struct buffer_page *tail_page,
851 struct buffer_page *next_page)
852{
853 struct buffer_page *old_tail;
854 unsigned long old_entries;
855 unsigned long old_write;
856 int ret = 0;
857
858 /*
859 * The tail page now needs to be moved forward.
860 *
861 * We need to reset the tail page, but without messing
862 * with possible erasing of data brought in by interrupts
863 * that have moved the tail page and are currently on it.
864 *
865 * We add a counter to the write field to denote this.
866 */
867 old_write = local_add_return(RB_WRITE_INTCNT, &next_page->write);
868 old_entries = local_add_return(RB_WRITE_INTCNT, &next_page->entries);
869
870 /*
871 * Just make sure we have seen our old_write and synchronize
872 * with any interrupts that come in.
873 */
874 barrier();
875
876 /*
877 * If the tail page is still the same as what we think
878 * it is, then it is up to us to update the tail
879 * pointer.
880 */
881 if (tail_page == cpu_buffer->tail_page) {
882 /* Zero the write counter */
883 unsigned long val = old_write & ~RB_WRITE_MASK;
884 unsigned long eval = old_entries & ~RB_WRITE_MASK;
885
886 /*
887 * This will only succeed if an interrupt did
888 * not come in and change it. In which case, we
889 * do not want to modify it.
Lai Jiangshanda706d82009-07-15 16:27:30 +0800890 *
891 * We add (void) to let the compiler know that we do not care
892 * about the return value of these functions. We use the
893 * cmpxchg to only update if an interrupt did not already
894 * do it for us. If the cmpxchg fails, we don't care.
Steven Rostedt77ae3652009-03-27 11:00:29 -0400895 */
Lai Jiangshanda706d82009-07-15 16:27:30 +0800896 (void)local_cmpxchg(&next_page->write, old_write, val);
897 (void)local_cmpxchg(&next_page->entries, old_entries, eval);
Steven Rostedt77ae3652009-03-27 11:00:29 -0400898
899 /*
900 * No need to worry about races with clearing out the commit.
901 * it only can increment when a commit takes place. But that
902 * only happens in the outer most nested commit.
903 */
904 local_set(&next_page->page->commit, 0);
905
906 old_tail = cmpxchg(&cpu_buffer->tail_page,
907 tail_page, next_page);
908
909 if (old_tail == tail_page)
910 ret = 1;
911 }
912
913 return ret;
914}
915
916static int rb_check_bpage(struct ring_buffer_per_cpu *cpu_buffer,
917 struct buffer_page *bpage)
918{
919 unsigned long val = (unsigned long)bpage;
920
921 if (RB_WARN_ON(cpu_buffer, val & RB_FLAG_MASK))
922 return 1;
923
924 return 0;
925}
926
927/**
928 * rb_check_list - make sure a pointer to a list has the last bits zero
929 */
930static int rb_check_list(struct ring_buffer_per_cpu *cpu_buffer,
931 struct list_head *list)
932{
933 if (RB_WARN_ON(cpu_buffer, rb_list_head(list->prev) != list->prev))
934 return 1;
935 if (RB_WARN_ON(cpu_buffer, rb_list_head(list->next) != list->next))
936 return 1;
937 return 0;
938}
939
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400940/**
941 * check_pages - integrity check of buffer pages
942 * @cpu_buffer: CPU buffer with pages to test
943 *
Wenji Huangc3706f02009-02-10 01:03:18 -0500944 * As a safety measure we check to make sure the data pages have not
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400945 * been corrupted.
946 */
947static int rb_check_pages(struct ring_buffer_per_cpu *cpu_buffer)
948{
Steven Rostedt3adc54f2009-03-30 15:32:01 -0400949 struct list_head *head = cpu_buffer->pages;
Steven Rostedt044fa782008-12-02 23:50:03 -0500950 struct buffer_page *bpage, *tmp;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400951
Steven Rostedt308f7ee2012-05-16 19:46:32 -0400952 /* Reset the head page if it exists */
953 if (cpu_buffer->head_page)
954 rb_set_head_page(cpu_buffer);
955
Steven Rostedt77ae3652009-03-27 11:00:29 -0400956 rb_head_page_deactivate(cpu_buffer);
957
Steven Rostedt3e89c7b2008-11-11 15:28:41 -0500958 if (RB_WARN_ON(cpu_buffer, head->next->prev != head))
959 return -1;
960 if (RB_WARN_ON(cpu_buffer, head->prev->next != head))
961 return -1;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400962
Steven Rostedt77ae3652009-03-27 11:00:29 -0400963 if (rb_check_list(cpu_buffer, head))
964 return -1;
965
Steven Rostedt044fa782008-12-02 23:50:03 -0500966 list_for_each_entry_safe(bpage, tmp, head, list) {
Steven Rostedt3e89c7b2008-11-11 15:28:41 -0500967 if (RB_WARN_ON(cpu_buffer,
Steven Rostedt044fa782008-12-02 23:50:03 -0500968 bpage->list.next->prev != &bpage->list))
Steven Rostedt3e89c7b2008-11-11 15:28:41 -0500969 return -1;
970 if (RB_WARN_ON(cpu_buffer,
Steven Rostedt044fa782008-12-02 23:50:03 -0500971 bpage->list.prev->next != &bpage->list))
Steven Rostedt3e89c7b2008-11-11 15:28:41 -0500972 return -1;
Steven Rostedt77ae3652009-03-27 11:00:29 -0400973 if (rb_check_list(cpu_buffer, &bpage->list))
974 return -1;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400975 }
976
Steven Rostedt77ae3652009-03-27 11:00:29 -0400977 rb_head_page_activate(cpu_buffer);
978
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400979 return 0;
980}
981
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -0800982static int __rb_allocate_pages(int nr_pages, struct list_head *pages, int cpu)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400983{
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -0800984 int i;
Steven Rostedt044fa782008-12-02 23:50:03 -0500985 struct buffer_page *bpage, *tmp;
Steven Rostedt3adc54f2009-03-30 15:32:01 -0400986
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400987 for (i = 0; i < nr_pages; i++) {
Vaibhav Nagarnaik7ea59062011-05-03 17:56:42 -0700988 struct page *page;
Vaibhav Nagarnaikd7ec4bf2011-06-07 17:01:42 -0700989 /*
990 * __GFP_NORETRY flag makes sure that the allocation fails
991 * gracefully without invoking oom-killer and the system is
992 * not destabilized.
993 */
Steven Rostedt044fa782008-12-02 23:50:03 -0500994 bpage = kzalloc_node(ALIGN(sizeof(*bpage), cache_line_size()),
Vaibhav Nagarnaikd7ec4bf2011-06-07 17:01:42 -0700995 GFP_KERNEL | __GFP_NORETRY,
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -0800996 cpu_to_node(cpu));
Steven Rostedt044fa782008-12-02 23:50:03 -0500997 if (!bpage)
Steven Rostedte4c2ce82008-10-01 11:14:54 -0400998 goto free_pages;
Steven Rostedt77ae3652009-03-27 11:00:29 -0400999
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08001000 list_add(&bpage->list, pages);
Steven Rostedt77ae3652009-03-27 11:00:29 -04001001
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08001002 page = alloc_pages_node(cpu_to_node(cpu),
Vaibhav Nagarnaikd7ec4bf2011-06-07 17:01:42 -07001003 GFP_KERNEL | __GFP_NORETRY, 0);
Vaibhav Nagarnaik7ea59062011-05-03 17:56:42 -07001004 if (!page)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001005 goto free_pages;
Vaibhav Nagarnaik7ea59062011-05-03 17:56:42 -07001006 bpage->page = page_address(page);
Steven Rostedt044fa782008-12-02 23:50:03 -05001007 rb_init_page(bpage->page);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001008 }
1009
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08001010 return 0;
1011
1012free_pages:
1013 list_for_each_entry_safe(bpage, tmp, pages, list) {
1014 list_del_init(&bpage->list);
1015 free_buffer_page(bpage);
1016 }
1017
1018 return -ENOMEM;
1019}
1020
1021static int rb_allocate_pages(struct ring_buffer_per_cpu *cpu_buffer,
1022 unsigned nr_pages)
1023{
1024 LIST_HEAD(pages);
1025
1026 WARN_ON(!nr_pages);
1027
1028 if (__rb_allocate_pages(nr_pages, &pages, cpu_buffer->cpu))
1029 return -ENOMEM;
1030
Steven Rostedt3adc54f2009-03-30 15:32:01 -04001031 /*
1032 * The ring buffer page list is a circular list that does not
1033 * start and end with a list head. All page list items point to
1034 * other pages.
1035 */
1036 cpu_buffer->pages = pages.next;
1037 list_del(&pages);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001038
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08001039 cpu_buffer->nr_pages = nr_pages;
1040
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001041 rb_check_pages(cpu_buffer);
1042
1043 return 0;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001044}
1045
1046static struct ring_buffer_per_cpu *
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08001047rb_allocate_cpu_buffer(struct ring_buffer *buffer, int nr_pages, int cpu)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001048{
1049 struct ring_buffer_per_cpu *cpu_buffer;
Steven Rostedt044fa782008-12-02 23:50:03 -05001050 struct buffer_page *bpage;
Vaibhav Nagarnaik7ea59062011-05-03 17:56:42 -07001051 struct page *page;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001052 int ret;
1053
1054 cpu_buffer = kzalloc_node(ALIGN(sizeof(*cpu_buffer), cache_line_size()),
1055 GFP_KERNEL, cpu_to_node(cpu));
1056 if (!cpu_buffer)
1057 return NULL;
1058
1059 cpu_buffer->cpu = cpu;
1060 cpu_buffer->buffer = buffer;
Thomas Gleixner5389f6f2009-07-25 17:13:33 +02001061 raw_spin_lock_init(&cpu_buffer->reader_lock);
Peter Zijlstra1f8a6a12009-06-08 18:18:39 +02001062 lockdep_set_class(&cpu_buffer->reader_lock, buffer->reader_lock_key);
Thomas Gleixneredc35bd2009-12-03 12:38:57 +01001063 cpu_buffer->lock = (arch_spinlock_t)__ARCH_SPIN_LOCK_UNLOCKED;
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07001064 INIT_WORK(&cpu_buffer->update_pages_work, update_pages_handler);
Vaibhav Nagarnaik05fdd702012-05-18 13:29:51 -07001065 init_completion(&cpu_buffer->update_done);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001066
Steven Rostedt044fa782008-12-02 23:50:03 -05001067 bpage = kzalloc_node(ALIGN(sizeof(*bpage), cache_line_size()),
Steven Rostedte4c2ce82008-10-01 11:14:54 -04001068 GFP_KERNEL, cpu_to_node(cpu));
Steven Rostedt044fa782008-12-02 23:50:03 -05001069 if (!bpage)
Steven Rostedte4c2ce82008-10-01 11:14:54 -04001070 goto fail_free_buffer;
1071
Steven Rostedt77ae3652009-03-27 11:00:29 -04001072 rb_check_bpage(cpu_buffer, bpage);
1073
Steven Rostedt044fa782008-12-02 23:50:03 -05001074 cpu_buffer->reader_page = bpage;
Vaibhav Nagarnaik7ea59062011-05-03 17:56:42 -07001075 page = alloc_pages_node(cpu_to_node(cpu), GFP_KERNEL, 0);
1076 if (!page)
Steven Rostedte4c2ce82008-10-01 11:14:54 -04001077 goto fail_free_reader;
Vaibhav Nagarnaik7ea59062011-05-03 17:56:42 -07001078 bpage->page = page_address(page);
Steven Rostedt044fa782008-12-02 23:50:03 -05001079 rb_init_page(bpage->page);
Steven Rostedte4c2ce82008-10-01 11:14:54 -04001080
Steven Rostedtd7690412008-10-01 00:29:53 -04001081 INIT_LIST_HEAD(&cpu_buffer->reader_page->list);
Vaibhav Nagarnaik44b99462012-06-22 11:50:05 -07001082 INIT_LIST_HEAD(&cpu_buffer->new_pages);
Steven Rostedtd7690412008-10-01 00:29:53 -04001083
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08001084 ret = rb_allocate_pages(cpu_buffer, nr_pages);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001085 if (ret < 0)
Steven Rostedtd7690412008-10-01 00:29:53 -04001086 goto fail_free_reader;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001087
1088 cpu_buffer->head_page
Steven Rostedt3adc54f2009-03-30 15:32:01 -04001089 = list_entry(cpu_buffer->pages, struct buffer_page, list);
Steven Rostedtbf41a152008-10-04 02:00:59 -04001090 cpu_buffer->tail_page = cpu_buffer->commit_page = cpu_buffer->head_page;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001091
Steven Rostedt77ae3652009-03-27 11:00:29 -04001092 rb_head_page_activate(cpu_buffer);
1093
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001094 return cpu_buffer;
1095
Steven Rostedtd7690412008-10-01 00:29:53 -04001096 fail_free_reader:
1097 free_buffer_page(cpu_buffer->reader_page);
1098
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001099 fail_free_buffer:
1100 kfree(cpu_buffer);
1101 return NULL;
1102}
1103
1104static void rb_free_cpu_buffer(struct ring_buffer_per_cpu *cpu_buffer)
1105{
Steven Rostedt3adc54f2009-03-30 15:32:01 -04001106 struct list_head *head = cpu_buffer->pages;
Steven Rostedt044fa782008-12-02 23:50:03 -05001107 struct buffer_page *bpage, *tmp;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001108
Steven Rostedtd7690412008-10-01 00:29:53 -04001109 free_buffer_page(cpu_buffer->reader_page);
1110
Steven Rostedt77ae3652009-03-27 11:00:29 -04001111 rb_head_page_deactivate(cpu_buffer);
1112
Steven Rostedt3adc54f2009-03-30 15:32:01 -04001113 if (head) {
1114 list_for_each_entry_safe(bpage, tmp, head, list) {
1115 list_del_init(&bpage->list);
1116 free_buffer_page(bpage);
1117 }
1118 bpage = list_entry(head, struct buffer_page, list);
Steven Rostedt044fa782008-12-02 23:50:03 -05001119 free_buffer_page(bpage);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001120 }
Steven Rostedt3adc54f2009-03-30 15:32:01 -04001121
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001122 kfree(cpu_buffer);
1123}
1124
Steven Rostedt59222ef2009-03-12 11:46:03 -04001125#ifdef CONFIG_HOTPLUG_CPU
Frederic Weisbecker09c9e842009-03-21 04:33:36 +01001126static int rb_cpu_notify(struct notifier_block *self,
1127 unsigned long action, void *hcpu);
Steven Rostedt554f7862009-03-11 22:00:13 -04001128#endif
1129
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001130/**
1131 * ring_buffer_alloc - allocate a new ring_buffer
Robert Richter68814b52008-11-24 12:24:12 +01001132 * @size: the size in bytes per cpu that is needed.
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001133 * @flags: attributes to set for the ring buffer.
1134 *
1135 * Currently the only flag that is available is the RB_FL_OVERWRITE
1136 * flag. This flag means that the buffer will overwrite old data
1137 * when the buffer wraps. If this flag is not set, the buffer will
1138 * drop data when the tail hits the head.
1139 */
Peter Zijlstra1f8a6a12009-06-08 18:18:39 +02001140struct ring_buffer *__ring_buffer_alloc(unsigned long size, unsigned flags,
1141 struct lock_class_key *key)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001142{
1143 struct ring_buffer *buffer;
1144 int bsize;
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08001145 int cpu, nr_pages;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001146
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001147 /* keep it in its own cache line */
1148 buffer = kzalloc(ALIGN(sizeof(*buffer), cache_line_size()),
1149 GFP_KERNEL);
1150 if (!buffer)
1151 return NULL;
1152
Rusty Russell9e01c1b2009-01-01 10:12:22 +10301153 if (!alloc_cpumask_var(&buffer->cpumask, GFP_KERNEL))
1154 goto fail_free_buffer;
1155
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08001156 nr_pages = DIV_ROUND_UP(size, BUF_PAGE_SIZE);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001157 buffer->flags = flags;
Steven Rostedt37886f62009-03-17 17:22:06 -04001158 buffer->clock = trace_clock_local;
Peter Zijlstra1f8a6a12009-06-08 18:18:39 +02001159 buffer->reader_lock_key = key;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001160
1161 /* need at least two pages */
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08001162 if (nr_pages < 2)
1163 nr_pages = 2;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001164
Frederic Weisbecker3bf832c2009-03-19 14:47:33 +01001165 /*
1166 * In case of non-hotplug cpu, if the ring-buffer is allocated
1167 * in early initcall, it will not be notified of secondary cpus.
1168 * In that off case, we need to allocate for all possible cpus.
1169 */
1170#ifdef CONFIG_HOTPLUG_CPU
Steven Rostedt554f7862009-03-11 22:00:13 -04001171 get_online_cpus();
1172 cpumask_copy(buffer->cpumask, cpu_online_mask);
Frederic Weisbecker3bf832c2009-03-19 14:47:33 +01001173#else
1174 cpumask_copy(buffer->cpumask, cpu_possible_mask);
1175#endif
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001176 buffer->cpus = nr_cpu_ids;
1177
1178 bsize = sizeof(void *) * nr_cpu_ids;
1179 buffer->buffers = kzalloc(ALIGN(bsize, cache_line_size()),
1180 GFP_KERNEL);
1181 if (!buffer->buffers)
Rusty Russell9e01c1b2009-01-01 10:12:22 +10301182 goto fail_free_cpumask;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001183
1184 for_each_buffer_cpu(buffer, cpu) {
1185 buffer->buffers[cpu] =
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08001186 rb_allocate_cpu_buffer(buffer, nr_pages, cpu);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001187 if (!buffer->buffers[cpu])
1188 goto fail_free_buffers;
1189 }
1190
Steven Rostedt59222ef2009-03-12 11:46:03 -04001191#ifdef CONFIG_HOTPLUG_CPU
Steven Rostedt554f7862009-03-11 22:00:13 -04001192 buffer->cpu_notify.notifier_call = rb_cpu_notify;
1193 buffer->cpu_notify.priority = 0;
1194 register_cpu_notifier(&buffer->cpu_notify);
1195#endif
1196
1197 put_online_cpus();
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001198 mutex_init(&buffer->mutex);
1199
1200 return buffer;
1201
1202 fail_free_buffers:
1203 for_each_buffer_cpu(buffer, cpu) {
1204 if (buffer->buffers[cpu])
1205 rb_free_cpu_buffer(buffer->buffers[cpu]);
1206 }
1207 kfree(buffer->buffers);
1208
Rusty Russell9e01c1b2009-01-01 10:12:22 +10301209 fail_free_cpumask:
1210 free_cpumask_var(buffer->cpumask);
Steven Rostedt554f7862009-03-11 22:00:13 -04001211 put_online_cpus();
Rusty Russell9e01c1b2009-01-01 10:12:22 +10301212
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001213 fail_free_buffer:
1214 kfree(buffer);
1215 return NULL;
1216}
Peter Zijlstra1f8a6a12009-06-08 18:18:39 +02001217EXPORT_SYMBOL_GPL(__ring_buffer_alloc);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001218
1219/**
1220 * ring_buffer_free - free a ring buffer.
1221 * @buffer: the buffer to free.
1222 */
1223void
1224ring_buffer_free(struct ring_buffer *buffer)
1225{
1226 int cpu;
1227
Steven Rostedt554f7862009-03-11 22:00:13 -04001228 get_online_cpus();
1229
Steven Rostedt59222ef2009-03-12 11:46:03 -04001230#ifdef CONFIG_HOTPLUG_CPU
Steven Rostedt554f7862009-03-11 22:00:13 -04001231 unregister_cpu_notifier(&buffer->cpu_notify);
1232#endif
1233
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001234 for_each_buffer_cpu(buffer, cpu)
1235 rb_free_cpu_buffer(buffer->buffers[cpu]);
1236
Steven Rostedt554f7862009-03-11 22:00:13 -04001237 put_online_cpus();
1238
Eric Dumazetbd3f0222009-08-07 12:49:29 +02001239 kfree(buffer->buffers);
Rusty Russell9e01c1b2009-01-01 10:12:22 +10301240 free_cpumask_var(buffer->cpumask);
1241
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001242 kfree(buffer);
1243}
Robert Richterc4f50182008-12-11 16:49:22 +01001244EXPORT_SYMBOL_GPL(ring_buffer_free);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001245
Steven Rostedt37886f62009-03-17 17:22:06 -04001246void ring_buffer_set_clock(struct ring_buffer *buffer,
1247 u64 (*clock)(void))
1248{
1249 buffer->clock = clock;
1250}
1251
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001252static void rb_reset_cpu(struct ring_buffer_per_cpu *cpu_buffer);
1253
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07001254static inline unsigned long rb_page_entries(struct buffer_page *bpage)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001255{
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07001256 return local_read(&bpage->entries) & RB_WRITE_MASK;
1257}
1258
1259static inline unsigned long rb_page_write(struct buffer_page *bpage)
1260{
1261 return local_read(&bpage->write) & RB_WRITE_MASK;
1262}
1263
Vaibhav Nagarnaik5040b4b2012-05-03 18:59:51 -07001264static int
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07001265rb_remove_pages(struct ring_buffer_per_cpu *cpu_buffer, unsigned int nr_pages)
1266{
1267 struct list_head *tail_page, *to_remove, *next_page;
1268 struct buffer_page *to_remove_page, *tmp_iter_page;
1269 struct buffer_page *last_page, *first_page;
1270 unsigned int nr_removed;
1271 unsigned long head_bit;
1272 int page_entries;
1273
1274 head_bit = 0;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001275
Thomas Gleixner5389f6f2009-07-25 17:13:33 +02001276 raw_spin_lock_irq(&cpu_buffer->reader_lock);
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07001277 atomic_inc(&cpu_buffer->record_disabled);
1278 /*
1279 * We don't race with the readers since we have acquired the reader
1280 * lock. We also don't race with writers after disabling recording.
1281 * This makes it easy to figure out the first and the last page to be
1282 * removed from the list. We unlink all the pages in between including
1283 * the first and last pages. This is done in a busy loop so that we
1284 * lose the least number of traces.
1285 * The pages are freed after we restart recording and unlock readers.
1286 */
1287 tail_page = &cpu_buffer->tail_page->list;
Steven Rostedt77ae3652009-03-27 11:00:29 -04001288
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07001289 /*
1290 * tail page might be on reader page, we remove the next page
1291 * from the ring buffer
1292 */
1293 if (cpu_buffer->tail_page == cpu_buffer->reader_page)
1294 tail_page = rb_list_head(tail_page->next);
1295 to_remove = tail_page;
1296
1297 /* start of pages to remove */
1298 first_page = list_entry(rb_list_head(to_remove->next),
1299 struct buffer_page, list);
1300
1301 for (nr_removed = 0; nr_removed < nr_pages; nr_removed++) {
1302 to_remove = rb_list_head(to_remove)->next;
1303 head_bit |= (unsigned long)to_remove & RB_PAGE_HEAD;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001304 }
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001305
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07001306 next_page = rb_list_head(to_remove)->next;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001307
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07001308 /*
1309 * Now we remove all pages between tail_page and next_page.
1310 * Make sure that we have head_bit value preserved for the
1311 * next page
1312 */
1313 tail_page->next = (struct list_head *)((unsigned long)next_page |
1314 head_bit);
1315 next_page = rb_list_head(next_page);
1316 next_page->prev = tail_page;
1317
1318 /* make sure pages points to a valid page in the ring buffer */
1319 cpu_buffer->pages = next_page;
1320
1321 /* update head page */
1322 if (head_bit)
1323 cpu_buffer->head_page = list_entry(next_page,
1324 struct buffer_page, list);
1325
1326 /*
1327 * change read pointer to make sure any read iterators reset
1328 * themselves
1329 */
1330 cpu_buffer->read = 0;
1331
1332 /* pages are removed, resume tracing and then free the pages */
1333 atomic_dec(&cpu_buffer->record_disabled);
Thomas Gleixner5389f6f2009-07-25 17:13:33 +02001334 raw_spin_unlock_irq(&cpu_buffer->reader_lock);
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07001335
1336 RB_WARN_ON(cpu_buffer, list_empty(cpu_buffer->pages));
1337
1338 /* last buffer page to remove */
1339 last_page = list_entry(rb_list_head(to_remove), struct buffer_page,
1340 list);
1341 tmp_iter_page = first_page;
1342
1343 do {
1344 to_remove_page = tmp_iter_page;
1345 rb_inc_page(cpu_buffer, &tmp_iter_page);
1346
1347 /* update the counters */
1348 page_entries = rb_page_entries(to_remove_page);
1349 if (page_entries) {
1350 /*
1351 * If something was added to this page, it was full
1352 * since it is not the tail page. So we deduct the
1353 * bytes consumed in ring buffer from here.
Vaibhav Nagarnaik48fdc722012-06-29 12:31:41 -07001354 * Increment overrun to account for the lost events.
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07001355 */
Vaibhav Nagarnaik48fdc722012-06-29 12:31:41 -07001356 local_add(page_entries, &cpu_buffer->overrun);
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07001357 local_sub(BUF_PAGE_SIZE, &cpu_buffer->entries_bytes);
1358 }
1359
1360 /*
1361 * We have already removed references to this list item, just
1362 * free up the buffer_page and its page
1363 */
1364 free_buffer_page(to_remove_page);
1365 nr_removed--;
1366
1367 } while (to_remove_page != last_page);
1368
1369 RB_WARN_ON(cpu_buffer, nr_removed);
Vaibhav Nagarnaik5040b4b2012-05-03 18:59:51 -07001370
1371 return nr_removed == 0;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001372}
1373
Vaibhav Nagarnaik5040b4b2012-05-03 18:59:51 -07001374static int
1375rb_insert_pages(struct ring_buffer_per_cpu *cpu_buffer)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001376{
Vaibhav Nagarnaik5040b4b2012-05-03 18:59:51 -07001377 struct list_head *pages = &cpu_buffer->new_pages;
1378 int retries, success;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001379
Thomas Gleixner5389f6f2009-07-25 17:13:33 +02001380 raw_spin_lock_irq(&cpu_buffer->reader_lock);
Vaibhav Nagarnaik5040b4b2012-05-03 18:59:51 -07001381 /*
1382 * We are holding the reader lock, so the reader page won't be swapped
1383 * in the ring buffer. Now we are racing with the writer trying to
1384 * move head page and the tail page.
1385 * We are going to adapt the reader page update process where:
1386 * 1. We first splice the start and end of list of new pages between
1387 * the head page and its previous page.
1388 * 2. We cmpxchg the prev_page->next to point from head page to the
1389 * start of new pages list.
1390 * 3. Finally, we update the head->prev to the end of new list.
1391 *
1392 * We will try this process 10 times, to make sure that we don't keep
1393 * spinning.
1394 */
1395 retries = 10;
1396 success = 0;
1397 while (retries--) {
1398 struct list_head *head_page, *prev_page, *r;
1399 struct list_head *last_page, *first_page;
1400 struct list_head *head_page_with_bit;
Steven Rostedt77ae3652009-03-27 11:00:29 -04001401
Vaibhav Nagarnaik5040b4b2012-05-03 18:59:51 -07001402 head_page = &rb_set_head_page(cpu_buffer)->list;
Steven Rostedt54f7be52012-11-29 22:27:22 -05001403 if (!head_page)
1404 break;
Vaibhav Nagarnaik5040b4b2012-05-03 18:59:51 -07001405 prev_page = head_page->prev;
1406
1407 first_page = pages->next;
1408 last_page = pages->prev;
1409
1410 head_page_with_bit = (struct list_head *)
1411 ((unsigned long)head_page | RB_PAGE_HEAD);
1412
1413 last_page->next = head_page_with_bit;
1414 first_page->prev = prev_page;
1415
1416 r = cmpxchg(&prev_page->next, head_page_with_bit, first_page);
1417
1418 if (r == head_page_with_bit) {
1419 /*
1420 * yay, we replaced the page pointer to our new list,
1421 * now, we just have to update to head page's prev
1422 * pointer to point to end of list
1423 */
1424 head_page->prev = last_page;
1425 success = 1;
1426 break;
1427 }
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001428 }
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001429
Vaibhav Nagarnaik5040b4b2012-05-03 18:59:51 -07001430 if (success)
1431 INIT_LIST_HEAD(pages);
1432 /*
1433 * If we weren't successful in adding in new pages, warn and stop
1434 * tracing
1435 */
1436 RB_WARN_ON(cpu_buffer, !success);
Thomas Gleixner5389f6f2009-07-25 17:13:33 +02001437 raw_spin_unlock_irq(&cpu_buffer->reader_lock);
Vaibhav Nagarnaik5040b4b2012-05-03 18:59:51 -07001438
1439 /* free pages if they weren't inserted */
1440 if (!success) {
1441 struct buffer_page *bpage, *tmp;
1442 list_for_each_entry_safe(bpage, tmp, &cpu_buffer->new_pages,
1443 list) {
1444 list_del_init(&bpage->list);
1445 free_buffer_page(bpage);
1446 }
1447 }
1448 return success;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001449}
1450
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07001451static void rb_update_pages(struct ring_buffer_per_cpu *cpu_buffer)
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08001452{
Vaibhav Nagarnaik5040b4b2012-05-03 18:59:51 -07001453 int success;
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07001454
Vaibhav Nagarnaik5040b4b2012-05-03 18:59:51 -07001455 if (cpu_buffer->nr_pages_to_update > 0)
1456 success = rb_insert_pages(cpu_buffer);
1457 else
1458 success = rb_remove_pages(cpu_buffer,
1459 -cpu_buffer->nr_pages_to_update);
1460
1461 if (success)
1462 cpu_buffer->nr_pages += cpu_buffer->nr_pages_to_update;
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07001463}
1464
1465static void update_pages_handler(struct work_struct *work)
1466{
1467 struct ring_buffer_per_cpu *cpu_buffer = container_of(work,
1468 struct ring_buffer_per_cpu, update_pages_work);
1469 rb_update_pages(cpu_buffer);
Vaibhav Nagarnaik05fdd702012-05-18 13:29:51 -07001470 complete(&cpu_buffer->update_done);
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08001471}
1472
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001473/**
1474 * ring_buffer_resize - resize the ring buffer
1475 * @buffer: the buffer to resize.
1476 * @size: the new size.
1477 *
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001478 * Minimum size is 2 * BUF_PAGE_SIZE.
1479 *
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07001480 * Returns 0 on success and < 0 on failure.
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001481 */
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08001482int ring_buffer_resize(struct ring_buffer *buffer, unsigned long size,
1483 int cpu_id)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001484{
1485 struct ring_buffer_per_cpu *cpu_buffer;
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08001486 unsigned nr_pages;
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07001487 int cpu, err = 0;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001488
Ingo Molnaree51a1d2008-11-13 14:58:31 +01001489 /*
1490 * Always succeed at resizing a non-existent buffer:
1491 */
1492 if (!buffer)
1493 return size;
1494
Steven Rostedt6a31e1f2012-05-23 15:35:17 -04001495 /* Make sure the requested buffer exists */
1496 if (cpu_id != RING_BUFFER_ALL_CPUS &&
1497 !cpumask_test_cpu(cpu_id, buffer->cpumask))
1498 return size;
1499
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001500 size = DIV_ROUND_UP(size, BUF_PAGE_SIZE);
1501 size *= BUF_PAGE_SIZE;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001502
1503 /* we need a minimum of two pages */
1504 if (size < BUF_PAGE_SIZE * 2)
1505 size = BUF_PAGE_SIZE * 2;
1506
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001507 nr_pages = DIV_ROUND_UP(size, BUF_PAGE_SIZE);
1508
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07001509 /*
1510 * Don't succeed if resizing is disabled, as a reader might be
1511 * manipulating the ring buffer and is expecting a sane state while
1512 * this is true.
1513 */
1514 if (atomic_read(&buffer->resize_disabled))
1515 return -EBUSY;
1516
1517 /* prevent another thread from changing buffer sizes */
1518 mutex_lock(&buffer->mutex);
1519
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08001520 if (cpu_id == RING_BUFFER_ALL_CPUS) {
1521 /* calculate the pages to update */
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001522 for_each_buffer_cpu(buffer, cpu) {
1523 cpu_buffer = buffer->buffers[cpu];
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001524
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08001525 cpu_buffer->nr_pages_to_update = nr_pages -
1526 cpu_buffer->nr_pages;
Vaibhav Nagarnaikd7ec4bf2011-06-07 17:01:42 -07001527 /*
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08001528 * nothing more to do for removing pages or no update
Vaibhav Nagarnaikd7ec4bf2011-06-07 17:01:42 -07001529 */
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08001530 if (cpu_buffer->nr_pages_to_update <= 0)
1531 continue;
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08001532 /*
1533 * to add pages, make sure all new pages can be
1534 * allocated without receiving ENOMEM
1535 */
1536 INIT_LIST_HEAD(&cpu_buffer->new_pages);
1537 if (__rb_allocate_pages(cpu_buffer->nr_pages_to_update,
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07001538 &cpu_buffer->new_pages, cpu)) {
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08001539 /* not enough memory for new pages */
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07001540 err = -ENOMEM;
1541 goto out_err;
1542 }
1543 }
1544
1545 get_online_cpus();
1546 /*
1547 * Fire off all the required work handlers
Vaibhav Nagarnaik05fdd702012-05-18 13:29:51 -07001548 * We can't schedule on offline CPUs, but it's not necessary
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07001549 * since we can change their buffer sizes without any race.
1550 */
1551 for_each_buffer_cpu(buffer, cpu) {
1552 cpu_buffer = buffer->buffers[cpu];
Vaibhav Nagarnaik05fdd702012-05-18 13:29:51 -07001553 if (!cpu_buffer->nr_pages_to_update)
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07001554 continue;
1555
Vaibhav Nagarnaik05fdd702012-05-18 13:29:51 -07001556 if (cpu_online(cpu))
1557 schedule_work_on(cpu,
1558 &cpu_buffer->update_pages_work);
1559 else
1560 rb_update_pages(cpu_buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001561 }
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001562
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08001563 /* wait for all the updates to complete */
1564 for_each_buffer_cpu(buffer, cpu) {
1565 cpu_buffer = buffer->buffers[cpu];
Vaibhav Nagarnaik05fdd702012-05-18 13:29:51 -07001566 if (!cpu_buffer->nr_pages_to_update)
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07001567 continue;
1568
Vaibhav Nagarnaik05fdd702012-05-18 13:29:51 -07001569 if (cpu_online(cpu))
1570 wait_for_completion(&cpu_buffer->update_done);
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07001571 cpu_buffer->nr_pages_to_update = 0;
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08001572 }
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07001573
1574 put_online_cpus();
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08001575 } else {
Vaibhav Nagarnaik8e49f412012-10-10 16:40:27 -07001576 /* Make sure this CPU has been intitialized */
1577 if (!cpumask_test_cpu(cpu_id, buffer->cpumask))
1578 goto out;
1579
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08001580 cpu_buffer = buffer->buffers[cpu_id];
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07001581
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08001582 if (nr_pages == cpu_buffer->nr_pages)
1583 goto out;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001584
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08001585 cpu_buffer->nr_pages_to_update = nr_pages -
1586 cpu_buffer->nr_pages;
1587
1588 INIT_LIST_HEAD(&cpu_buffer->new_pages);
1589 if (cpu_buffer->nr_pages_to_update > 0 &&
1590 __rb_allocate_pages(cpu_buffer->nr_pages_to_update,
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07001591 &cpu_buffer->new_pages, cpu_id)) {
1592 err = -ENOMEM;
1593 goto out_err;
1594 }
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08001595
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07001596 get_online_cpus();
1597
1598 if (cpu_online(cpu_id)) {
1599 schedule_work_on(cpu_id,
1600 &cpu_buffer->update_pages_work);
Vaibhav Nagarnaik05fdd702012-05-18 13:29:51 -07001601 wait_for_completion(&cpu_buffer->update_done);
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07001602 } else
1603 rb_update_pages(cpu_buffer);
1604
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07001605 cpu_buffer->nr_pages_to_update = 0;
Vaibhav Nagarnaik05fdd702012-05-18 13:29:51 -07001606 put_online_cpus();
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08001607 }
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001608
1609 out:
Steven Rostedt659f4512012-05-14 17:02:33 -04001610 /*
1611 * The ring buffer resize can happen with the ring buffer
1612 * enabled, so that the update disturbs the tracing as little
1613 * as possible. But if the buffer is disabled, we do not need
1614 * to worry about that, and we can take the time to verify
1615 * that the buffer is not corrupt.
1616 */
1617 if (atomic_read(&buffer->record_disabled)) {
1618 atomic_inc(&buffer->record_disabled);
1619 /*
1620 * Even though the buffer was disabled, we must make sure
1621 * that it is truly disabled before calling rb_check_pages.
1622 * There could have been a race between checking
1623 * record_disable and incrementing it.
1624 */
1625 synchronize_sched();
1626 for_each_buffer_cpu(buffer, cpu) {
1627 cpu_buffer = buffer->buffers[cpu];
1628 rb_check_pages(cpu_buffer);
1629 }
1630 atomic_dec(&buffer->record_disabled);
1631 }
1632
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001633 mutex_unlock(&buffer->mutex);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001634 return size;
1635
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07001636 out_err:
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08001637 for_each_buffer_cpu(buffer, cpu) {
1638 struct buffer_page *bpage, *tmp;
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07001639
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08001640 cpu_buffer = buffer->buffers[cpu];
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08001641 cpu_buffer->nr_pages_to_update = 0;
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07001642
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08001643 if (list_empty(&cpu_buffer->new_pages))
1644 continue;
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07001645
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08001646 list_for_each_entry_safe(bpage, tmp, &cpu_buffer->new_pages,
1647 list) {
1648 list_del_init(&bpage->list);
1649 free_buffer_page(bpage);
1650 }
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001651 }
Vegard Nossum641d2f62008-11-18 19:22:13 +01001652 mutex_unlock(&buffer->mutex);
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07001653 return err;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001654}
Robert Richterc4f50182008-12-11 16:49:22 +01001655EXPORT_SYMBOL_GPL(ring_buffer_resize);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001656
David Sharp750912f2010-12-08 13:46:47 -08001657void ring_buffer_change_overwrite(struct ring_buffer *buffer, int val)
1658{
1659 mutex_lock(&buffer->mutex);
1660 if (val)
1661 buffer->flags |= RB_FL_OVERWRITE;
1662 else
1663 buffer->flags &= ~RB_FL_OVERWRITE;
1664 mutex_unlock(&buffer->mutex);
1665}
1666EXPORT_SYMBOL_GPL(ring_buffer_change_overwrite);
1667
Steven Rostedt8789a9e2008-12-02 15:34:07 -05001668static inline void *
Steven Rostedt044fa782008-12-02 23:50:03 -05001669__rb_data_page_index(struct buffer_data_page *bpage, unsigned index)
Steven Rostedt8789a9e2008-12-02 15:34:07 -05001670{
Steven Rostedt044fa782008-12-02 23:50:03 -05001671 return bpage->data + index;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05001672}
1673
Steven Rostedt044fa782008-12-02 23:50:03 -05001674static inline void *__rb_page_index(struct buffer_page *bpage, unsigned index)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001675{
Steven Rostedt044fa782008-12-02 23:50:03 -05001676 return bpage->page->data + index;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001677}
1678
1679static inline struct ring_buffer_event *
Steven Rostedtd7690412008-10-01 00:29:53 -04001680rb_reader_event(struct ring_buffer_per_cpu *cpu_buffer)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001681{
Steven Rostedt6f807ac2008-10-04 02:00:58 -04001682 return __rb_page_index(cpu_buffer->reader_page,
1683 cpu_buffer->reader_page->read);
1684}
1685
1686static inline struct ring_buffer_event *
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001687rb_iter_head_event(struct ring_buffer_iter *iter)
1688{
Steven Rostedt6f807ac2008-10-04 02:00:58 -04001689 return __rb_page_index(iter->head_page, iter->head);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001690}
1691
Steven Rostedtbf41a152008-10-04 02:00:59 -04001692static inline unsigned rb_page_commit(struct buffer_page *bpage)
1693{
Steven Rostedtabc9b562008-12-02 15:34:06 -05001694 return local_read(&bpage->page->commit);
Steven Rostedtbf41a152008-10-04 02:00:59 -04001695}
1696
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001697/* Size is determined by what has been committed */
Steven Rostedtbf41a152008-10-04 02:00:59 -04001698static inline unsigned rb_page_size(struct buffer_page *bpage)
1699{
1700 return rb_page_commit(bpage);
1701}
1702
1703static inline unsigned
1704rb_commit_index(struct ring_buffer_per_cpu *cpu_buffer)
1705{
1706 return rb_page_commit(cpu_buffer->commit_page);
1707}
1708
Steven Rostedtbf41a152008-10-04 02:00:59 -04001709static inline unsigned
1710rb_event_index(struct ring_buffer_event *event)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001711{
Steven Rostedtbf41a152008-10-04 02:00:59 -04001712 unsigned long addr = (unsigned long)event;
1713
Steven Rostedt22f470f2009-06-11 09:29:58 -04001714 return (addr & ~PAGE_MASK) - BUF_PAGE_HDR_SIZE;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001715}
1716
Steven Rostedt0f0c85f2009-05-11 16:08:00 -04001717static inline int
Steven Rostedtfa743952009-06-16 12:37:57 -04001718rb_event_is_commit(struct ring_buffer_per_cpu *cpu_buffer,
1719 struct ring_buffer_event *event)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001720{
Steven Rostedtbf41a152008-10-04 02:00:59 -04001721 unsigned long addr = (unsigned long)event;
1722 unsigned long index;
1723
1724 index = rb_event_index(event);
1725 addr &= PAGE_MASK;
1726
1727 return cpu_buffer->commit_page->page == (void *)addr &&
1728 rb_commit_index(cpu_buffer) == index;
1729}
1730
Andrew Morton34a148b2009-01-09 12:27:09 -08001731static void
Steven Rostedtbf41a152008-10-04 02:00:59 -04001732rb_set_commit_to_write(struct ring_buffer_per_cpu *cpu_buffer)
1733{
Steven Rostedt77ae3652009-03-27 11:00:29 -04001734 unsigned long max_count;
1735
Steven Rostedtbf41a152008-10-04 02:00:59 -04001736 /*
1737 * We only race with interrupts and NMIs on this CPU.
1738 * If we own the commit event, then we can commit
1739 * all others that interrupted us, since the interruptions
1740 * are in stack format (they finish before they come
1741 * back to us). This allows us to do a simple loop to
1742 * assign the commit to the tail.
1743 */
Steven Rostedta8ccf1d2008-12-23 11:32:24 -05001744 again:
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08001745 max_count = cpu_buffer->nr_pages * 100;
Steven Rostedt77ae3652009-03-27 11:00:29 -04001746
Steven Rostedtbf41a152008-10-04 02:00:59 -04001747 while (cpu_buffer->commit_page != cpu_buffer->tail_page) {
Steven Rostedt77ae3652009-03-27 11:00:29 -04001748 if (RB_WARN_ON(cpu_buffer, !(--max_count)))
1749 return;
1750 if (RB_WARN_ON(cpu_buffer,
1751 rb_is_reader_page(cpu_buffer->tail_page)))
1752 return;
1753 local_set(&cpu_buffer->commit_page->page->commit,
1754 rb_page_write(cpu_buffer->commit_page));
Steven Rostedtbf41a152008-10-04 02:00:59 -04001755 rb_inc_page(cpu_buffer, &cpu_buffer->commit_page);
Steven Rostedtabc9b562008-12-02 15:34:06 -05001756 cpu_buffer->write_stamp =
1757 cpu_buffer->commit_page->page->time_stamp;
Steven Rostedtbf41a152008-10-04 02:00:59 -04001758 /* add barrier to keep gcc from optimizing too much */
1759 barrier();
1760 }
1761 while (rb_commit_index(cpu_buffer) !=
1762 rb_page_write(cpu_buffer->commit_page)) {
Steven Rostedt77ae3652009-03-27 11:00:29 -04001763
1764 local_set(&cpu_buffer->commit_page->page->commit,
1765 rb_page_write(cpu_buffer->commit_page));
1766 RB_WARN_ON(cpu_buffer,
1767 local_read(&cpu_buffer->commit_page->page->commit) &
1768 ~RB_WRITE_MASK);
Steven Rostedtbf41a152008-10-04 02:00:59 -04001769 barrier();
1770 }
Steven Rostedta8ccf1d2008-12-23 11:32:24 -05001771
1772 /* again, keep gcc from optimizing */
1773 barrier();
1774
1775 /*
1776 * If an interrupt came in just after the first while loop
1777 * and pushed the tail page forward, we will be left with
1778 * a dangling commit that will never go forward.
1779 */
1780 if (unlikely(cpu_buffer->commit_page != cpu_buffer->tail_page))
1781 goto again;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001782}
1783
Steven Rostedtd7690412008-10-01 00:29:53 -04001784static void rb_reset_reader_page(struct ring_buffer_per_cpu *cpu_buffer)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001785{
Steven Rostedtabc9b562008-12-02 15:34:06 -05001786 cpu_buffer->read_stamp = cpu_buffer->reader_page->page->time_stamp;
Steven Rostedt6f807ac2008-10-04 02:00:58 -04001787 cpu_buffer->reader_page->read = 0;
Steven Rostedtd7690412008-10-01 00:29:53 -04001788}
1789
Andrew Morton34a148b2009-01-09 12:27:09 -08001790static void rb_inc_iter(struct ring_buffer_iter *iter)
Steven Rostedtd7690412008-10-01 00:29:53 -04001791{
1792 struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
1793
1794 /*
1795 * The iterator could be on the reader page (it starts there).
1796 * But the head could have moved, since the reader was
1797 * found. Check for this case and assign the iterator
1798 * to the head page instead of next.
1799 */
1800 if (iter->head_page == cpu_buffer->reader_page)
Steven Rostedt77ae3652009-03-27 11:00:29 -04001801 iter->head_page = rb_set_head_page(cpu_buffer);
Steven Rostedtd7690412008-10-01 00:29:53 -04001802 else
1803 rb_inc_page(cpu_buffer, &iter->head_page);
1804
Steven Rostedtabc9b562008-12-02 15:34:06 -05001805 iter->read_stamp = iter->head_page->page->time_stamp;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001806 iter->head = 0;
1807}
1808
Steven Rostedt69d1b832010-10-07 18:18:05 -04001809/* Slow path, do not inline */
1810static noinline struct ring_buffer_event *
1811rb_add_time_stamp(struct ring_buffer_event *event, u64 delta)
1812{
1813 event->type_len = RINGBUF_TYPE_TIME_EXTEND;
1814
1815 /* Not the first event on the page? */
1816 if (rb_event_index(event)) {
1817 event->time_delta = delta & TS_MASK;
1818 event->array[0] = delta >> TS_SHIFT;
1819 } else {
1820 /* nope, just zero it */
1821 event->time_delta = 0;
1822 event->array[0] = 0;
1823 }
1824
1825 return skip_time_extend(event);
1826}
1827
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001828/**
David Sharp01e3e712012-06-07 16:46:24 -07001829 * rb_update_event - update event type and data
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001830 * @event: the even to update
1831 * @type: the type of event
1832 * @length: the size of the event field in the ring buffer
1833 *
1834 * Update the type and data fields of the event. The length
1835 * is the actual size that is written to the ring buffer,
1836 * and with this, we can determine what to place into the
1837 * data field.
1838 */
Andrew Morton34a148b2009-01-09 12:27:09 -08001839static void
Steven Rostedt69d1b832010-10-07 18:18:05 -04001840rb_update_event(struct ring_buffer_per_cpu *cpu_buffer,
1841 struct ring_buffer_event *event, unsigned length,
1842 int add_timestamp, u64 delta)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001843{
Steven Rostedt69d1b832010-10-07 18:18:05 -04001844 /* Only a commit updates the timestamp */
1845 if (unlikely(!rb_event_is_commit(cpu_buffer, event)))
1846 delta = 0;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001847
Steven Rostedt69d1b832010-10-07 18:18:05 -04001848 /*
1849 * If we need to add a timestamp, then we
1850 * add it to the start of the resevered space.
1851 */
1852 if (unlikely(add_timestamp)) {
1853 event = rb_add_time_stamp(event, delta);
1854 length -= RB_LEN_TIME_EXTEND;
1855 delta = 0;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001856 }
Steven Rostedt69d1b832010-10-07 18:18:05 -04001857
1858 event->time_delta = delta;
1859 length -= RB_EVNT_HDR_SIZE;
1860 if (length > RB_MAX_SMALL_DATA || RB_FORCE_8BYTE_ALIGNMENT) {
1861 event->type_len = 0;
1862 event->array[0] = length;
1863 } else
1864 event->type_len = DIV_ROUND_UP(length, RB_ALIGNMENT);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001865}
1866
Steven Rostedt77ae3652009-03-27 11:00:29 -04001867/*
1868 * rb_handle_head_page - writer hit the head page
1869 *
1870 * Returns: +1 to retry page
1871 * 0 to continue
1872 * -1 on error
1873 */
1874static int
1875rb_handle_head_page(struct ring_buffer_per_cpu *cpu_buffer,
1876 struct buffer_page *tail_page,
1877 struct buffer_page *next_page)
1878{
1879 struct buffer_page *new_head;
1880 int entries;
1881 int type;
1882 int ret;
1883
1884 entries = rb_page_entries(next_page);
1885
1886 /*
1887 * The hard part is here. We need to move the head
1888 * forward, and protect against both readers on
1889 * other CPUs and writers coming in via interrupts.
1890 */
1891 type = rb_head_page_set_update(cpu_buffer, next_page, tail_page,
1892 RB_PAGE_HEAD);
1893
1894 /*
1895 * type can be one of four:
1896 * NORMAL - an interrupt already moved it for us
1897 * HEAD - we are the first to get here.
1898 * UPDATE - we are the interrupt interrupting
1899 * a current move.
1900 * MOVED - a reader on another CPU moved the next
1901 * pointer to its reader page. Give up
1902 * and try again.
1903 */
1904
1905 switch (type) {
1906 case RB_PAGE_HEAD:
1907 /*
1908 * We changed the head to UPDATE, thus
1909 * it is our responsibility to update
1910 * the counters.
1911 */
1912 local_add(entries, &cpu_buffer->overrun);
Vaibhav Nagarnaikc64e1482011-08-16 14:46:16 -07001913 local_sub(BUF_PAGE_SIZE, &cpu_buffer->entries_bytes);
Steven Rostedt77ae3652009-03-27 11:00:29 -04001914
1915 /*
1916 * The entries will be zeroed out when we move the
1917 * tail page.
1918 */
1919
1920 /* still more to do */
1921 break;
1922
1923 case RB_PAGE_UPDATE:
1924 /*
1925 * This is an interrupt that interrupt the
1926 * previous update. Still more to do.
1927 */
1928 break;
1929 case RB_PAGE_NORMAL:
1930 /*
1931 * An interrupt came in before the update
1932 * and processed this for us.
1933 * Nothing left to do.
1934 */
1935 return 1;
1936 case RB_PAGE_MOVED:
1937 /*
1938 * The reader is on another CPU and just did
1939 * a swap with our next_page.
1940 * Try again.
1941 */
1942 return 1;
1943 default:
1944 RB_WARN_ON(cpu_buffer, 1); /* WTF??? */
1945 return -1;
1946 }
1947
1948 /*
1949 * Now that we are here, the old head pointer is
1950 * set to UPDATE. This will keep the reader from
1951 * swapping the head page with the reader page.
1952 * The reader (on another CPU) will spin till
1953 * we are finished.
1954 *
1955 * We just need to protect against interrupts
1956 * doing the job. We will set the next pointer
1957 * to HEAD. After that, we set the old pointer
1958 * to NORMAL, but only if it was HEAD before.
1959 * otherwise we are an interrupt, and only
1960 * want the outer most commit to reset it.
1961 */
1962 new_head = next_page;
1963 rb_inc_page(cpu_buffer, &new_head);
1964
1965 ret = rb_head_page_set_head(cpu_buffer, new_head, next_page,
1966 RB_PAGE_NORMAL);
1967
1968 /*
1969 * Valid returns are:
1970 * HEAD - an interrupt came in and already set it.
1971 * NORMAL - One of two things:
1972 * 1) We really set it.
1973 * 2) A bunch of interrupts came in and moved
1974 * the page forward again.
1975 */
1976 switch (ret) {
1977 case RB_PAGE_HEAD:
1978 case RB_PAGE_NORMAL:
1979 /* OK */
1980 break;
1981 default:
1982 RB_WARN_ON(cpu_buffer, 1);
1983 return -1;
1984 }
1985
1986 /*
1987 * It is possible that an interrupt came in,
1988 * set the head up, then more interrupts came in
1989 * and moved it again. When we get back here,
1990 * the page would have been set to NORMAL but we
1991 * just set it back to HEAD.
1992 *
1993 * How do you detect this? Well, if that happened
1994 * the tail page would have moved.
1995 */
1996 if (ret == RB_PAGE_NORMAL) {
1997 /*
1998 * If the tail had moved passed next, then we need
1999 * to reset the pointer.
2000 */
2001 if (cpu_buffer->tail_page != tail_page &&
2002 cpu_buffer->tail_page != next_page)
2003 rb_head_page_set_normal(cpu_buffer, new_head,
2004 next_page,
2005 RB_PAGE_HEAD);
2006 }
2007
2008 /*
2009 * If this was the outer most commit (the one that
2010 * changed the original pointer from HEAD to UPDATE),
2011 * then it is up to us to reset it to NORMAL.
2012 */
2013 if (type == RB_PAGE_HEAD) {
2014 ret = rb_head_page_set_normal(cpu_buffer, next_page,
2015 tail_page,
2016 RB_PAGE_UPDATE);
2017 if (RB_WARN_ON(cpu_buffer,
2018 ret != RB_PAGE_UPDATE))
2019 return -1;
2020 }
2021
2022 return 0;
2023}
2024
Andrew Morton34a148b2009-01-09 12:27:09 -08002025static unsigned rb_calculate_event_length(unsigned length)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002026{
2027 struct ring_buffer_event event; /* Used only for sizeof array */
2028
2029 /* zero length can cause confusions */
2030 if (!length)
2031 length = 1;
2032
Steven Rostedt22710482010-03-18 17:54:19 -04002033 if (length > RB_MAX_SMALL_DATA || RB_FORCE_8BYTE_ALIGNMENT)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002034 length += sizeof(event.array[0]);
2035
2036 length += RB_EVNT_HDR_SIZE;
Steven Rostedt22710482010-03-18 17:54:19 -04002037 length = ALIGN(length, RB_ARCH_ALIGNMENT);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002038
2039 return length;
2040}
2041
Steven Rostedtc7b09302009-06-11 11:12:00 -04002042static inline void
2043rb_reset_tail(struct ring_buffer_per_cpu *cpu_buffer,
2044 struct buffer_page *tail_page,
2045 unsigned long tail, unsigned long length)
2046{
2047 struct ring_buffer_event *event;
2048
2049 /*
2050 * Only the event that crossed the page boundary
2051 * must fill the old tail_page with padding.
2052 */
2053 if (tail >= BUF_PAGE_SIZE) {
Steven Rostedtb3230c82010-05-21 11:55:21 -04002054 /*
2055 * If the page was filled, then we still need
2056 * to update the real_end. Reset it to zero
2057 * and the reader will ignore it.
2058 */
2059 if (tail == BUF_PAGE_SIZE)
2060 tail_page->real_end = 0;
2061
Steven Rostedtc7b09302009-06-11 11:12:00 -04002062 local_sub(length, &tail_page->write);
2063 return;
2064 }
2065
2066 event = __rb_page_index(tail_page, tail);
Linus Torvaldsb0b70652009-06-20 10:56:46 -07002067 kmemcheck_annotate_bitfield(event, bitfield);
Steven Rostedtc7b09302009-06-11 11:12:00 -04002068
Vaibhav Nagarnaikc64e1482011-08-16 14:46:16 -07002069 /* account for padding bytes */
2070 local_add(BUF_PAGE_SIZE - tail, &cpu_buffer->entries_bytes);
2071
Steven Rostedtc7b09302009-06-11 11:12:00 -04002072 /*
Steven Rostedtff0ff842010-03-31 22:11:42 -04002073 * Save the original length to the meta data.
2074 * This will be used by the reader to add lost event
2075 * counter.
2076 */
2077 tail_page->real_end = tail;
2078
2079 /*
Steven Rostedtc7b09302009-06-11 11:12:00 -04002080 * If this event is bigger than the minimum size, then
2081 * we need to be careful that we don't subtract the
2082 * write counter enough to allow another writer to slip
2083 * in on this page.
2084 * We put in a discarded commit instead, to make sure
2085 * that this space is not used again.
2086 *
2087 * If we are less than the minimum size, we don't need to
2088 * worry about it.
2089 */
2090 if (tail > (BUF_PAGE_SIZE - RB_EVNT_MIN_SIZE)) {
2091 /* No room for any events */
2092
2093 /* Mark the rest of the page with padding */
2094 rb_event_set_padding(event);
2095
2096 /* Set the write back to the previous setting */
2097 local_sub(length, &tail_page->write);
2098 return;
2099 }
2100
2101 /* Put in a discarded event */
2102 event->array[0] = (BUF_PAGE_SIZE - tail) - RB_EVNT_HDR_SIZE;
2103 event->type_len = RINGBUF_TYPE_PADDING;
2104 /* time delta must be non zero */
2105 event->time_delta = 1;
Steven Rostedtc7b09302009-06-11 11:12:00 -04002106
2107 /* Set write to end of buffer */
2108 length = (tail + length) - BUF_PAGE_SIZE;
2109 local_sub(length, &tail_page->write);
2110}
Steven Rostedt6634ff22009-05-06 15:30:07 -04002111
Steven Rostedt747e94a2010-10-08 13:51:48 -04002112/*
2113 * This is the slow path, force gcc not to inline it.
2114 */
2115static noinline struct ring_buffer_event *
Steven Rostedt6634ff22009-05-06 15:30:07 -04002116rb_move_tail(struct ring_buffer_per_cpu *cpu_buffer,
2117 unsigned long length, unsigned long tail,
Steven Rostedte8bc43e2010-10-20 10:58:02 -04002118 struct buffer_page *tail_page, u64 ts)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002119{
Steven Rostedt5a50e332009-11-17 08:43:01 -05002120 struct buffer_page *commit_page = cpu_buffer->commit_page;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002121 struct ring_buffer *buffer = cpu_buffer->buffer;
Steven Rostedt77ae3652009-03-27 11:00:29 -04002122 struct buffer_page *next_page;
2123 int ret;
Steven Rostedtaa20ae82009-05-05 21:16:11 -04002124
2125 next_page = tail_page;
2126
Steven Rostedtaa20ae82009-05-05 21:16:11 -04002127 rb_inc_page(cpu_buffer, &next_page);
2128
Steven Rostedtaa20ae82009-05-05 21:16:11 -04002129 /*
2130 * If for some reason, we had an interrupt storm that made
2131 * it all the way around the buffer, bail, and warn
2132 * about it.
2133 */
2134 if (unlikely(next_page == commit_page)) {
Steven Rostedt77ae3652009-03-27 11:00:29 -04002135 local_inc(&cpu_buffer->commit_overrun);
Steven Rostedtaa20ae82009-05-05 21:16:11 -04002136 goto out_reset;
2137 }
2138
Steven Rostedt77ae3652009-03-27 11:00:29 -04002139 /*
2140 * This is where the fun begins!
2141 *
2142 * We are fighting against races between a reader that
2143 * could be on another CPU trying to swap its reader
2144 * page with the buffer head.
2145 *
2146 * We are also fighting against interrupts coming in and
2147 * moving the head or tail on us as well.
2148 *
2149 * If the next page is the head page then we have filled
2150 * the buffer, unless the commit page is still on the
2151 * reader page.
2152 */
2153 if (rb_is_head_page(cpu_buffer, next_page, &tail_page->list)) {
Steven Rostedtaa20ae82009-05-05 21:16:11 -04002154
Steven Rostedt77ae3652009-03-27 11:00:29 -04002155 /*
2156 * If the commit is not on the reader page, then
2157 * move the header page.
2158 */
2159 if (!rb_is_reader_page(cpu_buffer->commit_page)) {
2160 /*
2161 * If we are not in overwrite mode,
2162 * this is easy, just stop here.
2163 */
Slava Pestov884bfe82011-07-15 14:23:58 -07002164 if (!(buffer->flags & RB_FL_OVERWRITE)) {
2165 local_inc(&cpu_buffer->dropped_events);
Steven Rostedt77ae3652009-03-27 11:00:29 -04002166 goto out_reset;
Slava Pestov884bfe82011-07-15 14:23:58 -07002167 }
Steven Rostedtaa20ae82009-05-05 21:16:11 -04002168
Steven Rostedt77ae3652009-03-27 11:00:29 -04002169 ret = rb_handle_head_page(cpu_buffer,
2170 tail_page,
2171 next_page);
2172 if (ret < 0)
2173 goto out_reset;
2174 if (ret)
2175 goto out_again;
2176 } else {
2177 /*
2178 * We need to be careful here too. The
2179 * commit page could still be on the reader
2180 * page. We could have a small buffer, and
2181 * have filled up the buffer with events
2182 * from interrupts and such, and wrapped.
2183 *
2184 * Note, if the tail page is also the on the
2185 * reader_page, we let it move out.
2186 */
2187 if (unlikely((cpu_buffer->commit_page !=
2188 cpu_buffer->tail_page) &&
2189 (cpu_buffer->commit_page ==
2190 cpu_buffer->reader_page))) {
2191 local_inc(&cpu_buffer->commit_overrun);
2192 goto out_reset;
2193 }
Steven Rostedtaa20ae82009-05-05 21:16:11 -04002194 }
2195 }
2196
Steven Rostedt77ae3652009-03-27 11:00:29 -04002197 ret = rb_tail_page_update(cpu_buffer, tail_page, next_page);
2198 if (ret) {
2199 /*
2200 * Nested commits always have zero deltas, so
2201 * just reread the time stamp
2202 */
Steven Rostedte8bc43e2010-10-20 10:58:02 -04002203 ts = rb_time_stamp(buffer);
2204 next_page->page->time_stamp = ts;
Steven Rostedtaa20ae82009-05-05 21:16:11 -04002205 }
2206
Steven Rostedt77ae3652009-03-27 11:00:29 -04002207 out_again:
Steven Rostedtaa20ae82009-05-05 21:16:11 -04002208
Steven Rostedt77ae3652009-03-27 11:00:29 -04002209 rb_reset_tail(cpu_buffer, tail_page, tail, length);
Steven Rostedtaa20ae82009-05-05 21:16:11 -04002210
2211 /* fail and let the caller try again */
2212 return ERR_PTR(-EAGAIN);
2213
Steven Rostedt45141d42009-02-12 13:19:48 -05002214 out_reset:
Lai Jiangshan6f3b3442009-01-12 11:06:18 +08002215 /* reset write */
Steven Rostedtc7b09302009-06-11 11:12:00 -04002216 rb_reset_tail(cpu_buffer, tail_page, tail, length);
Lai Jiangshan6f3b3442009-01-12 11:06:18 +08002217
Steven Rostedtbf41a152008-10-04 02:00:59 -04002218 return NULL;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002219}
2220
Steven Rostedt6634ff22009-05-06 15:30:07 -04002221static struct ring_buffer_event *
2222__rb_reserve_next(struct ring_buffer_per_cpu *cpu_buffer,
Steven Rostedt69d1b832010-10-07 18:18:05 -04002223 unsigned long length, u64 ts,
2224 u64 delta, int add_timestamp)
Steven Rostedt6634ff22009-05-06 15:30:07 -04002225{
Steven Rostedt5a50e332009-11-17 08:43:01 -05002226 struct buffer_page *tail_page;
Steven Rostedt6634ff22009-05-06 15:30:07 -04002227 struct ring_buffer_event *event;
2228 unsigned long tail, write;
2229
Steven Rostedt69d1b832010-10-07 18:18:05 -04002230 /*
2231 * If the time delta since the last event is too big to
2232 * hold in the time field of the event, then we append a
2233 * TIME EXTEND event ahead of the data event.
2234 */
2235 if (unlikely(add_timestamp))
2236 length += RB_LEN_TIME_EXTEND;
2237
Steven Rostedt6634ff22009-05-06 15:30:07 -04002238 tail_page = cpu_buffer->tail_page;
2239 write = local_add_return(length, &tail_page->write);
Steven Rostedt77ae3652009-03-27 11:00:29 -04002240
2241 /* set write to only the index of the write */
2242 write &= RB_WRITE_MASK;
Steven Rostedt6634ff22009-05-06 15:30:07 -04002243 tail = write - length;
2244
2245 /* See if we shot pass the end of this buffer page */
Steven Rostedt747e94a2010-10-08 13:51:48 -04002246 if (unlikely(write > BUF_PAGE_SIZE))
Steven Rostedt6634ff22009-05-06 15:30:07 -04002247 return rb_move_tail(cpu_buffer, length, tail,
Steven Rostedt5a50e332009-11-17 08:43:01 -05002248 tail_page, ts);
Steven Rostedt6634ff22009-05-06 15:30:07 -04002249
2250 /* We reserved something on the buffer */
2251
Steven Rostedt6634ff22009-05-06 15:30:07 -04002252 event = __rb_page_index(tail_page, tail);
Vegard Nossum1744a212009-02-28 08:29:44 +01002253 kmemcheck_annotate_bitfield(event, bitfield);
Steven Rostedt69d1b832010-10-07 18:18:05 -04002254 rb_update_event(cpu_buffer, event, length, add_timestamp, delta);
Steven Rostedt6634ff22009-05-06 15:30:07 -04002255
Steven Rostedt69d1b832010-10-07 18:18:05 -04002256 local_inc(&tail_page->entries);
Steven Rostedt6634ff22009-05-06 15:30:07 -04002257
2258 /*
Steven Rostedtfa743952009-06-16 12:37:57 -04002259 * If this is the first commit on the page, then update
2260 * its timestamp.
Steven Rostedt6634ff22009-05-06 15:30:07 -04002261 */
Steven Rostedtfa743952009-06-16 12:37:57 -04002262 if (!tail)
Steven Rostedte8bc43e2010-10-20 10:58:02 -04002263 tail_page->page->time_stamp = ts;
Steven Rostedt6634ff22009-05-06 15:30:07 -04002264
Vaibhav Nagarnaikc64e1482011-08-16 14:46:16 -07002265 /* account for these added bytes */
2266 local_add(length, &cpu_buffer->entries_bytes);
2267
Steven Rostedt6634ff22009-05-06 15:30:07 -04002268 return event;
2269}
2270
Steven Rostedtedd813bf2009-06-02 23:00:53 -04002271static inline int
2272rb_try_to_discard(struct ring_buffer_per_cpu *cpu_buffer,
2273 struct ring_buffer_event *event)
2274{
2275 unsigned long new_index, old_index;
2276 struct buffer_page *bpage;
2277 unsigned long index;
2278 unsigned long addr;
2279
2280 new_index = rb_event_index(event);
Steven Rostedt69d1b832010-10-07 18:18:05 -04002281 old_index = new_index + rb_event_ts_length(event);
Steven Rostedtedd813bf2009-06-02 23:00:53 -04002282 addr = (unsigned long)event;
2283 addr &= PAGE_MASK;
2284
2285 bpage = cpu_buffer->tail_page;
2286
2287 if (bpage->page == (void *)addr && rb_page_write(bpage) == old_index) {
Steven Rostedt77ae3652009-03-27 11:00:29 -04002288 unsigned long write_mask =
2289 local_read(&bpage->write) & ~RB_WRITE_MASK;
Vaibhav Nagarnaikc64e1482011-08-16 14:46:16 -07002290 unsigned long event_length = rb_event_length(event);
Steven Rostedtedd813bf2009-06-02 23:00:53 -04002291 /*
2292 * This is on the tail page. It is possible that
2293 * a write could come in and move the tail page
2294 * and write to the next page. That is fine
2295 * because we just shorten what is on this page.
2296 */
Steven Rostedt77ae3652009-03-27 11:00:29 -04002297 old_index += write_mask;
2298 new_index += write_mask;
Steven Rostedtedd813bf2009-06-02 23:00:53 -04002299 index = local_cmpxchg(&bpage->write, old_index, new_index);
Vaibhav Nagarnaikc64e1482011-08-16 14:46:16 -07002300 if (index == old_index) {
2301 /* update counters */
2302 local_sub(event_length, &cpu_buffer->entries_bytes);
Steven Rostedtedd813bf2009-06-02 23:00:53 -04002303 return 1;
Vaibhav Nagarnaikc64e1482011-08-16 14:46:16 -07002304 }
Steven Rostedtedd813bf2009-06-02 23:00:53 -04002305 }
2306
2307 /* could not discard */
2308 return 0;
2309}
2310
Steven Rostedtfa743952009-06-16 12:37:57 -04002311static void rb_start_commit(struct ring_buffer_per_cpu *cpu_buffer)
2312{
2313 local_inc(&cpu_buffer->committing);
2314 local_inc(&cpu_buffer->commits);
2315}
2316
Steven Rostedtd9abde22010-10-19 13:17:08 -04002317static inline void rb_end_commit(struct ring_buffer_per_cpu *cpu_buffer)
Steven Rostedtfa743952009-06-16 12:37:57 -04002318{
2319 unsigned long commits;
2320
2321 if (RB_WARN_ON(cpu_buffer,
2322 !local_read(&cpu_buffer->committing)))
2323 return;
2324
2325 again:
2326 commits = local_read(&cpu_buffer->commits);
2327 /* synchronize with interrupts */
2328 barrier();
2329 if (local_read(&cpu_buffer->committing) == 1)
2330 rb_set_commit_to_write(cpu_buffer);
2331
2332 local_dec(&cpu_buffer->committing);
2333
2334 /* synchronize with interrupts */
2335 barrier();
2336
2337 /*
2338 * Need to account for interrupts coming in between the
2339 * updating of the commit page and the clearing of the
2340 * committing counter.
2341 */
2342 if (unlikely(local_read(&cpu_buffer->commits) != commits) &&
2343 !local_read(&cpu_buffer->committing)) {
2344 local_inc(&cpu_buffer->committing);
2345 goto again;
2346 }
2347}
2348
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002349static struct ring_buffer_event *
Steven Rostedt62f0b3e2009-09-04 14:11:34 -04002350rb_reserve_next_event(struct ring_buffer *buffer,
2351 struct ring_buffer_per_cpu *cpu_buffer,
Steven Rostedt1cd8d732009-05-11 14:08:09 -04002352 unsigned long length)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002353{
2354 struct ring_buffer_event *event;
Steven Rostedt69d1b832010-10-07 18:18:05 -04002355 u64 ts, delta;
Steven Rostedt818e3dd2008-10-31 09:58:35 -04002356 int nr_loops = 0;
Steven Rostedt69d1b832010-10-07 18:18:05 -04002357 int add_timestamp;
Steven Rostedt140ff892010-10-08 10:50:30 -04002358 u64 diff;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002359
Steven Rostedtfa743952009-06-16 12:37:57 -04002360 rb_start_commit(cpu_buffer);
2361
Steven Rostedt85bac322009-09-04 14:24:40 -04002362#ifdef CONFIG_RING_BUFFER_ALLOW_SWAP
Steven Rostedt62f0b3e2009-09-04 14:11:34 -04002363 /*
2364 * Due to the ability to swap a cpu buffer from a buffer
2365 * it is possible it was swapped before we committed.
2366 * (committing stops a swap). We check for it here and
2367 * if it happened, we have to fail the write.
2368 */
2369 barrier();
2370 if (unlikely(ACCESS_ONCE(cpu_buffer->buffer) != buffer)) {
2371 local_dec(&cpu_buffer->committing);
2372 local_dec(&cpu_buffer->commits);
2373 return NULL;
2374 }
Steven Rostedt85bac322009-09-04 14:24:40 -04002375#endif
Steven Rostedt62f0b3e2009-09-04 14:11:34 -04002376
Steven Rostedtbe957c42009-05-11 14:42:53 -04002377 length = rb_calculate_event_length(length);
Steven Rostedtbf41a152008-10-04 02:00:59 -04002378 again:
Steven Rostedt69d1b832010-10-07 18:18:05 -04002379 add_timestamp = 0;
2380 delta = 0;
2381
Steven Rostedt818e3dd2008-10-31 09:58:35 -04002382 /*
2383 * We allow for interrupts to reenter here and do a trace.
2384 * If one does, it will cause this original code to loop
2385 * back here. Even with heavy interrupts happening, this
2386 * should only happen a few times in a row. If this happens
2387 * 1000 times in a row, there must be either an interrupt
2388 * storm or we have something buggy.
2389 * Bail!
2390 */
Steven Rostedt3e89c7b2008-11-11 15:28:41 -05002391 if (RB_WARN_ON(cpu_buffer, ++nr_loops > 1000))
Steven Rostedtfa743952009-06-16 12:37:57 -04002392 goto out_fail;
Steven Rostedt818e3dd2008-10-31 09:58:35 -04002393
Jiri Olsa6d3f1e12009-10-23 19:36:19 -04002394 ts = rb_time_stamp(cpu_buffer->buffer);
Steven Rostedt140ff892010-10-08 10:50:30 -04002395 diff = ts - cpu_buffer->write_stamp;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002396
Steven Rostedt140ff892010-10-08 10:50:30 -04002397 /* make sure this diff is calculated here */
2398 barrier();
Steven Rostedtbf41a152008-10-04 02:00:59 -04002399
Steven Rostedt140ff892010-10-08 10:50:30 -04002400 /* Did the write stamp get updated already? */
2401 if (likely(ts >= cpu_buffer->write_stamp)) {
Steven Rostedt168b6b12009-05-11 22:11:05 -04002402 delta = diff;
2403 if (unlikely(test_time_stamp(delta))) {
Jiri Olsa31274d72011-02-18 15:52:19 +01002404 int local_clock_stable = 1;
2405#ifdef CONFIG_HAVE_UNSTABLE_SCHED_CLOCK
2406 local_clock_stable = sched_clock_stable;
2407#endif
Steven Rostedt69d1b832010-10-07 18:18:05 -04002408 WARN_ONCE(delta > (1ULL << 59),
Jiri Olsa31274d72011-02-18 15:52:19 +01002409 KERN_WARNING "Delta way too big! %llu ts=%llu write stamp = %llu\n%s",
Steven Rostedt69d1b832010-10-07 18:18:05 -04002410 (unsigned long long)delta,
2411 (unsigned long long)ts,
Jiri Olsa31274d72011-02-18 15:52:19 +01002412 (unsigned long long)cpu_buffer->write_stamp,
2413 local_clock_stable ? "" :
2414 "If you just came from a suspend/resume,\n"
2415 "please switch to the trace global clock:\n"
2416 " echo global > /sys/kernel/debug/tracing/trace_clock\n");
Steven Rostedt69d1b832010-10-07 18:18:05 -04002417 add_timestamp = 1;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002418 }
Steven Rostedt168b6b12009-05-11 22:11:05 -04002419 }
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002420
Steven Rostedt69d1b832010-10-07 18:18:05 -04002421 event = __rb_reserve_next(cpu_buffer, length, ts,
2422 delta, add_timestamp);
Steven Rostedt168b6b12009-05-11 22:11:05 -04002423 if (unlikely(PTR_ERR(event) == -EAGAIN))
Steven Rostedtbf41a152008-10-04 02:00:59 -04002424 goto again;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002425
Steven Rostedtfa743952009-06-16 12:37:57 -04002426 if (!event)
2427 goto out_fail;
Steven Rostedtbf41a152008-10-04 02:00:59 -04002428
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002429 return event;
Steven Rostedtfa743952009-06-16 12:37:57 -04002430
2431 out_fail:
2432 rb_end_commit(cpu_buffer);
2433 return NULL;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002434}
2435
Paul Mundt1155de42009-06-25 14:30:12 +09002436#ifdef CONFIG_TRACING
2437
Steven Rostedt567cd4d2012-11-02 18:33:05 -04002438/*
2439 * The lock and unlock are done within a preempt disable section.
2440 * The current_context per_cpu variable can only be modified
2441 * by the current task between lock and unlock. But it can
2442 * be modified more than once via an interrupt. To pass this
2443 * information from the lock to the unlock without having to
2444 * access the 'in_interrupt()' functions again (which do show
2445 * a bit of overhead in something as critical as function tracing,
2446 * we use a bitmask trick.
2447 *
2448 * bit 0 = NMI context
2449 * bit 1 = IRQ context
2450 * bit 2 = SoftIRQ context
2451 * bit 3 = normal context.
2452 *
2453 * This works because this is the order of contexts that can
2454 * preempt other contexts. A SoftIRQ never preempts an IRQ
2455 * context.
2456 *
2457 * When the context is determined, the corresponding bit is
2458 * checked and set (if it was set, then a recursion of that context
2459 * happened).
2460 *
2461 * On unlock, we need to clear this bit. To do so, just subtract
2462 * 1 from the current_context and AND it to itself.
2463 *
2464 * (binary)
2465 * 101 - 1 = 100
2466 * 101 & 100 = 100 (clearing bit zero)
2467 *
2468 * 1010 - 1 = 1001
2469 * 1010 & 1001 = 1000 (clearing bit 1)
2470 *
2471 * The least significant bit can be cleared this way, and it
2472 * just so happens that it is the same bit corresponding to
2473 * the current context.
2474 */
2475static DEFINE_PER_CPU(unsigned int, current_context);
Steven Rostedt261842b2009-04-16 21:41:52 -04002476
Steven Rostedt567cd4d2012-11-02 18:33:05 -04002477static __always_inline int trace_recursive_lock(void)
Steven Rostedt261842b2009-04-16 21:41:52 -04002478{
Steven Rostedt567cd4d2012-11-02 18:33:05 -04002479 unsigned int val = this_cpu_read(current_context);
2480 int bit;
Frederic Weisbeckere057a5e2009-04-19 23:38:12 +02002481
Steven Rostedt567cd4d2012-11-02 18:33:05 -04002482 if (in_interrupt()) {
2483 if (in_nmi())
2484 bit = 0;
2485 else if (in_irq())
2486 bit = 1;
2487 else
2488 bit = 2;
2489 } else
2490 bit = 3;
Frederic Weisbeckere057a5e2009-04-19 23:38:12 +02002491
Steven Rostedt567cd4d2012-11-02 18:33:05 -04002492 if (unlikely(val & (1 << bit)))
2493 return 1;
2494
2495 val |= (1 << bit);
2496 this_cpu_write(current_context, val);
2497
2498 return 0;
Steven Rostedtd9abde22010-10-19 13:17:08 -04002499}
2500
Steven Rostedt567cd4d2012-11-02 18:33:05 -04002501static __always_inline void trace_recursive_unlock(void)
Steven Rostedtd9abde22010-10-19 13:17:08 -04002502{
Steven Rostedt567cd4d2012-11-02 18:33:05 -04002503 unsigned int val = this_cpu_read(current_context);
Steven Rostedtd9abde22010-10-19 13:17:08 -04002504
Steven Rostedt567cd4d2012-11-02 18:33:05 -04002505 val--;
2506 val &= this_cpu_read(current_context);
2507 this_cpu_write(current_context, val);
Steven Rostedt261842b2009-04-16 21:41:52 -04002508}
2509
Paul Mundt1155de42009-06-25 14:30:12 +09002510#else
2511
2512#define trace_recursive_lock() (0)
2513#define trace_recursive_unlock() do { } while (0)
2514
2515#endif
2516
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002517/**
2518 * ring_buffer_lock_reserve - reserve a part of the buffer
2519 * @buffer: the ring buffer to reserve from
2520 * @length: the length of the data to reserve (excluding event header)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002521 *
2522 * Returns a reseverd event on the ring buffer to copy directly to.
2523 * The user of this interface will need to get the body to write into
2524 * and can use the ring_buffer_event_data() interface.
2525 *
2526 * The length is the length of the data needed, not the event length
2527 * which also includes the event header.
2528 *
2529 * Must be paired with ring_buffer_unlock_commit, unless NULL is returned.
2530 * If NULL is returned, then nothing has been allocated or locked.
2531 */
2532struct ring_buffer_event *
Arnaldo Carvalho de Melo0a987752009-02-05 16:12:56 -02002533ring_buffer_lock_reserve(struct ring_buffer *buffer, unsigned long length)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002534{
2535 struct ring_buffer_per_cpu *cpu_buffer;
2536 struct ring_buffer_event *event;
Steven Rostedt5168ae52010-06-03 09:36:50 -04002537 int cpu;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002538
Steven Rostedt033601a2008-11-21 12:41:55 -05002539 if (ring_buffer_flags != RB_BUFFERS_ON)
Steven Rostedta3583242008-11-11 15:01:42 -05002540 return NULL;
2541
Steven Rostedtbf41a152008-10-04 02:00:59 -04002542 /* If we are tracing schedule, we don't want to recurse */
Steven Rostedt5168ae52010-06-03 09:36:50 -04002543 preempt_disable_notrace();
Steven Rostedtbf41a152008-10-04 02:00:59 -04002544
Lai Jiangshan52fbe9c2010-03-08 14:50:43 +08002545 if (atomic_read(&buffer->record_disabled))
2546 goto out_nocheck;
2547
Steven Rostedt261842b2009-04-16 21:41:52 -04002548 if (trace_recursive_lock())
2549 goto out_nocheck;
2550
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002551 cpu = raw_smp_processor_id();
2552
Rusty Russell9e01c1b2009-01-01 10:12:22 +10302553 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedtd7690412008-10-01 00:29:53 -04002554 goto out;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002555
2556 cpu_buffer = buffer->buffers[cpu];
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002557
2558 if (atomic_read(&cpu_buffer->record_disabled))
Steven Rostedtd7690412008-10-01 00:29:53 -04002559 goto out;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002560
Steven Rostedtbe957c42009-05-11 14:42:53 -04002561 if (length > BUF_MAX_DATA_SIZE)
Steven Rostedtbf41a152008-10-04 02:00:59 -04002562 goto out;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002563
Steven Rostedt62f0b3e2009-09-04 14:11:34 -04002564 event = rb_reserve_next_event(buffer, cpu_buffer, length);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002565 if (!event)
Steven Rostedtd7690412008-10-01 00:29:53 -04002566 goto out;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002567
2568 return event;
2569
Steven Rostedtd7690412008-10-01 00:29:53 -04002570 out:
Steven Rostedt261842b2009-04-16 21:41:52 -04002571 trace_recursive_unlock();
2572
2573 out_nocheck:
Steven Rostedt5168ae52010-06-03 09:36:50 -04002574 preempt_enable_notrace();
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002575 return NULL;
2576}
Robert Richterc4f50182008-12-11 16:49:22 +01002577EXPORT_SYMBOL_GPL(ring_buffer_lock_reserve);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002578
Steven Rostedta1863c22009-09-03 10:23:58 -04002579static void
2580rb_update_write_stamp(struct ring_buffer_per_cpu *cpu_buffer,
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002581 struct ring_buffer_event *event)
2582{
Steven Rostedt69d1b832010-10-07 18:18:05 -04002583 u64 delta;
2584
Steven Rostedtfa743952009-06-16 12:37:57 -04002585 /*
2586 * The event first in the commit queue updates the
2587 * time stamp.
2588 */
Steven Rostedt69d1b832010-10-07 18:18:05 -04002589 if (rb_event_is_commit(cpu_buffer, event)) {
2590 /*
2591 * A commit event that is first on a page
2592 * updates the write timestamp with the page stamp
2593 */
2594 if (!rb_event_index(event))
2595 cpu_buffer->write_stamp =
2596 cpu_buffer->commit_page->page->time_stamp;
2597 else if (event->type_len == RINGBUF_TYPE_TIME_EXTEND) {
2598 delta = event->array[0];
2599 delta <<= TS_SHIFT;
2600 delta += event->time_delta;
2601 cpu_buffer->write_stamp += delta;
2602 } else
2603 cpu_buffer->write_stamp += event->time_delta;
2604 }
Steven Rostedta1863c22009-09-03 10:23:58 -04002605}
Steven Rostedtbf41a152008-10-04 02:00:59 -04002606
Steven Rostedta1863c22009-09-03 10:23:58 -04002607static void rb_commit(struct ring_buffer_per_cpu *cpu_buffer,
2608 struct ring_buffer_event *event)
2609{
2610 local_inc(&cpu_buffer->entries);
2611 rb_update_write_stamp(cpu_buffer, event);
Steven Rostedtfa743952009-06-16 12:37:57 -04002612 rb_end_commit(cpu_buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002613}
2614
2615/**
2616 * ring_buffer_unlock_commit - commit a reserved
2617 * @buffer: The buffer to commit to
2618 * @event: The event pointer to commit.
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002619 *
2620 * This commits the data to the ring buffer, and releases any locks held.
2621 *
2622 * Must be paired with ring_buffer_lock_reserve.
2623 */
2624int ring_buffer_unlock_commit(struct ring_buffer *buffer,
Arnaldo Carvalho de Melo0a987752009-02-05 16:12:56 -02002625 struct ring_buffer_event *event)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002626{
2627 struct ring_buffer_per_cpu *cpu_buffer;
2628 int cpu = raw_smp_processor_id();
2629
2630 cpu_buffer = buffer->buffers[cpu];
2631
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002632 rb_commit(cpu_buffer, event);
2633
Steven Rostedt261842b2009-04-16 21:41:52 -04002634 trace_recursive_unlock();
2635
Steven Rostedt5168ae52010-06-03 09:36:50 -04002636 preempt_enable_notrace();
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002637
2638 return 0;
2639}
Robert Richterc4f50182008-12-11 16:49:22 +01002640EXPORT_SYMBOL_GPL(ring_buffer_unlock_commit);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002641
Frederic Weisbeckerf3b9aae2009-04-19 23:39:33 +02002642static inline void rb_event_discard(struct ring_buffer_event *event)
2643{
Steven Rostedt69d1b832010-10-07 18:18:05 -04002644 if (event->type_len == RINGBUF_TYPE_TIME_EXTEND)
2645 event = skip_time_extend(event);
2646
Lai Jiangshan334d4162009-04-24 11:27:05 +08002647 /* array[0] holds the actual length for the discarded event */
2648 event->array[0] = rb_event_data_length(event) - RB_EVNT_HDR_SIZE;
2649 event->type_len = RINGBUF_TYPE_PADDING;
Frederic Weisbeckerf3b9aae2009-04-19 23:39:33 +02002650 /* time delta must be non zero */
2651 if (!event->time_delta)
2652 event->time_delta = 1;
2653}
2654
Steven Rostedta1863c22009-09-03 10:23:58 -04002655/*
2656 * Decrement the entries to the page that an event is on.
2657 * The event does not even need to exist, only the pointer
2658 * to the page it is on. This may only be called before the commit
2659 * takes place.
2660 */
2661static inline void
2662rb_decrement_entry(struct ring_buffer_per_cpu *cpu_buffer,
2663 struct ring_buffer_event *event)
2664{
2665 unsigned long addr = (unsigned long)event;
2666 struct buffer_page *bpage = cpu_buffer->commit_page;
2667 struct buffer_page *start;
2668
2669 addr &= PAGE_MASK;
2670
2671 /* Do the likely case first */
2672 if (likely(bpage->page == (void *)addr)) {
2673 local_dec(&bpage->entries);
2674 return;
2675 }
2676
2677 /*
2678 * Because the commit page may be on the reader page we
2679 * start with the next page and check the end loop there.
2680 */
2681 rb_inc_page(cpu_buffer, &bpage);
2682 start = bpage;
2683 do {
2684 if (bpage->page == (void *)addr) {
2685 local_dec(&bpage->entries);
2686 return;
2687 }
2688 rb_inc_page(cpu_buffer, &bpage);
2689 } while (bpage != start);
2690
2691 /* commit not part of this buffer?? */
2692 RB_WARN_ON(cpu_buffer, 1);
2693}
2694
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002695/**
Steven Rostedtfa1b47d2009-04-02 00:09:41 -04002696 * ring_buffer_commit_discard - discard an event that has not been committed
2697 * @buffer: the ring buffer
2698 * @event: non committed event to discard
2699 *
Steven Rostedtdc892f72009-09-03 15:33:41 -04002700 * Sometimes an event that is in the ring buffer needs to be ignored.
2701 * This function lets the user discard an event in the ring buffer
2702 * and then that event will not be read later.
2703 *
2704 * This function only works if it is called before the the item has been
2705 * committed. It will try to free the event from the ring buffer
Steven Rostedtfa1b47d2009-04-02 00:09:41 -04002706 * if another event has not been added behind it.
2707 *
2708 * If another event has been added behind it, it will set the event
2709 * up as discarded, and perform the commit.
2710 *
2711 * If this function is called, do not call ring_buffer_unlock_commit on
2712 * the event.
2713 */
2714void ring_buffer_discard_commit(struct ring_buffer *buffer,
2715 struct ring_buffer_event *event)
2716{
2717 struct ring_buffer_per_cpu *cpu_buffer;
Steven Rostedtfa1b47d2009-04-02 00:09:41 -04002718 int cpu;
2719
2720 /* The event is discarded regardless */
Frederic Weisbeckerf3b9aae2009-04-19 23:39:33 +02002721 rb_event_discard(event);
Steven Rostedtfa1b47d2009-04-02 00:09:41 -04002722
Steven Rostedtfa743952009-06-16 12:37:57 -04002723 cpu = smp_processor_id();
2724 cpu_buffer = buffer->buffers[cpu];
2725
Steven Rostedtfa1b47d2009-04-02 00:09:41 -04002726 /*
2727 * This must only be called if the event has not been
2728 * committed yet. Thus we can assume that preemption
2729 * is still disabled.
2730 */
Steven Rostedtfa743952009-06-16 12:37:57 -04002731 RB_WARN_ON(buffer, !local_read(&cpu_buffer->committing));
Steven Rostedtfa1b47d2009-04-02 00:09:41 -04002732
Steven Rostedta1863c22009-09-03 10:23:58 -04002733 rb_decrement_entry(cpu_buffer, event);
Steven Rostedt0f2541d2009-08-05 12:02:48 -04002734 if (rb_try_to_discard(cpu_buffer, event))
Steven Rostedtedd813bf2009-06-02 23:00:53 -04002735 goto out;
Steven Rostedtfa1b47d2009-04-02 00:09:41 -04002736
2737 /*
2738 * The commit is still visible by the reader, so we
Steven Rostedta1863c22009-09-03 10:23:58 -04002739 * must still update the timestamp.
Steven Rostedtfa1b47d2009-04-02 00:09:41 -04002740 */
Steven Rostedta1863c22009-09-03 10:23:58 -04002741 rb_update_write_stamp(cpu_buffer, event);
Steven Rostedtfa1b47d2009-04-02 00:09:41 -04002742 out:
Steven Rostedtfa743952009-06-16 12:37:57 -04002743 rb_end_commit(cpu_buffer);
Steven Rostedtfa1b47d2009-04-02 00:09:41 -04002744
Frederic Weisbeckerf3b9aae2009-04-19 23:39:33 +02002745 trace_recursive_unlock();
2746
Steven Rostedt5168ae52010-06-03 09:36:50 -04002747 preempt_enable_notrace();
Steven Rostedtfa1b47d2009-04-02 00:09:41 -04002748
2749}
2750EXPORT_SYMBOL_GPL(ring_buffer_discard_commit);
2751
2752/**
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002753 * ring_buffer_write - write data to the buffer without reserving
2754 * @buffer: The ring buffer to write to.
2755 * @length: The length of the data being written (excluding the event header)
2756 * @data: The data to write to the buffer.
2757 *
2758 * This is like ring_buffer_lock_reserve and ring_buffer_unlock_commit as
2759 * one function. If you already have the data to write to the buffer, it
2760 * may be easier to simply call this function.
2761 *
2762 * Note, like ring_buffer_lock_reserve, the length is the length of the data
2763 * and not the length of the event which would hold the header.
2764 */
2765int ring_buffer_write(struct ring_buffer *buffer,
David Sharp01e3e712012-06-07 16:46:24 -07002766 unsigned long length,
2767 void *data)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002768{
2769 struct ring_buffer_per_cpu *cpu_buffer;
2770 struct ring_buffer_event *event;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002771 void *body;
2772 int ret = -EBUSY;
Steven Rostedt5168ae52010-06-03 09:36:50 -04002773 int cpu;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002774
Steven Rostedt033601a2008-11-21 12:41:55 -05002775 if (ring_buffer_flags != RB_BUFFERS_ON)
Steven Rostedta3583242008-11-11 15:01:42 -05002776 return -EBUSY;
2777
Steven Rostedt5168ae52010-06-03 09:36:50 -04002778 preempt_disable_notrace();
Steven Rostedtbf41a152008-10-04 02:00:59 -04002779
Lai Jiangshan52fbe9c2010-03-08 14:50:43 +08002780 if (atomic_read(&buffer->record_disabled))
2781 goto out;
2782
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002783 cpu = raw_smp_processor_id();
2784
Rusty Russell9e01c1b2009-01-01 10:12:22 +10302785 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedtd7690412008-10-01 00:29:53 -04002786 goto out;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002787
2788 cpu_buffer = buffer->buffers[cpu];
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002789
2790 if (atomic_read(&cpu_buffer->record_disabled))
2791 goto out;
2792
Steven Rostedtbe957c42009-05-11 14:42:53 -04002793 if (length > BUF_MAX_DATA_SIZE)
2794 goto out;
2795
Steven Rostedt62f0b3e2009-09-04 14:11:34 -04002796 event = rb_reserve_next_event(buffer, cpu_buffer, length);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002797 if (!event)
2798 goto out;
2799
2800 body = rb_event_data(event);
2801
2802 memcpy(body, data, length);
2803
2804 rb_commit(cpu_buffer, event);
2805
2806 ret = 0;
2807 out:
Steven Rostedt5168ae52010-06-03 09:36:50 -04002808 preempt_enable_notrace();
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002809
2810 return ret;
2811}
Robert Richterc4f50182008-12-11 16:49:22 +01002812EXPORT_SYMBOL_GPL(ring_buffer_write);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002813
Andrew Morton34a148b2009-01-09 12:27:09 -08002814static int rb_per_cpu_empty(struct ring_buffer_per_cpu *cpu_buffer)
Steven Rostedtbf41a152008-10-04 02:00:59 -04002815{
2816 struct buffer_page *reader = cpu_buffer->reader_page;
Steven Rostedt77ae3652009-03-27 11:00:29 -04002817 struct buffer_page *head = rb_set_head_page(cpu_buffer);
Steven Rostedtbf41a152008-10-04 02:00:59 -04002818 struct buffer_page *commit = cpu_buffer->commit_page;
2819
Steven Rostedt77ae3652009-03-27 11:00:29 -04002820 /* In case of error, head will be NULL */
2821 if (unlikely(!head))
2822 return 1;
2823
Steven Rostedtbf41a152008-10-04 02:00:59 -04002824 return reader->read == rb_page_commit(reader) &&
2825 (commit == reader ||
2826 (commit == head &&
2827 head->read == rb_page_commit(commit)));
2828}
2829
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002830/**
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002831 * ring_buffer_record_disable - stop all writes into the buffer
2832 * @buffer: The ring buffer to stop writes to.
2833 *
2834 * This prevents all writes to the buffer. Any attempt to write
2835 * to the buffer after this will fail and return NULL.
2836 *
2837 * The caller should call synchronize_sched() after this.
2838 */
2839void ring_buffer_record_disable(struct ring_buffer *buffer)
2840{
2841 atomic_inc(&buffer->record_disabled);
2842}
Robert Richterc4f50182008-12-11 16:49:22 +01002843EXPORT_SYMBOL_GPL(ring_buffer_record_disable);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002844
2845/**
2846 * ring_buffer_record_enable - enable writes to the buffer
2847 * @buffer: The ring buffer to enable writes
2848 *
2849 * Note, multiple disables will need the same number of enables
Adam Buchbinderc41b20e2009-12-11 16:35:39 -05002850 * to truly enable the writing (much like preempt_disable).
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002851 */
2852void ring_buffer_record_enable(struct ring_buffer *buffer)
2853{
2854 atomic_dec(&buffer->record_disabled);
2855}
Robert Richterc4f50182008-12-11 16:49:22 +01002856EXPORT_SYMBOL_GPL(ring_buffer_record_enable);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002857
2858/**
Steven Rostedt499e5472012-02-22 15:50:28 -05002859 * ring_buffer_record_off - stop all writes into the buffer
2860 * @buffer: The ring buffer to stop writes to.
2861 *
2862 * This prevents all writes to the buffer. Any attempt to write
2863 * to the buffer after this will fail and return NULL.
2864 *
2865 * This is different than ring_buffer_record_disable() as
Wang Tianhong87abb3b2012-08-02 14:02:00 +08002866 * it works like an on/off switch, where as the disable() version
Steven Rostedt499e5472012-02-22 15:50:28 -05002867 * must be paired with a enable().
2868 */
2869void ring_buffer_record_off(struct ring_buffer *buffer)
2870{
2871 unsigned int rd;
2872 unsigned int new_rd;
2873
2874 do {
2875 rd = atomic_read(&buffer->record_disabled);
2876 new_rd = rd | RB_BUFFER_OFF;
2877 } while (atomic_cmpxchg(&buffer->record_disabled, rd, new_rd) != rd);
2878}
2879EXPORT_SYMBOL_GPL(ring_buffer_record_off);
2880
2881/**
2882 * ring_buffer_record_on - restart writes into the buffer
2883 * @buffer: The ring buffer to start writes to.
2884 *
2885 * This enables all writes to the buffer that was disabled by
2886 * ring_buffer_record_off().
2887 *
2888 * This is different than ring_buffer_record_enable() as
Wang Tianhong87abb3b2012-08-02 14:02:00 +08002889 * it works like an on/off switch, where as the enable() version
Steven Rostedt499e5472012-02-22 15:50:28 -05002890 * must be paired with a disable().
2891 */
2892void ring_buffer_record_on(struct ring_buffer *buffer)
2893{
2894 unsigned int rd;
2895 unsigned int new_rd;
2896
2897 do {
2898 rd = atomic_read(&buffer->record_disabled);
2899 new_rd = rd & ~RB_BUFFER_OFF;
2900 } while (atomic_cmpxchg(&buffer->record_disabled, rd, new_rd) != rd);
2901}
2902EXPORT_SYMBOL_GPL(ring_buffer_record_on);
2903
2904/**
2905 * ring_buffer_record_is_on - return true if the ring buffer can write
2906 * @buffer: The ring buffer to see if write is enabled
2907 *
2908 * Returns true if the ring buffer is in a state that it accepts writes.
2909 */
2910int ring_buffer_record_is_on(struct ring_buffer *buffer)
2911{
2912 return !atomic_read(&buffer->record_disabled);
2913}
2914
2915/**
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002916 * ring_buffer_record_disable_cpu - stop all writes into the cpu_buffer
2917 * @buffer: The ring buffer to stop writes to.
2918 * @cpu: The CPU buffer to stop
2919 *
2920 * This prevents all writes to the buffer. Any attempt to write
2921 * to the buffer after this will fail and return NULL.
2922 *
2923 * The caller should call synchronize_sched() after this.
2924 */
2925void ring_buffer_record_disable_cpu(struct ring_buffer *buffer, int cpu)
2926{
2927 struct ring_buffer_per_cpu *cpu_buffer;
2928
Rusty Russell9e01c1b2009-01-01 10:12:22 +10302929 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedt8aabee52009-03-12 13:13:49 -04002930 return;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002931
2932 cpu_buffer = buffer->buffers[cpu];
2933 atomic_inc(&cpu_buffer->record_disabled);
2934}
Robert Richterc4f50182008-12-11 16:49:22 +01002935EXPORT_SYMBOL_GPL(ring_buffer_record_disable_cpu);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002936
2937/**
2938 * ring_buffer_record_enable_cpu - enable writes to the buffer
2939 * @buffer: The ring buffer to enable writes
2940 * @cpu: The CPU to enable.
2941 *
2942 * Note, multiple disables will need the same number of enables
Adam Buchbinderc41b20e2009-12-11 16:35:39 -05002943 * to truly enable the writing (much like preempt_disable).
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002944 */
2945void ring_buffer_record_enable_cpu(struct ring_buffer *buffer, int cpu)
2946{
2947 struct ring_buffer_per_cpu *cpu_buffer;
2948
Rusty Russell9e01c1b2009-01-01 10:12:22 +10302949 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedt8aabee52009-03-12 13:13:49 -04002950 return;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002951
2952 cpu_buffer = buffer->buffers[cpu];
2953 atomic_dec(&cpu_buffer->record_disabled);
2954}
Robert Richterc4f50182008-12-11 16:49:22 +01002955EXPORT_SYMBOL_GPL(ring_buffer_record_enable_cpu);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002956
Steven Rostedtf6195aa2010-09-01 12:23:12 -04002957/*
2958 * The total entries in the ring buffer is the running counter
2959 * of entries entered into the ring buffer, minus the sum of
2960 * the entries read from the ring buffer and the number of
2961 * entries that were overwritten.
2962 */
2963static inline unsigned long
2964rb_num_of_entries(struct ring_buffer_per_cpu *cpu_buffer)
2965{
2966 return local_read(&cpu_buffer->entries) -
2967 (local_read(&cpu_buffer->overrun) + cpu_buffer->read);
2968}
2969
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04002970/**
Vaibhav Nagarnaikc64e1482011-08-16 14:46:16 -07002971 * ring_buffer_oldest_event_ts - get the oldest event timestamp from the buffer
2972 * @buffer: The ring buffer
2973 * @cpu: The per CPU buffer to read from.
2974 */
Yoshihiro YUNOMAE50ecf2c2012-10-11 16:27:54 -07002975u64 ring_buffer_oldest_event_ts(struct ring_buffer *buffer, int cpu)
Vaibhav Nagarnaikc64e1482011-08-16 14:46:16 -07002976{
2977 unsigned long flags;
2978 struct ring_buffer_per_cpu *cpu_buffer;
2979 struct buffer_page *bpage;
Linus Torvaldsda830e52012-12-11 18:18:58 -08002980 u64 ret = 0;
Vaibhav Nagarnaikc64e1482011-08-16 14:46:16 -07002981
2982 if (!cpumask_test_cpu(cpu, buffer->cpumask))
2983 return 0;
2984
2985 cpu_buffer = buffer->buffers[cpu];
Linus Torvalds7115e3f2011-10-26 17:03:38 +02002986 raw_spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
Vaibhav Nagarnaikc64e1482011-08-16 14:46:16 -07002987 /*
2988 * if the tail is on reader_page, oldest time stamp is on the reader
2989 * page
2990 */
2991 if (cpu_buffer->tail_page == cpu_buffer->reader_page)
2992 bpage = cpu_buffer->reader_page;
2993 else
2994 bpage = rb_set_head_page(cpu_buffer);
Steven Rostedt54f7be52012-11-29 22:27:22 -05002995 if (bpage)
2996 ret = bpage->page->time_stamp;
Linus Torvalds7115e3f2011-10-26 17:03:38 +02002997 raw_spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
Vaibhav Nagarnaikc64e1482011-08-16 14:46:16 -07002998
2999 return ret;
3000}
3001EXPORT_SYMBOL_GPL(ring_buffer_oldest_event_ts);
3002
3003/**
3004 * ring_buffer_bytes_cpu - get the number of bytes consumed in a cpu buffer
3005 * @buffer: The ring buffer
3006 * @cpu: The per CPU buffer to read from.
3007 */
3008unsigned long ring_buffer_bytes_cpu(struct ring_buffer *buffer, int cpu)
3009{
3010 struct ring_buffer_per_cpu *cpu_buffer;
3011 unsigned long ret;
3012
3013 if (!cpumask_test_cpu(cpu, buffer->cpumask))
3014 return 0;
3015
3016 cpu_buffer = buffer->buffers[cpu];
3017 ret = local_read(&cpu_buffer->entries_bytes) - cpu_buffer->read_bytes;
3018
3019 return ret;
3020}
3021EXPORT_SYMBOL_GPL(ring_buffer_bytes_cpu);
3022
3023/**
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003024 * ring_buffer_entries_cpu - get the number of entries in a cpu buffer
3025 * @buffer: The ring buffer
3026 * @cpu: The per CPU buffer to get the entries from.
3027 */
3028unsigned long ring_buffer_entries_cpu(struct ring_buffer *buffer, int cpu)
3029{
3030 struct ring_buffer_per_cpu *cpu_buffer;
3031
Rusty Russell9e01c1b2009-01-01 10:12:22 +10303032 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedt8aabee52009-03-12 13:13:49 -04003033 return 0;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003034
3035 cpu_buffer = buffer->buffers[cpu];
Steven Rostedt554f7862009-03-11 22:00:13 -04003036
Steven Rostedtf6195aa2010-09-01 12:23:12 -04003037 return rb_num_of_entries(cpu_buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003038}
Robert Richterc4f50182008-12-11 16:49:22 +01003039EXPORT_SYMBOL_GPL(ring_buffer_entries_cpu);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003040
3041/**
Slava Pestov884bfe82011-07-15 14:23:58 -07003042 * ring_buffer_overrun_cpu - get the number of overruns caused by the ring
3043 * buffer wrapping around (only if RB_FL_OVERWRITE is on).
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003044 * @buffer: The ring buffer
3045 * @cpu: The per CPU buffer to get the number of overruns from
3046 */
3047unsigned long ring_buffer_overrun_cpu(struct ring_buffer *buffer, int cpu)
3048{
3049 struct ring_buffer_per_cpu *cpu_buffer;
Steven Rostedt8aabee52009-03-12 13:13:49 -04003050 unsigned long ret;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003051
Rusty Russell9e01c1b2009-01-01 10:12:22 +10303052 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedt8aabee52009-03-12 13:13:49 -04003053 return 0;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003054
3055 cpu_buffer = buffer->buffers[cpu];
Steven Rostedt77ae3652009-03-27 11:00:29 -04003056 ret = local_read(&cpu_buffer->overrun);
Steven Rostedt554f7862009-03-11 22:00:13 -04003057
3058 return ret;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003059}
Robert Richterc4f50182008-12-11 16:49:22 +01003060EXPORT_SYMBOL_GPL(ring_buffer_overrun_cpu);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003061
3062/**
Slava Pestov884bfe82011-07-15 14:23:58 -07003063 * ring_buffer_commit_overrun_cpu - get the number of overruns caused by
3064 * commits failing due to the buffer wrapping around while there are uncommitted
3065 * events, such as during an interrupt storm.
Steven Rostedtf0d2c682009-04-29 13:43:37 -04003066 * @buffer: The ring buffer
3067 * @cpu: The per CPU buffer to get the number of overruns from
3068 */
3069unsigned long
3070ring_buffer_commit_overrun_cpu(struct ring_buffer *buffer, int cpu)
3071{
3072 struct ring_buffer_per_cpu *cpu_buffer;
3073 unsigned long ret;
3074
3075 if (!cpumask_test_cpu(cpu, buffer->cpumask))
3076 return 0;
3077
3078 cpu_buffer = buffer->buffers[cpu];
Steven Rostedt77ae3652009-03-27 11:00:29 -04003079 ret = local_read(&cpu_buffer->commit_overrun);
Steven Rostedtf0d2c682009-04-29 13:43:37 -04003080
3081 return ret;
3082}
3083EXPORT_SYMBOL_GPL(ring_buffer_commit_overrun_cpu);
3084
3085/**
Slava Pestov884bfe82011-07-15 14:23:58 -07003086 * ring_buffer_dropped_events_cpu - get the number of dropped events caused by
3087 * the ring buffer filling up (only if RB_FL_OVERWRITE is off).
3088 * @buffer: The ring buffer
3089 * @cpu: The per CPU buffer to get the number of overruns from
3090 */
3091unsigned long
3092ring_buffer_dropped_events_cpu(struct ring_buffer *buffer, int cpu)
3093{
3094 struct ring_buffer_per_cpu *cpu_buffer;
3095 unsigned long ret;
3096
3097 if (!cpumask_test_cpu(cpu, buffer->cpumask))
3098 return 0;
3099
3100 cpu_buffer = buffer->buffers[cpu];
3101 ret = local_read(&cpu_buffer->dropped_events);
3102
3103 return ret;
3104}
3105EXPORT_SYMBOL_GPL(ring_buffer_dropped_events_cpu);
3106
3107/**
Steven Rostedt (Red Hat)ad964702013-01-29 17:45:49 -05003108 * ring_buffer_read_events_cpu - get the number of events successfully read
3109 * @buffer: The ring buffer
3110 * @cpu: The per CPU buffer to get the number of events read
3111 */
3112unsigned long
3113ring_buffer_read_events_cpu(struct ring_buffer *buffer, int cpu)
3114{
3115 struct ring_buffer_per_cpu *cpu_buffer;
3116
3117 if (!cpumask_test_cpu(cpu, buffer->cpumask))
3118 return 0;
3119
3120 cpu_buffer = buffer->buffers[cpu];
3121 return cpu_buffer->read;
3122}
3123EXPORT_SYMBOL_GPL(ring_buffer_read_events_cpu);
3124
3125/**
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003126 * ring_buffer_entries - get the number of entries in a buffer
3127 * @buffer: The ring buffer
3128 *
3129 * Returns the total number of entries in the ring buffer
3130 * (all CPU entries)
3131 */
3132unsigned long ring_buffer_entries(struct ring_buffer *buffer)
3133{
3134 struct ring_buffer_per_cpu *cpu_buffer;
3135 unsigned long entries = 0;
3136 int cpu;
3137
3138 /* if you care about this being correct, lock the buffer */
3139 for_each_buffer_cpu(buffer, cpu) {
3140 cpu_buffer = buffer->buffers[cpu];
Steven Rostedtf6195aa2010-09-01 12:23:12 -04003141 entries += rb_num_of_entries(cpu_buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003142 }
3143
3144 return entries;
3145}
Robert Richterc4f50182008-12-11 16:49:22 +01003146EXPORT_SYMBOL_GPL(ring_buffer_entries);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003147
3148/**
Jiri Olsa67b394f2009-10-23 19:36:18 -04003149 * ring_buffer_overruns - get the number of overruns in buffer
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003150 * @buffer: The ring buffer
3151 *
3152 * Returns the total number of overruns in the ring buffer
3153 * (all CPU entries)
3154 */
3155unsigned long ring_buffer_overruns(struct ring_buffer *buffer)
3156{
3157 struct ring_buffer_per_cpu *cpu_buffer;
3158 unsigned long overruns = 0;
3159 int cpu;
3160
3161 /* if you care about this being correct, lock the buffer */
3162 for_each_buffer_cpu(buffer, cpu) {
3163 cpu_buffer = buffer->buffers[cpu];
Steven Rostedt77ae3652009-03-27 11:00:29 -04003164 overruns += local_read(&cpu_buffer->overrun);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003165 }
3166
3167 return overruns;
3168}
Robert Richterc4f50182008-12-11 16:49:22 +01003169EXPORT_SYMBOL_GPL(ring_buffer_overruns);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003170
Steven Rostedt642edba2008-11-12 00:01:26 -05003171static void rb_iter_reset(struct ring_buffer_iter *iter)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003172{
3173 struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
3174
Steven Rostedtd7690412008-10-01 00:29:53 -04003175 /* Iterator usage is expected to have record disabled */
3176 if (list_empty(&cpu_buffer->reader_page->list)) {
Steven Rostedt77ae3652009-03-27 11:00:29 -04003177 iter->head_page = rb_set_head_page(cpu_buffer);
3178 if (unlikely(!iter->head_page))
3179 return;
3180 iter->head = iter->head_page->read;
Steven Rostedtd7690412008-10-01 00:29:53 -04003181 } else {
3182 iter->head_page = cpu_buffer->reader_page;
Steven Rostedt6f807ac2008-10-04 02:00:58 -04003183 iter->head = cpu_buffer->reader_page->read;
Steven Rostedtd7690412008-10-01 00:29:53 -04003184 }
3185 if (iter->head)
3186 iter->read_stamp = cpu_buffer->read_stamp;
3187 else
Steven Rostedtabc9b562008-12-02 15:34:06 -05003188 iter->read_stamp = iter->head_page->page->time_stamp;
Steven Rostedt492a74f2010-01-25 15:17:47 -05003189 iter->cache_reader_page = cpu_buffer->reader_page;
3190 iter->cache_read = cpu_buffer->read;
Steven Rostedt642edba2008-11-12 00:01:26 -05003191}
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003192
Steven Rostedt642edba2008-11-12 00:01:26 -05003193/**
3194 * ring_buffer_iter_reset - reset an iterator
3195 * @iter: The iterator to reset
3196 *
3197 * Resets the iterator, so that it will start from the beginning
3198 * again.
3199 */
3200void ring_buffer_iter_reset(struct ring_buffer_iter *iter)
3201{
Steven Rostedt554f7862009-03-11 22:00:13 -04003202 struct ring_buffer_per_cpu *cpu_buffer;
Steven Rostedt642edba2008-11-12 00:01:26 -05003203 unsigned long flags;
3204
Steven Rostedt554f7862009-03-11 22:00:13 -04003205 if (!iter)
3206 return;
3207
3208 cpu_buffer = iter->cpu_buffer;
3209
Thomas Gleixner5389f6f2009-07-25 17:13:33 +02003210 raw_spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
Steven Rostedt642edba2008-11-12 00:01:26 -05003211 rb_iter_reset(iter);
Thomas Gleixner5389f6f2009-07-25 17:13:33 +02003212 raw_spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003213}
Robert Richterc4f50182008-12-11 16:49:22 +01003214EXPORT_SYMBOL_GPL(ring_buffer_iter_reset);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003215
3216/**
3217 * ring_buffer_iter_empty - check if an iterator has no more to read
3218 * @iter: The iterator to check
3219 */
3220int ring_buffer_iter_empty(struct ring_buffer_iter *iter)
3221{
3222 struct ring_buffer_per_cpu *cpu_buffer;
3223
3224 cpu_buffer = iter->cpu_buffer;
3225
Steven Rostedtbf41a152008-10-04 02:00:59 -04003226 return iter->head_page == cpu_buffer->commit_page &&
3227 iter->head == rb_commit_index(cpu_buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003228}
Robert Richterc4f50182008-12-11 16:49:22 +01003229EXPORT_SYMBOL_GPL(ring_buffer_iter_empty);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003230
3231static void
3232rb_update_read_stamp(struct ring_buffer_per_cpu *cpu_buffer,
3233 struct ring_buffer_event *event)
3234{
3235 u64 delta;
3236
Lai Jiangshan334d4162009-04-24 11:27:05 +08003237 switch (event->type_len) {
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003238 case RINGBUF_TYPE_PADDING:
3239 return;
3240
3241 case RINGBUF_TYPE_TIME_EXTEND:
3242 delta = event->array[0];
3243 delta <<= TS_SHIFT;
3244 delta += event->time_delta;
3245 cpu_buffer->read_stamp += delta;
3246 return;
3247
3248 case RINGBUF_TYPE_TIME_STAMP:
3249 /* FIXME: not implemented */
3250 return;
3251
3252 case RINGBUF_TYPE_DATA:
3253 cpu_buffer->read_stamp += event->time_delta;
3254 return;
3255
3256 default:
3257 BUG();
3258 }
3259 return;
3260}
3261
3262static void
3263rb_update_iter_read_stamp(struct ring_buffer_iter *iter,
3264 struct ring_buffer_event *event)
3265{
3266 u64 delta;
3267
Lai Jiangshan334d4162009-04-24 11:27:05 +08003268 switch (event->type_len) {
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003269 case RINGBUF_TYPE_PADDING:
3270 return;
3271
3272 case RINGBUF_TYPE_TIME_EXTEND:
3273 delta = event->array[0];
3274 delta <<= TS_SHIFT;
3275 delta += event->time_delta;
3276 iter->read_stamp += delta;
3277 return;
3278
3279 case RINGBUF_TYPE_TIME_STAMP:
3280 /* FIXME: not implemented */
3281 return;
3282
3283 case RINGBUF_TYPE_DATA:
3284 iter->read_stamp += event->time_delta;
3285 return;
3286
3287 default:
3288 BUG();
3289 }
3290 return;
3291}
3292
Steven Rostedtd7690412008-10-01 00:29:53 -04003293static struct buffer_page *
3294rb_get_reader_page(struct ring_buffer_per_cpu *cpu_buffer)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003295{
Steven Rostedtd7690412008-10-01 00:29:53 -04003296 struct buffer_page *reader = NULL;
Steven Rostedt66a8cb92010-03-31 13:21:56 -04003297 unsigned long overwrite;
Steven Rostedtd7690412008-10-01 00:29:53 -04003298 unsigned long flags;
Steven Rostedt818e3dd2008-10-31 09:58:35 -04003299 int nr_loops = 0;
Steven Rostedt77ae3652009-03-27 11:00:29 -04003300 int ret;
Steven Rostedtd7690412008-10-01 00:29:53 -04003301
Steven Rostedt3e03fb72008-11-06 00:09:43 -05003302 local_irq_save(flags);
Thomas Gleixner0199c4e2009-12-02 20:01:25 +01003303 arch_spin_lock(&cpu_buffer->lock);
Steven Rostedtd7690412008-10-01 00:29:53 -04003304
3305 again:
Steven Rostedt818e3dd2008-10-31 09:58:35 -04003306 /*
3307 * This should normally only loop twice. But because the
3308 * start of the reader inserts an empty page, it causes
3309 * a case where we will loop three times. There should be no
3310 * reason to loop four times (that I know of).
3311 */
Steven Rostedt3e89c7b2008-11-11 15:28:41 -05003312 if (RB_WARN_ON(cpu_buffer, ++nr_loops > 3)) {
Steven Rostedt818e3dd2008-10-31 09:58:35 -04003313 reader = NULL;
3314 goto out;
3315 }
3316
Steven Rostedtd7690412008-10-01 00:29:53 -04003317 reader = cpu_buffer->reader_page;
3318
3319 /* If there's more to read, return this page */
Steven Rostedtbf41a152008-10-04 02:00:59 -04003320 if (cpu_buffer->reader_page->read < rb_page_size(reader))
Steven Rostedtd7690412008-10-01 00:29:53 -04003321 goto out;
3322
3323 /* Never should we have an index greater than the size */
Steven Rostedt3e89c7b2008-11-11 15:28:41 -05003324 if (RB_WARN_ON(cpu_buffer,
3325 cpu_buffer->reader_page->read > rb_page_size(reader)))
3326 goto out;
Steven Rostedtd7690412008-10-01 00:29:53 -04003327
3328 /* check if we caught up to the tail */
3329 reader = NULL;
Steven Rostedtbf41a152008-10-04 02:00:59 -04003330 if (cpu_buffer->commit_page == cpu_buffer->reader_page)
Steven Rostedtd7690412008-10-01 00:29:53 -04003331 goto out;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003332
Steven Rostedta5fb8332012-06-28 13:35:04 -04003333 /* Don't bother swapping if the ring buffer is empty */
3334 if (rb_num_of_entries(cpu_buffer) == 0)
3335 goto out;
3336
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003337 /*
Steven Rostedtd7690412008-10-01 00:29:53 -04003338 * Reset the reader page to size zero.
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003339 */
Steven Rostedt77ae3652009-03-27 11:00:29 -04003340 local_set(&cpu_buffer->reader_page->write, 0);
3341 local_set(&cpu_buffer->reader_page->entries, 0);
3342 local_set(&cpu_buffer->reader_page->page->commit, 0);
Steven Rostedtff0ff842010-03-31 22:11:42 -04003343 cpu_buffer->reader_page->real_end = 0;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003344
Steven Rostedt77ae3652009-03-27 11:00:29 -04003345 spin:
3346 /*
3347 * Splice the empty reader page into the list around the head.
3348 */
3349 reader = rb_set_head_page(cpu_buffer);
Steven Rostedt54f7be52012-11-29 22:27:22 -05003350 if (!reader)
3351 goto out;
Steven Rostedt0e1ff5d2010-01-06 20:40:44 -05003352 cpu_buffer->reader_page->list.next = rb_list_head(reader->list.next);
Steven Rostedtd7690412008-10-01 00:29:53 -04003353 cpu_buffer->reader_page->list.prev = reader->list.prev;
Steven Rostedtbf41a152008-10-04 02:00:59 -04003354
Steven Rostedt3adc54f2009-03-30 15:32:01 -04003355 /*
3356 * cpu_buffer->pages just needs to point to the buffer, it
3357 * has no specific buffer page to point to. Lets move it out
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003358 * of our way so we don't accidentally swap it.
Steven Rostedt3adc54f2009-03-30 15:32:01 -04003359 */
3360 cpu_buffer->pages = reader->list.prev;
3361
Steven Rostedt77ae3652009-03-27 11:00:29 -04003362 /* The reader page will be pointing to the new head */
3363 rb_set_list_to_head(cpu_buffer, &cpu_buffer->reader_page->list);
Steven Rostedtd7690412008-10-01 00:29:53 -04003364
3365 /*
Steven Rostedt66a8cb92010-03-31 13:21:56 -04003366 * We want to make sure we read the overruns after we set up our
3367 * pointers to the next object. The writer side does a
3368 * cmpxchg to cross pages which acts as the mb on the writer
3369 * side. Note, the reader will constantly fail the swap
3370 * while the writer is updating the pointers, so this
3371 * guarantees that the overwrite recorded here is the one we
3372 * want to compare with the last_overrun.
3373 */
3374 smp_mb();
3375 overwrite = local_read(&(cpu_buffer->overrun));
3376
3377 /*
Steven Rostedt77ae3652009-03-27 11:00:29 -04003378 * Here's the tricky part.
3379 *
3380 * We need to move the pointer past the header page.
3381 * But we can only do that if a writer is not currently
3382 * moving it. The page before the header page has the
3383 * flag bit '1' set if it is pointing to the page we want.
3384 * but if the writer is in the process of moving it
3385 * than it will be '2' or already moved '0'.
Steven Rostedtd7690412008-10-01 00:29:53 -04003386 */
Steven Rostedtd7690412008-10-01 00:29:53 -04003387
Steven Rostedt77ae3652009-03-27 11:00:29 -04003388 ret = rb_head_page_replace(reader, cpu_buffer->reader_page);
3389
3390 /*
3391 * If we did not convert it, then we must try again.
3392 */
3393 if (!ret)
3394 goto spin;
3395
3396 /*
3397 * Yeah! We succeeded in replacing the page.
3398 *
3399 * Now make the new head point back to the reader page.
3400 */
David Sharp5ded3dc2010-01-06 17:12:07 -08003401 rb_list_head(reader->list.next)->prev = &cpu_buffer->reader_page->list;
Steven Rostedt77ae3652009-03-27 11:00:29 -04003402 rb_inc_page(cpu_buffer, &cpu_buffer->head_page);
Steven Rostedtd7690412008-10-01 00:29:53 -04003403
3404 /* Finally update the reader page to the new head */
3405 cpu_buffer->reader_page = reader;
3406 rb_reset_reader_page(cpu_buffer);
3407
Steven Rostedt66a8cb92010-03-31 13:21:56 -04003408 if (overwrite != cpu_buffer->last_overrun) {
3409 cpu_buffer->lost_events = overwrite - cpu_buffer->last_overrun;
3410 cpu_buffer->last_overrun = overwrite;
3411 }
3412
Steven Rostedtd7690412008-10-01 00:29:53 -04003413 goto again;
3414
3415 out:
Thomas Gleixner0199c4e2009-12-02 20:01:25 +01003416 arch_spin_unlock(&cpu_buffer->lock);
Steven Rostedt3e03fb72008-11-06 00:09:43 -05003417 local_irq_restore(flags);
Steven Rostedtd7690412008-10-01 00:29:53 -04003418
3419 return reader;
3420}
3421
3422static void rb_advance_reader(struct ring_buffer_per_cpu *cpu_buffer)
3423{
3424 struct ring_buffer_event *event;
3425 struct buffer_page *reader;
3426 unsigned length;
3427
3428 reader = rb_get_reader_page(cpu_buffer);
3429
3430 /* This function should not be called when buffer is empty */
Steven Rostedt3e89c7b2008-11-11 15:28:41 -05003431 if (RB_WARN_ON(cpu_buffer, !reader))
3432 return;
Steven Rostedtd7690412008-10-01 00:29:53 -04003433
3434 event = rb_reader_event(cpu_buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003435
Steven Rostedta1863c22009-09-03 10:23:58 -04003436 if (event->type_len <= RINGBUF_TYPE_DATA_TYPE_LEN_MAX)
Steven Rostedte4906ef2009-04-30 20:49:44 -04003437 cpu_buffer->read++;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003438
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003439 rb_update_read_stamp(cpu_buffer, event);
3440
Steven Rostedtd7690412008-10-01 00:29:53 -04003441 length = rb_event_length(event);
Steven Rostedt6f807ac2008-10-04 02:00:58 -04003442 cpu_buffer->reader_page->read += length;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003443}
3444
3445static void rb_advance_iter(struct ring_buffer_iter *iter)
3446{
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003447 struct ring_buffer_per_cpu *cpu_buffer;
3448 struct ring_buffer_event *event;
3449 unsigned length;
3450
3451 cpu_buffer = iter->cpu_buffer;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003452
3453 /*
3454 * Check if we are at the end of the buffer.
3455 */
Steven Rostedtbf41a152008-10-04 02:00:59 -04003456 if (iter->head >= rb_page_size(iter->head_page)) {
Steven Rostedtea05b572009-06-03 09:30:10 -04003457 /* discarded commits can make the page empty */
3458 if (iter->head_page == cpu_buffer->commit_page)
Steven Rostedt3e89c7b2008-11-11 15:28:41 -05003459 return;
Steven Rostedtd7690412008-10-01 00:29:53 -04003460 rb_inc_iter(iter);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003461 return;
3462 }
3463
3464 event = rb_iter_head_event(iter);
3465
3466 length = rb_event_length(event);
3467
3468 /*
3469 * This should not be called to advance the header if we are
3470 * at the tail of the buffer.
3471 */
Steven Rostedt3e89c7b2008-11-11 15:28:41 -05003472 if (RB_WARN_ON(cpu_buffer,
Steven Rostedtf536aaf2008-11-10 23:07:30 -05003473 (iter->head_page == cpu_buffer->commit_page) &&
Steven Rostedt3e89c7b2008-11-11 15:28:41 -05003474 (iter->head + length > rb_commit_index(cpu_buffer))))
3475 return;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003476
3477 rb_update_iter_read_stamp(iter, event);
3478
3479 iter->head += length;
3480
3481 /* check for end of page padding */
Steven Rostedtbf41a152008-10-04 02:00:59 -04003482 if ((iter->head >= rb_page_size(iter->head_page)) &&
3483 (iter->head_page != cpu_buffer->commit_page))
Steven Rostedt771e0382012-11-30 10:41:57 -05003484 rb_inc_iter(iter);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003485}
3486
Steven Rostedt66a8cb92010-03-31 13:21:56 -04003487static int rb_lost_events(struct ring_buffer_per_cpu *cpu_buffer)
3488{
3489 return cpu_buffer->lost_events;
3490}
3491
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003492static struct ring_buffer_event *
Steven Rostedt66a8cb92010-03-31 13:21:56 -04003493rb_buffer_peek(struct ring_buffer_per_cpu *cpu_buffer, u64 *ts,
3494 unsigned long *lost_events)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003495{
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003496 struct ring_buffer_event *event;
Steven Rostedtd7690412008-10-01 00:29:53 -04003497 struct buffer_page *reader;
Steven Rostedt818e3dd2008-10-31 09:58:35 -04003498 int nr_loops = 0;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003499
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003500 again:
Steven Rostedt818e3dd2008-10-31 09:58:35 -04003501 /*
Steven Rostedt69d1b832010-10-07 18:18:05 -04003502 * We repeat when a time extend is encountered.
3503 * Since the time extend is always attached to a data event,
3504 * we should never loop more than once.
3505 * (We never hit the following condition more than twice).
Steven Rostedt818e3dd2008-10-31 09:58:35 -04003506 */
Steven Rostedt69d1b832010-10-07 18:18:05 -04003507 if (RB_WARN_ON(cpu_buffer, ++nr_loops > 2))
Steven Rostedt818e3dd2008-10-31 09:58:35 -04003508 return NULL;
Steven Rostedt818e3dd2008-10-31 09:58:35 -04003509
Steven Rostedtd7690412008-10-01 00:29:53 -04003510 reader = rb_get_reader_page(cpu_buffer);
3511 if (!reader)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003512 return NULL;
3513
Steven Rostedtd7690412008-10-01 00:29:53 -04003514 event = rb_reader_event(cpu_buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003515
Lai Jiangshan334d4162009-04-24 11:27:05 +08003516 switch (event->type_len) {
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003517 case RINGBUF_TYPE_PADDING:
Tom Zanussi2d622712009-03-22 03:30:49 -05003518 if (rb_null_event(event))
3519 RB_WARN_ON(cpu_buffer, 1);
3520 /*
3521 * Because the writer could be discarding every
3522 * event it creates (which would probably be bad)
3523 * if we were to go back to "again" then we may never
3524 * catch up, and will trigger the warn on, or lock
3525 * the box. Return the padding, and we will release
3526 * the current locks, and try again.
3527 */
Tom Zanussi2d622712009-03-22 03:30:49 -05003528 return event;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003529
3530 case RINGBUF_TYPE_TIME_EXTEND:
3531 /* Internal data, OK to advance */
Steven Rostedtd7690412008-10-01 00:29:53 -04003532 rb_advance_reader(cpu_buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003533 goto again;
3534
3535 case RINGBUF_TYPE_TIME_STAMP:
3536 /* FIXME: not implemented */
Steven Rostedtd7690412008-10-01 00:29:53 -04003537 rb_advance_reader(cpu_buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003538 goto again;
3539
3540 case RINGBUF_TYPE_DATA:
3541 if (ts) {
3542 *ts = cpu_buffer->read_stamp + event->time_delta;
Robert Richterd8eeb2d2009-07-31 14:58:04 +02003543 ring_buffer_normalize_time_stamp(cpu_buffer->buffer,
Steven Rostedt37886f62009-03-17 17:22:06 -04003544 cpu_buffer->cpu, ts);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003545 }
Steven Rostedt66a8cb92010-03-31 13:21:56 -04003546 if (lost_events)
3547 *lost_events = rb_lost_events(cpu_buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003548 return event;
3549
3550 default:
3551 BUG();
3552 }
3553
3554 return NULL;
3555}
Robert Richterc4f50182008-12-11 16:49:22 +01003556EXPORT_SYMBOL_GPL(ring_buffer_peek);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003557
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003558static struct ring_buffer_event *
3559rb_iter_peek(struct ring_buffer_iter *iter, u64 *ts)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003560{
3561 struct ring_buffer *buffer;
3562 struct ring_buffer_per_cpu *cpu_buffer;
3563 struct ring_buffer_event *event;
Steven Rostedt818e3dd2008-10-31 09:58:35 -04003564 int nr_loops = 0;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003565
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003566 cpu_buffer = iter->cpu_buffer;
3567 buffer = cpu_buffer->buffer;
3568
Steven Rostedt492a74f2010-01-25 15:17:47 -05003569 /*
3570 * Check if someone performed a consuming read to
3571 * the buffer. A consuming read invalidates the iterator
3572 * and we need to reset the iterator in this case.
3573 */
3574 if (unlikely(iter->cache_read != cpu_buffer->read ||
3575 iter->cache_reader_page != cpu_buffer->reader_page))
3576 rb_iter_reset(iter);
3577
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003578 again:
Steven Rostedt3c05d742010-01-26 16:14:08 -05003579 if (ring_buffer_iter_empty(iter))
3580 return NULL;
3581
Steven Rostedt818e3dd2008-10-31 09:58:35 -04003582 /*
Steven Rostedt69d1b832010-10-07 18:18:05 -04003583 * We repeat when a time extend is encountered.
3584 * Since the time extend is always attached to a data event,
3585 * we should never loop more than once.
3586 * (We never hit the following condition more than twice).
Steven Rostedt818e3dd2008-10-31 09:58:35 -04003587 */
Steven Rostedt69d1b832010-10-07 18:18:05 -04003588 if (RB_WARN_ON(cpu_buffer, ++nr_loops > 2))
Steven Rostedt818e3dd2008-10-31 09:58:35 -04003589 return NULL;
Steven Rostedt818e3dd2008-10-31 09:58:35 -04003590
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003591 if (rb_per_cpu_empty(cpu_buffer))
3592 return NULL;
3593
Steven Rostedt3c05d742010-01-26 16:14:08 -05003594 if (iter->head >= local_read(&iter->head_page->page->commit)) {
3595 rb_inc_iter(iter);
3596 goto again;
3597 }
3598
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003599 event = rb_iter_head_event(iter);
3600
Lai Jiangshan334d4162009-04-24 11:27:05 +08003601 switch (event->type_len) {
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003602 case RINGBUF_TYPE_PADDING:
Tom Zanussi2d622712009-03-22 03:30:49 -05003603 if (rb_null_event(event)) {
3604 rb_inc_iter(iter);
3605 goto again;
3606 }
3607 rb_advance_iter(iter);
3608 return event;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003609
3610 case RINGBUF_TYPE_TIME_EXTEND:
3611 /* Internal data, OK to advance */
3612 rb_advance_iter(iter);
3613 goto again;
3614
3615 case RINGBUF_TYPE_TIME_STAMP:
3616 /* FIXME: not implemented */
3617 rb_advance_iter(iter);
3618 goto again;
3619
3620 case RINGBUF_TYPE_DATA:
3621 if (ts) {
3622 *ts = iter->read_stamp + event->time_delta;
Steven Rostedt37886f62009-03-17 17:22:06 -04003623 ring_buffer_normalize_time_stamp(buffer,
3624 cpu_buffer->cpu, ts);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003625 }
3626 return event;
3627
3628 default:
3629 BUG();
3630 }
3631
3632 return NULL;
3633}
Robert Richterc4f50182008-12-11 16:49:22 +01003634EXPORT_SYMBOL_GPL(ring_buffer_iter_peek);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003635
Steven Rostedt8d707e82009-06-16 21:22:48 -04003636static inline int rb_ok_to_lock(void)
3637{
3638 /*
3639 * If an NMI die dumps out the content of the ring buffer
3640 * do not grab locks. We also permanently disable the ring
3641 * buffer too. A one time deal is all you get from reading
3642 * the ring buffer from an NMI.
3643 */
Steven Rostedt464e85e2009-08-05 15:26:37 -04003644 if (likely(!in_nmi()))
Steven Rostedt8d707e82009-06-16 21:22:48 -04003645 return 1;
3646
3647 tracing_off_permanent();
3648 return 0;
3649}
3650
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003651/**
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003652 * ring_buffer_peek - peek at the next event to be read
3653 * @buffer: The ring buffer to read
3654 * @cpu: The cpu to peak at
3655 * @ts: The timestamp counter of this event.
Steven Rostedt66a8cb92010-03-31 13:21:56 -04003656 * @lost_events: a variable to store if events were lost (may be NULL)
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003657 *
3658 * This will return the event that will be read next, but does
3659 * not consume the data.
3660 */
3661struct ring_buffer_event *
Steven Rostedt66a8cb92010-03-31 13:21:56 -04003662ring_buffer_peek(struct ring_buffer *buffer, int cpu, u64 *ts,
3663 unsigned long *lost_events)
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003664{
3665 struct ring_buffer_per_cpu *cpu_buffer = buffer->buffers[cpu];
Steven Rostedt8aabee52009-03-12 13:13:49 -04003666 struct ring_buffer_event *event;
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003667 unsigned long flags;
Steven Rostedt8d707e82009-06-16 21:22:48 -04003668 int dolock;
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003669
Steven Rostedt554f7862009-03-11 22:00:13 -04003670 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedt8aabee52009-03-12 13:13:49 -04003671 return NULL;
Steven Rostedt554f7862009-03-11 22:00:13 -04003672
Steven Rostedt8d707e82009-06-16 21:22:48 -04003673 dolock = rb_ok_to_lock();
Tom Zanussi2d622712009-03-22 03:30:49 -05003674 again:
Steven Rostedt8d707e82009-06-16 21:22:48 -04003675 local_irq_save(flags);
3676 if (dolock)
Thomas Gleixner5389f6f2009-07-25 17:13:33 +02003677 raw_spin_lock(&cpu_buffer->reader_lock);
Steven Rostedt66a8cb92010-03-31 13:21:56 -04003678 event = rb_buffer_peek(cpu_buffer, ts, lost_events);
Robert Richter469535a2009-07-30 19:19:18 +02003679 if (event && event->type_len == RINGBUF_TYPE_PADDING)
3680 rb_advance_reader(cpu_buffer);
Steven Rostedt8d707e82009-06-16 21:22:48 -04003681 if (dolock)
Thomas Gleixner5389f6f2009-07-25 17:13:33 +02003682 raw_spin_unlock(&cpu_buffer->reader_lock);
Steven Rostedt8d707e82009-06-16 21:22:48 -04003683 local_irq_restore(flags);
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003684
Steven Rostedt1b959e12009-09-03 10:12:13 -04003685 if (event && event->type_len == RINGBUF_TYPE_PADDING)
Tom Zanussi2d622712009-03-22 03:30:49 -05003686 goto again;
Tom Zanussi2d622712009-03-22 03:30:49 -05003687
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003688 return event;
3689}
3690
3691/**
3692 * ring_buffer_iter_peek - peek at the next event to be read
3693 * @iter: The ring buffer iterator
3694 * @ts: The timestamp counter of this event.
3695 *
3696 * This will return the event that will be read next, but does
3697 * not increment the iterator.
3698 */
3699struct ring_buffer_event *
3700ring_buffer_iter_peek(struct ring_buffer_iter *iter, u64 *ts)
3701{
3702 struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
3703 struct ring_buffer_event *event;
3704 unsigned long flags;
3705
Tom Zanussi2d622712009-03-22 03:30:49 -05003706 again:
Thomas Gleixner5389f6f2009-07-25 17:13:33 +02003707 raw_spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003708 event = rb_iter_peek(iter, ts);
Thomas Gleixner5389f6f2009-07-25 17:13:33 +02003709 raw_spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003710
Steven Rostedt1b959e12009-09-03 10:12:13 -04003711 if (event && event->type_len == RINGBUF_TYPE_PADDING)
Tom Zanussi2d622712009-03-22 03:30:49 -05003712 goto again;
Tom Zanussi2d622712009-03-22 03:30:49 -05003713
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003714 return event;
3715}
3716
3717/**
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003718 * ring_buffer_consume - return an event and consume it
3719 * @buffer: The ring buffer to get the next event from
Steven Rostedt66a8cb92010-03-31 13:21:56 -04003720 * @cpu: the cpu to read the buffer from
3721 * @ts: a variable to store the timestamp (may be NULL)
3722 * @lost_events: a variable to store if events were lost (may be NULL)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003723 *
3724 * Returns the next event in the ring buffer, and that event is consumed.
3725 * Meaning, that sequential reads will keep returning a different event,
3726 * and eventually empty the ring buffer if the producer is slower.
3727 */
3728struct ring_buffer_event *
Steven Rostedt66a8cb92010-03-31 13:21:56 -04003729ring_buffer_consume(struct ring_buffer *buffer, int cpu, u64 *ts,
3730 unsigned long *lost_events)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003731{
Steven Rostedt554f7862009-03-11 22:00:13 -04003732 struct ring_buffer_per_cpu *cpu_buffer;
3733 struct ring_buffer_event *event = NULL;
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003734 unsigned long flags;
Steven Rostedt8d707e82009-06-16 21:22:48 -04003735 int dolock;
3736
3737 dolock = rb_ok_to_lock();
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003738
Tom Zanussi2d622712009-03-22 03:30:49 -05003739 again:
Steven Rostedt554f7862009-03-11 22:00:13 -04003740 /* might be called in atomic */
3741 preempt_disable();
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003742
Steven Rostedt554f7862009-03-11 22:00:13 -04003743 if (!cpumask_test_cpu(cpu, buffer->cpumask))
3744 goto out;
3745
3746 cpu_buffer = buffer->buffers[cpu];
Steven Rostedt8d707e82009-06-16 21:22:48 -04003747 local_irq_save(flags);
3748 if (dolock)
Thomas Gleixner5389f6f2009-07-25 17:13:33 +02003749 raw_spin_lock(&cpu_buffer->reader_lock);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003750
Steven Rostedt66a8cb92010-03-31 13:21:56 -04003751 event = rb_buffer_peek(cpu_buffer, ts, lost_events);
3752 if (event) {
3753 cpu_buffer->lost_events = 0;
Robert Richter469535a2009-07-30 19:19:18 +02003754 rb_advance_reader(cpu_buffer);
Steven Rostedt66a8cb92010-03-31 13:21:56 -04003755 }
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003756
Steven Rostedt8d707e82009-06-16 21:22:48 -04003757 if (dolock)
Thomas Gleixner5389f6f2009-07-25 17:13:33 +02003758 raw_spin_unlock(&cpu_buffer->reader_lock);
Steven Rostedt8d707e82009-06-16 21:22:48 -04003759 local_irq_restore(flags);
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003760
Steven Rostedt554f7862009-03-11 22:00:13 -04003761 out:
3762 preempt_enable();
3763
Steven Rostedt1b959e12009-09-03 10:12:13 -04003764 if (event && event->type_len == RINGBUF_TYPE_PADDING)
Tom Zanussi2d622712009-03-22 03:30:49 -05003765 goto again;
Tom Zanussi2d622712009-03-22 03:30:49 -05003766
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003767 return event;
3768}
Robert Richterc4f50182008-12-11 16:49:22 +01003769EXPORT_SYMBOL_GPL(ring_buffer_consume);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003770
3771/**
David Miller72c9ddf2010-04-20 15:47:11 -07003772 * ring_buffer_read_prepare - Prepare for a non consuming read of the buffer
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003773 * @buffer: The ring buffer to read from
3774 * @cpu: The cpu buffer to iterate over
3775 *
David Miller72c9ddf2010-04-20 15:47:11 -07003776 * This performs the initial preparations necessary to iterate
3777 * through the buffer. Memory is allocated, buffer recording
3778 * is disabled, and the iterator pointer is returned to the caller.
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003779 *
David Miller72c9ddf2010-04-20 15:47:11 -07003780 * Disabling buffer recordng prevents the reading from being
3781 * corrupted. This is not a consuming read, so a producer is not
3782 * expected.
3783 *
3784 * After a sequence of ring_buffer_read_prepare calls, the user is
3785 * expected to make at least one call to ring_buffer_prepare_sync.
3786 * Afterwards, ring_buffer_read_start is invoked to get things going
3787 * for real.
3788 *
3789 * This overall must be paired with ring_buffer_finish.
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003790 */
3791struct ring_buffer_iter *
David Miller72c9ddf2010-04-20 15:47:11 -07003792ring_buffer_read_prepare(struct ring_buffer *buffer, int cpu)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003793{
3794 struct ring_buffer_per_cpu *cpu_buffer;
Steven Rostedt8aabee52009-03-12 13:13:49 -04003795 struct ring_buffer_iter *iter;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003796
Rusty Russell9e01c1b2009-01-01 10:12:22 +10303797 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedt8aabee52009-03-12 13:13:49 -04003798 return NULL;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003799
3800 iter = kmalloc(sizeof(*iter), GFP_KERNEL);
3801 if (!iter)
Steven Rostedt8aabee52009-03-12 13:13:49 -04003802 return NULL;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003803
3804 cpu_buffer = buffer->buffers[cpu];
3805
3806 iter->cpu_buffer = cpu_buffer;
3807
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07003808 atomic_inc(&buffer->resize_disabled);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003809 atomic_inc(&cpu_buffer->record_disabled);
David Miller72c9ddf2010-04-20 15:47:11 -07003810
3811 return iter;
3812}
3813EXPORT_SYMBOL_GPL(ring_buffer_read_prepare);
3814
3815/**
3816 * ring_buffer_read_prepare_sync - Synchronize a set of prepare calls
3817 *
3818 * All previously invoked ring_buffer_read_prepare calls to prepare
3819 * iterators will be synchronized. Afterwards, read_buffer_read_start
3820 * calls on those iterators are allowed.
3821 */
3822void
3823ring_buffer_read_prepare_sync(void)
3824{
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003825 synchronize_sched();
David Miller72c9ddf2010-04-20 15:47:11 -07003826}
3827EXPORT_SYMBOL_GPL(ring_buffer_read_prepare_sync);
3828
3829/**
3830 * ring_buffer_read_start - start a non consuming read of the buffer
3831 * @iter: The iterator returned by ring_buffer_read_prepare
3832 *
3833 * This finalizes the startup of an iteration through the buffer.
3834 * The iterator comes from a call to ring_buffer_read_prepare and
3835 * an intervening ring_buffer_read_prepare_sync must have been
3836 * performed.
3837 *
3838 * Must be paired with ring_buffer_finish.
3839 */
3840void
3841ring_buffer_read_start(struct ring_buffer_iter *iter)
3842{
3843 struct ring_buffer_per_cpu *cpu_buffer;
3844 unsigned long flags;
3845
3846 if (!iter)
3847 return;
3848
3849 cpu_buffer = iter->cpu_buffer;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003850
Thomas Gleixner5389f6f2009-07-25 17:13:33 +02003851 raw_spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
Thomas Gleixner0199c4e2009-12-02 20:01:25 +01003852 arch_spin_lock(&cpu_buffer->lock);
Steven Rostedt642edba2008-11-12 00:01:26 -05003853 rb_iter_reset(iter);
Thomas Gleixner0199c4e2009-12-02 20:01:25 +01003854 arch_spin_unlock(&cpu_buffer->lock);
Thomas Gleixner5389f6f2009-07-25 17:13:33 +02003855 raw_spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003856}
Robert Richterc4f50182008-12-11 16:49:22 +01003857EXPORT_SYMBOL_GPL(ring_buffer_read_start);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003858
3859/**
3860 * ring_buffer_finish - finish reading the iterator of the buffer
3861 * @iter: The iterator retrieved by ring_buffer_start
3862 *
3863 * This re-enables the recording to the buffer, and frees the
3864 * iterator.
3865 */
3866void
3867ring_buffer_read_finish(struct ring_buffer_iter *iter)
3868{
3869 struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
Steven Rostedt9366c1b2012-11-29 22:31:16 -05003870 unsigned long flags;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003871
Steven Rostedt659f4512012-05-14 17:02:33 -04003872 /*
3873 * Ring buffer is disabled from recording, here's a good place
Steven Rostedt9366c1b2012-11-29 22:31:16 -05003874 * to check the integrity of the ring buffer.
3875 * Must prevent readers from trying to read, as the check
3876 * clears the HEAD page and readers require it.
Steven Rostedt659f4512012-05-14 17:02:33 -04003877 */
Steven Rostedt9366c1b2012-11-29 22:31:16 -05003878 raw_spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
Steven Rostedt659f4512012-05-14 17:02:33 -04003879 rb_check_pages(cpu_buffer);
Steven Rostedt9366c1b2012-11-29 22:31:16 -05003880 raw_spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
Steven Rostedt659f4512012-05-14 17:02:33 -04003881
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003882 atomic_dec(&cpu_buffer->record_disabled);
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07003883 atomic_dec(&cpu_buffer->buffer->resize_disabled);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003884 kfree(iter);
3885}
Robert Richterc4f50182008-12-11 16:49:22 +01003886EXPORT_SYMBOL_GPL(ring_buffer_read_finish);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003887
3888/**
3889 * ring_buffer_read - read the next item in the ring buffer by the iterator
3890 * @iter: The ring buffer iterator
3891 * @ts: The time stamp of the event read.
3892 *
3893 * This reads the next event in the ring buffer and increments the iterator.
3894 */
3895struct ring_buffer_event *
3896ring_buffer_read(struct ring_buffer_iter *iter, u64 *ts)
3897{
3898 struct ring_buffer_event *event;
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003899 struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;
3900 unsigned long flags;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003901
Thomas Gleixner5389f6f2009-07-25 17:13:33 +02003902 raw_spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
Steven Rostedt7e9391c2009-09-03 10:02:09 -04003903 again:
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003904 event = rb_iter_peek(iter, ts);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003905 if (!event)
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003906 goto out;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003907
Steven Rostedt7e9391c2009-09-03 10:02:09 -04003908 if (event->type_len == RINGBUF_TYPE_PADDING)
3909 goto again;
3910
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003911 rb_advance_iter(iter);
Steven Rostedtf83c9d02008-11-11 18:47:44 +01003912 out:
Thomas Gleixner5389f6f2009-07-25 17:13:33 +02003913 raw_spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003914
3915 return event;
3916}
Robert Richterc4f50182008-12-11 16:49:22 +01003917EXPORT_SYMBOL_GPL(ring_buffer_read);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003918
3919/**
3920 * ring_buffer_size - return the size of the ring buffer (in bytes)
3921 * @buffer: The ring buffer.
3922 */
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08003923unsigned long ring_buffer_size(struct ring_buffer *buffer, int cpu)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003924{
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08003925 /*
3926 * Earlier, this method returned
3927 * BUF_PAGE_SIZE * buffer->nr_pages
3928 * Since the nr_pages field is now removed, we have converted this to
3929 * return the per cpu buffer value.
3930 */
3931 if (!cpumask_test_cpu(cpu, buffer->cpumask))
3932 return 0;
3933
3934 return BUF_PAGE_SIZE * buffer->buffers[cpu]->nr_pages;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003935}
Robert Richterc4f50182008-12-11 16:49:22 +01003936EXPORT_SYMBOL_GPL(ring_buffer_size);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003937
3938static void
3939rb_reset_cpu(struct ring_buffer_per_cpu *cpu_buffer)
3940{
Steven Rostedt77ae3652009-03-27 11:00:29 -04003941 rb_head_page_deactivate(cpu_buffer);
3942
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003943 cpu_buffer->head_page
Steven Rostedt3adc54f2009-03-30 15:32:01 -04003944 = list_entry(cpu_buffer->pages, struct buffer_page, list);
Steven Rostedtbf41a152008-10-04 02:00:59 -04003945 local_set(&cpu_buffer->head_page->write, 0);
Steven Rostedt778c55d2009-05-01 18:44:45 -04003946 local_set(&cpu_buffer->head_page->entries, 0);
Steven Rostedtabc9b562008-12-02 15:34:06 -05003947 local_set(&cpu_buffer->head_page->page->commit, 0);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003948
Steven Rostedt6f807ac2008-10-04 02:00:58 -04003949 cpu_buffer->head_page->read = 0;
Steven Rostedtbf41a152008-10-04 02:00:59 -04003950
3951 cpu_buffer->tail_page = cpu_buffer->head_page;
3952 cpu_buffer->commit_page = cpu_buffer->head_page;
3953
3954 INIT_LIST_HEAD(&cpu_buffer->reader_page->list);
Vaibhav Nagarnaik5040b4b2012-05-03 18:59:51 -07003955 INIT_LIST_HEAD(&cpu_buffer->new_pages);
Steven Rostedtbf41a152008-10-04 02:00:59 -04003956 local_set(&cpu_buffer->reader_page->write, 0);
Steven Rostedt778c55d2009-05-01 18:44:45 -04003957 local_set(&cpu_buffer->reader_page->entries, 0);
Steven Rostedtabc9b562008-12-02 15:34:06 -05003958 local_set(&cpu_buffer->reader_page->page->commit, 0);
Steven Rostedt6f807ac2008-10-04 02:00:58 -04003959 cpu_buffer->reader_page->read = 0;
Steven Rostedtd7690412008-10-01 00:29:53 -04003960
Vaibhav Nagarnaikc64e1482011-08-16 14:46:16 -07003961 local_set(&cpu_buffer->entries_bytes, 0);
Steven Rostedt77ae3652009-03-27 11:00:29 -04003962 local_set(&cpu_buffer->overrun, 0);
Slava Pestov884bfe82011-07-15 14:23:58 -07003963 local_set(&cpu_buffer->commit_overrun, 0);
3964 local_set(&cpu_buffer->dropped_events, 0);
Steven Rostedte4906ef2009-04-30 20:49:44 -04003965 local_set(&cpu_buffer->entries, 0);
Steven Rostedtfa743952009-06-16 12:37:57 -04003966 local_set(&cpu_buffer->committing, 0);
3967 local_set(&cpu_buffer->commits, 0);
Steven Rostedt77ae3652009-03-27 11:00:29 -04003968 cpu_buffer->read = 0;
Vaibhav Nagarnaikc64e1482011-08-16 14:46:16 -07003969 cpu_buffer->read_bytes = 0;
Steven Rostedt69507c02009-01-21 18:45:57 -05003970
3971 cpu_buffer->write_stamp = 0;
3972 cpu_buffer->read_stamp = 0;
Steven Rostedt77ae3652009-03-27 11:00:29 -04003973
Steven Rostedt66a8cb92010-03-31 13:21:56 -04003974 cpu_buffer->lost_events = 0;
3975 cpu_buffer->last_overrun = 0;
3976
Steven Rostedt77ae3652009-03-27 11:00:29 -04003977 rb_head_page_activate(cpu_buffer);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003978}
3979
3980/**
3981 * ring_buffer_reset_cpu - reset a ring buffer per CPU buffer
3982 * @buffer: The ring buffer to reset a per cpu buffer of
3983 * @cpu: The CPU buffer to be reset
3984 */
3985void ring_buffer_reset_cpu(struct ring_buffer *buffer, int cpu)
3986{
3987 struct ring_buffer_per_cpu *cpu_buffer = buffer->buffers[cpu];
3988 unsigned long flags;
3989
Rusty Russell9e01c1b2009-01-01 10:12:22 +10303990 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedt8aabee52009-03-12 13:13:49 -04003991 return;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04003992
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07003993 atomic_inc(&buffer->resize_disabled);
Steven Rostedt41ede232009-05-01 20:26:54 -04003994 atomic_inc(&cpu_buffer->record_disabled);
3995
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07003996 /* Make sure all commits have finished */
3997 synchronize_sched();
3998
Thomas Gleixner5389f6f2009-07-25 17:13:33 +02003999 raw_spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
Steven Rostedtf83c9d02008-11-11 18:47:44 +01004000
Steven Rostedt41b6a952009-09-02 09:59:48 -04004001 if (RB_WARN_ON(cpu_buffer, local_read(&cpu_buffer->committing)))
4002 goto out;
4003
Thomas Gleixner0199c4e2009-12-02 20:01:25 +01004004 arch_spin_lock(&cpu_buffer->lock);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004005
4006 rb_reset_cpu(cpu_buffer);
4007
Thomas Gleixner0199c4e2009-12-02 20:01:25 +01004008 arch_spin_unlock(&cpu_buffer->lock);
Steven Rostedtf83c9d02008-11-11 18:47:44 +01004009
Steven Rostedt41b6a952009-09-02 09:59:48 -04004010 out:
Thomas Gleixner5389f6f2009-07-25 17:13:33 +02004011 raw_spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
Steven Rostedt41ede232009-05-01 20:26:54 -04004012
4013 atomic_dec(&cpu_buffer->record_disabled);
Vaibhav Nagarnaik83f40312012-05-03 18:59:50 -07004014 atomic_dec(&buffer->resize_disabled);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004015}
Robert Richterc4f50182008-12-11 16:49:22 +01004016EXPORT_SYMBOL_GPL(ring_buffer_reset_cpu);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004017
4018/**
4019 * ring_buffer_reset - reset a ring buffer
4020 * @buffer: The ring buffer to reset all cpu buffers
4021 */
4022void ring_buffer_reset(struct ring_buffer *buffer)
4023{
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004024 int cpu;
4025
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004026 for_each_buffer_cpu(buffer, cpu)
Steven Rostedtd7690412008-10-01 00:29:53 -04004027 ring_buffer_reset_cpu(buffer, cpu);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004028}
Robert Richterc4f50182008-12-11 16:49:22 +01004029EXPORT_SYMBOL_GPL(ring_buffer_reset);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004030
4031/**
4032 * rind_buffer_empty - is the ring buffer empty?
4033 * @buffer: The ring buffer to test
4034 */
4035int ring_buffer_empty(struct ring_buffer *buffer)
4036{
4037 struct ring_buffer_per_cpu *cpu_buffer;
Steven Rostedtd4788202009-06-17 00:39:43 -04004038 unsigned long flags;
Steven Rostedt8d707e82009-06-16 21:22:48 -04004039 int dolock;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004040 int cpu;
Steven Rostedtd4788202009-06-17 00:39:43 -04004041 int ret;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004042
Steven Rostedt8d707e82009-06-16 21:22:48 -04004043 dolock = rb_ok_to_lock();
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004044
4045 /* yes this is racy, but if you don't like the race, lock the buffer */
4046 for_each_buffer_cpu(buffer, cpu) {
4047 cpu_buffer = buffer->buffers[cpu];
Steven Rostedt8d707e82009-06-16 21:22:48 -04004048 local_irq_save(flags);
4049 if (dolock)
Thomas Gleixner5389f6f2009-07-25 17:13:33 +02004050 raw_spin_lock(&cpu_buffer->reader_lock);
Steven Rostedtd4788202009-06-17 00:39:43 -04004051 ret = rb_per_cpu_empty(cpu_buffer);
Steven Rostedt8d707e82009-06-16 21:22:48 -04004052 if (dolock)
Thomas Gleixner5389f6f2009-07-25 17:13:33 +02004053 raw_spin_unlock(&cpu_buffer->reader_lock);
Steven Rostedt8d707e82009-06-16 21:22:48 -04004054 local_irq_restore(flags);
4055
Steven Rostedtd4788202009-06-17 00:39:43 -04004056 if (!ret)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004057 return 0;
4058 }
Steven Rostedt554f7862009-03-11 22:00:13 -04004059
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004060 return 1;
4061}
Robert Richterc4f50182008-12-11 16:49:22 +01004062EXPORT_SYMBOL_GPL(ring_buffer_empty);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004063
4064/**
4065 * ring_buffer_empty_cpu - is a cpu buffer of a ring buffer empty?
4066 * @buffer: The ring buffer
4067 * @cpu: The CPU buffer to test
4068 */
4069int ring_buffer_empty_cpu(struct ring_buffer *buffer, int cpu)
4070{
4071 struct ring_buffer_per_cpu *cpu_buffer;
Steven Rostedtd4788202009-06-17 00:39:43 -04004072 unsigned long flags;
Steven Rostedt8d707e82009-06-16 21:22:48 -04004073 int dolock;
Steven Rostedt8aabee52009-03-12 13:13:49 -04004074 int ret;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004075
Rusty Russell9e01c1b2009-01-01 10:12:22 +10304076 if (!cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedt8aabee52009-03-12 13:13:49 -04004077 return 1;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004078
Steven Rostedt8d707e82009-06-16 21:22:48 -04004079 dolock = rb_ok_to_lock();
Steven Rostedt554f7862009-03-11 22:00:13 -04004080
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004081 cpu_buffer = buffer->buffers[cpu];
Steven Rostedt8d707e82009-06-16 21:22:48 -04004082 local_irq_save(flags);
4083 if (dolock)
Thomas Gleixner5389f6f2009-07-25 17:13:33 +02004084 raw_spin_lock(&cpu_buffer->reader_lock);
Steven Rostedt554f7862009-03-11 22:00:13 -04004085 ret = rb_per_cpu_empty(cpu_buffer);
Steven Rostedt8d707e82009-06-16 21:22:48 -04004086 if (dolock)
Thomas Gleixner5389f6f2009-07-25 17:13:33 +02004087 raw_spin_unlock(&cpu_buffer->reader_lock);
Steven Rostedt8d707e82009-06-16 21:22:48 -04004088 local_irq_restore(flags);
Steven Rostedt554f7862009-03-11 22:00:13 -04004089
4090 return ret;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004091}
Robert Richterc4f50182008-12-11 16:49:22 +01004092EXPORT_SYMBOL_GPL(ring_buffer_empty_cpu);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004093
Steven Rostedt85bac322009-09-04 14:24:40 -04004094#ifdef CONFIG_RING_BUFFER_ALLOW_SWAP
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004095/**
4096 * ring_buffer_swap_cpu - swap a CPU buffer between two ring buffers
4097 * @buffer_a: One buffer to swap with
4098 * @buffer_b: The other buffer to swap with
4099 *
4100 * This function is useful for tracers that want to take a "snapshot"
4101 * of a CPU buffer and has another back up buffer lying around.
4102 * it is expected that the tracer handles the cpu buffer not being
4103 * used at the moment.
4104 */
4105int ring_buffer_swap_cpu(struct ring_buffer *buffer_a,
4106 struct ring_buffer *buffer_b, int cpu)
4107{
4108 struct ring_buffer_per_cpu *cpu_buffer_a;
4109 struct ring_buffer_per_cpu *cpu_buffer_b;
Steven Rostedt554f7862009-03-11 22:00:13 -04004110 int ret = -EINVAL;
4111
Rusty Russell9e01c1b2009-01-01 10:12:22 +10304112 if (!cpumask_test_cpu(cpu, buffer_a->cpumask) ||
4113 !cpumask_test_cpu(cpu, buffer_b->cpumask))
Steven Rostedt554f7862009-03-11 22:00:13 -04004114 goto out;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004115
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08004116 cpu_buffer_a = buffer_a->buffers[cpu];
4117 cpu_buffer_b = buffer_b->buffers[cpu];
4118
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004119 /* At least make sure the two buffers are somewhat the same */
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08004120 if (cpu_buffer_a->nr_pages != cpu_buffer_b->nr_pages)
Steven Rostedt554f7862009-03-11 22:00:13 -04004121 goto out;
4122
4123 ret = -EAGAIN;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004124
Steven Rostedt97b17ef2009-01-21 15:24:56 -05004125 if (ring_buffer_flags != RB_BUFFERS_ON)
Steven Rostedt554f7862009-03-11 22:00:13 -04004126 goto out;
Steven Rostedt97b17ef2009-01-21 15:24:56 -05004127
4128 if (atomic_read(&buffer_a->record_disabled))
Steven Rostedt554f7862009-03-11 22:00:13 -04004129 goto out;
Steven Rostedt97b17ef2009-01-21 15:24:56 -05004130
4131 if (atomic_read(&buffer_b->record_disabled))
Steven Rostedt554f7862009-03-11 22:00:13 -04004132 goto out;
Steven Rostedt97b17ef2009-01-21 15:24:56 -05004133
Steven Rostedt97b17ef2009-01-21 15:24:56 -05004134 if (atomic_read(&cpu_buffer_a->record_disabled))
Steven Rostedt554f7862009-03-11 22:00:13 -04004135 goto out;
Steven Rostedt97b17ef2009-01-21 15:24:56 -05004136
4137 if (atomic_read(&cpu_buffer_b->record_disabled))
Steven Rostedt554f7862009-03-11 22:00:13 -04004138 goto out;
Steven Rostedt97b17ef2009-01-21 15:24:56 -05004139
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004140 /*
4141 * We can't do a synchronize_sched here because this
4142 * function can be called in atomic context.
4143 * Normally this will be called from the same CPU as cpu.
4144 * If not it's up to the caller to protect this.
4145 */
4146 atomic_inc(&cpu_buffer_a->record_disabled);
4147 atomic_inc(&cpu_buffer_b->record_disabled);
4148
Steven Rostedt98277992009-09-02 10:56:15 -04004149 ret = -EBUSY;
4150 if (local_read(&cpu_buffer_a->committing))
4151 goto out_dec;
4152 if (local_read(&cpu_buffer_b->committing))
4153 goto out_dec;
4154
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004155 buffer_a->buffers[cpu] = cpu_buffer_b;
4156 buffer_b->buffers[cpu] = cpu_buffer_a;
4157
4158 cpu_buffer_b->buffer = buffer_a;
4159 cpu_buffer_a->buffer = buffer_b;
4160
Steven Rostedt98277992009-09-02 10:56:15 -04004161 ret = 0;
4162
4163out_dec:
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004164 atomic_dec(&cpu_buffer_a->record_disabled);
4165 atomic_dec(&cpu_buffer_b->record_disabled);
Steven Rostedt554f7862009-03-11 22:00:13 -04004166out:
Steven Rostedt554f7862009-03-11 22:00:13 -04004167 return ret;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004168}
Robert Richterc4f50182008-12-11 16:49:22 +01004169EXPORT_SYMBOL_GPL(ring_buffer_swap_cpu);
Steven Rostedt85bac322009-09-04 14:24:40 -04004170#endif /* CONFIG_RING_BUFFER_ALLOW_SWAP */
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04004171
Steven Rostedt8789a9e2008-12-02 15:34:07 -05004172/**
4173 * ring_buffer_alloc_read_page - allocate a page to read from buffer
4174 * @buffer: the buffer to allocate for.
4175 *
4176 * This function is used in conjunction with ring_buffer_read_page.
4177 * When reading a full page from the ring buffer, these functions
4178 * can be used to speed up the process. The calling function should
4179 * allocate a few pages first with this function. Then when it
4180 * needs to get pages from the ring buffer, it passes the result
4181 * of this function into ring_buffer_read_page, which will swap
4182 * the page that was allocated, with the read page of the buffer.
4183 *
4184 * Returns:
4185 * The page allocated, or NULL on error.
4186 */
Vaibhav Nagarnaik7ea59062011-05-03 17:56:42 -07004187void *ring_buffer_alloc_read_page(struct ring_buffer *buffer, int cpu)
Steven Rostedt8789a9e2008-12-02 15:34:07 -05004188{
Steven Rostedt044fa782008-12-02 23:50:03 -05004189 struct buffer_data_page *bpage;
Vaibhav Nagarnaik7ea59062011-05-03 17:56:42 -07004190 struct page *page;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05004191
Vaibhav Nagarnaikd7ec4bf2011-06-07 17:01:42 -07004192 page = alloc_pages_node(cpu_to_node(cpu),
4193 GFP_KERNEL | __GFP_NORETRY, 0);
Vaibhav Nagarnaik7ea59062011-05-03 17:56:42 -07004194 if (!page)
Steven Rostedt8789a9e2008-12-02 15:34:07 -05004195 return NULL;
4196
Vaibhav Nagarnaik7ea59062011-05-03 17:56:42 -07004197 bpage = page_address(page);
Steven Rostedt8789a9e2008-12-02 15:34:07 -05004198
Steven Rostedtef7a4a12009-03-03 00:27:49 -05004199 rb_init_page(bpage);
4200
Steven Rostedt044fa782008-12-02 23:50:03 -05004201 return bpage;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05004202}
Steven Rostedtd6ce96d2009-05-05 01:15:24 -04004203EXPORT_SYMBOL_GPL(ring_buffer_alloc_read_page);
Steven Rostedt8789a9e2008-12-02 15:34:07 -05004204
4205/**
4206 * ring_buffer_free_read_page - free an allocated read page
4207 * @buffer: the buffer the page was allocate for
4208 * @data: the page to free
4209 *
4210 * Free a page allocated from ring_buffer_alloc_read_page.
4211 */
4212void ring_buffer_free_read_page(struct ring_buffer *buffer, void *data)
4213{
4214 free_page((unsigned long)data);
4215}
Steven Rostedtd6ce96d2009-05-05 01:15:24 -04004216EXPORT_SYMBOL_GPL(ring_buffer_free_read_page);
Steven Rostedt8789a9e2008-12-02 15:34:07 -05004217
4218/**
4219 * ring_buffer_read_page - extract a page from the ring buffer
4220 * @buffer: buffer to extract from
4221 * @data_page: the page to use allocated from ring_buffer_alloc_read_page
Steven Rostedtef7a4a12009-03-03 00:27:49 -05004222 * @len: amount to extract
Steven Rostedt8789a9e2008-12-02 15:34:07 -05004223 * @cpu: the cpu of the buffer to extract
4224 * @full: should the extraction only happen when the page is full.
4225 *
4226 * This function will pull out a page from the ring buffer and consume it.
4227 * @data_page must be the address of the variable that was returned
4228 * from ring_buffer_alloc_read_page. This is because the page might be used
4229 * to swap with a page in the ring buffer.
4230 *
4231 * for example:
Lai Jiangshanb85fa012009-02-09 14:21:14 +08004232 * rpage = ring_buffer_alloc_read_page(buffer);
Steven Rostedt8789a9e2008-12-02 15:34:07 -05004233 * if (!rpage)
4234 * return error;
Steven Rostedtef7a4a12009-03-03 00:27:49 -05004235 * ret = ring_buffer_read_page(buffer, &rpage, len, cpu, 0);
Lai Jiangshan667d2412009-02-09 14:21:17 +08004236 * if (ret >= 0)
4237 * process_page(rpage, ret);
Steven Rostedt8789a9e2008-12-02 15:34:07 -05004238 *
4239 * When @full is set, the function will not return true unless
4240 * the writer is off the reader page.
4241 *
4242 * Note: it is up to the calling functions to handle sleeps and wakeups.
4243 * The ring buffer can be used anywhere in the kernel and can not
4244 * blindly call wake_up. The layer that uses the ring buffer must be
4245 * responsible for that.
4246 *
4247 * Returns:
Lai Jiangshan667d2412009-02-09 14:21:17 +08004248 * >=0 if data has been transferred, returns the offset of consumed data.
4249 * <0 if no data has been transferred.
Steven Rostedt8789a9e2008-12-02 15:34:07 -05004250 */
4251int ring_buffer_read_page(struct ring_buffer *buffer,
Steven Rostedtef7a4a12009-03-03 00:27:49 -05004252 void **data_page, size_t len, int cpu, int full)
Steven Rostedt8789a9e2008-12-02 15:34:07 -05004253{
4254 struct ring_buffer_per_cpu *cpu_buffer = buffer->buffers[cpu];
4255 struct ring_buffer_event *event;
Steven Rostedt044fa782008-12-02 23:50:03 -05004256 struct buffer_data_page *bpage;
Steven Rostedtef7a4a12009-03-03 00:27:49 -05004257 struct buffer_page *reader;
Steven Rostedtff0ff842010-03-31 22:11:42 -04004258 unsigned long missed_events;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05004259 unsigned long flags;
Steven Rostedtef7a4a12009-03-03 00:27:49 -05004260 unsigned int commit;
Lai Jiangshan667d2412009-02-09 14:21:17 +08004261 unsigned int read;
Steven Rostedt4f3640f2009-03-03 23:52:42 -05004262 u64 save_timestamp;
Lai Jiangshan667d2412009-02-09 14:21:17 +08004263 int ret = -1;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05004264
Steven Rostedt554f7862009-03-11 22:00:13 -04004265 if (!cpumask_test_cpu(cpu, buffer->cpumask))
4266 goto out;
4267
Steven Rostedt474d32b2009-03-03 19:51:40 -05004268 /*
4269 * If len is not big enough to hold the page header, then
4270 * we can not copy anything.
4271 */
4272 if (len <= BUF_PAGE_HDR_SIZE)
Steven Rostedt554f7862009-03-11 22:00:13 -04004273 goto out;
Steven Rostedt474d32b2009-03-03 19:51:40 -05004274
4275 len -= BUF_PAGE_HDR_SIZE;
4276
Steven Rostedt8789a9e2008-12-02 15:34:07 -05004277 if (!data_page)
Steven Rostedt554f7862009-03-11 22:00:13 -04004278 goto out;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05004279
Steven Rostedt044fa782008-12-02 23:50:03 -05004280 bpage = *data_page;
4281 if (!bpage)
Steven Rostedt554f7862009-03-11 22:00:13 -04004282 goto out;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05004283
Thomas Gleixner5389f6f2009-07-25 17:13:33 +02004284 raw_spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
Steven Rostedt8789a9e2008-12-02 15:34:07 -05004285
Steven Rostedtef7a4a12009-03-03 00:27:49 -05004286 reader = rb_get_reader_page(cpu_buffer);
4287 if (!reader)
Steven Rostedt554f7862009-03-11 22:00:13 -04004288 goto out_unlock;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05004289
Steven Rostedtef7a4a12009-03-03 00:27:49 -05004290 event = rb_reader_event(cpu_buffer);
Lai Jiangshan667d2412009-02-09 14:21:17 +08004291
Steven Rostedtef7a4a12009-03-03 00:27:49 -05004292 read = reader->read;
4293 commit = rb_page_commit(reader);
4294
Steven Rostedt66a8cb92010-03-31 13:21:56 -04004295 /* Check if any events were dropped */
Steven Rostedtff0ff842010-03-31 22:11:42 -04004296 missed_events = cpu_buffer->lost_events;
Steven Rostedt66a8cb92010-03-31 13:21:56 -04004297
Steven Rostedt8789a9e2008-12-02 15:34:07 -05004298 /*
Steven Rostedt474d32b2009-03-03 19:51:40 -05004299 * If this page has been partially read or
4300 * if len is not big enough to read the rest of the page or
4301 * a writer is still on the page, then
4302 * we must copy the data from the page to the buffer.
4303 * Otherwise, we can simply swap the page with the one passed in.
Steven Rostedt8789a9e2008-12-02 15:34:07 -05004304 */
Steven Rostedt474d32b2009-03-03 19:51:40 -05004305 if (read || (len < (commit - read)) ||
Steven Rostedtef7a4a12009-03-03 00:27:49 -05004306 cpu_buffer->reader_page == cpu_buffer->commit_page) {
Lai Jiangshan667d2412009-02-09 14:21:17 +08004307 struct buffer_data_page *rpage = cpu_buffer->reader_page->page;
Steven Rostedt474d32b2009-03-03 19:51:40 -05004308 unsigned int rpos = read;
4309 unsigned int pos = 0;
Steven Rostedtef7a4a12009-03-03 00:27:49 -05004310 unsigned int size;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05004311
4312 if (full)
Steven Rostedt554f7862009-03-11 22:00:13 -04004313 goto out_unlock;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05004314
Steven Rostedtef7a4a12009-03-03 00:27:49 -05004315 if (len > (commit - read))
4316 len = (commit - read);
4317
Steven Rostedt69d1b832010-10-07 18:18:05 -04004318 /* Always keep the time extend and data together */
4319 size = rb_event_ts_length(event);
Steven Rostedtef7a4a12009-03-03 00:27:49 -05004320
4321 if (len < size)
Steven Rostedt554f7862009-03-11 22:00:13 -04004322 goto out_unlock;
Steven Rostedtef7a4a12009-03-03 00:27:49 -05004323
Steven Rostedt4f3640f2009-03-03 23:52:42 -05004324 /* save the current timestamp, since the user will need it */
4325 save_timestamp = cpu_buffer->read_stamp;
4326
Steven Rostedtef7a4a12009-03-03 00:27:49 -05004327 /* Need to copy one event at a time */
4328 do {
David Sharpe1e35922010-12-22 16:38:24 -08004329 /* We need the size of one event, because
4330 * rb_advance_reader only advances by one event,
4331 * whereas rb_event_ts_length may include the size of
4332 * one or two events.
4333 * We have already ensured there's enough space if this
4334 * is a time extend. */
4335 size = rb_event_length(event);
Steven Rostedt474d32b2009-03-03 19:51:40 -05004336 memcpy(bpage->data + pos, rpage->data + rpos, size);
Steven Rostedtef7a4a12009-03-03 00:27:49 -05004337
4338 len -= size;
4339
4340 rb_advance_reader(cpu_buffer);
Steven Rostedt474d32b2009-03-03 19:51:40 -05004341 rpos = reader->read;
4342 pos += size;
Steven Rostedtef7a4a12009-03-03 00:27:49 -05004343
Huang Ying18fab912010-07-28 14:14:01 +08004344 if (rpos >= commit)
4345 break;
4346
Steven Rostedtef7a4a12009-03-03 00:27:49 -05004347 event = rb_reader_event(cpu_buffer);
Steven Rostedt69d1b832010-10-07 18:18:05 -04004348 /* Always keep the time extend and data together */
4349 size = rb_event_ts_length(event);
David Sharpe1e35922010-12-22 16:38:24 -08004350 } while (len >= size);
Lai Jiangshan667d2412009-02-09 14:21:17 +08004351
4352 /* update bpage */
Steven Rostedtef7a4a12009-03-03 00:27:49 -05004353 local_set(&bpage->commit, pos);
Steven Rostedt4f3640f2009-03-03 23:52:42 -05004354 bpage->time_stamp = save_timestamp;
Steven Rostedtef7a4a12009-03-03 00:27:49 -05004355
Steven Rostedt474d32b2009-03-03 19:51:40 -05004356 /* we copied everything to the beginning */
4357 read = 0;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05004358 } else {
Steven Rostedtafbab762009-05-01 19:40:05 -04004359 /* update the entry counter */
Steven Rostedt77ae3652009-03-27 11:00:29 -04004360 cpu_buffer->read += rb_page_entries(reader);
Vaibhav Nagarnaikc64e1482011-08-16 14:46:16 -07004361 cpu_buffer->read_bytes += BUF_PAGE_SIZE;
Steven Rostedtafbab762009-05-01 19:40:05 -04004362
Steven Rostedt8789a9e2008-12-02 15:34:07 -05004363 /* swap the pages */
Steven Rostedt044fa782008-12-02 23:50:03 -05004364 rb_init_page(bpage);
Steven Rostedtef7a4a12009-03-03 00:27:49 -05004365 bpage = reader->page;
4366 reader->page = *data_page;
4367 local_set(&reader->write, 0);
Steven Rostedt778c55d2009-05-01 18:44:45 -04004368 local_set(&reader->entries, 0);
Steven Rostedtef7a4a12009-03-03 00:27:49 -05004369 reader->read = 0;
Steven Rostedt044fa782008-12-02 23:50:03 -05004370 *data_page = bpage;
Steven Rostedtff0ff842010-03-31 22:11:42 -04004371
4372 /*
4373 * Use the real_end for the data size,
4374 * This gives us a chance to store the lost events
4375 * on the page.
4376 */
4377 if (reader->real_end)
4378 local_set(&bpage->commit, reader->real_end);
Steven Rostedt8789a9e2008-12-02 15:34:07 -05004379 }
Lai Jiangshan667d2412009-02-09 14:21:17 +08004380 ret = read;
Steven Rostedt8789a9e2008-12-02 15:34:07 -05004381
Steven Rostedt66a8cb92010-03-31 13:21:56 -04004382 cpu_buffer->lost_events = 0;
Steven Rostedt2711ca22010-05-21 13:32:26 -04004383
4384 commit = local_read(&bpage->commit);
Steven Rostedt66a8cb92010-03-31 13:21:56 -04004385 /*
4386 * Set a flag in the commit field if we lost events
4387 */
Steven Rostedtff0ff842010-03-31 22:11:42 -04004388 if (missed_events) {
Steven Rostedtff0ff842010-03-31 22:11:42 -04004389 /* If there is room at the end of the page to save the
4390 * missed events, then record it there.
4391 */
4392 if (BUF_PAGE_SIZE - commit >= sizeof(missed_events)) {
4393 memcpy(&bpage->data[commit], &missed_events,
4394 sizeof(missed_events));
4395 local_add(RB_MISSED_STORED, &bpage->commit);
Steven Rostedt2711ca22010-05-21 13:32:26 -04004396 commit += sizeof(missed_events);
Steven Rostedtff0ff842010-03-31 22:11:42 -04004397 }
Steven Rostedt66a8cb92010-03-31 13:21:56 -04004398 local_add(RB_MISSED_EVENTS, &bpage->commit);
Steven Rostedtff0ff842010-03-31 22:11:42 -04004399 }
Steven Rostedt66a8cb92010-03-31 13:21:56 -04004400
Steven Rostedt2711ca22010-05-21 13:32:26 -04004401 /*
4402 * This page may be off to user land. Zero it out here.
4403 */
4404 if (commit < BUF_PAGE_SIZE)
4405 memset(&bpage->data[commit], 0, BUF_PAGE_SIZE - commit);
4406
Steven Rostedt554f7862009-03-11 22:00:13 -04004407 out_unlock:
Thomas Gleixner5389f6f2009-07-25 17:13:33 +02004408 raw_spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
Steven Rostedt8789a9e2008-12-02 15:34:07 -05004409
Steven Rostedt554f7862009-03-11 22:00:13 -04004410 out:
Steven Rostedt8789a9e2008-12-02 15:34:07 -05004411 return ret;
4412}
Steven Rostedtd6ce96d2009-05-05 01:15:24 -04004413EXPORT_SYMBOL_GPL(ring_buffer_read_page);
Steven Rostedt8789a9e2008-12-02 15:34:07 -05004414
Steven Rostedt59222ef2009-03-12 11:46:03 -04004415#ifdef CONFIG_HOTPLUG_CPU
Frederic Weisbecker09c9e842009-03-21 04:33:36 +01004416static int rb_cpu_notify(struct notifier_block *self,
4417 unsigned long action, void *hcpu)
Steven Rostedt554f7862009-03-11 22:00:13 -04004418{
4419 struct ring_buffer *buffer =
4420 container_of(self, struct ring_buffer, cpu_notify);
4421 long cpu = (long)hcpu;
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08004422 int cpu_i, nr_pages_same;
4423 unsigned int nr_pages;
Steven Rostedt554f7862009-03-11 22:00:13 -04004424
4425 switch (action) {
4426 case CPU_UP_PREPARE:
4427 case CPU_UP_PREPARE_FROZEN:
Rusty Russell3f237a72009-06-12 21:15:30 +09304428 if (cpumask_test_cpu(cpu, buffer->cpumask))
Steven Rostedt554f7862009-03-11 22:00:13 -04004429 return NOTIFY_OK;
4430
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08004431 nr_pages = 0;
4432 nr_pages_same = 1;
4433 /* check if all cpu sizes are same */
4434 for_each_buffer_cpu(buffer, cpu_i) {
4435 /* fill in the size from first enabled cpu */
4436 if (nr_pages == 0)
4437 nr_pages = buffer->buffers[cpu_i]->nr_pages;
4438 if (nr_pages != buffer->buffers[cpu_i]->nr_pages) {
4439 nr_pages_same = 0;
4440 break;
4441 }
4442 }
4443 /* allocate minimum pages, user can later expand it */
4444 if (!nr_pages_same)
4445 nr_pages = 2;
Steven Rostedt554f7862009-03-11 22:00:13 -04004446 buffer->buffers[cpu] =
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08004447 rb_allocate_cpu_buffer(buffer, nr_pages, cpu);
Steven Rostedt554f7862009-03-11 22:00:13 -04004448 if (!buffer->buffers[cpu]) {
4449 WARN(1, "failed to allocate ring buffer on CPU %ld\n",
4450 cpu);
4451 return NOTIFY_OK;
4452 }
4453 smp_wmb();
Rusty Russell3f237a72009-06-12 21:15:30 +09304454 cpumask_set_cpu(cpu, buffer->cpumask);
Steven Rostedt554f7862009-03-11 22:00:13 -04004455 break;
4456 case CPU_DOWN_PREPARE:
4457 case CPU_DOWN_PREPARE_FROZEN:
4458 /*
4459 * Do nothing.
4460 * If we were to free the buffer, then the user would
4461 * lose any trace that was in the buffer.
4462 */
4463 break;
4464 default:
4465 break;
4466 }
4467 return NOTIFY_OK;
4468}
4469#endif